From d266d40deaaf0fc7234a06217e121a158c980085 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 9 Feb 2017 12:50:05 -0200 Subject: [PATCH 0001/1145] error when calling close method without arguments (e.g., |io.stdin.close()|) --- liolib.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/liolib.c b/liolib.c index 8491edca39..3fa874613d 100644 --- a/liolib.c +++ b/liolib.c @@ -1,5 +1,5 @@ /* -** $Id: liolib.c,v 2.150 2016/09/01 16:14:56 roberto Exp roberto $ +** $Id: liolib.c,v 2.151 2016/12/20 18:37:00 roberto Exp roberto $ ** Standard I/O (and system) library ** See Copyright Notice in lua.h */ @@ -206,11 +206,16 @@ static int aux_close (lua_State *L) { } +static int f_close (lua_State *L) { + tofile(L); /* make sure argument is an open stream */ + return aux_close(L); +} + + static int io_close (lua_State *L) { if (lua_isnone(L, 1)) /* no argument? */ lua_getfield(L, LUA_REGISTRYINDEX, IO_OUTPUT); /* use standard output */ - tofile(L); /* make sure argument is an open stream */ - return aux_close(L); + return f_close(L); } @@ -712,7 +717,7 @@ static const luaL_Reg iolib[] = { ** methods for file handles */ static const luaL_Reg flib[] = { - {"close", io_close}, + {"close", f_close}, {"flush", f_flush}, {"lines", f_lines}, {"read", f_read}, From e6c1e6005a9346d378e004a6d6e7fd98c7ee191b Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 15 Feb 2017 16:52:13 -0200 Subject: [PATCH 0002/1145] comments about gray lists --- lstate.h | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/lstate.h b/lstate.h index 9985545e47..67efa7f2d6 100644 --- a/lstate.h +++ b/lstate.h @@ -1,5 +1,5 @@ /* -** $Id: lstate.h,v 2.132 2016/10/19 12:31:42 roberto Exp roberto $ +** $Id: lstate.h,v 2.133 2016/12/22 13:08:50 roberto Exp roberto $ ** Global State ** See Copyright Notice in lua.h */ @@ -26,6 +26,24 @@ ** 'tobefnz': all objects ready to be finalized; ** 'fixedgc': all objects that are not to be collected (currently ** only small strings, such as reserved words). +** +** Moreover, there is another set of lists that control gray objects. +** These lists are linked by fields 'gclist'. (All objects that +** can become gray have such a field. The field is not the same +** in all objects, but it always has this name.) Any gray object +** must belong to one of these lists, and all objects in these lists +** must be gray: +** +** 'gray': regular gray objects, still waiting to be visited. +** 'grayagain': objects that must be revisited at the atomic phase. +** That includes +** - black objects got in a write barrier; +** - all kinds of weak tables during propagation phase; +** - all threads. +** 'weak': tables with weak values to be cleared; +** 'ephemeron': ephemeron tables with white->white entries; +** 'allweak': tables with weak keys and/or weak values to be cleared. +** The last three lists are used only during the atomic phase. */ From f5f3df3bd17fb3489bbd26ab39fe1580a8dbf9c9 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 23 Feb 2017 18:07:34 -0300 Subject: [PATCH 0003/1145] generational collection: new attempt (still incomplete) --- lapi.c | 10 ++- lbaselib.c | 6 +- lgc.c | 183 ++++++++++++++++++++++++++++++++++++++++++++++------- lgc.h | 9 ++- lstate.c | 3 +- lstate.h | 8 ++- ltests.c | 12 ++-- lua.h | 4 +- 8 files changed, 195 insertions(+), 40 deletions(-) diff --git a/lapi.c b/lapi.c index 1c4d07ddaf..494fce759f 100644 --- a/lapi.c +++ b/lapi.c @@ -1,5 +1,5 @@ /* -** $Id: lapi.c,v 2.258 2016/01/05 16:07:21 roberto Exp roberto $ +** $Id: lapi.c,v 2.259 2016/02/29 14:27:14 roberto Exp roberto $ ** Lua API ** See Copyright Notice in lua.h */ @@ -1097,6 +1097,14 @@ LUA_API int lua_gc (lua_State *L, int what, int data) { res = g->gcrunning; break; } + case LUA_GCGEN: { + luaC_changemode(L, KGC_GEN); + break; + } + case LUA_GCINC: { + luaC_changemode(L, KGC_NORMAL); + break; + } default: res = -1; /* invalid option */ } lua_unlock(L); diff --git a/lbaselib.c b/lbaselib.c index 98602952c4..04fdd5c2ef 100644 --- a/lbaselib.c +++ b/lbaselib.c @@ -1,5 +1,5 @@ /* -** $Id: lbaselib.c,v 1.313 2016/04/11 19:18:40 roberto Exp roberto $ +** $Id: lbaselib.c,v 1.314 2016/09/05 19:06:34 roberto Exp roberto $ ** Basic library ** See Copyright Notice in lua.h */ @@ -173,10 +173,10 @@ static int luaB_rawset (lua_State *L) { static int luaB_collectgarbage (lua_State *L) { static const char *const opts[] = {"stop", "restart", "collect", "count", "step", "setpause", "setstepmul", - "isrunning", NULL}; + "isrunning", "generational", "incremental", NULL}; static const int optsnum[] = {LUA_GCSTOP, LUA_GCRESTART, LUA_GCCOLLECT, LUA_GCCOUNT, LUA_GCSTEP, LUA_GCSETPAUSE, LUA_GCSETSTEPMUL, - LUA_GCISRUNNING}; + LUA_GCISRUNNING, LUA_GCGEN, LUA_GCINC}; int o = optsnum[luaL_checkoption(L, 1, "collect", opts)]; int ex = (int)luaL_optinteger(L, 2, 0); int res = lua_gc(L, o, ex); diff --git a/lgc.c b/lgc.c index de3f2a2155..44e5dc31d5 100644 --- a/lgc.c +++ b/lgc.c @@ -1,5 +1,5 @@ /* -** $Id: lgc.c,v 2.214 2016/11/07 12:38:35 roberto Exp roberto $ +** $Id: lgc.c,v 2.215 2016/12/22 13:08:50 roberto Exp roberto $ ** Garbage Collector ** See Copyright Notice in lua.h */ @@ -59,11 +59,10 @@ #define PAUSEADJ 100 -/* -** 'makewhite' erases all color bits then sets only the current white -** bit -*/ +/* mask to erase all color bits */ #define maskcolors (~(bitmask(BLACKBIT) | WHITEBITS)) + +/* macro to erase all color bits then sets only the current white bit */ #define makewhite(g,x) \ (x->marked = cast_byte((x->marked & maskcolors) | luaC_white(g))) @@ -92,6 +91,7 @@ #define markobjectN(g,t) { if (t) markobject(g,t); } static void reallymarkobject (global_State *g, GCObject *o); +static l_mem atomic (lua_State *L); /* @@ -155,8 +155,11 @@ static int iscleared (global_State *g, const TValue *o) { void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v) { global_State *g = G(L); lua_assert(isblack(o) && iswhite(v) && !isdead(g, v) && !isdead(g, o)); - if (keepinvariant(g)) /* must keep invariant? */ + if (keepinvariant(g)) { /* must keep invariant? */ reallymarkobject(g, v); /* restore invariant */ + if (isold(o)) + l_setbit((v)->marked, OLDBIT); + } else { /* sweep phase */ lua_assert(issweepphase(g)); makewhite(g, o); /* mark main obj. as white to avoid other barriers */ @@ -186,8 +189,10 @@ void luaC_upvalbarrier_ (lua_State *L, UpVal *uv) { global_State *g = G(L); GCObject *o = gcvalue(uv->v); lua_assert(!upisopen(uv)); /* ensured by macro luaC_upvalbarrier */ - if (keepinvariant(g)) + if (keepinvariant(g)) { markobject(g, o); + l_setbit((o)->marked, OLDBIT); + } } @@ -922,6 +927,121 @@ void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt) { /* }====================================================== */ +/* +** {====================================================== +** Generational Collector +** ======================================================= +*/ + + +#if 0 +static int count (GCObject *p, GCObject *limit) { + int res = 0; + for (; p != NULL && p != limit; p = p->next) + res++; + return res; +} +#endif + + +static GCObject **sweepgen (lua_State *L, GCObject **p, GCObject *limit, + int zeromask, int onemask) { + global_State *g = G(L); + int ow = otherwhite(g); + GCObject *curr; + while ((curr = *p) != limit) { + int marked = curr->marked; + if (isdeadm(ow, marked)) { /* is 'curr' dead? */ + lua_assert(!isold(curr)); + *p = curr->next; /* remove 'curr' from list */ + freeobj(L, curr); /* erase 'curr' */ + } + else { /* correct mark */ + if (!isold(curr)) /* don't change old objects */ + curr->marked = cast_byte((marked & zeromask) | onemask); + p = &curr->next; /* go to next element */ + } + } + return p; +} + + +static void startgencycle (lua_State *L, global_State *g) { + propagateall(g); + atomic(L); +} + + +static void finishgencycle (lua_State *L, global_State *g, int mask) { + sweepgen(L, &g->finobj, NULL, ~0, mask); + sweepgen(L, &g->tobefnz, NULL, ~0, mask); + checkSizes(L, g); + g->gcstate = GCSpropagate; /* skip restart */ + callallpendingfinalizers(L); +} + + +static void youngcollection (lua_State *L, global_State *g) { + GCObject **psurvival; + lua_assert(g->gcstate == GCSpropagate); + startgencycle(L, g); + /* sweep nursery */ + psurvival = sweepgen(L, &g->allgc, g->survival, maskcolors, luaC_white(g)); + lua_assert(*psurvival == g->survival); + /* sweep 'survival' list, making elements old */ + sweepgen(L, psurvival, g->old, ~0, bitmask(OLDBIT)); + /* incorporate 'survival' list into old list */ + g->old = *psurvival; + /* surviving young objects go to 'survival' list */ + g->survival = g->allgc; + finishgencycle(L, g, 0); +lua_checkmemory(L); +} + + +static void entergen (lua_State *L, global_State *g) { +lua_checkmemory(L); + lua_assert(g->old == NULL && g->survival == NULL); + luaC_runtilstate(L, bitmask(GCSpause)); /* prepare to start a new cycle */ + luaC_runtilstate(L, bitmask(GCSpropagate)); /* start new cycle */ + startgencycle(L, g); + /* sweep all ellements making them old */ + sweepgen(L, &g->allgc, g->survival, ~0, bitmask(OLDBIT)); + /* everything alive now is old; 'survival' is empty */ + g->old = g->survival = g->allgc; + finishgencycle(L, g, bitmask(OLDBIT)); +lua_checkmemory(L); +} + + +void luaC_changemode (lua_State *L, int newmode) { + global_State *g = G(L); + if (newmode != g->gckind) { /* otherwise, nothing to be done */ + if (newmode == KGC_GEN) /* entering generational mode? */ + entergen(L, g); + else { /* entering incremental mode */ +lua_checkmemory(L); + youngcollection(L, g); + g->old = g->survival = NULL; +lua_checkmemory(L); + } + g->gckind = newmode; + } +} + + +static void genstep (lua_State *L, global_State *g) { + lu_mem mem; + youngcollection(L, g); + mem = gettotalbytes(g); + luaE_setdebt(g, -((mem / 100) * 20)); +} + + + + +/* }====================================================== */ + /* ** {====================================================== @@ -963,17 +1083,24 @@ static void entersweep (lua_State *L) { } +static void deletealllist (lua_State *L, GCObject *p) { + while (p) { + GCObject *next = p->next; + freeobj(L, p); + p = next; + } +} + + void luaC_freeallobjects (lua_State *L) { global_State *g = G(L); separatetobefnz(g, 1); /* separate all objects with finalizers */ lua_assert(g->finobj == NULL); callallpendingfinalizers(L); lua_assert(g->tobefnz == NULL); - g->currentwhite = WHITEBITS; /* this "white" makes all objects look dead */ - g->gckind = KGC_NORMAL; - sweepwholelist(L, &g->finobj); - sweepwholelist(L, &g->allgc); - sweepwholelist(L, &g->fixedgc); /* collect fixed objects */ + deletealllist(L, g->finobj); + deletealllist(L, g->allgc); + deletealllist(L, g->fixedgc); /* collect fixed objects */ lua_assert(g->strt.nuse == 0); } @@ -996,6 +1123,7 @@ static l_mem atomic (lua_State *L) { propagateall(g); /* propagate changes */ work = g->GCmemtrav; /* stop counting (do not recount 'grayagain') */ g->gray = grayagain; + g->grayagain = NULL; propagateall(g); /* traverse 'grayagain' list */ g->GCmemtrav = 0; /* restart counting */ convergeephemerons(g); @@ -1052,10 +1180,10 @@ static lu_mem singlestep (lua_State *L) { } case GCSpropagate: { g->GCmemtrav = 0; - lua_assert(g->gray); - propagatemark(g); - if (g->gray == NULL) /* no more gray objects? */ + if (g->gray == NULL) /* no more gray objects? */ g->gcstate = GCSatomic; /* finish propagate phase */ + else + propagatemark(g); /* traverse one gray object */ return g->GCmemtrav; /* memory traversed in this step */ } case GCSatomic: { @@ -1123,15 +1251,10 @@ static l_mem getdebt (global_State *g) { } /* -** performs a basic GC step when collector is running +** performs a basic incremental step */ -void luaC_step (lua_State *L) { - global_State *g = G(L); - l_mem debt = getdebt(g); /* GC deficit (be paid now) */ - if (!g->gcrunning) { /* not running? */ - luaE_setdebt(g, -GCSTEPSIZE * 10); /* avoid being called too often */ - return; - } +static void incstep (lua_State *L, global_State *g) { + l_mem debt = getdebt(g); /* GC deficit (to be paid now) */ do { /* repeat until pause or enough "credit" (negative debt) */ lu_mem work = singlestep(L); /* perform one single step */ debt -= work; @@ -1145,6 +1268,19 @@ void luaC_step (lua_State *L) { } } +/* +** performs a basic GC step when collector is running +*/ +void luaC_step (lua_State *L) { + global_State *g = G(L); + if (!g->gcrunning) /* not running? */ + luaE_setdebt(g, -GCSTEPSIZE * 10); /* avoid being called too often */ + else if (g->gckind == KGC_NORMAL) + incstep(L, g); + else + genstep(L, g); +} + /* ** Performs a full GC cycle; if 'isemergency', set a flag to avoid @@ -1164,7 +1300,6 @@ void luaC_fullgc (lua_State *L, int isemergency) { } /* finish any pending sweep phase to start a new cycle */ luaC_runtilstate(L, bitmask(GCSpause)); - luaC_runtilstate(L, ~bitmask(GCSpause)); /* start new collection */ luaC_runtilstate(L, bitmask(GCScallfin)); /* run up to finalizers */ /* estimate must be correct after a full GC cycle */ lua_assert(g->GCestimate == gettotalbytes(g)); diff --git a/lgc.h b/lgc.h index 75f24bc03d..e75ff85ce2 100644 --- a/lgc.h +++ b/lgc.h @@ -1,5 +1,5 @@ /* -** $Id: lgc.h,v 2.90 2015/10/21 18:15:15 roberto Exp roberto $ +** $Id: lgc.h,v 2.91 2015/12/21 13:02:14 roberto Exp roberto $ ** Garbage Collector ** See Copyright Notice in lua.h */ @@ -79,7 +79,8 @@ #define WHITE1BIT 1 /* object is white (type 1) */ #define BLACKBIT 2 /* object is black */ #define FINALIZEDBIT 3 /* object has been marked for finalization */ -/* bit 7 is currently used by tests (luaL_checkmemory) */ +#define OLDBIT 4 /* object is old (gen. mode) */ +#define TESTGRAYBIT 7 /* used by tests (luaL_checkmemory) */ #define WHITEBITS bit2mask(WHITE0BIT, WHITE1BIT) @@ -88,11 +89,12 @@ #define isblack(x) testbit((x)->marked, BLACKBIT) #define isgray(x) /* neither white nor black */ \ (!testbits((x)->marked, WHITEBITS | bitmask(BLACKBIT))) +#define isold(x) testbit((x)->marked, OLDBIT) #define tofinalize(x) testbit((x)->marked, FINALIZEDBIT) #define otherwhite(g) ((g)->currentwhite ^ WHITEBITS) -#define isdeadm(ow,m) (!(((m) ^ WHITEBITS) & (ow))) +#define isdeadm(ow,m) ((m) & (ow)) #define isdead(g,v) isdeadm(otherwhite(g), (v)->marked) #define changewhite(x) ((x)->marked ^= WHITEBITS) @@ -142,6 +144,7 @@ LUAI_FUNC void luaC_barrierback_ (lua_State *L, Table *o); LUAI_FUNC void luaC_upvalbarrier_ (lua_State *L, UpVal *uv); LUAI_FUNC void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt); LUAI_FUNC void luaC_upvdeccount (lua_State *L, UpVal *uv); +LUAI_FUNC void luaC_changemode (lua_State *L, int newmode); #endif diff --git a/lstate.c b/lstate.c index 11299d3743..4900d72423 100644 --- a/lstate.c +++ b/lstate.c @@ -1,5 +1,5 @@ /* -** $Id: lstate.c,v 2.132 2015/11/02 16:01:41 roberto Exp roberto $ +** $Id: lstate.c,v 2.133 2015/11/13 12:16:51 roberto Exp roberto $ ** Global State ** See Copyright Notice in lua.h */ @@ -319,6 +319,7 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { g->gcstate = GCSpause; g->gckind = KGC_NORMAL; g->allgc = g->finobj = g->tobefnz = g->fixedgc = NULL; + g->survival = g->old = NULL; g->sweepgc = NULL; g->gray = g->grayagain = NULL; g->weak = g->ephemeron = g->allweak = NULL; diff --git a/lstate.h b/lstate.h index 67efa7f2d6..51128db193 100644 --- a/lstate.h +++ b/lstate.h @@ -1,5 +1,5 @@ /* -** $Id: lstate.h,v 2.133 2016/12/22 13:08:50 roberto Exp roberto $ +** $Id: lstate.h,v 2.134 2017/02/15 18:52:13 roberto Exp roberto $ ** Global State ** See Copyright Notice in lua.h */ @@ -70,7 +70,8 @@ struct lua_longjmp; /* defined in ldo.c */ /* kinds of Garbage Collection */ #define KGC_NORMAL 0 -#define KGC_EMERGENCY 1 /* gc was forced by an allocation failure */ +#define KGC_GEN 1 /* generational gc */ +#define KGC_EMERGENCY 2 /* gc was forced by an allocation failure */ typedef struct stringtable { @@ -158,6 +159,9 @@ typedef struct global_State { GCObject *allweak; /* list of all-weak tables */ GCObject *tobefnz; /* list of userdata to be GC */ GCObject *fixedgc; /* list of objects not to be collected */ + /* fields for generational collector */ + GCObject *old; /* start of old objects */ + GCObject *survival; /* start of objects that survived one GC cycle */ struct lua_State *twups; /* list of threads with open upvalues */ unsigned int gcfinnum; /* number of finalizers to call in each GC step */ int gcpause; /* size of pause between successive GCs */ diff --git a/ltests.c b/ltests.c index 6dba514a6e..ac177de87e 100644 --- a/ltests.c +++ b/ltests.c @@ -1,5 +1,5 @@ /* -** $Id: ltests.c,v 2.210 2016/11/07 12:38:35 roberto Exp roberto $ +** $Id: ltests.c,v 2.211 2016/12/04 20:17:24 roberto Exp roberto $ ** Internal Module for Debugging of the Lua Implementation ** See Copyright Notice in lua.h */ @@ -195,9 +195,13 @@ static int testobjref1 (global_State *g, GCObject *f, GCObject *t) { static void printobj (global_State *g, GCObject *o) { - printf("||%s(%p)-%c(%02X)||", + printf("||%s(%p)-%c%c(%02X)||", ttypename(novariant(o->tt)), (void *)o, - isdead(g,o)?'d':isblack(o)?'b':iswhite(o)?'w':'g', o->marked); + isdead(g,o) ? 'd' : isblack(o) ? 'b' : iswhite(o) ? 'w' : 'g', + testbit(o->marked, OLDBIT) ? 'o' : 'n', + o->marked); + if (o->tt == LUA_TSHRSTR || o->tt == LUA_TLNGSTR) + printf(" '%s'", getstr(gco2ts(o))); } @@ -364,8 +368,6 @@ static void checkobject (global_State *g, GCObject *o, int maybedead) { } -#define TESTGRAYBIT 7 - static void checkgraylist (global_State *g, GCObject *o) { ((void)g); /* better to keep it available if we need to print an object */ while (o) { diff --git a/lua.h b/lua.h index fc4e23889d..d9962cec57 100644 --- a/lua.h +++ b/lua.h @@ -1,5 +1,5 @@ /* -** $Id: lua.h,v 1.331 2016/05/30 15:53:28 roberto Exp roberto $ +** $Id: lua.h,v 1.332 2016/12/22 15:51:20 roberto Exp roberto $ ** Lua - A Scripting Language ** Lua.org, PUC-Rio, Brazil (http://www.lua.org) ** See Copyright Notice at the end of this file @@ -308,6 +308,8 @@ LUA_API int (lua_isyieldable) (lua_State *L); #define LUA_GCSETPAUSE 6 #define LUA_GCSETSTEPMUL 7 #define LUA_GCISRUNNING 9 +#define LUA_GCGEN 10 +#define LUA_GCINC 11 LUA_API int (lua_gc) (lua_State *L, int what, int data); From 1a1b2f3d7f321dd6f28118c985986940b189c635 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 14 Mar 2017 09:40:44 -0300 Subject: [PATCH 0004/1145] added 'return' to calls to 'luaL_error' (to signal to the compiler that the function cannot continue past that call) --- loslib.c | 8 +++++--- lstrlib.c | 8 ++++---- lutf8lib.c | 4 ++-- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/loslib.c b/loslib.c index dd2bb378d1..62988d270b 100644 --- a/loslib.c +++ b/loslib.c @@ -1,5 +1,5 @@ /* -** $Id: loslib.c,v 1.64 2016/04/18 13:06:55 roberto Exp $ +** $Id: loslib.c,v 1.65 2016/07/18 17:58:58 roberto Exp roberto $ ** Standard Operating System library ** See Copyright Notice in lua.h */ @@ -293,7 +293,8 @@ static int os_date (lua_State *L) { else stm = l_localtime(&t, &tmr); if (stm == NULL) /* invalid date? */ - luaL_error(L, "time result cannot be represented in this installation"); + return luaL_error(L, + "time result cannot be represented in this installation"); if (strcmp(s, "*t") == 0) { lua_createtable(L, 0, 9); /* 9 = number of fields */ setallfields(L, stm); @@ -340,7 +341,8 @@ static int os_time (lua_State *L) { setallfields(L, &ts); /* update fields with normalized values */ } if (t != (time_t)(l_timet)t || t == (time_t)(-1)) - luaL_error(L, "time result cannot be represented in this installation"); + return luaL_error(L, + "time result cannot be represented in this installation"); l_pushtime(L, t); return 1; } diff --git a/lstrlib.c b/lstrlib.c index 934b7db88c..6ed2bdb86c 100644 --- a/lstrlib.c +++ b/lstrlib.c @@ -1,5 +1,5 @@ /* -** $Id: lstrlib.c,v 1.253 2016/12/20 18:37:00 roberto Exp roberto $ +** $Id: lstrlib.c,v 1.254 2016/12/22 13:08:50 roberto Exp roberto $ ** Standard library for string operations and pattern-matching ** See Copyright Notice in lua.h */ @@ -879,7 +879,7 @@ static int lua_number2strx (lua_State *L, char *buff, int sz, buff[i] = toupper(uchar(buff[i])); } else if (fmt[SIZELENMOD] != 'a') - luaL_error(L, "modifiers for format '%%a'/'%%A' not implemented"); + return luaL_error(L, "modifiers for format '%%a'/'%%A' not implemented"); return n; } @@ -1199,8 +1199,8 @@ static int getnum (const char **fmt, int df) { static int getnumlimit (Header *h, const char **fmt, int df) { int sz = getnum(fmt, df); if (sz > MAXINTSIZE || sz <= 0) - luaL_error(h->L, "integral size (%d) out of limits [1,%d]", - sz, MAXINTSIZE); + return luaL_error(h->L, "integral size (%d) out of limits [1,%d]", + sz, MAXINTSIZE); return sz; } diff --git a/lutf8lib.c b/lutf8lib.c index 6db6fd3762..57bd59e01a 100644 --- a/lutf8lib.c +++ b/lutf8lib.c @@ -1,5 +1,5 @@ /* -** $Id: lutf8lib.c,v 1.15 2015/03/28 19:16:55 roberto Exp roberto $ +** $Id: lutf8lib.c,v 1.16 2016/12/22 13:08:50 roberto Exp roberto $ ** Standard library for UTF-8 manipulation ** See Copyright Notice in lua.h */ @@ -171,7 +171,7 @@ static int byteoffset (lua_State *L) { } else { if (iscont(s + posi)) - luaL_error(L, "initial position is a continuation byte"); + return luaL_error(L, "initial position is a continuation byte"); if (n < 0) { while (n < 0 && posi > 0) { /* move back */ do { /* find beginning of previous character */ From e4287da3a6b0b167da465fd449e5191b9ac9ef46 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 5 Apr 2017 13:50:51 -0300 Subject: [PATCH 0005/1145] generational collector (still not complete) --- lgc.c | 359 ++++++++++++++++++++++++++++++++++++++++++------------- lgc.h | 54 ++++++--- lstate.c | 5 +- lstate.h | 8 +- 4 files changed, 324 insertions(+), 102 deletions(-) diff --git a/lgc.c b/lgc.c index 44e5dc31d5..8d185cd7cc 100644 --- a/lgc.c +++ b/lgc.c @@ -1,5 +1,5 @@ /* -** $Id: lgc.c,v 2.215 2016/12/22 13:08:50 roberto Exp roberto $ +** $Id: lgc.c,v 2.216 2017/02/23 21:07:34 roberto Exp roberto $ ** Garbage Collector ** See Copyright Notice in lua.h */ @@ -9,7 +9,7 @@ #include "lprefix.h" - +#include #include #include "lua.h" @@ -26,12 +26,6 @@ #include "ltm.h" -/* -** internal state for collector while inside the atomic phase. The -** collector should never be in this state while running regular code. -*/ -#define GCSinsideatomic (GCSpause + 1) - /* ** cost of sweeping one element (the size of a small object divided ** by some adjust for the sweep speed) @@ -59,8 +53,9 @@ #define PAUSEADJ 100 -/* mask to erase all color bits */ -#define maskcolors (~(bitmask(BLACKBIT) | WHITEBITS)) +/* mask to erase all color bits (plus gen. related stuff) */ +#define maskcolors (~(bitmask(BLACKBIT) | WHITEBITS | AGEBITS)) + /* macro to erase all color bits then sets only the current white bit */ #define makewhite(g,x) \ @@ -157,8 +152,10 @@ void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v) { lua_assert(isblack(o) && iswhite(v) && !isdead(g, v) && !isdead(g, o)); if (keepinvariant(g)) { /* must keep invariant? */ reallymarkobject(g, v); /* restore invariant */ - if (isold(o)) - l_setbit((v)->marked, OLDBIT); + if (isold(o)) { + lua_assert(!isold(v)); + setage(v, G_OLD0); + } } else { /* sweep phase */ lua_assert(issweepphase(g)); @@ -174,8 +171,12 @@ void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v) { void luaC_barrierback_ (lua_State *L, Table *t) { global_State *g = G(L); lua_assert(isblack(t) && !isdead(g, t)); + lua_assert(issweepphase(g) || getage(t) != G_TOUCHED1); + lua_assert(g->gckind != KGC_GEN || isold(t)); + if (getage(t) != G_TOUCHED2) /* not already in gray list? */ + linkgclist(t, g->grayagain); /* link it in 'grayagain' */ black2gray(t); /* make table gray (again) */ - linkgclist(t, g->grayagain); + setage(t, G_TOUCHED1); /* touched in current cycle */ } @@ -188,10 +189,10 @@ void luaC_barrierback_ (lua_State *L, Table *t) { void luaC_upvalbarrier_ (lua_State *L, UpVal *uv) { global_State *g = G(L); GCObject *o = gcvalue(uv->v); - lua_assert(!upisopen(uv)); /* ensured by macro luaC_upvalbarrier */ if (keepinvariant(g)) { markobject(g, o); - l_setbit((o)->marked, OLDBIT); + if (!isold(o)) + setage(o, G_OLD0); } } @@ -379,10 +380,10 @@ static void traverseweakvalue (global_State *g, Table *h) { hasclears = 1; /* table will have to be cleared */ } } - if (g->gcstate == GCSpropagate) - linkgclist(h, g->grayagain); /* must retraverse it in atomic phase */ - else if (hasclears) + if (g->gcstate == GCSatomic && hasclears) linkgclist(h, g->weak); /* has to be cleared later */ + else + linkgclist(h, g->grayagain); /* must retraverse it in atomic phase */ } @@ -431,6 +432,8 @@ static int traverseephemeron (global_State *g, Table *h) { linkgclist(h, g->ephemeron); /* have to propagate again */ else if (hasclears) /* table has white keys? */ linkgclist(h, g->allweak); /* may have to clean white keys */ + else if (g->gckind == KGC_GEN) + linkgclist(h, g->grayagain); /* keep it in some list */ return marked; } @@ -450,6 +453,10 @@ static void traversestrongtable (global_State *g, Table *h) { markvalue(g, gval(n)); /* mark value */ } } + if (g->gckind == KGC_GEN) { + linkgclist(h, g->grayagain); /* keep it in some gray list */ + black2gray(h); + } } @@ -522,7 +529,7 @@ static lu_mem traverseLclosure (global_State *g, LClosure *cl) { for (i = 0; i < cl->nupvalues; i++) { /* mark its upvalues */ UpVal *uv = cl->upvals[i]; if (uv != NULL) { - if (upisopen(uv) && g->gcstate != GCSinsideatomic) + if (upisopen(uv) && g->gcstate != GCSatomic) uv->u.open.touched = 1; /* can be marked in 'remarkupvals' */ else markvalue(g, uv->v); @@ -536,11 +543,11 @@ static lu_mem traversethread (global_State *g, lua_State *th) { StkId o = th->stack; if (o == NULL) return 1; /* stack not completely built yet */ - lua_assert(g->gcstate == GCSinsideatomic || + lua_assert(g->gcstate == GCSatomic || th->openupval == NULL || isintwups(th)); for (; o < th->top; o++) /* mark live elements in the stack */ markvalue(g, o); - if (g->gcstate == GCSinsideatomic) { /* final traversal? */ + if (g->gcstate == GCSatomic) { /* final traversal? */ StkId lim = th->stack + th->stacksize; /* real end of stack */ for (; o < lim; o++) /* clear not-marked stack slice */ setnilvalue(o); @@ -564,7 +571,7 @@ static lu_mem traversethread (global_State *g, lua_State *th) { static void propagatemark (global_State *g) { lu_mem size; GCObject *o = g->gray; - lua_assert(isgray(o)); + lua_assert(ongraylist(o)); gray2black(o); switch (o->tt) { case LUA_TTABLE: { @@ -638,11 +645,10 @@ static void convergeephemerons (global_State *g) { /* -** clear entries with unmarked keys from all weaktables in list 'l' up -** to element 'f' +** clear entries with unmarked keys from all weaktables in list 'l' */ -static void clearkeys (global_State *g, GCObject *l, GCObject *f) { - for (; l != f; l = gco2t(l)->gclist) { +static void clearkeys (global_State *g, GCObject *l) { + for (; l; l = gco2t(l)->gclist) { Table *h = gco2t(l); Node *n, *limit = gnodelast(h); for (n = gnode(h, 0); n < limit; n++) { @@ -885,11 +891,13 @@ static void separatetobefnz (global_State *g, int all) { GCObject *curr; GCObject **p = &g->finobj; GCObject **lastnext = findlast(&g->tobefnz); - while ((curr = *p) != NULL) { /* traverse all finalizable objects */ + while ((curr = *p) != g->finobjold) { /* traverse all finalizable objects */ lua_assert(tofinalize(curr)); if (!(iswhite(curr) || all)) /* not being collected? */ p = &curr->next; /* don't bother with it */ else { + if (curr == g->finobjsur) + g->finobjsur = curr->next; *p = curr->next; /* remove 'curr' from 'finobj' list */ curr->next = *lastnext; /* link at the end of 'tobefnz' list */ *lastnext = curr; @@ -915,6 +923,14 @@ void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt) { if (g->sweepgc == &o->next) /* should not remove 'sweepgc' object */ g->sweepgc = sweeptolive(L, g->sweepgc); /* change 'sweepgc' */ } + else { + if (o == g->survival) + g->survival = o->next; + if (o == g->old) + g->old = o->next; + if (o == g->reallyold) + g->reallyold = o->next; + } /* search for pointer pointing to 'o' */ for (p = &g->allgc; *p != o; p = &(*p)->next) { /* empty */ } *p = o->next; /* remove 'o' from 'allgc' list */ @@ -934,31 +950,65 @@ void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt) { */ +/* mask to erase all color bits (not changing gen-related stuff) */ +#define maskgencolors (~(bitmask(BLACKBIT) | WHITEBITS)) + #if 0 static int count (GCObject *p, GCObject *limit) { int res = 0; - for (; p != NULL && p != limit; p = p->next) + for (; p != NULL && p != limit; p = p->next) { res++; + } return res; } #endif -static GCObject **sweepgen (lua_State *L, GCObject **p, GCObject *limit, - int zeromask, int onemask) { - global_State *g = G(L); - int ow = otherwhite(g); +static void sweep2old (lua_State *L, GCObject **p) { + GCObject *curr; + while ((curr = *p) != NULL) { + if (iswhite(curr)) { /* is 'curr' dead? */ + lua_assert(isdead(G(L), curr)); + *p = curr->next; /* remove 'curr' from list */ + freeobj(L, curr); /* erase 'curr' */ + } + else { /* all surviving objects become old */ + setage(curr, G_OLD); + p = &curr->next; /* go to next element */ + } + } +} + + +static GCObject **sweepgen (lua_State *L, global_State *g, GCObject **p, + GCObject *limit) { + int white = luaC_white(g); GCObject *curr; while ((curr = *p) != limit) { int marked = curr->marked; - if (isdeadm(ow, marked)) { /* is 'curr' dead? */ - lua_assert(!isold(curr)); + if (iswhite(curr)) { /* is 'curr' dead? */ + lua_assert(!isold(curr) && !testbits(curr->marked, white)); *p = curr->next; /* remove 'curr' from list */ freeobj(L, curr); /* erase 'curr' */ } - else { /* correct mark */ - if (!isold(curr)) /* don't change old objects */ - curr->marked = cast_byte((marked & zeromask) | onemask); + else { /* correct mark and age */ + switch (getage(curr)) { + case G_NEW: /* make white and go to next age */ + curr->marked = cast_byte((marked & maskgencolors) | white); + changeage(curr, G_NEW, G_SURVIVAL); + break; + case G_SURVIVAL: /* go to next age */ + changeage(curr, G_SURVIVAL, G_OLD1); + break; + case G_OLD0: /* go to next age */ + changeage(curr, G_OLD0, G_OLD1); + break; + case G_OLD1: /* go to next age */ + changeage(curr, G_OLD1, G_OLD); + break; + default: /* don't change 'old', 'touched1', and 'touched2' */ + break; + } p = &curr->next; /* go to next element */ } } @@ -966,51 +1016,184 @@ static GCObject **sweepgen (lua_State *L, GCObject **p, GCObject *limit, } -static void startgencycle (lua_State *L, global_State *g) { - propagateall(g); - atomic(L); +static void whitelist (global_State *g, GCObject *p) { + int white = luaC_white(g); + for (; p != NULL; p = p->next) + p->marked = cast_byte((p->marked & maskcolors) | white); } -static void finishgencycle (lua_State *L, global_State *g, int mask) { - sweepgen(L, &g->finobj, NULL, ~0, mask); - sweepgen(L, &g->tobefnz, NULL, ~0, mask); +static void finishgencycle (lua_State *L, global_State *g) { + // sweepgen(L, &g->tobefnz, ~0, mask); checkSizes(L, g); g->gcstate = GCSpropagate; /* skip restart */ callallpendingfinalizers(L); } +static void printgray (GCObject *o) { + printf("gray: "); + while (o) { + printf("%p %d %02x ", (void*)o, o->tt, o->marked); + switch (o->tt) { + case LUA_TTABLE: { + Table *h = gco2t(o); + o = h->gclist; + break; + } + case LUA_TLCL: { + LClosure *cl = gco2lcl(o); + o = cl->gclist; + break; + } + case LUA_TCCL: { + CClosure *cl = gco2ccl(o); + o = cl->gclist; + break; + } + case LUA_TTHREAD: { + lua_State *th = gco2th(o); + o = th->gclist; + break; + } + case LUA_TPROTO: { + Proto *p = gco2p(o); + o = p->gclist; + break; + } + default: lua_assert(0); return; + } + } + printf("\n"); +} + + + +static GCObject **correctgraylist (GCObject **p) { + GCObject *curr; + while ((curr = *p) != NULL) { + switch (curr->tt) { + case LUA_TTABLE: { + Table *h = gco2t(curr); + if (getage(h) == G_TOUCHED1) { /* touched in this cycle? */ + lua_assert(isgray(h)); + gray2black(h); /* make it black, for next barrier */ + changeage(h, G_TOUCHED1, G_TOUCHED2); + p = &h->gclist; /* go to next element */ + } + else { + if (!iswhite(h)) { + lua_assert(isold(h)); + if (getage(h) == G_TOUCHED2) + changeage(h, G_TOUCHED2, G_OLD); + gray2black(h); /* make it black */ + } + *p = h->gclist; /* remove 'curr' from gray list */ + } + break; + } + case LUA_TTHREAD: { + lua_State *th = gco2th(curr); + lua_assert(!isblack(th)); + if (iswhite(th)) /* new object? */ + *p = th->gclist; /* remove from gray list */ + else /* old threads remain gray */ + p = &th->gclist; /* go to next element */ + break; + } + default: lua_assert(0); /* nothing more could be gray here */ + } + } + return p; +} + + +static void correctgraylists (global_State *g) { + GCObject **list = correctgraylist(&g->grayagain); + *list = g->weak; g->weak = NULL; + list = correctgraylist(list); + *list = g->allweak; g->allweak = NULL; + list = correctgraylist(list); + *list = g->ephemeron; g->ephemeron = NULL; + correctgraylist(list); +} + + +static void markold (global_State *g, GCObject *from, GCObject *to) { + GCObject *p; + for (p = from; p != to; p = p->next) { + if (getage(p) == G_OLD1) { + lua_assert(!iswhite(p)); + if (isblack(p)) { + black2gray(p); /* should be '2white', but gray works too */ + reallymarkobject(g, p); + } + else + lua_assert(p->tt == LUA_TTHREAD); /* threads are always gray */ + } + } +} + static void youngcollection (lua_State *L, global_State *g) { GCObject **psurvival; lua_assert(g->gcstate == GCSpropagate); - startgencycle(L, g); + markold(g, g->survival, g->reallyold); + markold(g, g->finobj, g->finobjrold); /* ??? */ + atomic(L); + /* sweep nursery */ - psurvival = sweepgen(L, &g->allgc, g->survival, maskcolors, luaC_white(g)); - lua_assert(*psurvival == g->survival); - /* sweep 'survival' list, making elements old */ - sweepgen(L, psurvival, g->old, ~0, bitmask(OLDBIT)); - /* incorporate 'survival' list into old list */ - g->old = *psurvival; - /* surviving young objects go to 'survival' list */ - g->survival = g->allgc; - finishgencycle(L, g, 0); -lua_checkmemory(L); + psurvival = sweepgen(L, g, &g->allgc, g->survival); + /* sweep 'survival' and 'old' */ + sweepgen(L, g, psurvival, g->reallyold); + g->reallyold = g->old; + g->old = *psurvival; /* 'survival' survivals are old now */ + g->survival = g->allgc; /* all news are survivals */ + + /* repeat for 'finobj' lists */ + psurvival = sweepgen(L, g, &g->finobj, g->finobjsur); + /* sweep 'survival' and 'old' */ + sweepgen(L, g, psurvival, g->finobjrold); + g->finobjrold = g->finobjold; + g->finobjold = *psurvival; /* 'survival' survivals are old now */ + g->finobjsur = g->finobj; /* all news are survivals */ + + sweepgen(L, g, &g->tobefnz, NULL); + + finishgencycle(L, g); + correctgraylists(g); +//printf("check: \n");lua_checkmemory(L); } static void entergen (lua_State *L, global_State *g) { -lua_checkmemory(L); - lua_assert(g->old == NULL && g->survival == NULL); + lua_assert(g->reallyold == NULL && g->old == NULL && g->survival == NULL); luaC_runtilstate(L, bitmask(GCSpause)); /* prepare to start a new cycle */ luaC_runtilstate(L, bitmask(GCSpropagate)); /* start new cycle */ - startgencycle(L, g); + atomic(L); /* sweep all ellements making them old */ - sweepgen(L, &g->allgc, g->survival, ~0, bitmask(OLDBIT)); - /* everything alive now is old; 'survival' is empty */ - g->old = g->survival = g->allgc; - finishgencycle(L, g, bitmask(OLDBIT)); -lua_checkmemory(L); + sweep2old(L, &g->allgc); + /* everything alive now is old */ + g->reallyold = g->old = g->survival = g->allgc; + + /* repeat for 'finobj' lists */ + sweep2old(L, &g->finobj); + g->finobjrold = g->finobjold = g->finobjsur = g->finobj; + + finishgencycle(L, g); + correctgraylists(g); + g->gckind = KGC_GEN; +} + + +static void enterinc (global_State *g) { + makewhite(g, g->mainthread); + whitelist(g, g->allgc); + g->reallyold = g->old = g->survival = NULL; + whitelist(g, g->finobj); + g->finobjrold = g->finobjold = g->finobjsur = NULL; + lua_assert(g->tobefnz == NULL); /* no need to sweep */ + g->gcstate = GCSpause; + g->gckind = KGC_NORMAL; } @@ -1019,17 +1202,18 @@ void luaC_changemode (lua_State *L, int newmode) { if (newmode != g->gckind) { /* otherwise, nothing to be done */ if (newmode == KGC_GEN) /* entering generational mode? */ entergen(L, g); - else { /* entering incremental mode */ -lua_checkmemory(L); - youngcollection(L, g); - g->old = g->survival = NULL; -lua_checkmemory(L); - } - g->gckind = newmode; + else + enterinc(g); /* entering incremental mode */ } } +static void fullgen (lua_State *L, global_State *g) { + enterinc(g); + entergen(L, g); +} + + static void genstep (lua_State *L, global_State *g) { lu_mem mem; youngcollection(L, g); @@ -1094,10 +1278,10 @@ static void deletealllist (lua_State *L, GCObject *p) { void luaC_freeallobjects (lua_State *L) { global_State *g = G(L); + luaC_changemode(L, KGC_NORMAL); separatetobefnz(g, 1); /* separate all objects with finalizers */ lua_assert(g->finobj == NULL); callallpendingfinalizers(L); - lua_assert(g->tobefnz == NULL); deletealllist(L, g->finobj); deletealllist(L, g->allgc); deletealllist(L, g->fixedgc); /* collect fixed objects */ @@ -1110,9 +1294,10 @@ static l_mem atomic (lua_State *L) { l_mem work; GCObject *origweak, *origall; GCObject *grayagain = g->grayagain; /* save original list */ + g->grayagain = NULL; lua_assert(g->ephemeron == NULL && g->weak == NULL); lua_assert(!iswhite(g->mainthread)); - g->gcstate = GCSinsideatomic; + g->gcstate = GCSatomic; g->GCmemtrav = 0; /* start counting work */ markobject(g, L); /* mark running thread */ /* registry and global metatables may be changed by API */ @@ -1123,7 +1308,6 @@ static l_mem atomic (lua_State *L) { propagateall(g); /* propagate changes */ work = g->GCmemtrav; /* stop counting (do not recount 'grayagain') */ g->gray = grayagain; - g->grayagain = NULL; propagateall(g); /* traverse 'grayagain' list */ g->GCmemtrav = 0; /* restart counting */ convergeephemerons(g); @@ -1141,13 +1325,14 @@ static l_mem atomic (lua_State *L) { convergeephemerons(g); /* at this point, all resurrected objects are marked. */ /* remove dead objects from weak tables */ - clearkeys(g, g->ephemeron, NULL); /* clear keys from all ephemeron tables */ - clearkeys(g, g->allweak, NULL); /* clear keys from all 'allweak' tables */ + clearkeys(g, g->ephemeron); /* clear keys from all ephemeron tables */ + clearkeys(g, g->allweak); /* clear keys from all 'allweak' tables */ /* clear values from resurrected weak tables */ clearvalues(g, g->weak, origweak); clearvalues(g, g->allweak, origall); luaS_clearcache(g); g->currentwhite = cast_byte(otherwhite(g)); /* flip current white */ + lua_assert(g->gray == NULL); work += g->GCmemtrav; /* complete counting */ return work; /* estimate of memory marked by 'atomic' */ } @@ -1181,12 +1366,12 @@ static lu_mem singlestep (lua_State *L) { case GCSpropagate: { g->GCmemtrav = 0; if (g->gray == NULL) /* no more gray objects? */ - g->gcstate = GCSatomic; /* finish propagate phase */ + g->gcstate = GCSenteratomic; /* finish propagate phase */ else propagatemark(g); /* traverse one gray object */ return g->GCmemtrav; /* memory traversed in this step */ } - case GCSatomic: { + case GCSenteratomic: { lu_mem work; propagateall(g); /* make sure gray list is empty */ work = atomic(L); /* work is what was traversed by 'atomic' */ @@ -1291,23 +1476,31 @@ void luaC_step (lua_State *L) { ** to sweep all objects to turn them back to white (as white has not ** changed, nothing will be collected). */ -void luaC_fullgc (lua_State *L, int isemergency) { - global_State *g = G(L); - lua_assert(g->gckind == KGC_NORMAL); - if (isemergency) g->gckind = KGC_EMERGENCY; /* set flag */ - if (keepinvariant(g)) { /* black objects? */ +static void fullinc (lua_State *L, global_State *g) { + if (keepinvariant(g)) /* black objects? */ entersweep(L); /* sweep everything to turn them back to white */ - } /* finish any pending sweep phase to start a new cycle */ luaC_runtilstate(L, bitmask(GCSpause)); luaC_runtilstate(L, bitmask(GCScallfin)); /* run up to finalizers */ /* estimate must be correct after a full GC cycle */ lua_assert(g->GCestimate == gettotalbytes(g)); luaC_runtilstate(L, bitmask(GCSpause)); /* finish collection */ - g->gckind = KGC_NORMAL; setpause(g); } + +void luaC_fullgc (lua_State *L, int isemergency) { + global_State *g = G(L); + int gckind = g->gckind; + if (isemergency) + g->gckind = KGC_EMERGENCY; /* set flag */ + if (gckind == KGC_NORMAL) + fullinc(L, g); + else + fullgen(L, g); + g->gckind = gckind; +} + /* }====================================================== */ diff --git a/lgc.h b/lgc.h index e75ff85ce2..158c0e3835 100644 --- a/lgc.h +++ b/lgc.h @@ -1,5 +1,5 @@ /* -** $Id: lgc.h,v 2.91 2015/12/21 13:02:14 roberto Exp roberto $ +** $Id: lgc.h,v 2.92 2017/02/23 21:07:34 roberto Exp roberto $ ** Garbage Collector ** See Copyright Notice in lua.h */ @@ -37,13 +37,14 @@ ** Possible states of the Garbage Collector */ #define GCSpropagate 0 -#define GCSatomic 1 -#define GCSswpallgc 2 -#define GCSswpfinobj 3 -#define GCSswptobefnz 4 -#define GCSswpend 5 -#define GCScallfin 6 -#define GCSpause 7 +#define GCSenteratomic 1 +#define GCSatomic 2 +#define GCSswpallgc 3 +#define GCSswpfinobj 4 +#define GCSswptobefnz 5 +#define GCSswpend 6 +#define GCScallfin 7 +#define GCSpause 8 #define issweepphase(g) \ @@ -74,14 +75,17 @@ #define testbit(x,b) testbits(x, bitmask(b)) -/* Layout for bit use in 'marked' field: */ -#define WHITE0BIT 0 /* object is white (type 0) */ -#define WHITE1BIT 1 /* object is white (type 1) */ -#define BLACKBIT 2 /* object is black */ -#define FINALIZEDBIT 3 /* object has been marked for finalization */ -#define OLDBIT 4 /* object is old (gen. mode) */ +/* +** Layout for bit use in 'marked' field. First three bits are +** used for object "age" in generational mode. +*/ +#define WHITE0BIT 3 /* object is white (type 0) */ +#define WHITE1BIT 4 /* object is white (type 1) */ +#define BLACKBIT 5 /* object is black */ +#define FINALIZEDBIT 6 /* object has been marked for finalization */ #define TESTGRAYBIT 7 /* used by tests (luaL_checkmemory) */ + #define WHITEBITS bit2mask(WHITE0BIT, WHITE1BIT) @@ -89,7 +93,6 @@ #define isblack(x) testbit((x)->marked, BLACKBIT) #define isgray(x) /* neither white nor black */ \ (!testbits((x)->marked, WHITEBITS | bitmask(BLACKBIT))) -#define isold(x) testbit((x)->marked, OLDBIT) #define tofinalize(x) testbit((x)->marked, FINALIZEDBIT) @@ -103,6 +106,27 @@ #define luaC_white(g) cast(lu_byte, (g)->currentwhite & WHITEBITS) +/* object age in generational mode */ +#define G_NEW 0 /* created in current cycle */ +#define G_SURVIVAL 1 /* created in previous cycle */ +#define G_OLD1 2 /* first full cycle as old */ +#define G_OLD0 3 /* marked old by frw. barrier in this cycle */ +#define G_OLD 4 /* really old object (not to be visited) */ +#define G_TOUCHED1 5 /* old object touched this cycle */ +#define G_TOUCHED2 6 /* old object touched in previous cycle */ + +#define AGEBITS 7 /* all age bits (111) */ + +#define getage(o) ((o)->marked & AGEBITS) +#define setage(o,a) ((o)->marked = cast_byte(((o)->marked & (~AGEBITS)) | a)) +#define isold(o) (getage(o) > G_SURVIVAL) + +#define changeage(o,f,t) \ + check_exp(getage(o) == (f), (o)->marked ^= ((f)^(t))) + +#define ongraylist(o) (isgray(o) || getage(o) == G_TOUCHED2) + + /* ** Does one step of collection when debt becomes positive. 'pre'/'pos' ** allows some adjustments to be done only when needed. macro diff --git a/lstate.c b/lstate.c index 4900d72423..d2f3858892 100644 --- a/lstate.c +++ b/lstate.c @@ -1,5 +1,5 @@ /* -** $Id: lstate.c,v 2.133 2015/11/13 12:16:51 roberto Exp roberto $ +** $Id: lstate.c,v 2.134 2017/02/23 21:07:34 roberto Exp roberto $ ** Global State ** See Copyright Notice in lua.h */ @@ -319,7 +319,8 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { g->gcstate = GCSpause; g->gckind = KGC_NORMAL; g->allgc = g->finobj = g->tobefnz = g->fixedgc = NULL; - g->survival = g->old = NULL; + g->survival = g->old = g->reallyold = NULL; + g->finobjsur = g->finobjold = g->finobjrold = NULL; g->sweepgc = NULL; g->gray = g->grayagain = NULL; g->weak = g->ephemeron = g->allweak = NULL; diff --git a/lstate.h b/lstate.h index 51128db193..b01859aa24 100644 --- a/lstate.h +++ b/lstate.h @@ -1,5 +1,5 @@ /* -** $Id: lstate.h,v 2.134 2017/02/15 18:52:13 roberto Exp roberto $ +** $Id: lstate.h,v 2.135 2017/02/23 21:07:34 roberto Exp $ ** Global State ** See Copyright Notice in lua.h */ @@ -160,8 +160,12 @@ typedef struct global_State { GCObject *tobefnz; /* list of userdata to be GC */ GCObject *fixedgc; /* list of objects not to be collected */ /* fields for generational collector */ - GCObject *old; /* start of old objects */ GCObject *survival; /* start of objects that survived one GC cycle */ + GCObject *old; /* start of old objects */ + GCObject *reallyold; /* old objects with more than one cycle */ + GCObject *finobjsur; /* list of survival objects with finalizers */ + GCObject *finobjold; /* list of old objects with finalizers */ + GCObject *finobjrold; /* list of really old objects with finalizers */ struct lua_State *twups; /* list of threads with open upvalues */ unsigned int gcfinnum; /* number of finalizers to call in each GC step */ int gcpause; /* size of pause between successive GCs */ From 2331e1beec01babf78ca09fea8701b5bb3c78d4c Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 6 Apr 2017 10:08:56 -0300 Subject: [PATCH 0006/1145] small changes in 'luaC_upvalbarrier' --- lapi.c | 10 +++++----- lfunc.c | 9 +++++---- lgc.c | 8 +++----- lgc.h | 8 ++++---- lvm.c | 4 ++-- 5 files changed, 19 insertions(+), 20 deletions(-) diff --git a/lapi.c b/lapi.c index 494fce759f..25ed81fdf9 100644 --- a/lapi.c +++ b/lapi.c @@ -1,5 +1,5 @@ /* -** $Id: lapi.c,v 2.259 2016/02/29 14:27:14 roberto Exp roberto $ +** $Id: lapi.c,v 2.260 2017/02/23 21:07:34 roberto Exp roberto $ ** Lua API ** See Copyright Notice in lua.h */ @@ -1004,7 +1004,7 @@ LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data, const TValue *gt = luaH_getint(reg, LUA_RIDX_GLOBALS); /* set global table as 1st upvalue of 'f' (may be LUA_ENV) */ setobj(L, f->upvals[0]->v, gt); - luaC_upvalbarrier(L, f->upvals[0]); + luaC_upvalbarrier(L, f->upvals[0], gt); } } lua_unlock(L); @@ -1253,8 +1253,8 @@ LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) { if (name) { L->top--; setobj(L, val, L->top); - if (owner) { luaC_barrier(L, owner, L->top); } - else if (uv) { luaC_upvalbarrier(L, uv); } + if (owner) { luaC_barrier(L, owner, val); } + else if (uv) { luaC_upvalbarrier(L, uv, val); } } lua_unlock(L); return name; @@ -1300,7 +1300,7 @@ LUA_API void lua_upvaluejoin (lua_State *L, int fidx1, int n1, *up1 = *up2; (*up1)->refcount++; if (upisopen(*up1)) (*up1)->u.open.touched = 1; - luaC_upvalbarrier(L, *up1); + luaC_upvalbarrier(L, *up1, (*up1)->v); } diff --git a/lfunc.c b/lfunc.c index 4c10230e3f..0f839e7c04 100644 --- a/lfunc.c +++ b/lfunc.c @@ -1,5 +1,5 @@ /* -** $Id: lfunc.c,v 2.44 2014/10/25 11:50:46 roberto Exp roberto $ +** $Id: lfunc.c,v 2.45 2014/11/02 19:19:04 roberto Exp roberto $ ** Auxiliary functions to manipulate prototypes and closures ** See Copyright Notice in lua.h */ @@ -88,9 +88,10 @@ void luaF_close (lua_State *L, StkId level) { if (uv->refcount == 0) /* no references? */ luaM_free(L, uv); /* free upvalue */ else { - setobj(L, &uv->u.value, uv->v); /* move value to upvalue slot */ - uv->v = &uv->u.value; /* now current value lives here */ - luaC_upvalbarrier(L, uv); + TValue *slot = &uv->u.value; /* new position for value */ + setobj(L, slot, uv->v); /* move value to upvalue slot */ + uv->v = slot; /* now current value lives here */ + luaC_upvalbarrier(L, uv, slot); } } } diff --git a/lgc.c b/lgc.c index 8d185cd7cc..731143007a 100644 --- a/lgc.c +++ b/lgc.c @@ -186,13 +186,11 @@ void luaC_barrierback_ (lua_State *L, Table *t) { ** closures pointing to it. So, we assume that the object being assigned ** must be marked. */ -void luaC_upvalbarrier_ (lua_State *L, UpVal *uv) { +void luaC_upvalbarrier_ (lua_State *L, GCObject *o) { global_State *g = G(L); - GCObject *o = gcvalue(uv->v); - if (keepinvariant(g)) { + if (keepinvariant(g) && !isold(o)) { markobject(g, o); - if (!isold(o)) - setage(o, G_OLD0); + setage(o, G_OLD0); } } diff --git a/lgc.h b/lgc.h index 158c0e3835..c5dfcf2eb2 100644 --- a/lgc.h +++ b/lgc.h @@ -153,9 +153,9 @@ (isblack(p) && iswhite(o)) ? \ luaC_barrier_(L,obj2gco(p),obj2gco(o)) : cast_void(0)) -#define luaC_upvalbarrier(L,uv) ( \ - (iscollectable((uv)->v) && !upisopen(uv)) ? \ - luaC_upvalbarrier_(L,uv) : cast_void(0)) +#define luaC_upvalbarrier(L,uv,x) ( \ + (iscollectable(x) && !upisopen(uv)) ? \ + luaC_upvalbarrier_(L,gcvalue(x)) : cast_void(0)) LUAI_FUNC void luaC_fix (lua_State *L, GCObject *o); LUAI_FUNC void luaC_freeallobjects (lua_State *L); @@ -165,7 +165,7 @@ LUAI_FUNC void luaC_fullgc (lua_State *L, int isemergency); LUAI_FUNC GCObject *luaC_newobj (lua_State *L, int tt, size_t sz); LUAI_FUNC void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v); LUAI_FUNC void luaC_barrierback_ (lua_State *L, Table *o); -LUAI_FUNC void luaC_upvalbarrier_ (lua_State *L, UpVal *uv); +LUAI_FUNC void luaC_upvalbarrier_ (lua_State *L, GCObject *o); LUAI_FUNC void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt); LUAI_FUNC void luaC_upvdeccount (lua_State *L, UpVal *uv); LUAI_FUNC void luaC_changemode (lua_State *L, int newmode); diff --git a/lvm.c b/lvm.c index 3709d77a4c..81b2a4815e 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.267 2016/01/05 16:07:21 roberto Exp roberto $ +** $Id: lvm.c,v 2.268 2016/02/05 19:59:14 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -855,7 +855,7 @@ void luaV_execute (lua_State *L) { vmcase(OP_SETUPVAL) { UpVal *uv = cl->upvals[GETARG_B(i)]; setobj(L, uv->v, ra); - luaC_upvalbarrier(L, uv); + luaC_upvalbarrier(L, uv, ra); vmbreak; } vmcase(OP_SETTABLE) { From 9569ad6b0ddcde43eb893d2cfe5bcdb715c0ff20 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 10 Apr 2017 10:33:04 -0300 Subject: [PATCH 0007/1145] Comments for generational collector --- lgc.c | 191 +++++++++++++++++++++++++++++++++++++++------------------- lgc.h | 6 +- 2 files changed, 131 insertions(+), 66 deletions(-) diff --git a/lgc.c b/lgc.c index 731143007a..68a7622f43 100644 --- a/lgc.c +++ b/lgc.c @@ -1,5 +1,5 @@ /* -** $Id: lgc.c,v 2.216 2017/02/23 21:07:34 roberto Exp roberto $ +** $Id: lgc.c,v 2.218 2017/04/06 13:08:56 roberto Exp roberto $ ** Garbage Collector ** See Copyright Notice in lua.h */ @@ -153,8 +153,8 @@ void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v) { if (keepinvariant(g)) { /* must keep invariant? */ reallymarkobject(g, v); /* restore invariant */ if (isold(o)) { - lua_assert(!isold(v)); - setage(v, G_OLD0); + lua_assert(!isold(v)); /* white object could not be old */ + setage(v, G_OLD0); /* restore generational invariant */ } } else { /* sweep phase */ @@ -171,8 +171,7 @@ void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v) { void luaC_barrierback_ (lua_State *L, Table *t) { global_State *g = G(L); lua_assert(isblack(t) && !isdead(g, t)); - lua_assert(issweepphase(g) || getage(t) != G_TOUCHED1); - lua_assert(g->gckind != KGC_GEN || isold(t)); + lua_assert(g->gckind != KGC_GEN || (isold(t) && getage(t) != G_TOUCHED1)); if (getage(t) != G_TOUCHED2) /* not already in gray list? */ linkgclist(t, g->grayagain); /* link it in 'grayagain' */ black2gray(t); /* make table gray (again) */ @@ -393,7 +392,8 @@ static void traverseweakvalue (global_State *g, Table *h) { ** the atomic phase, if table has any white->white entry, it has to ** be revisited during ephemeron convergence (as that key may turn ** black). Otherwise, if it has any white key, table has to be cleared -** (in the atomic phase). +** (in the atomic phase). In generational mode, it (like all visited +** tables) must be kept in some gray list for post-processing. */ static int traverseephemeron (global_State *g, Table *h) { int marked = 0; /* true if an object is marked in this traversal */ @@ -524,9 +524,9 @@ static lu_mem traverseCclosure (global_State *g, CClosure *cl) { static lu_mem traverseLclosure (global_State *g, LClosure *cl) { int i; markobjectN(g, cl->p); /* mark its prototype */ - for (i = 0; i < cl->nupvalues; i++) { /* mark its upvalues */ + for (i = 0; i < cl->nupvalues; i++) { /* visit its upvalues */ UpVal *uv = cl->upvals[i]; - if (uv != NULL) { + if (uv != NULL) { /* can be NULL while closure is being built */ if (upisopen(uv) && g->gcstate != GCSatomic) uv->u.open.touched = 1; /* can be marked in 'remarkupvals' */ else @@ -683,6 +683,10 @@ static void clearvalues (global_State *g, GCObject *l, GCObject *f) { } +/* +** Decrement the reference count of an upvalue. If it goes to zero and +** upvalue is closed, delete it. +*/ void luaC_upvdeccount (lua_State *L, UpVal *uv) { lua_assert(uv->refcount > 0); uv->refcount--; @@ -704,26 +708,31 @@ static void freeLclosure (lua_State *L, LClosure *cl) { static void freeobj (lua_State *L, GCObject *o) { switch (o->tt) { - case LUA_TPROTO: luaF_freeproto(L, gco2p(o)); break; - case LUA_TLCL: { + case LUA_TPROTO: + luaF_freeproto(L, gco2p(o)); + break; + case LUA_TLCL: freeLclosure(L, gco2lcl(o)); break; - } - case LUA_TCCL: { + case LUA_TCCL: luaM_freemem(L, o, sizeCclosure(gco2ccl(o)->nupvalues)); break; - } - case LUA_TTABLE: luaH_free(L, gco2t(o)); break; - case LUA_TTHREAD: luaE_freethread(L, gco2th(o)); break; - case LUA_TUSERDATA: luaM_freemem(L, o, sizeudata(gco2u(o))); break; + case LUA_TTABLE: + luaH_free(L, gco2t(o)); + break; + case LUA_TTHREAD: + luaE_freethread(L, gco2th(o)); + break; + case LUA_TUSERDATA: + luaM_freemem(L, o, sizeudata(gco2u(o))); + break; case LUA_TSHRSTR: luaS_remove(L, gco2ts(o)); /* remove it from hash table */ luaM_freemem(L, o, sizelstring(gco2ts(o)->shrlen)); break; - case LUA_TLNGSTR: { + case LUA_TLNGSTR: luaM_freemem(L, o, sizelstring(gco2ts(o)->u.lnglen)); break; - } default: lua_assert(0); } } @@ -793,6 +802,10 @@ static void checkSizes (lua_State *L, global_State *g) { } +/* +** Get the next udata to be finalized from the 'tobefnz' list, and +** link it back into the 'allgc' list. +*/ static GCObject *udata2finalize (global_State *g) { GCObject *o = g->tobefnz; /* get first element */ lua_assert(tofinalize(o)); @@ -882,8 +895,11 @@ static GCObject **findlast (GCObject **p) { /* -** move all unreachable objects (or 'all' objects) that need -** finalization from list 'finobj' to list 'tobefnz' (to be finalized) +** Move all unreachable objects (or 'all' objects) that need +** finalization from list 'finobj' to list 'tobefnz' (to be finalized). +** (Note that objects after 'finobjold' cannot be white, so they +** don't need to be traversed. In incremental mode, 'finobjold' is NULL, +** so the whole list is traversed.) */ static void separatetobefnz (global_State *g, int all) { GCObject *curr; @@ -894,8 +910,8 @@ static void separatetobefnz (global_State *g, int all) { if (!(iswhite(curr) || all)) /* not being collected? */ p = &curr->next; /* don't bother with it */ else { - if (curr == g->finobjsur) - g->finobjsur = curr->next; + if (curr == g->finobjsur) /* removing 'finobjsur'? */ + g->finobjsur = curr->next; /* correct it */ *p = curr->next; /* remove 'curr' from 'finobj' list */ curr->next = *lastnext; /* link at the end of 'tobefnz' list */ *lastnext = curr; @@ -921,7 +937,7 @@ void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt) { if (g->sweepgc == &o->next) /* should not remove 'sweepgc' object */ g->sweepgc = sweeptolive(L, g->sweepgc); /* change 'sweepgc' */ } - else { + else { /* correct pointers into 'allgc' list, if needed */ if (o == g->survival) g->survival = o->next; if (o == g->old) @@ -948,7 +964,7 @@ void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt) { */ -/* mask to erase all color bits (not changing gen-related stuff) */ +/* mask to erase all color bits, not changing gen-related stuff */ #define maskgencolors (~(bitmask(BLACKBIT) | WHITEBITS)) #if 0 @@ -962,6 +978,10 @@ static int count (GCObject *p, GCObject *limit) { #endif +/* +** Sweep a list of objects, deleting dead ones and turning +** the non dead to old (without changing their colors). +*/ static void sweep2old (lua_State *L, GCObject **p) { GCObject *curr; while ((curr = *p) != NULL) { @@ -978,35 +998,36 @@ static void sweep2old (lua_State *L, GCObject **p) { } +/* +** Sweep for generational mode. Delete dead objects. (Because the +** collection is not incremental, there are no "new white" objects +** during the sweep. So, any white object must be dead.) For +** non-dead objects, advance their ages and clear the color of +** new objects. (Old objects keep their colors.) +*/ static GCObject **sweepgen (lua_State *L, global_State *g, GCObject **p, GCObject *limit) { + static lu_byte nextage[] = { + G_SURVIVAL, /* from G_NEW */ + G_OLD1, /* from G_SURVIVAL */ + G_OLD1, /* from G_OLD0 */ + G_OLD, /* from G_OLD1 */ + G_OLD, /* from G_OLD (do not change) */ + G_TOUCHED1, /* from G_TOUCHED1 (do not change) */ + G_TOUCHED2 /* from G_TOUCHED2 (do not change) */ + }; int white = luaC_white(g); GCObject *curr; while ((curr = *p) != limit) { - int marked = curr->marked; if (iswhite(curr)) { /* is 'curr' dead? */ - lua_assert(!isold(curr) && !testbits(curr->marked, white)); + lua_assert(!isold(curr) && isdead(g, curr)); *p = curr->next; /* remove 'curr' from list */ freeobj(L, curr); /* erase 'curr' */ } else { /* correct mark and age */ - switch (getage(curr)) { - case G_NEW: /* make white and go to next age */ - curr->marked = cast_byte((marked & maskgencolors) | white); - changeage(curr, G_NEW, G_SURVIVAL); - break; - case G_SURVIVAL: /* go to next age */ - changeage(curr, G_SURVIVAL, G_OLD1); - break; - case G_OLD0: /* go to next age */ - changeage(curr, G_OLD0, G_OLD1); - break; - case G_OLD1: /* go to next age */ - changeage(curr, G_OLD1, G_OLD); - break; - default: /* don't change 'old', 'touched1', and 'touched2' */ - break; - } + if (getage(curr) == G_NEW) + curr->marked = cast_byte((curr->marked & maskgencolors) | white); + setage(curr, nextage[getage(curr)]); p = &curr->next; /* go to next element */ } } @@ -1014,20 +1035,16 @@ static GCObject **sweepgen (lua_State *L, global_State *g, GCObject **p, } +/* +** Traverse a list making all its elements white and clearing their +** age. +*/ static void whitelist (global_State *g, GCObject *p) { int white = luaC_white(g); for (; p != NULL; p = p->next) p->marked = cast_byte((p->marked & maskcolors) | white); } - -static void finishgencycle (lua_State *L, global_State *g) { - // sweepgen(L, &g->tobefnz, ~0, mask); - checkSizes(L, g); - g->gcstate = GCSpropagate; /* skip restart */ - callallpendingfinalizers(L); -} - static void printgray (GCObject *o) { printf("gray: "); while (o) { @@ -1065,7 +1082,14 @@ static void printgray (GCObject *o) { } - +/* +** Correct a list of gray objects. Because this correction is +** done after sweeping, young objects can be white and still +** be in the list. They are only removed. +** For tables, advance 'touched1' to 'touched2'; 'touched2' objects +** become regular old and are removed from the list. +** For threads, just remove white ones from the list. +*/ static GCObject **correctgraylist (GCObject **p) { GCObject *curr; while ((curr = *p) != NULL) { @@ -1105,6 +1129,9 @@ static GCObject **correctgraylist (GCObject **p) { } +/* +** Correct all gray lists, coalescing them into 'grayagain'. +*/ static void correctgraylists (global_State *g) { GCObject **list = correctgraylist(&g->grayagain); *list = g->weak; g->weak = NULL; @@ -1116,30 +1143,50 @@ static void correctgraylists (global_State *g) { } +/* +** Mark 'old1' objects when starting a new young collection. ('old1' +** tables are always black, threads are always gray.) +*/ static void markold (global_State *g, GCObject *from, GCObject *to) { GCObject *p; for (p = from; p != to; p = p->next) { if (getage(p) == G_OLD1) { - lua_assert(!iswhite(p)); + lua_assert((p->tt == LUA_TTHREAD) ? isgray(p) : isblack(p)); if (isblack(p)) { black2gray(p); /* should be '2white', but gray works too */ reallymarkobject(g, p); } - else - lua_assert(p->tt == LUA_TTHREAD); /* threads are always gray */ } } } +/* +** Finish a young-generation collection. +*/ +static void finishgencycle (lua_State *L, global_State *g) { + correctgraylists(g); + checkSizes(L, g); + g->gcstate = GCSpropagate; /* skip restart */ + callallpendingfinalizers(L); +} + + +/* +** Does a young collection. First, mark 'old1' objects. (Only survival +** and "recent old" lists can contain 'old1' objects. New lists cannot +** contain 'old1' objects, at most 'old0' objects that were already +** visited when marked old.) Then does the atomic step. Then, +** sweep all lists and advance pointers. Finally, finish the collection. +*/ static void youngcollection (lua_State *L, global_State *g) { - GCObject **psurvival; + GCObject **psurvival; /* to point to first non-dead survival object */ lua_assert(g->gcstate == GCSpropagate); markold(g, g->survival, g->reallyold); - markold(g, g->finobj, g->finobjrold); /* ??? */ + markold(g, g->finobj, g->finobjrold); atomic(L); - /* sweep nursery */ + /* sweep nursery and get a pointer to its last live element */ psurvival = sweepgen(L, g, &g->allgc, g->survival); /* sweep 'survival' and 'old' */ sweepgen(L, g, psurvival, g->reallyold); @@ -1158,13 +1205,15 @@ static void youngcollection (lua_State *L, global_State *g) { sweepgen(L, g, &g->tobefnz, NULL); finishgencycle(L, g); - correctgraylists(g); -//printf("check: \n");lua_checkmemory(L); } +/* +** Enter generational mode. Must go until the end of an atomic cycle +** to ensure that all threads are in the gray list. Then, turn all +** objects into old and finishes the collection. +*/ static void entergen (lua_State *L, global_State *g) { - lua_assert(g->reallyold == NULL && g->old == NULL && g->survival == NULL); luaC_runtilstate(L, bitmask(GCSpause)); /* prepare to start a new cycle */ luaC_runtilstate(L, bitmask(GCSpropagate)); /* start new cycle */ atomic(L); @@ -1177,12 +1226,18 @@ static void entergen (lua_State *L, global_State *g) { sweep2old(L, &g->finobj); g->finobjrold = g->finobjold = g->finobjsur = g->finobj; + sweep2old(L, &g->tobefnz); + finishgencycle(L, g); - correctgraylists(g); g->gckind = KGC_GEN; } +/* +** Enter incremental mode. Turn all objects white, make all +** intermediate lists point to NULL (to avoid invalid pointers), +** and go to pause state. +*/ static void enterinc (global_State *g) { makewhite(g, g->mainthread); whitelist(g, g->allgc); @@ -1195,9 +1250,12 @@ static void enterinc (global_State *g) { } +/* +** Change collector mode to 'newmode'. +*/ void luaC_changemode (lua_State *L, int newmode) { global_State *g = G(L); - if (newmode != g->gckind) { /* otherwise, nothing to be done */ + if (newmode != g->gckind) { if (newmode == KGC_GEN) /* entering generational mode? */ entergen(L, g); else @@ -1206,12 +1264,19 @@ void luaC_changemode (lua_State *L, int newmode) { } +/* +** Does a full collection in generational mode. +*/ static void fullgen (lua_State *L, global_State *g) { enterinc(g); entergen(L, g); } +/* +** Does a generational "step". For now that means a young +** collection. (We still has to implement the full control.) +*/ static void genstep (lua_State *L, global_State *g) { lu_mem mem; youngcollection(L, g); diff --git a/lgc.h b/lgc.h index c5dfcf2eb2..13896d4246 100644 --- a/lgc.h +++ b/lgc.h @@ -1,5 +1,5 @@ /* -** $Id: lgc.h,v 2.92 2017/02/23 21:07:34 roberto Exp roberto $ +** $Id: lgc.h,v 2.94 2017/04/06 13:08:56 roberto Exp roberto $ ** Garbage Collector ** See Copyright Notice in lua.h */ @@ -109,8 +109,8 @@ /* object age in generational mode */ #define G_NEW 0 /* created in current cycle */ #define G_SURVIVAL 1 /* created in previous cycle */ -#define G_OLD1 2 /* first full cycle as old */ -#define G_OLD0 3 /* marked old by frw. barrier in this cycle */ +#define G_OLD0 2 /* marked old by frw. barrier in this cycle */ +#define G_OLD1 3 /* first full cycle as old */ #define G_OLD 4 /* really old object (not to be visited) */ #define G_TOUCHED1 5 /* old object touched this cycle */ #define G_TOUCHED2 6 /* old object touched in previous cycle */ From a3d36fe283c09d4e56474da98f22d13162cc9fec Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 11 Apr 2017 15:41:09 -0300 Subject: [PATCH 0008/1145] Upvalues collected like everything else (with mark-sweep) instead of reference count (simpler and better for generational mode) --- lapi.c | 25 +++++++--------- lfunc.c | 47 +++++++++++++++++------------ lfunc.h | 18 ++--------- lgc.c | 89 ++++++++++++++++++++----------------------------------- lgc.h | 10 +------ lobject.h | 22 ++++++++++---- lstate.h | 4 ++- ltm.c | 4 +-- lvm.c | 5 ++-- 9 files changed, 97 insertions(+), 127 deletions(-) diff --git a/lapi.c b/lapi.c index 25ed81fdf9..eb67d0909d 100644 --- a/lapi.c +++ b/lapi.c @@ -1,5 +1,5 @@ /* -** $Id: lapi.c,v 2.260 2017/02/23 21:07:34 roberto Exp roberto $ +** $Id: lapi.c,v 2.261 2017/04/06 13:08:56 roberto Exp roberto $ ** Lua API ** See Copyright Notice in lua.h */ @@ -1004,7 +1004,7 @@ LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data, const TValue *gt = luaH_getint(reg, LUA_RIDX_GLOBALS); /* set global table as 1st upvalue of 'f' (may be LUA_ENV) */ setobj(L, f->upvals[0]->v, gt); - luaC_upvalbarrier(L, f->upvals[0], gt); + luaC_barrier(L, f->upvals[0], gt); } } lua_unlock(L); @@ -1202,13 +1202,13 @@ LUA_API void *lua_newuserdata (lua_State *L, size_t size) { static const char *aux_upvalue (StkId fi, int n, TValue **val, - CClosure **owner, UpVal **uv) { + GCObject **owner) { switch (ttype(fi)) { case LUA_TCCL: { /* C closure */ CClosure *f = clCvalue(fi); if (!(1 <= n && n <= f->nupvalues)) return NULL; *val = &f->upvalue[n-1]; - if (owner) *owner = f; + if (owner) *owner = obj2gco(f); return ""; } case LUA_TLCL: { /* Lua closure */ @@ -1217,7 +1217,7 @@ static const char *aux_upvalue (StkId fi, int n, TValue **val, Proto *p = f->p; if (!(1 <= n && n <= p->sizeupvalues)) return NULL; *val = f->upvals[n-1]->v; - if (uv) *uv = f->upvals[n - 1]; + if (owner) *owner = obj2gco(f->upvals[n - 1]); name = p->upvalues[n-1].name; return (name == NULL) ? "(*no name)" : getstr(name); } @@ -1230,7 +1230,7 @@ LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) { const char *name; TValue *val = NULL; /* to avoid warnings */ lua_lock(L); - name = aux_upvalue(index2addr(L, funcindex), n, &val, NULL, NULL); + name = aux_upvalue(index2addr(L, funcindex), n, &val, NULL); if (name) { setobj2s(L, L->top, val); api_incr_top(L); @@ -1243,18 +1243,16 @@ LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) { LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) { const char *name; TValue *val = NULL; /* to avoid warnings */ - CClosure *owner = NULL; - UpVal *uv = NULL; + GCObject *owner = NULL; /* to avoid warnings */ StkId fi; lua_lock(L); fi = index2addr(L, funcindex); api_checknelems(L, 1); - name = aux_upvalue(fi, n, &val, &owner, &uv); + name = aux_upvalue(fi, n, &val, &owner); if (name) { L->top--; setobj(L, val, L->top); - if (owner) { luaC_barrier(L, owner, val); } - else if (uv) { luaC_upvalbarrier(L, uv, val); } + luaC_barrier(L, owner, val); } lua_unlock(L); return name; @@ -1296,11 +1294,8 @@ LUA_API void lua_upvaluejoin (lua_State *L, int fidx1, int n1, LClosure *f1; UpVal **up1 = getupvalref(L, fidx1, n1, &f1); UpVal **up2 = getupvalref(L, fidx2, n2, NULL); - luaC_upvdeccount(L, *up1); *up1 = *up2; - (*up1)->refcount++; - if (upisopen(*up1)) (*up1)->u.open.touched = 1; - luaC_upvalbarrier(L, *up1, (*up1)->v); + luaC_objbarrier(L, f1, *up1); } diff --git a/lfunc.c b/lfunc.c index 0f839e7c04..898104acc7 100644 --- a/lfunc.c +++ b/lfunc.c @@ -1,5 +1,5 @@ /* -** $Id: lfunc.c,v 2.45 2014/11/02 19:19:04 roberto Exp roberto $ +** $Id: lfunc.c,v 2.46 2017/04/06 13:08:56 roberto Exp roberto $ ** Auxiliary functions to manipulate prototypes and closures ** See Copyright Notice in lua.h */ @@ -45,31 +45,35 @@ LClosure *luaF_newLclosure (lua_State *L, int n) { void luaF_initupvals (lua_State *L, LClosure *cl) { int i; for (i = 0; i < cl->nupvalues; i++) { - UpVal *uv = luaM_new(L, UpVal); - uv->refcount = 1; + GCObject *o = luaC_newobj(L, LUA_TUPVAL, sizeof(UpVal)); + UpVal *uv = gco2upv(o); uv->v = &uv->u.value; /* make it closed */ setnilvalue(uv->v); cl->upvals[i] = uv; + luaC_objbarrier(L, cl, o); } } UpVal *luaF_findupval (lua_State *L, StkId level) { UpVal **pp = &L->openupval; + GCObject *o; UpVal *p; UpVal *uv; lua_assert(isintwups(L) || L->openupval == NULL); - while (*pp != NULL && (p = *pp)->v >= level) { + while ((p = *pp) != NULL && p->v >= level) { lua_assert(upisopen(p)); if (p->v == level) /* found a corresponding upvalue? */ return p; /* return it */ pp = &p->u.open.next; } - /* not found: create a new upvalue */ - uv = luaM_new(L, UpVal); - uv->refcount = 0; - uv->u.open.next = *pp; /* link it to list of open upvalues */ - uv->u.open.touched = 1; + /* not found: create a new upvalue between 'pp' and 'p' */ + o = luaC_newobj(L, LUA_TUPVAL, sizeof(UpVal)); + uv = gco2upv(o); + uv->u.open.next = p; /* link it to list of open upvalues */ + uv->u.open.previous = pp; + if (p) + p->u.open.previous = &uv->u.open.next; *pp = uv; uv->v = level; /* current value lives in the stack */ if (!isintwups(L)) { /* thread not in list of threads with upvalues? */ @@ -80,19 +84,24 @@ UpVal *luaF_findupval (lua_State *L, StkId level) { } +void luaF_unlinkupval (UpVal *uv) { + lua_assert(upisopen(uv)); + *uv->u.open.previous = uv->u.open.next; + if (uv->u.open.next) + uv->u.open.next->u.open.previous = uv->u.open.previous; +} + + void luaF_close (lua_State *L, StkId level) { UpVal *uv; while (L->openupval != NULL && (uv = L->openupval)->v >= level) { - lua_assert(upisopen(uv)); - L->openupval = uv->u.open.next; /* remove from 'open' list */ - if (uv->refcount == 0) /* no references? */ - luaM_free(L, uv); /* free upvalue */ - else { - TValue *slot = &uv->u.value; /* new position for value */ - setobj(L, slot, uv->v); /* move value to upvalue slot */ - uv->v = slot; /* now current value lives here */ - luaC_upvalbarrier(L, uv, slot); - } + TValue *slot = &uv->u.value; /* new position for value */ + luaF_unlinkupval(uv); + setobj(L, slot, uv->v); /* move value to upvalue slot */ + uv->v = slot; /* now current value lives here */ + if (!iswhite(uv)) + gray2black(uv); /* closed upvalues cannot be gray */ + luaC_barrier(L, uv, slot); } } diff --git a/lfunc.h b/lfunc.h index 6fd3fbac03..7d0eca4af3 100644 --- a/lfunc.h +++ b/lfunc.h @@ -1,5 +1,5 @@ /* -** $Id: lfunc.h,v 2.14 2014/06/19 18:27:20 roberto Exp roberto $ +** $Id: lfunc.h,v 2.15 2015/01/13 15:49:11 roberto Exp roberto $ ** Auxiliary functions to manipulate prototypes and closures ** See Copyright Notice in lua.h */ @@ -29,21 +29,6 @@ #define MAXUPVAL 255 -/* -** Upvalues for Lua closures -*/ -struct UpVal { - TValue *v; /* points to stack or to its own value */ - lu_mem refcount; /* reference counter */ - union { - struct { /* (when open) */ - UpVal *next; /* linked list */ - int touched; /* mark to avoid cycles with dead threads */ - } open; - TValue value; /* the value (when closed) */ - } u; -}; - #define upisopen(up) ((up)->v != &(up)->u.value) @@ -53,6 +38,7 @@ LUAI_FUNC LClosure *luaF_newLclosure (lua_State *L, int nelems); LUAI_FUNC void luaF_initupvals (lua_State *L, LClosure *cl); LUAI_FUNC UpVal *luaF_findupval (lua_State *L, StkId level); LUAI_FUNC void luaF_close (lua_State *L, StkId level); +LUAI_FUNC void luaF_unlinkupval (UpVal *uv); LUAI_FUNC void luaF_freeproto (lua_State *L, Proto *f); LUAI_FUNC const char *luaF_getlocalname (const Proto *func, int local_number, int pc); diff --git a/lgc.c b/lgc.c index 68a7622f43..f42235884f 100644 --- a/lgc.c +++ b/lgc.c @@ -1,5 +1,5 @@ /* -** $Id: lgc.c,v 2.218 2017/04/06 13:08:56 roberto Exp roberto $ +** $Id: lgc.c,v 2.219 2017/04/10 13:33:04 roberto Exp roberto $ ** Garbage Collector ** See Copyright Notice in lua.h */ @@ -179,25 +179,11 @@ void luaC_barrierback_ (lua_State *L, Table *t) { } -/* -** barrier for assignments to closed upvalues. Because upvalues are -** shared among closures, it is impossible to know the color of all -** closures pointing to it. So, we assume that the object being assigned -** must be marked. -*/ -void luaC_upvalbarrier_ (lua_State *L, GCObject *o) { - global_State *g = G(L); - if (keepinvariant(g) && !isold(o)) { - markobject(g, o); - setage(o, G_OLD0); - } -} - - void luaC_fix (lua_State *L, GCObject *o) { global_State *g = G(L); lua_assert(g->allgc == o); /* object must be 1st in 'allgc' list! */ white2gray(o); /* they will be gray forever */ + setage(o, G_OLD); /* and old forever */ g->allgc = o->next; /* remove object from 'allgc' list */ o->next = g->fixedgc; /* link it to 'fixedgc' list */ g->fixedgc = o; @@ -230,10 +216,11 @@ GCObject *luaC_newobj (lua_State *L, int tt, size_t sz) { /* -** mark an object. Userdata, strings, and closed upvalues are visited +** Mark an object. Userdata, strings, and closed upvalues are visited ** and turned black here. Other objects are marked gray and added ** to appropriate list to be visited (and turned black) later. (Open -** upvalues are already linked in 'headuv' list.) +** upvalues are already linked in 'headuv' list. They are kept gray +** to avoid barriers, as their values will be revisited by the thread.) */ static void reallymarkobject (global_State *g, GCObject *o) { reentry: @@ -261,6 +248,14 @@ static void reallymarkobject (global_State *g, GCObject *o) { } break; } + case LUA_TUPVAL: { + UpVal *uv = gco2upv(o); + g->GCmemtrav += sizeof(UpVal); + if (!upisopen(uv)) /* open upvalues are kept gray */ + gray2black(o); + markvalue(g, uv->v); /* mark its content */ + break; + } case LUA_TLCL: { linkgclist(gco2lcl(o), g->gray); break; @@ -324,10 +319,8 @@ static void remarkupvals (global_State *g) { *p = thread->twups; /* remove thread from the list */ thread->twups = thread; /* mark that it is out of list */ for (uv = thread->openupval; uv != NULL; uv = uv->u.open.next) { - if (uv->u.open.touched) { - markvalue(g, uv->v); /* remark upvalue's value */ - uv->u.open.touched = 0; - } + if (!iswhite(uv)) /* upvalue already visited? */ + markvalue(g, uv->v); /* mark its value */ } } } @@ -516,22 +509,15 @@ static lu_mem traverseCclosure (global_State *g, CClosure *cl) { } /* -** open upvalues point to values in a thread, so those values should -** be marked when the thread is traversed except in the atomic phase -** (because then the value cannot be changed by the thread and the -** thread may not be traversed again) +** Traverse a Lua closure, marking its prototype and its upvalues. +** (Both can be NULL while closure is being created.) */ static lu_mem traverseLclosure (global_State *g, LClosure *cl) { int i; markobjectN(g, cl->p); /* mark its prototype */ for (i = 0; i < cl->nupvalues; i++) { /* visit its upvalues */ UpVal *uv = cl->upvals[i]; - if (uv != NULL) { /* can be NULL while closure is being built */ - if (upisopen(uv) && g->gcstate != GCSatomic) - uv->u.open.touched = 1; /* can be marked in 'remarkupvals' */ - else - markvalue(g, uv->v); - } + markobjectN(g, uv); /* mark upvalue */ } return sizeLclosure(cl->nupvalues); } @@ -569,7 +555,6 @@ static lu_mem traversethread (global_State *g, lua_State *th) { static void propagatemark (global_State *g) { lu_mem size; GCObject *o = g->gray; - lua_assert(ongraylist(o)); gray2black(o); switch (o->tt) { case LUA_TTABLE: { @@ -683,26 +668,10 @@ static void clearvalues (global_State *g, GCObject *l, GCObject *f) { } -/* -** Decrement the reference count of an upvalue. If it goes to zero and -** upvalue is closed, delete it. -*/ -void luaC_upvdeccount (lua_State *L, UpVal *uv) { - lua_assert(uv->refcount > 0); - uv->refcount--; - if (uv->refcount == 0 && !upisopen(uv)) - luaM_free(L, uv); -} - - -static void freeLclosure (lua_State *L, LClosure *cl) { - int i; - for (i = 0; i < cl->nupvalues; i++) { - UpVal *uv = cl->upvals[i]; - if (uv) - luaC_upvdeccount(L, uv); - } - luaM_freemem(L, cl, sizeLclosure(cl->nupvalues)); +static void freeupval (lua_State *L, UpVal *uv) { + if (upisopen(uv)) + luaF_unlinkupval(uv); + luaM_free(L, uv); } @@ -711,8 +680,11 @@ static void freeobj (lua_State *L, GCObject *o) { case LUA_TPROTO: luaF_freeproto(L, gco2p(o)); break; + case LUA_TUPVAL: + freeupval(L, gco2upv(o)); + break; case LUA_TLCL: - freeLclosure(L, gco2lcl(o)); + luaM_freemem(L, o, sizeLclosure(gco2lcl(o)->nupvalues)); break; case LUA_TCCL: luaM_freemem(L, o, sizeCclosure(gco2ccl(o)->nupvalues)); @@ -1144,14 +1116,14 @@ static void correctgraylists (global_State *g) { /* -** Mark 'old1' objects when starting a new young collection. ('old1' -** tables are always black, threads are always gray.) +** Mark 'old1' objects when starting a new young collection. (Threads +** and open upvalues are always gray, and do not need to be marked. +** All other old objects are black.) */ static void markold (global_State *g, GCObject *from, GCObject *to) { GCObject *p; for (p = from; p != to; p = p->next) { if (getage(p) == G_OLD1) { - lua_assert((p->tt == LUA_TTHREAD) ? isgray(p) : isblack(p)); if (isblack(p)) { black2gray(p); /* should be '2white', but gray works too */ reallymarkobject(g, p); @@ -1228,6 +1200,8 @@ static void entergen (lua_State *L, global_State *g) { sweep2old(L, &g->tobefnz); + setage(g->mainthread, G_OLD); + finishgencycle(L, g); g->gckind = KGC_GEN; } @@ -1282,6 +1256,7 @@ static void genstep (lua_State *L, global_State *g) { youngcollection(L, g); mem = gettotalbytes(g); luaE_setdebt(g, -((mem / 100) * 20)); +lua_checkmemory(L); } diff --git a/lgc.h b/lgc.h index 13896d4246..396f5a4e48 100644 --- a/lgc.h +++ b/lgc.h @@ -1,5 +1,5 @@ /* -** $Id: lgc.h,v 2.94 2017/04/06 13:08:56 roberto Exp roberto $ +** $Id: lgc.h,v 2.95 2017/04/10 13:33:04 roberto Exp roberto $ ** Garbage Collector ** See Copyright Notice in lua.h */ @@ -124,8 +124,6 @@ #define changeage(o,f,t) \ check_exp(getage(o) == (f), (o)->marked ^= ((f)^(t))) -#define ongraylist(o) (isgray(o) || getage(o) == G_TOUCHED2) - /* ** Does one step of collection when debt becomes positive. 'pre'/'pos' @@ -153,10 +151,6 @@ (isblack(p) && iswhite(o)) ? \ luaC_barrier_(L,obj2gco(p),obj2gco(o)) : cast_void(0)) -#define luaC_upvalbarrier(L,uv,x) ( \ - (iscollectable(x) && !upisopen(uv)) ? \ - luaC_upvalbarrier_(L,gcvalue(x)) : cast_void(0)) - LUAI_FUNC void luaC_fix (lua_State *L, GCObject *o); LUAI_FUNC void luaC_freeallobjects (lua_State *L); LUAI_FUNC void luaC_step (lua_State *L); @@ -165,9 +159,7 @@ LUAI_FUNC void luaC_fullgc (lua_State *L, int isemergency); LUAI_FUNC GCObject *luaC_newobj (lua_State *L, int tt, size_t sz); LUAI_FUNC void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v); LUAI_FUNC void luaC_barrierback_ (lua_State *L, Table *o); -LUAI_FUNC void luaC_upvalbarrier_ (lua_State *L, GCObject *o); LUAI_FUNC void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt); -LUAI_FUNC void luaC_upvdeccount (lua_State *L, UpVal *uv); LUAI_FUNC void luaC_changemode (lua_State *L, int newmode); diff --git a/lobject.h b/lobject.h index eeddfdef50..7c521ba250 100644 --- a/lobject.h +++ b/lobject.h @@ -1,5 +1,5 @@ /* -** $Id: lobject.h,v 2.116 2015/11/03 18:33:10 roberto Exp roberto $ +** $Id: lobject.h,v 2.117 2016/08/01 19:51:24 roberto Exp roberto $ ** Type definitions for Lua objects ** See Copyright Notice in lua.h */ @@ -19,8 +19,9 @@ /* ** Extra tags for non-values */ -#define LUA_TPROTO LUA_NUMTAGS /* function prototypes */ -#define LUA_TDEADKEY (LUA_NUMTAGS+1) /* removed keys in tables */ +#define LUA_TUPVAL LUA_NUMTAGS /* upvalues */ +#define LUA_TPROTO (LUA_NUMTAGS+1) /* function prototypes */ +#define LUA_TDEADKEY (LUA_NUMTAGS+2) /* removed keys in tables */ /* ** number of all possible tags (including LUA_TNONE but excluding DEADKEY) @@ -431,9 +432,20 @@ typedef struct Proto { /* -** Lua Upvalues +** Upvalues for Lua closures */ -typedef struct UpVal UpVal; +typedef struct UpVal { + CommonHeader; + TValue *v; /* points to stack or to its own value */ + union { + struct { /* (when open) */ + struct UpVal *next; /* linked list */ + struct UpVal **previous; + } open; + TValue value; /* the value (when closed) */ + } u; +} UpVal; + /* diff --git a/lstate.h b/lstate.h index b01859aa24..dc74370b34 100644 --- a/lstate.h +++ b/lstate.h @@ -1,5 +1,5 @@ /* -** $Id: lstate.h,v 2.135 2017/02/23 21:07:34 roberto Exp $ +** $Id: lstate.h,v 2.136 2017/04/05 16:50:51 roberto Exp roberto $ ** Global State ** See Copyright Notice in lua.h */ @@ -224,6 +224,7 @@ union GCUnion { struct Table h; struct Proto p; struct lua_State th; /* thread */ + struct UpVal upv; }; @@ -240,6 +241,7 @@ union GCUnion { #define gco2t(o) check_exp((o)->tt == LUA_TTABLE, &((cast_u(o))->h)) #define gco2p(o) check_exp((o)->tt == LUA_TPROTO, &((cast_u(o))->p)) #define gco2th(o) check_exp((o)->tt == LUA_TTHREAD, &((cast_u(o))->th)) +#define gco2upv(o) check_exp((o)->tt == LUA_TUPVAL, &((cast_u(o))->upv)) /* macro to convert a Lua object into a GCObject */ diff --git a/ltm.c b/ltm.c index b664dbed12..473413844a 100644 --- a/ltm.c +++ b/ltm.c @@ -1,5 +1,5 @@ /* -** $Id: ltm.c,v 2.37 2016/02/26 19:20:15 roberto Exp roberto $ +** $Id: ltm.c,v 2.38 2016/12/22 13:08:50 roberto Exp roberto $ ** Tag methods ** See Copyright Notice in lua.h */ @@ -30,7 +30,7 @@ LUAI_DDEF const char *const luaT_typenames_[LUA_TOTALTAGS] = { "no value", "nil", "boolean", udatatypename, "number", "string", "table", "function", udatatypename, "thread", - "proto" /* this last case is used for tests only */ + "upvalue", "proto" /* these last cases are used for tests only */ }; diff --git a/lvm.c b/lvm.c index 81b2a4815e..3295866554 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.268 2016/02/05 19:59:14 roberto Exp roberto $ +** $Id: lvm.c,v 2.269 2017/04/06 13:08:56 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -642,7 +642,6 @@ static void pushclosure (lua_State *L, Proto *p, UpVal **encup, StkId base, ncl->upvals[i] = luaF_findupval(L, base + uv[i].idx); else /* get upvalue from enclosing function */ ncl->upvals[i] = encup[uv[i].idx]; - ncl->upvals[i]->refcount++; /* new closure is white, so we do not need a barrier here */ } if (!isblack(p)) /* cache will not break GC invariant? */ @@ -855,7 +854,7 @@ void luaV_execute (lua_State *L) { vmcase(OP_SETUPVAL) { UpVal *uv = cl->upvals[GETARG_B(i)]; setobj(L, uv->v, ra); - luaC_upvalbarrier(L, uv, ra); + luaC_barrier(L, uv, ra); vmbreak; } vmcase(OP_SETTABLE) { From 0c8a7e071b00a1ea9822101f3b9fe4392d05c3c6 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 11 Apr 2017 16:00:27 -0300 Subject: [PATCH 0009/1145] 'mainthread' lives in 'allgc' list, like everybody else --- lgc.c | 16 ++++++---------- lstate.c | 7 ++++--- 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/lgc.c b/lgc.c index f42235884f..c0af55f4d6 100644 --- a/lgc.c +++ b/lgc.c @@ -1,5 +1,5 @@ /* -** $Id: lgc.c,v 2.219 2017/04/10 13:33:04 roberto Exp roberto $ +** $Id: lgc.c,v 2.220 2017/04/11 18:41:09 roberto Exp roberto $ ** Garbage Collector ** See Copyright Notice in lua.h */ @@ -1200,8 +1200,6 @@ static void entergen (lua_State *L, global_State *g) { sweep2old(L, &g->tobefnz); - setage(g->mainthread, G_OLD); - finishgencycle(L, g); g->gckind = KGC_GEN; } @@ -1213,7 +1211,6 @@ static void entergen (lua_State *L, global_State *g) { ** and go to pause state. */ static void enterinc (global_State *g) { - makewhite(g, g->mainthread); whitelist(g, g->allgc); g->reallyold = g->old = g->survival = NULL; whitelist(g, g->finobj); @@ -1305,8 +1302,8 @@ static void entersweep (lua_State *L) { } -static void deletealllist (lua_State *L, GCObject *p) { - while (p) { +static void deletealllist (lua_State *L, GCObject *p, GCObject *limit) { + while (p != limit) { GCObject *next = p->next; freeobj(L, p); p = next; @@ -1320,9 +1317,9 @@ void luaC_freeallobjects (lua_State *L) { separatetobefnz(g, 1); /* separate all objects with finalizers */ lua_assert(g->finobj == NULL); callallpendingfinalizers(L); - deletealllist(L, g->finobj); - deletealllist(L, g->allgc); - deletealllist(L, g->fixedgc); /* collect fixed objects */ + deletealllist(L, g->allgc, g->mainthread); + deletealllist(L, g->finobj, NULL); + deletealllist(L, g->fixedgc, NULL); /* collect fixed objects */ lua_assert(g->strt.nuse == 0); } @@ -1427,7 +1424,6 @@ static lu_mem singlestep (lua_State *L) { return sweepstep(L, g, GCSswpend, NULL); } case GCSswpend: { /* finish sweeps */ - makewhite(g, g->mainthread); /* sweep main thread */ checkSizes(L, g); g->gcstate = GCScallfin; return 0; diff --git a/lstate.c b/lstate.c index d2f3858892..0b731c8739 100644 --- a/lstate.c +++ b/lstate.c @@ -1,5 +1,5 @@ /* -** $Id: lstate.c,v 2.134 2017/02/23 21:07:34 roberto Exp roberto $ +** $Id: lstate.c,v 2.135 2017/04/05 16:50:51 roberto Exp $ ** Global State ** See Copyright Notice in lua.h */ @@ -300,11 +300,12 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { if (l == NULL) return NULL; L = &l->l.l; g = &l->g; - L->next = NULL; L->tt = LUA_TTHREAD; g->currentwhite = bitmask(WHITE0BIT); L->marked = luaC_white(g); preinit_thread(L, g); + g->allgc = obj2gco(L); /* by now, only object is the main thread */ + L->next = NULL; g->frealloc = f; g->ud = ud; g->mainthread = L; @@ -318,7 +319,7 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { g->version = NULL; g->gcstate = GCSpause; g->gckind = KGC_NORMAL; - g->allgc = g->finobj = g->tobefnz = g->fixedgc = NULL; + g->finobj = g->tobefnz = g->fixedgc = NULL; g->survival = g->old = g->reallyold = NULL; g->finobjsur = g->finobjold = g->finobjrold = NULL; g->sweepgc = NULL; From 16001acb15456c832474edc9f0323e0238e28a11 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 12 Apr 2017 15:01:40 -0300 Subject: [PATCH 0010/1145] small corrections + removal of debugging functions 'count' and 'printgray'. --- lgc.c | 52 ++++------------------------------------------------ 1 file changed, 4 insertions(+), 48 deletions(-) diff --git a/lgc.c b/lgc.c index c0af55f4d6..c8d401e6a0 100644 --- a/lgc.c +++ b/lgc.c @@ -1,5 +1,5 @@ /* -** $Id: lgc.c,v 2.220 2017/04/11 18:41:09 roberto Exp roberto $ +** $Id: lgc.c,v 2.221 2017/04/11 19:00:27 roberto Exp roberto $ ** Garbage Collector ** See Copyright Notice in lua.h */ @@ -425,6 +425,8 @@ static int traverseephemeron (global_State *g, Table *h) { linkgclist(h, g->allweak); /* may have to clean white keys */ else if (g->gckind == KGC_GEN) linkgclist(h, g->grayagain); /* keep it in some list */ + else + gray2black(h); return marked; } @@ -939,16 +941,6 @@ void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt) { /* mask to erase all color bits, not changing gen-related stuff */ #define maskgencolors (~(bitmask(BLACKBIT) | WHITEBITS)) -#if 0 -static int count (GCObject *p, GCObject *limit) { - int res = 0; - for (; p != NULL && p != limit; p = p->next) { - res++; - } - return res; -} -#endif - /* ** Sweep a list of objects, deleting dead ones and turning @@ -1017,42 +1009,6 @@ static void whitelist (global_State *g, GCObject *p) { p->marked = cast_byte((p->marked & maskcolors) | white); } -static void printgray (GCObject *o) { - printf("gray: "); - while (o) { - printf("%p %d %02x ", (void*)o, o->tt, o->marked); - switch (o->tt) { - case LUA_TTABLE: { - Table *h = gco2t(o); - o = h->gclist; - break; - } - case LUA_TLCL: { - LClosure *cl = gco2lcl(o); - o = cl->gclist; - break; - } - case LUA_TCCL: { - CClosure *cl = gco2ccl(o); - o = cl->gclist; - break; - } - case LUA_TTHREAD: { - lua_State *th = gco2th(o); - o = th->gclist; - break; - } - case LUA_TPROTO: { - Proto *p = gco2p(o); - o = p->gclist; - break; - } - default: lua_assert(0); return; - } - } - printf("\n"); -} - /* ** Correct a list of gray objects. Because this correction is @@ -1317,7 +1273,7 @@ void luaC_freeallobjects (lua_State *L) { separatetobefnz(g, 1); /* separate all objects with finalizers */ lua_assert(g->finobj == NULL); callallpendingfinalizers(L); - deletealllist(L, g->allgc, g->mainthread); + deletealllist(L, g->allgc, obj2gco(g->mainthread)); deletealllist(L, g->finobj, NULL); deletealllist(L, g->fixedgc, NULL); /* collect fixed objects */ lua_assert(g->strt.nuse == 0); From f74b87c3c29fb052adc681317a54dfbe4a5a281b Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 12 Apr 2017 15:56:25 -0300 Subject: [PATCH 0011/1145] removed initialization of 'GCestimate' (it is initialized during a GC cycle, when it start counting) --- lstate.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lstate.c b/lstate.c index 0b731c8739..adc1bce902 100644 --- a/lstate.c +++ b/lstate.c @@ -1,5 +1,5 @@ /* -** $Id: lstate.c,v 2.135 2017/04/05 16:50:51 roberto Exp $ +** $Id: lstate.c,v 2.136 2017/04/11 19:00:27 roberto Exp roberto $ ** Global State ** See Copyright Notice in lua.h */ @@ -311,7 +311,6 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { g->mainthread = L; g->seed = makeseed(L); g->gcrunning = 0; /* no GC while building state */ - g->GCestimate = 0; g->strt.size = g->strt.nuse = 0; g->strt.hash = NULL; setnilvalue(&g->l_registry); From 46792947965aa2b28232c212add07b08749e7adf Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 18 Apr 2017 16:42:12 -0300 Subject: [PATCH 0012/1145] memory check adapted to generational mode --- ltests.c | 196 +++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 133 insertions(+), 63 deletions(-) diff --git a/ltests.c b/ltests.c index ac177de87e..4022809cd5 100644 --- a/ltests.c +++ b/ltests.c @@ -1,5 +1,5 @@ /* -** $Id: ltests.c,v 2.211 2016/12/04 20:17:24 roberto Exp roberto $ +** $Id: ltests.c,v 2.212 2017/02/23 21:07:34 roberto Exp roberto $ ** Internal Module for Debugging of the Lua Implementation ** See Copyright Notice in lua.h */ @@ -186,11 +186,22 @@ void *debug_realloc (void *ud, void *b, size_t oldsize, size_t size) { */ +/* +** Check GC invariants. For incremental mode, a black object cannot +** point to a white one. For generational mode, really old objects +** cannot point to young objects. (Threads and open upvalues, despite +** being marked "really old", continue to be visited in all collections, +** and therefore can point to new objects. They, and only they, are +** old but gray.) +*/ static int testobjref1 (global_State *g, GCObject *f, GCObject *t) { if (isdead(g,t)) return 0; - if (!issweepphase(g)) - return !(isblack(f) && iswhite(t)); - else return 1; + if (issweepphase(g)) + return 1; /* no invariants */ + else if (g->gckind == KGC_NORMAL) + return !(isblack(f) && iswhite(t)); /* basic incremental invariant */ + else + return !((getage(f) == G_OLD && isblack(f)) && !isold(t)); } @@ -198,8 +209,7 @@ static void printobj (global_State *g, GCObject *o) { printf("||%s(%p)-%c%c(%02X)||", ttypename(novariant(o->tt)), (void *)o, isdead(g,o) ? 'd' : isblack(o) ? 'b' : iswhite(o) ? 'w' : 'g', - testbit(o->marked, OLDBIT) ? 'o' : 'n', - o->marked); + "ns01oTt"[getage(o)], o->marked); if (o->tt == LUA_TSHRSTR || o->tt == LUA_TLNGSTR) printf(" '%s'", getstr(gco2ts(o))); } @@ -282,9 +292,9 @@ static void checkLclosure (global_State *g, LClosure *cl) { for (i=0; inupvalues; i++) { UpVal *uv = cl->upvals[i]; if (uv) { - if (!upisopen(uv)) /* only closed upvalues matter to invariant */ - checkvalref(g, clgc, uv->v); - lua_assert(uv->refcount > 0); + checkobjref(g, clgc, uv); + if (!upisopen(uv)) + checkvalref(g, obj2gco(uv), uv->v); } } } @@ -323,47 +333,67 @@ static void checkstack (global_State *g, lua_State *L1) { } -static void checkobject (global_State *g, GCObject *o, int maybedead) { +static void checkrefs (global_State *g, GCObject *o) { + switch (o->tt) { + case LUA_TUSERDATA: { + TValue uservalue; + Table *mt = gco2u(o)->metatable; + checkobjref(g, o, mt); + getuservalue(g->mainthread, gco2u(o), &uservalue); + checkvalref(g, o, &uservalue); + break; + } + case LUA_TUPVAL: { + checkvalref(g, o, gco2upv(o)->v); + break; + } + case LUA_TTABLE: { + checktable(g, gco2t(o)); + break; + } + case LUA_TTHREAD: { + checkstack(g, gco2th(o)); + break; + } + case LUA_TLCL: { + checkLclosure(g, gco2lcl(o)); + break; + } + case LUA_TCCL: { + checkCclosure(g, gco2ccl(o)); + break; + } + case LUA_TPROTO: { + checkproto(g, gco2p(o)); + break; + } + case LUA_TSHRSTR: + case LUA_TLNGSTR: { + lua_assert(!isgray(o)); /* strings are never gray */ + break; + } + default: lua_assert(0); + } +} + + +static void checkobject (global_State *g, GCObject *o, int maybedead, + int listage) { if (isdead(g, o)) lua_assert(maybedead); else { lua_assert(g->gcstate != GCSpause || iswhite(o)); - switch (o->tt) { - case LUA_TUSERDATA: { - TValue uservalue; - Table *mt = gco2u(o)->metatable; - checkobjref(g, o, mt); - getuservalue(g->mainthread, gco2u(o), &uservalue); - checkvalref(g, o, &uservalue); - break; + if (g->gckind == KGC_GEN) { /* generational mode? */ + lua_assert(getage(o) >= listage); + lua_assert(!iswhite(o) || !isold(o)); + if (isold(o)) { + lua_assert(isblack(o) || + getage(o) == G_TOUCHED1 || + o->tt == LUA_TTHREAD || + (o->tt == LUA_TUPVAL && upisopen(gco2upv(o)))); } - case LUA_TTABLE: { - checktable(g, gco2t(o)); - break; - } - case LUA_TTHREAD: { - checkstack(g, gco2th(o)); - break; - } - case LUA_TLCL: { - checkLclosure(g, gco2lcl(o)); - break; - } - case LUA_TCCL: { - checkCclosure(g, gco2ccl(o)); - break; - } - case LUA_TPROTO: { - checkproto(g, gco2p(o)); - break; - } - case LUA_TSHRSTR: - case LUA_TLNGSTR: { - lua_assert(!isgray(o)); /* strings are never gray */ - break; - } - default: lua_assert(0); } + checkrefs(g, o); } } @@ -371,7 +401,7 @@ static void checkobject (global_State *g, GCObject *o, int maybedead) { static void checkgraylist (global_State *g, GCObject *o) { ((void)g); /* better to keep it available if we need to print an object */ while (o) { - lua_assert(isgray(o)); + lua_assert(isgray(o) || getage(o) == G_TOUCHED2); lua_assert(!testbit(o->marked, TESTGRAYBIT)); l_setbit(o->marked, TESTGRAYBIT); switch (o->tt) { @@ -380,7 +410,7 @@ static void checkgraylist (global_State *g, GCObject *o) { case LUA_TCCL: o = gco2ccl(o)->gclist; break; case LUA_TTHREAD: o = gco2th(o)->gclist; break; case LUA_TPROTO: o = gco2p(o)->gclist; break; - default: lua_assert(0); /* other objects cannot be gray */ + default: lua_assert(0); /* other objects cannot be in a gray list */ } } } @@ -402,7 +432,7 @@ static void markgrays (global_State *g) { static void checkgray (global_State *g, GCObject *o) { for (; o != NULL; o = o->next) { - if (isgray(o)) { + if ((isgray(o) && o->tt != LUA_TUPVAL) || getage(o) == G_TOUCHED2) { lua_assert(!keepinvariant(g) || testbit(o->marked, TESTGRAYBIT)); resetbit(o->marked, TESTGRAYBIT); } @@ -411,6 +441,28 @@ static void checkgray (global_State *g, GCObject *o) { } +static void checklist (global_State *g, int maybedead, int tof, + GCObject *new, GCObject *survival, GCObject *old, GCObject *reallyold) { + GCObject *o; + for (o = new; o != survival; o = o->next) { + checkobject(g, o, maybedead, G_NEW); + lua_assert(!tof == !tofinalize(o)); + } + for (o = survival; o != old; o = o->next) { + checkobject(g, o, 0, G_SURVIVAL); + lua_assert(!tof == !tofinalize(o)); + } + for (o = old; o != reallyold; o = o->next) { + checkobject(g, o, 0, G_OLD1); + lua_assert(!tof == !tofinalize(o)); + } + for (o = reallyold; o != NULL; o = o->next) { + checkobject(g, o, 0, G_OLD); + lua_assert(!tof == !tofinalize(o)); + } +} + + int lua_checkmemory (lua_State *L) { global_State *g = G(L); GCObject *o; @@ -420,32 +472,27 @@ int lua_checkmemory (lua_State *L) { lua_assert(!iswhite(gcvalue(&g->l_registry))); } lua_assert(!isdead(g, gcvalue(&g->l_registry))); - checkstack(g, g->mainthread); - resetbit(g->mainthread->marked, TESTGRAYBIT); lua_assert(g->sweepgc == NULL || issweepphase(g)); markgrays(g); + /* check 'fixedgc' list */ for (o = g->fixedgc; o != NULL; o = o->next) { - lua_assert(o->tt == LUA_TSHRSTR && isgray(o)); + lua_assert(o->tt == LUA_TSHRSTR && isgray(o) && getage(o) == G_OLD); } + /* check 'allgc' list */ checkgray(g, g->allgc); maybedead = (GCSatomic < g->gcstate && g->gcstate <= GCSswpallgc); - for (o = g->allgc; o != NULL; o = o->next) { - checkobject(g, o, maybedead); - lua_assert(!tofinalize(o)); - } + checklist(g, maybedead, 0, g->allgc, g->survival, g->old, g->reallyold); + /* check 'finobj' list */ checkgray(g, g->finobj); - for (o = g->finobj; o != NULL; o = o->next) { - checkobject(g, o, 0); - lua_assert(tofinalize(o)); - lua_assert(o->tt == LUA_TUSERDATA || o->tt == LUA_TTABLE); - } + checklist(g, 0, 1, g->finobj, g->finobjsur, g->finobjold, g->finobjrold); + /* check 'tobefnz' list */ checkgray(g, g->tobefnz); for (o = g->tobefnz; o != NULL; o = o->next) { - checkobject(g, o, 0); + checkobject(g, o, 0, G_NEW); lua_assert(tofinalize(o)); lua_assert(o->tt == LUA_TUSERDATA || o->tt == LUA_TTABLE); } @@ -621,22 +668,42 @@ static int gc_color (lua_State *L) { TValue *o; luaL_checkany(L, 1); o = obj_at(L, 1); - if (!iscollectable(o)) + if (!iscollectable(o)) { lua_pushstring(L, "no collectable"); + return 1; + } else { + static const char *gennames[] = {"new", "survival", "old0", "old1", + "old", "touched1", "touched2"}; GCObject *obj = gcvalue(o); lua_pushstring(L, isdead(G(L), obj) ? "dead" : iswhite(obj) ? "white" : isblack(obj) ? "black" : "grey"); + lua_pushstring(L, gennames[getage(obj)]); + return 2; } - return 1; +} + + +static int gc_printobj (lua_State *L) { + TValue *o; + luaL_checkany(L, 1); + o = obj_at(L, 1); + if (!iscollectable(o)) + printf("no collectable\n"); + else { + GCObject *obj = gcvalue(o); + printobj(G(L), obj); + printf("\n"); + } + return 0; } static int gc_state (lua_State *L) { static const char *statenames[] = {"propagate", "atomic", "sweepallgc", "sweepfinobj", "sweeptobefnz", "sweepend", "pause", ""}; - static const int states[] = {GCSpropagate, GCSatomic, GCSswpallgc, + static const int states[] = {GCSpropagate, GCSenteratomic, GCSswpallgc, GCSswpfinobj, GCSswptobefnz, GCSswpend, GCSpause, -1}; int option = states[luaL_checkoption(L, 1, "", statenames)]; if (option == -1) { @@ -645,6 +712,8 @@ static int gc_state (lua_State *L) { } else { global_State *g = G(L); + if (G(L)->gckind == KGC_GEN) + luaL_error(L, "cannot change states in generational mode"); lua_lock(L); if (option < g->gcstate) { /* must cross 'pause'? */ luaC_runtilstate(L, bitmask(GCSpause)); /* run until pause */ @@ -1519,6 +1588,7 @@ static const struct luaL_Reg tests_funcs[] = { {"doremote", doremote}, {"gccolor", gc_color}, {"gcstate", gc_state}, + {"pobj", gc_printobj}, {"getref", getref}, {"hash", hash_query}, {"int2fb", int2fb_aux}, From 9e1f1b1f6230f71d95eba4457d8ac2719ed9e7c7 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 19 Apr 2017 09:49:17 -0300 Subject: [PATCH 0013/1145] detail in usage message for '-l' option --- lua.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lua.c b/lua.c index 62de0f5853..2f011861dc 100644 --- a/lua.c +++ b/lua.c @@ -1,5 +1,5 @@ /* -** $Id: lua.c,v 1.229 2016/12/22 13:08:50 roberto Exp roberto $ +** $Id: lua.c,v 1.230 2017/01/12 17:14:26 roberto Exp roberto $ ** Lua stand-alone interpreter ** See Copyright Notice in lua.h */ @@ -138,7 +138,7 @@ static void print_usage (const char *badoption) { "Available options are:\n" " -e stat execute string 'stat'\n" " -i enter interactive mode after executing 'script'\n" - " -l name require library 'name'\n" + " -l name require library 'name' into global 'name'\n" " -v show version information\n" " -E ignore environment variables\n" " -- stop handling options\n" From a45945b6d511b00b1c84dc73881474030737e956 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 19 Apr 2017 13:34:35 -0300 Subject: [PATCH 0014/1145] new macro 'lua_pointer2str' to encapsulate use of 'l_sprintf' inside the kernel --- lobject.c | 5 +++-- luaconf.h | 9 ++++++++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/lobject.c b/lobject.c index e234df3d61..4a7006b18c 100644 --- a/lobject.c +++ b/lobject.c @@ -1,5 +1,5 @@ /* -** $Id: lobject.c,v 2.112 2016/06/27 13:15:08 roberto Exp roberto $ +** $Id: lobject.c,v 2.113 2016/12/22 13:08:50 roberto Exp roberto $ ** Some generic functions over Lua objects ** See Copyright Notice in lua.h */ @@ -435,7 +435,8 @@ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) { } case 'p': { /* a pointer */ char buff[4*sizeof(void *) + 8]; /* should be enough space for a '%p' */ - int l = l_sprintf(buff, sizeof(buff), "%p", va_arg(argp, void *)); + void *p = va_arg(argp, void *); + int l = lua_pointer2str(buff, sizeof(buff), p); pushstr(L, buff, l); break; } diff --git a/luaconf.h b/luaconf.h index 118f997a99..33ff360458 100644 --- a/luaconf.h +++ b/luaconf.h @@ -1,5 +1,5 @@ /* -** $Id: luaconf.h,v 1.258 2016/12/20 18:37:00 roberto Exp roberto $ +** $Id: luaconf.h,v 1.259 2016/12/22 13:08:50 roberto Exp roberto $ ** Configuration file for Lua ** See Copyright Notice in lua.h */ @@ -620,6 +620,13 @@ #endif +/* +@@ lua_pointer2str converts a pointer to a readable string in a +** non-specified way. +*/ +#define lua_pointer2str(buff,sz,p) l_sprintf(buff,sz,"%p",p) + + /* @@ lua_number2strx converts a float to an hexadecimal numeric string. ** In C99, 'sprintf' (with format specifiers '%a'/'%A') does that. From c7bdc0e0e8ab03820b472a87b87c04475def5997 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 19 Apr 2017 14:02:50 -0300 Subject: [PATCH 0015/1145] first version of control for the generational collector --- lapi.c | 6 +++++- lgc.c | 18 +++++++++++++----- lstate.h | 4 +++- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/lapi.c b/lapi.c index eb67d0909d..9bd4a55b2b 100644 --- a/lapi.c +++ b/lapi.c @@ -1,5 +1,5 @@ /* -** $Id: lapi.c,v 2.261 2017/04/06 13:08:56 roberto Exp roberto $ +** $Id: lapi.c,v 2.262 2017/04/11 18:41:09 roberto Exp roberto $ ** Lua API ** See Copyright Notice in lua.h */ @@ -1098,6 +1098,10 @@ LUA_API int lua_gc (lua_State *L, int what, int data) { break; } case LUA_GCGEN: { + lu_byte aux = data & 0xff; + g->genminormul = (aux == 0) ? 20 : aux; + aux = (data >> 8) & 0xff; + g->genmajormul = (aux == 0) ? 100 : aux; luaC_changemode(L, KGC_GEN); break; } diff --git a/lgc.c b/lgc.c index c8d401e6a0..9995fbed90 100644 --- a/lgc.c +++ b/lgc.c @@ -1,5 +1,5 @@ /* -** $Id: lgc.c,v 2.221 2017/04/11 19:00:27 roberto Exp roberto $ +** $Id: lgc.c,v 2.222 2017/04/12 18:01:40 roberto Exp roberto $ ** Garbage Collector ** See Copyright Notice in lua.h */ @@ -1158,6 +1158,7 @@ static void entergen (lua_State *L, global_State *g) { finishgencycle(L, g); g->gckind = KGC_GEN; + g->GCestimate = gettotalbytes(g); /* base for memory control */ } @@ -1205,10 +1206,17 @@ static void fullgen (lua_State *L, global_State *g) { ** collection. (We still has to implement the full control.) */ static void genstep (lua_State *L, global_State *g) { - lu_mem mem; - youngcollection(L, g); - mem = gettotalbytes(g); - luaE_setdebt(g, -((mem / 100) * 20)); + lu_mem majorbase = g->GCestimate; +lua_checkmemory(L); + if (gettotalbytes(g) > (majorbase / 100) * (100 + g->genmajormul)) + fullgen(L, g); + else { + lu_mem mem; + youngcollection(L, g); + mem = gettotalbytes(g); + luaE_setdebt(g, -((mem / 100) * g->genminormul)); + g->GCestimate = mem; + } lua_checkmemory(L); } diff --git a/lstate.h b/lstate.h index dc74370b34..d844fc8ce8 100644 --- a/lstate.h +++ b/lstate.h @@ -1,5 +1,5 @@ /* -** $Id: lstate.h,v 2.136 2017/04/05 16:50:51 roberto Exp roberto $ +** $Id: lstate.h,v 2.137 2017/04/11 18:41:09 roberto Exp roberto $ ** Global State ** See Copyright Notice in lua.h */ @@ -148,6 +148,8 @@ typedef struct global_State { lu_byte currentwhite; lu_byte gcstate; /* state of garbage collector */ lu_byte gckind; /* kind of GC running */ + lu_byte genminormul; /* control for minor generational collections */ + lu_byte genmajormul; /* control for major generational collections */ lu_byte gcrunning; /* true if GC is running */ GCObject *allgc; /* list of all collectable objects */ GCObject **sweepgc; /* current position of sweep in list */ From 7ae180f8e8c987c1992ad95b3f5f31c69e5650e0 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 19 Apr 2017 15:46:47 -0300 Subject: [PATCH 0016/1145] corrected some checks about colors of old objects + new test function 'gcage' --- ltests.c | 61 ++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 46 insertions(+), 15 deletions(-) diff --git a/ltests.c b/ltests.c index 4022809cd5..9679bee297 100644 --- a/ltests.c +++ b/ltests.c @@ -1,5 +1,5 @@ /* -** $Id: ltests.c,v 2.212 2017/02/23 21:07:34 roberto Exp roberto $ +** $Id: ltests.c,v 2.213 2017/04/18 19:42:12 roberto Exp roberto $ ** Internal Module for Debugging of the Lua Implementation ** See Copyright Notice in lua.h */ @@ -189,10 +189,11 @@ void *debug_realloc (void *ud, void *b, size_t oldsize, size_t size) { /* ** Check GC invariants. For incremental mode, a black object cannot ** point to a white one. For generational mode, really old objects -** cannot point to young objects. (Threads and open upvalues, despite -** being marked "really old", continue to be visited in all collections, -** and therefore can point to new objects. They, and only they, are -** old but gray.) +** cannot point to young objects. Both old1 and touched2 objects +** cannot point to new objects (but can point to survivals). +** (Threads and open upvalues, despite being marked "really old", +** continue to be visited in all collections, and therefore can point to +** new objects. They, and only they, are old but gray.) */ static int testobjref1 (global_State *g, GCObject *f, GCObject *t) { if (isdead(g,t)) return 0; @@ -200,8 +201,14 @@ static int testobjref1 (global_State *g, GCObject *f, GCObject *t) { return 1; /* no invariants */ else if (g->gckind == KGC_NORMAL) return !(isblack(f) && iswhite(t)); /* basic incremental invariant */ - else - return !((getage(f) == G_OLD && isblack(f)) && !isold(t)); + else { /* generational mode */ + if ((getage(f) == G_OLD && isblack(f)) && !isold(t)) + return 0; + if (((getage(f) == G_OLD1 || getage(f) == G_TOUCHED2) && isblack(f)) && + getage(t) == G_NEW) + return 0; + return 1; + } } @@ -377,6 +384,17 @@ static void checkrefs (global_State *g, GCObject *o) { } +/* +** Check consistency of an object: +** - Dead objects can only happen in the 'allgc' list during a sweep +** phase (controled by the caller through 'maybedead'). +** - During pause, all objects must be white. +** - In generational mode: +** * objects must be old enough for their lists ('listage'). +** * old objects cannot be white. +** * old objects must be black, except for 'touched1', 'old0', +** threads, and open upvalues. +*/ static void checkobject (global_State *g, GCObject *o, int maybedead, int listage) { if (isdead(g, o)) @@ -389,6 +407,7 @@ static void checkobject (global_State *g, GCObject *o, int maybedead, if (isold(o)) { lua_assert(isblack(o) || getage(o) == G_TOUCHED1 || + getage(o) == G_OLD0 || o->tt == LUA_TTHREAD || (o->tt == LUA_TUPVAL && upisopen(gco2upv(o)))); } @@ -442,9 +461,9 @@ static void checkgray (global_State *g, GCObject *o) { static void checklist (global_State *g, int maybedead, int tof, - GCObject *new, GCObject *survival, GCObject *old, GCObject *reallyold) { + GCObject *newl, GCObject *survival, GCObject *old, GCObject *reallyold) { GCObject *o; - for (o = new; o != survival; o = o->next) { + for (o = newl; o != survival; o = o->next) { checkobject(g, o, maybedead, G_NEW); lua_assert(!tof == !tofinalize(o)); } @@ -668,20 +687,31 @@ static int gc_color (lua_State *L) { TValue *o; luaL_checkany(L, 1); o = obj_at(L, 1); - if (!iscollectable(o)) { + if (!iscollectable(o)) lua_pushstring(L, "no collectable"); - return 1; - } else { - static const char *gennames[] = {"new", "survival", "old0", "old1", - "old", "touched1", "touched2"}; GCObject *obj = gcvalue(o); lua_pushstring(L, isdead(G(L), obj) ? "dead" : iswhite(obj) ? "white" : isblack(obj) ? "black" : "grey"); + } + return 1; +} + + +static int gc_age (lua_State *L) { + TValue *o; + luaL_checkany(L, 1); + o = obj_at(L, 1); + if (!iscollectable(o)) + lua_pushstring(L, "no collectable"); + else { + static const char *gennames[] = {"new", "survival", "old0", "old1", + "old", "touched1", "touched2"}; + GCObject *obj = gcvalue(o); lua_pushstring(L, gennames[getage(obj)]); - return 2; } + return 1; } @@ -1587,6 +1617,7 @@ static const struct luaL_Reg tests_funcs[] = { {"doonnewstack", doonnewstack}, {"doremote", doremote}, {"gccolor", gc_color}, + {"gcage", gc_age}, {"gcstate", gc_state}, {"pobj", gc_printobj}, {"getref", getref}, From f748b4bb40bf94303d5cc609006054c0e9b7b2d7 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 20 Apr 2017 15:22:44 -0300 Subject: [PATCH 0017/1145] macros to define default parameters for generational collection --- lapi.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/lapi.c b/lapi.c index 9bd4a55b2b..29f3d2650d 100644 --- a/lapi.c +++ b/lapi.c @@ -1,5 +1,5 @@ /* -** $Id: lapi.c,v 2.262 2017/04/11 18:41:09 roberto Exp roberto $ +** $Id: lapi.c,v 2.263 2017/04/19 17:02:50 roberto Exp roberto $ ** Lua API ** See Copyright Notice in lua.h */ @@ -1036,6 +1036,11 @@ LUA_API int lua_status (lua_State *L) { ** Garbage-collection function */ +#if !defined(LUA_GENMAJORMUL) +#define LUA_GENMAJORMUL 100 +#define LUA_GENMINORMUL 5 +#endif + LUA_API int lua_gc (lua_State *L, int what, int data) { int res = 0; global_State *g; @@ -1099,9 +1104,9 @@ LUA_API int lua_gc (lua_State *L, int what, int data) { } case LUA_GCGEN: { lu_byte aux = data & 0xff; - g->genminormul = (aux == 0) ? 20 : aux; + g->genminormul = (aux == 0) ? LUA_GENMINORMUL : aux; aux = (data >> 8) & 0xff; - g->genmajormul = (aux == 0) ? 100 : aux; + g->genmajormul = (aux == 0) ? LUA_GENMAJORMUL : aux; luaC_changemode(L, KGC_GEN); break; } From c354211744e80c14314b3a363e7d57f5751be835 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 20 Apr 2017 15:24:33 -0300 Subject: [PATCH 0018/1145] small bug in generational control --- lgc.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/lgc.c b/lgc.c index 9995fbed90..59b6dde160 100644 --- a/lgc.c +++ b/lgc.c @@ -1,5 +1,5 @@ /* -** $Id: lgc.c,v 2.222 2017/04/12 18:01:40 roberto Exp roberto $ +** $Id: lgc.c,v 2.223 2017/04/19 17:02:50 roberto Exp roberto $ ** Garbage Collector ** See Copyright Notice in lua.h */ @@ -1207,17 +1207,18 @@ static void fullgen (lua_State *L, global_State *g) { */ static void genstep (lua_State *L, global_State *g) { lu_mem majorbase = g->GCestimate; -lua_checkmemory(L); - if (gettotalbytes(g) > (majorbase / 100) * (100 + g->genmajormul)) +//lua_checkmemory(L); + if (gettotalbytes(g) > (majorbase / 100) * (100 + g->genmajormul)) { fullgen(L, g); + } else { lu_mem mem; youngcollection(L, g); mem = gettotalbytes(g); luaE_setdebt(g, -((mem / 100) * g->genminormul)); - g->GCestimate = mem; + g->GCestimate = majorbase; /* preserve base value */ } -lua_checkmemory(L); +//lua_checkmemory(L); } From 6a98aa0bb0426acdf4a9ef75b1d3200fd972e77d Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 20 Apr 2017 16:53:55 -0300 Subject: [PATCH 0019/1145] new opcode LOADI (for loading immediate integers) --- lcode.c | 16 ++++++++++++---- lcode.h | 5 ++--- lopcodes.c | 4 +++- lopcodes.h | 3 ++- lparser.c | 4 ++-- lvm.c | 7 ++++++- 6 files changed, 27 insertions(+), 12 deletions(-) diff --git a/lcode.c b/lcode.c index aca0256d44..f48221cf53 100644 --- a/lcode.c +++ b/lcode.c @@ -1,5 +1,5 @@ /* -** $Id: lcode.c,v 2.111 2016/07/19 17:12:07 roberto Exp roberto $ +** $Id: lcode.c,v 2.112 2016/12/22 13:08:50 roberto Exp roberto $ ** Code generator for Lua ** See Copyright Notice in lua.h */ @@ -343,7 +343,7 @@ static int codeextraarg (FuncState *fs, int a) { ** (if constant index 'k' fits in 18 bits) or an 'OP_LOADKX' ** instruction with "extra argument". */ -int luaK_codek (FuncState *fs, int reg, int k) { +static int luaK_codek (FuncState *fs, int reg, int k) { if (k <= MAXARG_Bx) return luaK_codeABx(fs, OP_LOADK, reg, k); else { @@ -468,7 +468,7 @@ int luaK_stringK (FuncState *fs, TString *s) { ** same value; conversion to 'void*' is used only for hashing, so there ** are no "precision" problems. */ -int luaK_intK (FuncState *fs, lua_Integer n) { +static int luaK_intK (FuncState *fs, lua_Integer n) { TValue k, o; setpvalue(&k, cast(void*, cast(size_t, n))); setivalue(&o, n); @@ -507,6 +507,14 @@ static int nilK (FuncState *fs) { } +void luaK_int (FuncState *fs, int reg, lua_Integer i) { + if (-MAXARG_sBx <= i && i <= MAXARG_sBx) + luaK_codeAsBx(fs, OP_LOADI, reg, cast_int(i)); + else + luaK_codek(fs, reg, luaK_intK(fs, i)); +} + + /* ** Fix an expression to return the number of results 'nresults'. ** Either 'e' is a multi-ret expression (function call or vararg) @@ -612,7 +620,7 @@ static void discharge2reg (FuncState *fs, expdesc *e, int reg) { break; } case VKINT: { - luaK_codek(fs, reg, luaK_intK(fs, e->u.ival)); + luaK_int(fs, reg, e->u.ival); break; } case VRELOCABLE: { diff --git a/lcode.h b/lcode.h index d2b5100e18..e7ff352272 100644 --- a/lcode.h +++ b/lcode.h @@ -1,5 +1,5 @@ /* -** $Id: lcode.h,v 1.63 2013/12/30 20:47:58 roberto Exp roberto $ +** $Id: lcode.h,v 1.64 2016/01/05 16:22:37 roberto Exp roberto $ ** Code generator for Lua ** See Copyright Notice in lua.h */ @@ -51,13 +51,12 @@ typedef enum UnOpr { OPR_MINUS, OPR_BNOT, OPR_NOT, OPR_LEN, OPR_NOUNOPR } UnOpr; LUAI_FUNC int luaK_codeABx (FuncState *fs, OpCode o, int A, unsigned int Bx); LUAI_FUNC int luaK_codeABC (FuncState *fs, OpCode o, int A, int B, int C); -LUAI_FUNC int luaK_codek (FuncState *fs, int reg, int k); LUAI_FUNC void luaK_fixline (FuncState *fs, int line); LUAI_FUNC void luaK_nil (FuncState *fs, int from, int n); LUAI_FUNC void luaK_reserveregs (FuncState *fs, int n); LUAI_FUNC void luaK_checkstack (FuncState *fs, int n); LUAI_FUNC int luaK_stringK (FuncState *fs, TString *s); -LUAI_FUNC int luaK_intK (FuncState *fs, lua_Integer n); +LUAI_FUNC void luaK_int (FuncState *fs, int reg, lua_Integer n); LUAI_FUNC void luaK_dischargevars (FuncState *fs, expdesc *e); LUAI_FUNC int luaK_exp2anyreg (FuncState *fs, expdesc *e); LUAI_FUNC void luaK_exp2anyregup (FuncState *fs, expdesc *e); diff --git a/lopcodes.c b/lopcodes.c index 10d4bbce17..18199764af 100644 --- a/lopcodes.c +++ b/lopcodes.c @@ -1,5 +1,5 @@ /* -** $Id: lopcodes.c,v 1.54 2014/11/02 19:19:04 roberto Exp roberto $ +** $Id: lopcodes.c,v 1.55 2015/01/05 13:48:33 roberto Exp roberto $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ @@ -20,6 +20,7 @@ LUAI_DDEF const char *const luaP_opnames[NUM_OPCODES+1] = { "MOVE", "LOADK", + "LOADI", "LOADKX", "LOADBOOL", "LOADNIL", @@ -75,6 +76,7 @@ LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = { /* T A B C mode opcode */ opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_MOVE */ ,opmode(0, 1, OpArgK, OpArgN, iABx) /* OP_LOADK */ + ,opmode(0, 1, OpArgU, OpArgN, iAsBx) /* OP_LOADI */ ,opmode(0, 1, OpArgN, OpArgN, iABx) /* OP_LOADKX */ ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_LOADBOOL */ ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_LOADNIL */ diff --git a/lopcodes.h b/lopcodes.h index dd2a7571d9..afb9205abf 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -1,5 +1,5 @@ /* -** $Id: lopcodes.h,v 1.148 2014/10/25 11:50:46 roberto Exp roberto $ +** $Id: lopcodes.h,v 1.149 2016/07/19 17:12:21 roberto Exp roberto $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ @@ -170,6 +170,7 @@ name args description ------------------------------------------------------------------------*/ OP_MOVE,/* A B R(A) := R(B) */ OP_LOADK,/* A Bx R(A) := Kst(Bx) */ +OP_LOADI,/* A sBx R(A) := sBx */ OP_LOADKX,/* A R(A) := Kst(extra arg) */ OP_LOADBOOL,/* A B C R(A) := (Bool)B; if (C) pc++ */ OP_LOADNIL,/* A B R(A), R(A+1), ..., R(A+B) := nil */ diff --git a/lparser.c b/lparser.c index 5894d8ffc7..25134f11e6 100644 --- a/lparser.c +++ b/lparser.c @@ -1,5 +1,5 @@ /* -** $Id: lparser.c,v 2.154 2016/06/22 15:48:25 roberto Exp roberto $ +** $Id: lparser.c,v 2.155 2016/08/01 19:51:24 roberto Exp roberto $ ** Lua Parser ** See Copyright Notice in lua.h */ @@ -1329,7 +1329,7 @@ static void fornum (LexState *ls, TString *varname, int line) { if (testnext(ls, ',')) exp1(ls); /* optional step */ else { /* default step = 1 */ - luaK_codek(fs, fs->freereg, luaK_intK(fs, 1)); + luaK_int(fs, fs->freereg, 1); luaK_reserveregs(fs, 1); } forbody(ls, base, line, 1, 1); diff --git a/lvm.c b/lvm.c index 3295866554..a15ebe561c 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.269 2017/04/06 13:08:56 roberto Exp roberto $ +** $Id: lvm.c,v 2.270 2017/04/11 18:41:09 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -808,6 +808,11 @@ void luaV_execute (lua_State *L) { setobj2s(L, ra, rb); vmbreak; } + vmcase(OP_LOADI) { + lua_Integer b = GETARG_sBx(i); + setivalue(ra, b); + vmbreak; + } vmcase(OP_LOADKX) { TValue *rb; lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_EXTRAARG); From 69371c4b84becac09c445aae01d005b49658ef82 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 24 Apr 2017 13:59:26 -0300 Subject: [PATCH 0020/1145] 'KGC_NORMAL' -> 'KGC_INC' + emergency GC signalled by flag (instead of mode) --- lapi.c | 4 ++-- lgc.c | 23 +++++++++++------------ lstate.c | 5 +++-- lstate.h | 6 +++--- ltests.c | 4 ++-- 5 files changed, 21 insertions(+), 21 deletions(-) diff --git a/lapi.c b/lapi.c index 29f3d2650d..2be5263167 100644 --- a/lapi.c +++ b/lapi.c @@ -1,5 +1,5 @@ /* -** $Id: lapi.c,v 2.263 2017/04/19 17:02:50 roberto Exp roberto $ +** $Id: lapi.c,v 2.264 2017/04/20 18:22:44 roberto Exp roberto $ ** Lua API ** See Copyright Notice in lua.h */ @@ -1111,7 +1111,7 @@ LUA_API int lua_gc (lua_State *L, int what, int data) { break; } case LUA_GCINC: { - luaC_changemode(L, KGC_NORMAL); + luaC_changemode(L, KGC_INC); break; } default: res = -1; /* invalid option */ diff --git a/lgc.c b/lgc.c index 59b6dde160..d27ea42bcf 100644 --- a/lgc.c +++ b/lgc.c @@ -1,5 +1,5 @@ /* -** $Id: lgc.c,v 2.223 2017/04/19 17:02:50 roberto Exp roberto $ +** $Id: lgc.c,v 2.224 2017/04/20 18:24:33 roberto Exp roberto $ ** Garbage Collector ** See Copyright Notice in lua.h */ @@ -543,7 +543,7 @@ static lu_mem traversethread (global_State *g, lua_State *th) { g->twups = th; } } - else if (g->gckind != KGC_EMERGENCY) + else if (!g->gcemergency) luaD_shrinkstack(th); /* do not change stack in emergency cycle */ return (sizeof(lua_State) + sizeof(TValue) * th->stacksize + sizeof(CallInfo) * th->nci); @@ -767,7 +767,7 @@ static GCObject **sweeptolive (lua_State *L, GCObject **p) { ** If possible, shrink string table */ static void checkSizes (lua_State *L, global_State *g) { - if (g->gckind != KGC_EMERGENCY) { + if (!g->gcemergency) { l_mem olddebt = g->GCdebt; if (g->strt.nuse < g->strt.size / 4) /* string table too big? */ luaS_resize(L, g->strt.size / 2); /* shrink it a little */ @@ -1174,7 +1174,7 @@ static void enterinc (global_State *g) { g->finobjrold = g->finobjold = g->finobjsur = NULL; lua_assert(g->tobefnz == NULL); /* no need to sweep */ g->gcstate = GCSpause; - g->gckind = KGC_NORMAL; + g->gckind = KGC_INC; } @@ -1278,7 +1278,7 @@ static void deletealllist (lua_State *L, GCObject *p, GCObject *limit) { void luaC_freeallobjects (lua_State *L) { global_State *g = G(L); - luaC_changemode(L, KGC_NORMAL); + luaC_changemode(L, KGC_INC); separatetobefnz(g, 1); /* separate all objects with finalizers */ lua_assert(g->finobj == NULL); callallpendingfinalizers(L); @@ -1394,7 +1394,7 @@ static lu_mem singlestep (lua_State *L) { return 0; } case GCScallfin: { /* call remaining finalizers */ - if (g->tobefnz && g->gckind != KGC_EMERGENCY) { + if (g->tobefnz && !g->gcemergency) { int n = runafewfinalizers(L); return (n * GCFINALIZECOST); } @@ -1459,7 +1459,7 @@ void luaC_step (lua_State *L) { global_State *g = G(L); if (!g->gcrunning) /* not running? */ luaE_setdebt(g, -GCSTEPSIZE * 10); /* avoid being called too often */ - else if (g->gckind == KGC_NORMAL) + else if (g->gckind == KGC_INC) incstep(L, g); else genstep(L, g); @@ -1490,14 +1490,13 @@ static void fullinc (lua_State *L, global_State *g) { void luaC_fullgc (lua_State *L, int isemergency) { global_State *g = G(L); - int gckind = g->gckind; - if (isemergency) - g->gckind = KGC_EMERGENCY; /* set flag */ - if (gckind == KGC_NORMAL) + lua_assert(!g->gcemergency); + g->gcemergency = isemergency; /* set flag */ + if (g->gckind == KGC_INC) fullinc(L, g); else fullgen(L, g); - g->gckind = gckind; + g->gcemergency = 0; } /* }====================================================== */ diff --git a/lstate.c b/lstate.c index adc1bce902..653c90f2d0 100644 --- a/lstate.c +++ b/lstate.c @@ -1,5 +1,5 @@ /* -** $Id: lstate.c,v 2.136 2017/04/11 19:00:27 roberto Exp roberto $ +** $Id: lstate.c,v 2.137 2017/04/12 18:56:25 roberto Exp roberto $ ** Global State ** See Copyright Notice in lua.h */ @@ -209,6 +209,7 @@ static void f_luaopen (lua_State *L, void *ud) { luaT_init(L); luaX_init(L); g->gcrunning = 1; /* allow gc */ + g->gcemergency = 0; g->version = lua_version(NULL); luai_userstateopen(L); } @@ -317,7 +318,7 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { g->panic = NULL; g->version = NULL; g->gcstate = GCSpause; - g->gckind = KGC_NORMAL; + g->gckind = KGC_INC; g->finobj = g->tobefnz = g->fixedgc = NULL; g->survival = g->old = g->reallyold = NULL; g->finobjsur = g->finobjold = g->finobjrold = NULL; diff --git a/lstate.h b/lstate.h index d844fc8ce8..d2f23839e8 100644 --- a/lstate.h +++ b/lstate.h @@ -1,5 +1,5 @@ /* -** $Id: lstate.h,v 2.137 2017/04/11 18:41:09 roberto Exp roberto $ +** $Id: lstate.h,v 2.138 2017/04/19 17:02:50 roberto Exp roberto $ ** Global State ** See Copyright Notice in lua.h */ @@ -69,9 +69,8 @@ struct lua_longjmp; /* defined in ldo.c */ /* kinds of Garbage Collection */ -#define KGC_NORMAL 0 +#define KGC_INC 0 /* incremental gc */ #define KGC_GEN 1 /* generational gc */ -#define KGC_EMERGENCY 2 /* gc was forced by an allocation failure */ typedef struct stringtable { @@ -151,6 +150,7 @@ typedef struct global_State { lu_byte genminormul; /* control for minor generational collections */ lu_byte genmajormul; /* control for major generational collections */ lu_byte gcrunning; /* true if GC is running */ + lu_byte gcemergency; /* true if this is an emergency collection */ GCObject *allgc; /* list of all collectable objects */ GCObject **sweepgc; /* current position of sweep in list */ GCObject *finobj; /* list of collectable objects with finalizers */ diff --git a/ltests.c b/ltests.c index 9679bee297..77f0163daf 100644 --- a/ltests.c +++ b/ltests.c @@ -1,5 +1,5 @@ /* -** $Id: ltests.c,v 2.213 2017/04/18 19:42:12 roberto Exp roberto $ +** $Id: ltests.c,v 2.214 2017/04/19 18:46:47 roberto Exp roberto $ ** Internal Module for Debugging of the Lua Implementation ** See Copyright Notice in lua.h */ @@ -199,7 +199,7 @@ static int testobjref1 (global_State *g, GCObject *f, GCObject *t) { if (isdead(g,t)) return 0; if (issweepphase(g)) return 1; /* no invariants */ - else if (g->gckind == KGC_NORMAL) + else if (g->gckind == KGC_INC) return !(isblack(f) && iswhite(t)); /* basic incremental invariant */ else { /* generational mode */ if ((getage(f) == G_OLD && isblack(f)) && !isold(t)) From f399e6705fab15013ae468049c910577e1a9a5a1 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 24 Apr 2017 14:52:18 -0300 Subject: [PATCH 0021/1145] ensures that "collectgarbage'step'" in generational mode does a minor collection --- lgc.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/lgc.c b/lgc.c index d27ea42bcf..692326f31b 100644 --- a/lgc.c +++ b/lgc.c @@ -1,5 +1,5 @@ /* -** $Id: lgc.c,v 2.224 2017/04/20 18:24:33 roberto Exp roberto $ +** $Id: lgc.c,v 2.225 2017/04/24 16:59:26 roberto Exp $ ** Garbage Collector ** See Copyright Notice in lua.h */ @@ -1202,13 +1202,18 @@ static void fullgen (lua_State *L, global_State *g) { /* -** Does a generational "step". For now that means a young -** collection. (We still has to implement the full control.) +** Does a generational "step". If memory grows 'genmajormul'% larger +** than last major collection (kept in 'g->GCestimate'), does a major +** collection. Otherwise, does a minor collection and set debt to make +** another collection when memory grows 'genminormul'% larger. +** 'GCdebt <= 0' means an explicity call to GC step with "size" zero; +** in that case, always do a minor collection. */ static void genstep (lua_State *L, global_State *g) { lu_mem majorbase = g->GCestimate; //lua_checkmemory(L); - if (gettotalbytes(g) > (majorbase / 100) * (100 + g->genmajormul)) { + if (g->GCdebt > 0 && + gettotalbytes(g) > (majorbase / 100) * (100 + g->genmajormul)) { fullgen(L, g); } else { From 2caecf1b3efdbee4e08888a04143421589d6143b Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 24 Apr 2017 15:06:12 -0300 Subject: [PATCH 0022/1145] type 'L_Umaxalign' replaced by macro 'LUAI_MAXALIGN', which is also added to the auxlib buffer --- lauxlib.c | 6 +++--- lauxlib.h | 7 +++++-- llimits.h | 17 +---------------- lobject.h | 6 +++--- ltests.c | 4 ++-- luaconf.h | 8 +++++++- 6 files changed, 21 insertions(+), 27 deletions(-) diff --git a/lauxlib.c b/lauxlib.c index 7b14ca4d2b..2d16c9ce35 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -1,5 +1,5 @@ /* -** $Id: lauxlib.c,v 1.288 2016/12/04 20:17:24 roberto Exp roberto $ +** $Id: lauxlib.c,v 1.289 2016/12/20 18:37:00 roberto Exp roberto $ ** Auxiliary functions for building Lua libraries ** See Copyright Notice in lua.h */ @@ -496,7 +496,7 @@ static void *newbox (lua_State *L, size_t newsize) { ** check whether buffer is using a userdata on the stack as a temporary ** buffer */ -#define buffonstack(B) ((B)->b != (B)->initb) +#define buffonstack(B) ((B)->b != (B)->init.b) /* @@ -568,7 +568,7 @@ LUALIB_API void luaL_addvalue (luaL_Buffer *B) { LUALIB_API void luaL_buffinit (lua_State *L, luaL_Buffer *B) { B->L = L; - B->b = B->initb; + B->b = B->init.b; B->n = 0; B->size = LUAL_BUFFERSIZE; } diff --git a/lauxlib.h b/lauxlib.h index 1d65c975ad..8de199167d 100644 --- a/lauxlib.h +++ b/lauxlib.h @@ -1,5 +1,5 @@ /* -** $Id: lauxlib.h,v 1.130 2016/12/04 20:17:24 roberto Exp roberto $ +** $Id: lauxlib.h,v 1.131 2016/12/06 14:54:31 roberto Exp roberto $ ** Auxiliary functions for building Lua libraries ** See Copyright Notice in lua.h */ @@ -150,7 +150,10 @@ typedef struct luaL_Buffer { size_t size; /* buffer size */ size_t n; /* number of characters in buffer */ lua_State *L; - char initb[LUAL_BUFFERSIZE]; /* initial buffer */ + union { + LUAI_MAXALIGN; /* ensure maximum alignment for buffer */ + char b[LUAL_BUFFERSIZE]; /* initial buffer */ + } init; } luaL_Buffer; diff --git a/llimits.h b/llimits.h index fa14dfc739..14940550c8 100644 --- a/llimits.h +++ b/llimits.h @@ -1,5 +1,5 @@ /* -** $Id: llimits.h,v 1.140 2015/10/21 18:40:47 roberto Exp roberto $ +** $Id: llimits.h,v 1.141 2015/11/19 19:16:22 roberto Exp roberto $ ** Limits, basic types, and some other 'installation-dependent' definitions ** See Copyright Notice in lua.h */ @@ -60,21 +60,6 @@ typedef unsigned char lu_byte; -/* type to ensure maximum alignment */ -#if defined(LUAI_USER_ALIGNMENT_T) -typedef LUAI_USER_ALIGNMENT_T L_Umaxalign; -#else -typedef union { - lua_Number n; - double u; - void *s; - lua_Integer i; - long l; -} L_Umaxalign; -#endif - - - /* types of 'usual argument conversions' for lua_Number and lua_Integer */ typedef LUAI_UACNUMBER l_uacNumber; typedef LUAI_UACINT l_uacInt; diff --git a/lobject.h b/lobject.h index 7c521ba250..c8f9adb3b3 100644 --- a/lobject.h +++ b/lobject.h @@ -1,5 +1,5 @@ /* -** $Id: lobject.h,v 2.117 2016/08/01 19:51:24 roberto Exp roberto $ +** $Id: lobject.h,v 2.118 2017/04/11 18:41:09 roberto Exp roberto $ ** Type definitions for Lua objects ** See Copyright Notice in lua.h */ @@ -317,7 +317,7 @@ typedef struct TString { ** Ensures that address after this type is always fully aligned. */ typedef union UTString { - L_Umaxalign dummy; /* ensures maximum alignment for strings */ + LUAI_MAXALIGN; /* ensures maximum alignment for strings */ TString tsv; } UTString; @@ -357,7 +357,7 @@ typedef struct Udata { ** Ensures that address after this type is always fully aligned. */ typedef union UUdata { - L_Umaxalign dummy; /* ensures maximum alignment for 'local' udata */ + LUAI_MAXALIGN; /* ensures maximum alignment for 'local' udata */ Udata uv; } UUdata; diff --git a/ltests.c b/ltests.c index 77f0163daf..4ac2c702be 100644 --- a/ltests.c +++ b/ltests.c @@ -1,5 +1,5 @@ /* -** $Id: ltests.c,v 2.214 2017/04/19 18:46:47 roberto Exp roberto $ +** $Id: ltests.c,v 2.215 2017/04/24 16:59:26 roberto Exp roberto $ ** Internal Module for Debugging of the Lua Implementation ** See Copyright Notice in lua.h */ @@ -81,7 +81,7 @@ static int tpanic (lua_State *L) { #define MARK 0x55 /* 01010101 (a nice pattern) */ typedef union Header { - L_Umaxalign a; /* ensures maximum alignment for Header */ + LUAI_MAXALIGN; struct { size_t size; int type; diff --git a/luaconf.h b/luaconf.h index 33ff360458..f7ac7160a2 100644 --- a/luaconf.h +++ b/luaconf.h @@ -1,5 +1,5 @@ /* -** $Id: luaconf.h,v 1.259 2016/12/22 13:08:50 roberto Exp roberto $ +** $Id: luaconf.h,v 1.260 2017/04/19 16:34:35 roberto Exp roberto $ ** Configuration file for Lua ** See Copyright Notice in lua.h */ @@ -761,6 +761,12 @@ #define LUAL_BUFFERSIZE ((int)(0x80 * sizeof(void*) * sizeof(lua_Integer))) #endif +/* +@@ LUAI_MAXALIGN defines fields that, when used in a union, ensure +** "maximum" alignment for the other items in that union. +*/ +#define LUAI_MAXALIGN lua_Number n; double u; void *s; lua_Integer i; long l + /* }================================================================== */ From cb3d5dce30089512085f78a0bef79e30ef732e30 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 24 Apr 2017 17:26:39 -0300 Subject: [PATCH 0023/1145] opcodes 'OP_GETTABUP'/'OP_SETTABUP' operate only with string keys, so they can use fast-track table access --- lcode.c | 14 ++++++++++++-- lopcodes.h | 4 +++- lvm.c | 22 +++++++++++++++------- 3 files changed, 30 insertions(+), 10 deletions(-) diff --git a/lcode.c b/lcode.c index f48221cf53..a8f196b60a 100644 --- a/lcode.c +++ b/lcode.c @@ -1,5 +1,5 @@ /* -** $Id: lcode.c,v 2.112 2016/12/22 13:08:50 roberto Exp roberto $ +** $Id: lcode.c,v 2.113 2017/04/20 19:53:55 roberto Exp roberto $ ** Code generator for Lua ** See Copyright Notice in lua.h */ @@ -947,12 +947,22 @@ static void codenot (FuncState *fs, expdesc *e) { } +/* +** Check whether expression 'e' is a literal string +*/ +static int isKstr (FuncState *fs, expdesc *e) { + return (e->k == VK && ttisstring(&fs->f->k[e->u.info])); +} + + /* ** Create expression 't[k]'. 't' must have its final result already in a -** register or upvalue. +** register or upvalue. Upvalues can only be indexed by literal strings. */ void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) { lua_assert(!hasjumps(t) && (vkisinreg(t->k) || t->k == VUPVAL)); + if (t->k == VUPVAL && !isKstr(fs, k)) /* upvalue indexed by non string? */ + luaK_exp2anyreg(fs, t); /* put it in a register */ t->u.ind.t = t->u.info; /* register or upvalue index */ t->u.ind.idx = luaK_exp2RK(fs, k); /* R/K index for key */ t->u.ind.vt = (t->k == VUPVAL) ? VUPVAL : VLOCAL; diff --git a/lopcodes.h b/lopcodes.h index afb9205abf..ed3ea66db2 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -1,5 +1,5 @@ /* -** $Id: lopcodes.h,v 1.149 2016/07/19 17:12:21 roberto Exp roberto $ +** $Id: lopcodes.h,v 1.150 2017/04/20 19:53:55 roberto Exp roberto $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ @@ -255,6 +255,8 @@ OP_EXTRAARG/* Ax extra (larger) argument for previous opcode */ (*) In OP_LOADKX, the next 'instruction' is always EXTRAARG. + (*) In OP_GETTABUP, OP_SETTABUP, and OP_SELF, the index must be a string. + (*) For comparisons, A specifies what condition the test should accept (true or false). diff --git a/lvm.c b/lvm.c index a15ebe561c..e1268567c4 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.270 2017/04/11 18:41:09 roberto Exp roberto $ +** $Id: lvm.c,v 2.271 2017/04/20 19:53:55 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -838,9 +838,14 @@ void luaV_execute (lua_State *L) { vmbreak; } vmcase(OP_GETTABUP) { + const TValue *slot; TValue *upval = cl->upvals[GETARG_B(i)]->v; TValue *rc = RKC(i); - gettableProtected(L, upval, rc, ra); + TString *key = tsvalue(rc); /* key must be a string */ + if (luaV_fastget(L, upval, key, slot, luaH_getstr)) { + setobj2s(L, ra, slot); + } + else Protect(luaV_finishget(L, upval, rc, ra, slot)); vmbreak; } vmcase(OP_GETTABLE) { @@ -850,10 +855,13 @@ void luaV_execute (lua_State *L) { vmbreak; } vmcase(OP_SETTABUP) { + const TValue *slot; TValue *upval = cl->upvals[GETARG_A(i)]->v; TValue *rb = RKB(i); TValue *rc = RKC(i); - settableProtected(L, upval, rb, rc); + TString *key = tsvalue(rb); /* key must be a string */ + if (!luaV_fastset(L, upval, key, slot, luaH_getstr, rc)) + Protect(luaV_finishset(L, upval, rb, rc, slot)); vmbreak; } vmcase(OP_SETUPVAL) { @@ -879,15 +887,15 @@ void luaV_execute (lua_State *L) { vmbreak; } vmcase(OP_SELF) { - const TValue *aux; + const TValue *slot; StkId rb = RB(i); TValue *rc = RKC(i); TString *key = tsvalue(rc); /* key must be a string */ setobjs2s(L, ra + 1, rb); - if (luaV_fastget(L, rb, key, aux, luaH_getstr)) { - setobj2s(L, ra, aux); + if (luaV_fastget(L, rb, key, slot, luaH_getstr)) { + setobj2s(L, ra, slot); } - else Protect(luaV_finishget(L, rb, rc, ra, aux)); + else Protect(luaV_finishget(L, rb, rc, ra, slot)); vmbreak; } vmcase(OP_ADD) { From 6dbae1b5d961a76d1e2ffd5d50d20b86290f483c Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 25 Apr 2017 15:28:25 -0300 Subject: [PATCH 0024/1145] registers in a VINDEXED expression must be freed in order --- lcode.c | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/lcode.c b/lcode.c index a8f196b60a..4271a5a74d 100644 --- a/lcode.c +++ b/lcode.c @@ -1,5 +1,5 @@ /* -** $Id: lcode.c,v 2.113 2017/04/20 19:53:55 roberto Exp roberto $ +** $Id: lcode.c,v 2.114 2017/04/24 20:26:39 roberto Exp roberto $ ** Code generator for Lua ** See Copyright Notice in lua.h */ @@ -391,6 +391,21 @@ static void freereg (FuncState *fs, int reg) { } +/* +** Free two registers in proper order +*/ +static void freeregs (FuncState *fs, int r1, int r2) { + if (r1 > r2) { + freereg(fs, r1); + freereg(fs, r2); + } + else { + freereg(fs, r2); + freereg(fs, r1); + } +} + + /* ** Free register used by expression 'e' (if any) */ @@ -407,14 +422,7 @@ static void freeexp (FuncState *fs, expdesc *e) { static void freeexps (FuncState *fs, expdesc *e1, expdesc *e2) { int r1 = (e1->k == VNONRELOC) ? e1->u.info : -1; int r2 = (e2->k == VNONRELOC) ? e2->u.info : -1; - if (r1 > r2) { - freereg(fs, r1); - freereg(fs, r2); - } - else { - freereg(fs, r2); - freereg(fs, r1); - } + freeregs(fs, r1, r2); } @@ -574,13 +582,13 @@ void luaK_dischargevars (FuncState *fs, expdesc *e) { } case VINDEXED: { OpCode op; - freereg(fs, e->u.ind.idx); if (e->u.ind.vt == VLOCAL) { /* is 't' in a register? */ - freereg(fs, e->u.ind.t); + freeregs(fs, e->u.ind.t, e->u.ind.idx); op = OP_GETTABLE; } else { lua_assert(e->u.ind.vt == VUPVAL); + freereg(fs, e->u.ind.idx); op = OP_GETTABUP; /* 't' is in an upvalue */ } e->u.info = luaK_codeABC(fs, op, 0, e->u.ind.t, e->u.ind.idx); From a3f9c1a77abb2add30334160055dc2ed11eb0b2e Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 25 Apr 2017 17:01:14 -0300 Subject: [PATCH 0025/1145] detail (using unsigned comparison in range check for LOADI) --- lcode.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lcode.c b/lcode.c index 4271a5a74d..12dc2505e5 100644 --- a/lcode.c +++ b/lcode.c @@ -1,5 +1,5 @@ /* -** $Id: lcode.c,v 2.114 2017/04/24 20:26:39 roberto Exp roberto $ +** $Id: lcode.c,v 2.115 2017/04/25 18:28:25 roberto Exp roberto $ ** Code generator for Lua ** See Copyright Notice in lua.h */ @@ -516,7 +516,7 @@ static int nilK (FuncState *fs) { void luaK_int (FuncState *fs, int reg, lua_Integer i) { - if (-MAXARG_sBx <= i && i <= MAXARG_sBx) + if (l_castS2U(i) + MAXARG_sBx <= l_castS2U(MAXARG_Bx)) luaK_codeAsBx(fs, OP_LOADI, reg, cast_int(i)); else luaK_codek(fs, reg, luaK_intK(fs, i)); From 173e41b2ebed59a716d299470de25e50aee3b921 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 26 Apr 2017 14:46:52 -0300 Subject: [PATCH 0026/1145] new opcode OP_ADDI (for immediate integer operand) (Experimental) --- lcode.c | 32 ++++++++++++++++++++++++++++---- ldebug.c | 5 ++++- lopcodes.c | 4 +++- lopcodes.h | 8 ++++++-- lvm.c | 27 ++++++++++++++++++++++++--- 5 files changed, 65 insertions(+), 11 deletions(-) diff --git a/lcode.c b/lcode.c index 12dc2505e5..ccf7b1aec9 100644 --- a/lcode.c +++ b/lcode.c @@ -1,5 +1,5 @@ /* -** $Id: lcode.c,v 2.115 2017/04/25 18:28:25 roberto Exp roberto $ +** $Id: lcode.c,v 2.116 2017/04/25 20:01:14 roberto Exp roberto $ ** Code generator for Lua ** See Copyright Notice in lua.h */ @@ -963,6 +963,16 @@ static int isKstr (FuncState *fs, expdesc *e) { } +/* +** Check whether expression 'e' is a literal integer in +** proper range +*/ +static int isKint (expdesc *e) { + return (e->k == VKINT && !hasjumps(e) && + l_castS2U(e->u.ival) <= l_castS2U(MAXARG_C)); +} + + /* ** Create expression 't[k]'. 't' must have its final result already in a ** register or upvalue. Upvalues can only be indexed by literal strings. @@ -1047,10 +1057,24 @@ static void codeunexpval (FuncState *fs, OpCode op, expdesc *e, int line) { */ static void codebinexpval (FuncState *fs, OpCode op, expdesc *e1, expdesc *e2, int line) { - int rk2 = luaK_exp2RK(fs, e2); /* both operands are "RK" */ - int rk1 = luaK_exp2RK(fs, e1); + int v1, v2; + if (op == OP_ADD && (isKint(e1) || isKint(e2))) { + if (isKint(e2)) { + v2 = cast_int(e2->u.ival); + v1 = luaK_exp2anyreg(fs, e1); + } + else { /* exchange operands to make 2nd one a constant */ + v2 = cast_int(e1->u.ival); + v1 = luaK_exp2anyreg(fs, e2) | BITRK; /* K bit signal the exchange */ + } + op = OP_ADDI; + } + else { + v2 = luaK_exp2RK(fs, e2); /* both operands are "RK" */ + v1 = luaK_exp2RK(fs, e1); + } freeexps(fs, e1, e2); - e1->u.info = luaK_codeABC(fs, op, 0, rk1, rk2); /* generate opcode */ + e1->u.info = luaK_codeABC(fs, op, 0, v1, v2); /* generate opcode */ e1->k = VRELOCABLE; /* all those operations are relocatable */ luaK_fixline(fs, line); } diff --git a/ldebug.c b/ldebug.c index f1835890a5..4f35d211d5 100644 --- a/ldebug.c +++ b/ldebug.c @@ -1,5 +1,5 @@ /* -** $Id: ldebug.c,v 2.120 2016/03/31 19:01:21 roberto Exp roberto $ +** $Id: ldebug.c,v 2.121 2016/10/19 12:32:10 roberto Exp roberto $ ** Debug Interface ** See Copyright Notice in lua.h */ @@ -513,6 +513,9 @@ static const char *funcnamefromcode (lua_State *L, CallInfo *ci, case OP_SETTABUP: case OP_SETTABLE: tm = TM_NEWINDEX; break; + case OP_ADDI: + tm = TM_ADD; + break; case OP_ADD: case OP_SUB: case OP_MUL: case OP_MOD: case OP_POW: case OP_DIV: case OP_IDIV: case OP_BAND: case OP_BOR: case OP_BXOR: case OP_SHL: case OP_SHR: { diff --git a/lopcodes.c b/lopcodes.c index 18199764af..f66f209aa9 100644 --- a/lopcodes.c +++ b/lopcodes.c @@ -1,5 +1,5 @@ /* -** $Id: lopcodes.c,v 1.55 2015/01/05 13:48:33 roberto Exp roberto $ +** $Id: lopcodes.c,v 1.56 2017/04/20 19:53:55 roberto Exp roberto $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ @@ -32,6 +32,7 @@ LUAI_DDEF const char *const luaP_opnames[NUM_OPCODES+1] = { "SETTABLE", "NEWTABLE", "SELF", + "ADDI", "ADD", "SUB", "MUL", @@ -88,6 +89,7 @@ LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = { ,opmode(0, 0, OpArgK, OpArgK, iABC) /* OP_SETTABLE */ ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_NEWTABLE */ ,opmode(0, 1, OpArgR, OpArgK, iABC) /* OP_SELF */ + ,opmode(0, 1, OpArgR, OpArgU, iABC) /* OP_ADDI */ ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_ADD */ ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_SUB */ ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_MUL */ diff --git a/lopcodes.h b/lopcodes.h index ed3ea66db2..c359581d0b 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -1,5 +1,5 @@ /* -** $Id: lopcodes.h,v 1.150 2017/04/20 19:53:55 roberto Exp roberto $ +** $Id: lopcodes.h,v 1.151 2017/04/24 20:26:39 roberto Exp roberto $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ @@ -90,7 +90,7 @@ enum OpMode {iABC, iABx, iAsBx, iAx}; /* basic instruction format */ #define SET_OPCODE(i,o) ((i) = (((i)&MASK0(SIZE_OP,POS_OP)) | \ ((cast(Instruction, o)<>pos) & MASK1(size,0))) +#define getarg(i,pos,size) (cast(int, ((i)>>(pos)) & MASK1(size,0))) #define setarg(i,v,pos,size) ((i) = (((i)&MASK0(size,pos)) | \ ((cast(Instruction, v)< Date: Fri, 28 Apr 2017 17:57:45 -0300 Subject: [PATCH 0027/1145] new opcodes for table access with constant keys (strings and integers) --- lcode.c | 74 ++++++++++++++++++++++++++++++++----------- ldebug.c | 92 ++++++++++++++++++++++++++++++++++++------------------ lopcodes.c | 18 ++++++++--- lopcodes.h | 20 ++++++------ lparser.c | 40 ++++++++++++++---------- lparser.h | 24 +++++++++----- lvm.c | 76 ++++++++++++++++++++++++++++++++++++-------- 7 files changed, 245 insertions(+), 99 deletions(-) diff --git a/lcode.c b/lcode.c index ccf7b1aec9..a154c339f8 100644 --- a/lcode.c +++ b/lcode.c @@ -1,5 +1,5 @@ /* -** $Id: lcode.c,v 2.116 2017/04/25 20:01:14 roberto Exp roberto $ +** $Id: lcode.c,v 2.117 2017/04/26 17:46:52 roberto Exp roberto $ ** Code generator for Lua ** See Copyright Notice in lua.h */ @@ -580,18 +580,26 @@ void luaK_dischargevars (FuncState *fs, expdesc *e) { e->k = VRELOCABLE; break; } + case VINDEXUP: { + e->u.info = luaK_codeABC(fs, OP_GETTABUP, 0, e->u.ind.t, e->u.ind.idx); + e->k = VRELOCABLE; + break; + } + case VINDEXI: { + freereg(fs, e->u.ind.t); + e->u.info = luaK_codeABC(fs, OP_GETI, 0, e->u.ind.t, e->u.ind.idx); + e->k = VRELOCABLE; + break; + } + case VINDEXSTR: { + freereg(fs, e->u.ind.t); + e->u.info = luaK_codeABC(fs, OP_GETFIELD, 0, e->u.ind.t, e->u.ind.idx); + e->k = VRELOCABLE; + break; + } case VINDEXED: { - OpCode op; - if (e->u.ind.vt == VLOCAL) { /* is 't' in a register? */ - freeregs(fs, e->u.ind.t, e->u.ind.idx); - op = OP_GETTABLE; - } - else { - lua_assert(e->u.ind.vt == VUPVAL); - freereg(fs, e->u.ind.idx); - op = OP_GETTABUP; /* 't' is in an upvalue */ - } - e->u.info = luaK_codeABC(fs, op, 0, e->u.ind.t, e->u.ind.idx); + freeregs(fs, e->u.ind.t, e->u.ind.idx); + e->u.info = luaK_codeABC(fs, OP_GETTABLE, 0, e->u.ind.t, e->u.ind.idx); e->k = VRELOCABLE; break; } @@ -807,10 +815,24 @@ void luaK_storevar (FuncState *fs, expdesc *var, expdesc *ex) { luaK_codeABC(fs, OP_SETUPVAL, e, var->u.info, 0); break; } + case VINDEXUP: { + int e = luaK_exp2RK(fs, ex); + luaK_codeABC(fs, OP_SETTABUP, var->u.ind.t, var->u.ind.idx, e); + break; + } + case VINDEXI: { + int e = luaK_exp2RK(fs, ex); + luaK_codeABC(fs, OP_SETI, var->u.ind.t, var->u.ind.idx, e); + break; + } + case VINDEXSTR: { + int e = luaK_exp2RK(fs, ex); + luaK_codeABC(fs, OP_SETFIELD, var->u.ind.t, var->u.ind.idx, e); + break; + } case VINDEXED: { - OpCode op = (var->u.ind.vt == VLOCAL) ? OP_SETTABLE : OP_SETTABUP; int e = luaK_exp2RK(fs, ex); - luaK_codeABC(fs, op, var->u.ind.t, var->u.ind.idx, e); + luaK_codeABC(fs, OP_SETTABLE, var->u.ind.t, var->u.ind.idx, e); break; } default: lua_assert(0); /* invalid var kind to store */ @@ -959,7 +981,8 @@ static void codenot (FuncState *fs, expdesc *e) { ** Check whether expression 'e' is a literal string */ static int isKstr (FuncState *fs, expdesc *e) { - return (e->k == VK && ttisstring(&fs->f->k[e->u.info])); + return (e->k == VK && !hasjumps(e) && e->u.info <= MAXARG_C && + ttisstring(&fs->f->k[e->u.info])); } @@ -976,15 +999,30 @@ static int isKint (expdesc *e) { /* ** Create expression 't[k]'. 't' must have its final result already in a ** register or upvalue. Upvalues can only be indexed by literal strings. +** Keys can be literal strings in the constant table or arbitrary +** values in registers. */ void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) { lua_assert(!hasjumps(t) && (vkisinreg(t->k) || t->k == VUPVAL)); if (t->k == VUPVAL && !isKstr(fs, k)) /* upvalue indexed by non string? */ luaK_exp2anyreg(fs, t); /* put it in a register */ t->u.ind.t = t->u.info; /* register or upvalue index */ - t->u.ind.idx = luaK_exp2RK(fs, k); /* R/K index for key */ - t->u.ind.vt = (t->k == VUPVAL) ? VUPVAL : VLOCAL; - t->k = VINDEXED; + if (t->k == VUPVAL) { + t->u.ind.idx = k->u.info; /* literal string */ + t->k = VINDEXUP; + } + else if (isKstr(fs, k)) { + t->u.ind.idx = k->u.info; /* literal string */ + t->k = VINDEXSTR; + } + else if (isKint(k)) { + t->u.ind.idx = k->u.ival; /* integer constant */ + t->k = VINDEXI; + } + else { + t->u.ind.idx = luaK_exp2anyreg(fs, k); /* register */ + t->k = VINDEXED; + } } diff --git a/ldebug.c b/ldebug.c index 4f35d211d5..8a36e2f80a 100644 --- a/ldebug.c +++ b/ldebug.c @@ -1,5 +1,5 @@ /* -** $Id: ldebug.c,v 2.121 2016/10/19 12:32:10 roberto Exp roberto $ +** $Id: ldebug.c,v 2.122 2017/04/26 17:46:52 roberto Exp roberto $ ** Debug Interface ** See Copyright Notice in lua.h */ @@ -350,28 +350,36 @@ static const char *getobjname (Proto *p, int lastpc, int reg, /* -** find a "name" for the RK value 'c' +** Find a "name" for the constant 'c'. */ -static void kname (Proto *p, int pc, int c, const char **name) { - if (ISK(c)) { /* is 'c' a constant? */ - TValue *kvalue = &p->k[INDEXK(c)]; - if (ttisstring(kvalue)) { /* literal constant? */ - *name = svalue(kvalue); /* it is its own name */ - return; - } - /* else no reasonable name found */ - } - else { /* 'c' is a register */ - const char *what = getobjname(p, pc, c, name); /* search for 'c' */ - if (what && *what == 'c') { /* found a constant name? */ - return; /* 'name' already filled */ - } - /* else no reasonable name found */ - } - *name = "?"; /* no reasonable name found */ +static void kname (Proto *p, int c, const char **name) { + TValue *kvalue = &p->k[INDEXK(c)]; + *name = (ttisstring(kvalue)) ? svalue(kvalue) : "?"; +} + + +/* +** Find a "name" for the register 'c'. +*/ +static void rname (Proto *p, int pc, int c, const char **name) { + const char *what = getobjname(p, pc, c, name); /* search for 'c' */ + if (!(what && *what == 'c')) /* did not find a constant name? */ + *name = "?"; +} + + +/* +** Find a "name" for the R/K index 'c'. +*/ +static void rkname (Proto *p, int pc, int c, const char **name) { + if (ISK(c)) /* is 'c' a constant? */ + kname(p, INDEXK(c), name); + else /* 'c' is a register */ + rname(p, pc, c, name); } + static int filterpc (int pc, int jmptarget) { if (pc < jmptarget) /* is code conditional (inside a jump)? */ return -1; /* cannot know who sets that register */ @@ -428,8 +436,22 @@ static int findsetreg (Proto *p, int lastpc, int reg) { } -static const char *getobjname (Proto *p, int lastpc, int reg, - const char **name) { +/* +** Check whether table being indexed by instruction 'i' is the +** environment '_ENV' +*/ +static const char *gxf (Proto *p, int pc, Instruction i, int isup) { + int t = GETARG_B(i); /* table index */ + const char *name; /* name of indexed variable */ + if (isup) /* is an upvalue? */ + name = upvalname(p, t); + else + getobjname(p, pc, t, &name); + return (name && strcmp(name, LUA_ENV) == 0) ? "global" : "field"; +} + + + const char *getobjname (Proto *p, int lastpc, int reg, const char **name) { int pc; *name = luaF_getlocalname(p, reg + 1, lastpc); if (*name) /* is a local? */ @@ -446,15 +468,24 @@ static const char *getobjname (Proto *p, int lastpc, int reg, return getobjname(p, pc, b, name); /* get name for 'b' */ break; } - case OP_GETTABUP: + case OP_GETTABUP: { + int k = GETARG_C(i); /* key index */ + kname(p, k, name); + return gxf(p, pc, i, 1); + } case OP_GETTABLE: { int k = GETARG_C(i); /* key index */ - int t = GETARG_B(i); /* table index */ - const char *vn = (op == OP_GETTABLE) /* name of indexed variable */ - ? luaF_getlocalname(p, t + 1, pc) - : upvalname(p, t); - kname(p, pc, k, name); - return (vn && strcmp(vn, LUA_ENV) == 0) ? "global" : "field"; + rname(p, pc, k, name); + return gxf(p, pc, i, 0); + } + case OP_GETI: { + *name = "integer index"; + return "field"; + } + case OP_GETFIELD: { + int k = GETARG_C(i); /* key index */ + kname(p, k, name); + return gxf(p, pc, i, 0); } case OP_GETUPVAL: { *name = upvalname(p, GETARG_B(i)); @@ -472,7 +503,7 @@ static const char *getobjname (Proto *p, int lastpc, int reg, } case OP_SELF: { int k = GETARG_C(i); /* key index */ - kname(p, pc, k, name); + rkname(p, pc, k, name); return "method"; } default: break; /* go through to return NULL */ @@ -508,9 +539,10 @@ static const char *funcnamefromcode (lua_State *L, CallInfo *ci, } /* other instructions can do calls through metamethods */ case OP_SELF: case OP_GETTABUP: case OP_GETTABLE: + case OP_GETI: case OP_GETFIELD: tm = TM_INDEX; break; - case OP_SETTABUP: case OP_SETTABLE: + case OP_SETTABUP: case OP_SETTABLE: case OP_SETI: case OP_SETFIELD: tm = TM_NEWINDEX; break; case OP_ADDI: diff --git a/lopcodes.c b/lopcodes.c index f66f209aa9..1ffc52c920 100644 --- a/lopcodes.c +++ b/lopcodes.c @@ -1,5 +1,5 @@ /* -** $Id: lopcodes.c,v 1.56 2017/04/20 19:53:55 roberto Exp roberto $ +** $Id: lopcodes.c,v 1.57 2017/04/26 17:46:52 roberto Exp roberto $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ @@ -25,11 +25,15 @@ LUAI_DDEF const char *const luaP_opnames[NUM_OPCODES+1] = { "LOADBOOL", "LOADNIL", "GETUPVAL", + "SETUPVAL", "GETTABUP", "GETTABLE", + "GETI", + "GETFIELD", "SETTABUP", - "SETUPVAL", "SETTABLE", + "SETI", + "SETFIELD", "NEWTABLE", "SELF", "ADDI", @@ -82,11 +86,15 @@ LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = { ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_LOADBOOL */ ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_LOADNIL */ ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_GETUPVAL */ + ,opmode(0, 0, OpArgU, OpArgN, iABC) /* OP_SETUPVAL */ ,opmode(0, 1, OpArgU, OpArgK, iABC) /* OP_GETTABUP */ - ,opmode(0, 1, OpArgR, OpArgK, iABC) /* OP_GETTABLE */ + ,opmode(0, 1, OpArgR, OpArgR, iABC) /* OP_GETTABLE */ + ,opmode(0, 1, OpArgR, OpArgU, iABC) /* OP_GETI */ + ,opmode(0, 1, OpArgR, OpArgK, iABC) /* OP_GETFIELD */ ,opmode(0, 0, OpArgK, OpArgK, iABC) /* OP_SETTABUP */ - ,opmode(0, 0, OpArgU, OpArgN, iABC) /* OP_SETUPVAL */ - ,opmode(0, 0, OpArgK, OpArgK, iABC) /* OP_SETTABLE */ + ,opmode(0, 0, OpArgR, OpArgK, iABC) /* OP_SETTABLE */ + ,opmode(0, 0, OpArgU, OpArgK, iABC) /* OP_SETI */ + ,opmode(0, 0, OpArgK, OpArgK, iABC) /* OP_SETFIELD */ ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_NEWTABLE */ ,opmode(0, 1, OpArgR, OpArgK, iABC) /* OP_SELF */ ,opmode(0, 1, OpArgR, OpArgU, iABC) /* OP_ADDI */ diff --git a/lopcodes.h b/lopcodes.h index c359581d0b..6c9df6f32f 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -1,5 +1,5 @@ /* -** $Id: lopcodes.h,v 1.151 2017/04/24 20:26:39 roberto Exp roberto $ +** $Id: lopcodes.h,v 1.152 2017/04/26 17:46:52 roberto Exp roberto $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ @@ -178,17 +178,21 @@ OP_LOADKX,/* A R(A) := Kst(extra arg) */ OP_LOADBOOL,/* A B C R(A) := (Bool)B; if (C) pc++ */ OP_LOADNIL,/* A B R(A), R(A+1), ..., R(A+B) := nil */ OP_GETUPVAL,/* A B R(A) := UpValue[B] */ +OP_SETUPVAL,/* A B UpValue[B] := R(A) */ -OP_GETTABUP,/* A B C R(A) := UpValue[B][RK(C)] */ -OP_GETTABLE,/* A B C R(A) := R(B)[RK(C)] */ +OP_GETTABUP,/* A B C R(A) := UpValue[B][K(C):string] */ +OP_GETTABLE,/* A B C R(A) := R(B)[R(C)] */ +OP_GETI,/* A B C R(A) := R(B)[C] */ +OP_GETFIELD,/* A B C R(A) := R(B)[Kst(C):string] */ -OP_SETTABUP,/* A B C UpValue[A][RK(B)] := RK(C) */ -OP_SETUPVAL,/* A B UpValue[B] := R(A) */ -OP_SETTABLE,/* A B C R(A)[RK(B)] := RK(C) */ +OP_SETTABUP,/* A B C UpValue[A][K(B):string] := RK(C) */ +OP_SETTABLE,/* A B C R(A)[R(B)] := RK(C) */ +OP_SETI,/* A B C R(A)[B] := RK(C) */ +OP_SETFIELD,/* A B C R(A)[K(B):string] := RK(C) */ OP_NEWTABLE,/* A B C R(A) := {} (size = B,C) */ -OP_SELF,/* A B C R(A+1) := R(B); R(A) := R(B)[RK(C)] */ +OP_SELF,/* A B C R(A+1) := R(B); R(A) := R(B)[RK(C):string] */ OP_ADDI,/* A B C R(A) := R(B) + C */ OP_ADD,/* A B C R(A) := RK(B) + RK(C) */ @@ -259,8 +263,6 @@ OP_EXTRAARG/* Ax extra (larger) argument for previous opcode */ (*) In OP_LOADKX, the next 'instruction' is always EXTRAARG. - (*) In OP_GETTABUP, OP_SETTABUP, and OP_SELF, the index must be a string. - (*) For comparisons, A specifies what condition the test should accept (true or false). diff --git a/lparser.c b/lparser.c index 25134f11e6..f78f64bda8 100644 --- a/lparser.c +++ b/lparser.c @@ -1,5 +1,5 @@ /* -** $Id: lparser.c,v 2.155 2016/08/01 19:51:24 roberto Exp roberto $ +** $Id: lparser.c,v 2.156 2017/04/20 19:53:55 roberto Exp roberto $ ** Lua Parser ** See Copyright Notice in lua.h */ @@ -647,8 +647,7 @@ static void recfield (LexState *ls, struct ConsControl *cc) { /* recfield -> (NAME | '['exp1']') = exp1 */ FuncState *fs = ls->fs; int reg = ls->fs->freereg; - expdesc key, val; - int rkkey; + expdesc tab, key, val; if (ls->t.token == TK_NAME) { checklimit(fs, cc->nh, MAX_INT, "items in a constructor"); checkname(ls, &key); @@ -657,9 +656,10 @@ static void recfield (LexState *ls, struct ConsControl *cc) { yindex(ls, &key); cc->nh++; checknext(ls, '='); - rkkey = luaK_exp2RK(fs, &key); + tab = *cc->t; + luaK_indexed(fs, &tab, &key); expr(ls, &val); - luaK_codeABC(fs, OP_SETTABLE, cc->t->u.info, rkkey, luaK_exp2RK(fs, &val)); + luaK_storevar(fs, &tab, &val); fs->freereg = reg; /* free registers */ } @@ -1121,17 +1121,25 @@ static void check_conflict (LexState *ls, struct LHS_assign *lh, expdesc *v) { 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 */ + if (vkisindexed(lh->v.k)) { /* assignment to table field? */ + if (lh->v.k == VINDEXUP) { /* is table an upvalue? */ + if (v->k == VUPVAL && lh->v.u.ind.t == v->u.info) { + conflict = 1; /* table is the upvalue being assigned now */ + lh->v.k = VINDEXSTR; + lh->v.u.ind.t = extra; /* 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 */ + else { /* table is a register */ + if (v->k == VLOCAL && lh->v.u.ind.t == v->u.info) { + conflict = 1; /* table is the local being assigned now */ + lh->v.u.ind.t = extra; /* assignment will use safe copy */ + } + /* is index the local being assigned? */ + if (lh->v.k == VINDEXED && 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 */ + } } } } @@ -1151,7 +1159,7 @@ static void assignment (LexState *ls, struct LHS_assign *lh, int nvars) { struct LHS_assign nv; nv.prev = lh; suffixedexp(ls, &nv.v); - if (nv.v.k != VINDEXED) + if (!vkisindexed(nv.v.k)) check_conflict(ls, lh, &nv.v); checklimit(ls->fs, nvars + ls->L->nCcalls, LUAI_MAXCCALLS, "C levels"); diff --git a/lparser.h b/lparser.h index 13e613accc..1612418125 100644 --- a/lparser.h +++ b/lparser.h @@ -1,5 +1,5 @@ /* -** $Id: lparser.h,v 1.75 2015/12/17 15:44:50 roberto Exp roberto $ +** $Id: lparser.h,v 1.76 2015/12/30 18:16:13 roberto Exp roberto $ ** Lua Parser ** See Copyright Notice in lua.h */ @@ -36,9 +36,17 @@ typedef enum { VLOCAL, /* local variable; info = local register */ VUPVAL, /* upvalue variable; info = index of upvalue in 'upvalues' */ VINDEXED, /* indexed variable; - ind.vt = whether 't' is register or upvalue; - ind.t = table register or upvalue; - ind.idx = key's R/K index */ + ind.t = table register; + ind.idx = key's R index */ + VINDEXUP, /* indexed upvalue; + ind.t = table upvalue; + ind.idx = key's K index */ + VINDEXI, /* indexed variable with constant integer; + ind.t = table register; + ind.idx = key's value */ + VINDEXSTR, /* indexed variable with literal string; + ind.t = table register; + ind.idx = key's K index */ VJMP, /* expression is a test/comparison; info = pc of corresponding jump instruction */ VRELOCABLE, /* expression can put result in any register; @@ -48,7 +56,8 @@ typedef enum { } expkind; -#define vkisvar(k) (VLOCAL <= (k) && (k) <= VINDEXED) +#define vkisvar(k) (VLOCAL <= (k) && (k) <= VINDEXSTR) +#define vkisindexed(k) (VINDEXED <= (k) && (k) <= VINDEXSTR) #define vkisinreg(k) ((k) == VNONRELOC || (k) == VLOCAL) typedef struct expdesc { @@ -57,10 +66,9 @@ typedef struct expdesc { lua_Integer ival; /* for VKINT */ lua_Number nval; /* for VKFLT */ int info; /* for generic use */ - struct { /* for indexed variables (VINDEXED) */ - short idx; /* index (R/K) */ + struct { /* for indexed variables */ + short idx; /* index (R or "long" K) */ lu_byte t; /* table (register or upvalue) */ - lu_byte vt; /* whether 't' is register (VLOCAL) or upvalue (VUPVAL) */ } ind; } u; int t; /* patch list of 'exit when true' */ diff --git a/lvm.c b/lvm.c index 52a6ca35ff..9fa9daf593 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.272 2017/04/24 20:26:39 roberto Exp roberto $ +** $Id: lvm.c,v 2.273 2017/04/26 17:46:52 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -658,11 +658,13 @@ void luaV_finishOp (lua_State *L) { Instruction inst = *(ci->u.l.savedpc - 1); /* interrupted instruction */ OpCode op = GET_OPCODE(inst); switch (op) { /* finish its execution */ - case OP_ADD: case OP_SUB: case OP_MUL: case OP_DIV: case OP_IDIV: + case OP_ADDI: case OP_ADD: case OP_SUB: + case OP_MUL: case OP_DIV: case OP_IDIV: case OP_BAND: case OP_BOR: case OP_BXOR: case OP_SHL: case OP_SHR: case OP_MOD: case OP_POW: case OP_UNM: case OP_BNOT: case OP_LEN: - case OP_GETTABUP: case OP_GETTABLE: case OP_SELF: { + case OP_GETTABUP: case OP_GETTABLE: case OP_GETI: + case OP_GETFIELD: case OP_SELF: { setobjs2s(L, base + GETARG_A(inst), --L->top); break; } @@ -704,6 +706,7 @@ void luaV_finishOp (lua_State *L) { break; } case OP_TAILCALL: case OP_SETTABUP: case OP_SETTABLE: + case OP_SETI: case OP_SETFIELD: break; default: lua_assert(0); } @@ -726,7 +729,9 @@ void luaV_finishOp (lua_State *L) { #define RA(i) (base+GETARG_A(i)) #define RB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgR, base+GETARG_Br(i)) +#define KB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgK, k+GETARG_B(i)) #define RC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgR, base+GETARG_C(i)) +#define KC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgK, k+GETARG_C(i)) #define RKB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgK, \ (GETARG_Bk(i)) ? k+GETARG_Br(i) : base+GETARG_Br(i)) #define RKC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgK, \ @@ -837,10 +842,16 @@ void luaV_execute (lua_State *L) { setobj2s(L, ra, cl->upvals[b]->v); vmbreak; } + vmcase(OP_SETUPVAL) { + UpVal *uv = cl->upvals[GETARG_B(i)]; + setobj(L, uv->v, ra); + luaC_barrier(L, uv, ra); + vmbreak; + } vmcase(OP_GETTABUP) { const TValue *slot; TValue *upval = cl->upvals[GETARG_B(i)]->v; - TValue *rc = RKC(i); + TValue *rc = KC(i); TString *key = tsvalue(rc); /* key must be a string */ if (luaV_fastget(L, upval, key, slot, luaH_getstr)) { setobj2s(L, ra, slot); @@ -850,32 +861,71 @@ void luaV_execute (lua_State *L) { } vmcase(OP_GETTABLE) { StkId rb = RB(i); - TValue *rc = RKC(i); + TValue *rc = RC(i); gettableProtected(L, rb, rc, ra); vmbreak; } + vmcase(OP_GETI) { + const TValue *slot; + StkId rb = RB(i); + int c = GETARG_C(i); + if (luaV_fastget(L, rb, c, slot, luaH_getint)) { + setobj2s(L, ra, slot); + } + else { + TValue key; + setivalue(&key, c); + Protect(luaV_finishget(L, rb, &key, ra, slot)); + } + vmbreak; + } + vmcase(OP_GETFIELD) { + const TValue *slot; + StkId rb = RB(i); + TValue *rc = KC(i); + TString *key = tsvalue(rc); /* key must be a string */ + if (luaV_fastget(L, rb, key, slot, luaH_getstr)) { + setobj2s(L, ra, slot); + } + else Protect(luaV_finishget(L, rb, rc, ra, slot)); + vmbreak; + } vmcase(OP_SETTABUP) { const TValue *slot; TValue *upval = cl->upvals[GETARG_A(i)]->v; - TValue *rb = RKB(i); + TValue *rb = KB(i); TValue *rc = RKC(i); TString *key = tsvalue(rb); /* key must be a string */ if (!luaV_fastset(L, upval, key, slot, luaH_getstr, rc)) Protect(luaV_finishset(L, upval, rb, rc, slot)); vmbreak; } - vmcase(OP_SETUPVAL) { - UpVal *uv = cl->upvals[GETARG_B(i)]; - setobj(L, uv->v, ra); - luaC_barrier(L, uv, ra); - vmbreak; - } vmcase(OP_SETTABLE) { - TValue *rb = RKB(i); + TValue *rb = RB(i); TValue *rc = RKC(i); settableProtected(L, ra, rb, rc); vmbreak; } + vmcase(OP_SETI) { + const TValue *slot; + int c = GETARG_B(i); + TValue *rc = RKC(i); + if (!luaV_fastset(L, ra, c, slot, luaH_getint, rc)) { + TValue key; + setivalue(&key, c); + Protect(luaV_finishset(L, ra, &key, rc, slot)); + } + vmbreak; + } + vmcase(OP_SETFIELD) { + const TValue *slot; + TValue *rb = KB(i); + TValue *rc = RKC(i); + TString *key = tsvalue(rb); /* key must be a string */ + if (!luaV_fastset(L, ra, key, slot, luaH_getstr, rc)) + Protect(luaV_finishset(L, ra, rb, rc, slot)); + vmbreak; + } vmcase(OP_NEWTABLE) { int b = GETARG_B(i); int c = GETARG_C(i); From a454e884e0b6657dd34c6dc36732d0e70ade35e8 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Sat, 29 Apr 2017 12:28:38 -0300 Subject: [PATCH 0028/1145] details in 'findsetreg' --- ldebug.c | 36 +++++++++++++++++------------------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/ldebug.c b/ldebug.c index 8a36e2f80a..ead1679449 100644 --- a/ldebug.c +++ b/ldebug.c @@ -1,5 +1,5 @@ /* -** $Id: ldebug.c,v 2.122 2017/04/26 17:46:52 roberto Exp roberto $ +** $Id: ldebug.c,v 2.123 2017/04/28 20:57:45 roberto Exp roberto $ ** Debug Interface ** See Copyright Notice in lua.h */ @@ -398,39 +398,37 @@ static int findsetreg (Proto *p, int lastpc, int reg) { Instruction i = p->code[pc]; OpCode op = GET_OPCODE(i); int a = GETARG_A(i); + int change; /* true if current instruction changed 'reg' */ switch (op) { - case OP_LOADNIL: { + case OP_LOADNIL: { /* set registers from 'a' to 'a+b' */ int b = GETARG_B(i); - if (a <= reg && reg <= a + b) /* set registers from 'a' to 'a+b' */ - setreg = filterpc(pc, jmptarget); + change = (a <= reg && reg <= a + b); break; } - case OP_TFORCALL: { - if (reg >= a + 2) /* affect all regs above its base */ - setreg = filterpc(pc, jmptarget); + case OP_TFORCALL: { /* affect all regs above its base */ + change = (reg >= a + 2); break; } case OP_CALL: - case OP_TAILCALL: { - if (reg >= a) /* affect all registers above base */ - setreg = filterpc(pc, jmptarget); + case OP_TAILCALL: { /* affect all registers above base */ + change = (reg >= a); break; } - case OP_JMP: { + case OP_JMP: { /* doesn't change registers, but changes 'jmptarget' */ int b = GETARG_sBx(i); int dest = pc + 1 + b; - /* jump is forward and do not skip 'lastpc'? */ - if (pc < dest && dest <= lastpc) { - if (dest > jmptarget) - jmptarget = dest; /* update 'jmptarget' */ - } + /* jump does not skip 'lastpc' and is larger than current one? */ + if (dest <= lastpc && dest > jmptarget) + jmptarget = dest; /* update 'jmptarget' */ + change = 0; break; } - default: - if (testAMode(op) && reg == a) /* any instruction that set A */ - setreg = filterpc(pc, jmptarget); + default: /* any instruction that sets A */ + change = (testAMode(op) && reg == a); break; } + if (change) + setreg = filterpc(pc, jmptarget); } return setreg; } From 5ecb31003f8086f057444468d0356771526ec7b4 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Sat, 29 Apr 2017 15:09:17 -0300 Subject: [PATCH 0029/1145] bug: cannot "skip" labels after if-goto before the jump over the 'then' part --- lparser.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lparser.c b/lparser.c index f78f64bda8..af5891c2c3 100644 --- a/lparser.c +++ b/lparser.c @@ -1,5 +1,5 @@ /* -** $Id: lparser.c,v 2.156 2017/04/20 19:53:55 roberto Exp roberto $ +** $Id: lparser.c,v 2.157 2017/04/28 20:57:45 roberto Exp roberto $ ** Lua Parser ** See Copyright Notice in lua.h */ @@ -1400,7 +1400,7 @@ static void test_then_block (LexState *ls, int *escapelist) { luaK_goiffalse(ls->fs, &v); /* will jump to label if condition is true */ enterblock(fs, &bl, 0); /* must enter block before 'goto' */ gotostat(ls, v.t); /* handle goto/break */ - skipnoopstat(ls); /* skip other no-op statements */ + while (testnext(ls, ';')) {} /* skip colons */ if (block_follow(ls, 0)) { /* 'goto' is the entire block? */ leaveblock(fs); return; /* and that is it */ From 8634b2a0119e698e362fdb765f30258e79e1dfd0 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Sun, 30 Apr 2017 17:43:26 -0300 Subject: [PATCH 0030/1145] added 'cachemiss' field to prototype to avoid wasting time checking hits that fail too often --- lfunc.c | 3 ++- lgc.c | 3 ++- lobject.h | 3 ++- lvm.c | 12 +++++++++--- 4 files changed, 15 insertions(+), 6 deletions(-) diff --git a/lfunc.c b/lfunc.c index 898104acc7..5c6ebd27f4 100644 --- a/lfunc.c +++ b/lfunc.c @@ -1,5 +1,5 @@ /* -** $Id: lfunc.c,v 2.46 2017/04/06 13:08:56 roberto Exp roberto $ +** $Id: lfunc.c,v 2.47 2017/04/11 18:41:09 roberto Exp roberto $ ** Auxiliary functions to manipulate prototypes and closures ** See Copyright Notice in lua.h */ @@ -115,6 +115,7 @@ Proto *luaF_newproto (lua_State *L) { f->sizep = 0; f->code = NULL; f->cache = NULL; + f->cachemiss = 0; f->sizecode = 0; f->lineinfo = NULL; f->sizelineinfo = 0; diff --git a/lgc.c b/lgc.c index 692326f31b..65f4909cd7 100644 --- a/lgc.c +++ b/lgc.c @@ -1,5 +1,5 @@ /* -** $Id: lgc.c,v 2.225 2017/04/24 16:59:26 roberto Exp $ +** $Id: lgc.c,v 2.226 2017/04/24 17:52:18 roberto Exp roberto $ ** Garbage Collector ** See Copyright Notice in lua.h */ @@ -485,6 +485,7 @@ static int traverseproto (global_State *g, Proto *f) { int i; if (f->cache && iswhite(f->cache)) f->cache = NULL; /* allow cache to be collected */ + f->cachemiss = 0; /* restart counting */ markobjectN(g, f->source); for (i = 0; i < f->sizek; i++) /* mark literals */ markvalue(g, &f->k[i]); diff --git a/lobject.h b/lobject.h index c8f9adb3b3..f8a73a143a 100644 --- a/lobject.h +++ b/lobject.h @@ -1,5 +1,5 @@ /* -** $Id: lobject.h,v 2.118 2017/04/11 18:41:09 roberto Exp roberto $ +** $Id: lobject.h,v 2.119 2017/04/24 18:06:12 roberto Exp roberto $ ** Type definitions for Lua objects ** See Copyright Notice in lua.h */ @@ -410,6 +410,7 @@ typedef struct Proto { lu_byte numparams; /* number of fixed parameters */ lu_byte is_vararg; lu_byte maxstacksize; /* number of registers needed by this function */ + lu_byte cachemiss; /* count for successive misses for 'cache' field */ int sizeupvalues; /* size of 'upvalues' */ int sizek; /* size of 'k' */ int sizecode; diff --git a/lvm.c b/lvm.c index 9fa9daf593..744ca6dd7b 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.273 2017/04/26 17:46:52 roberto Exp roberto $ +** $Id: lvm.c,v 2.274 2017/04/28 20:57:45 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -618,6 +618,7 @@ static LClosure *getcached (Proto *p, UpVal **encup, StkId base) { if (c->upvals[i]->v != v) return NULL; /* wrong upvalue; cannot reuse closure */ } + p->cachemiss = 0; /* got a hit */ } return c; /* return cached closure (or NULL if no cached closure) */ } @@ -644,8 +645,13 @@ static void pushclosure (lua_State *L, Proto *p, UpVal **encup, StkId base, ncl->upvals[i] = encup[uv[i].idx]; /* new closure is white, so we do not need a barrier here */ } - if (!isblack(p)) /* cache will not break GC invariant? */ - p->cache = ncl; /* save it on cache for reuse */ + if (p->cachemiss >= 10) /* too many missings? */ + p->cache = NULL; /* give up cache */ + else { + if (!isblack(p)) /* cache will not break GC invariant? */ + p->cache = ncl; /* save it on cache for reuse */ + p->cachemiss++; + } } From 2376eb634751f92e6bcb9dc8dbc1ef88b9873319 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 4 May 2017 10:32:01 -0300 Subject: [PATCH 0031/1145] barrier for prototype's cache (with new gray list 'protogray' to keep prototypes to have their caches visited again) + constant 'MAXMISS' --- lfunc.h | 9 ++++++- lgc.c | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++------ lgc.h | 6 ++++- lstate.c | 4 +-- lstate.h | 6 +++-- ltests.c | 5 +++- lvm.c | 12 ++++----- 7 files changed, 95 insertions(+), 22 deletions(-) diff --git a/lfunc.h b/lfunc.h index 7d0eca4af3..eca83e4f2e 100644 --- a/lfunc.h +++ b/lfunc.h @@ -1,5 +1,5 @@ /* -** $Id: lfunc.h,v 2.15 2015/01/13 15:49:11 roberto Exp roberto $ +** $Id: lfunc.h,v 2.16 2017/04/11 18:41:09 roberto Exp roberto $ ** Auxiliary functions to manipulate prototypes and closures ** See Copyright Notice in lua.h */ @@ -32,6 +32,13 @@ #define upisopen(up) ((up)->v != &(up)->u.value) +/* +** maximum number of misses before giving up the cache of closures +** in prototypes +*/ +#define MAXMISS 10 + + LUAI_FUNC Proto *luaF_newproto (lua_State *L); LUAI_FUNC CClosure *luaF_newCclosure (lua_State *L, int nelems); LUAI_FUNC LClosure *luaF_newLclosure (lua_State *L, int nelems); diff --git a/lgc.c b/lgc.c index 65f4909cd7..a025340561 100644 --- a/lgc.c +++ b/lgc.c @@ -1,5 +1,5 @@ /* -** $Id: lgc.c,v 2.226 2017/04/24 17:52:18 roberto Exp roberto $ +** $Id: lgc.c,v 2.227 2017/04/30 20:43:26 roberto Exp roberto $ ** Garbage Collector ** See Copyright Notice in lua.h */ @@ -179,6 +179,26 @@ void luaC_barrierback_ (lua_State *L, Table *t) { } +/* +** Barrier for prototype's cache of closures. For an 'old1' +** object, making it gray stops it from being visited by 'markold', +** so it is linked in the 'grayagain' list to ensure it will be +** visited. Otherwise, it goes to 'protogray', as only its 'cache' field +** needs to be revisited. (A prototype to be in this barrier must be +** already finished, so its other fields cannot change and do not need +** to be revisited.) +*/ +LUAI_FUNC void luaC_protobarrier_ (lua_State *L, Proto *p) { + global_State *g = G(L); + lua_assert(g->gckind != KGC_GEN || isold(p)); + if (getage(p) == G_OLD1) /* still need to be visited? */ + linkgclist(p, g->grayagain); /* link it in 'grayagain' */ + else + linkgclist(p, g->protogray); /* link it in 'protogray' */ + black2gray(p); /* make prototype gray (to avoid other barriers) */ +} + + void luaC_fix (lua_State *L, GCObject *o) { global_State *g = G(L); lua_assert(g->allgc == o); /* object must be 1st in 'allgc' list! */ @@ -332,7 +352,7 @@ static void remarkupvals (global_State *g) { */ static void restartcollection (global_State *g) { g->gray = g->grayagain = NULL; - g->weak = g->allweak = g->ephemeron = NULL; + g->weak = g->allweak = g->ephemeron = g->protogray = NULL; markobject(g, g->mainthread); markvalue(g, &g->l_registry); markmt(g); @@ -476,6 +496,32 @@ static lu_mem traversetable (global_State *g, Table *h) { } +/* +** Check the cache of a prototype, to keep invariants. If the +** cache is white, clear it. (A cache should not prevent the +** collection of its reference.) Otherwise, if in generational +** mode, check the generational invariant. If the cache is old, +** everything is ok. If the prototype is 'old0', everything +** is ok too. (It will naturally be visited again.) If the +** prototype is older than 'old0', then its cache (whith is new) +** must be visited again in the next collection, so the prototype +** goes to the 'protogray' list. (If the prototype has a cache, +** it is already immutable and does not need other barriers; +** then, it can become gray without problems for its other fields.) +*/ +static void checkprotocache (global_State *g, Proto *p) { + if (p->cache) { + if (iswhite(p->cache)) + p->cache = NULL; /* allow cache to be collected */ + else if (g->gckind == KGC_GEN && !isold(p->cache) && getage(p) >= G_OLD1) { + linkgclist(p, g->protogray); /* link it in 'protogray' */ + black2gray(p); /* make prototype gray */ + } + } + p->cachemiss = 0; /* restart counting */ +} + + /* ** Traverse a prototype. (While a prototype is being build, its ** arrays can be larger than needed; the extra slots are filled with @@ -483,9 +529,7 @@ static lu_mem traversetable (global_State *g, Table *h) { */ static int traverseproto (global_State *g, Proto *f) { int i; - if (f->cache && iswhite(f->cache)) - f->cache = NULL; /* allow cache to be collected */ - f->cachemiss = 0; /* restart counting */ + checkprotocache(g, f); markobjectN(g, f->source); for (i = 0; i < f->sizek; i++) /* mark literals */ markvalue(g, &f->k[i]); @@ -629,6 +673,19 @@ static void convergeephemerons (global_State *g) { ** ======================================================= */ +static void clearprotolist (global_State *g) { + GCObject *p = g->protogray; + g->protogray = NULL; + while (p != NULL) { + Proto *pp = gco2p(p); + GCObject *next = pp->gclist; + lua_assert(isgray(pp) && (pp->cache != NULL || pp->cachemiss >= MAXMISS)); + gray2black(pp); + checkprotocache(g, pp); + p = next; + } +} + /* ** clear entries with unmarked keys from all weaktables in list 'l' @@ -1073,14 +1130,15 @@ static void correctgraylists (global_State *g) { /* -** Mark 'old1' objects when starting a new young collection. (Threads -** and open upvalues are always gray, and do not need to be marked. -** All other old objects are black.) +** Mark 'old1' objects when starting a new young collection. +** Gray objects are already in some gray list, and so will be visited +** in the atomic step. */ static void markold (global_State *g, GCObject *from, GCObject *to) { GCObject *p; for (p = from; p != to; p = p->next) { if (getage(p) == G_OLD1) { + lua_assert(!iswhite(p)); if (isblack(p)) { black2gray(p); /* should be '2white', but gray works too */ reallymarkobject(g, p); @@ -1337,6 +1395,7 @@ static l_mem atomic (lua_State *L) { clearvalues(g, g->weak, origweak); clearvalues(g, g->allweak, origall); luaS_clearcache(g); + clearprotolist(g); g->currentwhite = cast_byte(otherwhite(g)); /* flip current white */ lua_assert(g->gray == NULL); work += g->GCmemtrav; /* complete counting */ diff --git a/lgc.h b/lgc.h index 396f5a4e48..6ee0b01275 100644 --- a/lgc.h +++ b/lgc.h @@ -1,5 +1,5 @@ /* -** $Id: lgc.h,v 2.95 2017/04/10 13:33:04 roberto Exp roberto $ +** $Id: lgc.h,v 2.96 2017/04/11 18:41:09 roberto Exp roberto $ ** Garbage Collector ** See Copyright Notice in lua.h */ @@ -151,6 +151,9 @@ (isblack(p) && iswhite(o)) ? \ luaC_barrier_(L,obj2gco(p),obj2gco(o)) : cast_void(0)) +#define luaC_protobarrier(L,p,o) \ + (isblack(p) ? luaC_protobarrier_(L,p) : cast_void(0)) + LUAI_FUNC void luaC_fix (lua_State *L, GCObject *o); LUAI_FUNC void luaC_freeallobjects (lua_State *L); LUAI_FUNC void luaC_step (lua_State *L); @@ -159,6 +162,7 @@ LUAI_FUNC void luaC_fullgc (lua_State *L, int isemergency); LUAI_FUNC GCObject *luaC_newobj (lua_State *L, int tt, size_t sz); LUAI_FUNC void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v); LUAI_FUNC void luaC_barrierback_ (lua_State *L, Table *o); +LUAI_FUNC void luaC_protobarrier_ (lua_State *L, Proto *p); LUAI_FUNC void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt); LUAI_FUNC void luaC_changemode (lua_State *L, int newmode); diff --git a/lstate.c b/lstate.c index 653c90f2d0..3bce076d6f 100644 --- a/lstate.c +++ b/lstate.c @@ -1,5 +1,5 @@ /* -** $Id: lstate.c,v 2.137 2017/04/12 18:56:25 roberto Exp roberto $ +** $Id: lstate.c,v 2.138 2017/04/24 16:59:26 roberto Exp roberto $ ** Global State ** See Copyright Notice in lua.h */ @@ -324,7 +324,7 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { g->finobjsur = g->finobjold = g->finobjrold = NULL; g->sweepgc = NULL; g->gray = g->grayagain = NULL; - g->weak = g->ephemeron = g->allweak = NULL; + g->weak = g->ephemeron = g->allweak = g->protogray = NULL; g->twups = NULL; g->totalbytes = sizeof(LG); g->GCdebt = 0; diff --git a/lstate.h b/lstate.h index d2f23839e8..533d6ad6fc 100644 --- a/lstate.h +++ b/lstate.h @@ -1,5 +1,5 @@ /* -** $Id: lstate.h,v 2.138 2017/04/19 17:02:50 roberto Exp roberto $ +** $Id: lstate.h,v 2.139 2017/04/24 16:59:26 roberto Exp roberto $ ** Global State ** See Copyright Notice in lua.h */ @@ -43,7 +43,8 @@ ** 'weak': tables with weak values to be cleared; ** 'ephemeron': ephemeron tables with white->white entries; ** 'allweak': tables with weak keys and/or weak values to be cleared. -** The last three lists are used only during the atomic phase. +** There is also a list 'protogray' for prototypes that need to have +** their caches cleared. */ @@ -159,6 +160,7 @@ typedef struct global_State { GCObject *weak; /* list of tables with weak values */ GCObject *ephemeron; /* list of ephemeron tables (weak keys) */ GCObject *allweak; /* list of all-weak tables */ + GCObject *protogray; /* list of prototypes with "new" caches */ GCObject *tobefnz; /* list of userdata to be GC */ GCObject *fixedgc; /* list of objects not to be collected */ /* fields for generational collector */ diff --git a/ltests.c b/ltests.c index 4ac2c702be..93a8428409 100644 --- a/ltests.c +++ b/ltests.c @@ -1,5 +1,5 @@ /* -** $Id: ltests.c,v 2.215 2017/04/24 16:59:26 roberto Exp roberto $ +** $Id: ltests.c,v 2.216 2017/04/24 18:06:12 roberto Exp roberto $ ** Internal Module for Debugging of the Lua Implementation ** See Copyright Notice in lua.h */ @@ -409,6 +409,8 @@ static void checkobject (global_State *g, GCObject *o, int maybedead, getage(o) == G_TOUCHED1 || getage(o) == G_OLD0 || o->tt == LUA_TTHREAD || + (o->tt == LUA_TPROTO && + (gco2p(o)->cache != NULL || gco2p(o)->cachemiss >= MAXMISS)) || (o->tt == LUA_TUPVAL && upisopen(gco2upv(o)))); } } @@ -446,6 +448,7 @@ static void markgrays (global_State *g) { checkgraylist(g, g->weak); checkgraylist(g, g->ephemeron); checkgraylist(g, g->allweak); + checkgraylist(g, g->protogray); } diff --git a/lvm.c b/lvm.c index 744ca6dd7b..28833458b9 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.274 2017/04/28 20:57:45 roberto Exp roberto $ +** $Id: lvm.c,v 2.275 2017/04/30 20:43:26 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -626,9 +626,7 @@ static LClosure *getcached (Proto *p, UpVal **encup, StkId base) { /* ** create a new Lua closure, push it in the stack, and initialize -** its upvalues. Note that the closure is not cached if prototype is -** already black (which means that 'cache' was already cleared by the -** GC). +** its upvalues. ??? */ static void pushclosure (lua_State *L, Proto *p, UpVal **encup, StkId base, StkId ra) { @@ -645,11 +643,11 @@ static void pushclosure (lua_State *L, Proto *p, UpVal **encup, StkId base, ncl->upvals[i] = encup[uv[i].idx]; /* new closure is white, so we do not need a barrier here */ } - if (p->cachemiss >= 10) /* too many missings? */ + if (p->cachemiss >= MAXMISS) /* too many missings? */ p->cache = NULL; /* give up cache */ else { - if (!isblack(p)) /* cache will not break GC invariant? */ - p->cache = ncl; /* save it on cache for reuse */ + p->cache = ncl; /* save it on cache for reuse */ + luaC_protobarrier(L, p, ncl); p->cachemiss++; } } From 4ce8d2047c0613992ab092cf3ed78ebceffa61e3 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 5 May 2017 12:55:36 -0300 Subject: [PATCH 0032/1145] bug: Wrong code for a goto followed by a label inside an 'if' --- bugs | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/bugs b/bugs index caafae5aa9..d2ec34d269 100644 --- a/bugs +++ b/bugs @@ -3656,9 +3656,9 @@ It needs an "interceptor" 'memcmp' function that continues reading memory after a difference is found.]], patch = [[ 2c2 -< ** $Id: bugs,v 1.150 2016/07/19 17:10:45 roberto Exp roberto $ +< ** $Id: bugs,v 1.151 2016/10/19 12:34:27 roberto Exp roberto $ --- -> ** $Id: bugs,v 1.150 2016/07/19 17:10:45 roberto Exp roberto $ +> ** $Id: bugs,v 1.151 2016/10/19 12:34:27 roberto Exp roberto $ 263c263,264 < for (option = LUA_STRFTIMEOPTIONS; *option != '\0'; option += oplen) { --- @@ -3714,6 +3714,38 @@ patch = [[ ]=] +Bug{ +what = [[Wrong code for a goto followed by a label inside an 'if']], +report = [[云风, 2017/04/13]], +since = [[5.2]], +fix = nil, +example = [[ +-- should print 32323232..., but prints only '3' +if true then + goto LBL + ::loop:: + print(2) + ::LBL:: + print(3) + goto loop +end +]], +patch = [[ +--- lparser.c 2017/04/19 17:20:42 2.155.1.1 ++++ lparser.c 2017/04/29 18:11:40 2.155.1.2 +@@ -1392,7 +1392,7 @@ + luaK_goiffalse(ls->fs, &v); /* will jump to label if condition is true */ + enterblock(fs, &bl, 0); /* must enter block before 'goto' */ + gotostat(ls, v.t); /* handle goto/break */ +- skipnoopstat(ls); /* skip other no-op statements */ ++ while (testnext(ls, ';')) {} /* skip colons */ + if (block_follow(ls, 0)) { /* 'goto' is the entire block? */ + leaveblock(fs); + return; /* and that is it */ +]] +} + + --[=[ Bug{ what = [[ ]], From e8757a73e64c351bdafb75ee45be60e7ab4b3426 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 5 May 2017 14:16:11 -0300 Subject: [PATCH 0033/1145] 'luaV_execute' keeps local copy of program counter and hook mask, to avoid excessive access to globals. --- lvm.c | 91 ++++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 62 insertions(+), 29 deletions(-) diff --git a/lvm.c b/lvm.c index 28833458b9..cab599d003 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.275 2017/04/30 20:43:26 roberto Exp roberto $ +** $Id: lvm.c,v 2.276 2017/05/04 13:32:01 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -742,17 +742,37 @@ void luaV_finishOp (lua_State *L) { ISK(GETARG_C(i)) ? k+INDEXK(GETARG_C(i)) : base+GETARG_C(i)) -/* execute a jump instruction */ + +#define updatemask(L) (mask = L->hookmask & (LUA_MASKLINE | LUA_MASKCOUNT)) + + +/* +** Execute a jump instruction. The 'updatemask' allows signals to stop +** tight loops. (Without it, the local copy of 'mask' could never change.) +*/ #define dojump(ci,i,e) \ { int a = GETARG_A(i); \ if (a != 0) luaF_close(L, ci->u.l.base + a - 1); \ - ci->u.l.savedpc += GETARG_sBx(i) + e; } + pc += GETARG_sBx(i) + e; updatemask(L); } + /* for test instructions, execute the jump instruction that follows it */ -#define donextjump(ci) { i = *ci->u.l.savedpc; dojump(ci, i, 1); } +#define donextjump(ci) { i = *pc; dojump(ci, i, 1); } +/* +** Whenever code can raise errors (including memory errors), the global +** 'pc' must be correct to report occasional errors. +*/ +#define savepc(L) (ci->u.l.savedpc = pc) + + +/* +** Protect code that, in general, can raise errors, reallocate the +** stack, and change the hooks. +*/ +#define Protect(code) \ + { savepc(L); {code;}; base = ci->u.l.base; updatemask(L); } -#define Protect(x) { {x;}; base = ci->u.l.base; } #define checkGC(L,c) \ { luaC_condGC(L, L->top = (c), /* limit of live values */ \ @@ -762,12 +782,9 @@ void luaV_finishOp (lua_State *L) { /* fetch an instruction and prepare its execution */ #define vmfetch() { \ - i = *(ci->u.l.savedpc++); \ - if (L->hookmask & (LUA_MASKLINE | LUA_MASKCOUNT)) \ - Protect(luaG_traceexec(L)); \ + i = *(pc++); \ + if (mask) Protect(luaG_traceexec(L)); \ ra = RA(i); /* WARNING: any stack reallocation invalidates 'ra' */ \ - lua_assert(base == ci->u.l.base); \ - lua_assert(base <= L->top && L->top < L->stack + L->stacksize); \ } #define vmdispatch(o) switch(o) @@ -795,18 +812,24 @@ void luaV_execute (lua_State *L) { CallInfo *ci = L->ci; LClosure *cl; TValue *k; - StkId base; + StkId base; /* local copy of 'ci->u.l.base' */ + int mask; /* local copy of 'L->hookmask & (LUA_MASKLINE | LUA_MASKCOUNT)' */ + const Instruction *pc; /* local copy of 'ci->u.l.savedpc' */ ci->callstatus |= CIST_FRESH; /* fresh invocation of 'luaV_execute" */ newframe: /* reentry point when frame changes (call/return) */ lua_assert(ci == L->ci); cl = clLvalue(ci->func); /* local reference to function's closure */ k = cl->p->k; /* local reference to function's constant table */ - base = ci->u.l.base; /* local copy of function's base */ + updatemask(L); + base = ci->u.l.base; + pc = ci->u.l.savedpc; /* main loop of interpreter */ for (;;) { Instruction i; StkId ra; vmfetch(); + lua_assert(base == ci->u.l.base); + lua_assert(base <= L->top && L->top < L->stack + L->stacksize); vmdispatch (GET_OPCODE(i)) { vmcase(OP_MOVE) { setobjs2s(L, ra, RB(i)); @@ -824,14 +847,14 @@ void luaV_execute (lua_State *L) { } vmcase(OP_LOADKX) { TValue *rb; - lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_EXTRAARG); - rb = k + GETARG_Ax(*ci->u.l.savedpc++); + lua_assert(GET_OPCODE(*pc) == OP_EXTRAARG); + rb = k + GETARG_Ax(*pc++); setobj2s(L, ra, rb); vmbreak; } vmcase(OP_LOADBOOL) { setbvalue(ra, GETARG_B(i)); - if (GETARG_C(i)) ci->u.l.savedpc++; /* skip next instruction (if C) */ + if (GETARG_C(i)) pc++; /* skip next instruction (if C) */ vmbreak; } vmcase(OP_LOADNIL) { @@ -933,7 +956,9 @@ void luaV_execute (lua_State *L) { vmcase(OP_NEWTABLE) { int b = GETARG_B(i); int c = GETARG_C(i); - Table *t = luaH_new(L); + Table *t; + savepc(L); /* in case of allocation errors */ + t = luaH_new(L); sethvalue(L, ra, t); if (b != 0 || c != 0) luaH_resize(L, t, luaO_fb2int(b), luaO_fb2int(c)); @@ -1173,7 +1198,7 @@ void luaV_execute (lua_State *L) { TValue *rc = RKC(i); Protect( if (luaV_equalobj(L, rb, rc) != GETARG_A(i)) - ci->u.l.savedpc++; + pc++; else donextjump(ci); ) @@ -1182,7 +1207,7 @@ void luaV_execute (lua_State *L) { vmcase(OP_LT) { Protect( if (luaV_lessthan(L, RKB(i), RKC(i)) != GETARG_A(i)) - ci->u.l.savedpc++; + pc++; else donextjump(ci); ) @@ -1191,7 +1216,7 @@ void luaV_execute (lua_State *L) { vmcase(OP_LE) { Protect( if (luaV_lessequal(L, RKB(i), RKC(i)) != GETARG_A(i)) - ci->u.l.savedpc++; + pc++; else donextjump(ci); ) @@ -1199,7 +1224,7 @@ void luaV_execute (lua_State *L) { } vmcase(OP_TEST) { if (GETARG_C(i) ? l_isfalse(ra) : !l_isfalse(ra)) - ci->u.l.savedpc++; + pc++; else donextjump(ci); vmbreak; @@ -1207,7 +1232,7 @@ void luaV_execute (lua_State *L) { vmcase(OP_TESTSET) { TValue *rb = RB(i); if (GETARG_C(i) ? l_isfalse(rb) : !l_isfalse(rb)) - ci->u.l.savedpc++; + pc++; else { setobjs2s(L, ra, rb); donextjump(ci); @@ -1218,6 +1243,7 @@ void luaV_execute (lua_State *L) { int b = GETARG_B(i); int nresults = GETARG_C(i) - 1; if (b != 0) L->top = ra+b; /* else previous instruction set top */ + savepc(L); if (luaD_precall(L, ra, nresults)) { /* C function? */ if (nresults >= 0) L->top = ci->top; /* adjust results */ @@ -1233,6 +1259,7 @@ void luaV_execute (lua_State *L) { int b = GETARG_B(i); if (b != 0) L->top = ra+b; /* else previous instruction set top */ lua_assert(GETARG_C(i) - 1 == LUA_MULTRET); + savepc(L); if (luaD_precall(L, ra, LUA_MULTRET)) { /* C function? */ Protect((void)0); /* update 'base' */ } @@ -1263,6 +1290,7 @@ void luaV_execute (lua_State *L) { vmcase(OP_RETURN) { int b = GETARG_B(i); if (cl->p->sizep > 0) luaF_close(L, base); + savepc(L); b = luaD_poscall(L, ci, ra, (b != 0 ? b - 1 : cast_int(L->top - ra))); if (ci->callstatus & CIST_FRESH) /* local 'ci' still from callee */ return; /* external invocation: return */ @@ -1280,7 +1308,7 @@ void luaV_execute (lua_State *L) { lua_Integer idx = intop(+, ivalue(ra), step); /* increment index */ lua_Integer limit = ivalue(ra + 1); if ((0 < step) ? (idx <= limit) : (limit <= idx)) { - ci->u.l.savedpc += GETARG_sBx(i); /* jump back */ + pc += GETARG_sBx(i); /* jump back */ chgivalue(ra, idx); /* update internal index... */ setivalue(ra + 3, idx); /* ...and external index */ } @@ -1291,11 +1319,12 @@ void luaV_execute (lua_State *L) { lua_Number limit = fltvalue(ra + 1); if (luai_numlt(0, step) ? luai_numle(idx, limit) : luai_numle(limit, idx)) { - ci->u.l.savedpc += GETARG_sBx(i); /* jump back */ + pc += GETARG_sBx(i); /* jump back */ chgfltvalue(ra, idx); /* update internal index... */ setfltvalue(ra + 3, idx); /* ...and external index */ } } + updatemask(L); vmbreak; } vmcase(OP_FORPREP) { @@ -1313,6 +1342,7 @@ void luaV_execute (lua_State *L) { } else { /* try making all values floats */ lua_Number ninit; lua_Number nlimit; lua_Number nstep; + savepc(L); /* in case of errors */ if (!tonumber(plimit, &nlimit)) luaG_runerror(L, "'for' limit must be a number"); setfltvalue(plimit, nlimit); @@ -1323,7 +1353,7 @@ void luaV_execute (lua_State *L) { luaG_runerror(L, "'for' initial value must be a number"); setfltvalue(init, luai_numsub(L, ninit, nstep)); } - ci->u.l.savedpc += GETARG_sBx(i); + pc += GETARG_sBx(i); vmbreak; } vmcase(OP_TFORCALL) { @@ -1334,7 +1364,7 @@ void luaV_execute (lua_State *L) { L->top = cb + 3; /* func. + 2 args (state and index) */ Protect(luaD_call(L, cb, GETARG_C(i))); L->top = ci->top; - i = *(ci->u.l.savedpc++); /* go to next instruction */ + i = *(pc++); /* go to next instruction */ ra = RA(i); lua_assert(GET_OPCODE(i) == OP_TFORLOOP); goto l_tforloop; @@ -1343,7 +1373,7 @@ void luaV_execute (lua_State *L) { l_tforloop: if (!ttisnil(ra + 1)) { /* continue loop? */ setobjs2s(L, ra, ra + 1); /* save control variable */ - ci->u.l.savedpc += GETARG_sBx(i); /* jump back */ + pc += GETARG_sBx(i); /* jump back */ } vmbreak; } @@ -1354,11 +1384,12 @@ void luaV_execute (lua_State *L) { Table *h; if (n == 0) n = cast_int(L->top - ra) - 1; if (c == 0) { - lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_EXTRAARG); - c = GETARG_Ax(*ci->u.l.savedpc++); + lua_assert(GET_OPCODE(*pc) == OP_EXTRAARG); + c = GETARG_Ax(*pc++); } h = hvalue(ra); last = ((c-1)*LFIELDS_PER_FLUSH) + n; + savepc(L); /* in case of allocation errors */ if (last > h->sizearray) /* needs more space? */ luaH_resizearray(L, h, last); /* preallocate it at once */ for (; n > 0; n--) { @@ -1372,8 +1403,10 @@ void luaV_execute (lua_State *L) { vmcase(OP_CLOSURE) { Proto *p = cl->p->p[GETARG_Bx(i)]; LClosure *ncl = getcached(p, cl->upvals, base); /* cached closure */ - if (ncl == NULL) /* no match? */ + if (ncl == NULL) { /* no match? */ + savepc(L); /* in case of allocation errors */ pushclosure(L, p, cl->upvals, base, ra); /* create a new one */ + } else setclLvalue(L, ra, ncl); /* push cashed closure */ checkGC(L, ra + 1); From fb9de1b4d77494d55c6e0c3c8e5c681dad36b49e Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 8 May 2017 12:57:23 -0300 Subject: [PATCH 0034/1145] detail ('luaT_callbinTM' does not need to be extern) --- ltm.c | 10 +++++----- ltm.h | 4 +--- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/ltm.c b/ltm.c index 473413844a..0ef763131d 100644 --- a/ltm.c +++ b/ltm.c @@ -1,5 +1,5 @@ /* -** $Id: ltm.c,v 2.38 2016/12/22 13:08:50 roberto Exp roberto $ +** $Id: ltm.c,v 2.39 2017/04/11 18:41:09 roberto Exp roberto $ ** Tag methods ** See Copyright Notice in lua.h */ @@ -121,8 +121,8 @@ void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1, } -int luaT_callbinTM (lua_State *L, const TValue *p1, const TValue *p2, - StkId res, TMS event) { +static int callbinTM (lua_State *L, const TValue *p1, const TValue *p2, + StkId res, TMS event) { const TValue *tm = luaT_gettmbyobj(L, p1, event); /* try first operand */ if (ttisnil(tm)) tm = luaT_gettmbyobj(L, p2, event); /* try second operand */ @@ -134,7 +134,7 @@ int luaT_callbinTM (lua_State *L, const TValue *p1, const TValue *p2, void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2, StkId res, TMS event) { - if (!luaT_callbinTM(L, p1, p2, res, event)) { + if (!callbinTM(L, p1, p2, res, event)) { switch (event) { case TM_CONCAT: luaG_concaterror(L, p1, p2); @@ -157,7 +157,7 @@ void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2, int luaT_callorderTM (lua_State *L, const TValue *p1, const TValue *p2, TMS event) { - if (!luaT_callbinTM(L, p1, p2, L->top, event)) + if (!callbinTM(L, p1, p2, L->top, event)) return -1; /* no metamethod */ else return !l_isfalse(L->top); diff --git a/ltm.h b/ltm.h index 70569f82c9..535e188505 100644 --- a/ltm.h +++ b/ltm.h @@ -1,5 +1,5 @@ /* -** $Id: ltm.h,v 2.21 2014/10/25 11:50:46 roberto Exp roberto $ +** $Id: ltm.h,v 2.22 2016/02/26 19:20:15 roberto Exp roberto $ ** Tag methods ** See Copyright Notice in lua.h */ @@ -64,8 +64,6 @@ LUAI_FUNC void luaT_init (lua_State *L); LUAI_FUNC void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1, const TValue *p2, TValue *p3, int hasres); -LUAI_FUNC int luaT_callbinTM (lua_State *L, const TValue *p1, const TValue *p2, - StkId res, TMS event); LUAI_FUNC void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2, StkId res, TMS event); LUAI_FUNC int luaT_callorderTM (lua_State *L, const TValue *p1, From ab5a6500296dbd847632ccc2fb43b44bba4c34f5 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 8 May 2017 13:08:01 -0300 Subject: [PATCH 0035/1145] details (direct access to 'Ck' bit in instructions) --- lopcodes.h | 5 ++++- lvm.c | 6 +++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/lopcodes.h b/lopcodes.h index 6c9df6f32f..683f8928b1 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -1,5 +1,5 @@ /* -** $Id: lopcodes.h,v 1.152 2017/04/26 17:46:52 roberto Exp roberto $ +** $Id: lopcodes.h,v 1.153 2017/04/28 20:57:45 roberto Exp roberto $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ @@ -106,6 +106,9 @@ enum OpMode {iABC, iABx, iAsBx, iAx}; /* basic instruction format */ #define GETARG_C(i) getarg(i, POS_C, SIZE_C) #define SETARG_C(i,v) setarg(i, v, POS_C, SIZE_C) +#define GETARG_Cr(i) getarg(i, POS_C, SIZE_C - 1) +#define GETARG_Ck(i) getarg(i, (POS_C + SIZE_C - 1), 1) + #define GETARG_Bx(i) getarg(i, POS_Bx, SIZE_Bx) #define SETARG_Bx(i,v) setarg(i, v, POS_Bx, SIZE_Bx) diff --git a/lvm.c b/lvm.c index cab599d003..1a7531e178 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.276 2017/05/04 13:32:01 roberto Exp roberto $ +** $Id: lvm.c,v 2.277 2017/05/05 17:16:11 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -737,9 +737,9 @@ void luaV_finishOp (lua_State *L) { #define RC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgR, base+GETARG_C(i)) #define KC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgK, k+GETARG_C(i)) #define RKB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgK, \ - (GETARG_Bk(i)) ? k+GETARG_Br(i) : base+GETARG_Br(i)) + (GETARG_Bk(i)) ? k + GETARG_Br(i) : base + GETARG_Br(i)) #define RKC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgK, \ - ISK(GETARG_C(i)) ? k+INDEXK(GETARG_C(i)) : base+GETARG_C(i)) + (GETARG_Ck(i)) ? k + GETARG_Cr(i) : base + GETARG_Cr(i)) From b1b7790f7cf11708221aff37b4da816de97300fb Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 9 May 2017 11:39:46 -0300 Subject: [PATCH 0036/1145] detail ('1' -> '1u' in unsigned operation) --- ltable.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ltable.c b/ltable.c index 92c165adac..1f09369a4f 100644 --- a/ltable.c +++ b/ltable.c @@ -1,5 +1,5 @@ /* -** $Id: ltable.c,v 2.117 2015/11/19 19:16:22 roberto Exp roberto $ +** $Id: ltable.c,v 2.118 2016/11/07 12:38:35 roberto Exp roberto $ ** Lua tables (hash) ** See Copyright Notice in lua.h */ @@ -496,7 +496,7 @@ TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) { */ const TValue *luaH_getint (Table *t, lua_Integer key) { /* (1 <= key && key <= t->sizearray) */ - if (l_castS2U(key) - 1 < t->sizearray) + if (l_castS2U(key) - 1u < t->sizearray) return &t->array[key - 1]; else { Node *n = hashint(t, key); From 7184f6343a0074d02f6f0e35654cfa55427710d3 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 10 May 2017 14:32:19 -0300 Subject: [PATCH 0037/1145] more integer fast tracks (for OP_LT, OP_LE, OP_SETTABLE, and OP_GETTABLE) --- lvm.c | 68 ++++++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 51 insertions(+), 17 deletions(-) diff --git a/lvm.c b/lvm.c index 1a7531e178..27d467f30c 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.277 2017/05/05 17:16:11 roberto Exp roberto $ +** $Id: lvm.c,v 2.278 2017/05/08 16:08:01 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -118,10 +118,9 @@ int luaV_tointeger (const TValue *obj, lua_Integer *p, int mode) { /* -** Try to convert a 'for' limit to an integer, preserving the -** semantics of the loop. -** (The following explanation assumes a non-negative step; it is valid -** for negative steps mutatis mutandis.) +** Try to convert a 'for' limit to an integer, preserving the semantics +** of the loop. (The following explanation assumes a non-negative step; +** it is valid for negative steps mutatis mutandis.) ** If the limit can be converted to an integer, rounding down, that is ** it. ** Otherwise, check whether the limit can be converted to a number. If @@ -807,6 +806,19 @@ void luaV_finishOp (lua_State *L) { Protect(luaV_finishset(L,t,k,v,slot)); } +/* +** Predicate for integer fast track in GET/SETTABLE. +** (Strings are usually constant and therefore coded with +** GET/SETFIELD). Check that index is an integer, "table" is +** a table, index is in the array part, and value is not nil +** (otherwise it will need metamethods). Use 'n' and 't' for +** for caching the integer and table values. +*/ +#define fastintindex(tbl,idx,t,n) \ + (ttisinteger(idx) && ttistable(tbl) && \ + (n = l_castS2U(ivalue(idx)) - 1u, t = hvalue(tbl), \ + n < t->sizearray) && !ttisnil(&t->array[n])) + void luaV_execute (lua_State *L) { CallInfo *ci = L->ci; @@ -889,7 +901,12 @@ void luaV_execute (lua_State *L) { vmcase(OP_GETTABLE) { StkId rb = RB(i); TValue *rc = RC(i); - gettableProtected(L, rb, rc, ra); + Table *t; lua_Unsigned n; + if (fastintindex(rb, rc, t, n)) { + setobj2s(L, ra, &t->array[n]); + } + else + gettableProtected(L, rb, rc, ra); vmbreak; } vmcase(OP_GETI) { @@ -930,7 +947,12 @@ void luaV_execute (lua_State *L) { vmcase(OP_SETTABLE) { TValue *rb = RB(i); TValue *rc = RKC(i); - settableProtected(L, ra, rb, rc); + Table *t; lua_Unsigned n; + if (fastintindex(ra, rb, t, n)) { + setobj2t(L, &t->array[n], rc); + } + else + settableProtected(L, ra, rb, rc); vmbreak; } vmcase(OP_SETI) { @@ -1205,21 +1227,33 @@ void luaV_execute (lua_State *L) { vmbreak; } vmcase(OP_LT) { - Protect( - if (luaV_lessthan(L, RKB(i), RKC(i)) != GETARG_A(i)) - pc++; - else - donextjump(ci); + TValue *rb = RKB(i); + TValue *rc = RKC(i); + int res; + if (ttisinteger(rb) && ttisinteger(rc)) + res = (ivalue(rb) < ivalue(rc)); + else Protect( + res = luaV_lessthan(L, rb, rc); ) + if (res != GETARG_A(i)) + pc++; + else + donextjump(ci); vmbreak; } vmcase(OP_LE) { - Protect( - if (luaV_lessequal(L, RKB(i), RKC(i)) != GETARG_A(i)) - pc++; - else - donextjump(ci); + TValue *rb = RKB(i); + TValue *rc = RKC(i); + int res; + if (ttisinteger(rb) && ttisinteger(rc)) + res = (ivalue(rb) <= ivalue(rc)); + else Protect( + res = luaV_lessequal(L, rb, rc); ) + if (res != GETARG_A(i)) + pc++; + else + donextjump(ci); vmbreak; } vmcase(OP_TEST) { From 7647d5d13d016f114dac4be0b9da62d502eab400 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 11 May 2017 15:57:46 -0300 Subject: [PATCH 0038/1145] revamp of fast track for table access (table set uses the same macros as table get + new macro for integer keys) --- lapi.c | 41 +++++++++++++++++---------- lvm.c | 87 ++++++++++++++++++++++++---------------------------------- lvm.h | 47 +++++++++++++------------------ 3 files changed, 81 insertions(+), 94 deletions(-) diff --git a/lapi.c b/lapi.c index 2be5263167..facf9541d0 100644 --- a/lapi.c +++ b/lapi.c @@ -1,5 +1,5 @@ /* -** $Id: lapi.c,v 2.264 2017/04/20 18:22:44 roberto Exp roberto $ +** $Id: lapi.c,v 2.265 2017/04/24 16:59:26 roberto Exp roberto $ ** Lua API ** See Copyright Notice in lua.h */ @@ -609,10 +609,15 @@ LUA_API int lua_getglobal (lua_State *L, const char *name) { LUA_API int lua_gettable (lua_State *L, int idx) { + const TValue *slot; StkId t; lua_lock(L); t = index2addr(L, idx); - luaV_gettable(L, t, L->top - 1, L->top - 1); + if (luaV_fastget(L, t, L->top - 1, slot, luaH_get)) { + setobj2s(L, L->top - 1, slot); + } + else + luaV_finishget(L, t, L->top - 1, L->top - 1, slot); lua_unlock(L); return ttnov(L->top - 1); } @@ -629,15 +634,15 @@ LUA_API int lua_geti (lua_State *L, int idx, lua_Integer n) { const TValue *slot; lua_lock(L); t = index2addr(L, idx); - if (luaV_fastget(L, t, n, slot, luaH_getint)) { + if (luaV_fastgeti(L, t, n, slot)) { setobj2s(L, L->top, slot); - api_incr_top(L); } else { - setivalue(L->top, n); - api_incr_top(L); - luaV_finishget(L, t, L->top - 1, L->top - 1, slot); + TValue aux; + setivalue(&aux, n); + luaV_finishget(L, t, &aux, L->top, slot); } + api_incr_top(L); lua_unlock(L); return ttnov(L->top - 1); } @@ -743,8 +748,10 @@ static void auxsetstr (lua_State *L, const TValue *t, const char *k) { const TValue *slot; TString *str = luaS_new(L, k); api_checknelems(L, 1); - if (luaV_fastset(L, t, str, slot, luaH_getstr, L->top - 1)) + if (luaV_fastget(L, t, str, slot, luaH_getstr)) { + luaV_finishfastset(L, t, slot, L->top - 1); L->top--; /* pop value */ + } else { setsvalue2s(L, L->top, str); /* push 'str' (to make it a TValue) */ api_incr_top(L); @@ -764,10 +771,14 @@ LUA_API void lua_setglobal (lua_State *L, const char *name) { LUA_API void lua_settable (lua_State *L, int idx) { StkId t; + const TValue *slot; lua_lock(L); api_checknelems(L, 2); t = index2addr(L, idx); - luaV_settable(L, t, L->top - 2, L->top - 1); + if (luaV_fastget(L, t, L->top - 2, slot, luaH_get)) + luaV_finishfastset(L, t, slot, L->top - 1); + else + luaV_finishset(L, t, L->top - 2, L->top - 1, slot); L->top -= 2; /* pop index and value */ lua_unlock(L); } @@ -785,14 +796,14 @@ LUA_API void lua_seti (lua_State *L, int idx, lua_Integer n) { lua_lock(L); api_checknelems(L, 1); t = index2addr(L, idx); - if (luaV_fastset(L, t, n, slot, luaH_getint, L->top - 1)) - L->top--; /* pop value */ + if (luaV_fastgeti(L, t, n, slot)) + luaV_finishfastset(L, t, slot, L->top - 1); else { - setivalue(L->top, n); - api_incr_top(L); - luaV_finishset(L, t, L->top - 1, L->top - 2, slot); - L->top -= 2; /* pop value and key */ + TValue aux; + setivalue(&aux, n); + luaV_finishset(L, t, &aux, L->top - 1, slot); } + L->top--; /* pop value */ lua_unlock(L); } diff --git a/lvm.c b/lvm.c index 27d467f30c..9a60bb5b97 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.278 2017/05/08 16:08:01 roberto Exp roberto $ +** $Id: lvm.c,v 2.279 2017/05/10 17:32:19 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -182,7 +182,7 @@ void luaV_finishget (lua_State *L, const TValue *t, TValue *key, StkId val, return; } t = tm; /* else try to access 'tm[key]' */ - if (luaV_fastget(L,t,key,slot,luaH_get)) { /* fast track? */ + if (luaV_fastget(L, t, key, slot, luaH_get)) { /* fast track? */ setobj2s(L, val, slot); /* done */ return; } @@ -196,7 +196,7 @@ void luaV_finishget (lua_State *L, const TValue *t, TValue *key, StkId val, ** Finish a table assignment 't[key] = val'. ** If 'slot' is NULL, 't' is not a table. Otherwise, 'slot' points ** to the entry 't[key]', or to 'luaO_nilobject' if there is no such -** entry. (The value at 'slot' must be nil, otherwise 'luaV_fastset' +** entry. (The value at 'slot' must be nil, otherwise 'luaV_fastget' ** would have done the job.) */ void luaV_finishset (lua_State *L, const TValue *t, TValue *key, @@ -229,9 +229,11 @@ void luaV_finishset (lua_State *L, const TValue *t, TValue *key, return; } t = tm; /* else repeat assignment over 'tm' */ - if (luaV_fastset(L, t, key, slot, luaH_get, val)) + if (luaV_fastget(L, t, key, slot, luaH_get)) { + luaV_finishfastset(L, t, slot, val); return; /* done */ - /* else loop */ + } + /* else 'return luaV_finishset(L, t, key, val, slot)' (loop) */ } luaG_runerror(L, "'__newindex' chain too long; possible loop"); } @@ -791,35 +793,6 @@ void luaV_finishOp (lua_State *L) { #define vmbreak break -/* -** copy of 'luaV_gettable', but protecting the call to potential -** metamethod (which can reallocate the stack) -*/ -#define gettableProtected(L,t,k,v) { const TValue *slot; \ - if (luaV_fastget(L,t,k,slot,luaH_get)) { setobj2s(L, v, slot); } \ - else Protect(luaV_finishget(L,t,k,v,slot)); } - - -/* same for 'luaV_settable' */ -#define settableProtected(L,t,k,v) { const TValue *slot; \ - if (!luaV_fastset(L,t,k,slot,luaH_get,v)) \ - Protect(luaV_finishset(L,t,k,v,slot)); } - - -/* -** Predicate for integer fast track in GET/SETTABLE. -** (Strings are usually constant and therefore coded with -** GET/SETFIELD). Check that index is an integer, "table" is -** a table, index is in the array part, and value is not nil -** (otherwise it will need metamethods). Use 'n' and 't' for -** for caching the integer and table values. -*/ -#define fastintindex(tbl,idx,t,n) \ - (ttisinteger(idx) && ttistable(tbl) && \ - (n = l_castS2U(ivalue(idx)) - 1u, t = hvalue(tbl), \ - n < t->sizearray) && !ttisnil(&t->array[n])) - - void luaV_execute (lua_State *L) { CallInfo *ci = L->ci; LClosure *cl; @@ -899,21 +872,24 @@ void luaV_execute (lua_State *L) { vmbreak; } vmcase(OP_GETTABLE) { + const TValue *slot; StkId rb = RB(i); TValue *rc = RC(i); - Table *t; lua_Unsigned n; - if (fastintindex(rb, rc, t, n)) { - setobj2s(L, ra, &t->array[n]); + lua_Unsigned n; + if (ttisinteger(rc) /* fast track for integers? */ + ? (n = ivalue(rc), luaV_fastgeti(L, rb, n, slot)) + : luaV_fastget(L, rb, rc, slot, luaH_get)) { + setobj2s(L, ra, slot); } else - gettableProtected(L, rb, rc, ra); + Protect(luaV_finishget(L, rb, rc, ra, slot)); vmbreak; } vmcase(OP_GETI) { const TValue *slot; StkId rb = RB(i); int c = GETARG_C(i); - if (luaV_fastget(L, rb, c, slot, luaH_getint)) { + if (luaV_fastgeti(L, rb, c, slot)) { setobj2s(L, ra, slot); } else { @@ -940,26 +916,32 @@ void luaV_execute (lua_State *L) { TValue *rb = KB(i); TValue *rc = RKC(i); TString *key = tsvalue(rb); /* key must be a string */ - if (!luaV_fastset(L, upval, key, slot, luaH_getstr, rc)) + if (luaV_fastget(L, upval, key, slot, luaH_getstr)) + luaV_finishfastset(L, upval, slot, rc); + else Protect(luaV_finishset(L, upval, rb, rc, slot)); vmbreak; } vmcase(OP_SETTABLE) { - TValue *rb = RB(i); - TValue *rc = RKC(i); - Table *t; lua_Unsigned n; - if (fastintindex(ra, rb, t, n)) { - setobj2t(L, &t->array[n], rc); - } + const TValue *slot; + TValue *rb = RB(i); /* key (table is in 'ra') */ + TValue *rc = RKC(i); /* value */ + lua_Unsigned n; + if (ttisinteger(rb) /* fast track for integers? */ + ? (n = ivalue(rb), luaV_fastgeti(L, ra, n, slot)) + : luaV_fastget(L, ra, rb, slot, luaH_get)) + luaV_finishfastset(L, ra, slot, rc); else - settableProtected(L, ra, rb, rc); + Protect(luaV_finishset(L, ra, rb, rc, slot)); vmbreak; } vmcase(OP_SETI) { const TValue *slot; int c = GETARG_B(i); TValue *rc = RKC(i); - if (!luaV_fastset(L, ra, c, slot, luaH_getint, rc)) { + if (luaV_fastgeti(L, ra, c, slot)) + luaV_finishfastset(L, ra, slot, rc); + else { TValue key; setivalue(&key, c); Protect(luaV_finishset(L, ra, &key, rc, slot)); @@ -971,7 +953,9 @@ void luaV_execute (lua_State *L) { TValue *rb = KB(i); TValue *rc = RKC(i); TString *key = tsvalue(rb); /* key must be a string */ - if (!luaV_fastset(L, ra, key, slot, luaH_getstr, rc)) + if (luaV_fastget(L, ra, key, slot, luaH_getstr)) + luaV_finishfastset(L, ra, slot, rc); + else Protect(luaV_finishset(L, ra, rb, rc, slot)); vmbreak; } @@ -1427,8 +1411,9 @@ void luaV_execute (lua_State *L) { if (last > h->sizearray) /* needs more space? */ luaH_resizearray(L, h, last); /* preallocate it at once */ for (; n > 0; n--) { - TValue *val = ra+n; - luaH_setint(L, h, last--, val); + TValue *val = ra + n; + setobj2t(L, &h->array[last - 1], val); + last--; luaC_barrierback(L, h, val); } L->top = ci->top; /* correct top (in case of previous open call) */ diff --git a/lvm.h b/lvm.h index ca75a3386e..17be5842f8 100644 --- a/lvm.h +++ b/lvm.h @@ -1,5 +1,5 @@ /* -** $Id: lvm.h,v 2.40 2016/01/05 16:07:21 roberto Exp roberto $ +** $Id: lvm.h,v 2.41 2016/12/22 13:08:50 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -50,10 +50,10 @@ /* ** fast track for 'gettable': if 't' is a table and 't[k]' is not nil, -** return 1 with 'slot' pointing to 't[k]' (final result). Otherwise, -** return 0 (meaning it will have to check metamethod) with 'slot' -** pointing to a nil 't[k]' (if 't' is a table) or NULL (otherwise). -** 'f' is the raw get function to use. +** return 1 with 'slot' pointing to 't[k]' (position of final result). +** Otherwise, return 0 (meaning it will have to check metamethod) +** with 'slot' pointing to a nil 't[k]' (if 't' is a table) or NULL +** (otherwise). 'f' is the raw get function to use. */ #define luaV_fastget(L,t,k,slot,f) \ (!ttistable(t) \ @@ -61,35 +61,26 @@ : (slot = f(hvalue(t), k), /* else, do raw access */ \ !ttisnil(slot))) /* result not nil? */ + /* -** standard implementation for 'gettable' +** Special case of 'luaV_fastget' for integers, inlining the fast case +** of 'luaH_getint'. */ -#define luaV_gettable(L,t,k,v) { const TValue *slot; \ - if (luaV_fastget(L,t,k,slot,luaH_get)) { setobj2s(L, v, slot); } \ - else luaV_finishget(L,t,k,v,slot); } +#define luaV_fastgeti(L,t,k,slot) \ + (!ttistable(t) \ + ? (slot = NULL, 0) /* not a table; 'slot' is NULL and result is 0 */ \ + : (slot = (l_castS2U(k) - 1u < hvalue(t)->sizearray) \ + ? &hvalue(t)->array[k - 1] : luaH_getint(hvalue(t), k), \ + !ttisnil(slot))) /* result not nil? */ /* -** Fast track for set table. If 't' is a table and 't[k]' is not nil, -** call GC barrier, do a raw 't[k]=v', and return true; otherwise, -** return false with 'slot' equal to NULL (if 't' is not a table) or -** 'nil'. (This is needed by 'luaV_finishget'.) Note that, if the macro -** returns true, there is no need to 'invalidateTMcache', because the -** call is not creating a new entry. +** Finish a fast set operation (when fast get succeeds). In that case, +** 'slot' points to the place to put the value. */ -#define luaV_fastset(L,t,k,slot,f,v) \ - (!ttistable(t) \ - ? (slot = NULL, 0) \ - : (slot = f(hvalue(t), k), \ - ttisnil(slot) ? 0 \ - : (luaC_barrierback(L, hvalue(t), v), \ - setobj2t(L, cast(TValue *,slot), v), \ - 1))) - - -#define luaV_settable(L,t,k,v) { const TValue *slot; \ - if (!luaV_fastset(L,t,k,slot,luaH_get,v)) \ - luaV_finishset(L,t,k,v,slot); } +#define luaV_finishfastset(L,t,slot,v) \ + (setobj2t(L, cast(TValue *,slot), v), luaC_barrierback(L, hvalue(t), v)) + From 5c8770f8969a73cf4ca503f54c2217f76de62e04 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Sat, 13 May 2017 10:04:33 -0300 Subject: [PATCH 0039/1145] back to old-style vararg system (with vararg table collecting extra arguments) --- ldebug.c | 21 +++------------------ ldo.c | 41 ++++++----------------------------------- ldo.h | 10 +++++++++- lparser.c | 11 ++++++++++- ltm.c | 40 +++++++++++++++++++++++++++++++++++++++- ltm.h | 5 ++++- lvm.c | 18 +++--------------- 7 files changed, 74 insertions(+), 72 deletions(-) diff --git a/ldebug.c b/ldebug.c index ead1679449..776b05ebad 100644 --- a/ldebug.c +++ b/ldebug.c @@ -1,5 +1,5 @@ /* -** $Id: ldebug.c,v 2.123 2017/04/28 20:57:45 roberto Exp roberto $ +** $Id: ldebug.c,v 2.124 2017/04/29 15:28:38 roberto Exp roberto $ ** Debug Interface ** See Copyright Notice in lua.h */ @@ -131,28 +131,13 @@ static const char *upvalname (Proto *p, int uv) { } -static const char *findvararg (CallInfo *ci, int n, StkId *pos) { - int nparams = clLvalue(ci->func)->p->numparams; - if (n >= cast_int(ci->u.l.base - ci->func) - nparams) - return NULL; /* no such vararg */ - else { - *pos = ci->func + nparams + n; - return "(*vararg)"; /* generic name for any vararg */ - } -} - - static const char *findlocal (lua_State *L, CallInfo *ci, int n, StkId *pos) { const char *name = NULL; StkId base; if (isLua(ci)) { - if (n < 0) /* access to vararg values? */ - return findvararg(ci, -n, pos); - else { - base = ci->u.l.base; - name = luaF_getlocalname(ci_func(ci)->p, n, currentpc(ci)); - } + base = ci->u.l.base; + name = luaF_getlocalname(ci_func(ci)->p, n, currentpc(ci)); } else base = ci->func + 1; diff --git a/ldo.c b/ldo.c index 1338751602..14d1adc6da 100644 --- a/ldo.c +++ b/ldo.c @@ -1,5 +1,5 @@ /* -** $Id: ldo.c,v 2.156 2016/09/20 16:37:45 roberto Exp roberto $ +** $Id: ldo.c,v 2.157 2016/12/13 15:52:21 roberto Exp roberto $ ** Stack and Call structure of Lua ** See Copyright Notice in lua.h */ @@ -290,23 +290,6 @@ static void callhook (lua_State *L, CallInfo *ci) { } -static StkId adjust_varargs (lua_State *L, Proto *p, int actual) { - int i; - int nfixargs = p->numparams; - StkId base, fixed; - /* move fixed parameters to final position */ - fixed = L->top - actual; /* first fixed argument */ - base = L->top; /* final position of first argument */ - for (i = 0; i < nfixargs && i < actual; i++) { - setobjs2s(L, L->top++, fixed + i); - setnilvalue(fixed + i); /* erase original copy (for GC) */ - } - for (; i < nfixargs; i++) - setnilvalue(L->top++); /* complete missing arguments */ - return base; -} - - /* ** Check whether __call metafield of 'func' is a function. If so, put ** it in stack below original 'func' so that 'luaD_precall' can call @@ -395,14 +378,6 @@ int luaD_poscall (lua_State *L, CallInfo *ci, StkId firstResult, int nres) { #define next_ci(L) (L->ci = (L->ci->next ? L->ci->next : luaE_extendCI(L))) -/* macro to check stack size, preserving 'p' */ -#define checkstackp(L,n,p) \ - luaD_checkstackaux(L, n, \ - ptrdiff_t t__ = savestack(L, p); /* save 'p' */ \ - luaC_checkGC(L), /* stack grow uses memory */ \ - p = restorestack(L, t__)) /* 'pos' part: restore 'p' */ - - /* ** Prepares a function call: checks the stack, creates a new CallInfo ** entry, fills in the relevant information, calls hook if needed. @@ -438,23 +413,19 @@ int luaD_precall (lua_State *L, StkId func, int nresults) { return 1; } case LUA_TLCL: { /* Lua function: prepare its call */ - StkId base; Proto *p = clLvalue(func)->p; int n = cast_int(L->top - func) - 1; /* number of real arguments */ int fsize = p->maxstacksize; /* frame size */ checkstackp(L, fsize, func); + for (; n < p->numparams - p->is_vararg; n++) + setnilvalue(L->top++); /* complete missing arguments */ if (p->is_vararg) - base = adjust_varargs(L, p, n); - else { /* non vararg function */ - for (; n < p->numparams; n++) - setnilvalue(L->top++); /* complete missing arguments */ - base = func + 1; - } + luaT_adjustvarargs(L, p, n); ci = next_ci(L); /* now 'enter' new function */ ci->nresults = nresults; ci->func = func; - ci->u.l.base = base; - L->top = ci->top = base + fsize; + ci->u.l.base = func + 1; + L->top = ci->top = func + 1 + fsize; lua_assert(ci->top <= L->stack_last); ci->u.l.savedpc = p->code; /* starting point */ ci->callstatus = CIST_LUA; diff --git a/ldo.h b/ldo.h index b2065cfa68..4717620f58 100644 --- a/ldo.h +++ b/ldo.h @@ -1,5 +1,5 @@ /* -** $Id: ldo.h,v 2.28 2015/11/23 11:29:43 roberto Exp roberto $ +** $Id: ldo.h,v 2.29 2015/12/21 13:02:14 roberto Exp roberto $ ** Stack and Call structure of Lua ** See Copyright Notice in lua.h */ @@ -33,6 +33,14 @@ #define restorestack(L,n) ((TValue *)((char *)L->stack + (n))) +/* macro to check stack size, preserving 'p' */ +#define checkstackp(L,n,p) \ + luaD_checkstackaux(L, n, \ + ptrdiff_t t__ = savestack(L, p); /* save 'p' */ \ + luaC_checkGC(L), /* stack grow uses memory */ \ + p = restorestack(L, t__)) /* 'pos' part: restore 'p' */ + + /* type of protected functions, to be ran by 'runprotected' */ typedef void (*Pfunc) (lua_State *L, void *ud); diff --git a/lparser.c b/lparser.c index af5891c2c3..37f84cce9e 100644 --- a/lparser.c +++ b/lparser.c @@ -1,5 +1,5 @@ /* -** $Id: lparser.c,v 2.157 2017/04/28 20:57:45 roberto Exp roberto $ +** $Id: lparser.c,v 2.158 2017/04/29 18:09:17 roberto Exp roberto $ ** Lua Parser ** See Copyright Notice in lua.h */ @@ -766,7 +766,12 @@ static void parlist (LexState *ls) { } case TK_DOTS: { /* param -> '...' */ luaX_next(ls); + if (testnext(ls, '=')) + new_localvar(ls, str_checkname(ls)); + else + new_localvarliteral(ls, "_ARG"); f->is_vararg = 1; /* declared vararg */ + nparams++; break; } default: luaX_syntaxerror(ls, " or '...' expected"); @@ -1622,6 +1627,10 @@ static void mainfunc (LexState *ls, FuncState *fs) { expdesc v; open_func(ls, fs, &bl); fs->f->is_vararg = 1; /* main function is always declared vararg */ + fs->f->numparams = 1; + new_localvarliteral(ls, "_ARG"); + adjustlocalvars(ls, 1); + luaK_reserveregs(fs, 1); /* reserve register for vararg */ init_exp(&v, VLOCAL, 0); /* create and... */ newupvalue(fs, ls->envn, &v); /* ...set environment upvalue */ luaX_next(ls); /* read first token */ diff --git a/ltm.c b/ltm.c index 0ef763131d..9da191f604 100644 --- a/ltm.c +++ b/ltm.c @@ -1,5 +1,5 @@ /* -** $Id: ltm.c,v 2.39 2017/04/11 18:41:09 roberto Exp roberto $ +** $Id: ltm.c,v 2.40 2017/05/08 15:57:23 roberto Exp roberto $ ** Tag methods ** See Copyright Notice in lua.h */ @@ -163,3 +163,41 @@ int luaT_callorderTM (lua_State *L, const TValue *p1, const TValue *p2, return !l_isfalse(L->top); } + +void luaT_adjustvarargs (lua_State *L, Proto *p, int actual) { + int i; + Table *vtab; + TValue nname; + int nfixparams = p->numparams - 1; /* number of fixed parameters */ + actual -= nfixparams; /* number of extra arguments */ + vtab = luaH_new(L); /* create vararg table */ + sethvalue(L, L->top, vtab); /* anchor it for resizing */ + L->top++; /* space ensured by caller */ + luaH_resize(L, vtab, actual, 1); + for (i = 0; i < actual; i++) /* put extra arguments into vararg table */ + setobj2n(L, &vtab->array[i], L->top - actual + i - 1); + setsvalue(L, &nname, luaS_newliteral(L, "n")); /* get field 'n' */ + setivalue(luaH_set(L, vtab, &nname), actual); /* store counter there */ + L->top -= actual; /* remove extra elements from the stack */ + sethvalue(L, L->top - 1, vtab); /* move table to new top */ +} + + +void luaT_getvarargs (lua_State *L, StkId t, StkId where, int wanted) { + if (!ttistable(t)) + luaG_runerror(L, "'vararg' parameter is not a table"); + else { + int i; + Table *h = hvalue(t); + if (wanted < 0) { /* get all? */ + const TValue *ns = luaH_getstr(h, luaS_newliteral(L, "n")); + int n = (ttisinteger(ns)) ? ivalue(ns) : 0; + wanted = n; + checkstackp(L, n, where); + L->top = where + n; + } + for (i = 0; i < wanted; i++) /* get what is available */ + setobj2s(L, where + i, luaH_getint(h, i + 1)); + return; + } +} diff --git a/ltm.h b/ltm.h index 535e188505..2dfb46c11d 100644 --- a/ltm.h +++ b/ltm.h @@ -1,5 +1,5 @@ /* -** $Id: ltm.h,v 2.22 2016/02/26 19:20:15 roberto Exp roberto $ +** $Id: ltm.h,v 2.23 2017/05/08 15:57:23 roberto Exp roberto $ ** Tag methods ** See Copyright Notice in lua.h */ @@ -69,6 +69,9 @@ LUAI_FUNC void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2, LUAI_FUNC int luaT_callorderTM (lua_State *L, const TValue *p1, const TValue *p2, TMS event); +LUAI_FUNC void luaT_adjustvarargs (lua_State *L, Proto *p, int actual); +LUAI_FUNC void luaT_getvarargs (lua_State *L, StkId t, StkId where, + int wanted); #endif diff --git a/lvm.c b/lvm.c index 9a60bb5b97..3053d929bc 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.279 2017/05/10 17:32:19 roberto Exp roberto $ +** $Id: lvm.c,v 2.280 2017/05/11 18:57:46 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -1433,20 +1433,8 @@ void luaV_execute (lua_State *L) { } vmcase(OP_VARARG) { int b = GETARG_B(i) - 1; /* required results */ - int j; - int n = cast_int(base - ci->func) - cl->p->numparams - 1; - if (n < 0) /* less arguments than parameters? */ - n = 0; /* no vararg arguments */ - if (b < 0) { /* B == 0? */ - b = n; /* get all var. arguments */ - Protect(luaD_checkstack(L, n)); - ra = RA(i); /* previous call may change the stack */ - L->top = ra + n; - } - for (j = 0; j < b && j < n; j++) - setobjs2s(L, ra + j, base - n + j); - for (; j < b; j++) /* complete required results with nil */ - setnilvalue(ra + j); + StkId vtab = base + cl->p->numparams - 1; /* vararg table */ + Protect(luaT_getvarargs(L, vtab, ra, b)); vmbreak; } vmcase(OP_EXTRAARG) { From 6d95de83c644da5f720cf468c38df9782f1c890d Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Sat, 13 May 2017 10:54:47 -0300 Subject: [PATCH 0040/1145] no more field 'base' in CallInfo (base is always equal to 'func + 1', with old/new vararg implementation) --- ldebug.c | 11 ++++++----- ldo.c | 5 +---- lstate.h | 3 +-- lvm.c | 23 +++++++++++------------ 4 files changed, 19 insertions(+), 23 deletions(-) diff --git a/ldebug.c b/ldebug.c index 776b05ebad..4193cda8c6 100644 --- a/ldebug.c +++ b/ldebug.c @@ -1,5 +1,5 @@ /* -** $Id: ldebug.c,v 2.124 2017/04/29 15:28:38 roberto Exp roberto $ +** $Id: ldebug.c,v 2.125 2017/05/13 13:04:33 roberto Exp roberto $ ** Debug Interface ** See Copyright Notice in lua.h */ @@ -136,7 +136,7 @@ static const char *findlocal (lua_State *L, CallInfo *ci, int n, const char *name = NULL; StkId base; if (isLua(ci)) { - base = ci->u.l.base; + base = ci->func + 1; name = luaF_getlocalname(ci_func(ci)->p, n, currentpc(ci)); } else @@ -562,8 +562,9 @@ static const char *funcnamefromcode (lua_State *L, CallInfo *ci, ** checks are ISO C and ensure a correct result. */ static int isinstack (CallInfo *ci, const TValue *o) { - ptrdiff_t i = o - ci->u.l.base; - return (0 <= i && i < (ci->top - ci->u.l.base) && ci->u.l.base + i == o); + StkId base = ci->func + 1; + ptrdiff_t i = o - base; + return (0 <= i && i < (ci->top - base) && base + i == o); } @@ -594,7 +595,7 @@ static const char *varinfo (lua_State *L, const TValue *o) { kind = getupvalname(ci, o, &name); /* check whether 'o' is an upvalue */ if (!kind && isinstack(ci, o)) /* no? try a register */ kind = getobjname(ci_func(ci)->p, currentpc(ci), - cast_int(o - ci->u.l.base), &name); + cast_int(o - (ci->func + 1)), &name); } return (kind) ? luaO_pushfstring(L, " (%s '%s')", kind, name) : ""; } diff --git a/ldo.c b/ldo.c index 14d1adc6da..5e28fda63d 100644 --- a/ldo.c +++ b/ldo.c @@ -1,5 +1,5 @@ /* -** $Id: ldo.c,v 2.157 2016/12/13 15:52:21 roberto Exp roberto $ +** $Id: ldo.c,v 2.158 2017/05/13 12:57:20 roberto Exp roberto $ ** Stack and Call structure of Lua ** See Copyright Notice in lua.h */ @@ -164,8 +164,6 @@ static void correctstack (lua_State *L, TValue *oldstack) { for (ci = L->ci; ci != NULL; ci = ci->previous) { ci->top = (ci->top - oldstack) + L->stack; ci->func = (ci->func - oldstack) + L->stack; - if (isLua(ci)) - ci->u.l.base = (ci->u.l.base - oldstack) + L->stack; } } @@ -424,7 +422,6 @@ int luaD_precall (lua_State *L, StkId func, int nresults) { ci = next_ci(L); /* now 'enter' new function */ ci->nresults = nresults; ci->func = func; - ci->u.l.base = func + 1; L->top = ci->top = func + 1 + fsize; lua_assert(ci->top <= L->stack_last); ci->u.l.savedpc = p->code; /* starting point */ diff --git a/lstate.h b/lstate.h index 533d6ad6fc..d2dabc4dc6 100644 --- a/lstate.h +++ b/lstate.h @@ -1,5 +1,5 @@ /* -** $Id: lstate.h,v 2.139 2017/04/24 16:59:26 roberto Exp roberto $ +** $Id: lstate.h,v 2.140 2017/05/04 13:32:01 roberto Exp roberto $ ** Global State ** See Copyright Notice in lua.h */ @@ -96,7 +96,6 @@ typedef struct CallInfo { struct CallInfo *previous, *next; /* dynamic call link */ union { struct { /* only for Lua functions */ - StkId base; /* base for this function */ const Instruction *savedpc; } l; struct { /* only for C functions */ diff --git a/lvm.c b/lvm.c index 3053d929bc..284646bbb3 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.280 2017/05/11 18:57:46 roberto Exp roberto $ +** $Id: lvm.c,v 2.281 2017/05/13 12:57:20 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -659,7 +659,7 @@ static void pushclosure (lua_State *L, Proto *p, UpVal **encup, StkId base, */ void luaV_finishOp (lua_State *L) { CallInfo *ci = L->ci; - StkId base = ci->u.l.base; + StkId base = ci->func + 1; Instruction inst = *(ci->u.l.savedpc - 1); /* interrupted instruction */ OpCode op = GET_OPCODE(inst); switch (op) { /* finish its execution */ @@ -696,7 +696,7 @@ void luaV_finishOp (lua_State *L) { luaV_concat(L, total); /* concat them (may yield again) */ } /* move final result to final position */ - setobj2s(L, ci->u.l.base + GETARG_A(inst), L->top - 1); + setobj2s(L, ci->func + 1 + GETARG_A(inst), L->top - 1); L->top = ci->top; /* restore top */ break; } @@ -753,7 +753,7 @@ void luaV_finishOp (lua_State *L) { */ #define dojump(ci,i,e) \ { int a = GETARG_A(i); \ - if (a != 0) luaF_close(L, ci->u.l.base + a - 1); \ + if (a != 0) luaF_close(L, ci->func + a); \ pc += GETARG_sBx(i) + e; updatemask(L); } @@ -772,7 +772,7 @@ void luaV_finishOp (lua_State *L) { ** stack, and change the hooks. */ #define Protect(code) \ - { savepc(L); {code;}; base = ci->u.l.base; updatemask(L); } + { savepc(L); {code;}; base = ci->func + 1; updatemask(L); } #define checkGC(L,c) \ @@ -797,7 +797,7 @@ void luaV_execute (lua_State *L) { CallInfo *ci = L->ci; LClosure *cl; TValue *k; - StkId base; /* local copy of 'ci->u.l.base' */ + StkId base; /* local copy of 'ci->func + 1' */ int mask; /* local copy of 'L->hookmask & (LUA_MASKLINE | LUA_MASKCOUNT)' */ const Instruction *pc; /* local copy of 'ci->u.l.savedpc' */ ci->callstatus |= CIST_FRESH; /* fresh invocation of 'luaV_execute" */ @@ -806,14 +806,14 @@ void luaV_execute (lua_State *L) { cl = clLvalue(ci->func); /* local reference to function's closure */ k = cl->p->k; /* local reference to function's constant table */ updatemask(L); - base = ci->u.l.base; + base = ci->func + 1; pc = ci->u.l.savedpc; /* main loop of interpreter */ for (;;) { Instruction i; StkId ra; vmfetch(); - lua_assert(base == ci->u.l.base); + lua_assert(base == ci->func + 1); lua_assert(base <= L->top && L->top < L->stack + L->stacksize); vmdispatch (GET_OPCODE(i)) { vmcase(OP_MOVE) { @@ -1288,19 +1288,18 @@ void luaV_execute (lua_State *L) { StkId nfunc = nci->func; /* called function */ StkId ofunc = oci->func; /* caller function */ /* last stack slot filled by 'precall' */ - StkId lim = nci->u.l.base + getproto(nfunc)->numparams; + StkId lim = nci->func + 1 + getproto(nfunc)->numparams; int aux; /* close all upvalues from previous call */ - if (cl->p->sizep > 0) luaF_close(L, oci->u.l.base); + if (cl->p->sizep > 0) luaF_close(L, oci->func + 1); /* move new frame into old one */ for (aux = 0; nfunc + aux < lim; aux++) setobjs2s(L, ofunc + aux, nfunc + aux); - oci->u.l.base = ofunc + (nci->u.l.base - nfunc); /* correct base */ oci->top = L->top = ofunc + (L->top - nfunc); /* correct top */ oci->u.l.savedpc = nci->u.l.savedpc; oci->callstatus |= CIST_TAIL; /* function was tail called */ ci = L->ci = oci; /* remove new frame */ - lua_assert(L->top == oci->u.l.base + getproto(ofunc)->maxstacksize); + lua_assert(L->top == oci->func + 1 + getproto(ofunc)->maxstacksize); goto newframe; /* restart luaV_execute over new Lua function */ } vmbreak; From 3d879fbc5df8076aa02bc0cec48e10b6a9ef0665 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 16 May 2017 16:07:08 -0300 Subject: [PATCH 0041/1145] reimplementation of 'luaH_getn', trying to handle numeric limits properly. --- ltable.c | 77 ++++++++++++++++++++++++++++++++------------------------ 1 file changed, 44 insertions(+), 33 deletions(-) diff --git a/ltable.c b/ltable.c index 1f09369a4f..c9a498e510 100644 --- a/ltable.c +++ b/ltable.c @@ -1,5 +1,5 @@ /* -** $Id: ltable.c,v 2.118 2016/11/07 12:38:35 roberto Exp roberto $ +** $Id: ltable.c,v 2.119 2017/05/09 14:39:46 roberto Exp roberto $ ** Lua tables (hash) ** See Copyright Notice in lua.h */ @@ -224,12 +224,10 @@ static unsigned int computesizes (unsigned int nums[], unsigned int *pna) { unsigned int optimal = 0; /* optimal size for array part */ /* loop while keys can fill more than half of total size */ for (i = 0, twotoi = 1; *pna > twotoi / 2; i++, twotoi *= 2) { - if (nums[i] > 0) { - a += nums[i]; - if (a > twotoi/2) { /* more than half elements present? */ - optimal = twotoi; /* optimal size (till now) */ - na = a; /* all elements up to 'optimal' will go to array part */ - } + a += nums[i]; + if (a > twotoi/2) { /* more than half elements present? */ + optimal = twotoi; /* optimal size (till now) */ + na = a; /* all elements up to 'optimal' will go to array part */ } } lua_assert((optimal == 0 || optimal / 2 < na) && na <= optimal); @@ -610,23 +608,31 @@ void luaH_setint (lua_State *L, Table *t, lua_Integer key, TValue *value) { } -static int unbound_search (Table *t, unsigned int j) { - unsigned int i = j; /* i is zero or a present index */ - j++; - /* find 'i' and 'j' such that i is present and j is not */ - while (!ttisnil(luaH_getint(t, j))) { - i = j; - if (j > cast(unsigned int, MAX_INT)/2) { /* overflow? */ - /* table was built with bad purposes: resort to linear search */ - i = 1; - while (!ttisnil(luaH_getint(t, i))) i++; - return i - 1; - } - j *= 2; +/* +** Try to find a boundary in the hash part of table 't'. From the +** caller, we know that 'i' is zero or present. We need to find an +** upper bound (an absent index larger than 'i') to do a binary search +** for a boundary. We try 'max', a number larger than the total number +** of keys in the table. (Given the size of the array elements, 'max' +** computation cannot overflow a 'size_t'.) If 'max' does not fit in a +** lua_Integer or it is present in the table, we try LUA_MAXINTEGER. If +** LUA_MAXINTEGER is present, it is a boundary, so we are done. Otherwise, +** we are left with a 'j' that is within the size of lua_Integers and +** absent, so can do the binary search. +*/ +static lua_Unsigned hash_search (Table *t, lua_Unsigned i) { + lua_Unsigned j; + size_t max = (cast(size_t, i) + sizenode(t) + 10) * 2; + if (max <= l_castS2U(LUA_MAXINTEGER) && ttisnil(luaH_getint(t, max))) + j = max; + else { + j = LUA_MAXINTEGER; + if (!ttisnil(luaH_getint(t, j))) /* weird case? */ + return j; /* well, that is a boundary... */ } - /* now do a binary search between them */ - while (j - i > 1) { - unsigned int m = (i+j)/2; + /* now, 'i' is zero or present and 'j' is absent */ + while (j - i > 1u) { /* do a binary search between them */ + size_t m = (i + j) / 2; if (ttisnil(luaH_getint(t, m))) j = m; else i = m; } @@ -635,25 +641,30 @@ static int unbound_search (Table *t, unsigned int j) { /* -** Try to find a boundary in table 't'. A 'boundary' is an integer index -** such that t[i] is non-nil and t[i+1] is nil (and 0 if t[1] is nil). +** Try to find a boundary in table 't'. (A 'boundary' is an integer index +** such that t[i] is non-nil and t[i+1] is nil (or 0 if t[1] is nil).) +** First, try the array part: if there is an array part and its last +** element is nil, there must be a boundary there; a binary search +** finds that boundary. Otherwise, if the hash part is empty or does not +** contain 'j + 1', 'j' is a boundary. Othersize, call 'hash_search' +** to find a boundary in the hash part. */ -int luaH_getn (Table *t) { +lua_Unsigned luaH_getn (Table *t) { unsigned int j = t->sizearray; if (j > 0 && ttisnil(&t->array[j - 1])) { - /* there is a boundary in the array part: (binary) search for it */ unsigned int i = 0; - while (j - i > 1) { - unsigned int m = (i+j)/2; + while (j - i > 1u) { /* binary search */ + unsigned int m = (i + j) / 2; if (ttisnil(&t->array[m - 1])) j = m; else i = m; } return i; } - /* else must find a boundary in hash part */ - else if (isdummy(t)) /* hash part is empty? */ - return j; /* that is easy... */ - else return unbound_search(t, j); + /* 'j' is zero or present in table */ + else if (isdummy(t) || ttisnil(luaH_getint(t, l_castU2S(j + 1)))) + return j; /* 'j + 1' is absent... */ + else + return hash_search(t, j); } From 49f7aab62af7e5d5b46c551c1620a8a89f448f7e Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 18 May 2017 09:34:58 -0300 Subject: [PATCH 0042/1145] 'lua_rawlen' returns 'lua_Unsigned' instead of 'size_t'. (Real length of strings and userdata are limited by Lua integers, but table length is hard to compute limiting it to 'size_t'.) --- lapi.c | 4 ++-- lua.h | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lapi.c b/lapi.c index facf9541d0..e7d59b45c9 100644 --- a/lapi.c +++ b/lapi.c @@ -1,5 +1,5 @@ /* -** $Id: lapi.c,v 2.265 2017/04/24 16:59:26 roberto Exp roberto $ +** $Id: lapi.c,v 2.266 2017/05/11 18:57:46 roberto Exp roberto $ ** Lua API ** See Copyright Notice in lua.h */ @@ -389,7 +389,7 @@ LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) { } -LUA_API size_t lua_rawlen (lua_State *L, int idx) { +LUA_API lua_Unsigned lua_rawlen (lua_State *L, int idx) { StkId o = index2addr(L, idx); switch (ttype(o)) { case LUA_TSHRSTR: return tsvalue(o)->shrlen; diff --git a/lua.h b/lua.h index d9962cec57..735a2599e9 100644 --- a/lua.h +++ b/lua.h @@ -1,5 +1,5 @@ /* -** $Id: lua.h,v 1.332 2016/12/22 15:51:20 roberto Exp roberto $ +** $Id: lua.h,v 1.333 2017/02/23 21:07:34 roberto Exp roberto $ ** Lua - A Scripting Language ** Lua.org, PUC-Rio, Brazil (http://www.lua.org) ** See Copyright Notice at the end of this file @@ -182,7 +182,7 @@ LUA_API lua_Number (lua_tonumberx) (lua_State *L, int idx, int *isnum); LUA_API lua_Integer (lua_tointegerx) (lua_State *L, int idx, int *isnum); LUA_API int (lua_toboolean) (lua_State *L, int idx); LUA_API const char *(lua_tolstring) (lua_State *L, int idx, size_t *len); -LUA_API size_t (lua_rawlen) (lua_State *L, int idx); +LUA_API lua_Unsigned (lua_rawlen) (lua_State *L, int idx); LUA_API lua_CFunction (lua_tocfunction) (lua_State *L, int idx); LUA_API void *(lua_touserdata) (lua_State *L, int idx); LUA_API lua_State *(lua_tothread) (lua_State *L, int idx); From 92b3deaffa642f331326d54a6b93d80240b3af60 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 18 May 2017 16:34:39 -0300 Subject: [PATCH 0043/1145] details in OP_CALL + comments --- lvm.c | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/lvm.c b/lvm.c index 284646bbb3..73a58e938a 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.281 2017/05/13 12:57:20 roberto Exp roberto $ +** $Id: lvm.c,v 2.282 2017/05/13 13:54:47 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -1260,12 +1260,15 @@ void luaV_execute (lua_State *L) { vmcase(OP_CALL) { int b = GETARG_B(i); int nresults = GETARG_C(i) - 1; - if (b != 0) L->top = ra+b; /* else previous instruction set top */ - savepc(L); - if (luaD_precall(L, ra, nresults)) { /* C function? */ - if (nresults >= 0) - L->top = ci->top; /* adjust results */ - Protect((void)0); /* update 'base' */ + int isC; + if (b != 0) /* fixed number of arguments? */ + L->top = ra + b; /* top signals number of arguments */ + /* else previous instruction set top */ + Protect(isC = luaD_precall(L, ra, nresults)); + if (isC) { /* C function? */ + if (nresults >= 0) /* fixed number of results? */ + L->top = ci->top; /* correct top */ + /* else leave top for next instruction */ } else { /* Lua function */ ci = L->ci; @@ -1283,8 +1286,8 @@ void luaV_execute (lua_State *L) { } else { /* tail call: put called frame (n) in place of caller one (o) */ - CallInfo *nci = L->ci; /* called frame */ - CallInfo *oci = nci->previous; /* caller frame */ + CallInfo *nci = L->ci; /* called frame (new) */ + CallInfo *oci = nci->previous; /* caller frame (old) */ StkId nfunc = nci->func; /* called function */ StkId ofunc = oci->func; /* caller function */ /* last stack slot filled by 'precall' */ From de74289049acf199d422feaf00605bb6e1fbaf72 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 18 May 2017 16:44:19 -0300 Subject: [PATCH 0044/1145] table field names for dedicated opcodes can be restricted to small strings for slightly faster access --- lcode.c | 6 +++--- lvm.c | 10 +++++----- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/lcode.c b/lcode.c index a154c339f8..2d39600024 100644 --- a/lcode.c +++ b/lcode.c @@ -1,5 +1,5 @@ /* -** $Id: lcode.c,v 2.117 2017/04/26 17:46:52 roberto Exp roberto $ +** $Id: lcode.c,v 2.118 2017/04/28 20:57:45 roberto Exp roberto $ ** Code generator for Lua ** See Copyright Notice in lua.h */ @@ -978,11 +978,11 @@ static void codenot (FuncState *fs, expdesc *e) { /* -** Check whether expression 'e' is a literal string +** Check whether expression 'e' is a small literal string */ static int isKstr (FuncState *fs, expdesc *e) { return (e->k == VK && !hasjumps(e) && e->u.info <= MAXARG_C && - ttisstring(&fs->f->k[e->u.info])); + ttisshrstring(&fs->f->k[e->u.info])); } diff --git a/lvm.c b/lvm.c index 73a58e938a..453ba2d4d2 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.282 2017/05/13 13:54:47 roberto Exp roberto $ +** $Id: lvm.c,v 2.283 2017/05/18 19:34:39 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -865,7 +865,7 @@ void luaV_execute (lua_State *L) { TValue *upval = cl->upvals[GETARG_B(i)]->v; TValue *rc = KC(i); TString *key = tsvalue(rc); /* key must be a string */ - if (luaV_fastget(L, upval, key, slot, luaH_getstr)) { + if (luaV_fastget(L, upval, key, slot, luaH_getshortstr)) { setobj2s(L, ra, slot); } else Protect(luaV_finishget(L, upval, rc, ra, slot)); @@ -904,7 +904,7 @@ void luaV_execute (lua_State *L) { StkId rb = RB(i); TValue *rc = KC(i); TString *key = tsvalue(rc); /* key must be a string */ - if (luaV_fastget(L, rb, key, slot, luaH_getstr)) { + if (luaV_fastget(L, rb, key, slot, luaH_getshortstr)) { setobj2s(L, ra, slot); } else Protect(luaV_finishget(L, rb, rc, ra, slot)); @@ -916,7 +916,7 @@ void luaV_execute (lua_State *L) { TValue *rb = KB(i); TValue *rc = RKC(i); TString *key = tsvalue(rb); /* key must be a string */ - if (luaV_fastget(L, upval, key, slot, luaH_getstr)) + if (luaV_fastget(L, upval, key, slot, luaH_getshortstr)) luaV_finishfastset(L, upval, slot, rc); else Protect(luaV_finishset(L, upval, rb, rc, slot)); @@ -953,7 +953,7 @@ void luaV_execute (lua_State *L) { TValue *rb = KB(i); TValue *rc = RKC(i); TString *key = tsvalue(rb); /* key must be a string */ - if (luaV_fastget(L, ra, key, slot, luaH_getstr)) + if (luaV_fastget(L, ra, key, slot, luaH_getshortstr)) luaV_finishfastset(L, ra, slot, rc); else Protect(luaV_finishset(L, ra, rb, rc, slot)); From 84910e04e22ea451f507f66e2af6f9982a711a80 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 19 May 2017 09:47:00 -0300 Subject: [PATCH 0045/1145] better implementation for 'hash_search', without using 'size_t' (simpler to implement and to explain) --- ltable.c | 64 +++++++++++++++++++++++++++++++------------------------- 1 file changed, 36 insertions(+), 28 deletions(-) diff --git a/ltable.c b/ltable.c index c9a498e510..30e9ad3d9f 100644 --- a/ltable.c +++ b/ltable.c @@ -1,5 +1,5 @@ /* -** $Id: ltable.c,v 2.119 2017/05/09 14:39:46 roberto Exp roberto $ +** $Id: ltable.c,v 2.120 2017/05/16 19:07:08 roberto Exp roberto $ ** Lua tables (hash) ** See Copyright Notice in lua.h */ @@ -610,29 +610,35 @@ void luaH_setint (lua_State *L, Table *t, lua_Integer key, TValue *value) { /* ** Try to find a boundary in the hash part of table 't'. From the -** caller, we know that 'i' is zero or present. We need to find an -** upper bound (an absent index larger than 'i') to do a binary search -** for a boundary. We try 'max', a number larger than the total number -** of keys in the table. (Given the size of the array elements, 'max' -** computation cannot overflow a 'size_t'.) If 'max' does not fit in a -** lua_Integer or it is present in the table, we try LUA_MAXINTEGER. If -** LUA_MAXINTEGER is present, it is a boundary, so we are done. Otherwise, -** we are left with a 'j' that is within the size of lua_Integers and -** absent, so can do the binary search. +** caller, we know that 'j' is zero or present and that 'j + 1' is +** present. We want to find a larger key that is absent from the +** table, so that we can do a binary search between the two keys to +** find a boundary. We keep doubling 'j' until we get an absent index. +** If the doubling would overflow, we try LUA_MAXINTEGER. If it is +** absent, we are ready for the binary search. ('j', being max integer, +** is larger or equal to 'i', but it cannot be equal because it is +** absent while 'i' is present; so 'j > i'.) Otherwise, 'j' is a +** boundary. ('j + 1' cannot be a present integer key because it is +** not a valid integer in Lua.) */ -static lua_Unsigned hash_search (Table *t, lua_Unsigned i) { - lua_Unsigned j; - size_t max = (cast(size_t, i) + sizenode(t) + 10) * 2; - if (max <= l_castS2U(LUA_MAXINTEGER) && ttisnil(luaH_getint(t, max))) - j = max; - else { - j = LUA_MAXINTEGER; - if (!ttisnil(luaH_getint(t, j))) /* weird case? */ - return j; /* well, that is a boundary... */ - } - /* now, 'i' is zero or present and 'j' is absent */ +static lua_Unsigned hash_search (Table *t, lua_Unsigned j) { + lua_Unsigned i; + if (j == 0) j++; /* the caller ensures 'j + 1' is present */ + do { + i = j; /* 'i' is a present index */ + if (j <= l_castS2U(LUA_MAXINTEGER) / 2) + j *= 2; + else { + j = LUA_MAXINTEGER; + if (ttisnil(luaH_getint(t, j))) /* t[j] == nil? */ + break; /* 'j' now is an absent index */ + else /* weird case */ + return j; /* well, max integer is a boundary... */ + } + } while (!ttisnil(luaH_getint(t, j))); /* repeat until t[j] == nil */ + /* i < j && t[i] !≃ nil && t[j] == nil */ while (j - i > 1u) { /* do a binary search between them */ - size_t m = (i + j) / 2; + lua_Unsigned m = (i + j) / 2; if (ttisnil(luaH_getint(t, m))) j = m; else i = m; } @@ -642,7 +648,8 @@ static lua_Unsigned hash_search (Table *t, lua_Unsigned i) { /* ** Try to find a boundary in table 't'. (A 'boundary' is an integer index -** such that t[i] is non-nil and t[i+1] is nil (or 0 if t[1] is nil).) +** such that t[i] is non-nil and t[i+1] is nil, plus 0 if t[1] is nil +** and 'maxinteger' if t[maxinteger] is not nil.) ** First, try the array part: if there is an array part and its last ** element is nil, there must be a boundary there; a binary search ** finds that boundary. Otherwise, if the hash part is empty or does not @@ -660,11 +667,12 @@ lua_Unsigned luaH_getn (Table *t) { } return i; } - /* 'j' is zero or present in table */ - else if (isdummy(t) || ttisnil(luaH_getint(t, l_castU2S(j + 1)))) - return j; /* 'j + 1' is absent... */ - else - return hash_search(t, j); + else { /* 'j' is zero or present in table */ + if (isdummy(t) || ttisnil(luaH_getint(t, l_castU2S(j + 1)))) + return j; /* 'j + 1' is absent... */ + else /* 'j + 1' is also present */ + return hash_search(t, j); + } } From e39ee2cc5841eff37f728ceeffd90cd748180f57 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 19 May 2017 09:48:15 -0300 Subject: [PATCH 0046/1145] 'luaH_getn' must return 'lua_Unsigned' (or 'lua_Integer'), to allow the boundary-search algorithm to use 'maxinteger' when it cannot find a good upper bound. --- ltable.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ltable.h b/ltable.h index bd3543b530..ee22cf62cb 100644 --- a/ltable.h +++ b/ltable.h @@ -1,5 +1,5 @@ /* -** $Id: ltable.h,v 2.22 2016/11/07 12:38:35 roberto Exp roberto $ +** $Id: ltable.h,v 2.23 2016/12/22 13:08:50 roberto Exp roberto $ ** Lua tables (hash) ** See Copyright Notice in lua.h */ @@ -54,7 +54,7 @@ LUAI_FUNC void luaH_resize (lua_State *L, Table *t, unsigned int nasize, LUAI_FUNC void luaH_resizearray (lua_State *L, Table *t, unsigned int nasize); LUAI_FUNC void luaH_free (lua_State *L, Table *t); LUAI_FUNC int luaH_next (lua_State *L, Table *t, StkId key); -LUAI_FUNC int luaH_getn (Table *t); +LUAI_FUNC lua_Unsigned luaH_getn (Table *t); #if defined(LUA_DEBUG) From e3d52da144dcb1179988ba315a6cb6703fe9a4f8 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 19 May 2017 09:57:10 -0300 Subject: [PATCH 0047/1145] BUG: in 'computesizes', 'twotoi' overflows when a sequence has more than 2^30 elements. --- ltable.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/ltable.c b/ltable.c index 30e9ad3d9f..4ce33319cd 100644 --- a/ltable.c +++ b/ltable.c @@ -1,5 +1,5 @@ /* -** $Id: ltable.c,v 2.120 2017/05/16 19:07:08 roberto Exp roberto $ +** $Id: ltable.c,v 2.121 2017/05/19 12:47:00 roberto Exp roberto $ ** Lua tables (hash) ** See Copyright Notice in lua.h */ @@ -214,7 +214,8 @@ int luaH_next (lua_State *L, Table *t, StkId key) { ** "count array" where 'nums[i]' is the number of integers in the table ** between 2^(i - 1) + 1 and 2^i. 'pna' enters with the total number of ** integer keys in the table and leaves with the number of keys that -** will go to the array part; return the optimal size. +** will go to the array part; return the optimal size. (The condition +** 'twotoi > 0' in the for loop stops the loop if 'twotoi' overflows.) */ static unsigned int computesizes (unsigned int nums[], unsigned int *pna) { int i; @@ -223,7 +224,9 @@ static unsigned int computesizes (unsigned int nums[], unsigned int *pna) { unsigned int na = 0; /* number of elements to go to array part */ unsigned int optimal = 0; /* optimal size for array part */ /* loop while keys can fill more than half of total size */ - for (i = 0, twotoi = 1; *pna > twotoi / 2; i++, twotoi *= 2) { + for (i = 0, twotoi = 1; + twotoi > 0 && *pna > twotoi / 2; + i++, twotoi *= 2) { a += nums[i]; if (a > twotoi/2) { /* more than half elements present? */ optimal = twotoi; /* optimal size (till now) */ From 1bdc328c7542fac5101684a4ac31a25be3dff112 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 19 May 2017 09:58:40 -0300 Subject: [PATCH 0048/1145] bug: Lua crashes when building sequences with more than 2^30 elements. bug: Table length computation overflows for sequences larger than 2^31 elements.. --- bugs | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 51 insertions(+), 2 deletions(-) diff --git a/bugs b/bugs index d2ec34d269..9d947cac2c 100644 --- a/bugs +++ b/bugs @@ -3656,9 +3656,9 @@ It needs an "interceptor" 'memcmp' function that continues reading memory after a difference is found.]], patch = [[ 2c2 -< ** $Id: bugs,v 1.151 2016/10/19 12:34:27 roberto Exp roberto $ +< ** $Id: bugs,v 1.152 2017/05/05 15:55:36 roberto Exp roberto $ --- -> ** $Id: bugs,v 1.151 2016/10/19 12:34:27 roberto Exp roberto $ +> ** $Id: bugs,v 1.152 2017/05/05 15:55:36 roberto Exp roberto $ 263c263,264 < for (option = LUA_STRFTIMEOPTIONS; *option != '\0'; option += oplen) { --- @@ -3746,6 +3746,55 @@ patch = [[ } +Bug{ +what = [[Lua crashes when building sequences with more than 2^30 elements.]], +report = [[Viacheslav Usov, 2017/05/11]], +since = [[ ]], +fix = nil, +example = [[ +-- crashes if machine has enough memory +local t = {} +for i = 1, 0x7fffffff do + t[i] = i +end +]], +patch = [[ +]] +} + + +Bug{ +what = [[Table length computation overflows for sequences larger than +2^31 elements.]], +report = [[Viacheslav Usov, 2017/05/12]], +since = [[ ]], +fix = nil, +example = [[ +-- on a machine with enough memory +local t = {} +for i = 1, 2147483681 do + t[i] = i +end +print(#t) +]], +patch = [[ +]] +} + + +--[=[ +Bug{ +what = [[ ]], +report = [[ ]], +since = [[ ]], +fix = nil, +example = [[ ]], +patch = [[ +]] +} +]=] + + --[=[ Bug{ what = [[ ]], From 01c96ad12e1e45d6a36a07ae8ec97c09fd0ff913 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 19 May 2017 13:29:40 -0300 Subject: [PATCH 0049/1145] handling of inf, -inf, and NaN by string.format'%q' --- lstrlib.c | 42 ++++++++++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/lstrlib.c b/lstrlib.c index 6ed2bdb86c..89896fa6fc 100644 --- a/lstrlib.c +++ b/lstrlib.c @@ -1,5 +1,5 @@ /* -** $Id: lstrlib.c,v 1.254 2016/12/22 13:08:50 roberto Exp roberto $ +** $Id: lstrlib.c,v 1.255 2017/03/14 12:40:44 roberto Exp roberto $ ** Standard library for string operations and pattern-matching ** See Copyright Notice in lua.h */ @@ -14,6 +14,7 @@ #include #include #include +#include #include #include #include @@ -813,8 +814,6 @@ static int str_gsub (lua_State *L) { ** Hexadecimal floating-point formatter */ -#include - #define SIZELENMOD (sizeof(LUA_NUMBER_FRMLEN)/sizeof(char)) @@ -929,14 +928,32 @@ static void addquoted (luaL_Buffer *b, const char *s, size_t len) { /* -** Ensures the 'buff' string uses a dot as the radix character. +** Serialize a floating-point number in such a way that it can be +** scanned back by Lua. Use hexadecimal format for "common" numbers +** (to preserve precision); inf, -inf, and NaN are handled separately. +** (NaN cannot be expressed as a numeral, so we write '(0/0)' for it.) */ -static void checkdp (char *buff, int nb) { - if (memchr(buff, '.', nb) == NULL) { /* no dot? */ - char point = lua_getlocaledecpoint(); /* try locale point */ - char *ppoint = (char *)memchr(buff, point, nb); - if (ppoint) *ppoint = '.'; /* change it to a dot */ +static int quotefloat (lua_State *L, char *buff, lua_Number n) { + const char *s; /* for the fixed representations */ + if (n == (lua_Number)HUGE_VAL) /* inf? */ + s = "1e9999"; + else if (n == -(lua_Number)HUGE_VAL) /* -inf? */ + s = "-1e9999"; + else if (n != n) /* NaN? */ + s = "(0/0)"; + else { /* format number as hexadecimal */ + int nb = lua_number2strx(L, buff, MAX_ITEM, + "%" LUA_NUMBER_FRMLEN "a", n); + /* ensures that 'buff' string uses a dot as the radix character */ + if (memchr(buff, '.', nb) == NULL) { /* no dot? */ + char point = lua_getlocaledecpoint(); /* try locale point */ + char *ppoint = (char *)memchr(buff, point, nb); + if (ppoint) *ppoint = '.'; /* change it to a dot */ + } + return nb; } + /* for the fixed representations */ + return l_sprintf(buff, MAX_ITEM, "%s", s); } @@ -951,11 +968,8 @@ static void addliteral (lua_State *L, luaL_Buffer *b, int arg) { case LUA_TNUMBER: { char *buff = luaL_prepbuffsize(b, MAX_ITEM); int nb; - if (!lua_isinteger(L, arg)) { /* float? */ - lua_Number n = lua_tonumber(L, arg); /* write as hexa ('%a') */ - nb = lua_number2strx(L, buff, MAX_ITEM, "%" LUA_NUMBER_FRMLEN "a", n); - checkdp(buff, nb); /* ensure it uses a dot */ - } + if (!lua_isinteger(L, arg)) /* float? */ + nb = quotefloat(L, buff, lua_tonumber(L, arg)); else { /* integers */ lua_Integer n = lua_tointeger(L, arg); const char *format = (n == LUA_MININTEGER) /* corner case? */ From 03094da80cc8e501cc33f1f3e37bf8536556b4e9 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 22 May 2017 09:55:16 -0300 Subject: [PATCH 0050/1145] detail (extra closing brackets) --- bugs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/bugs b/bugs index 9d947cac2c..24b16bd758 100644 --- a/bugs +++ b/bugs @@ -3656,9 +3656,9 @@ It needs an "interceptor" 'memcmp' function that continues reading memory after a difference is found.]], patch = [[ 2c2 -< ** $Id: bugs,v 1.152 2017/05/05 15:55:36 roberto Exp roberto $ +< ** $Id: bugs,v 1.153 2017/05/19 12:58:40 roberto Exp roberto $ --- -> ** $Id: bugs,v 1.152 2017/05/05 15:55:36 roberto Exp roberto $ +> ** $Id: bugs,v 1.153 2017/05/19 12:58:40 roberto Exp roberto $ 263c263,264 < for (option = LUA_STRFTIMEOPTIONS; *option != '\0'; option += oplen) { --- @@ -3711,7 +3711,6 @@ A() patch = [[ ]] } -]=] Bug{ From c25380c28da154b271d91543e61e27ae974e9ecc Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 23 May 2017 09:50:11 -0300 Subject: [PATCH 0051/1145] details (using proper version of 'setobj') --- ldo.c | 4 ++-- lvm.c | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/ldo.c b/ldo.c index 5e28fda63d..02f93a41b0 100644 --- a/ldo.c +++ b/ldo.c @@ -1,5 +1,5 @@ /* -** $Id: ldo.c,v 2.158 2017/05/13 12:57:20 roberto Exp roberto $ +** $Id: ldo.c,v 2.159 2017/05/13 13:54:47 roberto Exp roberto $ ** Stack and Call structure of Lua ** See Copyright Notice in lua.h */ @@ -116,7 +116,7 @@ l_noret luaD_throw (lua_State *L, int errcode) { global_State *g = G(L); L->status = cast_byte(errcode); /* mark it as dead */ if (g->mainthread->errorJmp) { /* main thread has a handler? */ - setobjs2s(L, g->mainthread->top++, L->top - 1); /* copy error obj. */ + setobj2s(L, g->mainthread->top++, L->top - 1); /* copy error obj. */ luaD_throw(g->mainthread, errcode); /* re-throw in main thread */ } else { /* no handler at all; abort */ diff --git a/lvm.c b/lvm.c index 453ba2d4d2..9cbb9f45e7 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.283 2017/05/18 19:34:39 roberto Exp roberto $ +** $Id: lvm.c,v 2.284 2017/05/18 19:44:19 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -690,13 +690,13 @@ void luaV_finishOp (lua_State *L) { StkId top = L->top - 1; /* top when 'luaT_trybinTM' was called */ int b = GETARG_B(inst); /* first element to concatenate */ int total = cast_int(top - 1 - (base + b)); /* yet to concatenate */ - setobj2s(L, top - 2, top); /* put TM result in proper position */ + setobjs2s(L, top - 2, top); /* put TM result in proper position */ if (total > 1) { /* are there elements to concat? */ L->top = top - 1; /* top is one after last element (at top-2) */ luaV_concat(L, total); /* concat them (may yield again) */ } /* move final result to final position */ - setobj2s(L, ci->func + 1 + GETARG_A(inst), L->top - 1); + setobjs2s(L, ci->func + 1 + GETARG_A(inst), L->top - 1); L->top = ci->top; /* restore top */ break; } From a9dbc2d64129d54379192b240623b41d8705a465 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 24 May 2017 10:47:11 -0300 Subject: [PATCH 0052/1145] assert removed in 'luaO_arith' (nobody calls it with L==NULL) --- lobject.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lobject.c b/lobject.c index 4a7006b18c..43938c9c2d 100644 --- a/lobject.c +++ b/lobject.c @@ -1,5 +1,5 @@ /* -** $Id: lobject.c,v 2.113 2016/12/22 13:08:50 roberto Exp roberto $ +** $Id: lobject.c,v 2.114 2017/04/19 16:34:35 roberto Exp roberto $ ** Some generic functions over Lua objects ** See Copyright Notice in lua.h */ @@ -155,7 +155,6 @@ void luaO_arith (lua_State *L, int op, const TValue *p1, const TValue *p2, } } /* could not perform raw operation; try metamethod */ - lua_assert(L != NULL); /* should not fail when folding (compile time) */ luaT_trybinTM(L, p1, p2, res, cast(TMS, (op - LUA_OPADD) + TM_ADD)); } From be0d951be86ae7628ec22bb8b4b75870c21817ff Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 24 May 2017 15:54:54 -0300 Subject: [PATCH 0053/1145] bug: cannot reuse a dying upvalue --- lfunc.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lfunc.c b/lfunc.c index 5c6ebd27f4..e84538c9af 100644 --- a/lfunc.c +++ b/lfunc.c @@ -1,5 +1,5 @@ /* -** $Id: lfunc.c,v 2.47 2017/04/11 18:41:09 roberto Exp roberto $ +** $Id: lfunc.c,v 2.48 2017/04/30 20:43:26 roberto Exp roberto $ ** Auxiliary functions to manipulate prototypes and closures ** See Copyright Notice in lua.h */ @@ -63,7 +63,7 @@ UpVal *luaF_findupval (lua_State *L, StkId level) { lua_assert(isintwups(L) || L->openupval == NULL); while ((p = *pp) != NULL && p->v >= level) { lua_assert(upisopen(p)); - if (p->v == level) /* found a corresponding upvalue? */ + if (p->v == level && !isdead(G(L), p)) /* corresponding upvalue? */ return p; /* return it */ pp = &p->u.open.next; } From 4804bbd9bbfb89deb8a6c7dc2bc65601a701222b Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 24 May 2017 18:11:19 -0300 Subject: [PATCH 0054/1145] include first standard header files (Some broken compiler has problems with 'signal.h' being included without a definition for 'size_t'.) --- lua.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lua.c b/lua.c index 2f011861dc..8d5184b366 100644 --- a/lua.c +++ b/lua.c @@ -1,5 +1,5 @@ /* -** $Id: lua.c,v 1.230 2017/01/12 17:14:26 roberto Exp roberto $ +** $Id: lua.c,v 1.231 2017/04/19 12:49:17 roberto Exp roberto $ ** Lua stand-alone interpreter ** See Copyright Notice in lua.h */ @@ -9,11 +9,12 @@ #include "lprefix.h" -#include #include #include #include +#include + #include "lua.h" #include "lauxlib.h" From 72d82a296c1430a1d2bb3fac8ed8cbfb97868707 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 26 May 2017 16:14:29 -0300 Subject: [PATCH 0055/1145] revamping the incremental collector Some simplifications (not counting bytes, couting only slots visited; no more 'gcfinnum'); more GC parameters; using vararg in 'lua_gc' to set parameters in different GC modes --- lapi.c | 44 +++++---- lbaselib.c | 39 ++++++-- lgc.c | 278 +++++++++++++++++++++++++---------------------------- lgc.h | 20 ++-- lstate.c | 12 +-- lstate.h | 9 +- lua.h | 4 +- 7 files changed, 208 insertions(+), 198 deletions(-) diff --git a/lapi.c b/lapi.c index e7d59b45c9..0c109e1eb4 100644 --- a/lapi.c +++ b/lapi.c @@ -1,5 +1,5 @@ /* -** $Id: lapi.c,v 2.266 2017/05/11 18:57:46 roberto Exp roberto $ +** $Id: lapi.c,v 2.267 2017/05/18 12:34:58 roberto Exp roberto $ ** Lua API ** See Copyright Notice in lua.h */ @@ -1046,17 +1046,12 @@ LUA_API int lua_status (lua_State *L) { /* ** Garbage-collection function */ - -#if !defined(LUA_GENMAJORMUL) -#define LUA_GENMAJORMUL 100 -#define LUA_GENMINORMUL 5 -#endif - -LUA_API int lua_gc (lua_State *L, int what, int data) { +LUA_API int lua_gc (lua_State *L, int what, ...) { + va_list argp; int res = 0; - global_State *g; + global_State *g = G(L); lua_lock(L); - g = G(L); + va_start(argp, what); switch (what) { case LUA_GCSTOP: { g->gcrunning = 0; @@ -1081,11 +1076,12 @@ LUA_API int lua_gc (lua_State *L, int what, int data) { break; } case LUA_GCSTEP: { + int data = va_arg(argp, int); l_mem debt = 1; /* =1 to signal that it did an actual step */ lu_byte oldrunning = g->gcrunning; g->gcrunning = 1; /* allow GC to run */ if (data == 0) { - luaE_setdebt(g, -GCSTEPSIZE); /* to do a "small" step */ + luaE_setdebt(g, 0); /* do a basic step */ luaC_step(L); } else { /* add 'data' to total debt */ @@ -1099,14 +1095,15 @@ LUA_API int lua_gc (lua_State *L, int what, int data) { break; } case LUA_GCSETPAUSE: { - res = g->gcpause; - g->gcpause = data; + int data = va_arg(argp, int); + res = g->gcpause + 100; + g->gcpause = (data >= 100) ? data - 100 : 0; break; } case LUA_GCSETSTEPMUL: { - res = g->gcstepmul; - if (data < 40) data = 40; /* avoid ridiculous low values (and 0) */ - g->gcstepmul = data; + int data = va_arg(argp, int); + res = g->gcstepmul * 10; + g->gcstepmul = data / 10; break; } case LUA_GCISRUNNING: { @@ -1114,19 +1111,26 @@ LUA_API int lua_gc (lua_State *L, int what, int data) { break; } case LUA_GCGEN: { - lu_byte aux = data & 0xff; - g->genminormul = (aux == 0) ? LUA_GENMINORMUL : aux; - aux = (data >> 8) & 0xff; - g->genmajormul = (aux == 0) ? LUA_GENMAJORMUL : aux; + int minormul = va_arg(argp, int); + int majormul = va_arg(argp, int); + g->genminormul = (minormul == 0) ? LUAI_GENMINORMUL : minormul; + g->genmajormul = (majormul == 0) ? LUAI_GENMAJORMUL : majormul; luaC_changemode(L, KGC_GEN); break; } case LUA_GCINC: { + int pause = va_arg(argp, int); + int stepmul = va_arg(argp, int); + int stepsize = va_arg(argp, int); + g->gcpause = (pause == 0) ? LUAI_GCPAUSE : pause; + g->gcstepmul = (stepmul == 0) ? LUAI_GCMUL : stepmul; + g->gcstepsize = (stepsize == 0) ? LUAI_GCSTEPSIZE : stepsize; luaC_changemode(L, KGC_INC); break; } default: res = -1; /* invalid option */ } + va_end(argp); lua_unlock(L); return res; } diff --git a/lbaselib.c b/lbaselib.c index 04fdd5c2ef..55b56f35ef 100644 --- a/lbaselib.c +++ b/lbaselib.c @@ -1,5 +1,5 @@ /* -** $Id: lbaselib.c,v 1.314 2016/09/05 19:06:34 roberto Exp roberto $ +** $Id: lbaselib.c,v 1.315 2017/02/23 21:07:34 roberto Exp roberto $ ** Basic library ** See Copyright Notice in lua.h */ @@ -178,19 +178,46 @@ static int luaB_collectgarbage (lua_State *L) { LUA_GCCOUNT, LUA_GCSTEP, LUA_GCSETPAUSE, LUA_GCSETSTEPMUL, LUA_GCISRUNNING, LUA_GCGEN, LUA_GCINC}; int o = optsnum[luaL_checkoption(L, 1, "collect", opts)]; - int ex = (int)luaL_optinteger(L, 2, 0); - int res = lua_gc(L, o, ex); switch (o) { case LUA_GCCOUNT: { - int b = lua_gc(L, LUA_GCCOUNTB, 0); - lua_pushnumber(L, (lua_Number)res + ((lua_Number)b/1024)); + int k = lua_gc(L, o); + int b = lua_gc(L, LUA_GCCOUNTB); + lua_pushnumber(L, (lua_Number)k + ((lua_Number)b/1024)); return 1; } - case LUA_GCSTEP: case LUA_GCISRUNNING: { + case LUA_GCSTEP: { + int step = (int)luaL_optinteger(L, 2, 0); + int res = lua_gc(L, o, step); lua_pushboolean(L, res); return 1; } + case LUA_GCSETPAUSE: + case LUA_GCSETSTEPMUL: { + int p = (int)luaL_optinteger(L, 2, 0); + int previous = lua_gc(L, o, p); + lua_pushinteger(L, previous); + return 1; + } + case LUA_GCISRUNNING: { + int res = lua_gc(L, o); + lua_pushboolean(L, res); + return 1; + } + case LUA_GCGEN: { + int minormul = (int)luaL_optinteger(L, 2, 0); + int majormul = (int)luaL_optinteger(L, 3, 0); + lua_gc(L, o, minormul, majormul); + return 0; + } + case LUA_GCINC: { + int pause = (int)luaL_optinteger(L, 2, 0); + int stepmul = (int)luaL_optinteger(L, 3, 0); + int stepsize = (int)luaL_optinteger(L, 4, 0); + lua_gc(L, o, pause, stepmul, stepsize); + return 0; + } default: { + int res = lua_gc(L, o); lua_pushinteger(L, res); return 1; } diff --git a/lgc.c b/lgc.c index a025340561..4600c43580 100644 --- a/lgc.c +++ b/lgc.c @@ -1,5 +1,5 @@ /* -** $Id: lgc.c,v 2.227 2017/04/30 20:43:26 roberto Exp roberto $ +** $Id: lgc.c,v 2.228 2017/05/04 13:32:01 roberto Exp roberto $ ** Garbage Collector ** See Copyright Notice in lua.h */ @@ -27,23 +27,29 @@ /* -** cost of sweeping one element (the size of a small object divided -** by some adjust for the sweep speed) +** Maximum number of elements to sweep in each single step. +** (Large enough to dissipate fixed overheads but small enough +** to allow small steps for the collector.) */ -#define GCSWEEPCOST ((sizeof(TString) + 4) / 4) +#define GCSWEEPMAX 100 + +/* +** Maximum number of finalizers to call in each single step. +*/ +#define GCFINMAX 10 -/* maximum number of elements to sweep in each single step */ -#define GCSWEEPMAX (cast_int((GCSTEPSIZE / GCSWEEPCOST) / 4)) -/* cost of calling one finalizer */ -#define GCFINALIZECOST GCSWEEPCOST +/* +** Cost of calling one finalizer. +*/ +#define GCFINALIZECOST 50 /* -** macro to adjust 'stepmul': 'stepmul' is actually used like -** 'stepmul / STEPMULADJ' (value chosen by tests) +** The equivalent, in bytes, of one unit of "work" (visiting a slot, +** sweeping an object, etc.) * 10 (for scaling) */ -#define STEPMULADJ 200 +#define WORK2MEM (sizeof(TValue) * 10) /* @@ -86,7 +92,7 @@ #define markobjectN(g,t) { if (t) markobject(g,t); } static void reallymarkobject (global_State *g, GCObject *o); -static l_mem atomic (lua_State *L); +static lu_mem atomic (lua_State *L); /* @@ -246,21 +252,15 @@ static void reallymarkobject (global_State *g, GCObject *o) { reentry: white2gray(o); switch (o->tt) { - case LUA_TSHRSTR: { - gray2black(o); - g->GCmemtrav += sizelstring(gco2ts(o)->shrlen); - break; - } + case LUA_TSHRSTR: case LUA_TLNGSTR: { gray2black(o); - g->GCmemtrav += sizelstring(gco2ts(o)->u.lnglen); break; } case LUA_TUSERDATA: { TValue uvalue; markobjectN(g, gco2u(o)->metatable); /* mark its metatable */ gray2black(o); - g->GCmemtrav += sizeudata(gco2u(o)); getuservalue(g->mainthread, gco2u(o), &uvalue); if (valiswhite(&uvalue)) { /* markvalue(g, &uvalue); */ o = gcvalue(&uvalue); @@ -270,7 +270,6 @@ static void reallymarkobject (global_State *g, GCObject *o) { } case LUA_TUPVAL: { UpVal *uv = gco2upv(o); - g->GCmemtrav += sizeof(UpVal); if (!upisopen(uv)) /* open upvalues are kept gray */ gray2black(o); markvalue(g, uv->v); /* mark its content */ @@ -314,10 +313,14 @@ static void markmt (global_State *g) { /* ** mark all objects in list of being-finalized */ -static void markbeingfnz (global_State *g) { +static lu_mem markbeingfnz (global_State *g) { GCObject *o; - for (o = g->tobefnz; o != NULL; o = o->next) + lu_mem count = 0; + for (o = g->tobefnz; o != NULL; o = o->next) { + count++; markobject(g, o); + } + return count; } @@ -327,10 +330,12 @@ static void markbeingfnz (global_State *g) { ** thread.) Remove from the list threads that no longer have upvalues and ** not-marked threads. */ -static void remarkupvals (global_State *g) { +static int remarkupvals (global_State *g) { lua_State *thread; lua_State **p = &g->twups; + int work = 0; while ((thread = *p) != NULL) { + work++; lua_assert(!isblack(thread)); /* threads are never black */ if (isgray(thread) && thread->openupval != NULL) p = &thread->twups; /* keep marked thread with upvalues in the list */ @@ -339,11 +344,13 @@ static void remarkupvals (global_State *g) { *p = thread->twups; /* remove thread from the list */ thread->twups = thread; /* mark that it is out of list */ for (uv = thread->openupval; uv != NULL; uv = uv->u.open.next) { + work++; if (!iswhite(uv)) /* upvalue already visited? */ markvalue(g, uv->v); /* mark its value */ } } } + return work; } @@ -491,8 +498,7 @@ static lu_mem traversetable (global_State *g, Table *h) { } else /* not weak */ traversestrongtable(g, h); - return sizeof(Table) + sizeof(TValue) * h->sizearray + - sizeof(Node) * cast(size_t, allocsizenode(h)); + return 1 + h->sizearray + 2 * allocsizenode(h); } @@ -539,38 +545,33 @@ static int traverseproto (global_State *g, Proto *f) { markobjectN(g, f->p[i]); for (i = 0; i < f->sizelocvars; i++) /* mark local-variable names */ markobjectN(g, f->locvars[i].varname); - return sizeof(Proto) + sizeof(Instruction) * f->sizecode + - sizeof(Proto *) * f->sizep + - sizeof(TValue) * f->sizek + - sizeof(int) * f->sizelineinfo + - sizeof(LocVar) * f->sizelocvars + - sizeof(Upvaldesc) * f->sizeupvalues; + return 1 + f->sizek + f->sizeupvalues + f->sizep + f->sizelocvars; } -static lu_mem traverseCclosure (global_State *g, CClosure *cl) { +static int traverseCclosure (global_State *g, CClosure *cl) { int i; for (i = 0; i < cl->nupvalues; i++) /* mark its upvalues */ markvalue(g, &cl->upvalue[i]); - return sizeCclosure(cl->nupvalues); + return 1 + cl->nupvalues; } /* ** Traverse a Lua closure, marking its prototype and its upvalues. ** (Both can be NULL while closure is being created.) */ -static lu_mem traverseLclosure (global_State *g, LClosure *cl) { +static int traverseLclosure (global_State *g, LClosure *cl) { int i; markobjectN(g, cl->p); /* mark its prototype */ for (i = 0; i < cl->nupvalues; i++) { /* visit its upvalues */ UpVal *uv = cl->upvals[i]; markobjectN(g, uv); /* mark upvalue */ } - return sizeLclosure(cl->nupvalues); + return 1 + cl->nupvalues; } -static lu_mem traversethread (global_State *g, lua_State *th) { +static int traversethread (global_State *g, lua_State *th) { StkId o = th->stack; if (o == NULL) return 1; /* stack not completely built yet */ @@ -590,8 +591,7 @@ static lu_mem traversethread (global_State *g, lua_State *th) { } else if (!g->gcemergency) luaD_shrinkstack(th); /* do not change stack in emergency cycle */ - return (sizeof(lua_State) + sizeof(TValue) * th->stacksize + - sizeof(CallInfo) * th->nci); + return 1 + th->stacksize; } @@ -599,51 +599,47 @@ static lu_mem traversethread (global_State *g, lua_State *th) { ** traverse one gray object, turning it to black (except for threads, ** which are always gray). */ -static void propagatemark (global_State *g) { - lu_mem size; +static lu_mem propagatemark (global_State *g) { GCObject *o = g->gray; gray2black(o); switch (o->tt) { case LUA_TTABLE: { Table *h = gco2t(o); g->gray = h->gclist; /* remove from 'gray' list */ - size = traversetable(g, h); - break; + return traversetable(g, h); } case LUA_TLCL: { LClosure *cl = gco2lcl(o); g->gray = cl->gclist; /* remove from 'gray' list */ - size = traverseLclosure(g, cl); - break; + return traverseLclosure(g, cl); } case LUA_TCCL: { CClosure *cl = gco2ccl(o); g->gray = cl->gclist; /* remove from 'gray' list */ - size = traverseCclosure(g, cl); - break; + return traverseCclosure(g, cl); } case LUA_TTHREAD: { lua_State *th = gco2th(o); g->gray = th->gclist; /* remove from 'gray' list */ linkgclist(th, g->grayagain); /* insert into 'grayagain' list */ black2gray(o); - size = traversethread(g, th); - break; + return traversethread(g, th); } case LUA_TPROTO: { Proto *p = gco2p(o); g->gray = p->gclist; /* remove from 'gray' list */ - size = traverseproto(g, p); - break; + return traverseproto(g, p); } - default: lua_assert(0); return; + default: lua_assert(0); return 0; } - g->GCmemtrav += size; } -static void propagateall (global_State *g) { - while (g->gray) propagatemark(g); +static lu_mem propagateall (global_State *g) { + lu_mem tot = 0; + while (g->gray) + tot += propagatemark(g); + return tot; } @@ -719,7 +715,7 @@ static void clearvalues (global_State *g, GCObject *l, GCObject *f) { setnilvalue(o); /* remove value */ } for (n = gnode(h, 0); n < limit; n++) { - if (!ttisnil(gval(n)) && iscleared(g, gval(n))) { + if (iscleared(g, gval(n))) { setnilvalue(gval(n)); /* remove value ... */ removeentry(n); /* and remove entry from table */ } @@ -770,22 +766,20 @@ static void freeobj (lua_State *L, GCObject *o) { } -#define sweepwholelist(L,p) sweeplist(L,p,MAX_LUMEM) -static GCObject **sweeplist (lua_State *L, GCObject **p, lu_mem count); - - /* -** sweep at most 'count' elements from a list of GCObjects erasing dead +** sweep at most 'countin' elements from a list of GCObjects erasing dead ** objects, where a dead object is one marked with the old (non current) ** white; change all non-dead objects back to white, preparing for next ** collection cycle. Return where to continue the traversal or NULL if -** list is finished. +** list is finished. ('*countout' gets the number of elements traversed.) */ -static GCObject **sweeplist (lua_State *L, GCObject **p, lu_mem count) { +static GCObject **sweeplist (lua_State *L, GCObject **p, int countin, + int *countout) { global_State *g = G(L); int ow = otherwhite(g); + int i; int white = luaC_white(g); /* current white */ - while (*p != NULL && count-- > 0) { + for (i = 0; *p != NULL && i < countin; i++) { GCObject *curr = *p; int marked = curr->marked; if (isdeadm(ow, marked)) { /* is 'curr' dead? */ @@ -797,6 +791,8 @@ static GCObject **sweeplist (lua_State *L, GCObject **p, lu_mem count) { p = &curr->next; /* go to next element */ } } + if (countout) + *countout = i; /* number of elements traversed */ return (*p == NULL) ? NULL : p; } @@ -807,7 +803,7 @@ static GCObject **sweeplist (lua_State *L, GCObject **p, lu_mem count) { static GCObject **sweeptolive (lua_State *L, GCObject **p) { GCObject **old = p; do { - p = sweeplist(L, p, 1); + p = sweeplist(L, p, 1, NULL); } while (p == old); return p; } @@ -892,16 +888,13 @@ static void GCTM (lua_State *L, int propagateerrors) { /* -** call a few (up to 'g->gcfinnum') finalizers +** Call a few finalizers */ -static int runafewfinalizers (lua_State *L) { +static int runafewfinalizers (lua_State *L, int n) { global_State *g = G(L); - unsigned int i; - lua_assert(!g->tobefnz || g->gcfinnum > 0); - for (i = 0; g->tobefnz && i < g->gcfinnum; i++) + int i; + for (i = 0; i < n && g->tobefnz; i++) GCTM(L, 1); /* call one finalizer */ - g->gcfinnum = (!g->tobefnz) ? 0 /* nothing more to finalize? */ - : g->gcfinnum * 2; /* else call a few more next time */ return i; } @@ -1285,9 +1278,6 @@ static void genstep (lua_State *L, global_State *g) { //lua_checkmemory(L); } - - - /* }====================================================== */ @@ -1299,26 +1289,28 @@ static void genstep (lua_State *L, global_State *g) { /* -** Set a reasonable "time" to wait before starting a new GC cycle; cycle -** will start when memory use hits threshold. (Division by 'estimate' -** should be OK: it cannot be zero (because Lua cannot even start with -** less than PAUSEADJ bytes). +** Set the "time" to wait before starting a new GC cycle; cycle will +** start when memory use hits the threshold of ('estimate' * gcpause / +** PAUSEADJ). (Division by 'estimate' should be OK: it cannot be zero, +** because Lua cannot even start with less than PAUSEADJ bytes). */ static void setpause (global_State *g) { l_mem threshold, debt; + int pause = g->gcpause + 100; l_mem estimate = g->GCestimate / PAUSEADJ; /* adjust 'estimate' */ lua_assert(estimate > 0); - threshold = (g->gcpause < MAX_LMEM / estimate) /* overflow? */ - ? estimate * g->gcpause /* no overflow */ + threshold = (pause < MAX_LMEM / estimate) /* overflow? */ + ? estimate * pause /* no overflow */ : MAX_LMEM; /* overflow; truncate to maximum */ debt = gettotalbytes(g) - threshold; + if (debt > 0) debt = 0; luaE_setdebt(g, debt); } /* ** Enter first sweep phase. -** The call to 'sweeplist' tries to make pointer point to an object +** The call to 'sweeptolive' makes the pointer point to an object ** inside the list (instead of to the header), so that the real sweep do ** not need to skip objects created between "now" and the start of the ** real sweep. @@ -1327,11 +1319,15 @@ static void entersweep (lua_State *L) { global_State *g = G(L); g->gcstate = GCSswpallgc; lua_assert(g->sweepgc == NULL); - g->sweepgc = sweeplist(L, &g->allgc, 1); + g->sweepgc = sweeptolive(L, &g->allgc); } -static void deletealllist (lua_State *L, GCObject *p, GCObject *limit) { +/* +** Delete all objects in list 'p' until (but not including) object +** 'limit'. +*/ +static void deletelist (lua_State *L, GCObject *p, GCObject *limit) { while (p != limit) { GCObject *next = p->next; freeobj(L, p); @@ -1340,52 +1336,50 @@ static void deletealllist (lua_State *L, GCObject *p, GCObject *limit) { } +/* +** Call all finalizers of the objects in the given Lua state, and +** then free all objects, except for the main thread. +*/ void luaC_freeallobjects (lua_State *L) { global_State *g = G(L); luaC_changemode(L, KGC_INC); separatetobefnz(g, 1); /* separate all objects with finalizers */ lua_assert(g->finobj == NULL); callallpendingfinalizers(L); - deletealllist(L, g->allgc, obj2gco(g->mainthread)); - deletealllist(L, g->finobj, NULL); - deletealllist(L, g->fixedgc, NULL); /* collect fixed objects */ + deletelist(L, g->allgc, obj2gco(g->mainthread)); + deletelist(L, g->finobj, NULL); + deletelist(L, g->fixedgc, NULL); /* collect fixed objects */ lua_assert(g->strt.nuse == 0); } -static l_mem atomic (lua_State *L) { +static lu_mem atomic (lua_State *L) { global_State *g = G(L); - l_mem work; + lu_mem work = 0; GCObject *origweak, *origall; GCObject *grayagain = g->grayagain; /* save original list */ g->grayagain = NULL; lua_assert(g->ephemeron == NULL && g->weak == NULL); lua_assert(!iswhite(g->mainthread)); g->gcstate = GCSatomic; - g->GCmemtrav = 0; /* start counting work */ markobject(g, L); /* mark running thread */ /* registry and global metatables may be changed by API */ markvalue(g, &g->l_registry); markmt(g); /* mark global metatables */ /* remark occasional upvalues of (maybe) dead threads */ - remarkupvals(g); - propagateall(g); /* propagate changes */ - work = g->GCmemtrav; /* stop counting (do not recount 'grayagain') */ + work += remarkupvals(g); + work += propagateall(g); /* propagate changes */ g->gray = grayagain; - propagateall(g); /* traverse 'grayagain' list */ - g->GCmemtrav = 0; /* restart counting */ + work += propagateall(g); /* traverse 'grayagain' list */ convergeephemerons(g); /* at this point, all strongly accessible objects are marked. */ /* Clear values from weak tables, before checking finalizers */ clearvalues(g, g->weak, NULL); clearvalues(g, g->allweak, NULL); origweak = g->weak; origall = g->allweak; - work += g->GCmemtrav; /* stop counting (objects being finalized) */ separatetobefnz(g, 0); /* separate objects to be finalized */ - g->gcfinnum = 1; /* there may be objects to be finalized */ - markbeingfnz(g); /* mark objects that will be finalized */ - propagateall(g); /* remark, to propagate 'resurrection' */ - g->GCmemtrav = 0; /* restart counting */ + work += markbeingfnz(g); /* mark objects that will be finalized */ + work += propagateall(g); /* remark, to propagate 'resurrection' */ convergeephemerons(g); /* at this point, all resurrected objects are marked. */ /* remove dead objects from weak tables */ @@ -1398,24 +1392,24 @@ static l_mem atomic (lua_State *L) { clearprotolist(g); g->currentwhite = cast_byte(otherwhite(g)); /* flip current white */ lua_assert(g->gray == NULL); - work += g->GCmemtrav; /* complete counting */ - return work; /* estimate of memory marked by 'atomic' */ + return work; /* estimate of slots marked by 'atomic' */ } -static lu_mem sweepstep (lua_State *L, global_State *g, - int nextstate, GCObject **nextlist) { +static int sweepstep (lua_State *L, global_State *g, + int nextstate, GCObject **nextlist) { if (g->sweepgc) { l_mem olddebt = g->GCdebt; - g->sweepgc = sweeplist(L, g->sweepgc, GCSWEEPMAX); + int count; + g->sweepgc = sweeplist(L, g->sweepgc, GCSWEEPMAX, &count); g->GCestimate += g->GCdebt - olddebt; /* update estimate */ - if (g->sweepgc) /* is there still something to sweep? */ - return (GCSWEEPMAX * GCSWEEPCOST); + return count; + } + else { /* enter next state */ + g->gcstate = nextstate; + g->sweepgc = nextlist; + return 0; /* no work done */ } - /* else enter next state */ - g->gcstate = nextstate; - g->sweepgc = nextlist; - return 0; } @@ -1423,23 +1417,21 @@ static lu_mem singlestep (lua_State *L) { global_State *g = G(L); switch (g->gcstate) { case GCSpause: { - g->GCmemtrav = g->strt.size * sizeof(GCObject*); restartcollection(g); g->gcstate = GCSpropagate; - return g->GCmemtrav; + return 1; } case GCSpropagate: { - g->GCmemtrav = 0; - if (g->gray == NULL) /* no more gray objects? */ + if (g->gray == NULL) { /* no more gray objects? */ g->gcstate = GCSenteratomic; /* finish propagate phase */ + return 0; + } else - propagatemark(g); /* traverse one gray object */ - return g->GCmemtrav; /* memory traversed in this step */ + return propagatemark(g); /* traverse one gray object */ } case GCSenteratomic: { - lu_mem work; - propagateall(g); /* make sure gray list is empty */ - work = atomic(L); /* work is what was traversed by 'atomic' */ + lu_mem work = propagateall(g); /* make sure gray list is empty */ + work += atomic(L); /* work is what was traversed by 'atomic' */ entersweep(L); g->GCestimate = gettotalbytes(g); /* first estimate */; return work; @@ -1460,8 +1452,8 @@ static lu_mem singlestep (lua_State *L) { } case GCScallfin: { /* call remaining finalizers */ if (g->tobefnz && !g->gcemergency) { - int n = runafewfinalizers(L); - return (n * GCFINALIZECOST); + int n = runafewfinalizers(L, GCFINMAX); + return n * GCFINALIZECOST; } else { /* emergency mode or no more finalizers */ g->gcstate = GCSpause; /* finish collection */ @@ -1485,45 +1477,36 @@ void luaC_runtilstate (lua_State *L, int statesmask) { /* -** get GC debt and convert it from Kb to 'work units' (avoid zero debt -** and overflows) -*/ -static l_mem getdebt (global_State *g) { - l_mem debt = g->GCdebt; - int stepmul = g->gcstepmul; - if (debt <= 0) return 0; /* minimal debt */ - else { - debt = (debt / STEPMULADJ) + 1; - debt = (debt < MAX_LMEM / stepmul) ? debt * stepmul : MAX_LMEM; - return debt; - } -} - -/* -** performs a basic incremental step +** Performs a basic incremental step. The debt and step size are +** converted from bytes to "units of work"; then the function loops +** running single steps until adding that many units of work or +** finishing a cycle (pause state). Finally, it sets the debt that +** controls when next step will be performed. */ static void incstep (lua_State *L, global_State *g) { - l_mem debt = getdebt(g); /* GC deficit (to be paid now) */ + int stepmul = (g->gcstepmul | 1); /* avoid division by 0 */ + l_mem debt = (g->GCdebt / WORK2MEM) * stepmul; + l_mem stepsize = cast(l_mem, 1) << g->gcstepsize; + stepsize = -((stepsize / WORK2MEM) * stepmul); do { /* repeat until pause or enough "credit" (negative debt) */ lu_mem work = singlestep(L); /* perform one single step */ debt -= work; - } while (debt > -GCSTEPSIZE && g->gcstate != GCSpause); + } while (debt > stepsize && g->gcstate != GCSpause); if (g->gcstate == GCSpause) setpause(g); /* pause until next cycle */ else { - debt = (debt / g->gcstepmul) * STEPMULADJ; /* convert 'work units' to Kb */ + debt = (debt / stepmul) * WORK2MEM; /* convert 'work units' to bytes */ luaE_setdebt(g, debt); - runafewfinalizers(L); } } /* -** performs a basic GC step when collector is running +** performs a basic GC step if collector is running */ void luaC_step (lua_State *L) { global_State *g = G(L); if (!g->gcrunning) /* not running? */ - luaE_setdebt(g, -GCSTEPSIZE * 10); /* avoid being called too often */ + luaE_setdebt(g, -MAX_LMEM); /* avoid being called without need */ else if (g->gckind == KGC_INC) incstep(L, g); else @@ -1532,9 +1515,7 @@ void luaC_step (lua_State *L) { /* -** Performs a full GC cycle; if 'isemergency', set a flag to avoid -** some operations which could change the interpreter state in some -** unexpected ways (running finalizers and shrinking some structures). +** Perform a full collection in incremental mode. ** Before running the collection, check 'keepinvariant'; if it is true, ** there may be some objects marked as black, so the collector has ** to sweep all objects to turn them back to white (as white has not @@ -1553,6 +1534,11 @@ static void fullinc (lua_State *L, global_State *g) { } +/* +** Performs a full GC cycle; if 'isemergency', set a flag to avoid +** some operations which could change the interpreter state in some +** unexpected ways (running finalizers and shrinking some structures). +*/ void luaC_fullgc (lua_State *L, int isemergency) { global_State *g = G(L); lua_assert(!g->gcemergency); diff --git a/lgc.h b/lgc.h index 6ee0b01275..36f361ec12 100644 --- a/lgc.h +++ b/lgc.h @@ -1,5 +1,5 @@ /* -** $Id: lgc.h,v 2.96 2017/04/11 18:41:09 roberto Exp roberto $ +** $Id: lgc.h,v 2.97 2017/05/04 13:32:01 roberto Exp roberto $ ** Garbage Collector ** See Copyright Notice in lua.h */ @@ -25,14 +25,6 @@ */ - -/* how much to allocate before next GC step */ -#if !defined(GCSTEPSIZE) -/* ~100 small strings */ -#define GCSTEPSIZE (cast_int(100 * sizeof(TString))) -#endif - - /* ** Possible states of the Garbage Collector */ @@ -125,6 +117,16 @@ check_exp(getage(o) == (f), (o)->marked ^= ((f)^(t))) +/* Default Values for GC parameters */ +#define LUAI_GENMAJORMUL 100 +#define LUAI_GENMINORMUL 5 + +#define LUAI_GCPAUSE 100 /* 100% */ +#define LUAI_GCMUL 10 +/* how much to allocate before next GC step (log2) */ +#define LUAI_GCSTEPSIZE 13 /* 8 KB */ + + /* ** Does one step of collection when debt becomes positive. 'pre'/'pos' ** allows some adjustments to be done only when needed. macro diff --git a/lstate.c b/lstate.c index 3bce076d6f..5f1c6e4836 100644 --- a/lstate.c +++ b/lstate.c @@ -1,5 +1,5 @@ /* -** $Id: lstate.c,v 2.138 2017/04/24 16:59:26 roberto Exp roberto $ +** $Id: lstate.c,v 2.139 2017/05/04 13:32:01 roberto Exp roberto $ ** Global State ** See Copyright Notice in lua.h */ @@ -28,14 +28,6 @@ #include "ltm.h" -#if !defined(LUAI_GCPAUSE) -#define LUAI_GCPAUSE 200 /* 200% */ -#endif - -#if !defined(LUAI_GCMUL) -#define LUAI_GCMUL 200 /* GC runs 'twice the speed' of memory allocation */ -#endif - /* ** a macro to help the creation of a unique random seed when a state is @@ -328,9 +320,9 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { g->twups = NULL; g->totalbytes = sizeof(LG); g->GCdebt = 0; - g->gcfinnum = 0; g->gcpause = LUAI_GCPAUSE; g->gcstepmul = LUAI_GCMUL; + g->gcstepsize = LUAI_GCSTEPSIZE; for (i=0; i < LUA_NUMTAGS; i++) g->mt[i] = NULL; if (luaD_rawrunprotected(L, f_luaopen, NULL) != LUA_OK) { /* memory allocation error: free partial state */ diff --git a/lstate.h b/lstate.h index d2dabc4dc6..9a3e778a85 100644 --- a/lstate.h +++ b/lstate.h @@ -1,5 +1,5 @@ /* -** $Id: lstate.h,v 2.140 2017/05/04 13:32:01 roberto Exp roberto $ +** $Id: lstate.h,v 2.141 2017/05/13 13:54:47 roberto Exp roberto $ ** Global State ** See Copyright Notice in lua.h */ @@ -139,7 +139,6 @@ typedef struct global_State { void *ud; /* auxiliary data to 'frealloc' */ l_mem totalbytes; /* number of bytes currently allocated - GCdebt */ l_mem GCdebt; /* bytes allocated not yet compensated by the collector */ - lu_mem GCmemtrav; /* memory traversed by the GC */ lu_mem GCestimate; /* an estimate of the non-garbage memory in use */ stringtable strt; /* hash table for strings */ TValue l_registry; @@ -151,6 +150,9 @@ typedef struct global_State { lu_byte genmajormul; /* control for major generational collections */ lu_byte gcrunning; /* true if GC is running */ lu_byte gcemergency; /* true if this is an emergency collection */ + lu_byte gcpause; /* size of pause between successive GCs */ + lu_byte gcstepmul; /* GC "speed" */ + lu_byte gcstepsize; /* (log2 of) GC granularity */ GCObject *allgc; /* list of all collectable objects */ GCObject **sweepgc; /* current position of sweep in list */ GCObject *finobj; /* list of collectable objects with finalizers */ @@ -170,9 +172,6 @@ typedef struct global_State { GCObject *finobjold; /* list of old objects with finalizers */ GCObject *finobjrold; /* list of really old objects with finalizers */ struct lua_State *twups; /* list of threads with open upvalues */ - unsigned int gcfinnum; /* number of finalizers to call in each GC step */ - int gcpause; /* size of pause between successive GCs */ - int gcstepmul; /* GC 'granularity' */ lua_CFunction panic; /* to be called in unprotected errors */ struct lua_State *mainthread; const lua_Number *version; /* pointer to version number */ diff --git a/lua.h b/lua.h index 735a2599e9..1499c04e9c 100644 --- a/lua.h +++ b/lua.h @@ -1,5 +1,5 @@ /* -** $Id: lua.h,v 1.333 2017/02/23 21:07:34 roberto Exp roberto $ +** $Id: lua.h,v 1.334 2017/05/18 12:34:58 roberto Exp roberto $ ** Lua - A Scripting Language ** Lua.org, PUC-Rio, Brazil (http://www.lua.org) ** See Copyright Notice at the end of this file @@ -311,7 +311,7 @@ LUA_API int (lua_isyieldable) (lua_State *L); #define LUA_GCGEN 10 #define LUA_GCINC 11 -LUA_API int (lua_gc) (lua_State *L, int what, int data); +LUA_API int (lua_gc) (lua_State *L, int what, ...); /* From 882174684147b2fbc3f3a740c463b199978d9286 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 31 May 2017 15:54:58 -0300 Subject: [PATCH 0056/1145] updated GC states in function 'T.gcstate' --- ltests.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/ltests.c b/ltests.c index 93a8428409..fc233c7fac 100644 --- a/ltests.c +++ b/ltests.c @@ -1,5 +1,5 @@ /* -** $Id: ltests.c,v 2.216 2017/04/24 18:06:12 roberto Exp roberto $ +** $Id: ltests.c,v 2.217 2017/05/04 13:32:01 roberto Exp $ ** Internal Module for Debugging of the Lua Implementation ** See Copyright Notice in lua.h */ @@ -734,10 +734,12 @@ static int gc_printobj (lua_State *L) { static int gc_state (lua_State *L) { - static const char *statenames[] = {"propagate", "atomic", "sweepallgc", - "sweepfinobj", "sweeptobefnz", "sweepend", "pause", ""}; - static const int states[] = {GCSpropagate, GCSenteratomic, GCSswpallgc, - GCSswpfinobj, GCSswptobefnz, GCSswpend, GCSpause, -1}; + static const char *statenames[] = { + "propagate", "atomic", "enteratomic", "sweepallgc", "sweepfinobj", + "sweeptobefnz", "sweepend", "callfin", "pause", ""}; + static const int states[] = { + GCSpropagate, GCSenteratomic, GCSatomic, GCSswpallgc, GCSswpfinobj, + GCSswptobefnz, GCSswpend, GCScallfin, GCSpause, -1}; int option = states[luaL_checkoption(L, 1, "", statenames)]; if (option == -1) { lua_pushstring(L, statenames[G(L)->gcstate]); From 4bc33d64de9bb2c1cd96240337ba8486300759da Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 1 Jun 2017 16:16:34 -0300 Subject: [PATCH 0057/1145] avoid overflows in computation of step size --- lgc.c | 6 ++++-- llimits.h | 9 ++++++++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/lgc.c b/lgc.c index 4600c43580..fa6cf799a3 100644 --- a/lgc.c +++ b/lgc.c @@ -1,5 +1,5 @@ /* -** $Id: lgc.c,v 2.228 2017/05/04 13:32:01 roberto Exp roberto $ +** $Id: lgc.c,v 2.229 2017/05/26 19:14:29 roberto Exp roberto $ ** Garbage Collector ** See Copyright Notice in lua.h */ @@ -1486,7 +1486,9 @@ void luaC_runtilstate (lua_State *L, int statesmask) { static void incstep (lua_State *L, global_State *g) { int stepmul = (g->gcstepmul | 1); /* avoid division by 0 */ l_mem debt = (g->GCdebt / WORK2MEM) * stepmul; - l_mem stepsize = cast(l_mem, 1) << g->gcstepsize; + l_mem stepsize = (g->gcstepsize <= log2maxs(l_mem)) + ? cast(l_mem, 1) << g->gcstepsize + : MAX_LMEM; stepsize = -((stepsize / WORK2MEM) * stepmul); do { /* repeat until pause or enough "credit" (negative debt) */ lu_mem work = singlestep(L); /* perform one single step */ diff --git a/llimits.h b/llimits.h index 14940550c8..909aba3b15 100644 --- a/llimits.h +++ b/llimits.h @@ -1,5 +1,5 @@ /* -** $Id: llimits.h,v 1.141 2015/11/19 19:16:22 roberto Exp roberto $ +** $Id: llimits.h,v 1.142 2017/04/24 18:06:12 roberto Exp roberto $ ** Limits, basic types, and some other 'installation-dependent' definitions ** See Copyright Notice in lua.h */ @@ -51,6 +51,13 @@ typedef unsigned char lu_byte; #define MAX_INT INT_MAX /* maximum value of an int */ +/* +** floor of the log2 of the maximum signed value for integral type 't'. +** (That is, maximum 'n' such that '2^n' fits in the given signed type.) +*/ +#define log2maxs(t) (sizeof(t) * 8 - 2) + + /* ** conversion of pointer to unsigned integer: ** this is for hashing only; there is no problem if the integer From b029e7ea2070b834e1061492367faa11a1d54e3c Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 1 Jun 2017 17:22:33 -0300 Subject: [PATCH 0058/1145] macro 'luaV_fastget' may need protection ({}) to be used inside 'if's --- lapi.c | 8 +++++--- lvm.c | 14 +++++++++----- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/lapi.c b/lapi.c index 0c109e1eb4..d78d9455b1 100644 --- a/lapi.c +++ b/lapi.c @@ -1,5 +1,5 @@ /* -** $Id: lapi.c,v 2.267 2017/05/18 12:34:58 roberto Exp roberto $ +** $Id: lapi.c,v 2.268 2017/05/26 19:14:29 roberto Exp roberto $ ** Lua API ** See Copyright Notice in lua.h */ @@ -775,8 +775,9 @@ LUA_API void lua_settable (lua_State *L, int idx) { lua_lock(L); api_checknelems(L, 2); t = index2addr(L, idx); - if (luaV_fastget(L, t, L->top - 2, slot, luaH_get)) + if (luaV_fastget(L, t, L->top - 2, slot, luaH_get)) { luaV_finishfastset(L, t, slot, L->top - 1); + } else luaV_finishset(L, t, L->top - 2, L->top - 1, slot); L->top -= 2; /* pop index and value */ @@ -796,8 +797,9 @@ LUA_API void lua_seti (lua_State *L, int idx, lua_Integer n) { lua_lock(L); api_checknelems(L, 1); t = index2addr(L, idx); - if (luaV_fastgeti(L, t, n, slot)) + if (luaV_fastgeti(L, t, n, slot)) { luaV_finishfastset(L, t, slot, L->top - 1); + } else { TValue aux; setivalue(&aux, n); diff --git a/lvm.c b/lvm.c index 9cbb9f45e7..7c15e89979 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.284 2017/05/18 19:44:19 roberto Exp roberto $ +** $Id: lvm.c,v 2.285 2017/05/23 12:50:11 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -916,8 +916,9 @@ void luaV_execute (lua_State *L) { TValue *rb = KB(i); TValue *rc = RKC(i); TString *key = tsvalue(rb); /* key must be a string */ - if (luaV_fastget(L, upval, key, slot, luaH_getshortstr)) + if (luaV_fastget(L, upval, key, slot, luaH_getshortstr)) { luaV_finishfastset(L, upval, slot, rc); + } else Protect(luaV_finishset(L, upval, rb, rc, slot)); vmbreak; @@ -929,8 +930,9 @@ void luaV_execute (lua_State *L) { lua_Unsigned n; if (ttisinteger(rb) /* fast track for integers? */ ? (n = ivalue(rb), luaV_fastgeti(L, ra, n, slot)) - : luaV_fastget(L, ra, rb, slot, luaH_get)) + : luaV_fastget(L, ra, rb, slot, luaH_get)) { luaV_finishfastset(L, ra, slot, rc); + } else Protect(luaV_finishset(L, ra, rb, rc, slot)); vmbreak; @@ -939,8 +941,9 @@ void luaV_execute (lua_State *L) { const TValue *slot; int c = GETARG_B(i); TValue *rc = RKC(i); - if (luaV_fastgeti(L, ra, c, slot)) + if (luaV_fastgeti(L, ra, c, slot)) { luaV_finishfastset(L, ra, slot, rc); + } else { TValue key; setivalue(&key, c); @@ -953,8 +956,9 @@ void luaV_execute (lua_State *L) { TValue *rb = KB(i); TValue *rc = RKC(i); TString *key = tsvalue(rb); /* key must be a string */ - if (luaV_fastget(L, ra, key, slot, luaH_getshortstr)) + if (luaV_fastget(L, ra, key, slot, luaH_getshortstr)) { luaV_finishfastset(L, ra, slot, rc); + } else Protect(luaV_finishset(L, ra, rb, rc, slot)); vmbreak; From dad85e41311646e37d995d677156d6b5daeff329 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 1 Jun 2017 17:23:27 -0300 Subject: [PATCH 0059/1145] macro 'setobj2t' may not be an expression --- lvm.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lvm.h b/lvm.h index 17be5842f8..d682a7a2e8 100644 --- a/lvm.h +++ b/lvm.h @@ -1,5 +1,5 @@ /* -** $Id: lvm.h,v 2.41 2016/12/22 13:08:50 roberto Exp roberto $ +** $Id: lvm.h,v 2.42 2017/05/11 18:57:46 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -79,7 +79,8 @@ ** 'slot' points to the place to put the value. */ #define luaV_finishfastset(L,t,slot,v) \ - (setobj2t(L, cast(TValue *,slot), v), luaC_barrierback(L, hvalue(t), v)) + { setobj2t(L, cast(TValue *,slot), v); \ + luaC_barrierback(L, hvalue(t), v); } From 4bb30f461b146e1d189ee301472953e948699acf Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 1 Jun 2017 17:24:05 -0300 Subject: [PATCH 0060/1145] when assigning to a 'TValue', better assign only exact fields, to allow us to put stuff after the 'TValuefields' if needed --- lobject.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lobject.h b/lobject.h index f8a73a143a..2bef659a07 100644 --- a/lobject.h +++ b/lobject.h @@ -1,5 +1,5 @@ /* -** $Id: lobject.h,v 2.119 2017/04/24 18:06:12 roberto Exp roberto $ +** $Id: lobject.h,v 2.120 2017/04/30 20:43:26 roberto Exp roberto $ ** Type definitions for Lua objects ** See Copyright Notice in lua.h */ @@ -108,7 +108,7 @@ typedef union Value { } Value; -#define TValuefields Value value_; int tt_ +#define TValuefields Value value_; lu_byte tt_ typedef struct lua_TValue { @@ -258,7 +258,8 @@ typedef struct lua_TValue { #define setobj(L,obj1,obj2) \ - { TValue *io1=(obj1); *io1 = *(obj2); \ + { TValue *io1=(obj1); const TValue *io2=(obj2); \ + io1->value_ = io2->value_; io1->tt_ = io2->tt_; \ (void)L; checkliveness(L,io1); } @@ -278,9 +279,8 @@ typedef struct lua_TValue { /* to new object */ #define setobj2n setobj #define setsvalue2n setsvalue - -/* to table (define it as an expression to be used in macros) */ -#define setobj2t(L,o1,o2) ((void)L, *(o1)=*(o2), checkliveness(L,(o1))) +/* to table */ +#define setobj2t setobj From b6f87491afe32140563fe3c546b8812c28a63410 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 9 Jun 2017 13:48:44 -0300 Subject: [PATCH 0061/1145] in hash nodes, keys are stored in separate pieces to avoid wasting space with alignments --- lgc.c | 42 +++++++++-------- llex.c | 6 +-- lobject.h | 98 +++++++++++++++++++++++++++++++--------- ltable.c | 131 +++++++++++++++++++++++++++++++++++------------------- ltable.h | 18 ++------ ltests.c | 16 ++++--- 6 files changed, 201 insertions(+), 110 deletions(-) diff --git a/lgc.c b/lgc.c index fa6cf799a3..36e3436800 100644 --- a/lgc.c +++ b/lgc.c @@ -1,5 +1,5 @@ /* -** $Id: lgc.c,v 2.229 2017/05/26 19:14:29 roberto Exp roberto $ +** $Id: lgc.c,v 2.230 2017/06/01 19:16:34 roberto Exp roberto $ ** Garbage Collector ** See Copyright Notice in lua.h */ @@ -73,7 +73,9 @@ #define valiswhite(x) (iscollectable(x) && iswhite(gcvalue(x))) -#define checkdeadkey(n) lua_assert(!ttisdeadkey(gkey(n)) || ttisnil(gval(n))) +#define keyiswhite(n) (keyiscollectable(n) && iswhite(gckey(n))) + +#define checkdeadkey(n) lua_assert(!keyisdead(n) || ttisnil(gval(n))) #define checkconsistency(obj) \ @@ -83,6 +85,8 @@ #define markvalue(g,o) { checkconsistency(o); \ if (valiswhite(o)) reallymarkobject(g,gcvalue(o)); } +#define markkey(g, n) { if keyiswhite(n) reallymarkobject(g,gckey(n)); } + #define markobject(g,t) { if (iswhite(t)) reallymarkobject(g, obj2gco(t)); } /* @@ -125,8 +129,8 @@ static lu_mem atomic (lua_State *L); */ static void removeentry (Node *n) { lua_assert(ttisnil(gval(n))); - if (valiswhite(gkey(n))) - setdeadvalue(wgkey(n)); /* unused and unmarked key; remove it */ + if (keyiswhite(n)) + setdeadkey(n); /* unused and unmarked key; remove it */ } @@ -137,13 +141,13 @@ static void removeentry (Node *n) { ** other objects: if really collected, cannot keep them; for objects ** being finalized, keep them in keys, but not in values */ -static int iscleared (global_State *g, const TValue *o) { - if (!iscollectable(o)) return 0; - else if (ttisstring(o)) { - markobject(g, tsvalue(o)); /* strings are 'values', so are never weak */ +static int iscleared (global_State *g, const GCObject *o) { + if (o == NULL) return 0; /* non-collectable value */ + else if (novariant(o->tt) == LUA_TSTRING) { + markobject(g, o); /* strings are 'values', so are never weak */ return 0; } - else return iswhite(gcvalue(o)); + else return iswhite(o); } @@ -391,9 +395,9 @@ static void traverseweakvalue (global_State *g, Table *h) { if (ttisnil(gval(n))) /* entry is empty? */ removeentry(n); /* remove it */ else { - lua_assert(!ttisnil(gkey(n))); - markvalue(g, gkey(n)); /* mark key */ - if (!hasclears && iscleared(g, gval(n))) /* is there a white value? */ + lua_assert(!keyisnil(n)); + markkey(g, n); + if (!hasclears && iscleared(g, gcvalueN(gval(n)))) /* a white value? */ hasclears = 1; /* table will have to be cleared */ } } @@ -433,7 +437,7 @@ static int traverseephemeron (global_State *g, Table *h) { checkdeadkey(n); if (ttisnil(gval(n))) /* entry is empty? */ removeentry(n); /* remove it */ - else if (iscleared(g, gkey(n))) { /* key is not marked (yet)? */ + else if (iscleared(g, gckeyN(n))) { /* key is not marked (yet)? */ hasclears = 1; /* table must be cleared */ if (valiswhite(gval(n))) /* value not marked yet? */ hasww = 1; /* white-white entry */ @@ -468,9 +472,9 @@ static void traversestrongtable (global_State *g, Table *h) { if (ttisnil(gval(n))) /* entry is empty? */ removeentry(n); /* remove it */ else { - lua_assert(!ttisnil(gkey(n))); - markvalue(g, gkey(n)); /* mark key */ - markvalue(g, gval(n)); /* mark value */ + lua_assert(!keyisnil(n)); + markkey(g, n); + markvalue(g, gval(n)); } } if (g->gckind == KGC_GEN) { @@ -691,7 +695,7 @@ static void clearkeys (global_State *g, GCObject *l) { Table *h = gco2t(l); Node *n, *limit = gnodelast(h); for (n = gnode(h, 0); n < limit; n++) { - if (!ttisnil(gval(n)) && (iscleared(g, gkey(n)))) { + if (!ttisnil(gval(n)) && (iscleared(g, gckeyN(n)))) { setnilvalue(gval(n)); /* remove value ... */ removeentry(n); /* and remove entry from table */ } @@ -711,11 +715,11 @@ static void clearvalues (global_State *g, GCObject *l, GCObject *f) { unsigned int i; for (i = 0; i < h->sizearray; i++) { TValue *o = &h->array[i]; - if (iscleared(g, o)) /* value was collected? */ + if (iscleared(g, gcvalueN(o))) /* value was collected? */ setnilvalue(o); /* remove value */ } for (n = gnode(h, 0); n < limit; n++) { - if (iscleared(g, gval(n))) { + if (iscleared(g, gcvalueN(gval(n)))) { setnilvalue(gval(n)); /* remove value ... */ removeentry(n); /* and remove entry from table */ } diff --git a/llex.c b/llex.c index 8f44cbef2e..d840a3ca16 100644 --- a/llex.c +++ b/llex.c @@ -1,5 +1,5 @@ /* -** $Id: llex.c,v 2.95 2015/11/19 19:16:22 roberto Exp roberto $ +** $Id: llex.c,v 2.96 2016/05/02 14:02:12 roberto Exp roberto $ ** Lexical Analyzer ** See Copyright Notice in lua.h */ @@ -132,12 +132,12 @@ TString *luaX_newstring (LexState *ls, const char *str, size_t l) { o = luaH_set(L, ls->h, L->top - 1); if (ttisnil(o)) { /* not in use yet? */ /* boolean value does not need GC barrier; - table has no metatable, so it does not need to invalidate cache */ + table is not a metatable, so it does not need to invalidate cache */ setbvalue(o, 1); /* t[string] = true */ luaC_checkGC(L); } else { /* string already present */ - ts = tsvalue(keyfromval(o)); /* re-use value previously stored */ + ts = keystrval(nodefromval(o)); /* re-use value previously stored */ } L->top--; /* remove string from stack */ return ts; diff --git a/lobject.h b/lobject.h index 2bef659a07..97afa143cd 100644 --- a/lobject.h +++ b/lobject.h @@ -1,5 +1,5 @@ /* -** $Id: lobject.h,v 2.120 2017/04/30 20:43:26 roberto Exp roberto $ +** $Id: lobject.h,v 2.121 2017/06/01 20:24:05 roberto Exp roberto $ ** Type definitions for Lua objects ** See Copyright Notice in lua.h */ @@ -122,6 +122,7 @@ typedef struct lua_TValue { #define val_(o) ((o)->value_) +#define valraw(o) (&val_(o)) /* raw type tag of a TValue */ @@ -131,7 +132,8 @@ typedef struct lua_TValue { #define novariant(x) ((x) & 0x0F) /* type tag of a TValue (bits 0-3 for tags + variant bits 4-5) */ -#define ttype(o) (rttype(o) & 0x3F) +#define ttyperaw(t) ((t) & 0x3F) +#define ttype(o) ttyperaw(rttype(o)) /* type tag of a TValue with no variants (bits 0-3) */ #define ttnov(o) (novariant(rttype(o))) @@ -157,7 +159,19 @@ typedef struct lua_TValue { #define ttislcf(o) checktag((o), LUA_TLCF) #define ttisfulluserdata(o) checktag((o), ctb(LUA_TUSERDATA)) #define ttisthread(o) checktag((o), ctb(LUA_TTHREAD)) -#define ttisdeadkey(o) checktag((o), LUA_TDEADKEY) + + +/* +** Macros to access unstructured values (may come both from +** 'TValue's and table keys) +*/ +#define ivalueraw(v) ((v).i) +#define fltvalueraw(v) ((v).n) +#define gcvalueraw(v) ((v).gc) +#define pvalueraw(v) ((v).p) +#define tsvalueraw(v) (gco2ts((v).gc)) +#define fvalueraw(v) ((v).f) +#define bvalueraw(v) ((v).b) /* Macros to access values */ @@ -176,8 +190,6 @@ typedef struct lua_TValue { #define hvalue(o) check_exp(ttistable(o), gco2t(val_(o).gc)) #define bvalue(o) check_exp(ttisboolean(o), val_(o).b) #define thvalue(o) check_exp(ttisthread(o), gco2th(val_(o).gc)) -/* a dead value may get the 'gc' field, but cannot access its contents */ -#define deadvalue(o) check_exp(ttisdeadkey(o), cast(void *, val_(o).gc)) #define l_isfalse(o) (ttisnil(o) || (ttisboolean(o) && bvalue(o) == 0)) @@ -185,6 +197,12 @@ typedef struct lua_TValue { #define iscollectable(o) (rttype(o) & BIT_ISCOLLECTABLE) +/* +** Protected access to objects in values +*/ +#define gcvalueN(o) (iscollectable(o) ? gcvalue(o) : NULL) + + /* Macros for internal tests */ #define righttt(obj) (ttype(obj) == gcvalue(obj)->tt) @@ -253,8 +271,6 @@ typedef struct lua_TValue { val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TTABLE)); \ checkliveness(L,io); } -#define setdeadvalue(obj) settt_(obj, LUA_TDEADKEY) - #define setobj(L,obj1,obj2) \ @@ -485,26 +501,37 @@ typedef union Closure { ** Tables */ -typedef union TKey { - struct { - TValuefields; - int next; /* for chaining (offset for next node) */ - } nk; - TValue tvk; -} TKey; + +/* +** Nodes for Hash tables. A pack of two TValue's (key-value pairs) +** plus a 'next' field to link colliding entries. The distribuition +** of the key's fields ('key_tt' and 'key_val') not forming a proper +** 'TValue' allows for a smaller size for 'Node' both in 4-byte +** and 8-byte alignments. +*/ +typedef union Node { + struct NodeKey { + TValuefields; /* fields for value */ + lu_byte key_tt; /* key type */ + int next; /* for chaining */ + Value key_val; /* key value */ + } u; + TValue i_val; /* direct access to node's value as a proper 'TValue' */ +} Node; -/* copy a value into a key without messing up field 'next' */ -#define setnodekey(L,key,obj) \ - { TKey *k_=(key); const TValue *io_=(obj); \ - k_->nk.value_ = io_->value_; k_->nk.tt_ = io_->tt_; \ +/* copy a value into a key */ +#define setnodekey(L,node,obj) \ + { Node *n_=(node); const TValue *io_=(obj); \ + n_->u.key_val = io_->value_; n_->u.key_tt = io_->tt_; \ (void)L; checkliveness(L,io_); } -typedef struct Node { - TValue i_val; - TKey i_key; -} Node; +/* copy a value from a key */ +#define getnodekey(L,obj,node) \ + { TValue *io_=(obj); const Node *n_=(node); \ + io_->value_ = n_->u.key_val; io_->tt_ = n_->u.key_tt; \ + (void)L; checkliveness(L,io_); } typedef struct Table { @@ -520,6 +547,33 @@ typedef struct Table { } Table; +/* +** Macros to manipulate keys inserted in nodes +*/ +#define keytt(node) ((node)->u.key_tt) +#define keyval(node) ((node)->u.key_val) + +#define keyisnil(node) (keytt(node) == LUA_TNIL) +#define keyisinteger(node) (keytt(node) == LUA_TNUMINT) +#define keyival(node) (keyval(node).i) +#define keyisshrstr(node) (keytt(node) == ctb(LUA_TSHRSTR)) +#define keystrval(node) (gco2ts(keyval(node).gc)) + +#define keyisdead(node) (keytt(node) == LUA_TDEADKEY) + +#define setnilkey(node) (keytt(node) = LUA_TNIL) +#define setdeadkey(node) (keytt(node) = LUA_TDEADKEY) + +/* a dead value may get the 'gc' field, but cannot access its contents */ +#define deadkey(n) \ + check_exp(keytt(n) == LUA_TDEADKEY, cast(void *, keyval(n).gc)) + +#define keyiscollectable(n) (keytt(n) & BIT_ISCOLLECTABLE) + +#define gckey(n) (keyval(n).gc) +#define gckeyN(n) (keyiscollectable(n) ? gckey(n) : NULL) + + /* ** 'module' operation for hashing (size is always a power of 2) diff --git a/ltable.c b/ltable.c index 4ce33319cd..498eb1214f 100644 --- a/ltable.c +++ b/ltable.c @@ -1,5 +1,5 @@ /* -** $Id: ltable.c,v 2.121 2017/05/19 12:47:00 roberto Exp roberto $ +** $Id: ltable.c,v 2.122 2017/05/19 12:57:10 roberto Exp roberto $ ** Lua tables (hash) ** See Copyright Notice in lua.h */ @@ -75,8 +75,8 @@ #define dummynode (&dummynode_) static const Node dummynode_ = { - {NILCONSTANT}, /* value */ - {{NILCONSTANT, 0}} /* key */ + {{NULL}, LUA_TNIL, /* value's value and type */ + LUA_TNIL, 0, {NULL}} /* key type, next, and key value */ }; @@ -111,43 +111,79 @@ static int l_hashfloat (lua_Number n) { /* -** returns the 'main' position of an element in a table (that is, the index -** of its hash value) +** returns the 'main' position of an element in a table (that is, +** the index of its hash value). The key comes broken (tag in 'ktt' +** and value in 'vkl') so that we can call it on keys inserted into +** nodes. */ -static Node *mainposition (const Table *t, const TValue *key) { - switch (ttype(key)) { +static Node *mainposition (const Table *t, int ktt, const Value *kvl) { + switch (ttyperaw(ktt)) { case LUA_TNUMINT: - return hashint(t, ivalue(key)); + return hashint(t, ivalueraw(*kvl)); case LUA_TNUMFLT: - return hashmod(t, l_hashfloat(fltvalue(key))); + return hashmod(t, l_hashfloat(fltvalueraw(*kvl))); case LUA_TSHRSTR: - return hashstr(t, tsvalue(key)); + return hashstr(t, tsvalueraw(*kvl)); case LUA_TLNGSTR: - return hashpow2(t, luaS_hashlongstr(tsvalue(key))); + return hashpow2(t, luaS_hashlongstr(tsvalueraw(*kvl))); case LUA_TBOOLEAN: - return hashboolean(t, bvalue(key)); + return hashboolean(t, bvalueraw(*kvl)); case LUA_TLIGHTUSERDATA: - return hashpointer(t, pvalue(key)); + return hashpointer(t, pvalueraw(*kvl)); case LUA_TLCF: - return hashpointer(t, fvalue(key)); + return hashpointer(t, fvalueraw(*kvl)); default: - lua_assert(!ttisdeadkey(key)); - return hashpointer(t, gcvalue(key)); + return hashpointer(t, gcvalueraw(*kvl)); } } +static Node *mainpositionTV (const Table *t, const TValue *key) { + return mainposition(t, rttype(key), valraw(key)); +} + + /* -** returns the index for 'key' if 'key' is an appropriate key to live in -** the array part of the table, 0 otherwise. +** Check whether key 'k1' is equal to the key in node 'n2'. +** This equality is raw, so there are no metamethods. Floats +** with integer values have been normalized, so integers cannot +** be equal to floats. It is assumed that 'eqshrstr' is simply +** pointer equality, so that short strings are handled in the +** default case. */ -static unsigned int arrayindex (const TValue *key) { - if (ttisinteger(key)) { - lua_Integer k = ivalue(key); - if (0 < k && (lua_Unsigned)k <= MAXASIZE) - return cast(unsigned int, k); /* 'key' is an appropriate array index */ +static int equalkey (const TValue *k1, const Node *n2) { + if (rttype(k1) != keytt(n2)) /* not the same variants? */ + return 0; /* cannot be same key */ + switch (ttype(k1)) { + case LUA_TNIL: + return 1; + case LUA_TNUMINT: + return (ivalue(k1) == keyival(n2)); + case LUA_TNUMFLT: + return luai_numeq(fltvalue(k1), fltvalueraw(keyval(n2))); + case LUA_TBOOLEAN: + return bvalue(k1) == bvalueraw(keyval(n2)); + case LUA_TLIGHTUSERDATA: + return pvalue(k1) == pvalueraw(keyval(n2)); + case LUA_TLCF: + return fvalue(k1) == fvalueraw(keyval(n2)); + case LUA_TLNGSTR: + return luaS_eqlngstr(tsvalue(k1), keystrval(n2)); + default: + return gcvalue(k1) == gcvalueraw(keyval(n2)); } - return 0; /* 'key' did not match some condition */ +} + + +/* +** returns the index for 'k' if 'k' is an appropriate key to live in +** the array part of a table, 0 otherwise. +*/ +static unsigned int arrayindex (lua_Integer k) { + if (0 < k && l_castS2U(k) <= MAXASIZE) + return cast(unsigned int, k); /* 'key' is an appropriate array index */ + else + return 0; } @@ -159,17 +195,17 @@ static unsigned int arrayindex (const TValue *key) { static unsigned int findindex (lua_State *L, Table *t, StkId key) { unsigned int i; if (ttisnil(key)) return 0; /* first iteration */ - i = arrayindex(key); + i = ttisinteger(key) ? arrayindex(ivalue(key)) : 0; if (i != 0 && i <= t->sizearray) /* is 'key' inside array part? */ return i; /* yes; that's the index */ else { int nx; - Node *n = mainposition(t, key); + Node *n = mainpositionTV(t, key); for (;;) { /* check whether 'key' is somewhere in the chain */ /* key may be dead already, but it is ok to use it in 'next' */ - if (luaV_rawequalobj(gkey(n), key) || - (ttisdeadkey(gkey(n)) && iscollectable(key) && - deadvalue(gkey(n)) == gcvalue(key))) { + if (equalkey(key, n) || + (keyisdead(n) && iscollectable(key) && + deadkey(n) == gcvalue(key))) { i = cast_int(n - gnode(t, 0)); /* key index in hash table */ /* hash elements are numbered after array ones */ return (i + 1) + t->sizearray; @@ -194,8 +230,9 @@ int luaH_next (lua_State *L, Table *t, StkId key) { } for (i -= t->sizearray; cast_int(i) < sizenode(t); i++) { /* hash part */ if (!ttisnil(gval(gnode(t, i)))) { /* a non-nil value? */ - setobj2s(L, key, gkey(gnode(t, i))); - setobj2s(L, key+1, gval(gnode(t, i))); + Node *n = gnode(t, i); + getnodekey(L, key, n); + setobj2s(L, key + 1, gval(n)); return 1; } } @@ -239,7 +276,7 @@ static unsigned int computesizes (unsigned int nums[], unsigned int *pna) { } -static int countint (const TValue *key, unsigned int *nums) { +static int countint (lua_Integer key, unsigned int *nums) { unsigned int k = arrayindex(key); if (k != 0) { /* is 'key' an appropriate array index? */ nums[luaO_ceillog2(k)]++; /* count as such */ @@ -288,7 +325,8 @@ static int numusehash (const Table *t, unsigned int *nums, unsigned int *pna) { while (i--) { Node *n = &t->node[i]; if (!ttisnil(gval(n))) { - ause += countint(gkey(n), nums); + if (keyisinteger(n)) + ause += countint(keyival(n), nums); totaluse++; } } @@ -322,7 +360,7 @@ static void setnodevector (lua_State *L, Table *t, unsigned int size) { for (i = 0; i < (int)size; i++) { Node *n = gnode(t, i); gnext(n) = 0; - setnilvalue(wgkey(n)); + setnilkey(n); setnilvalue(gval(n)); } t->lsizenode = cast_byte(lsize); @@ -358,7 +396,8 @@ void luaH_resize (lua_State *L, Table *t, unsigned int nasize, if (!ttisnil(gval(old))) { /* doesn't need barrier/invalidate cache, as entry was already present in the table */ - setobjt2t(L, luaH_set(L, t, gkey(old)), gval(old)); + TValue k; getnodekey(L, &k, old); + setobjt2t(L, luaH_set(L, t, &k), gval(old)); } } if (oldhsize > 0) /* not the dummy node? */ @@ -385,7 +424,8 @@ static void rehash (lua_State *L, Table *t, const TValue *ek) { totaluse = na; /* all those keys are integer keys */ totaluse += numusehash(t, nums, &na); /* count keys in hash part */ /* count extra key */ - na += countint(ek, nums); + if (ttisinteger(ek)) + na += countint(ivalue(ek), nums); totaluse++; /* compute new size for array part */ asize = computesizes(nums, &na); @@ -424,7 +464,7 @@ static Node *getfreepos (Table *t) { if (!isdummy(t)) { while (t->lastfree > t->node) { t->lastfree--; - if (ttisnil(gkey(t->lastfree))) + if (keyisnil(t->lastfree)) return t->lastfree; } } @@ -453,7 +493,7 @@ TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) { else if (luai_numisnan(fltvalue(key))) luaG_runerror(L, "table index is NaN"); } - mp = mainposition(t, key); + mp = mainpositionTV(t, key); if (!ttisnil(gval(mp)) || isdummy(t)) { /* main position is taken? */ Node *othern; Node *f = getfreepos(t); /* get a free place */ @@ -463,7 +503,7 @@ TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) { return luaH_set(L, t, key); /* insert key into grown table */ } lua_assert(!isdummy(t)); - othern = mainposition(t, gkey(mp)); + othern = mainposition(t, keytt(mp), &keyval(mp)); if (othern != mp) { /* is colliding node out of its main position? */ /* yes; move colliding node into free position */ while (othern + gnext(othern) != mp) /* find previous */ @@ -485,7 +525,7 @@ TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) { mp = f; } } - setnodekey(L, &mp->i_key, key); + setnodekey(L, mp, key); luaC_barrierback(L, t, key); lua_assert(ttisnil(gval(mp))); return gval(mp); @@ -502,7 +542,7 @@ const TValue *luaH_getint (Table *t, lua_Integer key) { else { Node *n = hashint(t, key); for (;;) { /* check whether 'key' is somewhere in the chain */ - if (ttisinteger(gkey(n)) && ivalue(gkey(n)) == key) + if (keyisinteger(n) && keyival(n) == key) return gval(n); /* that's it */ else { int nx = gnext(n); @@ -522,8 +562,7 @@ const TValue *luaH_getshortstr (Table *t, TString *key) { Node *n = hashstr(t, key); lua_assert(key->tt == LUA_TSHRSTR); for (;;) { /* check whether 'key' is somewhere in the chain */ - const TValue *k = gkey(n); - if (ttisshrstring(k) && eqshrstr(tsvalue(k), key)) + if (keyisshrstr(n) && eqshrstr(keystrval(n), key)) return gval(n); /* that's it */ else { int nx = gnext(n); @@ -540,9 +579,9 @@ const TValue *luaH_getshortstr (Table *t, TString *key) { ** which may be in array part, nor for floats with integral values.) */ static const TValue *getgeneric (Table *t, const TValue *key) { - Node *n = mainposition(t, key); + Node *n = mainpositionTV(t, key); for (;;) { /* check whether 'key' is somewhere in the chain */ - if (luaV_rawequalobj(gkey(n), key)) + if (equalkey(key, n)) return gval(n); /* that's it */ else { int nx = gnext(n); @@ -683,7 +722,7 @@ lua_Unsigned luaH_getn (Table *t) { #if defined(LUA_DEBUG) Node *luaH_mainposition (const Table *t, const TValue *key) { - return mainposition(t, key); + return mainpositionTV(t, key); } int luaH_isdummy (const Table *t) { return isdummy(t); } diff --git a/ltable.h b/ltable.h index ee22cf62cb..88f906361b 100644 --- a/ltable.h +++ b/ltable.h @@ -1,5 +1,5 @@ /* -** $Id: ltable.h,v 2.23 2016/12/22 13:08:50 roberto Exp roberto $ +** $Id: ltable.h,v 2.24 2017/05/19 12:48:15 roberto Exp roberto $ ** Lua tables (hash) ** See Copyright Notice in lua.h */ @@ -12,18 +12,9 @@ #define gnode(t,i) (&(t)->node[i]) #define gval(n) (&(n)->i_val) -#define gnext(n) ((n)->i_key.nk.next) +#define gnext(n) ((n)->u.next) -/* 'const' to avoid wrong writings that can mess up field 'next' */ -#define gkey(n) cast(const TValue*, (&(n)->i_key.tvk)) - -/* -** writable version of 'gkey'; allows updates to individual fields, -** but not to the whole (which has incompatible type) -*/ -#define wgkey(n) (&(n)->i_key.nk) - #define invalidateTMcache(t) ((t)->flags = 0) @@ -35,9 +26,8 @@ #define allocsizenode(t) (isdummy(t) ? 0 : sizenode(t)) -/* returns the key, given the value of a table entry */ -#define keyfromval(v) \ - (gkey(cast(Node *, cast(char *, (v)) - offsetof(Node, i_val)))) +/* returns the Node, given the value of a table entry */ +#define nodefromval(v) cast(Node *, (v)) LUAI_FUNC const TValue *luaH_getint (Table *t, lua_Integer key); diff --git a/ltests.c b/ltests.c index fc233c7fac..a9c93132e1 100644 --- a/ltests.c +++ b/ltests.c @@ -1,5 +1,5 @@ /* -** $Id: ltests.c,v 2.217 2017/05/04 13:32:01 roberto Exp $ +** $Id: ltests.c,v 2.218 2017/05/31 18:54:58 roberto Exp roberto $ ** Internal Module for Debugging of the Lua Implementation ** See Copyright Notice in lua.h */ @@ -253,8 +253,10 @@ static void checktable (global_State *g, Table *h) { checkvalref(g, hgc, &h->array[i]); for (n = gnode(h, 0); n < limit; n++) { if (!ttisnil(gval(n))) { - lua_assert(!ttisnil(gkey(n))); - checkvalref(g, hgc, gkey(n)); + TValue k; + getnodekey(g->mainthread, &k, n); + lua_assert(!keyisnil(n)); + checkvalref(g, hgc, &k); checkvalref(g, hgc, gval(n)); } } @@ -802,10 +804,12 @@ static int table_query (lua_State *L) { lua_pushnil(L); } else if ((i -= t->sizearray) < sizenode(t)) { + TValue k; + getnodekey(L, &k, gnode(t, i)); if (!ttisnil(gval(gnode(t, i))) || - ttisnil(gkey(gnode(t, i))) || - ttisnumber(gkey(gnode(t, i)))) { - pushobject(L, gkey(gnode(t, i))); + ttisnil(&k) || + ttisnumber(&k)) { + pushobject(L, &k); } else lua_pushliteral(L, ""); From d13a3fb070fd0ec9ec3b85f88e0fcd914aa666d3 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 9 Jun 2017 16:16:41 -0300 Subject: [PATCH 0062/1145] detail (removed empty spaces at the end of lines) --- lvm.c | 4 ++-- lvm.h | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lvm.c b/lvm.c index 7c15e89979..8533af63ae 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.285 2017/05/23 12:50:11 roberto Exp roberto $ +** $Id: lvm.c,v 2.286 2017/06/01 20:22:33 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -1439,7 +1439,7 @@ void luaV_execute (lua_State *L) { } vmcase(OP_VARARG) { int b = GETARG_B(i) - 1; /* required results */ - StkId vtab = base + cl->p->numparams - 1; /* vararg table */ + StkId vtab = base + cl->p->numparams - 1; /* vararg table */ Protect(luaT_getvarargs(L, vtab, ra, b)); vmbreak; } diff --git a/lvm.h b/lvm.h index d682a7a2e8..3cf1f869db 100644 --- a/lvm.h +++ b/lvm.h @@ -1,5 +1,5 @@ /* -** $Id: lvm.h,v 2.42 2017/05/11 18:57:46 roberto Exp roberto $ +** $Id: lvm.h,v 2.43 2017/06/01 20:23:27 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -76,7 +76,7 @@ /* ** Finish a fast set operation (when fast get succeeds). In that case, -** 'slot' points to the place to put the value. +** 'slot' points to the place to put the value. */ #define luaV_finishfastset(L,t,slot,v) \ { setobj2t(L, cast(TValue *,slot), v); \ From 73ec04fcf3e3f7017786fbaf0a83291b22bec5c4 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 12 Jun 2017 11:21:44 -0300 Subject: [PATCH 0063/1145] no more 'DEADKEY'. Table traversals do not need to consider dead keys; if the key is dead, it cannot be given to 'next'. Instead, we now use a 'table' tag without the collectable bit, which makes it a unique tag good enough to reserve space. --- lgc.c | 21 ++++++------------ lobject.h | 22 ++++++++++--------- lstate.h | 5 ++--- ltable.c | 64 +++++++++++++++++++++++-------------------------------- ltests.c | 3 +-- 5 files changed, 49 insertions(+), 66 deletions(-) diff --git a/lgc.c b/lgc.c index 36e3436800..18a051420f 100644 --- a/lgc.c +++ b/lgc.c @@ -1,5 +1,5 @@ /* -** $Id: lgc.c,v 2.230 2017/06/01 19:16:34 roberto Exp roberto $ +** $Id: lgc.c,v 2.231 2017/06/09 16:48:44 roberto Exp roberto $ ** Garbage Collector ** See Copyright Notice in lua.h */ @@ -75,8 +75,6 @@ #define keyiswhite(n) (keyiscollectable(n) && iswhite(gckey(n))) -#define checkdeadkey(n) lua_assert(!keyisdead(n) || ttisnil(gval(n))) - #define checkconsistency(obj) \ lua_longassert(!iscollectable(obj) || righttt(obj)) @@ -119,13 +117,11 @@ static lu_mem atomic (lua_State *L); /* -** If key is not marked, mark its entry as dead. This allows key to be -** collected, but keeps its entry in the table. A dead node is needed -** when Lua looks up for a key (it may be part of a chain) and when -** traversing a weak table (key might be removed from the table during -** traversal). Other places never manipulate dead keys, because its -** associated nil value is enough to signal that the entry is logically -** empty. +** If key is not marked, mark its entry as dead. This allows the +** collection of the key, but keeps its entry in the table (its removal +** could break a chain). Other places never manipulate dead keys, +** because its associated nil value is enough to signal that the entry +** is logically empty. */ static void removeentry (Node *n) { lua_assert(ttisnil(gval(n))); @@ -203,7 +199,7 @@ LUAI_FUNC void luaC_protobarrier_ (lua_State *L, Proto *p) { lua_assert(g->gckind != KGC_GEN || isold(p)); if (getage(p) == G_OLD1) /* still need to be visited? */ linkgclist(p, g->grayagain); /* link it in 'grayagain' */ - else + else linkgclist(p, g->protogray); /* link it in 'protogray' */ black2gray(p); /* make prototype gray (to avoid other barriers) */ } @@ -391,7 +387,6 @@ static void traverseweakvalue (global_State *g, Table *h) { worth traversing it now just to check) */ int hasclears = (h->sizearray > 0); for (n = gnode(h, 0); n < limit; n++) { /* traverse hash part */ - checkdeadkey(n); if (ttisnil(gval(n))) /* entry is empty? */ removeentry(n); /* remove it */ else { @@ -434,7 +429,6 @@ static int traverseephemeron (global_State *g, Table *h) { } /* traverse hash part */ for (n = gnode(h, 0); n < limit; n++) { - checkdeadkey(n); if (ttisnil(gval(n))) /* entry is empty? */ removeentry(n); /* remove it */ else if (iscleared(g, gckeyN(n))) { /* key is not marked (yet)? */ @@ -468,7 +462,6 @@ static void traversestrongtable (global_State *g, Table *h) { for (i = 0; i < h->sizearray; i++) /* traverse array part */ markvalue(g, &h->array[i]); for (n = gnode(h, 0); n < limit; n++) { /* traverse hash part */ - checkdeadkey(n); if (ttisnil(gval(n))) /* entry is empty? */ removeentry(n); /* remove it */ else { diff --git a/lobject.h b/lobject.h index 97afa143cd..e245f306c4 100644 --- a/lobject.h +++ b/lobject.h @@ -1,5 +1,5 @@ /* -** $Id: lobject.h,v 2.121 2017/06/01 20:24:05 roberto Exp roberto $ +** $Id: lobject.h,v 2.122 2017/06/09 16:48:44 roberto Exp roberto $ ** Type definitions for Lua objects ** See Copyright Notice in lua.h */ @@ -21,10 +21,9 @@ */ #define LUA_TUPVAL LUA_NUMTAGS /* upvalues */ #define LUA_TPROTO (LUA_NUMTAGS+1) /* function prototypes */ -#define LUA_TDEADKEY (LUA_NUMTAGS+2) /* removed keys in tables */ /* -** number of all possible tags (including LUA_TNONE but excluding DEADKEY) +** number of all possible tags (including LUA_TNONE) */ #define LUA_TOTALTAGS (LUA_TPROTO + 2) @@ -559,14 +558,7 @@ typedef struct Table { #define keyisshrstr(node) (keytt(node) == ctb(LUA_TSHRSTR)) #define keystrval(node) (gco2ts(keyval(node).gc)) -#define keyisdead(node) (keytt(node) == LUA_TDEADKEY) - #define setnilkey(node) (keytt(node) = LUA_TNIL) -#define setdeadkey(node) (keytt(node) = LUA_TDEADKEY) - -/* a dead value may get the 'gc' field, but cannot access its contents */ -#define deadkey(n) \ - check_exp(keytt(n) == LUA_TDEADKEY, cast(void *, keyval(n).gc)) #define keyiscollectable(n) (keytt(n) & BIT_ISCOLLECTABLE) @@ -574,6 +566,16 @@ typedef struct Table { #define gckeyN(n) (keyiscollectable(n) ? gckey(n) : NULL) +/* +** Use a "nil table" to mark dead keys in a table. Those keys serve +** only to keep space for removed entries, which may still be part of +** chains. Note that the 'keytt' does not have the BIT_ISCOLLECTABLE +** set, so these values are considered not collectable and are different +** from any valid value. +*/ +#define setdeadkey(n) (keytt(n) = LUA_TTABLE, gckey(n) = NULL) + + /* ** 'module' operation for hashing (size is always a power of 2) diff --git a/lstate.h b/lstate.h index 9a3e778a85..12dddf3b36 100644 --- a/lstate.h +++ b/lstate.h @@ -1,5 +1,5 @@ /* -** $Id: lstate.h,v 2.141 2017/05/13 13:54:47 roberto Exp roberto $ +** $Id: lstate.h,v 2.142 2017/05/26 19:14:29 roberto Exp roberto $ ** Global State ** See Copyright Notice in lua.h */ @@ -247,8 +247,7 @@ union GCUnion { /* macro to convert a Lua object into a GCObject */ -#define obj2gco(v) \ - check_exp(novariant((v)->tt) < LUA_TDEADKEY, (&(cast_u(v)->gc))) +#define obj2gco(v) (&(cast_u(v)->gc)) /* actual number of total bytes allocated */ diff --git a/ltable.c b/ltable.c index 498eb1214f..b8244dca3a 100644 --- a/ltable.c +++ b/ltable.c @@ -1,5 +1,5 @@ /* -** $Id: ltable.c,v 2.122 2017/05/19 12:57:10 roberto Exp roberto $ +** $Id: ltable.c,v 2.123 2017/06/09 16:48:44 roberto Exp roberto $ ** Lua tables (hash) ** See Copyright Notice in lua.h */ @@ -175,6 +175,25 @@ static int equalkey (const TValue *k1, const Node *n2) { } +/* +** "Generic" get version. (Not that generic: not valid for integers, +** which may be in array part, nor for floats with integral values.) +*/ +static const TValue *getgeneric (Table *t, const TValue *key) { + Node *n = mainpositionTV(t, key); + for (;;) { /* check whether 'key' is somewhere in the chain */ + if (equalkey(key, n)) + return gval(n); /* that's it */ + else { + int nx = gnext(n); + if (nx == 0) + return luaO_nilobject; /* not found */ + n += nx; + } + } +} + + /* ** returns the index for 'k' if 'k' is an appropriate key to live in ** the array part of a table, 0 otherwise. @@ -199,22 +218,12 @@ static unsigned int findindex (lua_State *L, Table *t, StkId key) { if (i != 0 && i <= t->sizearray) /* is 'key' inside array part? */ return i; /* yes; that's the index */ else { - int nx; - Node *n = mainpositionTV(t, key); - for (;;) { /* check whether 'key' is somewhere in the chain */ - /* key may be dead already, but it is ok to use it in 'next' */ - if (equalkey(key, n) || - (keyisdead(n) && iscollectable(key) && - deadkey(n) == gcvalue(key))) { - i = cast_int(n - gnode(t, 0)); /* key index in hash table */ - /* hash elements are numbered after array ones */ - return (i + 1) + t->sizearray; - } - nx = gnext(n); - if (nx == 0) - luaG_runerror(L, "invalid key to 'next'"); /* key not found */ - else n += nx; - } + const TValue *n = getgeneric(t, key); + if (n == luaO_nilobject) + luaG_runerror(L, "invalid key to 'next'"); /* key not found */ + i = cast_int(nodefromval(n) - gnode(t, 0)); /* key index in hash table */ + /* hash elements are numbered after array ones */ + return (i + 1) + t->sizearray; } } @@ -574,25 +583,6 @@ const TValue *luaH_getshortstr (Table *t, TString *key) { } -/* -** "Generic" get version. (Not that generic: not valid for integers, -** which may be in array part, nor for floats with integral values.) -*/ -static const TValue *getgeneric (Table *t, const TValue *key) { - Node *n = mainpositionTV(t, key); - for (;;) { /* check whether 'key' is somewhere in the chain */ - if (equalkey(key, n)) - return gval(n); /* that's it */ - else { - int nx = gnext(n); - if (nx == 0) - return luaO_nilobject; /* not found */ - n += nx; - } - } -} - - const TValue *luaH_getstr (Table *t, TString *key) { if (key->tt == LUA_TSHRSTR) return luaH_getshortstr(t, key); @@ -662,7 +652,7 @@ void luaH_setint (lua_State *L, Table *t, lua_Integer key, TValue *value) { ** absent while 'i' is present; so 'j > i'.) Otherwise, 'j' is a ** boundary. ('j + 1' cannot be a present integer key because it is ** not a valid integer in Lua.) -*/ +*/ static lua_Unsigned hash_search (Table *t, lua_Unsigned j) { lua_Unsigned i; if (j == 0) j++; /* the caller ensures 'j + 1' is present */ diff --git a/ltests.c b/ltests.c index a9c93132e1..733e47d498 100644 --- a/ltests.c +++ b/ltests.c @@ -1,5 +1,5 @@ /* -** $Id: ltests.c,v 2.218 2017/05/31 18:54:58 roberto Exp roberto $ +** $Id: ltests.c,v 2.219 2017/06/09 16:48:44 roberto Exp roberto $ ** Internal Module for Debugging of the Lua Implementation ** See Copyright Notice in lua.h */ @@ -449,7 +449,6 @@ static void markgrays (global_State *g) { checkgraylist(g, g->grayagain); checkgraylist(g, g->weak); checkgraylist(g, g->ephemeron); - checkgraylist(g, g->allweak); checkgraylist(g, g->protogray); } From 60a7492d249860d20098ac2f0b9d863606c38450 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 27 Jun 2017 08:35:01 -0300 Subject: [PATCH 0064/1145] new type 'ls_byte' for signed bytes --- llimits.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/llimits.h b/llimits.h index 909aba3b15..4b35dfcb8f 100644 --- a/llimits.h +++ b/llimits.h @@ -1,5 +1,5 @@ /* -** $Id: llimits.h,v 1.142 2017/04/24 18:06:12 roberto Exp roberto $ +** $Id: llimits.h,v 1.143 2017/06/01 19:16:34 roberto Exp roberto $ ** Limits, basic types, and some other 'installation-dependent' definitions ** See Copyright Notice in lua.h */ @@ -33,6 +33,7 @@ typedef long l_mem; /* chars used as small naturals (so that 'char' is reserved for characters) */ typedef unsigned char lu_byte; +typedef signed char ls_byte; /* maximum value for size_t */ From b42430fd3a6200eaaf4020be90c4d47f7e251b67 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 27 Jun 2017 08:35:31 -0300 Subject: [PATCH 0065/1145] 'lineinfo' in prototypes saved as differences instead of absolute values, so that the array can use bytes instead of ints, reducing its size. (A new array 'abslineinfo' is used when line differences do not fit in a byte.) --- lcode.c | 52 ++++++++++++++++++++++++++----- ldebug.c | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++----- ldebug.h | 10 ++++-- ldump.c | 8 ++++- lfunc.c | 5 ++- lobject.h | 25 ++++++++++++--- lparser.c | 13 +++++--- lparser.h | 5 ++- ltests.c | 4 +-- ltests.h | 3 +- lundump.c | 11 +++++-- 11 files changed, 195 insertions(+), 33 deletions(-) diff --git a/lcode.c b/lcode.c index 2d39600024..740cf564a6 100644 --- a/lcode.c +++ b/lcode.c @@ -1,5 +1,5 @@ /* -** $Id: lcode.c,v 2.118 2017/04/28 20:57:45 roberto Exp roberto $ +** $Id: lcode.c,v 2.119 2017/05/18 19:44:19 roberto Exp roberto $ ** Code generator for Lua ** See Copyright Notice in lua.h */ @@ -10,6 +10,7 @@ #include "lprefix.h" +#include #include #include @@ -285,6 +286,33 @@ void luaK_patchclose (FuncState *fs, int list, int level) { } } +#if !defined(MAXIWTHABS) +#define MAXIWTHABS 120 +#endif + +/* +** Save line info for a new instruction. If difference from last line +** does not fit in a byte, of after that many instructions, save a new +** absolute line info; (in that case, the special value 'ABSLINEINFO' +** in 'lineinfo' signals the existence of this absolute information.) +** Otherwise, store the difference from last line in 'lineinfo'. +*/ +static void savelineinfo (FuncState *fs, Proto *f, int pc, int line) { + int linedif = line - fs->previousline; + if (abs(linedif) >= 0x80 || fs->iwthabs++ > MAXIWTHABS) { + luaM_growvector(fs->ls->L, f->abslineinfo, fs->nabslineinfo, + f->sizeabslineinfo, AbsLineInfo, MAX_INT, "lines"); + f->abslineinfo[fs->nabslineinfo].pc = pc; + f->abslineinfo[fs->nabslineinfo++].line = line; + linedif = ABSLINEINFO; /* signal there is absolute information */ + fs->iwthabs = 0; /* restart counter */ + } + luaM_growvector(fs->ls->L, f->lineinfo, pc, f->sizelineinfo, ls_byte, + MAX_INT, "opcodes"); + f->lineinfo[pc] = linedif; + fs->previousline = line; /* last line saved */ +} + /* ** Emit instruction 'i', checking for array sizes and saving also its @@ -297,10 +325,7 @@ static int luaK_code (FuncState *fs, Instruction i) { luaM_growvector(fs->ls->L, f->code, fs->pc, f->sizecode, Instruction, MAX_INT, "opcodes"); f->code[fs->pc] = i; - /* save corresponding line information */ - luaM_growvector(fs->ls->L, f->lineinfo, fs->pc, f->sizelineinfo, int, - MAX_INT, "opcodes"); - f->lineinfo[fs->pc] = fs->ls->lastline; + savelineinfo(fs, f, fs->pc, fs->ls->lastline); return fs->pc++; } @@ -1260,10 +1285,23 @@ void luaK_posfix (FuncState *fs, BinOpr op, /* -** Change line information associated with current position. +** Change line information associated with current position. If that +** information is absolute, just change it and correct 'previousline'. +** Otherwise, restore 'previousline' to its value before saving the +** current position and than saves the line information again, with the +** new line. */ void luaK_fixline (FuncState *fs, int line) { - fs->f->lineinfo[fs->pc - 1] = line; + Proto *f = fs->f; + if (f->lineinfo[fs->pc - 1] == ABSLINEINFO) { + lua_assert(f->abslineinfo[fs->nabslineinfo - 1].pc == fs->pc - 1); + f->abslineinfo[fs->nabslineinfo - 1].line = line; + fs->previousline = line; + } + else { + fs->previousline -= f->lineinfo[fs->pc - 1]; /* undo previous info. */ + savelineinfo(fs, f, fs->pc - 1, line); /* redo it */ + } } diff --git a/ldebug.c b/ldebug.c index 4193cda8c6..6971e475bf 100644 --- a/ldebug.c +++ b/ldebug.c @@ -1,5 +1,5 @@ /* -** $Id: ldebug.c,v 2.125 2017/05/13 13:04:33 roberto Exp roberto $ +** $Id: ldebug.c,v 2.126 2017/05/13 13:54:47 roberto Exp roberto $ ** Debug Interface ** See Copyright Notice in lua.h */ @@ -48,8 +48,61 @@ static int currentpc (CallInfo *ci) { } +/* +** Get a "base line" to find the line corresponding to an instruction. +** For that, search the array of absolute line info for the largest saved +** instruction smaller or equal to the wanted instrution. A special +** case is when there is no absolute info or the instruction is before +** the first absolute one. +*/ +static int getbaseline (Proto *f, int pc, int *basepc) { + if (f->sizeabslineinfo == 0 || pc < f->abslineinfo[0].pc) { + *basepc = -1; /* start from the beginning */ + return f->linedefined; + } + else { + unsigned int i; + if (pc >= f->abslineinfo[f->sizeabslineinfo - 1].pc) + i = f->sizeabslineinfo - 1; /* instruction is after last saved one */ + else { /* binary search */ + unsigned int j = f->sizeabslineinfo - 1; /* pc < anchorlines[j] */ + i = 0; /* abslineinfo[i] <= pc */ + while (i < j - 1) { + unsigned int m = (j + i) / 2; + if (pc >= f->abslineinfo[m].pc) + i = m; + else + j = m; + } + } + *basepc = f->abslineinfo[i].pc; + return f->abslineinfo[i].line; + } +} + + +/* +** Get the line corresponding to instruction 'pc' in function 'f'; +** first gets a base line and from there does the increments until +** the desired instruction. +*/ +int luaG_getfuncline (Proto *f, int pc) { + if (f->lineinfo == NULL) /* no debug information? */ + return -1; + else { + int basepc; + int baseline = getbaseline(f, pc, &basepc); + while (basepc++ < pc) { /* walk until given instruction */ + lua_assert(f->lineinfo[basepc] != ABSLINEINFO); + baseline += f->lineinfo[basepc]; /* correct line */ + } + return baseline; + } +} + + static int currentline (CallInfo *ci) { - return getfuncline(ci_func(ci)->p, currentpc(ci)); + return luaG_getfuncline(ci_func(ci)->p, currentpc(ci)); } @@ -211,6 +264,14 @@ static void funcinfo (lua_Debug *ar, Closure *cl) { } +static int nextline (Proto *p, int currentline, int pc) { + if (p->lineinfo[pc] != ABSLINEINFO) + return currentline + p->lineinfo[pc]; + else + return luaG_getfuncline(p, pc); +} + + static void collectvalidlines (lua_State *L, Closure *f) { if (noLuaClosure(f)) { setnilvalue(L->top); @@ -219,13 +280,16 @@ static void collectvalidlines (lua_State *L, Closure *f) { else { int i; TValue v; - int *lineinfo = f->l.p->lineinfo; + Proto *p = f->l.p; + int currentline = p->linedefined; Table *t = luaH_new(L); /* new table to store active lines */ sethvalue(L, L->top, t); /* push it on stack */ api_incr_top(L); setbvalue(&v, 1); /* boolean 'true' to be the value of all indices */ - for (i = 0; i < f->l.p->sizelineinfo; i++) /* for all lines with code */ - luaH_setint(L, t, lineinfo[i], &v); /* table[line] = true */ + for (i = 0; i < p->sizelineinfo; i++) { /* for all lines with code */ + currentline = nextline(p, currentline, i); + luaH_setint(L, t, currentline, &v); /* table[line] = true */ + } } } @@ -681,6 +745,19 @@ l_noret luaG_runerror (lua_State *L, const char *fmt, ...) { } +/* +** Check whether new instruction 'newpc' is in a different line from +** previous instruction 'oldpc'. +*/ +static int changedline (Proto *p, int oldpc, int newpc) { + while (oldpc++ < newpc) { + if (p->lineinfo[oldpc] != 0) + return (luaG_getfuncline(p, oldpc - 1) != luaG_getfuncline(p, newpc)); + } + return 0; /* no line changes in the way */ +} + + void luaG_traceexec (lua_State *L) { CallInfo *ci = L->ci; lu_byte mask = L->hookmask; @@ -698,11 +775,12 @@ void luaG_traceexec (lua_State *L) { if (mask & LUA_MASKLINE) { Proto *p = ci_func(ci)->p; int npc = pcRel(ci->u.l.savedpc, p); - int newline = getfuncline(p, npc); if (npc == 0 || /* call linehook when enter a new function, */ ci->u.l.savedpc <= L->oldpc || /* when jump back (loop), or when */ - newline != getfuncline(p, pcRel(L->oldpc, p))) /* enter a new line */ + changedline(p, pcRel(L->oldpc, p), npc)) { /* enter new line */ + int newline = luaG_getfuncline(p, npc); /* new line */ luaD_hook(L, LUA_HOOKLINE, newline); /* call line hook */ + } } L->oldpc = ci->u.l.savedpc; if (L->status == LUA_YIELD) { /* did hook yield? */ diff --git a/ldebug.h b/ldebug.h index 9c0a03a6fe..9062f4bd0c 100644 --- a/ldebug.h +++ b/ldebug.h @@ -1,5 +1,5 @@ /* -** $Id: ldebug.h,v 2.13 2015/03/11 16:10:41 roberto Exp roberto $ +** $Id: ldebug.h,v 2.14 2015/05/22 17:45:56 roberto Exp roberto $ ** Auxiliary functions from Debug Interface module ** See Copyright Notice in lua.h */ @@ -13,11 +13,15 @@ #define pcRel(pc, p) (cast(int, (pc) - (p)->code) - 1) -#define getfuncline(f,pc) (((f)->lineinfo) ? (f)->lineinfo[pc] : -1) - #define resethookcount(L) (L->hookcount = L->basehookcount) +/* +** mark for entries in 'lineinfo' array that has absolute information in +** 'abslineinfo' array +*/ +#define ABSLINEINFO (-0x80) +LUAI_FUNC int luaG_getfuncline (Proto *f, int pc); LUAI_FUNC l_noret luaG_typeerror (lua_State *L, const TValue *o, const char *opname); LUAI_FUNC l_noret luaG_concaterror (lua_State *L, const TValue *p1, diff --git a/ldump.c b/ldump.c index 19030edc87..a22d6197c0 100644 --- a/ldump.c +++ b/ldump.c @@ -1,5 +1,5 @@ /* -** $Id: ldump.c,v 2.36 2015/03/30 15:43:51 roberto Exp roberto $ +** $Id: ldump.c,v 2.37 2015/10/08 15:53:49 roberto Exp roberto $ ** save precompiled Lua chunks ** See Copyright Notice in lua.h */ @@ -149,6 +149,12 @@ static void DumpDebug (const Proto *f, DumpState *D) { n = (D->strip) ? 0 : f->sizelineinfo; DumpInt(n, D); DumpVector(f->lineinfo, n, D); + n = (D->strip) ? 0 : f->sizeabslineinfo; + DumpInt(n, D); + for (i = 0; i < n; i++) { + DumpInt(f->abslineinfo[i].pc, D); + DumpInt(f->abslineinfo[i].line, D); + } n = (D->strip) ? 0 : f->sizelocvars; DumpInt(n, D); for (i = 0; i < n; i++) { diff --git a/lfunc.c b/lfunc.c index e84538c9af..a4a4e29d4d 100644 --- a/lfunc.c +++ b/lfunc.c @@ -1,5 +1,5 @@ /* -** $Id: lfunc.c,v 2.48 2017/04/30 20:43:26 roberto Exp roberto $ +** $Id: lfunc.c,v 2.49 2017/05/24 18:54:54 roberto Exp roberto $ ** Auxiliary functions to manipulate prototypes and closures ** See Copyright Notice in lua.h */ @@ -119,6 +119,8 @@ Proto *luaF_newproto (lua_State *L) { f->sizecode = 0; f->lineinfo = NULL; f->sizelineinfo = 0; + f->abslineinfo = NULL; + f->sizeabslineinfo = 0; f->upvalues = NULL; f->sizeupvalues = 0; f->numparams = 0; @@ -138,6 +140,7 @@ void luaF_freeproto (lua_State *L, Proto *f) { luaM_freearray(L, f->p, f->sizep); luaM_freearray(L, f->k, f->sizek); luaM_freearray(L, f->lineinfo, f->sizelineinfo); + luaM_freearray(L, f->abslineinfo, f->sizeabslineinfo); luaM_freearray(L, f->locvars, f->sizelocvars); luaM_freearray(L, f->upvalues, f->sizeupvalues); luaM_free(L, f); diff --git a/lobject.h b/lobject.h index e245f306c4..9af501af7f 100644 --- a/lobject.h +++ b/lobject.h @@ -1,5 +1,5 @@ /* -** $Id: lobject.h,v 2.122 2017/06/09 16:48:44 roberto Exp roberto $ +** $Id: lobject.h,v 2.123 2017/06/12 14:21:44 roberto Exp roberto $ ** Type definitions for Lua objects ** See Copyright Notice in lua.h */ @@ -417,6 +417,21 @@ typedef struct LocVar { } LocVar; +/* +** Associates the absolute line source for a given instruction ('pc'). +** The array 'lineinfo' gives, for each instruction, the difference in +** lines from the previous instruction. When that difference does not +** fit into a byte, Lua saves the absolute line for that instruction. +** (Lua also saves the absolute line periodically, to speed up the +** computation of a line number: we can use binary search in the +** absolute-line array, but we must traverse the 'lineinfo' array +** linearly to compute a line.) +*/ +typedef struct AbsLineInfo { + int pc; + int line; +} AbsLineInfo; + /* ** Function Prototypes */ @@ -432,15 +447,17 @@ typedef struct Proto { int sizelineinfo; int sizep; /* size of 'p' */ int sizelocvars; + int sizeabslineinfo; /* size of 'abslineinfo' */ int linedefined; /* debug information */ int lastlinedefined; /* debug information */ TValue *k; /* constants used by the function */ + struct LClosure *cache; /* last-created closure with this prototype */ Instruction *code; /* opcodes */ struct Proto **p; /* functions defined inside the function */ - int *lineinfo; /* map from opcodes to source lines (debug information) */ - LocVar *locvars; /* information about local variables (debug information) */ Upvaldesc *upvalues; /* upvalue information */ - struct LClosure *cache; /* last-created closure with this prototype */ + ls_byte *lineinfo; /* information about source lines (debug information) */ + AbsLineInfo *abslineinfo; /* idem */ + LocVar *locvars; /* information about local variables (debug information) */ TString *source; /* used for debug information */ GCObject *gclist; } Proto; diff --git a/lparser.c b/lparser.c index 37f84cce9e..5e244e24ba 100644 --- a/lparser.c +++ b/lparser.c @@ -1,5 +1,5 @@ /* -** $Id: lparser.c,v 2.158 2017/04/29 18:09:17 roberto Exp roberto $ +** $Id: lparser.c,v 2.159 2017/05/13 12:57:20 roberto Exp roberto $ ** Lua Parser ** See Copyright Notice in lua.h */ @@ -527,22 +527,24 @@ static void codeclosure (LexState *ls, expdesc *v) { static void open_func (LexState *ls, FuncState *fs, BlockCnt *bl) { - Proto *f; + Proto *f = fs->f; fs->prev = ls->fs; /* linked list of funcstates */ fs->ls = ls; ls->fs = fs; fs->pc = 0; + fs->previousline = f->linedefined; + fs->iwthabs = 0; fs->lasttarget = 0; fs->jpc = NO_JUMP; fs->freereg = 0; fs->nk = 0; + fs->nabslineinfo = 0; fs->np = 0; fs->nups = 0; fs->nlocvars = 0; fs->nactvar = 0; fs->firstlocal = ls->dyd->actvar.n; fs->bl = NULL; - f = fs->f; f->source = ls->source; f->maxstacksize = 2; /* registers 0/1 are always valid */ enterblock(fs, bl, 0); @@ -557,8 +559,11 @@ static void close_func (LexState *ls) { leaveblock(fs); luaM_reallocvector(L, f->code, f->sizecode, fs->pc, Instruction); f->sizecode = fs->pc; - luaM_reallocvector(L, f->lineinfo, f->sizelineinfo, fs->pc, int); + luaM_reallocvector(L, f->lineinfo, f->sizelineinfo, fs->pc, ls_byte); f->sizelineinfo = fs->pc; + luaM_reallocvector(L, f->abslineinfo, f->sizeabslineinfo, + fs->nabslineinfo, AbsLineInfo); + f->sizeabslineinfo = fs->nabslineinfo; luaM_reallocvector(L, f->k, f->sizek, fs->nk, TValue); f->sizek = fs->nk; luaM_reallocvector(L, f->p, f->sizep, fs->np, Proto *); diff --git a/lparser.h b/lparser.h index 1612418125..3ab6065daf 100644 --- a/lparser.h +++ b/lparser.h @@ -1,5 +1,5 @@ /* -** $Id: lparser.h,v 1.76 2015/12/30 18:16:13 roberto Exp roberto $ +** $Id: lparser.h,v 1.77 2017/04/28 20:57:45 roberto Exp roberto $ ** Lua Parser ** See Copyright Notice in lua.h */ @@ -123,14 +123,17 @@ typedef struct FuncState { struct BlockCnt *bl; /* chain of current blocks */ int pc; /* next position to code (equivalent to 'ncode') */ int lasttarget; /* 'label' of last 'jump label' */ + int previousline; /* last line that was saved in 'lineinfo' */ int jpc; /* list of pending jumps to 'pc' */ int nk; /* number of elements in 'k' */ int np; /* number of elements in 'p' */ + int nabslineinfo; /* number of elements in 'abslineinfo' */ int firstlocal; /* index of first local var (in Dyndata array) */ short nlocvars; /* number of elements in 'f->locvars' */ lu_byte nactvar; /* number of active local variables */ lu_byte nups; /* number of upvalues */ lu_byte freereg; /* first free register */ + lu_byte iwthabs; /* instructions issued since last absolute line info */ } FuncState; diff --git a/ltests.c b/ltests.c index 733e47d498..8b2c0ee1e2 100644 --- a/ltests.c +++ b/ltests.c @@ -1,5 +1,5 @@ /* -** $Id: ltests.c,v 2.219 2017/06/09 16:48:44 roberto Exp roberto $ +** $Id: ltests.c,v 2.220 2017/06/12 14:21:44 roberto Exp roberto $ ** Internal Module for Debugging of the Lua Implementation ** See Copyright Notice in lua.h */ @@ -537,7 +537,7 @@ static char *buildop (Proto *p, int pc, char *buff) { Instruction i = p->code[pc]; OpCode o = GET_OPCODE(i); const char *name = luaP_opnames[o]; - int line = getfuncline(p, pc); + int line = luaG_getfuncline(p, pc); sprintf(buff, "(%4d) %4d - ", line, pc); switch (getOpMode(o)) { case iABC: diff --git a/ltests.h b/ltests.h index 9d26fcb099..82ccc97c21 100644 --- a/ltests.h +++ b/ltests.h @@ -1,5 +1,5 @@ /* -** $Id: ltests.h,v 2.49 2015/09/22 14:18:24 roberto Exp roberto $ +** $Id: ltests.h,v 2.50 2016/07/19 17:13:00 roberto Exp roberto $ ** Internal Header for Debugging of the Lua Implementation ** See Copyright Notice in lua.h */ @@ -111,6 +111,7 @@ LUA_API void *debug_realloc (void *ud, void *block, #define LUAL_BUFFERSIZE 23 #define MINSTRTABSIZE 2 #define MAXINDEXRK 1 +#define MAXIWTHABS 3 /* make stack-overflow tests run faster */ diff --git a/lundump.c b/lundump.c index 13916bc1f1..7fb05762a2 100644 --- a/lundump.c +++ b/lundump.c @@ -1,5 +1,5 @@ /* -** $Id: lundump.c,v 2.43 2015/09/17 15:51:05 roberto Exp roberto $ +** $Id: lundump.c,v 2.44 2015/11/02 16:09:30 roberto Exp roberto $ ** load precompiled Lua chunks ** See Copyright Notice in lua.h */ @@ -180,10 +180,17 @@ static void LoadUpvalues (LoadState *S, Proto *f) { static void LoadDebug (LoadState *S, Proto *f) { int i, n; n = LoadInt(S); - f->lineinfo = luaM_newvector(S->L, n, int); + f->lineinfo = luaM_newvector(S->L, n, ls_byte); f->sizelineinfo = n; LoadVector(S, f->lineinfo, n); n = LoadInt(S); + f->abslineinfo = luaM_newvector(S->L, n, AbsLineInfo); + f->sizeabslineinfo = n; + for (i = 0; i < n; i++) { + f->abslineinfo[i].pc = LoadInt(S); + f->abslineinfo[i].line = LoadInt(S); + } + n = LoadInt(S); f->locvars = luaM_newvector(S->L, n, LocVar); f->sizelocvars = n; for (i = 0; i < n; i++) From 124bfd20817d4624d2b69a4dc41182485912b821 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 27 Jun 2017 11:21:12 -0300 Subject: [PATCH 0066/1145] dumping ints and size_ts compacted --- ldump.c | 32 +++++++++++++++++++++----------- lundump.c | 26 +++++++++++++++++--------- 2 files changed, 38 insertions(+), 20 deletions(-) diff --git a/ldump.c b/ldump.c index a22d6197c0..16a95f9bd6 100644 --- a/ldump.c +++ b/ldump.c @@ -1,5 +1,5 @@ /* -** $Id: ldump.c,v 2.37 2015/10/08 15:53:49 roberto Exp roberto $ +** $Id: ldump.c,v 2.38 2017/06/27 11:35:31 roberto Exp roberto $ ** save precompiled Lua chunks ** See Copyright Notice in lua.h */ @@ -55,8 +55,23 @@ static void DumpByte (int y, DumpState *D) { } +/* DumpInt Buff Size */ +#define DIBS ((sizeof(size_t) * 8 / 7) + 1) + +static void DumpSize (size_t x, DumpState *D) { + lu_byte buff[DIBS]; + int n = 0; + do { + buff[DIBS - (++n)] = x & 0x7f; /* fill buffer in reverse order */ + x >>= 7; + } while (x != 0); + buff[DIBS - 1] |= 0x80; /* mark last byte */ + DumpVector(buff + DIBS - n, n, D); +} + + static void DumpInt (int x, DumpState *D) { - DumpVar(x, D); + DumpSize(x, D); } @@ -72,17 +87,12 @@ static void DumpInteger (lua_Integer x, DumpState *D) { static void DumpString (const TString *s, DumpState *D) { if (s == NULL) - DumpByte(0, D); + DumpSize(0, D); else { - size_t size = tsslen(s) + 1; /* include trailing '\0' */ + size_t size = tsslen(s); const char *str = getstr(s); - if (size < 0xFF) - DumpByte(cast_int(size), D); - else { - DumpByte(0xFF, D); - DumpVar(size, D); - } - DumpVector(str, size - 1, D); /* no need to save '\0' */ + DumpSize(size + 1, D); + DumpVector(str, size, D); } } diff --git a/lundump.c b/lundump.c index 7fb05762a2..f98c70e133 100644 --- a/lundump.c +++ b/lundump.c @@ -1,5 +1,5 @@ /* -** $Id: lundump.c,v 2.44 2015/11/02 16:09:30 roberto Exp roberto $ +** $Id: lundump.c,v 2.45 2017/06/27 11:35:31 roberto Exp roberto $ ** load precompiled Lua chunks ** See Copyright Notice in lua.h */ @@ -58,16 +58,26 @@ static void LoadBlock (LoadState *S, void *b, size_t size) { static lu_byte LoadByte (LoadState *S) { - lu_byte x; - LoadVar(S, x); + int b = zgetc(S->Z); + if (b == EOZ) + error(S, "truncated"); + return cast_byte(b); +} + + +static size_t LoadSize (LoadState *S) { + size_t x = 0; + int b; + do { + b = LoadByte(S); + x = (x << 7) | (b & 0x7f); + } while ((b & 0x80) == 0); return x; } static int LoadInt (LoadState *S) { - int x; - LoadVar(S, x); - return x; + return cast_int(LoadSize(S)); } @@ -86,9 +96,7 @@ static lua_Integer LoadInteger (LoadState *S) { static TString *LoadString (LoadState *S) { - size_t size = LoadByte(S); - if (size == 0xFF) - LoadVar(S, size); + size_t size = LoadSize(S); if (size == 0) return NULL; else if (--size <= LUAI_MAXSHORTLEN) { /* short string? */ From 5a1c8d8ef343bf0157851a4832c2c937b812b64f Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 27 Jun 2017 15:32:49 -0300 Subject: [PATCH 0067/1145] new constant 'LUA_GNAME' for the name of the global table "_G" --- lauxlib.c | 4 ++-- lauxlib.h | 6 +++++- lbaselib.c | 6 +++--- linit.c | 4 ++-- ltests.c | 4 ++-- 5 files changed, 14 insertions(+), 10 deletions(-) diff --git a/lauxlib.c b/lauxlib.c index 2d16c9ce35..2c26107739 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -1,5 +1,5 @@ /* -** $Id: lauxlib.c,v 1.289 2016/12/20 18:37:00 roberto Exp roberto $ +** $Id: lauxlib.c,v 1.290 2017/04/24 18:06:12 roberto Exp roberto $ ** Auxiliary functions for building Lua libraries ** See Copyright Notice in lua.h */ @@ -76,7 +76,7 @@ static int pushglobalfuncname (lua_State *L, lua_Debug *ar) { lua_getfield(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE); if (findfield(L, top + 1, 2)) { const char *name = lua_tostring(L, -1); - if (strncmp(name, "_G.", 3) == 0) { /* name start with '_G.'? */ + if (strncmp(name, LUA_GNAME ".", 3) == 0) { /* name start with '_G.'? */ lua_pushstring(L, name + 3); /* push name without prefix */ lua_remove(L, -2); /* remove original name */ } diff --git a/lauxlib.h b/lauxlib.h index 8de199167d..79d1f30ab9 100644 --- a/lauxlib.h +++ b/lauxlib.h @@ -1,5 +1,5 @@ /* -** $Id: lauxlib.h,v 1.131 2016/12/06 14:54:31 roberto Exp roberto $ +** $Id: lauxlib.h,v 1.132 2017/04/24 18:06:12 roberto Exp roberto $ ** Auxiliary functions for building Lua libraries ** See Copyright Notice in lua.h */ @@ -15,6 +15,10 @@ #include "lua.h" +/* global table */ +#define LUA_GNAME "_G" + + /* extra error code for 'luaL_loadfilex' */ #define LUA_ERRFILE (LUA_ERRERR+1) diff --git a/lbaselib.c b/lbaselib.c index 55b56f35ef..87b0ce834f 100644 --- a/lbaselib.c +++ b/lbaselib.c @@ -1,5 +1,5 @@ /* -** $Id: lbaselib.c,v 1.315 2017/02/23 21:07:34 roberto Exp roberto $ +** $Id: lbaselib.c,v 1.316 2017/05/26 19:14:29 roberto Exp roberto $ ** Basic library ** See Copyright Notice in lua.h */ @@ -504,7 +504,7 @@ static const luaL_Reg base_funcs[] = { {"type", luaB_type}, {"xpcall", luaB_xpcall}, /* placeholders */ - {"_G", NULL}, + {LUA_GNAME, NULL}, {"_VERSION", NULL}, {NULL, NULL} }; @@ -516,7 +516,7 @@ LUAMOD_API int luaopen_base (lua_State *L) { luaL_setfuncs(L, base_funcs, 0); /* set global _G */ lua_pushvalue(L, -1); - lua_setfield(L, -2, "_G"); + lua_setfield(L, -2, LUA_GNAME); /* set global _VERSION */ lua_pushliteral(L, LUA_VERSION); lua_setfield(L, -2, "_VERSION"); diff --git a/linit.c b/linit.c index 897ae3523b..3c2b6023be 100644 --- a/linit.c +++ b/linit.c @@ -1,5 +1,5 @@ /* -** $Id: linit.c,v 1.38 2015/01/05 13:48:33 roberto Exp roberto $ +** $Id: linit.c,v 1.39 2016/12/04 20:17:24 roberto Exp roberto $ ** Initialization of libraries for lua.c and other clients ** See Copyright Notice in lua.h */ @@ -40,7 +40,7 @@ ** program */ static const luaL_Reg loadedlibs[] = { - {"_G", luaopen_base}, + {LUA_GNAME, luaopen_base}, {LUA_LOADLIBNAME, luaopen_package}, {LUA_COLIBNAME, luaopen_coroutine}, {LUA_TABLIBNAME, luaopen_table}, diff --git a/ltests.c b/ltests.c index 8b2c0ee1e2..36da64e8b9 100644 --- a/ltests.c +++ b/ltests.c @@ -1,5 +1,5 @@ /* -** $Id: ltests.c,v 2.220 2017/06/12 14:21:44 roberto Exp roberto $ +** $Id: ltests.c,v 2.221 2017/06/27 11:35:31 roberto Exp roberto $ ** Internal Module for Debugging of the Lua Implementation ** See Copyright Notice in lua.h */ @@ -960,7 +960,7 @@ static lua_State *getstate (lua_State *L) { static int loadlib (lua_State *L) { static const luaL_Reg libs[] = { - {"_G", luaopen_base}, + {LUA_GNAME, luaopen_base}, {"coroutine", luaopen_coroutine}, {"debug", luaopen_debug}, {"io", luaopen_io}, From f96497397addca22f22a6ba6eeabc906be43f16b Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 29 Jun 2017 12:06:44 -0300 Subject: [PATCH 0068/1145] new type 'StackValue' for stack elements (we may want to put extra info there in the future) --- lapi.c | 268 +++++++++++++++++++++++++++++------------------------- lcode.c | 4 +- ldebug.c | 30 +++--- ldo.c | 38 ++++---- ldo.h | 4 +- lfunc.c | 12 +-- lfunc.h | 5 +- lgc.c | 10 +- llex.c | 4 +- lobject.c | 45 +++++---- lobject.h | 33 ++++--- lparser.c | 6 +- lstate.c | 10 +- ltable.c | 12 +-- ltests.c | 8 +- ltm.c | 46 ++++++---- ltm.h | 8 +- lundump.c | 4 +- lvm.c | 209 +++++++++++++++++++++--------------------- lvm.h | 4 +- 20 files changed, 409 insertions(+), 351 deletions(-) diff --git a/lapi.c b/lapi.c index d78d9455b1..4985da6be1 100644 --- a/lapi.c +++ b/lapi.c @@ -1,5 +1,5 @@ /* -** $Id: lapi.c,v 2.268 2017/05/26 19:14:29 roberto Exp roberto $ +** $Id: lapi.c,v 2.269 2017/06/01 20:22:33 roberto Exp roberto $ ** Lua API ** See Copyright Notice in lua.h */ @@ -57,33 +57,48 @@ const char lua_ident[] = api_check(l, isstackindex(i, o), "index not in the stack") -static TValue *index2addr (lua_State *L, int idx) { +static TValue *index2value (lua_State *L, int idx) { CallInfo *ci = L->ci; if (idx > 0) { - TValue *o = ci->func + idx; + StkId o = ci->func + idx; api_check(L, idx <= ci->top - (ci->func + 1), "unacceptable index"); if (o >= L->top) return NONVALIDVALUE; - else return o; + else return s2v(o); } else if (!ispseudo(idx)) { /* negative index */ api_check(L, idx != 0 && -idx <= L->top - (ci->func + 1), "invalid index"); - return L->top + idx; + return s2v(L->top + idx); } else if (idx == LUA_REGISTRYINDEX) return &G(L)->l_registry; else { /* upvalues */ idx = LUA_REGISTRYINDEX - idx; api_check(L, idx <= MAXUPVAL + 1, "upvalue index too large"); - if (ttislcf(ci->func)) /* light C function? */ + if (ttislcf(s2v(ci->func))) /* light C function? */ return NONVALIDVALUE; /* it has no upvalues */ else { - CClosure *func = clCvalue(ci->func); + CClosure *func = clCvalue(s2v(ci->func)); return (idx <= func->nupvalues) ? &func->upvalue[idx-1] : NONVALIDVALUE; } } } +static StkId index2stack (lua_State *L, int idx) { + CallInfo *ci = L->ci; + if (idx > 0) { + StkId o = ci->func + idx; + api_check(L, o < L->top, "unacceptable index"); + return o; + } + else { /* non-positive index */ + api_check(L, idx != 0 && -idx <= L->top - (ci->func + 1), "invalid index"); + api_check(L, !ispseudo(idx), "invalid index"); + return L->top + idx; + } +} + + /* ** to be called by 'lua_checkstack' in protected mode, to grow stack ** capturing memory errors @@ -124,7 +139,7 @@ LUA_API void lua_xmove (lua_State *from, lua_State *to, int n) { api_check(from, to->ci->top - to->top >= n, "stack overflow"); from->top -= n; for (i = 0; i < n; i++) { - setobj2s(to, to->top, from->top + i); + setobjs2s(to, to->top, from->top + i); to->top++; /* stack already checked by previous 'api_check' */ } lua_unlock(to); @@ -175,7 +190,7 @@ LUA_API void lua_settop (lua_State *L, int idx) { if (idx >= 0) { api_check(L, idx <= L->stack_last - (func + 1), "new top too large"); while (L->top < (func + 1) + idx) - setnilvalue(L->top++); + setnilvalue(s2v(L->top++)); L->top = (func + 1) + idx; } else { @@ -189,11 +204,13 @@ LUA_API void lua_settop (lua_State *L, int idx) { /* ** Reverse the stack segment from 'from' to 'to' ** (auxiliary to 'lua_rotate') +** Note that we move(copy) only the value inside the stack. +** (We do not move addicional fields that may exist.) */ static void reverse (lua_State *L, StkId from, StkId to) { for (; from < to; from++, to--) { TValue temp; - setobj(L, &temp, from); + setobj(L, &temp, s2v(from)); setobjs2s(L, from, to); setobj2s(L, to, &temp); } @@ -208,8 +225,7 @@ LUA_API void lua_rotate (lua_State *L, int idx, int n) { StkId p, t, m; lua_lock(L); t = L->top - 1; /* end of stack segment being rotated */ - p = index2addr(L, idx); /* start of segment */ - api_checkstackindex(L, idx, p); + p = index2stack(L, idx); /* start of segment */ api_check(L, (n >= 0 ? n : -n) <= (t - p + 1), "invalid 'n'"); m = (n >= 0 ? t - n : p - n - 1); /* end of prefix */ reverse(L, p, m); /* reverse the prefix with length 'n' */ @@ -222,12 +238,12 @@ LUA_API void lua_rotate (lua_State *L, int idx, int n) { LUA_API void lua_copy (lua_State *L, int fromidx, int toidx) { TValue *fr, *to; lua_lock(L); - fr = index2addr(L, fromidx); - to = index2addr(L, toidx); + fr = index2value(L, fromidx); + to = index2value(L, toidx); api_checkvalidindex(L, to); setobj(L, to, fr); if (isupvalue(toidx)) /* function upvalue? */ - luaC_barrier(L, clCvalue(L->ci->func), fr); + luaC_barrier(L, clCvalue(s2v(L->ci->func)), fr); /* LUA_REGISTRYINDEX does not need gc barrier (collector revisits it before finishing collection) */ lua_unlock(L); @@ -236,7 +252,7 @@ LUA_API void lua_copy (lua_State *L, int fromidx, int toidx) { LUA_API void lua_pushvalue (lua_State *L, int idx) { lua_lock(L); - setobj2s(L, L->top, index2addr(L, idx)); + setobj2s(L, L->top, index2value(L, idx)); api_incr_top(L); lua_unlock(L); } @@ -249,7 +265,7 @@ LUA_API void lua_pushvalue (lua_State *L, int idx) { LUA_API int lua_type (lua_State *L, int idx) { - StkId o = index2addr(L, idx); + const TValue *o = index2value(L, idx); return (isvalid(o) ? ttnov(o) : LUA_TNONE); } @@ -262,39 +278,39 @@ LUA_API const char *lua_typename (lua_State *L, int t) { LUA_API int lua_iscfunction (lua_State *L, int idx) { - StkId o = index2addr(L, idx); + const TValue *o = index2value(L, idx); return (ttislcf(o) || (ttisCclosure(o))); } LUA_API int lua_isinteger (lua_State *L, int idx) { - StkId o = index2addr(L, idx); + const TValue *o = index2value(L, idx); return ttisinteger(o); } LUA_API int lua_isnumber (lua_State *L, int idx) { lua_Number n; - const TValue *o = index2addr(L, idx); + const TValue *o = index2value(L, idx); return tonumber(o, &n); } LUA_API int lua_isstring (lua_State *L, int idx) { - const TValue *o = index2addr(L, idx); + const TValue *o = index2value(L, idx); return (ttisstring(o) || cvt2str(o)); } LUA_API int lua_isuserdata (lua_State *L, int idx) { - const TValue *o = index2addr(L, idx); + const TValue *o = index2value(L, idx); return (ttisfulluserdata(o) || ttislightuserdata(o)); } LUA_API int lua_rawequal (lua_State *L, int index1, int index2) { - StkId o1 = index2addr(L, index1); - StkId o2 = index2addr(L, index2); + const TValue *o1 = index2value(L, index1); + const TValue *o2 = index2value(L, index2); return (isvalid(o1) && isvalid(o2)) ? luaV_rawequalobj(o1, o2) : 0; } @@ -309,18 +325,19 @@ LUA_API void lua_arith (lua_State *L, int op) { api_incr_top(L); } /* first operand at top - 2, second at top - 1; result go to top - 2 */ - luaO_arith(L, op, L->top - 2, L->top - 1, L->top - 2); + luaO_arith(L, op, s2v(L->top - 2), s2v(L->top - 1), L->top - 2); L->top--; /* remove second operand */ lua_unlock(L); } LUA_API int lua_compare (lua_State *L, int index1, int index2, int op) { - StkId o1, o2; + const TValue *o1; + const TValue *o2; int i = 0; lua_lock(L); /* may call tag method */ - o1 = index2addr(L, index1); - o2 = index2addr(L, index2); + o1 = index2value(L, index1); + o2 = index2value(L, index2); if (isvalid(o1) && isvalid(o2)) { switch (op) { case LUA_OPEQ: i = luaV_equalobj(L, o1, o2); break; @@ -335,7 +352,7 @@ LUA_API int lua_compare (lua_State *L, int index1, int index2, int op) { LUA_API size_t lua_stringtonumber (lua_State *L, const char *s) { - size_t sz = luaO_str2num(s, L->top); + size_t sz = luaO_str2num(s, s2v(L->top)); if (sz != 0) api_incr_top(L); return sz; @@ -344,7 +361,7 @@ LUA_API size_t lua_stringtonumber (lua_State *L, const char *s) { LUA_API lua_Number lua_tonumberx (lua_State *L, int idx, int *pisnum) { lua_Number n; - const TValue *o = index2addr(L, idx); + const TValue *o = index2value(L, idx); int isnum = tonumber(o, &n); if (!isnum) n = 0; /* call to 'tonumber' may change 'n' even if it fails */ @@ -355,7 +372,7 @@ LUA_API lua_Number lua_tonumberx (lua_State *L, int idx, int *pisnum) { LUA_API lua_Integer lua_tointegerx (lua_State *L, int idx, int *pisnum) { lua_Integer res; - const TValue *o = index2addr(L, idx); + const TValue *o = index2value(L, idx); int isnum = tointeger(o, &res); if (!isnum) res = 0; /* call to 'tointeger' may change 'n' even if it fails */ @@ -365,13 +382,13 @@ LUA_API lua_Integer lua_tointegerx (lua_State *L, int idx, int *pisnum) { LUA_API int lua_toboolean (lua_State *L, int idx) { - const TValue *o = index2addr(L, idx); + const TValue *o = index2value(L, idx); return !l_isfalse(o); } LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) { - StkId o = index2addr(L, idx); + TValue *o = index2value(L, idx); if (!ttisstring(o)) { if (!cvt2str(o)) { /* not convertible? */ if (len != NULL) *len = 0; @@ -380,7 +397,7 @@ LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) { lua_lock(L); /* 'luaO_tostring' may create a new string */ luaO_tostring(L, o); luaC_checkGC(L); - o = index2addr(L, idx); /* previous call may reallocate the stack */ + o = index2value(L, idx); /* previous call may reallocate the stack */ lua_unlock(L); } if (len != NULL) @@ -390,7 +407,7 @@ LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) { LUA_API lua_Unsigned lua_rawlen (lua_State *L, int idx) { - StkId o = index2addr(L, idx); + const TValue *o = index2value(L, idx); switch (ttype(o)) { case LUA_TSHRSTR: return tsvalue(o)->shrlen; case LUA_TLNGSTR: return tsvalue(o)->u.lnglen; @@ -402,7 +419,7 @@ LUA_API lua_Unsigned lua_rawlen (lua_State *L, int idx) { LUA_API lua_CFunction lua_tocfunction (lua_State *L, int idx) { - StkId o = index2addr(L, idx); + const TValue *o = index2value(L, idx); if (ttislcf(o)) return fvalue(o); else if (ttisCclosure(o)) return clCvalue(o)->f; @@ -411,7 +428,7 @@ LUA_API lua_CFunction lua_tocfunction (lua_State *L, int idx) { LUA_API void *lua_touserdata (lua_State *L, int idx) { - StkId o = index2addr(L, idx); + const TValue *o = index2value(L, idx); switch (ttnov(o)) { case LUA_TUSERDATA: return getudatamem(uvalue(o)); case LUA_TLIGHTUSERDATA: return pvalue(o); @@ -421,13 +438,13 @@ LUA_API void *lua_touserdata (lua_State *L, int idx) { LUA_API lua_State *lua_tothread (lua_State *L, int idx) { - StkId o = index2addr(L, idx); + const TValue *o = index2value(L, idx); return (!ttisthread(o)) ? NULL : thvalue(o); } LUA_API const void *lua_topointer (lua_State *L, int idx) { - StkId o = index2addr(L, idx); + const TValue *o = index2value(L, idx); switch (ttype(o)) { case LUA_TTABLE: return hvalue(o); case LUA_TLCL: return clLvalue(o); @@ -449,7 +466,7 @@ LUA_API const void *lua_topointer (lua_State *L, int idx) { LUA_API void lua_pushnil (lua_State *L) { lua_lock(L); - setnilvalue(L->top); + setnilvalue(s2v(L->top)); api_incr_top(L); lua_unlock(L); } @@ -457,7 +474,7 @@ LUA_API void lua_pushnil (lua_State *L) { LUA_API void lua_pushnumber (lua_State *L, lua_Number n) { lua_lock(L); - setfltvalue(L->top, n); + setfltvalue(s2v(L->top), n); api_incr_top(L); lua_unlock(L); } @@ -465,7 +482,7 @@ LUA_API void lua_pushnumber (lua_State *L, lua_Number n) { LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) { lua_lock(L); - setivalue(L->top, n); + setivalue(s2v(L->top), n); api_incr_top(L); lua_unlock(L); } @@ -491,7 +508,7 @@ LUA_API const char *lua_pushlstring (lua_State *L, const char *s, size_t len) { LUA_API const char *lua_pushstring (lua_State *L, const char *s) { lua_lock(L); if (s == NULL) - setnilvalue(L->top); + setnilvalue(s2v(L->top)); else { TString *ts; ts = luaS_new(L, s); @@ -532,7 +549,7 @@ LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) { LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) { lua_lock(L); if (n == 0) { - setfvalue(L->top, fn); + setfvalue(s2v(L->top), fn); } else { CClosure *cl; @@ -542,10 +559,10 @@ LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) { cl->f = fn; L->top -= n; while (n--) { - setobj2n(L, &cl->upvalue[n], L->top + n); + setobj2n(L, &cl->upvalue[n], s2v(L->top + n)); /* does not need barrier because closure is white */ } - setclCvalue(L, L->top, cl); + setclCvalue(L, s2v(L->top), cl); } api_incr_top(L); luaC_checkGC(L); @@ -555,7 +572,7 @@ LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) { LUA_API void lua_pushboolean (lua_State *L, int b) { lua_lock(L); - setbvalue(L->top, (b != 0)); /* ensure that true is 1 */ + setbvalue(s2v(L->top), (b != 0)); /* ensure that true is 1 */ api_incr_top(L); lua_unlock(L); } @@ -563,7 +580,7 @@ LUA_API void lua_pushboolean (lua_State *L, int b) { LUA_API void lua_pushlightuserdata (lua_State *L, void *p) { lua_lock(L); - setpvalue(L->top, p); + setpvalue(s2v(L->top), p); api_incr_top(L); lua_unlock(L); } @@ -571,7 +588,7 @@ LUA_API void lua_pushlightuserdata (lua_State *L, void *p) { LUA_API int lua_pushthread (lua_State *L) { lua_lock(L); - setthvalue(L, L->top, L); + setthvalue(L, s2v(L->top), L); api_incr_top(L); lua_unlock(L); return (G(L)->mainthread == L); @@ -594,10 +611,10 @@ static int auxgetstr (lua_State *L, const TValue *t, const char *k) { else { setsvalue2s(L, L->top, str); api_incr_top(L); - luaV_finishget(L, t, L->top - 1, L->top - 1, slot); + luaV_finishget(L, t, s2v(L->top - 1), L->top - 1, slot); } lua_unlock(L); - return ttnov(L->top - 1); + return ttnov(s2v(L->top - 1)); } @@ -610,30 +627,30 @@ LUA_API int lua_getglobal (lua_State *L, const char *name) { LUA_API int lua_gettable (lua_State *L, int idx) { const TValue *slot; - StkId t; + TValue *t; lua_lock(L); - t = index2addr(L, idx); - if (luaV_fastget(L, t, L->top - 1, slot, luaH_get)) { + t = index2value(L, idx); + if (luaV_fastget(L, t, s2v(L->top - 1), slot, luaH_get)) { setobj2s(L, L->top - 1, slot); } else - luaV_finishget(L, t, L->top - 1, L->top - 1, slot); + luaV_finishget(L, t, s2v(L->top - 1), L->top - 1, slot); lua_unlock(L); - return ttnov(L->top - 1); + return ttnov(s2v(L->top - 1)); } LUA_API int lua_getfield (lua_State *L, int idx, const char *k) { lua_lock(L); - return auxgetstr(L, index2addr(L, idx), k); + return auxgetstr(L, index2value(L, idx), k); } LUA_API int lua_geti (lua_State *L, int idx, lua_Integer n) { - StkId t; + TValue *t; const TValue *slot; lua_lock(L); - t = index2addr(L, idx); + t = index2value(L, idx); if (luaV_fastgeti(L, t, n, slot)) { setobj2s(L, L->top, slot); } @@ -644,44 +661,44 @@ LUA_API int lua_geti (lua_State *L, int idx, lua_Integer n) { } api_incr_top(L); lua_unlock(L); - return ttnov(L->top - 1); + return ttnov(s2v(L->top - 1)); } LUA_API int lua_rawget (lua_State *L, int idx) { - StkId t; + TValue *t; lua_lock(L); - t = index2addr(L, idx); + t = index2value(L, idx); api_check(L, ttistable(t), "table expected"); - setobj2s(L, L->top - 1, luaH_get(hvalue(t), L->top - 1)); + setobj2s(L, L->top - 1, luaH_get(hvalue(t), s2v(L->top - 1))); lua_unlock(L); - return ttnov(L->top - 1); + return ttnov(s2v(L->top - 1)); } LUA_API int lua_rawgeti (lua_State *L, int idx, lua_Integer n) { - StkId t; + TValue *t; lua_lock(L); - t = index2addr(L, idx); + t = index2value(L, idx); api_check(L, ttistable(t), "table expected"); setobj2s(L, L->top, luaH_getint(hvalue(t), n)); api_incr_top(L); lua_unlock(L); - return ttnov(L->top - 1); + return ttnov(s2v(L->top - 1)); } LUA_API int lua_rawgetp (lua_State *L, int idx, const void *p) { - StkId t; + TValue *t; TValue k; lua_lock(L); - t = index2addr(L, idx); + t = index2value(L, idx); api_check(L, ttistable(t), "table expected"); setpvalue(&k, cast(void *, p)); setobj2s(L, L->top, luaH_get(hvalue(t), &k)); api_incr_top(L); lua_unlock(L); - return ttnov(L->top - 1); + return ttnov(s2v(L->top - 1)); } @@ -689,7 +706,7 @@ LUA_API void lua_createtable (lua_State *L, int narray, int nrec) { Table *t; lua_lock(L); t = luaH_new(L); - sethvalue(L, L->top, t); + sethvalue2s(L, L->top, t); api_incr_top(L); if (narray > 0 || nrec > 0) luaH_resize(L, t, narray, nrec); @@ -703,7 +720,7 @@ LUA_API int lua_getmetatable (lua_State *L, int objindex) { Table *mt; int res = 0; lua_lock(L); - obj = index2addr(L, objindex); + obj = index2value(L, objindex); switch (ttnov(obj)) { case LUA_TTABLE: mt = hvalue(obj)->metatable; @@ -716,7 +733,7 @@ LUA_API int lua_getmetatable (lua_State *L, int objindex) { break; } if (mt != NULL) { - sethvalue(L, L->top, mt); + sethvalue2s(L, L->top, mt); api_incr_top(L); res = 1; } @@ -726,14 +743,14 @@ LUA_API int lua_getmetatable (lua_State *L, int objindex) { LUA_API int lua_getuservalue (lua_State *L, int idx) { - StkId o; + TValue *o; lua_lock(L); - o = index2addr(L, idx); + o = index2value(L, idx); api_check(L, ttisfulluserdata(o), "full userdata expected"); - getuservalue(L, uvalue(o), L->top); + getuservalue(L, uvalue(o), s2v(L->top)); api_incr_top(L); lua_unlock(L); - return ttnov(L->top - 1); + return ttnov(s2v(L->top - 1)); } @@ -749,13 +766,13 @@ static void auxsetstr (lua_State *L, const TValue *t, const char *k) { TString *str = luaS_new(L, k); api_checknelems(L, 1); if (luaV_fastget(L, t, str, slot, luaH_getstr)) { - luaV_finishfastset(L, t, slot, L->top - 1); + luaV_finishfastset(L, t, slot, s2v(L->top - 1)); L->top--; /* pop value */ } else { setsvalue2s(L, L->top, str); /* push 'str' (to make it a TValue) */ api_incr_top(L); - luaV_finishset(L, t, L->top - 1, L->top - 2, slot); + luaV_finishset(L, t, s2v(L->top - 1), s2v(L->top - 2), slot); L->top -= 2; /* pop value and key */ } lua_unlock(L); /* lock done by caller */ @@ -770,16 +787,16 @@ LUA_API void lua_setglobal (lua_State *L, const char *name) { LUA_API void lua_settable (lua_State *L, int idx) { - StkId t; + TValue *t; const TValue *slot; lua_lock(L); api_checknelems(L, 2); - t = index2addr(L, idx); - if (luaV_fastget(L, t, L->top - 2, slot, luaH_get)) { - luaV_finishfastset(L, t, slot, L->top - 1); + t = index2value(L, idx); + if (luaV_fastget(L, t, s2v(L->top - 2), slot, luaH_get)) { + luaV_finishfastset(L, t, slot, s2v(L->top - 1)); } else - luaV_finishset(L, t, L->top - 2, L->top - 1, slot); + luaV_finishset(L, t, s2v(L->top - 2), s2v(L->top - 1), slot); L->top -= 2; /* pop index and value */ lua_unlock(L); } @@ -787,23 +804,23 @@ LUA_API void lua_settable (lua_State *L, int idx) { LUA_API void lua_setfield (lua_State *L, int idx, const char *k) { lua_lock(L); /* unlock done in 'auxsetstr' */ - auxsetstr(L, index2addr(L, idx), k); + auxsetstr(L, index2value(L, idx), k); } LUA_API void lua_seti (lua_State *L, int idx, lua_Integer n) { - StkId t; + TValue *t; const TValue *slot; lua_lock(L); api_checknelems(L, 1); - t = index2addr(L, idx); + t = index2value(L, idx); if (luaV_fastgeti(L, t, n, slot)) { - luaV_finishfastset(L, t, slot, L->top - 1); + luaV_finishfastset(L, t, slot, s2v(L->top - 1)); } else { TValue aux; setivalue(&aux, n); - luaV_finishset(L, t, &aux, L->top - 1, slot); + luaV_finishset(L, t, &aux, s2v(L->top - 1), slot); } L->top--; /* pop value */ lua_unlock(L); @@ -811,45 +828,45 @@ LUA_API void lua_seti (lua_State *L, int idx, lua_Integer n) { LUA_API void lua_rawset (lua_State *L, int idx) { - StkId o; + TValue *o; TValue *slot; lua_lock(L); api_checknelems(L, 2); - o = index2addr(L, idx); + o = index2value(L, idx); api_check(L, ttistable(o), "table expected"); - slot = luaH_set(L, hvalue(o), L->top - 2); - setobj2t(L, slot, L->top - 1); + slot = luaH_set(L, hvalue(o), s2v(L->top - 2)); + setobj2t(L, slot, s2v(L->top - 1)); invalidateTMcache(hvalue(o)); - luaC_barrierback(L, hvalue(o), L->top-1); + luaC_barrierback(L, hvalue(o), s2v(L->top - 1)); L->top -= 2; lua_unlock(L); } LUA_API void lua_rawseti (lua_State *L, int idx, lua_Integer n) { - StkId o; + TValue *o; lua_lock(L); api_checknelems(L, 1); - o = index2addr(L, idx); + o = index2value(L, idx); api_check(L, ttistable(o), "table expected"); - luaH_setint(L, hvalue(o), n, L->top - 1); - luaC_barrierback(L, hvalue(o), L->top-1); + luaH_setint(L, hvalue(o), n, s2v(L->top - 1)); + luaC_barrierback(L, hvalue(o), s2v(L->top - 1)); L->top--; lua_unlock(L); } LUA_API void lua_rawsetp (lua_State *L, int idx, const void *p) { - StkId o; + TValue *o; TValue k, *slot; lua_lock(L); api_checknelems(L, 1); - o = index2addr(L, idx); + o = index2value(L, idx); api_check(L, ttistable(o), "table expected"); setpvalue(&k, cast(void *, p)); slot = luaH_set(L, hvalue(o), &k); - setobj2t(L, slot, L->top - 1); - luaC_barrierback(L, hvalue(o), L->top - 1); + setobj2t(L, slot, s2v(L->top - 1)); + luaC_barrierback(L, hvalue(o), s2v(L->top - 1)); L->top--; lua_unlock(L); } @@ -860,12 +877,12 @@ LUA_API int lua_setmetatable (lua_State *L, int objindex) { Table *mt; lua_lock(L); api_checknelems(L, 1); - obj = index2addr(L, objindex); - if (ttisnil(L->top - 1)) + obj = index2value(L, objindex); + if (ttisnil(s2v(L->top - 1))) mt = NULL; else { - api_check(L, ttistable(L->top - 1), "table expected"); - mt = hvalue(L->top - 1); + api_check(L, ttistable(s2v(L->top - 1)), "table expected"); + mt = hvalue(s2v(L->top - 1)); } switch (ttnov(obj)) { case LUA_TTABLE: { @@ -896,13 +913,13 @@ LUA_API int lua_setmetatable (lua_State *L, int objindex) { LUA_API void lua_setuservalue (lua_State *L, int idx) { - StkId o; + TValue *o; lua_lock(L); api_checknelems(L, 1); - o = index2addr(L, idx); + o = index2value(L, idx); api_check(L, ttisfulluserdata(o), "full userdata expected"); - setuservalue(L, uvalue(o), L->top - 1); - luaC_barrier(L, gcvalue(o), L->top - 1); + setuservalue(L, uvalue(o), s2v(L->top - 1)); + luaC_barrier(L, gcvalue(o), s2v(L->top - 1)); L->top--; lua_unlock(L); } @@ -971,8 +988,7 @@ LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc, if (errfunc == 0) func = 0; else { - StkId o = index2addr(L, errfunc); - api_checkstackindex(L, errfunc, o); + StkId o = index2stack(L, errfunc); func = savestack(L, o); } c.func = L->top - (nargs+1); /* function to be called */ @@ -1010,7 +1026,7 @@ LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data, luaZ_init(L, &z, reader, data); status = luaD_protectedparser(L, &z, chunkname, mode); if (status == LUA_OK) { /* no errors? */ - LClosure *f = clLvalue(L->top - 1); /* get newly created function */ + LClosure *f = clLvalue(s2v(L->top - 1)); /* get newly created function */ if (f->nupvalues >= 1) { /* does it have an upvalue? */ /* get global table from registry */ Table *reg = hvalue(&G(L)->l_registry); @@ -1030,7 +1046,7 @@ LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data, int strip) { TValue *o; lua_lock(L); api_checknelems(L, 1); - o = L->top - 1; + o = s2v(L->top - 1); if (isLfunction(o)) status = luaU_dump(L, getproto(o), writer, data, strip); else @@ -1154,10 +1170,10 @@ LUA_API int lua_error (lua_State *L) { LUA_API int lua_next (lua_State *L, int idx) { - StkId t; + TValue *t; int more; lua_lock(L); - t = index2addr(L, idx); + t = index2value(L, idx); api_check(L, ttistable(t), "table expected"); more = luaH_next(L, hvalue(t), L->top - 1); if (more) { @@ -1187,9 +1203,9 @@ LUA_API void lua_concat (lua_State *L, int n) { LUA_API void lua_len (lua_State *L, int idx) { - StkId t; + TValue *t; lua_lock(L); - t = index2addr(L, idx); + t = index2value(L, idx); luaV_objlen(L, L->top, t); api_incr_top(L); lua_unlock(L); @@ -1218,7 +1234,7 @@ LUA_API void *lua_newuserdata (lua_State *L, size_t size) { Udata *u; lua_lock(L); u = luaS_newudata(L, size); - setuvalue(L, L->top, u); + setuvalue(L, s2v(L->top), u); api_incr_top(L); luaC_checkGC(L); lua_unlock(L); @@ -1227,7 +1243,7 @@ LUA_API void *lua_newuserdata (lua_State *L, size_t size) { -static const char *aux_upvalue (StkId fi, int n, TValue **val, +static const char *aux_upvalue (TValue *fi, int n, TValue **val, GCObject **owner) { switch (ttype(fi)) { case LUA_TCCL: { /* C closure */ @@ -1256,7 +1272,7 @@ LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) { const char *name; TValue *val = NULL; /* to avoid warnings */ lua_lock(L); - name = aux_upvalue(index2addr(L, funcindex), n, &val, NULL); + name = aux_upvalue(index2value(L, funcindex), n, &val, NULL); if (name) { setobj2s(L, L->top, val); api_incr_top(L); @@ -1270,14 +1286,14 @@ LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) { const char *name; TValue *val = NULL; /* to avoid warnings */ GCObject *owner = NULL; /* to avoid warnings */ - StkId fi; + TValue *fi; lua_lock(L); - fi = index2addr(L, funcindex); + fi = index2value(L, funcindex); api_checknelems(L, 1); name = aux_upvalue(fi, n, &val, &owner); if (name) { L->top--; - setobj(L, val, L->top); + setobj(L, val, s2v(L->top)); luaC_barrier(L, owner, val); } lua_unlock(L); @@ -1287,7 +1303,7 @@ LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) { static UpVal **getupvalref (lua_State *L, int fidx, int n, LClosure **pf) { LClosure *f; - StkId fi = index2addr(L, fidx); + TValue *fi = index2value(L, fidx); api_check(L, ttisLclosure(fi), "Lua function expected"); f = clLvalue(fi); api_check(L, (1 <= n && n <= f->p->sizeupvalues), "invalid upvalue index"); @@ -1297,7 +1313,7 @@ static UpVal **getupvalref (lua_State *L, int fidx, int n, LClosure **pf) { LUA_API void *lua_upvalueid (lua_State *L, int fidx, int n) { - StkId fi = index2addr(L, fidx); + TValue *fi = index2value(L, fidx); switch (ttype(fi)) { case LUA_TLCL: { /* lua closure */ return *getupvalref(L, fidx, n, NULL); diff --git a/lcode.c b/lcode.c index 740cf564a6..169c439dbe 100644 --- a/lcode.c +++ b/lcode.c @@ -1,5 +1,5 @@ /* -** $Id: lcode.c,v 2.119 2017/05/18 19:44:19 roberto Exp roberto $ +** $Id: lcode.c,v 2.120 2017/06/27 11:35:31 roberto Exp roberto $ ** Code generator for Lua ** See Copyright Notice in lua.h */ @@ -1079,7 +1079,7 @@ static int constfolding (FuncState *fs, int op, expdesc *e1, TValue v1, v2, res; if (!tonumeral(e1, &v1) || !tonumeral(e2, &v2) || !validop(op, &v1, &v2)) return 0; /* non-numeric operands or not safe to fold */ - luaO_arith(fs->ls->L, op, &v1, &v2, &res); /* does operation */ + luaO_rawarith(fs->ls->L, op, &v1, &v2, &res); /* does operation */ if (ttisinteger(&res)) { e1->k = VKINT; e1->u.ival = ivalue(&res); diff --git a/ldebug.c b/ldebug.c index 6971e475bf..ac0b7d547e 100644 --- a/ldebug.c +++ b/ldebug.c @@ -1,5 +1,5 @@ /* -** $Id: ldebug.c,v 2.126 2017/05/13 13:54:47 roberto Exp roberto $ +** $Id: ldebug.c,v 2.127 2017/06/27 11:35:31 roberto Exp roberto $ ** Debug Interface ** See Copyright Notice in lua.h */ @@ -35,7 +35,7 @@ /* Active Lua function (given call info) */ -#define ci_func(ci) (clLvalue((ci)->func)) +#define ci_func(ci) (clLvalue(s2v((ci)->func))) static const char *funcnamefromcode (lua_State *L, CallInfo *ci, @@ -211,16 +211,16 @@ LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) { lua_lock(L); swapextra(L); if (ar == NULL) { /* information about non-active function? */ - if (!isLfunction(L->top - 1)) /* not a Lua function? */ + if (!isLfunction(s2v(L->top - 1))) /* not a Lua function? */ name = NULL; else /* consider live variables at function start (parameters) */ - name = luaF_getlocalname(clLvalue(L->top - 1)->p, n, 0); + name = luaF_getlocalname(clLvalue(s2v(L->top - 1))->p, n, 0); } else { /* active function; get information through 'ar' */ StkId pos = NULL; /* to avoid warnings */ name = findlocal(L, ar->i_ci, n, &pos); if (name) { - setobj2s(L, L->top, pos); + setobjs2s(L, L->top, pos); api_incr_top(L); } } @@ -274,7 +274,7 @@ static int nextline (Proto *p, int currentline, int pc) { static void collectvalidlines (lua_State *L, Closure *f) { if (noLuaClosure(f)) { - setnilvalue(L->top); + setnilvalue(s2v(L->top)); api_incr_top(L); } else { @@ -283,7 +283,7 @@ static void collectvalidlines (lua_State *L, Closure *f) { Proto *p = f->l.p; int currentline = p->linedefined; Table *t = luaH_new(L); /* new table to store active lines */ - sethvalue(L, L->top, t); /* push it on stack */ + sethvalue2s(L, L->top, t); /* push it on stack */ api_incr_top(L); setbvalue(&v, 1); /* boolean 'true' to be the value of all indices */ for (i = 0; i < p->sizelineinfo; i++) { /* for all lines with code */ @@ -359,25 +359,25 @@ LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) { int status; Closure *cl; CallInfo *ci; - StkId func; + TValue *func; lua_lock(L); swapextra(L); if (*what == '>') { ci = NULL; - func = L->top - 1; + func = s2v(L->top - 1); api_check(L, ttisfunction(func), "function expected"); what++; /* skip the '>' */ L->top--; /* pop function */ } else { ci = ar->i_ci; - func = ci->func; - lua_assert(ttisfunction(ci->func)); + func = s2v(ci->func); + lua_assert(ttisfunction(func)); } cl = ttisclosure(func) ? clvalue(func) : NULL; status = auxgetinfo(L, what, ar, cl, ci); if (strchr(what, 'f')) { - setobjs2s(L, L->top, func); + setobj2s(L, L->top, func); api_incr_top(L); } swapextra(L); /* correct before option 'L', which can raise a mem. error */ @@ -627,8 +627,8 @@ static const char *funcnamefromcode (lua_State *L, CallInfo *ci, */ static int isinstack (CallInfo *ci, const TValue *o) { StkId base = ci->func + 1; - ptrdiff_t i = o - base; - return (0 <= i && i < (ci->top - base) && base + i == o); + ptrdiff_t i = cast(StkId, o) - base; + return (0 <= i && i < (ci->top - base) && s2v(base + i) == o); } @@ -659,7 +659,7 @@ static const char *varinfo (lua_State *L, const TValue *o) { kind = getupvalname(ci, o, &name); /* check whether 'o' is an upvalue */ if (!kind && isinstack(ci, o)) /* no? try a register */ kind = getobjname(ci_func(ci)->p, currentpc(ci), - cast_int(o - (ci->func + 1)), &name); + cast_int(cast(StkId, o) - (ci->func + 1)), &name); } return (kind) ? luaO_pushfstring(L, " (%s '%s')", kind, name) : ""; } diff --git a/ldo.c b/ldo.c index 02f93a41b0..5917f78f22 100644 --- a/ldo.c +++ b/ldo.c @@ -1,5 +1,5 @@ /* -** $Id: ldo.c,v 2.159 2017/05/13 13:54:47 roberto Exp roberto $ +** $Id: ldo.c,v 2.160 2017/05/23 12:50:11 roberto Exp roberto $ ** Stack and Call structure of Lua ** See Copyright Notice in lua.h */ @@ -116,7 +116,7 @@ l_noret luaD_throw (lua_State *L, int errcode) { global_State *g = G(L); L->status = cast_byte(errcode); /* mark it as dead */ if (g->mainthread->errorJmp) { /* main thread has a handler? */ - setobj2s(L, g->mainthread->top++, L->top - 1); /* copy error obj. */ + setobjs2s(L, g->mainthread->top++, L->top - 1); /* copy error obj. */ luaD_throw(g->mainthread, errcode); /* re-throw in main thread */ } else { /* no handler at all; abort */ @@ -155,12 +155,12 @@ int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) { ** Stack reallocation ** =================================================================== */ -static void correctstack (lua_State *L, TValue *oldstack) { +static void correctstack (lua_State *L, StkId oldstack) { CallInfo *ci; UpVal *up; L->top = (L->top - oldstack) + L->stack; for (up = L->openupval; up != NULL; up = up->u.open.next) - up->v = (up->v - oldstack) + L->stack; + up->v = s2v((uplevel(up) - oldstack) + L->stack); for (ci = L->ci; ci != NULL; ci = ci->previous) { ci->top = (ci->top - oldstack) + L->stack; ci->func = (ci->func - oldstack) + L->stack; @@ -173,13 +173,13 @@ static void correctstack (lua_State *L, TValue *oldstack) { void luaD_reallocstack (lua_State *L, int newsize) { - TValue *oldstack = L->stack; + StkId oldstack = L->stack; int lim = L->stacksize; lua_assert(newsize <= LUAI_MAXSTACK || newsize == ERRORSTACKSIZE); lua_assert(L->stack_last - L->stack == L->stacksize - EXTRA_STACK); - luaM_reallocvector(L, L->stack, L->stacksize, newsize, TValue); + luaM_reallocvector(L, L->stack, L->stacksize, newsize, StackValue); for (; lim < newsize; lim++) - setnilvalue(L->stack + lim); /* erase new segment */ + setnilvalue(s2v(L->stack + lim)); /* erase new segment */ L->stacksize = newsize; L->stack_last = L->stack + newsize - EXTRA_STACK; correctstack(L, oldstack); @@ -294,10 +294,10 @@ static void callhook (lua_State *L, CallInfo *ci) { ** it. Raise an error if __call metafield is not a function. */ static void tryfuncTM (lua_State *L, StkId func) { - const TValue *tm = luaT_gettmbyobj(L, func, TM_CALL); + const TValue *tm = luaT_gettmbyobj(L, s2v(func), TM_CALL); StkId p; if (!ttisfunction(tm)) - luaG_typeerror(L, func, "call"); + luaG_typeerror(L, s2v(func), "call"); /* Open a hole inside the stack at 'func' */ for (p = L->top; p > func; p--) setobjs2s(L, p, p-1); @@ -312,14 +312,15 @@ static void tryfuncTM (lua_State *L, StkId func) { ** expressions, multiple results for tail calls/single parameters) ** separated. */ -static int moveresults (lua_State *L, const TValue *firstResult, StkId res, +static int moveresults (lua_State *L, StkId firstResult, StkId res, int nres, int wanted) { switch (wanted) { /* handle typical cases separately */ case 0: break; /* nothing to move */ case 1: { /* one result needed */ if (nres == 0) /* no results? */ - firstResult = luaO_nilobject; /* adjust with nil */ - setobjs2s(L, res, firstResult); /* move it to proper place */ + setnilvalue(s2v(res)); /* adjust with nil */ + else + setobjs2s(L, res, firstResult); /* move it to proper place */ break; } case LUA_MULTRET: { @@ -339,7 +340,7 @@ static int moveresults (lua_State *L, const TValue *firstResult, StkId res, for (i = 0; i < nres; i++) /* move all results to correct place */ setobjs2s(L, res + i, firstResult + i); for (; i < wanted; i++) /* complete wanted number of results */ - setnilvalue(res + i); + setnilvalue(s2v(res + i)); } break; } @@ -385,13 +386,14 @@ int luaD_poscall (lua_State *L, CallInfo *ci, StkId firstResult, int nres) { */ int luaD_precall (lua_State *L, StkId func, int nresults) { lua_CFunction f; + TValue *funcv = s2v(func); CallInfo *ci; - switch (ttype(func)) { + switch (ttype(funcv)) { case LUA_TCCL: /* C closure */ - f = clCvalue(func)->f; + f = clCvalue(funcv)->f; goto Cfunc; case LUA_TLCF: /* light C function */ - f = fvalue(func); + f = fvalue(funcv); Cfunc: { int n; /* number of returns */ checkstackp(L, LUA_MINSTACK, func); /* ensure minimum stack size */ @@ -411,12 +413,12 @@ int luaD_precall (lua_State *L, StkId func, int nresults) { return 1; } case LUA_TLCL: { /* Lua function: prepare its call */ - Proto *p = clLvalue(func)->p; + Proto *p = clLvalue(funcv)->p; int n = cast_int(L->top - func) - 1; /* number of real arguments */ int fsize = p->maxstacksize; /* frame size */ checkstackp(L, fsize, func); for (; n < p->numparams - p->is_vararg; n++) - setnilvalue(L->top++); /* complete missing arguments */ + setnilvalue(s2v(L->top++)); /* complete missing arguments */ if (p->is_vararg) luaT_adjustvarargs(L, p, n); ci = next_ci(L); /* now 'enter' new function */ diff --git a/ldo.h b/ldo.h index 4717620f58..fedf70d43e 100644 --- a/ldo.h +++ b/ldo.h @@ -1,5 +1,5 @@ /* -** $Id: ldo.h,v 2.29 2015/12/21 13:02:14 roberto Exp roberto $ +** $Id: ldo.h,v 2.30 2017/05/13 12:57:20 roberto Exp roberto $ ** Stack and Call structure of Lua ** See Copyright Notice in lua.h */ @@ -30,7 +30,7 @@ #define savestack(L,p) ((char *)(p) - (char *)L->stack) -#define restorestack(L,n) ((TValue *)((char *)L->stack + (n))) +#define restorestack(L,n) ((StkId)((char *)L->stack + (n))) /* macro to check stack size, preserving 'p' */ diff --git a/lfunc.c b/lfunc.c index a4a4e29d4d..307a857bd9 100644 --- a/lfunc.c +++ b/lfunc.c @@ -1,5 +1,5 @@ /* -** $Id: lfunc.c,v 2.49 2017/05/24 18:54:54 roberto Exp roberto $ +** $Id: lfunc.c,v 2.50 2017/06/27 11:35:31 roberto Exp roberto $ ** Auxiliary functions to manipulate prototypes and closures ** See Copyright Notice in lua.h */ @@ -61,9 +61,8 @@ UpVal *luaF_findupval (lua_State *L, StkId level) { UpVal *p; UpVal *uv; lua_assert(isintwups(L) || L->openupval == NULL); - while ((p = *pp) != NULL && p->v >= level) { - lua_assert(upisopen(p)); - if (p->v == level && !isdead(G(L), p)) /* corresponding upvalue? */ + while ((p = *pp) != NULL && uplevel(p) >= level) { + if (uplevel(p) == level && !isdead(G(L), p)) /* corresponding upvalue? */ return p; /* return it */ pp = &p->u.open.next; } @@ -75,7 +74,7 @@ UpVal *luaF_findupval (lua_State *L, StkId level) { if (p) p->u.open.previous = &uv->u.open.next; *pp = uv; - uv->v = level; /* current value lives in the stack */ + uv->v = s2v(level); /* current value lives in the stack */ if (!isintwups(L)) { /* thread not in list of threads with upvalues? */ L->twups = G(L)->twups; /* link it to the list */ G(L)->twups = L; @@ -94,7 +93,8 @@ void luaF_unlinkupval (UpVal *uv) { void luaF_close (lua_State *L, StkId level) { UpVal *uv; - while (L->openupval != NULL && (uv = L->openupval)->v >= level) { + while (L->openupval != NULL && + (uv = L->openupval, uplevel(uv) >= level)) { TValue *slot = &uv->u.value; /* new position for value */ luaF_unlinkupval(uv); setobj(L, slot, uv->v); /* move value to upvalue slot */ diff --git a/lfunc.h b/lfunc.h index eca83e4f2e..b7d758694c 100644 --- a/lfunc.h +++ b/lfunc.h @@ -1,5 +1,5 @@ /* -** $Id: lfunc.h,v 2.16 2017/04/11 18:41:09 roberto Exp roberto $ +** $Id: lfunc.h,v 2.17 2017/05/04 13:32:01 roberto Exp roberto $ ** Auxiliary functions to manipulate prototypes and closures ** See Copyright Notice in lua.h */ @@ -32,6 +32,9 @@ #define upisopen(up) ((up)->v != &(up)->u.value) +#define uplevel(up) check_exp(upisopen(up), cast(StkId, (up)->v)) + + /* ** maximum number of misses before giving up the cache of closures ** in prototypes diff --git a/lgc.c b/lgc.c index 18a051420f..c39850a437 100644 --- a/lgc.c +++ b/lgc.c @@ -1,5 +1,5 @@ /* -** $Id: lgc.c,v 2.231 2017/06/09 16:48:44 roberto Exp roberto $ +** $Id: lgc.c,v 2.232 2017/06/12 14:21:44 roberto Exp roberto $ ** Garbage Collector ** See Copyright Notice in lua.h */ @@ -575,11 +575,11 @@ static int traversethread (global_State *g, lua_State *th) { lua_assert(g->gcstate == GCSatomic || th->openupval == NULL || isintwups(th)); for (; o < th->top; o++) /* mark live elements in the stack */ - markvalue(g, o); + markvalue(g, s2v(o)); if (g->gcstate == GCSatomic) { /* final traversal? */ StkId lim = th->stack + th->stacksize; /* real end of stack */ for (; o < lim; o++) /* clear not-marked stack slice */ - setnilvalue(o); + setnilvalue(s2v(o)); /* 'remarkupvals' may have removed thread from 'twups' list */ if (!isintwups(th) && th->openupval != NULL) { th->twups = g->twups; /* link it back to the list */ @@ -872,8 +872,8 @@ static void GCTM (lua_State *L, int propagateerrors) { g->gcrunning = running; /* restore state */ if (status != LUA_OK && propagateerrors) { /* error while running __gc? */ if (status == LUA_ERRRUN) { /* is there an error object? */ - const char *msg = (ttisstring(L->top - 1)) - ? svalue(L->top - 1) + const char *msg = (ttisstring(s2v(L->top - 1))) + ? svalue(s2v(L->top - 1)) : "no message"; luaO_pushfstring(L, "error in __gc metamethod (%s)", msg); status = LUA_ERRGCMM; /* error in __gc metamethod */ diff --git a/llex.c b/llex.c index d840a3ca16..eaa400fad2 100644 --- a/llex.c +++ b/llex.c @@ -1,5 +1,5 @@ /* -** $Id: llex.c,v 2.96 2016/05/02 14:02:12 roberto Exp roberto $ +** $Id: llex.c,v 2.97 2017/06/09 16:48:44 roberto Exp roberto $ ** Lexical Analyzer ** See Copyright Notice in lua.h */ @@ -129,7 +129,7 @@ TString *luaX_newstring (LexState *ls, const char *str, size_t l) { TValue *o; /* entry for 'str' */ TString *ts = luaS_newlstr(L, str, l); /* create new string */ setsvalue2s(L, L->top++, ts); /* temporarily anchor it in stack */ - o = luaH_set(L, ls->h, L->top - 1); + o = luaH_set(L, ls->h, s2v(L->top - 1)); if (ttisnil(o)) { /* not in use yet? */ /* boolean value does not need GC barrier; table is not a metatable, so it does not need to invalidate cache */ diff --git a/lobject.c b/lobject.c index 43938c9c2d..9ad833c994 100644 --- a/lobject.c +++ b/lobject.c @@ -1,5 +1,5 @@ /* -** $Id: lobject.c,v 2.114 2017/04/19 16:34:35 roberto Exp roberto $ +** $Id: lobject.c,v 2.115 2017/05/24 13:47:11 roberto Exp roberto $ ** Some generic functions over Lua objects ** See Copyright Notice in lua.h */ @@ -120,8 +120,8 @@ static lua_Number numarith (lua_State *L, int op, lua_Number v1, } -void luaO_arith (lua_State *L, int op, const TValue *p1, const TValue *p2, - TValue *res) { +int luaO_rawarith (lua_State *L, int op, const TValue *p1, const TValue *p2, + TValue *res) { switch (op) { case LUA_OPBAND: case LUA_OPBOR: case LUA_OPBXOR: case LUA_OPSHL: case LUA_OPSHR: @@ -129,33 +129,40 @@ void luaO_arith (lua_State *L, int op, const TValue *p1, const TValue *p2, lua_Integer i1; lua_Integer i2; if (tointeger(p1, &i1) && tointeger(p2, &i2)) { setivalue(res, intarith(L, op, i1, i2)); - return; + return 1; } - else break; /* go to the end */ + else return 0; /* fail */ } case LUA_OPDIV: case LUA_OPPOW: { /* operate only on floats */ lua_Number n1; lua_Number n2; if (tonumber(p1, &n1) && tonumber(p2, &n2)) { setfltvalue(res, numarith(L, op, n1, n2)); - return; + return 1; } - else break; /* go to the end */ + else return 0; /* fail */ } default: { /* other operations */ lua_Number n1; lua_Number n2; if (ttisinteger(p1) && ttisinteger(p2)) { setivalue(res, intarith(L, op, ivalue(p1), ivalue(p2))); - return; + return 1; } else if (tonumber(p1, &n1) && tonumber(p2, &n2)) { setfltvalue(res, numarith(L, op, n1, n2)); - return; + return 1; } - else break; /* go to the end */ + else return 0; /* fail */ } } - /* could not perform raw operation; try metamethod */ - luaT_trybinTM(L, p1, p2, res, cast(TMS, (op - LUA_OPADD) + TM_ADD)); +} + + +void luaO_arith (lua_State *L, int op, const TValue *p1, const TValue *p2, + StkId res) { + if (!luaO_rawarith(L, op, p1, p2, s2v(res))) { + /* could not perform raw operation; try metamethod */ + luaT_trybinTM(L, p1, p2, res, cast(TMS, (op - LUA_OPADD) + TM_ADD)); + } } @@ -367,7 +374,7 @@ int luaO_utf8esc (char *buff, unsigned long x) { /* ** Convert a number object to a string */ -void luaO_tostring (lua_State *L, StkId obj) { +void luaO_tostring (lua_State *L, TValue *obj) { char buff[MAXNUMBER2STR]; size_t len; lua_assert(ttisnumber(obj)); @@ -382,7 +389,7 @@ void luaO_tostring (lua_State *L, StkId obj) { } #endif } - setsvalue2s(L, obj, luaS_newlstr(L, buff, len)); + setsvalue(L, obj, luaS_newlstr(L, buff, len)); } @@ -418,18 +425,18 @@ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) { break; } case 'd': { /* an 'int' */ - setivalue(L->top, va_arg(argp, int)); + setivalue(s2v(L->top), va_arg(argp, int)); goto top2str; } case 'I': { /* a 'lua_Integer' */ - setivalue(L->top, cast(lua_Integer, va_arg(argp, l_uacInt))); + setivalue(s2v(L->top), cast(lua_Integer, va_arg(argp, l_uacInt))); goto top2str; } case 'f': { /* a 'lua_Number' */ - setfltvalue(L->top, cast_num(va_arg(argp, l_uacNumber))); + setfltvalue(s2v(L->top), cast_num(va_arg(argp, l_uacNumber))); top2str: /* convert the top element to a string */ luaD_inctop(L); - luaO_tostring(L, L->top - 1); + luaO_tostring(L, s2v(L->top - 1)); break; } case 'p': { /* a pointer */ @@ -460,7 +467,7 @@ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) { luaD_checkstack(L, 1); pushstr(L, fmt, strlen(fmt)); if (n > 0) luaV_concat(L, n + 1); - return svalue(L->top - 1); + return svalue(s2v(L->top - 1)); } diff --git a/lobject.h b/lobject.h index 9af501af7f..769be79def 100644 --- a/lobject.h +++ b/lobject.h @@ -1,5 +1,5 @@ /* -** $Id: lobject.h,v 2.123 2017/06/12 14:21:44 roberto Exp roberto $ +** $Id: lobject.h,v 2.124 2017/06/27 11:35:31 roberto Exp roberto $ ** Type definitions for Lua objects ** See Copyright Notice in lua.h */ @@ -110,7 +110,7 @@ typedef union Value { #define TValuefields Value value_; lu_byte tt_ -typedef struct lua_TValue { +typedef struct TValue { TValuefields; } TValue; @@ -282,13 +282,15 @@ typedef struct lua_TValue { ** different types of assignments, according to destination */ -/* from stack to (same) stack */ -#define setobjs2s setobj +/* from stack to stack */ +#define setobjs2s(L,o1,o2) setobj(L,s2v(o1),s2v(o2)) /* to stack (not from same stack) */ -#define setobj2s setobj -#define setsvalue2s setsvalue -#define sethvalue2s sethvalue -#define setptvalue2s setptvalue +#define setobj2s(L,o1,o2) setobj(L,s2v(o1),o2) +#define setsvalue2s(L,o,s) setsvalue(L,s2v(o),s) +#define sethvalue2s(L,o,h) sethvalue(L,s2v(o),h) +#define setthvalue2s(L,o,t) setthvalue(L,s2v(o),t) +#define setptvalue2s(L,o,p) setptvalue(L,s2v(o),p) +#define setclLvalue2s(L,o,cl) setclLvalue(L,s2v(o),cl) /* from table to same table */ #define setobjt2t setobj /* to new object */ @@ -307,9 +309,16 @@ typedef struct lua_TValue { */ -typedef TValue *StkId; /* index to stack elements */ +typedef union StackValue { + TValue val; +} StackValue; +typedef StackValue *StkId; /* index to stack elements */ + +/* convert a 'StackValue' to a 'TValue' */ +#define s2v(o) (&(o)->val) + /* @@ -620,11 +629,13 @@ LUAI_FUNC int luaO_int2fb (unsigned int x); LUAI_FUNC int luaO_fb2int (int x); LUAI_FUNC int luaO_utf8esc (char *buff, unsigned long x); LUAI_FUNC int luaO_ceillog2 (unsigned int x); +LUAI_FUNC int luaO_rawarith (lua_State *L, int op, const TValue *p1, + const TValue *p2, TValue *res); LUAI_FUNC void luaO_arith (lua_State *L, int op, const TValue *p1, - const TValue *p2, TValue *res); + const TValue *p2, StkId res); LUAI_FUNC size_t luaO_str2num (const char *s, TValue *o); LUAI_FUNC int luaO_hexavalue (int c); -LUAI_FUNC void luaO_tostring (lua_State *L, StkId obj); +LUAI_FUNC void luaO_tostring (lua_State *L, TValue *obj); LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp); LUAI_FUNC const char *luaO_pushfstring (lua_State *L, const char *fmt, ...); diff --git a/lparser.c b/lparser.c index 5e244e24ba..a8c77c44d7 100644 --- a/lparser.c +++ b/lparser.c @@ -1,5 +1,5 @@ /* -** $Id: lparser.c,v 2.159 2017/05/13 12:57:20 roberto Exp roberto $ +** $Id: lparser.c,v 2.160 2017/06/27 11:35:31 roberto Exp roberto $ ** Lua Parser ** See Copyright Notice in lua.h */ @@ -1650,10 +1650,10 @@ LClosure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, LexState lexstate; FuncState funcstate; LClosure *cl = luaF_newLclosure(L, 1); /* create main closure */ - setclLvalue(L, L->top, cl); /* anchor it (to avoid being collected) */ + setclLvalue2s(L, L->top, cl); /* anchor it (to avoid being collected) */ luaD_inctop(L); lexstate.h = luaH_new(L); /* create table for scanner */ - sethvalue(L, L->top, lexstate.h); /* anchor it */ + sethvalue2s(L, L->top, lexstate.h); /* anchor it */ luaD_inctop(L); funcstate.f = cl->p = luaF_newproto(L); funcstate.f->source = luaS_new(L, name); /* create and anchor TString */ diff --git a/lstate.c b/lstate.c index 5f1c6e4836..b2c3e0c49b 100644 --- a/lstate.c +++ b/lstate.c @@ -1,5 +1,5 @@ /* -** $Id: lstate.c,v 2.139 2017/05/04 13:32:01 roberto Exp roberto $ +** $Id: lstate.c,v 2.140 2017/05/26 19:14:29 roberto Exp roberto $ ** Global State ** See Copyright Notice in lua.h */ @@ -143,10 +143,10 @@ void luaE_shrinkCI (lua_State *L) { static void stack_init (lua_State *L1, lua_State *L) { int i; CallInfo *ci; /* initialize stack array */ - L1->stack = luaM_newvector(L, BASIC_STACK_SIZE, TValue); + L1->stack = luaM_newvector(L, BASIC_STACK_SIZE, StackValue); L1->stacksize = BASIC_STACK_SIZE; for (i = 0; i < BASIC_STACK_SIZE; i++) - setnilvalue(L1->stack + i); /* erase new stack */ + setnilvalue(s2v(L1->stack + i)); /* erase new stack */ L1->top = L1->stack; L1->stack_last = L1->stack + L1->stacksize - EXTRA_STACK; /* initialize first ci */ @@ -154,7 +154,7 @@ static void stack_init (lua_State *L1, lua_State *L) { ci->next = ci->previous = NULL; ci->callstatus = 0; ci->func = L1->top; - setnilvalue(L1->top++); /* 'function' entry for this 'ci' */ + setnilvalue(s2v(L1->top++)); /* 'function' entry for this 'ci' */ ci->top = L1->top + LUA_MINSTACK; L1->ci = ci; } @@ -258,7 +258,7 @@ LUA_API lua_State *lua_newthread (lua_State *L) { L1->next = g->allgc; g->allgc = obj2gco(L1); /* anchor it on L stack */ - setthvalue(L, L->top, L1); + setthvalue2s(L, L->top, L1); api_incr_top(L); preinit_thread(L1, g); L1->hookmask = L->hookmask; diff --git a/ltable.c b/ltable.c index b8244dca3a..37fc3d0bb3 100644 --- a/ltable.c +++ b/ltable.c @@ -1,5 +1,5 @@ /* -** $Id: ltable.c,v 2.123 2017/06/09 16:48:44 roberto Exp roberto $ +** $Id: ltable.c,v 2.124 2017/06/12 14:21:44 roberto Exp roberto $ ** Lua tables (hash) ** See Copyright Notice in lua.h */ @@ -211,7 +211,7 @@ static unsigned int arrayindex (lua_Integer k) { ** elements in the array part, then elements in the hash part. The ** beginning of a traversal is signaled by 0. */ -static unsigned int findindex (lua_State *L, Table *t, StkId key) { +static unsigned int findindex (lua_State *L, Table *t, TValue *key) { unsigned int i; if (ttisnil(key)) return 0; /* first iteration */ i = ttisinteger(key) ? arrayindex(ivalue(key)) : 0; @@ -229,18 +229,18 @@ static unsigned int findindex (lua_State *L, Table *t, StkId key) { int luaH_next (lua_State *L, Table *t, StkId key) { - unsigned int i = findindex(L, t, key); /* find original element */ + unsigned int i = findindex(L, t, s2v(key)); /* find original element */ for (; i < t->sizearray; i++) { /* try first array part */ if (!ttisnil(&t->array[i])) { /* a non-nil value? */ - setivalue(key, i + 1); - setobj2s(L, key+1, &t->array[i]); + setivalue(s2v(key), i + 1); + setobj2s(L, key + 1, &t->array[i]); return 1; } } for (i -= t->sizearray; cast_int(i) < sizenode(t); i++) { /* hash part */ if (!ttisnil(gval(gnode(t, i)))) { /* a non-nil value? */ Node *n = gnode(t, i); - getnodekey(L, key, n); + getnodekey(L, s2v(key), n); setobj2s(L, key + 1, gval(n)); return 1; } diff --git a/ltests.c b/ltests.c index 36da64e8b9..4388e1c9d6 100644 --- a/ltests.c +++ b/ltests.c @@ -1,5 +1,5 @@ /* -** $Id: ltests.c,v 2.221 2017/06/27 11:35:31 roberto Exp roberto $ +** $Id: ltests.c,v 2.222 2017/06/27 18:32:49 roberto Exp roberto $ ** Internal Module for Debugging of the Lua Implementation ** See Copyright Notice in lua.h */ @@ -46,7 +46,7 @@ void *l_Trick = 0; int islocked = 0; -#define obj_at(L,k) (L->ci->func + (k)) +#define obj_at(L,k) s2v(L->ci->func + (k)) static int runC (lua_State *L, lua_State *L1, const char *pc); @@ -316,7 +316,7 @@ static int lua_checkpc (lua_State *L, CallInfo *ci) { StkId f = (L->status != LUA_YIELD || ci != L->ci) ? ci->func : restorestack(L, ci->extra); - Proto *p = clLvalue(f)->p; + Proto *p = clLvalue(s2v(f))->p; return p->code <= ci->u.l.savedpc && ci->u.l.savedpc <= p->code + p->sizecode; } @@ -336,7 +336,7 @@ static void checkstack (global_State *g, lua_State *L1) { } if (L1->stack) { /* complete thread? */ for (o = L1->stack; o < L1->stack_last + EXTRA_STACK; o++) - checkliveness(L1, o); /* entire stack must have valid values */ + checkliveness(L1, s2v(o)); /* entire stack must have valid values */ } else lua_assert(L1->stacksize == 0); } diff --git a/ltm.c b/ltm.c index 9da191f604..3629dc69f0 100644 --- a/ltm.c +++ b/ltm.c @@ -1,5 +1,5 @@ /* -** $Id: ltm.c,v 2.40 2017/05/08 15:57:23 roberto Exp roberto $ +** $Id: ltm.c,v 2.41 2017/05/13 12:57:20 roberto Exp roberto $ ** Tag methods ** See Copyright Notice in lua.h */ @@ -100,24 +100,36 @@ const char *luaT_objtypename (lua_State *L, const TValue *o) { void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1, - const TValue *p2, TValue *p3, int hasres) { - ptrdiff_t result = savestack(L, p3); + const TValue *p2, const TValue *p3) { + StkId func = L->top; + setobj2s(L, func, f); /* push function (assume EXTRA_STACK) */ + setobj2s(L, func + 1, p1); /* 1st argument */ + setobj2s(L, func + 2, p2); /* 2nd argument */ + setobj2s(L, func + 3, p3); /* 3rd argument */ + L->top += 4; + /* metamethod may yield only when called from Lua code */ + if (isLua(L->ci)) + luaD_call(L, func, 0); + else + luaD_callnoyield(L, func, 0); +} + + +void luaT_callTMres (lua_State *L, const TValue *f, const TValue *p1, + const TValue *p2, StkId res) { + ptrdiff_t result = savestack(L, res); StkId func = L->top; setobj2s(L, func, f); /* push function (assume EXTRA_STACK) */ setobj2s(L, func + 1, p1); /* 1st argument */ setobj2s(L, func + 2, p2); /* 2nd argument */ L->top += 3; - if (!hasres) /* no result? 'p3' is third argument */ - setobj2s(L, L->top++, p3); /* 3rd argument */ /* metamethod may yield only when called from Lua code */ if (isLua(L->ci)) - luaD_call(L, func, hasres); + luaD_call(L, func, 1); else - luaD_callnoyield(L, func, hasres); - if (hasres) { /* if has result, move it to its place */ - p3 = restorestack(L, result); - setobjs2s(L, p3, --L->top); - } + luaD_callnoyield(L, func, 1); + res = restorestack(L, result); + setobjs2s(L, res, --L->top); /* more result to its place */ } @@ -127,7 +139,7 @@ static int callbinTM (lua_State *L, const TValue *p1, const TValue *p2, if (ttisnil(tm)) tm = luaT_gettmbyobj(L, p2, event); /* try second operand */ if (ttisnil(tm)) return 0; - luaT_callTM(L, tm, p1, p2, res, 1); + luaT_callTMres(L, tm, p1, p2, res); return 1; } @@ -160,7 +172,7 @@ int luaT_callorderTM (lua_State *L, const TValue *p1, const TValue *p2, if (!callbinTM(L, p1, p2, L->top, event)) return -1; /* no metamethod */ else - return !l_isfalse(L->top); + return !l_isfalse(s2v(L->top)); } @@ -171,19 +183,19 @@ void luaT_adjustvarargs (lua_State *L, Proto *p, int actual) { int nfixparams = p->numparams - 1; /* number of fixed parameters */ actual -= nfixparams; /* number of extra arguments */ vtab = luaH_new(L); /* create vararg table */ - sethvalue(L, L->top, vtab); /* anchor it for resizing */ + sethvalue2s(L, L->top, vtab); /* anchor it for resizing */ L->top++; /* space ensured by caller */ luaH_resize(L, vtab, actual, 1); for (i = 0; i < actual; i++) /* put extra arguments into vararg table */ - setobj2n(L, &vtab->array[i], L->top - actual + i - 1); + setobj2n(L, &vtab->array[i], s2v(L->top - actual + i - 1)); setsvalue(L, &nname, luaS_newliteral(L, "n")); /* get field 'n' */ setivalue(luaH_set(L, vtab, &nname), actual); /* store counter there */ L->top -= actual; /* remove extra elements from the stack */ - sethvalue(L, L->top - 1, vtab); /* move table to new top */ + sethvalue2s(L, L->top - 1, vtab); /* move table to new top */ } -void luaT_getvarargs (lua_State *L, StkId t, StkId where, int wanted) { +void luaT_getvarargs (lua_State *L, TValue *t, StkId where, int wanted) { if (!ttistable(t)) luaG_runerror(L, "'vararg' parameter is not a table"); else { diff --git a/ltm.h b/ltm.h index 2dfb46c11d..c98b10378f 100644 --- a/ltm.h +++ b/ltm.h @@ -1,5 +1,5 @@ /* -** $Id: ltm.h,v 2.23 2017/05/08 15:57:23 roberto Exp roberto $ +** $Id: ltm.h,v 2.24 2017/05/13 12:57:20 roberto Exp roberto $ ** Tag methods ** See Copyright Notice in lua.h */ @@ -63,14 +63,16 @@ LUAI_FUNC const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, LUAI_FUNC void luaT_init (lua_State *L); LUAI_FUNC void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1, - const TValue *p2, TValue *p3, int hasres); + const TValue *p2, const TValue *p3); +LUAI_FUNC void luaT_callTMres (lua_State *L, const TValue *f, + const TValue *p1, const TValue *p2, StkId p3); LUAI_FUNC void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2, StkId res, TMS event); LUAI_FUNC int luaT_callorderTM (lua_State *L, const TValue *p1, const TValue *p2, TMS event); LUAI_FUNC void luaT_adjustvarargs (lua_State *L, Proto *p, int actual); -LUAI_FUNC void luaT_getvarargs (lua_State *L, StkId t, StkId where, +LUAI_FUNC void luaT_getvarargs (lua_State *L, TValue *t, StkId where, int wanted); diff --git a/lundump.c b/lundump.c index f98c70e133..1f2f1a925a 100644 --- a/lundump.c +++ b/lundump.c @@ -1,5 +1,5 @@ /* -** $Id: lundump.c,v 2.45 2017/06/27 11:35:31 roberto Exp roberto $ +** $Id: lundump.c,v 2.46 2017/06/27 14:21:12 roberto Exp roberto $ ** load precompiled Lua chunks ** See Copyright Notice in lua.h */ @@ -283,7 +283,7 @@ LClosure *luaU_undump(lua_State *L, ZIO *Z, const char *name) { S.Z = Z; checkHeader(&S); cl = luaF_newLclosure(L, LoadByte(&S)); - setclLvalue(L, L->top, cl); + setclLvalue2s(L, L->top, cl); luaD_inctop(L); cl->p = luaF_newproto(L); LoadFunction(&S, cl->p, NULL); diff --git a/lvm.c b/lvm.c index 8533af63ae..05669d7b34 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.286 2017/06/01 20:22:33 roberto Exp roberto $ +** $Id: lvm.c,v 2.287 2017/06/09 19:16:41 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -172,13 +172,13 @@ void luaV_finishget (lua_State *L, const TValue *t, TValue *key, StkId val, lua_assert(ttisnil(slot)); tm = fasttm(L, hvalue(t)->metatable, TM_INDEX); /* table's metamethod */ if (tm == NULL) { /* no metamethod? */ - setnilvalue(val); /* result is nil */ + setnilvalue(s2v(val)); /* result is nil */ return; } /* else will try the metamethod */ } if (ttisfunction(tm)) { /* is metamethod a function? */ - luaT_callTM(L, tm, t, key, val, 1); /* call it */ + luaT_callTMres(L, tm, t, key, val); /* call it */ return; } t = tm; /* else try to access 'tm[key]' */ @@ -200,7 +200,7 @@ void luaV_finishget (lua_State *L, const TValue *t, TValue *key, StkId val, ** would have done the job.) */ void luaV_finishset (lua_State *L, const TValue *t, TValue *key, - StkId val, const TValue *slot) { + TValue *val, const TValue *slot) { int loop; /* counter to avoid infinite loops */ for (loop = 0; loop < MAXTAGLOOP; loop++) { const TValue *tm; /* '__newindex' metamethod */ @@ -225,7 +225,7 @@ void luaV_finishset (lua_State *L, const TValue *t, TValue *key, } /* try the metamethod */ if (ttisfunction(tm)) { - luaT_callTM(L, tm, t, key, val, 0); + luaT_callTM(L, tm, t, key, val); return; } t = tm; /* else repeat assignment over 'tm' */ @@ -446,8 +446,8 @@ int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2) { } if (tm == NULL) /* no TM? */ return 0; /* objects are different */ - luaT_callTM(L, tm, t1, t2, L->top, 1); /* call TM */ - return !l_isfalse(L->top); + luaT_callTMres(L, tm, t1, t2, L->top); /* call TM */ + return !l_isfalse(s2v(L->top)); } @@ -461,8 +461,8 @@ int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2) { static void copy2buff (StkId top, int n, char *buff) { size_t tl = 0; /* size already copied */ do { - size_t l = vslen(top - n); /* length of string being copied */ - memcpy(buff + tl, svalue(top - n), l * sizeof(char)); + size_t l = vslen(s2v(top - n)); /* length of string being copied */ + memcpy(buff + tl, svalue(s2v(top - n)), l * sizeof(char)); tl += l; } while (--n > 0); } @@ -477,20 +477,21 @@ void luaV_concat (lua_State *L, int total) { do { StkId top = L->top; int n = 2; /* number of elements handled in this pass (at least 2) */ - if (!(ttisstring(top-2) || cvt2str(top-2)) || !tostring(L, top-1)) - luaT_trybinTM(L, top-2, top-1, top-2, TM_CONCAT); - else if (isemptystr(top - 1)) /* second operand is empty? */ - cast_void(tostring(L, top - 2)); /* result is first operand */ - else if (isemptystr(top - 2)) { /* first operand is an empty string? */ + if (!(ttisstring(s2v(top - 2)) || cvt2str(s2v(top - 2))) || + !tostring(L, s2v(top - 1))) + luaT_trybinTM(L, s2v(top - 2), s2v(top - 1), top - 2, TM_CONCAT); + else if (isemptystr(s2v(top - 1))) /* second operand is empty? */ + cast_void(tostring(L, s2v(top - 2))); /* result is first operand */ + else if (isemptystr(s2v(top - 2))) { /* first operand is empty string? */ setobjs2s(L, top - 2, top - 1); /* result is second op. */ } else { /* at least two non-empty string values; get as many as possible */ - size_t tl = vslen(top - 1); + size_t tl = vslen(s2v(top - 1)); TString *ts; /* collect total length and number of strings */ - for (n = 1; n < total && tostring(L, top - n - 1); n++) { - size_t l = vslen(top - n - 1); + for (n = 1; n < total && tostring(L, s2v(top - n - 1)); n++) { + size_t l = vslen(s2v(top - n - 1)); if (l >= (MAX_SIZE/sizeof(char)) - tl) luaG_runerror(L, "string length overflow"); tl += l; @@ -522,15 +523,15 @@ void luaV_objlen (lua_State *L, StkId ra, const TValue *rb) { Table *h = hvalue(rb); tm = fasttm(L, h->metatable, TM_LEN); if (tm) break; /* metamethod? break switch to call it */ - setivalue(ra, luaH_getn(h)); /* else primitive len */ + setivalue(s2v(ra), luaH_getn(h)); /* else primitive len */ return; } case LUA_TSHRSTR: { - setivalue(ra, tsvalue(rb)->shrlen); + setivalue(s2v(ra), tsvalue(rb)->shrlen); return; } case LUA_TLNGSTR: { - setivalue(ra, tsvalue(rb)->u.lnglen); + setivalue(s2v(ra), tsvalue(rb)->u.lnglen); return; } default: { /* try metamethod */ @@ -540,7 +541,7 @@ void luaV_objlen (lua_State *L, StkId ra, const TValue *rb) { break; } } - luaT_callTM(L, tm, rb, rb, ra, 1); + luaT_callTMres(L, tm, rb, rb, ra); } @@ -615,7 +616,7 @@ static LClosure *getcached (Proto *p, UpVal **encup, StkId base) { Upvaldesc *uv = p->upvalues; int i; for (i = 0; i < nup; i++) { /* check whether it has right upvalues */ - TValue *v = uv[i].instack ? base + uv[i].idx : encup[uv[i].idx]->v; + TValue *v = uv[i].instack ? s2v(base + uv[i].idx) : encup[uv[i].idx]->v; if (c->upvals[i]->v != v) return NULL; /* wrong upvalue; cannot reuse closure */ } @@ -636,7 +637,7 @@ static void pushclosure (lua_State *L, Proto *p, UpVal **encup, StkId base, int i; LClosure *ncl = luaF_newLclosure(L, nup); ncl->p = p; - setclLvalue(L, ra, ncl); /* anchor new closure in stack */ + setclLvalue2s(L, ra, ncl); /* anchor new closure in stack */ for (i = 0; i < nup; i++) { /* fill in its upvalues */ if (uv[i].instack) /* upvalue refers to local variable? */ ncl->upvals[i] = luaF_findupval(L, base + uv[i].idx); @@ -674,7 +675,7 @@ void luaV_finishOp (lua_State *L) { break; } case OP_LE: case OP_LT: case OP_EQ: { - int res = !l_isfalse(L->top - 1); + int res = !l_isfalse(s2v(L->top - 1)); L->top--; if (ci->callstatus & CIST_LEQ) { /* "<=" using "<" instead? */ lua_assert(op == OP_LE); @@ -734,13 +735,15 @@ void luaV_finishOp (lua_State *L) { #define RA(i) (base+GETARG_A(i)) #define RB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgR, base+GETARG_Br(i)) +#define vRB(i) s2v(RB(i)) #define KB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgK, k+GETARG_B(i)) #define RC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgR, base+GETARG_C(i)) +#define vRC(i) s2v(RC(i)) #define KC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgK, k+GETARG_C(i)) #define RKB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgK, \ - (GETARG_Bk(i)) ? k + GETARG_Br(i) : base + GETARG_Br(i)) + (GETARG_Bk(i)) ? k + GETARG_Br(i) : s2v(base + GETARG_Br(i))) #define RKC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgK, \ - (GETARG_Ck(i)) ? k + GETARG_Cr(i) : base + GETARG_Cr(i)) + (GETARG_Ck(i)) ? k + GETARG_Cr(i) : s2v(base + GETARG_Cr(i))) @@ -803,7 +806,7 @@ void luaV_execute (lua_State *L) { ci->callstatus |= CIST_FRESH; /* fresh invocation of 'luaV_execute" */ newframe: /* reentry point when frame changes (call/return) */ lua_assert(ci == L->ci); - cl = clLvalue(ci->func); /* local reference to function's closure */ + cl = clLvalue(s2v(ci->func)); /* local reference to function's closure */ k = cl->p->k; /* local reference to function's constant table */ updatemask(L); base = ci->func + 1; @@ -827,7 +830,7 @@ void luaV_execute (lua_State *L) { } vmcase(OP_LOADI) { lua_Integer b = GETARG_sBx(i); - setivalue(ra, b); + setivalue(s2v(ra), b); vmbreak; } vmcase(OP_LOADKX) { @@ -838,14 +841,14 @@ void luaV_execute (lua_State *L) { vmbreak; } vmcase(OP_LOADBOOL) { - setbvalue(ra, GETARG_B(i)); + setbvalue(s2v(ra), GETARG_B(i)); if (GETARG_C(i)) pc++; /* skip next instruction (if C) */ vmbreak; } vmcase(OP_LOADNIL) { int b = GETARG_B(i); do { - setnilvalue(ra++); + setnilvalue(s2v(ra++)); } while (b--); vmbreak; } @@ -856,8 +859,8 @@ void luaV_execute (lua_State *L) { } vmcase(OP_SETUPVAL) { UpVal *uv = cl->upvals[GETARG_B(i)]; - setobj(L, uv->v, ra); - luaC_barrier(L, uv, ra); + setobj(L, uv->v, s2v(ra)); + luaC_barrier(L, uv, s2v(ra)); vmbreak; } vmcase(OP_GETTABUP) { @@ -873,8 +876,8 @@ void luaV_execute (lua_State *L) { } vmcase(OP_GETTABLE) { const TValue *slot; - StkId rb = RB(i); - TValue *rc = RC(i); + TValue *rb = vRB(i); + TValue *rc = vRC(i); lua_Unsigned n; if (ttisinteger(rc) /* fast track for integers? */ ? (n = ivalue(rc), luaV_fastgeti(L, rb, n, slot)) @@ -887,7 +890,7 @@ void luaV_execute (lua_State *L) { } vmcase(OP_GETI) { const TValue *slot; - StkId rb = RB(i); + TValue *rb = vRB(i); int c = GETARG_C(i); if (luaV_fastgeti(L, rb, c, slot)) { setobj2s(L, ra, slot); @@ -901,7 +904,7 @@ void luaV_execute (lua_State *L) { } vmcase(OP_GETFIELD) { const TValue *slot; - StkId rb = RB(i); + TValue *rb = vRB(i); TValue *rc = KC(i); TString *key = tsvalue(rc); /* key must be a string */ if (luaV_fastget(L, rb, key, slot, luaH_getshortstr)) { @@ -925,29 +928,29 @@ void luaV_execute (lua_State *L) { } vmcase(OP_SETTABLE) { const TValue *slot; - TValue *rb = RB(i); /* key (table is in 'ra') */ + TValue *rb = vRB(i); /* key (table is in 'ra') */ TValue *rc = RKC(i); /* value */ lua_Unsigned n; if (ttisinteger(rb) /* fast track for integers? */ - ? (n = ivalue(rb), luaV_fastgeti(L, ra, n, slot)) - : luaV_fastget(L, ra, rb, slot, luaH_get)) { - luaV_finishfastset(L, ra, slot, rc); + ? (n = ivalue(rb), luaV_fastgeti(L, s2v(ra), n, slot)) + : luaV_fastget(L, s2v(ra), rb, slot, luaH_get)) { + luaV_finishfastset(L, s2v(ra), slot, rc); } else - Protect(luaV_finishset(L, ra, rb, rc, slot)); + Protect(luaV_finishset(L, s2v(ra), rb, rc, slot)); vmbreak; } vmcase(OP_SETI) { const TValue *slot; int c = GETARG_B(i); TValue *rc = RKC(i); - if (luaV_fastgeti(L, ra, c, slot)) { - luaV_finishfastset(L, ra, slot, rc); + if (luaV_fastgeti(L, s2v(ra), c, slot)) { + luaV_finishfastset(L, s2v(ra), slot, rc); } else { TValue key; setivalue(&key, c); - Protect(luaV_finishset(L, ra, &key, rc, slot)); + Protect(luaV_finishset(L, s2v(ra), &key, rc, slot)); } vmbreak; } @@ -956,11 +959,11 @@ void luaV_execute (lua_State *L) { TValue *rb = KB(i); TValue *rc = RKC(i); TString *key = tsvalue(rb); /* key must be a string */ - if (luaV_fastget(L, ra, key, slot, luaH_getshortstr)) { - luaV_finishfastset(L, ra, slot, rc); + if (luaV_fastget(L, s2v(ra), key, slot, luaH_getshortstr)) { + luaV_finishfastset(L, s2v(ra), slot, rc); } else - Protect(luaV_finishset(L, ra, rb, rc, slot)); + Protect(luaV_finishset(L, s2v(ra), rb, rc, slot)); vmbreak; } vmcase(OP_NEWTABLE) { @@ -969,7 +972,7 @@ void luaV_execute (lua_State *L) { Table *t; savepc(L); /* in case of allocation errors */ t = luaH_new(L); - sethvalue(L, ra, t); + sethvalue2s(L, ra, t); if (b != 0 || c != 0) luaH_resize(L, t, luaO_fb2int(b), luaO_fb2int(c)); checkGC(L, ra + 1); @@ -977,10 +980,10 @@ void luaV_execute (lua_State *L) { } vmcase(OP_SELF) { const TValue *slot; - StkId rb = RB(i); + TValue *rb = vRB(i); TValue *rc = RKC(i); TString *key = tsvalue(rc); /* key must be a string */ - setobjs2s(L, ra + 1, rb); + setobj2s(L, ra + 1, rb); if (luaV_fastget(L, rb, key, slot, luaH_getstr)) { setobj2s(L, ra, slot); } @@ -988,14 +991,14 @@ void luaV_execute (lua_State *L) { vmbreak; } vmcase(OP_ADDI) { - TValue *rb = RB(i); + TValue *rb = vRB(i); int ic = GETARG_C(i); lua_Number nb; if (ttisinteger(rb)) { - setivalue(ra, intop(+, ivalue(rb), ic)); + setivalue(s2v(ra), intop(+, ivalue(rb), ic)); } else if (tonumber(rb, &nb)) { - setfltvalue(ra, luai_numadd(L, nb, cast_num(ic))); + setfltvalue(s2v(ra), luai_numadd(L, nb, cast_num(ic))); } else { TValue aux; TValue *rc; @@ -1014,10 +1017,10 @@ void luaV_execute (lua_State *L) { lua_Number nb; lua_Number nc; if (ttisinteger(rb) && ttisinteger(rc)) { lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc); - setivalue(ra, intop(+, ib, ic)); + setivalue(s2v(ra), intop(+, ib, ic)); } else if (tonumber(rb, &nb) && tonumber(rc, &nc)) { - setfltvalue(ra, luai_numadd(L, nb, nc)); + setfltvalue(s2v(ra), luai_numadd(L, nb, nc)); } else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_ADD)); } vmbreak; @@ -1028,10 +1031,10 @@ void luaV_execute (lua_State *L) { lua_Number nb; lua_Number nc; if (ttisinteger(rb) && ttisinteger(rc)) { lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc); - setivalue(ra, intop(-, ib, ic)); + setivalue(s2v(ra), intop(-, ib, ic)); } else if (tonumber(rb, &nb) && tonumber(rc, &nc)) { - setfltvalue(ra, luai_numsub(L, nb, nc)); + setfltvalue(s2v(ra), luai_numsub(L, nb, nc)); } else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_SUB)); } vmbreak; @@ -1042,10 +1045,10 @@ void luaV_execute (lua_State *L) { lua_Number nb; lua_Number nc; if (ttisinteger(rb) && ttisinteger(rc)) { lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc); - setivalue(ra, intop(*, ib, ic)); + setivalue(s2v(ra), intop(*, ib, ic)); } else if (tonumber(rb, &nb) && tonumber(rc, &nc)) { - setfltvalue(ra, luai_nummul(L, nb, nc)); + setfltvalue(s2v(ra), luai_nummul(L, nb, nc)); } else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_MUL)); } vmbreak; @@ -1055,7 +1058,7 @@ void luaV_execute (lua_State *L) { TValue *rc = RKC(i); lua_Number nb; lua_Number nc; if (tonumber(rb, &nb) && tonumber(rc, &nc)) { - setfltvalue(ra, luai_numdiv(L, nb, nc)); + setfltvalue(s2v(ra), luai_numdiv(L, nb, nc)); } else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_DIV)); } vmbreak; @@ -1065,7 +1068,7 @@ void luaV_execute (lua_State *L) { TValue *rc = RKC(i); lua_Integer ib; lua_Integer ic; if (tointeger(rb, &ib) && tointeger(rc, &ic)) { - setivalue(ra, intop(&, ib, ic)); + setivalue(s2v(ra), intop(&, ib, ic)); } else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_BAND)); } vmbreak; @@ -1075,7 +1078,7 @@ void luaV_execute (lua_State *L) { TValue *rc = RKC(i); lua_Integer ib; lua_Integer ic; if (tointeger(rb, &ib) && tointeger(rc, &ic)) { - setivalue(ra, intop(|, ib, ic)); + setivalue(s2v(ra), intop(|, ib, ic)); } else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_BOR)); } vmbreak; @@ -1085,7 +1088,7 @@ void luaV_execute (lua_State *L) { TValue *rc = RKC(i); lua_Integer ib; lua_Integer ic; if (tointeger(rb, &ib) && tointeger(rc, &ic)) { - setivalue(ra, intop(^, ib, ic)); + setivalue(s2v(ra), intop(^, ib, ic)); } else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_BXOR)); } vmbreak; @@ -1095,7 +1098,7 @@ void luaV_execute (lua_State *L) { TValue *rc = RKC(i); lua_Integer ib; lua_Integer ic; if (tointeger(rb, &ib) && tointeger(rc, &ic)) { - setivalue(ra, luaV_shiftl(ib, ic)); + setivalue(s2v(ra), luaV_shiftl(ib, ic)); } else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_SHL)); } vmbreak; @@ -1105,7 +1108,7 @@ void luaV_execute (lua_State *L) { TValue *rc = RKC(i); lua_Integer ib; lua_Integer ic; if (tointeger(rb, &ib) && tointeger(rc, &ic)) { - setivalue(ra, luaV_shiftl(ib, -ic)); + setivalue(s2v(ra), luaV_shiftl(ib, -ic)); } else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_SHR)); } vmbreak; @@ -1116,12 +1119,12 @@ void luaV_execute (lua_State *L) { lua_Number nb; lua_Number nc; if (ttisinteger(rb) && ttisinteger(rc)) { lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc); - setivalue(ra, luaV_mod(L, ib, ic)); + setivalue(s2v(ra), luaV_mod(L, ib, ic)); } else if (tonumber(rb, &nb) && tonumber(rc, &nc)) { lua_Number m; luai_nummod(L, nb, nc, m); - setfltvalue(ra, m); + setfltvalue(s2v(ra), m); } else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_MOD)); } vmbreak; @@ -1132,10 +1135,10 @@ void luaV_execute (lua_State *L) { lua_Number nb; lua_Number nc; if (ttisinteger(rb) && ttisinteger(rc)) { lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc); - setivalue(ra, luaV_div(L, ib, ic)); + setivalue(s2v(ra), luaV_div(L, ib, ic)); } else if (tonumber(rb, &nb) && tonumber(rc, &nc)) { - setfltvalue(ra, luai_numidiv(L, nb, nc)); + setfltvalue(s2v(ra), luai_numidiv(L, nb, nc)); } else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_IDIV)); } vmbreak; @@ -1145,20 +1148,20 @@ void luaV_execute (lua_State *L) { TValue *rc = RKC(i); lua_Number nb; lua_Number nc; if (tonumber(rb, &nb) && tonumber(rc, &nc)) { - setfltvalue(ra, luai_numpow(L, nb, nc)); + setfltvalue(s2v(ra), luai_numpow(L, nb, nc)); } else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_POW)); } vmbreak; } vmcase(OP_UNM) { - TValue *rb = RB(i); + TValue *rb = vRB(i); lua_Number nb; if (ttisinteger(rb)) { lua_Integer ib = ivalue(rb); - setivalue(ra, intop(-, 0, ib)); + setivalue(s2v(ra), intop(-, 0, ib)); } else if (tonumber(rb, &nb)) { - setfltvalue(ra, luai_numunm(L, nb)); + setfltvalue(s2v(ra), luai_numunm(L, nb)); } else { Protect(luaT_trybinTM(L, rb, rb, ra, TM_UNM)); @@ -1166,10 +1169,10 @@ void luaV_execute (lua_State *L) { vmbreak; } vmcase(OP_BNOT) { - TValue *rb = RB(i); + TValue *rb = vRB(i); lua_Integer ib; if (tointeger(rb, &ib)) { - setivalue(ra, intop(^, ~l_castS2U(0), ib)); + setivalue(s2v(ra), intop(^, ~l_castS2U(0), ib)); } else { Protect(luaT_trybinTM(L, rb, rb, ra, TM_BNOT)); @@ -1177,13 +1180,13 @@ void luaV_execute (lua_State *L) { vmbreak; } vmcase(OP_NOT) { - TValue *rb = RB(i); + TValue *rb = vRB(i); int res = l_isfalse(rb); /* next assignment may change this value */ - setbvalue(ra, res); + setbvalue(s2v(ra), res); vmbreak; } vmcase(OP_LEN) { - Protect(luaV_objlen(L, ra, RB(i))); + Protect(luaV_objlen(L, ra, vRB(i))); vmbreak; } vmcase(OP_CONCAT) { @@ -1245,18 +1248,18 @@ void luaV_execute (lua_State *L) { vmbreak; } vmcase(OP_TEST) { - if (GETARG_C(i) ? l_isfalse(ra) : !l_isfalse(ra)) + if (GETARG_C(i) ? l_isfalse(s2v(ra)) : !l_isfalse(s2v(ra))) pc++; else donextjump(ci); vmbreak; } vmcase(OP_TESTSET) { - TValue *rb = RB(i); + TValue *rb = vRB(i); if (GETARG_C(i) ? l_isfalse(rb) : !l_isfalse(rb)) pc++; else { - setobjs2s(L, ra, rb); + setobj2s(L, ra, rb); donextjump(ci); } vmbreak; @@ -1295,7 +1298,7 @@ void luaV_execute (lua_State *L) { StkId nfunc = nci->func; /* called function */ StkId ofunc = oci->func; /* caller function */ /* last stack slot filled by 'precall' */ - StkId lim = nci->func + 1 + getproto(nfunc)->numparams; + StkId lim = nci->func + 1 + getproto(s2v(nfunc))->numparams; int aux; /* close all upvalues from previous call */ if (cl->p->sizep > 0) luaF_close(L, oci->func + 1); @@ -1306,7 +1309,8 @@ void luaV_execute (lua_State *L) { oci->u.l.savedpc = nci->u.l.savedpc; oci->callstatus |= CIST_TAIL; /* function was tail called */ ci = L->ci = oci; /* remove new frame */ - lua_assert(L->top == oci->func + 1 + getproto(ofunc)->maxstacksize); + lua_assert(L->top == + oci->func + 1 + getproto(s2v(ofunc))->maxstacksize); goto newframe; /* restart luaV_execute over new Lua function */ } vmbreak; @@ -1327,34 +1331,35 @@ void luaV_execute (lua_State *L) { } } vmcase(OP_FORLOOP) { - if (ttisinteger(ra)) { /* integer loop? */ - lua_Integer step = ivalue(ra + 2); - lua_Integer idx = intop(+, ivalue(ra), step); /* increment index */ - lua_Integer limit = ivalue(ra + 1); + if (ttisinteger(s2v(ra))) { /* integer loop? */ + lua_Integer step = ivalue(s2v(ra + 2)); + lua_Integer idx = intop(+, ivalue(s2v(ra)), step); /* increment index */ + lua_Integer limit = ivalue(s2v(ra + 1)); if ((0 < step) ? (idx <= limit) : (limit <= idx)) { pc += GETARG_sBx(i); /* jump back */ - chgivalue(ra, idx); /* update internal index... */ - setivalue(ra + 3, idx); /* ...and external index */ + chgivalue(s2v(ra), idx); /* update internal index... */ + setivalue(s2v(ra + 3), idx); /* ...and external index */ } } else { /* floating loop */ - lua_Number step = fltvalue(ra + 2); - lua_Number idx = luai_numadd(L, fltvalue(ra), step); /* inc. index */ - lua_Number limit = fltvalue(ra + 1); + lua_Number step = fltvalue(s2v(ra + 2)); + lua_Number limit = fltvalue(s2v(ra + 1)); + lua_Number idx = fltvalue(s2v(ra)); + idx = luai_numadd(L, idx, step); /* inc. index */ if (luai_numlt(0, step) ? luai_numle(idx, limit) : luai_numle(limit, idx)) { pc += GETARG_sBx(i); /* jump back */ - chgfltvalue(ra, idx); /* update internal index... */ - setfltvalue(ra + 3, idx); /* ...and external index */ + chgfltvalue(s2v(ra), idx); /* update internal index... */ + setfltvalue(s2v(ra + 3), idx); /* ...and external index */ } } updatemask(L); vmbreak; } vmcase(OP_FORPREP) { - TValue *init = ra; - TValue *plimit = ra + 1; - TValue *pstep = ra + 2; + TValue *init = s2v(ra); + TValue *plimit = s2v(ra + 1); + TValue *pstep = s2v(ra + 2); lua_Integer ilimit; int stopnow; if (ttisinteger(init) && ttisinteger(pstep) && @@ -1395,7 +1400,7 @@ void luaV_execute (lua_State *L) { } vmcase(OP_TFORLOOP) { l_tforloop: - if (!ttisnil(ra + 1)) { /* continue loop? */ + if (!ttisnil(s2v(ra + 1))) { /* continue loop? */ setobjs2s(L, ra, ra + 1); /* save control variable */ pc += GETARG_sBx(i); /* jump back */ } @@ -1411,13 +1416,13 @@ void luaV_execute (lua_State *L) { lua_assert(GET_OPCODE(*pc) == OP_EXTRAARG); c = GETARG_Ax(*pc++); } - h = hvalue(ra); + h = hvalue(s2v(ra)); last = ((c-1)*LFIELDS_PER_FLUSH) + n; savepc(L); /* in case of allocation errors */ if (last > h->sizearray) /* needs more space? */ luaH_resizearray(L, h, last); /* preallocate it at once */ for (; n > 0; n--) { - TValue *val = ra + n; + TValue *val = s2v(ra + n); setobj2t(L, &h->array[last - 1], val); last--; luaC_barrierback(L, h, val); @@ -1433,13 +1438,13 @@ void luaV_execute (lua_State *L) { pushclosure(L, p, cl->upvals, base, ra); /* create a new one */ } else - setclLvalue(L, ra, ncl); /* push cashed closure */ + setclLvalue2s(L, ra, ncl); /* push cashed closure */ checkGC(L, ra + 1); vmbreak; } vmcase(OP_VARARG) { int b = GETARG_B(i) - 1; /* required results */ - StkId vtab = base + cl->p->numparams - 1; /* vararg table */ + TValue *vtab = s2v(base + cl->p->numparams - 1); /* vararg table */ Protect(luaT_getvarargs(L, vtab, ra, b)); vmbreak; } diff --git a/lvm.h b/lvm.h index 3cf1f869db..7ac959afe3 100644 --- a/lvm.h +++ b/lvm.h @@ -1,5 +1,5 @@ /* -** $Id: lvm.h,v 2.43 2017/06/01 20:23:27 roberto Exp roberto $ +** $Id: lvm.h,v 2.44 2017/06/09 19:16:41 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -93,7 +93,7 @@ LUAI_FUNC int luaV_tointeger (const TValue *obj, lua_Integer *p, int mode); LUAI_FUNC void luaV_finishget (lua_State *L, const TValue *t, TValue *key, StkId val, const TValue *slot); LUAI_FUNC void luaV_finishset (lua_State *L, const TValue *t, TValue *key, - StkId val, const TValue *slot); + TValue *val, const TValue *slot); LUAI_FUNC void luaV_finishOp (lua_State *L); LUAI_FUNC void luaV_execute (lua_State *L); LUAI_FUNC void luaV_concat (lua_State *L, int total); From 07db10813cb044252c18973688f63b5eac6a90a6 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 29 Jun 2017 12:38:41 -0300 Subject: [PATCH 0069/1145] 'OP_VARARG' has the vararg parameter as an operand --- lopcodes.c | 4 ++-- lopcodes.h | 6 +++--- lparser.c | 5 +++-- lvm.c | 4 ++-- 4 files changed, 10 insertions(+), 9 deletions(-) diff --git a/lopcodes.c b/lopcodes.c index 1ffc52c920..163eff608e 100644 --- a/lopcodes.c +++ b/lopcodes.c @@ -1,5 +1,5 @@ /* -** $Id: lopcodes.c,v 1.57 2017/04/26 17:46:52 roberto Exp roberto $ +** $Id: lopcodes.c,v 1.58 2017/04/28 20:57:45 roberto Exp roberto $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ @@ -130,7 +130,7 @@ LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = { ,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_TFORLOOP */ ,opmode(0, 0, OpArgU, OpArgU, iABC) /* OP_SETLIST */ ,opmode(0, 1, OpArgU, OpArgN, iABx) /* OP_CLOSURE */ - ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_VARARG */ + ,opmode(0, 1, OpArgU, OpArgR, iABC) /* OP_VARARG */ ,opmode(0, 0, OpArgU, OpArgU, iAx) /* OP_EXTRAARG */ }; diff --git a/lopcodes.h b/lopcodes.h index 683f8928b1..fb989ac606 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -1,5 +1,5 @@ /* -** $Id: lopcodes.h,v 1.153 2017/04/28 20:57:45 roberto Exp roberto $ +** $Id: lopcodes.h,v 1.154 2017/05/08 16:08:01 roberto Exp roberto $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ @@ -240,7 +240,7 @@ OP_SETLIST,/* A B C R(A)[(C-1)*FPF+i] := R(A+i), 1 <= i <= B */ OP_CLOSURE,/* A Bx R(A) := closure(KPROTO[Bx]) */ -OP_VARARG,/* A B R(A), R(A+1), ..., R(A+B-2) = vararg */ +OP_VARARG,/* A B C R(A), R(A+1), ..., R(A+B-2) = vararg(C) */ OP_EXTRAARG/* Ax extra (larger) argument for previous opcode */ } OpCode; @@ -257,7 +257,7 @@ OP_EXTRAARG/* Ax extra (larger) argument for previous opcode */ OP_SETLIST) may use 'top'. (*) In OP_VARARG, if (B == 0) then use actual number of varargs and - set top (like in OP_CALL with C == 0). + set top (like in OP_CALL with C == 0). C is the vararg parameter. (*) In OP_RETURN, if (B == 0) then return up to 'top'. diff --git a/lparser.c b/lparser.c index a8c77c44d7..1a0a4e5190 100644 --- a/lparser.c +++ b/lparser.c @@ -1,5 +1,5 @@ /* -** $Id: lparser.c,v 2.160 2017/06/27 11:35:31 roberto Exp roberto $ +** $Id: lparser.c,v 2.161 2017/06/29 15:06:44 roberto Exp roberto $ ** Lua Parser ** See Copyright Notice in lua.h */ @@ -970,9 +970,10 @@ static void simpleexp (LexState *ls, expdesc *v) { } case TK_DOTS: { /* vararg */ FuncState *fs = ls->fs; + int lastparam = fs->f->numparams - 1; check_condition(ls, fs->f->is_vararg, "cannot use '...' outside a vararg function"); - init_exp(v, VVARARG, luaK_codeABC(fs, OP_VARARG, 0, 1, 0)); + init_exp(v, VVARARG, luaK_codeABC(fs, OP_VARARG, 0, 1, lastparam)); break; } case '{': { /* constructor */ diff --git a/lvm.c b/lvm.c index 05669d7b34..8f8cc49f9e 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.287 2017/06/09 19:16:41 roberto Exp roberto $ +** $Id: lvm.c,v 2.288 2017/06/29 15:06:44 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -1444,7 +1444,7 @@ void luaV_execute (lua_State *L) { } vmcase(OP_VARARG) { int b = GETARG_B(i) - 1; /* required results */ - TValue *vtab = s2v(base + cl->p->numparams - 1); /* vararg table */ + TValue *vtab = vRC(i); /* vararg table */ Protect(luaT_getvarargs(L, vtab, ra, b)); vmbreak; } From 4dff277255e3785b76b6b52fe2a98592e6cbc834 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 7 Jul 2017 13:34:32 -0300 Subject: [PATCH 0070/1145] coercion string->number in arithmetic operations moved to string library --- ldebug.c | 5 ++-- lobject.c | 4 +-- lstrlib.c | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- lvm.c | 21 +++++++------ lvm.h | 12 +++++++- 5 files changed, 111 insertions(+), 19 deletions(-) diff --git a/ldebug.c b/ldebug.c index ac0b7d547e..b83b2f89fb 100644 --- a/ldebug.c +++ b/ldebug.c @@ -1,5 +1,5 @@ /* -** $Id: ldebug.c,v 2.127 2017/06/27 11:35:31 roberto Exp roberto $ +** $Id: ldebug.c,v 2.128 2017/06/29 15:06:44 roberto Exp roberto $ ** Debug Interface ** See Copyright Notice in lua.h */ @@ -679,8 +679,7 @@ l_noret luaG_concaterror (lua_State *L, const TValue *p1, const TValue *p2) { l_noret luaG_opinterror (lua_State *L, const TValue *p1, const TValue *p2, const char *msg) { - lua_Number temp; - if (!tonumber(p1, &temp)) /* first operand is wrong? */ + if (!ttisnumber(p1)) /* first operand is wrong? */ p2 = p1; /* now second is wrong */ luaG_typeerror(L, p2, msg); } diff --git a/lobject.c b/lobject.c index 9ad833c994..21616fade9 100644 --- a/lobject.c +++ b/lobject.c @@ -1,5 +1,5 @@ /* -** $Id: lobject.c,v 2.115 2017/05/24 13:47:11 roberto Exp roberto $ +** $Id: lobject.c,v 2.116 2017/06/29 15:06:44 roberto Exp roberto $ ** Some generic functions over Lua objects ** See Copyright Notice in lua.h */ @@ -147,7 +147,7 @@ int luaO_rawarith (lua_State *L, int op, const TValue *p1, const TValue *p2, setivalue(res, intarith(L, op, ivalue(p1), ivalue(p2))); return 1; } - else if (tonumber(p1, &n1) && tonumber(p2, &n2)) { + else if (tonumberns(p1, n1) && tonumberns(p2, n2)) { setfltvalue(res, numarith(L, op, n1, n2)); return 1; } diff --git a/lstrlib.c b/lstrlib.c index 89896fa6fc..c7cfa4214a 100644 --- a/lstrlib.c +++ b/lstrlib.c @@ -1,5 +1,5 @@ /* -** $Id: lstrlib.c,v 1.255 2017/03/14 12:40:44 roberto Exp roberto $ +** $Id: lstrlib.c,v 1.256 2017/05/19 16:29:40 roberto Exp roberto $ ** Standard library for string operations and pattern-matching ** See Copyright Notice in lua.h */ @@ -201,6 +201,88 @@ static int str_dump (lua_State *L) { +/* +** {====================================================== +** METAMETHODS +** ======================================================= +*/ + +static int tonum (lua_State *L, int arg) { + if (lua_type(L, arg) == LUA_TNUMBER) { /* already a number? */ + lua_pushvalue(L, arg); + return 1; + } + else { /* check whether it is a numerical string */ + size_t len; + const char *s = lua_tolstring(L, arg, &len); + return (s != NULL && lua_stringtonumber(L, s) == len + 1); + } +} + + +static int arith (lua_State *L, int op, const char *mtname) { + if (tonum(L, 1) && tonum(L, 2)) + lua_arith(L, op); /* result will be on the top */ + else { + lua_settop(L, 2); /* back to the original arguments */ + if (lua_type(L, 2) == LUA_TSTRING || !luaL_getmetafield(L, 2, mtname)) + return luaL_error(L, "attempt to %s a '%s' with a '%s'", mtname + 2, + luaL_typename(L, -2), luaL_typename(L, -1)); + lua_insert(L, -3); /* put metamethod before arguments */ + lua_call(L, 2, 1); /* call metamethod */ + } + return 1; +} + + +static int arith_add (lua_State *L) { + return arith(L, LUA_OPADD, "__add"); +} + +static int arith_sub (lua_State *L) { + return arith(L, LUA_OPSUB, "__sub"); +} + +static int arith_mul (lua_State *L) { + return arith(L, LUA_OPMUL, "__mul"); +} + +static int arith_mod (lua_State *L) { + return arith(L, LUA_OPMOD, "__mod"); +} + +static int arith_pow (lua_State *L) { + return arith(L, LUA_OPPOW, "__pow"); +} + +static int arith_div (lua_State *L) { + return arith(L, LUA_OPDIV, "__div"); +} + +static int arith_idiv (lua_State *L) { + return arith(L, LUA_OPIDIV, "__idiv"); +} + +static int arith_unm (lua_State *L) { + return arith(L, LUA_OPUNM, "__unm"); +} + + +static const luaL_Reg stringmetamethods[] = { + {"__add", arith_add}, + {"__sub", arith_sub}, + {"__mul", arith_mul}, + {"__mod", arith_mod}, + {"__pow", arith_pow}, + {"__div", arith_div}, + {"__idiv", arith_idiv}, + {"__unm", arith_unm}, + {"__index", NULL}, /* placeholder */ + {NULL, NULL} +}; + +/* }====================================================== */ + /* ** {====================================================== ** PATTERN MATCHING @@ -1576,7 +1658,9 @@ static const luaL_Reg strlib[] = { static void createmetatable (lua_State *L) { - lua_createtable(L, 0, 1); /* table to be metatable for strings */ + /* table to be metatable for strings */ + luaL_newlibtable(L, stringmetamethods); + luaL_setfuncs(L, stringmetamethods, 0); lua_pushliteral(L, ""); /* dummy string */ lua_pushvalue(L, -2); /* copy table */ lua_setmetatable(L, -2); /* set table as metatable for strings */ diff --git a/lvm.c b/lvm.c index 8f8cc49f9e..18799be7ce 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.288 2017/06/29 15:06:44 roberto Exp roberto $ +** $Id: lvm.c,v 2.289 2017/06/29 15:38:41 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -35,7 +35,6 @@ #define MAXTAGLOOP 2000 - /* ** 'l_intfitsf' checks whether a given integer can be converted to a ** float without rounding. Used in comparisons. Left undefined if @@ -997,7 +996,7 @@ void luaV_execute (lua_State *L) { if (ttisinteger(rb)) { setivalue(s2v(ra), intop(+, ivalue(rb), ic)); } - else if (tonumber(rb, &nb)) { + else if (tonumberns(rb, nb)) { setfltvalue(s2v(ra), luai_numadd(L, nb, cast_num(ic))); } else { @@ -1019,7 +1018,7 @@ void luaV_execute (lua_State *L) { lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc); setivalue(s2v(ra), intop(+, ib, ic)); } - else if (tonumber(rb, &nb) && tonumber(rc, &nc)) { + else if (tonumberns(rb, nb) && tonumberns(rc, nc)) { setfltvalue(s2v(ra), luai_numadd(L, nb, nc)); } else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_ADD)); } @@ -1033,7 +1032,7 @@ void luaV_execute (lua_State *L) { lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc); setivalue(s2v(ra), intop(-, ib, ic)); } - else if (tonumber(rb, &nb) && tonumber(rc, &nc)) { + else if (tonumberns(rb, nb) && tonumberns(rc, nc)) { setfltvalue(s2v(ra), luai_numsub(L, nb, nc)); } else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_SUB)); } @@ -1047,7 +1046,7 @@ void luaV_execute (lua_State *L) { lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc); setivalue(s2v(ra), intop(*, ib, ic)); } - else if (tonumber(rb, &nb) && tonumber(rc, &nc)) { + else if (tonumberns(rb, nb) && tonumberns(rc, nc)) { setfltvalue(s2v(ra), luai_nummul(L, nb, nc)); } else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_MUL)); } @@ -1057,7 +1056,7 @@ void luaV_execute (lua_State *L) { TValue *rb = RKB(i); TValue *rc = RKC(i); lua_Number nb; lua_Number nc; - if (tonumber(rb, &nb) && tonumber(rc, &nc)) { + if (tonumberns(rb, nb) && tonumberns(rc, nc)) { setfltvalue(s2v(ra), luai_numdiv(L, nb, nc)); } else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_DIV)); } @@ -1121,7 +1120,7 @@ void luaV_execute (lua_State *L) { lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc); setivalue(s2v(ra), luaV_mod(L, ib, ic)); } - else if (tonumber(rb, &nb) && tonumber(rc, &nc)) { + else if (tonumberns(rb, nb) && tonumberns(rc, nc)) { lua_Number m; luai_nummod(L, nb, nc, m); setfltvalue(s2v(ra), m); @@ -1137,7 +1136,7 @@ void luaV_execute (lua_State *L) { lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc); setivalue(s2v(ra), luaV_div(L, ib, ic)); } - else if (tonumber(rb, &nb) && tonumber(rc, &nc)) { + else if (tonumberns(rb, nb) && tonumberns(rc, nc)) { setfltvalue(s2v(ra), luai_numidiv(L, nb, nc)); } else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_IDIV)); } @@ -1147,7 +1146,7 @@ void luaV_execute (lua_State *L) { TValue *rb = RKB(i); TValue *rc = RKC(i); lua_Number nb; lua_Number nc; - if (tonumber(rb, &nb) && tonumber(rc, &nc)) { + if (tonumberns(rb, nb) && tonumberns(rc, nc)) { setfltvalue(s2v(ra), luai_numpow(L, nb, nc)); } else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_POW)); } @@ -1160,7 +1159,7 @@ void luaV_execute (lua_State *L) { lua_Integer ib = ivalue(rb); setivalue(s2v(ra), intop(-, 0, ib)); } - else if (tonumber(rb, &nb)) { + else if (tonumberns(rb, nb)) { setfltvalue(s2v(ra), luai_numunm(L, nb)); } else { diff --git a/lvm.h b/lvm.h index 7ac959afe3..6bb5818d73 100644 --- a/lvm.h +++ b/lvm.h @@ -1,5 +1,5 @@ /* -** $Id: lvm.h,v 2.44 2017/06/09 19:16:41 roberto Exp roberto $ +** $Id: lvm.h,v 2.45 2017/06/29 15:06:44 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -37,12 +37,22 @@ #endif +/* convert an object to a float (including string coercion) */ #define tonumber(o,n) \ (ttisfloat(o) ? (*(n) = fltvalue(o), 1) : luaV_tonumber_(o,n)) + +/* convert an object to a float (without string coercion) */ +#define tonumberns(o,n) \ + (ttisfloat(o) ? ((n) = fltvalue(o), 1) : \ + (ttisinteger(o) ? ((n) = cast_num(ivalue(o)), 1) : 0)) + + +/* convert an object to an integer (including string coercion) */ #define tointeger(o,i) \ (ttisinteger(o) ? (*(i) = ivalue(o), 1) : luaV_tointeger(o,i,LUA_FLOORN2I)) + #define intop(op,v1,v2) l_castU2S(l_castS2U(v1) op l_castS2U(v2)) #define luaV_rawequalobj(t1,t2) luaV_equalobj(NULL,t1,t2) From b1daa069bac27055f17753e85dca6aa256efef6f Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 10 Jul 2017 14:35:12 -0300 Subject: [PATCH 0071/1145] bug: Lua does not check GC when creating error messages --- ldebug.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ldebug.c b/ldebug.c index b83b2f89fb..fbb3839d19 100644 --- a/ldebug.c +++ b/ldebug.c @@ -1,5 +1,5 @@ /* -** $Id: ldebug.c,v 2.128 2017/06/29 15:06:44 roberto Exp roberto $ +** $Id: ldebug.c,v 2.129 2017/07/07 16:34:32 roberto Exp roberto $ ** Debug Interface ** See Copyright Notice in lua.h */ @@ -735,6 +735,7 @@ l_noret luaG_runerror (lua_State *L, const char *fmt, ...) { CallInfo *ci = L->ci; const char *msg; va_list argp; + luaC_checkGC(L); /* error message uses memory */ va_start(argp, fmt); msg = luaO_pushvfstring(L, fmt, argp); /* format message */ va_end(argp); From 11769b203f052f7423575c1a66901cb9eb277206 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 27 Jul 2017 10:36:54 -0300 Subject: [PATCH 0072/1145] new version (5.4) --- lua.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lua.h b/lua.h index 1499c04e9c..eb76a27f9f 100644 --- a/lua.h +++ b/lua.h @@ -1,5 +1,5 @@ /* -** $Id: lua.h,v 1.334 2017/05/18 12:34:58 roberto Exp roberto $ +** $Id: lua.h,v 1.335 2017/05/26 19:14:29 roberto Exp roberto $ ** Lua - A Scripting Language ** Lua.org, PUC-Rio, Brazil (http://www.lua.org) ** See Copyright Notice at the end of this file @@ -17,9 +17,9 @@ #define LUA_VERSION_MAJOR "5" -#define LUA_VERSION_MINOR "3" -#define LUA_VERSION_NUM 503 -#define LUA_VERSION_RELEASE "4" +#define LUA_VERSION_MINOR "4" +#define LUA_VERSION_NUM 504 +#define LUA_VERSION_RELEASE "0" #define LUA_VERSION "Lua " LUA_VERSION_MAJOR "." LUA_VERSION_MINOR #define LUA_RELEASE LUA_VERSION "." LUA_VERSION_RELEASE From 6d998055c80a0fe8d4698ba02becb1813203eee9 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 27 Jul 2017 10:50:16 -0300 Subject: [PATCH 0073/1145] no more reference 'memerrmsg' + new reference to "n" (both can be retrieved by 'luaS_newliteral' without creating anything, because they are fixed, but "n" deserves fast access while 'memerrmsg' does not) --- ldo.c | 5 +++-- lstate.h | 4 ++-- lstring.c | 16 ++++++++-------- lstring.h | 9 ++++++++- ltm.c | 6 +++--- 5 files changed, 24 insertions(+), 16 deletions(-) diff --git a/ldo.c b/ldo.c index 5917f78f22..0c0bb0bc9e 100644 --- a/ldo.c +++ b/ldo.c @@ -1,5 +1,5 @@ /* -** $Id: ldo.c,v 2.160 2017/05/23 12:50:11 roberto Exp roberto $ +** $Id: ldo.c,v 2.161 2017/06/29 15:06:44 roberto Exp roberto $ ** Stack and Call structure of Lua ** See Copyright Notice in lua.h */ @@ -91,7 +91,8 @@ struct lua_longjmp { static void seterrorobj (lua_State *L, int errcode, StkId oldtop) { switch (errcode) { case LUA_ERRMEM: { /* memory error? */ - setsvalue2s(L, oldtop, G(L)->memerrmsg); /* reuse preregistered msg. */ + TString *memerrmsg = luaS_newliteral(L, MEMERRMSG); + setsvalue2s(L, oldtop, memerrmsg); /* reuse preregistered msg. */ break; } case LUA_ERRERR: { diff --git a/lstate.h b/lstate.h index 12dddf3b36..5ccc402507 100644 --- a/lstate.h +++ b/lstate.h @@ -1,5 +1,5 @@ /* -** $Id: lstate.h,v 2.142 2017/05/26 19:14:29 roberto Exp roberto $ +** $Id: lstate.h,v 2.143 2017/06/12 14:21:44 roberto Exp roberto $ ** Global State ** See Copyright Notice in lua.h */ @@ -175,7 +175,7 @@ typedef struct global_State { lua_CFunction panic; /* to be called in unprotected errors */ struct lua_State *mainthread; const lua_Number *version; /* pointer to version number */ - TString *memerrmsg; /* memory-error message */ + TString *nfield; /* string "n" (key in vararg tables) */ TString *tmname[TM_N]; /* array with tag-method names */ struct Table *mt[LUA_NUMTAGS]; /* metatables for basic types */ TString *strcache[STRCACHE_N][STRCACHE_M]; /* cache for strings in API */ diff --git a/lstring.c b/lstring.c index fc9eb220e7..a851fd7415 100644 --- a/lstring.c +++ b/lstring.c @@ -1,5 +1,5 @@ /* -** $Id: lstring.c,v 2.55 2015/11/03 15:36:01 roberto Exp roberto $ +** $Id: lstring.c,v 2.56 2015/11/23 11:32:51 roberto Exp roberto $ ** String table (keeps all strings handled by Lua) ** See Copyright Notice in lua.h */ @@ -22,9 +22,6 @@ #include "lstring.h" -#define MEMERRMSG "not enough memory" - - /* ** Lua will use at most ~(2^LUAI_HASHLIMIT) bytes from a string to ** compute its hash @@ -105,7 +102,7 @@ void luaS_clearcache (global_State *g) { for (i = 0; i < STRCACHE_N; i++) for (j = 0; j < STRCACHE_M; j++) { if (iswhite(g->strcache[i][j])) /* will entry be collected? */ - g->strcache[i][j] = g->memerrmsg; /* replace it with something fixed */ + g->strcache[i][j] = g->nfield; /* replace it with something fixed */ } } @@ -116,13 +113,16 @@ void luaS_clearcache (global_State *g) { void luaS_init (lua_State *L) { global_State *g = G(L); int i, j; + TString *memerrmsg; luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */ /* pre-create memory-error message */ - g->memerrmsg = luaS_newliteral(L, MEMERRMSG); - luaC_fix(L, obj2gco(g->memerrmsg)); /* it should never be collected */ + memerrmsg = luaS_newliteral(L, MEMERRMSG); + luaC_fix(L, obj2gco(memerrmsg)); /* it should never be collected */ + g->nfield = luaS_newliteral(L, "n"); /* pre-create "n" field name */ + luaC_fix(L, obj2gco(g->nfield)); /* it also should never be collected */ for (i = 0; i < STRCACHE_N; i++) /* fill cache with valid strings */ for (j = 0; j < STRCACHE_M; j++) - g->strcache[i][j] = g->memerrmsg; + g->strcache[i][j] = g->nfield; } diff --git a/lstring.h b/lstring.h index 6351003af8..416d951928 100644 --- a/lstring.h +++ b/lstring.h @@ -1,5 +1,5 @@ /* -** $Id: lstring.h,v 1.60 2015/09/08 15:41:05 roberto Exp roberto $ +** $Id: lstring.h,v 1.61 2015/11/03 15:36:01 roberto Exp roberto $ ** String table (keep all strings handled by Lua) ** See Copyright Notice in lua.h */ @@ -12,6 +12,13 @@ #include "lstate.h" +/* +** Memory-allocation error message must be preallocated (it cannot +** be created after memory is exausted) +*/ +#define MEMERRMSG "not enough memory" + + #define sizelstring(l) (sizeof(union UTString) + ((l) + 1) * sizeof(char)) #define sizeludata(l) (sizeof(union UUdata) + (l)) diff --git a/ltm.c b/ltm.c index 3629dc69f0..cbc4cc5e93 100644 --- a/ltm.c +++ b/ltm.c @@ -1,5 +1,5 @@ /* -** $Id: ltm.c,v 2.41 2017/05/13 12:57:20 roberto Exp roberto $ +** $Id: ltm.c,v 2.42 2017/06/29 15:06:44 roberto Exp roberto $ ** Tag methods ** See Copyright Notice in lua.h */ @@ -188,7 +188,7 @@ void luaT_adjustvarargs (lua_State *L, Proto *p, int actual) { luaH_resize(L, vtab, actual, 1); for (i = 0; i < actual; i++) /* put extra arguments into vararg table */ setobj2n(L, &vtab->array[i], s2v(L->top - actual + i - 1)); - setsvalue(L, &nname, luaS_newliteral(L, "n")); /* get field 'n' */ + setsvalue(L, &nname, G(L)->nfield); /* get field 'n' */ setivalue(luaH_set(L, vtab, &nname), actual); /* store counter there */ L->top -= actual; /* remove extra elements from the stack */ sethvalue2s(L, L->top - 1, vtab); /* move table to new top */ @@ -202,7 +202,7 @@ void luaT_getvarargs (lua_State *L, TValue *t, StkId where, int wanted) { int i; Table *h = hvalue(t); if (wanted < 0) { /* get all? */ - const TValue *ns = luaH_getstr(h, luaS_newliteral(L, "n")); + const TValue *ns = luaH_getstr(h, G(L)->nfield); int n = (ttisinteger(ns)) ? ivalue(ns) : 0; wanted = n; checkstackp(L, n, where); From 4053eae9ebbf14963a388ba864454f9e4ec16663 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 27 Jul 2017 10:55:38 -0300 Subject: [PATCH 0074/1145] bug: Lua does not check GC when creating error messages --- bugs | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 54 insertions(+), 9 deletions(-) diff --git a/bugs b/bugs index 24b16bd758..a071937e74 100644 --- a/bugs +++ b/bugs @@ -2899,6 +2899,30 @@ patch = [[ ]] } +Bug{ +what = [[Lua does not check memory use when creating error messages]], +report = [[John Dunn, 2012/09/24]], +since = [[5.2.0]], +fix = nil, +example = [[ +local code = "function test()\n bob.joe.larry = 23\n end" + +load(code)() + +-- memory will grow steadly +for i = 1, math.huge do + pcall(test) + if i % 100000 == 0 then + io.write(collectgarbage'count'*1024, "\n") + end +end +]], +patch = [[ +]] +} + + + ----------------------------------------------------------------- @@ -3656,9 +3680,9 @@ It needs an "interceptor" 'memcmp' function that continues reading memory after a difference is found.]], patch = [[ 2c2 -< ** $Id: bugs,v 1.153 2017/05/19 12:58:40 roberto Exp roberto $ +< ** $Id: bugs,v 1.154 2017/05/22 12:55:16 roberto Exp roberto $ --- -> ** $Id: bugs,v 1.153 2017/05/19 12:58:40 roberto Exp roberto $ +> ** $Id: bugs,v 1.154 2017/05/22 12:55:16 roberto Exp roberto $ 263c263,264 < for (option = LUA_STRFTIMEOPTIONS; *option != '\0'; option += oplen) { --- @@ -3780,18 +3804,39 @@ patch = [[ ]] } - ---[=[ Bug{ -what = [[ ]], -report = [[ ]], -since = [[ ]], +what = [[Lua does not check GC when creating error messages]], +report = [[Viacheslav Usov, 2017/07/06]], +since = [[5.3.2]], fix = nil, -example = [[ ]], +example = [[ +function test() + bob.joe.larry = 23 +end + +-- memory will grow steadly +for i = 1, math.huge do + pcall(test) + if i % 100000 == 0 then + io.write(collectgarbage'count'*1024, "\n") + end +end +]], patch = [[ +--- ldebug.c 2017/04/19 17:20:42 2.121.1.1 ++++ ldebug.c 2017/07/10 17:08:39 +@@ -653,6 +653,7 @@ + CallInfo *ci = L->ci; + const char *msg; + va_list argp; ++ luaC_checkGC(L); /* error message uses memory */ + va_start(argp, fmt); + msg = luaO_pushvfstring(L, fmt, argp); /* format message */ + va_end(argp); ]] } -]=] + + --[=[ From b77f792b23fa4fe12918659d61961b2a50d151d0 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Sat, 12 Aug 2017 10:12:21 -0300 Subject: [PATCH 0075/1145] comment --- lparser.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lparser.c b/lparser.c index 1a0a4e5190..3df5a95845 100644 --- a/lparser.c +++ b/lparser.c @@ -1,5 +1,5 @@ /* -** $Id: lparser.c,v 2.161 2017/06/29 15:06:44 roberto Exp roberto $ +** $Id: lparser.c,v 2.162 2017/06/29 15:38:41 roberto Exp roberto $ ** Lua Parser ** See Copyright Notice in lua.h */ @@ -1411,7 +1411,7 @@ static void test_then_block (LexState *ls, int *escapelist) { luaK_goiffalse(ls->fs, &v); /* will jump to label if condition is true */ enterblock(fs, &bl, 0); /* must enter block before 'goto' */ gotostat(ls, v.t); /* handle goto/break */ - while (testnext(ls, ';')) {} /* skip colons */ + while (testnext(ls, ';')) {} /* skip semicolons */ if (block_follow(ls, 0)) { /* 'goto' is the entire block? */ leaveblock(fs); return; /* and that is it */ From f185c0132e09a5e0a32782c674de75b510f887d8 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Sat, 12 Aug 2017 10:12:42 -0300 Subject: [PATCH 0076/1145] comment in code fragment --- bugs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bugs b/bugs index a071937e74..8761cb6392 100644 --- a/bugs +++ b/bugs @@ -3680,9 +3680,9 @@ It needs an "interceptor" 'memcmp' function that continues reading memory after a difference is found.]], patch = [[ 2c2 -< ** $Id: bugs,v 1.154 2017/05/22 12:55:16 roberto Exp roberto $ +< ** $Id: bugs,v 1.155 2017/07/27 13:55:38 roberto Exp roberto $ --- -> ** $Id: bugs,v 1.154 2017/05/22 12:55:16 roberto Exp roberto $ +> ** $Id: bugs,v 1.155 2017/07/27 13:55:38 roberto Exp roberto $ 263c263,264 < for (option = LUA_STRFTIMEOPTIONS; *option != '\0'; option += oplen) { --- @@ -3761,7 +3761,7 @@ patch = [[ enterblock(fs, &bl, 0); /* must enter block before 'goto' */ gotostat(ls, v.t); /* handle goto/break */ - skipnoopstat(ls); /* skip other no-op statements */ -+ while (testnext(ls, ';')) {} /* skip colons */ ++ while (testnext(ls, ';')) {} /* skip semicolons */ if (block_follow(ls, 0)) { /* 'goto' is the entire block? */ leaveblock(fs); return; /* and that is it */ From ac65bab25f1eaaf70e1826a6937a74b3a55b521b Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 14 Aug 2017 15:33:14 -0300 Subject: [PATCH 0077/1145] jumps in 'for' loops don't need to be signed --- lopcodes.c | 8 ++++---- lopcodes.h | 10 +++++----- lparser.c | 30 ++++++++++++++++++++++++------ lvm.c | 10 +++++----- 4 files changed, 38 insertions(+), 20 deletions(-) diff --git a/lopcodes.c b/lopcodes.c index 163eff608e..74195d42a7 100644 --- a/lopcodes.c +++ b/lopcodes.c @@ -1,5 +1,5 @@ /* -** $Id: lopcodes.c,v 1.58 2017/04/28 20:57:45 roberto Exp roberto $ +** $Id: lopcodes.c,v 1.59 2017/06/29 15:38:41 roberto Exp roberto $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ @@ -124,10 +124,10 @@ LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = { ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_CALL */ ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_TAILCALL */ ,opmode(0, 0, OpArgU, OpArgN, iABC) /* OP_RETURN */ - ,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_FORLOOP */ - ,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_FORPREP */ + ,opmode(0, 1, OpArgR, OpArgN, iABx) /* OP_FORLOOP */ + ,opmode(0, 1, OpArgR, OpArgN, iABx) /* OP_FORPREP */ ,opmode(0, 0, OpArgN, OpArgU, iABC) /* OP_TFORCALL */ - ,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_TFORLOOP */ + ,opmode(0, 1, OpArgR, OpArgN, iABx) /* OP_TFORLOOP */ ,opmode(0, 0, OpArgU, OpArgU, iABC) /* OP_SETLIST */ ,opmode(0, 1, OpArgU, OpArgN, iABx) /* OP_CLOSURE */ ,opmode(0, 1, OpArgU, OpArgR, iABC) /* OP_VARARG */ diff --git a/lopcodes.h b/lopcodes.h index fb989ac606..f35de02a78 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -1,5 +1,5 @@ /* -** $Id: lopcodes.h,v 1.154 2017/05/08 16:08:01 roberto Exp roberto $ +** $Id: lopcodes.h,v 1.155 2017/06/29 15:38:41 roberto Exp roberto $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ @@ -229,12 +229,12 @@ OP_CALL,/* A B C R(A), ... ,R(A+C-2) := R(A)(R(A+1), ... ,R(A+B-1)) */ OP_TAILCALL,/* A B C return R(A)(R(A+1), ... ,R(A+B-1)) */ OP_RETURN,/* A B return R(A), ... ,R(A+B-2) (see note) */ -OP_FORLOOP,/* A sBx R(A)+=R(A+2); - if R(A) f->code[pc]; + int offset = dest - (pc + 1); + if (back) + offset = -offset; + if (offset > MAXARG_Bx) + luaX_syntaxerror(fs->ls, "control structure too long"); + SETARG_Bx(*jmp, offset); +} + + static void forbody (LexState *ls, int base, int line, int nvars, int isnum) { /* forbody -> DO block */ BlockCnt bl; @@ -1314,21 +1330,23 @@ static void forbody (LexState *ls, int base, int line, int nvars, int isnum) { int prep, endfor; adjustlocalvars(ls, 3); /* control variables */ checknext(ls, TK_DO); - prep = isnum ? luaK_codeAsBx(fs, OP_FORPREP, base, NO_JUMP) : luaK_jump(fs); + prep = isnum ? luaK_codeABx(fs, OP_FORPREP, base, 0) : luaK_jump(fs); enterblock(fs, &bl, 0); /* scope for declared variables */ adjustlocalvars(ls, nvars); luaK_reserveregs(fs, nvars); block(ls); leaveblock(fs); /* end of scope for declared variables */ - luaK_patchtohere(fs, prep); - if (isnum) /* numeric for? */ - endfor = luaK_codeAsBx(fs, OP_FORLOOP, base, NO_JUMP); + if (isnum) { /* numeric for? */ + fixforjump(fs, prep, luaK_getlabel(fs), 0); + endfor = luaK_codeABx(fs, OP_FORLOOP, base, 0); + } else { /* generic for */ + luaK_patchtohere(fs, prep); luaK_codeABC(fs, OP_TFORCALL, base, 0, nvars); luaK_fixline(fs, line); endfor = luaK_codeAsBx(fs, OP_TFORLOOP, base + 2, NO_JUMP); } - luaK_patchlist(fs, endfor, prep + 1); + fixforjump(fs, endfor, prep + 1, 1); luaK_fixline(fs, line); } diff --git a/lvm.c b/lvm.c index 18799be7ce..be4793d12f 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.289 2017/06/29 15:38:41 roberto Exp roberto $ +** $Id: lvm.c,v 2.290 2017/07/07 16:34:32 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -1335,7 +1335,7 @@ void luaV_execute (lua_State *L) { lua_Integer idx = intop(+, ivalue(s2v(ra)), step); /* increment index */ lua_Integer limit = ivalue(s2v(ra + 1)); if ((0 < step) ? (idx <= limit) : (limit <= idx)) { - pc += GETARG_sBx(i); /* jump back */ + pc -= GETARG_Bx(i); /* jump back */ chgivalue(s2v(ra), idx); /* update internal index... */ setivalue(s2v(ra + 3), idx); /* ...and external index */ } @@ -1347,7 +1347,7 @@ void luaV_execute (lua_State *L) { idx = luai_numadd(L, idx, step); /* inc. index */ if (luai_numlt(0, step) ? luai_numle(idx, limit) : luai_numle(limit, idx)) { - pc += GETARG_sBx(i); /* jump back */ + pc -= GETARG_Bx(i); /* jump back */ chgfltvalue(s2v(ra), idx); /* update internal index... */ setfltvalue(s2v(ra + 3), idx); /* ...and external index */ } @@ -1381,7 +1381,7 @@ void luaV_execute (lua_State *L) { luaG_runerror(L, "'for' initial value must be a number"); setfltvalue(init, luai_numsub(L, ninit, nstep)); } - pc += GETARG_sBx(i); + pc += GETARG_Bx(i); vmbreak; } vmcase(OP_TFORCALL) { @@ -1401,7 +1401,7 @@ void luaV_execute (lua_State *L) { l_tforloop: if (!ttisnil(s2v(ra + 1))) { /* continue loop? */ setobjs2s(L, ra, ra + 1); /* save control variable */ - pc += GETARG_sBx(i); /* jump back */ + pc -= GETARG_Bx(i); /* jump back */ } vmbreak; } From 029d269f4d1afd0e9ea0f889eb3e65a7f0fab0f9 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 31 Aug 2017 13:14:41 -0300 Subject: [PATCH 0078/1145] bug: dead keys with nil values can stay in weak tables --- bugs | 38 ++++++++++++++++++++++++++++++++++++-- lgc.c | 21 +++++++++++---------- 2 files changed, 47 insertions(+), 12 deletions(-) diff --git a/bugs b/bugs index 8761cb6392..f3caeec23d 100644 --- a/bugs +++ b/bugs @@ -3680,9 +3680,9 @@ It needs an "interceptor" 'memcmp' function that continues reading memory after a difference is found.]], patch = [[ 2c2 -< ** $Id: bugs,v 1.155 2017/07/27 13:55:38 roberto Exp roberto $ +< ** $Id: bugs,v 1.156 2017/08/12 13:12:42 roberto Exp roberto $ --- -> ** $Id: bugs,v 1.155 2017/07/27 13:55:38 roberto Exp roberto $ +> ** $Id: bugs,v 1.156 2017/08/12 13:12:42 roberto Exp roberto $ 263c263,264 < for (option = LUA_STRFTIMEOPTIONS; *option != '\0'; option += oplen) { --- @@ -3837,6 +3837,40 @@ patch = [[ } +Bug{ +what = [[dead keys with nil values can stay in weak tables]], +report = [[云风 Cloud Wu, 2017/08/15]], +since = [[5.2]], +fix = nil, +example = [[ +-- The following chunk, under a memory checker like valgrind, +-- produces a memory access violation. + +local a = setmetatable({}, {__mode = 'kv'}) + +a['ABCDEFGHIJKLMNOPQRSTUVWXYZ' .. 'abcdefghijklmnopqrstuvwxyz'] = {} +a[next(a)] = nil +collectgarbage() +print(a['BCDEFGHIJKLMNOPQRSTUVWXYZ' .. 'abcdefghijklmnopqrstuvwxyz']) +]], +patch = [[ +--- lgc.c 2016/12/22 13:08:50 2.215 ++++ lgc.c 2017/08/31 16:08:23 +@@ -643,8 +643,9 @@ + for (n = gnode(h, 0); n < limit; n++) { + if (!ttisnil(gval(n)) && (iscleared(g, gkey(n)))) { + setnilvalue(gval(n)); /* remove value ... */ +- removeentry(n); /* and remove entry from table */ + } ++ if (ttisnil(gval(n))) /* is entry empty? */ ++ removeentry(n); /* remove entry from table */ + } + } + } +]] +} + + --[=[ diff --git a/lgc.c b/lgc.c index c39850a437..e658a6f8dc 100644 --- a/lgc.c +++ b/lgc.c @@ -1,5 +1,5 @@ /* -** $Id: lgc.c,v 2.232 2017/06/12 14:21:44 roberto Exp roberto $ +** $Id: lgc.c,v 2.233 2017/06/29 15:06:44 roberto Exp roberto $ ** Garbage Collector ** See Copyright Notice in lua.h */ @@ -117,7 +117,8 @@ static lu_mem atomic (lua_State *L); /* -** If key is not marked, mark its entry as dead. This allows the +** Clear keys for empty entries in tables. If entry is empty +** and its key is not marked, mark its entry as dead. This allows the ** collection of the key, but keeps its entry in the table (its removal ** could break a chain). Other places never manipulate dead keys, ** because its associated nil value is enough to signal that the entry @@ -688,10 +689,10 @@ static void clearkeys (global_State *g, GCObject *l) { Table *h = gco2t(l); Node *n, *limit = gnodelast(h); for (n = gnode(h, 0); n < limit; n++) { - if (!ttisnil(gval(n)) && (iscleared(g, gckeyN(n)))) { - setnilvalue(gval(n)); /* remove value ... */ - removeentry(n); /* and remove entry from table */ - } + if (!ttisnil(gval(n)) && (iscleared(g, gckeyN(n)))) /* unmarked key? */ + setnilvalue(gval(n)); /* clear value */ + if (ttisnil(gval(n))) /* is entry empty? */ + removeentry(n); /* remove it from table */ } } } @@ -712,10 +713,10 @@ static void clearvalues (global_State *g, GCObject *l, GCObject *f) { setnilvalue(o); /* remove value */ } for (n = gnode(h, 0); n < limit; n++) { - if (iscleared(g, gcvalueN(gval(n)))) { - setnilvalue(gval(n)); /* remove value ... */ - removeentry(n); /* and remove entry from table */ - } + if (iscleared(g, gcvalueN(gval(n)))) /* unmarked value? */ + setnilvalue(gval(n)); /* clear value */ + if (ttisnil(gval(n))) /* is entry empty? */ + removeentry(n); /* remove it from table */ } } } From 80d9b09f351c7a9be557116e9c79ae11e9b3f032 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 13 Sep 2017 16:50:08 -0300 Subject: [PATCH 0079/1145] jumps do not close upvalues (to be faster and simpler); explicit instruction to close upvalues; command 'break' not handled like a 'goto' (to optimize removal of uneeded 'close' instructions) --- lcode.c | 51 ++++++++++++++++----- lcode.h | 6 ++- lopcodes.c | 6 ++- lopcodes.h | 5 ++- lparser.c | 127 ++++++++++++++++++++++++++++++++++------------------- lvm.c | 11 ++--- 6 files changed, 139 insertions(+), 67 deletions(-) diff --git a/lcode.c b/lcode.c index 169c439dbe..1edf08aa10 100644 --- a/lcode.c +++ b/lcode.c @@ -1,5 +1,5 @@ /* -** $Id: lcode.c,v 2.120 2017/06/27 11:35:31 roberto Exp roberto $ +** $Id: lcode.c,v 2.121 2017/06/29 15:06:44 roberto Exp roberto $ ** Code generator for Lua ** See Copyright Notice in lua.h */ @@ -272,20 +272,51 @@ void luaK_patchlist (FuncState *fs, int list, int target) { /* -** Path all jumps in 'list' to close upvalues up to given 'level' -** (The assertion checks that jumps either were closing nothing -** or were closing higher levels, from inner blocks.) +** Check whether some jump in given list needs a close instruction. */ -void luaK_patchclose (FuncState *fs, int list, int level) { - level++; /* argument is +1 to reserve 0 as non-op */ +int luaK_needclose (FuncState *fs, int list) { for (; list != NO_JUMP; list = getjump(fs, list)) { - lua_assert(GET_OPCODE(fs->f->code[list]) == OP_JMP && - (GETARG_A(fs->f->code[list]) == 0 || - GETARG_A(fs->f->code[list]) >= level)); - SETARG_A(fs->f->code[list], level); + if (GETARG_A(fs->f->code[list])) /* needs close? */ + return 1; + } + return 0; +} + + +/* +** Correct a jump list to jump to 'target'. If 'hasclose' is true, +** 'target' contains an OP_CLOSE instruction (see first assert). +** Only jumps with the A arg true need that close; other jumps +** avoid it jumping to the next instruction. +*/ +void luaK_patchgoto (FuncState *fs, int list, int target, int hasclose) { + lua_assert(!hasclose || GET_OPCODE(fs->f->code[target]) == OP_CLOSE); + while (list != NO_JUMP) { + int next = getjump(fs, list); + lua_assert(!GETARG_A(fs->f->code[list]) || hasclose); + patchtestreg(fs, list, NO_REG); /* do not generate values */ + if (!hasclose || GETARG_A(fs->f->code[list])) + fixjump(fs, list, target); + else /* there is a CLOSE instruction but jump does not need it */ + fixjump(fs, list, target + 1); /* avoid CLOSE instruction */ + list = next; } } + +/* +** Mark (using the A arg) all jumps in 'list' to close upvalues. Mark +** will instruct 'luaK_patchgoto' to make these jumps go to OP_CLOSE +** instructions. +*/ +void luaK_patchclose (FuncState *fs, int list) { + for (; list != NO_JUMP; list = getjump(fs, list)) { + lua_assert(GET_OPCODE(fs->f->code[list]) == OP_JMP); + SETARG_A(fs->f->code[list], 1); + } +} + + #if !defined(MAXIWTHABS) #define MAXIWTHABS 120 #endif diff --git a/lcode.h b/lcode.h index e7ff352272..70953559a4 100644 --- a/lcode.h +++ b/lcode.h @@ -1,5 +1,5 @@ /* -** $Id: lcode.h,v 1.64 2016/01/05 16:22:37 roberto Exp roberto $ +** $Id: lcode.h,v 1.65 2017/04/20 19:53:55 roberto Exp roberto $ ** Code generator for Lua ** See Copyright Notice in lua.h */ @@ -73,8 +73,10 @@ LUAI_FUNC void luaK_setoneret (FuncState *fs, expdesc *e); LUAI_FUNC int luaK_jump (FuncState *fs); LUAI_FUNC void luaK_ret (FuncState *fs, int first, int nret); LUAI_FUNC void luaK_patchlist (FuncState *fs, int list, int target); +void luaK_patchgoto (FuncState *fs, int list, int target, int hasclose); LUAI_FUNC void luaK_patchtohere (FuncState *fs, int list); -LUAI_FUNC void luaK_patchclose (FuncState *fs, int list, int level); +LUAI_FUNC void luaK_patchclose (FuncState *fs, int list); +LUAI_FUNC int luaK_needclose (FuncState *fs, int list); LUAI_FUNC void luaK_concat (FuncState *fs, int *l1, int l2); LUAI_FUNC int luaK_getlabel (FuncState *fs); LUAI_FUNC void luaK_prefix (FuncState *fs, UnOpr op, expdesc *v, int line); diff --git a/lopcodes.c b/lopcodes.c index 74195d42a7..a64c29cefc 100644 --- a/lopcodes.c +++ b/lopcodes.c @@ -1,5 +1,5 @@ /* -** $Id: lopcodes.c,v 1.59 2017/06/29 15:38:41 roberto Exp roberto $ +** $Id: lopcodes.c,v 1.60 2017/08/14 18:33:14 roberto Exp roberto $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ @@ -54,6 +54,7 @@ LUAI_DDEF const char *const luaP_opnames[NUM_OPCODES+1] = { "NOT", "LEN", "CONCAT", + "CLOSE", "JMP", "EQ", "LT", @@ -115,7 +116,8 @@ LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = { ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_NOT */ ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_LEN */ ,opmode(0, 1, OpArgR, OpArgR, iABC) /* OP_CONCAT */ - ,opmode(0, 0, OpArgR, OpArgN, iAsBx) /* OP_JMP */ + ,opmode(0, 0, OpArgN, OpArgN, iABC) /* OP_CLOSE */ + ,opmode(0, 0, OpArgU, OpArgN, iAsBx) /* OP_JMP */ ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_EQ */ ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_LT */ ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_LE */ diff --git a/lopcodes.h b/lopcodes.h index f35de02a78..4dfe66cea3 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -1,5 +1,5 @@ /* -** $Id: lopcodes.h,v 1.155 2017/06/29 15:38:41 roberto Exp roberto $ +** $Id: lopcodes.h,v 1.156 2017/08/14 18:33:14 roberto Exp roberto $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ @@ -217,7 +217,8 @@ OP_LEN,/* A B R(A) := length of R(B) */ OP_CONCAT,/* A B C R(A) := R(B).. ... ..R(C) */ -OP_JMP,/* A sBx pc+=sBx; if (A) close all upvalues >= R(A - 1) */ +OP_CLOSE,/* A close all upvalues >= R(A) */ +OP_JMP,/* sBx pc+=sBx */ OP_EQ,/* A B C if ((RK(B) == RK(C)) ~= A) then pc++ */ OP_LT,/* A B C if ((RK(B) < RK(C)) ~= A) then pc++ */ OP_LE,/* A B C if ((RK(B) <= RK(C)) ~= A) then pc++ */ diff --git a/lparser.c b/lparser.c index 2eb5581fa6..7fd62f507a 100644 --- a/lparser.c +++ b/lparser.c @@ -1,5 +1,5 @@ /* -** $Id: lparser.c,v 2.163 2017/08/12 13:12:21 roberto Exp roberto $ +** $Id: lparser.c,v 2.164 2017/08/14 18:33:14 roberto Exp roberto $ ** Lua Parser ** See Copyright Notice in lua.h */ @@ -49,6 +49,8 @@ typedef struct BlockCnt { struct BlockCnt *previous; /* chain */ int firstlabel; /* index of first label in this block */ int firstgoto; /* index of first pending goto in this block */ + int brks; /* list of break jumps in this block */ + lu_byte brkcls; /* true if some 'break' needs to close upvalues */ lu_byte nactvar; /* # active locals outside the block */ lu_byte upval; /* true if some variable in the block is an upvalue */ lu_byte isloop; /* true if 'block' is a loop */ @@ -351,7 +353,7 @@ static void closegoto (LexState *ls, int g, Labeldesc *label) { getstr(gt->name), gt->line, getstr(vname)); semerror(ls, msg); } - luaK_patchlist(fs, gt->pc, label->pc); + luaK_patchgoto(fs, gt->pc, label->pc, 1); /* remove goto from pending list */ for (i = g; i < gl->n - 1; i++) gl->arr[i] = gl->arr[i + 1]; @@ -362,7 +364,7 @@ static void closegoto (LexState *ls, int g, Labeldesc *label) { /* ** try to close a goto with existing labels; this solves backward jumps */ -static int findlabel (LexState *ls, int g) { +static int solvelabel (LexState *ls, int g) { int i; BlockCnt *bl = ls->fs->bl; Dyndata *dyd = ls->dyd; @@ -373,7 +375,7 @@ static int findlabel (LexState *ls, int g) { if (eqstr(lb->name, gt->name)) { /* correct label? */ if (gt->nactvar > lb->nactvar && (bl->upval || dyd->label.n > bl->firstlabel)) - luaK_patchclose(ls->fs, gt->pc, lb->nactvar); + luaK_patchclose(ls->fs, gt->pc); closegoto(ls, g, lb); /* close it */ return 1; } @@ -400,12 +402,12 @@ static int newlabelentry (LexState *ls, Labellist *l, TString *name, ** check whether new label 'lb' matches any pending gotos in current ** block; solves forward jumps */ -static void findgotos (LexState *ls, Labeldesc *lb) { +static void solvegotos (LexState *ls, Labeldesc *lb) { Labellist *gl = &ls->dyd->gt; int i = ls->fs->bl->firstgoto; while (i < gl->n) { if (eqstr(gl->arr[i].name, lb->name)) - closegoto(ls, i, lb); + closegoto(ls, i, lb); /* will remove 'i' from the list */ else i++; } @@ -416,23 +418,32 @@ static void findgotos (LexState *ls, Labeldesc *lb) { ** export pending gotos to outer level, to check them against ** outer labels; if the block being exited has upvalues, and ** the goto exits the scope of any variable (which can be the -** upvalue), close those variables being exited. +** upvalue), close those variables being exited. Also export +** break list. */ static void movegotosout (FuncState *fs, BlockCnt *bl) { int i = bl->firstgoto; Labellist *gl = &fs->ls->dyd->gt; /* correct pending gotos to current block and try to close it with visible labels */ - while (i < gl->n) { + while (i < gl->n) { /* for each pending goto */ Labeldesc *gt = &gl->arr[i]; - if (gt->nactvar > bl->nactvar) { - if (bl->upval) - luaK_patchclose(fs, gt->pc, bl->nactvar); - gt->nactvar = bl->nactvar; + if (gt->nactvar > bl->nactvar) { /* leaving a variable scope? */ + if (bl->upval) /* variable may be an upvalue? */ + luaK_patchclose(fs, gt->pc); /* jump will need a close */ + gt->nactvar = bl->nactvar; /* update goto level */ } - if (!findlabel(fs->ls, i)) + if (!solvelabel(fs->ls, i)) i++; /* move to next one */ + /* else, 'solvelabel' removed current goto from the list + and 'i' now points to next one */ } + /* handles break list */ + if (bl->upval) /* exiting the scope of an upvalue? */ + luaK_patchclose(fs, bl->brks); /* breaks will need OP_CLOSE */ + /* move breaks to outer block */ + luaK_concat(fs, &bl->previous->brks, bl->brks); + bl->previous->brkcls |= bl->brkcls; } @@ -441,6 +452,8 @@ static void enterblock (FuncState *fs, BlockCnt *bl, lu_byte isloop) { bl->nactvar = fs->nactvar; bl->firstlabel = fs->ls->dyd->label.n; bl->firstgoto = fs->ls->dyd->gt.n; + bl->brks = NO_JUMP; + bl->brkcls = 0; bl->upval = 0; bl->previous = fs->bl; fs->bl = bl; @@ -449,22 +462,24 @@ static void enterblock (FuncState *fs, BlockCnt *bl, lu_byte isloop) { /* -** create a label named 'break' to resolve break statements +** Fix all breaks in block 'bl' to jump to the end of the block. */ -static void breaklabel (LexState *ls) { - TString *n = luaS_new(ls->L, "break"); - int l = newlabelentry(ls, &ls->dyd->label, n, 0, ls->fs->pc); - findgotos(ls, &ls->dyd->label.arr[l]); +static void fixbreaks (FuncState *fs, BlockCnt *bl) { + int target = fs->pc; + if (bl->brkcls) /* does the block need to close upvalues? */ + luaK_codeABC(fs, OP_CLOSE, bl->nactvar, 0, 0); + luaK_patchgoto(fs, bl->brks, target, bl->brkcls); + bl->brks = NO_JUMP; /* no more breaks to fix */ + bl->brkcls = 0; /* no more need to close upvalues */ + lua_assert(!bl->upval); /* loop body cannot have local variables */ } + /* -** generates an error for an undefined 'goto'; choose appropriate -** message when label name is a reserved word (which can only be 'break') +** generates an error for an undefined 'goto'. */ static l_noret undefgoto (LexState *ls, Labeldesc *gt) { - const char *msg = isreserved(gt->name) - ? "<%s> at line %d not inside a loop" - : "no visible label '%s' for at line %d"; + const char *msg = "no visible label '%s' for at line %d"; msg = luaO_pushfstring(ls->L, msg, getstr(gt->name), gt->line); semerror(ls, msg); } @@ -473,14 +488,12 @@ static l_noret undefgoto (LexState *ls, Labeldesc *gt) { static void leaveblock (FuncState *fs) { BlockCnt *bl = fs->bl; LexState *ls = fs->ls; - if (bl->previous && bl->upval) { - /* create a 'jump to here' to close upvalues */ - int j = luaK_jump(fs); - luaK_patchclose(fs, j, bl->nactvar); - luaK_patchtohere(fs, j); - } + if (bl->upval && bl->brks != NO_JUMP) /* breaks in upvalue scopes? */ + bl->brkcls = 1; /* these breaks must close the upvalues */ if (bl->isloop) - breaklabel(ls); /* close pending breaks */ + fixbreaks(fs, bl); /* fix pending breaks */ + if (bl->previous && bl->upval) + luaK_codeABC(fs, OP_CLOSE, bl->nactvar, 0, 0); fs->bl = bl->previous; removevars(fs, bl->nactvar); lua_assert(bl->nactvar == fs->nactvar); @@ -488,8 +501,11 @@ static void leaveblock (FuncState *fs) { ls->dyd->label.n = bl->firstlabel; /* remove local labels */ if (bl->previous) /* inner block? */ movegotosout(fs, bl); /* update pending gotos to outer block */ - else if (bl->firstgoto < ls->dyd->gt.n) /* pending gotos in outer block? */ - undefgoto(ls, &ls->dyd->gt.arr[bl->firstgoto]); /* error */ + else { + lua_assert(bl->brks == NO_JUMP); /* no pending breaks */ + if (bl->firstgoto < ls->dyd->gt.n) /* pending gotos in outer block? */ + undefgoto(ls, &ls->dyd->gt.arr[bl->firstgoto]); /* error */ + } } @@ -1205,16 +1221,21 @@ static int cond (LexState *ls) { static void gotostat (LexState *ls, int pc) { int line = ls->linenumber; - TString *label; int g; - if (testnext(ls, TK_GOTO)) - label = str_checkname(ls); - else { - luaX_next(ls); /* skip break */ - label = luaS_new(ls->L, "break"); - } - g = newlabelentry(ls, &ls->dyd->gt, label, line, pc); - findlabel(ls, g); /* close it if label already defined */ + luaX_next(ls); /* skip 'goto' */ + g = newlabelentry(ls, &ls->dyd->gt, str_checkname(ls), line, pc); + solvelabel(ls, g); /* close it if label already defined */ +} + + +static void breakstat (LexState *ls, int pc) { + FuncState *fs = ls->fs; + BlockCnt *bl = fs->bl; + luaX_next(ls); /* skip break */ + while (bl && !bl->isloop) { bl = bl->previous; } + if (!bl) + luaX_syntaxerror(ls, "no loop to break"); + luaK_concat(fs, &fs->bl->brks, pc); } @@ -1248,12 +1269,13 @@ static void labelstat (LexState *ls, TString *label, int line) { checknext(ls, TK_DBCOLON); /* skip double colon */ /* create new entry for this label */ l = newlabelentry(ls, ll, label, line, luaK_getlabel(fs)); + luaK_codeABC(fs, OP_CLOSE, fs->nactvar, 0, 0); skipnoopstat(ls); /* skip other no-op statements */ if (block_follow(ls, 0)) { /* label is last no-op statement in the block? */ /* assume that locals are already out of scope */ ll->arr[l].nactvar = fs->bl->nactvar; } - findgotos(ls, &ll->arr[l]); + solvegotos(ls, &ll->arr[l]); } @@ -1289,8 +1311,15 @@ static void repeatstat (LexState *ls, int line) { check_match(ls, TK_UNTIL, TK_REPEAT, line); condexit = cond(ls); /* read condition (inside scope block) */ if (bl2.upval) /* upvalues? */ - luaK_patchclose(fs, condexit, bl2.nactvar); + luaK_patchclose(fs, condexit); leaveblock(fs); /* finish scope */ + if (bl2.upval) { /* upvalues? */ + int exit = luaK_jump(fs); /* normal exit must jump over fix */ + luaK_patchtohere(fs, condexit); /* repetition must close upvalues */ + luaK_codeABC(fs, OP_CLOSE, bl2.nactvar, 0, 0); + condexit = luaK_jump(fs); /* repeat after closing upvalues */ + luaK_patchtohere(fs, exit); /* normal exit comes to here */ + } luaK_patchlist(fs, condexit, repeat_init); /* close the loop */ leaveblock(fs); /* finish loop */ } @@ -1428,9 +1457,12 @@ static void test_then_block (LexState *ls, int *escapelist) { if (ls->t.token == TK_GOTO || ls->t.token == TK_BREAK) { luaK_goiffalse(ls->fs, &v); /* will jump to label if condition is true */ enterblock(fs, &bl, 0); /* must enter block before 'goto' */ - gotostat(ls, v.t); /* handle goto/break */ + if (ls->t.token == TK_GOTO) + gotostat(ls, v.t); /* handle goto */ + else + breakstat(ls, v.t); /* handle break */ while (testnext(ls, ';')) {} /* skip semicolons */ - if (block_follow(ls, 0)) { /* 'goto' is the entire block? */ + if (block_follow(ls, 0)) { /* 'goto'/'break' is the entire block? */ leaveblock(fs); return; /* and that is it */ } @@ -1623,7 +1655,10 @@ static void statement (LexState *ls) { retstat(ls); break; } - case TK_BREAK: /* stat -> breakstat */ + case TK_BREAK: { /* stat -> breakstat */ + breakstat(ls, luaK_jump(ls->fs)); + break; + } case TK_GOTO: { /* stat -> 'goto' NAME */ gotostat(ls, luaK_jump(ls->fs)); break; diff --git a/lvm.c b/lvm.c index be4793d12f..b1e74f3d95 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.290 2017/07/07 16:34:32 roberto Exp roberto $ +** $Id: lvm.c,v 2.291 2017/08/14 18:33:14 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -753,10 +753,7 @@ void luaV_finishOp (lua_State *L) { ** Execute a jump instruction. The 'updatemask' allows signals to stop ** tight loops. (Without it, the local copy of 'mask' could never change.) */ -#define dojump(ci,i,e) \ - { int a = GETARG_A(i); \ - if (a != 0) luaF_close(L, ci->func + a); \ - pc += GETARG_sBx(i) + e; updatemask(L); } +#define dojump(ci,i,e) { pc += GETARG_sBx(i) + e; updatemask(L); } /* for test instructions, execute the jump instruction that follows it */ @@ -1201,6 +1198,10 @@ void luaV_execute (lua_State *L) { L->top = ci->top; /* restore top */ vmbreak; } + vmcase(OP_CLOSE) { + luaF_close(L, ra); + vmbreak; + } vmcase(OP_JMP) { dojump(ci, i, 0); vmbreak; From 022e4427cfc5edde499c782ba716f8cf95b63f91 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 15 Sep 2017 11:19:06 -0300 Subject: [PATCH 0080/1145] detail (keep OP_LOADK and OP_LOADKX together) --- lopcodes.c | 6 +++--- lopcodes.h | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lopcodes.c b/lopcodes.c index a64c29cefc..7e1ace2567 100644 --- a/lopcodes.c +++ b/lopcodes.c @@ -1,5 +1,5 @@ /* -** $Id: lopcodes.c,v 1.60 2017/08/14 18:33:14 roberto Exp roberto $ +** $Id: lopcodes.c,v 1.61 2017/09/13 19:50:08 roberto Exp roberto $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ @@ -19,8 +19,8 @@ LUAI_DDEF const char *const luaP_opnames[NUM_OPCODES+1] = { "MOVE", - "LOADK", "LOADI", + "LOADK", "LOADKX", "LOADBOOL", "LOADNIL", @@ -81,8 +81,8 @@ LUAI_DDEF const char *const luaP_opnames[NUM_OPCODES+1] = { LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = { /* T A B C mode opcode */ opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_MOVE */ - ,opmode(0, 1, OpArgK, OpArgN, iABx) /* OP_LOADK */ ,opmode(0, 1, OpArgU, OpArgN, iAsBx) /* OP_LOADI */ + ,opmode(0, 1, OpArgK, OpArgN, iABx) /* OP_LOADK */ ,opmode(0, 1, OpArgN, OpArgN, iABx) /* OP_LOADKX */ ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_LOADBOOL */ ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_LOADNIL */ diff --git a/lopcodes.h b/lopcodes.h index 4dfe66cea3..7f17de1cda 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -1,5 +1,5 @@ /* -** $Id: lopcodes.h,v 1.156 2017/08/14 18:33:14 roberto Exp roberto $ +** $Id: lopcodes.h,v 1.157 2017/09/13 19:50:08 roberto Exp roberto $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ @@ -175,8 +175,8 @@ typedef enum { name args description ------------------------------------------------------------------------*/ OP_MOVE,/* A B R(A) := R(B) */ -OP_LOADK,/* A Bx R(A) := Kst(Bx) */ OP_LOADI,/* A sBx R(A) := sBx */ +OP_LOADK,/* A Bx R(A) := Kst(Bx) */ OP_LOADKX,/* A R(A) := Kst(extra arg) */ OP_LOADBOOL,/* A B C R(A) := (Bool)B; if (C) pc++ */ OP_LOADNIL,/* A B R(A), R(A+1), ..., R(A+B) := nil */ From e0c0e2ee14f519ad33429533f87519753f07051a Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 18 Sep 2017 13:07:54 -0300 Subject: [PATCH 0081/1145] comments (documentation about instruction formats) --- lopcodes.h | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/lopcodes.h b/lopcodes.h index 7f17de1cda..3d69196c78 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -1,5 +1,5 @@ /* -** $Id: lopcodes.h,v 1.157 2017/09/13 19:50:08 roberto Exp roberto $ +** $Id: lopcodes.h,v 1.158 2017/09/15 14:19:06 roberto Exp roberto $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ @@ -11,21 +11,20 @@ /*=========================================================================== - We assume that instructions are unsigned numbers. + We assume that instructions are unsigned 32-bit integers. All instructions have an opcode in the first 6 bits. - Instructions can have the following fields: - 'A' : 8 bits - 'B' : 9 bits - 'C' : 9 bits - 'Ax' : 26 bits ('A', 'B', and 'C' together) - 'Bx' : 18 bits ('B' and 'C' together) - 'sBx' : signed Bx - - A signed argument is represented in excess K; that is, the number - value is the unsigned value minus K. K is exactly the maximum value - for that argument (so that -max is represented by 0, and +max is - represented by 2*max), which is half the maximum for the corresponding - unsigned argument. + Instructions can have the following formats: + + 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 + 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 +iABC | C(9) | | B(9) | | A(8) | | Op(6) | +iABx | Bx(18) | | A(8) | | Op(6) | +iAsBx | sBx (signed)(18) | | A(8) | | Op(6) | +iAx | Ax(26) | | Op(6) | + + A signed argument is represented in excess K: the represented value is + the written unsigned value minus K, where K is half the maximum for the + corresponding unsigned argument. ===========================================================================*/ From abb17cf19bf7328a394f1758542172c4b4dbb539 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 19 Sep 2017 15:38:14 -0300 Subject: [PATCH 0082/1145] new opcode OP_LOADF (load immediate float) --- lcode.c | 16 ++++++++++++++-- lopcodes.c | 4 +++- lopcodes.h | 3 ++- lvm.c | 7 ++++++- 4 files changed, 25 insertions(+), 5 deletions(-) diff --git a/lcode.c b/lcode.c index 1edf08aa10..f7bc21408f 100644 --- a/lcode.c +++ b/lcode.c @@ -1,5 +1,5 @@ /* -** $Id: lcode.c,v 2.121 2017/06/29 15:06:44 roberto Exp roberto $ +** $Id: lcode.c,v 2.122 2017/09/13 19:50:08 roberto Exp roberto $ ** Code generator for Lua ** See Copyright Notice in lua.h */ @@ -579,6 +579,18 @@ void luaK_int (FuncState *fs, int reg, lua_Integer i) { } +static void luaK_float (FuncState *fs, int reg, lua_Number f) { + TValue v; + lua_Integer fi; + setfltvalue(&v, f); + if (luaV_tointeger(&v, &fi, 0) && + l_castS2U(fi) + MAXARG_sBx <= l_castS2U(MAXARG_Bx)) + luaK_codeAsBx(fs, OP_LOADF, reg, cast_int(fi)); + else + luaK_codek(fs, reg, luaK_numberK(fs, f)); +} + + /* ** Fix an expression to return the number of results 'nresults'. ** Either 'e' is a multi-ret expression (function call or vararg) @@ -688,7 +700,7 @@ static void discharge2reg (FuncState *fs, expdesc *e, int reg) { break; } case VKFLT: { - luaK_codek(fs, reg, luaK_numberK(fs, e->u.nval)); + luaK_float(fs, reg, e->u.nval); break; } case VKINT: { diff --git a/lopcodes.c b/lopcodes.c index 7e1ace2567..9279a8dfac 100644 --- a/lopcodes.c +++ b/lopcodes.c @@ -1,5 +1,5 @@ /* -** $Id: lopcodes.c,v 1.61 2017/09/13 19:50:08 roberto Exp roberto $ +** $Id: lopcodes.c,v 1.62 2017/09/15 14:19:06 roberto Exp roberto $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ @@ -20,6 +20,7 @@ LUAI_DDEF const char *const luaP_opnames[NUM_OPCODES+1] = { "MOVE", "LOADI", + "LOADF", "LOADK", "LOADKX", "LOADBOOL", @@ -82,6 +83,7 @@ LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = { /* T A B C mode opcode */ opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_MOVE */ ,opmode(0, 1, OpArgU, OpArgN, iAsBx) /* OP_LOADI */ + ,opmode(0, 1, OpArgU, OpArgN, iAsBx) /* OP_LOADF */ ,opmode(0, 1, OpArgK, OpArgN, iABx) /* OP_LOADK */ ,opmode(0, 1, OpArgN, OpArgN, iABx) /* OP_LOADKX */ ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_LOADBOOL */ diff --git a/lopcodes.h b/lopcodes.h index 3d69196c78..9e0ce498a8 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -1,5 +1,5 @@ /* -** $Id: lopcodes.h,v 1.158 2017/09/15 14:19:06 roberto Exp roberto $ +** $Id: lopcodes.h,v 1.159 2017/09/18 16:07:54 roberto Exp roberto $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ @@ -175,6 +175,7 @@ name args description ------------------------------------------------------------------------*/ OP_MOVE,/* A B R(A) := R(B) */ OP_LOADI,/* A sBx R(A) := sBx */ +OP_LOADF,/* A sBx R(A) := (lua_Number)sBx */ OP_LOADK,/* A Bx R(A) := Kst(Bx) */ OP_LOADKX,/* A R(A) := Kst(extra arg) */ OP_LOADBOOL,/* A B C R(A) := (Bool)B; if (C) pc++ */ diff --git a/lvm.c b/lvm.c index b1e74f3d95..a41b44d63e 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.291 2017/08/14 18:33:14 roberto Exp roberto $ +** $Id: lvm.c,v 2.292 2017/09/13 19:50:08 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -829,6 +829,11 @@ void luaV_execute (lua_State *L) { setivalue(s2v(ra), b); vmbreak; } + vmcase(OP_LOADF) { + int b = GETARG_sBx(i); + setfltvalue(s2v(ra), cast_num(b)); + vmbreak; + } vmcase(OP_LOADKX) { TValue *rb; lua_assert(GET_OPCODE(*pc) == OP_EXTRAARG); From 13256a4173af4004518d65211b4bf1aebcd5fc25 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 26 Sep 2017 14:10:49 -0300 Subject: [PATCH 0083/1145] detail put explicit all cases for a switch of an enumeration --- lcode.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lcode.c b/lcode.c index f7bc21408f..0fcde1547c 100644 --- a/lcode.c +++ b/lcode.c @@ -1,5 +1,5 @@ /* -** $Id: lcode.c,v 2.122 2017/09/13 19:50:08 roberto Exp roberto $ +** $Id: lcode.c,v 2.123 2017/09/19 18:38:14 roberto Exp roberto $ ** Code generator for Lua ** See Copyright Notice in lua.h */ @@ -1263,10 +1263,12 @@ void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) { /* else keep numeral, which may be folded with 2nd operand */ break; } - default: { + case OPR_EQ: case OPR_LT: case OPR_LE: + case OPR_NE: case OPR_GT: case OPR_GE: { luaK_exp2RK(fs, v); break; } + default: lua_assert(0); } } From 00e728af885d2781e365071557530a6685110d7e Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 26 Sep 2017 15:14:45 -0300 Subject: [PATCH 0084/1145] binary operators use R instead of RK faster + nobody uses RK(B), so B can be smaller (freeing one bit for more opcodes, soon) --- lcode.c | 21 +++++++++--------- lopcodes.c | 32 +++++++++++++-------------- lopcodes.h | 32 +++++++++++++-------------- lvm.c | 64 ++++++++++++++++++++++++++---------------------------- 4 files changed, 73 insertions(+), 76 deletions(-) diff --git a/lcode.c b/lcode.c index 0fcde1547c..775a22c39e 100644 --- a/lcode.c +++ b/lcode.c @@ -1,5 +1,5 @@ /* -** $Id: lcode.c,v 2.123 2017/09/19 18:38:14 roberto Exp roberto $ +** $Id: lcode.c,v 2.124 2017/09/26 17:10:49 roberto Exp roberto $ ** Code generator for Lua ** See Copyright Notice in lua.h */ @@ -1157,7 +1157,7 @@ static void codeunexpval (FuncState *fs, OpCode op, expdesc *e, int line) { ** (everything but logical operators 'and'/'or' and comparison ** operators). ** Expression to produce final result will be encoded in 'e1'. -** Because 'luaK_exp2RK' can free registers, its calls must be +** Because 'luaK_exp2anyreg' can free registers, its calls must be ** in "stack order" (that is, first on 'e2', which may have more ** recent registers to be released). */ @@ -1176,8 +1176,8 @@ static void codebinexpval (FuncState *fs, OpCode op, op = OP_ADDI; } else { - v2 = luaK_exp2RK(fs, e2); /* both operands are "RK" */ - v1 = luaK_exp2RK(fs, e1); + v2 = luaK_exp2anyreg(fs, e2); /* both operands are in registers */ + v1 = luaK_exp2anyreg(fs, e1); } freeexps(fs, e1, e2); e1->u.info = luaK_codeABC(fs, op, 0, v1, v2); /* generate opcode */ @@ -1188,12 +1188,12 @@ static void codebinexpval (FuncState *fs, OpCode op, /* ** Emit code for comparisons. -** 'e1' was already put in R/K form by 'luaK_infix'. +** 'e1' was already put in register by 'luaK_infix'. */ static void codecomp (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) { int rk1 = (e1->k == VK) ? RKASK(e1->u.info) : check_exp(e1->k == VNONRELOC, e1->u.info); - int rk2 = luaK_exp2RK(fs, e2); + int rk2 = luaK_exp2anyreg(fs, e2); freeexps(fs, e1, e2); switch (opr) { case OPR_NE: { /* '(a ~= b)' ==> 'not (a == b)' */ @@ -1258,14 +1258,13 @@ void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) { case OPR_MOD: case OPR_POW: case OPR_BAND: case OPR_BOR: case OPR_BXOR: case OPR_SHL: case OPR_SHR: { - if (!tonumeral(v, NULL)) - luaK_exp2RK(fs, v); - /* else keep numeral, which may be folded with 2nd operand */ - break; + if (tonumeral(v, NULL)) + break; /* keep numeral, which may be folded with 2nd operand */ + /* else *//* FALLTHROUGH */ } case OPR_EQ: case OPR_LT: case OPR_LE: case OPR_NE: case OPR_GT: case OPR_GE: { - luaK_exp2RK(fs, v); + luaK_exp2anyreg(fs, v); break; } default: lua_assert(0); diff --git a/lopcodes.c b/lopcodes.c index 9279a8dfac..9617b7202d 100644 --- a/lopcodes.c +++ b/lopcodes.c @@ -1,5 +1,5 @@ /* -** $Id: lopcodes.c,v 1.62 2017/09/15 14:19:06 roberto Exp roberto $ +** $Id: lopcodes.c,v 1.63 2017/09/19 18:38:14 roberto Exp roberto $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ @@ -101,18 +101,18 @@ LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = { ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_NEWTABLE */ ,opmode(0, 1, OpArgR, OpArgK, iABC) /* OP_SELF */ ,opmode(0, 1, OpArgR, OpArgU, iABC) /* OP_ADDI */ - ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_ADD */ - ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_SUB */ - ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_MUL */ - ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_MOD */ - ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_POW */ - ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_DIV */ - ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_IDIV */ - ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_BAND */ - ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_BOR */ - ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_BXOR */ - ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_SHL */ - ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_SHR */ + ,opmode(0, 1, OpArgR, OpArgR, iABC) /* OP_ADD */ + ,opmode(0, 1, OpArgR, OpArgR, iABC) /* OP_SUB */ + ,opmode(0, 1, OpArgR, OpArgR, iABC) /* OP_MUL */ + ,opmode(0, 1, OpArgR, OpArgR, iABC) /* OP_MOD */ + ,opmode(0, 1, OpArgR, OpArgR, iABC) /* OP_POW */ + ,opmode(0, 1, OpArgR, OpArgR, iABC) /* OP_DIV */ + ,opmode(0, 1, OpArgR, OpArgR, iABC) /* OP_IDIV */ + ,opmode(0, 1, OpArgR, OpArgR, iABC) /* OP_BAND */ + ,opmode(0, 1, OpArgR, OpArgR, iABC) /* OP_BOR */ + ,opmode(0, 1, OpArgR, OpArgR, iABC) /* OP_BXOR */ + ,opmode(0, 1, OpArgR, OpArgR, iABC) /* OP_SHL */ + ,opmode(0, 1, OpArgR, OpArgR, iABC) /* OP_SHR */ ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_UNM */ ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_BNOT */ ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_NOT */ @@ -120,9 +120,9 @@ LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = { ,opmode(0, 1, OpArgR, OpArgR, iABC) /* OP_CONCAT */ ,opmode(0, 0, OpArgN, OpArgN, iABC) /* OP_CLOSE */ ,opmode(0, 0, OpArgU, OpArgN, iAsBx) /* OP_JMP */ - ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_EQ */ - ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_LT */ - ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_LE */ + ,opmode(1, 0, OpArgR, OpArgR, iABC) /* OP_EQ */ + ,opmode(1, 0, OpArgR, OpArgR, iABC) /* OP_LT */ + ,opmode(1, 0, OpArgR, OpArgR, iABC) /* OP_LE */ ,opmode(1, 0, OpArgN, OpArgU, iABC) /* OP_TEST */ ,opmode(1, 1, OpArgR, OpArgU, iABC) /* OP_TESTSET */ ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_CALL */ diff --git a/lopcodes.h b/lopcodes.h index 9e0ce498a8..d28948d8eb 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -1,5 +1,5 @@ /* -** $Id: lopcodes.h,v 1.159 2017/09/18 16:07:54 roberto Exp roberto $ +** $Id: lopcodes.h,v 1.160 2017/09/19 18:38:14 roberto Exp roberto $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ @@ -198,18 +198,18 @@ OP_NEWTABLE,/* A B C R(A) := {} (size = B,C) */ OP_SELF,/* A B C R(A+1) := R(B); R(A) := R(B)[RK(C):string] */ OP_ADDI,/* A B C R(A) := R(B) + C */ -OP_ADD,/* A B C R(A) := RK(B) + RK(C) */ -OP_SUB,/* A B C R(A) := RK(B) - RK(C) */ -OP_MUL,/* A B C R(A) := RK(B) * RK(C) */ -OP_MOD,/* A B C R(A) := RK(B) % RK(C) */ -OP_POW,/* A B C R(A) := RK(B) ^ RK(C) */ -OP_DIV,/* A B C R(A) := RK(B) / RK(C) */ -OP_IDIV,/* A B C R(A) := RK(B) // RK(C) */ -OP_BAND,/* A B C R(A) := RK(B) & RK(C) */ -OP_BOR,/* A B C R(A) := RK(B) | RK(C) */ -OP_BXOR,/* A B C R(A) := RK(B) ~ RK(C) */ -OP_SHL,/* A B C R(A) := RK(B) << RK(C) */ -OP_SHR,/* A B C R(A) := RK(B) >> RK(C) */ +OP_ADD,/* A B C R(A) := R(B) + R(C) */ +OP_SUB,/* A B C R(A) := R(B) - R(C) */ +OP_MUL,/* A B C R(A) := R(B) * R(C) */ +OP_MOD,/* A B C R(A) := R(B) % R(C) */ +OP_POW,/* A B C R(A) := R(B) ^ R(C) */ +OP_DIV,/* A B C R(A) := R(B) / R(C) */ +OP_IDIV,/* A B C R(A) := R(B) // R(C) */ +OP_BAND,/* A B C R(A) := R(B) & R(C) */ +OP_BOR,/* A B C R(A) := R(B) | R(C) */ +OP_BXOR,/* A B C R(A) := R(B) ~ R(C) */ +OP_SHL,/* A B C R(A) := R(B) << R(C) */ +OP_SHR,/* A B C R(A) := R(B) >> R(C) */ OP_UNM,/* A B R(A) := -R(B) */ OP_BNOT,/* A B R(A) := ~R(B) */ OP_NOT,/* A B R(A) := not R(B) */ @@ -219,9 +219,9 @@ OP_CONCAT,/* A B C R(A) := R(B).. ... ..R(C) */ OP_CLOSE,/* A close all upvalues >= R(A) */ OP_JMP,/* sBx pc+=sBx */ -OP_EQ,/* A B C if ((RK(B) == RK(C)) ~= A) then pc++ */ -OP_LT,/* A B C if ((RK(B) < RK(C)) ~= A) then pc++ */ -OP_LE,/* A B C if ((RK(B) <= RK(C)) ~= A) then pc++ */ +OP_EQ,/* A B C if ((R(B) == R(C)) ~= A) then pc++ */ +OP_LT,/* A B C if ((R(B) < R(C)) ~= A) then pc++ */ +OP_LE,/* A B C if ((R(B) <= R(C)) ~= A) then pc++ */ OP_TEST,/* A C if not (R(A) <=> C) then pc++ */ OP_TESTSET,/* A B C if (R(B) <=> C) then R(A) := R(B) else pc++ */ diff --git a/lvm.c b/lvm.c index a41b44d63e..75eb8fe46a 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.292 2017/09/13 19:50:08 roberto Exp roberto $ +** $Id: lvm.c,v 2.293 2017/09/19 18:38:14 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -739,8 +739,6 @@ void luaV_finishOp (lua_State *L) { #define RC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgR, base+GETARG_C(i)) #define vRC(i) s2v(RC(i)) #define KC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgK, k+GETARG_C(i)) -#define RKB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgK, \ - (GETARG_Bk(i)) ? k + GETARG_Br(i) : s2v(base + GETARG_Br(i))) #define RKC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgK, \ (GETARG_Ck(i)) ? k + GETARG_Cr(i) : s2v(base + GETARG_Cr(i))) @@ -1013,8 +1011,8 @@ void luaV_execute (lua_State *L) { vmbreak; } vmcase(OP_ADD) { - TValue *rb = RKB(i); - TValue *rc = RKC(i); + TValue *rb = vRB(i); + TValue *rc = vRC(i); lua_Number nb; lua_Number nc; if (ttisinteger(rb) && ttisinteger(rc)) { lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc); @@ -1027,8 +1025,8 @@ void luaV_execute (lua_State *L) { vmbreak; } vmcase(OP_SUB) { - TValue *rb = RKB(i); - TValue *rc = RKC(i); + TValue *rb = vRB(i); + TValue *rc = vRC(i); lua_Number nb; lua_Number nc; if (ttisinteger(rb) && ttisinteger(rc)) { lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc); @@ -1041,8 +1039,8 @@ void luaV_execute (lua_State *L) { vmbreak; } vmcase(OP_MUL) { - TValue *rb = RKB(i); - TValue *rc = RKC(i); + TValue *rb = vRB(i); + TValue *rc = vRC(i); lua_Number nb; lua_Number nc; if (ttisinteger(rb) && ttisinteger(rc)) { lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc); @@ -1055,8 +1053,8 @@ void luaV_execute (lua_State *L) { vmbreak; } vmcase(OP_DIV) { /* float division (always with floats) */ - TValue *rb = RKB(i); - TValue *rc = RKC(i); + TValue *rb = vRB(i); + TValue *rc = vRC(i); lua_Number nb; lua_Number nc; if (tonumberns(rb, nb) && tonumberns(rc, nc)) { setfltvalue(s2v(ra), luai_numdiv(L, nb, nc)); @@ -1065,8 +1063,8 @@ void luaV_execute (lua_State *L) { vmbreak; } vmcase(OP_BAND) { - TValue *rb = RKB(i); - TValue *rc = RKC(i); + TValue *rb = vRB(i); + TValue *rc = vRC(i); lua_Integer ib; lua_Integer ic; if (tointeger(rb, &ib) && tointeger(rc, &ic)) { setivalue(s2v(ra), intop(&, ib, ic)); @@ -1075,8 +1073,8 @@ void luaV_execute (lua_State *L) { vmbreak; } vmcase(OP_BOR) { - TValue *rb = RKB(i); - TValue *rc = RKC(i); + TValue *rb = vRB(i); + TValue *rc = vRC(i); lua_Integer ib; lua_Integer ic; if (tointeger(rb, &ib) && tointeger(rc, &ic)) { setivalue(s2v(ra), intop(|, ib, ic)); @@ -1085,8 +1083,8 @@ void luaV_execute (lua_State *L) { vmbreak; } vmcase(OP_BXOR) { - TValue *rb = RKB(i); - TValue *rc = RKC(i); + TValue *rb = vRB(i); + TValue *rc = vRC(i); lua_Integer ib; lua_Integer ic; if (tointeger(rb, &ib) && tointeger(rc, &ic)) { setivalue(s2v(ra), intop(^, ib, ic)); @@ -1095,8 +1093,8 @@ void luaV_execute (lua_State *L) { vmbreak; } vmcase(OP_SHL) { - TValue *rb = RKB(i); - TValue *rc = RKC(i); + TValue *rb = vRB(i); + TValue *rc = vRC(i); lua_Integer ib; lua_Integer ic; if (tointeger(rb, &ib) && tointeger(rc, &ic)) { setivalue(s2v(ra), luaV_shiftl(ib, ic)); @@ -1105,8 +1103,8 @@ void luaV_execute (lua_State *L) { vmbreak; } vmcase(OP_SHR) { - TValue *rb = RKB(i); - TValue *rc = RKC(i); + TValue *rb = vRB(i); + TValue *rc = vRC(i); lua_Integer ib; lua_Integer ic; if (tointeger(rb, &ib) && tointeger(rc, &ic)) { setivalue(s2v(ra), luaV_shiftl(ib, -ic)); @@ -1115,8 +1113,8 @@ void luaV_execute (lua_State *L) { vmbreak; } vmcase(OP_MOD) { - TValue *rb = RKB(i); - TValue *rc = RKC(i); + TValue *rb = vRB(i); + TValue *rc = vRC(i); lua_Number nb; lua_Number nc; if (ttisinteger(rb) && ttisinteger(rc)) { lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc); @@ -1131,8 +1129,8 @@ void luaV_execute (lua_State *L) { vmbreak; } vmcase(OP_IDIV) { /* floor division */ - TValue *rb = RKB(i); - TValue *rc = RKC(i); + TValue *rb = vRB(i); + TValue *rc = vRC(i); lua_Number nb; lua_Number nc; if (ttisinteger(rb) && ttisinteger(rc)) { lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc); @@ -1145,8 +1143,8 @@ void luaV_execute (lua_State *L) { vmbreak; } vmcase(OP_POW) { - TValue *rb = RKB(i); - TValue *rc = RKC(i); + TValue *rb = vRB(i); + TValue *rc = vRC(i); lua_Number nb; lua_Number nc; if (tonumberns(rb, nb) && tonumberns(rc, nc)) { setfltvalue(s2v(ra), luai_numpow(L, nb, nc)); @@ -1212,8 +1210,8 @@ void luaV_execute (lua_State *L) { vmbreak; } vmcase(OP_EQ) { - TValue *rb = RKB(i); - TValue *rc = RKC(i); + TValue *rb = vRB(i); + TValue *rc = vRC(i); Protect( if (luaV_equalobj(L, rb, rc) != GETARG_A(i)) pc++; @@ -1223,8 +1221,8 @@ void luaV_execute (lua_State *L) { vmbreak; } vmcase(OP_LT) { - TValue *rb = RKB(i); - TValue *rc = RKC(i); + TValue *rb = vRB(i); + TValue *rc = vRC(i); int res; if (ttisinteger(rb) && ttisinteger(rc)) res = (ivalue(rb) < ivalue(rc)); @@ -1238,8 +1236,8 @@ void luaV_execute (lua_State *L) { vmbreak; } vmcase(OP_LE) { - TValue *rb = RKB(i); - TValue *rc = RKC(i); + TValue *rb = vRB(i); + TValue *rc = vRC(i); int res; if (ttisinteger(rb) && ttisinteger(rc)) res = (ivalue(rb) <= ivalue(rc)); From 1b100335839e13021b4731f0407b87e4f7544dc0 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 27 Sep 2017 15:59:08 -0300 Subject: [PATCH 0085/1145] new function 'luaT_trybiniTM' to handle tag methods for instructions with immediate integer arguments --- ltm.c | 14 +++++++++++++- ltm.h | 4 +++- lvm.c | 13 +++---------- 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/ltm.c b/ltm.c index cbc4cc5e93..2de75a313f 100644 --- a/ltm.c +++ b/ltm.c @@ -1,5 +1,5 @@ /* -** $Id: ltm.c,v 2.42 2017/06/29 15:06:44 roberto Exp roberto $ +** $Id: ltm.c,v 2.43 2017/07/27 13:50:16 roberto Exp roberto $ ** Tag methods ** See Copyright Notice in lua.h */ @@ -167,6 +167,18 @@ void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2, } +void luaT_trybiniTM (lua_State *L, const TValue *p1, int i2, + int inv, StkId res, TMS event) { + TValue aux; TValue *p2; + setivalue(&aux, i2); + if (inv) { /* arguments were exchanged? */ + p2 = p1; p1 = &aux; /* correct them */ + } + else p2 = &aux; + luaT_trybinTM(L, p1, p2, res, event); +} + + int luaT_callorderTM (lua_State *L, const TValue *p1, const TValue *p2, TMS event) { if (!callbinTM(L, p1, p2, L->top, event)) diff --git a/ltm.h b/ltm.h index c98b10378f..31454a5cac 100644 --- a/ltm.h +++ b/ltm.h @@ -1,5 +1,5 @@ /* -** $Id: ltm.h,v 2.24 2017/05/13 12:57:20 roberto Exp roberto $ +** $Id: ltm.h,v 2.25 2017/06/29 15:06:44 roberto Exp roberto $ ** Tag methods ** See Copyright Notice in lua.h */ @@ -68,6 +68,8 @@ LUAI_FUNC void luaT_callTMres (lua_State *L, const TValue *f, const TValue *p1, const TValue *p2, StkId p3); LUAI_FUNC void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2, StkId res, TMS event); +LUAI_FUNC void luaT_trybiniTM (lua_State *L, const TValue *p1, int i2, + int inv, StkId res, TMS event); LUAI_FUNC int luaT_callorderTM (lua_State *L, const TValue *p1, const TValue *p2, TMS event); diff --git a/lvm.c b/lvm.c index 75eb8fe46a..f34f3dfec1 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.293 2017/09/19 18:38:14 roberto Exp roberto $ +** $Id: lvm.c,v 2.294 2017/09/26 18:14:45 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -999,15 +999,8 @@ void luaV_execute (lua_State *L) { else if (tonumberns(rb, nb)) { setfltvalue(s2v(ra), luai_numadd(L, nb, cast_num(ic))); } - else { - TValue aux; TValue *rc; - setivalue(&aux, ic); - if (GETARG_Bk(i)) { /* arguments were exchanged? */ - rc = rb; rb = &aux; /* correct them */ - } - else rc = &aux; - Protect(luaT_trybinTM(L, rb, rc, ra, TM_ADD)); - } + else + Protect(luaT_trybiniTM(L, rb, ic, GETARG_Bk(i), ra, TM_ADD)); vmbreak; } vmcase(OP_ADD) { From 722bdbe17d0192baf72978f88069d12a921e9bfb Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 28 Sep 2017 13:53:29 -0300 Subject: [PATCH 0086/1145] no more 'getBMode'-'getCMode' (imprecise + we will need more space for op mode) + better control of op modes --- lcode.c | 18 ++++++--- lcode.h | 4 +- lopcodes.c | 116 ++++++++++++++++++++++++++--------------------------- lopcodes.h | 57 +++++++++++++------------- lparser.c | 4 +- lvm.c | 19 ++++----- 6 files changed, 109 insertions(+), 109 deletions(-) diff --git a/lcode.c b/lcode.c index 775a22c39e..1f6e9c11f8 100644 --- a/lcode.c +++ b/lcode.c @@ -1,5 +1,5 @@ /* -** $Id: lcode.c,v 2.124 2017/09/26 17:10:49 roberto Exp roberto $ +** $Id: lcode.c,v 2.125 2017/09/26 18:14:45 roberto Exp roberto $ ** Code generator for Lua ** See Copyright Notice in lua.h */ @@ -367,8 +367,6 @@ static int luaK_code (FuncState *fs, Instruction i) { */ int luaK_codeABC (FuncState *fs, OpCode o, int a, int b, int c) { lua_assert(getOpMode(o) == iABC); - lua_assert(getBMode(o) != OpArgN || b == 0); - lua_assert(getCMode(o) != OpArgN || c == 0); lua_assert(a <= MAXARG_A && b <= MAXARG_B && c <= MAXARG_C); return luaK_code(fs, CREATE_ABC(o, a, b, c)); } @@ -378,13 +376,23 @@ int luaK_codeABC (FuncState *fs, OpCode o, int a, int b, int c) { ** Format and emit an 'iABx' instruction. */ int luaK_codeABx (FuncState *fs, OpCode o, int a, unsigned int bc) { - lua_assert(getOpMode(o) == iABx || getOpMode(o) == iAsBx); - lua_assert(getCMode(o) == OpArgN); + lua_assert(getOpMode(o) == iABx); lua_assert(a <= MAXARG_A && bc <= MAXARG_Bx); return luaK_code(fs, CREATE_ABx(o, a, bc)); } +/* +** Format and emit an 'iAsBx' instruction. +*/ +int luaK_codeAsBx (FuncState *fs, OpCode o, int a, int bc) { + unsigned int b = bc + MAXARG_sBx; + lua_assert(getOpMode(o) == iAsBx); + lua_assert(a <= MAXARG_A && b <= MAXARG_Bx); + return luaK_code(fs, CREATE_ABx(o, a, b)); +} + + /* ** Emit an "extra argument" instruction (format 'iAx') */ diff --git a/lcode.h b/lcode.h index 70953559a4..e7f048fd15 100644 --- a/lcode.h +++ b/lcode.h @@ -1,5 +1,5 @@ /* -** $Id: lcode.h,v 1.65 2017/04/20 19:53:55 roberto Exp roberto $ +** $Id: lcode.h,v 1.66 2017/09/13 19:50:08 roberto Exp roberto $ ** Code generator for Lua ** See Copyright Notice in lua.h */ @@ -43,13 +43,13 @@ typedef enum UnOpr { OPR_MINUS, OPR_BNOT, OPR_NOT, OPR_LEN, OPR_NOUNOPR } UnOpr; /* get (pointer to) instruction of given 'expdesc' */ #define getinstruction(fs,e) ((fs)->f->code[(e)->u.info]) -#define luaK_codeAsBx(fs,o,A,sBx) luaK_codeABx(fs,o,A,(sBx)+MAXARG_sBx) #define luaK_setmultret(fs,e) luaK_setreturns(fs, e, LUA_MULTRET) #define luaK_jumpto(fs,t) luaK_patchlist(fs, luaK_jump(fs), t) LUAI_FUNC int luaK_codeABx (FuncState *fs, OpCode o, int A, unsigned int Bx); +LUAI_FUNC int luaK_codeAsBx (FuncState *fs, OpCode o, int A, int Bx); LUAI_FUNC int luaK_codeABC (FuncState *fs, OpCode o, int A, int B, int C); LUAI_FUNC void luaK_fixline (FuncState *fs, int line); LUAI_FUNC void luaK_nil (FuncState *fs, int from, int n); diff --git a/lopcodes.c b/lopcodes.c index 9617b7202d..5051f2a87b 100644 --- a/lopcodes.c +++ b/lopcodes.c @@ -1,5 +1,5 @@ /* -** $Id: lopcodes.c,v 1.63 2017/09/19 18:38:14 roberto Exp roberto $ +** $Id: lopcodes.c,v 1.64 2017/09/26 18:14:45 roberto Exp roberto $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ @@ -77,64 +77,62 @@ LUAI_DDEF const char *const luaP_opnames[NUM_OPCODES+1] = { }; -#define opmode(t,a,b,c,m) (((t)<<7) | ((a)<<6) | ((b)<<4) | ((c)<<2) | (m)) - LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = { -/* T A B C mode opcode */ - opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_MOVE */ - ,opmode(0, 1, OpArgU, OpArgN, iAsBx) /* OP_LOADI */ - ,opmode(0, 1, OpArgU, OpArgN, iAsBx) /* OP_LOADF */ - ,opmode(0, 1, OpArgK, OpArgN, iABx) /* OP_LOADK */ - ,opmode(0, 1, OpArgN, OpArgN, iABx) /* OP_LOADKX */ - ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_LOADBOOL */ - ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_LOADNIL */ - ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_GETUPVAL */ - ,opmode(0, 0, OpArgU, OpArgN, iABC) /* OP_SETUPVAL */ - ,opmode(0, 1, OpArgU, OpArgK, iABC) /* OP_GETTABUP */ - ,opmode(0, 1, OpArgR, OpArgR, iABC) /* OP_GETTABLE */ - ,opmode(0, 1, OpArgR, OpArgU, iABC) /* OP_GETI */ - ,opmode(0, 1, OpArgR, OpArgK, iABC) /* OP_GETFIELD */ - ,opmode(0, 0, OpArgK, OpArgK, iABC) /* OP_SETTABUP */ - ,opmode(0, 0, OpArgR, OpArgK, iABC) /* OP_SETTABLE */ - ,opmode(0, 0, OpArgU, OpArgK, iABC) /* OP_SETI */ - ,opmode(0, 0, OpArgK, OpArgK, iABC) /* OP_SETFIELD */ - ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_NEWTABLE */ - ,opmode(0, 1, OpArgR, OpArgK, iABC) /* OP_SELF */ - ,opmode(0, 1, OpArgR, OpArgU, iABC) /* OP_ADDI */ - ,opmode(0, 1, OpArgR, OpArgR, iABC) /* OP_ADD */ - ,opmode(0, 1, OpArgR, OpArgR, iABC) /* OP_SUB */ - ,opmode(0, 1, OpArgR, OpArgR, iABC) /* OP_MUL */ - ,opmode(0, 1, OpArgR, OpArgR, iABC) /* OP_MOD */ - ,opmode(0, 1, OpArgR, OpArgR, iABC) /* OP_POW */ - ,opmode(0, 1, OpArgR, OpArgR, iABC) /* OP_DIV */ - ,opmode(0, 1, OpArgR, OpArgR, iABC) /* OP_IDIV */ - ,opmode(0, 1, OpArgR, OpArgR, iABC) /* OP_BAND */ - ,opmode(0, 1, OpArgR, OpArgR, iABC) /* OP_BOR */ - ,opmode(0, 1, OpArgR, OpArgR, iABC) /* OP_BXOR */ - ,opmode(0, 1, OpArgR, OpArgR, iABC) /* OP_SHL */ - ,opmode(0, 1, OpArgR, OpArgR, iABC) /* OP_SHR */ - ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_UNM */ - ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_BNOT */ - ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_NOT */ - ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_LEN */ - ,opmode(0, 1, OpArgR, OpArgR, iABC) /* OP_CONCAT */ - ,opmode(0, 0, OpArgN, OpArgN, iABC) /* OP_CLOSE */ - ,opmode(0, 0, OpArgU, OpArgN, iAsBx) /* OP_JMP */ - ,opmode(1, 0, OpArgR, OpArgR, iABC) /* OP_EQ */ - ,opmode(1, 0, OpArgR, OpArgR, iABC) /* OP_LT */ - ,opmode(1, 0, OpArgR, OpArgR, iABC) /* OP_LE */ - ,opmode(1, 0, OpArgN, OpArgU, iABC) /* OP_TEST */ - ,opmode(1, 1, OpArgR, OpArgU, iABC) /* OP_TESTSET */ - ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_CALL */ - ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_TAILCALL */ - ,opmode(0, 0, OpArgU, OpArgN, iABC) /* OP_RETURN */ - ,opmode(0, 1, OpArgR, OpArgN, iABx) /* OP_FORLOOP */ - ,opmode(0, 1, OpArgR, OpArgN, iABx) /* OP_FORPREP */ - ,opmode(0, 0, OpArgN, OpArgU, iABC) /* OP_TFORCALL */ - ,opmode(0, 1, OpArgR, OpArgN, iABx) /* OP_TFORLOOP */ - ,opmode(0, 0, OpArgU, OpArgU, iABC) /* OP_SETLIST */ - ,opmode(0, 1, OpArgU, OpArgN, iABx) /* OP_CLOSURE */ - ,opmode(0, 1, OpArgU, OpArgR, iABC) /* OP_VARARG */ - ,opmode(0, 0, OpArgU, OpArgU, iAx) /* OP_EXTRAARG */ +/* T A mode opcode */ + opmode(0, 1, iABC) /* OP_MOVE */ + ,opmode(0, 1, iAsBx) /* OP_LOADI */ + ,opmode(0, 1, iAsBx) /* OP_LOADF */ + ,opmode(0, 1, iABx) /* OP_LOADK */ + ,opmode(0, 1, iABx) /* OP_LOADKX */ + ,opmode(0, 1, iABC) /* OP_LOADBOOL */ + ,opmode(0, 1, iABC) /* OP_LOADNIL */ + ,opmode(0, 1, iABC) /* OP_GETUPVAL */ + ,opmode(0, 0, iABC) /* OP_SETUPVAL */ + ,opmode(0, 1, iABC) /* OP_GETTABUP */ + ,opmode(0, 1, iABC) /* OP_GETTABLE */ + ,opmode(0, 1, iABC) /* OP_GETI */ + ,opmode(0, 1, iABC) /* OP_GETFIELD */ + ,opmode(0, 0, iABC) /* OP_SETTABUP */ + ,opmode(0, 0, iABC) /* OP_SETTABLE */ + ,opmode(0, 0, iABC) /* OP_SETI */ + ,opmode(0, 0, iABC) /* OP_SETFIELD */ + ,opmode(0, 1, iABC) /* OP_NEWTABLE */ + ,opmode(0, 1, iABC) /* OP_SELF */ + ,opmode(0, 1, iABC) /* OP_ADDI */ + ,opmode(0, 1, iABC) /* OP_ADD */ + ,opmode(0, 1, iABC) /* OP_SUB */ + ,opmode(0, 1, iABC) /* OP_MUL */ + ,opmode(0, 1, iABC) /* OP_MOD */ + ,opmode(0, 1, iABC) /* OP_POW */ + ,opmode(0, 1, iABC) /* OP_DIV */ + ,opmode(0, 1, iABC) /* OP_IDIV */ + ,opmode(0, 1, iABC) /* OP_BAND */ + ,opmode(0, 1, iABC) /* OP_BOR */ + ,opmode(0, 1, iABC) /* OP_BXOR */ + ,opmode(0, 1, iABC) /* OP_SHL */ + ,opmode(0, 1, iABC) /* OP_SHR */ + ,opmode(0, 1, iABC) /* OP_UNM */ + ,opmode(0, 1, iABC) /* OP_BNOT */ + ,opmode(0, 1, iABC) /* OP_NOT */ + ,opmode(0, 1, iABC) /* OP_LEN */ + ,opmode(0, 1, iABC) /* OP_CONCAT */ + ,opmode(0, 0, iABC) /* OP_CLOSE */ + ,opmode(0, 0, iAsBx) /* OP_JMP */ + ,opmode(1, 0, iABC) /* OP_EQ */ + ,opmode(1, 0, iABC) /* OP_LT */ + ,opmode(1, 0, iABC) /* OP_LE */ + ,opmode(1, 0, iABC) /* OP_TEST */ + ,opmode(1, 1, iABC) /* OP_TESTSET */ + ,opmode(0, 1, iABC) /* OP_CALL */ + ,opmode(0, 1, iABC) /* OP_TAILCALL */ + ,opmode(0, 0, iABC) /* OP_RETURN */ + ,opmode(0, 1, iABx) /* OP_FORLOOP */ + ,opmode(0, 1, iABx) /* OP_FORPREP */ + ,opmode(0, 0, iABC) /* OP_TFORCALL */ + ,opmode(0, 1, iABx) /* OP_TFORLOOP */ + ,opmode(0, 0, iABC) /* OP_SETLIST */ + ,opmode(0, 1, iABx) /* OP_CLOSURE */ + ,opmode(0, 1, iABC) /* OP_VARARG */ + ,opmode(0, 0, iAx) /* OP_EXTRAARG */ }; diff --git a/lopcodes.h b/lopcodes.h index d28948d8eb..6b9c3aebbc 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -1,5 +1,5 @@ /* -** $Id: lopcodes.h,v 1.160 2017/09/19 18:38:14 roberto Exp roberto $ +** $Id: lopcodes.h,v 1.161 2017/09/26 18:14:45 roberto Exp roberto $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ @@ -89,6 +89,9 @@ enum OpMode {iABC, iABx, iAsBx, iAx}; /* basic instruction format */ #define SET_OPCODE(i,o) ((i) = (((i)&MASK0(SIZE_OP,POS_OP)) | \ ((cast(Instruction, o)<>(pos)) & MASK1(size,0))) #define setarg(i,v,pos,size) ((i) = (((i)&MASK0(size,pos)) | \ ((cast(Instruction, v)<> 4) & 3)) -#define getCMode(m) (cast(enum OpArgMask, (luaP_opmodes[m] >> 2) & 3)) -#define testAMode(m) (luaP_opmodes[m] & (1 << 6)) -#define testTMode(m) (luaP_opmodes[m] & (1 << 7)) +#define getOpMode(m) (cast(enum OpMode, luaP_opmodes[m] & 7)) +#define testAMode(m) (luaP_opmodes[m] & (1 << 3)) +#define testTMode(m) (luaP_opmodes[m] & (1 << 4)) + +#define opmode(t,a,m) (((t)<<4) | ((a)<<3) | (m)) LUAI_DDEC const char *const luaP_opnames[NUM_OPCODES+1]; /* opcode names */ diff --git a/lparser.c b/lparser.c index 7fd62f507a..1831ea01e8 100644 --- a/lparser.c +++ b/lparser.c @@ -1,5 +1,5 @@ /* -** $Id: lparser.c,v 2.164 2017/08/14 18:33:14 roberto Exp roberto $ +** $Id: lparser.c,v 2.165 2017/09/13 19:50:08 roberto Exp roberto $ ** Lua Parser ** See Copyright Notice in lua.h */ @@ -1373,7 +1373,7 @@ static void forbody (LexState *ls, int base, int line, int nvars, int isnum) { luaK_patchtohere(fs, prep); luaK_codeABC(fs, OP_TFORCALL, base, 0, nvars); luaK_fixline(fs, line); - endfor = luaK_codeAsBx(fs, OP_TFORLOOP, base + 2, NO_JUMP); + endfor = luaK_codeABx(fs, OP_TFORLOOP, base + 2, 0); } fixforjump(fs, endfor, prep + 1, 1); luaK_fixline(fs, line); diff --git a/lvm.c b/lvm.c index f34f3dfec1..fd02ad5bc0 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.294 2017/09/26 18:14:45 roberto Exp roberto $ +** $Id: lvm.c,v 2.295 2017/09/27 18:59:08 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -733,14 +733,13 @@ void luaV_finishOp (lua_State *L) { #define RA(i) (base+GETARG_A(i)) -#define RB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgR, base+GETARG_Br(i)) +#define RB(i) (base+GETARG_Br(i)) #define vRB(i) s2v(RB(i)) -#define KB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgK, k+GETARG_B(i)) -#define RC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgR, base+GETARG_C(i)) +#define KB(i) (k+GETARG_B(i)) +#define RC(i) (base+GETARG_C(i)) #define vRC(i) s2v(RC(i)) -#define KC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgK, k+GETARG_C(i)) -#define RKC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgK, \ - (GETARG_Ck(i)) ? k + GETARG_Cr(i) : s2v(base + GETARG_Cr(i))) +#define KC(i) (k+GETARG_C(i)) +#define RKC(i) ((GETARG_Ck(i)) ? k + GETARG_Cr(i) : s2v(base + GETARG_Cr(i))) @@ -834,8 +833,7 @@ void luaV_execute (lua_State *L) { } vmcase(OP_LOADKX) { TValue *rb; - lua_assert(GET_OPCODE(*pc) == OP_EXTRAARG); - rb = k + GETARG_Ax(*pc++); + rb = k + GETARG_Ax(*pc); pc++; setobj2s(L, ra, rb); vmbreak; } @@ -1409,8 +1407,7 @@ void luaV_execute (lua_State *L) { Table *h; if (n == 0) n = cast_int(L->top - ra) - 1; if (c == 0) { - lua_assert(GET_OPCODE(*pc) == OP_EXTRAARG); - c = GETARG_Ax(*pc++); + c = GETARG_Ax(*pc); pc++; } h = hvalue(s2v(ra)); last = ((c-1)*LFIELDS_PER_FLUSH) + n; From bc1b0733b808f02639ed79fff02f69f731636539 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Sun, 1 Oct 2017 16:13:43 -0300 Subject: [PATCH 0087/1145] avoid the use of bit 'Bk' ('B' will lose this bit soon) --- lcode.c | 8 ++++---- lopcodes.h | 9 +++------ lvm.c | 8 ++++---- 3 files changed, 11 insertions(+), 14 deletions(-) diff --git a/lcode.c b/lcode.c index 1f6e9c11f8..1331a54ec4 100644 --- a/lcode.c +++ b/lcode.c @@ -1,5 +1,5 @@ /* -** $Id: lcode.c,v 2.125 2017/09/26 18:14:45 roberto Exp roberto $ +** $Id: lcode.c,v 2.126 2017/09/28 16:53:29 roberto Exp roberto $ ** Code generator for Lua ** See Copyright Notice in lua.h */ @@ -1068,7 +1068,7 @@ static int isKstr (FuncState *fs, expdesc *e) { */ static int isKint (expdesc *e) { return (e->k == VKINT && !hasjumps(e) && - l_castS2U(e->u.ival) <= l_castS2U(MAXARG_C)); + l_castS2U(e->u.ival) <= l_castS2U(MAXARG_Cr)); } @@ -1178,8 +1178,8 @@ static void codebinexpval (FuncState *fs, OpCode op, v1 = luaK_exp2anyreg(fs, e1); } else { /* exchange operands to make 2nd one a constant */ - v2 = cast_int(e1->u.ival); - v1 = luaK_exp2anyreg(fs, e2) | BITRK; /* K bit signal the exchange */ + v2 = cast_int(e1->u.ival) | BITRK; /* K bit signal the exchange */ + v1 = luaK_exp2anyreg(fs, e2); } op = OP_ADDI; } diff --git a/lopcodes.h b/lopcodes.h index 6b9c3aebbc..5aa75b16eb 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -1,5 +1,5 @@ /* -** $Id: lopcodes.h,v 1.161 2017/09/26 18:14:45 roberto Exp roberto $ +** $Id: lopcodes.h,v 1.162 2017/09/28 16:53:29 roberto Exp roberto $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ @@ -73,6 +73,7 @@ enum OpMode {iABC, iABx, iAsBx, iAx}; /* basic instruction format */ #define MAXARG_A ((1< Date: Sun, 1 Oct 2017 16:17:51 -0300 Subject: [PATCH 0088/1145] new function 'printcode' --- ltests.c | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/ltests.c b/ltests.c index 4388e1c9d6..b45186048c 100644 --- a/ltests.c +++ b/ltests.c @@ -1,5 +1,5 @@ /* -** $Id: ltests.c,v 2.222 2017/06/27 18:32:49 roberto Exp roberto $ +** $Id: ltests.c,v 2.223 2017/06/29 15:06:44 roberto Exp roberto $ ** Internal Module for Debugging of the Lua Implementation ** See Copyright Notice in lua.h */ @@ -595,6 +595,22 @@ static int listcode (lua_State *L) { } +static int printcode (lua_State *L) { + int pc; + Proto *p; + luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1), + 1, "Lua function expected"); + p = getproto(obj_at(L, 1)); + printf("maxstack: %d\n", p->maxstacksize); + printf("numparams: %d\n", p->numparams); + for (pc=0; pcsizecode; pc++) { + char buff[100]; + printf("%d\t%s\n", pc + 1, buildop(p, pc, buff)); + } + return 0; +} + + static int listk (lua_State *L) { Proto *p; int i; @@ -1634,6 +1650,7 @@ static const struct luaL_Reg tests_funcs[] = { {"log2", log2_aux}, {"limits", get_limits}, {"listcode", listcode}, + {"printcode", printcode}, {"listk", listk}, {"listlocals", listlocals}, {"loadlib", loadlib}, From f3b3d9b5c2e014148fad830b6762efb57fdcb24b Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 2 Oct 2017 19:50:57 -0300 Subject: [PATCH 0089/1145] string constants (Kstr) must fit into 'B' register --- lcode.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lcode.c b/lcode.c index 1331a54ec4..82bcdcbb48 100644 --- a/lcode.c +++ b/lcode.c @@ -1,5 +1,5 @@ /* -** $Id: lcode.c,v 2.126 2017/09/28 16:53:29 roberto Exp roberto $ +** $Id: lcode.c,v 2.127 2017/10/01 19:13:43 roberto Exp roberto $ ** Code generator for Lua ** See Copyright Notice in lua.h */ @@ -1057,7 +1057,7 @@ static void codenot (FuncState *fs, expdesc *e) { ** Check whether expression 'e' is a small literal string */ static int isKstr (FuncState *fs, expdesc *e) { - return (e->k == VK && !hasjumps(e) && e->u.info <= MAXARG_C && + return (e->k == VK && !hasjumps(e) && e->u.info <= MAXARG_B && ttisshrstring(&fs->f->k[e->u.info])); } From 940472c75cf8f1a31a267748e3164f539e289b54 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 2 Oct 2017 19:51:32 -0300 Subject: [PATCH 0090/1145] opcode size increased to 7 bits --- lopcodes.h | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lopcodes.h b/lopcodes.h index 5aa75b16eb..5c0bb8366d 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -1,5 +1,5 @@ /* -** $Id: lopcodes.h,v 1.162 2017/09/28 16:53:29 roberto Exp roberto $ +** $Id: lopcodes.h,v 1.163 2017/10/01 19:13:43 roberto Exp roberto $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ @@ -12,15 +12,15 @@ /*=========================================================================== We assume that instructions are unsigned 32-bit integers. - All instructions have an opcode in the first 6 bits. + All instructions have an opcode in the first 7 bits. Instructions can have the following formats: 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 -iABC | C(9) | | B(9) | | A(8) | | Op(6) | -iABx | Bx(18) | | A(8) | | Op(6) | -iAsBx | sBx (signed)(18) | | A(8) | | Op(6) | -iAx | Ax(26) | | Op(6) | +iABC | C(9) | | B(8) | | A(8) | | Op(7) | +iABx | Bx(17) | | A(8) | | Op(7) | +iAsBx | sBx (signed)(17) | | A(8) | | Op(7) | +iAx | Ax(25) | | Op(7) | A signed argument is represented in excess K: the represented value is the written unsigned value minus K, where K is half the maximum for the @@ -35,12 +35,12 @@ enum OpMode {iABC, iABx, iAsBx, iAx}; /* basic instruction format */ ** size and position of opcode arguments. */ #define SIZE_C 9 -#define SIZE_B 9 +#define SIZE_B 8 #define SIZE_Bx (SIZE_C + SIZE_B) #define SIZE_A 8 #define SIZE_Ax (SIZE_C + SIZE_B + SIZE_A) -#define SIZE_OP 6 +#define SIZE_OP 7 #define POS_OP 0 #define POS_A (POS_OP + SIZE_OP) From 9ed9f40f1e3674f9ed7ffbe73c0c9718782fe6cd Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 4 Oct 2017 12:49:05 -0300 Subject: [PATCH 0091/1145] avoid warning about 'const' --- ltm.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ltm.c b/ltm.c index 2de75a313f..1534992b37 100644 --- a/ltm.c +++ b/ltm.c @@ -1,5 +1,5 @@ /* -** $Id: ltm.c,v 2.43 2017/07/27 13:50:16 roberto Exp roberto $ +** $Id: ltm.c,v 2.44 2017/09/27 18:59:08 roberto Exp roberto $ ** Tag methods ** See Copyright Notice in lua.h */ @@ -169,7 +169,7 @@ void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2, void luaT_trybiniTM (lua_State *L, const TValue *p1, int i2, int inv, StkId res, TMS event) { - TValue aux; TValue *p2; + TValue aux; const TValue *p2; setivalue(&aux, i2); if (inv) { /* arguments were exchanged? */ p2 = p1; p1 = &aux; /* correct them */ From 8fbe9e3470295e3b70273fa32ef56459cc0b5201 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 4 Oct 2017 12:49:24 -0300 Subject: [PATCH 0092/1145] new opcodes with immediate integer operand for all arithmetic operations --- lcode.c | 71 ++++++++++++++++++++++++++++++------------ ldebug.c | 13 +++++--- lopcodes.c | 14 ++++++++- lopcodes.h | 9 +++++- lvm.c | 91 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 5 files changed, 170 insertions(+), 28 deletions(-) diff --git a/lcode.c b/lcode.c index 82bcdcbb48..786fd3421b 100644 --- a/lcode.c +++ b/lcode.c @@ -1,5 +1,5 @@ /* -** $Id: lcode.c,v 2.127 2017/10/01 19:13:43 roberto Exp roberto $ +** $Id: lcode.c,v 2.128 2017/10/02 22:50:57 roberto Exp roberto $ ** Code generator for Lua ** See Copyright Notice in lua.h */ @@ -1171,22 +1171,8 @@ static void codeunexpval (FuncState *fs, OpCode op, expdesc *e, int line) { */ static void codebinexpval (FuncState *fs, OpCode op, expdesc *e1, expdesc *e2, int line) { - int v1, v2; - if (op == OP_ADD && (isKint(e1) || isKint(e2))) { - if (isKint(e2)) { - v2 = cast_int(e2->u.ival); - v1 = luaK_exp2anyreg(fs, e1); - } - else { /* exchange operands to make 2nd one a constant */ - v2 = cast_int(e1->u.ival) | BITRK; /* K bit signal the exchange */ - v1 = luaK_exp2anyreg(fs, e2); - } - op = OP_ADDI; - } - else { - v2 = luaK_exp2anyreg(fs, e2); /* both operands are in registers */ - v1 = luaK_exp2anyreg(fs, e1); - } + int v2 = luaK_exp2anyreg(fs, e2); /* both operands are in registers */ + int v1 = luaK_exp2anyreg(fs, e1); freeexps(fs, e1, e2); e1->u.info = luaK_codeABC(fs, op, 0, v1, v2); /* generate opcode */ e1->k = VRELOCABLE; /* all those operations are relocatable */ @@ -1194,6 +1180,44 @@ static void codebinexpval (FuncState *fs, OpCode op, } +/* +** Code arithmetic operators ('+', '-', ...). If second operand is a +** constant in the proper range, use variant opcodes with immediate +** operands. +*/ +static void codearith (FuncState *fs, OpCode op, + expdesc *e1, expdesc *e2, int flip, int line) { + if (!isKint(e2)) + codebinexpval(fs, op, e1, e2, line); /* use standard operators */ + else { /* use immediate operators */ + int v2 = cast_int(e2->u.ival); /* immediate operand */ + int v1 = luaK_exp2anyreg(fs, e1); + if (flip) + v2 |= BITRK; /* signal that operands were flipped */ + op = cast(OpCode, op - OP_ADD + OP_ADDI); + freeexp(fs, e1); + e1->u.info = luaK_codeABC(fs, op, 0, v1, v2); /* generate opcode */ + e1->k = VRELOCABLE; /* all those operations are relocatable */ + luaK_fixline(fs, line); + } +} + + +/* +** Code commutative operators ('+', '*'). If first operand is a +** constant, change order of operands to use immediate operator. +*/ +static void codecommutative (FuncState *fs, OpCode op, + expdesc *e1, expdesc *e2, int line) { + int flip = 0; + if (isKint(e1)) { + expdesc temp = *e1; *e1 = *e2; *e2 = temp; /* swap 'e1' and 'e2' */ + flip = 1; + } + codearith(fs, op, e1, e2, flip, line); +} + + /* ** Emit code for comparisons. ** 'e1' was already put in register by 'luaK_infix'. @@ -1318,8 +1342,17 @@ void luaK_posfix (FuncState *fs, BinOpr op, } break; } - case OPR_ADD: case OPR_SUB: case OPR_MUL: case OPR_DIV: - case OPR_IDIV: case OPR_MOD: case OPR_POW: + case OPR_ADD: case OPR_MUL: { + if (!constfolding(fs, op + LUA_OPADD, e1, e2)) + codecommutative(fs, cast(OpCode, op + OP_ADD), e1, e2, line); + break; + } + case OPR_SUB: case OPR_DIV: + case OPR_IDIV: case OPR_MOD: case OPR_POW: { + if (!constfolding(fs, op + LUA_OPADD, e1, e2)) + codearith(fs, cast(OpCode, op + OP_ADD), e1, e2, 0, line); + break; + } case OPR_BAND: case OPR_BOR: case OPR_BXOR: case OPR_SHL: case OPR_SHR: { if (!constfolding(fs, op + LUA_OPADD, e1, e2)) diff --git a/ldebug.c b/ldebug.c index fbb3839d19..67b9496afc 100644 --- a/ldebug.c +++ b/ldebug.c @@ -1,5 +1,5 @@ /* -** $Id: ldebug.c,v 2.129 2017/07/07 16:34:32 roberto Exp roberto $ +** $Id: ldebug.c,v 2.130 2017/07/10 17:35:12 roberto Exp roberto $ ** Debug Interface ** See Copyright Notice in lua.h */ @@ -592,14 +592,17 @@ static const char *funcnamefromcode (lua_State *L, CallInfo *ci, case OP_SETTABUP: case OP_SETTABLE: case OP_SETI: case OP_SETFIELD: tm = TM_NEWINDEX; break; - case OP_ADDI: - tm = TM_ADD; + case OP_ADDI: case OP_SUBI: case OP_MULI: case OP_MODI: + case OP_POWI: case OP_DIVI: case OP_IDIVI: { + int offset = GET_OPCODE(i) - OP_ADDI; /* ORDER OP */ + tm = cast(TMS, offset + TM_ADD); /* ORDER TM */ break; + } case OP_ADD: case OP_SUB: case OP_MUL: case OP_MOD: case OP_POW: case OP_DIV: case OP_IDIV: case OP_BAND: case OP_BOR: case OP_BXOR: case OP_SHL: case OP_SHR: { - int offset = cast_int(GET_OPCODE(i)) - cast_int(OP_ADD); /* ORDER OP */ - tm = cast(TMS, offset + cast_int(TM_ADD)); /* ORDER TM */ + int offset = GET_OPCODE(i) - OP_ADD; /* ORDER OP */ + tm = cast(TMS, offset + TM_ADD); /* ORDER TM */ break; } case OP_UNM: tm = TM_UNM; break; diff --git a/lopcodes.c b/lopcodes.c index 5051f2a87b..42ce73d902 100644 --- a/lopcodes.c +++ b/lopcodes.c @@ -1,5 +1,5 @@ /* -** $Id: lopcodes.c,v 1.64 2017/09/26 18:14:45 roberto Exp roberto $ +** $Id: lopcodes.c,v 1.65 2017/09/28 16:53:29 roberto Exp roberto $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ @@ -38,6 +38,12 @@ LUAI_DDEF const char *const luaP_opnames[NUM_OPCODES+1] = { "NEWTABLE", "SELF", "ADDI", + "SUBI", + "MULI", + "MODI", + "POWI", + "DIVI", + "IDIVI", "ADD", "SUB", "MUL", @@ -99,6 +105,12 @@ LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = { ,opmode(0, 1, iABC) /* OP_NEWTABLE */ ,opmode(0, 1, iABC) /* OP_SELF */ ,opmode(0, 1, iABC) /* OP_ADDI */ + ,opmode(0, 1, iABC) /* OP_SUBI */ + ,opmode(0, 1, iABC) /* OP_MULI */ + ,opmode(0, 1, iABC) /* OP_MODI */ + ,opmode(0, 1, iABC) /* OP_POWI */ + ,opmode(0, 1, iABC) /* OP_DIVI */ + ,opmode(0, 1, iABC) /* OP_IDIVI */ ,opmode(0, 1, iABC) /* OP_ADD */ ,opmode(0, 1, iABC) /* OP_SUB */ ,opmode(0, 1, iABC) /* OP_MUL */ diff --git a/lopcodes.h b/lopcodes.h index 5c0bb8366d..39f36aa70d 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -1,5 +1,5 @@ /* -** $Id: lopcodes.h,v 1.163 2017/10/01 19:13:43 roberto Exp roberto $ +** $Id: lopcodes.h,v 1.164 2017/10/02 22:51:32 roberto Exp roberto $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ @@ -201,6 +201,13 @@ OP_NEWTABLE,/* A B C R(A) := {} (size = B,C) */ OP_SELF,/* A B C R(A+1) := R(B); R(A) := R(B)[RK(C):string] */ OP_ADDI,/* A B C R(A) := R(B) + C */ +OP_SUBI,/* A B C R(A) := R(B) - C */ +OP_MULI,/* A B C R(A) := R(B) * C */ +OP_MODI,/* A B C R(A) := R(B) % C */ +OP_POWI,/* A B C R(A) := R(B) ^ C */ +OP_DIVI,/* A B C R(A) := R(B) / C */ +OP_IDIVI,/* A B C R(A) := R(B) // C */ + OP_ADD,/* A B C R(A) := R(B) + R(C) */ OP_SUB,/* A B C R(A) := R(B) - R(C) */ OP_MUL,/* A B C R(A) := R(B) * R(C) */ diff --git a/lvm.c b/lvm.c index b534f0a01d..9c2b1345ec 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.296 2017/09/28 16:53:29 roberto Exp roberto $ +** $Id: lvm.c,v 2.297 2017/10/01 19:13:43 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -663,7 +663,10 @@ void luaV_finishOp (lua_State *L) { Instruction inst = *(ci->u.l.savedpc - 1); /* interrupted instruction */ OpCode op = GET_OPCODE(inst); switch (op) { /* finish its execution */ - case OP_ADDI: case OP_ADD: case OP_SUB: + case OP_ADDI: case OP_SUBI: + case OP_MULI: case OP_DIVI: case OP_IDIVI: + case OP_MODI: case OP_POWI: + case OP_ADD: case OP_SUB: case OP_MUL: case OP_DIV: case OP_IDIV: case OP_BAND: case OP_BOR: case OP_BXOR: case OP_SHL: case OP_SHR: case OP_MOD: case OP_POW: @@ -1001,6 +1004,90 @@ void luaV_execute (lua_State *L) { Protect(luaT_trybiniTM(L, rb, ic, GETARG_Ck(i), ra, TM_ADD)); vmbreak; } + vmcase(OP_SUBI) { + TValue *rb = vRB(i); + int ic = GETARG_Cr(i); + lua_Number nb; + if (ttisinteger(rb)) { + setivalue(s2v(ra), intop(-, ivalue(rb), ic)); + } + else if (tonumberns(rb, nb)) { + setfltvalue(s2v(ra), luai_numsub(L, nb, cast_num(ic))); + } + else + Protect(luaT_trybiniTM(L, rb, ic, 0, ra, TM_SUB)); + vmbreak; + } + vmcase(OP_MULI) { + TValue *rb = vRB(i); + int ic = GETARG_Cr(i); + lua_Number nb; + if (ttisinteger(rb)) { + setivalue(s2v(ra), intop(*, ivalue(rb), ic)); + } + else if (tonumberns(rb, nb)) { + setfltvalue(s2v(ra), luai_nummul(L, nb, cast_num(ic))); + } + else + Protect(luaT_trybiniTM(L, rb, ic, GETARG_Ck(i), ra, TM_MUL)); + vmbreak; + } + vmcase(OP_MODI) { + TValue *rb = vRB(i); + int ic = GETARG_Cr(i); + lua_Number nb; + if (ttisinteger(rb)) { + setivalue(s2v(ra), luaV_mod(L, ivalue(rb), ic)); + } + else if (tonumberns(rb, nb)) { + lua_Number m; + lua_Number nc = cast_num(ic); + luai_nummod(L, nb, nc, m); + setfltvalue(s2v(ra), m); + } + else + Protect(luaT_trybiniTM(L, rb, ic, 0, ra, TM_MOD)); + vmbreak; + } + vmcase(OP_POWI) { + TValue *rb = vRB(i); + int ic = GETARG_Cr(i); + lua_Number nb; + if (tonumberns(rb, nb)) { + lua_Number nc = cast_num(ic); + setfltvalue(s2v(ra), luai_numpow(L, nb, nc)); + } + else + Protect(luaT_trybiniTM(L, rb, ic, 0, ra, TM_POW)); + vmbreak; + } + vmcase(OP_DIVI) { + TValue *rb = vRB(i); + int ic = GETARG_Cr(i); + lua_Number nb; + if (tonumberns(rb, nb)) { + lua_Number nc = cast_num(ic); + setfltvalue(s2v(ra), luai_numdiv(L, nb, nc)); + } + else + Protect(luaT_trybiniTM(L, rb, ic, 0, ra, TM_DIV)); + vmbreak; + } + vmcase(OP_IDIVI) { + TValue *rb = vRB(i); + int ic = GETARG_Cr(i); + lua_Number nb; + if (ttisinteger(rb)) { + setivalue(s2v(ra), luaV_div(L, ivalue(rb), ic)); + } + else if (tonumberns(rb, nb)) { + lua_Number nc = cast_num(ic); + setfltvalue(s2v(ra), luai_numdiv(L, nb, nc)); + } + else + Protect(luaT_trybiniTM(L, rb, ic, 0, ra, TM_IDIV)); + vmbreak; + } vmcase(OP_ADD) { TValue *rb = vRB(i); TValue *rc = vRC(i); From 283e7455ffe32235eaf790ebd3c40c7970b7a833 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 4 Oct 2017 18:53:03 -0300 Subject: [PATCH 0093/1145] detail --- lparser.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lparser.c b/lparser.c index 1831ea01e8..0637a0b753 100644 --- a/lparser.c +++ b/lparser.c @@ -1,5 +1,5 @@ /* -** $Id: lparser.c,v 2.165 2017/09/13 19:50:08 roberto Exp roberto $ +** $Id: lparser.c,v 2.166 2017/09/28 16:53:29 roberto Exp roberto $ ** Lua Parser ** See Copyright Notice in lua.h */ @@ -1564,8 +1564,9 @@ static void exprstat (LexState *ls) { assignment(ls, &v, 1); } else { /* stat -> func */ + Instruction *inst = &getinstruction(fs, &v.v); check_condition(ls, v.v.k == VCALL, "syntax error"); - SETARG_C(getinstruction(fs, &v.v), 1); /* call statement uses no results */ + SETARG_C(*inst, 1); /* call statement uses no results */ } } From a1ef58b3a5986293ed0b7acef50073d94c7f932f Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 4 Oct 2017 18:56:32 -0300 Subject: [PATCH 0094/1145] eplicit 1-bit opcode operand 'k' --- lcode.c | 74 +++++++++++++++++++++++++++++++++--------------------- lcode.h | 8 ++++-- ldebug.c | 17 ++++++------- lopcodes.h | 71 +++++++++++++++++++++------------------------------ ltests.c | 7 +++--- lvm.c | 22 ++++++++-------- 6 files changed, 103 insertions(+), 96 deletions(-) diff --git a/lcode.c b/lcode.c index 786fd3421b..f94afb0b1a 100644 --- a/lcode.c +++ b/lcode.c @@ -1,5 +1,5 @@ /* -** $Id: lcode.c,v 2.128 2017/10/02 22:50:57 roberto Exp roberto $ +** $Id: lcode.c,v 2.129 2017/10/04 15:49:24 roberto Exp roberto $ ** Code generator for Lua ** See Copyright Notice in lua.h */ @@ -202,7 +202,7 @@ static int patchtestreg (FuncState *fs, int node, int reg) { else { /* no register to put value or register already has the value; change instruction to simple test */ - *i = CREATE_ABC(OP_TEST, GETARG_B(*i), 0, GETARG_C(*i)); + *i = CREATE_ABCk(OP_TEST, GETARG_B(*i), 0, GETARG_C(*i), 0); } return 1; } @@ -365,13 +365,18 @@ static int luaK_code (FuncState *fs, Instruction i) { ** Format and emit an 'iABC' instruction. (Assertions check consistency ** of parameters versus opcode.) */ -int luaK_codeABC (FuncState *fs, OpCode o, int a, int b, int c) { +int luaK_codeABCk (FuncState *fs, OpCode o, int a, int b, int c, int k) { lua_assert(getOpMode(o) == iABC); - lua_assert(a <= MAXARG_A && b <= MAXARG_B && c <= MAXARG_C); - return luaK_code(fs, CREATE_ABC(o, a, b, c)); + lua_assert(a <= MAXARG_A && b <= MAXARG_B && + c <= MAXARG_C && (k & ~1) == 0); + return luaK_code(fs, CREATE_ABCk(o, a, b, c, k)); } +#define codeABsC(fs,o,a,b,c,k) luaK_codeABCk(fs,o,a,b,((c) + MAXARG_sC),k) + + + /* ** Format and emit an 'iABx' instruction. */ @@ -448,7 +453,7 @@ void luaK_reserveregs (FuncState *fs, int n) { ) */ static void freereg (FuncState *fs, int reg) { - if (!ISK(reg) && reg >= fs->nactvar) { + if (reg >= fs->nactvar) { fs->freereg--; lua_assert(reg == fs->freereg); } @@ -853,7 +858,7 @@ void luaK_exp2val (FuncState *fs, expdesc *e) { ** Ensures final expression result is in a valid R/K index ** (that is, it is either in a register or in 'k' with an index ** in the range of R/K indices). -** Returns R/K index. +** Returns 1 if expression is K, 0 otherwise. */ int luaK_exp2RK (FuncState *fs, expdesc *e) { luaK_exp2val(fs, e); @@ -867,12 +872,20 @@ int luaK_exp2RK (FuncState *fs, expdesc *e) { vk: e->k = VK; if (e->u.info <= MAXINDEXRK) /* constant fits in 'argC'? */ - return RKASK(e->u.info); + return 1; else break; default: break; } /* not a constant in the right range: put it in a register */ - return luaK_exp2anyreg(fs, e); + luaK_exp2anyreg(fs, e); + return 0; +} + + +static void codeABRK (FuncState *fs, OpCode o, int a, int b, + expdesc *ec) { + int k = luaK_exp2RK(fs, ec); + luaK_codeABCk(fs, o, a, b, ec->u.info, k); } @@ -892,23 +905,19 @@ void luaK_storevar (FuncState *fs, expdesc *var, expdesc *ex) { break; } case VINDEXUP: { - int e = luaK_exp2RK(fs, ex); - luaK_codeABC(fs, OP_SETTABUP, var->u.ind.t, var->u.ind.idx, e); + codeABRK(fs, OP_SETTABUP, var->u.ind.t, var->u.ind.idx, ex); break; } case VINDEXI: { - int e = luaK_exp2RK(fs, ex); - luaK_codeABC(fs, OP_SETI, var->u.ind.t, var->u.ind.idx, e); + codeABRK(fs, OP_SETI, var->u.ind.t, var->u.ind.idx, ex); break; } case VINDEXSTR: { - int e = luaK_exp2RK(fs, ex); - luaK_codeABC(fs, OP_SETFIELD, var->u.ind.t, var->u.ind.idx, e); + codeABRK(fs, OP_SETFIELD, var->u.ind.t, var->u.ind.idx, ex); break; } case VINDEXED: { - int e = luaK_exp2RK(fs, ex); - luaK_codeABC(fs, OP_SETTABLE, var->u.ind.t, var->u.ind.idx, e); + codeABRK(fs, OP_SETTABLE, var->u.ind.t, var->u.ind.idx, ex); break; } default: lua_assert(0); /* invalid var kind to store */ @@ -928,7 +937,7 @@ void luaK_self (FuncState *fs, expdesc *e, expdesc *key) { e->u.info = fs->freereg; /* base register for op_self */ e->k = VNONRELOC; /* self expression has a fixed register */ luaK_reserveregs(fs, 2); /* function and 'self' produced by op_self */ - luaK_codeABC(fs, OP_SELF, e->u.info, ereg, luaK_exp2RK(fs, key)); + codeABRK(fs, OP_SELF, e->u.info, ereg, key); freeexp(fs, key); } @@ -1064,11 +1073,21 @@ static int isKstr (FuncState *fs, expdesc *e) { /* ** Check whether expression 'e' is a literal integer in -** proper range +** proper range to fit in register C +*/ +static int isCint (expdesc *e) { + return (e->k == VKINT && !hasjumps(e) && + l_castS2U(e->u.ival) <= l_castS2U(MAXARG_C)); +} + + +/* +** Check whether expression 'e' is a literal integer in +** proper range to fit in register sC */ -static int isKint (expdesc *e) { +static int isSCint (expdesc *e) { return (e->k == VKINT && !hasjumps(e) && - l_castS2U(e->u.ival) <= l_castS2U(MAXARG_Cr)); + l_castS2U(e->u.ival + MAXARG_sC) <= l_castS2U(MAXARG_C)); } @@ -1091,7 +1110,7 @@ void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) { t->u.ind.idx = k->u.info; /* literal string */ t->k = VINDEXSTR; } - else if (isKint(k)) { + else if (isCint(k)) { t->u.ind.idx = k->u.ival; /* integer constant */ t->k = VINDEXI; } @@ -1187,16 +1206,14 @@ static void codebinexpval (FuncState *fs, OpCode op, */ static void codearith (FuncState *fs, OpCode op, expdesc *e1, expdesc *e2, int flip, int line) { - if (!isKint(e2)) + if (!isSCint(e2)) codebinexpval(fs, op, e1, e2, line); /* use standard operators */ else { /* use immediate operators */ int v2 = cast_int(e2->u.ival); /* immediate operand */ int v1 = luaK_exp2anyreg(fs, e1); - if (flip) - v2 |= BITRK; /* signal that operands were flipped */ op = cast(OpCode, op - OP_ADD + OP_ADDI); freeexp(fs, e1); - e1->u.info = luaK_codeABC(fs, op, 0, v1, v2); /* generate opcode */ + e1->u.info = codeABsC(fs, op, 0, v1, v2, flip); /* generate opcode */ e1->k = VRELOCABLE; /* all those operations are relocatable */ luaK_fixline(fs, line); } @@ -1210,7 +1227,7 @@ static void codearith (FuncState *fs, OpCode op, static void codecommutative (FuncState *fs, OpCode op, expdesc *e1, expdesc *e2, int line) { int flip = 0; - if (isKint(e1)) { + if (isSCint(e1)) { expdesc temp = *e1; *e1 = *e2; *e2 = temp; /* swap 'e1' and 'e2' */ flip = 1; } @@ -1223,8 +1240,7 @@ static void codecommutative (FuncState *fs, OpCode op, ** 'e1' was already put in register by 'luaK_infix'. */ static void codecomp (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) { - int rk1 = (e1->k == VK) ? RKASK(e1->u.info) - : check_exp(e1->k == VNONRELOC, e1->u.info); + int rk1 = check_exp(e1->k == VNONRELOC, e1->u.info); int rk2 = luaK_exp2anyreg(fs, e2); freeexps(fs, e1, e2); switch (opr) { diff --git a/lcode.h b/lcode.h index e7f048fd15..1c3229d982 100644 --- a/lcode.h +++ b/lcode.h @@ -1,5 +1,5 @@ /* -** $Id: lcode.h,v 1.66 2017/09/13 19:50:08 roberto Exp roberto $ +** $Id: lcode.h,v 1.67 2017/09/28 16:53:29 roberto Exp roberto $ ** Code generator for Lua ** See Copyright Notice in lua.h */ @@ -37,6 +37,9 @@ typedef enum BinOpr { } BinOpr; +#define luaK_codeABC(fs,o,a,b,c) luaK_codeABCk(fs,o,a,b,c,0) + + typedef enum UnOpr { OPR_MINUS, OPR_BNOT, OPR_NOT, OPR_LEN, OPR_NOUNOPR } UnOpr; @@ -50,7 +53,8 @@ typedef enum UnOpr { OPR_MINUS, OPR_BNOT, OPR_NOT, OPR_LEN, OPR_NOUNOPR } UnOpr; LUAI_FUNC int luaK_codeABx (FuncState *fs, OpCode o, int A, unsigned int Bx); LUAI_FUNC int luaK_codeAsBx (FuncState *fs, OpCode o, int A, int Bx); -LUAI_FUNC int luaK_codeABC (FuncState *fs, OpCode o, int A, int B, int C); +LUAI_FUNC int luaK_codeABCk (FuncState *fs, OpCode o, int A, + int B, int C, int k); LUAI_FUNC void luaK_fixline (FuncState *fs, int line); LUAI_FUNC void luaK_nil (FuncState *fs, int from, int n); LUAI_FUNC void luaK_reserveregs (FuncState *fs, int n); diff --git a/ldebug.c b/ldebug.c index 67b9496afc..41eaf68c66 100644 --- a/ldebug.c +++ b/ldebug.c @@ -1,5 +1,5 @@ /* -** $Id: ldebug.c,v 2.130 2017/07/10 17:35:12 roberto Exp roberto $ +** $Id: ldebug.c,v 2.131 2017/10/04 15:49:24 roberto Exp roberto $ ** Debug Interface ** See Copyright Notice in lua.h */ @@ -402,7 +402,7 @@ static const char *getobjname (Proto *p, int lastpc, int reg, ** Find a "name" for the constant 'c'. */ static void kname (Proto *p, int c, const char **name) { - TValue *kvalue = &p->k[INDEXK(c)]; + TValue *kvalue = &p->k[c]; *name = (ttisstring(kvalue)) ? svalue(kvalue) : "?"; } @@ -418,17 +418,17 @@ static void rname (Proto *p, int pc, int c, const char **name) { /* -** Find a "name" for the R/K index 'c'. +** Find a "name" for a 'C' value in an RK instruction. */ -static void rkname (Proto *p, int pc, int c, const char **name) { - if (ISK(c)) /* is 'c' a constant? */ - kname(p, INDEXK(c), name); +static void rkname (Proto *p, int pc, Instruction i, const char **name) { + int c = GETARG_C(i); /* key index */ + if (GETARG_k(i)) /* is 'c' a constant? */ + kname(p, c, name); else /* 'c' is a register */ rname(p, pc, c, name); } - static int filterpc (int pc, int jmptarget) { if (pc < jmptarget) /* is code conditional (inside a jump)? */ return -1; /* cannot know who sets that register */ @@ -549,8 +549,7 @@ static const char *gxf (Proto *p, int pc, Instruction i, int isup) { break; } case OP_SELF: { - int k = GETARG_C(i); /* key index */ - rkname(p, pc, k, name); + rkname(p, pc, i, name); return "method"; } default: break; /* go through to return NULL */ diff --git a/lopcodes.h b/lopcodes.h index 39f36aa70d..de0c5cc129 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -1,5 +1,5 @@ /* -** $Id: lopcodes.h,v 1.164 2017/10/02 22:51:32 roberto Exp roberto $ +** $Id: lopcodes.h,v 1.165 2017/10/04 15:49:24 roberto Exp roberto $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ @@ -17,7 +17,7 @@ 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 -iABC | C(9) | | B(8) | | A(8) | | Op(7) | +iABC |k| C(8) | | B(8) | | A(8) | | Op(7) | iABx | Bx(17) | | A(8) | | Op(7) | iAsBx | sBx (signed)(17) | | A(8) | | Op(7) | iAx | Ax(25) | | Op(7) | @@ -34,19 +34,21 @@ enum OpMode {iABC, iABx, iAsBx, iAx}; /* basic instruction format */ /* ** size and position of opcode arguments. */ -#define SIZE_C 9 +#define SIZE_C 8 +#define SIZE_Cx (SIZE_C + 1) #define SIZE_B 8 -#define SIZE_Bx (SIZE_C + SIZE_B) +#define SIZE_Bx (SIZE_Cx + SIZE_B) #define SIZE_A 8 -#define SIZE_Ax (SIZE_C + SIZE_B + SIZE_A) +#define SIZE_Ax (SIZE_Cx + SIZE_B + SIZE_A) #define SIZE_OP 7 #define POS_OP 0 #define POS_A (POS_OP + SIZE_OP) -#define POS_C (POS_A + SIZE_A) -#define POS_B (POS_C + SIZE_C) -#define POS_Bx POS_C +#define POS_B (POS_A + SIZE_A) +#define POS_C (POS_B + SIZE_B) +#define POS_k (POS_C + SIZE_C) +#define POS_Bx POS_B #define POS_Ax POS_A @@ -70,10 +72,11 @@ enum OpMode {iABC, iABx, iAsBx, iAx}; /* basic instruction format */ #endif -#define MAXARG_A ((1<> 1) +#define MAXARG_Cx ((1<<(SIZE_C + 1))-1) /* creates a mask with 'n' 1 bits at position 'p' */ @@ -104,11 +107,10 @@ enum OpMode {iABC, iABx, iAsBx, iAx}; /* basic instruction format */ #define SETARG_B(i,v) setarg(i, v, POS_B, SIZE_B) #define GETARG_C(i) check_exp(checkopm(i, iABC), getarg(i, POS_C, SIZE_C)) +#define GETARG_sC(i) (GETARG_C(i) - MAXARG_sC) #define SETARG_C(i,v) setarg(i, v, POS_C, SIZE_C) -#define GETARG_Cr(i) \ - check_exp(checkopm(i, iABC), getarg(i, POS_C, SIZE_C - 1)) -#define GETARG_Ck(i) getarg(i, (POS_C + SIZE_C - 1), 1) +#define GETARG_k(i) (cast(int, ((i) & (1 << POS_k)))) #define GETARG_Bx(i) check_exp(checkopm(i, iABx), getarg(i, POS_Bx, SIZE_Bx)) #define SETARG_Bx(i,v) setarg(i, v, POS_Bx, SIZE_Bx) @@ -121,10 +123,11 @@ enum OpMode {iABC, iABx, iAsBx, iAx}; /* basic instruction format */ #define SETARG_sBx(i,b) SETARG_Bx((i),cast(unsigned int, (b)+MAXARG_sBx)) -#define CREATE_ABC(o,a,b,c) ((cast(Instruction, o)< Date: Tue, 10 Oct 2017 17:05:40 -0300 Subject: [PATCH 0095/1145] raw operation should not convert strings to numbers --- lobject.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lobject.c b/lobject.c index 21616fade9..470d75db42 100644 --- a/lobject.c +++ b/lobject.c @@ -1,5 +1,5 @@ /* -** $Id: lobject.c,v 2.116 2017/06/29 15:06:44 roberto Exp roberto $ +** $Id: lobject.c,v 2.117 2017/07/07 16:34:32 roberto Exp roberto $ ** Some generic functions over Lua objects ** See Copyright Notice in lua.h */ @@ -135,7 +135,7 @@ int luaO_rawarith (lua_State *L, int op, const TValue *p1, const TValue *p2, } case LUA_OPDIV: case LUA_OPPOW: { /* operate only on floats */ lua_Number n1; lua_Number n2; - if (tonumber(p1, &n1) && tonumber(p2, &n2)) { + if (tonumberns(p1, n1) && tonumberns(p2, n2)) { setfltvalue(res, numarith(L, op, n1, n2)); return 1; } From 1d8920dd7f508af5f2fd743678be1327f30c079b Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 11 Oct 2017 09:38:45 -0300 Subject: [PATCH 0096/1145] some cleaning in GC parameters --- lapi.c | 25 +++++++++++++++---------- lgc.c | 26 ++++++++++++-------------- lgc.h | 18 ++++++++++++++---- lstate.c | 8 +++++--- 4 files changed, 46 insertions(+), 31 deletions(-) diff --git a/lapi.c b/lapi.c index 4985da6be1..6e9b972ddc 100644 --- a/lapi.c +++ b/lapi.c @@ -1,5 +1,5 @@ /* -** $Id: lapi.c,v 2.269 2017/06/01 20:22:33 roberto Exp roberto $ +** $Id: lapi.c,v 2.270 2017/06/29 15:06:44 roberto Exp roberto $ ** Lua API ** See Copyright Notice in lua.h */ @@ -1114,14 +1114,14 @@ LUA_API int lua_gc (lua_State *L, int what, ...) { } case LUA_GCSETPAUSE: { int data = va_arg(argp, int); - res = g->gcpause + 100; - g->gcpause = (data >= 100) ? data - 100 : 0; + res = getgcparam(g->gcpause); + setgcparam(g->gcpause, data); break; } case LUA_GCSETSTEPMUL: { int data = va_arg(argp, int); - res = g->gcstepmul * 10; - g->gcstepmul = data / 10; + res = getgcparam(g->gcstepmul); + setgcparam(g->gcstepmul, data); break; } case LUA_GCISRUNNING: { @@ -1131,8 +1131,10 @@ LUA_API int lua_gc (lua_State *L, int what, ...) { case LUA_GCGEN: { int minormul = va_arg(argp, int); int majormul = va_arg(argp, int); - g->genminormul = (minormul == 0) ? LUAI_GENMINORMUL : minormul; - g->genmajormul = (majormul == 0) ? LUAI_GENMAJORMUL : majormul; + if (minormul != 0) + g->genminormul = minormul; + if (majormul != 0) + setgcparam(g->genmajormul, majormul); luaC_changemode(L, KGC_GEN); break; } @@ -1140,9 +1142,12 @@ LUA_API int lua_gc (lua_State *L, int what, ...) { int pause = va_arg(argp, int); int stepmul = va_arg(argp, int); int stepsize = va_arg(argp, int); - g->gcpause = (pause == 0) ? LUAI_GCPAUSE : pause; - g->gcstepmul = (stepmul == 0) ? LUAI_GCMUL : stepmul; - g->gcstepsize = (stepsize == 0) ? LUAI_GCSTEPSIZE : stepsize; + if (pause != 0) + setgcparam(g->gcpause, pause); + if (stepmul != 0) + setgcparam(g->gcstepmul, stepmul); + if (stepsize != 0) + g->gcstepsize = stepsize; luaC_changemode(L, KGC_INC); break; } diff --git a/lgc.c b/lgc.c index e658a6f8dc..3e67d6d775 100644 --- a/lgc.c +++ b/lgc.c @@ -1,5 +1,5 @@ /* -** $Id: lgc.c,v 2.233 2017/06/29 15:06:44 roberto Exp roberto $ +** $Id: lgc.c,v 2.234 2017/08/31 16:06:51 roberto Exp roberto $ ** Garbage Collector ** See Copyright Notice in lua.h */ @@ -47,9 +47,9 @@ /* ** The equivalent, in bytes, of one unit of "work" (visiting a slot, -** sweeping an object, etc.) * 10 (for scaling) +** sweeping an object, etc.) */ -#define WORK2MEM (sizeof(TValue) * 10) +#define WORK2MEM sizeof(TValue) /* @@ -1256,14 +1256,14 @@ static void fullgen (lua_State *L, global_State *g) { ** than last major collection (kept in 'g->GCestimate'), does a major ** collection. Otherwise, does a minor collection and set debt to make ** another collection when memory grows 'genminormul'% larger. -** 'GCdebt <= 0' means an explicity call to GC step with "size" zero; +** 'GCdebt <= 0' means an explicit call to GC step with "size" zero; ** in that case, always do a minor collection. */ static void genstep (lua_State *L, global_State *g) { lu_mem majorbase = g->GCestimate; -//lua_checkmemory(L); + int majormul = getgcparam(g->genmajormul); if (g->GCdebt > 0 && - gettotalbytes(g) > (majorbase / 100) * (100 + g->genmajormul)) { + gettotalbytes(g) > (majorbase / 100) * (100 + majormul)) { fullgen(L, g); } else { @@ -1273,7 +1273,6 @@ static void genstep (lua_State *L, global_State *g) { luaE_setdebt(g, -((mem / 100) * g->genminormul)); g->GCestimate = majorbase; /* preserve base value */ } -//lua_checkmemory(L); } /* }====================================================== */ @@ -1288,13 +1287,13 @@ static void genstep (lua_State *L, global_State *g) { /* ** Set the "time" to wait before starting a new GC cycle; cycle will -** start when memory use hits the threshold of ('estimate' * gcpause / +** start when memory use hits the threshold of ('estimate' * pause / ** PAUSEADJ). (Division by 'estimate' should be OK: it cannot be zero, ** because Lua cannot even start with less than PAUSEADJ bytes). */ static void setpause (global_State *g) { l_mem threshold, debt; - int pause = g->gcpause + 100; + int pause = getgcparam(g->gcpause); l_mem estimate = g->GCestimate / PAUSEADJ; /* adjust 'estimate' */ lua_assert(estimate > 0); threshold = (pause < MAX_LMEM / estimate) /* overflow? */ @@ -1482,16 +1481,15 @@ void luaC_runtilstate (lua_State *L, int statesmask) { ** controls when next step will be performed. */ static void incstep (lua_State *L, global_State *g) { - int stepmul = (g->gcstepmul | 1); /* avoid division by 0 */ + int stepmul = (getgcparam(g->gcstepmul) | 1); /* avoid division by 0 */ l_mem debt = (g->GCdebt / WORK2MEM) * stepmul; l_mem stepsize = (g->gcstepsize <= log2maxs(l_mem)) - ? cast(l_mem, 1) << g->gcstepsize - : MAX_LMEM; - stepsize = -((stepsize / WORK2MEM) * stepmul); + ? ((cast(l_mem, 1) << g->gcstepsize) / WORK2MEM) * stepmul + : MAX_LMEM; /* overflow; keep maximum value */ do { /* repeat until pause or enough "credit" (negative debt) */ lu_mem work = singlestep(L); /* perform one single step */ debt -= work; - } while (debt > stepsize && g->gcstate != GCSpause); + } while (debt > -stepsize && g->gcstate != GCSpause); if (g->gcstate == GCSpause) setpause(g); /* pause until next cycle */ else { diff --git a/lgc.h b/lgc.h index 36f361ec12..5113e922f0 100644 --- a/lgc.h +++ b/lgc.h @@ -1,5 +1,5 @@ /* -** $Id: lgc.h,v 2.97 2017/05/04 13:32:01 roberto Exp roberto $ +** $Id: lgc.h,v 2.98 2017/05/26 19:14:29 roberto Exp roberto $ ** Garbage Collector ** See Copyright Notice in lua.h */ @@ -119,10 +119,20 @@ /* Default Values for GC parameters */ #define LUAI_GENMAJORMUL 100 -#define LUAI_GENMINORMUL 5 +#define LUAI_GENMINORMUL 12 + +/* wait memory to double before starting new cycle */ +#define LUAI_GCPAUSE 200 /* 200% */ + +/* +** gc parameters are stored divided by 4 to allow a maximum value larger +** than 1000 in an 'lu_byte'. +*/ +#define getgcparam(p) ((p) * 4) +#define setgcparam(p,v) ((p) = (v) / 4) + +#define LUAI_GCMUL 100 -#define LUAI_GCPAUSE 100 /* 100% */ -#define LUAI_GCMUL 10 /* how much to allocate before next GC step (log2) */ #define LUAI_GCSTEPSIZE 13 /* 8 KB */ diff --git a/lstate.c b/lstate.c index b2c3e0c49b..626377ae52 100644 --- a/lstate.c +++ b/lstate.c @@ -1,5 +1,5 @@ /* -** $Id: lstate.c,v 2.140 2017/05/26 19:14:29 roberto Exp roberto $ +** $Id: lstate.c,v 2.141 2017/06/29 15:06:44 roberto Exp roberto $ ** Global State ** See Copyright Notice in lua.h */ @@ -320,9 +320,11 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { g->twups = NULL; g->totalbytes = sizeof(LG); g->GCdebt = 0; - g->gcpause = LUAI_GCPAUSE; - g->gcstepmul = LUAI_GCMUL; + setgcparam(g->gcpause, LUAI_GCPAUSE); + setgcparam(g->gcstepmul, LUAI_GCMUL); g->gcstepsize = LUAI_GCSTEPSIZE; + setgcparam(g->genmajormul, LUAI_GENMAJORMUL); + g->genminormul = LUAI_GENMINORMUL; for (i=0; i < LUA_NUMTAGS; i++) g->mt[i] = NULL; if (luaD_rawrunprotected(L, f_luaopen, NULL) != LUA_OK) { /* memory allocation error: free partial state */ From de9128d09d55318f81d58fc8933ef180885c8850 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 31 Oct 2017 13:29:28 -0200 Subject: [PATCH 0097/1145] do not mess up the debt when the collector is not running --- lgc.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lgc.c b/lgc.c index 3e67d6d775..d9b3195878 100644 --- a/lgc.c +++ b/lgc.c @@ -1,5 +1,5 @@ /* -** $Id: lgc.c,v 2.234 2017/08/31 16:06:51 roberto Exp roberto $ +** $Id: lgc.c,v 2.235 2017/10/11 12:38:45 roberto Exp roberto $ ** Garbage Collector ** See Copyright Notice in lua.h */ @@ -1503,12 +1503,12 @@ static void incstep (lua_State *L, global_State *g) { */ void luaC_step (lua_State *L) { global_State *g = G(L); - if (!g->gcrunning) /* not running? */ - luaE_setdebt(g, -MAX_LMEM); /* avoid being called without need */ - else if (g->gckind == KGC_INC) - incstep(L, g); - else - genstep(L, g); + if (g->gcrunning) { /* running? */ + if (g->gckind == KGC_INC) + incstep(L, g); + else + genstep(L, g); + } } From ad5dcdcf0f1e139c8cc5b1aafd11db69f54010de Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 31 Oct 2017 15:14:02 -0200 Subject: [PATCH 0098/1145] detail: in 'isinstack', check against the whole stack instead of against the stack frame --- ldebug.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/ldebug.c b/ldebug.c index 41eaf68c66..f3f807f1b2 100644 --- a/ldebug.c +++ b/ldebug.c @@ -1,5 +1,5 @@ /* -** $Id: ldebug.c,v 2.131 2017/10/04 15:49:24 roberto Exp roberto $ +** $Id: ldebug.c,v 2.132 2017/10/04 21:56:32 roberto Exp roberto $ ** Debug Interface ** See Copyright Notice in lua.h */ @@ -627,10 +627,10 @@ static const char *funcnamefromcode (lua_State *L, CallInfo *ci, ** not ISO C, but it should not crash a program; the subsequent ** checks are ISO C and ensure a correct result. */ -static int isinstack (CallInfo *ci, const TValue *o) { - StkId base = ci->func + 1; +static int isinstack (lua_State *L, const TValue *o) { + StkId base = L->stack; ptrdiff_t i = cast(StkId, o) - base; - return (0 <= i && i < (ci->top - base) && s2v(base + i) == o); + return (0 <= i && i < (L->top - base) && s2v(base + i) == o); } @@ -659,7 +659,7 @@ static const char *varinfo (lua_State *L, const TValue *o) { const char *kind = NULL; if (isLua(ci)) { kind = getupvalname(ci, o, &name); /* check whether 'o' is an upvalue */ - if (!kind && isinstack(ci, o)) /* no? try a register */ + if (!kind && isinstack(L, o)) /* no? try a register */ kind = getobjname(ci_func(ci)->p, currentpc(ci), cast_int(cast(StkId, o) - (ci->func + 1)), &name); } From c5482468fde11c6c169da3b331a0653455f8fc94 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 31 Oct 2017 15:54:35 -0200 Subject: [PATCH 0099/1145] baby steps to remove 'CallInfo': keeping 'L->func' correct --- ldo.c | 14 +++++++++++--- lobject.h | 6 +++++- lstate.c | 9 ++++++--- lstate.h | 3 ++- lvm.c | 3 ++- 5 files changed, 26 insertions(+), 9 deletions(-) diff --git a/ldo.c b/ldo.c index 0c0bb0bc9e..4d2b3c76f2 100644 --- a/ldo.c +++ b/ldo.c @@ -1,5 +1,5 @@ /* -** $Id: ldo.c,v 2.161 2017/06/29 15:06:44 roberto Exp roberto $ +** $Id: ldo.c,v 2.162 2017/07/27 13:50:16 roberto Exp roberto $ ** Stack and Call structure of Lua ** See Copyright Notice in lua.h */ @@ -160,6 +160,7 @@ static void correctstack (lua_State *L, StkId oldstack) { CallInfo *ci; UpVal *up; L->top = (L->top - oldstack) + L->stack; + L->func = (L->func - oldstack) + L->stack; for (up = L->openupval; up != NULL; up = up->u.open.next) up->v = s2v((uplevel(up) - oldstack) + L->stack); for (ci = L->ci; ci != NULL; ci = ci->previous) { @@ -369,6 +370,8 @@ int luaD_poscall (lua_State *L, CallInfo *ci, StkId firstResult, int nres) { } res = ci->func; /* res == final position of 1st result */ L->ci = ci->previous; /* back to caller */ + L->func -= L->func->stkci.previous; + lua_assert(L->func == L->ci->func); /* move results to proper place */ return moveresults(L, firstResult, res, nres, wanted); } @@ -400,7 +403,8 @@ int luaD_precall (lua_State *L, StkId func, int nresults) { checkstackp(L, LUA_MINSTACK, func); /* ensure minimum stack size */ ci = next_ci(L); /* now 'enter' new function */ ci->nresults = nresults; - ci->func = func; + func->stkci.previous = func - L->func; + L->func = ci->func = func; ci->top = L->top + LUA_MINSTACK; lua_assert(ci->top <= L->stack_last); ci->callstatus = 0; @@ -424,7 +428,8 @@ int luaD_precall (lua_State *L, StkId func, int nresults) { luaT_adjustvarargs(L, p, n); ci = next_ci(L); /* now 'enter' new function */ ci->nresults = nresults; - ci->func = func; + func->stkci.previous = func - L->func; + L->func = ci->func = func; L->top = ci->top = func + 1 + fsize; lua_assert(ci->top <= L->stack_last); ci->u.l.savedpc = p->code; /* starting point */ @@ -558,6 +563,7 @@ static int recover (lua_State *L, int status) { luaF_close(L, oldtop); seterrorobj(L, status, oldtop); L->ci = ci; + L->func = ci->func; L->allowhook = getoah(ci->callstatus); /* restore original 'allowhook' */ L->nny = 0; /* should be zero to be yieldable */ luaD_shrinkstack(L); @@ -693,6 +699,7 @@ int luaD_pcall (lua_State *L, Pfunc func, void *u, ptrdiff_t old_top, ptrdiff_t ef) { int status; CallInfo *old_ci = L->ci; + ptrdiff_t oldfunc = savestack(L, L->func); lu_byte old_allowhooks = L->allowhook; unsigned short old_nny = L->nny; ptrdiff_t old_errfunc = L->errfunc; @@ -703,6 +710,7 @@ int luaD_pcall (lua_State *L, Pfunc func, void *u, luaF_close(L, oldtop); /* close possible pending closures */ seterrorobj(L, status, oldtop); L->ci = old_ci; + L->func = restorestack(L, oldfunc); L->allowhook = old_allowhooks; L->nny = old_nny; luaD_shrinkstack(L); diff --git a/lobject.h b/lobject.h index 769be79def..e3ae005063 100644 --- a/lobject.h +++ b/lobject.h @@ -1,5 +1,5 @@ /* -** $Id: lobject.h,v 2.124 2017/06/27 11:35:31 roberto Exp roberto $ +** $Id: lobject.h,v 2.125 2017/06/29 15:06:44 roberto Exp roberto $ ** Type definitions for Lua objects ** See Copyright Notice in lua.h */ @@ -311,6 +311,10 @@ typedef struct TValue { typedef union StackValue { TValue val; + struct { + TValuefields; + unsigned short previous; /* difference to previous 'func' */ + } stkci; } StackValue; diff --git a/lstate.c b/lstate.c index 626377ae52..c4305ed84e 100644 --- a/lstate.c +++ b/lstate.c @@ -1,5 +1,5 @@ /* -** $Id: lstate.c,v 2.141 2017/06/29 15:06:44 roberto Exp roberto $ +** $Id: lstate.c,v 2.142 2017/10/11 12:38:45 roberto Exp roberto $ ** Global State ** See Copyright Notice in lua.h */ @@ -153,8 +153,10 @@ static void stack_init (lua_State *L1, lua_State *L) { ci = &L1->base_ci; ci->next = ci->previous = NULL; ci->callstatus = 0; - ci->func = L1->top; - setnilvalue(s2v(L1->top++)); /* 'function' entry for this 'ci' */ + L1->func = ci->func = L1->top; + L1->func->stkci.previous = 0; /* end of linked list */ + setnilvalue(s2v(L1->top)); /* 'function' entry for this 'ci' */ + L1->top++; ci->top = L1->top + LUA_MINSTACK; L1->ci = ci; } @@ -215,6 +217,7 @@ static void preinit_thread (lua_State *L, global_State *g) { G(L) = g; L->stack = NULL; L->ci = NULL; + L->func = NULL; L->nci = 0; L->stacksize = 0; L->twups = L; /* thread has no upvalues */ diff --git a/lstate.h b/lstate.h index 5ccc402507..76ce110fa9 100644 --- a/lstate.h +++ b/lstate.h @@ -1,5 +1,5 @@ /* -** $Id: lstate.h,v 2.143 2017/06/12 14:21:44 roberto Exp roberto $ +** $Id: lstate.h,v 2.144 2017/07/27 13:50:16 roberto Exp roberto $ ** Global State ** See Copyright Notice in lua.h */ @@ -192,6 +192,7 @@ struct lua_State { StkId top; /* first free slot in the stack */ global_State *l_G; CallInfo *ci; /* call info for current function */ + StkId func; /* current function */ const Instruction *oldpc; /* last pc traced */ StkId stack_last; /* last free slot in the stack */ StkId stack; /* stack base */ diff --git a/lvm.c b/lvm.c index b3e32047ac..4fabd74400 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.298 2017/10/04 15:49:24 roberto Exp roberto $ +** $Id: lvm.c,v 2.299 2017/10/04 21:56:32 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -1390,6 +1390,7 @@ void luaV_execute (lua_State *L) { oci->u.l.savedpc = nci->u.l.savedpc; oci->callstatus |= CIST_TAIL; /* function was tail called */ ci = L->ci = oci; /* remove new frame */ + L->func = ofunc; lua_assert(L->top == oci->func + 1 + getproto(s2v(ofunc))->maxstacksize); goto newframe; /* restart luaV_execute over new Lua function */ From b9e76be8a691ed83a716a85c6b85cb80f66cc480 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 1 Nov 2017 16:20:48 -0200 Subject: [PATCH 0100/1145] using 'L->func' when possible --- lapi.c | 25 ++++++++++++------------- lapi.h | 4 ++-- ldebug.c | 8 ++++---- ldo.c | 10 +++++----- ltests.c | 4 ++-- lvm.c | 25 ++++++++++++------------- 6 files changed, 37 insertions(+), 39 deletions(-) diff --git a/lapi.c b/lapi.c index 6e9b972ddc..0f88e8dead 100644 --- a/lapi.c +++ b/lapi.c @@ -1,5 +1,5 @@ /* -** $Id: lapi.c,v 2.270 2017/06/29 15:06:44 roberto Exp roberto $ +** $Id: lapi.c,v 2.271 2017/10/11 12:38:45 roberto Exp roberto $ ** Lua API ** See Copyright Notice in lua.h */ @@ -60,13 +60,13 @@ const char lua_ident[] = static TValue *index2value (lua_State *L, int idx) { CallInfo *ci = L->ci; if (idx > 0) { - StkId o = ci->func + idx; - api_check(L, idx <= ci->top - (ci->func + 1), "unacceptable index"); + StkId o = L->func + idx; + api_check(L, idx <= ci->top - (L->func + 1), "unacceptable index"); if (o >= L->top) return NONVALIDVALUE; else return s2v(o); } else if (!ispseudo(idx)) { /* negative index */ - api_check(L, idx != 0 && -idx <= L->top - (ci->func + 1), "invalid index"); + api_check(L, idx != 0 && -idx <= L->top - (L->func + 1), "invalid index"); return s2v(L->top + idx); } else if (idx == LUA_REGISTRYINDEX) @@ -74,10 +74,10 @@ static TValue *index2value (lua_State *L, int idx) { else { /* upvalues */ idx = LUA_REGISTRYINDEX - idx; api_check(L, idx <= MAXUPVAL + 1, "upvalue index too large"); - if (ttislcf(s2v(ci->func))) /* light C function? */ + if (ttislcf(s2v(L->func))) /* light C function? */ return NONVALIDVALUE; /* it has no upvalues */ else { - CClosure *func = clCvalue(s2v(ci->func)); + CClosure *func = clCvalue(s2v(L->func)); return (idx <= func->nupvalues) ? &func->upvalue[idx-1] : NONVALIDVALUE; } } @@ -85,14 +85,13 @@ static TValue *index2value (lua_State *L, int idx) { static StkId index2stack (lua_State *L, int idx) { - CallInfo *ci = L->ci; if (idx > 0) { - StkId o = ci->func + idx; + StkId o = L->func + idx; api_check(L, o < L->top, "unacceptable index"); return o; } else { /* non-positive index */ - api_check(L, idx != 0 && -idx <= L->top - (ci->func + 1), "invalid index"); + api_check(L, idx != 0 && -idx <= L->top - (L->func + 1), "invalid index"); api_check(L, !ispseudo(idx), "invalid index"); return L->top + idx; } @@ -175,17 +174,17 @@ LUA_API const lua_Number *lua_version (lua_State *L) { LUA_API int lua_absindex (lua_State *L, int idx) { return (idx > 0 || ispseudo(idx)) ? idx - : cast_int(L->top - L->ci->func) + idx; + : cast_int(L->top - L->func) + idx; } LUA_API int lua_gettop (lua_State *L) { - return cast_int(L->top - (L->ci->func + 1)); + return cast_int(L->top - (L->func + 1)); } LUA_API void lua_settop (lua_State *L, int idx) { - StkId func = L->ci->func; + StkId func = L->func; lua_lock(L); if (idx >= 0) { api_check(L, idx <= L->stack_last - (func + 1), "new top too large"); @@ -243,7 +242,7 @@ LUA_API void lua_copy (lua_State *L, int fromidx, int toidx) { api_checkvalidindex(L, to); setobj(L, to, fr); if (isupvalue(toidx)) /* function upvalue? */ - luaC_barrier(L, clCvalue(s2v(L->ci->func)), fr); + luaC_barrier(L, clCvalue(s2v(L->func)), fr); /* LUA_REGISTRYINDEX does not need gc barrier (collector revisits it before finishing collection) */ lua_unlock(L); diff --git a/lapi.h b/lapi.h index b39898ebd5..ce64a7274c 100644 --- a/lapi.h +++ b/lapi.h @@ -1,5 +1,5 @@ /* -** $Id: lapi.h,v 2.8 2014/07/15 21:26:50 roberto Exp roberto $ +** $Id: lapi.h,v 2.9 2015/03/06 19:49:50 roberto Exp roberto $ ** Auxiliary functions from Lua API ** See Copyright Notice in lua.h */ @@ -17,7 +17,7 @@ #define adjustresults(L,nres) \ { if ((nres) == LUA_MULTRET && L->ci->top < L->top) L->ci->top = L->top; } -#define api_checknelems(L,n) api_check(L, (n) < (L->top - L->ci->func), \ +#define api_checknelems(L,n) api_check(L, (n) < (L->top - L->func), \ "not enough elements in the stack") diff --git a/ldebug.c b/ldebug.c index f3f807f1b2..9b6f3bbe6c 100644 --- a/ldebug.c +++ b/ldebug.c @@ -1,5 +1,5 @@ /* -** $Id: ldebug.c,v 2.132 2017/10/04 21:56:32 roberto Exp roberto $ +** $Id: ldebug.c,v 2.133 2017/10/31 17:14:02 roberto Exp roberto $ ** Debug Interface ** See Copyright Notice in lua.h */ @@ -116,7 +116,7 @@ static void swapextra (lua_State *L) { if (L->status == LUA_YIELD) { CallInfo *ci = L->ci; /* get function that yielded */ StkId temp = ci->func; /* exchange its 'func' and 'extra' values */ - ci->func = restorestack(L, ci->extra); + L->func = ci->func = restorestack(L, ci->extra); ci->extra = savestack(L, temp); } } @@ -661,7 +661,7 @@ static const char *varinfo (lua_State *L, const TValue *o) { kind = getupvalname(ci, o, &name); /* check whether 'o' is an upvalue */ if (!kind && isinstack(L, o)) /* no? try a register */ kind = getobjname(ci_func(ci)->p, currentpc(ci), - cast_int(cast(StkId, o) - (ci->func + 1)), &name); + cast_int(cast(StkId, o) - (L->func + 1)), &name); } return (kind) ? luaO_pushfstring(L, " (%s '%s')", kind, name) : ""; } @@ -790,7 +790,7 @@ void luaG_traceexec (lua_State *L) { L->hookcount = 1; /* undo decrement to zero */ ci->u.l.savedpc--; /* undo increment (resume will increment it again) */ ci->callstatus |= CIST_HOOKYIELD; /* mark that it yielded */ - ci->func = L->top - 1; /* protect stack below results */ + L->func = ci->func = L->top - 1; /* protect stack below results */ luaD_throw(L, LUA_YIELD); } } diff --git a/ldo.c b/ldo.c index 4d2b3c76f2..46d6dcecfd 100644 --- a/ldo.c +++ b/ldo.c @@ -1,5 +1,5 @@ /* -** $Id: ldo.c,v 2.162 2017/07/27 13:50:16 roberto Exp roberto $ +** $Id: ldo.c,v 2.163 2017/10/31 17:54:35 roberto Exp roberto $ ** Stack and Call structure of Lua ** See Copyright Notice in lua.h */ @@ -368,7 +368,7 @@ int luaD_poscall (lua_State *L, CallInfo *ci, StkId firstResult, int nres) { } L->oldpc = ci->previous->u.l.savedpc; /* 'oldpc' for caller function */ } - res = ci->func; /* res == final position of 1st result */ + res = L->func; /* res == final position of 1st result */ L->ci = ci->previous; /* back to caller */ L->func -= L->func->stkci.previous; lua_assert(L->func == L->ci->func); @@ -604,7 +604,7 @@ static void resume (lua_State *L, void *ud) { else { /* resuming from previous yield */ lua_assert(L->status == LUA_YIELD); L->status = LUA_OK; /* mark that it is running (again) */ - ci->func = restorestack(L, ci->extra); + L->func = ci->func = restorestack(L, ci->extra); if (isLua(ci)) /* yielded inside a hook? */ luaV_execute(L); /* just continue running Lua code */ else { /* 'common' yield */ @@ -679,14 +679,14 @@ LUA_API int lua_yieldk (lua_State *L, int nresults, lua_KContext ctx, luaG_runerror(L, "attempt to yield from outside a coroutine"); } L->status = LUA_YIELD; - ci->extra = savestack(L, ci->func); /* save current 'func' */ + ci->extra = savestack(L, L->func); /* save current 'func' */ if (isLua(ci)) { /* inside a hook? */ api_check(L, k == NULL, "hooks cannot continue after yielding"); } else { if ((ci->u.c.k = k) != NULL) /* is there a continuation? */ ci->u.c.ctx = ctx; /* save context */ - ci->func = L->top - nresults - 1; /* protect stack below results */ + L->func = ci->func = L->top - nresults - 1; /* protect stack below results */ luaD_throw(L, LUA_YIELD); } lua_assert(ci->callstatus & CIST_HOOKED); /* must be inside a hook */ diff --git a/ltests.c b/ltests.c index 7420fe3b76..94b4cd3100 100644 --- a/ltests.c +++ b/ltests.c @@ -1,5 +1,5 @@ /* -** $Id: ltests.c,v 2.224 2017/10/01 19:17:51 roberto Exp roberto $ +** $Id: ltests.c,v 2.225 2017/10/04 21:56:32 roberto Exp roberto $ ** Internal Module for Debugging of the Lua Implementation ** See Copyright Notice in lua.h */ @@ -46,7 +46,7 @@ void *l_Trick = 0; int islocked = 0; -#define obj_at(L,k) s2v(L->ci->func + (k)) +#define obj_at(L,k) s2v(L->func + (k)) static int runC (lua_State *L, lua_State *L1, const char *pc); diff --git a/lvm.c b/lvm.c index 4fabd74400..25b953a4c0 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.299 2017/10/04 21:56:32 roberto Exp roberto $ +** $Id: lvm.c,v 2.300 2017/10/31 17:54:35 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -659,7 +659,7 @@ static void pushclosure (lua_State *L, Proto *p, UpVal **encup, StkId base, */ void luaV_finishOp (lua_State *L) { CallInfo *ci = L->ci; - StkId base = ci->func + 1; + StkId base = L->func + 1; Instruction inst = *(ci->u.l.savedpc - 1); /* interrupted instruction */ OpCode op = GET_OPCODE(inst); switch (op) { /* finish its execution */ @@ -699,7 +699,7 @@ void luaV_finishOp (lua_State *L) { luaV_concat(L, total); /* concat them (may yield again) */ } /* move final result to final position */ - setobjs2s(L, ci->func + 1 + GETARG_A(inst), L->top - 1); + setobjs2s(L, L->func + 1 + GETARG_A(inst), L->top - 1); L->top = ci->top; /* restore top */ break; } @@ -771,7 +771,7 @@ void luaV_finishOp (lua_State *L) { ** stack, and change the hooks. */ #define Protect(code) \ - { savepc(L); {code;}; base = ci->func + 1; updatemask(L); } + { savepc(L); {code;}; base = L->func + 1; updatemask(L); } #define checkGC(L,c) \ @@ -796,23 +796,23 @@ void luaV_execute (lua_State *L) { CallInfo *ci = L->ci; LClosure *cl; TValue *k; - StkId base; /* local copy of 'ci->func + 1' */ + StkId base; /* local copy of 'L->func + 1' */ int mask; /* local copy of 'L->hookmask & (LUA_MASKLINE | LUA_MASKCOUNT)' */ const Instruction *pc; /* local copy of 'ci->u.l.savedpc' */ ci->callstatus |= CIST_FRESH; /* fresh invocation of 'luaV_execute" */ newframe: /* reentry point when frame changes (call/return) */ lua_assert(ci == L->ci); - cl = clLvalue(s2v(ci->func)); /* local reference to function's closure */ + cl = clLvalue(s2v(L->func)); /* local reference to function's closure */ k = cl->p->k; /* local reference to function's constant table */ updatemask(L); - base = ci->func + 1; + base = L->func + 1; pc = ci->u.l.savedpc; /* main loop of interpreter */ for (;;) { Instruction i; StkId ra; vmfetch(); - lua_assert(base == ci->func + 1); + lua_assert(base == L->func + 1); lua_assert(base <= L->top && L->top < L->stack + L->stacksize); vmdispatch (GET_OPCODE(i)) { vmcase(OP_MOVE) { @@ -1377,12 +1377,12 @@ void luaV_execute (lua_State *L) { CallInfo *nci = L->ci; /* called frame (new) */ CallInfo *oci = nci->previous; /* caller frame (old) */ StkId nfunc = nci->func; /* called function */ - StkId ofunc = oci->func; /* caller function */ + StkId ofunc = nfunc - nfunc->stkci.previous; /* caller function */ /* last stack slot filled by 'precall' */ - StkId lim = nci->func + 1 + getproto(s2v(nfunc))->numparams; + StkId lim = nfunc + 1 + getproto(s2v(nfunc))->numparams; int aux; /* close all upvalues from previous call */ - if (cl->p->sizep > 0) luaF_close(L, oci->func + 1); + if (cl->p->sizep > 0) luaF_close(L, ofunc + 1); /* move new frame into old one */ for (aux = 0; nfunc + aux < lim; aux++) setobjs2s(L, ofunc + aux, nfunc + aux); @@ -1391,8 +1391,7 @@ void luaV_execute (lua_State *L) { oci->callstatus |= CIST_TAIL; /* function was tail called */ ci = L->ci = oci; /* remove new frame */ L->func = ofunc; - lua_assert(L->top == - oci->func + 1 + getproto(s2v(ofunc))->maxstacksize); + lua_assert(L->top == ofunc + 1 + getproto(s2v(ofunc))->maxstacksize); goto newframe; /* restart luaV_execute over new Lua function */ } vmbreak; From ba36180fd7b68341ad57e0fbe7a55cdfb334908d Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 2 Nov 2017 09:28:56 -0200 Subject: [PATCH 0101/1145] new API for 'lua_resume' + cleaning the uses of the 'extra' field in 'CallInfo' --- lapi.c | 7 +++---- lcorolib.c | 7 +++---- ldebug.c | 25 +------------------------ ldo.c | 14 ++++++++------ lstate.h | 13 +++++-------- ltests.c | 18 ++++++++---------- lua.h | 5 +++-- 7 files changed, 31 insertions(+), 58 deletions(-) diff --git a/lapi.c b/lapi.c index 0f88e8dead..8d4d5e7d23 100644 --- a/lapi.c +++ b/lapi.c @@ -1,5 +1,5 @@ /* -** $Id: lapi.c,v 2.271 2017/10/11 12:38:45 roberto Exp roberto $ +** $Id: lapi.c,v 2.272 2017/11/01 18:20:48 roberto Exp roberto $ ** Lua API ** See Copyright Notice in lua.h */ @@ -58,10 +58,9 @@ const char lua_ident[] = static TValue *index2value (lua_State *L, int idx) { - CallInfo *ci = L->ci; if (idx > 0) { StkId o = L->func + idx; - api_check(L, idx <= ci->top - (L->func + 1), "unacceptable index"); + api_check(L, idx <= L->ci->top - (L->func + 1), "unacceptable index"); if (o >= L->top) return NONVALIDVALUE; else return s2v(o); } @@ -1000,7 +999,7 @@ LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc, ci->u.c.k = k; /* save continuation */ ci->u.c.ctx = ctx; /* save context */ /* save information for error recovery */ - ci->extra = savestack(L, c.func); + ci->u2.funcidx = savestack(L, c.func); ci->u.c.old_errfunc = L->errfunc; L->errfunc = func; setoah(ci->callstatus, L->allowhook); /* save value of 'allowhook' */ diff --git a/lcorolib.c b/lcorolib.c index 95467264cd..49a3cf28d1 100644 --- a/lcorolib.c +++ b/lcorolib.c @@ -1,5 +1,5 @@ /* -** $Id: lcorolib.c,v 1.9 2014/11/02 19:19:04 roberto Exp roberto $ +** $Id: lcorolib.c,v 1.10 2016/04/11 19:19:55 roberto Exp roberto $ ** Coroutine Library ** See Copyright Notice in lua.h */ @@ -26,7 +26,7 @@ static lua_State *getco (lua_State *L) { static int auxresume (lua_State *L, lua_State *co, int narg) { - int status; + int status, nres; if (!lua_checkstack(co, narg)) { lua_pushliteral(L, "too many arguments to resume"); return -1; /* error flag */ @@ -36,9 +36,8 @@ static int auxresume (lua_State *L, lua_State *co, int narg) { return -1; /* error flag */ } lua_xmove(L, co, narg); - status = lua_resume(co, L, narg); + status = lua_resume(co, L, narg, &nres); if (status == LUA_OK || status == LUA_YIELD) { - int nres = lua_gettop(co); if (!lua_checkstack(L, nres + 1)) { lua_pop(co, nres); /* remove results anyway */ lua_pushliteral(L, "too many results to resume"); diff --git a/ldebug.c b/ldebug.c index 9b6f3bbe6c..5ca78bd11e 100644 --- a/ldebug.c +++ b/ldebug.c @@ -1,5 +1,5 @@ /* -** $Id: ldebug.c,v 2.133 2017/10/31 17:14:02 roberto Exp roberto $ +** $Id: ldebug.c,v 2.134 2017/11/01 18:20:48 roberto Exp roberto $ ** Debug Interface ** See Copyright Notice in lua.h */ @@ -106,22 +106,6 @@ static int currentline (CallInfo *ci) { } -/* -** If function yielded, its 'func' can be in the 'extra' field. The -** next function restores 'func' to its correct value for debugging -** purposes. (It exchanges 'func' and 'extra'; so, when called again, -** after debugging, it also "re-restores" ** 'func' to its altered value. -*/ -static void swapextra (lua_State *L) { - if (L->status == LUA_YIELD) { - CallInfo *ci = L->ci; /* get function that yielded */ - StkId temp = ci->func; /* exchange its 'func' and 'extra' values */ - L->func = ci->func = restorestack(L, ci->extra); - ci->extra = savestack(L, temp); - } -} - - /* ** This function can be called asynchronously (e.g. during a signal). ** Fields 'oldpc', 'basehookcount', and 'hookcount' (set by @@ -209,7 +193,6 @@ static const char *findlocal (lua_State *L, CallInfo *ci, int n, LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) { const char *name; lua_lock(L); - swapextra(L); if (ar == NULL) { /* information about non-active function? */ if (!isLfunction(s2v(L->top - 1))) /* not a Lua function? */ name = NULL; @@ -224,7 +207,6 @@ LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) { api_incr_top(L); } } - swapextra(L); lua_unlock(L); return name; } @@ -234,13 +216,11 @@ LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) { StkId pos = NULL; /* to avoid warnings */ const char *name; lua_lock(L); - swapextra(L); name = findlocal(L, ar->i_ci, n, &pos); if (name) { setobjs2s(L, pos, L->top - 1); L->top--; /* pop value */ } - swapextra(L); lua_unlock(L); return name; } @@ -361,7 +341,6 @@ LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) { CallInfo *ci; TValue *func; lua_lock(L); - swapextra(L); if (*what == '>') { ci = NULL; func = s2v(L->top - 1); @@ -380,7 +359,6 @@ LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) { setobj2s(L, L->top, func); api_incr_top(L); } - swapextra(L); /* correct before option 'L', which can raise a mem. error */ if (strchr(what, 'L')) collectvalidlines(L, cl); lua_unlock(L); @@ -790,7 +768,6 @@ void luaG_traceexec (lua_State *L) { L->hookcount = 1; /* undo decrement to zero */ ci->u.l.savedpc--; /* undo increment (resume will increment it again) */ ci->callstatus |= CIST_HOOKYIELD; /* mark that it yielded */ - L->func = ci->func = L->top - 1; /* protect stack below results */ luaD_throw(L, LUA_YIELD); } } diff --git a/ldo.c b/ldo.c index 46d6dcecfd..8c2fca1343 100644 --- a/ldo.c +++ b/ldo.c @@ -1,5 +1,5 @@ /* -** $Id: ldo.c,v 2.163 2017/10/31 17:54:35 roberto Exp roberto $ +** $Id: ldo.c,v 2.164 2017/11/01 18:20:48 roberto Exp roberto $ ** Stack and Call structure of Lua ** See Copyright Notice in lua.h */ @@ -559,7 +559,7 @@ static int recover (lua_State *L, int status) { CallInfo *ci = findpcall(L); if (ci == NULL) return 0; /* no recovery point */ /* "finish" luaD_pcall */ - oldtop = restorestack(L, ci->extra); + oldtop = restorestack(L, ci->u2.funcidx); luaF_close(L, oldtop); seterrorobj(L, status, oldtop); L->ci = ci; @@ -604,7 +604,6 @@ static void resume (lua_State *L, void *ud) { else { /* resuming from previous yield */ lua_assert(L->status == LUA_YIELD); L->status = LUA_OK; /* mark that it is running (again) */ - L->func = ci->func = restorestack(L, ci->extra); if (isLua(ci)) /* yielded inside a hook? */ luaV_execute(L); /* just continue running Lua code */ else { /* 'common' yield */ @@ -622,7 +621,8 @@ static void resume (lua_State *L, void *ud) { } -LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs) { +LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs, + int *nresults) { int status; unsigned short oldnny = L->nny; /* save "number of non-yieldable" calls */ lua_lock(L); @@ -653,6 +653,8 @@ LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs) { } else lua_assert(status == L->status); /* normal end or yield */ } + *nresults = (status == LUA_YIELD) ? L->ci->u2.nyield + : L->top - (L->func + 1); L->nny = oldnny; /* restore 'nny' */ L->nCcalls--; lua_assert(L->nCcalls == ((from) ? from->nCcalls : 0)); @@ -679,14 +681,14 @@ LUA_API int lua_yieldk (lua_State *L, int nresults, lua_KContext ctx, luaG_runerror(L, "attempt to yield from outside a coroutine"); } L->status = LUA_YIELD; - ci->extra = savestack(L, L->func); /* save current 'func' */ if (isLua(ci)) { /* inside a hook? */ api_check(L, k == NULL, "hooks cannot continue after yielding"); + ci->u2.nyield = 0; /* no results */ } else { if ((ci->u.c.k = k) != NULL) /* is there a continuation? */ ci->u.c.ctx = ctx; /* save context */ - L->func = ci->func = L->top - nresults - 1; /* protect stack below results */ + ci->u2.nyield = nresults; /* save number of results */ luaD_throw(L, LUA_YIELD); } lua_assert(ci->callstatus & CIST_HOOKED); /* must be inside a hook */ diff --git a/lstate.h b/lstate.h index 76ce110fa9..8b9eb60627 100644 --- a/lstate.h +++ b/lstate.h @@ -1,5 +1,5 @@ /* -** $Id: lstate.h,v 2.144 2017/07/27 13:50:16 roberto Exp roberto $ +** $Id: lstate.h,v 2.145 2017/10/31 17:54:35 roberto Exp roberto $ ** Global State ** See Copyright Notice in lua.h */ @@ -83,12 +83,6 @@ typedef struct stringtable { /* ** Information about a call. -** When a thread yields, 'func' is adjusted to pretend that the -** top function has only the yielded values in its stack; in that -** case, the actual 'func' value is saved in field 'extra'. -** When a function calls another with a continuation, 'extra' keeps -** the function index so that, in case of errors, the continuation -** function can be called with the correct top. */ typedef struct CallInfo { StkId func; /* function index in the stack */ @@ -104,7 +98,10 @@ typedef struct CallInfo { lua_KContext ctx; /* context info. in case of yields */ } c; } u; - ptrdiff_t extra; + union { + ptrdiff_t funcidx; /* called-function index */ + int nyield; /* number of values yielded */ + } u2; short nresults; /* expected number of results from this function */ unsigned short callstatus; } CallInfo; diff --git a/ltests.c b/ltests.c index 94b4cd3100..d05fc3b179 100644 --- a/ltests.c +++ b/ltests.c @@ -1,5 +1,5 @@ /* -** $Id: ltests.c,v 2.225 2017/10/04 21:56:32 roberto Exp roberto $ +** $Id: ltests.c,v 2.226 2017/11/01 18:20:48 roberto Exp roberto $ ** Internal Module for Debugging of the Lua Implementation ** See Copyright Notice in lua.h */ @@ -309,13 +309,10 @@ static void checkLclosure (global_State *g, LClosure *cl) { } -static int lua_checkpc (lua_State *L, CallInfo *ci) { +static int lua_checkpc (CallInfo *ci) { if (!isLua(ci)) return 1; else { - /* if function yielded (inside a hook), real 'func' is in 'extra' field */ - StkId f = (L->status != LUA_YIELD || ci != L->ci) - ? ci->func - : restorestack(L, ci->extra); + StkId f = ci->func; Proto *p = clLvalue(s2v(f))->p; return p->code <= ci->u.l.savedpc && ci->u.l.savedpc <= p->code + p->sizecode; @@ -332,7 +329,7 @@ static void checkstack (global_State *g, lua_State *L1) { lua_assert(upisopen(uv)); /* must be open */ for (ci = L1->ci; ci != NULL; ci = ci->previous) { lua_assert(ci->top <= L1->stack_last); - lua_assert(lua_checkpc(L1, ci)); + lua_assert(lua_checkpc(ci)); } if (L1->stack) { /* complete thread? */ for (o = L1->stack; o < L1->stack_last + EXTRA_STACK; o++) @@ -1411,7 +1408,8 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) { } else if EQ("resume") { int i = getindex; - status = lua_resume(lua_tothread(L1, i), L, getnum); + int nres; + status = lua_resume(lua_tothread(L1, i), L, getnum, &nres); } else if EQ("return") { int n = getnum; @@ -1616,10 +1614,10 @@ static int sethook (lua_State *L) { static int coresume (lua_State *L) { - int status; + int status, nres; lua_State *co = lua_tothread(L, 1); luaL_argcheck(L, co, 1, "coroutine expected"); - status = lua_resume(co, L, 0); + status = lua_resume(co, L, 0, &nres); if (status != LUA_OK && status != LUA_YIELD) { lua_pushboolean(L, 0); lua_insert(L, -2); diff --git a/lua.h b/lua.h index eb76a27f9f..be99e14c09 100644 --- a/lua.h +++ b/lua.h @@ -1,5 +1,5 @@ /* -** $Id: lua.h,v 1.335 2017/05/26 19:14:29 roberto Exp roberto $ +** $Id: lua.h,v 1.336 2017/07/27 13:36:54 roberto Exp roberto $ ** Lua - A Scripting Language ** Lua.org, PUC-Rio, Brazil (http://www.lua.org) ** See Copyright Notice at the end of this file @@ -288,7 +288,8 @@ LUA_API int (lua_dump) (lua_State *L, lua_Writer writer, void *data, int strip); */ LUA_API int (lua_yieldk) (lua_State *L, int nresults, lua_KContext ctx, lua_KFunction k); -LUA_API int (lua_resume) (lua_State *L, lua_State *from, int narg); +LUA_API int (lua_resume) (lua_State *L, lua_State *from, int narg, + int *nres); LUA_API int (lua_status) (lua_State *L); LUA_API int (lua_isyieldable) (lua_State *L); From 54eb35a8aa0f60265cf1b4764beabe1199d66f42 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 3 Nov 2017 10:12:30 -0200 Subject: [PATCH 0102/1145] more fields moved out of 'CallInfo' --- lapi.c | 45 +++++++++++++++++-------------- lapi.h | 7 ++--- ldebug.c | 29 ++++++++++---------- ldo.c | 80 ++++++++++++++++++++++++++++++------------------------- lgc.c | 6 ++--- lobject.h | 13 ++++++++- lstate.c | 6 ++--- lstate.h | 22 +++++++-------- ltests.c | 11 ++++---- ltm.c | 6 ++--- lvm.c | 54 ++++++++++++++++++++----------------- 11 files changed, 152 insertions(+), 127 deletions(-) diff --git a/lapi.c b/lapi.c index 8d4d5e7d23..6b6bc56550 100644 --- a/lapi.c +++ b/lapi.c @@ -1,5 +1,5 @@ /* -** $Id: lapi.c,v 2.272 2017/11/01 18:20:48 roberto Exp roberto $ +** $Id: lapi.c,v 2.273 2017/11/02 11:28:56 roberto Exp roberto $ ** Lua API ** See Copyright Notice in lua.h */ @@ -10,6 +10,7 @@ #include "lprefix.h" +#include #include #include @@ -60,12 +61,12 @@ const char lua_ident[] = static TValue *index2value (lua_State *L, int idx) { if (idx > 0) { StkId o = L->func + idx; - api_check(L, idx <= L->ci->top - (L->func + 1), "unacceptable index"); + api_check(L, idx < L->func->stkci.framesize, "unacceptable index"); if (o >= L->top) return NONVALIDVALUE; else return s2v(o); } else if (!ispseudo(idx)) { /* negative index */ - api_check(L, idx != 0 && -idx <= L->top - (L->func + 1), "invalid index"); + api_check(L, idx != 0 && -idx < L->func->stkci.framesize, "invalid index"); return s2v(L->top + idx); } else if (idx == LUA_REGISTRYINDEX) @@ -109,10 +110,12 @@ static void growstack (lua_State *L, void *ud) { LUA_API int lua_checkstack (lua_State *L, int n) { int res; - CallInfo *ci = L->ci; + int frameuse = L->top - L->func; lua_lock(L); api_check(L, n >= 0, "negative 'n'"); - if (L->stack_last - L->top > n) /* stack large enough? */ + if (n >= USHRT_MAX - frameuse) + res = 0; /* frame size overflow */ + else if (L->stack_last - L->top > n) /* stack large enough? */ res = 1; /* yes; check is OK */ else { /* no; need to grow stack */ int inuse = cast_int(L->top - L->stack) + EXTRA_STACK; @@ -121,8 +124,8 @@ LUA_API int lua_checkstack (lua_State *L, int n) { else /* try to grow stack */ res = (luaD_rawrunprotected(L, &growstack, &n) == LUA_OK); } - if (res && ci->top < L->top + n) - ci->top = L->top + n; /* adjust frame top */ + if (res && L->func->stkci.framesize < frameuse + n) + L->func->stkci.framesize = frameuse + n; /* adjust frame size */ lua_unlock(L); return res; } @@ -134,7 +137,7 @@ LUA_API void lua_xmove (lua_State *from, lua_State *to, int n) { lua_lock(to); api_checknelems(from, n); api_check(from, G(from) == G(to), "moving among independent states"); - api_check(from, to->ci->top - to->top >= n, "stack overflow"); + api_check(from, functop(to->func) - to->top >= n, "stack overflow"); from->top -= n; for (i = 0; i < n; i++) { setobjs2s(to, to->top, from->top + i); @@ -929,15 +932,16 @@ LUA_API void lua_setuservalue (lua_State *L, int idx) { #define checkresults(L,na,nr) \ - api_check(L, (nr) == LUA_MULTRET || (L->ci->top - L->top >= (nr) - (na)), \ - "results from function overflow current stack size") + api_check(L, (nr) == LUA_MULTRET || \ + (functop(L->func) - L->top >= (nr) - (na)), \ + "results from function overflow current frame size") LUA_API void lua_callk (lua_State *L, int nargs, int nresults, lua_KContext ctx, lua_KFunction k) { StkId func; lua_lock(L); - api_check(L, k == NULL || !isLua(L->ci), + api_check(L, k == NULL || !isLua(L->func), "cannot use continuations inside hooks"); api_checknelems(L, nargs+1); api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread"); @@ -976,36 +980,37 @@ LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc, lua_KContext ctx, lua_KFunction k) { struct CallS c; int status; - ptrdiff_t func; + ptrdiff_t efunc; lua_lock(L); - api_check(L, k == NULL || !isLua(L->ci), + api_check(L, k == NULL || !isLua(L->func), "cannot use continuations inside hooks"); api_checknelems(L, nargs+1); api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread"); checkresults(L, nargs, nresults); if (errfunc == 0) - func = 0; + efunc = 0; else { StkId o = index2stack(L, errfunc); - func = savestack(L, o); + efunc = savestack(L, o); } c.func = L->top - (nargs+1); /* function to be called */ if (k == NULL || L->nny > 0) { /* no continuation or no yieldable? */ c.nresults = nresults; /* do a 'conventional' protected call */ - status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func); + status = luaD_pcall(L, f_call, &c, savestack(L, c.func), efunc); } else { /* prepare continuation (call is already protected by 'resume') */ CallInfo *ci = L->ci; + StkId func = L->func; ci->u.c.k = k; /* save continuation */ ci->u.c.ctx = ctx; /* save context */ /* save information for error recovery */ ci->u2.funcidx = savestack(L, c.func); ci->u.c.old_errfunc = L->errfunc; - L->errfunc = func; - setoah(ci->callstatus, L->allowhook); /* save value of 'allowhook' */ - ci->callstatus |= CIST_YPCALL; /* function can do error recovery */ + L->errfunc = efunc; + setoah(callstatus(func), L->allowhook); /* save value of 'allowhook' */ + callstatus(func) |= CIST_YPCALL; /* function can do error recovery */ luaD_call(L, c.func, nresults); /* do the call */ - ci->callstatus &= ~CIST_YPCALL; + callstatus(func) &= ~CIST_YPCALL; L->errfunc = ci->u.c.old_errfunc; status = LUA_OK; /* if it is here, there were no errors */ } diff --git a/lapi.h b/lapi.h index ce64a7274c..d851d161f6 100644 --- a/lapi.h +++ b/lapi.h @@ -1,5 +1,5 @@ /* -** $Id: lapi.h,v 2.9 2015/03/06 19:49:50 roberto Exp roberto $ +** $Id: lapi.h,v 2.10 2017/11/01 18:20:48 roberto Exp roberto $ ** Auxiliary functions from Lua API ** See Copyright Notice in lua.h */ @@ -11,11 +11,12 @@ #include "llimits.h" #include "lstate.h" -#define api_incr_top(L) {L->top++; api_check(L, L->top <= L->ci->top, \ +#define api_incr_top(L) {L->top++; api_check(L, L->top <= functop(L->func), \ "stack overflow");} #define adjustresults(L,nres) \ - { if ((nres) == LUA_MULTRET && L->ci->top < L->top) L->ci->top = L->top; } + { if ((nres) == LUA_MULTRET && functop(L->func) < L->top) \ + setfunctop(L->func, L->top); } #define api_checknelems(L,n) api_check(L, (n) < (L->top - L->func), \ "not enough elements in the stack") diff --git a/ldebug.c b/ldebug.c index 5ca78bd11e..2db0910c72 100644 --- a/ldebug.c +++ b/ldebug.c @@ -1,5 +1,5 @@ /* -** $Id: ldebug.c,v 2.134 2017/11/01 18:20:48 roberto Exp roberto $ +** $Id: ldebug.c,v 2.135 2017/11/02 11:28:56 roberto Exp roberto $ ** Debug Interface ** See Copyright Notice in lua.h */ @@ -43,7 +43,7 @@ static const char *funcnamefromcode (lua_State *L, CallInfo *ci, static int currentpc (CallInfo *ci) { - lua_assert(isLua(ci)); + lua_assert(isLua(ci->func)); return pcRel(ci->u.l.savedpc, ci_func(ci)->p); } @@ -120,7 +120,7 @@ LUA_API void lua_sethook (lua_State *L, lua_Hook func, int mask, int count) { mask = 0; func = NULL; } - if (isLua(L->ci)) + if (isLua(L->func)) L->oldpc = L->ci->u.l.savedpc; L->hook = func; L->basehookcount = count; @@ -172,7 +172,7 @@ static const char *findlocal (lua_State *L, CallInfo *ci, int n, StkId *pos) { const char *name = NULL; StkId base; - if (isLua(ci)) { + if (isLua(ci->func)) { base = ci->func + 1; name = luaF_getlocalname(ci_func(ci)->p, n, currentpc(ci)); } @@ -277,12 +277,12 @@ static void collectvalidlines (lua_State *L, Closure *f) { static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) { if (ci == NULL) /* no 'ci'? */ return NULL; /* no info */ - else if (ci->callstatus & CIST_FIN) { /* is this a finalizer? */ + else if (callstatus(ci->func) & CIST_FIN) { /* is this a finalizer? */ *name = "__gc"; return "metamethod"; /* report it as such */ } /* calling function is a known Lua function? */ - else if (!(ci->callstatus & CIST_TAIL) && isLua(ci->previous)) + else if (!(callstatus(ci->func) & CIST_TAIL) && isLua(ci->previous->func)) return funcnamefromcode(L, ci->previous, name); else return NULL; /* no way to find a name */ } @@ -298,7 +298,7 @@ static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar, break; } case 'l': { - ar->currentline = (ci && isLua(ci)) ? currentline(ci) : -1; + ar->currentline = (ci && isLua(ci->func)) ? currentline(ci) : -1; break; } case 'u': { @@ -314,7 +314,7 @@ static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar, break; } case 't': { - ar->istailcall = (ci) ? ci->callstatus & CIST_TAIL : 0; + ar->istailcall = (ci) ? callstatus(ci->func) & CIST_TAIL : 0; break; } case 'n': { @@ -549,7 +549,7 @@ static const char *funcnamefromcode (lua_State *L, CallInfo *ci, Proto *p = ci_func(ci)->p; /* calling function */ int pc = currentpc(ci); /* calling instruction index */ Instruction i = p->code[pc]; /* calling instruction */ - if (ci->callstatus & CIST_HOOKED) { /* was it called inside a hook? */ + if (callstatus(ci->func) & CIST_HOOKED) { /* was it called inside a hook? */ *name = "?"; return "hook"; } @@ -635,7 +635,7 @@ static const char *varinfo (lua_State *L, const TValue *o) { const char *name = NULL; /* to avoid warnings */ CallInfo *ci = L->ci; const char *kind = NULL; - if (isLua(ci)) { + if (isLua(L->func)) { kind = getupvalname(ci, o, &name); /* check whether 'o' is an upvalue */ if (!kind && isinstack(L, o)) /* no? try a register */ kind = getobjname(ci_func(ci)->p, currentpc(ci), @@ -719,7 +719,7 @@ l_noret luaG_runerror (lua_State *L, const char *fmt, ...) { va_start(argp, fmt); msg = luaO_pushvfstring(L, fmt, argp); /* format message */ va_end(argp); - if (isLua(ci)) /* if Lua function, add source:line information */ + if (isLua(L->func)) /* if Lua function, add source:line information */ luaG_addinfo(L, msg, ci_func(ci)->p->source, currentline(ci)); luaG_errormsg(L); } @@ -740,14 +740,15 @@ static int changedline (Proto *p, int oldpc, int newpc) { void luaG_traceexec (lua_State *L) { CallInfo *ci = L->ci; + StkId func = L->func; lu_byte mask = L->hookmask; int counthook = (--L->hookcount == 0 && (mask & LUA_MASKCOUNT)); if (counthook) resethookcount(L); /* reset count */ else if (!(mask & LUA_MASKLINE)) return; /* no line hook and count != 0; nothing to be done */ - if (ci->callstatus & CIST_HOOKYIELD) { /* called hook last time? */ - ci->callstatus &= ~CIST_HOOKYIELD; /* erase mark */ + if (callstatus(func) & CIST_HOOKYIELD) { /* called hook last time? */ + callstatus(func) &= ~CIST_HOOKYIELD; /* erase mark */ return; /* do not call hook again (VM yielded, so it did not move) */ } if (counthook) @@ -767,7 +768,7 @@ void luaG_traceexec (lua_State *L) { if (counthook) L->hookcount = 1; /* undo decrement to zero */ ci->u.l.savedpc--; /* undo increment (resume will increment it again) */ - ci->callstatus |= CIST_HOOKYIELD; /* mark that it yielded */ + callstatus(func) |= CIST_HOOKYIELD; /* mark that it yielded */ luaD_throw(L, LUA_YIELD); } } diff --git a/ldo.c b/ldo.c index 8c2fca1343..cbb1e0f3a5 100644 --- a/ldo.c +++ b/ldo.c @@ -1,5 +1,5 @@ /* -** $Id: ldo.c,v 2.164 2017/11/01 18:20:48 roberto Exp roberto $ +** $Id: ldo.c,v 2.165 2017/11/02 11:28:56 roberto Exp roberto $ ** Stack and Call structure of Lua ** See Copyright Notice in lua.h */ @@ -123,8 +123,8 @@ l_noret luaD_throw (lua_State *L, int errcode) { else { /* no handler at all; abort */ if (g->panic) { /* panic function? */ seterrorobj(L, errcode, L->top); /* assume EXTRA_STACK */ - if (L->ci->top < L->top) - L->ci->top = L->top; /* pushing msg. can break this invariant */ + if (functop(L->func) < L->top) /* check invariant */ + setfunctop(L->func, L->top); lua_unlock(L); g->panic(L); /* call panic function (last chance to jump out) */ } @@ -164,7 +164,6 @@ static void correctstack (lua_State *L, StkId oldstack) { for (up = L->openupval; up != NULL; up = up->u.open.next) up->v = s2v((uplevel(up) - oldstack) + L->stack); for (ci = L->ci; ci != NULL; ci = ci->previous) { - ci->top = (ci->top - oldstack) + L->stack; ci->func = (ci->func - oldstack) + L->stack; } } @@ -208,10 +207,11 @@ void luaD_growstack (lua_State *L, int n) { static int stackinuse (lua_State *L) { - CallInfo *ci; StkId lim = L->top; - for (ci = L->ci; ci != NULL; ci = ci->previous) { - if (lim < ci->top) lim = ci->top; + StkId func = L->func; + for (; func->stkci.previous != 0; func -= func->stkci.previous) { + if (lim < functop(func)) + lim = functop(func); } lua_assert(lim <= L->stack_last); return cast_int(lim - L->stack) + 1; /* part of stack in use */ @@ -255,34 +255,38 @@ void luaD_hook (lua_State *L, int event, int line) { if (hook && L->allowhook) { /* make sure there is a hook */ CallInfo *ci = L->ci; ptrdiff_t top = savestack(L, L->top); - ptrdiff_t ci_top = savestack(L, ci->top); + int origframesize = L->func->stkci.framesize; + int tmpframesize; /* frame size to run hook */ lua_Debug ar; ar.event = event; ar.currentline = line; ar.i_ci = ci; luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */ - ci->top = L->top + LUA_MINSTACK; - lua_assert(ci->top <= L->stack_last); + tmpframesize = L->top - L->func + LUA_MINSTACK; + if (tmpframesize > origframesize) /* need to grow frame? */ + L->func->stkci.framesize = tmpframesize; + lua_assert(functop(L->func) <= L->stack_last); L->allowhook = 0; /* cannot call hooks inside a hook */ - ci->callstatus |= CIST_HOOKED; + callstatus(L->func) |= CIST_HOOKED; lua_unlock(L); (*hook)(L, &ar); lua_lock(L); lua_assert(!L->allowhook); L->allowhook = 1; - ci->top = restorestack(L, ci_top); + L->func->stkci.framesize = origframesize; L->top = restorestack(L, top); - ci->callstatus &= ~CIST_HOOKED; + callstatus(L->func) &= ~CIST_HOOKED; } } static void callhook (lua_State *L, CallInfo *ci) { int hook = LUA_HOOKCALL; + StkId previous = L->func - L->func->stkci.previous; ci->u.l.savedpc++; /* hooks assume 'pc' is already incremented */ - if (isLua(ci->previous) && + if (isLua(previous) && GET_OPCODE(*(ci->previous->u.l.savedpc - 1)) == OP_TAILCALL) { - ci->callstatus |= CIST_TAIL; + callstatus(L->func) |= CIST_TAIL; hook = LUA_HOOKTAILCALL; } luaD_hook(L, hook, -1); @@ -358,8 +362,8 @@ static int moveresults (lua_State *L, StkId firstResult, StkId res, ** wanted multiple (variable number of) results. */ int luaD_poscall (lua_State *L, CallInfo *ci, StkId firstResult, int nres) { - StkId res; - int wanted = ci->nresults; + StkId res = L->func; /* res == final position of 1st result */ + int wanted = res->stkci.nresults; if (L->hookmask & (LUA_MASKRET | LUA_MASKLINE)) { if (L->hookmask & LUA_MASKRET) { ptrdiff_t fr = savestack(L, firstResult); /* hook may change stack */ @@ -368,7 +372,6 @@ int luaD_poscall (lua_State *L, CallInfo *ci, StkId firstResult, int nres) { } L->oldpc = ci->previous->u.l.savedpc; /* 'oldpc' for caller function */ } - res = L->func; /* res == final position of 1st result */ L->ci = ci->previous; /* back to caller */ L->func -= L->func->stkci.previous; lua_assert(L->func == L->ci->func); @@ -402,12 +405,12 @@ int luaD_precall (lua_State *L, StkId func, int nresults) { int n; /* number of returns */ checkstackp(L, LUA_MINSTACK, func); /* ensure minimum stack size */ ci = next_ci(L); /* now 'enter' new function */ - ci->nresults = nresults; + func->stkci.nresults = nresults; func->stkci.previous = func - L->func; L->func = ci->func = func; - ci->top = L->top + LUA_MINSTACK; - lua_assert(ci->top <= L->stack_last); - ci->callstatus = 0; + setfunctop(func, L->top + LUA_MINSTACK); + lua_assert(functop(func) <= L->stack_last); + callstatus(func) = 0; if (L->hookmask & LUA_MASKCALL) luaD_hook(L, LUA_HOOKCALL, -1); lua_unlock(L); @@ -427,13 +430,14 @@ int luaD_precall (lua_State *L, StkId func, int nresults) { if (p->is_vararg) luaT_adjustvarargs(L, p, n); ci = next_ci(L); /* now 'enter' new function */ - ci->nresults = nresults; + func->stkci.nresults = nresults; func->stkci.previous = func - L->func; + func->stkci.framesize = fsize + 1; /* size includes function itself */ L->func = ci->func = func; - L->top = ci->top = func + 1 + fsize; - lua_assert(ci->top <= L->stack_last); + L->top = func + 1 + fsize; + lua_assert(functop(func) <= L->stack_last); ci->u.l.savedpc = p->code; /* starting point */ - ci->callstatus = CIST_LUA; + callstatus(func) = 0; if (L->hookmask & LUA_MASKCALL) callhook(L, ci); return 0; @@ -493,18 +497,19 @@ void luaD_callnoyield (lua_State *L, StkId func, int nResults) { */ static void finishCcall (lua_State *L, int status) { CallInfo *ci = L->ci; + StkId func = L->func; int n; /* must have a continuation and must be able to call it */ lua_assert(ci->u.c.k != NULL && L->nny == 0); /* error status can only happen in a protected call */ - lua_assert((ci->callstatus & CIST_YPCALL) || status == LUA_YIELD); - if (ci->callstatus & CIST_YPCALL) { /* was inside a pcall? */ - ci->callstatus &= ~CIST_YPCALL; /* continuation is also inside it */ + lua_assert((callstatus(func) & CIST_YPCALL) || status == LUA_YIELD); + if (callstatus(func) & CIST_YPCALL) { /* was inside a pcall? */ + callstatus(func) &= ~CIST_YPCALL; /* continuation is also inside it */ L->errfunc = ci->u.c.old_errfunc; /* with the same error function */ } /* finish 'lua_callk'/'lua_pcall'; CIST_YPCALL and 'errfunc' already handled */ - adjustresults(L, ci->nresults); + adjustresults(L, func->stkci.nresults); lua_unlock(L); n = (*ci->u.c.k)(L, status, ci->u.c.ctx); /* call continuation function */ lua_lock(L); @@ -525,7 +530,7 @@ static void unroll (lua_State *L, void *ud) { if (ud != NULL) /* error status? */ finishCcall(L, *(int *)ud); /* finish 'lua_pcallk' callee */ while (L->ci != &L->base_ci) { /* something in the stack */ - if (!isLua(L->ci)) /* C function? */ + if (!isLua(L->func)) /* C function? */ finishCcall(L, LUA_YIELD); /* complete its execution */ else { /* Lua function */ luaV_finishOp(L); /* finish interrupted instruction */ @@ -542,7 +547,7 @@ static void unroll (lua_State *L, void *ud) { static CallInfo *findpcall (lua_State *L) { CallInfo *ci; for (ci = L->ci; ci != NULL; ci = ci->previous) { /* search for a pcall */ - if (ci->callstatus & CIST_YPCALL) + if (callstatus(ci->func) & CIST_YPCALL) return ci; } return NULL; /* no pending pcall */ @@ -564,7 +569,7 @@ static int recover (lua_State *L, int status) { seterrorobj(L, status, oldtop); L->ci = ci; L->func = ci->func; - L->allowhook = getoah(ci->callstatus); /* restore original 'allowhook' */ + L->allowhook = getoah(callstatus(L->func)); /* restore original 'allowhook' */ L->nny = 0; /* should be zero to be yieldable */ luaD_shrinkstack(L); L->errfunc = ci->u.c.old_errfunc; @@ -604,7 +609,7 @@ static void resume (lua_State *L, void *ud) { else { /* resuming from previous yield */ lua_assert(L->status == LUA_YIELD); L->status = LUA_OK; /* mark that it is running (again) */ - if (isLua(ci)) /* yielded inside a hook? */ + if (isLua(L->func)) /* yielded inside a hook? */ luaV_execute(L); /* just continue running Lua code */ else { /* 'common' yield */ if (ci->u.c.k != NULL) { /* does it have a continuation function? */ @@ -649,7 +654,7 @@ LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs, if (errorstatus(status)) { /* unrecoverable error? */ L->status = cast_byte(status); /* mark thread as 'dead' */ seterrorobj(L, status, L->top); /* push error message */ - L->ci->top = L->top; + L->func->stkci.framesize = L->top - L->func; } else lua_assert(status == L->status); /* normal end or yield */ } @@ -671,6 +676,7 @@ LUA_API int lua_isyieldable (lua_State *L) { LUA_API int lua_yieldk (lua_State *L, int nresults, lua_KContext ctx, lua_KFunction k) { CallInfo *ci = L->ci; + StkId func = L->func; luai_userstateyield(L, nresults); lua_lock(L); api_checknelems(L, nresults); @@ -681,7 +687,7 @@ LUA_API int lua_yieldk (lua_State *L, int nresults, lua_KContext ctx, luaG_runerror(L, "attempt to yield from outside a coroutine"); } L->status = LUA_YIELD; - if (isLua(ci)) { /* inside a hook? */ + if (isLua(func)) { /* inside a hook? */ api_check(L, k == NULL, "hooks cannot continue after yielding"); ci->u2.nyield = 0; /* no results */ } @@ -691,7 +697,7 @@ LUA_API int lua_yieldk (lua_State *L, int nresults, lua_KContext ctx, ci->u2.nyield = nresults; /* save number of results */ luaD_throw(L, LUA_YIELD); } - lua_assert(ci->callstatus & CIST_HOOKED); /* must be inside a hook */ + lua_assert(callstatus(func) & CIST_HOOKED); /* must be inside a hook */ lua_unlock(L); return 0; /* return to 'luaD_hook' */ } diff --git a/lgc.c b/lgc.c index d9b3195878..2155b28d23 100644 --- a/lgc.c +++ b/lgc.c @@ -1,5 +1,5 @@ /* -** $Id: lgc.c,v 2.235 2017/10/11 12:38:45 roberto Exp roberto $ +** $Id: lgc.c,v 2.236 2017/10/31 15:29:28 roberto Exp roberto $ ** Garbage Collector ** See Copyright Notice in lua.h */ @@ -866,9 +866,9 @@ static void GCTM (lua_State *L, int propagateerrors) { setobj2s(L, L->top, tm); /* push finalizer... */ setobj2s(L, L->top + 1, &v); /* ... and its argument */ L->top += 2; /* and (next line) call the finalizer */ - L->ci->callstatus |= CIST_FIN; /* will run a finalizer */ + callstatus(L->func) |= CIST_FIN; /* will run a finalizer */ status = luaD_pcall(L, dothecall, NULL, savestack(L, L->top - 2), 0); - L->ci->callstatus &= ~CIST_FIN; /* not running a finalizer anymore */ + callstatus(L->func) &= ~CIST_FIN; /* not running a finalizer anymore */ L->allowhook = oldah; /* restore hooks */ g->gcrunning = running; /* restore state */ if (status != LUA_OK && propagateerrors) { /* error while running __gc? */ diff --git a/lobject.h b/lobject.h index e3ae005063..66c41e6f1d 100644 --- a/lobject.h +++ b/lobject.h @@ -1,5 +1,5 @@ /* -** $Id: lobject.h,v 2.125 2017/06/29 15:06:44 roberto Exp roberto $ +** $Id: lobject.h,v 2.126 2017/10/31 17:54:35 roberto Exp roberto $ ** Type definitions for Lua objects ** See Copyright Notice in lua.h */ @@ -313,10 +313,21 @@ typedef union StackValue { TValue val; struct { TValuefields; + lu_byte callstatus_; unsigned short previous; /* difference to previous 'func' */ + short nresults; /* expected number of results from this function */ + unsigned short framesize; /* stack space available for this function */ } stkci; } StackValue; +#define callstatus(ar) ((ar)->stkci.callstatus_) + +/* top of a function (first element after its frame) */ +#define functop(func) ((func) + (func)->stkci.framesize) + +/* set top of a function to a specific value */ +#define setfunctop(func,v) ((func)->stkci.framesize = (v) - (func)) + typedef StackValue *StkId; /* index to stack elements */ diff --git a/lstate.c b/lstate.c index c4305ed84e..8e3177ca99 100644 --- a/lstate.c +++ b/lstate.c @@ -1,5 +1,5 @@ /* -** $Id: lstate.c,v 2.142 2017/10/11 12:38:45 roberto Exp roberto $ +** $Id: lstate.c,v 2.143 2017/10/31 17:54:35 roberto Exp roberto $ ** Global State ** See Copyright Notice in lua.h */ @@ -152,12 +152,12 @@ static void stack_init (lua_State *L1, lua_State *L) { /* initialize first ci */ ci = &L1->base_ci; ci->next = ci->previous = NULL; - ci->callstatus = 0; L1->func = ci->func = L1->top; L1->func->stkci.previous = 0; /* end of linked list */ + L1->func->stkci.framesize = LUA_MINSTACK + 1; + callstatus(L1->func) = 0; setnilvalue(s2v(L1->top)); /* 'function' entry for this 'ci' */ L1->top++; - ci->top = L1->top + LUA_MINSTACK; L1->ci = ci; } diff --git a/lstate.h b/lstate.h index 8b9eb60627..3d76e9b95f 100644 --- a/lstate.h +++ b/lstate.h @@ -1,5 +1,5 @@ /* -** $Id: lstate.h,v 2.145 2017/10/31 17:54:35 roberto Exp roberto $ +** $Id: lstate.h,v 2.146 2017/11/02 11:28:56 roberto Exp roberto $ ** Global State ** See Copyright Notice in lua.h */ @@ -86,7 +86,6 @@ typedef struct stringtable { */ typedef struct CallInfo { StkId func; /* function index in the stack */ - StkId top; /* top for this function */ struct CallInfo *previous, *next; /* dynamic call link */ union { struct { /* only for Lua functions */ @@ -102,8 +101,6 @@ typedef struct CallInfo { ptrdiff_t funcidx; /* called-function index */ int nyield; /* number of values yielded */ } u2; - short nresults; /* expected number of results from this function */ - unsigned short callstatus; } CallInfo; @@ -111,17 +108,16 @@ typedef struct CallInfo { ** Bits in CallInfo status */ #define CIST_OAH (1<<0) /* original value of 'allowhook' */ -#define CIST_LUA (1<<1) /* call is running a Lua function */ -#define CIST_HOOKED (1<<2) /* call is running a debug hook */ -#define CIST_FRESH (1<<3) /* call is running on a fresh invocation +#define CIST_HOOKED (1<<1) /* call is running a debug hook */ +#define CIST_FRESH (1<<2) /* call is running on a fresh invocation of luaV_execute */ -#define CIST_YPCALL (1<<4) /* call is a yieldable protected call */ -#define CIST_TAIL (1<<5) /* call was tail called */ -#define CIST_HOOKYIELD (1<<6) /* last hook called yielded */ -#define CIST_LEQ (1<<7) /* using __lt for __le */ -#define CIST_FIN (1<<8) /* call is running a finalizer */ +#define CIST_YPCALL (1<<3) /* call is a yieldable protected call */ +#define CIST_TAIL (1<<4) /* call was tail called */ +#define CIST_HOOKYIELD (1<<5) /* last hook called yielded */ +#define CIST_LEQ (1<<6) /* using __lt for __le */ +#define CIST_FIN (1<<7) /* call is running a finalizer */ -#define isLua(ci) ((ci)->callstatus & CIST_LUA) +#define isLua(func) isLfunction(s2v(func)) /* assume that CIST_OAH has offset 0 and that 'v' is strictly 0/1 */ #define setoah(st,v) ((st) = ((st) & ~CIST_OAH) | (v)) diff --git a/ltests.c b/ltests.c index d05fc3b179..71ad09ea53 100644 --- a/ltests.c +++ b/ltests.c @@ -1,5 +1,5 @@ /* -** $Id: ltests.c,v 2.226 2017/11/01 18:20:48 roberto Exp roberto $ +** $Id: ltests.c,v 2.227 2017/11/02 11:28:56 roberto Exp roberto $ ** Internal Module for Debugging of the Lua Implementation ** See Copyright Notice in lua.h */ @@ -310,7 +310,7 @@ static void checkLclosure (global_State *g, LClosure *cl) { static int lua_checkpc (CallInfo *ci) { - if (!isLua(ci)) return 1; + if (!isLua(ci->func)) return 1; else { StkId f = ci->func; Proto *p = clLvalue(s2v(f))->p; @@ -327,10 +327,11 @@ static void checkstack (global_State *g, lua_State *L1) { lua_assert(!isdead(g, L1)); for (uv = L1->openupval; uv != NULL; uv = uv->u.open.next) lua_assert(upisopen(uv)); /* must be open */ - for (ci = L1->ci; ci != NULL; ci = ci->previous) { - lua_assert(ci->top <= L1->stack_last); + for (ci = L1->ci; ci != NULL; ci = ci->previous) lua_assert(lua_checkpc(ci)); - } + for (o = L1->func; o->stkci.previous != 0; o -= o->stkci.previous) + lua_assert(functop(o) <= L1->stack_last); + lua_assert(o == L1->stack); if (L1->stack) { /* complete thread? */ for (o = L1->stack; o < L1->stack_last + EXTRA_STACK; o++) checkliveness(L1, s2v(o)); /* entire stack must have valid values */ diff --git a/ltm.c b/ltm.c index 1534992b37..6c13e8d132 100644 --- a/ltm.c +++ b/ltm.c @@ -1,5 +1,5 @@ /* -** $Id: ltm.c,v 2.44 2017/09/27 18:59:08 roberto Exp roberto $ +** $Id: ltm.c,v 2.45 2017/10/04 15:49:05 roberto Exp roberto $ ** Tag methods ** See Copyright Notice in lua.h */ @@ -108,7 +108,7 @@ void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1, setobj2s(L, func + 3, p3); /* 3rd argument */ L->top += 4; /* metamethod may yield only when called from Lua code */ - if (isLua(L->ci)) + if (isLua(L->func)) luaD_call(L, func, 0); else luaD_callnoyield(L, func, 0); @@ -124,7 +124,7 @@ void luaT_callTMres (lua_State *L, const TValue *f, const TValue *p1, setobj2s(L, func + 2, p2); /* 2nd argument */ L->top += 3; /* metamethod may yield only when called from Lua code */ - if (isLua(L->ci)) + if (isLua(L->func)) luaD_call(L, func, 1); else luaD_callnoyield(L, func, 1); diff --git a/lvm.c b/lvm.c index 25b953a4c0..7acb387f21 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.300 2017/10/31 17:54:35 roberto Exp roberto $ +** $Id: lvm.c,v 2.301 2017/11/01 18:20:48 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -390,9 +390,9 @@ int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r) { else if ((res = luaT_callorderTM(L, l, r, TM_LE)) >= 0) /* try 'le' */ return res; else { /* try 'lt': */ - L->ci->callstatus |= CIST_LEQ; /* mark it is doing 'lt' for 'le' */ + callstatus(L->func) |= CIST_LEQ; /* mark it is doing 'lt' for 'le' */ res = luaT_callorderTM(L, r, l, TM_LT); - L->ci->callstatus ^= CIST_LEQ; /* clear mark */ + callstatus(L->func) ^= CIST_LEQ; /* clear mark */ if (res < 0) luaG_ordererror(L, l, r); return !res; /* result is negated */ @@ -679,9 +679,9 @@ void luaV_finishOp (lua_State *L) { case OP_LE: case OP_LT: case OP_EQ: { int res = !l_isfalse(s2v(L->top - 1)); L->top--; - if (ci->callstatus & CIST_LEQ) { /* "<=" using "<" instead? */ + if (callstatus(base - 1) & CIST_LEQ) { /* "<=" using "<" ? */ lua_assert(op == OP_LE); - ci->callstatus ^= CIST_LEQ; /* clear mark */ + callstatus(base - 1) ^= CIST_LEQ; /* clear mark */ res = !res; /* negate result */ } lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_JMP); @@ -700,17 +700,17 @@ void luaV_finishOp (lua_State *L) { } /* move final result to final position */ setobjs2s(L, L->func + 1 + GETARG_A(inst), L->top - 1); - L->top = ci->top; /* restore top */ + L->top = functop(base - 1); /* restore top */ break; } case OP_TFORCALL: { lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_TFORLOOP); - L->top = ci->top; /* correct top */ + L->top = functop(base - 1); /* correct top */ break; } case OP_CALL: { if (GETARG_C(inst) - 1 >= 0) /* nresults >= 0? */ - L->top = ci->top; /* adjust results */ + L->top = functop(base - 1); /* adjust results */ break; } case OP_TAILCALL: case OP_SETTABUP: case OP_SETTABLE: @@ -775,9 +775,9 @@ void luaV_finishOp (lua_State *L) { #define checkGC(L,c) \ - { luaC_condGC(L, L->top = (c), /* limit of live values */ \ - Protect(L->top = ci->top)); /* restore top */ \ - luai_threadyield(L); } + { luaC_condGC(L, L->top = (c), /* limit of live values */ \ + {Protect((void)0); L->top = functop(base - 1);}); /* restore top */ \ + luai_threadyield(L); } /* fetch an instruction and prepare its execution */ @@ -796,16 +796,15 @@ void luaV_execute (lua_State *L) { CallInfo *ci = L->ci; LClosure *cl; TValue *k; - StkId base; /* local copy of 'L->func + 1' */ + StkId base = L->func + 1; /* local copy of 'L->func + 1' */ int mask; /* local copy of 'L->hookmask & (LUA_MASKLINE | LUA_MASKCOUNT)' */ const Instruction *pc; /* local copy of 'ci->u.l.savedpc' */ - ci->callstatus |= CIST_FRESH; /* fresh invocation of 'luaV_execute" */ + callstatus(base - 1) |= CIST_FRESH; /* fresh invocation of 'luaV_execute" */ newframe: /* reentry point when frame changes (call/return) */ lua_assert(ci == L->ci); cl = clLvalue(s2v(L->func)); /* local reference to function's closure */ k = cl->p->k; /* local reference to function's constant table */ updatemask(L); - base = L->func + 1; pc = ci->u.l.savedpc; /* main loop of interpreter */ for (;;) { @@ -1276,7 +1275,7 @@ void luaV_execute (lua_State *L) { rb = base + b; setobjs2s(L, ra, rb); checkGC(L, (ra >= rb ? ra + 1 : rb)); - L->top = ci->top; /* restore top */ + L->top = functop(base - 1); /* restore top */ vmbreak; } vmcase(OP_CLOSE) { @@ -1355,11 +1354,12 @@ void luaV_execute (lua_State *L) { Protect(isC = luaD_precall(L, ra, nresults)); if (isC) { /* C function? */ if (nresults >= 0) /* fixed number of results? */ - L->top = ci->top; /* correct top */ + L->top = functop(base - 1); /* correct top */ /* else leave top for next instruction */ } else { /* Lua function */ ci = L->ci; + base = L->func + 1; goto newframe; /* restart luaV_execute over new Lua function */ } vmbreak; @@ -1386,12 +1386,14 @@ void luaV_execute (lua_State *L) { /* move new frame into old one */ for (aux = 0; nfunc + aux < lim; aux++) setobjs2s(L, ofunc + aux, nfunc + aux); - oci->top = L->top = ofunc + (L->top - nfunc); /* correct top */ + ofunc->stkci.framesize = L->top - nfunc; + L->top = functop(ofunc); /* correct top */ oci->u.l.savedpc = nci->u.l.savedpc; - oci->callstatus |= CIST_TAIL; /* function was tail called */ + callstatus(ofunc) |= CIST_TAIL; /* function was tail called */ ci = L->ci = oci; /* remove new frame */ + base = ofunc + 1; L->func = ofunc; - lua_assert(L->top == ofunc + 1 + getproto(s2v(ofunc))->maxstacksize); + lua_assert(L->top == base + getproto(s2v(ofunc))->maxstacksize); goto newframe; /* restart luaV_execute over new Lua function */ } vmbreak; @@ -1401,14 +1403,15 @@ void luaV_execute (lua_State *L) { if (cl->p->sizep > 0) luaF_close(L, base); savepc(L); b = luaD_poscall(L, ci, ra, (b != 0 ? b - 1 : cast_int(L->top - ra))); - if (ci->callstatus & CIST_FRESH) /* local 'ci' still from callee */ + if (callstatus(base - 1) & CIST_FRESH) /* local 'base' still from callee */ return; /* external invocation: return */ else { /* invocation via reentry: continue execution */ ci = L->ci; - if (b) L->top = ci->top; - lua_assert(isLua(ci)); + base = L->func + 1; + if (b) L->top = functop(base - 1); + lua_assert(isLua(L->func)); lua_assert(GET_OPCODE(*((ci)->u.l.savedpc - 1)) == OP_CALL); - goto newframe; /* restart luaV_execute over new Lua function */ + goto newframe; /* restart luaV_execute over previous Lua function */ } } vmcase(OP_FORLOOP) { @@ -1473,7 +1476,7 @@ void luaV_execute (lua_State *L) { setobjs2s(L, cb, ra); L->top = cb + 3; /* func. + 2 args (state and index) */ Protect(luaD_call(L, cb, GETARG_C(i))); - L->top = ci->top; + L->top = functop(base - 1); i = *(pc++); /* go to next instruction */ ra = RA(i); lua_assert(GET_OPCODE(i) == OP_TFORLOOP); @@ -1507,7 +1510,8 @@ void luaV_execute (lua_State *L) { last--; luaC_barrierback(L, h, val); } - L->top = ci->top; /* correct top (in case of previous open call) */ + /* correct top (in case of previous open call) */ + L->top = functop(base - 1); vmbreak; } vmcase(OP_CLOSURE) { From 472c560705a29e33fa7b2dba7438b5cbf3d8170f Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 3 Nov 2017 15:22:54 -0200 Subject: [PATCH 0103/1145] no more useful fields in CallInfo --- lapi.c | 18 +++++++++--------- ldebug.c | 18 ++++++++++-------- ldo.c | 54 ++++++++++++++++++++++++++++++------------------------ lobject.h | 18 ++++++++++++++++-- lstate.h | 16 +--------------- ltests.c | 20 +++++++++----------- lvm.c | 47 +++++++++++++++++++++++++---------------------- 7 files changed, 100 insertions(+), 91 deletions(-) diff --git a/lapi.c b/lapi.c index 6b6bc56550..8750cefa16 100644 --- a/lapi.c +++ b/lapi.c @@ -1,5 +1,5 @@ /* -** $Id: lapi.c,v 2.273 2017/11/02 11:28:56 roberto Exp roberto $ +** $Id: lapi.c,v 2.274 2017/11/03 12:12:30 roberto Exp roberto $ ** Lua API ** See Copyright Notice in lua.h */ @@ -948,8 +948,8 @@ LUA_API void lua_callk (lua_State *L, int nargs, int nresults, checkresults(L, nargs, nresults); func = L->top - (nargs+1); if (k != NULL && L->nny == 0) { /* need to prepare continuation? */ - L->ci->u.c.k = k; /* save continuation */ - L->ci->u.c.ctx = ctx; /* save context */ + L->func->stkci.u.c.k = k; /* save continuation */ + L->func->stkci.u.c.ctx = ctx; /* save context */ luaD_call(L, func, nresults); /* do the call */ } else /* no continuation or no yieldable */ @@ -999,19 +999,19 @@ LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc, status = luaD_pcall(L, f_call, &c, savestack(L, c.func), efunc); } else { /* prepare continuation (call is already protected by 'resume') */ - CallInfo *ci = L->ci; StkId func = L->func; - ci->u.c.k = k; /* save continuation */ - ci->u.c.ctx = ctx; /* save context */ + func->stkci.u.c.k = k; /* save continuation */ + func->stkci.u.c.ctx = ctx; /* save context */ /* save information for error recovery */ - ci->u2.funcidx = savestack(L, c.func); - ci->u.c.old_errfunc = L->errfunc; + func->stkci.u2.funcidx = c.func - func; + func->stkci.u.c.old_errfunc = L->errfunc; L->errfunc = efunc; setoah(callstatus(func), L->allowhook); /* save value of 'allowhook' */ callstatus(func) |= CIST_YPCALL; /* function can do error recovery */ luaD_call(L, c.func, nresults); /* do the call */ + func = L->func; /* previous call can reallocate stack */ callstatus(func) &= ~CIST_YPCALL; - L->errfunc = ci->u.c.old_errfunc; + L->errfunc = func->stkci.u.c.old_errfunc; status = LUA_OK; /* if it is here, there were no errors */ } adjustresults(L, nresults); diff --git a/ldebug.c b/ldebug.c index 2db0910c72..b212d8758b 100644 --- a/ldebug.c +++ b/ldebug.c @@ -1,5 +1,5 @@ /* -** $Id: ldebug.c,v 2.135 2017/11/02 11:28:56 roberto Exp roberto $ +** $Id: ldebug.c,v 2.136 2017/11/03 12:12:30 roberto Exp roberto $ ** Debug Interface ** See Copyright Notice in lua.h */ @@ -44,7 +44,7 @@ static const char *funcnamefromcode (lua_State *L, CallInfo *ci, static int currentpc (CallInfo *ci) { lua_assert(isLua(ci->func)); - return pcRel(ci->u.l.savedpc, ci_func(ci)->p); + return pcRel(ci->func->stkci.u.l.savedpc, ci_func(ci)->p); } @@ -121,7 +121,7 @@ LUA_API void lua_sethook (lua_State *L, lua_Hook func, int mask, int count) { func = NULL; } if (isLua(L->func)) - L->oldpc = L->ci->u.l.savedpc; + L->oldpc = L->func->stkci.u.l.savedpc; L->hook = func; L->basehookcount = count; resethookcount(L); @@ -755,19 +755,21 @@ void luaG_traceexec (lua_State *L) { luaD_hook(L, LUA_HOOKCOUNT, -1); /* call count hook */ if (mask & LUA_MASKLINE) { Proto *p = ci_func(ci)->p; - int npc = pcRel(ci->u.l.savedpc, p); + int npc = pcRel(func->stkci.u.l.savedpc, p); if (npc == 0 || /* call linehook when enter a new function, */ - ci->u.l.savedpc <= L->oldpc || /* when jump back (loop), or when */ - changedline(p, pcRel(L->oldpc, p), npc)) { /* enter new line */ + func->stkci.u.l.savedpc <= L->oldpc || /* when jump back (loop), */ + changedline(p, pcRel(L->oldpc, p), npc)) { /* when enter new line */ int newline = luaG_getfuncline(p, npc); /* new line */ luaD_hook(L, LUA_HOOKLINE, newline); /* call line hook */ } } - L->oldpc = ci->u.l.savedpc; + func = L->func; /* previous calls can reallocate stack */ + L->oldpc = func->stkci.u.l.savedpc; if (L->status == LUA_YIELD) { /* did hook yield? */ if (counthook) L->hookcount = 1; /* undo decrement to zero */ - ci->u.l.savedpc--; /* undo increment (resume will increment it again) */ + /* undo increment (resume will increment it again) */ + func->stkci.u.l.savedpc--; callstatus(func) |= CIST_HOOKYIELD; /* mark that it yielded */ luaD_throw(L, LUA_YIELD); } diff --git a/ldo.c b/ldo.c index cbb1e0f3a5..3b01c8fb5c 100644 --- a/ldo.c +++ b/ldo.c @@ -1,5 +1,5 @@ /* -** $Id: ldo.c,v 2.165 2017/11/02 11:28:56 roberto Exp roberto $ +** $Id: ldo.c,v 2.166 2017/11/03 12:12:30 roberto Exp roberto $ ** Stack and Call structure of Lua ** See Copyright Notice in lua.h */ @@ -280,17 +280,19 @@ void luaD_hook (lua_State *L, int event, int line) { } -static void callhook (lua_State *L, CallInfo *ci) { +static void callhook (lua_State *L) { int hook = LUA_HOOKCALL; - StkId previous = L->func - L->func->stkci.previous; - ci->u.l.savedpc++; /* hooks assume 'pc' is already incremented */ + StkId func = L->func; + StkId previous = func - L->func->stkci.previous; + func->stkci.u.l.savedpc++; /* hooks assume 'pc' is already incremented */ if (isLua(previous) && - GET_OPCODE(*(ci->previous->u.l.savedpc - 1)) == OP_TAILCALL) { + GET_OPCODE(*(previous->stkci.u.l.savedpc - 1)) == OP_TAILCALL) { callstatus(L->func) |= CIST_TAIL; hook = LUA_HOOKTAILCALL; } luaD_hook(L, hook, -1); - ci->u.l.savedpc--; /* correct 'pc' */ + func = L->func; /* previous call can change stack */ + func->stkci.u.l.savedpc--; /* correct 'pc' */ } @@ -369,11 +371,13 @@ int luaD_poscall (lua_State *L, CallInfo *ci, StkId firstResult, int nres) { ptrdiff_t fr = savestack(L, firstResult); /* hook may change stack */ luaD_hook(L, LUA_HOOKRET, -1); firstResult = restorestack(L, fr); + res = L->func; } - L->oldpc = ci->previous->u.l.savedpc; /* 'oldpc' for caller function */ + /* 'oldpc' for caller function */ + L->oldpc = (res - res->stkci.previous)->stkci.u.l.savedpc; } L->ci = ci->previous; /* back to caller */ - L->func -= L->func->stkci.previous; + L->func = res - res->stkci.previous; lua_assert(L->func == L->ci->func); /* move results to proper place */ return moveresults(L, firstResult, res, nres, wanted); @@ -436,10 +440,10 @@ int luaD_precall (lua_State *L, StkId func, int nresults) { L->func = ci->func = func; L->top = func + 1 + fsize; lua_assert(functop(func) <= L->stack_last); - ci->u.l.savedpc = p->code; /* starting point */ + func->stkci.u.l.savedpc = p->code; /* starting point */ callstatus(func) = 0; if (L->hookmask & LUA_MASKCALL) - callhook(L, ci); + callhook(L); return 0; } default: { /* not a function */ @@ -500,18 +504,19 @@ static void finishCcall (lua_State *L, int status) { StkId func = L->func; int n; /* must have a continuation and must be able to call it */ - lua_assert(ci->u.c.k != NULL && L->nny == 0); + lua_assert(func->stkci.u.c.k != NULL && L->nny == 0); /* error status can only happen in a protected call */ lua_assert((callstatus(func) & CIST_YPCALL) || status == LUA_YIELD); if (callstatus(func) & CIST_YPCALL) { /* was inside a pcall? */ callstatus(func) &= ~CIST_YPCALL; /* continuation is also inside it */ - L->errfunc = ci->u.c.old_errfunc; /* with the same error function */ + L->errfunc = func->stkci.u.c.old_errfunc; /* with same error function */ } /* finish 'lua_callk'/'lua_pcall'; CIST_YPCALL and 'errfunc' already handled */ adjustresults(L, func->stkci.nresults); lua_unlock(L); - n = (*ci->u.c.k)(L, status, ci->u.c.ctx); /* call continuation function */ + /* call continuation function */ + n = (*func->stkci.u.c.k)(L, status, func->stkci.u.c.ctx); lua_lock(L); api_checknelems(L, n); luaD_poscall(L, ci, L->top - n, n); /* finish 'luaD_precall' */ @@ -564,7 +569,7 @@ static int recover (lua_State *L, int status) { CallInfo *ci = findpcall(L); if (ci == NULL) return 0; /* no recovery point */ /* "finish" luaD_pcall */ - oldtop = restorestack(L, ci->u2.funcidx); + oldtop = ci->func + ci->func->stkci.u2.funcidx; luaF_close(L, oldtop); seterrorobj(L, status, oldtop); L->ci = ci; @@ -572,7 +577,7 @@ static int recover (lua_State *L, int status) { L->allowhook = getoah(callstatus(L->func)); /* restore original 'allowhook' */ L->nny = 0; /* should be zero to be yieldable */ luaD_shrinkstack(L); - L->errfunc = ci->u.c.old_errfunc; + L->errfunc = ci->func->stkci.u.c.old_errfunc; return 1; /* continue running the coroutine */ } @@ -602,6 +607,7 @@ static void resume (lua_State *L, void *ud) { int n = *(cast(int*, ud)); /* number of arguments */ StkId firstArg = L->top - n; /* first argument */ CallInfo *ci = L->ci; + StkId func = L->func; if (L->status == LUA_OK) { /* starting a coroutine? */ if (!luaD_precall(L, firstArg - 1, LUA_MULTRET)) /* Lua function? */ luaV_execute(L); /* call it */ @@ -609,12 +615,13 @@ static void resume (lua_State *L, void *ud) { else { /* resuming from previous yield */ lua_assert(L->status == LUA_YIELD); L->status = LUA_OK; /* mark that it is running (again) */ - if (isLua(L->func)) /* yielded inside a hook? */ + if (isLua(func)) /* yielded inside a hook? */ luaV_execute(L); /* just continue running Lua code */ else { /* 'common' yield */ - if (ci->u.c.k != NULL) { /* does it have a continuation function? */ + if (func->stkci.u.c.k != NULL) { /* does it have a continuation? */ lua_unlock(L); - n = (*ci->u.c.k)(L, LUA_YIELD, ci->u.c.ctx); /* call continuation */ + /* call continuation */ + n = (*func->stkci.u.c.k)(L, LUA_YIELD, func->stkci.u.c.ctx); lua_lock(L); api_checknelems(L, n); firstArg = L->top - n; /* yield results come from continuation */ @@ -658,7 +665,7 @@ LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs, } else lua_assert(status == L->status); /* normal end or yield */ } - *nresults = (status == LUA_YIELD) ? L->ci->u2.nyield + *nresults = (status == LUA_YIELD) ? L->func->stkci.u2.nyield : L->top - (L->func + 1); L->nny = oldnny; /* restore 'nny' */ L->nCcalls--; @@ -675,7 +682,6 @@ LUA_API int lua_isyieldable (lua_State *L) { LUA_API int lua_yieldk (lua_State *L, int nresults, lua_KContext ctx, lua_KFunction k) { - CallInfo *ci = L->ci; StkId func = L->func; luai_userstateyield(L, nresults); lua_lock(L); @@ -689,12 +695,12 @@ LUA_API int lua_yieldk (lua_State *L, int nresults, lua_KContext ctx, L->status = LUA_YIELD; if (isLua(func)) { /* inside a hook? */ api_check(L, k == NULL, "hooks cannot continue after yielding"); - ci->u2.nyield = 0; /* no results */ + func->stkci.u2.nyield = 0; /* no results */ } else { - if ((ci->u.c.k = k) != NULL) /* is there a continuation? */ - ci->u.c.ctx = ctx; /* save context */ - ci->u2.nyield = nresults; /* save number of results */ + if ((func->stkci.u.c.k = k) != NULL) /* is there a continuation? */ + func->stkci.u.c.ctx = ctx; /* save context */ + func->stkci.u2.nyield = nresults; /* save number of results */ luaD_throw(L, LUA_YIELD); } lua_assert(callstatus(func) & CIST_HOOKED); /* must be inside a hook */ diff --git a/lobject.h b/lobject.h index 66c41e6f1d..925f958c92 100644 --- a/lobject.h +++ b/lobject.h @@ -1,5 +1,5 @@ /* -** $Id: lobject.h,v 2.126 2017/10/31 17:54:35 roberto Exp roberto $ +** $Id: lobject.h,v 2.127 2017/11/03 12:12:30 roberto Exp roberto $ ** Type definitions for Lua objects ** See Copyright Notice in lua.h */ @@ -314,9 +314,23 @@ typedef union StackValue { struct { TValuefields; lu_byte callstatus_; - unsigned short previous; /* difference to previous 'func' */ short nresults; /* expected number of results from this function */ + unsigned short previous; /* difference to previous 'func' */ unsigned short framesize; /* stack space available for this function */ + union { + unsigned short funcidx; /* called-function index */ + unsigned short nyield; /* number of values yielded */ + } u2; + union { + struct { /* only for Lua functions */ + const Instruction *savedpc; + } l; + struct { /* only for C functions */ + lua_KFunction k; /* continuation in case of yields */ + ptrdiff_t old_errfunc; + lua_KContext ctx; /* context info. in case of yields */ + } c; + } u; } stkci; } StackValue; diff --git a/lstate.h b/lstate.h index 3d76e9b95f..df1630eab6 100644 --- a/lstate.h +++ b/lstate.h @@ -1,5 +1,5 @@ /* -** $Id: lstate.h,v 2.146 2017/11/02 11:28:56 roberto Exp roberto $ +** $Id: lstate.h,v 2.147 2017/11/03 12:12:30 roberto Exp roberto $ ** Global State ** See Copyright Notice in lua.h */ @@ -87,20 +87,6 @@ typedef struct stringtable { typedef struct CallInfo { StkId func; /* function index in the stack */ struct CallInfo *previous, *next; /* dynamic call link */ - union { - struct { /* only for Lua functions */ - const Instruction *savedpc; - } l; - struct { /* only for C functions */ - lua_KFunction k; /* continuation in case of yields */ - ptrdiff_t old_errfunc; - lua_KContext ctx; /* context info. in case of yields */ - } c; - } u; - union { - ptrdiff_t funcidx; /* called-function index */ - int nyield; /* number of values yielded */ - } u2; } CallInfo; diff --git a/ltests.c b/ltests.c index 71ad09ea53..ebf73840dd 100644 --- a/ltests.c +++ b/ltests.c @@ -1,5 +1,5 @@ /* -** $Id: ltests.c,v 2.227 2017/11/02 11:28:56 roberto Exp roberto $ +** $Id: ltests.c,v 2.228 2017/11/03 12:12:30 roberto Exp roberto $ ** Internal Module for Debugging of the Lua Implementation ** See Copyright Notice in lua.h */ @@ -309,28 +309,26 @@ static void checkLclosure (global_State *g, LClosure *cl) { } -static int lua_checkpc (CallInfo *ci) { - if (!isLua(ci->func)) return 1; +static int lua_checkpc (StkId func) { + if (!isLua(func)) return 1; else { - StkId f = ci->func; - Proto *p = clLvalue(s2v(f))->p; - return p->code <= ci->u.l.savedpc && - ci->u.l.savedpc <= p->code + p->sizecode; + Proto *p = clLvalue(s2v(func))->p; + return p->code <= func->stkci.u.l.savedpc && + func->stkci.u.l.savedpc <= p->code + p->sizecode; } } static void checkstack (global_State *g, lua_State *L1) { StkId o; - CallInfo *ci; UpVal *uv; lua_assert(!isdead(g, L1)); for (uv = L1->openupval; uv != NULL; uv = uv->u.open.next) lua_assert(upisopen(uv)); /* must be open */ - for (ci = L1->ci; ci != NULL; ci = ci->previous) - lua_assert(lua_checkpc(ci)); - for (o = L1->func; o->stkci.previous != 0; o -= o->stkci.previous) + for (o = L1->func; o->stkci.previous != 0; o -= o->stkci.previous) { lua_assert(functop(o) <= L1->stack_last); + lua_assert(lua_checkpc(o)); + } lua_assert(o == L1->stack); if (L1->stack) { /* complete thread? */ for (o = L1->stack; o < L1->stack_last + EXTRA_STACK; o++) diff --git a/lvm.c b/lvm.c index 7acb387f21..ce15d61b51 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.301 2017/11/01 18:20:48 roberto Exp roberto $ +** $Id: lvm.c,v 2.302 2017/11/03 12:12:30 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -654,13 +654,14 @@ static void pushclosure (lua_State *L, Proto *p, UpVal **encup, StkId base, } +#define basepc(base) ((base - 1)->stkci.u.l.savedpc) + /* ** finish execution of an opcode interrupted by an yield */ void luaV_finishOp (lua_State *L) { - CallInfo *ci = L->ci; StkId base = L->func + 1; - Instruction inst = *(ci->u.l.savedpc - 1); /* interrupted instruction */ + Instruction inst = *(basepc(base) - 1); /* interrupted instruction */ OpCode op = GET_OPCODE(inst); switch (op) { /* finish its execution */ case OP_ADDI: case OP_SUBI: @@ -684,9 +685,9 @@ void luaV_finishOp (lua_State *L) { callstatus(base - 1) ^= CIST_LEQ; /* clear mark */ res = !res; /* negate result */ } - lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_JMP); + lua_assert(GET_OPCODE(*basepc(base)) == OP_JMP); if (res != GETARG_A(inst)) /* condition failed? */ - ci->u.l.savedpc++; /* skip jump instruction */ + basepc(base)++; /* skip jump instruction */ break; } case OP_CONCAT: { @@ -704,7 +705,7 @@ void luaV_finishOp (lua_State *L) { break; } case OP_TFORCALL: { - lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_TFORLOOP); + lua_assert(GET_OPCODE(*basepc(base)) == OP_TFORLOOP); L->top = functop(base - 1); /* correct top */ break; } @@ -763,20 +764,22 @@ void luaV_finishOp (lua_State *L) { ** Whenever code can raise errors (including memory errors), the global ** 'pc' must be correct to report occasional errors. */ -#define savepc(L) (ci->u.l.savedpc = pc) +#define savepc(base) (basepc(base) = pc) + +/* update internal copies to its correct values */ +#define updatestate() (base = L->func + 1, updatemask(L)) /* ** Protect code that, in general, can raise errors, reallocate the ** stack, and change the hooks. */ -#define Protect(code) \ - { savepc(L); {code;}; base = L->func + 1; updatemask(L); } +#define Protect(code) { savepc(base); {code;}; updatestate(); } #define checkGC(L,c) \ { luaC_condGC(L, L->top = (c), /* limit of live values */ \ - {Protect((void)0); L->top = functop(base - 1);}); /* restore top */ \ + {updatestate(); L->top = functop(base - 1);}); /* restore top */ \ luai_threadyield(L); } @@ -798,14 +801,14 @@ void luaV_execute (lua_State *L) { TValue *k; StkId base = L->func + 1; /* local copy of 'L->func + 1' */ int mask; /* local copy of 'L->hookmask & (LUA_MASKLINE | LUA_MASKCOUNT)' */ - const Instruction *pc; /* local copy of 'ci->u.l.savedpc' */ + const Instruction *pc; /* local copy of 'basepc(base)' */ callstatus(base - 1) |= CIST_FRESH; /* fresh invocation of 'luaV_execute" */ newframe: /* reentry point when frame changes (call/return) */ lua_assert(ci == L->ci); cl = clLvalue(s2v(L->func)); /* local reference to function's closure */ k = cl->p->k; /* local reference to function's constant table */ updatemask(L); - pc = ci->u.l.savedpc; + pc = basepc(base); /* main loop of interpreter */ for (;;) { Instruction i; @@ -969,7 +972,7 @@ void luaV_execute (lua_State *L) { int b = GETARG_B(i); int c = GETARG_C(i); Table *t; - savepc(L); /* in case of allocation errors */ + savepc(base); /* in case of allocation errors */ t = luaH_new(L); sethvalue2s(L, ra, t); if (b != 0 || c != 0) @@ -1368,9 +1371,9 @@ void luaV_execute (lua_State *L) { int b = GETARG_B(i); if (b != 0) L->top = ra+b; /* else previous instruction set top */ lua_assert(GETARG_C(i) - 1 == LUA_MULTRET); - savepc(L); + savepc(base); if (luaD_precall(L, ra, LUA_MULTRET)) { /* C function? */ - Protect((void)0); /* update 'base' */ + updatestate(); /* update 'base' */ } else { /* tail call: put called frame (n) in place of caller one (o) */ @@ -1388,7 +1391,7 @@ void luaV_execute (lua_State *L) { setobjs2s(L, ofunc + aux, nfunc + aux); ofunc->stkci.framesize = L->top - nfunc; L->top = functop(ofunc); /* correct top */ - oci->u.l.savedpc = nci->u.l.savedpc; + ofunc->stkci.u.l.savedpc = nfunc->stkci.u.l.savedpc; callstatus(ofunc) |= CIST_TAIL; /* function was tail called */ ci = L->ci = oci; /* remove new frame */ base = ofunc + 1; @@ -1401,7 +1404,7 @@ void luaV_execute (lua_State *L) { vmcase(OP_RETURN) { int b = GETARG_B(i); if (cl->p->sizep > 0) luaF_close(L, base); - savepc(L); + savepc(base); b = luaD_poscall(L, ci, ra, (b != 0 ? b - 1 : cast_int(L->top - ra))); if (callstatus(base - 1) & CIST_FRESH) /* local 'base' still from callee */ return; /* external invocation: return */ @@ -1409,8 +1412,8 @@ void luaV_execute (lua_State *L) { ci = L->ci; base = L->func + 1; if (b) L->top = functop(base - 1); - lua_assert(isLua(L->func)); - lua_assert(GET_OPCODE(*((ci)->u.l.savedpc - 1)) == OP_CALL); + lua_assert(isLua(base - 1)); + lua_assert(GET_OPCODE(*(basepc(base) - 1)) == OP_CALL); goto newframe; /* restart luaV_execute over previous Lua function */ } } @@ -1455,7 +1458,7 @@ void luaV_execute (lua_State *L) { } else { /* try making all values floats */ lua_Number ninit; lua_Number nlimit; lua_Number nstep; - savepc(L); /* in case of errors */ + savepc(base); /* in case of errors */ if (!tonumber(plimit, &nlimit)) luaG_runerror(L, "'for' limit must be a number"); setfltvalue(plimit, nlimit); @@ -1501,7 +1504,7 @@ void luaV_execute (lua_State *L) { } h = hvalue(s2v(ra)); last = ((c-1)*LFIELDS_PER_FLUSH) + n; - savepc(L); /* in case of allocation errors */ + savepc(base); /* in case of allocation errors */ if (last > h->sizearray) /* needs more space? */ luaH_resizearray(L, h, last); /* preallocate it at once */ for (; n > 0; n--) { @@ -1518,7 +1521,7 @@ void luaV_execute (lua_State *L) { Proto *p = cl->p->p[GETARG_Bx(i)]; LClosure *ncl = getcached(p, cl->upvals, base); /* cached closure */ if (ncl == NULL) { /* no match? */ - savepc(L); /* in case of allocation errors */ + savepc(base); /* in case of allocation errors */ pushclosure(L, p, cl->upvals, base, ra); /* create a new one */ } else From 7612f7735d32e298774d5379e847afb5e2a978ad Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 3 Nov 2017 17:33:22 -0200 Subject: [PATCH 0104/1145] removing uses of 'CallInfo' --- ldebug.c | 116 ++++++++++++++++++++++++++++++------------------------- lvm.c | 24 ++++++------ 2 files changed, 75 insertions(+), 65 deletions(-) diff --git a/ldebug.c b/ldebug.c index b212d8758b..553af07189 100644 --- a/ldebug.c +++ b/ldebug.c @@ -1,5 +1,5 @@ /* -** $Id: ldebug.c,v 2.136 2017/11/03 12:12:30 roberto Exp roberto $ +** $Id: ldebug.c,v 2.137 2017/11/03 17:22:54 roberto Exp roberto $ ** Debug Interface ** See Copyright Notice in lua.h */ @@ -34,17 +34,17 @@ #define noLuaClosure(f) ((f) == NULL || (f)->c.tt == LUA_TCCL) -/* Active Lua function (given call info) */ -#define ci_func(ci) (clLvalue(s2v((ci)->func))) +/* Active Lua function (given stack function) */ +#define ci_func(func) (clLvalue(s2v(func))) -static const char *funcnamefromcode (lua_State *L, CallInfo *ci, +static const char *funcnamefromcode (lua_State *L, StkId stkf, const char **name); -static int currentpc (CallInfo *ci) { - lua_assert(isLua(ci->func)); - return pcRel(ci->func->stkci.u.l.savedpc, ci_func(ci)->p); +static int currentpc (StkId func) { + lua_assert(isLua(func)); + return pcRel(func->stkci.u.l.savedpc, ci_func(func)->p); } @@ -101,8 +101,8 @@ int luaG_getfuncline (Proto *f, int pc) { } -static int currentline (CallInfo *ci) { - return luaG_getfuncline(ci_func(ci)->p, currentpc(ci)); +static int currentline (StkId func) { + return luaG_getfuncline(ci_func(func)->p, currentpc(func)); } @@ -168,24 +168,33 @@ static const char *upvalname (Proto *p, int uv) { } -static const char *findlocal (lua_State *L, CallInfo *ci, int n, +static StkId findcalled (lua_State *L, StkId caller) { + StkId func = L->func; + for (;;) { + StkId previous = func - func->stkci.previous; + lua_assert(previous < func); + if (previous == caller) + return func; + else + func = previous; + } +} + + +static const char *findlocal (lua_State *L, StkId stkf, int n, StkId *pos) { const char *name = NULL; - StkId base; - if (isLua(ci->func)) { - base = ci->func + 1; - name = luaF_getlocalname(ci_func(ci)->p, n, currentpc(ci)); + if (isLua(stkf)) { + name = luaF_getlocalname(ci_func(stkf)->p, n, currentpc(stkf)); } - else - base = ci->func + 1; if (name == NULL) { /* no 'standard' name? */ - StkId limit = (ci == L->ci) ? L->top : ci->next->func; - if (limit - base >= n && n > 0) /* is 'n' inside 'ci' stack? */ + StkId limit = (stkf == L->func) ? L->top : findcalled(L, stkf); + if (limit - stkf > n && n > 0) /* is 'n' inside 'ci' stack? */ name = "(*temporary)"; /* generic name for any valid slot */ else return NULL; /* no name */ } - *pos = base + (n - 1); + *pos = stkf + n; return name; } @@ -201,7 +210,7 @@ LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) { } else { /* active function; get information through 'ar' */ StkId pos = NULL; /* to avoid warnings */ - name = findlocal(L, ar->i_ci, n, &pos); + name = findlocal(L, ar->i_ci->func, n, &pos); if (name) { setobjs2s(L, L->top, pos); api_incr_top(L); @@ -216,7 +225,7 @@ LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) { StkId pos = NULL; /* to avoid warnings */ const char *name; lua_lock(L); - name = findlocal(L, ar->i_ci, n, &pos); + name = findlocal(L, ar->i_ci->func, n, &pos); if (name) { setobjs2s(L, pos, L->top - 1); L->top--; /* pop value */ @@ -274,22 +283,25 @@ static void collectvalidlines (lua_State *L, Closure *f) { } -static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) { - if (ci == NULL) /* no 'ci'? */ +static const char *getfuncname (lua_State *L, StkId stkf, const char **name) { + if (stkf == NULL) /* no function? */ return NULL; /* no info */ - else if (callstatus(ci->func) & CIST_FIN) { /* is this a finalizer? */ + else if (callstatus(stkf) & CIST_FIN) { /* is this a finalizer? */ *name = "__gc"; return "metamethod"; /* report it as such */ } /* calling function is a known Lua function? */ - else if (!(callstatus(ci->func) & CIST_TAIL) && isLua(ci->previous->func)) - return funcnamefromcode(L, ci->previous, name); - else return NULL; /* no way to find a name */ + else { + StkId previous = stkf - stkf->stkci.previous; + if (!(callstatus(stkf) & CIST_TAIL) && isLua(previous)) + return funcnamefromcode(L, previous, name); + else return NULL; /* no way to find a name */ + } } static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar, - Closure *f, CallInfo *ci) { + Closure *f, StkId stkf) { int status = 1; for (; *what; what++) { switch (*what) { @@ -298,7 +310,7 @@ static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar, break; } case 'l': { - ar->currentline = (ci && isLua(ci->func)) ? currentline(ci) : -1; + ar->currentline = (stkf && isLua(stkf)) ? currentline(stkf) : -1; break; } case 'u': { @@ -314,11 +326,11 @@ static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar, break; } case 't': { - ar->istailcall = (ci) ? callstatus(ci->func) & CIST_TAIL : 0; + ar->istailcall = (stkf) ? callstatus(stkf) & CIST_TAIL : 0; break; } case 'n': { - ar->namewhat = getfuncname(L, ci, &ar->name); + ar->namewhat = getfuncname(L, stkf, &ar->name); if (ar->namewhat == NULL) { ar->namewhat = ""; /* not found */ ar->name = NULL; @@ -338,23 +350,23 @@ static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar, LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) { int status; Closure *cl; - CallInfo *ci; + StkId stkf; TValue *func; lua_lock(L); if (*what == '>') { - ci = NULL; + stkf = NULL; func = s2v(L->top - 1); api_check(L, ttisfunction(func), "function expected"); what++; /* skip the '>' */ L->top--; /* pop function */ } else { - ci = ar->i_ci; - func = s2v(ci->func); + stkf = ar->i_ci->func; + func = s2v(stkf); lua_assert(ttisfunction(func)); } cl = ttisclosure(func) ? clvalue(func) : NULL; - status = auxgetinfo(L, what, ar, cl, ci); + status = auxgetinfo(L, what, ar, cl, stkf); if (strchr(what, 'f')) { setobj2s(L, L->top, func); api_incr_top(L); @@ -543,13 +555,13 @@ static const char *gxf (Proto *p, int pc, Instruction i, int isup) { ** Returns what the name is (e.g., "for iterator", "method", ** "metamethod") and sets '*name' to point to the name. */ -static const char *funcnamefromcode (lua_State *L, CallInfo *ci, +static const char *funcnamefromcode (lua_State *L, StkId stkf, const char **name) { TMS tm = (TMS)0; /* (initial value avoids warnings) */ - Proto *p = ci_func(ci)->p; /* calling function */ - int pc = currentpc(ci); /* calling instruction index */ + Proto *p = ci_func(stkf)->p; /* calling function */ + int pc = currentpc(stkf); /* calling instruction index */ Instruction i = p->code[pc]; /* calling instruction */ - if (callstatus(ci->func) & CIST_HOOKED) { /* was it called inside a hook? */ + if (callstatus(stkf) & CIST_HOOKED) { /* was it called inside a hook? */ *name = "?"; return "hook"; } @@ -617,9 +629,9 @@ static int isinstack (lua_State *L, const TValue *o) { ** with instructions OP_GETTABUP/OP_SETTABUP, which operate directly on ** upvalues.) */ -static const char *getupvalname (CallInfo *ci, const TValue *o, +static const char *getupvalname (StkId stkf, const TValue *o, const char **name) { - LClosure *c = ci_func(ci); + LClosure *c = ci_func(stkf); int i; for (i = 0; i < c->nupvalues; i++) { if (c->upvals[i]->v == o) { @@ -633,13 +645,13 @@ static const char *getupvalname (CallInfo *ci, const TValue *o, static const char *varinfo (lua_State *L, const TValue *o) { const char *name = NULL; /* to avoid warnings */ - CallInfo *ci = L->ci; + StkId stkf = L->func; const char *kind = NULL; - if (isLua(L->func)) { - kind = getupvalname(ci, o, &name); /* check whether 'o' is an upvalue */ + if (isLua(stkf)) { + kind = getupvalname(stkf, o, &name); /* check whether 'o' is an upvalue */ if (!kind && isinstack(L, o)) /* no? try a register */ - kind = getobjname(ci_func(ci)->p, currentpc(ci), - cast_int(cast(StkId, o) - (L->func + 1)), &name); + kind = getobjname(ci_func(stkf)->p, currentpc(stkf), + cast_int(cast(StkId, o) - (stkf + 1)), &name); } return (kind) ? luaO_pushfstring(L, " (%s '%s')", kind, name) : ""; } @@ -712,15 +724,16 @@ l_noret luaG_errormsg (lua_State *L) { l_noret luaG_runerror (lua_State *L, const char *fmt, ...) { - CallInfo *ci = L->ci; + StkId func; const char *msg; va_list argp; luaC_checkGC(L); /* error message uses memory */ va_start(argp, fmt); msg = luaO_pushvfstring(L, fmt, argp); /* format message */ va_end(argp); - if (isLua(L->func)) /* if Lua function, add source:line information */ - luaG_addinfo(L, msg, ci_func(ci)->p->source, currentline(ci)); + func = L->func; /* previous calls can change the stack */ + if (isLua(func)) /* if Lua function, add source:line information */ + luaG_addinfo(L, msg, ci_func(func)->p->source, currentline(func)); luaG_errormsg(L); } @@ -739,7 +752,6 @@ static int changedline (Proto *p, int oldpc, int newpc) { void luaG_traceexec (lua_State *L) { - CallInfo *ci = L->ci; StkId func = L->func; lu_byte mask = L->hookmask; int counthook = (--L->hookcount == 0 && (mask & LUA_MASKCOUNT)); @@ -754,7 +766,7 @@ void luaG_traceexec (lua_State *L) { if (counthook) luaD_hook(L, LUA_HOOKCOUNT, -1); /* call count hook */ if (mask & LUA_MASKLINE) { - Proto *p = ci_func(ci)->p; + Proto *p = ci_func(func)->p; int npc = pcRel(func->stkci.u.l.savedpc, p); if (npc == 0 || /* call linehook when enter a new function, */ func->stkci.u.l.savedpc <= L->oldpc || /* when jump back (loop), */ diff --git a/lvm.c b/lvm.c index ce15d61b51..28952da0cc 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.302 2017/11/03 12:12:30 roberto Exp roberto $ +** $Id: lvm.c,v 2.303 2017/11/03 17:22:54 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -754,11 +754,11 @@ void luaV_finishOp (lua_State *L) { ** Execute a jump instruction. The 'updatemask' allows signals to stop ** tight loops. (Without it, the local copy of 'mask' could never change.) */ -#define dojump(ci,i,e) { pc += GETARG_sBx(i) + e; updatemask(L); } +#define dojump(i,e) { pc += GETARG_sBx(i) + e; updatemask(L); } /* for test instructions, execute the jump instruction that follows it */ -#define donextjump(ci) { i = *pc; dojump(ci, i, 1); } +#define donextjump() { i = *pc; dojump(i, 1); } /* ** Whenever code can raise errors (including memory errors), the global @@ -1286,7 +1286,7 @@ void luaV_execute (lua_State *L) { vmbreak; } vmcase(OP_JMP) { - dojump(ci, i, 0); + dojump(i, 0); vmbreak; } vmcase(OP_EQ) { @@ -1296,7 +1296,7 @@ void luaV_execute (lua_State *L) { if (luaV_equalobj(L, rb, rc) != GETARG_A(i)) pc++; else - donextjump(ci); + donextjump(); ) vmbreak; } @@ -1312,7 +1312,7 @@ void luaV_execute (lua_State *L) { if (res != GETARG_A(i)) pc++; else - donextjump(ci); + donextjump(); vmbreak; } vmcase(OP_LE) { @@ -1327,14 +1327,14 @@ void luaV_execute (lua_State *L) { if (res != GETARG_A(i)) pc++; else - donextjump(ci); + donextjump(); vmbreak; } vmcase(OP_TEST) { if (GETARG_C(i) ? l_isfalse(s2v(ra)) : !l_isfalse(s2v(ra))) pc++; else - donextjump(ci); + donextjump(); vmbreak; } vmcase(OP_TESTSET) { @@ -1343,7 +1343,7 @@ void luaV_execute (lua_State *L) { pc++; else { setobj2s(L, ra, rb); - donextjump(ci); + donextjump(); } vmbreak; } @@ -1377,9 +1377,7 @@ void luaV_execute (lua_State *L) { } else { /* tail call: put called frame (n) in place of caller one (o) */ - CallInfo *nci = L->ci; /* called frame (new) */ - CallInfo *oci = nci->previous; /* caller frame (old) */ - StkId nfunc = nci->func; /* called function */ + StkId nfunc = L->func; /* called function */ StkId ofunc = nfunc - nfunc->stkci.previous; /* caller function */ /* last stack slot filled by 'precall' */ StkId lim = nfunc + 1 + getproto(s2v(nfunc))->numparams; @@ -1393,7 +1391,7 @@ void luaV_execute (lua_State *L) { L->top = functop(ofunc); /* correct top */ ofunc->stkci.u.l.savedpc = nfunc->stkci.u.l.savedpc; callstatus(ofunc) |= CIST_TAIL; /* function was tail called */ - ci = L->ci = oci; /* remove new frame */ + ci = L->ci = L->ci->previous; /* remove new frame */ base = ofunc + 1; L->func = ofunc; lua_assert(L->top == base + getproto(s2v(ofunc))->maxstacksize); From 6bb3e40a8d2cdb64a6ac1962d8748dd638a11721 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 3 Nov 2017 18:41:05 -0200 Subject: [PATCH 0105/1145] 'lua_Debug' not using 'CallInfo' --- ldebug.c | 24 ++++++++++++++---------- ldo.c | 6 +++--- lua.h | 5 +++-- 3 files changed, 20 insertions(+), 15 deletions(-) diff --git a/ldebug.c b/ldebug.c index 553af07189..100b9b7a86 100644 --- a/ldebug.c +++ b/ldebug.c @@ -1,5 +1,5 @@ /* -** $Id: ldebug.c,v 2.137 2017/11/03 17:22:54 roberto Exp roberto $ +** $Id: ldebug.c,v 2.138 2017/11/03 19:33:22 roberto Exp roberto $ ** Debug Interface ** See Copyright Notice in lua.h */ @@ -146,14 +146,17 @@ LUA_API int lua_gethookcount (lua_State *L) { LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) { int status; - CallInfo *ci; + StkId func; if (level < 0) return 0; /* invalid (negative) level */ lua_lock(L); - for (ci = L->ci; level > 0 && ci != &L->base_ci; ci = ci->previous) + for (func = L->func; + level > 0 && func->stkci.previous != 0; + func -= func->stkci.previous) level--; - if (level == 0 && ci != &L->base_ci) { /* level found? */ + if (level == 0 && func->stkci.previous != 0) { /* level found? */ status = 1; - ar->i_ci = ci; + ar->i_actf = func - L->stack; + ar->i_actL = L; } else status = 0; /* no such level */ lua_unlock(L); @@ -181,9 +184,10 @@ static StkId findcalled (lua_State *L, StkId caller) { } -static const char *findlocal (lua_State *L, StkId stkf, int n, - StkId *pos) { +static const char *findlocal (lua_State *L, const lua_Debug *ar, + int n, StkId *pos) { const char *name = NULL; + StkId stkf = ar->i_actL->stack + ar->i_actf; if (isLua(stkf)) { name = luaF_getlocalname(ci_func(stkf)->p, n, currentpc(stkf)); } @@ -210,7 +214,7 @@ LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) { } else { /* active function; get information through 'ar' */ StkId pos = NULL; /* to avoid warnings */ - name = findlocal(L, ar->i_ci->func, n, &pos); + name = findlocal(L, ar, n, &pos); if (name) { setobjs2s(L, L->top, pos); api_incr_top(L); @@ -225,7 +229,7 @@ LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) { StkId pos = NULL; /* to avoid warnings */ const char *name; lua_lock(L); - name = findlocal(L, ar->i_ci->func, n, &pos); + name = findlocal(L, ar, n, &pos); if (name) { setobjs2s(L, pos, L->top - 1); L->top--; /* pop value */ @@ -361,7 +365,7 @@ LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) { L->top--; /* pop function */ } else { - stkf = ar->i_ci->func; + stkf = ar->i_actL->stack + ar->i_actf; func = s2v(stkf); lua_assert(ttisfunction(func)); } diff --git a/ldo.c b/ldo.c index 3b01c8fb5c..5cd518efb6 100644 --- a/ldo.c +++ b/ldo.c @@ -1,5 +1,5 @@ /* -** $Id: ldo.c,v 2.166 2017/11/03 12:12:30 roberto Exp roberto $ +** $Id: ldo.c,v 2.167 2017/11/03 17:22:54 roberto Exp roberto $ ** Stack and Call structure of Lua ** See Copyright Notice in lua.h */ @@ -253,14 +253,14 @@ void luaD_inctop (lua_State *L) { void luaD_hook (lua_State *L, int event, int line) { lua_Hook hook = L->hook; if (hook && L->allowhook) { /* make sure there is a hook */ - CallInfo *ci = L->ci; ptrdiff_t top = savestack(L, L->top); int origframesize = L->func->stkci.framesize; int tmpframesize; /* frame size to run hook */ lua_Debug ar; ar.event = event; ar.currentline = line; - ar.i_ci = ci; + ar.i_actf = L->func - L->stack; + ar.i_actL = L; luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */ tmpframesize = L->top - L->func + LUA_MINSTACK; if (tmpframesize > origframesize) /* need to grow frame? */ diff --git a/lua.h b/lua.h index be99e14c09..fe97e8a6c1 100644 --- a/lua.h +++ b/lua.h @@ -1,5 +1,5 @@ /* -** $Id: lua.h,v 1.336 2017/07/27 13:36:54 roberto Exp roberto $ +** $Id: lua.h,v 1.337 2017/11/02 11:28:56 roberto Exp roberto $ ** Lua - A Scripting Language ** Lua.org, PUC-Rio, Brazil (http://www.lua.org) ** See Copyright Notice at the end of this file @@ -456,7 +456,8 @@ struct lua_Debug { char istailcall; /* (t) */ char short_src[LUA_IDSIZE]; /* (S) */ /* private part */ - struct CallInfo *i_ci; /* active function */ + int i_actf; /* active function */ + lua_State *i_actL; /* where active function is active */ }; /* }====================================================================== */ From 93fd67b7936f0f1fc20645d18eb85a193887c315 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Sat, 4 Nov 2017 10:57:02 -0200 Subject: [PATCH 0106/1145] no more 'CallInfo' structure --- ldo.c | 64 +++++++++++++++++++------------------------------------- ldo.h | 5 ++--- lstate.c | 57 ++++--------------------------------------------- lstate.h | 15 +------------ lvm.c | 9 ++------ 5 files changed, 31 insertions(+), 119 deletions(-) diff --git a/ldo.c b/ldo.c index 5cd518efb6..bb18956501 100644 --- a/ldo.c +++ b/ldo.c @@ -1,5 +1,5 @@ /* -** $Id: ldo.c,v 2.167 2017/11/03 17:22:54 roberto Exp roberto $ +** $Id: ldo.c,v 2.168 2017/11/03 20:41:05 roberto Exp roberto $ ** Stack and Call structure of Lua ** See Copyright Notice in lua.h */ @@ -157,15 +157,11 @@ int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) { ** =================================================================== */ static void correctstack (lua_State *L, StkId oldstack) { - CallInfo *ci; UpVal *up; L->top = (L->top - oldstack) + L->stack; L->func = (L->func - oldstack) + L->stack; for (up = L->openupval; up != NULL; up = up->u.open.next) up->v = s2v((uplevel(up) - oldstack) + L->stack); - for (ci = L->ci; ci != NULL; ci = ci->previous) { - ci->func = (ci->func - oldstack) + L->stack; - } } @@ -223,10 +219,6 @@ void luaD_shrinkstack (lua_State *L) { int goodsize = inuse + (inuse / 8) + 2*EXTRA_STACK; if (goodsize > LUAI_MAXSTACK) goodsize = LUAI_MAXSTACK; /* respect stack limit */ - if (L->stacksize > LUAI_MAXSTACK) /* had been handling stack overflow? */ - luaE_freeCI(L); /* free all CIs (list grew because of an error) */ - else - luaE_shrinkCI(L); /* shrink list */ /* if thread is currently not handling a stack overflow and its good size is smaller than current size, shrink its stack */ if (inuse <= (LUAI_MAXSTACK - EXTRA_STACK) && @@ -363,7 +355,7 @@ static int moveresults (lua_State *L, StkId firstResult, StkId res, ** moves current number of results to proper place; returns 0 iff call ** wanted multiple (variable number of) results. */ -int luaD_poscall (lua_State *L, CallInfo *ci, StkId firstResult, int nres) { +int luaD_poscall (lua_State *L, StkId firstResult, int nres) { StkId res = L->func; /* res == final position of 1st result */ int wanted = res->stkci.nresults; if (L->hookmask & (LUA_MASKRET | LUA_MASKLINE)) { @@ -376,18 +368,12 @@ int luaD_poscall (lua_State *L, CallInfo *ci, StkId firstResult, int nres) { /* 'oldpc' for caller function */ L->oldpc = (res - res->stkci.previous)->stkci.u.l.savedpc; } - L->ci = ci->previous; /* back to caller */ L->func = res - res->stkci.previous; - lua_assert(L->func == L->ci->func); /* move results to proper place */ return moveresults(L, firstResult, res, nres, wanted); } - -#define next_ci(L) (L->ci = (L->ci->next ? L->ci->next : luaE_extendCI(L))) - - /* ** Prepares a function call: checks the stack, creates a new CallInfo ** entry, fills in the relevant information, calls hook if needed. @@ -398,7 +384,6 @@ int luaD_poscall (lua_State *L, CallInfo *ci, StkId firstResult, int nres) { int luaD_precall (lua_State *L, StkId func, int nresults) { lua_CFunction f; TValue *funcv = s2v(func); - CallInfo *ci; switch (ttype(funcv)) { case LUA_TCCL: /* C closure */ f = clCvalue(funcv)->f; @@ -408,10 +393,9 @@ int luaD_precall (lua_State *L, StkId func, int nresults) { Cfunc: { int n; /* number of returns */ checkstackp(L, LUA_MINSTACK, func); /* ensure minimum stack size */ - ci = next_ci(L); /* now 'enter' new function */ func->stkci.nresults = nresults; func->stkci.previous = func - L->func; - L->func = ci->func = func; + L->func = func; setfunctop(func, L->top + LUA_MINSTACK); lua_assert(functop(func) <= L->stack_last); callstatus(func) = 0; @@ -421,7 +405,7 @@ int luaD_precall (lua_State *L, StkId func, int nresults) { n = (*f)(L); /* do the actual call */ lua_lock(L); api_checknelems(L, n); - luaD_poscall(L, ci, L->top - n, n); + luaD_poscall(L, L->top - n, n); return 1; } case LUA_TLCL: { /* Lua function: prepare its call */ @@ -433,11 +417,10 @@ int luaD_precall (lua_State *L, StkId func, int nresults) { setnilvalue(s2v(L->top++)); /* complete missing arguments */ if (p->is_vararg) luaT_adjustvarargs(L, p, n); - ci = next_ci(L); /* now 'enter' new function */ func->stkci.nresults = nresults; func->stkci.previous = func - L->func; func->stkci.framesize = fsize + 1; /* size includes function itself */ - L->func = ci->func = func; + L->func = func; L->top = func + 1 + fsize; lua_assert(functop(func) <= L->stack_last); func->stkci.u.l.savedpc = p->code; /* starting point */ @@ -500,7 +483,6 @@ void luaD_callnoyield (lua_State *L, StkId func, int nResults) { ** continuation function. */ static void finishCcall (lua_State *L, int status) { - CallInfo *ci = L->ci; StkId func = L->func; int n; /* must have a continuation and must be able to call it */ @@ -519,7 +501,7 @@ static void finishCcall (lua_State *L, int status) { n = (*func->stkci.u.c.k)(L, status, func->stkci.u.c.ctx); lua_lock(L); api_checknelems(L, n); - luaD_poscall(L, ci, L->top - n, n); /* finish 'luaD_precall' */ + luaD_poscall(L, L->top - n, n); /* finish 'luaD_precall' */ } @@ -534,7 +516,7 @@ static void finishCcall (lua_State *L, int status) { static void unroll (lua_State *L, void *ud) { if (ud != NULL) /* error status? */ finishCcall(L, *(int *)ud); /* finish 'lua_pcallk' callee */ - while (L->ci != &L->base_ci) { /* something in the stack */ + while (L->func != L->stack) { /* something in the stack */ if (!isLua(L->func)) /* C function? */ finishCcall(L, LUA_YIELD); /* complete its execution */ else { /* Lua function */ @@ -549,11 +531,13 @@ static void unroll (lua_State *L, void *ud) { ** Try to find a suspended protected call (a "recover point") for the ** given thread. */ -static CallInfo *findpcall (lua_State *L) { - CallInfo *ci; - for (ci = L->ci; ci != NULL; ci = ci->previous) { /* search for a pcall */ - if (callstatus(ci->func) & CIST_YPCALL) - return ci; +static StkId findpcall (lua_State *L) { + StkId func; + for (func = L->func; + func->stkci.previous != 0; + func -= func->stkci.previous) { + if (callstatus(func) & CIST_YPCALL) + return func; } return NULL; /* no pending pcall */ } @@ -566,18 +550,17 @@ static CallInfo *findpcall (lua_State *L) { */ static int recover (lua_State *L, int status) { StkId oldtop; - CallInfo *ci = findpcall(L); - if (ci == NULL) return 0; /* no recovery point */ + StkId recf = findpcall(L); + if (recf == NULL) return 0; /* no recovery point */ /* "finish" luaD_pcall */ - oldtop = ci->func + ci->func->stkci.u2.funcidx; + oldtop = recf + recf->stkci.u2.funcidx; luaF_close(L, oldtop); seterrorobj(L, status, oldtop); - L->ci = ci; - L->func = ci->func; - L->allowhook = getoah(callstatus(L->func)); /* restore original 'allowhook' */ + L->func = recf; + L->allowhook = getoah(callstatus(recf)); /* restore original 'allowhook' */ L->nny = 0; /* should be zero to be yieldable */ luaD_shrinkstack(L); - L->errfunc = ci->func->stkci.u.c.old_errfunc; + L->errfunc = recf->stkci.u.c.old_errfunc; return 1; /* continue running the coroutine */ } @@ -606,7 +589,6 @@ static int resume_error (lua_State *L, const char *msg, int narg) { static void resume (lua_State *L, void *ud) { int n = *(cast(int*, ud)); /* number of arguments */ StkId firstArg = L->top - n; /* first argument */ - CallInfo *ci = L->ci; StkId func = L->func; if (L->status == LUA_OK) { /* starting a coroutine? */ if (!luaD_precall(L, firstArg - 1, LUA_MULTRET)) /* Lua function? */ @@ -626,7 +608,7 @@ static void resume (lua_State *L, void *ud) { api_checknelems(L, n); firstArg = L->top - n; /* yield results come from continuation */ } - luaD_poscall(L, ci, firstArg, n); /* finish 'luaD_precall' */ + luaD_poscall(L, firstArg, n); /* finish 'luaD_precall' */ } unroll(L, NULL); /* run continuation */ } @@ -639,7 +621,7 @@ LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs, unsigned short oldnny = L->nny; /* save "number of non-yieldable" calls */ lua_lock(L); if (L->status == LUA_OK) { /* may be starting a coroutine */ - if (L->ci != &L->base_ci) /* not in base level? */ + if (L->func != L->stack) /* not in base level? */ return resume_error(L, "cannot resume non-suspended coroutine", nargs); } else if (L->status != LUA_YIELD) @@ -712,7 +694,6 @@ LUA_API int lua_yieldk (lua_State *L, int nresults, lua_KContext ctx, int luaD_pcall (lua_State *L, Pfunc func, void *u, ptrdiff_t old_top, ptrdiff_t ef) { int status; - CallInfo *old_ci = L->ci; ptrdiff_t oldfunc = savestack(L, L->func); lu_byte old_allowhooks = L->allowhook; unsigned short old_nny = L->nny; @@ -723,7 +704,6 @@ int luaD_pcall (lua_State *L, Pfunc func, void *u, StkId oldtop = restorestack(L, old_top); luaF_close(L, oldtop); /* close possible pending closures */ seterrorobj(L, status, oldtop); - L->ci = old_ci; L->func = restorestack(L, oldfunc); L->allowhook = old_allowhooks; L->nny = old_nny; diff --git a/ldo.h b/ldo.h index fedf70d43e..4c03d5941c 100644 --- a/ldo.h +++ b/ldo.h @@ -1,5 +1,5 @@ /* -** $Id: ldo.h,v 2.30 2017/05/13 12:57:20 roberto Exp roberto $ +** $Id: ldo.h,v 2.31 2017/06/29 15:06:44 roberto Exp roberto $ ** Stack and Call structure of Lua ** See Copyright Notice in lua.h */ @@ -52,8 +52,7 @@ LUAI_FUNC void luaD_call (lua_State *L, StkId func, int nResults); LUAI_FUNC void luaD_callnoyield (lua_State *L, StkId func, int nResults); LUAI_FUNC int luaD_pcall (lua_State *L, Pfunc func, void *u, ptrdiff_t oldtop, ptrdiff_t ef); -LUAI_FUNC int luaD_poscall (lua_State *L, CallInfo *ci, StkId firstResult, - int nres); +LUAI_FUNC int luaD_poscall (lua_State *L, StkId firstResult, int nres); LUAI_FUNC void luaD_reallocstack (lua_State *L, int newsize); LUAI_FUNC void luaD_growstack (lua_State *L, int n); LUAI_FUNC void luaD_shrinkstack (lua_State *L); diff --git a/lstate.c b/lstate.c index 8e3177ca99..76daa2e775 100644 --- a/lstate.c +++ b/lstate.c @@ -1,5 +1,5 @@ /* -** $Id: lstate.c,v 2.143 2017/10/31 17:54:35 roberto Exp roberto $ +** $Id: lstate.c,v 2.144 2017/11/03 12:12:30 roberto Exp roberto $ ** Global State ** See Copyright Notice in lua.h */ @@ -97,51 +97,8 @@ void luaE_setdebt (global_State *g, l_mem debt) { } -CallInfo *luaE_extendCI (lua_State *L) { - CallInfo *ci = luaM_new(L, CallInfo); - lua_assert(L->ci->next == NULL); - L->ci->next = ci; - ci->previous = L->ci; - ci->next = NULL; - L->nci++; - return ci; -} - - -/* -** free all CallInfo structures not in use by a thread -*/ -void luaE_freeCI (lua_State *L) { - CallInfo *ci = L->ci; - CallInfo *next = ci->next; - ci->next = NULL; - while ((ci = next) != NULL) { - next = ci->next; - luaM_free(L, ci); - L->nci--; - } -} - - -/* -** free half of the CallInfo structures not in use by a thread -*/ -void luaE_shrinkCI (lua_State *L) { - CallInfo *ci = L->ci; - CallInfo *next2; /* next's next */ - /* while there are two nexts */ - while (ci->next != NULL && (next2 = ci->next->next) != NULL) { - luaM_free(L, ci->next); /* free next */ - L->nci--; - ci->next = next2; /* remove 'next' from the list */ - next2->previous = ci; - ci = next2; /* keep next's next */ - } -} - - static void stack_init (lua_State *L1, lua_State *L) { - int i; CallInfo *ci; + int i; /* initialize stack array */ L1->stack = luaM_newvector(L, BASIC_STACK_SIZE, StackValue); L1->stacksize = BASIC_STACK_SIZE; @@ -149,24 +106,19 @@ static void stack_init (lua_State *L1, lua_State *L) { setnilvalue(s2v(L1->stack + i)); /* erase new stack */ L1->top = L1->stack; L1->stack_last = L1->stack + L1->stacksize - EXTRA_STACK; - /* initialize first ci */ - ci = &L1->base_ci; - ci->next = ci->previous = NULL; - L1->func = ci->func = L1->top; + /* initialize first 'function' */ + L1->func = L1->stack; L1->func->stkci.previous = 0; /* end of linked list */ L1->func->stkci.framesize = LUA_MINSTACK + 1; callstatus(L1->func) = 0; setnilvalue(s2v(L1->top)); /* 'function' entry for this 'ci' */ L1->top++; - L1->ci = ci; } static void freestack (lua_State *L) { if (L->stack == NULL) return; /* stack not completely built yet */ - L->ci = &L->base_ci; /* free the entire 'ci' list */ - luaE_freeCI(L); lua_assert(L->nci == 0); luaM_freearray(L, L->stack, L->stacksize); /* free stack array */ } @@ -216,7 +168,6 @@ static void f_luaopen (lua_State *L, void *ud) { static void preinit_thread (lua_State *L, global_State *g) { G(L) = g; L->stack = NULL; - L->ci = NULL; L->func = NULL; L->nci = 0; L->stacksize = 0; diff --git a/lstate.h b/lstate.h index df1630eab6..07cb3d3dfc 100644 --- a/lstate.h +++ b/lstate.h @@ -1,5 +1,5 @@ /* -** $Id: lstate.h,v 2.147 2017/11/03 12:12:30 roberto Exp roberto $ +** $Id: lstate.h,v 2.148 2017/11/03 17:22:54 roberto Exp roberto $ ** Global State ** See Copyright Notice in lua.h */ @@ -81,14 +81,6 @@ typedef struct stringtable { } stringtable; -/* -** Information about a call. -*/ -typedef struct CallInfo { - StkId func; /* function index in the stack */ - struct CallInfo *previous, *next; /* dynamic call link */ -} CallInfo; - /* ** Bits in CallInfo status @@ -170,7 +162,6 @@ struct lua_State { lu_byte status; StkId top; /* first free slot in the stack */ global_State *l_G; - CallInfo *ci; /* call info for current function */ StkId func; /* current function */ const Instruction *oldpc; /* last pc traced */ StkId stack_last; /* last free slot in the stack */ @@ -179,7 +170,6 @@ struct lua_State { GCObject *gclist; struct lua_State *twups; /* list of threads with open upvalues */ struct lua_longjmp *errorJmp; /* current error recover point */ - CallInfo base_ci; /* CallInfo for first level (C calling Lua) */ volatile lua_Hook hook; ptrdiff_t errfunc; /* current error handling function (stack index) */ int stacksize; @@ -235,9 +225,6 @@ union GCUnion { LUAI_FUNC void luaE_setdebt (global_State *g, l_mem debt); LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1); -LUAI_FUNC CallInfo *luaE_extendCI (lua_State *L); -LUAI_FUNC void luaE_freeCI (lua_State *L); -LUAI_FUNC void luaE_shrinkCI (lua_State *L); #endif diff --git a/lvm.c b/lvm.c index 28952da0cc..b1cf4666b8 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.303 2017/11/03 17:22:54 roberto Exp roberto $ +** $Id: lvm.c,v 2.304 2017/11/03 19:33:22 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -796,7 +796,6 @@ void luaV_finishOp (lua_State *L) { void luaV_execute (lua_State *L) { - CallInfo *ci = L->ci; LClosure *cl; TValue *k; StkId base = L->func + 1; /* local copy of 'L->func + 1' */ @@ -804,7 +803,6 @@ void luaV_execute (lua_State *L) { const Instruction *pc; /* local copy of 'basepc(base)' */ callstatus(base - 1) |= CIST_FRESH; /* fresh invocation of 'luaV_execute" */ newframe: /* reentry point when frame changes (call/return) */ - lua_assert(ci == L->ci); cl = clLvalue(s2v(L->func)); /* local reference to function's closure */ k = cl->p->k; /* local reference to function's constant table */ updatemask(L); @@ -1361,7 +1359,6 @@ void luaV_execute (lua_State *L) { /* else leave top for next instruction */ } else { /* Lua function */ - ci = L->ci; base = L->func + 1; goto newframe; /* restart luaV_execute over new Lua function */ } @@ -1391,7 +1388,6 @@ void luaV_execute (lua_State *L) { L->top = functop(ofunc); /* correct top */ ofunc->stkci.u.l.savedpc = nfunc->stkci.u.l.savedpc; callstatus(ofunc) |= CIST_TAIL; /* function was tail called */ - ci = L->ci = L->ci->previous; /* remove new frame */ base = ofunc + 1; L->func = ofunc; lua_assert(L->top == base + getproto(s2v(ofunc))->maxstacksize); @@ -1403,11 +1399,10 @@ void luaV_execute (lua_State *L) { int b = GETARG_B(i); if (cl->p->sizep > 0) luaF_close(L, base); savepc(base); - b = luaD_poscall(L, ci, ra, (b != 0 ? b - 1 : cast_int(L->top - ra))); + b = luaD_poscall(L, ra, (b != 0 ? b - 1 : cast_int(L->top - ra))); if (callstatus(base - 1) & CIST_FRESH) /* local 'base' still from callee */ return; /* external invocation: return */ else { /* invocation via reentry: continue execution */ - ci = L->ci; base = L->func + 1; if (b) L->top = functop(base - 1); lua_assert(isLua(base - 1)); From 5a3f26f85558bedfa439027919d928abfdd00b6d Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 6 Nov 2017 15:34:06 -0200 Subject: [PATCH 0107/1145] fitting a StackValue structure into 32 bytes (for 64-bit machines) --- lobject.h | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/lobject.h b/lobject.h index 925f958c92..56dfebba37 100644 --- a/lobject.h +++ b/lobject.h @@ -1,5 +1,5 @@ /* -** $Id: lobject.h,v 2.127 2017/11/03 12:12:30 roberto Exp roberto $ +** $Id: lobject.h,v 2.128 2017/11/03 17:22:54 roberto Exp roberto $ ** Type definitions for Lua objects ** See Copyright Notice in lua.h */ @@ -314,26 +314,27 @@ typedef union StackValue { struct { TValuefields; lu_byte callstatus_; - short nresults; /* expected number of results from this function */ - unsigned short previous; /* difference to previous 'func' */ - unsigned short framesize; /* stack space available for this function */ + char nresults; /* expected number of results from this function */ union { - unsigned short funcidx; /* called-function index */ - unsigned short nyield; /* number of values yielded */ + unsigned char funcidx; /* called-function index */ + unsigned char nyield; /* number of values yielded */ } u2; + unsigned short previous; /* difference to previous 'func' */ + unsigned short framesize; /* stack space available for this function */ union { struct { /* only for Lua functions */ const Instruction *savedpc; } l; struct { /* only for C functions */ lua_KFunction k; /* continuation in case of yields */ - ptrdiff_t old_errfunc; - lua_KContext ctx; /* context info. in case of yields */ + int old_errfunc; + int ctx; /* context info. in case of yields */ } c; } u; } stkci; } StackValue; + #define callstatus(ar) ((ar)->stkci.callstatus_) /* top of a function (first element after its frame) */ From ad0704e40cc7b3135fedc6d40a522addb039e090 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 7 Nov 2017 11:25:26 -0200 Subject: [PATCH 0108/1145] back to 'CallInfo' (no gains with its removal) --- lapi.c | 81 +++++++++++------------ lapi.h | 9 ++- ldebug.c | 161 ++++++++++++++++++++------------------------- ldo.c | 190 +++++++++++++++++++++++++++--------------------------- ldo.h | 5 +- lgc.c | 6 +- lobject.h | 32 +-------- lstate.c | 63 +++++++++++++++--- lstate.h | 50 +++++++++++--- ltests.c | 23 +++---- ltm.c | 6 +- lua.h | 5 +- lvm.c | 133 +++++++++++++++++++------------------- 13 files changed, 394 insertions(+), 370 deletions(-) diff --git a/lapi.c b/lapi.c index 8750cefa16..1c1e8f9e1b 100644 --- a/lapi.c +++ b/lapi.c @@ -1,5 +1,5 @@ /* -** $Id: lapi.c,v 2.274 2017/11/03 12:12:30 roberto Exp roberto $ +** $Id: lapi.c,v 2.273 2017/11/02 11:28:56 roberto Exp $ ** Lua API ** See Copyright Notice in lua.h */ @@ -10,7 +10,6 @@ #include "lprefix.h" -#include #include #include @@ -59,14 +58,15 @@ const char lua_ident[] = static TValue *index2value (lua_State *L, int idx) { + CallInfo *ci = L->ci; if (idx > 0) { - StkId o = L->func + idx; - api_check(L, idx < L->func->stkci.framesize, "unacceptable index"); + StkId o = ci->func + idx; + api_check(L, idx <= L->ci->top - (ci->func + 1), "unacceptable index"); if (o >= L->top) return NONVALIDVALUE; else return s2v(o); } else if (!ispseudo(idx)) { /* negative index */ - api_check(L, idx != 0 && -idx < L->func->stkci.framesize, "invalid index"); + api_check(L, idx != 0 && -idx <= L->top - (ci->func + 1), "invalid index"); return s2v(L->top + idx); } else if (idx == LUA_REGISTRYINDEX) @@ -74,10 +74,10 @@ static TValue *index2value (lua_State *L, int idx) { else { /* upvalues */ idx = LUA_REGISTRYINDEX - idx; api_check(L, idx <= MAXUPVAL + 1, "upvalue index too large"); - if (ttislcf(s2v(L->func))) /* light C function? */ + if (ttislcf(s2v(ci->func))) /* light C function? */ return NONVALIDVALUE; /* it has no upvalues */ else { - CClosure *func = clCvalue(s2v(L->func)); + CClosure *func = clCvalue(s2v(ci->func)); return (idx <= func->nupvalues) ? &func->upvalue[idx-1] : NONVALIDVALUE; } } @@ -85,13 +85,14 @@ static TValue *index2value (lua_State *L, int idx) { static StkId index2stack (lua_State *L, int idx) { + CallInfo *ci = L->ci; if (idx > 0) { - StkId o = L->func + idx; + StkId o = ci->func + idx; api_check(L, o < L->top, "unacceptable index"); return o; } else { /* non-positive index */ - api_check(L, idx != 0 && -idx <= L->top - (L->func + 1), "invalid index"); + api_check(L, idx != 0 && -idx <= L->top - (ci->func + 1), "invalid index"); api_check(L, !ispseudo(idx), "invalid index"); return L->top + idx; } @@ -110,12 +111,10 @@ static void growstack (lua_State *L, void *ud) { LUA_API int lua_checkstack (lua_State *L, int n) { int res; - int frameuse = L->top - L->func; + CallInfo *ci = L->ci; lua_lock(L); api_check(L, n >= 0, "negative 'n'"); - if (n >= USHRT_MAX - frameuse) - res = 0; /* frame size overflow */ - else if (L->stack_last - L->top > n) /* stack large enough? */ + if (L->stack_last - L->top > n) /* stack large enough? */ res = 1; /* yes; check is OK */ else { /* no; need to grow stack */ int inuse = cast_int(L->top - L->stack) + EXTRA_STACK; @@ -124,8 +123,8 @@ LUA_API int lua_checkstack (lua_State *L, int n) { else /* try to grow stack */ res = (luaD_rawrunprotected(L, &growstack, &n) == LUA_OK); } - if (res && L->func->stkci.framesize < frameuse + n) - L->func->stkci.framesize = frameuse + n; /* adjust frame size */ + if (res && ci->top < L->top + n) + ci->top = L->top + n; /* adjust frame top */ lua_unlock(L); return res; } @@ -137,7 +136,7 @@ LUA_API void lua_xmove (lua_State *from, lua_State *to, int n) { lua_lock(to); api_checknelems(from, n); api_check(from, G(from) == G(to), "moving among independent states"); - api_check(from, functop(to->func) - to->top >= n, "stack overflow"); + api_check(from, to->ci->top - to->top >= n, "stack overflow"); from->top -= n; for (i = 0; i < n; i++) { setobjs2s(to, to->top, from->top + i); @@ -176,17 +175,17 @@ LUA_API const lua_Number *lua_version (lua_State *L) { LUA_API int lua_absindex (lua_State *L, int idx) { return (idx > 0 || ispseudo(idx)) ? idx - : cast_int(L->top - L->func) + idx; + : cast_int(L->top - L->ci->func) + idx; } LUA_API int lua_gettop (lua_State *L) { - return cast_int(L->top - (L->func + 1)); + return cast_int(L->top - (L->ci->func + 1)); } LUA_API void lua_settop (lua_State *L, int idx) { - StkId func = L->func; + StkId func = L->ci->func; lua_lock(L); if (idx >= 0) { api_check(L, idx <= L->stack_last - (func + 1), "new top too large"); @@ -244,7 +243,7 @@ LUA_API void lua_copy (lua_State *L, int fromidx, int toidx) { api_checkvalidindex(L, to); setobj(L, to, fr); if (isupvalue(toidx)) /* function upvalue? */ - luaC_barrier(L, clCvalue(s2v(L->func)), fr); + luaC_barrier(L, clCvalue(s2v(L->ci->func)), fr); /* LUA_REGISTRYINDEX does not need gc barrier (collector revisits it before finishing collection) */ lua_unlock(L); @@ -932,24 +931,23 @@ LUA_API void lua_setuservalue (lua_State *L, int idx) { #define checkresults(L,na,nr) \ - api_check(L, (nr) == LUA_MULTRET || \ - (functop(L->func) - L->top >= (nr) - (na)), \ - "results from function overflow current frame size") + api_check(L, (nr) == LUA_MULTRET || (L->ci->top - L->top >= (nr) - (na)), \ + "results from function overflow current stack size") LUA_API void lua_callk (lua_State *L, int nargs, int nresults, lua_KContext ctx, lua_KFunction k) { StkId func; lua_lock(L); - api_check(L, k == NULL || !isLua(L->func), + api_check(L, k == NULL || !isLua(L->ci), "cannot use continuations inside hooks"); api_checknelems(L, nargs+1); api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread"); checkresults(L, nargs, nresults); func = L->top - (nargs+1); if (k != NULL && L->nny == 0) { /* need to prepare continuation? */ - L->func->stkci.u.c.k = k; /* save continuation */ - L->func->stkci.u.c.ctx = ctx; /* save context */ + L->ci->u.c.k = k; /* save continuation */ + L->ci->u.c.ctx = ctx; /* save context */ luaD_call(L, func, nresults); /* do the call */ } else /* no continuation or no yieldable */ @@ -980,38 +978,37 @@ LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc, lua_KContext ctx, lua_KFunction k) { struct CallS c; int status; - ptrdiff_t efunc; + ptrdiff_t func; lua_lock(L); - api_check(L, k == NULL || !isLua(L->func), + api_check(L, k == NULL || !isLua(L->ci), "cannot use continuations inside hooks"); api_checknelems(L, nargs+1); api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread"); checkresults(L, nargs, nresults); if (errfunc == 0) - efunc = 0; + func = 0; else { StkId o = index2stack(L, errfunc); - efunc = savestack(L, o); + func = savestack(L, o); } c.func = L->top - (nargs+1); /* function to be called */ if (k == NULL || L->nny > 0) { /* no continuation or no yieldable? */ c.nresults = nresults; /* do a 'conventional' protected call */ - status = luaD_pcall(L, f_call, &c, savestack(L, c.func), efunc); + status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func); } else { /* prepare continuation (call is already protected by 'resume') */ - StkId func = L->func; - func->stkci.u.c.k = k; /* save continuation */ - func->stkci.u.c.ctx = ctx; /* save context */ + CallInfo *ci = L->ci; + ci->u.c.k = k; /* save continuation */ + ci->u.c.ctx = ctx; /* save context */ /* save information for error recovery */ - func->stkci.u2.funcidx = c.func - func; - func->stkci.u.c.old_errfunc = L->errfunc; - L->errfunc = efunc; - setoah(callstatus(func), L->allowhook); /* save value of 'allowhook' */ - callstatus(func) |= CIST_YPCALL; /* function can do error recovery */ + ci->u2.funcidx = savestack(L, c.func); + ci->u.c.old_errfunc = L->errfunc; + L->errfunc = func; + setoah(ci->callstatus, L->allowhook); /* save value of 'allowhook' */ + ci->callstatus |= CIST_YPCALL; /* function can do error recovery */ luaD_call(L, c.func, nresults); /* do the call */ - func = L->func; /* previous call can reallocate stack */ - callstatus(func) &= ~CIST_YPCALL; - L->errfunc = func->stkci.u.c.old_errfunc; + ci->callstatus &= ~CIST_YPCALL; + L->errfunc = ci->u.c.old_errfunc; status = LUA_OK; /* if it is here, there were no errors */ } adjustresults(L, nresults); diff --git a/lapi.h b/lapi.h index d851d161f6..77ebb30027 100644 --- a/lapi.h +++ b/lapi.h @@ -1,5 +1,5 @@ /* -** $Id: lapi.h,v 2.10 2017/11/01 18:20:48 roberto Exp roberto $ +** $Id: lapi.h,v 2.10 2017/11/01 18:20:48 roberto Exp $ ** Auxiliary functions from Lua API ** See Copyright Notice in lua.h */ @@ -11,14 +11,13 @@ #include "llimits.h" #include "lstate.h" -#define api_incr_top(L) {L->top++; api_check(L, L->top <= functop(L->func), \ +#define api_incr_top(L) {L->top++; api_check(L, L->top <= L->ci->top, \ "stack overflow");} #define adjustresults(L,nres) \ - { if ((nres) == LUA_MULTRET && functop(L->func) < L->top) \ - setfunctop(L->func, L->top); } + { if ((nres) == LUA_MULTRET && L->ci->top < L->top) L->ci->top = L->top; } -#define api_checknelems(L,n) api_check(L, (n) < (L->top - L->func), \ +#define api_checknelems(L,n) api_check(L, (n) < (L->top - L->ci->func), \ "not enough elements in the stack") diff --git a/ldebug.c b/ldebug.c index 100b9b7a86..83ddde72e5 100644 --- a/ldebug.c +++ b/ldebug.c @@ -1,5 +1,5 @@ /* -** $Id: ldebug.c,v 2.138 2017/11/03 19:33:22 roberto Exp roberto $ +** $Id: ldebug.c,v 2.135 2017/11/02 11:28:56 roberto Exp $ ** Debug Interface ** See Copyright Notice in lua.h */ @@ -34,17 +34,17 @@ #define noLuaClosure(f) ((f) == NULL || (f)->c.tt == LUA_TCCL) -/* Active Lua function (given stack function) */ -#define ci_func(func) (clLvalue(s2v(func))) +/* Active Lua function (given call info) */ +#define ci_func(ci) (clLvalue(s2v((ci)->func))) -static const char *funcnamefromcode (lua_State *L, StkId stkf, +static const char *funcnamefromcode (lua_State *L, CallInfo *ci, const char **name); -static int currentpc (StkId func) { - lua_assert(isLua(func)); - return pcRel(func->stkci.u.l.savedpc, ci_func(func)->p); +static int currentpc (CallInfo *ci) { + lua_assert(isLua(ci)); + return pcRel(ci->u.l.savedpc, ci_func(ci)->p); } @@ -101,8 +101,8 @@ int luaG_getfuncline (Proto *f, int pc) { } -static int currentline (StkId func) { - return luaG_getfuncline(ci_func(func)->p, currentpc(func)); +static int currentline (CallInfo *ci) { + return luaG_getfuncline(ci_func(ci)->p, currentpc(ci)); } @@ -120,8 +120,8 @@ LUA_API void lua_sethook (lua_State *L, lua_Hook func, int mask, int count) { mask = 0; func = NULL; } - if (isLua(L->func)) - L->oldpc = L->func->stkci.u.l.savedpc; + if (isLua(L->ci)) + L->oldpc = L->ci->u.l.savedpc; L->hook = func; L->basehookcount = count; resethookcount(L); @@ -146,17 +146,14 @@ LUA_API int lua_gethookcount (lua_State *L) { LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) { int status; - StkId func; + CallInfo *ci; if (level < 0) return 0; /* invalid (negative) level */ lua_lock(L); - for (func = L->func; - level > 0 && func->stkci.previous != 0; - func -= func->stkci.previous) + for (ci = L->ci; level > 0 && ci != &L->base_ci; ci = ci->previous) level--; - if (level == 0 && func->stkci.previous != 0) { /* level found? */ + if (level == 0 && ci != &L->base_ci) { /* level found? */ status = 1; - ar->i_actf = func - L->stack; - ar->i_actL = L; + ar->i_ci = ci; } else status = 0; /* no such level */ lua_unlock(L); @@ -171,34 +168,24 @@ static const char *upvalname (Proto *p, int uv) { } -static StkId findcalled (lua_State *L, StkId caller) { - StkId func = L->func; - for (;;) { - StkId previous = func - func->stkci.previous; - lua_assert(previous < func); - if (previous == caller) - return func; - else - func = previous; - } -} - - -static const char *findlocal (lua_State *L, const lua_Debug *ar, - int n, StkId *pos) { +static const char *findlocal (lua_State *L, CallInfo *ci, int n, + StkId *pos) { const char *name = NULL; - StkId stkf = ar->i_actL->stack + ar->i_actf; - if (isLua(stkf)) { - name = luaF_getlocalname(ci_func(stkf)->p, n, currentpc(stkf)); + StkId base; + if (isLua(ci)) { + base = ci->func + 1; + name = luaF_getlocalname(ci_func(ci)->p, n, currentpc(ci)); } + else + base = ci->func + 1; if (name == NULL) { /* no 'standard' name? */ - StkId limit = (stkf == L->func) ? L->top : findcalled(L, stkf); - if (limit - stkf > n && n > 0) /* is 'n' inside 'ci' stack? */ + StkId limit = (ci == L->ci) ? L->top : ci->next->func; + if (limit - base >= n && n > 0) /* is 'n' inside 'ci' stack? */ name = "(*temporary)"; /* generic name for any valid slot */ else return NULL; /* no name */ } - *pos = stkf + n; + *pos = base + (n - 1); return name; } @@ -214,7 +201,7 @@ LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) { } else { /* active function; get information through 'ar' */ StkId pos = NULL; /* to avoid warnings */ - name = findlocal(L, ar, n, &pos); + name = findlocal(L, ar->i_ci, n, &pos); if (name) { setobjs2s(L, L->top, pos); api_incr_top(L); @@ -229,7 +216,7 @@ LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) { StkId pos = NULL; /* to avoid warnings */ const char *name; lua_lock(L); - name = findlocal(L, ar, n, &pos); + name = findlocal(L, ar->i_ci, n, &pos); if (name) { setobjs2s(L, pos, L->top - 1); L->top--; /* pop value */ @@ -287,25 +274,22 @@ static void collectvalidlines (lua_State *L, Closure *f) { } -static const char *getfuncname (lua_State *L, StkId stkf, const char **name) { - if (stkf == NULL) /* no function? */ +static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) { + if (ci == NULL) /* no 'ci'? */ return NULL; /* no info */ - else if (callstatus(stkf) & CIST_FIN) { /* is this a finalizer? */ + else if (ci->callstatus & CIST_FIN) { /* is this a finalizer? */ *name = "__gc"; return "metamethod"; /* report it as such */ } /* calling function is a known Lua function? */ - else { - StkId previous = stkf - stkf->stkci.previous; - if (!(callstatus(stkf) & CIST_TAIL) && isLua(previous)) - return funcnamefromcode(L, previous, name); - else return NULL; /* no way to find a name */ - } + else if (!(ci->callstatus & CIST_TAIL) && isLua(ci->previous)) + return funcnamefromcode(L, ci->previous, name); + else return NULL; /* no way to find a name */ } static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar, - Closure *f, StkId stkf) { + Closure *f, CallInfo *ci) { int status = 1; for (; *what; what++) { switch (*what) { @@ -314,7 +298,7 @@ static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar, break; } case 'l': { - ar->currentline = (stkf && isLua(stkf)) ? currentline(stkf) : -1; + ar->currentline = (ci && isLua(ci)) ? currentline(ci) : -1; break; } case 'u': { @@ -330,11 +314,11 @@ static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar, break; } case 't': { - ar->istailcall = (stkf) ? callstatus(stkf) & CIST_TAIL : 0; + ar->istailcall = (ci) ? ci->callstatus & CIST_TAIL : 0; break; } case 'n': { - ar->namewhat = getfuncname(L, stkf, &ar->name); + ar->namewhat = getfuncname(L, ci, &ar->name); if (ar->namewhat == NULL) { ar->namewhat = ""; /* not found */ ar->name = NULL; @@ -354,23 +338,23 @@ static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar, LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) { int status; Closure *cl; - StkId stkf; + CallInfo *ci; TValue *func; lua_lock(L); if (*what == '>') { - stkf = NULL; + ci = NULL; func = s2v(L->top - 1); api_check(L, ttisfunction(func), "function expected"); what++; /* skip the '>' */ L->top--; /* pop function */ } else { - stkf = ar->i_actL->stack + ar->i_actf; - func = s2v(stkf); + ci = ar->i_ci; + func = s2v(ci->func); lua_assert(ttisfunction(func)); } cl = ttisclosure(func) ? clvalue(func) : NULL; - status = auxgetinfo(L, what, ar, cl, stkf); + status = auxgetinfo(L, what, ar, cl, ci); if (strchr(what, 'f')) { setobj2s(L, L->top, func); api_incr_top(L); @@ -559,13 +543,13 @@ static const char *gxf (Proto *p, int pc, Instruction i, int isup) { ** Returns what the name is (e.g., "for iterator", "method", ** "metamethod") and sets '*name' to point to the name. */ -static const char *funcnamefromcode (lua_State *L, StkId stkf, +static const char *funcnamefromcode (lua_State *L, CallInfo *ci, const char **name) { TMS tm = (TMS)0; /* (initial value avoids warnings) */ - Proto *p = ci_func(stkf)->p; /* calling function */ - int pc = currentpc(stkf); /* calling instruction index */ + Proto *p = ci_func(ci)->p; /* calling function */ + int pc = currentpc(ci); /* calling instruction index */ Instruction i = p->code[pc]; /* calling instruction */ - if (callstatus(stkf) & CIST_HOOKED) { /* was it called inside a hook? */ + if (ci->callstatus & CIST_HOOKED) { /* was it called inside a hook? */ *name = "?"; return "hook"; } @@ -621,10 +605,10 @@ static const char *funcnamefromcode (lua_State *L, StkId stkf, ** not ISO C, but it should not crash a program; the subsequent ** checks are ISO C and ensure a correct result. */ -static int isinstack (lua_State *L, const TValue *o) { - StkId base = L->stack; +static int isinstack (CallInfo *ci, const TValue *o) { + StkId base = ci->func + 1; ptrdiff_t i = cast(StkId, o) - base; - return (0 <= i && i < (L->top - base) && s2v(base + i) == o); + return (0 <= i && i < (ci->top - base) && s2v(base + i) == o); } @@ -633,9 +617,9 @@ static int isinstack (lua_State *L, const TValue *o) { ** with instructions OP_GETTABUP/OP_SETTABUP, which operate directly on ** upvalues.) */ -static const char *getupvalname (StkId stkf, const TValue *o, +static const char *getupvalname (CallInfo *ci, const TValue *o, const char **name) { - LClosure *c = ci_func(stkf); + LClosure *c = ci_func(ci); int i; for (i = 0; i < c->nupvalues; i++) { if (c->upvals[i]->v == o) { @@ -649,13 +633,13 @@ static const char *getupvalname (StkId stkf, const TValue *o, static const char *varinfo (lua_State *L, const TValue *o) { const char *name = NULL; /* to avoid warnings */ - StkId stkf = L->func; + CallInfo *ci = L->ci; const char *kind = NULL; - if (isLua(stkf)) { - kind = getupvalname(stkf, o, &name); /* check whether 'o' is an upvalue */ - if (!kind && isinstack(L, o)) /* no? try a register */ - kind = getobjname(ci_func(stkf)->p, currentpc(stkf), - cast_int(cast(StkId, o) - (stkf + 1)), &name); + if (isLua(ci)) { + kind = getupvalname(ci, o, &name); /* check whether 'o' is an upvalue */ + if (!kind && isinstack(ci, o)) /* no? try a register */ + kind = getobjname(ci_func(ci)->p, currentpc(ci), + cast_int(cast(StkId, o) - (ci->func + 1)), &name); } return (kind) ? luaO_pushfstring(L, " (%s '%s')", kind, name) : ""; } @@ -728,16 +712,15 @@ l_noret luaG_errormsg (lua_State *L) { l_noret luaG_runerror (lua_State *L, const char *fmt, ...) { - StkId func; + CallInfo *ci = L->ci; const char *msg; va_list argp; luaC_checkGC(L); /* error message uses memory */ va_start(argp, fmt); msg = luaO_pushvfstring(L, fmt, argp); /* format message */ va_end(argp); - func = L->func; /* previous calls can change the stack */ - if (isLua(func)) /* if Lua function, add source:line information */ - luaG_addinfo(L, msg, ci_func(func)->p->source, currentline(func)); + if (isLua(ci)) /* if Lua function, add source:line information */ + luaG_addinfo(L, msg, ci_func(ci)->p->source, currentline(ci)); luaG_errormsg(L); } @@ -756,37 +739,35 @@ static int changedline (Proto *p, int oldpc, int newpc) { void luaG_traceexec (lua_State *L) { - StkId func = L->func; + CallInfo *ci = L->ci; lu_byte mask = L->hookmask; int counthook = (--L->hookcount == 0 && (mask & LUA_MASKCOUNT)); if (counthook) resethookcount(L); /* reset count */ else if (!(mask & LUA_MASKLINE)) return; /* no line hook and count != 0; nothing to be done */ - if (callstatus(func) & CIST_HOOKYIELD) { /* called hook last time? */ - callstatus(func) &= ~CIST_HOOKYIELD; /* erase mark */ + if (ci->callstatus & CIST_HOOKYIELD) { /* called hook last time? */ + ci->callstatus &= ~CIST_HOOKYIELD; /* erase mark */ return; /* do not call hook again (VM yielded, so it did not move) */ } if (counthook) luaD_hook(L, LUA_HOOKCOUNT, -1); /* call count hook */ if (mask & LUA_MASKLINE) { - Proto *p = ci_func(func)->p; - int npc = pcRel(func->stkci.u.l.savedpc, p); + Proto *p = ci_func(ci)->p; + int npc = pcRel(ci->u.l.savedpc, p); if (npc == 0 || /* call linehook when enter a new function, */ - func->stkci.u.l.savedpc <= L->oldpc || /* when jump back (loop), */ - changedline(p, pcRel(L->oldpc, p), npc)) { /* when enter new line */ + ci->u.l.savedpc <= L->oldpc || /* when jump back (loop), or when */ + changedline(p, pcRel(L->oldpc, p), npc)) { /* enter new line */ int newline = luaG_getfuncline(p, npc); /* new line */ luaD_hook(L, LUA_HOOKLINE, newline); /* call line hook */ } } - func = L->func; /* previous calls can reallocate stack */ - L->oldpc = func->stkci.u.l.savedpc; + L->oldpc = ci->u.l.savedpc; if (L->status == LUA_YIELD) { /* did hook yield? */ if (counthook) L->hookcount = 1; /* undo decrement to zero */ - /* undo increment (resume will increment it again) */ - func->stkci.u.l.savedpc--; - callstatus(func) |= CIST_HOOKYIELD; /* mark that it yielded */ + ci->u.l.savedpc--; /* undo increment (resume will increment it again) */ + ci->callstatus |= CIST_HOOKYIELD; /* mark that it yielded */ luaD_throw(L, LUA_YIELD); } } diff --git a/ldo.c b/ldo.c index bb18956501..efdcbc1b9e 100644 --- a/ldo.c +++ b/ldo.c @@ -1,5 +1,5 @@ /* -** $Id: ldo.c,v 2.168 2017/11/03 20:41:05 roberto Exp roberto $ +** $Id: ldo.c,v 2.165 2017/11/02 11:28:56 roberto Exp $ ** Stack and Call structure of Lua ** See Copyright Notice in lua.h */ @@ -123,8 +123,8 @@ l_noret luaD_throw (lua_State *L, int errcode) { else { /* no handler at all; abort */ if (g->panic) { /* panic function? */ seterrorobj(L, errcode, L->top); /* assume EXTRA_STACK */ - if (functop(L->func) < L->top) /* check invariant */ - setfunctop(L->func, L->top); + if (L->ci->top < L->top) + L->ci->top = L->top; /* pushing msg. can break this invariant */ lua_unlock(L); g->panic(L); /* call panic function (last chance to jump out) */ } @@ -157,11 +157,15 @@ int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) { ** =================================================================== */ static void correctstack (lua_State *L, StkId oldstack) { + CallInfo *ci; UpVal *up; L->top = (L->top - oldstack) + L->stack; - L->func = (L->func - oldstack) + L->stack; for (up = L->openupval; up != NULL; up = up->u.open.next) up->v = s2v((uplevel(up) - oldstack) + L->stack); + for (ci = L->ci; ci != NULL; ci = ci->previous) { + ci->top = (ci->top - oldstack) + L->stack; + ci->func = (ci->func - oldstack) + L->stack; + } } @@ -203,11 +207,10 @@ void luaD_growstack (lua_State *L, int n) { static int stackinuse (lua_State *L) { + CallInfo *ci; StkId lim = L->top; - StkId func = L->func; - for (; func->stkci.previous != 0; func -= func->stkci.previous) { - if (lim < functop(func)) - lim = functop(func); + for (ci = L->ci; ci != NULL; ci = ci->previous) { + if (lim < ci->top) lim = ci->top; } lua_assert(lim <= L->stack_last); return cast_int(lim - L->stack) + 1; /* part of stack in use */ @@ -219,6 +222,10 @@ void luaD_shrinkstack (lua_State *L) { int goodsize = inuse + (inuse / 8) + 2*EXTRA_STACK; if (goodsize > LUAI_MAXSTACK) goodsize = LUAI_MAXSTACK; /* respect stack limit */ + if (L->stacksize > LUAI_MAXSTACK) /* had been handling stack overflow? */ + luaE_freeCI(L); /* free all CIs (list grew because of an error) */ + else + luaE_shrinkCI(L); /* shrink list */ /* if thread is currently not handling a stack overflow and its good size is smaller than current size, shrink its stack */ if (inuse <= (LUAI_MAXSTACK - EXTRA_STACK) && @@ -245,46 +252,40 @@ void luaD_inctop (lua_State *L) { void luaD_hook (lua_State *L, int event, int line) { lua_Hook hook = L->hook; if (hook && L->allowhook) { /* make sure there is a hook */ + CallInfo *ci = L->ci; ptrdiff_t top = savestack(L, L->top); - int origframesize = L->func->stkci.framesize; - int tmpframesize; /* frame size to run hook */ + ptrdiff_t ci_top = savestack(L, ci->top); lua_Debug ar; ar.event = event; ar.currentline = line; - ar.i_actf = L->func - L->stack; - ar.i_actL = L; + ar.i_ci = ci; luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */ - tmpframesize = L->top - L->func + LUA_MINSTACK; - if (tmpframesize > origframesize) /* need to grow frame? */ - L->func->stkci.framesize = tmpframesize; - lua_assert(functop(L->func) <= L->stack_last); + ci->top = L->top + LUA_MINSTACK; + lua_assert(ci->top <= L->stack_last); L->allowhook = 0; /* cannot call hooks inside a hook */ - callstatus(L->func) |= CIST_HOOKED; + ci->callstatus |= CIST_HOOKED; lua_unlock(L); (*hook)(L, &ar); lua_lock(L); lua_assert(!L->allowhook); L->allowhook = 1; - L->func->stkci.framesize = origframesize; + ci->top = restorestack(L, ci_top); L->top = restorestack(L, top); - callstatus(L->func) &= ~CIST_HOOKED; + ci->callstatus &= ~CIST_HOOKED; } } -static void callhook (lua_State *L) { +static void callhook (lua_State *L, CallInfo *ci) { int hook = LUA_HOOKCALL; - StkId func = L->func; - StkId previous = func - L->func->stkci.previous; - func->stkci.u.l.savedpc++; /* hooks assume 'pc' is already incremented */ - if (isLua(previous) && - GET_OPCODE(*(previous->stkci.u.l.savedpc - 1)) == OP_TAILCALL) { - callstatus(L->func) |= CIST_TAIL; + ci->u.l.savedpc++; /* hooks assume 'pc' is already incremented */ + if (isLua(ci->previous) && + GET_OPCODE(*(ci->previous->u.l.savedpc - 1)) == OP_TAILCALL) { + ci->callstatus |= CIST_TAIL; hook = LUA_HOOKTAILCALL; } luaD_hook(L, hook, -1); - func = L->func; /* previous call can change stack */ - func->stkci.u.l.savedpc--; /* correct 'pc' */ + ci->u.l.savedpc--; /* correct 'pc' */ } @@ -355,25 +356,28 @@ static int moveresults (lua_State *L, StkId firstResult, StkId res, ** moves current number of results to proper place; returns 0 iff call ** wanted multiple (variable number of) results. */ -int luaD_poscall (lua_State *L, StkId firstResult, int nres) { - StkId res = L->func; /* res == final position of 1st result */ - int wanted = res->stkci.nresults; +int luaD_poscall (lua_State *L, CallInfo *ci, StkId firstResult, int nres) { + StkId res; + int wanted = ci->nresults; if (L->hookmask & (LUA_MASKRET | LUA_MASKLINE)) { if (L->hookmask & LUA_MASKRET) { ptrdiff_t fr = savestack(L, firstResult); /* hook may change stack */ luaD_hook(L, LUA_HOOKRET, -1); firstResult = restorestack(L, fr); - res = L->func; } - /* 'oldpc' for caller function */ - L->oldpc = (res - res->stkci.previous)->stkci.u.l.savedpc; + L->oldpc = ci->previous->u.l.savedpc; /* 'oldpc' for caller function */ } - L->func = res - res->stkci.previous; + res = ci->func; /* res == final position of 1st result */ + L->ci = ci->previous; /* back to caller */ /* move results to proper place */ return moveresults(L, firstResult, res, nres, wanted); } + +#define next_ci(L) (L->ci = (L->ci->next ? L->ci->next : luaE_extendCI(L))) + + /* ** Prepares a function call: checks the stack, creates a new CallInfo ** entry, fills in the relevant information, calls hook if needed. @@ -384,6 +388,7 @@ int luaD_poscall (lua_State *L, StkId firstResult, int nres) { int luaD_precall (lua_State *L, StkId func, int nresults) { lua_CFunction f; TValue *funcv = s2v(func); + CallInfo *ci; switch (ttype(funcv)) { case LUA_TCCL: /* C closure */ f = clCvalue(funcv)->f; @@ -393,19 +398,19 @@ int luaD_precall (lua_State *L, StkId func, int nresults) { Cfunc: { int n; /* number of returns */ checkstackp(L, LUA_MINSTACK, func); /* ensure minimum stack size */ - func->stkci.nresults = nresults; - func->stkci.previous = func - L->func; - L->func = func; - setfunctop(func, L->top + LUA_MINSTACK); - lua_assert(functop(func) <= L->stack_last); - callstatus(func) = 0; + ci = next_ci(L); /* now 'enter' new function */ + ci->nresults = nresults; + ci->func = func; + ci->top = L->top + LUA_MINSTACK; + lua_assert(ci->top <= L->stack_last); + ci->callstatus = 0; if (L->hookmask & LUA_MASKCALL) luaD_hook(L, LUA_HOOKCALL, -1); lua_unlock(L); n = (*f)(L); /* do the actual call */ lua_lock(L); api_checknelems(L, n); - luaD_poscall(L, L->top - n, n); + luaD_poscall(L, ci, L->top - n, n); return 1; } case LUA_TLCL: { /* Lua function: prepare its call */ @@ -417,16 +422,15 @@ int luaD_precall (lua_State *L, StkId func, int nresults) { setnilvalue(s2v(L->top++)); /* complete missing arguments */ if (p->is_vararg) luaT_adjustvarargs(L, p, n); - func->stkci.nresults = nresults; - func->stkci.previous = func - L->func; - func->stkci.framesize = fsize + 1; /* size includes function itself */ - L->func = func; - L->top = func + 1 + fsize; - lua_assert(functop(func) <= L->stack_last); - func->stkci.u.l.savedpc = p->code; /* starting point */ - callstatus(func) = 0; + ci = next_ci(L); /* now 'enter' new function */ + ci->nresults = nresults; + ci->func = func; + L->top = ci->top = func + 1 + fsize; + lua_assert(ci->top <= L->stack_last); + ci->u.l.savedpc = p->code; /* starting point */ + ci->callstatus = CIST_LUA; if (L->hookmask & LUA_MASKCALL) - callhook(L); + callhook(L, ci); return 0; } default: { /* not a function */ @@ -483,25 +487,24 @@ void luaD_callnoyield (lua_State *L, StkId func, int nResults) { ** continuation function. */ static void finishCcall (lua_State *L, int status) { - StkId func = L->func; + CallInfo *ci = L->ci; int n; /* must have a continuation and must be able to call it */ - lua_assert(func->stkci.u.c.k != NULL && L->nny == 0); + lua_assert(ci->u.c.k != NULL && L->nny == 0); /* error status can only happen in a protected call */ - lua_assert((callstatus(func) & CIST_YPCALL) || status == LUA_YIELD); - if (callstatus(func) & CIST_YPCALL) { /* was inside a pcall? */ - callstatus(func) &= ~CIST_YPCALL; /* continuation is also inside it */ - L->errfunc = func->stkci.u.c.old_errfunc; /* with same error function */ + lua_assert((ci->callstatus & CIST_YPCALL) || status == LUA_YIELD); + if (ci->callstatus & CIST_YPCALL) { /* was inside a pcall? */ + ci->callstatus &= ~CIST_YPCALL; /* continuation is also inside it */ + L->errfunc = ci->u.c.old_errfunc; /* with the same error function */ } /* finish 'lua_callk'/'lua_pcall'; CIST_YPCALL and 'errfunc' already handled */ - adjustresults(L, func->stkci.nresults); + adjustresults(L, ci->nresults); lua_unlock(L); - /* call continuation function */ - n = (*func->stkci.u.c.k)(L, status, func->stkci.u.c.ctx); + n = (*ci->u.c.k)(L, status, ci->u.c.ctx); /* call continuation function */ lua_lock(L); api_checknelems(L, n); - luaD_poscall(L, L->top - n, n); /* finish 'luaD_precall' */ + luaD_poscall(L, ci, L->top - n, n); /* finish 'luaD_precall' */ } @@ -516,8 +519,8 @@ static void finishCcall (lua_State *L, int status) { static void unroll (lua_State *L, void *ud) { if (ud != NULL) /* error status? */ finishCcall(L, *(int *)ud); /* finish 'lua_pcallk' callee */ - while (L->func != L->stack) { /* something in the stack */ - if (!isLua(L->func)) /* C function? */ + while (L->ci != &L->base_ci) { /* something in the stack */ + if (!isLua(L->ci)) /* C function? */ finishCcall(L, LUA_YIELD); /* complete its execution */ else { /* Lua function */ luaV_finishOp(L); /* finish interrupted instruction */ @@ -531,13 +534,11 @@ static void unroll (lua_State *L, void *ud) { ** Try to find a suspended protected call (a "recover point") for the ** given thread. */ -static StkId findpcall (lua_State *L) { - StkId func; - for (func = L->func; - func->stkci.previous != 0; - func -= func->stkci.previous) { - if (callstatus(func) & CIST_YPCALL) - return func; +static CallInfo *findpcall (lua_State *L) { + CallInfo *ci; + for (ci = L->ci; ci != NULL; ci = ci->previous) { /* search for a pcall */ + if (ci->callstatus & CIST_YPCALL) + return ci; } return NULL; /* no pending pcall */ } @@ -550,17 +551,17 @@ static StkId findpcall (lua_State *L) { */ static int recover (lua_State *L, int status) { StkId oldtop; - StkId recf = findpcall(L); - if (recf == NULL) return 0; /* no recovery point */ + CallInfo *ci = findpcall(L); + if (ci == NULL) return 0; /* no recovery point */ /* "finish" luaD_pcall */ - oldtop = recf + recf->stkci.u2.funcidx; + oldtop = restorestack(L, ci->u2.funcidx); luaF_close(L, oldtop); seterrorobj(L, status, oldtop); - L->func = recf; - L->allowhook = getoah(callstatus(recf)); /* restore original 'allowhook' */ + L->ci = ci; + L->allowhook = getoah(ci->callstatus); /* restore original 'allowhook' */ L->nny = 0; /* should be zero to be yieldable */ luaD_shrinkstack(L); - L->errfunc = recf->stkci.u.c.old_errfunc; + L->errfunc = ci->u.c.old_errfunc; return 1; /* continue running the coroutine */ } @@ -589,7 +590,7 @@ static int resume_error (lua_State *L, const char *msg, int narg) { static void resume (lua_State *L, void *ud) { int n = *(cast(int*, ud)); /* number of arguments */ StkId firstArg = L->top - n; /* first argument */ - StkId func = L->func; + CallInfo *ci = L->ci; if (L->status == LUA_OK) { /* starting a coroutine? */ if (!luaD_precall(L, firstArg - 1, LUA_MULTRET)) /* Lua function? */ luaV_execute(L); /* call it */ @@ -597,18 +598,17 @@ static void resume (lua_State *L, void *ud) { else { /* resuming from previous yield */ lua_assert(L->status == LUA_YIELD); L->status = LUA_OK; /* mark that it is running (again) */ - if (isLua(func)) /* yielded inside a hook? */ + if (isLua(ci)) /* yielded inside a hook? */ luaV_execute(L); /* just continue running Lua code */ else { /* 'common' yield */ - if (func->stkci.u.c.k != NULL) { /* does it have a continuation? */ + if (ci->u.c.k != NULL) { /* does it have a continuation function? */ lua_unlock(L); - /* call continuation */ - n = (*func->stkci.u.c.k)(L, LUA_YIELD, func->stkci.u.c.ctx); + n = (*ci->u.c.k)(L, LUA_YIELD, ci->u.c.ctx); /* call continuation */ lua_lock(L); api_checknelems(L, n); firstArg = L->top - n; /* yield results come from continuation */ } - luaD_poscall(L, firstArg, n); /* finish 'luaD_precall' */ + luaD_poscall(L, ci, firstArg, n); /* finish 'luaD_precall' */ } unroll(L, NULL); /* run continuation */ } @@ -621,7 +621,7 @@ LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs, unsigned short oldnny = L->nny; /* save "number of non-yieldable" calls */ lua_lock(L); if (L->status == LUA_OK) { /* may be starting a coroutine */ - if (L->func != L->stack) /* not in base level? */ + if (L->ci != &L->base_ci) /* not in base level? */ return resume_error(L, "cannot resume non-suspended coroutine", nargs); } else if (L->status != LUA_YIELD) @@ -643,12 +643,12 @@ LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs, if (errorstatus(status)) { /* unrecoverable error? */ L->status = cast_byte(status); /* mark thread as 'dead' */ seterrorobj(L, status, L->top); /* push error message */ - L->func->stkci.framesize = L->top - L->func; + L->ci->top = L->top; } else lua_assert(status == L->status); /* normal end or yield */ } - *nresults = (status == LUA_YIELD) ? L->func->stkci.u2.nyield - : L->top - (L->func + 1); + *nresults = (status == LUA_YIELD) ? L->ci->u2.nyield + : L->top - (L->ci->func + 1); L->nny = oldnny; /* restore 'nny' */ L->nCcalls--; lua_assert(L->nCcalls == ((from) ? from->nCcalls : 0)); @@ -664,7 +664,7 @@ LUA_API int lua_isyieldable (lua_State *L) { LUA_API int lua_yieldk (lua_State *L, int nresults, lua_KContext ctx, lua_KFunction k) { - StkId func = L->func; + CallInfo *ci = L->ci; luai_userstateyield(L, nresults); lua_lock(L); api_checknelems(L, nresults); @@ -675,17 +675,17 @@ LUA_API int lua_yieldk (lua_State *L, int nresults, lua_KContext ctx, luaG_runerror(L, "attempt to yield from outside a coroutine"); } L->status = LUA_YIELD; - if (isLua(func)) { /* inside a hook? */ + if (isLua(ci)) { /* inside a hook? */ api_check(L, k == NULL, "hooks cannot continue after yielding"); - func->stkci.u2.nyield = 0; /* no results */ + ci->u2.nyield = 0; /* no results */ } else { - if ((func->stkci.u.c.k = k) != NULL) /* is there a continuation? */ - func->stkci.u.c.ctx = ctx; /* save context */ - func->stkci.u2.nyield = nresults; /* save number of results */ + if ((ci->u.c.k = k) != NULL) /* is there a continuation? */ + ci->u.c.ctx = ctx; /* save context */ + ci->u2.nyield = nresults; /* save number of results */ luaD_throw(L, LUA_YIELD); } - lua_assert(callstatus(func) & CIST_HOOKED); /* must be inside a hook */ + lua_assert(ci->callstatus & CIST_HOOKED); /* must be inside a hook */ lua_unlock(L); return 0; /* return to 'luaD_hook' */ } @@ -694,7 +694,7 @@ LUA_API int lua_yieldk (lua_State *L, int nresults, lua_KContext ctx, int luaD_pcall (lua_State *L, Pfunc func, void *u, ptrdiff_t old_top, ptrdiff_t ef) { int status; - ptrdiff_t oldfunc = savestack(L, L->func); + CallInfo *old_ci = L->ci; lu_byte old_allowhooks = L->allowhook; unsigned short old_nny = L->nny; ptrdiff_t old_errfunc = L->errfunc; @@ -704,7 +704,7 @@ int luaD_pcall (lua_State *L, Pfunc func, void *u, StkId oldtop = restorestack(L, old_top); luaF_close(L, oldtop); /* close possible pending closures */ seterrorobj(L, status, oldtop); - L->func = restorestack(L, oldfunc); + L->ci = old_ci; L->allowhook = old_allowhooks; L->nny = old_nny; luaD_shrinkstack(L); diff --git a/ldo.h b/ldo.h index 4c03d5941c..3dc16d7160 100644 --- a/ldo.h +++ b/ldo.h @@ -1,5 +1,5 @@ /* -** $Id: ldo.h,v 2.31 2017/06/29 15:06:44 roberto Exp roberto $ +** $Id: ldo.h,v 2.31 2017/06/29 15:06:44 roberto Exp $ ** Stack and Call structure of Lua ** See Copyright Notice in lua.h */ @@ -52,7 +52,8 @@ LUAI_FUNC void luaD_call (lua_State *L, StkId func, int nResults); LUAI_FUNC void luaD_callnoyield (lua_State *L, StkId func, int nResults); LUAI_FUNC int luaD_pcall (lua_State *L, Pfunc func, void *u, ptrdiff_t oldtop, ptrdiff_t ef); -LUAI_FUNC int luaD_poscall (lua_State *L, StkId firstResult, int nres); +LUAI_FUNC int luaD_poscall (lua_State *L, CallInfo *ci, StkId firstResult, + int nres); LUAI_FUNC void luaD_reallocstack (lua_State *L, int newsize); LUAI_FUNC void luaD_growstack (lua_State *L, int n); LUAI_FUNC void luaD_shrinkstack (lua_State *L); diff --git a/lgc.c b/lgc.c index 2155b28d23..036020ae85 100644 --- a/lgc.c +++ b/lgc.c @@ -1,5 +1,5 @@ /* -** $Id: lgc.c,v 2.236 2017/10/31 15:29:28 roberto Exp roberto $ +** $Id: lgc.c,v 2.236 2017/10/31 15:29:28 roberto Exp $ ** Garbage Collector ** See Copyright Notice in lua.h */ @@ -866,9 +866,9 @@ static void GCTM (lua_State *L, int propagateerrors) { setobj2s(L, L->top, tm); /* push finalizer... */ setobj2s(L, L->top + 1, &v); /* ... and its argument */ L->top += 2; /* and (next line) call the finalizer */ - callstatus(L->func) |= CIST_FIN; /* will run a finalizer */ + L->ci->callstatus |= CIST_FIN; /* will run a finalizer */ status = luaD_pcall(L, dothecall, NULL, savestack(L, L->top - 2), 0); - callstatus(L->func) &= ~CIST_FIN; /* not running a finalizer anymore */ + L->ci->callstatus &= ~CIST_FIN; /* not running a finalizer anymore */ L->allowhook = oldah; /* restore hooks */ g->gcrunning = running; /* restore state */ if (status != LUA_OK && propagateerrors) { /* error while running __gc? */ diff --git a/lobject.h b/lobject.h index 56dfebba37..372ec13b1f 100644 --- a/lobject.h +++ b/lobject.h @@ -1,5 +1,5 @@ /* -** $Id: lobject.h,v 2.128 2017/11/03 17:22:54 roberto Exp roberto $ +** $Id: lobject.h,v 2.125 2017/06/29 15:06:44 roberto Exp $ ** Type definitions for Lua objects ** See Copyright Notice in lua.h */ @@ -311,39 +311,9 @@ typedef struct TValue { typedef union StackValue { TValue val; - struct { - TValuefields; - lu_byte callstatus_; - char nresults; /* expected number of results from this function */ - union { - unsigned char funcidx; /* called-function index */ - unsigned char nyield; /* number of values yielded */ - } u2; - unsigned short previous; /* difference to previous 'func' */ - unsigned short framesize; /* stack space available for this function */ - union { - struct { /* only for Lua functions */ - const Instruction *savedpc; - } l; - struct { /* only for C functions */ - lua_KFunction k; /* continuation in case of yields */ - int old_errfunc; - int ctx; /* context info. in case of yields */ - } c; - } u; - } stkci; } StackValue; -#define callstatus(ar) ((ar)->stkci.callstatus_) - -/* top of a function (first element after its frame) */ -#define functop(func) ((func) + (func)->stkci.framesize) - -/* set top of a function to a specific value */ -#define setfunctop(func,v) ((func)->stkci.framesize = (v) - (func)) - - typedef StackValue *StkId; /* index to stack elements */ /* convert a 'StackValue' to a 'TValue' */ diff --git a/lstate.c b/lstate.c index 76daa2e775..7f67527b75 100644 --- a/lstate.c +++ b/lstate.c @@ -1,5 +1,5 @@ /* -** $Id: lstate.c,v 2.144 2017/11/03 12:12:30 roberto Exp roberto $ +** $Id: lstate.c,v 2.143 2017/10/31 17:54:35 roberto Exp $ ** Global State ** See Copyright Notice in lua.h */ @@ -97,8 +97,51 @@ void luaE_setdebt (global_State *g, l_mem debt) { } +CallInfo *luaE_extendCI (lua_State *L) { + CallInfo *ci = luaM_new(L, CallInfo); + lua_assert(L->ci->next == NULL); + L->ci->next = ci; + ci->previous = L->ci; + ci->next = NULL; + L->nci++; + return ci; +} + + +/* +** free all CallInfo structures not in use by a thread +*/ +void luaE_freeCI (lua_State *L) { + CallInfo *ci = L->ci; + CallInfo *next = ci->next; + ci->next = NULL; + while ((ci = next) != NULL) { + next = ci->next; + luaM_free(L, ci); + L->nci--; + } +} + + +/* +** free half of the CallInfo structures not in use by a thread +*/ +void luaE_shrinkCI (lua_State *L) { + CallInfo *ci = L->ci; + CallInfo *next2; /* next's next */ + /* while there are two nexts */ + while (ci->next != NULL && (next2 = ci->next->next) != NULL) { + luaM_free(L, ci->next); /* free next */ + L->nci--; + ci->next = next2; /* remove 'next' from the list */ + next2->previous = ci; + ci = next2; /* keep next's next */ + } +} + + static void stack_init (lua_State *L1, lua_State *L) { - int i; + int i; CallInfo *ci; /* initialize stack array */ L1->stack = luaM_newvector(L, BASIC_STACK_SIZE, StackValue); L1->stacksize = BASIC_STACK_SIZE; @@ -106,19 +149,23 @@ static void stack_init (lua_State *L1, lua_State *L) { setnilvalue(s2v(L1->stack + i)); /* erase new stack */ L1->top = L1->stack; L1->stack_last = L1->stack + L1->stacksize - EXTRA_STACK; - /* initialize first 'function' */ - L1->func = L1->stack; - L1->func->stkci.previous = 0; /* end of linked list */ - L1->func->stkci.framesize = LUA_MINSTACK + 1; - callstatus(L1->func) = 0; + /* initialize first ci */ + ci = &L1->base_ci; + ci->next = ci->previous = NULL; + ci->callstatus = 0; + ci->func = L1->top; setnilvalue(s2v(L1->top)); /* 'function' entry for this 'ci' */ L1->top++; + ci->top = L1->top + LUA_MINSTACK; + L1->ci = ci; } static void freestack (lua_State *L) { if (L->stack == NULL) return; /* stack not completely built yet */ + L->ci = &L->base_ci; /* free the entire 'ci' list */ + luaE_freeCI(L); lua_assert(L->nci == 0); luaM_freearray(L, L->stack, L->stacksize); /* free stack array */ } @@ -168,7 +215,7 @@ static void f_luaopen (lua_State *L, void *ud) { static void preinit_thread (lua_State *L, global_State *g) { G(L) = g; L->stack = NULL; - L->func = NULL; + L->ci = NULL; L->nci = 0; L->stacksize = 0; L->twups = L; /* thread has no upvalues */ diff --git a/lstate.h b/lstate.h index 07cb3d3dfc..4798474c6a 100644 --- a/lstate.h +++ b/lstate.h @@ -1,5 +1,5 @@ /* -** $Id: lstate.h,v 2.148 2017/11/03 17:22:54 roberto Exp roberto $ +** $Id: lstate.h,v 2.146 2017/11/02 11:28:56 roberto Exp $ ** Global State ** See Copyright Notice in lua.h */ @@ -81,21 +81,47 @@ typedef struct stringtable { } stringtable; +/* +** Information about a call. +*/ +typedef struct CallInfo { + StkId func; /* function index in the stack */ + StkId top; /* top for this function */ + struct CallInfo *previous, *next; /* dynamic call link */ + union { + struct { /* only for Lua functions */ + const Instruction *savedpc; + } l; + struct { /* only for C functions */ + lua_KFunction k; /* continuation in case of yields */ + ptrdiff_t old_errfunc; + lua_KContext ctx; /* context info. in case of yields */ + } c; + } u; + union { + int funcidx; /* called-function index */ + int nyield; /* number of values yielded */ + } u2; + short nresults; /* expected number of results from this function */ + unsigned short callstatus; +} CallInfo; + /* ** Bits in CallInfo status */ #define CIST_OAH (1<<0) /* original value of 'allowhook' */ -#define CIST_HOOKED (1<<1) /* call is running a debug hook */ -#define CIST_FRESH (1<<2) /* call is running on a fresh invocation +#define CIST_LUA (1<<1) /* call is running a Lua function */ +#define CIST_HOOKED (1<<2) /* call is running a debug hook */ +#define CIST_FRESH (1<<3) /* call is running on a fresh invocation of luaV_execute */ -#define CIST_YPCALL (1<<3) /* call is a yieldable protected call */ -#define CIST_TAIL (1<<4) /* call was tail called */ -#define CIST_HOOKYIELD (1<<5) /* last hook called yielded */ -#define CIST_LEQ (1<<6) /* using __lt for __le */ -#define CIST_FIN (1<<7) /* call is running a finalizer */ +#define CIST_YPCALL (1<<4) /* call is a yieldable protected call */ +#define CIST_TAIL (1<<5) /* call was tail called */ +#define CIST_HOOKYIELD (1<<6) /* last hook called yielded */ +#define CIST_LEQ (1<<7) /* using __lt for __le */ +#define CIST_FIN (1<<8) /* call is running a finalizer */ -#define isLua(func) isLfunction(s2v(func)) +#define isLua(ci) ((ci)->callstatus & CIST_LUA) /* assume that CIST_OAH has offset 0 and that 'v' is strictly 0/1 */ #define setoah(st,v) ((st) = ((st) & ~CIST_OAH) | (v)) @@ -162,7 +188,7 @@ struct lua_State { lu_byte status; StkId top; /* first free slot in the stack */ global_State *l_G; - StkId func; /* current function */ + CallInfo *ci; /* call info for current function */ const Instruction *oldpc; /* last pc traced */ StkId stack_last; /* last free slot in the stack */ StkId stack; /* stack base */ @@ -170,6 +196,7 @@ struct lua_State { GCObject *gclist; struct lua_State *twups; /* list of threads with open upvalues */ struct lua_longjmp *errorJmp; /* current error recover point */ + CallInfo base_ci; /* CallInfo for first level (C calling Lua) */ volatile lua_Hook hook; ptrdiff_t errfunc; /* current error handling function (stack index) */ int stacksize; @@ -225,6 +252,9 @@ union GCUnion { LUAI_FUNC void luaE_setdebt (global_State *g, l_mem debt); LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1); +LUAI_FUNC CallInfo *luaE_extendCI (lua_State *L); +LUAI_FUNC void luaE_freeCI (lua_State *L); +LUAI_FUNC void luaE_shrinkCI (lua_State *L); #endif diff --git a/ltests.c b/ltests.c index ebf73840dd..bee734f348 100644 --- a/ltests.c +++ b/ltests.c @@ -1,5 +1,5 @@ /* -** $Id: ltests.c,v 2.228 2017/11/03 12:12:30 roberto Exp roberto $ +** $Id: ltests.c,v 2.227 2017/11/02 11:28:56 roberto Exp $ ** Internal Module for Debugging of the Lua Implementation ** See Copyright Notice in lua.h */ @@ -46,7 +46,7 @@ void *l_Trick = 0; int islocked = 0; -#define obj_at(L,k) s2v(L->func + (k)) +#define obj_at(L,k) s2v(L->ci->func + (k)) static int runC (lua_State *L, lua_State *L1, const char *pc); @@ -309,27 +309,28 @@ static void checkLclosure (global_State *g, LClosure *cl) { } -static int lua_checkpc (StkId func) { - if (!isLua(func)) return 1; +static int lua_checkpc (CallInfo *ci) { + if (!isLua(ci)) return 1; else { - Proto *p = clLvalue(s2v(func))->p; - return p->code <= func->stkci.u.l.savedpc && - func->stkci.u.l.savedpc <= p->code + p->sizecode; + StkId f = ci->func; + Proto *p = clLvalue(s2v(f))->p; + return p->code <= ci->u.l.savedpc && + ci->u.l.savedpc <= p->code + p->sizecode; } } static void checkstack (global_State *g, lua_State *L1) { StkId o; + CallInfo *ci; UpVal *uv; lua_assert(!isdead(g, L1)); for (uv = L1->openupval; uv != NULL; uv = uv->u.open.next) lua_assert(upisopen(uv)); /* must be open */ - for (o = L1->func; o->stkci.previous != 0; o -= o->stkci.previous) { - lua_assert(functop(o) <= L1->stack_last); - lua_assert(lua_checkpc(o)); + for (ci = L1->ci; ci != NULL; ci = ci->previous) { + lua_assert(ci->top <= L1->stack_last); + lua_assert(lua_checkpc(ci)); } - lua_assert(o == L1->stack); if (L1->stack) { /* complete thread? */ for (o = L1->stack; o < L1->stack_last + EXTRA_STACK; o++) checkliveness(L1, s2v(o)); /* entire stack must have valid values */ diff --git a/ltm.c b/ltm.c index 6c13e8d132..91f622e49d 100644 --- a/ltm.c +++ b/ltm.c @@ -1,5 +1,5 @@ /* -** $Id: ltm.c,v 2.45 2017/10/04 15:49:05 roberto Exp roberto $ +** $Id: ltm.c,v 2.45 2017/10/04 15:49:05 roberto Exp $ ** Tag methods ** See Copyright Notice in lua.h */ @@ -108,7 +108,7 @@ void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1, setobj2s(L, func + 3, p3); /* 3rd argument */ L->top += 4; /* metamethod may yield only when called from Lua code */ - if (isLua(L->func)) + if (isLua(L->ci)) luaD_call(L, func, 0); else luaD_callnoyield(L, func, 0); @@ -124,7 +124,7 @@ void luaT_callTMres (lua_State *L, const TValue *f, const TValue *p1, setobj2s(L, func + 2, p2); /* 2nd argument */ L->top += 3; /* metamethod may yield only when called from Lua code */ - if (isLua(L->func)) + if (isLua(L->ci)) luaD_call(L, func, 1); else luaD_callnoyield(L, func, 1); diff --git a/lua.h b/lua.h index fe97e8a6c1..2d8ff8382e 100644 --- a/lua.h +++ b/lua.h @@ -1,5 +1,5 @@ /* -** $Id: lua.h,v 1.337 2017/11/02 11:28:56 roberto Exp roberto $ +** $Id: lua.h,v 1.337 2017/11/02 11:28:56 roberto Exp $ ** Lua - A Scripting Language ** Lua.org, PUC-Rio, Brazil (http://www.lua.org) ** See Copyright Notice at the end of this file @@ -456,8 +456,7 @@ struct lua_Debug { char istailcall; /* (t) */ char short_src[LUA_IDSIZE]; /* (S) */ /* private part */ - int i_actf; /* active function */ - lua_State *i_actL; /* where active function is active */ + struct CallInfo *i_ci; /* active function */ }; /* }====================================================================== */ diff --git a/lvm.c b/lvm.c index b1cf4666b8..b5e1c813a1 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.304 2017/11/03 19:33:22 roberto Exp roberto $ +** $Id: lvm.c,v 2.301 2017/11/01 18:20:48 roberto Exp $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -390,9 +390,9 @@ int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r) { else if ((res = luaT_callorderTM(L, l, r, TM_LE)) >= 0) /* try 'le' */ return res; else { /* try 'lt': */ - callstatus(L->func) |= CIST_LEQ; /* mark it is doing 'lt' for 'le' */ + L->ci->callstatus |= CIST_LEQ; /* mark it is doing 'lt' for 'le' */ res = luaT_callorderTM(L, r, l, TM_LT); - callstatus(L->func) ^= CIST_LEQ; /* clear mark */ + L->ci->callstatus ^= CIST_LEQ; /* clear mark */ if (res < 0) luaG_ordererror(L, l, r); return !res; /* result is negated */ @@ -654,14 +654,13 @@ static void pushclosure (lua_State *L, Proto *p, UpVal **encup, StkId base, } -#define basepc(base) ((base - 1)->stkci.u.l.savedpc) - /* ** finish execution of an opcode interrupted by an yield */ void luaV_finishOp (lua_State *L) { - StkId base = L->func + 1; - Instruction inst = *(basepc(base) - 1); /* interrupted instruction */ + CallInfo *ci = L->ci; + StkId base = ci->func + 1; + Instruction inst = *(ci->u.l.savedpc - 1); /* interrupted instruction */ OpCode op = GET_OPCODE(inst); switch (op) { /* finish its execution */ case OP_ADDI: case OP_SUBI: @@ -680,14 +679,14 @@ void luaV_finishOp (lua_State *L) { case OP_LE: case OP_LT: case OP_EQ: { int res = !l_isfalse(s2v(L->top - 1)); L->top--; - if (callstatus(base - 1) & CIST_LEQ) { /* "<=" using "<" ? */ + if (ci->callstatus & CIST_LEQ) { /* "<=" using "<" instead? */ lua_assert(op == OP_LE); - callstatus(base - 1) ^= CIST_LEQ; /* clear mark */ + ci->callstatus ^= CIST_LEQ; /* clear mark */ res = !res; /* negate result */ } - lua_assert(GET_OPCODE(*basepc(base)) == OP_JMP); + lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_JMP); if (res != GETARG_A(inst)) /* condition failed? */ - basepc(base)++; /* skip jump instruction */ + ci->u.l.savedpc++; /* skip jump instruction */ break; } case OP_CONCAT: { @@ -700,18 +699,18 @@ void luaV_finishOp (lua_State *L) { luaV_concat(L, total); /* concat them (may yield again) */ } /* move final result to final position */ - setobjs2s(L, L->func + 1 + GETARG_A(inst), L->top - 1); - L->top = functop(base - 1); /* restore top */ + setobjs2s(L, ci->func + 1 + GETARG_A(inst), L->top - 1); + L->top = ci->top; /* restore top */ break; } case OP_TFORCALL: { - lua_assert(GET_OPCODE(*basepc(base)) == OP_TFORLOOP); - L->top = functop(base - 1); /* correct top */ + lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_TFORLOOP); + L->top = ci->top; /* correct top */ break; } case OP_CALL: { if (GETARG_C(inst) - 1 >= 0) /* nresults >= 0? */ - L->top = functop(base - 1); /* adjust results */ + L->top = ci->top; /* adjust results */ break; } case OP_TAILCALL: case OP_SETTABUP: case OP_SETTABLE: @@ -754,33 +753,31 @@ void luaV_finishOp (lua_State *L) { ** Execute a jump instruction. The 'updatemask' allows signals to stop ** tight loops. (Without it, the local copy of 'mask' could never change.) */ -#define dojump(i,e) { pc += GETARG_sBx(i) + e; updatemask(L); } +#define dojump(ci,i,e) { pc += GETARG_sBx(i) + e; updatemask(L); } /* for test instructions, execute the jump instruction that follows it */ -#define donextjump() { i = *pc; dojump(i, 1); } +#define donextjump(ci) { i = *pc; dojump(ci, i, 1); } /* ** Whenever code can raise errors (including memory errors), the global ** 'pc' must be correct to report occasional errors. */ -#define savepc(base) (basepc(base) = pc) - +#define savepc(L) (ci->u.l.savedpc = pc) -/* update internal copies to its correct values */ -#define updatestate() (base = L->func + 1, updatemask(L)) /* ** Protect code that, in general, can raise errors, reallocate the ** stack, and change the hooks. */ -#define Protect(code) { savepc(base); {code;}; updatestate(); } +#define Protect(code) \ + { savepc(L); {code;}; base = ci->func + 1; updatemask(L); } #define checkGC(L,c) \ - { luaC_condGC(L, L->top = (c), /* limit of live values */ \ - {updatestate(); L->top = functop(base - 1);}); /* restore top */ \ - luai_threadyield(L); } + { luaC_condGC(L, L->top = (c), /* limit of live values */ \ + Protect(L->top = ci->top)); /* restore top */ \ + luai_threadyield(L); } /* fetch an instruction and prepare its execution */ @@ -796,23 +793,26 @@ void luaV_finishOp (lua_State *L) { void luaV_execute (lua_State *L) { + CallInfo *ci = L->ci; LClosure *cl; TValue *k; - StkId base = L->func + 1; /* local copy of 'L->func + 1' */ + StkId base; /* local copy of 'ci->func + 1' */ int mask; /* local copy of 'L->hookmask & (LUA_MASKLINE | LUA_MASKCOUNT)' */ - const Instruction *pc; /* local copy of 'basepc(base)' */ - callstatus(base - 1) |= CIST_FRESH; /* fresh invocation of 'luaV_execute" */ + const Instruction *pc; /* local copy of 'ci->u.l.savedpc' */ + ci->callstatus |= CIST_FRESH; /* fresh invocation of 'luaV_execute" */ newframe: /* reentry point when frame changes (call/return) */ - cl = clLvalue(s2v(L->func)); /* local reference to function's closure */ + lua_assert(ci == L->ci); + cl = clLvalue(s2v(ci->func)); /* local reference to function's closure */ k = cl->p->k; /* local reference to function's constant table */ updatemask(L); - pc = basepc(base); + base = ci->func + 1; + pc = ci->u.l.savedpc; /* main loop of interpreter */ for (;;) { Instruction i; StkId ra; vmfetch(); - lua_assert(base == L->func + 1); + lua_assert(base == ci->func + 1); lua_assert(base <= L->top && L->top < L->stack + L->stacksize); vmdispatch (GET_OPCODE(i)) { vmcase(OP_MOVE) { @@ -970,7 +970,7 @@ void luaV_execute (lua_State *L) { int b = GETARG_B(i); int c = GETARG_C(i); Table *t; - savepc(base); /* in case of allocation errors */ + savepc(L); /* in case of allocation errors */ t = luaH_new(L); sethvalue2s(L, ra, t); if (b != 0 || c != 0) @@ -1276,7 +1276,7 @@ void luaV_execute (lua_State *L) { rb = base + b; setobjs2s(L, ra, rb); checkGC(L, (ra >= rb ? ra + 1 : rb)); - L->top = functop(base - 1); /* restore top */ + L->top = ci->top; /* restore top */ vmbreak; } vmcase(OP_CLOSE) { @@ -1284,7 +1284,7 @@ void luaV_execute (lua_State *L) { vmbreak; } vmcase(OP_JMP) { - dojump(i, 0); + dojump(ci, i, 0); vmbreak; } vmcase(OP_EQ) { @@ -1294,7 +1294,7 @@ void luaV_execute (lua_State *L) { if (luaV_equalobj(L, rb, rc) != GETARG_A(i)) pc++; else - donextjump(); + donextjump(ci); ) vmbreak; } @@ -1310,7 +1310,7 @@ void luaV_execute (lua_State *L) { if (res != GETARG_A(i)) pc++; else - donextjump(); + donextjump(ci); vmbreak; } vmcase(OP_LE) { @@ -1325,14 +1325,14 @@ void luaV_execute (lua_State *L) { if (res != GETARG_A(i)) pc++; else - donextjump(); + donextjump(ci); vmbreak; } vmcase(OP_TEST) { if (GETARG_C(i) ? l_isfalse(s2v(ra)) : !l_isfalse(s2v(ra))) pc++; else - donextjump(); + donextjump(ci); vmbreak; } vmcase(OP_TESTSET) { @@ -1341,7 +1341,7 @@ void luaV_execute (lua_State *L) { pc++; else { setobj2s(L, ra, rb); - donextjump(); + donextjump(ci); } vmbreak; } @@ -1355,11 +1355,11 @@ void luaV_execute (lua_State *L) { Protect(isC = luaD_precall(L, ra, nresults)); if (isC) { /* C function? */ if (nresults >= 0) /* fixed number of results? */ - L->top = functop(base - 1); /* correct top */ + L->top = ci->top; /* correct top */ /* else leave top for next instruction */ } else { /* Lua function */ - base = L->func + 1; + ci = L->ci; goto newframe; /* restart luaV_execute over new Lua function */ } vmbreak; @@ -1368,14 +1368,16 @@ void luaV_execute (lua_State *L) { int b = GETARG_B(i); if (b != 0) L->top = ra+b; /* else previous instruction set top */ lua_assert(GETARG_C(i) - 1 == LUA_MULTRET); - savepc(base); + savepc(L); if (luaD_precall(L, ra, LUA_MULTRET)) { /* C function? */ - updatestate(); /* update 'base' */ + Protect((void)0); /* update 'base' */ } else { /* tail call: put called frame (n) in place of caller one (o) */ - StkId nfunc = L->func; /* called function */ - StkId ofunc = nfunc - nfunc->stkci.previous; /* caller function */ + CallInfo *nci = L->ci; /* called frame (new) */ + CallInfo *oci = nci->previous; /* caller frame (old) */ + StkId nfunc = nci->func; /* called function */ + StkId ofunc = oci->func; /* caller function */ /* last stack slot filled by 'precall' */ StkId lim = nfunc + 1 + getproto(s2v(nfunc))->numparams; int aux; @@ -1384,13 +1386,11 @@ void luaV_execute (lua_State *L) { /* move new frame into old one */ for (aux = 0; nfunc + aux < lim; aux++) setobjs2s(L, ofunc + aux, nfunc + aux); - ofunc->stkci.framesize = L->top - nfunc; - L->top = functop(ofunc); /* correct top */ - ofunc->stkci.u.l.savedpc = nfunc->stkci.u.l.savedpc; - callstatus(ofunc) |= CIST_TAIL; /* function was tail called */ - base = ofunc + 1; - L->func = ofunc; - lua_assert(L->top == base + getproto(s2v(ofunc))->maxstacksize); + oci->top = L->top = ofunc + (L->top - nfunc); /* correct top */ + oci->u.l.savedpc = nci->u.l.savedpc; + oci->callstatus |= CIST_TAIL; /* function was tail called */ + ci = L->ci = oci; /* remove new frame */ + lua_assert(L->top == ofunc + 1 + getproto(s2v(ofunc))->maxstacksize); goto newframe; /* restart luaV_execute over new Lua function */ } vmbreak; @@ -1398,16 +1398,16 @@ void luaV_execute (lua_State *L) { vmcase(OP_RETURN) { int b = GETARG_B(i); if (cl->p->sizep > 0) luaF_close(L, base); - savepc(base); - b = luaD_poscall(L, ra, (b != 0 ? b - 1 : cast_int(L->top - ra))); - if (callstatus(base - 1) & CIST_FRESH) /* local 'base' still from callee */ + savepc(L); + b = luaD_poscall(L, ci, ra, (b != 0 ? b - 1 : cast_int(L->top - ra))); + if (ci->callstatus & CIST_FRESH) /* local 'ci' still from callee */ return; /* external invocation: return */ else { /* invocation via reentry: continue execution */ - base = L->func + 1; - if (b) L->top = functop(base - 1); - lua_assert(isLua(base - 1)); - lua_assert(GET_OPCODE(*(basepc(base) - 1)) == OP_CALL); - goto newframe; /* restart luaV_execute over previous Lua function */ + ci = L->ci; + if (b) L->top = ci->top; + lua_assert(isLua(ci)); + lua_assert(GET_OPCODE(*((ci)->u.l.savedpc - 1)) == OP_CALL); + goto newframe; /* restart luaV_execute over new Lua function */ } } vmcase(OP_FORLOOP) { @@ -1451,7 +1451,7 @@ void luaV_execute (lua_State *L) { } else { /* try making all values floats */ lua_Number ninit; lua_Number nlimit; lua_Number nstep; - savepc(base); /* in case of errors */ + savepc(L); /* in case of errors */ if (!tonumber(plimit, &nlimit)) luaG_runerror(L, "'for' limit must be a number"); setfltvalue(plimit, nlimit); @@ -1472,7 +1472,7 @@ void luaV_execute (lua_State *L) { setobjs2s(L, cb, ra); L->top = cb + 3; /* func. + 2 args (state and index) */ Protect(luaD_call(L, cb, GETARG_C(i))); - L->top = functop(base - 1); + L->top = ci->top; i = *(pc++); /* go to next instruction */ ra = RA(i); lua_assert(GET_OPCODE(i) == OP_TFORLOOP); @@ -1497,7 +1497,7 @@ void luaV_execute (lua_State *L) { } h = hvalue(s2v(ra)); last = ((c-1)*LFIELDS_PER_FLUSH) + n; - savepc(base); /* in case of allocation errors */ + savepc(L); /* in case of allocation errors */ if (last > h->sizearray) /* needs more space? */ luaH_resizearray(L, h, last); /* preallocate it at once */ for (; n > 0; n--) { @@ -1506,15 +1506,14 @@ void luaV_execute (lua_State *L) { last--; luaC_barrierback(L, h, val); } - /* correct top (in case of previous open call) */ - L->top = functop(base - 1); + L->top = ci->top; /* correct top (in case of previous open call) */ vmbreak; } vmcase(OP_CLOSURE) { Proto *p = cl->p->p[GETARG_Bx(i)]; LClosure *ncl = getcached(p, cl->upvals, base); /* cached closure */ if (ncl == NULL) { /* no match? */ - savepc(base); /* in case of allocation errors */ + savepc(L); /* in case of allocation errors */ pushclosure(L, p, cl->upvals, base, ra); /* create a new one */ } else From c3e5946fb2b7b5781d9bca9d303967abe6263482 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 7 Nov 2017 15:20:42 -0200 Subject: [PATCH 0109/1145] new format for JUMP instructions (to allow larger offsets) --- lcode.c | 35 +++++++++++++++++++++++++---------- ldebug.c | 4 ++-- lopcodes.c | 4 ++-- lopcodes.h | 30 +++++++++++++++++++++++++----- ltests.c | 12 +++++++++--- lvm.c | 4 ++-- 6 files changed, 65 insertions(+), 24 deletions(-) diff --git a/lcode.c b/lcode.c index f94afb0b1a..5ca597eb15 100644 --- a/lcode.c +++ b/lcode.c @@ -1,5 +1,5 @@ /* -** $Id: lcode.c,v 2.129 2017/10/04 15:49:24 roberto Exp roberto $ +** $Id: lcode.c,v 2.130 2017/10/04 21:56:32 roberto Exp roberto $ ** Code generator for Lua ** See Copyright Notice in lua.h */ @@ -37,6 +37,9 @@ #define hasjumps(e) ((e)->t != (e)->f) +static int codesJ (FuncState *fs, OpCode o, int sj, int k); + + /* ** If expression is a numeric constant, fills 'v' with its value ** and returns 1. Otherwise, returns 0. @@ -89,7 +92,7 @@ void luaK_nil (FuncState *fs, int from, int n) { ** a list of jumps. */ static int getjump (FuncState *fs, int pc) { - int offset = GETARG_sBx(fs->f->code[pc]); + int offset = GETARG_sJ(fs->f->code[pc]); if (offset == NO_JUMP) /* point to itself represents end of list */ return NO_JUMP; /* end of list */ else @@ -105,9 +108,10 @@ static void fixjump (FuncState *fs, int pc, int dest) { Instruction *jmp = &fs->f->code[pc]; int offset = dest - (pc + 1); lua_assert(dest != NO_JUMP); - if (abs(offset) > MAXARG_sBx) + if (abs(offset) > MAXARG_sJ) luaX_syntaxerror(fs->ls, "control structure too long"); - SETARG_sBx(*jmp, offset); + lua_assert(GET_OPCODE(*jmp) == OP_JMP); + SETARG_sJ(*jmp, offset); } @@ -138,7 +142,7 @@ int luaK_jump (FuncState *fs) { int jpc = fs->jpc; /* save list of jumps to here */ int j; fs->jpc = NO_JUMP; /* no more jumps to here */ - j = luaK_codeAsBx(fs, OP_JMP, 0, NO_JUMP); + j = codesJ(fs, OP_JMP, NO_JUMP, 0); luaK_concat(fs, &j, jpc); /* keep them on hold */ return j; } @@ -286,16 +290,16 @@ int luaK_needclose (FuncState *fs, int list) { /* ** Correct a jump list to jump to 'target'. If 'hasclose' is true, ** 'target' contains an OP_CLOSE instruction (see first assert). -** Only jumps with the A arg true need that close; other jumps +** Only jumps with the 'k' arg true need that close; other jumps ** avoid it jumping to the next instruction. */ void luaK_patchgoto (FuncState *fs, int list, int target, int hasclose) { lua_assert(!hasclose || GET_OPCODE(fs->f->code[target]) == OP_CLOSE); while (list != NO_JUMP) { int next = getjump(fs, list); - lua_assert(!GETARG_A(fs->f->code[list]) || hasclose); + lua_assert(!GETARG_k(fs->f->code[list]) || hasclose); patchtestreg(fs, list, NO_REG); /* do not generate values */ - if (!hasclose || GETARG_A(fs->f->code[list])) + if (!hasclose || GETARG_k(fs->f->code[list])) fixjump(fs, list, target); else /* there is a CLOSE instruction but jump does not need it */ fixjump(fs, list, target + 1); /* avoid CLOSE instruction */ @@ -305,14 +309,14 @@ void luaK_patchgoto (FuncState *fs, int list, int target, int hasclose) { /* -** Mark (using the A arg) all jumps in 'list' to close upvalues. Mark +** Mark (using the 'k' arg) all jumps in 'list' to close upvalues. Mark ** will instruct 'luaK_patchgoto' to make these jumps go to OP_CLOSE ** instructions. */ void luaK_patchclose (FuncState *fs, int list) { for (; list != NO_JUMP; list = getjump(fs, list)) { lua_assert(GET_OPCODE(fs->f->code[list]) == OP_JMP); - SETARG_A(fs->f->code[list], 1); + SETARG_k(fs->f->code[list], 1); } } @@ -398,6 +402,17 @@ int luaK_codeAsBx (FuncState *fs, OpCode o, int a, int bc) { } +/* +** Format and emit an 'isJ' instruction. +*/ +static int codesJ (FuncState *fs, OpCode o, int sj, int k) { + unsigned int j = sj + MAXARG_sJ; + lua_assert(getOpMode(o) == isJ); + lua_assert(j <= MAXARG_sJ && (k & ~1) == 0); + return luaK_code(fs, CREATE_sJ(o, j, k)); +} + + /* ** Emit an "extra argument" instruction (format 'iAx') */ diff --git a/ldebug.c b/ldebug.c index 83ddde72e5..e1f9c01533 100644 --- a/ldebug.c +++ b/ldebug.c @@ -1,5 +1,5 @@ /* -** $Id: ldebug.c,v 2.135 2017/11/02 11:28:56 roberto Exp $ +** $Id: ldebug.c,v 2.140 2017/11/07 13:25:26 roberto Exp roberto $ ** Debug Interface ** See Copyright Notice in lua.h */ @@ -442,7 +442,7 @@ static int findsetreg (Proto *p, int lastpc, int reg) { break; } case OP_JMP: { /* doesn't change registers, but changes 'jmptarget' */ - int b = GETARG_sBx(i); + int b = GETARG_sJ(i); int dest = pc + 1 + b; /* jump does not skip 'lastpc' and is larger than current one? */ if (dest <= lastpc && dest > jmptarget) diff --git a/lopcodes.c b/lopcodes.c index 42ce73d902..6ea54d094f 100644 --- a/lopcodes.c +++ b/lopcodes.c @@ -1,5 +1,5 @@ /* -** $Id: lopcodes.c,v 1.65 2017/09/28 16:53:29 roberto Exp roberto $ +** $Id: lopcodes.c,v 1.66 2017/10/04 15:49:24 roberto Exp roberto $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ @@ -129,7 +129,7 @@ LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = { ,opmode(0, 1, iABC) /* OP_LEN */ ,opmode(0, 1, iABC) /* OP_CONCAT */ ,opmode(0, 0, iABC) /* OP_CLOSE */ - ,opmode(0, 0, iAsBx) /* OP_JMP */ + ,opmode(0, 0, isJ) /* OP_JMP */ ,opmode(1, 0, iABC) /* OP_EQ */ ,opmode(1, 0, iABC) /* OP_LT */ ,opmode(1, 0, iABC) /* OP_LE */ diff --git a/lopcodes.h b/lopcodes.h index de0c5cc129..bc8d722f95 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -1,5 +1,5 @@ /* -** $Id: lopcodes.h,v 1.165 2017/10/04 15:49:24 roberto Exp roberto $ +** $Id: lopcodes.h,v 1.166 2017/10/04 21:56:32 roberto Exp roberto $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ @@ -21,6 +21,7 @@ iABC |k| C(8) | | B(8) | | A(8) | | Op(7) | iABx | Bx(17) | | A(8) | | Op(7) | iAsBx | sBx (signed)(17) | | A(8) | | Op(7) | iAx | Ax(25) | | Op(7) | +iksJ |k| sJ(24) | | Op(7) | A signed argument is represented in excess K: the represented value is the written unsigned value minus K, where K is half the maximum for the @@ -28,7 +29,7 @@ iAx | Ax(25) | | Op(7) | ===========================================================================*/ -enum OpMode {iABC, iABx, iAsBx, iAx}; /* basic instruction format */ +enum OpMode {iABC, iABx, iAsBx, iAx, isJ}; /* basic instruction formats */ /* @@ -40,6 +41,8 @@ enum OpMode {iABC, iABx, iAsBx, iAx}; /* basic instruction format */ #define SIZE_Bx (SIZE_Cx + SIZE_B) #define SIZE_A 8 #define SIZE_Ax (SIZE_Cx + SIZE_B + SIZE_A) +#define SIZE_sJ (SIZE_C + SIZE_B + SIZE_A) + #define SIZE_OP 7 @@ -50,6 +53,7 @@ enum OpMode {iABC, iABx, iAsBx, iAx}; /* basic instruction format */ #define POS_k (POS_C + SIZE_C) #define POS_Bx POS_B #define POS_Ax POS_A +#define POS_sJ POS_A /* @@ -71,6 +75,12 @@ enum OpMode {iABC, iABx, iAsBx, iAx}; /* basic instruction format */ #define MAXARG_Ax MAX_INT #endif +#if SIZE_sJ < LUAI_BITSINT-1 +#define MAXARG_sJ ((1 << (SIZE_sJ - 1)) - 1) +#else +#define MAXARG_sJ MAX_INT +#endif + #define MAXARG_A ((1<= R(A) */ -OP_JMP,/* sBx pc+=sBx */ +OP_JMP,/* k sJ pc += sJ (k is used in code generation) */ OP_EQ,/* A B C if ((R(B) == R(C)) ~= A) then pc++ */ OP_LT,/* A B C if ((R(B) < R(C)) ~= A) then pc++ */ OP_LE,/* A B C if ((R(B) <= R(C)) ~= A) then pc++ */ diff --git a/ltests.c b/ltests.c index bee734f348..5f7b406469 100644 --- a/ltests.c +++ b/ltests.c @@ -1,5 +1,5 @@ /* -** $Id: ltests.c,v 2.227 2017/11/02 11:28:56 roberto Exp $ +** $Id: ltests.c,v 2.230 2017/11/07 13:25:26 roberto Exp roberto $ ** Internal Module for Debugging of the Lua Implementation ** See Copyright Notice in lua.h */ @@ -543,14 +543,20 @@ static char *buildop (Proto *p, int pc, char *buff) { GETARG_k(i) ? " (k)" : ""); break; case iABx: - sprintf(buff+strlen(buff), "%-12s%4d %4d", name, GETARG_A(i), GETARG_Bx(i)); + sprintf(buff+strlen(buff), "%-12s%4d %4d", name, GETARG_A(i), + GETARG_Bx(i)); break; case iAsBx: - sprintf(buff+strlen(buff), "%-12s%4d %4d", name, GETARG_A(i), GETARG_sBx(i)); + sprintf(buff+strlen(buff), "%-12s%4d %4d", name, GETARG_A(i), + GETARG_sBx(i)); break; case iAx: sprintf(buff+strlen(buff), "%-12s%4d", name, GETARG_Ax(i)); break; + case isJ: + sprintf(buff+strlen(buff), "%-12s%4d (%1d)", name, GETARG_sJ(i), + !!GETARG_k(i)); + break; } return buff; } diff --git a/lvm.c b/lvm.c index b5e1c813a1..009dfc5786 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.301 2017/11/01 18:20:48 roberto Exp $ +** $Id: lvm.c,v 2.306 2017/11/07 13:25:26 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -753,7 +753,7 @@ void luaV_finishOp (lua_State *L) { ** Execute a jump instruction. The 'updatemask' allows signals to stop ** tight loops. (Without it, the local copy of 'mask' could never change.) */ -#define dojump(ci,i,e) { pc += GETARG_sBx(i) + e; updatemask(L); } +#define dojump(ci,i,e) { pc += GETARG_sJ(i) + e; updatemask(L); } /* for test instructions, execute the jump instruction that follows it */ From 26679ea35bf261f2b5edc2fb2f5cc4df483fd50d Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 8 Nov 2017 12:50:23 -0200 Subject: [PATCH 0110/1145] new function 'luaV_flttointeger' to convert floats to integers (without string coercions) + string operands to bitwise operations handled by string metamethods --- lcode.c | 6 ++--- ldebug.c | 4 +-- lobject.c | 4 +-- lstrlib.c | 80 ++++++++++++++++++++++++++++++++++++++++++++++++------- ltable.c | 6 ++--- ltm.c | 5 ++-- lvm.c | 60 ++++++++++++++++++++++++----------------- lvm.h | 9 ++++++- 8 files changed, 126 insertions(+), 48 deletions(-) diff --git a/lcode.c b/lcode.c index 5ca597eb15..c36a49ad7b 100644 --- a/lcode.c +++ b/lcode.c @@ -1,5 +1,5 @@ /* -** $Id: lcode.c,v 2.130 2017/10/04 21:56:32 roberto Exp roberto $ +** $Id: lcode.c,v 2.131 2017/11/07 17:20:42 roberto Exp roberto $ ** Code generator for Lua ** See Copyright Notice in lua.h */ @@ -611,7 +611,7 @@ static void luaK_float (FuncState *fs, int reg, lua_Number f) { TValue v; lua_Integer fi; setfltvalue(&v, f); - if (luaV_tointeger(&v, &fi, 0) && + if (luaV_flttointeger(&v, &fi, 0) && l_castS2U(fi) + MAXARG_sBx <= l_castS2U(MAXARG_Bx)) luaK_codeAsBx(fs, OP_LOADF, reg, cast_int(fi)); else @@ -1146,7 +1146,7 @@ static int validop (int op, TValue *v1, TValue *v2) { case LUA_OPBAND: case LUA_OPBOR: case LUA_OPBXOR: case LUA_OPSHL: case LUA_OPSHR: case LUA_OPBNOT: { /* conversion errors */ lua_Integer i; - return (tointeger(v1, &i) && tointeger(v2, &i)); + return (tointegerns(v1, &i) && tointegerns(v2, &i)); } case LUA_OPDIV: case LUA_OPIDIV: case LUA_OPMOD: /* division by 0 */ return (nvalue(v2) != 0); diff --git a/ldebug.c b/ldebug.c index e1f9c01533..037809a2f8 100644 --- a/ldebug.c +++ b/ldebug.c @@ -1,5 +1,5 @@ /* -** $Id: ldebug.c,v 2.140 2017/11/07 13:25:26 roberto Exp roberto $ +** $Id: ldebug.c,v 2.141 2017/11/07 17:20:42 roberto Exp roberto $ ** Debug Interface ** See Copyright Notice in lua.h */ @@ -670,7 +670,7 @@ l_noret luaG_opinterror (lua_State *L, const TValue *p1, */ l_noret luaG_tointerror (lua_State *L, const TValue *p1, const TValue *p2) { lua_Integer temp; - if (!tointeger(p1, &temp)) + if (!tointegerns(p1, &temp)) p2 = p1; luaG_runerror(L, "number%s has no integer representation", varinfo(L, p2)); } diff --git a/lobject.c b/lobject.c index 470d75db42..32cdc4b0f7 100644 --- a/lobject.c +++ b/lobject.c @@ -1,5 +1,5 @@ /* -** $Id: lobject.c,v 2.117 2017/07/07 16:34:32 roberto Exp roberto $ +** $Id: lobject.c,v 2.118 2017/10/10 20:05:40 roberto Exp roberto $ ** Some generic functions over Lua objects ** See Copyright Notice in lua.h */ @@ -127,7 +127,7 @@ int luaO_rawarith (lua_State *L, int op, const TValue *p1, const TValue *p2, case LUA_OPSHL: case LUA_OPSHR: case LUA_OPBNOT: { /* operate only on integers */ lua_Integer i1; lua_Integer i2; - if (tointeger(p1, &i1) && tointeger(p2, &i2)) { + if (tointegerns(p1, &i1) && tointegerns(p2, &i2)) { setivalue(res, intarith(L, op, i1, i2)); return 1; } diff --git a/lstrlib.c b/lstrlib.c index c7cfa4214a..626c9159e1 100644 --- a/lstrlib.c +++ b/lstrlib.c @@ -1,5 +1,5 @@ /* -** $Id: lstrlib.c,v 1.256 2017/05/19 16:29:40 roberto Exp roberto $ +** $Id: lstrlib.c,v 1.257 2017/07/07 16:34:32 roberto Exp roberto $ ** Standard library for string operations and pattern-matching ** See Copyright Notice in lua.h */ @@ -220,17 +220,49 @@ static int tonum (lua_State *L, int arg) { } +static int toint (lua_State *L, int arg) { + if (!tonum(L, arg)) + return 0; /* not coercible to a number */ + else if (lua_isinteger(L, arg)) + return 1; /* already an integer */ + else { /* a float */ + int ok; + lua_Integer n = lua_tointegerx(L, arg, &ok); + if (!ok) + return 0; + else { + lua_pop(L, 1); /* remove the float */ + lua_pushinteger(L, n); /* push an integer */ + return 1; + } + } +} + + +static void trymt (lua_State *L, const char *mtname) { + lua_settop(L, 2); /* back to the original arguments */ + if (lua_type(L, 2) == LUA_TSTRING || !luaL_getmetafield(L, 2, mtname)) + luaL_error(L, "attempt to %s a '%s' with a '%s'", mtname + 2, + luaL_typename(L, -2), luaL_typename(L, -1)); + lua_insert(L, -3); /* put metamethod before arguments */ + lua_call(L, 2, 1); /* call metamethod */ +} + + static int arith (lua_State *L, int op, const char *mtname) { if (tonum(L, 1) && tonum(L, 2)) lua_arith(L, op); /* result will be on the top */ - else { - lua_settop(L, 2); /* back to the original arguments */ - if (lua_type(L, 2) == LUA_TSTRING || !luaL_getmetafield(L, 2, mtname)) - return luaL_error(L, "attempt to %s a '%s' with a '%s'", mtname + 2, - luaL_typename(L, -2), luaL_typename(L, -1)); - lua_insert(L, -3); /* put metamethod before arguments */ - lua_call(L, 2, 1); /* call metamethod */ - } + else + trymt(L, mtname); + return 1; +} + + +static int bitwise (lua_State *L, int op, const char *mtname) { + if (toint(L, 1) && toint(L, 2)) + lua_arith(L, op); /* result will be on the top */ + else + trymt(L, mtname); return 1; } @@ -267,6 +299,30 @@ static int arith_unm (lua_State *L) { return arith(L, LUA_OPUNM, "__unm"); } +static int bitwise_band (lua_State *L) { + return bitwise(L, LUA_OPBAND, "__band"); +} + +static int bitwise_bor (lua_State *L) { + return bitwise(L, LUA_OPBOR, "__bor"); +} + +static int bitwise_bxor (lua_State *L) { + return bitwise(L, LUA_OPBXOR, "__bxor"); +} + +static int bitwise_shl (lua_State *L) { + return bitwise(L, LUA_OPSHL, "__shl"); +} + +static int bitwise_shr (lua_State *L) { + return bitwise(L, LUA_OPSHR, "__shr"); +} + +static int bitwise_bnot (lua_State *L) { + return bitwise(L, LUA_OPBNOT, "__bnot"); +} + static const luaL_Reg stringmetamethods[] = { {"__add", arith_add}, @@ -277,6 +333,12 @@ static const luaL_Reg stringmetamethods[] = { {"__div", arith_div}, {"__idiv", arith_idiv}, {"__unm", arith_unm}, + {"__band", bitwise_band}, + {"__bor", bitwise_bor}, + {"__bxor", bitwise_bxor}, + {"__shl", bitwise_shl}, + {"__shr", bitwise_shr}, + {"__bnot", bitwise_bnot}, {"__index", NULL}, /* placeholder */ {NULL, NULL} }; diff --git a/ltable.c b/ltable.c index 37fc3d0bb3..33c6852a03 100644 --- a/ltable.c +++ b/ltable.c @@ -1,5 +1,5 @@ /* -** $Id: ltable.c,v 2.124 2017/06/12 14:21:44 roberto Exp roberto $ +** $Id: ltable.c,v 2.125 2017/06/29 15:06:44 roberto Exp roberto $ ** Lua tables (hash) ** See Copyright Notice in lua.h */ @@ -495,7 +495,7 @@ TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) { if (ttisnil(key)) luaG_runerror(L, "table index is nil"); else if (ttisfloat(key)) { lua_Integer k; - if (luaV_tointeger(key, &k, 0)) { /* does index fit in an integer? */ + if (luaV_flttointeger(key, &k, 0)) { /* does index fit in an integer? */ setivalue(&aux, k); key = &aux; /* insert it as an integer */ } @@ -604,7 +604,7 @@ const TValue *luaH_get (Table *t, const TValue *key) { case LUA_TNIL: return luaO_nilobject; case LUA_TNUMFLT: { lua_Integer k; - if (luaV_tointeger(key, &k, 0)) /* index is int? */ + if (luaV_flttointeger(key, &k, 0)) /* index is an integral? */ return luaH_getint(t, k); /* use specialized version */ /* else... */ } /* FALLTHROUGH */ diff --git a/ltm.c b/ltm.c index 91f622e49d..4c03d2e410 100644 --- a/ltm.c +++ b/ltm.c @@ -1,5 +1,5 @@ /* -** $Id: ltm.c,v 2.45 2017/10/04 15:49:05 roberto Exp $ +** $Id: ltm.c,v 2.47 2017/11/07 13:25:26 roberto Exp roberto $ ** Tag methods ** See Copyright Notice in lua.h */ @@ -153,8 +153,7 @@ void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2, /* call never returns, but to avoid warnings: *//* FALLTHROUGH */ case TM_BAND: case TM_BOR: case TM_BXOR: case TM_SHL: case TM_SHR: case TM_BNOT: { - lua_Number dummy; - if (tonumber(p1, &dummy) && tonumber(p2, &dummy)) + if (ttisnumber(p1) && ttisnumber(p2)) luaG_tointerror(L, p1, p2); else luaG_opinterror(L, p1, p2, "perform bitwise operation on"); diff --git a/lvm.c b/lvm.c index 009dfc5786..24208cddd0 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.306 2017/11/07 13:25:26 roberto Exp roberto $ +** $Id: lvm.c,v 2.307 2017/11/07 17:20:42 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -74,7 +74,7 @@ int luaV_tonumber_ (const TValue *obj, lua_Number *n) { *n = cast_num(ivalue(obj)); return 1; } - else if (cvt2num(obj) && /* string convertible to number? */ + else if (cvt2num(obj) && /* string coercible to number? */ luaO_str2num(svalue(obj), &v) == vslen(obj) + 1) { *n = nvalue(&v); /* convert result of 'luaO_str2num' to a float */ return 1; @@ -85,15 +85,15 @@ int luaV_tonumber_ (const TValue *obj, lua_Number *n) { /* -** try to convert a value to an integer, rounding according to 'mode': +** try to convert a float to an integer, rounding according to 'mode': ** mode == 0: accepts only integral values ** mode == 1: takes the floor of the number ** mode == 2: takes the ceil of the number */ -int luaV_tointeger (const TValue *obj, lua_Integer *p, int mode) { - TValue v; - again: - if (ttisfloat(obj)) { +int luaV_flttointeger (const TValue *obj, lua_Integer *p, int mode) { + if (!ttisfloat(obj)) + return 0; + else { lua_Number n = fltvalue(obj); lua_Number f = l_floor(n); if (n != f) { /* not an integral value? */ @@ -103,16 +103,23 @@ int luaV_tointeger (const TValue *obj, lua_Integer *p, int mode) { } return lua_numbertointeger(f, p); } - else if (ttisinteger(obj)) { +} + + +/* +** try to convert a value to an integer. ("Fast track" is handled +** by macro 'tointeger'.) +*/ +int luaV_tointeger (const TValue *obj, lua_Integer *p, int mode) { + TValue v; + if (cvt2num(obj) && luaO_str2num(svalue(obj), &v) == vslen(obj) + 1) + obj = &v; /* change string to its corresponding number */ + if (ttisinteger(obj)) { *p = ivalue(obj); return 1; } - else if (cvt2num(obj) && - luaO_str2num(svalue(obj), &v) == vslen(obj) + 1) { - obj = &v; - goto again; /* convert result from 'luaO_str2num' to an integer */ - } - return 0; /* conversion failed */ + else + return luaV_flttointeger(obj, p, mode); } @@ -120,9 +127,9 @@ int luaV_tointeger (const TValue *obj, lua_Integer *p, int mode) { ** Try to convert a 'for' limit to an integer, preserving the semantics ** of the loop. (The following explanation assumes a non-negative step; ** it is valid for negative steps mutatis mutandis.) -** If the limit can be converted to an integer, rounding down, that is -** it. -** Otherwise, check whether the limit can be converted to a number. If +** If the limit is an integer or can be converted to an integer, +** rounding down, that is it. +** Otherwise, check whether the limit can be converted to a float. If ** the number is too large, it is OK to set the limit as LUA_MAXINTEGER, ** which means no limit. If the number is too negative, the loop ** should not run, because any initial integer value is larger than the @@ -133,7 +140,10 @@ int luaV_tointeger (const TValue *obj, lua_Integer *p, int mode) { static int forlimit (const TValue *obj, lua_Integer *p, lua_Integer step, int *stopnow) { *stopnow = 0; /* usually, let loops run */ - if (!luaV_tointeger(obj, p, (step < 0 ? 2 : 1))) { /* not fit in integer? */ + if (ttisinteger(obj)) + *p = ivalue(obj); + else if (!luaV_tointeger(obj, p, (step < 0 ? 2 : 1))) { + /* not coercible to in integer */ lua_Number n; /* try to convert to float */ if (!tonumber(obj, &n)) /* cannot convert to float? */ return 0; /* not a number */ @@ -411,7 +421,7 @@ int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2) { return 0; /* only numbers can be equal with different variants */ else { /* two numbers with different variants */ lua_Integer i1, i2; /* compare them as integers */ - return (tointeger(t1, &i1) && tointeger(t2, &i2) && i1 == i2); + return (tointegerns(t1, &i1) && tointegerns(t2, &i2) && i1 == i2); } } /* values have same type and same variant */ @@ -1144,7 +1154,7 @@ void luaV_execute (lua_State *L) { TValue *rb = vRB(i); TValue *rc = vRC(i); lua_Integer ib; lua_Integer ic; - if (tointeger(rb, &ib) && tointeger(rc, &ic)) { + if (tointegerns(rb, &ib) && tointegerns(rc, &ic)) { setivalue(s2v(ra), intop(&, ib, ic)); } else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_BAND)); } @@ -1154,7 +1164,7 @@ void luaV_execute (lua_State *L) { TValue *rb = vRB(i); TValue *rc = vRC(i); lua_Integer ib; lua_Integer ic; - if (tointeger(rb, &ib) && tointeger(rc, &ic)) { + if (tointegerns(rb, &ib) && tointegerns(rc, &ic)) { setivalue(s2v(ra), intop(|, ib, ic)); } else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_BOR)); } @@ -1164,7 +1174,7 @@ void luaV_execute (lua_State *L) { TValue *rb = vRB(i); TValue *rc = vRC(i); lua_Integer ib; lua_Integer ic; - if (tointeger(rb, &ib) && tointeger(rc, &ic)) { + if (tointegerns(rb, &ib) && tointegerns(rc, &ic)) { setivalue(s2v(ra), intop(^, ib, ic)); } else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_BXOR)); } @@ -1174,7 +1184,7 @@ void luaV_execute (lua_State *L) { TValue *rb = vRB(i); TValue *rc = vRC(i); lua_Integer ib; lua_Integer ic; - if (tointeger(rb, &ib) && tointeger(rc, &ic)) { + if (tointegerns(rb, &ib) && tointegerns(rc, &ic)) { setivalue(s2v(ra), luaV_shiftl(ib, ic)); } else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_SHL)); } @@ -1184,7 +1194,7 @@ void luaV_execute (lua_State *L) { TValue *rb = vRB(i); TValue *rc = vRC(i); lua_Integer ib; lua_Integer ic; - if (tointeger(rb, &ib) && tointeger(rc, &ic)) { + if (tointegerns(rb, &ib) && tointegerns(rc, &ic)) { setivalue(s2v(ra), luaV_shiftl(ib, -ic)); } else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_SHR)); } @@ -1248,7 +1258,7 @@ void luaV_execute (lua_State *L) { vmcase(OP_BNOT) { TValue *rb = vRB(i); lua_Integer ib; - if (tointeger(rb, &ib)) { + if (tointegerns(rb, &ib)) { setivalue(s2v(ra), intop(^, ~l_castS2U(0), ib)); } else { diff --git a/lvm.h b/lvm.h index 6bb5818d73..c2e2557420 100644 --- a/lvm.h +++ b/lvm.h @@ -1,5 +1,5 @@ /* -** $Id: lvm.h,v 2.45 2017/06/29 15:06:44 roberto Exp roberto $ +** $Id: lvm.h,v 2.46 2017/07/07 16:34:32 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -53,6 +53,12 @@ (ttisinteger(o) ? (*(i) = ivalue(o), 1) : luaV_tointeger(o,i,LUA_FLOORN2I)) +/* convert an object to an integer (without string coercion) */ +#define tointegerns(o,i) \ + (ttisinteger(o) ? (*(i) = ivalue(o), 1) \ + : luaV_flttointeger(o,i,LUA_FLOORN2I)) + + #define intop(op,v1,v2) l_castU2S(l_castS2U(v1) op l_castS2U(v2)) #define luaV_rawequalobj(t1,t2) luaV_equalobj(NULL,t1,t2) @@ -100,6 +106,7 @@ LUAI_FUNC int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r); LUAI_FUNC int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r); LUAI_FUNC int luaV_tonumber_ (const TValue *obj, lua_Number *n); LUAI_FUNC int luaV_tointeger (const TValue *obj, lua_Integer *p, int mode); +LUAI_FUNC int luaV_flttointeger (const TValue *obj, lua_Integer *p, int mode); LUAI_FUNC void luaV_finishget (lua_State *L, const TValue *t, TValue *key, StkId val, const TValue *slot); LUAI_FUNC void luaV_finishset (lua_State *L, const TValue *t, TValue *key, From 32fef6074325f7fa464c4e9371867777ad74cd29 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 8 Nov 2017 17:01:02 -0200 Subject: [PATCH 0111/1145] detail ('Protect' defined as an expression) --- lvm.c | 81 +++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 45 insertions(+), 36 deletions(-) diff --git a/lvm.c b/lvm.c index 24208cddd0..c91fa6382d 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.307 2017/11/07 17:20:42 roberto Exp roberto $ +** $Id: lvm.c,v 2.308 2017/11/08 14:50:23 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -780,8 +780,7 @@ void luaV_finishOp (lua_State *L) { ** Protect code that, in general, can raise errors, reallocate the ** stack, and change the hooks. */ -#define Protect(code) \ - { savepc(L); {code;}; base = ci->func + 1; updatemask(L); } +#define Protect(exp) (savepc(L), (exp), base = ci->func + 1, updatemask(L)) #define checkGC(L,c) \ @@ -881,7 +880,8 @@ void luaV_execute (lua_State *L) { if (luaV_fastget(L, upval, key, slot, luaH_getshortstr)) { setobj2s(L, ra, slot); } - else Protect(luaV_finishget(L, upval, rc, ra, slot)); + else + Protect(luaV_finishget(L, upval, rc, ra, slot)); vmbreak; } vmcase(OP_GETTABLE) { @@ -920,7 +920,8 @@ void luaV_execute (lua_State *L) { if (luaV_fastget(L, rb, key, slot, luaH_getshortstr)) { setobj2s(L, ra, slot); } - else Protect(luaV_finishget(L, rb, rc, ra, slot)); + else + Protect(luaV_finishget(L, rb, rc, ra, slot)); vmbreak; } vmcase(OP_SETTABUP) { @@ -997,7 +998,8 @@ void luaV_execute (lua_State *L) { if (luaV_fastget(L, rb, key, slot, luaH_getstr)) { setobj2s(L, ra, slot); } - else Protect(luaV_finishget(L, rb, rc, ra, slot)); + else + Protect(luaV_finishget(L, rb, rc, ra, slot)); vmbreak; } vmcase(OP_ADDI) { @@ -1109,7 +1111,8 @@ void luaV_execute (lua_State *L) { else if (tonumberns(rb, nb) && tonumberns(rc, nc)) { setfltvalue(s2v(ra), luai_numadd(L, nb, nc)); } - else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_ADD)); } + else + Protect(luaT_trybinTM(L, rb, rc, ra, TM_ADD)); vmbreak; } vmcase(OP_SUB) { @@ -1123,7 +1126,8 @@ void luaV_execute (lua_State *L) { else if (tonumberns(rb, nb) && tonumberns(rc, nc)) { setfltvalue(s2v(ra), luai_numsub(L, nb, nc)); } - else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_SUB)); } + else + Protect(luaT_trybinTM(L, rb, rc, ra, TM_SUB)); vmbreak; } vmcase(OP_MUL) { @@ -1137,7 +1141,8 @@ void luaV_execute (lua_State *L) { else if (tonumberns(rb, nb) && tonumberns(rc, nc)) { setfltvalue(s2v(ra), luai_nummul(L, nb, nc)); } - else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_MUL)); } + else + Protect(luaT_trybinTM(L, rb, rc, ra, TM_MUL)); vmbreak; } vmcase(OP_DIV) { /* float division (always with floats) */ @@ -1147,7 +1152,8 @@ void luaV_execute (lua_State *L) { if (tonumberns(rb, nb) && tonumberns(rc, nc)) { setfltvalue(s2v(ra), luai_numdiv(L, nb, nc)); } - else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_DIV)); } + else + Protect(luaT_trybinTM(L, rb, rc, ra, TM_DIV)); vmbreak; } vmcase(OP_BAND) { @@ -1157,7 +1163,8 @@ void luaV_execute (lua_State *L) { if (tointegerns(rb, &ib) && tointegerns(rc, &ic)) { setivalue(s2v(ra), intop(&, ib, ic)); } - else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_BAND)); } + else + Protect(luaT_trybinTM(L, rb, rc, ra, TM_BAND)); vmbreak; } vmcase(OP_BOR) { @@ -1167,7 +1174,8 @@ void luaV_execute (lua_State *L) { if (tointegerns(rb, &ib) && tointegerns(rc, &ic)) { setivalue(s2v(ra), intop(|, ib, ic)); } - else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_BOR)); } + else + Protect(luaT_trybinTM(L, rb, rc, ra, TM_BOR)); vmbreak; } vmcase(OP_BXOR) { @@ -1177,7 +1185,8 @@ void luaV_execute (lua_State *L) { if (tointegerns(rb, &ib) && tointegerns(rc, &ic)) { setivalue(s2v(ra), intop(^, ib, ic)); } - else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_BXOR)); } + else + Protect(luaT_trybinTM(L, rb, rc, ra, TM_BXOR)); vmbreak; } vmcase(OP_SHL) { @@ -1187,7 +1196,8 @@ void luaV_execute (lua_State *L) { if (tointegerns(rb, &ib) && tointegerns(rc, &ic)) { setivalue(s2v(ra), luaV_shiftl(ib, ic)); } - else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_SHL)); } + else + Protect(luaT_trybinTM(L, rb, rc, ra, TM_SHL)); vmbreak; } vmcase(OP_SHR) { @@ -1197,7 +1207,8 @@ void luaV_execute (lua_State *L) { if (tointegerns(rb, &ib) && tointegerns(rc, &ic)) { setivalue(s2v(ra), luaV_shiftl(ib, -ic)); } - else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_SHR)); } + else + Protect(luaT_trybinTM(L, rb, rc, ra, TM_SHR)); vmbreak; } vmcase(OP_MOD) { @@ -1213,7 +1224,8 @@ void luaV_execute (lua_State *L) { luai_nummod(L, nb, nc, m); setfltvalue(s2v(ra), m); } - else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_MOD)); } + else + Protect(luaT_trybinTM(L, rb, rc, ra, TM_MOD)); vmbreak; } vmcase(OP_IDIV) { /* floor division */ @@ -1227,7 +1239,8 @@ void luaV_execute (lua_State *L) { else if (tonumberns(rb, nb) && tonumberns(rc, nc)) { setfltvalue(s2v(ra), luai_numidiv(L, nb, nc)); } - else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_IDIV)); } + else + Protect(luaT_trybinTM(L, rb, rc, ra, TM_IDIV)); vmbreak; } vmcase(OP_POW) { @@ -1237,7 +1250,8 @@ void luaV_execute (lua_State *L) { if (tonumberns(rb, nb) && tonumberns(rc, nc)) { setfltvalue(s2v(ra), luai_numpow(L, nb, nc)); } - else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_POW)); } + else + Protect(luaT_trybinTM(L, rb, rc, ra, TM_POW)); vmbreak; } vmcase(OP_UNM) { @@ -1250,9 +1264,8 @@ void luaV_execute (lua_State *L) { else if (tonumberns(rb, nb)) { setfltvalue(s2v(ra), luai_numunm(L, nb)); } - else { + else Protect(luaT_trybinTM(L, rb, rb, ra, TM_UNM)); - } vmbreak; } vmcase(OP_BNOT) { @@ -1261,9 +1274,8 @@ void luaV_execute (lua_State *L) { if (tointegerns(rb, &ib)) { setivalue(s2v(ra), intop(^, ~l_castS2U(0), ib)); } - else { + else Protect(luaT_trybinTM(L, rb, rb, ra, TM_BNOT)); - } vmbreak; } vmcase(OP_NOT) { @@ -1300,12 +1312,12 @@ void luaV_execute (lua_State *L) { vmcase(OP_EQ) { TValue *rb = vRB(i); TValue *rc = vRC(i); - Protect( - if (luaV_equalobj(L, rb, rc) != GETARG_A(i)) - pc++; - else - donextjump(ci); - ) + int res; + Protect(res = luaV_equalobj(L, rb, rc)); + if (res != GETARG_A(i)) + pc++; + else + donextjump(ci); vmbreak; } vmcase(OP_LT) { @@ -1314,9 +1326,8 @@ void luaV_execute (lua_State *L) { int res; if (ttisinteger(rb) && ttisinteger(rc)) res = (ivalue(rb) < ivalue(rc)); - else Protect( - res = luaV_lessthan(L, rb, rc); - ) + else + Protect(res = luaV_lessthan(L, rb, rc)); if (res != GETARG_A(i)) pc++; else @@ -1329,9 +1340,8 @@ void luaV_execute (lua_State *L) { int res; if (ttisinteger(rb) && ttisinteger(rc)) res = (ivalue(rb) <= ivalue(rc)); - else Protect( - res = luaV_lessequal(L, rb, rc); - ) + else + Protect(res = luaV_lessequal(L, rb, rc)); if (res != GETARG_A(i)) pc++; else @@ -1379,9 +1389,8 @@ void luaV_execute (lua_State *L) { if (b != 0) L->top = ra+b; /* else previous instruction set top */ lua_assert(GETARG_C(i) - 1 == LUA_MULTRET); savepc(L); - if (luaD_precall(L, ra, LUA_MULTRET)) { /* C function? */ + if (luaD_precall(L, ra, LUA_MULTRET)) /* C function? */ Protect((void)0); /* update 'base' */ - } else { /* tail call: put called frame (n) in place of caller one (o) */ CallInfo *nci = L->ci; /* called frame (new) */ From 7c0175bc833f8cf08a5ea7d27c80ef3e2071968b Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 9 Nov 2017 11:31:29 -0200 Subject: [PATCH 0112/1145] removed unused variable 'islocked' --- ltests.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/ltests.c b/ltests.c index 5f7b406469..5cc2b80ace 100644 --- a/ltests.c +++ b/ltests.c @@ -1,5 +1,5 @@ /* -** $Id: ltests.c,v 2.230 2017/11/07 13:25:26 roberto Exp roberto $ +** $Id: ltests.c,v 2.231 2017/11/07 17:20:42 roberto Exp roberto $ ** Internal Module for Debugging of the Lua Implementation ** See Copyright Notice in lua.h */ @@ -43,9 +43,6 @@ void *l_Trick = 0; -int islocked = 0; - - #define obj_at(L,k) s2v(L->ci->func + (k)) From fb9be62f793240234ee3cedd29678b5103e3824d Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 13 Nov 2017 10:19:35 -0200 Subject: [PATCH 0113/1145] includes 'stdio.h' to allow prints when testing --- ltests.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ltests.h b/ltests.h index 82ccc97c21..e3d1ca1e0f 100644 --- a/ltests.h +++ b/ltests.h @@ -1,5 +1,5 @@ /* -** $Id: ltests.h,v 2.50 2016/07/19 17:13:00 roberto Exp roberto $ +** $Id: ltests.h,v 2.51 2017/06/27 11:35:31 roberto Exp roberto $ ** Internal Header for Debugging of the Lua Implementation ** See Copyright Notice in lua.h */ @@ -8,6 +8,7 @@ #define ltests_h +#include #include /* test Lua with no compatibility code */ From 62f3b7c472c6bb293b3622ebdcd1747dc0e19d61 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 13 Nov 2017 10:20:51 -0200 Subject: [PATCH 0114/1145] detail --- ldebug.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/ldebug.c b/ldebug.c index 037809a2f8..89b36f079f 100644 --- a/ldebug.c +++ b/ldebug.c @@ -1,5 +1,5 @@ /* -** $Id: ldebug.c,v 2.141 2017/11/07 17:20:42 roberto Exp roberto $ +** $Id: ldebug.c,v 2.142 2017/11/08 14:50:23 roberto Exp roberto $ ** Debug Interface ** See Copyright Notice in lua.h */ @@ -754,15 +754,16 @@ void luaG_traceexec (lua_State *L) { luaD_hook(L, LUA_HOOKCOUNT, -1); /* call count hook */ if (mask & LUA_MASKLINE) { Proto *p = ci_func(ci)->p; - int npc = pcRel(ci->u.l.savedpc, p); - if (npc == 0 || /* call linehook when enter a new function, */ - ci->u.l.savedpc <= L->oldpc || /* when jump back (loop), or when */ - changedline(p, pcRel(L->oldpc, p), npc)) { /* enter new line */ - int newline = luaG_getfuncline(p, npc); /* new line */ + const Instruction *npc = ci->u.l.savedpc; + int npci = pcRel(npc, p); + if (npci == 0 || /* call linehook when enter a new function, */ + npc <= L->oldpc || /* when jump back (loop), or when */ + changedline(p, pcRel(L->oldpc, p), npci)) { /* enter new line */ + int newline = luaG_getfuncline(p, npci); /* new line */ luaD_hook(L, LUA_HOOKLINE, newline); /* call line hook */ } + L->oldpc = npc; } - L->oldpc = ci->u.l.savedpc; if (L->status == LUA_YIELD) { /* did hook yield? */ if (counthook) L->hookcount = 1; /* undo decrement to zero */ From 7d4828cc9fdc982ec713922777e77240892474e8 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 13 Nov 2017 10:26:30 -0200 Subject: [PATCH 0115/1145] avoid accessing wrong union field --- ldo.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/ldo.c b/ldo.c index efdcbc1b9e..86e77f9703 100644 --- a/ldo.c +++ b/ldo.c @@ -1,5 +1,5 @@ /* -** $Id: ldo.c,v 2.165 2017/11/02 11:28:56 roberto Exp $ +** $Id: ldo.c,v 2.170 2017/11/07 13:25:26 roberto Exp roberto $ ** Stack and Call structure of Lua ** See Copyright Notice in lua.h */ @@ -365,7 +365,8 @@ int luaD_poscall (lua_State *L, CallInfo *ci, StkId firstResult, int nres) { luaD_hook(L, LUA_HOOKRET, -1); firstResult = restorestack(L, fr); } - L->oldpc = ci->previous->u.l.savedpc; /* 'oldpc' for caller function */ + if (isLua(ci->previous)) + L->oldpc = ci->previous->u.l.savedpc; } res = ci->func; /* res == final position of 1st result */ L->ci = ci->previous; /* back to caller */ From 5440b42f434ecb6fe7a8e0d19fb8e8baf82e90b7 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 13 Nov 2017 13:36:52 -0200 Subject: [PATCH 0116/1145] using 'trap' to stop 'luaV_execute' when necessary (tracing and to update its copy of 'base' when the stack is reallocated) --- ldebug.c | 23 +++++++++++++++++++++-- ldo.c | 15 ++++++++++++--- lstate.c | 3 ++- lstate.h | 3 ++- lvm.c | 47 ++++++++++++++++++++++++++++++----------------- 5 files changed, 67 insertions(+), 24 deletions(-) diff --git a/ldebug.c b/ldebug.c index 89b36f079f..7323f23a8e 100644 --- a/ldebug.c +++ b/ldebug.c @@ -1,5 +1,5 @@ /* -** $Id: ldebug.c,v 2.142 2017/11/08 14:50:23 roberto Exp roberto $ +** $Id: ldebug.c,v 2.143 2017/11/13 12:20:51 roberto Exp roberto $ ** Debug Interface ** See Copyright Notice in lua.h */ @@ -107,7 +107,24 @@ static int currentline (CallInfo *ci) { /* -** This function can be called asynchronously (e.g. during a signal). +** This function can be called asynchronously (e.g. during a signal), +** under "reasonable" assumptions. A new 'ci' is completely linked +** in the list before it becomes part of the "active" list, and +** we assume that pointers are atomic (see comment in next function). +** (If we traverse one more item, there is no problem. If we traverse +** one less item, the worst that can happen is that the signal will +** not interrupt the script.) +*/ +static void settraps (CallInfo *ci) { + for (; ci != NULL; ci = ci->previous) + if (isLua(ci)) + ci->u.l.trap = 1; +} + + +/* +** This function can be called asynchronously (e.g. during a signal), +** under "reasonable" assumptions. ** Fields 'oldpc', 'basehookcount', and 'hookcount' (set by ** 'resethookcount') are for debug only, and it is no problem if they ** get arbitrary values (causes at most one wrong hook call). 'hookmask' @@ -126,6 +143,8 @@ LUA_API void lua_sethook (lua_State *L, lua_Hook func, int mask, int count) { L->basehookcount = count; resethookcount(L); L->hookmask = cast_byte(mask); + if (mask & (LUA_MASKLINE | LUA_MASKCOUNT)) + settraps(L->ci); /* to trace inside 'luaV_execute' */ } diff --git a/ldo.c b/ldo.c index 86e77f9703..970b890b0d 100644 --- a/ldo.c +++ b/ldo.c @@ -1,5 +1,5 @@ /* -** $Id: ldo.c,v 2.170 2017/11/07 13:25:26 roberto Exp roberto $ +** $Id: ldo.c,v 2.171 2017/11/13 12:26:30 roberto Exp roberto $ ** Stack and Call structure of Lua ** See Copyright Notice in lua.h */ @@ -159,12 +159,16 @@ int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) { static void correctstack (lua_State *L, StkId oldstack) { CallInfo *ci; UpVal *up; + if (L->stack == oldstack) + return; /* stack address did not change */ L->top = (L->top - oldstack) + L->stack; for (up = L->openupval; up != NULL; up = up->u.open.next) up->v = s2v((uplevel(up) - oldstack) + L->stack); for (ci = L->ci; ci != NULL; ci = ci->previous) { ci->top = (ci->top - oldstack) + L->stack; ci->func = (ci->func - oldstack) + L->stack; + if (isLua(ci)) + ci->u.l.trap = 1; /* signal to update 'trap' in 'luaV_execute' */ } } @@ -277,13 +281,18 @@ void luaD_hook (lua_State *L, int event, int line) { static void callhook (lua_State *L, CallInfo *ci) { - int hook = LUA_HOOKCALL; + int hook; + ci->u.l.trap = 1; + if (!(L->hookmask & LUA_MASKCALL)) + return; /* some other hook */ ci->u.l.savedpc++; /* hooks assume 'pc' is already incremented */ if (isLua(ci->previous) && GET_OPCODE(*(ci->previous->u.l.savedpc - 1)) == OP_TAILCALL) { ci->callstatus |= CIST_TAIL; hook = LUA_HOOKTAILCALL; } + else + hook = LUA_HOOKCALL; luaD_hook(L, hook, -1); ci->u.l.savedpc--; /* correct 'pc' */ } @@ -430,7 +439,7 @@ int luaD_precall (lua_State *L, StkId func, int nresults) { lua_assert(ci->top <= L->stack_last); ci->u.l.savedpc = p->code; /* starting point */ ci->callstatus = CIST_LUA; - if (L->hookmask & LUA_MASKCALL) + if (L->hookmask) callhook(L, ci); return 0; } diff --git a/lstate.c b/lstate.c index 7f67527b75..54e390b504 100644 --- a/lstate.c +++ b/lstate.c @@ -1,5 +1,5 @@ /* -** $Id: lstate.c,v 2.143 2017/10/31 17:54:35 roberto Exp $ +** $Id: lstate.c,v 2.146 2017/11/07 13:25:26 roberto Exp roberto $ ** Global State ** See Copyright Notice in lua.h */ @@ -103,6 +103,7 @@ CallInfo *luaE_extendCI (lua_State *L) { L->ci->next = ci; ci->previous = L->ci; ci->next = NULL; + ci->u.l.trap = 0; L->nci++; return ci; } diff --git a/lstate.h b/lstate.h index 4798474c6a..02715dfbd9 100644 --- a/lstate.h +++ b/lstate.h @@ -1,5 +1,5 @@ /* -** $Id: lstate.h,v 2.146 2017/11/02 11:28:56 roberto Exp $ +** $Id: lstate.h,v 2.150 2017/11/07 13:25:26 roberto Exp roberto $ ** Global State ** See Copyright Notice in lua.h */ @@ -91,6 +91,7 @@ typedef struct CallInfo { union { struct { /* only for Lua functions */ const Instruction *savedpc; + l_signalT trap; } l; struct { /* only for C functions */ lua_KFunction k; /* continuation in case of yields */ diff --git a/lvm.c b/lvm.c index c91fa6382d..a8285ac6e5 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.308 2017/11/08 14:50:23 roberto Exp roberto $ +** $Id: lvm.c,v 2.309 2017/11/08 19:01:02 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -756,14 +756,16 @@ void luaV_finishOp (lua_State *L) { -#define updatemask(L) (mask = L->hookmask & (LUA_MASKLINE | LUA_MASKCOUNT)) +#define updatetrap(ci) (trap = ci->u.l.trap) + +#define updatebase(ci) (base = ci->func + 1) /* -** Execute a jump instruction. The 'updatemask' allows signals to stop -** tight loops. (Without it, the local copy of 'mask' could never change.) +** Execute a jump instruction. The 'updatetrap' allows signals to stop +** tight loops. (Without it, the local copy of 'trap' could never change.) */ -#define dojump(ci,i,e) { pc += GETARG_sJ(i) + e; updatemask(L); } +#define dojump(ci,i,e) { pc += GETARG_sJ(i) + e; updatetrap(ci); } /* for test instructions, execute the jump instruction that follows it */ @@ -780,19 +782,24 @@ void luaV_finishOp (lua_State *L) { ** Protect code that, in general, can raise errors, reallocate the ** stack, and change the hooks. */ -#define Protect(exp) (savepc(L), (exp), base = ci->func + 1, updatemask(L)) +#define Protect(exp) (savepc(L), (exp), updatetrap(ci)) #define checkGC(L,c) \ { luaC_condGC(L, L->top = (c), /* limit of live values */ \ - Protect(L->top = ci->top)); /* restore top */ \ + (L->top = ci->top, updatetrap(ci))); /* restore top */ \ luai_threadyield(L); } /* fetch an instruction and prepare its execution */ #define vmfetch() { \ i = *(pc++); \ - if (mask) Protect(luaG_traceexec(L)); \ + if (trap) { \ + if (!(L->hookmask & (LUA_MASKLINE | LUA_MASKCOUNT))) \ + trap = ci->u.l.trap = 0; /* no need to stop again */ \ + else { savepc(L); luaG_traceexec(L); } \ + updatebase(ci); /* the trap may be just for that */ \ + } \ ra = RA(i); /* WARNING: any stack reallocation invalidates 'ra' */ \ } @@ -802,19 +809,19 @@ void luaV_finishOp (lua_State *L) { void luaV_execute (lua_State *L) { - CallInfo *ci = L->ci; + CallInfo *ci = L->ci; /* local copy of 'L->ci' */ LClosure *cl; TValue *k; StkId base; /* local copy of 'ci->func + 1' */ - int mask; /* local copy of 'L->hookmask & (LUA_MASKLINE | LUA_MASKCOUNT)' */ + int trap; const Instruction *pc; /* local copy of 'ci->u.l.savedpc' */ ci->callstatus |= CIST_FRESH; /* fresh invocation of 'luaV_execute" */ newframe: /* reentry point when frame changes (call/return) */ lua_assert(ci == L->ci); cl = clLvalue(s2v(ci->func)); /* local reference to function's closure */ k = cl->p->k; /* local reference to function's constant table */ - updatemask(L); - base = ci->func + 1; + updatetrap(ci); + updatebase(ci); pc = ci->u.l.savedpc; /* main loop of interpreter */ for (;;) { @@ -1294,7 +1301,10 @@ void luaV_execute (lua_State *L) { StkId rb; L->top = base + c + 1; /* mark the end of concat operands */ Protect(luaV_concat(L, c - b + 1)); - ra = RA(i); /* 'luaV_concat' may invoke TMs and move the stack */ + if (trap) { /* 'luaV_concat' may move the stack */ + updatebase(ci); + ra = RA(i); + } rb = base + b; setobjs2s(L, ra, rb); checkGC(L, (ra >= rb ? ra + 1 : rb)); @@ -1390,7 +1400,7 @@ void luaV_execute (lua_State *L) { lua_assert(GETARG_C(i) - 1 == LUA_MULTRET); savepc(L); if (luaD_precall(L, ra, LUA_MULTRET)) /* C function? */ - Protect((void)0); /* update 'base' */ + updatetrap(ci); else { /* tail call: put called frame (n) in place of caller one (o) */ CallInfo *nci = L->ci; /* called frame (new) */ @@ -1416,7 +1426,8 @@ void luaV_execute (lua_State *L) { } vmcase(OP_RETURN) { int b = GETARG_B(i); - if (cl->p->sizep > 0) luaF_close(L, base); + if (cl->p->sizep > 0) + luaF_close(L, base); savepc(L); b = luaD_poscall(L, ci, ra, (b != 0 ? b - 1 : cast_int(L->top - ra))); if (ci->callstatus & CIST_FRESH) /* local 'ci' still from callee */ @@ -1452,7 +1463,7 @@ void luaV_execute (lua_State *L) { setfltvalue(s2v(ra + 3), idx); /* ...and external index */ } } - updatemask(L); + updatetrap(ci); vmbreak; } vmcase(OP_FORPREP) { @@ -1492,8 +1503,10 @@ void luaV_execute (lua_State *L) { L->top = cb + 3; /* func. + 2 args (state and index) */ Protect(luaD_call(L, cb, GETARG_C(i))); L->top = ci->top; + if (trap) /* keep 'base' correct for next instruction */ + updatebase(ci); i = *(pc++); /* go to next instruction */ - ra = RA(i); + ra = RA(i); /* get its 'ra' */ lua_assert(GET_OPCODE(i) == OP_TFORLOOP); goto l_tforloop; } From 4c0e36a46e11be2f101203ed0db66ea750bf4333 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 16 Nov 2017 10:59:14 -0200 Subject: [PATCH 0117/1145] new instruction 'OP_EQK' (for equality with constants) --- lcode.c | 55 +++++++++++++++++++++++++++++++++++++++++++----------- lopcodes.c | 4 +++- lopcodes.h | 4 +++- lvm.c | 12 +++++++++++- 4 files changed, 61 insertions(+), 14 deletions(-) diff --git a/lcode.c b/lcode.c index c36a49ad7b..ccd25711c4 100644 --- a/lcode.c +++ b/lcode.c @@ -1,5 +1,5 @@ /* -** $Id: lcode.c,v 2.131 2017/11/07 17:20:42 roberto Exp roberto $ +** $Id: lcode.c,v 2.132 2017/11/08 14:50:23 roberto Exp roberto $ ** Code generator for Lua ** See Copyright Notice in lua.h */ @@ -1251,10 +1251,10 @@ static void codecommutative (FuncState *fs, OpCode op, /* -** Emit code for comparisons. +** Emit code for order comparisons. ** 'e1' was already put in register by 'luaK_infix'. */ -static void codecomp (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) { +static void codeorder (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) { int rk1 = check_exp(e1->k == VNONRELOC, e1->u.info); int rk2 = luaK_exp2anyreg(fs, e2); freeexps(fs, e1, e2); @@ -1279,6 +1279,30 @@ static void codecomp (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) { } +/* +** Emit code for equality comparisons ('==', '~='). +** 'e1' was already put as RK by 'luaK_infix'. +*/ +static void codeeq (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) { + int r1, rk2; + OpCode op = OP_EQK; /* will try first to use a constant */ + if (e1->k == VK) { /* 1st expression is constant? */ + rk2 = e1->u.info; /* constant index */ + r1 = luaK_exp2anyreg(fs, e2); /* 2nd expression must be in register */ + } + else { + lua_assert(e1->k == VNONRELOC); /* 1st expression is in a register */ + r1 = e1->u.info; + if (!luaK_exp2RK(fs, e2)) /* 2nd expression is not constant? */ + op = OP_EQ; /* will compare two registers */ + rk2 = e2->u.info; /* constant/register index */ + } + freeexps(fs, e1, e2); + e1->u.info = condjump(fs, op, (opr == OPR_EQ), r1, rk2); + e1->k = VJMP; +} + + /* ** Aplly prefix operation 'op' to expression 'e'. */ @@ -1321,12 +1345,17 @@ void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) { case OPR_MOD: case OPR_POW: case OPR_BAND: case OPR_BOR: case OPR_BXOR: case OPR_SHL: case OPR_SHR: { - if (tonumeral(v, NULL)) - break; /* keep numeral, which may be folded with 2nd operand */ - /* else *//* FALLTHROUGH */ + if (!tonumeral(v, NULL)) + luaK_exp2anyreg(fs, v); + /* else keep numeral, which may be folded with 2nd operand */ + break; + } + case OPR_EQ: case OPR_NE: { + luaK_exp2RK(fs, v); + break; } - case OPR_EQ: case OPR_LT: case OPR_LE: - case OPR_NE: case OPR_GT: case OPR_GE: { + case OPR_LT: case OPR_LE: + case OPR_GT: case OPR_GE: { luaK_exp2anyreg(fs, v); break; } @@ -1390,9 +1419,13 @@ void luaK_posfix (FuncState *fs, BinOpr op, codebinexpval(fs, cast(OpCode, op + OP_ADD), e1, e2, line); break; } - case OPR_EQ: case OPR_LT: case OPR_LE: - case OPR_NE: case OPR_GT: case OPR_GE: { - codecomp(fs, op, e1, e2); + case OPR_EQ: case OPR_NE: { + codeeq(fs, op, e1, e2); + break; + } + case OPR_LT: case OPR_LE: + case OPR_GT: case OPR_GE: { + codeorder(fs, op, e1, e2); break; } default: lua_assert(0); diff --git a/lopcodes.c b/lopcodes.c index 6ea54d094f..8740875209 100644 --- a/lopcodes.c +++ b/lopcodes.c @@ -1,5 +1,5 @@ /* -** $Id: lopcodes.c,v 1.66 2017/10/04 15:49:24 roberto Exp roberto $ +** $Id: lopcodes.c,v 1.67 2017/11/07 17:20:42 roberto Exp roberto $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ @@ -66,6 +66,7 @@ LUAI_DDEF const char *const luaP_opnames[NUM_OPCODES+1] = { "EQ", "LT", "LE", + "EQK", "TEST", "TESTSET", "CALL", @@ -133,6 +134,7 @@ LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = { ,opmode(1, 0, iABC) /* OP_EQ */ ,opmode(1, 0, iABC) /* OP_LT */ ,opmode(1, 0, iABC) /* OP_LE */ + ,opmode(1, 0, iABC) /* OP_EQK */ ,opmode(1, 0, iABC) /* OP_TEST */ ,opmode(1, 1, iABC) /* OP_TESTSET */ ,opmode(0, 1, iABC) /* OP_CALL */ diff --git a/lopcodes.h b/lopcodes.h index bc8d722f95..a805ba636a 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -1,5 +1,5 @@ /* -** $Id: lopcodes.h,v 1.166 2017/10/04 21:56:32 roberto Exp roberto $ +** $Id: lopcodes.h,v 1.167 2017/11/07 17:20:42 roberto Exp roberto $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ @@ -240,6 +240,8 @@ OP_EQ,/* A B C if ((R(B) == R(C)) ~= A) then pc++ */ OP_LT,/* A B C if ((R(B) < R(C)) ~= A) then pc++ */ OP_LE,/* A B C if ((R(B) <= R(C)) ~= A) then pc++ */ +OP_EQK,/* A B C if ((R(B) == K(C)) ~= A) then pc++ */ + OP_TEST,/* A C if not (R(A) <=> C) then pc++ */ OP_TESTSET,/* A B C if (R(B) <=> C) then R(A) := R(B) else pc++ */ diff --git a/lvm.c b/lvm.c index a8285ac6e5..717335d25c 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.309 2017/11/08 19:01:02 roberto Exp roberto $ +** $Id: lvm.c,v 2.310 2017/11/13 15:36:52 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -1358,6 +1358,16 @@ void luaV_execute (lua_State *L) { donextjump(ci); vmbreak; } + vmcase(OP_EQK) { + TValue *rb = vRB(i); + TValue *rc = KC(i); + /* basic types do not use '__eq'; we can use raw equality */ + if (luaV_equalobj(NULL, rb, rc) != GETARG_A(i)) + pc++; + else + donextjump(ci); + vmbreak; + } vmcase(OP_TEST) { if (GETARG_C(i) ? l_isfalse(s2v(ra)) : !l_isfalse(s2v(ra))) pc++; From e4e5aa85a24bba6c243aeeaedc66ec712856c6db Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 16 Nov 2017 11:19:06 -0200 Subject: [PATCH 0118/1145] detail ('signal' -> 'sign' in comments) --- lbaselib.c | 4 ++-- lbitlib.c | 4 ++-- liolib.c | 6 +++--- lobject.c | 8 ++++---- lstrlib.c | 4 ++-- 5 files changed, 13 insertions(+), 13 deletions(-) diff --git a/lbaselib.c b/lbaselib.c index 87b0ce834f..00452f2d3a 100644 --- a/lbaselib.c +++ b/lbaselib.c @@ -1,5 +1,5 @@ /* -** $Id: lbaselib.c,v 1.316 2017/05/26 19:14:29 roberto Exp roberto $ +** $Id: lbaselib.c,v 1.317 2017/06/27 18:32:49 roberto Exp roberto $ ** Basic library ** See Copyright Notice in lua.h */ @@ -49,7 +49,7 @@ static const char *b_str2int (const char *s, int base, lua_Integer *pn) { lua_Unsigned n = 0; int neg = 0; s += strspn(s, SPACECHARS); /* skip initial spaces */ - if (*s == '-') { s++; neg = 1; } /* handle signal */ + if (*s == '-') { s++; neg = 1; } /* handle sign */ else if (*s == '+') s++; if (!isalnum((unsigned char)*s)) /* no digit? */ return NULL; diff --git a/lbitlib.c b/lbitlib.c index 02483d695b..63dbbe1d1b 100644 --- a/lbitlib.c +++ b/lbitlib.c @@ -1,5 +1,5 @@ /* -** $Id: lbitlib.c,v 1.29 2015/10/08 15:55:35 roberto Exp roberto $ +** $Id: lbitlib.c,v 1.30 2015/11/11 19:08:09 roberto Exp roberto $ ** Standard library for bitwise operations ** See Copyright Notice in lua.h */ @@ -131,7 +131,7 @@ static int b_arshift (lua_State *L) { else { /* arithmetic shift for 'negative' number */ if (i >= LUA_NBITS) r = ALLONES; else - r = trim((r >> i) | ~(trim(~(lua_Unsigned)0) >> i)); /* add signal bit */ + r = trim((r >> i) | ~(trim(~(lua_Unsigned)0) >> i)); /* add sign bit */ pushunsigned(L, r); return 1; } diff --git a/liolib.c b/liolib.c index 3fa874613d..19068f8786 100644 --- a/liolib.c +++ b/liolib.c @@ -1,5 +1,5 @@ /* -** $Id: liolib.c,v 2.151 2016/12/20 18:37:00 roberto Exp roberto $ +** $Id: liolib.c,v 2.152 2017/02/09 14:50:05 roberto Exp roberto $ ** Standard I/O (and system) library ** See Copyright Notice in lua.h */ @@ -447,7 +447,7 @@ static int read_number (lua_State *L, FILE *f) { decp[1] = '.'; /* always accept a dot */ l_lockfile(rn.f); do { rn.c = l_getc(rn.f); } while (isspace(rn.c)); /* skip spaces */ - test2(&rn, "-+"); /* optional signal */ + test2(&rn, "-+"); /* optional sign */ if (test2(&rn, "00")) { if (test2(&rn, "xX")) hex = 1; /* numeral is hexadecimal */ else count = 1; /* count initial '0' as a valid digit */ @@ -456,7 +456,7 @@ static int read_number (lua_State *L, FILE *f) { if (test2(&rn, decp)) /* decimal point? */ count += readdigits(&rn, hex); /* fractional part */ if (count > 0 && test2(&rn, (hex ? "pP" : "eE"))) { /* exponent mark? */ - test2(&rn, "-+"); /* exponent signal */ + test2(&rn, "-+"); /* exponent sign */ readdigits(&rn, 0); /* exponent digits */ } ungetc(rn.c, rn.f); /* unread look-ahead char */ diff --git a/lobject.c b/lobject.c index 32cdc4b0f7..fb2c172c04 100644 --- a/lobject.c +++ b/lobject.c @@ -1,5 +1,5 @@ /* -** $Id: lobject.c,v 2.118 2017/10/10 20:05:40 roberto Exp roberto $ +** $Id: lobject.c,v 2.119 2017/11/08 14:50:23 roberto Exp roberto $ ** Some generic functions over Lua objects ** See Copyright Notice in lua.h */ @@ -206,7 +206,7 @@ static lua_Number lua_strx2number (const char *s, char **endptr) { int hasdot = 0; /* true after seen a dot */ *endptr = cast(char *, s); /* nothing is valid yet */ while (lisspace(cast_uchar(*s))) s++; /* skip initial spaces */ - neg = isneg(&s); /* check signal */ + neg = isneg(&s); /* check sign */ if (!(*s == '0' && (*(s + 1) == 'x' || *(s + 1) == 'X'))) /* check '0x' */ return 0.0; /* invalid format (no '0x') */ for (s += 2; ; s++) { /* skip '0x' and read numeral */ @@ -230,9 +230,9 @@ static lua_Number lua_strx2number (const char *s, char **endptr) { e *= 4; /* each digit multiplies/divides value by 2^4 */ if (*s == 'p' || *s == 'P') { /* exponent part? */ int exp1 = 0; /* exponent value */ - int neg1; /* exponent signal */ + int neg1; /* exponent sign */ s++; /* skip 'p' */ - neg1 = isneg(&s); /* signal */ + neg1 = isneg(&s); /* sign */ if (!lisdigit(cast_uchar(*s))) return 0.0; /* invalid; must have at least one digit */ while (lisdigit(cast_uchar(*s))) /* read exponent */ diff --git a/lstrlib.c b/lstrlib.c index 626c9159e1..9a4cf90d49 100644 --- a/lstrlib.c +++ b/lstrlib.c @@ -1,5 +1,5 @@ /* -** $Id: lstrlib.c,v 1.257 2017/07/07 16:34:32 roberto Exp roberto $ +** $Id: lstrlib.c,v 1.258 2017/11/08 14:50:23 roberto Exp roberto $ ** Standard library for string operations and pattern-matching ** See Copyright Notice in lua.h */ @@ -994,7 +994,7 @@ static int num2straux (char *buff, int sz, lua_Number x) { lua_Number m = l_mathop(frexp)(x, &e); /* 'x' fraction and exponent */ int n = 0; /* character count */ if (m < 0) { /* is number negative? */ - buff[n++] = '-'; /* add signal */ + buff[n++] = '-'; /* add sign */ m = -m; /* make it positive */ } buff[n++] = '0'; buff[n++] = 'x'; /* add "0x" */ From c47111bd4e33b2dcb4fc2cceb7b268ab02eea452 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 16 Nov 2017 14:28:36 -0200 Subject: [PATCH 0119/1145] 'io.read' accepts multiple formats in a single string argument --- liolib.c | 58 ++++++++++++++++++++++++++++++-------------------------- 1 file changed, 31 insertions(+), 27 deletions(-) diff --git a/liolib.c b/liolib.c index 19068f8786..f3c914d7f2 100644 --- a/liolib.c +++ b/liolib.c @@ -1,5 +1,5 @@ /* -** $Id: liolib.c,v 2.152 2017/02/09 14:50:05 roberto Exp roberto $ +** $Id: liolib.c,v 2.153 2017/11/16 13:19:06 roberto Exp roberto $ ** Standard I/O (and system) library ** See Copyright Notice in lua.h */ @@ -528,40 +528,44 @@ static int read_chars (lua_State *L, FILE *f, size_t n) { static int g_read (lua_State *L, FILE *f, int first) { int nargs = lua_gettop(L) - 1; - int success; - int n; + int nresults, success; clearerr(f); if (nargs == 0) { /* no arguments? */ success = read_line(L, f, 1); - n = first+1; /* to return 1 result */ + nresults = 1; } - else { /* ensure stack space for all results and for auxlib's buffer */ - luaL_checkstack(L, nargs+LUA_MINSTACK, "too many arguments"); + else { success = 1; - for (n = first; nargs-- && success; n++) { - if (lua_type(L, n) == LUA_TNUMBER) { - size_t l = (size_t)luaL_checkinteger(L, n); + nresults = 0; + for (; nargs-- && success; first++) { + luaL_checkstack(L, LUA_MINSTACK, "too many arguments"); + if (lua_type(L, first) == LUA_TNUMBER) { + size_t l = (size_t)luaL_checkinteger(L, first); success = (l == 0) ? test_eof(L, f) : read_chars(L, f, l); + nresults++; } else { - const char *p = luaL_checkstring(L, n); + const char *p = luaL_checkstring(L, first); if (*p == '*') p++; /* skip optional '*' (for compatibility) */ - switch (*p) { - case 'n': /* number */ - success = read_number(L, f); - break; - case 'l': /* line */ - success = read_line(L, f, 1); - break; - case 'L': /* line with end-of-line */ - success = read_line(L, f, 0); - break; - case 'a': /* file */ - read_all(L, f); /* read entire file */ - success = 1; /* always success */ - break; - default: - return luaL_argerror(L, n, "invalid format"); + for (; success && *p != '\0'; p++) { + nresults++; + switch (*p) { + case 'n': /* number */ + success = read_number(L, f); + break; + case 'l': /* line */ + success = read_line(L, f, 1); + break; + case 'L': /* line with end-of-line */ + success = read_line(L, f, 0); + break; + case 'a': /* file */ + read_all(L, f); /* read entire file */ + success = 1; /* always success */ + break; + default: + return luaL_argerror(L, first, "invalid format"); + } } } } @@ -572,7 +576,7 @@ static int g_read (lua_State *L, FILE *f, int first) { lua_pop(L, 1); /* remove last result */ lua_pushnil(L); /* push nil instead */ } - return n - first; + return nresults; } From f3ca52bfa994edacfa8fbbc6f1014724b384de9a Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 20 Nov 2017 10:57:39 -0200 Subject: [PATCH 0120/1145] in order comparison opcodes, fast track for floats too --- lvm.c | 60 +++++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 42 insertions(+), 18 deletions(-) diff --git a/lvm.c b/lvm.c index 717335d25c..ab28ea72cd 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.310 2017/11/13 15:36:52 roberto Exp roberto $ +** $Id: lvm.c,v 2.311 2017/11/16 12:59:14 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -326,6 +326,7 @@ static int LEintfloat (lua_Integer i, lua_Number f) { ** Return 'l < r', for numbers. */ static int LTnum (const TValue *l, const TValue *r) { + lua_assert(ttisnumber(l) && ttisnumber(r)); if (ttisinteger(l)) { lua_Integer li = ivalue(l); if (ttisinteger(r)) @@ -349,6 +350,7 @@ static int LTnum (const TValue *l, const TValue *r) { ** Return 'l <= r', for numbers. */ static int LEnum (const TValue *l, const TValue *r) { + lua_assert(ttisnumber(l) && ttisnumber(r)); if (ttisinteger(l)) { lua_Integer li = ivalue(l); if (ttisinteger(r)) @@ -369,13 +371,12 @@ static int LEnum (const TValue *l, const TValue *r) { /* -** Main operation less than; return 'l < r'. +** return 'l < r' for non-numbers. */ -int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r) { +static int lessthanothers (lua_State *L, const TValue *l, const TValue *r) { int res; - if (ttisnumber(l) && ttisnumber(r)) /* both operands are numbers? */ - return LTnum(l, r); - else if (ttisstring(l) && ttisstring(r)) /* both are strings? */ + lua_assert(!ttisnumber(l) || !ttisnumber(r)); + if (ttisstring(l) && ttisstring(r)) /* both are strings? */ return l_strcmp(tsvalue(l), tsvalue(r)) < 0; else if ((res = luaT_callorderTM(L, l, r, TM_LT)) < 0) /* no metamethod? */ luaG_ordererror(L, l, r); /* error */ @@ -384,18 +385,27 @@ int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r) { /* -** Main operation less than or equal to; return 'l <= r'. If it needs -** a metamethod and there is no '__le', try '__lt', based on -** l <= r iff !(r < l) (assuming a total order). If the metamethod -** yields during this substitution, the continuation has to know -** about it (to negate the result of r= 0) /* try 'le' */ return res; @@ -410,6 +420,16 @@ int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r) { } +/* +** Main operation less than or equal to; return 'l <= r'. +*/ +int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r) { + if (ttisnumber(l) && ttisnumber(r)) /* both operands are numbers? */ + return LEnum(l, r); + else return lessequalothers(L, l, r); +} + + /* ** Main operation for equality of Lua values; return 't1 == t2'. ** L == NULL means raw equality (no metamethods) @@ -1336,8 +1356,10 @@ void luaV_execute (lua_State *L) { int res; if (ttisinteger(rb) && ttisinteger(rc)) res = (ivalue(rb) < ivalue(rc)); + else if (ttisnumber(rb) && ttisnumber(rc)) + res = LTnum(rb, rc); else - Protect(res = luaV_lessthan(L, rb, rc)); + Protect(res = lessthanothers(L, rb, rc)); if (res != GETARG_A(i)) pc++; else @@ -1350,8 +1372,10 @@ void luaV_execute (lua_State *L) { int res; if (ttisinteger(rb) && ttisinteger(rc)) res = (ivalue(rb) <= ivalue(rc)); + else if (ttisnumber(rb) && ttisnumber(rc)) + res = LEnum(rb, rc); else - Protect(res = luaV_lessequal(L, rb, rc)); + Protect(res = lessequalothers(L, rb, rc)); if (res != GETARG_A(i)) pc++; else From 14c3aa12b5a609f45b9be49e11ee862f9193a014 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 21 Nov 2017 12:18:03 -0200 Subject: [PATCH 0121/1145] more direct implementation for tail calls. --- ldo.c | 47 +++++++++++++++++++++++++++++++++++++---------- ldo.h | 4 +++- lvm.c | 42 +++++++++++++++++------------------------- 3 files changed, 57 insertions(+), 36 deletions(-) diff --git a/ldo.c b/ldo.c index 970b890b0d..3095412310 100644 --- a/ldo.c +++ b/ldo.c @@ -1,5 +1,5 @@ /* -** $Id: ldo.c,v 2.171 2017/11/13 12:26:30 roberto Exp roberto $ +** $Id: ldo.c,v 2.172 2017/11/13 15:36:52 roberto Exp roberto $ ** Stack and Call structure of Lua ** See Copyright Notice in lua.h */ @@ -280,14 +280,13 @@ void luaD_hook (lua_State *L, int event, int line) { } -static void callhook (lua_State *L, CallInfo *ci) { +static void callhook (lua_State *L, CallInfo *ci, int istail) { int hook; ci->u.l.trap = 1; if (!(L->hookmask & LUA_MASKCALL)) return; /* some other hook */ ci->u.l.savedpc++; /* hooks assume 'pc' is already incremented */ - if (isLua(ci->previous) && - GET_OPCODE(*(ci->previous->u.l.savedpc - 1)) == OP_TAILCALL) { + if (istail) { ci->callstatus |= CIST_TAIL; hook = LUA_HOOKTAILCALL; } @@ -303,16 +302,18 @@ static void callhook (lua_State *L, CallInfo *ci) { ** it in stack below original 'func' so that 'luaD_precall' can call ** it. Raise an error if __call metafield is not a function. */ -static void tryfuncTM (lua_State *L, StkId func) { +StkId luaD_tryfuncTM (lua_State *L, StkId func) { const TValue *tm = luaT_gettmbyobj(L, s2v(func), TM_CALL); StkId p; if (!ttisfunction(tm)) luaG_typeerror(L, s2v(func), "call"); /* Open a hole inside the stack at 'func' */ + checkstackp(L, 1, func); /* ensure space for metamethod */ for (p = L->top; p > func; p--) setobjs2s(L, p, p-1); - L->top++; /* slot ensured by caller */ - setobj2s(L, func, tm); /* tag method is the new function to be called */ + L->top++; + setobj2s(L, func, tm); /* metamethod is the new function to be called */ + return func; } @@ -388,6 +389,33 @@ int luaD_poscall (lua_State *L, CallInfo *ci, StkId firstResult, int nres) { #define next_ci(L) (L->ci = (L->ci->next ? L->ci->next : luaE_extendCI(L))) +/* +** Prepare a function for a tail call, building its call info on top +** of the current call info. 'n' is the number of arguments plus 1 +** (so that it includes the function itself). +*/ +void luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, int n) { + Proto *p = clLvalue(s2v(func))->p; + int fsize = p->maxstacksize; /* frame size */ + int i; + for (i = 0; i < n; i++) /* move down function and arguments */ + setobjs2s(L, ci->func + i, func + i); + checkstackp(L, fsize, func); + for (; i < p->numparams - p->is_vararg; i++) + setnilvalue(s2v(ci->func + i)); /* complete missing parameters */ + if (p->is_vararg) { + L->top -= (func - ci->func); /* move down top */ + luaT_adjustvarargs(L, p, n - 1); + } + L->top = ci->top = ci->func + 1 + fsize; /* top for new function */ + lua_assert(ci->top <= L->stack_last); + ci->u.l.savedpc = p->code; /* starting point */ + ci->callstatus |= CIST_TAIL; + if (L->hookmask) + callhook(L, ci, 1); +} + + /* ** Prepares a function call: checks the stack, creates a new CallInfo ** entry, fills in the relevant information, calls hook if needed. @@ -440,12 +468,11 @@ int luaD_precall (lua_State *L, StkId func, int nresults) { ci->u.l.savedpc = p->code; /* starting point */ ci->callstatus = CIST_LUA; if (L->hookmask) - callhook(L, ci); + callhook(L, ci, 0); return 0; } default: { /* not a function */ - checkstackp(L, 1, func); /* ensure space for metamethod */ - tryfuncTM(L, func); /* try to get '__call' metamethod */ + func = luaD_tryfuncTM(L, func); /* try to get '__call' metamethod */ return luaD_precall(L, func, nresults); /* now it must be a function */ } } diff --git a/ldo.h b/ldo.h index 3dc16d7160..2640c08830 100644 --- a/ldo.h +++ b/ldo.h @@ -1,5 +1,5 @@ /* -** $Id: ldo.h,v 2.31 2017/06/29 15:06:44 roberto Exp $ +** $Id: ldo.h,v 2.33 2017/11/07 13:25:26 roberto Exp roberto $ ** Stack and Call structure of Lua ** See Copyright Notice in lua.h */ @@ -48,8 +48,10 @@ LUAI_FUNC int luaD_protectedparser (lua_State *L, ZIO *z, const char *name, const char *mode); LUAI_FUNC void luaD_hook (lua_State *L, int event, int line); LUAI_FUNC int luaD_precall (lua_State *L, StkId func, int nresults); +LUAI_FUNC void luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, int n); LUAI_FUNC void luaD_call (lua_State *L, StkId func, int nResults); LUAI_FUNC void luaD_callnoyield (lua_State *L, StkId func, int nResults); +LUAI_FUNC StkId luaD_tryfuncTM (lua_State *L, StkId func); LUAI_FUNC int luaD_pcall (lua_State *L, Pfunc func, void *u, ptrdiff_t oldtop, ptrdiff_t ef); LUAI_FUNC int luaD_poscall (lua_State *L, CallInfo *ci, StkId firstResult, diff --git a/lvm.c b/lvm.c index ab28ea72cd..b673db916c 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.311 2017/11/16 12:59:14 roberto Exp roberto $ +** $Id: lvm.c,v 2.312 2017/11/20 12:57:39 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -1429,31 +1429,23 @@ void luaV_execute (lua_State *L) { vmbreak; } vmcase(OP_TAILCALL) { - int b = GETARG_B(i); - if (b != 0) L->top = ra+b; /* else previous instruction set top */ + int b = GETARG_B(i); /* number of arguments + 1 (function) */ + if (b != 0) + L->top = ra + b; + else /* previous instruction set top */ + b = L->top - ra; lua_assert(GETARG_C(i) - 1 == LUA_MULTRET); - savepc(L); - if (luaD_precall(L, ra, LUA_MULTRET)) /* C function? */ - updatetrap(ci); - else { - /* tail call: put called frame (n) in place of caller one (o) */ - CallInfo *nci = L->ci; /* called frame (new) */ - CallInfo *oci = nci->previous; /* caller frame (old) */ - StkId nfunc = nci->func; /* called function */ - StkId ofunc = oci->func; /* caller function */ - /* last stack slot filled by 'precall' */ - StkId lim = nfunc + 1 + getproto(s2v(nfunc))->numparams; - int aux; - /* close all upvalues from previous call */ - if (cl->p->sizep > 0) luaF_close(L, ofunc + 1); - /* move new frame into old one */ - for (aux = 0; nfunc + aux < lim; aux++) - setobjs2s(L, ofunc + aux, nfunc + aux); - oci->top = L->top = ofunc + (L->top - nfunc); /* correct top */ - oci->u.l.savedpc = nci->u.l.savedpc; - oci->callstatus |= CIST_TAIL; /* function was tail called */ - ci = L->ci = oci; /* remove new frame */ - lua_assert(L->top == ofunc + 1 + getproto(s2v(ofunc))->maxstacksize); + if (!ttisfunction(s2v(ra))) { /* not a function? */ + /* try to get '__call' metamethod */ + Protect(ra = luaD_tryfuncTM(L, ra)); + b++; /* there is now one extra argument */ + } + if (!ttisLclosure(s2v(ra))) /* C function? */ + Protect(luaD_precall(L, ra, LUA_MULTRET)); /* call it */ + else { /* tail call */ + if (cl->p->sizep > 0) /* close upvalues from previous call */ + luaF_close(L, ci->func + 1); + luaD_pretailcall(L, ci, ra, b); /* prepare call frame */ goto newframe; /* restart luaV_execute over new Lua function */ } vmbreak; From 41f2936d8f2ec3894e0ef013f6b9e8f6ea17c181 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 22 Nov 2017 16:41:20 -0200 Subject: [PATCH 0122/1145] new opcode 'OP_EQI' for equality with immediate numbers --- lcode.c | 73 ++++++++++++++++++++++++++++++++++++++---------------- lopcodes.c | 4 ++- lopcodes.h | 3 ++- lvm.c | 13 +++++++++- 4 files changed, 68 insertions(+), 25 deletions(-) diff --git a/lcode.c b/lcode.c index ccd25711c4..08e92ea17d 100644 --- a/lcode.c +++ b/lcode.c @@ -1,5 +1,5 @@ /* -** $Id: lcode.c,v 2.132 2017/11/08 14:50:23 roberto Exp roberto $ +** $Id: lcode.c,v 2.133 2017/11/16 12:59:14 roberto Exp roberto $ ** Code generator for Lua ** See Copyright Notice in lua.h */ @@ -607,12 +607,17 @@ void luaK_int (FuncState *fs, int reg, lua_Integer i) { } -static void luaK_float (FuncState *fs, int reg, lua_Number f) { +static int floatI (lua_Number f, lua_Integer *fi) { TValue v; - lua_Integer fi; setfltvalue(&v, f); - if (luaV_flttointeger(&v, &fi, 0) && - l_castS2U(fi) + MAXARG_sBx <= l_castS2U(MAXARG_Bx)) + return (luaV_flttointeger(&v, fi, 0) && + l_castS2U(*fi) + MAXARG_sBx <= l_castS2U(MAXARG_Bx)); +} + + +static void luaK_float (FuncState *fs, int reg, lua_Number f) { + lua_Integer fi; + if (floatI(f, &fi)) luaK_codeAsBx(fs, OP_LOADF, reg, cast_int(fi)); else luaK_codek(fs, reg, luaK_numberK(fs, f)); @@ -1106,6 +1111,20 @@ static int isSCint (expdesc *e) { } +/* +** Check whether expression 'e' is a literal integer or float in +** proper range to fit in register sC +*/ +static int isSCnumber (expdesc *e, lua_Integer *i) { + if (e->k == VKINT) + *i = e->u.ival; + else if (!(e->k == VKFLT && floatI(e->u.nval, i))) + return 0; /* not a number */ + *i += MAXARG_sC; + return (!hasjumps(e) && l_castS2U(*i) <= l_castS2U(MAXARG_C)); +} + + /* ** Create expression 't[k]'. 't' must have its final result already in a ** register or upvalue. Upvalues can only be indexed by literal strings. @@ -1235,6 +1254,11 @@ static void codearith (FuncState *fs, OpCode op, } +static void swapexps (expdesc *e1, expdesc *e2) { + expdesc temp = *e1; *e1 = *e2; *e2 = temp; /* swap 'e1' and 'e2' */ +} + + /* ** Code commutative operators ('+', '*'). If first operand is a ** constant, change order of operands to use immediate operator. @@ -1243,7 +1267,7 @@ static void codecommutative (FuncState *fs, OpCode op, expdesc *e1, expdesc *e2, int line) { int flip = 0; if (isSCint(e1)) { - expdesc temp = *e1; *e1 = *e2; *e2 = temp; /* swap 'e1' and 'e2' */ + swapexps(e1, e2); flip = 1; } codearith(fs, op, e1, e2, flip, line); @@ -1259,10 +1283,6 @@ static void codeorder (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) { int rk2 = luaK_exp2anyreg(fs, e2); freeexps(fs, e1, e2); switch (opr) { - case OPR_NE: { /* '(a ~= b)' ==> 'not (a == b)' */ - e1->u.info = condjump(fs, OP_EQ, 0, rk1, rk2); - break; - } case OPR_GT: case OPR_GE: { /* '(a > b)' ==> '(b < a)'; '(a >= b)' ==> '(b <= a)' */ OpCode op = cast(OpCode, (opr - OPR_NE) + OP_EQ); @@ -1284,21 +1304,28 @@ static void codeorder (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) { ** 'e1' was already put as RK by 'luaK_infix'. */ static void codeeq (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) { - int r1, rk2; - OpCode op = OP_EQK; /* will try first to use a constant */ - if (e1->k == VK) { /* 1st expression is constant? */ - rk2 = e1->u.info; /* constant index */ - r1 = luaK_exp2anyreg(fs, e2); /* 2nd expression must be in register */ + int r1, r2; + lua_Integer im; + OpCode op; + if (e1->k != VNONRELOC) { + lua_assert(e1->k == VK || e1->k == VKINT || e1->k == VKFLT); + swapexps(e1, e2); + } + r1 = luaK_exp2anyreg(fs, e1); /* 1nd expression must be in register */ + if (isSCnumber(e2, &im)) { + op = OP_EQI; + r2 = cast_int(im); /* immediate operand */ + } + else if (luaK_exp2RK(fs, e2)) { /* 1st expression is constant? */ + op = OP_EQK; + r2 = e2->u.info; /* constant index */ } else { - lua_assert(e1->k == VNONRELOC); /* 1st expression is in a register */ - r1 = e1->u.info; - if (!luaK_exp2RK(fs, e2)) /* 2nd expression is not constant? */ - op = OP_EQ; /* will compare two registers */ - rk2 = e2->u.info; /* constant/register index */ + op = OP_EQ; /* will compare two registers */ + r2 = luaK_exp2anyreg(fs, e2); } freeexps(fs, e1, e2); - e1->u.info = condjump(fs, op, (opr == OPR_EQ), r1, rk2); + e1->u.info = condjump(fs, op, (opr == OPR_EQ), r1, r2); e1->k = VJMP; } @@ -1351,7 +1378,9 @@ void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) { break; } case OPR_EQ: case OPR_NE: { - luaK_exp2RK(fs, v); + if (!tonumeral(v, NULL)) + luaK_exp2RK(fs, v); + /* else keep numeral, which may be an immediate operand */ break; } case OPR_LT: case OPR_LE: diff --git a/lopcodes.c b/lopcodes.c index 8740875209..05700950a6 100644 --- a/lopcodes.c +++ b/lopcodes.c @@ -1,5 +1,5 @@ /* -** $Id: lopcodes.c,v 1.67 2017/11/07 17:20:42 roberto Exp roberto $ +** $Id: lopcodes.c,v 1.68 2017/11/16 12:59:14 roberto Exp roberto $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ @@ -67,6 +67,7 @@ LUAI_DDEF const char *const luaP_opnames[NUM_OPCODES+1] = { "LT", "LE", "EQK", + "EQI", "TEST", "TESTSET", "CALL", @@ -135,6 +136,7 @@ LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = { ,opmode(1, 0, iABC) /* OP_LT */ ,opmode(1, 0, iABC) /* OP_LE */ ,opmode(1, 0, iABC) /* OP_EQK */ + ,opmode(1, 0, iABC) /* OP_EQI */ ,opmode(1, 0, iABC) /* OP_TEST */ ,opmode(1, 1, iABC) /* OP_TESTSET */ ,opmode(0, 1, iABC) /* OP_CALL */ diff --git a/lopcodes.h b/lopcodes.h index a805ba636a..6829711c32 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -1,5 +1,5 @@ /* -** $Id: lopcodes.h,v 1.167 2017/11/07 17:20:42 roberto Exp roberto $ +** $Id: lopcodes.h,v 1.168 2017/11/16 12:59:14 roberto Exp roberto $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ @@ -241,6 +241,7 @@ OP_LT,/* A B C if ((R(B) < R(C)) ~= A) then pc++ */ OP_LE,/* A B C if ((R(B) <= R(C)) ~= A) then pc++ */ OP_EQK,/* A B C if ((R(B) == K(C)) ~= A) then pc++ */ +OP_EQI,/* A B C if ((R(B) == C) ~= A) then pc++ */ OP_TEST,/* A C if not (R(A) <=> C) then pc++ */ OP_TESTSET,/* A B C if (R(B) <=> C) then R(A) := R(B) else pc++ */ diff --git a/lvm.c b/lvm.c index b673db916c..6c4bab0842 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.312 2017/11/20 12:57:39 roberto Exp roberto $ +** $Id: lvm.c,v 2.313 2017/11/21 14:17:35 roberto Exp $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -1392,6 +1392,17 @@ void luaV_execute (lua_State *L) { donextjump(ci); vmbreak; } + vmcase(OP_EQI) { + TValue *rb = vRB(i); + int ic = GETARG_sC(i); + if ((ttisinteger(rb) ? (ivalue(rb) == ic) + :ttisfloat(rb) ? luai_numeq(fltvalue(rb), cast_num(ic)) + : 0) != GETARG_A(i)) + pc++; + else + donextjump(ci); + vmbreak; + } vmcase(OP_TEST) { if (GETARG_C(i) ? l_isfalse(s2v(ra)) : !l_isfalse(s2v(ra))) pc++; From 3c230cc8254538d81be300d55481cdac0103e3c5 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 22 Nov 2017 17:15:44 -0200 Subject: [PATCH 0123/1145] using 'A' for register instead of 'B' in relational opcodes ('R(A)' is already created by default for all instructions.) --- lcode.c | 10 +++++----- lopcodes.h | 12 ++++++------ lvm.c | 45 ++++++++++++++++++++------------------------- 3 files changed, 31 insertions(+), 36 deletions(-) diff --git a/lcode.c b/lcode.c index 08e92ea17d..f2c9de7b7c 100644 --- a/lcode.c +++ b/lcode.c @@ -1,5 +1,5 @@ /* -** $Id: lcode.c,v 2.133 2017/11/16 12:59:14 roberto Exp roberto $ +** $Id: lcode.c,v 2.134 2017/11/22 18:41:20 roberto Exp roberto $ ** Code generator for Lua ** See Copyright Notice in lua.h */ @@ -969,7 +969,7 @@ static void negatecondition (FuncState *fs, expdesc *e) { Instruction *pc = getjumpcontrol(fs, e->u.info); lua_assert(testTMode(GET_OPCODE(*pc)) && GET_OPCODE(*pc) != OP_TESTSET && GET_OPCODE(*pc) != OP_TEST); - SETARG_A(*pc, !(GETARG_A(*pc))); + SETARG_B(*pc, !(GETARG_B(*pc))); } @@ -1286,12 +1286,12 @@ static void codeorder (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) { case OPR_GT: case OPR_GE: { /* '(a > b)' ==> '(b < a)'; '(a >= b)' ==> '(b <= a)' */ OpCode op = cast(OpCode, (opr - OPR_NE) + OP_EQ); - e1->u.info = condjump(fs, op, 1, rk2, rk1); /* invert operands */ + e1->u.info = condjump(fs, op, rk2, 1, rk1); /* invert operands */ break; } default: { /* '==', '<', '<=' use their own opcodes */ OpCode op = cast(OpCode, (opr - OPR_EQ) + OP_EQ); - e1->u.info = condjump(fs, op, 1, rk1, rk2); + e1->u.info = condjump(fs, op, rk1, 1, rk2); break; } } @@ -1325,7 +1325,7 @@ static void codeeq (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) { r2 = luaK_exp2anyreg(fs, e2); } freeexps(fs, e1, e2); - e1->u.info = condjump(fs, op, (opr == OPR_EQ), r1, r2); + e1->u.info = condjump(fs, op, r1, (opr == OPR_EQ), r2); e1->k = VJMP; } diff --git a/lopcodes.h b/lopcodes.h index 6829711c32..a964562026 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -1,5 +1,5 @@ /* -** $Id: lopcodes.h,v 1.168 2017/11/16 12:59:14 roberto Exp roberto $ +** $Id: lopcodes.h,v 1.169 2017/11/22 18:41:20 roberto Exp roberto $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ @@ -236,12 +236,12 @@ OP_CONCAT,/* A B C R(A) := R(B).. ... ..R(C) */ OP_CLOSE,/* A close all upvalues >= R(A) */ OP_JMP,/* k sJ pc += sJ (k is used in code generation) */ -OP_EQ,/* A B C if ((R(B) == R(C)) ~= A) then pc++ */ -OP_LT,/* A B C if ((R(B) < R(C)) ~= A) then pc++ */ -OP_LE,/* A B C if ((R(B) <= R(C)) ~= A) then pc++ */ +OP_EQ,/* A B C if ((R(A) == R(C)) ~= B) then pc++ */ +OP_LT,/* A B C if ((R(A) < R(C)) ~= B) then pc++ */ +OP_LE,/* A B C if ((R(A) <= R(C)) ~= B) then pc++ */ -OP_EQK,/* A B C if ((R(B) == K(C)) ~= A) then pc++ */ -OP_EQI,/* A B C if ((R(B) == C) ~= A) then pc++ */ +OP_EQK,/* A B C if ((R(A) == K(C)) ~= B) then pc++ */ +OP_EQI,/* A B C if ((R(A) == C) ~= B) then pc++ */ OP_TEST,/* A C if not (R(A) <=> C) then pc++ */ OP_TESTSET,/* A B C if (R(B) <=> C) then R(A) := R(B) else pc++ */ diff --git a/lvm.c b/lvm.c index 6c4bab0842..457da1ddf7 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.313 2017/11/21 14:17:35 roberto Exp $ +** $Id: lvm.c,v 2.314 2017/11/22 18:41:20 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -715,7 +715,7 @@ void luaV_finishOp (lua_State *L) { res = !res; /* negate result */ } lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_JMP); - if (res != GETARG_A(inst)) /* condition failed? */ + if (res != GETARG_B(inst)) /* condition failed? */ ci->u.l.savedpc++; /* skip jump instruction */ break; } @@ -1340,64 +1340,59 @@ void luaV_execute (lua_State *L) { vmbreak; } vmcase(OP_EQ) { - TValue *rb = vRB(i); TValue *rc = vRC(i); int res; - Protect(res = luaV_equalobj(L, rb, rc)); - if (res != GETARG_A(i)) + Protect(res = luaV_equalobj(L, s2v(ra), rc)); + if (res != GETARG_B(i)) pc++; else donextjump(ci); vmbreak; } vmcase(OP_LT) { - TValue *rb = vRB(i); TValue *rc = vRC(i); int res; - if (ttisinteger(rb) && ttisinteger(rc)) - res = (ivalue(rb) < ivalue(rc)); - else if (ttisnumber(rb) && ttisnumber(rc)) - res = LTnum(rb, rc); + if (ttisinteger(s2v(ra)) && ttisinteger(rc)) + res = (ivalue(s2v(ra)) < ivalue(rc)); + else if (ttisnumber(s2v(ra)) && ttisnumber(rc)) + res = LTnum(s2v(ra), rc); else - Protect(res = lessthanothers(L, rb, rc)); - if (res != GETARG_A(i)) + Protect(res = lessthanothers(L, s2v(ra), rc)); + if (res != GETARG_B(i)) pc++; else donextjump(ci); vmbreak; } vmcase(OP_LE) { - TValue *rb = vRB(i); TValue *rc = vRC(i); int res; - if (ttisinteger(rb) && ttisinteger(rc)) - res = (ivalue(rb) <= ivalue(rc)); - else if (ttisnumber(rb) && ttisnumber(rc)) - res = LEnum(rb, rc); + if (ttisinteger(s2v(ra)) && ttisinteger(rc)) + res = (ivalue(s2v(ra)) <= ivalue(rc)); + else if (ttisnumber(s2v(ra)) && ttisnumber(rc)) + res = LEnum(s2v(ra), rc); else - Protect(res = lessequalothers(L, rb, rc)); - if (res != GETARG_A(i)) + Protect(res = lessequalothers(L, s2v(ra), rc)); + if (res != GETARG_B(i)) pc++; else donextjump(ci); vmbreak; } vmcase(OP_EQK) { - TValue *rb = vRB(i); TValue *rc = KC(i); /* basic types do not use '__eq'; we can use raw equality */ - if (luaV_equalobj(NULL, rb, rc) != GETARG_A(i)) + if (luaV_equalobj(NULL, s2v(ra), rc) != GETARG_B(i)) pc++; else donextjump(ci); vmbreak; } vmcase(OP_EQI) { - TValue *rb = vRB(i); int ic = GETARG_sC(i); - if ((ttisinteger(rb) ? (ivalue(rb) == ic) - :ttisfloat(rb) ? luai_numeq(fltvalue(rb), cast_num(ic)) - : 0) != GETARG_A(i)) + if ((ttisinteger(s2v(ra)) ? (ivalue(s2v(ra)) == ic) + :ttisfloat(s2v(ra)) ? luai_numeq(fltvalue(s2v(ra)), cast_num(ic)) + : 0) != GETARG_B(i)) pc++; else donextjump(ci); From 39f26b1480502cc2f75a5af6e06e5410e06634ba Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 23 Nov 2017 13:38:42 -0200 Subject: [PATCH 0124/1145] more information from 'T.stacklevel' --- ltests.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/ltests.c b/ltests.c index 5cc2b80ace..a2bbb49fc3 100644 --- a/ltests.c +++ b/ltests.c @@ -1,5 +1,5 @@ /* -** $Id: ltests.c,v 2.231 2017/11/07 17:20:42 roberto Exp roberto $ +** $Id: ltests.c,v 2.232 2017/11/09 13:31:29 roberto Exp roberto $ ** Internal Module for Debugging of the Lua Implementation ** See Copyright Notice in lua.h */ @@ -799,8 +799,10 @@ static int stacklevel (lua_State *L) { unsigned long a = 0; lua_pushinteger(L, (L->top - L->stack)); lua_pushinteger(L, (L->stack_last - L->stack)); + lua_pushinteger(L, L->nCcalls); + lua_pushinteger(L, L->nci); lua_pushinteger(L, (unsigned long)&a); - return 3; + return 5; } From 196c87c9cecfacf978f37de4ec69eba0a5971256 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 23 Nov 2017 14:41:16 -0200 Subject: [PATCH 0125/1145] no more 'stackless' implementation; 'luaV_execute' calls itself recursively to execute function calls. 'unroll' continues all executions suspended by an yield (through a long jump) --- ldebug.c | 3 ++- ldo.c | 66 +++++++++++++++---------------------------------------- ldo.h | 3 +-- llimits.h | 7 +++--- lparser.c | 12 ++++------ lstate.c | 28 +++++++++++++++++++++-- lstate.h | 17 +++++++------- ltests.h | 6 ++++- lvm.c | 60 +++++++++++++++++++------------------------------- 9 files changed, 90 insertions(+), 112 deletions(-) diff --git a/ldebug.c b/ldebug.c index 7323f23a8e..94b3596ce4 100644 --- a/ldebug.c +++ b/ldebug.c @@ -1,5 +1,5 @@ /* -** $Id: ldebug.c,v 2.143 2017/11/13 12:20:51 roberto Exp roberto $ +** $Id: ldebug.c,v 2.144 2017/11/13 15:36:52 roberto Exp roberto $ ** Debug Interface ** See Copyright Notice in lua.h */ @@ -724,6 +724,7 @@ l_noret luaG_errormsg (lua_State *L) { setobjs2s(L, L->top, L->top - 1); /* move argument */ setobjs2s(L, L->top - 1, errfunc); /* push function */ L->top++; /* assume EXTRA_STACK */ + luaE_incCcalls(L); luaD_callnoyield(L, L->top - 2, 1); /* call it */ } luaD_throw(L, LUA_ERRRUN); diff --git a/ldo.c b/ldo.c index 3095412310..6b8162d2d5 100644 --- a/ldo.c +++ b/ldo.c @@ -1,5 +1,5 @@ /* -** $Id: ldo.c,v 2.172 2017/11/13 15:36:52 roberto Exp roberto $ +** $Id: ldo.c,v 2.173 2017/11/21 14:17:35 roberto Exp roberto $ ** Stack and Call structure of Lua ** See Copyright Notice in lua.h */ @@ -135,7 +135,7 @@ l_noret luaD_throw (lua_State *L, int errcode) { int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) { - unsigned short oldnCcalls = L->nCcalls; + unsigned short oldnCcalls = L->nCcalls - L->nci; struct lua_longjmp lj; lj.status = LUA_OK; lj.previous = L->errorJmp; /* chain new error handler */ @@ -144,7 +144,7 @@ int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) { (*f)(L, ud); ); L->errorJmp = lj.previous; /* restore old error handler */ - L->nCcalls = oldnCcalls; + L->nCcalls = oldnCcalls + L->nci; return lj.status; } @@ -299,7 +299,7 @@ static void callhook (lua_State *L, CallInfo *ci, int istail) { /* ** Check whether __call metafield of 'func' is a function. If so, put -** it in stack below original 'func' so that 'luaD_precall' can call +** it in stack below original 'func' so that 'luaD_call' can call ** it. Raise an error if __call metafield is not a function. */ StkId luaD_tryfuncTM (lua_State *L, StkId func) { @@ -417,13 +417,12 @@ void luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, int n) { /* -** Prepares a function call: checks the stack, creates a new CallInfo -** entry, fills in the relevant information, calls hook if needed. -** If function is a C function, does the call, too. (Otherwise, leave -** the execution ('luaV_execute') to the caller, to allow stackless -** calls.) Returns true iff function has been executed (C function). +** Call a function (C or Lua). The function to be called is at *func. +** The arguments are on the stack, right after the function. +** When returns, all the results are on the stack, starting at the original +** function position. */ -int luaD_precall (lua_State *L, StkId func, int nresults) { +void luaD_call (lua_State *L, StkId func, int nresults) { lua_CFunction f; TValue *funcv = s2v(func); CallInfo *ci; @@ -449,7 +448,7 @@ int luaD_precall (lua_State *L, StkId func, int nresults) { lua_lock(L); api_checknelems(L, n); luaD_poscall(L, ci, L->top - n, n); - return 1; + break; } case LUA_TLCL: { /* Lua function: prepare its call */ Proto *p = clLvalue(funcv)->p; @@ -469,46 +468,18 @@ int luaD_precall (lua_State *L, StkId func, int nresults) { ci->callstatus = CIST_LUA; if (L->hookmask) callhook(L, ci, 0); - return 0; + luaV_execute(L); /* run the function */ + break; } default: { /* not a function */ func = luaD_tryfuncTM(L, func); /* try to get '__call' metamethod */ - return luaD_precall(L, func, nresults); /* now it must be a function */ + luaD_call(L, func, nresults); /* now it must be a function */ + break; } } } -/* -** Check appropriate error for stack overflow ("regular" overflow or -** overflow while handling stack overflow). If 'nCalls' is larger than -** LUAI_MAXCCALLS (which means it is handling a "regular" overflow) but -** smaller than 9/8 of LUAI_MAXCCALLS, does not report an error (to -** allow overflow handling to work) -*/ -static void stackerror (lua_State *L) { - if (L->nCcalls == LUAI_MAXCCALLS) - luaG_runerror(L, "C stack overflow"); - else if (L->nCcalls >= (LUAI_MAXCCALLS + (LUAI_MAXCCALLS>>3))) - luaD_throw(L, LUA_ERRERR); /* error while handing stack error */ -} - - -/* -** Call a function (C or Lua). The function to be called is at *func. -** The arguments are on the stack, right after the function. -** When returns, all the results are on the stack, starting at the original -** function position. -*/ -void luaD_call (lua_State *L, StkId func, int nResults) { - if (++L->nCcalls >= LUAI_MAXCCALLS) - stackerror(L); - if (!luaD_precall(L, func, nResults)) /* is a Lua function? */ - luaV_execute(L); /* call it */ - L->nCcalls--; -} - - /* ** Similar to 'luaD_call', but does not allow yields during the call */ @@ -541,7 +512,7 @@ static void finishCcall (lua_State *L, int status) { n = (*ci->u.c.k)(L, status, ci->u.c.ctx); /* call continuation function */ lua_lock(L); api_checknelems(L, n); - luaD_poscall(L, ci, L->top - n, n); /* finish 'luaD_precall' */ + luaD_poscall(L, ci, L->top - n, n); /* finish 'luaD_call' */ } @@ -629,8 +600,7 @@ static void resume (lua_State *L, void *ud) { StkId firstArg = L->top - n; /* first argument */ CallInfo *ci = L->ci; if (L->status == LUA_OK) { /* starting a coroutine? */ - if (!luaD_precall(L, firstArg - 1, LUA_MULTRET)) /* Lua function? */ - luaV_execute(L); /* call it */ + luaD_call(L, firstArg - 1, LUA_MULTRET); } else { /* resuming from previous yield */ lua_assert(L->status == LUA_YIELD); @@ -645,7 +615,7 @@ static void resume (lua_State *L, void *ud) { api_checknelems(L, n); firstArg = L->top - n; /* yield results come from continuation */ } - luaD_poscall(L, ci, firstArg, n); /* finish 'luaD_precall' */ + luaD_poscall(L, ci, firstArg, n); /* finish 'luaD_call' */ } unroll(L, NULL); /* run continuation */ } @@ -688,7 +658,7 @@ LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs, : L->top - (L->ci->func + 1); L->nny = oldnny; /* restore 'nny' */ L->nCcalls--; - lua_assert(L->nCcalls == ((from) ? from->nCcalls : 0)); + // lua_assert(L->nCcalls == ((from) ? from->nCcalls : 0)); lua_unlock(L); return status; } diff --git a/ldo.h b/ldo.h index 2640c08830..4fba45b5d6 100644 --- a/ldo.h +++ b/ldo.h @@ -1,5 +1,5 @@ /* -** $Id: ldo.h,v 2.33 2017/11/07 13:25:26 roberto Exp roberto $ +** $Id: ldo.h,v 2.34 2017/11/21 14:18:03 roberto Exp roberto $ ** Stack and Call structure of Lua ** See Copyright Notice in lua.h */ @@ -47,7 +47,6 @@ typedef void (*Pfunc) (lua_State *L, void *ud); LUAI_FUNC int luaD_protectedparser (lua_State *L, ZIO *z, const char *name, const char *mode); LUAI_FUNC void luaD_hook (lua_State *L, int event, int line); -LUAI_FUNC int luaD_precall (lua_State *L, StkId func, int nresults); LUAI_FUNC void luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, int n); LUAI_FUNC void luaD_call (lua_State *L, StkId func, int nResults); LUAI_FUNC void luaD_callnoyield (lua_State *L, StkId func, int nResults); diff --git a/llimits.h b/llimits.h index 4b35dfcb8f..4e641008a6 100644 --- a/llimits.h +++ b/llimits.h @@ -1,5 +1,5 @@ /* -** $Id: llimits.h,v 1.143 2017/06/01 19:16:34 roberto Exp roberto $ +** $Id: llimits.h,v 1.144 2017/06/27 11:35:01 roberto Exp roberto $ ** Limits, basic types, and some other 'installation-dependent' definitions ** See Copyright Notice in lua.h */ @@ -140,10 +140,11 @@ typedef LUAI_UACINT l_uacInt; /* ** maximum depth for nested C calls and syntactical nested non-terminals -** in a program. (Value must fit in an unsigned short int.) +** in a program. (Value must fit in an unsigned short int. It must also +** be compatible with the size of the C stack.) */ #if !defined(LUAI_MAXCCALLS) -#define LUAI_MAXCCALLS 200 +#define LUAI_MAXCCALLS 1000 #endif diff --git a/lparser.c b/lparser.c index 0637a0b753..3eb83fba01 100644 --- a/lparser.c +++ b/lparser.c @@ -1,5 +1,5 @@ /* -** $Id: lparser.c,v 2.166 2017/09/28 16:53:29 roberto Exp roberto $ +** $Id: lparser.c,v 2.167 2017/10/04 21:53:03 roberto Exp roberto $ ** Lua Parser ** See Copyright Notice in lua.h */ @@ -330,11 +330,7 @@ static void adjust_assign (LexState *ls, int nvars, int nexps, expdesc *e) { } -static void enterlevel (LexState *ls) { - lua_State *L = ls->L; - ++L->nCcalls; - checklimit(ls->fs, L->nCcalls, LUAI_MAXCCALLS, "C levels"); -} +#define enterlevel(ls) luaE_incCcalls((ls)->L) #define leavelevel(ls) ((ls)->L->nCcalls--) @@ -1188,9 +1184,9 @@ static void assignment (LexState *ls, struct LHS_assign *lh, int nvars) { suffixedexp(ls, &nv.v); if (!vkisindexed(nv.v.k)) check_conflict(ls, lh, &nv.v); - checklimit(ls->fs, nvars + ls->L->nCcalls, LUAI_MAXCCALLS, - "C levels"); + luaE_incCcalls(ls->L); /* control recursion depth */ assignment(ls, &nv, nvars+1); + ls->L->nCcalls--; } else { /* assignment -> '=' explist */ int nexps; diff --git a/lstate.c b/lstate.c index 54e390b504..22702a007f 100644 --- a/lstate.c +++ b/lstate.c @@ -1,5 +1,5 @@ /* -** $Id: lstate.c,v 2.146 2017/11/07 13:25:26 roberto Exp roberto $ +** $Id: lstate.c,v 2.147 2017/11/13 15:36:52 roberto Exp roberto $ ** Global State ** See Copyright Notice in lua.h */ @@ -97,8 +97,28 @@ void luaE_setdebt (global_State *g, l_mem debt) { } +/* +** Increment count of "C calls" and check for overflows. In case of +** a stack overflow, check appropriate error ("regular" overflow or +** overflow while handling stack overflow). If 'nCalls' is larger than +** LUAI_MAXCCALLS (which means it is handling a "regular" overflow) but +** smaller than 9/8 of LUAI_MAXCCALLS, does not report an error (to +** allow overflow handling to work) +*/ +void luaE_incCcalls (lua_State *L) { + if (++L->nCcalls >= LUAI_MAXCCALLS) { + if (L->nCcalls == LUAI_MAXCCALLS) + luaG_runerror(L, "C stack overflow"); + else if (L->nCcalls >= (LUAI_MAXCCALLS + (LUAI_MAXCCALLS>>3))) + luaD_throw(L, LUA_ERRERR); /* error while handing stack error */ + } +} + + CallInfo *luaE_extendCI (lua_State *L) { - CallInfo *ci = luaM_new(L, CallInfo); + CallInfo *ci; + luaE_incCcalls(L); + ci = luaM_new(L, CallInfo); lua_assert(L->ci->next == NULL); L->ci->next = ci; ci->previous = L->ci; @@ -116,11 +136,13 @@ void luaE_freeCI (lua_State *L) { CallInfo *ci = L->ci; CallInfo *next = ci->next; ci->next = NULL; + L->nCcalls -= L->nci; /* to subtract removed elements from 'nCcalls' */ while ((ci = next) != NULL) { next = ci->next; luaM_free(L, ci); L->nci--; } + L->nCcalls += L->nci; /* to subtract removed elements from 'nCcalls' */ } @@ -130,6 +152,7 @@ void luaE_freeCI (lua_State *L) { void luaE_shrinkCI (lua_State *L) { CallInfo *ci = L->ci; CallInfo *next2; /* next's next */ + L->nCcalls -= L->nci; /* to subtract removed elements from 'nCcalls' */ /* while there are two nexts */ while (ci->next != NULL && (next2 = ci->next->next) != NULL) { luaM_free(L, ci->next); /* free next */ @@ -138,6 +161,7 @@ void luaE_shrinkCI (lua_State *L) { next2->previous = ci; ci = next2; /* keep next's next */ } + L->nCcalls += L->nci; /* to subtract removed elements from 'nCcalls' */ } diff --git a/lstate.h b/lstate.h index 02715dfbd9..fad5463458 100644 --- a/lstate.h +++ b/lstate.h @@ -1,5 +1,5 @@ /* -** $Id: lstate.h,v 2.150 2017/11/07 13:25:26 roberto Exp roberto $ +** $Id: lstate.h,v 2.151 2017/11/13 15:36:52 roberto Exp roberto $ ** Global State ** See Copyright Notice in lua.h */ @@ -104,7 +104,7 @@ typedef struct CallInfo { int nyield; /* number of values yielded */ } u2; short nresults; /* expected number of results from this function */ - unsigned short callstatus; + lu_byte callstatus; } CallInfo; @@ -114,13 +114,11 @@ typedef struct CallInfo { #define CIST_OAH (1<<0) /* original value of 'allowhook' */ #define CIST_LUA (1<<1) /* call is running a Lua function */ #define CIST_HOOKED (1<<2) /* call is running a debug hook */ -#define CIST_FRESH (1<<3) /* call is running on a fresh invocation - of luaV_execute */ -#define CIST_YPCALL (1<<4) /* call is a yieldable protected call */ -#define CIST_TAIL (1<<5) /* call was tail called */ -#define CIST_HOOKYIELD (1<<6) /* last hook called yielded */ -#define CIST_LEQ (1<<7) /* using __lt for __le */ -#define CIST_FIN (1<<8) /* call is running a finalizer */ +#define CIST_YPCALL (1<<3) /* call is a yieldable protected call */ +#define CIST_TAIL (1<<4) /* call was tail called */ +#define CIST_HOOKYIELD (1<<5) /* last hook called yielded */ +#define CIST_LEQ (1<<6) /* using __lt for __le */ +#define CIST_FIN (1<<7) /* call is running a finalizer */ #define isLua(ci) ((ci)->callstatus & CIST_LUA) @@ -256,6 +254,7 @@ LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1); LUAI_FUNC CallInfo *luaE_extendCI (lua_State *L); LUAI_FUNC void luaE_freeCI (lua_State *L); LUAI_FUNC void luaE_shrinkCI (lua_State *L); +LUAI_FUNC void luaE_incCcalls (lua_State *L); #endif diff --git a/ltests.h b/ltests.h index e3d1ca1e0f..04aa91b365 100644 --- a/ltests.h +++ b/ltests.h @@ -1,5 +1,5 @@ /* -** $Id: ltests.h,v 2.51 2017/06/27 11:35:31 roberto Exp roberto $ +** $Id: ltests.h,v 2.52 2017/11/13 12:19:35 roberto Exp roberto $ ** Internal Header for Debugging of the Lua Implementation ** See Copyright Notice in lua.h */ @@ -34,6 +34,10 @@ #define lua_assert(c) assert(c) +/* compiled with -O0, Lua uses a lot of C stack space... */ +#undef LUAI_MAXCCALLS +#define LUAI_MAXCCALLS 300 + /* to avoid warnings, and to make sure value is really unused */ #define UNUSED(x) (x=0, (void)(x)) diff --git a/lvm.c b/lvm.c index 457da1ddf7..ca65c79871 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,9 @@ /* -** $Id: lvm.c,v 2.314 2017/11/22 18:41:20 roberto Exp roberto $ +<<<<<<< lvm.c +** $Id: lvm.c,v 2.313 2017/11/21 14:17:35 roberto Exp roberto $ +======= +** $Id: lvm.c,v 2.315 2017/11/22 19:15:44 roberto Exp $ +>>>>>>> 2.315 ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -829,20 +833,12 @@ void luaV_finishOp (lua_State *L) { void luaV_execute (lua_State *L) { - CallInfo *ci = L->ci; /* local copy of 'L->ci' */ - LClosure *cl; - TValue *k; - StkId base; /* local copy of 'ci->func + 1' */ - int trap; - const Instruction *pc; /* local copy of 'ci->u.l.savedpc' */ - ci->callstatus |= CIST_FRESH; /* fresh invocation of 'luaV_execute" */ - newframe: /* reentry point when frame changes (call/return) */ - lua_assert(ci == L->ci); - cl = clLvalue(s2v(ci->func)); /* local reference to function's closure */ - k = cl->p->k; /* local reference to function's constant table */ - updatetrap(ci); - updatebase(ci); - pc = ci->u.l.savedpc; + CallInfo *ci = L->ci; + LClosure *cl = clLvalue(s2v(ci->func)); + TValue *k = cl->p->k; + StkId base = ci->func + 1; + int trap = ci->u.l.trap; + const Instruction *pc = ci->u.l.savedpc; /* main loop of interpreter */ for (;;) { Instruction i; @@ -1418,20 +1414,13 @@ void luaV_execute (lua_State *L) { vmcase(OP_CALL) { int b = GETARG_B(i); int nresults = GETARG_C(i) - 1; - int isC; if (b != 0) /* fixed number of arguments? */ L->top = ra + b; /* top signals number of arguments */ /* else previous instruction set top */ - Protect(isC = luaD_precall(L, ra, nresults)); - if (isC) { /* C function? */ - if (nresults >= 0) /* fixed number of results? */ - L->top = ci->top; /* correct top */ - /* else leave top for next instruction */ - } - else { /* Lua function */ - ci = L->ci; - goto newframe; /* restart luaV_execute over new Lua function */ - } + Protect(luaD_call(L, ra, nresults)); + if (nresults >= 0) /* fixed number of results? */ + L->top = ci->top; /* correct top */ + /* else leave top for next instruction */ vmbreak; } vmcase(OP_TAILCALL) { @@ -1447,12 +1436,15 @@ void luaV_execute (lua_State *L) { b++; /* there is now one extra argument */ } if (!ttisLclosure(s2v(ra))) /* C function? */ - Protect(luaD_precall(L, ra, LUA_MULTRET)); /* call it */ + Protect(luaD_call(L, ra, LUA_MULTRET)); /* call it */ else { /* tail call */ if (cl->p->sizep > 0) /* close upvalues from previous call */ luaF_close(L, ci->func + 1); luaD_pretailcall(L, ci, ra, b); /* prepare call frame */ - goto newframe; /* restart luaV_execute over new Lua function */ + cl = clLvalue(s2v(ci->func)); + k = cl->p->k; + updatebase(ci); + pc = ci->u.l.savedpc; } vmbreak; } @@ -1461,16 +1453,8 @@ void luaV_execute (lua_State *L) { if (cl->p->sizep > 0) luaF_close(L, base); savepc(L); - b = luaD_poscall(L, ci, ra, (b != 0 ? b - 1 : cast_int(L->top - ra))); - if (ci->callstatus & CIST_FRESH) /* local 'ci' still from callee */ - return; /* external invocation: return */ - else { /* invocation via reentry: continue execution */ - ci = L->ci; - if (b) L->top = ci->top; - lua_assert(isLua(ci)); - lua_assert(GET_OPCODE(*((ci)->u.l.savedpc - 1)) == OP_CALL); - goto newframe; /* restart luaV_execute over new Lua function */ - } + luaD_poscall(L, ci, ra, (b != 0 ? b - 1 : cast_int(L->top - ra))); + return; /* external invocation: return */ } vmcase(OP_FORLOOP) { if (ttisinteger(s2v(ra))) { /* integer loop? */ From 194a4f9710130cdb90df9f4094d81813bdb8ad6b Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 23 Nov 2017 16:29:41 -0200 Subject: [PATCH 0126/1145] small simplifications in 'luaD_poscall' --- ldo.c | 36 ++++++++++++++++++------------------ ldo.h | 4 ++-- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/ldo.c b/ldo.c index 6b8162d2d5..37688a877e 100644 --- a/ldo.c +++ b/ldo.c @@ -1,5 +1,5 @@ /* -** $Id: ldo.c,v 2.173 2017/11/21 14:17:35 roberto Exp roberto $ +** $Id: ldo.c,v 2.174 2017/11/23 16:35:54 roberto Exp roberto $ ** Stack and Call structure of Lua ** See Copyright Notice in lua.h */ @@ -297,6 +297,14 @@ static void callhook (lua_State *L, CallInfo *ci, int istail) { } +static void rethook (lua_State *L, CallInfo *ci) { + if (L->hookmask & LUA_MASKRET) /* is return hook on? */ + luaD_hook(L, LUA_HOOKRET, -1); /* call it */ + if (isLua(ci->previous)) + L->oldpc = ci->previous->u.l.savedpc; /* update 'oldpc' */ +} + + /* ** Check whether __call metafield of 'func' is a function. If so, put ** it in stack below original 'func' so that 'luaD_call' can call @@ -323,8 +331,8 @@ StkId luaD_tryfuncTM (lua_State *L, StkId func) { ** expressions, multiple results for tail calls/single parameters) ** separated. */ -static int moveresults (lua_State *L, StkId firstResult, StkId res, - int nres, int wanted) { +static void moveresults (lua_State *L, StkId firstResult, StkId res, + int nres, int wanted) { switch (wanted) { /* handle typical cases separately */ case 0: break; /* nothing to move */ case 1: { /* one result needed */ @@ -339,7 +347,7 @@ static int moveresults (lua_State *L, StkId firstResult, StkId res, for (i = 0; i < nres; i++) /* move all results to correct place */ setobjs2s(L, res + i, firstResult + i); L->top = res + nres; - return 0; /* wanted == LUA_MULTRET */ + return; } default: { int i; @@ -357,7 +365,6 @@ static int moveresults (lua_State *L, StkId firstResult, StkId res, } } L->top = res + wanted; /* top points after the last result */ - return 1; } @@ -366,22 +373,15 @@ static int moveresults (lua_State *L, StkId firstResult, StkId res, ** moves current number of results to proper place; returns 0 iff call ** wanted multiple (variable number of) results. */ -int luaD_poscall (lua_State *L, CallInfo *ci, StkId firstResult, int nres) { - StkId res; - int wanted = ci->nresults; - if (L->hookmask & (LUA_MASKRET | LUA_MASKLINE)) { - if (L->hookmask & LUA_MASKRET) { - ptrdiff_t fr = savestack(L, firstResult); /* hook may change stack */ - luaD_hook(L, LUA_HOOKRET, -1); - firstResult = restorestack(L, fr); - } - if (isLua(ci->previous)) - L->oldpc = ci->previous->u.l.savedpc; +void luaD_poscall (lua_State *L, CallInfo *ci, StkId firstResult, int nres) { + if (L->hookmask) { + ptrdiff_t fr = savestack(L, firstResult); /* hook may change stack */ + rethook(L, ci); + firstResult = restorestack(L, fr); } - res = ci->func; /* res == final position of 1st result */ L->ci = ci->previous; /* back to caller */ /* move results to proper place */ - return moveresults(L, firstResult, res, nres, wanted); + moveresults(L, firstResult, ci->func, nres, ci->nresults); } diff --git a/ldo.h b/ldo.h index 4fba45b5d6..a9b46cdf4d 100644 --- a/ldo.h +++ b/ldo.h @@ -1,5 +1,5 @@ /* -** $Id: ldo.h,v 2.34 2017/11/21 14:18:03 roberto Exp roberto $ +** $Id: ldo.h,v 2.35 2017/11/23 16:35:54 roberto Exp roberto $ ** Stack and Call structure of Lua ** See Copyright Notice in lua.h */ @@ -53,7 +53,7 @@ LUAI_FUNC void luaD_callnoyield (lua_State *L, StkId func, int nResults); LUAI_FUNC StkId luaD_tryfuncTM (lua_State *L, StkId func); LUAI_FUNC int luaD_pcall (lua_State *L, Pfunc func, void *u, ptrdiff_t oldtop, ptrdiff_t ef); -LUAI_FUNC int luaD_poscall (lua_State *L, CallInfo *ci, StkId firstResult, +LUAI_FUNC void luaD_poscall (lua_State *L, CallInfo *ci, StkId firstResult, int nres); LUAI_FUNC void luaD_reallocstack (lua_State *L, int newsize); LUAI_FUNC void luaD_growstack (lua_State *L, int n); From 73abfde2ef16223b12cf04800f6e53bfc68ad356 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 23 Nov 2017 17:18:10 -0200 Subject: [PATCH 0127/1145] small simplifications around 'luaT_callorderTM' --- ltm.c | 17 +++++++++++++---- lvm.c | 23 ++++++----------------- 2 files changed, 19 insertions(+), 21 deletions(-) diff --git a/ltm.c b/ltm.c index 4c03d2e410..873c2be47c 100644 --- a/ltm.c +++ b/ltm.c @@ -1,5 +1,5 @@ /* -** $Id: ltm.c,v 2.47 2017/11/07 13:25:26 roberto Exp roberto $ +** $Id: ltm.c,v 2.48 2017/11/08 14:50:23 roberto Exp $ ** Tag methods ** See Copyright Notice in lua.h */ @@ -180,10 +180,19 @@ void luaT_trybiniTM (lua_State *L, const TValue *p1, int i2, int luaT_callorderTM (lua_State *L, const TValue *p1, const TValue *p2, TMS event) { - if (!callbinTM(L, p1, p2, L->top, event)) - return -1; /* no metamethod */ - else + if (callbinTM(L, p1, p2, L->top, event)) /* try original event */ return !l_isfalse(s2v(L->top)); + else if (event == TM_LE) { + /* try '!(p2 < p1)' for '(p1 <= p2)' */ + L->ci->callstatus |= CIST_LEQ; /* mark it is doing 'lt' for 'le' */ + if (callbinTM(L, p2, p1, L->top, TM_LT)) { + L->ci->callstatus ^= CIST_LEQ; /* clear mark */ + return l_isfalse(s2v(L->top)); + } + /* else error will remove this 'ci'; no need to clear mark */ + } + luaG_ordererror(L, p1, p2); /* no metamethod found */ + return 0; /* to avoid warnings */ } diff --git a/lvm.c b/lvm.c index ca65c79871..5d9440a6e6 100644 --- a/lvm.c +++ b/lvm.c @@ -1,8 +1,8 @@ /* <<<<<<< lvm.c -** $Id: lvm.c,v 2.313 2017/11/21 14:17:35 roberto Exp roberto $ +** $Id: lvm.c,v 2.316 2017/11/23 16:41:16 roberto Exp roberto $ ======= -** $Id: lvm.c,v 2.315 2017/11/22 19:15:44 roberto Exp $ +** $Id: lvm.c,v 2.316 2017/11/23 16:41:16 roberto Exp roberto $ >>>>>>> 2.315 ** Lua virtual machine ** See Copyright Notice in lua.h @@ -378,13 +378,11 @@ static int LEnum (const TValue *l, const TValue *r) { ** return 'l < r' for non-numbers. */ static int lessthanothers (lua_State *L, const TValue *l, const TValue *r) { - int res; lua_assert(!ttisnumber(l) || !ttisnumber(r)); if (ttisstring(l) && ttisstring(r)) /* both are strings? */ return l_strcmp(tsvalue(l), tsvalue(r)) < 0; - else if ((res = luaT_callorderTM(L, l, r, TM_LT)) < 0) /* no metamethod? */ - luaG_ordererror(L, l, r); /* error */ - return res; + else + return luaT_callorderTM(L, l, r, TM_LT); } @@ -407,20 +405,11 @@ int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r) { ** keeps that information. */ static int lessequalothers (lua_State *L, const TValue *l, const TValue *r) { - int res; lua_assert(!ttisnumber(l) || !ttisnumber(r)); if (ttisstring(l) && ttisstring(r)) /* both are strings? */ return l_strcmp(tsvalue(l), tsvalue(r)) <= 0; - else if ((res = luaT_callorderTM(L, l, r, TM_LE)) >= 0) /* try 'le' */ - return res; - else { /* try 'lt': */ - L->ci->callstatus |= CIST_LEQ; /* mark it is doing 'lt' for 'le' */ - res = luaT_callorderTM(L, r, l, TM_LT); - L->ci->callstatus ^= CIST_LEQ; /* clear mark */ - if (res < 0) - luaG_ordererror(L, l, r); - return !res; /* result is negated */ - } + else + return luaT_callorderTM(L, l, r, TM_LE); } From 599f1742c628db70ef84794b3b8b25fdef9e5004 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 23 Nov 2017 17:29:04 -0200 Subject: [PATCH 0128/1145] detail (typo in comments) --- lapi.c | 4 ++-- lcode.c | 4 ++-- ldebug.c | 4 ++-- lgc.c | 6 +++--- lobject.c | 4 ++-- lobject.h | 4 ++-- lstring.h | 4 ++-- lstrlib.c | 4 ++-- ltable.c | 4 ++-- 9 files changed, 19 insertions(+), 19 deletions(-) diff --git a/lapi.c b/lapi.c index 1c1e8f9e1b..181c90d608 100644 --- a/lapi.c +++ b/lapi.c @@ -1,5 +1,5 @@ /* -** $Id: lapi.c,v 2.273 2017/11/02 11:28:56 roberto Exp $ +** $Id: lapi.c,v 2.276 2017/11/07 13:25:26 roberto Exp roberto $ ** Lua API ** See Copyright Notice in lua.h */ @@ -205,7 +205,7 @@ LUA_API void lua_settop (lua_State *L, int idx) { ** Reverse the stack segment from 'from' to 'to' ** (auxiliary to 'lua_rotate') ** Note that we move(copy) only the value inside the stack. -** (We do not move addicional fields that may exist.) +** (We do not move additional fields that may exist.) */ static void reverse (lua_State *L, StkId from, StkId to) { for (; from < to; from++, to--) { diff --git a/lcode.c b/lcode.c index f2c9de7b7c..7b50f3cfd8 100644 --- a/lcode.c +++ b/lcode.c @@ -1,5 +1,5 @@ /* -** $Id: lcode.c,v 2.134 2017/11/22 18:41:20 roberto Exp roberto $ +** $Id: lcode.c,v 2.135 2017/11/22 19:15:44 roberto Exp roberto $ ** Code generator for Lua ** See Copyright Notice in lua.h */ @@ -1331,7 +1331,7 @@ static void codeeq (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) { /* -** Aplly prefix operation 'op' to expression 'e'. +** Apply prefix operation 'op' to expression 'e'. */ void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e, int line) { static const expdesc ef = {VKINT, {0}, NO_JUMP, NO_JUMP}; diff --git a/ldebug.c b/ldebug.c index 94b3596ce4..1010240583 100644 --- a/ldebug.c +++ b/ldebug.c @@ -1,5 +1,5 @@ /* -** $Id: ldebug.c,v 2.144 2017/11/13 15:36:52 roberto Exp roberto $ +** $Id: ldebug.c,v 2.145 2017/11/23 16:35:54 roberto Exp roberto $ ** Debug Interface ** See Copyright Notice in lua.h */ @@ -51,7 +51,7 @@ static int currentpc (CallInfo *ci) { /* ** Get a "base line" to find the line corresponding to an instruction. ** For that, search the array of absolute line info for the largest saved -** instruction smaller or equal to the wanted instrution. A special +** instruction smaller or equal to the wanted instruction. A special ** case is when there is no absolute info or the instruction is before ** the first absolute one. */ diff --git a/lgc.c b/lgc.c index 036020ae85..389d69bb7b 100644 --- a/lgc.c +++ b/lgc.c @@ -1,5 +1,5 @@ /* -** $Id: lgc.c,v 2.236 2017/10/31 15:29:28 roberto Exp $ +** $Id: lgc.c,v 2.238 2017/11/07 13:25:26 roberto Exp roberto $ ** Garbage Collector ** See Copyright Notice in lua.h */ @@ -507,7 +507,7 @@ static lu_mem traversetable (global_State *g, Table *h) { ** mode, check the generational invariant. If the cache is old, ** everything is ok. If the prototype is 'old0', everything ** is ok too. (It will naturally be visited again.) If the -** prototype is older than 'old0', then its cache (whith is new) +** prototype is older than 'old0', then its cache (which is new) ** must be visited again in the next collection, so the prototype ** goes to the 'protogray' list. (If the prototype has a cache, ** it is already immutable and does not need other barriers; @@ -1195,7 +1195,7 @@ static void entergen (lua_State *L, global_State *g) { luaC_runtilstate(L, bitmask(GCSpause)); /* prepare to start a new cycle */ luaC_runtilstate(L, bitmask(GCSpropagate)); /* start new cycle */ atomic(L); - /* sweep all ellements making them old */ + /* sweep all elements making them old */ sweep2old(L, &g->allgc); /* everything alive now is old */ g->reallyold = g->old = g->survival = g->allgc; diff --git a/lobject.c b/lobject.c index fb2c172c04..e9e037f743 100644 --- a/lobject.c +++ b/lobject.c @@ -1,5 +1,5 @@ /* -** $Id: lobject.c,v 2.119 2017/11/08 14:50:23 roberto Exp roberto $ +** $Id: lobject.c,v 2.120 2017/11/16 13:19:06 roberto Exp roberto $ ** Some generic functions over Lua objects ** See Copyright Notice in lua.h */ @@ -272,7 +272,7 @@ static const char *l_str2dloc (const char *s, lua_Number *result, int mode) { ** - 'n'/'N' means 'inf' or 'nan' (which should be rejected) ** - '.' just optimizes the search for the common case (nothing special) ** This function accepts both the current locale or a dot as the radix -** mark. If the convertion fails, it may mean number has a dot but +** mark. If the conversion fails, it may mean number has a dot but ** locale accepts something else. In that case, the code copies 's' ** to a buffer (because 's' is read-only), changes the dot to the ** current locale radix mark, and tries to convert again. diff --git a/lobject.h b/lobject.h index 372ec13b1f..c7b6be18d4 100644 --- a/lobject.h +++ b/lobject.h @@ -1,5 +1,5 @@ /* -** $Id: lobject.h,v 2.125 2017/06/29 15:06:44 roberto Exp $ +** $Id: lobject.h,v 2.130 2017/11/07 13:25:26 roberto Exp roberto $ ** Type definitions for Lua objects ** See Copyright Notice in lua.h */ @@ -529,7 +529,7 @@ typedef union Closure { /* ** Nodes for Hash tables. A pack of two TValue's (key-value pairs) -** plus a 'next' field to link colliding entries. The distribuition +** plus a 'next' field to link colliding entries. The distribution ** of the key's fields ('key_tt' and 'key_val') not forming a proper ** 'TValue' allows for a smaller size for 'Node' both in 4-byte ** and 8-byte alignments. diff --git a/lstring.h b/lstring.h index 416d951928..a994fe1e5b 100644 --- a/lstring.h +++ b/lstring.h @@ -1,5 +1,5 @@ /* -** $Id: lstring.h,v 1.61 2015/11/03 15:36:01 roberto Exp roberto $ +** $Id: lstring.h,v 1.62 2017/07/27 13:50:16 roberto Exp roberto $ ** String table (keep all strings handled by Lua) ** See Copyright Notice in lua.h */ @@ -14,7 +14,7 @@ /* ** Memory-allocation error message must be preallocated (it cannot -** be created after memory is exausted) +** be created after memory is exhausted) */ #define MEMERRMSG "not enough memory" diff --git a/lstrlib.c b/lstrlib.c index 9a4cf90d49..84b6e4eb2e 100644 --- a/lstrlib.c +++ b/lstrlib.c @@ -1,5 +1,5 @@ /* -** $Id: lstrlib.c,v 1.258 2017/11/08 14:50:23 roberto Exp roberto $ +** $Id: lstrlib.c,v 1.259 2017/11/16 13:19:06 roberto Exp roberto $ ** Standard library for string operations and pattern-matching ** See Copyright Notice in lua.h */ @@ -1117,7 +1117,7 @@ static void addliteral (lua_State *L, luaL_Buffer *b, int arg) { else { /* integers */ lua_Integer n = lua_tointeger(L, arg); const char *format = (n == LUA_MININTEGER) /* corner case? */ - ? "0x%" LUA_INTEGER_FRMLEN "x" /* use hexa */ + ? "0x%" LUA_INTEGER_FRMLEN "x" /* use hex */ : LUA_INTEGER_FMT; /* else use default format */ nb = l_sprintf(buff, MAX_ITEM, format, (LUAI_UACINT)n); } diff --git a/ltable.c b/ltable.c index 33c6852a03..54799c210f 100644 --- a/ltable.c +++ b/ltable.c @@ -1,5 +1,5 @@ /* -** $Id: ltable.c,v 2.125 2017/06/29 15:06:44 roberto Exp roberto $ +** $Id: ltable.c,v 2.126 2017/11/08 14:50:23 roberto Exp roberto $ ** Lua tables (hash) ** See Copyright Notice in lua.h */ @@ -685,7 +685,7 @@ static lua_Unsigned hash_search (Table *t, lua_Unsigned j) { ** First, try the array part: if there is an array part and its last ** element is nil, there must be a boundary there; a binary search ** finds that boundary. Otherwise, if the hash part is empty or does not -** contain 'j + 1', 'j' is a boundary. Othersize, call 'hash_search' +** contain 'j + 1', 'j' is a boundary. Otherwize, call 'hash_search' ** to find a boundary in the hash part. */ lua_Unsigned luaH_getn (Table *t) { From 093c16b67b263e334600ed306310b9015f65fa8b Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 27 Nov 2017 15:44:31 -0200 Subject: [PATCH 0129/1145] new opcodes 'OP_LTI' and 'OP_LEI' --- lopcodes.c | 6 +++++- lopcodes.h | 4 +++- ltm.c | 16 +++++++++++++++- ltm.h | 4 +++- lvm.c | 53 +++++++++++++++++++++++++++++++++++++++++++++-------- 5 files changed, 71 insertions(+), 12 deletions(-) diff --git a/lopcodes.c b/lopcodes.c index 05700950a6..122157eb41 100644 --- a/lopcodes.c +++ b/lopcodes.c @@ -1,5 +1,5 @@ /* -** $Id: lopcodes.c,v 1.68 2017/11/16 12:59:14 roberto Exp roberto $ +** $Id: lopcodes.c,v 1.69 2017/11/22 18:41:20 roberto Exp roberto $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ @@ -68,6 +68,8 @@ LUAI_DDEF const char *const luaP_opnames[NUM_OPCODES+1] = { "LE", "EQK", "EQI", + "LTI", + "LEI", "TEST", "TESTSET", "CALL", @@ -137,6 +139,8 @@ LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = { ,opmode(1, 0, iABC) /* OP_LE */ ,opmode(1, 0, iABC) /* OP_EQK */ ,opmode(1, 0, iABC) /* OP_EQI */ + ,opmode(1, 0, iABC) /* OP_LTI */ + ,opmode(1, 0, iABC) /* OP_LEI */ ,opmode(1, 0, iABC) /* OP_TEST */ ,opmode(1, 1, iABC) /* OP_TESTSET */ ,opmode(0, 1, iABC) /* OP_CALL */ diff --git a/lopcodes.h b/lopcodes.h index a964562026..5b955ce8d4 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -1,5 +1,5 @@ /* -** $Id: lopcodes.h,v 1.169 2017/11/22 18:41:20 roberto Exp roberto $ +** $Id: lopcodes.h,v 1.170 2017/11/22 19:15:44 roberto Exp roberto $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ @@ -242,6 +242,8 @@ OP_LE,/* A B C if ((R(A) <= R(C)) ~= B) then pc++ */ OP_EQK,/* A B C if ((R(A) == K(C)) ~= B) then pc++ */ OP_EQI,/* A B C if ((R(A) == C) ~= B) then pc++ */ +OP_LTI,/* A B C if ((R(A) < C) ~= B) then pc++ */ +OP_LEI,/* A B C if ((R(A) <= C) ~= B) then pc++ */ OP_TEST,/* A C if not (R(A) <=> C) then pc++ */ OP_TESTSET,/* A B C if (R(B) <=> C) then R(A) := R(B) else pc++ */ diff --git a/ltm.c b/ltm.c index 873c2be47c..5ee123f564 100644 --- a/ltm.c +++ b/ltm.c @@ -1,5 +1,5 @@ /* -** $Id: ltm.c,v 2.48 2017/11/08 14:50:23 roberto Exp $ +** $Id: ltm.c,v 2.49 2017/11/23 19:18:10 roberto Exp roberto $ ** Tag methods ** See Copyright Notice in lua.h */ @@ -196,6 +196,20 @@ int luaT_callorderTM (lua_State *L, const TValue *p1, const TValue *p2, } +int luaT_callorderiTM (lua_State *L, const TValue *p1, int v2, + int inv, TMS event) { + TValue aux; const TValue *p2; + setivalue(&aux, v2); + if (inv) { /* arguments were exchanged? */ + p2 = p1; p1 = &aux; /* correct them */ + event = (event == TM_LE) ? TM_LT : TM_LE; + } + else + p2 = &aux; + return (luaT_callorderTM(L, p1, p2, event) != inv); +} + + void luaT_adjustvarargs (lua_State *L, Proto *p, int actual) { int i; Table *vtab; diff --git a/ltm.h b/ltm.h index 31454a5cac..c8fed77c62 100644 --- a/ltm.h +++ b/ltm.h @@ -1,5 +1,5 @@ /* -** $Id: ltm.h,v 2.25 2017/06/29 15:06:44 roberto Exp roberto $ +** $Id: ltm.h,v 2.26 2017/09/27 18:59:08 roberto Exp roberto $ ** Tag methods ** See Copyright Notice in lua.h */ @@ -72,6 +72,8 @@ LUAI_FUNC void luaT_trybiniTM (lua_State *L, const TValue *p1, int i2, int inv, StkId res, TMS event); LUAI_FUNC int luaT_callorderTM (lua_State *L, const TValue *p1, const TValue *p2, TMS event); +LUAI_FUNC int luaT_callorderiTM (lua_State *L, const TValue *p1, int v2, + int inv, TMS event); LUAI_FUNC void luaT_adjustvarargs (lua_State *L, Proto *p, int actual); LUAI_FUNC void luaT_getvarargs (lua_State *L, TValue *t, StkId where, diff --git a/lvm.c b/lvm.c index 5d9440a6e6..364c64dd39 100644 --- a/lvm.c +++ b/lvm.c @@ -1,9 +1,5 @@ /* -<<<<<<< lvm.c -** $Id: lvm.c,v 2.316 2017/11/23 16:41:16 roberto Exp roberto $ -======= -** $Id: lvm.c,v 2.316 2017/11/23 16:41:16 roberto Exp roberto $ ->>>>>>> 2.315 +** $Id: lvm.c,v 2.317 2017/11/23 19:18:10 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -699,16 +695,21 @@ void luaV_finishOp (lua_State *L) { setobjs2s(L, base + GETARG_A(inst), --L->top); break; } - case OP_LE: case OP_LT: case OP_EQ: { + case OP_LT: case OP_LE: + case OP_LTI: case OP_LEI: + case OP_EQ: { /* note that 'OP_EQI'/'OP_EQK' cannot yield */ int res = !l_isfalse(s2v(L->top - 1)); L->top--; if (ci->callstatus & CIST_LEQ) { /* "<=" using "<" instead? */ - lua_assert(op == OP_LE); + lua_assert(op == OP_LE || + (op == OP_LEI && !(GETARG_B(inst) & 2)) || + (op == OP_LTI && GETARG_B(inst) & 2)); ci->callstatus ^= CIST_LEQ; /* clear mark */ res = !res; /* negate result */ } lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_JMP); - if (res != GETARG_B(inst)) /* condition failed? */ + if (GETARG_B(inst) & 2) res = !res; + if (res != (GETARG_B(inst) & 1)) /* condition failed? */ ci->u.l.savedpc++; /* skip jump instruction */ break; } @@ -1383,6 +1384,42 @@ void luaV_execute (lua_State *L) { donextjump(ci); vmbreak; } + vmcase(OP_LTI) { + int res; + int ic = GETARG_sC(i); + if (ttisinteger(s2v(ra))) + res = (ivalue(s2v(ra)) < ic); + else if (ttisfloat(s2v(ra))) { + lua_Number f = fltvalue(s2v(ra)); + res = (!luai_numisnan(f)) ? luai_numlt(f, cast_num(ic)) + : GETARG_B(i) >> 1; /* NaN? */ + } + else + Protect(res = luaT_callorderiTM(L, s2v(ra), ic, GETARG_B(i) >> 1, TM_LT)); + if (res != (GETARG_B(i) & 1)) + pc++; + else + donextjump(ci); + vmbreak; + } + vmcase(OP_LEI) { + int res; + int ic = GETARG_sC(i); + if (ttisinteger(s2v(ra))) + res = (ivalue(s2v(ra)) <= ic); + else if (ttisfloat(s2v(ra))) { + lua_Number f = fltvalue(s2v(ra)); + res = (!luai_numisnan(f)) ? luai_numle(f, cast_num(ic)) + : GETARG_B(i) >> 1; /* NaN? */ + } + else + Protect(res = luaT_callorderiTM(L, s2v(ra), ic, GETARG_B(i) >> 1, TM_LE)); + if (res != (GETARG_B(i) & 1)) + pc++; + else + donextjump(ci); + vmbreak; + } vmcase(OP_TEST) { if (GETARG_C(i) ? l_isfalse(s2v(ra)) : !l_isfalse(s2v(ra))) pc++; From dfd188ba12f22db8d31cb80426d568e97345e1e2 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 28 Nov 2017 09:19:07 -0200 Subject: [PATCH 0130/1145] detail (identation of switch) --- ldump.c | 35 +++++++++++++++++------------------ lundump.c | 37 ++++++++++++++++++------------------- 2 files changed, 35 insertions(+), 37 deletions(-) diff --git a/ldump.c b/ldump.c index 16a95f9bd6..4d3f643574 100644 --- a/ldump.c +++ b/ldump.c @@ -1,5 +1,5 @@ /* -** $Id: ldump.c,v 2.38 2017/06/27 11:35:31 roberto Exp roberto $ +** $Id: ldump.c,v 2.39 2017/06/27 14:21:12 roberto Exp roberto $ ** save precompiled Lua chunks ** See Copyright Notice in lua.h */ @@ -113,23 +113,22 @@ static void DumpConstants (const Proto *f, DumpState *D) { const TValue *o = &f->k[i]; DumpByte(ttype(o), D); switch (ttype(o)) { - case LUA_TNIL: - break; - case LUA_TBOOLEAN: - DumpByte(bvalue(o), D); - break; - case LUA_TNUMFLT: - DumpNumber(fltvalue(o), D); - break; - case LUA_TNUMINT: - DumpInteger(ivalue(o), D); - break; - case LUA_TSHRSTR: - case LUA_TLNGSTR: - DumpString(tsvalue(o), D); - break; - default: - lua_assert(0); + case LUA_TNIL: + break; + case LUA_TBOOLEAN: + DumpByte(bvalue(o), D); + break; + case LUA_TNUMFLT: + DumpNumber(fltvalue(o), D); + break; + case LUA_TNUMINT: + DumpInteger(ivalue(o), D); + break; + case LUA_TSHRSTR: + case LUA_TLNGSTR: + DumpString(tsvalue(o), D); + break; + default: lua_assert(0); } } } diff --git a/lundump.c b/lundump.c index 1f2f1a925a..489a149479 100644 --- a/lundump.c +++ b/lundump.c @@ -1,5 +1,5 @@ /* -** $Id: lundump.c,v 2.46 2017/06/27 14:21:12 roberto Exp roberto $ +** $Id: lundump.c,v 2.47 2017/06/29 15:06:44 roberto Exp roberto $ ** load precompiled Lua chunks ** See Copyright Notice in lua.h */ @@ -134,24 +134,23 @@ static void LoadConstants (LoadState *S, Proto *f) { TValue *o = &f->k[i]; int t = LoadByte(S); switch (t) { - case LUA_TNIL: - setnilvalue(o); - break; - case LUA_TBOOLEAN: - setbvalue(o, LoadByte(S)); - break; - case LUA_TNUMFLT: - setfltvalue(o, LoadNumber(S)); - break; - case LUA_TNUMINT: - setivalue(o, LoadInteger(S)); - break; - case LUA_TSHRSTR: - case LUA_TLNGSTR: - setsvalue2n(S->L, o, LoadString(S)); - break; - default: - lua_assert(0); + case LUA_TNIL: + setnilvalue(o); + break; + case LUA_TBOOLEAN: + setbvalue(o, LoadByte(S)); + break; + case LUA_TNUMFLT: + setfltvalue(o, LoadNumber(S)); + break; + case LUA_TNUMINT: + setivalue(o, LoadInteger(S)); + break; + case LUA_TSHRSTR: + case LUA_TLNGSTR: + setsvalue2n(S->L, o, LoadString(S)); + break; + default: lua_assert(0); } } } From ff5fe5104413cf2a5156f86479d4b9b130bad7a6 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 28 Nov 2017 10:58:18 -0200 Subject: [PATCH 0131/1145] using register 'k' for conditions in tests (we only need one bit there) --- lcode.c | 94 +++++++++++++++++++++++++++++++++--------------------- lopcodes.h | 30 ++++++++++------- lvm.c | 88 +++++++++++++++++++++++++------------------------- 3 files changed, 120 insertions(+), 92 deletions(-) diff --git a/lcode.c b/lcode.c index 7b50f3cfd8..04d7b9e032 100644 --- a/lcode.c +++ b/lcode.c @@ -1,5 +1,5 @@ /* -** $Id: lcode.c,v 2.135 2017/11/22 19:15:44 roberto Exp roberto $ +** $Id: lcode.c,v 2.136 2017/11/23 19:29:04 roberto Exp roberto $ ** Code generator for Lua ** See Copyright Notice in lua.h */ @@ -160,8 +160,8 @@ void luaK_ret (FuncState *fs, int first, int nret) { ** Code a "conditional jump", that is, a test or comparison opcode ** followed by a jump. Return jump position. */ -static int condjump (FuncState *fs, OpCode op, int A, int B, int C) { - luaK_codeABC(fs, op, A, B, C); +static int condjump (FuncState *fs, OpCode op, int A, int B, int C, int k) { + luaK_codeABCk(fs, op, A, B, C, k); return luaK_jump(fs); } @@ -206,7 +206,7 @@ static int patchtestreg (FuncState *fs, int node, int reg) { else { /* no register to put value or register already has the value; change instruction to simple test */ - *i = CREATE_ABCk(OP_TEST, GETARG_B(*i), 0, GETARG_C(*i), 0); + *i = CREATE_ABCk(OP_TEST, GETARG_B(*i), 0, 0, GETARG_k(*i)); } return 1; } @@ -969,7 +969,7 @@ static void negatecondition (FuncState *fs, expdesc *e) { Instruction *pc = getjumpcontrol(fs, e->u.info); lua_assert(testTMode(GET_OPCODE(*pc)) && GET_OPCODE(*pc) != OP_TESTSET && GET_OPCODE(*pc) != OP_TEST); - SETARG_B(*pc, !(GETARG_B(*pc))); + SETARG_k(*pc, (GETARG_k(*pc) ^ 1)); } @@ -984,13 +984,13 @@ static int jumponcond (FuncState *fs, expdesc *e, int cond) { Instruction ie = getinstruction(fs, e); if (GET_OPCODE(ie) == OP_NOT) { fs->pc--; /* remove previous OP_NOT */ - return condjump(fs, OP_TEST, GETARG_B(ie), 0, !cond); + return condjump(fs, OP_TEST, GETARG_B(ie), 0, 0, !cond); } /* else go through */ } discharge2anyreg(fs, e); freeexp(fs, e); - return condjump(fs, OP_TESTSET, NO_REG, e->u.info, cond); + return condjump(fs, OP_TESTSET, NO_REG, e->u.info, 0, cond); } @@ -1276,25 +1276,37 @@ static void codecommutative (FuncState *fs, OpCode op, /* ** Emit code for order comparisons. -** 'e1' was already put in register by 'luaK_infix'. +** When the first operand is an integral value in the proper range, +** change (A < B) to (!(B <= A)) and (A <= B) to (!(B < A)) so that +** it can use an immediate operand. In this case, C indicates this +** change, for cases that cannot assume a total order (NaN and +** metamethods). */ -static void codeorder (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) { - int rk1 = check_exp(e1->k == VNONRELOC, e1->u.info); - int rk2 = luaK_exp2anyreg(fs, e2); - freeexps(fs, e1, e2); - switch (opr) { - case OPR_GT: case OPR_GE: { - /* '(a > b)' ==> '(b < a)'; '(a >= b)' ==> '(b <= a)' */ - OpCode op = cast(OpCode, (opr - OPR_NE) + OP_EQ); - e1->u.info = condjump(fs, op, rk2, 1, rk1); /* invert operands */ - break; - } - default: { /* '==', '<', '<=' use their own opcodes */ - OpCode op = cast(OpCode, (opr - OPR_EQ) + OP_EQ); - e1->u.info = condjump(fs, op, rk1, 1, rk2); - break; - } +static void codeorder (FuncState *fs, OpCode op, expdesc *e1, expdesc *e2) { + int r1, r2; + int cond = 1; + int C = 0; + lua_Integer im; + if (isSCnumber(e2, &im)) { + /* use immediate operand */ + r1 = luaK_exp2anyreg(fs, e1); + r2 = cast_int(im); + op = cast(OpCode, (op - OP_LT) + OP_LTI); + } + else if (isSCnumber(e1, &im)) { + /* transform (A < B) to (!(B <= A)) and (A <= B) to (!(B < A)) */ + r1 = luaK_exp2anyreg(fs, e2); + r2 = cast_int(im); + op = (op == OP_LT) ? OP_LEI : OP_LTI; + cond = 0; /* negate original test */ + C = 1; /* indication that it used the transformations */ } + else { /* regular case, compare two registers */ + r1 = luaK_exp2anyreg(fs, e1); + r2 = luaK_exp2anyreg(fs, e2); + } + freeexps(fs, e1, e2); + e1->u.info = condjump(fs, op, r1, r2, C, cond); e1->k = VJMP; } @@ -1325,7 +1337,7 @@ static void codeeq (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) { r2 = luaK_exp2anyreg(fs, e2); } freeexps(fs, e1, e2); - e1->u.info = condjump(fs, op, r1, (opr == OPR_EQ), r2); + e1->u.info = condjump(fs, op, r1, r2, 0, (opr == OPR_EQ)); e1->k = VJMP; } @@ -1385,7 +1397,10 @@ void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) { } case OPR_LT: case OPR_LE: case OPR_GT: case OPR_GE: { - luaK_exp2anyreg(fs, v); + lua_Integer dummy; + if (!isSCnumber(v, &dummy)) + luaK_exp2RK(fs, v); + /* else keep numeral, which may be an immediate operand */ break; } default: lua_assert(0); @@ -1399,9 +1414,9 @@ void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) { ** concatenation is right associative), merge second CONCAT into first ** one. */ -void luaK_posfix (FuncState *fs, BinOpr op, +void luaK_posfix (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2, int line) { - switch (op) { + switch (opr) { case OPR_AND: { lua_assert(e1->t == NO_JUMP); /* list closed by 'luK_infix' */ luaK_dischargevars(fs, e2); @@ -1432,28 +1447,35 @@ void luaK_posfix (FuncState *fs, BinOpr op, break; } case OPR_ADD: case OPR_MUL: { - if (!constfolding(fs, op + LUA_OPADD, e1, e2)) - codecommutative(fs, cast(OpCode, op + OP_ADD), e1, e2, line); + if (!constfolding(fs, opr + LUA_OPADD, e1, e2)) + codecommutative(fs, cast(OpCode, opr + OP_ADD), e1, e2, line); break; } case OPR_SUB: case OPR_DIV: case OPR_IDIV: case OPR_MOD: case OPR_POW: { - if (!constfolding(fs, op + LUA_OPADD, e1, e2)) - codearith(fs, cast(OpCode, op + OP_ADD), e1, e2, 0, line); + if (!constfolding(fs, opr + LUA_OPADD, e1, e2)) + codearith(fs, cast(OpCode, opr + OP_ADD), e1, e2, 0, line); break; } case OPR_BAND: case OPR_BOR: case OPR_BXOR: case OPR_SHL: case OPR_SHR: { - if (!constfolding(fs, op + LUA_OPADD, e1, e2)) - codebinexpval(fs, cast(OpCode, op + OP_ADD), e1, e2, line); + if (!constfolding(fs, opr + LUA_OPADD, e1, e2)) + codebinexpval(fs, cast(OpCode, opr + OP_ADD), e1, e2, line); break; } case OPR_EQ: case OPR_NE: { - codeeq(fs, op, e1, e2); + codeeq(fs, opr, e1, e2); + break; + } + case OPR_LT: case OPR_LE: { + OpCode op = cast(OpCode, (opr - OPR_EQ) + OP_EQ); + codeorder(fs, op, e1, e2); break; } - case OPR_LT: case OPR_LE: case OPR_GT: case OPR_GE: { + /* '(a > b)' <=> '(b < a)'; '(a >= b)' <=> '(b <= a)' */ + OpCode op = cast(OpCode, (opr - OPR_NE) + OP_EQ); + swapexps(e1, e2); codeorder(fs, op, e1, e2); break; } diff --git a/lopcodes.h b/lopcodes.h index 5b955ce8d4..f2b6716566 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -1,5 +1,5 @@ /* -** $Id: lopcodes.h,v 1.170 2017/11/22 19:15:44 roberto Exp roberto $ +** $Id: lopcodes.h,v 1.171 2017/11/27 17:44:31 roberto Exp roberto $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ @@ -114,13 +114,15 @@ enum OpMode {iABC, iABx, iAsBx, iAx, isJ}; /* basic instruction formats */ #define SETARG_A(i,v) setarg(i, v, POS_A, SIZE_A) #define GETARG_B(i) check_exp(checkopm(i, iABC), getarg(i, POS_B, SIZE_B)) +#define GETARG_sB(i) (GETARG_B(i) - MAXARG_sC) #define SETARG_B(i,v) setarg(i, v, POS_B, SIZE_B) #define GETARG_C(i) check_exp(checkopm(i, iABC), getarg(i, POS_C, SIZE_C)) #define GETARG_sC(i) (GETARG_C(i) - MAXARG_sC) #define SETARG_C(i,v) setarg(i, v, POS_C, SIZE_C) -#define GETARG_k(i) (cast(int, ((i) & (1 << POS_k)))) +#define TESTARG_k(i) (cast(int, ((i) & (1 << POS_k)))) +#define GETARG_k(i) getarg(i, POS_k, 1) #define SETARG_k(i,v) setarg(i, v, POS_k, 1) #define GETARG_Bx(i) check_exp(checkopm(i, iABx), getarg(i, POS_Bx, SIZE_Bx)) @@ -236,17 +238,17 @@ OP_CONCAT,/* A B C R(A) := R(B).. ... ..R(C) */ OP_CLOSE,/* A close all upvalues >= R(A) */ OP_JMP,/* k sJ pc += sJ (k is used in code generation) */ -OP_EQ,/* A B C if ((R(A) == R(C)) ~= B) then pc++ */ -OP_LT,/* A B C if ((R(A) < R(C)) ~= B) then pc++ */ -OP_LE,/* A B C if ((R(A) <= R(C)) ~= B) then pc++ */ +OP_EQ,/* A B if ((R(A) == R(B)) ~= k) then pc++ */ +OP_LT,/* A B if ((R(A) < R(B)) ~= k) then pc++ */ +OP_LE,/* A B if ((R(A) <= R(B)) ~= k) then pc++ */ -OP_EQK,/* A B C if ((R(A) == K(C)) ~= B) then pc++ */ -OP_EQI,/* A B C if ((R(A) == C) ~= B) then pc++ */ -OP_LTI,/* A B C if ((R(A) < C) ~= B) then pc++ */ -OP_LEI,/* A B C if ((R(A) <= C) ~= B) then pc++ */ +OP_EQK,/* A B if ((R(A) == K(B)) ~= k) then pc++ */ +OP_EQI,/* A sB if ((R(A) == sB) ~= k) then pc++ */ +OP_LTI,/* A sB if ((R(A) < sB) ~= k) then pc++ */ +OP_LEI,/* A sB if ((R(A) <= sB) ~= k) then pc++ */ -OP_TEST,/* A C if not (R(A) <=> C) then pc++ */ -OP_TESTSET,/* A B C if (R(B) <=> C) then R(A) := R(B) else pc++ */ +OP_TEST,/* A if (not R(A) == k) then pc++ */ +OP_TESTSET,/* A B if (not R(B) == k) then R(A) := R(B) else pc++ */ OP_CALL,/* A B C R(A), ... ,R(A+C-2) := R(A)(R(A+1), ... ,R(A+B-1)) */ OP_TAILCALL,/* A B C return R(A)(R(A+1), ... ,R(A+B-1)) */ @@ -289,9 +291,13 @@ OP_EXTRAARG/* Ax extra (larger) argument for previous opcode */ (*) In OP_LOADKX, the next 'instruction' is always EXTRAARG. - (*) For comparisons, A specifies what condition the test should accept + (*) For comparisons, k specifies what condition the test should accept (true or false). + (*) For OP_LTI/OP_LEI, C indicates that the transformations + (A (!(B<=A)) or (A<=B) => (!(Btop--; if (ci->callstatus & CIST_LEQ) { /* "<=" using "<" instead? */ lua_assert(op == OP_LE || - (op == OP_LEI && !(GETARG_B(inst) & 2)) || - (op == OP_LTI && GETARG_B(inst) & 2)); + (op == OP_LTI && GETARG_C(inst)) || + (op == OP_LEI && !GETARG_C(inst))); ci->callstatus ^= CIST_LEQ; /* clear mark */ res = !res; /* negate result */ } lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_JMP); - if (GETARG_B(inst) & 2) res = !res; - if (res != (GETARG_B(inst) & 1)) /* condition failed? */ + if (GETARG_C(inst)) res = !res; + if (res != GETARG_k(inst)) /* condition failed? */ ci->u.l.savedpc++; /* skip jump instruction */ break; } @@ -766,7 +766,7 @@ void luaV_finishOp (lua_State *L) { #define RC(i) (base+GETARG_C(i)) #define vRC(i) s2v(RC(i)) #define KC(i) (k+GETARG_C(i)) -#define RKC(i) ((GETARG_k(i)) ? k + GETARG_C(i) : s2v(base + GETARG_C(i))) +#define RKC(i) ((TESTARG_k(i)) ? k + GETARG_C(i) : s2v(base + GETARG_C(i))) @@ -1326,59 +1326,59 @@ void luaV_execute (lua_State *L) { vmbreak; } vmcase(OP_EQ) { - TValue *rc = vRC(i); + TValue *rb = vRB(i); int res; - Protect(res = luaV_equalobj(L, s2v(ra), rc)); - if (res != GETARG_B(i)) + Protect(res = luaV_equalobj(L, s2v(ra), rb)); + if (res != GETARG_k(i)) pc++; else donextjump(ci); vmbreak; } vmcase(OP_LT) { - TValue *rc = vRC(i); + TValue *rb = vRB(i); int res; - if (ttisinteger(s2v(ra)) && ttisinteger(rc)) - res = (ivalue(s2v(ra)) < ivalue(rc)); - else if (ttisnumber(s2v(ra)) && ttisnumber(rc)) - res = LTnum(s2v(ra), rc); + if (ttisinteger(s2v(ra)) && ttisinteger(rb)) + res = (ivalue(s2v(ra)) < ivalue(rb)); + else if (ttisnumber(s2v(ra)) && ttisnumber(rb)) + res = LTnum(s2v(ra), rb); else - Protect(res = lessthanothers(L, s2v(ra), rc)); - if (res != GETARG_B(i)) + Protect(res = lessthanothers(L, s2v(ra), rb)); + if (res != GETARG_k(i)) pc++; else donextjump(ci); vmbreak; } vmcase(OP_LE) { - TValue *rc = vRC(i); + TValue *rb = vRB(i); int res; - if (ttisinteger(s2v(ra)) && ttisinteger(rc)) - res = (ivalue(s2v(ra)) <= ivalue(rc)); - else if (ttisnumber(s2v(ra)) && ttisnumber(rc)) - res = LEnum(s2v(ra), rc); + if (ttisinteger(s2v(ra)) && ttisinteger(rb)) + res = (ivalue(s2v(ra)) <= ivalue(rb)); + else if (ttisnumber(s2v(ra)) && ttisnumber(rb)) + res = LEnum(s2v(ra), rb); else - Protect(res = lessequalothers(L, s2v(ra), rc)); - if (res != GETARG_B(i)) + Protect(res = lessequalothers(L, s2v(ra), rb)); + if (res != GETARG_k(i)) pc++; else donextjump(ci); vmbreak; } vmcase(OP_EQK) { - TValue *rc = KC(i); + TValue *rb = KB(i); /* basic types do not use '__eq'; we can use raw equality */ - if (luaV_equalobj(NULL, s2v(ra), rc) != GETARG_B(i)) + if (luaV_equalobj(NULL, s2v(ra), rb) != GETARG_k(i)) pc++; else donextjump(ci); vmbreak; } vmcase(OP_EQI) { - int ic = GETARG_sC(i); - if ((ttisinteger(s2v(ra)) ? (ivalue(s2v(ra)) == ic) - :ttisfloat(s2v(ra)) ? luai_numeq(fltvalue(s2v(ra)), cast_num(ic)) - : 0) != GETARG_B(i)) + int im = GETARG_sB(i); + if ((ttisinteger(s2v(ra)) ? (ivalue(s2v(ra)) == im) + :ttisfloat(s2v(ra)) ? luai_numeq(fltvalue(s2v(ra)), cast_num(im)) + : 0) != GETARG_k(i)) pc++; else donextjump(ci); @@ -1386,17 +1386,17 @@ void luaV_execute (lua_State *L) { } vmcase(OP_LTI) { int res; - int ic = GETARG_sC(i); + int im = GETARG_sB(i); if (ttisinteger(s2v(ra))) - res = (ivalue(s2v(ra)) < ic); + res = (ivalue(s2v(ra)) < im); else if (ttisfloat(s2v(ra))) { lua_Number f = fltvalue(s2v(ra)); - res = (!luai_numisnan(f)) ? luai_numlt(f, cast_num(ic)) - : GETARG_B(i) >> 1; /* NaN? */ + res = (!luai_numisnan(f)) ? luai_numlt(f, cast_num(im)) + : GETARG_C(i); /* NaN? */ } else - Protect(res = luaT_callorderiTM(L, s2v(ra), ic, GETARG_B(i) >> 1, TM_LT)); - if (res != (GETARG_B(i) & 1)) + Protect(res = luaT_callorderiTM(L, s2v(ra), im, GETARG_C(i), TM_LT)); + if (res != GETARG_k(i)) pc++; else donextjump(ci); @@ -1404,32 +1404,32 @@ void luaV_execute (lua_State *L) { } vmcase(OP_LEI) { int res; - int ic = GETARG_sC(i); + int im = GETARG_sB(i); if (ttisinteger(s2v(ra))) - res = (ivalue(s2v(ra)) <= ic); + res = (ivalue(s2v(ra)) <= im); else if (ttisfloat(s2v(ra))) { lua_Number f = fltvalue(s2v(ra)); - res = (!luai_numisnan(f)) ? luai_numle(f, cast_num(ic)) - : GETARG_B(i) >> 1; /* NaN? */ + res = (!luai_numisnan(f)) ? luai_numle(f, cast_num(im)) + : GETARG_C(i); /* NaN? */ } else - Protect(res = luaT_callorderiTM(L, s2v(ra), ic, GETARG_B(i) >> 1, TM_LE)); - if (res != (GETARG_B(i) & 1)) + Protect(res = luaT_callorderiTM(L, s2v(ra), im, GETARG_C(i), TM_LE)); + if (res != GETARG_k(i)) pc++; else donextjump(ci); vmbreak; } vmcase(OP_TEST) { - if (GETARG_C(i) ? l_isfalse(s2v(ra)) : !l_isfalse(s2v(ra))) + if (l_isfalse(s2v(ra)) == GETARG_k(i)) pc++; else - donextjump(ci); + donextjump(ci); vmbreak; } vmcase(OP_TESTSET) { TValue *rb = vRB(i); - if (GETARG_C(i) ? l_isfalse(rb) : !l_isfalse(rb)) + if (l_isfalse(rb) == GETARG_k(i)) pc++; else { setobj2s(L, ra, rb); From 1a5e8c1014a5416108032042d57f06595553d2bd Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 28 Nov 2017 12:51:00 -0200 Subject: [PATCH 0132/1145] conditional jumps unified in label "condjump' + new variable 'vra' to avoid excessive use of macro 's2v' --- lvm.c | 234 ++++++++++++++++++++++++++-------------------------------- 1 file changed, 106 insertions(+), 128 deletions(-) diff --git a/lvm.c b/lvm.c index 7e6ad2a6ea..63e65675f6 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.318 2017/11/27 17:44:31 roberto Exp roberto $ +** $Id: lvm.c,v 2.319 2017/11/28 12:58:18 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -815,6 +815,7 @@ void luaV_finishOp (lua_State *L) { updatebase(ci); /* the trap may be just for that */ \ } \ ra = RA(i); /* WARNING: any stack reallocation invalidates 'ra' */ \ + vra = s2v(ra); \ } #define vmdispatch(o) switch(o) @@ -831,8 +832,10 @@ void luaV_execute (lua_State *L) { const Instruction *pc = ci->u.l.savedpc; /* main loop of interpreter */ for (;;) { - Instruction i; - StkId ra; + int cond; /* flag for conditional jumps */ + Instruction i; /* instruction being executed */ + StkId ra; /* instruction's A register */ + TValue *vra; /* corresponding value */ vmfetch(); lua_assert(base == ci->func + 1); lua_assert(base <= L->top && L->top < L->stack + L->stacksize); @@ -848,12 +851,12 @@ void luaV_execute (lua_State *L) { } vmcase(OP_LOADI) { lua_Integer b = GETARG_sBx(i); - setivalue(s2v(ra), b); + setivalue(vra, b); vmbreak; } vmcase(OP_LOADF) { int b = GETARG_sBx(i); - setfltvalue(s2v(ra), cast_num(b)); + setfltvalue(vra, cast_num(b)); vmbreak; } vmcase(OP_LOADKX) { @@ -863,7 +866,7 @@ void luaV_execute (lua_State *L) { vmbreak; } vmcase(OP_LOADBOOL) { - setbvalue(s2v(ra), GETARG_B(i)); + setbvalue(vra, GETARG_B(i)); if (GETARG_C(i)) pc++; /* skip next instruction (if C) */ vmbreak; } @@ -881,8 +884,8 @@ void luaV_execute (lua_State *L) { } vmcase(OP_SETUPVAL) { UpVal *uv = cl->upvals[GETARG_B(i)]; - setobj(L, uv->v, s2v(ra)); - luaC_barrier(L, uv, s2v(ra)); + setobj(L, uv->v, vra); + luaC_barrier(L, uv, vra); vmbreak; } vmcase(OP_GETTABUP) { @@ -956,25 +959,25 @@ void luaV_execute (lua_State *L) { TValue *rc = RKC(i); /* value */ lua_Unsigned n; if (ttisinteger(rb) /* fast track for integers? */ - ? (n = ivalue(rb), luaV_fastgeti(L, s2v(ra), n, slot)) - : luaV_fastget(L, s2v(ra), rb, slot, luaH_get)) { - luaV_finishfastset(L, s2v(ra), slot, rc); + ? (n = ivalue(rb), luaV_fastgeti(L, vra, n, slot)) + : luaV_fastget(L, vra, rb, slot, luaH_get)) { + luaV_finishfastset(L, vra, slot, rc); } else - Protect(luaV_finishset(L, s2v(ra), rb, rc, slot)); + Protect(luaV_finishset(L, vra, rb, rc, slot)); vmbreak; } vmcase(OP_SETI) { const TValue *slot; int c = GETARG_B(i); TValue *rc = RKC(i); - if (luaV_fastgeti(L, s2v(ra), c, slot)) { - luaV_finishfastset(L, s2v(ra), slot, rc); + if (luaV_fastgeti(L, vra, c, slot)) { + luaV_finishfastset(L, vra, slot, rc); } else { TValue key; setivalue(&key, c); - Protect(luaV_finishset(L, s2v(ra), &key, rc, slot)); + Protect(luaV_finishset(L, vra, &key, rc, slot)); } vmbreak; } @@ -983,11 +986,11 @@ void luaV_execute (lua_State *L) { TValue *rb = KB(i); TValue *rc = RKC(i); TString *key = tsvalue(rb); /* key must be a string */ - if (luaV_fastget(L, s2v(ra), key, slot, luaH_getshortstr)) { - luaV_finishfastset(L, s2v(ra), slot, rc); + if (luaV_fastget(L, vra, key, slot, luaH_getshortstr)) { + luaV_finishfastset(L, vra, slot, rc); } else - Protect(luaV_finishset(L, s2v(ra), rb, rc, slot)); + Protect(luaV_finishset(L, vra, rb, rc, slot)); vmbreak; } vmcase(OP_NEWTABLE) { @@ -1020,10 +1023,10 @@ void luaV_execute (lua_State *L) { int ic = GETARG_sC(i); lua_Number nb; if (ttisinteger(rb)) { - setivalue(s2v(ra), intop(+, ivalue(rb), ic)); + setivalue(vra, intop(+, ivalue(rb), ic)); } else if (tonumberns(rb, nb)) { - setfltvalue(s2v(ra), luai_numadd(L, nb, cast_num(ic))); + setfltvalue(vra, luai_numadd(L, nb, cast_num(ic))); } else Protect(luaT_trybiniTM(L, rb, ic, GETARG_k(i), ra, TM_ADD)); @@ -1034,10 +1037,10 @@ void luaV_execute (lua_State *L) { int ic = GETARG_sC(i); lua_Number nb; if (ttisinteger(rb)) { - setivalue(s2v(ra), intop(-, ivalue(rb), ic)); + setivalue(vra, intop(-, ivalue(rb), ic)); } else if (tonumberns(rb, nb)) { - setfltvalue(s2v(ra), luai_numsub(L, nb, cast_num(ic))); + setfltvalue(vra, luai_numsub(L, nb, cast_num(ic))); } else Protect(luaT_trybiniTM(L, rb, ic, 0, ra, TM_SUB)); @@ -1048,10 +1051,10 @@ void luaV_execute (lua_State *L) { int ic = GETARG_sC(i); lua_Number nb; if (ttisinteger(rb)) { - setivalue(s2v(ra), intop(*, ivalue(rb), ic)); + setivalue(vra, intop(*, ivalue(rb), ic)); } else if (tonumberns(rb, nb)) { - setfltvalue(s2v(ra), luai_nummul(L, nb, cast_num(ic))); + setfltvalue(vra, luai_nummul(L, nb, cast_num(ic))); } else Protect(luaT_trybiniTM(L, rb, ic, GETARG_k(i), ra, TM_MUL)); @@ -1062,13 +1065,13 @@ void luaV_execute (lua_State *L) { int ic = GETARG_sC(i); lua_Number nb; if (ttisinteger(rb)) { - setivalue(s2v(ra), luaV_mod(L, ivalue(rb), ic)); + setivalue(vra, luaV_mod(L, ivalue(rb), ic)); } else if (tonumberns(rb, nb)) { lua_Number m; lua_Number nc = cast_num(ic); luai_nummod(L, nb, nc, m); - setfltvalue(s2v(ra), m); + setfltvalue(vra, m); } else Protect(luaT_trybiniTM(L, rb, ic, 0, ra, TM_MOD)); @@ -1080,7 +1083,7 @@ void luaV_execute (lua_State *L) { lua_Number nb; if (tonumberns(rb, nb)) { lua_Number nc = cast_num(ic); - setfltvalue(s2v(ra), luai_numpow(L, nb, nc)); + setfltvalue(vra, luai_numpow(L, nb, nc)); } else Protect(luaT_trybiniTM(L, rb, ic, 0, ra, TM_POW)); @@ -1092,7 +1095,7 @@ void luaV_execute (lua_State *L) { lua_Number nb; if (tonumberns(rb, nb)) { lua_Number nc = cast_num(ic); - setfltvalue(s2v(ra), luai_numdiv(L, nb, nc)); + setfltvalue(vra, luai_numdiv(L, nb, nc)); } else Protect(luaT_trybiniTM(L, rb, ic, 0, ra, TM_DIV)); @@ -1103,11 +1106,11 @@ void luaV_execute (lua_State *L) { int ic = GETARG_sC(i); lua_Number nb; if (ttisinteger(rb)) { - setivalue(s2v(ra), luaV_div(L, ivalue(rb), ic)); + setivalue(vra, luaV_div(L, ivalue(rb), ic)); } else if (tonumberns(rb, nb)) { lua_Number nc = cast_num(ic); - setfltvalue(s2v(ra), luai_numdiv(L, nb, nc)); + setfltvalue(vra, luai_numdiv(L, nb, nc)); } else Protect(luaT_trybiniTM(L, rb, ic, 0, ra, TM_IDIV)); @@ -1119,10 +1122,10 @@ void luaV_execute (lua_State *L) { lua_Number nb; lua_Number nc; if (ttisinteger(rb) && ttisinteger(rc)) { lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc); - setivalue(s2v(ra), intop(+, ib, ic)); + setivalue(vra, intop(+, ib, ic)); } else if (tonumberns(rb, nb) && tonumberns(rc, nc)) { - setfltvalue(s2v(ra), luai_numadd(L, nb, nc)); + setfltvalue(vra, luai_numadd(L, nb, nc)); } else Protect(luaT_trybinTM(L, rb, rc, ra, TM_ADD)); @@ -1134,10 +1137,10 @@ void luaV_execute (lua_State *L) { lua_Number nb; lua_Number nc; if (ttisinteger(rb) && ttisinteger(rc)) { lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc); - setivalue(s2v(ra), intop(-, ib, ic)); + setivalue(vra, intop(-, ib, ic)); } else if (tonumberns(rb, nb) && tonumberns(rc, nc)) { - setfltvalue(s2v(ra), luai_numsub(L, nb, nc)); + setfltvalue(vra, luai_numsub(L, nb, nc)); } else Protect(luaT_trybinTM(L, rb, rc, ra, TM_SUB)); @@ -1149,10 +1152,10 @@ void luaV_execute (lua_State *L) { lua_Number nb; lua_Number nc; if (ttisinteger(rb) && ttisinteger(rc)) { lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc); - setivalue(s2v(ra), intop(*, ib, ic)); + setivalue(vra, intop(*, ib, ic)); } else if (tonumberns(rb, nb) && tonumberns(rc, nc)) { - setfltvalue(s2v(ra), luai_nummul(L, nb, nc)); + setfltvalue(vra, luai_nummul(L, nb, nc)); } else Protect(luaT_trybinTM(L, rb, rc, ra, TM_MUL)); @@ -1163,7 +1166,7 @@ void luaV_execute (lua_State *L) { TValue *rc = vRC(i); lua_Number nb; lua_Number nc; if (tonumberns(rb, nb) && tonumberns(rc, nc)) { - setfltvalue(s2v(ra), luai_numdiv(L, nb, nc)); + setfltvalue(vra, luai_numdiv(L, nb, nc)); } else Protect(luaT_trybinTM(L, rb, rc, ra, TM_DIV)); @@ -1174,7 +1177,7 @@ void luaV_execute (lua_State *L) { TValue *rc = vRC(i); lua_Integer ib; lua_Integer ic; if (tointegerns(rb, &ib) && tointegerns(rc, &ic)) { - setivalue(s2v(ra), intop(&, ib, ic)); + setivalue(vra, intop(&, ib, ic)); } else Protect(luaT_trybinTM(L, rb, rc, ra, TM_BAND)); @@ -1185,7 +1188,7 @@ void luaV_execute (lua_State *L) { TValue *rc = vRC(i); lua_Integer ib; lua_Integer ic; if (tointegerns(rb, &ib) && tointegerns(rc, &ic)) { - setivalue(s2v(ra), intop(|, ib, ic)); + setivalue(vra, intop(|, ib, ic)); } else Protect(luaT_trybinTM(L, rb, rc, ra, TM_BOR)); @@ -1196,7 +1199,7 @@ void luaV_execute (lua_State *L) { TValue *rc = vRC(i); lua_Integer ib; lua_Integer ic; if (tointegerns(rb, &ib) && tointegerns(rc, &ic)) { - setivalue(s2v(ra), intop(^, ib, ic)); + setivalue(vra, intop(^, ib, ic)); } else Protect(luaT_trybinTM(L, rb, rc, ra, TM_BXOR)); @@ -1207,7 +1210,7 @@ void luaV_execute (lua_State *L) { TValue *rc = vRC(i); lua_Integer ib; lua_Integer ic; if (tointegerns(rb, &ib) && tointegerns(rc, &ic)) { - setivalue(s2v(ra), luaV_shiftl(ib, ic)); + setivalue(vra, luaV_shiftl(ib, ic)); } else Protect(luaT_trybinTM(L, rb, rc, ra, TM_SHL)); @@ -1218,7 +1221,7 @@ void luaV_execute (lua_State *L) { TValue *rc = vRC(i); lua_Integer ib; lua_Integer ic; if (tointegerns(rb, &ib) && tointegerns(rc, &ic)) { - setivalue(s2v(ra), luaV_shiftl(ib, -ic)); + setivalue(vra, luaV_shiftl(ib, -ic)); } else Protect(luaT_trybinTM(L, rb, rc, ra, TM_SHR)); @@ -1230,12 +1233,12 @@ void luaV_execute (lua_State *L) { lua_Number nb; lua_Number nc; if (ttisinteger(rb) && ttisinteger(rc)) { lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc); - setivalue(s2v(ra), luaV_mod(L, ib, ic)); + setivalue(vra, luaV_mod(L, ib, ic)); } else if (tonumberns(rb, nb) && tonumberns(rc, nc)) { lua_Number m; luai_nummod(L, nb, nc, m); - setfltvalue(s2v(ra), m); + setfltvalue(vra, m); } else Protect(luaT_trybinTM(L, rb, rc, ra, TM_MOD)); @@ -1247,10 +1250,10 @@ void luaV_execute (lua_State *L) { lua_Number nb; lua_Number nc; if (ttisinteger(rb) && ttisinteger(rc)) { lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc); - setivalue(s2v(ra), luaV_div(L, ib, ic)); + setivalue(vra, luaV_div(L, ib, ic)); } else if (tonumberns(rb, nb) && tonumberns(rc, nc)) { - setfltvalue(s2v(ra), luai_numidiv(L, nb, nc)); + setfltvalue(vra, luai_numidiv(L, nb, nc)); } else Protect(luaT_trybinTM(L, rb, rc, ra, TM_IDIV)); @@ -1261,7 +1264,7 @@ void luaV_execute (lua_State *L) { TValue *rc = vRC(i); lua_Number nb; lua_Number nc; if (tonumberns(rb, nb) && tonumberns(rc, nc)) { - setfltvalue(s2v(ra), luai_numpow(L, nb, nc)); + setfltvalue(vra, luai_numpow(L, nb, nc)); } else Protect(luaT_trybinTM(L, rb, rc, ra, TM_POW)); @@ -1272,10 +1275,10 @@ void luaV_execute (lua_State *L) { lua_Number nb; if (ttisinteger(rb)) { lua_Integer ib = ivalue(rb); - setivalue(s2v(ra), intop(-, 0, ib)); + setivalue(vra, intop(-, 0, ib)); } else if (tonumberns(rb, nb)) { - setfltvalue(s2v(ra), luai_numunm(L, nb)); + setfltvalue(vra, luai_numunm(L, nb)); } else Protect(luaT_trybinTM(L, rb, rb, ra, TM_UNM)); @@ -1285,7 +1288,7 @@ void luaV_execute (lua_State *L) { TValue *rb = vRB(i); lua_Integer ib; if (tointegerns(rb, &ib)) { - setivalue(s2v(ra), intop(^, ~l_castS2U(0), ib)); + setivalue(vra, intop(^, ~l_castS2U(0), ib)); } else Protect(luaT_trybinTM(L, rb, rb, ra, TM_BNOT)); @@ -1293,8 +1296,8 @@ void luaV_execute (lua_State *L) { } vmcase(OP_NOT) { TValue *rb = vRB(i); - int res = l_isfalse(rb); /* next assignment may change this value */ - setbvalue(s2v(ra), res); + int nrb = l_isfalse(rb); /* next assignment may change this value */ + setbvalue(vra, nrb); vmbreak; } vmcase(OP_LEN) { @@ -1327,105 +1330,79 @@ void luaV_execute (lua_State *L) { } vmcase(OP_EQ) { TValue *rb = vRB(i); - int res; - Protect(res = luaV_equalobj(L, s2v(ra), rb)); - if (res != GETARG_k(i)) - pc++; + Protect(cond = luaV_equalobj(L, vra, rb)); + condjump: + if (cond != GETARG_k(i)) + pc++; /* skip next jump */ else donextjump(ci); vmbreak; } vmcase(OP_LT) { TValue *rb = vRB(i); - int res; - if (ttisinteger(s2v(ra)) && ttisinteger(rb)) - res = (ivalue(s2v(ra)) < ivalue(rb)); - else if (ttisnumber(s2v(ra)) && ttisnumber(rb)) - res = LTnum(s2v(ra), rb); - else - Protect(res = lessthanothers(L, s2v(ra), rb)); - if (res != GETARG_k(i)) - pc++; + if (ttisinteger(vra) && ttisinteger(rb)) + cond = (ivalue(vra) < ivalue(rb)); + else if (ttisnumber(vra) && ttisnumber(rb)) + cond = LTnum(vra, rb); else - donextjump(ci); - vmbreak; + Protect(cond = lessthanothers(L, vra, rb)); + goto condjump; } vmcase(OP_LE) { TValue *rb = vRB(i); - int res; - if (ttisinteger(s2v(ra)) && ttisinteger(rb)) - res = (ivalue(s2v(ra)) <= ivalue(rb)); - else if (ttisnumber(s2v(ra)) && ttisnumber(rb)) - res = LEnum(s2v(ra), rb); - else - Protect(res = lessequalothers(L, s2v(ra), rb)); - if (res != GETARG_k(i)) - pc++; + if (ttisinteger(vra) && ttisinteger(rb)) + cond = (ivalue(vra) <= ivalue(rb)); + else if (ttisnumber(vra) && ttisnumber(rb)) + cond = LEnum(vra, rb); else - donextjump(ci); - vmbreak; + Protect(cond = lessequalothers(L, vra, rb)); + goto condjump; } vmcase(OP_EQK) { TValue *rb = KB(i); /* basic types do not use '__eq'; we can use raw equality */ - if (luaV_equalobj(NULL, s2v(ra), rb) != GETARG_k(i)) - pc++; - else - donextjump(ci); - vmbreak; + cond = luaV_equalobj(NULL, vra, rb); + goto condjump; } vmcase(OP_EQI) { int im = GETARG_sB(i); - if ((ttisinteger(s2v(ra)) ? (ivalue(s2v(ra)) == im) - :ttisfloat(s2v(ra)) ? luai_numeq(fltvalue(s2v(ra)), cast_num(im)) - : 0) != GETARG_k(i)) - pc++; + if (ttisinteger(vra)) + cond = (ivalue(vra) == im); + else if (ttisfloat(vra)) + cond = luai_numeq(fltvalue(vra), cast_num(im)); else - donextjump(ci); - vmbreak; + cond = 0; /* other types cannot be equal to a number */ + goto condjump; } vmcase(OP_LTI) { - int res; int im = GETARG_sB(i); - if (ttisinteger(s2v(ra))) - res = (ivalue(s2v(ra)) < im); - else if (ttisfloat(s2v(ra))) { - lua_Number f = fltvalue(s2v(ra)); - res = (!luai_numisnan(f)) ? luai_numlt(f, cast_num(im)) - : GETARG_C(i); /* NaN? */ + if (ttisinteger(vra)) + cond = (ivalue(vra) < im); + else if (ttisfloat(vra)) { + lua_Number f = fltvalue(vra); + cond = (!luai_numisnan(f)) ? luai_numlt(f, cast_num(im)) + : GETARG_C(i); /* NaN */ } else - Protect(res = luaT_callorderiTM(L, s2v(ra), im, GETARG_C(i), TM_LT)); - if (res != GETARG_k(i)) - pc++; - else - donextjump(ci); - vmbreak; + Protect(cond = luaT_callorderiTM(L, vra, im, GETARG_C(i), TM_LT)); + goto condjump; } vmcase(OP_LEI) { - int res; int im = GETARG_sB(i); - if (ttisinteger(s2v(ra))) - res = (ivalue(s2v(ra)) <= im); - else if (ttisfloat(s2v(ra))) { - lua_Number f = fltvalue(s2v(ra)); - res = (!luai_numisnan(f)) ? luai_numle(f, cast_num(im)) + if (ttisinteger(vra)) + cond = (ivalue(vra) <= im); + else if (ttisfloat(vra)) { + lua_Number f = fltvalue(vra); + cond = (!luai_numisnan(f)) ? luai_numle(f, cast_num(im)) : GETARG_C(i); /* NaN? */ } else - Protect(res = luaT_callorderiTM(L, s2v(ra), im, GETARG_C(i), TM_LE)); - if (res != GETARG_k(i)) - pc++; - else - donextjump(ci); - vmbreak; + Protect(cond = luaT_callorderiTM(L, vra, im, GETARG_C(i), TM_LE)); + goto condjump; } vmcase(OP_TEST) { - if (l_isfalse(s2v(ra)) == GETARG_k(i)) - pc++; - else - donextjump(ci); - vmbreak; + cond = !l_isfalse(vra); + goto condjump; } vmcase(OP_TESTSET) { TValue *rb = vRB(i); @@ -1456,12 +1433,13 @@ void luaV_execute (lua_State *L) { else /* previous instruction set top */ b = L->top - ra; lua_assert(GETARG_C(i) - 1 == LUA_MULTRET); - if (!ttisfunction(s2v(ra))) { /* not a function? */ + if (!ttisfunction(vra)) { /* not a function? */ /* try to get '__call' metamethod */ Protect(ra = luaD_tryfuncTM(L, ra)); + vra = s2v(ra); b++; /* there is now one extra argument */ } - if (!ttisLclosure(s2v(ra))) /* C function? */ + if (!ttisLclosure(vra)) /* C function? */ Protect(luaD_call(L, ra, LUA_MULTRET)); /* call it */ else { /* tail call */ if (cl->p->sizep > 0) /* close upvalues from previous call */ @@ -1483,25 +1461,25 @@ void luaV_execute (lua_State *L) { return; /* external invocation: return */ } vmcase(OP_FORLOOP) { - if (ttisinteger(s2v(ra))) { /* integer loop? */ + if (ttisinteger(vra)) { /* integer loop? */ lua_Integer step = ivalue(s2v(ra + 2)); - lua_Integer idx = intop(+, ivalue(s2v(ra)), step); /* increment index */ + lua_Integer idx = intop(+, ivalue(vra), step); /* increment index */ lua_Integer limit = ivalue(s2v(ra + 1)); if ((0 < step) ? (idx <= limit) : (limit <= idx)) { pc -= GETARG_Bx(i); /* jump back */ - chgivalue(s2v(ra), idx); /* update internal index... */ + chgivalue(vra, idx); /* update internal index... */ setivalue(s2v(ra + 3), idx); /* ...and external index */ } } else { /* floating loop */ lua_Number step = fltvalue(s2v(ra + 2)); lua_Number limit = fltvalue(s2v(ra + 1)); - lua_Number idx = fltvalue(s2v(ra)); + lua_Number idx = fltvalue(vra); idx = luai_numadd(L, idx, step); /* inc. index */ if (luai_numlt(0, step) ? luai_numle(idx, limit) : luai_numle(limit, idx)) { pc -= GETARG_Bx(i); /* jump back */ - chgfltvalue(s2v(ra), idx); /* update internal index... */ + chgfltvalue(vra, idx); /* update internal index... */ setfltvalue(s2v(ra + 3), idx); /* ...and external index */ } } @@ -1509,7 +1487,7 @@ void luaV_execute (lua_State *L) { vmbreak; } vmcase(OP_FORPREP) { - TValue *init = s2v(ra); + TValue *init = vra; TValue *plimit = s2v(ra + 1); TValue *pstep = s2v(ra + 2); lua_Integer ilimit; @@ -1569,7 +1547,7 @@ void luaV_execute (lua_State *L) { if (c == 0) { c = GETARG_Ax(*pc); pc++; } - h = hvalue(s2v(ra)); + h = hvalue(vra); last = ((c-1)*LFIELDS_PER_FLUSH) + n; savepc(L); /* in case of allocation errors */ if (last > h->sizearray) /* needs more space? */ From 36aecd4548e5cafc3b6b7ab9f6045db866cf7d9a Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 28 Nov 2017 13:26:15 -0200 Subject: [PATCH 0133/1145] order opcodes cannot use 'K' operands --- lcode.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lcode.c b/lcode.c index 04d7b9e032..97aa9dda20 100644 --- a/lcode.c +++ b/lcode.c @@ -1,5 +1,5 @@ /* -** $Id: lcode.c,v 2.136 2017/11/23 19:29:04 roberto Exp roberto $ +** $Id: lcode.c,v 2.137 2017/11/28 12:58:18 roberto Exp roberto $ ** Code generator for Lua ** See Copyright Notice in lua.h */ @@ -1399,7 +1399,7 @@ void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) { case OPR_GT: case OPR_GE: { lua_Integer dummy; if (!isSCnumber(v, &dummy)) - luaK_exp2RK(fs, v); + luaK_exp2anyreg(fs, v); /* else keep numeral, which may be an immediate operand */ break; } From c766e4103db888063c4f928747afd6eb448e306f Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 29 Nov 2017 11:02:17 -0200 Subject: [PATCH 0134/1145] 'luaV_execute' gets call info as extra argument (it is always available on call sites) --- ldo.c | 13 +++++++------ lvm.c | 5 ++--- lvm.h | 4 ++-- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/ldo.c b/ldo.c index 37688a877e..628aef1241 100644 --- a/ldo.c +++ b/ldo.c @@ -1,5 +1,5 @@ /* -** $Id: ldo.c,v 2.174 2017/11/23 16:35:54 roberto Exp roberto $ +** $Id: ldo.c,v 2.175 2017/11/23 18:29:41 roberto Exp roberto $ ** Stack and Call structure of Lua ** See Copyright Notice in lua.h */ @@ -468,7 +468,7 @@ void luaD_call (lua_State *L, StkId func, int nresults) { ci->callstatus = CIST_LUA; if (L->hookmask) callhook(L, ci, 0); - luaV_execute(L); /* run the function */ + luaV_execute(L, ci); /* run the function */ break; } default: { /* not a function */ @@ -525,14 +525,15 @@ static void finishCcall (lua_State *L, int status) { ** status is LUA_YIELD). */ static void unroll (lua_State *L, void *ud) { + CallInfo *ci; if (ud != NULL) /* error status? */ finishCcall(L, *(int *)ud); /* finish 'lua_pcallk' callee */ - while (L->ci != &L->base_ci) { /* something in the stack */ - if (!isLua(L->ci)) /* C function? */ + while ((ci = L->ci) != &L->base_ci) { /* something in the stack */ + if (!isLua(ci)) /* C function? */ finishCcall(L, LUA_YIELD); /* complete its execution */ else { /* Lua function */ luaV_finishOp(L); /* finish interrupted instruction */ - luaV_execute(L); /* execute down to higher C 'boundary' */ + luaV_execute(L, ci); /* execute down to higher C 'boundary' */ } } } @@ -606,7 +607,7 @@ static void resume (lua_State *L, void *ud) { lua_assert(L->status == LUA_YIELD); L->status = LUA_OK; /* mark that it is running (again) */ if (isLua(ci)) /* yielded inside a hook? */ - luaV_execute(L); /* just continue running Lua code */ + luaV_execute(L, ci); /* just continue running Lua code */ else { /* 'common' yield */ if (ci->u.c.k != NULL) { /* does it have a continuation function? */ lua_unlock(L); diff --git a/lvm.c b/lvm.c index 63e65675f6..05a4646eeb 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.319 2017/11/28 12:58:18 roberto Exp roberto $ +** $Id: lvm.c,v 2.320 2017/11/28 14:51:00 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -823,8 +823,7 @@ void luaV_finishOp (lua_State *L) { #define vmbreak break -void luaV_execute (lua_State *L) { - CallInfo *ci = L->ci; +void luaV_execute (lua_State *L, CallInfo *ci) { LClosure *cl = clLvalue(s2v(ci->func)); TValue *k = cl->p->k; StkId base = ci->func + 1; diff --git a/lvm.h b/lvm.h index c2e2557420..0d2eed14f4 100644 --- a/lvm.h +++ b/lvm.h @@ -1,5 +1,5 @@ /* -** $Id: lvm.h,v 2.46 2017/07/07 16:34:32 roberto Exp roberto $ +** $Id: lvm.h,v 2.47 2017/11/08 14:50:23 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -112,7 +112,7 @@ LUAI_FUNC void luaV_finishget (lua_State *L, const TValue *t, TValue *key, LUAI_FUNC void luaV_finishset (lua_State *L, const TValue *t, TValue *key, TValue *val, const TValue *slot); LUAI_FUNC void luaV_finishOp (lua_State *L); -LUAI_FUNC void luaV_execute (lua_State *L); +LUAI_FUNC void luaV_execute (lua_State *L, CallInfo *ci); LUAI_FUNC void luaV_concat (lua_State *L, int total); LUAI_FUNC lua_Integer luaV_div (lua_State *L, lua_Integer x, lua_Integer y); LUAI_FUNC lua_Integer luaV_mod (lua_State *L, lua_Integer x, lua_Integer y); From 745eb4199333f153a28c75837b6a1addf614f4a9 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 29 Nov 2017 14:57:36 -0200 Subject: [PATCH 0135/1145] new opcodes OP_RETURN0/OP_RETURN1 --- lcode.c | 14 +++++++++-- lopcodes.c | 6 ++++- lopcodes.h | 5 +++- lvm.c | 69 ++++++++++++++++++++++++++++++++++++++++++++---------- 4 files changed, 77 insertions(+), 17 deletions(-) diff --git a/lcode.c b/lcode.c index 97aa9dda20..8f9fa40823 100644 --- a/lcode.c +++ b/lcode.c @@ -1,5 +1,5 @@ /* -** $Id: lcode.c,v 2.137 2017/11/28 12:58:18 roberto Exp roberto $ +** $Id: lcode.c,v 2.138 2017/11/28 15:26:15 roberto Exp roberto $ ** Code generator for Lua ** See Copyright Notice in lua.h */ @@ -152,7 +152,17 @@ int luaK_jump (FuncState *fs) { ** Code a 'return' instruction */ void luaK_ret (FuncState *fs, int first, int nret) { - luaK_codeABC(fs, OP_RETURN, first, nret+1, 0); + switch (nret) { + case 0: + luaK_codeABC(fs, OP_RETURN0, 0, 0, 0); + break; + case 1: + luaK_codeABC(fs, OP_RETURN1, first, 0, 0); + break; + default: + luaK_codeABC(fs, OP_RETURN, first, nret + 1, 0); + break; + } } diff --git a/lopcodes.c b/lopcodes.c index 122157eb41..1b081639ab 100644 --- a/lopcodes.c +++ b/lopcodes.c @@ -1,5 +1,5 @@ /* -** $Id: lopcodes.c,v 1.69 2017/11/22 18:41:20 roberto Exp roberto $ +** $Id: lopcodes.c,v 1.70 2017/11/27 17:44:31 roberto Exp roberto $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ @@ -75,6 +75,8 @@ LUAI_DDEF const char *const luaP_opnames[NUM_OPCODES+1] = { "CALL", "TAILCALL", "RETURN", + "RETURN0", + "RETURN1", "FORLOOP", "FORPREP", "TFORCALL", @@ -146,6 +148,8 @@ LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = { ,opmode(0, 1, iABC) /* OP_CALL */ ,opmode(0, 1, iABC) /* OP_TAILCALL */ ,opmode(0, 0, iABC) /* OP_RETURN */ + ,opmode(0, 0, iABC) /* OP_RETURN0 */ + ,opmode(0, 0, iABC) /* OP_RETURN1 */ ,opmode(0, 1, iABx) /* OP_FORLOOP */ ,opmode(0, 1, iABx) /* OP_FORPREP */ ,opmode(0, 0, iABC) /* OP_TFORCALL */ diff --git a/lopcodes.h b/lopcodes.h index f2b6716566..433d97a27d 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -1,5 +1,5 @@ /* -** $Id: lopcodes.h,v 1.171 2017/11/27 17:44:31 roberto Exp roberto $ +** $Id: lopcodes.h,v 1.172 2017/11/28 12:58:18 roberto Exp roberto $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ @@ -252,7 +252,10 @@ OP_TESTSET,/* A B if (not R(B) == k) then R(A) := R(B) else pc++ */ OP_CALL,/* A B C R(A), ... ,R(A+C-2) := R(A)(R(A+1), ... ,R(A+B-1)) */ OP_TAILCALL,/* A B C return R(A)(R(A+1), ... ,R(A+B-1)) */ + OP_RETURN,/* A B return R(A), ... ,R(A+B-2) (see note) */ +OP_RETURN0,/* return */ +OP_RETURN1,/* A return R(A) */ OP_FORLOOP,/* A Bx R(A)+=R(A+2); if R(A) top = (c), /* limit of live values */ \ @@ -824,11 +829,16 @@ void luaV_finishOp (lua_State *L) { void luaV_execute (lua_State *L, CallInfo *ci) { - LClosure *cl = clLvalue(s2v(ci->func)); - TValue *k = cl->p->k; - StkId base = ci->func + 1; + LClosure *cl; + TValue *k; + StkId base; + const Instruction *pc; int trap = ci->u.l.trap; - const Instruction *pc = ci->u.l.savedpc; + tailcall: + cl = clLvalue(s2v(ci->func)); + k = cl->p->k; + base = ci->func + 1; + pc = ci->u.l.savedpc; /* main loop of interpreter */ for (;;) { int cond; /* flag for conditional jumps */ @@ -1438,16 +1448,15 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vra = s2v(ra); b++; /* there is now one extra argument */ } - if (!ttisLclosure(vra)) /* C function? */ + if (!ttisLclosure(vra)) { /* C function? */ Protect(luaD_call(L, ra, LUA_MULTRET)); /* call it */ + /* next instruction will do the return */ + } else { /* tail call */ if (cl->p->sizep > 0) /* close upvalues from previous call */ luaF_close(L, ci->func + 1); luaD_pretailcall(L, ci, ra, b); /* prepare call frame */ - cl = clLvalue(s2v(ci->func)); - k = cl->p->k; - updatebase(ci); - pc = ci->u.l.savedpc; + goto tailcall; } vmbreak; } @@ -1455,9 +1464,43 @@ void luaV_execute (lua_State *L, CallInfo *ci) { int b = GETARG_B(i); if (cl->p->sizep > 0) luaF_close(L, base); - savepc(L); - luaD_poscall(L, ci, ra, (b != 0 ? b - 1 : cast_int(L->top - ra))); - return; /* external invocation: return */ + halfProtect( + luaD_poscall(L, ci, ra, (b != 0 ? b - 1 : cast_int(L->top - ra))) + ); + return; + } + vmcase(OP_RETURN0) { + if (cl->p->sizep > 0) + luaF_close(L, base); + if (L->hookmask) + halfProtect(luaD_poscall(L, ci, ra, 0)); /* no hurry... */ + else { + int nres = ci->nresults; + L->ci = ci->previous; /* back to caller */ + L->top = base - 1; + while (nres-- > 0) + setnilvalue(s2v(L->top++)); /* all results are nil */ + } + return; + } + vmcase(OP_RETURN1) { + if (cl->p->sizep > 0) + luaF_close(L, base); + if (L->hookmask) + halfProtect(luaD_poscall(L, ci, ra, 1)); /* no hurry... */ + else { + int nres = ci->nresults; + L->ci = ci->previous; /* back to caller */ + if (nres == 0) + L->top = base - 1; /* asked for no results */ + else { + setobjs2s(L, base - 1, ra); /* at least this result */ + L->top = base; + while (--nres > 0) /* complete missing results */ + setnilvalue(s2v(L->top++)); + } + } + return; } vmcase(OP_FORLOOP) { if (ttisinteger(vra)) { /* integer loop? */ From 19c6b375c39e18441d1fa560d08688d66f746717 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 30 Nov 2017 10:03:00 -0200 Subject: [PATCH 0136/1145] detail (spacing) --- lopcodes.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lopcodes.h b/lopcodes.h index 433d97a27d..690c4c34b3 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -1,5 +1,5 @@ /* -** $Id: lopcodes.h,v 1.172 2017/11/28 12:58:18 roberto Exp roberto $ +** $Id: lopcodes.h,v 1.173 2017/11/29 16:57:36 roberto Exp roberto $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ @@ -258,11 +258,11 @@ OP_RETURN0,/* return */ OP_RETURN1,/* A return R(A) */ OP_FORLOOP,/* A Bx R(A)+=R(A+2); - if R(A) Date: Thu, 30 Nov 2017 11:16:43 -0200 Subject: [PATCH 0137/1145] details (comments) --- lopcodes.h | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/lopcodes.h b/lopcodes.h index 690c4c34b3..0d697c552a 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -1,5 +1,5 @@ /* -** $Id: lopcodes.h,v 1.173 2017/11/29 16:57:36 roberto Exp roberto $ +** $Id: lopcodes.h,v 1.174 2017/11/30 12:03:00 roberto Exp roberto $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ @@ -289,8 +289,8 @@ OP_EXTRAARG/* Ax extra (larger) argument for previous opcode */ (*) In OP_RETURN, if (B == 0) then return up to 'top'. - (*) In OP_SETLIST, if (B == 0) then B = 'top'; if (C == 0) then next - 'instruction' is EXTRAARG(real C). + (*) In OP_SETLIST, if (B == 0) then real B = 'top'; if (C == 0) then + next 'instruction' is EXTRAARG(real C). (*) In OP_LOADKX, the next 'instruction' is always EXTRAARG. @@ -298,11 +298,16 @@ OP_EXTRAARG/* Ax extra (larger) argument for previous opcode */ (true or false). (*) For OP_LTI/OP_LEI, C indicates that the transformations - (A (!(B<=A)) or (A<=B) => (!(B (!(B<=A)) or (A<=B) => (!(B Date: Thu, 30 Nov 2017 11:29:18 -0200 Subject: [PATCH 0138/1145] small peephole optimizations --- lcode.c | 91 ++++++++++++++++++++++++++++++++----------------------- lcode.h | 3 +- lparser.c | 4 +-- lparser.h | 3 +- lvm.c | 10 +++--- 5 files changed, 63 insertions(+), 48 deletions(-) diff --git a/lcode.c b/lcode.c index 8f9fa40823..d158519376 100644 --- a/lcode.c +++ b/lcode.c @@ -1,5 +1,5 @@ /* -** $Id: lcode.c,v 2.138 2017/11/28 15:26:15 roberto Exp roberto $ +** $Id: lcode.c,v 2.140 2017/11/30 13:17:06 roberto Exp roberto $ ** Code generator for Lua ** See Copyright Notice in lua.h */ @@ -134,17 +134,10 @@ void luaK_concat (FuncState *fs, int *l1, int l2) { /* ** Create a jump instruction and return its position, so its destination -** can be fixed later (with 'fixjump'). If there are jumps to -** this position (kept in 'jpc'), link them all together so that -** 'patchlistaux' will fix all them directly to the final destination. +** can be fixed later (with 'fixjump'). */ int luaK_jump (FuncState *fs) { - int jpc = fs->jpc; /* save list of jumps to here */ - int j; - fs->jpc = NO_JUMP; /* no more jumps to here */ - j = codesJ(fs, OP_JMP, NO_JUMP, 0); - luaK_concat(fs, &j, jpc); /* keep them on hold */ - return j; + return codesJ(fs, OP_JMP, NO_JUMP, 0); } @@ -249,39 +242,20 @@ static void patchlistaux (FuncState *fs, int list, int vtarget, int reg, } -/* -** Ensure all pending jumps to current position are fixed (jumping -** to current position with no values) and reset list of pending -** jumps -*/ -static void dischargejpc (FuncState *fs) { - patchlistaux(fs, fs->jpc, fs->pc, NO_REG, fs->pc); - fs->jpc = NO_JUMP; -} - - -/* -** Add elements in 'list' to list of pending jumps to "here" -** (current position) -*/ -void luaK_patchtohere (FuncState *fs, int list) { - luaK_getlabel(fs); /* mark "here" as a jump target */ - luaK_concat(fs, &fs->jpc, list); -} - - /* ** Path all jumps in 'list' to jump to 'target'. ** (The assert means that we cannot fix a jump to a forward address ** because we only know addresses once code is generated.) */ void luaK_patchlist (FuncState *fs, int list, int target) { - if (target == fs->pc) /* 'target' is current position? */ - luaK_patchtohere(fs, list); /* add list to pending jumps */ - else { - lua_assert(target < fs->pc); - patchlistaux(fs, list, target, NO_REG, target); - } + lua_assert(target <= fs->pc); + patchlistaux(fs, list, target, NO_REG, target); +} + + +void luaK_patchtohere (FuncState *fs, int list) { + int hr = luaK_getlabel(fs); /* mark "here" as a jump target */ + luaK_patchlist(fs, list, hr); } @@ -365,7 +339,6 @@ static void savelineinfo (FuncState *fs, Proto *f, int pc, int line) { */ static int luaK_code (FuncState *fs, Instruction i) { Proto *f = fs->f; - dischargejpc(fs); /* 'pc' will change */ /* put new instruction in code array */ luaM_growvector(fs->ls->L, f->code, fs->pc, f->sizecode, Instruction, MAX_INT, "opcodes"); @@ -1537,3 +1510,45 @@ void luaK_setlist (FuncState *fs, int base, int nelems, int tostore) { fs->freereg = base + 1; /* free registers with list values */ } + +/* +** return the final target of a jump (skipping jumps to jumps) +*/ +static int finaltarget (Instruction *code, int i) { + int count; + for (count = 0; count < 100; count++) { /* avoid infinite loops */ + Instruction pc = code[i]; + if (GET_OPCODE(pc) != OP_JMP) + break; + else + i += GETARG_sJ(pc) + 1; + } + return i; +} + + +/* +** Do a final pass over the code of a function, doing small peephole +** optimizations and adjustments. +*/ +void luaK_finish (FuncState *fs) { + int i; + Proto *p = fs->f; + for (i = 0; i < fs->pc; i++) { + Instruction *pc = &p->code[i]; + switch (GET_OPCODE(*pc)) { + case OP_RETURN: case OP_RETURN0: case OP_RETURN1: + case OP_TAILCALL: { + if (p->sizep > 0) + SETARG_k(*pc, 1); /* signal that they must close upvalues */ + break; + } + case OP_JMP: { + int target = finaltarget(p->code, i); + fixjump(fs, i, target); + break; + } + default: break; + } + } +} diff --git a/lcode.h b/lcode.h index 1c3229d982..a1fd735dfc 100644 --- a/lcode.h +++ b/lcode.h @@ -1,5 +1,5 @@ /* -** $Id: lcode.h,v 1.67 2017/09/28 16:53:29 roberto Exp roberto $ +** $Id: lcode.h,v 1.68 2017/10/04 21:56:32 roberto Exp roberto $ ** Code generator for Lua ** See Copyright Notice in lua.h */ @@ -88,6 +88,7 @@ LUAI_FUNC void luaK_infix (FuncState *fs, BinOpr op, expdesc *v); LUAI_FUNC void luaK_posfix (FuncState *fs, BinOpr op, expdesc *v1, expdesc *v2, int line); LUAI_FUNC void luaK_setlist (FuncState *fs, int base, int nelems, int tostore); +LUAI_FUNC void luaK_finish (FuncState *fs); #endif diff --git a/lparser.c b/lparser.c index 3eb83fba01..1d46c6f29a 100644 --- a/lparser.c +++ b/lparser.c @@ -1,5 +1,5 @@ /* -** $Id: lparser.c,v 2.167 2017/10/04 21:53:03 roberto Exp roberto $ +** $Id: lparser.c,v 2.168 2017/11/23 16:35:54 roberto Exp roberto $ ** Lua Parser ** See Copyright Notice in lua.h */ @@ -547,7 +547,6 @@ static void open_func (LexState *ls, FuncState *fs, BlockCnt *bl) { fs->previousline = f->linedefined; fs->iwthabs = 0; fs->lasttarget = 0; - fs->jpc = NO_JUMP; fs->freereg = 0; fs->nk = 0; fs->nabslineinfo = 0; @@ -569,6 +568,7 @@ static void close_func (LexState *ls) { Proto *f = fs->f; luaK_ret(fs, 0, 0); /* final return */ leaveblock(fs); + luaK_finish(fs); luaM_reallocvector(L, f->code, f->sizecode, fs->pc, Instruction); f->sizecode = fs->pc; luaM_reallocvector(L, f->lineinfo, f->sizelineinfo, fs->pc, ls_byte); diff --git a/lparser.h b/lparser.h index 3ab6065daf..4d342d325c 100644 --- a/lparser.h +++ b/lparser.h @@ -1,5 +1,5 @@ /* -** $Id: lparser.h,v 1.77 2017/04/28 20:57:45 roberto Exp roberto $ +** $Id: lparser.h,v 1.78 2017/06/27 11:35:31 roberto Exp roberto $ ** Lua Parser ** See Copyright Notice in lua.h */ @@ -124,7 +124,6 @@ typedef struct FuncState { int pc; /* next position to code (equivalent to 'ncode') */ int lasttarget; /* 'label' of last 'jump label' */ int previousline; /* last line that was saved in 'lineinfo' */ - int jpc; /* list of pending jumps to 'pc' */ int nk; /* number of elements in 'k' */ int np; /* number of elements in 'p' */ int nabslineinfo; /* number of elements in 'abslineinfo' */ diff --git a/lvm.c b/lvm.c index 16e4fde02a..0ba78b3dd4 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.321 2017/11/29 13:02:17 roberto Exp roberto $ +** $Id: lvm.c,v 2.322 2017/11/29 16:57:36 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -1453,7 +1453,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { /* next instruction will do the return */ } else { /* tail call */ - if (cl->p->sizep > 0) /* close upvalues from previous call */ + if (TESTARG_k(i)) /* close upvalues from previous call */ luaF_close(L, ci->func + 1); luaD_pretailcall(L, ci, ra, b); /* prepare call frame */ goto tailcall; @@ -1462,7 +1462,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { } vmcase(OP_RETURN) { int b = GETARG_B(i); - if (cl->p->sizep > 0) + if (TESTARG_k(i)) luaF_close(L, base); halfProtect( luaD_poscall(L, ci, ra, (b != 0 ? b - 1 : cast_int(L->top - ra))) @@ -1470,7 +1470,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { return; } vmcase(OP_RETURN0) { - if (cl->p->sizep > 0) + if (TESTARG_k(i)) luaF_close(L, base); if (L->hookmask) halfProtect(luaD_poscall(L, ci, ra, 0)); /* no hurry... */ @@ -1484,7 +1484,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { return; } vmcase(OP_RETURN1) { - if (cl->p->sizep > 0) + if (TESTARG_k(i)) luaF_close(L, base); if (L->hookmask) halfProtect(luaD_poscall(L, ci, ra, 1)); /* no hurry... */ From ac78b914b655dd8e89d306f6336a229452046ead Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 30 Nov 2017 13:37:16 -0200 Subject: [PATCH 0139/1145] warnings from Visual Studio /W3 --- lcode.c | 4 ++-- lgc.c | 4 ++-- ltm.c | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lcode.c b/lcode.c index d158519376..56fd8522eb 100644 --- a/lcode.c +++ b/lcode.c @@ -1,5 +1,5 @@ /* -** $Id: lcode.c,v 2.140 2017/11/30 13:17:06 roberto Exp roberto $ +** $Id: lcode.c,v 2.140 2017/11/30 13:29:18 roberto Exp roberto $ ** Code generator for Lua ** See Copyright Notice in lua.h */ @@ -1128,7 +1128,7 @@ void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) { t->k = VINDEXSTR; } else if (isCint(k)) { - t->u.ind.idx = k->u.ival; /* integer constant */ + t->u.ind.idx = cast_int(k->u.ival); /* integer constant in proper range */ t->k = VINDEXI; } else { diff --git a/lgc.c b/lgc.c index 389d69bb7b..f4b8d6a45a 100644 --- a/lgc.c +++ b/lgc.c @@ -1,5 +1,5 @@ /* -** $Id: lgc.c,v 2.238 2017/11/07 13:25:26 roberto Exp roberto $ +** $Id: lgc.c,v 2.239 2017/11/23 19:29:04 roberto Exp roberto $ ** Garbage Collector ** See Copyright Notice in lua.h */ @@ -1270,7 +1270,7 @@ static void genstep (lua_State *L, global_State *g) { lu_mem mem; youngcollection(L, g); mem = gettotalbytes(g); - luaE_setdebt(g, -((mem / 100) * g->genminormul)); + luaE_setdebt(g, -(cast(l_mem, (mem / 100)) * g->genminormul)); g->GCestimate = majorbase; /* preserve base value */ } } diff --git a/ltm.c b/ltm.c index 5ee123f564..fe87553d05 100644 --- a/ltm.c +++ b/ltm.c @@ -1,5 +1,5 @@ /* -** $Id: ltm.c,v 2.49 2017/11/23 19:18:10 roberto Exp roberto $ +** $Id: ltm.c,v 2.50 2017/11/27 17:44:31 roberto Exp roberto $ ** Tag methods ** See Copyright Notice in lua.h */ @@ -237,7 +237,7 @@ void luaT_getvarargs (lua_State *L, TValue *t, StkId where, int wanted) { Table *h = hvalue(t); if (wanted < 0) { /* get all? */ const TValue *ns = luaH_getstr(h, G(L)->nfield); - int n = (ttisinteger(ns)) ? ivalue(ns) : 0; + int n = (ttisinteger(ns)) ? cast_int(ivalue(ns)) : 0; wanted = n; checkstackp(L, n, where); L->top = where + n; From d0356d5f15aea1f9934bd9e223bcb12a865a8608 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 1 Dec 2017 13:08:14 -0200 Subject: [PATCH 0140/1145] another value for LUAI_MAXCCALLS (must think more about that) --- llimits.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/llimits.h b/llimits.h index 4e641008a6..5834614098 100644 --- a/llimits.h +++ b/llimits.h @@ -1,5 +1,5 @@ /* -** $Id: llimits.h,v 1.144 2017/06/27 11:35:01 roberto Exp roberto $ +** $Id: llimits.h,v 1.145 2017/11/23 16:35:54 roberto Exp roberto $ ** Limits, basic types, and some other 'installation-dependent' definitions ** See Copyright Notice in lua.h */ @@ -144,7 +144,7 @@ typedef LUAI_UACINT l_uacInt; ** be compatible with the size of the C stack.) */ #if !defined(LUAI_MAXCCALLS) -#define LUAI_MAXCCALLS 1000 +#define LUAI_MAXCCALLS 2200 #endif From e0bece77d6ee926de4ab3108b0fa84f03ff759d4 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 1 Dec 2017 13:44:51 -0200 Subject: [PATCH 0141/1145] detail --- ldo.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ldo.c b/ldo.c index 628aef1241..fe1fcb259a 100644 --- a/ldo.c +++ b/ldo.c @@ -1,5 +1,5 @@ /* -** $Id: ldo.c,v 2.175 2017/11/23 18:29:41 roberto Exp roberto $ +** $Id: ldo.c,v 2.176 2017/11/29 13:02:17 roberto Exp roberto $ ** Stack and Call structure of Lua ** See Copyright Notice in lua.h */ @@ -346,8 +346,8 @@ static void moveresults (lua_State *L, StkId firstResult, StkId res, int i; for (i = 0; i < nres; i++) /* move all results to correct place */ setobjs2s(L, res + i, firstResult + i); - L->top = res + nres; - return; + wanted = nres; /* it wanted what it had */ + break; } default: { int i; From 9d28b401527ccb41f544d431abe52da69e8beb3c Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 1 Dec 2017 14:40:29 -0200 Subject: [PATCH 0142/1145] rehashes string table always allocating a new array instead of reallocating old one. (Avoids problems if reallocation to a small size fails.) --- lstring.c | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/lstring.c b/lstring.c index a851fd7415..e8f6f5c9b5 100644 --- a/lstring.c +++ b/lstring.c @@ -1,5 +1,5 @@ /* -** $Id: lstring.c,v 2.56 2015/11/23 11:32:51 roberto Exp roberto $ +** $Id: lstring.c,v 2.57 2017/07/27 13:50:16 roberto Exp roberto $ ** String table (keeps all strings handled by Lua) ** See Copyright Notice in lua.h */ @@ -63,32 +63,26 @@ unsigned int luaS_hashlongstr (TString *ts) { /* -** resizes the string table +** Resizes the string table. */ void luaS_resize (lua_State *L, int newsize) { int i; + TString **newhash = luaM_newvector(L, newsize, TString *); stringtable *tb = &G(L)->strt; - if (newsize > tb->size) { /* grow table if needed */ - luaM_reallocvector(L, tb->hash, tb->size, newsize, TString *); - for (i = tb->size; i < newsize; i++) - tb->hash[i] = NULL; - } - for (i = 0; i < tb->size; i++) { /* rehash */ + for (i = 0; i < newsize; i++) /* initialize new hash array */ + newhash[i] = NULL; + for (i = 0; i < tb->size; i++) { /* rehash all elements into new array */ TString *p = tb->hash[i]; - tb->hash[i] = NULL; - while (p) { /* for each node in the list */ + while (p) { /* for each string in the list */ TString *hnext = p->u.hnext; /* save next */ unsigned int h = lmod(p->hash, newsize); /* new position */ - p->u.hnext = tb->hash[h]; /* chain it */ - tb->hash[h] = p; + p->u.hnext = newhash[h]; /* chain it into new array */ + newhash[h] = p; p = hnext; } } - if (newsize < tb->size) { /* shrink table if needed */ - /* vanishing slice should be empty */ - lua_assert(tb->hash[newsize] == NULL && tb->hash[tb->size - 1] == NULL); - luaM_reallocvector(L, tb->hash, tb->size, newsize, TString *); - } + luaM_freearray(L, tb->hash, tb->size); /* free old array */ + tb->hash = newhash; tb->size = newsize; } From 421e459684f7fdf483454d99c048b47783cb0611 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 1 Dec 2017 15:38:49 -0200 Subject: [PATCH 0143/1145] 'luaS_resize' can raise memory errors --- lgc.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/lgc.c b/lgc.c index f4b8d6a45a..f893e122d5 100644 --- a/lgc.c +++ b/lgc.c @@ -1,5 +1,5 @@ /* -** $Id: lgc.c,v 2.239 2017/11/23 19:29:04 roberto Exp roberto $ +** $Id: lgc.c,v 2.240 2017/11/30 15:37:16 roberto Exp roberto $ ** Garbage Collector ** See Copyright Notice in lua.h */ @@ -816,14 +816,19 @@ static GCObject **sweeptolive (lua_State *L, GCObject **p) { */ /* -** If possible, shrink string table +** If possible, shrink string table (protected from memory errors). */ +static void shrinkstrtable (lua_State *L, void *ud) { + luaS_resize(L, *cast(int*, ud) / 2); +} + + static void checkSizes (lua_State *L, global_State *g) { if (!g->gcemergency) { l_mem olddebt = g->GCdebt; if (g->strt.nuse < g->strt.size / 4) /* string table too big? */ - luaS_resize(L, g->strt.size / 2); /* shrink it a little */ - g->GCestimate += g->GCdebt - olddebt; /* update estimate */ + luaD_rawrunprotected(L, &shrinkstrtable, &g->strt.size); + g->GCestimate += g->GCdebt - olddebt; /* correct estimate */ } } From c7ee7fe026aa2247377bdd7915089a8e0074de1a Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 4 Dec 2017 15:41:30 -0200 Subject: [PATCH 0144/1145] new opcodes OP_SHLI/OP_SHRI --- lcode.c | 69 +++++++++++++++++++++++++++++++++++++++++++----------- lopcodes.c | 6 ++++- lopcodes.h | 5 +++- lvm.c | 32 +++++++++++++++++++++++-- 4 files changed, 94 insertions(+), 18 deletions(-) diff --git a/lcode.c b/lcode.c index 56fd8522eb..a09731a7a7 100644 --- a/lcode.c +++ b/lcode.c @@ -1,5 +1,5 @@ /* -** $Id: lcode.c,v 2.140 2017/11/30 13:29:18 roberto Exp roberto $ +** $Id: lcode.c,v 2.141 2017/11/30 15:37:16 roberto Exp roberto $ ** Code generator for Lua ** See Copyright Notice in lua.h */ @@ -1216,6 +1216,20 @@ static void codebinexpval (FuncState *fs, OpCode op, } +/* +** Code binary operators ('+', '-', ...) with immediate operands. +*/ +static void codebini (FuncState *fs, OpCode op, + expdesc *e1, expdesc *e2, int k, int line) { + int v2 = cast_int(e2->u.ival); /* immediate operand */ + int v1 = luaK_exp2anyreg(fs, e1); + freeexp(fs, e1); + e1->u.info = codeABsC(fs, op, 0, v1, v2, k); /* generate opcode */ + e1->k = VRELOCABLE; /* all those operations are relocatable */ + luaK_fixline(fs, line); +} + + /* ** Code arithmetic operators ('+', '-', ...). If second operand is a ** constant in the proper range, use variant opcodes with immediate @@ -1225,15 +1239,8 @@ static void codearith (FuncState *fs, OpCode op, expdesc *e1, expdesc *e2, int flip, int line) { if (!isSCint(e2)) codebinexpval(fs, op, e1, e2, line); /* use standard operators */ - else { /* use immediate operators */ - int v2 = cast_int(e2->u.ival); /* immediate operand */ - int v1 = luaK_exp2anyreg(fs, e1); - op = cast(OpCode, op - OP_ADD + OP_ADDI); - freeexp(fs, e1); - e1->u.info = codeABsC(fs, op, 0, v1, v2, flip); /* generate opcode */ - e1->k = VRELOCABLE; /* all those operations are relocatable */ - luaK_fixline(fs, line); - } + else /* use immediate operators */ + codebini(fs, cast(OpCode, op - OP_ADD + OP_ADDI), e1, e2, flip, line); } @@ -1257,12 +1264,31 @@ static void codecommutative (FuncState *fs, OpCode op, } +/* +** Code shift operators. If second operand is constant, use immediate +** operand (negating it if shift is in the other direction). +*/ +static void codeshift (FuncState *fs, OpCode op, + expdesc *e1, expdesc *e2, int line) { + if (isSCint(e2)) { + int changedir = 0; + if (op == OP_SHL) { + changedir = 1; + e2->u.ival = -(e2->u.ival); + } + codebini(fs, OP_SHRI, e1, e2, changedir, line); + } + else + codebinexpval(fs, op, e1, e2, line); +} + + /* ** Emit code for order comparisons. ** When the first operand is an integral value in the proper range, ** change (A < B) to (!(B <= A)) and (A <= B) to (!(B < A)) so that ** it can use an immediate operand. In this case, C indicates this -** change, for cases that cannot assume a total order (NaN and +** change, for cases that cannot assume a total order (NaN and ** metamethods). */ static void codeorder (FuncState *fs, OpCode op, expdesc *e1, expdesc *e2) { @@ -1440,12 +1466,27 @@ void luaK_posfix (FuncState *fs, BinOpr opr, codearith(fs, cast(OpCode, opr + OP_ADD), e1, e2, 0, line); break; } - case OPR_BAND: case OPR_BOR: case OPR_BXOR: - case OPR_SHL: case OPR_SHR: { + case OPR_BAND: case OPR_BOR: case OPR_BXOR: { if (!constfolding(fs, opr + LUA_OPADD, e1, e2)) codebinexpval(fs, cast(OpCode, opr + OP_ADD), e1, e2, line); break; } + case OPR_SHL: { + if (!constfolding(fs, LUA_OPSHL, e1, e2)) { + if (isSCint(e1)) { + swapexps(e1, e2); + codebini(fs, OP_SHLI, e1, e2, 1, line); + } + else + codeshift(fs, OP_SHL, e1, e2, line); + } + break; + } + case OPR_SHR: { + if (!constfolding(fs, LUA_OPSHR, e1, e2)) + codeshift(fs, OP_SHR, e1, e2, line); + break; + } case OPR_EQ: case OPR_NE: { codeeq(fs, opr, e1, e2); break; @@ -1483,7 +1524,7 @@ void luaK_fixline (FuncState *fs, int line) { } else { fs->previousline -= f->lineinfo[fs->pc - 1]; /* undo previous info. */ - savelineinfo(fs, f, fs->pc - 1, line); /* redo it */ + savelineinfo(fs, f, fs->pc - 1, line); /* redo it */ } } diff --git a/lopcodes.c b/lopcodes.c index 1b081639ab..55b6050daf 100644 --- a/lopcodes.c +++ b/lopcodes.c @@ -1,5 +1,5 @@ /* -** $Id: lopcodes.c,v 1.70 2017/11/27 17:44:31 roberto Exp roberto $ +** $Id: lopcodes.c,v 1.71 2017/11/29 16:57:36 roberto Exp roberto $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ @@ -44,6 +44,8 @@ LUAI_DDEF const char *const luaP_opnames[NUM_OPCODES+1] = { "POWI", "DIVI", "IDIVI", + "SHRI", + "SHLI", "ADD", "SUB", "MUL", @@ -117,6 +119,8 @@ LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = { ,opmode(0, 1, iABC) /* OP_POWI */ ,opmode(0, 1, iABC) /* OP_DIVI */ ,opmode(0, 1, iABC) /* OP_IDIVI */ + ,opmode(0, 1, iABC) /* OP_SHRI */ + ,opmode(0, 1, iABC) /* OP_SHLI */ ,opmode(0, 1, iABC) /* OP_ADD */ ,opmode(0, 1, iABC) /* OP_SUB */ ,opmode(0, 1, iABC) /* OP_MUL */ diff --git a/lopcodes.h b/lopcodes.h index 0d697c552a..7468b9e390 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -1,5 +1,5 @@ /* -** $Id: lopcodes.h,v 1.174 2017/11/30 12:03:00 roberto Exp roberto $ +** $Id: lopcodes.h,v 1.175 2017/11/30 13:16:43 roberto Exp roberto $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ @@ -217,6 +217,9 @@ OP_POWI,/* A B sC R(A) := R(B) ^ C */ OP_DIVI,/* A B sC R(A) := R(B) / C */ OP_IDIVI,/* A B sC R(A) := R(B) // C */ +OP_SHRI,/* A B C R(A) := R(B) >> C */ +OP_SHLI,/* A B C R(A) := C << R(B) */ + OP_ADD,/* A B C R(A) := R(B) + R(C) */ OP_SUB,/* A B C R(A) := R(B) - R(C) */ OP_MUL,/* A B C R(A) := R(B) * R(C) */ diff --git a/lvm.c b/lvm.c index 0ba78b3dd4..9cbba4a98b 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.322 2017/11/29 16:57:36 roberto Exp roberto $ +** $Id: lvm.c,v 2.323 2017/11/30 13:29:18 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -687,7 +687,8 @@ void luaV_finishOp (lua_State *L) { case OP_MODI: case OP_POWI: case OP_ADD: case OP_SUB: case OP_MUL: case OP_DIV: case OP_IDIV: - case OP_BAND: case OP_BOR: case OP_BXOR: case OP_SHL: case OP_SHR: + case OP_BAND: case OP_BOR: case OP_BXOR: + case OP_SHRI: case OP_SHL: case OP_SHR: case OP_MOD: case OP_POW: case OP_UNM: case OP_BNOT: case OP_LEN: case OP_GETTABUP: case OP_GETTABLE: case OP_GETI: @@ -1214,6 +1215,33 @@ void luaV_execute (lua_State *L, CallInfo *ci) { Protect(luaT_trybinTM(L, rb, rc, ra, TM_BXOR)); vmbreak; } + vmcase(OP_SHRI) { + TValue *rb = vRB(i); + int ic = GETARG_sC(i); + lua_Integer ib; + if (tointegerns(rb, &ib)) { + setivalue(vra, luaV_shiftl(ib, -ic)); + } + else { + TMS ev = TM_SHR; + if (TESTARG_k(i)) { + ic = -ic; ev = TM_SHL; + } + Protect(luaT_trybiniTM(L, rb, ic, 0, ra, ev)); + } + vmbreak; + } + vmcase(OP_SHLI) { + TValue *rb = vRB(i); + int ic = GETARG_sC(i); + lua_Integer ib; + if (tointegerns(rb, &ib)) { + setivalue(vra, luaV_shiftl(ic, ib)); + } + else + Protect(luaT_trybiniTM(L, rb, ic, 1, ra, TM_SHL)); + vmbreak; + } vmcase(OP_SHL) { TValue *rb = vRB(i); TValue *rc = vRC(i); From ae11e37e53be81f1d1eb21dde02dd26d6a21c194 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 6 Dec 2017 16:08:03 -0200 Subject: [PATCH 0145/1145] bug: 'lua_pushcclosure' should not call the GC when 'n' is zero --- lapi.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lapi.c b/lapi.c index 181c90d608..3098cb9c36 100644 --- a/lapi.c +++ b/lapi.c @@ -1,5 +1,5 @@ /* -** $Id: lapi.c,v 2.276 2017/11/07 13:25:26 roberto Exp roberto $ +** $Id: lapi.c,v 2.277 2017/11/23 19:29:04 roberto Exp roberto $ ** Lua API ** See Copyright Notice in lua.h */ @@ -550,6 +550,7 @@ LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) { lua_lock(L); if (n == 0) { setfvalue(s2v(L->top), fn); + api_incr_top(L); } else { CClosure *cl; @@ -563,9 +564,9 @@ LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) { /* does not need barrier because closure is white */ } setclCvalue(L, s2v(L->top), cl); + api_incr_top(L); + luaC_checkGC(L); } - api_incr_top(L); - luaC_checkGC(L); lua_unlock(L); } From 348fa1ca56efeb81ef66d8f09c3cd1405b0a121d Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 6 Dec 2017 16:20:28 -0200 Subject: [PATCH 0146/1145] bug: 'lua_pushcclosure' should not call the garbage collector when 'n' is zero. --- bugs | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/bugs b/bugs index f3caeec23d..bd4b313c19 100644 --- a/bugs +++ b/bugs @@ -3680,9 +3680,9 @@ It needs an "interceptor" 'memcmp' function that continues reading memory after a difference is found.]], patch = [[ 2c2 -< ** $Id: bugs,v 1.156 2017/08/12 13:12:42 roberto Exp roberto $ +< ** $Id: bugs,v 1.157 2017/08/31 16:14:41 roberto Exp roberto $ --- -> ** $Id: bugs,v 1.156 2017/08/12 13:12:42 roberto Exp roberto $ +> ** $Id: bugs,v 1.157 2017/08/31 16:14:41 roberto Exp roberto $ 263c263,264 < for (option = LUA_STRFTIMEOPTIONS; *option != '\0'; option += oplen) { --- @@ -3871,6 +3871,39 @@ patch = [[ } +Bug{ +what = [['lua_pushcclosure' should not call the garbage collector when +'n' is zero.]], +report = [[Andrew Gierth, 2017/12/05]], +since = [[5.3.3]], +fix = nil, +example = [[ ]], +patch = [[ +--- lapi.c 2017/04/19 17:13:00 2.259.1.1 ++++ lapi.c 2017/12/06 18:14:45 +@@ -533,6 +533,7 @@ + lua_lock(L); + if (n == 0) { + setfvalue(L->top, fn); ++ api_incr_top(L); + } + else { + CClosure *cl; +@@ -546,9 +547,9 @@ + /* does not need barrier because closure is white */ + } + setclCvalue(L, L->top, cl); ++ api_incr_top(L); ++ luaC_checkGC(L); + } +- api_incr_top(L); +- luaC_checkGC(L); + lua_unlock(L); + } +]] +} + + --[=[ From 49dfaf7447ab707d17fc6061fe1d59c187e4e221 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 6 Dec 2017 16:36:31 -0200 Subject: [PATCH 0147/1145] avoid using one function for different tasks (malloc, free, etc.) --- lmem.c | 77 +++++++++++++++++++++++++++++++++++++++++++++++-------- lmem.h | 53 +++++++++++++++++++++++--------------- lparser.c | 25 +++++++----------- 3 files changed, 108 insertions(+), 47 deletions(-) diff --git a/lmem.c b/lmem.c index 83a9082c5f..ebbfb56a45 100644 --- a/lmem.c +++ b/lmem.c @@ -1,5 +1,5 @@ /* -** $Id: lmem.c,v 1.90 2015/03/03 18:18:29 roberto Exp roberto $ +** $Id: lmem.c,v 1.91 2015/03/06 19:45:54 roberto Exp roberto $ ** Interface to Memory Manager ** See Copyright Notice in lua.h */ @@ -22,6 +22,14 @@ #include "lstate.h" +#if defined(HARDMEMTESTS) +#define hardtest(L,os,s) /* force a GC whenever possible */ \ + if ((s) > (os) && (G(L))->gcrunning) luaC_fullgc(L, 1); +#else +#define hardtest(L,os,s) ((void)0) +#endif + + /* ** About the realloc function: @@ -45,10 +53,12 @@ #define MINSIZEARRAY 4 -void *luaM_growaux_ (lua_State *L, void *block, int *size, size_t size_elems, - int limit, const char *what) { +void *luaM_growaux_ (lua_State *L, void *block, int nelems, int *size, + int size_elems, int limit, const char *what) { void *newblock; int newsize; + if (nelems + 1 <= *size) /* does one extra element still fit? */ + return block; /* nothing to be done */ if (*size >= limit/2) { /* cannot double it? */ if (*size >= limit) /* cannot grow even a little? */ luaG_runerror(L, "too many %s (limit is %d)", what, limit); @@ -65,11 +75,40 @@ void *luaM_growaux_ (lua_State *L, void *block, int *size, size_t size_elems, } +void *luaM_shrinkvector_ (lua_State *L, void *block, int *size, + int final_n, int size_elem) { + global_State *g = G(L); + void *newblock; + size_t oldsize = cast(size_t, (*size) * size_elem); + size_t newsize = cast(size_t, final_n * size_elem); + lua_assert(newsize <= oldsize); + newblock = (*g->frealloc)(g->ud, block, oldsize, newsize); + if (newblock == NULL && final_n > 0) /* allocation failed? */ + return block; /* keep old block */ + else { + g->GCdebt += newsize - oldsize; + *size = final_n; + return newblock; + } +} + + l_noret luaM_toobig (lua_State *L) { luaG_runerror(L, "memory allocation error: block too big"); } +/* +** Free memory +*/ +void luaM_free_ (lua_State *L, void *block, size_t osize) { + global_State *g = G(L); + lua_assert((block == 0) == (block == NULL)); + (*g->frealloc)(g->ud, block, osize, 0); + g->GCdebt -= osize; +} + + /* ** generic allocation routine. @@ -77,15 +116,11 @@ l_noret luaM_toobig (lua_State *L) { void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) { void *newblock; global_State *g = G(L); - size_t realosize = (block) ? osize : 0; - lua_assert((realosize == 0) == (block == NULL)); -#if defined(HARDMEMTESTS) - if (nsize > realosize && g->gcrunning) - luaC_fullgc(L, 1); /* force a GC whenever possible */ -#endif + lua_assert((osize == 0) == (block == NULL)); + hardtest(L, osize, nsize); newblock = (*g->frealloc)(g->ud, block, osize, nsize); if (newblock == NULL && nsize > 0) { - lua_assert(nsize > realosize); /* cannot fail when shrinking a block */ + lua_assert(nsize > osize); /* cannot fail when shrinking a block */ if (g->version) { /* is state fully built? */ luaC_fullgc(L, 1); /* try to free some memory... */ newblock = (*g->frealloc)(g->ud, block, osize, nsize); /* try again */ @@ -94,7 +129,27 @@ void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) { luaD_throw(L, LUA_ERRMEM); } lua_assert((nsize == 0) == (newblock == NULL)); - g->GCdebt = (g->GCdebt + nsize) - realosize; + g->GCdebt = (g->GCdebt + nsize) - osize; return newblock; } + +void *luaM_malloc (lua_State *L, size_t size, int tag) { + hardtest(L, 0, size); + if (size == 0) + return NULL; /* that's all */ + else { + global_State *g = G(L); + void *newblock = (*g->frealloc)(g->ud, NULL, tag, size); + if (newblock == NULL) { + if (g->version) { /* is state fully built? */ + luaC_fullgc(L, 1); /* try to free some memory... */ + newblock = (*g->frealloc)(g->ud, NULL, tag, size); /* try again */ + } + if (newblock == NULL) + luaD_throw(L, LUA_ERRMEM); + } + g->GCdebt += size; + return newblock; + } +} diff --git a/lmem.h b/lmem.h index 7af316f020..4ccb88ab2f 100644 --- a/lmem.h +++ b/lmem.h @@ -1,5 +1,5 @@ /* -** $Id: lmem.h,v 1.42 2014/12/19 13:45:40 roberto Exp roberto $ +** $Id: lmem.h,v 1.43 2014/12/19 17:26:14 roberto Exp roberto $ ** Interface to Memory Manager ** See Copyright Notice in lua.h */ @@ -15,11 +15,9 @@ /* -** This macro reallocs a vector 'b' from 'on' to 'n' elements, where -** each element has size 'e'. In case of arithmetic overflow of the -** product 'n'*'e', it raises an error (calling 'luaM_toobig'). Because -** 'e' is always constant, it avoids the runtime division MAX_SIZET/(e). -** +** This macro tests whether it is safe to multiply 'n' by the size of +** type 't' without overflows. Because 'e' is always constant, it avoids +** the runtime division MAX_SIZET/(e). ** (The macro is somewhat complex to avoid warnings: The 'sizeof' ** comparison avoids a runtime comparison when overflow cannot occur. ** The compiler should be able to optimize the real test by itself, but @@ -27,10 +25,20 @@ ** false due to limited range of data type"; the +1 tricks the compiler, ** avoiding this warning but also this optimization.) */ +#define luaM_testsize(n,e) \ + (sizeof(n) >= sizeof(size_t) && cast(size_t, (n)) + 1 > MAX_SIZET/(e)) + +#define luaM_checksize(L,n,e) \ + (luaM_testsize(n,e) ? luaM_toobig(L) : cast_void(0)) + +/* +** This macro reallocs a vector 'b' from 'on' to 'n' elements, where +** each element has size 'e'. In case of arithmetic overflow of the +** product 'n'*'e', it raises an error (calling 'luaM_toobig'). +*/ #define luaM_reallocv(L,b,on,n,e) \ - (((sizeof(n) >= sizeof(size_t) && cast(size_t, (n)) + 1 > MAX_SIZET/(e)) \ - ? luaM_toobig(L) : cast_void(0)) , \ - luaM_realloc_(L, (b), (on)*(e), (n)*(e))) + (luaM_checksize(L,n,e), \ + luaM_realloc_(L, (b), cast(size_t, on)*(e), cast(size_t, n)*(e))) /* ** Arrays of chars do not need any test @@ -38,32 +46,37 @@ #define luaM_reallocvchar(L,b,on,n) \ cast(char *, luaM_realloc_(L, (b), (on)*sizeof(char), (n)*sizeof(char))) -#define luaM_freemem(L, b, s) luaM_realloc_(L, (b), (s), 0) -#define luaM_free(L, b) luaM_realloc_(L, (b), sizeof(*(b)), 0) -#define luaM_freearray(L, b, n) luaM_realloc_(L, (b), (n)*sizeof(*(b)), 0) +#define luaM_freemem(L, b, s) luaM_free_(L, (b), (s)) +#define luaM_free(L, b) luaM_free_(L, (b), sizeof(*(b))) +#define luaM_freearray(L, b, n) luaM_free_(L, (b), (n)*sizeof(*(b))) -#define luaM_malloc(L,s) luaM_realloc_(L, NULL, 0, (s)) -#define luaM_new(L,t) cast(t *, luaM_malloc(L, sizeof(t))) +#define luaM_new(L,t) cast(t *, luaM_malloc(L, sizeof(t), 0)) #define luaM_newvector(L,n,t) \ - cast(t *, luaM_reallocv(L, NULL, 0, n, sizeof(t))) + (luaM_checksize(L,n,sizeof(t)), cast(t *, luaM_malloc(L, (n)*sizeof(t), 0))) -#define luaM_newobject(L,tag,s) luaM_realloc_(L, NULL, tag, (s)) +#define luaM_newobject(L,tag,s) luaM_malloc(L, (s), tag) #define luaM_growvector(L,v,nelems,size,t,limit,e) \ - if ((nelems)+1 > (size)) \ - ((v)=cast(t *, luaM_growaux_(L,v,&(size),sizeof(t),limit,e))) + ((v)=cast(t *, luaM_growaux_(L,v,nelems,&(size),sizeof(t),limit,e))) #define luaM_reallocvector(L, v,oldn,n,t) \ ((v)=cast(t *, luaM_reallocv(L, v, oldn, n, sizeof(t)))) +#define luaM_shrinkvector(L,v,size,fs,t) \ + ((v)=cast(t *, luaM_shrinkvector_(L, v, &(size), fs, sizeof(t)))) + LUAI_FUNC l_noret luaM_toobig (lua_State *L); /* not to be called directly */ LUAI_FUNC void *luaM_realloc_ (lua_State *L, void *block, size_t oldsize, size_t size); -LUAI_FUNC void *luaM_growaux_ (lua_State *L, void *block, int *size, - size_t size_elem, int limit, +LUAI_FUNC void luaM_free_ (lua_State *L, void *block, size_t osize); +LUAI_FUNC void *luaM_growaux_ (lua_State *L, void *block, int nelems, + int *size, int size_elem, int limit, const char *what); +LUAI_FUNC void *luaM_shrinkvector_ (lua_State *L, void *block, int *nelem, + int final_n, int size_elem); +LUAI_FUNC void *luaM_malloc (lua_State *L, size_t size, int tag); #endif diff --git a/lparser.c b/lparser.c index 1d46c6f29a..6155a85161 100644 --- a/lparser.c +++ b/lparser.c @@ -1,5 +1,5 @@ /* -** $Id: lparser.c,v 2.168 2017/11/23 16:35:54 roberto Exp roberto $ +** $Id: lparser.c,v 2.169 2017/11/30 13:29:18 roberto Exp roberto $ ** Lua Parser ** See Copyright Notice in lua.h */ @@ -569,21 +569,14 @@ static void close_func (LexState *ls) { luaK_ret(fs, 0, 0); /* final return */ leaveblock(fs); luaK_finish(fs); - luaM_reallocvector(L, f->code, f->sizecode, fs->pc, Instruction); - f->sizecode = fs->pc; - luaM_reallocvector(L, f->lineinfo, f->sizelineinfo, fs->pc, ls_byte); - f->sizelineinfo = fs->pc; - luaM_reallocvector(L, f->abslineinfo, f->sizeabslineinfo, - fs->nabslineinfo, AbsLineInfo); - f->sizeabslineinfo = fs->nabslineinfo; - luaM_reallocvector(L, f->k, f->sizek, fs->nk, TValue); - f->sizek = fs->nk; - luaM_reallocvector(L, f->p, f->sizep, fs->np, Proto *); - f->sizep = fs->np; - luaM_reallocvector(L, f->locvars, f->sizelocvars, fs->nlocvars, LocVar); - f->sizelocvars = fs->nlocvars; - luaM_reallocvector(L, f->upvalues, f->sizeupvalues, fs->nups, Upvaldesc); - f->sizeupvalues = fs->nups; + luaM_shrinkvector(L, f->code, f->sizecode, fs->pc, Instruction); + luaM_shrinkvector(L, f->lineinfo, f->sizelineinfo, fs->pc, ls_byte); + luaM_shrinkvector(L, f->abslineinfo, f->sizeabslineinfo, + fs->nabslineinfo, AbsLineInfo); + luaM_shrinkvector(L, f->k, f->sizek, fs->nk, TValue); + luaM_shrinkvector(L, f->p, f->sizep, fs->np, Proto *); + luaM_shrinkvector(L, f->locvars, f->sizelocvars, fs->nlocvars, LocVar); + luaM_shrinkvector(L, f->upvalues, f->sizeupvalues, fs->nups, Upvaldesc); lua_assert(fs->bl == NULL); ls->fs = fs->prev; luaC_checkGC(L); From 9fa1baf6de64e3e9a3288c21e2da2a10bcd25cd4 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 7 Dec 2017 13:44:10 -0200 Subject: [PATCH 0148/1145] opcodes for order and shift can use several metamethods, so it is better to use a generic description + metamethod names in some error messages shown without '__' prefix --- ldebug.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/ldebug.c b/ldebug.c index 1010240583..160b38fcc9 100644 --- a/ldebug.c +++ b/ldebug.c @@ -1,5 +1,5 @@ /* -** $Id: ldebug.c,v 2.145 2017/11/23 16:35:54 roberto Exp roberto $ +** $Id: ldebug.c,v 2.146 2017/11/23 19:29:04 roberto Exp roberto $ ** Debug Interface ** See Copyright Notice in lua.h */ @@ -606,12 +606,16 @@ static const char *funcnamefromcode (lua_State *L, CallInfo *ci, case OP_LEN: tm = TM_LEN; break; case OP_CONCAT: tm = TM_CONCAT; break; case OP_EQ: tm = TM_EQ; break; - case OP_LT: tm = TM_LT; break; - case OP_LE: tm = TM_LE; break; + case OP_LT: case OP_LE: case OP_LTI: case OP_LEI: + *name = "order"; /* '<=' can call '__lt', etc. */ + return "metamethod"; + case OP_SHRI: case OP_SHLI: + *name = "shift"; + return "metamethod"; default: return NULL; /* cannot find a reasonable name */ } - *name = getstr(G(L)->tmname[tm]); + *name = getstr(G(L)->tmname[tm]) + 2; return "metamethod"; } From cc01d4624782e28c19b3c75ef7efda4b151dbf03 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 7 Dec 2017 16:51:39 -0200 Subject: [PATCH 0149/1145] new test function 'T.allocount' to restrict number of allocations before a memory-allocation error --- ltests.c | 21 ++++++++++++++++++--- ltests.h | 3 ++- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/ltests.c b/ltests.c index a2bbb49fc3..1f795b6c84 100644 --- a/ltests.c +++ b/ltests.c @@ -1,5 +1,5 @@ /* -** $Id: ltests.c,v 2.232 2017/11/09 13:31:29 roberto Exp roberto $ +** $Id: ltests.c,v 2.233 2017/11/23 15:38:42 roberto Exp roberto $ ** Internal Module for Debugging of the Lua Implementation ** See Copyright Notice in lua.h */ @@ -102,7 +102,7 @@ typedef union Header { Memcontrol l_memcontrol = - {0L, 0L, 0L, 0L, {0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L}}; + {0L, 0L, 0L, 0L, (~0L), {0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L}}; static void freeblock (Memcontrol *mc, Header *block) { @@ -141,7 +141,12 @@ void *debug_realloc (void *ud, void *b, size_t oldsize, size_t size) { freeblock(mc, block); return NULL; } - else if (size > oldsize && mc->total+size-oldsize > mc->memlimit) + if (mc->countlimit != ~0UL && size > oldsize) { /* count limit in use? */ + if (mc->countlimit == 0) + return NULL; /* fake a memory allocation error */ + mc->countlimit--; + } + if (size > oldsize && mc->total+size-oldsize > mc->memlimit) return NULL; /* fake a memory allocation error */ else { Header *newblock; @@ -695,6 +700,15 @@ static int mem_query (lua_State *L) { } +static int alloc_count (lua_State *L) { + if (lua_isnone(L, 1)) + l_memcontrol.countlimit = ~0L; + else + l_memcontrol.countlimit = luaL_checkinteger(L, 1); + return 0; +} + + static int settrick (lua_State *L) { if (ttisnil(obj_at(L, 1))) l_Trick = NULL; @@ -1673,6 +1687,7 @@ static const struct luaL_Reg tests_funcs[] = { {"testC", testC}, {"makeCfunc", makeCfunc}, {"totalmem", mem_query}, + {"alloccount", alloc_count}, {"trick", settrick}, {"udataval", udataval}, {"unref", unref}, diff --git a/ltests.h b/ltests.h index 04aa91b365..25598c0e68 100644 --- a/ltests.h +++ b/ltests.h @@ -1,5 +1,5 @@ /* -** $Id: ltests.h,v 2.52 2017/11/13 12:19:35 roberto Exp roberto $ +** $Id: ltests.h,v 2.53 2017/11/23 16:35:54 roberto Exp roberto $ ** Internal Header for Debugging of the Lua Implementation ** See Copyright Notice in lua.h */ @@ -57,6 +57,7 @@ typedef struct Memcontrol { unsigned long total; unsigned long maxmem; unsigned long memlimit; + unsigned long countlimit; unsigned long objcount[LUA_NUMTAGS]; } Memcontrol; From 46bc7f2bf7cf7f48c354fde6b9571b795bdd5b4d Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 7 Dec 2017 16:53:33 -0200 Subject: [PATCH 0150/1145] detail (comment) --- luaconf.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/luaconf.h b/luaconf.h index f7ac7160a2..2bf1bdd138 100644 --- a/luaconf.h +++ b/luaconf.h @@ -1,5 +1,5 @@ /* -** $Id: luaconf.h,v 1.260 2017/04/19 16:34:35 roberto Exp roberto $ +** $Id: luaconf.h,v 1.261 2017/04/24 18:06:12 roberto Exp roberto $ ** Configuration file for Lua ** See Copyright Notice in lua.h */ @@ -724,6 +724,7 @@ ** CHANGE it if you need a different limit. This limit is arbitrary; ** its only purpose is to stop Lua from consuming unlimited stack ** space (and to reserve some numbers for pseudo-indices). +** (It must fit into max(size_t)/32.) */ #if LUAI_BITSINT >= 32 #define LUAI_MAXSTACK 1000000 From 76223730332cbda5d47c09f019ce721b91bd5be2 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 7 Dec 2017 16:59:52 -0200 Subject: [PATCH 0151/1145] using explicit tests for allocation overflow whenever possible --- lmem.c | 28 +++++++++++++++------------- lmem.h | 32 ++++++++++++++++++-------------- lstring.c | 12 ++++++++++-- ltable.c | 38 +++++++++++++++++++++++++++++--------- lundump.c | 16 ++++++++-------- 5 files changed, 80 insertions(+), 46 deletions(-) diff --git a/lmem.c b/lmem.c index ebbfb56a45..23dc14d67b 100644 --- a/lmem.c +++ b/lmem.c @@ -1,5 +1,5 @@ /* -** $Id: lmem.c,v 1.91 2015/03/06 19:45:54 roberto Exp roberto $ +** $Id: lmem.c,v 1.92 2017/12/06 18:36:31 roberto Exp roberto $ ** Interface to Memory Manager ** See Copyright Notice in lua.h */ @@ -53,24 +53,26 @@ #define MINSIZEARRAY 4 -void *luaM_growaux_ (lua_State *L, void *block, int nelems, int *size, +void *luaM_growaux_ (lua_State *L, void *block, int nelems, int *psize, int size_elems, int limit, const char *what) { void *newblock; - int newsize; - if (nelems + 1 <= *size) /* does one extra element still fit? */ + int size = *psize; + if (nelems + 1 <= size) /* does one extra element still fit? */ return block; /* nothing to be done */ - if (*size >= limit/2) { /* cannot double it? */ - if (*size >= limit) /* cannot grow even a little? */ + if (size >= limit / 2) { /* cannot double it? */ + if (size >= limit) /* cannot grow even a little? */ luaG_runerror(L, "too many %s (limit is %d)", what, limit); - newsize = limit; /* still have at least one free place */ + size = limit; /* still have at least one free place */ } else { - newsize = (*size)*2; - if (newsize < MINSIZEARRAY) - newsize = MINSIZEARRAY; /* minimum size */ + size *= 2; + if (size < MINSIZEARRAY) + size = MINSIZEARRAY; /* minimum size */ } - newblock = luaM_reallocv(L, block, *size, newsize, size_elems); - *size = newsize; /* update only when everything else is OK */ + /* 'limit' ensures that multiplication will not overflow */ + newblock = luaM_realloc(L, block, cast(size_t, *psize) * size_elems, + cast(size_t, size) * size_elems); + *psize = size; /* update only when everything else is OK */ return newblock; } @@ -113,7 +115,7 @@ void luaM_free_ (lua_State *L, void *block, size_t osize) { /* ** generic allocation routine. */ -void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) { +void *luaM_realloc (lua_State *L, void *block, size_t osize, size_t nsize) { void *newblock; global_State *g = G(L); lua_assert((osize == 0) == (block == NULL)); diff --git a/lmem.h b/lmem.h index 4ccb88ab2f..3c828789e2 100644 --- a/lmem.h +++ b/lmem.h @@ -1,5 +1,5 @@ /* -** $Id: lmem.h,v 1.43 2014/12/19 17:26:14 roberto Exp roberto $ +** $Id: lmem.h,v 1.44 2017/12/06 18:36:31 roberto Exp roberto $ ** Interface to Memory Manager ** See Copyright Notice in lua.h */ @@ -31,36 +31,40 @@ #define luaM_checksize(L,n,e) \ (luaM_testsize(n,e) ? luaM_toobig(L) : cast_void(0)) + /* -** This macro reallocs a vector 'b' from 'on' to 'n' elements, where -** each element has size 'e'. In case of arithmetic overflow of the -** product 'n'*'e', it raises an error (calling 'luaM_toobig'). +** Computes the minimum between 'n' and 'MAX_SIZET/sizeof(t)', so that +** the result is not larger than 'n' and cannot overflow a 'size_t' +** when multiplied by the size of type 't'. (Assumes that 'n' is an +** 'int' or 'unsigned int' and that 'int' is not larger than 'size_t'.) */ -#define luaM_reallocv(L,b,on,n,e) \ - (luaM_checksize(L,n,e), \ - luaM_realloc_(L, (b), cast(size_t, on)*(e), cast(size_t, n)*(e))) +#define luaM_limitN(n,t) \ + ((cast(size_t, n) > MAX_SIZET/sizeof(t)) ? (MAX_SIZET/sizeof(t)) : (n)) /* ** Arrays of chars do not need any test */ #define luaM_reallocvchar(L,b,on,n) \ - cast(char *, luaM_realloc_(L, (b), (on)*sizeof(char), (n)*sizeof(char))) + cast(char *, luaM_realloc(L, (b), (on)*sizeof(char), (n)*sizeof(char))) #define luaM_freemem(L, b, s) luaM_free_(L, (b), (s)) #define luaM_free(L, b) luaM_free_(L, (b), sizeof(*(b))) #define luaM_freearray(L, b, n) luaM_free_(L, (b), (n)*sizeof(*(b))) -#define luaM_new(L,t) cast(t *, luaM_malloc(L, sizeof(t), 0)) -#define luaM_newvector(L,n,t) \ - (luaM_checksize(L,n,sizeof(t)), cast(t *, luaM_malloc(L, (n)*sizeof(t), 0))) +#define luaM_new(L,t) cast(t*, luaM_malloc(L, sizeof(t), 0)) +#define luaM_newvector(L,n,t) cast(t*, luaM_malloc(L, (n)*sizeof(t), 0)) +#define luaM_newvectorchecked(L,n,t) \ + (luaM_checksize(L,n,sizeof(t)), luaM_newvector(L,n,t)) #define luaM_newobject(L,tag,s) luaM_malloc(L, (s), tag) #define luaM_growvector(L,v,nelems,size,t,limit,e) \ - ((v)=cast(t *, luaM_growaux_(L,v,nelems,&(size),sizeof(t),limit,e))) + ((v)=cast(t *, luaM_growaux_(L,v,nelems,&(size),sizeof(t), \ + luaM_limitN(limit,t),e))) #define luaM_reallocvector(L, v,oldn,n,t) \ - ((v)=cast(t *, luaM_reallocv(L, v, oldn, n, sizeof(t)))) + ((v)=cast(t *, luaM_realloc(L, v, cast(size_t, oldn) * sizeof(t), \ + cast(size_t, n) * sizeof(t)))) #define luaM_shrinkvector(L,v,size,fs,t) \ ((v)=cast(t *, luaM_shrinkvector_(L, v, &(size), fs, sizeof(t)))) @@ -68,7 +72,7 @@ LUAI_FUNC l_noret luaM_toobig (lua_State *L); /* not to be called directly */ -LUAI_FUNC void *luaM_realloc_ (lua_State *L, void *block, size_t oldsize, +LUAI_FUNC void *luaM_realloc (lua_State *L, void *block, size_t oldsize, size_t size); LUAI_FUNC void luaM_free_ (lua_State *L, void *block, size_t osize); LUAI_FUNC void *luaM_growaux_ (lua_State *L, void *block, int nelems, diff --git a/lstring.c b/lstring.c index e8f6f5c9b5..1b3f53e139 100644 --- a/lstring.c +++ b/lstring.c @@ -1,5 +1,5 @@ /* -** $Id: lstring.c,v 2.57 2017/07/27 13:50:16 roberto Exp roberto $ +** $Id: lstring.c,v 2.58 2017/12/01 16:40:29 roberto Exp roberto $ ** String table (keeps all strings handled by Lua) ** See Copyright Notice in lua.h */ @@ -31,6 +31,13 @@ #endif + +/* +** Maximum size for string table. +*/ +#define MAXSTRTB cast_int(luaM_limitN(MAX_INT, TString*)) + + /* ** equality for long strings */ @@ -173,7 +180,8 @@ static TString *internshrstr (lua_State *L, const char *str, size_t l) { return ts; } } - if (g->strt.nuse >= g->strt.size && g->strt.size <= MAX_INT/2) { + if (g->strt.nuse >= g->strt.size && + g->strt.size <= MAXSTRTB / 2) { luaS_resize(L, g->strt.size * 2); list = &g->strt.hash[lmod(h, g->strt.size)]; /* recompute with new size */ } diff --git a/ltable.c b/ltable.c index 54799c210f..d1345009b6 100644 --- a/ltable.c +++ b/ltable.c @@ -1,5 +1,5 @@ /* -** $Id: ltable.c,v 2.126 2017/11/08 14:50:23 roberto Exp roberto $ +** $Id: ltable.c,v 2.127 2017/11/23 19:29:04 roberto Exp roberto $ ** Lua tables (hash) ** See Copyright Notice in lua.h */ @@ -40,21 +40,34 @@ /* -** Maximum size of array part (MAXASIZE) is 2^MAXABITS. MAXABITS is -** the largest integer such that MAXASIZE fits in an unsigned int. +** MAXABITS is the largest integer such that MAXASIZE fits in an +** unsigned int. */ #define MAXABITS cast_int(sizeof(int) * CHAR_BIT - 1) -#define MAXASIZE (1u << MAXABITS) + /* -** Maximum size of hash part is 2^MAXHBITS. MAXHBITS is the largest -** integer such that 2^MAXHBITS fits in a signed int. (Note that the -** maximum number of elements in a table, 2^MAXABITS + 2^MAXHBITS, still -** fits comfortably in an unsigned int.) +** MAXASIZE is the maximum size of the array part. It is the minimum +** between 2^MAXABITS and the maximum size such that, measured in bytes, +** it fits in a 'size_t'. +*/ +#define MAXASIZE luaM_limitN(1u << MAXABITS, TValue) + +/* +** MAXHBITS is the largest integer such that 2^MAXHBITS fits in a +** signed int. */ #define MAXHBITS (MAXABITS - 1) +/* +** MAXHSIZE is the maximum size of the hash part. It is the minimum +** between 2^MAXHBITS and the maximum size such that, measured in bytes, +** it fits in a 'size_t'. +*/ +#define MAXHSIZE luaM_limitN(1u << MAXHBITS, Node) + + #define hashpow2(t,n) (gnode(t, lmod((n), sizenode(t)))) #define hashstr(t,str) hashpow2(t, (str)->hash) @@ -353,6 +366,13 @@ static void setarrayvector (lua_State *L, Table *t, unsigned int size) { } +/* +** Creates an array for the hash part of a table with the given +** size, or reuses the dummy node if size is zero. +** The computation for size overflow is in two steps: the first +** comparison ensures that the shift in the second one does not +** overflow. +*/ static void setnodevector (lua_State *L, Table *t, unsigned int size) { if (size == 0) { /* no elements to hash part? */ t->node = cast(Node *, dummynode); /* use common 'dummynode' */ @@ -362,7 +382,7 @@ static void setnodevector (lua_State *L, Table *t, unsigned int size) { else { int i; int lsize = luaO_ceillog2(size); - if (lsize > MAXHBITS) + if (lsize > MAXHBITS || (1u << lsize) > MAXHSIZE) luaG_runerror(L, "table overflow"); size = twoto(lsize); t->node = luaM_newvector(L, size, Node); diff --git a/lundump.c b/lundump.c index 489a149479..25ab102dab 100644 --- a/lundump.c +++ b/lundump.c @@ -1,5 +1,5 @@ /* -** $Id: lundump.c,v 2.47 2017/06/29 15:06:44 roberto Exp roberto $ +** $Id: lundump.c,v 2.48 2017/11/28 11:19:07 roberto Exp roberto $ ** load precompiled Lua chunks ** See Copyright Notice in lua.h */ @@ -114,7 +114,7 @@ static TString *LoadString (LoadState *S) { static void LoadCode (LoadState *S, Proto *f) { int n = LoadInt(S); - f->code = luaM_newvector(S->L, n, Instruction); + f->code = luaM_newvectorchecked(S->L, n, Instruction); f->sizecode = n; LoadVector(S, f->code, n); } @@ -126,7 +126,7 @@ static void LoadFunction(LoadState *S, Proto *f, TString *psource); static void LoadConstants (LoadState *S, Proto *f) { int i; int n = LoadInt(S); - f->k = luaM_newvector(S->L, n, TValue); + f->k = luaM_newvectorchecked(S->L, n, TValue); f->sizek = n; for (i = 0; i < n; i++) setnilvalue(&f->k[i]); @@ -159,7 +159,7 @@ static void LoadConstants (LoadState *S, Proto *f) { static void LoadProtos (LoadState *S, Proto *f) { int i; int n = LoadInt(S); - f->p = luaM_newvector(S->L, n, Proto *); + f->p = luaM_newvectorchecked(S->L, n, Proto *); f->sizep = n; for (i = 0; i < n; i++) f->p[i] = NULL; @@ -173,7 +173,7 @@ static void LoadProtos (LoadState *S, Proto *f) { static void LoadUpvalues (LoadState *S, Proto *f) { int i, n; n = LoadInt(S); - f->upvalues = luaM_newvector(S->L, n, Upvaldesc); + f->upvalues = luaM_newvectorchecked(S->L, n, Upvaldesc); f->sizeupvalues = n; for (i = 0; i < n; i++) f->upvalues[i].name = NULL; @@ -187,18 +187,18 @@ static void LoadUpvalues (LoadState *S, Proto *f) { static void LoadDebug (LoadState *S, Proto *f) { int i, n; n = LoadInt(S); - f->lineinfo = luaM_newvector(S->L, n, ls_byte); + f->lineinfo = luaM_newvectorchecked(S->L, n, ls_byte); f->sizelineinfo = n; LoadVector(S, f->lineinfo, n); n = LoadInt(S); - f->abslineinfo = luaM_newvector(S->L, n, AbsLineInfo); + f->abslineinfo = luaM_newvectorchecked(S->L, n, AbsLineInfo); f->sizeabslineinfo = n; for (i = 0; i < n; i++) { f->abslineinfo[i].pc = LoadInt(S); f->abslineinfo[i].line = LoadInt(S); } n = LoadInt(S); - f->locvars = luaM_newvector(S->L, n, LocVar); + f->locvars = luaM_newvectorchecked(S->L, n, LocVar); f->sizelocvars = n; for (i = 0; i < n; i++) f->locvars[i].varname = NULL; From 40f823ec907fd725617e37199199b3ed424bd88c Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 8 Dec 2017 13:19:13 -0200 Subject: [PATCH 0152/1145] new C instruction "rawcheckstack" (to test failing in 'lua_checkstack') --- ltests.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/ltests.c b/ltests.c index 1f795b6c84..88333528d2 100644 --- a/ltests.c +++ b/ltests.c @@ -1,5 +1,5 @@ /* -** $Id: ltests.c,v 2.233 2017/11/23 15:38:42 roberto Exp roberto $ +** $Id: ltests.c,v 2.234 2017/12/07 18:51:39 roberto Exp roberto $ ** Internal Module for Debugging of the Lua Implementation ** See Copyright Notice in lua.h */ @@ -1255,6 +1255,10 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) { msg = NULL; /* to test 'luaL_checkstack' with no message */ luaL_checkstack(L1, sz, msg); } + else if EQ("rawcheckstack") { + int sz = getnum; + lua_pushboolean(L1, lua_checkstack(L1, sz)); + } else if EQ("compare") { const char *opt = getstring; /* EQ, LT, or LE */ int op = (opt[0] == 'E') ? LUA_OPEQ @@ -1434,8 +1438,17 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) { int n = getnum; if (L1 != L) { int i; - for (i = 0; i < n; i++) - lua_pushstring(L, lua_tostring(L1, -(n - i))); + for (i = 0; i < n; i++) { + int idx = -(n - i); + switch (lua_type(L1, idx)) { + case LUA_TBOOLEAN: + lua_pushboolean(L, lua_toboolean(L1, idx)); + break; + default: + lua_pushstring(L, lua_tostring(L1, idx)); + break; + } + } } return n; } From e663a24ab03a54fa221c20a793812e5c5ffdf94f Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 8 Dec 2017 15:28:25 -0200 Subject: [PATCH 0153/1145] more freedom in handling memory-allocation errors (not all allocations automatically raise an error), which allows fixing a bug when resizing a table. --- lapi.c | 14 ++------ ldo.c | 42 +++++++++++++----------- ldo.h | 9 +++--- lgc.c | 11 ++----- lmem.c | 30 ++++++++++++------ lmem.h | 23 ++++++++------ lstring.c | 7 ++-- ltable.c | 95 ++++++++++++++++++++++++++++++++++++------------------- 8 files changed, 136 insertions(+), 95 deletions(-) diff --git a/lapi.c b/lapi.c index 3098cb9c36..d6aaf8a873 100644 --- a/lapi.c +++ b/lapi.c @@ -1,5 +1,5 @@ /* -** $Id: lapi.c,v 2.277 2017/11/23 19:29:04 roberto Exp roberto $ +** $Id: lapi.c,v 2.278 2017/12/06 18:08:03 roberto Exp roberto $ ** Lua API ** See Copyright Notice in lua.h */ @@ -99,16 +99,6 @@ static StkId index2stack (lua_State *L, int idx) { } -/* -** to be called by 'lua_checkstack' in protected mode, to grow stack -** capturing memory errors -*/ -static void growstack (lua_State *L, void *ud) { - int size = *(int *)ud; - luaD_growstack(L, size); -} - - LUA_API int lua_checkstack (lua_State *L, int n) { int res; CallInfo *ci = L->ci; @@ -121,7 +111,7 @@ LUA_API int lua_checkstack (lua_State *L, int n) { if (inuse > LUAI_MAXSTACK - n) /* can grow without overflow? */ res = 0; /* no */ else /* try to grow stack */ - res = (luaD_rawrunprotected(L, &growstack, &n) == LUA_OK); + res = luaD_growstack(L, n, 0); } if (res && ci->top < L->top + n) ci->top = L->top + n; /* adjust frame top */ diff --git a/ldo.c b/ldo.c index fe1fcb259a..136aef55f4 100644 --- a/ldo.c +++ b/ldo.c @@ -1,5 +1,5 @@ /* -** $Id: ldo.c,v 2.176 2017/11/29 13:02:17 roberto Exp roberto $ +** $Id: ldo.c,v 2.177 2017/12/01 15:44:51 roberto Exp roberto $ ** Stack and Call structure of Lua ** See Copyright Notice in lua.h */ @@ -156,17 +156,17 @@ int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) { ** Stack reallocation ** =================================================================== */ -static void correctstack (lua_State *L, StkId oldstack) { +static void correctstack (lua_State *L, StkId oldstack, StkId newstack) { CallInfo *ci; UpVal *up; - if (L->stack == oldstack) + if (oldstack == newstack) return; /* stack address did not change */ - L->top = (L->top - oldstack) + L->stack; + L->top = (L->top - oldstack) + newstack; for (up = L->openupval; up != NULL; up = up->u.open.next) - up->v = s2v((uplevel(up) - oldstack) + L->stack); + up->v = s2v((uplevel(up) - oldstack) + newstack); for (ci = L->ci; ci != NULL; ci = ci->previous) { - ci->top = (ci->top - oldstack) + L->stack; - ci->func = (ci->func - oldstack) + L->stack; + ci->top = (ci->top - oldstack) + newstack; + ci->func = (ci->func - oldstack) + newstack; if (isLua(ci)) ci->u.l.trap = 1; /* signal to update 'trap' in 'luaV_execute' */ } @@ -177,36 +177,40 @@ static void correctstack (lua_State *L, StkId oldstack) { #define ERRORSTACKSIZE (LUAI_MAXSTACK + 200) -void luaD_reallocstack (lua_State *L, int newsize) { - StkId oldstack = L->stack; +int luaD_reallocstack (lua_State *L, int newsize, int safe) { int lim = L->stacksize; + StkId newstack = luaM_reallocvector(L, L->stack, lim, newsize, StackValue); lua_assert(newsize <= LUAI_MAXSTACK || newsize == ERRORSTACKSIZE); lua_assert(L->stack_last - L->stack == L->stacksize - EXTRA_STACK); - luaM_reallocvector(L, L->stack, L->stacksize, newsize, StackValue); + if (newstack == NULL) { /* reallocation failed? */ + if (safe) luaM_error(L); + else return 0; /* no-safe mode: signal the error */ + } for (; lim < newsize; lim++) - setnilvalue(s2v(L->stack + lim)); /* erase new segment */ + setnilvalue(s2v(newstack + lim)); /* erase new segment */ + correctstack(L, L->stack, newstack); + L->stack = newstack; L->stacksize = newsize; L->stack_last = L->stack + newsize - EXTRA_STACK; - correctstack(L, oldstack); + return 1; } -void luaD_growstack (lua_State *L, int n) { +int luaD_growstack (lua_State *L, int n, int safe) { int size = L->stacksize; + int newsize = 2 * size; if (size > LUAI_MAXSTACK) /* error after extra size? */ luaD_throw(L, LUA_ERRERR); else { int needed = cast_int(L->top - L->stack) + n + EXTRA_STACK; - int newsize = 2 * size; if (newsize > LUAI_MAXSTACK) newsize = LUAI_MAXSTACK; if (newsize < needed) newsize = needed; if (newsize > LUAI_MAXSTACK) { /* stack overflow? */ - luaD_reallocstack(L, ERRORSTACKSIZE); + luaD_reallocstack(L, ERRORSTACKSIZE, 1); luaG_runerror(L, "stack overflow"); } - else - luaD_reallocstack(L, newsize); - } + } /* else */ + return luaD_reallocstack(L, newsize, safe); } @@ -234,7 +238,7 @@ void luaD_shrinkstack (lua_State *L) { good size is smaller than current size, shrink its stack */ if (inuse <= (LUAI_MAXSTACK - EXTRA_STACK) && goodsize < L->stacksize) - luaD_reallocstack(L, goodsize); + luaD_reallocstack(L, goodsize, 0); /* ok if that fails */ else /* don't change stack */ condmovestack(L,{},{}); /* (change only for debugging) */ } diff --git a/ldo.h b/ldo.h index a9b46cdf4d..388f6c3ea4 100644 --- a/ldo.h +++ b/ldo.h @@ -1,5 +1,5 @@ /* -** $Id: ldo.h,v 2.35 2017/11/23 16:35:54 roberto Exp roberto $ +** $Id: ldo.h,v 2.36 2017/11/23 18:29:41 roberto Exp roberto $ ** Stack and Call structure of Lua ** See Copyright Notice in lua.h */ @@ -22,7 +22,8 @@ */ #define luaD_checkstackaux(L,n,pre,pos) \ if (L->stack_last - L->top <= (n)) \ - { pre; luaD_growstack(L, n); pos; } else { condmovestack(L,pre,pos); } + { pre; luaD_growstack(L, n, 1); pos; } \ + else { condmovestack(L,pre,pos); } /* In general, 'pre'/'pos' are empty (nothing to save) */ #define luaD_checkstack(L,n) luaD_checkstackaux(L,n,(void)0,(void)0) @@ -55,8 +56,8 @@ LUAI_FUNC int luaD_pcall (lua_State *L, Pfunc func, void *u, ptrdiff_t oldtop, ptrdiff_t ef); LUAI_FUNC void luaD_poscall (lua_State *L, CallInfo *ci, StkId firstResult, int nres); -LUAI_FUNC void luaD_reallocstack (lua_State *L, int newsize); -LUAI_FUNC void luaD_growstack (lua_State *L, int n); +LUAI_FUNC int luaD_reallocstack (lua_State *L, int newsize, int safe); +LUAI_FUNC int luaD_growstack (lua_State *L, int n, int safe); LUAI_FUNC void luaD_shrinkstack (lua_State *L); LUAI_FUNC void luaD_inctop (lua_State *L); diff --git a/lgc.c b/lgc.c index f893e122d5..8d28d3bf14 100644 --- a/lgc.c +++ b/lgc.c @@ -1,5 +1,5 @@ /* -** $Id: lgc.c,v 2.240 2017/11/30 15:37:16 roberto Exp roberto $ +** $Id: lgc.c,v 2.241 2017/12/01 17:38:49 roberto Exp roberto $ ** Garbage Collector ** See Copyright Notice in lua.h */ @@ -816,18 +816,13 @@ static GCObject **sweeptolive (lua_State *L, GCObject **p) { */ /* -** If possible, shrink string table (protected from memory errors). +** If possible, shrink string table. */ -static void shrinkstrtable (lua_State *L, void *ud) { - luaS_resize(L, *cast(int*, ud) / 2); -} - - static void checkSizes (lua_State *L, global_State *g) { if (!g->gcemergency) { l_mem olddebt = g->GCdebt; if (g->strt.nuse < g->strt.size / 4) /* string table too big? */ - luaD_rawrunprotected(L, &shrinkstrtable, &g->strt.size); + luaS_resize(L, g->strt.size / 2); g->GCestimate += g->GCdebt - olddebt; /* correct estimate */ } } diff --git a/lmem.c b/lmem.c index 23dc14d67b..afacbb9f69 100644 --- a/lmem.c +++ b/lmem.c @@ -1,5 +1,5 @@ /* -** $Id: lmem.c,v 1.92 2017/12/06 18:36:31 roberto Exp roberto $ +** $Id: lmem.c,v 1.93 2017/12/07 18:59:52 roberto Exp roberto $ ** Interface to Memory Manager ** See Copyright Notice in lua.h */ @@ -69,9 +69,12 @@ void *luaM_growaux_ (lua_State *L, void *block, int nelems, int *psize, if (size < MINSIZEARRAY) size = MINSIZEARRAY; /* minimum size */ } + lua_assert(nelems + 1 <= size && size <= limit); /* 'limit' ensures that multiplication will not overflow */ - newblock = luaM_realloc(L, block, cast(size_t, *psize) * size_elems, - cast(size_t, size) * size_elems); + newblock = luaM_realloc_(L, block, cast(size_t, *psize) * size_elems, + cast(size_t, size) * size_elems); + if (newblock == NULL) + luaM_error(L); *psize = size; /* update only when everything else is OK */ return newblock; } @@ -115,20 +118,20 @@ void luaM_free_ (lua_State *L, void *block, size_t osize) { /* ** generic allocation routine. */ -void *luaM_realloc (lua_State *L, void *block, size_t osize, size_t nsize) { +void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) { void *newblock; global_State *g = G(L); lua_assert((osize == 0) == (block == NULL)); hardtest(L, osize, nsize); newblock = (*g->frealloc)(g->ud, block, osize, nsize); if (newblock == NULL && nsize > 0) { - lua_assert(nsize > osize); /* cannot fail when shrinking a block */ - if (g->version) { /* is state fully built? */ + /* Is state fully built? Not shrinking a block? */ + if (g->version && nsize > osize) { luaC_fullgc(L, 1); /* try to free some memory... */ newblock = (*g->frealloc)(g->ud, block, osize, nsize); /* try again */ } if (newblock == NULL) - luaD_throw(L, LUA_ERRMEM); + return NULL; } lua_assert((nsize == 0) == (newblock == NULL)); g->GCdebt = (g->GCdebt + nsize) - osize; @@ -136,7 +139,16 @@ void *luaM_realloc (lua_State *L, void *block, size_t osize, size_t nsize) { } -void *luaM_malloc (lua_State *L, size_t size, int tag) { +void *luaM_saferealloc_ (lua_State *L, void *block, size_t osize, + size_t nsize) { + void *newblock = luaM_realloc_(L, block, osize, nsize); + if (newblock == NULL && nsize > 0) /* allocation failed? */ + luaM_error(L); + return newblock; +} + + +void *luaM_malloc_ (lua_State *L, size_t size, int tag) { hardtest(L, 0, size); if (size == 0) return NULL; /* that's all */ @@ -149,7 +161,7 @@ void *luaM_malloc (lua_State *L, size_t size, int tag) { newblock = (*g->frealloc)(g->ud, NULL, tag, size); /* try again */ } if (newblock == NULL) - luaD_throw(L, LUA_ERRMEM); + luaM_error(L); } g->GCdebt += size; return newblock; diff --git a/lmem.h b/lmem.h index 3c828789e2..e98aabdbdc 100644 --- a/lmem.h +++ b/lmem.h @@ -1,5 +1,5 @@ /* -** $Id: lmem.h,v 1.44 2017/12/06 18:36:31 roberto Exp roberto $ +** $Id: lmem.h,v 1.45 2017/12/07 18:59:52 roberto Exp roberto $ ** Interface to Memory Manager ** See Copyright Notice in lua.h */ @@ -14,6 +14,9 @@ #include "lua.h" +#define luaM_error(L) luaD_throw(L, LUA_ERRMEM) + + /* ** This macro tests whether it is safe to multiply 'n' by the size of ** type 't' without overflows. Because 'e' is always constant, it avoids @@ -45,26 +48,26 @@ ** Arrays of chars do not need any test */ #define luaM_reallocvchar(L,b,on,n) \ - cast(char *, luaM_realloc(L, (b), (on)*sizeof(char), (n)*sizeof(char))) + cast(char *, luaM_saferealloc_(L, (b), (on)*sizeof(char), (n)*sizeof(char))) #define luaM_freemem(L, b, s) luaM_free_(L, (b), (s)) #define luaM_free(L, b) luaM_free_(L, (b), sizeof(*(b))) #define luaM_freearray(L, b, n) luaM_free_(L, (b), (n)*sizeof(*(b))) -#define luaM_new(L,t) cast(t*, luaM_malloc(L, sizeof(t), 0)) -#define luaM_newvector(L,n,t) cast(t*, luaM_malloc(L, (n)*sizeof(t), 0)) +#define luaM_new(L,t) cast(t*, luaM_malloc_(L, sizeof(t), 0)) +#define luaM_newvector(L,n,t) cast(t*, luaM_malloc_(L, (n)*sizeof(t), 0)) #define luaM_newvectorchecked(L,n,t) \ (luaM_checksize(L,n,sizeof(t)), luaM_newvector(L,n,t)) -#define luaM_newobject(L,tag,s) luaM_malloc(L, (s), tag) +#define luaM_newobject(L,tag,s) luaM_malloc_(L, (s), tag) #define luaM_growvector(L,v,nelems,size,t,limit,e) \ ((v)=cast(t *, luaM_growaux_(L,v,nelems,&(size),sizeof(t), \ luaM_limitN(limit,t),e))) #define luaM_reallocvector(L, v,oldn,n,t) \ - ((v)=cast(t *, luaM_realloc(L, v, cast(size_t, oldn) * sizeof(t), \ - cast(size_t, n) * sizeof(t)))) + (cast(t *, luaM_realloc_(L, v, cast(size_t, oldn) * sizeof(t), \ + cast(size_t, n) * sizeof(t)))) #define luaM_shrinkvector(L,v,size,fs,t) \ ((v)=cast(t *, luaM_shrinkvector_(L, v, &(size), fs, sizeof(t)))) @@ -72,15 +75,17 @@ LUAI_FUNC l_noret luaM_toobig (lua_State *L); /* not to be called directly */ -LUAI_FUNC void *luaM_realloc (lua_State *L, void *block, size_t oldsize, +LUAI_FUNC void *luaM_realloc_ (lua_State *L, void *block, size_t oldsize, size_t size); +LUAI_FUNC void *luaM_saferealloc_ (lua_State *L, void *block, size_t oldsize, + size_t size); LUAI_FUNC void luaM_free_ (lua_State *L, void *block, size_t osize); LUAI_FUNC void *luaM_growaux_ (lua_State *L, void *block, int nelems, int *size, int size_elem, int limit, const char *what); LUAI_FUNC void *luaM_shrinkvector_ (lua_State *L, void *block, int *nelem, int final_n, int size_elem); -LUAI_FUNC void *luaM_malloc (lua_State *L, size_t size, int tag); +LUAI_FUNC void *luaM_malloc_ (lua_State *L, size_t size, int tag); #endif diff --git a/lstring.c b/lstring.c index 1b3f53e139..1675b87a4e 100644 --- a/lstring.c +++ b/lstring.c @@ -1,5 +1,5 @@ /* -** $Id: lstring.c,v 2.58 2017/12/01 16:40:29 roberto Exp roberto $ +** $Id: lstring.c,v 2.59 2017/12/07 18:59:52 roberto Exp roberto $ ** String table (keeps all strings handled by Lua) ** See Copyright Notice in lua.h */ @@ -70,12 +70,15 @@ unsigned int luaS_hashlongstr (TString *ts) { /* -** Resizes the string table. +** Resize the string table. If allocation fails, keep the current size. +** (This can degrade performance, but any size should work correctly.) */ void luaS_resize (lua_State *L, int newsize) { int i; TString **newhash = luaM_newvector(L, newsize, TString *); stringtable *tb = &G(L)->strt; + if (newhash == NULL) /* allocation failed? */ + return; /* leave hash as it is */ for (i = 0; i < newsize; i++) /* initialize new hash array */ newhash[i] = NULL; for (i = 0; i < tb->size; i++) { /* rehash all elements into new array */ diff --git a/ltable.c b/ltable.c index d1345009b6..85101a8a6f 100644 --- a/ltable.c +++ b/ltable.c @@ -1,5 +1,5 @@ /* -** $Id: ltable.c,v 2.127 2017/11/23 19:29:04 roberto Exp roberto $ +** $Id: ltable.c,v 2.128 2017/12/07 18:59:52 roberto Exp roberto $ ** Lua tables (hash) ** See Copyright Notice in lua.h */ @@ -357,15 +357,6 @@ static int numusehash (const Table *t, unsigned int *nums, unsigned int *pna) { } -static void setarrayvector (lua_State *L, Table *t, unsigned int size) { - unsigned int i; - luaM_reallocvector(L, t->array, t->sizearray, size, TValue); - for (i=t->sizearray; iarray[i]); - t->sizearray = size; -} - - /* ** Creates an array for the hash part of a table with the given ** size, or reuses the dummy node if size is zero. @@ -398,39 +389,79 @@ static void setnodevector (lua_State *L, Table *t, unsigned int size) { } -void luaH_resize (lua_State *L, Table *t, unsigned int nasize, +/* +** (Re)insert all elements from list 'nodes' into table 't'. +*/ +static void reinsert(lua_State *L, Node *nodes, int nsize, Table *t) { + int j; + for (j = nsize - 1; j >= 0; j--) { + Node *old = nodes + j; + if (!ttisnil(gval(old))) { + /* doesn't need barrier/invalidate cache, as entry was + already present in the table */ + TValue k; + getnodekey(L, &k, old); + setobjt2t(L, luaH_set(L, t, &k), gval(old)); + } + } +} + + +/* +** Resize table 't' for the new given sizes. Both allocations +** (for the hash part and for the array part) can fail, which +** creates some subtleties. If the first allocation, for the hash +** part, fails, an error is raised and that is it. Otherwise, +** copy the elements in the shrinking part of the array (if it +** is shrinking) into the new hash. Then it reallocates the array part. +** If that fails, it frees the new hash part and restores the old hash +** part (to restore the original state of the table), and then raises +** the allocation error. Otherwise, initialize the new part of the +** array (if any) with nils and reinsert the elements in the old +** hash back into the new parts of the table. +*/ +void luaH_resize (lua_State *L, Table *t, unsigned int newasize, unsigned int nhsize) { unsigned int i; - int j; - unsigned int oldasize = t->sizearray; + Node *oldnode = t->node; /* save old hash ... */ + Node *oldlastfree = t->lastfree; + int oldlsizenode = t->lsizenode; int oldhsize = allocsizenode(t); - Node *nold = t->node; /* save old hash ... */ - if (nasize > oldasize) /* array part must grow? */ - setarrayvector(L, t, nasize); + unsigned int oldasize = t->sizearray; + TValue *newarray; /* create new hash part with appropriate size */ setnodevector(L, t, nhsize); - if (nasize < oldasize) { /* array part must shrink? */ - t->sizearray = nasize; - /* re-insert elements from vanishing slice */ - for (i=nasize; isizearray = newasize; /* pretend array has new size */ + for (i = newasize; i < oldasize; i++) { if (!ttisnil(&t->array[i])) luaH_setint(L, t, i + 1, &t->array[i]); } - /* shrink array */ - luaM_reallocvector(L, t->array, oldasize, nasize, TValue); + t->sizearray = oldasize; /* restore current size */ } - /* re-insert elements from hash part */ - for (j = oldhsize - 1; j >= 0; j--) { - Node *old = nold + j; - if (!ttisnil(gval(old))) { - /* doesn't need barrier/invalidate cache, as entry was - already present in the table */ - TValue k; getnodekey(L, &k, old); - setobjt2t(L, luaH_set(L, t, &k), gval(old)); - } + /* allocate new array */ + newarray = luaM_reallocvector(L, t->array, oldasize, newasize, TValue); + if (newarray == NULL && newasize > 0) { /* allocation failed? */ + if (nhsize > 0) /* not the dummy node? */ + luaM_freearray(L, t->node, allocsizenode(t)); /* release new hash part */ + t->node = oldnode; /* restore original hash part */ + t->lastfree = oldlastfree; + t->lsizenode = oldlsizenode; + lua_assert(!isdummy(t) == (t->node != dummynode)); + luaM_error(L); /* error with array unchanged */ } + /* allocation ok; initialize new part of the array */ + t->array = newarray; + t->sizearray = newasize; + for (i = oldasize; i < newasize; i++) + setnilvalue(&t->array[i]); + /* re-insert elements from old hash part into new parts */ + reinsert(L, oldnode, oldhsize, t); + /* free old hash */ if (oldhsize > 0) /* not the dummy node? */ - luaM_freearray(L, nold, cast(size_t, oldhsize)); /* free old hash */ + luaM_freearray(L, oldnode, cast(size_t, oldhsize)); + lua_assert(!isdummy(t) == (t->node != dummynode)); } From c5ebed73997c118306e548ac5ccb98302dbf90e4 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 11 Dec 2017 10:27:48 -0200 Subject: [PATCH 0154/1145] 'luaM_shrinkvector' raises an error if it cannot shrink the block (several parts of Lua use array size in protos as proxies for number of valid elements) --- lmem.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lmem.c b/lmem.c index afacbb9f69..ecafef4932 100644 --- a/lmem.c +++ b/lmem.c @@ -1,5 +1,5 @@ /* -** $Id: lmem.c,v 1.93 2017/12/07 18:59:52 roberto Exp roberto $ +** $Id: lmem.c,v 1.94 2017/12/08 17:28:25 roberto Exp roberto $ ** Interface to Memory Manager ** See Copyright Notice in lua.h */ @@ -89,7 +89,7 @@ void *luaM_shrinkvector_ (lua_State *L, void *block, int *size, lua_assert(newsize <= oldsize); newblock = (*g->frealloc)(g->ud, block, oldsize, newsize); if (newblock == NULL && final_n > 0) /* allocation failed? */ - return block; /* keep old block */ + luaM_error(L); else { g->GCdebt += newsize - oldsize; *size = final_n; From bfb88e99e9cbc492b35e5dc53594b1d1216171cd Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 11 Dec 2017 10:43:40 -0200 Subject: [PATCH 0155/1145] 'luaD_growstack' cannot raise any errors when 'raiseerror' is false (+ some comments) --- ldo.c | 41 +++++++++++++++++++++++++++-------------- ldo.h | 6 +++--- 2 files changed, 30 insertions(+), 17 deletions(-) diff --git a/ldo.c b/ldo.c index 136aef55f4..a4a2ecbf3f 100644 --- a/ldo.c +++ b/ldo.c @@ -1,5 +1,5 @@ /* -** $Id: ldo.c,v 2.177 2017/12/01 15:44:51 roberto Exp roberto $ +** $Id: ldo.c,v 2.178 2017/12/08 17:28:25 roberto Exp roberto $ ** Stack and Call structure of Lua ** See Copyright Notice in lua.h */ @@ -177,14 +177,15 @@ static void correctstack (lua_State *L, StkId oldstack, StkId newstack) { #define ERRORSTACKSIZE (LUAI_MAXSTACK + 200) -int luaD_reallocstack (lua_State *L, int newsize, int safe) { +int luaD_reallocstack (lua_State *L, int newsize, int raiseerror) { int lim = L->stacksize; StkId newstack = luaM_reallocvector(L, L->stack, lim, newsize, StackValue); lua_assert(newsize <= LUAI_MAXSTACK || newsize == ERRORSTACKSIZE); lua_assert(L->stack_last - L->stack == L->stacksize - EXTRA_STACK); if (newstack == NULL) { /* reallocation failed? */ - if (safe) luaM_error(L); - else return 0; /* no-safe mode: signal the error */ + if (raiseerror) + luaM_error(L); + else return 0; /* do not raise an error */ } for (; lim < newsize; lim++) setnilvalue(s2v(newstack + lim)); /* erase new segment */ @@ -196,21 +197,33 @@ int luaD_reallocstack (lua_State *L, int newsize, int safe) { } -int luaD_growstack (lua_State *L, int n, int safe) { +/* +** Try to grow the stack by at least 'n' elements. when 'raiseerror' +** is true, raises any error; otherwise, return 0 in case of errors. +*/ +int luaD_growstack (lua_State *L, int n, int raiseerror) { int size = L->stacksize; - int newsize = 2 * size; - if (size > LUAI_MAXSTACK) /* error after extra size? */ - luaD_throw(L, LUA_ERRERR); + int newsize = 2 * size; /* tentative new size */ + if (size > LUAI_MAXSTACK) { /* need more space after extra size? */ + if (raiseerror) + luaD_throw(L, LUA_ERRERR); /* error inside message handler */ + else return 0; + } else { int needed = cast_int(L->top - L->stack) + n + EXTRA_STACK; - if (newsize > LUAI_MAXSTACK) newsize = LUAI_MAXSTACK; - if (newsize < needed) newsize = needed; + if (newsize > LUAI_MAXSTACK) /* cannot cross the limit */ + newsize = LUAI_MAXSTACK; + if (newsize < needed) /* but must respect what was asked for */ + newsize = needed; if (newsize > LUAI_MAXSTACK) { /* stack overflow? */ - luaD_reallocstack(L, ERRORSTACKSIZE, 1); - luaG_runerror(L, "stack overflow"); + /* add extra size to be able to handle the error message */ + luaD_reallocstack(L, ERRORSTACKSIZE, raiseerror); + if (raiseerror) + luaG_runerror(L, "stack overflow"); + else return 0; } - } /* else */ - return luaD_reallocstack(L, newsize, safe); + } /* else no errors */ + return luaD_reallocstack(L, newsize, raiseerror); } diff --git a/ldo.h b/ldo.h index 388f6c3ea4..765f6cef5f 100644 --- a/ldo.h +++ b/ldo.h @@ -1,5 +1,5 @@ /* -** $Id: ldo.h,v 2.36 2017/11/23 18:29:41 roberto Exp roberto $ +** $Id: ldo.h,v 2.37 2017/12/08 17:28:25 roberto Exp roberto $ ** Stack and Call structure of Lua ** See Copyright Notice in lua.h */ @@ -56,8 +56,8 @@ LUAI_FUNC int luaD_pcall (lua_State *L, Pfunc func, void *u, ptrdiff_t oldtop, ptrdiff_t ef); LUAI_FUNC void luaD_poscall (lua_State *L, CallInfo *ci, StkId firstResult, int nres); -LUAI_FUNC int luaD_reallocstack (lua_State *L, int newsize, int safe); -LUAI_FUNC int luaD_growstack (lua_State *L, int n, int safe); +LUAI_FUNC int luaD_reallocstack (lua_State *L, int newsize, int raiseerror); +LUAI_FUNC int luaD_growstack (lua_State *L, int n, int raiseerror); LUAI_FUNC void luaD_shrinkstack (lua_State *L); LUAI_FUNC void luaD_inctop (lua_State *L); From 7ad20af2cf478c8ed900c1906f16ff73d4676851 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 11 Dec 2017 16:53:53 -0200 Subject: [PATCH 0156/1145] more freedom in handling memory-allocation errors (not all allocations automatically raise an error), which allows fixing a bug when resizing a table. --- llimits.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/llimits.h b/llimits.h index 5834614098..a939c79948 100644 --- a/llimits.h +++ b/llimits.h @@ -1,5 +1,5 @@ /* -** $Id: llimits.h,v 1.145 2017/11/23 16:35:54 roberto Exp roberto $ +** $Id: llimits.h,v 1.146 2017/12/01 15:08:14 roberto Exp roberto $ ** Limits, basic types, and some other 'installation-dependent' definitions ** See Copyright Notice in lua.h */ @@ -304,7 +304,7 @@ typedef unsigned long Instruction; #else /* realloc stack keeping its size */ #define condmovestack(L,pre,pos) \ - { int sz_ = (L)->stacksize; pre; luaD_reallocstack((L), sz_); pos; } + { int sz_ = (L)->stacksize; pre; luaD_reallocstack((L), sz_, 0); pos; } #endif #if !defined(HARDMEMTESTS) From 3cf340f676aea0d2f4a1135bf9aebc979f93d30a Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 11 Dec 2017 16:55:31 -0200 Subject: [PATCH 0157/1145] allows memory-allocation errors when shrinking blocks --- ltests.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/ltests.c b/ltests.c index 88333528d2..3e9c82a048 100644 --- a/ltests.c +++ b/ltests.c @@ -1,5 +1,5 @@ /* -** $Id: ltests.c,v 2.234 2017/12/07 18:51:39 roberto Exp roberto $ +** $Id: ltests.c,v 2.235 2017/12/08 15:19:13 roberto Exp roberto $ ** Internal Module for Debugging of the Lua Implementation ** See Copyright Notice in lua.h */ @@ -141,7 +141,7 @@ void *debug_realloc (void *ud, void *b, size_t oldsize, size_t size) { freeblock(mc, block); return NULL; } - if (mc->countlimit != ~0UL && size > oldsize) { /* count limit in use? */ + if (mc->countlimit != ~0UL && size > 0) { /* count limit in use? */ if (mc->countlimit == 0) return NULL; /* fake a memory allocation error */ mc->countlimit--; @@ -1001,6 +1001,7 @@ static int loadlib (lua_State *L) { {"math", luaopen_math}, {"string", luaopen_string}, {"table", luaopen_table}, + {"T", luaB_opentests}, {NULL, NULL} }; lua_State *L1 = getstate(L); From b077b20206bf90a4d5cb683d8c63595f2af48756 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 12 Dec 2017 09:52:35 -0200 Subject: [PATCH 0158/1145] back to reallocation when resizing the string table. (Not a good idea to explicitly allocate new memory when shrinking something.) --- lstring.c | 90 ++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 60 insertions(+), 30 deletions(-) diff --git a/lstring.c b/lstring.c index 1675b87a4e..6e2836b312 100644 --- a/lstring.c +++ b/lstring.c @@ -1,5 +1,5 @@ /* -** $Id: lstring.c,v 2.59 2017/12/07 18:59:52 roberto Exp roberto $ +** $Id: lstring.c,v 2.60 2017/12/08 17:28:25 roberto Exp roberto $ ** String table (keeps all strings handled by Lua) ** See Copyright Notice in lua.h */ @@ -69,31 +69,47 @@ unsigned int luaS_hashlongstr (TString *ts) { } -/* -** Resize the string table. If allocation fails, keep the current size. -** (This can degrade performance, but any size should work correctly.) -*/ -void luaS_resize (lua_State *L, int newsize) { +static void rehash (TString **vect, int osize, int nsize) { int i; - TString **newhash = luaM_newvector(L, newsize, TString *); - stringtable *tb = &G(L)->strt; - if (newhash == NULL) /* allocation failed? */ - return; /* leave hash as it is */ - for (i = 0; i < newsize; i++) /* initialize new hash array */ - newhash[i] = NULL; - for (i = 0; i < tb->size; i++) { /* rehash all elements into new array */ - TString *p = tb->hash[i]; + for (i = osize; i < nsize; i++) /* clear new elements */ + vect[i] = NULL; + for (i = 0; i < osize; i++) { /* rehash old part of the array */ + TString *p = vect[i]; + vect[i] = NULL; while (p) { /* for each string in the list */ TString *hnext = p->u.hnext; /* save next */ - unsigned int h = lmod(p->hash, newsize); /* new position */ - p->u.hnext = newhash[h]; /* chain it into new array */ - newhash[h] = p; + unsigned int h = lmod(p->hash, nsize); /* new position */ + p->u.hnext = vect[h]; /* chain it into array */ + vect[h] = p; p = hnext; } } - luaM_freearray(L, tb->hash, tb->size); /* free old array */ - tb->hash = newhash; - tb->size = newsize; +} + + +/* +** Resize the string table. If allocation fails, keep the current size. +** (This can degrade performance, but any non-zero size should work +** correctly.) +*/ +void luaS_resize (lua_State *L, int nsize) { + stringtable *tb = &G(L)->strt; + int osize = tb->size; + TString **newvect; + if (nsize < osize) /* shrinking table? */ + rehash(tb->hash, osize, nsize); /* remove elements from shrinking part */ + newvect = luaM_reallocvector(L, tb->hash, osize, nsize, TString*); + if (newvect == NULL) { /* reallocation failed? */ + if (nsize < osize) /* was it shrinking table? */ + rehash(tb->hash, nsize, osize); /* restore to original size */ + /* leave table as it was */ + } + else { /* allocation succeeded */ + tb->hash = newvect; + tb->size = nsize; + if (nsize > osize) + rehash(newvect, osize, nsize); /* rehash for new size */ + } } @@ -118,7 +134,10 @@ void luaS_init (lua_State *L) { global_State *g = G(L); int i, j; TString *memerrmsg; - luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */ + stringtable *tb = &G(L)->strt; + tb->hash = luaM_newvector(L, MINSTRTABSIZE, TString*); + rehash(tb->hash, 0, MINSTRTABSIZE); /* clear array */ + tb->size = MINSTRTABSIZE; /* pre-create memory-error message */ memerrmsg = luaS_newliteral(L, MEMERRMSG); luaC_fix(L, obj2gco(memerrmsg)); /* it should never be collected */ @@ -165,35 +184,46 @@ void luaS_remove (lua_State *L, TString *ts) { } +static void growstrtab (lua_State *L, stringtable *tb) { + if (tb->nuse == MAX_INT) { /* too many strings? */ + luaC_fullgc(L, 1); /* try to free some... */ + if (tb->nuse == MAX_INT) /* still too many? */ + luaM_error(L); /* cannot even create a message... */ + } + if (tb->size <= MAXSTRTB / 2) /* can grow string table? */ + luaS_resize(L, tb->size * 2); +} + + /* -** checks whether short string exists and reuses it or creates a new one +** Checks whether short string exists and reuses it or creates a new one. */ static TString *internshrstr (lua_State *L, const char *str, size_t l) { TString *ts; global_State *g = G(L); + stringtable *tb = &g->strt; unsigned int h = luaS_hash(str, l, g->seed); - TString **list = &g->strt.hash[lmod(h, g->strt.size)]; + TString **list = &tb->hash[lmod(h, tb->size)]; lua_assert(str != NULL); /* otherwise 'memcmp'/'memcpy' are undefined */ for (ts = *list; ts != NULL; ts = ts->u.hnext) { - if (l == ts->shrlen && - (memcmp(str, getstr(ts), l * sizeof(char)) == 0)) { + if (l == ts->shrlen && (memcmp(str, getstr(ts), l * sizeof(char)) == 0)) { /* found! */ if (isdead(g, ts)) /* dead (but not collected yet)? */ changewhite(ts); /* resurrect it */ return ts; } } - if (g->strt.nuse >= g->strt.size && - g->strt.size <= MAXSTRTB / 2) { - luaS_resize(L, g->strt.size * 2); - list = &g->strt.hash[lmod(h, g->strt.size)]; /* recompute with new size */ + /* else must create a new string */ + if (tb->nuse >= tb->size) { /* need to grow string table? */ + growstrtab(L, tb); + list = &tb->hash[lmod(h, tb->size)]; /* rehash with new size */ } ts = createstrobj(L, l, LUA_TSHRSTR, h); memcpy(getstr(ts), str, l * sizeof(char)); ts->shrlen = cast_byte(l); ts->u.hnext = *list; *list = ts; - g->strt.nuse++; + tb->nuse++; return ts; } From 725c15a4caec1e40ea475c70224473b23a2d8a51 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 12 Dec 2017 09:57:30 -0200 Subject: [PATCH 0159/1145] when shrinking stack, always shrinks the CI list. (Stack overflow is not corelated to CI overflow anymore.) --- ldo.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/ldo.c b/ldo.c index a4a2ecbf3f..c06d71333e 100644 --- a/ldo.c +++ b/ldo.c @@ -1,5 +1,5 @@ /* -** $Id: ldo.c,v 2.178 2017/12/08 17:28:25 roberto Exp roberto $ +** $Id: ldo.c,v 2.179 2017/12/11 12:43:40 roberto Exp roberto $ ** Stack and Call structure of Lua ** See Copyright Notice in lua.h */ @@ -243,10 +243,6 @@ void luaD_shrinkstack (lua_State *L) { int goodsize = inuse + (inuse / 8) + 2*EXTRA_STACK; if (goodsize > LUAI_MAXSTACK) goodsize = LUAI_MAXSTACK; /* respect stack limit */ - if (L->stacksize > LUAI_MAXSTACK) /* had been handling stack overflow? */ - luaE_freeCI(L); /* free all CIs (list grew because of an error) */ - else - luaE_shrinkCI(L); /* shrink list */ /* if thread is currently not handling a stack overflow and its good size is smaller than current size, shrink its stack */ if (inuse <= (LUAI_MAXSTACK - EXTRA_STACK) && @@ -254,6 +250,7 @@ void luaD_shrinkstack (lua_State *L) { luaD_reallocstack(L, goodsize, 0); /* ok if that fails */ else /* don't change stack */ condmovestack(L,{},{}); /* (change only for debugging) */ + luaE_shrinkCI(L); /* shrink CI list */ } From 36cf8f3a3c44da00cc9255797153df3c02895379 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 13 Dec 2017 10:51:42 -0200 Subject: [PATCH 0160/1145] Code should not change the stack level after the initialization of a string buffer. --- loadlib.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/loadlib.c b/loadlib.c index d1941a99fa..176330428d 100644 --- a/loadlib.c +++ b/loadlib.c @@ -1,5 +1,5 @@ /* -** $Id: loadlib.c,v 1.129 2016/12/04 20:17:24 roberto Exp roberto $ +** $Id: loadlib.c,v 1.130 2017/01/12 17:14:26 roberto Exp roberto $ ** Dynamic library loader for Lua ** See Copyright Notice in lua.h ** @@ -437,9 +437,9 @@ static const char *searchpath (lua_State *L, const char *name, const char *sep, const char *dirsep) { luaL_Buffer msg; /* to build error message */ - luaL_buffinit(L, &msg); if (*sep != '\0') /* non-empty separator? */ name = luaL_gsub(L, name, sep, dirsep); /* replace it by 'dirsep' */ + luaL_buffinit(L, &msg); while ((path = pushnexttemplate(L, path)) != NULL) { const char *filename = luaL_gsub(L, lua_tostring(L, -1), LUA_PATH_MARK, name); @@ -569,10 +569,10 @@ static int searcher_preload (lua_State *L) { static void findloader (lua_State *L, const char *name) { int i; luaL_Buffer msg; /* to build error message */ - luaL_buffinit(L, &msg); /* push 'package.searchers' to index 3 in the stack */ if (lua_getfield(L, lua_upvalueindex(1), "searchers") != LUA_TTABLE) luaL_error(L, "'package.searchers' must be a table"); + luaL_buffinit(L, &msg); /* iterate over available searchers to find a loader */ for (i = 1; ; i++) { if (lua_rawgeti(L, 3, i) == LUA_TNIL) { /* no more searchers? */ From 86431a2f1c668844c665f9d09e246de906b511d8 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 13 Dec 2017 16:32:09 -0200 Subject: [PATCH 0161/1145] new opcodes BANDK/BORK/BXORK. (They do not use immediate operands because, too often, masks in bitwise operations are integers larger than one byte.) --- lcode.c | 52 ++++++++++++++++++++++++++++++++++++++++++---------- ldebug.c | 5 +++-- lopcodes.c | 8 +++++++- lopcodes.h | 6 +++++- ltm.c | 19 ++++++++++++------- ltm.h | 4 +++- lvm.c | 36 +++++++++++++++++++++++++++++++++++- 7 files changed, 107 insertions(+), 23 deletions(-) diff --git a/lcode.c b/lcode.c index a09731a7a7..fc7de6e30b 100644 --- a/lcode.c +++ b/lcode.c @@ -1,5 +1,5 @@ /* -** $Id: lcode.c,v 2.141 2017/11/30 15:37:16 roberto Exp roberto $ +** $Id: lcode.c,v 2.142 2017/12/04 17:41:30 roberto Exp roberto $ ** Code generator for Lua ** See Copyright Notice in lua.h */ @@ -1196,6 +1196,15 @@ static void codeunexpval (FuncState *fs, OpCode op, expdesc *e, int line) { } +static void finishbinexpval (FuncState *fs, expdesc *e1, expdesc *e2, + int pc, int line) { + freeexps(fs, e1, e2); + e1->u.info = pc; + e1->k = VRELOCABLE; /* all those operations are relocatable */ + luaK_fixline(fs, line); +} + + /* ** Emit code for binary expressions that "produce values" ** (everything but logical operators 'and'/'or' and comparison @@ -1209,10 +1218,8 @@ static void codebinexpval (FuncState *fs, OpCode op, expdesc *e1, expdesc *e2, int line) { int v2 = luaK_exp2anyreg(fs, e2); /* both operands are in registers */ int v1 = luaK_exp2anyreg(fs, e1); - freeexps(fs, e1, e2); - e1->u.info = luaK_codeABC(fs, op, 0, v1, v2); /* generate opcode */ - e1->k = VRELOCABLE; /* all those operations are relocatable */ - luaK_fixline(fs, line); + int pc = luaK_codeABC(fs, op, 0, v1, v2); /* generate opcode */ + finishbinexpval(fs, e1, e2, pc, line); } @@ -1223,10 +1230,8 @@ static void codebini (FuncState *fs, OpCode op, expdesc *e1, expdesc *e2, int k, int line) { int v2 = cast_int(e2->u.ival); /* immediate operand */ int v1 = luaK_exp2anyreg(fs, e1); - freeexp(fs, e1); - e1->u.info = codeABsC(fs, op, 0, v1, v2, k); /* generate opcode */ - e1->k = VRELOCABLE; /* all those operations are relocatable */ - luaK_fixline(fs, line); + int pc = codeABsC(fs, op, 0, v1, v2, k); /* generate opcode */ + finishbinexpval(fs, e1, e2, pc, line); } @@ -1264,6 +1269,33 @@ static void codecommutative (FuncState *fs, OpCode op, } +/* +** Code bitwise operations; they are all associative, so the function +** tries to put an integer constant as the 2nd operand (a K operand). +*/ +static void codebitwise (FuncState *fs, BinOpr opr, + expdesc *e1, expdesc *e2, int line) { + int inv = 0; + int v1, v2, pc; + OpCode op; + if (e1->k == VKINT && luaK_exp2RK(fs, e1)) { + swapexps(e1, e2); /* 'e2' will be the constant operand */ + inv = 1; + } + else if (!(e2->k == VKINT && luaK_exp2RK(fs, e2))) { /* no constants? */ + op = cast(OpCode, opr - OPR_BAND + OP_BAND); + codebinexpval(fs, op, e1, e2, line); /* all-register opcodes */ + return; + } + v1 = luaK_exp2anyreg(fs, e1); + v2 = e2->u.info; /* index in K array */ + op = cast(OpCode, opr - OPR_BAND + OP_BANDK); + lua_assert(ttisinteger(&fs->f->k[v2])); + pc = luaK_codeABCk(fs, op, 0, v1, v2, inv); + finishbinexpval(fs, e1, e2, pc, line); +} + + /* ** Code shift operators. If second operand is constant, use immediate ** operand (negating it if shift is in the other direction). @@ -1468,7 +1500,7 @@ void luaK_posfix (FuncState *fs, BinOpr opr, } case OPR_BAND: case OPR_BOR: case OPR_BXOR: { if (!constfolding(fs, opr + LUA_OPADD, e1, e2)) - codebinexpval(fs, cast(OpCode, opr + OP_ADD), e1, e2, line); + codebitwise(fs, opr, e1, e2, line); break; } case OPR_SHL: { diff --git a/ldebug.c b/ldebug.c index 160b38fcc9..93f2d53d61 100644 --- a/ldebug.c +++ b/ldebug.c @@ -1,5 +1,5 @@ /* -** $Id: ldebug.c,v 2.146 2017/11/23 19:29:04 roberto Exp roberto $ +** $Id: ldebug.c,v 2.147 2017/12/07 15:44:10 roberto Exp roberto $ ** Debug Interface ** See Copyright Notice in lua.h */ @@ -589,7 +589,8 @@ static const char *funcnamefromcode (lua_State *L, CallInfo *ci, tm = TM_NEWINDEX; break; case OP_ADDI: case OP_SUBI: case OP_MULI: case OP_MODI: - case OP_POWI: case OP_DIVI: case OP_IDIVI: { + case OP_POWI: case OP_DIVI: case OP_IDIVI: + case OP_BANDK: case OP_BORK: case OP_BXORK: { int offset = GET_OPCODE(i) - OP_ADDI; /* ORDER OP */ tm = cast(TMS, offset + TM_ADD); /* ORDER TM */ break; diff --git a/lopcodes.c b/lopcodes.c index 55b6050daf..1d2463846d 100644 --- a/lopcodes.c +++ b/lopcodes.c @@ -1,5 +1,5 @@ /* -** $Id: lopcodes.c,v 1.71 2017/11/29 16:57:36 roberto Exp roberto $ +** $Id: lopcodes.c,v 1.72 2017/12/04 17:41:30 roberto Exp roberto $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ @@ -44,6 +44,9 @@ LUAI_DDEF const char *const luaP_opnames[NUM_OPCODES+1] = { "POWI", "DIVI", "IDIVI", + "BANDK", + "BORK", + "BXORK", "SHRI", "SHLI", "ADD", @@ -119,6 +122,9 @@ LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = { ,opmode(0, 1, iABC) /* OP_POWI */ ,opmode(0, 1, iABC) /* OP_DIVI */ ,opmode(0, 1, iABC) /* OP_IDIVI */ + ,opmode(0, 1, iABC) /* OP_BANDK */ + ,opmode(0, 1, iABC) /* OP_BORK */ + ,opmode(0, 1, iABC) /* OP_BXORK */ ,opmode(0, 1, iABC) /* OP_SHRI */ ,opmode(0, 1, iABC) /* OP_SHLI */ ,opmode(0, 1, iABC) /* OP_ADD */ diff --git a/lopcodes.h b/lopcodes.h index 7468b9e390..f3c3e05595 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -1,5 +1,5 @@ /* -** $Id: lopcodes.h,v 1.175 2017/11/30 13:16:43 roberto Exp roberto $ +** $Id: lopcodes.h,v 1.176 2017/12/04 17:41:30 roberto Exp roberto $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ @@ -217,6 +217,10 @@ OP_POWI,/* A B sC R(A) := R(B) ^ C */ OP_DIVI,/* A B sC R(A) := R(B) / C */ OP_IDIVI,/* A B sC R(A) := R(B) // C */ +OP_BANDK,/* A B C R(A) := R(B) & K(C):integer */ +OP_BORK,/* A B C R(A) := R(B) | K(C):integer */ +OP_BXORK,/* A B C R(A) := R(B) ~ K(C):integer */ + OP_SHRI,/* A B C R(A) := R(B) >> C */ OP_SHLI,/* A B C R(A) := C << R(B) */ diff --git a/ltm.c b/ltm.c index fe87553d05..d30bef2f8d 100644 --- a/ltm.c +++ b/ltm.c @@ -1,5 +1,5 @@ /* -** $Id: ltm.c,v 2.50 2017/11/27 17:44:31 roberto Exp roberto $ +** $Id: ltm.c,v 2.51 2017/11/30 15:37:16 roberto Exp roberto $ ** Tag methods ** See Copyright Notice in lua.h */ @@ -166,15 +166,20 @@ void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2, } +void luaT_trybinassocTM (lua_State *L, const TValue *p1, const TValue *p2, + StkId res, int inv, TMS event) { + if (inv) + luaT_trybinTM(L, p2, p1, res, event); + else + luaT_trybinTM(L, p1, p2, res, event); +} + + void luaT_trybiniTM (lua_State *L, const TValue *p1, int i2, int inv, StkId res, TMS event) { - TValue aux; const TValue *p2; + TValue aux; setivalue(&aux, i2); - if (inv) { /* arguments were exchanged? */ - p2 = p1; p1 = &aux; /* correct them */ - } - else p2 = &aux; - luaT_trybinTM(L, p1, p2, res, event); + luaT_trybinassocTM(L, p1, &aux, res, inv, event); } diff --git a/ltm.h b/ltm.h index c8fed77c62..34dbc82cc4 100644 --- a/ltm.h +++ b/ltm.h @@ -1,5 +1,5 @@ /* -** $Id: ltm.h,v 2.26 2017/09/27 18:59:08 roberto Exp roberto $ +** $Id: ltm.h,v 2.27 2017/11/27 17:44:31 roberto Exp roberto $ ** Tag methods ** See Copyright Notice in lua.h */ @@ -68,6 +68,8 @@ LUAI_FUNC void luaT_callTMres (lua_State *L, const TValue *f, const TValue *p1, const TValue *p2, StkId p3); LUAI_FUNC void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2, StkId res, TMS event); +LUAI_FUNC void luaT_trybinassocTM (lua_State *L, const TValue *p1, + const TValue *p2, StkId res, int inv, TMS event); LUAI_FUNC void luaT_trybiniTM (lua_State *L, const TValue *p1, int i2, int inv, StkId res, TMS event); LUAI_FUNC int luaT_callorderTM (lua_State *L, const TValue *p1, diff --git a/lvm.c b/lvm.c index 9cbba4a98b..90fd106060 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.323 2017/11/30 13:29:18 roberto Exp roberto $ +** $Id: lvm.c,v 2.324 2017/12/04 17:41:30 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -687,6 +687,7 @@ void luaV_finishOp (lua_State *L) { case OP_MODI: case OP_POWI: case OP_ADD: case OP_SUB: case OP_MUL: case OP_DIV: case OP_IDIV: + case OP_BANDK: case OP_BORK: case OP_BXORK: case OP_BAND: case OP_BOR: case OP_BXOR: case OP_SHRI: case OP_SHL: case OP_SHR: case OP_MOD: case OP_POW: @@ -1182,6 +1183,39 @@ void luaV_execute (lua_State *L, CallInfo *ci) { Protect(luaT_trybinTM(L, rb, rc, ra, TM_DIV)); vmbreak; } + vmcase(OP_BANDK) { + TValue *p1 = vRB(i); + TValue *p2 = KC(i); + lua_Integer i1; + if (tointegerns(p1, &i1)) { + setivalue(vra, intop(&, i1, ivalue(p2))); + } + else + Protect(luaT_trybinassocTM(L, p1, p2, ra, TESTARG_k(i), TM_BAND)); + vmbreak; + } + vmcase(OP_BORK) { + TValue *p1 = vRB(i); + TValue *p2 = KC(i); + lua_Integer i1; + if (tointegerns(p1, &i1)) { + setivalue(vra, intop(|, i1, ivalue(p2))); + } + else + Protect(luaT_trybinassocTM(L, p1, p2, ra, TESTARG_k(i), TM_BOR)); + vmbreak; + } + vmcase(OP_BXORK) { + TValue *p1 = vRB(i); + TValue *p2 = KC(i); + lua_Integer i1; + if (tointegerns(p1, &i1)) { + setivalue(vra, intop(^, i1, ivalue(p2))); + } + else + Protect(luaT_trybinassocTM(L, p1, p2, ra, TESTARG_k(i), TM_BXOR)); + vmbreak; + } vmcase(OP_BAND) { TValue *rb = vRB(i); TValue *rc = vRC(i); From e752d84ed8250820aa3f6a097e008de6c2ec8322 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 13 Dec 2017 16:35:03 -0200 Subject: [PATCH 0162/1145] bug: memory-allocation error when resizing a table can leave it in an inconsistent state. --- bugs | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/bugs b/bugs index bd4b313c19..d796facb38 100644 --- a/bugs +++ b/bugs @@ -3680,9 +3680,9 @@ It needs an "interceptor" 'memcmp' function that continues reading memory after a difference is found.]], patch = [[ 2c2 -< ** $Id: bugs,v 1.157 2017/08/31 16:14:41 roberto Exp roberto $ +< ** $Id: bugs,v 1.158 2017/12/06 18:20:28 roberto Exp roberto $ --- -> ** $Id: bugs,v 1.157 2017/08/31 16:14:41 roberto Exp roberto $ +> ** $Id: bugs,v 1.158 2017/12/06 18:20:28 roberto Exp roberto $ 263c263,264 < for (option = LUA_STRFTIMEOPTIONS; *option != '\0'; option += oplen) { --- @@ -3904,6 +3904,31 @@ patch = [[ } +Bug{ +what = [[memory-allocation error when resizing a table can leave it +in an inconsistent state.]], +report = [[Roberto, 2017/12/08]], +since = [[5.0]], +fix = nil, +example = [[ +local a = {x = 1, y = 1, z = 1} +a[1] = 10 -- goes to the hash part (which has 4 slots) +print(a[1]) --> 10 + +-- assume that the 2nd memory allocation from now fails +pcall(rawset, a, 2, 20) -- forces a rehash + +-- a[1] now exists both in the array part (because the array part +-- grew) and in the hash part (because the allocation of the hash +-- part failed, keeping it as it was). +-- This makes the following traversal goes forever... +for k,v in pairs(a) do print(k,v) end +]], +patch = [[ +]] +} + + --[=[ From e001d5aea693ca1bcf1a4dfd2f900737fc0a8604 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 14 Dec 2017 12:24:02 -0200 Subject: [PATCH 0163/1145] 'VRELOCABLE' -> 'VRELOC' --- lcode.c | 32 ++++++++++++++++---------------- lparser.c | 6 +++--- lparser.h | 6 +++--- 3 files changed, 22 insertions(+), 22 deletions(-) diff --git a/lcode.c b/lcode.c index fc7de6e30b..660e5270a5 100644 --- a/lcode.c +++ b/lcode.c @@ -1,5 +1,5 @@ /* -** $Id: lcode.c,v 2.142 2017/12/04 17:41:30 roberto Exp roberto $ +** $Id: lcode.c,v 2.143 2017/12/13 18:32:09 roberto Exp roberto $ ** Code generator for Lua ** See Copyright Notice in lua.h */ @@ -632,7 +632,7 @@ void luaK_setreturns (FuncState *fs, expdesc *e, int nresults) { ** vararg), it already returns one result, so nothing needs to be done. ** Function calls become VNONRELOC expressions (as its result comes ** fixed in the base register of the call), while vararg expressions -** become VRELOCABLE (as OP_VARARG puts its results where it wants). +** become VRELOC (as OP_VARARG puts its results where it wants). ** (Calls are created returning one result, so that does not need ** to be fixed.) */ @@ -645,7 +645,7 @@ void luaK_setoneret (FuncState *fs, expdesc *e) { } else if (e->k == VVARARG) { SETARG_B(getinstruction(fs, e), 2); - e->k = VRELOCABLE; /* can relocate its simple result */ + e->k = VRELOC; /* can relocate its simple result */ } } @@ -661,30 +661,30 @@ void luaK_dischargevars (FuncState *fs, expdesc *e) { } case VUPVAL: { /* move value to some (pending) register */ e->u.info = luaK_codeABC(fs, OP_GETUPVAL, 0, e->u.info, 0); - e->k = VRELOCABLE; + e->k = VRELOC; break; } case VINDEXUP: { e->u.info = luaK_codeABC(fs, OP_GETTABUP, 0, e->u.ind.t, e->u.ind.idx); - e->k = VRELOCABLE; + e->k = VRELOC; break; } case VINDEXI: { freereg(fs, e->u.ind.t); e->u.info = luaK_codeABC(fs, OP_GETI, 0, e->u.ind.t, e->u.ind.idx); - e->k = VRELOCABLE; + e->k = VRELOC; break; } case VINDEXSTR: { freereg(fs, e->u.ind.t); e->u.info = luaK_codeABC(fs, OP_GETFIELD, 0, e->u.ind.t, e->u.ind.idx); - e->k = VRELOCABLE; + e->k = VRELOC; break; } case VINDEXED: { freeregs(fs, e->u.ind.t, e->u.ind.idx); e->u.info = luaK_codeABC(fs, OP_GETTABLE, 0, e->u.ind.t, e->u.ind.idx); - e->k = VRELOCABLE; + e->k = VRELOC; break; } case VVARARG: case VCALL: { @@ -723,7 +723,7 @@ static void discharge2reg (FuncState *fs, expdesc *e, int reg) { luaK_int(fs, reg, e->u.ival); break; } - case VRELOCABLE: { + case VRELOC: { Instruction *pc = &getinstruction(fs, e); SETARG_A(*pc, reg); /* instruction will put result in 'reg' */ break; @@ -963,7 +963,7 @@ static void negatecondition (FuncState *fs, expdesc *e) { ** and removing the 'not'. */ static int jumponcond (FuncState *fs, expdesc *e, int cond) { - if (e->k == VRELOCABLE) { + if (e->k == VRELOC) { Instruction ie = getinstruction(fs, e); if (GET_OPCODE(ie) == OP_NOT) { fs->pc--; /* remove previous OP_NOT */ @@ -1048,12 +1048,12 @@ static void codenot (FuncState *fs, expdesc *e) { negatecondition(fs, e); break; } - case VRELOCABLE: + case VRELOC: case VNONRELOC: { discharge2anyreg(fs, e); freeexp(fs, e); e->u.info = luaK_codeABC(fs, OP_NOT, 0, e->u.info, 0); - e->k = VRELOCABLE; + e->k = VRELOC; break; } default: lua_assert(0); /* cannot happen */ @@ -1191,7 +1191,7 @@ static void codeunexpval (FuncState *fs, OpCode op, expdesc *e, int line) { int r = luaK_exp2anyreg(fs, e); /* opcodes operate only on registers */ freeexp(fs, e); e->u.info = luaK_codeABC(fs, op, 0, r, 0); /* generate opcode */ - e->k = VRELOCABLE; /* all those operations are relocatable */ + e->k = VRELOC; /* all those operations are relocatable */ luaK_fixline(fs, line); } @@ -1200,7 +1200,7 @@ static void finishbinexpval (FuncState *fs, expdesc *e1, expdesc *e2, int pc, int line) { freeexps(fs, e1, e2); e1->u.info = pc; - e1->k = VRELOCABLE; /* all those operations are relocatable */ + e1->k = VRELOC; /* all those operations are relocatable */ luaK_fixline(fs, line); } @@ -1474,12 +1474,12 @@ void luaK_posfix (FuncState *fs, BinOpr opr, } case OPR_CONCAT: { luaK_exp2val(fs, e2); - if (e2->k == VRELOCABLE && + if (e2->k == VRELOC && GET_OPCODE(getinstruction(fs, e2)) == OP_CONCAT) { lua_assert(e1->u.info == GETARG_B(getinstruction(fs, e2))-1); freeexp(fs, e1); SETARG_B(getinstruction(fs, e2), e1->u.info); - e1->k = VRELOCABLE; e1->u.info = e2->u.info; + e1->k = VRELOC; e1->u.info = e2->u.info; } else { luaK_exp2nextreg(fs, e2); /* operand must be on the 'stack' */ diff --git a/lparser.c b/lparser.c index 6155a85161..a4a7e5fa68 100644 --- a/lparser.c +++ b/lparser.c @@ -1,5 +1,5 @@ /* -** $Id: lparser.c,v 2.169 2017/11/30 13:29:18 roberto Exp roberto $ +** $Id: lparser.c,v 2.170 2017/12/06 18:36:31 roberto Exp roberto $ ** Lua Parser ** See Copyright Notice in lua.h */ @@ -533,7 +533,7 @@ static Proto *addprototype (LexState *ls) { */ static void codeclosure (LexState *ls, expdesc *v) { FuncState *fs = ls->fs->prev; - init_exp(v, VRELOCABLE, luaK_codeABx(fs, OP_CLOSURE, 0, fs->np - 1)); + init_exp(v, VRELOC, luaK_codeABx(fs, OP_CLOSURE, 0, fs->np - 1)); luaK_exp2nextreg(fs, v); /* fix it at the last register */ } @@ -740,7 +740,7 @@ static void constructor (LexState *ls, expdesc *t) { struct ConsControl cc; cc.na = cc.nh = cc.tostore = 0; cc.t = t; - init_exp(t, VRELOCABLE, pc); + init_exp(t, VRELOC, pc); init_exp(&cc.v, VVOID, 0); /* no value (yet) */ luaK_exp2nextreg(ls->fs, t); /* fix it at stack top */ checknext(ls, '{'); diff --git a/lparser.h b/lparser.h index 4d342d325c..6007d618bf 100644 --- a/lparser.h +++ b/lparser.h @@ -1,5 +1,5 @@ /* -** $Id: lparser.h,v 1.78 2017/06/27 11:35:31 roberto Exp roberto $ +** $Id: lparser.h,v 1.79 2017/11/30 13:29:18 roberto Exp roberto $ ** Lua Parser ** See Copyright Notice in lua.h */ @@ -49,8 +49,8 @@ typedef enum { ind.idx = key's K index */ VJMP, /* expression is a test/comparison; info = pc of corresponding jump instruction */ - VRELOCABLE, /* expression can put result in any register; - info = instruction pc */ + VRELOC, /* expression can put result in any register; + info = instruction pc */ VCALL, /* expression is a function call; info = instruction pc */ VVARARG /* vararg expression; info = instruction pc */ } expkind; From b3f924bc695ed6ebe2539d365197696e41863a91 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 15 Dec 2017 11:07:10 -0200 Subject: [PATCH 0164/1145] 'Proto->numparams' does not include vararg parameter (one less subtraction when calling functions...) --- ldebug.c | 4 ++-- ldo.c | 6 +++--- lparser.c | 9 ++++----- ltm.c | 4 ++-- 4 files changed, 11 insertions(+), 12 deletions(-) diff --git a/ldebug.c b/ldebug.c index 93f2d53d61..11d2b6a7f4 100644 --- a/ldebug.c +++ b/ldebug.c @@ -1,5 +1,5 @@ /* -** $Id: ldebug.c,v 2.147 2017/12/07 15:44:10 roberto Exp roberto $ +** $Id: ldebug.c,v 2.148 2017/12/13 18:32:09 roberto Exp roberto $ ** Debug Interface ** See Copyright Notice in lua.h */ @@ -328,7 +328,7 @@ static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar, } else { ar->isvararg = f->l.p->is_vararg; - ar->nparams = f->l.p->numparams; + ar->nparams = f->l.p->numparams + f->l.p->is_vararg; } break; } diff --git a/ldo.c b/ldo.c index c06d71333e..e58efa528e 100644 --- a/ldo.c +++ b/ldo.c @@ -1,5 +1,5 @@ /* -** $Id: ldo.c,v 2.179 2017/12/11 12:43:40 roberto Exp roberto $ +** $Id: ldo.c,v 2.180 2017/12/12 11:57:30 roberto Exp roberto $ ** Stack and Call structure of Lua ** See Copyright Notice in lua.h */ @@ -415,7 +415,7 @@ void luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, int n) { for (i = 0; i < n; i++) /* move down function and arguments */ setobjs2s(L, ci->func + i, func + i); checkstackp(L, fsize, func); - for (; i < p->numparams - p->is_vararg; i++) + for (; i < p->numparams; i++) setnilvalue(s2v(ci->func + i)); /* complete missing parameters */ if (p->is_vararg) { L->top -= (func - ci->func); /* move down top */ @@ -469,7 +469,7 @@ void luaD_call (lua_State *L, StkId func, int nresults) { int n = cast_int(L->top - func) - 1; /* number of real arguments */ int fsize = p->maxstacksize; /* frame size */ checkstackp(L, fsize, func); - for (; n < p->numparams - p->is_vararg; n++) + for (; n < p->numparams; n++) setnilvalue(s2v(L->top++)); /* complete missing arguments */ if (p->is_vararg) luaT_adjustvarargs(L, p, n); diff --git a/lparser.c b/lparser.c index a4a7e5fa68..9d4d5effd1 100644 --- a/lparser.c +++ b/lparser.c @@ -1,5 +1,5 @@ /* -** $Id: lparser.c,v 2.170 2017/12/06 18:36:31 roberto Exp roberto $ +** $Id: lparser.c,v 2.171 2017/12/14 14:24:02 roberto Exp roberto $ ** Lua Parser ** See Copyright Notice in lua.h */ @@ -765,7 +765,6 @@ static void parlist (LexState *ls) { FuncState *fs = ls->fs; Proto *f = fs->f; int nparams = 0; - f->is_vararg = 0; if (ls->t.token != ')') { /* is 'parlist' not empty? */ do { switch (ls->t.token) { @@ -789,7 +788,7 @@ static void parlist (LexState *ls) { } while (!f->is_vararg && testnext(ls, ',')); } adjustlocalvars(ls, nparams); - f->numparams = cast_byte(fs->nactvar); + f->numparams = cast_byte(fs->nactvar) - f->is_vararg; luaK_reserveregs(fs, fs->nactvar); /* reserve register for parameters */ } @@ -975,7 +974,7 @@ static void simpleexp (LexState *ls, expdesc *v) { } case TK_DOTS: { /* vararg */ FuncState *fs = ls->fs; - int lastparam = fs->f->numparams - 1; + int lastparam = fs->f->numparams; check_condition(ls, fs->f->is_vararg, "cannot use '...' outside a vararg function"); init_exp(v, VVARARG, luaK_codeABC(fs, OP_VARARG, 0, 1, lastparam)); @@ -1676,7 +1675,7 @@ static void mainfunc (LexState *ls, FuncState *fs) { expdesc v; open_func(ls, fs, &bl); fs->f->is_vararg = 1; /* main function is always declared vararg */ - fs->f->numparams = 1; + fs->f->numparams = 0; new_localvarliteral(ls, "_ARG"); adjustlocalvars(ls, 1); luaK_reserveregs(fs, 1); /* reserve register for vararg */ diff --git a/ltm.c b/ltm.c index d30bef2f8d..049c1712a7 100644 --- a/ltm.c +++ b/ltm.c @@ -1,5 +1,5 @@ /* -** $Id: ltm.c,v 2.51 2017/11/30 15:37:16 roberto Exp roberto $ +** $Id: ltm.c,v 2.52 2017/12/13 18:32:09 roberto Exp roberto $ ** Tag methods ** See Copyright Notice in lua.h */ @@ -219,7 +219,7 @@ void luaT_adjustvarargs (lua_State *L, Proto *p, int actual) { int i; Table *vtab; TValue nname; - int nfixparams = p->numparams - 1; /* number of fixed parameters */ + int nfixparams = p->numparams; /* number of fixed parameters */ actual -= nfixparams; /* number of extra arguments */ vtab = luaH_new(L); /* create vararg table */ sethvalue2s(L, L->top, vtab); /* anchor it for resizing */ From 4b6928e7f524e2c2740fc1f9d16e130e0e2f4172 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 15 Dec 2017 16:35:22 -0200 Subject: [PATCH 0165/1145] (1 << 31) with signed integer has undefined behavior in C --- lopcodes.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lopcodes.h b/lopcodes.h index f3c3e05595..3338f717d4 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -1,5 +1,5 @@ /* -** $Id: lopcodes.h,v 1.176 2017/12/04 17:41:30 roberto Exp roberto $ +** $Id: lopcodes.h,v 1.177 2017/12/13 18:32:09 roberto Exp roberto $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ @@ -121,7 +121,7 @@ enum OpMode {iABC, iABx, iAsBx, iAx, isJ}; /* basic instruction formats */ #define GETARG_sC(i) (GETARG_C(i) - MAXARG_sC) #define SETARG_C(i,v) setarg(i, v, POS_C, SIZE_C) -#define TESTARG_k(i) (cast(int, ((i) & (1 << POS_k)))) +#define TESTARG_k(i) (cast(int, ((i) & (1u << POS_k)))) #define GETARG_k(i) getarg(i, POS_k, 1) #define SETARG_k(i,v) setarg(i, v, POS_k, 1) From f8c1c1469aac2253f0787c0cb7cc228b1a57e738 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 15 Dec 2017 16:53:48 -0200 Subject: [PATCH 0166/1145] some cleaning on signed opcode parameters --- lcode.c | 44 ++++++++++++++++++++++++++++++++------------ lopcodes.h | 29 ++++++++++++++++------------- ltests.c | 4 ++-- 3 files changed, 50 insertions(+), 27 deletions(-) diff --git a/lcode.c b/lcode.c index 660e5270a5..162b732d61 100644 --- a/lcode.c +++ b/lcode.c @@ -1,5 +1,5 @@ /* -** $Id: lcode.c,v 2.143 2017/12/13 18:32:09 roberto Exp roberto $ +** $Id: lcode.c,v 2.144 2017/12/14 14:24:02 roberto Exp roberto $ ** Code generator for Lua ** See Copyright Notice in lua.h */ @@ -108,7 +108,7 @@ static void fixjump (FuncState *fs, int pc, int dest) { Instruction *jmp = &fs->f->code[pc]; int offset = dest - (pc + 1); lua_assert(dest != NO_JUMP); - if (abs(offset) > MAXARG_sJ) + if (!(-OFFSET_sJ <= offset && offset <= MAXARG_sJ - OFFSET_sJ)) luaX_syntaxerror(fs->ls, "control structure too long"); lua_assert(GET_OPCODE(*jmp) == OP_JMP); SETARG_sJ(*jmp, offset); @@ -360,7 +360,7 @@ int luaK_codeABCk (FuncState *fs, OpCode o, int a, int b, int c, int k) { } -#define codeABsC(fs,o,a,b,c,k) luaK_codeABCk(fs,o,a,b,((c) + MAXARG_sC),k) +#define codeABsC(fs,o,a,b,c,k) luaK_codeABCk(fs,o,a,b,((c) + OFFSET_sC),k) @@ -378,7 +378,7 @@ int luaK_codeABx (FuncState *fs, OpCode o, int a, unsigned int bc) { ** Format and emit an 'iAsBx' instruction. */ int luaK_codeAsBx (FuncState *fs, OpCode o, int a, int bc) { - unsigned int b = bc + MAXARG_sBx; + unsigned int b = bc + OFFSET_sBx; lua_assert(getOpMode(o) == iAsBx); lua_assert(a <= MAXARG_A && b <= MAXARG_Bx); return luaK_code(fs, CREATE_ABx(o, a, b)); @@ -389,7 +389,7 @@ int luaK_codeAsBx (FuncState *fs, OpCode o, int a, int bc) { ** Format and emit an 'isJ' instruction. */ static int codesJ (FuncState *fs, OpCode o, int sj, int k) { - unsigned int j = sj + MAXARG_sJ; + unsigned int j = sj + OFFSET_sJ; lua_assert(getOpMode(o) == isJ); lua_assert(j <= MAXARG_sJ && (k & ~1) == 0); return luaK_code(fs, CREATE_sJ(o, j, k)); @@ -582,8 +582,26 @@ static int nilK (FuncState *fs) { } +/* +** Check whether 'i' can be stored in an 'sC' operand. +** Equivalent to (0 <= i + OFFSET_sC && i + OFFSET_sC <= MAXARG_C) +** but without risk of overflows in the addition. +*/ +static int fitsC (lua_Integer i) { + return (-OFFSET_sC <= i && i <= MAXARG_C - OFFSET_sC); +} + + +/* +** Check whether 'i' can be stored in an 'sBx' operand. +*/ +static int fitsBx (lua_Integer i) { + return (-OFFSET_sBx <= i && i <= MAXARG_Bx - OFFSET_sBx); +} + + void luaK_int (FuncState *fs, int reg, lua_Integer i) { - if (l_castS2U(i) + MAXARG_sBx <= l_castS2U(MAXARG_Bx)) + if (fitsBx(i)) luaK_codeAsBx(fs, OP_LOADI, reg, cast_int(i)); else luaK_codek(fs, reg, luaK_intK(fs, i)); @@ -593,8 +611,7 @@ void luaK_int (FuncState *fs, int reg, lua_Integer i) { static int floatI (lua_Number f, lua_Integer *fi) { TValue v; setfltvalue(&v, f); - return (luaV_flttointeger(&v, fi, 0) && - l_castS2U(*fi) + MAXARG_sBx <= l_castS2U(MAXARG_Bx)); + return (luaV_flttointeger(&v, fi, 0) && fitsBx(*fi)); } @@ -1089,8 +1106,7 @@ static int isCint (expdesc *e) { ** proper range to fit in register sC */ static int isSCint (expdesc *e) { - return (e->k == VKINT && !hasjumps(e) && - l_castS2U(e->u.ival + MAXARG_sC) <= l_castS2U(MAXARG_C)); + return (e->k == VKINT && !hasjumps(e) && fitsC(e->u.ival)); } @@ -1103,8 +1119,12 @@ static int isSCnumber (expdesc *e, lua_Integer *i) { *i = e->u.ival; else if (!(e->k == VKFLT && floatI(e->u.nval, i))) return 0; /* not a number */ - *i += MAXARG_sC; - return (!hasjumps(e) && l_castS2U(*i) <= l_castS2U(MAXARG_C)); + if (!hasjumps(e) && fitsC(*i)) { + *i += OFFSET_sC; + return 1; + } + else + return 0; } diff --git a/lopcodes.h b/lopcodes.h index 3338f717d4..c3269dffdf 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -1,5 +1,5 @@ /* -** $Id: lopcodes.h,v 1.177 2017/12/13 18:32:09 roberto Exp roberto $ +** $Id: lopcodes.h,v 1.178 2017/12/15 18:35:22 roberto Exp roberto $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ @@ -62,13 +62,14 @@ enum OpMode {iABC, iABx, iAsBx, iAx, isJ}; /* basic instruction formats */ ** so they must fit in LUAI_BITSINT-1 bits (-1 for sign) */ #if SIZE_Bx < LUAI_BITSINT-1 -#define MAXARG_Bx ((1<>1) /* 'sBx' is signed */ +#define MAXARG_Bx ((1<>1) /* 'sBx' is signed */ + + #if SIZE_Ax < LUAI_BITSINT-1 #define MAXARG_Ax ((1<> 1) + #define MAXARG_A ((1<> 1) +#define OFFSET_sC (MAXARG_C >> 1) #define MAXARG_Cx ((1<<(SIZE_C + 1))-1) @@ -114,11 +117,11 @@ enum OpMode {iABC, iABx, iAsBx, iAx, isJ}; /* basic instruction formats */ #define SETARG_A(i,v) setarg(i, v, POS_A, SIZE_A) #define GETARG_B(i) check_exp(checkopm(i, iABC), getarg(i, POS_B, SIZE_B)) -#define GETARG_sB(i) (GETARG_B(i) - MAXARG_sC) +#define GETARG_sB(i) (GETARG_B(i) - OFFSET_sC) #define SETARG_B(i,v) setarg(i, v, POS_B, SIZE_B) #define GETARG_C(i) check_exp(checkopm(i, iABC), getarg(i, POS_C, SIZE_C)) -#define GETARG_sC(i) (GETARG_C(i) - MAXARG_sC) +#define GETARG_sC(i) (GETARG_C(i) - OFFSET_sC) #define SETARG_C(i,v) setarg(i, v, POS_C, SIZE_C) #define TESTARG_k(i) (cast(int, ((i) & (1u << POS_k)))) @@ -132,13 +135,13 @@ enum OpMode {iABC, iABx, iAsBx, iAx, isJ}; /* basic instruction formats */ #define SETARG_Ax(i,v) setarg(i, v, POS_Ax, SIZE_Ax) #define GETARG_sBx(i) \ - check_exp(checkopm(i, iAsBx), getarg(i, POS_Bx, SIZE_Bx) - MAXARG_sBx) -#define SETARG_sBx(i,b) SETARG_Bx((i),cast(unsigned int, (b)+MAXARG_sBx)) + check_exp(checkopm(i, iAsBx), getarg(i, POS_Bx, SIZE_Bx) - OFFSET_sBx) +#define SETARG_sBx(i,b) SETARG_Bx((i),cast(unsigned int, (b)+OFFSET_sBx)) #define GETARG_sJ(i) \ - check_exp(checkopm(i, isJ), getarg(i, POS_sJ, SIZE_sJ) - MAXARG_sJ) + check_exp(checkopm(i, isJ), getarg(i, POS_sJ, SIZE_sJ) - OFFSET_sJ) #define SETARG_sJ(i,j) \ - setarg(i, cast(unsigned int, (j)+MAXARG_sJ), POS_sJ, SIZE_sJ) + setarg(i, cast(unsigned int, (j)+OFFSET_sJ), POS_sJ, SIZE_sJ) #define CREATE_ABCk(o,a,b,c,k) ((cast(Instruction, o)< Date: Mon, 18 Dec 2017 10:33:54 -0200 Subject: [PATCH 0167/1145] details (cleaning uses of 'exp1') --- lparser.c | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/lparser.c b/lparser.c index 9d4d5effd1..8f25eec34d 100644 --- a/lparser.c +++ b/lparser.c @@ -1,5 +1,5 @@ /* -** $Id: lparser.c,v 2.171 2017/12/14 14:24:02 roberto Exp roberto $ +** $Id: lparser.c,v 2.172 2017/12/15 13:07:10 roberto Exp roberto $ ** Lua Parser ** See Copyright Notice in lua.h */ @@ -654,7 +654,7 @@ struct ConsControl { static void recfield (LexState *ls, struct ConsControl *cc) { - /* recfield -> (NAME | '['exp1']') = exp1 */ + /* recfield -> (NAME | '['exp']') = exp */ FuncState *fs = ls->fs; int reg = ls->fs->freereg; expdesc tab, key, val; @@ -916,7 +916,7 @@ static void suffixedexp (LexState *ls, expdesc *v) { fieldsel(ls, v); break; } - case '[': { /* '[' exp1 ']' */ + case '[': { /* '[' exp ']' */ expdesc key; luaK_exp2anyregup(fs, v); yindex(ls, &key); @@ -1313,14 +1313,11 @@ static void repeatstat (LexState *ls, int line) { } -static int exp1 (LexState *ls) { +static void exp1 (LexState *ls) { expdesc e; - int reg; expr(ls, &e); luaK_exp2nextreg(ls->fs, &e); lua_assert(e.k == VNONRELOC); - reg = e.u.info; - return reg; } @@ -1369,7 +1366,7 @@ static void forbody (LexState *ls, int base, int line, int nvars, int isnum) { static void fornum (LexState *ls, TString *varname, int line) { - /* fornum -> NAME = exp1,exp1[,exp1] forbody */ + /* fornum -> NAME = exp,exp[,exp] forbody */ FuncState *fs = ls->fs; int base = fs->freereg; new_localvarliteral(ls, "(for index)"); From 2f6f6abeba452bbbbaeb0e4d70e748e46d314894 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 18 Dec 2017 11:00:57 -0200 Subject: [PATCH 0168/1145] 'rehash' -> 'tablerehash' (to avoid name colisions when compiling Lua as a single file) --- lstring.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lstring.c b/lstring.c index 6e2836b312..60d4702de6 100644 --- a/lstring.c +++ b/lstring.c @@ -1,5 +1,5 @@ /* -** $Id: lstring.c,v 2.60 2017/12/08 17:28:25 roberto Exp roberto $ +** $Id: lstring.c,v 2.61 2017/12/12 11:52:35 roberto Exp roberto $ ** String table (keeps all strings handled by Lua) ** See Copyright Notice in lua.h */ @@ -69,7 +69,7 @@ unsigned int luaS_hashlongstr (TString *ts) { } -static void rehash (TString **vect, int osize, int nsize) { +static void tablerehash (TString **vect, int osize, int nsize) { int i; for (i = osize; i < nsize; i++) /* clear new elements */ vect[i] = NULL; @@ -97,18 +97,18 @@ void luaS_resize (lua_State *L, int nsize) { int osize = tb->size; TString **newvect; if (nsize < osize) /* shrinking table? */ - rehash(tb->hash, osize, nsize); /* remove elements from shrinking part */ + tablerehash(tb->hash, osize, nsize); /* depopulate shrinking part */ newvect = luaM_reallocvector(L, tb->hash, osize, nsize, TString*); if (newvect == NULL) { /* reallocation failed? */ if (nsize < osize) /* was it shrinking table? */ - rehash(tb->hash, nsize, osize); /* restore to original size */ + tablerehash(tb->hash, nsize, osize); /* restore to original size */ /* leave table as it was */ } else { /* allocation succeeded */ tb->hash = newvect; tb->size = nsize; if (nsize > osize) - rehash(newvect, osize, nsize); /* rehash for new size */ + tablerehash(newvect, osize, nsize); /* rehash for new size */ } } @@ -136,7 +136,7 @@ void luaS_init (lua_State *L) { TString *memerrmsg; stringtable *tb = &G(L)->strt; tb->hash = luaM_newvector(L, MINSTRTABSIZE, TString*); - rehash(tb->hash, 0, MINSTRTABSIZE); /* clear array */ + tablerehash(tb->hash, 0, MINSTRTABSIZE); /* clear array */ tb->size = MINSTRTABSIZE; /* pre-create memory-error message */ memerrmsg = luaS_newliteral(L, MEMERRMSG); From 7024f49c422956259c620fe6673ac42b33398adb Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 18 Dec 2017 11:01:49 -0200 Subject: [PATCH 0169/1145] default now is compiling without compatibility options + smaller stack size in debug mode (clang uses still more stack space when debugging). --- ltests.h | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/ltests.h b/ltests.h index 25598c0e68..001b205ce5 100644 --- a/ltests.h +++ b/ltests.h @@ -1,5 +1,5 @@ /* -** $Id: ltests.h,v 2.53 2017/11/23 16:35:54 roberto Exp roberto $ +** $Id: ltests.h,v 2.54 2017/12/07 18:51:39 roberto Exp roberto $ ** Internal Header for Debugging of the Lua Implementation ** See Copyright Notice in lua.h */ @@ -11,18 +11,18 @@ #include #include -/* test Lua with no compatibility code */ -#undef LUA_COMPAT_MATHLIB -#undef LUA_COMPAT_IPAIRS -#undef LUA_COMPAT_BITLIB -#undef LUA_COMPAT_APIINTCASTS -#undef LUA_COMPAT_FLOATSTRING -#undef LUA_COMPAT_UNPACK -#undef LUA_COMPAT_LOADERS -#undef LUA_COMPAT_LOG10 -#undef LUA_COMPAT_LOADSTRING -#undef LUA_COMPAT_MAXN -#undef LUA_COMPAT_MODULE +/* test Lua with compatibility code */ +#define LUA_COMPAT_MATHLIB +#define LUA_COMPAT_IPAIRS +#define LUA_COMPAT_BITLIB +#define LUA_COMPAT_APIINTCASTS +#define LUA_COMPAT_FLOATSTRING +#define LUA_COMPAT_UNPACK +#define LUA_COMPAT_LOADERS +#define LUA_COMPAT_LOG10 +#define LUA_COMPAT_LOADSTRING +#define LUA_COMPAT_MAXN +#define LUA_COMPAT_MODULE #define LUA_DEBUG @@ -36,7 +36,7 @@ /* compiled with -O0, Lua uses a lot of C stack space... */ #undef LUAI_MAXCCALLS -#define LUAI_MAXCCALLS 300 +#define LUAI_MAXCCALLS 200 /* to avoid warnings, and to make sure value is really unused */ #define UNUSED(x) (x=0, (void)(x)) From ab07005568a399f2e97996677472d9e6e69c9f4f Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 18 Dec 2017 13:44:44 -0200 Subject: [PATCH 0170/1145] new auxiliary function 'luaK_isKint' + removal of 'luaK_needclose', which was not being used anywhere. --- lcode.c | 26 ++++++++++---------------- lcode.h | 4 ++-- 2 files changed, 12 insertions(+), 18 deletions(-) diff --git a/lcode.c b/lcode.c index 162b732d61..04b534e2dd 100644 --- a/lcode.c +++ b/lcode.c @@ -1,5 +1,5 @@ /* -** $Id: lcode.c,v 2.144 2017/12/14 14:24:02 roberto Exp roberto $ +** $Id: lcode.c,v 2.145 2017/12/15 18:53:48 roberto Exp roberto $ ** Code generator for Lua ** See Copyright Notice in lua.h */ @@ -259,18 +259,6 @@ void luaK_patchtohere (FuncState *fs, int list) { } -/* -** Check whether some jump in given list needs a close instruction. -*/ -int luaK_needclose (FuncState *fs, int list) { - for (; list != NO_JUMP; list = getjump(fs, list)) { - if (GETARG_A(fs->f->code[list])) /* needs close? */ - return 1; - } - return 0; -} - - /* ** Correct a jump list to jump to 'target'. If 'hasclose' is true, ** 'target' contains an OP_CLOSE instruction (see first assert). @@ -1090,14 +1078,20 @@ static int isKstr (FuncState *fs, expdesc *e) { ttisshrstring(&fs->f->k[e->u.info])); } +/* +** Check whether expression 'e' is a literal integer. +*/ +int luaK_isKint (expdesc *e) { + return (e->k == VKINT && !hasjumps(e)); +} + /* ** Check whether expression 'e' is a literal integer in ** proper range to fit in register C */ static int isCint (expdesc *e) { - return (e->k == VKINT && !hasjumps(e) && - l_castS2U(e->u.ival) <= l_castS2U(MAXARG_C)); + return luaK_isKint(e) && (l_castS2U(e->u.ival) <= l_castS2U(MAXARG_C)); } @@ -1106,7 +1100,7 @@ static int isCint (expdesc *e) { ** proper range to fit in register sC */ static int isSCint (expdesc *e) { - return (e->k == VKINT && !hasjumps(e) && fitsC(e->u.ival)); + return luaK_isKint(e) && fitsC(e->u.ival); } diff --git a/lcode.h b/lcode.h index a1fd735dfc..beeba54f58 100644 --- a/lcode.h +++ b/lcode.h @@ -1,5 +1,5 @@ /* -** $Id: lcode.h,v 1.68 2017/10/04 21:56:32 roberto Exp roberto $ +** $Id: lcode.h,v 1.69 2017/11/30 13:29:18 roberto Exp roberto $ ** Code generator for Lua ** See Copyright Notice in lua.h */ @@ -55,6 +55,7 @@ LUAI_FUNC int luaK_codeABx (FuncState *fs, OpCode o, int A, unsigned int Bx); LUAI_FUNC int luaK_codeAsBx (FuncState *fs, OpCode o, int A, int Bx); LUAI_FUNC int luaK_codeABCk (FuncState *fs, OpCode o, int A, int B, int C, int k); +LUAI_FUNC int luaK_isKint (expdesc *e); LUAI_FUNC void luaK_fixline (FuncState *fs, int line); LUAI_FUNC void luaK_nil (FuncState *fs, int from, int n); LUAI_FUNC void luaK_reserveregs (FuncState *fs, int n); @@ -80,7 +81,6 @@ LUAI_FUNC void luaK_patchlist (FuncState *fs, int list, int target); void luaK_patchgoto (FuncState *fs, int list, int target, int hasclose); LUAI_FUNC void luaK_patchtohere (FuncState *fs, int list); LUAI_FUNC void luaK_patchclose (FuncState *fs, int list); -LUAI_FUNC int luaK_needclose (FuncState *fs, int list); LUAI_FUNC void luaK_concat (FuncState *fs, int *l1, int l2); LUAI_FUNC int luaK_getlabel (FuncState *fs); LUAI_FUNC void luaK_prefix (FuncState *fs, UnOpr op, expdesc *v, int line); From d388c165ef79cb9c668e28fa92242ffbc0430e54 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 18 Dec 2017 15:53:50 -0200 Subject: [PATCH 0171/1145] new opcodes 'FORLOOP1'/'FORPREP1' for "basic for" (integer variable with increment of 1) --- lopcodes.c | 6 +++++- lopcodes.h | 6 +++++- lparser.c | 51 ++++++++++++++++++++++++++++++++++++--------------- lvm.c | 28 +++++++++++++++++++++++++++- 4 files changed, 73 insertions(+), 18 deletions(-) diff --git a/lopcodes.c b/lopcodes.c index 1d2463846d..f5896ba03e 100644 --- a/lopcodes.c +++ b/lopcodes.c @@ -1,5 +1,5 @@ /* -** $Id: lopcodes.c,v 1.72 2017/12/04 17:41:30 roberto Exp roberto $ +** $Id: lopcodes.c,v 1.73 2017/12/13 18:32:09 roberto Exp roberto $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ @@ -82,6 +82,8 @@ LUAI_DDEF const char *const luaP_opnames[NUM_OPCODES+1] = { "RETURN", "RETURN0", "RETURN1", + "FORLOOP1", + "FORPREP1", "FORLOOP", "FORPREP", "TFORCALL", @@ -160,6 +162,8 @@ LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = { ,opmode(0, 0, iABC) /* OP_RETURN */ ,opmode(0, 0, iABC) /* OP_RETURN0 */ ,opmode(0, 0, iABC) /* OP_RETURN1 */ + ,opmode(0, 1, iABx) /* OP_FORLOOP1 */ + ,opmode(0, 1, iABx) /* OP_FORPREP1 */ ,opmode(0, 1, iABx) /* OP_FORLOOP */ ,opmode(0, 1, iABx) /* OP_FORPREP */ ,opmode(0, 0, iABC) /* OP_TFORCALL */ diff --git a/lopcodes.h b/lopcodes.h index c3269dffdf..ec880703a2 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -1,5 +1,5 @@ /* -** $Id: lopcodes.h,v 1.178 2017/12/15 18:35:22 roberto Exp roberto $ +** $Id: lopcodes.h,v 1.179 2017/12/15 18:53:48 roberto Exp roberto $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ @@ -267,6 +267,10 @@ OP_RETURN,/* A B return R(A), ... ,R(A+B-2) (see note) */ OP_RETURN0,/* return */ OP_RETURN1,/* A return R(A) */ +OP_FORLOOP1,/* A Bx R(A)++; + if R(A) <= R(A+1) then { pc-=Bx; R(A+3)=R(A) } */ +OP_FORPREP1,/* A Bx R(A)--; pc+=Bx */ + OP_FORLOOP,/* A Bx R(A)+=R(A+2); if R(A) fs, &e); lua_assert(e.k == VNONRELOC); + return res; } @@ -1337,29 +1346,37 @@ static void fixforjump (FuncState *fs, int pc, int dest, int back) { } -static void forbody (LexState *ls, int base, int line, int nvars, int isnum) { +/* +** Generate code for a 'for' loop. 'kind' can be zero (a common for +** loop), one (a basic for loop, with integer values and increment of +** 1), or two (a generic for loop). +*/ +static void forbody (LexState *ls, int base, int line, int nvars, int kind) { /* forbody -> DO block */ BlockCnt bl; FuncState *fs = ls->fs; int prep, endfor; adjustlocalvars(ls, 3); /* control variables */ checknext(ls, TK_DO); - prep = isnum ? luaK_codeABx(fs, OP_FORPREP, base, 0) : luaK_jump(fs); + prep = (kind == 0) ? luaK_codeABx(fs, OP_FORPREP, base, 0) + : (kind == 1) ? luaK_codeABx(fs, OP_FORPREP1, base, 0) + : luaK_jump(fs); enterblock(fs, &bl, 0); /* scope for declared variables */ adjustlocalvars(ls, nvars); luaK_reserveregs(fs, nvars); block(ls); leaveblock(fs); /* end of scope for declared variables */ - if (isnum) { /* numeric for? */ - fixforjump(fs, prep, luaK_getlabel(fs), 0); - endfor = luaK_codeABx(fs, OP_FORLOOP, base, 0); - } - else { /* generic for */ + if (kind == 2) { /* generic for? */ luaK_patchtohere(fs, prep); luaK_codeABC(fs, OP_TFORCALL, base, 0, nvars); luaK_fixline(fs, line); endfor = luaK_codeABx(fs, OP_TFORLOOP, base + 2, 0); } + else { + fixforjump(fs, prep, luaK_getlabel(fs), 0); + endfor = (kind == 0) ? luaK_codeABx(fs, OP_FORLOOP, base, 0) + : luaK_codeABx(fs, OP_FORLOOP1, base, 0); + } fixforjump(fs, endfor, prep + 1, 1); luaK_fixline(fs, line); } @@ -1369,21 +1386,25 @@ static void fornum (LexState *ls, TString *varname, int line) { /* fornum -> NAME = exp,exp[,exp] forbody */ FuncState *fs = ls->fs; int base = fs->freereg; + int basicfor = 1; /* true if it is a "basic" 'for' (integer + 1) */ new_localvarliteral(ls, "(for index)"); new_localvarliteral(ls, "(for limit)"); new_localvarliteral(ls, "(for step)"); new_localvar(ls, varname); checknext(ls, '='); - exp1(ls); /* initial value */ + if (!exp1(ls, 0)) /* initial value not an integer? */ + basicfor = 0; /* not a basic 'for' */ checknext(ls, ','); - exp1(ls); /* limit */ - if (testnext(ls, ',')) - exp1(ls); /* optional step */ + exp1(ls, 0); /* limit */ + if (testnext(ls, ',')) { + if (!exp1(ls, 1)) /* optional step not 1? */ + basicfor = 0; /* not a basic 'for' */ + } else { /* default step = 1 */ luaK_int(fs, fs->freereg, 1); luaK_reserveregs(fs, 1); } - forbody(ls, base, line, 1, 1); + forbody(ls, base, line, 1, basicfor); } @@ -1408,7 +1429,7 @@ static void forlist (LexState *ls, TString *indexname) { line = ls->linenumber; adjust_assign(ls, 3, explist(ls, &e), &e); luaK_checkstack(fs, 3); /* extra space to call generator */ - forbody(ls, base, line, nvars - 3, 0); + forbody(ls, base, line, nvars - 3, 2); } diff --git a/lvm.c b/lvm.c index 90fd106060..22d37bbbb7 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.324 2017/12/04 17:41:30 roberto Exp roberto $ +** $Id: lvm.c,v 2.326 2017/12/18 17:49:31 roberto Exp $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -1564,6 +1564,32 @@ void luaV_execute (lua_State *L, CallInfo *ci) { } return; } + vmcase(OP_FORLOOP1) { + lua_Integer idx = intop(+, ivalue(vra), 1); /* increment index */ + lua_Integer limit = ivalue(s2v(ra + 1)); + if (idx <= limit) { + pc -= GETARG_Bx(i); /* jump back */ + chgivalue(vra, idx); /* update internal index... */ + setivalue(s2v(ra + 3), idx); /* ...and external index */ + } + updatetrap(ci); + vmbreak; + } + vmcase(OP_FORPREP1) { + TValue *init = vra; + TValue *plimit = s2v(ra + 1); + lua_Integer ilimit, initv; + int stopnow; + if (!forlimit(plimit, &ilimit, 1, &stopnow)) { + savepc(L); /* for the error message */ + luaG_runerror(L, "'for' limit must be a number"); + } + initv = (stopnow ? 0 : ivalue(init)); + setivalue(plimit, ilimit); + setivalue(init, intop(-, initv, 1)); + pc += GETARG_Bx(i); + vmbreak; + } vmcase(OP_FORLOOP) { if (ttisinteger(vra)) { /* integer loop? */ lua_Integer step = ivalue(s2v(ra + 2)); From 3153a41e330a624fccfb7b9ade576767619b4a6b Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 19 Dec 2017 14:18:04 -0200 Subject: [PATCH 0172/1145] no need to save 'pc' in case of allocation errors (allocation errors do not call message handlers) --- lvm.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lvm.c b/lvm.c index 22d37bbbb7..29c6e373dc 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.326 2017/12/18 17:49:31 roberto Exp $ +** $Id: lvm.c,v 2.326 2017/12/18 17:53:50 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -1008,7 +1008,6 @@ void luaV_execute (lua_State *L, CallInfo *ci) { int b = GETARG_B(i); int c = GETARG_C(i); Table *t; - savepc(L); /* in case of allocation errors */ t = luaH_new(L); sethvalue2s(L, ra, t); if (b != 0 || c != 0) @@ -1679,7 +1678,6 @@ void luaV_execute (lua_State *L, CallInfo *ci) { } h = hvalue(vra); last = ((c-1)*LFIELDS_PER_FLUSH) + n; - savepc(L); /* in case of allocation errors */ if (last > h->sizearray) /* needs more space? */ luaH_resizearray(L, h, last); /* preallocate it at once */ for (; n > 0; n--) { From 4dc0be950ad67e4385400aacd25e10325a2a6e59 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 19 Dec 2017 14:40:17 -0200 Subject: [PATCH 0173/1145] new macro 'isLuacode' (to distinguish regular Lua code from hooks, where C code can run inside a Lua function). --- ldo.c | 7 ++++--- lstate.c | 4 ++-- lstate.h | 10 +++++++--- ltm.c | 6 +++--- 4 files changed, 16 insertions(+), 11 deletions(-) diff --git a/ldo.c b/ldo.c index e58efa528e..fa8a382bab 100644 --- a/ldo.c +++ b/ldo.c @@ -1,5 +1,5 @@ /* -** $Id: ldo.c,v 2.180 2017/12/12 11:57:30 roberto Exp roberto $ +** $Id: ldo.c,v 2.181 2017/12/15 13:07:10 roberto Exp roberto $ ** Stack and Call structure of Lua ** See Copyright Notice in lua.h */ @@ -454,7 +454,7 @@ void luaD_call (lua_State *L, StkId func, int nresults) { ci->func = func; ci->top = L->top + LUA_MINSTACK; lua_assert(ci->top <= L->stack_last); - ci->callstatus = 0; + ci->callstatus = CIST_C; if (L->hookmask & LUA_MASKCALL) luaD_hook(L, LUA_HOOKCALL, -1); lua_unlock(L); @@ -479,7 +479,7 @@ void luaD_call (lua_State *L, StkId func, int nresults) { L->top = ci->top = func + 1 + fsize; lua_assert(ci->top <= L->stack_last); ci->u.l.savedpc = p->code; /* starting point */ - ci->callstatus = CIST_LUA; + ci->callstatus = 0; if (L->hookmask) callhook(L, ci, 0); luaV_execute(L, ci); /* run the function */ @@ -698,6 +698,7 @@ LUA_API int lua_yieldk (lua_State *L, int nresults, lua_KContext ctx, } L->status = LUA_YIELD; if (isLua(ci)) { /* inside a hook? */ + lua_assert(!isLuacode(ci)); api_check(L, k == NULL, "hooks cannot continue after yielding"); ci->u2.nyield = 0; /* no results */ } diff --git a/lstate.c b/lstate.c index 22702a007f..81e10851a3 100644 --- a/lstate.c +++ b/lstate.c @@ -1,5 +1,5 @@ /* -** $Id: lstate.c,v 2.147 2017/11/13 15:36:52 roberto Exp roberto $ +** $Id: lstate.c,v 2.148 2017/11/23 16:35:54 roberto Exp roberto $ ** Global State ** See Copyright Notice in lua.h */ @@ -177,7 +177,7 @@ static void stack_init (lua_State *L1, lua_State *L) { /* initialize first ci */ ci = &L1->base_ci; ci->next = ci->previous = NULL; - ci->callstatus = 0; + ci->callstatus = CIST_C; ci->func = L1->top; setnilvalue(s2v(L1->top)); /* 'function' entry for this 'ci' */ L1->top++; diff --git a/lstate.h b/lstate.h index fad5463458..bc4df4e561 100644 --- a/lstate.h +++ b/lstate.h @@ -1,5 +1,5 @@ /* -** $Id: lstate.h,v 2.151 2017/11/13 15:36:52 roberto Exp roberto $ +** $Id: lstate.h,v 2.152 2017/11/23 16:35:54 roberto Exp roberto $ ** Global State ** See Copyright Notice in lua.h */ @@ -112,7 +112,7 @@ typedef struct CallInfo { ** Bits in CallInfo status */ #define CIST_OAH (1<<0) /* original value of 'allowhook' */ -#define CIST_LUA (1<<1) /* call is running a Lua function */ +#define CIST_C (1<<1) /* call is running a C function */ #define CIST_HOOKED (1<<2) /* call is running a debug hook */ #define CIST_YPCALL (1<<3) /* call is a yieldable protected call */ #define CIST_TAIL (1<<4) /* call was tail called */ @@ -120,7 +120,11 @@ typedef struct CallInfo { #define CIST_LEQ (1<<6) /* using __lt for __le */ #define CIST_FIN (1<<7) /* call is running a finalizer */ -#define isLua(ci) ((ci)->callstatus & CIST_LUA) +/* active function is a Lua function */ +#define isLua(ci) (!((ci)->callstatus & CIST_C)) + +/* call is running Lua code (not a hook) */ +#define isLuacode(ci) (!((ci)->callstatus & (CIST_C | CIST_HOOKED))) /* assume that CIST_OAH has offset 0 and that 'v' is strictly 0/1 */ #define setoah(st,v) ((st) = ((st) & ~CIST_OAH) | (v)) diff --git a/ltm.c b/ltm.c index 049c1712a7..5906a2fdae 100644 --- a/ltm.c +++ b/ltm.c @@ -1,5 +1,5 @@ /* -** $Id: ltm.c,v 2.52 2017/12/13 18:32:09 roberto Exp roberto $ +** $Id: ltm.c,v 2.53 2017/12/15 13:07:10 roberto Exp roberto $ ** Tag methods ** See Copyright Notice in lua.h */ @@ -108,7 +108,7 @@ void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1, setobj2s(L, func + 3, p3); /* 3rd argument */ L->top += 4; /* metamethod may yield only when called from Lua code */ - if (isLua(L->ci)) + if (isLuacode(L->ci)) luaD_call(L, func, 0); else luaD_callnoyield(L, func, 0); @@ -124,7 +124,7 @@ void luaT_callTMres (lua_State *L, const TValue *f, const TValue *p1, setobj2s(L, func + 2, p2); /* 2nd argument */ L->top += 3; /* metamethod may yield only when called from Lua code */ - if (isLua(L->ci)) + if (isLuacode(L->ci)) luaD_call(L, func, 1); else luaD_callnoyield(L, func, 1); From 1d5b885437286a307a77b5d12756d73d374efd54 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 20 Dec 2017 12:58:05 -0200 Subject: [PATCH 0174/1145] when running Lua code, there is no need to keep 'L->top' "correct"; set it only when needed. --- ldebug.c | 19 ++++++++++--------- ldo.c | 9 ++++++--- lgc.c | 14 ++++++++++++-- ltm.c | 26 +++++++++++++++++++------- lvm.c | 26 ++++++-------------------- 5 files changed, 53 insertions(+), 41 deletions(-) diff --git a/ldebug.c b/ldebug.c index 11d2b6a7f4..80c19d3147 100644 --- a/ldebug.c +++ b/ldebug.c @@ -1,5 +1,5 @@ /* -** $Id: ldebug.c,v 2.148 2017/12/13 18:32:09 roberto Exp roberto $ +** $Id: ldebug.c,v 2.149 2017/12/15 13:07:10 roberto Exp roberto $ ** Debug Interface ** See Copyright Notice in lua.h */ @@ -189,14 +189,10 @@ static const char *upvalname (Proto *p, int uv) { static const char *findlocal (lua_State *L, CallInfo *ci, int n, StkId *pos) { - const char *name = NULL; - StkId base; - if (isLua(ci)) { - base = ci->func + 1; - name = luaF_getlocalname(ci_func(ci)->p, n, currentpc(ci)); - } - else - base = ci->func + 1; + StkId base = ci->func + 1; + const char *name = (isLua(ci)) + ? luaF_getlocalname(ci_func(ci)->p, n, currentpc(ci)) + : NULL; if (name == NULL) { /* no 'standard' name? */ StkId limit = (ci == L->ci) ? L->top : ci->next->func; if (limit - base >= n && n > 0) /* is 'n' inside 'ci' stack? */ @@ -741,6 +737,8 @@ l_noret luaG_runerror (lua_State *L, const char *fmt, ...) { const char *msg; va_list argp; luaC_checkGC(L); /* error message uses memory */ + if (isLuacode(ci)) + L->top = ci->top; /* prepare top */ va_start(argp, fmt); msg = luaO_pushvfstring(L, fmt, argp); /* format message */ va_end(argp); @@ -764,6 +762,7 @@ static int changedline (Proto *p, int oldpc, int newpc) { void luaG_traceexec (lua_State *L) { + ptrdiff_t oldtop = savestack(L, L->top); CallInfo *ci = L->ci; lu_byte mask = L->hookmask; int counthook = (--L->hookcount == 0 && (mask & LUA_MASKCOUNT)); @@ -775,6 +774,7 @@ void luaG_traceexec (lua_State *L) { ci->callstatus &= ~CIST_HOOKYIELD; /* erase mark */ return; /* do not call hook again (VM yielded, so it did not move) */ } + L->top = ci->top; /* prepare top */ if (counthook) luaD_hook(L, LUA_HOOKCOUNT, -1); /* call count hook */ if (mask & LUA_MASKLINE) { @@ -789,6 +789,7 @@ void luaG_traceexec (lua_State *L) { } L->oldpc = npc; } + L->top = restorestack(L, oldtop); if (L->status == LUA_YIELD) { /* did hook yield? */ if (counthook) L->hookcount = 1; /* undo decrement to zero */ diff --git a/ldo.c b/ldo.c index fa8a382bab..77c408ba1c 100644 --- a/ldo.c +++ b/ldo.c @@ -1,5 +1,5 @@ /* -** $Id: ldo.c,v 2.181 2017/12/15 13:07:10 roberto Exp roberto $ +** $Id: ldo.c,v 2.182 2017/12/19 16:40:17 roberto Exp roberto $ ** Stack and Call structure of Lua ** See Copyright Notice in lua.h */ @@ -299,6 +299,7 @@ static void callhook (lua_State *L, CallInfo *ci, int istail) { ci->u.l.trap = 1; if (!(L->hookmask & LUA_MASKCALL)) return; /* some other hook */ + L->top = ci->top; /* prepare top */ ci->u.l.savedpc++; /* hooks assume 'pc' is already incremented */ if (istail) { ci->callstatus |= CIST_TAIL; @@ -312,6 +313,8 @@ static void callhook (lua_State *L, CallInfo *ci, int istail) { static void rethook (lua_State *L, CallInfo *ci) { + if (isLuacode(ci)) + L->top = ci->top; /* prepare top */ if (L->hookmask & LUA_MASKRET) /* is return hook on? */ luaD_hook(L, LUA_HOOKRET, -1); /* call it */ if (isLua(ci->previous)) @@ -421,7 +424,7 @@ void luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, int n) { L->top -= (func - ci->func); /* move down top */ luaT_adjustvarargs(L, p, n - 1); } - L->top = ci->top = ci->func + 1 + fsize; /* top for new function */ + ci->top = ci->func + 1 + fsize; /* top for new function */ lua_assert(ci->top <= L->stack_last); ci->u.l.savedpc = p->code; /* starting point */ ci->callstatus |= CIST_TAIL; @@ -476,7 +479,7 @@ void luaD_call (lua_State *L, StkId func, int nresults) { ci = next_ci(L); /* now 'enter' new function */ ci->nresults = nresults; ci->func = func; - L->top = ci->top = func + 1 + fsize; + ci->top = func + 1 + fsize; lua_assert(ci->top <= L->stack_last); ci->u.l.savedpc = p->code; /* starting point */ ci->callstatus = 0; diff --git a/lgc.c b/lgc.c index 8d28d3bf14..37bcdb8de1 100644 --- a/lgc.c +++ b/lgc.c @@ -1,5 +1,5 @@ /* -** $Id: lgc.c,v 2.241 2017/12/01 17:38:49 roberto Exp roberto $ +** $Id: lgc.c,v 2.242 2017/12/08 17:28:25 roberto Exp roberto $ ** Garbage Collector ** See Copyright Notice in lua.h */ @@ -569,13 +569,23 @@ static int traverseLclosure (global_State *g, LClosure *cl) { } +/* +** Traverse a thread, marking the elements in the stack up to its top +** and cleaning the rest of the stack in the last traversal. +** That ensures that the entire stack have valid (non-dead) objects. +** In an emergency collection running Lua code, 'L->top' may not be +** update. In that case, traverse at least up to 'ci->top'. +*/ static int traversethread (global_State *g, lua_State *th) { StkId o = th->stack; + StkId top = th->top; if (o == NULL) return 1; /* stack not completely built yet */ lua_assert(g->gcstate == GCSatomic || th->openupval == NULL || isintwups(th)); - for (; o < th->top; o++) /* mark live elements in the stack */ + if (g->gcemergency && isLuacode(th->ci) && top < th->ci->top) + top = th->ci->top; + for (; o < top; o++) /* mark live elements in the stack */ markvalue(g, s2v(o)); if (g->gcstate == GCSatomic) { /* final traversal? */ StkId lim = th->stack + th->stacksize; /* real end of stack */ diff --git a/ltm.c b/ltm.c index 5906a2fdae..d41ab98dad 100644 --- a/ltm.c +++ b/ltm.c @@ -1,5 +1,5 @@ /* -** $Id: ltm.c,v 2.53 2017/12/15 13:07:10 roberto Exp roberto $ +** $Id: ltm.c,v 2.54 2017/12/19 16:40:17 roberto Exp roberto $ ** Tag methods ** See Copyright Notice in lua.h */ @@ -101,12 +101,12 @@ const char *luaT_objtypename (lua_State *L, const TValue *o) { void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1, const TValue *p2, const TValue *p3) { - StkId func = L->top; + StkId func = (isLuacode(L->ci)) ? L->ci->top : L->top; setobj2s(L, func, f); /* push function (assume EXTRA_STACK) */ setobj2s(L, func + 1, p1); /* 1st argument */ setobj2s(L, func + 2, p2); /* 2nd argument */ setobj2s(L, func + 3, p3); /* 3rd argument */ - L->top += 4; + L->top = func + 4; /* metamethod may yield only when called from Lua code */ if (isLuacode(L->ci)) luaD_call(L, func, 0); @@ -115,8 +115,8 @@ void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1, } -void luaT_callTMres (lua_State *L, const TValue *f, const TValue *p1, - const TValue *p2, StkId res) { +static void reallycallTMres (lua_State *L, const TValue *f, const TValue *p1, + const TValue *p2, StkId res) { ptrdiff_t result = savestack(L, res); StkId func = L->top; setobj2s(L, func, f); /* push function (assume EXTRA_STACK) */ @@ -129,7 +129,15 @@ void luaT_callTMres (lua_State *L, const TValue *f, const TValue *p1, else luaD_callnoyield(L, func, 1); res = restorestack(L, result); - setobjs2s(L, res, --L->top); /* more result to its place */ + setobjs2s(L, res, --L->top); /* move result to its place */ +} + + +void luaT_callTMres (lua_State *L, const TValue *f, const TValue *p1, + const TValue *p2, StkId res) { + if (isLuacode(L->ci)) + L->top = L->ci->top; /* prepare top */ + reallycallTMres(L, f, p1, p2, res); } @@ -139,13 +147,15 @@ static int callbinTM (lua_State *L, const TValue *p1, const TValue *p2, if (ttisnil(tm)) tm = luaT_gettmbyobj(L, p2, event); /* try second operand */ if (ttisnil(tm)) return 0; - luaT_callTMres(L, tm, p1, p2, res); + reallycallTMres(L, tm, p1, p2, res); return 1; } void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2, StkId res, TMS event) { + if (event != TM_CONCAT && isLuacode(L->ci)) + L->top = L->ci->top; /* prepare top */ if (!callbinTM(L, p1, p2, res, event)) { switch (event) { case TM_CONCAT: @@ -185,6 +195,8 @@ void luaT_trybiniTM (lua_State *L, const TValue *p1, int i2, int luaT_callorderTM (lua_State *L, const TValue *p1, const TValue *p2, TMS event) { + if (isLuacode(L->ci)) + L->top = L->ci->top; /* prepare top */ if (callbinTM(L, p1, p2, L->top, event)) /* try original event */ return !l_isfalse(s2v(L->top)); else if (event == TM_LE) { diff --git a/lvm.c b/lvm.c index 29c6e373dc..8039770ef4 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.326 2017/12/18 17:53:50 roberto Exp roberto $ +** $Id: lvm.c,v 2.327 2017/12/19 16:18:04 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -464,6 +464,8 @@ int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2) { } if (tm == NULL) /* no TM? */ return 0; /* objects are different */ + if (isLuacode(L->ci)) + L->top = L->ci->top; /* prepare top */ luaT_callTMres(L, tm, t1, t2, L->top); /* call TM */ return !l_isfalse(s2v(L->top)); } @@ -726,20 +728,10 @@ void luaV_finishOp (lua_State *L) { } /* move final result to final position */ setobjs2s(L, ci->func + 1 + GETARG_A(inst), L->top - 1); - L->top = ci->top; /* restore top */ break; } - case OP_TFORCALL: { - lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_TFORLOOP); - L->top = ci->top; /* correct top */ - break; - } - case OP_CALL: { - if (GETARG_C(inst) - 1 >= 0) /* nresults >= 0? */ - L->top = ci->top; /* adjust results */ - break; - } - case OP_TAILCALL: case OP_SETTABUP: case OP_SETTABLE: + case OP_TFORCALL: case OP_CALL: case OP_TAILCALL: + case OP_SETTABUP: case OP_SETTABLE: case OP_SETI: case OP_SETFIELD: break; default: lua_assert(0); @@ -808,7 +800,7 @@ void luaV_finishOp (lua_State *L) { #define checkGC(L,c) \ { luaC_condGC(L, L->top = (c), /* limit of live values */ \ - (L->top = ci->top, updatetrap(ci))); /* restore top */ \ + updatetrap(ci)); \ luai_threadyield(L); } @@ -1387,7 +1379,6 @@ void luaV_execute (lua_State *L, CallInfo *ci) { rb = base + b; setobjs2s(L, ra, rb); checkGC(L, (ra >= rb ? ra + 1 : rb)); - L->top = ci->top; /* restore top */ vmbreak; } vmcase(OP_CLOSE) { @@ -1491,9 +1482,6 @@ void luaV_execute (lua_State *L, CallInfo *ci) { L->top = ra + b; /* top signals number of arguments */ /* else previous instruction set top */ Protect(luaD_call(L, ra, nresults)); - if (nresults >= 0) /* fixed number of results? */ - L->top = ci->top; /* correct top */ - /* else leave top for next instruction */ vmbreak; } vmcase(OP_TAILCALL) { @@ -1651,7 +1639,6 @@ void luaV_execute (lua_State *L, CallInfo *ci) { setobjs2s(L, cb, ra); L->top = cb + 3; /* func. + 2 args (state and index) */ Protect(luaD_call(L, cb, GETARG_C(i))); - L->top = ci->top; if (trap) /* keep 'base' correct for next instruction */ updatebase(ci); i = *(pc++); /* go to next instruction */ @@ -1686,7 +1673,6 @@ void luaV_execute (lua_State *L, CallInfo *ci) { last--; luaC_barrierback(L, h, val); } - L->top = ci->top; /* correct top (in case of previous open call) */ vmbreak; } vmcase(OP_CLOSURE) { From 4676f6599e04d4eaa78c050616e94994e6a36396 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 22 Dec 2017 12:16:46 -0200 Subject: [PATCH 0175/1145] new macros 'isOT'/'isIT' (plus exchanged parameters of OP_VARARG to make it similar to other 'isOT' instructions) --- lcode.c | 14 ++--- lopcodes.c | 152 ++++++++++++++++++++++++++--------------------------- lopcodes.h | 20 +++++-- lparser.c | 4 +- lvm.c | 8 +-- 5 files changed, 104 insertions(+), 94 deletions(-) diff --git a/lcode.c b/lcode.c index 04b534e2dd..521f597a79 100644 --- a/lcode.c +++ b/lcode.c @@ -1,5 +1,5 @@ /* -** $Id: lcode.c,v 2.145 2017/12/15 18:53:48 roberto Exp roberto $ +** $Id: lcode.c,v 2.146 2017/12/18 15:44:44 roberto Exp roberto $ ** Code generator for Lua ** See Copyright Notice in lua.h */ @@ -618,12 +618,11 @@ static void luaK_float (FuncState *fs, int reg, lua_Number f) { ** or 'nresults' is LUA_MULTRET (as any expression can satisfy that). */ void luaK_setreturns (FuncState *fs, expdesc *e, int nresults) { - if (e->k == VCALL) { /* expression is an open function call? */ - SETARG_C(getinstruction(fs, e), nresults + 1); - } + Instruction *pc = &getinstruction(fs, e); + if (e->k == VCALL) /* expression is an open function call? */ + SETARG_C(*pc, nresults + 1); else if (e->k == VVARARG) { - Instruction *pc = &getinstruction(fs, e); - SETARG_B(*pc, nresults + 1); + SETARG_C(*pc, nresults + 1); SETARG_A(*pc, fs->freereg); luaK_reserveregs(fs, 1); } @@ -649,7 +648,7 @@ void luaK_setoneret (FuncState *fs, expdesc *e) { e->u.info = GETARG_A(getinstruction(fs, e)); } else if (e->k == VVARARG) { - SETARG_B(getinstruction(fs, e), 2); + SETARG_C(getinstruction(fs, e), 2); e->k = VRELOC; /* can relocate its simple result */ } } @@ -1623,6 +1622,7 @@ void luaK_finish (FuncState *fs) { Proto *p = fs->f; for (i = 0; i < fs->pc; i++) { Instruction *pc = &p->code[i]; + lua_assert(isOT(*pc) == isIT(*(pc + 1))); switch (GET_OPCODE(*pc)) { case OP_RETURN: case OP_RETURN0: case OP_RETURN1: case OP_TAILCALL: { diff --git a/lopcodes.c b/lopcodes.c index f5896ba03e..a5867d2358 100644 --- a/lopcodes.c +++ b/lopcodes.c @@ -1,5 +1,5 @@ /* -** $Id: lopcodes.c,v 1.73 2017/12/13 18:32:09 roberto Exp roberto $ +** $Id: lopcodes.c,v 1.74 2017/12/18 17:49:31 roberto Exp $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ @@ -97,80 +97,80 @@ LUAI_DDEF const char *const luaP_opnames[NUM_OPCODES+1] = { LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = { -/* T A mode opcode */ - opmode(0, 1, iABC) /* OP_MOVE */ - ,opmode(0, 1, iAsBx) /* OP_LOADI */ - ,opmode(0, 1, iAsBx) /* OP_LOADF */ - ,opmode(0, 1, iABx) /* OP_LOADK */ - ,opmode(0, 1, iABx) /* OP_LOADKX */ - ,opmode(0, 1, iABC) /* OP_LOADBOOL */ - ,opmode(0, 1, iABC) /* OP_LOADNIL */ - ,opmode(0, 1, iABC) /* OP_GETUPVAL */ - ,opmode(0, 0, iABC) /* OP_SETUPVAL */ - ,opmode(0, 1, iABC) /* OP_GETTABUP */ - ,opmode(0, 1, iABC) /* OP_GETTABLE */ - ,opmode(0, 1, iABC) /* OP_GETI */ - ,opmode(0, 1, iABC) /* OP_GETFIELD */ - ,opmode(0, 0, iABC) /* OP_SETTABUP */ - ,opmode(0, 0, iABC) /* OP_SETTABLE */ - ,opmode(0, 0, iABC) /* OP_SETI */ - ,opmode(0, 0, iABC) /* OP_SETFIELD */ - ,opmode(0, 1, iABC) /* OP_NEWTABLE */ - ,opmode(0, 1, iABC) /* OP_SELF */ - ,opmode(0, 1, iABC) /* OP_ADDI */ - ,opmode(0, 1, iABC) /* OP_SUBI */ - ,opmode(0, 1, iABC) /* OP_MULI */ - ,opmode(0, 1, iABC) /* OP_MODI */ - ,opmode(0, 1, iABC) /* OP_POWI */ - ,opmode(0, 1, iABC) /* OP_DIVI */ - ,opmode(0, 1, iABC) /* OP_IDIVI */ - ,opmode(0, 1, iABC) /* OP_BANDK */ - ,opmode(0, 1, iABC) /* OP_BORK */ - ,opmode(0, 1, iABC) /* OP_BXORK */ - ,opmode(0, 1, iABC) /* OP_SHRI */ - ,opmode(0, 1, iABC) /* OP_SHLI */ - ,opmode(0, 1, iABC) /* OP_ADD */ - ,opmode(0, 1, iABC) /* OP_SUB */ - ,opmode(0, 1, iABC) /* OP_MUL */ - ,opmode(0, 1, iABC) /* OP_MOD */ - ,opmode(0, 1, iABC) /* OP_POW */ - ,opmode(0, 1, iABC) /* OP_DIV */ - ,opmode(0, 1, iABC) /* OP_IDIV */ - ,opmode(0, 1, iABC) /* OP_BAND */ - ,opmode(0, 1, iABC) /* OP_BOR */ - ,opmode(0, 1, iABC) /* OP_BXOR */ - ,opmode(0, 1, iABC) /* OP_SHL */ - ,opmode(0, 1, iABC) /* OP_SHR */ - ,opmode(0, 1, iABC) /* OP_UNM */ - ,opmode(0, 1, iABC) /* OP_BNOT */ - ,opmode(0, 1, iABC) /* OP_NOT */ - ,opmode(0, 1, iABC) /* OP_LEN */ - ,opmode(0, 1, iABC) /* OP_CONCAT */ - ,opmode(0, 0, iABC) /* OP_CLOSE */ - ,opmode(0, 0, isJ) /* OP_JMP */ - ,opmode(1, 0, iABC) /* OP_EQ */ - ,opmode(1, 0, iABC) /* OP_LT */ - ,opmode(1, 0, iABC) /* OP_LE */ - ,opmode(1, 0, iABC) /* OP_EQK */ - ,opmode(1, 0, iABC) /* OP_EQI */ - ,opmode(1, 0, iABC) /* OP_LTI */ - ,opmode(1, 0, iABC) /* OP_LEI */ - ,opmode(1, 0, iABC) /* OP_TEST */ - ,opmode(1, 1, iABC) /* OP_TESTSET */ - ,opmode(0, 1, iABC) /* OP_CALL */ - ,opmode(0, 1, iABC) /* OP_TAILCALL */ - ,opmode(0, 0, iABC) /* OP_RETURN */ - ,opmode(0, 0, iABC) /* OP_RETURN0 */ - ,opmode(0, 0, iABC) /* OP_RETURN1 */ - ,opmode(0, 1, iABx) /* OP_FORLOOP1 */ - ,opmode(0, 1, iABx) /* OP_FORPREP1 */ - ,opmode(0, 1, iABx) /* OP_FORLOOP */ - ,opmode(0, 1, iABx) /* OP_FORPREP */ - ,opmode(0, 0, iABC) /* OP_TFORCALL */ - ,opmode(0, 1, iABx) /* OP_TFORLOOP */ - ,opmode(0, 0, iABC) /* OP_SETLIST */ - ,opmode(0, 1, iABx) /* OP_CLOSURE */ - ,opmode(0, 1, iABC) /* OP_VARARG */ - ,opmode(0, 0, iAx) /* OP_EXTRAARG */ +/* OT IT T A mode opcode */ + opmode(0, 0, 0, 1, iABC) /* OP_MOVE */ + ,opmode(0, 0, 0, 1, iAsBx) /* OP_LOADI */ + ,opmode(0, 0, 0, 1, iAsBx) /* OP_LOADF */ + ,opmode(0, 0, 0, 1, iABx) /* OP_LOADK */ + ,opmode(0, 0, 0, 1, iABx) /* OP_LOADKX */ + ,opmode(0, 0, 0, 1, iABC) /* OP_LOADBOOL */ + ,opmode(0, 0, 0, 1, iABC) /* OP_LOADNIL */ + ,opmode(0, 0, 0, 1, iABC) /* OP_GETUPVAL */ + ,opmode(0, 0, 0, 0, iABC) /* OP_SETUPVAL */ + ,opmode(0, 0, 0, 1, iABC) /* OP_GETTABUP */ + ,opmode(0, 0, 0, 1, iABC) /* OP_GETTABLE */ + ,opmode(0, 0, 0, 1, iABC) /* OP_GETI */ + ,opmode(0, 0, 0, 1, iABC) /* OP_GETFIELD */ + ,opmode(0, 0, 0, 0, iABC) /* OP_SETTABUP */ + ,opmode(0, 0, 0, 0, iABC) /* OP_SETTABLE */ + ,opmode(0, 0, 0, 0, iABC) /* OP_SETI */ + ,opmode(0, 0, 0, 0, iABC) /* OP_SETFIELD */ + ,opmode(0, 0, 0, 1, iABC) /* OP_NEWTABLE */ + ,opmode(0, 0, 0, 1, iABC) /* OP_SELF */ + ,opmode(0, 0, 0, 1, iABC) /* OP_ADDI */ + ,opmode(0, 0, 0, 1, iABC) /* OP_SUBI */ + ,opmode(0, 0, 0, 1, iABC) /* OP_MULI */ + ,opmode(0, 0, 0, 1, iABC) /* OP_MODI */ + ,opmode(0, 0, 0, 1, iABC) /* OP_POWI */ + ,opmode(0, 0, 0, 1, iABC) /* OP_DIVI */ + ,opmode(0, 0, 0, 1, iABC) /* OP_IDIVI */ + ,opmode(0, 0, 0, 1, iABC) /* OP_BANDK */ + ,opmode(0, 0, 0, 1, iABC) /* OP_BORK */ + ,opmode(0, 0, 0, 1, iABC) /* OP_BXORK */ + ,opmode(0, 0, 0, 1, iABC) /* OP_SHRI */ + ,opmode(0, 0, 0, 1, iABC) /* OP_SHLI */ + ,opmode(0, 0, 0, 1, iABC) /* OP_ADD */ + ,opmode(0, 0, 0, 1, iABC) /* OP_SUB */ + ,opmode(0, 0, 0, 1, iABC) /* OP_MUL */ + ,opmode(0, 0, 0, 1, iABC) /* OP_MOD */ + ,opmode(0, 0, 0, 1, iABC) /* OP_POW */ + ,opmode(0, 0, 0, 1, iABC) /* OP_DIV */ + ,opmode(0, 0, 0, 1, iABC) /* OP_IDIV */ + ,opmode(0, 0, 0, 1, iABC) /* OP_BAND */ + ,opmode(0, 0, 0, 1, iABC) /* OP_BOR */ + ,opmode(0, 0, 0, 1, iABC) /* OP_BXOR */ + ,opmode(0, 0, 0, 1, iABC) /* OP_SHL */ + ,opmode(0, 0, 0, 1, iABC) /* OP_SHR */ + ,opmode(0, 0, 0, 1, iABC) /* OP_UNM */ + ,opmode(0, 0, 0, 1, iABC) /* OP_BNOT */ + ,opmode(0, 0, 0, 1, iABC) /* OP_NOT */ + ,opmode(0, 0, 0, 1, iABC) /* OP_LEN */ + ,opmode(0, 0, 0, 1, iABC) /* OP_CONCAT */ + ,opmode(0, 0, 0, 0, iABC) /* OP_CLOSE */ + ,opmode(0, 0, 0, 0, isJ) /* OP_JMP */ + ,opmode(0, 0, 1, 0, iABC) /* OP_EQ */ + ,opmode(0, 0, 1, 0, iABC) /* OP_LT */ + ,opmode(0, 0, 1, 0, iABC) /* OP_LE */ + ,opmode(0, 0, 1, 0, iABC) /* OP_EQK */ + ,opmode(0, 0, 1, 0, iABC) /* OP_EQI */ + ,opmode(0, 0, 1, 0, iABC) /* OP_LTI */ + ,opmode(0, 0, 1, 0, iABC) /* OP_LEI */ + ,opmode(0, 0, 1, 0, iABC) /* OP_TEST */ + ,opmode(0, 0, 1, 1, iABC) /* OP_TESTSET */ + ,opmode(1, 1, 0, 1, iABC) /* OP_CALL */ + ,opmode(1, 1, 0, 1, iABC) /* OP_TAILCALL */ + ,opmode(0, 1, 0, 0, iABC) /* OP_RETURN */ + ,opmode(0, 0, 0, 0, iABC) /* OP_RETURN0 */ + ,opmode(0, 0, 0, 0, iABC) /* OP_RETURN1 */ + ,opmode(0, 0, 0, 1, iABx) /* OP_FORLOOP1 */ + ,opmode(0, 0, 0, 1, iABx) /* OP_FORPREP1 */ + ,opmode(0, 0, 0, 1, iABx) /* OP_FORLOOP */ + ,opmode(0, 0, 0, 1, iABx) /* OP_FORPREP */ + ,opmode(0, 0, 0, 0, iABC) /* OP_TFORCALL */ + ,opmode(0, 0, 0, 1, iABx) /* OP_TFORLOOP */ + ,opmode(0, 1, 0, 0, iABC) /* OP_SETLIST */ + ,opmode(0, 0, 0, 1, iABx) /* OP_CLOSURE */ + ,opmode(1, 0, 0, 1, iABC) /* OP_VARARG */ + ,opmode(0, 0, 0, 0, iAx) /* OP_EXTRAARG */ }; diff --git a/lopcodes.h b/lopcodes.h index ec880703a2..eae0dfaf53 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -1,5 +1,5 @@ /* -** $Id: lopcodes.h,v 1.179 2017/12/15 18:53:48 roberto Exp roberto $ +** $Id: lopcodes.h,v 1.180 2017/12/18 17:49:31 roberto Exp roberto $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ @@ -282,7 +282,7 @@ OP_SETLIST,/* A B C R(A)[(C-1)*FPF+i] := R(A+i), 1 <= i <= B */ OP_CLOSURE,/* A Bx R(A) := closure(KPROTO[Bx]) */ -OP_VARARG,/* A B C R(A), R(A+1), ..., R(A+B-2) = vararg(C) */ +OP_VARARG,/* A B C R(A), R(A+1), ..., R(A+C-2) = vararg(B) */ OP_EXTRAARG/* Ax extra (larger) argument for previous opcode */ } OpCode; @@ -298,8 +298,8 @@ OP_EXTRAARG/* Ax extra (larger) argument for previous opcode */ set to last_result+1, so next open instruction (OP_CALL, OP_RETURN, OP_SETLIST) may use 'top'. - (*) In OP_VARARG, if (B == 0) then use actual number of varargs and - set top (like in OP_CALL with C == 0). C is the vararg parameter. + (*) In OP_VARARG, if (C == 0) then use actual number of varargs and + set top (like in OP_CALL with C == 0). B is the vararg parameter. (*) In OP_RETURN, if (B == 0) then return up to 'top'. @@ -330,6 +330,8 @@ OP_EXTRAARG/* Ax extra (larger) argument for previous opcode */ ** bits 0-2: op mode ** bit 3: instruction set register A ** bit 4: operator is a test (next instruction must be a jump) +** bit 5: instruction sets 'L->top' for next instruction (when C == 0) +** bit 6: instruction uses 'L->top' set by previous instruction (when B == 0) */ LUAI_DDEC const lu_byte luaP_opmodes[NUM_OPCODES]; @@ -337,8 +339,16 @@ LUAI_DDEC const lu_byte luaP_opmodes[NUM_OPCODES]; #define getOpMode(m) (cast(enum OpMode, luaP_opmodes[m] & 7)) #define testAMode(m) (luaP_opmodes[m] & (1 << 3)) #define testTMode(m) (luaP_opmodes[m] & (1 << 4)) +#define testOTMode(m) (luaP_opmodes[m] & (1 << 5)) +#define testITMode(m) (luaP_opmodes[m] & (1 << 6)) -#define opmode(t,a,m) (((t)<<4) | ((a)<<3) | (m)) +/* "out top" (set top for next instruction) */ +#define isOT(i) (testOTMode(GET_OPCODE(i)) && GETARG_C(i) == 0) + +/* "in top" (uses top from previous instruction) */ +#define isIT(i) (testITMode(GET_OPCODE(i)) && GETARG_B(i) == 0) + +#define opmode(ot,it,t,a,m) (((ot)<<5) | ((it)<<6) | ((t)<<4) | ((a)<<3) | (m)) LUAI_DDEC const char *const luaP_opnames[NUM_OPCODES+1]; /* opcode names */ diff --git a/lparser.c b/lparser.c index 6317a7889c..e99b33ffb2 100644 --- a/lparser.c +++ b/lparser.c @@ -1,5 +1,5 @@ /* -** $Id: lparser.c,v 2.173 2017/12/18 12:33:54 roberto Exp roberto $ +** $Id: lparser.c,v 2.174 2017/12/18 17:49:31 roberto Exp roberto $ ** Lua Parser ** See Copyright Notice in lua.h */ @@ -977,7 +977,7 @@ static void simpleexp (LexState *ls, expdesc *v) { int lastparam = fs->f->numparams; check_condition(ls, fs->f->is_vararg, "cannot use '...' outside a vararg function"); - init_exp(v, VVARARG, luaK_codeABC(fs, OP_VARARG, 0, 1, lastparam)); + init_exp(v, VVARARG, luaK_codeABC(fs, OP_VARARG, 0, lastparam, 1)); break; } case '{': { /* constructor */ diff --git a/lvm.c b/lvm.c index 8039770ef4..89646d247a 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.327 2017/12/19 16:18:04 roberto Exp roberto $ +** $Id: lvm.c,v 2.328 2017/12/20 14:58:05 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -1688,9 +1688,9 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_VARARG) { - int b = GETARG_B(i) - 1; /* required results */ - TValue *vtab = vRC(i); /* vararg table */ - Protect(luaT_getvarargs(L, vtab, ra, b)); + int n = GETARG_C(i) - 1; /* required results */ + TValue *vtab = vRB(i); /* vararg table */ + Protect(luaT_getvarargs(L, vtab, ra, n)); vmbreak; } vmcase(OP_EXTRAARG) { From f360e7778bb7e4be6afc4e27bd9412222b2c6c55 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 22 Dec 2017 14:43:59 -0200 Subject: [PATCH 0176/1145] assert cannot use instruction after the last --- lcode.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lcode.c b/lcode.c index 521f597a79..1eae475ab9 100644 --- a/lcode.c +++ b/lcode.c @@ -1,5 +1,5 @@ /* -** $Id: lcode.c,v 2.146 2017/12/18 15:44:44 roberto Exp roberto $ +** $Id: lcode.c,v 2.147 2017/12/22 14:16:46 roberto Exp roberto $ ** Code generator for Lua ** See Copyright Notice in lua.h */ @@ -1622,7 +1622,7 @@ void luaK_finish (FuncState *fs) { Proto *p = fs->f; for (i = 0; i < fs->pc; i++) { Instruction *pc = &p->code[i]; - lua_assert(isOT(*pc) == isIT(*(pc + 1))); + lua_assert(i == 0 || isOT(*(pc - 1)) == isIT(*pc)); switch (GET_OPCODE(*pc)) { case OP_RETURN: case OP_RETURN0: case OP_RETURN1: case OP_TAILCALL: { From f99f3c42fffe0b9672e6380c83de19db65b4f9d4 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 28 Dec 2017 09:51:00 -0200 Subject: [PATCH 0177/1145] comment --- llimits.h | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/llimits.h b/llimits.h index a939c79948..a9295645b2 100644 --- a/llimits.h +++ b/llimits.h @@ -1,5 +1,5 @@ /* -** $Id: llimits.h,v 1.146 2017/12/01 15:08:14 roberto Exp roberto $ +** $Id: llimits.h,v 1.147 2017/12/11 18:53:53 roberto Exp roberto $ ** Limits, basic types, and some other 'installation-dependent' definitions ** See Copyright Notice in lua.h */ @@ -219,8 +219,7 @@ typedef unsigned long Instruction; /* -** these macros allow user-specific actions on threads when you defined -** LUAI_EXTRASPACE and need to do something extra when a thread is +** these macros allow user-specific actions when a thread is ** created/deleted/resumed/yielded. */ #if !defined(luai_userstateopen) From 8691612f01ae4b32d861464032521d969766c1c5 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 28 Dec 2017 12:17:09 -0200 Subject: [PATCH 0178/1145] when calling a hook, cannot decrease 'ci->top' (to preserve stack size if the stack is reallocated) --- ldo.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/ldo.c b/ldo.c index 77c408ba1c..6a28e8eed1 100644 --- a/ldo.c +++ b/ldo.c @@ -1,5 +1,5 @@ /* -** $Id: ldo.c,v 2.182 2017/12/19 16:40:17 roberto Exp roberto $ +** $Id: ldo.c,v 2.183 2017/12/20 14:58:05 roberto Exp roberto $ ** Stack and Call structure of Lua ** See Copyright Notice in lua.h */ @@ -278,8 +278,8 @@ void luaD_hook (lua_State *L, int event, int line) { ar.currentline = line; ar.i_ci = ci; luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */ - ci->top = L->top + LUA_MINSTACK; - lua_assert(ci->top <= L->stack_last); + if (L->top + LUA_MINSTACK > ci->top) + ci->top = L->top + LUA_MINSTACK; L->allowhook = 0; /* cannot call hooks inside a hook */ ci->callstatus |= CIST_HOOKED; lua_unlock(L); @@ -294,7 +294,7 @@ void luaD_hook (lua_State *L, int event, int line) { } -static void callhook (lua_State *L, CallInfo *ci, int istail) { +static void hookcall (lua_State *L, CallInfo *ci, int istail) { int hook; ci->u.l.trap = 1; if (!(L->hookmask & LUA_MASKCALL)) @@ -429,7 +429,7 @@ void luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, int n) { ci->u.l.savedpc = p->code; /* starting point */ ci->callstatus |= CIST_TAIL; if (L->hookmask) - callhook(L, ci, 1); + hookcall(L, ci, 1); } @@ -484,7 +484,7 @@ void luaD_call (lua_State *L, StkId func, int nresults) { ci->u.l.savedpc = p->code; /* starting point */ ci->callstatus = 0; if (L->hookmask) - callhook(L, ci, 0); + hookcall(L, ci, 0); luaV_execute(L, ci); /* run the function */ break; } From cf7eff45f3abf2386d5392c0b431498bbb274ac7 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 28 Dec 2017 13:42:57 -0200 Subject: [PATCH 0179/1145] keep control of stack top in Lua functions concentrated in 'luaV_execute' --- ldebug.c | 9 +++------ lgc.c | 11 +++-------- ltm.c | 22 +++++----------------- lvm.c | 46 +++++++++++++++++++++++++++++----------------- 4 files changed, 40 insertions(+), 48 deletions(-) diff --git a/ldebug.c b/ldebug.c index 80c19d3147..5233560044 100644 --- a/ldebug.c +++ b/ldebug.c @@ -1,5 +1,5 @@ /* -** $Id: ldebug.c,v 2.149 2017/12/15 13:07:10 roberto Exp roberto $ +** $Id: ldebug.c,v 2.150 2017/12/20 14:58:05 roberto Exp roberto $ ** Debug Interface ** See Copyright Notice in lua.h */ @@ -737,8 +737,6 @@ l_noret luaG_runerror (lua_State *L, const char *fmt, ...) { const char *msg; va_list argp; luaC_checkGC(L); /* error message uses memory */ - if (isLuacode(ci)) - L->top = ci->top; /* prepare top */ va_start(argp, fmt); msg = luaO_pushvfstring(L, fmt, argp); /* format message */ va_end(argp); @@ -762,7 +760,6 @@ static int changedline (Proto *p, int oldpc, int newpc) { void luaG_traceexec (lua_State *L) { - ptrdiff_t oldtop = savestack(L, L->top); CallInfo *ci = L->ci; lu_byte mask = L->hookmask; int counthook = (--L->hookcount == 0 && (mask & LUA_MASKCOUNT)); @@ -774,7 +771,8 @@ void luaG_traceexec (lua_State *L) { ci->callstatus &= ~CIST_HOOKYIELD; /* erase mark */ return; /* do not call hook again (VM yielded, so it did not move) */ } - L->top = ci->top; /* prepare top */ + if (!isIT(*(ci->u.l.savedpc - 1))) + L->top = ci->top; /* prepare top */ if (counthook) luaD_hook(L, LUA_HOOKCOUNT, -1); /* call count hook */ if (mask & LUA_MASKLINE) { @@ -789,7 +787,6 @@ void luaG_traceexec (lua_State *L) { } L->oldpc = npc; } - L->top = restorestack(L, oldtop); if (L->status == LUA_YIELD) { /* did hook yield? */ if (counthook) L->hookcount = 1; /* undo decrement to zero */ diff --git a/lgc.c b/lgc.c index 37bcdb8de1..d02dc031c9 100644 --- a/lgc.c +++ b/lgc.c @@ -1,5 +1,5 @@ /* -** $Id: lgc.c,v 2.242 2017/12/08 17:28:25 roberto Exp roberto $ +** $Id: lgc.c,v 2.243 2017/12/20 14:58:05 roberto Exp roberto $ ** Garbage Collector ** See Copyright Notice in lua.h */ @@ -571,21 +571,16 @@ static int traverseLclosure (global_State *g, LClosure *cl) { /* ** Traverse a thread, marking the elements in the stack up to its top -** and cleaning the rest of the stack in the last traversal. +** and cleaning the rest of the stack in the final traversal. ** That ensures that the entire stack have valid (non-dead) objects. -** In an emergency collection running Lua code, 'L->top' may not be -** update. In that case, traverse at least up to 'ci->top'. */ static int traversethread (global_State *g, lua_State *th) { StkId o = th->stack; - StkId top = th->top; if (o == NULL) return 1; /* stack not completely built yet */ lua_assert(g->gcstate == GCSatomic || th->openupval == NULL || isintwups(th)); - if (g->gcemergency && isLuacode(th->ci) && top < th->ci->top) - top = th->ci->top; - for (; o < top; o++) /* mark live elements in the stack */ + for (; o < th->top; o++) /* mark live elements in the stack */ markvalue(g, s2v(o)); if (g->gcstate == GCSatomic) { /* final traversal? */ StkId lim = th->stack + th->stacksize; /* real end of stack */ diff --git a/ltm.c b/ltm.c index d41ab98dad..4b14f197db 100644 --- a/ltm.c +++ b/ltm.c @@ -1,5 +1,5 @@ /* -** $Id: ltm.c,v 2.54 2017/12/19 16:40:17 roberto Exp roberto $ +** $Id: ltm.c,v 2.55 2017/12/20 14:58:05 roberto Exp roberto $ ** Tag methods ** See Copyright Notice in lua.h */ @@ -101,7 +101,7 @@ const char *luaT_objtypename (lua_State *L, const TValue *o) { void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1, const TValue *p2, const TValue *p3) { - StkId func = (isLuacode(L->ci)) ? L->ci->top : L->top; + StkId func = L->top; setobj2s(L, func, f); /* push function (assume EXTRA_STACK) */ setobj2s(L, func + 1, p1); /* 1st argument */ setobj2s(L, func + 2, p2); /* 2nd argument */ @@ -115,8 +115,8 @@ void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1, } -static void reallycallTMres (lua_State *L, const TValue *f, const TValue *p1, - const TValue *p2, StkId res) { +void luaT_callTMres (lua_State *L, const TValue *f, const TValue *p1, + const TValue *p2, StkId res) { ptrdiff_t result = savestack(L, res); StkId func = L->top; setobj2s(L, func, f); /* push function (assume EXTRA_STACK) */ @@ -133,29 +133,19 @@ static void reallycallTMres (lua_State *L, const TValue *f, const TValue *p1, } -void luaT_callTMres (lua_State *L, const TValue *f, const TValue *p1, - const TValue *p2, StkId res) { - if (isLuacode(L->ci)) - L->top = L->ci->top; /* prepare top */ - reallycallTMres(L, f, p1, p2, res); -} - - static int callbinTM (lua_State *L, const TValue *p1, const TValue *p2, StkId res, TMS event) { const TValue *tm = luaT_gettmbyobj(L, p1, event); /* try first operand */ if (ttisnil(tm)) tm = luaT_gettmbyobj(L, p2, event); /* try second operand */ if (ttisnil(tm)) return 0; - reallycallTMres(L, tm, p1, p2, res); + luaT_callTMres(L, tm, p1, p2, res); return 1; } void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2, StkId res, TMS event) { - if (event != TM_CONCAT && isLuacode(L->ci)) - L->top = L->ci->top; /* prepare top */ if (!callbinTM(L, p1, p2, res, event)) { switch (event) { case TM_CONCAT: @@ -195,8 +185,6 @@ void luaT_trybiniTM (lua_State *L, const TValue *p1, int i2, int luaT_callorderTM (lua_State *L, const TValue *p1, const TValue *p2, TMS event) { - if (isLuacode(L->ci)) - L->top = L->ci->top; /* prepare top */ if (callbinTM(L, p1, p2, L->top, event)) /* try original event */ return !l_isfalse(s2v(L->top)); else if (event == TM_LE) { diff --git a/lvm.c b/lvm.c index 89646d247a..212d5f9d32 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.328 2017/12/20 14:58:05 roberto Exp roberto $ +** $Id: lvm.c,v 2.329 2017/12/22 14:16:46 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -464,8 +464,6 @@ int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2) { } if (tm == NULL) /* no TM? */ return 0; /* objects are different */ - if (isLuacode(L->ci)) - L->top = L->ci->top; /* prepare top */ luaT_callTMres(L, tm, t1, t2, L->top); /* call TM */ return !l_isfalse(s2v(L->top)); } @@ -780,20 +778,29 @@ void luaV_finishOp (lua_State *L) { #define donextjump(ci) { i = *pc; dojump(ci, i, 1); } /* -** Whenever code can raise errors (including memory errors), the global -** 'pc' must be correct to report occasional errors. +** Correct global 'pc'. */ #define savepc(L) (ci->u.l.savedpc = pc) +/* +** Whenever code can raise errors, the global 'pc' and the global +** 'top' must be correct to report occasional errors. +*/ +#define savestate(L,ci) (savepc(L), L->top = ci->top) + + /* ** Protect code that, in general, can raise errors, reallocate the ** stack, and change the hooks. */ -#define Protect(exp) (savepc(L), (exp), updatetrap(ci)) +#define Protect(exp) (savestate(L,ci), (exp), updatetrap(ci)) + +/* special version that does not change the top */ +#define ProtectNT(exp) (savepc(L), (exp), updatetrap(ci)) /* -** Protect code that will return. +** Protect code that will finish the loop (returns). */ #define halfProtect(exp) (savepc(L), (exp)) @@ -842,6 +849,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmfetch(); lua_assert(base == ci->func + 1); lua_assert(base <= L->top && L->top < L->stack + L->stacksize); + lua_assert(ci->top < L->stack + L->stacksize); vmdispatch (GET_OPCODE(i)) { vmcase(OP_MOVE) { setobjs2s(L, ra, RB(i)); @@ -1000,10 +1008,11 @@ void luaV_execute (lua_State *L, CallInfo *ci) { int b = GETARG_B(i); int c = GETARG_C(i); Table *t; - t = luaH_new(L); + L->top = ci->top; /* correct top in case of GC */ + t = luaH_new(L); /* memory allocation */ sethvalue2s(L, ra, t); if (b != 0 || c != 0) - luaH_resize(L, t, luaO_fb2int(b), luaO_fb2int(c)); + luaH_resize(L, t, luaO_fb2int(b), luaO_fb2int(c)); /* idem */ checkGC(L, ra + 1); vmbreak; } @@ -1371,7 +1380,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { int c = GETARG_C(i); StkId rb; L->top = base + c + 1; /* mark the end of concat operands */ - Protect(luaV_concat(L, c - b + 1)); + ProtectNT(luaV_concat(L, c - b + 1)); if (trap) { /* 'luaV_concat' may move the stack */ updatebase(ci); ra = RA(i); @@ -1481,7 +1490,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { if (b != 0) /* fixed number of arguments? */ L->top = ra + b; /* top signals number of arguments */ /* else previous instruction set top */ - Protect(luaD_call(L, ra, nresults)); + ProtectNT(luaD_call(L, ra, nresults)); vmbreak; } vmcase(OP_TAILCALL) { @@ -1493,12 +1502,12 @@ void luaV_execute (lua_State *L, CallInfo *ci) { lua_assert(GETARG_C(i) - 1 == LUA_MULTRET); if (!ttisfunction(vra)) { /* not a function? */ /* try to get '__call' metamethod */ - Protect(ra = luaD_tryfuncTM(L, ra)); + ProtectNT(ra = luaD_tryfuncTM(L, ra)); vra = s2v(ra); b++; /* there is now one extra argument */ } if (!ttisLclosure(vra)) { /* C function? */ - Protect(luaD_call(L, ra, LUA_MULTRET)); /* call it */ + ProtectNT(luaD_call(L, ra, LUA_MULTRET)); /* call it */ /* next instruction will do the return */ } else { /* tail call */ @@ -1568,7 +1577,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { lua_Integer ilimit, initv; int stopnow; if (!forlimit(plimit, &ilimit, 1, &stopnow)) { - savepc(L); /* for the error message */ + savestate(L, ci); /* for the error message */ luaG_runerror(L, "'for' limit must be a number"); } initv = (stopnow ? 0 : ivalue(init)); @@ -1618,7 +1627,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { } else { /* try making all values floats */ lua_Number ninit; lua_Number nlimit; lua_Number nstep; - savepc(L); /* in case of errors */ + savestate(L, ci); /* in case of errors */ if (!tonumber(plimit, &nlimit)) luaG_runerror(L, "'for' limit must be a number"); setfltvalue(plimit, nlimit); @@ -1659,7 +1668,10 @@ void luaV_execute (lua_State *L, CallInfo *ci) { int c = GETARG_C(i); unsigned int last; Table *h; - if (n == 0) n = cast_int(L->top - ra) - 1; + if (n == 0) + n = cast_int(L->top - ra) - 1; + else + L->top = ci->top; /* correct top in case of GC */ if (c == 0) { c = GETARG_Ax(*pc); pc++; } @@ -1679,7 +1691,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { Proto *p = cl->p->p[GETARG_Bx(i)]; LClosure *ncl = getcached(p, cl->upvals, base); /* cached closure */ if (ncl == NULL) { /* no match? */ - savepc(L); /* in case of allocation errors */ + savestate(L, ci); /* in case of allocation errors */ pushclosure(L, p, cl->upvals, base, ra); /* create a new one */ } else From 28323aeaa6963e53171b1855cc4a51b519985f48 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 29 Dec 2017 13:44:51 -0200 Subject: [PATCH 0180/1145] by-one error when filling missing arguments in a tail call --- ldo.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ldo.c b/ldo.c index 6a28e8eed1..1abd105c82 100644 --- a/ldo.c +++ b/ldo.c @@ -1,5 +1,5 @@ /* -** $Id: ldo.c,v 2.183 2017/12/20 14:58:05 roberto Exp roberto $ +** $Id: ldo.c,v 2.184 2017/12/28 14:17:09 roberto Exp roberto $ ** Stack and Call structure of Lua ** See Copyright Notice in lua.h */ @@ -418,8 +418,8 @@ void luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, int n) { for (i = 0; i < n; i++) /* move down function and arguments */ setobjs2s(L, ci->func + i, func + i); checkstackp(L, fsize, func); - for (; i < p->numparams; i++) - setnilvalue(s2v(ci->func + i)); /* complete missing parameters */ + for (; i <= p->numparams; i++) + setnilvalue(s2v(ci->func + i)); /* complete missing arguments */ if (p->is_vararg) { L->top -= (func - ci->func); /* move down top */ luaT_adjustvarargs(L, p, n - 1); From 68af7cc81aea60b1030e796c39be37efd7e10195 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 29 Dec 2017 13:58:23 -0200 Subject: [PATCH 0181/1145] another try with table resize. (Old version was leaving some elements unanchored while allocating new memory) --- ltable.c | 96 ++++++++++++++++++++++++++++++++------------------------ 1 file changed, 55 insertions(+), 41 deletions(-) diff --git a/ltable.c b/ltable.c index 85101a8a6f..419f9f6c5d 100644 --- a/ltable.c +++ b/ltable.c @@ -1,5 +1,5 @@ /* -** $Id: ltable.c,v 2.128 2017/12/07 18:59:52 roberto Exp roberto $ +** $Id: ltable.c,v 2.129 2017/12/08 17:28:25 roberto Exp roberto $ ** Lua tables (hash) ** See Copyright Notice in lua.h */ @@ -262,6 +262,12 @@ int luaH_next (lua_State *L, Table *t, StkId key) { } +static void freehash (lua_State *L, Table *t) { + if (!isdummy(t)) + luaM_freearray(L, t->node, cast(size_t, sizenode(t))); +} + + /* ** {============================================================= ** Rehash @@ -390,12 +396,13 @@ static void setnodevector (lua_State *L, Table *t, unsigned int size) { /* -** (Re)insert all elements from list 'nodes' into table 't'. +** (Re)insert all elements from the hash part of 'ot' into table 't'. */ -static void reinsert(lua_State *L, Node *nodes, int nsize, Table *t) { +static void reinsert (lua_State *L, Table *ot, Table *t) { int j; - for (j = nsize - 1; j >= 0; j--) { - Node *old = nodes + j; + int size = sizenode(ot); + for (j = 0; j < size; j++) { + Node *old = gnode(ot, j); if (!ttisnil(gval(old))) { /* doesn't need barrier/invalidate cache, as entry was already present in the table */ @@ -408,60 +415,68 @@ static void reinsert(lua_State *L, Node *nodes, int nsize, Table *t) { /* -** Resize table 't' for the new given sizes. Both allocations -** (for the hash part and for the array part) can fail, which -** creates some subtleties. If the first allocation, for the hash -** part, fails, an error is raised and that is it. Otherwise, -** copy the elements in the shrinking part of the array (if it -** is shrinking) into the new hash. Then it reallocates the array part. -** If that fails, it frees the new hash part and restores the old hash -** part (to restore the original state of the table), and then raises -** the allocation error. Otherwise, initialize the new part of the -** array (if any) with nils and reinsert the elements in the old -** hash back into the new parts of the table. +** Exchange the hash part of 't1' and 't2'. +*/ +static void exchangehashpart (Table *t1, Table *t2) { + lu_byte lsizenode = t1->lsizenode; + Node *node = t1->node; + Node *lastfree = t1->lastfree; + t1->lsizenode = t2->lsizenode; + t1->node = t2->node; + t1->lastfree = t2->lastfree; + t2->lsizenode = lsizenode; + t2->node = node; + t2->lastfree = lastfree; +} + + +/* +** Resize table 't' for the new given sizes. Both allocations (for +** the hash part and for the array part) can fail, which creates some +** subtleties. If the first allocation, for the hash part, fails, an +** error is raised and that is it. Otherwise, it copies the elements from +** the shrinking part of the array (if it is shrinking) into the new +** hash. Then it reallocates the array part. If that fails, the table +** is in its original state; the function frees the new hash part and then +** raises the allocation error. Otherwise, it sets the new hash part +** into the table, initializes the new part of the array (if any) with +** nils and reinserts the elements of the old hash back into the new +** parts of the table. */ void luaH_resize (lua_State *L, Table *t, unsigned int newasize, unsigned int nhsize) { unsigned int i; - Node *oldnode = t->node; /* save old hash ... */ - Node *oldlastfree = t->lastfree; - int oldlsizenode = t->lsizenode; - int oldhsize = allocsizenode(t); + Table newt; /* to keep the new hash part */ unsigned int oldasize = t->sizearray; TValue *newarray; - /* create new hash part with appropriate size */ - setnodevector(L, t, nhsize); + /* create new hash part with appropriate size into 'newt' */ + setnodevector(L, &newt, nhsize); if (newasize < oldasize) { /* will array shrink? */ - /* re-insert into the hash the elements from vanishing slice */ - t->sizearray = newasize; /* pretend array has new size */ + t->sizearray = newasize; /* pretend array has new size... */ + exchangehashpart(t, &newt); /* and new hash */ + /* re-insert into the new hash the elements from vanishing slice */ for (i = newasize; i < oldasize; i++) { if (!ttisnil(&t->array[i])) luaH_setint(L, t, i + 1, &t->array[i]); } - t->sizearray = oldasize; /* restore current size */ + t->sizearray = oldasize; /* restore current size... */ + exchangehashpart(t, &newt); /* and hash (in case of errors) */ } /* allocate new array */ newarray = luaM_reallocvector(L, t->array, oldasize, newasize, TValue); if (newarray == NULL && newasize > 0) { /* allocation failed? */ - if (nhsize > 0) /* not the dummy node? */ - luaM_freearray(L, t->node, allocsizenode(t)); /* release new hash part */ - t->node = oldnode; /* restore original hash part */ - t->lastfree = oldlastfree; - t->lsizenode = oldlsizenode; - lua_assert(!isdummy(t) == (t->node != dummynode)); - luaM_error(L); /* error with array unchanged */ + freehash(L, &newt); /* release new hash part */ + luaM_error(L); /* raise error (with array unchanged) */ } /* allocation ok; initialize new part of the array */ - t->array = newarray; + exchangehashpart(t, &newt); /* 't' has the new hash ('newt' has the old) */ + t->array = newarray; /* set new array part */ t->sizearray = newasize; - for (i = oldasize; i < newasize; i++) + for (i = oldasize; i < newasize; i++) /* clear new slice of the array */ setnilvalue(&t->array[i]); /* re-insert elements from old hash part into new parts */ - reinsert(L, oldnode, oldhsize, t); - /* free old hash */ - if (oldhsize > 0) /* not the dummy node? */ - luaM_freearray(L, oldnode, cast(size_t, oldhsize)); - lua_assert(!isdummy(t) == (t->node != dummynode)); + reinsert(L, &newt, t); /* 'newt' now has the old hash */ + freehash(L, &newt); /* free old hash part */ } @@ -513,8 +528,7 @@ Table *luaH_new (lua_State *L) { void luaH_free (lua_State *L, Table *t) { - if (!isdummy(t)) - luaM_freearray(L, t->node, cast(size_t, sizenode(t))); + freehash(L, t); luaM_freearray(L, t->array, t->sizearray); luaM_free(L, t); } From c6fedc92f8b1c3e73d835f7641bffe4b199f0dd1 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 29 Dec 2017 13:59:37 -0200 Subject: [PATCH 0182/1145] new command 'print' (to print literal strings) in mini-language --- ltests.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/ltests.c b/ltests.c index 130f5d6b49..513c846b45 100644 --- a/ltests.c +++ b/ltests.c @@ -1,5 +1,5 @@ /* -** $Id: ltests.c,v 2.236 2017/12/11 18:55:31 roberto Exp roberto $ +** $Id: ltests.c,v 2.237 2017/12/15 18:53:48 roberto Exp roberto $ ** Internal Module for Debugging of the Lua Implementation ** See Copyright Notice in lua.h */ @@ -1377,7 +1377,7 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) { else if EQ("pop") { lua_pop(L1, getnum); } - else if EQ("print") { + else if EQ("printstack") { int n = getnum; if (n != 0) { printf("%s\n", luaL_tolstring(L1, n, NULL)); @@ -1385,6 +1385,10 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) { } else printstack(L1); } + else if EQ("print") { + const char *msg = getstring; + printf("%s\n", msg); + } else if EQ("pushbool") { lua_pushboolean(L1, getnum); } From a9295a2b8ebca6bb7071c4424fd318afa33ebb9e Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Sat, 30 Dec 2017 18:46:18 -0200 Subject: [PATCH 0183/1145] typos in comments --- lobject.c | 6 +++--- luaconf.h | 6 +++--- lvm.c | 4 ++-- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/lobject.c b/lobject.c index e9e037f743..c7d4c06a66 100644 --- a/lobject.c +++ b/lobject.c @@ -1,5 +1,5 @@ /* -** $Id: lobject.c,v 2.120 2017/11/16 13:19:06 roberto Exp roberto $ +** $Id: lobject.c,v 2.121 2017/11/23 19:29:04 roberto Exp roberto $ ** Some generic functions over Lua objects ** See Copyright Notice in lua.h */ @@ -193,7 +193,7 @@ static int isneg (const char **s) { #define MAXSIGDIG 30 /* -** convert an hexadecimal numeric string to a number, following +** convert a hexadecimal numeric string to a number, following ** C99 specification for 'strtod' */ static lua_Number lua_strx2number (const char *s, char **endptr) { @@ -268,7 +268,7 @@ static const char *l_str2dloc (const char *s, lua_Number *result, int mode) { ** Convert string 's' to a Lua number (put in 'result'). Return NULL ** on fail or the address of the ending '\0' on success. ** 'pmode' points to (and 'mode' contains) special things in the string: -** - 'x'/'X' means an hexadecimal numeral +** - 'x'/'X' means a hexadecimal numeral ** - 'n'/'N' means 'inf' or 'nan' (which should be rejected) ** - '.' just optimizes the search for the common case (nothing special) ** This function accepts both the current locale or a dot as the radix diff --git a/luaconf.h b/luaconf.h index 2bf1bdd138..888e402b33 100644 --- a/luaconf.h +++ b/luaconf.h @@ -1,5 +1,5 @@ /* -** $Id: luaconf.h,v 1.261 2017/04/24 18:06:12 roberto Exp roberto $ +** $Id: luaconf.h,v 1.262 2017/12/07 18:53:33 roberto Exp roberto $ ** Configuration file for Lua ** See Copyright Notice in lua.h */ @@ -610,7 +610,7 @@ /* -@@ lua_strx2number converts an hexadecimal numeric string to a number. +@@ lua_strx2number converts a hexadecimal numeric string to a number. ** In C99, 'strtod' does that conversion. Otherwise, you can ** leave 'lua_strx2number' undefined and Lua will provide its own ** implementation. @@ -628,7 +628,7 @@ /* -@@ lua_number2strx converts a float to an hexadecimal numeric string. +@@ lua_number2strx converts a float to a hexadecimal numeric string. ** In C99, 'sprintf' (with format specifiers '%a'/'%A') does that. ** Otherwise, you can leave 'lua_number2strx' undefined and Lua will ** provide its own implementation. diff --git a/lvm.c b/lvm.c index 212d5f9d32..2b03593256 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.329 2017/12/22 14:16:46 roberto Exp roberto $ +** $Id: lvm.c,v 2.330 2017/12/28 15:42:57 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -674,7 +674,7 @@ static void pushclosure (lua_State *L, Proto *p, UpVal **encup, StkId base, /* -** finish execution of an opcode interrupted by an yield +** finish execution of an opcode interrupted by a yield */ void luaV_finishOp (lua_State *L) { CallInfo *ci = L->ci; From 33e3774f447cbcfa4fe43b8b47d0306e52937428 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 9 Jan 2018 09:24:12 -0200 Subject: [PATCH 0184/1145] keep more opcode arguments byte-aligned --- lcode.c | 12 ++++++------ lopcodes.h | 36 ++++++++++++++++++++---------------- ltests.c | 4 ++-- 3 files changed, 28 insertions(+), 24 deletions(-) diff --git a/lcode.c b/lcode.c index 1eae475ab9..ae024da857 100644 --- a/lcode.c +++ b/lcode.c @@ -1,5 +1,5 @@ /* -** $Id: lcode.c,v 2.147 2017/12/22 14:16:46 roberto Exp roberto $ +** $Id: lcode.c,v 2.149 2018/01/09 11:21:41 roberto Exp $ ** Code generator for Lua ** See Copyright Notice in lua.h */ @@ -262,16 +262,16 @@ void luaK_patchtohere (FuncState *fs, int list) { /* ** Correct a jump list to jump to 'target'. If 'hasclose' is true, ** 'target' contains an OP_CLOSE instruction (see first assert). -** Only jumps with the 'k' arg true need that close; other jumps +** Only the jumps with ('m' == true) need that close; other jumps ** avoid it jumping to the next instruction. */ void luaK_patchgoto (FuncState *fs, int list, int target, int hasclose) { lua_assert(!hasclose || GET_OPCODE(fs->f->code[target]) == OP_CLOSE); while (list != NO_JUMP) { int next = getjump(fs, list); - lua_assert(!GETARG_k(fs->f->code[list]) || hasclose); + lua_assert(!GETARG_m(fs->f->code[list]) || hasclose); patchtestreg(fs, list, NO_REG); /* do not generate values */ - if (!hasclose || GETARG_k(fs->f->code[list])) + if (!hasclose || GETARG_m(fs->f->code[list])) fixjump(fs, list, target); else /* there is a CLOSE instruction but jump does not need it */ fixjump(fs, list, target + 1); /* avoid CLOSE instruction */ @@ -281,14 +281,14 @@ void luaK_patchgoto (FuncState *fs, int list, int target, int hasclose) { /* -** Mark (using the 'k' arg) all jumps in 'list' to close upvalues. Mark +** Mark (using the 'm' arg) all jumps in 'list' to close upvalues. Mark ** will instruct 'luaK_patchgoto' to make these jumps go to OP_CLOSE ** instructions. */ void luaK_patchclose (FuncState *fs, int list) { for (; list != NO_JUMP; list = getjump(fs, list)) { lua_assert(GET_OPCODE(fs->f->code[list]) == OP_JMP); - SETARG_k(fs->f->code[list], 1); + SETARG_m(fs->f->code[list], 1); } } diff --git a/lopcodes.h b/lopcodes.h index eae0dfaf53..22de7a7142 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -1,5 +1,5 @@ /* -** $Id: lopcodes.h,v 1.180 2017/12/18 17:49:31 roberto Exp roberto $ +** $Id: lopcodes.h,v 1.182 2018/01/09 11:21:41 roberto Exp $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ @@ -17,11 +17,11 @@ 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 -iABC |k| C(8) | | B(8) | | A(8) | | Op(7) | -iABx | Bx(17) | | A(8) | | Op(7) | -iAsBx | sBx (signed)(17) | | A(8) | | Op(7) | -iAx | Ax(25) | | Op(7) | -iksJ |k| sJ(24) | | Op(7) | +iABC C(8) | B(8) |k| A(8) | Op(7) | +iABx Bx(17) | A(8) | Op(7) | +iAsB sBx (signed)(17) | A(8) | Op(7) | +iAx Ax(25) | Op(7) | +isJ sJ(24) |m| Op(7) | A signed argument is represented in excess K: the represented value is the written unsigned value minus K, where K is half the maximum for the @@ -36,25 +36,27 @@ enum OpMode {iABC, iABx, iAsBx, iAx, isJ}; /* basic instruction formats */ ** size and position of opcode arguments. */ #define SIZE_C 8 -#define SIZE_Cx (SIZE_C + 1) #define SIZE_B 8 -#define SIZE_Bx (SIZE_Cx + SIZE_B) +#define SIZE_Bx (SIZE_C + SIZE_B + 1) #define SIZE_A 8 -#define SIZE_Ax (SIZE_Cx + SIZE_B + SIZE_A) -#define SIZE_sJ (SIZE_C + SIZE_B + SIZE_A) - +#define SIZE_Ax (SIZE_Bx + SIZE_A) +#define SIZE_sJ (SIZE_Bx + SIZE_A - 1) #define SIZE_OP 7 #define POS_OP 0 + #define POS_A (POS_OP + SIZE_OP) -#define POS_B (POS_A + SIZE_A) +#define POS_k (POS_A + SIZE_A) +#define POS_B (POS_k + 1) #define POS_C (POS_B + SIZE_B) -#define POS_k (POS_C + SIZE_C) -#define POS_Bx POS_B + +#define POS_Bx POS_k + #define POS_Ax POS_A -#define POS_sJ POS_A +#define POS_m POS_A +#define POS_sJ (POS_A + 1) /* ** limits for opcode arguments. @@ -125,7 +127,7 @@ enum OpMode {iABC, iABx, iAsBx, iAx, isJ}; /* basic instruction formats */ #define SETARG_C(i,v) setarg(i, v, POS_C, SIZE_C) #define TESTARG_k(i) (cast(int, ((i) & (1u << POS_k)))) -#define GETARG_k(i) getarg(i, POS_k, 1) +#define GETARG_k(i) check_exp(checkopm(i, iABC), getarg(i, POS_k, 1)) #define SETARG_k(i,v) setarg(i, v, POS_k, 1) #define GETARG_Bx(i) check_exp(checkopm(i, iABx), getarg(i, POS_Bx, SIZE_Bx)) @@ -142,6 +144,8 @@ enum OpMode {iABC, iABx, iAsBx, iAx, isJ}; /* basic instruction formats */ check_exp(checkopm(i, isJ), getarg(i, POS_sJ, SIZE_sJ) - OFFSET_sJ) #define SETARG_sJ(i,j) \ setarg(i, cast(unsigned int, (j)+OFFSET_sJ), POS_sJ, SIZE_sJ) +#define GETARG_m(i) check_exp(checkopm(i, isJ), getarg(i, POS_m, 1)) +#define SETARG_m(i,m) setarg(i, m, POS_m, 1) #define CREATE_ABCk(o,a,b,c,k) ((cast(Instruction, o)< Date: Tue, 9 Jan 2018 12:23:40 -0200 Subject: [PATCH 0185/1145] avoid jumping into a variable scope (C++ does not allow that) --- lvm.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lvm.c b/lvm.c index 2b03593256..8ba7f1c705 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.330 2017/12/28 15:42:57 roberto Exp roberto $ +** $Id: lvm.c,v 2.331 2017/12/30 20:46:18 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -1401,12 +1401,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmcase(OP_EQ) { TValue *rb = vRB(i); Protect(cond = luaV_equalobj(L, vra, rb)); - condjump: - if (cond != GETARG_k(i)) - pc++; /* skip next jump */ - else - donextjump(ci); - vmbreak; + goto condjump; } vmcase(OP_LT) { TValue *rb = vRB(i); @@ -1472,7 +1467,12 @@ void luaV_execute (lua_State *L, CallInfo *ci) { } vmcase(OP_TEST) { cond = !l_isfalse(vra); - goto condjump; + condjump: + if (cond != GETARG_k(i)) + pc++; /* skip next jump */ + else + donextjump(ci); + vmbreak; } vmcase(OP_TESTSET) { TValue *rb = vRB(i); From 728ff9459579894ef7fd861c90be6247831799be Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 10 Jan 2018 10:02:35 -0200 Subject: [PATCH 0186/1145] error handler in protected calls must be a function --- lapi.c | 3 ++- ldebug.c | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/lapi.c b/lapi.c index d6aaf8a873..142baf6a86 100644 --- a/lapi.c +++ b/lapi.c @@ -1,5 +1,5 @@ /* -** $Id: lapi.c,v 2.278 2017/12/06 18:08:03 roberto Exp roberto $ +** $Id: lapi.c,v 2.279 2017/12/08 17:28:25 roberto Exp roberto $ ** Lua API ** See Copyright Notice in lua.h */ @@ -980,6 +980,7 @@ LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc, func = 0; else { StkId o = index2stack(L, errfunc); + api_check(L, ttisfunction(s2v(o)), "error handler must be a function"); func = savestack(L, o); } c.func = L->top - (nargs+1); /* function to be called */ diff --git a/ldebug.c b/ldebug.c index 5233560044..3ea6367fd1 100644 --- a/ldebug.c +++ b/ldebug.c @@ -1,5 +1,5 @@ /* -** $Id: ldebug.c,v 2.150 2017/12/20 14:58:05 roberto Exp roberto $ +** $Id: ldebug.c,v 2.151 2017/12/28 15:42:57 roberto Exp roberto $ ** Debug Interface ** See Copyright Notice in lua.h */ @@ -722,10 +722,10 @@ const char *luaG_addinfo (lua_State *L, const char *msg, TString *src, l_noret luaG_errormsg (lua_State *L) { if (L->errfunc != 0) { /* is there an error handling function? */ StkId errfunc = restorestack(L, L->errfunc); + lua_assert(ttisfunction(s2v(errfunc))); setobjs2s(L, L->top, L->top - 1); /* move argument */ setobjs2s(L, L->top - 1, errfunc); /* push function */ L->top++; /* assume EXTRA_STACK */ - luaE_incCcalls(L); luaD_callnoyield(L, L->top - 2, 1); /* call it */ } luaD_throw(L, LUA_ERRRUN); From ab0a851db438c4dd50b76bcf624c15623cc057e4 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 10 Jan 2018 17:19:27 -0200 Subject: [PATCH 0187/1145] 'luaD_tryfuncTM' can ensure it does not change the stack --- ldo.c | 11 ++++------- ldo.h | 4 ++-- lvm.c | 6 ++---- 3 files changed, 8 insertions(+), 13 deletions(-) diff --git a/ldo.c b/ldo.c index 1abd105c82..dd2ecb89b6 100644 --- a/ldo.c +++ b/ldo.c @@ -1,5 +1,5 @@ /* -** $Id: ldo.c,v 2.184 2017/12/28 14:17:09 roberto Exp roberto $ +** $Id: ldo.c,v 2.185 2017/12/29 15:44:51 roberto Exp roberto $ ** Stack and Call structure of Lua ** See Copyright Notice in lua.h */ @@ -327,18 +327,15 @@ static void rethook (lua_State *L, CallInfo *ci) { ** it in stack below original 'func' so that 'luaD_call' can call ** it. Raise an error if __call metafield is not a function. */ -StkId luaD_tryfuncTM (lua_State *L, StkId func) { +void luaD_tryfuncTM (lua_State *L, StkId func) { const TValue *tm = luaT_gettmbyobj(L, s2v(func), TM_CALL); StkId p; if (!ttisfunction(tm)) luaG_typeerror(L, s2v(func), "call"); - /* Open a hole inside the stack at 'func' */ - checkstackp(L, 1, func); /* ensure space for metamethod */ for (p = L->top; p > func; p--) setobjs2s(L, p, p-1); - L->top++; + L->top++; /* assume EXTRA_STACK */ setobj2s(L, func, tm); /* metamethod is the new function to be called */ - return func; } @@ -489,7 +486,7 @@ void luaD_call (lua_State *L, StkId func, int nresults) { break; } default: { /* not a function */ - func = luaD_tryfuncTM(L, func); /* try to get '__call' metamethod */ + luaD_tryfuncTM(L, func); /* try to get '__call' metamethod */ luaD_call(L, func, nresults); /* now it must be a function */ break; } diff --git a/ldo.h b/ldo.h index 765f6cef5f..50f79242ac 100644 --- a/ldo.h +++ b/ldo.h @@ -1,5 +1,5 @@ /* -** $Id: ldo.h,v 2.37 2017/12/08 17:28:25 roberto Exp roberto $ +** $Id: ldo.h,v 2.38 2017/12/11 12:43:40 roberto Exp roberto $ ** Stack and Call structure of Lua ** See Copyright Notice in lua.h */ @@ -51,7 +51,7 @@ LUAI_FUNC void luaD_hook (lua_State *L, int event, int line); LUAI_FUNC void luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, int n); LUAI_FUNC void luaD_call (lua_State *L, StkId func, int nResults); LUAI_FUNC void luaD_callnoyield (lua_State *L, StkId func, int nResults); -LUAI_FUNC StkId luaD_tryfuncTM (lua_State *L, StkId func); +LUAI_FUNC void luaD_tryfuncTM (lua_State *L, StkId func); LUAI_FUNC int luaD_pcall (lua_State *L, Pfunc func, void *u, ptrdiff_t oldtop, ptrdiff_t ef); LUAI_FUNC void luaD_poscall (lua_State *L, CallInfo *ci, StkId firstResult, diff --git a/lvm.c b/lvm.c index 8ba7f1c705..bdea807fa6 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.331 2017/12/30 20:46:18 roberto Exp roberto $ +** $Id: lvm.c,v 2.332 2018/01/09 14:23:40 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -1501,9 +1501,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { b = L->top - ra; lua_assert(GETARG_C(i) - 1 == LUA_MULTRET); if (!ttisfunction(vra)) { /* not a function? */ - /* try to get '__call' metamethod */ - ProtectNT(ra = luaD_tryfuncTM(L, ra)); - vra = s2v(ra); + ProtectNT(luaD_tryfuncTM(L, ra)); /* try '__call' metamethod */ b++; /* there is now one extra argument */ } if (!ttisLclosure(vra)) { /* C function? */ From d2fb34ac8865b2a7ec944a449d90145739030a98 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Sun, 14 Jan 2018 15:27:50 -0200 Subject: [PATCH 0188/1145] 'OP_TAILCALL' calling C functions finishes the call and returns (instead of waiting for following 'OP_RETURN') --- lvm.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/lvm.c b/lvm.c index bdea807fa6..12597eb44b 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.332 2018/01/09 14:23:40 roberto Exp roberto $ +** $Id: lvm.c,v 2.333 2018/01/10 19:19:27 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -1504,13 +1504,18 @@ void luaV_execute (lua_State *L, CallInfo *ci) { ProtectNT(luaD_tryfuncTM(L, ra)); /* try '__call' metamethod */ b++; /* there is now one extra argument */ } + if (TESTARG_k(i)) + luaF_close(L, base); /* close upvalues from current call */ if (!ttisLclosure(vra)) { /* C function? */ ProtectNT(luaD_call(L, ra, LUA_MULTRET)); /* call it */ - /* next instruction will do the return */ + if (trap) { + updatebase(ci); + ra = RA(i); + } + luaD_poscall(L, ci, ra, cast_int(L->top - ra)); + return; } - else { /* tail call */ - if (TESTARG_k(i)) /* close upvalues from previous call */ - luaF_close(L, ci->func + 1); + else { /* Lua tail call */ luaD_pretailcall(L, ci, ra, b); /* prepare call frame */ goto tailcall; } From 28f215ecf8b8d987f0bf64f3154b0e2fbc5e5168 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 18 Jan 2018 14:24:31 -0200 Subject: [PATCH 0189/1145] comments --- lcode.c | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/lcode.c b/lcode.c index ae024da857..e062079198 100644 --- a/lcode.c +++ b/lcode.c @@ -1,5 +1,5 @@ /* -** $Id: lcode.c,v 2.149 2018/01/09 11:21:41 roberto Exp $ +** $Id: lcode.c,v 2.149 2018/01/09 11:24:12 roberto Exp roberto $ ** Code generator for Lua ** See Copyright Notice in lua.h */ @@ -656,6 +656,7 @@ void luaK_setoneret (FuncState *fs, expdesc *e) { /* ** Ensure that expression 'e' is not a variable. +** (Expression still may have jump lists.) */ void luaK_dischargevars (FuncState *fs, expdesc *e) { switch (e->k) { @@ -703,6 +704,7 @@ void luaK_dischargevars (FuncState *fs, expdesc *e) { /* ** Ensures expression value is in register 'reg' (and therefore ** 'e' will become a non-relocatable expression). +** (Expression still may have jump lists.) */ static void discharge2reg (FuncState *fs, expdesc *e, int reg) { luaK_dischargevars(fs, e); @@ -749,6 +751,7 @@ static void discharge2reg (FuncState *fs, expdesc *e, int reg) { /* ** Ensures expression value is in any register. +** (Expression still may have jump lists.) */ static void discharge2anyreg (FuncState *fs, expdesc *e) { if (e->k != VNONRELOC) { /* no fixed register yet? */ @@ -778,8 +781,8 @@ static int need_value (FuncState *fs, int list) { /* -** Ensures final expression result (including results from its jump -** lists) is in register 'reg'. +** Ensures final expression result (which includes results from its +** jump ** lists) is in register 'reg'. ** If expression has jumps, need to patch these jumps either to ** its final position or to "load" instructions (for those tests ** that do not produce values). @@ -809,8 +812,7 @@ static void exp2reg (FuncState *fs, expdesc *e, int reg) { /* -** Ensures final expression result (including results from its jump -** lists) is in next available register. +** Ensures final expression result is in next available register. */ void luaK_exp2nextreg (FuncState *fs, expdesc *e) { luaK_dischargevars(fs, e); @@ -821,8 +823,8 @@ void luaK_exp2nextreg (FuncState *fs, expdesc *e) { /* -** Ensures final expression result (including results from its jump -** lists) is in some (any) register and return that register. +** Ensures final expression result is in some (any) register +** and return that register. */ int luaK_exp2anyreg (FuncState *fs, expdesc *e) { luaK_dischargevars(fs, e); @@ -840,8 +842,8 @@ int luaK_exp2anyreg (FuncState *fs, expdesc *e) { /* -** Ensures final expression result is either in a register or in an -** upvalue. +** Ensures final expression result is either in a register +** or in an upvalue. */ void luaK_exp2anyregup (FuncState *fs, expdesc *e) { if (e->k != VUPVAL || hasjumps(e)) @@ -850,8 +852,8 @@ void luaK_exp2anyregup (FuncState *fs, expdesc *e) { /* -** Ensures final expression result is either in a register or it is -** a constant. +** Ensures final expression result is either in a register +** or it is a constant. */ void luaK_exp2val (FuncState *fs, expdesc *e) { if (hasjumps(e)) From 5bd8d388de6704cc7f0eb60de33636a4dfcd61bd Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Sat, 27 Jan 2018 14:56:33 -0200 Subject: [PATCH 0190/1145] OP_CONCAT does not move its result (to simplify its execution) --- lcode.c | 84 +++++++++++++++++++++++++++++++++--------------------- lopcodes.h | 4 +-- lvm.c | 24 +++++----------- 3 files changed, 61 insertions(+), 51 deletions(-) diff --git a/lcode.c b/lcode.c index e062079198..7e573f0266 100644 --- a/lcode.c +++ b/lcode.c @@ -1,5 +1,5 @@ /* -** $Id: lcode.c,v 2.149 2018/01/09 11:24:12 roberto Exp roberto $ +** $Id: lcode.c,v 2.150 2018/01/18 16:24:31 roberto Exp roberto $ ** Code generator for Lua ** See Copyright Notice in lua.h */ @@ -59,6 +59,21 @@ static int tonumeral(const expdesc *e, TValue *v) { } +/* +** Return the previous instruction of the current code. If there +** may be a jump target between the current instruction and the +** previous one, return an invalid instruction (to avoid wrong +** optimizations). +*/ +static Instruction *previousinstruction (FuncState *fs) { + static const Instruction invalidinstruction = -1; + if (fs->pc > fs->lasttarget) + return &fs->f->code[fs->pc - 1]; /* previous instruction */ + else + return cast(Instruction*, &invalidinstruction); +} + + /* ** Create a OP_LOADNIL instruction, but try to optimize: if the previous ** instruction is also OP_LOADNIL and ranges are compatible, adjust @@ -66,21 +81,18 @@ static int tonumeral(const expdesc *e, TValue *v) { ** instance, 'local a; local b' will generate a single opcode.) */ void luaK_nil (FuncState *fs, int from, int n) { - Instruction *previous; int l = from + n - 1; /* last register to set nil */ - if (fs->pc > fs->lasttarget) { /* no jumps to current position? */ - previous = &fs->f->code[fs->pc-1]; - if (GET_OPCODE(*previous) == OP_LOADNIL) { /* previous is LOADNIL? */ - int pfrom = GETARG_A(*previous); /* get previous range */ - int pl = pfrom + GETARG_B(*previous); - if ((pfrom <= from && from <= pl + 1) || - (from <= pfrom && pfrom <= l + 1)) { /* can connect both? */ - if (pfrom < from) from = pfrom; /* from = min(from, pfrom) */ - if (pl > l) l = pl; /* l = max(l, pl) */ - SETARG_A(*previous, from); - SETARG_B(*previous, l - from); - return; - } + Instruction *previous = previousinstruction(fs); + if (GET_OPCODE(*previous) == OP_LOADNIL) { /* previous is LOADNIL? */ + int pfrom = GETARG_A(*previous); /* get previous range */ + int pl = pfrom + GETARG_B(*previous); + if ((pfrom <= from && from <= pl + 1) || + (from <= pfrom && pfrom <= l + 1)) { /* can connect both? */ + if (pfrom < from) from = pfrom; /* from = min(from, pfrom) */ + if (pl > l) l = pl; /* l = max(l, pl) */ + SETARG_A(*previous, from); + SETARG_B(*previous, l - from); + return; } /* else go through */ } luaK_codeABC(fs, OP_LOADNIL, from, n - 1, 0); /* else no optimization */ @@ -1432,7 +1444,7 @@ void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) { break; } case OPR_CONCAT: { - luaK_exp2nextreg(fs, v); /* operand must be on the 'stack' */ + luaK_exp2nextreg(fs, v); /* operand must be on the stack */ break; } case OPR_ADD: case OPR_SUB: @@ -1463,12 +1475,30 @@ void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) { } } +/* +** Create code for '(e1 .. e2)'. +** For '(e1 .. e2.1 .. e2.2)' (which is '(e1 .. (e2.1 .. e2.2))', +** because concatenation is right associative), merge both CONCATs. +*/ +static void codeconcat (FuncState *fs, expdesc *e1, expdesc *e2, int line) { + Instruction *ie2 = previousinstruction(fs); + if (GET_OPCODE(*ie2) == OP_CONCAT) { /* is 'e2' a concatenation? */ + int n = GETARG_B(*ie2); /* # of elements concatenated in 'e2' */ + lua_assert(e1->u.info + 1 == GETARG_A(*ie2)); + freeexp(fs, e2); + SETARG_A(*ie2, e1->u.info); /* correct first element ('e1') */ + SETARG_B(*ie2, n + 1); /* will concatenate one more element */ + } + else { /* 'e2' is not a concatenation */ + luaK_codeABC(fs, OP_CONCAT, e1->u.info, 2, 0); /* new concat opcode */ + freeexp(fs, e2); + luaK_fixline(fs, line); + } +} + /* ** Finalize code for binary operation, after reading 2nd operand. -** For '(a .. b .. c)' (which is '(a .. (b .. c))', because -** concatenation is right associative), merge second CONCAT into first -** one. */ void luaK_posfix (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2, int line) { @@ -1487,19 +1517,9 @@ void luaK_posfix (FuncState *fs, BinOpr opr, *e1 = *e2; break; } - case OPR_CONCAT: { - luaK_exp2val(fs, e2); - if (e2->k == VRELOC && - GET_OPCODE(getinstruction(fs, e2)) == OP_CONCAT) { - lua_assert(e1->u.info == GETARG_B(getinstruction(fs, e2))-1); - freeexp(fs, e1); - SETARG_B(getinstruction(fs, e2), e1->u.info); - e1->k = VRELOC; e1->u.info = e2->u.info; - } - else { - luaK_exp2nextreg(fs, e2); /* operand must be on the 'stack' */ - codebinexpval(fs, OP_CONCAT, e1, e2, line); - } + case OPR_CONCAT: { /* e1 .. e2 */ + luaK_exp2nextreg(fs, e2); + codeconcat(fs, e1, e2, line); break; } case OPR_ADD: case OPR_MUL: { diff --git a/lopcodes.h b/lopcodes.h index 22de7a7142..2f4a48fa76 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -1,5 +1,5 @@ /* -** $Id: lopcodes.h,v 1.182 2018/01/09 11:21:41 roberto Exp $ +** $Id: lopcodes.h,v 1.182 2018/01/09 11:24:12 roberto Exp roberto $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ @@ -248,7 +248,7 @@ OP_BNOT,/* A B R(A) := ~R(B) */ OP_NOT,/* A B R(A) := not R(B) */ OP_LEN,/* A B R(A) := length of R(B) */ -OP_CONCAT,/* A B C R(A) := R(B).. ... ..R(C) */ +OP_CONCAT,/* A B R(A) := R(A).. ... ..R(A + B - 1) */ OP_CLOSE,/* A close all upvalues >= R(A) */ OP_JMP,/* k sJ pc += sJ (k is used in code generation) */ diff --git a/lvm.c b/lvm.c index 12597eb44b..73256267a4 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.333 2018/01/10 19:19:27 roberto Exp roberto $ +** $Id: lvm.c,v 2.334 2018/01/14 17:27:50 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -717,15 +717,13 @@ void luaV_finishOp (lua_State *L) { } case OP_CONCAT: { StkId top = L->top - 1; /* top when 'luaT_trybinTM' was called */ - int b = GETARG_B(inst); /* first element to concatenate */ - int total = cast_int(top - 1 - (base + b)); /* yet to concatenate */ + int a = GETARG_A(inst); /* first element to concatenate */ + int total = cast_int(top - 1 - (base + a)); /* yet to concatenate */ setobjs2s(L, top - 2, top); /* put TM result in proper position */ if (total > 1) { /* are there elements to concat? */ L->top = top - 1; /* top is one after last element (at top-2) */ luaV_concat(L, total); /* concat them (may yield again) */ } - /* move final result to final position */ - setobjs2s(L, ci->func + 1 + GETARG_A(inst), L->top - 1); break; } case OP_TFORCALL: case OP_CALL: case OP_TAILCALL: @@ -1376,18 +1374,10 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_CONCAT) { - int b = GETARG_B(i); - int c = GETARG_C(i); - StkId rb; - L->top = base + c + 1; /* mark the end of concat operands */ - ProtectNT(luaV_concat(L, c - b + 1)); - if (trap) { /* 'luaV_concat' may move the stack */ - updatebase(ci); - ra = RA(i); - } - rb = base + b; - setobjs2s(L, ra, rb); - checkGC(L, (ra >= rb ? ra + 1 : rb)); + int n = GETARG_B(i); /* number of elements to concatenate */ + L->top = ra + n; /* mark the end of concat operands */ + ProtectNT(luaV_concat(L, n)); + checkGC(L, L->top); /* 'luaV_concat' ensures correct top */ vmbreak; } vmcase(OP_CLOSE) { From 6710a2b0ef164cb5bf3412353400aada5ac3f373 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Sun, 28 Jan 2018 10:07:53 -0200 Subject: [PATCH 0191/1145] detail (comment) --- lobject.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lobject.h b/lobject.h index c7b6be18d4..6541dea4d9 100644 --- a/lobject.h +++ b/lobject.h @@ -1,5 +1,5 @@ /* -** $Id: lobject.h,v 2.130 2017/11/07 13:25:26 roberto Exp roberto $ +** $Id: lobject.h,v 2.131 2017/11/23 19:29:04 roberto Exp roberto $ ** Type definitions for Lua objects ** See Copyright Notice in lua.h */ @@ -446,7 +446,7 @@ typedef struct AbsLineInfo { */ typedef struct Proto { CommonHeader; - lu_byte numparams; /* number of fixed parameters */ + lu_byte numparams; /* number of fixed (named) parameters */ lu_byte is_vararg; lu_byte maxstacksize; /* number of registers needed by this function */ lu_byte cachemiss; /* count for successive misses for 'cache' field */ From 53979dfe0dbd7eba767ff37b1148d1e4dc9f8294 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Sun, 28 Jan 2018 10:08:04 -0200 Subject: [PATCH 0192/1145] calling a vararg function needs to check GC (because it creates a new table) --- ldo.c | 14 +++++++------- ltm.c | 4 +++- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/ldo.c b/ldo.c index dd2ecb89b6..50ab00129e 100644 --- a/ldo.c +++ b/ldo.c @@ -1,5 +1,5 @@ /* -** $Id: ldo.c,v 2.185 2017/12/29 15:44:51 roberto Exp roberto $ +** $Id: ldo.c,v 2.186 2018/01/10 19:19:27 roberto Exp roberto $ ** Stack and Call structure of Lua ** See Copyright Notice in lua.h */ @@ -417,14 +417,14 @@ void luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, int n) { checkstackp(L, fsize, func); for (; i <= p->numparams; i++) setnilvalue(s2v(ci->func + i)); /* complete missing arguments */ - if (p->is_vararg) { - L->top -= (func - ci->func); /* move down top */ - luaT_adjustvarargs(L, p, n - 1); - } ci->top = ci->func + 1 + fsize; /* top for new function */ lua_assert(ci->top <= L->stack_last); ci->u.l.savedpc = p->code; /* starting point */ ci->callstatus |= CIST_TAIL; + if (p->is_vararg) { + L->top -= (func - ci->func); /* move down top */ + luaT_adjustvarargs(L, p, n - 1); + } if (L->hookmask) hookcall(L, ci, 1); } @@ -471,8 +471,6 @@ void luaD_call (lua_State *L, StkId func, int nresults) { checkstackp(L, fsize, func); for (; n < p->numparams; n++) setnilvalue(s2v(L->top++)); /* complete missing arguments */ - if (p->is_vararg) - luaT_adjustvarargs(L, p, n); ci = next_ci(L); /* now 'enter' new function */ ci->nresults = nresults; ci->func = func; @@ -480,6 +478,8 @@ void luaD_call (lua_State *L, StkId func, int nresults) { lua_assert(ci->top <= L->stack_last); ci->u.l.savedpc = p->code; /* starting point */ ci->callstatus = 0; + if (p->is_vararg) + luaT_adjustvarargs(L, p, n); /* may invoke GC */ if (L->hookmask) hookcall(L, ci, 0); luaV_execute(L, ci); /* run the function */ diff --git a/ltm.c b/ltm.c index 4b14f197db..8108abb21d 100644 --- a/ltm.c +++ b/ltm.c @@ -1,5 +1,5 @@ /* -** $Id: ltm.c,v 2.55 2017/12/20 14:58:05 roberto Exp roberto $ +** $Id: ltm.c,v 2.56 2017/12/28 15:42:57 roberto Exp roberto $ ** Tag methods ** See Copyright Notice in lua.h */ @@ -16,6 +16,7 @@ #include "ldebug.h" #include "ldo.h" +#include "lgc.h" #include "lobject.h" #include "lstate.h" #include "lstring.h" @@ -231,6 +232,7 @@ void luaT_adjustvarargs (lua_State *L, Proto *p, int actual) { setivalue(luaH_set(L, vtab, &nname), actual); /* store counter there */ L->top -= actual; /* remove extra elements from the stack */ sethvalue2s(L, L->top - 1, vtab); /* move table to new top */ + luaC_checkGC(L); } From 89110986d7a9e81960261ae682780d5fd06dc4ac Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Sun, 28 Jan 2018 11:39:52 -0200 Subject: [PATCH 0193/1145] bug in tailcall of vararg functions (when adjusting missing parameters) --- ldo.c | 29 ++++++++++++++++------------- ltm.c | 17 ++++++++--------- ltm.h | 4 ++-- 3 files changed, 26 insertions(+), 24 deletions(-) diff --git a/ldo.c b/ldo.c index 50ab00129e..15bec17300 100644 --- a/ldo.c +++ b/ldo.c @@ -1,5 +1,5 @@ /* -** $Id: ldo.c,v 2.186 2018/01/10 19:19:27 roberto Exp roberto $ +** $Id: ldo.c,v 2.187 2018/01/28 12:08:04 roberto Exp roberto $ ** Stack and Call structure of Lua ** See Copyright Notice in lua.h */ @@ -405,25 +405,27 @@ void luaD_poscall (lua_State *L, CallInfo *ci, StkId firstResult, int nres) { /* ** Prepare a function for a tail call, building its call info on top -** of the current call info. 'n' is the number of arguments plus 1 +** of the current call info. 'narg1' is the number of arguments plus 1 ** (so that it includes the function itself). */ -void luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, int n) { +void luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, int narg1) { Proto *p = clLvalue(s2v(func))->p; int fsize = p->maxstacksize; /* frame size */ + int nfixparams = p->numparams; int i; - for (i = 0; i < n; i++) /* move down function and arguments */ + for (i = 0; i < narg1; i++) /* move down function and arguments */ setobjs2s(L, ci->func + i, func + i); checkstackp(L, fsize, func); - for (; i <= p->numparams; i++) - setnilvalue(s2v(ci->func + i)); /* complete missing arguments */ - ci->top = ci->func + 1 + fsize; /* top for new function */ + func = ci->func; /* moved-down function */ + for (; narg1 <= nfixparams; narg1++) + setnilvalue(s2v(func + narg1)); /* complete missing arguments */ + ci->top = func + 1 + fsize; /* top for new function */ lua_assert(ci->top <= L->stack_last); ci->u.l.savedpc = p->code; /* starting point */ ci->callstatus |= CIST_TAIL; if (p->is_vararg) { - L->top -= (func - ci->func); /* move down top */ - luaT_adjustvarargs(L, p, n - 1); + L->top = func + narg1; /* set top */ + luaT_adjustvarargs(L, nfixparams, narg1 - 1); } if (L->hookmask) hookcall(L, ci, 1); @@ -464,12 +466,13 @@ void luaD_call (lua_State *L, StkId func, int nresults) { luaD_poscall(L, ci, L->top - n, n); break; } - case LUA_TLCL: { /* Lua function: prepare its call */ + case LUA_TLCL: { /* Lua function */ Proto *p = clLvalue(funcv)->p; - int n = cast_int(L->top - func) - 1; /* number of real arguments */ + int narg = cast_int(L->top - func) - 1; /* number of real arguments */ + int nfixparams = p->numparams; int fsize = p->maxstacksize; /* frame size */ checkstackp(L, fsize, func); - for (; n < p->numparams; n++) + for (; narg < nfixparams; narg++) setnilvalue(s2v(L->top++)); /* complete missing arguments */ ci = next_ci(L); /* now 'enter' new function */ ci->nresults = nresults; @@ -479,7 +482,7 @@ void luaD_call (lua_State *L, StkId func, int nresults) { ci->u.l.savedpc = p->code; /* starting point */ ci->callstatus = 0; if (p->is_vararg) - luaT_adjustvarargs(L, p, n); /* may invoke GC */ + luaT_adjustvarargs(L, nfixparams, narg); /* may invoke GC */ if (L->hookmask) hookcall(L, ci, 0); luaV_execute(L, ci); /* run the function */ diff --git a/ltm.c b/ltm.c index 8108abb21d..64622f204e 100644 --- a/ltm.c +++ b/ltm.c @@ -1,5 +1,5 @@ /* -** $Id: ltm.c,v 2.56 2017/12/28 15:42:57 roberto Exp roberto $ +** $Id: ltm.c,v 2.57 2018/01/28 12:08:04 roberto Exp roberto $ ** Tag methods ** See Copyright Notice in lua.h */ @@ -216,21 +216,20 @@ int luaT_callorderiTM (lua_State *L, const TValue *p1, int v2, } -void luaT_adjustvarargs (lua_State *L, Proto *p, int actual) { +void luaT_adjustvarargs (lua_State *L, int nfixparams, int actual) { int i; Table *vtab; TValue nname; - int nfixparams = p->numparams; /* number of fixed parameters */ - actual -= nfixparams; /* number of extra arguments */ + int nextra = actual - nfixparams; /* number of extra arguments */ vtab = luaH_new(L); /* create vararg table */ sethvalue2s(L, L->top, vtab); /* anchor it for resizing */ L->top++; /* space ensured by caller */ - luaH_resize(L, vtab, actual, 1); - for (i = 0; i < actual; i++) /* put extra arguments into vararg table */ - setobj2n(L, &vtab->array[i], s2v(L->top - actual + i - 1)); + luaH_resize(L, vtab, nextra, 1); + for (i = 0; i < nextra; i++) /* put extra arguments into vararg table */ + setobj2n(L, &vtab->array[i], s2v(L->top - nextra + i - 1)); setsvalue(L, &nname, G(L)->nfield); /* get field 'n' */ - setivalue(luaH_set(L, vtab, &nname), actual); /* store counter there */ - L->top -= actual; /* remove extra elements from the stack */ + setivalue(luaH_set(L, vtab, &nname), nextra); /* store counter there */ + L->top -= nextra; /* remove extra elements from the stack */ sethvalue2s(L, L->top - 1, vtab); /* move table to new top */ luaC_checkGC(L); } diff --git a/ltm.h b/ltm.h index 34dbc82cc4..fbba067ad4 100644 --- a/ltm.h +++ b/ltm.h @@ -1,5 +1,5 @@ /* -** $Id: ltm.h,v 2.27 2017/11/27 17:44:31 roberto Exp roberto $ +** $Id: ltm.h,v 2.28 2017/12/13 18:32:09 roberto Exp roberto $ ** Tag methods ** See Copyright Notice in lua.h */ @@ -77,7 +77,7 @@ LUAI_FUNC int luaT_callorderTM (lua_State *L, const TValue *p1, LUAI_FUNC int luaT_callorderiTM (lua_State *L, const TValue *p1, int v2, int inv, TMS event); -LUAI_FUNC void luaT_adjustvarargs (lua_State *L, Proto *p, int actual); +LUAI_FUNC void luaT_adjustvarargs (lua_State *L, int nfixparams, int actual); LUAI_FUNC void luaT_getvarargs (lua_State *L, TValue *t, StkId where, int wanted); From e2b15aa21d2f31ccc93e35f50928e26a8d9c84ce Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Sun, 28 Jan 2018 13:13:26 -0200 Subject: [PATCH 0194/1145] janitor work on casts --- lapi.c | 8 ++++---- lcode.c | 4 ++-- ldebug.h | 4 ++-- lfunc.h | 10 +++++----- lgc.c | 4 ++-- lgc.h | 6 +++--- llex.c | 4 ++-- llex.h | 4 ++-- llimits.h | 9 +++++++-- lmem.c | 10 +++++----- lmem.h | 14 ++++++++------ lobject.c | 16 ++++++++-------- lobject.h | 8 ++++---- lopcodes.h | 12 ++++++------ lstate.c | 6 +++--- lstring.c | 4 ++-- ltable.c | 10 +++++----- ltests.c | 26 +++++++++++++------------- 18 files changed, 83 insertions(+), 76 deletions(-) diff --git a/lapi.c b/lapi.c index 142baf6a86..3d148e094a 100644 --- a/lapi.c +++ b/lapi.c @@ -1,5 +1,5 @@ /* -** $Id: lapi.c,v 2.279 2017/12/08 17:28:25 roberto Exp roberto $ +** $Id: lapi.c,v 2.280 2018/01/10 12:02:35 roberto Exp roberto $ ** Lua API ** See Copyright Notice in lua.h */ @@ -439,7 +439,7 @@ LUA_API const void *lua_topointer (lua_State *L, int idx) { case LUA_TTABLE: return hvalue(o); case LUA_TLCL: return clLvalue(o); case LUA_TCCL: return clCvalue(o); - case LUA_TLCF: return cast(void *, cast(size_t, fvalue(o))); + case LUA_TLCF: return cast_voidp(cast_sizet(fvalue(o))); case LUA_TTHREAD: return thvalue(o); case LUA_TUSERDATA: return getudatamem(uvalue(o)); case LUA_TLIGHTUSERDATA: return pvalue(o); @@ -685,7 +685,7 @@ LUA_API int lua_rawgetp (lua_State *L, int idx, const void *p) { lua_lock(L); t = index2value(L, idx); api_check(L, ttistable(t), "table expected"); - setpvalue(&k, cast(void *, p)); + setpvalue(&k, cast_voidp(p)); setobj2s(L, L->top, luaH_get(hvalue(t), &k)); api_incr_top(L); lua_unlock(L); @@ -854,7 +854,7 @@ LUA_API void lua_rawsetp (lua_State *L, int idx, const void *p) { api_checknelems(L, 1); o = index2value(L, idx); api_check(L, ttistable(o), "table expected"); - setpvalue(&k, cast(void *, p)); + setpvalue(&k, cast_voidp(p)); slot = luaH_set(L, hvalue(o), &k); setobj2t(L, slot, s2v(L->top - 1)); luaC_barrierback(L, hvalue(o), s2v(L->top - 1)); diff --git a/lcode.c b/lcode.c index 7e573f0266..7f240580bf 100644 --- a/lcode.c +++ b/lcode.c @@ -1,5 +1,5 @@ /* -** $Id: lcode.c,v 2.150 2018/01/18 16:24:31 roberto Exp roberto $ +** $Id: lcode.c,v 2.151 2018/01/27 16:56:33 roberto Exp roberto $ ** Code generator for Lua ** See Copyright Notice in lua.h */ @@ -545,7 +545,7 @@ int luaK_stringK (FuncState *fs, TString *s) { */ static int luaK_intK (FuncState *fs, lua_Integer n) { TValue k, o; - setpvalue(&k, cast(void*, cast(size_t, n))); + setpvalue(&k, cast_voidp(cast_sizet(n))); setivalue(&o, n); return addk(fs, &k, &o); } diff --git a/ldebug.h b/ldebug.h index 9062f4bd0c..e8652d8406 100644 --- a/ldebug.h +++ b/ldebug.h @@ -1,5 +1,5 @@ /* -** $Id: ldebug.h,v 2.14 2015/05/22 17:45:56 roberto Exp roberto $ +** $Id: ldebug.h,v 2.15 2017/06/27 11:35:31 roberto Exp roberto $ ** Auxiliary functions from Debug Interface module ** See Copyright Notice in lua.h */ @@ -11,7 +11,7 @@ #include "lstate.h" -#define pcRel(pc, p) (cast(int, (pc) - (p)->code) - 1) +#define pcRel(pc, p) (cast_int((pc) - (p)->code) - 1) #define resethookcount(L) (L->hookcount = L->basehookcount) diff --git a/lfunc.h b/lfunc.h index b7d758694c..dbda3fdf49 100644 --- a/lfunc.h +++ b/lfunc.h @@ -1,5 +1,5 @@ /* -** $Id: lfunc.h,v 2.17 2017/05/04 13:32:01 roberto Exp roberto $ +** $Id: lfunc.h,v 2.18 2017/06/29 15:06:44 roberto Exp roberto $ ** Auxiliary functions to manipulate prototypes and closures ** See Copyright Notice in lua.h */ @@ -11,11 +11,11 @@ #include "lobject.h" -#define sizeCclosure(n) (cast(int, sizeof(CClosure)) + \ - cast(int, sizeof(TValue)*((n)-1))) +#define sizeCclosure(n) (cast_int(sizeof(CClosure)) + \ + cast_int(sizeof(TValue)*((n)-1))) -#define sizeLclosure(n) (cast(int, sizeof(LClosure)) + \ - cast(int, sizeof(TValue *)*((n)-1))) +#define sizeLclosure(n) (cast_int(sizeof(LClosure)) + \ + cast_int(sizeof(TValue *)*((n)-1))) /* test whether thread is in 'twups' list */ diff --git a/lgc.c b/lgc.c index d02dc031c9..7b223bcb24 100644 --- a/lgc.c +++ b/lgc.c @@ -1,5 +1,5 @@ /* -** $Id: lgc.c,v 2.243 2017/12/20 14:58:05 roberto Exp roberto $ +** $Id: lgc.c,v 2.244 2017/12/28 15:42:57 roberto Exp roberto $ ** Garbage Collector ** See Copyright Notice in lua.h */ @@ -107,7 +107,7 @@ static lu_mem atomic (lua_State *L); /* ** one after last element in a hash array */ -#define gnodelast(h) gnode(h, cast(size_t, sizenode(h))) +#define gnodelast(h) gnode(h, cast_sizet(sizenode(h))) /* diff --git a/lgc.h b/lgc.h index 5113e922f0..fa53f13d71 100644 --- a/lgc.h +++ b/lgc.h @@ -1,5 +1,5 @@ /* -** $Id: lgc.h,v 2.98 2017/05/26 19:14:29 roberto Exp roberto $ +** $Id: lgc.h,v 2.99 2017/10/11 12:38:45 roberto Exp roberto $ ** Garbage Collector ** See Copyright Notice in lua.h */ @@ -57,7 +57,7 @@ /* ** some useful bit tricks */ -#define resetbits(x,m) ((x) &= cast(lu_byte, ~(m))) +#define resetbits(x,m) ((x) &= cast_byte(~(m))) #define setbits(x,m) ((x) |= (m)) #define testbits(x,m) ((x) & (m)) #define bitmask(b) (1<<(b)) @@ -95,7 +95,7 @@ #define changewhite(x) ((x)->marked ^= WHITEBITS) #define gray2black(x) l_setbit((x)->marked, BLACKBIT) -#define luaC_white(g) cast(lu_byte, (g)->currentwhite & WHITEBITS) +#define luaC_white(g) cast_byte((g)->currentwhite & WHITEBITS) /* object age in generational mode */ diff --git a/llex.c b/llex.c index eaa400fad2..be77514630 100644 --- a/llex.c +++ b/llex.c @@ -1,5 +1,5 @@ /* -** $Id: llex.c,v 2.97 2017/06/09 16:48:44 roberto Exp roberto $ +** $Id: llex.c,v 2.98 2017/06/29 15:06:44 roberto Exp roberto $ ** Lexical Analyzer ** See Copyright Notice in lua.h */ @@ -63,7 +63,7 @@ static void save (LexState *ls, int c) { newsize = luaZ_sizebuffer(b) * 2; luaZ_resizebuffer(ls->L, b, newsize); } - b->buffer[luaZ_bufflen(b)++] = cast(char, c); + b->buffer[luaZ_bufflen(b)++] = cast_char(c); } diff --git a/llex.h b/llex.h index a50b687351..9f23bd8932 100644 --- a/llex.h +++ b/llex.h @@ -1,5 +1,5 @@ /* -** $Id: llex.h,v 1.78 2014/10/29 15:38:24 roberto Exp roberto $ +** $Id: llex.h,v 1.79 2016/05/02 14:02:12 roberto Exp roberto $ ** Lexical Analyzer ** See Copyright Notice in lua.h */ @@ -37,7 +37,7 @@ enum RESERVED { }; /* number of reserved words */ -#define NUM_RESERVED (cast(int, TK_WHILE-FIRST_RESERVED+1)) +#define NUM_RESERVED (cast_int(TK_WHILE-FIRST_RESERVED + 1)) typedef union { diff --git a/llimits.h b/llimits.h index a9295645b2..9a3ae8d051 100644 --- a/llimits.h +++ b/llimits.h @@ -1,5 +1,5 @@ /* -** $Id: llimits.h,v 1.147 2017/12/11 18:53:53 roberto Exp roberto $ +** $Id: llimits.h,v 1.148 2017/12/28 11:51:00 roberto Exp roberto $ ** Limits, basic types, and some other 'installation-dependent' definitions ** See Copyright Notice in lua.h */ @@ -104,10 +104,15 @@ typedef LUAI_UACINT l_uacInt; #define cast(t, exp) ((t)(exp)) #define cast_void(i) cast(void, (i)) -#define cast_byte(i) cast(lu_byte, (i)) +#define cast_voidp(i) cast(void *, (i)) #define cast_num(i) cast(lua_Number, (i)) #define cast_int(i) cast(int, (i)) +#define cast_uint(i) cast(unsigned int, (i)) +#define cast_byte(i) cast(lu_byte, (i)) #define cast_uchar(i) cast(unsigned char, (i)) +#define cast_char(i) cast(char, (i)) +#define cast_charp(i) cast(char *, (i)) +#define cast_sizet(i) cast(size_t, (i)) /* cast a signed lua_Integer to lua_Unsigned */ diff --git a/lmem.c b/lmem.c index ecafef4932..2c1757f528 100644 --- a/lmem.c +++ b/lmem.c @@ -1,5 +1,5 @@ /* -** $Id: lmem.c,v 1.94 2017/12/08 17:28:25 roberto Exp roberto $ +** $Id: lmem.c,v 1.95 2017/12/11 12:27:48 roberto Exp roberto $ ** Interface to Memory Manager ** See Copyright Notice in lua.h */ @@ -71,8 +71,8 @@ void *luaM_growaux_ (lua_State *L, void *block, int nelems, int *psize, } lua_assert(nelems + 1 <= size && size <= limit); /* 'limit' ensures that multiplication will not overflow */ - newblock = luaM_realloc_(L, block, cast(size_t, *psize) * size_elems, - cast(size_t, size) * size_elems); + newblock = luaM_realloc_(L, block, cast_sizet(*psize) * size_elems, + cast_sizet(size) * size_elems); if (newblock == NULL) luaM_error(L); *psize = size; /* update only when everything else is OK */ @@ -84,8 +84,8 @@ void *luaM_shrinkvector_ (lua_State *L, void *block, int *size, int final_n, int size_elem) { global_State *g = G(L); void *newblock; - size_t oldsize = cast(size_t, (*size) * size_elem); - size_t newsize = cast(size_t, final_n * size_elem); + size_t oldsize = cast_sizet((*size) * size_elem); + size_t newsize = cast_sizet(final_n * size_elem); lua_assert(newsize <= oldsize); newblock = (*g->frealloc)(g->ud, block, oldsize, newsize); if (newblock == NULL && final_n > 0) /* allocation failed? */ diff --git a/lmem.h b/lmem.h index e98aabdbdc..f87c913248 100644 --- a/lmem.h +++ b/lmem.h @@ -1,5 +1,5 @@ /* -** $Id: lmem.h,v 1.45 2017/12/07 18:59:52 roberto Exp roberto $ +** $Id: lmem.h,v 1.46 2017/12/08 17:28:25 roberto Exp roberto $ ** Interface to Memory Manager ** See Copyright Notice in lua.h */ @@ -29,7 +29,7 @@ ** avoiding this warning but also this optimization.) */ #define luaM_testsize(n,e) \ - (sizeof(n) >= sizeof(size_t) && cast(size_t, (n)) + 1 > MAX_SIZET/(e)) + (sizeof(n) >= sizeof(size_t) && cast_sizet((n)) + 1 > MAX_SIZET/(e)) #define luaM_checksize(L,n,e) \ (luaM_testsize(n,e) ? luaM_toobig(L) : cast_void(0)) @@ -42,13 +42,15 @@ ** 'int' or 'unsigned int' and that 'int' is not larger than 'size_t'.) */ #define luaM_limitN(n,t) \ - ((cast(size_t, n) > MAX_SIZET/sizeof(t)) ? (MAX_SIZET/sizeof(t)) : (n)) + ((cast_sizet(n) <= MAX_SIZET/sizeof(t)) ? (n) : \ + cast_uint((MAX_SIZET/sizeof(t)))) + /* ** Arrays of chars do not need any test */ #define luaM_reallocvchar(L,b,on,n) \ - cast(char *, luaM_saferealloc_(L, (b), (on)*sizeof(char), (n)*sizeof(char))) + cast_charp(luaM_saferealloc_(L, (b), (on)*sizeof(char), (n)*sizeof(char))) #define luaM_freemem(L, b, s) luaM_free_(L, (b), (s)) #define luaM_free(L, b) luaM_free_(L, (b), sizeof(*(b))) @@ -66,8 +68,8 @@ luaM_limitN(limit,t),e))) #define luaM_reallocvector(L, v,oldn,n,t) \ - (cast(t *, luaM_realloc_(L, v, cast(size_t, oldn) * sizeof(t), \ - cast(size_t, n) * sizeof(t)))) + (cast(t *, luaM_realloc_(L, v, cast_sizet(oldn) * sizeof(t), \ + cast_sizet(n) * sizeof(t)))) #define luaM_shrinkvector(L,v,size,fs,t) \ ((v)=cast(t *, luaM_shrinkvector_(L, v, &(size), fs, sizeof(t)))) diff --git a/lobject.c b/lobject.c index c7d4c06a66..d5e9c1ce98 100644 --- a/lobject.c +++ b/lobject.c @@ -1,5 +1,5 @@ /* -** $Id: lobject.c,v 2.121 2017/11/23 19:29:04 roberto Exp roberto $ +** $Id: lobject.c,v 2.122 2017/12/30 20:46:18 roberto Exp roberto $ ** Some generic functions over Lua objects ** See Copyright Notice in lua.h */ @@ -204,7 +204,7 @@ static lua_Number lua_strx2number (const char *s, char **endptr) { int e = 0; /* exponent correction */ int neg; /* 1 if number is negative */ int hasdot = 0; /* true after seen a dot */ - *endptr = cast(char *, s); /* nothing is valid yet */ + *endptr = cast_charp(s); /* nothing is valid yet */ while (lisspace(cast_uchar(*s))) s++; /* skip initial spaces */ neg = isneg(&s); /* check sign */ if (!(*s == '0' && (*(s + 1) == 'x' || *(s + 1) == 'X'))) /* check '0x' */ @@ -226,7 +226,7 @@ static lua_Number lua_strx2number (const char *s, char **endptr) { } if (nosigdig + sigdig == 0) /* no digits? */ return 0.0; /* invalid format */ - *endptr = cast(char *, s); /* valid up to here */ + *endptr = cast_charp(s); /* valid up to here */ e *= 4; /* each digit multiplies/divides value by 2^4 */ if (*s == 'p' || *s == 'P') { /* exponent part? */ int exp1 = 0; /* exponent value */ @@ -239,7 +239,7 @@ static lua_Number lua_strx2number (const char *s, char **endptr) { exp1 = exp1 * 10 + *(s++) - '0'; if (neg1) exp1 = -exp1; e += exp1; - *endptr = cast(char *, s); /* valid up to here */ + *endptr = cast_charp(s); /* valid up to here */ } if (neg) r = -r; return l_mathop(ldexp)(r, e); @@ -353,15 +353,15 @@ int luaO_utf8esc (char *buff, unsigned long x) { int n = 1; /* number of bytes put in buffer (backwards) */ lua_assert(x <= 0x10FFFF); if (x < 0x80) /* ascii? */ - buff[UTF8BUFFSZ - 1] = cast(char, x); + buff[UTF8BUFFSZ - 1] = cast_char(x); else { /* need continuation bytes */ unsigned int mfb = 0x3f; /* maximum that fits in first byte */ do { /* add continuation bytes */ - buff[UTF8BUFFSZ - (n++)] = cast(char, 0x80 | (x & 0x3f)); + buff[UTF8BUFFSZ - (n++)] = cast_char(0x80 | (x & 0x3f)); x >>= 6; /* remove added bits */ mfb >>= 1; /* now there is one less bit available in first byte */ } while (x > mfb); /* still needs continuation byte? */ - buff[UTF8BUFFSZ - n] = cast(char, (~mfb << 1) | x); /* add first byte */ + buff[UTF8BUFFSZ - n] = cast_char((~mfb << 1) | x); /* add first byte */ } return n; } @@ -417,7 +417,7 @@ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) { break; } case 'c': { /* an 'int' as a character */ - char buff = cast(char, va_arg(argp, int)); + char buff = cast_char(va_arg(argp, int)); if (lisprint(cast_uchar(buff))) pushstr(L, &buff, 1); else /* non-printable character; print its code */ diff --git a/lobject.h b/lobject.h index 6541dea4d9..01bd39fa16 100644 --- a/lobject.h +++ b/lobject.h @@ -1,5 +1,5 @@ /* -** $Id: lobject.h,v 2.131 2017/11/23 19:29:04 roberto Exp roberto $ +** $Id: lobject.h,v 2.132 2018/01/28 12:07:53 roberto Exp roberto $ ** Type definitions for Lua objects ** See Copyright Notice in lua.h */ @@ -351,7 +351,7 @@ typedef union UTString { ** (Access to 'extra' ensures that value is really a 'TString'.) */ #define getstr(ts) \ - check_exp(sizeof((ts)->extra), cast(char *, (ts)) + sizeof(UTString)) + check_exp(sizeof((ts)->extra), cast_charp((ts)) + sizeof(UTString)) /* get the actual string (array of bytes) from a Lua value */ @@ -391,7 +391,7 @@ typedef union UUdata { ** (Access to 'ttuv_' ensures that value is really a 'Udata'.) */ #define getudatamem(u) \ - check_exp(sizeof((u)->ttuv_), (cast(char*, (u)) + sizeof(UUdata))) + check_exp(sizeof((u)->ttuv_), (cast_charp(u) + sizeof(UUdata))) #define setuservalue(L,u,o) \ { const TValue *io=(o); Udata *iu = (u); \ @@ -607,7 +607,7 @@ typedef struct Table { ** 'module' operation for hashing (size is always a power of 2) */ #define lmod(s,size) \ - (check_exp((size&(size-1))==0, (cast(int, (s) & ((size)-1))))) + (check_exp((size&(size-1))==0, (cast_int((s) & ((size)-1))))) #define twoto(x) (1<<(x)) diff --git a/lopcodes.h b/lopcodes.h index 2f4a48fa76..1e3974d9e9 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -1,5 +1,5 @@ /* -** $Id: lopcodes.h,v 1.182 2018/01/09 11:24:12 roberto Exp roberto $ +** $Id: lopcodes.h,v 1.183 2018/01/27 16:56:33 roberto Exp roberto $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ @@ -111,7 +111,7 @@ enum OpMode {iABC, iABx, iAsBx, iAx, isJ}; /* basic instruction formats */ #define checkopm(i,m) (getOpMode(GET_OPCODE(i)) == m) -#define getarg(i,pos,size) (cast(int, ((i)>>(pos)) & MASK1(size,0))) +#define getarg(i,pos,size) (cast_int(((i)>>(pos)) & MASK1(size,0))) #define setarg(i,v,pos,size) ((i) = (((i)&MASK0(size,pos)) | \ ((cast(Instruction, v)< -#define luai_makeseed() cast(unsigned int, time(NULL)) +#define luai_makeseed() cast_uint(time(NULL)) #endif @@ -67,7 +67,7 @@ typedef struct LG { ** Layout Randomization (if present) to increase randomness.. */ #define addbuff(b,p,e) \ - { size_t t = cast(size_t, e); \ + { size_t t = cast_sizet(e); \ memcpy(b + p, &t, sizeof(t)); p += sizeof(t); } static unsigned int makeseed (lua_State *L) { diff --git a/lstring.c b/lstring.c index 60d4702de6..f22ac5b8d6 100644 --- a/lstring.c +++ b/lstring.c @@ -1,5 +1,5 @@ /* -** $Id: lstring.c,v 2.61 2017/12/12 11:52:35 roberto Exp roberto $ +** $Id: lstring.c,v 2.62 2017/12/18 13:00:57 roberto Exp roberto $ ** String table (keeps all strings handled by Lua) ** See Copyright Notice in lua.h */ @@ -51,7 +51,7 @@ int luaS_eqlngstr (TString *a, TString *b) { unsigned int luaS_hash (const char *str, size_t l, unsigned int seed) { - unsigned int h = seed ^ cast(unsigned int, l); + unsigned int h = seed ^ cast_uint(l); size_t step = (l >> LUAI_HASHLIMIT) + 1; for (; l >= step; l -= step) h ^= ((h<<5) + (h>>2) + cast_byte(str[l - 1])); diff --git a/ltable.c b/ltable.c index 419f9f6c5d..a892996145 100644 --- a/ltable.c +++ b/ltable.c @@ -1,5 +1,5 @@ /* -** $Id: ltable.c,v 2.129 2017/12/08 17:28:25 roberto Exp roberto $ +** $Id: ltable.c,v 2.130 2017/12/29 15:58:23 roberto Exp roberto $ ** Lua tables (hash) ** See Copyright Notice in lua.h */ @@ -116,8 +116,8 @@ static int l_hashfloat (lua_Number n) { return 0; } else { /* normal case */ - unsigned int u = cast(unsigned int, i) + cast(unsigned int, ni); - return cast_int(u <= cast(unsigned int, INT_MAX) ? u : ~u); + unsigned int u = cast_uint(i) + cast_uint(ni); + return cast_int(u <= cast_uint(INT_MAX) ? u : ~u); } } #endif @@ -213,7 +213,7 @@ static const TValue *getgeneric (Table *t, const TValue *key) { */ static unsigned int arrayindex (lua_Integer k) { if (0 < k && l_castS2U(k) <= MAXASIZE) - return cast(unsigned int, k); /* 'key' is an appropriate array index */ + return cast_uint(k); /* 'key' is an appropriate array index */ else return 0; } @@ -264,7 +264,7 @@ int luaH_next (lua_State *L, Table *t, StkId key) { static void freehash (lua_State *L, Table *t) { if (!isdummy(t)) - luaM_freearray(L, t->node, cast(size_t, sizenode(t))); + luaM_freearray(L, t->node, cast_sizet(sizenode(t))); } diff --git a/ltests.c b/ltests.c index 569c1896c1..24753d5fa6 100644 --- a/ltests.c +++ b/ltests.c @@ -1,5 +1,5 @@ /* -** $Id: ltests.c,v 2.239 2018/01/09 11:21:41 roberto Exp $ +** $Id: ltests.c,v 2.239 2018/01/09 11:24:12 roberto Exp roberto $ ** Internal Module for Debugging of the Lua Implementation ** See Copyright Notice in lua.h */ @@ -110,7 +110,7 @@ static void freeblock (Memcontrol *mc, Header *block) { size_t size = block->d.size; int i; for (i = 0; i < MARKSIZE; i++) /* check marks after block */ - lua_assert(*(cast(char *, block + 1) + size + i) == MARK); + lua_assert(*(cast_charp(block + 1) + size + i) == MARK); mc->objcount[block->d.type]--; fillmem(block, sizeof(Header) + size + MARKSIZE); /* erase block */ free(block); /* actually free block */ @@ -161,10 +161,10 @@ void *debug_realloc (void *ud, void *b, size_t oldsize, size_t size) { freeblock(mc, block); /* erase (and check) old copy */ } /* initialize new part of the block with something weird */ - fillmem(cast(char *, newblock + 1) + commonsize, size - commonsize); + fillmem(cast_charp(newblock + 1) + commonsize, size - commonsize); /* initialize marks after block */ for (i = 0; i < MARKSIZE; i++) - *(cast(char *, newblock + 1) + size + i) = MARK; + *(cast_charp(newblock + 1) + size + i) = MARK; newblock->d.size = size; newblock->d.type = type; mc->total += size; @@ -919,8 +919,8 @@ static int upvalue (lua_State *L) { static int newuserdata (lua_State *L) { - size_t size = cast(size_t, luaL_checkinteger(L, 1)); - char *p = cast(char *, lua_newuserdata(L, size)); + size_t size = cast_sizet(luaL_checkinteger(L, 1)); + char *p = cast_charp(lua_newuserdata(L, size)); while (size--) *p++ = '\0'; return 1; } @@ -928,7 +928,7 @@ static int newuserdata (lua_State *L) { static int pushuserdata (lua_State *L) { lua_Integer u = luaL_checkinteger(L, 1); - lua_pushlightuserdata(L, cast(void *, cast(size_t, u))); + lua_pushlightuserdata(L, cast_voidp(cast_sizet(u))); return 1; } @@ -959,7 +959,7 @@ static int s2d (lua_State *L) { static int d2s (lua_State *L) { double d = luaL_checknumber(L, 1); - lua_pushlstring(L, cast(char *, &d), sizeof(d)); + lua_pushlstring(L, cast_charp(&d), sizeof(d)); return 1; } @@ -1277,7 +1277,7 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) { } else if EQ("func2num") { lua_CFunction func = lua_tocfunction(L1, getindex); - lua_pushnumber(L1, cast(size_t, func)); + lua_pushnumber(L1, cast_sizet(func)); } else if EQ("getfield") { int t = getindex; @@ -1422,11 +1422,11 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) { } else if EQ("rawgetp") { int t = getindex; - lua_rawgetp(L1, t, cast(void *, cast(size_t, getnum))); + lua_rawgetp(L1, t, cast_voidp(cast_sizet(getnum))); } else if EQ("rawsetp") { int t = getindex; - lua_rawsetp(L1, t, cast(void *, cast(size_t, getnum))); + lua_rawsetp(L1, t, cast_voidp(cast_sizet(getnum))); } else if EQ("remove") { lua_remove(L1, getnum); @@ -1511,7 +1511,7 @@ static struct X { int x; } x; lua_pushnumber(L1, lua_tonumber(L1, getindex)); } else if EQ("topointer") { - lua_pushnumber(L1, cast(size_t, lua_topointer(L1, getindex))); + lua_pushnumber(L1, cast_sizet(lua_topointer(L1, getindex))); } else if EQ("tostring") { const char *s = lua_tostring(L1, getindex); @@ -1725,7 +1725,7 @@ int luaB_opentests (lua_State *L) { lua_atpanic(L, &tpanic); atexit(checkfinalmem); lua_assert(lua_getallocf(L, &ud) == debug_realloc); - lua_assert(ud == cast(void *, &l_memcontrol)); + lua_assert(ud == cast_voidp(&l_memcontrol)); lua_setallocf(L, lua_getallocf(L, NULL), ud); luaL_newlib(L, tests_funcs); return 1; From dc0ab1e8ca38400d5d7b3a7d00ff7f6c33fdf3d3 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 29 Jan 2018 14:21:35 -0200 Subject: [PATCH 0195/1145] warnings in VS (implicit casts from ptrdiff_t to int) --- lapi.c | 4 ++-- ldo.c | 4 ++-- lvm.c | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lapi.c b/lapi.c index 3d148e094a..63b66f92fe 100644 --- a/lapi.c +++ b/lapi.c @@ -1,5 +1,5 @@ /* -** $Id: lapi.c,v 2.280 2018/01/10 12:02:35 roberto Exp roberto $ +** $Id: lapi.c,v 2.281 2018/01/28 15:13:26 roberto Exp roberto $ ** Lua API ** See Copyright Notice in lua.h */ @@ -993,7 +993,7 @@ LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc, ci->u.c.k = k; /* save continuation */ ci->u.c.ctx = ctx; /* save context */ /* save information for error recovery */ - ci->u2.funcidx = savestack(L, c.func); + ci->u2.funcidx = cast_int(savestack(L, c.func)); ci->u.c.old_errfunc = L->errfunc; L->errfunc = func; setoah(ci->callstatus, L->allowhook); /* save value of 'allowhook' */ diff --git a/ldo.c b/ldo.c index 15bec17300..6ab57f8e00 100644 --- a/ldo.c +++ b/ldo.c @@ -1,5 +1,5 @@ /* -** $Id: ldo.c,v 2.187 2018/01/28 12:08:04 roberto Exp roberto $ +** $Id: ldo.c,v 2.188 2018/01/28 13:39:52 roberto Exp roberto $ ** Stack and Call structure of Lua ** See Copyright Notice in lua.h */ @@ -673,7 +673,7 @@ LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs, else lua_assert(status == L->status); /* normal end or yield */ } *nresults = (status == LUA_YIELD) ? L->ci->u2.nyield - : L->top - (L->ci->func + 1); + : cast_int(L->top - (L->ci->func + 1)); L->nny = oldnny; /* restore 'nny' */ L->nCcalls--; // lua_assert(L->nCcalls == ((from) ? from->nCcalls : 0)); diff --git a/lvm.c b/lvm.c index 73256267a4..d6380e4b7a 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.334 2018/01/14 17:27:50 roberto Exp roberto $ +** $Id: lvm.c,v 2.335 2018/01/27 16:56:33 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -1488,7 +1488,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { if (b != 0) L->top = ra + b; else /* previous instruction set top */ - b = L->top - ra; + b = cast_int(L->top - ra); lua_assert(GETARG_C(i) - 1 == LUA_MULTRET); if (!ttisfunction(vra)) { /* not a function? */ ProtectNT(luaD_tryfuncTM(L, ra)); /* try '__call' metamethod */ From 90569630d6755190c5f499c51e1d0e6ef41c6cd8 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 29 Jan 2018 17:13:27 -0200 Subject: [PATCH 0196/1145] detail (uses a reserved-format name for an internal type in the registry) --- lauxlib.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lauxlib.c b/lauxlib.c index 2c26107739..09d89d8040 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -1,5 +1,5 @@ /* -** $Id: lauxlib.c,v 1.290 2017/04/24 18:06:12 roberto Exp roberto $ +** $Id: lauxlib.c,v 1.291 2017/06/27 18:32:49 roberto Exp roberto $ ** Auxiliary functions for building Lua libraries ** See Copyright Notice in lua.h */ @@ -483,7 +483,7 @@ static void *newbox (lua_State *L, size_t newsize) { UBox *box = (UBox *)lua_newuserdata(L, sizeof(UBox)); box->box = NULL; box->bsize = 0; - if (luaL_newmetatable(L, "LUABOX")) { /* creating metatable? */ + if (luaL_newmetatable(L, "_UBOX*")) { /* creating metatable? */ lua_pushcfunction(L, boxgc); lua_setfield(L, -2, "__gc"); /* metatable.__gc = boxgc */ } From 56e50e8bc546884957fbe1d82ff78d27f1eef93c Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 5 Feb 2018 15:10:52 -0200 Subject: [PATCH 0197/1145] 'collectgarbage' returns old mode when changing mode --- lapi.c | 6 +++++- lbaselib.c | 14 +++++++++----- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/lapi.c b/lapi.c index 63b66f92fe..d4bf7e17e3 100644 --- a/lapi.c +++ b/lapi.c @@ -1,5 +1,5 @@ /* -** $Id: lapi.c,v 2.281 2018/01/28 15:13:26 roberto Exp roberto $ +** $Id: lapi.c,v 2.282 2018/01/29 16:21:35 roberto Exp roberto $ ** Lua API ** See Copyright Notice in lua.h */ @@ -1121,6 +1121,7 @@ LUA_API int lua_gc (lua_State *L, int what, ...) { break; } case LUA_GCGEN: { + int oldmode = g->gckind; int minormul = va_arg(argp, int); int majormul = va_arg(argp, int); if (minormul != 0) @@ -1128,9 +1129,11 @@ LUA_API int lua_gc (lua_State *L, int what, ...) { if (majormul != 0) setgcparam(g->genmajormul, majormul); luaC_changemode(L, KGC_GEN); + res = (oldmode == KGC_GEN) ? LUA_GCGEN : LUA_GCINC; break; } case LUA_GCINC: { + int oldmode = g->gckind; int pause = va_arg(argp, int); int stepmul = va_arg(argp, int); int stepsize = va_arg(argp, int); @@ -1141,6 +1144,7 @@ LUA_API int lua_gc (lua_State *L, int what, ...) { if (stepsize != 0) g->gcstepsize = stepsize; luaC_changemode(L, KGC_INC); + res = (oldmode == KGC_GEN) ? LUA_GCGEN : LUA_GCINC; break; } default: res = -1; /* invalid option */ diff --git a/lbaselib.c b/lbaselib.c index 00452f2d3a..c95a6e535c 100644 --- a/lbaselib.c +++ b/lbaselib.c @@ -1,5 +1,5 @@ /* -** $Id: lbaselib.c,v 1.317 2017/06/27 18:32:49 roberto Exp roberto $ +** $Id: lbaselib.c,v 1.318 2017/11/16 13:19:06 roberto Exp roberto $ ** Basic library ** See Copyright Notice in lua.h */ @@ -170,6 +170,12 @@ static int luaB_rawset (lua_State *L) { } +static int pushmode (lua_State *L, int oldmode) { + lua_pushstring(L, (oldmode == LUA_GCINC) ? "incremental" : "generational"); + return 1; +} + + static int luaB_collectgarbage (lua_State *L) { static const char *const opts[] = {"stop", "restart", "collect", "count", "step", "setpause", "setstepmul", @@ -206,15 +212,13 @@ static int luaB_collectgarbage (lua_State *L) { case LUA_GCGEN: { int minormul = (int)luaL_optinteger(L, 2, 0); int majormul = (int)luaL_optinteger(L, 3, 0); - lua_gc(L, o, minormul, majormul); - return 0; + return pushmode(L, lua_gc(L, o, minormul, majormul)); } case LUA_GCINC: { int pause = (int)luaL_optinteger(L, 2, 0); int stepmul = (int)luaL_optinteger(L, 3, 0); int stepsize = (int)luaL_optinteger(L, 4, 0); - lua_gc(L, o, pause, stepmul, stepsize); - return 0; + return pushmode(L, lua_gc(L, o, pause, stepmul, stepsize)); } default: { int res = lua_gc(L, o); From 022abc301b5fc7fb5c1bf6833e69cad0e5c32b0c Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 5 Feb 2018 15:11:37 -0200 Subject: [PATCH 0198/1145] 'gcemergency' should be zero before any memory allocation --- lstate.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lstate.c b/lstate.c index 1cbb172413..6c92f2f0cf 100644 --- a/lstate.c +++ b/lstate.c @@ -1,5 +1,5 @@ /* -** $Id: lstate.c,v 2.149 2017/12/19 16:40:17 roberto Exp roberto $ +** $Id: lstate.c,v 2.150 2018/01/28 15:13:26 roberto Exp roberto $ ** Global State ** See Copyright Notice in lua.h */ @@ -227,7 +227,6 @@ static void f_luaopen (lua_State *L, void *ud) { luaT_init(L); luaX_init(L); g->gcrunning = 1; /* allow gc */ - g->gcemergency = 0; g->version = lua_version(NULL); luai_userstateopen(L); } @@ -337,6 +336,7 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { g->version = NULL; g->gcstate = GCSpause; g->gckind = KGC_INC; + g->gcemergency = 0; g->finobj = g->tobefnz = g->fixedgc = NULL; g->survival = g->old = g->reallyold = NULL; g->finobjsur = g->finobjold = g->finobjrold = NULL; From a131eae925cad604ab1bb248e0c2eda83c54bf82 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 5 Feb 2018 15:14:29 -0200 Subject: [PATCH 0199/1145] default for minor collection intervals a little larger --- lgc.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lgc.h b/lgc.h index fa53f13d71..fec0e5a7ed 100644 --- a/lgc.h +++ b/lgc.h @@ -1,5 +1,5 @@ /* -** $Id: lgc.h,v 2.99 2017/10/11 12:38:45 roberto Exp roberto $ +** $Id: lgc.h,v 2.100 2018/01/28 15:13:26 roberto Exp roberto $ ** Garbage Collector ** See Copyright Notice in lua.h */ @@ -119,14 +119,14 @@ /* Default Values for GC parameters */ #define LUAI_GENMAJORMUL 100 -#define LUAI_GENMINORMUL 12 +#define LUAI_GENMINORMUL 20 /* wait memory to double before starting new cycle */ #define LUAI_GCPAUSE 200 /* 200% */ /* -** gc parameters are stored divided by 4 to allow a maximum value larger -** than 1000 in an 'lu_byte'. +** some gc parameters are stored divided by 4 to allow a maximum value +** larger than 1000 in a 'lu_byte'. */ #define getgcparam(p) ((p) * 4) #define setgcparam(p,v) ((p) = (v) / 4) From daff7c3b4de14278a06d86997baa6ab9db7b662b Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 5 Feb 2018 15:14:54 -0200 Subject: [PATCH 0200/1145] small corrections in generational mode (cannot call finalizers in emergency collections + should set everything before calling finalizers) --- lgc.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/lgc.c b/lgc.c index 7b223bcb24..20397e4824 100644 --- a/lgc.c +++ b/lgc.c @@ -1,5 +1,5 @@ /* -** $Id: lgc.c,v 2.244 2017/12/28 15:42:57 roberto Exp roberto $ +** $Id: lgc.c,v 2.245 2018/01/28 15:13:26 roberto Exp roberto $ ** Garbage Collector ** See Copyright Notice in lua.h */ @@ -860,6 +860,7 @@ static void GCTM (lua_State *L, int propagateerrors) { global_State *g = G(L); const TValue *tm; TValue v; + lua_assert(!g->gcemergency); setgcovalue(L, &v, udata2finalize(g)); tm = luaT_gettmbyobj(L, &v, TM_GC); if (tm != NULL && ttisfunction(tm)) { /* is there a finalizer? */ @@ -905,10 +906,10 @@ static int runafewfinalizers (lua_State *L, int n) { /* ** call all pending finalizers */ -static void callallpendingfinalizers (lua_State *L) { +static void callallpendingfinalizers (lua_State *L, int propagateerrors) { global_State *g = G(L); while (g->tobefnz) - GCTM(L, 0); + GCTM(L, propagateerrors); } @@ -1151,7 +1152,8 @@ static void finishgencycle (lua_State *L, global_State *g) { correctgraylists(g); checkSizes(L, g); g->gcstate = GCSpropagate; /* skip restart */ - callallpendingfinalizers(L); + if (!g->gcemergency) + callallpendingfinalizers(L, 1); } @@ -1211,9 +1213,9 @@ static void entergen (lua_State *L, global_State *g) { sweep2old(L, &g->tobefnz); - finishgencycle(L, g); g->gckind = KGC_GEN; g->GCestimate = gettotalbytes(g); /* base for memory control */ + finishgencycle(L, g); } @@ -1226,8 +1228,8 @@ static void enterinc (global_State *g) { whitelist(g, g->allgc); g->reallyold = g->old = g->survival = NULL; whitelist(g, g->finobj); + whitelist(g, g->tobefnz); g->finobjrold = g->finobjold = g->finobjsur = NULL; - lua_assert(g->tobefnz == NULL); /* no need to sweep */ g->gcstate = GCSpause; g->gckind = KGC_INC; } @@ -1347,7 +1349,7 @@ void luaC_freeallobjects (lua_State *L) { luaC_changemode(L, KGC_INC); separatetobefnz(g, 1); /* separate all objects with finalizers */ lua_assert(g->finobj == NULL); - callallpendingfinalizers(L); + callallpendingfinalizers(L, 0); deletelist(L, g->allgc, obj2gco(g->mainthread)); deletelist(L, g->finobj, NULL); deletelist(L, g->fixedgc, NULL); /* collect fixed objects */ From fc3eaa2559f2b9b929892c4a798809f3aa93effe Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 6 Feb 2018 13:32:36 -0200 Subject: [PATCH 0201/1145] GC default mode for the stand-alone interpreter is generational mode --- lua.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lua.c b/lua.c index 8d5184b366..b0e7e1b92c 100644 --- a/lua.c +++ b/lua.c @@ -1,5 +1,5 @@ /* -** $Id: lua.c,v 1.231 2017/04/19 12:49:17 roberto Exp roberto $ +** $Id: lua.c,v 1.232 2017/05/24 21:11:19 roberto Exp roberto $ ** Lua stand-alone interpreter ** See Copyright Notice in lua.h */ @@ -571,6 +571,7 @@ static int pmain (lua_State *L) { } luaL_openlibs(L); /* open standard libraries */ createargtable(L, argv, argc, script); /* create table 'arg' */ + lua_gc(L, LUA_GCGEN, 0, 0); /* GC in generational mode */ if (!(args & has_E)) { /* no option '-E'? */ if (handle_luainit(L) != LUA_OK) /* run LUA_INIT */ return 0; /* error running LUA_INIT */ From 51280ef2ad87f3fcc657fdc0f52799432d2bc340 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 6 Feb 2018 17:16:56 -0200 Subject: [PATCH 0202/1145] call hooks for Lua functions called by 'luaV_execute' --- ldebug.c | 4 ++-- ldo.c | 30 ++++++++++++++---------------- ldo.h | 3 ++- lvm.c | 7 +++++-- 4 files changed, 23 insertions(+), 21 deletions(-) diff --git a/ldebug.c b/ldebug.c index 3ea6367fd1..93a935a5f8 100644 --- a/ldebug.c +++ b/ldebug.c @@ -1,5 +1,5 @@ /* -** $Id: ldebug.c,v 2.151 2017/12/28 15:42:57 roberto Exp roberto $ +** $Id: ldebug.c,v 2.152 2018/01/10 12:02:35 roberto Exp roberto $ ** Debug Interface ** See Copyright Notice in lua.h */ @@ -143,7 +143,7 @@ LUA_API void lua_sethook (lua_State *L, lua_Hook func, int mask, int count) { L->basehookcount = count; resethookcount(L); L->hookmask = cast_byte(mask); - if (mask & (LUA_MASKLINE | LUA_MASKCOUNT)) + if (mask) settraps(L->ci); /* to trace inside 'luaV_execute' */ } diff --git a/ldo.c b/ldo.c index 6ab57f8e00..d627941c08 100644 --- a/ldo.c +++ b/ldo.c @@ -1,5 +1,5 @@ /* -** $Id: ldo.c,v 2.188 2018/01/28 13:39:52 roberto Exp roberto $ +** $Id: ldo.c,v 2.189 2018/01/29 16:21:35 roberto Exp roberto $ ** Stack and Call structure of Lua ** See Copyright Notice in lua.h */ @@ -294,19 +294,21 @@ void luaD_hook (lua_State *L, int event, int line) { } -static void hookcall (lua_State *L, CallInfo *ci, int istail) { - int hook; - ci->u.l.trap = 1; - if (!(L->hookmask & LUA_MASKCALL)) - return; /* some other hook */ +/* +** Executes a call hook for Lua functions. This function is called +** whenever 'hookmask' is not zero, so it checks whether call hooks are +** active. Also, this function can be called when resuming a function, +** so it checks whether the function is in its first instruction. +*/ +void luaD_hookcall (lua_State *L, CallInfo *ci) { + Proto *p = clLvalue(s2v(ci->func))->p; + int hook = (ci->callstatus & CIST_TAIL) ? LUA_HOOKTAILCALL : LUA_HOOKCALL; + ci->u.l.trap = 1; /* there may be other hooks */ + if (!(L->hookmask & LUA_MASKCALL) || /* some other hook? */ + ci->u.l.savedpc != p->code) /* not 1st instruction? */ + return; /* don't call hook */ L->top = ci->top; /* prepare top */ ci->u.l.savedpc++; /* hooks assume 'pc' is already incremented */ - if (istail) { - ci->callstatus |= CIST_TAIL; - hook = LUA_HOOKTAILCALL; - } - else - hook = LUA_HOOKCALL; luaD_hook(L, hook, -1); ci->u.l.savedpc--; /* correct 'pc' */ } @@ -427,8 +429,6 @@ void luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, int narg1) { L->top = func + narg1; /* set top */ luaT_adjustvarargs(L, nfixparams, narg1 - 1); } - if (L->hookmask) - hookcall(L, ci, 1); } @@ -483,8 +483,6 @@ void luaD_call (lua_State *L, StkId func, int nresults) { ci->callstatus = 0; if (p->is_vararg) luaT_adjustvarargs(L, nfixparams, narg); /* may invoke GC */ - if (L->hookmask) - hookcall(L, ci, 0); luaV_execute(L, ci); /* run the function */ break; } diff --git a/ldo.h b/ldo.h index 50f79242ac..44e3af20c9 100644 --- a/ldo.h +++ b/ldo.h @@ -1,5 +1,5 @@ /* -** $Id: ldo.h,v 2.38 2017/12/11 12:43:40 roberto Exp roberto $ +** $Id: ldo.h,v 2.39 2018/01/10 19:19:27 roberto Exp roberto $ ** Stack and Call structure of Lua ** See Copyright Notice in lua.h */ @@ -48,6 +48,7 @@ typedef void (*Pfunc) (lua_State *L, void *ud); LUAI_FUNC int luaD_protectedparser (lua_State *L, ZIO *z, const char *name, const char *mode); LUAI_FUNC void luaD_hook (lua_State *L, int event, int line); +LUAI_FUNC void luaD_hookcall (lua_State *L, CallInfo *ci); LUAI_FUNC void luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, int n); LUAI_FUNC void luaD_call (lua_State *L, StkId func, int nResults); LUAI_FUNC void luaD_callnoyield (lua_State *L, StkId func, int nResults); diff --git a/lvm.c b/lvm.c index d6380e4b7a..d9814b62b0 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.335 2018/01/27 16:56:33 roberto Exp roberto $ +** $Id: lvm.c,v 2.336 2018/01/29 16:21:35 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -832,8 +832,11 @@ void luaV_execute (lua_State *L, CallInfo *ci) { TValue *k; StkId base; const Instruction *pc; - int trap = ci->u.l.trap; + int trap; tailcall: + trap = L->hookmask; + if (trap) + luaD_hookcall(L, ci); cl = clLvalue(s2v(ci->func)); k = cl->p->k; base = ci->func + 1; From 73d797ce7ea4c547cb97e39633a71a242c7356c8 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 7 Feb 2018 13:04:41 -0200 Subject: [PATCH 0203/1145] detail (order of 'OT' and 'IT' bits corresponds with macro 'opmode') --- lopcodes.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lopcodes.h b/lopcodes.h index 1e3974d9e9..31bd12f86c 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -1,5 +1,5 @@ /* -** $Id: lopcodes.h,v 1.183 2018/01/27 16:56:33 roberto Exp roberto $ +** $Id: lopcodes.h,v 1.184 2018/01/28 15:13:26 roberto Exp roberto $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ @@ -334,8 +334,8 @@ OP_EXTRAARG/* Ax extra (larger) argument for previous opcode */ ** bits 0-2: op mode ** bit 3: instruction set register A ** bit 4: operator is a test (next instruction must be a jump) -** bit 5: instruction sets 'L->top' for next instruction (when C == 0) -** bit 6: instruction uses 'L->top' set by previous instruction (when B == 0) +** bit 5: instruction uses 'L->top' set by previous instruction (when B == 0) +** bit 6: instruction sets 'L->top' for next instruction (when C == 0) */ LUAI_DDEC const lu_byte luaP_opmodes[NUM_OPCODES]; @@ -343,8 +343,8 @@ LUAI_DDEC const lu_byte luaP_opmodes[NUM_OPCODES]; #define getOpMode(m) (cast(enum OpMode, luaP_opmodes[m] & 7)) #define testAMode(m) (luaP_opmodes[m] & (1 << 3)) #define testTMode(m) (luaP_opmodes[m] & (1 << 4)) -#define testOTMode(m) (luaP_opmodes[m] & (1 << 5)) -#define testITMode(m) (luaP_opmodes[m] & (1 << 6)) +#define testITMode(m) (luaP_opmodes[m] & (1 << 5)) +#define testOTMode(m) (luaP_opmodes[m] & (1 << 6)) /* "out top" (set top for next instruction) */ #define isOT(i) (testOTMode(GET_OPCODE(i)) && GETARG_C(i) == 0) @@ -352,7 +352,7 @@ LUAI_DDEC const lu_byte luaP_opmodes[NUM_OPCODES]; /* "in top" (uses top from previous instruction) */ #define isIT(i) (testITMode(GET_OPCODE(i)) && GETARG_B(i) == 0) -#define opmode(ot,it,t,a,m) (((ot)<<5) | ((it)<<6) | ((t)<<4) | ((a)<<3) | (m)) +#define opmode(ot,it,t,a,m) (((ot)<<6) | ((it)<<5) | ((t)<<4) | ((a)<<3) | (m)) LUAI_DDEC const char *const luaP_opnames[NUM_OPCODES+1]; /* opcode names */ From 318a9a5859826d7af0294664e206236fc8814319 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 7 Feb 2018 13:18:04 -0200 Subject: [PATCH 0204/1145] new opcode 'PREPVARARG' (avoids test for vararg function in all function calls) --- ldo.c | 19 +++++-------------- lopcodes.c | 4 +++- lopcodes.h | 2 ++ lparser.c | 22 ++++++++++++++++------ ltm.c | 5 +++-- ltm.h | 4 ++-- lvm.c | 23 +++++++++++++++++++---- 7 files changed, 50 insertions(+), 29 deletions(-) diff --git a/ldo.c b/ldo.c index d627941c08..89c6335093 100644 --- a/ldo.c +++ b/ldo.c @@ -1,5 +1,5 @@ /* -** $Id: ldo.c,v 2.189 2018/01/29 16:21:35 roberto Exp roberto $ +** $Id: ldo.c,v 2.190 2018/02/06 19:16:56 roberto Exp roberto $ ** Stack and Call structure of Lua ** See Copyright Notice in lua.h */ @@ -297,15 +297,11 @@ void luaD_hook (lua_State *L, int event, int line) { /* ** Executes a call hook for Lua functions. This function is called ** whenever 'hookmask' is not zero, so it checks whether call hooks are -** active. Also, this function can be called when resuming a function, -** so it checks whether the function is in its first instruction. +** active. */ void luaD_hookcall (lua_State *L, CallInfo *ci) { - Proto *p = clLvalue(s2v(ci->func))->p; int hook = (ci->callstatus & CIST_TAIL) ? LUA_HOOKTAILCALL : LUA_HOOKCALL; - ci->u.l.trap = 1; /* there may be other hooks */ - if (!(L->hookmask & LUA_MASKCALL) || /* some other hook? */ - ci->u.l.savedpc != p->code) /* not 1st instruction? */ + if (!(L->hookmask & LUA_MASKCALL)) /* some other hook? */ return; /* don't call hook */ L->top = ci->top; /* prepare top */ ci->u.l.savedpc++; /* hooks assume 'pc' is already incremented */ @@ -417,7 +413,7 @@ void luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, int narg1) { int i; for (i = 0; i < narg1; i++) /* move down function and arguments */ setobjs2s(L, ci->func + i, func + i); - checkstackp(L, fsize, func); + luaD_checkstackaux(L, fsize, (void)0, luaC_checkGC(L)); func = ci->func; /* moved-down function */ for (; narg1 <= nfixparams; narg1++) setnilvalue(s2v(func + narg1)); /* complete missing arguments */ @@ -425,10 +421,7 @@ void luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, int narg1) { lua_assert(ci->top <= L->stack_last); ci->u.l.savedpc = p->code; /* starting point */ ci->callstatus |= CIST_TAIL; - if (p->is_vararg) { - L->top = func + narg1; /* set top */ - luaT_adjustvarargs(L, nfixparams, narg1 - 1); - } + L->top = func + narg1; /* set top */ } @@ -481,8 +474,6 @@ void luaD_call (lua_State *L, StkId func, int nresults) { lua_assert(ci->top <= L->stack_last); ci->u.l.savedpc = p->code; /* starting point */ ci->callstatus = 0; - if (p->is_vararg) - luaT_adjustvarargs(L, nfixparams, narg); /* may invoke GC */ luaV_execute(L, ci); /* run the function */ break; } diff --git a/lopcodes.c b/lopcodes.c index a5867d2358..cd85f1a9ca 100644 --- a/lopcodes.c +++ b/lopcodes.c @@ -1,5 +1,5 @@ /* -** $Id: lopcodes.c,v 1.74 2017/12/18 17:49:31 roberto Exp $ +** $Id: lopcodes.c,v 1.75 2017/12/22 14:16:46 roberto Exp roberto $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ @@ -91,6 +91,7 @@ LUAI_DDEF const char *const luaP_opnames[NUM_OPCODES+1] = { "SETLIST", "CLOSURE", "VARARG", + "PREPVARARG", "EXTRAARG", NULL }; @@ -171,6 +172,7 @@ LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = { ,opmode(0, 1, 0, 0, iABC) /* OP_SETLIST */ ,opmode(0, 0, 0, 1, iABx) /* OP_CLOSURE */ ,opmode(1, 0, 0, 1, iABC) /* OP_VARARG */ + ,opmode(0, 0, 0, 1, iABC) /* OP_PREPVARARG */ ,opmode(0, 0, 0, 0, iAx) /* OP_EXTRAARG */ }; diff --git a/lopcodes.h b/lopcodes.h index 31bd12f86c..47c72c6955 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -288,6 +288,8 @@ OP_CLOSURE,/* A Bx R(A) := closure(KPROTO[Bx]) */ OP_VARARG,/* A B C R(A), R(A+1), ..., R(A+C-2) = vararg(B) */ +OP_PREPVARARG,/*A (adjust vararg parameters) */ + OP_EXTRAARG/* Ax extra (larger) argument for previous opcode */ } OpCode; diff --git a/lparser.c b/lparser.c index e99b33ffb2..aab03dc8ac 100644 --- a/lparser.c +++ b/lparser.c @@ -1,5 +1,5 @@ /* -** $Id: lparser.c,v 2.174 2017/12/18 17:49:31 roberto Exp roberto $ +** $Id: lparser.c,v 2.175 2017/12/22 14:16:46 roberto Exp roberto $ ** Lua Parser ** See Copyright Notice in lua.h */ @@ -759,12 +759,18 @@ static void constructor (LexState *ls, expdesc *t) { /* }====================================================================== */ +static void setvararg (FuncState *fs, int nparams) { + fs->f->is_vararg = 1; + luaK_codeABC(fs, OP_PREPVARARG, nparams, 0, 0); +} + static void parlist (LexState *ls) { /* parlist -> [ param { ',' param } ] */ FuncState *fs = ls->fs; Proto *f = fs->f; int nparams = 0; + int isvararg = 0; if (ls->t.token != ')') { /* is 'parlist' not empty? */ do { switch (ls->t.token) { @@ -779,17 +785,21 @@ static void parlist (LexState *ls) { new_localvar(ls, str_checkname(ls)); else new_localvarliteral(ls, "_ARG"); - f->is_vararg = 1; /* declared vararg */ nparams++; + isvararg = 1; break; } default: luaX_syntaxerror(ls, " or '...' expected"); } - } while (!f->is_vararg && testnext(ls, ',')); + } while (!isvararg && testnext(ls, ',')); } adjustlocalvars(ls, nparams); - f->numparams = cast_byte(fs->nactvar) - f->is_vararg; - luaK_reserveregs(fs, fs->nactvar); /* reserve register for parameters */ + f->numparams = cast_byte(fs->nactvar); + if (isvararg) { + f->numparams--; /* exclude vararg parameter */ + setvararg(fs, f->numparams); /* declared vararg */ + } + luaK_reserveregs(fs, fs->nactvar); /* reserve registers for parameters */ } @@ -1692,7 +1702,7 @@ static void mainfunc (LexState *ls, FuncState *fs) { BlockCnt bl; expdesc v; open_func(ls, fs, &bl); - fs->f->is_vararg = 1; /* main function is always declared vararg */ + setvararg(fs, 0); /* main function is always declared vararg */ fs->f->numparams = 0; new_localvarliteral(ls, "_ARG"); adjustlocalvars(ls, 1); diff --git a/ltm.c b/ltm.c index 64622f204e..f36f462518 100644 --- a/ltm.c +++ b/ltm.c @@ -1,5 +1,5 @@ /* -** $Id: ltm.c,v 2.57 2018/01/28 12:08:04 roberto Exp roberto $ +** $Id: ltm.c,v 2.58 2018/01/28 13:39:52 roberto Exp roberto $ ** Tag methods ** See Copyright Notice in lua.h */ @@ -216,10 +216,11 @@ int luaT_callorderiTM (lua_State *L, const TValue *p1, int v2, } -void luaT_adjustvarargs (lua_State *L, int nfixparams, int actual) { +void luaT_adjustvarargs (lua_State *L, int nfixparams, StkId base) { int i; Table *vtab; TValue nname; + int actual = cast_int(L->top - base); /* number of arguments */ int nextra = actual - nfixparams; /* number of extra arguments */ vtab = luaH_new(L); /* create vararg table */ sethvalue2s(L, L->top, vtab); /* anchor it for resizing */ diff --git a/ltm.h b/ltm.h index fbba067ad4..a0f4f92eb1 100644 --- a/ltm.h +++ b/ltm.h @@ -1,5 +1,5 @@ /* -** $Id: ltm.h,v 2.28 2017/12/13 18:32:09 roberto Exp roberto $ +** $Id: ltm.h,v 2.29 2018/01/28 13:39:52 roberto Exp roberto $ ** Tag methods ** See Copyright Notice in lua.h */ @@ -77,7 +77,7 @@ LUAI_FUNC int luaT_callorderTM (lua_State *L, const TValue *p1, LUAI_FUNC int luaT_callorderiTM (lua_State *L, const TValue *p1, int v2, int inv, TMS event); -LUAI_FUNC void luaT_adjustvarargs (lua_State *L, int nfixparams, int actual); +LUAI_FUNC void luaT_adjustvarargs (lua_State *L, int nfixparams, StkId base); LUAI_FUNC void luaT_getvarargs (lua_State *L, TValue *t, StkId where, int wanted); diff --git a/lvm.c b/lvm.c index d9814b62b0..7f2e2492de 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.336 2018/01/29 16:21:35 roberto Exp roberto $ +** $Id: lvm.c,v 2.337 2018/02/06 19:16:56 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -835,12 +835,17 @@ void luaV_execute (lua_State *L, CallInfo *ci) { int trap; tailcall: trap = L->hookmask; - if (trap) - luaD_hookcall(L, ci); cl = clLvalue(s2v(ci->func)); k = cl->p->k; - base = ci->func + 1; pc = ci->u.l.savedpc; + if (trap) { + if (cl->p->is_vararg) + trap = 0; /* hooks will start with PREPVARARG instruction */ + else if (pc == cl->p->code) /* first instruction (not resuming)? */ + luaD_hookcall(L, ci); + ci->u.l.trap = 1; /* there may be other hooks */ + } + base = ci->func + 1; /* main loop of interpreter */ for (;;) { int cond; /* flag for conditional jumps */ @@ -1701,6 +1706,16 @@ void luaV_execute (lua_State *L, CallInfo *ci) { Protect(luaT_getvarargs(L, vtab, ra, n)); vmbreak; } + vmcase(OP_PREPVARARG) { + luaT_adjustvarargs(L, GETARG_A(i), base); + updatetrap(ci); + if (trap) { + luaD_hookcall(L, ci); + L->oldpc = pc + 1; /* next opcode will be seen as a new line */ + } + updatebase(ci); + vmbreak; + } vmcase(OP_EXTRAARG) { lua_assert(0); vmbreak; From 4e0de3a43cc30a83334c272cb7575bf8412bfeae Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 7 Feb 2018 13:55:18 -0200 Subject: [PATCH 0205/1145] details --- ldo.c | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/ldo.c b/ldo.c index 89c6335093..402b3703b8 100644 --- a/ldo.c +++ b/ldo.c @@ -1,5 +1,5 @@ /* -** $Id: ldo.c,v 2.190 2018/02/06 19:16:56 roberto Exp roberto $ +** $Id: ldo.c,v 2.191 2018/02/07 15:18:04 roberto Exp roberto $ ** Stack and Call structure of Lua ** See Copyright Notice in lua.h */ @@ -398,7 +398,11 @@ void luaD_poscall (lua_State *L, CallInfo *ci, StkId firstResult, int nres) { -#define next_ci(L) (L->ci = (L->ci->next ? L->ci->next : luaE_extendCI(L))) +#define next_ci(L) (L->ci->next ? L->ci->next : luaE_extendCI(L)) + + +#define checkstackGC(L,fsize) \ + luaD_checkstackaux(L, (fsize), (void)0, luaC_checkGC(L)) /* @@ -413,7 +417,7 @@ void luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, int narg1) { int i; for (i = 0; i < narg1; i++) /* move down function and arguments */ setobjs2s(L, ci->func + i, func + i); - luaD_checkstackaux(L, fsize, (void)0, luaC_checkGC(L)); + checkstackGC(L, fsize); func = ci->func; /* moved-down function */ for (; narg1 <= nfixparams; narg1++) setnilvalue(s2v(func + narg1)); /* complete missing arguments */ @@ -434,7 +438,8 @@ void luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, int narg1) { void luaD_call (lua_State *L, StkId func, int nresults) { lua_CFunction f; TValue *funcv = s2v(func); - CallInfo *ci; + CallInfo *ci = next_ci(L); + ci->nresults = nresults; switch (ttype(funcv)) { case LUA_TCCL: /* C closure */ f = clCvalue(funcv)->f; @@ -444,12 +449,11 @@ void luaD_call (lua_State *L, StkId func, int nresults) { Cfunc: { int n; /* number of returns */ checkstackp(L, LUA_MINSTACK, func); /* ensure minimum stack size */ - ci = next_ci(L); /* now 'enter' new function */ - ci->nresults = nresults; - ci->func = func; + ci->callstatus = CIST_C; ci->top = L->top + LUA_MINSTACK; + ci->func = func; lua_assert(ci->top <= L->stack_last); - ci->callstatus = CIST_C; + L->ci = ci; /* now 'enter' new function */ if (L->hookmask & LUA_MASKCALL) luaD_hook(L, LUA_HOOKCALL, -1); lua_unlock(L); @@ -464,16 +468,15 @@ void luaD_call (lua_State *L, StkId func, int nresults) { int narg = cast_int(L->top - func) - 1; /* number of real arguments */ int nfixparams = p->numparams; int fsize = p->maxstacksize; /* frame size */ + ci->u.l.savedpc = p->code; /* starting point */ checkstackp(L, fsize, func); for (; narg < nfixparams; narg++) setnilvalue(s2v(L->top++)); /* complete missing arguments */ - ci = next_ci(L); /* now 'enter' new function */ - ci->nresults = nresults; - ci->func = func; + ci->callstatus = 0; ci->top = func + 1 + fsize; + ci->func = func; + L->ci = ci; /* now 'enter' new function */ lua_assert(ci->top <= L->stack_last); - ci->u.l.savedpc = p->code; /* starting point */ - ci->callstatus = 0; luaV_execute(L, ci); /* run the function */ break; } From b1379936cf35787d3ef3aab82d1607a3e1562eef Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 9 Feb 2018 13:16:06 -0200 Subject: [PATCH 0206/1145] vararg back to '...' (but with another implementation) new implementation should have zero overhead for non-vararg functions --- lcode.c | 26 +++++++++++++------------- ldebug.c | 26 +++++++++++++++++++++----- ldo.c | 37 +++++++++++++++++------------------- ldo.h | 10 +++++++++- lopcodes.c | 4 +++- lopcodes.h | 10 ++++++---- lparser.c | 22 ++++++---------------- lstate.h | 3 ++- ltm.c | 55 +++++++++++++++++++++++------------------------------- ltm.h | 10 ++++++---- lvm.c | 40 ++++++++++++++++++++++++++------------- 11 files changed, 133 insertions(+), 110 deletions(-) diff --git a/lcode.c b/lcode.c index 7f240580bf..25fb0770c2 100644 --- a/lcode.c +++ b/lcode.c @@ -1,5 +1,5 @@ /* -** $Id: lcode.c,v 2.151 2018/01/27 16:56:33 roberto Exp roberto $ +** $Id: lcode.c,v 2.152 2018/01/28 15:13:26 roberto Exp roberto $ ** Code generator for Lua ** See Copyright Notice in lua.h */ @@ -31,7 +31,7 @@ /* Maximum number of registers in a Lua function (must fit in 8 bits) */ -#define MAXREGS 255 +#define MAXREGS 254 #define hasjumps(e) ((e)->t != (e)->f) @@ -157,17 +157,17 @@ int luaK_jump (FuncState *fs) { ** Code a 'return' instruction */ void luaK_ret (FuncState *fs, int first, int nret) { - switch (nret) { - case 0: - luaK_codeABC(fs, OP_RETURN0, 0, 0, 0); - break; - case 1: - luaK_codeABC(fs, OP_RETURN1, first, 0, 0); - break; - default: - luaK_codeABC(fs, OP_RETURN, first, nret + 1, 0); - break; + OpCode op; + if (fs->f->is_vararg) + op = OP_RETVARARG; + else { + switch (nret) { + case 0: op = OP_RETURN0; break; + case 1: op = OP_RETURN1; break; + default: op = OP_RETURN; break; + } } + luaK_codeABC(fs, op, first, nret + 1, fs->f->numparams); } @@ -1647,7 +1647,7 @@ void luaK_finish (FuncState *fs) { lua_assert(i == 0 || isOT(*(pc - 1)) == isIT(*pc)); switch (GET_OPCODE(*pc)) { case OP_RETURN: case OP_RETURN0: case OP_RETURN1: - case OP_TAILCALL: { + case OP_RETVARARG: case OP_TAILCALL: { if (p->sizep > 0) SETARG_k(*pc, 1); /* signal that they must close upvalues */ break; diff --git a/ldebug.c b/ldebug.c index 93a935a5f8..3ddac54709 100644 --- a/ldebug.c +++ b/ldebug.c @@ -1,5 +1,5 @@ /* -** $Id: ldebug.c,v 2.152 2018/01/10 12:02:35 roberto Exp roberto $ +** $Id: ldebug.c,v 2.153 2018/02/06 19:16:56 roberto Exp roberto $ ** Debug Interface ** See Copyright Notice in lua.h */ @@ -187,12 +187,28 @@ static const char *upvalname (Proto *p, int uv) { } +static const char *findvararg (CallInfo *ci, int n, StkId *pos) { + if (clLvalue(s2v(ci->func))->p->is_vararg) { + int nextra = ci->u.l.nextraargs; + if (n <= nextra) { + *pos = ci->func - nextra + (n - 1); + return "(*vararg)"; /* generic name for any vararg */ + } + } + return NULL; /* no such vararg */ +} + + static const char *findlocal (lua_State *L, CallInfo *ci, int n, StkId *pos) { StkId base = ci->func + 1; - const char *name = (isLua(ci)) - ? luaF_getlocalname(ci_func(ci)->p, n, currentpc(ci)) - : NULL; + const char *name = NULL; + if (isLua(ci)) { + if (n < 0) /* access to vararg values? */ + return findvararg(ci, -n, pos); + else + name = luaF_getlocalname(ci_func(ci)->p, n, currentpc(ci)); + } if (name == NULL) { /* no 'standard' name? */ StkId limit = (ci == L->ci) ? L->top : ci->next->func; if (limit - base >= n && n > 0) /* is 'n' inside 'ci' stack? */ @@ -324,7 +340,7 @@ static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar, } else { ar->isvararg = f->l.p->is_vararg; - ar->nparams = f->l.p->numparams + f->l.p->is_vararg; + ar->nparams = f->l.p->numparams; } break; } diff --git a/ldo.c b/ldo.c index 402b3703b8..64512487e8 100644 --- a/ldo.c +++ b/ldo.c @@ -1,5 +1,5 @@ /* -** $Id: ldo.c,v 2.191 2018/02/07 15:18:04 roberto Exp roberto $ +** $Id: ldo.c,v 2.192 2018/02/07 15:55:18 roberto Exp roberto $ ** Stack and Call structure of Lua ** See Copyright Notice in lua.h */ @@ -310,7 +310,7 @@ void luaD_hookcall (lua_State *L, CallInfo *ci) { } -static void rethook (lua_State *L, CallInfo *ci) { +void luaD_rethook (lua_State *L, CallInfo *ci) { if (isLuacode(ci)) L->top = ci->top; /* prepare top */ if (L->hookmask & LUA_MASKRET) /* is return hook on? */ @@ -343,8 +343,8 @@ void luaD_tryfuncTM (lua_State *L, StkId func) { ** expressions, multiple results for tail calls/single parameters) ** separated. */ -static void moveresults (lua_State *L, StkId firstResult, StkId res, - int nres, int wanted) { +void luaD_moveresults (lua_State *L, StkId firstResult, StkId res, + int nres, int wanted) { switch (wanted) { /* handle typical cases separately */ case 0: break; /* nothing to move */ case 1: { /* one result needed */ @@ -382,27 +382,22 @@ static void moveresults (lua_State *L, StkId firstResult, StkId res, /* ** Finishes a function call: calls hook if necessary, removes CallInfo, -** moves current number of results to proper place; returns 0 iff call -** wanted multiple (variable number of) results. +** moves current number of results to proper place. */ void luaD_poscall (lua_State *L, CallInfo *ci, StkId firstResult, int nres) { if (L->hookmask) { ptrdiff_t fr = savestack(L, firstResult); /* hook may change stack */ - rethook(L, ci); + luaD_rethook(L, ci); firstResult = restorestack(L, fr); } L->ci = ci->previous; /* back to caller */ /* move results to proper place */ - moveresults(L, firstResult, ci->func, nres, ci->nresults); + luaD_moveresults(L, firstResult, ci->func, nres, ci->nresults); } -#define next_ci(L) (L->ci->next ? L->ci->next : luaE_extendCI(L)) - - -#define checkstackGC(L,fsize) \ - luaD_checkstackaux(L, (fsize), (void)0, luaC_checkGC(L)) +#define next_ci(L) (L->ci = (L->ci->next ? L->ci->next : luaE_extendCI(L))) /* @@ -438,8 +433,6 @@ void luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, int narg1) { void luaD_call (lua_State *L, StkId func, int nresults) { lua_CFunction f; TValue *funcv = s2v(func); - CallInfo *ci = next_ci(L); - ci->nresults = nresults; switch (ttype(funcv)) { case LUA_TCCL: /* C closure */ f = clCvalue(funcv)->f; @@ -448,12 +441,14 @@ void luaD_call (lua_State *L, StkId func, int nresults) { f = fvalue(funcv); Cfunc: { int n; /* number of returns */ + CallInfo *ci; checkstackp(L, LUA_MINSTACK, func); /* ensure minimum stack size */ + ci = next_ci(L); + ci->nresults = nresults; ci->callstatus = CIST_C; ci->top = L->top + LUA_MINSTACK; ci->func = func; lua_assert(ci->top <= L->stack_last); - L->ci = ci; /* now 'enter' new function */ if (L->hookmask & LUA_MASKCALL) luaD_hook(L, LUA_HOOKCALL, -1); lua_unlock(L); @@ -464,18 +459,20 @@ void luaD_call (lua_State *L, StkId func, int nresults) { break; } case LUA_TLCL: { /* Lua function */ + CallInfo *ci; Proto *p = clLvalue(funcv)->p; int narg = cast_int(L->top - func) - 1; /* number of real arguments */ int nfixparams = p->numparams; int fsize = p->maxstacksize; /* frame size */ - ci->u.l.savedpc = p->code; /* starting point */ checkstackp(L, fsize, func); - for (; narg < nfixparams; narg++) - setnilvalue(s2v(L->top++)); /* complete missing arguments */ + ci = next_ci(L); + ci->nresults = nresults; + ci->u.l.savedpc = p->code; /* starting point */ ci->callstatus = 0; ci->top = func + 1 + fsize; ci->func = func; - L->ci = ci; /* now 'enter' new function */ + for (; narg < nfixparams; narg++) + setnilvalue(s2v(L->top++)); /* complete missing arguments */ lua_assert(ci->top <= L->stack_last); luaV_execute(L, ci); /* run the function */ break; diff --git a/ldo.h b/ldo.h index 44e3af20c9..864cf3e6ea 100644 --- a/ldo.h +++ b/ldo.h @@ -1,5 +1,5 @@ /* -** $Id: ldo.h,v 2.39 2018/01/10 19:19:27 roberto Exp roberto $ +** $Id: ldo.h,v 2.40 2018/02/06 19:16:56 roberto Exp roberto $ ** Stack and Call structure of Lua ** See Copyright Notice in lua.h */ @@ -42,6 +42,11 @@ p = restorestack(L, t__)) /* 'pos' part: restore 'p' */ +/* macro to check stack size and GC */ +#define checkstackGC(L,fsize) \ + luaD_checkstackaux(L, (fsize), (void)0, luaC_checkGC(L)) + + /* type of protected functions, to be ran by 'runprotected' */ typedef void (*Pfunc) (lua_State *L, void *ud); @@ -57,7 +62,10 @@ LUAI_FUNC int luaD_pcall (lua_State *L, Pfunc func, void *u, ptrdiff_t oldtop, ptrdiff_t ef); LUAI_FUNC void luaD_poscall (lua_State *L, CallInfo *ci, StkId firstResult, int nres); +LUAI_FUNC void luaD_rethook (lua_State *L, CallInfo *ci); LUAI_FUNC int luaD_reallocstack (lua_State *L, int newsize, int raiseerror); +LUAI_FUNC void luaD_moveresults (lua_State *L, StkId firstResult, StkId res, + int nres, int wanted); LUAI_FUNC int luaD_growstack (lua_State *L, int n, int raiseerror); LUAI_FUNC void luaD_shrinkstack (lua_State *L); LUAI_FUNC void luaD_inctop (lua_State *L); diff --git a/lopcodes.c b/lopcodes.c index cd85f1a9ca..d04e707f53 100644 --- a/lopcodes.c +++ b/lopcodes.c @@ -1,5 +1,5 @@ /* -** $Id: lopcodes.c,v 1.75 2017/12/22 14:16:46 roberto Exp roberto $ +** $Id: lopcodes.c,v 1.76 2018/02/07 15:18:04 roberto Exp roberto $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ @@ -80,6 +80,7 @@ LUAI_DDEF const char *const luaP_opnames[NUM_OPCODES+1] = { "CALL", "TAILCALL", "RETURN", + "RETVARARG", "RETURN0", "RETURN1", "FORLOOP1", @@ -161,6 +162,7 @@ LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = { ,opmode(1, 1, 0, 1, iABC) /* OP_CALL */ ,opmode(1, 1, 0, 1, iABC) /* OP_TAILCALL */ ,opmode(0, 1, 0, 0, iABC) /* OP_RETURN */ + ,opmode(0, 1, 0, 0, iABC) /* OP_RETVARARG */ ,opmode(0, 0, 0, 0, iABC) /* OP_RETURN0 */ ,opmode(0, 0, 0, 0, iABC) /* OP_RETURN1 */ ,opmode(0, 0, 0, 1, iABx) /* OP_FORLOOP1 */ diff --git a/lopcodes.h b/lopcodes.h index 47c72c6955..3c7a95739f 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -1,5 +1,5 @@ /* -** $Id: lopcodes.h,v 1.184 2018/01/28 15:13:26 roberto Exp roberto $ +** $Id: lopcodes.h,v 1.186 2018/02/07 15:18:04 roberto Exp roberto $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ @@ -268,6 +268,7 @@ OP_CALL,/* A B C R(A), ... ,R(A+C-2) := R(A)(R(A+1), ... ,R(A+B-1)) */ OP_TAILCALL,/* A B C return R(A)(R(A+1), ... ,R(A+B-1)) */ OP_RETURN,/* A B return R(A), ... ,R(A+B-2) (see note) */ +OP_RETVARARG,/* A B return R(A), ... ,R(A+B-2) (see note) */ OP_RETURN0,/* return */ OP_RETURN1,/* A return R(A) */ @@ -286,7 +287,7 @@ OP_SETLIST,/* A B C R(A)[(C-1)*FPF+i] := R(A+i), 1 <= i <= B */ OP_CLOSURE,/* A Bx R(A) := closure(KPROTO[Bx]) */ -OP_VARARG,/* A B C R(A), R(A+1), ..., R(A+C-2) = vararg(B) */ +OP_VARARG,/* A B C R(A), R(A+1), ..., R(A+C-2) = vararg */ OP_PREPVARARG,/*A (adjust vararg parameters) */ @@ -305,9 +306,10 @@ OP_EXTRAARG/* Ax extra (larger) argument for previous opcode */ OP_SETLIST) may use 'top'. (*) In OP_VARARG, if (C == 0) then use actual number of varargs and - set top (like in OP_CALL with C == 0). B is the vararg parameter. + set top (like in OP_CALL with C == 0). - (*) In OP_RETURN, if (B == 0) then return up to 'top'. + (*) In OP_RETURN/OP_RETVARARG, if (B == 0) then return up to 'top'. + (OP_RETVARARG is the return instruction for vararg functions.) (*) In OP_SETLIST, if (B == 0) then real B = 'top'; if (C == 0) then next 'instruction' is EXTRAARG(real C). diff --git a/lparser.c b/lparser.c index aab03dc8ac..802c64bd20 100644 --- a/lparser.c +++ b/lparser.c @@ -1,5 +1,5 @@ /* -** $Id: lparser.c,v 2.175 2017/12/22 14:16:46 roberto Exp roberto $ +** $Id: lparser.c,v 2.176 2018/02/07 15:18:04 roberto Exp roberto $ ** Lua Parser ** See Copyright Notice in lua.h */ @@ -568,6 +568,7 @@ static void close_func (LexState *ls) { Proto *f = fs->f; luaK_ret(fs, 0, 0); /* final return */ leaveblock(fs); + lua_assert(fs->bl == NULL); luaK_finish(fs); luaM_shrinkvector(L, f->code, f->sizecode, fs->pc, Instruction); luaM_shrinkvector(L, f->lineinfo, f->sizelineinfo, fs->pc, ls_byte); @@ -577,7 +578,8 @@ static void close_func (LexState *ls) { luaM_shrinkvector(L, f->p, f->sizep, fs->np, Proto *); luaM_shrinkvector(L, f->locvars, f->sizelocvars, fs->nlocvars, LocVar); luaM_shrinkvector(L, f->upvalues, f->sizeupvalues, fs->nups, Upvaldesc); - lua_assert(fs->bl == NULL); + if (f->is_vararg) + f->maxstacksize++; /* ensure space to copy the function */ ls->fs = fs->prev; luaC_checkGC(L); } @@ -781,11 +783,6 @@ static void parlist (LexState *ls) { } case TK_DOTS: { /* param -> '...' */ luaX_next(ls); - if (testnext(ls, '=')) - new_localvar(ls, str_checkname(ls)); - else - new_localvarliteral(ls, "_ARG"); - nparams++; isvararg = 1; break; } @@ -795,10 +792,8 @@ static void parlist (LexState *ls) { } adjustlocalvars(ls, nparams); f->numparams = cast_byte(fs->nactvar); - if (isvararg) { - f->numparams--; /* exclude vararg parameter */ + if (isvararg) setvararg(fs, f->numparams); /* declared vararg */ - } luaK_reserveregs(fs, fs->nactvar); /* reserve registers for parameters */ } @@ -984,10 +979,9 @@ static void simpleexp (LexState *ls, expdesc *v) { } case TK_DOTS: { /* vararg */ FuncState *fs = ls->fs; - int lastparam = fs->f->numparams; check_condition(ls, fs->f->is_vararg, "cannot use '...' outside a vararg function"); - init_exp(v, VVARARG, luaK_codeABC(fs, OP_VARARG, 0, lastparam, 1)); + init_exp(v, VVARARG, luaK_codeABC(fs, OP_VARARG, 0, 0, 1)); break; } case '{': { /* constructor */ @@ -1703,10 +1697,6 @@ static void mainfunc (LexState *ls, FuncState *fs) { expdesc v; open_func(ls, fs, &bl); setvararg(fs, 0); /* main function is always declared vararg */ - fs->f->numparams = 0; - new_localvarliteral(ls, "_ARG"); - adjustlocalvars(ls, 1); - luaK_reserveregs(fs, 1); /* reserve register for vararg */ init_exp(&v, VLOCAL, 0); /* create and... */ newupvalue(fs, ls->envn, &v); /* ...set environment upvalue */ luaX_next(ls); /* read first token */ diff --git a/lstate.h b/lstate.h index bc4df4e561..2cce8e4f93 100644 --- a/lstate.h +++ b/lstate.h @@ -1,5 +1,5 @@ /* -** $Id: lstate.h,v 2.152 2017/11/23 16:35:54 roberto Exp roberto $ +** $Id: lstate.h,v 2.153 2017/12/19 16:40:17 roberto Exp roberto $ ** Global State ** See Copyright Notice in lua.h */ @@ -92,6 +92,7 @@ typedef struct CallInfo { struct { /* only for Lua functions */ const Instruction *savedpc; l_signalT trap; + int nextraargs; /* # of extra arguments in vararg functions */ } l; struct { /* only for C functions */ lua_KFunction k; /* continuation in case of yields */ diff --git a/ltm.c b/ltm.c index f36f462518..d88b636be6 100644 --- a/ltm.c +++ b/ltm.c @@ -1,5 +1,5 @@ /* -** $Id: ltm.c,v 2.58 2018/01/28 13:39:52 roberto Exp roberto $ +** $Id: ltm.c,v 2.59 2018/02/07 15:18:04 roberto Exp roberto $ ** Tag methods ** See Copyright Notice in lua.h */ @@ -216,41 +216,32 @@ int luaT_callorderiTM (lua_State *L, const TValue *p1, int v2, } -void luaT_adjustvarargs (lua_State *L, int nfixparams, StkId base) { +void luaT_adjustvarargs (lua_State *L, int nfixparams, CallInfo *ci) { int i; - Table *vtab; - TValue nname; - int actual = cast_int(L->top - base); /* number of arguments */ + int actual = cast_int(L->top - ci->func) - 1; /* number of arguments */ int nextra = actual - nfixparams; /* number of extra arguments */ - vtab = luaH_new(L); /* create vararg table */ - sethvalue2s(L, L->top, vtab); /* anchor it for resizing */ - L->top++; /* space ensured by caller */ - luaH_resize(L, vtab, nextra, 1); - for (i = 0; i < nextra; i++) /* put extra arguments into vararg table */ - setobj2n(L, &vtab->array[i], s2v(L->top - nextra + i - 1)); - setsvalue(L, &nname, G(L)->nfield); /* get field 'n' */ - setivalue(luaH_set(L, vtab, &nname), nextra); /* store counter there */ - L->top -= nextra; /* remove extra elements from the stack */ - sethvalue2s(L, L->top - 1, vtab); /* move table to new top */ - luaC_checkGC(L); + ci->u.l.nextraargs = nextra; + checkstackGC(L, nfixparams + 1); + /* copy function and fixed parameters to the top of the stack */ + for (i = 0; i <= nfixparams; i++) { + setobjs2s(L, L->top++, ci->func + i); + setnilvalue(s2v(ci->func + i)); /* erase original copy (for GC) */ + } + ci->func += actual + 1; + ci->top += actual + 1; } -void luaT_getvarargs (lua_State *L, TValue *t, StkId where, int wanted) { - if (!ttistable(t)) - luaG_runerror(L, "'vararg' parameter is not a table"); - else { - int i; - Table *h = hvalue(t); - if (wanted < 0) { /* get all? */ - const TValue *ns = luaH_getstr(h, G(L)->nfield); - int n = (ttisinteger(ns)) ? cast_int(ivalue(ns)) : 0; - wanted = n; - checkstackp(L, n, where); - L->top = where + n; - } - for (i = 0; i < wanted; i++) /* get what is available */ - setobj2s(L, where + i, luaH_getint(h, i + 1)); - return; +void luaT_getvarargs (lua_State *L, CallInfo *ci, StkId where, int wanted) { + int i; + int nextra = ci->u.l.nextraargs; + if (wanted < 0) { + wanted = nextra; /* get all extra arguments available */ + checkstackp(L, nextra, where); /* ensure stack space */ + L->top = where + nextra; /* next instruction will need top */ } + for (i = 0; i < wanted && i < nextra; i++) + setobjs2s(L, where + i, ci->func - nextra + i); + for (; i < wanted; i++) /* complete required results with nil */ + setnilvalue(s2v(where + i)); } diff --git a/ltm.h b/ltm.h index a0f4f92eb1..54cece9e1a 100644 --- a/ltm.h +++ b/ltm.h @@ -1,5 +1,5 @@ /* -** $Id: ltm.h,v 2.29 2018/01/28 13:39:52 roberto Exp roberto $ +** $Id: ltm.h,v 2.30 2018/02/07 15:18:04 roberto Exp roberto $ ** Tag methods ** See Copyright Notice in lua.h */ @@ -9,6 +9,7 @@ #include "lobject.h" +#include "lstate.h" /* @@ -77,9 +78,10 @@ LUAI_FUNC int luaT_callorderTM (lua_State *L, const TValue *p1, LUAI_FUNC int luaT_callorderiTM (lua_State *L, const TValue *p1, int v2, int inv, TMS event); -LUAI_FUNC void luaT_adjustvarargs (lua_State *L, int nfixparams, StkId base); -LUAI_FUNC void luaT_getvarargs (lua_State *L, TValue *t, StkId where, - int wanted); +LUAI_FUNC void luaT_adjustvarargs (lua_State *L, int nfixparams, + struct CallInfo *ci); +LUAI_FUNC void luaT_getvarargs (lua_State *L, struct CallInfo *ci, + StkId where, int wanted); #endif diff --git a/lvm.c b/lvm.c index 7f2e2492de..2d01e4c18c 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.337 2018/02/06 19:16:56 roberto Exp roberto $ +** $Id: lvm.c,v 2.338 2018/02/07 15:18:04 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -1506,14 +1506,10 @@ void luaV_execute (lua_State *L, CallInfo *ci) { luaF_close(L, base); /* close upvalues from current call */ if (!ttisLclosure(vra)) { /* C function? */ ProtectNT(luaD_call(L, ra, LUA_MULTRET)); /* call it */ - if (trap) { - updatebase(ci); - ra = RA(i); - } - luaD_poscall(L, ci, ra, cast_int(L->top - ra)); - return; } else { /* Lua tail call */ + if (cl->p->is_vararg) + ci->func -= cl->p->numparams + ci->u.l.nextraargs + 1; luaD_pretailcall(L, ci, ra, b); /* prepare call frame */ goto tailcall; } @@ -1521,11 +1517,30 @@ void luaV_execute (lua_State *L, CallInfo *ci) { } vmcase(OP_RETURN) { int b = GETARG_B(i); + int n = (b != 0 ? b - 1 : cast_int(L->top - ra)); + if (TESTARG_k(i)) + luaF_close(L, base); + halfProtect(luaD_poscall(L, ci, ra, n)); + return; + } + vmcase(OP_RETVARARG) { + int b = GETARG_B(i); + int nparams = GETARG_C(i); + int nres = (b != 0 ? b - 1 : cast_int(L->top - ra)); + int delta = ci->u.l.nextraargs + nparams + 2; if (TESTARG_k(i)) luaF_close(L, base); - halfProtect( - luaD_poscall(L, ci, ra, (b != 0 ? b - 1 : cast_int(L->top - ra))) - ); + savepc(L); + /* code similar to 'luaD_poscall', but with a delta */ + if (L->hookmask) { + luaD_rethook(L, ci); + if (ci->u.l.trap) { + updatebase(ci); + ra = RA(i); + } + } + L->ci = ci->previous; /* back to caller */ + luaD_moveresults(L, ra, base - delta, nres, ci->nresults); return; } vmcase(OP_RETURN0) { @@ -1702,12 +1717,11 @@ void luaV_execute (lua_State *L, CallInfo *ci) { } vmcase(OP_VARARG) { int n = GETARG_C(i) - 1; /* required results */ - TValue *vtab = vRB(i); /* vararg table */ - Protect(luaT_getvarargs(L, vtab, ra, n)); + ProtectNT(luaT_getvarargs(L, ci, ra, n)); vmbreak; } vmcase(OP_PREPVARARG) { - luaT_adjustvarargs(L, GETARG_A(i), base); + luaT_adjustvarargs(L, GETARG_A(i), ci); updatetrap(ci); if (trap) { luaD_hookcall(L, ci); From 0682fe816929da2e5abe8e191ad9c8509e6bfc12 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 15 Feb 2018 13:34:29 -0200 Subject: [PATCH 0207/1145] some simplifications/optimizations in returns from Lua functions --- lcode.c | 32 ++++++++++++++++------------- ldo.c | 24 ++++++++++++++-------- ldo.h | 5 +---- lopcodes.c | 4 +--- lopcodes.h | 22 +++++++++++--------- ltm.c | 10 +++++---- lvm.c | 60 +++++++++++++++++++++++------------------------------- 7 files changed, 80 insertions(+), 77 deletions(-) diff --git a/lcode.c b/lcode.c index 25fb0770c2..eb5a2c8243 100644 --- a/lcode.c +++ b/lcode.c @@ -1,5 +1,5 @@ /* -** $Id: lcode.c,v 2.152 2018/01/28 15:13:26 roberto Exp roberto $ +** $Id: lcode.c,v 2.153 2018/02/09 15:16:06 roberto Exp roberto $ ** Code generator for Lua ** See Copyright Notice in lua.h */ @@ -158,16 +158,12 @@ int luaK_jump (FuncState *fs) { */ void luaK_ret (FuncState *fs, int first, int nret) { OpCode op; - if (fs->f->is_vararg) - op = OP_RETVARARG; - else { - switch (nret) { - case 0: op = OP_RETURN0; break; - case 1: op = OP_RETURN1; break; - default: op = OP_RETURN; break; - } + switch (nret) { + case 0: op = OP_RETURN0; break; + case 1: op = OP_RETURN1; break; + default: op = OP_RETURN; break; } - luaK_codeABC(fs, op, first, nret + 1, fs->f->numparams); + luaK_codeABC(fs, op, first, nret + 1, 0); } @@ -1646,10 +1642,18 @@ void luaK_finish (FuncState *fs) { Instruction *pc = &p->code[i]; lua_assert(i == 0 || isOT(*(pc - 1)) == isIT(*pc)); switch (GET_OPCODE(*pc)) { - case OP_RETURN: case OP_RETURN0: case OP_RETURN1: - case OP_RETVARARG: case OP_TAILCALL: { - if (p->sizep > 0) - SETARG_k(*pc, 1); /* signal that they must close upvalues */ + case OP_RETURN0: case OP_RETURN1: { + if (p->sizep == 0 && !p->is_vararg) + break; /* no extra work */ + /* else use OP_RETURN to do the extra work */ + SET_OPCODE(*pc, OP_RETURN); + /* FALLTHROUGH */ + } + case OP_RETURN: case OP_TAILCALL: { + if (p->sizep > 0 || p->is_vararg) { + SETARG_C(*pc, p->is_vararg ? p->numparams + 1 : 0); + SETARG_k(*pc, 1); /* signal that there is extra work */ + } break; } case OP_JMP: { diff --git a/ldo.c b/ldo.c index 64512487e8..19d6aefd38 100644 --- a/ldo.c +++ b/ldo.c @@ -1,5 +1,5 @@ /* -** $Id: ldo.c,v 2.192 2018/02/07 15:55:18 roberto Exp roberto $ +** $Id: ldo.c,v 2.193 2018/02/09 15:16:06 roberto Exp roberto $ ** Stack and Call structure of Lua ** See Copyright Notice in lua.h */ @@ -310,11 +310,19 @@ void luaD_hookcall (lua_State *L, CallInfo *ci) { } -void luaD_rethook (lua_State *L, CallInfo *ci) { - if (isLuacode(ci)) +static void rethook (lua_State *L, CallInfo *ci) { + int delta = 0; + if (isLuacode(ci)) { + Proto *p = clLvalue(s2v(ci->func))->p; + if (p->is_vararg) + delta = ci->u.l.nextraargs + p->numparams + 1; L->top = ci->top; /* prepare top */ - if (L->hookmask & LUA_MASKRET) /* is return hook on? */ + } + if (L->hookmask & LUA_MASKRET) { /* is return hook on? */ + ci->func += delta; /* if vararg, back to virtual 'func' */ luaD_hook(L, LUA_HOOKRET, -1); /* call it */ + ci->func -= delta; + } if (isLua(ci->previous)) L->oldpc = ci->previous->u.l.savedpc; /* update 'oldpc' */ } @@ -343,8 +351,8 @@ void luaD_tryfuncTM (lua_State *L, StkId func) { ** expressions, multiple results for tail calls/single parameters) ** separated. */ -void luaD_moveresults (lua_State *L, StkId firstResult, StkId res, - int nres, int wanted) { +static void moveresults (lua_State *L, StkId firstResult, StkId res, + int nres, int wanted) { switch (wanted) { /* handle typical cases separately */ case 0: break; /* nothing to move */ case 1: { /* one result needed */ @@ -387,12 +395,12 @@ void luaD_moveresults (lua_State *L, StkId firstResult, StkId res, void luaD_poscall (lua_State *L, CallInfo *ci, StkId firstResult, int nres) { if (L->hookmask) { ptrdiff_t fr = savestack(L, firstResult); /* hook may change stack */ - luaD_rethook(L, ci); + rethook(L, ci); firstResult = restorestack(L, fr); } L->ci = ci->previous; /* back to caller */ /* move results to proper place */ - luaD_moveresults(L, firstResult, ci->func, nres, ci->nresults); + moveresults(L, firstResult, ci->func, nres, ci->nresults); } diff --git a/ldo.h b/ldo.h index 864cf3e6ea..9d976b3223 100644 --- a/ldo.h +++ b/ldo.h @@ -1,5 +1,5 @@ /* -** $Id: ldo.h,v 2.40 2018/02/06 19:16:56 roberto Exp roberto $ +** $Id: ldo.h,v 2.41 2018/02/09 15:16:06 roberto Exp roberto $ ** Stack and Call structure of Lua ** See Copyright Notice in lua.h */ @@ -62,10 +62,7 @@ LUAI_FUNC int luaD_pcall (lua_State *L, Pfunc func, void *u, ptrdiff_t oldtop, ptrdiff_t ef); LUAI_FUNC void luaD_poscall (lua_State *L, CallInfo *ci, StkId firstResult, int nres); -LUAI_FUNC void luaD_rethook (lua_State *L, CallInfo *ci); LUAI_FUNC int luaD_reallocstack (lua_State *L, int newsize, int raiseerror); -LUAI_FUNC void luaD_moveresults (lua_State *L, StkId firstResult, StkId res, - int nres, int wanted); LUAI_FUNC int luaD_growstack (lua_State *L, int n, int raiseerror); LUAI_FUNC void luaD_shrinkstack (lua_State *L); LUAI_FUNC void luaD_inctop (lua_State *L); diff --git a/lopcodes.c b/lopcodes.c index d04e707f53..aa3055be3e 100644 --- a/lopcodes.c +++ b/lopcodes.c @@ -1,5 +1,5 @@ /* -** $Id: lopcodes.c,v 1.76 2018/02/07 15:18:04 roberto Exp roberto $ +** $Id: lopcodes.c,v 1.77 2018/02/09 15:16:06 roberto Exp roberto $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ @@ -80,7 +80,6 @@ LUAI_DDEF const char *const luaP_opnames[NUM_OPCODES+1] = { "CALL", "TAILCALL", "RETURN", - "RETVARARG", "RETURN0", "RETURN1", "FORLOOP1", @@ -162,7 +161,6 @@ LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = { ,opmode(1, 1, 0, 1, iABC) /* OP_CALL */ ,opmode(1, 1, 0, 1, iABC) /* OP_TAILCALL */ ,opmode(0, 1, 0, 0, iABC) /* OP_RETURN */ - ,opmode(0, 1, 0, 0, iABC) /* OP_RETVARARG */ ,opmode(0, 0, 0, 0, iABC) /* OP_RETURN0 */ ,opmode(0, 0, 0, 0, iABC) /* OP_RETURN1 */ ,opmode(0, 0, 0, 1, iABx) /* OP_FORLOOP1 */ diff --git a/lopcodes.h b/lopcodes.h index 3c7a95739f..b2e22c2786 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -1,5 +1,5 @@ /* -** $Id: lopcodes.h,v 1.186 2018/02/07 15:18:04 roberto Exp roberto $ +** $Id: lopcodes.h,v 1.187 2018/02/09 15:16:06 roberto Exp roberto $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ @@ -267,8 +267,7 @@ OP_TESTSET,/* A B if (not R(B) == k) then R(A) := R(B) else pc++ */ OP_CALL,/* A B C R(A), ... ,R(A+C-2) := R(A)(R(A+1), ... ,R(A+B-1)) */ OP_TAILCALL,/* A B C return R(A)(R(A+1), ... ,R(A+B-1)) */ -OP_RETURN,/* A B return R(A), ... ,R(A+B-2) (see note) */ -OP_RETVARARG,/* A B return R(A), ... ,R(A+B-2) (see note) */ +OP_RETURN,/* A B C return R(A), ... ,R(A+B-2) (see note) */ OP_RETURN0,/* return */ OP_RETURN1,/* A return R(A) */ @@ -302,14 +301,13 @@ OP_EXTRAARG/* Ax extra (larger) argument for previous opcode */ /*=========================================================================== Notes: (*) In OP_CALL, if (B == 0) then B = top. If (C == 0), then 'top' is - set to last_result+1, so next open instruction (OP_CALL, OP_RETURN, + set to last_result+1, so next open instruction (OP_CALL, OP_RETURN*, OP_SETLIST) may use 'top'. (*) In OP_VARARG, if (C == 0) then use actual number of varargs and set top (like in OP_CALL with C == 0). - (*) In OP_RETURN/OP_RETVARARG, if (B == 0) then return up to 'top'. - (OP_RETVARARG is the return instruction for vararg functions.) + (*) In OP_RETURN, if (B == 0) then return up to 'top'. (*) In OP_SETLIST, if (B == 0) then real B = 'top'; if (C == 0) then next 'instruction' is EXTRAARG(real C). @@ -326,9 +324,11 @@ OP_EXTRAARG/* Ax extra (larger) argument for previous opcode */ (*) All 'skips' (pc++) assume that next instruction is a jump. - (*) In instructions ending a function (OP_RETURN*, OP_TAILCALL), k - specifies that the function builds upvalues, which may need to be - closed. + (*) In instructions OP_RETURN/OP_TAILCALL, 'k' specifies that the + function either builds upvalues, which may need to be closed, or is + vararg, which must be corrected before returning. When 'k' is true, + C > 0 means the function is vararg and (C - 1) is its number of + fixed parameters. ===========================================================================*/ @@ -351,7 +351,9 @@ LUAI_DDEC const lu_byte luaP_opmodes[NUM_OPCODES]; #define testOTMode(m) (luaP_opmodes[m] & (1 << 6)) /* "out top" (set top for next instruction) */ -#define isOT(i) (testOTMode(GET_OPCODE(i)) && GETARG_C(i) == 0) +#define isOT(i) \ + ((testOTMode(GET_OPCODE(i)) && GETARG_C(i) == 0) || \ + GET_OPCODE(i) == OP_TAILCALL) /* "in top" (uses top from previous instruction) */ #define isIT(i) (testITMode(GET_OPCODE(i)) && GETARG_B(i) == 0) diff --git a/ltm.c b/ltm.c index d88b636be6..be7dc4f0fc 100644 --- a/ltm.c +++ b/ltm.c @@ -1,5 +1,5 @@ /* -** $Id: ltm.c,v 2.59 2018/02/07 15:18:04 roberto Exp roberto $ +** $Id: ltm.c,v 2.60 2018/02/09 15:16:06 roberto Exp roberto $ ** Tag methods ** See Copyright Notice in lua.h */ @@ -222,10 +222,12 @@ void luaT_adjustvarargs (lua_State *L, int nfixparams, CallInfo *ci) { int nextra = actual - nfixparams; /* number of extra arguments */ ci->u.l.nextraargs = nextra; checkstackGC(L, nfixparams + 1); - /* copy function and fixed parameters to the top of the stack */ - for (i = 0; i <= nfixparams; i++) { + /* copy function to the top of the stack */ + setobjs2s(L, L->top++, ci->func); + /* move fixed parameters to the top of the stack */ + for (i = 1; i <= nfixparams; i++) { setobjs2s(L, L->top++, ci->func + i); - setnilvalue(s2v(ci->func + i)); /* erase original copy (for GC) */ + setnilvalue(s2v(ci->func + i)); /* erase original parameter (for GC) */ } ci->func += actual + 1; ci->top += actual + 1; diff --git a/lvm.c b/lvm.c index 2d01e4c18c..0cc3a0b9ca 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.338 2018/02/07 15:18:04 roberto Exp roberto $ +** $Id: lvm.c,v 2.339 2018/02/09 15:16:06 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -1493,23 +1493,35 @@ void luaV_execute (lua_State *L, CallInfo *ci) { } vmcase(OP_TAILCALL) { int b = GETARG_B(i); /* number of arguments + 1 (function) */ + int delta = 0; /* virtual 'func' - real 'func' (vararg functions) */ if (b != 0) L->top = ra + b; else /* previous instruction set top */ b = cast_int(L->top - ra); - lua_assert(GETARG_C(i) - 1 == LUA_MULTRET); + savepc(ci); + if (TESTARG_k(i)) { + int nparams1 = GETARG_C(i); + if (nparams1) /* vararg function? */ + delta = ci->u.l.nextraargs + nparams1; + luaF_close(L, base); /* close upvalues from current call */ + } if (!ttisfunction(vra)) { /* not a function? */ - ProtectNT(luaD_tryfuncTM(L, ra)); /* try '__call' metamethod */ + luaD_tryfuncTM(L, ra); /* try '__call' metamethod */ b++; /* there is now one extra argument */ } - if (TESTARG_k(i)) - luaF_close(L, base); /* close upvalues from current call */ if (!ttisLclosure(vra)) { /* C function? */ - ProtectNT(luaD_call(L, ra, LUA_MULTRET)); /* call it */ + luaD_call(L, ra, LUA_MULTRET); /* call it */ + updatetrap(ci); + if (trap) { /* stack may have been relocated */ + updatebase(ci); + ra = RA(i); + } + ci->func -= delta; + luaD_poscall(L, ci, ra, cast_int(L->top - ra)); + return; } else { /* Lua tail call */ - if (cl->p->is_vararg) - ci->func -= cl->p->numparams + ci->u.l.nextraargs + 1; + ci->func -= delta; luaD_pretailcall(L, ci, ra, b); /* prepare call frame */ goto tailcall; } @@ -1518,34 +1530,16 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmcase(OP_RETURN) { int b = GETARG_B(i); int n = (b != 0 ? b - 1 : cast_int(L->top - ra)); - if (TESTARG_k(i)) - luaF_close(L, base); - halfProtect(luaD_poscall(L, ci, ra, n)); - return; - } - vmcase(OP_RETVARARG) { - int b = GETARG_B(i); - int nparams = GETARG_C(i); - int nres = (b != 0 ? b - 1 : cast_int(L->top - ra)); - int delta = ci->u.l.nextraargs + nparams + 2; - if (TESTARG_k(i)) - luaF_close(L, base); - savepc(L); - /* code similar to 'luaD_poscall', but with a delta */ - if (L->hookmask) { - luaD_rethook(L, ci); - if (ci->u.l.trap) { - updatebase(ci); - ra = RA(i); - } + if (TESTARG_k(i)) { + int nparams1 = GETARG_C(i); + if (nparams1) /* vararg function? */ + ci->func -= ci->u.l.nextraargs + nparams1; + luaF_close(L, base); /* there may be open upvalues */ } - L->ci = ci->previous; /* back to caller */ - luaD_moveresults(L, ra, base - delta, nres, ci->nresults); + halfProtect(luaD_poscall(L, ci, ra, n)); return; } vmcase(OP_RETURN0) { - if (TESTARG_k(i)) - luaF_close(L, base); if (L->hookmask) halfProtect(luaD_poscall(L, ci, ra, 0)); /* no hurry... */ else { @@ -1558,8 +1552,6 @@ void luaV_execute (lua_State *L, CallInfo *ci) { return; } vmcase(OP_RETURN1) { - if (TESTARG_k(i)) - luaF_close(L, base); if (L->hookmask) halfProtect(luaD_poscall(L, ci, ra, 1)); /* no hurry... */ else { From c7a8cba74512a16aa01a99f2cdf395b358ada2fc Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 15 Feb 2018 16:06:24 -0200 Subject: [PATCH 0208/1145] no more 'nfield' string --- lstate.h | 4 ++-- lstring.c | 13 +++++-------- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/lstate.h b/lstate.h index 2cce8e4f93..65229491f7 100644 --- a/lstate.h +++ b/lstate.h @@ -1,5 +1,5 @@ /* -** $Id: lstate.h,v 2.153 2017/12/19 16:40:17 roberto Exp roberto $ +** $Id: lstate.h,v 2.154 2018/02/09 15:16:06 roberto Exp roberto $ ** Global State ** See Copyright Notice in lua.h */ @@ -176,7 +176,7 @@ typedef struct global_State { lua_CFunction panic; /* to be called in unprotected errors */ struct lua_State *mainthread; const lua_Number *version; /* pointer to version number */ - TString *nfield; /* string "n" (key in vararg tables) */ + TString *memerrmsg; /* message for memory-allocation errors */ TString *tmname[TM_N]; /* array with tag-method names */ struct Table *mt[LUA_NUMTAGS]; /* metatables for basic types */ TString *strcache[STRCACHE_N][STRCACHE_M]; /* cache for strings in API */ diff --git a/lstring.c b/lstring.c index f22ac5b8d6..fbc3e02d17 100644 --- a/lstring.c +++ b/lstring.c @@ -1,5 +1,5 @@ /* -** $Id: lstring.c,v 2.62 2017/12/18 13:00:57 roberto Exp roberto $ +** $Id: lstring.c,v 2.63 2018/01/28 15:13:26 roberto Exp roberto $ ** String table (keeps all strings handled by Lua) ** See Copyright Notice in lua.h */ @@ -122,7 +122,7 @@ void luaS_clearcache (global_State *g) { for (i = 0; i < STRCACHE_N; i++) for (j = 0; j < STRCACHE_M; j++) { if (iswhite(g->strcache[i][j])) /* will entry be collected? */ - g->strcache[i][j] = g->nfield; /* replace it with something fixed */ + g->strcache[i][j] = g->memerrmsg; /* replace it with something fixed */ } } @@ -133,19 +133,16 @@ void luaS_clearcache (global_State *g) { void luaS_init (lua_State *L) { global_State *g = G(L); int i, j; - TString *memerrmsg; stringtable *tb = &G(L)->strt; tb->hash = luaM_newvector(L, MINSTRTABSIZE, TString*); tablerehash(tb->hash, 0, MINSTRTABSIZE); /* clear array */ tb->size = MINSTRTABSIZE; /* pre-create memory-error message */ - memerrmsg = luaS_newliteral(L, MEMERRMSG); - luaC_fix(L, obj2gco(memerrmsg)); /* it should never be collected */ - g->nfield = luaS_newliteral(L, "n"); /* pre-create "n" field name */ - luaC_fix(L, obj2gco(g->nfield)); /* it also should never be collected */ + g->memerrmsg = luaS_newliteral(L, MEMERRMSG); + luaC_fix(L, obj2gco(g->memerrmsg)); /* it should never be collected */ for (i = 0; i < STRCACHE_N; i++) /* fill cache with valid strings */ for (j = 0; j < STRCACHE_M; j++) - g->strcache[i][j] = g->nfield; + g->strcache[i][j] = g->memerrmsg; } From 104d249ffbf76828caa5e204979f5ddad45f2bcb Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Sat, 17 Feb 2018 16:22:00 -0200 Subject: [PATCH 0209/1145] in return hook, 'top' must be corrected only if smaller than 'ci->top'. (It may be larger when returning multiple values, and then it must be larger to preserve that stack slice.) --- ldo.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/ldo.c b/ldo.c index 19d6aefd38..f797df29a5 100644 --- a/ldo.c +++ b/ldo.c @@ -1,5 +1,5 @@ /* -** $Id: ldo.c,v 2.193 2018/02/09 15:16:06 roberto Exp roberto $ +** $Id: ldo.c,v 2.194 2018/02/15 15:34:29 roberto Exp roberto $ ** Stack and Call structure of Lua ** See Copyright Notice in lua.h */ @@ -316,7 +316,8 @@ static void rethook (lua_State *L, CallInfo *ci) { Proto *p = clLvalue(s2v(ci->func))->p; if (p->is_vararg) delta = ci->u.l.nextraargs + p->numparams + 1; - L->top = ci->top; /* prepare top */ + if (L->top < ci->top) + L->top = ci->top; /* correct top */ } if (L->hookmask & LUA_MASKRET) { /* is return hook on? */ ci->func += delta; /* if vararg, back to virtual 'func' */ From 49dae52d0808776f5861eb33efa1d13b05e44512 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Sat, 17 Feb 2018 17:20:00 -0200 Subject: [PATCH 0210/1145] correct way to check stack space for vararg functions --- lcode.c | 4 ++-- lparser.c | 4 +--- ltm.c | 8 +++++--- ltm.h | 4 ++-- lvm.c | 8 ++++---- 5 files changed, 14 insertions(+), 14 deletions(-) diff --git a/lcode.c b/lcode.c index eb5a2c8243..e280857191 100644 --- a/lcode.c +++ b/lcode.c @@ -1,5 +1,5 @@ /* -** $Id: lcode.c,v 2.153 2018/02/09 15:16:06 roberto Exp roberto $ +** $Id: lcode.c,v 2.154 2018/02/15 15:34:29 roberto Exp roberto $ ** Code generator for Lua ** See Copyright Notice in lua.h */ @@ -31,7 +31,7 @@ /* Maximum number of registers in a Lua function (must fit in 8 bits) */ -#define MAXREGS 254 +#define MAXREGS 255 #define hasjumps(e) ((e)->t != (e)->f) diff --git a/lparser.c b/lparser.c index 802c64bd20..da27c47222 100644 --- a/lparser.c +++ b/lparser.c @@ -1,5 +1,5 @@ /* -** $Id: lparser.c,v 2.176 2018/02/07 15:18:04 roberto Exp roberto $ +** $Id: lparser.c,v 2.177 2018/02/09 15:16:06 roberto Exp roberto $ ** Lua Parser ** See Copyright Notice in lua.h */ @@ -578,8 +578,6 @@ static void close_func (LexState *ls) { luaM_shrinkvector(L, f->p, f->sizep, fs->np, Proto *); luaM_shrinkvector(L, f->locvars, f->sizelocvars, fs->nlocvars, LocVar); luaM_shrinkvector(L, f->upvalues, f->sizeupvalues, fs->nups, Upvaldesc); - if (f->is_vararg) - f->maxstacksize++; /* ensure space to copy the function */ ls->fs = fs->prev; luaC_checkGC(L); } diff --git a/ltm.c b/ltm.c index be7dc4f0fc..e46cc150ea 100644 --- a/ltm.c +++ b/ltm.c @@ -1,5 +1,5 @@ /* -** $Id: ltm.c,v 2.60 2018/02/09 15:16:06 roberto Exp roberto $ +** $Id: ltm.c,v 2.61 2018/02/15 15:34:29 roberto Exp roberto $ ** Tag methods ** See Copyright Notice in lua.h */ @@ -216,12 +216,13 @@ int luaT_callorderiTM (lua_State *L, const TValue *p1, int v2, } -void luaT_adjustvarargs (lua_State *L, int nfixparams, CallInfo *ci) { +void luaT_adjustvarargs (lua_State *L, int nfixparams, CallInfo *ci, + Proto *p) { int i; int actual = cast_int(L->top - ci->func) - 1; /* number of arguments */ int nextra = actual - nfixparams; /* number of extra arguments */ ci->u.l.nextraargs = nextra; - checkstackGC(L, nfixparams + 1); + checkstackGC(L, p->maxstacksize + 1); /* copy function to the top of the stack */ setobjs2s(L, L->top++, ci->func); /* move fixed parameters to the top of the stack */ @@ -231,6 +232,7 @@ void luaT_adjustvarargs (lua_State *L, int nfixparams, CallInfo *ci) { } ci->func += actual + 1; ci->top += actual + 1; + lua_assert(L->top <= ci->top && ci->top <= L->stack_last); } diff --git a/ltm.h b/ltm.h index 54cece9e1a..dbb21bd5a6 100644 --- a/ltm.h +++ b/ltm.h @@ -1,5 +1,5 @@ /* -** $Id: ltm.h,v 2.30 2018/02/07 15:18:04 roberto Exp roberto $ +** $Id: ltm.h,v 2.31 2018/02/09 15:16:06 roberto Exp roberto $ ** Tag methods ** See Copyright Notice in lua.h */ @@ -79,7 +79,7 @@ LUAI_FUNC int luaT_callorderiTM (lua_State *L, const TValue *p1, int v2, int inv, TMS event); LUAI_FUNC void luaT_adjustvarargs (lua_State *L, int nfixparams, - struct CallInfo *ci); + struct CallInfo *ci, Proto *p); LUAI_FUNC void luaT_getvarargs (lua_State *L, struct CallInfo *ci, StkId where, int wanted); diff --git a/lvm.c b/lvm.c index 0cc3a0b9ca..0ba6b4bc74 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.339 2018/02/09 15:16:06 roberto Exp roberto $ +** $Id: lvm.c,v 2.340 2018/02/15 15:34:29 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -1713,13 +1713,13 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_PREPVARARG) { - luaT_adjustvarargs(L, GETARG_A(i), ci); + luaT_adjustvarargs(L, GETARG_A(i), ci, cl->p); updatetrap(ci); if (trap) { luaD_hookcall(L, ci); - L->oldpc = pc + 1; /* next opcode will be seen as a new line */ + L->oldpc = pc + 1; /* next opcode will be seen as a "new" line */ } - updatebase(ci); + updatebase(ci); /* function has new base after adjustment */ vmbreak; } vmcase(OP_EXTRAARG) { From 422318f6777d8d3bac13ade797d9c8eaa38686b6 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Sat, 17 Feb 2018 17:29:29 -0200 Subject: [PATCH 0211/1145] two new fields 'fTransfer'/'nTransfer' in 'lua_Debug' structure (for information about values being given and returned in function calls) --- ldblib.c | 8 ++++++-- ldebug.c | 14 +++++++++++--- ldo.c | 31 ++++++++++++++++++++++--------- ldo.h | 5 +++-- lstate.h | 9 +++++++-- lua.h | 4 +++- 6 files changed, 52 insertions(+), 19 deletions(-) diff --git a/ldblib.c b/ldblib.c index 0bcd2e9e93..c1e26e0d41 100644 --- a/ldblib.c +++ b/ldblib.c @@ -1,5 +1,5 @@ /* -** $Id: ldblib.c,v 1.150 2015/11/19 19:16:22 roberto Exp roberto $ +** $Id: ldblib.c,v 1.151 2015/11/23 11:29:43 roberto Exp roberto $ ** Interface from Lua to its debug API ** See Copyright Notice in lua.h */ @@ -146,7 +146,7 @@ static int db_getinfo (lua_State *L) { lua_Debug ar; int arg; lua_State *L1 = getthread(L, &arg); - const char *options = luaL_optstring(L, arg+2, "flnStu"); + const char *options = luaL_optstring(L, arg+2, "flnSrtu"); checkstack(L, L1, 3); if (lua_isfunction(L, arg + 1)) { /* info about a function? */ options = lua_pushfstring(L, ">%s", options); /* add '>' to 'options' */ @@ -180,6 +180,10 @@ static int db_getinfo (lua_State *L) { settabss(L, "name", ar.name); settabss(L, "namewhat", ar.namewhat); } + if (strchr(options, 'r')) { + settabsi(L, "fTransfer", ar.fTransfer); + settabsi(L, "nTransfer", ar.nTransfer); + } if (strchr(options, 't')) settabsb(L, "istailcall", ar.istailcall); if (strchr(options, 'L')) diff --git a/ldebug.c b/ldebug.c index 3ddac54709..bede7e3710 100644 --- a/ldebug.c +++ b/ldebug.c @@ -1,5 +1,5 @@ /* -** $Id: ldebug.c,v 2.153 2018/02/06 19:16:56 roberto Exp roberto $ +** $Id: ldebug.c,v 2.154 2018/02/09 15:16:06 roberto Exp roberto $ ** Debug Interface ** See Copyright Notice in lua.h */ @@ -356,6 +356,14 @@ static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar, } break; } + case 'r': { + if (ci == NULL || !(ci->callstatus & CIST_TRAN)) + ar->fTransfer = ar->nTransfer = 0; + else { + ar->fTransfer = ci->u2.transferinfo.fTransfer; + ar->nTransfer = ci->u2.transferinfo.nTransfer; + } + } case 'L': case 'f': /* handled by lua_getinfo */ break; @@ -790,7 +798,7 @@ void luaG_traceexec (lua_State *L) { if (!isIT(*(ci->u.l.savedpc - 1))) L->top = ci->top; /* prepare top */ if (counthook) - luaD_hook(L, LUA_HOOKCOUNT, -1); /* call count hook */ + luaD_hook(L, LUA_HOOKCOUNT, -1, 0, 0); /* call count hook */ if (mask & LUA_MASKLINE) { Proto *p = ci_func(ci)->p; const Instruction *npc = ci->u.l.savedpc; @@ -799,7 +807,7 @@ void luaG_traceexec (lua_State *L) { npc <= L->oldpc || /* when jump back (loop), or when */ changedline(p, pcRel(L->oldpc, p), npci)) { /* enter new line */ int newline = luaG_getfuncline(p, npci); /* new line */ - luaD_hook(L, LUA_HOOKLINE, newline); /* call line hook */ + luaD_hook(L, LUA_HOOKLINE, newline, 0, 0); /* call line hook */ } L->oldpc = npc; } diff --git a/ldo.c b/ldo.c index f797df29a5..6acd2f645b 100644 --- a/ldo.c +++ b/ldo.c @@ -267,9 +267,11 @@ void luaD_inctop (lua_State *L) { ** called. (Both 'L->hook' and 'L->hookmask', which triggers this ** function, can be changed asynchronously by signals.) */ -void luaD_hook (lua_State *L, int event, int line) { +void luaD_hook (lua_State *L, int event, int line, + int fTransfer, int nTransfer) { lua_Hook hook = L->hook; if (hook && L->allowhook) { /* make sure there is a hook */ + int mask = CIST_HOOKED; CallInfo *ci = L->ci; ptrdiff_t top = savestack(L, L->top); ptrdiff_t ci_top = savestack(L, ci->top); @@ -277,11 +279,16 @@ void luaD_hook (lua_State *L, int event, int line) { ar.event = event; ar.currentline = line; ar.i_ci = ci; + if (nTransfer != 0) { + mask |= CIST_TRAN; /* 'ci' has transfer information */ + ci->u2.transferinfo.fTransfer = fTransfer; + ci->u2.transferinfo.nTransfer = nTransfer; + } luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */ if (L->top + LUA_MINSTACK > ci->top) ci->top = L->top + LUA_MINSTACK; L->allowhook = 0; /* cannot call hooks inside a hook */ - ci->callstatus |= CIST_HOOKED; + ci->callstatus |= mask; lua_unlock(L); (*hook)(L, &ar); lua_lock(L); @@ -289,7 +296,7 @@ void luaD_hook (lua_State *L, int event, int line) { L->allowhook = 1; ci->top = restorestack(L, ci_top); L->top = restorestack(L, top); - ci->callstatus &= ~CIST_HOOKED; + ci->callstatus &= ~mask; } } @@ -301,16 +308,18 @@ void luaD_hook (lua_State *L, int event, int line) { */ void luaD_hookcall (lua_State *L, CallInfo *ci) { int hook = (ci->callstatus & CIST_TAIL) ? LUA_HOOKTAILCALL : LUA_HOOKCALL; + Proto *p; if (!(L->hookmask & LUA_MASKCALL)) /* some other hook? */ return; /* don't call hook */ + p = clLvalue(s2v(ci->func))->p; L->top = ci->top; /* prepare top */ ci->u.l.savedpc++; /* hooks assume 'pc' is already incremented */ - luaD_hook(L, hook, -1); + luaD_hook(L, hook, -1, 1, p->numparams); ci->u.l.savedpc--; /* correct 'pc' */ } -static void rethook (lua_State *L, CallInfo *ci) { +static void rethook (lua_State *L, CallInfo *ci, StkId firstres, int nres) { int delta = 0; if (isLuacode(ci)) { Proto *p = clLvalue(s2v(ci->func))->p; @@ -320,8 +329,10 @@ static void rethook (lua_State *L, CallInfo *ci) { L->top = ci->top; /* correct top */ } if (L->hookmask & LUA_MASKRET) { /* is return hook on? */ + int fTransfer; ci->func += delta; /* if vararg, back to virtual 'func' */ - luaD_hook(L, LUA_HOOKRET, -1); /* call it */ + fTransfer = cast(unsigned short, firstres - ci->func); + luaD_hook(L, LUA_HOOKRET, -1, fTransfer, nres); /* call it */ ci->func -= delta; } if (isLua(ci->previous)) @@ -396,7 +407,7 @@ static void moveresults (lua_State *L, StkId firstResult, StkId res, void luaD_poscall (lua_State *L, CallInfo *ci, StkId firstResult, int nres) { if (L->hookmask) { ptrdiff_t fr = savestack(L, firstResult); /* hook may change stack */ - rethook(L, ci); + rethook(L, ci, firstResult, nres); firstResult = restorestack(L, fr); } L->ci = ci->previous; /* back to caller */ @@ -458,8 +469,10 @@ void luaD_call (lua_State *L, StkId func, int nresults) { ci->top = L->top + LUA_MINSTACK; ci->func = func; lua_assert(ci->top <= L->stack_last); - if (L->hookmask & LUA_MASKCALL) - luaD_hook(L, LUA_HOOKCALL, -1); + if (L->hookmask & LUA_MASKCALL) { + int narg = cast_int(L->top - func) - 1; + luaD_hook(L, LUA_HOOKCALL, -1, 1, narg); + } lua_unlock(L); n = (*f)(L); /* do the actual call */ lua_lock(L); diff --git a/ldo.h b/ldo.h index 9d976b3223..1fdc75be04 100644 --- a/ldo.h +++ b/ldo.h @@ -1,5 +1,5 @@ /* -** $Id: ldo.h,v 2.41 2018/02/09 15:16:06 roberto Exp roberto $ +** $Id: ldo.h,v 2.42 2018/02/15 15:34:29 roberto Exp roberto $ ** Stack and Call structure of Lua ** See Copyright Notice in lua.h */ @@ -52,7 +52,8 @@ typedef void (*Pfunc) (lua_State *L, void *ud); LUAI_FUNC int luaD_protectedparser (lua_State *L, ZIO *z, const char *name, const char *mode); -LUAI_FUNC void luaD_hook (lua_State *L, int event, int line); +LUAI_FUNC void luaD_hook (lua_State *L, int event, int line, + int fTransfer, int nTransfer); LUAI_FUNC void luaD_hookcall (lua_State *L, CallInfo *ci); LUAI_FUNC void luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, int n); LUAI_FUNC void luaD_call (lua_State *L, StkId func, int nResults); diff --git a/lstate.h b/lstate.h index 65229491f7..06581b017b 100644 --- a/lstate.h +++ b/lstate.h @@ -1,5 +1,5 @@ /* -** $Id: lstate.h,v 2.154 2018/02/09 15:16:06 roberto Exp roberto $ +** $Id: lstate.h,v 2.155 2018/02/15 18:06:24 roberto Exp roberto $ ** Global State ** See Copyright Notice in lua.h */ @@ -103,9 +103,13 @@ typedef struct CallInfo { union { int funcidx; /* called-function index */ int nyield; /* number of values yielded */ + struct { /* info about transfered values (for call/return hooks) */ + unsigned short fTransfer; /* offset of first value transfered */ + unsigned short nTransfer; /* number of values transfered */ + } transferinfo; } u2; short nresults; /* expected number of results from this function */ - lu_byte callstatus; + unsigned short callstatus; } CallInfo; @@ -120,6 +124,7 @@ typedef struct CallInfo { #define CIST_HOOKYIELD (1<<5) /* last hook called yielded */ #define CIST_LEQ (1<<6) /* using __lt for __le */ #define CIST_FIN (1<<7) /* call is running a finalizer */ +#define CIST_TRAN (1<<8) /* 'ci' has transfer information */ /* active function is a Lua function */ #define isLua(ci) (!((ci)->callstatus & CIST_C)) diff --git a/lua.h b/lua.h index 2d8ff8382e..d6277b4e91 100644 --- a/lua.h +++ b/lua.h @@ -1,5 +1,5 @@ /* -** $Id: lua.h,v 1.337 2017/11/02 11:28:56 roberto Exp $ +** $Id: lua.h,v 1.339 2017/11/07 13:25:26 roberto Exp roberto $ ** Lua - A Scripting Language ** Lua.org, PUC-Rio, Brazil (http://www.lua.org) ** See Copyright Notice at the end of this file @@ -452,6 +452,8 @@ struct lua_Debug { int lastlinedefined; /* (S) */ unsigned char nups; /* (u) number of upvalues */ unsigned char nparams;/* (u) number of parameters */ + unsigned char fTransfer;/* (r) index of first value transfered */ + unsigned char nTransfer; /* (r) number of transfered values */ char isvararg; /* (u) */ char istailcall; /* (t) */ char short_src[LUA_IDSIZE]; /* (S) */ From 1afd5a152dc8b3a304236dc4e07bca38ea5eb53a Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 19 Feb 2018 17:06:56 -0300 Subject: [PATCH 0212/1145] more generic way to handle 'gclist' --- lapi.c | 8 ++-- lgc.c | 112 +++++++++++++++++++++++++------------------------------ lgc.h | 4 +- ltable.c | 4 +- lvm.c | 6 +-- lvm.h | 4 +- 6 files changed, 63 insertions(+), 75 deletions(-) diff --git a/lapi.c b/lapi.c index d4bf7e17e3..1ec649b0ee 100644 --- a/lapi.c +++ b/lapi.c @@ -1,5 +1,5 @@ /* -** $Id: lapi.c,v 2.282 2018/01/29 16:21:35 roberto Exp roberto $ +** $Id: lapi.c,v 2.283 2018/02/05 17:10:52 roberto Exp roberto $ ** Lua API ** See Copyright Notice in lua.h */ @@ -828,7 +828,7 @@ LUA_API void lua_rawset (lua_State *L, int idx) { slot = luaH_set(L, hvalue(o), s2v(L->top - 2)); setobj2t(L, slot, s2v(L->top - 1)); invalidateTMcache(hvalue(o)); - luaC_barrierback(L, hvalue(o), s2v(L->top - 1)); + luaC_barrierback(L, gcvalue(o), s2v(L->top - 1)); L->top -= 2; lua_unlock(L); } @@ -841,7 +841,7 @@ LUA_API void lua_rawseti (lua_State *L, int idx, lua_Integer n) { o = index2value(L, idx); api_check(L, ttistable(o), "table expected"); luaH_setint(L, hvalue(o), n, s2v(L->top - 1)); - luaC_barrierback(L, hvalue(o), s2v(L->top - 1)); + luaC_barrierback(L, gcvalue(o), s2v(L->top - 1)); L->top--; lua_unlock(L); } @@ -857,7 +857,7 @@ LUA_API void lua_rawsetp (lua_State *L, int idx, const void *p) { setpvalue(&k, cast_voidp(p)); slot = luaH_set(L, hvalue(o), &k); setobj2t(L, slot, s2v(L->top - 1)); - luaC_barrierback(L, hvalue(o), s2v(L->top - 1)); + luaC_barrierback(L, gcvalue(o), s2v(L->top - 1)); L->top--; lua_unlock(L); } diff --git a/lgc.c b/lgc.c index 20397e4824..77ccae0a3f 100644 --- a/lgc.c +++ b/lgc.c @@ -1,5 +1,5 @@ /* -** $Id: lgc.c,v 2.245 2018/01/28 15:13:26 roberto Exp roberto $ +** $Id: lgc.c,v 2.248 2018/02/19 16:02:25 roberto Exp roberto $ ** Garbage Collector ** See Copyright Notice in lua.h */ @@ -110,12 +110,31 @@ static lu_mem atomic (lua_State *L); #define gnodelast(h) gnode(h, cast_sizet(sizenode(h))) +static GCObject **getgclist (GCObject *o) { + switch (o->tt) { + case LUA_TTABLE: return &gco2t(o)->gclist; + case LUA_TLCL: return &gco2lcl(o)->gclist; + case LUA_TCCL: return &gco2ccl(o)->gclist; + case LUA_TTHREAD: return &gco2th(o)->gclist; + case LUA_TPROTO: return &gco2p(o)->gclist; + default: lua_assert(0); return 0; + } +} + + /* -** link collectable object 'o' into list pointed by 'p' +** Link a collectable object 'o' with a known type into list pointed by 'p'. */ #define linkgclist(o,p) ((o)->gclist = (p), (p) = obj2gco(o)) +/* +** Link a generic collectable object 'o' into list pointed by 'p'. +*/ +#define linkobjgclist(o,p) (*getgclist(o) = (p), (p) = obj2gco(o)) + + + /* ** Clear keys for empty entries in tables. If entry is empty ** and its key is not marked, mark its entry as dead. This allows the @@ -175,14 +194,14 @@ void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v) { ** barrier that moves collector backward, that is, mark the black object ** pointing to a white object as gray again. */ -void luaC_barrierback_ (lua_State *L, Table *t) { +void luaC_barrierback_ (lua_State *L, GCObject *o) { global_State *g = G(L); - lua_assert(isblack(t) && !isdead(g, t)); - lua_assert(g->gckind != KGC_GEN || (isold(t) && getage(t) != G_TOUCHED1)); - if (getage(t) != G_TOUCHED2) /* not already in gray list? */ - linkgclist(t, g->grayagain); /* link it in 'grayagain' */ - black2gray(t); /* make table gray (again) */ - setage(t, G_TOUCHED1); /* touched in current cycle */ + lua_assert(isblack(o) && !isdead(g, o)); + lua_assert(g->gckind != KGC_GEN || (isold(o) && getage(o) != G_TOUCHED1)); + if (getage(o) != G_TOUCHED2) /* not already in gray list? */ + linkobjgclist(o, g->grayagain); /* link it in 'grayagain' */ + black2gray(o); /* make table gray (again) */ + setage(o, G_TOUCHED1); /* touched in current cycle */ } @@ -276,24 +295,9 @@ static void reallymarkobject (global_State *g, GCObject *o) { markvalue(g, uv->v); /* mark its content */ break; } - case LUA_TLCL: { - linkgclist(gco2lcl(o), g->gray); - break; - } - case LUA_TCCL: { - linkgclist(gco2ccl(o), g->gray); - break; - } - case LUA_TTABLE: { - linkgclist(gco2t(o), g->gray); - break; - } - case LUA_TTHREAD: { - linkgclist(gco2th(o), g->gray); - break; - } - case LUA_TPROTO: { - linkgclist(gco2p(o), g->gray); + case LUA_TLCL: case LUA_TCCL: case LUA_TTABLE: + case LUA_TTHREAD: case LUA_TPROTO: { + linkobjgclist(o, g->gray); break; } default: lua_assert(0); break; @@ -605,34 +609,18 @@ static int traversethread (global_State *g, lua_State *th) { static lu_mem propagatemark (global_State *g) { GCObject *o = g->gray; gray2black(o); + g->gray = *getgclist(o); /* remove from 'gray' list */ switch (o->tt) { - case LUA_TTABLE: { - Table *h = gco2t(o); - g->gray = h->gclist; /* remove from 'gray' list */ - return traversetable(g, h); - } - case LUA_TLCL: { - LClosure *cl = gco2lcl(o); - g->gray = cl->gclist; /* remove from 'gray' list */ - return traverseLclosure(g, cl); - } - case LUA_TCCL: { - CClosure *cl = gco2ccl(o); - g->gray = cl->gclist; /* remove from 'gray' list */ - return traverseCclosure(g, cl); - } + case LUA_TTABLE: return traversetable(g, gco2t(o)); + case LUA_TLCL: return traverseLclosure(g, gco2lcl(o)); + case LUA_TCCL: return traverseCclosure(g, gco2ccl(o)); + case LUA_TPROTO: return traverseproto(g, gco2p(o)); case LUA_TTHREAD: { lua_State *th = gco2th(o); - g->gray = th->gclist; /* remove from 'gray' list */ linkgclist(th, g->grayagain); /* insert into 'grayagain' list */ black2gray(o); return traversethread(g, th); } - case LUA_TPROTO: { - Proto *p = gco2p(o); - g->gray = p->gclist; /* remove from 'gray' list */ - return traverseproto(g, p); - } default: lua_assert(0); return 0; } } @@ -1069,8 +1057,8 @@ static void whitelist (global_State *g, GCObject *p) { ** Correct a list of gray objects. Because this correction is ** done after sweeping, young objects can be white and still ** be in the list. They are only removed. -** For tables, advance 'touched1' to 'touched2'; 'touched2' objects -** become regular old and are removed from the list. +** For tables and userdata, advance 'touched1' to 'touched2'; 'touched2' +** objects become regular old and are removed from the list. ** For threads, just remove white ones from the list. */ static GCObject **correctgraylist (GCObject **p) { @@ -1078,21 +1066,21 @@ static GCObject **correctgraylist (GCObject **p) { while ((curr = *p) != NULL) { switch (curr->tt) { case LUA_TTABLE: { - Table *h = gco2t(curr); - if (getage(h) == G_TOUCHED1) { /* touched in this cycle? */ - lua_assert(isgray(h)); - gray2black(h); /* make it black, for next barrier */ - changeage(h, G_TOUCHED1, G_TOUCHED2); - p = &h->gclist; /* go to next element */ + GCObject **next = getgclist(curr); + if (getage(curr) == G_TOUCHED1) { /* touched in this cycle? */ + lua_assert(isgray(curr)); + gray2black(curr); /* make it black, for next barrier */ + changeage(curr, G_TOUCHED1, G_TOUCHED2); + p = next; /* go to next element */ } else { - if (!iswhite(h)) { - lua_assert(isold(h)); - if (getage(h) == G_TOUCHED2) - changeage(h, G_TOUCHED2, G_OLD); - gray2black(h); /* make it black */ + if (!iswhite(curr)) { + lua_assert(isold(curr)); + if (getage(curr) == G_TOUCHED2) + changeage(curr, G_TOUCHED2, G_OLD); + gray2black(curr); /* make it black */ } - *p = h->gclist; /* remove 'curr' from gray list */ + *p = *next; /* remove 'curr' from gray list */ } break; } diff --git a/lgc.h b/lgc.h index fec0e5a7ed..fc62cfd876 100644 --- a/lgc.h +++ b/lgc.h @@ -1,5 +1,5 @@ /* -** $Id: lgc.h,v 2.100 2018/01/28 15:13:26 roberto Exp roberto $ +** $Id: lgc.h,v 2.102 2018/02/19 13:55:34 roberto Exp roberto $ ** Garbage Collector ** See Copyright Notice in lua.h */ @@ -173,7 +173,7 @@ LUAI_FUNC void luaC_runtilstate (lua_State *L, int statesmask); LUAI_FUNC void luaC_fullgc (lua_State *L, int isemergency); LUAI_FUNC GCObject *luaC_newobj (lua_State *L, int tt, size_t sz); LUAI_FUNC void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v); -LUAI_FUNC void luaC_barrierback_ (lua_State *L, Table *o); +LUAI_FUNC void luaC_barrierback_ (lua_State *L, GCObject *o); LUAI_FUNC void luaC_protobarrier_ (lua_State *L, Proto *p); LUAI_FUNC void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt); LUAI_FUNC void luaC_changemode (lua_State *L, int newmode); diff --git a/ltable.c b/ltable.c index a892996145..dbe5e2ed57 100644 --- a/ltable.c +++ b/ltable.c @@ -1,5 +1,5 @@ /* -** $Id: ltable.c,v 2.130 2017/12/29 15:58:23 roberto Exp roberto $ +** $Id: ltable.c,v 2.131 2018/01/28 15:13:26 roberto Exp roberto $ ** Lua tables (hash) ** See Copyright Notice in lua.h */ @@ -600,7 +600,7 @@ TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) { } } setnodekey(L, mp, key); - luaC_barrierback(L, t, key); + luaC_barrierback(L, obj2gco(t), key); lua_assert(ttisnil(gval(mp))); return gval(mp); } diff --git a/lvm.c b/lvm.c index 0ba6b4bc74..0389352fb8 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.340 2018/02/15 15:34:29 roberto Exp roberto $ +** $Id: lvm.c,v 2.341 2018/02/17 19:20:00 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -223,7 +223,7 @@ void luaV_finishset (lua_State *L, const TValue *t, TValue *key, /* no metamethod and (now) there is an entry with given key */ setobj2t(L, cast(TValue *, slot), val); /* set its new value */ invalidateTMcache(h); - luaC_barrierback(L, h, val); + luaC_barrierback(L, obj2gco(h), val); return; } /* else will try the metamethod */ @@ -1691,7 +1691,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { TValue *val = s2v(ra + n); setobj2t(L, &h->array[last - 1], val); last--; - luaC_barrierback(L, h, val); + luaC_barrierback(L, obj2gco(h), val); } vmbreak; } diff --git a/lvm.h b/lvm.h index 0d2eed14f4..2cc4130aa0 100644 --- a/lvm.h +++ b/lvm.h @@ -1,5 +1,5 @@ /* -** $Id: lvm.h,v 2.47 2017/11/08 14:50:23 roberto Exp roberto $ +** $Id: lvm.h,v 2.48 2017/11/29 13:02:17 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -96,7 +96,7 @@ */ #define luaV_finishfastset(L,t,slot,v) \ { setobj2t(L, cast(TValue *,slot), v); \ - luaC_barrierback(L, hvalue(t), v); } + luaC_barrierback(L, gcvalue(t), v); } From ca6fe7449a74efde6f959605dbe77acf3e64ca0b Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 20 Feb 2018 13:52:50 -0300 Subject: [PATCH 0213/1145] userdata can have multiple user values --- lapi.c | 35 +++++++++++++++++++++++--------- ldblib.c | 13 ++++++++---- lgc.c | 33 +++++++++++++++--------------- lobject.h | 60 +++++++++++++++++++++++++++++++------------------------ lstring.c | 13 +++++++----- lstring.h | 7 ++----- ltests.c | 25 +++++++++++++---------- lua.h | 15 +++++++++----- 8 files changed, 121 insertions(+), 80 deletions(-) diff --git a/lapi.c b/lapi.c index 1ec649b0ee..78ae7e0b6d 100644 --- a/lapi.c +++ b/lapi.c @@ -10,6 +10,7 @@ #include "lprefix.h" +#include #include #include @@ -733,15 +734,23 @@ LUA_API int lua_getmetatable (lua_State *L, int objindex) { } -LUA_API int lua_getuservalue (lua_State *L, int idx) { +LUA_API int lua_getiuservalue (lua_State *L, int idx, int n) { TValue *o; + int t; lua_lock(L); o = index2value(L, idx); api_check(L, ttisfulluserdata(o), "full userdata expected"); - getuservalue(L, uvalue(o), s2v(L->top)); + if (n <= 0 || n > uvalue(o)->nuvalue) { + setnilvalue(s2v(L->top)); + t = LUA_TNONE; + } + else { + setobj2s(L, L->top, &uvalue(o)->uv[n - 1].uv); + t = ttnov(s2v(L->top)); + } api_incr_top(L); lua_unlock(L); - return ttnov(s2v(L->top - 1)); + return t; } @@ -903,16 +912,23 @@ LUA_API int lua_setmetatable (lua_State *L, int objindex) { } -LUA_API void lua_setuservalue (lua_State *L, int idx) { +LUA_API int lua_setiuservalue (lua_State *L, int idx, int n) { TValue *o; + int res; lua_lock(L); api_checknelems(L, 1); o = index2value(L, idx); api_check(L, ttisfulluserdata(o), "full userdata expected"); - setuservalue(L, uvalue(o), s2v(L->top - 1)); - luaC_barrier(L, gcvalue(o), s2v(L->top - 1)); - L->top--; + if (!(0 < n && n <= uvalue(o)->nuvalue)) + res = 0; + else { + setobj(L, &uvalue(o)->uv[n - 1].uv, s2v(L->top - 1)); + luaC_barrierback(L, gcvalue(o), s2v(L->top - 1)); + L->top--; + res = 1; + } lua_unlock(L); + return res; } @@ -1231,10 +1247,11 @@ LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud) { } -LUA_API void *lua_newuserdata (lua_State *L, size_t size) { +LUA_API void *lua_newuserdatauv (lua_State *L, size_t size, int nuvalue) { Udata *u; lua_lock(L); - u = luaS_newudata(L, size); + api_check(L, 0 <= nuvalue && nuvalue < USHRT_MAX, "invalid value"); + u = luaS_newudata(L, size, nuvalue); setuvalue(L, s2v(L->top), u); api_incr_top(L); luaC_checkGC(L); diff --git a/ldblib.c b/ldblib.c index c1e26e0d41..32da969729 100644 --- a/ldblib.c +++ b/ldblib.c @@ -1,5 +1,5 @@ /* -** $Id: ldblib.c,v 1.151 2015/11/23 11:29:43 roberto Exp roberto $ +** $Id: ldblib.c,v 1.152 2018/02/17 19:29:29 roberto Exp roberto $ ** Interface from Lua to its debug API ** See Copyright Notice in lua.h */ @@ -64,19 +64,24 @@ static int db_setmetatable (lua_State *L) { static int db_getuservalue (lua_State *L) { + int n = luaL_optinteger(L, 2, 1); if (lua_type(L, 1) != LUA_TUSERDATA) lua_pushnil(L); - else - lua_getuservalue(L, 1); + else if (lua_getiuservalue(L, 1, n) != LUA_TNONE) { + lua_pushboolean(L, 1); + return 2; + } return 1; } static int db_setuservalue (lua_State *L) { + int n = luaL_optinteger(L, 3, 1); luaL_checktype(L, 1, LUA_TUSERDATA); luaL_checkany(L, 2); lua_settop(L, 2); - lua_setuservalue(L, 1); + if (!lua_setiuservalue(L, 1, n)) + lua_pushnil(L); return 1; } diff --git a/lgc.c b/lgc.c index 77ccae0a3f..3421bd8ae3 100644 --- a/lgc.c +++ b/lgc.c @@ -113,6 +113,7 @@ static lu_mem atomic (lua_State *L); static GCObject **getgclist (GCObject *o) { switch (o->tt) { case LUA_TTABLE: return &gco2t(o)->gclist; + case LUA_TUSERDATA: return &gco2u(o)->gclist; case LUA_TLCL: return &gco2lcl(o)->gclist; case LUA_TCCL: return &gco2ccl(o)->gclist; case LUA_TTHREAD: return &gco2th(o)->gclist; @@ -269,7 +270,6 @@ GCObject *luaC_newobj (lua_State *L, int tt, size_t sz) { ** to avoid barriers, as their values will be revisited by the thread.) */ static void reallymarkobject (global_State *g, GCObject *o) { - reentry: white2gray(o); switch (o->tt) { case LUA_TSHRSTR: @@ -277,17 +277,6 @@ static void reallymarkobject (global_State *g, GCObject *o) { gray2black(o); break; } - case LUA_TUSERDATA: { - TValue uvalue; - markobjectN(g, gco2u(o)->metatable); /* mark its metatable */ - gray2black(o); - getuservalue(g->mainthread, gco2u(o), &uvalue); - if (valiswhite(&uvalue)) { /* markvalue(g, &uvalue); */ - o = gcvalue(&uvalue); - goto reentry; - } - break; - } case LUA_TUPVAL: { UpVal *uv = gco2upv(o); if (!upisopen(uv)) /* open upvalues are kept gray */ @@ -296,7 +285,7 @@ static void reallymarkobject (global_State *g, GCObject *o) { break; } case LUA_TLCL: case LUA_TCCL: case LUA_TTABLE: - case LUA_TTHREAD: case LUA_TPROTO: { + case LUA_TUSERDATA: case LUA_TTHREAD: case LUA_TPROTO: { linkobjgclist(o, g->gray); break; } @@ -602,6 +591,15 @@ static int traversethread (global_State *g, lua_State *th) { } +static int traverseudata (global_State *g, Udata *u) { + int i; + markobjectN(g, u->metatable); /* mark its metatable */ + for (i = 0; i < u->nuvalue; i++) + markvalue(g, &u->uv[i].uv); + return 1 + u->nuvalue; +} + + /* ** traverse one gray object, turning it to black (except for threads, ** which are always gray). @@ -612,6 +610,7 @@ static lu_mem propagatemark (global_State *g) { g->gray = *getgclist(o); /* remove from 'gray' list */ switch (o->tt) { case LUA_TTABLE: return traversetable(g, gco2t(o)); + case LUA_TUSERDATA: return traverseudata(g, gco2u(o)); case LUA_TLCL: return traverseLclosure(g, gco2lcl(o)); case LUA_TCCL: return traverseCclosure(g, gco2ccl(o)); case LUA_TPROTO: return traverseproto(g, gco2p(o)); @@ -742,9 +741,11 @@ static void freeobj (lua_State *L, GCObject *o) { case LUA_TTHREAD: luaE_freethread(L, gco2th(o)); break; - case LUA_TUSERDATA: - luaM_freemem(L, o, sizeudata(gco2u(o))); + case LUA_TUSERDATA: { + Udata *u = gco2u(o); + luaM_freemem(L, o, sizeudata(u->nuvalue, u->len)); break; + } case LUA_TSHRSTR: luaS_remove(L, gco2ts(o)); /* remove it from hash table */ luaM_freemem(L, o, sizelstring(gco2ts(o)->shrlen)); @@ -1065,7 +1066,7 @@ static GCObject **correctgraylist (GCObject **p) { GCObject *curr; while ((curr = *p) != NULL) { switch (curr->tt) { - case LUA_TTABLE: { + case LUA_TTABLE: case LUA_TUSERDATA: { GCObject **next = getgclist(curr); if (getage(curr) == G_TOUCHED1) { /* touched in this cycle? */ lua_assert(isgray(curr)); diff --git a/lobject.h b/lobject.h index 01bd39fa16..03396f11cc 100644 --- a/lobject.h +++ b/lobject.h @@ -1,5 +1,5 @@ /* -** $Id: lobject.h,v 2.132 2018/01/28 12:07:53 roberto Exp roberto $ +** $Id: lobject.h,v 2.133 2018/01/28 15:13:26 roberto Exp roberto $ ** Type definitions for Lua objects ** See Copyright Notice in lua.h */ @@ -365,46 +365,52 @@ typedef union UTString { /* -** Header for userdata; memory area follows the end of this structure -** (aligned according to 'UUdata'; see next). +** {================================================================== +** Userdata +** =================================================================== +*/ + +/* Ensures that addresses after this type are always fully aligned. */ +typedef union UValue { + TValue uv; + LUAI_MAXALIGN; /* ensures maximum alignment for udata bytes */ +} UValue; + + +/* +** Header for userdata; memory area follows the end of this structure. */ typedef struct Udata { CommonHeader; - lu_byte ttuv_; /* user value's tag */ - struct Table *metatable; + unsigned short nuvalue; /* number of user values */ size_t len; /* number of bytes */ - union Value user_; /* user value */ + struct Table *metatable; + GCObject *gclist; + UValue uv[1]; /* user values */ } Udata; -/* -** Ensures that address after this type is always fully aligned. -*/ -typedef union UUdata { - LUAI_MAXALIGN; /* ensures maximum alignment for 'local' udata */ - Udata uv; -} UUdata; - +/* computes the offset of the memory area of a userdata */ +#define udatamemoffset(nuv) (sizeof(Udata) + (sizeof(UValue) * ((nuv) - 1))) /* -** Get the address of memory block inside 'Udata'. -** (Access to 'ttuv_' ensures that value is really a 'Udata'.) +** Get the address of the memory block inside 'Udata'. */ -#define getudatamem(u) \ - check_exp(sizeof((u)->ttuv_), (cast_charp(u) + sizeof(UUdata))) +#define getudatamem(u) (cast_charp(u) + udatamemoffset((u)->nuvalue)) -#define setuservalue(L,u,o) \ - { const TValue *io=(o); Udata *iu = (u); \ - iu->user_ = io->value_; iu->ttuv_ = rttype(io); \ - checkliveness(L,io); } +/* computes the size of a userdata */ +#define sizeudata(nuv,nb) (udatamemoffset(nuv) + (nb)) +/* }================================================================== */ -#define getuservalue(L,u,o) \ - { TValue *io=(o); const Udata *iu = (u); \ - io->value_ = iu->user_; settt_(io, iu->ttuv_); \ - checkliveness(L,io); } +/* +** {================================================================== +** Prototypes +** =================================================================== +*/ + /* ** Description of an upvalue for function prototypes */ @@ -471,6 +477,8 @@ typedef struct Proto { GCObject *gclist; } Proto; +/* }================================================================== */ + /* diff --git a/lstring.c b/lstring.c index fbc3e02d17..29a082129f 100644 --- a/lstring.c +++ b/lstring.c @@ -1,5 +1,5 @@ /* -** $Id: lstring.c,v 2.63 2018/01/28 15:13:26 roberto Exp roberto $ +** $Id: lstring.c,v 2.64 2018/02/15 18:06:24 roberto Exp roberto $ ** String table (keeps all strings handled by Lua) ** See Copyright Notice in lua.h */ @@ -265,16 +265,19 @@ TString *luaS_new (lua_State *L, const char *str) { } -Udata *luaS_newudata (lua_State *L, size_t s) { +Udata *luaS_newudata (lua_State *L, size_t s, int nuvalue) { Udata *u; + int i; GCObject *o; - if (s > MAX_SIZE - sizeof(Udata)) + if (s > MAX_SIZE - udatamemoffset(nuvalue)) luaM_toobig(L); - o = luaC_newobj(L, LUA_TUSERDATA, sizeludata(s)); + o = luaC_newobj(L, LUA_TUSERDATA, sizeudata(nuvalue, s)); u = gco2u(o); u->len = s; + u->nuvalue = nuvalue; u->metatable = NULL; - setuservalue(L, u, luaO_nilobject); + for (i = 0; i < nuvalue; i++) + setnilvalue(&u->uv[i].uv); return u; } diff --git a/lstring.h b/lstring.h index a994fe1e5b..2be5a3ff0b 100644 --- a/lstring.h +++ b/lstring.h @@ -1,5 +1,5 @@ /* -** $Id: lstring.h,v 1.62 2017/07/27 13:50:16 roberto Exp roberto $ +** $Id: lstring.h,v 1.63 2017/11/23 19:29:04 roberto Exp roberto $ ** String table (keep all strings handled by Lua) ** See Copyright Notice in lua.h */ @@ -21,9 +21,6 @@ #define sizelstring(l) (sizeof(union UTString) + ((l) + 1) * sizeof(char)) -#define sizeludata(l) (sizeof(union UUdata) + (l)) -#define sizeudata(u) sizeludata((u)->len) - #define luaS_newliteral(L, s) (luaS_newlstr(L, "" s, \ (sizeof(s)/sizeof(char))-1)) @@ -47,7 +44,7 @@ LUAI_FUNC void luaS_resize (lua_State *L, int newsize); LUAI_FUNC void luaS_clearcache (global_State *g); LUAI_FUNC void luaS_init (lua_State *L); LUAI_FUNC void luaS_remove (lua_State *L, TString *ts); -LUAI_FUNC Udata *luaS_newudata (lua_State *L, size_t s); +LUAI_FUNC Udata *luaS_newudata (lua_State *L, size_t s, int nuvalue); LUAI_FUNC TString *luaS_newlstr (lua_State *L, const char *str, size_t l); LUAI_FUNC TString *luaS_new (lua_State *L, const char *str); LUAI_FUNC TString *luaS_createlngstrobj (lua_State *L, size_t l); diff --git a/ltests.c b/ltests.c index 24753d5fa6..97d1abd4f2 100644 --- a/ltests.c +++ b/ltests.c @@ -1,5 +1,5 @@ /* -** $Id: ltests.c,v 2.239 2018/01/09 11:24:12 roberto Exp roberto $ +** $Id: ltests.c,v 2.240 2018/01/28 15:13:26 roberto Exp roberto $ ** Internal Module for Debugging of the Lua Implementation ** See Copyright Notice in lua.h */ @@ -265,6 +265,15 @@ static void checktable (global_State *g, Table *h) { } +static void checkudata (global_State *g, Udata *u) { + int i; + GCObject *hgc = obj2gco(u); + checkobjref(g, hgc, u->metatable); + for (i = 0; i < u->nuvalue; i++) + checkvalref(g, hgc, &u->uv[i].uv); +} + + /* ** All marks are conditional because a GC may happen while the ** prototype is still being created @@ -287,7 +296,6 @@ static void checkproto (global_State *g, Proto *f) { } - static void checkCclosure (global_State *g, CClosure *cl) { GCObject *clgc = obj2gco(cl); int i; @@ -344,11 +352,7 @@ static void checkstack (global_State *g, lua_State *L1) { static void checkrefs (global_State *g, GCObject *o) { switch (o->tt) { case LUA_TUSERDATA: { - TValue uservalue; - Table *mt = gco2u(o)->metatable; - checkobjref(g, o, mt); - getuservalue(g->mainthread, gco2u(o), &uservalue); - checkvalref(g, o, &uservalue); + checkudata(g, gco2u(o)); break; } case LUA_TUPVAL: { @@ -728,7 +732,7 @@ static int gc_color (lua_State *L) { GCObject *obj = gcvalue(o); lua_pushstring(L, isdead(G(L), obj) ? "dead" : iswhite(obj) ? "white" : - isblack(obj) ? "black" : "grey"); + isblack(obj) ? "black" : "gray"); } return 1; } @@ -919,8 +923,9 @@ static int upvalue (lua_State *L) { static int newuserdata (lua_State *L) { - size_t size = cast_sizet(luaL_checkinteger(L, 1)); - char *p = cast_charp(lua_newuserdata(L, size)); + size_t size = cast_sizet(luaL_optinteger(L, 1, 0)); + int nuv = luaL_optinteger(L, 2, 0); + char *p = cast_charp(lua_newuserdatauv(L, size, nuv)); while (size--) *p++ = '\0'; return 1; } diff --git a/lua.h b/lua.h index d6277b4e91..8fb2ccbe44 100644 --- a/lua.h +++ b/lua.h @@ -1,5 +1,5 @@ /* -** $Id: lua.h,v 1.339 2017/11/07 13:25:26 roberto Exp roberto $ +** $Id: lua.h,v 1.340 2018/02/17 19:29:29 roberto Exp roberto $ ** Lua - A Scripting Language ** Lua.org, PUC-Rio, Brazil (http://www.lua.org) ** See Copyright Notice at the end of this file @@ -247,9 +247,9 @@ LUA_API int (lua_rawgeti) (lua_State *L, int idx, lua_Integer n); LUA_API int (lua_rawgetp) (lua_State *L, int idx, const void *p); LUA_API void (lua_createtable) (lua_State *L, int narr, int nrec); -LUA_API void *(lua_newuserdata) (lua_State *L, size_t sz); +LUA_API void *(lua_newuserdatauv) (lua_State *L, size_t sz, int nuvalue); LUA_API int (lua_getmetatable) (lua_State *L, int objindex); -LUA_API int (lua_getuservalue) (lua_State *L, int idx); +LUA_API int (lua_getiuservalue) (lua_State *L, int idx, int n); /* @@ -263,7 +263,7 @@ LUA_API void (lua_rawset) (lua_State *L, int idx); LUA_API void (lua_rawseti) (lua_State *L, int idx, lua_Integer n); LUA_API void (lua_rawsetp) (lua_State *L, int idx, const void *p); LUA_API int (lua_setmetatable) (lua_State *L, int objindex); -LUA_API void (lua_setuservalue) (lua_State *L, int idx); +LUA_API int (lua_setiuservalue) (lua_State *L, int idx, int n); /* @@ -380,7 +380,7 @@ LUA_API void (lua_setallocf) (lua_State *L, lua_Alloc f, void *ud); /* ** {============================================================== -** compatibility macros for unsigned conversions +** compatibility macros ** =============================================================== */ #if defined(LUA_COMPAT_APIINTCASTS) @@ -390,6 +390,11 @@ LUA_API void (lua_setallocf) (lua_State *L, lua_Alloc f, void *ud); #define lua_tounsigned(L,i) lua_tounsignedx(L,(i),NULL) #endif + +#define lua_newuserdata(L,s) lua_newuserdatauv(L,s,1) +#define lua_getuservalue(L,idx) lua_getiuservalue(L,idx,1) +#define lua_setuservalue(L,idx) lua_setiuservalue(L,idx,1) + /* }============================================================== */ /* From 6353d619a594b5dfbef5dad10af9d4c051878466 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 20 Feb 2018 17:52:50 -0300 Subject: [PATCH 0214/1145] detail (comment) --- luaconf.h | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/luaconf.h b/luaconf.h index 888e402b33..5ed062e243 100644 --- a/luaconf.h +++ b/luaconf.h @@ -1,5 +1,5 @@ /* -** $Id: luaconf.h,v 1.262 2017/12/07 18:53:33 roberto Exp roberto $ +** $Id: luaconf.h,v 1.263 2017/12/30 20:46:18 roberto Exp roberto $ ** Configuration file for Lua ** See Copyright Notice in lua.h */ @@ -437,12 +437,13 @@ l_sprintf((s), sz, LUA_NUMBER_FMT, (LUAI_UACNUMBER)(n)) /* -@@ lua_numbertointeger converts a float number to an integer, or -** returns 0 if float is not within the range of a lua_Integer. -** (The range comparisons are tricky because of rounding. The tests -** here assume a two-complement representation, where MININTEGER always -** has an exact representation as a float; MAXINTEGER may not have one, -** and therefore its conversion to float may have an ill-defined value.) +@@ lua_numbertointeger converts a float number with an integral value +** to an integer, or returns 0 if float is not within the range of +** a lua_Integer. (The range comparisons are tricky because of +** rounding. The tests here assume a two-complement representation, +** where MININTEGER always has an exact representation as a float; +** MAXINTEGER may not have one, and therefore its conversion to float +** may have an ill-defined value.) */ #define lua_numbertointeger(n,p) \ ((n) >= (LUA_NUMBER)(LUA_MININTEGER) && \ From 465b47489910c8660f7d8f9869ffacdbb2d5d292 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 21 Feb 2018 09:54:26 -0300 Subject: [PATCH 0215/1145] small reorganization of 'luaV_flttointeger'/'luaV_tointeger' --- lcode.c | 6 ++---- ltable.c | 9 +++++---- lvm.c | 48 +++++++++++++++++++++++++++--------------------- lvm.h | 10 +++++----- 4 files changed, 39 insertions(+), 34 deletions(-) diff --git a/lcode.c b/lcode.c index e280857191..c182794e93 100644 --- a/lcode.c +++ b/lcode.c @@ -1,5 +1,5 @@ /* -** $Id: lcode.c,v 2.154 2018/02/15 15:34:29 roberto Exp roberto $ +** $Id: lcode.c,v 2.155 2018/02/17 19:20:00 roberto Exp roberto $ ** Code generator for Lua ** See Copyright Notice in lua.h */ @@ -605,9 +605,7 @@ void luaK_int (FuncState *fs, int reg, lua_Integer i) { static int floatI (lua_Number f, lua_Integer *fi) { - TValue v; - setfltvalue(&v, f); - return (luaV_flttointeger(&v, fi, 0) && fitsBx(*fi)); + return (luaV_flttointeger(f, fi, 0) && fitsBx(*fi)); } diff --git a/ltable.c b/ltable.c index dbe5e2ed57..5378e31a86 100644 --- a/ltable.c +++ b/ltable.c @@ -1,5 +1,5 @@ /* -** $Id: ltable.c,v 2.131 2018/01/28 15:13:26 roberto Exp roberto $ +** $Id: ltable.c,v 2.132 2018/02/19 20:06:56 roberto Exp roberto $ ** Lua tables (hash) ** See Copyright Notice in lua.h */ @@ -559,12 +559,13 @@ TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) { TValue aux; if (ttisnil(key)) luaG_runerror(L, "table index is nil"); else if (ttisfloat(key)) { + lua_Number f = fltvalue(key); lua_Integer k; - if (luaV_flttointeger(key, &k, 0)) { /* does index fit in an integer? */ + if (luaV_flttointeger(f, &k, 0)) { /* does key fit in an integer? */ setivalue(&aux, k); key = &aux; /* insert it as an integer */ } - else if (luai_numisnan(fltvalue(key))) + else if (luai_numisnan(f)) luaG_runerror(L, "table index is NaN"); } mp = mainpositionTV(t, key); @@ -669,7 +670,7 @@ const TValue *luaH_get (Table *t, const TValue *key) { case LUA_TNIL: return luaO_nilobject; case LUA_TNUMFLT: { lua_Integer k; - if (luaV_flttointeger(key, &k, 0)) /* index is an integral? */ + if (luaV_flttointeger(fltvalue(key), &k, 0)) /* index is an integral? */ return luaH_getint(t, k); /* use specialized version */ /* else... */ } /* FALLTHROUGH */ diff --git a/lvm.c b/lvm.c index 0389352fb8..a1b750dc80 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.341 2018/02/17 19:20:00 roberto Exp roberto $ +** $Id: lvm.c,v 2.342 2018/02/19 20:06:56 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -90,36 +90,42 @@ int luaV_tonumber_ (const TValue *obj, lua_Number *n) { ** mode == 1: takes the floor of the number ** mode == 2: takes the ceil of the number */ -int luaV_flttointeger (const TValue *obj, lua_Integer *p, int mode) { - if (!ttisfloat(obj)) - return 0; - else { - lua_Number n = fltvalue(obj); - lua_Number f = l_floor(n); - if (n != f) { /* not an integral value? */ - if (mode == 0) return 0; /* fails if mode demands integral value */ - else if (mode > 1) /* needs ceil? */ - f += 1; /* convert floor to ceil (remember: n != f) */ - } - return lua_numbertointeger(f, p); +int luaV_flttointeger (lua_Number n, lua_Integer *p, int mode) { + lua_Number f = l_floor(n); + if (n != f) { /* not an integral value? */ + if (mode == 0) return 0; /* fails if mode demands integral value */ + else if (mode > 1) /* needs ceil? */ + f += 1; /* convert floor to ceil (remember: n != f) */ } + return lua_numbertointeger(f, p); } /* -** try to convert a value to an integer. ("Fast track" is handled -** by macro 'tointeger'.) +** try to convert a value to an integer, rounding according to 'mode', +** without string coercion. +** ("Fast track" handled by macro 'tointegerns'.) */ -int luaV_tointeger (const TValue *obj, lua_Integer *p, int mode) { - TValue v; - if (cvt2num(obj) && luaO_str2num(svalue(obj), &v) == vslen(obj) + 1) - obj = &v; /* change string to its corresponding number */ - if (ttisinteger(obj)) { +int luaV_tointegerns (const TValue *obj, lua_Integer *p, int mode) { + if (ttisfloat(obj)) + return luaV_flttointeger(fltvalue(obj), p, mode); + else if (ttisinteger(obj)) { *p = ivalue(obj); return 1; } else - return luaV_flttointeger(obj, p, mode); + return 0; +} + + +/* +** try to convert a value to an integer. +*/ +int luaV_tointeger (const TValue *obj, lua_Integer *p, int mode) { + TValue v; + if (cvt2num(obj) && luaO_str2num(svalue(obj), &v) == vslen(obj) + 1) + obj = &v; /* change string to its corresponding number */ + return luaV_tointegerns(obj, p, mode); } diff --git a/lvm.h b/lvm.h index 2cc4130aa0..cbf7e92264 100644 --- a/lvm.h +++ b/lvm.h @@ -1,5 +1,5 @@ /* -** $Id: lvm.h,v 2.48 2017/11/29 13:02:17 roberto Exp roberto $ +** $Id: lvm.h,v 2.49 2018/02/19 20:06:56 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -50,13 +50,12 @@ /* convert an object to an integer (including string coercion) */ #define tointeger(o,i) \ - (ttisinteger(o) ? (*(i) = ivalue(o), 1) : luaV_tointeger(o,i,LUA_FLOORN2I)) + (ttisinteger(o) ? (*(i) = ivalue(o), 1) : luaV_tointeger(o,i,LUA_FLOORN2I)) /* convert an object to an integer (without string coercion) */ #define tointegerns(o,i) \ - (ttisinteger(o) ? (*(i) = ivalue(o), 1) \ - : luaV_flttointeger(o,i,LUA_FLOORN2I)) + (ttisinteger(o) ? (*(i) = ivalue(o), 1) : luaV_tointegerns(o,i,LUA_FLOORN2I)) #define intop(op,v1,v2) l_castU2S(l_castS2U(v1) op l_castS2U(v2)) @@ -106,7 +105,8 @@ LUAI_FUNC int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r); LUAI_FUNC int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r); LUAI_FUNC int luaV_tonumber_ (const TValue *obj, lua_Number *n); LUAI_FUNC int luaV_tointeger (const TValue *obj, lua_Integer *p, int mode); -LUAI_FUNC int luaV_flttointeger (const TValue *obj, lua_Integer *p, int mode); +LUAI_FUNC int luaV_tointegerns (const TValue *obj, lua_Integer *p, int mode); +LUAI_FUNC int luaV_flttointeger (lua_Number n, lua_Integer *p, int mode); LUAI_FUNC void luaV_finishget (lua_State *L, const TValue *t, TValue *key, StkId val, const TValue *slot); LUAI_FUNC void luaV_finishset (lua_State *L, const TValue *t, TValue *key, From 06865aa01d1a8bfcf9f2f4b5a71e37a2561eeea6 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 21 Feb 2018 10:47:03 -0300 Subject: [PATCH 0216/1145] simpler implementation for 'LTintfloat'/'LEintfloat' + 'LTfloatint'/'LEfloatint' --- lvm.c | 116 +++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 71 insertions(+), 45 deletions(-) diff --git a/lvm.c b/lvm.c index a1b750dc80..76ce26364e 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.342 2018/02/19 20:06:56 roberto Exp roberto $ +** $Id: lvm.c,v 2.343 2018/02/21 12:54:26 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -31,17 +31,14 @@ #include "lvm.h" -/* limit for table tag-method chains (to avoid loops) */ +/* limit for table tag-method chains (to avoid infinite loops) */ #define MAXTAGLOOP 2000 /* ** 'l_intfitsf' checks whether a given integer can be converted to a -** float without rounding. Used in comparisons. Left undefined if -** all integers fit in a float precisely. +** float without rounding. Used in comparisons. */ -#if !defined(l_intfitsf) - /* number of bits in the mantissa of a float */ #define NBM (l_mathlim(MANT_DIG)) @@ -58,7 +55,9 @@ #define l_intfitsf(i) \ (-((lua_Integer)1 << NBM) <= (i) && (i) <= ((lua_Integer)1 << NBM)) -#endif +#else /* all integers fit in a float precisely */ + +#define l_intfitsf(i) 1 #endif @@ -157,7 +156,7 @@ static int forlimit (const TValue *obj, lua_Integer *p, lua_Integer step, *p = LUA_MAXINTEGER; if (step < 0) *stopnow = 1; } - else { /* float is smaller than min integer */ + else { /* float is less than min integer */ *p = LUA_MININTEGER; if (step >= 0) *stopnow = 1; } @@ -255,8 +254,8 @@ void luaV_finishset (lua_State *L, const TValue *t, TValue *key, /* -** Compare two strings 'ls' x 'rs', returning an integer smaller-equal- -** -larger than zero if 'ls' is smaller-equal-larger than 'rs'. +** Compare two strings 'ls' x 'rs', returning an integer less-equal- +** -greater than zero if 'ls' is less-equal-greater than 'rs'. ** The code is a little tricky because it allows '\0' in the strings ** and it uses 'strcoll' (to respect locales) for each segments ** of the strings. @@ -275,7 +274,7 @@ static int l_strcmp (const TString *ls, const TString *rs) { if (len == lr) /* 'rs' is finished? */ return (len == ll) ? 0 : 1; /* check 'ls' */ else if (len == ll) /* 'ls' is finished? */ - return -1; /* 'ls' is smaller than 'rs' ('rs' is not finished) */ + return -1; /* 'ls' is less than 'rs' ('rs' is not finished) */ /* both strings longer than 'len'; go on comparing after the '\0' */ len++; l += len; ll -= len; r += len; lr -= len; @@ -287,25 +286,24 @@ static int l_strcmp (const TString *ls, const TString *rs) { /* ** Check whether integer 'i' is less than float 'f'. If 'i' has an ** exact representation as a float ('l_intfitsf'), compare numbers as -** floats. Otherwise, if 'f' is outside the range for integers, result -** is trivial. Otherwise, compare them as integers. (When 'i' has no -** float representation, either 'f' is "far away" from 'i' or 'f' has -** no precision left for a fractional part; either way, how 'f' is -** truncated is irrelevant.) When 'f' is NaN, comparisons must result -** in false. +** floats. Otherwise, use the equivalence 'i < f <=> i < ceil(f)'. +** If 'ceil(f)' is out of integer range, either 'f' is greater than +** all integers or less than all integers. +** (The test with 'l_intfitsf' is only for performance; the else +** case is correct for all values, but it is slow due to the conversion +** from float to int.) +** When 'f' is NaN, comparisons must result in false. */ static int LTintfloat (lua_Integer i, lua_Number f) { -#if defined(l_intfitsf) - if (!l_intfitsf(i)) { - if (f >= -cast_num(LUA_MININTEGER)) /* -minint == maxint + 1 */ - return 1; /* f >= maxint + 1 > i */ - else if (f > cast_num(LUA_MININTEGER)) /* minint < f <= maxint ? */ - return (i < cast(lua_Integer, f)); /* compare them as integers */ - else /* f <= minint <= i (or 'f' is NaN) --> not(i < f) */ - return 0; + if (l_intfitsf(i)) + return luai_numlt(cast_num(i), f); /* compare them as floats */ + else { /* i < f <=> i < ceil(f) */ + lua_Integer fi; + if (luaV_flttointeger(f, &fi, 2)) /* fi = ceil(f) */ + return i < fi; /* compare them as integers */ + else /* 'f' is either greater or less than all integers */ + return f > 0; /* greater? */ } -#endif - return luai_numlt(cast_num(i), f); /* compare them as floats */ } @@ -314,17 +312,49 @@ static int LTintfloat (lua_Integer i, lua_Number f) { ** See comments on previous function. */ static int LEintfloat (lua_Integer i, lua_Number f) { -#if defined(l_intfitsf) - if (!l_intfitsf(i)) { - if (f >= -cast_num(LUA_MININTEGER)) /* -minint == maxint + 1 */ - return 1; /* f >= maxint + 1 > i */ - else if (f >= cast_num(LUA_MININTEGER)) /* minint <= f <= maxint ? */ - return (i <= cast(lua_Integer, f)); /* compare them as integers */ - else /* f < minint <= i (or 'f' is NaN) --> not(i <= f) */ - return 0; + if (l_intfitsf(i)) + return luai_numle(cast_num(i), f); /* compare them as floats */ + else { /* i <= f <=> i <= floor(f) */ + lua_Integer fi; + if (luaV_flttointeger(f, &fi, 1)) /* fi = floor(f) */ + return i <= fi; /* compare them as integers */ + else /* 'f' is either greater or less than all integers */ + return f > 0; /* greater? */ + } +} + + +/* +** Check whether float 'f' is less than integer 'i'. +** See comments on previous function. +*/ +static int LTfloatint (lua_Number f, lua_Integer i) { + if (l_intfitsf(i)) + return luai_numlt(f, cast_num(i)); /* compare them as floats */ + else { /* f < i <=> floor(f) < i */ + lua_Integer fi; + if (luaV_flttointeger(f, &fi, 1)) /* fi = floor(f) */ + return fi < i; /* compare them as integers */ + else /* 'f' is either greater or less than all integers */ + return f < 0; /* less? */ + } +} + + +/* +** Check whether float 'f' is less than or equal to integer 'i'. +** See comments on previous function. +*/ +static int LEfloatint (lua_Number f, lua_Integer i) { + if (l_intfitsf(i)) + return luai_numle(f, cast_num(i)); /* compare them as floats */ + else { /* f <= i <=> ceil(f) <= i */ + lua_Integer fi; + if (luaV_flttointeger(f, &fi, 2)) /* fi = ceil(f) */ + return fi <= i; /* compare them as integers */ + else /* 'f' is either greater or less than all integers */ + return f < 0; /* less? */ } -#endif - return luai_numle(cast_num(i), f); /* compare them as floats */ } @@ -344,10 +374,8 @@ static int LTnum (const TValue *l, const TValue *r) { lua_Number lf = fltvalue(l); /* 'l' must be float */ if (ttisfloat(r)) return luai_numlt(lf, fltvalue(r)); /* both are float */ - else if (luai_numisnan(lf)) /* 'r' is int and 'l' is float */ - return 0; /* NaN < i is always false */ - else /* without NaN, (l < r) <--> not(r <= l) */ - return !LEintfloat(ivalue(r), lf); /* not (r <= l) ? */ + else /* 'l' is float and 'r' is int */ + return LTfloatint(lf, ivalue(r)); } } @@ -368,10 +396,8 @@ static int LEnum (const TValue *l, const TValue *r) { lua_Number lf = fltvalue(l); /* 'l' must be float */ if (ttisfloat(r)) return luai_numle(lf, fltvalue(r)); /* both are float */ - else if (luai_numisnan(lf)) /* 'r' is int and 'l' is float */ - return 0; /* NaN <= i is always false */ - else /* without NaN, (l <= r) <--> not(r < l) */ - return !LTintfloat(ivalue(r), lf); /* not (r < l) ? */ + else /* 'l' is float and 'r' is int */ + return LEfloatint(lf, ivalue(r)); } } From c67603fafba7f982e92eb870ff4da6c6433c2a85 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 21 Feb 2018 10:48:44 -0300 Subject: [PATCH 0217/1145] using new 'lua_newuserdatauv' instead of 'lua_newuserdata' --- lauxlib.c | 4 ++-- liolib.c | 4 ++-- lstrlib.c | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lauxlib.c b/lauxlib.c index 09d89d8040..febd3eb17a 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -1,5 +1,5 @@ /* -** $Id: lauxlib.c,v 1.291 2017/06/27 18:32:49 roberto Exp roberto $ +** $Id: lauxlib.c,v 1.292 2018/01/29 19:13:27 roberto Exp roberto $ ** Auxiliary functions for building Lua libraries ** See Copyright Notice in lua.h */ @@ -480,7 +480,7 @@ static int boxgc (lua_State *L) { static void *newbox (lua_State *L, size_t newsize) { - UBox *box = (UBox *)lua_newuserdata(L, sizeof(UBox)); + UBox *box = (UBox *)lua_newuserdatauv(L, sizeof(UBox), 0); box->box = NULL; box->bsize = 0; if (luaL_newmetatable(L, "_UBOX*")) { /* creating metatable? */ diff --git a/liolib.c b/liolib.c index f3c914d7f2..7dc1dab9ae 100644 --- a/liolib.c +++ b/liolib.c @@ -1,5 +1,5 @@ /* -** $Id: liolib.c,v 2.153 2017/11/16 13:19:06 roberto Exp roberto $ +** $Id: liolib.c,v 2.154 2017/11/16 16:28:36 roberto Exp roberto $ ** Standard I/O (and system) library ** See Copyright Notice in lua.h */ @@ -186,7 +186,7 @@ static FILE *tofile (lua_State *L) { ** handle is in a consistent state. */ static LStream *newprefile (lua_State *L) { - LStream *p = (LStream *)lua_newuserdata(L, sizeof(LStream)); + LStream *p = (LStream *)lua_newuserdatauv(L, sizeof(LStream), 0); p->closef = NULL; /* mark file handle as 'closed' */ luaL_setmetatable(L, LUA_FILEHANDLE); return p; diff --git a/lstrlib.c b/lstrlib.c index 84b6e4eb2e..da9ef9110c 100644 --- a/lstrlib.c +++ b/lstrlib.c @@ -1,5 +1,5 @@ /* -** $Id: lstrlib.c,v 1.259 2017/11/16 13:19:06 roberto Exp roberto $ +** $Id: lstrlib.c,v 1.260 2017/11/23 19:29:04 roberto Exp roberto $ ** Standard library for string operations and pattern-matching ** See Copyright Notice in lua.h */ @@ -837,7 +837,7 @@ static int gmatch (lua_State *L) { const char *p = luaL_checklstring(L, 2, &lp); GMatchState *gm; lua_settop(L, 2); /* keep them on closure to avoid being collected */ - gm = (GMatchState *)lua_newuserdata(L, sizeof(GMatchState)); + gm = (GMatchState *)lua_newuserdatauv(L, sizeof(GMatchState), 0); prepstate(&gm->ms, L, s, ls, p, lp); gm->src = s; gm->p = p; gm->lastmatch = NULL; lua_pushcclosure(L, gmatch_aux, 3); From 212095a601ee68e99065b553f7b6bc216056d82e Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 21 Feb 2018 12:49:32 -0300 Subject: [PATCH 0218/1145] new opcodes OP_GTI/OP_GEI --- lcode.c | 30 ++++++++++++------------------ lopcodes.c | 6 +++++- lopcodes.h | 9 +++------ ltm.c | 5 ++--- lvm.c | 45 ++++++++++++++++++++++++++++----------------- 5 files changed, 50 insertions(+), 45 deletions(-) diff --git a/lcode.c b/lcode.c index c182794e93..30bf13d813 100644 --- a/lcode.c +++ b/lcode.c @@ -1,5 +1,5 @@ /* -** $Id: lcode.c,v 2.155 2018/02/17 19:20:00 roberto Exp roberto $ +** $Id: lcode.c,v 2.156 2018/02/21 12:54:26 roberto Exp roberto $ ** Code generator for Lua ** See Copyright Notice in lua.h */ @@ -171,8 +171,8 @@ void luaK_ret (FuncState *fs, int first, int nret) { ** Code a "conditional jump", that is, a test or comparison opcode ** followed by a jump. Return jump position. */ -static int condjump (FuncState *fs, OpCode op, int A, int B, int C, int k) { - luaK_codeABCk(fs, op, A, B, C, k); +static int condjump (FuncState *fs, OpCode op, int A, int B, int k) { + luaK_codeABCk(fs, op, A, B, 0, k); return luaK_jump(fs); } @@ -979,13 +979,13 @@ static int jumponcond (FuncState *fs, expdesc *e, int cond) { Instruction ie = getinstruction(fs, e); if (GET_OPCODE(ie) == OP_NOT) { fs->pc--; /* remove previous OP_NOT */ - return condjump(fs, OP_TEST, GETARG_B(ie), 0, 0, !cond); + return condjump(fs, OP_TEST, GETARG_B(ie), 0, !cond); } /* else go through */ } discharge2anyreg(fs, e); freeexp(fs, e); - return condjump(fs, OP_TESTSET, NO_REG, e->u.info, 0, cond); + return condjump(fs, OP_TESTSET, NO_REG, e->u.info, cond); } @@ -1338,16 +1338,12 @@ static void codeshift (FuncState *fs, OpCode op, /* ** Emit code for order comparisons. -** When the first operand is an integral value in the proper range, -** change (A < B) to (!(B <= A)) and (A <= B) to (!(B < A)) so that -** it can use an immediate operand. In this case, C indicates this -** change, for cases that cannot assume a total order (NaN and -** metamethods). +** When the first operand A is an integral value in the proper range, +** change (A < B) to (B > A) and (A <= B) to (B >= A) so that +** it can use an immediate operand. */ static void codeorder (FuncState *fs, OpCode op, expdesc *e1, expdesc *e2) { int r1, r2; - int cond = 1; - int C = 0; lua_Integer im; if (isSCnumber(e2, &im)) { /* use immediate operand */ @@ -1356,19 +1352,17 @@ static void codeorder (FuncState *fs, OpCode op, expdesc *e1, expdesc *e2) { op = cast(OpCode, (op - OP_LT) + OP_LTI); } else if (isSCnumber(e1, &im)) { - /* transform (A < B) to (!(B <= A)) and (A <= B) to (!(B < A)) */ + /* transform (A < B) to (B > A) and (A <= B) to (B >= A) */ r1 = luaK_exp2anyreg(fs, e2); r2 = cast_int(im); - op = (op == OP_LT) ? OP_LEI : OP_LTI; - cond = 0; /* negate original test */ - C = 1; /* indication that it used the transformations */ + op = (op == OP_LT) ? OP_GTI : OP_GEI; } else { /* regular case, compare two registers */ r1 = luaK_exp2anyreg(fs, e1); r2 = luaK_exp2anyreg(fs, e2); } freeexps(fs, e1, e2); - e1->u.info = condjump(fs, op, r1, r2, C, cond); + e1->u.info = condjump(fs, op, r1, r2, 1); e1->k = VJMP; } @@ -1399,7 +1393,7 @@ static void codeeq (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) { r2 = luaK_exp2anyreg(fs, e2); } freeexps(fs, e1, e2); - e1->u.info = condjump(fs, op, r1, r2, 0, (opr == OPR_EQ)); + e1->u.info = condjump(fs, op, r1, r2, (opr == OPR_EQ)); e1->k = VJMP; } diff --git a/lopcodes.c b/lopcodes.c index aa3055be3e..79d77cc1d8 100644 --- a/lopcodes.c +++ b/lopcodes.c @@ -1,5 +1,5 @@ /* -** $Id: lopcodes.c,v 1.77 2018/02/09 15:16:06 roberto Exp roberto $ +** $Id: lopcodes.c,v 1.78 2018/02/15 15:34:29 roberto Exp roberto $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ @@ -75,6 +75,8 @@ LUAI_DDEF const char *const luaP_opnames[NUM_OPCODES+1] = { "EQI", "LTI", "LEI", + "GTI", + "GEI", "TEST", "TESTSET", "CALL", @@ -156,6 +158,8 @@ LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = { ,opmode(0, 0, 1, 0, iABC) /* OP_EQI */ ,opmode(0, 0, 1, 0, iABC) /* OP_LTI */ ,opmode(0, 0, 1, 0, iABC) /* OP_LEI */ + ,opmode(0, 0, 1, 0, iABC) /* OP_GTI */ + ,opmode(0, 0, 1, 0, iABC) /* OP_GEI */ ,opmode(0, 0, 1, 0, iABC) /* OP_TEST */ ,opmode(0, 0, 1, 1, iABC) /* OP_TESTSET */ ,opmode(1, 1, 0, 1, iABC) /* OP_CALL */ diff --git a/lopcodes.h b/lopcodes.h index b2e22c2786..be7359e951 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -1,5 +1,5 @@ /* -** $Id: lopcodes.h,v 1.187 2018/02/09 15:16:06 roberto Exp roberto $ +** $Id: lopcodes.h,v 1.188 2018/02/15 15:34:29 roberto Exp roberto $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ @@ -260,6 +260,8 @@ OP_EQK,/* A B if ((R(A) == K(B)) ~= k) then pc++ */ OP_EQI,/* A sB if ((R(A) == sB) ~= k) then pc++ */ OP_LTI,/* A sB if ((R(A) < sB) ~= k) then pc++ */ OP_LEI,/* A sB if ((R(A) <= sB) ~= k) then pc++ */ +OP_GTI,/* A sB if ((R(A) > sB) ~= k) then pc++ */ +OP_GEI,/* A sB if ((R(A) >= sB) ~= k) then pc++ */ OP_TEST,/* A if (not R(A) == k) then pc++ */ OP_TESTSET,/* A B if (not R(B) == k) then R(A) := R(B) else pc++ */ @@ -317,11 +319,6 @@ OP_EXTRAARG/* Ax extra (larger) argument for previous opcode */ (*) For comparisons, k specifies what condition the test should accept (true or false). - (*) For OP_LTI/OP_LEI, C indicates that the transformations - (A (!(B<=A)) or (A<=B) => (!(Btop - 1)); L->top--; if (ci->callstatus & CIST_LEQ) { /* "<=" using "<" instead? */ - lua_assert(op == OP_LE || - (op == OP_LTI && GETARG_C(inst)) || - (op == OP_LEI && !GETARG_C(inst))); ci->callstatus ^= CIST_LEQ; /* clear mark */ res = !res; /* negate result */ } lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_JMP); - if (GETARG_C(inst)) res = !res; if (res != GETARG_k(inst)) /* condition failed? */ ci->u.l.savedpc++; /* skip jump instruction */ break; @@ -1473,26 +1470,40 @@ void luaV_execute (lua_State *L, CallInfo *ci) { int im = GETARG_sB(i); if (ttisinteger(vra)) cond = (ivalue(vra) < im); - else if (ttisfloat(vra)) { - lua_Number f = fltvalue(vra); - cond = (!luai_numisnan(f)) ? luai_numlt(f, cast_num(im)) - : GETARG_C(i); /* NaN */ - } + else if (ttisfloat(vra)) + cond = luai_numlt(fltvalue(vra), cast_num(im)); else - Protect(cond = luaT_callorderiTM(L, vra, im, GETARG_C(i), TM_LT)); + Protect(cond = luaT_callorderiTM(L, vra, im, 0, TM_LT)); goto condjump; } vmcase(OP_LEI) { int im = GETARG_sB(i); if (ttisinteger(vra)) cond = (ivalue(vra) <= im); - else if (ttisfloat(vra)) { - lua_Number f = fltvalue(vra); - cond = (!luai_numisnan(f)) ? luai_numle(f, cast_num(im)) - : GETARG_C(i); /* NaN? */ - } + else if (ttisfloat(vra)) + cond = luai_numle(fltvalue(vra), cast_num(im)); + else + Protect(cond = luaT_callorderiTM(L, vra, im, 0, TM_LE)); + goto condjump; + } + vmcase(OP_GTI) { + int im = GETARG_sB(i); + if (ttisinteger(vra)) + cond = (im < ivalue(vra)); + else if (ttisfloat(vra)) + cond = luai_numlt(cast_num(im), fltvalue(vra)); + else + Protect(cond = luaT_callorderiTM(L, vra, im, 1, TM_LT)); + goto condjump; + } + vmcase(OP_GEI) { + int im = GETARG_sB(i); + if (ttisinteger(vra)) + cond = (im <= ivalue(vra)); + else if (ttisfloat(vra)) + cond = luai_numle(cast_num(im), fltvalue(vra)); else - Protect(cond = luaT_callorderiTM(L, vra, im, GETARG_C(i), TM_LE)); + Protect(cond = luaT_callorderiTM(L, vra, im, 1, TM_LE)); goto condjump; } vmcase(OP_TEST) { From c80c7a49fdbd5c6540bd49ef47925edd9eed8c99 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 21 Feb 2018 13:28:12 -0300 Subject: [PATCH 0219/1145] details (comments) --- lobject.h | 45 ++++++++++++++++++++++++++------------------- 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/lobject.h b/lobject.h index 03396f11cc..5d6abdd1e6 100644 --- a/lobject.h +++ b/lobject.h @@ -1,5 +1,5 @@ /* -** $Id: lobject.h,v 2.133 2018/01/28 15:13:26 roberto Exp roberto $ +** $Id: lobject.h,v 2.134 2018/02/20 16:52:50 roberto Exp roberto $ ** Type definitions for Lua objects ** See Copyright Notice in lua.h */ @@ -88,12 +88,6 @@ struct GCObject { - -/* -** Tagged Values. This is the basic representation of values in Lua, -** an actual value plus a tag with its type. -*/ - /* ** Union of all Lua values */ @@ -107,8 +101,12 @@ typedef union Value { } Value; -#define TValuefields Value value_; lu_byte tt_ +/* +** Tagged Values. This is the basic representation of values in Lua: +** an actual value plus a tag with its type. +*/ +#define TValuefields Value value_; lu_byte tt_ typedef struct TValue { TValuefields; @@ -301,14 +299,6 @@ typedef struct TValue { - -/* -** {====================================================== -** types and prototypes -** ======================================================= -*/ - - typedef union StackValue { TValue val; } StackValue; @@ -321,6 +311,12 @@ typedef StackValue *StkId; /* index to stack elements */ +/* +** {================================================================== +** Strings +** =================================================================== +*/ + /* ** Header for string value; string bytes follow the end of this structure ** (aligned according to 'UTString'; see next). @@ -363,6 +359,8 @@ typedef union UTString { /* get string length from 'TValue *o' */ #define vslen(o) tsslen(tsvalue(o)) +/* }================================================================== */ + /* ** {================================================================== @@ -404,7 +402,6 @@ typedef struct Udata { /* }================================================================== */ - /* ** {================================================================== ** Prototypes @@ -480,6 +477,11 @@ typedef struct Proto { /* }================================================================== */ +/* +** {================================================================== +** Closures +** =================================================================== +*/ /* ** Upvalues for Lua closures @@ -529,14 +531,17 @@ typedef union Closure { #define getproto(o) (clLvalue(o)->p) +/* }================================================================== */ + /* +** {================================================================== ** Tables +** =================================================================== */ - /* -** Nodes for Hash tables. A pack of two TValue's (key-value pairs) +** Nodes for Hash tables: A pack of two TValue's (key-value pairs) ** plus a 'next' field to link colliding entries. The distribution ** of the key's fields ('key_tt' and 'key_val') not forming a proper ** 'TValue' allows for a smaller size for 'Node' both in 4-byte @@ -609,6 +614,8 @@ typedef struct Table { */ #define setdeadkey(n) (keytt(n) = LUA_TTABLE, gckey(n) = NULL) +/* }================================================================== */ + /* From b03dddf9e406f29e5d8bfa06ccb6db1dfad6e405 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 21 Feb 2018 14:48:31 -0300 Subject: [PATCH 0220/1145] removed coercion string->number in bitwise operations (can be done with a Lua module) --- lstrlib.c | 60 +------------------------------------------------------ 1 file changed, 1 insertion(+), 59 deletions(-) diff --git a/lstrlib.c b/lstrlib.c index da9ef9110c..54692a6eb6 100644 --- a/lstrlib.c +++ b/lstrlib.c @@ -1,5 +1,5 @@ /* -** $Id: lstrlib.c,v 1.260 2017/11/23 19:29:04 roberto Exp roberto $ +** $Id: lstrlib.c,v 1.261 2018/02/21 13:48:44 roberto Exp roberto $ ** Standard library for string operations and pattern-matching ** See Copyright Notice in lua.h */ @@ -220,25 +220,6 @@ static int tonum (lua_State *L, int arg) { } -static int toint (lua_State *L, int arg) { - if (!tonum(L, arg)) - return 0; /* not coercible to a number */ - else if (lua_isinteger(L, arg)) - return 1; /* already an integer */ - else { /* a float */ - int ok; - lua_Integer n = lua_tointegerx(L, arg, &ok); - if (!ok) - return 0; - else { - lua_pop(L, 1); /* remove the float */ - lua_pushinteger(L, n); /* push an integer */ - return 1; - } - } -} - - static void trymt (lua_State *L, const char *mtname) { lua_settop(L, 2); /* back to the original arguments */ if (lua_type(L, 2) == LUA_TSTRING || !luaL_getmetafield(L, 2, mtname)) @@ -258,15 +239,6 @@ static int arith (lua_State *L, int op, const char *mtname) { } -static int bitwise (lua_State *L, int op, const char *mtname) { - if (toint(L, 1) && toint(L, 2)) - lua_arith(L, op); /* result will be on the top */ - else - trymt(L, mtname); - return 1; -} - - static int arith_add (lua_State *L) { return arith(L, LUA_OPADD, "__add"); } @@ -299,30 +271,6 @@ static int arith_unm (lua_State *L) { return arith(L, LUA_OPUNM, "__unm"); } -static int bitwise_band (lua_State *L) { - return bitwise(L, LUA_OPBAND, "__band"); -} - -static int bitwise_bor (lua_State *L) { - return bitwise(L, LUA_OPBOR, "__bor"); -} - -static int bitwise_bxor (lua_State *L) { - return bitwise(L, LUA_OPBXOR, "__bxor"); -} - -static int bitwise_shl (lua_State *L) { - return bitwise(L, LUA_OPSHL, "__shl"); -} - -static int bitwise_shr (lua_State *L) { - return bitwise(L, LUA_OPSHR, "__shr"); -} - -static int bitwise_bnot (lua_State *L) { - return bitwise(L, LUA_OPBNOT, "__bnot"); -} - static const luaL_Reg stringmetamethods[] = { {"__add", arith_add}, @@ -333,12 +281,6 @@ static const luaL_Reg stringmetamethods[] = { {"__div", arith_div}, {"__idiv", arith_idiv}, {"__unm", arith_unm}, - {"__band", bitwise_band}, - {"__bor", bitwise_bor}, - {"__bxor", bitwise_bxor}, - {"__shl", bitwise_shl}, - {"__shr", bitwise_shr}, - {"__bnot", bitwise_bnot}, {"__index", NULL}, /* placeholder */ {NULL, NULL} }; From c72ac048b989bea0f66f8532ab00a598da71e46d Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 21 Feb 2018 16:43:44 -0300 Subject: [PATCH 0221/1145] conditional jumps "deunified" (if a jump table is used, the unification may harm jump prediction.) --- lvm.c | 43 ++++++++++++++++++++++++++++--------------- 1 file changed, 28 insertions(+), 15 deletions(-) diff --git a/lvm.c b/lvm.c index f61069de0b..efde6fb211 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.344 2018/02/21 13:47:03 roberto Exp roberto $ +** $Id: lvm.c,v 2.345 2018/02/21 15:49:32 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -804,6 +804,14 @@ void luaV_finishOp (lua_State *L) { /* for test instructions, execute the jump instruction that follows it */ #define donextjump(ci) { i = *pc; dojump(ci, i, 1); } +/* +** do a conditional jump: skip next instruction if 'cond' is not what +** was expected (parameter 'k'), else do next instruction, which must +** be a jump. +*/ +#define docondjump() if (cond != GETARG_k(i)) pc++; else donextjump(ci); + + /* ** Correct global 'pc'. */ @@ -1428,7 +1436,8 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmcase(OP_EQ) { TValue *rb = vRB(i); Protect(cond = luaV_equalobj(L, vra, rb)); - goto condjump; + docondjump(); + vmbreak; } vmcase(OP_LT) { TValue *rb = vRB(i); @@ -1438,7 +1447,8 @@ void luaV_execute (lua_State *L, CallInfo *ci) { cond = LTnum(vra, rb); else Protect(cond = lessthanothers(L, vra, rb)); - goto condjump; + docondjump(); + vmbreak; } vmcase(OP_LE) { TValue *rb = vRB(i); @@ -1448,13 +1458,15 @@ void luaV_execute (lua_State *L, CallInfo *ci) { cond = LEnum(vra, rb); else Protect(cond = lessequalothers(L, vra, rb)); - goto condjump; + docondjump(); + vmbreak; } vmcase(OP_EQK) { TValue *rb = KB(i); /* basic types do not use '__eq'; we can use raw equality */ cond = luaV_equalobj(NULL, vra, rb); - goto condjump; + docondjump(); + vmbreak; } vmcase(OP_EQI) { int im = GETARG_sB(i); @@ -1464,7 +1476,8 @@ void luaV_execute (lua_State *L, CallInfo *ci) { cond = luai_numeq(fltvalue(vra), cast_num(im)); else cond = 0; /* other types cannot be equal to a number */ - goto condjump; + docondjump(); + vmbreak; } vmcase(OP_LTI) { int im = GETARG_sB(i); @@ -1474,7 +1487,8 @@ void luaV_execute (lua_State *L, CallInfo *ci) { cond = luai_numlt(fltvalue(vra), cast_num(im)); else Protect(cond = luaT_callorderiTM(L, vra, im, 0, TM_LT)); - goto condjump; + docondjump(); + vmbreak; } vmcase(OP_LEI) { int im = GETARG_sB(i); @@ -1484,7 +1498,8 @@ void luaV_execute (lua_State *L, CallInfo *ci) { cond = luai_numle(fltvalue(vra), cast_num(im)); else Protect(cond = luaT_callorderiTM(L, vra, im, 0, TM_LE)); - goto condjump; + docondjump(); + vmbreak; } vmcase(OP_GTI) { int im = GETARG_sB(i); @@ -1494,7 +1509,8 @@ void luaV_execute (lua_State *L, CallInfo *ci) { cond = luai_numlt(cast_num(im), fltvalue(vra)); else Protect(cond = luaT_callorderiTM(L, vra, im, 1, TM_LT)); - goto condjump; + docondjump(); + vmbreak; } vmcase(OP_GEI) { int im = GETARG_sB(i); @@ -1504,15 +1520,12 @@ void luaV_execute (lua_State *L, CallInfo *ci) { cond = luai_numle(cast_num(im), fltvalue(vra)); else Protect(cond = luaT_callorderiTM(L, vra, im, 1, TM_LE)); - goto condjump; + docondjump(); + vmbreak; } vmcase(OP_TEST) { cond = !l_isfalse(vra); - condjump: - if (cond != GETARG_k(i)) - pc++; /* skip next jump */ - else - donextjump(ci); + docondjump(); vmbreak; } vmcase(OP_TESTSET) { From 477ca2fe8ceaf79038972977915987adbef0e425 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 22 Feb 2018 14:28:10 -0300 Subject: [PATCH 0222/1145] some reorganization in 'lobject.h' (just moving stuff around) --- lgc.c | 7 +- lobject.h | 405 +++++++++++++++++++++++++++++------------------------- 2 files changed, 224 insertions(+), 188 deletions(-) diff --git a/lgc.c b/lgc.c index 3421bd8ae3..7cb3c18d37 100644 --- a/lgc.c +++ b/lgc.c @@ -1,5 +1,5 @@ /* -** $Id: lgc.c,v 2.248 2018/02/19 16:02:25 roberto Exp roberto $ +** $Id: lgc.c,v 2.248 2018/02/20 16:52:50 roberto Exp roberto $ ** Garbage Collector ** See Copyright Notice in lua.h */ @@ -79,6 +79,11 @@ #define checkconsistency(obj) \ lua_longassert(!iscollectable(obj) || righttt(obj)) +/* +** Protected access to objects in values +*/ +#define gcvalueN(o) (iscollectable(o) ? gcvalue(o) : NULL) + #define markvalue(g,o) { checkconsistency(o); \ if (valiswhite(o)) reallymarkobject(g,gcvalue(o)); } diff --git a/lobject.h b/lobject.h index 5d6abdd1e6..2b65da3658 100644 --- a/lobject.h +++ b/lobject.h @@ -1,5 +1,5 @@ /* -** $Id: lobject.h,v 2.134 2018/02/20 16:52:50 roberto Exp roberto $ +** $Id: lobject.h,v 2.135 2018/02/21 16:28:12 roberto Exp roberto $ ** Type definitions for Lua objects ** See Copyright Notice in lua.h */ @@ -36,63 +36,12 @@ */ -/* -** LUA_TFUNCTION variants: -** 0 - Lua function -** 1 - light C function -** 2 - regular C function (closure) -*/ - -/* Variant tags for functions */ -#define LUA_TLCL (LUA_TFUNCTION | (0 << 4)) /* Lua closure */ -#define LUA_TLCF (LUA_TFUNCTION | (1 << 4)) /* light C function */ -#define LUA_TCCL (LUA_TFUNCTION | (2 << 4)) /* C closure */ - - -/* Variant tags for strings */ -#define LUA_TSHRSTR (LUA_TSTRING | (0 << 4)) /* short strings */ -#define LUA_TLNGSTR (LUA_TSTRING | (1 << 4)) /* long strings */ - - -/* Variant tags for numbers */ -#define LUA_TNUMFLT (LUA_TNUMBER | (0 << 4)) /* float numbers */ -#define LUA_TNUMINT (LUA_TNUMBER | (1 << 4)) /* integer numbers */ - - -/* Bit mark for collectable types */ -#define BIT_ISCOLLECTABLE (1 << 6) - -/* mark a tag as collectable */ -#define ctb(t) ((t) | BIT_ISCOLLECTABLE) - - -/* -** Common type for all collectable objects -*/ -typedef struct GCObject GCObject; - - -/* -** Common Header for all collectable objects (in macro form, to be -** included in other objects) -*/ -#define CommonHeader GCObject *next; lu_byte tt; lu_byte marked - - -/* -** Common type has only the common header -*/ -struct GCObject { - CommonHeader; -}; - - /* ** Union of all Lua values */ typedef union Value { - GCObject *gc; /* collectable objects */ + struct GCObject *gc; /* collectable objects */ void *p; /* light userdata */ int b; /* booleans */ lua_CFunction f; /* light C functions */ @@ -113,11 +62,6 @@ typedef struct TValue { } TValue; - -/* macro defining a nil value */ -#define NILCONSTANT {NULL}, LUA_TNIL - - #define val_(o) ((o)->value_) #define valraw(o) (&val_(o)) @@ -139,176 +83,187 @@ typedef struct TValue { /* Macros to test type */ #define checktag(o,t) (rttype(o) == (t)) #define checktype(o,t) (ttnov(o) == (t)) -#define ttisnumber(o) checktype((o), LUA_TNUMBER) -#define ttisfloat(o) checktag((o), LUA_TNUMFLT) -#define ttisinteger(o) checktag((o), LUA_TNUMINT) -#define ttisnil(o) checktag((o), LUA_TNIL) -#define ttisboolean(o) checktag((o), LUA_TBOOLEAN) -#define ttislightuserdata(o) checktag((o), LUA_TLIGHTUSERDATA) -#define ttisstring(o) checktype((o), LUA_TSTRING) -#define ttisshrstring(o) checktag((o), ctb(LUA_TSHRSTR)) -#define ttislngstring(o) checktag((o), ctb(LUA_TLNGSTR)) -#define ttistable(o) checktag((o), ctb(LUA_TTABLE)) -#define ttisfunction(o) checktype(o, LUA_TFUNCTION) -#define ttisclosure(o) ((rttype(o) & 0x1F) == LUA_TFUNCTION) -#define ttisCclosure(o) checktag((o), ctb(LUA_TCCL)) -#define ttisLclosure(o) checktag((o), ctb(LUA_TLCL)) -#define ttislcf(o) checktag((o), LUA_TLCF) -#define ttisfulluserdata(o) checktag((o), ctb(LUA_TUSERDATA)) -#define ttisthread(o) checktag((o), ctb(LUA_TTHREAD)) +/* Macros for internal tests */ +#define righttt(obj) (ttype(obj) == gcvalue(obj)->tt) + +#define checkliveness(L,obj) \ + lua_longassert(!iscollectable(obj) || \ + (righttt(obj) && (L == NULL || !isdead(G(L),gcvalue(obj))))) + + +/* Macros to set values */ +#define settt_(o,t) ((o)->tt_=(t)) + + +#define setobj(L,obj1,obj2) \ + { TValue *io1=(obj1); const TValue *io2=(obj2); \ + io1->value_ = io2->value_; io1->tt_ = io2->tt_; \ + (void)L; checkliveness(L,io1); } + /* -** Macros to access unstructured values (may come both from -** 'TValue's and table keys) +** different types of assignments, according to destination */ -#define ivalueraw(v) ((v).i) -#define fltvalueraw(v) ((v).n) -#define gcvalueraw(v) ((v).gc) -#define pvalueraw(v) ((v).p) -#define tsvalueraw(v) (gco2ts((v).gc)) -#define fvalueraw(v) ((v).f) -#define bvalueraw(v) ((v).b) + +/* from stack to stack */ +#define setobjs2s(L,o1,o2) setobj(L,s2v(o1),s2v(o2)) +/* to stack (not from same stack) */ +#define setobj2s(L,o1,o2) setobj(L,s2v(o1),o2) +/* from table to same table */ +#define setobjt2t setobj +/* to new object */ +#define setobj2n setobj +/* to table */ +#define setobj2t setobj -/* Macros to access values */ -#define ivalue(o) check_exp(ttisinteger(o), val_(o).i) -#define fltvalue(o) check_exp(ttisfloat(o), val_(o).n) -#define nvalue(o) check_exp(ttisnumber(o), \ - (ttisinteger(o) ? cast_num(ivalue(o)) : fltvalue(o))) -#define gcvalue(o) check_exp(iscollectable(o), val_(o).gc) -#define pvalue(o) check_exp(ttislightuserdata(o), val_(o).p) -#define tsvalue(o) check_exp(ttisstring(o), gco2ts(val_(o).gc)) -#define uvalue(o) check_exp(ttisfulluserdata(o), gco2u(val_(o).gc)) -#define clvalue(o) check_exp(ttisclosure(o), gco2cl(val_(o).gc)) -#define clLvalue(o) check_exp(ttisLclosure(o), gco2lcl(val_(o).gc)) -#define clCvalue(o) check_exp(ttisCclosure(o), gco2ccl(val_(o).gc)) -#define fvalue(o) check_exp(ttislcf(o), val_(o).f) -#define hvalue(o) check_exp(ttistable(o), gco2t(val_(o).gc)) -#define bvalue(o) check_exp(ttisboolean(o), val_(o).b) -#define thvalue(o) check_exp(ttisthread(o), gco2th(val_(o).gc)) -#define l_isfalse(o) (ttisnil(o) || (ttisboolean(o) && bvalue(o) == 0)) +typedef union StackValue { + TValue val; +} StackValue; -#define iscollectable(o) (rttype(o) & BIT_ISCOLLECTABLE) +typedef StackValue *StkId; /* index to stack elements */ + +/* convert a 'StackValue' to a 'TValue' */ +#define s2v(o) (&(o)->val) + /* -** Protected access to objects in values +** {================================================================== +** Nil +** =================================================================== */ -#define gcvalueN(o) (iscollectable(o) ? gcvalue(o) : NULL) +#define ttisnil(o) checktag((o), LUA_TNIL) -/* Macros for internal tests */ -#define righttt(obj) (ttype(obj) == gcvalue(obj)->tt) +/* macro defining a nil value */ +#define NILCONSTANT {NULL}, LUA_TNIL -#define checkliveness(L,obj) \ - lua_longassert(!iscollectable(obj) || \ - (righttt(obj) && (L == NULL || !isdead(G(L),gcvalue(obj))))) +#define setnilvalue(obj) settt_(obj, LUA_TNIL) -/* Macros to set values */ -#define settt_(o,t) ((o)->tt_=(t)) +/* (address of) a fixed nil value */ +#define luaO_nilobject (&luaO_nilobject_) -#define setfltvalue(obj,x) \ - { TValue *io=(obj); val_(io).n=(x); settt_(io, LUA_TNUMFLT); } +/* }================================================================== */ -#define chgfltvalue(obj,x) \ - { TValue *io=(obj); lua_assert(ttisfloat(io)); val_(io).n=(x); } -#define setivalue(obj,x) \ - { TValue *io=(obj); val_(io).i=(x); settt_(io, LUA_TNUMINT); } +/* +** {================================================================== +** Booleans +** =================================================================== +*/ -#define chgivalue(obj,x) \ - { TValue *io=(obj); lua_assert(ttisinteger(io)); val_(io).i=(x); } +#define ttisboolean(o) checktag((o), LUA_TBOOLEAN) -#define setnilvalue(obj) settt_(obj, LUA_TNIL) +#define bvalue(o) check_exp(ttisboolean(o), val_(o).b) -#define setfvalue(obj,x) \ - { TValue *io=(obj); val_(io).f=(x); settt_(io, LUA_TLCF); } +#define bvalueraw(v) ((v).b) -#define setpvalue(obj,x) \ - { TValue *io=(obj); val_(io).p=(x); settt_(io, LUA_TLIGHTUSERDATA); } +#define l_isfalse(o) (ttisnil(o) || (ttisboolean(o) && bvalue(o) == 0)) #define setbvalue(obj,x) \ { TValue *io=(obj); val_(io).b=(x); settt_(io, LUA_TBOOLEAN); } -#define setgcovalue(L,obj,x) \ - { TValue *io = (obj); GCObject *i_g=(x); \ - val_(io).gc = i_g; settt_(io, ctb(i_g->tt)); } +/* }================================================================== */ -#define setsvalue(L,obj,x) \ - { TValue *io = (obj); TString *x_ = (x); \ - val_(io).gc = obj2gco(x_); settt_(io, ctb(x_->tt)); \ - checkliveness(L,io); } -#define setuvalue(L,obj,x) \ - { TValue *io = (obj); Udata *x_ = (x); \ - val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TUSERDATA)); \ - checkliveness(L,io); } +/* +** {================================================================== +** Threads +** =================================================================== +*/ + +#define ttisthread(o) checktag((o), ctb(LUA_TTHREAD)) + +#define thvalue(o) check_exp(ttisthread(o), gco2th(val_(o).gc)) #define setthvalue(L,obj,x) \ { TValue *io = (obj); lua_State *x_ = (x); \ val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TTHREAD)); \ checkliveness(L,io); } -#define setclLvalue(L,obj,x) \ - { TValue *io = (obj); LClosure *x_ = (x); \ - val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TLCL)); \ - checkliveness(L,io); } +#define setthvalue2s(L,o,t) setthvalue(L,s2v(o),t) -#define setclCvalue(L,obj,x) \ - { TValue *io = (obj); CClosure *x_ = (x); \ - val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TCCL)); \ - checkliveness(L,io); } +/* }================================================================== */ -#define sethvalue(L,obj,x) \ - { TValue *io = (obj); Table *x_ = (x); \ - val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TTABLE)); \ - checkliveness(L,io); } +/* +** {================================================================== +** Collectable Objects +** =================================================================== +*/ + +/* +** Common Header for all collectable objects (in macro form, to be +** included in other objects) +*/ +#define CommonHeader struct GCObject *next; lu_byte tt; lu_byte marked -#define setobj(L,obj1,obj2) \ - { TValue *io1=(obj1); const TValue *io2=(obj2); \ - io1->value_ = io2->value_; io1->tt_ = io2->tt_; \ - (void)L; checkliveness(L,io1); } +/* Common type for all collectable objects */ +typedef struct GCObject { + CommonHeader; +} GCObject; + + +/* Bit mark for collectable types */ +#define BIT_ISCOLLECTABLE (1 << 6) + +#define iscollectable(o) (rttype(o) & BIT_ISCOLLECTABLE) + +/* mark a tag as collectable */ +#define ctb(t) ((t) | BIT_ISCOLLECTABLE) + +#define gcvalue(o) check_exp(iscollectable(o), val_(o).gc) + +#define gcvalueraw(v) ((v).gc) + +#define setgcovalue(L,obj,x) \ + { TValue *io = (obj); GCObject *i_g=(x); \ + val_(io).gc = i_g; settt_(io, ctb(i_g->tt)); } + +/* }================================================================== */ /* -** different types of assignments, according to destination +** {================================================================== +** Numbers +** =================================================================== */ -/* from stack to stack */ -#define setobjs2s(L,o1,o2) setobj(L,s2v(o1),s2v(o2)) -/* to stack (not from same stack) */ -#define setobj2s(L,o1,o2) setobj(L,s2v(o1),o2) -#define setsvalue2s(L,o,s) setsvalue(L,s2v(o),s) -#define sethvalue2s(L,o,h) sethvalue(L,s2v(o),h) -#define setthvalue2s(L,o,t) setthvalue(L,s2v(o),t) -#define setptvalue2s(L,o,p) setptvalue(L,s2v(o),p) -#define setclLvalue2s(L,o,cl) setclLvalue(L,s2v(o),cl) -/* from table to same table */ -#define setobjt2t setobj -/* to new object */ -#define setobj2n setobj -#define setsvalue2n setsvalue -/* to table */ -#define setobj2t setobj +/* Variant tags for numbers */ +#define LUA_TNUMFLT (LUA_TNUMBER | (0 << 4)) /* float numbers */ +#define LUA_TNUMINT (LUA_TNUMBER | (1 << 4)) /* integer numbers */ +#define ttisnumber(o) checktype((o), LUA_TNUMBER) +#define ttisfloat(o) checktag((o), LUA_TNUMFLT) +#define ttisinteger(o) checktag((o), LUA_TNUMINT) +#define nvalue(o) check_exp(ttisnumber(o), \ + (ttisinteger(o) ? cast_num(ivalue(o)) : fltvalue(o))) +#define fltvalue(o) check_exp(ttisfloat(o), val_(o).n) +#define ivalue(o) check_exp(ttisinteger(o), val_(o).i) -typedef union StackValue { - TValue val; -} StackValue; +#define fltvalueraw(v) ((v).n) +#define ivalueraw(v) ((v).i) +#define setfltvalue(obj,x) \ + { TValue *io=(obj); val_(io).n=(x); settt_(io, LUA_TNUMFLT); } -typedef StackValue *StkId; /* index to stack elements */ +#define chgfltvalue(obj,x) \ + { TValue *io=(obj); lua_assert(ttisfloat(io)); val_(io).n=(x); } -/* convert a 'StackValue' to a 'TValue' */ -#define s2v(o) (&(o)->val) +#define setivalue(obj,x) \ + { TValue *io=(obj); val_(io).i=(x); settt_(io, LUA_TNUMINT); } + +#define chgivalue(obj,x) \ + { TValue *io=(obj); lua_assert(ttisinteger(io)); val_(io).i=(x); } +/* }================================================================== */ /* @@ -317,6 +272,30 @@ typedef StackValue *StkId; /* index to stack elements */ ** =================================================================== */ +/* Variant tags for strings */ +#define LUA_TSHRSTR (LUA_TSTRING | (0 << 4)) /* short strings */ +#define LUA_TLNGSTR (LUA_TSTRING | (1 << 4)) /* long strings */ + +#define ttisstring(o) checktype((o), LUA_TSTRING) +#define ttisshrstring(o) checktag((o), ctb(LUA_TSHRSTR)) +#define ttislngstring(o) checktag((o), ctb(LUA_TLNGSTR)) + +#define tsvalueraw(v) (gco2ts((v).gc)) + +#define tsvalue(o) check_exp(ttisstring(o), gco2ts(val_(o).gc)) + +#define setsvalue(L,obj,x) \ + { TValue *io = (obj); TString *x_ = (x); \ + val_(io).gc = obj2gco(x_); settt_(io, ctb(x_->tt)); \ + checkliveness(L,io); } + +/* set a string to the stack */ +#define setsvalue2s(L,o,s) setsvalue(L,s2v(o),s) + +/* set a string to a new object */ +#define setsvalue2n setsvalue + + /* ** Header for string value; string bytes follow the end of this structure ** (aligned according to 'UTString'; see next). @@ -368,6 +347,23 @@ typedef union UTString { ** =================================================================== */ +#define ttislightuserdata(o) checktag((o), LUA_TLIGHTUSERDATA) +#define ttisfulluserdata(o) checktag((o), ctb(LUA_TUSERDATA)) + +#define pvalue(o) check_exp(ttislightuserdata(o), val_(o).p) +#define uvalue(o) check_exp(ttisfulluserdata(o), gco2u(val_(o).gc)) + +#define pvalueraw(v) ((v).p) + +#define setpvalue(obj,x) \ + { TValue *io=(obj); val_(io).p=(x); settt_(io, LUA_TLIGHTUSERDATA); } + +#define setuvalue(L,obj,x) \ + { TValue *io = (obj); Udata *x_ = (x); \ + val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TUSERDATA)); \ + checkliveness(L,io); } + + /* Ensures that addresses after this type are always fully aligned. */ typedef union UValue { TValue uv; @@ -483,6 +479,42 @@ typedef struct Proto { ** =================================================================== */ +/* Variant tags for functions */ +#define LUA_TLCL (LUA_TFUNCTION | (0 << 4)) /* Lua closure */ +#define LUA_TLCF (LUA_TFUNCTION | (1 << 4)) /* light C function */ +#define LUA_TCCL (LUA_TFUNCTION | (2 << 4)) /* C closure */ + +#define ttisfunction(o) checktype(o, LUA_TFUNCTION) +#define ttisclosure(o) ((rttype(o) & 0x1F) == LUA_TFUNCTION) +#define ttisLclosure(o) checktag((o), ctb(LUA_TLCL)) +#define ttislcf(o) checktag((o), LUA_TLCF) +#define ttisCclosure(o) checktag((o), ctb(LUA_TCCL)) + +#define isLfunction(o) ttisLclosure(o) + +#define clvalue(o) check_exp(ttisclosure(o), gco2cl(val_(o).gc)) +#define clLvalue(o) check_exp(ttisLclosure(o), gco2lcl(val_(o).gc)) +#define fvalue(o) check_exp(ttislcf(o), val_(o).f) +#define clCvalue(o) check_exp(ttisCclosure(o), gco2ccl(val_(o).gc)) + +#define fvalueraw(v) ((v).f) + +#define setclLvalue(L,obj,x) \ + { TValue *io = (obj); LClosure *x_ = (x); \ + val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TLCL)); \ + checkliveness(L,io); } + +#define setclLvalue2s(L,o,cl) setclLvalue(L,s2v(o),cl) + +#define setfvalue(obj,x) \ + { TValue *io=(obj); val_(io).f=(x); settt_(io, LUA_TLCF); } + +#define setclCvalue(L,obj,x) \ + { TValue *io = (obj); CClosure *x_ = (x); \ + val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TCCL)); \ + checkliveness(L,io); } + + /* ** Upvalues for Lua closures */ @@ -499,11 +531,6 @@ typedef struct UpVal { } UpVal; - -/* -** Closures -*/ - #define ClosureHeader \ CommonHeader; lu_byte nupvalues; GCObject *gclist @@ -527,8 +554,6 @@ typedef union Closure { } Closure; -#define isLfunction(o) ttisLclosure(o) - #define getproto(o) (clLvalue(o)->p) /* }================================================================== */ @@ -540,6 +565,18 @@ typedef union Closure { ** =================================================================== */ +#define ttistable(o) checktag((o), ctb(LUA_TTABLE)) + +#define hvalue(o) check_exp(ttistable(o), gco2t(val_(o).gc)) + +#define sethvalue(L,obj,x) \ + { TValue *io = (obj); Table *x_ = (x); \ + val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TTABLE)); \ + checkliveness(L,io); } + +#define sethvalue2s(L,o,h) sethvalue(L,s2v(o),h) + + /* ** Nodes for Hash tables: A pack of two TValue's (key-value pairs) ** plus a 'next' field to link colliding entries. The distribution @@ -629,12 +666,6 @@ typedef struct Table { #define sizenode(t) (twoto((t)->lsizenode)) -/* -** (address of) a fixed nil value -*/ -#define luaO_nilobject (&luaO_nilobject_) - - LUAI_DDEC const TValue luaO_nilobject_; /* size of buffer for 'luaO_utf8esc' function */ From 9243c414d92c253edd908f438caa31e2aa16f3f4 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 23 Feb 2018 10:16:18 -0300 Subject: [PATCH 0223/1145] first version of empty entries in tables (so that, in the future, tables can contain regular nil entries) --- lapi.c | 30 +++++++++++++++----------- lgc.c | 58 +++++++++++++++++++++++++------------------------- llex.c | 4 ++-- lobject.h | 30 +++++++++++++++++++++++--- ltable.c | 63 +++++++++++++++++++++++++++++-------------------------- ltable.h | 7 ++++++- ltests.c | 6 +++--- ltm.c | 8 +++---- ltm.h | 8 ++++++- lvm.c | 21 ++++++++++--------- lvm.h | 10 ++++----- 11 files changed, 146 insertions(+), 99 deletions(-) diff --git a/lapi.c b/lapi.c index 78ae7e0b6d..0debc1d33f 100644 --- a/lapi.c +++ b/lapi.c @@ -1,5 +1,5 @@ /* -** $Id: lapi.c,v 2.283 2018/02/05 17:10:52 roberto Exp roberto $ +** $Id: lapi.c,v 2.285 2018/02/20 16:52:50 roberto Exp roberto $ ** Lua API ** See Copyright Notice in lua.h */ @@ -657,14 +657,26 @@ LUA_API int lua_geti (lua_State *L, int idx, lua_Integer n) { } +static int finishrawget (lua_State *L, const TValue *val) { + if (isempty(val)) /* avoid copying empty items to the stack */ + setnilvalue(s2v(L->top)); + else + setobj2s(L, L->top, val); + api_incr_top(L); + lua_unlock(L); + return ttnov(s2v(L->top - 1)); +} + + LUA_API int lua_rawget (lua_State *L, int idx) { TValue *t; + const TValue *val; lua_lock(L); t = index2value(L, idx); api_check(L, ttistable(t), "table expected"); - setobj2s(L, L->top - 1, luaH_get(hvalue(t), s2v(L->top - 1))); - lua_unlock(L); - return ttnov(s2v(L->top - 1)); + val = luaH_get(hvalue(t), s2v(L->top - 1)); + L->top--; /* remove key */ + return finishrawget(L, val); } @@ -673,10 +685,7 @@ LUA_API int lua_rawgeti (lua_State *L, int idx, lua_Integer n) { lua_lock(L); t = index2value(L, idx); api_check(L, ttistable(t), "table expected"); - setobj2s(L, L->top, luaH_getint(hvalue(t), n)); - api_incr_top(L); - lua_unlock(L); - return ttnov(s2v(L->top - 1)); + return finishrawget(L, luaH_getint(hvalue(t), n)); } @@ -687,10 +696,7 @@ LUA_API int lua_rawgetp (lua_State *L, int idx, const void *p) { t = index2value(L, idx); api_check(L, ttistable(t), "table expected"); setpvalue(&k, cast_voidp(p)); - setobj2s(L, L->top, luaH_get(hvalue(t), &k)); - api_incr_top(L); - lua_unlock(L); - return ttnov(s2v(L->top - 1)); + return finishrawget(L, luaH_get(hvalue(t), &k)); } diff --git a/lgc.c b/lgc.c index 7cb3c18d37..8245dc2130 100644 --- a/lgc.c +++ b/lgc.c @@ -145,12 +145,13 @@ static GCObject **getgclist (GCObject *o) { ** Clear keys for empty entries in tables. If entry is empty ** and its key is not marked, mark its entry as dead. This allows the ** collection of the key, but keeps its entry in the table (its removal -** could break a chain). Other places never manipulate dead keys, -** because its associated nil value is enough to signal that the entry -** is logically empty. +** could break a chain). The main feature of a dead key is that it must +** be different from any other value, to do not disturb searches. +** Other places never manipulate dead keys, because its associated empty +** value is enough to signal that the entry is logically empty. */ -static void removeentry (Node *n) { - lua_assert(ttisnil(gval(n))); +static void clearkey (Node *n) { + lua_assert(isempty(gval(n))); if (keyiswhite(n)) setdeadkey(n); /* unused and unmarked key; remove it */ } @@ -386,8 +387,8 @@ static void traverseweakvalue (global_State *g, Table *h) { worth traversing it now just to check) */ int hasclears = (h->sizearray > 0); for (n = gnode(h, 0); n < limit; n++) { /* traverse hash part */ - if (ttisnil(gval(n))) /* entry is empty? */ - removeentry(n); /* remove it */ + if (isempty(gval(n))) /* entry is empty? */ + clearkey(n); /* clear its key */ else { lua_assert(!keyisnil(n)); markkey(g, n); @@ -428,8 +429,8 @@ static int traverseephemeron (global_State *g, Table *h) { } /* traverse hash part */ for (n = gnode(h, 0); n < limit; n++) { - if (ttisnil(gval(n))) /* entry is empty? */ - removeentry(n); /* remove it */ + if (isempty(gval(n))) /* entry is empty? */ + clearkey(n); /* clear its key */ else if (iscleared(g, gckeyN(n))) { /* key is not marked (yet)? */ hasclears = 1; /* table must be cleared */ if (valiswhite(gval(n))) /* value not marked yet? */ @@ -461,8 +462,8 @@ static void traversestrongtable (global_State *g, Table *h) { for (i = 0; i < h->sizearray; i++) /* traverse array part */ markvalue(g, &h->array[i]); for (n = gnode(h, 0); n < limit; n++) { /* traverse hash part */ - if (ttisnil(gval(n))) /* entry is empty? */ - removeentry(n); /* remove it */ + if (isempty(gval(n))) /* entry is empty? */ + clearkey(n); /* clear its key */ else { lua_assert(!keyisnil(n)); markkey(g, n); @@ -681,15 +682,16 @@ static void clearprotolist (global_State *g) { /* ** clear entries with unmarked keys from all weaktables in list 'l' */ -static void clearkeys (global_State *g, GCObject *l) { +static void clearbykeys (global_State *g, GCObject *l) { for (; l; l = gco2t(l)->gclist) { Table *h = gco2t(l); - Node *n, *limit = gnodelast(h); + Node *limit = gnodelast(h); + Node *n; for (n = gnode(h, 0); n < limit; n++) { - if (!ttisnil(gval(n)) && (iscleared(g, gckeyN(n)))) /* unmarked key? */ - setnilvalue(gval(n)); /* clear value */ - if (ttisnil(gval(n))) /* is entry empty? */ - removeentry(n); /* remove it from table */ + if (isempty(gval(n))) /* is entry empty? */ + clearkey(n); /* clear its key */ + else if (iscleared(g, gckeyN(n))) /* unmarked key? */ + setempty(gval(n)); /* remove entry */ } } } @@ -699,7 +701,7 @@ static void clearkeys (global_State *g, GCObject *l) { ** clear entries with unmarked values from all weaktables in list 'l' up ** to element 'f' */ -static void clearvalues (global_State *g, GCObject *l, GCObject *f) { +static void clearbyvalues (global_State *g, GCObject *l, GCObject *f) { for (; l != f; l = gco2t(l)->gclist) { Table *h = gco2t(l); Node *n, *limit = gnodelast(h); @@ -707,13 +709,13 @@ static void clearvalues (global_State *g, GCObject *l, GCObject *f) { for (i = 0; i < h->sizearray; i++) { TValue *o = &h->array[i]; if (iscleared(g, gcvalueN(o))) /* value was collected? */ - setnilvalue(o); /* remove value */ + setempty(o); /* remove entry */ } for (n = gnode(h, 0); n < limit; n++) { if (iscleared(g, gcvalueN(gval(n)))) /* unmarked value? */ - setnilvalue(gval(n)); /* clear value */ - if (ttisnil(gval(n))) /* is entry empty? */ - removeentry(n); /* remove it from table */ + setempty(gval(n)); /* remove entry */ + if (isempty(gval(n))) /* is entry empty? */ + clearkey(n); /* clear its key */ } } } @@ -1372,8 +1374,8 @@ static lu_mem atomic (lua_State *L) { convergeephemerons(g); /* at this point, all strongly accessible objects are marked. */ /* Clear values from weak tables, before checking finalizers */ - clearvalues(g, g->weak, NULL); - clearvalues(g, g->allweak, NULL); + clearbyvalues(g, g->weak, NULL); + clearbyvalues(g, g->allweak, NULL); origweak = g->weak; origall = g->allweak; separatetobefnz(g, 0); /* separate objects to be finalized */ work += markbeingfnz(g); /* mark objects that will be finalized */ @@ -1381,11 +1383,11 @@ static lu_mem atomic (lua_State *L) { convergeephemerons(g); /* at this point, all resurrected objects are marked. */ /* remove dead objects from weak tables */ - clearkeys(g, g->ephemeron); /* clear keys from all ephemeron tables */ - clearkeys(g, g->allweak); /* clear keys from all 'allweak' tables */ + clearbykeys(g, g->ephemeron); /* clear keys from all ephemeron tables */ + clearbykeys(g, g->allweak); /* clear keys from all 'allweak' tables */ /* clear values from resurrected weak tables */ - clearvalues(g, g->weak, origweak); - clearvalues(g, g->allweak, origall); + clearbyvalues(g, g->weak, origweak); + clearbyvalues(g, g->allweak, origall); luaS_clearcache(g); clearprotolist(g); g->currentwhite = cast_byte(otherwhite(g)); /* flip current white */ diff --git a/llex.c b/llex.c index be77514630..5d5efb0743 100644 --- a/llex.c +++ b/llex.c @@ -1,5 +1,5 @@ /* -** $Id: llex.c,v 2.98 2017/06/29 15:06:44 roberto Exp roberto $ +** $Id: llex.c,v 2.99 2018/01/28 15:13:26 roberto Exp roberto $ ** Lexical Analyzer ** See Copyright Notice in lua.h */ @@ -130,7 +130,7 @@ TString *luaX_newstring (LexState *ls, const char *str, size_t l) { TString *ts = luaS_newlstr(L, str, l); /* create new string */ setsvalue2s(L, L->top++, ts); /* temporarily anchor it in stack */ o = luaH_set(L, ls->h, s2v(L->top - 1)); - if (ttisnil(o)) { /* not in use yet? */ + if (isempty(o)) { /* not in use yet? */ /* boolean value does not need GC barrier; table is not a metatable, so it does not need to invalidate cache */ setbvalue(o, 1); /* t[string] = true */ diff --git a/lobject.h b/lobject.h index 2b65da3658..c6590cf041 100644 --- a/lobject.h +++ b/lobject.h @@ -1,5 +1,5 @@ /* -** $Id: lobject.h,v 2.135 2018/02/21 16:28:12 roberto Exp roberto $ +** $Id: lobject.h,v 2.136 2018/02/22 17:28:10 roberto Exp roberto $ ** Type definitions for Lua objects ** See Copyright Notice in lua.h */ @@ -100,7 +100,7 @@ typedef struct TValue { #define setobj(L,obj1,obj2) \ { TValue *io1=(obj1); const TValue *io2=(obj2); \ io1->value_ = io2->value_; io1->tt_ = io2->tt_; \ - (void)L; checkliveness(L,io1); } + (void)L; checkliveness(L,io1); lua_assert(!isreallyempty(io1)); } /* ** different types of assignments, according to destination @@ -148,6 +148,30 @@ typedef StackValue *StkId; /* index to stack elements */ /* (address of) a fixed nil value */ #define luaO_nilobject (&luaO_nilobject_) + +/* +** Variant tag, used only in tables to signal an empty slot +** (which might be different from a slot containing nil) +*/ +#define LUA_TEMPTY (LUA_TNIL | (1 << 4)) + +#define ttisnilorempty(v) checktype((v), LUA_TNIL) +/* +** By default, entries with any kind of nil are considered empty +*/ +#define isempty(v) ttisnilorempty(v) + +#define isreallyempty(v) checktag((v), LUA_TEMPTY) + +/* macro defining an empty value */ +#define EMPTYCONSTANT {NULL}, LUA_TEMPTY + + +/* mark an entry as empty */ +#define setempty(v) settt_(v, LUA_TEMPTY) + + + /* }================================================================== */ @@ -644,7 +668,7 @@ typedef struct Table { /* ** Use a "nil table" to mark dead keys in a table. Those keys serve -** only to keep space for removed entries, which may still be part of +** to keep space for removed entries, which may still be part of ** chains. Note that the 'keytt' does not have the BIT_ISCOLLECTABLE ** set, so these values are considered not collectable and are different ** from any valid value. diff --git a/ltable.c b/ltable.c index 5378e31a86..c71d627adc 100644 --- a/ltable.c +++ b/ltable.c @@ -1,5 +1,5 @@ /* -** $Id: ltable.c,v 2.132 2018/02/19 20:06:56 roberto Exp roberto $ +** $Id: ltable.c,v 2.133 2018/02/21 12:54:26 roberto Exp roberto $ ** Lua tables (hash) ** See Copyright Notice in lua.h */ @@ -88,11 +88,14 @@ #define dummynode (&dummynode_) static const Node dummynode_ = { - {{NULL}, LUA_TNIL, /* value's value and type */ + {{NULL}, LUA_TEMPTY, /* value's value and type */ LUA_TNIL, 0, {NULL}} /* key type, next, and key value */ }; +LUAI_DDEF const TValue luaH_emptyobject_ = {EMPTYCONSTANT}; + + /* ** Hash for floating-point numbers. ** The main computation should be just @@ -200,7 +203,7 @@ static const TValue *getgeneric (Table *t, const TValue *key) { else { int nx = gnext(n); if (nx == 0) - return luaO_nilobject; /* not found */ + return luaH_emptyobject; /* not found */ n += nx; } } @@ -232,7 +235,7 @@ static unsigned int findindex (lua_State *L, Table *t, TValue *key) { return i; /* yes; that's the index */ else { const TValue *n = getgeneric(t, key); - if (n == luaO_nilobject) + if (n == luaH_emptyobject) luaG_runerror(L, "invalid key to 'next'"); /* key not found */ i = cast_int(nodefromval(n) - gnode(t, 0)); /* key index in hash table */ /* hash elements are numbered after array ones */ @@ -244,14 +247,14 @@ static unsigned int findindex (lua_State *L, Table *t, TValue *key) { int luaH_next (lua_State *L, Table *t, StkId key) { unsigned int i = findindex(L, t, s2v(key)); /* find original element */ for (; i < t->sizearray; i++) { /* try first array part */ - if (!ttisnil(&t->array[i])) { /* a non-nil value? */ + if (!isempty(&t->array[i])) { /* a non-empty entry? */ setivalue(s2v(key), i + 1); setobj2s(L, key + 1, &t->array[i]); return 1; } } for (i -= t->sizearray; cast_int(i) < sizenode(t); i++) { /* hash part */ - if (!ttisnil(gval(gnode(t, i)))) { /* a non-nil value? */ + if (!isempty(gval(gnode(t, i)))) { /* a non-empty entry? */ Node *n = gnode(t, i); getnodekey(L, s2v(key), n); setobj2s(L, key + 1, gval(n)); @@ -336,7 +339,7 @@ static unsigned int numusearray (const Table *t, unsigned int *nums) { } /* count elements in range (2^(lg - 1), 2^lg] */ for (; i <= lim; i++) { - if (!ttisnil(&t->array[i-1])) + if (!isempty(&t->array[i-1])) lc++; } nums[lg] += lc; @@ -352,7 +355,7 @@ static int numusehash (const Table *t, unsigned int *nums, unsigned int *pna) { int i = sizenode(t); while (i--) { Node *n = &t->node[i]; - if (!ttisnil(gval(n))) { + if (!isempty(gval(n))) { if (keyisinteger(n)) ause += countint(keyival(n), nums); totaluse++; @@ -387,7 +390,7 @@ static void setnodevector (lua_State *L, Table *t, unsigned int size) { Node *n = gnode(t, i); gnext(n) = 0; setnilkey(n); - setnilvalue(gval(n)); + setempty(gval(n)); } t->lsizenode = cast_byte(lsize); t->lastfree = gnode(t, size); /* all positions are free */ @@ -403,7 +406,7 @@ static void reinsert (lua_State *L, Table *ot, Table *t) { int size = sizenode(ot); for (j = 0; j < size; j++) { Node *old = gnode(ot, j); - if (!ttisnil(gval(old))) { + if (!isempty(gval(old))) { /* doesn't need barrier/invalidate cache, as entry was already present in the table */ TValue k; @@ -456,7 +459,7 @@ void luaH_resize (lua_State *L, Table *t, unsigned int newasize, exchangehashpart(t, &newt); /* and new hash */ /* re-insert into the new hash the elements from vanishing slice */ for (i = newasize; i < oldasize; i++) { - if (!ttisnil(&t->array[i])) + if (!isempty(&t->array[i])) luaH_setint(L, t, i + 1, &t->array[i]); } t->sizearray = oldasize; /* restore current size... */ @@ -473,7 +476,7 @@ void luaH_resize (lua_State *L, Table *t, unsigned int newasize, t->array = newarray; /* set new array part */ t->sizearray = newasize; for (i = oldasize; i < newasize; i++) /* clear new slice of the array */ - setnilvalue(&t->array[i]); + setempty(&t->array[i]); /* re-insert elements from old hash part into new parts */ reinsert(L, &newt, t); /* 'newt' now has the old hash */ freehash(L, &newt); /* free old hash part */ @@ -569,7 +572,7 @@ TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) { luaG_runerror(L, "table index is NaN"); } mp = mainpositionTV(t, key); - if (!ttisnil(gval(mp)) || isdummy(t)) { /* main position is taken? */ + if (!isempty(gval(mp)) || isdummy(t)) { /* main position is taken? */ Node *othern; Node *f = getfreepos(t); /* get a free place */ if (f == NULL) { /* cannot find a free place? */ @@ -589,7 +592,7 @@ TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) { gnext(f) += cast_int(mp - f); /* correct 'next' */ gnext(mp) = 0; /* now 'mp' is free */ } - setnilvalue(gval(mp)); + setempty(gval(mp)); } else { /* colliding node is in its own main position */ /* new node will go into free position */ @@ -602,7 +605,7 @@ TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) { } setnodekey(L, mp, key); luaC_barrierback(L, obj2gco(t), key); - lua_assert(ttisnil(gval(mp))); + lua_assert(isempty(gval(mp))); return gval(mp); } @@ -625,7 +628,7 @@ const TValue *luaH_getint (Table *t, lua_Integer key) { n += nx; } } - return luaO_nilobject; + return luaH_emptyobject; } } @@ -642,7 +645,7 @@ const TValue *luaH_getshortstr (Table *t, TString *key) { else { int nx = gnext(n); if (nx == 0) - return luaO_nilobject; /* not found */ + return luaH_emptyobject; /* not found */ n += nx; } } @@ -667,7 +670,7 @@ const TValue *luaH_get (Table *t, const TValue *key) { switch (ttype(key)) { case LUA_TSHRSTR: return luaH_getshortstr(t, tsvalue(key)); case LUA_TNUMINT: return luaH_getint(t, ivalue(key)); - case LUA_TNIL: return luaO_nilobject; + case LUA_TNIL: return luaH_emptyobject; case LUA_TNUMFLT: { lua_Integer k; if (luaV_flttointeger(fltvalue(key), &k, 0)) /* index is an integral? */ @@ -686,7 +689,7 @@ const TValue *luaH_get (Table *t, const TValue *key) { */ TValue *luaH_set (lua_State *L, Table *t, const TValue *key) { const TValue *p = luaH_get(t, key); - if (p != luaO_nilobject) + if (p != luaH_emptyobject) return cast(TValue *, p); else return luaH_newkey(L, t, key); } @@ -695,7 +698,7 @@ TValue *luaH_set (lua_State *L, Table *t, const TValue *key) { void luaH_setint (lua_State *L, Table *t, lua_Integer key, TValue *value) { const TValue *p = luaH_getint(t, key); TValue *cell; - if (p != luaO_nilobject) + if (p != luaH_emptyobject) cell = cast(TValue *, p); else { TValue k; @@ -728,16 +731,16 @@ static lua_Unsigned hash_search (Table *t, lua_Unsigned j) { j *= 2; else { j = LUA_MAXINTEGER; - if (ttisnil(luaH_getint(t, j))) /* t[j] == nil? */ + if (isempty(luaH_getint(t, j))) /* t[j] not present? */ break; /* 'j' now is an absent index */ else /* weird case */ return j; /* well, max integer is a boundary... */ } - } while (!ttisnil(luaH_getint(t, j))); /* repeat until t[j] == nil */ - /* i < j && t[i] !≃ nil && t[j] == nil */ + } while (!isempty(luaH_getint(t, j))); /* repeat until an absent t[j] */ + /* i < j && t[i] present && t[j] absent */ while (j - i > 1u) { /* do a binary search between them */ lua_Unsigned m = (i + j) / 2; - if (ttisnil(luaH_getint(t, m))) j = m; + if (isempty(luaH_getint(t, m))) j = m; else i = m; } return i; @@ -746,27 +749,27 @@ static lua_Unsigned hash_search (Table *t, lua_Unsigned j) { /* ** Try to find a boundary in table 't'. (A 'boundary' is an integer index -** such that t[i] is non-nil and t[i+1] is nil, plus 0 if t[1] is nil -** and 'maxinteger' if t[maxinteger] is not nil.) +** such that t[i] is present and t[i+1] is absent, or 0 if t[1] is absent +** and 'maxinteger' if t[maxinteger] is present.) ** First, try the array part: if there is an array part and its last -** element is nil, there must be a boundary there; a binary search +** element is absent, there must be a boundary there; a binary search ** finds that boundary. Otherwise, if the hash part is empty or does not ** contain 'j + 1', 'j' is a boundary. Otherwize, call 'hash_search' ** to find a boundary in the hash part. */ lua_Unsigned luaH_getn (Table *t) { unsigned int j = t->sizearray; - if (j > 0 && ttisnil(&t->array[j - 1])) { + if (j > 0 && isempty(&t->array[j - 1])) { unsigned int i = 0; while (j - i > 1u) { /* binary search */ unsigned int m = (i + j) / 2; - if (ttisnil(&t->array[m - 1])) j = m; + if (isempty(&t->array[m - 1])) j = m; else i = m; } return i; } else { /* 'j' is zero or present in table */ - if (isdummy(t) || ttisnil(luaH_getint(t, l_castU2S(j + 1)))) + if (isdummy(t) || isempty(luaH_getint(t, l_castU2S(j + 1)))) return j; /* 'j + 1' is absent... */ else /* 'j + 1' is also present */ return hash_search(t, j); diff --git a/ltable.h b/ltable.h index 88f906361b..bcf92984ab 100644 --- a/ltable.h +++ b/ltable.h @@ -1,5 +1,5 @@ /* -** $Id: ltable.h,v 2.24 2017/05/19 12:48:15 roberto Exp roberto $ +** $Id: ltable.h,v 2.25 2017/06/09 16:48:44 roberto Exp roberto $ ** Lua tables (hash) ** See Copyright Notice in lua.h */ @@ -21,6 +21,8 @@ /* true when 't' is using 'dummynode' as its hash part */ #define isdummy(t) ((t)->lastfree == NULL) +#define luaH_emptyobject (&luaH_emptyobject_) + /* allocated size for hash nodes */ #define allocsizenode(t) (isdummy(t) ? 0 : sizenode(t)) @@ -30,6 +32,9 @@ #define nodefromval(v) cast(Node *, (v)) +LUAI_DDEC const TValue luaH_emptyobject_; + + LUAI_FUNC const TValue *luaH_getint (Table *t, lua_Integer key); LUAI_FUNC void luaH_setint (lua_State *L, Table *t, lua_Integer key, TValue *value); diff --git a/ltests.c b/ltests.c index 97d1abd4f2..a5781fb626 100644 --- a/ltests.c +++ b/ltests.c @@ -1,5 +1,5 @@ /* -** $Id: ltests.c,v 2.240 2018/01/28 15:13:26 roberto Exp roberto $ +** $Id: ltests.c,v 2.241 2018/02/20 16:52:50 roberto Exp roberto $ ** Internal Module for Debugging of the Lua Implementation ** See Copyright Notice in lua.h */ @@ -254,7 +254,7 @@ static void checktable (global_State *g, Table *h) { for (i = 0; i < h->sizearray; i++) checkvalref(g, hgc, &h->array[i]); for (n = gnode(h, 0); n < limit; n++) { - if (!ttisnil(gval(n))) { + if (!isempty(gval(n))) { TValue k; getnodekey(g->mainthread, &k, n); lua_assert(!keyisnil(n)); @@ -842,7 +842,7 @@ static int table_query (lua_State *L) { else if ((i -= t->sizearray) < sizenode(t)) { TValue k; getnodekey(L, &k, gnode(t, i)); - if (!ttisnil(gval(gnode(t, i))) || + if (!isempty(gval(gnode(t, i))) || ttisnil(&k) || ttisnumber(&k)) { pushobject(L, &k); diff --git a/ltm.c b/ltm.c index 3f29c83b7d..0187c6f59c 100644 --- a/ltm.c +++ b/ltm.c @@ -1,5 +1,5 @@ /* -** $Id: ltm.c,v 2.62 2018/02/17 19:20:00 roberto Exp roberto $ +** $Id: ltm.c,v 2.63 2018/02/21 15:49:32 roberto Exp roberto $ ** Tag methods ** See Copyright Notice in lua.h */ @@ -60,7 +60,7 @@ void luaT_init (lua_State *L) { const TValue *luaT_gettm (Table *events, TMS event, TString *ename) { const TValue *tm = luaH_getshortstr(events, ename); lua_assert(event <= TM_EQ); - if (ttisnil(tm)) { /* no tag method? */ + if (notm(tm)) { /* no tag method? */ events->flags |= cast_byte(1u<flags & (1u<<(e))) ? NULL : luaT_gettm(et, e, (g)->tmname[e])) diff --git a/lvm.c b/lvm.c index efde6fb211..99e0d6b779 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.345 2018/02/21 15:49:32 roberto Exp roberto $ +** $Id: lvm.c,v 2.346 2018/02/21 19:43:44 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -168,7 +168,7 @@ static int forlimit (const TValue *obj, lua_Integer *p, lua_Integer step, /* ** Finish the table access 'val = t[key]'. ** if 'slot' is NULL, 't' is not a table; otherwise, 'slot' points to -** t[k] entry (which must be nil). +** t[k] entry (which must be empty). */ void luaV_finishget (lua_State *L, const TValue *t, TValue *key, StkId val, const TValue *slot) { @@ -178,12 +178,12 @@ void luaV_finishget (lua_State *L, const TValue *t, TValue *key, StkId val, if (slot == NULL) { /* 't' is not a table? */ lua_assert(!ttistable(t)); tm = luaT_gettmbyobj(L, t, TM_INDEX); - if (ttisnil(tm)) + if (notm(tm)) luaG_typeerror(L, t, "index"); /* no metamethod */ /* else will try the metamethod */ } else { /* 't' is a table */ - lua_assert(ttisnil(slot)); + lua_assert(isempty(slot)); tm = fasttm(L, hvalue(t)->metatable, TM_INDEX); /* table's metamethod */ if (tm == NULL) { /* no metamethod? */ setnilvalue(s2v(val)); /* result is nil */ @@ -209,8 +209,8 @@ void luaV_finishget (lua_State *L, const TValue *t, TValue *key, StkId val, /* ** Finish a table assignment 't[key] = val'. ** If 'slot' is NULL, 't' is not a table. Otherwise, 'slot' points -** to the entry 't[key]', or to 'luaO_nilobject' if there is no such -** entry. (The value at 'slot' must be nil, otherwise 'luaV_fastget' +** to the entry 't[key]', or to 'luaH_emptyobject' if there is no such +** entry. (The value at 'slot' must be empty, otherwise 'luaV_fastget' ** would have done the job.) */ void luaV_finishset (lua_State *L, const TValue *t, TValue *key, @@ -220,10 +220,10 @@ void luaV_finishset (lua_State *L, const TValue *t, TValue *key, const TValue *tm; /* '__newindex' metamethod */ if (slot != NULL) { /* is 't' a table? */ Table *h = hvalue(t); /* save 't' table */ - lua_assert(ttisnil(slot)); /* old value must be nil */ + lua_assert(isempty(slot)); /* slot must be empty */ tm = fasttm(L, h->metatable, TM_NEWINDEX); /* get metamethod */ if (tm == NULL) { /* no metamethod? */ - if (slot == luaO_nilobject) /* no previous entry? */ + if (slot == luaH_emptyobject) /* no previous entry? */ slot = luaH_newkey(L, h, key); /* create one */ /* no metamethod and (now) there is an entry with given key */ setobj2t(L, cast(TValue *, slot), val); /* set its new value */ @@ -234,7 +234,8 @@ void luaV_finishset (lua_State *L, const TValue *t, TValue *key, /* else will try the metamethod */ } else { /* not a table; check metamethod */ - if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_NEWINDEX))) + tm = luaT_gettmbyobj(L, t, TM_NEWINDEX); + if (notm(tm)) luaG_typeerror(L, t, "index"); } /* try the metamethod */ @@ -586,7 +587,7 @@ void luaV_objlen (lua_State *L, StkId ra, const TValue *rb) { } default: { /* try metamethod */ tm = luaT_gettmbyobj(L, rb, TM_LEN); - if (ttisnil(tm)) /* no metamethod? */ + if (notm(tm)) /* no metamethod? */ luaG_typeerror(L, rb, "get length of"); break; } diff --git a/lvm.h b/lvm.h index cbf7e92264..c5d5206de5 100644 --- a/lvm.h +++ b/lvm.h @@ -1,5 +1,5 @@ /* -** $Id: lvm.h,v 2.49 2018/02/19 20:06:56 roberto Exp roberto $ +** $Id: lvm.h,v 2.50 2018/02/21 12:54:26 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -64,17 +64,17 @@ /* -** fast track for 'gettable': if 't' is a table and 't[k]' is not nil, +** fast track for 'gettable': if 't' is a table and 't[k]' is present, ** return 1 with 'slot' pointing to 't[k]' (position of final result). ** Otherwise, return 0 (meaning it will have to check metamethod) -** with 'slot' pointing to a nil 't[k]' (if 't' is a table) or NULL +** with 'slot' pointing to an empty 't[k]' (if 't' is a table) or NULL ** (otherwise). 'f' is the raw get function to use. */ #define luaV_fastget(L,t,k,slot,f) \ (!ttistable(t) \ ? (slot = NULL, 0) /* not a table; 'slot' is NULL and result is 0 */ \ : (slot = f(hvalue(t), k), /* else, do raw access */ \ - !ttisnil(slot))) /* result not nil? */ + !isempty(slot))) /* result not empty? */ /* @@ -86,7 +86,7 @@ ? (slot = NULL, 0) /* not a table; 'slot' is NULL and result is 0 */ \ : (slot = (l_castS2U(k) - 1u < hvalue(t)->sizearray) \ ? &hvalue(t)->array[k - 1] : luaH_getint(hvalue(t), k), \ - !ttisnil(slot))) /* result not nil? */ + !isempty(slot))) /* result not empty? */ /* From 4a7fe61806b266d895c0d2a4725dc427315022bb Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 23 Feb 2018 10:21:27 -0300 Subject: [PATCH 0224/1145] in 'clearbykeys', clear keys of just-removed entries too --- lgc.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lgc.c b/lgc.c index 8245dc2130..13f372d3bc 100644 --- a/lgc.c +++ b/lgc.c @@ -1,5 +1,5 @@ /* -** $Id: lgc.c,v 2.248 2018/02/20 16:52:50 roberto Exp roberto $ +** $Id: lgc.c,v 2.250 2018/02/23 13:16:18 roberto Exp roberto $ ** Garbage Collector ** See Copyright Notice in lua.h */ @@ -688,10 +688,10 @@ static void clearbykeys (global_State *g, GCObject *l) { Node *limit = gnodelast(h); Node *n; for (n = gnode(h, 0); n < limit; n++) { + if (iscleared(g, gckeyN(n))) /* unmarked key? */ + setempty(gval(n)); /* remove entry */ if (isempty(gval(n))) /* is entry empty? */ clearkey(n); /* clear its key */ - else if (iscleared(g, gckeyN(n))) /* unmarked key? */ - setempty(gval(n)); /* remove entry */ } } } From f055a9dffd9ba403a99266a662b9992bc89dcaa1 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Sun, 25 Feb 2018 09:43:52 -0300 Subject: [PATCH 0225/1145] added check in 'obj2gco' to prevent its use in non Lua-object pointers (otherwise its cast is blind, casting any value given to it) --- lstate.h | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/lstate.h b/lstate.h index 06581b017b..974249de3b 100644 --- a/lstate.h +++ b/lstate.h @@ -1,5 +1,5 @@ /* -** $Id: lstate.h,v 2.155 2018/02/15 18:06:24 roberto Exp roberto $ +** $Id: lstate.h,v 2.156 2018/02/17 19:29:29 roberto Exp roberto $ ** Global State ** See Copyright Notice in lua.h */ @@ -252,8 +252,11 @@ union GCUnion { #define gco2upv(o) check_exp((o)->tt == LUA_TUPVAL, &((cast_u(o))->upv)) -/* macro to convert a Lua object into a GCObject */ -#define obj2gco(v) (&(cast_u(v)->gc)) +/* +** macro to convert a Lua object into a GCObject +** (The access to 'tt' tries to ensure that 'v' is actually a Lua object.) +*/ +#define obj2gco(v) check_exp((v)->tt >= LUA_TSTRING, &(cast_u(v)->gc)) /* actual number of total bytes allocated */ From d766e2ae175495da85714d00e61d76174c5acc5b Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Sun, 25 Feb 2018 09:48:16 -0300 Subject: [PATCH 0226/1145] first (parcial) implementation of 'keyin'/'removekey' (still no metamethods, no raw verssions) --- lapi.c | 92 ++++++++++++++++++++++++++++++++++-------------------- lbaselib.c | 22 ++++++++++++- lobject.h | 17 +++++++--- ltablib.c | 6 ++-- lua.h | 4 ++- 5 files changed, 98 insertions(+), 43 deletions(-) diff --git a/lapi.c b/lapi.c index 0debc1d33f..b6c52721b4 100644 --- a/lapi.c +++ b/lapi.c @@ -1,5 +1,5 @@ /* -** $Id: lapi.c,v 2.285 2018/02/20 16:52:50 roberto Exp roberto $ +** $Id: lapi.c,v 2.286 2018/02/23 13:13:31 roberto Exp roberto $ ** Lua API ** See Copyright Notice in lua.h */ @@ -668,35 +668,65 @@ static int finishrawget (lua_State *L, const TValue *val) { } +static Table *gettable (lua_State *L, int idx) { + TValue *t = index2value(L, idx); + api_check(L, ttistable(t), "table expected"); + return hvalue(t); +} + + LUA_API int lua_rawget (lua_State *L, int idx) { - TValue *t; + Table *t; const TValue *val; lua_lock(L); - t = index2value(L, idx); - api_check(L, ttistable(t), "table expected"); - val = luaH_get(hvalue(t), s2v(L->top - 1)); + t = gettable(L, idx); + val = luaH_get(t, s2v(L->top - 1)); L->top--; /* remove key */ return finishrawget(L, val); } LUA_API int lua_rawgeti (lua_State *L, int idx, lua_Integer n) { - TValue *t; + Table *t; lua_lock(L); - t = index2value(L, idx); - api_check(L, ttistable(t), "table expected"); - return finishrawget(L, luaH_getint(hvalue(t), n)); + t = gettable(L, idx); + return finishrawget(L, luaH_getint(t, n)); } LUA_API int lua_rawgetp (lua_State *L, int idx, const void *p) { - TValue *t; + Table *t; TValue k; lua_lock(L); - t = index2value(L, idx); - api_check(L, ttistable(t), "table expected"); + t = gettable(L, idx); setpvalue(&k, cast_voidp(p)); - return finishrawget(L, luaH_get(hvalue(t), &k)); + return finishrawget(L, luaH_get(t, &k)); +} + + +static int auxkeyman (lua_State *L, int idx, int remove) { + Table *t; + const TValue *val; + int res; + lua_lock(L); + t = gettable(L, idx); + val = luaH_get(t, s2v(L->top - 1)); + L->top--; /* remove key */ + res = !isempty(val); + if (remove && res) /* key is present and should be removed? */ + setempty(cast(TValue*, val)); + lua_unlock(L); + return res; +} + + +LUA_API void lua_removekey (lua_State *L, int idx) { + auxkeyman(L, idx, 1); +} + + +LUA_API int lua_keyin (lua_State *L, int idx) { + return auxkeyman(L, idx, 0); } @@ -834,45 +864,42 @@ LUA_API void lua_seti (lua_State *L, int idx, lua_Integer n) { LUA_API void lua_rawset (lua_State *L, int idx) { - TValue *o; + Table *t; TValue *slot; lua_lock(L); api_checknelems(L, 2); - o = index2value(L, idx); - api_check(L, ttistable(o), "table expected"); - slot = luaH_set(L, hvalue(o), s2v(L->top - 2)); + t = gettable(L, idx); + slot = luaH_set(L, t, s2v(L->top - 2)); setobj2t(L, slot, s2v(L->top - 1)); - invalidateTMcache(hvalue(o)); - luaC_barrierback(L, gcvalue(o), s2v(L->top - 1)); + invalidateTMcache(t); + luaC_barrierback(L, obj2gco(t), s2v(L->top - 1)); L->top -= 2; lua_unlock(L); } LUA_API void lua_rawseti (lua_State *L, int idx, lua_Integer n) { - TValue *o; + Table *t; lua_lock(L); api_checknelems(L, 1); - o = index2value(L, idx); - api_check(L, ttistable(o), "table expected"); - luaH_setint(L, hvalue(o), n, s2v(L->top - 1)); - luaC_barrierback(L, gcvalue(o), s2v(L->top - 1)); + t = gettable(L, idx); + luaH_setint(L, t, n, s2v(L->top - 1)); + luaC_barrierback(L, obj2gco(t), s2v(L->top - 1)); L->top--; lua_unlock(L); } LUA_API void lua_rawsetp (lua_State *L, int idx, const void *p) { - TValue *o; + Table *t; TValue k, *slot; lua_lock(L); api_checknelems(L, 1); - o = index2value(L, idx); - api_check(L, ttistable(o), "table expected"); + t = gettable(L, idx); setpvalue(&k, cast_voidp(p)); - slot = luaH_set(L, hvalue(o), &k); + slot = luaH_set(L, t, &k); setobj2t(L, slot, s2v(L->top - 1)); - luaC_barrierback(L, gcvalue(o), s2v(L->top - 1)); + luaC_barrierback(L, obj2gco(t), s2v(L->top - 1)); L->top--; lua_unlock(L); } @@ -1193,12 +1220,11 @@ LUA_API int lua_error (lua_State *L) { LUA_API int lua_next (lua_State *L, int idx) { - TValue *t; + Table *t; int more; lua_lock(L); - t = index2value(L, idx); - api_check(L, ttistable(t), "table expected"); - more = luaH_next(L, hvalue(t), L->top - 1); + t = gettable(L, idx); + more = luaH_next(L, t, L->top - 1); if (more) { api_incr_top(L); } diff --git a/lbaselib.c b/lbaselib.c index c95a6e535c..443bf066da 100644 --- a/lbaselib.c +++ b/lbaselib.c @@ -1,5 +1,5 @@ /* -** $Id: lbaselib.c,v 1.318 2017/11/16 13:19:06 roberto Exp roberto $ +** $Id: lbaselib.c,v 1.319 2018/02/05 17:10:52 roberto Exp roberto $ ** Basic library ** See Copyright Notice in lua.h */ @@ -170,6 +170,24 @@ static int luaB_rawset (lua_State *L) { } +static int luaB_keyin (lua_State *L) { + luaL_checktype(L, 1, LUA_TTABLE); + luaL_checkany(L, 2); + lua_settop(L, 2); + lua_pushboolean(L, lua_keyin(L, 1)); + return 1; +} + + +static int luaB_removekey (lua_State *L) { + luaL_checktype(L, 1, LUA_TTABLE); + luaL_checkany(L, 2); + lua_settop(L, 2); + lua_removekey(L, 1); + return 0; +} + + static int pushmode (lua_State *L, int oldmode) { lua_pushstring(L, (oldmode == LUA_GCINC) ? "incremental" : "generational"); return 1; @@ -501,6 +519,8 @@ static const luaL_Reg base_funcs[] = { {"rawlen", luaB_rawlen}, {"rawget", luaB_rawget}, {"rawset", luaB_rawset}, + {"keyin", luaB_keyin}, + {"removekey", luaB_removekey}, {"select", luaB_select}, {"setmetatable", luaB_setmetatable}, {"tonumber", luaB_tonumber}, diff --git a/lobject.h b/lobject.h index c6590cf041..da1101282b 100644 --- a/lobject.h +++ b/lobject.h @@ -1,5 +1,5 @@ /* -** $Id: lobject.h,v 2.136 2018/02/22 17:28:10 roberto Exp roberto $ +** $Id: lobject.h,v 2.137 2018/02/23 13:13:31 roberto Exp roberto $ ** Type definitions for Lua objects ** See Copyright Notice in lua.h */ @@ -156,13 +156,20 @@ typedef StackValue *StkId; /* index to stack elements */ #define LUA_TEMPTY (LUA_TNIL | (1 << 4)) #define ttisnilorempty(v) checktype((v), LUA_TNIL) -/* -** By default, entries with any kind of nil are considered empty -*/ -#define isempty(v) ttisnilorempty(v) #define isreallyempty(v) checktag((v), LUA_TEMPTY) + +#if defined(LUA_NILINTABLE) + +#define isempty(v) isreallyempty(v) + +#else /* By default, entries with any kind of nil are considered empty */ + +#define isempty(v) ttisnilorempty(v) + +#endif + /* macro defining an empty value */ #define EMPTYCONSTANT {NULL}, LUA_TEMPTY diff --git a/ltablib.c b/ltablib.c index 588bf40d2f..982821edf0 100644 --- a/ltablib.c +++ b/ltablib.c @@ -1,5 +1,5 @@ /* -** $Id: ltablib.c,v 1.92 2016/02/08 12:55:19 roberto Exp roberto $ +** $Id: ltablib.c,v 1.93 2016/02/25 19:41:54 roberto Exp roberto $ ** Library for Table Manipulation ** See Copyright Notice in lua.h */ @@ -113,8 +113,8 @@ static int tremove (lua_State *L) { lua_geti(L, 1, pos + 1); lua_seti(L, 1, pos); /* t[pos] = t[pos + 1] */ } - lua_pushnil(L); - lua_seti(L, 1, pos); /* t[pos] = nil */ + lua_pushinteger(L, pos); + lua_removekey(L, 1); /* remove entry t[pos] */ return 1; } diff --git a/lua.h b/lua.h index 8fb2ccbe44..3c481a7023 100644 --- a/lua.h +++ b/lua.h @@ -1,5 +1,5 @@ /* -** $Id: lua.h,v 1.340 2018/02/17 19:29:29 roberto Exp roberto $ +** $Id: lua.h,v 1.341 2018/02/20 16:52:50 roberto Exp roberto $ ** Lua - A Scripting Language ** Lua.org, PUC-Rio, Brazil (http://www.lua.org) ** See Copyright Notice at the end of this file @@ -331,6 +331,8 @@ LUA_API size_t (lua_stringtonumber) (lua_State *L, const char *s); LUA_API lua_Alloc (lua_getallocf) (lua_State *L, void **ud); LUA_API void (lua_setallocf) (lua_State *L, lua_Alloc f, void *ud); +LUA_API void (lua_removekey) (lua_State *L, int idx); +LUA_API int (lua_keyin) (lua_State *L, int idx); /* From 75efc6722bf3bf31adffed44407674f74ac6caed Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Sun, 25 Feb 2018 09:52:32 -0300 Subject: [PATCH 0227/1145] avoid variant tags with the same value of the original type (to expose bugs more easily) --- lobject.h | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/lobject.h b/lobject.h index da1101282b..8e7f4101f8 100644 --- a/lobject.h +++ b/lobject.h @@ -1,5 +1,5 @@ /* -** $Id: lobject.h,v 2.137 2018/02/23 13:13:31 roberto Exp roberto $ +** $Id: lobject.h,v 2.138 2018/02/25 12:48:16 roberto Exp roberto $ ** Type definitions for Lua objects ** See Copyright Notice in lua.h */ @@ -267,8 +267,8 @@ typedef struct GCObject { */ /* Variant tags for numbers */ -#define LUA_TNUMFLT (LUA_TNUMBER | (0 << 4)) /* float numbers */ -#define LUA_TNUMINT (LUA_TNUMBER | (1 << 4)) /* integer numbers */ +#define LUA_TNUMFLT (LUA_TNUMBER | (1 << 4)) /* float numbers */ +#define LUA_TNUMINT (LUA_TNUMBER | (2 << 4)) /* integer numbers */ #define ttisnumber(o) checktype((o), LUA_TNUMBER) #define ttisfloat(o) checktag((o), LUA_TNUMFLT) @@ -304,8 +304,8 @@ typedef struct GCObject { */ /* Variant tags for strings */ -#define LUA_TSHRSTR (LUA_TSTRING | (0 << 4)) /* short strings */ -#define LUA_TLNGSTR (LUA_TSTRING | (1 << 4)) /* long strings */ +#define LUA_TSHRSTR (LUA_TSTRING | (1 << 4)) /* short strings */ +#define LUA_TLNGSTR (LUA_TSTRING | (2 << 4)) /* long strings */ #define ttisstring(o) checktype((o), LUA_TSTRING) #define ttisshrstring(o) checktag((o), ctb(LUA_TSHRSTR)) @@ -511,12 +511,12 @@ typedef struct Proto { */ /* Variant tags for functions */ -#define LUA_TLCL (LUA_TFUNCTION | (0 << 4)) /* Lua closure */ -#define LUA_TLCF (LUA_TFUNCTION | (1 << 4)) /* light C function */ -#define LUA_TCCL (LUA_TFUNCTION | (2 << 4)) /* C closure */ +#define LUA_TLCL (LUA_TFUNCTION | (1 << 4)) /* Lua closure */ +#define LUA_TLCF (LUA_TFUNCTION | (2 << 4)) /* light C function */ +#define LUA_TCCL (LUA_TFUNCTION | (3 << 4)) /* C closure */ #define ttisfunction(o) checktype(o, LUA_TFUNCTION) -#define ttisclosure(o) ((rttype(o) & 0x1F) == LUA_TFUNCTION) +#define ttisclosure(o) ((rttype(o) & 0x1F) == LUA_TLCL) #define ttisLclosure(o) checktag((o), ctb(LUA_TLCL)) #define ttislcf(o) checktag((o), LUA_TLCF) #define ttisCclosure(o) checktag((o), ctb(LUA_TCCL)) From 38d3bc89093010fa9c460b61a1965d9077b5378f Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Sun, 25 Feb 2018 10:40:00 -0300 Subject: [PATCH 0228/1145] using 'offsetof' to compute the size of parts of a structure --- lfunc.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lfunc.h b/lfunc.h index dbda3fdf49..74a9fc2e6e 100644 --- a/lfunc.h +++ b/lfunc.h @@ -1,5 +1,5 @@ /* -** $Id: lfunc.h,v 2.18 2017/06/29 15:06:44 roberto Exp roberto $ +** $Id: lfunc.h,v 2.19 2018/01/28 15:13:26 roberto Exp roberto $ ** Auxiliary functions to manipulate prototypes and closures ** See Copyright Notice in lua.h */ @@ -11,11 +11,11 @@ #include "lobject.h" -#define sizeCclosure(n) (cast_int(sizeof(CClosure)) + \ - cast_int(sizeof(TValue)*((n)-1))) +#define sizeCclosure(n) (cast_int(offsetof(CClosure, upvalue)) + \ + cast_int(sizeof(TValue)) * (n)) -#define sizeLclosure(n) (cast_int(sizeof(LClosure)) + \ - cast_int(sizeof(TValue *)*((n)-1))) +#define sizeLclosure(n) (cast_int(offsetof(LClosure, upvals)) + \ + cast_int(sizeof(TValue *)) * (n)) /* test whether thread is in 'twups' list */ From 2952bc5fc9cdc05ed061539cb7be26899513f004 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 26 Feb 2018 10:35:03 -0300 Subject: [PATCH 0229/1145] special compact representation for userdata with no user values (a common case) --- lgc.c | 19 ++++++++++++++++--- lobject.h | 37 ++++++++++++++++++++++++++++--------- 2 files changed, 44 insertions(+), 12 deletions(-) diff --git a/lgc.c b/lgc.c index 13f372d3bc..9da0f498e0 100644 --- a/lgc.c +++ b/lgc.c @@ -1,5 +1,5 @@ /* -** $Id: lgc.c,v 2.250 2018/02/23 13:16:18 roberto Exp roberto $ +** $Id: lgc.c,v 2.251 2018/02/23 13:21:27 roberto Exp roberto $ ** Garbage Collector ** See Copyright Notice in lua.h */ @@ -118,11 +118,15 @@ static lu_mem atomic (lua_State *L); static GCObject **getgclist (GCObject *o) { switch (o->tt) { case LUA_TTABLE: return &gco2t(o)->gclist; - case LUA_TUSERDATA: return &gco2u(o)->gclist; case LUA_TLCL: return &gco2lcl(o)->gclist; case LUA_TCCL: return &gco2ccl(o)->gclist; case LUA_TTHREAD: return &gco2th(o)->gclist; case LUA_TPROTO: return &gco2p(o)->gclist; + case LUA_TUSERDATA: { + Udata *u = gco2u(o); + lua_assert(u->nuvalue > 0); + return &u->gclist; + } default: lua_assert(0); return 0; } } @@ -290,8 +294,17 @@ static void reallymarkobject (global_State *g, GCObject *o) { markvalue(g, uv->v); /* mark its content */ break; } + case LUA_TUSERDATA: { + Udata *u = gco2u(o); + if (u->nuvalue == 0) { /* no user values? */ + markobjectN(g, u->metatable); /* mark its metatable */ + gray2black(o); /* nothing else to mark */ + break; + } + /* else *//* FALLTHROUGH */ + } case LUA_TLCL: case LUA_TCCL: case LUA_TTABLE: - case LUA_TUSERDATA: case LUA_TTHREAD: case LUA_TPROTO: { + case LUA_TTHREAD: case LUA_TPROTO: { linkobjgclist(o, g->gray); break; } diff --git a/lobject.h b/lobject.h index 8e7f4101f8..6d34759f7c 100644 --- a/lobject.h +++ b/lobject.h @@ -1,5 +1,5 @@ /* -** $Id: lobject.h,v 2.138 2018/02/25 12:48:16 roberto Exp roberto $ +** $Id: lobject.h,v 2.139 2018/02/25 12:52:32 roberto Exp roberto $ ** Type definitions for Lua objects ** See Copyright Notice in lua.h */ @@ -70,7 +70,7 @@ typedef struct TValue { #define rttype(o) ((o)->tt_) /* tag with no variants (bits 0-3) */ -#define novariant(x) ((x) & 0x0F) +#define novariant(t) ((t) & 0x0F) /* type tag of a TValue (bits 0-3 for tags + variant bits 4-5) */ #define ttyperaw(t) ((t) & 0x3F) @@ -379,7 +379,7 @@ typedef union UTString { */ #define ttislightuserdata(o) checktag((o), LUA_TLIGHTUSERDATA) -#define ttisfulluserdata(o) checktag((o), ctb(LUA_TUSERDATA)) +#define ttisfulluserdata(o) checktype((o), LUA_TUSERDATA) #define pvalue(o) check_exp(ttislightuserdata(o), val_(o).p) #define uvalue(o) check_exp(ttisfulluserdata(o), gco2u(val_(o).gc)) @@ -403,7 +403,8 @@ typedef union UValue { /* -** Header for userdata; memory area follows the end of this structure. +** Header for userdata with user values; +** memory area follows the end of this structure. */ typedef struct Udata { CommonHeader; @@ -415,15 +416,33 @@ typedef struct Udata { } Udata; -/* computes the offset of the memory area of a userdata */ -#define udatamemoffset(nuv) (sizeof(Udata) + (sizeof(UValue) * ((nuv) - 1))) - /* -** Get the address of the memory block inside 'Udata'. +** Header for userdata with no user values. These userdata do not need +** to be gray during GC, and therefore do not need a 'gclist' field. +** To simplify, the code always use 'Udata' for both kinds of userdata, +** making sure it never accesses 'gclist' on userdata with no user values. +** This structure here is used only to compute the correct size for +** this representation. (The 'bindata' field in its end ensures correct +** alignment for binary data following this header.) */ +typedef struct Udata0 { + CommonHeader; + unsigned short nuvalue; /* number of user values */ + size_t len; /* number of bytes */ + struct Table *metatable; + union {LUAI_MAXALIGN;} bindata; +} Udata0; + + +/* compute the offset of the memory area of a userdata */ +#define udatamemoffset(nuv) \ + ((nuv) == 0 ? offsetof(Udata0, bindata) \ + : offsetof(Udata, uv) + (sizeof(UValue) * (nuv))) + +/* get the address of the memory block inside 'Udata' */ #define getudatamem(u) (cast_charp(u) + udatamemoffset((u)->nuvalue)) -/* computes the size of a userdata */ +/* compute the size of a userdata */ #define sizeudata(nuv,nb) (udatamemoffset(nuv) + (nb)) /* }================================================================== */ From ef8263f81fdde2310ebb15c9a3fe5e954d57cab5 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 26 Feb 2018 11:16:05 -0300 Subject: [PATCH 0230/1145] better names for macros for tags and types. rttype -> rawtt; ttyperaw -> withvariant; ttype -> ttypetag; tnov -> ttype --- lapi.c | 32 ++++++++++++++++---------------- lcode.c | 4 ++-- ldo.c | 4 ++-- ldump.c | 6 +++--- lobject.h | 22 +++++++++++----------- ltable.c | 12 ++++++------ ltm.c | 8 ++++---- lvm.c | 10 +++++----- 8 files changed, 49 insertions(+), 49 deletions(-) diff --git a/lapi.c b/lapi.c index b6c52721b4..92e98def30 100644 --- a/lapi.c +++ b/lapi.c @@ -1,5 +1,5 @@ /* -** $Id: lapi.c,v 2.286 2018/02/23 13:13:31 roberto Exp roberto $ +** $Id: lapi.c,v 2.287 2018/02/25 12:48:16 roberto Exp roberto $ ** Lua API ** See Copyright Notice in lua.h */ @@ -257,7 +257,7 @@ LUA_API void lua_pushvalue (lua_State *L, int idx) { LUA_API int lua_type (lua_State *L, int idx) { const TValue *o = index2value(L, idx); - return (isvalid(o) ? ttnov(o) : LUA_TNONE); + return (isvalid(o) ? ttype(o) : LUA_TNONE); } @@ -399,7 +399,7 @@ LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) { LUA_API lua_Unsigned lua_rawlen (lua_State *L, int idx) { const TValue *o = index2value(L, idx); - switch (ttype(o)) { + switch (ttypetag(o)) { case LUA_TSHRSTR: return tsvalue(o)->shrlen; case LUA_TLNGSTR: return tsvalue(o)->u.lnglen; case LUA_TUSERDATA: return uvalue(o)->len; @@ -420,7 +420,7 @@ LUA_API lua_CFunction lua_tocfunction (lua_State *L, int idx) { LUA_API void *lua_touserdata (lua_State *L, int idx) { const TValue *o = index2value(L, idx); - switch (ttnov(o)) { + switch (ttype(o)) { case LUA_TUSERDATA: return getudatamem(uvalue(o)); case LUA_TLIGHTUSERDATA: return pvalue(o); default: return NULL; @@ -436,7 +436,7 @@ LUA_API lua_State *lua_tothread (lua_State *L, int idx) { LUA_API const void *lua_topointer (lua_State *L, int idx) { const TValue *o = index2value(L, idx); - switch (ttype(o)) { + switch (ttypetag(o)) { case LUA_TTABLE: return hvalue(o); case LUA_TLCL: return clLvalue(o); case LUA_TCCL: return clCvalue(o); @@ -606,7 +606,7 @@ static int auxgetstr (lua_State *L, const TValue *t, const char *k) { luaV_finishget(L, t, s2v(L->top - 1), L->top - 1, slot); } lua_unlock(L); - return ttnov(s2v(L->top - 1)); + return ttype(s2v(L->top - 1)); } @@ -628,7 +628,7 @@ LUA_API int lua_gettable (lua_State *L, int idx) { else luaV_finishget(L, t, s2v(L->top - 1), L->top - 1, slot); lua_unlock(L); - return ttnov(s2v(L->top - 1)); + return ttype(s2v(L->top - 1)); } @@ -653,7 +653,7 @@ LUA_API int lua_geti (lua_State *L, int idx, lua_Integer n) { } api_incr_top(L); lua_unlock(L); - return ttnov(s2v(L->top - 1)); + return ttype(s2v(L->top - 1)); } @@ -664,7 +664,7 @@ static int finishrawget (lua_State *L, const TValue *val) { setobj2s(L, L->top, val); api_incr_top(L); lua_unlock(L); - return ttnov(s2v(L->top - 1)); + return ttype(s2v(L->top - 1)); } @@ -749,7 +749,7 @@ LUA_API int lua_getmetatable (lua_State *L, int objindex) { int res = 0; lua_lock(L); obj = index2value(L, objindex); - switch (ttnov(obj)) { + switch (ttype(obj)) { case LUA_TTABLE: mt = hvalue(obj)->metatable; break; @@ -757,7 +757,7 @@ LUA_API int lua_getmetatable (lua_State *L, int objindex) { mt = uvalue(obj)->metatable; break; default: - mt = G(L)->mt[ttnov(obj)]; + mt = G(L)->mt[ttype(obj)]; break; } if (mt != NULL) { @@ -782,7 +782,7 @@ LUA_API int lua_getiuservalue (lua_State *L, int idx, int n) { } else { setobj2s(L, L->top, &uvalue(o)->uv[n - 1].uv); - t = ttnov(s2v(L->top)); + t = ttype(s2v(L->top)); } api_incr_top(L); lua_unlock(L); @@ -917,7 +917,7 @@ LUA_API int lua_setmetatable (lua_State *L, int objindex) { api_check(L, ttistable(s2v(L->top - 1)), "table expected"); mt = hvalue(s2v(L->top - 1)); } - switch (ttnov(obj)) { + switch (ttype(obj)) { case LUA_TTABLE: { hvalue(obj)->metatable = mt; if (mt) { @@ -935,7 +935,7 @@ LUA_API int lua_setmetatable (lua_State *L, int objindex) { break; } default: { - G(L)->mt[ttnov(obj)] = mt; + G(L)->mt[ttype(obj)] = mt; break; } } @@ -1295,7 +1295,7 @@ LUA_API void *lua_newuserdatauv (lua_State *L, size_t size, int nuvalue) { static const char *aux_upvalue (TValue *fi, int n, TValue **val, GCObject **owner) { - switch (ttype(fi)) { + switch (ttypetag(fi)) { case LUA_TCCL: { /* C closure */ CClosure *f = clCvalue(fi); if (!(1 <= n && n <= f->nupvalues)) return NULL; @@ -1364,7 +1364,7 @@ static UpVal **getupvalref (lua_State *L, int fidx, int n, LClosure **pf) { LUA_API void *lua_upvalueid (lua_State *L, int fidx, int n) { TValue *fi = index2value(L, fidx); - switch (ttype(fi)) { + switch (ttypetag(fi)) { case LUA_TLCL: { /* lua closure */ return *getupvalref(L, fidx, n, NULL); } diff --git a/lcode.c b/lcode.c index 30bf13d813..8f3c68c06f 100644 --- a/lcode.c +++ b/lcode.c @@ -1,5 +1,5 @@ /* -** $Id: lcode.c,v 2.156 2018/02/21 12:54:26 roberto Exp roberto $ +** $Id: lcode.c,v 2.157 2018/02/21 15:49:32 roberto Exp roberto $ ** Code generator for Lua ** See Copyright Notice in lua.h */ @@ -504,7 +504,7 @@ static int addk (FuncState *fs, TValue *key, TValue *v) { if (ttisinteger(idx)) { /* is there an index there? */ k = cast_int(ivalue(idx)); /* correct value? (warning: must distinguish floats from integers!) */ - if (k < fs->nk && ttype(&f->k[k]) == ttype(v) && + if (k < fs->nk && ttypetag(&f->k[k]) == ttypetag(v) && luaV_rawequalobj(&f->k[k], v)) return k; /* reuse index */ } diff --git a/ldo.c b/ldo.c index 6acd2f645b..7489a9621a 100644 --- a/ldo.c +++ b/ldo.c @@ -1,5 +1,5 @@ /* -** $Id: ldo.c,v 2.194 2018/02/15 15:34:29 roberto Exp roberto $ +** $Id: ldo.c,v 2.196 2018/02/17 19:29:29 roberto Exp roberto $ ** Stack and Call structure of Lua ** See Copyright Notice in lua.h */ @@ -453,7 +453,7 @@ void luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, int narg1) { void luaD_call (lua_State *L, StkId func, int nresults) { lua_CFunction f; TValue *funcv = s2v(func); - switch (ttype(funcv)) { + switch (ttypetag(funcv)) { case LUA_TCCL: /* C closure */ f = clCvalue(funcv)->f; goto Cfunc; diff --git a/ldump.c b/ldump.c index 4d3f643574..0724aeb5da 100644 --- a/ldump.c +++ b/ldump.c @@ -1,5 +1,5 @@ /* -** $Id: ldump.c,v 2.39 2017/06/27 14:21:12 roberto Exp roberto $ +** $Id: ldump.c,v 2.40 2017/11/28 11:19:07 roberto Exp roberto $ ** save precompiled Lua chunks ** See Copyright Notice in lua.h */ @@ -111,8 +111,8 @@ static void DumpConstants (const Proto *f, DumpState *D) { DumpInt(n, D); for (i = 0; i < n; i++) { const TValue *o = &f->k[i]; - DumpByte(ttype(o), D); - switch (ttype(o)) { + DumpByte(ttypetag(o), D); + switch (ttypetag(o)) { case LUA_TNIL: break; case LUA_TBOOLEAN: diff --git a/lobject.h b/lobject.h index 6d34759f7c..b18dce4710 100644 --- a/lobject.h +++ b/lobject.h @@ -1,5 +1,5 @@ /* -** $Id: lobject.h,v 2.139 2018/02/25 12:52:32 roberto Exp roberto $ +** $Id: lobject.h,v 2.140 2018/02/26 13:35:03 roberto Exp roberto $ ** Type definitions for Lua objects ** See Copyright Notice in lua.h */ @@ -67,26 +67,26 @@ typedef struct TValue { /* raw type tag of a TValue */ -#define rttype(o) ((o)->tt_) +#define rawtt(o) ((o)->tt_) /* tag with no variants (bits 0-3) */ #define novariant(t) ((t) & 0x0F) /* type tag of a TValue (bits 0-3 for tags + variant bits 4-5) */ -#define ttyperaw(t) ((t) & 0x3F) -#define ttype(o) ttyperaw(rttype(o)) +#define withvariant(t) ((t) & 0x3F) +#define ttypetag(o) withvariant(rawtt(o)) -/* type tag of a TValue with no variants (bits 0-3) */ -#define ttnov(o) (novariant(rttype(o))) +/* type of a TValue */ +#define ttype(o) (novariant(rawtt(o))) /* Macros to test type */ -#define checktag(o,t) (rttype(o) == (t)) -#define checktype(o,t) (ttnov(o) == (t)) +#define checktag(o,t) (rawtt(o) == (t)) +#define checktype(o,t) (ttype(o) == (t)) /* Macros for internal tests */ -#define righttt(obj) (ttype(obj) == gcvalue(obj)->tt) +#define righttt(obj) (ttypetag(obj) == gcvalue(obj)->tt) #define checkliveness(L,obj) \ lua_longassert(!iscollectable(obj) || \ @@ -244,7 +244,7 @@ typedef struct GCObject { /* Bit mark for collectable types */ #define BIT_ISCOLLECTABLE (1 << 6) -#define iscollectable(o) (rttype(o) & BIT_ISCOLLECTABLE) +#define iscollectable(o) (rawtt(o) & BIT_ISCOLLECTABLE) /* mark a tag as collectable */ #define ctb(t) ((t) | BIT_ISCOLLECTABLE) @@ -535,7 +535,7 @@ typedef struct Proto { #define LUA_TCCL (LUA_TFUNCTION | (3 << 4)) /* C closure */ #define ttisfunction(o) checktype(o, LUA_TFUNCTION) -#define ttisclosure(o) ((rttype(o) & 0x1F) == LUA_TLCL) +#define ttisclosure(o) ((rawtt(o) & 0x1F) == LUA_TLCL) #define ttisLclosure(o) checktag((o), ctb(LUA_TLCL)) #define ttislcf(o) checktag((o), LUA_TLCF) #define ttisCclosure(o) checktag((o), ctb(LUA_TCCL)) diff --git a/ltable.c b/ltable.c index c71d627adc..be8f67f0f9 100644 --- a/ltable.c +++ b/ltable.c @@ -1,5 +1,5 @@ /* -** $Id: ltable.c,v 2.133 2018/02/21 12:54:26 roberto Exp roberto $ +** $Id: ltable.c,v 2.134 2018/02/23 13:13:31 roberto Exp roberto $ ** Lua tables (hash) ** See Copyright Notice in lua.h */ @@ -133,7 +133,7 @@ static int l_hashfloat (lua_Number n) { ** nodes. */ static Node *mainposition (const Table *t, int ktt, const Value *kvl) { - switch (ttyperaw(ktt)) { + switch (withvariant(ktt)) { case LUA_TNUMINT: return hashint(t, ivalueraw(*kvl)); case LUA_TNUMFLT: @@ -155,7 +155,7 @@ static Node *mainposition (const Table *t, int ktt, const Value *kvl) { static Node *mainpositionTV (const Table *t, const TValue *key) { - return mainposition(t, rttype(key), valraw(key)); + return mainposition(t, rawtt(key), valraw(key)); } @@ -168,9 +168,9 @@ static Node *mainpositionTV (const Table *t, const TValue *key) { ** default case. */ static int equalkey (const TValue *k1, const Node *n2) { - if (rttype(k1) != keytt(n2)) /* not the same variants? */ + if (rawtt(k1) != keytt(n2)) /* not the same variants? */ return 0; /* cannot be same key */ - switch (ttype(k1)) { + switch (ttypetag(k1)) { case LUA_TNIL: return 1; case LUA_TNUMINT: @@ -667,7 +667,7 @@ const TValue *luaH_getstr (Table *t, TString *key) { ** main search function */ const TValue *luaH_get (Table *t, const TValue *key) { - switch (ttype(key)) { + switch (ttypetag(key)) { case LUA_TSHRSTR: return luaH_getshortstr(t, tsvalue(key)); case LUA_TNUMINT: return luaH_getint(t, ivalue(key)); case LUA_TNIL: return luaH_emptyobject; diff --git a/ltm.c b/ltm.c index 0187c6f59c..c06c8296a1 100644 --- a/ltm.c +++ b/ltm.c @@ -1,5 +1,5 @@ /* -** $Id: ltm.c,v 2.63 2018/02/21 15:49:32 roberto Exp roberto $ +** $Id: ltm.c,v 2.64 2018/02/23 13:13:31 roberto Exp roberto $ ** Tag methods ** See Copyright Notice in lua.h */ @@ -70,7 +70,7 @@ const TValue *luaT_gettm (Table *events, TMS event, TString *ename) { const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, TMS event) { Table *mt; - switch (ttnov(o)) { + switch (ttype(o)) { case LUA_TTABLE: mt = hvalue(o)->metatable; break; @@ -78,7 +78,7 @@ const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, TMS event) { mt = uvalue(o)->metatable; break; default: - mt = G(L)->mt[ttnov(o)]; + mt = G(L)->mt[ttype(o)]; } return (mt ? luaH_getshortstr(mt, G(L)->tmname[event]) : luaO_nilobject); } @@ -96,7 +96,7 @@ const char *luaT_objtypename (lua_State *L, const TValue *o) { if (ttisstring(name)) /* is '__name' a string? */ return getstr(tsvalue(name)); /* use it as type name */ } - return ttypename(ttnov(o)); /* else use standard type name */ + return ttypename(ttype(o)); /* else use standard type name */ } diff --git a/lvm.c b/lvm.c index 99e0d6b779..cd54369ef6 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.346 2018/02/21 19:43:44 roberto Exp roberto $ +** $Id: lvm.c,v 2.347 2018/02/23 13:13:31 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -458,8 +458,8 @@ int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r) { */ int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2) { const TValue *tm; - if (ttype(t1) != ttype(t2)) { /* not the same variant? */ - if (ttnov(t1) != ttnov(t2) || ttnov(t1) != LUA_TNUMBER) + if (ttypetag(t1) != ttypetag(t2)) { /* not the same variant? */ + if (ttype(t1) != ttype(t2) || ttype(t1) != LUA_TNUMBER) return 0; /* only numbers can be equal with different variants */ else { /* two numbers with different variants */ lua_Integer i1, i2; /* compare them as integers */ @@ -467,7 +467,7 @@ int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2) { } } /* values have same type and same variant */ - switch (ttype(t1)) { + switch (ttypetag(t1)) { case LUA_TNIL: return 1; case LUA_TNUMINT: return (ivalue(t1) == ivalue(t2)); case LUA_TNUMFLT: return luai_numeq(fltvalue(t1), fltvalue(t2)); @@ -569,7 +569,7 @@ void luaV_concat (lua_State *L, int total) { */ void luaV_objlen (lua_State *L, StkId ra, const TValue *rb) { const TValue *tm; - switch (ttype(rb)) { + switch (ttypetag(rb)) { case LUA_TTABLE: { Table *h = hvalue(rb); tm = fasttm(L, h->metatable, TM_LEN); From b7edf5d2d89ed2ce1e9087de496bcb451e39d131 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 27 Feb 2018 14:48:28 -0300 Subject: [PATCH 0231/1145] metamethods for 'removekey'/'keyin' --- lapi.c | 19 ++++++++----------- lbaselib.c | 8 +++----- ltm.c | 31 ++++++++++++++++++++++++++++++- ltm.h | 6 +++++- 4 files changed, 46 insertions(+), 18 deletions(-) diff --git a/lapi.c b/lapi.c index 92e98def30..7ecf68cb7e 100644 --- a/lapi.c +++ b/lapi.c @@ -1,5 +1,5 @@ /* -** $Id: lapi.c,v 2.287 2018/02/25 12:48:16 roberto Exp roberto $ +** $Id: lapi.c,v 2.288 2018/02/26 14:16:05 roberto Exp roberto $ ** Lua API ** See Copyright Notice in lua.h */ @@ -679,6 +679,7 @@ LUA_API int lua_rawget (lua_State *L, int idx) { Table *t; const TValue *val; lua_lock(L); + api_checknelems(L, 1); t = gettable(L, idx); val = luaH_get(t, s2v(L->top - 1)); L->top--; /* remove key */ @@ -704,29 +705,24 @@ LUA_API int lua_rawgetp (lua_State *L, int idx, const void *p) { } -static int auxkeyman (lua_State *L, int idx, int remove) { - Table *t; - const TValue *val; +static int auxkeydef (lua_State *L, int idx, int remove) { int res; lua_lock(L); - t = gettable(L, idx); - val = luaH_get(t, s2v(L->top - 1)); + api_checknelems(L, 1); + res = luaT_keydef(L, index2value(L, idx), s2v(L->top - 1), remove); L->top--; /* remove key */ - res = !isempty(val); - if (remove && res) /* key is present and should be removed? */ - setempty(cast(TValue*, val)); lua_unlock(L); return res; } LUA_API void lua_removekey (lua_State *L, int idx) { - auxkeyman(L, idx, 1); + auxkeydef(L, idx, 1); } LUA_API int lua_keyin (lua_State *L, int idx) { - return auxkeyman(L, idx, 0); + return auxkeydef(L, idx, 0); } @@ -1223,6 +1219,7 @@ LUA_API int lua_next (lua_State *L, int idx) { Table *t; int more; lua_lock(L); + api_checknelems(L, 1); t = gettable(L, idx); more = luaH_next(L, t, L->top - 1); if (more) { diff --git a/lbaselib.c b/lbaselib.c index 443bf066da..8e0338655a 100644 --- a/lbaselib.c +++ b/lbaselib.c @@ -1,5 +1,5 @@ /* -** $Id: lbaselib.c,v 1.319 2018/02/05 17:10:52 roberto Exp roberto $ +** $Id: lbaselib.c,v 1.320 2018/02/25 12:48:16 roberto Exp roberto $ ** Basic library ** See Copyright Notice in lua.h */ @@ -171,8 +171,7 @@ static int luaB_rawset (lua_State *L) { static int luaB_keyin (lua_State *L) { - luaL_checktype(L, 1, LUA_TTABLE); - luaL_checkany(L, 2); + luaL_checkany(L, 2); /* ensures a first argument too */ lua_settop(L, 2); lua_pushboolean(L, lua_keyin(L, 1)); return 1; @@ -180,8 +179,7 @@ static int luaB_keyin (lua_State *L) { static int luaB_removekey (lua_State *L) { - luaL_checktype(L, 1, LUA_TTABLE); - luaL_checkany(L, 2); + luaL_checkany(L, 2); /* ensures a first argument too */ lua_settop(L, 2); lua_removekey(L, 1); return 0; diff --git a/ltm.c b/ltm.c index c06c8296a1..2a9a4cbe4e 100644 --- a/ltm.c +++ b/ltm.c @@ -1,5 +1,5 @@ /* -** $Id: ltm.c,v 2.64 2018/02/23 13:13:31 roberto Exp roberto $ +** $Id: ltm.c,v 2.65 2018/02/26 14:16:05 roberto Exp roberto $ ** Tag methods ** See Copyright Notice in lua.h */ @@ -38,6 +38,7 @@ LUAI_DDEF const char *const luaT_typenames_[LUA_TOTALTAGS] = { void luaT_init (lua_State *L) { static const char *const luaT_eventname[] = { /* ORDER TM */ "__index", "__newindex", + "__undef", "__isdef", "__gc", "__mode", "__len", "__eq", "__add", "__sub", "__mul", "__mod", "__pow", "__div", "__idiv", @@ -248,3 +249,31 @@ void luaT_getvarargs (lua_State *L, CallInfo *ci, StkId where, int wanted) { for (; i < wanted; i++) /* complete required results with nil */ setnilvalue(s2v(where + i)); } + + +int luaT_keydef (lua_State *L, TValue *obj, TValue *key, int remove) { + const TValue *tm; + TMS event = remove ? TM_UNDEF : TM_ISDEF; + if (!ttistable(obj)) { /* not a table? */ + tm = luaT_gettmbyobj(L, obj, event); /* get its metamethod */ + if (notm(tm)) { /* no metamethod? */ + const char *msg = remove ? "remove key from" : "check key from"; + luaG_typeerror(L, obj, msg); /* error */ + } + /* else will call metamethod 'tm' */ + } + else { /* 'obj' is a table */ + Table *t = hvalue(obj); + tm = fasttm(L, t->metatable, event); + if (tm == NULL) { /* no metamethod? */ + const TValue *val = luaH_get(t, key); /* get entry */ + int res = !isempty(val); /* true if entry is not empty */ + if (remove && res) /* key is present and should be removed? */ + setempty(cast(TValue*, val)); /* remove it */ + return res; + } + /* else will call metamethod 'tm' */ + } + luaT_callTMres(L, tm, obj, key, L->top); + return !l_isfalse(s2v(L->top)); +} diff --git a/ltm.h b/ltm.h index 9b25ef083b..6e961d27b0 100644 --- a/ltm.h +++ b/ltm.h @@ -1,5 +1,5 @@ /* -** $Id: ltm.h,v 2.32 2018/02/17 19:20:00 roberto Exp roberto $ +** $Id: ltm.h,v 2.33 2018/02/23 13:13:31 roberto Exp roberto $ ** Tag methods ** See Copyright Notice in lua.h */ @@ -19,6 +19,8 @@ typedef enum { TM_INDEX, TM_NEWINDEX, + TM_UNDEF, + TM_ISDEF, TM_GC, TM_MODE, TM_LEN, @@ -89,5 +91,7 @@ LUAI_FUNC void luaT_adjustvarargs (lua_State *L, int nfixparams, LUAI_FUNC void luaT_getvarargs (lua_State *L, struct CallInfo *ci, StkId where, int wanted); +LUAI_FUNC int luaT_keydef (lua_State *L, TValue *obj, TValue *key, int remove); + #endif From 76ff55750ebbf48dcd687af08ee054d182961166 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 27 Feb 2018 15:24:23 -0300 Subject: [PATCH 0232/1145] no optimizations in test mode + no more compat with 5.2 + a few more options in comments --- makefile | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/makefile b/makefile index 8160d4fb10..8bc386df10 100644 --- a/makefile +++ b/makefile @@ -16,12 +16,12 @@ CWARNSCPP= \ -Wdisabled-optimization \ -Waggregate-return \ -Wdouble-promotion \ - #-Wno-aggressive-loop-optimizations # not accepted by clang \ - #-Wlogical-op # not accepted by clang \ + -Wstrict-aliasing=3 # not accepted by clang \ + -Wno-aggressive-loop-optimizations # not accepted by clang \ + -Wlogical-op # not accepted by clang \ # the next warnings generate too much noise, so they are disabled # -Wconversion -Wno-sign-conversion \ # -Wsign-conversion \ - # -Wconversion \ # -Wstrict-overflow=2 \ # -Wformat=2 \ # -Wcast-qual \ @@ -35,7 +35,7 @@ CWARNSC= -Wdeclaration-after-statement \ -Wold-style-definition \ -CWARNS= $(CWARNSCPP) $(CWARNSC) +CWARNS= $(CWARNSCPP) $(CWARNSC) # -DEXTERNMEMCHECK -DHARDSTACKTESTS -DHARDMEMTESTS -DTRACEMEM='"tempmem"' @@ -43,23 +43,24 @@ CWARNS= $(CWARNSCPP) $(CWARNSC) # -pg -malign-double # -DLUA_USE_CTYPE -DLUA_USE_APICHECK # (in clang, '-ftrapv' for runtime checks of integer overflows) -# -fsanitize=undefined -ftrapv -TESTS= -DLUA_USER_H='"ltests.h"' +# -fsanitize=undefined -ftrapv -fno-inline +TESTS= -DLUA_USER_H='"ltests.h"' -O0 # -mtune=native -fomit-frame-pointer # -fno-stack-protector -LOCAL = $(TESTS) $(CWARNS) -g +# -DLUA_NILINTABLE +LOCAL = $(TESTS) $(CWARNS) -g -DEXTERNMEMCHECK # enable Linux goodies -MYCFLAGS= $(LOCAL) -std=c99 -DLUA_USE_LINUX -DLUA_COMPAT_5_2 +MYCFLAGS= $(LOCAL) -std=c99 -DLUA_USE_LINUX MYLDFLAGS= $(LOCAL) -Wl,-E MYLIBS= -ldl -lreadline -CC= clang-3.8 -CFLAGS= -Wall -O2 $(MYCFLAGS) +CC= gcc +CFLAGS= -Wall -O2 $(MYCFLAGS) -Wfatal-errors AR= ar rcu RANLIB= ranlib RM= rm -f From 4105cafb84fe33b937eee8a3208b652227fcacf4 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 27 Feb 2018 15:27:30 -0300 Subject: [PATCH 0233/1145] no more 'bitlib' --- makefile | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/makefile b/makefile index 8bc386df10..1a98ccd92b 100644 --- a/makefile +++ b/makefile @@ -78,7 +78,7 @@ CORE_O= lapi.o lcode.o lctype.o ldebug.o ldo.o ldump.o lfunc.o lgc.o llex.o \ ltm.o lundump.o lvm.o lzio.o ltests.o AUX_O= lauxlib.o LIB_O= lbaselib.o ldblib.o liolib.o lmathlib.o loslib.o ltablib.o lstrlib.o \ - lutf8lib.o lbitlib.o loadlib.o lcorolib.o linit.o + lutf8lib.o loadlib.o lcorolib.o linit.o LUA_T= lua LUA_O= lua.o @@ -134,7 +134,6 @@ lapi.o: lapi.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \ ltable.h lundump.h lvm.h lauxlib.o: lauxlib.c lprefix.h lua.h luaconf.h lauxlib.h lbaselib.o: lbaselib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h -lbitlib.o: lbitlib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h lcode.o: lcode.c lprefix.h lua.h luaconf.h lcode.h llex.h lobject.h \ llimits.h lzio.h lmem.h lopcodes.h lparser.h ldebug.h lstate.h ltm.h \ ldo.h lgc.h lstring.h ltable.h lvm.h @@ -184,7 +183,7 @@ ltests.o: ltests.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \ lparser.h lctype.h ldebug.h ldo.h lfunc.h lstring.h lgc.h ltable.h \ lualib.h ltm.o: ltm.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \ - llimits.h ltm.h lzio.h lmem.h ldo.h lstring.h lgc.h ltable.h lvm.h + llimits.h ltm.h lzio.h lmem.h ldo.h lgc.h lstring.h ltable.h lvm.h lua.o: lua.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h lundump.o: lundump.c lprefix.h lua.h luaconf.h ldebug.h lstate.h \ lobject.h llimits.h ltm.h lzio.h lmem.h ldo.h lfunc.h lstring.h lgc.h \ From 12110dec0eda3813b7609051aedb0cde932fbf93 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 27 Feb 2018 15:46:26 -0300 Subject: [PATCH 0234/1145] module has been deprecated. --- lbitlib.c | 230 +----------------------------------------------------- 1 file changed, 2 insertions(+), 228 deletions(-) diff --git a/lbitlib.c b/lbitlib.c index 63dbbe1d1b..b9c33c6511 100644 --- a/lbitlib.c +++ b/lbitlib.c @@ -1,233 +1,7 @@ /* -** $Id: lbitlib.c,v 1.30 2015/11/11 19:08:09 roberto Exp roberto $ +** $Id: lbitlib.c,v 1.31 2017/11/16 13:19:06 roberto Exp roberto $ ** Standard library for bitwise operations ** See Copyright Notice in lua.h */ -#define lbitlib_c -#define LUA_LIB - -#include "lprefix.h" - - -#include "lua.h" - -#include "lauxlib.h" -#include "lualib.h" - - -#if defined(LUA_COMPAT_BITLIB) /* { */ - - -#define pushunsigned(L,n) lua_pushinteger(L, (lua_Integer)(n)) -#define checkunsigned(L,i) ((lua_Unsigned)luaL_checkinteger(L,i)) - - -/* number of bits to consider in a number */ -#if !defined(LUA_NBITS) -#define LUA_NBITS 32 -#endif - - -/* -** a lua_Unsigned with its first LUA_NBITS bits equal to 1. (Shift must -** be made in two parts to avoid problems when LUA_NBITS is equal to the -** number of bits in a lua_Unsigned.) -*/ -#define ALLONES (~(((~(lua_Unsigned)0) << (LUA_NBITS - 1)) << 1)) - - -/* macro to trim extra bits */ -#define trim(x) ((x) & ALLONES) - - -/* builds a number with 'n' ones (1 <= n <= LUA_NBITS) */ -#define mask(n) (~((ALLONES << 1) << ((n) - 1))) - - - -static lua_Unsigned andaux (lua_State *L) { - int i, n = lua_gettop(L); - lua_Unsigned r = ~(lua_Unsigned)0; - for (i = 1; i <= n; i++) - r &= checkunsigned(L, i); - return trim(r); -} - - -static int b_and (lua_State *L) { - lua_Unsigned r = andaux(L); - pushunsigned(L, r); - return 1; -} - - -static int b_test (lua_State *L) { - lua_Unsigned r = andaux(L); - lua_pushboolean(L, r != 0); - return 1; -} - - -static int b_or (lua_State *L) { - int i, n = lua_gettop(L); - lua_Unsigned r = 0; - for (i = 1; i <= n; i++) - r |= checkunsigned(L, i); - pushunsigned(L, trim(r)); - return 1; -} - - -static int b_xor (lua_State *L) { - int i, n = lua_gettop(L); - lua_Unsigned r = 0; - for (i = 1; i <= n; i++) - r ^= checkunsigned(L, i); - pushunsigned(L, trim(r)); - return 1; -} - - -static int b_not (lua_State *L) { - lua_Unsigned r = ~checkunsigned(L, 1); - pushunsigned(L, trim(r)); - return 1; -} - - -static int b_shift (lua_State *L, lua_Unsigned r, lua_Integer i) { - if (i < 0) { /* shift right? */ - i = -i; - r = trim(r); - if (i >= LUA_NBITS) r = 0; - else r >>= i; - } - else { /* shift left */ - if (i >= LUA_NBITS) r = 0; - else r <<= i; - r = trim(r); - } - pushunsigned(L, r); - return 1; -} - - -static int b_lshift (lua_State *L) { - return b_shift(L, checkunsigned(L, 1), luaL_checkinteger(L, 2)); -} - - -static int b_rshift (lua_State *L) { - return b_shift(L, checkunsigned(L, 1), -luaL_checkinteger(L, 2)); -} - - -static int b_arshift (lua_State *L) { - lua_Unsigned r = checkunsigned(L, 1); - lua_Integer i = luaL_checkinteger(L, 2); - if (i < 0 || !(r & ((lua_Unsigned)1 << (LUA_NBITS - 1)))) - return b_shift(L, r, -i); - else { /* arithmetic shift for 'negative' number */ - if (i >= LUA_NBITS) r = ALLONES; - else - r = trim((r >> i) | ~(trim(~(lua_Unsigned)0) >> i)); /* add sign bit */ - pushunsigned(L, r); - return 1; - } -} - - -static int b_rot (lua_State *L, lua_Integer d) { - lua_Unsigned r = checkunsigned(L, 1); - int i = d & (LUA_NBITS - 1); /* i = d % NBITS */ - r = trim(r); - if (i != 0) /* avoid undefined shift of LUA_NBITS when i == 0 */ - r = (r << i) | (r >> (LUA_NBITS - i)); - pushunsigned(L, trim(r)); - return 1; -} - - -static int b_lrot (lua_State *L) { - return b_rot(L, luaL_checkinteger(L, 2)); -} - - -static int b_rrot (lua_State *L) { - return b_rot(L, -luaL_checkinteger(L, 2)); -} - - -/* -** get field and width arguments for field-manipulation functions, -** checking whether they are valid. -** ('luaL_error' called without 'return' to avoid later warnings about -** 'width' being used uninitialized.) -*/ -static int fieldargs (lua_State *L, int farg, int *width) { - lua_Integer f = luaL_checkinteger(L, farg); - lua_Integer w = luaL_optinteger(L, farg + 1, 1); - luaL_argcheck(L, 0 <= f, farg, "field cannot be negative"); - luaL_argcheck(L, 0 < w, farg + 1, "width must be positive"); - if (f + w > LUA_NBITS) - luaL_error(L, "trying to access non-existent bits"); - *width = (int)w; - return (int)f; -} - - -static int b_extract (lua_State *L) { - int w; - lua_Unsigned r = trim(checkunsigned(L, 1)); - int f = fieldargs(L, 2, &w); - r = (r >> f) & mask(w); - pushunsigned(L, r); - return 1; -} - - -static int b_replace (lua_State *L) { - int w; - lua_Unsigned r = trim(checkunsigned(L, 1)); - lua_Unsigned v = trim(checkunsigned(L, 2)); - int f = fieldargs(L, 3, &w); - lua_Unsigned m = mask(w); - r = (r & ~(m << f)) | ((v & m) << f); - pushunsigned(L, r); - return 1; -} - - -static const luaL_Reg bitlib[] = { - {"arshift", b_arshift}, - {"band", b_and}, - {"bnot", b_not}, - {"bor", b_or}, - {"bxor", b_xor}, - {"btest", b_test}, - {"extract", b_extract}, - {"lrotate", b_lrot}, - {"lshift", b_lshift}, - {"replace", b_replace}, - {"rrotate", b_rrot}, - {"rshift", b_rshift}, - {NULL, NULL} -}; - - - -LUAMOD_API int luaopen_bit32 (lua_State *L) { - luaL_newlib(L, bitlib); - return 1; -} - - -#else /* }{ */ - - -LUAMOD_API int luaopen_bit32 (lua_State *L) { - return luaL_error(L, "library 'bit32' has been deprecated"); -} - -#endif /* } */ +Deprecated module. From 34b00c16e28c2bbc3e633b4007de956130905ed6 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 27 Feb 2018 15:47:32 -0300 Subject: [PATCH 0235/1145] removed compatibility code with older versions --- lauxlib.c | 83 +---------------------------------------------- lauxlib.h | 17 +--------- lbaselib.c | 38 +++++++--------------- linit.c | 5 +-- loadlib.c | 95 +----------------------------------------------------- lobject.c | 4 +-- ltablib.c | 28 +--------------- ltests.h | 12 +------ luaconf.h | 80 +++------------------------------------------ lualib.h | 5 +-- 10 files changed, 25 insertions(+), 342 deletions(-) diff --git a/lauxlib.c b/lauxlib.c index febd3eb17a..215a83b1ef 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -1,5 +1,5 @@ /* -** $Id: lauxlib.c,v 1.292 2018/01/29 19:13:27 roberto Exp roberto $ +** $Id: lauxlib.c,v 1.293 2018/02/21 13:48:44 roberto Exp roberto $ ** Auxiliary functions for building Lua libraries ** See Copyright Notice in lua.h */ @@ -845,87 +845,6 @@ LUALIB_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len) { } -/* -** {====================================================== -** Compatibility with 5.1 module functions -** ======================================================= -*/ -#if defined(LUA_COMPAT_MODULE) - -static const char *luaL_findtable (lua_State *L, int idx, - const char *fname, int szhint) { - const char *e; - if (idx) lua_pushvalue(L, idx); - do { - e = strchr(fname, '.'); - if (e == NULL) e = fname + strlen(fname); - lua_pushlstring(L, fname, e - fname); - if (lua_rawget(L, -2) == LUA_TNIL) { /* no such field? */ - lua_pop(L, 1); /* remove this nil */ - lua_createtable(L, 0, (*e == '.' ? 1 : szhint)); /* new table for field */ - lua_pushlstring(L, fname, e - fname); - lua_pushvalue(L, -2); - lua_settable(L, -4); /* set new table into field */ - } - else if (!lua_istable(L, -1)) { /* field has a non-table value? */ - lua_pop(L, 2); /* remove table and value */ - return fname; /* return problematic part of the name */ - } - lua_remove(L, -2); /* remove previous table */ - fname = e + 1; - } while (*e == '.'); - return NULL; -} - - -/* -** Count number of elements in a luaL_Reg list. -*/ -static int libsize (const luaL_Reg *l) { - int size = 0; - for (; l && l->name; l++) size++; - return size; -} - - -/* -** Find or create a module table with a given name. The function -** first looks at the LOADED table and, if that fails, try a -** global variable with that name. In any case, leaves on the stack -** the module table. -*/ -LUALIB_API void luaL_pushmodule (lua_State *L, const char *modname, - int sizehint) { - luaL_findtable(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE, 1); - if (lua_getfield(L, -1, modname) != LUA_TTABLE) { /* no LOADED[modname]? */ - lua_pop(L, 1); /* remove previous result */ - /* try global variable (and create one if it does not exist) */ - lua_pushglobaltable(L); - if (luaL_findtable(L, 0, modname, sizehint) != NULL) - luaL_error(L, "name conflict for module '%s'", modname); - lua_pushvalue(L, -1); - lua_setfield(L, -3, modname); /* LOADED[modname] = new table */ - } - lua_remove(L, -2); /* remove LOADED table */ -} - - -LUALIB_API void luaL_openlib (lua_State *L, const char *libname, - const luaL_Reg *l, int nup) { - luaL_checkversion(L); - if (libname) { - luaL_pushmodule(L, libname, libsize(l)); /* get/create library table */ - lua_insert(L, -(nup + 1)); /* move library table to below upvalues */ - } - if (l) - luaL_setfuncs(L, l, nup); - else - lua_pop(L, nup); /* remove upvalues */ -} - -#endif -/* }====================================================== */ - /* ** set functions from list 'l' into table at top - 'nup'; each ** function gets the 'nup' elements at the top as upvalues. diff --git a/lauxlib.h b/lauxlib.h index 79d1f30ab9..9f91f6a6b3 100644 --- a/lauxlib.h +++ b/lauxlib.h @@ -1,5 +1,5 @@ /* -** $Id: lauxlib.h,v 1.132 2017/04/24 18:06:12 roberto Exp roberto $ +** $Id: lauxlib.h,v 1.133 2017/06/27 18:32:49 roberto Exp roberto $ ** Auxiliary functions for building Lua libraries ** See Copyright Notice in lua.h */ @@ -204,21 +204,6 @@ typedef struct luaL_Stream { /* }====================================================== */ - - -/* compatibility with old module system */ -#if defined(LUA_COMPAT_MODULE) - -LUALIB_API void (luaL_pushmodule) (lua_State *L, const char *modname, - int sizehint); -LUALIB_API void (luaL_openlib) (lua_State *L, const char *libname, - const luaL_Reg *l, int nup); - -#define luaL_register(L,n,l) (luaL_openlib(L,(n),(l),0)) - -#endif - - /* ** {================================================================== ** "Abstraction Layer" for basic report of messages and errors diff --git a/lbaselib.c b/lbaselib.c index 8e0338655a..477d44d2ef 100644 --- a/lbaselib.c +++ b/lbaselib.c @@ -1,5 +1,5 @@ /* -** $Id: lbaselib.c,v 1.320 2018/02/25 12:48:16 roberto Exp roberto $ +** $Id: lbaselib.c,v 1.321 2018/02/27 17:48:28 roberto Exp roberto $ ** Basic library ** See Copyright Notice in lua.h */ @@ -253,23 +253,6 @@ static int luaB_type (lua_State *L) { } -static int pairsmeta (lua_State *L, const char *method, int iszero, - lua_CFunction iter) { - luaL_checkany(L, 1); - if (luaL_getmetafield(L, 1, method) == LUA_TNIL) { /* no metamethod? */ - lua_pushcfunction(L, iter); /* will return generator, */ - lua_pushvalue(L, 1); /* state, */ - if (iszero) lua_pushinteger(L, 0); /* and initial value */ - else lua_pushnil(L); - } - else { - lua_pushvalue(L, 1); /* argument 'self' to metamethod */ - lua_call(L, 1, 3); /* get 3 values from metamethod */ - } - return 3; -} - - static int luaB_next (lua_State *L) { luaL_checktype(L, 1, LUA_TTABLE); lua_settop(L, 2); /* create a 2nd argument if there isn't one */ @@ -283,7 +266,17 @@ static int luaB_next (lua_State *L) { static int luaB_pairs (lua_State *L) { - return pairsmeta(L, "__pairs", 0, luaB_next); + luaL_checkany(L, 1); + if (luaL_getmetafield(L, 1, "__pairs") == LUA_TNIL) { /* no metamethod? */ + lua_pushcfunction(L, luaB_next); /* will return generator, */ + lua_pushvalue(L, 1); /* state, */ + lua_pushnil(L); /* and initial value */ + } + else { + lua_pushvalue(L, 1); /* argument 'self' to metamethod */ + lua_call(L, 1, 3); /* get 3 values from metamethod */ + } + return 3; } @@ -302,15 +295,11 @@ static int ipairsaux (lua_State *L) { ** (The given "table" may not be a table.) */ static int luaB_ipairs (lua_State *L) { -#if defined(LUA_COMPAT_IPAIRS) - return pairsmeta(L, "__ipairs", 1, ipairsaux); -#else luaL_checkany(L, 1); lua_pushcfunction(L, ipairsaux); /* iteration function */ lua_pushvalue(L, 1); /* state */ lua_pushinteger(L, 0); /* initial value */ return 3; -#endif } @@ -506,9 +495,6 @@ static const luaL_Reg base_funcs[] = { {"ipairs", luaB_ipairs}, {"loadfile", luaB_loadfile}, {"load", luaB_load}, -#if defined(LUA_COMPAT_LOADSTRING) - {"loadstring", luaB_load}, -#endif {"next", luaB_next}, {"pairs", luaB_pairs}, {"pcall", luaB_pcall}, diff --git a/linit.c b/linit.c index 3c2b6023be..2db8d5b30d 100644 --- a/linit.c +++ b/linit.c @@ -1,5 +1,5 @@ /* -** $Id: linit.c,v 1.39 2016/12/04 20:17:24 roberto Exp roberto $ +** $Id: linit.c,v 1.40 2017/06/27 18:32:49 roberto Exp roberto $ ** Initialization of libraries for lua.c and other clients ** See Copyright Notice in lua.h */ @@ -50,9 +50,6 @@ static const luaL_Reg loadedlibs[] = { {LUA_MATHLIBNAME, luaopen_math}, {LUA_UTF8LIBNAME, luaopen_utf8}, {LUA_DBLIBNAME, luaopen_debug}, -#if defined(LUA_COMPAT_BITLIB) - {LUA_BITLIBNAME, luaopen_bit32}, -#endif {NULL, NULL} }; diff --git a/loadlib.c b/loadlib.c index 176330428d..a5e10e4506 100644 --- a/loadlib.c +++ b/loadlib.c @@ -1,5 +1,5 @@ /* -** $Id: loadlib.c,v 1.130 2017/01/12 17:14:26 roberto Exp roberto $ +** $Id: loadlib.c,v 1.131 2017/12/13 12:51:42 roberto Exp roberto $ ** Dynamic library loader for Lua ** See Copyright Notice in lua.h ** @@ -621,96 +621,10 @@ static int ll_require (lua_State *L) { -/* -** {====================================================== -** 'module' function -** ======================================================= -*/ -#if defined(LUA_COMPAT_MODULE) - -/* -** changes the environment variable of calling function -*/ -static void set_env (lua_State *L) { - lua_Debug ar; - if (lua_getstack(L, 1, &ar) == 0 || - lua_getinfo(L, "f", &ar) == 0 || /* get calling function */ - lua_iscfunction(L, -1)) - luaL_error(L, "'module' not called from a Lua function"); - lua_pushvalue(L, -2); /* copy new environment table to top */ - lua_setupvalue(L, -2, 1); - lua_pop(L, 1); /* remove function */ -} - - -static void dooptions (lua_State *L, int n) { - int i; - for (i = 2; i <= n; i++) { - if (lua_isfunction(L, i)) { /* avoid 'calling' extra info. */ - lua_pushvalue(L, i); /* get option (a function) */ - lua_pushvalue(L, -2); /* module */ - lua_call(L, 1, 0); - } - } -} - - -static void modinit (lua_State *L, const char *modname) { - const char *dot; - lua_pushvalue(L, -1); - lua_setfield(L, -2, "_M"); /* module._M = module */ - lua_pushstring(L, modname); - lua_setfield(L, -2, "_NAME"); - dot = strrchr(modname, '.'); /* look for last dot in module name */ - if (dot == NULL) dot = modname; - else dot++; - /* set _PACKAGE as package name (full module name minus last part) */ - lua_pushlstring(L, modname, dot - modname); - lua_setfield(L, -2, "_PACKAGE"); -} - - -static int ll_module (lua_State *L) { - const char *modname = luaL_checkstring(L, 1); - int lastarg = lua_gettop(L); /* last parameter */ - luaL_pushmodule(L, modname, 1); /* get/create module table */ - /* check whether table already has a _NAME field */ - if (lua_getfield(L, -1, "_NAME") != LUA_TNIL) - lua_pop(L, 1); /* table is an initialized module */ - else { /* no; initialize it */ - lua_pop(L, 1); - modinit(L, modname); - } - lua_pushvalue(L, -1); - set_env(L); - dooptions(L, lastarg); - return 1; -} - - -static int ll_seeall (lua_State *L) { - luaL_checktype(L, 1, LUA_TTABLE); - if (!lua_getmetatable(L, 1)) { - lua_createtable(L, 0, 1); /* create new metatable */ - lua_pushvalue(L, -1); - lua_setmetatable(L, 1); - } - lua_pushglobaltable(L); - lua_setfield(L, -2, "__index"); /* mt.__index = _G */ - return 0; -} - -#endif -/* }====================================================== */ - - static const luaL_Reg pk_funcs[] = { {"loadlib", ll_loadlib}, {"searchpath", ll_searchpath}, -#if defined(LUA_COMPAT_MODULE) - {"seeall", ll_seeall}, -#endif /* placeholders */ {"preload", NULL}, {"cpath", NULL}, @@ -722,9 +636,6 @@ static const luaL_Reg pk_funcs[] = { static const luaL_Reg ll_funcs[] = { -#if defined(LUA_COMPAT_MODULE) - {"module", ll_module}, -#endif {"require", ll_require}, {NULL, NULL} }; @@ -742,10 +653,6 @@ static void createsearcherstable (lua_State *L) { lua_pushcclosure(L, searchers[i], 1); lua_rawseti(L, -2, i+1); } -#if defined(LUA_COMPAT_LOADERS) - lua_pushvalue(L, -1); /* make a copy of 'searchers' table */ - lua_setfield(L, -3, "loaders"); /* put it in field 'loaders' */ -#endif lua_setfield(L, -2, "searchers"); /* put it in field 'searchers' */ } diff --git a/lobject.c b/lobject.c index d5e9c1ce98..f4aa014aeb 100644 --- a/lobject.c +++ b/lobject.c @@ -1,5 +1,5 @@ /* -** $Id: lobject.c,v 2.122 2017/12/30 20:46:18 roberto Exp roberto $ +** $Id: lobject.c,v 2.123 2018/01/28 15:13:26 roberto Exp roberto $ ** Some generic functions over Lua objects ** See Copyright Notice in lua.h */ @@ -382,12 +382,10 @@ void luaO_tostring (lua_State *L, TValue *obj) { len = lua_integer2str(buff, sizeof(buff), ivalue(obj)); else { len = lua_number2str(buff, sizeof(buff), fltvalue(obj)); -#if !defined(LUA_COMPAT_FLOATSTRING) if (buff[strspn(buff, "-0123456789")] == '\0') { /* looks like an int? */ buff[len++] = lua_getlocaledecpoint(); buff[len++] = '0'; /* adds '.0' to result */ } -#endif } setsvalue(L, obj, luaS_newlstr(L, buff, len)); } diff --git a/ltablib.c b/ltablib.c index 982821edf0..22bfae427a 100644 --- a/ltablib.c +++ b/ltablib.c @@ -1,5 +1,5 @@ /* -** $Id: ltablib.c,v 1.93 2016/02/25 19:41:54 roberto Exp roberto $ +** $Id: ltablib.c,v 1.94 2018/02/25 12:48:16 roberto Exp roberto $ ** Library for Table Manipulation ** See Copyright Notice in lua.h */ @@ -58,24 +58,6 @@ static void checktab (lua_State *L, int arg, int what) { } -#if defined(LUA_COMPAT_MAXN) -static int maxn (lua_State *L) { - lua_Number max = 0; - luaL_checktype(L, 1, LUA_TTABLE); - lua_pushnil(L); /* first key */ - while (lua_next(L, 1)) { - lua_pop(L, 1); /* remove value */ - if (lua_type(L, -1) == LUA_TNUMBER) { - lua_Number v = lua_tonumber(L, -1); - if (v > max) max = v; - } - } - lua_pushnumber(L, max); - return 1; -} -#endif - - static int tinsert (lua_State *L) { lua_Integer e = aux_getn(L, 1, TAB_RW) + 1; /* first empty element */ lua_Integer pos; /* where to insert new element */ @@ -425,9 +407,6 @@ static int sort (lua_State *L) { static const luaL_Reg tab_funcs[] = { {"concat", tconcat}, -#if defined(LUA_COMPAT_MAXN) - {"maxn", maxn}, -#endif {"insert", tinsert}, {"pack", pack}, {"unpack", unpack}, @@ -440,11 +419,6 @@ static const luaL_Reg tab_funcs[] = { LUAMOD_API int luaopen_table (lua_State *L) { luaL_newlib(L, tab_funcs); -#if defined(LUA_COMPAT_UNPACK) - /* _G.unpack = table.unpack */ - lua_getfield(L, -1, "unpack"); - lua_setglobal(L, "unpack"); -#endif return 1; } diff --git a/ltests.h b/ltests.h index 001b205ce5..7019f7161e 100644 --- a/ltests.h +++ b/ltests.h @@ -1,5 +1,5 @@ /* -** $Id: ltests.h,v 2.54 2017/12/07 18:51:39 roberto Exp roberto $ +** $Id: ltests.h,v 2.55 2017/12/18 13:01:49 roberto Exp roberto $ ** Internal Header for Debugging of the Lua Implementation ** See Copyright Notice in lua.h */ @@ -13,16 +13,6 @@ /* test Lua with compatibility code */ #define LUA_COMPAT_MATHLIB -#define LUA_COMPAT_IPAIRS -#define LUA_COMPAT_BITLIB -#define LUA_COMPAT_APIINTCASTS -#define LUA_COMPAT_FLOATSTRING -#define LUA_COMPAT_UNPACK -#define LUA_COMPAT_LOADERS -#define LUA_COMPAT_LOG10 -#define LUA_COMPAT_LOADSTRING -#define LUA_COMPAT_MAXN -#define LUA_COMPAT_MODULE #define LUA_DEBUG diff --git a/luaconf.h b/luaconf.h index 5ed062e243..e600bbf11b 100644 --- a/luaconf.h +++ b/luaconf.h @@ -1,5 +1,5 @@ /* -** $Id: luaconf.h,v 1.263 2017/12/30 20:46:18 roberto Exp roberto $ +** $Id: luaconf.h,v 1.264 2018/02/20 20:52:50 roberto Exp roberto $ ** Configuration file for Lua ** See Copyright Notice in lua.h */ @@ -295,29 +295,20 @@ */ /* -@@ LUA_COMPAT_5_2 controls other macros for compatibility with Lua 5.2. -@@ LUA_COMPAT_5_1 controls other macros for compatibility with Lua 5.1. +@@ LUA_COMPAT_5_3 controls other macros for compatibility with Lua 5.2. ** You can define it to get all options, or change specific options ** to fit your specific needs. */ -#if defined(LUA_COMPAT_5_2) /* { */ +#if defined(LUA_COMPAT_5_3) /* { */ /* @@ LUA_COMPAT_MATHLIB controls the presence of several deprecated ** functions in the mathematical library. +** (These functions were already officially removed in 5.3, but +** nevertheless they are available by default there.) */ #define LUA_COMPAT_MATHLIB -/* -@@ LUA_COMPAT_BITLIB controls the presence of library 'bit32'. -*/ -#define LUA_COMPAT_BITLIB - -/* -@@ LUA_COMPAT_IPAIRS controls the effectiveness of the __ipairs metamethod. -*/ -#define LUA_COMPAT_IPAIRS - /* @@ LUA_COMPAT_APIINTCASTS controls the presence of macros for ** manipulating other integer types (lua_pushunsigned, lua_tounsigned, @@ -328,50 +319,6 @@ #endif /* } */ -#if defined(LUA_COMPAT_5_1) /* { */ - -/* Incompatibilities from 5.2 -> 5.3 */ -#define LUA_COMPAT_MATHLIB -#define LUA_COMPAT_APIINTCASTS - -/* -@@ LUA_COMPAT_UNPACK controls the presence of global 'unpack'. -** You can replace it with 'table.unpack'. -*/ -#define LUA_COMPAT_UNPACK - -/* -@@ LUA_COMPAT_LOADERS controls the presence of table 'package.loaders'. -** You can replace it with 'package.searchers'. -*/ -#define LUA_COMPAT_LOADERS - -/* -@@ macro 'lua_cpcall' emulates deprecated function lua_cpcall. -** You can call your C function directly (with light C functions). -*/ -#define lua_cpcall(L,f,u) \ - (lua_pushcfunction(L, (f)), \ - lua_pushlightuserdata(L,(u)), \ - lua_pcall(L,1,0,0)) - - -/* -@@ LUA_COMPAT_LOG10 defines the function 'log10' in the math library. -** You can rewrite 'log10(x)' as 'log(x, 10)'. -*/ -#define LUA_COMPAT_LOG10 - -/* -@@ LUA_COMPAT_LOADSTRING defines the function 'loadstring' in the base -** library. You can rewrite 'loadstring(s)' as 'load(s)'. -*/ -#define LUA_COMPAT_LOADSTRING - -/* -@@ LUA_COMPAT_MAXN defines the function 'maxn' in the table library. -*/ -#define LUA_COMPAT_MAXN /* @@ The following macros supply trivial compatibility for some @@ -385,23 +332,6 @@ #define lua_equal(L,idx1,idx2) lua_compare(L,(idx1),(idx2),LUA_OPEQ) #define lua_lessthan(L,idx1,idx2) lua_compare(L,(idx1),(idx2),LUA_OPLT) -/* -@@ LUA_COMPAT_MODULE controls compatibility with previous -** module functions 'module' (Lua) and 'luaL_register' (C). -*/ -#define LUA_COMPAT_MODULE - -#endif /* } */ - - -/* -@@ LUA_COMPAT_FLOATSTRING makes Lua format integral floats without a -@@ a float mark ('.0'). -** This macro is not on by default even in compatibility mode, -** because this is not really an incompatibility. -*/ -/* #define LUA_COMPAT_FLOATSTRING */ - /* }================================================================== */ diff --git a/lualib.h b/lualib.h index c01eb9c8c2..12f7d02277 100644 --- a/lualib.h +++ b/lualib.h @@ -1,5 +1,5 @@ /* -** $Id: lualib.h,v 1.44 2014/02/06 17:32:33 roberto Exp roberto $ +** $Id: lualib.h,v 1.45 2017/01/12 17:14:26 roberto Exp roberto $ ** Lua standard libraries ** See Copyright Notice in lua.h */ @@ -35,9 +35,6 @@ LUAMOD_API int (luaopen_string) (lua_State *L); #define LUA_UTF8LIBNAME "utf8" LUAMOD_API int (luaopen_utf8) (lua_State *L); -#define LUA_BITLIBNAME "bit32" -LUAMOD_API int (luaopen_bit32) (lua_State *L); - #define LUA_MATHLIBNAME "math" LUAMOD_API int (luaopen_math) (lua_State *L); From fbea553ca291eb2f8a7bbd361403639af52fac96 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 27 Feb 2018 17:01:55 -0300 Subject: [PATCH 0236/1145] 'lua_setiuservalue' removes value from the stack even in case of error --- lapi.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lapi.c b/lapi.c index 7ecf68cb7e..eb872df8bd 100644 --- a/lapi.c +++ b/lapi.c @@ -1,5 +1,5 @@ /* -** $Id: lapi.c,v 2.288 2018/02/26 14:16:05 roberto Exp roberto $ +** $Id: lapi.c,v 2.289 2018/02/27 17:48:28 roberto Exp roberto $ ** Lua API ** See Copyright Notice in lua.h */ @@ -953,9 +953,9 @@ LUA_API int lua_setiuservalue (lua_State *L, int idx, int n) { else { setobj(L, &uvalue(o)->uv[n - 1].uv, s2v(L->top - 1)); luaC_barrierback(L, gcvalue(o), s2v(L->top - 1)); - L->top--; res = 1; } + L->top--; lua_unlock(L); return res; } From df4938451668c3225130426fb76839276d71853d Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 2 Mar 2018 13:30:47 -0300 Subject: [PATCH 0237/1145] year in copyright changed to 2018 --- lua.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lua.h b/lua.h index 3c481a7023..b988a7543c 100644 --- a/lua.h +++ b/lua.h @@ -1,5 +1,5 @@ /* -** $Id: lua.h,v 1.341 2018/02/20 16:52:50 roberto Exp roberto $ +** $Id: lua.h,v 1.342 2018/02/25 12:48:16 roberto Exp roberto $ ** Lua - A Scripting Language ** Lua.org, PUC-Rio, Brazil (http://www.lua.org) ** See Copyright Notice at the end of this file @@ -23,7 +23,7 @@ #define LUA_VERSION "Lua " LUA_VERSION_MAJOR "." LUA_VERSION_MINOR #define LUA_RELEASE LUA_VERSION "." LUA_VERSION_RELEASE -#define LUA_COPYRIGHT LUA_RELEASE " Copyright (C) 1994-2017 Lua.org, PUC-Rio" +#define LUA_COPYRIGHT LUA_RELEASE " Copyright (C) 1994-2018 Lua.org, PUC-Rio" #define LUA_AUTHORS "R. Ierusalimschy, L. H. de Figueiredo, W. Celes" @@ -472,7 +472,7 @@ struct lua_Debug { /****************************************************************************** -* Copyright (C) 1994-2017 Lua.org, PUC-Rio. +* Copyright (C) 1994-2018 Lua.org, PUC-Rio. * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the From 00008b8ed0aca4540a2d5480316a01c7172fb8ba Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 2 Mar 2018 15:25:00 -0300 Subject: [PATCH 0238/1145] back to one format per argument --- liolib.c | 56 ++++++++++++++++++++++++++------------------------------ 1 file changed, 26 insertions(+), 30 deletions(-) diff --git a/liolib.c b/liolib.c index 7dc1dab9ae..0884e9ac40 100644 --- a/liolib.c +++ b/liolib.c @@ -1,5 +1,5 @@ /* -** $Id: liolib.c,v 2.154 2017/11/16 16:28:36 roberto Exp roberto $ +** $Id: liolib.c,v 2.155 2018/02/21 13:48:44 roberto Exp roberto $ ** Standard I/O (and system) library ** See Copyright Notice in lua.h */ @@ -528,44 +528,40 @@ static int read_chars (lua_State *L, FILE *f, size_t n) { static int g_read (lua_State *L, FILE *f, int first) { int nargs = lua_gettop(L) - 1; - int nresults, success; + int n, success; clearerr(f); if (nargs == 0) { /* no arguments? */ success = read_line(L, f, 1); - nresults = 1; + n = first + 1; /* to return 1 result */ } else { + /* ensure stack space for all results and for auxlib's buffer */ + luaL_checkstack(L, nargs+LUA_MINSTACK, "too many arguments"); success = 1; - nresults = 0; - for (; nargs-- && success; first++) { - luaL_checkstack(L, LUA_MINSTACK, "too many arguments"); - if (lua_type(L, first) == LUA_TNUMBER) { - size_t l = (size_t)luaL_checkinteger(L, first); + for (n = first; nargs-- && success; n++) { + if (lua_type(L, n) == LUA_TNUMBER) { + size_t l = (size_t)luaL_checkinteger(L, n); success = (l == 0) ? test_eof(L, f) : read_chars(L, f, l); - nresults++; } else { - const char *p = luaL_checkstring(L, first); + const char *p = luaL_checkstring(L, n); if (*p == '*') p++; /* skip optional '*' (for compatibility) */ - for (; success && *p != '\0'; p++) { - nresults++; - switch (*p) { - case 'n': /* number */ - success = read_number(L, f); - break; - case 'l': /* line */ - success = read_line(L, f, 1); - break; - case 'L': /* line with end-of-line */ - success = read_line(L, f, 0); - break; - case 'a': /* file */ - read_all(L, f); /* read entire file */ - success = 1; /* always success */ - break; - default: - return luaL_argerror(L, first, "invalid format"); - } + switch (*p) { + case 'n': /* number */ + success = read_number(L, f); + break; + case 'l': /* line */ + success = read_line(L, f, 1); + break; + case 'L': /* line with end-of-line */ + success = read_line(L, f, 0); + break; + case 'a': /* file */ + read_all(L, f); /* read entire file */ + success = 1; /* always success */ + break; + default: + return luaL_argerror(L, n, "invalid format"); } } } @@ -576,7 +572,7 @@ static int g_read (lua_State *L, FILE *f, int first) { lua_pop(L, 1); /* remove last result */ lua_pushnil(L); /* push nil instead */ } - return nresults; + return n - first; } From 0eb10c6303dadf29be349b0331e6029b5ae03298 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 2 Mar 2018 15:31:51 -0300 Subject: [PATCH 0239/1145] 'LUA_USE_READLINE' moved to the make file --- luaconf.h | 4 +--- makefile | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/luaconf.h b/luaconf.h index e600bbf11b..5a62ddac71 100644 --- a/luaconf.h +++ b/luaconf.h @@ -1,5 +1,5 @@ /* -** $Id: luaconf.h,v 1.264 2018/02/20 20:52:50 roberto Exp roberto $ +** $Id: luaconf.h,v 1.265 2018/02/27 18:47:32 roberto Exp roberto $ ** Configuration file for Lua ** See Copyright Notice in lua.h */ @@ -61,14 +61,12 @@ #if defined(LUA_USE_LINUX) #define LUA_USE_POSIX #define LUA_USE_DLOPEN /* needs an extra library: -ldl */ -#define LUA_USE_READLINE /* needs some extra libraries */ #endif #if defined(LUA_USE_MACOSX) #define LUA_USE_POSIX #define LUA_USE_DLOPEN /* MacOS does not need -ldl */ -#define LUA_USE_READLINE /* needs an extra library: -lreadline */ #endif diff --git a/makefile b/makefile index 1a98ccd92b..b298486e6e 100644 --- a/makefile +++ b/makefile @@ -54,7 +54,7 @@ LOCAL = $(TESTS) $(CWARNS) -g -DEXTERNMEMCHECK # enable Linux goodies -MYCFLAGS= $(LOCAL) -std=c99 -DLUA_USE_LINUX +MYCFLAGS= $(LOCAL) -std=c99 -DLUA_USE_LINUX -DLUA_USE_READLINE MYLDFLAGS= $(LOCAL) -Wl,-E MYLIBS= -ldl -lreadline From 893f382e94e05578063f91830ee735f48ee95c8b Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 2 Mar 2018 15:56:14 -0300 Subject: [PATCH 0240/1145] Jump Table for the interpreter --- ljumptab.h | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 ljumptab.h diff --git a/ljumptab.h b/ljumptab.h new file mode 100644 index 0000000000..ac6a035766 --- /dev/null +++ b/ljumptab.h @@ -0,0 +1,98 @@ +#undef vmdispatch +#undef vmcase +#undef vmbreak + +#define vmdispatch(x) goto *disptab[x]; + +#define vmcase(l) L_##l: + +#define vmbreak vmfetch(); vmdispatch(GET_OPCODE(i)); + + +static void *disptab[] = { + +// you can update the following list with this command: +// +// sed -n '/^OP_/\!d; s/OP_/\&\&L_OP_/ ; s/,.*/,/ ; s/\/.*// ; p' lopcodes.h +// +// + +&&L_OP_MOVE, +&&L_OP_LOADI, +&&L_OP_LOADF, +&&L_OP_LOADK, +&&L_OP_LOADKX, +&&L_OP_LOADBOOL, +&&L_OP_LOADNIL, +&&L_OP_GETUPVAL, +&&L_OP_SETUPVAL, +&&L_OP_GETTABUP, +&&L_OP_GETTABLE, +&&L_OP_GETI, +&&L_OP_GETFIELD, +&&L_OP_SETTABUP, +&&L_OP_SETTABLE, +&&L_OP_SETI, +&&L_OP_SETFIELD, +&&L_OP_NEWTABLE, +&&L_OP_SELF, +&&L_OP_ADDI, +&&L_OP_SUBI, +&&L_OP_MULI, +&&L_OP_MODI, +&&L_OP_POWI, +&&L_OP_DIVI, +&&L_OP_IDIVI, +&&L_OP_BANDK, +&&L_OP_BORK, +&&L_OP_BXORK, +&&L_OP_SHRI, +&&L_OP_SHLI, +&&L_OP_ADD, +&&L_OP_SUB, +&&L_OP_MUL, +&&L_OP_MOD, +&&L_OP_POW, +&&L_OP_DIV, +&&L_OP_IDIV, +&&L_OP_BAND, +&&L_OP_BOR, +&&L_OP_BXOR, +&&L_OP_SHL, +&&L_OP_SHR, +&&L_OP_UNM, +&&L_OP_BNOT, +&&L_OP_NOT, +&&L_OP_LEN, +&&L_OP_CONCAT, +&&L_OP_CLOSE, +&&L_OP_JMP, +&&L_OP_EQ, +&&L_OP_LT, +&&L_OP_LE, +&&L_OP_EQK, +&&L_OP_EQI, +&&L_OP_LTI, +&&L_OP_LEI, +&&L_OP_GTI, +&&L_OP_GEI, +&&L_OP_TEST, +&&L_OP_TESTSET, +&&L_OP_CALL, +&&L_OP_TAILCALL, +&&L_OP_RETURN, +&&L_OP_RETURN0, +&&L_OP_RETURN1, +&&L_OP_FORLOOP1, +&&L_OP_FORPREP1, +&&L_OP_FORLOOP, +&&L_OP_FORPREP, +&&L_OP_TFORCALL, +&&L_OP_TFORLOOP, +&&L_OP_SETLIST, +&&L_OP_CLOSURE, +&&L_OP_VARARG, +&&L_OP_PREPVARARG, +&&L_OP_EXTRAARG + +}; From 62a392ff465b26b39f91a4959895a8330c4eeebd Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 2 Mar 2018 15:59:19 -0300 Subject: [PATCH 0241/1145] using jump tables when available --- lvm.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/lvm.c b/lvm.c index cd54369ef6..891e7d8d8a 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.347 2018/02/23 13:13:31 roberto Exp roberto $ +** $Id: lvm.c,v 2.348 2018/02/26 14:16:05 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -31,6 +31,16 @@ #include "lvm.h" +/* +** By default, use jump tables in the main interpreter loop on gcc +** and compatible compilers. +*/ +#if !defined(LUA_USE_JUMPTABLE) +#define LUA_USE_JUMPTABLE defined(__GNUC__) +#endif + + + /* limit for table tag-method chains (to avoid infinite loops) */ #define MAXTAGLOOP 2000 @@ -871,6 +881,9 @@ void luaV_execute (lua_State *L, CallInfo *ci) { StkId base; const Instruction *pc; int trap; +#if LUA_USE_JUMPTABLE +#include "ljumptab.h" +#endif tailcall: trap = L->hookmask; cl = clLvalue(s2v(ci->func)); From 66b7b075a65670e8a730c9373d28f971aceb8930 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 5 Mar 2018 11:07:48 -0300 Subject: [PATCH 0242/1145] 'math.random' using the xorshift128+ algorithm --- lmathlib.c | 231 ++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 195 insertions(+), 36 deletions(-) diff --git a/lmathlib.c b/lmathlib.c index e0240c9b54..708a19c050 100644 --- a/lmathlib.c +++ b/lmathlib.c @@ -1,5 +1,5 @@ /* -** $Id: lmathlib.c,v 1.118 2016/12/20 18:37:00 roberto Exp roberto $ +** $Id: lmathlib.c,v 1.119 2016/12/22 13:08:50 roberto Exp roberto $ ** Standard mathematical library ** See Copyright Notice in lua.h */ @@ -10,6 +10,7 @@ #include "lprefix.h" +#include #include #include @@ -23,19 +24,6 @@ #define PI (l_mathop(3.141592653589793238462643383279502884)) -#if !defined(l_rand) /* { */ -#if defined(LUA_USE_POSIX) -#define l_rand() random() -#define l_srand(x) srandom(x) -#define L_RANDMAX 2147483647 /* (2^31 - 1), following POSIX */ -#else -#define l_rand() rand() -#define l_srand(x) srand(x) -#define L_RANDMAX RAND_MAX -#endif -#endif /* } */ - - static int math_abs (lua_State *L) { if (lua_isinteger(L, 1)) { lua_Integer n = lua_tointeger(L, 1); @@ -239,22 +227,181 @@ static int math_max (lua_State *L) { return 1; } + +static int math_type (lua_State *L) { + if (lua_type(L, 1) == LUA_TNUMBER) { + if (lua_isinteger(L, 1)) + lua_pushliteral(L, "integer"); + else + lua_pushliteral(L, "float"); + } + else { + luaL_checkany(L, 1); + lua_pushnil(L); + } + return 1; +} + + + +/* +** {================================================================== +** Pseudo-Random Number Generator based on 'xorshift128+'. +** =================================================================== +*/ + + +#define twotomin53 (1.0 / 9007199254740992.0) /* 2^-53 */ + + +#if defined(LLONG_MAX) && !defined(LUA_DEBUG) /* { */ + +/* +** Assume long long. +*/ + +/* a 64-bit value */ +typedef unsigned long long I; + +static I xorshift128plus (I *state) { + I x = state[0]; + I y = state[1]; + state[0] = y; + x ^= x << 23; + state[1] = x ^ y ^ (x >> 18) ^ (y >> 5); + return state[1] + y; +} + + +#define mask53 (~(~0ll << 53)) + +/* +** Convert 53 bits from a random integer into a double in the +** interval [0,1). +*/ +static double I2d (I x) { + return (x & mask53) * twotomin53; +} + +/* convert an 'I' to a lua_Integer */ +#define I2Int(x) ((lua_Integer)(x)) + +/* convert a lua_Integer to an 'I' */ +#define Int2I(x) ((I)(x)) + +#else /* }{ */ + +/* +** No long long; Use two 32-bit integers to represent a 64-bit quantity. +*/ + +#if LUAI_BITSINT >= 32 +typedef unsigned int lu_int32; +#else +typedef unsigned long lu_int32; +#endif + +/* a 64-bit value */ +typedef struct I { + lu_int32 x1, x2; +} I; + + +/* +** basic operations on 'I' values +*/ + +static I pack (int x1, int x2) { + I result; + result.x1 = x1; + result.x2 = x2; + return result; +} + +static I Ishl (I i, int n) { + return pack((i.x1 << n) | (i.x2 >> (32 - n)), i.x2 << n); +} + +static I Ishr (I i, int n) { + return pack(i.x1 >> n, (i.x2 >> n) | (i.x1 << (32 - n))); +} + +static I Ixor (I i1, I i2) { + return pack(i1.x1 ^ i2.x1, i1.x2 ^ i2.x2); +} + +static I Iadd (I i1, I i2) { + I result = pack(i1.x1 + i2.x1, i1.x2 + i2.x2); + if (result.x2 < i1.x2) /* carry? */ + result.x1++; + return result; +} + + +/* +** implementation of 'xorshift128+' algorithm on 'I' values +*/ +static I xorshift128plus (I *state) { + I x = state[0]; + I y = state[1]; + state[0] = y; + x = Ixor(x, Ishl(x, 23)); /* x ^= x << 23; */ + /* s[1] = x ^ y ^ (x >> 18) ^ (y >> 5); */ + state[1] = Ixor(Ixor(Ixor(x, y), Ishr(x, 18)), Ishr(y, 5)); + return Iadd(state[1], y); /* return state[1] + y; */ +} + + +/* +** Converts an 'I' into a double, getting its lower half plus 21 +** (53 - 32) bits from its higher half and joining them into a double. +*/ + +#define mask32 0xffffffff +#define mask21 (~(~0 << 21)) + +#define twoto32 4294967296.0 /* 2^32 */ + +static double I2d (I x) { + return ((x.x1 & mask21) * twoto32 + (x.x2 & mask32)) * twotomin53; +} + +static lua_Integer I2Int (I x) { + return (((lua_Integer)x.x1 << 31) << 1) | x.x2; +} + +static I Int2I (lua_Integer n) { + return pack(n, (n >> 31) >> 1); +} + +#endif /* } */ + + /* -** This function uses 'double' (instead of 'lua_Number') to ensure that -** all bits from 'l_rand' can be represented, and that 'RANDMAX + 1.0' -** will keep full precision (ensuring that 'r' is always less than 1.0.) +** A state uses two 'I' values. */ +typedef struct { + I s[2]; +} RanState; + + static int math_random (lua_State *L) { lua_Integer low, up; - double r = (double)l_rand() * (1.0 / ((double)L_RANDMAX + 1.0)); + double r; + RanState *state = (RanState *)lua_touserdata(L, lua_upvalueindex(1)); + I rv = xorshift128plus(state->s); /* next pseudo-random value */ switch (lua_gettop(L)) { /* check number of arguments */ case 0: { /* no arguments */ - lua_pushnumber(L, (lua_Number)r); /* Number between 0 and 1 */ + lua_pushnumber(L, (lua_Number)I2d(rv)); /* float between 0 and 1 */ return 1; } case 1: { /* only upper limit */ low = 1; up = luaL_checkinteger(L, 1); + if (up == 0) { /* single 0 as argument? */ + lua_pushinteger(L, I2Int(rv)); /* full random integer */ + return 1; + } break; } case 2: { /* lower and upper limits */ @@ -268,33 +415,44 @@ static int math_random (lua_State *L) { luaL_argcheck(L, low <= up, 1, "interval is empty"); luaL_argcheck(L, low >= 0 || up <= LUA_MAXINTEGER + low, 1, "interval too large"); - r *= (double)(up - low) + 1.0; + r = I2d(rv); /* convert random value to a double */ + r *= (double)(up - low) + 1.0; /* scale it */ lua_pushinteger(L, (lua_Integer)r + low); return 1; } +static void setseed (I *state, lua_Integer n) { + int i; + state[0] = Int2I(n); + state[1] = Int2I(~n); + for (i = 0; i < 16; i++) + xorshift128plus(state); /* discard initial values */ +} + + static int math_randomseed (lua_State *L) { - l_srand((unsigned int)(lua_Integer)luaL_checknumber(L, 1)); - (void)l_rand(); /* discard first value to avoid undesirable correlations */ + RanState *state = (RanState *)lua_touserdata(L, lua_upvalueindex(1)); + lua_Integer n = luaL_checkinteger(L, 1); + setseed(state->s, n); return 0; } -static int math_type (lua_State *L) { - if (lua_type(L, 1) == LUA_TNUMBER) { - if (lua_isinteger(L, 1)) - lua_pushliteral(L, "integer"); - else - lua_pushliteral(L, "float"); - } - else { - luaL_checkany(L, 1); - lua_pushnil(L); - } - return 1; +static const luaL_Reg randfuncs[] = { + {"random", math_random}, + {"randomseed", math_randomseed}, + {NULL, NULL} +}; + +static void setrandfunc (lua_State *L) { + RanState *state = (RanState *)lua_newuserdatauv(L, sizeof(RanState), 0); + setseed(state->s, 0); + luaL_setfuncs(L, randfuncs, 1); } +/* }================================================================== */ + /* ** {================================================================== @@ -367,8 +525,6 @@ static const luaL_Reg mathlib[] = { {"min", math_min}, {"modf", math_modf}, {"rad", math_rad}, - {"random", math_random}, - {"randomseed", math_randomseed}, {"sin", math_sin}, {"sqrt", math_sqrt}, {"tan", math_tan}, @@ -384,6 +540,8 @@ static const luaL_Reg mathlib[] = { {"log10", math_log10}, #endif /* placeholders */ + {"random", NULL}, + {"randomseed", NULL}, {"pi", NULL}, {"huge", NULL}, {"maxinteger", NULL}, @@ -405,6 +563,7 @@ LUAMOD_API int luaopen_math (lua_State *L) { lua_setfield(L, -2, "maxinteger"); lua_pushinteger(L, LUA_MININTEGER); lua_setfield(L, -2, "mininteger"); + setrandfunc(L); return 1; } From 8c429311a38f0ca6f06c6df460f76414d4f4dfbe Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 5 Mar 2018 11:13:55 -0300 Subject: [PATCH 0243/1145] typo in comment --- ldo.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ldo.c b/ldo.c index 7489a9621a..4126f47bdb 100644 --- a/ldo.c +++ b/ldo.c @@ -1,5 +1,5 @@ /* -** $Id: ldo.c,v 2.196 2018/02/17 19:29:29 roberto Exp roberto $ +** $Id: ldo.c,v 2.197 2018/02/26 14:16:05 roberto Exp roberto $ ** Stack and Call structure of Lua ** See Copyright Notice in lua.h */ @@ -264,7 +264,7 @@ void luaD_inctop (lua_State *L) { /* ** Call a hook for the given event. Make sure there is a hook to be -** called. (Both 'L->hook' and 'L->hookmask', which triggers this +** called. (Both 'L->hook' and 'L->hookmask', which trigger this ** function, can be changed asynchronously by signals.) */ void luaD_hook (lua_State *L, int event, int line, From 8b0b675149cff61875e2ee6c8681a946c1ca88c5 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 5 Mar 2018 11:15:04 -0300 Subject: [PATCH 0244/1145] added casts (warnings in VS) --- ldblib.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ldblib.c b/ldblib.c index 32da969729..cb542f67d5 100644 --- a/ldblib.c +++ b/ldblib.c @@ -1,5 +1,5 @@ /* -** $Id: ldblib.c,v 1.152 2018/02/17 19:29:29 roberto Exp roberto $ +** $Id: ldblib.c,v 1.153 2018/02/20 16:52:50 roberto Exp roberto $ ** Interface from Lua to its debug API ** See Copyright Notice in lua.h */ @@ -64,7 +64,7 @@ static int db_setmetatable (lua_State *L) { static int db_getuservalue (lua_State *L) { - int n = luaL_optinteger(L, 2, 1); + int n = (int)luaL_optinteger(L, 2, 1); if (lua_type(L, 1) != LUA_TUSERDATA) lua_pushnil(L); else if (lua_getiuservalue(L, 1, n) != LUA_TNONE) { @@ -76,7 +76,7 @@ static int db_getuservalue (lua_State *L) { static int db_setuservalue (lua_State *L) { - int n = luaL_optinteger(L, 3, 1); + int n = (int)luaL_optinteger(L, 3, 1); luaL_checktype(L, 1, LUA_TUSERDATA); luaL_checkany(L, 2); lua_settop(L, 2); From 8b0434e5e6eb0f8588d97ba7a64d001e50f475f9 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 5 Mar 2018 11:15:32 -0300 Subject: [PATCH 0245/1145] both 'fTransfer' and 'nTransfer' may not fit in a 'char' --- lua.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lua.h b/lua.h index b988a7543c..5183459fc6 100644 --- a/lua.h +++ b/lua.h @@ -1,5 +1,5 @@ /* -** $Id: lua.h,v 1.342 2018/02/25 12:48:16 roberto Exp roberto $ +** $Id: lua.h,v 1.343 2018/03/02 16:30:47 roberto Exp roberto $ ** Lua - A Scripting Language ** Lua.org, PUC-Rio, Brazil (http://www.lua.org) ** See Copyright Notice at the end of this file @@ -459,10 +459,10 @@ struct lua_Debug { int lastlinedefined; /* (S) */ unsigned char nups; /* (u) number of upvalues */ unsigned char nparams;/* (u) number of parameters */ - unsigned char fTransfer;/* (r) index of first value transfered */ - unsigned char nTransfer; /* (r) number of transfered values */ char isvararg; /* (u) */ char istailcall; /* (t) */ + unsigned short fTransfer;/* (r) index of first value transfered */ + unsigned short nTransfer; /* (r) number of transfered values */ char short_src[LUA_IDSIZE]; /* (S) */ /* private part */ struct CallInfo *i_ci; /* active function */ From bdcde45d05f321fb6608e58bf24e1920fa3a3a23 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 5 Mar 2018 11:26:18 -0300 Subject: [PATCH 0246/1145] updated to use jump tables --- makefile | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/makefile b/makefile index b298486e6e..9367ffd7d2 100644 --- a/makefile +++ b/makefile @@ -6,7 +6,7 @@ # Warnings valid for both C and C++ CWARNSCPP= \ - -pedantic \ + # -pedantic \ /* warns if we use jump tables */ -Wextra \ -Wshadow \ -Wsign-compare \ @@ -14,7 +14,6 @@ CWARNSCPP= \ -Wwrite-strings \ -Wredundant-decls \ -Wdisabled-optimization \ - -Waggregate-return \ -Wdouble-promotion \ -Wstrict-aliasing=3 # not accepted by clang \ -Wno-aggressive-loop-optimizations # not accepted by clang \ @@ -191,7 +190,7 @@ lundump.o: lundump.c lprefix.h lua.h luaconf.h ldebug.h lstate.h \ lutf8lib.o: lutf8lib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h lvm.o: lvm.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \ llimits.h ltm.h lzio.h lmem.h ldo.h lfunc.h lgc.h lopcodes.h lstring.h \ - ltable.h lvm.h + ltable.h lvm.h ljumptab.h lzio.o: lzio.c lprefix.h lua.h luaconf.h llimits.h lmem.h lstate.h \ lobject.h ltm.h lzio.h From 464658b16a1a539fd590e1696bfcfb572a77fe13 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 6 Mar 2018 17:30:17 -0300 Subject: [PATCH 0247/1145] better modularization of the code for the REPL --- lua.c | 401 +++++++++++++++++++++++++++++----------------------------- 1 file changed, 203 insertions(+), 198 deletions(-) diff --git a/lua.c b/lua.c index b0e7e1b92c..2e95c40b3b 100644 --- a/lua.c +++ b/lua.c @@ -1,5 +1,5 @@ /* -** $Id: lua.c,v 1.232 2017/05/24 21:11:19 roberto Exp roberto $ +** $Id: lua.c,v 1.233 2018/02/06 15:32:36 roberto Exp roberto $ ** Lua stand-alone interpreter ** See Copyright Notice in lua.h */ @@ -21,20 +21,10 @@ #include "lualib.h" - -#if !defined(LUA_PROMPT) -#define LUA_PROMPT "> " -#define LUA_PROMPT2 ">> " -#endif - #if !defined(LUA_PROGNAME) #define LUA_PROGNAME "lua" #endif -#if !defined(LUA_MAXINPUT) -#define LUA_MAXINPUT 512 -#endif - #if !defined(LUA_INIT_VAR) #define LUA_INIT_VAR "LUA_INIT" #endif @@ -42,65 +32,6 @@ #define LUA_INITVARVERSION LUA_INIT_VAR LUA_VERSUFFIX -/* -** lua_stdin_is_tty detects whether the standard input is a 'tty' (that -** is, whether we're running lua interactively). -*/ -#if !defined(lua_stdin_is_tty) /* { */ - -#if defined(LUA_USE_POSIX) /* { */ - -#include -#define lua_stdin_is_tty() isatty(0) - -#elif defined(LUA_USE_WINDOWS) /* }{ */ - -#include -#include - -#define lua_stdin_is_tty() _isatty(_fileno(stdin)) - -#else /* }{ */ - -/* ISO C definition */ -#define lua_stdin_is_tty() 1 /* assume stdin is a tty */ - -#endif /* } */ - -#endif /* } */ - - -/* -** lua_readline defines how to show a prompt and then read a line from -** the standard input. -** lua_saveline defines how to "save" a read line in a "history". -** lua_freeline defines how to free a line read by lua_readline. -*/ -#if !defined(lua_readline) /* { */ - -#if defined(LUA_USE_READLINE) /* { */ - -#include -#include -#define lua_readline(L,b,p) ((void)L, ((b)=readline(p)) != NULL) -#define lua_saveline(L,line) ((void)L, add_history(line)) -#define lua_freeline(L,b) ((void)L, free(b)) - -#else /* }{ */ - -#define lua_readline(L,b,p) \ - ((void)L, fputs(p, stdout), fflush(stdout), /* show prompt */ \ - fgets(b, LUA_MAXINPUT, stdin) != NULL) /* get line */ -#define lua_saveline(L,line) { (void)L; (void)line; } -#define lua_freeline(L,b) { (void)L; (void)b; } - -#endif /* } */ - -#endif /* } */ - - - - static lua_State *globalL = NULL; static const char *progname = LUA_PROGNAME; @@ -268,6 +199,207 @@ static int dolibrary (lua_State *L, const char *name) { } +/* +** Push on the stack the contents of table 'arg' from 1 to #arg +*/ +static int pushargs (lua_State *L) { + int i, n; + if (lua_getglobal(L, "arg") != LUA_TTABLE) + luaL_error(L, "'arg' is not a table"); + n = (int)luaL_len(L, -1); + luaL_checkstack(L, n + 3, "too many arguments to script"); + for (i = 1; i <= n; i++) + lua_rawgeti(L, -i, i); + lua_remove(L, -i); /* remove table from the stack */ + return n; +} + + +static int handle_script (lua_State *L, char **argv) { + int status; + const char *fname = argv[0]; + if (strcmp(fname, "-") == 0 && strcmp(argv[-1], "--") != 0) + fname = NULL; /* stdin */ + status = luaL_loadfile(L, fname); + if (status == LUA_OK) { + int n = pushargs(L); /* push arguments to script */ + status = docall(L, n, LUA_MULTRET); + } + return report(L, status); +} + + +/* bits of various argument indicators in 'args' */ +#define has_error 1 /* bad option */ +#define has_i 2 /* -i */ +#define has_v 4 /* -v */ +#define has_e 8 /* -e */ +#define has_E 16 /* -E */ + + +/* +** Traverses all arguments from 'argv', returning a mask with those +** needed before running any Lua code (or an error code if it finds +** any invalid argument). 'first' returns the first not-handled argument +** (either the script name or a bad argument in case of error). +*/ +static int collectargs (char **argv, int *first) { + int args = 0; + int i; + for (i = 1; argv[i] != NULL; i++) { + *first = i; + if (argv[i][0] != '-') /* not an option? */ + return args; /* stop handling options */ + switch (argv[i][1]) { /* else check option */ + case '-': /* '--' */ + if (argv[i][2] != '\0') /* extra characters after '--'? */ + return has_error; /* invalid option */ + *first = i + 1; + return args; + case '\0': /* '-' */ + return args; /* script "name" is '-' */ + case 'E': + if (argv[i][2] != '\0') /* extra characters after 1st? */ + return has_error; /* invalid option */ + args |= has_E; + break; + case 'i': + args |= has_i; /* (-i implies -v) *//* FALLTHROUGH */ + case 'v': + if (argv[i][2] != '\0') /* extra characters after 1st? */ + return has_error; /* invalid option */ + args |= has_v; + break; + case 'e': + args |= has_e; /* FALLTHROUGH */ + case 'l': /* both options need an argument */ + if (argv[i][2] == '\0') { /* no concatenated argument? */ + i++; /* try next 'argv' */ + if (argv[i] == NULL || argv[i][0] == '-') + return has_error; /* no next argument or it is another option */ + } + break; + default: /* invalid option */ + return has_error; + } + } + *first = i; /* no script name */ + return args; +} + + +/* +** Processes options 'e' and 'l', which involve running Lua code. +** Returns 0 if some code raises an error. +*/ +static int runargs (lua_State *L, char **argv, int n) { + int i; + for (i = 1; i < n; i++) { + int option = argv[i][1]; + lua_assert(argv[i][0] == '-'); /* already checked */ + if (option == 'e' || option == 'l') { + int status; + const char *extra = argv[i] + 2; /* both options need an argument */ + if (*extra == '\0') extra = argv[++i]; + lua_assert(extra != NULL); + status = (option == 'e') + ? dostring(L, extra, "=(command line)") + : dolibrary(L, extra); + if (status != LUA_OK) return 0; + } + } + return 1; +} + + +static int handle_luainit (lua_State *L) { + const char *name = "=" LUA_INITVARVERSION; + const char *init = getenv(name + 1); + if (init == NULL) { + name = "=" LUA_INIT_VAR; + init = getenv(name + 1); /* try alternative name */ + } + if (init == NULL) return LUA_OK; + else if (init[0] == '@') + return dofile(L, init+1); + else + return dostring(L, init, name); +} + + +/* +** {================================================================== +** Read-Eval-Print Loop (REPL) +** =================================================================== +*/ + +#if !defined(LUA_PROMPT) +#define LUA_PROMPT "> " +#define LUA_PROMPT2 ">> " +#endif + +#if !defined(LUA_MAXINPUT) +#define LUA_MAXINPUT 512 +#endif + + +/* +** lua_stdin_is_tty detects whether the standard input is a 'tty' (that +** is, whether we're running lua interactively). +*/ +#if !defined(lua_stdin_is_tty) /* { */ + +#if defined(LUA_USE_POSIX) /* { */ + +#include +#define lua_stdin_is_tty() isatty(0) + +#elif defined(LUA_USE_WINDOWS) /* }{ */ + +#include +#include + +#define lua_stdin_is_tty() _isatty(_fileno(stdin)) + +#else /* }{ */ + +/* ISO C definition */ +#define lua_stdin_is_tty() 1 /* assume stdin is a tty */ + +#endif /* } */ + +#endif /* } */ + + +/* +** lua_readline defines how to show a prompt and then read a line from +** the standard input. +** lua_saveline defines how to "save" a read line in a "history". +** lua_freeline defines how to free a line read by lua_readline. +*/ +#if !defined(lua_readline) /* { */ + +#if defined(LUA_USE_READLINE) /* { */ + +#include +#include +#define lua_readline(L,b,p) ((void)L, ((b)=readline(p)) != NULL) +#define lua_saveline(L,line) ((void)L, add_history(line)) +#define lua_freeline(L,b) ((void)L, free(b)) + +#else /* }{ */ + +#define lua_readline(L,b,p) \ + ((void)L, fputs(p, stdout), fflush(stdout), /* show prompt */ \ + fgets(b, LUA_MAXINPUT, stdin) != NULL) /* get line */ +#define lua_saveline(L,line) { (void)L; (void)line; } +#define lua_freeline(L,b) { (void)L; (void)b; } + +#endif /* } */ + +#endif /* } */ + + /* ** Returns the string to be used as a prompt by the interpreter. */ @@ -418,134 +550,7 @@ static void doREPL (lua_State *L) { progname = oldprogname; } - -/* -** Push on the stack the contents of table 'arg' from 1 to #arg -*/ -static int pushargs (lua_State *L) { - int i, n; - if (lua_getglobal(L, "arg") != LUA_TTABLE) - luaL_error(L, "'arg' is not a table"); - n = (int)luaL_len(L, -1); - luaL_checkstack(L, n + 3, "too many arguments to script"); - for (i = 1; i <= n; i++) - lua_rawgeti(L, -i, i); - lua_remove(L, -i); /* remove table from the stack */ - return n; -} - - -static int handle_script (lua_State *L, char **argv) { - int status; - const char *fname = argv[0]; - if (strcmp(fname, "-") == 0 && strcmp(argv[-1], "--") != 0) - fname = NULL; /* stdin */ - status = luaL_loadfile(L, fname); - if (status == LUA_OK) { - int n = pushargs(L); /* push arguments to script */ - status = docall(L, n, LUA_MULTRET); - } - return report(L, status); -} - - - -/* bits of various argument indicators in 'args' */ -#define has_error 1 /* bad option */ -#define has_i 2 /* -i */ -#define has_v 4 /* -v */ -#define has_e 8 /* -e */ -#define has_E 16 /* -E */ - -/* -** Traverses all arguments from 'argv', returning a mask with those -** needed before running any Lua code (or an error code if it finds -** any invalid argument). 'first' returns the first not-handled argument -** (either the script name or a bad argument in case of error). -*/ -static int collectargs (char **argv, int *first) { - int args = 0; - int i; - for (i = 1; argv[i] != NULL; i++) { - *first = i; - if (argv[i][0] != '-') /* not an option? */ - return args; /* stop handling options */ - switch (argv[i][1]) { /* else check option */ - case '-': /* '--' */ - if (argv[i][2] != '\0') /* extra characters after '--'? */ - return has_error; /* invalid option */ - *first = i + 1; - return args; - case '\0': /* '-' */ - return args; /* script "name" is '-' */ - case 'E': - if (argv[i][2] != '\0') /* extra characters after 1st? */ - return has_error; /* invalid option */ - args |= has_E; - break; - case 'i': - args |= has_i; /* (-i implies -v) *//* FALLTHROUGH */ - case 'v': - if (argv[i][2] != '\0') /* extra characters after 1st? */ - return has_error; /* invalid option */ - args |= has_v; - break; - case 'e': - args |= has_e; /* FALLTHROUGH */ - case 'l': /* both options need an argument */ - if (argv[i][2] == '\0') { /* no concatenated argument? */ - i++; /* try next 'argv' */ - if (argv[i] == NULL || argv[i][0] == '-') - return has_error; /* no next argument or it is another option */ - } - break; - default: /* invalid option */ - return has_error; - } - } - *first = i; /* no script name */ - return args; -} - - -/* -** Processes options 'e' and 'l', which involve running Lua code. -** Returns 0 if some code raises an error. -*/ -static int runargs (lua_State *L, char **argv, int n) { - int i; - for (i = 1; i < n; i++) { - int option = argv[i][1]; - lua_assert(argv[i][0] == '-'); /* already checked */ - if (option == 'e' || option == 'l') { - int status; - const char *extra = argv[i] + 2; /* both options need an argument */ - if (*extra == '\0') extra = argv[++i]; - lua_assert(extra != NULL); - status = (option == 'e') - ? dostring(L, extra, "=(command line)") - : dolibrary(L, extra); - if (status != LUA_OK) return 0; - } - } - return 1; -} - - - -static int handle_luainit (lua_State *L) { - const char *name = "=" LUA_INITVARVERSION; - const char *init = getenv(name + 1); - if (init == NULL) { - name = "=" LUA_INIT_VAR; - init = getenv(name + 1); /* try alternative name */ - } - if (init == NULL) return LUA_OK; - else if (init[0] == '@') - return dofile(L, init+1); - else - return dostring(L, init, name); -} +/* }================================================================== */ /* From 4a1612ff9b968fe446bc4dd20460bfaccabeb3b3 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 7 Mar 2018 12:55:38 -0300 Subject: [PATCH 0248/1145] new experimental syntax using reserved word 'undef' --- lbaselib.c | 20 +---------------- lcode.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++--- lcode.h | 4 +++- ljumptab.h | 2 ++ llex.c | 4 ++-- llex.h | 4 ++-- lopcodes.c | 6 +++++- lopcodes.h | 5 ++++- lparser.c | 29 +++++++++++++++---------- lparser.h | 5 +++-- lvm.c | 14 +++++++++++- 11 files changed, 113 insertions(+), 43 deletions(-) diff --git a/lbaselib.c b/lbaselib.c index 477d44d2ef..12a9e888c8 100644 --- a/lbaselib.c +++ b/lbaselib.c @@ -1,5 +1,5 @@ /* -** $Id: lbaselib.c,v 1.321 2018/02/27 17:48:28 roberto Exp roberto $ +** $Id: lbaselib.c,v 1.322 2018/02/27 18:47:32 roberto Exp roberto $ ** Basic library ** See Copyright Notice in lua.h */ @@ -170,22 +170,6 @@ static int luaB_rawset (lua_State *L) { } -static int luaB_keyin (lua_State *L) { - luaL_checkany(L, 2); /* ensures a first argument too */ - lua_settop(L, 2); - lua_pushboolean(L, lua_keyin(L, 1)); - return 1; -} - - -static int luaB_removekey (lua_State *L) { - luaL_checkany(L, 2); /* ensures a first argument too */ - lua_settop(L, 2); - lua_removekey(L, 1); - return 0; -} - - static int pushmode (lua_State *L, int oldmode) { lua_pushstring(L, (oldmode == LUA_GCINC) ? "incremental" : "generational"); return 1; @@ -503,8 +487,6 @@ static const luaL_Reg base_funcs[] = { {"rawlen", luaB_rawlen}, {"rawget", luaB_rawget}, {"rawset", luaB_rawset}, - {"keyin", luaB_keyin}, - {"removekey", luaB_removekey}, {"select", luaB_select}, {"setmetatable", luaB_setmetatable}, {"tonumber", luaB_tonumber}, diff --git a/lcode.c b/lcode.c index 8f3c68c06f..95e87f317f 100644 --- a/lcode.c +++ b/lcode.c @@ -1,5 +1,5 @@ /* -** $Id: lcode.c,v 2.157 2018/02/21 15:49:32 roberto Exp roberto $ +** $Id: lcode.c,v 2.158 2018/02/26 14:16:05 roberto Exp roberto $ ** Code generator for Lua ** See Copyright Notice in lua.h */ @@ -40,6 +40,14 @@ static int codesJ (FuncState *fs, OpCode o, int sj, int k); + +/* semantic error */ +l_noret luaK_semerror (LexState *ls, const char *msg) { + ls->t.token = 0; /* remove "near " from final message */ + luaX_syntaxerror(ls, msg); +} + + /* ** If expression is a numeric constant, fills 'v' with its value ** and returns 1. Otherwise, returns 0. @@ -670,6 +678,10 @@ void luaK_dischargevars (FuncState *fs, expdesc *e) { e->k = VNONRELOC; /* becomes a non-relocatable value */ break; } + case VUNDEF: { /* not a real expression */ + luaK_semerror(fs->ls, "'undef' is not a value!!"); + break; + } case VUPVAL: { /* move value to some (pending) register */ e->u.info = luaK_codeABC(fs, OP_GETUPVAL, 0, e->u.info, 0); e->k = VRELOC; @@ -1398,6 +1410,48 @@ static void codeeq (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) { } +static void normalizeindexed (FuncState *fs, expdesc *v) { + if (v->k != VINDEXED) { /* not in proper form? */ + int key = fs->freereg; /* register with key value */ + luaK_reserveregs(fs, 1); + switch (v->k) { + case VINDEXI: + luaK_int(fs, key, v->u.ind.idx); + break; + case VINDEXSTR: + luaK_codek(fs, key, v->u.ind.idx); + break; + case VINDEXUP: + luaK_codek(fs, key, v->u.ind.idx); + luaK_codeABC(fs, OP_GETUPVAL, fs->freereg, v->u.ind.t, 0); + v->u.ind.t = fs->freereg; + luaK_reserveregs(fs, 1); /* one more register for the upvalue */ + break; + default: + luaK_semerror(fs->ls, "'undef' is not a value!!"); + break; + } + v->u.ind.idx = key; + v->k = VINDEXED; + } + freeregs(fs, v->u.ind.t, v->u.ind.idx); +} + + +static void codeisdef (FuncState *fs, int eq, expdesc *v) { + normalizeindexed(fs, v); + v->u.info = luaK_codeABCk(fs, OP_ISDEF, 0, v->u.ind.t, v->u.ind.idx, eq); + v->k = VRELOC; +} + + +void luaK_codeundef (FuncState *fs, expdesc *v) { + normalizeindexed(fs, v); + v->u.info = luaK_codeABC(fs, OP_UNDEF, v->u.ind.t, v->u.ind.idx, 0); + v->k = VRELOC; +} + + /* ** Apply prefix operation 'op' to expression 'e'. */ @@ -1446,7 +1500,7 @@ void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) { break; } case OPR_EQ: case OPR_NE: { - if (!tonumeral(v, NULL)) + if (!tonumeral(v, NULL) && fs->ls->t.token != TK_UNDEF) luaK_exp2RK(fs, v); /* else keep numeral, which may be an immediate operand */ break; @@ -1543,7 +1597,10 @@ void luaK_posfix (FuncState *fs, BinOpr opr, break; } case OPR_EQ: case OPR_NE: { - codeeq(fs, opr, e1, e2); + if (e2->k == VUNDEF) + codeisdef(fs, opr == OPR_NE, e1); + else + codeeq(fs, opr, e1, e2); break; } case OPR_LT: case OPR_LE: { diff --git a/lcode.h b/lcode.h index beeba54f58..e7c294d4a1 100644 --- a/lcode.h +++ b/lcode.h @@ -1,5 +1,5 @@ /* -** $Id: lcode.h,v 1.69 2017/11/30 13:29:18 roberto Exp roberto $ +** $Id: lcode.h,v 1.70 2017/12/18 15:44:44 roberto Exp roberto $ ** Code generator for Lua ** See Copyright Notice in lua.h */ @@ -89,6 +89,8 @@ LUAI_FUNC void luaK_posfix (FuncState *fs, BinOpr op, expdesc *v1, expdesc *v2, int line); LUAI_FUNC void luaK_setlist (FuncState *fs, int base, int nelems, int tostore); LUAI_FUNC void luaK_finish (FuncState *fs); +LUAI_FUNC void luaK_codeundef (FuncState *fs, expdesc *e); +LUAI_FUNC l_noret luaK_semerror (LexState *ls, const char *msg); #endif diff --git a/ljumptab.h b/ljumptab.h index ac6a035766..f9783ffbaa 100644 --- a/ljumptab.h +++ b/ljumptab.h @@ -78,6 +78,8 @@ static void *disptab[] = { &&L_OP_GEI, &&L_OP_TEST, &&L_OP_TESTSET, +&&L_OP_UNDEF, +&&L_OP_ISDEF, &&L_OP_CALL, &&L_OP_TAILCALL, &&L_OP_RETURN, diff --git a/llex.c b/llex.c index 5d5efb0743..7e59eb4939 100644 --- a/llex.c +++ b/llex.c @@ -1,5 +1,5 @@ /* -** $Id: llex.c,v 2.99 2018/01/28 15:13:26 roberto Exp roberto $ +** $Id: llex.c,v 2.100 2018/02/23 13:13:31 roberto Exp roberto $ ** Lexical Analyzer ** See Copyright Notice in lua.h */ @@ -41,7 +41,7 @@ static const char *const luaX_tokens [] = { "and", "break", "do", "else", "elseif", "end", "false", "for", "function", "goto", "if", "in", "local", "nil", "not", "or", "repeat", - "return", "then", "true", "until", "while", + "return", "then", "true", "undef", "until", "while", "//", "..", "...", "==", ">=", "<=", "~=", "<<", ">>", "::", "", "", "", "", "" diff --git a/llex.h b/llex.h index 9f23bd8932..fb33c33f4d 100644 --- a/llex.h +++ b/llex.h @@ -1,5 +1,5 @@ /* -** $Id: llex.h,v 1.79 2016/05/02 14:02:12 roberto Exp roberto $ +** $Id: llex.h,v 1.80 2018/01/28 15:13:26 roberto Exp roberto $ ** Lexical Analyzer ** See Copyright Notice in lua.h */ @@ -28,7 +28,7 @@ enum RESERVED { TK_AND = FIRST_RESERVED, TK_BREAK, TK_DO, TK_ELSE, TK_ELSEIF, TK_END, TK_FALSE, TK_FOR, TK_FUNCTION, TK_GOTO, TK_IF, TK_IN, TK_LOCAL, TK_NIL, TK_NOT, TK_OR, TK_REPEAT, - TK_RETURN, TK_THEN, TK_TRUE, TK_UNTIL, TK_WHILE, + TK_RETURN, TK_THEN, TK_TRUE, TK_UNDEF, TK_UNTIL, TK_WHILE, /* other terminal symbols */ TK_IDIV, TK_CONCAT, TK_DOTS, TK_EQ, TK_GE, TK_LE, TK_NE, TK_SHL, TK_SHR, diff --git a/lopcodes.c b/lopcodes.c index 79d77cc1d8..ac59537b86 100644 --- a/lopcodes.c +++ b/lopcodes.c @@ -1,5 +1,5 @@ /* -** $Id: lopcodes.c,v 1.78 2018/02/15 15:34:29 roberto Exp roberto $ +** $Id: lopcodes.c,v 1.79 2018/02/21 15:49:32 roberto Exp roberto $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ @@ -79,6 +79,8 @@ LUAI_DDEF const char *const luaP_opnames[NUM_OPCODES+1] = { "GEI", "TEST", "TESTSET", + "UNDEF", + "ISDEF", "CALL", "TAILCALL", "RETURN", @@ -162,6 +164,8 @@ LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = { ,opmode(0, 0, 1, 0, iABC) /* OP_GEI */ ,opmode(0, 0, 1, 0, iABC) /* OP_TEST */ ,opmode(0, 0, 1, 1, iABC) /* OP_TESTSET */ + ,opmode(0, 0, 0, 0, iABC) /* OP_UNDEF */ + ,opmode(0, 0, 0, 1, iABC) /* OP_ISDEF */ ,opmode(1, 1, 0, 1, iABC) /* OP_CALL */ ,opmode(1, 1, 0, 1, iABC) /* OP_TAILCALL */ ,opmode(0, 1, 0, 0, iABC) /* OP_RETURN */ diff --git a/lopcodes.h b/lopcodes.h index be7359e951..ddbaf8dacd 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -1,5 +1,5 @@ /* -** $Id: lopcodes.h,v 1.188 2018/02/15 15:34:29 roberto Exp roberto $ +** $Id: lopcodes.h,v 1.189 2018/02/21 15:49:32 roberto Exp roberto $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ @@ -266,6 +266,9 @@ OP_GEI,/* A sB if ((R(A) >= sB) ~= k) then pc++ */ OP_TEST,/* A if (not R(A) == k) then pc++ */ OP_TESTSET,/* A B if (not R(B) == k) then R(A) := R(B) else pc++ */ +OP_UNDEF,/* A B R(A)[R(B)] = undef */ +OP_ISDEF,/* A B C R(A) = (R(B)[R(C)] == undef */ + OP_CALL,/* A B C R(A), ... ,R(A+C-2) := R(A)(R(A+1), ... ,R(A+B-1)) */ OP_TAILCALL,/* A B C return R(A)(R(A+1), ... ,R(A+B-1)) */ diff --git a/lparser.c b/lparser.c index da27c47222..9bf5485e48 100644 --- a/lparser.c +++ b/lparser.c @@ -1,5 +1,5 @@ /* -** $Id: lparser.c,v 2.177 2018/02/09 15:16:06 roberto Exp roberto $ +** $Id: lparser.c,v 2.178 2018/02/17 19:20:00 roberto Exp roberto $ ** Lua Parser ** See Copyright Notice in lua.h */ @@ -65,13 +65,6 @@ static void statement (LexState *ls); static void expr (LexState *ls, expdesc *v); -/* semantic error */ -static l_noret semerror (LexState *ls, const char *msg) { - ls->t.token = 0; /* remove "near " from final message */ - luaX_syntaxerror(ls, msg); -} - - static l_noret error_expected (LexState *ls, int token) { luaX_syntaxerror(ls, luaO_pushfstring(ls->L, "%s expected", luaX_token2str(ls, token))); @@ -347,7 +340,7 @@ static void closegoto (LexState *ls, int g, Labeldesc *label) { const char *msg = luaO_pushfstring(ls->L, " at line %d jumps into the scope of local '%s'", getstr(gt->name), gt->line, getstr(vname)); - semerror(ls, msg); + luaK_semerror(ls, msg); } luaK_patchgoto(fs, gt->pc, label->pc, 1); /* remove goto from pending list */ @@ -477,7 +470,7 @@ static void fixbreaks (FuncState *fs, BlockCnt *bl) { static l_noret undefgoto (LexState *ls, Labeldesc *gt) { const char *msg = "no visible label '%s' for at line %d"; msg = luaO_pushfstring(ls->L, msg, getstr(gt->name), gt->line); - semerror(ls, msg); + luaK_semerror(ls, msg); } @@ -900,6 +893,11 @@ static void primaryexp (LexState *ls, expdesc *v) { singlevar(ls, v); return; } + case TK_UNDEF: { + luaX_next(ls); + init_exp(v, VUNDEF, 0); + return; + } default: { luaX_syntaxerror(ls, "unexpected symbol"); } @@ -1185,6 +1183,10 @@ static void assignment (LexState *ls, struct LHS_assign *lh, int nvars) { else { /* assignment -> '=' explist */ int nexps; checknext(ls, '='); + if (nvars == 1 && testnext(ls, TK_UNDEF)) { + luaK_codeundef(ls->fs, &lh->v); + return; + } nexps = explist(ls, &e); if (nexps != nvars) adjust_assign(ls, nvars, nexps, &e); @@ -1237,7 +1239,7 @@ static void checkrepeated (FuncState *fs, Labellist *ll, TString *label) { const char *msg = luaO_pushfstring(fs->ls->L, "label '%s' already defined on line %d", getstr(label), ll->arr[i].line); - semerror(fs->ls, msg); + luaK_semerror(fs->ls, msg); } } } @@ -1650,6 +1652,11 @@ static void statement (LexState *ls) { luaX_next(ls); /* skip LOCAL */ if (testnext(ls, TK_FUNCTION)) /* local function? */ localfunc(ls); + else if (testnext(ls, TK_UNDEF)) + (void)0; /* ignore */ + /* old versions may need to declare 'local undef' + when using 'undef' with no environment; so this + version accepts (and ignores) these declarations */ else localstat(ls); break; diff --git a/lparser.h b/lparser.h index 6007d618bf..b8705e847c 100644 --- a/lparser.h +++ b/lparser.h @@ -1,5 +1,5 @@ /* -** $Id: lparser.h,v 1.79 2017/11/30 13:29:18 roberto Exp roberto $ +** $Id: lparser.h,v 1.80 2017/12/14 14:24:02 roberto Exp roberto $ ** Lua Parser ** See Copyright Notice in lua.h */ @@ -52,7 +52,8 @@ typedef enum { VRELOC, /* expression can put result in any register; info = instruction pc */ VCALL, /* expression is a function call; info = instruction pc */ - VVARARG /* vararg expression; info = instruction pc */ + VVARARG, /* vararg expression; info = instruction pc */ + VUNDEF /* the 'undef' "expression" */ } expkind; diff --git a/lvm.c b/lvm.c index 891e7d8d8a..b3abbf131a 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.348 2018/02/26 14:16:05 roberto Exp roberto $ +** $Id: lvm.c,v 2.349 2018/03/02 18:59:19 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -1552,6 +1552,18 @@ void luaV_execute (lua_State *L, CallInfo *ci) { } vmbreak; } + vmcase(OP_UNDEF) { + TValue *rb = vRB(i); + luaT_keydef(L, vra, rb, 1); + vmbreak; + } + vmcase(OP_ISDEF) { + TValue *rb = vRB(i); + TValue *rc = vRC(i); + int res = luaT_keydef(L, rb, rc, 0); + setbvalue(vra, res == GETARG_k(i)); + vmbreak; + } vmcase(OP_CALL) { int b = GETARG_B(i); int nresults = GETARG_C(i) - 1; From 6480e735998a1dc738d8babc86af154babb3843c Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 7 Mar 2018 13:26:01 -0300 Subject: [PATCH 0249/1145] details (avoid using non-C89 '//' comment) --- ldo.c | 3 +-- ljumptab.h | 11 ++++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/ldo.c b/ldo.c index 4126f47bdb..20f364500e 100644 --- a/ldo.c +++ b/ldo.c @@ -1,5 +1,5 @@ /* -** $Id: ldo.c,v 2.197 2018/02/26 14:16:05 roberto Exp roberto $ +** $Id: ldo.c,v 2.198 2018/03/05 14:13:55 roberto Exp roberto $ ** Stack and Call structure of Lua ** See Copyright Notice in lua.h */ @@ -687,7 +687,6 @@ LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs, : cast_int(L->top - (L->ci->func + 1)); L->nny = oldnny; /* restore 'nny' */ L->nCcalls--; - // lua_assert(L->nCcalls == ((from) ? from->nCcalls : 0)); lua_unlock(L); return status; } diff --git a/ljumptab.h b/ljumptab.h index f9783ffbaa..373d541c1b 100644 --- a/ljumptab.h +++ b/ljumptab.h @@ -11,11 +11,12 @@ static void *disptab[] = { -// you can update the following list with this command: -// -// sed -n '/^OP_/\!d; s/OP_/\&\&L_OP_/ ; s/,.*/,/ ; s/\/.*// ; p' lopcodes.h -// -// +#if 0 +** you can update the following list with this command: +** +** sed -n '/^OP_/\!d; s/OP_/\&\&L_OP_/ ; s/,.*/,/ ; s/\/.*// ; p' lopcodes.h +** +#endif &&L_OP_MOVE, &&L_OP_LOADI, From 40683b493474199e0d85d5dfcbb8dd6af03e6987 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 9 Mar 2018 11:56:02 -0300 Subject: [PATCH 0250/1145] added definition for LUA_MAXUNSIGNED --- luaconf.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/luaconf.h b/luaconf.h index 5a62ddac71..144f51baf8 100644 --- a/luaconf.h +++ b/luaconf.h @@ -1,5 +1,5 @@ /* -** $Id: luaconf.h,v 1.265 2018/02/27 18:47:32 roberto Exp roberto $ +** $Id: luaconf.h,v 1.266 2018/03/02 18:31:51 roberto Exp roberto $ ** Configuration file for Lua ** See Copyright Notice in lua.h */ @@ -446,6 +446,7 @@ @@ LUA_INTEGER_FMT is the format for writing integers. @@ LUA_MAXINTEGER is the maximum value for a LUA_INTEGER. @@ LUA_MININTEGER is the minimum value for a LUA_INTEGER. +@@ LUA_MAXUNSIGNED is the maximum value for a LUA_UNSIGNED. @@ lua_integer2str converts an integer to a string. */ @@ -465,6 +466,8 @@ */ #define LUA_UNSIGNED unsigned LUAI_UACINT +#define LUA_MAXUNSIGNED (~(lua_Unsigned)0) + /* now the variable definitions */ From 80ae1c1c16991ef37c7360aa12da9acc68e1c0ac Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 9 Mar 2018 11:56:25 -0300 Subject: [PATCH 0251/1145] fairer projection of random integers into an integer interval --- lmathlib.c | 69 ++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 54 insertions(+), 15 deletions(-) diff --git a/lmathlib.c b/lmathlib.c index 708a19c050..e889839649 100644 --- a/lmathlib.c +++ b/lmathlib.c @@ -1,5 +1,5 @@ /* -** $Id: lmathlib.c,v 1.119 2016/12/22 13:08:50 roberto Exp roberto $ +** $Id: lmathlib.c,v 1.120 2018/03/05 14:07:48 roberto Exp roberto $ ** Standard mathematical library ** See Copyright Notice in lua.h */ @@ -273,7 +273,7 @@ static I xorshift128plus (I *state) { } -#define mask53 (~(~0ll << 53)) +#define mask53 (~(~0LLU << 53)) /* ** Convert 53 bits from a random integer into a double in the @@ -283,8 +283,8 @@ static double I2d (I x) { return (x & mask53) * twotomin53; } -/* convert an 'I' to a lua_Integer */ -#define I2Int(x) ((lua_Integer)(x)) +/* convert an 'I' to a lua_Unsigned */ +#define I2UInt(x) ((lua_Unsigned)(x)) /* convert a lua_Integer to an 'I' */ #define Int2I(x) ((I)(x)) @@ -358,7 +358,7 @@ static I xorshift128plus (I *state) { */ #define mask32 0xffffffff -#define mask21 (~(~0 << 21)) +#define mask21 (~(~0U << 21)) #define twoto32 4294967296.0 /* 2^32 */ @@ -366,12 +366,12 @@ static double I2d (I x) { return ((x.x1 & mask21) * twoto32 + (x.x2 & mask32)) * twotomin53; } -static lua_Integer I2Int (I x) { - return (((lua_Integer)x.x1 << 31) << 1) | x.x2; +static lua_Unsigned I2UInt (I x) { + return ((lua_Unsigned)x.x1 << 31 << 1) | x.x2; } static I Int2I (lua_Integer n) { - return pack(n, (n >> 31) >> 1); + return pack(n, n >> 31 >> 1); } #endif /* } */ @@ -385,9 +385,50 @@ typedef struct { } RanState; +/* +** Project the random integer 'ran' into the interval [0, n]. +** Because 'ran' has 2^B possible values, the projection can only +** be uniform when the size of the interval [0, n] is a power of 2 +** (exact division). With the fairest possible projection (e.g., +** '(ran % (n + 1))'), the maximum bias is 1 in 2^B/n. +** For a "small" 'n', this bias is acceptable. (Here, we accept +** a maximum bias of 0.0001%.) For a larger 'n', we first +** compute 'lim', the smallest (2^b - 1) not smaller than 'n', +** to get a uniform projection into [0,lim]. If the result is +** inside [0, n], we are done. Otherwise, we try we another +** 'ran' until we have a result inside the interval. +*/ + +#define MAXBIAS 1000000 + +static lua_Unsigned project (lua_Unsigned ran, lua_Unsigned n, + RanState *state) { + if (n < LUA_MAXUNSIGNED / MAXBIAS) + return ran % (n + 1); + else { + /* compute the smallest (2^b - 1) not smaller than 'n' */ + lua_Unsigned lim = n; + lim |= (lim >> 1); + lim |= (lim >> 2); + lim |= (lim >> 4); + lim |= (lim >> 8); + lim |= (lim >> 16); +#if (LUA_MAXINTEGER >> 30 >> 2) > 0 + lim |= (lim >> 32); /* integer type has more than 32 bits */ +#endif + lua_assert((lim & (lim + 1)) == 0 /* 'lim + 1' is a power of 2 */ + && lim >= n /* not smaller than 'n' */ + && (lim >> 1) < n); /* it is the smallest one */ + while ((ran & lim) > n) + ran = I2UInt(xorshift128plus(state->s)); + return ran & lim; + } +} + + static int math_random (lua_State *L) { lua_Integer low, up; - double r; + lua_Unsigned p; RanState *state = (RanState *)lua_touserdata(L, lua_upvalueindex(1)); I rv = xorshift128plus(state->s); /* next pseudo-random value */ switch (lua_gettop(L)) { /* check number of arguments */ @@ -399,7 +440,7 @@ static int math_random (lua_State *L) { low = 1; up = luaL_checkinteger(L, 1); if (up == 0) { /* single 0 as argument? */ - lua_pushinteger(L, I2Int(rv)); /* full random integer */ + lua_pushinteger(L, I2UInt(rv)); /* full random integer */ return 1; } break; @@ -413,11 +454,9 @@ static int math_random (lua_State *L) { } /* random integer in the interval [low, up] */ luaL_argcheck(L, low <= up, 1, "interval is empty"); - luaL_argcheck(L, low >= 0 || up <= LUA_MAXINTEGER + low, 1, - "interval too large"); - r = I2d(rv); /* convert random value to a double */ - r *= (double)(up - low) + 1.0; /* scale it */ - lua_pushinteger(L, (lua_Integer)r + low); + /* project random integer into the interval [0, up - low] */ + p = project(I2UInt(rv), (lua_Unsigned)up - (lua_Unsigned)low, state); + lua_pushinteger(L, p + (lua_Unsigned)low); return 1; } From 0b3db69e4102c148b45a4491ef533ff449b0b927 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 9 Mar 2018 12:05:13 -0300 Subject: [PATCH 0252/1145] slight simplification in 'xorshift128plus' --- lmathlib.c | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/lmathlib.c b/lmathlib.c index e889839649..062da4a099 100644 --- a/lmathlib.c +++ b/lmathlib.c @@ -1,5 +1,5 @@ /* -** $Id: lmathlib.c,v 1.120 2018/03/05 14:07:48 roberto Exp roberto $ +** $Id: lmathlib.c,v 1.121 2018/03/09 14:56:25 roberto Exp roberto $ ** Standard mathematical library ** See Copyright Notice in lua.h */ @@ -268,7 +268,7 @@ static I xorshift128plus (I *state) { I y = state[1]; state[0] = y; x ^= x << 23; - state[1] = x ^ y ^ (x >> 18) ^ (y >> 5); + state[1] = (x ^ (x >> 18)) ^ (y ^ (y >> 5)); return state[1] + y; } @@ -318,12 +318,14 @@ static I pack (int x1, int x2) { return result; } -static I Ishl (I i, int n) { - return pack((i.x1 << n) | (i.x2 >> (32 - n)), i.x2 << n); +/* i ^ (i << n) */ +static I Ixorshl (I i, int n) { + return pack(i.x1 ^ ((i.x1 << n) | (i.x2 >> (32 - n))), i.x2 ^ (i.x2 << n)); } -static I Ishr (I i, int n) { - return pack(i.x1 >> n, (i.x2 >> n) | (i.x1 << (32 - n))); +/* i ^ (i >> n) */ +static I Ixorshr (I i, int n) { + return pack(i.x1 ^ (i.x1 >> n), i.x2 ^ ((i.x2 >> n) | (i.x1 << (32 - n)))); } static I Ixor (I i1, I i2) { @@ -345,9 +347,9 @@ static I xorshift128plus (I *state) { I x = state[0]; I y = state[1]; state[0] = y; - x = Ixor(x, Ishl(x, 23)); /* x ^= x << 23; */ - /* s[1] = x ^ y ^ (x >> 18) ^ (y >> 5); */ - state[1] = Ixor(Ixor(Ixor(x, y), Ishr(x, 18)), Ishr(y, 5)); + x = Ixorshl(x, 23); /* x ^= x << 23; */ + /* state[1] = (x ^ (x >> 18)) ^ (y ^ (y >> 5)); */ + state[1] = Ixor(Ixorshr(x, 18), Ixorshr(y, 5)); return Iadd(state[1], y); /* return state[1] + y; */ } From dbec41f34ca26f4762bbda3c6e99ba227508d743 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 9 Mar 2018 16:23:39 -0300 Subject: [PATCH 0253/1145] random floats of different sizes get exactly needed number of random bits (up to 64) --- lmathlib.c | 88 +++++++++++++++++++++++++++++++++++------------------- 1 file changed, 58 insertions(+), 30 deletions(-) diff --git a/lmathlib.c b/lmathlib.c index 062da4a099..2b5fd46de3 100644 --- a/lmathlib.c +++ b/lmathlib.c @@ -1,5 +1,5 @@ /* -** $Id: lmathlib.c,v 1.121 2018/03/09 14:56:25 roberto Exp roberto $ +** $Id: lmathlib.c,v 1.122 2018/03/09 15:05:13 roberto Exp roberto $ ** Standard mathematical library ** See Copyright Notice in lua.h */ @@ -10,9 +10,10 @@ #include "lprefix.h" +#include #include -#include #include +#include #include "lua.h" @@ -250,11 +251,17 @@ static int math_type (lua_State *L) { ** =================================================================== */ +/* number of binary digits in the mantissa of a float */ +#define FIGS l_mathlim(MANT_DIG) -#define twotomin53 (1.0 / 9007199254740992.0) /* 2^-53 */ +#if FIGS > 64 +/* there are only 64 random bits; use them all */ +#undef FIGS +#define FIGS 64 +#endif -#if defined(LLONG_MAX) && !defined(LUA_DEBUG) /* { */ +#if !defined(LUA_USE_C89) && defined(LLONG_MAX) && !defined(LUA_DEBUG) /* { */ /* ** Assume long long. @@ -272,15 +279,17 @@ static I xorshift128plus (I *state) { return state[1] + y; } +/* must take care to not shift stuff by more than 63 slots */ -#define mask53 (~(~0LLU << 53)) +#define maskFIG (~(~1LLU << (FIGS - 1))) /* use FIGS bits */ +#define shiftFIG (l_mathop(0.5) / (1LLU << (FIGS - 1))) /* 2^(-FIG) */ /* -** Convert 53 bits from a random integer into a double in the +** Convert bits from a random integer into a float in the ** interval [0,1). */ -static double I2d (I x) { - return (x & mask53) * twotomin53; +static lua_Number I2d (I x) { + return (lua_Number)(x & maskFIG) * shiftFIG; } /* convert an 'I' to a lua_Unsigned */ @@ -289,10 +298,10 @@ static double I2d (I x) { /* convert a lua_Integer to an 'I' */ #define Int2I(x) ((I)(x)) -#else /* }{ */ +#else /* no long long }{ */ /* -** No long long; Use two 32-bit integers to represent a 64-bit quantity. +** Use two 32-bit integers to represent a 64-bit quantity. */ #if LUAI_BITSINT >= 32 @@ -303,7 +312,8 @@ typedef unsigned long lu_int32; /* a 64-bit value */ typedef struct I { - lu_int32 x1, x2; + lu_int32 h; /* higher half */ + lu_int32 l; /* lower half */ } I; @@ -311,31 +321,31 @@ typedef struct I { ** basic operations on 'I' values */ -static I pack (int x1, int x2) { +static I pack (int h, int l) { I result; - result.x1 = x1; - result.x2 = x2; + result.h = h; + result.l = l; return result; } /* i ^ (i << n) */ static I Ixorshl (I i, int n) { - return pack(i.x1 ^ ((i.x1 << n) | (i.x2 >> (32 - n))), i.x2 ^ (i.x2 << n)); + return pack(i.h ^ ((i.h << n) | (i.l >> (32 - n))), i.l ^ (i.l << n)); } /* i ^ (i >> n) */ static I Ixorshr (I i, int n) { - return pack(i.x1 ^ (i.x1 >> n), i.x2 ^ ((i.x2 >> n) | (i.x1 << (32 - n)))); + return pack(i.h ^ (i.h >> n), i.l ^ ((i.l >> n) | (i.h << (32 - n)))); } static I Ixor (I i1, I i2) { - return pack(i1.x1 ^ i2.x1, i1.x2 ^ i2.x2); + return pack(i1.h ^ i2.h, i1.l ^ i2.l); } static I Iadd (I i1, I i2) { - I result = pack(i1.x1 + i2.x1, i1.x2 + i2.x2); - if (result.x2 < i1.x2) /* carry? */ - result.x1++; + I result = pack(i1.h + i2.h, i1.l + i2.l); + if (result.l < i1.l) /* carry? */ + result.h++; return result; } @@ -355,28 +365,46 @@ static I xorshift128plus (I *state) { /* -** Converts an 'I' into a double, getting its lower half plus 21 -** (53 - 32) bits from its higher half and joining them into a double. +** Converts an 'I' into a float. */ -#define mask32 0xffffffff -#define mask21 (~(~0U << 21)) +#if FIGS <= 32 + +/* do not need bits from higher half */ +#define maskHF 0 +#define maskLOW (~(~1U << (FIGS - 1))) /* use FIG bits */ +#define shiftFIG (0.5 / (1U << (FIGS - 1))) /* 2^(-FIG) */ + +#else /* 32 < FIGS <= 64 */ + +/* must take care to not shift stuff by more than 31 slots */ + +/* use FIG - 32 bits from higher half */ +#define maskHF (~(~1U << (FIGS - 33))) + +/* use all bits from lower half */ +#define maskLOW (~0) + +/* 2^(-FIG) == (1 / 2^33) / 2^(FIG-33) */ +#define shiftFIG ((lua_Number)(1.0 / 8589934592.0) / (1U << (FIGS - 33))) + +#endif -#define twoto32 4294967296.0 /* 2^32 */ +#define twoto32 l_mathop(4294967296.0) /* 2^32 */ -static double I2d (I x) { - return ((x.x1 & mask21) * twoto32 + (x.x2 & mask32)) * twotomin53; +static lua_Number I2d (I x) { + return ((x.h & maskHF) * twoto32 + (x.l & maskLOW)) * shiftFIG; } static lua_Unsigned I2UInt (I x) { - return ((lua_Unsigned)x.x1 << 31 << 1) | x.x2; + return ((lua_Unsigned)x.h << 31 << 1) | x.l; } static I Int2I (lua_Integer n) { return pack(n, n >> 31 >> 1); } -#endif /* } */ +#endif /* } */ /* @@ -435,7 +463,7 @@ static int math_random (lua_State *L) { I rv = xorshift128plus(state->s); /* next pseudo-random value */ switch (lua_gettop(L)) { /* check number of arguments */ case 0: { /* no arguments */ - lua_pushnumber(L, (lua_Number)I2d(rv)); /* float between 0 and 1 */ + lua_pushnumber(L, I2d(rv)); /* float between 0 and 1 */ return 1; } case 1: { /* only upper limit */ From 9e3db70482e927e5992cccdb5a40424b33ad0a29 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 9 Mar 2018 16:24:45 -0300 Subject: [PATCH 0254/1145] details (casts between 'lua_Number' and 'double') --- ltests.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ltests.c b/ltests.c index a5781fb626..bc8b6b43f1 100644 --- a/ltests.c +++ b/ltests.c @@ -1,5 +1,5 @@ /* -** $Id: ltests.c,v 2.241 2018/02/20 16:52:50 roberto Exp roberto $ +** $Id: ltests.c,v 2.242 2018/02/23 13:13:31 roberto Exp roberto $ ** Internal Module for Debugging of the Lua Implementation ** See Copyright Notice in lua.h */ @@ -957,13 +957,13 @@ static int doonnewstack (lua_State *L) { static int s2d (lua_State *L) { - lua_pushnumber(L, *cast(const double *, luaL_checkstring(L, 1))); + lua_pushnumber(L, cast_num(*cast(const double *, luaL_checkstring(L, 1)))); return 1; } static int d2s (lua_State *L) { - double d = luaL_checknumber(L, 1); + double d = cast(double, luaL_checknumber(L, 1)); lua_pushlstring(L, cast_charp(&d), sizeof(d)); return 1; } From e3388ebfad4a566c933b2b1562cfa9f40a8afc13 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Sun, 11 Mar 2018 11:48:09 -0300 Subject: [PATCH 0255/1145] more explicit casts when converting an integer to a random float (to ensure computations are done with all bits) --- lmathlib.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lmathlib.c b/lmathlib.c index 2b5fd46de3..b60c661ac3 100644 --- a/lmathlib.c +++ b/lmathlib.c @@ -1,5 +1,5 @@ /* -** $Id: lmathlib.c,v 1.122 2018/03/09 15:05:13 roberto Exp roberto $ +** $Id: lmathlib.c,v 1.123 2018/03/09 19:23:39 roberto Exp roberto $ ** Standard mathematical library ** See Copyright Notice in lua.h */ @@ -373,7 +373,7 @@ static I xorshift128plus (I *state) { /* do not need bits from higher half */ #define maskHF 0 #define maskLOW (~(~1U << (FIGS - 1))) /* use FIG bits */ -#define shiftFIG (0.5 / (1U << (FIGS - 1))) /* 2^(-FIG) */ +#define shiftFIG (l_mathop(0.5) / (1U << (FIGS - 1))) /* 2^(-FIG) */ #else /* 32 < FIGS <= 64 */ @@ -393,7 +393,9 @@ static I xorshift128plus (I *state) { #define twoto32 l_mathop(4294967296.0) /* 2^32 */ static lua_Number I2d (I x) { - return ((x.h & maskHF) * twoto32 + (x.l & maskLOW)) * shiftFIG; + lua_Number h = (lua_Number)(x.h & maskHF); + lua_Number l = (lua_Number)(x.l & maskLOW); + return (h * twoto32 + l) * shiftFIG; } static lua_Unsigned I2UInt (I x) { From 6b01b6cf6a1631f7ca2ce527a5c355517095c209 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 12 Mar 2018 09:39:03 -0300 Subject: [PATCH 0256/1145] 'lu_int32' may not be 'int' --- lmathlib.c | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/lmathlib.c b/lmathlib.c index b60c661ac3..c5c3c54114 100644 --- a/lmathlib.c +++ b/lmathlib.c @@ -1,5 +1,5 @@ /* -** $Id: lmathlib.c,v 1.123 2018/03/09 19:23:39 roberto Exp roberto $ +** $Id: lmathlib.c,v 1.124 2018/03/11 14:48:09 roberto Exp roberto $ ** Standard mathematical library ** See Copyright Notice in lua.h */ @@ -321,7 +321,7 @@ typedef struct I { ** basic operations on 'I' values */ -static I pack (int h, int l) { +static I pack (lu_int32 h, lu_int32 l) { I result; result.h = h; result.l = l; @@ -368,25 +368,27 @@ static I xorshift128plus (I *state) { ** Converts an 'I' into a float. */ +/* an unsigned 1 with proper type */ +#define UONE ((lu_int32)1) + #if FIGS <= 32 -/* do not need bits from higher half */ -#define maskHF 0 -#define maskLOW (~(~1U << (FIGS - 1))) /* use FIG bits */ -#define shiftFIG (l_mathop(0.5) / (1U << (FIGS - 1))) /* 2^(-FIG) */ +#define maskHF 0 /* do not need bits from higher half */ +#define maskLOW (~(~UONE << (FIGS - 1))) /* use FIG bits */ +#define shiftFIG (l_mathop(0.5) / (UONE << (FIGS - 1))) /* 2^(-FIG) */ #else /* 32 < FIGS <= 64 */ /* must take care to not shift stuff by more than 31 slots */ /* use FIG - 32 bits from higher half */ -#define maskHF (~(~1U << (FIGS - 33))) +#define maskHF (~(~UONE << (FIGS - 33))) /* use all bits from lower half */ -#define maskLOW (~0) +#define maskLOW (~(lu_int32)0) /* 2^(-FIG) == (1 / 2^33) / 2^(FIG-33) */ -#define shiftFIG ((lua_Number)(1.0 / 8589934592.0) / (1U << (FIGS - 33))) +#define shiftFIG ((lua_Number)(1.0 / 8589934592.0) / (UONE << (FIGS - 33))) #endif @@ -403,7 +405,8 @@ static lua_Unsigned I2UInt (I x) { } static I Int2I (lua_Integer n) { - return pack(n, n >> 31 >> 1); + lua_Unsigned un = n; + return pack((lu_int32)un, (lu_int32)(un >> 31 >> 1)); } #endif /* } */ From 89da4168df5c8f6199504fed6e25fc5326c14fd2 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 16 Mar 2018 11:18:18 -0300 Subject: [PATCH 0257/1145] avoid functions named 'pack' (name too common, may collide when doing 'onelua.c') --- lmathlib.c | 14 +++++++------- ltablib.c | 10 +++++----- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/lmathlib.c b/lmathlib.c index c5c3c54114..aa9ef51a6b 100644 --- a/lmathlib.c +++ b/lmathlib.c @@ -1,5 +1,5 @@ /* -** $Id: lmathlib.c,v 1.124 2018/03/11 14:48:09 roberto Exp roberto $ +** $Id: lmathlib.c,v 1.125 2018/03/12 12:39:03 roberto Exp roberto $ ** Standard mathematical library ** See Copyright Notice in lua.h */ @@ -321,7 +321,7 @@ typedef struct I { ** basic operations on 'I' values */ -static I pack (lu_int32 h, lu_int32 l) { +static I packI (lu_int32 h, lu_int32 l) { I result; result.h = h; result.l = l; @@ -330,20 +330,20 @@ static I pack (lu_int32 h, lu_int32 l) { /* i ^ (i << n) */ static I Ixorshl (I i, int n) { - return pack(i.h ^ ((i.h << n) | (i.l >> (32 - n))), i.l ^ (i.l << n)); + return packI(i.h ^ ((i.h << n) | (i.l >> (32 - n))), i.l ^ (i.l << n)); } /* i ^ (i >> n) */ static I Ixorshr (I i, int n) { - return pack(i.h ^ (i.h >> n), i.l ^ ((i.l >> n) | (i.h << (32 - n)))); + return packI(i.h ^ (i.h >> n), i.l ^ ((i.l >> n) | (i.h << (32 - n)))); } static I Ixor (I i1, I i2) { - return pack(i1.h ^ i2.h, i1.l ^ i2.l); + return packI(i1.h ^ i2.h, i1.l ^ i2.l); } static I Iadd (I i1, I i2) { - I result = pack(i1.h + i2.h, i1.l + i2.l); + I result = packI(i1.h + i2.h, i1.l + i2.l); if (result.l < i1.l) /* carry? */ result.h++; return result; @@ -406,7 +406,7 @@ static lua_Unsigned I2UInt (I x) { static I Int2I (lua_Integer n) { lua_Unsigned un = n; - return pack((lu_int32)un, (lu_int32)(un >> 31 >> 1)); + return packI((lu_int32)un, (lu_int32)(un >> 31 >> 1)); } #endif /* } */ diff --git a/ltablib.c b/ltablib.c index 22bfae427a..5a78c15bc2 100644 --- a/ltablib.c +++ b/ltablib.c @@ -1,5 +1,5 @@ /* -** $Id: ltablib.c,v 1.94 2018/02/25 12:48:16 roberto Exp roberto $ +** $Id: ltablib.c,v 1.95 2018/02/27 18:47:32 roberto Exp roberto $ ** Library for Table Manipulation ** See Copyright Notice in lua.h */ @@ -173,7 +173,7 @@ static int tconcat (lua_State *L) { ** ======================================================= */ -static int pack (lua_State *L) { +static int tpack (lua_State *L) { int i; int n = lua_gettop(L); /* number of elements to pack */ lua_createtable(L, n, 1); /* create result table */ @@ -186,7 +186,7 @@ static int pack (lua_State *L) { } -static int unpack (lua_State *L) { +static int tunpack (lua_State *L) { lua_Unsigned n; lua_Integer i = luaL_optinteger(L, 2, 1); lua_Integer e = luaL_opt(L, luaL_checkinteger, 3, luaL_len(L, 1)); @@ -408,8 +408,8 @@ static int sort (lua_State *L) { static const luaL_Reg tab_funcs[] = { {"concat", tconcat}, {"insert", tinsert}, - {"pack", pack}, - {"unpack", unpack}, + {"pack", tpack}, + {"unpack", tunpack}, {"remove", tremove}, {"move", tmove}, {"sort", sort}, From 7b0b6b3b3945b1adcf460321227243143d7e1543 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 16 Mar 2018 11:21:20 -0300 Subject: [PATCH 0258/1145] cannot use 'defined' inside a macro + call to 'luaT_keydef' must be protected --- lvm.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/lvm.c b/lvm.c index b3abbf131a..fd74bff542 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.349 2018/03/02 18:59:19 roberto Exp roberto $ +** $Id: lvm.c,v 2.350 2018/03/07 15:55:38 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -36,7 +36,11 @@ ** and compatible compilers. */ #if !defined(LUA_USE_JUMPTABLE) -#define LUA_USE_JUMPTABLE defined(__GNUC__) +#if defined(__GNUC__) +#define LUA_USE_JUMPTABLE 1 +#else +#define LUA_USE_JUMPTABLE 0 +#endif #endif @@ -1560,7 +1564,8 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmcase(OP_ISDEF) { TValue *rb = vRB(i); TValue *rc = vRC(i); - int res = luaT_keydef(L, rb, rc, 0); + int res; + Protect(res = luaT_keydef(L, rb, rc, 0)); setbvalue(vra, res == GETARG_k(i)); vmbreak; } From 0c7738240ef81b88ded842e3a8ebfb3180bcf00c Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 16 Mar 2018 11:22:09 -0300 Subject: [PATCH 0259/1145] FALLTHROUGH comment must be last "statement" (so it does not work when inside a block) --- lcode.c | 5 ++--- lgc.c | 6 +++--- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/lcode.c b/lcode.c index 95e87f317f..661c791615 100644 --- a/lcode.c +++ b/lcode.c @@ -1,5 +1,5 @@ /* -** $Id: lcode.c,v 2.158 2018/02/26 14:16:05 roberto Exp roberto $ +** $Id: lcode.c,v 2.159 2018/03/07 15:55:38 roberto Exp roberto $ ** Code generator for Lua ** See Copyright Notice in lua.h */ @@ -1696,8 +1696,7 @@ void luaK_finish (FuncState *fs) { break; /* no extra work */ /* else use OP_RETURN to do the extra work */ SET_OPCODE(*pc, OP_RETURN); - /* FALLTHROUGH */ - } + } /* FALLTHROUGH */ case OP_RETURN: case OP_TAILCALL: { if (p->sizep > 0 || p->is_vararg) { SETARG_C(*pc, p->is_vararg ? p->numparams + 1 : 0); diff --git a/lgc.c b/lgc.c index 9da0f498e0..2e401cbdb0 100644 --- a/lgc.c +++ b/lgc.c @@ -1,5 +1,5 @@ /* -** $Id: lgc.c,v 2.251 2018/02/23 13:21:27 roberto Exp roberto $ +** $Id: lgc.c,v 2.252 2018/02/26 13:35:03 roberto Exp roberto $ ** Garbage Collector ** See Copyright Notice in lua.h */ @@ -301,8 +301,8 @@ static void reallymarkobject (global_State *g, GCObject *o) { gray2black(o); /* nothing else to mark */ break; } - /* else *//* FALLTHROUGH */ - } + /* else... */ + } /* FALLTHROUGH */ case LUA_TLCL: case LUA_TCCL: case LUA_TTABLE: case LUA_TTHREAD: case LUA_TPROTO: { linkobjgclist(o, g->gray); From c3cb31fa9aae0c004c1c145b2268c0ffa35a3f62 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 16 Mar 2018 11:23:08 -0300 Subject: [PATCH 0260/1145] some extra goodies for 'readline' ('rl_readline_name' and 'rl_inhibit_completion') --- lua.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lua.c b/lua.c index 2e95c40b3b..ae783a0d7e 100644 --- a/lua.c +++ b/lua.c @@ -1,5 +1,5 @@ /* -** $Id: lua.c,v 1.233 2018/02/06 15:32:36 roberto Exp roberto $ +** $Id: lua.c,v 1.234 2018/03/06 20:30:17 roberto Exp roberto $ ** Lua stand-alone interpreter ** See Copyright Notice in lua.h */ @@ -383,12 +383,15 @@ static int handle_luainit (lua_State *L) { #include #include +#define lua_initreadline(L) \ + ((void)L, rl_readline_name="lua", rl_inhibit_completion=1) #define lua_readline(L,b,p) ((void)L, ((b)=readline(p)) != NULL) #define lua_saveline(L,line) ((void)L, add_history(line)) #define lua_freeline(L,b) ((void)L, free(b)) #else /* }{ */ +#define lua_initreadline(L) ((void)L) #define lua_readline(L,b,p) \ ((void)L, fputs(p, stdout), fflush(stdout), /* show prompt */ \ fgets(b, LUA_MAXINPUT, stdin) != NULL) /* get line */ @@ -539,6 +542,7 @@ static void doREPL (lua_State *L) { int status; const char *oldprogname = progname; progname = NULL; /* no 'progname' on errors in interactive mode */ + lua_initreadline(L); while ((status = loadline(L)) != -1) { if (status == LUA_OK) status = docall(L, 0, LUA_MULTRET); From 4907444db9d31f340f29f6ff7c9284ee06a03f5b Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 16 Mar 2018 12:33:34 -0300 Subject: [PATCH 0261/1145] 'fTransfer' -> 'ftransfer' / 'nTransfer' -> 'ntransfer' (keep the standard of names in lower case) --- ldblib.c | 6 +++--- ldebug.c | 8 ++++---- ldo.c | 16 ++++++++-------- lstate.h | 6 +++--- lua.h | 6 +++--- 5 files changed, 21 insertions(+), 21 deletions(-) diff --git a/ldblib.c b/ldblib.c index cb542f67d5..a977654378 100644 --- a/ldblib.c +++ b/ldblib.c @@ -1,5 +1,5 @@ /* -** $Id: ldblib.c,v 1.153 2018/02/20 16:52:50 roberto Exp roberto $ +** $Id: ldblib.c,v 1.154 2018/03/05 14:15:04 roberto Exp roberto $ ** Interface from Lua to its debug API ** See Copyright Notice in lua.h */ @@ -186,8 +186,8 @@ static int db_getinfo (lua_State *L) { settabss(L, "namewhat", ar.namewhat); } if (strchr(options, 'r')) { - settabsi(L, "fTransfer", ar.fTransfer); - settabsi(L, "nTransfer", ar.nTransfer); + settabsi(L, "ftransfer", ar.ftransfer); + settabsi(L, "ntransfer", ar.ntransfer); } if (strchr(options, 't')) settabsb(L, "istailcall", ar.istailcall); diff --git a/ldebug.c b/ldebug.c index bede7e3710..996e8cb146 100644 --- a/ldebug.c +++ b/ldebug.c @@ -1,5 +1,5 @@ /* -** $Id: ldebug.c,v 2.154 2018/02/09 15:16:06 roberto Exp roberto $ +** $Id: ldebug.c,v 2.155 2018/02/17 19:29:29 roberto Exp roberto $ ** Debug Interface ** See Copyright Notice in lua.h */ @@ -358,10 +358,10 @@ static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar, } case 'r': { if (ci == NULL || !(ci->callstatus & CIST_TRAN)) - ar->fTransfer = ar->nTransfer = 0; + ar->ftransfer = ar->ntransfer = 0; else { - ar->fTransfer = ci->u2.transferinfo.fTransfer; - ar->nTransfer = ci->u2.transferinfo.nTransfer; + ar->ftransfer = ci->u2.transferinfo.ftransfer; + ar->ntransfer = ci->u2.transferinfo.ntransfer; } } case 'L': diff --git a/ldo.c b/ldo.c index 20f364500e..41b41756ed 100644 --- a/ldo.c +++ b/ldo.c @@ -1,5 +1,5 @@ /* -** $Id: ldo.c,v 2.198 2018/03/05 14:13:55 roberto Exp roberto $ +** $Id: ldo.c,v 2.199 2018/03/07 16:26:01 roberto Exp roberto $ ** Stack and Call structure of Lua ** See Copyright Notice in lua.h */ @@ -268,7 +268,7 @@ void luaD_inctop (lua_State *L) { ** function, can be changed asynchronously by signals.) */ void luaD_hook (lua_State *L, int event, int line, - int fTransfer, int nTransfer) { + int ftransfer, int ntransfer) { lua_Hook hook = L->hook; if (hook && L->allowhook) { /* make sure there is a hook */ int mask = CIST_HOOKED; @@ -279,10 +279,10 @@ void luaD_hook (lua_State *L, int event, int line, ar.event = event; ar.currentline = line; ar.i_ci = ci; - if (nTransfer != 0) { + if (ntransfer != 0) { mask |= CIST_TRAN; /* 'ci' has transfer information */ - ci->u2.transferinfo.fTransfer = fTransfer; - ci->u2.transferinfo.nTransfer = nTransfer; + ci->u2.transferinfo.ftransfer = ftransfer; + ci->u2.transferinfo.ntransfer = ntransfer; } luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */ if (L->top + LUA_MINSTACK > ci->top) @@ -329,10 +329,10 @@ static void rethook (lua_State *L, CallInfo *ci, StkId firstres, int nres) { L->top = ci->top; /* correct top */ } if (L->hookmask & LUA_MASKRET) { /* is return hook on? */ - int fTransfer; + int ftransfer; ci->func += delta; /* if vararg, back to virtual 'func' */ - fTransfer = cast(unsigned short, firstres - ci->func); - luaD_hook(L, LUA_HOOKRET, -1, fTransfer, nres); /* call it */ + ftransfer = cast(unsigned short, firstres - ci->func); + luaD_hook(L, LUA_HOOKRET, -1, ftransfer, nres); /* call it */ ci->func -= delta; } if (isLua(ci->previous)) diff --git a/lstate.h b/lstate.h index 974249de3b..cd8b4db7dc 100644 --- a/lstate.h +++ b/lstate.h @@ -1,5 +1,5 @@ /* -** $Id: lstate.h,v 2.156 2018/02/17 19:29:29 roberto Exp roberto $ +** $Id: lstate.h,v 2.157 2018/02/25 12:43:52 roberto Exp roberto $ ** Global State ** See Copyright Notice in lua.h */ @@ -104,8 +104,8 @@ typedef struct CallInfo { int funcidx; /* called-function index */ int nyield; /* number of values yielded */ struct { /* info about transfered values (for call/return hooks) */ - unsigned short fTransfer; /* offset of first value transfered */ - unsigned short nTransfer; /* number of values transfered */ + unsigned short ftransfer; /* offset of first value transfered */ + unsigned short ntransfer; /* number of values transfered */ } transferinfo; } u2; short nresults; /* expected number of results from this function */ diff --git a/lua.h b/lua.h index 5183459fc6..31e203971b 100644 --- a/lua.h +++ b/lua.h @@ -1,5 +1,5 @@ /* -** $Id: lua.h,v 1.343 2018/03/02 16:30:47 roberto Exp roberto $ +** $Id: lua.h,v 1.344 2018/03/05 14:15:32 roberto Exp roberto $ ** Lua - A Scripting Language ** Lua.org, PUC-Rio, Brazil (http://www.lua.org) ** See Copyright Notice at the end of this file @@ -461,8 +461,8 @@ struct lua_Debug { unsigned char nparams;/* (u) number of parameters */ char isvararg; /* (u) */ char istailcall; /* (t) */ - unsigned short fTransfer;/* (r) index of first value transfered */ - unsigned short nTransfer; /* (r) number of transfered values */ + unsigned short ftransfer; /* (r) index of first value transferred */ + unsigned short ntransfer; /* (r) number of transferred values */ char short_src[LUA_IDSIZE]; /* (S) */ /* private part */ struct CallInfo *i_ci; /* active function */ From 64867624630b4effacb3d3fb54708ecda19f0221 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 19 Mar 2018 17:03:44 -0300 Subject: [PATCH 0262/1145] missing LUAI_FUNC in prototype for 'luaK_patchgoto' --- lcode.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lcode.h b/lcode.h index e7c294d4a1..6ec8b0993a 100644 --- a/lcode.h +++ b/lcode.h @@ -1,5 +1,5 @@ /* -** $Id: lcode.h,v 1.70 2017/12/18 15:44:44 roberto Exp roberto $ +** $Id: lcode.h,v 1.71 2018/03/07 15:55:38 roberto Exp roberto $ ** Code generator for Lua ** See Copyright Notice in lua.h */ @@ -78,7 +78,8 @@ LUAI_FUNC void luaK_setoneret (FuncState *fs, expdesc *e); LUAI_FUNC int luaK_jump (FuncState *fs); LUAI_FUNC void luaK_ret (FuncState *fs, int first, int nret); LUAI_FUNC void luaK_patchlist (FuncState *fs, int list, int target); -void luaK_patchgoto (FuncState *fs, int list, int target, int hasclose); +LUAI_FUNC void luaK_patchgoto (FuncState *fs, int list, int target, + int hasclose); LUAI_FUNC void luaK_patchtohere (FuncState *fs, int list); LUAI_FUNC void luaK_patchclose (FuncState *fs, int list); LUAI_FUNC void luaK_concat (FuncState *fs, int *l1, int l2); From c5e3b2f81402773466f2afc8e87839509f65facc Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 22 Mar 2018 16:54:49 -0300 Subject: [PATCH 0263/1145] in random/'project', remove the special case for "small" intervals; it is slower than the general case. --- lmathlib.c | 39 +++++++++++++++------------------------ 1 file changed, 15 insertions(+), 24 deletions(-) diff --git a/lmathlib.c b/lmathlib.c index aa9ef51a6b..55b44f8bf1 100644 --- a/lmathlib.c +++ b/lmathlib.c @@ -1,5 +1,5 @@ /* -** $Id: lmathlib.c,v 1.125 2018/03/12 12:39:03 roberto Exp roberto $ +** $Id: lmathlib.c,v 1.126 2018/03/16 14:18:18 roberto Exp roberto $ ** Standard mathematical library ** See Copyright Notice in lua.h */ @@ -422,27 +422,18 @@ typedef struct { /* ** Project the random integer 'ran' into the interval [0, n]. -** Because 'ran' has 2^B possible values, the projection can only -** be uniform when the size of the interval [0, n] is a power of 2 -** (exact division). With the fairest possible projection (e.g., -** '(ran % (n + 1))'), the maximum bias is 1 in 2^B/n. -** For a "small" 'n', this bias is acceptable. (Here, we accept -** a maximum bias of 0.0001%.) For a larger 'n', we first -** compute 'lim', the smallest (2^b - 1) not smaller than 'n', -** to get a uniform projection into [0,lim]. If the result is -** inside [0, n], we are done. Otherwise, we try we another -** 'ran' until we have a result inside the interval. +** Because 'ran' has 2^B possible values, the projection can only be +** uniform when the size of the interval [0, n] is a power of 2 (exact +** division). To get a uniform projection into [0,lim], we first +** compute 'lim', the smallest (2^b - 1) not smaller than 'n'. If the +** random number is inside [0, n], we are done. Otherwise, we try with +** another 'ran' until we have a result inside the interval. */ - -#define MAXBIAS 1000000 - static lua_Unsigned project (lua_Unsigned ran, lua_Unsigned n, RanState *state) { - if (n < LUA_MAXUNSIGNED / MAXBIAS) - return ran % (n + 1); - else { + lua_Unsigned lim = n; + if ((lim & (lim + 1)) > 0) { /* 'lim + 1' is not a power of 2? */ /* compute the smallest (2^b - 1) not smaller than 'n' */ - lua_Unsigned lim = n; lim |= (lim >> 1); lim |= (lim >> 2); lim |= (lim >> 4); @@ -451,13 +442,13 @@ static lua_Unsigned project (lua_Unsigned ran, lua_Unsigned n, #if (LUA_MAXINTEGER >> 30 >> 2) > 0 lim |= (lim >> 32); /* integer type has more than 32 bits */ #endif - lua_assert((lim & (lim + 1)) == 0 /* 'lim + 1' is a power of 2 */ - && lim >= n /* not smaller than 'n' */ - && (lim >> 1) < n); /* it is the smallest one */ - while ((ran & lim) > n) - ran = I2UInt(xorshift128plus(state->s)); - return ran & lim; } + lua_assert((lim & (lim + 1)) == 0 /* 'lim + 1' is a power of 2 */ + && lim >= n /* not smaller than 'n' */ + && (lim == 0 || (lim >> 1) < n)); /* it is the smallest one */ + while ((ran & lim) > n) + ran = I2UInt(xorshift128plus(state->s)); + return ran & lim; } From bdd10a08b179d9825c1862ff8ac94cb62799e19f Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 26 Mar 2018 16:48:46 -0300 Subject: [PATCH 0264/1145] in 'random', uses high-order bits instead of low-order (better statistical properties) --- lmathlib.c | 117 +++++++++++++++++++++++++++++++---------------------- 1 file changed, 68 insertions(+), 49 deletions(-) diff --git a/lmathlib.c b/lmathlib.c index 55b44f8bf1..9432d40537 100644 --- a/lmathlib.c +++ b/lmathlib.c @@ -1,5 +1,5 @@ /* -** $Id: lmathlib.c,v 1.126 2018/03/16 14:18:18 roberto Exp roberto $ +** $Id: lmathlib.c,v 1.127 2018/03/22 19:54:49 roberto Exp roberto $ ** Standard mathematical library ** See Copyright Notice in lua.h */ @@ -281,19 +281,19 @@ static I xorshift128plus (I *state) { /* must take care to not shift stuff by more than 63 slots */ -#define maskFIG (~(~1LLU << (FIGS - 1))) /* use FIGS bits */ -#define shiftFIG (l_mathop(0.5) / (1LLU << (FIGS - 1))) /* 2^(-FIG) */ +#define shiftI (64 - FIGS) /* leave FIGS bits */ +#define shiftF (l_mathop(0.5) / (1LLU << (FIGS - 1))) /* 2^(-FIG) */ /* ** Convert bits from a random integer into a float in the ** interval [0,1). */ static lua_Number I2d (I x) { - return (lua_Number)(x & maskFIG) * shiftFIG; + return (lua_Number)(x >> shiftI) * shiftF; } -/* convert an 'I' to a lua_Unsigned */ -#define I2UInt(x) ((lua_Unsigned)(x)) +/* convert an 'I' to a lua_Unsigned (using higher bits) */ +#define I2UInt(x) ((lua_Unsigned)((x) >> (64 - LUA_UNSIGNEDBITS))) /* convert a lua_Integer to an 'I' */ #define Int2I(x) ((I)(x)) @@ -373,40 +373,47 @@ static I xorshift128plus (I *state) { #if FIGS <= 32 -#define maskHF 0 /* do not need bits from higher half */ -#define maskLOW (~(~UONE << (FIGS - 1))) /* use FIG bits */ -#define shiftFIG (l_mathop(0.5) / (UONE << (FIGS - 1))) /* 2^(-FIG) */ +#define maskLOW 0 /* do not need bits from lower half */ +#define maskHI (~(~(lu_int32)0 >> (FIGS - 1) >> 1)) /* use FIGS bits */ +#define shiftHI 1 /* no shift */ +#define shiftF (1 / l_mathop(4294967296.0)) /* 2^(-32) */ #else /* 32 < FIGS <= 64 */ /* must take care to not shift stuff by more than 31 slots */ -/* use FIG - 32 bits from higher half */ -#define maskHF (~(~UONE << (FIGS - 33))) +/* use FIGS - 32 bits from lower half */ +#define maskLOW (~(~(lu_int32)0 >> (FIGS - 33) >> 1)) -/* use all bits from lower half */ -#define maskLOW (~(lu_int32)0) +/* use all bits from higher half */ +#define maskHI (~(lu_int32)0) -/* 2^(-FIG) == (1 / 2^33) / 2^(FIG-33) */ -#define shiftFIG ((lua_Number)(1.0 / 8589934592.0) / (UONE << (FIGS - 33))) +#define shiftHI l_mathop(4294967296.0) /* 2^32 */ -#endif +/* 2^(-64) */ +#define shiftF ((lua_Number)(1 / (shiftHI * shiftHI))) -#define twoto32 l_mathop(4294967296.0) /* 2^32 */ +#endif static lua_Number I2d (I x) { - lua_Number h = (lua_Number)(x.h & maskHF); + lua_Number h = (lua_Number)(x.h & maskHI); lua_Number l = (lua_Number)(x.l & maskLOW); - return (h * twoto32 + l) * shiftFIG; + return (h * shiftHI + l) * shiftF; } static lua_Unsigned I2UInt (I x) { - return ((lua_Unsigned)x.h << 31 << 1) | x.l; +#if (LUA_MAXINTEGER >> 30) <= 1 +/* at most 32 bits; use only high bits */ + return ((lua_Unsigned)x.h); +#else +/* at least 33 bits */ + return ((lua_Unsigned)x.h << (LUA_UNSIGNEDBITS - 32)) | + (lua_Unsigned)x.l >> (64 - LUA_UNSIGNEDBITS); +#endif } -static I Int2I (lua_Integer n) { - lua_Unsigned un = n; - return packI((lu_int32)un, (lu_int32)(un >> 31 >> 1)); +static I Int2I (lua_Unsigned n) { + return packI((lu_int32)(n >> 31 >> 1), (lu_int32)n & ~(lu_int32)0); } #endif /* } */ @@ -420,35 +427,47 @@ typedef struct { } RanState; +/* +** Return the higher bit set in 'x' (first bit is 1). +*/ +static int higherbit (lua_Unsigned x) { + /* table of higher bits from 0 to 255 */ + static const unsigned char hb[256] = { + 0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5, + 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, + 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, + 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, + 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, + 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, + 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, + 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8 + }; + int l = 0; + while (x >= 256) { l += 8; x >>= 8; } + return l + hb[x]; +} + + /* ** Project the random integer 'ran' into the interval [0, n]. -** Because 'ran' has 2^B possible values, the projection can only be -** uniform when the size of the interval [0, n] is a power of 2 (exact -** division). To get a uniform projection into [0,lim], we first -** compute 'lim', the smallest (2^b - 1) not smaller than 'n'. If the -** random number is inside [0, n], we are done. Otherwise, we try with -** another 'ran' until we have a result inside the interval. +** To get a uniform projection into [0,n], we first compute 'shf', the +** largest number that we can right-shift 'ran' and still get numbers +** as larger as 'n'. We then shift 'ran'; if the result is inside [0, n], +** we are done. Otherwise, we try with another 'ran' until we have a +** result inside the interval. (We use right shifts to avoid the lowest +** bits of 'ran', which has poorer distributions.) */ static lua_Unsigned project (lua_Unsigned ran, lua_Unsigned n, RanState *state) { - lua_Unsigned lim = n; - if ((lim & (lim + 1)) > 0) { /* 'lim + 1' is not a power of 2? */ - /* compute the smallest (2^b - 1) not smaller than 'n' */ - lim |= (lim >> 1); - lim |= (lim >> 2); - lim |= (lim >> 4); - lim |= (lim >> 8); - lim |= (lim >> 16); -#if (LUA_MAXINTEGER >> 30 >> 2) > 0 - lim |= (lim >> 32); /* integer type has more than 32 bits */ -#endif + if (n == 0) return 0; /* special case for the unit set */ + else { + int shf = LUA_UNSIGNEDBITS - higherbit(n); + lua_assert(~(lua_Unsigned)0 >> shf >= n && /* not larger */ + ~(lua_Unsigned)0 >> shf >> 1 < n); /* largest */ + while ((ran >>= shf) > n) + ran = I2UInt(xorshift128plus(state->s)); + return ran; } - lua_assert((lim & (lim + 1)) == 0 /* 'lim + 1' is a power of 2 */ - && lim >= n /* not smaller than 'n' */ - && (lim == 0 || (lim >> 1) < n)); /* it is the smallest one */ - while ((ran & lim) > n) - ran = I2UInt(xorshift128plus(state->s)); - return ran & lim; } @@ -487,12 +506,12 @@ static int math_random (lua_State *L) { } -static void setseed (I *state, lua_Integer n) { +static void setseed (I *state, lua_Unsigned n) { int i; state[0] = Int2I(n); - state[1] = Int2I(~n); + state[1] = Int2I(0xff); /* avoid a zero state */ for (i = 0; i < 16; i++) - xorshift128plus(state); /* discard initial values */ + xorshift128plus(state); /* discard initial values to "spread" seed */ } From 8d50a998e381a444829effd5437d52e8ae6c7ee5 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 2 Apr 2018 10:58:33 -0300 Subject: [PATCH 0265/1145] definition for LUA_UNSIGNEDBITS (number of bits in a LUA_UNSIGNED) --- luaconf.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/luaconf.h b/luaconf.h index 144f51baf8..1f9d5f3b67 100644 --- a/luaconf.h +++ b/luaconf.h @@ -1,5 +1,5 @@ /* -** $Id: luaconf.h,v 1.266 2018/03/02 18:31:51 roberto Exp roberto $ +** $Id: luaconf.h,v 1.267 2018/03/09 14:56:02 roberto Exp roberto $ ** Configuration file for Lua ** See Copyright Notice in lua.h */ @@ -447,6 +447,7 @@ @@ LUA_MAXINTEGER is the maximum value for a LUA_INTEGER. @@ LUA_MININTEGER is the minimum value for a LUA_INTEGER. @@ LUA_MAXUNSIGNED is the maximum value for a LUA_UNSIGNED. +@@ LUA_UNSIGNEDBITS is the number of bits in a LUA_UNSIGNED. @@ lua_integer2str converts an integer to a string. */ @@ -468,6 +469,8 @@ #define LUA_MAXUNSIGNED (~(lua_Unsigned)0) +#define LUA_UNSIGNEDBITS (sizeof(LUA_UNSIGNED) * CHAR_BIT) + /* now the variable definitions */ From 3d0b5edfe4df7ec54d6885b6b6ce917faddf6661 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 2 Apr 2018 14:52:07 -0300 Subject: [PATCH 0266/1145] using unsigned comparison in 'l_intfitsf' (avoids one comparison) --- lvm.c | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/lvm.c b/lvm.c index fd74bff542..5e3c2ca0fa 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.350 2018/03/07 15:55:38 roberto Exp roberto $ +** $Id: lvm.c,v 2.351 2018/03/16 14:21:20 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -50,24 +50,28 @@ /* -** 'l_intfitsf' checks whether a given integer can be converted to a -** float without rounding. Used in comparisons. +** 'l_intfitsf' checks whether a given integer is in the range that +** can be converted to a float without rounding. Used in comparisons. */ + /* number of bits in the mantissa of a float */ #define NBM (l_mathlim(MANT_DIG)) /* -** Check whether some integers may not fit in a float, that is, whether -** (maxinteger >> NBM) > 0 (that implies (1 << NBM) <= maxinteger). -** (The shifts are done in parts to avoid shifting by more than the size +** Check whether some integers may not fit in a float, testing whether +** (maxinteger >> NBM) > 0. (That implies (1 << NBM) <= maxinteger.) +** (The shifts are done in parts, to avoid shifting by more than the size ** of an integer. In a worst case, NBM == 113 for long double and -** sizeof(integer) == 32.) +** sizeof(long) == 32.) */ #if ((((LUA_MAXINTEGER >> (NBM / 4)) >> (NBM / 4)) >> (NBM / 4)) \ >> (NBM - (3 * (NBM / 4)))) > 0 -#define l_intfitsf(i) \ - (-((lua_Integer)1 << NBM) <= (i) && (i) <= ((lua_Integer)1 << NBM)) +/* limit for integers that fit in a float */ +#define MAXINTFITSF ((lua_Unsigned)1 << NBM) + +/* check whether 'i' is in the interval [-MAXINTFITSF, MAXINTFITSF] */ +#define l_intfitsf(i) ((MAXINTFITSF + l_castS2U(i)) <= (2 * MAXINTFITSF)) #else /* all integers fit in a float precisely */ From 03c6a05ec836c3a90a6b8d730120afdad39c092b Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 4 Apr 2018 11:23:41 -0300 Subject: [PATCH 0267/1145] no more nil-in-table --- lapi.c | 23 +---------------------- lcode.c | 55 +++--------------------------------------------------- lcode.h | 3 +-- ljumptab.h | 2 -- llex.c | 4 ++-- llex.h | 4 ++-- lobject.h | 10 ++-------- lopcodes.c | 6 +----- lopcodes.h | 5 +---- lparser.c | 16 +--------------- lparser.h | 5 ++--- ltablib.c | 6 +++--- ltm.c | 30 +---------------------------- ltm.h | 6 +----- lua.h | 5 +---- lvm.c | 15 +-------------- 16 files changed, 23 insertions(+), 172 deletions(-) diff --git a/lapi.c b/lapi.c index eb872df8bd..877fb2ac51 100644 --- a/lapi.c +++ b/lapi.c @@ -1,5 +1,5 @@ /* -** $Id: lapi.c,v 2.289 2018/02/27 17:48:28 roberto Exp roberto $ +** $Id: lapi.c,v 2.290 2018/02/27 20:01:55 roberto Exp roberto $ ** Lua API ** See Copyright Notice in lua.h */ @@ -705,27 +705,6 @@ LUA_API int lua_rawgetp (lua_State *L, int idx, const void *p) { } -static int auxkeydef (lua_State *L, int idx, int remove) { - int res; - lua_lock(L); - api_checknelems(L, 1); - res = luaT_keydef(L, index2value(L, idx), s2v(L->top - 1), remove); - L->top--; /* remove key */ - lua_unlock(L); - return res; -} - - -LUA_API void lua_removekey (lua_State *L, int idx) { - auxkeydef(L, idx, 1); -} - - -LUA_API int lua_keyin (lua_State *L, int idx) { - return auxkeydef(L, idx, 0); -} - - LUA_API void lua_createtable (lua_State *L, int narray, int nrec) { Table *t; lua_lock(L); diff --git a/lcode.c b/lcode.c index 661c791615..ab91561cae 100644 --- a/lcode.c +++ b/lcode.c @@ -1,5 +1,5 @@ /* -** $Id: lcode.c,v 2.159 2018/03/07 15:55:38 roberto Exp roberto $ +** $Id: lcode.c,v 2.160 2018/03/16 14:22:09 roberto Exp roberto $ ** Code generator for Lua ** See Copyright Notice in lua.h */ @@ -678,10 +678,6 @@ void luaK_dischargevars (FuncState *fs, expdesc *e) { e->k = VNONRELOC; /* becomes a non-relocatable value */ break; } - case VUNDEF: { /* not a real expression */ - luaK_semerror(fs->ls, "'undef' is not a value!!"); - break; - } case VUPVAL: { /* move value to some (pending) register */ e->u.info = luaK_codeABC(fs, OP_GETUPVAL, 0, e->u.info, 0); e->k = VRELOC; @@ -1410,48 +1406,6 @@ static void codeeq (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) { } -static void normalizeindexed (FuncState *fs, expdesc *v) { - if (v->k != VINDEXED) { /* not in proper form? */ - int key = fs->freereg; /* register with key value */ - luaK_reserveregs(fs, 1); - switch (v->k) { - case VINDEXI: - luaK_int(fs, key, v->u.ind.idx); - break; - case VINDEXSTR: - luaK_codek(fs, key, v->u.ind.idx); - break; - case VINDEXUP: - luaK_codek(fs, key, v->u.ind.idx); - luaK_codeABC(fs, OP_GETUPVAL, fs->freereg, v->u.ind.t, 0); - v->u.ind.t = fs->freereg; - luaK_reserveregs(fs, 1); /* one more register for the upvalue */ - break; - default: - luaK_semerror(fs->ls, "'undef' is not a value!!"); - break; - } - v->u.ind.idx = key; - v->k = VINDEXED; - } - freeregs(fs, v->u.ind.t, v->u.ind.idx); -} - - -static void codeisdef (FuncState *fs, int eq, expdesc *v) { - normalizeindexed(fs, v); - v->u.info = luaK_codeABCk(fs, OP_ISDEF, 0, v->u.ind.t, v->u.ind.idx, eq); - v->k = VRELOC; -} - - -void luaK_codeundef (FuncState *fs, expdesc *v) { - normalizeindexed(fs, v); - v->u.info = luaK_codeABC(fs, OP_UNDEF, v->u.ind.t, v->u.ind.idx, 0); - v->k = VRELOC; -} - - /* ** Apply prefix operation 'op' to expression 'e'. */ @@ -1500,7 +1454,7 @@ void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) { break; } case OPR_EQ: case OPR_NE: { - if (!tonumeral(v, NULL) && fs->ls->t.token != TK_UNDEF) + if (!tonumeral(v, NULL)) luaK_exp2RK(fs, v); /* else keep numeral, which may be an immediate operand */ break; @@ -1597,10 +1551,7 @@ void luaK_posfix (FuncState *fs, BinOpr opr, break; } case OPR_EQ: case OPR_NE: { - if (e2->k == VUNDEF) - codeisdef(fs, opr == OPR_NE, e1); - else - codeeq(fs, opr, e1, e2); + codeeq(fs, opr, e1, e2); break; } case OPR_LT: case OPR_LE: { diff --git a/lcode.h b/lcode.h index 6ec8b0993a..0c4159735c 100644 --- a/lcode.h +++ b/lcode.h @@ -1,5 +1,5 @@ /* -** $Id: lcode.h,v 1.71 2018/03/07 15:55:38 roberto Exp roberto $ +** $Id: lcode.h,v 1.72 2018/03/19 20:03:44 roberto Exp roberto $ ** Code generator for Lua ** See Copyright Notice in lua.h */ @@ -90,7 +90,6 @@ LUAI_FUNC void luaK_posfix (FuncState *fs, BinOpr op, expdesc *v1, expdesc *v2, int line); LUAI_FUNC void luaK_setlist (FuncState *fs, int base, int nelems, int tostore); LUAI_FUNC void luaK_finish (FuncState *fs); -LUAI_FUNC void luaK_codeundef (FuncState *fs, expdesc *e); LUAI_FUNC l_noret luaK_semerror (LexState *ls, const char *msg); diff --git a/ljumptab.h b/ljumptab.h index 373d541c1b..c642c23e1f 100644 --- a/ljumptab.h +++ b/ljumptab.h @@ -79,8 +79,6 @@ static void *disptab[] = { &&L_OP_GEI, &&L_OP_TEST, &&L_OP_TESTSET, -&&L_OP_UNDEF, -&&L_OP_ISDEF, &&L_OP_CALL, &&L_OP_TAILCALL, &&L_OP_RETURN, diff --git a/llex.c b/llex.c index 7e59eb4939..9419a8cdf0 100644 --- a/llex.c +++ b/llex.c @@ -1,5 +1,5 @@ /* -** $Id: llex.c,v 2.100 2018/02/23 13:13:31 roberto Exp roberto $ +** $Id: llex.c,v 2.101 2018/03/07 15:55:38 roberto Exp roberto $ ** Lexical Analyzer ** See Copyright Notice in lua.h */ @@ -41,7 +41,7 @@ static const char *const luaX_tokens [] = { "and", "break", "do", "else", "elseif", "end", "false", "for", "function", "goto", "if", "in", "local", "nil", "not", "or", "repeat", - "return", "then", "true", "undef", "until", "while", + "return", "then", "true", "until", "while", "//", "..", "...", "==", ">=", "<=", "~=", "<<", ">>", "::", "", "", "", "", "" diff --git a/llex.h b/llex.h index fb33c33f4d..d1d5a4a8f5 100644 --- a/llex.h +++ b/llex.h @@ -1,5 +1,5 @@ /* -** $Id: llex.h,v 1.80 2018/01/28 15:13:26 roberto Exp roberto $ +** $Id: llex.h,v 1.81 2018/03/07 15:55:38 roberto Exp roberto $ ** Lexical Analyzer ** See Copyright Notice in lua.h */ @@ -28,7 +28,7 @@ enum RESERVED { TK_AND = FIRST_RESERVED, TK_BREAK, TK_DO, TK_ELSE, TK_ELSEIF, TK_END, TK_FALSE, TK_FOR, TK_FUNCTION, TK_GOTO, TK_IF, TK_IN, TK_LOCAL, TK_NIL, TK_NOT, TK_OR, TK_REPEAT, - TK_RETURN, TK_THEN, TK_TRUE, TK_UNDEF, TK_UNTIL, TK_WHILE, + TK_RETURN, TK_THEN, TK_TRUE, TK_UNTIL, TK_WHILE, /* other terminal symbols */ TK_IDIV, TK_CONCAT, TK_DOTS, TK_EQ, TK_GE, TK_LE, TK_NE, TK_SHL, TK_SHR, diff --git a/lobject.h b/lobject.h index b18dce4710..dc5f32a1df 100644 --- a/lobject.h +++ b/lobject.h @@ -1,5 +1,5 @@ /* -** $Id: lobject.h,v 2.140 2018/02/26 13:35:03 roberto Exp roberto $ +** $Id: lobject.h,v 2.141 2018/02/26 14:16:05 roberto Exp roberto $ ** Type definitions for Lua objects ** See Copyright Notice in lua.h */ @@ -160,15 +160,9 @@ typedef StackValue *StkId; /* index to stack elements */ #define isreallyempty(v) checktag((v), LUA_TEMPTY) -#if defined(LUA_NILINTABLE) - -#define isempty(v) isreallyempty(v) - -#else /* By default, entries with any kind of nil are considered empty */ - +/* By default, entries with any kind of nil are considered empty */ #define isempty(v) ttisnilorempty(v) -#endif /* macro defining an empty value */ #define EMPTYCONSTANT {NULL}, LUA_TEMPTY diff --git a/lopcodes.c b/lopcodes.c index ac59537b86..3db9db25a5 100644 --- a/lopcodes.c +++ b/lopcodes.c @@ -1,5 +1,5 @@ /* -** $Id: lopcodes.c,v 1.79 2018/02/21 15:49:32 roberto Exp roberto $ +** $Id: lopcodes.c,v 1.80 2018/03/07 15:55:38 roberto Exp roberto $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ @@ -79,8 +79,6 @@ LUAI_DDEF const char *const luaP_opnames[NUM_OPCODES+1] = { "GEI", "TEST", "TESTSET", - "UNDEF", - "ISDEF", "CALL", "TAILCALL", "RETURN", @@ -164,8 +162,6 @@ LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = { ,opmode(0, 0, 1, 0, iABC) /* OP_GEI */ ,opmode(0, 0, 1, 0, iABC) /* OP_TEST */ ,opmode(0, 0, 1, 1, iABC) /* OP_TESTSET */ - ,opmode(0, 0, 0, 0, iABC) /* OP_UNDEF */ - ,opmode(0, 0, 0, 1, iABC) /* OP_ISDEF */ ,opmode(1, 1, 0, 1, iABC) /* OP_CALL */ ,opmode(1, 1, 0, 1, iABC) /* OP_TAILCALL */ ,opmode(0, 1, 0, 0, iABC) /* OP_RETURN */ diff --git a/lopcodes.h b/lopcodes.h index ddbaf8dacd..b9e9ec969e 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -1,5 +1,5 @@ /* -** $Id: lopcodes.h,v 1.189 2018/02/21 15:49:32 roberto Exp roberto $ +** $Id: lopcodes.h,v 1.190 2018/03/07 15:55:38 roberto Exp roberto $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ @@ -266,9 +266,6 @@ OP_GEI,/* A sB if ((R(A) >= sB) ~= k) then pc++ */ OP_TEST,/* A if (not R(A) == k) then pc++ */ OP_TESTSET,/* A B if (not R(B) == k) then R(A) := R(B) else pc++ */ -OP_UNDEF,/* A B R(A)[R(B)] = undef */ -OP_ISDEF,/* A B C R(A) = (R(B)[R(C)] == undef */ - OP_CALL,/* A B C R(A), ... ,R(A+C-2) := R(A)(R(A+1), ... ,R(A+B-1)) */ OP_TAILCALL,/* A B C return R(A)(R(A+1), ... ,R(A+B-1)) */ diff --git a/lparser.c b/lparser.c index 9bf5485e48..e67d70ea6b 100644 --- a/lparser.c +++ b/lparser.c @@ -1,5 +1,5 @@ /* -** $Id: lparser.c,v 2.178 2018/02/17 19:20:00 roberto Exp roberto $ +** $Id: lparser.c,v 2.179 2018/03/07 15:55:38 roberto Exp roberto $ ** Lua Parser ** See Copyright Notice in lua.h */ @@ -893,11 +893,6 @@ static void primaryexp (LexState *ls, expdesc *v) { singlevar(ls, v); return; } - case TK_UNDEF: { - luaX_next(ls); - init_exp(v, VUNDEF, 0); - return; - } default: { luaX_syntaxerror(ls, "unexpected symbol"); } @@ -1183,10 +1178,6 @@ static void assignment (LexState *ls, struct LHS_assign *lh, int nvars) { else { /* assignment -> '=' explist */ int nexps; checknext(ls, '='); - if (nvars == 1 && testnext(ls, TK_UNDEF)) { - luaK_codeundef(ls->fs, &lh->v); - return; - } nexps = explist(ls, &e); if (nexps != nvars) adjust_assign(ls, nvars, nexps, &e); @@ -1652,11 +1643,6 @@ static void statement (LexState *ls) { luaX_next(ls); /* skip LOCAL */ if (testnext(ls, TK_FUNCTION)) /* local function? */ localfunc(ls); - else if (testnext(ls, TK_UNDEF)) - (void)0; /* ignore */ - /* old versions may need to declare 'local undef' - when using 'undef' with no environment; so this - version accepts (and ignores) these declarations */ else localstat(ls); break; diff --git a/lparser.h b/lparser.h index b8705e847c..da6a7483ee 100644 --- a/lparser.h +++ b/lparser.h @@ -1,5 +1,5 @@ /* -** $Id: lparser.h,v 1.80 2017/12/14 14:24:02 roberto Exp roberto $ +** $Id: lparser.h,v 1.81 2018/03/07 15:55:38 roberto Exp roberto $ ** Lua Parser ** See Copyright Notice in lua.h */ @@ -52,8 +52,7 @@ typedef enum { VRELOC, /* expression can put result in any register; info = instruction pc */ VCALL, /* expression is a function call; info = instruction pc */ - VVARARG, /* vararg expression; info = instruction pc */ - VUNDEF /* the 'undef' "expression" */ + VVARARG /* vararg expression; info = instruction pc */ } expkind; diff --git a/ltablib.c b/ltablib.c index 5a78c15bc2..7bbc3a0715 100644 --- a/ltablib.c +++ b/ltablib.c @@ -1,5 +1,5 @@ /* -** $Id: ltablib.c,v 1.95 2018/02/27 18:47:32 roberto Exp roberto $ +** $Id: ltablib.c,v 1.96 2018/03/16 14:18:18 roberto Exp roberto $ ** Library for Table Manipulation ** See Copyright Notice in lua.h */ @@ -95,8 +95,8 @@ static int tremove (lua_State *L) { lua_geti(L, 1, pos + 1); lua_seti(L, 1, pos); /* t[pos] = t[pos + 1] */ } - lua_pushinteger(L, pos); - lua_removekey(L, 1); /* remove entry t[pos] */ + lua_pushnil(L); + lua_seti(L, 1, pos); /* remove entry t[pos] */ return 1; } diff --git a/ltm.c b/ltm.c index 2a9a4cbe4e..6d285510aa 100644 --- a/ltm.c +++ b/ltm.c @@ -1,5 +1,5 @@ /* -** $Id: ltm.c,v 2.65 2018/02/26 14:16:05 roberto Exp roberto $ +** $Id: ltm.c,v 2.66 2018/02/27 17:48:28 roberto Exp roberto $ ** Tag methods ** See Copyright Notice in lua.h */ @@ -38,7 +38,6 @@ LUAI_DDEF const char *const luaT_typenames_[LUA_TOTALTAGS] = { void luaT_init (lua_State *L) { static const char *const luaT_eventname[] = { /* ORDER TM */ "__index", "__newindex", - "__undef", "__isdef", "__gc", "__mode", "__len", "__eq", "__add", "__sub", "__mul", "__mod", "__pow", "__div", "__idiv", @@ -250,30 +249,3 @@ void luaT_getvarargs (lua_State *L, CallInfo *ci, StkId where, int wanted) { setnilvalue(s2v(where + i)); } - -int luaT_keydef (lua_State *L, TValue *obj, TValue *key, int remove) { - const TValue *tm; - TMS event = remove ? TM_UNDEF : TM_ISDEF; - if (!ttistable(obj)) { /* not a table? */ - tm = luaT_gettmbyobj(L, obj, event); /* get its metamethod */ - if (notm(tm)) { /* no metamethod? */ - const char *msg = remove ? "remove key from" : "check key from"; - luaG_typeerror(L, obj, msg); /* error */ - } - /* else will call metamethod 'tm' */ - } - else { /* 'obj' is a table */ - Table *t = hvalue(obj); - tm = fasttm(L, t->metatable, event); - if (tm == NULL) { /* no metamethod? */ - const TValue *val = luaH_get(t, key); /* get entry */ - int res = !isempty(val); /* true if entry is not empty */ - if (remove && res) /* key is present and should be removed? */ - setempty(cast(TValue*, val)); /* remove it */ - return res; - } - /* else will call metamethod 'tm' */ - } - luaT_callTMres(L, tm, obj, key, L->top); - return !l_isfalse(s2v(L->top)); -} diff --git a/ltm.h b/ltm.h index 6e961d27b0..7eb60b4cf6 100644 --- a/ltm.h +++ b/ltm.h @@ -1,5 +1,5 @@ /* -** $Id: ltm.h,v 2.33 2018/02/23 13:13:31 roberto Exp roberto $ +** $Id: ltm.h,v 2.34 2018/02/27 17:48:28 roberto Exp roberto $ ** Tag methods ** See Copyright Notice in lua.h */ @@ -19,8 +19,6 @@ typedef enum { TM_INDEX, TM_NEWINDEX, - TM_UNDEF, - TM_ISDEF, TM_GC, TM_MODE, TM_LEN, @@ -91,7 +89,5 @@ LUAI_FUNC void luaT_adjustvarargs (lua_State *L, int nfixparams, LUAI_FUNC void luaT_getvarargs (lua_State *L, struct CallInfo *ci, StkId where, int wanted); -LUAI_FUNC int luaT_keydef (lua_State *L, TValue *obj, TValue *key, int remove); - #endif diff --git a/lua.h b/lua.h index 31e203971b..3f76fbea4d 100644 --- a/lua.h +++ b/lua.h @@ -1,5 +1,5 @@ /* -** $Id: lua.h,v 1.344 2018/03/05 14:15:32 roberto Exp roberto $ +** $Id: lua.h,v 1.345 2018/03/16 15:33:34 roberto Exp roberto $ ** Lua - A Scripting Language ** Lua.org, PUC-Rio, Brazil (http://www.lua.org) ** See Copyright Notice at the end of this file @@ -331,9 +331,6 @@ LUA_API size_t (lua_stringtonumber) (lua_State *L, const char *s); LUA_API lua_Alloc (lua_getallocf) (lua_State *L, void **ud); LUA_API void (lua_setallocf) (lua_State *L, lua_Alloc f, void *ud); -LUA_API void (lua_removekey) (lua_State *L, int idx); -LUA_API int (lua_keyin) (lua_State *L, int idx); - /* ** {============================================================== diff --git a/lvm.c b/lvm.c index 5e3c2ca0fa..9e044aaedd 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.351 2018/03/16 14:21:20 roberto Exp roberto $ +** $Id: lvm.c,v 2.352 2018/04/02 17:52:07 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -1560,19 +1560,6 @@ void luaV_execute (lua_State *L, CallInfo *ci) { } vmbreak; } - vmcase(OP_UNDEF) { - TValue *rb = vRB(i); - luaT_keydef(L, vra, rb, 1); - vmbreak; - } - vmcase(OP_ISDEF) { - TValue *rb = vRB(i); - TValue *rc = vRC(i); - int res; - Protect(res = luaT_keydef(L, rb, rc, 0)); - setbvalue(vra, res == GETARG_k(i)); - vmbreak; - } vmcase(OP_CALL) { int b = GETARG_B(i); int nresults = GETARG_C(i) - 1; From b44787652bd3a51dfc2ee92120abe05b28159d32 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 4 Apr 2018 13:12:53 -0300 Subject: [PATCH 0268/1145] using 'xoroshiro128+' for PRNG (plus a rotate at the final result to have better lower bits) --- lmathlib.c | 175 +++++++++++++++++++++++++++-------------------------- 1 file changed, 88 insertions(+), 87 deletions(-) diff --git a/lmathlib.c b/lmathlib.c index 9432d40537..3e5ceea4d1 100644 --- a/lmathlib.c +++ b/lmathlib.c @@ -1,5 +1,5 @@ /* -** $Id: lmathlib.c,v 1.127 2018/03/22 19:54:49 roberto Exp roberto $ +** $Id: lmathlib.c,v 1.128 2018/03/26 19:48:46 roberto Exp $ ** Standard mathematical library ** See Copyright Notice in lua.h */ @@ -247,7 +247,7 @@ static int math_type (lua_State *L) { /* ** {================================================================== -** Pseudo-Random Number Generator based on 'xorshift128+'. +** Pseudo-Random Number Generator based on 'xoroshiro128+'. ** =================================================================== */ @@ -270,34 +270,45 @@ static int math_type (lua_State *L) { /* a 64-bit value */ typedef unsigned long long I; -static I xorshift128plus (I *state) { - I x = state[0]; - I y = state[1]; - state[0] = y; - x ^= x << 23; - state[1] = (x ^ (x >> 18)) ^ (y ^ (y >> 5)); - return state[1] + y; + +/* rotate left 'x' by 'n' bits */ +static I rotl (I x, int n) { + return (x << n) | (x >> (64 - n)); +} + +static I nextrand (I *state) { + I s0 = state[0]; + I s1 = state[1]; + I res = s0 + s1; + res = rotl(res, 41); /* extra step to change place of lower bits */ + s1 = s1 ^ s0; + state[0] = rotl(s0, 55) ^ (s1 ^ (s1 << 14)); + state[1] = rotl(s1, 36); + return res; } + /* must take care to not shift stuff by more than 63 slots */ -#define shiftI (64 - FIGS) /* leave FIGS bits */ -#define shiftF (l_mathop(0.5) / (1LLU << (FIGS - 1))) /* 2^(-FIG) */ /* ** Convert bits from a random integer into a float in the ** interval [0,1). */ +#define maskFIG (~(~1LLU << (FIGS - 1))) /* use FIGS bits */ +#define shiftFIG (l_mathop(0.5) / (1LLU << (FIGS - 1))) /* 2^(-FIGS) */ + static lua_Number I2d (I x) { - return (lua_Number)(x >> shiftI) * shiftF; + return (lua_Number)(x & maskFIG) * shiftFIG; } -/* convert an 'I' to a lua_Unsigned (using higher bits) */ -#define I2UInt(x) ((lua_Unsigned)((x) >> (64 - LUA_UNSIGNEDBITS))) +/* convert an 'I' to a lua_Unsigned */ +#define I2UInt(x) ((lua_Unsigned)(x)) -/* convert a lua_Integer to an 'I' */ +/* convert a lua_Unsigned to an 'I' */ #define Int2I(x) ((I)(x)) + #else /* no long long }{ */ /* @@ -330,14 +341,10 @@ static I packI (lu_int32 h, lu_int32 l) { /* i ^ (i << n) */ static I Ixorshl (I i, int n) { + lua_assert(n > 0 && n < 32); return packI(i.h ^ ((i.h << n) | (i.l >> (32 - n))), i.l ^ (i.l << n)); } -/* i ^ (i >> n) */ -static I Ixorshr (I i, int n) { - return packI(i.h ^ (i.h >> n), i.l ^ ((i.l >> n) | (i.h << (32 - n)))); -} - static I Ixor (I i1, I i2) { return packI(i1.h ^ i2.h, i1.l ^ i2.l); } @@ -349,18 +356,29 @@ static I Iadd (I i1, I i2) { return result; } +/* +** Rotate left. As all offsets here are larger than 32, do a rotate right +** of 64 - offset. +*/ +static I Irotli (I i, int n) { + n = 64 - n; + lua_assert(n > 0 && n < 32); + return packI((i.h >> n) | (i.l << (32 - n)), + (i.h << (32 - n)) | (i.l >> n)); +} /* -** implementation of 'xorshift128+' algorithm on 'I' values +** implementation of 'xoroshiro128+' algorithm on 'I' values */ -static I xorshift128plus (I *state) { - I x = state[0]; - I y = state[1]; - state[0] = y; - x = Ixorshl(x, 23); /* x ^= x << 23; */ - /* state[1] = (x ^ (x >> 18)) ^ (y ^ (y >> 5)); */ - state[1] = Ixor(Ixorshr(x, 18), Ixorshr(y, 5)); - return Iadd(state[1], y); /* return state[1] + y; */ +static I nextrand (I *state) { + I s0 = state[0]; + I s1 = state[1]; + I res = Iadd(s0, s1); + res = Irotli(res, 41); + s1 = Ixor(s1, s0); + state[0] = Ixor(Irotli(s0, 55), Ixorshl(s1, 14)); + state[1] = Irotli(s1, 36); + return res; } @@ -373,45 +391,39 @@ static I xorshift128plus (I *state) { #if FIGS <= 32 -#define maskLOW 0 /* do not need bits from lower half */ -#define maskHI (~(~(lu_int32)0 >> (FIGS - 1) >> 1)) /* use FIGS bits */ -#define shiftHI 1 /* no shift */ -#define shiftF (1 / l_mathop(4294967296.0)) /* 2^(-32) */ +#define maskHI 0 /* do not need bits from higher half */ +#define maskLOW (~(~UONE << (FIGS - 1))) /* use FIGS bits */ +#define shiftFIG (l_mathop(0.5) / (UONE << (FIGS - 1))) /* 2^(-FIGS) */ #else /* 32 < FIGS <= 64 */ /* must take care to not shift stuff by more than 31 slots */ -/* use FIGS - 32 bits from lower half */ -#define maskLOW (~(~(lu_int32)0 >> (FIGS - 33) >> 1)) - -/* use all bits from higher half */ -#define maskHI (~(lu_int32)0) +/* use FIGS - 32 bits from higher half */ +#define maskHI (~(~UONE << (FIGS - 33))) -#define shiftHI l_mathop(4294967296.0) /* 2^32 */ +/* use all bits from lower half */ +#define maskLOW (~(lu_int32)0) -/* 2^(-64) */ -#define shiftF ((lua_Number)(1 / (shiftHI * shiftHI))) +/* 2^(-FIGS) == (1 / 2^33) / 2^(FIGS-33) */ +#define shiftFIG ((lua_Number)(1.0 / 8589934592.0) / (UONE << (FIGS - 33))) #endif +#define twoto32 l_mathop(4294967296.0) /* 2^32 */ + static lua_Number I2d (I x) { lua_Number h = (lua_Number)(x.h & maskHI); lua_Number l = (lua_Number)(x.l & maskLOW); - return (h * shiftHI + l) * shiftF; + return (h * twoto32 + l) * shiftFIG; } + static lua_Unsigned I2UInt (I x) { -#if (LUA_MAXINTEGER >> 30) <= 1 -/* at most 32 bits; use only high bits */ - return ((lua_Unsigned)x.h); -#else -/* at least 33 bits */ - return ((lua_Unsigned)x.h << (LUA_UNSIGNEDBITS - 32)) | - (lua_Unsigned)x.l >> (64 - LUA_UNSIGNEDBITS); -#endif + return ((lua_Unsigned)x.h << 31 << 1) | (lua_Unsigned)x.l; } + static I Int2I (lua_Unsigned n) { return packI((lu_int32)(n >> 31 >> 1), (lu_int32)n & ~(lu_int32)0); } @@ -427,47 +439,36 @@ typedef struct { } RanState; -/* -** Return the higher bit set in 'x' (first bit is 1). -*/ -static int higherbit (lua_Unsigned x) { - /* table of higher bits from 0 to 255 */ - static const unsigned char hb[256] = { - 0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5, - 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, - 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, - 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, - 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, - 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, - 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, - 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8 - }; - int l = 0; - while (x >= 256) { l += 8; x >>= 8; } - return l + hb[x]; -} - - /* ** Project the random integer 'ran' into the interval [0, n]. -** To get a uniform projection into [0,n], we first compute 'shf', the -** largest number that we can right-shift 'ran' and still get numbers -** as larger as 'n'. We then shift 'ran'; if the result is inside [0, n], -** we are done. Otherwise, we try with another 'ran' until we have a -** result inside the interval. (We use right shifts to avoid the lowest -** bits of 'ran', which has poorer distributions.) +** Because 'ran' has 2^B possible values, the projection can only be +** uniform when the size of the interval is a power of 2 (exact +** division). To get a uniform projection into [0, n], we first compute +** 'lim', the smallest Mersenne number not smaller than 'n'. We then +** project 'ran' into the interval [0, lim]. If the result is inside +** [0, n], we are done. Otherwise, we try with another 'ran' until we +** have a result inside the interval. */ static lua_Unsigned project (lua_Unsigned ran, lua_Unsigned n, RanState *state) { - if (n == 0) return 0; /* special case for the unit set */ - else { - int shf = LUA_UNSIGNEDBITS - higherbit(n); - lua_assert(~(lua_Unsigned)0 >> shf >= n && /* not larger */ - ~(lua_Unsigned)0 >> shf >> 1 < n); /* largest */ - while ((ran >>= shf) > n) - ran = I2UInt(xorshift128plus(state->s)); - return ran; + lua_Unsigned lim = n; + if ((lim & (lim + 1)) > 0) { /* 'lim + 1' is not a power of 2? */ + /* compute the smallest (2^b - 1) not smaller than 'n' */ + lim |= (lim >> 1); + lim |= (lim >> 2); + lim |= (lim >> 4); + lim |= (lim >> 8); + lim |= (lim >> 16); +#if (LUA_MAXINTEGER >> 30 >> 2) > 0 + lim |= (lim >> 32); /* integer type has more than 32 bits */ +#endif } + lua_assert((lim & (lim + 1)) == 0 /* 'lim + 1' is a power of 2 */ + && lim >= n /* not smaller than 'n' */ + && (lim == 0 || (lim >> 1) < n)); /* it is the smallest one */ + while ((ran &= lim) > n) + ran = I2UInt(nextrand(state->s)); + return ran; } @@ -475,7 +476,7 @@ static int math_random (lua_State *L) { lua_Integer low, up; lua_Unsigned p; RanState *state = (RanState *)lua_touserdata(L, lua_upvalueindex(1)); - I rv = xorshift128plus(state->s); /* next pseudo-random value */ + I rv = nextrand(state->s); /* next pseudo-random value */ switch (lua_gettop(L)) { /* check number of arguments */ case 0: { /* no arguments */ lua_pushnumber(L, I2d(rv)); /* float between 0 and 1 */ @@ -511,7 +512,7 @@ static void setseed (I *state, lua_Unsigned n) { state[0] = Int2I(n); state[1] = Int2I(0xff); /* avoid a zero state */ for (i = 0; i < 16; i++) - xorshift128plus(state); /* discard initial values to "spread" seed */ + nextrand(state); /* discard initial values to "spread" seed */ } From b8a04658b2787701996b6d787bf3cc256fd667d1 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 6 Apr 2018 12:41:29 -0300 Subject: [PATCH 0269/1145] PRNG changed from 'xoroshiro128+' to 'xoshiro256**' + "I' renamed 'Rand64' + implementation can use integer types larger than 64 (or 32) bits --- lmathlib.c | 182 ++++++++++++++++++++++++++++++++--------------------- 1 file changed, 109 insertions(+), 73 deletions(-) diff --git a/lmathlib.c b/lmathlib.c index 3e5ceea4d1..aa93f4befd 100644 --- a/lmathlib.c +++ b/lmathlib.c @@ -1,5 +1,5 @@ /* -** $Id: lmathlib.c,v 1.128 2018/03/26 19:48:46 roberto Exp $ +** $Id: lmathlib.c,v 1.129 2018/04/04 16:12:53 roberto Exp roberto $ ** Standard mathematical library ** See Copyright Notice in lua.h */ @@ -230,12 +230,8 @@ static int math_max (lua_State *L) { static int math_type (lua_State *L) { - if (lua_type(L, 1) == LUA_TNUMBER) { - if (lua_isinteger(L, 1)) - lua_pushliteral(L, "integer"); - else - lua_pushliteral(L, "float"); - } + if (lua_type(L, 1) == LUA_TNUMBER) + lua_pushstring(L, (lua_isinteger(L, 1)) ? "integer" : "float"); else { luaL_checkany(L, 1); lua_pushnil(L); @@ -247,7 +243,7 @@ static int math_type (lua_State *L) { /* ** {================================================================== -** Pseudo-Random Number Generator based on 'xoroshiro128+'. +** Pseudo-Random Number Generator based on 'xoshiro256**'. ** =================================================================== */ @@ -261,29 +257,43 @@ static int math_type (lua_State *L) { #endif -#if !defined(LUA_USE_C89) && defined(LLONG_MAX) && !defined(LUA_DEBUG) /* { */ +#if (!defined(LUA_USE_C89) && defined(LLONG_MAX) && !defined(LUA_DEBUG)) \ + || defined(Rand64) /* { */ /* ** Assume long long. */ +#if !defined(Rand64) /* a 64-bit value */ -typedef unsigned long long I; +typedef unsigned long long Rand64; +#endif + + +/* +** If 'Rand64' has more than 64 bits, the extra bits do not interfere +** with the 64 initial bits, except in a right shift. Otherwise, we just +** have to make sure we never use them. +*/ + +/* avoid using extra bits when needed */ +#define trim64(x) ((x) & 0xffffffffffffffff) /* rotate left 'x' by 'n' bits */ -static I rotl (I x, int n) { - return (x << n) | (x >> (64 - n)); -} - -static I nextrand (I *state) { - I s0 = state[0]; - I s1 = state[1]; - I res = s0 + s1; - res = rotl(res, 41); /* extra step to change place of lower bits */ - s1 = s1 ^ s0; - state[0] = rotl(s0, 55) ^ (s1 ^ (s1 << 14)); - state[1] = rotl(s1, 36); +static Rand64 rotl (Rand64 x, int n) { + return (x << n) | (trim64(x) >> (64 - n)); +} + +static Rand64 nextrand (Rand64 *state) { + Rand64 res = rotl(state[0] * 5, 7) * 9; + Rand64 t = state[1] << 17; + state[2] ^= state[0]; + state[3] ^= state[1]; + state[1] ^= state[2]; + state[0] ^= state[3]; + state[2] ^= t; + state[3] = rotl(state[3], 45); return res; } @@ -298,15 +308,15 @@ static I nextrand (I *state) { #define maskFIG (~(~1LLU << (FIGS - 1))) /* use FIGS bits */ #define shiftFIG (l_mathop(0.5) / (1LLU << (FIGS - 1))) /* 2^(-FIGS) */ -static lua_Number I2d (I x) { +static lua_Number I2d (Rand64 x) { return (lua_Number)(x & maskFIG) * shiftFIG; } -/* convert an 'I' to a lua_Unsigned */ -#define I2UInt(x) ((lua_Unsigned)(x)) +/* convert a 'Rand64' to a 'lua_Unsigned' */ +#define I2UInt(x) ((lua_Unsigned)trim64(x)) -/* convert a lua_Unsigned to an 'I' */ -#define Int2I(x) ((I)(x)) +/* convert a 'lua_Unsigned' to an 'Rand64' */ +#define Int2I(x) ((Rand64)(x)) #else /* no long long }{ */ @@ -321,69 +331,92 @@ typedef unsigned int lu_int32; typedef unsigned long lu_int32; #endif + /* a 64-bit value */ -typedef struct I { +typedef struct Rand64 { lu_int32 h; /* higher half */ lu_int32 l; /* lower half */ -} I; +} Rand64; /* -** basic operations on 'I' values +** If 'lu_int32' has more than 32 bits, the extra bits do not interfere +** with the 32 initial bits, except in a right shift and comparisons. +** Otherwise, we just have to make sure we never use them. */ -static I packI (lu_int32 h, lu_int32 l) { - I result; +/* avoid using extra bits when needed */ +#define trim32(x) ((x) & 0xffffffff) + + +/* +** basic operations on 'Rand64' values +*/ + +static Rand64 packI (lu_int32 h, lu_int32 l) { + Rand64 result; result.h = h; result.l = l; return result; } -/* i ^ (i << n) */ -static I Ixorshl (I i, int n) { +static Rand64 Ishl (Rand64 i, int n) { lua_assert(n > 0 && n < 32); - return packI(i.h ^ ((i.h << n) | (i.l >> (32 - n))), i.l ^ (i.l << n)); + return packI((i.h << n) | (trim32(i.l) >> (32 - n)), i.l << n); } -static I Ixor (I i1, I i2) { - return packI(i1.h ^ i2.h, i1.l ^ i2.l); +static void Ixor (Rand64 *i1, Rand64 i2) { + i1->h ^= i2.h; + i1->l ^= i2.l; } -static I Iadd (I i1, I i2) { - I result = packI(i1.h + i2.h, i1.l + i2.l); - if (result.l < i1.l) /* carry? */ +static Rand64 Iadd (Rand64 i1, Rand64 i2) { + Rand64 result = packI(i1.h + i2.h, i1.l + i2.l); + if (trim32(result.l) < trim32(i1.l)) /* carry? */ result.h++; return result; } -/* -** Rotate left. As all offsets here are larger than 32, do a rotate right -** of 64 - offset. -*/ -static I Irotli (I i, int n) { - n = 64 - n; +static Rand64 times5 (Rand64 i) { + return Iadd(Ishl(i, 2), i); /* i * 5 == (i << 2) + i */ +} + +static Rand64 times9 (Rand64 i) { + return Iadd(Ishl(i, 3), i); /* i * 9 == (i << 3) + i */ +} + +static Rand64 rotl (Rand64 i, int n) { lua_assert(n > 0 && n < 32); - return packI((i.h >> n) | (i.l << (32 - n)), - (i.h << (32 - n)) | (i.l >> n)); + return packI((i.h << n) | (trim32(i.l) >> (32 - n)), + (trim32(i.h) >> (32 - n)) | (i.l << n)); +} + +/* for offsets larger than 32, rotate right by 64 - offset */ +static Rand64 rotl1 (Rand64 i, int n) { + lua_assert(n > 32 && n < 64); + n = 64 - n; + return packI((trim32(i.h) >> n) | (i.l << (32 - n)), + (i.h << (32 - n)) | (trim32(i.l) >> n)); } /* -** implementation of 'xoroshiro128+' algorithm on 'I' values +** implementation of 'xoshiro256**' algorithm on 'Rand64' values */ -static I nextrand (I *state) { - I s0 = state[0]; - I s1 = state[1]; - I res = Iadd(s0, s1); - res = Irotli(res, 41); - s1 = Ixor(s1, s0); - state[0] = Ixor(Irotli(s0, 55), Ixorshl(s1, 14)); - state[1] = Irotli(s1, 36); +static Rand64 nextrand (Rand64 *state) { + Rand64 res = times9(rotl(times5(state[0]), 7)); + Rand64 t = Ishl(state[1], 17); + Ixor(&state[2], state[0]); + Ixor(&state[3], state[1]); + Ixor(&state[1], state[2]); + Ixor(&state[0], state[3]); + Ixor(&state[2], t); + state[3] = rotl1(state[3], 45); return res; } /* -** Converts an 'I' into a float. +** Converts an 'Rand64' into a float. */ /* an unsigned 1 with proper type */ @@ -402,8 +435,8 @@ static I nextrand (I *state) { /* use FIGS - 32 bits from higher half */ #define maskHI (~(~UONE << (FIGS - 33))) -/* use all bits from lower half */ -#define maskLOW (~(lu_int32)0) +/* use 32 bits from lower half */ +#define maskLOW (~(~UONE << 31)) /* 2^(-FIGS) == (1 / 2^33) / 2^(FIGS-33) */ #define shiftFIG ((lua_Number)(1.0 / 8589934592.0) / (UONE << (FIGS - 33))) @@ -412,30 +445,30 @@ static I nextrand (I *state) { #define twoto32 l_mathop(4294967296.0) /* 2^32 */ -static lua_Number I2d (I x) { +static lua_Number I2d (Rand64 x) { lua_Number h = (lua_Number)(x.h & maskHI); lua_Number l = (lua_Number)(x.l & maskLOW); return (h * twoto32 + l) * shiftFIG; } -static lua_Unsigned I2UInt (I x) { - return ((lua_Unsigned)x.h << 31 << 1) | (lua_Unsigned)x.l; +static lua_Unsigned I2UInt (Rand64 x) { + return ((lua_Unsigned)x.h << 31 << 1) | (lua_Unsigned)trim32(x.l); } -static I Int2I (lua_Unsigned n) { - return packI((lu_int32)(n >> 31 >> 1), (lu_int32)n & ~(lu_int32)0); +static Rand64 Int2I (lua_Unsigned n) { + return packI((lu_int32)(n >> 31 >> 1), (lu_int32)n); } #endif /* } */ /* -** A state uses two 'I' values. +** A state uses four 'Rand64' values. */ typedef struct { - I s[2]; + Rand64 s[4]; } RanState; @@ -476,7 +509,7 @@ static int math_random (lua_State *L) { lua_Integer low, up; lua_Unsigned p; RanState *state = (RanState *)lua_touserdata(L, lua_upvalueindex(1)); - I rv = nextrand(state->s); /* next pseudo-random value */ + Rand64 rv = nextrand(state->s); /* next pseudo-random value */ switch (lua_gettop(L)) { /* check number of arguments */ case 0: { /* no arguments */ lua_pushnumber(L, I2d(rv)); /* float between 0 and 1 */ @@ -507,10 +540,12 @@ static int math_random (lua_State *L) { } -static void setseed (I *state, lua_Unsigned n) { +static void setseed (Rand64 *state, lua_Unsigned n1, lua_Unsigned n2) { int i; - state[0] = Int2I(n); + state[0] = Int2I(n1); state[1] = Int2I(0xff); /* avoid a zero state */ + state[2] = Int2I(n2); + state[3] = Int2I(0); for (i = 0; i < 16; i++) nextrand(state); /* discard initial values to "spread" seed */ } @@ -518,8 +553,9 @@ static void setseed (I *state, lua_Unsigned n) { static int math_randomseed (lua_State *L) { RanState *state = (RanState *)lua_touserdata(L, lua_upvalueindex(1)); - lua_Integer n = luaL_checkinteger(L, 1); - setseed(state->s, n); + lua_Integer n1 = luaL_checkinteger(L, 1); + lua_Integer n2 = luaL_optinteger(L, 2, 0); + setseed(state->s, n1, n2); return 0; } @@ -532,7 +568,7 @@ static const luaL_Reg randfuncs[] = { static void setrandfunc (lua_State *L) { RanState *state = (RanState *)lua_newuserdatauv(L, sizeof(RanState), 0); - setseed(state->s, 0); + setseed(state->s, 0, 0); luaL_setfuncs(L, randfuncs, 1); } From 762baf05487a6574e6b9e55783e1a439b6fc9af3 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 6 Apr 2018 14:52:42 -0300 Subject: [PATCH 0270/1145] detail (trim constants are unsigned) --- lmathlib.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lmathlib.c b/lmathlib.c index aa93f4befd..3f17890ba6 100644 --- a/lmathlib.c +++ b/lmathlib.c @@ -1,5 +1,5 @@ /* -** $Id: lmathlib.c,v 1.129 2018/04/04 16:12:53 roberto Exp roberto $ +** $Id: lmathlib.c,v 1.130 2018/04/06 15:41:29 roberto Exp roberto $ ** Standard mathematical library ** See Copyright Notice in lua.h */ @@ -277,7 +277,7 @@ typedef unsigned long long Rand64; */ /* avoid using extra bits when needed */ -#define trim64(x) ((x) & 0xffffffffffffffff) +#define trim64(x) ((x) & 0xffffffffffffffffU) /* rotate left 'x' by 'n' bits */ @@ -346,7 +346,7 @@ typedef struct Rand64 { */ /* avoid using extra bits when needed */ -#define trim32(x) ((x) & 0xffffffff) +#define trim32(x) ((x) & 0xffffffffU) /* From f9c3d6fdbea21e83cfc50bcaa617ff59a3d36434 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 11 Apr 2018 13:49:36 -0300 Subject: [PATCH 0271/1145] use test mode to test the interpreter without jump tables --- ltests.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/ltests.h b/ltests.h index 7019f7161e..e718e7644d 100644 --- a/ltests.h +++ b/ltests.h @@ -1,5 +1,5 @@ /* -** $Id: ltests.h,v 2.55 2017/12/18 13:01:49 roberto Exp roberto $ +** $Id: ltests.h,v 2.56 2018/02/27 18:47:32 roberto Exp roberto $ ** Internal Header for Debugging of the Lua Implementation ** See Copyright Notice in lua.h */ @@ -41,6 +41,10 @@ #endif +/* get a chance to test code without jump tables */ +#define LUA_USE_JUMPTABLE 0 + + /* memory-allocator control variables */ typedef struct Memcontrol { unsigned long numblocks; From 26eb144541714343f8aeb124150d60cf7d91fac7 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 19 Apr 2018 12:42:41 -0300 Subject: [PATCH 0272/1145] no need to define 'luaP_opnames' in regular builds --- lopcodes.c | 6 +++++- ltests.h | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/lopcodes.c b/lopcodes.c index 3db9db25a5..823d348592 100644 --- a/lopcodes.c +++ b/lopcodes.c @@ -1,5 +1,5 @@ /* -** $Id: lopcodes.c,v 1.80 2018/03/07 15:55:38 roberto Exp roberto $ +** $Id: lopcodes.c,v 1.81 2018/04/04 14:23:41 roberto Exp roberto $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ @@ -17,6 +17,8 @@ /* ORDER OP */ +#if defined(LUAI_DEFOPNAMES) + LUAI_DDEF const char *const luaP_opnames[NUM_OPCODES+1] = { "MOVE", "LOADI", @@ -98,6 +100,8 @@ LUAI_DDEF const char *const luaP_opnames[NUM_OPCODES+1] = { NULL }; +#endif + LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = { /* OT IT T A mode opcode */ diff --git a/ltests.h b/ltests.h index e718e7644d..a6cee3c7f6 100644 --- a/ltests.h +++ b/ltests.h @@ -1,5 +1,5 @@ /* -** $Id: ltests.h,v 2.56 2018/02/27 18:47:32 roberto Exp roberto $ +** $Id: ltests.h,v 2.57 2018/04/11 16:49:36 roberto Exp roberto $ ** Internal Header for Debugging of the Lua Implementation ** See Copyright Notice in lua.h */ @@ -24,6 +24,10 @@ #define lua_assert(c) assert(c) +/* include opcode names */ +#define LUAI_DEFOPNAMES + + /* compiled with -O0, Lua uses a lot of C stack space... */ #undef LUAI_MAXCCALLS #define LUAI_MAXCCALLS 200 From deb807837c1ed327d6069fb6676e624784d01e22 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 25 Apr 2018 13:26:20 -0300 Subject: [PATCH 0273/1145] 'luaO_pushvfstring' does not need to reallocate stack (less error cases in the API) --- lobject.c | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/lobject.c b/lobject.c index f4aa014aeb..43dc1a250c 100644 --- a/lobject.c +++ b/lobject.c @@ -1,5 +1,5 @@ /* -** $Id: lobject.c,v 2.123 2018/01/28 15:13:26 roberto Exp roberto $ +** $Id: lobject.c,v 2.124 2018/02/27 18:47:32 roberto Exp roberto $ ** Some generic functions over Lua objects ** See Copyright Notice in lua.h */ @@ -393,7 +393,7 @@ void luaO_tostring (lua_State *L, TValue *obj) { static void pushstr (lua_State *L, const char *str, size_t l) { setsvalue2s(L, L->top, luaS_newlstr(L, str, l)); - luaD_inctop(L); + L->top++; } @@ -402,11 +402,10 @@ static void pushstr (lua_State *L, const char *str, size_t l) { conventional formats, plus Lua-specific '%I' and '%U' */ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) { - int n = 0; - for (;;) { - const char *e = strchr(fmt, '%'); - if (e == NULL) break; - pushstr(L, fmt, e - fmt); + int n = 0; /* number of strings in the stack to concatenate */ + const char *e; /* points to next conversion specifier */ + while ((e = strchr(fmt, '%')) != NULL) { + pushstr(L, fmt, e - fmt); /* string up to conversion specifier */ switch (*(e+1)) { case 's': { /* zero-terminated string */ const char *s = va_arg(argp, char *); @@ -433,7 +432,7 @@ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) { case 'f': { /* a 'lua_Number' */ setfltvalue(s2v(L->top), cast_num(va_arg(argp, l_uacNumber))); top2str: /* convert the top element to a string */ - luaD_inctop(L); + L->top++; luaO_tostring(L, s2v(L->top - 1)); break; } @@ -460,9 +459,12 @@ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) { } } n += 2; - fmt = e+2; + if (L->top + 2 > L->stack_last) { /* no free stack space? */ + luaV_concat(L, n); + n = 1; + } + fmt = e + 2; } - luaD_checkstack(L, 1); pushstr(L, fmt, strlen(fmt)); if (n > 0) luaV_concat(L, n + 1); return svalue(s2v(L->top - 1)); From e64e20ac8136b6cf53601127fc5c69310d644eeb Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 2 May 2018 15:17:59 -0300 Subject: [PATCH 0274/1145] minimizing the code ran by 'vmfetch' + no more 'vra' (the code is simpler without 'vra' and conversion is a no-op) --- ldebug.c | 27 ++++--- ldebug.h | 4 +- lvm.c | 222 +++++++++++++++++++++++++++---------------------------- 3 files changed, 128 insertions(+), 125 deletions(-) diff --git a/ldebug.c b/ldebug.c index 996e8cb146..c07a37f89f 100644 --- a/ldebug.c +++ b/ldebug.c @@ -1,5 +1,5 @@ /* -** $Id: ldebug.c,v 2.155 2018/02/17 19:29:29 roberto Exp roberto $ +** $Id: ldebug.c,v 2.156 2018/03/16 15:33:34 roberto Exp roberto $ ** Debug Interface ** See Copyright Notice in lua.h */ @@ -783,17 +783,24 @@ static int changedline (Proto *p, int oldpc, int newpc) { } -void luaG_traceexec (lua_State *L) { +int luaG_traceexec (lua_State *L, const Instruction *pc) { CallInfo *ci = L->ci; lu_byte mask = L->hookmask; - int counthook = (--L->hookcount == 0 && (mask & LUA_MASKCOUNT)); + int counthook; + if (!(mask & (LUA_MASKLINE | LUA_MASKCOUNT))) { /* no hooks? */ + ci->u.l.trap = 0; /* don't need to stop again */ + return 0; /* turn off 'trap' */ + } + pc++; /* reference is always next instruction */ + ci->u.l.savedpc = pc; /* save 'pc' */ + counthook = (--L->hookcount == 0 && (mask & LUA_MASKCOUNT)); if (counthook) resethookcount(L); /* reset count */ else if (!(mask & LUA_MASKLINE)) - return; /* no line hook and count != 0; nothing to be done */ + return 1; /* no line hook and count != 0; nothing to be done now */ if (ci->callstatus & CIST_HOOKYIELD) { /* called hook last time? */ ci->callstatus &= ~CIST_HOOKYIELD; /* erase mark */ - return; /* do not call hook again (VM yielded, so it did not move) */ + return 1; /* do not call hook again (VM yielded, so it did not move) */ } if (!isIT(*(ci->u.l.savedpc - 1))) L->top = ci->top; /* prepare top */ @@ -801,15 +808,14 @@ void luaG_traceexec (lua_State *L) { luaD_hook(L, LUA_HOOKCOUNT, -1, 0, 0); /* call count hook */ if (mask & LUA_MASKLINE) { Proto *p = ci_func(ci)->p; - const Instruction *npc = ci->u.l.savedpc; - int npci = pcRel(npc, p); + int npci = pcRel(pc, p); if (npci == 0 || /* call linehook when enter a new function, */ - npc <= L->oldpc || /* when jump back (loop), or when */ + pc <= L->oldpc || /* when jump back (loop), or when */ changedline(p, pcRel(L->oldpc, p), npci)) { /* enter new line */ - int newline = luaG_getfuncline(p, npci); /* new line */ + int newline = luaG_getfuncline(p, npci); luaD_hook(L, LUA_HOOKLINE, newline, 0, 0); /* call line hook */ } - L->oldpc = npc; + L->oldpc = pc; /* 'pc' of last call to line hook */ } if (L->status == LUA_YIELD) { /* did hook yield? */ if (counthook) @@ -818,5 +824,6 @@ void luaG_traceexec (lua_State *L) { ci->callstatus |= CIST_HOOKYIELD; /* mark that it yielded */ luaD_throw(L, LUA_YIELD); } + return 1; /* keep 'trap' on */ } diff --git a/ldebug.h b/ldebug.h index e8652d8406..97a7a00ed3 100644 --- a/ldebug.h +++ b/ldebug.h @@ -1,5 +1,5 @@ /* -** $Id: ldebug.h,v 2.15 2017/06/27 11:35:31 roberto Exp roberto $ +** $Id: ldebug.h,v 2.16 2018/01/28 15:13:26 roberto Exp roberto $ ** Auxiliary functions from Debug Interface module ** See Copyright Notice in lua.h */ @@ -37,7 +37,7 @@ LUAI_FUNC l_noret luaG_runerror (lua_State *L, const char *fmt, ...); LUAI_FUNC const char *luaG_addinfo (lua_State *L, const char *msg, TString *src, int line); LUAI_FUNC l_noret luaG_errormsg (lua_State *L); -LUAI_FUNC void luaG_traceexec (lua_State *L); +LUAI_FUNC int luaG_traceexec (lua_State *L, const Instruction *pc); #endif diff --git a/lvm.c b/lvm.c index 9e044aaedd..347a8269b5 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.352 2018/04/02 17:52:07 roberto Exp roberto $ +** $Id: lvm.c,v 2.353 2018/04/04 14:23:41 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -489,7 +489,7 @@ int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2) { case LUA_TNIL: return 1; case LUA_TNUMINT: return (ivalue(t1) == ivalue(t2)); case LUA_TNUMFLT: return luai_numeq(fltvalue(t1), fltvalue(t2)); - case LUA_TBOOLEAN: return bvalue(t1) == bvalue(t2); /* true must be 1 !! */ + case LUA_TBOOLEAN: return bvalue(t1) == bvalue(t2); /* true must be 1! */ case LUA_TLIGHTUSERDATA: return pvalue(t1) == pvalue(t2); case LUA_TLCF: return fvalue(t1) == fvalue(t2); case LUA_TSHRSTR: return eqshrstr(tsvalue(t1), tsvalue(t2)); @@ -867,15 +867,12 @@ void luaV_finishOp (lua_State *L) { /* fetch an instruction and prepare its execution */ #define vmfetch() { \ - i = *(pc++); \ - if (trap) { \ - if (!(L->hookmask & (LUA_MASKLINE | LUA_MASKCOUNT))) \ - trap = ci->u.l.trap = 0; /* no need to stop again */ \ - else { savepc(L); luaG_traceexec(L); } \ - updatebase(ci); /* the trap may be just for that */ \ + if (trap) { /* stack reallocation or hooks? */ \ + trap = luaG_traceexec(L, pc); /* handle hooks */ \ + updatebase(ci); /* correct stack */ \ } \ + i = *(pc++); \ ra = RA(i); /* WARNING: any stack reallocation invalidates 'ra' */ \ - vra = s2v(ra); \ } #define vmdispatch(o) switch(o) @@ -899,7 +896,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { pc = ci->u.l.savedpc; if (trap) { if (cl->p->is_vararg) - trap = 0; /* hooks will start with PREPVARARG instruction */ + trap = 0; /* hooks will start after PREPVARARG instruction */ else if (pc == cl->p->code) /* first instruction (not resuming)? */ luaD_hookcall(L, ci); ci->u.l.trap = 1; /* there may be other hooks */ @@ -910,7 +907,6 @@ void luaV_execute (lua_State *L, CallInfo *ci) { int cond; /* flag for conditional jumps */ Instruction i; /* instruction being executed */ StkId ra; /* instruction's A register */ - TValue *vra; /* corresponding value */ vmfetch(); lua_assert(base == ci->func + 1); lua_assert(base <= L->top && L->top < L->stack + L->stacksize); @@ -927,12 +923,12 @@ void luaV_execute (lua_State *L, CallInfo *ci) { } vmcase(OP_LOADI) { lua_Integer b = GETARG_sBx(i); - setivalue(vra, b); + setivalue(s2v(ra), b); vmbreak; } vmcase(OP_LOADF) { int b = GETARG_sBx(i); - setfltvalue(vra, cast_num(b)); + setfltvalue(s2v(ra), cast_num(b)); vmbreak; } vmcase(OP_LOADKX) { @@ -942,7 +938,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_LOADBOOL) { - setbvalue(vra, GETARG_B(i)); + setbvalue(s2v(ra), GETARG_B(i)); if (GETARG_C(i)) pc++; /* skip next instruction (if C) */ vmbreak; } @@ -960,8 +956,8 @@ void luaV_execute (lua_State *L, CallInfo *ci) { } vmcase(OP_SETUPVAL) { UpVal *uv = cl->upvals[GETARG_B(i)]; - setobj(L, uv->v, vra); - luaC_barrier(L, uv, vra); + setobj(L, uv->v, s2v(ra)); + luaC_barrier(L, uv, s2v(ra)); vmbreak; } vmcase(OP_GETTABUP) { @@ -1035,25 +1031,25 @@ void luaV_execute (lua_State *L, CallInfo *ci) { TValue *rc = RKC(i); /* value */ lua_Unsigned n; if (ttisinteger(rb) /* fast track for integers? */ - ? (n = ivalue(rb), luaV_fastgeti(L, vra, n, slot)) - : luaV_fastget(L, vra, rb, slot, luaH_get)) { - luaV_finishfastset(L, vra, slot, rc); + ? (n = ivalue(rb), luaV_fastgeti(L, s2v(ra), n, slot)) + : luaV_fastget(L, s2v(ra), rb, slot, luaH_get)) { + luaV_finishfastset(L, s2v(ra), slot, rc); } else - Protect(luaV_finishset(L, vra, rb, rc, slot)); + Protect(luaV_finishset(L, s2v(ra), rb, rc, slot)); vmbreak; } vmcase(OP_SETI) { const TValue *slot; int c = GETARG_B(i); TValue *rc = RKC(i); - if (luaV_fastgeti(L, vra, c, slot)) { - luaV_finishfastset(L, vra, slot, rc); + if (luaV_fastgeti(L, s2v(ra), c, slot)) { + luaV_finishfastset(L, s2v(ra), slot, rc); } else { TValue key; setivalue(&key, c); - Protect(luaV_finishset(L, vra, &key, rc, slot)); + Protect(luaV_finishset(L, s2v(ra), &key, rc, slot)); } vmbreak; } @@ -1062,11 +1058,11 @@ void luaV_execute (lua_State *L, CallInfo *ci) { TValue *rb = KB(i); TValue *rc = RKC(i); TString *key = tsvalue(rb); /* key must be a string */ - if (luaV_fastget(L, vra, key, slot, luaH_getshortstr)) { - luaV_finishfastset(L, vra, slot, rc); + if (luaV_fastget(L, s2v(ra), key, slot, luaH_getshortstr)) { + luaV_finishfastset(L, s2v(ra), slot, rc); } else - Protect(luaV_finishset(L, vra, rb, rc, slot)); + Protect(luaV_finishset(L, s2v(ra), rb, rc, slot)); vmbreak; } vmcase(OP_NEWTABLE) { @@ -1099,10 +1095,10 @@ void luaV_execute (lua_State *L, CallInfo *ci) { int ic = GETARG_sC(i); lua_Number nb; if (ttisinteger(rb)) { - setivalue(vra, intop(+, ivalue(rb), ic)); + setivalue(s2v(ra), intop(+, ivalue(rb), ic)); } else if (tonumberns(rb, nb)) { - setfltvalue(vra, luai_numadd(L, nb, cast_num(ic))); + setfltvalue(s2v(ra), luai_numadd(L, nb, cast_num(ic))); } else Protect(luaT_trybiniTM(L, rb, ic, GETARG_k(i), ra, TM_ADD)); @@ -1113,10 +1109,10 @@ void luaV_execute (lua_State *L, CallInfo *ci) { int ic = GETARG_sC(i); lua_Number nb; if (ttisinteger(rb)) { - setivalue(vra, intop(-, ivalue(rb), ic)); + setivalue(s2v(ra), intop(-, ivalue(rb), ic)); } else if (tonumberns(rb, nb)) { - setfltvalue(vra, luai_numsub(L, nb, cast_num(ic))); + setfltvalue(s2v(ra), luai_numsub(L, nb, cast_num(ic))); } else Protect(luaT_trybiniTM(L, rb, ic, 0, ra, TM_SUB)); @@ -1127,10 +1123,10 @@ void luaV_execute (lua_State *L, CallInfo *ci) { int ic = GETARG_sC(i); lua_Number nb; if (ttisinteger(rb)) { - setivalue(vra, intop(*, ivalue(rb), ic)); + setivalue(s2v(ra), intop(*, ivalue(rb), ic)); } else if (tonumberns(rb, nb)) { - setfltvalue(vra, luai_nummul(L, nb, cast_num(ic))); + setfltvalue(s2v(ra), luai_nummul(L, nb, cast_num(ic))); } else Protect(luaT_trybiniTM(L, rb, ic, GETARG_k(i), ra, TM_MUL)); @@ -1141,13 +1137,13 @@ void luaV_execute (lua_State *L, CallInfo *ci) { int ic = GETARG_sC(i); lua_Number nb; if (ttisinteger(rb)) { - setivalue(vra, luaV_mod(L, ivalue(rb), ic)); + setivalue(s2v(ra), luaV_mod(L, ivalue(rb), ic)); } else if (tonumberns(rb, nb)) { lua_Number m; lua_Number nc = cast_num(ic); luai_nummod(L, nb, nc, m); - setfltvalue(vra, m); + setfltvalue(s2v(ra), m); } else Protect(luaT_trybiniTM(L, rb, ic, 0, ra, TM_MOD)); @@ -1159,7 +1155,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { lua_Number nb; if (tonumberns(rb, nb)) { lua_Number nc = cast_num(ic); - setfltvalue(vra, luai_numpow(L, nb, nc)); + setfltvalue(s2v(ra), luai_numpow(L, nb, nc)); } else Protect(luaT_trybiniTM(L, rb, ic, 0, ra, TM_POW)); @@ -1171,7 +1167,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { lua_Number nb; if (tonumberns(rb, nb)) { lua_Number nc = cast_num(ic); - setfltvalue(vra, luai_numdiv(L, nb, nc)); + setfltvalue(s2v(ra), luai_numdiv(L, nb, nc)); } else Protect(luaT_trybiniTM(L, rb, ic, 0, ra, TM_DIV)); @@ -1182,11 +1178,11 @@ void luaV_execute (lua_State *L, CallInfo *ci) { int ic = GETARG_sC(i); lua_Number nb; if (ttisinteger(rb)) { - setivalue(vra, luaV_div(L, ivalue(rb), ic)); + setivalue(s2v(ra), luaV_div(L, ivalue(rb), ic)); } else if (tonumberns(rb, nb)) { lua_Number nc = cast_num(ic); - setfltvalue(vra, luai_numdiv(L, nb, nc)); + setfltvalue(s2v(ra), luai_numdiv(L, nb, nc)); } else Protect(luaT_trybiniTM(L, rb, ic, 0, ra, TM_IDIV)); @@ -1198,10 +1194,10 @@ void luaV_execute (lua_State *L, CallInfo *ci) { lua_Number nb; lua_Number nc; if (ttisinteger(rb) && ttisinteger(rc)) { lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc); - setivalue(vra, intop(+, ib, ic)); + setivalue(s2v(ra), intop(+, ib, ic)); } else if (tonumberns(rb, nb) && tonumberns(rc, nc)) { - setfltvalue(vra, luai_numadd(L, nb, nc)); + setfltvalue(s2v(ra), luai_numadd(L, nb, nc)); } else Protect(luaT_trybinTM(L, rb, rc, ra, TM_ADD)); @@ -1213,10 +1209,10 @@ void luaV_execute (lua_State *L, CallInfo *ci) { lua_Number nb; lua_Number nc; if (ttisinteger(rb) && ttisinteger(rc)) { lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc); - setivalue(vra, intop(-, ib, ic)); + setivalue(s2v(ra), intop(-, ib, ic)); } else if (tonumberns(rb, nb) && tonumberns(rc, nc)) { - setfltvalue(vra, luai_numsub(L, nb, nc)); + setfltvalue(s2v(ra), luai_numsub(L, nb, nc)); } else Protect(luaT_trybinTM(L, rb, rc, ra, TM_SUB)); @@ -1228,10 +1224,10 @@ void luaV_execute (lua_State *L, CallInfo *ci) { lua_Number nb; lua_Number nc; if (ttisinteger(rb) && ttisinteger(rc)) { lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc); - setivalue(vra, intop(*, ib, ic)); + setivalue(s2v(ra), intop(*, ib, ic)); } else if (tonumberns(rb, nb) && tonumberns(rc, nc)) { - setfltvalue(vra, luai_nummul(L, nb, nc)); + setfltvalue(s2v(ra), luai_nummul(L, nb, nc)); } else Protect(luaT_trybinTM(L, rb, rc, ra, TM_MUL)); @@ -1242,7 +1238,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { TValue *rc = vRC(i); lua_Number nb; lua_Number nc; if (tonumberns(rb, nb) && tonumberns(rc, nc)) { - setfltvalue(vra, luai_numdiv(L, nb, nc)); + setfltvalue(s2v(ra), luai_numdiv(L, nb, nc)); } else Protect(luaT_trybinTM(L, rb, rc, ra, TM_DIV)); @@ -1253,7 +1249,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { TValue *p2 = KC(i); lua_Integer i1; if (tointegerns(p1, &i1)) { - setivalue(vra, intop(&, i1, ivalue(p2))); + setivalue(s2v(ra), intop(&, i1, ivalue(p2))); } else Protect(luaT_trybinassocTM(L, p1, p2, ra, TESTARG_k(i), TM_BAND)); @@ -1264,7 +1260,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { TValue *p2 = KC(i); lua_Integer i1; if (tointegerns(p1, &i1)) { - setivalue(vra, intop(|, i1, ivalue(p2))); + setivalue(s2v(ra), intop(|, i1, ivalue(p2))); } else Protect(luaT_trybinassocTM(L, p1, p2, ra, TESTARG_k(i), TM_BOR)); @@ -1275,7 +1271,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { TValue *p2 = KC(i); lua_Integer i1; if (tointegerns(p1, &i1)) { - setivalue(vra, intop(^, i1, ivalue(p2))); + setivalue(s2v(ra), intop(^, i1, ivalue(p2))); } else Protect(luaT_trybinassocTM(L, p1, p2, ra, TESTARG_k(i), TM_BXOR)); @@ -1286,7 +1282,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { TValue *rc = vRC(i); lua_Integer ib; lua_Integer ic; if (tointegerns(rb, &ib) && tointegerns(rc, &ic)) { - setivalue(vra, intop(&, ib, ic)); + setivalue(s2v(ra), intop(&, ib, ic)); } else Protect(luaT_trybinTM(L, rb, rc, ra, TM_BAND)); @@ -1297,7 +1293,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { TValue *rc = vRC(i); lua_Integer ib; lua_Integer ic; if (tointegerns(rb, &ib) && tointegerns(rc, &ic)) { - setivalue(vra, intop(|, ib, ic)); + setivalue(s2v(ra), intop(|, ib, ic)); } else Protect(luaT_trybinTM(L, rb, rc, ra, TM_BOR)); @@ -1308,7 +1304,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { TValue *rc = vRC(i); lua_Integer ib; lua_Integer ic; if (tointegerns(rb, &ib) && tointegerns(rc, &ic)) { - setivalue(vra, intop(^, ib, ic)); + setivalue(s2v(ra), intop(^, ib, ic)); } else Protect(luaT_trybinTM(L, rb, rc, ra, TM_BXOR)); @@ -1319,7 +1315,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { int ic = GETARG_sC(i); lua_Integer ib; if (tointegerns(rb, &ib)) { - setivalue(vra, luaV_shiftl(ib, -ic)); + setivalue(s2v(ra), luaV_shiftl(ib, -ic)); } else { TMS ev = TM_SHR; @@ -1335,7 +1331,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { int ic = GETARG_sC(i); lua_Integer ib; if (tointegerns(rb, &ib)) { - setivalue(vra, luaV_shiftl(ic, ib)); + setivalue(s2v(ra), luaV_shiftl(ic, ib)); } else Protect(luaT_trybiniTM(L, rb, ic, 1, ra, TM_SHL)); @@ -1346,7 +1342,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { TValue *rc = vRC(i); lua_Integer ib; lua_Integer ic; if (tointegerns(rb, &ib) && tointegerns(rc, &ic)) { - setivalue(vra, luaV_shiftl(ib, ic)); + setivalue(s2v(ra), luaV_shiftl(ib, ic)); } else Protect(luaT_trybinTM(L, rb, rc, ra, TM_SHL)); @@ -1357,7 +1353,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { TValue *rc = vRC(i); lua_Integer ib; lua_Integer ic; if (tointegerns(rb, &ib) && tointegerns(rc, &ic)) { - setivalue(vra, luaV_shiftl(ib, -ic)); + setivalue(s2v(ra), luaV_shiftl(ib, -ic)); } else Protect(luaT_trybinTM(L, rb, rc, ra, TM_SHR)); @@ -1369,12 +1365,12 @@ void luaV_execute (lua_State *L, CallInfo *ci) { lua_Number nb; lua_Number nc; if (ttisinteger(rb) && ttisinteger(rc)) { lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc); - setivalue(vra, luaV_mod(L, ib, ic)); + setivalue(s2v(ra), luaV_mod(L, ib, ic)); } else if (tonumberns(rb, nb) && tonumberns(rc, nc)) { lua_Number m; luai_nummod(L, nb, nc, m); - setfltvalue(vra, m); + setfltvalue(s2v(ra), m); } else Protect(luaT_trybinTM(L, rb, rc, ra, TM_MOD)); @@ -1386,10 +1382,10 @@ void luaV_execute (lua_State *L, CallInfo *ci) { lua_Number nb; lua_Number nc; if (ttisinteger(rb) && ttisinteger(rc)) { lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc); - setivalue(vra, luaV_div(L, ib, ic)); + setivalue(s2v(ra), luaV_div(L, ib, ic)); } else if (tonumberns(rb, nb) && tonumberns(rc, nc)) { - setfltvalue(vra, luai_numidiv(L, nb, nc)); + setfltvalue(s2v(ra), luai_numidiv(L, nb, nc)); } else Protect(luaT_trybinTM(L, rb, rc, ra, TM_IDIV)); @@ -1400,7 +1396,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { TValue *rc = vRC(i); lua_Number nb; lua_Number nc; if (tonumberns(rb, nb) && tonumberns(rc, nc)) { - setfltvalue(vra, luai_numpow(L, nb, nc)); + setfltvalue(s2v(ra), luai_numpow(L, nb, nc)); } else Protect(luaT_trybinTM(L, rb, rc, ra, TM_POW)); @@ -1411,10 +1407,10 @@ void luaV_execute (lua_State *L, CallInfo *ci) { lua_Number nb; if (ttisinteger(rb)) { lua_Integer ib = ivalue(rb); - setivalue(vra, intop(-, 0, ib)); + setivalue(s2v(ra), intop(-, 0, ib)); } else if (tonumberns(rb, nb)) { - setfltvalue(vra, luai_numunm(L, nb)); + setfltvalue(s2v(ra), luai_numunm(L, nb)); } else Protect(luaT_trybinTM(L, rb, rb, ra, TM_UNM)); @@ -1424,7 +1420,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { TValue *rb = vRB(i); lua_Integer ib; if (tointegerns(rb, &ib)) { - setivalue(vra, intop(^, ~l_castS2U(0), ib)); + setivalue(s2v(ra), intop(^, ~l_castS2U(0), ib)); } else Protect(luaT_trybinTM(L, rb, rb, ra, TM_BNOT)); @@ -1433,7 +1429,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmcase(OP_NOT) { TValue *rb = vRB(i); int nrb = l_isfalse(rb); /* next assignment may change this value */ - setbvalue(vra, nrb); + setbvalue(s2v(ra), nrb); vmbreak; } vmcase(OP_LEN) { @@ -1457,45 +1453,45 @@ void luaV_execute (lua_State *L, CallInfo *ci) { } vmcase(OP_EQ) { TValue *rb = vRB(i); - Protect(cond = luaV_equalobj(L, vra, rb)); + Protect(cond = luaV_equalobj(L, s2v(ra), rb)); docondjump(); vmbreak; } vmcase(OP_LT) { TValue *rb = vRB(i); - if (ttisinteger(vra) && ttisinteger(rb)) - cond = (ivalue(vra) < ivalue(rb)); - else if (ttisnumber(vra) && ttisnumber(rb)) - cond = LTnum(vra, rb); + if (ttisinteger(s2v(ra)) && ttisinteger(rb)) + cond = (ivalue(s2v(ra)) < ivalue(rb)); + else if (ttisnumber(s2v(ra)) && ttisnumber(rb)) + cond = LTnum(s2v(ra), rb); else - Protect(cond = lessthanothers(L, vra, rb)); + Protect(cond = lessthanothers(L, s2v(ra), rb)); docondjump(); vmbreak; } vmcase(OP_LE) { TValue *rb = vRB(i); - if (ttisinteger(vra) && ttisinteger(rb)) - cond = (ivalue(vra) <= ivalue(rb)); - else if (ttisnumber(vra) && ttisnumber(rb)) - cond = LEnum(vra, rb); + if (ttisinteger(s2v(ra)) && ttisinteger(rb)) + cond = (ivalue(s2v(ra)) <= ivalue(rb)); + else if (ttisnumber(s2v(ra)) && ttisnumber(rb)) + cond = LEnum(s2v(ra), rb); else - Protect(cond = lessequalothers(L, vra, rb)); + Protect(cond = lessequalothers(L, s2v(ra), rb)); docondjump(); vmbreak; } vmcase(OP_EQK) { TValue *rb = KB(i); /* basic types do not use '__eq'; we can use raw equality */ - cond = luaV_equalobj(NULL, vra, rb); + cond = luaV_equalobj(NULL, s2v(ra), rb); docondjump(); vmbreak; } vmcase(OP_EQI) { int im = GETARG_sB(i); - if (ttisinteger(vra)) - cond = (ivalue(vra) == im); - else if (ttisfloat(vra)) - cond = luai_numeq(fltvalue(vra), cast_num(im)); + if (ttisinteger(s2v(ra))) + cond = (ivalue(s2v(ra)) == im); + else if (ttisfloat(s2v(ra))) + cond = luai_numeq(fltvalue(s2v(ra)), cast_num(im)); else cond = 0; /* other types cannot be equal to a number */ docondjump(); @@ -1503,50 +1499,50 @@ void luaV_execute (lua_State *L, CallInfo *ci) { } vmcase(OP_LTI) { int im = GETARG_sB(i); - if (ttisinteger(vra)) - cond = (ivalue(vra) < im); - else if (ttisfloat(vra)) - cond = luai_numlt(fltvalue(vra), cast_num(im)); + if (ttisinteger(s2v(ra))) + cond = (ivalue(s2v(ra)) < im); + else if (ttisfloat(s2v(ra))) + cond = luai_numlt(fltvalue(s2v(ra)), cast_num(im)); else - Protect(cond = luaT_callorderiTM(L, vra, im, 0, TM_LT)); + Protect(cond = luaT_callorderiTM(L, s2v(ra), im, 0, TM_LT)); docondjump(); vmbreak; } vmcase(OP_LEI) { int im = GETARG_sB(i); - if (ttisinteger(vra)) - cond = (ivalue(vra) <= im); - else if (ttisfloat(vra)) - cond = luai_numle(fltvalue(vra), cast_num(im)); + if (ttisinteger(s2v(ra))) + cond = (ivalue(s2v(ra)) <= im); + else if (ttisfloat(s2v(ra))) + cond = luai_numle(fltvalue(s2v(ra)), cast_num(im)); else - Protect(cond = luaT_callorderiTM(L, vra, im, 0, TM_LE)); + Protect(cond = luaT_callorderiTM(L, s2v(ra), im, 0, TM_LE)); docondjump(); vmbreak; } vmcase(OP_GTI) { int im = GETARG_sB(i); - if (ttisinteger(vra)) - cond = (im < ivalue(vra)); - else if (ttisfloat(vra)) - cond = luai_numlt(cast_num(im), fltvalue(vra)); + if (ttisinteger(s2v(ra))) + cond = (im < ivalue(s2v(ra))); + else if (ttisfloat(s2v(ra))) + cond = luai_numlt(cast_num(im), fltvalue(s2v(ra))); else - Protect(cond = luaT_callorderiTM(L, vra, im, 1, TM_LT)); + Protect(cond = luaT_callorderiTM(L, s2v(ra), im, 1, TM_LT)); docondjump(); vmbreak; } vmcase(OP_GEI) { int im = GETARG_sB(i); - if (ttisinteger(vra)) - cond = (im <= ivalue(vra)); - else if (ttisfloat(vra)) - cond = luai_numle(cast_num(im), fltvalue(vra)); + if (ttisinteger(s2v(ra))) + cond = (im <= ivalue(s2v(ra))); + else if (ttisfloat(s2v(ra))) + cond = luai_numle(cast_num(im), fltvalue(s2v(ra))); else - Protect(cond = luaT_callorderiTM(L, vra, im, 1, TM_LE)); + Protect(cond = luaT_callorderiTM(L, s2v(ra), im, 1, TM_LE)); docondjump(); vmbreak; } vmcase(OP_TEST) { - cond = !l_isfalse(vra); + cond = !l_isfalse(s2v(ra)); docondjump(); vmbreak; } @@ -1583,11 +1579,11 @@ void luaV_execute (lua_State *L, CallInfo *ci) { delta = ci->u.l.nextraargs + nparams1; luaF_close(L, base); /* close upvalues from current call */ } - if (!ttisfunction(vra)) { /* not a function? */ + if (!ttisfunction(s2v(ra))) { /* not a function? */ luaD_tryfuncTM(L, ra); /* try '__call' metamethod */ b++; /* there is now one extra argument */ } - if (!ttisLclosure(vra)) { /* C function? */ + if (!ttisLclosure(s2v(ra))) { /* C function? */ luaD_call(L, ra, LUA_MULTRET); /* call it */ updatetrap(ci); if (trap) { /* stack may have been relocated */ @@ -1647,18 +1643,18 @@ void luaV_execute (lua_State *L, CallInfo *ci) { return; } vmcase(OP_FORLOOP1) { - lua_Integer idx = intop(+, ivalue(vra), 1); /* increment index */ + lua_Integer idx = intop(+, ivalue(s2v(ra)), 1); /* increment index */ lua_Integer limit = ivalue(s2v(ra + 1)); if (idx <= limit) { pc -= GETARG_Bx(i); /* jump back */ - chgivalue(vra, idx); /* update internal index... */ + chgivalue(s2v(ra), idx); /* update internal index... */ setivalue(s2v(ra + 3), idx); /* ...and external index */ } updatetrap(ci); vmbreak; } vmcase(OP_FORPREP1) { - TValue *init = vra; + TValue *init = s2v(ra); TValue *plimit = s2v(ra + 1); lua_Integer ilimit, initv; int stopnow; @@ -1673,25 +1669,25 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_FORLOOP) { - if (ttisinteger(vra)) { /* integer loop? */ + if (ttisinteger(s2v(ra))) { /* integer loop? */ lua_Integer step = ivalue(s2v(ra + 2)); - lua_Integer idx = intop(+, ivalue(vra), step); /* increment index */ + lua_Integer idx = intop(+, ivalue(s2v(ra)), step); /* new index */ lua_Integer limit = ivalue(s2v(ra + 1)); if ((0 < step) ? (idx <= limit) : (limit <= idx)) { pc -= GETARG_Bx(i); /* jump back */ - chgivalue(vra, idx); /* update internal index... */ + chgivalue(s2v(ra), idx); /* update internal index... */ setivalue(s2v(ra + 3), idx); /* ...and external index */ } } else { /* floating loop */ lua_Number step = fltvalue(s2v(ra + 2)); lua_Number limit = fltvalue(s2v(ra + 1)); - lua_Number idx = fltvalue(vra); + lua_Number idx = fltvalue(s2v(ra)); idx = luai_numadd(L, idx, step); /* inc. index */ if (luai_numlt(0, step) ? luai_numle(idx, limit) : luai_numle(limit, idx)) { pc -= GETARG_Bx(i); /* jump back */ - chgfltvalue(vra, idx); /* update internal index... */ + chgfltvalue(s2v(ra), idx); /* update internal index... */ setfltvalue(s2v(ra + 3), idx); /* ...and external index */ } } @@ -1699,7 +1695,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_FORPREP) { - TValue *init = vra; + TValue *init = s2v(ra); TValue *plimit = s2v(ra + 1); TValue *pstep = s2v(ra + 2); lua_Integer ilimit; @@ -1761,7 +1757,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { if (c == 0) { c = GETARG_Ax(*pc); pc++; } - h = hvalue(vra); + h = hvalue(s2v(ra)); last = ((c-1)*LFIELDS_PER_FLUSH) + n; if (last > h->sizearray) /* needs more space? */ luaH_resizearray(L, h, last); /* preallocate it at once */ From 3e7415e846f2f75ed29e27e1211205ea6243e0c7 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 4 May 2018 17:01:45 -0300 Subject: [PATCH 0275/1145] reorganization of '#if's for sellecting a type for 'Rand64' + comments --- lmathlib.c | 87 +++++++++++++++++++++++++++++++++++------------------- ltests.h | 6 +++- 2 files changed, 61 insertions(+), 32 deletions(-) diff --git a/lmathlib.c b/lmathlib.c index 3f17890ba6..16b8a9cc8b 100644 --- a/lmathlib.c +++ b/lmathlib.c @@ -1,5 +1,5 @@ /* -** $Id: lmathlib.c,v 1.130 2018/04/06 15:41:29 roberto Exp roberto $ +** $Id: lmathlib.c,v 1.131 2018/04/06 17:52:42 roberto Exp roberto $ ** Standard mathematical library ** See Copyright Notice in lua.h */ @@ -257,27 +257,45 @@ static int math_type (lua_State *L) { #endif -#if (!defined(LUA_USE_C89) && defined(LLONG_MAX) && !defined(LUA_DEBUG)) \ - || defined(Rand64) /* { */ - /* -** Assume long long. +** LUA_RAND32 forces the use of 32-bit integers in the implementation +** of the PRN generator (mainly for testing). */ +#if !defined(LUA_RAND32) && !defined(Rand64) + +/* try to find an integer type with at least 64 bits */ + +#if (LONG_MAX >> 31 >> 31) >= 1 + +/* 'long' has at least 64 bits */ +#define Rand64 unsigned long + +#elif !defined(LUA_USE_C89) && defined(LLONG_MAX) + +/* there is a 'long long' type (which must have at least 64 bits) */ +#define Rand64 unsigned long long + +#elif (LUA_MAXINTEGER >> 31 >> 31) >= 1 + +/* 'lua_Integer' has at least 64 bits */ +#define Rand64 LUA_UNSIGNED + +#endif -#if !defined(Rand64) -/* a 64-bit value */ -typedef unsigned long long Rand64; #endif +#if defined(Rand64) /* { */ + /* +** Standard implementation, using 64-bit integers. ** If 'Rand64' has more than 64 bits, the extra bits do not interfere -** with the 64 initial bits, except in a right shift. Otherwise, we just -** have to make sure we never use them. +** with the 64 initial bits, except in a right shift. Moreover, the +** final result has to discard the extra bits. */ /* avoid using extra bits when needed */ -#define trim64(x) ((x) & 0xffffffffffffffffU) +#define trim64(x) ((x) & 0xffffffffffffffffu) /* rotate left 'x' by 'n' bits */ @@ -315,24 +333,23 @@ static lua_Number I2d (Rand64 x) { /* convert a 'Rand64' to a 'lua_Unsigned' */ #define I2UInt(x) ((lua_Unsigned)trim64(x)) -/* convert a 'lua_Unsigned' to an 'Rand64' */ +/* convert a 'lua_Unsigned' to a 'Rand64' */ #define Int2I(x) ((Rand64)(x)) -#else /* no long long }{ */ - -/* -** Use two 32-bit integers to represent a 64-bit quantity. -*/ +#else /* no 'Rand64' }{ */ -#if LUAI_BITSINT >= 32 +/* get an integer with at least 32 bits */ +#if (INT_MAX >> 30) >= 1 typedef unsigned int lu_int32; #else typedef unsigned long lu_int32; #endif -/* a 64-bit value */ +/* +** Use two 32-bit integers to represent a 64-bit quantity. +*/ typedef struct Rand64 { lu_int32 h; /* higher half */ lu_int32 l; /* lower half */ @@ -342,17 +359,18 @@ typedef struct Rand64 { /* ** If 'lu_int32' has more than 32 bits, the extra bits do not interfere ** with the 32 initial bits, except in a right shift and comparisons. -** Otherwise, we just have to make sure we never use them. +** Moreover, the final result has to discard the extra bits. */ /* avoid using extra bits when needed */ -#define trim32(x) ((x) & 0xffffffffU) +#define trim32(x) ((x) & 0xffffffffu) /* ** basic operations on 'Rand64' values */ +/* build a new Rand64 value */ static Rand64 packI (lu_int32 h, lu_int32 l) { Rand64 result; result.h = h; @@ -360,16 +378,19 @@ static Rand64 packI (lu_int32 h, lu_int32 l) { return result; } +/* return i << n */ static Rand64 Ishl (Rand64 i, int n) { lua_assert(n > 0 && n < 32); return packI((i.h << n) | (trim32(i.l) >> (32 - n)), i.l << n); } +/* i1 ^= i2 */ static void Ixor (Rand64 *i1, Rand64 i2) { i1->h ^= i2.h; i1->l ^= i2.l; } +/* return i1 + i2 */ static Rand64 Iadd (Rand64 i1, Rand64 i2) { Rand64 result = packI(i1.h + i2.h, i1.l + i2.l); if (trim32(result.l) < trim32(i1.l)) /* carry? */ @@ -377,14 +398,17 @@ static Rand64 Iadd (Rand64 i1, Rand64 i2) { return result; } +/* return i * 5 */ static Rand64 times5 (Rand64 i) { return Iadd(Ishl(i, 2), i); /* i * 5 == (i << 2) + i */ } +/* return i * 9 */ static Rand64 times9 (Rand64 i) { return Iadd(Ishl(i, 3), i); /* i * 9 == (i << 3) + i */ } +/* return 'i' rotated left 'n' bits */ static Rand64 rotl (Rand64 i, int n) { lua_assert(n > 0 && n < 32); return packI((i.h << n) | (trim32(i.l) >> (32 - n)), @@ -416,7 +440,7 @@ static Rand64 nextrand (Rand64 *state) { /* -** Converts an 'Rand64' into a float. +** Converts a 'Rand64' into a float. */ /* an unsigned 1 with proper type */ @@ -452,11 +476,12 @@ static lua_Number I2d (Rand64 x) { } +/* convert a 'Rand64' to a 'lua_Unsigned' */ static lua_Unsigned I2UInt (Rand64 x) { - return ((lua_Unsigned)x.h << 31 << 1) | (lua_Unsigned)trim32(x.l); + return ((lua_Unsigned)trim32(x.h) << 31 << 1) | (lua_Unsigned)trim32(x.l); } - +/* convert a 'lua_Unsigned' to a 'Rand64' */ static Rand64 Int2I (lua_Unsigned n) { return packI((lu_int32)(n >> 31 >> 1), (lu_int32)n); } @@ -479,7 +504,7 @@ typedef struct { ** division). To get a uniform projection into [0, n], we first compute ** 'lim', the smallest Mersenne number not smaller than 'n'. We then ** project 'ran' into the interval [0, lim]. If the result is inside -** [0, n], we are done. Otherwise, we try with another 'ran' until we +** [0, n], we are done. Otherwise, we try with another 'ran', until we ** have a result inside the interval. */ static lua_Unsigned project (lua_Unsigned ran, lua_Unsigned n, @@ -492,15 +517,15 @@ static lua_Unsigned project (lua_Unsigned ran, lua_Unsigned n, lim |= (lim >> 4); lim |= (lim >> 8); lim |= (lim >> 16); -#if (LUA_MAXINTEGER >> 30 >> 2) > 0 +#if (LUA_MAXINTEGER >> 30 >> 1) > 0 lim |= (lim >> 32); /* integer type has more than 32 bits */ #endif } - lua_assert((lim & (lim + 1)) == 0 /* 'lim + 1' is a power of 2 */ - && lim >= n /* not smaller than 'n' */ - && (lim == 0 || (lim >> 1) < n)); /* it is the smallest one */ - while ((ran &= lim) > n) - ran = I2UInt(nextrand(state->s)); + lua_assert((lim & (lim + 1)) == 0 /* 'lim + 1' is a power of 2, */ + && lim >= n /* not smaller than 'n', */ + && (lim == 0 || (lim >> 1) < n)); /* and it is the smallest one */ + while ((ran &= lim) > n) /* project 'ran' into [0..lim] */ + ran = I2UInt(nextrand(state->s)); /* not inside [0..n]? try again */ return ran; } diff --git a/ltests.h b/ltests.h index a6cee3c7f6..8d965e0a6c 100644 --- a/ltests.h +++ b/ltests.h @@ -1,5 +1,5 @@ /* -** $Id: ltests.h,v 2.57 2018/04/11 16:49:36 roberto Exp roberto $ +** $Id: ltests.h,v 2.58 2018/04/19 15:42:41 roberto Exp roberto $ ** Internal Header for Debugging of the Lua Implementation ** See Copyright Notice in lua.h */ @@ -49,6 +49,10 @@ #define LUA_USE_JUMPTABLE 0 +/* use 32-bit integers in random generator */ +#define LUA_RAND32 + + /* memory-allocator control variables */ typedef struct Memcontrol { unsigned long numblocks; From 80bd4a89407fcba641f0ea53379e93523943ea6a Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 9 May 2018 11:54:37 -0300 Subject: [PATCH 0276/1145] correction on xoshiro256** algorithm (should use state[1] instead of state[0] for output) --- lmathlib.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lmathlib.c b/lmathlib.c index 16b8a9cc8b..0dc8a35b84 100644 --- a/lmathlib.c +++ b/lmathlib.c @@ -1,5 +1,5 @@ /* -** $Id: lmathlib.c,v 1.131 2018/04/06 17:52:42 roberto Exp roberto $ +** $Id: lmathlib.c,v 1.132 2018/05/04 20:01:45 roberto Exp roberto $ ** Standard mathematical library ** See Copyright Notice in lua.h */ @@ -304,7 +304,7 @@ static Rand64 rotl (Rand64 x, int n) { } static Rand64 nextrand (Rand64 *state) { - Rand64 res = rotl(state[0] * 5, 7) * 9; + Rand64 res = rotl(state[1] * 5, 7) * 9; Rand64 t = state[1] << 17; state[2] ^= state[0]; state[3] ^= state[1]; @@ -427,7 +427,7 @@ static Rand64 rotl1 (Rand64 i, int n) { ** implementation of 'xoshiro256**' algorithm on 'Rand64' values */ static Rand64 nextrand (Rand64 *state) { - Rand64 res = times9(rotl(times5(state[0]), 7)); + Rand64 res = times9(rotl(times5(state[1]), 7)); Rand64 t = Ishl(state[1], 17); Ixor(&state[2], state[0]); Ixor(&state[3], state[1]); From de53c2ec7ed5115ec78dd0c497d62dadb7eb2161 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 16 May 2018 08:27:59 -0300 Subject: [PATCH 0277/1145] using some weak "randomness" (time and memory address) to initialize seeds for the PRNG --- lmathlib.c | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/lmathlib.c b/lmathlib.c index 0dc8a35b84..ecbf4dd184 100644 --- a/lmathlib.c +++ b/lmathlib.c @@ -1,5 +1,5 @@ /* -** $Id: lmathlib.c,v 1.132 2018/05/04 20:01:45 roberto Exp roberto $ +** $Id: lmathlib.c,v 1.133 2018/05/09 14:54:37 roberto Exp roberto $ ** Standard mathematical library ** See Copyright Notice in lua.h */ @@ -14,6 +14,7 @@ #include #include #include +#include #include "lua.h" @@ -304,14 +305,15 @@ static Rand64 rotl (Rand64 x, int n) { } static Rand64 nextrand (Rand64 *state) { - Rand64 res = rotl(state[1] * 5, 7) * 9; - Rand64 t = state[1] << 17; - state[2] ^= state[0]; - state[3] ^= state[1]; - state[1] ^= state[2]; - state[0] ^= state[3]; - state[2] ^= t; - state[3] = rotl(state[3], 45); + Rand64 state0 = state[0]; + Rand64 state1 = state[1]; + Rand64 state2 = state[2] ^ state0; + Rand64 state3 = state[3] ^ state1; + Rand64 res = rotl(state1 * 5, 7) * 9; + state[0] = state0 ^ state3; + state[1] = state1 ^ state2; + state[2] = state2 ^ (state1 << 17); + state[3] = rotl(state3, 45); return res; } @@ -591,9 +593,18 @@ static const luaL_Reg randfuncs[] = { {NULL, NULL} }; + +/* +** Register the random functions and initialize their state. +** To give some "randomness" to the initial seed, use the current time +** and the address of 'L' (in case the machine does address space layout +** randomization). +*/ static void setrandfunc (lua_State *L) { RanState *state = (RanState *)lua_newuserdatauv(L, sizeof(RanState), 0); - setseed(state->s, 0, 0); + lua_Unsigned seed1 = (lua_Unsigned)time(NULL); + lua_Unsigned seed2 = (lua_Unsigned)(size_t)L; + setseed(state->s, seed1, seed2); luaL_setfuncs(L, randfuncs, 1); } From 02ed0b2c300e9db643454743a26537756dc90b67 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 22 May 2018 09:02:36 -0300 Subject: [PATCH 0278/1145] in 'luaD_poscall', there is no need to compute 'firstResult' when 'nres==0' --- ldo.c | 69 ++++++++++++++++++++++++++--------------------------------- ldo.h | 5 ++--- lvm.c | 25 ++++++++++++++-------- 3 files changed, 48 insertions(+), 51 deletions(-) diff --git a/ldo.c b/ldo.c index 41b41756ed..169eb71453 100644 --- a/ldo.c +++ b/ldo.c @@ -1,5 +1,5 @@ /* -** $Id: ldo.c,v 2.199 2018/03/07 16:26:01 roberto Exp roberto $ +** $Id: ldo.c,v 2.200 2018/03/16 15:33:34 roberto Exp roberto $ ** Stack and Call structure of Lua ** See Copyright Notice in lua.h */ @@ -319,14 +319,15 @@ void luaD_hookcall (lua_State *L, CallInfo *ci) { } -static void rethook (lua_State *L, CallInfo *ci, StkId firstres, int nres) { +static StkId rethook (lua_State *L, CallInfo *ci, StkId firstres, int nres) { + ptrdiff_t oldtop = savestack(L, L->top); /* hook may change top */ int delta = 0; if (isLuacode(ci)) { Proto *p = clLvalue(s2v(ci->func))->p; if (p->is_vararg) delta = ci->u.l.nextraargs + p->numparams + 1; if (L->top < ci->top) - L->top = ci->top; /* correct top */ + L->top = ci->top; /* correct top to run hook */ } if (L->hookmask & LUA_MASKRET) { /* is return hook on? */ int ftransfer; @@ -337,6 +338,7 @@ static void rethook (lua_State *L, CallInfo *ci, StkId firstres, int nres) { } if (isLua(ci->previous)) L->oldpc = ci->previous->u.l.savedpc; /* update 'oldpc' */ + return restorestack(L, oldtop); } @@ -363,40 +365,33 @@ void luaD_tryfuncTM (lua_State *L, StkId func) { ** expressions, multiple results for tail calls/single parameters) ** separated. */ -static void moveresults (lua_State *L, StkId firstResult, StkId res, - int nres, int wanted) { +static void moveresults (lua_State *L, StkId res, int nres, int wanted) { switch (wanted) { /* handle typical cases separately */ - case 0: break; /* nothing to move */ - case 1: { /* one result needed */ + case 0: /* no values needed */ + L->top = res; + break; + case 1: /* one value needed */ if (nres == 0) /* no results? */ setnilvalue(s2v(res)); /* adjust with nil */ else - setobjs2s(L, res, firstResult); /* move it to proper place */ + setobjs2s(L, res, L->top - nres); /* move it to proper place */ + L->top = res + 1; break; - } - case LUA_MULTRET: { + case LUA_MULTRET: + wanted = nres; /* we want all results */ + /* FALLTHROUGH */ + default: { /* multiple results */ + StkId firstresult = L->top - nres; /* index of first result */ int i; - for (i = 0; i < nres; i++) /* move all results to correct place */ - setobjs2s(L, res + i, firstResult + i); - wanted = nres; /* it wanted what it had */ - break; - } - default: { - int i; - if (wanted <= nres) { /* enough results? */ - for (i = 0; i < wanted; i++) /* move wanted results to correct place */ - setobjs2s(L, res + i, firstResult + i); - } - else { /* not enough results; use all of them plus nils */ - for (i = 0; i < nres; i++) /* move all results to correct place */ - setobjs2s(L, res + i, firstResult + i); - for (; i < wanted; i++) /* complete wanted number of results */ - setnilvalue(s2v(res + i)); - } + /* move all results to correct place */ + for (i = 0; i < nres && i < wanted; i++) + setobjs2s(L, res + i, firstresult + i); + for (; i < wanted; i++) /* complete wanted number of results */ + setnilvalue(s2v(res + i)); + L->top = res + wanted; /* top points after the last result */ break; } } - L->top = res + wanted; /* top points after the last result */ } @@ -404,15 +399,12 @@ static void moveresults (lua_State *L, StkId firstResult, StkId res, ** Finishes a function call: calls hook if necessary, removes CallInfo, ** moves current number of results to proper place. */ -void luaD_poscall (lua_State *L, CallInfo *ci, StkId firstResult, int nres) { - if (L->hookmask) { - ptrdiff_t fr = savestack(L, firstResult); /* hook may change stack */ - rethook(L, ci, firstResult, nres); - firstResult = restorestack(L, fr); - } +void luaD_poscall (lua_State *L, CallInfo *ci, int nres) { + if (L->hookmask) + L->top = rethook(L, ci, L->top - nres, nres); L->ci = ci->previous; /* back to caller */ /* move results to proper place */ - moveresults(L, firstResult, ci->func, nres, ci->nresults); + moveresults(L, ci->func, nres, ci->nresults); } @@ -477,7 +469,7 @@ void luaD_call (lua_State *L, StkId func, int nresults) { n = (*f)(L); /* do the actual call */ lua_lock(L); api_checknelems(L, n); - luaD_poscall(L, ci, L->top - n, n); + luaD_poscall(L, ci, n); break; } case LUA_TLCL: { /* Lua function */ @@ -540,7 +532,7 @@ static void finishCcall (lua_State *L, int status) { n = (*ci->u.c.k)(L, status, ci->u.c.ctx); /* call continuation function */ lua_lock(L); api_checknelems(L, n); - luaD_poscall(L, ci, L->top - n, n); /* finish 'luaD_call' */ + luaD_poscall(L, ci, n); /* finish 'luaD_call' */ } @@ -642,9 +634,8 @@ static void resume (lua_State *L, void *ud) { n = (*ci->u.c.k)(L, LUA_YIELD, ci->u.c.ctx); /* call continuation */ lua_lock(L); api_checknelems(L, n); - firstArg = L->top - n; /* yield results come from continuation */ } - luaD_poscall(L, ci, firstArg, n); /* finish 'luaD_call' */ + luaD_poscall(L, ci, n); /* finish 'luaD_call' */ } unroll(L, NULL); /* run continuation */ } diff --git a/ldo.h b/ldo.h index 1fdc75be04..a3ac6f503a 100644 --- a/ldo.h +++ b/ldo.h @@ -1,5 +1,5 @@ /* -** $Id: ldo.h,v 2.42 2018/02/15 15:34:29 roberto Exp roberto $ +** $Id: ldo.h,v 2.43 2018/02/17 19:29:29 roberto Exp roberto $ ** Stack and Call structure of Lua ** See Copyright Notice in lua.h */ @@ -61,8 +61,7 @@ LUAI_FUNC void luaD_callnoyield (lua_State *L, StkId func, int nResults); LUAI_FUNC void luaD_tryfuncTM (lua_State *L, StkId func); LUAI_FUNC int luaD_pcall (lua_State *L, Pfunc func, void *u, ptrdiff_t oldtop, ptrdiff_t ef); -LUAI_FUNC void luaD_poscall (lua_State *L, CallInfo *ci, StkId firstResult, - int nres); +LUAI_FUNC void luaD_poscall (lua_State *L, CallInfo *ci, int nres); LUAI_FUNC int luaD_reallocstack (lua_State *L, int newsize, int raiseerror); LUAI_FUNC int luaD_growstack (lua_State *L, int n, int raiseerror); LUAI_FUNC void luaD_shrinkstack (lua_State *L); diff --git a/lvm.c b/lvm.c index 347a8269b5..4406afb64d 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.353 2018/04/04 14:23:41 roberto Exp roberto $ +** $Id: lvm.c,v 2.354 2018/05/02 18:17:59 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -1591,7 +1591,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { ra = RA(i); } ci->func -= delta; - luaD_poscall(L, ci, ra, cast_int(L->top - ra)); + luaD_poscall(L, ci, cast_int(L->top - ra)); return; } else { /* Lua tail call */ @@ -1602,20 +1602,25 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_RETURN) { - int b = GETARG_B(i); - int n = (b != 0 ? b - 1 : cast_int(L->top - ra)); + int n = GETARG_B(i) - 1; /* number of results */ + if (n < 0) /* not fixed? */ + n = cast_int(L->top - ra); /* get what is available */ + else + L->top = ra + n; /* set call for 'luaD_poscall' */ if (TESTARG_k(i)) { int nparams1 = GETARG_C(i); if (nparams1) /* vararg function? */ ci->func -= ci->u.l.nextraargs + nparams1; luaF_close(L, base); /* there may be open upvalues */ } - halfProtect(luaD_poscall(L, ci, ra, n)); + halfProtect(luaD_poscall(L, ci, n)); return; } vmcase(OP_RETURN0) { - if (L->hookmask) - halfProtect(luaD_poscall(L, ci, ra, 0)); /* no hurry... */ + if (L->hookmask) { + L->top = ra; + halfProtect(luaD_poscall(L, ci, 0)); /* no hurry... */ + } else { int nres = ci->nresults; L->ci = ci->previous; /* back to caller */ @@ -1626,8 +1631,10 @@ void luaV_execute (lua_State *L, CallInfo *ci) { return; } vmcase(OP_RETURN1) { - if (L->hookmask) - halfProtect(luaD_poscall(L, ci, ra, 1)); /* no hurry... */ + if (L->hookmask) { + L->top = ra + 1; + halfProtect(luaD_poscall(L, ci, 1)); /* no hurry... */ + } else { int nres = ci->nresults; L->ci = ci->previous; /* back to caller */ From 892aff2a07585eee25bf9fd68e9c2538d537b74f Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 23 May 2018 11:41:20 -0300 Subject: [PATCH 0279/1145] avoid circular inclusion between ltm.h <-> lstate.h --- ltm.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/ltm.h b/ltm.h index 7eb60b4cf6..24b9a84e9a 100644 --- a/ltm.h +++ b/ltm.h @@ -1,5 +1,5 @@ /* -** $Id: ltm.h,v 2.34 2018/02/27 17:48:28 roberto Exp roberto $ +** $Id: ltm.h,v 2.35 2018/04/04 14:23:41 roberto Exp roberto $ ** Tag methods ** See Copyright Notice in lua.h */ @@ -9,7 +9,6 @@ #include "lobject.h" -#include "lstate.h" /* From 3b8dba5279e98e03a194f048a82fcab9945bbbed Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 24 May 2018 17:25:14 -0300 Subject: [PATCH 0280/1145] added patches for two bugs (5.3.4-2 and 5.3.4-3) --- bugs | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 51 insertions(+), 2 deletions(-) diff --git a/bugs b/bugs index d796facb38..d87f12d318 100644 --- a/bugs +++ b/bugs @@ -3680,9 +3680,9 @@ It needs an "interceptor" 'memcmp' function that continues reading memory after a difference is found.]], patch = [[ 2c2 -< ** $Id: bugs,v 1.158 2017/12/06 18:20:28 roberto Exp roberto $ +< ** $Id: bugs,v 1.159 2017/12/13 18:35:03 roberto Exp roberto $ --- -> ** $Id: bugs,v 1.158 2017/12/06 18:20:28 roberto Exp roberto $ +> ** $Id: bugs,v 1.159 2017/12/13 18:35:03 roberto Exp roberto $ 263c263,264 < for (option = LUA_STRFTIMEOPTIONS; *option != '\0'; option += oplen) { --- @@ -3737,6 +3737,10 @@ patch = [[ } +----------------------------------------------------------------- +-- Lua 5.3.4 + + Bug{ what = [[Wrong code for a goto followed by a label inside an 'if']], report = [[云风, 2017/04/13]], @@ -3782,6 +3786,19 @@ for i = 1, 0x7fffffff do end ]], patch = [[ +--- ltable.c 2017/04/19 17:20:42 2.118.1.1 ++++ ltable.c 2018/05/24 18:34:38 +@@ -223,7 +223,9 @@ + unsigned int na = 0; /* number of elements to go to array part */ + unsigned int optimal = 0; /* optimal size for array part */ + /* loop while keys can fill more than half of total size */ +- for (i = 0, twotoi = 1; *pna > twotoi / 2; i++, twotoi *= 2) { ++ for (i = 0, twotoi = 1; ++ twotoi > 0 && *pna > twotoi / 2; ++ i++, twotoi *= 2) { + if (nums[i] > 0) { + a += nums[i]; + if (a > twotoi/2) { /* more than half elements present? */ ]] } @@ -3801,9 +3818,41 @@ end print(#t) ]], patch = [[ +--- ltable.h 2017/04/19 17:20:42 2.23.1.1 ++++ ltable.h 2018/05/24 19:31:50 +@@ -56,3 +56,3 @@ + LUAI_FUNC int luaH_next (lua_State *L, Table *t, StkId key); +-LUAI_FUNC int luaH_getn (Table *t); ++LUAI_FUNC lua_Unsigned luaH_getn (Table *t); + +--- ltable.c 2018/05/24 19:22:37 2.118.1.2 ++++ ltable.c 2018/05/24 19:25:05 +@@ -614,4 +614,4 @@ + +-static int unbound_search (Table *t, unsigned int j) { +- unsigned int i = j; /* i is zero or a present index */ ++static lua_Unsigned unbound_search (Table *t, lua_Unsigned j) { ++ lua_Unsigned i = j; /* i is zero or a present index */ + j++; +@@ -620,3 +620,3 @@ + i = j; +- if (j > cast(unsigned int, MAX_INT)/2) { /* overflow? */ ++ if (j > l_castS2U(LUA_MAXINTEGER) / 2) { /* overflow? */ + /* table was built with bad purposes: resort to linear search */ +@@ -630,3 +630,3 @@ + while (j - i > 1) { +- unsigned int m = (i+j)/2; ++ lua_Unsigned m = (i+j)/2; + if (ttisnil(luaH_getint(t, m))) j = m; +@@ -642,3 +642,3 @@ + */ +-int luaH_getn (Table *t) { ++lua_Unsigned luaH_getn (Table *t) { + unsigned int j = t->sizearray; ]] } + Bug{ what = [[Lua does not check GC when creating error messages]], report = [[Viacheslav Usov, 2017/07/06]], From b2d4d0642849319f90df48c067ba71c32d763523 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 25 May 2018 10:39:32 -0300 Subject: [PATCH 0281/1145] avoid possible overflows when checking sizes in 'string.unpack' --- lstrlib.c | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/lstrlib.c b/lstrlib.c index 54692a6eb6..b43464abcc 100644 --- a/lstrlib.c +++ b/lstrlib.c @@ -1,5 +1,5 @@ /* -** $Id: lstrlib.c,v 1.261 2018/02/21 13:48:44 roberto Exp roberto $ +** $Id: lstrlib.c,v 1.262 2018/02/21 17:48:31 roberto Exp roberto $ ** Standard library for string operations and pattern-matching ** See Copyright Notice in lua.h */ @@ -1522,17 +1522,12 @@ static int str_packsize (lua_State *L) { while (*fmt != '\0') { int size, ntoalign; KOption opt = getdetails(&h, totalsize, &fmt, &size, &ntoalign); + luaL_argcheck(L, opt != Kstring && opt != Kzstr, 1, + "variable-length format"); size += ntoalign; /* total space used by option */ luaL_argcheck(L, totalsize <= MAXSIZE - size, 1, "format result too large"); totalsize += size; - switch (opt) { - case Kstring: /* strings with length count */ - case Kzstr: /* zero-terminated string */ - luaL_argerror(L, 1, "variable-length format"); - /* call never return, but to avoid warnings: *//* FALLTHROUGH */ - default: break; - } } lua_pushinteger(L, (lua_Integer)totalsize); return 1; @@ -1585,8 +1580,8 @@ static int str_unpack (lua_State *L) { while (*fmt != '\0') { int size, ntoalign; KOption opt = getdetails(&h, pos, &fmt, &size, &ntoalign); - if ((size_t)ntoalign + size > ~pos || pos + ntoalign + size > ld) - luaL_argerror(L, 2, "data string too short"); + luaL_argcheck(L, (size_t)ntoalign + size <= ld - pos, 2, + "data string too short"); pos += ntoalign; /* skip alignment */ /* stack space for item + next position */ luaL_checkstack(L, 2, "too many results"); @@ -1615,13 +1610,15 @@ static int str_unpack (lua_State *L) { } case Kstring: { size_t len = (size_t)unpackint(L, data + pos, h.islittle, size, 0); - luaL_argcheck(L, pos + len + size <= ld, 2, "data string too short"); + luaL_argcheck(L, len <= ld - pos - size, 2, "data string too short"); lua_pushlstring(L, data + pos + size, len); pos += len; /* skip string */ break; } case Kzstr: { size_t len = (int)strlen(data + pos); + luaL_argcheck(L, pos + len < ld, 2, + "unfinished string for format 'z'"); lua_pushlstring(L, data + pos, len); pos += len + 1; /* skip string plus final '\0' */ break; From 950fbcb971f495144804c03f2a9e235dbd3e4837 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 29 May 2018 15:01:50 -0300 Subject: [PATCH 0282/1145] detail ('l_castU2S' should only be used over lua_Unsigned values) --- ltable.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ltable.c b/ltable.c index be8f67f0f9..7113f0ffb0 100644 --- a/ltable.c +++ b/ltable.c @@ -1,5 +1,5 @@ /* -** $Id: ltable.c,v 2.134 2018/02/23 13:13:31 roberto Exp roberto $ +** $Id: ltable.c,v 2.135 2018/02/26 14:16:05 roberto Exp roberto $ ** Lua tables (hash) ** See Copyright Notice in lua.h */ @@ -769,7 +769,7 @@ lua_Unsigned luaH_getn (Table *t) { return i; } else { /* 'j' is zero or present in table */ - if (isdummy(t) || isempty(luaH_getint(t, l_castU2S(j + 1)))) + if (isdummy(t) || isempty(luaH_getint(t, cast(lua_Integer, j + 1)))) return j; /* 'j + 1' is absent... */ else /* 'j + 1' is also present */ return hash_search(t, j); From 97e394ba1805fbe394a5704de660403901559e54 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 29 May 2018 15:02:51 -0300 Subject: [PATCH 0283/1145] macro 'luai_makeseed' now controls the whole process of making the seed --- lstate.c | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/lstate.c b/lstate.c index 6c92f2f0cf..5a2c357713 100644 --- a/lstate.c +++ b/lstate.c @@ -1,5 +1,5 @@ /* -** $Id: lstate.c,v 2.150 2018/01/28 15:13:26 roberto Exp roberto $ +** $Id: lstate.c,v 2.151 2018/02/05 17:11:37 roberto Exp roberto $ ** Global State ** See Copyright Notice in lua.h */ @@ -29,17 +29,6 @@ -/* -** a macro to help the creation of a unique random seed when a state is -** created; the seed is used to randomize hashes. -*/ -#if !defined(luai_makeseed) -#include -#define luai_makeseed() cast_uint(time(NULL)) -#endif - - - /* ** thread state + extra space */ @@ -63,16 +52,25 @@ typedef struct LG { /* -** Compute an initial seed as random as possible. Rely on Address Space -** Layout Randomization (if present) to increase randomness.. +** A macro to create a "random" seed when a state is created; +** the seed is used to randomize string hashes. +*/ +#if !defined(luai_makeseed) + +#include + +/* +** Compute an initial seed with some level of randomness. +** Rely on Address Space Layout Randomization (if present) and +** current time. */ #define addbuff(b,p,e) \ { size_t t = cast_sizet(e); \ memcpy(b + p, &t, sizeof(t)); p += sizeof(t); } -static unsigned int makeseed (lua_State *L) { +static unsigned int luai_makeseed (lua_State *L) { char buff[4 * sizeof(size_t)]; - unsigned int h = luai_makeseed(); + unsigned int h = cast_uint(time(NULL)); int p = 0; addbuff(buff, p, L); /* heap variable */ addbuff(buff, p, &h); /* local variable */ @@ -82,6 +80,8 @@ static unsigned int makeseed (lua_State *L) { return luaS_hash(buff, p, h); } +#endif + /* ** set GCdebt to a new value keeping the value (totalbytes + GCdebt) @@ -327,7 +327,7 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { g->frealloc = f; g->ud = ud; g->mainthread = L; - g->seed = makeseed(L); + g->seed = luai_makeseed(L); g->gcrunning = 0; /* no GC while building state */ g->strt.size = g->strt.nuse = 0; g->strt.hash = NULL; From 34aa0c5bd7493b6e01983df28f04af46a3d99967 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 30 May 2018 11:25:52 -0300 Subject: [PATCH 0284/1145] new macros 'likely'/'unlikely' with hints for jump predictions (used only in errors for now) --- ldo.c | 18 +++++++++--------- llimits.h | 21 ++++++++++++++++++++- lmem.c | 44 +++++++++++++++++++++++++++----------------- lstring.c | 10 +++++----- ltable.c | 11 ++++++----- lvm.c | 22 +++++++++++----------- 6 files changed, 78 insertions(+), 48 deletions(-) diff --git a/ldo.c b/ldo.c index 169eb71453..fb077211fe 100644 --- a/ldo.c +++ b/ldo.c @@ -1,5 +1,5 @@ /* -** $Id: ldo.c,v 2.200 2018/03/16 15:33:34 roberto Exp roberto $ +** $Id: ldo.c,v 2.201 2018/05/22 12:02:36 roberto Exp roberto $ ** Stack and Call structure of Lua ** See Copyright Notice in lua.h */ @@ -182,7 +182,7 @@ int luaD_reallocstack (lua_State *L, int newsize, int raiseerror) { StkId newstack = luaM_reallocvector(L, L->stack, lim, newsize, StackValue); lua_assert(newsize <= LUAI_MAXSTACK || newsize == ERRORSTACKSIZE); lua_assert(L->stack_last - L->stack == L->stacksize - EXTRA_STACK); - if (newstack == NULL) { /* reallocation failed? */ + if (unlikely(newstack == NULL)) { /* reallocation failed? */ if (raiseerror) luaM_error(L); else return 0; /* do not raise an error */ @@ -204,7 +204,7 @@ int luaD_reallocstack (lua_State *L, int newsize, int raiseerror) { int luaD_growstack (lua_State *L, int n, int raiseerror) { int size = L->stacksize; int newsize = 2 * size; /* tentative new size */ - if (size > LUAI_MAXSTACK) { /* need more space after extra size? */ + if (unlikely(size > LUAI_MAXSTACK)) { /* need more space after extra size? */ if (raiseerror) luaD_throw(L, LUA_ERRERR); /* error inside message handler */ else return 0; @@ -215,7 +215,7 @@ int luaD_growstack (lua_State *L, int n, int raiseerror) { newsize = LUAI_MAXSTACK; if (newsize < needed) /* but must respect what was asked for */ newsize = needed; - if (newsize > LUAI_MAXSTACK) { /* stack overflow? */ + if (unlikely(newsize > LUAI_MAXSTACK)) { /* stack overflow? */ /* add extra size to be able to handle the error message */ luaD_reallocstack(L, ERRORSTACKSIZE, raiseerror); if (raiseerror) @@ -350,7 +350,7 @@ static StkId rethook (lua_State *L, CallInfo *ci, StkId firstres, int nres) { void luaD_tryfuncTM (lua_State *L, StkId func) { const TValue *tm = luaT_gettmbyobj(L, s2v(func), TM_CALL); StkId p; - if (!ttisfunction(tm)) + if (unlikely(!ttisfunction(tm))) luaG_typeerror(L, s2v(func), "call"); for (p = L->top; p > func; p--) setobjs2s(L, p, p-1); @@ -660,14 +660,14 @@ LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs, L->nny = 0; /* allow yields */ api_checknelems(L, (L->status == LUA_OK) ? nargs + 1 : nargs); status = luaD_rawrunprotected(L, resume, &nargs); - if (status == -1) /* error calling 'lua_resume'? */ + if (unlikely(status == -1)) /* error calling 'lua_resume'? */ status = LUA_ERRRUN; else { /* continue running after recoverable errors */ while (errorstatus(status) && recover(L, status)) { /* unroll continuation */ status = luaD_rawrunprotected(L, unroll, &status); } - if (errorstatus(status)) { /* unrecoverable error? */ + if (unlikely(errorstatus(status))) { /* unrecoverable error? */ L->status = cast_byte(status); /* mark thread as 'dead' */ seterrorobj(L, status, L->top); /* push error message */ L->ci->top = L->top; @@ -694,7 +694,7 @@ LUA_API int lua_yieldk (lua_State *L, int nresults, lua_KContext ctx, luai_userstateyield(L, nresults); lua_lock(L); api_checknelems(L, nresults); - if (L->nny > 0) { + if (unlikely(L->nny > 0)) { if (L != G(L)->mainthread) luaG_runerror(L, "attempt to yield across a C-call boundary"); else @@ -727,7 +727,7 @@ int luaD_pcall (lua_State *L, Pfunc func, void *u, ptrdiff_t old_errfunc = L->errfunc; L->errfunc = ef; status = luaD_rawrunprotected(L, func, u); - if (status != LUA_OK) { /* an error occurred? */ + if (unlikely(status != LUA_OK)) { /* an error occurred? */ StkId oldtop = restorestack(L, old_top); luaF_close(L, oldtop); /* close possible pending closures */ seterrorobj(L, status, oldtop); diff --git a/llimits.h b/llimits.h index 9a3ae8d051..725d7c8b89 100644 --- a/llimits.h +++ b/llimits.h @@ -1,5 +1,5 @@ /* -** $Id: llimits.h,v 1.148 2017/12/28 11:51:00 roberto Exp roberto $ +** $Id: llimits.h,v 1.149 2018/01/28 15:13:26 roberto Exp roberto $ ** Limits, basic types, and some other 'installation-dependent' definitions ** See Copyright Notice in lua.h */ @@ -130,9 +130,27 @@ typedef LUAI_UACINT l_uacInt; #endif +/* +** macros to improve jump prediction (used mainly for error handling) +*/ +#if !defined(likely) + +#if defined(__GNUC__) +#define likely(x) (__builtin_expect(((x) != 0), 1)) +#define unlikely(x) (__builtin_expect(((x) != 0), 0)) +#else +#define likely(x) (x) +#define unlikely(x) (x) +#endif + +#endif + + /* ** non-return type */ +#if !defined(l_noret) + #if defined(__GNUC__) #define l_noret void __attribute__((noreturn)) #elif defined(_MSC_VER) && _MSC_VER >= 1200 @@ -141,6 +159,7 @@ typedef LUAI_UACINT l_uacInt; #define l_noret void #endif +#endif /* diff --git a/lmem.c b/lmem.c index 2c1757f528..5c73acd5ca 100644 --- a/lmem.c +++ b/lmem.c @@ -1,5 +1,5 @@ /* -** $Id: lmem.c,v 1.95 2017/12/11 12:27:48 roberto Exp roberto $ +** $Id: lmem.c,v 1.96 2018/01/28 15:13:26 roberto Exp roberto $ ** Interface to Memory Manager ** See Copyright Notice in lua.h */ @@ -60,7 +60,7 @@ void *luaM_growaux_ (lua_State *L, void *block, int nelems, int *psize, if (nelems + 1 <= size) /* does one extra element still fit? */ return block; /* nothing to be done */ if (size >= limit / 2) { /* cannot double it? */ - if (size >= limit) /* cannot grow even a little? */ + if (unlikely(size >= limit)) /* cannot grow even a little? */ luaG_runerror(L, "too many %s (limit is %d)", what, limit); size = limit; /* still have at least one free place */ } @@ -73,7 +73,7 @@ void *luaM_growaux_ (lua_State *L, void *block, int nelems, int *psize, /* 'limit' ensures that multiplication will not overflow */ newblock = luaM_realloc_(L, block, cast_sizet(*psize) * size_elems, cast_sizet(size) * size_elems); - if (newblock == NULL) + if (unlikely(newblock == NULL)) luaM_error(L); *psize = size; /* update only when everything else is OK */ return newblock; @@ -88,7 +88,7 @@ void *luaM_shrinkvector_ (lua_State *L, void *block, int *size, size_t newsize = cast_sizet(final_n * size_elem); lua_assert(newsize <= oldsize); newblock = (*g->frealloc)(g->ud, block, oldsize, newsize); - if (newblock == NULL && final_n > 0) /* allocation failed? */ + if (unlikely(newblock == NULL && final_n > 0)) /* allocation failed? */ luaM_error(L); else { g->GCdebt += newsize - oldsize; @@ -114,6 +114,22 @@ void luaM_free_ (lua_State *L, void *block, size_t osize) { } +/* +** In case of allocation fail, this function will call the GC to try +** to free some memory and then try the allocation again. +** (It should not be called when shrinking a block, because then the +** interpreter may be in the middle of a collection step.) +*/ +static void *tryagain (lua_State *L, void *block, + size_t osize, size_t nsize) { + global_State *g = G(L); + if (g->version) { /* is state fully build? */ + luaC_fullgc(L, 1); /* try to free some memory... */ + return (*g->frealloc)(g->ud, block, osize, nsize); /* try again */ + } + else return NULL; /* cannot free any memory without a full state */ +} + /* ** generic allocation routine. @@ -124,13 +140,10 @@ void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) { lua_assert((osize == 0) == (block == NULL)); hardtest(L, osize, nsize); newblock = (*g->frealloc)(g->ud, block, osize, nsize); - if (newblock == NULL && nsize > 0) { - /* Is state fully built? Not shrinking a block? */ - if (g->version && nsize > osize) { - luaC_fullgc(L, 1); /* try to free some memory... */ - newblock = (*g->frealloc)(g->ud, block, osize, nsize); /* try again */ - } - if (newblock == NULL) + if (unlikely(newblock == NULL && nsize > 0)) { + if (nsize > osize) /* not shrinking a block? */ + newblock = tryagain(L, block, osize, nsize); + if (newblock == NULL) /* still no memory? */ return NULL; } lua_assert((nsize == 0) == (newblock == NULL)); @@ -142,7 +155,7 @@ void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) { void *luaM_saferealloc_ (lua_State *L, void *block, size_t osize, size_t nsize) { void *newblock = luaM_realloc_(L, block, osize, nsize); - if (newblock == NULL && nsize > 0) /* allocation failed? */ + if (unlikely(newblock == NULL && nsize > 0)) /* allocation failed? */ luaM_error(L); return newblock; } @@ -155,11 +168,8 @@ void *luaM_malloc_ (lua_State *L, size_t size, int tag) { else { global_State *g = G(L); void *newblock = (*g->frealloc)(g->ud, NULL, tag, size); - if (newblock == NULL) { - if (g->version) { /* is state fully built? */ - luaC_fullgc(L, 1); /* try to free some memory... */ - newblock = (*g->frealloc)(g->ud, NULL, tag, size); /* try again */ - } + if (unlikely(newblock == NULL)) { + newblock = tryagain(L, NULL, tag, size); if (newblock == NULL) luaM_error(L); } diff --git a/lstring.c b/lstring.c index 29a082129f..f1e5d82b16 100644 --- a/lstring.c +++ b/lstring.c @@ -1,5 +1,5 @@ /* -** $Id: lstring.c,v 2.64 2018/02/15 18:06:24 roberto Exp roberto $ +** $Id: lstring.c,v 2.65 2018/02/20 16:52:50 roberto Exp roberto $ ** String table (keeps all strings handled by Lua) ** See Copyright Notice in lua.h */ @@ -99,7 +99,7 @@ void luaS_resize (lua_State *L, int nsize) { if (nsize < osize) /* shrinking table? */ tablerehash(tb->hash, osize, nsize); /* depopulate shrinking part */ newvect = luaM_reallocvector(L, tb->hash, osize, nsize, TString*); - if (newvect == NULL) { /* reallocation failed? */ + if (unlikely(newvect == NULL)) { /* reallocation failed? */ if (nsize < osize) /* was it shrinking table? */ tablerehash(tb->hash, nsize, osize); /* restore to original size */ /* leave table as it was */ @@ -182,7 +182,7 @@ void luaS_remove (lua_State *L, TString *ts) { static void growstrtab (lua_State *L, stringtable *tb) { - if (tb->nuse == MAX_INT) { /* too many strings? */ + if (unlikely(tb->nuse == MAX_INT)) { /* too many strings? */ luaC_fullgc(L, 1); /* try to free some... */ if (tb->nuse == MAX_INT) /* still too many? */ luaM_error(L); /* cannot even create a message... */ @@ -233,7 +233,7 @@ TString *luaS_newlstr (lua_State *L, const char *str, size_t l) { return internshrstr(L, str, l); else { TString *ts; - if (l >= (MAX_SIZE - sizeof(TString))/sizeof(char)) + if (unlikely(l >= (MAX_SIZE - sizeof(TString))/sizeof(char))) luaM_toobig(L); ts = luaS_createlngstrobj(L, l); memcpy(getstr(ts), str, l * sizeof(char)); @@ -269,7 +269,7 @@ Udata *luaS_newudata (lua_State *L, size_t s, int nuvalue) { Udata *u; int i; GCObject *o; - if (s > MAX_SIZE - udatamemoffset(nuvalue)) + if (unlikely(s > MAX_SIZE - udatamemoffset(nuvalue))) luaM_toobig(L); o = luaC_newobj(L, LUA_TUSERDATA, sizeudata(nuvalue, s)); u = gco2u(o); diff --git a/ltable.c b/ltable.c index 7113f0ffb0..56fe64fd9c 100644 --- a/ltable.c +++ b/ltable.c @@ -1,5 +1,5 @@ /* -** $Id: ltable.c,v 2.135 2018/02/26 14:16:05 roberto Exp roberto $ +** $Id: ltable.c,v 2.136 2018/05/29 18:01:50 roberto Exp roberto $ ** Lua tables (hash) ** See Copyright Notice in lua.h */ @@ -235,7 +235,7 @@ static unsigned int findindex (lua_State *L, Table *t, TValue *key) { return i; /* yes; that's the index */ else { const TValue *n = getgeneric(t, key); - if (n == luaH_emptyobject) + if (unlikely(n == luaH_emptyobject)) luaG_runerror(L, "invalid key to 'next'"); /* key not found */ i = cast_int(nodefromval(n) - gnode(t, 0)); /* key index in hash table */ /* hash elements are numbered after array ones */ @@ -467,7 +467,7 @@ void luaH_resize (lua_State *L, Table *t, unsigned int newasize, } /* allocate new array */ newarray = luaM_reallocvector(L, t->array, oldasize, newasize, TValue); - if (newarray == NULL && newasize > 0) { /* allocation failed? */ + if (unlikely(newarray == NULL && newasize > 0)) { /* allocation failed? */ freehash(L, &newt); /* release new hash part */ luaM_error(L); /* raise error (with array unchanged) */ } @@ -560,7 +560,8 @@ static Node *getfreepos (Table *t) { TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) { Node *mp; TValue aux; - if (ttisnil(key)) luaG_runerror(L, "table index is nil"); + if (unlikely(ttisnil(key))) + luaG_runerror(L, "table index is nil"); else if (ttisfloat(key)) { lua_Number f = fltvalue(key); lua_Integer k; @@ -568,7 +569,7 @@ TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) { setivalue(&aux, k); key = &aux; /* insert it as an integer */ } - else if (luai_numisnan(f)) + else if (unlikely(luai_numisnan(f))) luaG_runerror(L, "table index is NaN"); } mp = mainpositionTV(t, key); diff --git a/lvm.c b/lvm.c index 4406afb64d..36c7699f26 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.354 2018/05/02 18:17:59 roberto Exp roberto $ +** $Id: lvm.c,v 2.355 2018/05/22 12:02:36 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -196,7 +196,7 @@ void luaV_finishget (lua_State *L, const TValue *t, TValue *key, StkId val, if (slot == NULL) { /* 't' is not a table? */ lua_assert(!ttistable(t)); tm = luaT_gettmbyobj(L, t, TM_INDEX); - if (notm(tm)) + if (unlikely(notm(tm))) luaG_typeerror(L, t, "index"); /* no metamethod */ /* else will try the metamethod */ } @@ -253,7 +253,7 @@ void luaV_finishset (lua_State *L, const TValue *t, TValue *key, } else { /* not a table; check metamethod */ tm = luaT_gettmbyobj(L, t, TM_NEWINDEX); - if (notm(tm)) + if (unlikely(notm(tm))) luaG_typeerror(L, t, "index"); } /* try the metamethod */ @@ -561,7 +561,7 @@ void luaV_concat (lua_State *L, int total) { /* collect total length and number of strings */ for (n = 1; n < total && tostring(L, s2v(top - n - 1)); n++) { size_t l = vslen(s2v(top - n - 1)); - if (l >= (MAX_SIZE/sizeof(char)) - tl) + if (unlikely(l >= (MAX_SIZE/sizeof(char)) - tl)) luaG_runerror(L, "string length overflow"); tl += l; } @@ -605,7 +605,7 @@ void luaV_objlen (lua_State *L, StkId ra, const TValue *rb) { } default: { /* try metamethod */ tm = luaT_gettmbyobj(L, rb, TM_LEN); - if (notm(tm)) /* no metamethod? */ + if (unlikely(notm(tm))) /* no metamethod? */ luaG_typeerror(L, rb, "get length of"); break; } @@ -622,7 +622,7 @@ void luaV_objlen (lua_State *L, StkId ra, const TValue *rb) { */ lua_Integer luaV_div (lua_State *L, lua_Integer m, lua_Integer n) { if (l_castS2U(n) + 1u <= 1u) { /* special cases: -1 or 0 */ - if (n == 0) + if (unlikely(n == 0)) luaG_runerror(L, "attempt to divide by zero"); return intop(-, 0, m); /* n==-1; avoid overflow with 0x80000...//-1 */ } @@ -642,7 +642,7 @@ lua_Integer luaV_div (lua_State *L, lua_Integer m, lua_Integer n) { */ lua_Integer luaV_mod (lua_State *L, lua_Integer m, lua_Integer n) { if (l_castS2U(n) + 1u <= 1u) { /* special cases: -1 or 0 */ - if (n == 0) + if (unlikely(n == 0)) luaG_runerror(L, "attempt to perform 'n%%0'"); return 0; /* m % -1 == 0; avoid overflow with 0x80000...%-1 */ } @@ -1665,7 +1665,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { TValue *plimit = s2v(ra + 1); lua_Integer ilimit, initv; int stopnow; - if (!forlimit(plimit, &ilimit, 1, &stopnow)) { + if (unlikely(!forlimit(plimit, &ilimit, 1, &stopnow))) { savestate(L, ci); /* for the error message */ luaG_runerror(L, "'for' limit must be a number"); } @@ -1717,13 +1717,13 @@ void luaV_execute (lua_State *L, CallInfo *ci) { else { /* try making all values floats */ lua_Number ninit; lua_Number nlimit; lua_Number nstep; savestate(L, ci); /* in case of errors */ - if (!tonumber(plimit, &nlimit)) + if (unlikely(!tonumber(plimit, &nlimit))) luaG_runerror(L, "'for' limit must be a number"); setfltvalue(plimit, nlimit); - if (!tonumber(pstep, &nstep)) + if (unlikely(!tonumber(pstep, &nstep))) luaG_runerror(L, "'for' step must be a number"); setfltvalue(pstep, nstep); - if (!tonumber(init, &ninit)) + if (unlikely(!tonumber(init, &ninit))) luaG_runerror(L, "'for' initial value must be a number"); setfltvalue(init, luai_numsub(L, ninit, nstep)); } From b3970649550fe8471c55bfae57aa3752ddfa97a9 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 1 Jun 2018 13:45:58 -0300 Subject: [PATCH 0285/1145] avoid craches when loading tampered code with NULL as a string constant --- lundump.c | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/lundump.c b/lundump.c index 25ab102dab..64b643623a 100644 --- a/lundump.c +++ b/lundump.c @@ -1,5 +1,5 @@ /* -** $Id: lundump.c,v 2.48 2017/11/28 11:19:07 roberto Exp roberto $ +** $Id: lundump.c,v 2.49 2017/12/07 18:59:52 roberto Exp roberto $ ** load precompiled Lua chunks ** See Copyright Notice in lua.h */ @@ -36,7 +36,7 @@ typedef struct { } LoadState; -static l_noret error(LoadState *S, const char *why) { +static l_noret error (LoadState *S, const char *why) { luaO_pushfstring(S->L, "%s: %s precompiled chunk", S->name, why); luaD_throw(S->L, LUA_ERRSYNTAX); } @@ -95,7 +95,10 @@ static lua_Integer LoadInteger (LoadState *S) { } -static TString *LoadString (LoadState *S) { +/* +** Load a nullable string +*/ +static TString *LoadStringN (LoadState *S) { size_t size = LoadSize(S); if (size == 0) return NULL; @@ -112,6 +115,17 @@ static TString *LoadString (LoadState *S) { } +/* +** Load a non-nullable string. +*/ +static TString *LoadString (LoadState *S) { + TString *st = LoadStringN(S); + if (st == NULL) + error(S, "bad format for constant string"); + return st; +} + + static void LoadCode (LoadState *S, Proto *f) { int n = LoadInt(S); f->code = luaM_newvectorchecked(S->L, n, Instruction); @@ -203,18 +217,18 @@ static void LoadDebug (LoadState *S, Proto *f) { for (i = 0; i < n; i++) f->locvars[i].varname = NULL; for (i = 0; i < n; i++) { - f->locvars[i].varname = LoadString(S); + f->locvars[i].varname = LoadStringN(S); f->locvars[i].startpc = LoadInt(S); f->locvars[i].endpc = LoadInt(S); } n = LoadInt(S); for (i = 0; i < n; i++) - f->upvalues[i].name = LoadString(S); + f->upvalues[i].name = LoadStringN(S); } static void LoadFunction (LoadState *S, Proto *f, TString *psource) { - f->source = LoadString(S); + f->source = LoadStringN(S); if (f->source == NULL) /* no source in dump? */ f->source = psource; /* reuse parent's source */ f->linedefined = LoadInt(S); From fb8fa661366e15e98c60d8929feaab9e551a02f9 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 1 Jun 2018 13:51:34 -0300 Subject: [PATCH 0286/1145] no more 'luaH_emptyobject' and comparisons of addresses of global variables (instead, use a different kind of nil to signal the fake entry returned when a key is not found in a table) --- lobject.h | 36 ++++++++++++++++++++++++++++-------- ltable.c | 19 ++++++++++--------- ltable.h | 7 +------ ltm.h | 4 ++-- lvm.c | 10 +++++----- 5 files changed, 46 insertions(+), 30 deletions(-) diff --git a/lobject.h b/lobject.h index dc5f32a1df..a7b4318fc2 100644 --- a/lobject.h +++ b/lobject.h @@ -1,5 +1,5 @@ /* -** $Id: lobject.h,v 2.141 2018/02/26 14:16:05 roberto Exp roberto $ +** $Id: lobject.h,v 2.142 2018/04/04 14:23:41 roberto Exp roberto $ ** Type definitions for Lua objects ** See Copyright Notice in lua.h */ @@ -137,7 +137,12 @@ typedef StackValue *StkId; /* index to stack elements */ ** =================================================================== */ -#define ttisnil(o) checktag((o), LUA_TNIL) +/* macro to test for (any kind of) nil */ +#define ttisnil(v) checktype((v), LUA_TNIL) + +/* macro to test for a "pure" nil */ +#define ttisstrictnil(o) checktag((o), LUA_TNIL) + /* macro defining a nil value */ #define NILCONSTANT {NULL}, LUA_TNIL @@ -155,17 +160,32 @@ typedef StackValue *StkId; /* index to stack elements */ */ #define LUA_TEMPTY (LUA_TNIL | (1 << 4)) -#define ttisnilorempty(v) checktype((v), LUA_TNIL) +/* +** Variant used only in the value returned for a key not found in a +** table (absent key). +*/ +#define LUA_TABSTKEY (LUA_TNIL | (2 << 4)) + -#define isreallyempty(v) checktag((v), LUA_TEMPTY) +#define isabstkey(v) checktag((v), LUA_TABSTKEY) -/* By default, entries with any kind of nil are considered empty */ -#define isempty(v) ttisnilorempty(v) +/* +** macro to detect non-standard nils (used only in assertions) +*/ +#define isreallyempty(v) (ttisnil(v) && !ttisstrictnil(v)) + + +/* +** By default, entries with any kind of nil are considered empty. +** (In any definition, values associated with absent keys must also +** be accepted as empty.) +*/ +#define isempty(v) ttisnil(v) -/* macro defining an empty value */ -#define EMPTYCONSTANT {NULL}, LUA_TEMPTY +/* macro defining a value corresponding to an absent key */ +#define ABSTKEYCONSTANT {NULL}, LUA_TABSTKEY /* mark an entry as empty */ diff --git a/ltable.c b/ltable.c index 56fe64fd9c..cc98f45663 100644 --- a/ltable.c +++ b/ltable.c @@ -1,5 +1,5 @@ /* -** $Id: ltable.c,v 2.136 2018/05/29 18:01:50 roberto Exp roberto $ +** $Id: ltable.c,v 2.137 2018/05/30 14:25:52 roberto Exp roberto $ ** Lua tables (hash) ** See Copyright Notice in lua.h */ @@ -93,7 +93,8 @@ static const Node dummynode_ = { }; -LUAI_DDEF const TValue luaH_emptyobject_ = {EMPTYCONSTANT}; +static const TValue absentkey = {ABSTKEYCONSTANT}; + /* @@ -203,7 +204,7 @@ static const TValue *getgeneric (Table *t, const TValue *key) { else { int nx = gnext(n); if (nx == 0) - return luaH_emptyobject; /* not found */ + return &absentkey; /* not found */ n += nx; } } @@ -235,7 +236,7 @@ static unsigned int findindex (lua_State *L, Table *t, TValue *key) { return i; /* yes; that's the index */ else { const TValue *n = getgeneric(t, key); - if (unlikely(n == luaH_emptyobject)) + if (unlikely(isabstkey(n))) luaG_runerror(L, "invalid key to 'next'"); /* key not found */ i = cast_int(nodefromval(n) - gnode(t, 0)); /* key index in hash table */ /* hash elements are numbered after array ones */ @@ -629,7 +630,7 @@ const TValue *luaH_getint (Table *t, lua_Integer key) { n += nx; } } - return luaH_emptyobject; + return &absentkey; } } @@ -646,7 +647,7 @@ const TValue *luaH_getshortstr (Table *t, TString *key) { else { int nx = gnext(n); if (nx == 0) - return luaH_emptyobject; /* not found */ + return &absentkey; /* not found */ n += nx; } } @@ -671,7 +672,7 @@ const TValue *luaH_get (Table *t, const TValue *key) { switch (ttypetag(key)) { case LUA_TSHRSTR: return luaH_getshortstr(t, tsvalue(key)); case LUA_TNUMINT: return luaH_getint(t, ivalue(key)); - case LUA_TNIL: return luaH_emptyobject; + case LUA_TNIL: return &absentkey; case LUA_TNUMFLT: { lua_Integer k; if (luaV_flttointeger(fltvalue(key), &k, 0)) /* index is an integral? */ @@ -690,7 +691,7 @@ const TValue *luaH_get (Table *t, const TValue *key) { */ TValue *luaH_set (lua_State *L, Table *t, const TValue *key) { const TValue *p = luaH_get(t, key); - if (p != luaH_emptyobject) + if (!isabstkey(p)) return cast(TValue *, p); else return luaH_newkey(L, t, key); } @@ -699,7 +700,7 @@ TValue *luaH_set (lua_State *L, Table *t, const TValue *key) { void luaH_setint (lua_State *L, Table *t, lua_Integer key, TValue *value) { const TValue *p = luaH_getint(t, key); TValue *cell; - if (p != luaH_emptyobject) + if (!isabstkey(p)) cell = cast(TValue *, p); else { TValue k; diff --git a/ltable.h b/ltable.h index bcf92984ab..9b1a346492 100644 --- a/ltable.h +++ b/ltable.h @@ -1,5 +1,5 @@ /* -** $Id: ltable.h,v 2.25 2017/06/09 16:48:44 roberto Exp roberto $ +** $Id: ltable.h,v 2.26 2018/02/23 13:13:31 roberto Exp roberto $ ** Lua tables (hash) ** See Copyright Notice in lua.h */ @@ -21,8 +21,6 @@ /* true when 't' is using 'dummynode' as its hash part */ #define isdummy(t) ((t)->lastfree == NULL) -#define luaH_emptyobject (&luaH_emptyobject_) - /* allocated size for hash nodes */ #define allocsizenode(t) (isdummy(t) ? 0 : sizenode(t)) @@ -32,9 +30,6 @@ #define nodefromval(v) cast(Node *, (v)) -LUAI_DDEC const TValue luaH_emptyobject_; - - LUAI_FUNC const TValue *luaH_getint (Table *t, lua_Integer key); LUAI_FUNC void luaH_setint (lua_State *L, Table *t, lua_Integer key, TValue *value); diff --git a/ltm.h b/ltm.h index 24b9a84e9a..38b6e1b214 100644 --- a/ltm.h +++ b/ltm.h @@ -1,5 +1,5 @@ /* -** $Id: ltm.h,v 2.35 2018/04/04 14:23:41 roberto Exp roberto $ +** $Id: ltm.h,v 2.36 2018/05/23 14:41:20 roberto Exp roberto $ ** Tag methods ** See Copyright Notice in lua.h */ @@ -48,7 +48,7 @@ typedef enum { ** Test whether there is no tagmethod. ** (Because tagmethods use raw accesses, the result may be an "empty" nil.) */ -#define notm(tm) ttisnilorempty(tm) +#define notm(tm) ttisnil(tm) #define gfasttm(g,et,e) ((et) == NULL ? NULL : \ diff --git a/lvm.c b/lvm.c index 36c7699f26..3fed2a2762 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.355 2018/05/22 12:02:36 roberto Exp roberto $ +** $Id: lvm.c,v 2.356 2018/05/30 14:25:52 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -227,9 +227,9 @@ void luaV_finishget (lua_State *L, const TValue *t, TValue *key, StkId val, /* ** Finish a table assignment 't[key] = val'. ** If 'slot' is NULL, 't' is not a table. Otherwise, 'slot' points -** to the entry 't[key]', or to 'luaH_emptyobject' if there is no such -** entry. (The value at 'slot' must be empty, otherwise 'luaV_fastget' -** would have done the job.) +** to the entry 't[key]', or to a value with an absent key if there +** is no such entry. (The value at 'slot' must be empty, otherwise +** 'luaV_fastget' would have done the job.) */ void luaV_finishset (lua_State *L, const TValue *t, TValue *key, TValue *val, const TValue *slot) { @@ -241,7 +241,7 @@ void luaV_finishset (lua_State *L, const TValue *t, TValue *key, lua_assert(isempty(slot)); /* slot must be empty */ tm = fasttm(L, h->metatable, TM_NEWINDEX); /* get metamethod */ if (tm == NULL) { /* no metamethod? */ - if (slot == luaH_emptyobject) /* no previous entry? */ + if (isabstkey(slot)) /* no previous entry? */ slot = luaH_newkey(L, h, key); /* create one */ /* no metamethod and (now) there is an entry with given key */ setobj2t(L, cast(TValue *, slot), val); /* set its new value */ From 505fc9122255b8a2ef761720bca21fd5e9be8e73 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 1 Jun 2018 14:40:38 -0300 Subject: [PATCH 0287/1145] no more 'luaO_nilobject' to avoid comparison of global variable addresses (now uses static variables) --- lapi.c | 8 +++++--- lobject.c | 6 +----- lobject.h | 8 +------- lstate.c | 5 ++--- ltm.c | 5 +++-- 5 files changed, 12 insertions(+), 20 deletions(-) diff --git a/lapi.c b/lapi.c index 877fb2ac51..88552899ef 100644 --- a/lapi.c +++ b/lapi.c @@ -1,5 +1,5 @@ /* -** $Id: lapi.c,v 2.290 2018/02/27 20:01:55 roberto Exp roberto $ +** $Id: lapi.c,v 2.291 2018/04/04 14:23:41 roberto Exp roberto $ ** Lua API ** See Copyright Notice in lua.h */ @@ -38,10 +38,12 @@ const char lua_ident[] = /* value at a non-valid index */ -#define NONVALIDVALUE cast(TValue *, luaO_nilobject) + +static const TValue nonvalidvaluep = {NILCONSTANT}; +#define NONVALIDVALUE cast(TValue *, &nonvalidvaluep) /* corresponding test */ -#define isvalid(o) ((o) != luaO_nilobject) +#define isvalid(o) ((o) != &nonvalidvaluep) /* test for pseudo index */ #define ispseudo(i) ((i) <= LUA_REGISTRYINDEX) diff --git a/lobject.c b/lobject.c index 43dc1a250c..8b8de37c37 100644 --- a/lobject.c +++ b/lobject.c @@ -1,5 +1,5 @@ /* -** $Id: lobject.c,v 2.124 2018/02/27 18:47:32 roberto Exp roberto $ +** $Id: lobject.c,v 2.125 2018/04/25 16:26:20 roberto Exp roberto $ ** Some generic functions over Lua objects ** See Copyright Notice in lua.h */ @@ -29,10 +29,6 @@ #include "lvm.h" - -LUAI_DDEF const TValue luaO_nilobject_ = {NILCONSTANT}; - - /* ** converts an integer to a "floating point byte", represented as ** (eeeeexxx), where the real value is (1xxx) * 2^(eeeee - 1) if diff --git a/lobject.h b/lobject.h index a7b4318fc2..da89666291 100644 --- a/lobject.h +++ b/lobject.h @@ -1,5 +1,5 @@ /* -** $Id: lobject.h,v 2.142 2018/04/04 14:23:41 roberto Exp roberto $ +** $Id: lobject.h,v 2.143 2018/06/01 16:51:34 roberto Exp roberto $ ** Type definitions for Lua objects ** See Copyright Notice in lua.h */ @@ -150,10 +150,6 @@ typedef StackValue *StkId; /* index to stack elements */ #define setnilvalue(obj) settt_(obj, LUA_TNIL) -/* (address of) a fixed nil value */ -#define luaO_nilobject (&luaO_nilobject_) - - /* ** Variant tag, used only in tables to signal an empty slot ** (which might be different from a slot containing nil) @@ -730,8 +726,6 @@ typedef struct Table { #define sizenode(t) (twoto((t)->lsizenode)) -LUAI_DDEC const TValue luaO_nilobject_; - /* size of buffer for 'luaO_utf8esc' function */ #define UTF8BUFFSZ 8 diff --git a/lstate.c b/lstate.c index 5a2c357713..2622a1d922 100644 --- a/lstate.c +++ b/lstate.c @@ -1,5 +1,5 @@ /* -** $Id: lstate.c,v 2.151 2018/02/05 17:11:37 roberto Exp roberto $ +** $Id: lstate.c,v 2.152 2018/05/29 18:02:51 roberto Exp roberto $ ** Global State ** See Copyright Notice in lua.h */ @@ -69,12 +69,11 @@ typedef struct LG { memcpy(b + p, &t, sizeof(t)); p += sizeof(t); } static unsigned int luai_makeseed (lua_State *L) { - char buff[4 * sizeof(size_t)]; + char buff[3 * sizeof(size_t)]; unsigned int h = cast_uint(time(NULL)); int p = 0; addbuff(buff, p, L); /* heap variable */ addbuff(buff, p, &h); /* local variable */ - addbuff(buff, p, luaO_nilobject); /* global variable */ addbuff(buff, p, &lua_newstate); /* public function */ lua_assert(p == sizeof(buff)); return luaS_hash(buff, p, h); diff --git a/ltm.c b/ltm.c index 6d285510aa..f0784305b1 100644 --- a/ltm.c +++ b/ltm.c @@ -1,5 +1,5 @@ /* -** $Id: ltm.c,v 2.66 2018/02/27 17:48:28 roberto Exp roberto $ +** $Id: ltm.c,v 2.67 2018/04/04 14:23:41 roberto Exp roberto $ ** Tag methods ** See Copyright Notice in lua.h */ @@ -69,6 +69,7 @@ const TValue *luaT_gettm (Table *events, TMS event, TString *ename) { const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, TMS event) { + static const TValue nilobject = {NILCONSTANT}; Table *mt; switch (ttype(o)) { case LUA_TTABLE: @@ -80,7 +81,7 @@ const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, TMS event) { default: mt = G(L)->mt[ttype(o)]; } - return (mt ? luaH_getshortstr(mt, G(L)->tmname[event]) : luaO_nilobject); + return (mt ? luaH_getshortstr(mt, G(L)->tmname[event]) : &nilobject); } From c5dc521d654cbe9b04310a633e94dbcf1927e0f6 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 8 Jun 2018 13:23:18 -0300 Subject: [PATCH 0288/1145] added patch for bug 5.3.4-7 --- bugs | 42 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/bugs b/bugs index d87f12d318..d7a717c38f 100644 --- a/bugs +++ b/bugs @@ -3680,9 +3680,9 @@ It needs an "interceptor" 'memcmp' function that continues reading memory after a difference is found.]], patch = [[ 2c2 -< ** $Id: bugs,v 1.159 2017/12/13 18:35:03 roberto Exp roberto $ +< ** $Id: bugs,v 1.160 2018/05/24 20:25:14 roberto Exp roberto $ --- -> ** $Id: bugs,v 1.159 2017/12/13 18:35:03 roberto Exp roberto $ +> ** $Id: bugs,v 1.160 2018/05/24 20:25:14 roberto Exp roberto $ 263c263,264 < for (option = LUA_STRFTIMEOPTIONS; *option != '\0'; option += oplen) { --- @@ -3974,6 +3974,44 @@ pcall(rawset, a, 2, 20) -- forces a rehash for k,v in pairs(a) do print(k,v) end ]], patch = [[ +--- ltable.c 2018/05/24 19:39:05 2.118.1.3 ++++ ltable.c 2018/06/04 16:00:25 +@@ -332,17 +332,34 @@ + } + + ++typedef struct { ++ Table *t; ++ unsigned int nhsize; ++} AuxsetnodeT; ++ ++ ++static void auxsetnode (lua_State *L, void *ud) { ++ AuxsetnodeT *asn = cast(AuxsetnodeT *, ud); ++ setnodevector(L, asn->t, asn->nhsize); ++} ++ ++ + void luaH_resize (lua_State *L, Table *t, unsigned int nasize, + unsigned int nhsize) { + unsigned int i; + int j; ++ AuxsetnodeT asn; + unsigned int oldasize = t->sizearray; + int oldhsize = allocsizenode(t); + Node *nold = t->node; /* save old hash ... */ + if (nasize > oldasize) /* array part must grow? */ + setarrayvector(L, t, nasize); + /* create new hash part with appropriate size */ +- setnodevector(L, t, nhsize); ++ asn.t = t; asn.nhsize = nhsize; ++ if (luaD_rawrunprotected(L, auxsetnode, &asn) != LUA_OK) { /* mem. error? */ ++ setarrayvector(L, t, oldasize); /* array back to its original size */ ++ luaD_throw(L, LUA_ERRMEM); /* rethrow memory error */ ++ } + if (nasize < oldasize) { /* array part must shrink? */ + t->sizearray = nasize; + /* re-insert elements from vanishing slice */ ]] } From 6f2b8e21c4ea9d730ccfe7e41bfc178704d1fc75 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 8 Jun 2018 16:06:59 -0300 Subject: [PATCH 0289/1145] added 'const' to 'Proto*' when possible --- ldebug.c | 35 ++++++++++++++++++----------------- ldebug.h | 4 ++-- ltm.c | 4 ++-- ltm.h | 4 ++-- 4 files changed, 24 insertions(+), 23 deletions(-) diff --git a/ldebug.c b/ldebug.c index c07a37f89f..f3c093a05b 100644 --- a/ldebug.c +++ b/ldebug.c @@ -1,5 +1,5 @@ /* -** $Id: ldebug.c,v 2.156 2018/03/16 15:33:34 roberto Exp roberto $ +** $Id: ldebug.c,v 2.157 2018/05/02 18:17:59 roberto Exp roberto $ ** Debug Interface ** See Copyright Notice in lua.h */ @@ -55,7 +55,7 @@ static int currentpc (CallInfo *ci) { ** case is when there is no absolute info or the instruction is before ** the first absolute one. */ -static int getbaseline (Proto *f, int pc, int *basepc) { +static int getbaseline (const Proto *f, int pc, int *basepc) { if (f->sizeabslineinfo == 0 || pc < f->abslineinfo[0].pc) { *basepc = -1; /* start from the beginning */ return f->linedefined; @@ -86,7 +86,7 @@ static int getbaseline (Proto *f, int pc, int *basepc) { ** first gets a base line and from there does the increments until ** the desired instruction. */ -int luaG_getfuncline (Proto *f, int pc) { +int luaG_getfuncline (const Proto *f, int pc) { if (f->lineinfo == NULL) /* no debug information? */ return -1; else { @@ -180,7 +180,7 @@ LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) { } -static const char *upvalname (Proto *p, int uv) { +static const char *upvalname (const Proto *p, int uv) { TString *s = check_exp(uv < p->sizeupvalues, p->upvalues[uv].name); if (s == NULL) return "?"; else return getstr(s); @@ -265,7 +265,7 @@ static void funcinfo (lua_Debug *ar, Closure *cl) { ar->what = "C"; } else { - Proto *p = cl->l.p; + const Proto *p = cl->l.p; ar->source = p->source ? getstr(p->source) : "=?"; ar->linedefined = p->linedefined; ar->lastlinedefined = p->lastlinedefined; @@ -275,7 +275,7 @@ static void funcinfo (lua_Debug *ar, Closure *cl) { } -static int nextline (Proto *p, int currentline, int pc) { +static int nextline (const Proto *p, int currentline, int pc) { if (p->lineinfo[pc] != ABSLINEINFO) return currentline + p->lineinfo[pc]; else @@ -291,7 +291,7 @@ static void collectvalidlines (lua_State *L, Closure *f) { else { int i; TValue v; - Proto *p = f->l.p; + const Proto *p = f->l.p; int currentline = p->linedefined; Table *t = luaH_new(L); /* new table to store active lines */ sethvalue2s(L, L->top, t); /* push it on stack */ @@ -411,14 +411,14 @@ LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) { ** ======================================================= */ -static const char *getobjname (Proto *p, int lastpc, int reg, +static const char *getobjname (const Proto *p, int lastpc, int reg, const char **name); /* ** Find a "name" for the constant 'c'. */ -static void kname (Proto *p, int c, const char **name) { +static void kname (const Proto *p, int c, const char **name) { TValue *kvalue = &p->k[c]; *name = (ttisstring(kvalue)) ? svalue(kvalue) : "?"; } @@ -427,7 +427,7 @@ static void kname (Proto *p, int c, const char **name) { /* ** Find a "name" for the register 'c'. */ -static void rname (Proto *p, int pc, int c, const char **name) { +static void rname (const Proto *p, int pc, int c, const char **name) { const char *what = getobjname(p, pc, c, name); /* search for 'c' */ if (!(what && *what == 'c')) /* did not find a constant name? */ *name = "?"; @@ -437,7 +437,7 @@ static void rname (Proto *p, int pc, int c, const char **name) { /* ** Find a "name" for a 'C' value in an RK instruction. */ -static void rkname (Proto *p, int pc, Instruction i, const char **name) { +static void rkname (const Proto *p, int pc, Instruction i, const char **name) { int c = GETARG_C(i); /* key index */ if (GETARG_k(i)) /* is 'c' a constant? */ kname(p, c, name); @@ -456,7 +456,7 @@ static int filterpc (int pc, int jmptarget) { /* ** try to find last instruction before 'lastpc' that modified register 'reg' */ -static int findsetreg (Proto *p, int lastpc, int reg) { +static int findsetreg (const Proto *p, int lastpc, int reg) { int pc; int setreg = -1; /* keep last instruction that changed 'reg' */ int jmptarget = 0; /* any code before this address is conditional */ @@ -504,7 +504,7 @@ static int findsetreg (Proto *p, int lastpc, int reg) { ** Check whether table being indexed by instruction 'i' is the ** environment '_ENV' */ -static const char *gxf (Proto *p, int pc, Instruction i, int isup) { +static const char *gxf (const Proto *p, int pc, Instruction i, int isup) { int t = GETARG_B(i); /* table index */ const char *name; /* name of indexed variable */ if (isup) /* is an upvalue? */ @@ -515,7 +515,8 @@ static const char *gxf (Proto *p, int pc, Instruction i, int isup) { } - const char *getobjname (Proto *p, int lastpc, int reg, const char **name) { + const char *getobjname (const Proto *p, int lastpc, int reg, + const char **name) { int pc; *name = luaF_getlocalname(p, reg + 1, lastpc); if (*name) /* is a local? */ @@ -585,7 +586,7 @@ static const char *gxf (Proto *p, int pc, Instruction i, int isup) { static const char *funcnamefromcode (lua_State *L, CallInfo *ci, const char **name) { TMS tm = (TMS)0; /* (initial value avoids warnings) */ - Proto *p = ci_func(ci)->p; /* calling function */ + const Proto *p = ci_func(ci)->p; /* calling function */ int pc = currentpc(ci); /* calling instruction index */ Instruction i = p->code[pc]; /* calling instruction */ if (ci->callstatus & CIST_HOOKED) { /* was it called inside a hook? */ @@ -774,7 +775,7 @@ l_noret luaG_runerror (lua_State *L, const char *fmt, ...) { ** Check whether new instruction 'newpc' is in a different line from ** previous instruction 'oldpc'. */ -static int changedline (Proto *p, int oldpc, int newpc) { +static int changedline (const Proto *p, int oldpc, int newpc) { while (oldpc++ < newpc) { if (p->lineinfo[oldpc] != 0) return (luaG_getfuncline(p, oldpc - 1) != luaG_getfuncline(p, newpc)); @@ -807,7 +808,7 @@ int luaG_traceexec (lua_State *L, const Instruction *pc) { if (counthook) luaD_hook(L, LUA_HOOKCOUNT, -1, 0, 0); /* call count hook */ if (mask & LUA_MASKLINE) { - Proto *p = ci_func(ci)->p; + const Proto *p = ci_func(ci)->p; int npci = pcRel(pc, p); if (npci == 0 || /* call linehook when enter a new function, */ pc <= L->oldpc || /* when jump back (loop), or when */ diff --git a/ldebug.h b/ldebug.h index 97a7a00ed3..3b8e6ae0f5 100644 --- a/ldebug.h +++ b/ldebug.h @@ -1,5 +1,5 @@ /* -** $Id: ldebug.h,v 2.16 2018/01/28 15:13:26 roberto Exp roberto $ +** $Id: ldebug.h,v 2.17 2018/05/02 18:17:59 roberto Exp roberto $ ** Auxiliary functions from Debug Interface module ** See Copyright Notice in lua.h */ @@ -21,7 +21,7 @@ */ #define ABSLINEINFO (-0x80) -LUAI_FUNC int luaG_getfuncline (Proto *f, int pc); +LUAI_FUNC int luaG_getfuncline (const Proto *f, int pc); LUAI_FUNC l_noret luaG_typeerror (lua_State *L, const TValue *o, const char *opname); LUAI_FUNC l_noret luaG_concaterror (lua_State *L, const TValue *p1, diff --git a/ltm.c b/ltm.c index f0784305b1..551a2b97c0 100644 --- a/ltm.c +++ b/ltm.c @@ -1,5 +1,5 @@ /* -** $Id: ltm.c,v 2.67 2018/04/04 14:23:41 roberto Exp roberto $ +** $Id: ltm.c,v 2.68 2018/06/01 17:40:38 roberto Exp roberto $ ** Tag methods ** See Copyright Notice in lua.h */ @@ -217,7 +217,7 @@ int luaT_callorderiTM (lua_State *L, const TValue *p1, int v2, void luaT_adjustvarargs (lua_State *L, int nfixparams, CallInfo *ci, - Proto *p) { + const Proto *p) { int i; int actual = cast_int(L->top - ci->func) - 1; /* number of arguments */ int nextra = actual - nfixparams; /* number of extra arguments */ diff --git a/ltm.h b/ltm.h index 38b6e1b214..3069a3ba98 100644 --- a/ltm.h +++ b/ltm.h @@ -1,5 +1,5 @@ /* -** $Id: ltm.h,v 2.36 2018/05/23 14:41:20 roberto Exp roberto $ +** $Id: ltm.h,v 2.37 2018/06/01 16:51:34 roberto Exp roberto $ ** Tag methods ** See Copyright Notice in lua.h */ @@ -84,7 +84,7 @@ LUAI_FUNC int luaT_callorderiTM (lua_State *L, const TValue *p1, int v2, int inv, TMS event); LUAI_FUNC void luaT_adjustvarargs (lua_State *L, int nfixparams, - struct CallInfo *ci, Proto *p); + struct CallInfo *ci, const Proto *p); LUAI_FUNC void luaT_getvarargs (lua_State *L, struct CallInfo *ci, StkId where, int wanted); From 588dfa4ce5f7fa699495a2ee590a57a8305be420 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 8 Jun 2018 16:07:27 -0300 Subject: [PATCH 0290/1145] detail in comment --- lopcodes.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lopcodes.h b/lopcodes.h index b9e9ec969e..9c447a41c5 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -1,5 +1,5 @@ /* -** $Id: lopcodes.h,v 1.190 2018/03/07 15:55:38 roberto Exp roberto $ +** $Id: lopcodes.h,v 1.191 2018/04/04 14:23:41 roberto Exp roberto $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ @@ -288,7 +288,7 @@ OP_SETLIST,/* A B C R(A)[(C-1)*FPF+i] := R(A+i), 1 <= i <= B */ OP_CLOSURE,/* A Bx R(A) := closure(KPROTO[Bx]) */ -OP_VARARG,/* A B C R(A), R(A+1), ..., R(A+C-2) = vararg */ +OP_VARARG,/* A C R(A), R(A+1), ..., R(A+C-2) = vararg */ OP_PREPVARARG,/*A (adjust vararg parameters) */ From 992b6d2712c50ba2a20f161aaaddc7225734c5f1 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 11 Jun 2018 11:19:50 -0300 Subject: [PATCH 0291/1145] no more 'TESTGRAYBIT' (to free this bit for real uses) --- lgc.h | 7 ++++--- ltests.c | 25 ++++--------------------- 2 files changed, 8 insertions(+), 24 deletions(-) diff --git a/lgc.h b/lgc.h index fc62cfd876..d7adf44429 100644 --- a/lgc.h +++ b/lgc.h @@ -1,5 +1,5 @@ /* -** $Id: lgc.h,v 2.102 2018/02/19 13:55:34 roberto Exp roberto $ +** $Id: lgc.h,v 2.102 2018/02/19 20:06:56 roberto Exp roberto $ ** Garbage Collector ** See Copyright Notice in lua.h */ @@ -69,13 +69,14 @@ /* ** Layout for bit use in 'marked' field. First three bits are -** used for object "age" in generational mode. +** used for object "age" in generational mode. Last bit is free +** to be used by respective objects. */ #define WHITE0BIT 3 /* object is white (type 0) */ #define WHITE1BIT 4 /* object is white (type 1) */ #define BLACKBIT 5 /* object is black */ #define FINALIZEDBIT 6 /* object has been marked for finalization */ -#define TESTGRAYBIT 7 /* used by tests (luaL_checkmemory) */ + #define WHITEBITS bit2mask(WHITE0BIT, WHITE1BIT) diff --git a/ltests.c b/ltests.c index bc8b6b43f1..452785c3ce 100644 --- a/ltests.c +++ b/ltests.c @@ -1,5 +1,5 @@ /* -** $Id: ltests.c,v 2.242 2018/02/23 13:13:31 roberto Exp roberto $ +** $Id: ltests.c,v 2.243 2018/03/09 19:24:45 roberto Exp roberto $ ** Internal Module for Debugging of the Lua Implementation ** See Copyright Notice in lua.h */ @@ -428,8 +428,6 @@ static void checkgraylist (global_State *g, GCObject *o) { ((void)g); /* better to keep it available if we need to print an object */ while (o) { lua_assert(isgray(o) || getage(o) == G_TOUCHED2); - lua_assert(!testbit(o->marked, TESTGRAYBIT)); - l_setbit(o->marked, TESTGRAYBIT); switch (o->tt) { case LUA_TTABLE: o = gco2t(o)->gclist; break; case LUA_TLCL: o = gco2lcl(o)->gclist; break; @@ -443,10 +441,9 @@ static void checkgraylist (global_State *g, GCObject *o) { /* -** mark all objects in gray lists with the TESTGRAYBIT, so that -** 'checkmemory' can check that all gray objects are in a gray list +** Check objects in gray lists. */ -static void markgrays (global_State *g) { +static void checkgrays (global_State *g) { if (!keepinvariant(g)) return; checkgraylist(g, g->gray); checkgraylist(g, g->grayagain); @@ -456,17 +453,6 @@ static void markgrays (global_State *g) { } -static void checkgray (global_State *g, GCObject *o) { - for (; o != NULL; o = o->next) { - if ((isgray(o) && o->tt != LUA_TUPVAL) || getage(o) == G_TOUCHED2) { - lua_assert(!keepinvariant(g) || testbit(o->marked, TESTGRAYBIT)); - resetbit(o->marked, TESTGRAYBIT); - } - lua_assert(!testbit(o->marked, TESTGRAYBIT)); - } -} - - static void checklist (global_State *g, int maybedead, int tof, GCObject *newl, GCObject *survival, GCObject *old, GCObject *reallyold) { GCObject *o; @@ -499,7 +485,7 @@ int lua_checkmemory (lua_State *L) { } lua_assert(!isdead(g, gcvalue(&g->l_registry))); lua_assert(g->sweepgc == NULL || issweepphase(g)); - markgrays(g); + checkgrays(g); /* check 'fixedgc' list */ for (o = g->fixedgc; o != NULL; o = o->next) { @@ -507,16 +493,13 @@ int lua_checkmemory (lua_State *L) { } /* check 'allgc' list */ - checkgray(g, g->allgc); maybedead = (GCSatomic < g->gcstate && g->gcstate <= GCSswpallgc); checklist(g, maybedead, 0, g->allgc, g->survival, g->old, g->reallyold); /* check 'finobj' list */ - checkgray(g, g->finobj); checklist(g, 0, 1, g->finobj, g->finobjsur, g->finobjold, g->finobjrold); /* check 'tobefnz' list */ - checkgray(g, g->tobefnz); for (o = g->tobefnz; o != NULL; o = o->next) { checkobject(g, o, 0, G_NEW); lua_assert(tofinalize(o)); From aedcfb94144b173a5af52c05c590e9af12b6632b Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 14 Jun 2018 15:47:22 -0300 Subject: [PATCH 0292/1145] type 'Rand64' may not be long long, so it should not use 'LL' in its constants --- lmathlib.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/lmathlib.c b/lmathlib.c index ecbf4dd184..4101279731 100644 --- a/lmathlib.c +++ b/lmathlib.c @@ -1,5 +1,5 @@ /* -** $Id: lmathlib.c,v 1.133 2018/05/09 14:54:37 roberto Exp roberto $ +** $Id: lmathlib.c,v 1.134 2018/05/16 11:27:59 roberto Exp roberto $ ** Standard mathematical library ** See Copyright Notice in lua.h */ @@ -279,7 +279,7 @@ static int math_type (lua_State *L) { #elif (LUA_MAXINTEGER >> 31 >> 31) >= 1 /* 'lua_Integer' has at least 64 bits */ -#define Rand64 LUA_UNSIGNED +#define Rand64 lua_Unsigned #endif @@ -325,8 +325,9 @@ static Rand64 nextrand (Rand64 *state) { ** Convert bits from a random integer into a float in the ** interval [0,1). */ -#define maskFIG (~(~1LLU << (FIGS - 1))) /* use FIGS bits */ -#define shiftFIG (l_mathop(0.5) / (1LLU << (FIGS - 1))) /* 2^(-FIGS) */ +#define maskFIG (~(~(Rand64)1 << (FIGS - 1))) /* use FIGS bits */ +#define shiftFIG \ + (l_mathop(0.5) / ((Rand64)1 << (FIGS - 1))) /* 2^(-FIGS) */ static lua_Number I2d (Rand64 x) { return (lua_Number)(x & maskFIG) * shiftFIG; From 06127927ffb4eb8459523f6c07bf8f22390c31b9 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 15 Jun 2018 11:13:45 -0300 Subject: [PATCH 0293/1145] new macro 'ispow2' --- llimits.h | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/llimits.h b/llimits.h index 725d7c8b89..715d55b811 100644 --- a/llimits.h +++ b/llimits.h @@ -1,5 +1,5 @@ /* -** $Id: llimits.h,v 1.149 2018/01/28 15:13:26 roberto Exp roberto $ +** $Id: llimits.h,v 1.150 2018/05/30 14:25:52 roberto Exp roberto $ ** Limits, basic types, and some other 'installation-dependent' definitions ** See Copyright Notice in lua.h */ @@ -59,6 +59,12 @@ typedef signed char ls_byte; #define log2maxs(t) (sizeof(t) * 8 - 2) +/* +** test whether an unsigned value is a power of 2 (or zero) +*/ +#define ispow2(x) (((x) & ((x) - 1)) == 0) + + /* ** conversion of pointer to unsigned integer: ** this is for hashing only; there is no problem if the integer From 6e600695f8398843a156ce02023f731c6d687ae8 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 15 Jun 2018 11:14:20 -0300 Subject: [PATCH 0294/1145] field 'sizearray' in struct 'Table' changed to 'alimit', which can be used as a hint for '#t' --- lgc.c | 15 +++-- lobject.h | 17 ++++- ltable.c | 197 ++++++++++++++++++++++++++++++++++++++++++++---------- ltable.h | 3 +- ltests.c | 15 +++-- lvm.c | 4 +- lvm.h | 4 +- 7 files changed, 201 insertions(+), 54 deletions(-) diff --git a/lgc.c b/lgc.c index 2e401cbdb0..b0e3c363a2 100644 --- a/lgc.c +++ b/lgc.c @@ -1,5 +1,5 @@ /* -** $Id: lgc.c,v 2.252 2018/02/26 13:35:03 roberto Exp roberto $ +** $Id: lgc.c,v 2.253 2018/03/16 14:22:09 roberto Exp roberto $ ** Garbage Collector ** See Copyright Notice in lua.h */ @@ -398,7 +398,7 @@ static void traverseweakvalue (global_State *g, Table *h) { Node *n, *limit = gnodelast(h); /* if there is array part, assume it may have white values (it is not worth traversing it now just to check) */ - int hasclears = (h->sizearray > 0); + int hasclears = (h->alimit > 0); for (n = gnode(h, 0); n < limit; n++) { /* traverse hash part */ if (isempty(gval(n))) /* entry is empty? */ clearkey(n); /* clear its key */ @@ -433,8 +433,9 @@ static int traverseephemeron (global_State *g, Table *h) { int hasww = 0; /* true if table has entry "white-key -> white-value" */ Node *n, *limit = gnodelast(h); unsigned int i; + unsigned int asize = luaH_realasize(h); /* traverse array part */ - for (i = 0; i < h->sizearray; i++) { + for (i = 0; i < asize; i++) { if (valiswhite(&h->array[i])) { marked = 1; reallymarkobject(g, gcvalue(&h->array[i])); @@ -472,7 +473,8 @@ static int traverseephemeron (global_State *g, Table *h) { static void traversestrongtable (global_State *g, Table *h) { Node *n, *limit = gnodelast(h); unsigned int i; - for (i = 0; i < h->sizearray; i++) /* traverse array part */ + unsigned int asize = luaH_realasize(h); + for (i = 0; i < asize; i++) /* traverse array part */ markvalue(g, &h->array[i]); for (n = gnode(h, 0); n < limit; n++) { /* traverse hash part */ if (isempty(gval(n))) /* entry is empty? */ @@ -508,7 +510,7 @@ static lu_mem traversetable (global_State *g, Table *h) { } else /* not weak */ traversestrongtable(g, h); - return 1 + h->sizearray + 2 * allocsizenode(h); + return 1 + h->alimit + 2 * allocsizenode(h); } @@ -719,7 +721,8 @@ static void clearbyvalues (global_State *g, GCObject *l, GCObject *f) { Table *h = gco2t(l); Node *n, *limit = gnodelast(h); unsigned int i; - for (i = 0; i < h->sizearray; i++) { + unsigned int asize = luaH_realasize(h); + for (i = 0; i < asize; i++) { TValue *o = &h->array[i]; if (iscleared(g, gcvalueN(o))) /* value was collected? */ setempty(o); /* remove entry */ diff --git a/lobject.h b/lobject.h index da89666291..7c2b3f844f 100644 --- a/lobject.h +++ b/lobject.h @@ -1,5 +1,5 @@ /* -** $Id: lobject.h,v 2.143 2018/06/01 16:51:34 roberto Exp roberto $ +** $Id: lobject.h,v 2.144 2018/06/01 17:40:38 roberto Exp roberto $ ** Type definitions for Lua objects ** See Copyright Notice in lua.h */ @@ -669,11 +669,24 @@ typedef union Node { (void)L; checkliveness(L,io_); } +/* +** About 'alimit': if 'isrealasize(t)' is true, then 'alimit' is the +** real size of 'array'. Otherwise, the real size of 'array' is the +** smallest power of two not smaller than 'alimit' (or zero iff 'alimit' +** is zero); 'alimit' is then used as a hint for #t. +*/ + +#define BITRAS (1 << 7) +#define isrealasize(t) (!((t)->marked & BITRAS)) +#define setrealasize(t) ((t)->marked &= cast_byte(~BITRAS)) +#define setnorealasize(t) ((t)->marked |= BITRAS) + + typedef struct Table { CommonHeader; lu_byte flags; /* 1<

alimit)) + + +/* +** Returns the real size of the 'array' array +*/ +LUAI_FUNC unsigned int luaH_realasize (const Table *t) { + if (limitequalsasize(t)) + return t->alimit; /* this is the size */ + else { + unsigned int size = t->alimit; + /* compute the smallest power of 2 not smaller than 'n' */ + size |= (size >> 1); + size |= (size >> 2); + size |= (size >> 4); + size |= (size >> 8); + size |= (size >> 16); +#if (INT_MAX >> 30 >> 1) > 0 + size |= (size >> 32); /* int has more than 32 bits */ +#endif + size++; + lua_assert(ispow2(size) && size/2 < t->alimit && t->alimit < size); + return size; + } +} + + +/* +** Check whether real size of the array is a power of 2. +** (If it is not, 'alimit' cannot be changed to any other value +** without changing the real size.) +*/ +static int ispow2realasize (const Table *t) { + return (!isrealasize(t) || ispow2(t->alimit)); +} + + +static unsigned int setlimittosize (Table *t) { + t->alimit = luaH_realasize(t); + setrealasize(t); + return t->alimit; +} + + +#define limitasasize(t) check_exp(isrealasize(t), t->alimit) + + + /* ** "Generic" get version. (Not that generic: not valid for integers, ** which may be in array part, nor for floats with integral values.) @@ -228,11 +281,12 @@ static unsigned int arrayindex (lua_Integer k) { ** elements in the array part, then elements in the hash part. The ** beginning of a traversal is signaled by 0. */ -static unsigned int findindex (lua_State *L, Table *t, TValue *key) { +static unsigned int findindex (lua_State *L, Table *t, TValue *key, + unsigned int asize) { unsigned int i; if (ttisnil(key)) return 0; /* first iteration */ i = ttisinteger(key) ? arrayindex(ivalue(key)) : 0; - if (i != 0 && i <= t->sizearray) /* is 'key' inside array part? */ + if (i != 0 && i <= asize) /* is 'key' inside array part? */ return i; /* yes; that's the index */ else { const TValue *n = getgeneric(t, key); @@ -240,21 +294,22 @@ static unsigned int findindex (lua_State *L, Table *t, TValue *key) { luaG_runerror(L, "invalid key to 'next'"); /* key not found */ i = cast_int(nodefromval(n) - gnode(t, 0)); /* key index in hash table */ /* hash elements are numbered after array ones */ - return (i + 1) + t->sizearray; + return (i + 1) + asize; } } int luaH_next (lua_State *L, Table *t, StkId key) { - unsigned int i = findindex(L, t, s2v(key)); /* find original element */ - for (; i < t->sizearray; i++) { /* try first array part */ + unsigned int asize = luaH_realasize(t); + unsigned int i = findindex(L, t, s2v(key), asize); /* find original key */ + for (; i < asize; i++) { /* try first array part */ if (!isempty(&t->array[i])) { /* a non-empty entry? */ setivalue(s2v(key), i + 1); setobj2s(L, key + 1, &t->array[i]); return 1; } } - for (i -= t->sizearray; cast_int(i) < sizenode(t); i++) { /* hash part */ + for (i -= asize; cast_int(i) < sizenode(t); i++) { /* hash part */ if (!isempty(gval(gnode(t, i)))) { /* a non-empty entry? */ Node *n = gnode(t, i); getnodekey(L, s2v(key), n); @@ -329,12 +384,13 @@ static unsigned int numusearray (const Table *t, unsigned int *nums) { unsigned int ttlg; /* 2^lg */ unsigned int ause = 0; /* summation of 'nums' */ unsigned int i = 1; /* count to traverse all array keys */ + unsigned int asize = limitasasize(t); /* real array size */ /* traverse each slice */ for (lg = 0, ttlg = 1; lg <= MAXABITS; lg++, ttlg *= 2) { unsigned int lc = 0; /* counter */ unsigned int lim = ttlg; - if (lim > t->sizearray) { - lim = t->sizearray; /* adjust upper limit */ + if (lim > asize) { + lim = asize; /* adjust upper limit */ if (i > lim) break; /* no more elements to count */ } @@ -451,19 +507,19 @@ void luaH_resize (lua_State *L, Table *t, unsigned int newasize, unsigned int nhsize) { unsigned int i; Table newt; /* to keep the new hash part */ - unsigned int oldasize = t->sizearray; + unsigned int oldasize = setlimittosize(t); TValue *newarray; /* create new hash part with appropriate size into 'newt' */ setnodevector(L, &newt, nhsize); if (newasize < oldasize) { /* will array shrink? */ - t->sizearray = newasize; /* pretend array has new size... */ + t->alimit = newasize; /* pretend array has new size... */ exchangehashpart(t, &newt); /* and new hash */ /* re-insert into the new hash the elements from vanishing slice */ for (i = newasize; i < oldasize; i++) { if (!isempty(&t->array[i])) luaH_setint(L, t, i + 1, &t->array[i]); } - t->sizearray = oldasize; /* restore current size... */ + t->alimit = oldasize; /* restore current size... */ exchangehashpart(t, &newt); /* and hash (in case of errors) */ } /* allocate new array */ @@ -475,7 +531,7 @@ void luaH_resize (lua_State *L, Table *t, unsigned int newasize, /* allocation ok; initialize new part of the array */ exchangehashpart(t, &newt); /* 't' has the new hash ('newt' has the old) */ t->array = newarray; /* set new array part */ - t->sizearray = newasize; + t->alimit = newasize; for (i = oldasize; i < newasize; i++) /* clear new slice of the array */ setempty(&t->array[i]); /* re-insert elements from old hash part into new parts */ @@ -499,6 +555,7 @@ static void rehash (lua_State *L, Table *t, const TValue *ek) { int i; int totaluse; for (i = 0; i <= MAXABITS; i++) nums[i] = 0; /* reset counts */ + setlimittosize(t); na = numusearray(t, nums); /* count keys in array part */ totaluse = na; /* all those keys are integer keys */ totaluse += numusehash(t, nums, &na); /* count keys in hash part */ @@ -525,7 +582,7 @@ Table *luaH_new (lua_State *L) { t->metatable = NULL; t->flags = cast_byte(~0); t->array = NULL; - t->sizearray = 0; + t->alimit = 0; setnodevector(L, t, 0); return t; } @@ -533,7 +590,7 @@ Table *luaH_new (lua_State *L) { void luaH_free (lua_State *L, Table *t) { freehash(L, t); - luaM_freearray(L, t->array, t->sizearray); + luaM_freearray(L, t->array, luaH_realasize(t)); luaM_free(L, t); } @@ -613,12 +670,21 @@ TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) { /* -** search function for integers +** Search function for integers. If integer is inside 'alimit', get it +** directly from the array part. Otherwise, if 'alimit' is not equal to +** the real size of the array, key still can be in the array part. In +** this case, try to avoid a call to 'luaH_realasize' when key is just +** one more than the limit (so that it can be incremented without +** changing the real size of the array). */ const TValue *luaH_getint (Table *t, lua_Integer key) { - /* (1 <= key && key <= t->sizearray) */ - if (l_castS2U(key) - 1u < t->sizearray) + if (l_castS2U(key) - 1u < t->alimit) /* (1 <= key && key <= t->alimit)? */ return &t->array[key - 1]; + else if (!limitequalsasize(t) && /* key still may be in the array part? */ + (key == t->alimit + 1 || l_castS2U(key) - 1u < luaH_realasize(t))) { + t->alimit = cast_uint(key); /* probably '#t' is here now */ + return &t->array[key - 1]; + } else { Node *n = hashint(t, key); for (;;) { /* check whether 'key' is somewhere in the chain */ @@ -749,33 +815,92 @@ static lua_Unsigned hash_search (Table *t, lua_Unsigned j) { } +static unsigned int binsearch (const TValue *array, unsigned int i, + unsigned int j) { + while (j - i > 1u) { /* binary search */ + unsigned int m = (i + j) / 2; + if (isempty(&array[m - 1])) j = m; + else i = m; + } + return i; +} + + /* ** Try to find a boundary in table 't'. (A 'boundary' is an integer index ** such that t[i] is present and t[i+1] is absent, or 0 if t[1] is absent ** and 'maxinteger' if t[maxinteger] is present.) -** First, try the array part: if there is an array part and its last -** element is absent, there must be a boundary there; a binary search -** finds that boundary. Otherwise, if the hash part is empty or does not -** contain 'j + 1', 'j' is a boundary. Otherwize, call 'hash_search' -** to find a boundary in the hash part. +** (In the next explanation, we use Lua indices, that is, with base 1. +** The code itself uses base 0 when indexing the array part of the table.) +** The code starts with 'limit', a position in the array part that may +** be a boundary. +** (1) If 't[limit]' is empty, there must be a boundary before it. +** As a common case (e.g., after 't[#t]=nil'), check whether 'hint-1' +** is present. If so, it is a boundary. Otherwise, do a binary search +** between 0 and limit to find a boundary. In both cases, try to +** use this boundary as the new 'limit', as a hint for the next call. +** (2) If 't[limit]' is not empty and the array has more elements +** after 'limit', try to find a boundary there. Again, try first +** the special case (which should be quite frequent) where 'limit+1' +** is empty, so that 'limit' is a boundary. Otherwise, check the +** last element of the array part (set it as a new limit). If it is empty, +** there must be a boundary between the old limit (present) and the new +** limit (absent), which is found with a binary search. (This boundary +** always can be a new limit.) +** (3) The last case is when there are no elements in the array part +** (limit == 0) or its last element (the new limit) is present. +** In this case, must check the hash part. If there is no hash part, +** the boundary is 0. Otherwise, if 'limit+1' is absent, 'limit' is +** a boundary. Finally, if 'limit+1' is present, call 'hash_search' +** to find a boundary in the hash part of the table. (In those +** cases, the boundary is not inside the array part, and therefore +** cannot be used as a new limit.) */ lua_Unsigned luaH_getn (Table *t) { - unsigned int j = t->sizearray; - if (j > 0 && isempty(&t->array[j - 1])) { - unsigned int i = 0; - while (j - i > 1u) { /* binary search */ - unsigned int m = (i + j) / 2; - if (isempty(&t->array[m - 1])) j = m; - else i = m; + unsigned int limit = t->alimit; + if (limit > 0 && isempty(&t->array[limit - 1])) { + /* (1) there must be a boundary before 'limit' */ + if (limit >= 2 && !isempty(&t->array[limit - 2])) { + /* 'limit - 1' is a boundary; can it be a new limit? */ + if (ispow2realasize(t) && !ispow2(limit - 1)) { + t->alimit = limit - 1; + setnorealasize(t); + } + return limit - 1; + } + else { /* must search for a boundary in [0, limit] */ + unsigned int boundary = binsearch(t->array, 0, limit); + /* can this boundary represent the real size of the array? */ + if (ispow2realasize(t) && boundary > luaH_realasize(t) / 2) { + t->alimit = boundary; /* use it as the new limit */ + setnorealasize(t); + } + return boundary; } - return i; } - else { /* 'j' is zero or present in table */ - if (isdummy(t) || isempty(luaH_getint(t, cast(lua_Integer, j + 1)))) - return j; /* 'j + 1' is absent... */ - else /* 'j + 1' is also present */ - return hash_search(t, j); + /* 'limit' is zero or present in table */ + if (!limitequalsasize(t)) { + /* (2) 'limit' > 0 and array has more elements after 'limit' */ + if (isempty(&t->array[limit])) /* 'limit + 1' is empty? */ + return limit; /* this is the boundary */ + /* else, try last element in the array */ + limit = luaH_realasize(t); + if (isempty(&t->array[limit - 1])) { /* empty? */ + /* there must be a boundary in the array after old limit, + and it must be a valid new limit */ + unsigned int boundary = binsearch(t->array, t->alimit, limit); + t->alimit = boundary; + return boundary; + } + /* else, new limit is present in the table; check the hash part */ } + /* (3) 'limit' is the last element and either is zero or present in table */ + lua_assert(limit == luaH_realasize(t) && + (limit == 0 || !isempty(&t->array[limit - 1]))); + if (isdummy(t) || isempty(luaH_getint(t, cast(lua_Integer, limit + 1)))) + return limit; /* 'limit + 1' is absent... */ + else /* 'limit + 1' is also present */ + return hash_search(t, limit); } diff --git a/ltable.h b/ltable.h index 9b1a346492..ba2dddc8f2 100644 --- a/ltable.h +++ b/ltable.h @@ -1,5 +1,5 @@ /* -** $Id: ltable.h,v 2.26 2018/02/23 13:13:31 roberto Exp roberto $ +** $Id: ltable.h,v 2.27 2018/06/01 16:51:34 roberto Exp roberto $ ** Lua tables (hash) ** See Copyright Notice in lua.h */ @@ -45,6 +45,7 @@ LUAI_FUNC void luaH_resizearray (lua_State *L, Table *t, unsigned int nasize); LUAI_FUNC void luaH_free (lua_State *L, Table *t); LUAI_FUNC int luaH_next (lua_State *L, Table *t, StkId key); LUAI_FUNC lua_Unsigned luaH_getn (Table *t); +LUAI_FUNC unsigned int luaH_realasize (const Table *t); #if defined(LUA_DEBUG) diff --git a/ltests.c b/ltests.c index 452785c3ce..82d6463a04 100644 --- a/ltests.c +++ b/ltests.c @@ -1,5 +1,5 @@ /* -** $Id: ltests.c,v 2.243 2018/03/09 19:24:45 roberto Exp roberto $ +** $Id: ltests.c,v 2.244 2018/06/11 14:19:50 roberto Exp roberto $ ** Internal Module for Debugging of the Lua Implementation ** See Copyright Notice in lua.h */ @@ -248,10 +248,11 @@ static void checkvalref (global_State *g, GCObject *f, const TValue *t) { static void checktable (global_State *g, Table *h) { unsigned int i; + unsigned int asize = luaH_realasize(h); Node *n, *limit = gnode(h, sizenode(h)); GCObject *hgc = obj2gco(h); checkobjref(g, hgc, h->metatable); - for (i = 0; i < h->sizearray; i++) + for (i = 0; i < asize; i++) checkvalref(g, hgc, &h->array[i]); for (n = gnode(h, 0); n < limit; n++) { if (!isempty(gval(n))) { @@ -810,19 +811,23 @@ static int stacklevel (lua_State *L) { static int table_query (lua_State *L) { const Table *t; int i = cast_int(luaL_optinteger(L, 2, -1)); + unsigned int asize; luaL_checktype(L, 1, LUA_TTABLE); t = hvalue(obj_at(L, 1)); + asize = luaH_realasize(t); if (i == -1) { - lua_pushinteger(L, t->sizearray); + lua_pushinteger(L, asize); lua_pushinteger(L, allocsizenode(t)); lua_pushinteger(L, isdummy(t) ? 0 : t->lastfree - t->node); + lua_pushinteger(L, t->alimit); + return 4; } - else if ((unsigned int)i < t->sizearray) { + else if ((unsigned int)i < asize) { lua_pushinteger(L, i); pushobject(L, &t->array[i]); lua_pushnil(L); } - else if ((i -= t->sizearray) < sizenode(t)) { + else if ((i -= asize) < sizenode(t)) { TValue k; getnodekey(L, &k, gnode(t, i)); if (!isempty(gval(gnode(t, i))) || diff --git a/lvm.c b/lvm.c index 3fed2a2762..1e45e2ce84 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.356 2018/05/30 14:25:52 roberto Exp roberto $ +** $Id: lvm.c,v 2.357 2018/06/01 16:51:34 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -1766,7 +1766,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { } h = hvalue(s2v(ra)); last = ((c-1)*LFIELDS_PER_FLUSH) + n; - if (last > h->sizearray) /* needs more space? */ + if (last > luaH_realasize(h)) /* needs more space? */ luaH_resizearray(L, h, last); /* preallocate it at once */ for (; n > 0; n--) { TValue *val = s2v(ra + n); diff --git a/lvm.h b/lvm.h index c5d5206de5..a789acc402 100644 --- a/lvm.h +++ b/lvm.h @@ -1,5 +1,5 @@ /* -** $Id: lvm.h,v 2.50 2018/02/21 12:54:26 roberto Exp roberto $ +** $Id: lvm.h,v 2.51 2018/02/23 13:13:31 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -84,7 +84,7 @@ #define luaV_fastgeti(L,t,k,slot) \ (!ttistable(t) \ ? (slot = NULL, 0) /* not a table; 'slot' is NULL and result is 0 */ \ - : (slot = (l_castS2U(k) - 1u < hvalue(t)->sizearray) \ + : (slot = (l_castS2U(k) - 1u < hvalue(t)->alimit) \ ? &hvalue(t)->array[k - 1] : luaH_getint(hvalue(t), k), \ !isempty(slot))) /* result not empty? */ From 2c107e13a8234ea16f0331fe0ff53b784af2bdc1 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 15 Jun 2018 11:18:40 -0300 Subject: [PATCH 0295/1145] warning (comparison between signed and unsigned integers) --- ltable.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/ltable.c b/ltable.c index b0d3944d04..6f9c4035dd 100644 --- a/ltable.c +++ b/ltable.c @@ -1,5 +1,5 @@ /* -** $Id: ltable.c,v 2.138 2018/06/01 16:51:34 roberto Exp roberto $ +** $Id: ltable.c,v 2.139 2018/06/15 14:14:20 roberto Exp roberto $ ** Lua tables (hash) ** See Copyright Notice in lua.h */ @@ -681,7 +681,8 @@ const TValue *luaH_getint (Table *t, lua_Integer key) { if (l_castS2U(key) - 1u < t->alimit) /* (1 <= key && key <= t->alimit)? */ return &t->array[key - 1]; else if (!limitequalsasize(t) && /* key still may be in the array part? */ - (key == t->alimit + 1 || l_castS2U(key) - 1u < luaH_realasize(t))) { + (l_castS2U(key) == t->alimit + 1 || + l_castS2U(key) - 1u < luaH_realasize(t))) { t->alimit = cast_uint(key); /* probably '#t' is here now */ return &t->array[key - 1]; } From b6780ee41b1f5c120601a72b12d81e3ce71a8971 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 15 Jun 2018 12:49:28 -0300 Subject: [PATCH 0296/1145] detail (removed unused definition for 'LUA_QS') --- luaconf.h | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/luaconf.h b/luaconf.h index 1f9d5f3b67..6456506658 100644 --- a/luaconf.h +++ b/luaconf.h @@ -1,5 +1,5 @@ /* -** $Id: luaconf.h,v 1.267 2018/03/09 14:56:02 roberto Exp roberto $ +** $Id: luaconf.h,v 1.268 2018/04/02 13:58:33 roberto Exp roberto $ ** Configuration file for Lua ** See Copyright Notice in lua.h */ @@ -699,21 +699,13 @@ /* @@ LUAI_MAXALIGN defines fields that, when used in a union, ensure -** "maximum" alignment for the other items in that union. +** maximum alignment for the other items in that union. */ #define LUAI_MAXALIGN lua_Number n; double u; void *s; lua_Integer i; long l /* }================================================================== */ -/* -@@ LUA_QL describes how error messages quote program elements. -** Lua does not use these macros anymore; they are here for -** compatibility only. -*/ -#define LUA_QL(x) "'" x "'" -#define LUA_QS LUA_QL("%s") - From d406d3d05ffe8bc01d6416437963ce092cfc9772 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 15 Jun 2018 14:30:52 -0300 Subject: [PATCH 0297/1145] removed unused macros 'isstackindex'/'api_checkstackindex' + macro 'api_checkvalidindex' (used only once) expanded and removed --- lapi.c | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/lapi.c b/lapi.c index 88552899ef..157c22b230 100644 --- a/lapi.c +++ b/lapi.c @@ -1,5 +1,5 @@ /* -** $Id: lapi.c,v 2.291 2018/04/04 14:23:41 roberto Exp roberto $ +** $Id: lapi.c,v 2.292 2018/06/01 17:40:38 roberto Exp roberto $ ** Lua API ** See Copyright Notice in lua.h */ @@ -51,14 +51,6 @@ static const TValue nonvalidvaluep = {NILCONSTANT}; /* test for upvalue */ #define isupvalue(i) ((i) < LUA_REGISTRYINDEX) -/* test for valid but not pseudo index */ -#define isstackindex(i, o) (isvalid(o) && !ispseudo(i)) - -#define api_checkvalidindex(l,o) api_check(l, isvalid(o), "invalid index") - -#define api_checkstackindex(l, i, o) \ - api_check(l, isstackindex(i, o), "index not in the stack") - static TValue *index2value (lua_State *L, int idx) { CallInfo *ci = L->ci; @@ -233,7 +225,7 @@ LUA_API void lua_copy (lua_State *L, int fromidx, int toidx) { lua_lock(L); fr = index2value(L, fromidx); to = index2value(L, toidx); - api_checkvalidindex(L, to); + api_check(l, isvalid(to), "invalid index"); setobj(L, to, fr); if (isupvalue(toidx)) /* function upvalue? */ luaC_barrier(L, clCvalue(s2v(L->ci->func)), fr); From b95e46621873cfb460e1d11dcd153914d5d69f86 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 15 Jun 2018 16:31:22 -0300 Subject: [PATCH 0298/1145] new field 'nilvalue' in struct 'global_State' to avoid the use of addresses of static variables --- lapi.c | 23 ++++++++++------------- lobject.h | 5 +---- lstate.c | 3 ++- lstate.h | 3 ++- ltm.c | 5 ++--- 5 files changed, 17 insertions(+), 22 deletions(-) diff --git a/lapi.c b/lapi.c index 157c22b230..5d10607651 100644 --- a/lapi.c +++ b/lapi.c @@ -1,5 +1,5 @@ /* -** $Id: lapi.c,v 2.292 2018/06/01 17:40:38 roberto Exp roberto $ +** $Id: lapi.c,v 2.293 2018/06/15 17:30:52 roberto Exp roberto $ ** Lua API ** See Copyright Notice in lua.h */ @@ -37,13 +37,10 @@ const char lua_ident[] = "$LuaAuthors: " LUA_AUTHORS " $"; -/* value at a non-valid index */ -static const TValue nonvalidvaluep = {NILCONSTANT}; -#define NONVALIDVALUE cast(TValue *, &nonvalidvaluep) +/* test for a valid index */ +#define isvalid(L, o) (!ttisnil(o) || o != &G(L)->nilvalue) -/* corresponding test */ -#define isvalid(o) ((o) != &nonvalidvaluep) /* test for pseudo index */ #define ispseudo(i) ((i) <= LUA_REGISTRYINDEX) @@ -57,7 +54,7 @@ static TValue *index2value (lua_State *L, int idx) { if (idx > 0) { StkId o = ci->func + idx; api_check(L, idx <= L->ci->top - (ci->func + 1), "unacceptable index"); - if (o >= L->top) return NONVALIDVALUE; + if (o >= L->top) return &G(L)->nilvalue; else return s2v(o); } else if (!ispseudo(idx)) { /* negative index */ @@ -70,10 +67,10 @@ static TValue *index2value (lua_State *L, int idx) { idx = LUA_REGISTRYINDEX - idx; api_check(L, idx <= MAXUPVAL + 1, "upvalue index too large"); if (ttislcf(s2v(ci->func))) /* light C function? */ - return NONVALIDVALUE; /* it has no upvalues */ + return &G(L)->nilvalue; /* it has no upvalues */ else { CClosure *func = clCvalue(s2v(ci->func)); - return (idx <= func->nupvalues) ? &func->upvalue[idx-1] : NONVALIDVALUE; + return (idx <= func->nupvalues) ? &func->upvalue[idx-1] : &G(L)->nilvalue; } } } @@ -225,7 +222,7 @@ LUA_API void lua_copy (lua_State *L, int fromidx, int toidx) { lua_lock(L); fr = index2value(L, fromidx); to = index2value(L, toidx); - api_check(l, isvalid(to), "invalid index"); + api_check(l, isvalid(L, to), "invalid index"); setobj(L, to, fr); if (isupvalue(toidx)) /* function upvalue? */ luaC_barrier(L, clCvalue(s2v(L->ci->func)), fr); @@ -251,7 +248,7 @@ LUA_API void lua_pushvalue (lua_State *L, int idx) { LUA_API int lua_type (lua_State *L, int idx) { const TValue *o = index2value(L, idx); - return (isvalid(o) ? ttype(o) : LUA_TNONE); + return (isvalid(L, o) ? ttype(o) : LUA_TNONE); } @@ -296,7 +293,7 @@ LUA_API int lua_isuserdata (lua_State *L, int idx) { LUA_API int lua_rawequal (lua_State *L, int index1, int index2) { const TValue *o1 = index2value(L, index1); const TValue *o2 = index2value(L, index2); - return (isvalid(o1) && isvalid(o2)) ? luaV_rawequalobj(o1, o2) : 0; + return (isvalid(L, o1) && isvalid(L, o2)) ? luaV_rawequalobj(o1, o2) : 0; } @@ -323,7 +320,7 @@ LUA_API int lua_compare (lua_State *L, int index1, int index2, int op) { lua_lock(L); /* may call tag method */ o1 = index2value(L, index1); o2 = index2value(L, index2); - if (isvalid(o1) && isvalid(o2)) { + if (isvalid(L, o1) && isvalid(L, o2)) { switch (op) { case LUA_OPEQ: i = luaV_equalobj(L, o1, o2); break; case LUA_OPLT: i = luaV_lessthan(L, o1, o2); break; diff --git a/lobject.h b/lobject.h index 7c2b3f844f..dbb9982d03 100644 --- a/lobject.h +++ b/lobject.h @@ -1,5 +1,5 @@ /* -** $Id: lobject.h,v 2.144 2018/06/01 17:40:38 roberto Exp roberto $ +** $Id: lobject.h,v 2.145 2018/06/15 14:14:20 roberto Exp roberto $ ** Type definitions for Lua objects ** See Copyright Notice in lua.h */ @@ -144,9 +144,6 @@ typedef StackValue *StkId; /* index to stack elements */ #define ttisstrictnil(o) checktag((o), LUA_TNIL) -/* macro defining a nil value */ -#define NILCONSTANT {NULL}, LUA_TNIL - #define setnilvalue(obj) settt_(obj, LUA_TNIL) diff --git a/lstate.c b/lstate.c index 2622a1d922..e369683eac 100644 --- a/lstate.c +++ b/lstate.c @@ -1,5 +1,5 @@ /* -** $Id: lstate.c,v 2.152 2018/05/29 18:02:51 roberto Exp roberto $ +** $Id: lstate.c,v 2.153 2018/06/01 17:40:38 roberto Exp roberto $ ** Global State ** See Copyright Notice in lua.h */ @@ -345,6 +345,7 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { g->twups = NULL; g->totalbytes = sizeof(LG); g->GCdebt = 0; + setnilvalue(&g->nilvalue); setgcparam(g->gcpause, LUAI_GCPAUSE); setgcparam(g->gcstepmul, LUAI_GCMUL); g->gcstepsize = LUAI_GCSTEPSIZE; diff --git a/lstate.h b/lstate.h index cd8b4db7dc..037b2be615 100644 --- a/lstate.h +++ b/lstate.h @@ -1,5 +1,5 @@ /* -** $Id: lstate.h,v 2.157 2018/02/25 12:43:52 roberto Exp roberto $ +** $Id: lstate.h,v 2.158 2018/03/16 15:33:34 roberto Exp roberto $ ** Global State ** See Copyright Notice in lua.h */ @@ -148,6 +148,7 @@ typedef struct global_State { lu_mem GCestimate; /* an estimate of the non-garbage memory in use */ stringtable strt; /* hash table for strings */ TValue l_registry; + TValue nilvalue; /* a nil value */ unsigned int seed; /* randomized seed for hashes */ lu_byte currentwhite; lu_byte gcstate; /* state of garbage collector */ diff --git a/ltm.c b/ltm.c index 551a2b97c0..d4e2b447b9 100644 --- a/ltm.c +++ b/ltm.c @@ -1,5 +1,5 @@ /* -** $Id: ltm.c,v 2.68 2018/06/01 17:40:38 roberto Exp roberto $ +** $Id: ltm.c,v 2.69 2018/06/08 19:06:59 roberto Exp roberto $ ** Tag methods ** See Copyright Notice in lua.h */ @@ -69,7 +69,6 @@ const TValue *luaT_gettm (Table *events, TMS event, TString *ename) { const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, TMS event) { - static const TValue nilobject = {NILCONSTANT}; Table *mt; switch (ttype(o)) { case LUA_TTABLE: @@ -81,7 +80,7 @@ const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, TMS event) { default: mt = G(L)->mt[ttype(o)]; } - return (mt ? luaH_getshortstr(mt, G(L)->tmname[event]) : &nilobject); + return (mt ? luaH_getshortstr(mt, G(L)->tmname[event]) : &G(L)->nilvalue); } From af70905246acfad225904b64d027e5b01c7b10eb Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 18 Jun 2018 09:08:10 -0300 Subject: [PATCH 0299/1145] no need to check whether libraries and host use the same kernel; Lua should work correctly with several copies of the kernel --- lapi.c | 9 ++++----- lauxlib.c | 10 ++++------ lmem.c | 4 ++-- lstate.c | 11 +++++------ lstate.h | 3 +-- lua.h | 4 ++-- 6 files changed, 18 insertions(+), 23 deletions(-) diff --git a/lapi.c b/lapi.c index 5d10607651..e69215a881 100644 --- a/lapi.c +++ b/lapi.c @@ -1,5 +1,5 @@ /* -** $Id: lapi.c,v 2.293 2018/06/15 17:30:52 roberto Exp roberto $ +** $Id: lapi.c,v 2.294 2018/06/15 19:31:22 roberto Exp roberto $ ** Lua API ** See Copyright Notice in lua.h */ @@ -138,10 +138,9 @@ LUA_API lua_CFunction lua_atpanic (lua_State *L, lua_CFunction panicf) { } -LUA_API const lua_Number *lua_version (lua_State *L) { - static const lua_Number version = LUA_VERSION_NUM; - if (L == NULL) return &version; - else return G(L)->version; +LUA_API lua_Number lua_version (lua_State *L) { + UNUSED(L); + return LUA_VERSION_NUM; } diff --git a/lauxlib.c b/lauxlib.c index 215a83b1ef..a8f2cc2e7a 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -1,5 +1,5 @@ /* -** $Id: lauxlib.c,v 1.293 2018/02/21 13:48:44 roberto Exp roberto $ +** $Id: lauxlib.c,v 1.294 2018/02/27 18:47:32 roberto Exp roberto $ ** Auxiliary functions for building Lua libraries ** See Copyright Notice in lua.h */ @@ -950,13 +950,11 @@ LUALIB_API lua_State *luaL_newstate (void) { LUALIB_API void luaL_checkversion_ (lua_State *L, lua_Number ver, size_t sz) { - const lua_Number *v = lua_version(L); + lua_Number v = lua_version(L); if (sz != LUAL_NUMSIZES) /* check numeric types */ luaL_error(L, "core and library have incompatible numeric types"); - if (v != lua_version(NULL)) - luaL_error(L, "multiple Lua VMs detected"); - else if (*v != ver) + else if (v != ver) luaL_error(L, "version mismatch: app. needs %f, Lua core provides %f", - (LUAI_UACNUMBER)ver, (LUAI_UACNUMBER)*v); + (LUAI_UACNUMBER)ver, (LUAI_UACNUMBER)v); } diff --git a/lmem.c b/lmem.c index 5c73acd5ca..56b4affe0e 100644 --- a/lmem.c +++ b/lmem.c @@ -1,5 +1,5 @@ /* -** $Id: lmem.c,v 1.96 2018/01/28 15:13:26 roberto Exp roberto $ +** $Id: lmem.c,v 1.97 2018/05/30 14:25:52 roberto Exp roberto $ ** Interface to Memory Manager ** See Copyright Notice in lua.h */ @@ -123,7 +123,7 @@ void luaM_free_ (lua_State *L, void *block, size_t osize) { static void *tryagain (lua_State *L, void *block, size_t osize, size_t nsize) { global_State *g = G(L); - if (g->version) { /* is state fully build? */ + if (ttisnil(&g->nilvalue)) { /* is state fully build? */ luaC_fullgc(L, 1); /* try to free some memory... */ return (*g->frealloc)(g->ud, block, osize, nsize); /* try again */ } diff --git a/lstate.c b/lstate.c index e369683eac..d4b4def80a 100644 --- a/lstate.c +++ b/lstate.c @@ -1,5 +1,5 @@ /* -** $Id: lstate.c,v 2.153 2018/06/01 17:40:38 roberto Exp roberto $ +** $Id: lstate.c,v 2.154 2018/06/15 19:31:22 roberto Exp roberto $ ** Global State ** See Copyright Notice in lua.h */ @@ -215,7 +215,7 @@ static void init_registry (lua_State *L, global_State *g) { /* ** open parts of the state that may cause memory-allocation errors. -** ('g->version' != NULL flags that the state was completely build) +** ('ttisnil(&g->nilvalue)'' flags that the state was completely build) */ static void f_luaopen (lua_State *L, void *ud) { global_State *g = G(L); @@ -226,7 +226,7 @@ static void f_luaopen (lua_State *L, void *ud) { luaT_init(L); luaX_init(L); g->gcrunning = 1; /* allow gc */ - g->version = lua_version(NULL); + setnilvalue(&g->nilvalue); luai_userstateopen(L); } @@ -260,7 +260,7 @@ static void close_state (lua_State *L) { global_State *g = G(L); luaF_close(L, L->stack); /* close all upvalues for this thread */ luaC_freeallobjects(L); /* collect all objects */ - if (g->version) /* closing a fully built state? */ + if (ttisnil(&g->nilvalue)) /* closing a fully built state? */ luai_userstateclose(L); luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size); freestack(L); @@ -332,7 +332,6 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { g->strt.hash = NULL; setnilvalue(&g->l_registry); g->panic = NULL; - g->version = NULL; g->gcstate = GCSpause; g->gckind = KGC_INC; g->gcemergency = 0; @@ -345,7 +344,7 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { g->twups = NULL; g->totalbytes = sizeof(LG); g->GCdebt = 0; - setnilvalue(&g->nilvalue); + setivalue(&g->nilvalue, 0); /* to signal that state is not yet built */ setgcparam(g->gcpause, LUAI_GCPAUSE); setgcparam(g->gcstepmul, LUAI_GCMUL); g->gcstepsize = LUAI_GCSTEPSIZE; diff --git a/lstate.h b/lstate.h index 037b2be615..0e173b5b00 100644 --- a/lstate.h +++ b/lstate.h @@ -1,5 +1,5 @@ /* -** $Id: lstate.h,v 2.158 2018/03/16 15:33:34 roberto Exp roberto $ +** $Id: lstate.h,v 2.159 2018/06/15 19:31:22 roberto Exp roberto $ ** Global State ** See Copyright Notice in lua.h */ @@ -181,7 +181,6 @@ typedef struct global_State { struct lua_State *twups; /* list of threads with open upvalues */ lua_CFunction panic; /* to be called in unprotected errors */ struct lua_State *mainthread; - const lua_Number *version; /* pointer to version number */ TString *memerrmsg; /* message for memory-allocation errors */ TString *tmname[TM_N]; /* array with tag-method names */ struct Table *mt[LUA_NUMTAGS]; /* metatables for basic types */ diff --git a/lua.h b/lua.h index 3f76fbea4d..12d9e2ec18 100644 --- a/lua.h +++ b/lua.h @@ -1,5 +1,5 @@ /* -** $Id: lua.h,v 1.345 2018/03/16 15:33:34 roberto Exp roberto $ +** $Id: lua.h,v 1.346 2018/04/04 14:23:41 roberto Exp roberto $ ** Lua - A Scripting Language ** Lua.org, PUC-Rio, Brazil (http://www.lua.org) ** See Copyright Notice at the end of this file @@ -149,7 +149,7 @@ LUA_API lua_State *(lua_newthread) (lua_State *L); LUA_API lua_CFunction (lua_atpanic) (lua_State *L, lua_CFunction panicf); -LUA_API const lua_Number *(lua_version) (lua_State *L); +LUA_API lua_Number (lua_version) (lua_State *L); /* From b43300c14f562bcdc1050f2c05e52fac3f6c99b7 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 18 Jun 2018 09:51:05 -0300 Subject: [PATCH 0300/1145] change in 'LUAI_DDEC' to allow variables to be static in 'onelua' + change in 'LUAMOD_API' as opening functions do not need to be global --- lctype.h | 4 ++-- lopcodes.h | 6 +++--- ltm.h | 4 ++-- luaconf.h | 16 ++++++++++------ 4 files changed, 17 insertions(+), 13 deletions(-) diff --git a/lctype.h b/lctype.h index 5dc17013c2..de1efd51fc 100644 --- a/lctype.h +++ b/lctype.h @@ -1,5 +1,5 @@ /* -** $Id: lctype.h,v 1.11 2011/06/27 18:22:46 roberto Exp roberto $ +** $Id: lctype.h,v 1.12 2011/07/15 12:50:29 roberto Exp roberto $ ** 'ctype' functions for Lua ** See Copyright Notice in lua.h */ @@ -68,7 +68,7 @@ /* two more entries for 0 and -1 (EOZ) */ -LUAI_DDEC const lu_byte luai_ctype_[UCHAR_MAX + 2]; +LUAI_DDEC(const lu_byte luai_ctype_[UCHAR_MAX + 2];) #else /* }{ */ diff --git a/lopcodes.h b/lopcodes.h index 9c447a41c5..821bb196de 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -1,5 +1,5 @@ /* -** $Id: lopcodes.h,v 1.191 2018/04/04 14:23:41 roberto Exp roberto $ +** $Id: lopcodes.h,v 1.192 2018/06/08 19:07:27 roberto Exp roberto $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ @@ -339,7 +339,7 @@ OP_EXTRAARG/* Ax extra (larger) argument for previous opcode */ ** bit 6: instruction sets 'L->top' for next instruction (when C == 0) */ -LUAI_DDEC const lu_byte luaP_opmodes[NUM_OPCODES]; +LUAI_DDEC(const lu_byte luaP_opmodes[NUM_OPCODES];) #define getOpMode(m) (cast(enum OpMode, luaP_opmodes[m] & 7)) #define testAMode(m) (luaP_opmodes[m] & (1 << 3)) @@ -358,7 +358,7 @@ LUAI_DDEC const lu_byte luaP_opmodes[NUM_OPCODES]; #define opmode(ot,it,t,a,m) (((ot)<<6) | ((it)<<5) | ((t)<<4) | ((a)<<3) | (m)) -LUAI_DDEC const char *const luaP_opnames[NUM_OPCODES+1]; /* opcode names */ +LUAI_DDEC(const char *const luaP_opnames[NUM_OPCODES+1];) /* opcode names */ /* number of list items to accumulate before a SETLIST instruction */ diff --git a/ltm.h b/ltm.h index 3069a3ba98..12ae09ded7 100644 --- a/ltm.h +++ b/ltm.h @@ -1,5 +1,5 @@ /* -** $Id: ltm.h,v 2.37 2018/06/01 16:51:34 roberto Exp roberto $ +** $Id: ltm.h,v 2.38 2018/06/08 19:06:59 roberto Exp roberto $ ** Tag methods ** See Copyright Notice in lua.h */ @@ -58,7 +58,7 @@ typedef enum { #define ttypename(x) luaT_typenames_[(x) + 1] -LUAI_DDEC const char *const luaT_typenames_[LUA_TOTALTAGS]; +LUAI_DDEC(const char *const luaT_typenames_[LUA_TOTALTAGS];) LUAI_FUNC const char *luaT_objtypename (lua_State *L, const TValue *o); diff --git a/luaconf.h b/luaconf.h index 6456506658..f1ca51f5a0 100644 --- a/luaconf.h +++ b/luaconf.h @@ -1,5 +1,5 @@ /* -** $Id: luaconf.h,v 1.268 2018/04/02 13:58:33 roberto Exp roberto $ +** $Id: luaconf.h,v 1.269 2018/06/15 15:49:28 roberto Exp roberto $ ** Configuration file for Lua ** See Copyright Notice in lua.h */ @@ -254,16 +254,20 @@ #endif /* } */ -/* more often than not the libs go together with the core */ +/* +** More often than not the libs go together with the core; +** Functions from the auxiliary library must be exported, +** but opening functions do not. +*/ #define LUALIB_API LUA_API -#define LUAMOD_API LUALIB_API +#define LUAMOD_API LUAI_FUNC /* @@ LUAI_FUNC is a mark for all extern functions that are not to be ** exported to outside modules. -@@ LUAI_DDEF and LUAI_DDEC are marks for all extern (const) variables -** that are not to be exported to outside modules (LUAI_DDEF for +@@ LUAI_DDEF and LUAI_DDEC are marks for all extern (const) variables, +** none of which to be exported to outside modules (LUAI_DDEF for ** definitions and LUAI_DDEC for declarations). ** CHANGE them if you need to mark them in some special way. Elf/gcc ** (versions 3.2 and later) mark them as "hidden" to optimize access @@ -280,7 +284,7 @@ #define LUAI_FUNC extern #endif /* } */ -#define LUAI_DDEC LUAI_FUNC +#define LUAI_DDEC(dec) LUAI_FUNC dec #define LUAI_DDEF /* empty */ /* }================================================================== */ From 15ce8d09040c07e5c738f9ffbb5a71a913b4be65 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 18 Jun 2018 14:57:20 -0300 Subject: [PATCH 0301/1145] in generational mode, an emergency collection can turn any object black during any memory allocation. --- lparser.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lparser.c b/lparser.c index e67d70ea6b..3c68d1c240 100644 --- a/lparser.c +++ b/lparser.c @@ -1,5 +1,5 @@ /* -** $Id: lparser.c,v 2.179 2018/03/07 15:55:38 roberto Exp roberto $ +** $Id: lparser.c,v 2.180 2018/04/04 14:23:41 roberto Exp roberto $ ** Lua Parser ** See Copyright Notice in lua.h */ @@ -1709,7 +1709,7 @@ LClosure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, luaD_inctop(L); funcstate.f = cl->p = luaF_newproto(L); funcstate.f->source = luaS_new(L, name); /* create and anchor TString */ - lua_assert(iswhite(funcstate.f)); /* do not need barrier here */ + luaC_objbarrier(L, funcstate.f, funcstate.f->source); lexstate.buff = buff; lexstate.dyd = dyd; dyd->actvar.n = dyd->gt.n = dyd->label.n = 0; From a314409dba5579b3523460f4e385574fce40aeb2 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 18 Jun 2018 14:58:21 -0300 Subject: [PATCH 0302/1145] in generational mode, an emergency collection can turn any object black during any memory allocation + 'luaT_getvarargs' may reallocate the stack, and therefore the top must be correct. --- lvm.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lvm.c b/lvm.c index 1e45e2ce84..9e8bec0c58 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.357 2018/06/01 16:51:34 roberto Exp roberto $ +** $Id: lvm.c,v 2.358 2018/06/15 14:14:20 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -712,7 +712,7 @@ static void pushclosure (lua_State *L, Proto *p, UpVal **encup, StkId base, ncl->upvals[i] = luaF_findupval(L, base + uv[i].idx); else /* get upvalue from enclosing function */ ncl->upvals[i] = encup[uv[i].idx]; - /* new closure is white, so we do not need a barrier here */ + luaC_objbarrier(L, ncl, ncl->upvals[i]); } if (p->cachemiss >= MAXMISS) /* too many missings? */ p->cache = NULL; /* give up cache */ @@ -1790,7 +1790,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { } vmcase(OP_VARARG) { int n = GETARG_C(i) - 1; /* required results */ - ProtectNT(luaT_getvarargs(L, ci, ra, n)); + Protect(luaT_getvarargs(L, ci, ra, n)); vmbreak; } vmcase(OP_PREPVARARG) { From 6683f83b51bf87cad0980712955c959c4c128e24 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 18 Jun 2018 15:25:19 -0300 Subject: [PATCH 0303/1145] several details --- makefile | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/makefile b/makefile index 9367ffd7d2..1263f7fdcf 100644 --- a/makefile +++ b/makefile @@ -6,7 +6,6 @@ # Warnings valid for both C and C++ CWARNSCPP= \ - # -pedantic \ /* warns if we use jump tables */ -Wextra \ -Wshadow \ -Wsign-compare \ @@ -15,9 +14,11 @@ CWARNSCPP= \ -Wredundant-decls \ -Wdisabled-optimization \ -Wdouble-promotion \ - -Wstrict-aliasing=3 # not accepted by clang \ - -Wno-aggressive-loop-optimizations # not accepted by clang \ - -Wlogical-op # not accepted by clang \ + -Wstrict-aliasing=3 \ + -Wno-aggressive-loop-optimizations \ + -Wlogical-op \ + -Werror \ + # -pedantic # warns if we use jump tables \ # the next warnings generate too much noise, so they are disabled # -Wconversion -Wno-sign-conversion \ # -Wsign-conversion \ @@ -41,14 +42,12 @@ CWARNS= $(CWARNSCPP) $(CWARNSC) # -g -DLUA_USER_H='"ltests.h"' # -pg -malign-double # -DLUA_USE_CTYPE -DLUA_USE_APICHECK -# (in clang, '-ftrapv' for runtime checks of integer overflows) +# ('-ftrapv' for runtime checks of integer overflows) # -fsanitize=undefined -ftrapv -fno-inline TESTS= -DLUA_USER_H='"ltests.h"' -O0 -# -mtune=native -fomit-frame-pointer -# -fno-stack-protector -# -DLUA_NILINTABLE -LOCAL = $(TESTS) $(CWARNS) -g -DEXTERNMEMCHECK + +# LOCAL = $(TESTS) $(CWARNS) -g @@ -59,8 +58,8 @@ MYLIBS= -ldl -lreadline CC= gcc -CFLAGS= -Wall -O2 $(MYCFLAGS) -Wfatal-errors -AR= ar rcu +CFLAGS= -Wall -O2 $(MYCFLAGS) -Wfatal-errors -fno-stack-protector -fno-common +AR= ar rc RANLIB= ranlib RM= rm -f From f59e6a93c0ad38a27a420e51abf8f13d962446b5 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 19 Jun 2018 16:23:57 -0300 Subject: [PATCH 0304/1145] opening functions must be exported! --- luaconf.h | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/luaconf.h b/luaconf.h index f1ca51f5a0..9cddb263b8 100644 --- a/luaconf.h +++ b/luaconf.h @@ -1,5 +1,5 @@ /* -** $Id: luaconf.h,v 1.269 2018/06/15 15:49:28 roberto Exp roberto $ +** $Id: luaconf.h,v 1.270 2018/06/18 12:51:05 roberto Exp roberto $ ** Configuration file for Lua ** See Copyright Notice in lua.h */ @@ -255,12 +255,10 @@ /* -** More often than not the libs go together with the core; -** Functions from the auxiliary library must be exported, -** but opening functions do not. +** More often than not the libs go together with the core. */ #define LUALIB_API LUA_API -#define LUAMOD_API LUAI_FUNC +#define LUAMOD_API LUA_API /* From 7c519dfbd0c68b952f0849e01deaa3750e1f8153 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 9 Jul 2018 12:33:01 -0300 Subject: [PATCH 0305/1145] Added manual and tests for version 5.4-w2 --- all | 7 + manual/2html | 518 +++ manual/manual.of | 8704 +++++++++++++++++++++++++++++++++++++++++ testes/all.lua | 294 ++ testes/api.lua | 1264 ++++++ testes/attrib.lua | 487 +++ testes/big.lua | 82 + testes/bitwise.lua | 346 ++ testes/bwcoercion.lua | 78 + testes/calls.lua | 435 ++ testes/closure.lua | 271 ++ testes/code.lua | 347 ++ testes/constructs.lua | 302 ++ testes/coroutine.lua | 918 +++++ testes/db.lua | 948 +++++ testes/errors.lua | 554 +++ testes/events.lua | 476 +++ testes/files.lua | 832 ++++ testes/gc.lua | 661 ++++ testes/goto.lua | 256 ++ testes/libs/lib1.c | 44 + testes/libs/lib11.c | 10 + testes/libs/lib2.c | 23 + testes/libs/lib21.c | 10 + testes/libs/makefile | 26 + testes/literals.lua | 302 ++ testes/locals.lua | 181 + testes/main.lua | 381 ++ testes/math.lua | 931 +++++ testes/nextvar.lua | 669 ++++ testes/pm.lua | 374 ++ testes/sort.lua | 310 ++ testes/strings.lua | 382 ++ testes/tpack.lua | 324 ++ testes/utf8.lua | 210 + testes/vararg.lua | 151 + testes/verybig.lua | 152 + 37 files changed, 22260 insertions(+) create mode 100755 all create mode 100755 manual/2html create mode 100644 manual/manual.of create mode 100755 testes/all.lua create mode 100644 testes/api.lua create mode 100644 testes/attrib.lua create mode 100644 testes/big.lua create mode 100755 testes/bitwise.lua create mode 100644 testes/bwcoercion.lua create mode 100644 testes/calls.lua create mode 100644 testes/closure.lua create mode 100644 testes/code.lua create mode 100644 testes/constructs.lua create mode 100644 testes/coroutine.lua create mode 100644 testes/db.lua create mode 100644 testes/errors.lua create mode 100644 testes/events.lua create mode 100644 testes/files.lua create mode 100644 testes/gc.lua create mode 100644 testes/goto.lua create mode 100644 testes/libs/lib1.c create mode 100644 testes/libs/lib11.c create mode 100644 testes/libs/lib2.c create mode 100644 testes/libs/lib21.c create mode 100644 testes/libs/makefile create mode 100644 testes/literals.lua create mode 100644 testes/locals.lua create mode 100644 testes/main.lua create mode 100644 testes/math.lua create mode 100644 testes/nextvar.lua create mode 100644 testes/pm.lua create mode 100644 testes/sort.lua create mode 100644 testes/strings.lua create mode 100644 testes/tpack.lua create mode 100644 testes/utf8.lua create mode 100644 testes/vararg.lua create mode 100644 testes/verybig.lua diff --git a/all b/all new file mode 100755 index 0000000000..12acaf3650 --- /dev/null +++ b/all @@ -0,0 +1,7 @@ +cd testes +ulimit -S -s 2000 +if { ../lua all.lua; } then + echo -e "\n\n final OK!!!!\n\n" +else + echo -e "\n\n >>>> BUG!!!!\n\n" +fi diff --git a/manual/2html b/manual/2html new file mode 100755 index 0000000000..04b2c61ed0 --- /dev/null +++ b/manual/2html @@ -0,0 +1,518 @@ +#!/usr/bin/env lua5.3 + + +-- special marks: +-- \1 - paragraph (empty line) +-- \4 - remove spaces around it +-- \3 - ref (followed by label|) + +--------------------------------------------------------------- +header = [[ + + + + +Lua 5.4 Reference Manual + + + + + + + +


+

+[Lua logo] +Lua 5.4 Reference Manual +

+ +by Roberto Ierusalimschy, Luiz Henrique de Figueiredo, Waldemar Celes +

+ +Copyright +© 2018 Lua.org, PUC-Rio. All rights reserved. + +


+ + +

+ +]] + +footer = "\n\n\n\n" + +local seefmt = '(see %s)' + +if arg[1] == 'port' then + seefmt = '(ver %s)' + header = string.gsub(header, "by (.-)\n", + "%1\n

Tradução: Sérgio Queiroz de Medeiros", 1) + header = string.gsub(header, "Lua (%d+.%d+) Reference Manual", + "Manual de Referência de Lua %1") + header = string.gsub(header, "All rights reserved", + "Todos os direitos reservados") +end + + +--------------------------------------------------------------- + +local function compose (f,g) + assert(f and g) + return function (s) return g(f(s)) end +end + +local function concat (f, g) + assert(f and g) + return function (s) return f(s) .. g(s) end +end + + +local Tag = {} + + +setmetatable(Tag, { + __index = function (t, tag) + local v = function (n, att) + local e = "" + if type(att) == "table" then + for k,v in pairs(att) do e = string.format('%s %s="%s"', e, k, v) end + end + if n then + return string.format("<%s%s>%s", tag, e, n, tag) + else + return string.format("<%s%s>", tag, e) + end + end + t[tag] = v + return v + end +}) + + + +--------------------------------------------------------------- +local labels = {} + + +local function anchor (text, label, link, textlink) + if labels[label] then + error("label " .. label .. " already defined") + end + labels[label] = {text = textlink, link = link} + return Tag.a(text, {name=link}) +end + +local function makeref (label) + assert(not string.find(label, "|")) + return string.format("\3%s\3", label) +end + +local function ref (label) + local l = labels[label] + if not l then + io.stderr:write("label ", label, " undefined\n") + return "@@@@@@@" + else + return Tag.a(l.text, {href="#"..l.link}) + end +end + +--------------------------------------------------------------- +local function nopara (t) + t = string.gsub(t, "\1", "\n\n") + t = string.gsub(t, "

%s*

", "") + return t +end + +local function fixpara (t) + t = string.gsub(t, "\1", "\n

\n\n

\n") + t = string.gsub(t, "

%s*

", "") + return t +end + +local function antipara (t) + return "

\n" .. t .. "

" +end + + +Tag.pre = compose(Tag.pre, antipara) +Tag.ul = compose(Tag.ul, antipara) + +--------------------------------------------------------------- +local Gfoots = 0 +local footnotes = {} + +local line = Tag.hr(nil) + +local function dischargefoots () + if #footnotes == 0 then return "" end + local fn = table.concat(footnotes) + footnotes = {} + return line .. Tag.h3"footnotes:" .. fn .. line +end + + +local Glists = 0 +local listings = {} + +local function dischargelist () + if #listings == 0 then return "" end + local l = listings + listings = {} + return line .. table.concat(l, line..line) .. line +end + +--------------------------------------------------------------- +local counters = { +h1 = {val = 1}, +h2 = {father = "h1", val = 1}, +h3 = {father = "h2", val = 1}, +listing = {father = "h1", val = 1}, +} + +local function inccounter (count) + counters[count].val = counters[count].val + 1 + for c, v in pairs(counters) do + if v.father == count then v.val = 1 end + end +end + +local function getcounter (count) + local c = counters[count] + if c.father then + return getcounter(c.father) .. "." .. c.val + else + return c.val .. "" + end +end +--------------------------------------------------------------- + + +local function fixed (x) + return function () return x end +end + +local function id (x) return x end + + +local function prepos (x, y) + assert(x and y) + return function (s) return string.format("%s%s%s", x, s, y) end +end + + +local rw = Tag.b + + + + +local function LuaName (name) + return Tag.code(name) +end + + +local function getparam (s) + local i, e = string.find(s, "^[^%s@|]+|") + if not i then return nil, s + else return string.sub(s, i, e - 1), string.sub(s, e + 1) + end +end + + +local function gettitle (h) + local title, p = assert(string.match(h, "(.-)()")) + return title, string.sub(h, p) +end + +local function getparamtitle (what, h, nonum) + local label, title, c, count + label, h = getparam(h) + title, h = gettitle(h) + if not nonum then + count = getcounter(what) + inccounter(what) + c = string.format("%s – ", count) + else + c = "" + end + label = label or count + if label then + title = anchor(title, label, count, "§"..count) + end + title = string.format("%s%s", c, title) + return title, h +end + +local function section (what, nonum) + return function (h) + local title + title, h = getparamtitle(what, h, nonum) + local fn = what == "h1" and dischargefoots() or "" + h = fixpara(Tag.p(h)) + return "

\n" .. Tag[what](title) .. h .. fn .. + dischargelist() .. "

" + end +end + + +local function verbatim (s) + s = nopara(s) + s = string.gsub(s, "\n", "\n ") + s = string.gsub(s, "\n%s*$", "\n") + return Tag.pre(s) +end + + +local function verb (s) + return Tag.code(s) +end + + +local function lua2link (e) + return string.find(e, "luaL?_") and e or "pdf-"..e +end + + +local verbfixed = verb + + +local Tex = { + +ANSI = function (func) + return "ISO C function " .. Tag.code(func) + end, +At = fixed"@", +B = Tag.b, +bigskip = fixed"", +bignum = id, +C = fixed"", +Ci = prepos(""), +CId = function (func) + return "C function " .. Tag.code(func) + end, +chapter = section"h1", +Char = compose(verbfixed, prepos("'", "'")), +Cdots = fixed"···", +Close = fixed"}", +col = Tag.td, +defid = function (name) + local l = lua2link(name) + local c = Tag.code(name) + return anchor(c, l, l, c) + end, +def = Tag.em, +description = compose(nopara, Tag.ul), +Em = fixed("\4" .. "—" .. "\4"), +emph = Tag.em, +emphx = Tag.em, -- emphasis plus index (if there was an index) +En = fixed("–"), +format = fixed"", +["false"] = fixed(Tag.b"false"), +id = Tag.code, +idx = Tag.code, +index = fixed"", +Lidx = fixed"", -- Tag.code, +ldots = fixed"...", +x = id, +itemize = compose(nopara, Tag.ul), +leq = fixed"≤", +Lid = function (s) + return makeref(lua2link(s)) + end, +M = Tag.em, +N = function (s) return (string.gsub(s, " ", " ")) end, +NE = id, -- tag"foreignphrase", +num = id, +["nil"] = fixed(Tag.b"nil"), +Open = fixed"{", +part = section("h1", true), +Pat = compose(verbfixed, prepos("'", "'")), +preface = section("h1", true), +psect = section("h2", true), +Q = prepos('"', '"'), +refchp = makeref, +refcode = makeref, +refsec = makeref, + +pi = fixed"π", +rep = Tag.em, -- compose(prepos("<", ">"), Tag.em), +Rw = rw, +rw = rw, +sb = Tag.sub, +sp = Tag.sup, +St = compose(verbfixed, prepos('"', '"')), +sect1 = section"h1", +sect2 = section"h2", +sect3 = section"h3", +sect4 = section("h4", true), +simplesect = id, +Tab2 = function (s) return Tag.table(s, {border=1}) end, +row = Tag.tr, +title = Tag.title, +todo = Tag.todo, +["true"] = fixed(Tag.b"true"), +T = verb, + +item = function (s) + local t, p = string.match(s, "^([^\n|]+)|()") + if t then + s = string.sub(s, p) + s = Tag.b(t..": ") .. s + end + return Tag.li(fixpara(s)) + end, + +verbatim = verbatim, + +manual = id, + + +-- for the manual + +link =function (s) + local l, t = getparam(s) + assert(l) + return string.format("%s (%s)", t, makeref(l)) +end, + +see = function (s) return string.format(seefmt, makeref(s)) end, +See = makeref, +seeC = function (s) + return string.format(seefmt, makeref(s)) + end, + +seeF = function (s) + return string.format(seefmt, makeref(lua2link(s))) + end, + +APIEntry = function (e) + local h, name + h, e = string.match(e, "^%s*(.-)%s*|(.*)$") + name = string.match(h, "(luaL?_[%w_]+)%)? +%(") or + string.match(h, "luaL?_[%w_]+") + local a = anchor(Tag.code(name), name, name, Tag.code(name)) + local apiicmd, ne = string.match(e, "^(.-)(.*)") +--io.stderr:write(e) + if not apiicmd then + return antipara(Tag.hr() .. Tag.h3(a)) .. Tag.pre(h) .. e + else + return antipara(Tag.hr() .. Tag.h3(a)) .. apiicmd .. Tag.pre(h) .. ne + end +end, + +LibEntry = function (e) + local h, name + h, e = string.match(e, "^(.-)|(.*)$") + name = string.gsub(h, " (.+", "") + local l = lua2link(name) + local a = anchor(Tag.code(h), l, l, Tag.code(name)) + return Tag.hr() .. Tag.h3(a) .. e +end, + +Produc = compose(nopara, Tag.pre), +producname = prepos("\t", " ::= "), +Or = fixed" | ", +VerBar = fixed"|", -- vertical bar +OrNL = fixed" | \4", +bnfNter = prepos("", ""), +bnfopt = prepos("[", "]"), +bnfrep = prepos("{", "}"), +bnfter = compose(Tag.b, prepos("‘", "’")), +producbody = function (s) + s = string.gsub(s, "%s+", " ") + s = string.gsub(s, "\4", "\n\t\t") + return s + end, + +apii = function (s) + local pop,push,err = string.match(s, "^(.-),(.-),(.*)$") + if pop ~= "?" and string.find(pop, "%W") then + pop = "(" .. pop .. ")" + end + if push ~= "?" and string.find(push, "%W") then + push = "(" .. push .. ")" + end + err = (err == "-") and "–" or Tag.em(err) + return Tag.span( + string.format("[-%s, +%s, %s]", pop, push, err), + {class="apii"} + ) + end, +} + +local others = prepos("?? "," ??") + +local function trata (t) + t = string.gsub(t, "@(%w+)(%b{})", function (w, f) + f = trata(string.sub(f, 2, -2)) + if type(Tex[w]) ~= "function" then + io.stderr:write(w .. "\n") + return others(f) + else + return Tex[w](f, w) + end + end) + return t +end + + +--------------------------------------------------------------------- +--------------------------------------------------------------------- + +-- read whole book +t = io.read"*a" + +t = string.gsub(t, "[<>&\128-\255]", + {["<"] = "<", + [">"] = ">", + ["&"] = "&", + ["\170"] = "ª", + ["\186"] = "º", + ["\192"] = "À", + ["\193"] = "Á", + ["\194"] = "Â", + ["\195"] = "Ã", + ["\199"] = "Ç", + ["\201"] = "É", + ["\202"] = "Ê", + ["\205"] = "Í", + ["\211"] = "Ó", + ["\212"] = "Ô", + ["\218"] = "Ú", + ["\224"] = "à", + ["\225"] = "á", + ["\226"] = "â", + ["\227"] = "ã", + ["\231"] = "ç", + ["\233"] = "é", + ["\234"] = "ê", + ["\237"] = "í", + ["\243"] = "ó", + ["\244"] = "ô", + ["\245"] = "õ", + ["\250"] = "ú", + ["\252"] = "ü" + }) + +t = string.gsub(t, "\n\n+", "\1") + + + +-- complete macros with no arguments +t = string.gsub(t, "(@%w+)([^{%w])", "%1{}%2") + +t = trata(t) + +-- correct references +t = string.gsub(t, "\3(.-)\3", ref) + +-- remove extra space (??) +t = string.gsub(t, "%s*\4%s*", "") + +t = nopara(t) + +-- HTML 3.2 does not need

(but complains when it is in wrong places :) +t = string.gsub(t, "

", "") + +io.write(header, t, footer) + diff --git a/manual/manual.of b/manual/manual.of new file mode 100644 index 0000000000..935990d0ed --- /dev/null +++ b/manual/manual.of @@ -0,0 +1,8704 @@ +@Ci{$Id: manual.of,v 1.175 2018/06/18 19:17:35 roberto Exp $} +@C{[(-------------------------------------------------------------------------} +@manual{ + +@sect1{@title{Introduction} + +Lua is a powerful, efficient, lightweight, embeddable scripting language. +It supports procedural programming, +object-oriented programming, functional programming, +data-driven programming, and data description. + +Lua combines simple procedural syntax with powerful data description +constructs based on associative arrays and extensible semantics. +Lua is dynamically typed, +runs by interpreting bytecode with a register-based +virtual machine, +and has automatic memory management with +incremental garbage collection, +making it ideal for configuration, scripting, +and rapid prototyping. + +Lua is implemented as a library, written in @emphx{clean C}, +the common subset of @N{Standard C} and C++. +The Lua distribution includes a host program called @id{lua}, +which uses the Lua library to offer a complete, +standalone Lua interpreter, +for interactive or batch use. +Lua is intended to be used both as a powerful, lightweight, +embeddable scripting language for any program that needs one, +and as a powerful but lightweight and efficient stand-alone language. + +As an extension language, Lua has no notion of a @Q{main} program: +it works @emph{embedded} in a host client, +called the @emph{embedding program} or simply the @emphx{host}. +(Frequently, this host is the stand-alone @id{lua} program.) +The host program can invoke functions to execute a piece of Lua code, +can write and read Lua variables, +and can register @N{C functions} to be called by Lua code. +Through the use of @N{C functions}, Lua can be augmented to cope with +a wide range of different domains, +thus creating customized programming languages sharing a syntactical framework. + +Lua is free software, +and is provided as usual with no guarantees, +as stated in its license. +The implementation described in this manual is available +at Lua's official web site, @id{www.lua.org}. + +Like any other reference manual, +this document is dry in places. +For a discussion of the decisions behind the design of Lua, +see the technical papers available at Lua's web site. +For a detailed introduction to programming in Lua, +see Roberto's book, @emphx{Programming in Lua}. + +} + + +@C{-------------------------------------------------------------------------} +@sect1{basic| @title{Basic Concepts} + +This section describes the basic concepts of the language. + +@sect2{TypesSec| @title{Values and Types} + +Lua is a dynamically typed language. +This means that +variables do not have types; only values do. +There are no type definitions in the language. +All values carry their own type. + +All values in Lua are first-class values. +This means that all values can be stored in variables, +passed as arguments to other functions, and returned as results. + +There are eight @x{basic types} in Lua: +@def{nil}, @def{boolean}, @def{number}, +@def{string}, @def{function}, @def{userdata}, +@def{thread}, and @def{table}. +The type @emph{nil} has one single value, @nil, +whose main property is to be different from any other value; +it usually represents the absence of a useful value. +The type @emph{boolean} has two values, @false and @true. +Both @nil and @false make a condition false; +any other value makes it true. +The type @emph{number} represents both +integer numbers and real (floating-point) numbers. +The type @emph{string} represents immutable sequences of bytes. +@index{eight-bit clean} +Lua is 8-bit clean: +strings can contain any 8-bit value, +including @x{embedded zeros} (@Char{\0}). +Lua is also encoding-agnostic; +it makes no assumptions about the contents of a string. + +The type @emph{number} uses two internal representations, +or two @x{subtypes}, +one called @def{integer} and the other called @def{float}. +Lua has explicit rules about when each representation is used, +but it also converts between them automatically as needed @see{coercion}. +Therefore, +the programmer may choose to mostly ignore the difference +between integers and floats +or to assume complete control over the representation of each number. +Standard Lua uses 64-bit integers and double-precision (64-bit) floats, +but you can also compile Lua so that it +uses 32-bit integers and/or single-precision (32-bit) floats. +The option with 32 bits for both integers and floats +is particularly attractive +for small machines and embedded systems. +(See macro @id{LUA_32BITS} in file @id{luaconf.h}.) + +Lua can call (and manipulate) functions written in Lua and +functions written in C @see{functioncall}. +Both are represented by the type @emph{function}. + +The type @emph{userdata} is provided to allow arbitrary @N{C data} to +be stored in Lua variables. +A userdata value represents a block of raw memory. +There are two kinds of userdata: +@emphx{full userdata}, +which is an object with a block of memory managed by Lua, +and @emphx{light userdata}, +which is simply a @N{C pointer} value. +Userdata has no predefined operations in Lua, +except assignment and identity test. +By using @emph{metatables}, +the programmer can define operations for full userdata values +@see{metatable}. +Userdata values cannot be created or modified in Lua, +only through the @N{C API}. +This guarantees the integrity of data owned by the host program. + +The type @def{thread} represents independent threads of execution +and it is used to implement coroutines @see{coroutine}. +Lua threads are not related to operating-system threads. +Lua supports coroutines on all systems, +even those that do not support threads natively. + +The type @emph{table} implements @x{associative arrays}, +that is, @x{arrays} that can have as indices not only numbers, +but any Lua value except @nil and @x{NaN}. +(@emphx{Not a Number} is a special floating-point value +used by the @x{IEEE 754} standard to represent +undefined or unrepresentable numerical results, such as @T{0/0}.) +Tables can be @emph{heterogeneous}; +that is, they can contain values of all types (except @nil). +Any key with value @nil is not considered part of the table. +Conversely, any key that is not part of a table has +an associated value @nil. + +Tables are the sole data-structuring mechanism in Lua; +they can be used to represent ordinary arrays, lists, +symbol tables, sets, records, graphs, trees, etc. +To represent @x{records}, Lua uses the field name as an index. +The language supports this representation by +providing @id{a.name} as syntactic sugar for @T{a["name"]}. +There are several convenient ways to create tables in Lua +@see{tableconstructor}. + +Like indices, +the values of table fields can be of any type. +In particular, +because functions are first-class values, +table fields can contain functions. +Thus tables can also carry @emph{methods} @see{func-def}. + +The indexing of tables follows +the definition of raw equality in the language. +The expressions @T{a[i]} and @T{a[j]} +denote the same table element +if and only if @id{i} and @id{j} are raw equal +(that is, equal without metamethods). +In particular, floats with integral values +are equal to their respective integers +(e.g., @T{1.0 == 1}). +To avoid ambiguities, +any float with integral value used as a key +is converted to its respective integer. +For instance, if you write @T{a[2.0] = true}, +the actual key inserted into the table will be the +integer @T{2}. +(On the other hand, +2 and @St{2} are different Lua values and therefore +denote different table entries.) + + +Tables, functions, threads, and (full) userdata values are @emph{objects}: +variables do not actually @emph{contain} these values, +only @emph{references} to them. +Assignment, parameter passing, and function returns +always manipulate references to such values; +these operations do not imply any kind of copy. + +The library function @Lid{type} returns a string describing the type +of a given value @see{predefined}. + +} + +@sect2{globalenv| @title{Environments and the Global Environment} + +As will be discussed in @refsec{variables} and @refsec{assignment}, +any reference to a free name +(that is, a name not bound to any declaration) @id{var} +is syntactically translated to @T{_ENV.var}. +Moreover, every chunk is compiled in the scope of +an external local variable named @id{_ENV} @see{chunks}, +so @id{_ENV} itself is never a free name in a chunk. + +Despite the existence of this external @id{_ENV} variable and +the translation of free names, +@id{_ENV} is a completely regular name. +In particular, +you can define new variables and parameters with that name. +Each reference to a free name uses the @id{_ENV} that is +visible at that point in the program, +following the usual visibility rules of Lua @see{visibility}. + +Any table used as the value of @id{_ENV} is called an @def{environment}. + +Lua keeps a distinguished environment called the @def{global environment}. +This value is kept at a special index in the C registry @see{registry}. +In Lua, the global variable @Lid{_G} is initialized with this same value. +(@Lid{_G} is never used internally.) + +When Lua loads a chunk, +the default value for its @id{_ENV} upvalue +is the global environment @seeF{load}. +Therefore, by default, +free names in Lua code refer to entries in the global environment +(and, therefore, they are also called @def{global variables}). +Moreover, all standard libraries are loaded in the global environment +and some functions there operate on that environment. +You can use @Lid{load} (or @Lid{loadfile}) +to load a chunk with a different environment. +(In C, you have to load the chunk and then change the value +of its first upvalue.) + +} + +@sect2{error| @title{Error Handling} + +Because Lua is an embedded extension language, +all Lua actions start from @N{C code} in the host program +calling a function from the Lua library. +(When you use Lua standalone, +the @id{lua} application is the host program.) +Whenever an error occurs during +the compilation or execution of a Lua chunk, +control returns to the host, +which can take appropriate measures +(such as printing an error message). + +Lua code can explicitly generate an error by calling the +@Lid{error} function. +If you need to catch errors in Lua, +you can use @Lid{pcall} or @Lid{xpcall} +to call a given function in @emphx{protected mode}. + +Whenever there is an error, +an @def{error object} (also called an @def{error message}) +is propagated with information about the error. +Lua itself only generates errors whose error object is a string, +but programs may generate errors with +any value as the error object. +It is up to the Lua program or its host to handle such error objects. + + +When you use @Lid{xpcall} or @Lid{lua_pcall}, +you may give a @def{message handler} +to be called in case of errors. +This function is called with the original error object +and returns a new error object. +It is called before the error unwinds the stack, +so that it can gather more information about the error, +for instance by inspecting the stack and creating a stack traceback. +This message handler is still protected by the protected call; +so, an error inside the message handler +will call the message handler again. +If this loop goes on for too long, +Lua breaks it and returns an appropriate message. +(The message handler is called only for regular runtime errors. +It is not called for memory-allocation errors +nor for errors while running finalizers.) + +} + +@sect2{metatable| @title{Metatables and Metamethods} + +Every value in Lua can have a @emph{metatable}. +This @def{metatable} is an ordinary Lua table +that defines the behavior of the original value +under certain special operations. +You can change several aspects of the behavior +of operations over a value by setting specific fields in its metatable. +For instance, when a non-numeric value is the operand of an addition, +Lua checks for a function in the field @St{__add} of the value's metatable. +If it finds one, +Lua calls this function to perform the addition. + +The key for each event in a metatable is a string +with the event name prefixed by two underscores; +the corresponding values are called @def{metamethods}. +In the previous example, the key is @St{__add} +and the metamethod is the function that performs the addition. +Unless stated otherwise, +metamethods should be function values. + +You can query the metatable of any value +using the @Lid{getmetatable} function. +Lua queries metamethods in metatables using a raw access @seeF{rawget}. +So, to retrieve the metamethod for event @id{ev} in object @id{o}, +Lua does the equivalent to the following code: +@verbatim{ +rawget(getmetatable(@rep{o}) or {}, "__@rep{ev}") +} + +You can replace the metatable of tables +using the @Lid{setmetatable} function. +You cannot change the metatable of other types from Lua code +(except by using the @link{debuglib|debug library}); +you should use the @N{C API} for that. + +Tables and full userdata have individual metatables +(although multiple tables and userdata can share their metatables). +Values of all other types share one single metatable per type; +that is, there is one single metatable for all numbers, +one for all strings, etc. +By default, a value has no metatable, +but the string library sets a metatable for the string type @see{strlib}. + +A metatable controls how an object behaves in +arithmetic operations, bitwise operations, +order comparisons, concatenation, length operation, calls, and indexing. +A metatable also can define a function to be called +when a userdata or a table is @link{GC|garbage collected}. + +For the unary operators (negation, length, and bitwise NOT), +the metamethod is computed and called with a dummy second operand, +equal to the first one. +This extra operand is only to simplify Lua's internals +(by making these operators behave like a binary operation) +and may be removed in future versions. +(For most uses this extra operand is irrelevant.) + +A detailed list of events controlled by metatables is given next. +Each operation is identified by its corresponding key. + +@description{ + +@item{@idx{__add}| +the addition (@T{+}) operation. +If any operand for an addition is not a number +(nor a string coercible to a number), +Lua will try to call a metamethod. +First, Lua will check the first operand (even if it is valid). +If that operand does not define a metamethod for @idx{__add}, +then Lua will check the second operand. +If Lua can find a metamethod, +it calls the metamethod with the two operands as arguments, +and the result of the call +(adjusted to one value) +is the result of the operation. +Otherwise, +it raises an error. +} + +@item{@idx{__sub}| +the subtraction (@T{-}) operation. +Behavior similar to the addition operation. +} + +@item{@idx{__mul}| +the multiplication (@T{*}) operation. +Behavior similar to the addition operation. +} + +@item{@idx{__div}| +the division (@T{/}) operation. +Behavior similar to the addition operation. +} + +@item{@idx{__mod}| +the modulo (@T{%}) operation. +Behavior similar to the addition operation. +} + +@item{@idx{__pow}| +the exponentiation (@T{^}) operation. +Behavior similar to the addition operation. +} + +@item{@idx{__unm}| +the negation (unary @T{-}) operation. +Behavior similar to the addition operation. +} + +@item{@idx{__idiv}| +the floor division (@T{//}) operation. +Behavior similar to the addition operation. +} + +@item{@idx{__band}| +the bitwise AND (@T{&}) operation. +Behavior similar to the addition operation, +except that Lua will try a metamethod +if any operand is neither an integer +nor a value coercible to an integer @see{coercion}. +} + +@item{@idx{__bor}| +the bitwise OR (@T{|}) operation. +Behavior similar to the bitwise AND operation. +} + +@item{@idx{__bxor}| +the bitwise exclusive OR (binary @T{~}) operation. +Behavior similar to the bitwise AND operation. +} + +@item{@idx{__bnot}| +the bitwise NOT (unary @T{~}) operation. +Behavior similar to the bitwise AND operation. +} + +@item{@idx{__shl}| +the bitwise left shift (@T{<<}) operation. +Behavior similar to the bitwise AND operation. +} + +@item{@idx{__shr}| +the bitwise right shift (@T{>>}) operation. +Behavior similar to the bitwise AND operation. +} + +@item{@idx{__concat}| +the concatenation (@T{..}) operation. +Behavior similar to the addition operation, +except that Lua will try a metamethod +if any operand is neither a string nor a number +(which is always coercible to a string). +} + +@item{@idx{__len}| +the length (@T{#}) operation. +If the object is not a string, +Lua will try its metamethod. +If there is a metamethod, +Lua calls it with the object as argument, +and the result of the call +(always adjusted to one value) +is the result of the operation. +If there is no metamethod but the object is a table, +then Lua uses the table length operation @see{len-op}. +Otherwise, Lua raises an error. +} + +@item{@idx{__eq}| +the equal (@T{==}) operation. +Behavior similar to the addition operation, +except that Lua will try a metamethod only when the values +being compared are either both tables or both full userdata +and they are not primitively equal. +The result of the call is always converted to a boolean. +} + +@item{@idx{__lt}| +the less than (@T{<}) operation. +Behavior similar to the addition operation, +except that Lua will try a metamethod only when the values +being compared are neither both numbers nor both strings. +The result of the call is always converted to a boolean. +} + +@item{@idx{__le}| +the less equal (@T{<=}) operation. +Unlike other operations, +the less-equal operation can use two different events. +First, Lua looks for the @idx{__le} metamethod in both operands, +like in the less than operation. +If it cannot find such a metamethod, +then it will try the @idx{__lt} metamethod, +assuming that @T{a <= b} is equivalent to @T{not (b < a)}. +As with the other comparison operators, +the result is always a boolean. +(This use of the @idx{__lt} event can be removed in future versions; +it is also slower than a real @idx{__le} metamethod.) +} + +@item{@idx{__index}| +The indexing access operation @T{table[key]}. +This event happens when @id{table} is not a table or +when @id{key} is not present in @id{table}. +The metamethod is looked up in @id{table}. + +Despite the name, +the metamethod for this event can be either a function or a table. +If it is a function, +it is called with @id{table} and @id{key} as arguments, +and the result of the call +(adjusted to one value) +is the result of the operation. +If it is a table, +the final result is the result of indexing this table with @id{key}. +(This indexing is regular, not raw, +and therefore can trigger another metamethod.) +} + +@item{@idx{__newindex}| +The indexing assignment @T{table[key] = value}. +Like the index event, +this event happens when @id{table} is not a table or +when @id{key} is not present in @id{table}. +The metamethod is looked up in @id{table}. + +Like with indexing, +the metamethod for this event can be either a function or a table. +If it is a function, +it is called with @id{table}, @id{key}, and @id{value} as arguments. +If it is a table, +Lua does an indexing assignment to this table with the same key and value. +(This assignment is regular, not raw, +and therefore can trigger another metamethod.) + +Whenever there is a @idx{__newindex} metamethod, +Lua does not perform the primitive assignment. +(If necessary, +the metamethod itself can call @Lid{rawset} +to do the assignment.) +} + +@item{@idx{__call}| +The call operation @T{func(args)}. +This event happens when Lua tries to call a non-function value +(that is, @id{func} is not a function). +The metamethod is looked up in @id{func}. +If present, +the metamethod is called with @id{func} as its first argument, +followed by the arguments of the original call (@id{args}). +All results of the call +are the result of the operation. +(This is the only metamethod that allows multiple results.) +} + +} + +It is a good practice to add all needed metamethods to a table +before setting it as a metatable of some object. +In particular, the @idx{__gc} metamethod works only when this order +is followed @see{finalizers}. + +Because metatables are regular tables, +they can contain arbitrary fields, +not only the event names defined above. +Some functions in the standard library +(e.g., @Lid{tostring}) +use other fields in metatables for their own purposes. + +} + +@sect2{GC| @title{Garbage Collection} + +Lua performs automatic memory management. +This means that +you do not have to worry about allocating memory for new objects +or freeing it when the objects are no longer needed. +Lua manages memory automatically by running +a @def{garbage collector} to collect all @emph{dead objects} +(that is, objects that are no longer accessible from Lua). +All memory used by Lua is subject to automatic management: +strings, tables, userdata, functions, threads, internal structures, etc. + +The garbage collector (GC) in Lua can work in two modes: +incremental and generational. + +The default GC mode with the default parameters +are adequate for most uses. +Programs that waste a large proportion of its time +allocating and freeing memory can benefit from other settings. +Keep in mind that the GC behavior is non-portable +both across platforms and across different Lua releases; +therefore, optimal settings are also non-portable. + +You can change the GC mode and parameters by calling +@Lid{lua_gc} in C +or @Lid{collectgarbage} in Lua. +You can also use these functions to control +the collector directly (e.g., stop and restart it). + +@sect3{@title{Incremental Garbage Collection} + +In incremental mode, +each GC cycle performs a mark-and-sweep collection in small steps +interleaved with the program's execution. +In this mode, +the collector uses three numbers to control its garbage-collection cycles: +the @def{garbage-collector pause}, +the @def{garbage-collector step multiplier}, +and the @def{garbage-collector step size}. + +The garbage-collector pause +controls how long the collector waits before starting a new cycle. +The collector starts a new cycle when the use of memory +hits @M{n%} of the use after the previous collection. +Larger values make the collector less aggressive. +Values smaller than 100 mean the collector will not wait to +start a new cycle. +A value of 200 means that the collector waits for the total memory in use +to double before starting a new cycle. +The default value is 200; the maximum value is 1000. + +The garbage-collector step multiplier +controls the relative speed of the collector relative to +memory allocation, +that is, +how many elements it marks or sweeps for each +kilobyte of memory allocated. +Larger values make the collector more aggressive but also increase +the size of each incremental step. +You should not use values smaller than 100, +because they make the collector too slow and +can result in the collector never finishing a cycle. +The default value is 100; the maximum value is 1000. + +The garbage-collector step size controls the +size of each incremental step, +specifically how many bytes the interpreter allocates +before performing a step. +This parameter is logarithmic: +A value of @M{n} means the interpreter will allocate @M{2@sp{n}} +bytes between steps and perform equivalent work during the step. +A large value (e.g., 60) makes the collector a stop-the-world +(non-incremental) collector. +The default value is 13, +which makes for steps of approximately @N{8 Kbytes}. + +} + +@sect3{@title{Generational Garbage Collection} + +In generational mode, +the collector does frequent @emph{minor} collections, +which traverses only objects recently created. +If after a minor collection the use of memory is still above a limit, +the collector does a @emph{major} collection, +which traverses all objects. +The generational mode uses two parameters: +the @def{major multiplier} and the @def{the minor multiplier}. + +The major multiplier controls the frequency of major collections. +For a major multiplier @M{x}, +a new major collection will be done when memory +grows @M{x%} larger than the memory in use after the previous major +collection. +For instance, for a multiplier of 100, +the collector will do a major collection when the use of memory +gets larger than twice the use after the previous collection. +The default value is 100; the maximum value is 1000. + +The minor multiplier controls the frequency of minor collections. +For a minor multiplier @M{x}, +a new minor collection will be done when memory +grows @M{x%} larger than the memory in use after the previous major +collection. +For instance, for a multiplier of 20, +the collector will do a minor collection when the use of memory +gets 20% larger than the use after the previous major collection. +The default value is 20; the maximum value is 200. + +} + +@sect3{finalizers| @title{Garbage-Collection Metamethods} + +You can set garbage-collector metamethods for tables +and, using the @N{C API}, +for full userdata @see{metatable}. +These metamethods are also called @def{finalizers}. +Finalizers allow you to coordinate Lua's garbage collection +with external resource management +(such as closing files, network or database connections, +or freeing your own memory). + +For an object (table or userdata) to be finalized when collected, +you must @emph{mark} it for finalization. +@index{mark (for finalization)} +You mark an object for finalization when you set its metatable +and the metatable has a field indexed by the string @St{__gc}. +Note that if you set a metatable without a @idx{__gc} field +and later create that field in the metatable, +the object will not be marked for finalization. + +When a marked object becomes garbage, +it is not collected immediately by the garbage collector. +Instead, Lua puts it in a list. +After the collection, +Lua goes through that list. +For each object in the list, +it checks the object's @idx{__gc} metamethod: +If it is a function, +Lua calls it with the object as its single argument; +if the metamethod is not a function, +Lua simply ignores it. + +At the end of each garbage-collection cycle, +the finalizers for objects are called in +the reverse order that the objects were marked for finalization, +among those collected in that cycle; +that is, the first finalizer to be called is the one associated +with the object marked last in the program. +The execution of each finalizer may occur at any point during +the execution of the regular code. + +Because the object being collected must still be used by the finalizer, +that object (and other objects accessible only through it) +must be @emph{resurrected} by Lua.@index{resurrection} +Usually, this resurrection is transient, +and the object memory is freed in the next garbage-collection cycle. +However, if the finalizer stores the object in some global place +(e.g., a global variable), +then the resurrection is permanent. +Moreover, if the finalizer marks a finalizing object for finalization again, +its finalizer will be called again in the next cycle where the +object is unreachable. +In any case, +the object memory is freed only in a GC cycle where +the object is unreachable and not marked for finalization. + +When you close a state @seeF{lua_close}, +Lua calls the finalizers of all objects marked for finalization, +following the reverse order that they were marked. +If any finalizer marks objects for collection during that phase, +these marks have no effect. + +} + +@sect3{weak-table| @title{Weak Tables} + +A @def{weak table} is a table whose elements are +@def{weak references}. +A weak reference is ignored by the garbage collector. +In other words, +if the only references to an object are weak references, +then the garbage collector will collect that object. + +A weak table can have weak keys, weak values, or both. +A table with weak values allows the collection of its values, +but prevents the collection of its keys. +A table with both weak keys and weak values allows the collection of +both keys and values. +In any case, if either the key or the value is collected, +the whole pair is removed from the table. +The weakness of a table is controlled by the +@idx{__mode} field of its metatable. +This field, if present, must be one of the following strings: +@St{k}, for a table with weak keys; +@St{v}, for a table with weak values; +or @St{kv}, for a table with both weak keys and values. + +A table with weak keys and strong values +is also called an @def{ephemeron table}. +In an ephemeron table, +a value is considered reachable only if its key is reachable. +In particular, +if the only reference to a key comes through its value, +the pair is removed. + +Any change in the weakness of a table may take effect only +at the next collect cycle. +In particular, if you change the weakness to a stronger mode, +Lua may still collect some items from that table +before the change takes effect. + +Only objects that have an explicit construction +are removed from weak tables. +Values, such as numbers and @x{light @N{C functions}}, +are not subject to garbage collection, +and therefore are not removed from weak tables +(unless their associated values are collected). +Although strings are subject to garbage collection, +they do not have an explicit construction, +and therefore are not removed from weak tables. + +Resurrected objects +(that is, objects being finalized +and objects accessible only through objects being finalized) +have a special behavior in weak tables. +They are removed from weak values before running their finalizers, +but are removed from weak keys only in the next collection +after running their finalizers, when such objects are actually freed. +This behavior allows the finalizer to access properties +associated with the object through weak tables. + +If a weak table is among the resurrected objects in a collection cycle, +it may not be properly cleared until the next cycle. + +} + +} + +@sect2{coroutine| @title{Coroutines} + +Lua supports coroutines, +also called @emphx{collaborative multithreading}. +A coroutine in Lua represents an independent thread of execution. +Unlike threads in multithread systems, however, +a coroutine only suspends its execution by explicitly calling +a yield function. + +You create a coroutine by calling @Lid{coroutine.create}. +Its sole argument is a function +that is the main function of the coroutine. +The @id{create} function only creates a new coroutine and +returns a handle to it (an object of type @emph{thread}); +it does not start the coroutine. + +You execute a coroutine by calling @Lid{coroutine.resume}. +When you first call @Lid{coroutine.resume}, +passing as its first argument +a thread returned by @Lid{coroutine.create}, +the coroutine starts its execution by +calling its main function. +Extra arguments passed to @Lid{coroutine.resume} are passed +as arguments to that function. +After the coroutine starts running, +it runs until it terminates or @emph{yields}. + +A coroutine can terminate its execution in two ways: +normally, when its main function returns +(explicitly or implicitly, after the last instruction); +and abnormally, if there is an unprotected error. +In case of normal termination, +@Lid{coroutine.resume} returns @true, +plus any values returned by the coroutine main function. +In case of errors, @Lid{coroutine.resume} returns @false +plus an error object. + +A coroutine yields by calling @Lid{coroutine.yield}. +When a coroutine yields, +the corresponding @Lid{coroutine.resume} returns immediately, +even if the yield happens inside nested function calls +(that is, not in the main function, +but in a function directly or indirectly called by the main function). +In the case of a yield, @Lid{coroutine.resume} also returns @true, +plus any values passed to @Lid{coroutine.yield}. +The next time you resume the same coroutine, +it continues its execution from the point where it yielded, +with the call to @Lid{coroutine.yield} returning any extra +arguments passed to @Lid{coroutine.resume}. + +Like @Lid{coroutine.create}, +the @Lid{coroutine.wrap} function also creates a coroutine, +but instead of returning the coroutine itself, +it returns a function that, when called, resumes the coroutine. +Any arguments passed to this function +go as extra arguments to @Lid{coroutine.resume}. +@Lid{coroutine.wrap} returns all the values returned by @Lid{coroutine.resume}, +except the first one (the boolean error code). +Unlike @Lid{coroutine.resume}, +@Lid{coroutine.wrap} does not catch errors; +any error is propagated to the caller. + +As an example of how coroutines work, +consider the following code: +@verbatim{ +function foo (a) + print("foo", a) + return coroutine.yield(2*a) +end + +co = coroutine.create(function (a,b) + print("co-body", a, b) + local r = foo(a+1) + print("co-body", r) + local r, s = coroutine.yield(a+b, a-b) + print("co-body", r, s) + return b, "end" +end) + +print("main", coroutine.resume(co, 1, 10)) +print("main", coroutine.resume(co, "r")) +print("main", coroutine.resume(co, "x", "y")) +print("main", coroutine.resume(co, "x", "y")) +} +When you run it, it produces the following output: +@verbatim{ +co-body 1 10 +foo 2 +main true 4 +co-body r +main true 11 -9 +co-body x y +main true 10 end +main false cannot resume dead coroutine +} + +You can also create and manipulate coroutines through the C API: +see functions @Lid{lua_newthread}, @Lid{lua_resume}, +and @Lid{lua_yield}. + +} + +} + + +@C{-------------------------------------------------------------------------} +@sect1{language| @title{The Language} + +This section describes the lexis, the syntax, and the semantics of Lua. +In other words, +this section describes +which tokens are valid, +how they can be combined, +and what their combinations mean. + +Language constructs will be explained using the usual extended BNF notation, +in which +@N{@bnfrep{@rep{a}} means 0} or more @rep{a}'s, and +@N{@bnfopt{@rep{a}} means} an optional @rep{a}. +Non-terminals are shown like @bnfNter{non-terminal}, +keywords are shown like @rw{kword}, +and other terminal symbols are shown like @bnfter{=}. +The complete syntax of Lua can be found in @refsec{BNF} +at the end of this manual. + +@sect2{lexical| @title{Lexical Conventions} + +Lua is a @x{free-form} language. +It ignores spaces (including new lines) and comments +between lexical elements (@x{tokens}), +except as delimiters between @x{names} and @x{keywords}. + +@def{Names} +(also called @def{identifiers}) +in Lua can be any string of letters, +digits, and underscores, +not beginning with a digit and +not being a reserved word. +Identifiers are used to name variables, table fields, and labels. + +The following @def{keywords} are reserved +and cannot be used as names: +@index{reserved words} +@verbatim{ +and break do else elseif end +false for function goto if in +local nil not or repeat return +then true until while +} + +Lua is a case-sensitive language: +@id{and} is a reserved word, but @id{And} and @id{AND} +are two different, valid names. +As a convention, +programs should avoid creating +names that start with an underscore followed by +one or more uppercase letters (such as @Lid{_VERSION}). + +The following strings denote other @x{tokens}: +@verbatim{ ++ - * / % ^ # +& ~ | << >> // +== ~= <= >= < > = +( ) { } [ ] :: +; : , . .. ... +} + +A @def{short literal string} +can be delimited by matching single or double quotes, +and can contain the following C-like escape sequences: +@Char{\a} (bell), +@Char{\b} (backspace), +@Char{\f} (form feed), +@Char{\n} (newline), +@Char{\r} (carriage return), +@Char{\t} (horizontal tab), +@Char{\v} (vertical tab), +@Char{\\} (backslash), +@Char{\"} (quotation mark [double quote]), +and @Char{\'} (apostrophe [single quote]). +A backslash followed by a line break +results in a newline in the string. +The escape sequence @Char{\z} skips the following span +of white-space characters, +including line breaks; +it is particularly useful to break and indent a long literal string +into multiple lines without adding the newlines and spaces +into the string contents. +A short literal string cannot contain unescaped line breaks +nor escapes not forming a valid escape sequence. + +We can specify any byte in a short literal string, +including @x{embedded zeros}, +by its numeric value. +This can be done +with the escape sequence @T{\x@rep{XX}}, +where @rep{XX} is a sequence of exactly two hexadecimal digits, +or with the escape sequence @T{\@rep{ddd}}, +where @rep{ddd} is a sequence of up to three decimal digits. +(Note that if a decimal escape sequence is to be followed by a digit, +it must be expressed using exactly three digits.) + +The @x{UTF-8} encoding of a @x{Unicode} character +can be inserted in a literal string with +the escape sequence @T{\u{@rep{XXX}}} +(note the mandatory enclosing brackets), +where @rep{XXX} is a sequence of one or more hexadecimal digits +representing the character code point. + +Literal strings can also be defined using a long format +enclosed by @def{long brackets}. +We define an @def{opening long bracket of level @rep{n}} as an opening +square bracket followed by @rep{n} equal signs followed by another +opening square bracket. +So, an opening long bracket of @N{level 0} is written as @T{[[}, @C{]]} +an opening long bracket of @N{level 1} is written as @T{[=[}, @C{]]} +and so on. +A @emph{closing long bracket} is defined similarly; +for instance, +a closing long bracket of @N{level 4} is written as @C{[[} @T{]====]}. +A @def{long literal} starts with an opening long bracket of any level and +ends at the first closing long bracket of the same level. +It can contain any text except a closing bracket of the same level. +Literals in this bracketed form can run for several lines, +do not interpret any escape sequences, +and ignore long brackets of any other level. +Any kind of end-of-line sequence +(carriage return, newline, carriage return followed by newline, +or newline followed by carriage return) +is converted to a simple newline. + +For convenience, +when the opening long bracket is immediately followed by a newline, +the newline is not included in the string. +As an example, in a system using ASCII +(in which @Char{a} is coded @N{as 97}, +newline is coded @N{as 10}, and @Char{1} is coded @N{as 49}), +the five literal strings below denote the same string: +@verbatim{ +a = 'alo\n123"' +a = "alo\n123\"" +a = '\97lo\10\04923"' +a = [[alo +123"]] +a = [==[ +alo +123"]==] +} + +Any byte in a literal string not +explicitly affected by the previous rules represents itself. +However, Lua opens files for parsing in text mode, +and the system file functions may have problems with +some control characters. +So, it is safer to represent +non-text data as a quoted literal with +explicit escape sequences for the non-text characters. + +A @def{numeric constant} (or @def{numeral}) +can be written with an optional fractional part +and an optional decimal exponent, +marked by a letter @Char{e} or @Char{E}. +Lua also accepts @x{hexadecimal constants}, +which start with @T{0x} or @T{0X}. +Hexadecimal constants also accept an optional fractional part +plus an optional binary exponent, +marked by a letter @Char{p} or @Char{P}. +A numeric constant with a radix point or an exponent +denotes a float; +otherwise, +if its value fits in an integer, +it denotes an integer. +Examples of valid integer constants are +@verbatim{ +3 345 0xff 0xBEBADA +} +Examples of valid float constants are +@verbatim{ +3.0 3.1416 314.16e-2 0.31416E1 34e1 +0x0.1E 0xA23p-4 0X1.921FB54442D18P+1 +} + +A @def{comment} starts with a double hyphen (@T{--}) +anywhere outside a string. +If the text immediately after @T{--} is not an opening long bracket, +the comment is a @def{short comment}, +which runs until the end of the line. +Otherwise, it is a @def{long comment}, +which runs until the corresponding closing long bracket. + +} + +@sect2{variables| @title{Variables} + +Variables are places that store values. +There are three kinds of variables in Lua: +global variables, local variables, and table fields. + +A single name can denote a global variable or a local variable +(or a function's formal parameter, +which is a particular kind of local variable): +@Produc{ +@producname{var}@producbody{@bnfNter{Name}} +} +@bnfNter{Name} denotes identifiers, as defined in @See{lexical}. + +Any variable name is assumed to be global unless explicitly declared +as a local @see{localvar}. +@x{Local variables} are @emph{lexically scoped}: +local variables can be freely accessed by functions +defined inside their scope @see{visibility}. + +Before the first assignment to a variable, its value is @nil. + +Square brackets are used to index a table: +@Produc{ +@producname{var}@producbody{prefixexp @bnfter{[} exp @bnfter{]}} +} +The meaning of accesses to table fields can be changed via metatables +@see{metatable}. + +The syntax @id{var.Name} is just syntactic sugar for +@T{var["Name"]}: +@Produc{ +@producname{var}@producbody{prefixexp @bnfter{.} @bnfNter{Name}} +} + +An access to a global variable @id{x} +is equivalent to @id{_ENV.x}. +Due to the way that chunks are compiled, +the variable @id{_ENV} itself is never global @see{globalenv}. + +} + +@sect2{stats| @title{Statements} + +Lua supports an almost conventional set of @x{statements}, +similar to those in Pascal or C. +This set includes +assignments, control structures, function calls, +and variable declarations. + +@sect3{@title{Blocks} + +A @x{block} is a list of statements, +which are executed sequentially: +@Produc{ +@producname{block}@producbody{@bnfrep{stat}} +} +Lua has @def{empty statements} +that allow you to separate statements with semicolons, +start a block with a semicolon +or write two semicolons in sequence: +@Produc{ +@producname{stat}@producbody{@bnfter{;}} +} + +Function calls and assignments +can start with an open parenthesis. +This possibility leads to an ambiguity in Lua's grammar. +Consider the following fragment: +@verbatim{ +a = b + c +(print or io.write)('done') +} +The grammar could see it in two ways: +@verbatim{ +a = b + c(print or io.write)('done') + +a = b + c; (print or io.write)('done') +} +The current parser always sees such constructions +in the first way, +interpreting the open parenthesis +as the start of the arguments to a call. +To avoid this ambiguity, +it is a good practice to always precede with a semicolon +statements that start with a parenthesis: +@verbatim{ +;(print or io.write)('done') +} + +A block can be explicitly delimited to produce a single statement: +@Produc{ +@producname{stat}@producbody{@Rw{do} block @Rw{end}} +} +Explicit blocks are useful +to control the scope of variable declarations. +Explicit blocks are also sometimes used to +add a @Rw{return} statement in the middle +of another block @see{control}. + +} + +@sect3{chunks| @title{Chunks} + +The unit of compilation of Lua is called a @def{chunk}. +Syntactically, +a chunk is simply a block: +@Produc{ +@producname{chunk}@producbody{block} +} + +Lua handles a chunk as the body of an anonymous function +with a variable number of arguments +@see{func-def}. +As such, chunks can define local variables, +receive arguments, and return values. +Moreover, such anonymous function is compiled as in the +scope of an external local variable called @id{_ENV} @see{globalenv}. +The resulting function always has @id{_ENV} as its only upvalue, +even if it does not use that variable. + +A chunk can be stored in a file or in a string inside the host program. +To execute a chunk, +Lua first @emph{loads} it, +precompiling the chunk's code into instructions for a virtual machine, +and then Lua executes the compiled code +with an interpreter for the virtual machine. + +Chunks can also be precompiled into binary form; +see program @idx{luac} and function @Lid{string.dump} for details. +Programs in source and compiled forms are interchangeable; +Lua automatically detects the file type and acts accordingly @seeF{load}. + +} + +@sect3{assignment| @title{Assignment} + +Lua allows @x{multiple assignments}. +Therefore, the syntax for assignment +defines a list of variables on the left side +and a list of expressions on the right side. +The elements in both lists are separated by commas: +@Produc{ +@producname{stat}@producbody{varlist @bnfter{=} explist} +@producname{varlist}@producbody{var @bnfrep{@bnfter{,} var}} +@producname{explist}@producbody{exp @bnfrep{@bnfter{,} exp}} +} +Expressions are discussed in @See{expressions}. + +Before the assignment, +the list of values is @emph{adjusted} to the length of +the list of variables.@index{adjustment} +If there are more values than needed, +the excess values are thrown away. +If there are fewer values than needed, +the list is extended with as many @nil's as needed. +If the list of expressions ends with a function call, +then all values returned by that call enter the list of values, +before the adjustment +(except when the call is enclosed in parentheses; see @See{expressions}). + +The assignment statement first evaluates all its expressions +and only then the assignments are performed. +Thus the code +@verbatim{ +i = 3 +i, a[i] = i+1, 20 +} +sets @T{a[3]} to 20, without affecting @T{a[4]} +because the @id{i} in @T{a[i]} is evaluated (to 3) +before it is @N{assigned 4}. +Similarly, the line +@verbatim{ +x, y = y, x +} +exchanges the values of @id{x} and @id{y}, +and +@verbatim{ +x, y, z = y, z, x +} +cyclically permutes the values of @id{x}, @id{y}, and @id{z}. + +An assignment to a global name @T{x = val} +is equivalent to the assignment +@T{_ENV.x = val} @see{globalenv}. + +The meaning of assignments to table fields and +global variables (which are actually table fields, too) +can be changed via metatables @see{metatable}. + +} + +@sect3{control| @title{Control Structures} +The control structures +@Rw{if}, @Rw{while}, and @Rw{repeat} have the usual meaning and +familiar syntax: +@index{while-do statement} +@index{repeat-until statement} +@index{if-then-else statement} +@Produc{ +@producname{stat}@producbody{@Rw{while} exp @Rw{do} block @Rw{end}} +@producname{stat}@producbody{@Rw{repeat} block @Rw{until} exp} +@producname{stat}@producbody{@Rw{if} exp @Rw{then} block + @bnfrep{@Rw{elseif} exp @Rw{then} block} + @bnfopt{@Rw{else} block} @Rw{end}} +} +Lua also has a @Rw{for} statement, in two flavors @see{for}. + +The @x{condition expression} of a +control structure can return any value. +Both @false and @nil test false. +All values different from @nil and @false test true. +(In particular, the number 0 and the empty string also test true). + +In the @Rw{repeat}@En@Rw{until} loop, +the inner block does not end at the @Rw{until} keyword, +but only after the condition. +So, the condition can refer to local variables +declared inside the loop block. + +The @Rw{goto} statement transfers the program control to a label. +For syntactical reasons, +labels in Lua are considered statements too: +@index{goto statement} +@index{label} +@Produc{ +@producname{stat}@producbody{@Rw{goto} Name} +@producname{stat}@producbody{label} +@producname{label}@producbody{@bnfter{::} Name @bnfter{::}} +} + +A label is visible in the entire block where it is defined, +except +inside nested blocks where a label with the same name is defined and +inside nested functions. +A goto may jump to any visible label as long as it does not +enter into the scope of a local variable. + +Labels and empty statements are called @def{void statements}, +as they perform no actions. + +The @Rw{break} statement terminates the execution of a +@Rw{while}, @Rw{repeat}, or @Rw{for} loop, +skipping to the next statement after the loop: +@index{break statement} +@Produc{ +@producname{stat}@producbody{@Rw{break}} +} +A @Rw{break} ends the innermost enclosing loop. + +The @Rw{return} statement is used to return values +from a function or a chunk +(which is an anonymous function). +@index{return statement} +Functions can return more than one value, +so the syntax for the @Rw{return} statement is +@Produc{ +@producname{stat}@producbody{@Rw{return} @bnfopt{explist} @bnfopt{@bnfter{;}}} +} + +The @Rw{return} statement can only be written +as the last statement of a block. +If it is really necessary to @Rw{return} in the middle of a block, +then an explicit inner block can be used, +as in the idiom @T{do return end}, +because now @Rw{return} is the last statement in its (inner) block. + +} + +@sect3{for| @title{For Statement} + +@index{for statement} +The @Rw{for} statement has two forms: +one numerical and one generic. + +The numerical @Rw{for} loop repeats a block of code while a +control variable runs through an arithmetic progression. +It has the following syntax: +@Produc{ +@producname{stat}@producbody{@Rw{for} @bnfNter{Name} @bnfter{=} + exp @bnfter{,} exp @bnfopt{@bnfter{,} exp} @Rw{do} block @Rw{end}} +} +The @emph{block} is repeated for @emph{name} starting at the value of +the first @emph{exp}, until it passes the second @emph{exp} by steps of the +third @emph{exp}. +More precisely, a @Rw{for} statement like +@verbatim{ +for v = @rep{e1}, @rep{e2}, @rep{e3} do @rep{block} end +} +is equivalent to the code: +@verbatim{ +do + local @rep{var}, @rep{limit}, @rep{step} = tonumber(@rep{e1}), tonumber(@rep{e2}), tonumber(@rep{e3}) + if not (@rep{var} and @rep{limit} and @rep{step}) then error() end + @rep{var} = @rep{var} - @rep{step} + while true do + @rep{var} = @rep{var} + @rep{step} + if (@rep{step} >= 0 and @rep{var} > @rep{limit}) or (@rep{step} < 0 and @rep{var} < @rep{limit}) then + break + end + local v = @rep{var} + @rep{block} + end +end +} + +Note the following: +@itemize{ + +@item{ +All three control expressions are evaluated only once, +before the loop starts. +They must all result in numbers. +} + +@item{ +@T{@rep{var}}, @T{@rep{limit}}, and @T{@rep{step}} are invisible variables. +The names shown here are for explanatory purposes only. +} + +@item{ +If the third expression (the step) is absent, +then a step @N{of 1} is used. +} + +@item{ +You can use @Rw{break} and @Rw{goto} to exit a @Rw{for} loop. +} + +@item{ +The loop variable @T{v} is local to the loop body. +If you need its value after the loop, +assign it to another variable before exiting the loop. +} + +@item{ +The values in @rep{var}, @rep{limit}, and @rep{step} +can be integers or floats. +All operations on them respect the usual rules in Lua. +} + +} + +The generic @Rw{for} statement works over functions, +called @def{iterators}. +On each iteration, the iterator function is called to produce a new value, +stopping when this new value is @nil. +The generic @Rw{for} loop has the following syntax: +@Produc{ +@producname{stat}@producbody{@Rw{for} namelist @Rw{in} explist + @Rw{do} block @Rw{end}} +@producname{namelist}@producbody{@bnfNter{Name} @bnfrep{@bnfter{,} @bnfNter{Name}}} +} +A @Rw{for} statement like +@verbatim{ +for @rep{var_1}, @Cdots, @rep{var_n} in @rep{explist} do @rep{block} end +} +is equivalent to the code: +@verbatim{ +do + local @rep{f}, @rep{s}, @rep{var} = @rep{explist} + while true do + local @rep{var_1}, @Cdots, @rep{var_n} = @rep{f}(@rep{s}, @rep{var}) + if @rep{var_1} == nil then break end + @rep{var} = @rep{var_1} + @rep{block} + end +end +} +Note the following: +@itemize{ + +@item{ +@T{@rep{explist}} is evaluated only once. +Its results are an @emph{iterator} function, +a @emph{state}, +and an initial value for the first @emph{iterator variable}. +} + +@item{ +@T{@rep{f}}, @T{@rep{s}}, and @T{@rep{var}} are invisible variables. +The names are here for explanatory purposes only. +} + +@item{ +You can use @Rw{break} to exit a @Rw{for} loop. +} + +@item{ +The loop variables @T{@rep{var_i}} are local to the loop; +you cannot use their values after the @Rw{for} ends. +If you need these values, +then assign them to other variables before breaking or exiting the loop. +} + +} + +} + +@sect3{funcstat| @title{Function Calls as Statements} +To allow possible side-effects, +function calls can be executed as statements: +@Produc{ +@producname{stat}@producbody{functioncall} +} +In this case, all returned values are thrown away. +Function calls are explained in @See{functioncall}. + +} + +@sect3{localvar| @title{Local Declarations} +@x{Local variables} can be declared anywhere inside a block. +The declaration can include an initial assignment: +@Produc{ +@producname{stat}@producbody{@Rw{local} namelist @bnfopt{@bnfter{=} explist}} +} +If present, an initial assignment has the same semantics +of a multiple assignment @see{assignment}. +Otherwise, all variables are initialized with @nil. + +A chunk is also a block @see{chunks}, +and so local variables can be declared in a chunk outside any explicit block. + +The visibility rules for local variables are explained in @See{visibility}. + +} + +} + +@sect2{expressions| @title{Expressions} + +The basic expressions in Lua are the following: +@Produc{ +@producname{exp}@producbody{prefixexp} +@producname{exp}@producbody{@Rw{nil} @Or @Rw{false} @Or @Rw{true}} +@producname{exp}@producbody{@bnfNter{Numeral}} +@producname{exp}@producbody{@bnfNter{LiteralString}} +@producname{exp}@producbody{functiondef} +@producname{exp}@producbody{tableconstructor} +@producname{exp}@producbody{@bnfter{...}} +@producname{exp}@producbody{exp binop exp} +@producname{exp}@producbody{unop exp} +@producname{prefixexp}@producbody{var @Or functioncall @Or + @bnfter{(} exp @bnfter{)}} +} + +Numerals and literal strings are explained in @See{lexical}; +variables are explained in @See{variables}; +function definitions are explained in @See{func-def}; +function calls are explained in @See{functioncall}; +table constructors are explained in @See{tableconstructor}. +Vararg expressions, +denoted by three dots (@Char{...}), can only be used when +directly inside a vararg function; +they are explained in @See{func-def}. + +Binary operators comprise arithmetic operators @see{arith}, +bitwise operators @see{bitwise}, +relational operators @see{rel-ops}, logical operators @see{logic}, +and the concatenation operator @see{concat}. +Unary operators comprise the unary minus @see{arith}, +the unary bitwise NOT @see{bitwise}, +the unary logical @Rw{not} @see{logic}, +and the unary @def{length operator} @see{len-op}. + +Both function calls and vararg expressions can result in multiple values. +If a function call is used as a statement @see{funcstat}, +then its return list is adjusted to zero elements, +thus discarding all returned values. +If an expression is used as the last (or the only) element +of a list of expressions, +then no adjustment is made +(unless the expression is enclosed in parentheses). +In all other contexts, +Lua adjusts the result list to one element, +either discarding all values except the first one +or adding a single @nil if there are no values. + +Here are some examples: +@verbatim{ +f() -- adjusted to 0 results +g(f(), x) -- f() is adjusted to 1 result +g(x, f()) -- g gets x plus all results from f() +a,b,c = f(), x -- f() is adjusted to 1 result (c gets nil) +a,b = ... -- a gets the first vararg argument, b gets + -- the second (both a and b can get nil if there + -- is no corresponding vararg argument) + +a,b,c = x, f() -- f() is adjusted to 2 results +a,b,c = f() -- f() is adjusted to 3 results +return f() -- returns all results from f() +return ... -- returns all received vararg arguments +return x,y,f() -- returns x, y, and all results from f() +{f()} -- creates a list with all results from f() +{...} -- creates a list with all vararg arguments +{f(), nil} -- f() is adjusted to 1 result +} + +Any expression enclosed in parentheses always results in only one value. +Thus, +@T{(f(x,y,z))} is always a single value, +even if @id{f} returns several values. +(The value of @T{(f(x,y,z))} is the first value returned by @id{f} +or @nil if @id{f} does not return any values.) + + + +@sect3{arith| @title{Arithmetic Operators} +Lua supports the following @x{arithmetic operators}: +@description{ +@item{@T{+}|addition} +@item{@T{-}|subtraction} +@item{@T{*}|multiplication} +@item{@T{/}|float division} +@item{@T{//}|floor division} +@item{@T{%}|modulo} +@item{@T{^}|exponentiation} +@item{@T{-}|unary minus} +} + +With the exception of exponentiation and float division, +the arithmetic operators work as follows: +If both operands are integers, +the operation is performed over integers and the result is an integer. +Otherwise, if both operands are numbers, +then they are converted to floats, +the operation is performed following the usual rules +for floating-point arithmetic +(usually the @x{IEEE 754} standard), +and the result is a float. +(The string library coerces strings to numbers in +arithmetic operations; see @See{coercion} for details.) + +Exponentiation and float division (@T{/}) +always convert their operands to floats +and the result is always a float. +Exponentiation uses the @ANSI{pow}, +so that it works for non-integer exponents too. + +Floor division (@T{//}) is a division +that rounds the quotient towards minus infinity, +that is, the floor of the division of its operands. + +Modulo is defined as the remainder of a division +that rounds the quotient towards minus infinity (floor division). + +In case of overflows in integer arithmetic, +all operations @emphx{wrap around}, +according to the usual rules of two-complement arithmetic. +(In other words, +they return the unique representable integer +that is equal modulo @M{2@sp{64}} to the mathematical result.) +} + +@sect3{bitwise| @title{Bitwise Operators} +Lua supports the following @x{bitwise operators}: +@description{ +@item{@T{&}|bitwise AND} +@item{@T{@VerBar}|bitwise OR} +@item{@T{~}|bitwise exclusive OR} +@item{@T{>>}|right shift} +@item{@T{<<}|left shift} +@item{@T{~}|unary bitwise NOT} +} + +All bitwise operations convert its operands to integers +@see{coercion}, +operate on all bits of those integers, +and result in an integer. + +Both right and left shifts fill the vacant bits with zeros. +Negative displacements shift to the other direction; +displacements with absolute values equal to or higher than +the number of bits in an integer +result in zero (as all bits are shifted out). + +} + +@sect3{coercion| @title{Coercions and Conversions} +Lua provides some automatic conversions between some +types and representations at run time. +Bitwise operators always convert float operands to integers. +Exponentiation and float division +always convert integer operands to floats. +All other arithmetic operations applied to mixed numbers +(integers and floats) convert the integer operand to a float. +The C API also converts both integers to floats and +floats to integers, as needed. +Moreover, string concatenation accepts numbers as arguments, +besides strings. + +In a conversion from integer to float, +if the integer value has an exact representation as a float, +that is the result. +Otherwise, +the conversion gets the nearest higher or +the nearest lower representable value. +This kind of conversion never fails. + +The conversion from float to integer +checks whether the float has an exact representation as an integer +(that is, the float has an integral value and +it is in the range of integer representation). +If it does, that representation is the result. +Otherwise, the conversion fails. + +The string library uses metamethods that try to coerce +strings to numbers in all arithmetic operations. +Any string operator is converted to an integer or a float, +following its syntax and the rules of the Lua lexer. +(The string may have also leading and trailing spaces and a sign.) +All conversions from strings to numbers +accept both a dot and the current locale mark +as the radix character. +(The Lua lexer, however, accepts only a dot.) + +The conversion from numbers to strings uses a +non-specified human-readable format. +For complete control over how numbers are converted to strings, +use the @id{format} function from the string library +@seeF{string.format}. + +} + +@sect3{rel-ops| @title{Relational Operators} +Lua supports the following @x{relational operators}: +@description{ +@item{@T{==}|equality} +@item{@T{~=}|inequality} +@item{@T{<}|less than} +@item{@T{>}|greater than} +@item{@T{<=}|less or equal} +@item{@T{>=}|greater or equal} +} +These operators always result in @false or @true. + +Equality (@T{==}) first compares the type of its operands. +If the types are different, then the result is @false. +Otherwise, the values of the operands are compared. +Strings are compared in the obvious way. +Numbers are equal if they denote the same mathematical value. + +Tables, userdata, and threads +are compared by reference: +two objects are considered equal only if they are the same object. +Every time you create a new object +(a table, userdata, or thread), +this new object is different from any previously existing object. +A closure is always equal to itself. +Closures with any detectable difference +(different behavior, different definition) are always different. +Closures created at different times but with no detectable differences +may be classified as equal or not +(depending on internal cashing details). + +You can change the way that Lua compares tables and userdata +by using the @idx{__eq} metamethod @see{metatable}. + +Equality comparisons do not convert strings to numbers +or vice versa. +Thus, @T{"0"==0} evaluates to @false, +and @T{t[0]} and @T{t["0"]} denote different +entries in a table. + +The operator @T{~=} is exactly the negation of equality (@T{==}). + +The order operators work as follows. +If both arguments are numbers, +then they are compared according to their mathematical values +(regardless of their subtypes). +Otherwise, if both arguments are strings, +then their values are compared according to the current locale. +Otherwise, Lua tries to call the @idx{__lt} or the @idx{__le} +metamethod @see{metatable}. +A comparison @T{a > b} is translated to @T{b < a} +and @T{a >= b} is translated to @T{b <= a}. + +Following the @x{IEEE 754} standard, +@x{NaN} is considered neither smaller than, +nor equal to, nor greater than any value (including itself). + +} + +@sect3{logic| @title{Logical Operators} +The @x{logical operators} in Lua are +@Rw{and}, @Rw{or}, and @Rw{not}. +Like the control structures @see{control}, +all logical operators consider both @false and @nil as false +and anything else as true. + +The negation operator @Rw{not} always returns @false or @true. +The conjunction operator @Rw{and} returns its first argument +if this value is @false or @nil; +otherwise, @Rw{and} returns its second argument. +The disjunction operator @Rw{or} returns its first argument +if this value is different from @nil and @false; +otherwise, @Rw{or} returns its second argument. +Both @Rw{and} and @Rw{or} use @x{short-circuit evaluation}; +that is, +the second operand is evaluated only if necessary. +Here are some examples: +@verbatim{ +10 or 20 --> 10 +10 or error() --> 10 +nil or "a" --> "a" +nil and 10 --> nil +false and error() --> false +false and nil --> false +false or nil --> nil +10 and 20 --> 20 +} + +} + +@sect3{concat| @title{Concatenation} +The string @x{concatenation} operator in Lua is +denoted by two dots (@Char{..}). +If both operands are strings or numbers, then they are converted to +strings according to the rules described in @See{coercion}. +Otherwise, the @idx{__concat} metamethod is called @see{metatable}. + +} + +@sect3{len-op| @title{The Length Operator} + +The length operator is denoted by the unary prefix operator @T{#}. + +The length of a string is its number of bytes +(that is, the usual meaning of string length when each +character is one byte). + +The length operator applied on a table +returns a @x{border} in that table. +A @def{border} in a table @id{t} is any natural number +that satisfies the following condition: +@verbatim{ +(border == 0 or t[border] ~= nil) and t[border + 1] == nil +} +In words, +a border is any (natural) index present in the table +that is followed by an absent index +(or zero, when index 1 is absent). + +A table with exactly one border is called a @def{sequence}. +For instance, the table @T{{10, 20, 30, 40, 50}} is a sequence, +as it has only one border (5). +The table @T{{10, 20, 30, nil, 50}} has two borders (3 and 5), +and therefore it is not a sequence. +The table @T{{nil, 20, 30, nil, nil, 60, nil}} +has three borders (0, 3, and 6), +so it is not a sequence, too. +The table @T{{}} is a sequence with border 0. +Note that non-natural keys do not interfere +with whether a table is a sequence. + +When @id{t} is a sequence, +@T{#t} returns its only border, +which corresponds to the intuitive notion of the length of the sequence. +When @id{t} is not a sequence, +@T{#t} can return any of its borders. +(The exact one depends on details of +the internal representation of the table, +which in turn can depend on how the table was populated and +the memory addresses of its non-numeric keys.) + +The computation of the length of a table +has a guaranteed worst time of @M{O(log n)}, +where @M{n} is the largest natural key in the table. + +A program can modify the behavior of the length operator for +any value but strings through the @idx{__len} metamethod @see{metatable}. + +} + +@sect3{prec| @title{Precedence} +@x{Operator precedence} in Lua follows the table below, +from lower to higher priority: +@verbatim{ +or +and +< > <= >= ~= == +| +~ +& +<< >> +.. ++ - +* / // % +unary operators (not # - ~) +^ +} +As usual, +you can use parentheses to change the precedences of an expression. +The concatenation (@Char{..}) and exponentiation (@Char{^}) +operators are right associative. +All other binary operators are left associative. + +} + +@sect3{tableconstructor| @title{Table Constructors} +Table @x{constructors} are expressions that create tables. +Every time a constructor is evaluated, a new table is created. +A constructor can be used to create an empty table +or to create a table and initialize some of its fields. +The general syntax for constructors is +@Produc{ +@producname{tableconstructor}@producbody{@bnfter{@Open} @bnfopt{fieldlist} @bnfter{@Close}} +@producname{fieldlist}@producbody{field @bnfrep{fieldsep field} @bnfopt{fieldsep}} +@producname{field}@producbody{@bnfter{[} exp @bnfter{]} @bnfter{=} exp @Or + @bnfNter{Name} @bnfter{=} exp @Or exp} +@producname{fieldsep}@producbody{@bnfter{,} @Or @bnfter{;}} +} + +Each field of the form @T{[exp1] = exp2} adds to the new table an entry +with key @id{exp1} and value @id{exp2}. +A field of the form @T{name = exp} is equivalent to +@T{["name"] = exp}. +Finally, fields of the form @id{exp} are equivalent to +@T{[i] = exp}, where @id{i} are consecutive integers +starting with 1. +Fields in the other formats do not affect this counting. +For example, +@verbatim{ +a = { [f(1)] = g; "x", "y"; x = 1, f(x), [30] = 23; 45 } +} +is equivalent to +@verbatim{ +do + local t = {} + t[f(1)] = g + t[1] = "x" -- 1st exp + t[2] = "y" -- 2nd exp + t.x = 1 -- t["x"] = 1 + t[3] = f(x) -- 3rd exp + t[30] = 23 + t[4] = 45 -- 4th exp + a = t +end +} + +The order of the assignments in a constructor is undefined. +(This order would be relevant only when there are repeated keys.) + +If the last field in the list has the form @id{exp} +and the expression is a function call or a vararg expression, +then all values returned by this expression enter the list consecutively +@see{functioncall}. + +The field list can have an optional trailing separator, +as a convenience for machine-generated code. + +} + +@sect3{functioncall| @title{Function Calls} +A @x{function call} in Lua has the following syntax: +@Produc{ +@producname{functioncall}@producbody{prefixexp args} +} +In a function call, +first @bnfNter{prefixexp} and @bnfNter{args} are evaluated. +If the value of @bnfNter{prefixexp} has type @emph{function}, +then this function is called +with the given arguments. +Otherwise, the @bnfNter{prefixexp} @idx{__call} metamethod is called, +having as first argument the value of @bnfNter{prefixexp}, +followed by the original call arguments +@see{metatable}. + +The form +@Produc{ +@producname{functioncall}@producbody{prefixexp @bnfter{:} @bnfNter{Name} args} +} +can be used to call @Q{methods}. +A call @T{v:name(@rep{args})} +is syntactic sugar for @T{v.name(v,@rep{args})}, +except that @id{v} is evaluated only once. + +Arguments have the following syntax: +@Produc{ +@producname{args}@producbody{@bnfter{(} @bnfopt{explist} @bnfter{)}} +@producname{args}@producbody{tableconstructor} +@producname{args}@producbody{@bnfNter{LiteralString}} +} +All argument expressions are evaluated before the call. +A call of the form @T{f{@rep{fields}}} is +syntactic sugar for @T{f({@rep{fields}})}; +that is, the argument list is a single new table. +A call of the form @T{f'@rep{string}'} +(or @T{f"@rep{string}"} or @T{f[[@rep{string}]]}) +is syntactic sugar for @T{f('@rep{string}')}; +that is, the argument list is a single literal string. + +A call of the form @T{return @rep{functioncall}} is called +a @def{tail call}. +Lua implements @def{proper tail calls} +(or @emph{proper tail recursion}): +in a tail call, +the called function reuses the stack entry of the calling function. +Therefore, there is no limit on the number of nested tail calls that +a program can execute. +However, a tail call erases any debug information about the +calling function. +Note that a tail call only happens with a particular syntax, +where the @Rw{return} has one single function call as argument; +this syntax makes the calling function return exactly +the returns of the called function. +So, none of the following examples are tail calls: +@verbatim{ +return (f(x)) -- results adjusted to 1 +return 2 * f(x) +return x, f(x) -- additional results +f(x); return -- results discarded +return x or f(x) -- results adjusted to 1 +} + +} + +@sect3{func-def| @title{Function Definitions} + +The syntax for function definition is +@Produc{ +@producname{functiondef}@producbody{@Rw{function} funcbody} +@producname{funcbody}@producbody{@bnfter{(} @bnfopt{parlist} @bnfter{)} block @Rw{end}} +} + +The following syntactic sugar simplifies function definitions: +@Produc{ +@producname{stat}@producbody{@Rw{function} funcname funcbody} +@producname{stat}@producbody{@Rw{local} @Rw{function} @bnfNter{Name} funcbody} +@producname{funcname}@producbody{@bnfNter{Name} @bnfrep{@bnfter{.} @bnfNter{Name}} @bnfopt{@bnfter{:} @bnfNter{Name}}} +} +The statement +@verbatim{ +function f () @rep{body} end +} +translates to +@verbatim{ +f = function () @rep{body} end +} +The statement +@verbatim{ +function t.a.b.c.f () @rep{body} end +} +translates to +@verbatim{ +t.a.b.c.f = function () @rep{body} end +} +The statement +@verbatim{ +local function f () @rep{body} end +} +translates to +@verbatim{ +local f; f = function () @rep{body} end +} +not to +@verbatim{ +local f = function () @rep{body} end +} +(This only makes a difference when the body of the function +contains references to @id{f}.) + +A function definition is an executable expression, +whose value has type @emph{function}. +When Lua precompiles a chunk, +all its function bodies are precompiled too. +Then, whenever Lua executes the function definition, +the function is @emph{instantiated} (or @emph{closed}). +This function instance (or @emphx{closure}) +is the final value of the expression. + +Parameters act as local variables that are +initialized with the argument values: +@Produc{ +@producname{parlist}@producbody{namelist @bnfopt{@bnfter{,} @bnfter{...}} @Or + @bnfter{...}} +} +When a Lua function is called, +it adjusts its list of @x{arguments} to +the length of its list of parameters, +unless the function is a @def{vararg function}, +which is indicated by three dots (@Char{...}) +at the end of its parameter list. +A vararg function does not adjust its argument list; +instead, it collects all extra arguments and supplies them +to the function through a @def{vararg expression}, +which is also written as three dots. +The value of this expression is a list of all actual extra arguments, +similar to a function with multiple results. +If a vararg expression is used inside another expression +or in the middle of a list of expressions, +then its return list is adjusted to one element. +If the expression is used as the last element of a list of expressions, +then no adjustment is made +(unless that last expression is enclosed in parentheses). + + +As an example, consider the following definitions: +@verbatim{ +function f(a, b) end +function g(a, b, ...) end +function r() return 1,2,3 end +} +Then, we have the following mapping from arguments to parameters and +to the vararg expression: +@verbatim{ +CALL PARAMETERS + +f(3) a=3, b=nil +f(3, 4) a=3, b=4 +f(3, 4, 5) a=3, b=4 +f(r(), 10) a=1, b=10 +f(r()) a=1, b=2 + +g(3) a=3, b=nil, ... --> (nothing) +g(3, 4) a=3, b=4, ... --> (nothing) +g(3, 4, 5, 8) a=3, b=4, ... --> 5 8 +g(5, r()) a=5, b=1, ... --> 2 3 +} + +Results are returned using the @Rw{return} statement @see{control}. +If control reaches the end of a function +without encountering a @Rw{return} statement, +then the function returns with no results. + +@index{multiple return} +There is a system-dependent limit on the number of values +that a function may return. +This limit is guaranteed to be larger than 1000. + +The @emphx{colon} syntax +is used for defining @def{methods}, +that is, functions that have an implicit extra parameter @idx{self}. +Thus, the statement +@verbatim{ +function t.a.b.c:f (@rep{params}) @rep{body} end +} +is syntactic sugar for +@verbatim{ +t.a.b.c.f = function (self, @rep{params}) @rep{body} end +} + +} + +} + +@sect2{visibility| @title{Visibility Rules} + +@index{visibility} +Lua is a lexically scoped language. +The scope of a local variable begins at the first statement after +its declaration and lasts until the last non-void statement +of the innermost block that includes the declaration. +Consider the following example: +@verbatim{ +x = 10 -- global variable +do -- new block + local x = x -- new 'x', with value 10 + print(x) --> 10 + x = x+1 + do -- another block + local x = x+1 -- another 'x' + print(x) --> 12 + end + print(x) --> 11 +end +print(x) --> 10 (the global one) +} + +Notice that, in a declaration like @T{local x = x}, +the new @id{x} being declared is not in scope yet, +and so the second @id{x} refers to the outside variable. + +Because of the @x{lexical scoping} rules, +local variables can be freely accessed by functions +defined inside their scope. +A local variable used by an inner function is called +an @def{upvalue}, or @emphx{external local variable}, +inside the inner function. + +Notice that each execution of a @Rw{local} statement +defines new local variables. +Consider the following example: +@verbatim{ +a = {} +local x = 20 +for i=1,10 do + local y = 0 + a[i] = function () y=y+1; return x+y end +end +} +The loop creates ten closures +(that is, ten instances of the anonymous function). +Each of these closures uses a different @id{y} variable, +while all of them share the same @id{x}. + +} + +} + + +@C{-------------------------------------------------------------------------} +@sect1{API| @title{The Application Program Interface} + +@index{C API} +This section describes the @N{C API} for Lua, that is, +the set of @N{C functions} available to the host program to communicate +with Lua. +All API functions and related types and constants +are declared in the header file @defid{lua.h}. + +Even when we use the term @Q{function}, +any facility in the API may be provided as a macro instead. +Except where stated otherwise, +all such macros use each of their arguments exactly once +(except for the first argument, which is always a Lua state), +and so do not generate any hidden side-effects. + +As in most @N{C libraries}, +the Lua API functions do not check their arguments +for validity or consistency. +However, you can change this behavior by compiling Lua +with the macro @defid{LUA_USE_APICHECK} defined. + +The Lua library is fully reentrant: +it has no global variables. +It keeps all information it needs in a dynamic structure, +called the @def{Lua state}. + +Each Lua state has one or more threads, +which correspond to independent, cooperative lines of execution. +The type @Lid{lua_State} (despite its name) refers to a thread. +(Indirectly, through the thread, it also refers to the +Lua state associated to the thread.) + +A pointer to a thread must be passed as the first argument to +every function in the library, except to @Lid{lua_newstate}, +which creates a Lua state from scratch and returns a pointer +to the @emph{main thread} in the new state. + + +@sect2{@title{The Stack} + +Lua uses a @emph{virtual stack} to pass values to and from C. +Each element in this stack represents a Lua value +(@nil, number, string, etc.). +Functions in the API can access this stack through the +Lua state parameter that they receive. + +Whenever Lua calls C, the called function gets a new stack, +which is independent of previous stacks and of stacks of +@N{C functions} that are still active. +This stack initially contains any arguments to the @N{C function} +and it is where the @N{C function} can store temporary +Lua values and must push its results +to be returned to the caller @seeC{lua_CFunction}. + +For convenience, +most query operations in the API do not follow a strict stack discipline. +Instead, they can refer to any element in the stack +by using an @emph{index}:@index{index (API stack)} +A positive index represents an absolute stack position +(starting @N{at 1}); +a negative index represents an offset relative to the top of the stack. +More specifically, if the stack has @rep{n} elements, +then @N{index 1} represents the first element +(that is, the element that was pushed onto the stack first) +and +@N{index @rep{n}} represents the last element; +@N{index @num{-1}} also represents the last element +(that is, the element at @N{the top}) +and index @M{-n} represents the first element. + +} + +@sect2{stacksize| @title{Stack Size} + +When you interact with the Lua API, +you are responsible for ensuring consistency. +In particular, +@emph{you are responsible for controlling stack overflow}. +You can use the function @Lid{lua_checkstack} +to ensure that the stack has enough space for pushing new elements. + +Whenever Lua calls C, +it ensures that the stack has space for +at least @defid{LUA_MINSTACK} extra slots. +@id{LUA_MINSTACK} is defined as 20, +so that usually you do not have to worry about stack space +unless your code has loops pushing elements onto the stack. + +When you call a Lua function +without a fixed number of results @seeF{lua_call}, +Lua ensures that the stack has enough space for all results, +but it does not ensure any extra space. +So, before pushing anything in the stack after such a call +you should use @Lid{lua_checkstack}. + +} + +@sect2{@title{Valid and Acceptable Indices} + +Any function in the API that receives stack indices +works only with @emphx{valid indices} or @emphx{acceptable indices}. + +A @def{valid index} is an index that refers to a +position that stores a modifiable Lua value. +It comprises stack indices @N{between 1} and the stack top +(@T{1 @leq abs(index) @leq top}) +@index{stack index} +plus @def{pseudo-indices}, +which represent some positions that are accessible to @N{C code} +but that are not in the stack. +Pseudo-indices are used to access the registry @see{registry} +and the upvalues of a @N{C function} @see{c-closure}. + +Functions that do not need a specific mutable position, +but only a value (e.g., query functions), +can be called with acceptable indices. +An @def{acceptable index} can be any valid index, +but it also can be any positive index after the stack top +within the space allocated for the stack, +that is, indices up to the stack size. +(Note that 0 is never an acceptable index.) +Indices to upvalues @see{c-closure} larger than the real number +of upvalues in the current @N{C function} are also acceptable (but invalid). +Except when noted otherwise, +functions in the API work with acceptable indices. + +Acceptable indices serve to avoid extra tests +against the stack top when querying the stack. +For instance, a @N{C function} can query its third argument +without the need to first check whether there is a third argument, +that is, without the need to check whether 3 is a valid index. + +For functions that can be called with acceptable indices, +any non-valid index is treated as if it +contains a value of a virtual type @defid{LUA_TNONE}, +which behaves like a nil value. + +} + +@sect2{c-closure| @title{C Closures} + +When a @N{C function} is created, +it is possible to associate some values with it, +thus creating a @def{@N{C closure}} +@seeC{lua_pushcclosure}; +these values are called @def{upvalues} and are +accessible to the function whenever it is called. + +Whenever a @N{C function} is called, +its upvalues are located at specific pseudo-indices. +These pseudo-indices are produced by the macro +@Lid{lua_upvalueindex}. +The first upvalue associated with a function is at index +@T{lua_upvalueindex(1)}, and so on. +Any access to @T{lua_upvalueindex(@rep{n})}, +where @rep{n} is greater than the number of upvalues of the +current function +(but not greater than 256, +which is one plus the maximum number of upvalues in a closure), +produces an acceptable but invalid index. + +A @N{C closure} can also change the values of its corresponding upvalues. + +} + +@sect2{registry| @title{Registry} + +Lua provides a @def{registry}, +a predefined table that can be used by any @N{C code} to +store whatever Lua values it needs to store. +The registry table is always located at pseudo-index +@defid{LUA_REGISTRYINDEX}. +Any @N{C library} can store data into this table, +but it must take care to choose keys +that are different from those used +by other libraries, to avoid collisions. +Typically, you should use as key a string containing your library name, +or a light userdata with the address of a @N{C object} in your code, +or any Lua object created by your code. +As with variable names, +string keys starting with an underscore followed by +uppercase letters are reserved for Lua. + +The integer keys in the registry are used +by the reference mechanism @seeC{luaL_ref} +and by some predefined values. +Therefore, integer keys must not be used for other purposes. + +When you create a new Lua state, +its registry comes with some predefined values. +These predefined values are indexed with integer keys +defined as constants in @id{lua.h}. +The following constants are defined: +@description{ +@item{@defid{LUA_RIDX_MAINTHREAD}| At this index the registry has +the main thread of the state. +(The main thread is the one created together with the state.) +} + +@item{@defid{LUA_RIDX_GLOBALS}| At this index the registry has +the @x{global environment}. +} +} + +} + +@sect2{C-error|@title{Error Handling in C} + +Internally, Lua uses the C @id{longjmp} facility to handle errors. +(Lua will use exceptions if you compile it as C++; +search for @id{LUAI_THROW} in the source code for details.) +When Lua faces any error +(such as a @x{memory allocation error} or a type error) +it @emph{raises} an error; +that is, it does a long jump. +A @emphx{protected environment} uses @id{setjmp} +to set a recovery point; +any error jumps to the most recent active recovery point. + +Inside a @N{C function} you can raise an error by calling @Lid{lua_error}. + +Most functions in the API can raise an error, +for instance due to a @x{memory allocation error}. +The documentation for each function indicates whether +it can raise errors. + +If an error happens outside any protected environment, +Lua calls a @def{panic function} (see @Lid{lua_atpanic}) +and then calls @T{abort}, +thus exiting the host application. +Your panic function can avoid this exit by +never returning +(e.g., doing a long jump to your own recovery point outside Lua). + +The panic function, +as its name implies, +is a mechanism of last resort. +Programs should avoid it. +As a general rule, +when a @N{C function} is called by Lua with a Lua state, +it can do whatever it wants on that Lua state, +as it should be already protected. +However, +when C code operates on other Lua states +(e.g., a Lua parameter to the function, +a Lua state stored in the registry, or +the result of @Lid{lua_newthread}), +it should use them only in API calls that cannot raise errors. + +The panic function runs as if it were a @x{message handler} @see{error}; +in particular, the error object is at the top of the stack. +However, there is no guarantee about stack space. +To push anything on the stack, +the panic function must first check the available space @see{stacksize}. + +} + +@sect2{continuations|@title{Handling Yields in C} + +Internally, Lua uses the C @id{longjmp} facility to yield a coroutine. +Therefore, if a @N{C function} @id{foo} calls an API function +and this API function yields +(directly or indirectly by calling another function that yields), +Lua cannot return to @id{foo} any more, +because the @id{longjmp} removes its frame from the C stack. + +To avoid this kind of problem, +Lua raises an error whenever it tries to yield across an API call, +except for three functions: +@Lid{lua_yieldk}, @Lid{lua_callk}, and @Lid{lua_pcallk}. +All those functions receive a @def{continuation function} +(as a parameter named @id{k}) to continue execution after a yield. + +We need to set some terminology to explain continuations. +We have a @N{C function} called from Lua which we will call +the @emph{original function}. +This original function then calls one of those three functions in the C API, +which we will call the @emph{callee function}, +that then yields the current thread. +(This can happen when the callee function is @Lid{lua_yieldk}, +or when the callee function is either @Lid{lua_callk} or @Lid{lua_pcallk} +and the function called by them yields.) + +Suppose the running thread yields while executing the callee function. +After the thread resumes, +it eventually will finish running the callee function. +However, +the callee function cannot return to the original function, +because its frame in the C stack was destroyed by the yield. +Instead, Lua calls a @def{continuation function}, +which was given as an argument to the callee function. +As the name implies, +the continuation function should continue the task +of the original function. + +As an illustration, consider the following function: +@verbatim{ +int original_function (lua_State *L) { + ... /* code 1 */ + status = lua_pcall(L, n, m, h); /* calls Lua */ + ... /* code 2 */ +} +} +Now we want to allow +the Lua code being run by @Lid{lua_pcall} to yield. +First, we can rewrite our function like here: +@verbatim{ +int k (lua_State *L, int status, lua_KContext ctx) { + ... /* code 2 */ +} + +int original_function (lua_State *L) { + ... /* code 1 */ + return k(L, lua_pcall(L, n, m, h), ctx); +} +} +In the above code, +the new function @id{k} is a +@emph{continuation function} (with type @Lid{lua_KFunction}), +which should do all the work that the original function +was doing after calling @Lid{lua_pcall}. +Now, we must inform Lua that it must call @id{k} if the Lua code +being executed by @Lid{lua_pcall} gets interrupted in some way +(errors or yielding), +so we rewrite the code as here, +replacing @Lid{lua_pcall} by @Lid{lua_pcallk}: +@verbatim{ +int original_function (lua_State *L) { + ... /* code 1 */ + return k(L, lua_pcallk(L, n, m, h, ctx2, k), ctx1); +} +} +Note the external, explicit call to the continuation: +Lua will call the continuation only if needed, that is, +in case of errors or resuming after a yield. +If the called function returns normally without ever yielding, +@Lid{lua_pcallk} (and @Lid{lua_callk}) will also return normally. +(Of course, instead of calling the continuation in that case, +you can do the equivalent work directly inside the original function.) + +Besides the Lua state, +the continuation function has two other parameters: +the final status of the call plus the context value (@id{ctx}) that +was passed originally to @Lid{lua_pcallk}. +(Lua does not use this context value; +it only passes this value from the original function to the +continuation function.) +For @Lid{lua_pcallk}, +the status is the same value that would be returned by @Lid{lua_pcallk}, +except that it is @Lid{LUA_YIELD} when being executed after a yield +(instead of @Lid{LUA_OK}). +For @Lid{lua_yieldk} and @Lid{lua_callk}, +the status is always @Lid{LUA_YIELD} when Lua calls the continuation. +(For these two functions, +Lua will not call the continuation in case of errors, +because they do not handle errors.) +Similarly, when using @Lid{lua_callk}, +you should call the continuation function +with @Lid{LUA_OK} as the status. +(For @Lid{lua_yieldk}, there is not much point in calling +directly the continuation function, +because @Lid{lua_yieldk} usually does not return.) + +Lua treats the continuation function as if it were the original function. +The continuation function receives the same Lua stack +from the original function, +in the same state it would be if the callee function had returned. +(For instance, +after a @Lid{lua_callk} the function and its arguments are +removed from the stack and replaced by the results from the call.) +It also has the same upvalues. +Whatever it returns is handled by Lua as if it were the return +of the original function. + +} + +@sect2{@title{Functions and Types} + +Here we list all functions and types from the @N{C API} in +alphabetical order. +Each function has an indicator like this: +@apii{o,p,x} + +The first field, @T{o}, +is how many elements the function pops from the stack. +The second field, @T{p}, +is how many elements the function pushes onto the stack. +(Any function always pushes its results after popping its arguments.) +A field in the form @T{x|y} means the function can push (or pop) +@T{x} or @T{y} elements, +depending on the situation; +an interrogation mark @Char{?} means that +we cannot know how many elements the function pops/pushes +by looking only at its arguments +(e.g., they may depend on what is on the stack). +The third field, @T{x}, +tells whether the function may raise errors: +@Char{-} means the function never raises any error; +@Char{m} means the function may raise out-of-memory errors +and errors running a finalizer; +@Char{v} means the function may raise the errors explained in the text; +@Char{e} means the function may raise any errors +(because it can run arbitrary Lua code, +either directly or through metamethods). + + +@APIEntry{int lua_absindex (lua_State *L, int idx);| +@apii{0,0,-} + +Converts the @x{acceptable index} @id{idx} +into an equivalent @x{absolute index} +(that is, one that does not depend on the stack top). + +} + + +@APIEntry{ +typedef void * (*lua_Alloc) (void *ud, + void *ptr, + size_t osize, + size_t nsize);| + +The type of the @x{memory-allocation function} used by Lua states. +The allocator function must provide a +functionality similar to @id{realloc}, +but not exactly the same. +Its arguments are +@id{ud}, an opaque pointer passed to @Lid{lua_newstate}; +@id{ptr}, a pointer to the block being allocated/reallocated/freed; +@id{osize}, the original size of the block or some code about what +is being allocated; +and @id{nsize}, the new size of the block. + +When @id{ptr} is not @id{NULL}, +@id{osize} is the size of the block pointed by @id{ptr}, +that is, the size given when it was allocated or reallocated. + +When @id{ptr} is @id{NULL}, +@id{osize} encodes the kind of object that Lua is allocating. +@id{osize} is any of +@Lid{LUA_TSTRING}, @Lid{LUA_TTABLE}, @Lid{LUA_TFUNCTION}, +@Lid{LUA_TUSERDATA}, or @Lid{LUA_TTHREAD} when (and only when) +Lua is creating a new object of that type. +When @id{osize} is some other value, +Lua is allocating memory for something else. + +Lua assumes the following behavior from the allocator function: + +When @id{nsize} is zero, +the allocator must behave like @id{free} +and return @id{NULL}. + +When @id{nsize} is not zero, +the allocator must behave like @id{realloc}. +The allocator returns @id{NULL} +if and only if it cannot fulfill the request. + +Here is a simple implementation for the @x{allocator function}. +It is used in the auxiliary library by @Lid{luaL_newstate}. +@verbatim{ +static void *l_alloc (void *ud, void *ptr, size_t osize, + size_t nsize) { + (void)ud; (void)osize; /* not used */ + if (nsize == 0) { + free(ptr); + return NULL; + } + else + return realloc(ptr, nsize); +} +} +Note that @N{Standard C} ensures +that @T{free(NULL)} has no effect and that +@T{realloc(NULL,size)} is equivalent to @T{malloc(size)}. + +} + +@APIEntry{void lua_arith (lua_State *L, int op);| +@apii{2|1,1,e} + +Performs an arithmetic or bitwise operation over the two values +(or one, in the case of negations) +at the top of the stack, +with the value at the top being the second operand, +pops these values, and pushes the result of the operation. +The function follows the semantics of the corresponding Lua operator +(that is, it may call metamethods). + +The value of @id{op} must be one of the following constants: +@description{ + +@item{@defid{LUA_OPADD}| performs addition (@T{+})} +@item{@defid{LUA_OPSUB}| performs subtraction (@T{-})} +@item{@defid{LUA_OPMUL}| performs multiplication (@T{*})} +@item{@defid{LUA_OPDIV}| performs float division (@T{/})} +@item{@defid{LUA_OPIDIV}| performs floor division (@T{//})} +@item{@defid{LUA_OPMOD}| performs modulo (@T{%})} +@item{@defid{LUA_OPPOW}| performs exponentiation (@T{^})} +@item{@defid{LUA_OPUNM}| performs mathematical negation (unary @T{-})} +@item{@defid{LUA_OPBNOT}| performs bitwise NOT (@T{~})} +@item{@defid{LUA_OPBAND}| performs bitwise AND (@T{&})} +@item{@defid{LUA_OPBOR}| performs bitwise OR (@T{|})} +@item{@defid{LUA_OPBXOR}| performs bitwise exclusive OR (@T{~})} +@item{@defid{LUA_OPSHL}| performs left shift (@T{<<})} +@item{@defid{LUA_OPSHR}| performs right shift (@T{>>})} + +} + +} + +@APIEntry{lua_CFunction lua_atpanic (lua_State *L, lua_CFunction panicf);| +@apii{0,0,-} + +Sets a new panic function and returns the old one @see{C-error}. + +} + +@APIEntry{void lua_call (lua_State *L, int nargs, int nresults);| +@apii{nargs+1,nresults,e} + +Calls a function. + +To do a call you must use the following protocol: +first, the value to be called is pushed onto the stack; +then, the arguments to the call are pushed +in direct order; +that is, the first argument is pushed first. +Finally you call @Lid{lua_call}; +@id{nargs} is the number of arguments that you pushed onto the stack. +All arguments and the function value are popped from the stack +when the function is called. +The function results are pushed onto the stack when the function returns. +The number of results is adjusted to @id{nresults}, +unless @id{nresults} is @defid{LUA_MULTRET}. +In this case, all results from the function are pushed; +Lua takes care that the returned values fit into the stack space, +but it does not ensure any extra space in the stack. +The function results are pushed onto the stack in direct order +(the first result is pushed first), +so that after the call the last result is on the top of the stack. + +Any error while calling and running the function is propagated upwards +(with a @id{longjmp}). +Like regular Lua calls, +this function respects the @idx{__call} metamethod. + +The following example shows how the host program can do the +equivalent to this Lua code: +@verbatim{ +a = f("how", t.x, 14) +} +Here it is @N{in C}: +@verbatim{ +lua_getglobal(L, "f"); /* function to be called */ +lua_pushliteral(L, "how"); /* 1st argument */ +lua_getglobal(L, "t"); /* table to be indexed */ +lua_getfield(L, -1, "x"); /* push result of t.x (2nd arg) */ +lua_remove(L, -2); /* remove 't' from the stack */ +lua_pushinteger(L, 14); /* 3rd argument */ +lua_call(L, 3, 1); /* call 'f' with 3 arguments and 1 result */ +lua_setglobal(L, "a"); /* set global 'a' */ +} +Note that the code above is @emph{balanced}: +at its end, the stack is back to its original configuration. +This is considered good programming practice. + +} + +@APIEntry{ +void lua_callk (lua_State *L, + int nargs, + int nresults, + lua_KContext ctx, + lua_KFunction k);| +@apii{nargs + 1,nresults,e} + +This function behaves exactly like @Lid{lua_call}, +but allows the called function to yield @see{continuations}. + +} + +@APIEntry{typedef int (*lua_CFunction) (lua_State *L);| + +Type for @N{C functions}. + +In order to communicate properly with Lua, +a @N{C function} must use the following protocol, +which defines the way parameters and results are passed: +a @N{C function} receives its arguments from Lua in its stack +in direct order (the first argument is pushed first). +So, when the function starts, +@T{lua_gettop(L)} returns the number of arguments received by the function. +The first argument (if any) is at index 1 +and its last argument is at index @T{lua_gettop(L)}. +To return values to Lua, a @N{C function} just pushes them onto the stack, +in direct order (the first result is pushed first), +and returns the number of results. +Any other value in the stack below the results will be properly +discarded by Lua. +Like a Lua function, a @N{C function} called by Lua can also return +many results. + +As an example, the following function receives a variable number +of numeric arguments and returns their average and their sum: +@verbatim{ +static int foo (lua_State *L) { + int n = lua_gettop(L); /* number of arguments */ + lua_Number sum = 0.0; + int i; + for (i = 1; i <= n; i++) { + if (!lua_isnumber(L, i)) { + lua_pushliteral(L, "incorrect argument"); + lua_error(L); + } + sum += lua_tonumber(L, i); + } + lua_pushnumber(L, sum/n); /* first result */ + lua_pushnumber(L, sum); /* second result */ + return 2; /* number of results */ +} +} + + + +} + + +@APIEntry{int lua_checkstack (lua_State *L, int n);| +@apii{0,0,-} + +Ensures that the stack has space for at least @id{n} extra slots +(that is, that you can safely push up to @id{n} values into it). +It returns false if it cannot fulfill the request, +either because it would cause the stack +to be larger than a fixed maximum size +(typically at least several thousand elements) or +because it cannot allocate memory for the extra space. +This function never shrinks the stack; +if the stack already has space for the extra slots, +it is left unchanged. + +} + +@APIEntry{void lua_close (lua_State *L);| +@apii{0,0,-} + +Destroys all objects in the given Lua state +(calling the corresponding garbage-collection metamethods, if any) +and frees all dynamic memory used by this state. +On several platforms, you may not need to call this function, +because all resources are naturally released when the host program ends. +On the other hand, long-running programs that create multiple states, +such as daemons or web servers, +will probably need to close states as soon as they are not needed. + +} + +@APIEntry{int lua_compare (lua_State *L, int index1, int index2, int op);| +@apii{0,0,e} + +Compares two Lua values. +Returns 1 if the value at index @id{index1} satisfies @id{op} +when compared with the value at index @id{index2}, +following the semantics of the corresponding Lua operator +(that is, it may call metamethods). +Otherwise @N{returns 0}. +Also @N{returns 0} if any of the indices is not valid. + +The value of @id{op} must be one of the following constants: +@description{ + +@item{@defid{LUA_OPEQ}| compares for equality (@T{==})} +@item{@defid{LUA_OPLT}| compares for less than (@T{<})} +@item{@defid{LUA_OPLE}| compares for less or equal (@T{<=})} + +} + +} + +@APIEntry{void lua_concat (lua_State *L, int n);| +@apii{n,1,e} + +Concatenates the @id{n} values at the top of the stack, +pops them, and leaves the result at the top. +If @N{@T{n} is 1}, the result is the single value on the stack +(that is, the function does nothing); +if @id{n} is 0, the result is the empty string. +Concatenation is performed following the usual semantics of Lua +@see{concat}. + +} + +@APIEntry{void lua_copy (lua_State *L, int fromidx, int toidx);| +@apii{0,0,-} + +Copies the element at index @id{fromidx} +into the valid index @id{toidx}, +replacing the value at that position. +Values at other positions are not affected. + +} + +@APIEntry{void lua_createtable (lua_State *L, int narr, int nrec);| +@apii{0,1,m} + +Creates a new empty table and pushes it onto the stack. +Parameter @id{narr} is a hint for how many elements the table +will have as a sequence; +parameter @id{nrec} is a hint for how many other elements +the table will have. +Lua may use these hints to preallocate memory for the new table. +This preallocation is useful for performance when you know in advance +how many elements the table will have. +Otherwise you can use the function @Lid{lua_newtable}. + +} + +@APIEntry{int lua_dump (lua_State *L, + lua_Writer writer, + void *data, + int strip);| +@apii{0,0,-} + +Dumps a function as a binary chunk. +Receives a Lua function on the top of the stack +and produces a binary chunk that, +if loaded again, +results in a function equivalent to the one dumped. +As it produces parts of the chunk, +@Lid{lua_dump} calls function @id{writer} @seeC{lua_Writer} +with the given @id{data} +to write them. + +If @id{strip} is true, +the binary representation may not include all debug information +about the function, +to save space. + +The value returned is the error code returned by the last +call to the writer; +@N{0 means} no errors. + +This function does not pop the Lua function from the stack. + +} + +@APIEntry{int lua_error (lua_State *L);| +@apii{1,0,v} + +Generates a Lua error, +using the value at the top of the stack as the error object. +This function does a long jump, +and therefore never returns +@seeC{luaL_error}. + +} + +@APIEntry{int lua_gc (lua_State *L, int what, int data);| +@apii{0,0,v} + +Controls the garbage collector. + +This function performs several tasks, +according to the value of the parameter @id{what}: +@description{ + +@item{@id{LUA_GCSTOP}| +stops the garbage collector. +} + +@item{@id{LUA_GCRESTART}| +restarts the garbage collector. +} + +@item{@id{LUA_GCCOLLECT}| +performs a full garbage-collection cycle. +} + +@item{@id{LUA_GCCOUNT}| +returns the current amount of memory (in Kbytes) in use by Lua. +} + +@item{@id{LUA_GCCOUNTB}| +returns the remainder of dividing the current amount of bytes of +memory in use by Lua by 1024. +} + +@item{@id{LUA_GCSTEP}| +performs an incremental step of garbage collection. +} + +@item{@id{LUA_GCSETPAUSE}| +sets @id{data} as the new value +for the @emph{pause} of the collector @see{GC} +and returns the previous value of the pause. +} + +@item{@id{LUA_GCSETSTEPMUL}| +sets @id{data} as the new value for the @emph{step multiplier} of +the collector @see{GC} +and returns the previous value of the step multiplier. +} + +@item{@id{LUA_GCISRUNNING}| +returns a boolean that tells whether the collector is running +(i.e., not stopped). +} + +} +For more details about these options, +see @Lid{collectgarbage}. + +This function may raise errors when calling finalizers. + +} + +@APIEntry{lua_Alloc lua_getallocf (lua_State *L, void **ud);| +@apii{0,0,-} + +Returns the @x{memory-allocation function} of a given state. +If @id{ud} is not @id{NULL}, Lua stores in @T{*ud} the +opaque pointer given when the memory-allocator function was set. + +} + +@APIEntry{int lua_getfield (lua_State *L, int index, const char *k);| +@apii{0,1,e} + +Pushes onto the stack the value @T{t[k]}, +where @id{t} is the value at the given index. +As in Lua, this function may trigger a metamethod +for the @Q{index} event @see{metatable}. + +Returns the type of the pushed value. + +} + +@APIEntry{void *lua_getextraspace (lua_State *L);| +@apii{0,0,-} + +Returns a pointer to a raw memory area associated with the +given Lua state. +The application can use this area for any purpose; +Lua does not use it for anything. + +Each new thread has this area initialized with a copy +of the area of the @x{main thread}. + +By default, this area has the size of a pointer to void, +but you can recompile Lua with a different size for this area. +(See @id{LUA_EXTRASPACE} in @id{luaconf.h}.) + +} + +@APIEntry{int lua_getglobal (lua_State *L, const char *name);| +@apii{0,1,e} + +Pushes onto the stack the value of the global @id{name}. +Returns the type of that value. + +} + +@APIEntry{int lua_geti (lua_State *L, int index, lua_Integer i);| +@apii{0,1,e} + +Pushes onto the stack the value @T{t[i]}, +where @id{t} is the value at the given index. +As in Lua, this function may trigger a metamethod +for the @Q{index} event @see{metatable}. + +Returns the type of the pushed value. + +} + +@APIEntry{int lua_getmetatable (lua_State *L, int index);| +@apii{0,0|1,-} + +If the value at the given index has a metatable, +the function pushes that metatable onto the stack and @N{returns 1}. +Otherwise, +the function @N{returns 0} and pushes nothing on the stack. + +} + +@APIEntry{int lua_gettable (lua_State *L, int index);| +@apii{1,1,e} + +Pushes onto the stack the value @T{t[k]}, +where @id{t} is the value at the given index +and @id{k} is the value at the top of the stack. + +This function pops the key from the stack, +pushing the resulting value in its place. +As in Lua, this function may trigger a metamethod +for the @Q{index} event @see{metatable}. + +Returns the type of the pushed value. + +} + +@APIEntry{int lua_gettop (lua_State *L);| +@apii{0,0,-} + +Returns the index of the top element in the stack. +Because indices start @N{at 1}, +this result is equal to the number of elements in the stack; +in particular, @N{0 means} an empty stack. + +} + +@APIEntry{int lua_getiuservalue (lua_State *L, int index, int n);| +@apii{0,1,-} + +Pushes onto the stack the @id{n}-th user value associated with the +full userdata at the given index and +returns the type of the pushed value. + +If the userdata does not have that value, +pushes @nil and returns @Lid{LUA_TNONE}. + +} + +@APIEntry{void lua_insert (lua_State *L, int index);| +@apii{1,1,-} + +Moves the top element into the given valid index, +shifting up the elements above this index to open space. +This function cannot be called with a pseudo-index, +because a pseudo-index is not an actual stack position. + +} + +@APIEntry{typedef @ldots lua_Integer;| + +The type of integers in Lua. + +By default this type is @id{long long}, +(usually a 64-bit two-complement integer), +but that can be changed to @id{long} or @id{int} +(usually a 32-bit two-complement integer). +(See @id{LUA_INT_TYPE} in @id{luaconf.h}.) + +Lua also defines the constants +@defid{LUA_MININTEGER} and @defid{LUA_MAXINTEGER}, +with the minimum and the maximum values that fit in this type. + +} + +@APIEntry{int lua_isboolean (lua_State *L, int index);| +@apii{0,0,-} + +Returns 1 if the value at the given index is a boolean, +and @N{0 otherwise}. + +} + +@APIEntry{int lua_iscfunction (lua_State *L, int index);| +@apii{0,0,-} + +Returns 1 if the value at the given index is a @N{C function}, +and @N{0 otherwise}. + +} + +@APIEntry{int lua_isfunction (lua_State *L, int index);| +@apii{0,0,-} + +Returns 1 if the value at the given index is a function +(either C or Lua), and @N{0 otherwise}. + +} + +@APIEntry{int lua_isinteger (lua_State *L, int index);| +@apii{0,0,-} + +Returns 1 if the value at the given index is an integer +(that is, the value is a number and is represented as an integer), +and @N{0 otherwise}. + +} + +@APIEntry{int lua_islightuserdata (lua_State *L, int index);| +@apii{0,0,-} + +Returns 1 if the value at the given index is a light userdata, +and @N{0 otherwise}. + +} + +@APIEntry{int lua_isnil (lua_State *L, int index);| +@apii{0,0,-} + +Returns 1 if the value at the given index is @nil, +and @N{0 otherwise}. + +} + +@APIEntry{int lua_isnone (lua_State *L, int index);| +@apii{0,0,-} + +Returns 1 if the given index is not valid, +and @N{0 otherwise}. + +} + +@APIEntry{int lua_isnoneornil (lua_State *L, int index);| +@apii{0,0,-} + +Returns 1 if the given index is not valid +or if the value at this index is @nil, +and @N{0 otherwise}. + +} + +@APIEntry{int lua_isnumber (lua_State *L, int index);| +@apii{0,0,-} + +Returns 1 if the value at the given index is a number +or a string convertible to a number, +and @N{0 otherwise}. + +} + +@APIEntry{int lua_isstring (lua_State *L, int index);| +@apii{0,0,-} + +Returns 1 if the value at the given index is a string +or a number (which is always convertible to a string), +and @N{0 otherwise}. + +} + +@APIEntry{int lua_istable (lua_State *L, int index);| +@apii{0,0,-} + +Returns 1 if the value at the given index is a table, +and @N{0 otherwise}. + +} + +@APIEntry{int lua_isthread (lua_State *L, int index);| +@apii{0,0,-} + +Returns 1 if the value at the given index is a thread, +and @N{0 otherwise}. + +} + +@APIEntry{int lua_isuserdata (lua_State *L, int index);| +@apii{0,0,-} + +Returns 1 if the value at the given index is a userdata +(either full or light), and @N{0 otherwise}. + +} + +@APIEntry{int lua_isyieldable (lua_State *L);| +@apii{0,0,-} + +Returns 1 if the given coroutine can yield, +and @N{0 otherwise}. + +} + +@APIEntry{typedef @ldots lua_KContext;| + +The type for continuation-function contexts. +It must be a numeric type. +This type is defined as @id{intptr_t} +when @id{intptr_t} is available, +so that it can store pointers too. +Otherwise, it is defined as @id{ptrdiff_t}. + +} + +@APIEntry{ +typedef int (*lua_KFunction) (lua_State *L, int status, lua_KContext ctx);| + +Type for continuation functions @see{continuations}. + +} + +@APIEntry{void lua_len (lua_State *L, int index);| +@apii{0,1,e} + +Returns the length of the value at the given index. +It is equivalent to the @Char{#} operator in Lua @see{len-op} and +may trigger a metamethod for the @Q{length} event @see{metatable}. +The result is pushed on the stack. + +} + +@APIEntry{ +int lua_load (lua_State *L, + lua_Reader reader, + void *data, + const char *chunkname, + const char *mode);| +@apii{0,1,-} + +Loads a Lua chunk without running it. +If there are no errors, +@id{lua_load} pushes the compiled chunk as a Lua +function on top of the stack. +Otherwise, it pushes an error message. + +The return values of @id{lua_load} are: +@description{ + +@item{@Lid{LUA_OK}| no errors;} + +@item{@defid{LUA_ERRSYNTAX}| +syntax error during precompilation;} + +@item{@Lid{LUA_ERRMEM}| +@x{memory allocation (out-of-memory) error};} + +@item{@Lid{LUA_ERRGCMM}| +error while running a @idx{__gc} metamethod. +(This error has no relation with the chunk being loaded. +It is generated by the garbage collector.) +} + +} + +The @id{lua_load} function uses a user-supplied @id{reader} function +to read the chunk @seeC{lua_Reader}. +The @id{data} argument is an opaque value passed to the reader function. + +The @id{chunkname} argument gives a name to the chunk, +which is used for error messages and in debug information @see{debugI}. + +@id{lua_load} automatically detects whether the chunk is text or binary +and loads it accordingly (see program @idx{luac}). +The string @id{mode} works as in function @Lid{load}, +with the addition that +a @id{NULL} value is equivalent to the string @St{bt}. + +@id{lua_load} uses the stack internally, +so the reader function must always leave the stack +unmodified when returning. + +If the resulting function has upvalues, +its first upvalue is set to the value of the @x{global environment} +stored at index @id{LUA_RIDX_GLOBALS} in the registry @see{registry}. +When loading main chunks, +this upvalue will be the @id{_ENV} variable @see{globalenv}. +Other upvalues are initialized with @nil. + +} + +@APIEntry{lua_State *lua_newstate (lua_Alloc f, void *ud);| +@apii{0,0,-} + +Creates a new thread running in a new, independent state. +Returns @id{NULL} if it cannot create the thread or the state +(due to lack of memory). +The argument @id{f} is the @x{allocator function}; +Lua does all memory allocation for this state +through this function @seeF{lua_Alloc}. +The second argument, @id{ud}, is an opaque pointer that Lua +passes to the allocator in every call. + +} + +@APIEntry{void lua_newtable (lua_State *L);| +@apii{0,1,m} + +Creates a new empty table and pushes it onto the stack. +It is equivalent to @T{lua_createtable(L, 0, 0)}. + +} + +@APIEntry{lua_State *lua_newthread (lua_State *L);| +@apii{0,1,m} + +Creates a new thread, pushes it on the stack, +and returns a pointer to a @Lid{lua_State} that represents this new thread. +The new thread returned by this function shares with the original thread +its global environment, +but has an independent execution stack. + +There is no explicit function to close or to destroy a thread. +Threads are subject to garbage collection, +like any Lua object. + +} + +@APIEntry{void *lua_newuserdatauv (lua_State *L, size_t size, int nuvalue);| +@apii{0,1,m} + +This function creates and pushes on the stack a new full userdata, +with @id{nuvalue} associated Lua values (called @id{user values}) +plus an associated block of raw memory with @id{size} bytes. +(The user values can be set and read with the functions +@Lid{lua_setiuservalue} and @Lid{lua_getiuservalue}.) + +The function returns the address of the block of memory. + +} + +@APIEntry{int lua_next (lua_State *L, int index);| +@apii{1,2|0,v} + +Pops a key from the stack, +and pushes a key@En{}value pair from the table at the given index +(the @Q{next} pair after the given key). +If there are no more elements in the table, +then @Lid{lua_next} returns 0 (and pushes nothing). + +A typical traversal looks like this: +@verbatim{ +/* table is in the stack at index 't' */ +lua_pushnil(L); /* first key */ +while (lua_next(L, t) != 0) { + /* uses 'key' (at index -2) and 'value' (at index -1) */ + printf("%s - %s\n", + lua_typename(L, lua_type(L, -2)), + lua_typename(L, lua_type(L, -1))); + /* removes 'value'; keeps 'key' for next iteration */ + lua_pop(L, 1); +} +} + +While traversing a table, +do not call @Lid{lua_tolstring} directly on a key, +unless you know that the key is actually a string. +Recall that @Lid{lua_tolstring} may change +the value at the given index; +this confuses the next call to @Lid{lua_next}. + +This function may raise an error if the given key +is neither @nil nor present in the table. +See function @Lid{next} for the caveats of modifying +the table during its traversal. + +} + +@APIEntry{typedef @ldots lua_Number;| + +The type of floats in Lua. + +By default this type is double, +but that can be changed to a single float or a long double. +(See @id{LUA_FLOAT_TYPE} in @id{luaconf.h}.) + +} + +@APIEntry{int lua_numbertointeger (lua_Number n, lua_Integer *p);| + +Converts a Lua float to a Lua integer. +This macro assumes that @id{n} has an integral value. +If that value is within the range of Lua integers, +it is converted to an integer and assigned to @T{*p}. +The macro results in a boolean indicating whether the +conversion was successful. +(Note that this range test can be tricky to do +correctly without this macro, +due to roundings.) + +This macro may evaluate its arguments more than once. + +} + +@APIEntry{int lua_pcall (lua_State *L, int nargs, int nresults, int msgh);| +@apii{nargs + 1,nresults|1,-} + +Calls a function (or a callable object) in protected mode. + +Both @id{nargs} and @id{nresults} have the same meaning as +in @Lid{lua_call}. +If there are no errors during the call, +@Lid{lua_pcall} behaves exactly like @Lid{lua_call}. +However, if there is any error, +@Lid{lua_pcall} catches it, +pushes a single value on the stack (the error object), +and returns an error code. +Like @Lid{lua_call}, +@Lid{lua_pcall} always removes the function +and its arguments from the stack. + +If @id{msgh} is 0, +then the error object returned on the stack +is exactly the original error object. +Otherwise, @id{msgh} is the stack index of a +@emph{message handler}. +(This index cannot be a pseudo-index.) +In case of runtime errors, +this function will be called with the error object +and its return value will be the object +returned on the stack by @Lid{lua_pcall}. + +Typically, the message handler is used to add more debug +information to the error object, such as a stack traceback. +Such information cannot be gathered after the return of @Lid{lua_pcall}, +since by then the stack has unwound. + +The @Lid{lua_pcall} function returns one of the following constants +(defined in @id{lua.h}): +@description{ + +@item{@defid{LUA_OK} (0)| +success.} + +@item{@defid{LUA_ERRRUN}| +a runtime error. +} + +@item{@defid{LUA_ERRMEM}| +@x{memory allocation error}. +For such errors, Lua does not call the @x{message handler}. +} + +@item{@defid{LUA_ERRERR}| +error while running the @x{message handler}. +} + +@item{@defid{LUA_ERRGCMM}| +error while running a @idx{__gc} metamethod. +For such errors, Lua does not call the @x{message handler} +(as this kind of error typically has no relation +with the function being called). +} + +} + +} + +@APIEntry{ +int lua_pcallk (lua_State *L, + int nargs, + int nresults, + int msgh, + lua_KContext ctx, + lua_KFunction k);| +@apii{nargs + 1,nresults|1,-} + +This function behaves exactly like @Lid{lua_pcall}, +but allows the called function to yield @see{continuations}. + +} + +@APIEntry{void lua_pop (lua_State *L, int n);| +@apii{n,0,-} + +Pops @id{n} elements from the stack. + +} + +@APIEntry{void lua_pushboolean (lua_State *L, int b);| +@apii{0,1,-} + +Pushes a boolean value with value @id{b} onto the stack. + +} + +@APIEntry{void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n);| +@apii{n,1,m} + +Pushes a new @N{C closure} onto the stack. +This function receives a pointer to a @N{C function} +and pushes onto the stack a Lua value of type @id{function} that, +when called, invokes the corresponding @N{C function}. +The parameter @id{n} tells how many upvalues this function will have +@see{c-closure}. + +Any function to be callable by Lua must +follow the correct protocol to receive its parameters +and return its results @seeC{lua_CFunction}. + +When a @N{C function} is created, +it is possible to associate some values with it, +thus creating a @x{@N{C closure}} @see{c-closure}; +these values are then accessible to the function whenever it is called. +To associate values with a @N{C function}, +first these values must be pushed onto the stack +(when there are multiple values, the first value is pushed first). +Then @Lid{lua_pushcclosure} +is called to create and push the @N{C function} onto the stack, +with the argument @id{n} telling how many values will be +associated with the function. +@Lid{lua_pushcclosure} also pops these values from the stack. + +The maximum value for @id{n} is 255. + +When @id{n} is zero, +this function creates a @def{light @N{C function}}, +which is just a pointer to the @N{C function}. +In that case, it never raises a memory error. + +} + +@APIEntry{void lua_pushcfunction (lua_State *L, lua_CFunction f);| +@apii{0,1,-} + +Pushes a @N{C function} onto the stack. + +} + +@APIEntry{const char *lua_pushfstring (lua_State *L, const char *fmt, ...);| +@apii{0,1,v} + +Pushes onto the stack a formatted string +and returns a pointer to this string. +It is similar to the @ANSI{sprintf}, +but has two important differences. +First, +you do not have to allocate space for the result; +the result is a Lua string and Lua takes care of memory allocation +(and deallocation, through garbage collection). +Second, +the conversion specifiers are quite restricted. +There are no flags, widths, or precisions. +The conversion specifiers can only be +@Char{%%} (inserts the character @Char{%}), +@Char{%s} (inserts a zero-terminated string, with no size restrictions), +@Char{%f} (inserts a @Lid{lua_Number}), +@Char{%I} (inserts a @Lid{lua_Integer}), +@Char{%p} (inserts a pointer as a hexadecimal numeral), +@Char{%d} (inserts an @T{int}), +@Char{%c} (inserts an @T{int} as a one-byte character), and +@Char{%U} (inserts a @T{long int} as a @x{UTF-8} byte sequence). + +This function may raise errors due to memory overflow +or an invalid conversion specifier. + +} + +@APIEntry{void lua_pushglobaltable (lua_State *L);| +@apii{0,1,-} + +Pushes the @x{global environment} onto the stack. + +} + +@APIEntry{void lua_pushinteger (lua_State *L, lua_Integer n);| +@apii{0,1,-} + +Pushes an integer with value @id{n} onto the stack. + +} + +@APIEntry{void lua_pushlightuserdata (lua_State *L, void *p);| +@apii{0,1,-} + +Pushes a light userdata onto the stack. + +Userdata represent @N{C values} in Lua. +A @def{light userdata} represents a pointer, a @T{void*}. +It is a value (like a number): +you do not create it, it has no individual metatable, +and it is not collected (as it was never created). +A light userdata is equal to @Q{any} +light userdata with the same @N{C address}. + +} + +@APIEntry{const char *lua_pushliteral (lua_State *L, const char *s);| +@apii{0,1,m} + +This macro is equivalent to @Lid{lua_pushstring}, +but should be used only when @id{s} is a literal string. + +} + +@APIEntry{const char *lua_pushlstring (lua_State *L, const char *s, size_t len);| +@apii{0,1,m} + +Pushes the string pointed to by @id{s} with size @id{len} +onto the stack. +Lua makes (or reuses) an internal copy of the given string, +so the memory at @id{s} can be freed or reused immediately after +the function returns. +The string can contain any binary data, +including @x{embedded zeros}. + +Returns a pointer to the internal copy of the string. + +} + +@APIEntry{void lua_pushnil (lua_State *L);| +@apii{0,1,-} + +Pushes a nil value onto the stack. + +} + +@APIEntry{void lua_pushnumber (lua_State *L, lua_Number n);| +@apii{0,1,-} + +Pushes a float with value @id{n} onto the stack. + +} + +@APIEntry{const char *lua_pushstring (lua_State *L, const char *s);| +@apii{0,1,m} + +Pushes the zero-terminated string pointed to by @id{s} +onto the stack. +Lua makes (or reuses) an internal copy of the given string, +so the memory at @id{s} can be freed or reused immediately after +the function returns. + +Returns a pointer to the internal copy of the string. + +If @id{s} is @id{NULL}, pushes @nil and returns @id{NULL}. + +} + +@APIEntry{int lua_pushthread (lua_State *L);| +@apii{0,1,-} + +Pushes the thread represented by @id{L} onto the stack. +Returns 1 if this thread is the @x{main thread} of its state. + +} + +@APIEntry{void lua_pushvalue (lua_State *L, int index);| +@apii{0,1,-} + +Pushes a copy of the element at the given index +onto the stack. + +} + +@APIEntry{ +const char *lua_pushvfstring (lua_State *L, + const char *fmt, + va_list argp);| +@apii{0,1,v} + +Equivalent to @Lid{lua_pushfstring}, except that it receives a @id{va_list} +instead of a variable number of arguments. + +} + +@APIEntry{int lua_rawequal (lua_State *L, int index1, int index2);| +@apii{0,0,-} + +Returns 1 if the two values in indices @id{index1} and +@id{index2} are primitively equal +(that is, without calling the @idx{__eq} metamethod). +Otherwise @N{returns 0}. +Also @N{returns 0} if any of the indices are not valid. + +} + +@APIEntry{int lua_rawget (lua_State *L, int index);| +@apii{1,1,-} + +Similar to @Lid{lua_gettable}, but does a raw access +(i.e., without metamethods). + +} + +@APIEntry{int lua_rawgeti (lua_State *L, int index, lua_Integer n);| +@apii{0,1,-} + +Pushes onto the stack the value @T{t[n]}, +where @id{t} is the table at the given index. +The access is raw, +that is, it does not invoke the @idx{__index} metamethod. + +Returns the type of the pushed value. + +} + +@APIEntry{int lua_rawgetp (lua_State *L, int index, const void *p);| +@apii{0,1,-} + +Pushes onto the stack the value @T{t[k]}, +where @id{t} is the table at the given index and +@id{k} is the pointer @id{p} represented as a light userdata. +The access is raw; +that is, it does not invoke the @idx{__index} metamethod. + +Returns the type of the pushed value. + +} + +@APIEntry{lua_Unsigned lua_rawlen (lua_State *L, int index);| +@apii{0,0,-} + +Returns the raw @Q{length} of the value at the given index: +for strings, this is the string length; +for tables, this is the result of the length operator (@Char{#}) +with no metamethods; +for userdata, this is the size of the block of memory allocated +for the userdata; +for other values, it @N{is 0}. + +} + +@APIEntry{void lua_rawset (lua_State *L, int index);| +@apii{2,0,m} + +Similar to @Lid{lua_settable}, but does a raw assignment +(i.e., without metamethods). + +} + +@APIEntry{void lua_rawseti (lua_State *L, int index, lua_Integer i);| +@apii{1,0,m} + +Does the equivalent of @T{t[i] = v}, +where @id{t} is the table at the given index +and @id{v} is the value at the top of the stack. + +This function pops the value from the stack. +The assignment is raw, +that is, it does not invoke the @idx{__newindex} metamethod. + +} + +@APIEntry{void lua_rawsetp (lua_State *L, int index, const void *p);| +@apii{1,0,m} + +Does the equivalent of @T{t[p] = v}, +where @id{t} is the table at the given index, +@id{p} is encoded as a light userdata, +and @id{v} is the value at the top of the stack. + +This function pops the value from the stack. +The assignment is raw, +that is, it does not invoke @idx{__newindex} metamethod. + +} + +@APIEntry{ +typedef const char * (*lua_Reader) (lua_State *L, + void *data, + size_t *size);| + +The reader function used by @Lid{lua_load}. +Every time it needs another piece of the chunk, +@Lid{lua_load} calls the reader, +passing along its @id{data} parameter. +The reader must return a pointer to a block of memory +with a new piece of the chunk +and set @id{size} to the block size. +The block must exist until the reader function is called again. +To signal the end of the chunk, +the reader must return @id{NULL} or set @id{size} to zero. +The reader function may return pieces of any size greater than zero. + +} + +@APIEntry{void lua_register (lua_State *L, const char *name, lua_CFunction f);| +@apii{0,0,e} + +Sets the @N{C function} @id{f} as the new value of global @id{name}. +It is defined as a macro: +@verbatim{ +#define lua_register(L,n,f) \ + (lua_pushcfunction(L, f), lua_setglobal(L, n)) +} + +} + +@APIEntry{void lua_remove (lua_State *L, int index);| +@apii{1,0,-} + +Removes the element at the given valid index, +shifting down the elements above this index to fill the gap. +This function cannot be called with a pseudo-index, +because a pseudo-index is not an actual stack position. + +} + +@APIEntry{void lua_replace (lua_State *L, int index);| +@apii{1,0,-} + +Moves the top element into the given valid index +without shifting any element +(therefore replacing the value at that given index), +and then pops the top element. + +} + +@APIEntry{int lua_resume (lua_State *L, lua_State *from, int nargs, + int *nresults);| +@apii{?,?,-} + +Starts and resumes a coroutine in the given thread @id{L}. + +To start a coroutine, +you push onto the thread stack the main function plus any arguments; +then you call @Lid{lua_resume}, +with @id{nargs} being the number of arguments. +This call returns when the coroutine suspends or finishes its execution. +When it returns, +@id{nresults} is updated and +the top of the stack contains +the @id{nresults} values passed to @Lid{lua_yield} +or returned by the body function. +@Lid{lua_resume} returns +@Lid{LUA_YIELD} if the coroutine yields, +@Lid{LUA_OK} if the coroutine finishes its execution +without errors, +or an error code in case of errors @seeC{lua_pcall}. + +In case of errors, +the stack is not unwound, +so you can use the debug API over it. +The error object is on the top of the stack. + +To resume a coroutine, +you remove all results from the last @Lid{lua_yield}, +put on its stack only the values to +be passed as results from @id{yield}, +and then call @Lid{lua_resume}. + +The parameter @id{from} represents the coroutine that is resuming @id{L}. +If there is no such coroutine, +this parameter can be @id{NULL}. + +} + +@APIEntry{void lua_rotate (lua_State *L, int idx, int n);| +@apii{0,0,-} + +Rotates the stack elements between the valid index @id{idx} +and the top of the stack. +The elements are rotated @id{n} positions in the direction of the top, +for a positive @id{n}, +or @T{-n} positions in the direction of the bottom, +for a negative @id{n}. +The absolute value of @id{n} must not be greater than the size +of the slice being rotated. +This function cannot be called with a pseudo-index, +because a pseudo-index is not an actual stack position. + +} + +@APIEntry{void lua_setallocf (lua_State *L, lua_Alloc f, void *ud);| +@apii{0,0,-} + +Changes the @x{allocator function} of a given state to @id{f} +with user data @id{ud}. + +} + +@APIEntry{void lua_setfield (lua_State *L, int index, const char *k);| +@apii{1,0,e} + +Does the equivalent to @T{t[k] = v}, +where @id{t} is the value at the given index +and @id{v} is the value at the top of the stack. + +This function pops the value from the stack. +As in Lua, this function may trigger a metamethod +for the @Q{newindex} event @see{metatable}. + +} + +@APIEntry{void lua_setglobal (lua_State *L, const char *name);| +@apii{1,0,e} + +Pops a value from the stack and +sets it as the new value of global @id{name}. + +} + +@APIEntry{void lua_seti (lua_State *L, int index, lua_Integer n);| +@apii{1,0,e} + +Does the equivalent to @T{t[n] = v}, +where @id{t} is the value at the given index +and @id{v} is the value at the top of the stack. + +This function pops the value from the stack. +As in Lua, this function may trigger a metamethod +for the @Q{newindex} event @see{metatable}. + +} + +@APIEntry{void lua_setmetatable (lua_State *L, int index);| +@apii{1,0,-} + +Pops a table from the stack and +sets it as the new metatable for the value at the given index. + +} + +@APIEntry{void lua_settable (lua_State *L, int index);| +@apii{2,0,e} + +Does the equivalent to @T{t[k] = v}, +where @id{t} is the value at the given index, +@id{v} is the value at the top of the stack, +and @id{k} is the value just below the top. + +This function pops both the key and the value from the stack. +As in Lua, this function may trigger a metamethod +for the @Q{newindex} event @see{metatable}. + +} + +@APIEntry{void lua_settop (lua_State *L, int index);| +@apii{?,?,-} + +Accepts any index, @N{or 0}, +and sets the stack top to this index. +If the new top is larger than the old one, +then the new elements are filled with @nil. +If @id{index} @N{is 0}, then all stack elements are removed. + +} + +@APIEntry{int lua_setiuservalue (lua_State *L, int index, int n);| +@apii{1,0,-} + +Pops a value from the stack and sets it as +the new @id{n}-th user value associated to the +full userdata at the given index. +Returns 0 if the userdata does not have that value. + +} + +@APIEntry{typedef struct lua_State lua_State;| + +An opaque structure that points to a thread and indirectly +(through the thread) to the whole state of a Lua interpreter. +The Lua library is fully reentrant: +it has no global variables. +All information about a state is accessible through this structure. + +A pointer to this structure must be passed as the first argument to +every function in the library, except to @Lid{lua_newstate}, +which creates a Lua state from scratch. + +} + +@APIEntry{int lua_status (lua_State *L);| +@apii{0,0,-} + +Returns the status of the thread @id{L}. + +The status can be 0 (@Lid{LUA_OK}) for a normal thread, +an error code if the thread finished the execution +of a @Lid{lua_resume} with an error, +or @defid{LUA_YIELD} if the thread is suspended. + +You can only call functions in threads with status @Lid{LUA_OK}. +You can resume threads with status @Lid{LUA_OK} +(to start a new coroutine) or @Lid{LUA_YIELD} +(to resume a coroutine). + +} + +@APIEntry{size_t lua_stringtonumber (lua_State *L, const char *s);| +@apii{0,1,-} + +Converts the zero-terminated string @id{s} to a number, +pushes that number into the stack, +and returns the total size of the string, +that is, its length plus one. +The conversion can result in an integer or a float, +according to the lexical conventions of Lua @see{lexical}. +The string may have leading and trailing spaces and a sign. +If the string is not a valid numeral, +returns 0 and pushes nothing. +(Note that the result can be used as a boolean, +true if the conversion succeeds.) + +} + +@APIEntry{int lua_toboolean (lua_State *L, int index);| +@apii{0,0,-} + +Converts the Lua value at the given index to a @N{C boolean} +value (@N{0 or 1}). +Like all tests in Lua, +@Lid{lua_toboolean} returns true for any Lua value +different from @false and @nil; +otherwise it returns false. +(If you want to accept only actual boolean values, +use @Lid{lua_isboolean} to test the value's type.) + +} + +@APIEntry{lua_CFunction lua_tocfunction (lua_State *L, int index);| +@apii{0,0,-} + +Converts a value at the given index to a @N{C function}. +That value must be a @N{C function}; +otherwise, returns @id{NULL}. + +} + +@APIEntry{lua_Integer lua_tointeger (lua_State *L, int index);| +@apii{0,0,-} + +Equivalent to @Lid{lua_tointegerx} with @id{isnum} equal to @id{NULL}. + +} + +@APIEntry{lua_Integer lua_tointegerx (lua_State *L, int index, int *isnum);| +@apii{0,0,-} + +Converts the Lua value at the given index +to the signed integral type @Lid{lua_Integer}. +The Lua value must be an integer, +or a number or string convertible to an integer @see{coercion}; +otherwise, @id{lua_tointegerx} @N{returns 0}. + +If @id{isnum} is not @id{NULL}, +its referent is assigned a boolean value that +indicates whether the operation succeeded. + +} + +@APIEntry{const char *lua_tolstring (lua_State *L, int index, size_t *len);| +@apii{0,0,m} + +Converts the Lua value at the given index to a @N{C string}. +If @id{len} is not @id{NULL}, +it sets @T{*len} with the string length. +The Lua value must be a string or a number; +otherwise, the function returns @id{NULL}. +If the value is a number, +then @id{lua_tolstring} also +@emph{changes the actual value in the stack to a string}. +(This change confuses @Lid{lua_next} +when @id{lua_tolstring} is applied to keys during a table traversal.) + +@id{lua_tolstring} returns a pointer +to a string inside the Lua state. +This string always has a zero (@Char{\0}) +after its last character (as @N{in C}), +but can contain other zeros in its body. + +Because Lua has garbage collection, +there is no guarantee that the pointer returned by @id{lua_tolstring} +will be valid after the corresponding Lua value is removed from the stack. + +} + +@APIEntry{lua_Number lua_tonumber (lua_State *L, int index);| +@apii{0,0,-} + +Equivalent to @Lid{lua_tonumberx} with @id{isnum} equal to @id{NULL}. + +} + +@APIEntry{lua_Number lua_tonumberx (lua_State *L, int index, int *isnum);| +@apii{0,0,-} + +Converts the Lua value at the given index +to the @N{C type} @Lid{lua_Number} @seeC{lua_Number}. +The Lua value must be a number or a string convertible to a number +@see{coercion}; +otherwise, @Lid{lua_tonumberx} @N{returns 0}. + +If @id{isnum} is not @id{NULL}, +its referent is assigned a boolean value that +indicates whether the operation succeeded. + +} + +@APIEntry{const void *lua_topointer (lua_State *L, int index);| +@apii{0,0,-} + +Converts the value at the given index to a generic +@N{C pointer} (@T{void*}). +The value can be a userdata, a table, a thread, or a function; +otherwise, @id{lua_topointer} returns @id{NULL}. +Different objects will give different pointers. +There is no way to convert the pointer back to its original value. + +Typically this function is used only for hashing and debug information. + +} + +@APIEntry{const char *lua_tostring (lua_State *L, int index);| +@apii{0,0,m} + +Equivalent to @Lid{lua_tolstring} with @id{len} equal to @id{NULL}. + +} + +@APIEntry{lua_State *lua_tothread (lua_State *L, int index);| +@apii{0,0,-} + +Converts the value at the given index to a Lua thread +(represented as @T{lua_State*}). +This value must be a thread; +otherwise, the function returns @id{NULL}. + +} + +@APIEntry{void *lua_touserdata (lua_State *L, int index);| +@apii{0,0,-} + +If the value at the given index is a full userdata, +returns its memory-block address. +If the value is a light userdata, +returns its pointer. +Otherwise, returns @id{NULL}. + +} + +@APIEntry{int lua_type (lua_State *L, int index);| +@apii{0,0,-} + +Returns the type of the value in the given valid index, +or @id{LUA_TNONE} for a non-valid (but acceptable) index. +The types returned by @Lid{lua_type} are coded by the following constants +defined in @id{lua.h}: +@defid{LUA_TNIL}, +@defid{LUA_TNUMBER}, +@defid{LUA_TBOOLEAN}, +@defid{LUA_TSTRING}, +@defid{LUA_TTABLE}, +@defid{LUA_TFUNCTION}, +@defid{LUA_TUSERDATA}, +@defid{LUA_TTHREAD}, +and +@defid{LUA_TLIGHTUSERDATA}. + +} + +@APIEntry{const char *lua_typename (lua_State *L, int tp);| +@apii{0,0,-} + +Returns the name of the type encoded by the value @id{tp}, +which must be one the values returned by @Lid{lua_type}. + +} + +@APIEntry{typedef @ldots lua_Unsigned;| + +The unsigned version of @Lid{lua_Integer}. + +} + +@APIEntry{int lua_upvalueindex (int i);| +@apii{0,0,-} + +Returns the pseudo-index that represents the @id{i}-th upvalue of +the running function @see{c-closure}. + +} + +@APIEntry{lua_Number lua_version (lua_State *L);| +@apii{0,0,-} + +Returns the version number of this core. + +} + +@APIEntry{ +typedef int (*lua_Writer) (lua_State *L, + const void* p, + size_t sz, + void* ud);| + +The type of the writer function used by @Lid{lua_dump}. +Every time it produces another piece of chunk, +@Lid{lua_dump} calls the writer, +passing along the buffer to be written (@id{p}), +its size (@id{sz}), +and the @id{data} parameter supplied to @Lid{lua_dump}. + +The writer returns an error code: +@N{0 means} no errors; +any other value means an error and stops @Lid{lua_dump} from +calling the writer again. + +} + +@APIEntry{void lua_xmove (lua_State *from, lua_State *to, int n);| +@apii{?,?,-} + +Exchange values between different threads of the same state. + +This function pops @id{n} values from the stack @id{from}, +and pushes them onto the stack @id{to}. + +} + +@APIEntry{int lua_yield (lua_State *L, int nresults);| +@apii{?,?,v} + +This function is equivalent to @Lid{lua_yieldk}, +but it has no continuation @see{continuations}. +Therefore, when the thread resumes, +it continues the function that called +the function calling @id{lua_yield}. +To avoid surprises, +this function should be called only in a tail call. + +} + + +@APIEntry{ +int lua_yieldk (lua_State *L, + int nresults, + lua_KContext ctx, + lua_KFunction k);| +@apii{?,?,v} + +Yields a coroutine (thread). + +When a @N{C function} calls @Lid{lua_yieldk}, +the running coroutine suspends its execution, +and the call to @Lid{lua_resume} that started this coroutine returns. +The parameter @id{nresults} is the number of values from the stack +that will be passed as results to @Lid{lua_resume}. + +When the coroutine is resumed again, +Lua calls the given @x{continuation function} @id{k} to continue +the execution of the @N{C function} that yielded @see{continuations}. +This continuation function receives the same stack +from the previous function, +with the @id{n} results removed and +replaced by the arguments passed to @Lid{lua_resume}. +Moreover, +the continuation function receives the value @id{ctx} +that was passed to @Lid{lua_yieldk}. + +Usually, this function does not return; +when the coroutine eventually resumes, +it continues executing the continuation function. +However, there is one special case, +which is when this function is called +from inside a line or a count hook @see{debugI}. +In that case, @id{lua_yieldk} should be called with no continuation +(probably in the form of @Lid{lua_yield}) and no results, +and the hook should return immediately after the call. +Lua will yield and, +when the coroutine resumes again, +it will continue the normal execution +of the (Lua) function that triggered the hook. + +This function can raise an error if it is called from a thread +with a pending C call with no continuation function +(what is called a @emphx{C-call boundary}, +or it is called from a thread that is not running inside a resume +(typically the main thread). + +} + +} + +@sect2{debugI| @title{The Debug Interface} + +Lua has no built-in debugging facilities. +Instead, it offers a special interface +by means of functions and @emph{hooks}. +This interface allows the construction of different +kinds of debuggers, profilers, and other tools +that need @Q{inside information} from the interpreter. + + +@APIEntry{ +typedef struct lua_Debug { + int event; + const char *name; /* (n) */ + const char *namewhat; /* (n) */ + const char *what; /* (S) */ + const char *source; /* (S) */ + int currentline; /* (l) */ + int linedefined; /* (S) */ + int lastlinedefined; /* (S) */ + unsigned char nups; /* (u) number of upvalues */ + unsigned char nparams; /* (u) number of parameters */ + char isvararg; /* (u) */ + char istailcall; /* (t) */ + unsigned short ftransfer; /* (r) index of first value transferred */ + unsigned short ntransfer; /* (r) number of transferred values */ + char short_src[LUA_IDSIZE]; /* (S) */ + /* private part */ + @rep{other fields} +} lua_Debug; +| + +A structure used to carry different pieces of +information about a function or an activation record. +@Lid{lua_getstack} fills only the private part +of this structure, for later use. +To fill the other fields of @Lid{lua_Debug} with useful information, +call @Lid{lua_getinfo}. + +The fields of @Lid{lua_Debug} have the following meaning: +@description{ + +@item{@id{source}| +the name of the chunk that created the function. +If @T{source} starts with a @Char{@At}, +it means that the function was defined in a file where +the file name follows the @Char{@At}. +If @T{source} starts with a @Char{=}, +the remainder of its contents describe the source in a user-dependent manner. +Otherwise, +the function was defined in a string where +@T{source} is that string. +} + +@item{@id{short_src}| +a @Q{printable} version of @T{source}, to be used in error messages. +} + +@item{@id{linedefined}| +the line number where the definition of the function starts. +} + +@item{@id{lastlinedefined}| +the line number where the definition of the function ends. +} + +@item{@id{what}| +the string @T{"Lua"} if the function is a Lua function, +@T{"C"} if it is a @N{C function}, +@T{"main"} if it is the main part of a chunk. +} + +@item{@id{currentline}| +the current line where the given function is executing. +When no line information is available, +@T{currentline} is set to @num{-1}. +} + +@item{@id{name}| +a reasonable name for the given function. +Because functions in Lua are first-class values, +they do not have a fixed name: +some functions can be the value of multiple global variables, +while others can be stored only in a table field. +The @T{lua_getinfo} function checks how the function was +called to find a suitable name. +If it cannot find a name, +then @id{name} is set to @id{NULL}. +} + +@item{@id{namewhat}| +explains the @T{name} field. +The value of @T{namewhat} can be +@T{"global"}, @T{"local"}, @T{"method"}, +@T{"field"}, @T{"upvalue"}, or @T{""} (the empty string), +according to how the function was called. +(Lua uses the empty string when no other option seems to apply.) +} + +@item{@id{istailcall}| +true if this function invocation was called by a tail call. +In this case, the caller of this level is not in the stack. +} + +@item{@id{nups}| +the number of upvalues of the function. +} + +@item{@id{nparams}| +the number of parameters of the function +(always @N{0 for} @N{C functions}). +} + +@item{@id{isvararg}| +true if the function is a vararg function +(always true for @N{C functions}). +} + +@item{@id{ftransfer}| +the index on the stack of the first value being @Q{transferred}, +that is, parameters in a call or return values in a return. +(The other values are in consecutive indices.) +Using this index, you can access and modify these values +through @Lid{lua_getlocal} and @Lid{lua_setlocal}. +This field is only meaningful during a +call hook, denoting the first parameter, +or a return hook, denoting the first value being returned. +(For call hooks, this value is always 1.) +} + +@item{@id{ntransfer}| +The number of values being transferred (see previous item). +(For calls of Lua functions, +this value is always equal to @id{nparams}.) +} + +} + +} + +@APIEntry{lua_Hook lua_gethook (lua_State *L);| +@apii{0,0,-} + +Returns the current hook function. + +} + +@APIEntry{int lua_gethookcount (lua_State *L);| +@apii{0,0,-} + +Returns the current hook count. + +} + +@APIEntry{int lua_gethookmask (lua_State *L);| +@apii{0,0,-} + +Returns the current hook mask. + +} + +@APIEntry{int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar);| +@apii{0|1,0|1|2,m} + +Gets information about a specific function or function invocation. + +To get information about a function invocation, +the parameter @id{ar} must be a valid activation record that was +filled by a previous call to @Lid{lua_getstack} or +given as argument to a hook @seeC{lua_Hook}. + +To get information about a function, you push it onto the stack +and start the @id{what} string with the character @Char{>}. +(In that case, +@id{lua_getinfo} pops the function from the top of the stack.) +For instance, to know in which line a function @id{f} was defined, +you can write the following code: +@verbatim{ +lua_Debug ar; +lua_getglobal(L, "f"); /* get global 'f' */ +lua_getinfo(L, ">S", &ar); +printf("%d\n", ar.linedefined); +} + +Each character in the string @id{what} +selects some fields of the structure @id{ar} to be filled or +a value to be pushed on the stack: +@description{ + +@item{@Char{n}| fills in the field @id{name} and @id{namewhat}; +} + +@item{@Char{S}| +fills in the fields @id{source}, @id{short_src}, +@id{linedefined}, @id{lastlinedefined}, and @id{what}; +} + +@item{@Char{l}| fills in the field @id{currentline}; +} + +@item{@Char{t}| fills in the field @id{istailcall}; +} + +@item{@Char{u}| fills in the fields +@id{nups}, @id{nparams}, and @id{isvararg}; +} + +@item{@Char{f}| +pushes onto the stack the function that is +running at the given level; +} + +@item{@Char{L}| +pushes onto the stack a table whose indices are the +numbers of the lines that are valid on the function. +(A @emph{valid line} is a line with some associated code, +that is, a line where you can put a break point. +Non-valid lines include empty lines and comments.) + +If this option is given together with option @Char{f}, +its table is pushed after the function. + +This is the only option that can raise a memory error. +} + +} + +This function returns 0 if given an invalid option in @id{what}. + +} + +@APIEntry{const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n);| +@apii{0,0|1,-} + +Gets information about a local variable or a temporary value +of a given activation record or a given function. + +In the first case, +the parameter @id{ar} must be a valid activation record that was +filled by a previous call to @Lid{lua_getstack} or +given as argument to a hook @seeC{lua_Hook}. +The index @id{n} selects which local variable to inspect; +see @Lid{debug.getlocal} for details about variable indices +and names. + +@Lid{lua_getlocal} pushes the variable's value onto the stack +and returns its name. + +In the second case, @id{ar} must be @id{NULL} and the function +to be inspected must be at the top of the stack. +In this case, only parameters of Lua functions are visible +(as there is no information about what variables are active) +and no values are pushed onto the stack. + +Returns @id{NULL} (and pushes nothing) +when the index is greater than +the number of active local variables. + +} + +@APIEntry{int lua_getstack (lua_State *L, int level, lua_Debug *ar);| +@apii{0,0,-} + +Gets information about the interpreter runtime stack. + +This function fills parts of a @Lid{lua_Debug} structure with +an identification of the @emph{activation record} +of the function executing at a given level. +@N{Level 0} is the current running function, +whereas level @M{n+1} is the function that has called level @M{n} +(except for tail calls, which do not count on the stack). +When there are no errors, @Lid{lua_getstack} returns 1; +when called with a level greater than the stack depth, +it returns 0. + +} + +@APIEntry{const char *lua_getupvalue (lua_State *L, int funcindex, int n);| +@apii{0,0|1,-} + +Gets information about the @id{n}-th upvalue +of the closure at index @id{funcindex}. +It pushes the upvalue's value onto the stack +and returns its name. +Returns @id{NULL} (and pushes nothing) +when the index @id{n} is greater than the number of upvalues. + +For @N{C functions}, this function uses the empty string @T{""} +as a name for all upvalues. +(For Lua functions, +upvalues are the external local variables that the function uses, +and that are consequently included in its closure.) + +Upvalues have no particular order, +as they are active through the whole function. +They are numbered in an arbitrary order. + +} + +@APIEntry{typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar);| + +Type for debugging hook functions. + +Whenever a hook is called, its @id{ar} argument has its field +@id{event} set to the specific event that triggered the hook. +Lua identifies these events with the following constants: +@defid{LUA_HOOKCALL}, @defid{LUA_HOOKRET}, +@defid{LUA_HOOKTAILCALL}, @defid{LUA_HOOKLINE}, +and @defid{LUA_HOOKCOUNT}. +Moreover, for line events, the field @id{currentline} is also set. +To get the value of any other field in @id{ar}, +the hook must call @Lid{lua_getinfo}. + +For call events, @id{event} can be @id{LUA_HOOKCALL}, +the normal value, or @id{LUA_HOOKTAILCALL}, for a tail call; +in this case, there will be no corresponding return event. + +While Lua is running a hook, it disables other calls to hooks. +Therefore, if a hook calls back Lua to execute a function or a chunk, +this execution occurs without any calls to hooks. + +Hook functions cannot have continuations, +that is, they cannot call @Lid{lua_yieldk}, +@Lid{lua_pcallk}, or @Lid{lua_callk} with a non-null @id{k}. + +Hook functions can yield under the following conditions: +Only count and line events can yield; +to yield, a hook function must finish its execution +calling @Lid{lua_yield} with @id{nresults} equal to zero +(that is, with no values). + +} + +@APIEntry{void lua_sethook (lua_State *L, lua_Hook f, int mask, int count);| +@apii{0,0,-} + +Sets the debugging hook function. + +Argument @id{f} is the hook function. +@id{mask} specifies on which events the hook will be called: +it is formed by a bitwise OR of the constants +@defid{LUA_MASKCALL}, +@defid{LUA_MASKRET}, +@defid{LUA_MASKLINE}, +and @defid{LUA_MASKCOUNT}. +The @id{count} argument is only meaningful when the mask +includes @id{LUA_MASKCOUNT}. +For each event, the hook is called as explained below: +@description{ + +@item{The call hook| is called when the interpreter calls a function. +The hook is called just after Lua enters the new function, +before the function gets its arguments. +} + +@item{The return hook| is called when the interpreter returns from a function. +The hook is called just before Lua leaves the function. +There is no standard way to access the values +to be returned by the function. +} + +@item{The line hook| is called when the interpreter is about to +start the execution of a new line of code, +or when it jumps back in the code (even to the same line). +(This event only happens while Lua is executing a Lua function.) +} + +@item{The count hook| is called after the interpreter executes every +@T{count} instructions. +(This event only happens while Lua is executing a Lua function.) +} + +} + +A hook is disabled by setting @id{mask} to zero. + +} + +@APIEntry{const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n);| +@apii{0|1,0,-} + +Sets the value of a local variable of a given activation record. +It assigns the value at the top of the stack +to the variable and returns its name. +It also pops the value from the stack. + +Returns @id{NULL} (and pops nothing) +when the index is greater than +the number of active local variables. + +Parameters @id{ar} and @id{n} are as in function @Lid{lua_getlocal}. + +} + +@APIEntry{const char *lua_setupvalue (lua_State *L, int funcindex, int n);| +@apii{0|1,0,-} + +Sets the value of a closure's upvalue. +It assigns the value at the top of the stack +to the upvalue and returns its name. +It also pops the value from the stack. + +Returns @id{NULL} (and pops nothing) +when the index @id{n} is greater than the number of upvalues. + +Parameters @id{funcindex} and @id{n} are as in function @Lid{lua_getupvalue}. + +} + +@APIEntry{void *lua_upvalueid (lua_State *L, int funcindex, int n);| +@apii{0,0,-} + +Returns a unique identifier for the upvalue numbered @id{n} +from the closure at index @id{funcindex}. + +These unique identifiers allow a program to check whether different +closures share upvalues. +Lua closures that share an upvalue +(that is, that access a same external local variable) +will return identical ids for those upvalue indices. + +Parameters @id{funcindex} and @id{n} are as in function @Lid{lua_getupvalue}, +but @id{n} cannot be greater than the number of upvalues. + +} + +@APIEntry{ +void lua_upvaluejoin (lua_State *L, int funcindex1, int n1, + int funcindex2, int n2);| +@apii{0,0,-} + +Make the @id{n1}-th upvalue of the Lua closure at index @id{funcindex1} +refer to the @id{n2}-th upvalue of the Lua closure at index @id{funcindex2}. + +} + +} + +} + + +@C{-------------------------------------------------------------------------} +@sect1{@title{The Auxiliary Library} + +@index{lauxlib.h} +The @def{auxiliary library} provides several convenient functions +to interface C with Lua. +While the basic API provides the primitive functions for all +interactions between C and Lua, +the auxiliary library provides higher-level functions for some +common tasks. + +All functions and types from the auxiliary library +are defined in header file @id{lauxlib.h} and +have a prefix @id{luaL_}. + +All functions in the auxiliary library are built on +top of the basic API, +and so they provide nothing that cannot be done with that API. +Nevertheless, the use of the auxiliary library ensures +more consistency to your code. + + +Several functions in the auxiliary library use internally some +extra stack slots. +When a function in the auxiliary library uses less than five slots, +it does not check the stack size; +it simply assumes that there are enough slots. + +Several functions in the auxiliary library are used to +check @N{C function} arguments. +Because the error message is formatted for arguments +(e.g., @St{bad argument #1}), +you should not use these functions for other stack values. + +Functions called @id{luaL_check*} +always raise an error if the check is not satisfied. + +@sect2{@title{Functions and Types} + +Here we list all functions and types from the auxiliary library +in alphabetical order. + + +@APIEntry{void luaL_addchar (luaL_Buffer *B, char c);| +@apii{?,?,m} + +Adds the byte @id{c} to the buffer @id{B} +@seeC{luaL_Buffer}. + +} + +@APIEntry{void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l);| +@apii{?,?,m} + +Adds the string pointed to by @id{s} with length @id{l} to +the buffer @id{B} +@seeC{luaL_Buffer}. +The string can contain @x{embedded zeros}. + +} + +@APIEntry{void luaL_addsize (luaL_Buffer *B, size_t n);| +@apii{?,?,-} + +Adds to the buffer @id{B} @seeC{luaL_Buffer} +a string of length @id{n} previously copied to the +buffer area @seeC{luaL_prepbuffer}. + +} + +@APIEntry{void luaL_addstring (luaL_Buffer *B, const char *s);| +@apii{?,?,m} + +Adds the zero-terminated string pointed to by @id{s} +to the buffer @id{B} +@seeC{luaL_Buffer}. + +} + +@APIEntry{void luaL_addvalue (luaL_Buffer *B);| +@apii{1,?,m} + +Adds the value at the top of the stack +to the buffer @id{B} +@seeC{luaL_Buffer}. +Pops the value. + +This is the only function on string buffers that can (and must) +be called with an extra element on the stack, +which is the value to be added to the buffer. + +} + +@APIEntry{ +void luaL_argcheck (lua_State *L, + int cond, + int arg, + const char *extramsg);| +@apii{0,0,v} + +Checks whether @id{cond} is true. +If it is not, raises an error with a standard message @seeF{luaL_argerror}. + +} + +@APIEntry{int luaL_argerror (lua_State *L, int arg, const char *extramsg);| +@apii{0,0,v} + +Raises an error reporting a problem with argument @id{arg} +of the @N{C function} that called it, +using a standard message +that includes @id{extramsg} as a comment: +@verbatim{ +bad argument #@rep{arg} to '@rep{funcname}' (@rep{extramsg}) +} +This function never returns. + +} + +@APIEntry{typedef struct luaL_Buffer luaL_Buffer;| + +Type for a @def{string buffer}. + +A string buffer allows @N{C code} to build Lua strings piecemeal. +Its pattern of use is as follows: +@itemize{ + +@item{First declare a variable @id{b} of type @Lid{luaL_Buffer}.} + +@item{Then initialize it with a call @T{luaL_buffinit(L, &b)}.} + +@item{ +Then add string pieces to the buffer calling any of +the @id{luaL_add*} functions. +} + +@item{ +Finish by calling @T{luaL_pushresult(&b)}. +This call leaves the final string on the top of the stack. +} + +} + +If you know beforehand the total size of the resulting string, +you can use the buffer like this: +@itemize{ + +@item{First declare a variable @id{b} of type @Lid{luaL_Buffer}.} + +@item{Then initialize it and preallocate a space of +size @id{sz} with a call @T{luaL_buffinitsize(L, &b, sz)}.} + +@item{Then produce the string into that space.} + +@item{ +Finish by calling @T{luaL_pushresultsize(&b, sz)}, +where @id{sz} is the total size of the resulting string +copied into that space. +} + +} + +During its normal operation, +a string buffer uses a variable number of stack slots. +So, while using a buffer, you cannot assume that you know where +the top of the stack is. +You can use the stack between successive calls to buffer operations +as long as that use is balanced; +that is, +when you call a buffer operation, +the stack is at the same level +it was immediately after the previous buffer operation. +(The only exception to this rule is @Lid{luaL_addvalue}.) +After calling @Lid{luaL_pushresult} the stack is back to its +level when the buffer was initialized, +plus the final string on its top. + +} + +@APIEntry{void luaL_buffinit (lua_State *L, luaL_Buffer *B);| +@apii{0,0,-} + +Initializes a buffer @id{B}. +This function does not allocate any space; +the buffer must be declared as a variable +@seeC{luaL_Buffer}. + +} + +@APIEntry{char *luaL_buffinitsize (lua_State *L, luaL_Buffer *B, size_t sz);| +@apii{?,?,m} + +Equivalent to the sequence +@Lid{luaL_buffinit}, @Lid{luaL_prepbuffsize}. + +} + +@APIEntry{int luaL_callmeta (lua_State *L, int obj, const char *e);| +@apii{0,0|1,e} + +Calls a metamethod. + +If the object at index @id{obj} has a metatable and this +metatable has a field @id{e}, +this function calls this field passing the object as its only argument. +In this case this function returns true and pushes onto the +stack the value returned by the call. +If there is no metatable or no metamethod, +this function returns false (without pushing any value on the stack). + +} + +@APIEntry{void luaL_checkany (lua_State *L, int arg);| +@apii{0,0,v} + +Checks whether the function has an argument +of any type (including @nil) at position @id{arg}. + +} + +@APIEntry{lua_Integer luaL_checkinteger (lua_State *L, int arg);| +@apii{0,0,v} + +Checks whether the function argument @id{arg} is an integer +(or can be converted to an integer) +and returns this integer cast to a @Lid{lua_Integer}. + +} + +@APIEntry{const char *luaL_checklstring (lua_State *L, int arg, size_t *l);| +@apii{0,0,v} + +Checks whether the function argument @id{arg} is a string +and returns this string; +if @id{l} is not @id{NULL} fills @T{*l} +with the string's length. + +This function uses @Lid{lua_tolstring} to get its result, +so all conversions and caveats of that function apply here. + +} + +@APIEntry{lua_Number luaL_checknumber (lua_State *L, int arg);| +@apii{0,0,v} + +Checks whether the function argument @id{arg} is a number +and returns this number. + +} + +@APIEntry{ +int luaL_checkoption (lua_State *L, + int arg, + const char *def, + const char *const lst[]);| +@apii{0,0,v} + +Checks whether the function argument @id{arg} is a string and +searches for this string in the array @id{lst} +(which must be NULL-terminated). +Returns the index in the array where the string was found. +Raises an error if the argument is not a string or +if the string cannot be found. + +If @id{def} is not @id{NULL}, +the function uses @id{def} as a default value when +there is no argument @id{arg} or when this argument is @nil. + +This is a useful function for mapping strings to @N{C enums}. +(The usual convention in Lua libraries is +to use strings instead of numbers to select options.) + +} + +@APIEntry{void luaL_checkstack (lua_State *L, int sz, const char *msg);| +@apii{0,0,v} + +Grows the stack size to @T{top + sz} elements, +raising an error if the stack cannot grow to that size. +@id{msg} is an additional text to go into the error message +(or @id{NULL} for no additional text). + +} + +@APIEntry{const char *luaL_checkstring (lua_State *L, int arg);| +@apii{0,0,v} + +Checks whether the function argument @id{arg} is a string +and returns this string. + +This function uses @Lid{lua_tolstring} to get its result, +so all conversions and caveats of that function apply here. + +} + +@APIEntry{void luaL_checktype (lua_State *L, int arg, int t);| +@apii{0,0,v} + +Checks whether the function argument @id{arg} has type @id{t}. +See @Lid{lua_type} for the encoding of types for @id{t}. + +} + +@APIEntry{void *luaL_checkudata (lua_State *L, int arg, const char *tname);| +@apii{0,0,v} + +Checks whether the function argument @id{arg} is a userdata +of the type @id{tname} @seeC{luaL_newmetatable} and +returns the userdata's memory-block address @seeC{lua_touserdata}. + +} + +@APIEntry{void luaL_checkversion (lua_State *L);| +@apii{0,0,v} + +Checks whether the code making the call and the Lua library being called +are using the same version of Lua and the same numeric types. + +} + +@APIEntry{int luaL_dofile (lua_State *L, const char *filename);| +@apii{0,?,m} + +Loads and runs the given file. +It is defined as the following macro: +@verbatim{ +(luaL_loadfile(L, filename) || lua_pcall(L, 0, LUA_MULTRET, 0)) +} +It returns false if there are no errors +or true in case of errors. + +} + +@APIEntry{int luaL_dostring (lua_State *L, const char *str);| +@apii{0,?,-} + +Loads and runs the given string. +It is defined as the following macro: +@verbatim{ +(luaL_loadstring(L, str) || lua_pcall(L, 0, LUA_MULTRET, 0)) +} +It returns false if there are no errors +or true in case of errors. + +} + +@APIEntry{int luaL_error (lua_State *L, const char *fmt, ...);| +@apii{0,0,v} + +Raises an error. +The error message format is given by @id{fmt} +plus any extra arguments, +following the same rules of @Lid{lua_pushfstring}. +It also adds at the beginning of the message the file name and +the line number where the error occurred, +if this information is available. + +This function never returns, +but it is an idiom to use it in @N{C functions} +as @T{return luaL_error(@rep{args})}. + +} + +@APIEntry{int luaL_execresult (lua_State *L, int stat);| +@apii{0,3,m} + +This function produces the return values for +process-related functions in the standard library +(@Lid{os.execute} and @Lid{io.close}). + +} + +@APIEntry{ +int luaL_fileresult (lua_State *L, int stat, const char *fname);| +@apii{0,1|3,m} + +This function produces the return values for +file-related functions in the standard library +(@Lid{io.open}, @Lid{os.rename}, @Lid{file:seek}, etc.). + +} + +@APIEntry{int luaL_getmetafield (lua_State *L, int obj, const char *e);| +@apii{0,0|1,m} + +Pushes onto the stack the field @id{e} from the metatable +of the object at index @id{obj} and returns the type of the pushed value. +If the object does not have a metatable, +or if the metatable does not have this field, +pushes nothing and returns @id{LUA_TNIL}. + +} + +@APIEntry{int luaL_getmetatable (lua_State *L, const char *tname);| +@apii{0,1,m} + +Pushes onto the stack the metatable associated with the name @id{tname} +in the registry @seeC{luaL_newmetatable}, +or @nil if there is no metatable associated with that name. +Returns the type of the pushed value. + +} + +@APIEntry{int luaL_getsubtable (lua_State *L, int idx, const char *fname);| +@apii{0,1,e} + +Ensures that the value @T{t[fname]}, +where @id{t} is the value at index @id{idx}, +is a table, +and pushes that table onto the stack. +Returns true if it finds a previous table there +and false if it creates a new table. + +} + +@APIEntry{ +const char *luaL_gsub (lua_State *L, + const char *s, + const char *p, + const char *r);| +@apii{0,1,m} + +Creates a copy of string @id{s} by replacing +any occurrence of the string @id{p} +with the string @id{r}. +Pushes the resulting string on the stack and returns it. + +} + +@APIEntry{lua_Integer luaL_len (lua_State *L, int index);| +@apii{0,0,e} + +Returns the @Q{length} of the value at the given index +as a number; +it is equivalent to the @Char{#} operator in Lua @see{len-op}. +Raises an error if the result of the operation is not an integer. +(This case only can happen through metamethods.) + +} + +@APIEntry{ +int luaL_loadbuffer (lua_State *L, + const char *buff, + size_t sz, + const char *name);| +@apii{0,1,-} + +Equivalent to @Lid{luaL_loadbufferx} with @id{mode} equal to @id{NULL}. + +} + + +@APIEntry{ +int luaL_loadbufferx (lua_State *L, + const char *buff, + size_t sz, + const char *name, + const char *mode);| +@apii{0,1,-} + +Loads a buffer as a Lua chunk. +This function uses @Lid{lua_load} to load the chunk in the +buffer pointed to by @id{buff} with size @id{sz}. + +This function returns the same results as @Lid{lua_load}. +@id{name} is the chunk name, +used for debug information and error messages. +The string @id{mode} works as in function @Lid{lua_load}. + +} + + +@APIEntry{int luaL_loadfile (lua_State *L, const char *filename);| +@apii{0,1,m} + +Equivalent to @Lid{luaL_loadfilex} with @id{mode} equal to @id{NULL}. + +} + +@APIEntry{int luaL_loadfilex (lua_State *L, const char *filename, + const char *mode);| +@apii{0,1,m} + +Loads a file as a Lua chunk. +This function uses @Lid{lua_load} to load the chunk in the file +named @id{filename}. +If @id{filename} is @id{NULL}, +then it loads from the standard input. +The first line in the file is ignored if it starts with a @T{#}. + +The string @id{mode} works as in function @Lid{lua_load}. + +This function returns the same results as @Lid{lua_load}, +but it has an extra error code @defid{LUA_ERRFILE} +for file-related errors +(e.g., it cannot open or read the file). + +As @Lid{lua_load}, this function only loads the chunk; +it does not run it. + +} + +@APIEntry{int luaL_loadstring (lua_State *L, const char *s);| +@apii{0,1,-} + +Loads a string as a Lua chunk. +This function uses @Lid{lua_load} to load the chunk in +the zero-terminated string @id{s}. + +This function returns the same results as @Lid{lua_load}. + +Also as @Lid{lua_load}, this function only loads the chunk; +it does not run it. + +} + + +@APIEntry{void luaL_newlib (lua_State *L, const luaL_Reg l[]);| +@apii{0,1,m} + +Creates a new table and registers there +the functions in list @id{l}. + +It is implemented as the following macro: +@verbatim{ +(luaL_newlibtable(L,l), luaL_setfuncs(L,l,0)) +} +The array @id{l} must be the actual array, +not a pointer to it. + +} + +@APIEntry{void luaL_newlibtable (lua_State *L, const luaL_Reg l[]);| +@apii{0,1,m} + +Creates a new table with a size optimized +to store all entries in the array @id{l} +(but does not actually store them). +It is intended to be used in conjunction with @Lid{luaL_setfuncs} +@seeF{luaL_newlib}. + +It is implemented as a macro. +The array @id{l} must be the actual array, +not a pointer to it. + +} + +@APIEntry{int luaL_newmetatable (lua_State *L, const char *tname);| +@apii{0,1,m} + +If the registry already has the key @id{tname}, +returns 0. +Otherwise, +creates a new table to be used as a metatable for userdata, +adds to this new table the pair @T{__name = tname}, +adds to the registry the pair @T{[tname] = new table}, +and returns 1. +(The entry @idx{__name} is used by some error-reporting functions.) + +In both cases pushes onto the stack the final value associated +with @id{tname} in the registry. + +} + +@APIEntry{lua_State *luaL_newstate (void);| +@apii{0,0,-} + +Creates a new Lua state. +It calls @Lid{lua_newstate} with an +allocator based on the @N{standard C} @id{realloc} function +and then sets a panic function @see{C-error} that prints +an error message to the standard error output in case of fatal +errors. + +Returns the new state, +or @id{NULL} if there is a @x{memory allocation error}. + +} + +@APIEntry{void luaL_openlibs (lua_State *L);| +@apii{0,0,e} + +Opens all standard Lua libraries into the given state. + +} + +@APIEntry{ +T luaL_opt (L, func, arg, dflt);| +@apii{0,0,-} + +This macro is defined as follows: +@verbatim{ +(lua_isnoneornil(L,(arg)) ? (dflt) : func(L,(arg))) +} +In words, if the argument @id{arg} is nil or absent, +the macro results in the default @id{dflt}. +Otherwise, it results in the result of calling @id{func} +with the state @id{L} and the argument index @id{arg} as +parameters. +Note that it evaluates the expression @id{dflt} only if needed. + +} + +@APIEntry{ +lua_Integer luaL_optinteger (lua_State *L, + int arg, + lua_Integer d);| +@apii{0,0,v} + +If the function argument @id{arg} is an integer +(or convertible to an integer), +returns this integer. +If this argument is absent or is @nil, +returns @id{d}. +Otherwise, raises an error. + +} + +@APIEntry{ +const char *luaL_optlstring (lua_State *L, + int arg, + const char *d, + size_t *l);| +@apii{0,0,v} + +If the function argument @id{arg} is a string, +returns this string. +If this argument is absent or is @nil, +returns @id{d}. +Otherwise, raises an error. + +If @id{l} is not @id{NULL}, +fills the position @T{*l} with the result's length. +If the result is @id{NULL} +(only possible when returning @id{d} and @T{d == NULL}), +its length is considered zero. + +This function uses @Lid{lua_tolstring} to get its result, +so all conversions and caveats of that function apply here. + +} + +@APIEntry{lua_Number luaL_optnumber (lua_State *L, int arg, lua_Number d);| +@apii{0,0,v} + +If the function argument @id{arg} is a number, +returns this number. +If this argument is absent or is @nil, +returns @id{d}. +Otherwise, raises an error. + +} + +@APIEntry{ +const char *luaL_optstring (lua_State *L, + int arg, + const char *d);| +@apii{0,0,v} + +If the function argument @id{arg} is a string, +returns this string. +If this argument is absent or is @nil, +returns @id{d}. +Otherwise, raises an error. + +} + +@APIEntry{char *luaL_prepbuffer (luaL_Buffer *B);| +@apii{?,?,m} + +Equivalent to @Lid{luaL_prepbuffsize} +with the predefined size @defid{LUAL_BUFFERSIZE}. + +} + +@APIEntry{char *luaL_prepbuffsize (luaL_Buffer *B, size_t sz);| +@apii{?,?,m} + +Returns an address to a space of size @id{sz} +where you can copy a string to be added to buffer @id{B} +@seeC{luaL_Buffer}. +After copying the string into this space you must call +@Lid{luaL_addsize} with the size of the string to actually add +it to the buffer. + +} + +@APIEntry{void luaL_pushresult (luaL_Buffer *B);| +@apii{?,1,m} + +Finishes the use of buffer @id{B} leaving the final string on +the top of the stack. + +} + +@APIEntry{void luaL_pushresultsize (luaL_Buffer *B, size_t sz);| +@apii{?,1,m} + +Equivalent to the sequence @Lid{luaL_addsize}, @Lid{luaL_pushresult}. + +} + +@APIEntry{int luaL_ref (lua_State *L, int t);| +@apii{1,0,m} + +Creates and returns a @def{reference}, +in the table at index @id{t}, +for the object at the top of the stack (and pops the object). + +A reference is a unique integer key. +As long as you do not manually add integer keys into table @id{t}, +@Lid{luaL_ref} ensures the uniqueness of the key it returns. +You can retrieve an object referred by reference @id{r} +by calling @T{lua_rawgeti(L, t, r)}. +Function @Lid{luaL_unref} frees a reference and its associated object. + +If the object at the top of the stack is @nil, +@Lid{luaL_ref} returns the constant @defid{LUA_REFNIL}. +The constant @defid{LUA_NOREF} is guaranteed to be different +from any reference returned by @Lid{luaL_ref}. + +} + +@APIEntry{ +typedef struct luaL_Reg { + const char *name; + lua_CFunction func; +} luaL_Reg; +| + +Type for arrays of functions to be registered by +@Lid{luaL_setfuncs}. +@id{name} is the function name and @id{func} is a pointer to +the function. +Any array of @Lid{luaL_Reg} must end with a sentinel entry +in which both @id{name} and @id{func} are @id{NULL}. + +} + +@APIEntry{ +void luaL_requiref (lua_State *L, const char *modname, + lua_CFunction openf, int glb);| +@apii{0,1,e} + +If @T{package.loaded[modname]} is not true, +calls function @id{openf} with string @id{modname} as an argument +and sets the call result to @T{package.loaded[modname]}, +as if that function has been called through @Lid{require}. + +If @id{glb} is true, +also stores the module into global @id{modname}. + +Leaves a copy of the module on the stack. + +} + +@APIEntry{void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup);| +@apii{nup,0,m} + +Registers all functions in the array @id{l} +@seeC{luaL_Reg} into the table on the top of the stack +(below optional upvalues, see next). + +When @id{nup} is not zero, +all functions are created with @id{nup} upvalues, +initialized with copies of the @id{nup} values +previously pushed on the stack +on top of the library table. +These values are popped from the stack after the registration. + +} + +@APIEntry{void luaL_setmetatable (lua_State *L, const char *tname);| +@apii{0,0,-} + +Sets the metatable of the object at the top of the stack +as the metatable associated with name @id{tname} +in the registry @seeC{luaL_newmetatable}. + +} + +@APIEntry{ +typedef struct luaL_Stream { + FILE *f; + lua_CFunction closef; +} luaL_Stream; +| + +The standard representation for @x{file handles}, +which is used by the standard I/O library. + +A file handle is implemented as a full userdata, +with a metatable called @id{LUA_FILEHANDLE} +(where @id{LUA_FILEHANDLE} is a macro with the actual metatable's name). +The metatable is created by the I/O library +@seeF{luaL_newmetatable}. + +This userdata must start with the structure @id{luaL_Stream}; +it can contain other data after this initial structure. +Field @id{f} points to the corresponding C stream +(or it can be @id{NULL} to indicate an incompletely created handle). +Field @id{closef} points to a Lua function +that will be called to close the stream +when the handle is closed or collected; +this function receives the file handle as its sole argument and +must return either @true (in case of success) +or @nil plus an error message (in case of error). +Once Lua calls this field, +it changes the field value to @id{NULL} +to signal that the handle is closed. + +} + +@APIEntry{void *luaL_testudata (lua_State *L, int arg, const char *tname);| +@apii{0,0,m} + +This function works like @Lid{luaL_checkudata}, +except that, when the test fails, +it returns @id{NULL} instead of raising an error. + +} + +@APIEntry{const char *luaL_tolstring (lua_State *L, int idx, size_t *len);| +@apii{0,1,e} + +Converts any Lua value at the given index to a @N{C string} +in a reasonable format. +The resulting string is pushed onto the stack and also +returned by the function. +If @id{len} is not @id{NULL}, +the function also sets @T{*len} with the string length. + +If the value has a metatable with a @idx{__tostring} field, +then @id{luaL_tolstring} calls the corresponding metamethod +with the value as argument, +and uses the result of the call as its result. + +} + +@APIEntry{ +void luaL_traceback (lua_State *L, lua_State *L1, const char *msg, + int level);| +@apii{0,1,m} + +Creates and pushes a traceback of the stack @id{L1}. +If @id{msg} is not @id{NULL} it is appended +at the beginning of the traceback. +The @id{level} parameter tells at which level +to start the traceback. + +} + +@APIEntry{const char *luaL_typename (lua_State *L, int index);| +@apii{0,0,-} + +Returns the name of the type of the value at the given index. + +} + +@APIEntry{void luaL_unref (lua_State *L, int t, int ref);| +@apii{0,0,-} + +Releases reference @id{ref} from the table at index @id{t} +@seeC{luaL_ref}. +The entry is removed from the table, +so that the referred object can be collected. +The reference @id{ref} is also freed to be used again. + +If @id{ref} is @Lid{LUA_NOREF} or @Lid{LUA_REFNIL}, +@Lid{luaL_unref} does nothing. + +} + +@APIEntry{void luaL_where (lua_State *L, int lvl);| +@apii{0,1,m} + +Pushes onto the stack a string identifying the current position +of the control at level @id{lvl} in the call stack. +Typically this string has the following format: +@verbatim{ +@rep{chunkname}:@rep{currentline}: +} +@N{Level 0} is the running function, +@N{level 1} is the function that called the running function, +etc. + +This function is used to build a prefix for error messages. + +} + +} + +} + + +@C{-------------------------------------------------------------------------} +@sect1{libraries| @title{Standard Libraries} + +The standard Lua libraries provide useful functions +that are implemented directly through the @N{C API}. +Some of these functions provide essential services to the language +(e.g., @Lid{type} and @Lid{getmetatable}); +others provide access to @Q{outside} services (e.g., I/O); +and others could be implemented in Lua itself, +but are quite useful or have critical performance requirements that +deserve an implementation in C (e.g., @Lid{table.sort}). + +All libraries are implemented through the official @N{C API} +and are provided as separate @N{C modules}. +Unless otherwise noted, +these library functions do not adjust its number of arguments +to its expected parameters. +For instance, a function documented as @T{foo(arg)} +should not be called without an argument. + +Currently, Lua has the following standard libraries: +@itemize{ + +@item{@link{predefined|basic library};} + +@item{@link{corolib|coroutine library};} + +@item{@link{packlib|package library};} + +@item{@link{strlib|string manipulation};} + +@item{@link{utf8|basic UTF-8 support};} + +@item{@link{tablib|table manipulation};} + +@item{@link{mathlib|mathematical functions} (sin, log, etc.);} + +@item{@link{iolib|input and output};} + +@item{@link{oslib|operating system facilities};} + +@item{@link{debuglib|debug facilities}.} + +} +Except for the basic and the package libraries, +each library provides all its functions as fields of a global table +or as methods of its objects. + +To have access to these libraries, +the @N{C host} program should call the @Lid{luaL_openlibs} function, +which opens all standard libraries. +Alternatively, +the host program can open them individually by using +@Lid{luaL_requiref} to call +@defid{luaopen_base} (for the basic library), +@defid{luaopen_package} (for the package library), +@defid{luaopen_coroutine} (for the coroutine library), +@defid{luaopen_string} (for the string library), +@defid{luaopen_utf8} (for the UTF8 library), +@defid{luaopen_table} (for the table library), +@defid{luaopen_math} (for the mathematical library), +@defid{luaopen_io} (for the I/O library), +@defid{luaopen_os} (for the operating system library), +and @defid{luaopen_debug} (for the debug library). +These functions are declared in @defid{lualib.h}. + +@sect2{predefined| @title{Basic Functions} + +The basic library provides core functions to Lua. +If you do not include this library in your application, +you should check carefully whether you need to provide +implementations for some of its facilities. + + +@LibEntry{assert (v [, message])| + +Calls @Lid{error} if +the value of its argument @id{v} is false (i.e., @nil or @false); +otherwise, returns all its arguments. +In case of error, +@id{message} is the error object; +when absent, it defaults to @St{assertion failed!} + +} + +@LibEntry{collectgarbage ([opt [, arg]])| + +This function is a generic interface to the garbage collector. +It performs different functions according to its first argument, @id{opt}: +@description{ + +@item{@St{collect}| +performs a full garbage-collection cycle. +This is the default option. +} + +@item{@St{stop}| +stops automatic execution of the garbage collector. +The collector will run only when explicitly invoked, +until a call to restart it. +} + +@item{@St{restart}| +restarts automatic execution of the garbage collector. +} + +@item{@St{count}| +returns the total memory in use by Lua in Kbytes. +The value has a fractional part, +so that it multiplied by 1024 +gives the exact number of bytes in use by Lua +(except for overflows). +} + +@item{@St{step}| +performs a garbage-collection step. +The step @Q{size} is controlled by @id{arg}. +With a zero value, +the collector will perform one basic (indivisible) step. +For non-zero values, +the collector will perform as if that amount of memory +(in KBytes) had been allocated by Lua. +Returns @true if the step finished a collection cycle. +} + +@item{@St{setpause}| +sets @id{arg} as the new value for the @emph{pause} of +the collector @see{GC}. +Returns the previous value for @emph{pause}. +} + +@item{@St{incremental}| +Change the collector mode to incremental. +This option can be followed by three numbers: +the garbage-collector pause, +the step multiplier, +and the step size. +} + +@item{@St{generational}| +Change the collector mode to generational. +This option can be followed by two numbers: +the garbage-collector minor multiplier +and the major multiplier. +} + +@item{@St{isrunning}| +returns a boolean that tells whether the collector is running +(i.e., not stopped). +} + +} + +} + +@LibEntry{dofile ([filename])| +Opens the named file and executes its contents as a Lua chunk. +When called without arguments, +@id{dofile} executes the contents of the standard input (@id{stdin}). +Returns all values returned by the chunk. +In case of errors, @id{dofile} propagates the error +to its caller (that is, @id{dofile} does not run in protected mode). + +} + +@LibEntry{error (message [, level])| +Terminates the last protected function called +and returns @id{message} as the error object. +Function @id{error} never returns. + +Usually, @id{error} adds some information about the error position +at the beginning of the message, if the message is a string. +The @id{level} argument specifies how to get the error position. +With @N{level 1} (the default), the error position is where the +@id{error} function was called. +@N{Level 2} points the error to where the function +that called @id{error} was called; and so on. +Passing a @N{level 0} avoids the addition of error position information +to the message. + +} + +@LibEntry{_G| +A global variable (not a function) that +holds the @x{global environment} @see{globalenv}. +Lua itself does not use this variable; +changing its value does not affect any environment, +nor vice versa. + +} + +@LibEntry{getmetatable (object)| + +If @id{object} does not have a metatable, returns @nil. +Otherwise, +if the object's metatable has a @idx{__metatable} field, +returns the associated value. +Otherwise, returns the metatable of the given object. + +} + +@LibEntry{ipairs (t)| + +Returns three values (an iterator function, the table @id{t}, and 0) +so that the construction +@verbatim{ +for i,v in ipairs(t) do @rep{body} end +} +will iterate over the key@En{}value pairs +(@T{1,t[1]}), (@T{2,t[2]}), @ldots, +up to the first absent index. + +} + +@LibEntry{load (chunk [, chunkname [, mode [, env]]])| + +Loads a chunk. + +If @id{chunk} is a string, the chunk is this string. +If @id{chunk} is a function, +@id{load} calls it repeatedly to get the chunk pieces. +Each call to @id{chunk} must return a string that concatenates +with previous results. +A return of an empty string, @nil, or no value signals the end of the chunk. + +If there are no syntactic errors, +returns the compiled chunk as a function; +otherwise, returns @nil plus the error message. + +If the resulting function has upvalues, +the first upvalue is set to the value of @id{env}, +if that parameter is given, +or to the value of the @x{global environment}. +Other upvalues are initialized with @nil. +(When you load a main chunk, +the resulting function will always have exactly one upvalue, +the @id{_ENV} variable @see{globalenv}. +However, +when you load a binary chunk created from a function @seeF{string.dump}, +the resulting function can have an arbitrary number of upvalues.) +All upvalues are fresh, that is, +they are not shared with any other function. + +@id{chunkname} is used as the name of the chunk for error messages +and debug information @see{debugI}. +When absent, +it defaults to @id{chunk}, if @id{chunk} is a string, +or to @St{=(load)} otherwise. + +The string @id{mode} controls whether the chunk can be text or binary +(that is, a precompiled chunk). +It may be the string @St{b} (only @x{binary chunk}s), +@St{t} (only text chunks), +or @St{bt} (both binary and text). +The default is @St{bt}. + +Lua does not check the consistency of binary chunks. +Maliciously crafted binary chunks can crash +the interpreter. + +} + +@LibEntry{loadfile ([filename [, mode [, env]]])| + +Similar to @Lid{load}, +but gets the chunk from file @id{filename} +or from the standard input, +if no file name is given. + +} + +@LibEntry{next (table [, index])| + +Allows a program to traverse all fields of a table. +Its first argument is a table and its second argument +is an index in this table. +@id{next} returns the next index of the table +and its associated value. +When called with @nil as its second argument, +@id{next} returns an initial index +and its associated value. +When called with the last index, +or with @nil in an empty table, +@id{next} returns @nil. +If the second argument is absent, then it is interpreted as @nil. +In particular, +you can use @T{next(t)} to check whether a table is empty. + +The order in which the indices are enumerated is not specified, +@emph{even for numeric indices}. +(To traverse a table in numerical order, +use a numerical @Rw{for}.) + +The behavior of @id{next} is undefined if, +during the traversal, +you assign any value to a non-existent field in the table. +You may however modify existing fields. +In particular, you may set existing fields to nil. + +} + +@LibEntry{pairs (t)| + +If @id{t} has a metamethod @idx{__pairs}, +calls it with @id{t} as argument and returns the first three +results from the call. + +Otherwise, +returns three values: the @Lid{next} function, the table @id{t}, and @nil, +so that the construction +@verbatim{ +for k,v in pairs(t) do @rep{body} end +} +will iterate over all key@En{}value pairs of table @id{t}. + +See function @Lid{next} for the caveats of modifying +the table during its traversal. + +} + +@LibEntry{pcall (f [, arg1, @Cdots])| + +Calls function @id{f} with +the given arguments in @def{protected mode}. +This means that any error @N{inside @T{f}} is not propagated; +instead, @id{pcall} catches the error +and returns a status code. +Its first result is the status code (a boolean), +which is true if the call succeeds without errors. +In such case, @id{pcall} also returns all results from the call, +after this first result. +In case of any error, @id{pcall} returns @false plus the error message. + +} + +@LibEntry{print (@Cdots)| +Receives any number of arguments +and prints their values to @id{stdout}, +using the @Lid{tostring} function to convert each argument to a string. +@id{print} is not intended for formatted output, +but only as a quick way to show a value, +for instance for debugging. +For complete control over the output, +use @Lid{string.format} and @Lid{io.write}. + +} + +@LibEntry{rawequal (v1, v2)| +Checks whether @id{v1} is equal to @id{v2}, +without invoking the @idx{__eq} metamethod. +Returns a boolean. + +} + +@LibEntry{rawget (table, index)| +Gets the real value of @T{table[index]}, +without invoking the @idx{__index} metamethod. +@id{table} must be a table; +@id{index} may be any value. + +} + +@LibEntry{rawlen (v)| +Returns the length of the object @id{v}, +which must be a table or a string, +without invoking the @idx{__len} metamethod. +Returns an integer. + +} + +@LibEntry{rawset (table, index, value)| +Sets the real value of @T{table[index]} to @id{value}, +without invoking the @idx{__newindex} metamethod. +@id{table} must be a table, +@id{index} any value different from @nil and @x{NaN}, +and @id{value} any Lua value. + +This function returns @id{table}. + +} + +@LibEntry{select (index, @Cdots)| + +If @id{index} is a number, +returns all arguments after argument number @id{index}; +a negative number indexes from the end (@num{-1} is the last argument). +Otherwise, @id{index} must be the string @T{"#"}, +and @id{select} returns the total number of extra arguments it received. + +} + +@LibEntry{setmetatable (table, metatable)| + +Sets the metatable for the given table. +(To change the metatable of other types from Lua code, +you must use the @link{debuglib|debug library}.) +If @id{metatable} is @nil, +removes the metatable of the given table. +If the original metatable has a @idx{__metatable} field, +raises an error. + +This function returns @id{table}. + +} + +@LibEntry{tonumber (e [, base])| + +When called with no @id{base}, +@id{tonumber} tries to convert its argument to a number. +If the argument is already a number or +a string convertible to a number, +then @id{tonumber} returns this number; +otherwise, it returns @nil. + +The conversion of strings can result in integers or floats, +according to the lexical conventions of Lua @see{lexical}. +(The string may have leading and trailing spaces and a sign.) + +When called with @id{base}, +then @id{e} must be a string to be interpreted as +an integer numeral in that base. +The base may be any integer between 2 and 36, inclusive. +In bases @N{above 10}, the letter @Char{A} (in either upper or lower case) +@N{represents 10}, @Char{B} @N{represents 11}, and so forth, +with @Char{Z} representing 35. +If the string @id{e} is not a valid numeral in the given base, +the function returns @nil. + +} + +@LibEntry{tostring (v)| +Receives a value of any type and +converts it to a string in a human-readable format. +(For complete control of how numbers are converted, +use @Lid{string.format}.) + +If the metatable of @id{v} has a @idx{__tostring} field, +then @id{tostring} calls the corresponding value +with @id{v} as argument, +and uses the result of the call as its result. + +} + +@LibEntry{type (v)| +Returns the type of its only argument, coded as a string. +The possible results of this function are +@St{nil} (a string, not the value @nil), +@St{number}, +@St{string}, +@St{boolean}, +@St{table}, +@St{function}, +@St{thread}, +and @St{userdata}. + +} + +@LibEntry{_VERSION| + +A global variable (not a function) that +holds a string containing the running Lua version. +The current value of this variable is @St{Lua 5.4}. + +} + +@LibEntry{xpcall (f, msgh [, arg1, @Cdots])| + +This function is similar to @Lid{pcall}, +except that it sets a new @x{message handler} @id{msgh}. + +} + +} + +@sect2{corolib| @title{Coroutine Manipulation} + +This library comprises the operations to manipulate coroutines, +which come inside the table @defid{coroutine}. +See @See{coroutine} for a general description of coroutines. + + +@LibEntry{coroutine.create (f)| + +Creates a new coroutine, with body @id{f}. +@id{f} must be a function. +Returns this new coroutine, +an object with type @T{"thread"}. + +} + +@LibEntry{coroutine.isyieldable ()| + +Returns true when the running coroutine can yield. + +A running coroutine is yieldable if it is not the main thread and +it is not inside a non-yieldable @N{C function}. + +} + +@LibEntry{coroutine.resume (co [, val1, @Cdots])| + +Starts or continues the execution of coroutine @id{co}. +The first time you resume a coroutine, +it starts running its body. +The values @id{val1}, @ldots are passed +as the arguments to the body function. +If the coroutine has yielded, +@id{resume} restarts it; +the values @id{val1}, @ldots are passed +as the results from the yield. + +If the coroutine runs without any errors, +@id{resume} returns @true plus any values passed to @id{yield} +(when the coroutine yields) or any values returned by the body function +(when the coroutine terminates). +If there is any error, +@id{resume} returns @false plus the error message. + +} + +@LibEntry{coroutine.running ()| + +Returns the running coroutine plus a boolean, +true when the running coroutine is the main one. + +} + +@LibEntry{coroutine.status (co)| + +Returns the status of coroutine @id{co}, as a string: +@T{"running"}, +if the coroutine is running (that is, it called @id{status}); +@T{"suspended"}, if the coroutine is suspended in a call to @id{yield}, +or if it has not started running yet; +@T{"normal"} if the coroutine is active but not running +(that is, it has resumed another coroutine); +and @T{"dead"} if the coroutine has finished its body function, +or if it has stopped with an error. + +} + +@LibEntry{coroutine.wrap (f)| + +Creates a new coroutine, with body @id{f}. +@id{f} must be a function. +Returns a function that resumes the coroutine each time it is called. +Any arguments passed to the function behave as the +extra arguments to @id{resume}. +Returns the same values returned by @id{resume}, +except the first boolean. +In case of error, propagates the error. + +} + +@LibEntry{coroutine.yield (@Cdots)| + +Suspends the execution of the calling coroutine. +Any arguments to @id{yield} are passed as extra results to @id{resume}. + +} + +} + +@sect2{packlib| @title{Modules} + +The package library provides basic +facilities for loading modules in Lua. +It exports one function directly in the global environment: +@Lid{require}. +Everything else is exported in a table @defid{package}. + + +@LibEntry{require (modname)| + +Loads the given module. +The function starts by looking into the @Lid{package.loaded} table +to determine whether @id{modname} is already loaded. +If it is, then @id{require} returns the value stored +at @T{package.loaded[modname]}. +Otherwise, it tries to find a @emph{loader} for the module. + +To find a loader, +@id{require} is guided by the @Lid{package.searchers} sequence. +By changing this sequence, +we can change how @id{require} looks for a module. +The following explanation is based on the default configuration +for @Lid{package.searchers}. + +First @id{require} queries @T{package.preload[modname]}. +If it has a value, +this value (which must be a function) is the loader. +Otherwise @id{require} searches for a Lua loader using the +path stored in @Lid{package.path}. +If that also fails, it searches for a @N{C loader} using the +path stored in @Lid{package.cpath}. +If that also fails, +it tries an @emph{all-in-one} loader @seeF{package.searchers}. + +Once a loader is found, +@id{require} calls the loader with two arguments: +@id{modname} and an extra value dependent on how it got the loader. +(If the loader came from a file, +this extra value is the file name.) +If the loader returns any non-nil value, +@id{require} assigns the returned value to @T{package.loaded[modname]}. +If the loader does not return a non-nil value and +has not assigned any value to @T{package.loaded[modname]}, +then @id{require} assigns @Rw{true} to this entry. +In any case, @id{require} returns the +final value of @T{package.loaded[modname]}. + +If there is any error loading or running the module, +or if it cannot find any loader for the module, +then @id{require} raises an error. + +} + +@LibEntry{package.config| + +A string describing some compile-time configurations for packages. +This string is a sequence of lines: +@itemize{ + +@item{The first line is the @x{directory separator} string. +Default is @Char{\} for @x{Windows} and @Char{/} for all other systems.} + +@item{The second line is the character that separates templates in a path. +Default is @Char{;}.} + +@item{The third line is the string that marks the +substitution points in a template. +Default is @Char{?}.} + +@item{The fourth line is a string that, in a path in @x{Windows}, +is replaced by the executable's directory. +Default is @Char{!}.} + +@item{The fifth line is a mark to ignore all text after it +when building the @id{luaopen_} function name. +Default is @Char{-}.} + +} + +} + +@LibEntry{package.cpath| + +The path used by @Lid{require} to search for a @N{C loader}. + +Lua initializes the @N{C path} @Lid{package.cpath} in the same way +it initializes the Lua path @Lid{package.path}, +using the environment variable @defid{LUA_CPATH_5_4}, +or the environment variable @defid{LUA_CPATH}, +or a default path defined in @id{luaconf.h}. + +} + +@LibEntry{package.loaded| + +A table used by @Lid{require} to control which +modules are already loaded. +When you require a module @id{modname} and +@T{package.loaded[modname]} is not false, +@Lid{require} simply returns the value stored there. + +This variable is only a reference to the real table; +assignments to this variable do not change the +table used by @Lid{require}. + +} + +@LibEntry{package.loadlib (libname, funcname)| + +Dynamically links the host program with the @N{C library} @id{libname}. + +If @id{funcname} is @St{*}, +then it only links with the library, +making the symbols exported by the library +available to other dynamically linked libraries. +Otherwise, +it looks for a function @id{funcname} inside the library +and returns this function as a @N{C function}. +So, @id{funcname} must follow the @Lid{lua_CFunction} prototype +@seeC{lua_CFunction}. + +This is a low-level function. +It completely bypasses the package and module system. +Unlike @Lid{require}, +it does not perform any path searching and +does not automatically adds extensions. +@id{libname} must be the complete file name of the @N{C library}, +including if necessary a path and an extension. +@id{funcname} must be the exact name exported by the @N{C library} +(which may depend on the @N{C compiler} and linker used). + +This function is not supported by @N{Standard C}. +As such, it is only available on some platforms +(Windows, Linux, Mac OS X, Solaris, BSD, +plus other Unix systems that support the @id{dlfcn} standard). + +} + +@LibEntry{package.path| + +The path used by @Lid{require} to search for a Lua loader. + +At start-up, Lua initializes this variable with +the value of the environment variable @defid{LUA_PATH_5_4} or +the environment variable @defid{LUA_PATH} or +with a default path defined in @id{luaconf.h}, +if those environment variables are not defined. +Any @St{;;} in the value of the environment variable +is replaced by the default path. + +} + +@LibEntry{package.preload| + +A table to store loaders for specific modules +@seeF{require}. + +This variable is only a reference to the real table; +assignments to this variable do not change the +table used by @Lid{require}. + +} + +@LibEntry{package.searchers| + +A table used by @Lid{require} to control how to load modules. + +Each entry in this table is a @def{searcher function}. +When looking for a module, +@Lid{require} calls each of these searchers in ascending order, +with the module name (the argument given to @Lid{require}) as its +sole parameter. +The function can return another function (the module @def{loader}) +plus an extra value that will be passed to that loader, +or a string explaining why it did not find that module +(or @nil if it has nothing to say). + +Lua initializes this table with four searcher functions. + +The first searcher simply looks for a loader in the +@Lid{package.preload} table. + +The second searcher looks for a loader as a Lua library, +using the path stored at @Lid{package.path}. +The search is done as described in function @Lid{package.searchpath}. + +The third searcher looks for a loader as a @N{C library}, +using the path given by the variable @Lid{package.cpath}. +Again, +the search is done as described in function @Lid{package.searchpath}. +For instance, +if the @N{C path} is the string +@verbatim{ +"./?.so;./?.dll;/usr/local/?/init.so" +} +the searcher for module @id{foo} +will try to open the files @T{./foo.so}, @T{./foo.dll}, +and @T{/usr/local/foo/init.so}, in that order. +Once it finds a @N{C library}, +this searcher first uses a dynamic link facility to link the +application with the library. +Then it tries to find a @N{C function} inside the library to +be used as the loader. +The name of this @N{C function} is the string @St{luaopen_} +concatenated with a copy of the module name where each dot +is replaced by an underscore. +Moreover, if the module name has a hyphen, +its suffix after (and including) the first hyphen is removed. +For instance, if the module name is @id{a.b.c-v2.1}, +the function name will be @id{luaopen_a_b_c}. + +The fourth searcher tries an @def{all-in-one loader}. +It searches the @N{C path} for a library for +the root name of the given module. +For instance, when requiring @id{a.b.c}, +it will search for a @N{C library} for @id{a}. +If found, it looks into it for an open function for +the submodule; +in our example, that would be @id{luaopen_a_b_c}. +With this facility, a package can pack several @N{C submodules} +into one single library, +with each submodule keeping its original open function. + +All searchers except the first one (preload) return as the extra value +the file name where the module was found, +as returned by @Lid{package.searchpath}. +The first searcher returns no extra value. + +} + +@LibEntry{package.searchpath (name, path [, sep [, rep]])| + +Searches for the given @id{name} in the given @id{path}. + +A path is a string containing a sequence of +@emph{templates} separated by semicolons. +For each template, +the function replaces each interrogation mark (if any) +in the template with a copy of @id{name} +wherein all occurrences of @id{sep} +(a dot, by default) +were replaced by @id{rep} +(the system's directory separator, by default), +and then tries to open the resulting file name. + +For instance, if the path is the string +@verbatim{ +"./?.lua;./?.lc;/usr/local/?/init.lua" +} +the search for the name @id{foo.a} +will try to open the files +@T{./foo/a.lua}, @T{./foo/a.lc}, and +@T{/usr/local/foo/a/init.lua}, in that order. + +Returns the resulting name of the first file that it can +open in read mode (after closing the file), +or @nil plus an error message if none succeeds. +(This error message lists all file names it tried to open.) + +} + +} + +@sect2{strlib| @title{String Manipulation} + +This library provides generic functions for string manipulation, +such as finding and extracting substrings, and pattern matching. +When indexing a string in Lua, the first character is at @N{position 1} +(not @N{at 0}, as in C). +Indices are allowed to be negative and are interpreted as indexing backwards, +from the end of the string. +Thus, the last character is at position @num{-1}, and so on. + +The string library provides all its functions inside the table +@defid{string}. +It also sets a @x{metatable for strings} +where the @idx{__index} field points to the @id{string} table. +Therefore, you can use the string functions in object-oriented style. +For instance, @T{string.byte(s,i)} +can be written as @T{s:byte(i)}. + +The string library assumes one-byte character encodings. + + +@LibEntry{string.byte (s [, i [, j]])| +Returns the internal numeric codes of the characters @T{s[i]}, +@T{s[i+1]}, @ldots, @T{s[j]}. +The default value for @id{i} @N{is 1}; +the default value for @id{j} @N{is @id{i}}. +These indices are corrected +following the same rules of function @Lid{string.sub}. + +Numeric codes are not necessarily portable across platforms. + +} + +@LibEntry{string.char (@Cdots)| +Receives zero or more integers. +Returns a string with length equal to the number of arguments, +in which each character has the internal numeric code equal +to its corresponding argument. + +Numeric codes are not necessarily portable across platforms. + +} + +@LibEntry{string.dump (function [, strip])| + +Returns a string containing a binary representation +(a @emph{binary chunk}) +of the given function, +so that a later @Lid{load} on this string returns +a copy of the function (but with new upvalues). +If @id{strip} is a true value, +the binary representation may not include all debug information +about the function, +to save space. + +Functions with upvalues have only their number of upvalues saved. +When (re)loaded, +those upvalues receive fresh instances containing @nil. +(You can use the debug library to serialize +and reload the upvalues of a function +in a way adequate to your needs.) + +} + +@LibEntry{string.find (s, pattern [, init [, plain]])| + +Looks for the first match of +@id{pattern} @see{pm} in the string @id{s}. +If it finds a match, then @id{find} returns the indices @N{of @T{s}} +where this occurrence starts and ends; +otherwise, it returns @nil. +A third, optional numeric argument @id{init} specifies +where to start the search; +its default value @N{is 1} and can be negative. +A value of @true as a fourth, optional argument @id{plain} +turns off the pattern matching facilities, +so the function does a plain @Q{find substring} operation, +with no characters in @id{pattern} being considered magic. +Note that if @id{plain} is given, then @id{init} must be given as well. + +If the pattern has captures, +then in a successful match +the captured values are also returned, +after the two indices. + +} + +@LibEntry{string.format (formatstring, @Cdots)| + +Returns a formatted version of its variable number of arguments +following the description given in its first argument (which must be a string). +The format string follows the same rules as the @ANSI{sprintf}. +The only differences are that the options/modifiers +@T{*}, @id{h}, @id{L}, @id{l}, @id{n}, +and @id{p} are not supported +and that there is an extra option, @id{q}. + +The @id{q} option formats booleans, nil, numbers, and strings +in a way that the result is a valid constant in Lua source code. +Booleans and nil are written in the obvious way +(@id{true}, @id{false}, @id{nil}). +Floats are written in hexadecimal, +to preserve full precision. +A string is written between double quotes, +using escape sequences when necessary to ensure that +it can safely be read back by the Lua interpreter. +For instance, the call +@verbatim{ +string.format('%q', 'a string with "quotes" and \n new line') +} +may produce the string: +@verbatim{ +"a string with \"quotes\" and \ + new line" +} + +Options +@id{A}, @id{a}, @id{E}, @id{e}, @id{f}, +@id{G}, and @id{g} all expect a number as argument. +Options @id{c}, @id{d}, +@id{i}, @id{o}, @id{u}, @id{X}, and @id{x} +expect an integer. +When Lua is compiled with a C89 compiler, +options @id{A} and @id{a} (hexadecimal floats) +do not support any modifier (flags, width, length). + +Option @id{s} expects a string; +if its argument is not a string, +it is converted to one following the same rules of @Lid{tostring}. +If the option has any modifier (flags, width, length), +the string argument should not contain @x{embedded zeros}. + +} + +@LibEntry{string.gmatch (s, pattern)| +Returns an iterator function that, +each time it is called, +returns the next captures from @id{pattern} @see{pm} +over the string @id{s}. +If @id{pattern} specifies no captures, +then the whole match is produced in each call. + +As an example, the following loop +will iterate over all the words from string @id{s}, +printing one per line: +@verbatim{ +s = "hello world from Lua" +for w in string.gmatch(s, "%a+") do + print(w) +end +} +The next example collects all pairs @T{key=value} from the +given string into a table: +@verbatim{ +t = {} +s = "from=world, to=Lua" +for k, v in string.gmatch(s, "(%w+)=(%w+)") do + t[k] = v +end +} + +For this function, a caret @Char{^} at the start of a pattern does not +work as an anchor, as this would prevent the iteration. + +} + +@LibEntry{string.gsub (s, pattern, repl [, n])| +Returns a copy of @id{s} +in which all (or the first @id{n}, if given) +occurrences of the @id{pattern} @see{pm} have been +replaced by a replacement string specified by @id{repl}, +which can be a string, a table, or a function. +@id{gsub} also returns, as its second value, +the total number of matches that occurred. +The name @id{gsub} comes from @emph{Global SUBstitution}. + +If @id{repl} is a string, then its value is used for replacement. +The @N{character @T{%}} works as an escape character: +any sequence in @id{repl} of the form @T{%@rep{d}}, +with @rep{d} between 1 and 9, +stands for the value of the @rep{d}-th captured substring. +The sequence @T{%0} stands for the whole match. +The sequence @T{%%} stands for a @N{single @T{%}}. + +If @id{repl} is a table, then the table is queried for every match, +using the first capture as the key. + +If @id{repl} is a function, then this function is called every time a +match occurs, with all captured substrings passed as arguments, +in order. + +In any case, +if the pattern specifies no captures, +then it behaves as if the whole pattern was inside a capture. + +If the value returned by the table query or by the function call +is a string or a number, +then it is used as the replacement string; +otherwise, if it is @Rw{false} or @nil, +then there is no replacement +(that is, the original match is kept in the string). + +Here are some examples: +@verbatim{ +x = string.gsub("hello world", "(%w+)", "%1 %1") +--> x="hello hello world world" + +x = string.gsub("hello world", "%w+", "%0 %0", 1) +--> x="hello hello world" + +x = string.gsub("hello world from Lua", "(%w+)%s*(%w+)", "%2 %1") +--> x="world hello Lua from" + +x = string.gsub("home = $HOME, user = $USER", "%$(%w+)", os.getenv) +--> x="home = /home/roberto, user = roberto" + +x = string.gsub("4+5 = $return 4+5$", "%$(.-)%$", function (s) + return load(s)() + end) +--> x="4+5 = 9" + +local t = {name="lua", version="5.4"} +x = string.gsub("$name-$version.tar.gz", "%$(%w+)", t) +--> x="lua-5.4.tar.gz" +} + +} + +@LibEntry{string.len (s)| +Receives a string and returns its length. +The empty string @T{""} has length 0. +Embedded zeros are counted, +so @T{"a\000bc\000"} has length 5. + +} + +@LibEntry{string.lower (s)| +Receives a string and returns a copy of this string with all +uppercase letters changed to lowercase. +All other characters are left unchanged. +The definition of what an uppercase letter is depends on the current locale. + +} + +@LibEntry{string.match (s, pattern [, init])| +Looks for the first @emph{match} of +@id{pattern} @see{pm} in the string @id{s}. +If it finds one, then @id{match} returns +the captures from the pattern; +otherwise it returns @nil. +If @id{pattern} specifies no captures, +then the whole match is returned. +A third, optional numeric argument @id{init} specifies +where to start the search; +its default value @N{is 1} and can be negative. + +} + +@LibEntry{string.pack (fmt, v1, v2, @Cdots)| + +Returns a binary string containing the values @id{v1}, @id{v2}, etc. +packed (that is, serialized in binary form) +according to the format string @id{fmt} @see{pack}. + +} + +@LibEntry{string.packsize (fmt)| + +Returns the size of a string resulting from @Lid{string.pack} +with the given format. +The format string cannot have the variable-length options +@Char{s} or @Char{z} @see{pack}. + +} + +@LibEntry{string.rep (s, n [, sep])| +Returns a string that is the concatenation of @id{n} copies of +the string @id{s} separated by the string @id{sep}. +The default value for @id{sep} is the empty string +(that is, no separator). +Returns the empty string if @id{n} is not positive. + +(Note that it is very easy to exhaust the memory of your machine +with a single call to this function.) + +} + +@LibEntry{string.reverse (s)| +Returns a string that is the string @id{s} reversed. + +} + +@LibEntry{string.sub (s, i [, j])| +Returns the substring of @id{s} that +starts at @id{i} and continues until @id{j}; +@id{i} and @id{j} can be negative. +If @id{j} is absent, then it is assumed to be equal to @num{-1} +(which is the same as the string length). +In particular, +the call @T{string.sub(s,1,j)} returns a prefix of @id{s} +with length @id{j}, +and @T{string.sub(s, -i)} (for a positive @id{i}) +returns a suffix of @id{s} +with length @id{i}. + +If, after the translation of negative indices, +@id{i} is less than 1, +it is corrected to 1. +If @id{j} is greater than the string length, +it is corrected to that length. +If, after these corrections, +@id{i} is greater than @id{j}, +the function returns the empty string. + +} + +@LibEntry{string.unpack (fmt, s [, pos])| + +Returns the values packed in string @id{s} @seeF{string.pack} +according to the format string @id{fmt} @see{pack}. +An optional @id{pos} marks where +to start reading in @id{s} (default is 1). +After the read values, +this function also returns the index of the first unread byte in @id{s}. + +} + +@LibEntry{string.upper (s)| +Receives a string and returns a copy of this string with all +lowercase letters changed to uppercase. +All other characters are left unchanged. +The definition of what a lowercase letter is depends on the current locale. + +} + + +@sect3{pm| @title{Patterns} + +Patterns in Lua are described by regular strings, +which are interpreted as patterns by the pattern-matching functions +@Lid{string.find}, +@Lid{string.gmatch}, +@Lid{string.gsub}, +and @Lid{string.match}. +This section describes the syntax and the meaning +(that is, what they match) of these strings. + +@sect4{@title{Character Class:} +A @def{character class} is used to represent a set of characters. +The following combinations are allowed in describing a character class: +@description{ + +@item{@rep{x}| +(where @rep{x} is not one of the @emphx{magic characters} +@T{^$()%.[]*+-?}) +represents the character @emph{x} itself. +} + +@item{@T{.}| (a dot) represents all characters.} + +@item{@T{%a}| represents all letters.} + +@item{@T{%c}| represents all control characters.} + +@item{@T{%d}| represents all digits.} + +@item{@T{%g}| represents all printable characters except space.} + +@item{@T{%l}| represents all lowercase letters.} + +@item{@T{%p}| represents all punctuation characters.} + +@item{@T{%s}| represents all space characters.} + +@item{@T{%u}| represents all uppercase letters.} + +@item{@T{%w}| represents all alphanumeric characters.} + +@item{@T{%x}| represents all hexadecimal digits.} + +@item{@T{%@rep{x}}| (where @rep{x} is any non-alphanumeric character) +represents the character @rep{x}. +This is the standard way to escape the magic characters. +Any non-alphanumeric character +(including all punctuation characters, even the non-magical) +can be preceded by a @Char{%} +when used to represent itself in a pattern. +} + +@item{@T{[@rep{set}]}| +represents the class which is the union of all +characters in @rep{set}. +A range of characters can be specified by +separating the end characters of the range, +in ascending order, with a @Char{-}. +All classes @T{%}@emph{x} described above can also be used as +components in @rep{set}. +All other characters in @rep{set} represent themselves. +For example, @T{[%w_]} (or @T{[_%w]}) +represents all alphanumeric characters plus the underscore, +@T{[0-7]} represents the octal digits, +and @T{[0-7%l%-]} represents the octal digits plus +the lowercase letters plus the @Char{-} character. + +You can put a closing square bracket in a set +by positioning it as the first character in the set. +You can put a hyphen in a set +by positioning it as the first or the last character in the set. +(You can also use an escape for both cases.) + +The interaction between ranges and classes is not defined. +Therefore, patterns like @T{[%a-z]} or @T{[a-%%]} +have no meaning. +} + +@item{@T{[^@rep{set}]}| +represents the complement of @rep{set}, +where @rep{set} is interpreted as above. +} + +} +For all classes represented by single letters (@T{%a}, @T{%c}, etc.), +the corresponding uppercase letter represents the complement of the class. +For instance, @T{%S} represents all non-space characters. + +The definitions of letter, space, and other character groups +depend on the current locale. +In particular, the class @T{[a-z]} may not be equivalent to @T{%l}. + +} + +@sect4{@title{Pattern Item:} +A @def{pattern item} can be +@itemize{ + +@item{ +a single character class, +which matches any single character in the class; +} + +@item{ +a single character class followed by @Char{*}, +which matches zero or more repetitions of characters in the class. +These repetition items will always match the longest possible sequence; +} + +@item{ +a single character class followed by @Char{+}, +which matches one or more repetitions of characters in the class. +These repetition items will always match the longest possible sequence; +} + +@item{ +a single character class followed by @Char{-}, +which also matches zero or more repetitions of characters in the class. +Unlike @Char{*}, +these repetition items will always match the shortest possible sequence; +} + +@item{ +a single character class followed by @Char{?}, +which matches zero or one occurrence of a character in the class. +It always matches one occurrence if possible; +} + +@item{ +@T{%@rep{n}}, for @rep{n} between 1 and 9; +such item matches a substring equal to the @rep{n}-th captured string +(see below); +} + +@item{ +@T{%b@rep{xy}}, where @rep{x} and @rep{y} are two distinct characters; +such item matches strings that start @N{with @rep{x}}, end @N{with @rep{y}}, +and where the @rep{x} and @rep{y} are @emph{balanced}. +This means that, if one reads the string from left to right, +counting @M{+1} for an @rep{x} and @M{-1} for a @rep{y}, +the ending @rep{y} is the first @rep{y} where the count reaches 0. +For instance, the item @T{%b()} matches expressions with +balanced parentheses. +} + +@item{ +@T{%f[@rep{set}]}, a @def{frontier pattern}; +such item matches an empty string at any position such that +the next character belongs to @rep{set} +and the previous character does not belong to @rep{set}. +The set @rep{set} is interpreted as previously described. +The beginning and the end of the subject are handled as if +they were the character @Char{\0}. +} + +} + +} + +@sect4{@title{Pattern:} +A @def{pattern} is a sequence of pattern items. +A caret @Char{^} at the beginning of a pattern anchors the match at the +beginning of the subject string. +A @Char{$} at the end of a pattern anchors the match at the +end of the subject string. +At other positions, +@Char{^} and @Char{$} have no special meaning and represent themselves. + +} + +@sect4{@title{Captures:} +A pattern can contain sub-patterns enclosed in parentheses; +they describe @def{captures}. +When a match succeeds, the substrings of the subject string +that match captures are stored (@emph{captured}) for future use. +Captures are numbered according to their left parentheses. +For instance, in the pattern @T{"(a*(.)%w(%s*))"}, +the part of the string matching @T{"a*(.)%w(%s*)"} is +stored as the first capture (and therefore has @N{number 1}); +the character matching @St{.} is captured with @N{number 2}, +and the part matching @St{%s*} has @N{number 3}. + +As a special case, the empty capture @T{()} captures +the current string position (a number). +For instance, if we apply the pattern @T{"()aa()"} on the +string @T{"flaaap"}, there will be two captures: @N{3 and 5}. + +} + +@sect4{@title{Multiple matches:} +The function @Lid{string.gsub} and the iterator @Lid{string.gmatch} +match multiple occurrences of the given pattern in the subject. +For these functions, +a new match is considered valid only +if it ends at least one byte after the previous match. +In other words, the pattern machine never accepts the +empty string as a match immediately after another match. +As an example, +consider the results of the following code: +@verbatim{ +> string.gsub("abc", "()a*()", print) +--> 1 2 +--> 3 3 +--> 4 4 +} +The second and third results come from Lua matching an empty +string after @Char{b} and another one after @Char{c}. +Lua does not match an empty string after @Char{a}, +because it would end at the same position of the previous match. + +} + +} + +@sect3{pack| @title{Format Strings for Pack and Unpack} + +The first argument to @Lid{string.pack}, +@Lid{string.packsize}, and @Lid{string.unpack} +is a format string, +which describes the layout of the structure being created or read. + +A format string is a sequence of conversion options. +The conversion options are as follows: +@description{ +@item{@T{<}|sets little endian} +@item{@T{>}|sets big endian} +@item{@T{=}|sets native endian} +@item{@T{![@rep{n}]}|sets maximum alignment to @id{n} +(default is native alignment)} +@item{@T{b}|a signed byte (@id{char})} +@item{@T{B}|an unsigned byte (@id{char})} +@item{@T{h}|a signed @id{short} (native size)} +@item{@T{H}|an unsigned @id{short} (native size)} +@item{@T{l}|a signed @id{long} (native size)} +@item{@T{L}|an unsigned @id{long} (native size)} +@item{@T{j}|a @id{lua_Integer}} +@item{@T{J}|a @id{lua_Unsigned}} +@item{@T{T}|a @id{size_t} (native size)} +@item{@T{i[@rep{n}]}|a signed @id{int} with @id{n} bytes +(default is native size)} +@item{@T{I[@rep{n}]}|an unsigned @id{int} with @id{n} bytes +(default is native size)} +@item{@T{f}|a @id{float} (native size)} +@item{@T{d}|a @id{double} (native size)} +@item{@T{n}|a @id{lua_Number}} +@item{@T{c@rep{n}}|a fixed-sized string with @id{n} bytes} +@item{@T{z}|a zero-terminated string} +@item{@T{s[@emph{n}]}|a string preceded by its length +coded as an unsigned integer with @id{n} bytes +(default is a @id{size_t})} +@item{@T{x}|one byte of padding} +@item{@T{X@rep{op}}|an empty item that aligns +according to option @id{op} +(which is otherwise ignored)} +@item{@Char{ }|(empty space) ignored} +} +(A @St{[@rep{n}]} means an optional integral numeral.) +Except for padding, spaces, and configurations +(options @St{xX <=>!}), +each option corresponds to an argument (in @Lid{string.pack}) +or a result (in @Lid{string.unpack}). + +For options @St{!@rep{n}}, @St{s@rep{n}}, @St{i@rep{n}}, and @St{I@rep{n}}, +@id{n} can be any integer between 1 and 16. +All integral options check overflows; +@Lid{string.pack} checks whether the given value fits in the given size; +@Lid{string.unpack} checks whether the read value fits in a Lua integer. + +Any format string starts as if prefixed by @St{!1=}, +that is, +with maximum alignment of 1 (no alignment) +and native endianness. + +Alignment works as follows: +For each option, +the format gets extra padding until the data starts +at an offset that is a multiple of the minimum between the +option size and the maximum alignment; +this minimum must be a power of 2. +Options @St{c} and @St{z} are not aligned; +option @St{s} follows the alignment of its starting integer. + +All padding is filled with zeros by @Lid{string.pack} +(and ignored by @Lid{string.unpack}). + +} + +} + +@sect2{utf8| @title{UTF-8 Support} + +This library provides basic support for @x{UTF-8} encoding. +It provides all its functions inside the table @defid{utf8}. +This library does not provide any support for @x{Unicode} other +than the handling of the encoding. +Any operation that needs the meaning of a character, +such as character classification, is outside its scope. + +Unless stated otherwise, +all functions that expect a byte position as a parameter +assume that the given position is either the start of a byte sequence +or one plus the length of the subject string. +As in the string library, +negative indices count from the end of the string. + + +@LibEntry{utf8.char (@Cdots)| +Receives zero or more integers, +converts each one to its corresponding UTF-8 byte sequence +and returns a string with the concatenation of all these sequences. + +} + +@LibEntry{utf8.charpattern| +The pattern (a string, not a function) @St{[\0-\x7F\xC2-\xF4][\x80-\xBF]*} +@see{pm}, +which matches exactly one UTF-8 byte sequence, +assuming that the subject is a valid UTF-8 string. + +} + +@LibEntry{utf8.codes (s)| + +Returns values so that the construction +@verbatim{ +for p, c in utf8.codes(s) do @rep{body} end +} +will iterate over all characters in string @id{s}, +with @id{p} being the position (in bytes) and @id{c} the code point +of each character. +It raises an error if it meets any invalid byte sequence. + +} + +@LibEntry{utf8.codepoint (s [, i [, j]])| +Returns the codepoints (as integers) from all characters in @id{s} +that start between byte position @id{i} and @id{j} (both included). +The default for @id{i} is 1 and for @id{j} is @id{i}. +It raises an error if it meets any invalid byte sequence. + +} + +@LibEntry{utf8.len (s [, i [, j]])| +Returns the number of UTF-8 characters in string @id{s} +that start between positions @id{i} and @id{j} (both inclusive). +The default for @id{i} is @num{1} and for @id{j} is @num{-1}. +If it finds any invalid byte sequence, +returns a false value plus the position of the first invalid byte. + +} + +@LibEntry{utf8.offset (s, n [, i])| +Returns the position (in bytes) where the encoding of the +@id{n}-th character of @id{s} +(counting from position @id{i}) starts. +A negative @id{n} gets characters before position @id{i}. +The default for @id{i} is 1 when @id{n} is non-negative +and @T{#s + 1} otherwise, +so that @T{utf8.offset(s, -n)} gets the offset of the +@id{n}-th character from the end of the string. +If the specified character is neither in the subject +nor right after its end, +the function returns @nil. + +As a special case, +when @id{n} is 0 the function returns the start of the encoding +of the character that contains the @id{i}-th byte of @id{s}. + +This function assumes that @id{s} is a valid UTF-8 string. + +} + +} + +@sect2{tablib| @title{Table Manipulation} + +This library provides generic functions for table manipulation. +It provides all its functions inside the table @defid{table}. + +Remember that, whenever an operation needs the length of a table, +all caveats about the length operator apply @see{len-op}. +All functions ignore non-numeric keys +in the tables given as arguments. + + +@LibEntry{table.concat (list [, sep [, i [, j]]])| + +Given a list where all elements are strings or numbers, +returns the string @T{list[i]..sep..list[i+1] @Cdots sep..list[j]}. +The default value for @id{sep} is the empty string, +the default for @id{i} is 1, +and the default for @id{j} is @T{#list}. +If @id{i} is greater than @id{j}, returns the empty string. + +} + +@LibEntry{table.insert (list, [pos,] value)| + +Inserts element @id{value} at position @id{pos} in @id{list}, +shifting up the elements +@T{list[pos], list[pos+1], @Cdots, list[#list]}. +The default value for @id{pos} is @T{#list+1}, +so that a call @T{table.insert(t,x)} inserts @id{x} at the end +of list @id{t}. + +} + +@LibEntry{table.move (a1, f, e, t [,a2])| + +Moves elements from table @id{a1} to table @id{a2}, +performing the equivalent to the following +multiple assignment: +@T{a2[t],@Cdots = a1[f],@Cdots,a1[e]}. +The default for @id{a2} is @id{a1}. +The destination range can overlap with the source range. +The number of elements to be moved must fit in a Lua integer. + +Returns the destination table @id{a2}. + +} + +@LibEntry{table.pack (@Cdots)| + +Returns a new table with all arguments stored into keys 1, 2, etc. +and with a field @St{n} with the total number of arguments. +Note that the resulting table may not be a sequence, +if some arguments are @nil. + +} + +@LibEntry{table.remove (list [, pos])| + +Removes from @id{list} the element at position @id{pos}, +returning the value of the removed element. +When @id{pos} is an integer between 1 and @T{#list}, +it shifts down the elements +@T{list[pos+1], list[pos+2], @Cdots, list[#list]} +and erases element @T{list[#list]}; +The index @id{pos} can also be 0 when @T{#list} is 0, +or @T{#list + 1}. + +The default value for @id{pos} is @T{#list}, +so that a call @T{table.remove(l)} removes the last element +of list @id{l}. + +} + +@LibEntry{table.sort (list [, comp])| + +Sorts list elements in a given order, @emph{in-place}, +from @T{list[1]} to @T{list[#list]}. +If @id{comp} is given, +then it must be a function that receives two list elements +and returns true when the first element must come +before the second in the final order +(so that, after the sort, +@T{i < j} implies @T{not comp(list[j],list[i])}). +If @id{comp} is not given, +then the standard Lua operator @T{<} is used instead. + +Note that the @id{comp} function must define +a strict partial order over the elements in the list; +that is, it must be asymmetric and transitive. +Otherwise, no valid sort may be possible. + +The sort algorithm is not stable: +elements considered equal by the given order +may have their relative positions changed by the sort. + +} + +@LibEntry{table.unpack (list [, i [, j]])| + +Returns the elements from the given list. +This function is equivalent to +@verbatim{ +return list[i], list[i+1], @Cdots, list[j] +} +By default, @id{i} @N{is 1} and @id{j} is @T{#list}. + +} + +} + +@sect2{mathlib| @title{Mathematical Functions} + +This library provides basic mathematical functions. +It provides all its functions and constants inside the table @defid{math}. +Functions with the annotation @St{integer/float} give +integer results for integer arguments +and float results for float (or mixed) arguments. +Rounding functions +(@Lid{math.ceil}, @Lid{math.floor}, and @Lid{math.modf}) +return an integer when the result fits in the range of an integer, +or a float otherwise. + +@LibEntry{math.abs (x)| + +Returns the absolute value of @id{x}. (integer/float) + +} + +@LibEntry{math.acos (x)| + +Returns the arc cosine of @id{x} (in radians). + +} + +@LibEntry{math.asin (x)| + +Returns the arc sine of @id{x} (in radians). + +} + +@LibEntry{math.atan (y [, x])| + +@index{atan2} +Returns the arc tangent of @T{y/x} (in radians), +but uses the signs of both parameters to find the +quadrant of the result. +(It also handles correctly the case of @id{x} being zero.) + +The default value for @id{x} is 1, +so that the call @T{math.atan(y)} +returns the arc tangent of @id{y}. + +} + +@LibEntry{math.ceil (x)| + +Returns the smallest integral value larger than or equal to @id{x}. + +} + +@LibEntry{math.cos (x)| + +Returns the cosine of @id{x} (assumed to be in radians). + +} + +@LibEntry{math.deg (x)| + +Converts the angle @id{x} from radians to degrees. + +} + +@LibEntry{math.exp (x)| + +Returns the value @M{e@sp{x}} +(where @id{e} is the base of natural logarithms). + +} + +@LibEntry{math.floor (x)| + +Returns the largest integral value smaller than or equal to @id{x}. + +} + +@LibEntry{math.fmod (x, y)| + +Returns the remainder of the division of @id{x} by @id{y} +that rounds the quotient towards zero. (integer/float) + +} + +@LibEntry{math.huge| + +The float value @idx{HUGE_VAL}, +a value larger than any other numeric value. + +} + +@LibEntry{math.log (x [, base])| + +Returns the logarithm of @id{x} in the given base. +The default for @id{base} is @M{e} +(so that the function returns the natural logarithm of @id{x}). + +} + +@LibEntry{math.max (x, @Cdots)| + +Returns the argument with the maximum value, +according to the Lua operator @T{<}. (integer/float) + +} + +@LibEntry{math.maxinteger| +An integer with the maximum value for an integer. + +} + +@LibEntry{math.min (x, @Cdots)| + +Returns the argument with the minimum value, +according to the Lua operator @T{<}. (integer/float) + +} + +@LibEntry{math.mininteger| +An integer with the minimum value for an integer. + +} + +@LibEntry{math.modf (x)| + +Returns the integral part of @id{x} and the fractional part of @id{x}. +Its second result is always a float. + +} + +@LibEntry{math.pi| + +The value of @M{@pi}. + +} + +@LibEntry{math.rad (x)| + +Converts the angle @id{x} from degrees to radians. + +} + +@LibEntry{math.random ([m [, n]])| + +When called without arguments, +returns a pseudo-random float with uniform distribution +in the range @C{(} @M{[0,1)}. @C{]} +When called with two integers @id{m} and @id{n}, +@id{math.random} returns a pseudo-random integer +with uniform distribution in the range @M{[m, n]}. +The call @T{math.random(n)}, for a positive @id{n}, +is equivalent to @T{math.random(1,n)}. +The call @T{math.random(0)} produces an integer with +all bits (pseudo)random. + +Lua initializes its pseudo-random generator with +a weak attempt for ``randomness'', +so that @id{math.random} should generate +different sequences of results each time the program runs. +To ensure a required level of randomness to the initial state +(or contrarily, to have a deterministic sequence, +for instance when debugging a program), +you should call @Lid{math.randomseed} explicitly. + +The results from this function have good statistical qualities, +but they are not cryptographically secure. +(For instance, there are no garanties that it is hard +to predict future results based on the observation of +some number of previous results.) + +} + +@LibEntry{math.randomseed (x [, y])| + +Sets @id{x} and @id{y} as the @Q{seed} +for the pseudo-random generator: +equal seeds produce equal sequences of numbers. +The default for @id{y} is zero. + +} + +@LibEntry{math.sin (x)| + +Returns the sine of @id{x} (assumed to be in radians). + +} + +@LibEntry{math.sqrt (x)| + +Returns the square root of @id{x}. +(You can also use the expression @T{x^0.5} to compute this value.) + +} + +@LibEntry{math.tan (x)| + +Returns the tangent of @id{x} (assumed to be in radians). + +} + +@LibEntry{math.tointeger (x)| + +If the value @id{x} is convertible to an integer, +returns that integer. +Otherwise, returns @nil. + +} + +@LibEntry{math.type (x)| + +Returns @St{integer} if @id{x} is an integer, +@St{float} if it is a float, +or @nil if @id{x} is not a number. + +} + +@LibEntry{math.ult (m, n)| + +Returns a boolean, +true if and only if integer @id{m} is below integer @id{n} when +they are compared as @x{unsigned integers}. + +} + +} + + +@sect2{iolib| @title{Input and Output Facilities} + +The I/O library provides two different styles for file manipulation. +The first one uses implicit file handles; +that is, there are operations to set a default input file and a +default output file, +and all input/output operations are over these default files. +The second style uses explicit file handles. + +When using implicit file handles, +all operations are supplied by table @defid{io}. +When using explicit file handles, +the operation @Lid{io.open} returns a file handle +and then all operations are supplied as methods of the file handle. + +The table @id{io} also provides +three predefined file handles with their usual meanings from C: +@defid{io.stdin}, @defid{io.stdout}, and @defid{io.stderr}. +The I/O library never closes these files. + +Unless otherwise stated, +all I/O functions return @nil on failure +(plus an error message as a second result and +a system-dependent error code as a third result) +and some value different from @nil on success. +On non-POSIX systems, +the computation of the error message and error code +in case of errors +may be not @x{thread safe}, +because they rely on the global C variable @id{errno}. + +@LibEntry{io.close ([file])| + +Equivalent to @T{file:close()}. +Without a @id{file}, closes the default output file. + +} + +@LibEntry{io.flush ()| + +Equivalent to @T{io.output():flush()}. + +} + +@LibEntry{io.input ([file])| + +When called with a file name, it opens the named file (in text mode), +and sets its handle as the default input file. +When called with a file handle, +it simply sets this file handle as the default input file. +When called without parameters, +it returns the current default input file. + +In case of errors this function raises the error, +instead of returning an error code. + +} + +@LibEntry{io.lines ([filename, @Cdots])| + +Opens the given file name in read mode +and returns an iterator function that +works like @T{file:lines(@Cdots)} over the opened file. +When the iterator function detects the end of file, +it returns no values (to finish the loop) and automatically closes the file. + +The call @T{io.lines()} (with no file name) is equivalent +to @T{io.input():lines("l")}; +that is, it iterates over the lines of the default input file. +In this case, the iterator does not close the file when the loop ends. + +In case of errors this function raises the error, +instead of returning an error code. + +} + +@LibEntry{io.open (filename [, mode])| + +This function opens a file, +in the mode specified in the string @id{mode}. +In case of success, +it returns a new file handle. + +The @id{mode} string can be any of the following: +@description{ +@item{@St{r}| read mode (the default);} +@item{@St{w}| write mode;} +@item{@St{a}| append mode;} +@item{@St{r+}| update mode, all previous data is preserved;} +@item{@St{w+}| update mode, all previous data is erased;} +@item{@St{a+}| append update mode, previous data is preserved, + writing is only allowed at the end of file.} +} +The @id{mode} string can also have a @Char{b} at the end, +which is needed in some systems to open the file in binary mode. + +} + +@LibEntry{io.output ([file])| + +Similar to @Lid{io.input}, but operates over the default output file. + +} + +@LibEntry{io.popen (prog [, mode])| + +This function is system dependent and is not available +on all platforms. + +Starts program @id{prog} in a separated process and returns +a file handle that you can use to read data from this program +(if @id{mode} is @T{"r"}, the default) +or to write data to this program +(if @id{mode} is @T{"w"}). + +} + +@LibEntry{io.read (@Cdots)| + +Equivalent to @T{io.input():read(@Cdots)}. + +} + +@LibEntry{io.tmpfile ()| + +In case of success, +returns a handle for a temporary file. +This file is opened in update mode +and it is automatically removed when the program ends. + +} + +@LibEntry{io.type (obj)| + +Checks whether @id{obj} is a valid file handle. +Returns the string @T{"file"} if @id{obj} is an open file handle, +@T{"closed file"} if @id{obj} is a closed file handle, +or @nil if @id{obj} is not a file handle. + +} + +@LibEntry{io.write (@Cdots)| + +Equivalent to @T{io.output():write(@Cdots)}. + + +} + +@LibEntry{file:close ()| + +Closes @id{file}. +Note that files are automatically closed when +their handles are garbage collected, +but that takes an unpredictable amount of time to happen. + +When closing a file handle created with @Lid{io.popen}, +@Lid{file:close} returns the same values +returned by @Lid{os.execute}. + +} + +@LibEntry{file:flush ()| + +Saves any written data to @id{file}. + +} + +@LibEntry{file:lines (@Cdots)| + +Returns an iterator function that, +each time it is called, +reads the file according to the given formats. +When no format is given, +uses @St{l} as a default. +As an example, the construction +@verbatim{ +for c in file:lines(1) do @rep{body} end +} +will iterate over all characters of the file, +starting at the current position. +Unlike @Lid{io.lines}, this function does not close the file +when the loop ends. + +In case of errors this function raises the error, +instead of returning an error code. + +} + +@LibEntry{file:read (@Cdots)| + +Reads the file @id{file}, +according to the given formats, which specify what to read. +For each format, +the function returns a string or a number with the characters read, +or @nil if it cannot read data with the specified format. +(In this latter case, +the function does not read subsequent formats.) +When called without parameters, +it uses a default format that reads the next line +(see below). + +The available formats are +@description{ + +@item{@St{n}| +reads a numeral and returns it as a float or an integer, +following the lexical conventions of Lua. +(The numeral may have leading spaces and a sign.) +This format always reads the longest input sequence that +is a valid prefix for a numeral; +if that prefix does not form a valid numeral +(e.g., an empty string, @St{0x}, or @St{3.4e-}), +it is discarded and the format returns @nil. +} + +@item{@St{a}| +reads the whole file, starting at the current position. +On end of file, it returns the empty string. +} + +@item{@St{l}| +reads the next line skipping the end of line, +returning @nil on end of file. +This is the default format. +} + +@item{@St{L}| +reads the next line keeping the end-of-line character (if present), +returning @nil on end of file. +} + +@item{@emph{number}| +reads a string with up to this number of bytes, +returning @nil on end of file. +If @id{number} is zero, +it reads nothing and returns an empty string, +or @nil on end of file. +} + +} +The formats @St{l} and @St{L} should be used only for text files. + +} + +@LibEntry{file:seek ([whence [, offset]])| + +Sets and gets the file position, +measured from the beginning of the file, +to the position given by @id{offset} plus a base +specified by the string @id{whence}, as follows: +@description{ +@item{@St{set}| base is position 0 (beginning of the file);} +@item{@St{cur}| base is current position;} +@item{@St{end}| base is end of file;} +} +In case of success, @id{seek} returns the final file position, +measured in bytes from the beginning of the file. +If @id{seek} fails, it returns @nil, +plus a string describing the error. + +The default value for @id{whence} is @T{"cur"}, +and for @id{offset} is 0. +Therefore, the call @T{file:seek()} returns the current +file position, without changing it; +the call @T{file:seek("set")} sets the position to the +beginning of the file (and returns 0); +and the call @T{file:seek("end")} sets the position to the +end of the file, and returns its size. + +} + +@LibEntry{file:setvbuf (mode [, size])| + +Sets the buffering mode for an output file. +There are three available modes: +@description{ + +@item{@St{no}| +no buffering; the result of any output operation appears immediately. +} + +@item{@St{full}| +full buffering; output operation is performed only +when the buffer is full or when +you explicitly @T{flush} the file @seeF{io.flush}. +} + +@item{@St{line}| +line buffering; output is buffered until a newline is output +or there is any input from some special files +(such as a terminal device). +} + +} +For the last two cases, @id{size} +specifies the size of the buffer, in bytes. +The default is an appropriate size. + +} + +@LibEntry{file:write (@Cdots)| + +Writes the value of each of its arguments to @id{file}. +The arguments must be strings or numbers. + +In case of success, this function returns @id{file}. +Otherwise it returns @nil plus a string describing the error. + +} + +} + +@sect2{oslib| @title{Operating System Facilities} + +This library is implemented through table @defid{os}. + + +@LibEntry{os.clock ()| + +Returns an approximation of the amount in seconds of CPU time +used by the program. + +} + +@LibEntry{os.date ([format [, time]])| + +Returns a string or a table containing date and time, +formatted according to the given string @id{format}. + +If the @id{time} argument is present, +this is the time to be formatted +(see the @Lid{os.time} function for a description of this value). +Otherwise, @id{date} formats the current time. + +If @id{format} starts with @Char{!}, +then the date is formatted in Coordinated Universal Time. +After this optional character, +if @id{format} is the string @St{*t}, +then @id{date} returns a table with the following fields: +@id{year}, @id{month} (1@En{}12), @id{day} (1@En{}31), +@id{hour} (0@En{}23), @id{min} (0@En{}59), +@id{sec} (0@En{}61, due to leap seconds), +@id{wday} (weekday, 1@En{}7, Sunday @N{is 1}), +@id{yday} (day of the year, 1@En{}366), +and @id{isdst} (daylight saving flag, a boolean). +This last field may be absent +if the information is not available. + +If @id{format} is not @St{*t}, +then @id{date} returns the date as a string, +formatted according to the same rules as the @ANSI{strftime}. + +When called without arguments, +@id{date} returns a reasonable date and time representation that depends on +the host system and on the current locale. +(More specifically, @T{os.date()} is equivalent to @T{os.date("%c")}.) + +On non-POSIX systems, +this function may be not @x{thread safe} +because of its reliance on @CId{gmtime} and @CId{localtime}. + +} + +@LibEntry{os.difftime (t2, t1)| + +Returns the difference, in seconds, +from time @id{t1} to time @id{t2} +(where the times are values returned by @Lid{os.time}). +In @x{POSIX}, @x{Windows}, and some other systems, +this value is exactly @id{t2}@M{-}@id{t1}. + +} + +@LibEntry{os.execute ([command])| + +This function is equivalent to the @ANSI{system}. +It passes @id{command} to be executed by an operating system shell. +Its first result is @true +if the command terminated successfully, +or @nil otherwise. +After this first result +the function returns a string plus a number, +as follows: +@description{ + +@item{@St{exit}| +the command terminated normally; +the following number is the exit status of the command. +} + +@item{@St{signal}| +the command was terminated by a signal; +the following number is the signal that terminated the command. +} + +} + +When called without a @id{command}, +@id{os.execute} returns a boolean that is true if a shell is available. + +} + +@LibEntry{os.exit ([code [, close]])| + +Calls the @ANSI{exit} to terminate the host program. +If @id{code} is @Rw{true}, +the returned status is @idx{EXIT_SUCCESS}; +if @id{code} is @Rw{false}, +the returned status is @idx{EXIT_FAILURE}; +if @id{code} is a number, +the returned status is this number. +The default value for @id{code} is @Rw{true}. + +If the optional second argument @id{close} is true, +closes the Lua state before exiting. + +} + +@LibEntry{os.getenv (varname)| + +Returns the value of the process environment variable @id{varname}, +or @nil if the variable is not defined. + +} + +@LibEntry{os.remove (filename)| + +Deletes the file (or empty directory, on @x{POSIX} systems) +with the given name. +If this function fails, it returns @nil, +plus a string describing the error and the error code. +Otherwise, it returns true. + +} + +@LibEntry{os.rename (oldname, newname)| + +Renames the file or directory named @id{oldname} to @id{newname}. +If this function fails, it returns @nil, +plus a string describing the error and the error code. +Otherwise, it returns true. + +} + +@LibEntry{os.setlocale (locale [, category])| + +Sets the current locale of the program. +@id{locale} is a system-dependent string specifying a locale; +@id{category} is an optional string describing which category to change: +@T{"all"}, @T{"collate"}, @T{"ctype"}, +@T{"monetary"}, @T{"numeric"}, or @T{"time"}; +the default category is @T{"all"}. +The function returns the name of the new locale, +or @nil if the request cannot be honored. + +If @id{locale} is the empty string, +the current locale is set to an implementation-defined native locale. +If @id{locale} is the string @St{C}, +the current locale is set to the standard C locale. + +When called with @nil as the first argument, +this function only returns the name of the current locale +for the given category. + +This function may be not @x{thread safe} +because of its reliance on @CId{setlocale}. + +} + +@LibEntry{os.time ([table])| + +Returns the current time when called without arguments, +or a time representing the local date and time specified by the given table. +This table must have fields @id{year}, @id{month}, and @id{day}, +and may have fields +@id{hour} (default is 12), +@id{min} (default is 0), +@id{sec} (default is 0), +and @id{isdst} (default is @nil). +Other fields are ignored. +For a description of these fields, see the @Lid{os.date} function. + +When the function is called, +the values in these fields do not need to be inside their valid ranges. +For instance, if @id{sec} is -10, +it means 10 seconds before the time specified by the other fields; +if @id{hour} is 1000, +it means 1000 hours after the time specified by the other fields. + +The returned value is a number, whose meaning depends on your system. +In @x{POSIX}, @x{Windows}, and some other systems, +this number counts the number +of seconds since some given start time (the @Q{epoch}). +In other systems, the meaning is not specified, +and the number returned by @id{time} can be used only as an argument to +@Lid{os.date} and @Lid{os.difftime}. + +When called with a table, +@id{os.time} also normalizes all the fields +documented in the @Lid{os.date} function, +so that they represent the same time as before the call +but with values inside their valid ranges. + +} + +@LibEntry{os.tmpname ()| + +Returns a string with a file name that can +be used for a temporary file. +The file must be explicitly opened before its use +and explicitly removed when no longer needed. + +In @x{POSIX} systems, +this function also creates a file with that name, +to avoid security risks. +(Someone else might create the file with wrong permissions +in the time between getting the name and creating the file.) +You still have to open the file to use it +and to remove it (even if you do not use it). + +When possible, +you may prefer to use @Lid{io.tmpfile}, +which automatically removes the file when the program ends. + +} + +} + +@sect2{debuglib| @title{The Debug Library} + +This library provides +the functionality of the @link{debugI|debug interface} to Lua programs. +You should exert care when using this library. +Several of its functions +violate basic assumptions about Lua code +(e.g., that variables local to a function +cannot be accessed from outside; +that userdata metatables cannot be changed by Lua code; +that Lua programs do not crash) +and therefore can compromise otherwise secure code. +Moreover, some functions in this library may be slow. + +All functions in this library are provided +inside the @defid{debug} table. +All functions that operate over a thread +have an optional first argument which is the +thread to operate over. +The default is always the current thread. + + +@LibEntry{debug.debug ()| + +Enters an interactive mode with the user, +running each string that the user enters. +Using simple commands and other debug facilities, +the user can inspect global and local variables, +change their values, evaluate expressions, and so on. +A line containing only the word @id{cont} finishes this function, +so that the caller continues its execution. + +Note that commands for @id{debug.debug} are not lexically nested +within any function and so have no direct access to local variables. + +} + +@LibEntry{debug.gethook ([thread])| + +Returns the current hook settings of the thread, as three values: +the current hook function, the current hook mask, +and the current hook count +(as set by the @Lid{debug.sethook} function). + +} + +@LibEntry{debug.getinfo ([thread,] f [, what])| + +Returns a table with information about a function. +You can give the function directly +or you can give a number as the value of @id{f}, +which means the function running at level @id{f} of the call stack +of the given thread: +@N{level 0} is the current function (@id{getinfo} itself); +@N{level 1} is the function that called @id{getinfo} +(except for tail calls, which do not count on the stack); +and so on. +If @id{f} is a number larger than the number of active functions, +then @id{getinfo} returns @nil. + +The returned table can contain all the fields returned by @Lid{lua_getinfo}, +with the string @id{what} describing which fields to fill in. +The default for @id{what} is to get all information available, +except the table of valid lines. +If present, +the option @Char{f} +adds a field named @id{func} with the function itself. +If present, +the option @Char{L} +adds a field named @id{activelines} with the table of +valid lines. + +For instance, the expression @T{debug.getinfo(1,"n").name} returns +a name for the current function, +if a reasonable name can be found, +and the expression @T{debug.getinfo(print)} +returns a table with all available information +about the @Lid{print} function. + +} + +@LibEntry{debug.getlocal ([thread,] f, local)| + +This function returns the name and the value of the local variable +with index @id{local} of the function at level @id{f} of the stack. +This function accesses not only explicit local variables, +but also parameters, temporaries, etc. + +The first parameter or local variable has @N{index 1}, and so on, +following the order that they are declared in the code, +counting only the variables that are active +in the current scope of the function. +Negative indices refer to vararg parameters; +@num{-1} is the first vararg parameter. +The function returns @nil if there is no variable with the given index, +and raises an error when called with a level out of range. +(You can call @Lid{debug.getinfo} to check whether the level is valid.) + +Variable names starting with @Char{(} (open parenthesis) @C{)} +represent variables with no known names +(internal variables such as loop control variables, +and variables from chunks saved without debug information). + +The parameter @id{f} may also be a function. +In that case, @id{getlocal} returns only the name of function parameters. + +} + +@LibEntry{debug.getmetatable (value)| + +Returns the metatable of the given @id{value} +or @nil if it does not have a metatable. + +} + +@LibEntry{debug.getregistry ()| + +Returns the registry table @see{registry}. + +} + +@LibEntry{debug.getupvalue (f, up)| + +This function returns the name and the value of the upvalue +with index @id{up} of the function @id{f}. +The function returns @nil if there is no upvalue with the given index. + +Variable names starting with @Char{(} (open parenthesis) @C{)} +represent variables with no known names +(variables from chunks saved without debug information). + +} + +@LibEntry{debug.getuservalue (u, n)| + +Returns the @id{n}-th user value associated +to the userdata @id{u} plus a boolean, +@false if the userdata does not have that value. + +} + +@LibEntry{debug.sethook ([thread,] hook, mask [, count])| + +Sets the given function as a hook. +The string @id{mask} and the number @id{count} describe +when the hook will be called. +The string mask may have any combination of the following characters, +with the given meaning: +@description{ +@item{@Char{c}| the hook is called every time Lua calls a function;} +@item{@Char{r}| the hook is called every time Lua returns from a function;} +@item{@Char{l}| the hook is called every time Lua enters a new line of code.} +} +Moreover, +with a @id{count} different from zero, +the hook is called also after every @id{count} instructions. + +When called without arguments, +@Lid{debug.sethook} turns off the hook. + +When the hook is called, its first parameter is a string +describing the event that has triggered its call: +@T{"call"} (or @T{"tail call"}), +@T{"return"}, +@T{"line"}, and @T{"count"}. +For line events, +the hook also gets the new line number as its second parameter. +Inside a hook, +you can call @id{getinfo} with @N{level 2} to get more information about +the running function +(@N{level 0} is the @id{getinfo} function, +and @N{level 1} is the hook function). + +} + +@LibEntry{debug.setlocal ([thread,] level, local, value)| + +This function assigns the value @id{value} to the local variable +with index @id{local} of the function at level @id{level} of the stack. +The function returns @nil if there is no local +variable with the given index, +and raises an error when called with a @id{level} out of range. +(You can call @id{getinfo} to check whether the level is valid.) +Otherwise, it returns the name of the local variable. + +See @Lid{debug.getlocal} for more information about +variable indices and names. + +} + +@LibEntry{debug.setmetatable (value, table)| + +Sets the metatable for the given @id{value} to the given @id{table} +(which can be @nil). +Returns @id{value}. + +} + +@LibEntry{debug.setupvalue (f, up, value)| + +This function assigns the value @id{value} to the upvalue +with index @id{up} of the function @id{f}. +The function returns @nil if there is no upvalue +with the given index. +Otherwise, it returns the name of the upvalue. + +} + +@LibEntry{debug.setuservalue (udata, value, n)| + +Sets the given @id{value} as +the @id{n}-th user value associated to the given @id{udata}. +@id{udata} must be a full userdata. + +Returns @id{udata}, +or @nil if the userdata does not have that value. + +} + +@LibEntry{debug.traceback ([thread,] [message [, level]])| + +If @id{message} is present but is neither a string nor @nil, +this function returns @id{message} without further processing. +Otherwise, +it returns a string with a traceback of the call stack. +The optional @id{message} string is appended +at the beginning of the traceback. +An optional @id{level} number tells at which level +to start the traceback +(default is 1, the function calling @id{traceback}). + +} + +@LibEntry{debug.upvalueid (f, n)| + +Returns a unique identifier (as a light userdata) +for the upvalue numbered @id{n} +from the given function. + +These unique identifiers allow a program to check whether different +closures share upvalues. +Lua closures that share an upvalue +(that is, that access a same external local variable) +will return identical ids for those upvalue indices. + +} + +@LibEntry{debug.upvaluejoin (f1, n1, f2, n2)| + +Make the @id{n1}-th upvalue of the Lua closure @id{f1} +refer to the @id{n2}-th upvalue of the Lua closure @id{f2}. + +} + +} + +} + + +@C{-------------------------------------------------------------------------} +@sect1{lua-sa| @title{Lua Standalone} + +Although Lua has been designed as an extension language, +to be embedded in a host @N{C program}, +it is also frequently used as a standalone language. +An interpreter for Lua as a standalone language, +called simply @id{lua}, +is provided with the standard distribution. +The @x{standalone interpreter} includes +all standard libraries, including the debug library. +Its usage is: +@verbatim{ +lua [options] [script [args]] +} +The options are: +@description{ +@item{@T{-e @rep{stat}}| executes string @rep{stat};} +@item{@T{-l @rep{mod}}| @Q{requires} @rep{mod} and assigns the + result to global @rep{mod};} +@item{@T{-i}| enters interactive mode after running @rep{script};} +@item{@T{-v}| prints version information;} +@item{@T{-E}| ignores environment variables;} +@item{@T{--}| stops handling options;} +@item{@T{-}| executes @id{stdin} as a file and stops handling options.} +} +After handling its options, @id{lua} runs the given @emph{script}. +When called without arguments, +@id{lua} behaves as @T{lua -v -i} +when the standard input (@id{stdin}) is a terminal, +and as @T{lua -} otherwise. + +When called without option @T{-E}, +the interpreter checks for an environment variable @defid{LUA_INIT_5_4} +(or @defid{LUA_INIT} if the versioned name is not defined) +before running any argument. +If the variable content has the format @T{@At@rep{filename}}, +then @id{lua} executes the file. +Otherwise, @id{lua} executes the string itself. + +When called with option @T{-E}, +besides ignoring @id{LUA_INIT}, +Lua also ignores +the values of @id{LUA_PATH} and @id{LUA_CPATH}, +setting the values of +@Lid{package.path} and @Lid{package.cpath} +with the default paths defined in @id{luaconf.h}. + +All options are handled in order, except @T{-i} and @T{-E}. +For instance, an invocation like +@verbatim{ +$ lua -e'a=1' -e 'print(a)' script.lua +} +will first set @id{a} to 1, then print the value of @id{a}, +and finally run the file @id{script.lua} with no arguments. +(Here @T{$} is the shell prompt. Your prompt may be different.) + +Before running any code, +@id{lua} collects all command-line arguments +in a global table called @id{arg}. +The script name goes to index 0, +the first argument after the script name goes to index 1, +and so on. +Any arguments before the script name +(that is, the interpreter name plus its options) +go to negative indices. +For instance, in the call +@verbatim{ +$ lua -la b.lua t1 t2 +} +the table is like this: +@verbatim{ +arg = { [-2] = "lua", [-1] = "-la", + [0] = "b.lua", + [1] = "t1", [2] = "t2" } +} +If there is no script in the call, +the interpreter name goes to index 0, +followed by the other arguments. +For instance, the call +@verbatim{ +$ lua -e "print(arg[1])" +} +will print @St{-e}. +If there is a script, +the script is called with parameters +@T{arg[1]}, @Cdots, @T{arg[#arg]}. +(Like all chunks in Lua, +the script is compiled as a vararg function.) + +In interactive mode, +Lua repeatedly prompts and waits for a line. +After reading a line, +Lua first try to interpret the line as an expression. +If it succeeds, it prints its value. +Otherwise, it interprets the line as a statement. +If you write an incomplete statement, +the interpreter waits for its completion +by issuing a different prompt. + +If the global variable @defid{_PROMPT} contains a string, +then its value is used as the prompt. +Similarly, if the global variable @defid{_PROMPT2} contains a string, +its value is used as the secondary prompt +(issued during incomplete statements). + +In case of unprotected errors in the script, +the interpreter reports the error to the standard error stream. +If the error object is not a string but +has a metamethod @idx{__tostring}, +the interpreter calls this metamethod to produce the final message. +Otherwise, the interpreter converts the error object to a string +and adds a stack traceback to it. + +When finishing normally, +the interpreter closes its main Lua state +@seeF{lua_close}. +The script can avoid this step by +calling @Lid{os.exit} to terminate. + +To allow the use of Lua as a +script interpreter in Unix systems, +the standalone interpreter skips +the first line of a chunk if it starts with @T{#}. +Therefore, Lua scripts can be made into executable programs +by using @T{chmod +x} and @N{the @T{#!}} form, +as in +@verbatim{ +#!/usr/local/bin/lua +} +(Of course, +the location of the Lua interpreter may be different in your machine. +If @id{lua} is in your @id{PATH}, +then +@verbatim{ +#!/usr/bin/env lua +} +is a more portable solution.) + +} + + +@sect1{incompat| @title{Incompatibilities with the Previous Version} + +Here we list the incompatibilities that you may find when moving a program +from @N{Lua 5.3} to @N{Lua 5.4}. +You can avoid some incompatibilities by compiling Lua with +appropriate options (see file @id{luaconf.h}). +However, +all these compatibility options will be removed in the future. + +Lua versions can always change the C API in ways that +do not imply source-code changes in a program, +such as the numeric values for constants +or the implementation of functions as macros. +Therefore, +you should not assume that binaries are compatible between +different Lua versions. +Always recompile clients of the Lua API when +using a new version. + +Similarly, Lua versions can always change the internal representation +of precompiled chunks; +precompiled chunks are not compatible between different Lua versions. + +The standard paths in the official distribution may +change between versions. + +@sect2{@title{Changes in the Language} +@itemize{ + +@item{ +The coercion of strings to numbers in +arithmetic and bitwise operations +has been removed from the core language. +The string library does a similar job +for arithmetic (but not for bitwise) operations +using the string metamethods. +However, unlike in previous versions, +the new implementation preserves the implicit type of the numeral +in the string. +For instance, the result of @T{"1" + "2"} now is an integer, +not a float. +} + +} + +} + +@sect2{@title{Changes in the Libraries} +@itemize{ + +@item{ +The pseudo-random number generator used by the function @Lid{math.random} +now starts with a somewhat random seed. +Moreover, it uses a different algorithm. +} + +} + +} + +@sect2{@title{Changes in the API} + +@itemize{ + +@item{ +Full userdata now has an arbitrary number of associated user values. +Therefore, the functions @id{lua_newuserdata}, +@id{lua_setuservalue}, and @id{lua_getuservalue} were +replaced by @Lid{lua_newuserdatauv}, +@Lid{lua_setiuservalue}, and @Lid{lua_getiuservalue}, +which have an extra argument. + +(For compatibility, the old names still work as macros assuming +one single user value.) +} + +@item{ +The function @Lid{lua_resume} has an extra parameter. +This out parameter returns the number of values on +the top of the stack that were yielded or returned by the coroutine. +(In older versions, +those values were the entire stack.) +} + +@item{ +The function @Lid{lua_version} returns the version number, +instead of an address of the version number. +(The Lua core should work correctly with libraries using their +own static copies of the same core, +so there is no need to check whether they are using the same +address space.) +} + +} + +} + +} + + +@C{[===============================================================} + +@sect1{BNF| @title{The Complete Syntax of Lua} + +Here is the complete syntax of Lua in extended BNF. +As usual in extended BNF, +@bnfNter{{A}} means 0 or more @bnfNter{A}s, +and @bnfNter{[A]} means an optional @bnfNter{A}. +(For operator precedences, see @See{prec}; +for a description of the terminals +@bnfNter{Name}, @bnfNter{Numeral}, +and @bnfNter{LiteralString}, see @See{lexical}.) +@index{grammar} + +@Produc{ + +@producname{chunk}@producbody{block} + +@producname{block}@producbody{@bnfrep{stat} @bnfopt{retstat}} + +@producname{stat}@producbody{ + @bnfter{;} +@OrNL varlist @bnfter{=} explist +@OrNL functioncall +@OrNL label +@OrNL @Rw{break} +@OrNL @Rw{goto} Name +@OrNL @Rw{do} block @Rw{end} +@OrNL @Rw{while} exp @Rw{do} block @Rw{end} +@OrNL @Rw{repeat} block @Rw{until} exp +@OrNL @Rw{if} exp @Rw{then} block + @bnfrep{@Rw{elseif} exp @Rw{then} block} + @bnfopt{@Rw{else} block} @Rw{end} +@OrNL @Rw{for} @bnfNter{Name} @bnfter{=} exp @bnfter{,} exp @bnfopt{@bnfter{,} exp} + @Rw{do} block @Rw{end} +@OrNL @Rw{for} namelist @Rw{in} explist @Rw{do} block @Rw{end} +@OrNL @Rw{function} funcname funcbody +@OrNL @Rw{local} @Rw{function} @bnfNter{Name} funcbody +@OrNL @Rw{local} namelist @bnfopt{@bnfter{=} explist} +} + +@producname{retstat}@producbody{@Rw{return} + @bnfopt{explist} @bnfopt{@bnfter{;}}} + +@producname{label}@producbody{@bnfter{::} Name @bnfter{::}} + +@producname{funcname}@producbody{@bnfNter{Name} @bnfrep{@bnfter{.} @bnfNter{Name}} + @bnfopt{@bnfter{:} @bnfNter{Name}}} + +@producname{varlist}@producbody{var @bnfrep{@bnfter{,} var}} + +@producname{var}@producbody{ + @bnfNter{Name} +@Or prefixexp @bnfter{[} exp @bnfter{]} +@Or prefixexp @bnfter{.} @bnfNter{Name} +} + +@producname{namelist}@producbody{@bnfNter{Name} @bnfrep{@bnfter{,} @bnfNter{Name}}} + + +@producname{explist}@producbody{exp @bnfrep{@bnfter{,} exp}} + +@producname{exp}@producbody{ + @Rw{nil} +@Or @Rw{false} +@Or @Rw{true} +@Or @bnfNter{Numeral} +@Or @bnfNter{LiteralString} +@Or @bnfter{...} +@Or functiondef +@OrNL prefixexp +@Or tableconstructor +@Or exp binop exp +@Or unop exp +} + +@producname{prefixexp}@producbody{var @Or functioncall @Or @bnfter{(} exp @bnfter{)}} + +@producname{functioncall}@producbody{ + prefixexp args +@Or prefixexp @bnfter{:} @bnfNter{Name} args +} + +@producname{args}@producbody{ + @bnfter{(} @bnfopt{explist} @bnfter{)} +@Or tableconstructor +@Or @bnfNter{LiteralString} +} + +@producname{functiondef}@producbody{@Rw{function} funcbody} + +@producname{funcbody}@producbody{@bnfter{(} @bnfopt{parlist} @bnfter{)} block @Rw{end}} + +@producname{parlist}@producbody{namelist @bnfopt{@bnfter{,} @bnfter{...}} + @Or @bnfter{...}} + +@producname{tableconstructor}@producbody{@bnfter{@Open} @bnfopt{fieldlist} @bnfter{@Close}} + +@producname{fieldlist}@producbody{field @bnfrep{fieldsep field} @bnfopt{fieldsep}} + +@producname{field}@producbody{@bnfter{[} exp @bnfter{]} @bnfter{=} exp @Or @bnfNter{Name} @bnfter{=} exp @Or exp} + +@producname{fieldsep}@producbody{@bnfter{,} @Or @bnfter{;}} + +@producname{binop}@producbody{ + @bnfter{+} @Or @bnfter{-} @Or @bnfter{*} @Or @bnfter{/} @Or @bnfter{//} + @Or @bnfter{^} @Or @bnfter{%} + @OrNL + @bnfter{&} @Or @bnfter{~} @Or @bnfter{|} @Or @bnfter{>>} @Or @bnfter{<<} + @Or @bnfter{..} + @OrNL + @bnfter{<} @Or @bnfter{<=} @Or @bnfter{>} @Or @bnfter{>=} + @Or @bnfter{==} @Or @bnfter{~=} + @OrNL + @Rw{and} @Or @Rw{or}} + +@producname{unop}@producbody{@bnfter{-} @Or @Rw{not} @Or @bnfter{#} @Or + @bnfter{~}} + +} + +} + +@C{]===============================================================} + +} +@C{)]-------------------------------------------------------------------------} diff --git a/testes/all.lua b/testes/all.lua new file mode 100755 index 0000000000..cfe2160397 --- /dev/null +++ b/testes/all.lua @@ -0,0 +1,294 @@ +#!../lua +-- $Id: all.lua,v 1.100 2018/03/09 14:23:48 roberto Exp $ +-- See Copyright Notice at the end of this file + + +local version = "Lua 5.4" +if _VERSION ~= version then + io.stderr:write("\nThis test suite is for ", version, ", not for ", _VERSION, + "\nExiting tests\n") + return +end + + +_G.ARG = arg -- save arg for other tests + + +-- next variables control the execution of some tests +-- true means no test (so an undefined variable does not skip a test) +-- defaults are for Linux; test everything. +-- Make true to avoid long or memory consuming tests +_soft = rawget(_G, "_soft") or false +-- Make true to avoid non-portable tests +_port = rawget(_G, "_port") or false +-- Make true to avoid messages about tests not performed +_nomsg = rawget(_G, "_nomsg") or false + + +local usertests = rawget(_G, "_U") + +if usertests then + -- tests for sissies ;) Avoid problems + _soft = true + _port = true + _nomsg = true +end + +-- tests should require debug when needed +debug = nil + +require"bwcoercion" + + +if usertests then + T = nil -- no "internal" tests for user tests +else + T = rawget(_G, "T") -- avoid problems with 'strict' module +end + +math.randomseed(0) + +--[=[ + example of a long [comment], + [[spanning several [lines]]] + +]=] + +print("current path:\n****" .. package.path .. "****\n") + + +local initclock = os.clock() +local lastclock = initclock +local walltime = os.time() + +local collectgarbage = collectgarbage + +do -- ( + +-- track messages for tests not performed +local msgs = {} +function Message (m) + if not _nomsg then + print(m) + msgs[#msgs+1] = string.sub(m, 3, -3) + end +end + +assert(os.setlocale"C") + +local T,print,format,write,assert,type,unpack,floor = + T,print,string.format,io.write,assert,type,table.unpack,math.floor + +-- use K for 1000 and M for 1000000 (not 2^10 -- 2^20) +local function F (m) + local function round (m) + m = m + 0.04999 + return format("%.1f", m) -- keep one decimal digit + end + if m < 1000 then return m + else + m = m / 1000 + if m < 1000 then return round(m).."K" + else + return round(m/1000).."M" + end + end +end + +local showmem +if not T then + local max = 0 + showmem = function () + local m = collectgarbage("count") * 1024 + max = (m > max) and m or max + print(format(" ---- total memory: %s, max memory: %s ----\n", + F(m), F(max))) + end +else + showmem = function () + T.checkmemory() + local total, numblocks, maxmem = T.totalmem() + local count = collectgarbage("count") + print(format( + "\n ---- total memory: %s (%.0fK), max use: %s, blocks: %d\n", + F(total), count, F(maxmem), numblocks)) + print(format("\t(strings: %d, tables: %d, functions: %d, ".. + "\n\tudata: %d, threads: %d)", + T.totalmem"string", T.totalmem"table", T.totalmem"function", + T.totalmem"userdata", T.totalmem"thread")) + end +end + + +-- +-- redefine dofile to run files through dump/undump +-- +local function report (n) print("\n***** FILE '"..n.."'*****") end +local olddofile = dofile +local dofile = function (n, strip) + showmem() + local c = os.clock() + print(string.format("time: %g (+%g)", c - initclock, c - lastclock)) + lastclock = c + report(n) + local f = assert(loadfile(n)) + local b = string.dump(f, strip) + f = assert(load(b)) + return f() +end + +dofile('main.lua') + +do + local next, setmetatable, stderr = next, setmetatable, io.stderr + -- track collections + local mt = {} + -- each time a table is collected, remark it for finalization + -- on next cycle + mt.__gc = function (o) + stderr:write'.' -- mark progress + local n = setmetatable(o, mt) -- remark it + end + local n = setmetatable({}, mt) -- create object +end + +report"gc.lua" +local f = assert(loadfile('gc.lua')) +f() + +dofile('db.lua') +assert(dofile('calls.lua') == deep and deep) +olddofile('strings.lua') +olddofile('literals.lua') +dofile('tpack.lua') +assert(dofile('attrib.lua') == 27) + +assert(dofile('locals.lua') == 5) +dofile('constructs.lua') +dofile('code.lua', true) +if not _G._soft then + report('big.lua') + local f = coroutine.wrap(assert(loadfile('big.lua'))) + assert(f() == 'b') + assert(f() == 'a') +end +dofile('nextvar.lua') +dofile('pm.lua') +dofile('utf8.lua') +dofile('api.lua') +assert(dofile('events.lua') == 12) +dofile('vararg.lua') +dofile('closure.lua') +dofile('coroutine.lua') +dofile('goto.lua', true) +dofile('errors.lua') +dofile('math.lua') +dofile('sort.lua', true) +dofile('bitwise.lua') +assert(dofile('verybig.lua', true) == 10); collectgarbage() +dofile('files.lua') + +if #msgs > 0 then + print("\ntests not performed:") + for i=1,#msgs do + print(msgs[i]) + end + print() +end + +-- no test module should define 'debug' +assert(debug == nil) + +local debug = require "debug" + +print(string.format("%d-bit integers, %d-bit floats", + string.packsize("j") * 8, string.packsize("n") * 8)) + +debug.sethook(function (a) assert(type(a) == 'string') end, "cr") + +-- to survive outside block +_G.showmem = showmem + +end --) + +local _G, showmem, print, format, clock, time, difftime, assert, open = + _G, showmem, print, string.format, os.clock, os.time, os.difftime, + assert, io.open + +-- file with time of last performed test +local fname = T and "time-debug.txt" or "time.txt" +local lasttime + +if not usertests then + -- open file with time of last performed test + local f = io.open(fname) + if f then + lasttime = assert(tonumber(f:read'a')) + f:close(); + else -- no such file; assume it is recording time for first time + lasttime = nil + end +end + +-- erase (almost) all globals +print('cleaning all!!!!') +for n in pairs(_G) do + if not ({___Glob = 1, tostring = 1})[n] then + _G[n] = undef + end +end + + +collectgarbage() +collectgarbage() +collectgarbage() +collectgarbage() +collectgarbage() +collectgarbage();showmem() + +local clocktime = clock() - initclock +walltime = difftime(time(), walltime) + +print(format("\n\ntotal time: %.2fs (wall time: %gs)\n", clocktime, walltime)) + +if not usertests then + lasttime = lasttime or clocktime -- if no last time, ignore difference + -- check whether current test time differs more than 5% from last time + local diff = (clocktime - lasttime) / lasttime + local tolerance = 0.05 -- 5% + if (diff >= tolerance or diff <= -tolerance) then + print(format("WARNING: time difference from previous test: %+.1f%%", + diff * 100)) + end + assert(open(fname, "w")):write(clocktime):close() +end + +print("final OK !!!") + + + +--[[ +***************************************************************************** +* Copyright (C) 1994-2016 Lua.org, PUC-Rio. +* +* Permission is hereby granted, free of charge, to any person obtaining +* a copy of this software and associated documentation files (the +* "Software"), to deal in the Software without restriction, including +* without limitation the rights to use, copy, modify, merge, publish, +* distribute, sublicense, and/or sell copies of the Software, and to +* permit persons to whom the Software is furnished to do so, subject to +* the following conditions: +* +* The above copyright notice and this permission notice shall be +* included in all copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +***************************************************************************** +]] + diff --git a/testes/api.lua b/testes/api.lua new file mode 100644 index 0000000000..836a6070a3 --- /dev/null +++ b/testes/api.lua @@ -0,0 +1,1264 @@ +-- $Id: api.lua,v 1.155 2018/03/09 14:23:48 roberto Exp $ +-- See Copyright Notice in file all.lua + +if T==nil then + (Message or print)('\n >>> testC not active: skipping API tests <<<\n') + return +end + +local debug = require "debug" + +local pack = table.pack + + +function tcheck (t1, t2) + assert(t1.n == (t2.n or #t2) + 1) + for i = 2, t1.n do assert(t1[i] == t2[i - 1]) end +end + + +local function checkerr (msg, f, ...) + local stat, err = pcall(f, ...) + assert(not stat and string.find(err, msg)) +end + + +print('testing C API') + +a = T.testC("pushvalue R; return 1") +assert(a == debug.getregistry()) + + +-- absindex +assert(T.testC("settop 10; absindex -1; return 1") == 10) +assert(T.testC("settop 5; absindex -5; return 1") == 1) +assert(T.testC("settop 10; absindex 1; return 1") == 1) +assert(T.testC("settop 10; absindex R; return 1") < -10) + +-- testing alignment +a = T.d2s(12458954321123.0) +assert(a == string.pack("d", 12458954321123.0)) +assert(T.s2d(a) == 12458954321123.0) + +a,b,c = T.testC("pushnum 1; pushnum 2; pushnum 3; return 2") +assert(a == 2 and b == 3 and not c) + +f = T.makeCfunc("pushnum 1; pushnum 2; pushnum 3; return 2") +a,b,c = f() +assert(a == 2 and b == 3 and not c) + +-- test that all trues are equal +a,b,c = T.testC("pushbool 1; pushbool 2; pushbool 0; return 3") +assert(a == b and a == true and c == false) +a,b,c = T.testC"pushbool 0; pushbool 10; pushnil;\ + tobool -3; tobool -3; tobool -3; return 3" +assert(a==false and b==true and c==false) + + +a,b,c = T.testC("gettop; return 2", 10, 20, 30, 40) +assert(a == 40 and b == 5 and not c) + +t = pack(T.testC("settop 5; return *", 2, 3)) +tcheck(t, {n=4,2,3}) + +t = pack(T.testC("settop 0; settop 15; return 10", 3, 1, 23)) +assert(t.n == 10 and t[1] == nil and t[10] == nil) + +t = pack(T.testC("remove -2; return *", 2, 3, 4)) +tcheck(t, {n=2,2,4}) + +t = pack(T.testC("insert -1; return *", 2, 3)) +tcheck(t, {n=2,2,3}) + +t = pack(T.testC("insert 3; return *", 2, 3, 4, 5)) +tcheck(t, {n=4,2,5,3,4}) + +t = pack(T.testC("replace 2; return *", 2, 3, 4, 5)) +tcheck(t, {n=3,5,3,4}) + +t = pack(T.testC("replace -2; return *", 2, 3, 4, 5)) +tcheck(t, {n=3,2,3,5}) + +t = pack(T.testC("remove 3; return *", 2, 3, 4, 5)) +tcheck(t, {n=3,2,4,5}) + +t = pack(T.testC("copy 3 4; return *", 2, 3, 4, 5)) +tcheck(t, {n=4,2,3,3,5}) + +t = pack(T.testC("copy -3 -1; return *", 2, 3, 4, 5)) +tcheck(t, {n=4,2,3,4,3}) + +do -- testing 'rotate' + local t = {10, 20, 30, 40, 50, 60} + for i = -6, 6 do + local s = string.format("rotate 2 %d; return 7", i) + local t1 = pack(T.testC(s, 10, 20, 30, 40, 50, 60)) + tcheck(t1, t) + table.insert(t, 1, table.remove(t)) + end + + t = pack(T.testC("rotate -2 1; return *", 10, 20, 30, 40)) + tcheck(t, {10, 20, 40, 30}) + t = pack(T.testC("rotate -2 -1; return *", 10, 20, 30, 40)) + tcheck(t, {10, 20, 40, 30}) + + -- some corner cases + t = pack(T.testC("rotate -1 0; return *", 10, 20, 30, 40)) + tcheck(t, {10, 20, 30, 40}) + t = pack(T.testC("rotate -1 1; return *", 10, 20, 30, 40)) + tcheck(t, {10, 20, 30, 40}) + t = pack(T.testC("rotate 5 -1; return *", 10, 20, 30, 40)) + tcheck(t, {10, 20, 30, 40}) +end + +-- testing message handlers +do + local f = T.makeCfunc[[ + getglobal error + pushstring bola + pcall 1 1 1 # call 'error' with given handler + pushstatus + return 2 # return error message and status + ]] + + local msg, st = f(string.upper) -- function handler + assert(st == "ERRRUN" and msg == "BOLA") + local msg, st = f(string.len) -- function handler + assert(st == "ERRRUN" and msg == 4) + +end + +t = pack(T.testC("insert 3; pushvalue 3; remove 3; pushvalue 2; remove 2; \ + insert 2; pushvalue 1; remove 1; insert 1; \ + insert -2; pushvalue -2; remove -3; return *", + 2, 3, 4, 5, 10, 40, 90)) +tcheck(t, {n=7,2,3,4,5,10,40,90}) + +t = pack(T.testC("concat 5; return *", "alo", 2, 3, "joao", 12)) +tcheck(t, {n=1,"alo23joao12"}) + +-- testing MULTRET +t = pack(T.testC("call 2,-1; return *", + function (a,b) return 1,2,3,4,a,b end, "alo", "joao")) +tcheck(t, {n=6,1,2,3,4,"alo", "joao"}) + +do -- test returning more results than fit in the caller stack + local a = {} + for i=1,1000 do a[i] = true end; a[999] = 10 + local b = T.testC([[pcall 1 -1 0; pop 1; tostring -1; return 1]], + table.unpack, a) + assert(b == "10") +end + + +-- testing globals +_G.a = 14; _G.b = "a31" +local a = {T.testC[[ + getglobal a; + getglobal b; + getglobal b; + setglobal a; + return * +]]} +assert(a[2] == 14 and a[3] == "a31" and a[4] == nil and _G.a == "a31") + + +-- testing arith +assert(T.testC("pushnum 10; pushnum 20; arith /; return 1") == 0.5) +assert(T.testC("pushnum 10; pushnum 20; arith -; return 1") == -10) +assert(T.testC("pushnum 10; pushnum -20; arith *; return 1") == -200) +assert(T.testC("pushnum 10; pushnum 3; arith ^; return 1") == 1000) +assert(T.testC("pushnum 10; pushstring 20; arith /; return 1") == 0.5) +assert(T.testC("pushstring 10; pushnum 20; arith -; return 1") == -10) +assert(T.testC("pushstring 10; pushstring -20; arith *; return 1") == -200) +assert(T.testC("pushstring 10; pushstring 3; arith ^; return 1") == 1000) +assert(T.testC("arith /; return 1", 2, 0) == 10.0/0) +a = T.testC("pushnum 10; pushint 3; arith \\; return 1") +assert(a == 3.0 and math.type(a) == "float") +a = T.testC("pushint 10; pushint 3; arith \\; return 1") +assert(a == 3 and math.type(a) == "integer") +a = assert(T.testC("pushint 10; pushint 3; arith +; return 1")) +assert(a == 13 and math.type(a) == "integer") +a = assert(T.testC("pushnum 10; pushint 3; arith +; return 1")) +assert(a == 13 and math.type(a) == "float") +a,b,c = T.testC([[pushnum 1; + pushstring 10; arith _; + pushstring 5; return 3]]) +assert(a == 1 and b == -10 and c == "5") +mt = {__add = function (a,b) return setmetatable({a[1] + b[1]}, mt) end, + __mod = function (a,b) return setmetatable({a[1] % b[1]}, mt) end, + __unm = function (a) return setmetatable({a[1]* 2}, mt) end} +a,b,c = setmetatable({4}, mt), + setmetatable({8}, mt), + setmetatable({-3}, mt) +x,y,z = T.testC("arith +; return 2", 10, a, b) +assert(x == 10 and y[1] == 12 and z == nil) +assert(T.testC("arith %; return 1", a, c)[1] == 4%-3) +assert(T.testC("arith _; arith +; arith %; return 1", b, a, c)[1] == + 8 % (4 + (-3)*2)) + +-- errors in arithmetic +checkerr("divide by zero", T.testC, "arith \\", 10, 0) +checkerr("%%0", T.testC, "arith %", 10, 0) + + +-- testing lessthan and lessequal +assert(T.testC("compare LT 2 5, return 1", 3, 2, 2, 4, 2, 2)) +assert(T.testC("compare LE 2 5, return 1", 3, 2, 2, 4, 2, 2)) +assert(not T.testC("compare LT 3 4, return 1", 3, 2, 2, 4, 2, 2)) +assert(T.testC("compare LE 3 4, return 1", 3, 2, 2, 4, 2, 2)) +assert(T.testC("compare LT 5 2, return 1", 4, 2, 2, 3, 2, 2)) +assert(not T.testC("compare LT 2 -3, return 1", "4", "2", "2", "3", "2", "2")) +assert(not T.testC("compare LT -3 2, return 1", "3", "2", "2", "4", "2", "2")) + +-- non-valid indices produce false +assert(not T.testC("compare LT 1 4, return 1")) +assert(not T.testC("compare LE 9 1, return 1")) +assert(not T.testC("compare EQ 9 9, return 1")) + +local b = {__lt = function (a,b) return a[1] < b[1] end} +local a1,a3,a4 = setmetatable({1}, b), + setmetatable({3}, b), + setmetatable({4}, b) +assert(T.testC("compare LT 2 5, return 1", a3, 2, 2, a4, 2, 2)) +assert(T.testC("compare LE 2 5, return 1", a3, 2, 2, a4, 2, 2)) +assert(T.testC("compare LT 5 -6, return 1", a4, 2, 2, a3, 2, 2)) +a,b = T.testC("compare LT 5 -6, return 2", a1, 2, 2, a3, 2, 20) +assert(a == 20 and b == false) +a,b = T.testC("compare LE 5 -6, return 2", a1, 2, 2, a3, 2, 20) +assert(a == 20 and b == false) +a,b = T.testC("compare LE 5 -6, return 2", a1, 2, 2, a1, 2, 20) +assert(a == 20 and b == true) + +-- testing length +local t = setmetatable({x = 20}, {__len = function (t) return t.x end}) +a,b,c = T.testC([[ + len 2; + Llen 2; + objsize 2; + return 3 +]], t) +assert(a == 20 and b == 20 and c == 0) + +t.x = "234"; t[1] = 20 +a,b,c = T.testC([[ + len 2; + Llen 2; + objsize 2; + return 3 +]], t) +assert(a == "234" and b == 234 and c == 1) + +t.x = print; t[1] = 20 +a,c = T.testC([[ + len 2; + objsize 2; + return 2 +]], t) +assert(a == print and c == 1) + + +-- testing __concat + +a = setmetatable({x="u"}, {__concat = function (a,b) return a.x..'.'..b.x end}) +x,y = T.testC([[ + pushnum 5 + pushvalue 2; + pushvalue 2; + concat 2; + pushvalue -2; + return 2; +]], a, a) +assert(x == a..a and y == 5) + +-- concat with 0 elements +assert(T.testC("concat 0; return 1") == "") + +-- concat with 1 element +assert(T.testC("concat 1; return 1", "xuxu") == "xuxu") + + + +-- testing lua_is + +function B(x) return x and 1 or 0 end + +function count (x, n) + n = n or 2 + local prog = [[ + isnumber %d; + isstring %d; + isfunction %d; + iscfunction %d; + istable %d; + isuserdata %d; + isnil %d; + isnull %d; + return 8 + ]] + prog = string.format(prog, n, n, n, n, n, n, n, n) + local a,b,c,d,e,f,g,h = T.testC(prog, x) + return B(a)+B(b)+B(c)+B(d)+B(e)+B(f)+B(g)+(100*B(h)) +end + +assert(count(3) == 2) +assert(count('alo') == 1) +assert(count('32') == 2) +assert(count({}) == 1) +assert(count(print) == 2) +assert(count(function () end) == 1) +assert(count(nil) == 1) +assert(count(io.stdin) == 1) +assert(count(nil, 15) == 100) + + +-- testing lua_to... + +function to (s, x, n) + n = n or 2 + return T.testC(string.format("%s %d; return 1", s, n), x) +end + +local hfunc = string.gmatch("", "") -- a "heavy C function" (with upvalues) +assert(debug.getupvalue(hfunc, 1)) +assert(to("tostring", {}) == nil) +assert(to("tostring", "alo") == "alo") +assert(to("tostring", 12) == "12") +assert(to("tostring", 12, 3) == nil) +assert(to("objsize", {}) == 0) +assert(to("objsize", {1,2,3}) == 3) +assert(to("objsize", "alo\0\0a") == 6) +assert(to("objsize", T.newuserdata(0)) == 0) +assert(to("objsize", T.newuserdata(101)) == 101) +assert(to("objsize", 124) == 0) +assert(to("objsize", true) == 0) +assert(to("tonumber", {}) == 0) +assert(to("tonumber", "12") == 12) +assert(to("tonumber", "s2") == 0) +assert(to("tonumber", 1, 20) == 0) +assert(to("topointer", 10) == 0) +assert(to("topointer", true) == 0) +assert(to("topointer", T.pushuserdata(20)) == 20) +assert(to("topointer", io.read) ~= 0) -- light C function +assert(to("topointer", hfunc) ~= 0) -- "heavy" C function +assert(to("topointer", function () end) ~= 0) -- Lua function +assert(to("topointer", io.stdin) ~= 0) -- full userdata +assert(to("func2num", 20) == 0) +assert(to("func2num", T.pushuserdata(10)) == 0) +assert(to("func2num", io.read) ~= 0) -- light C function +assert(to("func2num", hfunc) ~= 0) -- "heavy" C function (with upvalue) +a = to("tocfunction", math.deg) +assert(a(3) == math.deg(3) and a == math.deg) + + +print("testing panic function") +do + -- trivial error + assert(T.checkpanic("pushstring hi; error") == "hi") + + -- using the stack inside panic + assert(T.checkpanic("pushstring hi; error;", + [[checkstack 5 XX + pushstring ' alo' + pushstring ' mundo' + concat 3]]) == "hi alo mundo") + + -- "argerror" without frames + assert(T.checkpanic("loadstring 4") == + "bad argument #4 (string expected, got no value)") + + + -- memory error + T.totalmem(T.totalmem()+10000) -- set low memory limit (+10k) + assert(T.checkpanic("newuserdata 20000") == "not enough memory") + T.totalmem(0) -- restore high limit + + -- stack error + if not _soft then + local msg = T.checkpanic[[ + pushstring "function f() f() end" + loadstring -1; call 0 0 + getglobal f; call 0 0 + ]] + assert(string.find(msg, "stack overflow")) + end + +end + +-- testing deep C stack +if not _soft then + print("testing stack overflow") + collectgarbage("stop") + checkerr("XXXX", T.testC, "checkstack 1000023 XXXX") -- too deep + -- too deep (with no message) + checkerr("^stack overflow$", T.testC, "checkstack 1000023 ''") + local s = string.rep("pushnil;checkstack 1 XX;", 1000000) + checkerr("overflow", T.testC, s) + collectgarbage("restart") + print'+' +end + +local lim = _soft and 500 or 12000 +local prog = {"checkstack " .. (lim * 2 + 100) .. "msg", "newtable"} +for i = 1,lim do + prog[#prog + 1] = "pushnum " .. i + prog[#prog + 1] = "pushnum " .. i * 10 +end + +prog[#prog + 1] = "rawgeti R 2" -- get global table in registry +prog[#prog + 1] = "insert " .. -(2*lim + 2) + +for i = 1,lim do + prog[#prog + 1] = "settable " .. -(2*(lim - i + 1) + 1) +end + +prog[#prog + 1] = "return 2" + +prog = table.concat(prog, ";") +local g, t = T.testC(prog) +assert(g == _G) +for i = 1,lim do assert(t[i] == i*10); t[i] = undef end +assert(next(t) == nil) +prog, g, t = nil + +-- testing errors + +a = T.testC([[ + loadstring 2; pcall 0 1 0; + pushvalue 3; insert -2; pcall 1 1 0; + pcall 0 0 0; + return 1 +]], "x=150", function (a) assert(a==nil); return 3 end) + +assert(type(a) == 'string' and x == 150) + +function check3(p, ...) + local arg = {...} + assert(#arg == 3) + assert(string.find(arg[3], p)) +end +check3(":1:", T.testC("loadstring 2; return *", "x=")) +check3("%.", T.testC("loadfile 2; return *", ".")) +check3("xxxx", T.testC("loadfile 2; return *", "xxxx")) + +-- test errors in non protected threads +function checkerrnopro (code, msg) + local th = coroutine.create(function () end) -- create new thread + local stt, err = pcall(T.testC, th, code) -- run code there + assert(not stt and string.find(err, msg)) +end + +if not _soft then + checkerrnopro("pushnum 3; call 0 0", "attempt to call") + print"testing stack overflow in unprotected thread" + function f () f() end + checkerrnopro("getglobal 'f'; call 0 0;", "stack overflow") +end +print"+" + + +-- testing table access + +do -- getp/setp + local a = {} + T.testC("rawsetp 2 1", a, 20) + assert(a[T.pushuserdata(1)] == 20) + assert(T.testC("rawgetp 2 1; return 1", a) == 20) +end + +a = {x=0, y=12} +x, y = T.testC("gettable 2; pushvalue 4; gettable 2; return 2", + a, 3, "y", 4, "x") +assert(x == 0 and y == 12) +T.testC("settable -5", a, 3, 4, "x", 15) +assert(a.x == 15) +a[a] = print +x = T.testC("gettable 2; return 1", a) -- table and key are the same object! +assert(x == print) +T.testC("settable 2", a, "x") -- table and key are the same object! +assert(a[a] == "x") + +b = setmetatable({p = a}, {}) +getmetatable(b).__index = function (t, i) return t.p[i] end +k, x = T.testC("gettable 3, return 2", 4, b, 20, 35, "x") +assert(x == 15 and k == 35) +k = T.testC("getfield 2 y, return 1", b) +assert(k == 12) +getmetatable(b).__index = function (t, i) return a[i] end +getmetatable(b).__newindex = function (t, i,v ) a[i] = v end +y = T.testC("insert 2; gettable -5; return 1", 2, 3, 4, "y", b) +assert(y == 12) +k = T.testC("settable -5, return 1", b, 3, 4, "x", 16) +assert(a.x == 16 and k == 4) +a[b] = 'xuxu' +y = T.testC("gettable 2, return 1", b) +assert(y == 'xuxu') +T.testC("settable 2", b, 19) +assert(a[b] == 19) + +-- +do -- testing getfield/setfield with long keys + local t = {_012345678901234567890123456789012345678901234567890123456789 = 32} + local a = T.testC([[ + getfield 2 _012345678901234567890123456789012345678901234567890123456789 + return 1 + ]], t) + assert(a == 32) + local a = T.testC([[ + pushnum 33 + setglobal _012345678901234567890123456789012345678901234567890123456789 + ]]) + assert(_012345678901234567890123456789012345678901234567890123456789 == 33) + _012345678901234567890123456789012345678901234567890123456789 = nil +end + +-- testing next +a = {} +t = pack(T.testC("next; return *", a, nil)) +tcheck(t, {n=1,a}) +a = {a=3} +t = pack(T.testC("next; return *", a, nil)) +tcheck(t, {n=3,a,'a',3}) +t = pack(T.testC("next; pop 1; next; return *", a, nil)) +tcheck(t, {n=1,a}) + + + +-- testing upvalues + +do + local A = T.testC[[ pushnum 10; pushnum 20; pushcclosure 2; return 1]] + t, b, c = A([[pushvalue U0; pushvalue U1; pushvalue U2; return 3]]) + assert(b == 10 and c == 20 and type(t) == 'table') + a, b = A([[tostring U3; tonumber U4; return 2]]) + assert(a == nil and b == 0) + A([[pushnum 100; pushnum 200; replace U2; replace U1]]) + b, c = A([[pushvalue U1; pushvalue U2; return 2]]) + assert(b == 100 and c == 200) + A([[replace U2; replace U1]], {x=1}, {x=2}) + b, c = A([[pushvalue U1; pushvalue U2; return 2]]) + assert(b.x == 1 and c.x == 2) + T.checkmemory() +end + + +-- testing absent upvalues from C-function pointers +assert(T.testC[[isnull U1; return 1]] == true) +assert(T.testC[[isnull U100; return 1]] == true) +assert(T.testC[[pushvalue U1; return 1]] == nil) + +local f = T.testC[[ pushnum 10; pushnum 20; pushcclosure 2; return 1]] +assert(T.upvalue(f, 1) == 10 and + T.upvalue(f, 2) == 20 and + T.upvalue(f, 3) == nil) +T.upvalue(f, 2, "xuxu") +assert(T.upvalue(f, 2) == "xuxu") + + +-- large closures +do + local A = "checkstack 300 msg;" .. + string.rep("pushnum 10;", 255) .. + "pushcclosure 255; return 1" + A = T.testC(A) + for i=1,255 do + assert(A(("pushvalue U%d; return 1"):format(i)) == 10) + end + assert(A("isnull U256; return 1")) + assert(not A("isnil U256; return 1")) +end + + + +-- testing get/setuservalue +-- bug in 5.1.2 +checkerr("got number", debug.setuservalue, 3, {}) +checkerr("got nil", debug.setuservalue, nil, {}) +checkerr("got light userdata", debug.setuservalue, T.pushuserdata(1), {}) + +-- testing multiple user values +local b = T.newuserdata(0, 10) +for i = 1, 10 do + local v, p = debug.getuservalue(b, i) + assert(v == nil and p) +end +do -- indices out of range + local v, p = debug.getuservalue(b, -2) + assert(v == nil and not p) + local v, p = debug.getuservalue(b, 11) + assert(v == nil and not p) +end +local t = {true, false, 4.56, print, {}, b, "XYZ"} +for k, v in ipairs(t) do + debug.setuservalue(b, v, k) +end +for k, v in ipairs(t) do + local v1, p = debug.getuservalue(b, k) + assert(v1 == v and p) +end + +assert(debug.getuservalue(4) == nil) + +debug.setuservalue(b, function () return 10 end, 10) +collectgarbage() -- function should not be collected +assert(debug.getuservalue(b, 10)() == 10) + +debug.setuservalue(b, 134) +collectgarbage() -- number should not be a problem for collector +assert(debug.getuservalue(b) == 134) + + +-- test barrier for uservalues +do + local oldmode = collectgarbage("incremental") + T.gcstate("atomic") + assert(T.gccolor(b) == "black") + debug.setuservalue(b, {x = 100}) + T.gcstate("pause") -- complete collection + assert(debug.getuservalue(b).x == 100) -- uvalue should be there + collectgarbage(oldmode) +end + +-- long chain of userdata +for i = 1, 1000 do + local bb = T.newuserdata(0, 1) + debug.setuservalue(bb, b) + b = bb +end +collectgarbage() -- nothing should not be collected +for i = 1, 1000 do + b = debug.getuservalue(b) +end +assert(debug.getuservalue(b).x == 100) +b = nil + + +-- testing locks (refs) + +-- reuse of references +local i = T.ref{} +T.unref(i) +assert(T.ref{} == i) + +Arr = {} +Lim = 100 +for i=1,Lim do -- lock many objects + Arr[i] = T.ref({}) +end + +assert(T.ref(nil) == -1 and T.getref(-1) == nil) +T.unref(-1); T.unref(-1) + +for i=1,Lim do -- unlock all them + T.unref(Arr[i]) +end + +function printlocks () + local f = T.makeCfunc("gettable R; return 1") + local n = f("n") + print("n", n) + for i=0,n do + print(i, f(i)) + end +end + + +for i=1,Lim do -- lock many objects + Arr[i] = T.ref({}) +end + +for i=1,Lim,2 do -- unlock half of them + T.unref(Arr[i]) +end + +assert(type(T.getref(Arr[2])) == 'table') + + +assert(T.getref(-1) == nil) + + +a = T.ref({}) + +collectgarbage() + +assert(type(T.getref(a)) == 'table') + + +-- colect in cl the `val' of all collected userdata +tt = {} +cl = {n=0} +A = nil; B = nil +local F +F = function (x) + local udval = T.udataval(x) + table.insert(cl, udval) + local d = T.newuserdata(100) -- create garbage + d = nil + assert(debug.getmetatable(x).__gc == F) + assert(load("table.insert({}, {})"))() -- create more garbage + collectgarbage() -- force a GC during GC + assert(debug.getmetatable(x).__gc == F) -- previous GC did not mess this? + local dummy = {} -- create more garbage during GC + if A ~= nil then + assert(type(A) == "userdata") + assert(T.udataval(A) == B) + debug.getmetatable(A) -- just acess it + end + A = x -- ressucita userdata + B = udval + return 1,2,3 +end +tt.__gc = F + +-- test whether udate collection frees memory in the right time +do + collectgarbage(); + collectgarbage(); + local x = collectgarbage("count"); + local a = T.newuserdata(5001) + assert(T.testC("objsize 2; return 1", a) == 5001) + assert(collectgarbage("count") >= x+4) + a = nil + collectgarbage(); + assert(collectgarbage("count") <= x+1) + -- udata without finalizer + x = collectgarbage("count") + collectgarbage("stop") + for i=1,1000 do T.newuserdata(0) end + assert(collectgarbage("count") > x+10) + collectgarbage() + assert(collectgarbage("count") <= x+1) + -- udata with finalizer + collectgarbage() + x = collectgarbage("count") + collectgarbage("stop") + a = {__gc = function () end} + for i=1,1000 do debug.setmetatable(T.newuserdata(0), a) end + assert(collectgarbage("count") >= x+10) + collectgarbage() -- this collection only calls TM, without freeing memory + assert(collectgarbage("count") >= x+10) + collectgarbage() -- now frees memory + assert(collectgarbage("count") <= x+1) + collectgarbage("restart") +end + + +collectgarbage("stop") + +-- create 3 userdatas with tag `tt' +a = T.newuserdata(0); debug.setmetatable(a, tt); na = T.udataval(a) +b = T.newuserdata(0); debug.setmetatable(b, tt); nb = T.udataval(b) +c = T.newuserdata(0); debug.setmetatable(c, tt); nc = T.udataval(c) + +-- create userdata without meta table +x = T.newuserdata(4) +y = T.newuserdata(0) + +checkerr("FILE%* expected, got userdata", io.input, a) +checkerr("FILE%* expected, got userdata", io.input, x) + +assert(debug.getmetatable(x) == nil and debug.getmetatable(y) == nil) + +d=T.ref(a); +e=T.ref(b); +f=T.ref(c); +t = {T.getref(d), T.getref(e), T.getref(f)} +assert(t[1] == a and t[2] == b and t[3] == c) + +t=nil; a=nil; c=nil; +T.unref(e); T.unref(f) + +collectgarbage() + +-- check that unref objects have been collected +assert(#cl == 1 and cl[1] == nc) + +x = T.getref(d) +assert(type(x) == 'userdata' and debug.getmetatable(x) == tt) +x =nil +tt.b = b -- create cycle +tt=nil -- frees tt for GC +A = nil +b = nil +T.unref(d); +n5 = T.newuserdata(0) +debug.setmetatable(n5, {__gc=F}) +n5 = T.udataval(n5) +collectgarbage() +assert(#cl == 4) +-- check order of collection +assert(cl[2] == n5 and cl[3] == nb and cl[4] == na) + +collectgarbage"restart" + + +a, na = {}, {} +for i=30,1,-1 do + a[i] = T.newuserdata(0) + debug.setmetatable(a[i], {__gc=F}) + na[i] = T.udataval(a[i]) +end +cl = {} +a = nil; collectgarbage() +assert(#cl == 30) +for i=1,30 do assert(cl[i] == na[i]) end +na = nil + + +for i=2,Lim,2 do -- unlock the other half + T.unref(Arr[i]) +end + +x = T.newuserdata(41); debug.setmetatable(x, {__gc=F}) +assert(T.testC("objsize 2; return 1", x) == 41) +cl = {} +a = {[x] = 1} +x = T.udataval(x) +collectgarbage() +-- old `x' cannot be collected (`a' still uses it) +assert(#cl == 0) +for n in pairs(a) do a[n] = undef end +collectgarbage() +assert(#cl == 1 and cl[1] == x) -- old `x' must be collected + +-- testing lua_equal +assert(T.testC("compare EQ 2 4; return 1", print, 1, print, 20)) +assert(T.testC("compare EQ 3 2; return 1", 'alo', "alo")) +assert(T.testC("compare EQ 2 3; return 1", nil, nil)) +assert(not T.testC("compare EQ 2 3; return 1", {}, {})) +assert(not T.testC("compare EQ 2 3; return 1")) +assert(not T.testC("compare EQ 2 3; return 1", 3)) + +-- testing lua_equal with fallbacks +do + local map = {} + local t = {__eq = function (a,b) return map[a] == map[b] end} + local function f(x) + local u = T.newuserdata(0) + debug.setmetatable(u, t) + map[u] = x + return u + end + assert(f(10) == f(10)) + assert(f(10) ~= f(11)) + assert(T.testC("compare EQ 2 3; return 1", f(10), f(10))) + assert(not T.testC("compare EQ 2 3; return 1", f(10), f(20))) + t.__eq = nil + assert(f(10) ~= f(10)) +end + +print'+' + + + +-- testing changing hooks during hooks +_G.t = {} +T.sethook([[ + # set a line hook after 3 count hooks + sethook 4 0 ' + getglobal t; + pushvalue -3; append -2 + pushvalue -2; append -2 + ']], "c", 3) +local a = 1 -- counting +a = 1 -- counting +a = 1 -- count hook (set line hook) +a = 1 -- line hook +a = 1 -- line hook +debug.sethook() +t = _G.t +assert(t[1] == "line") +line = t[2] +assert(t[3] == "line" and t[4] == line + 1) +assert(t[5] == "line" and t[6] == line + 2) +assert(t[7] == nil) + + +------------------------------------------------------------------------- +do -- testing errors during GC + collectgarbage("stop") + local a = {} + for i=1,20 do + a[i] = T.newuserdata(i) -- creates several udata + end + for i=1,20,2 do -- mark half of them to raise errors during GC + debug.setmetatable(a[i], {__gc = function (x) error("error inside gc") end}) + end + for i=2,20,2 do -- mark the other half to count and to create more garbage + debug.setmetatable(a[i], {__gc = function (x) load("A=A+1")() end}) + end + _G.A = 0 + a = 0 + while 1 do + local stat, msg = pcall(collectgarbage) + if stat then + break -- stop when no more errors + else + a = a + 1 + assert(string.find(msg, "__gc")) + end + end + assert(a == 10) -- number of errors + + assert(A == 10) -- number of normal collections + collectgarbage("restart") +end +------------------------------------------------------------------------- +-- test for userdata vals +do + local a = {}; local lim = 30 + for i=0,lim do a[i] = T.pushuserdata(i) end + for i=0,lim do assert(T.udataval(a[i]) == i) end + for i=0,lim do assert(T.pushuserdata(i) == a[i]) end + for i=0,lim do a[a[i]] = i end + for i=0,lim do a[T.pushuserdata(i)] = i end + assert(type(tostring(a[1])) == "string") +end + + +------------------------------------------------------------------------- +-- testing multiple states +T.closestate(T.newstate()); +L1 = T.newstate() +assert(L1) + +assert(T.doremote(L1, "X='a'; return 'a'") == 'a') + + +assert(#pack(T.doremote(L1, "function f () return 'alo', 3 end; f()")) == 0) + +a, b = T.doremote(L1, "return f()") +assert(a == 'alo' and b == '3') + +T.doremote(L1, "_ERRORMESSAGE = nil") +-- error: `sin' is not defined +a, _, b = T.doremote(L1, "return sin(1)") +assert(a == nil and b == 2) -- 2 == run-time error + +-- error: syntax error +a, b, c = T.doremote(L1, "return a+") +assert(a == nil and c == 3 and type(b) == "string") -- 3 == syntax error + +T.loadlib(L1) +a, b, c = T.doremote(L1, [[ + string = require'string' + a = require'_G'; assert(a == _G and require("_G") == a) + io = require'io'; assert(type(io.read) == "function") + assert(require("io") == io) + a = require'table'; assert(type(a.insert) == "function") + a = require'debug'; assert(type(a.getlocal) == "function") + a = require'math'; assert(type(a.sin) == "function") + return string.sub('okinama', 1, 2) +]]) +assert(a == "ok") + +T.closestate(L1); + + +L1 = T.newstate() +T.loadlib(L1) +T.doremote(L1, "a = {}") +T.testC(L1, [[getglobal "a"; pushstring "x"; pushint 1; + settable -3]]) +assert(T.doremote(L1, "return a.x") == "1") + +T.closestate(L1) + +L1 = nil + +print('+') + +------------------------------------------------------------------------- +-- testing memory limits +------------------------------------------------------------------------- +print("memory-allocation errors") + +checkerr("block too big", T.newuserdata, math.maxinteger) +collectgarbage() +local f = load"local a={}; for i=1,100000 do a[i]=i end" +T.alloccount(10) +checkerr("not enough memory", f) +T.alloccount() -- remove limit + +-- test memory errors; increase limit for number of allocations one +-- by one, so that we get memory errors in all allocations of a given +-- task, until there is enough allocations to complete the task without +-- errors. + +function testamem (s, f) + collectgarbage(); collectgarbage() + local M = 0 + local a,b = nil + while true do + T.alloccount(M) + a, b = pcall(f) + T.alloccount() -- remove limit + if a and b then break end -- stop when no more errors + if not a and not -- `real' error? + (string.find(b, "memory") or string.find(b, "overflow")) then + error(b, 0) -- propagate it + end + M = M + 1 -- increase allocation limit + end + print(string.format("limit for %s: %d allocations", s, M)) + return b +end + + +-- doing nothing +b = testamem("doing nothing", function () return 10 end) +assert(b == 10) + +-- testing memory errors when creating a new state + +b = testamem("state creation", T.newstate) +T.closestate(b); -- close new state + +testamem("empty-table creation", function () + return {} +end) + +testamem("string creation", function () + return "XXX" .. "YYY" +end) + +testamem("coroutine creation", function() + return coroutine.create(print) +end) + + +-- testing threads + +-- get main thread from registry (at index LUA_RIDX_MAINTHREAD == 1) +mt = T.testC("rawgeti R 1; return 1") +assert(type(mt) == "thread" and coroutine.running() == mt) + + + +function expand (n,s) + if n==0 then return "" end + local e = string.rep("=", n) + return string.format("T.doonnewstack([%s[ %s;\n collectgarbage(); %s]%s])\n", + e, s, expand(n-1,s), e) +end + +G=0; collectgarbage(); a =collectgarbage("count") +load(expand(20,"G=G+1"))() +assert(G==20); collectgarbage(); -- assert(gcinfo() <= a+1) + +testamem("running code on new thread", function () + return T.doonnewstack("x=1") == 0 -- try to create thread +end) + + +-- testing memory x compiler + +testamem("loadstring", function () + return load("x=1") -- try to do load a string +end) + + +local testprog = [[ +local function foo () return end +local t = {"x"} +a = "aaa" +for i = 1, #t do a=a..t[i] end +return true +]] + +-- testing memory x dofile +_G.a = nil +local t =os.tmpname() +local f = assert(io.open(t, "w")) +f:write(testprog) +f:close() +testamem("dofile", function () + local a = loadfile(t) + return a and a() +end) +assert(os.remove(t)) +assert(_G.a == "aaax") + + +-- other generic tests + +testamem("gsub", function () + local a, b = string.gsub("alo alo", "(a)", function (x) return x..'b' end) + return (a == 'ablo ablo') +end) + +testamem("dump/undump", function () + local a = load(testprog) + local b = a and string.dump(a) + a = b and load(b) + return a and a() +end) + +local t = os.tmpname() +testamem("file creation", function () + local f = assert(io.open(t, 'w')) + assert (not io.open"nomenaoexistente") + io.close(f); + return not loadfile'nomenaoexistente' +end) +assert(os.remove(t)) + +testamem("table creation", function () + local a, lim = {}, 10 + for i=1,lim do a[i] = i; a[i..'a'] = {} end + return (type(a[lim..'a']) == 'table' and a[lim] == lim) +end) + +testamem("constructors", function () + local a = {10, 20, 30, 40, 50; a=1, b=2, c=3, d=4, e=5} + return (type(a) == 'table' and a.e == 5) +end) + +local a = 1 +close = nil +testamem("closure creation", function () + function close (b) + return function (x) return b + x end + end + return (close(2)(4) == 6) +end) + +testamem("using coroutines", function () + local a = coroutine.wrap(function () + coroutine.yield(string.rep("a", 10)) + return {} + end) + assert(string.len(a()) == 10) + return a() +end) + +do -- auxiliary buffer + local lim = 100 + local a = {}; for i = 1, lim do a[i] = "01234567890123456789" end + testamem("auxiliary buffer", function () + return (#table.concat(a, ",") == 20*lim + lim - 1) + end) +end + +testamem("growing stack", function () + local function foo (n) + if n == 0 then return 1 else return 1 + foo(n - 1) end + end + return foo(100) +end) + +do -- testing failing in 'lua_checkstack' + local res = T.testC([[rawcheckstack 500000; return 1]]) + assert(res == false) + local L = T.newstate() + T.alloccount(0) -- will be unable to reallocate the stack + res = T.testC(L, [[rawcheckstack 5000; return 1]]) + T.alloccount() + T.closestate(L) + assert(res == false) +end + +do -- closing state with no extra memory + local L = T.newstate() + T.alloccount(0) + T.closestate(L) + T.alloccount() +end + +do -- garbage collection with no extra memory + local L = T.newstate() + T.loadlib(L) + local res = (T.doremote(L, [[ + _ENV = require"_G" + local T = require"T" + local a = {} + for i = 1, 1000 do a[i] = 'i' .. i end -- grow string table + local stsize, stuse = T.querystr() + assert(stuse > 1000) + local function foo (n) + if n > 0 then foo(n - 1) end + end + foo(180) -- grow stack + local _, stksize = T.stacklevel() + assert(stksize > 180) + a = nil + T.alloccount(0) + collectgarbage() + T.alloccount() + -- stack and string table could not be reallocated, + -- so they kept their sizes (without errors) + assert(select(2, T.stacklevel()) == stksize) + assert(T.querystr() == stsize) + return 'ok' + ]])) + assert(res == 'ok') + T.closestate(L) +end + +print'+' + +-- testing some auxlib functions +local function gsub (a, b, c) + a, b = T.testC("gsub 2 3 4; gettop; return 2", a, b, c) + assert(b == 5) + return a +end + +assert(gsub("alo.alo.uhuh.", ".", "//") == "alo//alo//uhuh//") +assert(gsub("alo.alo.uhuh.", "alo", "//") == "//.//.uhuh.") +assert(gsub("", "alo", "//") == "") +assert(gsub("...", ".", "/.") == "/././.") +assert(gsub("...", "...", "") == "") + + +-- testing luaL_newmetatable +local mt_xuxu, res, top = T.testC("newmetatable xuxu; gettop; return 3") +assert(type(mt_xuxu) == "table" and res and top == 3) +local d, res, top = T.testC("newmetatable xuxu; gettop; return 3") +assert(mt_xuxu == d and not res and top == 3) +d, res, top = T.testC("newmetatable xuxu1; gettop; return 3") +assert(mt_xuxu ~= d and res and top == 3) + +x = T.newuserdata(0); +y = T.newuserdata(0); +T.testC("pushstring xuxu; gettable R; setmetatable 2", x) +assert(getmetatable(x) == mt_xuxu) + +-- testing luaL_testudata +-- correct metatable +local res1, res2, top = T.testC([[testudata -1 xuxu + testudata 2 xuxu + gettop + return 3]], x) +assert(res1 and res2 and top == 4) + +-- wrong metatable +res1, res2, top = T.testC([[testudata -1 xuxu1 + testudata 2 xuxu1 + gettop + return 3]], x) +assert(not res1 and not res2 and top == 4) + +-- non-existent type +res1, res2, top = T.testC([[testudata -1 xuxu2 + testudata 2 xuxu2 + gettop + return 3]], x) +assert(not res1 and not res2 and top == 4) + +-- userdata has no metatable +res1, res2, top = T.testC([[testudata -1 xuxu + testudata 2 xuxu + gettop + return 3]], y) +assert(not res1 and not res2 and top == 4) + +-- erase metatables +do + local r = debug.getregistry() + assert(r.xuxu == mt_xuxu and r.xuxu1 == d) + r.xuxu = nil; r.xuxu1 = nil +end + +print'OK' + diff --git a/testes/attrib.lua b/testes/attrib.lua new file mode 100644 index 0000000000..79a08a4f69 --- /dev/null +++ b/testes/attrib.lua @@ -0,0 +1,487 @@ +-- $Id: attrib.lua,v 1.69 2018/03/12 13:51:02 roberto Exp $ +-- See Copyright Notice in file all.lua + +print "testing require" + +assert(require"string" == string) +assert(require"math" == math) +assert(require"table" == table) +assert(require"io" == io) +assert(require"os" == os) +assert(require"coroutine" == coroutine) + +assert(type(package.path) == "string") +assert(type(package.cpath) == "string") +assert(type(package.loaded) == "table") +assert(type(package.preload) == "table") + +assert(type(package.config) == "string") +print("package config: "..string.gsub(package.config, "\n", "|")) + +do + -- create a path with 'max' templates, + -- each with 1-10 repetitions of '?' + local max = _soft and 100 or 2000 + local t = {} + for i = 1,max do t[i] = string.rep("?", i%10 + 1) end + t[#t + 1] = ";" -- empty template + local path = table.concat(t, ";") + -- use that path in a search + local s, err = package.searchpath("xuxu", path) + -- search fails; check that message has an occurence of + -- '??????????' with ? replaced by xuxu and at least 'max' lines + assert(not s and + string.find(err, string.rep("xuxu", 10)) and + #string.gsub(err, "[^\n]", "") >= max) + -- path with one very long template + local path = string.rep("?", max) + local s, err = package.searchpath("xuxu", path) + assert(not s and string.find(err, string.rep('xuxu', max))) +end + +do + local oldpath = package.path + package.path = {} + local s, err = pcall(require, "no-such-file") + assert(not s and string.find(err, "package.path")) + package.path = oldpath +end + +print('+') + + +-- The next tests for 'require' assume some specific directories and +-- libraries. + +if not _port then --[ + +local dirsep = string.match(package.config, "^([^\n]+)\n") + +-- auxiliary directory with C modules and temporary files +local DIR = "libs" .. dirsep + +-- prepend DIR to a name and correct directory separators +local function D (x) + x = string.gsub(x, "/", dirsep) + return DIR .. x +end + +-- prepend DIR and pospend proper C lib. extension to a name +local function DC (x) + local ext = (dirsep == '\\') and ".dll" or ".so" + return D(x .. ext) +end + + +local function createfiles (files, preextras, posextras) + for n,c in pairs(files) do + io.output(D(n)) + io.write(string.format(preextras, n)) + io.write(c) + io.write(string.format(posextras, n)) + io.close(io.output()) + end +end + +function removefiles (files) + for n in pairs(files) do + os.remove(D(n)) + end +end + +local files = { + ["names.lua"] = "do return {...} end\n", + ["err.lua"] = "B = 15; a = a + 1;", + ["synerr.lua"] = "B =", + ["A.lua"] = "", + ["B.lua"] = "assert(...=='B');require 'A'", + ["A.lc"] = "", + ["A"] = "", + ["L"] = "", + ["XXxX"] = "", + ["C.lua"] = "package.loaded[...] = 25; require'C'", +} + +AA = nil +local extras = [[ +NAME = '%s' +REQUIRED = ... +return AA]] + +createfiles(files, "", extras) + +-- testing explicit "dir" separator in 'searchpath' +assert(package.searchpath("C.lua", D"?", "", "") == D"C.lua") +assert(package.searchpath("C.lua", D"?", ".", ".") == D"C.lua") +assert(package.searchpath("--x-", D"?", "-", "X") == D"XXxX") +assert(package.searchpath("---xX", D"?", "---", "XX") == D"XXxX") +assert(package.searchpath(D"C.lua", "?", dirsep) == D"C.lua") +assert(package.searchpath(".\\C.lua", D"?", "\\") == D"./C.lua") + +local oldpath = package.path + +package.path = string.gsub("D/?.lua;D/?.lc;D/?;D/??x?;D/L", "D/", DIR) + +local try = function (p, n, r) + NAME = nil + local rr = require(p) + assert(NAME == n) + assert(REQUIRED == p) + assert(rr == r) +end + +a = require"names" +assert(a[1] == "names" and a[2] == D"names.lua") + +_G.a = nil +local st, msg = pcall(require, "err") +assert(not st and string.find(msg, "arithmetic") and B == 15) +st, msg = pcall(require, "synerr") +assert(not st and string.find(msg, "error loading module")) + +assert(package.searchpath("C", package.path) == D"C.lua") +assert(require"C" == 25) +assert(require"C" == 25) +AA = nil +try('B', 'B.lua', true) +assert(package.loaded.B) +assert(require"B" == true) +assert(package.loaded.A) +assert(require"C" == 25) +package.loaded.A = nil +try('B', nil, true) -- should not reload package +try('A', 'A.lua', true) +package.loaded.A = nil +os.remove(D'A.lua') +AA = {} +try('A', 'A.lc', AA) -- now must find second option +assert(package.searchpath("A", package.path) == D"A.lc") +assert(require("A") == AA) +AA = false +try('K', 'L', false) -- default option +try('K', 'L', false) -- default option (should reload it) +assert(rawget(_G, "_REQUIREDNAME") == nil) + +AA = "x" +try("X", "XXxX", AA) + + +removefiles(files) + + +-- testing require of sub-packages + +local _G = _G + +package.path = string.gsub("D/?.lua;D/?/init.lua", "D/", DIR) + +files = { + ["P1/init.lua"] = "AA = 10", + ["P1/xuxu.lua"] = "AA = 20", +} + +createfiles(files, "_ENV = {}\n", "\nreturn _ENV\n") +AA = 0 + +local m = assert(require"P1") +assert(AA == 0 and m.AA == 10) +assert(require"P1" == m) +assert(require"P1" == m) + +assert(package.searchpath("P1.xuxu", package.path) == D"P1/xuxu.lua") +m.xuxu = assert(require"P1.xuxu") +assert(AA == 0 and m.xuxu.AA == 20) +assert(require"P1.xuxu" == m.xuxu) +assert(require"P1.xuxu" == m.xuxu) +assert(require"P1" == m and m.AA == 10) + + +removefiles(files) + + +package.path = "" +assert(not pcall(require, "file_does_not_exist")) +package.path = "??\0?" +assert(not pcall(require, "file_does_not_exist1")) + +package.path = oldpath + +-- check 'require' error message +local fname = "file_does_not_exist2" +local m, err = pcall(require, fname) +for t in string.gmatch(package.path..";"..package.cpath, "[^;]+") do + t = string.gsub(t, "?", fname) + assert(string.find(err, t, 1, true)) +end + +do -- testing 'package.searchers' not being a table + local searchers = package.searchers + package.searchers = 3 + local st, msg = pcall(require, 'a') + assert(not st and string.find(msg, "must be a table")) + package.searchers = searchers +end + +local function import(...) + local f = {...} + return function (m) + for i=1, #f do m[f[i]] = _G[f[i]] end + end +end + +-- cannot change environment of a C function +assert(not pcall(module, 'XUXU')) + + + +-- testing require of C libraries + + +local p = "" -- On Mac OS X, redefine this to "_" + +-- check whether loadlib works in this system +local st, err, when = package.loadlib(DC"lib1", "*") +if not st then + local f, err, when = package.loadlib("donotexist", p.."xuxu") + assert(not f and type(err) == "string" and when == "absent") + ;(Message or print)('\n >>> cannot load dynamic library <<<\n') + print(err, when) +else + -- tests for loadlib + local f = assert(package.loadlib(DC"lib1", p.."onefunction")) + local a, b = f(15, 25) + assert(a == 25 and b == 15) + + f = assert(package.loadlib(DC"lib1", p.."anotherfunc")) + assert(f(10, 20) == "10%20\n") + + -- check error messages + local f, err, when = package.loadlib(DC"lib1", p.."xuxu") + assert(not f and type(err) == "string" and when == "init") + f, err, when = package.loadlib("donotexist", p.."xuxu") + assert(not f and type(err) == "string" and when == "open") + + -- symbols from 'lib1' must be visible to other libraries + f = assert(package.loadlib(DC"lib11", p.."luaopen_lib11")) + assert(f() == "exported") + + -- test C modules with prefixes in names + package.cpath = DC"?" + local lib2 = require"lib2-v2" + -- check correct access to global environment and correct + -- parameters + assert(_ENV.x == "lib2-v2" and _ENV.y == DC"lib2-v2") + assert(lib2.id("x") == "x") + + -- test C submodules + local fs = require"lib1.sub" + assert(_ENV.x == "lib1.sub" and _ENV.y == DC"lib1") + assert(fs.id(45) == 45) +end + +_ENV = _G + + +-- testing preload + +do + local p = package + package = {} + p.preload.pl = function (...) + local _ENV = {...} + function xuxu (x) return x+20 end + return _ENV + end + + local pl = require"pl" + assert(require"pl" == pl) + assert(pl.xuxu(10) == 30) + assert(pl[1] == "pl" and pl[2] == nil) + + package = p + assert(type(package.path) == "string") +end + +print('+') + +end --] + +print("testing assignments, logical operators, and constructors") + +local res, res2 = 27 + +a, b = 1, 2+3 +assert(a==1 and b==5) +a={} +function f() return 10, 11, 12 end +a.x, b, a[1] = 1, 2, f() +assert(a.x==1 and b==2 and a[1]==10) +a[f()], b, a[f()+3] = f(), a, 'x' +assert(a[10] == 10 and b == a and a[13] == 'x') + +do + local f = function (n) local x = {}; for i=1,n do x[i]=i end; + return table.unpack(x) end; + local a,b,c + a,b = 0, f(1) + assert(a == 0 and b == 1) + A,b = 0, f(1) + assert(A == 0 and b == 1) + a,b,c = 0,5,f(4) + assert(a==0 and b==5 and c==1) + a,b,c = 0,5,f(0) + assert(a==0 and b==5 and c==nil) +end + +a, b, c, d = 1 and nil, 1 or nil, (1 and (nil or 1)), 6 +assert(not a and b and c and d==6) + +d = 20 +a, b, c, d = f() +assert(a==10 and b==11 and c==12 and d==nil) +a,b = f(), 1, 2, 3, f() +assert(a==10 and b==1) + +assert(ab == true) +assert((10 and 2) == 2) +assert((10 or 2) == 10) +assert((10 or assert(nil)) == 10) +assert(not (nil and assert(nil))) +assert((nil or "alo") == "alo") +assert((nil and 10) == nil) +assert((false and 10) == false) +assert((true or 10) == true) +assert((false or 10) == 10) +assert(false ~= nil) +assert(nil ~= false) +assert(not nil == true) +assert(not not nil == false) +assert(not not 1 == true) +assert(not not a == true) +assert(not not (6 or nil) == true) +assert(not not (nil and 56) == false) +assert(not not (nil and true) == false) +assert(not 10 == false) +assert(not {} == false) +assert(not 0.5 == false) +assert(not "x" == false) + +assert({} ~= {}) +print('+') + +a = {} +a[true] = 20 +a[false] = 10 +assert(a[1<2] == 20 and a[1>2] == 10) + +function f(a) return a end + +local a = {} +for i=3000,-3000,-1 do a[i + 0.0] = i; end +a[10e30] = "alo"; a[true] = 10; a[false] = 20 +assert(a[10e30] == 'alo' and a[not 1] == 20 and a[10<20] == 10) +for i=3000,-3000,-1 do assert(a[i] == i); end +a[print] = assert +a[f] = print +a[a] = a +assert(a[a][a][a][a][print] == assert) +a[print](a[a[f]] == a[print]) +assert(not pcall(function () local a = {}; a[nil] = 10 end)) +assert(not pcall(function () local a = {[nil] = 10} end)) +assert(a[nil] == undef) +a = nil + +a = {10,9,8,7,6,5,4,3,2; [-3]='a', [f]=print, a='a', b='ab'} +a, a.x, a.y = a, a[-3] +assert(a[1]==10 and a[-3]==a.a and a[f]==print and a.x=='a' and not a.y) +a[1], f(a)[2], b, c = {['alo']=assert}, 10, a[1], a[f], 6, 10, 23, f(a), 2 +a[1].alo(a[2]==10 and b==10 and c==print) + +a.aVeryLongName012345678901234567890123456789012345678901234567890123456789 = 10 +local function foo () + return a.aVeryLongName012345678901234567890123456789012345678901234567890123456789 +end +assert(foo() == 10 and +a.aVeryLongName012345678901234567890123456789012345678901234567890123456789 == +10) + + + +-- test of large float/integer indices + +-- compute maximum integer where all bits fit in a float +local maxint = math.maxinteger + +-- trim (if needed) to fit in a float +while maxint ~= (maxint + 0.0) or (maxint - 1) ~= (maxint - 1.0) do + maxint = maxint // 2 +end + +maxintF = maxint + 0.0 -- float version + +assert(maxintF == maxint and math.type(maxintF) == "float" and + maxintF >= 2.0^14) + +-- floats and integers must index the same places +a[maxintF] = 10; a[maxintF - 1.0] = 11; +a[-maxintF] = 12; a[-maxintF + 1.0] = 13; + +assert(a[maxint] == 10 and a[maxint - 1] == 11 and + a[-maxint] == 12 and a[-maxint + 1] == 13) + +a[maxint] = 20 +a[-maxint] = 22 + +assert(a[maxintF] == 20 and a[maxintF - 1.0] == 11 and + a[-maxintF] == 22 and a[-maxintF + 1.0] == 13) + +a = nil + + +-- test conflicts in multiple assignment +do + local a,i,j,b + a = {'a', 'b'}; i=1; j=2; b=a + i, a[i], a, j, a[j], a[i+j] = j, i, i, b, j, i + assert(i == 2 and b[1] == 1 and a == 1 and j == b and b[2] == 2 and + b[3] == 1) + a = {} + local function foo () -- assigining to upvalues + b, a.x, a = a, 10, 20 + end + foo() + assert(a == 20 and b.x == 10) +end + +-- repeat test with upvalues +do + local a,i,j,b + a = {'a', 'b'}; i=1; j=2; b=a + local function foo () + i, a[i], a, j, a[j], a[i+j] = j, i, i, b, j, i + end + foo() + assert(i == 2 and b[1] == 1 and a == 1 and j == b and b[2] == 2 and + b[3] == 1) + local t = {} + (function (a) t[a], a = 10, 20 end)(1); + assert(t[1] == 10) +end + +-- bug in 5.2 beta +local function foo () + local a + return function () + local b + a, b = 3, 14 -- local and upvalue have same index + return a, b + end +end + +local a, b = foo()() +assert(a == 3 and b == 14) + +print('OK') + +return res + diff --git a/testes/big.lua b/testes/big.lua new file mode 100644 index 0000000000..ebee1ec0a4 --- /dev/null +++ b/testes/big.lua @@ -0,0 +1,82 @@ +-- $Id: big.lua,v 1.35 2018/03/09 14:23:48 roberto Exp $ +-- See Copyright Notice in file all.lua + +if _soft then + return 'a' +end + +print "testing large tables" + +local debug = require"debug" + +local lim = 2^18 + 1000 +local prog = { "local y = {0" } +for i = 1, lim do prog[#prog + 1] = i end +prog[#prog + 1] = "}\n" +prog[#prog + 1] = "X = y\n" +prog[#prog + 1] = ("assert(X[%d] == %d)"):format(lim - 1, lim - 2) +prog[#prog + 1] = "return 0" +prog = table.concat(prog, ";") + +local env = {string = string, assert = assert} +local f = assert(load(prog, nil, nil, env)) + +f() +assert(env.X[lim] == lim - 1 and env.X[lim + 1] == lim) +for k in pairs(env) do env[k] = undef end + +-- yields during accesses larger than K (in RK) +setmetatable(env, { + __index = function (t, n) coroutine.yield('g'); return _G[n] end, + __newindex = function (t, n, v) coroutine.yield('s'); _G[n] = v end, +}) + +X = nil +co = coroutine.wrap(f) +assert(co() == 's') +assert(co() == 'g') +assert(co() == 'g') +assert(co() == 0) + +assert(X[lim] == lim - 1 and X[lim + 1] == lim) + +-- errors in accesses larger than K (in RK) +getmetatable(env).__index = function () end +getmetatable(env).__newindex = function () end +local e, m = pcall(f) +assert(not e and m:find("global 'X'")) + +-- errors in metamethods +getmetatable(env).__newindex = function () error("hi") end +local e, m = xpcall(f, debug.traceback) +assert(not e and m:find("'newindex'")) + +f, X = nil + +coroutine.yield'b' + +if 2^32 == 0 then -- (small integers) { + +print "testing string length overflow" + +local repstrings = 192 -- number of strings to be concatenated +local ssize = math.ceil(2.0^32 / repstrings) + 1 -- size of each string + +assert(repstrings * ssize > 2.0^32) -- it should be larger than maximum size + +local longs = string.rep("\0", ssize) -- create one long string + +-- create function to concatentate 'repstrings' copies of its argument +local rep = assert(load( + "local a = ...; return " .. string.rep("a", repstrings, ".."))) + +local a, b = pcall(rep, longs) -- call that function + +-- it should fail without creating string (result would be too large) +assert(not a and string.find(b, "overflow")) + +end -- } + +print'OK' + +return 'a' diff --git a/testes/bitwise.lua b/testes/bitwise.lua new file mode 100755 index 0000000000..3e7079d386 --- /dev/null +++ b/testes/bitwise.lua @@ -0,0 +1,346 @@ +-- $Id: bitwise.lua,v 1.27 2018/02/21 17:49:39 roberto Exp $ +-- See Copyright Notice in file all.lua + +print("testing bitwise operations") + +require "bwcoercion" + +local numbits = string.packsize('j') * 8 + +assert(~0 == -1) + +assert((1 << (numbits - 1)) == math.mininteger) + +-- basic tests for bitwise operators; +-- use variables to avoid constant folding +local a, b, c, d +a = 0xFFFFFFFFFFFFFFFF +assert(a == -1 and a & -1 == a and a & 35 == 35) +a = 0xF0F0F0F0F0F0F0F0 +assert(a | -1 == -1) +assert(a ~ a == 0 and a ~ 0 == a and a ~ ~a == -1) +assert(a >> 4 == ~a) +a = 0xF0; b = 0xCC; c = 0xAA; d = 0xFD +assert(a | b ~ c & d == 0xF4) + +a = 0xF0.0; b = 0xCC.0; c = "0xAA.0"; d = "0xFD.0" +assert(a | b ~ c & d == 0xF4) + +a = 0xF0000000; b = 0xCC000000; +c = 0xAA000000; d = 0xFD000000 +assert(a | b ~ c & d == 0xF4000000) +assert(~~a == a and ~a == -1 ~ a and -d == ~d + 1) + +a = a << 32 +b = b << 32 +c = c << 32 +d = d << 32 +assert(a | b ~ c & d == 0xF4000000 << 32) +assert(~~a == a and ~a == -1 ~ a and -d == ~d + 1) + +assert(-1 >> 1 == (1 << (numbits - 1)) - 1 and 1 << 31 == 0x80000000) +assert(-1 >> (numbits - 1) == 1) +assert(-1 >> numbits == 0 and + -1 >> -numbits == 0 and + -1 << numbits == 0 and + -1 << -numbits == 0) + +assert((2^30 - 1) << 2^30 == 0) +assert((2^30 - 1) >> 2^30 == 0) + +assert(1 >> -3 == 1 << 3 and 1000 >> 5 == 1000 << -5) + + +-- coercion from strings to integers +assert("0xffffffffffffffff" | 0 == -1) +assert("0xfffffffffffffffe" & "-1" == -2) +assert(" \t-0xfffffffffffffffe\n\t" & "-1" == 2) +assert(" \n -45 \t " >> " -2 " == -45 * 4) +assert("1234.0" << "5.0" == 1234 * 32) +assert("0xffff.0" ~ "0xAAAA" == 0x5555) +assert(~"0x0.000p4" == -1) + +assert("7" .. 3 << 1 == 146) +assert(10 >> 1 .. "9" == 0) +assert(10 | 1 .. "9" == 27) + +do + local st, msg = pcall(function () return 4 & "a" end) + assert(string.find(msg, "'band'")) + + local st, msg = pcall(function () return ~"a" end) + assert(string.find(msg, "'bnot'")) +end + + +-- out of range number +assert(not pcall(function () return "0xffffffffffffffff.0" | 0 end)) + +-- embedded zeros +assert(not pcall(function () return "0xffffffffffffffff\0" | 0 end)) + +print'+' + + +package.preload.bit32 = function () --{ + +-- no built-in 'bit32' library: implement it using bitwise operators + +local bit = {} + +function bit.bnot (a) + return ~a & 0xFFFFFFFF +end + + +-- +-- in all vararg functions, avoid creating 'arg' table when there are +-- only 2 (or less) parameters, as 2 parameters is the common case +-- + +function bit.band (x, y, z, ...) + if not z then + return ((x or -1) & (y or -1)) & 0xFFFFFFFF + else + local arg = {...} + local res = x & y & z + for i = 1, #arg do res = res & arg[i] end + return res & 0xFFFFFFFF + end +end + +function bit.bor (x, y, z, ...) + if not z then + return ((x or 0) | (y or 0)) & 0xFFFFFFFF + else + local arg = {...} + local res = x | y | z + for i = 1, #arg do res = res | arg[i] end + return res & 0xFFFFFFFF + end +end + +function bit.bxor (x, y, z, ...) + if not z then + return ((x or 0) ~ (y or 0)) & 0xFFFFFFFF + else + local arg = {...} + local res = x ~ y ~ z + for i = 1, #arg do res = res ~ arg[i] end + return res & 0xFFFFFFFF + end +end + +function bit.btest (...) + return bit.band(...) ~= 0 +end + +function bit.lshift (a, b) + return ((a & 0xFFFFFFFF) << b) & 0xFFFFFFFF +end + +function bit.rshift (a, b) + return ((a & 0xFFFFFFFF) >> b) & 0xFFFFFFFF +end + +function bit.arshift (a, b) + a = a & 0xFFFFFFFF + if b <= 0 or (a & 0x80000000) == 0 then + return (a >> b) & 0xFFFFFFFF + else + return ((a >> b) | ~(0xFFFFFFFF >> b)) & 0xFFFFFFFF + end +end + +function bit.lrotate (a ,b) + b = b & 31 + a = a & 0xFFFFFFFF + a = (a << b) | (a >> (32 - b)) + return a & 0xFFFFFFFF +end + +function bit.rrotate (a, b) + return bit.lrotate(a, -b) +end + +local function checkfield (f, w) + w = w or 1 + assert(f >= 0, "field cannot be negative") + assert(w > 0, "width must be positive") + assert(f + w <= 32, "trying to access non-existent bits") + return f, ~(-1 << w) +end + +function bit.extract (a, f, w) + local f, mask = checkfield(f, w) + return (a >> f) & mask +end + +function bit.replace (a, v, f, w) + local f, mask = checkfield(f, w) + v = v & mask + a = (a & ~(mask << f)) | (v << f) + return a & 0xFFFFFFFF +end + +return bit + +end --} + + +print("testing bitwise library") + +local bit32 = require'bit32' + +assert(bit32.band() == bit32.bnot(0)) +assert(bit32.btest() == true) +assert(bit32.bor() == 0) +assert(bit32.bxor() == 0) + +assert(bit32.band() == bit32.band(0xffffffff)) +assert(bit32.band(1,2) == 0) + + +-- out-of-range numbers +assert(bit32.band(-1) == 0xffffffff) +assert(bit32.band((1 << 33) - 1) == 0xffffffff) +assert(bit32.band(-(1 << 33) - 1) == 0xffffffff) +assert(bit32.band((1 << 33) + 1) == 1) +assert(bit32.band(-(1 << 33) + 1) == 1) +assert(bit32.band(-(1 << 40)) == 0) +assert(bit32.band(1 << 40) == 0) +assert(bit32.band(-(1 << 40) - 2) == 0xfffffffe) +assert(bit32.band((1 << 40) - 4) == 0xfffffffc) + +assert(bit32.lrotate(0, -1) == 0) +assert(bit32.lrotate(0, 7) == 0) +assert(bit32.lrotate(0x12345678, 0) == 0x12345678) +assert(bit32.lrotate(0x12345678, 32) == 0x12345678) +assert(bit32.lrotate(0x12345678, 4) == 0x23456781) +assert(bit32.rrotate(0x12345678, -4) == 0x23456781) +assert(bit32.lrotate(0x12345678, -8) == 0x78123456) +assert(bit32.rrotate(0x12345678, 8) == 0x78123456) +assert(bit32.lrotate(0xaaaaaaaa, 2) == 0xaaaaaaaa) +assert(bit32.lrotate(0xaaaaaaaa, -2) == 0xaaaaaaaa) +for i = -50, 50 do + assert(bit32.lrotate(0x89abcdef, i) == bit32.lrotate(0x89abcdef, i%32)) +end + +assert(bit32.lshift(0x12345678, 4) == 0x23456780) +assert(bit32.lshift(0x12345678, 8) == 0x34567800) +assert(bit32.lshift(0x12345678, -4) == 0x01234567) +assert(bit32.lshift(0x12345678, -8) == 0x00123456) +assert(bit32.lshift(0x12345678, 32) == 0) +assert(bit32.lshift(0x12345678, -32) == 0) +assert(bit32.rshift(0x12345678, 4) == 0x01234567) +assert(bit32.rshift(0x12345678, 8) == 0x00123456) +assert(bit32.rshift(0x12345678, 32) == 0) +assert(bit32.rshift(0x12345678, -32) == 0) +assert(bit32.arshift(0x12345678, 0) == 0x12345678) +assert(bit32.arshift(0x12345678, 1) == 0x12345678 // 2) +assert(bit32.arshift(0x12345678, -1) == 0x12345678 * 2) +assert(bit32.arshift(-1, 1) == 0xffffffff) +assert(bit32.arshift(-1, 24) == 0xffffffff) +assert(bit32.arshift(-1, 32) == 0xffffffff) +assert(bit32.arshift(-1, -1) == bit32.band(-1 * 2, 0xffffffff)) + +assert(0x12345678 << 4 == 0x123456780) +assert(0x12345678 << 8 == 0x1234567800) +assert(0x12345678 << -4 == 0x01234567) +assert(0x12345678 << -8 == 0x00123456) +assert(0x12345678 << 32 == 0x1234567800000000) +assert(0x12345678 << -32 == 0) +assert(0x12345678 >> 4 == 0x01234567) +assert(0x12345678 >> 8 == 0x00123456) +assert(0x12345678 >> 32 == 0) +assert(0x12345678 >> -32 == 0x1234567800000000) + +print("+") +-- some special cases +local c = {0, 1, 2, 3, 10, 0x80000000, 0xaaaaaaaa, 0x55555555, + 0xffffffff, 0x7fffffff} + +for _, b in pairs(c) do + assert(bit32.band(b) == b) + assert(bit32.band(b, b) == b) + assert(bit32.band(b, b, b, b) == b) + assert(bit32.btest(b, b) == (b ~= 0)) + assert(bit32.band(b, b, b) == b) + assert(bit32.band(b, b, b, ~b) == 0) + assert(bit32.btest(b, b, b) == (b ~= 0)) + assert(bit32.band(b, bit32.bnot(b)) == 0) + assert(bit32.bor(b, bit32.bnot(b)) == bit32.bnot(0)) + assert(bit32.bor(b) == b) + assert(bit32.bor(b, b) == b) + assert(bit32.bor(b, b, b) == b) + assert(bit32.bor(b, b, 0, ~b) == 0xffffffff) + assert(bit32.bxor(b) == b) + assert(bit32.bxor(b, b) == 0) + assert(bit32.bxor(b, b, b) == b) + assert(bit32.bxor(b, b, b, b) == 0) + assert(bit32.bxor(b, 0) == b) + assert(bit32.bnot(b) ~= b) + assert(bit32.bnot(bit32.bnot(b)) == b) + assert(bit32.bnot(b) == (1 << 32) - 1 - b) + assert(bit32.lrotate(b, 32) == b) + assert(bit32.rrotate(b, 32) == b) + assert(bit32.lshift(bit32.lshift(b, -4), 4) == bit32.band(b, bit32.bnot(0xf))) + assert(bit32.rshift(bit32.rshift(b, 4), -4) == bit32.band(b, bit32.bnot(0xf))) +end + +-- for this test, use at most 24 bits (mantissa of a single float) +c = {0, 1, 2, 3, 10, 0x800000, 0xaaaaaa, 0x555555, 0xffffff, 0x7fffff} +for _, b in pairs(c) do + for i = -40, 40 do + local x = bit32.lshift(b, i) + local y = math.floor(math.fmod(b * 2.0^i, 2.0^32)) + assert(math.fmod(x - y, 2.0^32) == 0) + end +end + +assert(not pcall(bit32.band, {})) +assert(not pcall(bit32.bnot, "a")) +assert(not pcall(bit32.lshift, 45)) +assert(not pcall(bit32.lshift, 45, print)) +assert(not pcall(bit32.rshift, 45, print)) + +print("+") + + +-- testing extract/replace + +assert(bit32.extract(0x12345678, 0, 4) == 8) +assert(bit32.extract(0x12345678, 4, 4) == 7) +assert(bit32.extract(0xa0001111, 28, 4) == 0xa) +assert(bit32.extract(0xa0001111, 31, 1) == 1) +assert(bit32.extract(0x50000111, 31, 1) == 0) +assert(bit32.extract(0xf2345679, 0, 32) == 0xf2345679) + +assert(not pcall(bit32.extract, 0, -1)) +assert(not pcall(bit32.extract, 0, 32)) +assert(not pcall(bit32.extract, 0, 0, 33)) +assert(not pcall(bit32.extract, 0, 31, 2)) + +assert(bit32.replace(0x12345678, 5, 28, 4) == 0x52345678) +assert(bit32.replace(0x12345678, 0x87654321, 0, 32) == 0x87654321) +assert(bit32.replace(0, 1, 2) == 2^2) +assert(bit32.replace(0, -1, 4) == 2^4) +assert(bit32.replace(-1, 0, 31) == (1 << 31) - 1) +assert(bit32.replace(-1, 0, 1, 2) == (1 << 32) - 7) + + +-- testing conversion of floats + +assert(bit32.bor(3.0) == 3) +assert(bit32.bor(-4.0) == 0xfffffffc) + +-- large floats and large-enough integers? +if 2.0^50 < 2.0^50 + 1.0 and 2.0^50 < (-1 >> 1) then + assert(bit32.bor(2.0^32 - 5.0) == 0xfffffffb) + assert(bit32.bor(-2.0^32 - 6.0) == 0xfffffffa) + assert(bit32.bor(2.0^48 - 5.0) == 0xfffffffb) + assert(bit32.bor(-2.0^48 - 6.0) == 0xfffffffa) +end + +print'OK' + diff --git a/testes/bwcoercion.lua b/testes/bwcoercion.lua new file mode 100644 index 0000000000..cd735ab0b6 --- /dev/null +++ b/testes/bwcoercion.lua @@ -0,0 +1,78 @@ +local tonumber, tointeger = tonumber, math.tointeger +local type, getmetatable, rawget, error = type, getmetatable, rawget, error +local strsub = string.sub + +local print = print + +_ENV = nil + +-- Try to convert a value to an integer, without assuming any coercion. +local function toint (x) + x = tonumber(x) -- handle numerical strings + if not x then + return false -- not coercible to a number + end + return tointeger(x) +end + + +-- If operation fails, maybe second operand has a metamethod that should +-- have been called if not for this string metamethod, so try to +-- call it. +local function trymt (x, y, mtname) + if type(y) ~= "string" then -- avoid recalling original metamethod + local mt = getmetatable(y) + local mm = mt and rawget(mt, mtname) + if mm then + return mm(x, y) + end + end + -- if any test fails, there is no other metamethod to be called + error("attempt to '" .. strsub(mtname, 3) .. + "' a " .. type(x) .. " with a " .. type(y), 4) +end + + +local function checkargs (x, y, mtname) + local xi = toint(x) + local yi = toint(y) + if xi and yi then + return xi, yi + else + return trymt(x, y, mtname), nil + end +end + + +local smt = getmetatable("") + +smt.__band = function (x, y) + local x, y = checkargs(x, y, "__band") + return y and x & y or x +end + +smt.__bor = function (x, y) + local x, y = checkargs(x, y, "__bor") + return y and x | y or x +end + +smt.__bxor = function (x, y) + local x, y = checkargs(x, y, "__bxor") + return y and x ~ y or x +end + +smt.__shl = function (x, y) + local x, y = checkargs(x, y, "__shl") + return y and x << y or x +end + +smt.__shr = function (x, y) + local x, y = checkargs(x, y, "__shr") + return y and x >> y or x +end + +smt.__bnot = function (x) + local x, y = checkargs(x, x, "__bnot") + return y and ~x or x +end + diff --git a/testes/calls.lua b/testes/calls.lua new file mode 100644 index 0000000000..95d9d6d6ed --- /dev/null +++ b/testes/calls.lua @@ -0,0 +1,435 @@ +-- $Id: calls.lua,v 1.66 2018/02/09 16:35:21 roberto Exp $ +-- See Copyright Notice in file all.lua + +print("testing functions and calls") + +local debug = require "debug" + +-- get the opportunity to test 'type' too ;) + +assert(type(1<2) == 'boolean') +assert(type(true) == 'boolean' and type(false) == 'boolean') +assert(type(nil) == 'nil' + and type(-3) == 'number' + and type'x' == 'string' + and type{} == 'table' + and type(type) == 'function') + +assert(type(assert) == type(print)) +function f (x) return a:x (x) end +assert(type(f) == 'function') +assert(not pcall(type)) + + +do -- test error in 'print' too... + local tostring = _ENV.tostring + + _ENV.tostring = nil + local st, msg = pcall(print, 1) + assert(st == false and string.find(msg, "attempt to call a nil value")) + + _ENV.tostring = function () return {} end + local st, msg = pcall(print, 1) + assert(st == false and string.find(msg, "must return a string")) + + _ENV.tostring = tostring +end + + +-- testing local-function recursion +fact = false +do + local res = 1 + local function fact (n) + if n==0 then return res + else return n*fact(n-1) + end + end + assert(fact(5) == 120) +end +assert(fact == false) + +-- testing declarations +a = {i = 10} +self = 20 +function a:x (x) return x+self.i end +function a.y (x) return x+self end + +assert(a:x(1)+10 == a.y(1)) + +a.t = {i=-100} +a["t"].x = function (self, a,b) return self.i+a+b end + +assert(a.t:x(2,3) == -95) + +do + local a = {x=0} + function a:add (x) self.x, a.y = self.x+x, 20; return self end + assert(a:add(10):add(20):add(30).x == 60 and a.y == 20) +end + +local a = {b={c={}}} + +function a.b.c.f1 (x) return x+1 end +function a.b.c:f2 (x,y) self[x] = y end +assert(a.b.c.f1(4) == 5) +a.b.c:f2('k', 12); assert(a.b.c.k == 12) + +print('+') + +t = nil -- 'declare' t +function f(a,b,c) local d = 'a'; t={a,b,c,d} end + +f( -- this line change must be valid + 1,2) +assert(t[1] == 1 and t[2] == 2 and t[3] == nil and t[4] == 'a') +f(1,2, -- this one too + 3,4) +assert(t[1] == 1 and t[2] == 2 and t[3] == 3 and t[4] == 'a') + +function fat(x) + if x <= 1 then return 1 + else return x*load("return fat(" .. x-1 .. ")", "")() + end +end + +assert(load "load 'assert(fat(6)==720)' () ")() +a = load('return fat(5), 3') +a,b = a() +assert(a == 120 and b == 3) +print('+') + +function err_on_n (n) + if n==0 then error(); exit(1); + else err_on_n (n-1); exit(1); + end +end + +do + function dummy (n) + if n > 0 then + assert(not pcall(err_on_n, n)) + dummy(n-1) + end + end +end + +dummy(10) + +function deep (n) + if n>0 then deep(n-1) end +end +deep(10) +deep(180) + +-- testing tail calls +function deep (n) if n>0 then return deep(n-1) else return 101 end end +assert(deep(30000) == 101) +a = {} +function a:deep (n) if n>0 then return self:deep(n-1) else return 101 end end +assert(a:deep(30000) == 101) + +do -- tail calls x varargs + local function foo (x, ...) local a = {...}; return x, a[1], a[2] end + + local function foo1 (x) return foo(10, x, x + 1) end + + local a, b, c = foo1(-2) + assert(a == 10 and b == -2 and c == -1) + + -- tail calls x metamethods + local t = setmetatable({}, {__call = foo}) + local function foo2 (x) return t(10, x) end + a, b, c = foo2(100) + assert(a == t and b == 10 and c == 100) + + a, b = (function () return foo() end)() + assert(a == nil and b == nil) + + local X, Y, A + local function foo (x, y, ...) X = x; Y = y; A = {...} end + local function foo1 (...) return foo(...) end + + local a, b, c = foo1() + assert(X == nil and Y == nil and #A == 0) + + a, b, c = foo1(10) + assert(X == 10 and Y == nil and #A == 0) + + a, b, c = foo1(10, 20) + assert(X == 10 and Y == 20 and #A == 0) + + a, b, c = foo1(10, 20, 30) + assert(X == 10 and Y == 20 and #A == 1 and A[1] == 30) +end + +print('+') + + +a = nil +(function (x) a=x end)(23) +assert(a == 23 and (function (x) return x*2 end)(20) == 40) + + +-- testing closures + +-- fixed-point operator +Z = function (le) + local function a (f) + return le(function (x) return f(f)(x) end) + end + return a(a) + end + + +-- non-recursive factorial + +F = function (f) + return function (n) + if n == 0 then return 1 + else return n*f(n-1) end + end + end + +fat = Z(F) + +assert(fat(0) == 1 and fat(4) == 24 and Z(F)(5)==5*Z(F)(4)) + +local function g (z) + local function f (a,b,c,d) + return function (x,y) return a+b+c+d+a+x+y+z end + end + return f(z,z+1,z+2,z+3) +end + +f = g(10) +assert(f(9, 16) == 10+11+12+13+10+9+16+10) + +Z, F, f = nil +print('+') + +-- testing multiple returns + +function unlpack (t, i) + i = i or 1 + if (i <= #t) then + return t[i], unlpack(t, i+1) + end +end + +function equaltab (t1, t2) + assert(#t1 == #t2) + for i = 1, #t1 do + assert(t1[i] == t2[i]) + end +end + +local pack = function (...) return (table.pack(...)) end + +function f() return 1,2,30,4 end +function ret2 (a,b) return a,b end + +local a,b,c,d = unlpack{1,2,3} +assert(a==1 and b==2 and c==3 and d==nil) +a = {1,2,3,4,false,10,'alo',false,assert} +equaltab(pack(unlpack(a)), a) +equaltab(pack(unlpack(a), -1), {1,-1}) +a,b,c,d = ret2(f()), ret2(f()) +assert(a==1 and b==1 and c==2 and d==nil) +a,b,c,d = unlpack(pack(ret2(f()), ret2(f()))) +assert(a==1 and b==1 and c==2 and d==nil) +a,b,c,d = unlpack(pack(ret2(f()), (ret2(f())))) +assert(a==1 and b==1 and c==nil and d==nil) + +a = ret2{ unlpack{1,2,3}, unlpack{3,2,1}, unlpack{"a", "b"}} +assert(a[1] == 1 and a[2] == 3 and a[3] == "a" and a[4] == "b") + + +-- testing calls with 'incorrect' arguments +rawget({}, "x", 1) +rawset({}, "x", 1, 2) +assert(math.sin(1,2) == math.sin(1)) +table.sort({10,9,8,4,19,23,0,0}, function (a,b) return a 10 or a[i]() ~= x +assert(i == 11 and a[1]() == 1 and a[3]() == 3 and i == 4) + + +-- testing closures created in 'then' and 'else' parts of 'if's +a = {} +for i = 1, 10 do + if i % 3 == 0 then + local y = 0 + a[i] = function (x) local t = y; y = x; return t end + elseif i % 3 == 1 then + goto L1 + error'not here' + ::L1:: + local y = 1 + a[i] = function (x) local t = y; y = x; return t end + elseif i % 3 == 2 then + local t + goto l4 + ::l4a:: a[i] = t; goto l4b + error("should never be here!") + ::l4:: + local y = 2 + t = function (x) local t = y; y = x; return t end + goto l4a + error("should never be here!") + ::l4b:: + end +end + +for i = 1, 10 do + assert(a[i](i * 10) == i % 3 and a[i]() == i * 10) +end + +print'+' + + +-- test for correctly closing upvalues in tail calls of vararg functions +local function t () + local function c(a,b) assert(a=="test" and b=="OK") end + local function v(f, ...) c("test", f() ~= 1 and "FAILED" or "OK") end + local x = 1 + return v(function() return x end) +end +t() + + +-- test for debug manipulation of upvalues +local debug = require'debug' + +do + local a , b, c = 3, 5, 7 + foo1 = function () return a+b end; + foo2 = function () return b+a end; + do + local a = 10 + foo3 = function () return a+b end; + end +end + +assert(debug.upvalueid(foo1, 1)) +assert(debug.upvalueid(foo1, 2)) +assert(not pcall(debug.upvalueid, foo1, 3)) +assert(debug.upvalueid(foo1, 1) == debug.upvalueid(foo2, 2)) +assert(debug.upvalueid(foo1, 2) == debug.upvalueid(foo2, 1)) +assert(debug.upvalueid(foo3, 1)) +assert(debug.upvalueid(foo1, 1) ~= debug.upvalueid(foo3, 1)) +assert(debug.upvalueid(foo1, 2) == debug.upvalueid(foo3, 2)) + +assert(debug.upvalueid(string.gmatch("x", "x"), 1) ~= nil) + +assert(foo1() == 3 + 5 and foo2() == 5 + 3) +debug.upvaluejoin(foo1, 2, foo2, 2) +assert(foo1() == 3 + 3 and foo2() == 5 + 3) +assert(foo3() == 10 + 5) +debug.upvaluejoin(foo3, 2, foo2, 1) +assert(foo3() == 10 + 5) +debug.upvaluejoin(foo3, 2, foo2, 2) +assert(foo3() == 10 + 3) + +assert(not pcall(debug.upvaluejoin, foo1, 3, foo2, 1)) +assert(not pcall(debug.upvaluejoin, foo1, 1, foo2, 3)) +assert(not pcall(debug.upvaluejoin, foo1, 0, foo2, 1)) +assert(not pcall(debug.upvaluejoin, print, 1, foo2, 1)) +assert(not pcall(debug.upvaluejoin, {}, 1, foo2, 1)) +assert(not pcall(debug.upvaluejoin, foo1, 1, print, 1)) + +print'OK' diff --git a/testes/code.lua b/testes/code.lua new file mode 100644 index 0000000000..e39c62adc6 --- /dev/null +++ b/testes/code.lua @@ -0,0 +1,347 @@ +-- $Id: code.lua,v 1.55 2018/03/12 14:19:36 roberto Exp $ +-- See Copyright Notice in file all.lua + +if T==nil then + (Message or print)('\n >>> testC not active: skipping opcode tests <<<\n') + return +end +print "testing code generation and optimizations" + + +-- this code gave an error for the code checker +do + local function f (a) + for k,v,w in a do end + end +end + + +-- testing reuse in constant table +local function checkKlist (func, list) + local k = T.listk(func) + assert(#k == #list) + for i = 1, #k do + assert(k[i] == list[i] and math.type(k[i]) == math.type(list[i])) + end +end + +local function foo () + local a + a = 3; + a = 0; a = 0.0; a = -7 + 7 + a = 3.78/4; a = 3.78/4 + a = -3.78/4; a = 3.78/4; a = -3.78/4 + a = -3.79/4; a = 0.0; a = -0; + a = 3; a = 3.0; a = 3; a = 3.0 +end + +checkKlist(foo, {3.78/4, -3.78/4, -3.79/4}) + + +-- testing opcodes + +function check (f, ...) + local arg = {...} + local c = T.listcode(f) + for i=1, #arg do + local opcode = string.match(c[i], "%u%w+") + -- print(arg[i], opcode) + assert(arg[i] == opcode) + end + assert(c[#arg+2] == undef) +end + + +function checkequal (a, b) + a = T.listcode(a) + b = T.listcode(b) + for i = 1, #a do + a[i] = string.gsub(a[i], '%b()', '') -- remove line number + b[i] = string.gsub(b[i], '%b()', '') -- remove line number + assert(a[i] == b[i]) + end +end + + +-- some basic instructions +check(function () + (function () end){f()} +end, 'CLOSURE', 'NEWTABLE', 'GETTABUP', 'CALL', 'SETLIST', 'CALL', 'RETURN') + + +-- sequence of LOADNILs +check(function () + local a,b,c + local d; local e; + local f,g,h; + d = nil; d=nil; b=nil; a=nil; c=nil; +end, 'LOADNIL', 'RETURN0') + +check(function () + local a,b,c,d = 1,1,1,1 + d=nil;c=nil;b=nil;a=nil +end, 'LOADI', 'LOADI', 'LOADI', 'LOADI', 'LOADNIL', 'RETURN0') + +do + local a,b,c,d = 1,1,1,1 + d=nil;c=nil;b=nil;a=nil + assert(a == nil and b == nil and c == nil and d == nil) +end + + +-- single return +check (function (a,b,c) return a end, 'RETURN1') + + +-- infinite loops +check(function () while true do local a = -1 end end, +'LOADI', 'JMP', 'RETURN0') + +check(function () while 1 do local a = -1 end end, +'LOADI', 'JMP', 'RETURN0') + +check(function () repeat local x = 1 until true end, +'LOADI', 'RETURN0') + + +-- concat optimization +check(function (a,b,c,d) return a..b..c..d end, + 'MOVE', 'MOVE', 'MOVE', 'MOVE', 'CONCAT', 'RETURN1') + +-- not +check(function () return not not nil end, 'LOADBOOL', 'RETURN1') +check(function () return not not false end, 'LOADBOOL', 'RETURN1') +check(function () return not not true end, 'LOADBOOL', 'RETURN1') +check(function () return not not 1 end, 'LOADBOOL', 'RETURN1') + +-- direct access to locals +check(function () + local a,b,c,d + a = b*a + c.x, a[b] = -((a + d/b - a[b]) ^ a.x), b +end, + 'LOADNIL', + 'MUL', + 'DIV', 'ADD', 'GETTABLE', 'SUB', 'GETFIELD', 'POW', + 'UNM', 'SETTABLE', 'SETFIELD', 'RETURN0') + + +-- direct access to constants +check(function () + local a,b + a.x = 3.2 + a.x = b + a[b] = 'x' +end, + 'LOADNIL', 'SETFIELD', 'SETFIELD', 'SETTABLE', 'RETURN0') + +-- "get/set table" with numeric indices +check(function (a) + a[1] = a[100] + a[255] = a[256] + a[256] = 5 +end, + 'GETI', 'SETI', + 'LOADI', 'GETTABLE', 'SETI', + 'LOADI', 'SETTABLE', 'RETURN0') + +check(function () + local a,b + a = a - a + b = a/a + b = 5-4 +end, + 'LOADNIL', 'SUB', 'DIV', 'LOADI', 'RETURN0') + +check(function () + local a,b + a[true] = false +end, + 'LOADNIL', 'LOADBOOL', 'SETTABLE', 'RETURN0') + + +-- equalities +check(function (a) if a == 1 then return 2 end end, + 'EQI', 'JMP', 'LOADI', 'RETURN1') + +check(function (a) if -4.0 == a then return 2 end end, + 'EQI', 'JMP', 'LOADI', 'RETURN1') + +check(function (a) if a == "hi" then return 2 end end, + 'EQK', 'JMP', 'LOADI', 'RETURN1') + +check(function (a) if a == 10000 then return 2 end end, + 'EQK', 'JMP', 'LOADI', 'RETURN1') -- number too large + +check(function (a) if -10000 == a then return 2 end end, + 'EQK', 'JMP', 'LOADI', 'RETURN1') -- number too large + +-- comparisons + +check(function (a) if -10 <= a then return 2 end end, + 'GEI', 'JMP', 'LOADI', 'RETURN1') + +check(function (a) if 128.0 > a then return 2 end end, + 'LTI', 'JMP', 'LOADI', 'RETURN1') + +check(function (a) if -127.0 < a then return 2 end end, + 'GTI', 'JMP', 'LOADI', 'RETURN1') + +check(function (a) if 10 < a then return 2 end end, + 'GTI', 'JMP', 'LOADI', 'RETURN1') + +check(function (a) if 129 < a then return 2 end end, + 'LOADI', 'LT', 'JMP', 'LOADI', 'RETURN1') + +check(function (a) if a >= 23.0 then return 2 end end, + 'GEI', 'JMP', 'LOADI', 'RETURN1') + +check(function (a) if a >= 23.1 then return 2 end end, + 'LOADK', 'LE', 'JMP', 'LOADI', 'RETURN1') + +check(function (a) if a > 2300.0 then return 2 end end, + 'LOADF', 'LT', 'JMP', 'LOADI', 'RETURN1') + + +-- constant folding +local function checkK (func, val) + check(func, 'LOADK', 'RETURN1') + local k = T.listk(func) + assert(#k == 1 and k[1] == val and math.type(k[1]) == math.type(val)) + assert(func() == val) +end + +local function checkI (func, val) + check(func, 'LOADI', 'RETURN1') + assert(#T.listk(func) == 0) + assert(func() == val) +end + +local function checkF (func, val) + check(func, 'LOADF', 'RETURN1') + assert(#T.listk(func) == 0) + assert(func() == val) +end + +checkF(function () return 0.0 end, 0.0) +checkI(function () return 0 end, 0) +checkI(function () return -0//1 end, 0) +checkK(function () return 3^-1 end, 1/3) +checkK(function () return (1 + 1)^(50 + 50) end, 2^100) +checkK(function () return (-2)^(31 - 2) end, -0x20000000 + 0.0) +checkF(function () return (-3^0 + 5) // 3.0 end, 1.0) +checkI(function () return -3 % 5 end, 2) +checkF(function () return -((2.0^8 + -(-1)) % 8)/2 * 4 - 3 end, -5.0) +checkF(function () return -((2^8 + -(-1)) % 8)//2 * 4 - 3 end, -7.0) +checkI(function () return 0xF0.0 | 0xCC.0 ~ 0xAA & 0xFD end, 0xF4) +checkI(function () return ~(~0xFF0 | 0xFF0) end, 0) +checkI(function () return ~~-1024.0 end, -1024) +checkI(function () return ((100 << 6) << -4) >> 2 end, 100) + +-- borders around MAXARG_sBx ((((1 << 17) - 1) >> 1) == 65535) +local sbx = ((1 << "17") - 1) >> 1 -- avoid folding +checkI(function () return 65535 end, sbx) +checkI(function () return -65535 end, -sbx) +checkI(function () return 65536 end, sbx + 1) +checkK(function () return 65537 end, sbx + 2) +checkK(function () return -65536 end, -(sbx + 1)) + +checkF(function () return 65535.0 end, sbx + 0.0) +checkF(function () return -65535.0 end, -sbx + 0.0) +checkF(function () return 65536.0 end, (sbx + 1.0)) +checkK(function () return 65537.0 end, (sbx + 2.0)) +checkK(function () return -65536.0 end, -(sbx + 1.0)) + + +-- immediate operands +check(function (x) return x + 1 end, 'ADDI', 'RETURN1') +check(function (x) return 128 + x end, 'ADDI', 'RETURN1') +check(function (x) return x * -127 end, 'MULI', 'RETURN1') +check(function (x) return 20 * x end, 'MULI', 'RETURN1') +check(function (x) return x ^ -2 end, 'POWI', 'RETURN1') +check(function (x) return x / 40 end, 'DIVI', 'RETURN1') +check(function (x) return x // 1 end, 'IDIVI', 'RETURN1') +check(function (x) return x % (100 - 10) end, 'MODI', 'RETURN1') +check(function (x) return 1 << x end, 'SHLI', 'RETURN1') +check(function (x) return x << 2 end, 'SHRI', 'RETURN1') +check(function (x) return x >> 2 end, 'SHRI', 'RETURN1') +check(function (x) return x & 1 end, 'BANDK', 'RETURN1') +check(function (x) return 10 | x end, 'BORK', 'RETURN1') +check(function (x) return -10 ~ x end, 'BXORK', 'RETURN1') + +-- no foldings (and immediate operands) +check(function () return -0.0 end, 'LOADF', 'UNM', 'RETURN1') +check(function () return 3/0 end, 'LOADI', 'DIVI', 'RETURN1') +check(function () return 0%0 end, 'LOADI', 'MODI', 'RETURN1') +check(function () return -4//0 end, 'LOADI', 'IDIVI', 'RETURN1') +check(function (x) return x >> 2.0 end, 'LOADF', 'SHR', 'RETURN1') +check(function (x) return x & 2.0 end, 'LOADF', 'BAND', 'RETURN1') + +-- basic 'for' loops +check(function () for i = -10, 10.5 do end end, +'LOADI', 'LOADK', 'LOADI', 'FORPREP1', 'FORLOOP1', 'RETURN0') +check(function () for i = 0xfffffff, 10.0, 1 do end end, +'LOADK', 'LOADF', 'LOADI', 'FORPREP1', 'FORLOOP1', 'RETURN0') + +-- bug in constant folding for 5.1 +check(function () return -nil end, 'LOADNIL', 'UNM', 'RETURN1') + + +check(function () + local a,b,c + b[c], a = c, b + b[a], a = c, b + a, b = c, a + a = a +end, + 'LOADNIL', + 'MOVE', 'MOVE', 'SETTABLE', + 'MOVE', 'MOVE', 'MOVE', 'SETTABLE', + 'MOVE', 'MOVE', 'MOVE', + -- no code for a = a + 'RETURN0') + + +-- x == nil , x ~= nil +-- checkequal(function (b) if (a==nil) then a=1 end; if a~=nil then a=1 end end, +-- function () if (a==9) then a=1 end; if a~=9 then a=1 end end) + +-- check(function () if a==nil then a='a' end end, +-- 'GETTABUP', 'EQ', 'JMP', 'SETTABUP', 'RETURN') + +do -- tests for table access in upvalues + local t + check(function () t.x = t.y end, 'GETTABUP', 'SETTABUP') + check(function (a) t[a()] = t[a()] end, + 'MOVE', 'CALL', 'GETUPVAL', 'MOVE', 'CALL', + 'GETUPVAL', 'GETTABLE', 'SETTABLE') +end + +-- de morgan +checkequal(function () local a; if not (a or b) then b=a end end, + function () local a; if (not a and not b) then b=a end end) + +checkequal(function (l) local a; return 0 <= a and a <= l end, + function (l) local a; return not (not(a >= 0) or not(a <= l)) end) + + +-- if-goto optimizations +check(function (a, b, c, d, e) + if a == b then goto l1; + elseif a == c then goto l2; + elseif a == d then goto l2; + else if a == e then goto l3; + else goto l3 + end + end + ::l1:: ::l2:: ::l3:: ::l4:: +end, 'EQ', 'JMP', 'EQ', 'JMP', 'EQ', 'JMP', 'EQ', 'JMP', 'JMP', +'CLOSE', 'CLOSE', 'CLOSE', 'CLOSE', 'RETURN0') + +checkequal( +function (a) while a < 10 do a = a + 1 end end, +function (a) while true do if not(a < 10) then break end; a = a + 1; end end +) + +print 'OK' + diff --git a/testes/constructs.lua b/testes/constructs.lua new file mode 100644 index 0000000000..7796c46f72 --- /dev/null +++ b/testes/constructs.lua @@ -0,0 +1,302 @@ +-- $Id: constructs.lua,v 1.43 2018/02/21 17:41:07 roberto Exp $ +-- See Copyright Notice in file all.lua + +;;print "testing syntax";; + +local debug = require "debug" + + +local function checkload (s, msg) + assert(string.find(select(2, load(s)), msg)) +end + +-- testing semicollons +do ;;; end +; do ; a = 3; assert(a == 3) end; +; + + +-- invalid operations should not raise errors when not executed +if false then a = 3 // 0; a = 0 % 0 end + + +-- testing priorities + +assert(2^3^2 == 2^(3^2)); +assert(2^3*4 == (2^3)*4); +assert(2.0^-2 == 1/4 and -2^- -2 == - - -4); +assert(not nil and 2 and not(2>3 or 3<2)); +assert(-3-1-5 == 0+0-9); +assert(-2^2 == -4 and (-2)^2 == 4 and 2*2-3-1 == 0); +assert(-3%5 == 2 and -3+5 == 2) +assert(2*1+3/3 == 3 and 1+2 .. 3*1 == "33"); +assert(not(2+1 > 3*1) and "a".."b" > "a"); + +assert(0xF0 | 0xCC ~ 0xAA & 0xFD == 0xF4) +assert(0xFD & 0xAA ~ 0xCC | 0xF0 == 0xF4) +assert(0xF0 & 0x0F + 1 == 0x10) + +assert(3^4//2^3//5 == 2) + +assert(-3+4*5//2^3^2//9+4%10/3 == (-3)+(((4*5)//(2^(3^2)))//9)+((4%10)/3)) + +assert(not ((true or false) and nil)) +assert( true or false and nil) + +-- old bug +assert((((1 or false) and true) or false) == true) +assert((((nil and true) or false) and true) == false) + +local a,b = 1,nil; +assert(-(1 or 2) == -1 and (1 and 2)+(-1.25 or -4) == 0.75); +x = ((b or a)+1 == 2 and (10 or a)+1 == 11); assert(x); +x = (((2<3) or 1) == true and (2<3 and 4) == 4); assert(x); + +x,y=1,2; +assert((x>y) and x or y == 2); +x,y=2,1; +assert((x>y) and x or y == 2); + +assert(1234567890 == tonumber('1234567890') and 1234567890+1 == 1234567891) + + +-- silly loops +repeat until 1; repeat until true; +while false do end; while nil do end; + +do -- test old bug (first name could not be an `upvalue') + local a; function f(x) x={a=1}; x={x=1}; x={G=1} end +end + +function f (i) + if type(i) ~= 'number' then return i,'jojo'; end; + if i > 0 then return i, f(i-1); end; +end + +x = {f(3), f(5), f(10);}; +assert(x[1] == 3 and x[2] == 5 and x[3] == 10 and x[4] == 9 and x[12] == 1); +assert(x[nil] == nil) +x = {f'alo', f'xixi', nil}; +assert(x[1] == 'alo' and x[2] == 'xixi' and x[3] == nil); +x = {f'alo'..'xixi'}; +assert(x[1] == 'aloxixi') +x = {f{}} +assert(x[2] == 'jojo' and type(x[1]) == 'table') + + +local f = function (i) + if i < 10 then return 'a'; + elseif i < 20 then return 'b'; + elseif i < 30 then return 'c'; + end; +end + +assert(f(3) == 'a' and f(12) == 'b' and f(26) == 'c' and f(100) == nil) + +for i=1,1000 do break; end; +n=100; +i=3; +t = {}; +a=nil +while not a do + a=0; for i=1,n do for i=i,1,-1 do a=a+1; t[i]=1; end; end; +end +assert(a == n*(n+1)/2 and i==3); +assert(t[1] and t[n] and not t[0] and not t[n+1]) + +function f(b) + local x = 1; + repeat + local a; + if b==1 then local b=1; x=10; break + elseif b==2 then x=20; break; + elseif b==3 then x=30; + else local a,b,c,d=math.sin(1); x=x+1; + end + until x>=12; + return x; +end; + +assert(f(1) == 10 and f(2) == 20 and f(3) == 30 and f(4)==12) + + +local f = function (i) + if i < 10 then return 'a' + elseif i < 20 then return 'b' + elseif i < 30 then return 'c' + else return 8 + end +end + +assert(f(3) == 'a' and f(12) == 'b' and f(26) == 'c' and f(100) == 8) + +local a, b = nil, 23 +x = {f(100)*2+3 or a, a or b+2} +assert(x[1] == 19 and x[2] == 25) +x = {f=2+3 or a, a = b+2} +assert(x.f == 5 and x.a == 25) + +a={y=1} +x = {a.y} +assert(x[1] == 1) + +function f(i) + while 1 do + if i>0 then i=i-1; + else return; end; + end; +end; + +function g(i) + while 1 do + if i>0 then i=i-1 + else return end + end +end + +f(10); g(10); + +do + function f () return 1,2,3; end + local a, b, c = f(); + assert(a==1 and b==2 and c==3) + a, b, c = (f()); + assert(a==1 and b==nil and c==nil) +end + +local a,b = 3 and f(); +assert(a==1 and b==nil) + +function g() f(); return; end; +assert(g() == nil) +function g() return nil or f() end +a,b = g() +assert(a==1 and b==nil) + +print'+'; + + +f = [[ +return function ( a , b , c , d , e ) + local x = a >= b or c or ( d and e ) or nil + return x +end , { a = 1 , b = 2 >= 1 , } or { 1 }; +]] +f = string.gsub(f, "%s+", "\n"); -- force a SETLINE between opcodes +f,a = load(f)(); +assert(a.a == 1 and a.b) + +function g (a,b,c,d,e) + if not (a>=b or c or d and e or nil) then return 0; else return 1; end; +end + +function h (a,b,c,d,e) + while (a>=b or c or (d and e) or nil) do return 1; end; + return 0; +end; + +assert(f(2,1) == true and g(2,1) == 1 and h(2,1) == 1) +assert(f(1,2,'a') == 'a' and g(1,2,'a') == 1 and h(1,2,'a') == 1) +assert(f(1,2,'a') +~= -- force SETLINE before nil +nil, "") +assert(f(1,2,'a') == 'a' and g(1,2,'a') == 1 and h(1,2,'a') == 1) +assert(f(1,2,nil,1,'x') == 'x' and g(1,2,nil,1,'x') == 1 and + h(1,2,nil,1,'x') == 1) +assert(f(1,2,nil,nil,'x') == nil and g(1,2,nil,nil,'x') == 0 and + h(1,2,nil,nil,'x') == 0) +assert(f(1,2,nil,1,nil) == nil and g(1,2,nil,1,nil) == 0 and + h(1,2,nil,1,nil) == 0) + +assert(1 and 2<3 == true and 2<3 and 'a'<'b' == true) +x = 2<3 and not 3; assert(x==false) +x = 2<1 or (2>1 and 'a'); assert(x=='a') + + +do + local a; if nil then a=1; else a=2; end; -- this nil comes as PUSHNIL 2 + assert(a==2) +end + +function F(a) + assert(debug.getinfo(1, "n").name == 'F') + return a,2,3 +end + +a,b = F(1)~=nil; assert(a == true and b == nil); +a,b = F(nil)==nil; assert(a == true and b == nil) + +---------------------------------------------------------------- +------------------------------------------------------------------ + +-- sometimes will be 0, sometimes will not... +_ENV.GLOB1 = math.floor(os.time()) % 2 + +-- basic expressions with their respective values +local basiccases = { + {"nil", nil}, + {"false", false}, + {"true", true}, + {"10", 10}, + {"(0==_ENV.GLOB1)", 0 == _ENV.GLOB1}, +} + +print('testing short-circuit optimizations (' .. _ENV.GLOB1 .. ')') + + +-- operators with their respective values +local binops = { + {" and ", function (a,b) if not a then return a else return b end end}, + {" or ", function (a,b) if a then return a else return b end end}, +} + +local cases = {} + +-- creates all combinations of '(cases[i] op cases[n-i])' plus +-- 'not(cases[i] op cases[n-i])' (syntax + value) +local function createcases (n) + local res = {} + for i = 1, n - 1 do + for _, v1 in ipairs(cases[i]) do + for _, v2 in ipairs(cases[n - i]) do + for _, op in ipairs(binops) do + local t = { + "(" .. v1[1] .. op[1] .. v2[1] .. ")", + op[2](v1[2], v2[2]) + } + res[#res + 1] = t + res[#res + 1] = {"not" .. t[1], not t[2]} + end + end + end + end + return res +end + +-- do not do too many combinations for soft tests +local level = _soft and 3 or 4 + +cases[1] = basiccases +for i = 2, level do cases[i] = createcases(i) end +print("+") + +local prog = [[if %s then IX = true end; return %s]] + +local i = 0 +for n = 1, level do + for _, v in pairs(cases[n]) do + local s = v[1] + local p = load(string.format(prog, s, s), "") + IX = false + assert(p() == v[2] and IX == not not v[2]) + i = i + 1 + if i % 60000 == 0 then print('+') end + end +end +------------------------------------------------------------------ + +-- testing some syntax errors (chosen through 'gcov') +checkload("for x do", "expected") +checkload("x:call", "expected") + +print'OK' diff --git a/testes/coroutine.lua b/testes/coroutine.lua new file mode 100644 index 0000000000..220873200d --- /dev/null +++ b/testes/coroutine.lua @@ -0,0 +1,918 @@ +-- $Id: coroutine.lua,v 1.48 2018/03/12 14:19:36 roberto Exp $ +-- See Copyright Notice in file all.lua + +print "testing coroutines" + +local debug = require'debug' + +local f + +local main, ismain = coroutine.running() +assert(type(main) == "thread" and ismain) +assert(not coroutine.resume(main)) +assert(not coroutine.isyieldable()) +assert(not pcall(coroutine.yield)) + + +-- trivial errors +assert(not pcall(coroutine.resume, 0)) +assert(not pcall(coroutine.status, 0)) + + +-- tests for multiple yield/resume arguments + +local function eqtab (t1, t2) + assert(#t1 == #t2) + for i = 1, #t1 do + local v = t1[i] + assert(t2[i] == v) + end +end + +_G.x = nil -- declare x +function foo (a, ...) + local x, y = coroutine.running() + assert(x == f and y == false) + -- next call should not corrupt coroutine (but must fail, + -- as it attempts to resume the running coroutine) + assert(coroutine.resume(f) == false) + assert(coroutine.status(f) == "running") + local arg = {...} + assert(coroutine.isyieldable()) + for i=1,#arg do + _G.x = {coroutine.yield(table.unpack(arg[i]))} + end + return table.unpack(a) +end + +f = coroutine.create(foo) +assert(type(f) == "thread" and coroutine.status(f) == "suspended") +assert(string.find(tostring(f), "thread")) +local s,a,b,c,d +s,a,b,c,d = coroutine.resume(f, {1,2,3}, {}, {1}, {'a', 'b', 'c'}) +assert(s and a == nil and coroutine.status(f) == "suspended") +s,a,b,c,d = coroutine.resume(f) +eqtab(_G.x, {}) +assert(s and a == 1 and b == nil) +s,a,b,c,d = coroutine.resume(f, 1, 2, 3) +eqtab(_G.x, {1, 2, 3}) +assert(s and a == 'a' and b == 'b' and c == 'c' and d == nil) +s,a,b,c,d = coroutine.resume(f, "xuxu") +eqtab(_G.x, {"xuxu"}) +assert(s and a == 1 and b == 2 and c == 3 and d == nil) +assert(coroutine.status(f) == "dead") +s, a = coroutine.resume(f, "xuxu") +assert(not s and string.find(a, "dead") and coroutine.status(f) == "dead") + + +-- yields in tail calls +local function foo (i) return coroutine.yield(i) end +f = coroutine.wrap(function () + for i=1,10 do + assert(foo(i) == _G.x) + end + return 'a' +end) +for i=1,10 do _G.x = i; assert(f(i) == i) end +_G.x = 'xuxu'; assert(f('xuxu') == 'a') + +-- recursive +function pf (n, i) + coroutine.yield(n) + pf(n*i, i+1) +end + +f = coroutine.wrap(pf) +local s=1 +for i=1,10 do + assert(f(1, 1) == s) + s = s*i +end + +-- sieve +function gen (n) + return coroutine.wrap(function () + for i=2,n do coroutine.yield(i) end + end) +end + + +function filter (p, g) + return coroutine.wrap(function () + while 1 do + local n = g() + if n == nil then return end + if math.fmod(n, p) ~= 0 then coroutine.yield(n) end + end + end) +end + +local x = gen(100) +local a = {} +while 1 do + local n = x() + if n == nil then break end + table.insert(a, n) + x = filter(n, x) +end + +assert(#a == 25 and a[#a] == 97) +x, a = nil + +-- yielding across C boundaries + +co = coroutine.wrap(function() + assert(not pcall(table.sort,{1,2,3}, coroutine.yield)) + assert(coroutine.isyieldable()) + coroutine.yield(20) + return 30 + end) + +assert(co() == 20) +assert(co() == 30) + + +local f = function (s, i) return coroutine.yield(i) end + +local f1 = coroutine.wrap(function () + return xpcall(pcall, function (...) return ... end, + function () + local s = 0 + for i in f, nil, 1 do pcall(function () s = s + i end) end + error({s}) + end) + end) + +f1() +for i = 1, 10 do assert(f1(i) == i) end +local r1, r2, v = f1(nil) +assert(r1 and not r2 and v[1] == (10 + 1)*10/2) + + +function f (a, b) a = coroutine.yield(a); error{a + b} end +function g(x) return x[1]*2 end + +co = coroutine.wrap(function () + coroutine.yield(xpcall(f, g, 10, 20)) + end) + +assert(co() == 10) +r, msg = co(100) +assert(not r and msg == 240) + + +-- unyieldable C call +do + local function f (c) + assert(not coroutine.isyieldable()) + return c .. c + end + + local co = coroutine.wrap(function (c) + assert(coroutine.isyieldable()) + local s = string.gsub("a", ".", f) + return s + end) + assert(co() == "aa") +end + + + +do -- testing single trace of coroutines + local X + local co = coroutine.create(function () + coroutine.yield(10) + return 20; + end) + local trace = {} + local function dotrace (event) + trace[#trace + 1] = event + end + debug.sethook(co, dotrace, "clr") + repeat until not coroutine.resume(co) + local correcttrace = {"call", "line", "call", "return", "line", "return"} + assert(#trace == #correcttrace) + for k, v in pairs(trace) do + assert(v == correcttrace[k]) + end +end + +-- errors in coroutines +function foo () + assert(debug.getinfo(1).currentline == debug.getinfo(foo).linedefined + 1) + assert(debug.getinfo(2).currentline == debug.getinfo(goo).linedefined) + coroutine.yield(3) + error(foo) +end + +function goo() foo() end +x = coroutine.wrap(goo) +assert(x() == 3) +local a,b = pcall(x) +assert(not a and b == foo) + +x = coroutine.create(goo) +a,b = coroutine.resume(x) +assert(a and b == 3) +a,b = coroutine.resume(x) +assert(not a and b == foo and coroutine.status(x) == "dead") +a,b = coroutine.resume(x) +assert(not a and string.find(b, "dead") and coroutine.status(x) == "dead") + + +-- co-routines x for loop +function all (a, n, k) + if k == 0 then coroutine.yield(a) + else + for i=1,n do + a[k] = i + all(a, n, k-1) + end + end +end + +local a = 0 +for t in coroutine.wrap(function () all({}, 5, 4) end) do + a = a+1 +end +assert(a == 5^4) + + +-- access to locals of collected corroutines +local C = {}; setmetatable(C, {__mode = "kv"}) +local x = coroutine.wrap (function () + local a = 10 + local function f () a = a+10; return a end + while true do + a = a+1 + coroutine.yield(f) + end + end) + +C[1] = x; + +local f = x() +assert(f() == 21 and x()() == 32 and x() == f) +x = nil +collectgarbage() +assert(C[1] == undef) +assert(f() == 43 and f() == 53) + + +-- old bug: attempt to resume itself + +function co_func (current_co) + assert(coroutine.running() == current_co) + assert(coroutine.resume(current_co) == false) + coroutine.yield(10, 20) + assert(coroutine.resume(current_co) == false) + coroutine.yield(23) + return 10 +end + +local co = coroutine.create(co_func) +local a,b,c = coroutine.resume(co, co) +assert(a == true and b == 10 and c == 20) +a,b = coroutine.resume(co, co) +assert(a == true and b == 23) +a,b = coroutine.resume(co, co) +assert(a == true and b == 10) +assert(coroutine.resume(co, co) == false) +assert(coroutine.resume(co, co) == false) + + +-- other old bug when attempting to resume itself +-- (trigger C-code assertions) +do + local A = coroutine.running() + local B = coroutine.create(function() return coroutine.resume(A) end) + local st, res = coroutine.resume(B) + assert(st == true and res == false) + + A = coroutine.wrap(function() return pcall(A, 1) end) + st, res = A() + assert(not st and string.find(res, "non%-suspended")) +end + + +-- attempt to resume 'normal' coroutine +local co1, co2 +co1 = coroutine.create(function () return co2() end) +co2 = coroutine.wrap(function () + assert(coroutine.status(co1) == 'normal') + assert(not coroutine.resume(co1)) + coroutine.yield(3) + end) + +a,b = coroutine.resume(co1) +assert(a and b == 3) +assert(coroutine.status(co1) == 'dead') + +-- infinite recursion of coroutines +a = function(a) coroutine.wrap(a)(a) end +assert(not pcall(a, a)) +a = nil + + +-- access to locals of erroneous coroutines +local x = coroutine.create (function () + local a = 10 + _G.f = function () a=a+1; return a end + error('x') + end) + +assert(not coroutine.resume(x)) +-- overwrite previous position of local `a' +assert(not coroutine.resume(x, 1, 1, 1, 1, 1, 1, 1)) +assert(_G.f() == 11) +assert(_G.f() == 12) + + +if not T then + (Message or print)('\n >>> testC not active: skipping yield/hook tests <<<\n') +else + print "testing yields inside hooks" + + local turn + + function fact (t, x) + assert(turn == t) + if x == 0 then return 1 + else return x*fact(t, x-1) + end + end + + local A, B = 0, 0 + + local x = coroutine.create(function () + T.sethook("yield 0", "", 2) + A = fact("A", 6) + end) + + local y = coroutine.create(function () + T.sethook("yield 0", "", 3) + B = fact("B", 7) + end) + + while A==0 or B==0 do -- A ~= 0 when 'x' finishes (similar for 'B','y') + if A==0 then turn = "A"; assert(T.resume(x)) end + if B==0 then turn = "B"; assert(T.resume(y)) end + end + + assert(B // A == 7) -- fact(7) // fact(6) + + local line = debug.getinfo(1, "l").currentline + 2 -- get line number + local function foo () + local x = 10 --<< this line is 'line' + x = x + 10 + _G.XX = x + end + + -- testing yields in line hook + local co = coroutine.wrap(function () + T.sethook("setglobal X; yield 0", "l", 0); foo(); return 10 end) + + _G.XX = nil; + _G.X = nil; co(); assert(_G.X == line) + _G.X = nil; co(); assert(_G.X == line + 1) + _G.X = nil; co(); assert(_G.X == line + 2 and _G.XX == nil) + _G.X = nil; co(); assert(_G.X == line + 3 and _G.XX == 20) + assert(co() == 10) + + -- testing yields in count hook + co = coroutine.wrap(function () + T.sethook("yield 0", "", 1); foo(); return 10 end) + + _G.XX = nil; + local c = 0 + repeat c = c + 1; local a = co() until a == 10 + assert(_G.XX == 20 and c >= 5) + + co = coroutine.wrap(function () + T.sethook("yield 0", "", 2); foo(); return 10 end) + + _G.XX = nil; + local c = 0 + repeat c = c + 1; local a = co() until a == 10 + assert(_G.XX == 20 and c >= 5) + _G.X = nil; _G.XX = nil + + do + -- testing debug library on a coroutine suspended inside a hook + -- (bug in 5.2/5.3) + c = coroutine.create(function (a, ...) + T.sethook("yield 0", "l") -- will yield on next two lines + assert(a == 10) + return ... + end) + + assert(coroutine.resume(c, 1, 2, 3)) -- start coroutine + local n,v = debug.getlocal(c, 0, 1) -- check its local + assert(n == "a" and v == 1) + assert(debug.setlocal(c, 0, 1, 10)) -- test 'setlocal' + local t = debug.getinfo(c, 0) -- test 'getinfo' + assert(t.currentline == t.linedefined + 1) + assert(not debug.getinfo(c, 1)) -- no other level + assert(coroutine.resume(c)) -- run next line + v = {coroutine.resume(c)} -- finish coroutine + assert(v[1] == true and v[2] == 2 and v[3] == 3 and v[4] == undef) + assert(not coroutine.resume(c)) + end + + do + -- testing debug library on last function in a suspended coroutine + -- (bug in 5.2/5.3) + local c = coroutine.create(function () T.testC("yield 1", 10, 20) end) + local a, b = coroutine.resume(c) + assert(a and b == 20) + assert(debug.getinfo(c, 0).linedefined == -1) + a, b = debug.getlocal(c, 0, 2) + assert(b == 10) + end + + + print "testing coroutine API" + + -- reusing a thread + assert(T.testC([[ + newthread # create thread + pushvalue 2 # push body + pushstring 'a a a' # push argument + xmove 0 3 2 # move values to new thread + resume -1, 1 # call it first time + pushstatus + xmove 3 0 0 # move results back to stack + setglobal X # result + setglobal Y # status + pushvalue 2 # push body (to call it again) + pushstring 'b b b' + xmove 0 3 2 + resume -1, 1 # call it again + pushstatus + xmove 3 0 0 + return 1 # return result + ]], function (...) return ... end) == 'b b b') + + assert(X == 'a a a' and Y == 'OK') + + + -- resuming running coroutine + C = coroutine.create(function () + return T.testC([[ + pushnum 10; + pushnum 20; + resume -3 2; + pushstatus + gettop; + return 3]], C) + end) + local a, b, c, d = coroutine.resume(C) + assert(a == true and string.find(b, "non%-suspended") and + c == "ERRRUN" and d == 4) + + a, b, c, d = T.testC([[ + rawgeti R 1 # get main thread + pushnum 10; + pushnum 20; + resume -3 2; + pushstatus + gettop; + return 4]]) + assert(a == coroutine.running() and string.find(b, "non%-suspended") and + c == "ERRRUN" and d == 4) + + + -- using a main thread as a coroutine + local state = T.newstate() + T.loadlib(state) + + assert(T.doremote(state, [[ + coroutine = require'coroutine'; + X = function (x) coroutine.yield(x, 'BB'); return 'CC' end; + return 'ok']])) + + t = table.pack(T.testC(state, [[ + rawgeti R 1 # get main thread + pushstring 'XX' + getglobal X # get function for body + pushstring AA # arg + resume 1 1 # 'resume' shadows previous stack! + gettop + setglobal T # top + setglobal B # second yielded value + setglobal A # fist yielded value + rawgeti R 1 # get main thread + pushnum 5 # arg (noise) + resume 1 1 # after coroutine ends, previous stack is back + pushstatus + return * + ]])) + assert(t.n == 4 and t[2] == 'XX' and t[3] == 'CC' and t[4] == 'OK') + assert(T.doremote(state, "return T") == '2') + assert(T.doremote(state, "return A") == 'AA') + assert(T.doremote(state, "return B") == 'BB') + + T.closestate(state) + + print'+' + +end + + +-- leaving a pending coroutine open +_X = coroutine.wrap(function () + local a = 10 + local x = function () a = a+1 end + coroutine.yield() + end) + +_X() + + +if not _soft then + -- bug (stack overflow) + local j = 2^9 + local lim = 1000000 -- (C stack limit; assume 32-bit machine) + local t = {lim - 10, lim - 5, lim - 1, lim, lim + 1} + for i = 1, #t do + local j = t[i] + co = coroutine.create(function() + local t = {} + for i = 1, j do t[i] = i end + return table.unpack(t) + end) + local r, msg = coroutine.resume(co) + assert(not r) + end + co = nil +end + + +assert(coroutine.running() == main) + +print"+" + + +print"testing yields inside metamethods" + +local function val(x) + if type(x) == "table" then return x.x else return x end +end + +local mt = { + __eq = function(a,b) coroutine.yield(nil, "eq"); return val(a) == val(b) end, + __lt = function(a,b) coroutine.yield(nil, "lt"); return val(a) < val(b) end, + __le = function(a,b) coroutine.yield(nil, "le"); return a - b <= 0 end, + __add = function(a,b) coroutine.yield(nil, "add"); + return val(a) + val(b) end, + __sub = function(a,b) coroutine.yield(nil, "sub"); return val(a) - val(b) end, + __mul = function(a,b) coroutine.yield(nil, "mul"); return val(a) * val(b) end, + __div = function(a,b) coroutine.yield(nil, "div"); return val(a) / val(b) end, + __idiv = function(a,b) coroutine.yield(nil, "idiv"); + return val(a) // val(b) end, + __pow = function(a,b) coroutine.yield(nil, "pow"); return val(a) ^ val(b) end, + __mod = function(a,b) coroutine.yield(nil, "mod"); return val(a) % val(b) end, + __unm = function(a,b) coroutine.yield(nil, "unm"); return -val(a) end, + __bnot = function(a,b) coroutine.yield(nil, "bnot"); return ~val(a) end, + __shl = function(a,b) coroutine.yield(nil, "shl"); + return val(a) << val(b) end, + __shr = function(a,b) coroutine.yield(nil, "shr"); + return val(a) >> val(b) end, + __band = function(a,b) + coroutine.yield(nil, "band") + return val(a) & val(b) + end, + __bor = function(a,b) coroutine.yield(nil, "bor"); + return val(a) | val(b) end, + __bxor = function(a,b) coroutine.yield(nil, "bxor"); + return val(a) ~ val(b) end, + + __concat = function(a,b) + coroutine.yield(nil, "concat"); + return val(a) .. val(b) + end, + __index = function (t,k) coroutine.yield(nil, "idx"); return t.k[k] end, + __newindex = function (t,k,v) coroutine.yield(nil, "nidx"); t.k[k] = v end, +} + + +local function new (x) + return setmetatable({x = x, k = {}}, mt) +end + + +local a = new(10) +local b = new(12) +local c = new"hello" + +local function run (f, t) + local i = 1 + local c = coroutine.wrap(f) + while true do + local res, stat = c() + if res then assert(t[i] == undef); return res, t end + assert(stat == t[i]) + i = i + 1 + end +end + + +assert(run(function () if (a>=b) then return '>=' else return '<' end end, + {"le", "sub"}) == "<") +-- '<=' using '<' +mt.__le = nil +assert(run(function () if (a<=b) then return '<=' else return '>' end end, + {"lt"}) == "<=") +assert(run(function () if (a==b) then return '==' else return '~=' end end, + {"eq"}) == "~=") + +assert(run(function () return a & b + a end, {"add", "band"}) == 2) + +assert(run(function () return 1 + a end, {"add"}) == 11) +assert(run(function () return a - 25 end, {"sub"}) == -15) +assert(run(function () return 2 * a end, {"mul"}) == 20) +assert(run(function () return a ^ 2 end, {"pow"}) == 100) +assert(run(function () return a / 2 end, {"div"}) == 5) +assert(run(function () return a % 6 end, {"mod"}) == 4) +assert(run(function () return a // 3 end, {"idiv"}) == 3) + +assert(run(function () return a + b end, {"add"}) == 22) +assert(run(function () return a - b end, {"sub"}) == -2) +assert(run(function () return a * b end, {"mul"}) == 120) +assert(run(function () return a ^ b end, {"pow"}) == 10^12) +assert(run(function () return a / b end, {"div"}) == 10/12) +assert(run(function () return a % b end, {"mod"}) == 10) +assert(run(function () return a // b end, {"idiv"}) == 0) + + +assert(run(function () return a % b end, {"mod"}) == 10) + +assert(run(function () return ~a & b end, {"bnot", "band"}) == ~10 & 12) +assert(run(function () return a | b end, {"bor"}) == 10 | 12) +assert(run(function () return a ~ b end, {"bxor"}) == 10 ~ 12) +assert(run(function () return a << b end, {"shl"}) == 10 << 12) +assert(run(function () return a >> b end, {"shr"}) == 10 >> 12) + +assert(run(function () return 10 & b end, {"band"}) == 10 & 12) +assert(run(function () return a | 2 end, {"bor"}) == 10 | 2) +assert(run(function () return a ~ 2 end, {"bxor"}) == 10 ~ 2) + +assert(run(function () return a..b end, {"concat"}) == "1012") + +assert(run(function() return a .. b .. c .. a end, + {"concat", "concat", "concat"}) == "1012hello10") + +assert(run(function() return "a" .. "b" .. a .. "c" .. c .. b .. "x" end, + {"concat", "concat", "concat"}) == "ab10chello12x") + + +do -- a few more tests for comparsion operators + local mt1 = { + __le = function (a,b) + coroutine.yield(10) + return (val(a) <= val(b)) + end, + __lt = function (a,b) + coroutine.yield(10) + return val(a) < val(b) + end, + } + local mt2 = { __lt = mt1.__lt } -- no __le + + local function run (f) + local co = coroutine.wrap(f) + local res + repeat + res = co() + until res ~= 10 + return res + end + + local function test () + local a1 = setmetatable({x=1}, mt1) + local a2 = setmetatable({x=2}, mt2) + assert(a1 < a2) + assert(a1 <= a2) + assert(1 < a2) + assert(1 <= a2) + assert(2 > a1) + assert(2 >= a2) + return true + end + + run(test) + +end + +assert(run(function () + a.BB = print + return a.BB + end, {"nidx", "idx"}) == print) + +-- getuptable & setuptable +do local _ENV = _ENV + f = function () AAA = BBB + 1; return AAA end +end +g = new(10); g.k.BBB = 10; +debug.setupvalue(f, 1, g) +assert(run(f, {"idx", "nidx", "idx"}) == 11) +assert(g.k.AAA == 11) + +print"+" + +print"testing yields inside 'for' iterators" + +local f = function (s, i) + if i%2 == 0 then coroutine.yield(nil, "for") end + if i < s then return i + 1 end + end + +assert(run(function () + local s = 0 + for i in f, 4, 0 do s = s + i end + return s + end, {"for", "for", "for"}) == 10) + + + +-- tests for coroutine API +if T==nil then + (Message or print)('\n >>> testC not active: skipping coroutine API tests <<<\n') + return +end + +print('testing coroutine API') + +local function apico (...) + local x = {...} + return coroutine.wrap(function () + return T.testC(table.unpack(x)) + end) +end + +local a = {apico( +[[ + pushstring errorcode + pcallk 1 0 2; + invalid command (should not arrive here) +]], +[[return *]], +"stackmark", +error +)()} +assert(#a == 4 and + a[3] == "stackmark" and + a[4] == "errorcode" and + _G.status == "ERRRUN" and + _G.ctx == 2) -- 'ctx' to pcallk + +local co = apico( + "pushvalue 2; pushnum 10; pcallk 1 2 3; invalid command;", + coroutine.yield, + "getglobal status; getglobal ctx; pushvalue 2; pushstring a; pcallk 1 0 4; invalid command", + "getglobal status; getglobal ctx; return *") + +assert(co() == 10) +assert(co(20, 30) == 'a') +a = {co()} +assert(#a == 10 and + a[2] == coroutine.yield and + a[5] == 20 and a[6] == 30 and + a[7] == "YIELD" and a[8] == 3 and + a[9] == "YIELD" and a[10] == 4) +assert(not pcall(co)) -- coroutine is dead now + + +f = T.makeCfunc("pushnum 3; pushnum 5; yield 1;") +co = coroutine.wrap(function () + assert(f() == 23); assert(f() == 23); return 10 +end) +assert(co(23,16) == 5) +assert(co(23,16) == 5) +assert(co(23,16) == 10) + + +-- testing coroutines with C bodies +f = T.makeCfunc([[ + pushnum 102 + yieldk 1 U2 + cannot be here! +]], +[[ # continuation + pushvalue U3 # accessing upvalues inside a continuation + pushvalue U4 + return * +]], 23, "huu") + +x = coroutine.wrap(f) +assert(x() == 102) +eqtab({x()}, {23, "huu"}) + + +f = T.makeCfunc[[pushstring 'a'; pushnum 102; yield 2; ]] + +a, b, c, d = T.testC([[newthread; pushvalue 2; xmove 0 3 1; resume 3 0; + pushstatus; xmove 3 0 0; resume 3 0; pushstatus; + return 4; ]], f) + +assert(a == 'YIELD' and b == 'a' and c == 102 and d == 'OK') + + +-- testing chain of suspendable C calls + +local count = 3 -- number of levels + +f = T.makeCfunc([[ + remove 1; # remove argument + pushvalue U3; # get selection function + call 0 1; # call it (result is 'f' or 'yield') + pushstring hello # single argument for selected function + pushupvalueindex 2; # index of continuation program + callk 1 -1 .; # call selected function + errorerror # should never arrive here +]], +[[ + # continuation program + pushnum 34 # return value + return * # return all results +]], +function () -- selection function + count = count - 1 + if count == 0 then return coroutine.yield + else return f + end +end +) + +co = coroutine.wrap(function () return f(nil) end) +assert(co() == "hello") -- argument to 'yield' +a = {co()} +-- three '34's (one from each pending C call) +assert(#a == 3 and a[1] == a[2] and a[2] == a[3] and a[3] == 34) + + +-- testing yields with continuations + +co = coroutine.wrap(function (...) return + T.testC([[ # initial function + yieldk 1 2 + cannot be here! + ]], + [[ # 1st continuation + yieldk 0 3 + cannot be here! + ]], + [[ # 2nd continuation + yieldk 0 4 + cannot be here! + ]], + [[ # 3th continuation + pushvalue 6 # function which is last arg. to 'testC' here + pushnum 10; pushnum 20; + pcall 2 0 0 # call should throw an error and return to next line + pop 1 # remove error message + pushvalue 6 + getglobal status; getglobal ctx + pcallk 2 2 5 # call should throw an error and jump to continuation + cannot be here! + ]], + [[ # 4th (and last) continuation + return * + ]], + -- function called by 3th continuation + function (a,b) x=a; y=b; error("errmsg") end, + ... +) +end) + +local a = {co(3,4,6)} +assert(a[1] == 6 and a[2] == undef) +a = {co()}; assert(a[1] == undef and _G.status == "YIELD" and _G.ctx == 2) +a = {co()}; assert(a[1] == undef and _G.status == "YIELD" and _G.ctx == 3) +a = {co(7,8)}; +-- original arguments +assert(type(a[1]) == 'string' and type(a[2]) == 'string' and + type(a[3]) == 'string' and type(a[4]) == 'string' and + type(a[5]) == 'string' and type(a[6]) == 'function') +-- arguments left from fist resume +assert(a[7] == 3 and a[8] == 4) +-- arguments to last resume +assert(a[9] == 7 and a[10] == 8) +-- error message and nothing more +assert(a[11]:find("errmsg") and #a == 11) +-- check arguments to pcallk +assert(x == "YIELD" and y == 4) + +assert(not pcall(co)) -- coroutine should be dead + + +-- bug in nCcalls +local co = coroutine.wrap(function () + local a = {pcall(pcall,pcall,pcall,pcall,pcall,pcall,pcall,error,"hi")} + return pcall(assert, table.unpack(a)) +end) + +local a = {co()} +assert(a[10] == "hi") + +print'OK' diff --git a/testes/db.lua b/testes/db.lua new file mode 100644 index 0000000000..36d1cdaab6 --- /dev/null +++ b/testes/db.lua @@ -0,0 +1,948 @@ +-- $Id: db.lua,v 1.90 2018/04/02 17:55:58 roberto Exp $ +-- See Copyright Notice in file all.lua + +-- testing debug library + +local debug = require "debug" + +local function dostring(s) return assert(load(s))() end + +print"testing debug library and debug information" + +do +local a=1 +end + +assert(not debug.gethook()) + +local testline = 19 -- line where 'test' is defined +function test (s, l, p) -- this must be line 19 + collectgarbage() -- avoid gc during trace + local function f (event, line) + assert(event == 'line') + local l = table.remove(l, 1) + if p then print(l, line) end + assert(l == line, "wrong trace!!") + end + debug.sethook(f,"l"); load(s)(); debug.sethook() + assert(#l == 0) +end + + +do + assert(not pcall(debug.getinfo, print, "X")) -- invalid option + assert(not debug.getinfo(1000)) -- out of range level + assert(not debug.getinfo(-1)) -- out of range level + local a = debug.getinfo(print) + assert(a.what == "C" and a.short_src == "[C]") + a = debug.getinfo(print, "L") + assert(a.activelines == nil) + local b = debug.getinfo(test, "SfL") + assert(b.name == nil and b.what == "Lua" and b.linedefined == testline and + b.lastlinedefined == b.linedefined + 10 and + b.func == test and not string.find(b.short_src, "%[")) + assert(b.activelines[b.linedefined + 1] and + b.activelines[b.lastlinedefined]) + assert(not b.activelines[b.linedefined] and + not b.activelines[b.lastlinedefined + 1]) +end + + +-- test file and string names truncation +a = "function f () end" +local function dostring (s, x) return load(s, x)() end +dostring(a) +assert(debug.getinfo(f).short_src == string.format('[string "%s"]', a)) +dostring(a..string.format("; %s\n=1", string.rep('p', 400))) +assert(string.find(debug.getinfo(f).short_src, '^%[string [^\n]*%.%.%."%]$')) +dostring(a..string.format("; %s=1", string.rep('p', 400))) +assert(string.find(debug.getinfo(f).short_src, '^%[string [^\n]*%.%.%."%]$')) +dostring("\n"..a) +assert(debug.getinfo(f).short_src == '[string "..."]') +dostring(a, "") +assert(debug.getinfo(f).short_src == '[string ""]') +dostring(a, "@xuxu") +assert(debug.getinfo(f).short_src == "xuxu") +dostring(a, "@"..string.rep('p', 1000)..'t') +assert(string.find(debug.getinfo(f).short_src, "^%.%.%.p*t$")) +dostring(a, "=xuxu") +assert(debug.getinfo(f).short_src == "xuxu") +dostring(a, string.format("=%s", string.rep('x', 500))) +assert(string.find(debug.getinfo(f).short_src, "^x*$")) +dostring(a, "=") +assert(debug.getinfo(f).short_src == "") +a = nil; f = nil; + + +repeat + local g = {x = function () + local a = debug.getinfo(2) + assert(a.name == 'f' and a.namewhat == 'local') + a = debug.getinfo(1) + assert(a.name == 'x' and a.namewhat == 'field') + return 'xixi' + end} + local f = function () return 1+1 and (not 1 or g.x()) end + assert(f() == 'xixi') + g = debug.getinfo(f) + assert(g.what == "Lua" and g.func == f and g.namewhat == "" and not g.name) + + function f (x, name) -- local! + name = name or 'f' + local a = debug.getinfo(1) + assert(a.name == name and a.namewhat == 'local') + return x + end + + -- breaks in different conditions + if 3>4 then break end; f() + if 3<4 then a=1 else break end; f() + while 1 do local x=10; break end; f() + local b = 1 + if 3>4 then return math.sin(1) end; f() + a = 3<4; f() + a = 3<4 or 1; f() + repeat local x=20; if 4>3 then f() else break end; f() until 1 + g = {} + f(g).x = f(2) and f(10)+f(9) + assert(g.x == f(19)) + function g(x) if not x then return 3 end return (x('a', 'x')) end + assert(g(f) == 'a') +until 1 + +test([[if +math.sin(1) +then + a=1 +else + a=2 +end +]], {2,3,4,7}) + +test([[-- +if nil then + a=1 +else + a=2 +end +]], {2,5,6}) + +test([[a=1 +repeat + a=a+1 +until a==3 +]], {1,3,4,3,4}) + +test([[ do + return +end +]], {2}) + +test([[local a +a=1 +while a<=3 do + a=a+1 +end +]], {1,2,3,4,3,4,3,4,3,5}) + +test([[while math.sin(1) do + if math.sin(1) + then break + end +end +a=1]], {1,2,3,6}) + +test([[for i=1,3 do + a=i +end +]], {1,2,1,2,1,2,1,3}) + +test([[for i,v in pairs{'a','b'} do + a=tostring(i) .. v +end +]], {1,2,1,2,1,3}) + +test([[for i=1,4 do a=1 end]], {1,1,1,1,1}) + + +do -- testing line info/trace with large gaps in source + + local a = {1, 2, 3, 10, 124, 125, 126, 127, 128, 129, 130, + 255, 256, 257, 500, 1000} + local s = [[ + local b = {10} + a = b[1] X + Y b[1] + b = 4 + ]] + for _, i in ipairs(a) do + local subs = {X = string.rep("\n", i)} + for _, j in ipairs(a) do + subs.Y = string.rep("\n", j) + local s = string.gsub(s, "[XY]", subs) + test(s, {1, 2 + i, 2 + i + j, 2 + i, 2 + i + j, 3 + i + j}) + end + end +end + +print'+' + +-- invalid levels in [gs]etlocal +assert(not pcall(debug.getlocal, 20, 1)) +assert(not pcall(debug.setlocal, -1, 1, 10)) + + +-- parameter names +local function foo (a,b,...) local d, e end +local co = coroutine.create(foo) + +assert(debug.getlocal(foo, 1) == 'a') +assert(debug.getlocal(foo, 2) == 'b') +assert(not debug.getlocal(foo, 3)) +assert(debug.getlocal(co, foo, 1) == 'a') +assert(debug.getlocal(co, foo, 2) == 'b') +assert(not debug.getlocal(co, foo, 3)) + +assert(not debug.getlocal(print, 1)) + + +local function foo () return (debug.getlocal(1, -1)) end +assert(not foo(10)) + + +-- varargs +local function foo (a, ...) + local t = table.pack(...) + for i = 1, t.n do + local n, v = debug.getlocal(1, -i) + assert(n == "(*vararg)" and v == t[i]) + end + assert(not debug.getlocal(1, -(t.n + 1))) + assert(not debug.setlocal(1, -(t.n + 1), 30)) + if t.n > 0 then + (function (x) + assert(debug.setlocal(2, -1, x) == "(*vararg)") + assert(debug.setlocal(2, -t.n, x) == "(*vararg)") + end)(430) + assert(... == 430) + end +end + +foo() +foo(print) +foo(200, 3, 4) +local a = {} +for i = 1, (_soft and 100 or 1000) do a[i] = i end +foo(table.unpack(a)) +a = nil + + + +do -- test hook presence in debug info + assert(not debug.gethook()) + local count = 0 + local function f () + assert(debug.getinfo(1).namewhat == "hook") + local sndline = string.match(debug.traceback(), "\n(.-)\n") + assert(string.find(sndline, "hook")) + count = count + 1 + end + debug.sethook(f, "l") + local a = 0 + _ENV.a = a + a = 1 + debug.sethook() + assert(count == 4) +end + + +a = {}; L = nil +local glob = 1 +local oldglob = glob +debug.sethook(function (e,l) + collectgarbage() -- force GC during a hook + local f, m, c = debug.gethook() + assert(m == 'crl' and c == 0) + if e == "line" then + if glob ~= oldglob then + L = l-1 -- get the first line where "glob" has changed + oldglob = glob + end + elseif e == "call" then + local f = debug.getinfo(2, "f").func + a[f] = 1 + else assert(e == "return") + end +end, "crl") + + +function f(a,b) + collectgarbage() + local _, x = debug.getlocal(1, 1) + local _, y = debug.getlocal(1, 2) + assert(x == a and y == b) + assert(debug.setlocal(2, 3, "pera") == "AA".."AA") + assert(debug.setlocal(2, 4, "ma") == "B") + x = debug.getinfo(2) + assert(x.func == g and x.what == "Lua" and x.name == 'g' and + x.nups == 2 and string.find(x.source, "^@.*db%.lua$")) + glob = glob+1 + assert(debug.getinfo(1, "l").currentline == L+1) + assert(debug.getinfo(1, "l").currentline == L+2) +end + +function foo() + glob = glob+1 + assert(debug.getinfo(1, "l").currentline == L+1) +end; foo() -- set L +-- check line counting inside strings and empty lines + +_ = 'alo\ +alo' .. [[ + +]] +--[[ +]] +assert(debug.getinfo(1, "l").currentline == L+11) -- check count of lines + + +function g (...) + local arg = {...} + do local a,b,c; a=math.sin(40); end + local feijao + local AAAA,B = "xuxu", "mamo" + f(AAAA,B) + assert(AAAA == "pera" and B == "ma") + do + local B = 13 + local x,y = debug.getlocal(1,5) + assert(x == 'B' and y == 13) + end +end + +g() + + +assert(a[f] and a[g] and a[assert] and a[debug.getlocal] and not a[print]) + + +-- tests for manipulating non-registered locals (C and Lua temporaries) + +local n, v = debug.getlocal(0, 1) +assert(v == 0 and n == "(*temporary)") +local n, v = debug.getlocal(0, 2) +assert(v == 2 and n == "(*temporary)") +assert(not debug.getlocal(0, 3)) +assert(not debug.getlocal(0, 0)) + +function f() + assert(select(2, debug.getlocal(2,3)) == 1) + assert(not debug.getlocal(2,4)) + debug.setlocal(2, 3, 10) + return 20 +end + +function g(a,b) return (a+1) + f() end + +assert(g(0,0) == 30) + + +debug.sethook(nil); +assert(debug.gethook() == nil) + + +-- minimal tests for setuservalue/getuservalue +do + assert(debug.setuservalue(io.stdin, 10) == nil) + local a, b = debug.getuservalue(io.stdin, 10) + assert(a == nil and not b) +end + +-- testing iteraction between multiple values x hooks +do + local function f(...) return 3, ... end + local count = 0 + local a = {} + for i = 1, 100 do a[i] = i end + debug.sethook(function () count = count + 1 end, "", 1) + local t = {table.unpack(a)} + assert(#t == 100) + t = {table.unpack(a, 1, 3)} + assert(#t == 3) + t = {f(table.unpack(a, 1, 30))} + assert(#t == 31) +end + + +-- testing access to function arguments + +local function collectlocals (level) + local tab = {} + for i = 1, math.huge do + local n, v = debug.getlocal(level + 1, i) + if not (n and string.find(n, "^[a-zA-Z0-9_]+$")) then + break -- consider only real variables + end + tab[n] = v + end + return tab +end + + +X = nil +a = {} +function a:f (a, b, ...) local arg = {...}; local c = 13 end +debug.sethook(function (e) + assert(e == "call") + dostring("XX = 12") -- test dostring inside hooks + -- testing errors inside hooks + assert(not pcall(load("a='joao'+1"))) + debug.sethook(function (e, l) + assert(debug.getinfo(2, "l").currentline == l) + local f,m,c = debug.gethook() + assert(e == "line") + assert(m == 'l' and c == 0) + debug.sethook(nil) -- hook is called only once + assert(not X) -- check that + X = collectlocals(2) + end, "l") +end, "c") + +a:f(1,2,3,4,5) +assert(X.self == a and X.a == 1 and X.b == 2 and X.c == nil) +assert(XX == 12) +assert(debug.gethook() == nil) + + +-- testing access to local variables in return hook (bug in 5.2) +do + local X = false + + local function foo (a, b, ...) + do local x,y,z end + local c, d = 10, 20 + return + end + + local function aux () + if debug.getinfo(2).name == "foo" then + X = true -- to signal that it found 'foo' + local tab = {a = 100, b = 200, c = 10, d = 20} + for n, v in pairs(collectlocals(2)) do + assert(tab[n] == v) + tab[n] = undef + end + assert(next(tab) == nil) -- 'tab' must be empty + end + end + + debug.sethook(aux, "r"); foo(100, 200); debug.sethook() + assert(X) + +end + + +local function eqseq (t1, t2) + assert(#t1 == #t2) + for i = 1, #t1 do + assert(t1[i] == t2[i]) + end +end + + +do print("testing inspection of parameters/returned values") + local on = false + local inp, out + + local function hook (event) + if not on then return end + local ar = debug.getinfo(2, "ruS") + local t = {} + for i = ar.ftransfer, ar.ftransfer + ar.ntransfer - 1 do + local _, v = debug.getlocal(2, i) + t[#t + 1] = v + end + if event == "return" then + out = t + else + inp = t + end + end + + debug.sethook(hook, "cr") + + on = true; math.sin(3); on = false + eqseq(inp, {3}); eqseq(out, {math.sin(3)}) + + on = true; select(2, 10, 20, 30, 40); on = false + eqseq(inp, {2, 10, 20, 30, 40}); eqseq(out, {20, 30, 40}) + + local function foo (a, ...) return ... end + local function foo1 () on = not on; return foo(20, 10, 0) end + foo1(); on = false + eqseq(inp, {20}); eqseq(out, {10, 0}) + + debug.sethook() +end + + + +-- testing upvalue access +local function getupvalues (f) + local t = {} + local i = 1 + while true do + local name, value = debug.getupvalue(f, i) + if not name then break end + assert(not t[name]) + t[name] = value + i = i + 1 + end + return t +end + +local a,b,c = 1,2,3 +local function foo1 (a) b = a; return c end +local function foo2 (x) a = x; return c+b end +assert(not debug.getupvalue(foo1, 3)) +assert(not debug.getupvalue(foo1, 0)) +assert(not debug.setupvalue(foo1, 3, "xuxu")) +local t = getupvalues(foo1) +assert(t.a == nil and t.b == 2 and t.c == 3) +t = getupvalues(foo2) +assert(t.a == 1 and t.b == 2 and t.c == 3) +assert(debug.setupvalue(foo1, 1, "xuxu") == "b") +assert(({debug.getupvalue(foo2, 3)})[2] == "xuxu") +-- upvalues of C functions are allways "called" "" (the empty string) +assert(debug.getupvalue(string.gmatch("x", "x"), 1) == "") + + +-- testing count hooks +local a=0 +debug.sethook(function (e) a=a+1 end, "", 1) +a=0; for i=1,1000 do end; assert(1000 < a and a < 1012) +debug.sethook(function (e) a=a+1 end, "", 4) +a=0; for i=1,1000 do end; assert(250 < a and a < 255) +local f,m,c = debug.gethook() +assert(m == "" and c == 4) +debug.sethook(function (e) a=a+1 end, "", 4000) +a=0; for i=1,1000 do end; assert(a == 0) + +do + debug.sethook(print, "", 2^24 - 1) -- count upperbound + local f,m,c = debug.gethook() + assert(({debug.gethook()})[3] == 2^24 - 1) +end + +debug.sethook() + + +-- tests for tail calls +local function f (x) + if x then + assert(debug.getinfo(1, "S").what == "Lua") + assert(debug.getinfo(1, "t").istailcall == true) + local tail = debug.getinfo(2) + assert(tail.func == g1 and tail.istailcall == true) + assert(debug.getinfo(3, "S").what == "main") + print"+" + end +end + +function g(x) return f(x) end + +function g1(x) g(x) end + +local function h (x) local f=g1; return f(x) end + +h(true) + +local b = {} +debug.sethook(function (e) table.insert(b, e) end, "cr") +h(false) +debug.sethook() +local res = {"return", -- first return (from sethook) + "call", "tail call", "call", "tail call", + "return", "return", + "call", -- last call (to sethook) +} +for i = 1, #res do assert(res[i] == table.remove(b, 1)) end + +b = 0 +debug.sethook(function (e) + if e == "tail call" then + b = b + 1 + assert(debug.getinfo(2, "t").istailcall == true) + else + assert(debug.getinfo(2, "t").istailcall == false) + end + end, "c") +h(false) +debug.sethook() +assert(b == 2) -- two tail calls + +lim = _soft and 3000 or 30000 +local function foo (x) + if x==0 then + assert(debug.getinfo(2).what == "main") + local info = debug.getinfo(1) + assert(info.istailcall == true and info.func == foo) + else return foo(x-1) + end +end + +foo(lim) + + +print"+" + + +-- testing local function information +co = load[[ + local A = function () + return x + end + return +]] + +local a = 0 +-- 'A' should be visible to debugger only after its complete definition +debug.sethook(function (e, l) + if l == 3 then a = a + 1; assert(debug.getlocal(2, 1) == "(*temporary)") + elseif l == 4 then a = a + 1; assert(debug.getlocal(2, 1) == "A") + end +end, "l") +co() -- run local function definition +debug.sethook() -- turn off hook +assert(a == 2) -- ensure all two lines where hooked + +-- testing traceback + +assert(debug.traceback(print) == print) +assert(debug.traceback(print, 4) == print) +assert(string.find(debug.traceback("hi", 4), "^hi\n")) +assert(string.find(debug.traceback("hi"), "^hi\n")) +assert(not string.find(debug.traceback("hi"), "'debug.traceback'")) +assert(string.find(debug.traceback("hi", 0), "'debug.traceback'")) +assert(string.find(debug.traceback(), "^stack traceback:\n")) + +do -- C-function names in traceback + local st, msg = (function () return pcall end)()(debug.traceback) + assert(st == true and string.find(msg, "pcall")) +end + + +-- testing nparams, nups e isvararg +local t = debug.getinfo(print, "u") +assert(t.isvararg == true and t.nparams == 0 and t.nups == 0) + +t = debug.getinfo(function (a,b,c) end, "u") +assert(t.isvararg == false and t.nparams == 3 and t.nups == 0) + +t = debug.getinfo(function (a,b,...) return t[a] end, "u") +assert(t.isvararg == true and t.nparams == 2 and t.nups == 1) + +t = debug.getinfo(1) -- main +assert(t.isvararg == true and t.nparams == 0 and t.nups == 1 and + debug.getupvalue(t.func, 1) == "_ENV") + + + + +-- testing debugging of coroutines + +local function checktraceback (co, p, level) + local tb = debug.traceback(co, nil, level) + local i = 0 + for l in string.gmatch(tb, "[^\n]+\n?") do + assert(i == 0 or string.find(l, p[i])) + i = i+1 + end + assert(p[i] == undef) +end + + +local function f (n) + if n > 0 then f(n-1) + else coroutine.yield() end +end + +local co = coroutine.create(f) +coroutine.resume(co, 3) +checktraceback(co, {"yield", "db.lua", "db.lua", "db.lua", "db.lua"}) +checktraceback(co, {"db.lua", "db.lua", "db.lua", "db.lua"}, 1) +checktraceback(co, {"db.lua", "db.lua", "db.lua"}, 2) +checktraceback(co, {"db.lua"}, 4) +checktraceback(co, {}, 40) + + +co = coroutine.create(function (x) + local a = 1 + coroutine.yield(debug.getinfo(1, "l")) + coroutine.yield(debug.getinfo(1, "l").currentline) + return a + end) + +local tr = {} +local foo = function (e, l) if l then table.insert(tr, l) end end +debug.sethook(co, foo, "lcr") + +local _, l = coroutine.resume(co, 10) +local x = debug.getinfo(co, 1, "lfLS") +assert(x.currentline == l.currentline and x.activelines[x.currentline]) +assert(type(x.func) == "function") +for i=x.linedefined + 1, x.lastlinedefined do + assert(x.activelines[i]) + x.activelines[i] = undef +end +assert(next(x.activelines) == nil) -- no 'extra' elements +assert(not debug.getinfo(co, 2)) +local a,b = debug.getlocal(co, 1, 1) +assert(a == "x" and b == 10) +a,b = debug.getlocal(co, 1, 2) +assert(a == "a" and b == 1) +debug.setlocal(co, 1, 2, "hi") +assert(debug.gethook(co) == foo) +assert(#tr == 2 and + tr[1] == l.currentline-1 and tr[2] == l.currentline) + +a,b,c = pcall(coroutine.resume, co) +assert(a and b and c == l.currentline+1) +checktraceback(co, {"yield", "in function <"}) + +a,b = coroutine.resume(co) +assert(a and b == "hi") +assert(#tr == 4 and tr[4] == l.currentline+2) +assert(debug.gethook(co) == foo) +assert(not debug.gethook()) +checktraceback(co, {}) + + +-- check get/setlocal in coroutines +co = coroutine.create(function (x) + local a, b = coroutine.yield(x) + assert(a == 100 and b == nil) + return x +end) +a, b = coroutine.resume(co, 10) +assert(a and b == 10) +a, b = debug.getlocal(co, 1, 1) +assert(a == "x" and b == 10) +assert(not debug.getlocal(co, 1, 5)) +assert(debug.setlocal(co, 1, 1, 30) == "x") +assert(not debug.setlocal(co, 1, 5, 40)) +a, b = coroutine.resume(co, 100) +assert(a and b == 30) + + +-- check traceback of suspended (or dead with error) coroutines + +function f(i) if i==0 then error(i) else coroutine.yield(); f(i-1) end end + +co = coroutine.create(function (x) f(x) end) +a, b = coroutine.resume(co, 3) +t = {"'coroutine.yield'", "'f'", "in function <"} +while coroutine.status(co) == "suspended" do + checktraceback(co, t) + a, b = coroutine.resume(co) + table.insert(t, 2, "'f'") -- one more recursive call to 'f' +end +t[1] = "'error'" +checktraceback(co, t) + + +-- test acessing line numbers of a coroutine from a resume inside +-- a C function (this is a known bug in Lua 5.0) + +local function g(x) + coroutine.yield(x) +end + +local function f (i) + debug.sethook(function () end, "l") + for j=1,1000 do + g(i+j) + end +end + +local co = coroutine.wrap(f) +co(10) +pcall(co) +pcall(co) + + +assert(type(debug.getregistry()) == "table") + + +-- test tagmethod information +local a = {} +local function f (t) + local info = debug.getinfo(1); + assert(info.namewhat == "metamethod") + a.op = info.name + return info.name +end +setmetatable(a, { + __index = f; __add = f; __div = f; __mod = f; __concat = f; __pow = f; + __mul = f; __idiv = f; __unm = f; __len = f; __sub = f; + __shl = f; __shr = f; __bor = f; __bxor = f; + __eq = f; __le = f; __lt = f; __unm = f; __len = f; __band = f; + __bnot = f; +}) + +local b = setmetatable({}, getmetatable(a)) + +assert(a[3] == "index" and a^3 == "pow" and a..a == "concat") +assert(a/3 == "div" and 3%a == "mod") +assert(a+3 == "add" and 3-a == "sub" and a*3 == "mul" and + -a == "unm" and #a == "len" and a&3 == "band") +assert(a|3 == "bor" and 3~a == "bxor" and a<<3 == "shift" and + a>>1 == "shift") +assert (a==b and a.op == "eq") +assert (a>=b and a.op == "order") +assert (a>b and a.op == "order") +assert(~a == "bnot") + +do -- testing for-iterator name + local function f() + assert(debug.getinfo(1).name == "for iterator") + end + + for i in f do end +end + + +do -- testing debug info for finalizers + local name = nil + + -- create a piece of garbage with a finalizer + setmetatable({}, {__gc = function () + local t = debug.getinfo(2) -- get callee information + assert(t.namewhat == "metamethod") + name = t.name + end}) + + -- repeat until previous finalizer runs (setting 'name') + repeat local a = {} until name + assert(name == "__gc") +end + + +do + print("testing traceback sizes") + + local function countlines (s) + return select(2, string.gsub(s, "\n", "")) + end + + local function deep (lvl, n) + if lvl == 0 then + return (debug.traceback("message", n)) + else + return (deep(lvl-1, n)) + end + end + + local function checkdeep (total, start) + local s = deep(total, start) + local rest = string.match(s, "^message\nstack traceback:\n(.*)$") + local cl = countlines(rest) + -- at most 10 lines in first part, 11 in second, plus '...' + assert(cl <= 10 + 11 + 1) + local brk = string.find(rest, "%.%.%.") + if brk then -- does message have '...'? + local rest1 = string.sub(rest, 1, brk) + local rest2 = string.sub(rest, brk, #rest) + assert(countlines(rest1) == 10 and countlines(rest2) == 11) + else + assert(cl == total - start + 2) + end + end + + for d = 1, 51, 10 do + for l = 1, d do + -- use coroutines to ensure complete control of the stack + coroutine.wrap(checkdeep)(d, l) + end + end + +end + + +print("testing debug functions on chunk without debug info") +prog = [[-- program to be loaded without debug information +local debug = require'debug' +local a = 12 -- a local variable + +local n, v = debug.getlocal(1, 1) +assert(n == "(*temporary)" and v == debug) -- unkown name but known value +n, v = debug.getlocal(1, 2) +assert(n == "(*temporary)" and v == 12) -- unkown name but known value + +-- a function with an upvalue +local f = function () local x; return a end +n, v = debug.getupvalue(f, 1) +assert(n == "(*no name)" and v == 12) +assert(debug.setupvalue(f, 1, 13) == "(*no name)") +assert(a == 13) + +local t = debug.getinfo(f) +assert(t.name == nil and t.linedefined > 0 and + t.lastlinedefined == t.linedefined and + t.short_src == "?") +assert(debug.getinfo(1).currentline == -1) + +t = debug.getinfo(f, "L").activelines +assert(next(t) == nil) -- active lines are empty + +-- dump/load a function without debug info +f = load(string.dump(f)) + +t = debug.getinfo(f) +assert(t.name == nil and t.linedefined > 0 and + t.lastlinedefined == t.linedefined and + t.short_src == "?") +assert(debug.getinfo(1).currentline == -1) + +return a +]] + + +-- load 'prog' without debug info +local f = assert(load(string.dump(load(prog), true))) + +assert(f() == 13) + +do -- tests for 'source' in binary dumps + local prog = [[ + return function (x) + return function (y) + return x + y + end + end + ]] + local name = string.rep("x", 1000) + local p = assert(load(prog, name)) + -- load 'p' as a binary chunk with debug information + local c = string.dump(p) + assert(#c > 1000 and #c < 2000) -- no repetition of 'source' in dump + local f = assert(load(c)) + local g = f() + local h = g(3) + assert(h(5) == 8) + assert(debug.getinfo(f).source == name and -- all functions have 'source' + debug.getinfo(g).source == name and + debug.getinfo(h).source == name) + -- again, without debug info + local c = string.dump(p, true) + assert(#c < 500) -- no 'source' in dump + local f = assert(load(c)) + local g = f() + local h = g(30) + assert(h(50) == 80) + assert(debug.getinfo(f).source == '=?' and -- no function has 'source' + debug.getinfo(g).source == '=?' and + debug.getinfo(h).source == '=?') +end + +print"OK" + diff --git a/testes/errors.lua b/testes/errors.lua new file mode 100644 index 0000000000..63a7b740cd --- /dev/null +++ b/testes/errors.lua @@ -0,0 +1,554 @@ +-- $Id: errors.lua,v 1.97 2017/11/28 15:31:56 roberto Exp $ +-- See Copyright Notice in file all.lua + +print("testing errors") + +local debug = require"debug" + +-- avoid problems with 'strict' module (which may generate other error messages) +local mt = getmetatable(_G) or {} +local oldmm = mt.__index +mt.__index = nil + +local function checkerr (msg, f, ...) + local st, err = pcall(f, ...) + assert(not st and string.find(err, msg)) +end + + +local function doit (s) + local f, msg = load(s) + if f == nil then return msg end + local cond, msg = pcall(f) + return (not cond) and msg +end + + +local function checkmessage (prog, msg) + local m = doit(prog) + assert(string.find(m, msg, 1, true)) +end + +local function checksyntax (prog, extra, token, line) + local msg = doit(prog) + if not string.find(token, "^<%a") and not string.find(token, "^char%(") + then token = "'"..token.."'" end + token = string.gsub(token, "(%p)", "%%%1") + local pt = string.format([[^%%[string ".*"%%]:%d: .- near %s$]], + line, token) + assert(string.find(msg, pt)) + assert(string.find(msg, msg, 1, true)) +end + + +-- test error message with no extra info +assert(doit("error('hi', 0)") == 'hi') + +-- test error message with no info +assert(doit("error()") == nil) + + +-- test common errors/errors that crashed in the past +assert(doit("table.unpack({}, 1, n=2^30)")) +assert(doit("a=math.sin()")) +assert(not doit("tostring(1)") and doit("tostring()")) +assert(doit"tonumber()") +assert(doit"repeat until 1; a") +assert(doit"return;;") +assert(doit"assert(false)") +assert(doit"assert(nil)") +assert(doit("function a (... , ...) end")) +assert(doit("function a (, ...) end")) +assert(doit("local t={}; t = t[#t] + 1")) + +checksyntax([[ + local a = {4 + +]], "'}' expected (to close '{' at line 1)", "", 3) + + +if not T then + (Message or print) + ('\n >>> testC not active: skipping memory message test <<<\n') +else + print "testing memory error message" + local a = {} + for i = 1, 10000 do a[i] = true end -- preallocate array + collectgarbage() + T.totalmem(T.totalmem() + 10000) + -- force a memory error (by a small margin) + local st, msg = pcall(function() + for i = 1, 100000 do a[i] = tostring(i) end + end) + T.totalmem(0) + assert(not st and msg == "not enough" .. " memory") +end + + +-- tests for better error messages + +checkmessage("a = {} + 1", "arithmetic") +checkmessage("a = {} | 1", "bitwise operation") +checkmessage("a = {} < 1", "attempt to compare") +checkmessage("a = {} <= 1", "attempt to compare") + +checkmessage("a=1; bbbb=2; a=math.sin(3)+bbbb(3)", "global 'bbbb'") +checkmessage("a={}; do local a=1 end a:bbbb(3)", "method 'bbbb'") +checkmessage("local a={}; a.bbbb(3)", "field 'bbbb'") +assert(not string.find(doit"a={13}; local bbbb=1; a[bbbb](3)", "'bbbb'")) +checkmessage("a={13}; local bbbb=1; a[bbbb](3)", "number") +checkmessage("a=(1)..{}", "a table value") + +checkmessage("a = #print", "length of a function value") +checkmessage("a = #3", "length of a number value") + +aaa = nil +checkmessage("aaa.bbb:ddd(9)", "global 'aaa'") +checkmessage("local aaa={bbb=1}; aaa.bbb:ddd(9)", "field 'bbb'") +checkmessage("local aaa={bbb={}}; aaa.bbb:ddd(9)", "method 'ddd'") +checkmessage("local a,b,c; (function () a = b+1.1 end)()", "upvalue 'b'") +assert(not doit"local aaa={bbb={ddd=next}}; aaa.bbb:ddd(nil)") + +-- upvalues being indexed do not go to the stack +checkmessage("local a,b,cc; (function () a = cc[1] end)()", "upvalue 'cc'") +checkmessage("local a,b,cc; (function () a.x = 1 end)()", "upvalue 'a'") + +checkmessage("local _ENV = {x={}}; a = a + 1", "global 'a'") + +checkmessage("b=1; local aaa={}; x=aaa+b", "local 'aaa'") +checkmessage("aaa={}; x=3.3/aaa", "global 'aaa'") +checkmessage("aaa=2; b=nil;x=aaa*b", "global 'b'") +checkmessage("aaa={}; x=-aaa", "global 'aaa'") + +-- short circuit +checkmessage("a=1; local a,bbbb=2,3; a = math.sin(1) and bbbb(3)", + "local 'bbbb'") +checkmessage("a=1; local a,bbbb=2,3; a = bbbb(1) or a(3)", "local 'bbbb'") +checkmessage("local a,b,c,f = 1,1,1; f((a and b) or c)", "local 'f'") +checkmessage("local a,b,c = 1,1,1; ((a and b) or c)()", "call a number value") +assert(not string.find(doit"aaa={}; x=(aaa or aaa)+(aaa and aaa)", "'aaa'")) +assert(not string.find(doit"aaa={}; (aaa or aaa)()", "'aaa'")) + +checkmessage("print(print < 10)", "function with number") +checkmessage("print(print < print)", "two function values") +checkmessage("print('10' < 10)", "string with number") +checkmessage("print(10 < '23')", "number with string") + +-- float->integer conversions +checkmessage("local a = 2.0^100; x = a << 2", "local a") +checkmessage("local a = 1 >> 2.0^100", "has no integer representation") +checkmessage("local a = 10.1 << 2.0^100", "has no integer representation") +checkmessage("local a = 2.0^100 & 1", "has no integer representation") +checkmessage("local a = 2.0^100 & 1e100", "has no integer representation") +checkmessage("local a = 2.0 | 1e40", "has no integer representation") +checkmessage("local a = 2e100 ~ 1", "has no integer representation") +checkmessage("string.sub('a', 2.0^100)", "has no integer representation") +checkmessage("string.rep('a', 3.3)", "has no integer representation") +checkmessage("return 6e40 & 7", "has no integer representation") +checkmessage("return 34 << 7e30", "has no integer representation") +checkmessage("return ~-3e40", "has no integer representation") +checkmessage("return ~-3.009", "has no integer representation") +checkmessage("return 3.009 & 1", "has no integer representation") +checkmessage("return 34 >> {}", "table value") +checkmessage("a = 24 // 0", "divide by zero") +checkmessage("a = 1 % 0", "'n%0'") + + +-- passing light userdata instead of full userdata +_G.D = debug +checkmessage([[ + -- create light udata + local x = D.upvalueid(function () return debug end, 1) + D.setuservalue(x, {}) +]], "light userdata") +_G.D = nil + +do -- named objects (field '__name') + checkmessage("math.sin(io.input())", "(number expected, got FILE*)") + _G.XX = setmetatable({}, {__name = "My Type"}) + assert(string.find(tostring(XX), "^My Type")) + checkmessage("io.input(XX)", "(FILE* expected, got My Type)") + checkmessage("return XX + 1", "on a My Type value") + checkmessage("return ~io.stdin", "on a FILE* value") + checkmessage("return XX < XX", "two My Type values") + checkmessage("return {} < XX", "table with My Type") + checkmessage("return XX < io.stdin", "My Type with FILE*") + _G.XX = nil +end + +-- global functions +checkmessage("(io.write or print){}", "io.write") +checkmessage("(collectgarbage or print){}", "collectgarbage") + +-- errors in functions without debug info +do + local f = function (a) return a + 1 end + f = assert(load(string.dump(f, true))) + assert(f(3) == 4) + checkerr("^%?:%-1:", f, {}) + + -- code with a move to a local var ('OP_MOV A B' with A3+1, + {d = x and aaa[x or y]}} +]], "global 'aaa'") + +checkmessage([[ +local x,y = {},1 +if math.sin(1) == 0 then return 3 end -- return +x.a()]], "field 'a'") + +checkmessage([[ +prefix = nil +insert = nil +while 1 do + local a + if nil then break end + insert(prefix, a) +end]], "global 'insert'") + +checkmessage([[ -- tail call + return math.sin("a") +]], "'sin'") + +checkmessage([[collectgarbage("nooption")]], "invalid option") + +checkmessage([[x = print .. "a"]], "concatenate") +checkmessage([[x = "a" .. false]], "concatenate") +checkmessage([[x = {} .. 2]], "concatenate") + +checkmessage("getmetatable(io.stdin).__gc()", "no value") + +checkmessage([[ +local Var +local function main() + NoSuchName (function() Var=0 end) +end +main() +]], "global 'NoSuchName'") +print'+' + +a = {}; setmetatable(a, {__index = string}) +checkmessage("a:sub()", "bad self") +checkmessage("string.sub('a', {})", "#2") +checkmessage("('a'):sub{}", "#1") + +checkmessage("table.sort({1,2,3}, table.sort)", "'table.sort'") +checkmessage("string.gsub('s', 's', setmetatable)", "'setmetatable'") + +-- tests for errors in coroutines + +local function f (n) + local c = coroutine.create(f) + local a,b = coroutine.resume(c) + return b +end +assert(string.find(f(), "C stack overflow")) + +checkmessage("coroutine.yield()", "outside a coroutine") + +f = coroutine.wrap(function () table.sort({1,2,3}, coroutine.yield) end) +checkerr("yield across", f) + + +-- testing size of 'source' info; size of buffer for that info is +-- LUA_IDSIZE, declared as 60 in luaconf. Get one position for '\0'. +idsize = 60 - 1 +local function checksize (source) + -- syntax error + local _, msg = load("x", source) + msg = string.match(msg, "^([^:]*):") -- get source (1st part before ':') + assert(msg:len() <= idsize) +end + +for i = 60 - 10, 60 + 10 do -- check border cases around 60 + checksize("@" .. string.rep("x", i)) -- file names + checksize(string.rep("x", i - 10)) -- string sources + checksize("=" .. string.rep("x", i)) -- exact sources +end + + +-- testing line error + +local function lineerror (s, l) + local err,msg = pcall(load(s)) + local line = string.match(msg, ":(%d+):") + assert((line and line+0) == l) +end + +lineerror("local a\n for i=1,'a' do \n print(i) \n end", 2) +lineerror("\n local a \n for k,v in 3 \n do \n print(k) \n end", 3) +lineerror("\n\n for k,v in \n 3 \n do \n print(k) \n end", 4) +lineerror("function a.x.y ()\na=a+1\nend", 1) + +lineerror("a = \na\n+\n{}", 3) +lineerror("a = \n3\n+\n(\n4\n/\nprint)", 6) +lineerror("a = \nprint\n+\n(\n4\n/\n7)", 3) + +lineerror("a\n=\n-\n\nprint\n;", 3) + +lineerror([[ +a +( +23) +]], 1) + +lineerror([[ +local a = {x = 13} +a +. +x +( +23 +) +]], 2) + +lineerror([[ +local a = {x = 13} +a +. +x +( +23 + a +) +]], 6) + +local p = [[ + function g() f() end + function f(x) error('a', X) end +g() +]] +X=3;lineerror((p), 3) +X=0;lineerror((p), nil) +X=1;lineerror((p), 2) +X=2;lineerror((p), 1) + + +if not _soft then + -- several tests that exaust the Lua stack + collectgarbage() + print"testing stack overflow" + C = 0 + local l = debug.getinfo(1, "l").currentline; function y () C=C+1; y() end + + local function checkstackmessage (m) + return (string.find(m, "stack overflow")) + end + -- repeated stack overflows (to check stack recovery) + assert(checkstackmessage(doit('y()'))) + print('+') + assert(checkstackmessage(doit('y()'))) + print('+') + assert(checkstackmessage(doit('y()'))) + print('+') + + + -- error lines in stack overflow + C = 0 + local l1 + local function g(x) + l1 = debug.getinfo(x, "l").currentline; y() + end + local _, stackmsg = xpcall(g, debug.traceback, 1) + print('+') + local stack = {} + for line in string.gmatch(stackmsg, "[^\n]*") do + local curr = string.match(line, ":(%d+):") + if curr then table.insert(stack, tonumber(curr)) end + end + local i=1 + while stack[i] ~= l1 do + assert(stack[i] == l) + i = i+1 + end + assert(i > 15) + + + -- error in error handling + local res, msg = xpcall(error, error) + assert(not res and type(msg) == 'string') + print('+') + + local function f (x) + if x==0 then error('a\n') + else + local aux = function () return f(x-1) end + local a,b = xpcall(aux, aux) + return a,b + end + end + f(3) + + local function loop (x,y,z) return 1 + loop(x, y, z) end + + local res, msg = xpcall(loop, function (m) + assert(string.find(m, "stack overflow")) + checkerr("error handling", loop) + assert(math.sin(0) == 0) + return 15 + end) + assert(msg == 15) + + local f = function () + for i = 999900, 1000000, 1 do table.unpack({}, 1, i) end + end + checkerr("too many results", f) + +end + + +do + -- non string messages + local t = {} + local res, msg = pcall(function () error(t) end) + assert(not res and msg == t) + + res, msg = pcall(function () error(nil) end) + assert(not res and msg == nil) + + local function f() error{msg='x'} end + res, msg = xpcall(f, function (r) return {msg=r.msg..'y'} end) + assert(msg.msg == 'xy') + + -- 'assert' with extra arguments + res, msg = pcall(assert, false, "X", t) + assert(not res and msg == "X") + + -- 'assert' with no message + res, msg = pcall(function () assert(false) end) + local line = string.match(msg, "%w+%.lua:(%d+): assertion failed!$") + assert(tonumber(line) == debug.getinfo(1, "l").currentline - 2) + + -- 'assert' with non-string messages + res, msg = pcall(assert, false, t) + assert(not res and msg == t) + + res, msg = pcall(assert, nil, nil) + assert(not res and msg == nil) + + -- 'assert' without arguments + res, msg = pcall(assert) + assert(not res and string.find(msg, "value expected")) +end + +-- xpcall with arguments +a, b, c = xpcall(string.find, error, "alo", "al") +assert(a and b == 1 and c == 2) +a, b, c = xpcall(string.find, function (x) return {} end, true, "al") +assert(not a and type(b) == "table" and c == nil) + + +print("testing tokens in error messages") +checksyntax("syntax error", "", "error", 1) +checksyntax("1.000", "", "1.000", 1) +checksyntax("[[a]]", "", "[[a]]", 1) +checksyntax("'aa'", "", "'aa'", 1) +checksyntax("while << do end", "", "<<", 1) +checksyntax("for >> do end", "", ">>", 1) + +-- test invalid non-printable char in a chunk +checksyntax("a\1a = 1", "", "<\\1>", 1) + +-- test 255 as first char in a chunk +checksyntax("\255a = 1", "", "<\\255>", 1) + +doit('I = load("a=9+"); a=3') +assert(a==3 and I == nil) +print('+') + +lim = 1000 +if _soft then lim = 100 end +for i=1,lim do + doit('a = ') + doit('a = 4+nil') +end + + +-- testing syntax limits + +local function testrep (init, rep, close, repc) + local s = init .. string.rep(rep, 100) .. close .. string.rep(repc, 100) + assert(load(s)) -- 100 levels is OK + s = init .. string.rep(rep, 10000) + local res, msg = load(s) -- 10000 levels not ok + assert(not res and (string.find(msg, "too many registers") or + string.find(msg, "stack overflow"))) +end + +testrep("local a; a", ",a", "= 1", ",1") -- multiple assignment +testrep("local a; a=", "{", "0", "}") +testrep("local a; a=", "(", "2", ")") +testrep("local a; ", "a(", "2", ")") +testrep("", "do ", "", " end") +testrep("", "while a do ", "", " end") +testrep("local a; ", "if a then else ", "", " end") +testrep("", "function foo () ", "", " end") +testrep("local a; a=", "a..", "a", "") +testrep("local a; a=", "a^", "a", "") + +checkmessage("a = f(x" .. string.rep(",x", 260) .. ")", "too many registers") + + +-- testing other limits + +-- upvalues +local lim = 127 +local s = "local function fooA ()\n local " +for j = 1,lim do + s = s.."a"..j..", " +end +s = s.."b,c\n" +s = s.."local function fooB ()\n local " +for j = 1,lim do + s = s.."b"..j..", " +end +s = s.."b\n" +s = s.."function fooC () return b+c" +local c = 1+2 +for j = 1,lim do + s = s.."+a"..j.."+b"..j + c = c + 2 +end +s = s.."\nend end end" +local a,b = load(s) +assert(c > 255 and string.find(b, "too many upvalues") and + string.find(b, "line 5")) + +-- local variables +s = "\nfunction foo ()\n local " +for j = 1,300 do + s = s.."a"..j..", " +end +s = s.."b\n" +local a,b = load(s) +assert(string.find(b, "line 2") and string.find(b, "too many local variables")) + +mt.__index = oldmm + +print('OK') diff --git a/testes/events.lua b/testes/events.lua new file mode 100644 index 0000000000..cf064d3d6b --- /dev/null +++ b/testes/events.lua @@ -0,0 +1,476 @@ +-- $Id: events.lua,v 1.52 2018/03/12 13:51:02 roberto Exp $ +-- See Copyright Notice in file all.lua + +print('testing metatables') + +local debug = require'debug' + +X = 20; B = 30 + +_ENV = setmetatable({}, {__index=_G}) + +collectgarbage() + +X = X+10 +assert(X == 30 and _G.X == 20) +B = false +assert(B == false) +_ENV["B"] = undef +assert(B == 30) + +assert(getmetatable{} == nil) +assert(getmetatable(4) == nil) +assert(getmetatable(nil) == nil) +a={name = "NAME"}; setmetatable(a, {__metatable = "xuxu", + __tostring=function(x) return x.name end}) +assert(getmetatable(a) == "xuxu") +assert(tostring(a) == "NAME") +-- cannot change a protected metatable +assert(pcall(setmetatable, a, {}) == false) +a.name = "gororoba" +assert(tostring(a) == "gororoba") + +local a, t = {10,20,30; x="10", y="20"}, {} +assert(setmetatable(a,t) == a) +assert(getmetatable(a) == t) +assert(setmetatable(a,nil) == a) +assert(getmetatable(a) == nil) +assert(setmetatable(a,t) == a) + + +function f (t, i, e) + assert(not e) + local p = rawget(t, "parent") + return (p and p[i]+3), "dummy return" +end + +t.__index = f + +a.parent = {z=25, x=12, [4] = 24} +assert(a[1] == 10 and a.z == 28 and a[4] == 27 and a.x == "10") + +collectgarbage() + +a = setmetatable({}, t) +function f(t, i, v) rawset(t, i, v-3) end +setmetatable(t, t) -- causes a bug in 5.1 ! +t.__newindex = f +a[1] = 30; a.x = "101"; a[5] = 200 +assert(a[1] == 27 and a.x == 98 and a[5] == 197) + +do -- bug in Lua 5.3.2 + local mt = {} + mt.__newindex = mt + local t = setmetatable({}, mt) + t[1] = 10 -- will segfault on some machines + assert(mt[1] == 10) +end + + +local c = {} +a = setmetatable({}, t) +t.__newindex = c +t.__index = c +a[1] = 10; a[2] = 20; a[3] = 90; +for i = 4, 20 do a[i] = i * 10 end +assert(a[1] == 10 and a[2] == 20 and a[3] == 90) +for i = 4, 20 do assert(a[i] == i * 10) end +assert(next(a) == nil) + + +do + local a; + a = setmetatable({}, {__index = setmetatable({}, + {__index = setmetatable({}, + {__index = function (_,n) return a[n-3]+4, "lixo" end})})}) + a[0] = 20 + for i=0,10 do + assert(a[i*3] == 20 + i*4) + end +end + + +do -- newindex + local foi + local a = {} + for i=1,10 do a[i] = 0; a['a'..i] = 0; end + setmetatable(a, {__newindex = function (t,k,v) foi=true; rawset(t,k,v) end}) + foi = false; a[1]=0; assert(not foi) + foi = false; a['a1']=0; assert(not foi) + foi = false; a['a11']=0; assert(foi) + foi = false; a[11]=0; assert(foi) + foi = false; a[1]=undef; assert(not foi) + a[1] = undef + foi = false; a[1]=nil; assert(foi) +end + + +setmetatable(t, nil) +function f (t, ...) return t, {...} end +t.__call = f + +do + local x,y = a(table.unpack{'a', 1}) + assert(x==a and y[1]=='a' and y[2]==1 and y[3]==undef) + x,y = a() + assert(x==a and y[1]==undef) +end + + +local b = setmetatable({}, t) +setmetatable(b,t) + +function f(op) + return function (...) cap = {[0] = op, ...} ; return (...) end +end +t.__add = f("add") +t.__sub = f("sub") +t.__mul = f("mul") +t.__div = f("div") +t.__idiv = f("idiv") +t.__mod = f("mod") +t.__unm = f("unm") +t.__pow = f("pow") +t.__len = f("len") +t.__band = f("band") +t.__bor = f("bor") +t.__bxor = f("bxor") +t.__shl = f("shl") +t.__shr = f("shr") +t.__bnot = f("bnot") + +-- Some tests are done inside small anonymous functions to ensure +-- that constants go to constant table even in debug compilation, +-- when the constant table is very small. +assert(b+5 == b) +assert(cap[0] == "add" and cap[1] == b and cap[2] == 5 and cap[3]==undef) +assert(b+'5' == b) +assert(cap[0] == "add" and cap[1] == b and cap[2] == '5' and cap[3]==undef) +assert(5+b == 5) +assert(cap[0] == "add" and cap[1] == 5 and cap[2] == b and cap[3]==undef) +assert('5'+b == '5') +assert(cap[0] == "add" and cap[1] == '5' and cap[2] == b and cap[3]==undef) +b=b-3; assert(getmetatable(b) == t) +assert(cap[0] == "sub" and cap[1] == b and cap[2] == 3 and cap[3]==undef) +assert(5-a == 5) +assert(cap[0] == "sub" and cap[1] == 5 and cap[2] == a and cap[3]==undef) +assert('5'-a == '5') +assert(cap[0] == "sub" and cap[1] == '5' and cap[2] == a and cap[3]==undef) +assert(a*a == a) +assert(cap[0] == "mul" and cap[1] == a and cap[2] == a and cap[3]==undef) +assert(a/0 == a) +assert(cap[0] == "div" and cap[1] == a and cap[2] == 0 and cap[3]==undef) +assert(a%2 == a) +assert(cap[0] == "mod" and cap[1] == a and cap[2] == 2 and cap[3]==undef) +assert(a // (1/0) == a) +assert(cap[0] == "idiv" and cap[1] == a and cap[2] == 1/0 and cap[3]==undef) +;(function () assert(a & "hi" == a) end)() +assert(cap[0] == "band" and cap[1] == a and cap[2] == "hi" and cap[3]==undef) +;(function () assert(10 & a == 10) end)() +assert(cap[0] == "band" and cap[1] == 10 and cap[2] == a and cap[3]==undef) +;(function () assert(a | 10 == a) end)() +assert(cap[0] == "bor" and cap[1] == a and cap[2] == 10 and cap[3]==undef) +assert(a | "hi" == a) +assert(cap[0] == "bor" and cap[1] == a and cap[2] == "hi" and cap[3]==undef) +assert("hi" ~ a == "hi") +assert(cap[0] == "bxor" and cap[1] == "hi" and cap[2] == a and cap[3]==undef) +;(function () assert(10 ~ a == 10) end)() +assert(cap[0] == "bxor" and cap[1] == 10 and cap[2] == a and cap[3]==undef) +assert(-a == a) +assert(cap[0] == "unm" and cap[1] == a) +assert(a^4 == a) +assert(cap[0] == "pow" and cap[1] == a and cap[2] == 4 and cap[3]==undef) +assert(a^'4' == a) +assert(cap[0] == "pow" and cap[1] == a and cap[2] == '4' and cap[3]==undef) +assert(4^a == 4) +assert(cap[0] == "pow" and cap[1] == 4 and cap[2] == a and cap[3]==undef) +assert('4'^a == '4') +assert(cap[0] == "pow" and cap[1] == '4' and cap[2] == a and cap[3]==undef) +assert(#a == a) +assert(cap[0] == "len" and cap[1] == a) +assert(~a == a) +assert(cap[0] == "bnot" and cap[1] == a) +assert(a << 3 == a) +assert(cap[0] == "shl" and cap[1] == a and cap[2] == 3) +assert(1.5 >> a == 1.5) +assert(cap[0] == "shr" and cap[1] == 1.5 and cap[2] == a) + + +-- test for rawlen +t = setmetatable({1,2,3}, {__len = function () return 10 end}) +assert(#t == 10 and rawlen(t) == 3) +assert(rawlen"abc" == 3) +assert(not pcall(rawlen, io.stdin)) +assert(not pcall(rawlen, 34)) +assert(not pcall(rawlen)) + +-- rawlen for long strings +assert(rawlen(string.rep('a', 1000)) == 1000) + + +t = {} +t.__lt = function (a,b,c) + collectgarbage() + assert(c == nil) + if type(a) == 'table' then a = a.x end + if type(b) == 'table' then b = b.x end + return aOp(1)) and not(Op(1)>Op(2)) and (Op(2)>Op(1))) + assert(not(Op('a')>Op('a')) and not(Op('a')>Op('b')) and (Op('b')>Op('a'))) + assert((Op(1)>=Op(1)) and not(Op(1)>=Op(2)) and (Op(2)>=Op(1))) + assert((1 >= Op(1)) and not(1 >= Op(2)) and (Op(2) >= 1)) + assert((Op('a')>=Op('a')) and not(Op('a')>=Op('b')) and (Op('b')>=Op('a'))) + assert(('a' >= Op('a')) and not(Op('a') >= 'b') and (Op('b') >= Op('a'))) +end + +test() + +t.__le = function (a,b,c) + assert(c == nil) + if type(a) == 'table' then a = a.x end + if type(b) == 'table' then b = b.x end + return a<=b, "dummy" +end + +test() -- retest comparisons, now using both `lt' and `le' + + +-- test `partial order' + +local function rawSet(x) + local y = {} + for _,k in pairs(x) do y[k] = 1 end + return y +end + +local function Set(x) + return setmetatable(rawSet(x), t) +end + +t.__lt = function (a,b) + for k in pairs(a) do + if not b[k] then return false end + b[k] = undef + end + return next(b) ~= nil +end + +t.__le = nil + +assert(Set{1,2,3} < Set{1,2,3,4}) +assert(not(Set{1,2,3,4} < Set{1,2,3,4})) +assert((Set{1,2,3,4} <= Set{1,2,3,4})) +assert((Set{1,2,3,4} >= Set{1,2,3,4})) +assert((Set{1,3} <= Set{3,5})) -- wrong!! model needs a `le' method ;-) + +t.__le = function (a,b) + for k in pairs(a) do + if not b[k] then return false end + end + return true +end + +assert(not (Set{1,3} <= Set{3,5})) -- now its OK! +assert(not(Set{1,3} <= Set{3,5})) +assert(not(Set{1,3} >= Set{3,5})) + +t.__eq = function (a,b) + for k in pairs(a) do + if not b[k] then return false end + b[k] = undef + end + return next(b) == nil +end + +local s = Set{1,3,5} +assert(s == Set{3,5,1}) +assert(not rawequal(s, Set{3,5,1})) +assert(rawequal(s, s)) +assert(Set{1,3,5,1} == rawSet{3,5,1}) +assert(rawSet{1,3,5,1} == Set{3,5,1}) +assert(Set{1,3,5} ~= Set{3,5,1,6}) + +-- '__eq' is not used for table accesses +t[Set{1,3,5}] = 1 +assert(t[Set{1,3,5}] == undef) + + +if not T then + (Message or print)('\n >>> testC not active: skipping tests for \z +userdata <<<\n') +else + local u1 = T.newuserdata(0, 1) + local u2 = T.newuserdata(0, 1) + local u3 = T.newuserdata(0, 1) + assert(u1 ~= u2 and u1 ~= u3) + debug.setuservalue(u1, 1); + debug.setuservalue(u2, 2); + debug.setuservalue(u3, 1); + debug.setmetatable(u1, {__eq = function (a, b) + return debug.getuservalue(a) == debug.getuservalue(b) + end}) + debug.setmetatable(u2, {__eq = function (a, b) + return true + end}) + assert(u1 == u3 and u3 == u1 and u1 ~= u2) + assert(u2 == u1 and u2 == u3 and u3 == u2) + assert(u2 ~= {}) -- different types cannot be equal + + local mirror = {} + debug.setmetatable(u3, {__index = mirror, __newindex = mirror}) + for i = 1, 10 do u3[i] = i end + for i = 1, 10 do assert(u3[i] == i) end +end + + +t.__concat = function (a,b,c) + assert(c == nil) + if type(a) == 'table' then a = a.val end + if type(b) == 'table' then b = b.val end + if A then return a..b + else + return setmetatable({val=a..b}, t) + end +end + +c = {val="c"}; setmetatable(c, t) +d = {val="d"}; setmetatable(d, t) + +A = true +assert(c..d == 'cd') +assert(0 .."a".."b"..c..d.."e".."f"..(5+3).."g" == "0abcdef8g") + +A = false +assert((c..d..c..d).val == 'cdcd') +x = c..d +assert(getmetatable(x) == t and x.val == 'cd') +x = 0 .."a".."b"..c..d.."e".."f".."g" +assert(x.val == "0abcdefg") + + +-- concat metamethod x numbers (bug in 5.1.1) +c = {} +local x +setmetatable(c, {__concat = function (a,b) + assert(type(a) == "number" and b == c or type(b) == "number" and a == c) + return c +end}) +assert(c..5 == c and 5 .. c == c) +assert(4 .. c .. 5 == c and 4 .. 5 .. 6 .. 7 .. c == c) + + +-- test comparison compatibilities +local t1, t2, c, d +t1 = {}; c = {}; setmetatable(c, t1) +d = {} +t1.__eq = function () return true end +t1.__lt = function () return true end +setmetatable(d, t1) +assert(c == d and c < d and not(d <= c)) +t2 = {} +t2.__eq = t1.__eq +t2.__lt = t1.__lt +setmetatable(d, t2) +assert(c == d and c < d and not(d <= c)) + + + +-- test for several levels of calls +local i +local tt = { + __call = function (t, ...) + i = i+1 + if t.f then return t.f(...) + else return {...} + end + end +} + +local a = setmetatable({}, tt) +local b = setmetatable({f=a}, tt) +local c = setmetatable({f=b}, tt) + +i = 0 +x = c(3,4,5) +assert(i == 3 and x[1] == 3 and x[3] == 5) + + +assert(_G.X == 20) + +print'+' + +local _g = _G +_ENV = setmetatable({}, {__index=function (_,k) return _g[k] end}) + + +a = {} +rawset(a, "x", 1, 2, 3) +assert(a.x == 1 and rawget(a, "x", 3) == 1) + +print '+' + +-- testing metatables for basic types +mt = {__index = function (a,b) return a+b end, + __len = function (x) return math.floor(x) end} +debug.setmetatable(10, mt) +assert(getmetatable(-2) == mt) +assert((10)[3] == 13) +assert((10)["3"] == 13) +assert(#3.45 == 3) +debug.setmetatable(23, nil) +assert(getmetatable(-2) == nil) + +debug.setmetatable(true, mt) +assert(getmetatable(false) == mt) +mt.__index = function (a,b) return a or b end +assert((true)[false] == true) +assert((false)[false] == false) +debug.setmetatable(false, nil) +assert(getmetatable(true) == nil) + +debug.setmetatable(nil, mt) +assert(getmetatable(nil) == mt) +mt.__add = function (a,b) return (a or 1) + (b or 2) end +assert(10 + nil == 12) +assert(nil + 23 == 24) +assert(nil + nil == 3) +debug.setmetatable(nil, nil) +assert(getmetatable(nil) == nil) + +debug.setmetatable(nil, {}) + + +-- loops in delegation +a = {}; setmetatable(a, a); a.__index = a; a.__newindex = a +assert(not pcall(function (a,b) return a[b] end, a, 10)) +assert(not pcall(function (a,b,c) a[b] = c end, a, 10, true)) + +-- bug in 5.1 +T, K, V = nil +grandparent = {} +grandparent.__newindex = function(t,k,v) T=t; K=k; V=v end + +parent = {} +parent.__newindex = parent +setmetatable(parent, grandparent) + +child = setmetatable({}, parent) +child.foo = 10 --> CRASH (on some machines) +assert(T == parent and K == "foo" and V == 10) + +print 'OK' + +return 12 + + diff --git a/testes/files.lua b/testes/files.lua new file mode 100644 index 0000000000..b2c7c202f1 --- /dev/null +++ b/testes/files.lua @@ -0,0 +1,832 @@ +-- $Id: files.lua,v 1.101 2018/03/12 13:51:02 roberto Exp $ +-- See Copyright Notice in file all.lua + +local debug = require "debug" + +local maxint = math.maxinteger + +assert(type(os.getenv"PATH") == "string") + +assert(io.input(io.stdin) == io.stdin) +assert(not pcall(io.input, "non-existent-file")) +assert(io.output(io.stdout) == io.stdout) + + +local function testerr (msg, f, ...) + local stat, err = pcall(f, ...) + return (not stat and string.find(err, msg, 1, true)) +end + + +local function checkerr (msg, f, ...) + assert(testerr(msg, f, ...)) +end + + +-- cannot close standard files +assert(not io.close(io.stdin) and + not io.stdout:close() and + not io.stderr:close()) + +-- cannot call close method without an argument (new in 5.3.5) +checkerr("got no value", io.stdin.close) + + +assert(type(io.input()) == "userdata" and io.type(io.output()) == "file") +assert(type(io.stdin) == "userdata" and io.type(io.stderr) == "file") +assert(not io.type(8)) +local a = {}; setmetatable(a, {}) +assert(not io.type(a)) + +assert(getmetatable(io.input()).__name == "FILE*") + +local a,b,c = io.open('xuxu_nao_existe') +assert(not a and type(b) == "string" and type(c) == "number") + +a,b,c = io.open('/a/b/c/d', 'w') +assert(not a and type(b) == "string" and type(c) == "number") + +local file = os.tmpname() +local f, msg = io.open(file, "w") +if not f then + (Message or print)("'os.tmpname' file cannot be open; skipping file tests") + +else --{ most tests here need tmpname +f:close() + +print('testing i/o') + +local otherfile = os.tmpname() + +checkerr("invalid mode", io.open, file, "rw") +checkerr("invalid mode", io.open, file, "rb+") +checkerr("invalid mode", io.open, file, "r+bk") +checkerr("invalid mode", io.open, file, "") +checkerr("invalid mode", io.open, file, "+") +checkerr("invalid mode", io.open, file, "b") +assert(io.open(file, "r+b")):close() +assert(io.open(file, "r+")):close() +assert(io.open(file, "rb")):close() + +assert(os.setlocale('C', 'all')) + +io.input(io.stdin); io.output(io.stdout); + +os.remove(file) +assert(not loadfile(file)) +checkerr("", dofile, file) +assert(not io.open(file)) +io.output(file) +assert(io.output() ~= io.stdout) + +if not _port then -- invalid seek + local status, msg, code = io.stdin:seek("set", 1000) + assert(not status and type(msg) == "string" and type(code) == "number") +end + +assert(io.output():seek() == 0) +assert(io.write("alo alo"):seek() == string.len("alo alo")) +assert(io.output():seek("cur", -3) == string.len("alo alo")-3) +assert(io.write("joao")) +assert(io.output():seek("end") == string.len("alo joao")) + +assert(io.output():seek("set") == 0) + +assert(io.write('"lo"', "{a}\n", "second line\n", "third line \n")) +assert(io.write('fourth_line')) +io.output(io.stdout) +collectgarbage() -- file should be closed by GC +assert(io.input() == io.stdin and rawequal(io.output(), io.stdout)) +print('+') + +-- test GC for files +collectgarbage() +for i=1,120 do + for i=1,5 do + io.input(file) + assert(io.open(file, 'r')) + io.lines(file) + end + collectgarbage() +end + +io.input():close() +io.close() + +assert(os.rename(file, otherfile)) +assert(not os.rename(file, otherfile)) + +io.output(io.open(otherfile, "ab")) +assert(io.write("\n\n\t\t ", 3450, "\n")); +io.close() + +-- test writing/reading numbers +f = assert(io.open(file, "w")) +f:write(maxint, '\n') +f:write(string.format("0X%x\n", maxint)) +f:write("0xABCp-3", '\n') +f:write(0, '\n') +f:write(-maxint, '\n') +f:write(string.format("0x%X\n", -maxint)) +f:write("-0xABCp-3", '\n') +assert(f:close()) +f = assert(io.open(file, "r")) +assert(f:read("n") == maxint) +assert(f:read("n") == maxint) +assert(f:read("n") == 0xABCp-3) +assert(f:read("n") == 0) +assert(f:read("*n") == -maxint) -- test old format (with '*') +assert(f:read("n") == -maxint) +assert(f:read("*n") == -0xABCp-3) -- test old format (with '*') +assert(f:close()) +assert(os.remove(file)) + + +-- testing multiple arguments to io.read +do + local f = assert(io.open(file, "w")) + f:write[[ +a line +another line +1234 +3.45 +one +two +three +]] + local l1, l2, l3, l4, n1, n2, c, dummy + assert(f:close()) + f = assert(io.open(file, "r")) + l1, l2, n1, n2, dummy = f:read("l", "L", "n", "n") + assert(l1 == "a line" and l2 == "another line\n" and + n1 == 1234 and n2 == 3.45 and dummy == nil) + assert(f:close()) + f = assert(io.open(file, "r")) + l1, l2, n1, n2, c, l3, l4, dummy = f:read(7, "l", "n", "n", 1, "l", "l") + assert(l1 == "a line\n" and l2 == "another line" and c == '\n' and + n1 == 1234 and n2 == 3.45 and l3 == "one" and l4 == "two" + and dummy == nil) + assert(f:close()) + f = assert(io.open(file, "r")) + -- second item failing + l1, n1, n2, dummy = f:read("l", "n", "n", "l") + assert(l1 == "a line" and n1 == nil) + assert(f:close()) + assert(os.remove(file)) +end + + + +-- test yielding during 'dofile' +f = assert(io.open(file, "w")) +f:write[[ +local x, z = coroutine.yield(10) +local y = coroutine.yield(20) +return x + y * z +]] +assert(f:close()) +f = coroutine.wrap(dofile) +assert(f(file) == 10) +print(f(100, 101) == 20) +assert(f(200) == 100 + 200 * 101) +assert(os.remove(file)) + + +f = assert(io.open(file, "w")) +-- test number termination +f:write[[ +-12.3- -0xffff+ .3|5.E-3X +234e+13E 0xDEADBEEFDEADBEEFx +0x1.13Ap+3e +]] +-- very long number +f:write("1234"); for i = 1, 1000 do f:write("0") end; f:write("\n") +-- invalid sequences (must read and discard valid prefixes) +f:write[[ +.e+ 0.e; --; 0xX; +]] +assert(f:close()) +f = assert(io.open(file, "r")) +assert(f:read("n") == -12.3); assert(f:read(1) == "-") +assert(f:read("n") == -0xffff); assert(f:read(2) == "+ ") +assert(f:read("n") == 0.3); assert(f:read(1) == "|") +assert(f:read("n") == 5e-3); assert(f:read(1) == "X") +assert(f:read("n") == 234e13); assert(f:read(1) == "E") +assert(f:read("n") == 0Xdeadbeefdeadbeef); assert(f:read(2) == "x\n") +assert(f:read("n") == 0x1.13aP3); assert(f:read(1) == "e") + +do -- attempt to read too long number + assert(f:read("n") == nil) -- fails + local s = f:read("L") -- read rest of line + assert(string.find(s, "^00*\n$")) -- lots of 0's left +end + +assert(not f:read("n")); assert(f:read(2) == "e+") +assert(not f:read("n")); assert(f:read(1) == ";") +assert(not f:read("n")); assert(f:read(2) == "-;") +assert(not f:read("n")); assert(f:read(1) == "X") +assert(not f:read("n")); assert(f:read(1) == ";") +assert(not f:read("n")); assert(not f:read(0)) -- end of file +assert(f:close()) +assert(os.remove(file)) + + +-- test line generators +assert(not pcall(io.lines, "non-existent-file")) +assert(os.rename(otherfile, file)) +io.output(otherfile) +local n = 0 +local f = io.lines(file) +while f() do n = n + 1 end; +assert(n == 6) -- number of lines in the file +checkerr("file is already closed", f) +checkerr("file is already closed", f) +-- copy from file to otherfile +n = 0 +for l in io.lines(file) do io.write(l, "\n"); n = n + 1 end +io.close() +assert(n == 6) +-- copy from otherfile back to file +local f = assert(io.open(otherfile)) +assert(io.type(f) == "file") +io.output(file) +assert(not io.output():read()) +n = 0 +for l in f:lines() do io.write(l, "\n"); n = n + 1 end +assert(tostring(f):sub(1, 5) == "file ") +assert(f:close()); io.close() +assert(n == 6) +checkerr("closed file", io.close, f) +assert(tostring(f) == "file (closed)") +assert(io.type(f) == "closed file") +io.input(file) +f = io.open(otherfile):lines() +n = 0 +for l in io.lines() do assert(l == f()); n = n + 1 end +f = nil; collectgarbage() +assert(n == 6) +assert(os.remove(otherfile)) + +do -- bug in 5.3.1 + io.output(otherfile) + io.write(string.rep("a", 300), "\n") + io.close() + local t ={}; for i = 1, 250 do t[i] = 1 end + t = {io.lines(otherfile, table.unpack(t))()} + -- everything ok here + assert(#t == 250 and t[1] == 'a' and t[#t] == 'a') + t[#t + 1] = 1 -- one too many + checkerr("too many arguments", io.lines, otherfile, table.unpack(t)) + collectgarbage() -- ensure 'otherfile' is closed + assert(os.remove(otherfile)) +end + +io.input(file) +do -- test error returns + local a,b,c = io.input():write("xuxu") + assert(not a and type(b) == "string" and type(c) == "number") +end +checkerr("invalid format", io.read, "x") +assert(io.read(0) == "") -- not eof +assert(io.read(5, 'l') == '"lo"') +assert(io.read(0) == "") +assert(io.read() == "second line") +local x = io.input():seek() +assert(io.read() == "third line ") +assert(io.input():seek("set", x)) +assert(io.read('L') == "third line \n") +assert(io.read(1) == "") +assert(io.read(string.len"fourth_line") == "fourth_line") +assert(io.input():seek("cur", -string.len"fourth_line")) +assert(io.read() == "fourth_line") +assert(io.read() == "") -- empty line +assert(io.read('n') == 3450) +assert(io.read(1) == '\n') +assert(io.read(0) == nil) -- end of file +assert(io.read(1) == nil) -- end of file +assert(io.read(30000) == nil) -- end of file +assert(({io.read(1)})[2] == undef) +assert(io.read() == nil) -- end of file +assert(({io.read()})[2] == undef) +assert(io.read('n') == nil) -- end of file +assert(({io.read('n')})[2] == undef) +assert(io.read('a') == '') -- end of file (OK for 'a') +assert(io.read('a') == '') -- end of file (OK for 'a') +collectgarbage() +print('+') +io.close(io.input()) +checkerr(" input file is closed", io.read) + +assert(os.remove(file)) + +local t = '0123456789' +for i=1,10 do t = t..t; end +assert(string.len(t) == 10*2^10) + +io.output(file) +io.write("alo"):write("\n") +io.close() +checkerr(" output file is closed", io.write) +local f = io.open(file, "a+b") +io.output(f) +collectgarbage() + +assert(io.write(' ' .. t .. ' ')) +assert(io.write(';', 'end of file\n')) +f:flush(); io.flush() +f:close() +print('+') + +io.input(file) +assert(io.read() == "alo") +assert(io.read(1) == ' ') +assert(io.read(string.len(t)) == t) +assert(io.read(1) == ' ') +assert(io.read(0)) +assert(io.read('a') == ';end of file\n') +assert(io.read(0) == nil) +assert(io.close(io.input())) + + +-- test errors in read/write +do + local function ismsg (m) + -- error message is not a code number + return (type(m) == "string" and tonumber(m) == nil) + end + + -- read + local f = io.open(file, "w") + local r, m, c = f:read() + assert(not r and ismsg(m) and type(c) == "number") + assert(f:close()) + -- write + f = io.open(file, "r") + r, m, c = f:write("whatever") + assert(not r and ismsg(m) and type(c) == "number") + assert(f:close()) + -- lines + f = io.open(file, "w") + r, m = pcall(f:lines()) + assert(r == false and ismsg(m)) + assert(f:close()) +end + +assert(os.remove(file)) + +-- test for L format +io.output(file); io.write"\n\nline\nother":close() +io.input(file) +assert(io.read"L" == "\n") +assert(io.read"L" == "\n") +assert(io.read"L" == "line\n") +assert(io.read"L" == "other") +assert(io.read"L" == nil) +io.input():close() + +local f = assert(io.open(file)) +local s = "" +for l in f:lines("L") do s = s .. l end +assert(s == "\n\nline\nother") +f:close() + +io.input(file) +s = "" +for l in io.lines(nil, "L") do s = s .. l end +assert(s == "\n\nline\nother") +io.input():close() + +s = "" +for l in io.lines(file, "L") do s = s .. l end +assert(s == "\n\nline\nother") + +s = "" +for l in io.lines(file, "l") do s = s .. l end +assert(s == "lineother") + +io.output(file); io.write"a = 10 + 34\na = 2*a\na = -a\n":close() +local t = {} +assert(load(io.lines(file, "L"), nil, nil, t))() +assert(t.a == -((10 + 34) * 2)) + + +-- test for multipe arguments in 'lines' +io.output(file); io.write"0123456789\n":close() +for a,b in io.lines(file, 1, 1) do + if a == "\n" then assert(b == nil) + else assert(tonumber(a) == tonumber(b) - 1) + end +end + +for a,b,c in io.lines(file, 1, 2, "a") do + assert(a == "0" and b == "12" and c == "3456789\n") +end + +for a,b,c in io.lines(file, "a", 0, 1) do + if a == "" then break end + assert(a == "0123456789\n" and b == nil and c == nil) +end +collectgarbage() -- to close file in previous iteration + +io.output(file); io.write"00\n10\n20\n30\n40\n":close() +for a, b in io.lines(file, "n", "n") do + if a == 40 then assert(b == nil) + else assert(a == b - 10) + end +end + + +-- test load x lines +io.output(file); +io.write[[ +local y += X +X = +X * +2 + +X; +X = +X +- y; +]]:close() +_G.X = 1 +assert(not load(io.lines(file))) +collectgarbage() -- to close file in previous iteration +load(io.lines(file, "L"))() +assert(_G.X == 2) +load(io.lines(file, 1))() +assert(_G.X == 4) +load(io.lines(file, 3))() +assert(_G.X == 8) + +print('+') + +local x1 = "string\n\n\\com \"\"''coisas [[estranhas]] ]]'" +io.output(file) +assert(io.write(string.format("x2 = %q\n-- comment without ending EOS", x1))) +io.close() +assert(loadfile(file))() +assert(x1 == x2) +print('+') +assert(os.remove(file)) +assert(not os.remove(file)) +assert(not os.remove(otherfile)) + +-- testing loadfile +local function testloadfile (s, expres) + io.output(file) + if s then io.write(s) end + io.close() + local res = assert(loadfile(file))() + assert(os.remove(file)) + assert(res == expres) +end + +-- loading empty file +testloadfile(nil, nil) + +-- loading file with initial comment without end of line +testloadfile("# a non-ending comment", nil) + + +-- checking Unicode BOM in files +testloadfile("\xEF\xBB\xBF# some comment\nreturn 234", 234) +testloadfile("\xEF\xBB\xBFreturn 239", 239) +testloadfile("\xEF\xBB\xBF", nil) -- empty file with a BOM + + +-- checking line numbers in files with initial comments +testloadfile("# a comment\nreturn require'debug'.getinfo(1).currentline", 2) + + +-- loading binary file +io.output(io.open(file, "wb")) +assert(io.write(string.dump(function () return 10, '\0alo\255', 'hi' end))) +io.close() +a, b, c = assert(loadfile(file))() +assert(a == 10 and b == "\0alo\255" and c == "hi") +assert(os.remove(file)) + +-- bug in 5.2.1 +do + io.output(io.open(file, "wb")) + -- save function with no upvalues + assert(io.write(string.dump(function () return 1 end))) + io.close() + f = assert(loadfile(file, "b", {})) + assert(type(f) == "function" and f() == 1) + assert(os.remove(file)) +end + +-- loading binary file with initial comment +io.output(io.open(file, "wb")) +assert(io.write("#this is a comment for a binary file\0\n", + string.dump(function () return 20, '\0\0\0' end))) +io.close() +a, b, c = assert(loadfile(file))() +assert(a == 20 and b == "\0\0\0" and c == nil) +assert(os.remove(file)) + + +-- 'loadfile' with 'env' +do + local f = io.open(file, 'w') + f:write[[ + if (...) then a = 15; return b, c, d + else return _ENV + end + ]] + f:close() + local t = {b = 12, c = "xuxu", d = print} + local f = assert(loadfile(file, 't', t)) + local b, c, d = f(1) + assert(t.a == 15 and b == 12 and c == t.c and d == print) + assert(f() == t) + f = assert(loadfile(file, 't', nil)) + assert(f() == nil) + f = assert(loadfile(file)) + assert(f() == _G) + assert(os.remove(file)) +end + + +-- 'loadfile' x modes +do + io.open(file, 'w'):write("return 10"):close() + local s, m = loadfile(file, 'b') + assert(not s and string.find(m, "a text chunk")) + io.open(file, 'w'):write("\27 return 10"):close() + local s, m = loadfile(file, 't') + assert(not s and string.find(m, "a binary chunk")) + assert(os.remove(file)) +end + + +io.output(file) +assert(io.write("qualquer coisa\n")) +assert(io.write("mais qualquer coisa")) +io.close() +assert(io.output(assert(io.open(otherfile, 'wb'))) + :write("outra coisa\0\1\3\0\0\0\0\255\0") + :close()) + +local filehandle = assert(io.open(file, 'r+')) +local otherfilehandle = assert(io.open(otherfile, 'rb')) +assert(filehandle ~= otherfilehandle) +assert(type(filehandle) == "userdata") +assert(filehandle:read('l') == "qualquer coisa") +io.input(otherfilehandle) +assert(io.read(string.len"outra coisa") == "outra coisa") +assert(filehandle:read('l') == "mais qualquer coisa") +filehandle:close(); +assert(type(filehandle) == "userdata") +io.input(otherfilehandle) +assert(io.read(4) == "\0\1\3\0") +assert(io.read(3) == "\0\0\0") +assert(io.read(0) == "") -- 255 is not eof +assert(io.read(1) == "\255") +assert(io.read('a') == "\0") +assert(not io.read(0)) +assert(otherfilehandle == io.input()) +otherfilehandle:close() +assert(os.remove(file)) +assert(os.remove(otherfile)) +collectgarbage() + +io.output(file) + :write[[ + 123.4 -56e-2 not a number +second line +third line + +and the rest of the file +]] + :close() +io.input(file) +local _,a,b,c,d,e,h,__ = io.read(1, 'n', 'n', 'l', 'l', 'l', 'a', 10) +assert(io.close(io.input())) +assert(_ == ' ' and __ == nil) +assert(type(a) == 'number' and a==123.4 and b==-56e-2) +assert(d=='second line' and e=='third line') +assert(h==[[ + +and the rest of the file +]]) +assert(os.remove(file)) +collectgarbage() + +-- testing buffers +do + local f = assert(io.open(file, "w")) + local fr = assert(io.open(file, "r")) + assert(f:setvbuf("full", 2000)) + f:write("x") + assert(fr:read("all") == "") -- full buffer; output not written yet + f:close() + fr:seek("set") + assert(fr:read("all") == "x") -- `close' flushes it + f = assert(io.open(file), "w") + assert(f:setvbuf("no")) + f:write("x") + fr:seek("set") + assert(fr:read("all") == "x") -- no buffer; output is ready + f:close() + f = assert(io.open(file, "a")) + assert(f:setvbuf("line")) + f:write("x") + fr:seek("set", 1) + assert(fr:read("all") == "") -- line buffer; no output without `\n' + f:write("a\n"):seek("set", 1) + assert(fr:read("all") == "xa\n") -- now we have a whole line + f:close(); fr:close() + assert(os.remove(file)) +end + + +if not _soft then + print("testing large files (> BUFSIZ)") + io.output(file) + for i=1,5001 do io.write('0123456789123') end + io.write('\n12346'):close() + io.input(file) + local x = io.read('a') + io.input():seek('set', 0) + local y = io.read(30001)..io.read(1005)..io.read(0).. + io.read(1)..io.read(100003) + assert(x == y and string.len(x) == 5001*13 + 6) + io.input():seek('set', 0) + y = io.read() -- huge line + assert(x == y..'\n'..io.read()) + assert(io.read() == nil) + io.close(io.input()) + assert(os.remove(file)) + x = nil; y = nil +end + +if not _port then + local progname + do -- get name of running executable + local arg = arg or ARG + local i = 0 + while arg[i] do i = i - 1 end + progname = '"' .. arg[i + 1] .. '"' + end + print("testing popen/pclose and execute") + local tests = { + -- command, what, code + {"ls > /dev/null", "ok"}, + {"not-to-be-found-command", "exit"}, + {"exit 3", "exit", 3}, + {"exit 129", "exit", 129}, + {"kill -s HUP $$", "signal", 1}, + {"kill -s KILL $$", "signal", 9}, + {"sh -c 'kill -s HUP $$'", "exit"}, + {progname .. ' -e " "', "ok"}, + {progname .. ' -e "os.exit(0, true)"', "ok"}, + {progname .. ' -e "os.exit(20, true)"', "exit", 20}, + } + print("\n(some error messages are expected now)") + for _, v in ipairs(tests) do + local x, y, z = io.popen(v[1]):close() + local x1, y1, z1 = os.execute(v[1]) + assert(x == x1 and y == y1 and z == z1) + if v[2] == "ok" then + assert(x and y == 'exit' and z == 0) + else + assert(not x and y == v[2]) -- correct status and 'what' + -- correct code if known (but always different from 0) + assert((v[3] == nil and z > 0) or v[3] == z) + end + end +end + + +-- testing tmpfile +f = io.tmpfile() +assert(io.type(f) == "file") +f:write("alo") +f:seek("set") +assert(f:read"a" == "alo") + +end --} + +print'+' + +print("testing date/time") + +assert(os.date("") == "") +assert(os.date("!") == "") +assert(os.date("\0\0") == "\0\0") +assert(os.date("!\0\0") == "\0\0") +local x = string.rep("a", 10000) +assert(os.date(x) == x) +local t = os.time() +D = os.date("*t", t) +assert(os.date(string.rep("%d", 1000), t) == + string.rep(os.date("%d", t), 1000)) +assert(os.date(string.rep("%", 200)) == string.rep("%", 100)) + +local t = os.time() +D = os.date("*t", t) +load(os.date([[assert(D.year==%Y and D.month==%m and D.day==%d and + D.hour==%H and D.min==%M and D.sec==%S and + D.wday==%w+1 and D.yday==%j and type(D.isdst) == 'boolean')]], t))() + +checkerr("invalid conversion specifier", os.date, "%") +checkerr("invalid conversion specifier", os.date, "%9") +checkerr("invalid conversion specifier", os.date, "%") +checkerr("invalid conversion specifier", os.date, "%O") +checkerr("invalid conversion specifier", os.date, "%E") +checkerr("invalid conversion specifier", os.date, "%Ea") + +checkerr("not an integer", os.time, {year=1000, month=1, day=1, hour='x'}) +checkerr("not an integer", os.time, {year=1000, month=1, day=1, hour=1.5}) + +checkerr("missing", os.time, {hour = 12}) -- missing date + +if not _port then + -- test Posix-specific modifiers + assert(type(os.date("%Ex")) == 'string') + assert(type(os.date("%Oy")) == 'string') + + + -- test out-of-range dates (at least for Unix) + if maxint >= 2^62 then -- cannot do these tests in Small Lua + -- no arith overflows + checkerr("out-of-bound", os.time, {year = -maxint, month = 1, day = 1}) + if string.packsize("i") == 4 then -- 4-byte ints + if testerr("out-of-bound", os.date, "%Y", 2^40) then + -- time_t has 4 bytes and therefore cannot represent year 4000 + print(" 4-byte time_t") + checkerr("cannot be represented", os.time, {year=4000, month=1, day=1}) + else + -- time_t has 8 bytes; an int year cannot represent a huge time + print(" 8-byte time_t") + checkerr("cannot be represented", os.date, "%Y", 2^60) + -- it should have no problems with year 4000 + assert(tonumber(os.time{year=4000, month=1, day=1})) + end + else -- 8-byte ints + -- assume time_t has 8 bytes too + print(" 8-byte time_t") + assert(tonumber(os.date("%Y", 2^60))) + -- but still cannot represent a huge year + checkerr("cannot be represented", os.time, {year=2^60, month=1, day=1}) + end + end +end + + +D = os.date("!*t", t) +load(os.date([[!assert(D.year==%Y and D.month==%m and D.day==%d and + D.hour==%H and D.min==%M and D.sec==%S and + D.wday==%w+1 and D.yday==%j and type(D.isdst) == 'boolean')]], t))() + +do + local D = os.date("*t") + local t = os.time(D) + assert(type(D.isdst) == 'boolean') + D.isdst = nil + local t1 = os.time(D) + assert(t == t1) -- if isdst is absent uses correct default +end + +t = os.time(D) +D.year = D.year-1; +local t1 = os.time(D) +-- allow for leap years +assert(math.abs(os.difftime(t,t1)/(24*3600) - 365) < 2) + +-- should not take more than 1 second to execute these two lines +t = os.time() +t1 = os.time(os.date("*t")) +local diff = os.difftime(t1,t) +assert(0 <= diff and diff <= 1) +diff = os.difftime(t,t1) +assert(-1 <= diff and diff <= 0) + +local t1 = os.time{year=2000, month=10, day=1, hour=23, min=12} +local t2 = os.time{year=2000, month=10, day=1, hour=23, min=10, sec=19} +assert(os.difftime(t1,t2) == 60*2-19) + +-- since 5.3.3, 'os.time' normalizes table fields +t1 = {year = 2005, month = 1, day = 1, hour = 1, min = 0, sec = -3602} +os.time(t1) +assert(t1.day == 31 and t1.month == 12 and t1.year == 2004 and + t1.hour == 23 and t1.min == 59 and t1.sec == 58 and + t1.yday == 366) + +io.output(io.stdout) +local t = os.date('%d %m %Y %H %M %S') +local d, m, a, h, min, s = string.match(t, + "(%d+) (%d+) (%d+) (%d+) (%d+) (%d+)") +d = tonumber(d) +m = tonumber(m) +a = tonumber(a) +h = tonumber(h) +min = tonumber(min) +s = tonumber(s) +io.write(string.format('test done on %2.2d/%2.2d/%d', d, m, a)) +io.write(string.format(', at %2.2d:%2.2d:%2.2d\n', h, min, s)) +io.write(string.format('%s\n', _VERSION)) + + diff --git a/testes/gc.lua b/testes/gc.lua new file mode 100644 index 0000000000..9647cd542f --- /dev/null +++ b/testes/gc.lua @@ -0,0 +1,661 @@ +-- $Id: gc.lua,v 1.82 2018/03/12 14:19:36 roberto Exp $ +-- See Copyright Notice in file all.lua + +print('testing garbage collection') + +local debug = require"debug" + +assert(collectgarbage("isrunning")) + +collectgarbage() + +local oldmode = collectgarbage("incremental") + + +local function gcinfo () + return collectgarbage"count" * 1024 +end + + +-- test weird parameters to 'collectgarbage' +do + -- save original parameters + local a = collectgarbage("setpause", 200) + local b = collectgarbage("setstepmul", 200) + local t = {0, 2, 10, 90, 500, 5000, 30000, 0x7ffffffe} + for i = 1, #t do + local p = t[i] + for j = 1, #t do + local m = t[j] + collectgarbage("setpause", p) + collectgarbage("setstepmul", m) + collectgarbage("step", 0) + collectgarbage("step", 10000) + end + end + -- restore original parameters + collectgarbage("setpause", a) + collectgarbage("setstepmul", b) + collectgarbage() +end + + +_G["while"] = 234 + + +-- +-- tests for GC activation when creating different kinds of objects +-- +local function GC1 () + local u + local b -- (above 'u' it in the stack) + local finish = false + u = setmetatable({}, {__gc = function () finish = true end}) + b = {34} + repeat u = {} until finish + assert(b[1] == 34) -- 'u' was collected, but 'b' was not + + finish = false; local i = 1 + u = setmetatable({}, {__gc = function () finish = true end}) + repeat i = i + 1; u = tostring(i) .. tostring(i) until finish + assert(b[1] == 34) -- 'u' was collected, but 'b' was not + + finish = false + u = setmetatable({}, {__gc = function () finish = true end}) + repeat local i; u = function () return i end until finish + assert(b[1] == 34) -- 'u' was collected, but 'b' was not +end + +local function GC2 () + local u + local finish = false + u = {setmetatable({}, {__gc = function () finish = true end})} + local b = {34} + repeat u = {{}} until finish + assert(b[1] == 34) -- 'u' was collected, but 'b' was not + + finish = false; local i = 1 + u = {setmetatable({}, {__gc = function () finish = true end})} + repeat i = i + 1; u = {tostring(i) .. tostring(i)} until finish + assert(b[1] == 34) -- 'u' was collected, but 'b' was not + + finish = false + u = {setmetatable({}, {__gc = function () finish = true end})} + repeat local i; u = {function () return i end} until finish + assert(b[1] == 34) -- 'u' was collected, but 'b' was not +end + +local function GC() GC1(); GC2() end + + +do + print("creating many objects") + + local contCreate = 0 + + local limit = 5000 + + while contCreate <= limit do + local a = {}; a = nil + contCreate = contCreate+1 + end + + local a = "a" + + contCreate = 0 + while contCreate <= limit do + a = contCreate .. "b"; + a = string.gsub(a, '(%d%d*)', string.upper) + a = "a" + contCreate = contCreate+1 + end + + + contCreate = 0 + + a = {} + + function a:test () + while contCreate <= limit do + load(string.format("function temp(a) return 'a%d' end", contCreate), "")() + assert(temp() == string.format('a%d', contCreate)) + contCreate = contCreate+1 + end + end + + a:test() + +end + + +-- collection of functions without locals, globals, etc. +do local f = function () end end + + +print("functions with errors") +prog = [[ +do + a = 10; + function foo(x,y) + a = sin(a+0.456-0.23e-12); + return function (z) return sin(%x+z) end + end + local x = function (w) a=a+w; end +end +]] +do + local step = 1 + if _soft then step = 13 end + for i=1, string.len(prog), step do + for j=i, string.len(prog), step do + pcall(load(string.sub(prog, i, j), "")) + end + end +end + +foo = nil +print('long strings') +x = "01234567890123456789012345678901234567890123456789012345678901234567890123456789" +assert(string.len(x)==80) +s = '' +n = 0 +k = math.min(300, (math.maxinteger // 80) // 2) +while n < k do s = s..x; n=n+1; j=tostring(n) end +assert(string.len(s) == k*80) +s = string.sub(s, 1, 10000) +s, i = string.gsub(s, '(%d%d%d%d)', '') +assert(i==10000 // 4) +s = nil +x = nil + +assert(_G["while"] == 234) + + +-- +-- test the "size" of basic GC steps (whatever they mean...) +-- +do +print("steps") + + print("steps (2)") + + local function dosteps (siz) + collectgarbage() + local a = {} + for i=1,100 do a[i] = {{}}; local b = {} end + local x = gcinfo() + local i = 0 + repeat -- do steps until it completes a collection cycle + i = i+1 + until collectgarbage("step", siz) + assert(gcinfo() < x) + return i -- number of steps + end + + collectgarbage"stop" + + if not _port then + assert(dosteps(10) < dosteps(2)) + end + + -- collector should do a full collection with so many steps + assert(dosteps(20000) == 1) + assert(collectgarbage("step", 20000) == true) + assert(collectgarbage("step", 20000) == true) + + assert(not collectgarbage("isrunning")) + collectgarbage"restart" + assert(collectgarbage("isrunning")) + +end + + +if not _port then + -- test the pace of the collector + collectgarbage(); collectgarbage() + local x = gcinfo() + collectgarbage"stop" + repeat + local a = {} + until gcinfo() > 3 * x + collectgarbage"restart" + assert(collectgarbage("isrunning")) + repeat + local a = {} + until gcinfo() <= x * 2 +end + + +print("clearing tables") +lim = 15 +a = {} +-- fill a with `collectable' indices +for i=1,lim do a[{}] = i end +b = {} +for k,v in pairs(a) do b[k]=v end +-- remove all indices and collect them +for n in pairs(b) do + a[n] = undef + assert(type(n) == 'table' and next(n) == nil) + collectgarbage() +end +b = nil +collectgarbage() +for n in pairs(a) do error'cannot be here' end +for i=1,lim do a[i] = i end +for i=1,lim do assert(a[i] == i) end + + +print('weak tables') +a = {}; setmetatable(a, {__mode = 'k'}); +-- fill a with some `collectable' indices +for i=1,lim do a[{}] = i end +-- and some non-collectable ones +for i=1,lim do a[i] = i end +for i=1,lim do local s=string.rep('@', i); a[s] = s..'#' end +collectgarbage() +local i = 0 +for k,v in pairs(a) do assert(k==v or k..'#'==v); i=i+1 end +assert(i == 2*lim) + +a = {}; setmetatable(a, {__mode = 'v'}); +a[1] = string.rep('b', 21) +collectgarbage() +assert(a[1]) -- strings are *values* +a[1] = undef +-- fill a with some `collectable' values (in both parts of the table) +for i=1,lim do a[i] = {} end +for i=1,lim do a[i..'x'] = {} end +-- and some non-collectable ones +for i=1,lim do local t={}; a[t]=t end +for i=1,lim do a[i+lim]=i..'x' end +collectgarbage() +local i = 0 +for k,v in pairs(a) do assert(k==v or k-lim..'x' == v); i=i+1 end +assert(i == 2*lim) + +a = {}; setmetatable(a, {__mode = 'kv'}); +local x, y, z = {}, {}, {} +-- keep only some items +a[1], a[2], a[3] = x, y, z +a[string.rep('$', 11)] = string.rep('$', 11) +-- fill a with some `collectable' values +for i=4,lim do a[i] = {} end +for i=1,lim do a[{}] = i end +for i=1,lim do local t={}; a[t]=t end +collectgarbage() +assert(next(a) ~= nil) +local i = 0 +for k,v in pairs(a) do + assert((k == 1 and v == x) or + (k == 2 and v == y) or + (k == 3 and v == z) or k==v); + i = i+1 +end +assert(i == 4) +x,y,z=nil +collectgarbage() +assert(next(a) == string.rep('$', 11)) + + +-- 'bug' in 5.1 +a = {} +local t = {x = 10} +local C = setmetatable({key = t}, {__mode = 'v'}) +local C1 = setmetatable({[t] = 1}, {__mode = 'k'}) +a.x = t -- this should not prevent 't' from being removed from + -- weak table 'C' by the time 'a' is finalized + +setmetatable(a, {__gc = function (u) + assert(C.key == nil) + assert(type(next(C1)) == 'table') + end}) + +a, t = nil +collectgarbage() +collectgarbage() +assert(next(C) == nil and next(C1) == nil) +C, C1 = nil + + +-- ephemerons +local mt = {__mode = 'k'} +a = {{10},{20},{30},{40}}; setmetatable(a, mt) +x = nil +for i = 1, 100 do local n = {}; a[n] = {k = {x}}; x = n end +GC() +local n = x +local i = 0 +while n do n = a[n].k[1]; i = i + 1 end +assert(i == 100) +x = nil +GC() +for i = 1, 4 do assert(a[i][1] == i * 10); a[i] = undef end +assert(next(a) == nil) + +local K = {} +a[K] = {} +for i=1,10 do a[K][i] = {}; a[a[K][i]] = setmetatable({}, mt) end +x = nil +local k = 1 +for j = 1,100 do + local n = {}; local nk = k%10 + 1 + a[a[K][nk]][n] = {x, k = k}; x = n; k = nk +end +GC() +local n = x +local i = 0 +while n do local t = a[a[K][k]][n]; n = t[1]; k = t.k; i = i + 1 end +assert(i == 100) +K = nil +GC() +-- assert(next(a) == nil) + + +-- testing errors during GC +do +collectgarbage("stop") -- stop collection +local u = {} +local s = {}; setmetatable(s, {__mode = 'k'}) +setmetatable(u, {__gc = function (o) + local i = s[o] + s[i] = true + assert(not s[i - 1]) -- check proper finalization order + if i == 8 then error("here") end -- error during GC +end}) + +for i = 6, 10 do + local n = setmetatable({}, getmetatable(u)) + s[n] = i +end + +assert(not pcall(collectgarbage)) +for i = 8, 10 do assert(s[i]) end + +for i = 1, 5 do + local n = setmetatable({}, getmetatable(u)) + s[n] = i +end + +collectgarbage() +for i = 1, 10 do assert(s[i]) end + +getmetatable(u).__gc = false + + +-- __gc errors with non-string messages +setmetatable({}, {__gc = function () error{} end}) +local a, b = pcall(collectgarbage) +assert(not a and type(b) == "string" and string.find(b, "error in __gc")) + +end +print '+' + + +-- testing userdata +if T==nil then + (Message or print)('\n >>> testC not active: skipping userdata GC tests <<<\n') + +else + + local function newproxy(u) + return debug.setmetatable(T.newuserdata(0), debug.getmetatable(u)) + end + + collectgarbage("stop") -- stop collection + local u = newproxy(nil) + debug.setmetatable(u, {__gc = true}) + local s = 0 + local a = {[u] = 0}; setmetatable(a, {__mode = 'vk'}) + for i=1,10 do a[newproxy(u)] = i end + for k in pairs(a) do assert(getmetatable(k) == getmetatable(u)) end + local a1 = {}; for k,v in pairs(a) do a1[k] = v end + for k,v in pairs(a1) do a[v] = k end + for i =1,10 do assert(a[i]) end + getmetatable(u).a = a1 + getmetatable(u).u = u + do + local u = u + getmetatable(u).__gc = function (o) + assert(a[o] == 10-s) + assert(a[10-s] == undef) -- udata already removed from weak table + assert(getmetatable(o) == getmetatable(u)) + assert(getmetatable(o).a[o] == 10-s) + s=s+1 + end + end + a1, u = nil + assert(next(a) ~= nil) + collectgarbage() + assert(s==11) + collectgarbage() + assert(next(a) == nil) -- finalized keys are removed in two cycles +end + + +-- __gc x weak tables +local u = setmetatable({}, {__gc = true}) +-- __gc metamethod should be collected before running +setmetatable(getmetatable(u), {__mode = "v"}) +getmetatable(u).__gc = function (o) os.exit(1) end -- cannot happen +u = nil +collectgarbage() + +local u = setmetatable({}, {__gc = true}) +local m = getmetatable(u) +m.x = {[{0}] = 1; [0] = {1}}; setmetatable(m.x, {__mode = "kv"}); +m.__gc = function (o) + assert(next(getmetatable(o).x) == nil) + m = 10 +end +u, m = nil +collectgarbage() +assert(m==10) + +do -- tests for string keys in weak tables + collectgarbage(); collectgarbage() + local m = collectgarbage("count") -- current memory + local a = setmetatable({}, {__mode = "kv"}) + a[string.rep("a", 2^22)] = 25 -- long string key -> number value + a[string.rep("b", 2^22)] = {} -- long string key -> colectable value + a[{}] = 14 -- colectable key + assert(collectgarbage("count") > m + 2^13) -- 2^13 == 2 * 2^22 in KB + collectgarbage() + assert(collectgarbage("count") >= m + 2^12 and + collectgarbage("count") < m + 2^13) -- one key was collected + local k, v = next(a) -- string key with number value preserved + assert(k == string.rep("a", 2^22) and v == 25) + assert(next(a, k) == nil) -- everything else cleared + assert(a[string.rep("b", 2^22)] == undef) + a[k] = undef -- erase this last entry + k = nil + collectgarbage() + assert(next(a) == nil) + -- make sure will not try to compare with dead key + assert(a[string.rep("b", 100)] == undef) + assert(collectgarbage("count") <= m + 1) -- eveything collected +end + + +-- errors during collection +u = setmetatable({}, {__gc = function () error "!!!" end}) +u = nil +assert(not pcall(collectgarbage)) + + +if not _soft then + print("long list") + local a = {} + for i = 1,200000 do + a = {next = a} + end + a = nil + collectgarbage() +end + +-- create many threads with self-references and open upvalues +print("self-referenced threads") +local thread_id = 0 +local threads = {} + +local function fn (thread) + local x = {} + threads[thread_id] = function() + thread = x + end + coroutine.yield() +end + +while thread_id < 1000 do + local thread = coroutine.create(fn) + coroutine.resume(thread, thread) + thread_id = thread_id + 1 +end + + +-- Create a closure (function inside 'f') with an upvalue ('param') that +-- points (through a table) to the closure itself and to the thread +-- ('co' and the initial value of 'param') where closure is running. +-- Then, assert that table (and therefore everything else) will be +-- collected. +do + local collected = false -- to detect collection + collectgarbage(); collectgarbage("stop") + do + local function f (param) + ;(function () + assert(type(f) == 'function' and type(param) == 'thread') + param = {param, f} + setmetatable(param, {__gc = function () collected = true end}) + coroutine.yield(100) + end)() + end + local co = coroutine.create(f) + assert(coroutine.resume(co, co)) + end + -- Now, thread and closure are not reacheable any more. + collectgarbage() + assert(collected) + collectgarbage("restart") +end + + +do + collectgarbage() + collectgarbage"stop" + collectgarbage("step", 0) -- steps should not unblock the collector + local x = gcinfo() + repeat + for i=1,1000 do _ENV.a = {} end -- no collection during the loop + until gcinfo() > 2 * x + collectgarbage"restart" +end + + +if T then -- tests for weird cases collecting upvalues + + local function foo () + local a = {x = 20} + coroutine.yield(function () return a.x end) -- will run collector + assert(a.x == 20) -- 'a' is 'ok' + a = {x = 30} -- create a new object + assert(T.gccolor(a) == "white") -- of course it is new... + coroutine.yield(100) -- 'a' is still local to this thread + end + + local t = setmetatable({}, {__mode = "kv"}) + collectgarbage(); collectgarbage('stop') + -- create coroutine in a weak table, so it will never be marked + t.co = coroutine.wrap(foo) + local f = t.co() -- create function to access local 'a' + T.gcstate("atomic") -- ensure all objects are traversed + assert(T.gcstate() == "atomic") + assert(t.co() == 100) -- resume coroutine, creating new table for 'a' + assert(T.gccolor(t.co) == "white") -- thread was not traversed + T.gcstate("pause") -- collect thread, but should mark 'a' before that + assert(t.co == nil and f() == 30) -- ensure correct access to 'a' + + collectgarbage("restart") + + -- test barrier in sweep phase (backing userdata to gray) + local u = T.newuserdata(0, 1) -- create a userdata + collectgarbage() + collectgarbage"stop" + local a = {} -- avoid 'u' as first element in 'allgc' + T.gcstate"atomic" + T.gcstate"sweepallgc" + local x = {} + assert(T.gccolor(u) == "black") -- userdata is "old" (black) + assert(T.gccolor(x) == "white") -- table is "new" (white) + debug.setuservalue(u, x) -- trigger barrier + assert(T.gccolor(u) == "gray") -- userdata changed back to gray + collectgarbage"restart" + + print"+" +end + + +if T then + local debug = require "debug" + collectgarbage("stop") + local x = T.newuserdata(0) + local y = T.newuserdata(0) + debug.setmetatable(y, {__gc = true}) -- bless the new udata before... + debug.setmetatable(x, {__gc = true}) -- ...the old one + assert(T.gccolor(y) == "white") + T.checkmemory() + collectgarbage("restart") +end + + +if T then + print("emergency collections") + collectgarbage() + collectgarbage() + T.totalmem(T.totalmem() + 200) + for i=1,200 do local a = {} end + T.totalmem(0) + collectgarbage() + local t = T.totalmem("table") + local a = {{}, {}, {}} -- create 4 new tables + assert(T.totalmem("table") == t + 4) + t = T.totalmem("function") + a = function () end -- create 1 new closure + assert(T.totalmem("function") == t + 1) + t = T.totalmem("thread") + a = coroutine.create(function () end) -- create 1 new coroutine + assert(T.totalmem("thread") == t + 1) +end + +-- create an object to be collected when state is closed +do + local setmetatable,assert,type,print,getmetatable = + setmetatable,assert,type,print,getmetatable + local tt = {} + tt.__gc = function (o) + assert(getmetatable(o) == tt) + -- create new objects during GC + local a = 'xuxu'..(10+3)..'joao', {} + ___Glob = o -- ressurect object! + setmetatable({}, tt) -- creates a new one with same metatable + print(">>> closing state " .. "<<<\n") + end + local u = setmetatable({}, tt) + ___Glob = {u} -- avoid object being collected before program end +end + +-- create several objects to raise errors when collected while closing state +do + local mt = {__gc = function (o) return o + 1 end} + for i = 1,10 do + -- create object and preserve it until the end + table.insert(___Glob, setmetatable({}, mt)) + end +end + +-- just to make sure +assert(collectgarbage'isrunning') + +collectgarbage(oldmode) + +print('OK') diff --git a/testes/goto.lua b/testes/goto.lua new file mode 100644 index 0000000000..d22601f934 --- /dev/null +++ b/testes/goto.lua @@ -0,0 +1,256 @@ +-- $Id: goto.lua,v 1.15 2017/11/30 13:31:07 roberto Exp $ +-- See Copyright Notice in file all.lua + +collectgarbage() + +local function errmsg (code, m) + local st, msg = load(code) + assert(not st and string.find(msg, m)) +end + +-- cannot see label inside block +errmsg([[ goto l1; do ::l1:: end ]], "label 'l1'") +errmsg([[ do ::l1:: end goto l1; ]], "label 'l1'") + +-- repeated label +errmsg([[ ::l1:: ::l1:: ]], "label 'l1'") + + +-- undefined label +errmsg([[ goto l1; local aa ::l1:: ::l2:: print(3) ]], "local 'aa'") + +-- jumping over variable definition +errmsg([[ +do local bb, cc; goto l1; end +local aa +::l1:: print(3) +]], "local 'aa'") + +-- jumping into a block +errmsg([[ do ::l1:: end goto l1 ]], "label 'l1'") +errmsg([[ goto l1 do ::l1:: end ]], "label 'l1'") + +-- cannot continue a repeat-until with variables +errmsg([[ + repeat + if x then goto cont end + local xuxu = 10 + ::cont:: + until xuxu < x +]], "local 'xuxu'") + +-- simple gotos +local x +do + local y = 12 + goto l1 + ::l2:: x = x + 1; goto l3 + ::l1:: x = y; goto l2 +end +::l3:: ::l3_1:: assert(x == 13) + + +-- long labels +do + local prog = [[ + do + local a = 1 + goto l%sa; a = a + 1 + ::l%sa:: a = a + 10 + goto l%sb; a = a + 2 + ::l%sb:: a = a + 20 + return a + end + ]] + local label = string.rep("0123456789", 40) + prog = string.format(prog, label, label, label, label) + assert(assert(load(prog))() == 31) +end + +-- goto to correct label when nested +do goto l3; ::l3:: end -- does not loop jumping to previous label 'l3' + +-- ok to jump over local dec. to end of block +do + goto l1 + local a = 23 + x = a + ::l1::; +end + +while true do + goto l4 + goto l1 -- ok to jump over local dec. to end of block + goto l1 -- multiple uses of same label + local x = 45 + ::l1:: ;;; +end +::l4:: assert(x == 13) + +if print then + goto l1 -- ok to jump over local dec. to end of block + error("should not be here") + goto l2 -- ok to jump over local dec. to end of block + local x + ::l1:: ; ::l2:: ;; +else end + +-- to repeat a label in a different function is OK +local function foo () + local a = {} + goto l3 + ::l1:: a[#a + 1] = 1; goto l2; + ::l2:: a[#a + 1] = 2; goto l5; + ::l3:: + ::l3a:: a[#a + 1] = 3; goto l1; + ::l4:: a[#a + 1] = 4; goto l6; + ::l5:: a[#a + 1] = 5; goto l4; + ::l6:: assert(a[1] == 3 and a[2] == 1 and a[3] == 2 and + a[4] == 5 and a[5] == 4) + if not a[6] then a[6] = true; goto l3a end -- do it twice +end + +::l6:: foo() + + +do -- bug in 5.2 -> 5.3.2 + local x + ::L1:: + local y -- cannot join this SETNIL with previous one + assert(y == nil) + y = true + if x == nil then + x = 1 + goto L1 + else + x = x + 1 + end + assert(x == 2 and y == true) +end + +-- bug in 5.3 +do + local first = true + local a = false + if true then + goto LBL + ::loop:: + a = true + ::LBL:: + if first then + first = false + goto loop + end + end + assert(a) +end + +do -- compiling infinite loops + goto escape -- do not run the infinite loops + ::a:: goto a + ::b:: goto c + ::c:: goto b +end +::escape:: +-------------------------------------------------------------------------------- +-- testing closing of upvalues + +local debug = require 'debug' + +local function foo () + local t = {} + do + local i = 1 + local a, b, c, d + t[1] = function () return a, b, c, d end + ::l1:: + local b + do + local c + t[#t + 1] = function () return a, b, c, d end -- t[2], t[4], t[6] + if i > 2 then goto l2 end + do + local d + t[#t + 1] = function () return a, b, c, d end -- t[3], t[5] + i = i + 1 + local a + goto l1 + end + end + end + ::l2:: return t +end + +local a = foo() +assert(#a == 6) + +-- all functions share same 'a' +for i = 2, 6 do + assert(debug.upvalueid(a[1], 1) == debug.upvalueid(a[i], 1)) +end + +-- 'b' and 'c' are shared among some of them +for i = 2, 6 do + -- only a[1] uses external 'b'/'b' + assert(debug.upvalueid(a[1], 2) ~= debug.upvalueid(a[i], 2)) + assert(debug.upvalueid(a[1], 3) ~= debug.upvalueid(a[i], 3)) +end + +for i = 3, 5, 2 do + -- inner functions share 'b'/'c' with previous ones + assert(debug.upvalueid(a[i], 2) == debug.upvalueid(a[i - 1], 2)) + assert(debug.upvalueid(a[i], 3) == debug.upvalueid(a[i - 1], 3)) + -- but not with next ones + assert(debug.upvalueid(a[i], 2) ~= debug.upvalueid(a[i + 1], 2)) + assert(debug.upvalueid(a[i], 3) ~= debug.upvalueid(a[i + 1], 3)) +end + +-- only external 'd' is shared +for i = 2, 6, 2 do + assert(debug.upvalueid(a[1], 4) == debug.upvalueid(a[i], 4)) +end + +-- internal 'd's are all different +for i = 3, 5, 2 do + for j = 1, 6 do + assert((debug.upvalueid(a[i], 4) == debug.upvalueid(a[j], 4)) + == (i == j)) + end +end + +-------------------------------------------------------------------------------- +-- testing if x goto optimizations + +local function testG (a) + if a == 1 then + goto l1 + error("should never be here!") + elseif a == 2 then goto l2 + elseif a == 3 then goto l3 + elseif a == 4 then + goto l1 -- go to inside the block + error("should never be here!") + ::l1:: a = a + 1 -- must go to 'if' end + else + goto l4 + ::l4a:: a = a * 2; goto l4b + error("should never be here!") + ::l4:: goto l4a + error("should never be here!") + ::l4b:: + end + do return a end + ::l2:: do return "2" end + ::l3:: do return "3" end + ::l1:: return "1" +end + +assert(testG(1) == "1") +assert(testG(2) == "2") +assert(testG(3) == "3") +assert(testG(4) == 5) +assert(testG(5) == 10) +-------------------------------------------------------------------------------- + + +print'OK' diff --git a/testes/libs/lib1.c b/testes/libs/lib1.c new file mode 100644 index 0000000000..56b6ef419c --- /dev/null +++ b/testes/libs/lib1.c @@ -0,0 +1,44 @@ +#include "lua.h" +#include "lauxlib.h" + +static int id (lua_State *L) { + return lua_gettop(L); +} + + +static const struct luaL_Reg funcs[] = { + {"id", id}, + {NULL, NULL} +}; + + +/* function used by lib11.c */ +LUAMOD_API int lib1_export (lua_State *L) { + lua_pushstring(L, "exported"); + return 1; +} + + +LUAMOD_API int onefunction (lua_State *L) { + luaL_checkversion(L); + lua_settop(L, 2); + lua_pushvalue(L, 1); + return 2; +} + + +LUAMOD_API int anotherfunc (lua_State *L) { + luaL_checkversion(L); + lua_pushfstring(L, "%d%%%d\n", (int)lua_tointeger(L, 1), + (int)lua_tointeger(L, 2)); + return 1; +} + + +LUAMOD_API int luaopen_lib1_sub (lua_State *L) { + lua_setglobal(L, "y"); /* 2nd arg: extra value (file name) */ + lua_setglobal(L, "x"); /* 1st arg: module name */ + luaL_newlib(L, funcs); + return 1; +} + diff --git a/testes/libs/lib11.c b/testes/libs/lib11.c new file mode 100644 index 0000000000..377d0c484f --- /dev/null +++ b/testes/libs/lib11.c @@ -0,0 +1,10 @@ +#include "lua.h" + +/* function from lib1.c */ +int lib1_export (lua_State *L); + +LUAMOD_API int luaopen_lib11 (lua_State *L) { + return lib1_export(L); +} + + diff --git a/testes/libs/lib2.c b/testes/libs/lib2.c new file mode 100644 index 0000000000..bc9651eea5 --- /dev/null +++ b/testes/libs/lib2.c @@ -0,0 +1,23 @@ +#include "lua.h" +#include "lauxlib.h" + +static int id (lua_State *L) { + return lua_gettop(L); +} + + +static const struct luaL_Reg funcs[] = { + {"id", id}, + {NULL, NULL} +}; + + +LUAMOD_API int luaopen_lib2 (lua_State *L) { + lua_settop(L, 2); + lua_setglobal(L, "y"); /* y gets 2nd parameter */ + lua_setglobal(L, "x"); /* x gets 1st parameter */ + luaL_newlib(L, funcs); + return 1; +} + + diff --git a/testes/libs/lib21.c b/testes/libs/lib21.c new file mode 100644 index 0000000000..a39b683d8c --- /dev/null +++ b/testes/libs/lib21.c @@ -0,0 +1,10 @@ +#include "lua.h" + + +int luaopen_lib2 (lua_State *L); + +LUAMOD_API int luaopen_lib21 (lua_State *L) { + return luaopen_lib2(L); +} + + diff --git a/testes/libs/makefile b/testes/libs/makefile new file mode 100644 index 0000000000..9925fb005b --- /dev/null +++ b/testes/libs/makefile @@ -0,0 +1,26 @@ +# change this variable to point to the directory with Lua headers +# of the version being tested +LUA_DIR = ../../ + +CC = gcc + +# compilation should generate Dynamic-Link Libraries +CFLAGS = -Wall -std=gnu99 -O2 -I$(LUA_DIR) -fPIC -shared + +# libraries used by the tests +all: lib1.so lib11.so lib2.so lib21.so lib2-v2.so + +lib1.so: lib1.c + $(CC) $(CFLAGS) -o lib1.so lib1.c + +lib11.so: lib11.c + $(CC) $(CFLAGS) -o lib11.so lib11.c + +lib2.so: lib2.c + $(CC) $(CFLAGS) -o lib2.so lib2.c + +lib21.so: lib21.c + $(CC) $(CFLAGS) -o lib21.so lib21.c + +lib2-v2.so: lib2.so + mv lib2.so ./lib2-v2.so diff --git a/testes/literals.lua b/testes/literals.lua new file mode 100644 index 0000000000..3922b3f502 --- /dev/null +++ b/testes/literals.lua @@ -0,0 +1,302 @@ +-- $Id: literals.lua,v 1.36 2016/11/07 13:11:28 roberto Exp $ +-- See Copyright Notice in file all.lua + +print('testing scanner') + +local debug = require "debug" + + +local function dostring (x) return assert(load(x), "")() end + +dostring("x \v\f = \t\r 'a\0a' \v\f\f") +assert(x == 'a\0a' and string.len(x) == 3) + +-- escape sequences +assert('\n\"\'\\' == [[ + +"'\]]) + +assert(string.find("\a\b\f\n\r\t\v", "^%c%c%c%c%c%c%c$")) + +-- assume ASCII just for tests: +assert("\09912" == 'c12') +assert("\99ab" == 'cab') +assert("\099" == '\99') +assert("\099\n" == 'c\10') +assert('\0\0\0alo' == '\0' .. '\0\0' .. 'alo') + +assert(010 .. 020 .. -030 == "1020-30") + +-- hexadecimal escapes +assert("\x00\x05\x10\x1f\x3C\xfF\xe8" == "\0\5\16\31\60\255\232") + +local function lexstring (x, y, n) + local f = assert(load('return ' .. x .. + ', require"debug".getinfo(1).currentline', '')) + local s, l = f() + assert(s == y and l == n) +end + +lexstring("'abc\\z \n efg'", "abcefg", 2) +lexstring("'abc\\z \n\n\n'", "abc", 4) +lexstring("'\\z \n\t\f\v\n'", "", 3) +lexstring("[[\nalo\nalo\n\n]]", "alo\nalo\n\n", 5) +lexstring("[[\nalo\ralo\n\n]]", "alo\nalo\n\n", 5) +lexstring("[[\nalo\ralo\r\n]]", "alo\nalo\n", 4) +lexstring("[[\ralo\n\ralo\r\n]]", "alo\nalo\n", 4) +lexstring("[[alo]\n]alo]]", "alo]\n]alo", 2) + +assert("abc\z + def\z + ghi\z + " == 'abcdefghi') + + +-- UTF-8 sequences +assert("\u{0}\u{00000000}\x00\0" == string.char(0, 0, 0, 0)) + +-- limits for 1-byte sequences +assert("\u{0}\u{7F}" == "\x00\z\x7F") + +-- limits for 2-byte sequences +assert("\u{80}\u{7FF}" == "\xC2\x80\z\xDF\xBF") + +-- limits for 3-byte sequences +assert("\u{800}\u{FFFF}" == "\xE0\xA0\x80\z\xEF\xBF\xBF") + +-- limits for 4-byte sequences +assert("\u{10000}\u{10FFFF}" == "\xF0\x90\x80\x80\z\xF4\x8F\xBF\xBF") + + +-- Error in escape sequences +local function lexerror (s, err) + local st, msg = load('return ' .. s, '') + if err ~= '' then err = err .. "'" end + assert(not st and string.find(msg, "near .-" .. err)) +end + +lexerror([["abc\x"]], [[\x"]]) +lexerror([["abc\x]], [[\x]]) +lexerror([["\x]], [[\x]]) +lexerror([["\x5"]], [[\x5"]]) +lexerror([["\x5]], [[\x5]]) +lexerror([["\xr"]], [[\xr]]) +lexerror([["\xr]], [[\xr]]) +lexerror([["\x.]], [[\x.]]) +lexerror([["\x8%"]], [[\x8%%]]) +lexerror([["\xAG]], [[\xAG]]) +lexerror([["\g"]], [[\g]]) +lexerror([["\g]], [[\g]]) +lexerror([["\."]], [[\%.]]) + +lexerror([["\999"]], [[\999"]]) +lexerror([["xyz\300"]], [[\300"]]) +lexerror([[" \256"]], [[\256"]]) + +-- errors in UTF-8 sequences +lexerror([["abc\u{110000}"]], [[abc\u{110000]]) -- too large +lexerror([["abc\u11r"]], [[abc\u1]]) -- missing '{' +lexerror([["abc\u"]], [[abc\u"]]) -- missing '{' +lexerror([["abc\u{11r"]], [[abc\u{11r]]) -- missing '}' +lexerror([["abc\u{11"]], [[abc\u{11"]]) -- missing '}' +lexerror([["abc\u{11]], [[abc\u{11]]) -- missing '}' +lexerror([["abc\u{r"]], [[abc\u{r]]) -- no digits + +-- unfinished strings +lexerror("[=[alo]]", "") +lexerror("[=[alo]=", "") +lexerror("[=[alo]", "") +lexerror("'alo", "") +lexerror("'alo \\z \n\n", "") +lexerror("'alo \\z", "") +lexerror([['alo \98]], "") + +-- valid characters in variable names +for i = 0, 255 do + local s = string.char(i) + assert(not string.find(s, "[a-zA-Z_]") == not load(s .. "=1", "")) + assert(not string.find(s, "[a-zA-Z_0-9]") == + not load("a" .. s .. "1 = 1", "")) +end + + +-- long variable names + +var1 = string.rep('a', 15000) .. '1' +var2 = string.rep('a', 15000) .. '2' +prog = string.format([[ + %s = 5 + %s = %s + 1 + return function () return %s - %s end +]], var1, var2, var1, var1, var2) +local f = dostring(prog) +assert(_G[var1] == 5 and _G[var2] == 6 and f() == -1) +var1, var2, f = nil +print('+') + +-- escapes -- +assert("\n\t" == [[ + + ]]) +assert([[ + + $debug]] == "\n $debug") +assert([[ [ ]] ~= [[ ] ]]) +-- long strings -- +b = "001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789" +assert(string.len(b) == 960) +prog = [=[ +print('+') + +a1 = [["this is a 'string' with several 'quotes'"]] +a2 = "'quotes'" + +assert(string.find(a1, a2) == 34) +print('+') + +a1 = [==[temp = [[an arbitrary value]]; ]==] +assert(load(a1))() +assert(temp == 'an arbitrary value') +-- long strings -- +b = "001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789" +assert(string.len(b) == 960) +print('+') + +a = [[00123456789012345678901234567890123456789123456789012345678901234567890123456789 +00123456789012345678901234567890123456789123456789012345678901234567890123456789 +00123456789012345678901234567890123456789123456789012345678901234567890123456789 +00123456789012345678901234567890123456789123456789012345678901234567890123456789 +00123456789012345678901234567890123456789123456789012345678901234567890123456789 +00123456789012345678901234567890123456789123456789012345678901234567890123456789 +00123456789012345678901234567890123456789123456789012345678901234567890123456789 +00123456789012345678901234567890123456789123456789012345678901234567890123456789 +00123456789012345678901234567890123456789123456789012345678901234567890123456789 +00123456789012345678901234567890123456789123456789012345678901234567890123456789 +00123456789012345678901234567890123456789123456789012345678901234567890123456789 +00123456789012345678901234567890123456789123456789012345678901234567890123456789 +00123456789012345678901234567890123456789123456789012345678901234567890123456789 +00123456789012345678901234567890123456789123456789012345678901234567890123456789 +00123456789012345678901234567890123456789123456789012345678901234567890123456789 +00123456789012345678901234567890123456789123456789012345678901234567890123456789 +00123456789012345678901234567890123456789123456789012345678901234567890123456789 +00123456789012345678901234567890123456789123456789012345678901234567890123456789 +00123456789012345678901234567890123456789123456789012345678901234567890123456789 +00123456789012345678901234567890123456789123456789012345678901234567890123456789 +00123456789012345678901234567890123456789123456789012345678901234567890123456789 +00123456789012345678901234567890123456789123456789012345678901234567890123456789 +00123456789012345678901234567890123456789123456789012345678901234567890123456789 +]] +assert(string.len(a) == 1863) +assert(string.sub(a, 1, 40) == string.sub(b, 1, 40)) +x = 1 +]=] + +print('+') +x = nil +dostring(prog) +assert(x) + +prog = nil +a = nil +b = nil + + +-- testing line ends +prog = [[ +a = 1 -- a comment +b = 2 + + +x = [=[ +hi +]=] +y = "\ +hello\r\n\ +" +return require"debug".getinfo(1).currentline +]] + +for _, n in pairs{"\n", "\r", "\n\r", "\r\n"} do + local prog, nn = string.gsub(prog, "\n", n) + assert(dostring(prog) == nn) + assert(_G.x == "hi\n" and _G.y == "\nhello\r\n\n") +end + + +-- testing comments and strings with long brackets +a = [==[]=]==] +assert(a == "]=") + +a = [==[[===[[=[]]=][====[]]===]===]==] +assert(a == "[===[[=[]]=][====[]]===]===") + +a = [====[[===[[=[]]=][====[]]===]===]====] +assert(a == "[===[[=[]]=][====[]]===]===") + +a = [=[]]]]]]]]]=] +assert(a == "]]]]]]]]") + + +--[===[ +x y z [==[ blu foo +]== +] +]=]==] +error error]=]===] + +-- generate all strings of four of these chars +local x = {"=", "[", "]", "\n"} +local len = 4 +local function gen (c, n) + if n==0 then coroutine.yield(c) + else + for _, a in pairs(x) do + gen(c..a, n-1) + end + end +end + +for s in coroutine.wrap(function () gen("", len) end) do + assert(s == load("return [====[\n"..s.."]====]", "")()) +end + + +-- testing decimal point locale +if os.setlocale("pt_BR") or os.setlocale("ptb") then + assert(tonumber("3,4") == 3.4 and tonumber"3.4" == 3.4) + assert(tonumber(" -.4 ") == -0.4) + assert(tonumber(" +0x.41 ") == 0X0.41) + assert(not load("a = (3,4)")) + assert(assert(load("return 3.4"))() == 3.4) + assert(assert(load("return .4,3"))() == .4) + assert(assert(load("return 4."))() == 4.) + assert(assert(load("return 4.+.5"))() == 4.5) + + assert(" 0x.1 " + " 0x,1" + "-0X.1\t" == 0x0.1) + + assert(tonumber"inf" == nil and tonumber"NAN" == nil) + + assert(assert(load(string.format("return %q", 4.51)))() == 4.51) + + local a,b = load("return 4.5.") + assert(string.find(b, "'4%.5%.'")) + + assert(os.setlocale("C")) +else + (Message or print)( + '\n >>> pt_BR locale not available: skipping decimal point tests <<<\n') +end + + +-- testing %q x line ends +local s = "a string with \r and \n and \r\n and \n\r" +local c = string.format("return %q", s) +assert(assert(load(c))() == s) + +-- testing errors +assert(not load"a = 'non-ending string") +assert(not load"a = 'non-ending string\n'") +assert(not load"a = '\\345'") +assert(not load"a = [=x]") + +print('OK') diff --git a/testes/locals.lua b/testes/locals.lua new file mode 100644 index 0000000000..f0780a031b --- /dev/null +++ b/testes/locals.lua @@ -0,0 +1,181 @@ +-- $Id: locals.lua,v 1.41 2018/06/19 12:25:39 roberto Exp $ +-- See Copyright Notice in file all.lua + +print('testing local variables and environments') + +local debug = require"debug" + + +-- bug in 5.1: + +local function f(x) x = nil; return x end +assert(f(10) == nil) + +local function f() local x; return x end +assert(f(10) == nil) + +local function f(x) x = nil; local y; return x, y end +assert(f(10) == nil and select(2, f(20)) == nil) + +do + local i = 10 + do local i = 100; assert(i==100) end + do local i = 1000; assert(i==1000) end + assert(i == 10) + if i ~= 10 then + local i = 20 + else + local i = 30 + assert(i == 30) + end +end + + + +f = nil + +local f +x = 1 + +a = nil +load('local a = {}')() +assert(a == nil) + +function f (a) + local _1, _2, _3, _4, _5 + local _6, _7, _8, _9, _10 + local x = 3 + local b = a + local c,d = a,b + if (d == b) then + local x = 'q' + x = b + assert(x == 2) + else + assert(nil) + end + assert(x == 3) + local f = 10 +end + +local b=10 +local a; repeat local b; a,b=1,2; assert(a+1==b); until a+b==3 + + +assert(x == 1) + +f(2) +assert(type(f) == 'function') + + +local function getenv (f) + local a,b = debug.getupvalue(f, 1) + assert(a == '_ENV') + return b +end + +-- test for global table of loaded chunks +assert(getenv(load"a=3") == _G) +local c = {}; local f = load("a = 3", nil, nil, c) +assert(getenv(f) == c) +assert(c.a == nil) +f() +assert(c.a == 3) + +-- old test for limits for special instructions (now just a generic test) +do + local i = 2 + local p = 4 -- p == 2^i + repeat + for j=-3,3 do + assert(load(string.format([[local a=%s; + a=a+%s; + assert(a ==2^%s)]], j, p-j, i), '')) () + assert(load(string.format([[local a=%s; + a=a-%s; + assert(a==-2^%s)]], -j, p-j, i), '')) () + assert(load(string.format([[local a,b=0,%s; + a=b-%s; + assert(a==-2^%s)]], -j, p-j, i), '')) () + end + p = 2 * p; i = i + 1 + until p <= 0 +end + +print'+' + + +if rawget(_G, "T") then + -- testing clearing of dead elements from tables + collectgarbage("stop") -- stop GC + local a = {[{}] = 4, [3] = 0, alo = 1, + a1234567890123456789012345678901234567890 = 10} + + local t = T.querytab(a) + + for k,_ in pairs(a) do a[k] = undef end + collectgarbage() -- restore GC and collect dead fiels in `a' + for i=0,t-1 do + local k = querytab(a, i) + assert(k == nil or type(k) == 'number' or k == 'alo') + end + + -- testing allocation errors during table insertions + local a = {} + local function additems () + a.x = true; a.y = true; a.z = true + a[1] = true + a[2] = true + end + for i = 1, math.huge do + T.alloccount(i) + local st, msg = pcall(additems) + T.alloccount() + local count = 0 + for k, v in pairs(a) do + assert(a[k] == v) + count = count + 1 + end + if st then assert(count == 5); break end + end +end + + +-- testing lexical environments + +assert(_ENV == _G) + +do +local dummy +local _ENV = (function (...) return ... end)(_G, dummy) -- { + +do local _ENV = {assert=assert}; assert(true) end +mt = {_G = _G} +local foo,x +A = false -- "declare" A +do local _ENV = mt + function foo (x) + A = x + do local _ENV = _G; A = 1000 end + return function (x) return A .. x end + end +end +assert(getenv(foo) == mt) +x = foo('hi'); assert(mt.A == 'hi' and A == 1000) +assert(x('*') == mt.A .. '*') + +do local _ENV = {assert=assert, A=10}; + do local _ENV = {assert=assert, A=20}; + assert(A==20);x=A + end + assert(A==10 and x==20) +end +assert(x==20) + + +print('OK') + +return 5,f + +end -- } + diff --git a/testes/main.lua b/testes/main.lua new file mode 100644 index 0000000000..582b39c02e --- /dev/null +++ b/testes/main.lua @@ -0,0 +1,381 @@ +# testing special comment on first line +-- $Id: main.lua,v 1.69 2018/06/19 12:23:50 roberto Exp $ +-- See Copyright Notice in file all.lua + +-- most (all?) tests here assume a reasonable "Unix-like" shell +if _port then return end + +-- use only "double quotes" inside shell scripts (better change to +-- run on Windows) + + +print ("testing stand-alone interpreter") + +assert(os.execute()) -- machine has a system command + +local arg = arg or ARG + +local prog = os.tmpname() +local otherprog = os.tmpname() +local out = os.tmpname() + +local progname +do + local i = 0 + while arg[i] do i=i-1 end + progname = arg[i+1] +end +print("progname: "..progname) + +local prepfile = function (s, p) + p = p or prog + io.output(p) + io.write(s) + assert(io.close()) +end + +local function getoutput () + io.input(out) + local t = io.read("a") + io.input():close() + assert(os.remove(out)) + return t +end + +local function checkprogout (s) + local t = getoutput() + for line in string.gmatch(s, ".-\n") do + assert(string.find(t, line, 1, true)) + end +end + +local function checkout (s) + local t = getoutput() + if s ~= t then print(string.format("'%s' - '%s'\n", s, t)) end + assert(s == t) + return t +end + + +local function RUN (p, ...) + p = string.gsub(p, "lua", '"'..progname..'"', 1) + local s = string.format(p, ...) + assert(os.execute(s)) +end + +local function NoRun (msg, p, ...) + p = string.gsub(p, "lua", '"'..progname..'"', 1) + local s = string.format(p, ...) + s = string.format("%s 2> %s", s, out) -- will send error to 'out' + assert(not os.execute(s)) + assert(string.find(getoutput(), msg, 1, true)) -- check error message +end + +RUN('lua -v') + +print(string.format("(temporary program file used in these tests: %s)", prog)) + +-- running stdin as a file +prepfile"" +RUN('lua - < %s > %s', prog, out) +checkout("") + +prepfile[[ + print( +1, a +) +]] +RUN('lua - < %s > %s', prog, out) +checkout("1\tnil\n") + +RUN('echo "print(10)\nprint(2)\n" | lua > %s', out) +checkout("10\n2\n") + + +-- test option '-' +RUN('echo "print(arg[1])" | lua - -h > %s', out) +checkout("-h\n") + +-- test environment variables used by Lua + +prepfile("print(package.path)") + +-- test LUA_PATH +RUN('env LUA_INIT= LUA_PATH=x lua %s > %s', prog, out) +checkout("x\n") + +-- test LUA_PATH_version +RUN('env LUA_INIT= LUA_PATH_5_4=y LUA_PATH=x lua %s > %s', prog, out) +checkout("y\n") + +-- test LUA_CPATH +prepfile("print(package.cpath)") +RUN('env LUA_INIT= LUA_CPATH=xuxu lua %s > %s', prog, out) +checkout("xuxu\n") + +-- test LUA_CPATH_version +RUN('env LUA_INIT= LUA_CPATH_5_4=yacc LUA_CPATH=x lua %s > %s', prog, out) +checkout("yacc\n") + +-- test LUA_INIT (and its access to 'arg' table) +prepfile("print(X)") +RUN('env LUA_INIT="X=tonumber(arg[1])" lua %s 3.2 > %s', prog, out) +checkout("3.2\n") + +-- test LUA_INIT_version +prepfile("print(X)") +RUN('env LUA_INIT_5_4="X=10" LUA_INIT="X=3" lua %s > %s', prog, out) +checkout("10\n") + +-- test LUA_INIT for files +prepfile("x = x or 10; print(x); x = x + 1") +RUN('env LUA_INIT="@%s" lua %s > %s', prog, prog, out) +checkout("10\n11\n") + +-- test errors in LUA_INIT +NoRun('LUA_INIT:1: msg', 'env LUA_INIT="error(\'msg\')" lua') + +-- test option '-E' +local defaultpath, defaultCpath + +do + prepfile("print(package.path, package.cpath)") + RUN('env LUA_INIT="error(10)" LUA_PATH=xxx LUA_CPATH=xxx lua -E %s > %s', + prog, out) + local out = getoutput() + defaultpath = string.match(out, "^(.-)\t") + defaultCpath = string.match(out, "\t(.-)$") +end + +-- paths did not changed +assert(not string.find(defaultpath, "xxx") and + string.find(defaultpath, "lua") and + not string.find(defaultCpath, "xxx") and + string.find(defaultCpath, "lua")) + + +-- test replacement of ';;' to default path +local function convert (p) + prepfile("print(package.path)") + RUN('env LUA_PATH="%s" lua %s > %s', p, prog, out) + local expected = getoutput() + expected = string.sub(expected, 1, -2) -- cut final end of line + assert(string.gsub(p, ";;", ";"..defaultpath..";") == expected) +end + +convert(";") +convert(";;") +convert(";;;") +convert(";;;;") +convert(";;;;;") +convert(";;a;;;bc") + + +-- test -l over multiple libraries +prepfile("print(1); a=2; return {x=15}") +prepfile(("print(a); print(_G['%s'].x)"):format(prog), otherprog) +RUN('env LUA_PATH="?;;" lua -l %s -l%s -lstring -l io %s > %s', prog, otherprog, otherprog, out) +checkout("1\n2\n15\n2\n15\n") + +-- test 'arg' table +local a = [[ + assert(#arg == 3 and arg[1] == 'a' and + arg[2] == 'b' and arg[3] == 'c') + assert(arg[-1] == '--' and arg[-2] == "-e " and arg[-3] == '%s') + assert(arg[4] == undef and arg[-4] == undef) + local a, b, c = ... + assert(... == 'a' and a == 'a' and b == 'b' and c == 'c') +]] +a = string.format(a, progname) +prepfile(a) +RUN('lua "-e " -- %s a b c', prog) -- "-e " runs an empty command + +-- test 'arg' availability in libraries +prepfile"assert(arg)" +prepfile("assert(arg)", otherprog) +RUN('env LUA_PATH="?;;" lua -l%s - < %s', prog, otherprog) + +-- test messing up the 'arg' table +RUN('echo "print(...)" | lua -e "arg[1] = 100" - > %s', out) +checkout("100\n") +NoRun("'arg' is not a table", 'echo "" | lua -e "arg = 1" -') + +-- test error in 'print' +RUN('echo 10 | lua -e "print=nil" -i > /dev/null 2> %s', out) +assert(string.find(getoutput(), "error calling 'print'")) + +-- test 'debug.debug' +RUN('echo "io.stderr:write(1000)\ncont" | lua -e "require\'debug\'.debug()" 2> %s', out) +checkout("lua_debug> 1000lua_debug> ") + +-- test many arguments +prepfile[[print(({...})[30])]] +RUN('lua %s %s > %s', prog, string.rep(" a", 30), out) +checkout("a\n") + +RUN([[lua "-eprint(1)" -ea=3 -e "print(a)" > %s]], out) +checkout("1\n3\n") + +-- test iteractive mode +prepfile[[ +(6*2-6) -- === +a = +10 +print(a) +a]] +RUN([[lua -e"_PROMPT='' _PROMPT2=''" -i < %s > %s]], prog, out) +checkprogout("6\n10\n10\n\n") + +prepfile("a = [[b\nc\nd\ne]]\n=a") +RUN([[lua -e"_PROMPT='' _PROMPT2=''" -i < %s > %s]], prog, out) +checkprogout("b\nc\nd\ne\n\n") + +prompt = "alo" +prepfile[[ -- +a = 2 +]] +RUN([[lua "-e_PROMPT='%s'" -i < %s > %s]], prompt, prog, out) +local t = getoutput() +assert(string.find(t, prompt .. ".*" .. prompt .. ".*" .. prompt)) + +-- test for error objects +prepfile[[ +debug = require "debug" +m = {x=0} +setmetatable(m, {__tostring = function(x) + return tostring(debug.getinfo(4).currentline + x.x) +end}) +error(m) +]] +NoRun(progname .. ": 6\n", [[lua %s]], prog) + +prepfile("error{}") +NoRun("error object is a table value", [[lua %s]], prog) + + +-- chunk broken in many lines +s = [=[ -- +function f ( x ) + local a = [[ +xuxu +]] + local b = "\ +xuxu\n" + if x == 11 then return 1 + 12 , 2 + 20 end --[[ test multiple returns ]] + return x + 1 + --\\ +end +return( f( 100 ) ) +assert( a == b ) +do return f( 11 ) end ]=] +s = string.gsub(s, ' ', '\n\n') -- change all spaces for newlines +prepfile(s) +RUN([[lua -e"_PROMPT='' _PROMPT2=''" -i < %s > %s]], prog, out) +checkprogout("101\n13\t22\n\n") + +prepfile[[#comment in 1st line without \n at the end]] +RUN('lua %s', prog) + +prepfile[[#test line number when file starts with comment line +debug = require"debug" +print(debug.getinfo(1).currentline) +]] +RUN('lua %s > %s', prog, out) +checkprogout('3') + +-- close Lua with an open file +prepfile(string.format([[io.output(%q); io.write('alo')]], out)) +RUN('lua %s', prog) +checkout('alo') + +-- bug in 5.2 beta (extra \0 after version line) +RUN([[lua -v -e"print'hello'" > %s]], out) +t = getoutput() +assert(string.find(t, "PUC%-Rio\nhello")) + + +-- testing os.exit +prepfile("os.exit(nil, true)") +RUN('lua %s', prog) +prepfile("os.exit(0, true)") +RUN('lua %s', prog) +prepfile("os.exit(true, true)") +RUN('lua %s', prog) +prepfile("os.exit(1, true)") +NoRun("", "lua %s", prog) -- no message +prepfile("os.exit(false, true)") +NoRun("", "lua %s", prog) -- no message + +-- remove temporary files +assert(os.remove(prog)) +assert(os.remove(otherprog)) +assert(not os.remove(out)) + +-- invalid options +NoRun("unrecognized option '-h'", "lua -h") +NoRun("unrecognized option '---'", "lua ---") +NoRun("unrecognized option '-Ex'", "lua -Ex") +NoRun("unrecognized option '-vv'", "lua -vv") +NoRun("unrecognized option '-iv'", "lua -iv") +NoRun("'-e' needs argument", "lua -e") +NoRun("syntax error", "lua -e a") +NoRun("'-l' needs argument", "lua -l") + + +if T then -- auxiliary library? + print("testing 'not enough memory' to create a state") + NoRun("not enough memory", "env MEMLIMIT=100 lua") +end +print('+') + +print('testing Ctrl C') +do + -- interrupt a script + local function kill (pid) + return os.execute(string.format('kill -INT %s 2> /dev/null', pid)) + end + + -- function to run a script in background, returning its output file + -- descriptor and its pid + local function runback (luaprg) + -- shell script to run 'luaprg' in background and echo its pid + local shellprg = string.format('%s -e "%s" & echo $!', progname, luaprg) + local f = io.popen(shellprg, "r") -- run shell script + local pid = f:read() -- get pid for Lua script + print("(if test fails now, it may leave a Lua script running in \z + background, pid " .. pid .. ")") + return f, pid + end + + -- Lua script that runs protected infinite loop and then prints '42' + local f, pid = runback[[ + pcall(function () print(12); while true do end end); print(42)]] + -- wait until script is inside 'pcall' + assert(f:read() == "12") + kill(pid) -- send INT signal to Lua script + -- check that 'pcall' captured the exception and script continued running + assert(f:read() == "42") -- expected output + assert(f:close()) + print("done") + + -- Lua script in a long unbreakable search + local f, pid = runback[[ + print(15); string.find(string.rep('a', 100000), '.*b')]] + -- wait (so script can reach the loop) + assert(f:read() == "15") + assert(os.execute("sleep 1")) + -- must send at least two INT signals to stop this Lua script + local n = 100 + for i = 0, 100 do -- keep sending signals + if not kill(pid) then -- until it fails + n = i -- number of non-failed kills + break + end + end + assert(f:close()) + assert(n >= 2) + print(string.format("done (with %d kills)", n)) + +end + +print("OK") diff --git a/testes/math.lua b/testes/math.lua new file mode 100644 index 0000000000..66998460d0 --- /dev/null +++ b/testes/math.lua @@ -0,0 +1,931 @@ +-- $Id: math.lua,v 1.86 2018/05/09 14:55:52 roberto Exp $ +-- See Copyright Notice in file all.lua + +print("testing numbers and math lib") + +local minint = math.mininteger +local maxint = math.maxinteger + +local intbits = math.floor(math.log(maxint, 2) + 0.5) + 1 +assert((1 << intbits) == 0) + +assert(minint == 1 << (intbits - 1)) +assert(maxint == minint - 1) + +-- number of bits in the mantissa of a floating-point number +local floatbits = 24 +do + local p = 2.0^floatbits + while p < p + 1.0 do + p = p * 2.0 + floatbits = floatbits + 1 + end +end + +local function isNaN (x) + return (x ~= x) +end + +assert(isNaN(0/0)) +assert(not isNaN(1/0)) + + +do + local x = 2.0^floatbits + assert(x > x - 1.0 and x == x + 1.0) + + print(string.format("%d-bit integers, %d-bit (mantissa) floats", + intbits, floatbits)) +end + +assert(math.type(0) == "integer" and math.type(0.0) == "float" + and math.type("10") == nil) + + +local function checkerror (msg, f, ...) + local s, err = pcall(f, ...) + assert(not s and string.find(err, msg)) +end + +local msgf2i = "number.* has no integer representation" + +-- float equality +function eq (a,b,limit) + if not limit then + if floatbits >= 50 then limit = 1E-11 + else limit = 1E-5 + end + end + -- a == b needed for +inf/-inf + return a == b or math.abs(a-b) <= limit +end + + +-- equality with types +function eqT (a,b) + return a == b and math.type(a) == math.type(b) +end + + +-- basic float notation +assert(0e12 == 0 and .0 == 0 and 0. == 0 and .2e2 == 20 and 2.E-1 == 0.2) + +do + local a,b,c = "2", " 3e0 ", " 10 " + assert(a+b == 5 and -b == -3 and b+"2" == 5 and "10"-c == 0) + assert(type(a) == 'string' and type(b) == 'string' and type(c) == 'string') + assert(a == "2" and b == " 3e0 " and c == " 10 " and -c == -" 10 ") + assert(c%a == 0 and a^b == 08) + a = 0 + assert(a == -a and 0 == -0) +end + +do + local x = -1 + local mz = 0/x -- minus zero + t = {[0] = 10, 20, 30, 40, 50} + assert(t[mz] == t[0] and t[-0] == t[0]) +end + +do -- tests for 'modf' + local a,b = math.modf(3.5) + assert(a == 3.0 and b == 0.5) + a,b = math.modf(-2.5) + assert(a == -2.0 and b == -0.5) + a,b = math.modf(-3e23) + assert(a == -3e23 and b == 0.0) + a,b = math.modf(3e35) + assert(a == 3e35 and b == 0.0) + a,b = math.modf(-1/0) -- -inf + assert(a == -1/0 and b == 0.0) + a,b = math.modf(1/0) -- inf + assert(a == 1/0 and b == 0.0) + a,b = math.modf(0/0) -- NaN + assert(isNaN(a) and isNaN(b)) + a,b = math.modf(3) -- integer argument + assert(eqT(a, 3) and eqT(b, 0.0)) + a,b = math.modf(minint) + assert(eqT(a, minint) and eqT(b, 0.0)) +end + +assert(math.huge > 10e30) +assert(-math.huge < -10e30) + + +-- integer arithmetic +assert(minint < minint + 1) +assert(maxint - 1 < maxint) +assert(0 - minint == minint) +assert(minint * minint == 0) +assert(maxint * maxint * maxint == maxint) + + +-- testing floor division and conversions + +for _, i in pairs{-16, -15, -3, -2, -1, 0, 1, 2, 3, 15} do + for _, j in pairs{-16, -15, -3, -2, -1, 1, 2, 3, 15} do + for _, ti in pairs{0, 0.0} do -- try 'i' as integer and as float + for _, tj in pairs{0, 0.0} do -- try 'j' as integer and as float + local x = i + ti + local y = j + tj + assert(i//j == math.floor(i/j)) + end + end + end +end + +assert(1//0.0 == 1/0) +assert(-1 // 0.0 == -1/0) +assert(eqT(3.5 // 1.5, 2.0)) +assert(eqT(3.5 // -1.5, -3.0)) + +assert(maxint // maxint == 1) +assert(maxint // 1 == maxint) +assert((maxint - 1) // maxint == 0) +assert(maxint // (maxint - 1) == 1) +assert(minint // minint == 1) +assert(minint // minint == 1) +assert((minint + 1) // minint == 0) +assert(minint // (minint + 1) == 1) +assert(minint // 1 == minint) + +assert(minint // -1 == -minint) +assert(minint // -2 == 2^(intbits - 2)) +assert(maxint // -1 == -maxint) + + +-- negative exponents +do + assert(2^-3 == 1 / 2^3) + assert(eq((-3)^-3, 1 / (-3)^3)) + for i = -3, 3 do -- variables avoid constant folding + for j = -3, 3 do + -- domain errors (0^(-n)) are not portable + if not _port or i ~= 0 or j > 0 then + assert(eq(i^j, 1 / i^(-j))) + end + end + end +end + +-- comparison between floats and integers (border cases) +if floatbits < intbits then + assert(2.0^floatbits == (1 << floatbits)) + assert(2.0^floatbits - 1.0 == (1 << floatbits) - 1.0) + assert(2.0^floatbits - 1.0 ~= (1 << floatbits)) + -- float is rounded, int is not + assert(2.0^floatbits + 1.0 ~= (1 << floatbits) + 1) +else -- floats can express all integers with full accuracy + assert(maxint == maxint + 0.0) + assert(maxint - 1 == maxint - 1.0) + assert(minint + 1 == minint + 1.0) + assert(maxint ~= maxint - 1.0) +end +assert(maxint + 0.0 == 2.0^(intbits - 1) - 1.0) +assert(minint + 0.0 == minint) +assert(minint + 0.0 == -2.0^(intbits - 1)) + + +-- order between floats and integers +assert(1 < 1.1); assert(not (1 < 0.9)) +assert(1 <= 1.1); assert(not (1 <= 0.9)) +assert(-1 < -0.9); assert(not (-1 < -1.1)) +assert(1 <= 1.1); assert(not (-1 <= -1.1)) +assert(-1 < -0.9); assert(not (-1 < -1.1)) +assert(-1 <= -0.9); assert(not (-1 <= -1.1)) +assert(minint <= minint + 0.0) +assert(minint + 0.0 <= minint) +assert(not (minint < minint + 0.0)) +assert(not (minint + 0.0 < minint)) +assert(maxint < minint * -1.0) +assert(maxint <= minint * -1.0) + +do + local fmaxi1 = 2^(intbits - 1) + assert(maxint < fmaxi1) + assert(maxint <= fmaxi1) + assert(not (fmaxi1 <= maxint)) + assert(minint <= -2^(intbits - 1)) + assert(-2^(intbits - 1) <= minint) +end + +if floatbits < intbits then + print("testing order (floats cannot represent all integers)") + local fmax = 2^floatbits + local ifmax = fmax | 0 + assert(fmax < ifmax + 1) + assert(fmax - 1 < ifmax) + assert(-(fmax - 1) > -ifmax) + assert(not (fmax <= ifmax - 1)) + assert(-fmax > -(ifmax + 1)) + assert(not (-fmax >= -(ifmax - 1))) + + assert(fmax/2 - 0.5 < ifmax//2) + assert(-(fmax/2 - 0.5) > -ifmax//2) + + assert(maxint < 2^intbits) + assert(minint > -2^intbits) + assert(maxint <= 2^intbits) + assert(minint >= -2^intbits) +else + print("testing order (floats can represent all integers)") + assert(maxint < maxint + 1.0) + assert(maxint < maxint + 0.5) + assert(maxint - 1.0 < maxint) + assert(maxint - 0.5 < maxint) + assert(not (maxint + 0.0 < maxint)) + assert(maxint + 0.0 <= maxint) + assert(not (maxint < maxint + 0.0)) + assert(maxint + 0.0 <= maxint) + assert(maxint <= maxint + 0.0) + assert(not (maxint + 1.0 <= maxint)) + assert(not (maxint + 0.5 <= maxint)) + assert(not (maxint <= maxint - 1.0)) + assert(not (maxint <= maxint - 0.5)) + + assert(minint < minint + 1.0) + assert(minint < minint + 0.5) + assert(minint <= minint + 0.5) + assert(minint - 1.0 < minint) + assert(minint - 1.0 <= minint) + assert(not (minint + 0.0 < minint)) + assert(not (minint + 0.5 < minint)) + assert(not (minint < minint + 0.0)) + assert(minint + 0.0 <= minint) + assert(minint <= minint + 0.0) + assert(not (minint + 1.0 <= minint)) + assert(not (minint + 0.5 <= minint)) + assert(not (minint <= minint - 1.0)) +end + +do + local NaN = 0/0 + assert(not (NaN < 0)) + assert(not (NaN > minint)) + assert(not (NaN <= -9)) + assert(not (NaN <= maxint)) + assert(not (NaN < maxint)) + assert(not (minint <= NaN)) + assert(not (minint < NaN)) + assert(not (4 <= NaN)) + assert(not (4 < NaN)) +end + + +-- avoiding errors at compile time +local function checkcompt (msg, code) + checkerror(msg, assert(load(code))) +end +checkcompt("divide by zero", "return 2 // 0") +checkcompt(msgf2i, "return 2.3 >> 0") +checkcompt(msgf2i, ("return 2.0^%d & 1"):format(intbits - 1)) +checkcompt("field 'huge'", "return math.huge << 1") +checkcompt(msgf2i, ("return 1 | 2.0^%d"):format(intbits - 1)) +checkcompt(msgf2i, "return 2.3 ~ 0.0") + + +-- testing overflow errors when converting from float to integer (runtime) +local function f2i (x) return x | x end +checkerror(msgf2i, f2i, math.huge) -- +inf +checkerror(msgf2i, f2i, -math.huge) -- -inf +checkerror(msgf2i, f2i, 0/0) -- NaN + +if floatbits < intbits then + -- conversion tests when float cannot represent all integers + assert(maxint + 1.0 == maxint + 0.0) + assert(minint - 1.0 == minint + 0.0) + checkerror(msgf2i, f2i, maxint + 0.0) + assert(f2i(2.0^(intbits - 2)) == 1 << (intbits - 2)) + assert(f2i(-2.0^(intbits - 2)) == -(1 << (intbits - 2))) + assert((2.0^(floatbits - 1) + 1.0) // 1 == (1 << (floatbits - 1)) + 1) + -- maximum integer representable as a float + local mf = maxint - (1 << (floatbits - intbits)) + 1 + assert(f2i(mf + 0.0) == mf) -- OK up to here + mf = mf + 1 + assert(f2i(mf + 0.0) ~= mf) -- no more representable +else + -- conversion tests when float can represent all integers + assert(maxint + 1.0 > maxint) + assert(minint - 1.0 < minint) + assert(f2i(maxint + 0.0) == maxint) + checkerror("no integer rep", f2i, maxint + 1.0) + checkerror("no integer rep", f2i, minint - 1.0) +end + +-- 'minint' should be representable as a float no matter the precision +assert(f2i(minint + 0.0) == minint) + + +-- testing numeric strings + +assert("2" + 1 == 3) +assert("2 " + 1 == 3) +assert(" -2 " + 1 == -1) +assert(" -0xa " + 1 == -9) + + +-- Literal integer Overflows (new behavior in 5.3.3) +do + -- no overflows + assert(eqT(tonumber(tostring(maxint)), maxint)) + assert(eqT(tonumber(tostring(minint)), minint)) + + -- add 1 to last digit as a string (it cannot be 9...) + local function incd (n) + local s = string.format("%d", n) + s = string.gsub(s, "%d$", function (d) + assert(d ~= '9') + return string.char(string.byte(d) + 1) + end) + return s + end + + -- 'tonumber' with overflow by 1 + assert(eqT(tonumber(incd(maxint)), maxint + 1.0)) + assert(eqT(tonumber(incd(minint)), minint - 1.0)) + + -- large numbers + assert(eqT(tonumber("1"..string.rep("0", 30)), 1e30)) + assert(eqT(tonumber("-1"..string.rep("0", 30)), -1e30)) + + -- hexa format still wraps around + assert(eqT(tonumber("0x1"..string.rep("0", 30)), 0)) + + -- lexer in the limits + assert(minint == load("return " .. minint)()) + assert(eqT(maxint, load("return " .. maxint)())) + + assert(eqT(10000000000000000000000.0, 10000000000000000000000)) + assert(eqT(-10000000000000000000000.0, -10000000000000000000000)) +end + + +-- testing 'tonumber' + +-- 'tonumber' with numbers +assert(tonumber(3.4) == 3.4) +assert(eqT(tonumber(3), 3)) +assert(eqT(tonumber(maxint), maxint) and eqT(tonumber(minint), minint)) +assert(tonumber(1/0) == 1/0) + +-- 'tonumber' with strings +assert(tonumber("0") == 0) +assert(tonumber("") == nil) +assert(tonumber(" ") == nil) +assert(tonumber("-") == nil) +assert(tonumber(" -0x ") == nil) +assert(tonumber{} == nil) +assert(tonumber'+0.01' == 1/100 and tonumber'+.01' == 0.01 and + tonumber'.01' == 0.01 and tonumber'-1.' == -1 and + tonumber'+1.' == 1) +assert(tonumber'+ 0.01' == nil and tonumber'+.e1' == nil and + tonumber'1e' == nil and tonumber'1.0e+' == nil and + tonumber'.' == nil) +assert(tonumber('-012') == -010-2) +assert(tonumber('-1.2e2') == - - -120) + +assert(tonumber("0xffffffffffff") == (1 << (4*12)) - 1) +assert(tonumber("0x"..string.rep("f", (intbits//4))) == -1) +assert(tonumber("-0x"..string.rep("f", (intbits//4))) == 1) + +-- testing 'tonumber' with base +assert(tonumber(' 001010 ', 2) == 10) +assert(tonumber(' 001010 ', 10) == 001010) +assert(tonumber(' -1010 ', 2) == -10) +assert(tonumber('10', 36) == 36) +assert(tonumber(' -10 ', 36) == -36) +assert(tonumber(' +1Z ', 36) == 36 + 35) +assert(tonumber(' -1z ', 36) == -36 + -35) +assert(tonumber('-fFfa', 16) == -(10+(16*(15+(16*(15+(16*15))))))) +assert(tonumber(string.rep('1', (intbits - 2)), 2) + 1 == 2^(intbits - 2)) +assert(tonumber('ffffFFFF', 16)+1 == (1 << 32)) +assert(tonumber('0ffffFFFF', 16)+1 == (1 << 32)) +assert(tonumber('-0ffffffFFFF', 16) - 1 == -(1 << 40)) +for i = 2,36 do + local i2 = i * i + local i10 = i2 * i2 * i2 * i2 * i2 -- i^10 + assert(tonumber('\t10000000000\t', i) == i10) +end + +if not _soft then + -- tests with very long numerals + assert(tonumber("0x"..string.rep("f", 13)..".0") == 2.0^(4*13) - 1) + assert(tonumber("0x"..string.rep("f", 150)..".0") == 2.0^(4*150) - 1) + assert(tonumber("0x"..string.rep("f", 300)..".0") == 2.0^(4*300) - 1) + assert(tonumber("0x"..string.rep("f", 500)..".0") == 2.0^(4*500) - 1) + assert(tonumber('0x3.' .. string.rep('0', 1000)) == 3) + assert(tonumber('0x' .. string.rep('0', 1000) .. 'a') == 10) + assert(tonumber('0x0.' .. string.rep('0', 13).."1") == 2.0^(-4*14)) + assert(tonumber('0x0.' .. string.rep('0', 150).."1") == 2.0^(-4*151)) + assert(tonumber('0x0.' .. string.rep('0', 300).."1") == 2.0^(-4*301)) + assert(tonumber('0x0.' .. string.rep('0', 500).."1") == 2.0^(-4*501)) + + assert(tonumber('0xe03' .. string.rep('0', 1000) .. 'p-4000') == 3587.0) + assert(tonumber('0x.' .. string.rep('0', 1000) .. '74p4004') == 0x7.4) +end + +-- testing 'tonumber' for invalid formats + +local function f (...) + if select('#', ...) == 1 then + return (...) + else + return "***" + end +end + +assert(f(tonumber('fFfa', 15)) == nil) +assert(f(tonumber('099', 8)) == nil) +assert(f(tonumber('1\0', 2)) == nil) +assert(f(tonumber('', 8)) == nil) +assert(f(tonumber(' ', 9)) == nil) +assert(f(tonumber(' ', 9)) == nil) +assert(f(tonumber('0xf', 10)) == nil) + +assert(f(tonumber('inf')) == nil) +assert(f(tonumber(' INF ')) == nil) +assert(f(tonumber('Nan')) == nil) +assert(f(tonumber('nan')) == nil) + +assert(f(tonumber(' ')) == nil) +assert(f(tonumber('')) == nil) +assert(f(tonumber('1 a')) == nil) +assert(f(tonumber('1 a', 2)) == nil) +assert(f(tonumber('1\0')) == nil) +assert(f(tonumber('1 \0')) == nil) +assert(f(tonumber('1\0 ')) == nil) +assert(f(tonumber('e1')) == nil) +assert(f(tonumber('e 1')) == nil) +assert(f(tonumber(' 3.4.5 ')) == nil) + + +-- testing 'tonumber' for invalid hexadecimal formats + +assert(tonumber('0x') == nil) +assert(tonumber('x') == nil) +assert(tonumber('x3') == nil) +assert(tonumber('0x3.3.3') == nil) -- two decimal points +assert(tonumber('00x2') == nil) +assert(tonumber('0x 2') == nil) +assert(tonumber('0 x2') == nil) +assert(tonumber('23x') == nil) +assert(tonumber('- 0xaa') == nil) +assert(tonumber('-0xaaP ') == nil) -- no exponent +assert(tonumber('0x0.51p') == nil) +assert(tonumber('0x5p+-2') == nil) + + +-- testing hexadecimal numerals + +assert(0x10 == 16 and 0xfff == 2^12 - 1 and 0XFB == 251) +assert(0x0p12 == 0 and 0x.0p-3 == 0) +assert(0xFFFFFFFF == (1 << 32) - 1) +assert(tonumber('+0x2') == 2) +assert(tonumber('-0xaA') == -170) +assert(tonumber('-0xffFFFfff') == -(1 << 32) + 1) + +-- possible confusion with decimal exponent +assert(0E+1 == 0 and 0xE+1 == 15 and 0xe-1 == 13) + + +-- floating hexas + +assert(tonumber(' 0x2.5 ') == 0x25/16) +assert(tonumber(' -0x2.5 ') == -0x25/16) +assert(tonumber(' +0x0.51p+8 ') == 0x51) +assert(0x.FfffFFFF == 1 - '0x.00000001') +assert('0xA.a' + 0 == 10 + 10/16) +assert(0xa.aP4 == 0XAA) +assert(0x4P-2 == 1) +assert(0x1.1 == '0x1.' + '+0x.1') +assert(0Xabcdef.0 == 0x.ABCDEFp+24) + + +assert(1.1 == 1.+.1) +assert(100.0 == 1E2 and .01 == 1e-2) +assert(1111111111 - 1111111110 == 1000.00e-03) +assert(1.1 == '1.'+'.1') +assert(tonumber'1111111111' - tonumber'1111111110' == + tonumber" +0.001e+3 \n\t") + +assert(0.1e-30 > 0.9E-31 and 0.9E30 < 0.1e31) + +assert(0.123456 > 0.123455) + +assert(tonumber('+1.23E18') == 1.23*10.0^18) + +-- testing order operators +assert(not(1<1) and (1<2) and not(2<1)) +assert(not('a'<'a') and ('a'<'b') and not('b'<'a')) +assert((1<=1) and (1<=2) and not(2<=1)) +assert(('a'<='a') and ('a'<='b') and not('b'<='a')) +assert(not(1>1) and not(1>2) and (2>1)) +assert(not('a'>'a') and not('a'>'b') and ('b'>'a')) +assert((1>=1) and not(1>=2) and (2>=1)) +assert(('a'>='a') and not('a'>='b') and ('b'>='a')) +assert(1.3 < 1.4 and 1.3 <= 1.4 and not (1.3 < 1.3) and 1.3 <= 1.3) + +-- testing mod operator +assert(eqT(-4 % 3, 2)) +assert(eqT(4 % -3, -2)) +assert(eqT(-4.0 % 3, 2.0)) +assert(eqT(4 % -3.0, -2.0)) +assert(math.pi - math.pi % 1 == 3) +assert(math.pi - math.pi % 0.001 == 3.141) + +assert(eqT(minint % minint, 0)) +assert(eqT(maxint % maxint, 0)) +assert((minint + 1) % minint == minint + 1) +assert((maxint - 1) % maxint == maxint - 1) +assert(minint % maxint == maxint - 1) + +assert(minint % -1 == 0) +assert(minint % -2 == 0) +assert(maxint % -2 == -1) + +-- non-portable tests because Windows C library cannot compute +-- fmod(1, huge) correctly +if not _port then + local function anan (x) assert(isNaN(x)) end -- assert Not a Number + anan(0.0 % 0) + anan(1.3 % 0) + anan(math.huge % 1) + anan(math.huge % 1e30) + anan(-math.huge % 1e30) + anan(-math.huge % -1e30) + assert(1 % math.huge == 1) + assert(1e30 % math.huge == 1e30) + assert(1e30 % -math.huge == -math.huge) + assert(-1 % math.huge == math.huge) + assert(-1 % -math.huge == -1) +end + + +-- testing unsigned comparisons +assert(math.ult(3, 4)) +assert(not math.ult(4, 4)) +assert(math.ult(-2, -1)) +assert(math.ult(2, -1)) +assert(not math.ult(-2, -2)) +assert(math.ult(maxint, minint)) +assert(not math.ult(minint, maxint)) + + +assert(eq(math.sin(-9.8)^2 + math.cos(-9.8)^2, 1)) +assert(eq(math.tan(math.pi/4), 1)) +assert(eq(math.sin(math.pi/2), 1) and eq(math.cos(math.pi/2), 0)) +assert(eq(math.atan(1), math.pi/4) and eq(math.acos(0), math.pi/2) and + eq(math.asin(1), math.pi/2)) +assert(eq(math.deg(math.pi/2), 90) and eq(math.rad(90), math.pi/2)) +assert(math.abs(-10.43) == 10.43) +assert(eqT(math.abs(minint), minint)) +assert(eqT(math.abs(maxint), maxint)) +assert(eqT(math.abs(-maxint), maxint)) +assert(eq(math.atan(1,0), math.pi/2)) +assert(math.fmod(10,3) == 1) +assert(eq(math.sqrt(10)^2, 10)) +assert(eq(math.log(2, 10), math.log(2)/math.log(10))) +assert(eq(math.log(2, 2), 1)) +assert(eq(math.log(9, 3), 2)) +assert(eq(math.exp(0), 1)) +assert(eq(math.sin(10), math.sin(10%(2*math.pi)))) + + +assert(tonumber(' 1.3e-2 ') == 1.3e-2) +assert(tonumber(' -1.00000000000001 ') == -1.00000000000001) + +-- testing constant limits +-- 2^23 = 8388608 +assert(8388609 + -8388609 == 0) +assert(8388608 + -8388608 == 0) +assert(8388607 + -8388607 == 0) + + + +do -- testing floor & ceil + assert(eqT(math.floor(3.4), 3)) + assert(eqT(math.ceil(3.4), 4)) + assert(eqT(math.floor(-3.4), -4)) + assert(eqT(math.ceil(-3.4), -3)) + assert(eqT(math.floor(maxint), maxint)) + assert(eqT(math.ceil(maxint), maxint)) + assert(eqT(math.floor(minint), minint)) + assert(eqT(math.floor(minint + 0.0), minint)) + assert(eqT(math.ceil(minint), minint)) + assert(eqT(math.ceil(minint + 0.0), minint)) + assert(math.floor(1e50) == 1e50) + assert(math.ceil(1e50) == 1e50) + assert(math.floor(-1e50) == -1e50) + assert(math.ceil(-1e50) == -1e50) + for _, p in pairs{31,32,63,64} do + assert(math.floor(2^p) == 2^p) + assert(math.floor(2^p + 0.5) == 2^p) + assert(math.ceil(2^p) == 2^p) + assert(math.ceil(2^p - 0.5) == 2^p) + end + checkerror("number expected", math.floor, {}) + checkerror("number expected", math.ceil, print) + assert(eqT(math.tointeger(minint), minint)) + assert(eqT(math.tointeger(minint .. ""), minint)) + assert(eqT(math.tointeger(maxint), maxint)) + assert(eqT(math.tointeger(maxint .. ""), maxint)) + assert(eqT(math.tointeger(minint + 0.0), minint)) + assert(math.tointeger(0.0 - minint) == nil) + assert(math.tointeger(math.pi) == nil) + assert(math.tointeger(-math.pi) == nil) + assert(math.floor(math.huge) == math.huge) + assert(math.ceil(math.huge) == math.huge) + assert(math.tointeger(math.huge) == nil) + assert(math.floor(-math.huge) == -math.huge) + assert(math.ceil(-math.huge) == -math.huge) + assert(math.tointeger(-math.huge) == nil) + assert(math.tointeger("34.0") == 34) + assert(math.tointeger("34.3") == nil) + assert(math.tointeger({}) == nil) + assert(math.tointeger(0/0) == nil) -- NaN +end + + +-- testing fmod for integers +for i = -6, 6 do + for j = -6, 6 do + if j ~= 0 then + local mi = math.fmod(i, j) + local mf = math.fmod(i + 0.0, j) + assert(mi == mf) + assert(math.type(mi) == 'integer' and math.type(mf) == 'float') + if (i >= 0 and j >= 0) or (i <= 0 and j <= 0) or mi == 0 then + assert(eqT(mi, i % j)) + end + end + end +end +assert(eqT(math.fmod(minint, minint), 0)) +assert(eqT(math.fmod(maxint, maxint), 0)) +assert(eqT(math.fmod(minint + 1, minint), minint + 1)) +assert(eqT(math.fmod(maxint - 1, maxint), maxint - 1)) + +checkerror("zero", math.fmod, 3, 0) + + +do -- testing max/min + checkerror("value expected", math.max) + checkerror("value expected", math.min) + assert(eqT(math.max(3), 3)) + assert(eqT(math.max(3, 5, 9, 1), 9)) + assert(math.max(maxint, 10e60) == 10e60) + assert(eqT(math.max(minint, minint + 1), minint + 1)) + assert(eqT(math.min(3), 3)) + assert(eqT(math.min(3, 5, 9, 1), 1)) + assert(math.min(3.2, 5.9, -9.2, 1.1) == -9.2) + assert(math.min(1.9, 1.7, 1.72) == 1.7) + assert(math.min(-10e60, minint) == -10e60) + assert(eqT(math.min(maxint, maxint - 1), maxint - 1)) + assert(eqT(math.min(maxint - 2, maxint, maxint - 1), maxint - 2)) +end +-- testing implicit convertions + +local a,b = '10', '20' +assert(a*b == 200 and a+b == 30 and a-b == -10 and a/b == 0.5 and -b == -20) +assert(a == '10' and b == '20') + + +do + print("testing -0 and NaN") + local mz, z = -0.0, 0.0 + assert(mz == z) + assert(1/mz < 0 and 0 < 1/z) + local a = {[mz] = 1} + assert(a[z] == 1 and a[mz] == 1) + a[z] = 2 + assert(a[z] == 2 and a[mz] == 2) + local inf = math.huge * 2 + 1 + mz, z = -1/inf, 1/inf + assert(mz == z) + assert(1/mz < 0 and 0 < 1/z) + local NaN = inf - inf + assert(NaN ~= NaN) + assert(not (NaN < NaN)) + assert(not (NaN <= NaN)) + assert(not (NaN > NaN)) + assert(not (NaN >= NaN)) + assert(not (0 < NaN) and not (NaN < 0)) + local NaN1 = 0/0 + assert(NaN ~= NaN1 and not (NaN <= NaN1) and not (NaN1 <= NaN)) + local a = {} + assert(not pcall(rawset, a, NaN, 1)) + assert(a[NaN] == undef) + a[1] = 1 + assert(not pcall(rawset, a, NaN, 1)) + assert(a[NaN] == undef) + -- strings with same binary representation as 0.0 (might create problems + -- for constant manipulation in the pre-compiler) + local a1, a2, a3, a4, a5 = 0, 0, "\0\0\0\0\0\0\0\0", 0, "\0\0\0\0\0\0\0\0" + assert(a1 == a2 and a2 == a4 and a1 ~= a3) + assert(a3 == a5) +end + + +print("testing 'math.random'") + +local random, max, min = math.random, math.max, math.min + +local function testnear (val, ref, tol) + return (math.abs(val - ref) < ref * tol) +end + + +-- low-level!! For the current implementation of random in Lua, +-- the first call after seed 1007 should return 0x7a7040a5a323c9d6 +do + -- all computations assume at most 32-bit integers + local h = 0x7a7040a5 -- higher half + local l = 0xa323c9d6 -- lower half + + math.randomseed(1007) + -- get the low 'intbits' of the 64-bit expected result + local res = (h << 32 | l) & ~(~0 << intbits) + assert(random(0) == res) + + math.randomseed(1007, 0) + -- using lower bits to generate random floats; (the '% 2^32' converts + -- 32-bit integers to floats as unsigned) + local res + if floatbits <= 32 then + -- get all bits from the lower half + res = (l & ~(~0 << floatbits)) % 2^32 + else + -- get 32 bits from the lower half and the rest from the higher half + res = ((h & ~(~0 << (floatbits - 32))) % 2^32) * 2^32 + (l % 2^32) + end + assert(random() * 2^floatbits == res) +end + +math.randomseed(0, os.time()) + +do -- test random for floats + local randbits = math.min(floatbits, 64) -- at most 64 random bits + local mult = 2^randbits -- to make random float into an integral + local counts = {} -- counts for bits + for i = 1, randbits do counts[i] = 0 end + local up = -math.huge + local low = math.huge + local rounds = 100 * randbits -- 100 times for each bit + local totalrounds = 0 + ::doagain:: -- will repeat test until we get good statistics + for i = 0, rounds do + local t = random() + assert(0 <= t and t < 1) + up = max(up, t) + low = min(low, t) + assert(t * mult % 1 == 0) -- no extra bits + local bit = i % randbits -- bit to be tested + if (t * 2^bit) % 1 >= 0.5 then -- is bit set? + counts[bit + 1] = counts[bit + 1] + 1 -- increment its count + end + end + totalrounds = totalrounds + rounds + if not (eq(up, 1, 0.001) and eq(low, 0, 0.001)) then + goto doagain + end + -- all bit counts should be near 50% + local expected = (totalrounds / randbits / 2) + for i = 1, randbits do + if not testnear(counts[i], expected, 0.10) then + goto doagain + end + end + print(string.format("float random range in %d calls: [%f, %f]", + totalrounds, low, up)) +end + + +do -- test random for full integers + local up = 0 + local low = 0 + local counts = {} -- counts for bits + for i = 1, intbits do counts[i] = 0 end + local rounds = 100 * intbits -- 100 times for each bit + local totalrounds = 0 + ::doagain:: -- will repeat test until we get good statistics + for i = 0, rounds do + local t = random(0) + up = max(up, t) + low = min(low, t) + local bit = i % intbits -- bit to be tested + -- increment its count if it is set + counts[bit + 1] = counts[bit + 1] + ((t >> bit) & 1) + end + totalrounds = totalrounds + rounds + local lim = maxint >> 10 + if not (maxint - up < lim and low - minint < lim) then + goto doagain + end + -- all bit counts should be near 50% + local expected = (totalrounds / intbits / 2) + for i = 1, intbits do + if not testnear(counts[i], expected, 0.10) then + goto doagain + end + end + print(string.format( + "integer random range in %d calls: [minint + %.0fppm, maxint - %.0fppm]", + totalrounds, (minint - low) / minint * 1e6, + (maxint - up) / maxint * 1e6)) +end + +do + -- test distribution for a dice + local count = {0, 0, 0, 0, 0, 0} + local rep = 200 + local totalrep = 0 + ::doagain:: + for i = 1, rep * 6 do + local r = random(6) + count[r] = count[r] + 1 + end + totalrep = totalrep + rep + for i = 1, 6 do + if not testnear(count[i], totalrep, 0.05) then + goto doagain + end + end +end + +do + local function aux (x1, x2) -- test random for small intervals + local mark = {}; local count = 0 -- to check that all values appeared + while true do + local t = random(x1, x2) + assert(x1 <= t and t <= x2) + if not mark[t] then -- new value + mark[t] = true + count = count + 1 + if count == x2 - x1 + 1 then -- all values appeared; OK + goto ok + end + end + end + ::ok:: + end + + aux(-10,0) + aux(1, 6) + aux(1, 2) + aux(1, 32) + aux(-10, 10) + aux(-10,-10) -- unit set + aux(minint, minint) -- unit set + aux(maxint, maxint) -- unit set + aux(minint, minint + 9) + aux(maxint - 3, maxint) +end + +do + local function aux(p1, p2) -- test random for large intervals + local max = minint + local min = maxint + local n = 100 + local mark = {}; local count = 0 -- to count how many different values + ::doagain:: + for _ = 1, n do + local t = random(p1, p2) + if not mark[t] then -- new value + assert(p1 <= t and t <= p2) + max = math.max(max, t) + min = math.min(min, t) + mark[t] = true + count = count + 1 + end + end + -- at least 80% of values are different + if not (count >= n * 0.8) then + goto doagain + end + -- min and max not too far from formal min and max + local diff = (p2 - p1) >> 4 + if not (min < p1 + diff and max > p2 - diff) then + goto doagain + end + end + aux(0, maxint) + aux(1, maxint) + aux(minint, -1) + aux(minint // 2, maxint // 2) + aux(minint, maxint) + aux(minint + 1, maxint) + aux(minint, maxint - 1) + aux(0, 1 << (intbits - 5)) +end + + +assert(not pcall(random, 1, 2, 3)) -- too many arguments + +-- empty interval +assert(not pcall(random, minint + 1, minint)) +assert(not pcall(random, maxint, maxint - 1)) +assert(not pcall(random, maxint, minint)) + + + +print('OK') diff --git a/testes/nextvar.lua b/testes/nextvar.lua new file mode 100644 index 0000000000..3ac3acd94e --- /dev/null +++ b/testes/nextvar.lua @@ -0,0 +1,669 @@ +-- $Id: nextvar.lua,v 1.85 2018/06/19 12:24:19 roberto Exp $ +-- See Copyright Notice in file all.lua + +print('testing tables, next, and for') + +local function checkerror (msg, f, ...) + local s, err = pcall(f, ...) + assert(not s and string.find(err, msg)) +end + + +local a = {} + +-- make sure table has lots of space in hash part +for i=1,100 do a[i.."+"] = true end +for i=1,100 do a[i.."+"] = undef end +-- fill hash part with numeric indices testing size operator +for i=1,100 do + a[i] = true + assert(#a == i) +end + +-- testing ipairs +local x = 0 +for k,v in ipairs{10,20,30;x=12} do + x = x + 1 + assert(k == x and v == x * 10) +end + +for _ in ipairs{x=12, y=24} do assert(nil) end + +-- test for 'false' x ipair +x = false +local i = 0 +for k,v in ipairs{true,false,true,false} do + i = i + 1 + x = not x + assert(x == v) +end +assert(i == 4) + +-- iterator function is always the same +assert(type(ipairs{}) == 'function' and ipairs{} == ipairs{}) + + +if not T then + (Message or print) + ('\n >>> testC not active: skipping tests for table sizes <<<\n') +else --[ +-- testing table sizes + +local function log2 (x) return math.log(x, 2) end + +local function mp2 (n) -- minimum power of 2 >= n + local mp = 2^math.ceil(log2(n)) + assert(n == 0 or (mp/2 < n and n <= mp)) + return mp +end + +local function fb (n) + local r, nn = T.int2fb(n) + assert(r < 256) + return nn +end + +-- test fb function +for a = 1, 10000 do -- all numbers up to 10^4 + local n = fb(a) + assert(a <= n and n <= a*1.125) +end +local a = 1024 -- plus a few up to 2 ^30 +local lim = 2^30 +while a < lim do + local n = fb(a) + assert(a <= n and n <= a*1.125) + a = math.ceil(a*1.3) +end + + +local function check (t, na, nh) + local a, h = T.querytab(t) + if a ~= na or h ~= nh then + print(na, nh, a, h) + assert(nil) + end +end + + +-- testing C library sizes +do + local s = 0 + for _ in pairs(math) do s = s + 1 end + check(math, 0, mp2(s)) +end + + +-- testing constructor sizes +local lim = 40 +local s = 'return {' +for i=1,lim do + s = s..i..',' + local s = s + for k=0,lim do + local t = load(s..'}', '')() + assert(#t == i) + check(t, fb(i), mp2(k)) + s = string.format('%sa%d=%d,', s, k, k) + end +end + + +-- tests with unknown number of elements +local a = {} +for i=1,lim do a[i] = i end -- build auxiliary table +for k=0,lim do + local a = {table.unpack(a,1,k)} + assert(#a == k) + check(a, k, 0) + a = {1,2,3,table.unpack(a,1,k)} + check(a, k+3, 0) + assert(#a == k + 3) +end + + +-- testing tables dynamically built +local lim = 130 +local a = {}; a[2] = 1; check(a, 0, 1) +a = {}; a[0] = 1; check(a, 0, 1); a[2] = 1; check(a, 0, 2) +a = {}; a[0] = 1; a[1] = 1; check(a, 1, 1) +a = {} +for i = 1,lim do + a[i] = 1 + assert(#a == i) + check(a, mp2(i), 0) +end + +a = {} +for i = 1,lim do + a['a'..i] = 1 + assert(#a == 0) + check(a, 0, mp2(i)) +end + +a = {} +for i=1,16 do a[i] = i end +check(a, 16, 0) +do + for i=1,11 do a[i] = undef end + for i=30,50 do a[i] = true; a[i] = undef end -- force a rehash (?) + check(a, 0, 8) -- 5 elements in the table + a[10] = 1 + for i=30,50 do a[i] = true; a[i] = undef end -- force a rehash (?) + check(a, 0, 8) -- only 6 elements in the table + for i=1,14 do a[i] = true; a[i] = undef end + for i=18,50 do a[i] = true; a[i] = undef end -- force a rehash (?) + check(a, 0, 4) -- only 2 elements ([15] and [16]) +end + +-- reverse filling +for i=1,lim do + local a = {} + for i=i,1,-1 do a[i] = i end -- fill in reverse + check(a, mp2(i), 0) +end + +-- size tests for vararg +lim = 35 +function foo (n, ...) + local arg = {...} + check(arg, n, 0) + assert(select('#', ...) == n) + arg[n+1] = true + check(arg, mp2(n+1), 0) + arg.x = true + check(arg, mp2(n+1), 1) +end +local a = {} +for i=1,lim do a[i] = true; foo(i, table.unpack(a)) end + + +-- Table length with limit smaller than maximum value at array +local a = {} +for i = 1,64 do a[i] = true end -- make its array size 64 +for i = 1,64 do a[i] = nil end -- erase all elements +assert(T.querytab(a) == 64) -- array part has 64 elements +a[32] = true; a[48] = true; -- binary search will find these ones +a[51] = true -- binary search will miss this one +assert(#a == 48) -- this will set the limit +assert(select(4, T.querytab(a)) == 48) -- this is the limit now +a[50] = true -- this will set a new limit +assert(select(4, T.querytab(a)) == 50) -- this is the limit now +-- but the size is larger (and still inside the array part) +assert(#a == 51) + +end --] + + +-- test size operation on tables with nils +assert(#{} == 0) +assert(#{nil} == 0) +assert(#{nil, nil} == 0) +assert(#{nil, nil, nil} == 0) +assert(#{nil, nil, nil, nil} == 0) +assert(#{1, 2, 3, nil, nil} == 3) +print'+' + + +local nofind = {} + +a,b,c = 1,2,3 +a,b,c = nil + + +-- next uses always the same iteraction function +assert(next{} == next{}) + +local function find (name) + local n,v + while 1 do + n,v = next(_G, n) + if not n then return nofind end + assert(_G[n] ~= undef) + if n == name then return v end + end +end + +local function find1 (name) + for n,v in pairs(_G) do + if n==name then return v end + end + return nil -- not found +end + + +assert(print==find("print") and print == find1("print")) +assert(_G["print"]==find("print")) +assert(assert==find1("assert")) +assert(nofind==find("return")) +assert(not find1("return")) +_G["ret" .. "urn"] = undef +assert(nofind==find("return")) +_G["xxx"] = 1 +assert(xxx==find("xxx")) + +-- invalid key to 'next' +checkerror("invalid key", next, {10,20}, 3) + +-- both 'pairs' and 'ipairs' need an argument +checkerror("bad argument", pairs) +checkerror("bad argument", ipairs) + +print('+') + +a = {} +for i=0,10000 do + if math.fmod(i,10) ~= 0 then + a['x'..i] = i + end +end + +n = {n=0} +for i,v in pairs(a) do + n.n = n.n+1 + assert(i and v and a[i] == v) +end +assert(n.n == 9000) +a = nil + +do -- clear global table + local a = {} + for n,v in pairs(_G) do a[n]=v end + for n,v in pairs(a) do + if not package.loaded[n] and type(v) ~= "function" and + not string.find(n, "^[%u_]") then + _G[n] = undef + end + collectgarbage() + end +end + + +-- + +local function checknext (a) + local b = {} + do local k,v = next(a); while k do b[k] = v; k,v = next(a,k) end end + for k,v in pairs(b) do assert(a[k] == v) end + for k,v in pairs(a) do assert(b[k] == v) end +end + +checknext{1,x=1,y=2,z=3} +checknext{1,2,x=1,y=2,z=3} +checknext{1,2,3,x=1,y=2,z=3} +checknext{1,2,3,4,x=1,y=2,z=3} +checknext{1,2,3,4,5,x=1,y=2,z=3} + +assert(#{} == 0) +assert(#{[-1] = 2} == 0) +for i=0,40 do + local a = {} + for j=1,i do a[j]=j end + assert(#a == i) +end + +-- 'maxn' is now deprecated, but it is easily defined in Lua +function table.maxn (t) + local max = 0 + for k in pairs(t) do + max = (type(k) == 'number') and math.max(max, k) or max + end + return max +end + +assert(table.maxn{} == 0) +assert(table.maxn{["1000"] = true} == 0) +assert(table.maxn{["1000"] = true, [24.5] = 3} == 24.5) +assert(table.maxn{[1000] = true} == 1000) +assert(table.maxn{[10] = true, [100*math.pi] = print} == 100*math.pi) + +table.maxn = nil + +-- int overflow +a = {} +for i=0,50 do a[2^i] = true end +assert(a[#a]) + +print('+') + + +do -- testing 'next' with all kinds of keys + local a = { + [1] = 1, -- integer + [1.1] = 2, -- float + ['x'] = 3, -- short string + [string.rep('x', 1000)] = 4, -- long string + [print] = 5, -- C function + [checkerror] = 6, -- Lua function + [coroutine.running()] = 7, -- thread + [true] = 8, -- boolean + [io.stdin] = 9, -- userdata + [{}] = 10, -- table + } + local b = {}; for i = 1, 10 do b[i] = true end + for k, v in pairs(a) do + assert(b[v]); b[v] = undef + end + assert(next(b) == nil) -- 'b' now is empty +end + + +-- erasing values +local t = {[{1}] = 1, [{2}] = 2, [string.rep("x ", 4)] = 3, + [100.3] = 4, [4] = 5} + +local n = 0 +for k, v in pairs( t ) do + n = n+1 + assert(t[k] == v) + t[k] = undef + collectgarbage() + assert(t[k] == undef) +end +assert(n == 5) + + +local function test (a) + assert(not pcall(table.insert, a, 2, 20)); + table.insert(a, 10); table.insert(a, 2, 20); + table.insert(a, 1, -1); table.insert(a, 40); + table.insert(a, #a+1, 50) + table.insert(a, 2, -2) + assert(a[2] ~= undef) + assert(a["2"] == undef) + assert(not pcall(table.insert, a, 0, 20)); + assert(not pcall(table.insert, a, #a + 2, 20)); + assert(table.remove(a,1) == -1) + assert(table.remove(a,1) == -2) + assert(table.remove(a,1) == 10) + assert(table.remove(a,1) == 20) + assert(table.remove(a,1) == 40) + assert(table.remove(a,1) == 50) + assert(table.remove(a,1) == nil) + assert(table.remove(a) == nil) + assert(table.remove(a, #a) == nil) +end + +a = {n=0, [-7] = "ban"} +test(a) +assert(a.n == 0 and a[-7] == "ban") + +a = {[-7] = "ban"}; +test(a) +assert(a.n == nil and #a == 0 and a[-7] == "ban") + +a = {[-1] = "ban"} +test(a) +assert(#a == 0 and table.remove(a) == nil and a[-1] == "ban") + +a = {[0] = "ban"} +assert(#a == 0 and table.remove(a) == "ban" and a[0] == undef) + +table.insert(a, 1, 10); table.insert(a, 1, 20); table.insert(a, 1, -1) +assert(table.remove(a) == 10) +assert(table.remove(a) == 20) +assert(table.remove(a) == -1) +assert(table.remove(a) == nil) + +a = {'c', 'd'} +table.insert(a, 3, 'a') +table.insert(a, 'b') +assert(table.remove(a, 1) == 'c') +assert(table.remove(a, 1) == 'd') +assert(table.remove(a, 1) == 'a') +assert(table.remove(a, 1) == 'b') +assert(table.remove(a, 1) == nil) +assert(#a == 0 and a.n == nil) + +a = {10,20,30,40} +assert(table.remove(a, #a + 1) == nil) +assert(not pcall(table.remove, a, 0)) +assert(a[#a] == 40) +assert(table.remove(a, #a) == 40) +assert(a[#a] == 30) +assert(table.remove(a, 2) == 20) +assert(a[#a] == 30 and #a == 2) + +do -- testing table library with metamethods + local function test (proxy, t) + for i = 1, 10 do + table.insert(proxy, 1, i) + end + assert(#proxy == 10 and #t == 10 and proxy[1] ~= undef) + for i = 1, 10 do + assert(t[i] == 11 - i) + end + table.sort(proxy) + for i = 1, 10 do + assert(t[i] == i and proxy[i] == i) + end + assert(table.concat(proxy, ",") == "1,2,3,4,5,6,7,8,9,10") + for i = 1, 8 do + assert(table.remove(proxy, 1) == i) + end + assert(#proxy == 2 and #t == 2) + local a, b, c = table.unpack(proxy) + assert(a == 9 and b == 10 and c == nil) + end + + -- all virtual + local t = {} + local proxy = setmetatable({}, { + __len = function () return #t end, + __index = t, + __newindex = t, + }) + test(proxy, t) + + -- only __newindex + local count = 0 + t = setmetatable({}, { + __newindex = function (t,k,v) count = count + 1; rawset(t,k,v) end}) + test(t, t) + assert(count == 10) -- after first 10, all other sets are not new + + -- no __newindex + t = setmetatable({}, { + __index = function (_,k) return k + 1 end, + __len = function (_) return 5 end}) + assert(table.concat(t, ";") == "2;3;4;5;6") + +end + + +if not T then + (Message or print) + ('\n >>> testC not active: skipping tests for table library on non-tables <<<\n') +else --[ + local debug = require'debug' + local tab = {10, 20, 30} + local mt = {} + local u = T.newuserdata(0) + checkerror("table expected", table.insert, u, 40) + checkerror("table expected", table.remove, u) + debug.setmetatable(u, mt) + checkerror("table expected", table.insert, u, 40) + checkerror("table expected", table.remove, u) + mt.__index = tab + checkerror("table expected", table.insert, u, 40) + checkerror("table expected", table.remove, u) + mt.__newindex = tab + checkerror("table expected", table.insert, u, 40) + checkerror("table expected", table.remove, u) + mt.__len = function () return #tab end + table.insert(u, 40) + assert(#u == 4 and #tab == 4 and u[4] == 40 and tab[4] == 40) + assert(table.remove(u) == 40) + table.insert(u, 1, 50) + assert(#u == 4 and #tab == 4 and u[4] == 30 and tab[1] == 50) + + mt.__newindex = nil + mt.__len = nil + local tab2 = {} + local u2 = T.newuserdata(0) + debug.setmetatable(u2, {__newindex = function (_, k, v) tab2[k] = v end}) + table.move(u, 1, 4, 1, u2) + assert(#tab2 == 4 and tab2[1] == tab[1] and tab2[4] == tab[4]) + +end -- ] + +print('+') + +a = {} +for i=1,1000 do + a[i] = i; a[i - 1] = undef +end +assert(next(a,nil) == 1000 and next(a,1000) == nil) + +assert(next({}) == nil) +assert(next({}, nil) == nil) + +for a,b in pairs{} do error"not here" end +for i=1,0 do error'not here' end +for i=0,1,-1 do error'not here' end +a = nil; for i=1,1 do assert(not a); a=1 end; assert(a) +a = nil; for i=1,1,-1 do assert(not a); a=1 end; assert(a) + +do + print("testing floats in numeric for") + local a + -- integer count + a = 0; for i=1, 1, 1 do a=a+1 end; assert(a==1) + a = 0; for i=10000, 1e4, -1 do a=a+1 end; assert(a==1) + a = 0; for i=1, 0.99999, 1 do a=a+1 end; assert(a==0) + a = 0; for i=9999, 1e4, -1 do a=a+1 end; assert(a==0) + a = 0; for i=1, 0.99999, -1 do a=a+1 end; assert(a==1) + + -- float count + a = 0; for i=0, 0.999999999, 0.1 do a=a+1 end; assert(a==10) + a = 0; for i=1.0, 1, 1 do a=a+1 end; assert(a==1) + a = 0; for i=-1.5, -1.5, 1 do a=a+1 end; assert(a==1) + a = 0; for i=1e6, 1e6, -1 do a=a+1 end; assert(a==1) + a = 0; for i=1.0, 0.99999, 1 do a=a+1 end; assert(a==0) + a = 0; for i=99999, 1e5, -1.0 do a=a+1 end; assert(a==0) + a = 0; for i=1.0, 0.99999, -1 do a=a+1 end; assert(a==1) +end + +-- conversion +a = 0; for i="10","1","-2" do a=a+1 end; assert(a==5) + +do -- checking types + local c + local function checkfloat (i) + assert(math.type(i) == "float") + c = c + 1 + end + + c = 0; for i = 1.0, 10 do checkfloat(i) end + assert(c == 10) + + c = 0; for i = -1, -10, -1.0 do checkfloat(i) end + assert(c == 10) + + local function checkint (i) + assert(math.type(i) == "integer") + c = c + 1 + end + + local m = math.maxinteger + c = 0; for i = m, m - 10, -1 do checkint(i) end + assert(c == 11) + + c = 0; for i = 1, 10.9 do checkint(i) end + assert(c == 10) + + c = 0; for i = 10, 0.001, -1 do checkint(i) end + assert(c == 10) + + c = 0; for i = 1, "10.8" do checkint(i) end + assert(c == 10) + + c = 0; for i = 9, "3.4", -1 do checkint(i) end + assert(c == 6) + + c = 0; for i = 0, " -3.4 ", -1 do checkint(i) end + assert(c == 4) + + c = 0; for i = 100, "96.3", -2 do checkint(i) end + assert(c == 2) + + c = 0; for i = 1, math.huge do if i > 10 then break end; checkint(i) end + assert(c == 10) + + c = 0; for i = -1, -math.huge, -1 do + if i < -10 then break end; checkint(i) + end + assert(c == 10) + + + for i = math.mininteger, -10e100 do assert(false) end + for i = math.maxinteger, 10e100, -1 do assert(false) end + +end + +collectgarbage() + + +-- testing generic 'for' + +local function f (n, p) + local t = {}; for i=1,p do t[i] = i*10 end + return function (_,n) + if n > 0 then + n = n-1 + return n, table.unpack(t) + end + end, nil, n +end + +local x = 0 +for n,a,b,c,d in f(5,3) do + x = x+1 + assert(a == 10 and b == 20 and c == 30 and d == nil) +end +assert(x == 5) + + + +-- testing __pairs and __ipairs metamethod +a = {} +do + local x,y,z = pairs(a) + assert(type(x) == 'function' and y == a and z == nil) +end + +local function foo (e,i) + assert(e == a) + if i <= 10 then return i+1, i+2 end +end + +local function foo1 (e,i) + i = i + 1 + assert(e == a) + if i <= e.n then return i,a[i] end +end + +setmetatable(a, {__pairs = function (x) return foo, x, 0 end}) + +local i = 0 +for k,v in pairs(a) do + i = i + 1 + assert(k == i and v == k+1) +end + +a.n = 5 +a[3] = 30 + +-- testing ipairs with metamethods +a = {n=10} +setmetatable(a, { __index = function (t,k) + if k <= t.n then return k * 10 end + end}) +i = 0 +for k,v in ipairs(a) do + i = i + 1 + assert(k == i and v == i * 10) +end +assert(i == a.n) + +print"OK" diff --git a/testes/pm.lua b/testes/pm.lua new file mode 100644 index 0000000000..e517c8b668 --- /dev/null +++ b/testes/pm.lua @@ -0,0 +1,374 @@ +-- $Id: pm.lua,v 1.50 2018/03/12 14:19:36 roberto Exp $ +-- See Copyright Notice in file all.lua + +print('testing pattern matching') + +local function checkerror (msg, f, ...) + local s, err = pcall(f, ...) + assert(not s and string.find(err, msg)) +end + + +function f(s, p) + local i,e = string.find(s, p) + if i then return string.sub(s, i, e) end +end + +a,b = string.find('', '') -- empty patterns are tricky +assert(a == 1 and b == 0); +a,b = string.find('alo', '') +assert(a == 1 and b == 0) +a,b = string.find('a\0o a\0o a\0o', 'a', 1) -- first position +assert(a == 1 and b == 1) +a,b = string.find('a\0o a\0o a\0o', 'a\0o', 2) -- starts in the midle +assert(a == 5 and b == 7) +a,b = string.find('a\0o a\0o a\0o', 'a\0o', 9) -- starts in the midle +assert(a == 9 and b == 11) +a,b = string.find('a\0a\0a\0a\0\0ab', '\0ab', 2); -- finds at the end +assert(a == 9 and b == 11); +a,b = string.find('a\0a\0a\0a\0\0ab', 'b') -- last position +assert(a == 11 and b == 11) +assert(string.find('a\0a\0a\0a\0\0ab', 'b\0') == nil) -- check ending +assert(string.find('', '\0') == nil) +assert(string.find('alo123alo', '12') == 4) +assert(string.find('alo123alo', '^12') == nil) + +assert(string.match("aaab", ".*b") == "aaab") +assert(string.match("aaa", ".*a") == "aaa") +assert(string.match("b", ".*b") == "b") + +assert(string.match("aaab", ".+b") == "aaab") +assert(string.match("aaa", ".+a") == "aaa") +assert(not string.match("b", ".+b")) + +assert(string.match("aaab", ".?b") == "ab") +assert(string.match("aaa", ".?a") == "aa") +assert(string.match("b", ".?b") == "b") + +assert(f('aloALO', '%l*') == 'alo') +assert(f('aLo_ALO', '%a*') == 'aLo') + +assert(f(" \n\r*&\n\r xuxu \n\n", "%g%g%g+") == "xuxu") + +assert(f('aaab', 'a*') == 'aaa'); +assert(f('aaa', '^.*$') == 'aaa'); +assert(f('aaa', 'b*') == ''); +assert(f('aaa', 'ab*a') == 'aa') +assert(f('aba', 'ab*a') == 'aba') +assert(f('aaab', 'a+') == 'aaa') +assert(f('aaa', '^.+$') == 'aaa') +assert(f('aaa', 'b+') == nil) +assert(f('aaa', 'ab+a') == nil) +assert(f('aba', 'ab+a') == 'aba') +assert(f('a$a', '.$') == 'a') +assert(f('a$a', '.%$') == 'a$') +assert(f('a$a', '.$.') == 'a$a') +assert(f('a$a', '$$') == nil) +assert(f('a$b', 'a$') == nil) +assert(f('a$a', '$') == '') +assert(f('', 'b*') == '') +assert(f('aaa', 'bb*') == nil) +assert(f('aaab', 'a-') == '') +assert(f('aaa', '^.-$') == 'aaa') +assert(f('aabaaabaaabaaaba', 'b.*b') == 'baaabaaabaaab') +assert(f('aabaaabaaabaaaba', 'b.-b') == 'baaab') +assert(f('alo xo', '.o$') == 'xo') +assert(f(' \n isto assim', '%S%S*') == 'isto') +assert(f(' \n isto assim', '%S*$') == 'assim') +assert(f(' \n isto assim', '[a-z]*$') == 'assim') +assert(f('um caracter ? extra', '[^%sa-z]') == '?') +assert(f('', 'a?') == '') +assert(f('', '?') == '') +assert(f('bl', '?b?l?') == 'bl') +assert(f(' bl', '?b?l?') == '') +assert(f('aa', '^aa?a?a') == 'aa') +assert(f(']]]b', '[^]]') == '') +assert(f("0alo alo", "%x*") == "0a") +assert(f("alo alo", "%C+") == "alo alo") +print('+') + + +function f1(s, p) + p = string.gsub(p, "%%([0-9])", function (s) + return "%" .. (tonumber(s)+1) + end) + p = string.gsub(p, "^(^?)", "%1()", 1) + p = string.gsub(p, "($?)$", "()%1", 1) + local t = {string.match(s, p)} + return string.sub(s, t[1], t[#t] - 1) +end + +assert(f1('alo alx 123 b\0o b\0o', '(..*) %1') == "b\0o b\0o") +assert(f1('axz123= 4= 4 34', '(.+)=(.*)=%2 %1') == '3= 4= 4 3') +assert(f1('=======', '^(=*)=%1$') == '=======') +assert(string.match('==========', '^([=]*)=%1$') == nil) + +local function range (i, j) + if i <= j then + return i, range(i+1, j) + end +end + +local abc = string.char(range(0, 127)) .. string.char(range(128, 255)); + +assert(string.len(abc) == 256) + +function strset (p) + local res = {s=''} + string.gsub(abc, p, function (c) res.s = res.s .. c end) + return res.s +end; + +assert(string.len(strset('[\200-\210]')) == 11) + +assert(strset('[a-z]') == "abcdefghijklmnopqrstuvwxyz") +assert(strset('[a-z%d]') == strset('[%da-uu-z]')) +assert(strset('[a-]') == "-a") +assert(strset('[^%W]') == strset('[%w]')) +assert(strset('[]%%]') == '%]') +assert(strset('[a%-z]') == '-az') +assert(strset('[%^%[%-a%]%-b]') == '-[]^ab') +assert(strset('%Z') == strset('[\1-\255]')) +assert(strset('.') == strset('[\1-\255%z]')) +print('+'); + +assert(string.match("alo xyzK", "(%w+)K") == "xyz") +assert(string.match("254 K", "(%d*)K") == "") +assert(string.match("alo ", "(%w*)$") == "") +assert(string.match("alo ", "(%w+)$") == nil) +assert(string.find("(lo)", "%(") == 1) +local a, b, c, d, e = string.match("lo alo", "^(((.).).* (%w*))$") +assert(a == 'lo alo' and b == 'l' and c == '' and d == 'alo' and e == nil) +a, b, c, d = string.match('0123456789', '(.+(.?)())') +assert(a == '0123456789' and b == '' and c == 11 and d == nil) +print('+') + +assert(string.gsub('lo lo', '', 'x') == 'xlo xlo') +assert(string.gsub('alo lo ', ' +$', '') == 'alo lo') -- trim +assert(string.gsub(' alo alo ', '^%s*(.-)%s*$', '%1') == 'alo alo') -- double trim +assert(string.gsub('alo alo \n 123\n ', '%s+', ' ') == 'alo alo 123 ') +t = "ab d" +a, b = string.gsub(t, '(.)', '%1@') +assert('@'..a == string.gsub(t, '', '@') and b == 5) +a, b = string.gsub('abd', '(.)', '%0@', 2) +assert(a == 'a@b@d' and b == 2) +assert(string.gsub('alo alo', '()[al]', '%1') == '12o 56o') +assert(string.gsub("abc=xyz", "(%w*)(%p)(%w+)", "%3%2%1-%0") == + "xyz=abc-abc=xyz") +assert(string.gsub("abc", "%w", "%1%0") == "aabbcc") +assert(string.gsub("abc", "%w+", "%0%1") == "abcabc") +assert(string.gsub('', '$', '\0') == '\0') +assert(string.gsub('', '^', 'r') == 'r') +assert(string.gsub('', '$', 'r') == 'r') +print('+') + + +do -- new (5.3.3) semantics for empty matches + assert(string.gsub("a b cd", " *", "-") == "-a-b-c-d-") + + local res = "" + local sub = "a \nbc\t\td" + local i = 1 + for p, e in string.gmatch(sub, "()%s*()") do + res = res .. string.sub(sub, i, p - 1) .. "-" + i = e + end + assert(res == "-a-b-c-d-") +end + + +assert(string.gsub("um (dois) tres (quatro)", "(%(%w+%))", string.upper) == + "um (DOIS) tres (QUATRO)") + +do + local function setglobal (n,v) rawset(_G, n, v) end + string.gsub("a=roberto,roberto=a", "(%w+)=(%w%w*)", setglobal) + assert(_G.a=="roberto" and _G.roberto=="a") +end + +function f(a,b) return string.gsub(a,'.',b) end +assert(string.gsub("trocar tudo em |teste|b| |beleza|al|", "|([^|]*)|([^|]*)|", f) == + "trocar tudo em bbbbb alalalalalal") + +local function dostring (s) return load(s, "")() or "" end +assert(string.gsub("alo $a='x'$ novamente $return a$", + "$([^$]*)%$", + dostring) == "alo novamente x") + +x = string.gsub("$x=string.gsub('alo', '.', string.upper)$ assim vai para $return x$", + "$([^$]*)%$", dostring) +assert(x == ' assim vai para ALO') + +t = {} +s = 'a alo jose joao' +r = string.gsub(s, '()(%w+)()', function (a,w,b) + assert(string.len(w) == b-a); + t[a] = b-a; + end) +assert(s == r and t[1] == 1 and t[3] == 3 and t[7] == 4 and t[13] == 4) + + +function isbalanced (s) + return string.find(string.gsub(s, "%b()", ""), "[()]") == nil +end + +assert(isbalanced("(9 ((8))(\0) 7) \0\0 a b ()(c)() a")) +assert(not isbalanced("(9 ((8) 7) a b (\0 c) a")) +assert(string.gsub("alo 'oi' alo", "%b''", '"') == 'alo " alo') + + +local t = {"apple", "orange", "lime"; n=0} +assert(string.gsub("x and x and x", "x", function () t.n=t.n+1; return t[t.n] end) + == "apple and orange and lime") + +t = {n=0} +string.gsub("first second word", "%w%w*", function (w) t.n=t.n+1; t[t.n] = w end) +assert(t[1] == "first" and t[2] == "second" and t[3] == "word" and t.n == 3) + +t = {n=0} +assert(string.gsub("first second word", "%w+", + function (w) t.n=t.n+1; t[t.n] = w end, 2) == "first second word") +assert(t[1] == "first" and t[2] == "second" and t[3] == undef) + +checkerror("invalid replacement value %(a table%)", + string.gsub, "alo", ".", {a = {}}) +checkerror("invalid capture index %%2", string.gsub, "alo", ".", "%2") +checkerror("invalid capture index %%0", string.gsub, "alo", "(%0)", "a") +checkerror("invalid capture index %%1", string.gsub, "alo", "(%1)", "a") +checkerror("invalid use of '%%'", string.gsub, "alo", ".", "%x") + +-- bug since 2.5 (C-stack overflow) +do + local function f (size) + local s = string.rep("a", size) + local p = string.rep(".?", size) + return pcall(string.match, s, p) + end + local r, m = f(80) + assert(r and #m == 80) + r, m = f(200000) + assert(not r and string.find(m, "too complex")) +end + +if not _soft then + print("big strings") + local a = string.rep('a', 300000) + assert(string.find(a, '^a*.?$')) + assert(not string.find(a, '^a*.?b$')) + assert(string.find(a, '^a-.?$')) + + -- bug in 5.1.2 + a = string.rep('a', 10000) .. string.rep('b', 10000) + assert(not pcall(string.gsub, a, 'b')) +end + +-- recursive nest of gsubs +function rev (s) + return string.gsub(s, "(.)(.+)", function (c,s1) return rev(s1)..c end) +end + +local x = "abcdef" +assert(rev(rev(x)) == x) + + +-- gsub with tables +assert(string.gsub("alo alo", ".", {}) == "alo alo") +assert(string.gsub("alo alo", "(.)", {a="AA", l=""}) == "AAo AAo") +assert(string.gsub("alo alo", "(.).", {a="AA", l="K"}) == "AAo AAo") +assert(string.gsub("alo alo", "((.)(.?))", {al="AA", o=false}) == "AAo AAo") + +assert(string.gsub("alo alo", "().", {'x','yy','zzz'}) == "xyyzzz alo") + +t = {}; setmetatable(t, {__index = function (t,s) return string.upper(s) end}) +assert(string.gsub("a alo b hi", "%w%w+", t) == "a ALO b HI") + + +-- tests for gmatch +local a = 0 +for i in string.gmatch('abcde', '()') do assert(i == a+1); a=i end +assert(a==6) + +t = {n=0} +for w in string.gmatch("first second word", "%w+") do + t.n=t.n+1; t[t.n] = w +end +assert(t[1] == "first" and t[2] == "second" and t[3] == "word") + +t = {3, 6, 9} +for i in string.gmatch ("xuxx uu ppar r", "()(.)%2") do + assert(i == table.remove(t, 1)) +end +assert(#t == 0) + +t = {} +for i,j in string.gmatch("13 14 10 = 11, 15= 16, 22=23", "(%d+)%s*=%s*(%d+)") do + t[tonumber(i)] = tonumber(j) +end +a = 0 +for k,v in pairs(t) do assert(k+1 == v+0); a=a+1 end +assert(a == 3) + + +-- tests for `%f' (`frontiers') + +assert(string.gsub("aaa aa a aaa a", "%f[%w]a", "x") == "xaa xa x xaa x") +assert(string.gsub("[[]] [][] [[[[", "%f[[].", "x") == "x[]] x]x] x[[[") +assert(string.gsub("01abc45de3", "%f[%d]", ".") == ".01abc.45de.3") +assert(string.gsub("01abc45 de3x", "%f[%D]%w", ".") == "01.bc45 de3.") +assert(string.gsub("function", "%f[\1-\255]%w", ".") == ".unction") +assert(string.gsub("function", "%f[^\1-\255]", ".") == "function.") + +assert(string.find("a", "%f[a]") == 1) +assert(string.find("a", "%f[^%z]") == 1) +assert(string.find("a", "%f[^%l]") == 2) +assert(string.find("aba", "%f[a%z]") == 3) +assert(string.find("aba", "%f[%z]") == 4) +assert(not string.find("aba", "%f[%l%z]")) +assert(not string.find("aba", "%f[^%l%z]")) + +local i, e = string.find(" alo aalo allo", "%f[%S].-%f[%s].-%f[%S]") +assert(i == 2 and e == 5) +local k = string.match(" alo aalo allo", "%f[%S](.-%f[%s].-%f[%S])") +assert(k == 'alo ') + +local a = {1, 5, 9, 14, 17,} +for k in string.gmatch("alo alo th02 is 1hat", "()%f[%w%d]") do + assert(table.remove(a, 1) == k) +end +assert(#a == 0) + + +-- malformed patterns +local function malform (p, m) + m = m or "malformed" + local r, msg = pcall(string.find, "a", p) + assert(not r and string.find(msg, m)) +end + +malform("(.", "unfinished capture") +malform(".)", "invalid pattern capture") +malform("[a") +malform("[]") +malform("[^]") +malform("[a%]") +malform("[a%") +malform("%b") +malform("%ba") +malform("%") +malform("%f", "missing") + +-- \0 in patterns +assert(string.match("ab\0\1\2c", "[\0-\2]+") == "\0\1\2") +assert(string.match("ab\0\1\2c", "[\0-\0]+") == "\0") +assert(string.find("b$a", "$\0?") == 2) +assert(string.find("abc\0efg", "%\0") == 4) +assert(string.match("abc\0efg\0\1e\1g", "%b\0\1") == "\0efg\0\1e\1") +assert(string.match("abc\0\0\0", "%\0+") == "\0\0\0") +assert(string.match("abc\0\0\0", "%\0%\0?") == "\0\0") + +-- magic char after \0 +assert(string.find("abc\0\0","\0.") == 4) +assert(string.find("abcx\0\0abc\0abc","x\0\0abc\0a.") == 4) + +print('OK') + diff --git a/testes/sort.lua b/testes/sort.lua new file mode 100644 index 0000000000..6eb9b70643 --- /dev/null +++ b/testes/sort.lua @@ -0,0 +1,310 @@ +-- $Id: sort.lua,v 1.39 2018/03/12 13:51:02 roberto Exp $ +-- See Copyright Notice in file all.lua + +print "testing (parts of) table library" + +print "testing unpack" + +local unpack = table.unpack + +local maxI = math.maxinteger +local minI = math.mininteger + + +local function checkerror (msg, f, ...) + local s, err = pcall(f, ...) + assert(not s and string.find(err, msg)) +end + + +checkerror("wrong number of arguments", table.insert, {}, 2, 3, 4) + +local x,y,z,a,n +a = {}; lim = _soft and 200 or 2000 +for i=1, lim do a[i]=i end +assert(select(lim, unpack(a)) == lim and select('#', unpack(a)) == lim) +x = unpack(a) +assert(x == 1) +x = {unpack(a)} +assert(#x == lim and x[1] == 1 and x[lim] == lim) +x = {unpack(a, lim-2)} +assert(#x == 3 and x[1] == lim-2 and x[3] == lim) +x = {unpack(a, 10, 6)} +assert(next(x) == nil) -- no elements +x = {unpack(a, 11, 10)} +assert(next(x) == nil) -- no elements +x,y = unpack(a, 10, 10) +assert(x == 10 and y == nil) +x,y,z = unpack(a, 10, 11) +assert(x == 10 and y == 11 and z == nil) +a,x = unpack{1} +assert(a==1 and x==nil) +a,x = unpack({1,2}, 1, 1) +assert(a==1 and x==nil) + +do + local maxi = (1 << 31) - 1 -- maximum value for an int (usually) + local mini = -(1 << 31) -- minimum value for an int (usually) + checkerror("too many results", unpack, {}, 0, maxi) + checkerror("too many results", unpack, {}, 1, maxi) + checkerror("too many results", unpack, {}, 0, maxI) + checkerror("too many results", unpack, {}, 1, maxI) + checkerror("too many results", unpack, {}, mini, maxi) + checkerror("too many results", unpack, {}, -maxi, maxi) + checkerror("too many results", unpack, {}, minI, maxI) + unpack({}, maxi, 0) + unpack({}, maxi, 1) + unpack({}, maxI, minI) + pcall(unpack, {}, 1, maxi + 1) + local a, b = unpack({[maxi] = 20}, maxi, maxi) + assert(a == 20 and b == nil) + a, b = unpack({[maxi] = 20}, maxi - 1, maxi) + assert(a == nil and b == 20) + local t = {[maxI - 1] = 12, [maxI] = 23} + a, b = unpack(t, maxI - 1, maxI); assert(a == 12 and b == 23) + a, b = unpack(t, maxI, maxI); assert(a == 23 and b == nil) + a, b = unpack(t, maxI, maxI - 1); assert(a == nil and b == nil) + t = {[minI] = 12.3, [minI + 1] = 23.5} + a, b = unpack(t, minI, minI + 1); assert(a == 12.3 and b == 23.5) + a, b = unpack(t, minI, minI); assert(a == 12.3 and b == nil) + a, b = unpack(t, minI + 1, minI); assert(a == nil and b == nil) +end + +do -- length is not an integer + local t = setmetatable({}, {__len = function () return 'abc' end}) + assert(#t == 'abc') + checkerror("object length is not an integer", table.insert, t, 1) +end + +print "testing pack" + +a = table.pack() +assert(a[1] == undef and a.n == 0) + +a = table.pack(table) +assert(a[1] == table and a.n == 1) + +a = table.pack(nil, nil, nil, nil) +assert(a[1] == nil and a.n == 4) + + +-- testing move +do + + checkerror("table expected", table.move, 1, 2, 3, 4) + + local function eqT (a, b) + for k, v in pairs(a) do assert(b[k] == v) end + for k, v in pairs(b) do assert(a[k] == v) end + end + + local a = table.move({10,20,30}, 1, 3, 2) -- move forward + eqT(a, {10,10,20,30}) + + -- move forward with overlap of 1 + a = table.move({10, 20, 30}, 1, 3, 3) + eqT(a, {10, 20, 10, 20, 30}) + + -- moving to the same table (not being explicit about it) + a = {10, 20, 30, 40} + table.move(a, 1, 4, 2, a) + eqT(a, {10, 10, 20, 30, 40}) + + a = table.move({10,20,30}, 2, 3, 1) -- move backward + eqT(a, {20,30,30}) + + a = {} -- move to new table + assert(table.move({10,20,30}, 1, 3, 1, a) == a) + eqT(a, {10,20,30}) + + a = {} + assert(table.move({10,20,30}, 1, 0, 3, a) == a) -- empty move (no move) + eqT(a, {}) + + a = table.move({10,20,30}, 1, 10, 1) -- move to the same place + eqT(a, {10,20,30}) + + -- moving on the fringes + a = table.move({[maxI - 2] = 1, [maxI - 1] = 2, [maxI] = 3}, + maxI - 2, maxI, -10, {}) + eqT(a, {[-10] = 1, [-9] = 2, [-8] = 3}) + + a = table.move({[minI] = 1, [minI + 1] = 2, [minI + 2] = 3}, + minI, minI + 2, -10, {}) + eqT(a, {[-10] = 1, [-9] = 2, [-8] = 3}) + + a = table.move({45}, 1, 1, maxI) + eqT(a, {45, [maxI] = 45}) + + a = table.move({[maxI] = 100}, maxI, maxI, minI) + eqT(a, {[minI] = 100, [maxI] = 100}) + + a = table.move({[minI] = 100}, minI, minI, maxI) + eqT(a, {[minI] = 100, [maxI] = 100}) + + a = setmetatable({}, { + __index = function (_,k) return k * 10 end, + __newindex = error}) + local b = table.move(a, 1, 10, 3, {}) + eqT(a, {}) + eqT(b, {nil,nil,10,20,30,40,50,60,70,80,90,100}) + + b = setmetatable({""}, { + __index = error, + __newindex = function (t,k,v) + t[1] = string.format("%s(%d,%d)", t[1], k, v) + end}) + table.move(a, 10, 13, 3, b) + assert(b[1] == "(3,100)(4,110)(5,120)(6,130)") + local stat, msg = pcall(table.move, b, 10, 13, 3, b) + assert(not stat and msg == b) +end + +do + -- for very long moves, just check initial accesses and interrupt + -- move with an error + local function checkmove (f, e, t, x, y) + local pos1, pos2 + local a = setmetatable({}, { + __index = function (_,k) pos1 = k end, + __newindex = function (_,k) pos2 = k; error() end, }) + local st, msg = pcall(table.move, a, f, e, t) + assert(not st and not msg and pos1 == x and pos2 == y) + end + checkmove(1, maxI, 0, 1, 0) + checkmove(0, maxI - 1, 1, maxI - 1, maxI) + checkmove(minI, -2, -5, -2, maxI - 6) + checkmove(minI + 1, -1, -2, -1, maxI - 3) + checkmove(minI, -2, 0, minI, 0) -- non overlapping + checkmove(minI + 1, -1, 1, minI + 1, 1) -- non overlapping +end + +checkerror("too many", table.move, {}, 0, maxI, 1) +checkerror("too many", table.move, {}, -1, maxI - 1, 1) +checkerror("too many", table.move, {}, minI, -1, 1) +checkerror("too many", table.move, {}, minI, maxI, 1) +checkerror("wrap around", table.move, {}, 1, maxI, 2) +checkerror("wrap around", table.move, {}, 1, 2, maxI) +checkerror("wrap around", table.move, {}, minI, -2, 2) + + +print"testing sort" + + +-- strange lengths +local a = setmetatable({}, {__len = function () return -1 end}) +assert(#a == -1) +table.sort(a, error) -- should not compare anything +a = setmetatable({}, {__len = function () return maxI end}) +checkerror("too big", table.sort, a) + +-- test checks for invalid order functions +local function check (t) + local function f(a, b) assert(a and b); return true end + checkerror("invalid order function", table.sort, t, f) +end + +check{1,2,3,4} +check{1,2,3,4,5} +check{1,2,3,4,5,6} + + +function check (a, f) + f = f or function (x,y) return x 'alo\0alo\0') +assert('alo' < 'alo\0') +assert('alo\0' > 'alo') +assert('\0' < '\1') +assert('\0\0' < '\0\1') +assert('\1\0a\0a' <= '\1\0a\0a') +assert(not ('\1\0a\0b' <= '\1\0a\0a')) +assert('\0\0\0' < '\0\0\0\0') +assert(not('\0\0\0\0' < '\0\0\0')) +assert('\0\0\0' <= '\0\0\0\0') +assert(not('\0\0\0\0' <= '\0\0\0')) +assert('\0\0\0' <= '\0\0\0') +assert('\0\0\0' >= '\0\0\0') +assert(not ('\0\0b' < '\0\0a\0')) + +-- testing string.sub +assert(string.sub("123456789",2,4) == "234") +assert(string.sub("123456789",7) == "789") +assert(string.sub("123456789",7,6) == "") +assert(string.sub("123456789",7,7) == "7") +assert(string.sub("123456789",0,0) == "") +assert(string.sub("123456789",-10,10) == "123456789") +assert(string.sub("123456789",1,9) == "123456789") +assert(string.sub("123456789",-10,-20) == "") +assert(string.sub("123456789",-1) == "9") +assert(string.sub("123456789",-4) == "6789") +assert(string.sub("123456789",-6, -4) == "456") +assert(string.sub("123456789", mini, -4) == "123456") +assert(string.sub("123456789", mini, maxi) == "123456789") +assert(string.sub("123456789", mini, mini) == "") +assert(string.sub("\000123456789",3,5) == "234") +assert(("\000123456789"):sub(8) == "789") + +-- testing string.find +assert(string.find("123456789", "345") == 3) +a,b = string.find("123456789", "345") +assert(string.sub("123456789", a, b) == "345") +assert(string.find("1234567890123456789", "345", 3) == 3) +assert(string.find("1234567890123456789", "345", 4) == 13) +assert(string.find("1234567890123456789", "346", 4) == nil) +assert(string.find("1234567890123456789", ".45", -9) == 13) +assert(string.find("abcdefg", "\0", 5, 1) == nil) +assert(string.find("", "") == 1) +assert(string.find("", "", 1) == 1) +assert(not string.find("", "", 2)) +assert(string.find('', 'aaa', 1) == nil) +assert(('alo(.)alo'):find('(.)', 1, 1) == 4) + +assert(string.len("") == 0) +assert(string.len("\0\0\0") == 3) +assert(string.len("1234567890") == 10) + +assert(#"" == 0) +assert(#"\0\0\0" == 3) +assert(#"1234567890" == 10) + +-- testing string.byte/string.char +assert(string.byte("a") == 97) +assert(string.byte("\xe4") > 127) +assert(string.byte(string.char(255)) == 255) +assert(string.byte(string.char(0)) == 0) +assert(string.byte("\0") == 0) +assert(string.byte("\0\0alo\0x", -1) == string.byte('x')) +assert(string.byte("ba", 2) == 97) +assert(string.byte("\n\n", 2, -1) == 10) +assert(string.byte("\n\n", 2, 2) == 10) +assert(string.byte("") == nil) +assert(string.byte("hi", -3) == nil) +assert(string.byte("hi", 3) == nil) +assert(string.byte("hi", 9, 10) == nil) +assert(string.byte("hi", 2, 1) == nil) +assert(string.char() == "") +assert(string.char(0, 255, 0) == "\0\255\0") +assert(string.char(0, string.byte("\xe4"), 0) == "\0\xe4\0") +assert(string.char(string.byte("\xe4l\0u", 1, -1)) == "\xe4l\0u") +assert(string.char(string.byte("\xe4l\0u", 1, 0)) == "") +assert(string.char(string.byte("\xe4l\0u", -10, 100)) == "\xe4l\0u") + +assert(string.upper("ab\0c") == "AB\0C") +assert(string.lower("\0ABCc%$") == "\0abcc%$") +assert(string.rep('teste', 0) == '') +assert(string.rep('ts\00t', 2) == 'ts\0tts\000t') +assert(string.rep('', 10) == '') + +if string.packsize("i") == 4 then + -- result length would be 2^31 (int overflow) + checkerror("too large", string.rep, 'aa', (1 << 30)) + checkerror("too large", string.rep, 'a', (1 << 30), ',') +end + +-- repetitions with separator +assert(string.rep('teste', 0, 'xuxu') == '') +assert(string.rep('teste', 1, 'xuxu') == 'teste') +assert(string.rep('\1\0\1', 2, '\0\0') == '\1\0\1\0\0\1\0\1') +assert(string.rep('', 10, '.') == string.rep('.', 9)) +assert(not pcall(string.rep, "aa", maxi // 2 + 10)) +assert(not pcall(string.rep, "", maxi // 2 + 10, "aa")) + +assert(string.reverse"" == "") +assert(string.reverse"\0\1\2\3" == "\3\2\1\0") +assert(string.reverse"\0001234" == "4321\0") + +for i=0,30 do assert(string.len(string.rep('a', i)) == i) end + +assert(type(tostring(nil)) == 'string') +assert(type(tostring(12)) == 'string') +assert(string.find(tostring{}, 'table:')) +assert(string.find(tostring(print), 'function:')) +assert(#tostring('\0') == 1) +assert(tostring(true) == "true") +assert(tostring(false) == "false") +assert(tostring(-1203) == "-1203") +assert(tostring(1203.125) == "1203.125") +assert(tostring(-0.5) == "-0.5") +assert(tostring(-32767) == "-32767") +if math.tointeger(2147483647) then -- no overflow? (32 bits) + assert(tostring(-2147483647) == "-2147483647") +end +if math.tointeger(4611686018427387904) then -- no overflow? (64 bits) + assert(tostring(4611686018427387904) == "4611686018427387904") + assert(tostring(-4611686018427387904) == "-4611686018427387904") +end + +if tostring(0.0) == "0.0" then -- "standard" coercion float->string + assert('' .. 12 == '12' and 12.0 .. '' == '12.0') + assert(tostring(-1203 + 0.0) == "-1203.0") +else -- compatible coercion + assert(tostring(0.0) == "0") + assert('' .. 12 == '12' and 12.0 .. '' == '12') + assert(tostring(-1203 + 0.0) == "-1203") +end + + +x = '"lo"\n\\' +assert(string.format('%q%s', x, x) == '"\\"lo\\"\\\n\\\\""lo"\n\\') +assert(string.format('%q', "\0") == [["\0"]]) +assert(load(string.format('return %q', x))() == x) +x = "\0\1\0023\5\0009" +assert(load(string.format('return %q', x))() == x) +assert(string.format("\0%c\0%c%x\0", string.byte("\xe4"), string.byte("b"), 140) == + "\0\xe4\0b8c\0") +assert(string.format('') == "") +assert(string.format("%c",34)..string.format("%c",48)..string.format("%c",90)..string.format("%c",100) == + string.format("%c%c%c%c", 34, 48, 90, 100)) +assert(string.format("%s\0 is not \0%s", 'not be', 'be') == 'not be\0 is not \0be') +assert(string.format("%%%d %010d", 10, 23) == "%10 0000000023") +assert(tonumber(string.format("%f", 10.3)) == 10.3) +x = string.format('"%-50s"', 'a') +assert(#x == 52) +assert(string.sub(x, 1, 4) == '"a ') + +assert(string.format("-%.20s.20s", string.rep("%", 2000)) == + "-"..string.rep("%", 20)..".20s") +assert(string.format('"-%20s.20s"', string.rep("%", 2000)) == + string.format("%q", "-"..string.rep("%", 2000)..".20s")) + +do + local function checkQ (v) + local s = string.format("%q", v) + local nv = load("return " .. s)() + assert(v == nv and math.type(v) == math.type(nv)) + end + checkQ("\0\0\1\255\u{234}") + checkQ(math.maxinteger) + checkQ(math.mininteger) + checkQ(math.pi) + checkQ(0.1) + checkQ(true) + checkQ(nil) + checkQ(false) + checkQ(math.huge) + checkQ(-math.huge) + assert(string.format("%q", 0/0) == "(0/0)") -- NaN + checkerror("no literal", string.format, "%q", {}) +end + +assert(string.format("\0%s\0", "\0\0\1") == "\0\0\0\1\0") +checkerror("contains zeros", string.format, "%10s", "\0") + +-- format x tostring +assert(string.format("%s %s", nil, true) == "nil true") +assert(string.format("%s %.4s", false, true) == "false true") +assert(string.format("%.3s %.3s", false, true) == "fal tru") +local m = setmetatable({}, {__tostring = function () return "hello" end, + __name = "hi"}) +assert(string.format("%s %.10s", m, m) == "hello hello") +getmetatable(m).__tostring = nil -- will use '__name' from now on +assert(string.format("%.4s", m) == "hi: ") + +getmetatable(m).__tostring = function () return {} end +checkerror("'__tostring' must return a string", tostring, m) + + +assert(string.format("%x", 0.0) == "0") +assert(string.format("%02x", 0.0) == "00") +assert(string.format("%08X", 0xFFFFFFFF) == "FFFFFFFF") +assert(string.format("%+08d", 31501) == "+0031501") +assert(string.format("%+08d", -30927) == "-0030927") + + +do -- longest number that can be formatted + local i = 1 + local j = 10000 + while i + 1 < j do -- binary search for maximum finite float + local m = (i + j) // 2 + if 10^m < math.huge then i = m else j = m end + end + assert(10^i < math.huge and 10^j == math.huge) + local s = string.format('%.99f', -(10^i)) + assert(string.len(s) >= i + 101) + assert(tonumber(s) == -(10^i)) +end + + +-- testing large numbers for format +do -- assume at least 32 bits + local max, min = 0x7fffffff, -0x80000000 -- "large" for 32 bits + assert(string.sub(string.format("%8x", -1), -8) == "ffffffff") + assert(string.format("%x", max) == "7fffffff") + assert(string.sub(string.format("%x", min), -8) == "80000000") + assert(string.format("%d", max) == "2147483647") + assert(string.format("%d", min) == "-2147483648") + assert(string.format("%u", 0xffffffff) == "4294967295") + assert(string.format("%o", 0xABCD) == "125715") + + max, min = 0x7fffffffffffffff, -0x8000000000000000 + if max > 2.0^53 then -- only for 64 bits + assert(string.format("%x", (2^52 | 0) - 1) == "fffffffffffff") + assert(string.format("0x%8X", 0x8f000003) == "0x8F000003") + assert(string.format("%d", 2^53) == "9007199254740992") + assert(string.format("%i", -2^53) == "-9007199254740992") + assert(string.format("%x", max) == "7fffffffffffffff") + assert(string.format("%x", min) == "8000000000000000") + assert(string.format("%d", max) == "9223372036854775807") + assert(string.format("%d", min) == "-9223372036854775808") + assert(string.format("%u", ~(-1 << 64)) == "18446744073709551615") + assert(tostring(1234567890123) == '1234567890123') + end +end + + +do print("testing 'format %a %A'") + local function matchhexa (n) + local s = string.format("%a", n) + -- result matches ISO C requirements + assert(string.find(s, "^%-?0x[1-9a-f]%.?[0-9a-f]*p[-+]?%d+$")) + assert(tonumber(s) == n) -- and has full precision + s = string.format("%A", n) + assert(string.find(s, "^%-?0X[1-9A-F]%.?[0-9A-F]*P[-+]?%d+$")) + assert(tonumber(s) == n) + end + for _, n in ipairs{0.1, -0.1, 1/3, -1/3, 1e30, -1e30, + -45/247, 1, -1, 2, -2, 3e-20, -3e-20} do + matchhexa(n) + end + + assert(string.find(string.format("%A", 0.0), "^0X0%.?0?P%+?0$")) + assert(string.find(string.format("%a", -0.0), "^%-0x0%.?0?p%+?0$")) + + if not _port then -- test inf, -inf, NaN, and -0.0 + assert(string.find(string.format("%a", 1/0), "^inf")) + assert(string.find(string.format("%A", -1/0), "^%-INF")) + assert(string.find(string.format("%a", 0/0), "^%-?nan")) + assert(string.find(string.format("%a", -0.0), "^%-0x0")) + end + + if not pcall(string.format, "%.3a", 0) then + (Message or print)("\n >>> modifiers for format '%a' not available <<<\n") + else + assert(string.find(string.format("%+.2A", 12), "^%+0X%x%.%x0P%+?%d$")) + assert(string.find(string.format("%.4A", -12), "^%-0X%x%.%x000P%+?%d$")) + end +end + + +-- errors in format + +local function check (fmt, msg) + checkerror(msg, string.format, fmt, 10) +end + +local aux = string.rep('0', 600) +check("%100.3d", "too long") +check("%1"..aux..".3d", "too long") +check("%1.100d", "too long") +check("%10.1"..aux.."004d", "too long") +check("%t", "invalid option") +check("%"..aux.."d", "repeated flags") +check("%d %d", "no value") + + +assert(load("return 1\n--comment without ending EOL")() == 1) + + +checkerror("table expected", table.concat, 3) +assert(table.concat{} == "") +assert(table.concat({}, 'x') == "") +assert(table.concat({'\0', '\0\1', '\0\1\2'}, '.\0.') == "\0.\0.\0\1.\0.\0\1\2") +local a = {}; for i=1,300 do a[i] = "xuxu" end +assert(table.concat(a, "123").."123" == string.rep("xuxu123", 300)) +assert(table.concat(a, "b", 20, 20) == "xuxu") +assert(table.concat(a, "", 20, 21) == "xuxuxuxu") +assert(table.concat(a, "x", 22, 21) == "") +assert(table.concat(a, "3", 299) == "xuxu3xuxu") +assert(table.concat({}, "x", maxi, maxi - 1) == "") +assert(table.concat({}, "x", mini + 1, mini) == "") +assert(table.concat({}, "x", maxi, mini) == "") +assert(table.concat({[maxi] = "alo"}, "x", maxi, maxi) == "alo") +assert(table.concat({[maxi] = "alo", [maxi - 1] = "y"}, "-", maxi - 1, maxi) + == "y-alo") + +assert(not pcall(table.concat, {"a", "b", {}})) + +a = {"a","b","c"} +assert(table.concat(a, ",", 1, 0) == "") +assert(table.concat(a, ",", 1, 1) == "a") +assert(table.concat(a, ",", 1, 2) == "a,b") +assert(table.concat(a, ",", 2) == "b,c") +assert(table.concat(a, ",", 3) == "c") +assert(table.concat(a, ",", 4) == "") + +if not _port then + + local locales = { "ptb", "pt_BR.iso88591", "ISO-8859-1" } + local function trylocale (w) + for i = 1, #locales do + if os.setlocale(locales[i], w) then + print(string.format("'%s' locale set to '%s'", w, locales[i])) + return locales[i] + end + end + print(string.format("'%s' locale not found", w)) + return false + end + + if trylocale("collate") then + assert("alo" < "lo" and "lo" < "amo") + end + + if trylocale("ctype") then + assert(string.gsub("", "%a", "x") == "xxxxx") + assert(string.gsub("", "%l", "x") == "xx") + assert(string.gsub("", "%u", "x") == "xx") + assert(string.upper"{xuxu}o" == "{XUXU}O") + end + + os.setlocale("C") + assert(os.setlocale() == 'C') + assert(os.setlocale(nil, "numeric") == 'C') + +end + + +-- bug in Lua 5.3.2 +-- 'gmatch' iterator does not work across coroutines +do + local f = string.gmatch("1 2 3 4 5", "%d+") + assert(f() == "1") + co = coroutine.wrap(f) + assert(co() == "2") +end + +print('OK') + diff --git a/testes/tpack.lua b/testes/tpack.lua new file mode 100644 index 0000000000..0e639cc5b4 --- /dev/null +++ b/testes/tpack.lua @@ -0,0 +1,324 @@ +-- $Id: tpack.lua,v 1.14 2018/06/04 14:26:32 roberto Exp $ +-- See Copyright Notice in file all.lua + +local pack = string.pack +local packsize = string.packsize +local unpack = string.unpack + +print "testing pack/unpack" + +-- maximum size for integers +local NB = 16 + +local sizeshort = packsize("h") +local sizeint = packsize("i") +local sizelong = packsize("l") +local sizesize_t = packsize("T") +local sizeLI = packsize("j") +local sizefloat = packsize("f") +local sizedouble = packsize("d") +local sizenumber = packsize("n") +local little = (pack("i2", 1) == "\1\0") +local align = packsize("!xXi16") + +assert(1 <= sizeshort and sizeshort <= sizeint and sizeint <= sizelong and + sizefloat <= sizedouble) + +print("platform:") +print(string.format( + "\tshort %d, int %d, long %d, size_t %d, float %d, double %d,\n\z + \tlua Integer %d, lua Number %d", + sizeshort, sizeint, sizelong, sizesize_t, sizefloat, sizedouble, + sizeLI, sizenumber)) +print("\t" .. (little and "little" or "big") .. " endian") +print("\talignment: " .. align) + + +-- check errors in arguments +function checkerror (msg, f, ...) + local status, err = pcall(f, ...) + -- print(status, err, msg) + assert(not status and string.find(err, msg)) +end + +-- minimum behavior for integer formats +assert(unpack("B", pack("B", 0xff)) == 0xff) +assert(unpack("b", pack("b", 0x7f)) == 0x7f) +assert(unpack("b", pack("b", -0x80)) == -0x80) + +assert(unpack("H", pack("H", 0xffff)) == 0xffff) +assert(unpack("h", pack("h", 0x7fff)) == 0x7fff) +assert(unpack("h", pack("h", -0x8000)) == -0x8000) + +assert(unpack("L", pack("L", 0xffffffff)) == 0xffffffff) +assert(unpack("l", pack("l", 0x7fffffff)) == 0x7fffffff) +assert(unpack("l", pack("l", -0x80000000)) == -0x80000000) + + +for i = 1, NB do + -- small numbers with signal extension ("\xFF...") + local s = string.rep("\xff", i) + assert(pack("i" .. i, -1) == s) + assert(packsize("i" .. i) == #s) + assert(unpack("i" .. i, s) == -1) + + -- small unsigned number ("\0...\xAA") + s = "\xAA" .. string.rep("\0", i - 1) + assert(pack("I" .. i, 0xAA) == s:reverse()) + assert(unpack(">I" .. i, s:reverse()) == 0xAA) +end + +do + local lnum = 0x13121110090807060504030201 + local s = pack("i" .. i, ("\xFF"):rep(i - sizeLI) .. s:reverse()) == -lnum) + assert(unpack("i" .. i, "\1" .. ("\x00"):rep(i - 1)) + end +end + +for i = 1, sizeLI do + local lstr = "\1\2\3\4\5\6\7\8\9\10\11\12\13" + local lnum = 0x13121110090807060504030201 + local n = lnum & (~(-1 << (i * 8))) + local s = string.sub(lstr, 1, i) + assert(pack("i" .. i, n) == s:reverse()) + assert(unpack(">i" .. i, s:reverse()) == n) +end + +-- sign extension +do + local u = 0xf0 + for i = 1, sizeLI - 1 do + assert(unpack("I"..i, "\xf0"..("\xff"):rep(i - 1)) == u) + u = u * 256 + 0xff + end +end + +-- mixed endianness +do + assert(pack(">i2 i2", "\10\0\0\20") + assert(a == 10 and b == 20) + assert(pack("=i4", 2001) == pack("i4", 2001)) +end + +print("testing invalid formats") + +checkerror("out of limits", pack, "i0", 0) +checkerror("out of limits", pack, "i" .. NB + 1, 0) +checkerror("out of limits", pack, "!" .. NB + 1, 0) +checkerror("%(17%) out of limits %[1,16%]", pack, "Xi" .. NB + 1) +checkerror("invalid format option 'r'", pack, "i3r", 0) +checkerror("16%-byte integer", unpack, "i16", string.rep('\3', 16)) +checkerror("not power of 2", pack, "!4i3", 0); +checkerror("missing size", pack, "c", "") +checkerror("variable%-length format", packsize, "s") +checkerror("variable%-length format", packsize, "z") + +-- overflow in option size (error will be in digit after limit) +checkerror("invalid format", packsize, "c1" .. string.rep("0", 40)) + +if packsize("i") == 4 then + -- result would be 2^31 (2^3 repetitions of 2^28 strings) + local s = string.rep("c268435456", 2^3) + checkerror("too large", packsize, s) + -- one less is OK + s = string.rep("c268435456", 2^3 - 1) .. "c268435455" + assert(packsize(s) == 0x7fffffff) +end + +-- overflow in packing +for i = 1, sizeLI - 1 do + local umax = (1 << (i * 8)) - 1 + local max = umax >> 1 + local min = ~max + checkerror("overflow", pack, "I" .. i, umax + 1) + + checkerror("overflow", pack, ">i" .. i, umax) + checkerror("overflow", pack, ">i" .. i, max + 1) + checkerror("overflow", pack, "i" .. i, pack(">i" .. i, max)) == max) + assert(unpack("I" .. i, pack(">I" .. i, umax)) == umax) +end + +-- Lua integer size +assert(unpack(">j", pack(">j", math.maxinteger)) == math.maxinteger) +assert(unpack("f", 24)) +end + +print "testing pack/unpack of floating-point numbers" + +for _, n in ipairs{0, -1.1, 1.9, 1/0, -1/0, 1e20, -1e20, 0.1, 2000.7} do + assert(unpack("n", pack("n", n)) == n) + assert(unpack("n", pack(">n", n)) == n) + assert(pack("f", n):reverse()) + assert(pack(">d", n) == pack("f", pack(">f", n)) == n) + assert(unpack("d", pack(">d", n)) == n) +end + +print "testing pack/unpack of strings" +do + local s = string.rep("abc", 1000) + assert(pack("zB", s, 247) == s .. "\0\xF7") + local s1, b = unpack("zB", s .. "\0\xF9") + assert(b == 249 and s1 == s) + s1 = pack("s", s) + assert(unpack("s", s1) == s) + + checkerror("does not fit", pack, "s1", s) + + checkerror("contains zeros", pack, "z", "alo\0"); + + checkerror("unfinished string", unpack, "zc10000000", "alo") + + for i = 2, NB do + local s1 = pack("s" .. i, s) + assert(unpack("s" .. i, s1) == s and #s1 == #s + i) + end +end + +do + local x = pack("s", "alo") + checkerror("too short", unpack, "s", x:sub(1, -2)) + checkerror("too short", unpack, "c5", "abcd") + checkerror("out of limits", pack, "s100", "alo") +end + +do + assert(pack("c0", "") == "") + assert(packsize("c0") == 0) + assert(unpack("c0", "") == "") + assert(pack("!4 c6", "abcdef") == "abcdef") + assert(pack("c3", "123") == "123") + assert(pack("c0", "") == "") + assert(pack("c8", "123456") == "123456\0\0") + assert(pack("c88", "") == string.rep("\0", 88)) + assert(pack("c188", "ab") == "ab" .. string.rep("\0", 188 - 2)) + local a, b, c = unpack("!4 z c3", "abcdefghi\0xyz") + assert(a == "abcdefghi" and b == "xyz" and c == 14) + checkerror("longer than", pack, "c3", "1234") +end + + +-- testing multiple types and sequence +do + local x = pack("!8 b Xh i4 i8 c1 Xi8", -12, 100, 200, "\xEC") + assert(#x == packsize(">!8 b Xh i4 i8 c1 Xi8")) + assert(x == "\xf4" .. "\0\0\0" .. + "\0\0\0\100" .. + "\0\0\0\0\0\0\0\xC8" .. + "\xEC" .. "\0\0\0\0\0\0\0") + local a, b, c, d, pos = unpack(">!8 c1 Xh i4 i8 b Xi8 XI XH", x) + assert(a == "\xF4" and b == 100 and c == 200 and d == -20 and (pos - 1) == #x) + + x = pack(">!4 c3 c4 c2 z i4 c5 c2 Xi4", + "abc", "abcd", "xz", "hello", 5, "world", "xy") + assert(x == "abcabcdxzhello\0\0\0\0\0\5worldxy\0") + local a, b, c, d, e, f, g, pos = unpack(">!4 c3 c4 c2 z i4 c5 c2 Xh Xi4", x) + assert(a == "abc" and b == "abcd" and c == "xz" and d == "hello" and + e == 5 and f == "world" and g == "xy" and (pos - 1) % 4 == 0) + + x = pack(" b b Xd b Xb x", 1, 2, 3) + assert(packsize(" b b Xd b Xb x") == 4) + assert(x == "\1\2\3\0") + a, b, c, pos = unpack("bbXdb", x) + assert(a == 1 and b == 2 and c == 3 and pos == #x) + + -- only alignment + assert(packsize("!8 xXi8") == 8) + local pos = unpack("!8 xXi8", "0123456701234567"); assert(pos == 9) + assert(packsize("!8 xXi2") == 2) + local pos = unpack("!8 xXi2", "0123456701234567"); assert(pos == 3) + assert(packsize("!2 xXi2") == 2) + local pos = unpack("!2 xXi2", "0123456701234567"); assert(pos == 3) + assert(packsize("!2 xXi8") == 2) + local pos = unpack("!2 xXi8", "0123456701234567"); assert(pos == 3) + assert(packsize("!16 xXi16") == 16) + local pos = unpack("!16 xXi16", "0123456701234567"); assert(pos == 17) + + checkerror("invalid next option", pack, "X") + checkerror("invalid next option", unpack, "XXi", "") + checkerror("invalid next option", unpack, "X i", "") + checkerror("invalid next option", pack, "Xc1") +end + +do -- testing initial position + local x = pack("i4i4i4i4", 1, 2, 3, 4) + for pos = 1, 16, 4 do + local i, p = unpack("i4", x, pos) + assert(i == pos//4 + 1 and p == pos + 4) + end + + -- with alignment + for pos = 0, 12 do -- will always round position to power of 2 + local i, p = unpack("!4 i4", x, pos + 1) + assert(i == (pos + 3)//4 + 1 and p == i*4 + 1) + end + + -- negative indices + local i, p = unpack("!4 i4", x, -4) + assert(i == 4 and p == 17) + local i, p = unpack("!4 i4", x, -7) + assert(i == 4 and p == 17) + local i, p = unpack("!4 i4", x, -#x) + assert(i == 1 and p == 5) + + -- limits + for i = 1, #x + 1 do + assert(unpack("c0", x, i) == "") + end + checkerror("out of string", unpack, "c0", x, 0) + checkerror("out of string", unpack, "c0", x, #x + 2) + checkerror("out of string", unpack, "c0", x, -(#x + 1)) + +end + +print "OK" + diff --git a/testes/utf8.lua b/testes/utf8.lua new file mode 100644 index 0000000000..ebc190b75d --- /dev/null +++ b/testes/utf8.lua @@ -0,0 +1,210 @@ +-- $Id: utf8.lua,v 1.12 2016/11/07 13:11:28 roberto Exp $ +-- See Copyright Notice in file all.lua + +print "testing UTF-8 library" + +local utf8 = require'utf8' + + +local function checkerror (msg, f, ...) + local s, err = pcall(f, ...) + assert(not s and string.find(err, msg)) +end + + +local function len (s) + return #string.gsub(s, "[\x80-\xBF]", "") +end + + +local justone = "^" .. utf8.charpattern .. "$" + +-- 't' is the list of codepoints of 's' +local function checksyntax (s, t) + local ts = {"return '"} + for i = 1, #t do ts[i + 1] = string.format("\\u{%x}", t[i]) end + ts[#t + 2] = "'" + ts = table.concat(ts) + assert(assert(load(ts))() == s) +end + +assert(utf8.offset("alo", 5) == nil) +assert(utf8.offset("alo", -4) == nil) + +-- 't' is the list of codepoints of 's' +local function check (s, t) + local l = utf8.len(s) + assert(#t == l and len(s) == l) + assert(utf8.char(table.unpack(t)) == s) + + assert(utf8.offset(s, 0) == 1) + + checksyntax(s, t) + + local t1 = {utf8.codepoint(s, 1, -1)} + assert(#t == #t1) + for i = 1, #t do assert(t[i] == t1[i]) end + + for i = 1, l do + local pi = utf8.offset(s, i) -- position of i-th char + local pi1 = utf8.offset(s, 2, pi) -- position of next char + assert(string.find(string.sub(s, pi, pi1 - 1), justone)) + assert(utf8.offset(s, -1, pi1) == pi) + assert(utf8.offset(s, i - l - 1) == pi) + assert(pi1 - pi == #utf8.char(utf8.codepoint(s, pi))) + for j = pi, pi1 - 1 do + assert(utf8.offset(s, 0, j) == pi) + end + for j = pi + 1, pi1 - 1 do + assert(not utf8.len(s, j)) + end + assert(utf8.len(s, pi, pi) == 1) + assert(utf8.len(s, pi, pi1 - 1) == 1) + assert(utf8.len(s, pi) == l - i + 1) + assert(utf8.len(s, pi1) == l - i) + assert(utf8.len(s, 1, pi) == i) + end + + local i = 0 + for p, c in utf8.codes(s) do + i = i + 1 + assert(c == t[i] and p == utf8.offset(s, i)) + assert(utf8.codepoint(s, p) == c) + end + assert(i == #t) + + i = 0 + for p, c in utf8.codes(s) do + i = i + 1 + assert(c == t[i] and p == utf8.offset(s, i)) + end + assert(i == #t) + + i = 0 + for c in string.gmatch(s, utf8.charpattern) do + i = i + 1 + assert(c == utf8.char(t[i])) + end + assert(i == #t) + + for i = 1, l do + assert(utf8.offset(s, i) == utf8.offset(s, i - l - 1, #s + 1)) + end + +end + + +do -- error indication in utf8.len + local function check (s, p) + local a, b = utf8.len(s) + assert(not a and b == p) + end + check("abc\xE3def", 4) + check("汉字\x80", #("汉字") + 1) + check("\xF4\x9F\xBF", 1) + check("\xF4\x9F\xBF\xBF", 1) +end + +-- error in utf8.codes +checkerror("invalid UTF%-8 code", + function () + local s = "ab\xff" + for c in utf8.codes(s) do assert(c) end + end) + + +-- error in initial position for offset +checkerror("position out of range", utf8.offset, "abc", 1, 5) +checkerror("position out of range", utf8.offset, "abc", 1, -4) +checkerror("position out of range", utf8.offset, "", 1, 2) +checkerror("position out of range", utf8.offset, "", 1, -1) +checkerror("continuation byte", utf8.offset, "𦧺", 1, 2) +checkerror("continuation byte", utf8.offset, "𦧺", 1, 2) +checkerror("continuation byte", utf8.offset, "\x80", 1) + + + +local s = "hello World" +local t = {string.byte(s, 1, -1)} +for i = 1, utf8.len(s) do assert(t[i] == string.byte(s, i)) end +check(s, t) + +check("汉字/漢字", {27721, 23383, 47, 28450, 23383,}) + +do + local s = "áéí\128" + local t = {utf8.codepoint(s,1,#s - 1)} + assert(#t == 3 and t[1] == 225 and t[2] == 233 and t[3] == 237) + checkerror("invalid UTF%-8 code", utf8.codepoint, s, 1, #s) + checkerror("out of range", utf8.codepoint, s, #s + 1) + t = {utf8.codepoint(s, 4, 3)} + assert(#t == 0) + checkerror("out of range", utf8.codepoint, s, -(#s + 1), 1) + checkerror("out of range", utf8.codepoint, s, 1, #s + 1) +end + +assert(utf8.char() == "") +assert(utf8.char(97, 98, 99) == "abc") + +assert(utf8.codepoint(utf8.char(0x10FFFF)) == 0x10FFFF) + +checkerror("value out of range", utf8.char, 0x10FFFF + 1) + +local function invalid (s) + checkerror("invalid UTF%-8 code", utf8.codepoint, s) + assert(not utf8.len(s)) +end + +-- UTF-8 representation for 0x11ffff (value out of valid range) +invalid("\xF4\x9F\xBF\xBF") + +-- overlong sequences +invalid("\xC0\x80") -- zero +invalid("\xC1\xBF") -- 0x7F (should be coded in 1 byte) +invalid("\xE0\x9F\xBF") -- 0x7FF (should be coded in 2 bytes) +invalid("\xF0\x8F\xBF\xBF") -- 0xFFFF (should be coded in 3 bytes) + + +-- invalid bytes +invalid("\x80") -- continuation byte +invalid("\xBF") -- continuation byte +invalid("\xFE") -- invalid byte +invalid("\xFF") -- invalid byte + + +-- empty string +check("", {}) + +-- minimum and maximum values for each sequence size +s = "\0 \x7F\z + \xC2\x80 \xDF\xBF\z + \xE0\xA0\x80 \xEF\xBF\xBF\z + \xF0\x90\x80\x80 \xF4\x8F\xBF\xBF" +s = string.gsub(s, " ", "") +check(s, {0,0x7F, 0x80,0x7FF, 0x800,0xFFFF, 0x10000,0x10FFFF}) + +x = "日本語a-4\0éó" +check(x, {26085, 26412, 35486, 97, 45, 52, 0, 233, 243}) + + +-- Supplementary Characters +check("𣲷𠜎𠱓𡁻𠵼ab𠺢", + {0x23CB7, 0x2070E, 0x20C53, 0x2107B, 0x20D7C, 0x61, 0x62, 0x20EA2,}) + +check("𨳊𩶘𦧺𨳒𥄫𤓓\xF4\x8F\xBF\xBF", + {0x28CCA, 0x29D98, 0x269FA, 0x28CD2, 0x2512B, 0x244D3, 0x10ffff}) + + +local i = 0 +for p, c in string.gmatch(x, "()(" .. utf8.charpattern .. ")") do + i = i + 1 + assert(utf8.offset(x, i) == p) + assert(utf8.len(x, p) == utf8.len(x) - i + 1) + assert(utf8.len(c) == 1) + for j = 1, #c - 1 do + assert(utf8.offset(x, 0, p + j - 1) == p) + end +end + +print'ok' + diff --git a/testes/vararg.lua b/testes/vararg.lua new file mode 100644 index 0000000000..4d5ce4abe8 --- /dev/null +++ b/testes/vararg.lua @@ -0,0 +1,151 @@ +-- $Id: vararg.lua,v 1.29 2018/03/12 14:19:36 roberto Exp $ +-- See Copyright Notice in file all.lua + +print('testing vararg') + +function f(a, ...) + local x = {n = select('#', ...), ...} + for i = 1, x.n do assert(a[i] == x[i]) end + return x.n +end + +function c12 (...) + assert(arg == _G.arg) -- no local 'arg' + local x = {...}; x.n = #x + local res = (x.n==2 and x[1] == 1 and x[2] == 2) + if res then res = 55 end + return res, 2 +end + +function vararg (...) return {n = select('#', ...), ...} end + +local call = function (f, args) return f(table.unpack(args, 1, args.n)) end + +assert(f() == 0) +assert(f({1,2,3}, 1, 2, 3) == 3) +assert(f({"alo", nil, 45, f, nil}, "alo", nil, 45, f, nil) == 5) + +assert(vararg().n == 0) +assert(vararg(nil, nil).n == 2) + +assert(c12(1,2)==55) +a,b = assert(call(c12, {1,2})) +assert(a == 55 and b == 2) +a = call(c12, {1,2;n=2}) +assert(a == 55 and b == 2) +a = call(c12, {1,2;n=1}) +assert(not a) +assert(c12(1,2,3) == false) +local a = vararg(call(next, {_G,nil;n=2})) +local b,c = next(_G) +assert(a[1] == b and a[2] == c and a.n == 2) +a = vararg(call(call, {c12, {1,2}})) +assert(a.n == 2 and a[1] == 55 and a[2] == 2) +a = call(print, {'+'}) +assert(a == nil) + +local t = {1, 10} +function t:f (...) local arg = {...}; return self[...]+#arg end +assert(t:f(1,4) == 3 and t:f(2) == 11) +print('+') + +lim = 20 +local i, a = 1, {} +while i <= lim do a[i] = i+0.3; i=i+1 end + +function f(a, b, c, d, ...) + local more = {...} + assert(a == 1.3 and more[1] == 5.3 and + more[lim-4] == lim+0.3 and not more[lim-3]) +end + +function g(a,b,c) + assert(a == 1.3 and b == 2.3 and c == 3.3) +end + +call(f, a) +call(g, a) + +a = {} +i = 1 +while i <= lim do a[i] = i; i=i+1 end +assert(call(math.max, a) == lim) + +print("+") + + +-- new-style varargs + +function oneless (a, ...) return ... end + +function f (n, a, ...) + local b + assert(arg == _G.arg) -- no local 'arg' + if n == 0 then + local b, c, d = ... + return a, b, c, d, oneless(oneless(oneless(...))) + else + n, b, a = n-1, ..., a + assert(b == ...) + return f(n, a, ...) + end +end + +a,b,c,d,e = assert(f(10,5,4,3,2,1)) +assert(a==5 and b==4 and c==3 and d==2 and e==1) + +a,b,c,d,e = f(4) +assert(a==nil and b==nil and c==nil and d==nil and e==nil) + + +-- varargs for main chunks +f = load[[ return {...} ]] +x = f(2,3) +assert(x[1] == 2 and x[2] == 3 and x[3] == undef) + + +f = load[[ + local x = {...} + for i=1,select('#', ...) do assert(x[i] == select(i, ...)) end + assert(x[select('#', ...)+1] == undef) + return true +]] + +assert(f("a", "b", nil, {}, assert)) +assert(f()) + +a = {select(3, table.unpack{10,20,30,40})} +assert(#a == 2 and a[1] == 30 and a[2] == 40) +a = {select(1)} +assert(next(a) == nil) +a = {select(-1, 3, 5, 7)} +assert(a[1] == 7 and a[2] == undef) +a = {select(-2, 3, 5, 7)} +assert(a[1] == 5 and a[2] == 7 and a[3] == undef) +pcall(select, 10000) +pcall(select, -10000) + + +-- bug in 5.2.2 + +function f(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, +p11, p12, p13, p14, p15, p16, p17, p18, p19, p20, +p21, p22, p23, p24, p25, p26, p27, p28, p29, p30, +p31, p32, p33, p34, p35, p36, p37, p38, p39, p40, +p41, p42, p43, p44, p45, p46, p48, p49, p50, ...) + local a1,a2,a3,a4,a5,a6,a7 + local a8,a9,a10,a11,a12,a13,a14 +end + +-- assertion fail here +f() + +-- missing arguments in tail call +do + local function f(a,b,c) return c, b end + local function g() return f(1,2) end + local a, b = g() + assert(a == nil and b == 2) +end +print('OK') + diff --git a/testes/verybig.lua b/testes/verybig.lua new file mode 100644 index 0000000000..5b29dea7ae --- /dev/null +++ b/testes/verybig.lua @@ -0,0 +1,152 @@ +-- $Id: verybig.lua,v 1.27 2018/03/09 14:23:48 roberto Exp $ +-- See Copyright Notice in file all.lua + +print "testing RK" + +-- testing opcodes with RK arguments larger than K limit +local function foo () + local dummy = { + -- fill first 256 entries in table of constants + 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, + 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, + 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, + 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, + 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, + 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, + 97, 98, 99, 100, 101, 102, 103, 104, + 105, 106, 107, 108, 109, 110, 111, 112, + 113, 114, 115, 116, 117, 118, 119, 120, + 121, 122, 123, 124, 125, 126, 127, 128, + 129, 130, 131, 132, 133, 134, 135, 136, + 137, 138, 139, 140, 141, 142, 143, 144, + 145, 146, 147, 148, 149, 150, 151, 152, + 153, 154, 155, 156, 157, 158, 159, 160, + 161, 162, 163, 164, 165, 166, 167, 168, + 169, 170, 171, 172, 173, 174, 175, 176, + 177, 178, 179, 180, 181, 182, 183, 184, + 185, 186, 187, 188, 189, 190, 191, 192, + 193, 194, 195, 196, 197, 198, 199, 200, + 201, 202, 203, 204, 205, 206, 207, 208, + 209, 210, 211, 212, 213, 214, 215, 216, + 217, 218, 219, 220, 221, 222, 223, 224, + 225, 226, 227, 228, 229, 230, 231, 232, + 233, 234, 235, 236, 237, 238, 239, 240, + 241, 242, 243, 244, 245, 246, 247, 248, + 249, 250, 251, 252, 253, 254, 255, 256, + } + assert(24.5 + 0.6 == 25.1) + local t = {foo = function (self, x) return x + self.x end, x = 10} + t.t = t + assert(t:foo(1.5) == 11.5) + assert(t.t:foo(0.5) == 10.5) -- bug in 5.2 alpha + assert(24.3 == 24.3) + assert((function () return t.x end)() == 10) +end + + +foo() +foo = nil + +if _soft then return 10 end + +print "testing large programs (>64k)" + +-- template to create a very big test file +prog = [[$ + +local a,b + +b = {$1$ + b30009 = 65534, + b30010 = 65535, + b30011 = 65536, + b30012 = 65537, + b30013 = 16777214, + b30014 = 16777215, + b30015 = 16777216, + b30016 = 16777217, + b30017 = 0x7fffff, + b30018 = -0x7fffff, + b30019 = 0x1ffffff, + b30020 = -0x1ffffd, + b30021 = -65534, + b30022 = -65535, + b30023 = -65536, + b30024 = -0xffffff, + b30025 = 15012.5, + $2$ +}; + +assert(b.a50008 == 25004 and b["a11"] == -5.5) +assert(b.a33007 == -16503.5 and b.a50009 == -25004.5) +assert(b["b"..30024] == -0xffffff) + +function b:xxx (a,b) return a+b end +assert(b:xxx(10, 12) == 22) -- pushself with non-constant index +b["xxx"] = undef + +s = 0; n=0 +for a,b in pairs(b) do s=s+b; n=n+1 end +-- with 32-bit floats, exact value of 's' depends on summation order +assert(81800000.0 < s and s < 81860000 and n == 70001) + +a = nil; b = nil +print'+' + +function f(x) b=x end + +a = f{$3$} or 10 + +assert(a==10) +assert(b[1] == "a10" and b[2] == 5 and b[#b-1] == "a50009") + + +function xxxx (x) return b[x] end + +assert(xxxx(3) == "a11") + +a = nil; b=nil +xxxx = nil + +return 10 + +]] + +-- functions to fill in the $n$ + +local function sig (x) + return (x % 2 == 0) and '' or '-' +end + +F = { +function () -- $1$ + for i=10,50009 do + io.write('a', i, ' = ', sig(i), 5+((i-10)/2), ',\n') + end +end, + +function () -- $2$ + for i=30026,50009 do + io.write('b', i, ' = ', sig(i), 15013+((i-30026)/2), ',\n') + end +end, + +function () -- $3$ + for i=10,50009 do + io.write('"a', i, '", ', sig(i), 5+((i-10)/2), ',\n') + end +end, +} + +file = os.tmpname() +io.output(file) +for s in string.gmatch(prog, "$([^$]+)") do + local n = tonumber(s) + if not n then io.write(s) else F[n]() end +end +io.close() +result = dofile(file) +assert(os.remove(file)) +print'OK' +return result + From 06e08c6d05b1346df935634be395f4d86035e3ea Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 9 Jul 2018 12:41:24 -0300 Subject: [PATCH 0306/1145] Fixed bug in OP_IDIVI Opocode was using 'luai_numdiv' (float division) instead of 'luai_numidiv' (integer division). --- lvm.c | 4 ++-- testes/math.lua | 13 ++++++++++++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/lvm.c b/lvm.c index 9e8bec0c58..4e4ef270e4 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.358 2018/06/15 14:14:20 roberto Exp roberto $ +** $Id: lvm.c,v 2.359 2018/06/18 17:58:21 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -1182,7 +1182,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { } else if (tonumberns(rb, nb)) { lua_Number nc = cast_num(ic); - setfltvalue(s2v(ra), luai_numdiv(L, nb, nc)); + setfltvalue(s2v(ra), luai_numidiv(L, nb, nc)); } else Protect(luaT_trybiniTM(L, rb, ic, 0, ra, TM_IDIV)); diff --git a/testes/math.lua b/testes/math.lua index 66998460d0..e5c9d8ec5f 100644 --- a/testes/math.lua +++ b/testes/math.lua @@ -1,4 +1,4 @@ --- $Id: math.lua,v 1.86 2018/05/09 14:55:52 roberto Exp $ +-- $Id: math.lua,v 1.86 2018/05/09 14:55:52 roberto Exp roberto $ -- See Copyright Notice in file all.lua print("testing numbers and math lib") @@ -139,6 +139,17 @@ assert(-1 // 0.0 == -1/0) assert(eqT(3.5 // 1.5, 2.0)) assert(eqT(3.5 // -1.5, -3.0)) +do -- tests for different kinds of opcodes + local x, y + x = 1; assert(x // 0.0 == 1/0) + x = 1.0; assert(x // 0 == 1/0) + x = 3.5; assert(eqT(x // 1, 3.0)) + assert(eqT(x // -1, -4.0)) + + x = 3.5; y = 1.5; assert(eqT(x // y, 2.0)) + x = 3.5; y = -1.5; assert(eqT(x // y, -3.0)) +end + assert(maxint // maxint == 1) assert(maxint // 1 == maxint) assert((maxint - 1) // maxint == 0) From b08c9079c5ab026f07942fb898f791325d270177 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 9 Jul 2018 12:50:51 -0300 Subject: [PATCH 0307/1145] Opcode names moved to a new header file The array with the names of the opcodes was moved to a header file ('lopnames.h'), as it is not used by the Lua kernel. Files that need that array ('luac.c' and 'ltests.c') include the header file to get a private (static) copy. --- lopcodes.c | 88 +------------------------------------------------- lopcodes.h | 5 +-- lopnames.h | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ ltests.c | 7 ++-- 4 files changed, 100 insertions(+), 94 deletions(-) create mode 100644 lopnames.h diff --git a/lopcodes.c b/lopcodes.c index 823d348592..1a3b0e6af8 100644 --- a/lopcodes.c +++ b/lopcodes.c @@ -1,5 +1,5 @@ /* -** $Id: lopcodes.c,v 1.81 2018/04/04 14:23:41 roberto Exp roberto $ +** $Id: lopcodes.c,v 1.83 2018/06/26 18:00:55 roberto Exp $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ @@ -17,92 +17,6 @@ /* ORDER OP */ -#if defined(LUAI_DEFOPNAMES) - -LUAI_DDEF const char *const luaP_opnames[NUM_OPCODES+1] = { - "MOVE", - "LOADI", - "LOADF", - "LOADK", - "LOADKX", - "LOADBOOL", - "LOADNIL", - "GETUPVAL", - "SETUPVAL", - "GETTABUP", - "GETTABLE", - "GETI", - "GETFIELD", - "SETTABUP", - "SETTABLE", - "SETI", - "SETFIELD", - "NEWTABLE", - "SELF", - "ADDI", - "SUBI", - "MULI", - "MODI", - "POWI", - "DIVI", - "IDIVI", - "BANDK", - "BORK", - "BXORK", - "SHRI", - "SHLI", - "ADD", - "SUB", - "MUL", - "MOD", - "POW", - "DIV", - "IDIV", - "BAND", - "BOR", - "BXOR", - "SHL", - "SHR", - "UNM", - "BNOT", - "NOT", - "LEN", - "CONCAT", - "CLOSE", - "JMP", - "EQ", - "LT", - "LE", - "EQK", - "EQI", - "LTI", - "LEI", - "GTI", - "GEI", - "TEST", - "TESTSET", - "CALL", - "TAILCALL", - "RETURN", - "RETURN0", - "RETURN1", - "FORLOOP1", - "FORPREP1", - "FORLOOP", - "FORPREP", - "TFORCALL", - "TFORLOOP", - "SETLIST", - "CLOSURE", - "VARARG", - "PREPVARARG", - "EXTRAARG", - NULL -}; - -#endif - - LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = { /* OT IT T A mode opcode */ opmode(0, 0, 0, 1, iABC) /* OP_MOVE */ diff --git a/lopcodes.h b/lopcodes.h index 821bb196de..d7b5dfe013 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -1,5 +1,5 @@ /* -** $Id: lopcodes.h,v 1.192 2018/06/08 19:07:27 roberto Exp roberto $ +** $Id: lopcodes.h,v 1.194 2018/06/26 18:00:55 roberto Exp $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ @@ -358,9 +358,6 @@ LUAI_DDEC(const lu_byte luaP_opmodes[NUM_OPCODES];) #define opmode(ot,it,t,a,m) (((ot)<<6) | ((it)<<5) | ((t)<<4) | ((a)<<3) | (m)) -LUAI_DDEC(const char *const luaP_opnames[NUM_OPCODES+1];) /* opcode names */ - - /* number of list items to accumulate before a SETLIST instruction */ #define LFIELDS_PER_FLUSH 50 diff --git a/lopnames.h b/lopnames.h new file mode 100644 index 0000000000..ab434b5979 --- /dev/null +++ b/lopnames.h @@ -0,0 +1,94 @@ +/* +** $Id: lopnames.h,v 1.1 2018/06/26 18:00:55 roberto Exp $ +** Opcode names +** See Copyright Notice in lua.h +*/ + +#if !defined(lopnames_h) +#define lopnames_h + +/* ORDER OP */ + +static const char *const opnames[] = { + "MOVE", + "LOADI", + "LOADF", + "LOADK", + "LOADKX", + "LOADBOOL", + "LOADNIL", + "GETUPVAL", + "SETUPVAL", + "GETTABUP", + "GETTABLE", + "GETI", + "GETFIELD", + "SETTABUP", + "SETTABLE", + "SETI", + "SETFIELD", + "NEWTABLE", + "SELF", + "ADDI", + "SUBI", + "MULI", + "MODI", + "POWI", + "DIVI", + "IDIVI", + "BANDK", + "BORK", + "BXORK", + "SHRI", + "SHLI", + "ADD", + "SUB", + "MUL", + "MOD", + "POW", + "DIV", + "IDIV", + "BAND", + "BOR", + "BXOR", + "SHL", + "SHR", + "UNM", + "BNOT", + "NOT", + "LEN", + "CONCAT", + "CLOSE", + "JMP", + "EQ", + "LT", + "LE", + "EQK", + "EQI", + "LTI", + "LEI", + "GTI", + "GEI", + "TEST", + "TESTSET", + "CALL", + "TAILCALL", + "RETURN", + "RETURN0", + "RETURN1", + "FORLOOP1", + "FORPREP1", + "FORLOOP", + "FORPREP", + "TFORCALL", + "TFORLOOP", + "SETLIST", + "CLOSURE", + "VARARG", + "PREPVARARG", + "EXTRAARG", + NULL +}; + +#endif + diff --git a/ltests.c b/ltests.c index 82d6463a04..ac548a95c3 100644 --- a/ltests.c +++ b/ltests.c @@ -1,5 +1,5 @@ /* -** $Id: ltests.c,v 2.244 2018/06/11 14:19:50 roberto Exp roberto $ +** $Id: ltests.c,v 2.246 2018/06/26 18:00:55 roberto Exp roberto $ ** Internal Module for Debugging of the Lua Implementation ** See Copyright Notice in lua.h */ @@ -27,6 +27,7 @@ #include "lfunc.h" #include "lmem.h" #include "lopcodes.h" +#include "lopnames.h" #include "lstate.h" #include "lstring.h" #include "ltable.h" @@ -523,7 +524,7 @@ int lua_checkmemory (lua_State *L) { static char *buildop (Proto *p, int pc, char *buff) { Instruction i = p->code[pc]; OpCode o = GET_OPCODE(i); - const char *name = luaP_opnames[o]; + const char *name = opnames[o]; int line = luaG_getfuncline(p, pc); sprintf(buff, "(%4d) %4d - ", line, pc); switch (getOpMode(o)) { @@ -599,7 +600,7 @@ static int printcode (lua_State *L) { printf("numparams: %d\n", p->numparams); for (pc=0; pcsizecode; pc++) { char buff[100]; - printf("%d\t%s\n", pc + 1, buildop(p, pc, buff)); + printf("%s\n", buildop(p, pc, buff)); } return 0; } From de2caf7ee4dfc7d641d00bf1d74b261482993b2d Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 9 Jul 2018 12:54:51 -0300 Subject: [PATCH 0308/1145] Bit-library file removed from the project (as it was deprecated) This commit only removed the file 'lbitlib.c' from the project; the makefile already was not using it. --- lbitlib.c | 7 ------- 1 file changed, 7 deletions(-) delete mode 100644 lbitlib.c diff --git a/lbitlib.c b/lbitlib.c deleted file mode 100644 index b9c33c6511..0000000000 --- a/lbitlib.c +++ /dev/null @@ -1,7 +0,0 @@ -/* -** $Id: lbitlib.c,v 1.31 2017/11/16 13:19:06 roberto Exp roberto $ -** Standard library for bitwise operations -** See Copyright Notice in lua.h -*/ - -Deprecated module. From ccf6d098f6a46053ef671b42100e68f12352c5ec Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 9 Jul 2018 13:29:08 -0300 Subject: [PATCH 0309/1145] 'searchpath' creates less temporary strings When creating error messages, package loaders may create dozens of temporary strings (one or more for each tried template). This change reduces the number of these strings, and avoid creating some of them if the search is successful. --- loadlib.c | 52 ++++++++++++++++++++++++++++++++++------------------ 1 file changed, 34 insertions(+), 18 deletions(-) diff --git a/loadlib.c b/loadlib.c index a5e10e4506..8c8ab8c586 100644 --- a/loadlib.c +++ b/loadlib.c @@ -1,5 +1,5 @@ /* -** $Id: loadlib.c,v 1.131 2017/12/13 12:51:42 roberto Exp roberto $ +** $Id: loadlib.c,v 1.133 2018/07/06 13:38:38 roberto Exp $ ** Dynamic library loader for Lua ** See Copyright Notice in lua.h ** @@ -421,14 +421,33 @@ static int readable (const char *filename) { } -static const char *pushnexttemplate (lua_State *L, const char *path) { +static const char *pushnextfilename (lua_State *L, const char *path) { const char *l; - while (*path == *LUA_PATH_SEP) path++; /* skip separators */ - if (*path == '\0') return NULL; /* no more templates */ + if (*path == *LUA_PATH_SEP) + path++; /* skip separator */ + if (*path == '\0') + return NULL; /* no more names */ l = strchr(path, *LUA_PATH_SEP); /* find next separator */ - if (l == NULL) l = path + strlen(path); - lua_pushlstring(L, path, l - path); /* template */ - return l; + if (l == NULL) /* no more separators? */ + l = path + strlen(path); /* go until the end */ + lua_pushlstring(L, path, l - path); /* file name */ + return l; /* rest of the path */ +} + + +/* +** Given a path such as ";blabla.so;blublu.so", pushes the string +** +** no file 'blabla.so' +** no file 'blublu.so' +*/ +static void pusherrornotfound (lua_State *L, const char *path) { + if (*path == *LUA_PATH_SEP) + path++; /* skip separator */ + lua_pushstring(L, "\n\tno file '"); + luaL_gsub(L, path, LUA_PATH_SEP, "'\n\tno file '"); + lua_pushstring(L, "'"); + lua_concat(L, 3); } @@ -436,21 +455,18 @@ static const char *searchpath (lua_State *L, const char *name, const char *path, const char *sep, const char *dirsep) { - luaL_Buffer msg; /* to build error message */ - if (*sep != '\0') /* non-empty separator? */ + /* separator is non-empty and appears in 'name'? */ + if (*sep != '\0' && strchr(name, *sep) != NULL) name = luaL_gsub(L, name, sep, dirsep); /* replace it by 'dirsep' */ - luaL_buffinit(L, &msg); - while ((path = pushnexttemplate(L, path)) != NULL) { - const char *filename = luaL_gsub(L, lua_tostring(L, -1), - LUA_PATH_MARK, name); - lua_remove(L, -2); /* remove path template */ + /* replace marks ('?') in 'path' by the file name */ + path = luaL_gsub(L, path, LUA_PATH_MARK, name); + while ((path = pushnextfilename(L, path)) != NULL) { + const char *filename = lua_tostring(L, -1); if (readable(filename)) /* does file exist and is readable? */ return filename; /* return that file name */ - lua_pushfstring(L, "\n\tno file '%s'", filename); - lua_remove(L, -2); /* remove file name */ - luaL_addvalue(&msg); /* concatenate error msg. entry */ + lua_pop(L, 1); /* else remove file name */ } - luaL_pushresult(&msg); /* create error message */ + pusherrornotfound(L, lua_tostring(L, -1)); /* create error message */ return NULL; /* not found */ } From 626cf0581bc214722033d61c69d5db9e51e53465 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 9 Jul 2018 14:22:09 -0300 Subject: [PATCH 0310/1145] Generational mode may wait longer after a major collection When Lua is building large long-duration structures, frequent small minor collections just waste time. Trying to avoid this, the collector will do a larger pause after a major collection when it does not collect enough garbage (which is a hint that memory is being used for long-lasting objects). --- lgc.c | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/lgc.c b/lgc.c index b0e3c363a2..fb02f015a2 100644 --- a/lgc.c +++ b/lgc.c @@ -1,5 +1,5 @@ /* -** $Id: lgc.c,v 2.253 2018/03/16 14:22:09 roberto Exp roberto $ +** $Id$ ** Garbage Collector ** See Copyright Notice in lua.h */ @@ -12,6 +12,7 @@ #include #include + #include "lua.h" #include "ldebug.h" @@ -1004,6 +1005,8 @@ void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt) { ** ======================================================= */ +static void setpause (global_State *g); + /* mask to erase all color bits, not changing gen-related stuff */ #define maskgencolors (~(bitmask(BLACKBIT) | WHITEBITS)) @@ -1275,21 +1278,35 @@ static void fullgen (lua_State *L, global_State *g) { ** than last major collection (kept in 'g->GCestimate'), does a major ** collection. Otherwise, does a minor collection and set debt to make ** another collection when memory grows 'genminormul'% larger. +** When it does a major collection, it then checks whether it could +** reclaim at least ?? memory. If not, it sets a long pause for the +** next collection. (Therefore, the next collection will be a major +** one, too.) ** 'GCdebt <= 0' means an explicit call to GC step with "size" zero; ** in that case, always do a minor collection. */ static void genstep (lua_State *L, global_State *g) { - lu_mem majorbase = g->GCestimate; - int majormul = getgcparam(g->genmajormul); - if (g->GCdebt > 0 && - gettotalbytes(g) > (majorbase / 100) * (100 + majormul)) { + lu_mem majorbase = g->GCestimate; /* memory after last major collection */ + lu_mem majorinc = (majorbase / 100) * getgcparam(g->genmajormul); + lu_mem memnew = gettotalbytes(g); + if (g->GCdebt > 0 && memnew > majorbase + majorinc) { fullgen(L, g); + memnew = gettotalbytes(g); + if (memnew < majorbase + (majorinc / 2)) { + /* collected at least half of memory growth since last major + collection; go back to minor collections */ + luaE_setdebt(g, -(cast(l_mem, (memnew / 100)) * g->genminormul)); + } + else { + /* memory seems to be growing; do a long wait for next (major) + collection */ + setpause(g); + } } else { - lu_mem mem; youngcollection(L, g); - mem = gettotalbytes(g); - luaE_setdebt(g, -(cast(l_mem, (mem / 100)) * g->genminormul)); + memnew = gettotalbytes(g); + luaE_setdebt(g, -(cast(l_mem, (memnew / 100)) * g->genminormul)); g->GCestimate = majorbase; /* preserve base value */ } } From 21f663d29fd46b0ebf2bbc3f039ed31bef2e29d4 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 10 Jul 2018 13:40:30 -0300 Subject: [PATCH 0311/1145] Added missing $Id$ to file 'ljumptab.h' --- ljumptab.h | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/ljumptab.h b/ljumptab.h index c642c23e1f..01d2c11e5a 100644 --- a/ljumptab.h +++ b/ljumptab.h @@ -1,3 +1,10 @@ +/* +** $Id: ljumptab.h 2018/07/10 13:40:16 $ +** Jump Table for the Lua interpreter +** See Copyright Notice in lua.h +*/ + + #undef vmdispatch #undef vmcase #undef vmbreak From 941b189d98c7019c1be093bb0b709d90771b72d9 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 10 Jul 2018 13:48:19 -0300 Subject: [PATCH 0312/1145] Improvements in the manual - More precise use of 'argument' x 'parameter'. - Clarification about what the lexer considers 'letter', 'space', and 'digit'. --- manual/manual.of | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/manual/manual.of b/manual/manual.of index 935990d0ed..f92be508f9 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -1,4 +1,4 @@ -@Ci{$Id: manual.of,v 1.175 2018/06/18 19:17:35 roberto Exp $} +@Ci{$Id: manual.of,v 1.175 2018/06/18 19:17:35 roberto Exp roberto $} @C{[(-------------------------------------------------------------------------} @manual{ @@ -925,14 +925,17 @@ at the end of this manual. @sect2{lexical| @title{Lexical Conventions} Lua is a @x{free-form} language. -It ignores spaces (including new lines) and comments -between lexical elements (@x{tokens}), +It ignores spaces and comments between lexical elements (@x{tokens}), except as delimiters between @x{names} and @x{keywords}. +In source code, +Lua recognizes as spaces the standard ASCII white-space +characters space, form feed, newline, +carriage return, horizontal tab, and vertical tab. @def{Names} (also called @def{identifiers}) -in Lua can be any string of letters, -digits, and underscores, +in Lua can be any string of Latin letters, +Arabic-Indic digits, and underscores, not beginning with a digit and not being a reserved word. Identifiers are used to name variables, table fields, and labels. @@ -2436,7 +2439,7 @@ it can do whatever it wants on that Lua state, as it should be already protected. However, when C code operates on other Lua states -(e.g., a Lua parameter to the function, +(e.g., a Lua-state argument to the function, a Lua state stored in the registry, or the result of @Lid{lua_newthread}), it should use them only in API calls that cannot raise errors. @@ -5376,7 +5379,7 @@ In words, if the argument @id{arg} is nil or absent, the macro results in the default @id{dflt}. Otherwise, it results in the result of calling @id{func} with the state @id{L} and the argument index @id{arg} as -parameters. +arguments. Note that it evaluates the expression @id{dflt} only if needed. } @@ -6408,7 +6411,7 @@ Each entry in this table is a @def{searcher function}. When looking for a module, @Lid{require} calls each of these searchers in ascending order, with the module name (the argument given to @Lid{require}) as its -sole parameter. +sole argument. The function can return another function (the module @def{loader}) plus an extra value that will be passed to that loader, or a string explaining why it did not find that module @@ -7355,7 +7358,7 @@ Returns the arc sine of @id{x} (in radians). @index{atan2} Returns the arc tangent of @T{y/x} (in radians), -but uses the signs of both parameters to find the +but uses the signs of both arguments to find the quadrant of the result. (It also handles correctly the case of @id{x} being zero.) @@ -7596,7 +7599,7 @@ When called with a file name, it opens the named file (in text mode), and sets its handle as the default input file. When called with a file handle, it simply sets this file handle as the default input file. -When called without parameters, +When called without arguments, it returns the current default input file. In case of errors this function raises the error, @@ -7743,7 +7746,7 @@ the function returns a string or a number with the characters read, or @nil if it cannot read data with the specified format. (In this latter case, the function does not read subsequent formats.) -When called without parameters, +When called without arguments, it uses a default format that reads the next line (see below). @@ -8166,8 +8169,8 @@ The first parameter or local variable has @N{index 1}, and so on, following the order that they are declared in the code, counting only the variables that are active in the current scope of the function. -Negative indices refer to vararg parameters; -@num{-1} is the first vararg parameter. +Negative indices refer to vararg arguments; +@num{-1} is the first vararg argument. The function returns @nil if there is no variable with the given index, and raises an error when called with a level out of range. (You can call @Lid{debug.getinfo} to check whether the level is valid.) @@ -8418,7 +8421,7 @@ $ lua -e "print(arg[1])" } will print @St{-e}. If there is a script, -the script is called with parameters +the script is called with arguments @T{arg[1]}, @Cdots, @T{arg[#arg]}. (Like all chunks in Lua, the script is compiled as a vararg function.) From 9a825f6bb9a141023ac519a73f6a9958c113659e Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 10 Jul 2018 14:55:16 -0300 Subject: [PATCH 0313/1145] In tests of opcodes, avoid coercion in bitwise operation --- testes/code.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/testes/code.lua b/testes/code.lua index e39c62adc6..1a01c4a22e 100644 --- a/testes/code.lua +++ b/testes/code.lua @@ -239,7 +239,7 @@ checkI(function () return ~~-1024.0 end, -1024) checkI(function () return ((100 << 6) << -4) >> 2 end, 100) -- borders around MAXARG_sBx ((((1 << 17) - 1) >> 1) == 65535) -local sbx = ((1 << "17") - 1) >> 1 -- avoid folding +local a = 17; local sbx = ((1 << a) - 1) >> 1 -- avoid folding checkI(function () return 65535 end, sbx) checkI(function () return -65535 end, -sbx) checkI(function () return 65536 end, sbx + 1) From 4d5de1c1fb2decb39d74dfb092ca5643ce47176f Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 11 Jul 2018 12:53:23 -0300 Subject: [PATCH 0314/1145] Fixed bug in line info. when using 'not' operator When creating code for a jump on a 'not' condition, the code generator was removing an instruction (the OP_NOT) without adjusting its corresponding line information. This fix also added tests for this case and extra functionality in the test library to debug line info. structures. --- lcode.c | 76 +++++++++++++++++++++++++++++++++-------------- ltests.c | 22 +++++++++++++- testes/errors.lua | 21 +++++++++++-- 3 files changed, 93 insertions(+), 26 deletions(-) diff --git a/lcode.c b/lcode.c index ab91561cae..d00038dd7a 100644 --- a/lcode.c +++ b/lcode.c @@ -1,5 +1,5 @@ /* -** $Id: lcode.c,v 2.160 2018/03/16 14:22:09 roberto Exp roberto $ +** $Id: lcode.c $ ** Code generator for Lua ** See Copyright Notice in lua.h */ @@ -309,10 +309,19 @@ void luaK_patchclose (FuncState *fs, int list) { } +/* +** MAXimum number of successive Instructions WiTHout ABSolute line +** information. +*/ #if !defined(MAXIWTHABS) #define MAXIWTHABS 120 #endif + +/* limit for difference between lines in relative line info. */ +#define LIMLINEDIFF 0x80 + + /* ** Save line info for a new instruction. If difference from last line ** does not fit in a byte, of after that many instructions, save a new @@ -320,14 +329,15 @@ void luaK_patchclose (FuncState *fs, int list) { ** in 'lineinfo' signals the existence of this absolute information.) ** Otherwise, store the difference from last line in 'lineinfo'. */ -static void savelineinfo (FuncState *fs, Proto *f, int pc, int line) { +static void savelineinfo (FuncState *fs, Proto *f, int line) { int linedif = line - fs->previousline; - if (abs(linedif) >= 0x80 || fs->iwthabs++ > MAXIWTHABS) { + int pc = fs->pc - 1; /* last instruction coded */ + if (abs(linedif) >= LIMLINEDIFF || fs->iwthabs++ > MAXIWTHABS) { luaM_growvector(fs->ls->L, f->abslineinfo, fs->nabslineinfo, f->sizeabslineinfo, AbsLineInfo, MAX_INT, "lines"); f->abslineinfo[fs->nabslineinfo].pc = pc; f->abslineinfo[fs->nabslineinfo++].line = line; - linedif = ABSLINEINFO; /* signal there is absolute information */ + linedif = ABSLINEINFO; /* signal that there is absolute information */ fs->iwthabs = 0; /* restart counter */ } luaM_growvector(fs->ls->L, f->lineinfo, pc, f->sizelineinfo, ls_byte, @@ -337,6 +347,37 @@ static void savelineinfo (FuncState *fs, Proto *f, int pc, int line) { } +/* +** Remove line information from the last instruction. +** If line information for that instruction is absolute, set 'iwthabs' +** above its max to force the new (replacing) instruction to have +** absolute line info, too. +*/ +static void removelastlineinfo (FuncState *fs) { + Proto *f = fs->f; + int pc = fs->pc - 1; /* last instruction coded */ + if (f->lineinfo[pc] != ABSLINEINFO) { /* relative line info? */ + fs->previousline -= f->lineinfo[pc]; /* last line saved */ + fs->iwthabs--; + } + else { /* absolute line information */ + fs->nabslineinfo--; /* remove it */ + lua_assert(f->abslineinfo[fs->nabslineinfo].pc = pc); + fs->iwthabs = MAXIWTHABS + 1; /* force next line info to be absolute */ + } +} + + +/* +** Remove the last instruction created, correcting line information +** accordingly. +*/ +static void removelastinstruction (FuncState *fs) { + removelastlineinfo(fs); + fs->pc--; +} + + /* ** Emit instruction 'i', checking for array sizes and saving also its ** line information. Return 'i' position. @@ -346,9 +387,9 @@ static int luaK_code (FuncState *fs, Instruction i) { /* put new instruction in code array */ luaM_growvector(fs->ls->L, f->code, fs->pc, f->sizecode, Instruction, MAX_INT, "opcodes"); - f->code[fs->pc] = i; - savelineinfo(fs, f, fs->pc, fs->ls->lastline); - return fs->pc++; + f->code[fs->pc++] = i; + savelineinfo(fs, f, fs->ls->lastline); + return fs->pc - 1; /* index of new instruction */ } @@ -986,7 +1027,7 @@ static int jumponcond (FuncState *fs, expdesc *e, int cond) { if (e->k == VRELOC) { Instruction ie = getinstruction(fs, e); if (GET_OPCODE(ie) == OP_NOT) { - fs->pc--; /* remove previous OP_NOT */ + removelastinstruction(fs); /* remove previous OP_NOT */ return condjump(fs, OP_TEST, GETARG_B(ie), 0, !cond); } /* else go through */ @@ -1572,23 +1613,12 @@ void luaK_posfix (FuncState *fs, BinOpr opr, /* -** Change line information associated with current position. If that -** information is absolute, just change it and correct 'previousline'. -** Otherwise, restore 'previousline' to its value before saving the -** current position and than saves the line information again, with the -** new line. +** Change line information associated with current position, by removing +** previous info and adding it again with new line. */ void luaK_fixline (FuncState *fs, int line) { - Proto *f = fs->f; - if (f->lineinfo[fs->pc - 1] == ABSLINEINFO) { - lua_assert(f->abslineinfo[fs->nabslineinfo - 1].pc == fs->pc - 1); - f->abslineinfo[fs->nabslineinfo - 1].line = line; - fs->previousline = line; - } - else { - fs->previousline -= f->lineinfo[fs->pc - 1]; /* undo previous info. */ - savelineinfo(fs, f, fs->pc - 1, line); /* redo it */ - } + removelastlineinfo(fs); + savelineinfo(fs, fs->f, line); } diff --git a/ltests.c b/ltests.c index ac548a95c3..13717da9b3 100644 --- a/ltests.c +++ b/ltests.c @@ -526,7 +526,8 @@ static char *buildop (Proto *p, int pc, char *buff) { OpCode o = GET_OPCODE(i); const char *name = opnames[o]; int line = luaG_getfuncline(p, pc); - sprintf(buff, "(%4d) %4d - ", line, pc); + int lineinfo = (p->lineinfo != NULL) ? p->lineinfo[pc] : 0; + sprintf(buff, "(%2d - %4d) %4d - ", lineinfo, line, pc); switch (getOpMode(o)) { case iABC: sprintf(buff+strlen(buff), "%-12s%4d %4d %4d%s", name, @@ -621,6 +622,24 @@ static int listk (lua_State *L) { } +static int listabslineinfo (lua_State *L) { + Proto *p; + int i; + luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1), + 1, "Lua function expected"); + p = getproto(obj_at(L, 1)); + luaL_argcheck(L, p->abslineinfo != NULL, 1, "function has no debug info"); + lua_createtable(L, 2 * p->sizeabslineinfo, 0); + for (i=0; i < p->sizeabslineinfo; i++) { + lua_pushinteger(L, p->abslineinfo[i].pc); + lua_rawseti(L, -2, 2 * i + 1); + lua_pushinteger(L, p->abslineinfo[i].line); + lua_rawseti(L, -2, 2 * i + 2); + } + return 1; +} + + static int listlocals (lua_State *L) { Proto *p; int pc = cast_int(luaL_checkinteger(L, 2)) - 1; @@ -1682,6 +1701,7 @@ static const struct luaL_Reg tests_funcs[] = { {"listcode", listcode}, {"printcode", printcode}, {"listk", listk}, + {"listabslineinfo", listabslineinfo}, {"listlocals", listlocals}, {"loadlib", loadlib}, {"checkpanic", checkpanic}, diff --git a/testes/errors.lua b/testes/errors.lua index 63a7b740cd..19a7b6faad 100644 --- a/testes/errors.lua +++ b/testes/errors.lua @@ -1,4 +1,4 @@ --- $Id: errors.lua,v 1.97 2017/11/28 15:31:56 roberto Exp $ +-- $Id: errors.lua $ -- See Copyright Notice in file all.lua print("testing errors") @@ -299,7 +299,7 @@ end local function lineerror (s, l) local err,msg = pcall(load(s)) local line = string.match(msg, ":(%d+):") - assert((line and line+0) == l) + assert(tonumber(line) == l) end lineerror("local a\n for i=1,'a' do \n print(i) \n end", 2) @@ -350,6 +350,23 @@ X=1;lineerror((p), 2) X=2;lineerror((p), 1) +lineerror([[ +local b = false +if not b then + error 'test' +end]], 3) + +lineerror([[ +local b = false +if not b then + if not b then + if not b then + error 'test' + end + end +end]], 5) + + if not _soft then -- several tests that exaust the Lua stack collectgarbage() From 84058b15068a382e66f4eeb4e29a2dbf1704fa8b Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 11 Jul 2018 13:17:46 -0300 Subject: [PATCH 0315/1145] Added definition for LUA_VERSION_RELEASE_NUM LUA_VERSION_RELEASE_NUM is set to the release number of the Lua interpreter (e.g., 5.4.0 becomes the integer 50400). --- lua.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lua.h b/lua.h index 12d9e2ec18..03d92c0ba3 100644 --- a/lua.h +++ b/lua.h @@ -18,9 +18,11 @@ #define LUA_VERSION_MAJOR "5" #define LUA_VERSION_MINOR "4" -#define LUA_VERSION_NUM 504 #define LUA_VERSION_RELEASE "0" +#define LUA_VERSION_NUM 504 +#define LUA_VERSION_RELEASE_NUM (LUA_VERSION_NUM * 100 + 0) + #define LUA_VERSION "Lua " LUA_VERSION_MAJOR "." LUA_VERSION_MINOR #define LUA_RELEASE LUA_VERSION "." LUA_VERSION_RELEASE #define LUA_COPYRIGHT LUA_RELEASE " Copyright (C) 1994-2018 Lua.org, PUC-Rio" From 96f9643f330a4bf0f8cd1973fecdd7161ffbbf68 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 11 Jul 2018 16:11:50 -0300 Subject: [PATCH 0316/1145] Bug: wrong 'nCcalls' when resuming a coroutine The counter 'nCcalls' now includes the number of CallInfo structures pre-allocated (so that these "potential" C calls can be made without checking 'nCcalls'). So, when copying this value from a thread to another, in 'lua_resume', it must be corrected to the number of CallInfo structures in the thread being resumed. --- ldo.c | 9 ++++++--- lstate.h | 20 ++++++++++++++++++-- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/ldo.c b/ldo.c index fb077211fe..ebb35c971c 100644 --- a/ldo.c +++ b/ldo.c @@ -1,5 +1,5 @@ /* -** $Id: ldo.c,v 2.201 2018/05/22 12:02:36 roberto Exp roberto $ +** $Id: ldo.c $ ** Stack and Call structure of Lua ** See Copyright Notice in lua.h */ @@ -137,6 +137,7 @@ l_noret luaD_throw (lua_State *L, int errcode) { int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) { unsigned short oldnCcalls = L->nCcalls - L->nci; struct lua_longjmp lj; + lua_assert(L->nCcalls >= L->nci); lj.status = LUA_OK; lj.previous = L->errorJmp; /* chain new error handler */ L->errorJmp = &lj; @@ -653,7 +654,10 @@ LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs, } else if (L->status != LUA_YIELD) return resume_error(L, "cannot resume dead coroutine", nargs); - L->nCcalls = (from) ? from->nCcalls + 1 : 1; + if (from == NULL) + L->nCcalls = 1; + else /* correct 'nCcalls' for this thread */ + L->nCcalls = from->nCcalls - from->nci + L->nci + 1; if (L->nCcalls >= LUAI_MAXCCALLS) return resume_error(L, "C stack overflow", nargs); luai_userstateresume(L, nargs); @@ -677,7 +681,6 @@ LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs, *nresults = (status == LUA_YIELD) ? L->ci->u2.nyield : cast_int(L->top - (L->ci->func + 1)); L->nny = oldnny; /* restore 'nny' */ - L->nCcalls--; lua_unlock(L); return status; } diff --git a/lstate.h b/lstate.h index 0e173b5b00..2a36bd9632 100644 --- a/lstate.h +++ b/lstate.h @@ -1,5 +1,5 @@ /* -** $Id: lstate.h,v 2.159 2018/06/15 19:31:22 roberto Exp roberto $ +** $Id: lstate.h $ ** Global State ** See Copyright Notice in lua.h */ @@ -49,6 +49,22 @@ */ +/* + +** About 'nCcalls': each thread in Lua (a lua_State) keeps a count of +** how many "C calls" it has in the C stack, to avoid C-stack overflow. +** This count is very rough approximation; it considers only recursive +** functions inside the interpreter, as non-recursive calls can be +** considered using a fixed (although unknown) amount of stack space. +** +** The proper count also includes the number of CallInfo structures +** allocated by Lua, as a kind of "potential" calls. So, when Lua +** calls a function (and "consumes" one CallInfo), it needs neither to +** increment nor to check 'nCcalls', as its use of C stack is already +** accounted for. + +*/ + struct lua_longjmp; /* defined in ldo.c */ @@ -212,7 +228,7 @@ struct lua_State { int basehookcount; int hookcount; unsigned short nny; /* number of non-yieldable calls in stack */ - unsigned short nCcalls; /* number of nested C calls */ + unsigned short nCcalls; /* number of nested C calls + 'nny' */ l_signalT hookmask; lu_byte allowhook; }; From fb18346dddcb0400d0396111c56a817a8d4bd8bd Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 12 Jul 2018 15:56:44 -0300 Subject: [PATCH 0317/1145] Avoid using 'int' for UTF-8 values An 'int' may have only 16 bits, so it may not be big enough for UTF-8 values. The new type 'utfint' (in the utf8 library) ensures at least 21 bits for those values. --- lutf8lib.c | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/lutf8lib.c b/lutf8lib.c index 57bd59e01a..dc95b285c9 100644 --- a/lutf8lib.c +++ b/lutf8lib.c @@ -1,5 +1,5 @@ /* -** $Id: lutf8lib.c,v 1.16 2016/12/22 13:08:50 roberto Exp roberto $ +** $Id: lutf8lib.c $ ** Standard library for UTF-8 manipulation ** See Copyright Notice in lua.h */ @@ -20,8 +20,19 @@ #include "lauxlib.h" #include "lualib.h" + #define MAXUNICODE 0x10FFFF +/* +** Integer type for decoded UTF-8 values; MAXUNICODE needs 21 bits. +*/ +#if LUAI_BITSINT >= 21 +typedef unsigned int utfint; +#else +typedef unsigned long utfint; +#endif + + #define iscont(p) ((*(p) & 0xC0) == 0x80) @@ -37,11 +48,11 @@ static lua_Integer u_posrelat (lua_Integer pos, size_t len) { /* ** Decode one UTF-8 sequence, returning NULL if byte sequence is invalid. */ -static const char *utf8_decode (const char *o, int *val) { +static const char *utf8_decode (const char *o, utfint *val) { static const unsigned int limits[] = {0xFF, 0x7F, 0x7FF, 0xFFFF}; const unsigned char *s = (const unsigned char *)o; unsigned int c = s[0]; - unsigned int res = 0; /* final result */ + utfint res = 0; /* final result */ if (c < 0x80) /* ascii? */ res = c; else { @@ -53,7 +64,7 @@ static const char *utf8_decode (const char *o, int *val) { res = (res << 6) | (cc & 0x3F); /* add lower 6 bits from cont. byte */ c <<= 1; /* to test next bit */ } - res |= ((c & 0x7F) << (count * 5)); /* add first byte */ + res |= ((utfint)(c & 0x7F) << (count * 5)); /* add first byte */ if (count > 3 || res > MAXUNICODE || res <= limits[count]) return NULL; /* invalid byte sequence */ s += count; /* skip continuation bytes read */ @@ -69,8 +80,8 @@ static const char *utf8_decode (const char *o, int *val) { ** that interval */ static int utflen (lua_State *L) { - int n = 0; - size_t len; + lua_Integer n = 0; /* counter for the number of characters */ + size_t len; /* string length in bytes */ const char *s = luaL_checklstring(L, 1, &len); lua_Integer posi = u_posrelat(luaL_optinteger(L, 2, 1), len); lua_Integer posj = u_posrelat(luaL_optinteger(L, 3, -1), len); @@ -109,12 +120,12 @@ static int codepoint (lua_State *L) { if (posi > pose) return 0; /* empty interval; return no values */ if (pose - posi >= INT_MAX) /* (lua_Integer -> int) overflow? */ return luaL_error(L, "string slice too long"); - n = (int)(pose - posi) + 1; + n = (int)(pose - posi) + 1; /* upper bound for number of returns */ luaL_checkstack(L, n, "string slice too long"); - n = 0; - se = s + pose; + n = 0; /* count the number of returns */ + se = s + pose; /* string end */ for (s += posi - 1; s < se;) { - int code; + utfint code; s = utf8_decode(s, &code); if (s == NULL) return luaL_error(L, "invalid UTF-8 code"); @@ -211,7 +222,7 @@ static int iter_aux (lua_State *L) { if (n >= (lua_Integer)len) return 0; /* no more codepoints */ else { - int code; + utfint code; const char *next = utf8_decode(s + n, &code); if (next == NULL || iscont(next)) return luaL_error(L, "invalid UTF-8 code"); From 2e297d6ab37c1bb255b6984b91dd92d9080e02c9 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 13 Jul 2018 15:43:02 -0300 Subject: [PATCH 0318/1145] Fixed bug in generational collection of userdata During generational collection, a userdatum must become gray and go to a gray list after being traversed (like tables), so that 'correctgraylist' can handle it to its next stage. This commit also added minimum tests for the generational collector, including one that would detect this bug. --- lgc.c | 26 ++++++++------- testes/all.lua | 4 +-- testes/gc.lua | 4 +-- testes/gengc.lua | 83 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 102 insertions(+), 15 deletions(-) create mode 100644 testes/gengc.lua diff --git a/lgc.c b/lgc.c index fb02f015a2..c11a528036 100644 --- a/lgc.c +++ b/lgc.c @@ -1,5 +1,5 @@ /* -** $Id$ +** $Id: lgc.c $ ** Garbage Collector ** See Copyright Notice in lua.h */ @@ -212,7 +212,7 @@ void luaC_barrierback_ (lua_State *L, GCObject *o) { lua_assert(g->gckind != KGC_GEN || (isold(o) && getage(o) != G_TOUCHED1)); if (getage(o) != G_TOUCHED2) /* not already in gray list? */ linkobjgclist(o, g->grayagain); /* link it in 'grayagain' */ - black2gray(o); /* make table gray (again) */ + black2gray(o); /* make object gray (again) */ setage(o, G_TOUCHED1); /* touched in current cycle */ } @@ -515,6 +515,19 @@ static lu_mem traversetable (global_State *g, Table *h) { } +static int traverseudata (global_State *g, Udata *u) { + int i; + markobjectN(g, u->metatable); /* mark its metatable */ + for (i = 0; i < u->nuvalue; i++) + markvalue(g, &u->uv[i].uv); + if (g->gckind == KGC_GEN) { + linkgclist(u, g->grayagain); /* keep it in some gray list */ + black2gray(u); + } + return 1 + u->nuvalue; +} + + /* ** Check the cache of a prototype, to keep invariants. If the ** cache is white, clear it. (A cache should not prevent the @@ -613,15 +626,6 @@ static int traversethread (global_State *g, lua_State *th) { } -static int traverseudata (global_State *g, Udata *u) { - int i; - markobjectN(g, u->metatable); /* mark its metatable */ - for (i = 0; i < u->nuvalue; i++) - markvalue(g, &u->uv[i].uv); - return 1 + u->nuvalue; -} - - /* ** traverse one gray object, turning it to black (except for threads, ** which are always gray). diff --git a/testes/all.lua b/testes/all.lua index cfe2160397..3779625400 100755 --- a/testes/all.lua +++ b/testes/all.lua @@ -1,5 +1,5 @@ #!../lua --- $Id: all.lua,v 1.100 2018/03/09 14:23:48 roberto Exp $ +-- $Id: all.lua $ -- See Copyright Notice at the end of this file @@ -162,7 +162,7 @@ olddofile('strings.lua') olddofile('literals.lua') dofile('tpack.lua') assert(dofile('attrib.lua') == 27) - +dofile('gengc.lua') assert(dofile('locals.lua') == 5) dofile('constructs.lua') dofile('code.lua', true) diff --git a/testes/gc.lua b/testes/gc.lua index 9647cd542f..05072efd30 100644 --- a/testes/gc.lua +++ b/testes/gc.lua @@ -1,7 +1,7 @@ --- $Id: gc.lua,v 1.82 2018/03/12 14:19:36 roberto Exp $ +-- $Id: gc.lua $ -- See Copyright Notice in file all.lua -print('testing garbage collection') +print('testing incremental garbage collection') local debug = require"debug" diff --git a/testes/gengc.lua b/testes/gengc.lua new file mode 100644 index 0000000000..36aed806ef --- /dev/null +++ b/testes/gengc.lua @@ -0,0 +1,83 @@ +-- $Id: gengc.lua $ +-- See Copyright Notice in file all.lua + +print('testing generational garbage collection') + +local debug = require"debug" + +assert(collectgarbage("isrunning")) + +collectgarbage() + +local oldmode = collectgarbage("generational") + + +-- ensure that table barrier evolves correctly +do + local U = {} + -- full collection makes 'U' old + collectgarbage() + assert(not T or T.gcage(U) == "old") + + -- U refers to a new table, so it becomes 'touched1' + U[1] = {x = {234}} + assert(not T or (T.gcage(U) == "touched1" and T.gcage(U[1]) == "new")) + + -- both U and the table survive one more collection + collectgarbage("step", 0) + assert(not T or (T.gcage(U) == "touched2" and T.gcage(U[1]) == "survival")) + + -- both U and the table survive yet another collection + -- now everything is old + collectgarbage("step", 0) + assert(not T or (T.gcage(U) == "old" and T.gcage(U[1]) == "old1")) + + -- data was not corrupted + assert(U[1].x[1] == 234) +end + + +if T == nil then + (Message or print)('\n >>> testC not active: \z + skipping some generational tests <<<\n') + print 'OK' + return +end + + +-- ensure that userdata barrier evolves correctly +do + local U = T.newuserdata(0, 1) + -- full collection makes 'U' old + collectgarbage() + assert(T.gcage(U) == "old") + + -- U refers to a new table, so it becomes 'touched1' + debug.setuservalue(U, {x = {234}}) + assert(T.gcage(U) == "touched1" and + T.gcage(debug.getuservalue(U)) == "new") + + -- both U and the table survive one more collection + collectgarbage("step", 0) + assert(T.gcage(U) == "touched2" and + T.gcage(debug.getuservalue(U)) == "survival") + + -- both U and the table survive yet another collection + -- now everything is old + collectgarbage("step", 0) + assert(T.gcage(U) == "old" and + T.gcage(debug.getuservalue(U)) == "old1") + + -- data was not corrupted + assert(debug.getuservalue(U).x[1] == 234) +end + + + +-- just to make sure +assert(collectgarbage'isrunning') + +collectgarbage(oldmode) + +print('OK') + From ccae0f5aad11b448fa630a41b2c7c54e69d134d8 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 18 Jul 2018 11:43:45 -0300 Subject: [PATCH 0319/1145] Comments about OLD0/OLD1 ages Improved the comments in file 'lgc.c' explaining the roles of "ages" OLD0 and OLD1 in the generacional collector. --- lgc.c | 54 ++++++++++++++++++++++++++++++------------------------ 1 file changed, 30 insertions(+), 24 deletions(-) diff --git a/lgc.c b/lgc.c index c11a528036..e8429e1b38 100644 --- a/lgc.c +++ b/lgc.c @@ -181,9 +181,13 @@ static int iscleared (global_State *g, const GCObject *o) { /* ** barrier that moves collector forward, that is, mark the white object -** being pointed by a black object. (If in sweep phase, clear the black -** object to white [sweep it] to avoid other barrier calls for this -** same object.) +** 'v' being pointed by the black object 'o'. (If in sweep phase, clear +** the black object to white [sweep it] to avoid other barrier calls for +** this same object.) In the generational mode, 'v' must also become +** old, if 'o' is old; however, it cannot be changed directly to OLD, +** because it may still point to non-old objects. So, it is marked as +** OLD0. In the next cycle it will become OLD1, and in the next it +** will finally become OLD (regular old). */ void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v) { global_State *g = G(L); @@ -218,13 +222,14 @@ void luaC_barrierback_ (lua_State *L, GCObject *o) { /* -** Barrier for prototype's cache of closures. For an 'old1' -** object, making it gray stops it from being visited by 'markold', -** so it is linked in the 'grayagain' list to ensure it will be -** visited. Otherwise, it goes to 'protogray', as only its 'cache' field -** needs to be revisited. (A prototype to be in this barrier must be -** already finished, so its other fields cannot change and do not need -** to be revisited.) +** Barrier for prototype's cache of closures. It turns the prototype +** back to gray (it was black). For an 'OLD1' prototype, making it +** gray stops it from being visited by 'markold', so it is linked in +** the 'grayagain' list to ensure it will be visited. For other ages, +** it goes to the 'protogray' list, as only its 'cache' field needs to +** be revisited. (A prototype to be in this barrier must be already +** finished, so its other fields cannot change and do not need to be +** revisited.) */ LUAI_FUNC void luaC_protobarrier_ (lua_State *L, Proto *p) { global_State *g = G(L); @@ -233,7 +238,7 @@ LUAI_FUNC void luaC_protobarrier_ (lua_State *L, Proto *p) { linkgclist(p, g->grayagain); /* link it in 'grayagain' */ else linkgclist(p, g->protogray); /* link it in 'protogray' */ - black2gray(p); /* make prototype gray (to avoid other barriers) */ + black2gray(p); /* make prototype gray */ } @@ -533,9 +538,9 @@ static int traverseudata (global_State *g, Udata *u) { ** cache is white, clear it. (A cache should not prevent the ** collection of its reference.) Otherwise, if in generational ** mode, check the generational invariant. If the cache is old, -** everything is ok. If the prototype is 'old0', everything +** everything is ok. If the prototype is 'OLD0', everything ** is ok too. (It will naturally be visited again.) If the -** prototype is older than 'old0', then its cache (which is new) +** prototype is older than 'OLD0', then its cache (which is new) ** must be visited again in the next collection, so the prototype ** goes to the 'protogray' list. (If the prototype has a cache, ** it is already immutable and does not need other barriers; @@ -1085,9 +1090,9 @@ static void whitelist (global_State *g, GCObject *p) { /* -** Correct a list of gray objects. Because this correction is -** done after sweeping, young objects can be white and still -** be in the list. They are only removed. +** Correct a list of gray objects. +** Because this correction is done after sweeping, young objects might +** be turned white and still be in the list. They are only removed. ** For tables and userdata, advance 'touched1' to 'touched2'; 'touched2' ** objects become regular old and are removed from the list. ** For threads, just remove white ones from the list. @@ -1104,13 +1109,14 @@ static GCObject **correctgraylist (GCObject **p) { changeage(curr, G_TOUCHED1, G_TOUCHED2); p = next; /* go to next element */ } - else { - if (!iswhite(curr)) { + else { /* not touched in this cycle */ + if (!iswhite(curr)) { /* not white? */ lua_assert(isold(curr)); - if (getage(curr) == G_TOUCHED2) - changeage(curr, G_TOUCHED2, G_OLD); + if (getage(curr) == G_TOUCHED2) /* advance from G_TOUCHED2... */ + changeage(curr, G_TOUCHED2, G_OLD); /* ... to G_OLD */ gray2black(curr); /* make it black */ } + /* else, object is white: just remove it from this list */ *p = *next; /* remove 'curr' from gray list */ } break; @@ -1146,7 +1152,7 @@ static void correctgraylists (global_State *g) { /* -** Mark 'old1' objects when starting a new young collection. +** Mark 'OLD1' objects when starting a new young collection. ** Gray objects are already in some gray list, and so will be visited ** in the atomic step. */ @@ -1177,9 +1183,9 @@ static void finishgencycle (lua_State *L, global_State *g) { /* -** Does a young collection. First, mark 'old1' objects. (Only survival -** and "recent old" lists can contain 'old1' objects. New lists cannot -** contain 'old1' objects, at most 'old0' objects that were already +** Does a young collection. First, mark 'OLD1' objects. (Only survival +** and "recent old" lists can contain 'OLD1' objects. New lists cannot +** contain 'OLD1' objects, at most 'OLD0' objects that were already ** visited when marked old.) Then does the atomic step. Then, ** sweep all lists and advance pointers. Finally, finish the collection. */ From e885dee5ab4dbee2457ee2023340e848fdabdef9 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 25 Jul 2018 11:44:46 -0300 Subject: [PATCH 0320/1145] File operations try an "emergency collection" when failing If a file operation fails do to lack of resources (too many open files or not enough memory), it does a full garbage collection and tries the operation again. Lack of resources are "too many open files" (process wise and system wise) and "not enough memory". The code is full of '#if's because error codes are not part of the standard ISO C. --- liolib.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++---- loslib.c | 3 +-- 2 files changed, 65 insertions(+), 6 deletions(-) diff --git a/liolib.c b/liolib.c index 0884e9ac40..75e10ded0b 100644 --- a/liolib.c +++ b/liolib.c @@ -1,5 +1,5 @@ /* -** $Id: liolib.c,v 2.155 2018/02/21 13:48:44 roberto Exp roberto $ +** $Id: liolib.c $ ** Standard I/O (and system) library ** See Copyright Notice in lua.h */ @@ -68,7 +68,7 @@ static int l_checkmode (const char *mode) { /* ISO C definitions */ #define l_popen(L,c,m) \ - ((void)((void)c, m), \ + ((void)c, (void)m, \ luaL_error(L, "'popen' not supported"), \ (FILE*)0) #define l_pclose(L,file) ((void)L, (void)file, -1) @@ -133,6 +133,51 @@ static int l_checkmode (const char *mode) { /* }====================================================== */ +/* +** {====================================================== +** 'resourcetryagain' +** This function uses 'errno' to check whether the last error was +** related to lack of resources (e.g., not enough memory or too many +** open files). If so, the function performs a full garbage collection +** to try to release resources, and then it returns 1 to signal to +** the caller that it is worth trying again the failed operation. +** Otherwise, it returns 0. Because error codes are not ANSI C, the +** code must handle any combination of error codes that are defined. +** ======================================================= +*/ + +static int resourcetryagain (lua_State *L) { + +/* these are the resource-related errors in Linux */ +#if defined(EMFILE) || defined(ENFILE) || defined(ENOMEM) + +#if !defined(EMFILE) /* too many open files in the process */ +#define EMFILE -1 /* if not defined, use an impossible value */ +#endif + +#if !defined(ENFILE) /* too many open files in the system */ +#define ENFILE -1 +#endif + +#if !defined(ENOMEM) /* not enough memory */ +#define ENOMEM -1 +#endif + + if (errno == EMFILE || errno == ENFILE || errno == ENOMEM) { + lua_gc(L, LUA_GCCOLLECT); /* try to release resources with a full GC */ + return 1; /* signal to try again the creation */ + } + +#endif + + return 0; /* else, asume errors are not due to lack of resources */ + +} + +/* }====================================================== */ + + + #define IO_PREFIX "_IO_" #define IOPREF_LEN (sizeof(IO_PREFIX)/sizeof(char) - 1) #define IO_INPUT (IO_PREFIX "input") @@ -245,9 +290,22 @@ static LStream *newfile (lua_State *L) { } +/* +** Equivalent to 'fopen', but if it fails due to a lack of resources +** (see 'resourcetryagain'), do an "emergency" garbage collection to try +** to close some files and then tries to open the file again. +*/ +static FILE *trytoopen (lua_State *L, const char *path, const char *mode) { + FILE *f = fopen(path, mode); + if (f == NULL && resourcetryagain(L)) /* resource failure? */ + f = fopen(path, mode); /* try to open again */ + return f; +} + + static void opencheck (lua_State *L, const char *fname, const char *mode) { LStream *p = newfile(L); - p->f = fopen(fname, mode); + p->f = trytoopen(L, fname, mode); if (p->f == NULL) luaL_error(L, "cannot open file '%s' (%s)", fname, strerror(errno)); } @@ -259,7 +317,7 @@ static int io_open (lua_State *L) { LStream *p = newfile(L); const char *md = mode; /* to traverse/check mode */ luaL_argcheck(L, l_checkmode(md), 2, "invalid mode"); - p->f = fopen(filename, mode); + p->f = trytoopen(L, filename, mode); return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1; } @@ -278,6 +336,8 @@ static int io_popen (lua_State *L) { const char *mode = luaL_optstring(L, 2, "r"); LStream *p = newprefile(L); p->f = l_popen(L, filename, mode); + if (p->f == NULL && resourcetryagain(L)) /* resource failure? */ + p->f = l_popen(L, filename, mode); /* try to open again */ p->closef = &io_pclose; return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1; } diff --git a/loslib.c b/loslib.c index 62988d270b..8809e5ea2a 100644 --- a/loslib.c +++ b/loslib.c @@ -1,5 +1,5 @@ /* -** $Id: loslib.c,v 1.65 2016/07/18 17:58:58 roberto Exp roberto $ +** $Id: loslib.c $ ** Standard Operating System library ** See Copyright Notice in lua.h */ @@ -10,7 +10,6 @@ #include "lprefix.h" -#include #include #include #include From b47f2cd068fb14a27d3da6fb3d08305b7d7b354d Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 25 Jul 2018 14:56:42 -0300 Subject: [PATCH 0321/1145] Small improvements in the manual --- manual/manual.of | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/manual/manual.of b/manual/manual.of index f92be508f9..659daa55b5 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -1,4 +1,4 @@ -@Ci{$Id: manual.of,v 1.175 2018/06/18 19:17:35 roberto Exp roberto $} +@Ci{$Id: manual.of $} @C{[(-------------------------------------------------------------------------} @manual{ @@ -4567,7 +4567,8 @@ This is the only option that can raise a memory error. } -This function returns 0 if given an invalid option in @id{what}. +This function returns 0 to signal an invalid option in @id{what}; +even then the valid options are handled correctly. } @@ -7488,7 +7489,7 @@ you should call @Lid{math.randomseed} explicitly. The results from this function have good statistical qualities, but they are not cryptographically secure. -(For instance, there are no garanties that it is hard +(For instance, there are no guarantees that it is hard to predict future results based on the observation of some number of previous results.) @@ -7496,8 +7497,9 @@ some number of previous results.) @LibEntry{math.randomseed (x [, y])| -Sets @id{x} and @id{y} as the @Q{seed} -for the pseudo-random generator: +The integer parameters @id{x} and @id{y} are +concatenated into a 128-bit @Q{seed} that +is used to reinitialize the pseudo-random generator; equal seeds produce equal sequences of numbers. The default for @id{y} is zero. From aa4c5cf190f77ab2730af5e21cfd2b830ff329df Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 25 Jul 2018 15:31:04 -0300 Subject: [PATCH 0322/1145] Added directory to test file names in '$Id:' From the point of view of 'git', all names are relative to the root directory of the project. So, file names in '$Id:' also should be relative to that directory: the proper name for test file 'all.lua' is 'testes/all.lua'. --- testes/all.lua | 2 +- testes/api.lua | 2 +- testes/attrib.lua | 2 +- testes/big.lua | 2 +- testes/bitwise.lua | 2 +- testes/calls.lua | 2 +- testes/closure.lua | 2 +- testes/code.lua | 2 +- testes/constructs.lua | 2 +- testes/coroutine.lua | 2 +- testes/db.lua | 2 +- testes/errors.lua | 2 +- testes/events.lua | 2 +- testes/files.lua | 2 +- testes/gc.lua | 2 +- testes/gengc.lua | 2 +- testes/goto.lua | 2 +- testes/literals.lua | 2 +- testes/locals.lua | 2 +- testes/main.lua | 2 +- testes/math.lua | 2 +- testes/nextvar.lua | 2 +- testes/pm.lua | 2 +- testes/sort.lua | 2 +- testes/strings.lua | 2 +- testes/tpack.lua | 2 +- testes/utf8.lua | 2 +- testes/vararg.lua | 2 +- testes/verybig.lua | 2 +- 29 files changed, 29 insertions(+), 29 deletions(-) diff --git a/testes/all.lua b/testes/all.lua index 3779625400..26d2497659 100755 --- a/testes/all.lua +++ b/testes/all.lua @@ -1,5 +1,5 @@ #!../lua --- $Id: all.lua $ +-- $Id: testes/all.lua $ -- See Copyright Notice at the end of this file diff --git a/testes/api.lua b/testes/api.lua index 836a6070a3..bebb6d2d25 100644 --- a/testes/api.lua +++ b/testes/api.lua @@ -1,4 +1,4 @@ --- $Id: api.lua,v 1.155 2018/03/09 14:23:48 roberto Exp $ +-- $Id: testes/api.lua $ -- See Copyright Notice in file all.lua if T==nil then diff --git a/testes/attrib.lua b/testes/attrib.lua index 79a08a4f69..dcafd6345d 100644 --- a/testes/attrib.lua +++ b/testes/attrib.lua @@ -1,4 +1,4 @@ --- $Id: attrib.lua,v 1.69 2018/03/12 13:51:02 roberto Exp $ +-- $Id: testes/attrib.lua $ -- See Copyright Notice in file all.lua print "testing require" diff --git a/testes/big.lua b/testes/big.lua index ebee1ec0a4..150d15dc06 100644 --- a/testes/big.lua +++ b/testes/big.lua @@ -1,4 +1,4 @@ --- $Id: big.lua,v 1.35 2018/03/09 14:23:48 roberto Exp $ +-- $Id: testes/big.lua $ -- See Copyright Notice in file all.lua if _soft then diff --git a/testes/bitwise.lua b/testes/bitwise.lua index 3e7079d386..af542e7f68 100755 --- a/testes/bitwise.lua +++ b/testes/bitwise.lua @@ -1,4 +1,4 @@ --- $Id: bitwise.lua,v 1.27 2018/02/21 17:49:39 roberto Exp $ +-- $Id: testes/bitwise.lua $ -- See Copyright Notice in file all.lua print("testing bitwise operations") diff --git a/testes/calls.lua b/testes/calls.lua index 95d9d6d6ed..f5108a470e 100644 --- a/testes/calls.lua +++ b/testes/calls.lua @@ -1,4 +1,4 @@ --- $Id: calls.lua,v 1.66 2018/02/09 16:35:21 roberto Exp $ +-- $Id: testes/calls.lua $ -- See Copyright Notice in file all.lua print("testing functions and calls") diff --git a/testes/closure.lua b/testes/closure.lua index 79da3cc0f1..5d090d9116 100644 --- a/testes/closure.lua +++ b/testes/closure.lua @@ -1,4 +1,4 @@ --- $Id: closure.lua,v 1.62 2018/03/12 14:19:36 roberto Exp $ +-- $Id: testes/closure.lua $ -- See Copyright Notice in file all.lua print "testing closures" diff --git a/testes/code.lua b/testes/code.lua index 1a01c4a22e..6bd6ebfa8f 100644 --- a/testes/code.lua +++ b/testes/code.lua @@ -1,4 +1,4 @@ --- $Id: code.lua,v 1.55 2018/03/12 14:19:36 roberto Exp $ +-- $Id: testes/code.lua $ -- See Copyright Notice in file all.lua if T==nil then diff --git a/testes/constructs.lua b/testes/constructs.lua index 7796c46f72..a83df79eaf 100644 --- a/testes/constructs.lua +++ b/testes/constructs.lua @@ -1,4 +1,4 @@ --- $Id: constructs.lua,v 1.43 2018/02/21 17:41:07 roberto Exp $ +-- $Id: testes/constructs.lua $ -- See Copyright Notice in file all.lua ;;print "testing syntax";; diff --git a/testes/coroutine.lua b/testes/coroutine.lua index 220873200d..182c1e185e 100644 --- a/testes/coroutine.lua +++ b/testes/coroutine.lua @@ -1,4 +1,4 @@ --- $Id: coroutine.lua,v 1.48 2018/03/12 14:19:36 roberto Exp $ +-- $Id: testes/coroutine.lua $ -- See Copyright Notice in file all.lua print "testing coroutines" diff --git a/testes/db.lua b/testes/db.lua index 36d1cdaab6..2feaaef183 100644 --- a/testes/db.lua +++ b/testes/db.lua @@ -1,4 +1,4 @@ --- $Id: db.lua,v 1.90 2018/04/02 17:55:58 roberto Exp $ +-- $Id: testes/db.lua $ -- See Copyright Notice in file all.lua -- testing debug library diff --git a/testes/errors.lua b/testes/errors.lua index 19a7b6faad..142e8b3325 100644 --- a/testes/errors.lua +++ b/testes/errors.lua @@ -1,4 +1,4 @@ --- $Id: errors.lua $ +-- $Id: testes/errors.lua $ -- See Copyright Notice in file all.lua print("testing errors") diff --git a/testes/events.lua b/testes/events.lua index cf064d3d6b..21a822a7cf 100644 --- a/testes/events.lua +++ b/testes/events.lua @@ -1,4 +1,4 @@ --- $Id: events.lua,v 1.52 2018/03/12 13:51:02 roberto Exp $ +-- $Id: testes/events.lua $ -- See Copyright Notice in file all.lua print('testing metatables') diff --git a/testes/files.lua b/testes/files.lua index b2c7c202f1..c3e42235bc 100644 --- a/testes/files.lua +++ b/testes/files.lua @@ -1,4 +1,4 @@ --- $Id: files.lua,v 1.101 2018/03/12 13:51:02 roberto Exp $ +-- $Id: testes/files.lua $ -- See Copyright Notice in file all.lua local debug = require "debug" diff --git a/testes/gc.lua b/testes/gc.lua index 05072efd30..8b9179c858 100644 --- a/testes/gc.lua +++ b/testes/gc.lua @@ -1,4 +1,4 @@ --- $Id: gc.lua $ +-- $Id: testes/gc.lua $ -- See Copyright Notice in file all.lua print('testing incremental garbage collection') diff --git a/testes/gengc.lua b/testes/gengc.lua index 36aed806ef..b02f471b36 100644 --- a/testes/gengc.lua +++ b/testes/gengc.lua @@ -1,4 +1,4 @@ --- $Id: gengc.lua $ +-- $Id: testes/gengc.lua $ -- See Copyright Notice in file all.lua print('testing generational garbage collection') diff --git a/testes/goto.lua b/testes/goto.lua index d22601f934..238bc04aba 100644 --- a/testes/goto.lua +++ b/testes/goto.lua @@ -1,4 +1,4 @@ --- $Id: goto.lua,v 1.15 2017/11/30 13:31:07 roberto Exp $ +-- $Id: testes/goto.lua $ -- See Copyright Notice in file all.lua collectgarbage() diff --git a/testes/literals.lua b/testes/literals.lua index 3922b3f502..76c08f12b6 100644 --- a/testes/literals.lua +++ b/testes/literals.lua @@ -1,4 +1,4 @@ --- $Id: literals.lua,v 1.36 2016/11/07 13:11:28 roberto Exp $ +-- $Id: testes/literals.lua $ -- See Copyright Notice in file all.lua print('testing scanner') diff --git a/testes/locals.lua b/testes/locals.lua index f0780a031b..14e49a7ce6 100644 --- a/testes/locals.lua +++ b/testes/locals.lua @@ -1,4 +1,4 @@ --- $Id: locals.lua,v 1.41 2018/06/19 12:25:39 roberto Exp $ +-- $Id: testes/locals.lua $ -- See Copyright Notice in file all.lua print('testing local variables and environments') diff --git a/testes/main.lua b/testes/main.lua index 582b39c02e..c7bde0d962 100644 --- a/testes/main.lua +++ b/testes/main.lua @@ -1,5 +1,5 @@ # testing special comment on first line --- $Id: main.lua,v 1.69 2018/06/19 12:23:50 roberto Exp $ +-- $Id: testes/main.lua $ -- See Copyright Notice in file all.lua -- most (all?) tests here assume a reasonable "Unix-like" shell diff --git a/testes/math.lua b/testes/math.lua index e5c9d8ec5f..853dc20fb4 100644 --- a/testes/math.lua +++ b/testes/math.lua @@ -1,4 +1,4 @@ --- $Id: math.lua,v 1.86 2018/05/09 14:55:52 roberto Exp roberto $ +-- $Id: testes/math.lua $ -- See Copyright Notice in file all.lua print("testing numbers and math lib") diff --git a/testes/nextvar.lua b/testes/nextvar.lua index 3ac3acd94e..d2306ed1ff 100644 --- a/testes/nextvar.lua +++ b/testes/nextvar.lua @@ -1,4 +1,4 @@ --- $Id: nextvar.lua,v 1.85 2018/06/19 12:24:19 roberto Exp $ +-- $Id: testes/nextvar.lua $ -- See Copyright Notice in file all.lua print('testing tables, next, and for') diff --git a/testes/pm.lua b/testes/pm.lua index e517c8b668..cdcf3becb8 100644 --- a/testes/pm.lua +++ b/testes/pm.lua @@ -1,4 +1,4 @@ --- $Id: pm.lua,v 1.50 2018/03/12 14:19:36 roberto Exp $ +-- $Id: testes/pm.lua $ -- See Copyright Notice in file all.lua print('testing pattern matching') diff --git a/testes/sort.lua b/testes/sort.lua index 6eb9b70643..ef405d9219 100644 --- a/testes/sort.lua +++ b/testes/sort.lua @@ -1,4 +1,4 @@ --- $Id: sort.lua,v 1.39 2018/03/12 13:51:02 roberto Exp $ +-- $Id: testes/sort.lua $ -- See Copyright Notice in file all.lua print "testing (parts of) table library" diff --git a/testes/strings.lua b/testes/strings.lua index dd720b65a7..1260dbcc26 100644 --- a/testes/strings.lua +++ b/testes/strings.lua @@ -1,4 +1,4 @@ --- $Id: strings.lua,v 1.89 2018/06/19 12:25:15 roberto Exp $ +-- $Id: testes/strings.lua $ -- See Copyright Notice in file all.lua print('testing strings and string library') diff --git a/testes/tpack.lua b/testes/tpack.lua index 0e639cc5b4..4c5fc7f74d 100644 --- a/testes/tpack.lua +++ b/testes/tpack.lua @@ -1,4 +1,4 @@ --- $Id: tpack.lua,v 1.14 2018/06/04 14:26:32 roberto Exp $ +-- $Id: testes/tpack.lua $ -- See Copyright Notice in file all.lua local pack = string.pack diff --git a/testes/utf8.lua b/testes/utf8.lua index ebc190b75d..4b6a57fd49 100644 --- a/testes/utf8.lua +++ b/testes/utf8.lua @@ -1,4 +1,4 @@ --- $Id: utf8.lua,v 1.12 2016/11/07 13:11:28 roberto Exp $ +-- $Id: testes/utf8.lua $ -- See Copyright Notice in file all.lua print "testing UTF-8 library" diff --git a/testes/vararg.lua b/testes/vararg.lua index 4d5ce4abe8..44848d254f 100644 --- a/testes/vararg.lua +++ b/testes/vararg.lua @@ -1,4 +1,4 @@ --- $Id: vararg.lua,v 1.29 2018/03/12 14:19:36 roberto Exp $ +-- $Id: testes/vararg.lua $ -- See Copyright Notice in file all.lua print('testing vararg') diff --git a/testes/verybig.lua b/testes/verybig.lua index 5b29dea7ae..8fb7b13e8a 100644 --- a/testes/verybig.lua +++ b/testes/verybig.lua @@ -1,4 +1,4 @@ --- $Id: verybig.lua,v 1.27 2018/03/09 14:23:48 roberto Exp $ +-- $Id: testes/verybig.lua $ -- See Copyright Notice in file all.lua print "testing RK" From 3d838f635cc81ec3332f9a904992db1c6d8a46ad Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 27 Jul 2018 15:50:53 -0300 Subject: [PATCH 0323/1145] Added "emergency collection" to 'io.tmpfile' and 'os.tmpname' These operations also can give errors for lack of resources, so they also will try "emergency collections" in case of resource errors. Because there are now two libraries with that kind of handling, 'resourcetryagain' was moved to the auxiliary library to be shared by the libraries. --- lauxlib.c | 46 ++++++++++++++++++++++++++++++++++++++++- lauxlib.h | 5 ++++- liolib.c | 54 ++++++------------------------------------------ loslib.c | 2 ++ manual/manual.of | 14 +++++++++++++ 5 files changed, 71 insertions(+), 50 deletions(-) diff --git a/lauxlib.c b/lauxlib.c index a8f2cc2e7a..53b8c9bb34 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -1,5 +1,5 @@ /* -** $Id: lauxlib.c,v 1.294 2018/02/27 18:47:32 roberto Exp roberto $ +** $Id: lauxlib.c $ ** Auxiliary functions for building Lua libraries ** See Copyright Notice in lua.h */ @@ -290,6 +290,50 @@ LUALIB_API int luaL_execresult (lua_State *L, int stat) { /* }====================================================== */ +/* +** {====================================================== +** 'luaL_resourcetryagain' +** This function uses 'errno' to check whether the last error was +** related to lack of resources (e.g., not enough memory or too many +** open files). If so, the function performs a full garbage collection +** to try to release resources, and then it returns 1 to signal to +** the caller that it is worth trying again the failed operation. +** Otherwise, it returns 0. Because error codes are not ANSI C, the +** code must handle any combination of error codes that are defined. +** ======================================================= +*/ + +LUALIB_API int luaL_resourcetryagain (lua_State *L) { + +/* these are the resource-related errors in Linux */ +#if defined(EMFILE) || defined(ENFILE) || defined(ENOMEM) + +#if !defined(EMFILE) /* too many open files in the process */ +#define EMFILE -1 /* if not defined, use an impossible value */ +#endif + +#if !defined(ENFILE) /* too many open files in the system */ +#define ENFILE -1 +#endif + +#if !defined(ENOMEM) /* not enough memory */ +#define ENOMEM -1 +#endif + + if (errno == EMFILE || errno == ENFILE || errno == ENOMEM) { + lua_gc(L, LUA_GCCOLLECT); /* try to release resources with a full GC */ + return 1; /* signal to try again the creation */ + } + +#endif + + return 0; /* else, asume errors are not due to lack of resources */ + +} + +/* }====================================================== */ + + /* ** {====================================================== ** Userdata's metatable manipulation diff --git a/lauxlib.h b/lauxlib.h index 9f91f6a6b3..cd4d01e50a 100644 --- a/lauxlib.h +++ b/lauxlib.h @@ -1,5 +1,5 @@ /* -** $Id: lauxlib.h,v 1.133 2017/06/27 18:32:49 roberto Exp roberto $ +** $Id: lauxlib.h $ ** Auxiliary functions for building Lua libraries ** See Copyright Notice in lua.h */ @@ -77,6 +77,9 @@ LUALIB_API int (luaL_checkoption) (lua_State *L, int arg, const char *def, LUALIB_API int (luaL_fileresult) (lua_State *L, int stat, const char *fname); LUALIB_API int (luaL_execresult) (lua_State *L, int stat); +LUALIB_API int (luaL_resourcetryagain) (lua_State *L); + + /* predefined references */ #define LUA_NOREF (-2) #define LUA_REFNIL (-1) diff --git a/liolib.c b/liolib.c index 75e10ded0b..21305b8c76 100644 --- a/liolib.c +++ b/liolib.c @@ -133,50 +133,6 @@ static int l_checkmode (const char *mode) { /* }====================================================== */ -/* -** {====================================================== -** 'resourcetryagain' -** This function uses 'errno' to check whether the last error was -** related to lack of resources (e.g., not enough memory or too many -** open files). If so, the function performs a full garbage collection -** to try to release resources, and then it returns 1 to signal to -** the caller that it is worth trying again the failed operation. -** Otherwise, it returns 0. Because error codes are not ANSI C, the -** code must handle any combination of error codes that are defined. -** ======================================================= -*/ - -static int resourcetryagain (lua_State *L) { - -/* these are the resource-related errors in Linux */ -#if defined(EMFILE) || defined(ENFILE) || defined(ENOMEM) - -#if !defined(EMFILE) /* too many open files in the process */ -#define EMFILE -1 /* if not defined, use an impossible value */ -#endif - -#if !defined(ENFILE) /* too many open files in the system */ -#define ENFILE -1 -#endif - -#if !defined(ENOMEM) /* not enough memory */ -#define ENOMEM -1 -#endif - - if (errno == EMFILE || errno == ENFILE || errno == ENOMEM) { - lua_gc(L, LUA_GCCOLLECT); /* try to release resources with a full GC */ - return 1; /* signal to try again the creation */ - } - -#endif - - return 0; /* else, asume errors are not due to lack of resources */ - -} - -/* }====================================================== */ - - #define IO_PREFIX "_IO_" #define IOPREF_LEN (sizeof(IO_PREFIX)/sizeof(char) - 1) @@ -292,12 +248,12 @@ static LStream *newfile (lua_State *L) { /* ** Equivalent to 'fopen', but if it fails due to a lack of resources -** (see 'resourcetryagain'), do an "emergency" garbage collection to try -** to close some files and then tries to open the file again. +** (see 'luaL_resourcetryagain'), do an "emergency" garbage collection +** to try to close some files and then tries to open the file again. */ static FILE *trytoopen (lua_State *L, const char *path, const char *mode) { FILE *f = fopen(path, mode); - if (f == NULL && resourcetryagain(L)) /* resource failure? */ + if (f == NULL && luaL_resourcetryagain(L)) /* resource failure? */ f = fopen(path, mode); /* try to open again */ return f; } @@ -336,7 +292,7 @@ static int io_popen (lua_State *L) { const char *mode = luaL_optstring(L, 2, "r"); LStream *p = newprefile(L); p->f = l_popen(L, filename, mode); - if (p->f == NULL && resourcetryagain(L)) /* resource failure? */ + if (p->f == NULL && luaL_resourcetryagain(L)) /* resource failure? */ p->f = l_popen(L, filename, mode); /* try to open again */ p->closef = &io_pclose; return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1; @@ -346,6 +302,8 @@ static int io_popen (lua_State *L) { static int io_tmpfile (lua_State *L) { LStream *p = newfile(L); p->f = tmpfile(); + if (p->f == NULL && luaL_resourcetryagain(L)) /* resource failure? */ + p->f = tmpfile(); /* try to open again */ return (p->f == NULL) ? luaL_fileresult(L, 0, NULL) : 1; } diff --git a/loslib.c b/loslib.c index 8809e5ea2a..1962f55fb5 100644 --- a/loslib.c +++ b/loslib.c @@ -166,6 +166,8 @@ static int os_tmpname (lua_State *L) { char buff[LUA_TMPNAMBUFSIZE]; int err; lua_tmpnam(buff, err); + if (err && luaL_resourcetryagain(L)) /* resource failure? */ + lua_tmpnam(buff, err); /* try again */ if (err) return luaL_error(L, "unable to generate a unique filename"); lua_pushstring(L, buff); diff --git a/manual/manual.of b/manual/manual.of index 659daa55b5..5a8b1b2c28 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -5538,6 +5538,20 @@ Leaves a copy of the module on the stack. } +@APIEntry{int luaL_resourcetryagain (lua_State *L);| +@apii{0,0,m} + +Try to release resources in case of errors. +This function uses @id{errno} to check whether the last error was +related to lack of resources (e.g., not enough memory or too many +open files). +If so, the function performs a full garbage collection +to try to release resources, and then it returns 1 to signal to +the caller that it is worth trying again the failed operation. +Otherwise, it returns 0. + +} + @APIEntry{void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup);| @apii{nup,0,m} From faaf7e481fbadc2ef520a96b638dd910f3c9ff14 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 16 Aug 2018 14:38:05 -0300 Subject: [PATCH 0324/1145] Removed use of 'rl_inhibit_completion' in 'lua.c' Some old systems (e.g., Mac OS X 10.4) do not define 'rl_inhibit_completion', even when line history is available. Anyway, the user can configure this option externally, using '~/.inputrc'. --- lua.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lua.c b/lua.c index ae783a0d7e..fa534ba2b4 100644 --- a/lua.c +++ b/lua.c @@ -1,5 +1,5 @@ /* -** $Id: lua.c,v 1.234 2018/03/06 20:30:17 roberto Exp roberto $ +** $Id: lua.c $ ** Lua stand-alone interpreter ** See Copyright Notice in lua.h */ @@ -383,8 +383,7 @@ static int handle_luainit (lua_State *L) { #include #include -#define lua_initreadline(L) \ - ((void)L, rl_readline_name="lua", rl_inhibit_completion=1) +#define lua_initreadline(L) ((void)L, rl_readline_name="lua") #define lua_readline(L,b,p) ((void)L, ((b)=readline(p)) != NULL) #define lua_saveline(L,line) ((void)L, add_history(line)) #define lua_freeline(L,b) ((void)L, free(b)) From 3dcd04ad610d151afbe8cd92c43e2232e621fbb5 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 17 Aug 2018 15:53:39 -0300 Subject: [PATCH 0325/1145] details Minor optimizations in 'lvm.c'. (Not exactly optimizations, but more chances for optimizations.) --- lvm.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lvm.c b/lvm.c index 4e4ef270e4..7550dcc8c0 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.359 2018/06/18 17:58:21 roberto Exp roberto $ +** $Id: lvm.c $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -621,8 +621,8 @@ void luaV_objlen (lua_State *L, StkId ra, const TValue *rb) { ** otherwise 'floor(q) == trunc(q) - 1'. */ lua_Integer luaV_div (lua_State *L, lua_Integer m, lua_Integer n) { - if (l_castS2U(n) + 1u <= 1u) { /* special cases: -1 or 0 */ - if (unlikely(n == 0)) + if (unlikely(l_castS2U(n) + 1u <= 1u)) { /* special cases: -1 or 0 */ + if (n == 0) luaG_runerror(L, "attempt to divide by zero"); return intop(-, 0, m); /* n==-1; avoid overflow with 0x80000...//-1 */ } @@ -641,14 +641,14 @@ lua_Integer luaV_div (lua_State *L, lua_Integer m, lua_Integer n) { ** about luaV_div.) */ lua_Integer luaV_mod (lua_State *L, lua_Integer m, lua_Integer n) { - if (l_castS2U(n) + 1u <= 1u) { /* special cases: -1 or 0 */ - if (unlikely(n == 0)) + if (unlikely(l_castS2U(n) + 1u <= 1u)) { /* special cases: -1 or 0 */ + if (n == 0) luaG_runerror(L, "attempt to perform 'n%%0'"); return 0; /* m % -1 == 0; avoid overflow with 0x80000...%-1 */ } else { lua_Integer r = m % n; - if (r != 0 && (m ^ n) < 0) /* 'm/n' would be non-integer negative? */ + if (r != 0 && (r ^ n) < 0) /* 'm/n' would be non-integer negative? */ r += n; /* correct result for different rounding */ return r; } From f99509581ee73c1c2dbddb3398e87c098771d31f Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 23 Aug 2018 14:26:12 -0300 Subject: [PATCH 0326/1145] Removed extra information from RCS keyword strings Version numbers and dates (mostly wrong) from RCS keyword strings removed from all source files; only the file name are kept. --- lapi.c | 2 +- lapi.h | 2 +- lbaselib.c | 2 +- lcode.h | 2 +- lcorolib.c | 2 +- lctype.c | 2 +- lctype.h | 2 +- ldblib.c | 2 +- ldebug.c | 2 +- ldebug.h | 2 +- ldo.h | 2 +- ldump.c | 2 +- lfunc.c | 2 +- lfunc.h | 2 +- lgc.h | 2 +- linit.c | 2 +- ljumptab.h | 2 +- llex.c | 2 +- llex.h | 2 +- llimits.h | 2 +- lmathlib.c | 2 +- lmem.c | 2 +- lmem.h | 2 +- loadlib.c | 2 +- lobject.c | 2 +- lobject.h | 2 +- lopcodes.c | 2 +- lopcodes.h | 2 +- lopnames.h | 2 +- lparser.c | 2 +- lparser.h | 2 +- lprefix.h | 2 +- lstate.c | 2 +- lstring.c | 2 +- lstring.h | 2 +- lstrlib.c | 2 +- ltable.c | 2 +- ltable.h | 2 +- ltablib.c | 2 +- ltests.c | 2 +- ltests.h | 2 +- ltm.c | 2 +- ltm.h | 2 +- lua.h | 2 +- luaconf.h | 2 +- lualib.h | 2 +- lundump.c | 2 +- lundump.h | 2 +- lvm.h | 2 +- lzio.c | 2 +- lzio.h | 2 +- 51 files changed, 51 insertions(+), 51 deletions(-) diff --git a/lapi.c b/lapi.c index e69215a881..0ca9a32145 100644 --- a/lapi.c +++ b/lapi.c @@ -1,5 +1,5 @@ /* -** $Id: lapi.c,v 2.294 2018/06/15 19:31:22 roberto Exp roberto $ +** $Id: lapi.c $ ** Lua API ** See Copyright Notice in lua.h */ diff --git a/lapi.h b/lapi.h index 77ebb30027..016f78cccd 100644 --- a/lapi.h +++ b/lapi.h @@ -1,5 +1,5 @@ /* -** $Id: lapi.h,v 2.10 2017/11/01 18:20:48 roberto Exp $ +** $Id: lapi.h $ ** Auxiliary functions from Lua API ** See Copyright Notice in lua.h */ diff --git a/lbaselib.c b/lbaselib.c index 12a9e888c8..e776c2a25b 100644 --- a/lbaselib.c +++ b/lbaselib.c @@ -1,5 +1,5 @@ /* -** $Id: lbaselib.c,v 1.322 2018/02/27 18:47:32 roberto Exp roberto $ +** $Id: lbaselib.c $ ** Basic library ** See Copyright Notice in lua.h */ diff --git a/lcode.h b/lcode.h index 0c4159735c..dd091c7818 100644 --- a/lcode.h +++ b/lcode.h @@ -1,5 +1,5 @@ /* -** $Id: lcode.h,v 1.72 2018/03/19 20:03:44 roberto Exp roberto $ +** $Id: lcode.h $ ** Code generator for Lua ** See Copyright Notice in lua.h */ diff --git a/lcorolib.c b/lcorolib.c index 49a3cf28d1..9038f9fb5d 100644 --- a/lcorolib.c +++ b/lcorolib.c @@ -1,5 +1,5 @@ /* -** $Id: lcorolib.c,v 1.10 2016/04/11 19:19:55 roberto Exp roberto $ +** $Id: lcorolib.c $ ** Coroutine Library ** See Copyright Notice in lua.h */ diff --git a/lctype.c b/lctype.c index 367640c662..4eaad167ed 100644 --- a/lctype.c +++ b/lctype.c @@ -1,5 +1,5 @@ /* -** $Id: lctype.c,v 1.11 2011/10/03 16:19:23 roberto Exp roberto $ +** $Id: lctype.c $ ** 'ctype' functions for Lua ** See Copyright Notice in lua.h */ diff --git a/lctype.h b/lctype.h index de1efd51fc..cbff4d7e19 100644 --- a/lctype.h +++ b/lctype.h @@ -1,5 +1,5 @@ /* -** $Id: lctype.h,v 1.12 2011/07/15 12:50:29 roberto Exp roberto $ +** $Id: lctype.h $ ** 'ctype' functions for Lua ** See Copyright Notice in lua.h */ diff --git a/ldblib.c b/ldblib.c index a977654378..200108426e 100644 --- a/ldblib.c +++ b/ldblib.c @@ -1,5 +1,5 @@ /* -** $Id: ldblib.c,v 1.154 2018/03/05 14:15:04 roberto Exp roberto $ +** $Id: ldblib.c $ ** Interface from Lua to its debug API ** See Copyright Notice in lua.h */ diff --git a/ldebug.c b/ldebug.c index f3c093a05b..3590010c9a 100644 --- a/ldebug.c +++ b/ldebug.c @@ -1,5 +1,5 @@ /* -** $Id: ldebug.c,v 2.157 2018/05/02 18:17:59 roberto Exp roberto $ +** $Id: ldebug.c $ ** Debug Interface ** See Copyright Notice in lua.h */ diff --git a/ldebug.h b/ldebug.h index 3b8e6ae0f5..31ecc2f6cf 100644 --- a/ldebug.h +++ b/ldebug.h @@ -1,5 +1,5 @@ /* -** $Id: ldebug.h,v 2.17 2018/05/02 18:17:59 roberto Exp roberto $ +** $Id: ldebug.h $ ** Auxiliary functions from Debug Interface module ** See Copyright Notice in lua.h */ diff --git a/ldo.h b/ldo.h index a3ac6f503a..c836a2a174 100644 --- a/ldo.h +++ b/ldo.h @@ -1,5 +1,5 @@ /* -** $Id: ldo.h,v 2.43 2018/02/17 19:29:29 roberto Exp roberto $ +** $Id: ldo.h $ ** Stack and Call structure of Lua ** See Copyright Notice in lua.h */ diff --git a/ldump.c b/ldump.c index 0724aeb5da..aca6245bf4 100644 --- a/ldump.c +++ b/ldump.c @@ -1,5 +1,5 @@ /* -** $Id: ldump.c,v 2.40 2017/11/28 11:19:07 roberto Exp roberto $ +** $Id: ldump.c $ ** save precompiled Lua chunks ** See Copyright Notice in lua.h */ diff --git a/lfunc.c b/lfunc.c index 307a857bd9..16e007318d 100644 --- a/lfunc.c +++ b/lfunc.c @@ -1,5 +1,5 @@ /* -** $Id: lfunc.c,v 2.50 2017/06/27 11:35:31 roberto Exp roberto $ +** $Id: lfunc.c $ ** Auxiliary functions to manipulate prototypes and closures ** See Copyright Notice in lua.h */ diff --git a/lfunc.h b/lfunc.h index 74a9fc2e6e..859ccc1205 100644 --- a/lfunc.h +++ b/lfunc.h @@ -1,5 +1,5 @@ /* -** $Id: lfunc.h,v 2.19 2018/01/28 15:13:26 roberto Exp roberto $ +** $Id: lfunc.h $ ** Auxiliary functions to manipulate prototypes and closures ** See Copyright Notice in lua.h */ diff --git a/lgc.h b/lgc.h index d7adf44429..e0a8806b14 100644 --- a/lgc.h +++ b/lgc.h @@ -1,5 +1,5 @@ /* -** $Id: lgc.h,v 2.102 2018/02/19 20:06:56 roberto Exp roberto $ +** $Id: lgc.h $ ** Garbage Collector ** See Copyright Notice in lua.h */ diff --git a/linit.c b/linit.c index 2db8d5b30d..69808f84f4 100644 --- a/linit.c +++ b/linit.c @@ -1,5 +1,5 @@ /* -** $Id: linit.c,v 1.40 2017/06/27 18:32:49 roberto Exp roberto $ +** $Id: linit.c $ ** Initialization of libraries for lua.c and other clients ** See Copyright Notice in lua.h */ diff --git a/ljumptab.h b/ljumptab.h index 01d2c11e5a..c775f10a4a 100644 --- a/ljumptab.h +++ b/ljumptab.h @@ -1,5 +1,5 @@ /* -** $Id: ljumptab.h 2018/07/10 13:40:16 $ +** $Id: ljumptab.h $ ** Jump Table for the Lua interpreter ** See Copyright Notice in lua.h */ diff --git a/llex.c b/llex.c index 9419a8cdf0..4a25607cf3 100644 --- a/llex.c +++ b/llex.c @@ -1,5 +1,5 @@ /* -** $Id: llex.c,v 2.101 2018/03/07 15:55:38 roberto Exp roberto $ +** $Id: llex.c $ ** Lexical Analyzer ** See Copyright Notice in lua.h */ diff --git a/llex.h b/llex.h index d1d5a4a8f5..d1a4cba7c2 100644 --- a/llex.h +++ b/llex.h @@ -1,5 +1,5 @@ /* -** $Id: llex.h,v 1.81 2018/03/07 15:55:38 roberto Exp roberto $ +** $Id: llex.h $ ** Lexical Analyzer ** See Copyright Notice in lua.h */ diff --git a/llimits.h b/llimits.h index 715d55b811..6afa8997e0 100644 --- a/llimits.h +++ b/llimits.h @@ -1,5 +1,5 @@ /* -** $Id: llimits.h,v 1.150 2018/05/30 14:25:52 roberto Exp roberto $ +** $Id: llimits.h $ ** Limits, basic types, and some other 'installation-dependent' definitions ** See Copyright Notice in lua.h */ diff --git a/lmathlib.c b/lmathlib.c index 4101279731..e8e88e7a5b 100644 --- a/lmathlib.c +++ b/lmathlib.c @@ -1,5 +1,5 @@ /* -** $Id: lmathlib.c,v 1.134 2018/05/16 11:27:59 roberto Exp roberto $ +** $Id: lmathlib.c $ ** Standard mathematical library ** See Copyright Notice in lua.h */ diff --git a/lmem.c b/lmem.c index 56b4affe0e..7751870741 100644 --- a/lmem.c +++ b/lmem.c @@ -1,5 +1,5 @@ /* -** $Id: lmem.c,v 1.97 2018/05/30 14:25:52 roberto Exp roberto $ +** $Id: lmem.c $ ** Interface to Memory Manager ** See Copyright Notice in lua.h */ diff --git a/lmem.h b/lmem.h index f87c913248..8c75a44beb 100644 --- a/lmem.h +++ b/lmem.h @@ -1,5 +1,5 @@ /* -** $Id: lmem.h,v 1.46 2017/12/08 17:28:25 roberto Exp roberto $ +** $Id: lmem.h $ ** Interface to Memory Manager ** See Copyright Notice in lua.h */ diff --git a/loadlib.c b/loadlib.c index 8c8ab8c586..a6ce30d4f3 100644 --- a/loadlib.c +++ b/loadlib.c @@ -1,5 +1,5 @@ /* -** $Id: loadlib.c,v 1.133 2018/07/06 13:38:38 roberto Exp $ +** $Id: loadlib.c $ ** Dynamic library loader for Lua ** See Copyright Notice in lua.h ** diff --git a/lobject.c b/lobject.c index 8b8de37c37..79cf55b8e1 100644 --- a/lobject.c +++ b/lobject.c @@ -1,5 +1,5 @@ /* -** $Id: lobject.c,v 2.125 2018/04/25 16:26:20 roberto Exp roberto $ +** $Id: lobject.c $ ** Some generic functions over Lua objects ** See Copyright Notice in lua.h */ diff --git a/lobject.h b/lobject.h index dbb9982d03..ddcf609cf6 100644 --- a/lobject.h +++ b/lobject.h @@ -1,5 +1,5 @@ /* -** $Id: lobject.h,v 2.145 2018/06/15 14:14:20 roberto Exp roberto $ +** $Id: lobject.h $ ** Type definitions for Lua objects ** See Copyright Notice in lua.h */ diff --git a/lopcodes.c b/lopcodes.c index 1a3b0e6af8..95347b2728 100644 --- a/lopcodes.c +++ b/lopcodes.c @@ -1,5 +1,5 @@ /* -** $Id: lopcodes.c,v 1.83 2018/06/26 18:00:55 roberto Exp $ +** $Id: lopcodes.c $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ diff --git a/lopcodes.h b/lopcodes.h index d7b5dfe013..9442a33689 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -1,5 +1,5 @@ /* -** $Id: lopcodes.h,v 1.194 2018/06/26 18:00:55 roberto Exp $ +** $Id: lopcodes.h $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ diff --git a/lopnames.h b/lopnames.h index ab434b5979..c40eaeae26 100644 --- a/lopnames.h +++ b/lopnames.h @@ -1,5 +1,5 @@ /* -** $Id: lopnames.h,v 1.1 2018/06/26 18:00:55 roberto Exp $ +** $Id: lopnames.h $ ** Opcode names ** See Copyright Notice in lua.h */ diff --git a/lparser.c b/lparser.c index 3c68d1c240..9f156dd973 100644 --- a/lparser.c +++ b/lparser.c @@ -1,5 +1,5 @@ /* -** $Id: lparser.c,v 2.180 2018/04/04 14:23:41 roberto Exp roberto $ +** $Id: lparser.c $ ** Lua Parser ** See Copyright Notice in lua.h */ diff --git a/lparser.h b/lparser.h index da6a7483ee..e158c9d918 100644 --- a/lparser.h +++ b/lparser.h @@ -1,5 +1,5 @@ /* -** $Id: lparser.h,v 1.81 2018/03/07 15:55:38 roberto Exp roberto $ +** $Id: lparser.h $ ** Lua Parser ** See Copyright Notice in lua.h */ diff --git a/lprefix.h b/lprefix.h index 6b72f0eba7..dd14767ed2 100644 --- a/lprefix.h +++ b/lprefix.h @@ -1,5 +1,5 @@ /* -** $Id: lprefix.h,v 1.1 2014/11/03 15:12:44 roberto Exp roberto $ +** $Id: lprefix.h $ ** Definitions for Lua code that must come before any other header file ** See Copyright Notice in lua.h */ diff --git a/lstate.c b/lstate.c index d4b4def80a..8b0219bccd 100644 --- a/lstate.c +++ b/lstate.c @@ -1,5 +1,5 @@ /* -** $Id: lstate.c,v 2.154 2018/06/15 19:31:22 roberto Exp roberto $ +** $Id: lstate.c $ ** Global State ** See Copyright Notice in lua.h */ diff --git a/lstring.c b/lstring.c index f1e5d82b16..c52539a6d5 100644 --- a/lstring.c +++ b/lstring.c @@ -1,5 +1,5 @@ /* -** $Id: lstring.c,v 2.65 2018/02/20 16:52:50 roberto Exp roberto $ +** $Id: lstring.c $ ** String table (keeps all strings handled by Lua) ** See Copyright Notice in lua.h */ diff --git a/lstring.h b/lstring.h index 2be5a3ff0b..e7e4aedc48 100644 --- a/lstring.h +++ b/lstring.h @@ -1,5 +1,5 @@ /* -** $Id: lstring.h,v 1.63 2017/11/23 19:29:04 roberto Exp roberto $ +** $Id: lstring.h $ ** String table (keep all strings handled by Lua) ** See Copyright Notice in lua.h */ diff --git a/lstrlib.c b/lstrlib.c index b43464abcc..a635e9d4d6 100644 --- a/lstrlib.c +++ b/lstrlib.c @@ -1,5 +1,5 @@ /* -** $Id: lstrlib.c,v 1.262 2018/02/21 17:48:31 roberto Exp roberto $ +** $Id: lstrlib.c $ ** Standard library for string operations and pattern-matching ** See Copyright Notice in lua.h */ diff --git a/ltable.c b/ltable.c index 6f9c4035dd..e12381b2d8 100644 --- a/ltable.c +++ b/ltable.c @@ -1,5 +1,5 @@ /* -** $Id: ltable.c,v 2.139 2018/06/15 14:14:20 roberto Exp roberto $ +** $Id: ltable.c $ ** Lua tables (hash) ** See Copyright Notice in lua.h */ diff --git a/ltable.h b/ltable.h index ba2dddc8f2..9565833f64 100644 --- a/ltable.h +++ b/ltable.h @@ -1,5 +1,5 @@ /* -** $Id: ltable.h,v 2.27 2018/06/01 16:51:34 roberto Exp roberto $ +** $Id: ltable.h $ ** Lua tables (hash) ** See Copyright Notice in lua.h */ diff --git a/ltablib.c b/ltablib.c index 7bbc3a0715..29c53e9483 100644 --- a/ltablib.c +++ b/ltablib.c @@ -1,5 +1,5 @@ /* -** $Id: ltablib.c,v 1.96 2018/03/16 14:18:18 roberto Exp roberto $ +** $Id: ltablib.c $ ** Library for Table Manipulation ** See Copyright Notice in lua.h */ diff --git a/ltests.c b/ltests.c index 13717da9b3..bc71d937f5 100644 --- a/ltests.c +++ b/ltests.c @@ -1,5 +1,5 @@ /* -** $Id: ltests.c,v 2.246 2018/06/26 18:00:55 roberto Exp roberto $ +** $Id: ltests.c $ ** Internal Module for Debugging of the Lua Implementation ** See Copyright Notice in lua.h */ diff --git a/ltests.h b/ltests.h index 8d965e0a6c..54bc4f5f7e 100644 --- a/ltests.h +++ b/ltests.h @@ -1,5 +1,5 @@ /* -** $Id: ltests.h,v 2.58 2018/04/19 15:42:41 roberto Exp roberto $ +** $Id: ltests.h $ ** Internal Header for Debugging of the Lua Implementation ** See Copyright Notice in lua.h */ diff --git a/ltm.c b/ltm.c index d4e2b447b9..1c1a18b7a3 100644 --- a/ltm.c +++ b/ltm.c @@ -1,5 +1,5 @@ /* -** $Id: ltm.c,v 2.69 2018/06/08 19:06:59 roberto Exp roberto $ +** $Id: ltm.c $ ** Tag methods ** See Copyright Notice in lua.h */ diff --git a/ltm.h b/ltm.h index 12ae09ded7..89aee78adc 100644 --- a/ltm.h +++ b/ltm.h @@ -1,5 +1,5 @@ /* -** $Id: ltm.h,v 2.38 2018/06/08 19:06:59 roberto Exp roberto $ +** $Id: ltm.h $ ** Tag methods ** See Copyright Notice in lua.h */ diff --git a/lua.h b/lua.h index 03d92c0ba3..a014be1fdd 100644 --- a/lua.h +++ b/lua.h @@ -1,5 +1,5 @@ /* -** $Id: lua.h,v 1.346 2018/04/04 14:23:41 roberto Exp roberto $ +** $Id: lua.h $ ** Lua - A Scripting Language ** Lua.org, PUC-Rio, Brazil (http://www.lua.org) ** See Copyright Notice at the end of this file diff --git a/luaconf.h b/luaconf.h index 9cddb263b8..cc8e1bdcff 100644 --- a/luaconf.h +++ b/luaconf.h @@ -1,5 +1,5 @@ /* -** $Id: luaconf.h,v 1.270 2018/06/18 12:51:05 roberto Exp roberto $ +** $Id: luaconf.h $ ** Configuration file for Lua ** See Copyright Notice in lua.h */ diff --git a/lualib.h b/lualib.h index 12f7d02277..eb08b530a6 100644 --- a/lualib.h +++ b/lualib.h @@ -1,5 +1,5 @@ /* -** $Id: lualib.h,v 1.45 2017/01/12 17:14:26 roberto Exp roberto $ +** $Id: lualib.h $ ** Lua standard libraries ** See Copyright Notice in lua.h */ diff --git a/lundump.c b/lundump.c index 64b643623a..00ff6deffb 100644 --- a/lundump.c +++ b/lundump.c @@ -1,5 +1,5 @@ /* -** $Id: lundump.c,v 2.49 2017/12/07 18:59:52 roberto Exp roberto $ +** $Id: lundump.c $ ** load precompiled Lua chunks ** See Copyright Notice in lua.h */ diff --git a/lundump.h b/lundump.h index bc9f99a29c..739c7fcde0 100644 --- a/lundump.h +++ b/lundump.h @@ -1,5 +1,5 @@ /* -** $Id: lundump.h,v 1.44 2014/06/19 18:27:20 roberto Exp roberto $ +** $Id: lundump.h $ ** load precompiled Lua chunks ** See Copyright Notice in lua.h */ diff --git a/lvm.h b/lvm.h index a789acc402..d58daacdc0 100644 --- a/lvm.h +++ b/lvm.h @@ -1,5 +1,5 @@ /* -** $Id: lvm.h,v 2.51 2018/02/23 13:13:31 roberto Exp roberto $ +** $Id: lvm.h $ ** Lua virtual machine ** See Copyright Notice in lua.h */ diff --git a/lzio.c b/lzio.c index eb6151a0b2..cd0a02d5f9 100644 --- a/lzio.c +++ b/lzio.c @@ -1,5 +1,5 @@ /* -** $Id: lzio.c,v 1.36 2014/11/02 19:19:04 roberto Exp roberto $ +** $Id: lzio.c $ ** Buffered streams ** See Copyright Notice in lua.h */ diff --git a/lzio.h b/lzio.h index fb310b99c5..38f397fd28 100644 --- a/lzio.h +++ b/lzio.h @@ -1,5 +1,5 @@ /* -** $Id: lzio.h,v 1.30 2014/12/19 17:26:14 roberto Exp roberto $ +** $Id: lzio.h $ ** Buffered streams ** See Copyright Notice in lua.h */ From 8c8a91f2ef7acccb99e3737913faad8d48b39571 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 24 Aug 2018 10:17:54 -0300 Subject: [PATCH 0327/1145] Deprecated the emulation of '__le' using '__lt' As hinted in the manual for Lua 5.3, the emulation of the metamethod for '__le' using '__le' has been deprecated. It is slow, complicates the logic, and it is easy to avoid this emulation by defining a proper '__le' function. Moreover, often this emulation was used wrongly, with a programmer assuming that an order is total when it is not (e.g., NaN in floating-point numbers). --- lstate.h | 8 +++++--- ltests.h | 1 + ltm.c | 2 ++ luaconf.h | 8 +++++++- lvm.c | 2 ++ manual/manual.of | 21 +++++++++------------ testes/coroutine.lua | 8 +++----- testes/events.lua | 34 +++++++++++++++------------------- 8 files changed, 44 insertions(+), 40 deletions(-) diff --git a/lstate.h b/lstate.h index 2a36bd9632..5461b29119 100644 --- a/lstate.h +++ b/lstate.h @@ -138,9 +138,11 @@ typedef struct CallInfo { #define CIST_YPCALL (1<<3) /* call is a yieldable protected call */ #define CIST_TAIL (1<<4) /* call was tail called */ #define CIST_HOOKYIELD (1<<5) /* last hook called yielded */ -#define CIST_LEQ (1<<6) /* using __lt for __le */ -#define CIST_FIN (1<<7) /* call is running a finalizer */ -#define CIST_TRAN (1<<8) /* 'ci' has transfer information */ +#define CIST_FIN (1<<6) /* call is running a finalizer */ +#define CIST_TRAN (1<<7) /* 'ci' has transfer information */ +#if defined(LUA_COMPAT_LT_LE) +#define CIST_LEQ (1<<8) /* using __lt for __le */ +#endif /* active function is a Lua function */ #define isLua(ci) (!((ci)->callstatus & CIST_C)) diff --git a/ltests.h b/ltests.h index 54bc4f5f7e..d44974a444 100644 --- a/ltests.h +++ b/ltests.h @@ -13,6 +13,7 @@ /* test Lua with compatibility code */ #define LUA_COMPAT_MATHLIB +#define LUA_COMPAT_LT_LE #define LUA_DEBUG diff --git a/ltm.c b/ltm.c index 1c1a18b7a3..5c148180aa 100644 --- a/ltm.c +++ b/ltm.c @@ -188,6 +188,7 @@ int luaT_callorderTM (lua_State *L, const TValue *p1, const TValue *p2, TMS event) { if (callbinTM(L, p1, p2, L->top, event)) /* try original event */ return !l_isfalse(s2v(L->top)); +#if defined(LUA_COMPAT_LT_LE) else if (event == TM_LE) { /* try '!(p2 < p1)' for '(p1 <= p2)' */ L->ci->callstatus |= CIST_LEQ; /* mark it is doing 'lt' for 'le' */ @@ -197,6 +198,7 @@ int luaT_callorderTM (lua_State *L, const TValue *p1, const TValue *p2, } /* else error will remove this 'ci'; no need to clear mark */ } +#endif luaG_ordererror(L, p1, p2); /* no metamethod found */ return 0; /* to avoid warnings */ } diff --git a/luaconf.h b/luaconf.h index cc8e1bdcff..126257dce3 100644 --- a/luaconf.h +++ b/luaconf.h @@ -295,7 +295,7 @@ */ /* -@@ LUA_COMPAT_5_3 controls other macros for compatibility with Lua 5.2. +@@ LUA_COMPAT_5_3 controls other macros for compatibility with Lua 5.3. ** You can define it to get all options, or change specific options ** to fit your specific needs. */ @@ -316,6 +316,12 @@ */ #define LUA_COMPAT_APIINTCASTS +/* +@@ LUA_COMPAT_LT_LE controls the emulation of the '__le' metamethod +** using '__lt'. +*/ +#define LUA_COMPAT_LT_LE + #endif /* } */ diff --git a/lvm.c b/lvm.c index 7550dcc8c0..add269422c 100644 --- a/lvm.c +++ b/lvm.c @@ -754,10 +754,12 @@ void luaV_finishOp (lua_State *L) { case OP_EQ: { /* note that 'OP_EQI'/'OP_EQK' cannot yield */ int res = !l_isfalse(s2v(L->top - 1)); L->top--; +#if defined(LUA_COMPAT_LT_LE) if (ci->callstatus & CIST_LEQ) { /* "<=" using "<" instead? */ ci->callstatus ^= CIST_LEQ; /* clear mark */ res = !res; /* negate result */ } +#endif lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_JMP); if (res != GETARG_k(inst)) /* condition failed? */ ci->u.l.savedpc++; /* skip jump instruction */ diff --git a/manual/manual.of b/manual/manual.of index 5a8b1b2c28..47a551bfa2 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -474,17 +474,7 @@ The result of the call is always converted to a boolean. @item{@idx{__le}| the less equal (@T{<=}) operation. -Unlike other operations, -the less-equal operation can use two different events. -First, Lua looks for the @idx{__le} metamethod in both operands, -like in the less than operation. -If it cannot find such a metamethod, -then it will try the @idx{__lt} metamethod, -assuming that @T{a <= b} is equivalent to @T{not (b < a)}. -As with the other comparison operators, -the result is always a boolean. -(This use of the @idx{__lt} event can be removed in future versions; -it is also slower than a real @idx{__le} metamethod.) +Behavior similar to the less than operation. } @item{@idx{__index}| @@ -1643,7 +1633,8 @@ all operations @emphx{wrap around}, according to the usual rules of two-complement arithmetic. (In other words, they return the unique representable integer -that is equal modulo @M{2@sp{64}} to the mathematical result.) +that is equal modulo @M{2@sp{n}} to the mathematical result, +where @M{n} is the number of bits of the integer type.) } @sect3{bitwise| @title{Bitwise Operators} @@ -8537,6 +8528,12 @@ For instance, the result of @T{"1" + "2"} now is an integer, not a float. } +@item{ +The use of the @idx{__lt} metamethod to emulate @id{__le} +has been removed. +When needed, this metamethod must be explicitly defined. +} + } } diff --git a/testes/coroutine.lua b/testes/coroutine.lua index 182c1e185e..36eae44aea 100644 --- a/testes/coroutine.lua +++ b/testes/coroutine.lua @@ -1,4 +1,4 @@ --- $Id: testes/coroutine.lua $ +-- $Id: testes/coroutine.lua 2018-07-25 15:31:04 -0300 $ -- See Copyright Notice in file all.lua print "testing coroutines" @@ -619,10 +619,8 @@ end assert(run(function () if (a>=b) then return '>=' else return '<' end end, {"le", "sub"}) == "<") --- '<=' using '<' -mt.__le = nil assert(run(function () if (a<=b) then return '<=' else return '>' end end, - {"lt"}) == "<=") + {"le", "sub"}) == "<=") assert(run(function () if (a==b) then return '==' else return '~=' end end, {"eq"}) == "~=") @@ -677,7 +675,7 @@ do -- a few more tests for comparsion operators return val(a) < val(b) end, } - local mt2 = { __lt = mt1.__lt } -- no __le + local mt2 = { __lt = mt1.__lt, __le = mt1.__le } local function run (f) local co = coroutine.wrap(f) diff --git a/testes/events.lua b/testes/events.lua index 21a822a7cf..c4d43d5183 100644 --- a/testes/events.lua +++ b/testes/events.lua @@ -1,4 +1,4 @@ --- $Id: testes/events.lua $ +-- $Id: testes/events.lua 2018-07-25 15:31:04 -0300 $ -- See Copyright Notice in file all.lua print('testing metatables') @@ -217,6 +217,13 @@ t.__lt = function (a,b,c) return a= Set{1,2,3,4})) -assert((Set{1,3} <= Set{3,5})) -- wrong!! model needs a `le' method ;-) - t.__le = function (a,b) for k in pairs(a) do if not b[k] then return false end @@ -281,10 +271,15 @@ t.__le = function (a,b) return true end -assert(not (Set{1,3} <= Set{3,5})) -- now its OK! +assert(Set{1,2,3} < Set{1,2,3,4}) +assert(not(Set{1,2,3,4} < Set{1,2,3,4})) +assert((Set{1,2,3,4} <= Set{1,2,3,4})) +assert((Set{1,2,3,4} >= Set{1,2,3,4})) +assert(not (Set{1,3} <= Set{3,5})) assert(not(Set{1,3} <= Set{3,5})) assert(not(Set{1,3} >= Set{3,5})) + t.__eq = function (a,b) for k in pairs(a) do if not b[k] then return false end @@ -376,6 +371,7 @@ t1 = {}; c = {}; setmetatable(c, t1) d = {} t1.__eq = function () return true end t1.__lt = function () return true end +t1.__le = function () return false end setmetatable(d, t1) assert(c == d and c < d and not(d <= c)) t2 = {} From 5382a22e0eea878339c504b2a9a3b36bcd839fcc Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 28 Aug 2018 12:36:58 -0300 Subject: [PATCH 0328/1145] Corrections in the implementation of '%' for floats. The multiplication (m*b) used to test whether 'm' is non-zero and 'm' and 'b' have different signs can underflow for very small numbers, giving a wrong result. The use of explicit comparisons solves this problem. This commit also adds several new tests for '%' (both for floats and for integers) to exercise more corner cases, such as very large and very small values. --- llimits.h | 14 ++++++----- lobject.c | 6 +---- lvm.c | 18 +++++++++----- lvm.h | 1 + testes/math.lua | 66 ++++++++++++++++++++++++++++++++++++++++++++++++- 5 files changed, 87 insertions(+), 18 deletions(-) diff --git a/llimits.h b/llimits.h index 6afa8997e0..e91310a09f 100644 --- a/llimits.h +++ b/llimits.h @@ -293,15 +293,17 @@ typedef unsigned long Instruction; #endif /* -** modulo: defined as 'a - floor(a/b)*b'; this definition gives NaN when -** 'b' is huge, but the result should be 'a'. 'fmod' gives the result of -** 'a - trunc(a/b)*b', and therefore must be corrected when 'trunc(a/b) -** ~= floor(a/b)'. That happens when the division has a non-integer -** negative result, which is equivalent to the test below. +** modulo: defined as 'a - floor(a/b)*b'; the direct computation +** using this definition has several problems with rounding errors, +** so it is better to use 'fmod'. 'fmod' gives the result of +** 'a - trunc(a/b)*b', and therefore must be corrected when +** 'trunc(a/b) ~= floor(a/b)'. That happens when the division has a +** non-integer negative result, which is equivalent to the tests below. */ #if !defined(luai_nummod) #define luai_nummod(L,a,b,m) \ - { (m) = l_mathop(fmod)(a,b); if ((m)*(b) < 0) (m) += (b); } + { (void)L; (m) = l_mathop(fmod)(a,b); \ + if (((m) > 0) ? (b) < 0 : ((m) < 0 && (b) > 0)) (m) += (b); } #endif /* exponentiation */ diff --git a/lobject.c b/lobject.c index 79cf55b8e1..d011c85fc3 100644 --- a/lobject.c +++ b/lobject.c @@ -106,11 +106,7 @@ static lua_Number numarith (lua_State *L, int op, lua_Number v1, case LUA_OPPOW: return luai_numpow(L, v1, v2); case LUA_OPIDIV: return luai_numidiv(L, v1, v2); case LUA_OPUNM: return luai_numunm(L, v1); - case LUA_OPMOD: { - lua_Number m; - luai_nummod(L, v1, v2, m); - return m; - } + case LUA_OPMOD: return luaV_modf(L, v1, v2); default: lua_assert(0); return 0; } } diff --git a/lvm.c b/lvm.c index add269422c..dd6a660bd0 100644 --- a/lvm.c +++ b/lvm.c @@ -655,6 +655,16 @@ lua_Integer luaV_mod (lua_State *L, lua_Integer m, lua_Integer n) { } +/* +** Float modulus +*/ +lua_Number luaV_modf (lua_State *L, lua_Number m, lua_Number n) { + lua_Number r; + luai_nummod(L, m, n, r); + return r; +} + + /* number of bits in an integer */ #define NBITS cast_int(sizeof(lua_Integer) * CHAR_BIT) @@ -1142,10 +1152,8 @@ void luaV_execute (lua_State *L, CallInfo *ci) { setivalue(s2v(ra), luaV_mod(L, ivalue(rb), ic)); } else if (tonumberns(rb, nb)) { - lua_Number m; lua_Number nc = cast_num(ic); - luai_nummod(L, nb, nc, m); - setfltvalue(s2v(ra), m); + setfltvalue(s2v(ra), luaV_modf(L, nb, nc)); } else Protect(luaT_trybiniTM(L, rb, ic, 0, ra, TM_MOD)); @@ -1370,9 +1378,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { setivalue(s2v(ra), luaV_mod(L, ib, ic)); } else if (tonumberns(rb, nb) && tonumberns(rc, nc)) { - lua_Number m; - luai_nummod(L, nb, nc, m); - setfltvalue(s2v(ra), m); + setfltvalue(s2v(ra), luaV_modf(L, nb, nc)); } else Protect(luaT_trybinTM(L, rb, rc, ra, TM_MOD)); diff --git a/lvm.h b/lvm.h index d58daacdc0..8ead0c50b3 100644 --- a/lvm.h +++ b/lvm.h @@ -116,6 +116,7 @@ LUAI_FUNC void luaV_execute (lua_State *L, CallInfo *ci); LUAI_FUNC void luaV_concat (lua_State *L, int total); LUAI_FUNC lua_Integer luaV_div (lua_State *L, lua_Integer x, lua_Integer y); LUAI_FUNC lua_Integer luaV_mod (lua_State *L, lua_Integer x, lua_Integer y); +LUAI_FUNC lua_Number luaV_modf (lua_State *L, lua_Number x, lua_Number y); LUAI_FUNC lua_Integer luaV_shiftl (lua_Integer x, lua_Integer y); LUAI_FUNC void luaV_objlen (lua_State *L, StkId ra, const TValue *rb); diff --git a/testes/math.lua b/testes/math.lua index 853dc20fb4..b387977e8d 100644 --- a/testes/math.lua +++ b/testes/math.lua @@ -1,4 +1,4 @@ --- $Id: testes/math.lua $ +-- $Id: testes/math.lua 2018-07-25 15:31:04 -0300 $ -- See Copyright Notice in file all.lua print("testing numbers and math lib") @@ -541,9 +541,73 @@ assert(eqT(-4 % 3, 2)) assert(eqT(4 % -3, -2)) assert(eqT(-4.0 % 3, 2.0)) assert(eqT(4 % -3.0, -2.0)) +assert(eqT(4 % -5, -1)) +assert(eqT(4 % -5.0, -1.0)) +assert(eqT(4 % 5, 4)) +assert(eqT(4 % 5.0, 4.0)) +assert(eqT(-4 % -5, -4)) +assert(eqT(-4 % -5.0, -4.0)) +assert(eqT(-4 % 5, 1)) +assert(eqT(-4 % 5.0, 1.0)) +assert(eqT(4.25 % 4, 0.25)) +assert(eqT(10.0 % 2, 0.0)) +assert(eqT(-10.0 % 2, 0.0)) +assert(eqT(-10.0 % -2, 0.0)) assert(math.pi - math.pi % 1 == 3) assert(math.pi - math.pi % 0.001 == 3.141) +do -- very small numbers + local i, j = 0, 20000 + while i < j do + local m = (i + j) // 2 + if 10^-m > 0 then + i = m + 1 + else + j = m + end + end + -- 'i' is the smallest possible ten-exponent + local b = 10^-(i - (i // 10)) -- a very small number + assert(b > 0 and b * b == 0) + local delta = b / 1000 + assert(eq((2.1 * b) % (2 * b), (0.1 * b), delta)) + assert(eq((-2.1 * b) % (2 * b), (2 * b) - (0.1 * b), delta)) + assert(eq((2.1 * b) % (-2 * b), (0.1 * b) - (2 * b), delta)) + assert(eq((-2.1 * b) % (-2 * b), (-0.1 * b), delta)) +end + + +-- basic consistency between integer modulo and float modulo +for i = -10, 10 do + for j = -10, 10 do + if j ~= 0 then + assert((i + 0.0) % j == i % j) + end + end +end + +for i = 0, 10 do + for j = -10, 10 do + if j ~= 0 then + assert((2^i) % j == (1 << i) % j) + end + end +end + +do -- precision of module for large numbers + local i = 10 + while (1 << i) > 0 do + assert((1 << i) % 3 == i % 2 + 1) + i = i + 1 + end + + i = 10 + while 2^i < math.huge do + assert(2^i % 3 == i % 2 + 1) + i = i + 1 + end +end + assert(eqT(minint % minint, 0)) assert(eqT(maxint % maxint, 0)) assert((minint + 1) % minint == minint + 1) From 9cbf17b0f1bb4001b237c4027b271f0db9bde62c Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 11 Sep 2018 08:39:12 -0300 Subject: [PATCH 0329/1145] Details (comments) --- lapi.c | 6 +++++- llimits.h | 5 ++++- lparser.c | 4 ++-- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/lapi.c b/lapi.c index 0ca9a32145..ae6b07aebd 100644 --- a/lapi.c +++ b/lapi.c @@ -38,7 +38,11 @@ const char lua_ident[] = -/* test for a valid index */ +/* +** Test for a valid index. +** '!ttisnil(o)' implies 'o != &G(L)->nilvalue', so it is not needed. +** However, it covers the most common cases in a faster way. +*/ #define isvalid(L, o) (!ttisnil(o) || o != &G(L)->nilvalue) diff --git a/llimits.h b/llimits.h index e91310a09f..8ab58b5ce9 100644 --- a/llimits.h +++ b/llimits.h @@ -298,7 +298,10 @@ typedef unsigned long Instruction; ** so it is better to use 'fmod'. 'fmod' gives the result of ** 'a - trunc(a/b)*b', and therefore must be corrected when ** 'trunc(a/b) ~= floor(a/b)'. That happens when the division has a -** non-integer negative result, which is equivalent to the tests below. +** non-integer negative result: non-integer result is equivalent to +** a non-zero remainder 'm'; negative result is equivalent to 'a' and +** 'b' with different signs, or 'm' and 'b' with different signs +** (as the result 'm' of 'fmod' has the same sign of 'a'). */ #if !defined(luai_nummod) #define luai_nummod(L,a,b,m) \ diff --git a/lparser.c b/lparser.c index 9f156dd973..32500b023a 100644 --- a/lparser.c +++ b/lparser.c @@ -1171,9 +1171,9 @@ static void assignment (LexState *ls, struct LHS_assign *lh, int nvars) { suffixedexp(ls, &nv.v); if (!vkisindexed(nv.v.k)) check_conflict(ls, lh, &nv.v); - luaE_incCcalls(ls->L); /* control recursion depth */ + enterlevel(ls); /* control recursion depth */ assignment(ls, &nv, nvars+1); - ls->L->nCcalls--; + leavelevel(ls); } else { /* assignment -> '=' explist */ int nexps; From b114c7d4871051cbdd7af185a61f35fe4028da79 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 11 Sep 2018 14:24:14 -0300 Subject: [PATCH 0330/1145] Added "cost" for the use of C stack by a coroutine invocation. Resuming a coroutine uses more C stack than other operations (such as function calls or recursive syntax). So, to avoid stack overflow in recursive coroutine invocations, either LUAI_MAXCCALLS must be too small or a coroutine invocation must "pay" a higher price. New constant LUAL_COROCSTK ("COROutine C STaK") defines how much is this price. --- ldo.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/ldo.c b/ldo.c index ebb35c971c..0d68d36c00 100644 --- a/ldo.c +++ b/ldo.c @@ -610,6 +610,13 @@ static int resume_error (lua_State *L, const char *msg, int narg) { } +/* +** "Cost" in the C stack for a coroutine invocation. +*/ +#if !defined(LUAL_COROCSTK) +#define LUAL_COROCSTK 3 +#endif + /* ** Do the work for 'lua_resume' in protected mode. Most of the work ** depends on the status of the coroutine: initial state, suspended @@ -642,7 +649,6 @@ static void resume (lua_State *L, void *ud) { } } - LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs, int *nresults) { int status; @@ -657,7 +663,7 @@ LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs, if (from == NULL) L->nCcalls = 1; else /* correct 'nCcalls' for this thread */ - L->nCcalls = from->nCcalls - from->nci + L->nci + 1; + L->nCcalls = from->nCcalls - from->nci + L->nci + LUAL_COROCSTK; if (L->nCcalls >= LUAI_MAXCCALLS) return resume_error(L, "C stack overflow", nargs); luai_userstateresume(L, nargs); From 4cd1f4aac01184765818e0cebf02da454ccf6590 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 8 Oct 2018 10:42:07 -0300 Subject: [PATCH 0331/1145] Towards "to closed" local variables Start of the implementation of "scoped variables" or "to be closed" variables, local variables whose '__close' (or themselves) are called when they go out of scope. This commit implements the syntax, the opcode, and the creation of the corresponding upvalue, but it still does not call the finalizations when the variable goes out of scope (the most important part). Currently, the syntax is 'local scoped name = exp', but that will probably change. --- lcode.c | 4 ++-- ldo.c | 3 +-- lgc.c | 4 +++- ljumptab.h | 1 + lobject.h | 4 ++++ lopcodes.c | 1 + lopcodes.h | 1 + lopnames.h | 1 + lparser.c | 34 ++++++++++++++++++++++++++++++---- lparser.h | 1 + lstate.h | 3 ++- ltests.c | 25 ++++++++++++++----------- lvm.c | 6 ++++++ testes/code.lua | 6 +++++- testes/locals.lua | 9 +++++++++ 15 files changed, 81 insertions(+), 22 deletions(-) diff --git a/lcode.c b/lcode.c index d00038dd7a..e84b85ac5f 100644 --- a/lcode.c +++ b/lcode.c @@ -1673,13 +1673,13 @@ void luaK_finish (FuncState *fs) { lua_assert(i == 0 || isOT(*(pc - 1)) == isIT(*pc)); switch (GET_OPCODE(*pc)) { case OP_RETURN0: case OP_RETURN1: { - if (p->sizep == 0 && !p->is_vararg) + if (!(fs->needclose || p->is_vararg)) break; /* no extra work */ /* else use OP_RETURN to do the extra work */ SET_OPCODE(*pc, OP_RETURN); } /* FALLTHROUGH */ case OP_RETURN: case OP_TAILCALL: { - if (p->sizep > 0 || p->is_vararg) { + if (fs->needclose || p->is_vararg) { SETARG_C(*pc, p->is_vararg ? p->numparams + 1 : 0); SETARG_k(*pc, 1); /* signal that there is extra work */ } diff --git a/ldo.c b/ldo.c index 0d68d36c00..2349aaed16 100644 --- a/ldo.c +++ b/ldo.c @@ -91,8 +91,7 @@ struct lua_longjmp { static void seterrorobj (lua_State *L, int errcode, StkId oldtop) { switch (errcode) { case LUA_ERRMEM: { /* memory error? */ - TString *memerrmsg = luaS_newliteral(L, MEMERRMSG); - setsvalue2s(L, oldtop, memerrmsg); /* reuse preregistered msg. */ + setsvalue2s(L, oldtop, G(L)->memerrmsg); /* reuse preregistered msg. */ break; } case LUA_ERRERR: { diff --git a/lgc.c b/lgc.c index e8429e1b38..39b3ab7397 100644 --- a/lgc.c +++ b/lgc.c @@ -293,7 +293,8 @@ static void reallymarkobject (global_State *g, GCObject *o) { gray2black(o); break; } - case LUA_TUPVAL: { + case LUA_TUPVAL: + case LUA_TUPVALTBC: { UpVal *uv = gco2upv(o); if (!upisopen(uv)) /* open upvalues are kept gray */ gray2black(o); @@ -760,6 +761,7 @@ static void freeobj (lua_State *L, GCObject *o) { luaF_freeproto(L, gco2p(o)); break; case LUA_TUPVAL: + case LUA_TUPVALTBC: freeupval(L, gco2upv(o)); break; case LUA_TLCL: diff --git a/ljumptab.h b/ljumptab.h index c775f10a4a..da4cf7b70d 100644 --- a/ljumptab.h +++ b/ljumptab.h @@ -74,6 +74,7 @@ static void *disptab[] = { &&L_OP_LEN, &&L_OP_CONCAT, &&L_OP_CLOSE, +&&L_OP_TBC, &&L_OP_JMP, &&L_OP_EQ, &&L_OP_LT, diff --git a/lobject.h b/lobject.h index ddcf609cf6..ea3511deba 100644 --- a/lobject.h +++ b/lobject.h @@ -588,6 +588,10 @@ typedef struct UpVal { } UpVal; +/* variant for "To Be Closed" upvalues */ +#define LUA_TUPVALTBC (LUA_TUPVAL | (1 << 4)) + + #define ClosureHeader \ CommonHeader; lu_byte nupvalues; GCObject *gclist diff --git a/lopcodes.c b/lopcodes.c index 95347b2728..f6915beb88 100644 --- a/lopcodes.c +++ b/lopcodes.c @@ -68,6 +68,7 @@ LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = { ,opmode(0, 0, 0, 1, iABC) /* OP_LEN */ ,opmode(0, 0, 0, 1, iABC) /* OP_CONCAT */ ,opmode(0, 0, 0, 0, iABC) /* OP_CLOSE */ + ,opmode(0, 0, 0, 0, iABC) /* OP_TBC */ ,opmode(0, 0, 0, 0, isJ) /* OP_JMP */ ,opmode(0, 0, 1, 0, iABC) /* OP_EQ */ ,opmode(0, 0, 1, 0, iABC) /* OP_LT */ diff --git a/lopcodes.h b/lopcodes.h index 9442a33689..4d144000fe 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -251,6 +251,7 @@ OP_LEN,/* A B R(A) := length of R(B) */ OP_CONCAT,/* A B R(A) := R(A).. ... ..R(A + B - 1) */ OP_CLOSE,/* A close all upvalues >= R(A) */ +OP_TBC,/* A mark variable A "to be closed" */ OP_JMP,/* k sJ pc += sJ (k is used in code generation) */ OP_EQ,/* A B if ((R(A) == R(B)) ~= k) then pc++ */ OP_LT,/* A B if ((R(A) < R(B)) ~= k) then pc++ */ diff --git a/lopnames.h b/lopnames.h index c40eaeae26..304d3b6cac 100644 --- a/lopnames.h +++ b/lopnames.h @@ -59,6 +59,7 @@ static const char *const opnames[] = { "LEN", "CONCAT", "CLOSE", + "TBC", "JMP", "EQ", "LT", diff --git a/lparser.c b/lparser.c index 32500b023a..84abeb90b7 100644 --- a/lparser.c +++ b/lparser.c @@ -255,6 +255,7 @@ static void markupval (FuncState *fs, int level) { while (bl->nactvar > level) bl = bl->previous; bl->upval = 1; + fs->needclose = 1; } @@ -547,6 +548,7 @@ static void open_func (LexState *ls, FuncState *fs, BlockCnt *bl) { fs->nups = 0; fs->nlocvars = 0; fs->nactvar = 0; + fs->needclose = 0; fs->firstlocal = ls->dyd->actvar.n; fs->bl = NULL; f->source = ls->source; @@ -1509,15 +1511,16 @@ static void localfunc (LexState *ls) { } -static void localstat (LexState *ls) { +static void commonlocalstat (LexState *ls, TString *firstvar) { /* stat -> LOCAL NAME {',' NAME} ['=' explist] */ - int nvars = 0; + int nvars = 1; int nexps; expdesc e; - do { + new_localvar(ls, firstvar); + while (testnext(ls, ',')) { new_localvar(ls, str_checkname(ls)); nvars++; - } while (testnext(ls, ',')); + } if (testnext(ls, '=')) nexps = explist(ls, &e); else { @@ -1529,6 +1532,29 @@ static void localstat (LexState *ls) { } +static void scopedlocalstat (LexState *ls) { + FuncState *fs = ls->fs; + new_localvar(ls, str_checkname(ls)); + checknext(ls, '='); + luaK_codeABC(fs, OP_TBC, fs->nactvar, 0, 0); + markupval(fs, fs->nactvar); + exp1(ls, 0); + adjustlocalvars(ls, 1); +} + + +static void localstat (LexState *ls) { + /* stat -> LOCAL NAME {',' NAME} ['=' explist] + | LOCAL SCOPED NAME '=' exp */ + TString *firstvar = str_checkname(ls); + if (ls->t.token == TK_NAME && + eqshrstr(firstvar, luaS_newliteral(ls->L, "scoped"))) + scopedlocalstat(ls); + else + commonlocalstat(ls, firstvar); +} + + static int funcname (LexState *ls, expdesc *v) { /* funcname -> NAME {fieldsel} [':' NAME] */ int ismethod = 0; diff --git a/lparser.h b/lparser.h index e158c9d918..1b94a97a6f 100644 --- a/lparser.h +++ b/lparser.h @@ -133,6 +133,7 @@ typedef struct FuncState { lu_byte nups; /* number of upvalues */ lu_byte freereg; /* first free register */ lu_byte iwthabs; /* instructions issued since last absolute line info */ + lu_byte needclose; /* function needs to close upvalues when returning */ } FuncState; diff --git a/lstate.h b/lstate.h index 5461b29119..f08c23554c 100644 --- a/lstate.h +++ b/lstate.h @@ -267,7 +267,8 @@ union GCUnion { #define gco2t(o) check_exp((o)->tt == LUA_TTABLE, &((cast_u(o))->h)) #define gco2p(o) check_exp((o)->tt == LUA_TPROTO, &((cast_u(o))->p)) #define gco2th(o) check_exp((o)->tt == LUA_TTHREAD, &((cast_u(o))->th)) -#define gco2upv(o) check_exp((o)->tt == LUA_TUPVAL, &((cast_u(o))->upv)) +#define gco2upv(o) \ + check_exp(novariant((o)->tt) == LUA_TUPVAL, &((cast_u(o))->upv)) /* diff --git a/ltests.c b/ltests.c index bc71d937f5..ff962543cb 100644 --- a/ltests.c +++ b/ltests.c @@ -357,7 +357,8 @@ static void checkrefs (global_State *g, GCObject *o) { checkudata(g, gco2u(o)); break; } - case LUA_TUPVAL: { + case LUA_TUPVAL: + case LUA_TUPVALTBC: { checkvalref(g, o, gco2upv(o)->v); break; } @@ -522,35 +523,37 @@ int lua_checkmemory (lua_State *L) { static char *buildop (Proto *p, int pc, char *buff) { + char *obuff = buff; Instruction i = p->code[pc]; OpCode o = GET_OPCODE(i); const char *name = opnames[o]; int line = luaG_getfuncline(p, pc); int lineinfo = (p->lineinfo != NULL) ? p->lineinfo[pc] : 0; - sprintf(buff, "(%2d - %4d) %4d - ", lineinfo, line, pc); + if (lineinfo == ABSLINEINFO) + buff += sprintf(buff, "(__"); + else + buff += sprintf(buff, "(%2d", lineinfo); + buff += sprintf(buff, " - %4d) %4d - ", line, pc); switch (getOpMode(o)) { case iABC: - sprintf(buff+strlen(buff), "%-12s%4d %4d %4d%s", name, + sprintf(buff, "%-12s%4d %4d %4d%s", name, GETARG_A(i), GETARG_B(i), GETARG_C(i), GETARG_k(i) ? " (k)" : ""); break; case iABx: - sprintf(buff+strlen(buff), "%-12s%4d %4d", name, GETARG_A(i), - GETARG_Bx(i)); + sprintf(buff, "%-12s%4d %4d", name, GETARG_A(i), GETARG_Bx(i)); break; case iAsBx: - sprintf(buff+strlen(buff), "%-12s%4d %4d", name, GETARG_A(i), - GETARG_sBx(i)); + sprintf(buff, "%-12s%4d %4d", name, GETARG_A(i), GETARG_sBx(i)); break; case iAx: - sprintf(buff+strlen(buff), "%-12s%4d", name, GETARG_Ax(i)); + sprintf(buff, "%-12s%4d", name, GETARG_Ax(i)); break; case isJ: - sprintf(buff+strlen(buff), "%-12s%4d (%1d)", name, GETARG_sJ(i), - !!GETARG_m(i)); + sprintf(buff, "%-12s%4d (%1d)", name, GETARG_sJ(i), !!GETARG_m(i)); break; } - return buff; + return obuff; } diff --git a/lvm.c b/lvm.c index dd6a660bd0..fdd99a4282 100644 --- a/lvm.c +++ b/lvm.c @@ -1455,6 +1455,12 @@ void luaV_execute (lua_State *L, CallInfo *ci) { luaF_close(L, ra); vmbreak; } + vmcase(OP_TBC) { + UpVal *up = luaF_findupval(L, ra); /* create new upvalue */ + up->tt = LUA_TUPVALTBC; /* mark it to be closed */ + setnilvalue(s2v(ra)); /* intialize it with nil */ + vmbreak; + } vmcase(OP_JMP) { dojump(ci, i, 0); vmbreak; diff --git a/testes/code.lua b/testes/code.lua index 6bd6ebfa8f..ad48448594 100644 --- a/testes/code.lua +++ b/testes/code.lua @@ -64,8 +64,12 @@ end -- some basic instructions -check(function () +check(function () -- function does not create upvalues (function () end){f()} +end, 'CLOSURE', 'NEWTABLE', 'GETTABUP', 'CALL', 'SETLIST', 'CALL', 'RETURN0') + +check(function (x) -- function creates upvalues + (function () return x end){f()} end, 'CLOSURE', 'NEWTABLE', 'GETTABUP', 'CALL', 'SETLIST', 'CALL', 'RETURN') diff --git a/testes/locals.lua b/testes/locals.lua index 14e49a7ce6..20ecae4bc5 100644 --- a/testes/locals.lua +++ b/testes/locals.lua @@ -173,6 +173,15 @@ end assert(x==20) +-- tests for to-be-closed variables +do + local scoped x = 3 + local a + local scoped y = 5 + assert(x == 3 and y == 5) +end + + print('OK') return 5,f From bd96330d037660d9a1769c6c0d989f017e5f0278 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 17 Oct 2018 10:44:42 -0300 Subject: [PATCH 0332/1145] First "complete" implementation of to-be-closed variables Still missing: - handling of memory errors when creating upvalue (must run closing method all the same) - interaction with coroutines --- ldo.c | 20 +++++++++------ ldo.h | 1 + lfunc.c | 47 +++++++++++++++++++++++++++++++--- lfunc.h | 2 +- lgc.c | 5 ++++ lparser.c | 2 +- lstate.c | 4 +-- ltests.c | 2 +- ltm.c | 2 +- ltm.h | 1 + lvm.c | 7 +++--- testes/api.lua | 14 ++++++++++- testes/locals.lua | 64 +++++++++++++++++++++++++++++++++++++++++++---- 13 files changed, 145 insertions(+), 26 deletions(-) diff --git a/ldo.c b/ldo.c index 2349aaed16..f2d12f0430 100644 --- a/ldo.c +++ b/ldo.c @@ -88,7 +88,7 @@ struct lua_longjmp { }; -static void seterrorobj (lua_State *L, int errcode, StkId oldtop) { +void luaD_seterrorobj (lua_State *L, int errcode, StkId oldtop) { switch (errcode) { case LUA_ERRMEM: { /* memory error? */ setsvalue2s(L, oldtop, G(L)->memerrmsg); /* reuse preregistered msg. */ @@ -121,7 +121,7 @@ l_noret luaD_throw (lua_State *L, int errcode) { } else { /* no handler at all; abort */ if (g->panic) { /* panic function? */ - seterrorobj(L, errcode, L->top); /* assume EXTRA_STACK */ + luaD_seterrorobj(L, errcode, L->top); /* assume EXTRA_STACK */ if (L->ci->top < L->top) L->ci->top = L->top; /* pushing msg. can break this invariant */ lua_unlock(L); @@ -584,8 +584,8 @@ static int recover (lua_State *L, int status) { if (ci == NULL) return 0; /* no recovery point */ /* "finish" luaD_pcall */ oldtop = restorestack(L, ci->u2.funcidx); - luaF_close(L, oldtop); - seterrorobj(L, status, oldtop); + luaF_close(L, oldtop, status); + luaD_seterrorobj(L, status, oldtop); L->ci = ci; L->allowhook = getoah(ci->callstatus); /* restore original 'allowhook' */ L->nny = 0; /* should be zero to be yieldable */ @@ -678,7 +678,7 @@ LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs, } if (unlikely(errorstatus(status))) { /* unrecoverable error? */ L->status = cast_byte(status); /* mark thread as 'dead' */ - seterrorobj(L, status, L->top); /* push error message */ + luaD_seterrorobj(L, status, L->top); /* push error message */ L->ci->top = L->top; } else lua_assert(status == L->status); /* normal end or yield */ @@ -726,6 +726,11 @@ LUA_API int lua_yieldk (lua_State *L, int nresults, lua_KContext ctx, } +/* +** Call the C function 'func' in protected mode, restoring basic +** thread information ('allowhook', 'nny', etc.) and in particular +** its stack level in case of errors. +*/ int luaD_pcall (lua_State *L, Pfunc func, void *u, ptrdiff_t old_top, ptrdiff_t ef) { int status; @@ -737,11 +742,12 @@ int luaD_pcall (lua_State *L, Pfunc func, void *u, status = luaD_rawrunprotected(L, func, u); if (unlikely(status != LUA_OK)) { /* an error occurred? */ StkId oldtop = restorestack(L, old_top); - luaF_close(L, oldtop); /* close possible pending closures */ - seterrorobj(L, status, oldtop); L->ci = old_ci; L->allowhook = old_allowhooks; L->nny = old_nny; + status = luaF_close(L, oldtop, status); + oldtop = restorestack(L, old_top); /* previous call may change stack */ + luaD_seterrorobj(L, status, oldtop); luaD_shrinkstack(L); } L->errfunc = old_errfunc; diff --git a/ldo.h b/ldo.h index c836a2a174..7760f853b2 100644 --- a/ldo.h +++ b/ldo.h @@ -50,6 +50,7 @@ /* type of protected functions, to be ran by 'runprotected' */ typedef void (*Pfunc) (lua_State *L, void *ud); +LUAI_FUNC void luaD_seterrorobj (lua_State *L, int errcode, StkId oldtop); LUAI_FUNC int luaD_protectedparser (lua_State *L, ZIO *z, const char *name, const char *mode); LUAI_FUNC void luaD_hook (lua_State *L, int event, int line, diff --git a/lfunc.c b/lfunc.c index 16e007318d..fde72b8c46 100644 --- a/lfunc.c +++ b/lfunc.c @@ -14,6 +14,7 @@ #include "lua.h" +#include "ldo.h" #include "lfunc.h" #include "lgc.h" #include "lmem.h" @@ -83,6 +84,40 @@ UpVal *luaF_findupval (lua_State *L, StkId level) { } +static void callclose (lua_State *L, void *ud) { + luaD_callnoyield(L, cast(StkId, ud), 0); +} + + +static int closeupval (lua_State *L, UpVal *uv, StkId level, int status) { + StkId func = level + 1; /* save slot for old error message */ + if (status != LUA_OK) /* was there an error? */ + luaD_seterrorobj(L, status, level); /* save error message */ + else + setnilvalue(s2v(level)); + if (ttisfunction(uv->v)) { /* object to-be-closed is a function? */ + setobj2s(L, func, uv->v); /* will call it */ + setobjs2s(L, func + 1, level); /* error msg. as argument */ + } + else { /* try '__close' metamethod */ + const TValue *tm = luaT_gettmbyobj(L, uv->v, TM_CLOSE); + if (ttisnil(tm)) + return status; /* no metamethod */ + setobj2s(L, func, tm); /* will call metamethod */ + setobj2s(L, func + 1, uv->v); /* with 'self' as argument */ + } + L->top = func + 2; /* add function and argument */ + if (status == LUA_OK) /* not in "error mode"? */ + callclose(L, func); /* call closing method */ + else { /* already inside error handler; cannot raise another error */ + int newstatus = luaD_pcall(L, callclose, func, savestack(L, level), 0); + if (newstatus != LUA_OK) /* error when closing? */ + status = newstatus; /* this will be the new error */ + } + return status; +} + + void luaF_unlinkupval (UpVal *uv) { lua_assert(upisopen(uv)); *uv->u.open.previous = uv->u.open.next; @@ -91,10 +126,10 @@ void luaF_unlinkupval (UpVal *uv) { } -void luaF_close (lua_State *L, StkId level) { +int luaF_close (lua_State *L, StkId level, int status) { UpVal *uv; - while (L->openupval != NULL && - (uv = L->openupval, uplevel(uv) >= level)) { + while ((uv = L->openupval) != NULL && uplevel(uv) >= level) { + StkId upl = uplevel(uv); TValue *slot = &uv->u.value; /* new position for value */ luaF_unlinkupval(uv); setobj(L, slot, uv->v); /* move value to upvalue slot */ @@ -102,7 +137,13 @@ void luaF_close (lua_State *L, StkId level) { if (!iswhite(uv)) gray2black(uv); /* closed upvalues cannot be gray */ luaC_barrier(L, uv, slot); + if (status >= 0 && uv->tt == LUA_TUPVALTBC) { /* must be closed? */ + ptrdiff_t levelrel = savestack(L, level); + status = closeupval(L, uv, upl, status); /* may reallocate the stack */ + level = restorestack(L, levelrel); + } } + return status; } diff --git a/lfunc.h b/lfunc.h index 859ccc1205..4c78800568 100644 --- a/lfunc.h +++ b/lfunc.h @@ -47,7 +47,7 @@ LUAI_FUNC CClosure *luaF_newCclosure (lua_State *L, int nelems); LUAI_FUNC LClosure *luaF_newLclosure (lua_State *L, int nelems); LUAI_FUNC void luaF_initupvals (lua_State *L, LClosure *cl); LUAI_FUNC UpVal *luaF_findupval (lua_State *L, StkId level); -LUAI_FUNC void luaF_close (lua_State *L, StkId level); +LUAI_FUNC int luaF_close (lua_State *L, StkId level, int status); LUAI_FUNC void luaF_unlinkupval (UpVal *uv); LUAI_FUNC void luaF_freeproto (lua_State *L, Proto *f); LUAI_FUNC const char *luaF_getlocalname (const Proto *func, int local_number, diff --git a/lgc.c b/lgc.c index 39b3ab7397..9d196a18b7 100644 --- a/lgc.c +++ b/lgc.c @@ -609,6 +609,7 @@ static int traverseLclosure (global_State *g, LClosure *cl) { ** That ensures that the entire stack have valid (non-dead) objects. */ static int traversethread (global_State *g, lua_State *th) { + UpVal *uv; StkId o = th->stack; if (o == NULL) return 1; /* stack not completely built yet */ @@ -616,6 +617,10 @@ static int traversethread (global_State *g, lua_State *th) { th->openupval == NULL || isintwups(th)); for (; o < th->top; o++) /* mark live elements in the stack */ markvalue(g, s2v(o)); + for (uv = th->openupval; uv != NULL; uv = uv->u.open.next) { + if (uv->tt == LUA_TUPVALTBC) /* to be closed? */ + markobject(g, uv); /* cannot be collected */ + } if (g->gcstate == GCSatomic) { /* final traversal? */ StkId lim = th->stack + th->stacksize; /* real end of stack */ for (; o < lim; o++) /* clear not-marked stack slice */ diff --git a/lparser.c b/lparser.c index 84abeb90b7..6b14b80047 100644 --- a/lparser.c +++ b/lparser.c @@ -1536,9 +1536,9 @@ static void scopedlocalstat (LexState *ls) { FuncState *fs = ls->fs; new_localvar(ls, str_checkname(ls)); checknext(ls, '='); + exp1(ls, 0); luaK_codeABC(fs, OP_TBC, fs->nactvar, 0, 0); markupval(fs, fs->nactvar); - exp1(ls, 0); adjustlocalvars(ls, 1); } diff --git a/lstate.c b/lstate.c index 8b0219bccd..4a2453d15a 100644 --- a/lstate.c +++ b/lstate.c @@ -258,7 +258,7 @@ static void preinit_thread (lua_State *L, global_State *g) { static void close_state (lua_State *L) { global_State *g = G(L); - luaF_close(L, L->stack); /* close all upvalues for this thread */ + luaF_close(L, L->stack, -1); /* close all upvalues for this thread */ luaC_freeallobjects(L); /* collect all objects */ if (ttisnil(&g->nilvalue)) /* closing a fully built state? */ luai_userstateclose(L); @@ -301,7 +301,7 @@ LUA_API lua_State *lua_newthread (lua_State *L) { void luaE_freethread (lua_State *L, lua_State *L1) { LX *l = fromstate(L1); - luaF_close(L1, L1->stack); /* close all upvalues for this thread */ + luaF_close(L1, L1->stack, -1); /* close all upvalues for this thread */ lua_assert(L1->openupval == NULL); luai_userstatefree(L, L1); freestack(L1); diff --git a/ltests.c b/ltests.c index ff962543cb..a6968653a5 100644 --- a/ltests.c +++ b/ltests.c @@ -1208,7 +1208,7 @@ static int getindex_aux (lua_State *L, lua_State *L1, const char **pc) { static void pushcode (lua_State *L, int code) { static const char *const codes[] = {"OK", "YIELD", "ERRRUN", - "ERRSYNTAX", "ERRMEM", "ERRGCMM", "ERRERR"}; + "ERRSYNTAX", MEMERRMSG, "ERRGCMM", "ERRERR"}; lua_pushstring(L, codes[code]); } diff --git a/ltm.c b/ltm.c index 5c148180aa..53e15c7fdc 100644 --- a/ltm.c +++ b/ltm.c @@ -43,7 +43,7 @@ void luaT_init (lua_State *L) { "__div", "__idiv", "__band", "__bor", "__bxor", "__shl", "__shr", "__unm", "__bnot", "__lt", "__le", - "__concat", "__call" + "__concat", "__call", "__close" }; int i; for (i=0; itt = LUA_TUPVALTBC; /* mark it to be closed */ - setnilvalue(s2v(ra)); /* intialize it with nil */ vmbreak; } vmcase(OP_JMP) { @@ -1591,7 +1590,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { int nparams1 = GETARG_C(i); if (nparams1) /* vararg function? */ delta = ci->u.l.nextraargs + nparams1; - luaF_close(L, base); /* close upvalues from current call */ + luaF_close(L, base, LUA_OK); /* close upvalues from current call */ } if (!ttisfunction(s2v(ra))) { /* not a function? */ luaD_tryfuncTM(L, ra); /* try '__call' metamethod */ @@ -1625,7 +1624,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { int nparams1 = GETARG_C(i); if (nparams1) /* vararg function? */ ci->func -= ci->u.l.nextraargs + nparams1; - luaF_close(L, base); /* there may be open upvalues */ + luaF_close(L, base, LUA_OK); /* there may be open upvalues */ } halfProtect(luaD_poscall(L, ci, n)); return; diff --git a/testes/api.lua b/testes/api.lua index bebb6d2d25..925a80c1ea 100644 --- a/testes/api.lua +++ b/testes/api.lua @@ -1,4 +1,4 @@ --- $Id: testes/api.lua $ +-- $Id: testes/api.lua 2018-07-25 15:31:04 -0300 $ -- See Copyright Notice in file all.lua if T==nil then @@ -1027,6 +1027,18 @@ testamem("coroutine creation", function() end) +-- testing to-be-closed variables +testamem("to-be-closed variables", function() + local flag + do + local scoped x = function () flag = true end + flag = false + local x = {} + end + return flag +end) + + -- testing threads -- get main thread from registry (at index LUA_RIDX_MAINTHREAD == 1) diff --git a/testes/locals.lua b/testes/locals.lua index 20ecae4bc5..8d55e9f5bf 100644 --- a/testes/locals.lua +++ b/testes/locals.lua @@ -173,15 +173,69 @@ end assert(x==20) --- tests for to-be-closed variables +print"testing to-be-closed variables" + +do + local a = {} + do + local scoped x = setmetatable({"x"}, {__close = function (self) + a[#a + 1] = self[1] end}) + local scoped y = function () a[#a + 1] = "y" end + a[#a + 1] = "in" + end + a[#a + 1] = "out" + assert(a[1] == "in" and a[2] == "y" and a[3] == "x" and a[4] == "out") +end + + +do -- errors in __close + local log = {} + local function foo (err) + local scoped x = function (msg) log[#log + 1] = msg; error(1) end + local scoped x1 = function (msg) log[#log + 1] = msg; end + local scoped gc = function () collectgarbage() end + local scoped y = function (msg) log[#log + 1] = msg; error(2) end + local scoped z = function (msg) log[#log + 1] = msg or 10; error(3) end + if err then error(4) end + end + local stat, msg = pcall(foo, false) + assert(msg == 1) + assert(log[1] == 10 and log[2] == 3 and log[3] == 2 and log[4] == 2 + and #log == 4) + + log = {} + local stat, msg = pcall(foo, true) + assert(msg == 1) + assert(log[1] == 4 and log[2] == 3 and log[3] == 2 and log[4] == 2 + and #log == 4) +end + do - local scoped x = 3 - local a - local scoped y = 5 - assert(x == 3 and y == 5) + -- memory error inside closing function + local function foo () + local scoped y = function () io.write(2); T.alloccount() end + local scoped x = setmetatable({}, {__close = function () + T.alloccount(0); local x = {} -- force a memory error + end}) + io.write("1\n") + error("a") -- common error inside the function's body + end + + local _, msg = pcall(foo) +T.alloccount() + assert(msg == "not enough memory") + end +-- a suspended coroutine should not close its variables when collected +local co = coroutine.wrap(function() + local scoped x = function () os.exit(1) end -- should not run + coroutine.yield() +end) +co() +co = nil + print('OK') return 5,f From 3c7dc52909ce0688bdb20cacaf686413a79aaf48 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 18 Oct 2018 16:15:09 -0300 Subject: [PATCH 0333/1145] Handling of memory errors when creating to-be-closed upvalues --- lfunc.c | 126 ++++++++++++++++++++++++++++++++++------------ lfunc.h | 1 + lvm.c | 3 +- testes/locals.lua | 58 ++++++++++++++++++--- 4 files changed, 148 insertions(+), 40 deletions(-) diff --git a/lfunc.c b/lfunc.c index fde72b8c46..4f9362f3c4 100644 --- a/lfunc.c +++ b/lfunc.c @@ -40,6 +40,7 @@ LClosure *luaF_newLclosure (lua_State *L, int n) { return c; } + /* ** fill a closure with new closed upvalues */ @@ -56,31 +57,43 @@ void luaF_initupvals (lua_State *L, LClosure *cl) { } +/* +** Create a new upvalue with the given tag at the given level, +** and link it to the list of open upvalues of 'L' after entry 'prev'. +**/ +static UpVal *newupval (lua_State *L, int tag, StkId level, UpVal **prev) { + GCObject *o = luaC_newobj(L, tag, sizeof(UpVal)); + UpVal *uv = gco2upv(o); + UpVal *next = *prev; + uv->v = s2v(level); /* current value lives in the stack */ + uv->u.open.next = next; /* link it to list of open upvalues */ + uv->u.open.previous = prev; + if (next) + next->u.open.previous = &uv->u.open.next; + *prev = uv; + if (!isintwups(L)) { /* thread not in list of threads with upvalues? */ + L->twups = G(L)->twups; /* link it to the list */ + G(L)->twups = L; + } + return uv; +} + + +/* +** Find and reuse, or create if it does not exist, a regular upvalue +** at the given level. +*/ UpVal *luaF_findupval (lua_State *L, StkId level) { UpVal **pp = &L->openupval; - GCObject *o; UpVal *p; - UpVal *uv; lua_assert(isintwups(L) || L->openupval == NULL); - while ((p = *pp) != NULL && uplevel(p) >= level) { + while ((p = *pp) != NULL && uplevel(p) >= level) { /* search for it */ if (uplevel(p) == level && !isdead(G(L), p)) /* corresponding upvalue? */ return p; /* return it */ pp = &p->u.open.next; } - /* not found: create a new upvalue between 'pp' and 'p' */ - o = luaC_newobj(L, LUA_TUPVAL, sizeof(UpVal)); - uv = gco2upv(o); - uv->u.open.next = p; /* link it to list of open upvalues */ - uv->u.open.previous = pp; - if (p) - p->u.open.previous = &uv->u.open.next; - *pp = uv; - uv->v = s2v(level); /* current value lives in the stack */ - if (!isintwups(L)) { /* thread not in list of threads with upvalues? */ - L->twups = G(L)->twups; /* link it to the list */ - G(L)->twups = L; - } - return uv; + /* not found: create a new upvalue after 'pp' */ + return newupval(L, LUA_TUPVAL, level, pp); } @@ -89,25 +102,44 @@ static void callclose (lua_State *L, void *ud) { } -static int closeupval (lua_State *L, UpVal *uv, StkId level, int status) { - StkId func = level + 1; /* save slot for old error message */ - if (status != LUA_OK) /* was there an error? */ - luaD_seterrorobj(L, status, level); /* save error message */ - else - setnilvalue(s2v(level)); - if (ttisfunction(uv->v)) { /* object to-be-closed is a function? */ - setobj2s(L, func, uv->v); /* will call it */ - setobjs2s(L, func + 1, level); /* error msg. as argument */ +/* +** Prepare closing method with its argument for object at +** index 'func' in the stack. Assume there is an error message +** (or nil) just below the object. +*/ +static int prepclosingmethod (lua_State *L, StkId func) { + if (ttisfunction(s2v(func))) { /* object to-be-closed is a function? */ + setobjs2s(L, func + 1, func - 1); /* push error msg. as argument */ } else { /* try '__close' metamethod */ - const TValue *tm = luaT_gettmbyobj(L, uv->v, TM_CLOSE); - if (ttisnil(tm)) - return status; /* no metamethod */ + const TValue *tm = luaT_gettmbyobj(L, s2v(func), TM_CLOSE); + if (ttisnil(tm)) /* no metamethod? */ + return 0; /* nothing to call */ + setobjs2s(L, func + 1, func); /* 'self' is the argument */ setobj2s(L, func, tm); /* will call metamethod */ - setobj2s(L, func + 1, uv->v); /* with 'self' as argument */ } L->top = func + 2; /* add function and argument */ - if (status == LUA_OK) /* not in "error mode"? */ + return 1; +} + + +/* +** Prepare and call a closing method. If status is OK, code is +** still inside the original protected call, and so any error +** will be handled there. Otherwise, a previous error already +** activated original protected call, and so the call to the +** closing method must be protected here. +*/ +static int closeupval (lua_State *L, TValue *uv, StkId level, int status) { + StkId func = level + 1; /* save slot for old error message */ + if (unlikely(status != LUA_OK)) /* was there an error? */ + luaD_seterrorobj(L, status, level); /* save error message */ + else + setnilvalue(s2v(level)); /* no error message */ + setobj2s(L, func, uv); /* put object on top of error message */ + if (!prepclosingmethod(L, func)) + return status; /* nothing to call */ + if (likely(status == LUA_OK)) /* not in "error mode"? */ callclose(L, func); /* call closing method */ else { /* already inside error handler; cannot raise another error */ int newstatus = luaD_pcall(L, callclose, func, savestack(L, level), 0); @@ -118,6 +150,36 @@ static int closeupval (lua_State *L, UpVal *uv, StkId level, int status) { } +/* +** Try to create a to-be-closed upvalue +** (can raise a memory-allocation error) +*/ +static void trynewtbcupval (lua_State *L, void *ud) { + StkId level = cast(StkId, ud); + lua_assert(L->openupval == NULL || uplevel(L->openupval) < level); + newupval(L, LUA_TUPVALTBC, level, &L->openupval); +} + + +/* +** Create a to-be-closed upvalue. If there is a memory error +** when creating the upvalue, the closing method must be called here, +** as there is no upvalue to call it later. +*/ +void luaF_newtbcupval (lua_State *L, StkId level) { + int status = luaD_rawrunprotected(L, trynewtbcupval, level); + if (unlikely(status != LUA_OK)) { /* memory error creating upvalue? */ + StkId func = level + 1; + lua_assert(status == LUA_ERRMEM); + setobjs2s(L, func, level); /* open space for error message */ + luaD_seterrorobj(L, status, level); /* save error message */ + if (prepclosingmethod(L, func)) + callclose(L, func); /* call closing method */ + luaD_throw(L, LUA_ERRMEM); /* throw memory error */ + } +} + + void luaF_unlinkupval (UpVal *uv) { lua_assert(upisopen(uv)); *uv->u.open.previous = uv->u.open.next; @@ -139,7 +201,7 @@ int luaF_close (lua_State *L, StkId level, int status) { luaC_barrier(L, uv, slot); if (status >= 0 && uv->tt == LUA_TUPVALTBC) { /* must be closed? */ ptrdiff_t levelrel = savestack(L, level); - status = closeupval(L, uv, upl, status); /* may reallocate the stack */ + status = closeupval(L, uv->v, upl, status); /* may reallocate the stack */ level = restorestack(L, levelrel); } } diff --git a/lfunc.h b/lfunc.h index 4c78800568..c9fe13148a 100644 --- a/lfunc.h +++ b/lfunc.h @@ -47,6 +47,7 @@ LUAI_FUNC CClosure *luaF_newCclosure (lua_State *L, int nelems); LUAI_FUNC LClosure *luaF_newLclosure (lua_State *L, int nelems); LUAI_FUNC void luaF_initupvals (lua_State *L, LClosure *cl); LUAI_FUNC UpVal *luaF_findupval (lua_State *L, StkId level); +LUAI_FUNC void luaF_newtbcupval (lua_State *L, StkId level); LUAI_FUNC int luaF_close (lua_State *L, StkId level, int status); LUAI_FUNC void luaF_unlinkupval (UpVal *uv); LUAI_FUNC void luaF_freeproto (lua_State *L, Proto *f); diff --git a/lvm.c b/lvm.c index e2994aa9fe..0d82756b93 100644 --- a/lvm.c +++ b/lvm.c @@ -1456,8 +1456,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_TBC) { - UpVal *up = luaF_findupval(L, ra); /* create new upvalue */ - up->tt = LUA_TUPVALTBC; /* mark it to be closed */ + luaF_newtbcupval(L, ra); /* create new to-be-closed upvalue */ vmbreak; } vmcase(OP_JMP) { diff --git a/testes/locals.lua b/testes/locals.lua index 8d55e9f5bf..d12c70a087 100644 --- a/testes/locals.lua +++ b/testes/locals.lua @@ -180,7 +180,7 @@ do do local scoped x = setmetatable({"x"}, {__close = function (self) a[#a + 1] = self[1] end}) - local scoped y = function () a[#a + 1] = "y" end + local scoped y = function (x) assert(x == nil); a[#a + 1] = "y" end a[#a + 1] = "in" end a[#a + 1] = "out" @@ -210,27 +210,73 @@ do -- errors in __close and #log == 4) end -do +if rawget(_G, "T") then + local function stack(n) n = (n == 0) or stack(n - 1); end; -- memory error inside closing function local function foo () - local scoped y = function () io.write(2); T.alloccount() end + local scoped y = function () T.alloccount() end local scoped x = setmetatable({}, {__close = function () T.alloccount(0); local x = {} -- force a memory error end}) - io.write("1\n") error("a") -- common error inside the function's body end + stack(5) -- ensure a minimal number of CI structures + + -- despite memory error, 'y' will be executed and + -- memory limit will be lifted local _, msg = pcall(foo) -T.alloccount() assert(msg == "not enough memory") + local function close (msg) + T.alloccount() + assert(msg == "not enough memory") + end + + -- set a memory limit and return a closing function to remove the limit + local function enter (count) + stack(10) -- reserve some stack space + T.alloccount(count) + return close + end + + local function test () + local scoped x = enter(0) -- set a memory limit + -- creation of previous upvalue will raise a memory error + os.exit(false) -- should not run + end + + local _, msg = pcall(test) + assert(msg == "not enough memory") + + -- now use metamethod for closing + close = setmetatable({}, {__close = function () + T.alloccount() + end}) + + -- repeat test with extra closing upvalues + local function test () + local scoped xxx = function (msg) + assert(msg == "not enough memory"); + error(1000) -- raise another error + end + local scoped xx = function (msg) + assert(msg == "not enough memory"); + end + local scoped x = enter(0) -- set a memory limit + -- creation of previous upvalue will raise a memory error + os.exit(false) -- should not run + end + + local _, msg = pcall(test) + assert(msg == 1000) + end -- a suspended coroutine should not close its variables when collected local co = coroutine.wrap(function() - local scoped x = function () os.exit(1) end -- should not run + local scoped x = function () os.exit(false) end -- should not run coroutine.yield() end) co() From c90176f96924ee7d207501b32f216925773d3bdb Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 22 Oct 2018 14:55:51 -0300 Subject: [PATCH 0334/1145] Complete implementation of to-be-closed variables --- ldo.c | 9 ++++++--- testes/db.lua | 9 ++++----- testes/locals.lua | 33 +++++++++++++++++++++++++++++---- 3 files changed, 39 insertions(+), 12 deletions(-) diff --git a/ldo.c b/ldo.c index f2d12f0430..78e4c5c3fb 100644 --- a/ldo.c +++ b/ldo.c @@ -676,12 +676,15 @@ LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs, /* unroll continuation */ status = luaD_rawrunprotected(L, unroll, &status); } - if (unlikely(errorstatus(status))) { /* unrecoverable error? */ + if (likely(!errorstatus(status))) + lua_assert(status == L->status); /* normal end or yield */ + else { /* unrecoverable error */ + status = luaF_close(L, L->stack, status); /* close all upvalues */ L->status = cast_byte(status); /* mark thread as 'dead' */ - luaD_seterrorobj(L, status, L->top); /* push error message */ + luaD_seterrorobj(L, status, L->stack + 1); /* push error message */ + L->ci = &L->base_ci; /* back to the original C level */ L->ci->top = L->top; } - else lua_assert(status == L->status); /* normal end or yield */ } *nresults = (status == LUA_YIELD) ? L->ci->u2.nyield : cast_int(L->top - (L->ci->func + 1)); diff --git a/testes/db.lua b/testes/db.lua index 2feaaef183..9da682102e 100644 --- a/testes/db.lua +++ b/testes/db.lua @@ -734,19 +734,18 @@ a, b = coroutine.resume(co, 100) assert(a and b == 30) --- check traceback of suspended (or dead with error) coroutines +-- check traceback of suspended coroutines -function f(i) if i==0 then error(i) else coroutine.yield(); f(i-1) end end +function f(i) coroutine.yield(i == 0); f(i - 1) end co = coroutine.create(function (x) f(x) end) a, b = coroutine.resume(co, 3) t = {"'coroutine.yield'", "'f'", "in function <"} -while coroutine.status(co) == "suspended" do +repeat checktraceback(co, t) a, b = coroutine.resume(co) table.insert(t, 2, "'f'") -- one more recursive call to 'f' -end -t[1] = "'error'" +until b checktraceback(co, t) diff --git a/testes/locals.lua b/testes/locals.lua index d12c70a087..f21fa2ec92 100644 --- a/testes/locals.lua +++ b/testes/locals.lua @@ -274,13 +274,38 @@ if rawget(_G, "T") then end +-- to-be-closed variables in coroutines +do + -- an error in a coroutine closes variables + local x = false + local y = false + local co = coroutine.create(function () + local scoped xv = function () x = true end + do + local scoped yv = function () y = true end + coroutine.yield(100) -- yield doesn't close variable + end + coroutine.yield(200) -- yield doesn't close variable + error(23) -- error does + end) + + local a, b = coroutine.resume(co) + assert(a and b == 100 and not x and not y) + a, b = coroutine.resume(co) + assert(a and b == 200 and not x and y) + a, b = coroutine.resume(co) + assert(not a and b == 23 and x and y) +end + -- a suspended coroutine should not close its variables when collected -local co = coroutine.wrap(function() +local co +co = coroutine.wrap(function() local scoped x = function () os.exit(false) end -- should not run - coroutine.yield() + co = nil + coroutine.yield() end) -co() -co = nil +co() -- start coroutine +assert(co == nil) -- eventually it will be collected print('OK') From 7c8146d556714508224dc3f3a68677c18ece00b7 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 22 Oct 2018 15:02:09 -0300 Subject: [PATCH 0335/1145] Small improvements in the manual --- manual/manual.of | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/manual/manual.of b/manual/manual.of index 47a551bfa2..d8bac5dad9 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -664,7 +664,9 @@ The default value is 20; the maximum value is 200. You can set garbage-collector metamethods for tables and, using the @N{C API}, for full userdata @see{metatable}. -These metamethods are also called @def{finalizers}. +These metamethods, called @def{finalizers}, +are called when the garbage collector detects that the +corresponding table or userdata is unreachable. Finalizers allow you to coordinate Lua's garbage collection with external resource management (such as closing files, network or database connections, @@ -720,6 +722,10 @@ Lua calls the finalizers of all objects marked for finalization, following the reverse order that they were marked. If any finalizer marks objects for collection during that phase, these marks have no effect. +If any finalizer raises an error during that phase, +its execution is interrupted but the error is ignored. + +Finalizers cannot yield. } @@ -5911,17 +5917,21 @@ If there are no syntactic errors, returns the compiled chunk as a function; otherwise, returns @nil plus the error message. -If the resulting function has upvalues, -the first upvalue is set to the value of @id{env}, -if that parameter is given, -or to the value of the @x{global environment}. -Other upvalues are initialized with @nil. -(When you load a main chunk, +When you load a main chunk, the resulting function will always have exactly one upvalue, the @id{_ENV} variable @see{globalenv}. However, when you load a binary chunk created from a function @seeF{string.dump}, -the resulting function can have an arbitrary number of upvalues.) +the resulting function can have an arbitrary number of upvalues, +and there is no guarantee that its first upvalue will be +the @id{_ENV} variable. +(A non-main function may not even have an @id{_ENV} upvalue.) + +Regardless, if the resulting function has any upvalues, +its first upvalue is set to the value of @id{env}, +if that parameter is given, +or to the value of the @x{global environment}. +Other upvalues are initialized with @nil. All upvalues are fresh, that is, they are not shared with any other function. From 6a4b9bb2b47a9ca564b986b27d2451e602bc2c5b Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 22 Oct 2018 15:20:07 -0300 Subject: [PATCH 0336/1145] Removed extra information from RCS keyword strings in tests Version numbers and dates (mostly wrong) from RCS keyword strings removed from all test files; only the file name are kept. --- testes/all.lua | 0 testes/api.lua | 2 +- testes/bitwise.lua | 0 testes/coroutine.lua | 2 +- testes/events.lua | 2 +- testes/math.lua | 2 +- 6 files changed, 4 insertions(+), 4 deletions(-) mode change 100755 => 100644 testes/all.lua mode change 100755 => 100644 testes/bitwise.lua diff --git a/testes/all.lua b/testes/all.lua old mode 100755 new mode 100644 diff --git a/testes/api.lua b/testes/api.lua index 925a80c1ea..6e11c6ed76 100644 --- a/testes/api.lua +++ b/testes/api.lua @@ -1,4 +1,4 @@ --- $Id: testes/api.lua 2018-07-25 15:31:04 -0300 $ +-- $Id: testes/api.lua $ -- See Copyright Notice in file all.lua if T==nil then diff --git a/testes/bitwise.lua b/testes/bitwise.lua old mode 100755 new mode 100644 diff --git a/testes/coroutine.lua b/testes/coroutine.lua index 36eae44aea..7d42eadde0 100644 --- a/testes/coroutine.lua +++ b/testes/coroutine.lua @@ -1,4 +1,4 @@ --- $Id: testes/coroutine.lua 2018-07-25 15:31:04 -0300 $ +-- $Id: testes/coroutine.lua $ -- See Copyright Notice in file all.lua print "testing coroutines" diff --git a/testes/events.lua b/testes/events.lua index c4d43d5183..b071c2a314 100644 --- a/testes/events.lua +++ b/testes/events.lua @@ -1,4 +1,4 @@ --- $Id: testes/events.lua 2018-07-25 15:31:04 -0300 $ +-- $Id: testes/events.lua $ -- See Copyright Notice in file all.lua print('testing metatables') diff --git a/testes/math.lua b/testes/math.lua index b387977e8d..7c780e592a 100644 --- a/testes/math.lua +++ b/testes/math.lua @@ -1,4 +1,4 @@ --- $Id: testes/math.lua 2018-07-25 15:31:04 -0300 $ +-- $Id: testes/math.lua $ -- See Copyright Notice in file all.lua print("testing numbers and math lib") From ea1322ef5438aa38f16fa00d3ab377811bba5a76 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 23 Oct 2018 12:58:38 -0300 Subject: [PATCH 0337/1145] Detail: bad assertion in 'luaM_free_' --- lmem.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lmem.c b/lmem.c index 7751870741..53f8dcb9d6 100644 --- a/lmem.c +++ b/lmem.c @@ -108,7 +108,7 @@ l_noret luaM_toobig (lua_State *L) { */ void luaM_free_ (lua_State *L, void *block, size_t osize) { global_State *g = G(L); - lua_assert((block == 0) == (block == NULL)); + lua_assert((osize == 0) == (block == NULL)); (*g->frealloc)(g->ud, block, osize, 0); g->GCdebt -= osize; } From 0a9aca56caa925c42aaa683b43560357ab736ea4 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 23 Oct 2018 13:57:25 -0300 Subject: [PATCH 0338/1145] Added a '__close' metamethod to file handles --- liolib.c | 1 + testes/files.lua | 57 +++++++++++++++++++++++++++++------------------- 2 files changed, 36 insertions(+), 22 deletions(-) diff --git a/liolib.c b/liolib.c index 21305b8c76..3dc509bdb8 100644 --- a/liolib.c +++ b/liolib.c @@ -743,6 +743,7 @@ static const luaL_Reg flib[] = { {"setvbuf", f_setvbuf}, {"write", f_write}, {"__gc", f_gc}, + {"__close", f_gc}, {"__tostring", f_tostring}, {NULL, NULL} }; diff --git a/testes/files.lua b/testes/files.lua index c3e42235bc..9aae591324 100644 --- a/testes/files.lua +++ b/testes/files.lua @@ -120,31 +120,45 @@ io.output(io.open(otherfile, "ab")) assert(io.write("\n\n\t\t ", 3450, "\n")); io.close() --- test writing/reading numbers -f = assert(io.open(file, "w")) -f:write(maxint, '\n') -f:write(string.format("0X%x\n", maxint)) -f:write("0xABCp-3", '\n') -f:write(0, '\n') -f:write(-maxint, '\n') -f:write(string.format("0x%X\n", -maxint)) -f:write("-0xABCp-3", '\n') -assert(f:close()) -f = assert(io.open(file, "r")) -assert(f:read("n") == maxint) -assert(f:read("n") == maxint) -assert(f:read("n") == 0xABCp-3) -assert(f:read("n") == 0) -assert(f:read("*n") == -maxint) -- test old format (with '*') -assert(f:read("n") == -maxint) -assert(f:read("*n") == -0xABCp-3) -- test old format (with '*') -assert(f:close()) + +do + -- closing file by scope + local F = nil + do + local scoped f = assert(io.open(file, "w")) + F = f + end + assert(tostring(F) == "file (closed)") +end +assert(os.remove(file)) + + +do + -- test writing/reading numbers + local scoped f = assert(io.open(file, "w")) + f:write(maxint, '\n') + f:write(string.format("0X%x\n", maxint)) + f:write("0xABCp-3", '\n') + f:write(0, '\n') + f:write(-maxint, '\n') + f:write(string.format("0x%X\n", -maxint)) + f:write("-0xABCp-3", '\n') + assert(f:close()) + f = assert(io.open(file, "r")) + assert(f:read("n") == maxint) + assert(f:read("n") == maxint) + assert(f:read("n") == 0xABCp-3) + assert(f:read("n") == 0) + assert(f:read("*n") == -maxint) -- test old format (with '*') + assert(f:read("n") == -maxint) + assert(f:read("*n") == -0xABCp-3) -- test old format (with '*') +end assert(os.remove(file)) -- testing multiple arguments to io.read do - local f = assert(io.open(file, "w")) + local scoped f = assert(io.open(file, "w")) f:write[[ a line another line @@ -171,9 +185,8 @@ three -- second item failing l1, n1, n2, dummy = f:read("l", "n", "n", "l") assert(l1 == "a line" and n1 == nil) - assert(f:close()) - assert(os.remove(file)) end +assert(os.remove(file)) From 41c800b352149e037bdebd5f20d2f25ed2a0e2a5 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 25 Oct 2018 12:50:20 -0300 Subject: [PATCH 0339/1145] Closing methods should not interfere with returning values A closing method cannot be called in its own stack slot, as there may be returning values in the stack after that slot, and the call would corrupt those values. Instead, the closing method must be copied to the top of the stack to be called. Moreover, even when a function returns no value, its return istruction still has to have its position (which will set the stack top) after the local variables, otherwise a closing method might corrupt another not-yet-called closing method. --- lfunc.c | 64 ++++++++++++++++++++++++----------------------- lparser.c | 15 ++++++----- lvm.c | 6 +++-- testes/locals.lua | 55 +++++++++++++++++++++++++++++++++++++++- 4 files changed, 98 insertions(+), 42 deletions(-) diff --git a/lfunc.c b/lfunc.c index 4f9362f3c4..15874f88b2 100644 --- a/lfunc.c +++ b/lfunc.c @@ -98,27 +98,29 @@ UpVal *luaF_findupval (lua_State *L, StkId level) { static void callclose (lua_State *L, void *ud) { - luaD_callnoyield(L, cast(StkId, ud), 0); + UNUSED(ud); + luaD_callnoyield(L, L->top - 2, 0); } /* -** Prepare closing method with its argument for object at -** index 'func' in the stack. Assume there is an error message -** (or nil) just below the object. +** Prepare closing method plus its argument for object 'obj' with +** error message 'err'. (This function assumes EXTRA_STACK.) */ -static int prepclosingmethod (lua_State *L, StkId func) { - if (ttisfunction(s2v(func))) { /* object to-be-closed is a function? */ - setobjs2s(L, func + 1, func - 1); /* push error msg. as argument */ +static int prepclosingmethod (lua_State *L, TValue *obj, TValue *err) { + StkId top = L->top; + if (ttisfunction(obj)) { /* object to-be-closed is a function? */ + setobj2s(L, top, obj); /* push function */ + setobj2s(L, top + 1, err); /* push error msg. as argument */ } else { /* try '__close' metamethod */ - const TValue *tm = luaT_gettmbyobj(L, s2v(func), TM_CLOSE); + const TValue *tm = luaT_gettmbyobj(L, obj, TM_CLOSE); if (ttisnil(tm)) /* no metamethod? */ return 0; /* nothing to call */ - setobjs2s(L, func + 1, func); /* 'self' is the argument */ - setobj2s(L, func, tm); /* will call metamethod */ + setobj2s(L, top, tm); /* will call metamethod... */ + setobj2s(L, top + 1, obj); /* with 'self' as the argument */ } - L->top = func + 2; /* add function and argument */ + L->top = top + 2; /* add function and argument */ return 1; } @@ -129,22 +131,24 @@ static int prepclosingmethod (lua_State *L, StkId func) { ** will be handled there. Otherwise, a previous error already ** activated original protected call, and so the call to the ** closing method must be protected here. +** If status is OK, the call to the closing method will be pushed +** at the top of the stack. Otherwise, values are pushed after +** the 'level' of the upvalue being closed, as everything after +** that won't be used again. */ static int closeupval (lua_State *L, TValue *uv, StkId level, int status) { - StkId func = level + 1; /* save slot for old error message */ - if (unlikely(status != LUA_OK)) /* was there an error? */ - luaD_seterrorobj(L, status, level); /* save error message */ - else - setnilvalue(s2v(level)); /* no error message */ - setobj2s(L, func, uv); /* put object on top of error message */ - if (!prepclosingmethod(L, func)) - return status; /* nothing to call */ - if (likely(status == LUA_OK)) /* not in "error mode"? */ - callclose(L, func); /* call closing method */ - else { /* already inside error handler; cannot raise another error */ - int newstatus = luaD_pcall(L, callclose, func, savestack(L, level), 0); - if (newstatus != LUA_OK) /* error when closing? */ - status = newstatus; /* this will be the new error */ + if (likely(status == LUA_OK)) { + if (prepclosingmethod(L, uv, &G(L)->nilvalue)) /* something to call? */ + callclose(L, NULL); /* call closing method */ + } + else { /* there was an error */ + /* save error message and set stack top to 'level + 1' */ + luaD_seterrorobj(L, status, level); + if (prepclosingmethod(L, uv, s2v(level))) { /* something to call? */ + int newstatus = luaD_pcall(L, callclose, NULL, savestack(L, level), 0); + if (newstatus != LUA_OK) /* another error when closing? */ + status = newstatus; /* this will be the new error */ + } } return status; } @@ -169,12 +173,10 @@ static void trynewtbcupval (lua_State *L, void *ud) { void luaF_newtbcupval (lua_State *L, StkId level) { int status = luaD_rawrunprotected(L, trynewtbcupval, level); if (unlikely(status != LUA_OK)) { /* memory error creating upvalue? */ - StkId func = level + 1; lua_assert(status == LUA_ERRMEM); - setobjs2s(L, func, level); /* open space for error message */ - luaD_seterrorobj(L, status, level); /* save error message */ - if (prepclosingmethod(L, func)) - callclose(L, func); /* call closing method */ + luaD_seterrorobj(L, LUA_ERRMEM, level + 1); /* save error message */ + if (prepclosingmethod(L, s2v(level), s2v(level + 1))) + callclose(L, NULL); /* call closing method */ luaD_throw(L, LUA_ERRMEM); /* throw memory error */ } } @@ -201,7 +203,7 @@ int luaF_close (lua_State *L, StkId level, int status) { luaC_barrier(L, uv, slot); if (status >= 0 && uv->tt == LUA_TUPVALTBC) { /* must be closed? */ ptrdiff_t levelrel = savestack(L, level); - status = closeupval(L, uv->v, upl, status); /* may reallocate the stack */ + status = closeupval(L, uv->v, upl, status); /* may realloc. the stack */ level = restorestack(L, levelrel); } } diff --git a/lparser.c b/lparser.c index 6b14b80047..c0c40eaeec 100644 --- a/lparser.c +++ b/lparser.c @@ -561,7 +561,7 @@ static void close_func (LexState *ls) { lua_State *L = ls->L; FuncState *fs = ls->fs; Proto *f = fs->f; - luaK_ret(fs, 0, 0); /* final return */ + luaK_ret(fs, fs->nactvar, 0); /* final return */ leaveblock(fs); lua_assert(fs->bl == NULL); luaK_finish(fs); @@ -1602,9 +1602,10 @@ static void retstat (LexState *ls) { /* stat -> RETURN [explist] [';'] */ FuncState *fs = ls->fs; expdesc e; - int first, nret; /* registers with returned values */ + int nret; /* number of values being returned */ + int first = fs->nactvar; /* first slot to be returned */ if (block_follow(ls, 1) || ls->t.token == ';') - first = nret = 0; /* return no values */ + nret = 0; /* return no values */ else { nret = explist(ls, &e); /* optional return values */ if (hasmultret(e.k)) { @@ -1613,15 +1614,13 @@ static void retstat (LexState *ls) { SET_OPCODE(getinstruction(fs,&e), OP_TAILCALL); lua_assert(GETARG_A(getinstruction(fs,&e)) == fs->nactvar); } - first = fs->nactvar; nret = LUA_MULTRET; /* return all values */ } else { if (nret == 1) /* only one single value? */ - first = luaK_exp2anyreg(fs, &e); - else { - luaK_exp2nextreg(fs, &e); /* values must go to the stack */ - first = fs->nactvar; /* return all active values */ + first = luaK_exp2anyreg(fs, &e); /* can use original slot */ + else { /* values must go to the top of the stack */ + luaK_exp2nextreg(fs, &e); lua_assert(nret == fs->freereg - first); } } diff --git a/lvm.c b/lvm.c index 0d82756b93..2a1ee175a7 100644 --- a/lvm.c +++ b/lvm.c @@ -1452,7 +1452,8 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_CLOSE) { - luaF_close(L, ra, LUA_OK); + L->top = ra + 1; /* everything is free after this slot */ + ProtectNT(luaF_close(L, ra, LUA_OK)); vmbreak; } vmcase(OP_TBC) { @@ -1619,13 +1620,14 @@ void luaV_execute (lua_State *L, CallInfo *ci) { n = cast_int(L->top - ra); /* get what is available */ else L->top = ra + n; /* set call for 'luaD_poscall' */ + savepc(ci); if (TESTARG_k(i)) { int nparams1 = GETARG_C(i); if (nparams1) /* vararg function? */ ci->func -= ci->u.l.nextraargs + nparams1; luaF_close(L, base, LUA_OK); /* there may be open upvalues */ } - halfProtect(luaD_poscall(L, ci, n)); + luaD_poscall(L, ci, n); return; } vmcase(OP_RETURN0) { diff --git a/testes/locals.lua b/testes/locals.lua index f21fa2ec92..65b145dbb5 100644 --- a/testes/locals.lua +++ b/testes/locals.lua @@ -175,6 +175,9 @@ assert(x==20) print"testing to-be-closed variables" +local function stack(n) n = ((n == 0) or stack(n - 1)) end + + do local a = {} do @@ -187,6 +190,57 @@ do assert(a[1] == "in" and a[2] == "y" and a[3] == "x" and a[4] == "out") end +do + local X = false + + local function closescope () stack(10); X = true end + + -- closing functions do not corrupt returning values + local function foo (x) + local scoped _ = closescope + return x, X, 23 + end + + local a, b, c = foo(1.5) + assert(a == 1.5 and b == false and c == 23 and X == true) + + X = false + foo = function (x) + local scoped _ = closescope + local y = 15 + return y + end + + assert(foo() == 15 and X == true) + + X = false + foo = function () + local scoped x = closescope + return x + end + + assert(foo() == closescope and X == true) + +end + + +do + -- to-be-closed variables must be closed in tail calls + local X, Y + local function foo () + local scoped _ = function () Y = 10 end + assert(X == 20 and Y == nil) + return 1,2,3 + end + + local function bar () + local scoped _ = function () X = 20 end + return foo() + end + + local a, b, c, d = bar() + assert(a == 1 and b == 2 and c == 3 and X == 20 and Y == 10 and d == nil) +end do -- errors in __close local log = {} @@ -211,7 +265,6 @@ do -- errors in __close end if rawget(_G, "T") then - local function stack(n) n = (n == 0) or stack(n - 1); end; -- memory error inside closing function local function foo () local scoped y = function () T.alloccount() end From 34840301b529686ce8168828b140a478a5d44b53 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 25 Oct 2018 15:30:15 -0300 Subject: [PATCH 0340/1145] To-be-closed variables in the C API --- lapi.c | 15 +++++++++-- lapi.h | 15 ++++++++++- ldo.c | 32 ++++++++++++++--------- ltests.c | 3 +++ lua.h | 2 ++ testes/api.lua | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 122 insertions(+), 16 deletions(-) diff --git a/lapi.c b/lapi.c index ae6b07aebd..4fef43b713 100644 --- a/lapi.c +++ b/lapi.c @@ -173,15 +173,17 @@ LUA_API void lua_settop (lua_State *L, int idx) { StkId func = L->ci->func; lua_lock(L); if (idx >= 0) { + StkId newtop = (func + 1) + idx; api_check(L, idx <= L->stack_last - (func + 1), "new top too large"); - while (L->top < (func + 1) + idx) + while (L->top < newtop) setnilvalue(s2v(L->top++)); - L->top = (func + 1) + idx; + L->top = newtop; } else { api_check(L, -(idx+1) <= (L->top - (func + 1)), "invalid new top"); L->top += idx+1; /* 'subtract' index (index is negative) */ } + luaF_close(L, L->top, LUA_OK); lua_unlock(L); } @@ -1205,6 +1207,15 @@ LUA_API int lua_next (lua_State *L, int idx) { } +LUA_API void lua_tobeclosed (lua_State *L) { + int nresults = L->ci->nresults; + luaF_newtbcupval(L, L->top - 1); /* create new to-be-closed upvalue */ + if (!hastocloseCfunc(nresults)) /* function not marked yet? */ + L->ci->nresults = codeNresults(nresults); /* mark it */ + lua_assert(hastocloseCfunc(L->ci->nresults)); +} + + LUA_API void lua_concat (lua_State *L, int n) { lua_lock(L); api_checknelems(L, n); diff --git a/lapi.h b/lapi.h index 016f78cccd..5a4206f1ae 100644 --- a/lapi.h +++ b/lapi.h @@ -15,10 +15,23 @@ "stack overflow");} #define adjustresults(L,nres) \ - { if ((nres) == LUA_MULTRET && L->ci->top < L->top) L->ci->top = L->top; } + { if ((nres) <= LUA_MULTRET && L->ci->top < L->top) L->ci->top = L->top; } #define api_checknelems(L,n) api_check(L, (n) < (L->top - L->ci->func), \ "not enough elements in the stack") +/* +** To reduce the overhead of returning from C functions, the presence of +** to-be-closed variables in these functions is coded in the CallInfo's +** field 'nresults', in a way that functions with no to-be-closed variables +** with zero, one, or "all" wanted results have no overhead. Functions +** with other number of wanted results, as well as functions with +** variables to be closed, have an extra check. +*/ + +#define hastocloseCfunc(n) ((n) < LUA_MULTRET) + +#define codeNresults(n) (-(n) - 3) + #endif diff --git a/ldo.c b/ldo.c index 78e4c5c3fb..b7a76ef6f7 100644 --- a/ldo.c +++ b/ldo.c @@ -366,32 +366,38 @@ void luaD_tryfuncTM (lua_State *L, StkId func) { ** separated. */ static void moveresults (lua_State *L, StkId res, int nres, int wanted) { + StkId firstresult; + int i; switch (wanted) { /* handle typical cases separately */ case 0: /* no values needed */ L->top = res; - break; + return; case 1: /* one value needed */ if (nres == 0) /* no results? */ setnilvalue(s2v(res)); /* adjust with nil */ else setobjs2s(L, res, L->top - nres); /* move it to proper place */ L->top = res + 1; - break; + return; case LUA_MULTRET: wanted = nres; /* we want all results */ - /* FALLTHROUGH */ - default: { /* multiple results */ - StkId firstresult = L->top - nres; /* index of first result */ - int i; - /* move all results to correct place */ - for (i = 0; i < nres && i < wanted; i++) - setobjs2s(L, res + i, firstresult + i); - for (; i < wanted; i++) /* complete wanted number of results */ - setnilvalue(s2v(res + i)); - L->top = res + wanted; /* top points after the last result */ break; - } + default: /* multiple results (or to-be-closed variables) */ + if (hastocloseCfunc(wanted)) { + luaF_close(L, res, LUA_OK); + wanted = codeNresults(wanted); /* correct value */ + if (wanted == LUA_MULTRET) + wanted = nres; + } + break; } + firstresult = L->top - nres; /* index of first result */ + /* move all results to correct place */ + for (i = 0; i < nres && i < wanted; i++) + setobjs2s(L, res + i, firstresult + i); + for (; i < wanted; i++) /* complete wanted number of results */ + setnilvalue(s2v(res + i)); + L->top = res + wanted; /* top points after the last result */ } diff --git a/ltests.c b/ltests.c index a6968653a5..fbcb747571 100644 --- a/ltests.c +++ b/ltests.c @@ -1554,6 +1554,9 @@ static struct X { int x; } x; int i = getindex; return lua_yieldk(L1, nres, i, Cfunck); } + else if EQ("tobeclosed") { + lua_tobeclosed(L); + } else luaL_error(L, "unknown instruction %s", buff); } return 0; diff --git a/lua.h b/lua.h index a014be1fdd..16d685ccb0 100644 --- a/lua.h +++ b/lua.h @@ -333,6 +333,8 @@ LUA_API size_t (lua_stringtonumber) (lua_State *L, const char *s); LUA_API lua_Alloc (lua_getallocf) (lua_State *L, void **ud); LUA_API void (lua_setallocf) (lua_State *L, lua_Alloc f, void *ud); +LUA_API void (lua_tobeclosed) (lua_State *L); + /* ** {============================================================== diff --git a/testes/api.lua b/testes/api.lua index 6e11c6ed76..988250f719 100644 --- a/testes/api.lua +++ b/testes/api.lua @@ -967,6 +967,77 @@ T.closestate(L1) L1 = nil print('+') +------------------------------------------------------------------------- +-- testing to-be-closed variables +------------------------------------------------------------------------- +print"testing to-be-closed variables" + +do + local openresource = {} + + local function newresource () + local x = setmetatable({10}, {__close = function(y) + assert(openresource[#openresource] == y) + openresource[#openresource] = nil + y[1] = y[1] + 1 + end}) + openresource[#openresource + 1] = x + return x + end + + local a = T.testC([[ + call 0 1 # create resource + tobeclosed # mark it to be closed + return 1 + ]], newresource) + assert(a[1] == 11) + assert(#openresource == 0) -- was closed + + -- repeat the test, but calling function in a 'multret' context + local a = {T.testC([[ + call 0 1 # create resource + tobeclosed # mark it to be closed + return 2 + ]], newresource)} + assert(type(a[1]) == "string" and a[2][1] == 11) + assert(#openresource == 0) -- was closed + + -- error + local a, b = pcall(T.testC, [[ + call 0 1 # create resource + tobeclosed # mark it to be closed + error # resource is the error object + ]], newresource) + assert(a == false and b[1] == 11) + assert(#openresource == 0) -- was closed + + local function check (n) + assert(#openresource == n) + end + + -- closing resources with 'settop' + local a = T.testC([[ + pushvalue 2 + call 0 1 # create resource + tobeclosed # mark it to be closed + pushvalue 2 + call 0 1 # create another resource + tobeclosed # mark it to be closed + pushvalue 3 + pushint 2 # there should be two open resources + call 1 0 + pop 1 # pop second resource from the stack + pushvalue 3 + pushint 1 # there should be one open resource + call 1 0 + pop 1 # pop second resource from the stack + pushint * + return 1 # return stack size + ]], newresource, check) + assert(a == 3) -- no extra items left in the stack + +end + ------------------------------------------------------------------------- -- testing memory limits From 6e9b719694bffb8de711f182d405ec37d32ae0b1 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 26 Oct 2018 10:38:50 -0300 Subject: [PATCH 0341/1145] More uniformity in code generation for 'for' loops Added new instruction 'OP_TFORPREP' to prepare a generic for loop. Currently it is equivalent to a jump (but with a format 'iABx', similar to other for-loop preparing instructions), but soon it will be the place to create upvalues for closing loop states. --- ljumptab.h | 1 + lopcodes.c | 1 + lopcodes.h | 1 + lopnames.h | 1 + lparser.c | 16 ++++++---------- lvm.c | 4 ++++ 6 files changed, 14 insertions(+), 10 deletions(-) diff --git a/ljumptab.h b/ljumptab.h index da4cf7b70d..6767e95b18 100644 --- a/ljumptab.h +++ b/ljumptab.h @@ -96,6 +96,7 @@ static void *disptab[] = { &&L_OP_FORPREP1, &&L_OP_FORLOOP, &&L_OP_FORPREP, +&&L_OP_TFORPREP, &&L_OP_TFORCALL, &&L_OP_TFORLOOP, &&L_OP_SETLIST, diff --git a/lopcodes.c b/lopcodes.c index f6915beb88..11a73c2957 100644 --- a/lopcodes.c +++ b/lopcodes.c @@ -90,6 +90,7 @@ LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = { ,opmode(0, 0, 0, 1, iABx) /* OP_FORPREP1 */ ,opmode(0, 0, 0, 1, iABx) /* OP_FORLOOP */ ,opmode(0, 0, 0, 1, iABx) /* OP_FORPREP */ + ,opmode(0, 0, 0, 0, iABx) /* OP_TFORPREP */ ,opmode(0, 0, 0, 0, iABC) /* OP_TFORCALL */ ,opmode(0, 0, 0, 1, iABx) /* OP_TFORLOOP */ ,opmode(0, 1, 0, 0, iABC) /* OP_SETLIST */ diff --git a/lopcodes.h b/lopcodes.h index 4d144000fe..4797d7c37c 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -282,6 +282,7 @@ OP_FORLOOP,/* A Bx R(A)+=R(A+2); if R(A) DO block */ + static OpCode forprep[3] = {OP_FORPREP, OP_FORPREP1, OP_TFORPREP}; + static OpCode forloop[3] = {OP_FORLOOP, OP_FORLOOP1, OP_TFORLOOP}; BlockCnt bl; FuncState *fs = ls->fs; int prep, endfor; adjustlocalvars(ls, 3); /* control variables */ checknext(ls, TK_DO); - prep = (kind == 0) ? luaK_codeABx(fs, OP_FORPREP, base, 0) - : (kind == 1) ? luaK_codeABx(fs, OP_FORPREP1, base, 0) - : luaK_jump(fs); + prep = luaK_codeABx(fs, forprep[kind], base, 0); enterblock(fs, &bl, 0); /* scope for declared variables */ adjustlocalvars(ls, nvars); luaK_reserveregs(fs, nvars); block(ls); leaveblock(fs); /* end of scope for declared variables */ + fixforjump(fs, prep, luaK_getlabel(fs), 0); if (kind == 2) { /* generic for? */ - luaK_patchtohere(fs, prep); luaK_codeABC(fs, OP_TFORCALL, base, 0, nvars); luaK_fixline(fs, line); - endfor = luaK_codeABx(fs, OP_TFORLOOP, base + 2, 0); - } - else { - fixforjump(fs, prep, luaK_getlabel(fs), 0); - endfor = (kind == 0) ? luaK_codeABx(fs, OP_FORLOOP, base, 0) - : luaK_codeABx(fs, OP_FORLOOP1, base, 0); + base += 2; /* base for 'OP_TFORLOOP' (skips function and state) */ } + endfor = luaK_codeABx(fs, forloop[kind], base, 0); fixforjump(fs, endfor, prep + 1, 1); luaK_fixline(fs, line); } diff --git a/lvm.c b/lvm.c index 2a1ee175a7..35a58089ca 100644 --- a/lvm.c +++ b/lvm.c @@ -1744,6 +1744,10 @@ void luaV_execute (lua_State *L, CallInfo *ci) { pc += GETARG_Bx(i); vmbreak; } + vmcase(OP_TFORPREP) { + pc += GETARG_Bx(i); + vmbreak; + } vmcase(OP_TFORCALL) { StkId cb = ra + 3; /* call base */ setobjs2s(L, cb+2, ra+2); From a006514ea138a29b6031058d9002b48a572b5dd6 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 29 Oct 2018 14:26:48 -0300 Subject: [PATCH 0342/1145] Big revamp in the implmentation of labels/gotos Added restriction that, when a label is created, there cannot be another label with the same name visible. That allows backward goto's to be resolved when they are read. Backward goto's get a close if they jump out of the scope of some variable; labels get a close only if previous goto to it jumps out of the scope of some upvalue. --- lcode.c | 34 ------- lcode.h | 3 - lopcodes.h | 9 +- lparser.c | 241 +++++++++++++++++++++++------------------------- lparser.h | 2 + ltests.c | 2 +- testes/code.lua | 19 ++-- testes/goto.lua | 3 +- 8 files changed, 131 insertions(+), 182 deletions(-) diff --git a/lcode.c b/lcode.c index e84b85ac5f..054b28fd1b 100644 --- a/lcode.c +++ b/lcode.c @@ -275,40 +275,6 @@ void luaK_patchtohere (FuncState *fs, int list) { } -/* -** Correct a jump list to jump to 'target'. If 'hasclose' is true, -** 'target' contains an OP_CLOSE instruction (see first assert). -** Only the jumps with ('m' == true) need that close; other jumps -** avoid it jumping to the next instruction. -*/ -void luaK_patchgoto (FuncState *fs, int list, int target, int hasclose) { - lua_assert(!hasclose || GET_OPCODE(fs->f->code[target]) == OP_CLOSE); - while (list != NO_JUMP) { - int next = getjump(fs, list); - lua_assert(!GETARG_m(fs->f->code[list]) || hasclose); - patchtestreg(fs, list, NO_REG); /* do not generate values */ - if (!hasclose || GETARG_m(fs->f->code[list])) - fixjump(fs, list, target); - else /* there is a CLOSE instruction but jump does not need it */ - fixjump(fs, list, target + 1); /* avoid CLOSE instruction */ - list = next; - } -} - - -/* -** Mark (using the 'm' arg) all jumps in 'list' to close upvalues. Mark -** will instruct 'luaK_patchgoto' to make these jumps go to OP_CLOSE -** instructions. -*/ -void luaK_patchclose (FuncState *fs, int list) { - for (; list != NO_JUMP; list = getjump(fs, list)) { - lua_assert(GET_OPCODE(fs->f->code[list]) == OP_JMP); - SETARG_m(fs->f->code[list], 1); - } -} - - /* ** MAXimum number of successive Instructions WiTHout ABSolute line ** information. diff --git a/lcode.h b/lcode.h index dd091c7818..0758f88dee 100644 --- a/lcode.h +++ b/lcode.h @@ -78,10 +78,7 @@ LUAI_FUNC void luaK_setoneret (FuncState *fs, expdesc *e); LUAI_FUNC int luaK_jump (FuncState *fs); LUAI_FUNC void luaK_ret (FuncState *fs, int first, int nret); LUAI_FUNC void luaK_patchlist (FuncState *fs, int list, int target); -LUAI_FUNC void luaK_patchgoto (FuncState *fs, int list, int target, - int hasclose); LUAI_FUNC void luaK_patchtohere (FuncState *fs, int list); -LUAI_FUNC void luaK_patchclose (FuncState *fs, int list); LUAI_FUNC void luaK_concat (FuncState *fs, int *l1, int l2); LUAI_FUNC int luaK_getlabel (FuncState *fs); LUAI_FUNC void luaK_prefix (FuncState *fs, UnOpr op, expdesc *v, int line); diff --git a/lopcodes.h b/lopcodes.h index 4797d7c37c..5a38f76746 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -21,7 +21,7 @@ iABC C(8) | B(8) |k| A(8) | Op(7) | iABx Bx(17) | A(8) | Op(7) | iAsB sBx (signed)(17) | A(8) | Op(7) | iAx Ax(25) | Op(7) | -isJ sJ(24) |m| Op(7) | +isJ sJ(25) | Op(7) | A signed argument is represented in excess K: the represented value is the written unsigned value minus K, where K is half the maximum for the @@ -40,7 +40,7 @@ enum OpMode {iABC, iABx, iAsBx, iAx, isJ}; /* basic instruction formats */ #define SIZE_Bx (SIZE_C + SIZE_B + 1) #define SIZE_A 8 #define SIZE_Ax (SIZE_Bx + SIZE_A) -#define SIZE_sJ (SIZE_Bx + SIZE_A - 1) +#define SIZE_sJ (SIZE_Bx + SIZE_A) #define SIZE_OP 7 @@ -55,8 +55,7 @@ enum OpMode {iABC, iABx, iAsBx, iAx, isJ}; /* basic instruction formats */ #define POS_Ax POS_A -#define POS_m POS_A -#define POS_sJ (POS_A + 1) +#define POS_sJ POS_A /* ** limits for opcode arguments. @@ -144,8 +143,6 @@ enum OpMode {iABC, iABx, iAsBx, iAx, isJ}; /* basic instruction formats */ check_exp(checkopm(i, isJ), getarg(i, POS_sJ, SIZE_sJ) - OFFSET_sJ) #define SETARG_sJ(i,j) \ setarg(i, cast_uint((j)+OFFSET_sJ), POS_sJ, SIZE_sJ) -#define GETARG_m(i) check_exp(checkopm(i, isJ), getarg(i, POS_m, 1)) -#define SETARG_m(i,m) setarg(i, m, POS_m, 1) #define CREATE_ABCk(o,a,b,c,k) ((cast(Instruction, o)<L->nCcalls--) -static void closegoto (LexState *ls, int g, Labeldesc *label) { +/* +** Generates an error that a goto jumps into the scope of some +** local variable. +*/ +static l_noret jumpscopeerror (LexState *ls, Labeldesc *gt) { + const char *varname = getstr(getlocvar(ls->fs, gt->nactvar)->varname); + const char *msg = " at line %d jumps into the scope of local '%s'"; + msg = luaO_pushfstring(ls->L, msg, getstr(gt->name), gt->line, varname); + luaK_semerror(ls, msg); /* raise the error */ +} + + +/* +** Solves the goto at index 'g' to given 'label' and removes it +** from the list of pending goto's. +** If it jumps into the scope of some variable, raises an error. +*/ +static void solvegoto (LexState *ls, int g, Labeldesc *label) { int i; - FuncState *fs = ls->fs; - Labellist *gl = &ls->dyd->gt; - Labeldesc *gt = &gl->arr[g]; + Labellist *gl = &ls->dyd->gt; /* list of goto's */ + Labeldesc *gt = &gl->arr[g]; /* goto to be resolved */ lua_assert(eqstr(gt->name, label->name)); - if (gt->nactvar < label->nactvar) { - TString *vname = getlocvar(fs, gt->nactvar)->varname; - const char *msg = luaO_pushfstring(ls->L, - " at line %d jumps into the scope of local '%s'", - getstr(gt->name), gt->line, getstr(vname)); - luaK_semerror(ls, msg); - } - luaK_patchgoto(fs, gt->pc, label->pc, 1); - /* remove goto from pending list */ - for (i = g; i < gl->n - 1; i++) + if (gt->nactvar < label->nactvar) /* enter some scope? */ + jumpscopeerror(ls, gt); + luaK_patchlist(ls->fs, gt->pc, label->pc); + for (i = g; i < gl->n - 1; i++) /* remove goto from pending list */ gl->arr[i] = gl->arr[i + 1]; gl->n--; } /* -** try to close a goto with existing labels; this solves backward jumps +** Search for an active label with the given name. */ -static int solvelabel (LexState *ls, int g) { +static Labeldesc *findlabel (LexState *ls, TString *name) { int i; - BlockCnt *bl = ls->fs->bl; Dyndata *dyd = ls->dyd; - Labeldesc *gt = &dyd->gt.arr[g]; - /* check labels in current block for a match */ - for (i = bl->firstlabel; i < dyd->label.n; i++) { + /* check labels in current function for a match */ + for (i = ls->fs->firstlabel; i < dyd->label.n; i++) { Labeldesc *lb = &dyd->label.arr[i]; - if (eqstr(lb->name, gt->name)) { /* correct label? */ - if (gt->nactvar > lb->nactvar && - (bl->upval || dyd->label.n > bl->firstlabel)) - luaK_patchclose(ls->fs, gt->pc); - closegoto(ls, g, lb); /* close it */ - return 1; - } + if (eqstr(lb->name, name)) /* correct label? */ + return lb; } - return 0; /* label not found; cannot close goto */ + return NULL; /* label not found */ } +/* +** Adds a new label/goto in the corresponding list. +*/ static int newlabelentry (LexState *ls, Labellist *l, TString *name, int line, int pc) { int n = l->n; @@ -382,6 +386,7 @@ static int newlabelentry (LexState *ls, Labellist *l, TString *name, l->arr[n].name = name; l->arr[n].line = line; l->arr[n].nactvar = ls->fs->nactvar; + l->arr[n].close = 0; l->arr[n].pc = pc; l->n = n + 1; return n; @@ -389,51 +394,64 @@ static int newlabelentry (LexState *ls, Labellist *l, TString *name, /* -** check whether new label 'lb' matches any pending gotos in current -** block; solves forward jumps +** Solves forward jumps. Check whether new label 'lb' matches any +** pending gotos in current block and solves them. Return true +** if any of the goto's need to close upvalues. */ -static void solvegotos (LexState *ls, Labeldesc *lb) { +static int solvegotos (LexState *ls, Labeldesc *lb) { Labellist *gl = &ls->dyd->gt; int i = ls->fs->bl->firstgoto; + int needsclose = 0; while (i < gl->n) { - if (eqstr(gl->arr[i].name, lb->name)) - closegoto(ls, i, lb); /* will remove 'i' from the list */ + if (eqstr(gl->arr[i].name, lb->name)) { + needsclose |= gl->arr[i].close; + solvegoto(ls, i, lb); /* will remove 'i' from the list */ + } else i++; } + return needsclose; +} + + +/* +** Create a new label with the given 'name' at the given 'line'. +** 'last' tells whether label is the last non-op statement in its +** block. Solves all pending goto's to this new label and adds +** a close instruction if necessary. +** Returns true iff it added a close instruction. +*/ +static int createlabel (LexState *ls, TString *name, int line, + int last) { + FuncState *fs = ls->fs; + Labellist *ll = &ls->dyd->label; + int l = newlabelentry(ls, ll, name, line, luaK_getlabel(fs)); + if (last) { /* label is last no-op statement in the block? */ + /* assume that locals are already out of scope */ + ll->arr[l].nactvar = fs->bl->nactvar; + } + if (solvegotos(ls, &ll->arr[l])) { /* need close? */ + luaK_codeABC(fs, OP_CLOSE, fs->nactvar, 0, 0); + return 1; + } + return 0; } /* -** export pending gotos to outer level, to check them against -** outer labels; if the block being exited has upvalues, and -** the goto exits the scope of any variable (which can be the -** upvalue), close those variables being exited. Also export -** break list. +** Adjust pending gotos to outer level of a block. */ static void movegotosout (FuncState *fs, BlockCnt *bl) { - int i = bl->firstgoto; + int i; Labellist *gl = &fs->ls->dyd->gt; - /* correct pending gotos to current block and try to close it - with visible labels */ - while (i < gl->n) { /* for each pending goto */ + /* correct pending gotos to current block */ + for (i = bl->firstgoto; i < gl->n; i++) { /* for each pending goto */ Labeldesc *gt = &gl->arr[i]; if (gt->nactvar > bl->nactvar) { /* leaving a variable scope? */ - if (bl->upval) /* variable may be an upvalue? */ - luaK_patchclose(fs, gt->pc); /* jump will need a close */ gt->nactvar = bl->nactvar; /* update goto level */ + gt->close |= bl->upval; /* jump may need a close */ } - if (!solvelabel(fs->ls, i)) - i++; /* move to next one */ - /* else, 'solvelabel' removed current goto from the list - and 'i' now points to next one */ } - /* handles break list */ - if (bl->upval) /* exiting the scope of an upvalue? */ - luaK_patchclose(fs, bl->brks); /* breaks will need OP_CLOSE */ - /* move breaks to outer block */ - luaK_concat(fs, &bl->previous->brks, bl->brks); - bl->previous->brkcls |= bl->brkcls; } @@ -442,8 +460,6 @@ static void enterblock (FuncState *fs, BlockCnt *bl, lu_byte isloop) { bl->nactvar = fs->nactvar; bl->firstlabel = fs->ls->dyd->label.n; bl->firstgoto = fs->ls->dyd->gt.n; - bl->brks = NO_JUMP; - bl->brkcls = 0; bl->upval = 0; bl->previous = fs->bl; fs->bl = bl; @@ -451,20 +467,6 @@ static void enterblock (FuncState *fs, BlockCnt *bl, lu_byte isloop) { } -/* -** Fix all breaks in block 'bl' to jump to the end of the block. -*/ -static void fixbreaks (FuncState *fs, BlockCnt *bl) { - int target = fs->pc; - if (bl->brkcls) /* does the block need to close upvalues? */ - luaK_codeABC(fs, OP_CLOSE, bl->nactvar, 0, 0); - luaK_patchgoto(fs, bl->brks, target, bl->brkcls); - bl->brks = NO_JUMP; /* no more breaks to fix */ - bl->brkcls = 0; /* no more need to close upvalues */ - lua_assert(!bl->upval); /* loop body cannot have local variables */ -} - - /* ** generates an error for an undefined 'goto'. */ @@ -478,11 +480,10 @@ static l_noret undefgoto (LexState *ls, Labeldesc *gt) { static void leaveblock (FuncState *fs) { BlockCnt *bl = fs->bl; LexState *ls = fs->ls; - if (bl->upval && bl->brks != NO_JUMP) /* breaks in upvalue scopes? */ - bl->brkcls = 1; /* these breaks must close the upvalues */ - if (bl->isloop) - fixbreaks(fs, bl); /* fix pending breaks */ - if (bl->previous && bl->upval) + int hasclose = 0; + if (bl->isloop) /* fix pending breaks? */ + hasclose = createlabel(ls, luaS_newliteral(ls->L, "break"), 0, 0); + if (!hasclose && bl->previous && bl->upval) luaK_codeABC(fs, OP_CLOSE, bl->nactvar, 0, 0); fs->bl = bl->previous; removevars(fs, bl->nactvar); @@ -492,7 +493,6 @@ static void leaveblock (FuncState *fs) { if (bl->previous) /* inner block? */ movegotosout(fs, bl); /* update pending gotos to outer block */ else { - lua_assert(bl->brks == NO_JUMP); /* no pending breaks */ if (bl->firstgoto < ls->dyd->gt.n) /* pending gotos in outer block? */ undefgoto(ls, &ls->dyd->gt.arr[bl->firstgoto]); /* error */ } @@ -550,6 +550,7 @@ static void open_func (LexState *ls, FuncState *fs, BlockCnt *bl) { fs->nactvar = 0; fs->needclose = 0; fs->firstlocal = ls->dyd->actvar.n; + fs->firstlabel = ls->dyd->label.n; fs->bl = NULL; f->source = ls->source; f->maxstacksize = 2; /* registers 0/1 are always valid */ @@ -1204,63 +1205,59 @@ static int cond (LexState *ls) { } -static void gotostat (LexState *ls, int pc) { +static void gotostat (LexState *ls) { + FuncState *fs = ls->fs; int line = ls->linenumber; - int g; - luaX_next(ls); /* skip 'goto' */ - g = newlabelentry(ls, &ls->dyd->gt, str_checkname(ls), line, pc); - solvelabel(ls, g); /* close it if label already defined */ + TString *name = str_checkname(ls); /* label's name */ + Labeldesc *lb = findlabel(ls, name); + if (lb == NULL) /* no label? */ + /* forward jump; will be resolved when the label is declared */ + newlabelentry(ls, &ls->dyd->gt, name, line, luaK_jump(fs)); + else { /* found a label */ + /* backward jump; will be resolved here */ + if (fs->nactvar > lb->nactvar) /* leaving the scope of some variable? */ + luaK_codeABC(fs, OP_CLOSE, lb->nactvar, 0, 0); + /* create jump and link it to the label */ + luaK_patchlist(fs, luaK_jump(fs), lb->pc); + } } +/* +** Break statement. Semantically equivalent to "goto break". +*/ static void breakstat (LexState *ls, int pc) { FuncState *fs = ls->fs; + int line = ls->linenumber; BlockCnt *bl = fs->bl; luaX_next(ls); /* skip break */ + newlabelentry(ls, &ls->dyd->gt, luaS_newliteral(ls->L, "break"), line, pc); while (bl && !bl->isloop) { bl = bl->previous; } if (!bl) luaX_syntaxerror(ls, "no loop to break"); - luaK_concat(fs, &fs->bl->brks, pc); } -/* check for repeated labels on the same block */ -static void checkrepeated (FuncState *fs, Labellist *ll, TString *label) { - int i; - for (i = fs->bl->firstlabel; i < ll->n; i++) { - if (eqstr(label, ll->arr[i].name)) { - const char *msg = luaO_pushfstring(fs->ls->L, - "label '%s' already defined on line %d", - getstr(label), ll->arr[i].line); - luaK_semerror(fs->ls, msg); - } +/* +** Check whether there is already a label with the given 'name'. +*/ +static void checkrepeated (LexState *ls, TString *name) { + Labeldesc *lb = findlabel(ls, name); + if (lb != NULL) { /* already defined? */ + const char *msg = "label '%s' already defined on line %d"; + msg = luaO_pushfstring(ls->L, msg, getstr(name), lb->line); + luaK_semerror(ls, msg); /* error */ } } -/* skip no-op statements */ -static void skipnoopstat (LexState *ls) { - while (ls->t.token == ';' || ls->t.token == TK_DBCOLON) - statement(ls); -} - - -static void labelstat (LexState *ls, TString *label, int line) { +static void labelstat (LexState *ls, TString *name, int line) { /* label -> '::' NAME '::' */ - FuncState *fs = ls->fs; - Labellist *ll = &ls->dyd->label; - int l; /* index of new label being created */ - checkrepeated(fs, ll, label); /* check for repeated labels */ checknext(ls, TK_DBCOLON); /* skip double colon */ - /* create new entry for this label */ - l = newlabelentry(ls, ll, label, line, luaK_getlabel(fs)); - luaK_codeABC(fs, OP_CLOSE, fs->nactvar, 0, 0); - skipnoopstat(ls); /* skip other no-op statements */ - if (block_follow(ls, 0)) { /* label is last no-op statement in the block? */ - /* assume that locals are already out of scope */ - ll->arr[l].nactvar = fs->bl->nactvar; - } - solvegotos(ls, &ll->arr[l]); + while (ls->t.token == ';' || ls->t.token == TK_DBCOLON) + statement(ls); /* skip other no-op statements */ + checkrepeated(ls, name); /* check for repeated labels */ + createlabel(ls, name, line, block_follow(ls, 0)); } @@ -1295,8 +1292,6 @@ static void repeatstat (LexState *ls, int line) { statlist(ls); check_match(ls, TK_UNTIL, TK_REPEAT, line); condexit = cond(ls); /* read condition (inside scope block) */ - if (bl2.upval) /* upvalues? */ - luaK_patchclose(fs, condexit); leaveblock(fs); /* finish scope */ if (bl2.upval) { /* upvalues? */ int exit = luaK_jump(fs); /* normal exit must jump over fix */ @@ -1453,15 +1448,12 @@ static void test_then_block (LexState *ls, int *escapelist) { luaX_next(ls); /* skip IF or ELSEIF */ expr(ls, &v); /* read condition */ checknext(ls, TK_THEN); - if (ls->t.token == TK_GOTO || ls->t.token == TK_BREAK) { + if (ls->t.token == TK_BREAK) { luaK_goiffalse(ls->fs, &v); /* will jump to label if condition is true */ enterblock(fs, &bl, 0); /* must enter block before 'goto' */ - if (ls->t.token == TK_GOTO) - gotostat(ls, v.t); /* handle goto */ - else - breakstat(ls, v.t); /* handle break */ + breakstat(ls, v.t); /* handle break */ while (testnext(ls, ';')) {} /* skip semicolons */ - if (block_follow(ls, 0)) { /* 'goto'/'break' is the entire block? */ + if (block_follow(ls, 0)) { /* 'break' is the entire block? */ leaveblock(fs); return; /* and that is it */ } @@ -1683,7 +1675,8 @@ static void statement (LexState *ls) { break; } case TK_GOTO: { /* stat -> 'goto' NAME */ - gotostat(ls, luaK_jump(ls->fs)); + luaX_next(ls); /* skip 'goto' */ + gotostat(ls); break; } default: { /* stat -> func | assignment */ diff --git a/lparser.h b/lparser.h index 1b94a97a6f..8b070b0e42 100644 --- a/lparser.h +++ b/lparser.h @@ -88,6 +88,7 @@ typedef struct Labeldesc { int pc; /* position in code */ int line; /* line where it appeared */ lu_byte nactvar; /* local level where it appears in current block */ + lu_byte close; /* goto that escapes upvalues */ } Labeldesc; @@ -128,6 +129,7 @@ typedef struct FuncState { int np; /* number of elements in 'p' */ int nabslineinfo; /* number of elements in 'abslineinfo' */ int firstlocal; /* index of first local var (in Dyndata array) */ + int firstlabel; /* index of first label (in 'dyd->label->arr') */ short nlocvars; /* number of elements in 'f->locvars' */ lu_byte nactvar; /* number of active local variables */ lu_byte nups; /* number of upvalues */ diff --git a/ltests.c b/ltests.c index fbcb747571..7cf0889197 100644 --- a/ltests.c +++ b/ltests.c @@ -550,7 +550,7 @@ static char *buildop (Proto *p, int pc, char *buff) { sprintf(buff, "%-12s%4d", name, GETARG_Ax(i)); break; case isJ: - sprintf(buff, "%-12s%4d (%1d)", name, GETARG_sJ(i), !!GETARG_m(i)); + sprintf(buff, "%-12s%4d", name, GETARG_sJ(i)); break; } return obuff; diff --git a/testes/code.lua b/testes/code.lua index ad48448594..9b3f2b6864 100644 --- a/testes/code.lua +++ b/testes/code.lua @@ -297,7 +297,7 @@ check(function () b[a], a = c, b a, b = c, a a = a -end, +end, 'LOADNIL', 'MOVE', 'MOVE', 'SETTABLE', 'MOVE', 'MOVE', 'MOVE', 'SETTABLE', @@ -329,18 +329,13 @@ checkequal(function (l) local a; return 0 <= a and a <= l end, function (l) local a; return not (not(a >= 0) or not(a <= l)) end) --- if-goto optimizations -check(function (a, b, c, d, e) - if a == b then goto l1; - elseif a == c then goto l2; - elseif a == d then goto l2; - else if a == e then goto l3; - else goto l3 - end +-- if-break optimizations +check(function (a, b) + while a do + if b then break else a = a + 1 end end - ::l1:: ::l2:: ::l3:: ::l4:: -end, 'EQ', 'JMP', 'EQ', 'JMP', 'EQ', 'JMP', 'EQ', 'JMP', 'JMP', -'CLOSE', 'CLOSE', 'CLOSE', 'CLOSE', 'RETURN0') + end, +'TEST', 'JMP', 'TEST', 'JMP', 'ADDI', 'JMP', 'RETURN0') checkequal( function (a) while a < 10 do a = a + 1 end end, diff --git a/testes/goto.lua b/testes/goto.lua index 238bc04aba..85038d0269 100644 --- a/testes/goto.lua +++ b/testes/goto.lua @@ -14,6 +14,7 @@ errmsg([[ do ::l1:: end goto l1; ]], "label 'l1'") -- repeated label errmsg([[ ::l1:: ::l1:: ]], "label 'l1'") +errmsg([[ ::l1:: do ::l1:: end]], "label 'l1'") -- undefined label @@ -67,8 +68,6 @@ do assert(assert(load(prog))() == 31) end --- goto to correct label when nested -do goto l3; ::l3:: end -- does not loop jumping to previous label 'l3' -- ok to jump over local dec. to end of block do From 2316ec4c24a475e091ec3153a5bd908801a3a109 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 30 Oct 2018 15:04:19 -0300 Subject: [PATCH 0343/1145] Back with optimization for 'if cond then goto' Statements like 'if cond then goto label' generate code so that the jump in the 'if' goes directly to the given label. This optimization cannot be done when the jump is backwards leaving the scope of some variable, as it cannot add the needed 'close' instruction. (The jumps were already generated by the 'if'.) This commit also added 'likely'/'unlikely' for tests for errors in the parser, and it changed the way breaks outside loops are detected. (Now they are detected like other goto's with undefined labels.) --- lparser.c | 84 ++++++++++++++++++++++++++++++++++++++----------- testes/code.lua | 20 +++++++++++- testes/goto.lua | 16 ++++++++++ 3 files changed, 100 insertions(+), 20 deletions(-) diff --git a/lparser.c b/lparser.c index 499e95313a..9419f880bd 100644 --- a/lparser.c +++ b/lparser.c @@ -113,7 +113,7 @@ static void checknext (LexState *ls, int c) { static void check_match (LexState *ls, int what, int who, int where) { - if (!testnext(ls, what)) { + if (unlikely(!testnext(ls, what))) { if (where == ls->linenumber) error_expected(ls, what); else { @@ -350,7 +350,7 @@ static void solvegoto (LexState *ls, int g, Labeldesc *label) { Labellist *gl = &ls->dyd->gt; /* list of goto's */ Labeldesc *gt = &gl->arr[g]; /* goto to be resolved */ lua_assert(eqstr(gt->name, label->name)); - if (gt->nactvar < label->nactvar) /* enter some scope? */ + if (unlikely(gt->nactvar < label->nactvar)) /* enter some scope? */ jumpscopeerror(ls, gt); luaK_patchlist(ls->fs, gt->pc, label->pc); for (i = g; i < gl->n - 1; i++) /* remove goto from pending list */ @@ -393,6 +393,11 @@ static int newlabelentry (LexState *ls, Labellist *l, TString *name, } +static int newgotoentry (LexState *ls, TString *name, int line, int pc) { + return newlabelentry(ls, &ls->dyd->gt, name, line, pc); +} + + /* ** Solves forward jumps. Check whether new label 'lb' matches any ** pending gotos in current block and solves them. Return true @@ -471,8 +476,15 @@ static void enterblock (FuncState *fs, BlockCnt *bl, lu_byte isloop) { ** generates an error for an undefined 'goto'. */ static l_noret undefgoto (LexState *ls, Labeldesc *gt) { - const char *msg = "no visible label '%s' for at line %d"; - msg = luaO_pushfstring(ls->L, msg, getstr(gt->name), gt->line); + const char *msg; + if (eqstr(gt->name, luaS_newliteral(ls->L, "break"))) { + msg = "break outside loop at line %d"; + msg = luaO_pushfstring(ls->L, msg, gt->line); + } + else { + msg = "no visible label '%s' for at line %d"; + msg = luaO_pushfstring(ls->L, msg, getstr(gt->name), gt->line); + } luaK_semerror(ls, msg); } @@ -1212,7 +1224,7 @@ static void gotostat (LexState *ls) { Labeldesc *lb = findlabel(ls, name); if (lb == NULL) /* no label? */ /* forward jump; will be resolved when the label is declared */ - newlabelentry(ls, &ls->dyd->gt, name, line, luaK_jump(fs)); + newgotoentry(ls, name, line, luaK_jump(fs)); else { /* found a label */ /* backward jump; will be resolved here */ if (fs->nactvar > lb->nactvar) /* leaving the scope of some variable? */ @@ -1226,15 +1238,10 @@ static void gotostat (LexState *ls) { /* ** Break statement. Semantically equivalent to "goto break". */ -static void breakstat (LexState *ls, int pc) { - FuncState *fs = ls->fs; +static void breakstat (LexState *ls) { int line = ls->linenumber; - BlockCnt *bl = fs->bl; luaX_next(ls); /* skip break */ - newlabelentry(ls, &ls->dyd->gt, luaS_newliteral(ls->L, "break"), line, pc); - while (bl && !bl->isloop) { bl = bl->previous; } - if (!bl) - luaX_syntaxerror(ls, "no loop to break"); + newgotoentry(ls, luaS_newliteral(ls->L, "break"), line, luaK_jump(ls->fs)); } @@ -1243,7 +1250,7 @@ static void breakstat (LexState *ls, int pc) { */ static void checkrepeated (LexState *ls, TString *name) { Labeldesc *lb = findlabel(ls, name); - if (lb != NULL) { /* already defined? */ + if (unlikely(lb != NULL)) { /* already defined? */ const char *msg = "label '%s' already defined on line %d"; msg = luaO_pushfstring(ls->L, msg, getstr(name), lb->line); luaK_semerror(ls, msg); /* error */ @@ -1332,7 +1339,7 @@ static void fixforjump (FuncState *fs, int pc, int dest, int back) { int offset = dest - (pc + 1); if (back) offset = -offset; - if (offset > MAXARG_Bx) + if (unlikely(offset > MAXARG_Bx)) luaX_syntaxerror(fs->ls, "control structure too long"); SETARG_Bx(*jmp, offset); } @@ -1439,28 +1446,67 @@ static void forstat (LexState *ls, int line) { } +/* +** Check whether next instruction is a single jump (a 'break', a 'goto' +** to a forward label, or a 'goto' to a backward label with no variable +** to close). If so, set the name of the 'label' it is jumping to +** ("break" for a 'break') or to where it is jumping to ('target') and +** return true. If not a single jump, leave input unchanged, to be +** handled as a regular statement. +*/ +static int issinglejump (LexState *ls, TString **label, int *target) { + if (testnext(ls, TK_BREAK)) { /* a break? */ + *label = luaS_newliteral(ls->L, "break"); + return 1; + } + else if (ls->t.token != TK_GOTO || luaX_lookahead(ls) != TK_NAME) + return 0; /* not a valid goto */ + else { + TString *lname = ls->lookahead.seminfo.ts; /* label's id */ + Labeldesc *lb = findlabel(ls, lname); + if (lb) { /* a backward jump? */ + if (ls->fs->nactvar > lb->nactvar) /* needs to close variables? */ + return 0; /* not a single jump; cannot optimize */ + *target = lb->pc; + } + else /* jump forward */ + *label = lname; + luaX_next(ls); /* skip goto */ + luaX_next(ls); /* skip name */ + return 1; + } +} + + static void test_then_block (LexState *ls, int *escapelist) { /* test_then_block -> [IF | ELSEIF] cond THEN block */ BlockCnt bl; + int line; FuncState *fs = ls->fs; + TString *jlb = NULL; + int target = NO_JUMP; expdesc v; int jf; /* instruction to skip 'then' code (if condition is false) */ luaX_next(ls); /* skip IF or ELSEIF */ expr(ls, &v); /* read condition */ checknext(ls, TK_THEN); - if (ls->t.token == TK_BREAK) { + line = ls->linenumber; + if (issinglejump(ls, &jlb, &target)) { /* 'if x then goto' ? */ luaK_goiffalse(ls->fs, &v); /* will jump to label if condition is true */ enterblock(fs, &bl, 0); /* must enter block before 'goto' */ - breakstat(ls, v.t); /* handle break */ + if (jlb != NULL) /* forward jump? */ + newgotoentry(ls, jlb, line, v.t); /* will be resolved later */ + else /* backward jump */ + luaK_patchlist(fs, v.t, target); /* jump directly to 'target' */ while (testnext(ls, ';')) {} /* skip semicolons */ - if (block_follow(ls, 0)) { /* 'break' is the entire block? */ + if (block_follow(ls, 0)) { /* jump is the entire block? */ leaveblock(fs); return; /* and that is it */ } else /* must skip over 'then' part if condition is false */ jf = luaK_jump(fs); } - else { /* regular case (not goto/break) */ + else { /* regular case (not a jump) */ luaK_goiftrue(ls->fs, &v); /* skip over block if condition is false */ enterblock(fs, &bl, 0); jf = v.f; @@ -1671,7 +1717,7 @@ static void statement (LexState *ls) { break; } case TK_BREAK: { /* stat -> breakstat */ - breakstat(ls, luaK_jump(ls->fs)); + breakstat(ls); break; } case TK_GOTO: { /* stat -> 'goto' NAME */ diff --git a/testes/code.lua b/testes/code.lua index 9b3f2b6864..4d44fa6aa3 100644 --- a/testes/code.lua +++ b/testes/code.lua @@ -339,8 +339,26 @@ check(function (a, b) checkequal( function (a) while a < 10 do a = a + 1 end end, -function (a) while true do if not(a < 10) then break end; a = a + 1; end end +function (a) + ::loop:: + if not (a < 10) then goto exit end + a = a + 1 + goto loop +::exit:: +end ) +checkequal( +function (a) repeat local x = a + 1; a = x until a > 0 end, +function (a) + ::loop:: do + local x = a + 1 + a = x + end + if not (a > 0) then goto loop end +end +) + + print 'OK' diff --git a/testes/goto.lua b/testes/goto.lua index 85038d0269..92f048fb69 100644 --- a/testes/goto.lua +++ b/testes/goto.lua @@ -249,6 +249,22 @@ assert(testG(2) == "2") assert(testG(3) == "3") assert(testG(4) == 5) assert(testG(5) == 10) + +do + -- if x back goto out of scope of upvalue + local X + goto L1 + + ::L2:: goto L3 + + ::L1:: do + local scoped a = function () X = true end + assert(X == nil) + if a then goto L2 end -- jumping back out of scope of 'a' + end + + ::L3:: assert(X == true) -- checks that 'a' was correctly closed +end -------------------------------------------------------------------------------- From e073cbc2e538369e0611abfc9948f301aea6aef3 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 30 Oct 2018 15:46:56 -0300 Subject: [PATCH 0344/1145] Better error messages for invalid operands in numeric 'for' "Better" and similar to error messages for invalid function arguments. *old message: 'for' limit must be a number *new message: bad 'for' limit (number expected, got table) --- ldebug.c | 6 ++++++ ldebug.h | 2 ++ lvm.c | 8 ++++---- testes/errors.lua | 10 ++++++++++ 4 files changed, 22 insertions(+), 4 deletions(-) diff --git a/ldebug.c b/ldebug.c index 3590010c9a..ee1b87d901 100644 --- a/ldebug.c +++ b/ldebug.c @@ -696,6 +696,12 @@ l_noret luaG_typeerror (lua_State *L, const TValue *o, const char *op) { } +l_noret luaG_forerror (lua_State *L, const TValue *o, const char *what) { + luaG_runerror(L, "bad 'for' %s (number expected, got %s)", + what, luaT_objtypename(L, o)); +} + + l_noret luaG_concaterror (lua_State *L, const TValue *p1, const TValue *p2) { if (ttisstring(p1) || cvt2str(p1)) p1 = p2; luaG_typeerror(L, p1, "concatenate"); diff --git a/ldebug.h b/ldebug.h index 31ecc2f6cf..f080711d59 100644 --- a/ldebug.h +++ b/ldebug.h @@ -24,6 +24,8 @@ LUAI_FUNC int luaG_getfuncline (const Proto *f, int pc); LUAI_FUNC l_noret luaG_typeerror (lua_State *L, const TValue *o, const char *opname); +LUAI_FUNC l_noret luaG_forerror (lua_State *L, const TValue *o, + const char *what); LUAI_FUNC l_noret luaG_concaterror (lua_State *L, const TValue *p1, const TValue *p2); LUAI_FUNC l_noret luaG_opinterror (lua_State *L, const TValue *p1, diff --git a/lvm.c b/lvm.c index 35a58089ca..aad965d608 100644 --- a/lvm.c +++ b/lvm.c @@ -1681,7 +1681,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { int stopnow; if (unlikely(!forlimit(plimit, &ilimit, 1, &stopnow))) { savestate(L, ci); /* for the error message */ - luaG_runerror(L, "'for' limit must be a number"); + luaG_forerror(L, plimit, "limit"); } initv = (stopnow ? 0 : ivalue(init)); setivalue(plimit, ilimit); @@ -1732,13 +1732,13 @@ void luaV_execute (lua_State *L, CallInfo *ci) { lua_Number ninit; lua_Number nlimit; lua_Number nstep; savestate(L, ci); /* in case of errors */ if (unlikely(!tonumber(plimit, &nlimit))) - luaG_runerror(L, "'for' limit must be a number"); + luaG_forerror(L, plimit, "limit"); setfltvalue(plimit, nlimit); if (unlikely(!tonumber(pstep, &nstep))) - luaG_runerror(L, "'for' step must be a number"); + luaG_forerror(L, pstep, "step"); setfltvalue(pstep, nstep); if (unlikely(!tonumber(init, &ninit))) - luaG_runerror(L, "'for' initial value must be a number"); + luaG_forerror(L, init, "initial value"); setfltvalue(init, luai_numsub(L, ninit, nstep)); } pc += GETARG_Bx(i); diff --git a/testes/errors.lua b/testes/errors.lua index 142e8b3325..74975e3143 100644 --- a/testes/errors.lua +++ b/testes/errors.lua @@ -154,6 +154,16 @@ checkmessage("a = 24 // 0", "divide by zero") checkmessage("a = 1 % 0", "'n%0'") +-- numeric for loops +checkmessage("for i = {}, 10 do end", "table") +checkmessage("for i = io.stdin, 10 do end", "FILE") +checkmessage("for i = {}, 10 do end", "initial value") +checkmessage("for i = 1, 'x', 10 do end", "string") +checkmessage("for i = 1, {}, 10 do end", "limit") +checkmessage("for i = 1, {} do end", "limit") +checkmessage("for i = 1, 10, print do end", "step") +checkmessage("for i = 1, 10, print do end", "function") + -- passing light userdata instead of full userdata _G.D = debug checkmessage([[ From 947a372f5860a76fcafb4a2845abc322e440d6fc Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 31 Oct 2018 14:54:45 -0300 Subject: [PATCH 0345/1145] State in generic 'for' acts as a to-be-closed variable The implicit variable 'state' in a generic 'for' is marked as a to-be-closed variable, so that the state will be closed as soon as the loop ends, no matter how. Taking advantage of this new facility, the call 'io.lines(filename)' now returns the open file as a second result. Therefore, an iteraction like 'for l in io.lines(name)...' will close the file even when the loop ends with a break or an error. --- liolib.c | 35 +++++++++++++++++++++++++++------ lparser.c | 1 + lvm.c | 41 ++++++++++++++++++++++++++------------ testes/files.lua | 8 ++++---- testes/locals.lua | 50 ++++++++++++++++++++++++++++++++++++++++++++++- 5 files changed, 111 insertions(+), 24 deletions(-) diff --git a/liolib.c b/liolib.c index 3dc509bdb8..5881b0293a 100644 --- a/liolib.c +++ b/liolib.c @@ -354,12 +354,22 @@ static int io_readline (lua_State *L); */ #define MAXARGLINE 250 +/* +** Auxiliar function to create the iteration function for 'lines'. +** The iteration function is a closure over 'io_readline', with +** the following upvalues: +** 1) The file being read (first value in the stack) +** 2) the number of arguments to read +** 3) a boolean, true iff file has to be closed when finished ('toclose') +** *) a variable number of format arguments (rest of the stack) +*/ static void aux_lines (lua_State *L, int toclose) { int n = lua_gettop(L) - 1; /* number of arguments to read */ luaL_argcheck(L, n <= MAXARGLINE, MAXARGLINE + 2, "too many arguments"); + lua_pushvalue(L, 1); /* file */ lua_pushinteger(L, n); /* number of arguments to read */ lua_pushboolean(L, toclose); /* close/not close file when finished */ - lua_rotate(L, 2, 2); /* move 'n' and 'toclose' to their positions */ + lua_rotate(L, 2, 3); /* move the three values to their positions */ lua_pushcclosure(L, io_readline, 3 + n); } @@ -371,6 +381,11 @@ static int f_lines (lua_State *L) { } +/* +** Return an iteration function for 'io.lines'. If file has to be +** closed, also returns the file itself as a second result (to be +** closed as the state at the exit of a generic for). +*/ static int io_lines (lua_State *L) { int toclose; if (lua_isnone(L, 1)) lua_pushnil(L); /* at least one argument */ @@ -386,8 +401,13 @@ static int io_lines (lua_State *L) { lua_replace(L, 1); /* put file at index 1 */ toclose = 1; /* close it after iteration */ } - aux_lines(L, toclose); - return 1; + aux_lines(L, toclose); /* push iteration function */ + if (toclose) { + lua_pushvalue(L, 1); /* file will be second result */ + return 2; + } + else + return 1; } @@ -453,7 +473,7 @@ static int readdigits (RN *rn, int hex) { /* ** Read a number: first reads a valid prefix of a numeral into a buffer. ** Then it calls 'lua_stringtonumber' to check whether the format is -** correct and to convert it to a Lua number +** correct and to convert it to a Lua number. */ static int read_number (lua_State *L, FILE *f) { RN rn; @@ -604,6 +624,9 @@ static int f_read (lua_State *L) { } +/* +** Iteration function for 'lines'. +*/ static int io_readline (lua_State *L) { LStream *p = (LStream *)lua_touserdata(L, lua_upvalueindex(1)); int i; @@ -624,8 +647,8 @@ static int io_readline (lua_State *L) { return luaL_error(L, "%s", lua_tostring(L, -n + 1)); } if (lua_toboolean(L, lua_upvalueindex(3))) { /* generator created file? */ - lua_settop(L, 0); - lua_pushvalue(L, lua_upvalueindex(1)); + lua_settop(L, 0); /* clear stack */ + lua_pushvalue(L, lua_upvalueindex(1)); /* push file at index 1 */ aux_close(L); /* close it */ } return 0; diff --git a/lparser.c b/lparser.c index 9419f880bd..a5b84aa1e8 100644 --- a/lparser.c +++ b/lparser.c @@ -1413,6 +1413,7 @@ static void forlist (LexState *ls, TString *indexname) { /* create control variables */ new_localvarliteral(ls, "(for generator)"); new_localvarliteral(ls, "(for state)"); + markupval(fs, fs->nactvar); /* state may create an upvalue */ new_localvarliteral(ls, "(for control)"); /* create declared variables */ new_localvar(ls, indexname); diff --git a/lvm.c b/lvm.c index aad965d608..1535700fc9 100644 --- a/lvm.c +++ b/lvm.c @@ -866,7 +866,8 @@ void luaV_finishOp (lua_State *L) { #define ProtectNT(exp) (savepc(L), (exp), updatetrap(ci)) /* -** Protect code that will finish the loop (returns). +** Protect code that will finish the loop (returns) or can only raise +** errors. */ #define halfProtect(exp) (savepc(L), (exp)) @@ -1457,7 +1458,8 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_TBC) { - luaF_newtbcupval(L, ra); /* create new to-be-closed upvalue */ + /* create new to-be-closed upvalue */ + halfProtect(luaF_newtbcupval(L, ra)); vmbreak; } vmcase(OP_JMP) { @@ -1745,21 +1747,34 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_TFORPREP) { + /* is 'state' a function or has a '__close' metamethod? */ + if (ttisfunction(s2v(ra + 1)) || + !ttisnil(luaT_gettmbyobj(L, s2v(ra + 1), TM_CLOSE))) { + /* create to-be-closed upvalue for it */ + halfProtect(luaF_newtbcupval(L, ra + 1)); + } pc += GETARG_Bx(i); - vmbreak; + i = *(pc++); /* go to next instruction */ + lua_assert(GET_OPCODE(i) == OP_TFORCALL && ra == RA(i)); + goto l_tforcall; } vmcase(OP_TFORCALL) { - StkId cb = ra + 3; /* call base */ - setobjs2s(L, cb+2, ra+2); - setobjs2s(L, cb+1, ra+1); - setobjs2s(L, cb, ra); - L->top = cb + 3; /* func. + 2 args (state and index) */ - Protect(luaD_call(L, cb, GETARG_C(i))); - if (trap) /* keep 'base' correct for next instruction */ - updatebase(ci); + l_tforcall: + /* 'ra' has the iterator function, 'ra + 1' has the state, + and 'ra + 2' has the control variable. The call will use + the stack after these values (starting at 'ra + 3') + */ + /* push function, state, and control variable */ + memcpy(ra + 3, ra, 3 * sizeof(*ra)); + L->top = ra + 6; + Protect(luaD_call(L, ra + 3, GETARG_C(i))); /* do the call */ + if (trap) { /* stack may have changed? */ + updatebase(ci); /* keep 'base' correct */ + ra = RA(i); /* keep 'ra' correct for next instruction */ + } i = *(pc++); /* go to next instruction */ - ra = RA(i); /* get its 'ra' */ - lua_assert(GET_OPCODE(i) == OP_TFORLOOP); + ra += 2; /* adjust for next instruction */ + lua_assert(GET_OPCODE(i) == OP_TFORLOOP && ra == RA(i)); goto l_tforloop; } vmcase(OP_TFORLOOP) { diff --git a/testes/files.lua b/testes/files.lua index 9aae591324..a11c5e617d 100644 --- a/testes/files.lua +++ b/testes/files.lua @@ -462,13 +462,13 @@ X - y; ]]:close() _G.X = 1 -assert(not load(io.lines(file))) +assert(not load((io.lines(file)))) collectgarbage() -- to close file in previous iteration -load(io.lines(file, "L"))() +load((io.lines(file, "L")))() assert(_G.X == 2) -load(io.lines(file, 1))() +load((io.lines(file, 1)))() assert(_G.X == 4) -load(io.lines(file, 3))() +load((io.lines(file, 3)))() assert(_G.X == 8) print('+') diff --git a/testes/locals.lua b/testes/locals.lua index 65b145dbb5..1e0f525ba4 100644 --- a/testes/locals.lua +++ b/testes/locals.lua @@ -108,7 +108,7 @@ print'+' if rawget(_G, "T") then -- testing clearing of dead elements from tables collectgarbage("stop") -- stop GC - local a = {[{}] = 4, [3] = 0, alo = 1, + local a = {[{}] = 4, [3] = 0, alo = 1, a1234567890123456789012345678901234567890 = 10} local t = T.querytab(a) @@ -360,6 +360,54 @@ end) co() -- start coroutine assert(co == nil) -- eventually it will be collected + +-- to-be-closed variables in generic for loops +do + local numopen = 0 + local function open (x) + numopen = numopen + 1 + return + function () -- iteraction function + x = x - 1 + if x > 0 then return x end + end, + function () -- closing function + numopen = numopen - 1 + end + end + + local s = 0 + for i in open(10) do + s = s + i + end + assert(s == 45 and numopen == 0) + + local s = 0 + for i in open(10) do + if i < 5 then break end + s = s + i + end + assert(s == 35 and numopen == 0) + + -- repeat test with '__open' metamethod instead of a function + local function open (x) + numopen = numopen + 1 + return + function (t) -- iteraction function + t[1] = t[1] - 1 + if t[1] > 0 then return t[1] end + end, + setmetatable({x}, {__close = function () numopen = numopen - 1 end}) + end + + local s = 0 + for i in open(10) do + if (i < 5) then break end + s = s + i + end + assert(s == 35 and numopen == 0) +end + print('OK') return 5,f From 2fc6b55dae7a120b4272ca0e9c356d1f96053bd9 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 31 Oct 2018 16:25:29 -0300 Subject: [PATCH 0346/1145] Removed resource-related "emergency collections" New to-be-closed variables is a better way to ensure the proper release of resources. --- lauxlib.c | 43 ------------------------------------------- lauxlib.h | 2 -- liolib.c | 21 ++------------------- loslib.c | 2 -- manual/manual.of | 14 -------------- 5 files changed, 2 insertions(+), 80 deletions(-) diff --git a/lauxlib.c b/lauxlib.c index 53b8c9bb34..78dfb4e952 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -290,49 +290,6 @@ LUALIB_API int luaL_execresult (lua_State *L, int stat) { /* }====================================================== */ -/* -** {====================================================== -** 'luaL_resourcetryagain' -** This function uses 'errno' to check whether the last error was -** related to lack of resources (e.g., not enough memory or too many -** open files). If so, the function performs a full garbage collection -** to try to release resources, and then it returns 1 to signal to -** the caller that it is worth trying again the failed operation. -** Otherwise, it returns 0. Because error codes are not ANSI C, the -** code must handle any combination of error codes that are defined. -** ======================================================= -*/ - -LUALIB_API int luaL_resourcetryagain (lua_State *L) { - -/* these are the resource-related errors in Linux */ -#if defined(EMFILE) || defined(ENFILE) || defined(ENOMEM) - -#if !defined(EMFILE) /* too many open files in the process */ -#define EMFILE -1 /* if not defined, use an impossible value */ -#endif - -#if !defined(ENFILE) /* too many open files in the system */ -#define ENFILE -1 -#endif - -#if !defined(ENOMEM) /* not enough memory */ -#define ENOMEM -1 -#endif - - if (errno == EMFILE || errno == ENFILE || errno == ENOMEM) { - lua_gc(L, LUA_GCCOLLECT); /* try to release resources with a full GC */ - return 1; /* signal to try again the creation */ - } - -#endif - - return 0; /* else, asume errors are not due to lack of resources */ - -} - -/* }====================================================== */ - /* ** {====================================================== diff --git a/lauxlib.h b/lauxlib.h index cd4d01e50a..9ec0f5317b 100644 --- a/lauxlib.h +++ b/lauxlib.h @@ -77,8 +77,6 @@ LUALIB_API int (luaL_checkoption) (lua_State *L, int arg, const char *def, LUALIB_API int (luaL_fileresult) (lua_State *L, int stat, const char *fname); LUALIB_API int (luaL_execresult) (lua_State *L, int stat); -LUALIB_API int (luaL_resourcetryagain) (lua_State *L); - /* predefined references */ #define LUA_NOREF (-2) diff --git a/liolib.c b/liolib.c index 5881b0293a..b2a2fec8a1 100644 --- a/liolib.c +++ b/liolib.c @@ -246,22 +246,9 @@ static LStream *newfile (lua_State *L) { } -/* -** Equivalent to 'fopen', but if it fails due to a lack of resources -** (see 'luaL_resourcetryagain'), do an "emergency" garbage collection -** to try to close some files and then tries to open the file again. -*/ -static FILE *trytoopen (lua_State *L, const char *path, const char *mode) { - FILE *f = fopen(path, mode); - if (f == NULL && luaL_resourcetryagain(L)) /* resource failure? */ - f = fopen(path, mode); /* try to open again */ - return f; -} - - static void opencheck (lua_State *L, const char *fname, const char *mode) { LStream *p = newfile(L); - p->f = trytoopen(L, fname, mode); + p->f = fopen(fname, mode); if (p->f == NULL) luaL_error(L, "cannot open file '%s' (%s)", fname, strerror(errno)); } @@ -273,7 +260,7 @@ static int io_open (lua_State *L) { LStream *p = newfile(L); const char *md = mode; /* to traverse/check mode */ luaL_argcheck(L, l_checkmode(md), 2, "invalid mode"); - p->f = trytoopen(L, filename, mode); + p->f = fopen(filename, mode); return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1; } @@ -292,8 +279,6 @@ static int io_popen (lua_State *L) { const char *mode = luaL_optstring(L, 2, "r"); LStream *p = newprefile(L); p->f = l_popen(L, filename, mode); - if (p->f == NULL && luaL_resourcetryagain(L)) /* resource failure? */ - p->f = l_popen(L, filename, mode); /* try to open again */ p->closef = &io_pclose; return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1; } @@ -302,8 +287,6 @@ static int io_popen (lua_State *L) { static int io_tmpfile (lua_State *L) { LStream *p = newfile(L); p->f = tmpfile(); - if (p->f == NULL && luaL_resourcetryagain(L)) /* resource failure? */ - p->f = tmpfile(); /* try to open again */ return (p->f == NULL) ? luaL_fileresult(L, 0, NULL) : 1; } diff --git a/loslib.c b/loslib.c index 1962f55fb5..8809e5ea2a 100644 --- a/loslib.c +++ b/loslib.c @@ -166,8 +166,6 @@ static int os_tmpname (lua_State *L) { char buff[LUA_TMPNAMBUFSIZE]; int err; lua_tmpnam(buff, err); - if (err && luaL_resourcetryagain(L)) /* resource failure? */ - lua_tmpnam(buff, err); /* try again */ if (err) return luaL_error(L, "unable to generate a unique filename"); lua_pushstring(L, buff); diff --git a/manual/manual.of b/manual/manual.of index d8bac5dad9..91ba8c46cf 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -5535,20 +5535,6 @@ Leaves a copy of the module on the stack. } -@APIEntry{int luaL_resourcetryagain (lua_State *L);| -@apii{0,0,m} - -Try to release resources in case of errors. -This function uses @id{errno} to check whether the last error was -related to lack of resources (e.g., not enough memory or too many -open files). -If so, the function performs a full garbage collection -to try to release resources, and then it returns 1 to signal to -the caller that it is worth trying again the failed operation. -Otherwise, it returns 0. - -} - @APIEntry{void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup);| @apii{nup,0,m} From e8c779736f3029df353038352c14c8ab63728811 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 1 Nov 2018 13:21:00 -0300 Subject: [PATCH 0347/1145] Removed internal cache for closures The mechanism of "caching the last closure created for a prototype to try to reuse it the next time a closure for that prototype is created" was removed. There are several reasons: - It is hard to find a natural example where this cache has a measurable impact on performance. - Programmers already perceive closure creation as something slow, so they tend to avoid it inside hot paths. (Any case where the cache could reuse a closure can be rewritten predefining the closure in some variable and using that variable.) - The implementation was somewhat complex, due to a bad interaction with the generational collector. (Typically, new closures are new, while prototypes are old. So, the cache breaks the invariant that old objects should not point to new ones.) --- lfunc.c | 2 -- lgc.c | 64 +--------------------------------------------- lgc.h | 4 --- lobject.h | 2 -- lstate.c | 2 +- lstate.h | 3 --- ltests.c | 4 --- lvm.c | 39 ++-------------------------- testes/closure.lua | 13 +++++----- 9 files changed, 10 insertions(+), 123 deletions(-) diff --git a/lfunc.c b/lfunc.c index 15874f88b2..aa6ce58f17 100644 --- a/lfunc.c +++ b/lfunc.c @@ -219,8 +219,6 @@ Proto *luaF_newproto (lua_State *L) { f->p = NULL; f->sizep = 0; f->code = NULL; - f->cache = NULL; - f->cachemiss = 0; f->sizecode = 0; f->lineinfo = NULL; f->sizelineinfo = 0; diff --git a/lgc.c b/lgc.c index 9d196a18b7..95a8ad5b97 100644 --- a/lgc.c +++ b/lgc.c @@ -221,27 +221,6 @@ void luaC_barrierback_ (lua_State *L, GCObject *o) { } -/* -** Barrier for prototype's cache of closures. It turns the prototype -** back to gray (it was black). For an 'OLD1' prototype, making it -** gray stops it from being visited by 'markold', so it is linked in -** the 'grayagain' list to ensure it will be visited. For other ages, -** it goes to the 'protogray' list, as only its 'cache' field needs to -** be revisited. (A prototype to be in this barrier must be already -** finished, so its other fields cannot change and do not need to be -** revisited.) -*/ -LUAI_FUNC void luaC_protobarrier_ (lua_State *L, Proto *p) { - global_State *g = G(L); - lua_assert(g->gckind != KGC_GEN || isold(p)); - if (getage(p) == G_OLD1) /* still need to be visited? */ - linkgclist(p, g->grayagain); /* link it in 'grayagain' */ - else - linkgclist(p, g->protogray); /* link it in 'protogray' */ - black2gray(p); /* make prototype gray */ -} - - void luaC_fix (lua_State *L, GCObject *o) { global_State *g = G(L); lua_assert(g->allgc == o); /* object must be 1st in 'allgc' list! */ @@ -379,7 +358,7 @@ static int remarkupvals (global_State *g) { */ static void restartcollection (global_State *g) { g->gray = g->grayagain = NULL; - g->weak = g->allweak = g->ephemeron = g->protogray = NULL; + g->weak = g->allweak = g->ephemeron = NULL; markobject(g, g->mainthread); markvalue(g, &g->l_registry); markmt(g); @@ -534,32 +513,6 @@ static int traverseudata (global_State *g, Udata *u) { } -/* -** Check the cache of a prototype, to keep invariants. If the -** cache is white, clear it. (A cache should not prevent the -** collection of its reference.) Otherwise, if in generational -** mode, check the generational invariant. If the cache is old, -** everything is ok. If the prototype is 'OLD0', everything -** is ok too. (It will naturally be visited again.) If the -** prototype is older than 'OLD0', then its cache (which is new) -** must be visited again in the next collection, so the prototype -** goes to the 'protogray' list. (If the prototype has a cache, -** it is already immutable and does not need other barriers; -** then, it can become gray without problems for its other fields.) -*/ -static void checkprotocache (global_State *g, Proto *p) { - if (p->cache) { - if (iswhite(p->cache)) - p->cache = NULL; /* allow cache to be collected */ - else if (g->gckind == KGC_GEN && !isold(p->cache) && getage(p) >= G_OLD1) { - linkgclist(p, g->protogray); /* link it in 'protogray' */ - black2gray(p); /* make prototype gray */ - } - } - p->cachemiss = 0; /* restart counting */ -} - - /* ** Traverse a prototype. (While a prototype is being build, its ** arrays can be larger than needed; the extra slots are filled with @@ -567,7 +520,6 @@ static void checkprotocache (global_State *g, Proto *p) { */ static int traverseproto (global_State *g, Proto *f) { int i; - checkprotocache(g, f); markobjectN(g, f->source); for (i = 0; i < f->sizek; i++) /* mark literals */ markvalue(g, &f->k[i]); @@ -696,19 +648,6 @@ static void convergeephemerons (global_State *g) { ** ======================================================= */ -static void clearprotolist (global_State *g) { - GCObject *p = g->protogray; - g->protogray = NULL; - while (p != NULL) { - Proto *pp = gco2p(p); - GCObject *next = pp->gclist; - lua_assert(isgray(pp) && (pp->cache != NULL || pp->cachemiss >= MAXMISS)); - gray2black(pp); - checkprotocache(g, pp); - p = next; - } -} - /* ** clear entries with unmarked keys from all weaktables in list 'l' @@ -1439,7 +1378,6 @@ static lu_mem atomic (lua_State *L) { clearbyvalues(g, g->weak, origweak); clearbyvalues(g, g->allweak, origall); luaS_clearcache(g); - clearprotolist(g); g->currentwhite = cast_byte(otherwhite(g)); /* flip current white */ lua_assert(g->gray == NULL); return work; /* estimate of slots marked by 'atomic' */ diff --git a/lgc.h b/lgc.h index e0a8806b14..6b1c286153 100644 --- a/lgc.h +++ b/lgc.h @@ -164,9 +164,6 @@ (isblack(p) && iswhite(o)) ? \ luaC_barrier_(L,obj2gco(p),obj2gco(o)) : cast_void(0)) -#define luaC_protobarrier(L,p,o) \ - (isblack(p) ? luaC_protobarrier_(L,p) : cast_void(0)) - LUAI_FUNC void luaC_fix (lua_State *L, GCObject *o); LUAI_FUNC void luaC_freeallobjects (lua_State *L); LUAI_FUNC void luaC_step (lua_State *L); @@ -175,7 +172,6 @@ LUAI_FUNC void luaC_fullgc (lua_State *L, int isemergency); LUAI_FUNC GCObject *luaC_newobj (lua_State *L, int tt, size_t sz); LUAI_FUNC void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v); LUAI_FUNC void luaC_barrierback_ (lua_State *L, GCObject *o); -LUAI_FUNC void luaC_protobarrier_ (lua_State *L, Proto *p); LUAI_FUNC void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt); LUAI_FUNC void luaC_changemode (lua_State *L, int newmode); diff --git a/lobject.h b/lobject.h index ea3511deba..df31a1a0d1 100644 --- a/lobject.h +++ b/lobject.h @@ -505,7 +505,6 @@ typedef struct Proto { lu_byte numparams; /* number of fixed (named) parameters */ lu_byte is_vararg; lu_byte maxstacksize; /* number of registers needed by this function */ - lu_byte cachemiss; /* count for successive misses for 'cache' field */ int sizeupvalues; /* size of 'upvalues' */ int sizek; /* size of 'k' */ int sizecode; @@ -516,7 +515,6 @@ typedef struct Proto { int linedefined; /* debug information */ int lastlinedefined; /* debug information */ TValue *k; /* constants used by the function */ - struct LClosure *cache; /* last-created closure with this prototype */ Instruction *code; /* opcodes */ struct Proto **p; /* functions defined inside the function */ Upvaldesc *upvalues; /* upvalue information */ diff --git a/lstate.c b/lstate.c index 4a2453d15a..9d39995964 100644 --- a/lstate.c +++ b/lstate.c @@ -340,7 +340,7 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { g->finobjsur = g->finobjold = g->finobjrold = NULL; g->sweepgc = NULL; g->gray = g->grayagain = NULL; - g->weak = g->ephemeron = g->allweak = g->protogray = NULL; + g->weak = g->ephemeron = g->allweak = NULL; g->twups = NULL; g->totalbytes = sizeof(LG); g->GCdebt = 0; diff --git a/lstate.h b/lstate.h index f08c23554c..ce33770748 100644 --- a/lstate.h +++ b/lstate.h @@ -43,8 +43,6 @@ ** 'weak': tables with weak values to be cleared; ** 'ephemeron': ephemeron tables with white->white entries; ** 'allweak': tables with weak keys and/or weak values to be cleared. -** There is also a list 'protogray' for prototypes that need to have -** their caches cleared. */ @@ -186,7 +184,6 @@ typedef struct global_State { GCObject *weak; /* list of tables with weak values */ GCObject *ephemeron; /* list of ephemeron tables (weak keys) */ GCObject *allweak; /* list of all-weak tables */ - GCObject *protogray; /* list of prototypes with "new" caches */ GCObject *tobefnz; /* list of userdata to be GC */ GCObject *fixedgc; /* list of objects not to be collected */ /* fields for generational collector */ diff --git a/ltests.c b/ltests.c index 7cf0889197..aba44b8731 100644 --- a/ltests.c +++ b/ltests.c @@ -283,7 +283,6 @@ static void checkudata (global_State *g, Udata *u) { static void checkproto (global_State *g, Proto *f) { int i; GCObject *fgc = obj2gco(f); - checkobjref(g, fgc, f->cache); checkobjref(g, fgc, f->source); for (i=0; isizek; i++) { if (ttisstring(f->k + i)) @@ -417,8 +416,6 @@ static void checkobject (global_State *g, GCObject *o, int maybedead, getage(o) == G_TOUCHED1 || getage(o) == G_OLD0 || o->tt == LUA_TTHREAD || - (o->tt == LUA_TPROTO && - (gco2p(o)->cache != NULL || gco2p(o)->cachemiss >= MAXMISS)) || (o->tt == LUA_TUPVAL && upisopen(gco2upv(o)))); } } @@ -452,7 +449,6 @@ static void checkgrays (global_State *g) { checkgraylist(g, g->grayagain); checkgraylist(g, g->weak); checkgraylist(g, g->ephemeron); - checkgraylist(g, g->protogray); } diff --git a/lvm.c b/lvm.c index 1535700fc9..d96710558f 100644 --- a/lvm.c +++ b/lvm.c @@ -683,31 +683,9 @@ lua_Integer luaV_shiftl (lua_Integer x, lua_Integer y) { } -/* -** check whether cached closure in prototype 'p' may be reused, that is, -** whether there is a cached closure with the same upvalues needed by -** new closure to be created. -*/ -static LClosure *getcached (Proto *p, UpVal **encup, StkId base) { - LClosure *c = p->cache; - if (c != NULL) { /* is there a cached closure? */ - int nup = p->sizeupvalues; - Upvaldesc *uv = p->upvalues; - int i; - for (i = 0; i < nup; i++) { /* check whether it has right upvalues */ - TValue *v = uv[i].instack ? s2v(base + uv[i].idx) : encup[uv[i].idx]->v; - if (c->upvals[i]->v != v) - return NULL; /* wrong upvalue; cannot reuse closure */ - } - p->cachemiss = 0; /* got a hit */ - } - return c; /* return cached closure (or NULL if no cached closure) */ -} - - /* ** create a new Lua closure, push it in the stack, and initialize -** its upvalues. ??? +** its upvalues. */ static void pushclosure (lua_State *L, Proto *p, UpVal **encup, StkId base, StkId ra) { @@ -724,13 +702,6 @@ static void pushclosure (lua_State *L, Proto *p, UpVal **encup, StkId base, ncl->upvals[i] = encup[uv[i].idx]; luaC_objbarrier(L, ncl, ncl->upvals[i]); } - if (p->cachemiss >= MAXMISS) /* too many missings? */ - p->cache = NULL; /* give up cache */ - else { - p->cache = ncl; /* save it on cache for reuse */ - luaC_protobarrier(L, p, ncl); - p->cachemiss++; - } } @@ -1811,13 +1782,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { } vmcase(OP_CLOSURE) { Proto *p = cl->p->p[GETARG_Bx(i)]; - LClosure *ncl = getcached(p, cl->upvals, base); /* cached closure */ - if (ncl == NULL) { /* no match? */ - savestate(L, ci); /* in case of allocation errors */ - pushclosure(L, p, cl->upvals, base, ra); /* create a new one */ - } - else - setclLvalue2s(L, ra, ncl); /* push cashed closure */ + halfProtect(pushclosure(L, p, cl->upvals, base, ra)); checkGC(L, ra + 1); vmbreak; } diff --git a/testes/closure.lua b/testes/closure.lua index 5d090d9116..cdeaebaa02 100644 --- a/testes/closure.lua +++ b/testes/closure.lua @@ -44,18 +44,17 @@ assert(B.g == 19) -- testing equality a = {} -collectgarbage"stop" -for i = 1, 5 do a[i] = function (x) return x + a + _ENV end end -collectgarbage"restart" -assert(a[3] == a[4] and a[4] == a[5]) for i = 1, 5 do a[i] = function (x) return i + a + _ENV end end assert(a[3] ~= a[4] and a[4] ~= a[5]) -local function f() - return function (x) return math.sin(_ENV[x]) end +do + local a = function (x) return math.sin(_ENV[x]) end + local function f() + return a + end + assert(f() == f()) end -assert(f() == f()) -- testing closures with 'for' control variable From 5e76a4fd313a8690d300085c4e8fcb9dca50c01a Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 5 Nov 2018 16:10:42 -0200 Subject: [PATCH 0348/1145] New macros for arithmetic/bitwise operations in 'luaV_execute' The repetitive code of the arithmetic and bitwise operators in the main iterpreter loop was moved to appropriate macros. (As a detail, the function 'luaV_div' was renamed 'luaV_idiv', as it does an "integer division" (floor division). --- lobject.c | 2 +- ltm.c | 2 +- ltm.h | 2 +- lvm.c | 368 ++++++++++++++++++++++-------------------------------- lvm.h | 2 +- 5 files changed, 156 insertions(+), 220 deletions(-) diff --git a/lobject.c b/lobject.c index d011c85fc3..3ce052c29a 100644 --- a/lobject.c +++ b/lobject.c @@ -83,7 +83,7 @@ static lua_Integer intarith (lua_State *L, int op, lua_Integer v1, case LUA_OPSUB:return intop(-, v1, v2); case LUA_OPMUL:return intop(*, v1, v2); case LUA_OPMOD: return luaV_mod(L, v1, v2); - case LUA_OPIDIV: return luaV_div(L, v1, v2); + case LUA_OPIDIV: return luaV_idiv(L, v1, v2); case LUA_OPBAND: return intop(&, v1, v2); case LUA_OPBOR: return intop(|, v1, v2); case LUA_OPBXOR: return intop(^, v1, v2); diff --git a/ltm.c b/ltm.c index 53e15c7fdc..23a97a62cc 100644 --- a/ltm.c +++ b/ltm.c @@ -176,7 +176,7 @@ void luaT_trybinassocTM (lua_State *L, const TValue *p1, const TValue *p2, } -void luaT_trybiniTM (lua_State *L, const TValue *p1, int i2, +void luaT_trybiniTM (lua_State *L, const TValue *p1, lua_Integer i2, int inv, StkId res, TMS event) { TValue aux; setivalue(&aux, i2); diff --git a/ltm.h b/ltm.h index e8560f82cb..fad47842a1 100644 --- a/ltm.h +++ b/ltm.h @@ -77,7 +77,7 @@ LUAI_FUNC void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2, StkId res, TMS event); LUAI_FUNC void luaT_trybinassocTM (lua_State *L, const TValue *p1, const TValue *p2, StkId res, int inv, TMS event); -LUAI_FUNC void luaT_trybiniTM (lua_State *L, const TValue *p1, int i2, +LUAI_FUNC void luaT_trybiniTM (lua_State *L, const TValue *p1, lua_Integer i2, int inv, StkId res, TMS event); LUAI_FUNC int luaT_callorderTM (lua_State *L, const TValue *p1, const TValue *p2, TMS event); diff --git a/lvm.c b/lvm.c index d96710558f..9977bda4f7 100644 --- a/lvm.c +++ b/lvm.c @@ -620,7 +620,7 @@ void luaV_objlen (lua_State *L, StkId ra, const TValue *rb) { ** 'floor(q) == trunc(q)' when 'q >= 0' or when 'q' is integer, ** otherwise 'floor(q) == trunc(q) - 1'. */ -lua_Integer luaV_div (lua_State *L, lua_Integer m, lua_Integer n) { +lua_Integer luaV_idiv (lua_State *L, lua_Integer m, lua_Integer n) { if (unlikely(l_castS2U(n) + 1u <= 1u)) { /* special cases: -1 or 0 */ if (n == 0) luaG_runerror(L, "attempt to divide by zero"); @@ -638,7 +638,7 @@ lua_Integer luaV_div (lua_State *L, lua_Integer m, lua_Integer n) { /* ** Integer modulus; return 'm % n'. (Assume that C '%' with ** negative operands follows C99 behavior. See previous comment -** about luaV_div.) +** about luaV_idiv.) */ lua_Integer luaV_mod (lua_State *L, lua_Integer m, lua_Integer n) { if (unlikely(l_castS2U(n) + 1u <= 1u)) { /* special cases: -1 or 0 */ @@ -770,11 +770,126 @@ void luaV_finishOp (lua_State *L) { /* ** {================================================================== -** Function 'luaV_execute': main interpreter loop +** Macros for arithmetic/bitwise opcodes in 'luaV_execute' ** =================================================================== */ +#define l_addi(L,a,b) intop(+, a, b) +#define l_subi(L,a,b) intop(-, a, b) +#define l_muli(L,a,b) intop(*, a, b) +#define l_band(L,a,b) intop(&, a, b) +#define l_bor(L,a,b) intop(|, a, b) +#define l_bxor(L,a,b) intop(^, a, b) + + +/* +** Auxiliary macro for arithmetic operations over floats and others +** with immediate operand. 'fop' is the float operation; 'tm' is the +** corresponding metamethod; 'flip' is true if operands were flipped. +*/ +#define op_arithfI_aux(L,v1,imm,fop,tm,flip) { \ + lua_Number nb; \ + if (tonumberns(v1, nb)) { \ + setfltvalue(s2v(ra), fop(L, nb, cast_num(imm))); \ + } \ + else \ + Protect(luaT_trybiniTM(L, v1, imm, flip, ra, tm)); } + + +/* +** Arithmetic operations over floats and others with immediate operand. +*/ +#define op_arithfI(L,fop,tm) { \ + TValue *v1 = vRB(i); \ + int imm = GETARG_sC(i); \ + op_arithfI_aux(L, v1, imm, fop, tm, 0); } + +/* +** Arithmetic operations with immediate operands. 'iop' is the integer +** operation. +*/ +#define op_arithI(L,iop,fop,tm,flip) { \ + TValue *v1 = vRB(i); \ + int imm = GETARG_sC(i); \ + if (ttisinteger(v1)) { \ + setivalue(s2v(ra), iop(L, ivalue(v1), imm)); \ + } \ + else op_arithfI_aux(L, v1, imm, fop, tm, flip); } + + +/* +** Auxiliary function for arithmetic operations over floats and others +** with two register operands. +*/ +#define op_arithf_aux(L,v1,v2,fop,tm) { \ + lua_Number n1; lua_Number n2; \ + if (tonumberns(v1, n1) && tonumberns(v2, n2)) { \ + setfltvalue(s2v(ra), fop(L, n1, n2)); \ + } \ + else \ + Protect(luaT_trybinTM(L, v1, v2, ra, tm)); } + + +/* +** Arithmetic operations over floats and others with register operands. +*/ +#define op_arithf(L,fop,tm) { \ + TValue *v1 = vRB(i); \ + TValue *v2 = vRC(i); \ + op_arithf_aux(L, v1, v2, fop, tm); } + + +/* +** Arithmetic operations with register operands. +*/ +#define op_arith(L,iop,fop,tm) { \ + TValue *v1 = vRB(i); \ + TValue *v2 = vRC(i); \ + if (ttisinteger(v1) && ttisinteger(v2)) { \ + lua_Integer i1 = ivalue(v1); lua_Integer i2 = ivalue(v2); \ + setivalue(s2v(ra), iop(L, i1, i2)); \ + } \ + else op_arithf_aux(L, v1, v2, fop, tm); } + + +/* +** Bitwise operations with constant operand. +*/ +#define op_bitwiseK(L,op,tm) { \ + TValue *v1 = vRB(i); \ + TValue *v2 = KC(i); \ + lua_Integer i1; \ + lua_Integer i2 = ivalue(v2); \ + if (tointegerns(v1, &i1)) { \ + setivalue(s2v(ra), op(L, i1, i2)); \ + } \ + else \ + Protect(luaT_trybiniTM(L, v1, i2, TESTARG_k(i), ra, tm)); } + + +/* +** Bitwise operations with register operands. +*/ +#define op_bitwise(L,op,tm) { \ + TValue *v1 = vRB(i); \ + TValue *v2 = vRC(i); \ + lua_Integer i1; lua_Integer i2; \ + if (tointegerns(v1, &i1) && tointegerns(v2, &i2)) { \ + setivalue(s2v(ra), op(L, i1, i2)); \ + } \ + else \ + Protect(luaT_trybinTM(L, v1, v2, ra, tm)); } + +/* }================================================================== */ + + +/* +** {================================================================== +** Function 'luaV_execute': main interpreter loop +** =================================================================== +*/ + /* ** some macros for common tasks in 'luaV_execute' */ @@ -1075,221 +1190,83 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_ADDI) { - TValue *rb = vRB(i); - int ic = GETARG_sC(i); - lua_Number nb; - if (ttisinteger(rb)) { - setivalue(s2v(ra), intop(+, ivalue(rb), ic)); - } - else if (tonumberns(rb, nb)) { - setfltvalue(s2v(ra), luai_numadd(L, nb, cast_num(ic))); - } - else - Protect(luaT_trybiniTM(L, rb, ic, GETARG_k(i), ra, TM_ADD)); + op_arithI(L, l_addi, luai_numadd, TM_ADD, GETARG_k(i)); vmbreak; } vmcase(OP_SUBI) { - TValue *rb = vRB(i); - int ic = GETARG_sC(i); - lua_Number nb; - if (ttisinteger(rb)) { - setivalue(s2v(ra), intop(-, ivalue(rb), ic)); - } - else if (tonumberns(rb, nb)) { - setfltvalue(s2v(ra), luai_numsub(L, nb, cast_num(ic))); - } - else - Protect(luaT_trybiniTM(L, rb, ic, 0, ra, TM_SUB)); + op_arithI(L, l_subi, luai_numsub, TM_SUB, 0); vmbreak; } vmcase(OP_MULI) { - TValue *rb = vRB(i); - int ic = GETARG_sC(i); - lua_Number nb; - if (ttisinteger(rb)) { - setivalue(s2v(ra), intop(*, ivalue(rb), ic)); - } - else if (tonumberns(rb, nb)) { - setfltvalue(s2v(ra), luai_nummul(L, nb, cast_num(ic))); - } - else - Protect(luaT_trybiniTM(L, rb, ic, GETARG_k(i), ra, TM_MUL)); + op_arithI(L, l_muli, luai_nummul, TM_MUL, GETARG_k(i)); vmbreak; } vmcase(OP_MODI) { - TValue *rb = vRB(i); - int ic = GETARG_sC(i); - lua_Number nb; - if (ttisinteger(rb)) { - setivalue(s2v(ra), luaV_mod(L, ivalue(rb), ic)); - } - else if (tonumberns(rb, nb)) { - lua_Number nc = cast_num(ic); - setfltvalue(s2v(ra), luaV_modf(L, nb, nc)); - } - else - Protect(luaT_trybiniTM(L, rb, ic, 0, ra, TM_MOD)); + op_arithI(L, luaV_mod, luaV_modf, TM_MOD, 0); vmbreak; } vmcase(OP_POWI) { - TValue *rb = vRB(i); - int ic = GETARG_sC(i); - lua_Number nb; - if (tonumberns(rb, nb)) { - lua_Number nc = cast_num(ic); - setfltvalue(s2v(ra), luai_numpow(L, nb, nc)); - } - else - Protect(luaT_trybiniTM(L, rb, ic, 0, ra, TM_POW)); + op_arithfI(L, luai_numpow, TM_POW); vmbreak; } vmcase(OP_DIVI) { - TValue *rb = vRB(i); - int ic = GETARG_sC(i); - lua_Number nb; - if (tonumberns(rb, nb)) { - lua_Number nc = cast_num(ic); - setfltvalue(s2v(ra), luai_numdiv(L, nb, nc)); - } - else - Protect(luaT_trybiniTM(L, rb, ic, 0, ra, TM_DIV)); + op_arithfI(L, luai_numdiv, TM_DIV); vmbreak; } vmcase(OP_IDIVI) { - TValue *rb = vRB(i); - int ic = GETARG_sC(i); - lua_Number nb; - if (ttisinteger(rb)) { - setivalue(s2v(ra), luaV_div(L, ivalue(rb), ic)); - } - else if (tonumberns(rb, nb)) { - lua_Number nc = cast_num(ic); - setfltvalue(s2v(ra), luai_numidiv(L, nb, nc)); - } - else - Protect(luaT_trybiniTM(L, rb, ic, 0, ra, TM_IDIV)); + op_arithI(L, luaV_idiv, luai_numidiv, TM_IDIV, 0); vmbreak; } vmcase(OP_ADD) { - TValue *rb = vRB(i); - TValue *rc = vRC(i); - lua_Number nb; lua_Number nc; - if (ttisinteger(rb) && ttisinteger(rc)) { - lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc); - setivalue(s2v(ra), intop(+, ib, ic)); - } - else if (tonumberns(rb, nb) && tonumberns(rc, nc)) { - setfltvalue(s2v(ra), luai_numadd(L, nb, nc)); - } - else - Protect(luaT_trybinTM(L, rb, rc, ra, TM_ADD)); + op_arith(L, l_addi, luai_numadd, TM_ADD); vmbreak; } vmcase(OP_SUB) { - TValue *rb = vRB(i); - TValue *rc = vRC(i); - lua_Number nb; lua_Number nc; - if (ttisinteger(rb) && ttisinteger(rc)) { - lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc); - setivalue(s2v(ra), intop(-, ib, ic)); - } - else if (tonumberns(rb, nb) && tonumberns(rc, nc)) { - setfltvalue(s2v(ra), luai_numsub(L, nb, nc)); - } - else - Protect(luaT_trybinTM(L, rb, rc, ra, TM_SUB)); + op_arith(L, l_subi, luai_numsub, TM_SUB); vmbreak; } vmcase(OP_MUL) { - TValue *rb = vRB(i); - TValue *rc = vRC(i); - lua_Number nb; lua_Number nc; - if (ttisinteger(rb) && ttisinteger(rc)) { - lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc); - setivalue(s2v(ra), intop(*, ib, ic)); - } - else if (tonumberns(rb, nb) && tonumberns(rc, nc)) { - setfltvalue(s2v(ra), luai_nummul(L, nb, nc)); - } - else - Protect(luaT_trybinTM(L, rb, rc, ra, TM_MUL)); + op_arith(L, l_muli, luai_nummul, TM_MUL); + vmbreak; + } + vmcase(OP_MOD) { + op_arith(L, luaV_mod, luaV_modf, TM_MOD); + vmbreak; + } + vmcase(OP_POW) { + op_arithf(L, luai_numpow, TM_POW); vmbreak; } vmcase(OP_DIV) { /* float division (always with floats) */ - TValue *rb = vRB(i); - TValue *rc = vRC(i); - lua_Number nb; lua_Number nc; - if (tonumberns(rb, nb) && tonumberns(rc, nc)) { - setfltvalue(s2v(ra), luai_numdiv(L, nb, nc)); - } - else - Protect(luaT_trybinTM(L, rb, rc, ra, TM_DIV)); + op_arithf(L, luai_numdiv, TM_DIV); + vmbreak; + } + vmcase(OP_IDIV) { /* floor division */ + op_arith(L, luaV_idiv, luai_numidiv, TM_IDIV); vmbreak; } vmcase(OP_BANDK) { - TValue *p1 = vRB(i); - TValue *p2 = KC(i); - lua_Integer i1; - if (tointegerns(p1, &i1)) { - setivalue(s2v(ra), intop(&, i1, ivalue(p2))); - } - else - Protect(luaT_trybinassocTM(L, p1, p2, ra, TESTARG_k(i), TM_BAND)); + op_bitwiseK(L, l_band, TM_BAND); vmbreak; } vmcase(OP_BORK) { - TValue *p1 = vRB(i); - TValue *p2 = KC(i); - lua_Integer i1; - if (tointegerns(p1, &i1)) { - setivalue(s2v(ra), intop(|, i1, ivalue(p2))); - } - else - Protect(luaT_trybinassocTM(L, p1, p2, ra, TESTARG_k(i), TM_BOR)); + op_bitwiseK(L, l_bor, TM_BOR); vmbreak; } vmcase(OP_BXORK) { - TValue *p1 = vRB(i); - TValue *p2 = KC(i); - lua_Integer i1; - if (tointegerns(p1, &i1)) { - setivalue(s2v(ra), intop(^, i1, ivalue(p2))); - } - else - Protect(luaT_trybinassocTM(L, p1, p2, ra, TESTARG_k(i), TM_BXOR)); + op_bitwiseK(L, l_bxor, TM_BXOR); vmbreak; } vmcase(OP_BAND) { - TValue *rb = vRB(i); - TValue *rc = vRC(i); - lua_Integer ib; lua_Integer ic; - if (tointegerns(rb, &ib) && tointegerns(rc, &ic)) { - setivalue(s2v(ra), intop(&, ib, ic)); - } - else - Protect(luaT_trybinTM(L, rb, rc, ra, TM_BAND)); + op_bitwise(L, l_band, TM_BAND); vmbreak; } vmcase(OP_BOR) { - TValue *rb = vRB(i); - TValue *rc = vRC(i); - lua_Integer ib; lua_Integer ic; - if (tointegerns(rb, &ib) && tointegerns(rc, &ic)) { - setivalue(s2v(ra), intop(|, ib, ic)); - } - else - Protect(luaT_trybinTM(L, rb, rc, ra, TM_BOR)); + op_bitwise(L, l_bor, TM_BOR); vmbreak; } vmcase(OP_BXOR) { - TValue *rb = vRB(i); - TValue *rc = vRC(i); - lua_Integer ib; lua_Integer ic; - if (tointegerns(rb, &ib) && tointegerns(rc, &ic)) { - setivalue(s2v(ra), intop(^, ib, ic)); - } - else - Protect(luaT_trybinTM(L, rb, rc, ra, TM_BXOR)); + op_bitwise(L, l_bxor, TM_BXOR); vmbreak; } vmcase(OP_SHRI) { @@ -1319,17 +1296,6 @@ void luaV_execute (lua_State *L, CallInfo *ci) { Protect(luaT_trybiniTM(L, rb, ic, 1, ra, TM_SHL)); vmbreak; } - vmcase(OP_SHL) { - TValue *rb = vRB(i); - TValue *rc = vRC(i); - lua_Integer ib; lua_Integer ic; - if (tointegerns(rb, &ib) && tointegerns(rc, &ic)) { - setivalue(s2v(ra), luaV_shiftl(ib, ic)); - } - else - Protect(luaT_trybinTM(L, rb, rc, ra, TM_SHL)); - vmbreak; - } vmcase(OP_SHR) { TValue *rb = vRB(i); TValue *rc = vRC(i); @@ -1341,45 +1307,15 @@ void luaV_execute (lua_State *L, CallInfo *ci) { Protect(luaT_trybinTM(L, rb, rc, ra, TM_SHR)); vmbreak; } - vmcase(OP_MOD) { - TValue *rb = vRB(i); - TValue *rc = vRC(i); - lua_Number nb; lua_Number nc; - if (ttisinteger(rb) && ttisinteger(rc)) { - lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc); - setivalue(s2v(ra), luaV_mod(L, ib, ic)); - } - else if (tonumberns(rb, nb) && tonumberns(rc, nc)) { - setfltvalue(s2v(ra), luaV_modf(L, nb, nc)); - } - else - Protect(luaT_trybinTM(L, rb, rc, ra, TM_MOD)); - vmbreak; - } - vmcase(OP_IDIV) { /* floor division */ - TValue *rb = vRB(i); - TValue *rc = vRC(i); - lua_Number nb; lua_Number nc; - if (ttisinteger(rb) && ttisinteger(rc)) { - lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc); - setivalue(s2v(ra), luaV_div(L, ib, ic)); - } - else if (tonumberns(rb, nb) && tonumberns(rc, nc)) { - setfltvalue(s2v(ra), luai_numidiv(L, nb, nc)); - } - else - Protect(luaT_trybinTM(L, rb, rc, ra, TM_IDIV)); - vmbreak; - } - vmcase(OP_POW) { + vmcase(OP_SHL) { TValue *rb = vRB(i); TValue *rc = vRC(i); - lua_Number nb; lua_Number nc; - if (tonumberns(rb, nb) && tonumberns(rc, nc)) { - setfltvalue(s2v(ra), luai_numpow(L, nb, nc)); + lua_Integer ib; lua_Integer ic; + if (tointegerns(rb, &ib) && tointegerns(rc, &ic)) { + setivalue(s2v(ra), luaV_shiftl(ib, ic)); } else - Protect(luaT_trybinTM(L, rb, rc, ra, TM_POW)); + Protect(luaT_trybinTM(L, rb, rc, ra, TM_SHL)); vmbreak; } vmcase(OP_UNM) { diff --git a/lvm.h b/lvm.h index 8ead0c50b3..7e8ec7155e 100644 --- a/lvm.h +++ b/lvm.h @@ -114,7 +114,7 @@ LUAI_FUNC void luaV_finishset (lua_State *L, const TValue *t, TValue *key, LUAI_FUNC void luaV_finishOp (lua_State *L); LUAI_FUNC void luaV_execute (lua_State *L, CallInfo *ci); LUAI_FUNC void luaV_concat (lua_State *L, int total); -LUAI_FUNC lua_Integer luaV_div (lua_State *L, lua_Integer x, lua_Integer y); +LUAI_FUNC lua_Integer luaV_idiv (lua_State *L, lua_Integer x, lua_Integer y); LUAI_FUNC lua_Integer luaV_mod (lua_State *L, lua_Integer x, lua_Integer y); LUAI_FUNC lua_Number luaV_modf (lua_State *L, lua_Number x, lua_Number y); LUAI_FUNC lua_Integer luaV_shiftl (lua_Integer x, lua_Integer y); From b8fed93215a23a3f443c5b0126f0de1725771b44 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 7 Nov 2018 10:03:05 -0200 Subject: [PATCH 0349/1145] New syntax for to-be-closed variables The new syntax is . The mark '*' allows other attributes to be added later without the need of new keywords; it also allows better error messages. The API function was also renamed ('lua_tobeclosed' -> 'lua_toclose'). --- lapi.c | 2 +- lparser.c | 25 +++++++++++++------------ ltests.c | 4 ++-- lua.h | 2 +- testes/api.lua | 12 ++++++------ testes/files.lua | 6 +++--- testes/goto.lua | 2 +- testes/locals.lua | 42 +++++++++++++++++++++--------------------- 8 files changed, 48 insertions(+), 47 deletions(-) diff --git a/lapi.c b/lapi.c index 4fef43b713..91a6e38916 100644 --- a/lapi.c +++ b/lapi.c @@ -1207,7 +1207,7 @@ LUA_API int lua_next (lua_State *L, int idx) { } -LUA_API void lua_tobeclosed (lua_State *L) { +LUA_API void lua_toclose (lua_State *L) { int nresults = L->ci->nresults; luaF_newtbcupval(L, L->top - 1); /* create new to-be-closed upvalue */ if (!hastocloseCfunc(nresults)) /* function not marked yet? */ diff --git a/lparser.c b/lparser.c index a5b84aa1e8..e4f11cb615 100644 --- a/lparser.c +++ b/lparser.c @@ -1546,16 +1546,15 @@ static void localfunc (LexState *ls) { } -static void commonlocalstat (LexState *ls, TString *firstvar) { +static void commonlocalstat (LexState *ls) { /* stat -> LOCAL NAME {',' NAME} ['=' explist] */ - int nvars = 1; + int nvars = 0; int nexps; expdesc e; - new_localvar(ls, firstvar); - while (testnext(ls, ',')) { + do { new_localvar(ls, str_checkname(ls)); nvars++; - } + } while (testnext(ls, ',')); if (testnext(ls, '=')) nexps = explist(ls, &e); else { @@ -1567,8 +1566,12 @@ static void commonlocalstat (LexState *ls, TString *firstvar) { } -static void scopedlocalstat (LexState *ls) { +static void tocloselocalstat (LexState *ls) { FuncState *fs = ls->fs; + TString *attr = str_checkname(ls); + if (strcmp(getstr(attr), "toclose") != 0) + luaK_semerror(ls, + luaO_pushfstring(ls->L, "unknown attribute '%s'", getstr(attr))); new_localvar(ls, str_checkname(ls)); checknext(ls, '='); exp1(ls, 0); @@ -1580,13 +1583,11 @@ static void scopedlocalstat (LexState *ls) { static void localstat (LexState *ls) { /* stat -> LOCAL NAME {',' NAME} ['=' explist] - | LOCAL SCOPED NAME '=' exp */ - TString *firstvar = str_checkname(ls); - if (ls->t.token == TK_NAME && - eqshrstr(firstvar, luaS_newliteral(ls->L, "scoped"))) - scopedlocalstat(ls); + | LOCAL *toclose NAME '=' exp */ + if (testnext(ls, '*')) + tocloselocalstat(ls); else - commonlocalstat(ls, firstvar); + commonlocalstat(ls); } diff --git a/ltests.c b/ltests.c index aba44b8731..192ae86193 100644 --- a/ltests.c +++ b/ltests.c @@ -1550,8 +1550,8 @@ static struct X { int x; } x; int i = getindex; return lua_yieldk(L1, nres, i, Cfunck); } - else if EQ("tobeclosed") { - lua_tobeclosed(L); + else if EQ("toclose") { + lua_toclose(L); } else luaL_error(L, "unknown instruction %s", buff); } diff --git a/lua.h b/lua.h index 16d685ccb0..fe468c8ef4 100644 --- a/lua.h +++ b/lua.h @@ -333,7 +333,7 @@ LUA_API size_t (lua_stringtonumber) (lua_State *L, const char *s); LUA_API lua_Alloc (lua_getallocf) (lua_State *L, void **ud); LUA_API void (lua_setallocf) (lua_State *L, lua_Alloc f, void *ud); -LUA_API void (lua_tobeclosed) (lua_State *L); +LUA_API void (lua_toclose) (lua_State *L); /* diff --git a/testes/api.lua b/testes/api.lua index 988250f719..a6ddca8e3c 100644 --- a/testes/api.lua +++ b/testes/api.lua @@ -987,7 +987,7 @@ do local a = T.testC([[ call 0 1 # create resource - tobeclosed # mark it to be closed + toclose # mark it to be closed return 1 ]], newresource) assert(a[1] == 11) @@ -996,7 +996,7 @@ do -- repeat the test, but calling function in a 'multret' context local a = {T.testC([[ call 0 1 # create resource - tobeclosed # mark it to be closed + toclose # mark it to be closed return 2 ]], newresource)} assert(type(a[1]) == "string" and a[2][1] == 11) @@ -1005,7 +1005,7 @@ do -- error local a, b = pcall(T.testC, [[ call 0 1 # create resource - tobeclosed # mark it to be closed + toclose # mark it to be closed error # resource is the error object ]], newresource) assert(a == false and b[1] == 11) @@ -1019,10 +1019,10 @@ do local a = T.testC([[ pushvalue 2 call 0 1 # create resource - tobeclosed # mark it to be closed + toclose # mark it to be closed pushvalue 2 call 0 1 # create another resource - tobeclosed # mark it to be closed + toclose # mark it to be closed pushvalue 3 pushint 2 # there should be two open resources call 1 0 @@ -1102,7 +1102,7 @@ end) testamem("to-be-closed variables", function() local flag do - local scoped x = function () flag = true end + local *toclose x = function () flag = true end flag = false local x = {} end diff --git a/testes/files.lua b/testes/files.lua index a11c5e617d..e68eb9b865 100644 --- a/testes/files.lua +++ b/testes/files.lua @@ -125,7 +125,7 @@ do -- closing file by scope local F = nil do - local scoped f = assert(io.open(file, "w")) + local *toclose f = assert(io.open(file, "w")) F = f end assert(tostring(F) == "file (closed)") @@ -135,7 +135,7 @@ assert(os.remove(file)) do -- test writing/reading numbers - local scoped f = assert(io.open(file, "w")) + local *toclose f = assert(io.open(file, "w")) f:write(maxint, '\n') f:write(string.format("0X%x\n", maxint)) f:write("0xABCp-3", '\n') @@ -158,7 +158,7 @@ assert(os.remove(file)) -- testing multiple arguments to io.read do - local scoped f = assert(io.open(file, "w")) + local *toclose f = assert(io.open(file, "w")) f:write[[ a line another line diff --git a/testes/goto.lua b/testes/goto.lua index 92f048fb69..5d863a428d 100644 --- a/testes/goto.lua +++ b/testes/goto.lua @@ -258,7 +258,7 @@ do ::L2:: goto L3 ::L1:: do - local scoped a = function () X = true end + local *toclose a = function () X = true end assert(X == nil) if a then goto L2 end -- jumping back out of scope of 'a' end diff --git a/testes/locals.lua b/testes/locals.lua index 1e0f525ba4..869ac1ff2e 100644 --- a/testes/locals.lua +++ b/testes/locals.lua @@ -181,9 +181,9 @@ local function stack(n) n = ((n == 0) or stack(n - 1)) end do local a = {} do - local scoped x = setmetatable({"x"}, {__close = function (self) + local *toclose x = setmetatable({"x"}, {__close = function (self) a[#a + 1] = self[1] end}) - local scoped y = function (x) assert(x == nil); a[#a + 1] = "y" end + local *toclose y = function (x) assert(x == nil); a[#a + 1] = "y" end a[#a + 1] = "in" end a[#a + 1] = "out" @@ -197,7 +197,7 @@ do -- closing functions do not corrupt returning values local function foo (x) - local scoped _ = closescope + local *toclose _ = closescope return x, X, 23 end @@ -206,7 +206,7 @@ do X = false foo = function (x) - local scoped _ = closescope + local *toclose _ = closescope local y = 15 return y end @@ -215,7 +215,7 @@ do X = false foo = function () - local scoped x = closescope + local *toclose x = closescope return x end @@ -228,13 +228,13 @@ do -- to-be-closed variables must be closed in tail calls local X, Y local function foo () - local scoped _ = function () Y = 10 end + local *toclose _ = function () Y = 10 end assert(X == 20 and Y == nil) return 1,2,3 end local function bar () - local scoped _ = function () X = 20 end + local *toclose _ = function () X = 20 end return foo() end @@ -245,11 +245,11 @@ end do -- errors in __close local log = {} local function foo (err) - local scoped x = function (msg) log[#log + 1] = msg; error(1) end - local scoped x1 = function (msg) log[#log + 1] = msg; end - local scoped gc = function () collectgarbage() end - local scoped y = function (msg) log[#log + 1] = msg; error(2) end - local scoped z = function (msg) log[#log + 1] = msg or 10; error(3) end + local *toclose x = function (msg) log[#log + 1] = msg; error(1) end + local *toclose x1 = function (msg) log[#log + 1] = msg; end + local *toclose gc = function () collectgarbage() end + local *toclose y = function (msg) log[#log + 1] = msg; error(2) end + local *toclose z = function (msg) log[#log + 1] = msg or 10; error(3) end if err then error(4) end end local stat, msg = pcall(foo, false) @@ -267,8 +267,8 @@ end if rawget(_G, "T") then -- memory error inside closing function local function foo () - local scoped y = function () T.alloccount() end - local scoped x = setmetatable({}, {__close = function () + local *toclose y = function () T.alloccount() end + local *toclose x = setmetatable({}, {__close = function () T.alloccount(0); local x = {} -- force a memory error end}) error("a") -- common error inside the function's body @@ -294,7 +294,7 @@ if rawget(_G, "T") then end local function test () - local scoped x = enter(0) -- set a memory limit + local *toclose x = enter(0) -- set a memory limit -- creation of previous upvalue will raise a memory error os.exit(false) -- should not run end @@ -309,14 +309,14 @@ if rawget(_G, "T") then -- repeat test with extra closing upvalues local function test () - local scoped xxx = function (msg) + local *toclose xxx = function (msg) assert(msg == "not enough memory"); error(1000) -- raise another error end - local scoped xx = function (msg) + local *toclose xx = function (msg) assert(msg == "not enough memory"); end - local scoped x = enter(0) -- set a memory limit + local *toclose x = enter(0) -- set a memory limit -- creation of previous upvalue will raise a memory error os.exit(false) -- should not run end @@ -333,9 +333,9 @@ do local x = false local y = false local co = coroutine.create(function () - local scoped xv = function () x = true end + local *toclose xv = function () x = true end do - local scoped yv = function () y = true end + local *toclose yv = function () y = true end coroutine.yield(100) -- yield doesn't close variable end coroutine.yield(200) -- yield doesn't close variable @@ -353,7 +353,7 @@ end -- a suspended coroutine should not close its variables when collected local co co = coroutine.wrap(function() - local scoped x = function () os.exit(false) end -- should not run + local *toclose x = function () os.exit(false) end -- should not run co = nil coroutine.yield() end) From 7f6f70853c8a2730fca2e95d5968ad52cf470bda Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 7 Nov 2018 14:42:05 -0200 Subject: [PATCH 0350/1145] To-be-closed variable in 'for' loop separated from the state The variable to be closed in a generic 'for' loop now is the 4th value produced in the loop initialization, instead of being the loop state (the 2nd value produced). That allows a loop to use a state with a '__toclose' metamethod but do not close it. (As an example, 'f:lines()' might use the file 'f' as a state for the loop, but it should not close the file when the loop ends.) --- liolib.c | 6 ++++-- lopcodes.h | 6 +++--- lparser.c | 25 ++++++++++++++----------- lvm.c | 23 ++++++++++++----------- testes/files.lua | 37 ++++++++++++++++++++++++++++++++++++- testes/locals.lua | 8 +++++++- 6 files changed, 76 insertions(+), 29 deletions(-) diff --git a/liolib.c b/liolib.c index b2a2fec8a1..7d6d51e672 100644 --- a/liolib.c +++ b/liolib.c @@ -386,8 +386,10 @@ static int io_lines (lua_State *L) { } aux_lines(L, toclose); /* push iteration function */ if (toclose) { - lua_pushvalue(L, 1); /* file will be second result */ - return 2; + lua_pushnil(L); /* state */ + lua_pushnil(L); /* control */ + lua_pushvalue(L, 1); /* file is the to-be-closed variable (4th result) */ + return 4; } else return 1; diff --git a/lopcodes.h b/lopcodes.h index 5a38f76746..7bb83a1e6e 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -279,9 +279,9 @@ OP_FORLOOP,/* A Bx R(A)+=R(A+2); if R(A) fs; Dyndata *dyd = ls->dyd; @@ -176,13 +179,8 @@ static void new_localvar (LexState *ls, TString *name) { dyd->actvar.arr[dyd->actvar.n++].idx = cast(short, reg); } - -static void new_localvarliteral_ (LexState *ls, const char *name, size_t sz) { - new_localvar(ls, luaX_newstring(ls, name, sz)); -} - #define new_localvarliteral(ls,v) \ - new_localvarliteral_(ls, "" v, (sizeof(v)/sizeof(char))-1) + new_localvar(ls, luaX_newstring(ls, "" v, (sizeof(v)/sizeof(char)) - 1)); static LocVar *getlocvar (FuncState *fs, int i) { @@ -192,6 +190,9 @@ static LocVar *getlocvar (FuncState *fs, int i) { } +/* +** Start the scope for the last 'nvars' created variables. +*/ static void adjustlocalvars (LexState *ls, int nvars) { FuncState *fs = ls->fs; fs->nactvar = cast_byte(fs->nactvar + nvars); @@ -1357,7 +1358,6 @@ static void forbody (LexState *ls, int base, int line, int nvars, int kind) { BlockCnt bl; FuncState *fs = ls->fs; int prep, endfor; - adjustlocalvars(ls, 3); /* control variables */ checknext(ls, TK_DO); prep = luaK_codeABx(fs, forprep[kind], base, 0); enterblock(fs, &bl, 0); /* scope for declared variables */ @@ -1399,6 +1399,7 @@ static void fornum (LexState *ls, TString *varname, int line) { luaK_int(fs, fs->freereg, 1); luaK_reserveregs(fs, 1); } + adjustlocalvars(ls, 3); /* control variables */ forbody(ls, base, line, 1, basicfor); } @@ -1407,7 +1408,7 @@ static void forlist (LexState *ls, TString *indexname) { /* forlist -> NAME {,NAME} IN explist forbody */ FuncState *fs = ls->fs; expdesc e; - int nvars = 4; /* gen, state, control, plus at least one declared var */ + int nvars = 5; /* gen, state, control, toclose, 'indexname' */ int line; int base = fs->freereg; /* create control variables */ @@ -1415,6 +1416,7 @@ static void forlist (LexState *ls, TString *indexname) { new_localvarliteral(ls, "(for state)"); markupval(fs, fs->nactvar); /* state may create an upvalue */ new_localvarliteral(ls, "(for control)"); + new_localvarliteral(ls, "(for toclose)"); /* create declared variables */ new_localvar(ls, indexname); while (testnext(ls, ',')) { @@ -1423,9 +1425,10 @@ static void forlist (LexState *ls, TString *indexname) { } checknext(ls, TK_IN); line = ls->linenumber; - adjust_assign(ls, 3, explist(ls, &e), &e); + adjust_assign(ls, 4, explist(ls, &e), &e); + adjustlocalvars(ls, 4); /* control variables */ luaK_checkstack(fs, 3); /* extra space to call generator */ - forbody(ls, base, line, nvars - 3, 2); + forbody(ls, base, line, nvars - 4, 2); } @@ -1575,9 +1578,9 @@ static void tocloselocalstat (LexState *ls) { new_localvar(ls, str_checkname(ls)); checknext(ls, '='); exp1(ls, 0); - luaK_codeABC(fs, OP_TBC, fs->nactvar, 0, 0); markupval(fs, fs->nactvar); adjustlocalvars(ls, 1); + luaK_codeABC(fs, OP_TBC, fs->nactvar - 1, 0, 0); } diff --git a/lvm.c b/lvm.c index 9977bda4f7..7d5ab9db85 100644 --- a/lvm.c +++ b/lvm.c @@ -1654,11 +1654,11 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_TFORPREP) { - /* is 'state' a function or has a '__close' metamethod? */ - if (ttisfunction(s2v(ra + 1)) || - !ttisnil(luaT_gettmbyobj(L, s2v(ra + 1), TM_CLOSE))) { + /* is 'toclose' a function or has a '__close' metamethod? */ + if (ttisfunction(s2v(ra + 3)) || + !ttisnil(luaT_gettmbyobj(L, s2v(ra + 3), TM_CLOSE))) { /* create to-be-closed upvalue for it */ - halfProtect(luaF_newtbcupval(L, ra + 1)); + halfProtect(luaF_newtbcupval(L, ra + 3)); } pc += GETARG_Bx(i); i = *(pc++); /* go to next instruction */ @@ -1668,13 +1668,14 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmcase(OP_TFORCALL) { l_tforcall: /* 'ra' has the iterator function, 'ra + 1' has the state, - and 'ra + 2' has the control variable. The call will use - the stack after these values (starting at 'ra + 3') + 'ra + 2' has the control variable, and 'ra + 3' has the + to-be-closed variable. The call will use the stack after + these values (starting at 'ra + 4') */ /* push function, state, and control variable */ - memcpy(ra + 3, ra, 3 * sizeof(*ra)); - L->top = ra + 6; - Protect(luaD_call(L, ra + 3, GETARG_C(i))); /* do the call */ + memcpy(ra + 4, ra, 3 * sizeof(*ra)); + L->top = ra + 4 + 3; + Protect(luaD_call(L, ra + 4, GETARG_C(i))); /* do the call */ if (trap) { /* stack may have changed? */ updatebase(ci); /* keep 'base' correct */ ra = RA(i); /* keep 'ra' correct for next instruction */ @@ -1686,8 +1687,8 @@ void luaV_execute (lua_State *L, CallInfo *ci) { } vmcase(OP_TFORLOOP) { l_tforloop: - if (!ttisnil(s2v(ra + 1))) { /* continue loop? */ - setobjs2s(L, ra, ra + 1); /* save control variable */ + if (!ttisnil(s2v(ra + 2))) { /* continue loop? */ + setobjs2s(L, ra, ra + 2); /* save control variable */ pc -= GETARG_Bx(i); /* jump back */ } vmbreak; diff --git a/testes/files.lua b/testes/files.lua index e68eb9b865..34fcf85134 100644 --- a/testes/files.lua +++ b/testes/files.lua @@ -200,7 +200,7 @@ return x + y * z assert(f:close()) f = coroutine.wrap(dofile) assert(f(file) == 10) -print(f(100, 101) == 20) +assert(f(100, 101) == 20) assert(f(200) == 100 + 200 * 101) assert(os.remove(file)) @@ -422,6 +422,41 @@ assert(load(io.lines(file, "L"), nil, nil, t))() assert(t.a == -((10 + 34) * 2)) +do -- testing closing file in line iteration + + -- get the to-be-closed variable from a loop + local function gettoclose (lv) + lv = lv + 1 + for i = 1, math.maxinteger do + local n, v = debug.getlocal(lv, i) + if n == "(for toclose)" then + return v + end + end + end + + local f + for l in io.lines(file) do + f = gettoclose(1) + assert(io.type(f) == "file") + break + end + assert(io.type(f) == "closed file") + + f = nil + local function foo (name) + for l in io.lines(name) do + f = gettoclose(1) + assert(io.type(f) == "file") + error(f) -- exit loop with an error + end + end + local st, msg = pcall(foo, file) + assert(st == false and io.type(msg) == "closed file") + +end + + -- test for multipe arguments in 'lines' io.output(file); io.write"0123456789\n":close() for a,b in io.lines(file, 1, 1) do diff --git a/testes/locals.lua b/testes/locals.lua index 869ac1ff2e..28f88e5480 100644 --- a/testes/locals.lua +++ b/testes/locals.lua @@ -371,6 +371,8 @@ do x = x - 1 if x > 0 then return x end end, + nil, -- state + nil, -- control variable function () -- closing function numopen = numopen - 1 end @@ -392,12 +394,16 @@ do -- repeat test with '__open' metamethod instead of a function local function open (x) numopen = numopen + 1 + local state = setmetatable({x}, + {__close = function () numopen = numopen - 1 end}) return function (t) -- iteraction function t[1] = t[1] - 1 if t[1] > 0 then return t[1] end end, - setmetatable({x}, {__close = function () numopen = numopen - 1 end}) + state, + nil, + state -- to-be-closed end local s = 0 From 9eafe9c053ef17a0980ab32082bf229bd58e963b Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 9 Nov 2018 17:03:52 -0200 Subject: [PATCH 0351/1145] New implementation for 'luaL_addvalue' The function 'luaL_addvalue' (from the buffer system) was rewritten so that it does not change the position of the box (if present) in the stack. --- lauxlib.c | 81 ++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 59 insertions(+), 22 deletions(-) diff --git a/lauxlib.c b/lauxlib.c index 78dfb4e952..68842a9855 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -27,6 +27,12 @@ #include "lauxlib.h" +#if !defined(MAX_SIZET) +/* maximum value for size_t */ +#define MAX_SIZET ((size_t)(~(size_t)0)) +#endif + + /* ** {====================================================== ** Traceback @@ -501,34 +507,56 @@ static void *newbox (lua_State *L, size_t newsize) { /* -** returns a pointer to a free area with at least 'sz' bytes +** Compute new size for buffer 'B', enough to accommodate extra 'sz' +** bytes. */ -LUALIB_API char *luaL_prepbuffsize (luaL_Buffer *B, size_t sz) { - lua_State *L = B->L; - if (B->size - B->n < sz) { /* not enough space? */ +static size_t newbuffsize (luaL_Buffer *B, size_t sz) { + size_t newsize = B->size * 2; /* double buffer size */ + if (MAX_SIZET - sz < B->n) /* overflow in (B->n + sz)? */ + return luaL_error(B->L, "buffer too large"); + if (newsize < B->n + sz) /* double is not big enough? */ + newsize = B->n + sz; + return newsize; +} + + +/* +** Returns a pointer to a free area with at least 'sz' bytes in buffer +** 'B'. 'boxidx' is the position in the stack where the buffer's box is +** or should be. +*/ +static char *prepbuffsize (luaL_Buffer *B, size_t sz, int boxidx) { + if (B->size - B->n >= sz) /* enough space? */ + return B->b + B->n; + else { + lua_State *L = B->L; char *newbuff; - size_t newsize = B->size * 2; /* double buffer size */ - if (newsize - B->n < sz) /* not big enough? */ - newsize = B->n + sz; - if (newsize < B->n || newsize - B->n < sz) - luaL_error(L, "buffer too large"); + size_t newsize = newbuffsize(B, sz); /* create larger buffer */ - if (buffonstack(B)) - newbuff = (char *)resizebox(L, -1, newsize); - else { /* no buffer yet */ - newbuff = (char *)newbox(L, newsize); + if (buffonstack(B)) /* buffer already has a box? */ + newbuff = (char *)resizebox(L, boxidx, newsize); /* resize it */ + else { /* no box yet */ + newbuff = (char *)newbox(L, newsize); /* create a new box */ memcpy(newbuff, B->b, B->n * sizeof(char)); /* copy original content */ + lua_insert(L, boxidx); /* move box to its intended position */ } B->b = newbuff; B->size = newsize; + return newbuff + B->n; } - return &B->b[B->n]; +} + +/* +** returns a pointer to a free area with at least 'sz' bytes +*/ +LUALIB_API char *luaL_prepbuffsize (luaL_Buffer *B, size_t sz) { + return prepbuffsize(B, sz, -1); } LUALIB_API void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l) { if (l > 0) { /* avoid 'memcpy' when 's' can be NULL */ - char *b = luaL_prepbuffsize(B, l); + char *b = prepbuffsize(B, l, -1); memcpy(b, s, l * sizeof(char)); luaL_addsize(B, l); } @@ -556,14 +584,23 @@ LUALIB_API void luaL_pushresultsize (luaL_Buffer *B, size_t sz) { } +/* +** 'luaL_addvalue' is the only function in the Buffer system where the +** box (if existent) is not on the top of the stack. So, instead of +** calling 'luaL_addlstring', it replicates the code using -2 as the +** last argument to 'prepbuffsize', signaling that the box is (or will +** be) bellow the string being added to the buffer. (Box creation can +** trigger an emergency GC, so we should not remove the string from the +** stack before we have the space guaranteed.) +*/ LUALIB_API void luaL_addvalue (luaL_Buffer *B) { lua_State *L = B->L; - size_t l; - const char *s = lua_tolstring(L, -1, &l); - if (buffonstack(B)) - lua_insert(L, -2); /* put value below buffer */ - luaL_addlstring(B, s, l); - lua_remove(L, (buffonstack(B)) ? -2 : -1); /* remove value */ + size_t len; + const char *s = lua_tolstring(L, -1, &len); + char *b = prepbuffsize(B, len, -2); + memcpy(b, s, len * sizeof(char)); + luaL_addsize(B, len); + lua_pop(L, 1); /* pop string */ } @@ -577,7 +614,7 @@ LUALIB_API void luaL_buffinit (lua_State *L, luaL_Buffer *B) { LUALIB_API char *luaL_buffinitsize (lua_State *L, luaL_Buffer *B, size_t sz) { luaL_buffinit(L, B); - return luaL_prepbuffsize(B, sz); + return prepbuffsize(B, sz, -1); } /* }====================================================== */ From 5fda30b4f9f82b901113a6e666c797f835c708eb Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 12 Nov 2018 14:15:50 -0200 Subject: [PATCH 0352/1145] 'lua_toclose' gets the index to be closed as an argument Sometimes it is useful to mark to-be-closed an index that is not at the top of the stack (e.g., if the value to be closed came from a function call returning multiple values). --- lapi.c | 13 ++++++++++--- ltests.c | 2 +- lua.h | 2 +- testes/api.lua | 18 ++++++++++-------- 4 files changed, 22 insertions(+), 13 deletions(-) diff --git a/lapi.c b/lapi.c index 91a6e38916..da866a660b 100644 --- a/lapi.c +++ b/lapi.c @@ -1207,12 +1207,19 @@ LUA_API int lua_next (lua_State *L, int idx) { } -LUA_API void lua_toclose (lua_State *L) { - int nresults = L->ci->nresults; - luaF_newtbcupval(L, L->top - 1); /* create new to-be-closed upvalue */ +LUA_API void lua_toclose (lua_State *L, int idx) { + int nresults; + StkId o; + lua_lock(L); + o = index2stack(L, idx); + nresults = L->ci->nresults; + api_check(L, L->openupval == NULL || uplevel(L->openupval) < o, + "there is an already marked index below"); + luaF_newtbcupval(L, o); /* create new to-be-closed upvalue */ if (!hastocloseCfunc(nresults)) /* function not marked yet? */ L->ci->nresults = codeNresults(nresults); /* mark it */ lua_assert(hastocloseCfunc(L->ci->nresults)); + lua_unlock(L); } diff --git a/ltests.c b/ltests.c index 192ae86193..c5c1040ad6 100644 --- a/ltests.c +++ b/ltests.c @@ -1551,7 +1551,7 @@ static struct X { int x; } x; return lua_yieldk(L1, nres, i, Cfunck); } else if EQ("toclose") { - lua_toclose(L); + lua_toclose(L, getnum); } else luaL_error(L, "unknown instruction %s", buff); } diff --git a/lua.h b/lua.h index fe468c8ef4..6aa184d18b 100644 --- a/lua.h +++ b/lua.h @@ -333,7 +333,7 @@ LUA_API size_t (lua_stringtonumber) (lua_State *L, const char *s); LUA_API lua_Alloc (lua_getallocf) (lua_State *L, void **ud); LUA_API void (lua_setallocf) (lua_State *L, lua_Alloc f, void *ud); -LUA_API void (lua_toclose) (lua_State *L); +LUA_API void (lua_toclose) (lua_State *L, int idx); /* diff --git a/testes/api.lua b/testes/api.lua index a6ddca8e3c..ed857fd055 100644 --- a/testes/api.lua +++ b/testes/api.lua @@ -985,18 +985,20 @@ do return x end - local a = T.testC([[ + local a, b = T.testC([[ call 0 1 # create resource - toclose # mark it to be closed - return 1 + pushint 34 + toclose -2 # mark call result to be closed + toclose -1 # mark number to be closed (will be ignored) + return 2 ]], newresource) - assert(a[1] == 11) + assert(a[1] == 11 and b == 34) assert(#openresource == 0) -- was closed -- repeat the test, but calling function in a 'multret' context local a = {T.testC([[ call 0 1 # create resource - toclose # mark it to be closed + toclose 2 # mark it to be closed return 2 ]], newresource)} assert(type(a[1]) == "string" and a[2][1] == 11) @@ -1005,7 +1007,7 @@ do -- error local a, b = pcall(T.testC, [[ call 0 1 # create resource - toclose # mark it to be closed + toclose -1 # mark it to be closed error # resource is the error object ]], newresource) assert(a == false and b[1] == 11) @@ -1019,10 +1021,10 @@ do local a = T.testC([[ pushvalue 2 call 0 1 # create resource - toclose # mark it to be closed + toclose -1 # mark it to be closed pushvalue 2 call 0 1 # create another resource - toclose # mark it to be closed + toclose -1 # mark it to be closed pushvalue 3 pushint 2 # there should be two open resources call 1 0 From 8cb84210ab95e882c01363a6794508ec923ade90 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 13 Nov 2018 13:50:33 -0200 Subject: [PATCH 0353/1145] String buffer using to-be-closed variable The string buffers in the C API now mark their boxes as to-be-closed variables, to release their buffers in case of errors. --- lauxlib.c | 26 +++++++++++++++----------- testes/locals.lua | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 11 deletions(-) diff --git a/lauxlib.c b/lauxlib.c index 68842a9855..6069ed1b24 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -470,10 +470,8 @@ static void *resizebox (lua_State *L, int idx, size_t newsize) { lua_Alloc allocf = lua_getallocf(L, &ud); UBox *box = (UBox *)lua_touserdata(L, idx); void *temp = allocf(ud, box->box, box->bsize, newsize); - if (temp == NULL && newsize > 0) { /* allocation error? */ - resizebox(L, idx, 0); /* free buffer */ + if (temp == NULL && newsize > 0) /* allocation error? */ luaL_error(L, "not enough memory for buffer allocation"); - } box->box = temp; box->bsize = newsize; return temp; @@ -486,16 +484,20 @@ static int boxgc (lua_State *L) { } -static void *newbox (lua_State *L, size_t newsize) { +static const luaL_Reg boxmt[] = { /* box metamethods */ + {"__gc", boxgc}, + {"__close", boxgc}, + {NULL, NULL} +}; + + +static void newbox (lua_State *L) { UBox *box = (UBox *)lua_newuserdatauv(L, sizeof(UBox), 0); box->box = NULL; box->bsize = 0; - if (luaL_newmetatable(L, "_UBOX*")) { /* creating metatable? */ - lua_pushcfunction(L, boxgc); - lua_setfield(L, -2, "__gc"); /* metatable.__gc = boxgc */ - } + if (luaL_newmetatable(L, "_UBOX*")) /* creating metatable? */ + luaL_setfuncs(L, boxmt, 0); /* set its metamethods */ lua_setmetatable(L, -2); - return resizebox(L, -1, newsize); } @@ -536,9 +538,11 @@ static char *prepbuffsize (luaL_Buffer *B, size_t sz, int boxidx) { if (buffonstack(B)) /* buffer already has a box? */ newbuff = (char *)resizebox(L, boxidx, newsize); /* resize it */ else { /* no box yet */ - newbuff = (char *)newbox(L, newsize); /* create a new box */ - memcpy(newbuff, B->b, B->n * sizeof(char)); /* copy original content */ + newbox(L); /* create a new box */ lua_insert(L, boxidx); /* move box to its intended position */ + lua_toclose(L, boxidx); + newbuff = (char *)resizebox(L, boxidx, newsize); + memcpy(newbuff, B->b, B->n * sizeof(char)); /* copy original content */ } B->b = newbuff; B->size = newsize; diff --git a/testes/locals.lua b/testes/locals.lua index 28f88e5480..8766b3d5ce 100644 --- a/testes/locals.lua +++ b/testes/locals.lua @@ -324,6 +324,38 @@ if rawget(_G, "T") then local _, msg = pcall(test) assert(msg == 1000) + + do -- testing 'toclose' in C string buffer + local s = string.rep("a", 10000) + local a = {s, s} + + -- ensure proper initialization (stack space, metatable) + table.concat(a) + collectgarbage(); collectgarbage() + + local m = T.totalmem() + + -- error in the second buffer allocation + T.alloccount(3) + assert(not pcall(table.concat, a)) + T.alloccount() + -- first buffer was released by 'toclose' + assert(T.totalmem() - m <= 5000) + + -- error in creation of final string + T.alloccount(4) + assert(not pcall(table.concat, a)) + T.alloccount() + -- second buffer was released by 'toclose' + assert(T.totalmem() - m <= 5000) + + -- userdata, upvalue, buffer, buffer, string + T.alloccount(5) + assert(#table.concat(a) == 20000) + T.alloccount() + + print'+' + end end From d40cd315f50538e9dbd4362a4763c01527f4eb3a Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 13 Nov 2018 13:58:46 -0200 Subject: [PATCH 0354/1145] Visibility of non-API functions changed to "internal" The visibility for functions marked as LUAI_FUNC was changed from "hidden" to "internal". These functions cannot be called from outside the Lua kernel, and "internal" visibility offers more chances for optimizations. --- luaconf.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/luaconf.h b/luaconf.h index 126257dce3..ff7085131a 100644 --- a/luaconf.h +++ b/luaconf.h @@ -277,7 +277,7 @@ */ #if defined(__GNUC__) && ((__GNUC__*100 + __GNUC_MINOR__) >= 302) && \ defined(__ELF__) /* { */ -#define LUAI_FUNC __attribute__((visibility("hidden"))) extern +#define LUAI_FUNC __attribute__((visibility("internal"))) extern #else /* }{ */ #define LUAI_FUNC extern #endif /* } */ From bb0185b196773b878f0bf6b6c6a4a01dbf8c2b83 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 13 Nov 2018 14:33:14 -0200 Subject: [PATCH 0355/1145] Documentation for to-be-closed variables --- manual/manual.of | 148 ++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 122 insertions(+), 26 deletions(-) diff --git a/manual/manual.of b/manual/manual.of index 91ba8c46cf..f891c33e94 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -1448,7 +1448,9 @@ for @rep{var_1}, @Cdots, @rep{var_n} in @rep{explist} do @rep{block} end is equivalent to the code: @verbatim{ do - local @rep{f}, @rep{s}, @rep{var} = @rep{explist} + local @rep{f}, @rep{s}, @rep{var} + local *toclose @rep{tbc} = nil + @rep{f}, @rep{s}, @rep{var}, @rep{tbc} = @rep{explist} while true do local @rep{var_1}, @Cdots, @rep{var_n} = @rep{f}(@rep{s}, @rep{var}) if @rep{var_1} == nil then break end @@ -1464,11 +1466,14 @@ Note the following: @T{@rep{explist}} is evaluated only once. Its results are an @emph{iterator} function, a @emph{state}, -and an initial value for the first @emph{iterator variable}. +an initial value for the first @emph{iterator variable}, +and a to-be-closed variable @see{to-be-closed}, +which can be used to release resources when the loop ends. } @item{ -@T{@rep{f}}, @T{@rep{s}}, and @T{@rep{var}} are invisible variables. +@T{@rep{f}}, @T{@rep{s}}, @T{@rep{var}}, and @T{@rep{tbc}} +are invisible variables. The names are here for explanatory purposes only. } @@ -1515,6 +1520,52 @@ The visibility rules for local variables are explained in @See{visibility}. } +@sect3{to-be-closed| @title{To-be-closed Variables} + +A local variable can be declared as a @def{to-be-closed} variable, +with the following syntax: +@Produc{ +@producname{stat}@producbody{ + @Rw{local} @bnfter{*} @bnfter{toclose} Name @bnfter{=} exp +}} +A to-be-closed variable behaves like a normal local variable, +except that its value is @emph{closed} whenever the variable +goes out of scope, including normal block termination, +exiting its block by @Rw{break}/@Rw{goto}/@Rw{return}, +or exiting by an error. +If a block ends in a tail call @see{functioncall}, +all variables of the caller function go out of scope +before the start of the callee function. + +To \emph{close} a value has the following meaning here: +If the value of the variable when it goes out of scope is a function, +that function is called; +otherwise, if the value has a @idx{__close} metamethod, +that metamethod is called; +otherwise, nothing is done. +In the function case, +if the scope is being closed by an error, +the error object is passed as an argument to the function; +if there is no error, the function gets @nil. +In the metamethod case, +the value itself always is passed as an argument to the metamethod. + +If several to-be-closed variables go out of scope at the same event, +they are closed in the reverse order that they were declared. +If there is any error while running a closing function, +that error is handled like an error in the regular code +where the variable was defined; +in particular, +the other pending closing functions will still be called. + +If a coroutine yields inside a block and is never resumed again, +the variables visible at that block will never go out of scope, +and therefore they will not be closed. +Similarly, if a script is interrupted by an unprotected error, +its to-be-closed variables will not be closed. + +} + } @sect2{expressions| @title{Expressions} @@ -2442,7 +2493,7 @@ the result of @Lid{lua_newthread}), it should use them only in API calls that cannot raise errors. The panic function runs as if it were a @x{message handler} @see{error}; -in particular, the error object is at the top of the stack. +in particular, the error object is on the top of the stack. However, there is no guarantee about stack space. To push anything on the stack, the panic function must first check the available space @see{stacksize}. @@ -2593,9 +2644,9 @@ tells whether the function may raise errors: @Char{m} means the function may raise out-of-memory errors and errors running a finalizer; @Char{v} means the function may raise the errors explained in the text; -@Char{e} means the function may raise any errors -(because it can run arbitrary Lua code, -either directly or through metamethods). +@Char{e} means the function can run arbitrary Lua code, +either directly or through metamethods, +and therefore may raise any errors. @APIEntry{int lua_absindex (lua_State *L, int idx);| @@ -2675,7 +2726,7 @@ that @T{free(NULL)} has no effect and that Performs an arithmetic or bitwise operation over the two values (or one, in the case of negations) at the top of the stack, -with the value at the top being the second operand, +with the value on the top being the second operand, pops these values, and pushes the result of the operation. The function follows the semantics of the corresponding Lua operator (that is, it may call metamethods). @@ -2875,7 +2926,7 @@ The value of @id{op} must be one of the following constants: @apii{n,1,e} Concatenates the @id{n} values at the top of the stack, -pops them, and leaves the result at the top. +pops them, and leaves the result on the top. If @N{@T{n} is 1}, the result is the single value on the stack (that is, the function does nothing); if @id{n} is 0, the result is the empty string. @@ -2942,7 +2993,7 @@ This function does not pop the Lua function from the stack. @apii{1,0,v} Generates a Lua error, -using the value at the top of the stack as the error object. +using the value on the top of the stack as the error object. This function does a long jump, and therefore never returns @seeC{luaL_error}. @@ -3081,7 +3132,7 @@ the function @N{returns 0} and pushes nothing on the stack. Pushes onto the stack the value @T{t[k]}, where @id{t} is the value at the given index -and @id{k} is the value at the top of the stack. +and @id{k} is the value on the top of the stack. This function pops the key from the stack, pushing the resulting value in its place. @@ -3798,7 +3849,7 @@ Similar to @Lid{lua_settable}, but does a raw assignment Does the equivalent of @T{t[i] = v}, where @id{t} is the table at the given index -and @id{v} is the value at the top of the stack. +and @id{v} is the value on the top of the stack. This function pops the value from the stack. The assignment is raw, @@ -3812,7 +3863,7 @@ that is, it does not invoke the @idx{__newindex} metamethod. Does the equivalent of @T{t[p] = v}, where @id{t} is the table at the given index, @id{p} is encoded as a light userdata, -and @id{v} is the value at the top of the stack. +and @id{v} is the value on the top of the stack. This function pops the value from the stack. The assignment is raw, @@ -3939,7 +3990,7 @@ with user data @id{ud}. Does the equivalent to @T{t[k] = v}, where @id{t} is the value at the given index -and @id{v} is the value at the top of the stack. +and @id{v} is the value on the top of the stack. This function pops the value from the stack. As in Lua, this function may trigger a metamethod @@ -3960,7 +4011,7 @@ sets it as the new value of global @id{name}. Does the equivalent to @T{t[n] = v}, where @id{t} is the value at the given index -and @id{v} is the value at the top of the stack. +and @id{v} is the value on the top of the stack. This function pops the value from the stack. As in Lua, this function may trigger a metamethod @@ -3981,7 +4032,7 @@ sets it as the new metatable for the value at the given index. Does the equivalent to @T{t[k] = v}, where @id{t} is the value at the given index, -@id{v} is the value at the top of the stack, +@id{v} is the value on the top of the stack, and @id{k} is the value just below the top. This function pops both the key and the value from the stack. @@ -4082,6 +4133,31 @@ otherwise, returns @id{NULL}. } +@APIEntry{void lua_toclose (lua_State *L, int index);| +@apii{0,0,v} + +Marks the given index in the stack as a +to-be-closed @Q{variable} @see{to-be-closed}. +Like a to-be-closed variable in Lua, +the value at that index in the stack will be closed +when it goes out of scope. +Here, in the context of a C function, +to go out of scope means that the running function returns (to Lua), +there is an error, +or the index is removed from the stack through +@Lid{lua_settop} or @Lid{lua_pop}. +An index marked as to-be-closed should not be removed from the stack +by any other function in the API except @Lid{lua_settop} or @Lid{lua_pop}. + +This function should not be called for an index +that is equal to or below an already marked to-be-closed index. + +This function can raise an out-of-memory error. +In that case, the value in the given index is immediately closed, +as if it was already marked. + +} + @APIEntry{lua_Integer lua_tointeger (lua_State *L, int index);| @apii{0,0,-} @@ -4587,7 +4663,7 @@ and names. and returns its name. In the second case, @id{ar} must be @id{NULL} and the function -to be inspected must be at the top of the stack. +to be inspected must be on the top of the stack. In this case, only parameters of Lua functions are visible (as there is no information about what variables are active) and no values are pushed onto the stack. @@ -4720,7 +4796,7 @@ A hook is disabled by setting @id{mask} to zero. @apii{0|1,0,-} Sets the value of a local variable of a given activation record. -It assigns the value at the top of the stack +It assigns the value on the top of the stack to the variable and returns its name. It also pops the value from the stack. @@ -4736,7 +4812,7 @@ Parameters @id{ar} and @id{n} are as in function @Lid{lua_getlocal}. @apii{0|1,0,-} Sets the value of a closure's upvalue. -It assigns the value at the top of the stack +It assigns the value on the top of the stack to the upvalue and returns its name. It also pops the value from the stack. @@ -4861,7 +4937,7 @@ to the buffer @id{B} @APIEntry{void luaL_addvalue (luaL_Buffer *B);| @apii{1,?,m} -Adds the value at the top of the stack +Adds the value on the top of the stack to the buffer @id{B} @seeC{luaL_Buffer}. Pops the value. @@ -5486,7 +5562,7 @@ Equivalent to the sequence @Lid{luaL_addsize}, @Lid{luaL_pushresult}. Creates and returns a @def{reference}, in the table at index @id{t}, -for the object at the top of the stack (and pops the object). +for the object on the top of the stack (and pops the object). A reference is a unique integer key. As long as you do not manually add integer keys into table @id{t}, @@ -5495,7 +5571,7 @@ You can retrieve an object referred by reference @id{r} by calling @T{lua_rawgeti(L, t, r)}. Function @Lid{luaL_unref} frees a reference and its associated object. -If the object at the top of the stack is @nil, +If the object on the top of the stack is @nil, @Lid{luaL_ref} returns the constant @defid{LUA_REFNIL}. The constant @defid{LUA_NOREF} is guaranteed to be different from any reference returned by @Lid{luaL_ref}. @@ -5554,7 +5630,7 @@ These values are popped from the stack after the registration. @APIEntry{void luaL_setmetatable (lua_State *L, const char *tname);| @apii{0,0,-} -Sets the metatable of the object at the top of the stack +Sets the metatable of the object on the top of the stack as the metatable associated with name @id{tname} in the registry @seeC{luaL_newmetatable}. @@ -7571,6 +7647,9 @@ The table @id{io} also provides three predefined file handles with their usual meanings from C: @defid{io.stdin}, @defid{io.stdout}, and @defid{io.stderr}. The I/O library never closes these files. +The metatable for file handles provides metamethods +for @idx{__gc} and @idx{__close} that try +to close the file when called. Unless otherwise stated, all I/O functions return @nil on failure @@ -7617,6 +7696,13 @@ and returns an iterator function that works like @T{file:lines(@Cdots)} over the opened file. When the iterator function detects the end of file, it returns no values (to finish the loop) and automatically closes the file. +Besides the iterator function, +@id{io.lines} returns three other values: +two @nil values as placeholders, +plus the created file handle. +Therefore, when used in a generic @Rw{for} loop, +the file is closed also if the loop is interrupted by an +error or a @Rw{break}. The call @T{io.lines()} (with no file name) is equivalent to @T{io.input():lines("l")}; @@ -8543,6 +8629,13 @@ now starts with a somewhat random seed. Moreover, it uses a different algorithm. } +@item{ +The function @Lid{io.lines} now returns three extra values, +besides the iterator function. +You can enclose the call in parentheses if you need to +discard these extra results. +} + } } @@ -8559,15 +8652,17 @@ replaced by @Lid{lua_newuserdatauv}, @Lid{lua_setiuservalue}, and @Lid{lua_getiuservalue}, which have an extra argument. -(For compatibility, the old names still work as macros assuming -one single user value.) +For compatibility, the old names still work as macros assuming +one single user value. +Note, however, that the call @T{lua_newuserdatauv(L,size,0)} +produces a smaller userdata. } @item{ The function @Lid{lua_resume} has an extra parameter. This out parameter returns the number of values on the top of the stack that were yielded or returned by the coroutine. -(In older versions, +(In previous versions, those values were the entire stack.) } @@ -8626,6 +8721,7 @@ and @bnfNter{LiteralString}, see @See{lexical}.) @OrNL @Rw{function} funcname funcbody @OrNL @Rw{local} @Rw{function} @bnfNter{Name} funcbody @OrNL @Rw{local} namelist @bnfopt{@bnfter{=} explist} +@OrNL @Rw{local} @bnfter{*} @bnfter{toclose} Name @bnfter{=} exp } @producname{retstat}@producbody{@Rw{return} From 35296e1fde705b3ac8356a4f4ce426497cf7b64c Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 22 Nov 2018 13:56:04 -0200 Subject: [PATCH 0356/1145] Details comments and other janitorial work. --- lparser.c | 118 +++++++++++++++++++++++++++++++++++++++--------------- lparser.h | 2 +- lvm.c | 12 +++--- 3 files changed, 93 insertions(+), 39 deletions(-) diff --git a/lparser.c b/lparser.c index afc5aeab61..e525e57816 100644 --- a/lparser.c +++ b/lparser.c @@ -10,6 +10,7 @@ #include "lprefix.h" +#include #include #include "lua.h" @@ -87,6 +88,9 @@ static void checklimit (FuncState *fs, int v, int l, const char *what) { } +/* +** Test whether next token is 'c'; if so, skip it. +*/ static int testnext (LexState *ls, int c) { if (ls->t.token == c) { luaX_next(ls); @@ -96,12 +100,18 @@ static int testnext (LexState *ls, int c) { } +/* +** Check that next token is 'c'. +*/ static void check (LexState *ls, int c) { if (ls->t.token != c) error_expected(ls, c); } +/* +** Check that next token is 'c' and skip it. +*/ static void checknext (LexState *ls, int c) { check(ls, c); luaX_next(ls); @@ -111,11 +121,15 @@ static void checknext (LexState *ls, int c) { #define check_condition(ls,c,msg) { if (!(c)) luaX_syntaxerror(ls, msg); } - +/* +** Check that next token is 'what' and skip it. In case of error, +** raise an error that the expected 'what' should match a 'who' +** in line 'where' (if that is not the current line). +*/ static void check_match (LexState *ls, int what, int who, int where) { if (unlikely(!testnext(ls, what))) { - if (where == ls->linenumber) - error_expected(ls, what); + if (where == ls->linenumber) /* all in the same line? */ + error_expected(ls, what); /* do not need a complex message */ else { luaX_syntaxerror(ls, luaO_pushfstring(ls->L, "%s expected (to close %s at line %d)", @@ -146,11 +160,15 @@ static void codestring (LexState *ls, expdesc *e, TString *s) { } -static void checkname (LexState *ls, expdesc *e) { +static void codename (LexState *ls, expdesc *e) { codestring(ls, e, str_checkname(ls)); } +/* +** Register a new local variable in the active 'Proto' (for debug +** information). +*/ static int registerlocalvar (LexState *ls, TString *varname) { FuncState *fs = ls->fs; Proto *f = fs->f; @@ -183,6 +201,9 @@ static void new_localvar (LexState *ls, TString *name) { new_localvar(ls, luaX_newstring(ls, "" v, (sizeof(v)/sizeof(char)) - 1)); +/* +** Get the debug-information entry for current variable 'i'. +*/ static LocVar *getlocvar (FuncState *fs, int i) { int idx = fs->ls->dyd->actvar.arr[fs->firstlocal + i].idx; lua_assert(idx < fs->nlocvars); @@ -192,6 +213,7 @@ static LocVar *getlocvar (FuncState *fs, int i) { /* ** Start the scope for the last 'nvars' created variables. +** (debug info.) */ static void adjustlocalvars (LexState *ls, int nvars) { FuncState *fs = ls->fs; @@ -202,6 +224,10 @@ static void adjustlocalvars (LexState *ls, int nvars) { } +/* +** Close the scope for all variables up to level 'tolevel'. +** (debug info.) +*/ static void removevars (FuncState *fs, int tolevel) { fs->ls->dyd->actvar.n -= (fs->nactvar - tolevel); while (fs->nactvar > tolevel) @@ -209,6 +235,10 @@ static void removevars (FuncState *fs, int tolevel) { } +/* +** Search the upvalues of the function 'fs' for one +** with the given 'name'. +*/ static int searchupvalue (FuncState *fs, TString *name) { int i; Upvaldesc *up = fs->f->upvalues; @@ -235,6 +265,10 @@ static int newupvalue (FuncState *fs, TString *name, expdesc *v) { } +/* +** Look for an active local variable with the name 'n' in the +** function 'fs'. +*/ static int searchvar (FuncState *fs, TString *n) { int i; for (i = cast_int(fs->nactvar) - 1; i >= 0; i--) { @@ -246,8 +280,8 @@ static int searchvar (FuncState *fs, TString *n) { /* - Mark block where variable at given level was defined - (to emit close instructions later). +** Mark block where variable at given level was defined +** (to emit close instructions later). */ static void markupval (FuncState *fs, int level) { BlockCnt *bl = fs->bl; @@ -259,8 +293,9 @@ static void markupval (FuncState *fs, int level) { /* - Find variable with given name 'n'. If it is an upvalue, add this - upvalue into all intermediate functions. +** Find a variable with the given name 'n'. If it is an upvalue, add +** this upvalue into all intermediate functions. If it is a global, set +** 'var' as 'void' as a flag. */ static void singlevaraux (FuncState *fs, TString *n, expdesc *var, int base) { if (fs == NULL) /* no more levels? */ @@ -287,6 +322,10 @@ static void singlevaraux (FuncState *fs, TString *n, expdesc *var, int base) { } +/* +** Find a variable with the given name 'n', handling global variables +** too. +*/ static void singlevar (LexState *ls, expdesc *var) { TString *varname = str_checkname(ls); FuncState *fs = ls->fs; @@ -301,25 +340,29 @@ static void singlevar (LexState *ls, expdesc *var) { } +/* +** Adjust the number of results from an expression list 'e' with 'nexps' +** expressions to 'nvars' values. +*/ static void adjust_assign (LexState *ls, int nvars, int nexps, expdesc *e) { FuncState *fs = ls->fs; - int extra = nvars - nexps; - if (hasmultret(e->k)) { - extra++; /* includes call itself */ - if (extra < 0) extra = 0; + int needed = nvars - nexps; /* extra values needed */ + if (hasmultret(e->k)) { /* last expression has multiple returns? */ + int extra = needed + 1; /* discount last expression itself */ + if (extra < 0) + extra = 0; luaK_setreturns(fs, e, extra); /* last exp. provides the difference */ - if (extra > 1) luaK_reserveregs(fs, extra-1); } else { - if (e->k != VVOID) luaK_exp2nextreg(fs, e); /* close last expression */ - if (extra > 0) { - int reg = fs->freereg; - luaK_reserveregs(fs, extra); - luaK_nil(fs, reg, extra); - } + if (e->k != VVOID) /* at least one expression? */ + luaK_exp2nextreg(fs, e); /* close last expression */ + if (needed > 0) /* missing values? */ + luaK_nil(fs, fs->freereg, needed); /* complete with nils */ } - if (nexps > nvars) - ls->fs->freereg -= nexps - nvars; /* remove extra values */ + if (needed > 0) + luaK_reserveregs(fs, needed); /* registers for extra values */ + else /* adding 'needed' is actually a subtraction */ + fs->freereg += needed; /* remove extra values */ } @@ -632,7 +675,7 @@ static void fieldsel (LexState *ls, expdesc *v) { expdesc key; luaK_exp2anyregup(fs, v); luaX_next(ls); /* skip the dot or colon */ - checkname(ls, &key); + codename(ls, &key); luaK_indexed(fs, v, &key); } @@ -669,7 +712,7 @@ static void recfield (LexState *ls, struct ConsControl *cc) { expdesc tab, key, val; if (ls->t.token == TK_NAME) { checklimit(fs, cc->nh, MAX_INT, "items in a constructor"); - checkname(ls, &key); + codename(ls, &key); } else /* ls->t.token == '[' */ yindex(ls, &key); @@ -938,7 +981,7 @@ static void suffixedexp (LexState *ls, expdesc *v) { case ':': { /* ':' NAME funcargs */ expdesc key; luaX_next(ls); - checkname(ls, &key); + codename(ls, &key); luaK_self(fs, v, &key); funcargs(ls, v, line); break; @@ -1048,6 +1091,9 @@ static BinOpr getbinopr (int op) { } +/* +** Priority table for binary operators. +*/ static const struct { lu_byte left; /* left priority for each binary operator */ lu_byte right; /* right priority */ @@ -1076,9 +1122,9 @@ static BinOpr subexpr (LexState *ls, expdesc *v, int limit) { UnOpr uop; enterlevel(ls); uop = getunopr(ls->t.token); - if (uop != OPR_NOUNOPR) { + if (uop != OPR_NOUNOPR) { /* prefix (unary) operator? */ int line = ls->linenumber; - luaX_next(ls); + luaX_next(ls); /* skip operator */ subexpr(ls, v, UNARY_PRIORITY); luaK_prefix(ls->fs, uop, v, line); } @@ -1089,7 +1135,7 @@ static BinOpr subexpr (LexState *ls, expdesc *v, int limit) { expdesc v2; BinOpr nextop; int line = ls->linenumber; - luaX_next(ls); + luaX_next(ls); /* skip operator */ luaK_infix(ls->fs, op, v); /* read sub-expression with higher priority */ nextop = subexpr(ls, &v2, priority[op].right); @@ -1177,21 +1223,27 @@ static void check_conflict (LexState *ls, struct LHS_assign *lh, expdesc *v) { } } - -static void assignment (LexState *ls, struct LHS_assign *lh, int nvars) { +/* +** Parse and compile a mulitple assignment. The first "variable" +** (a 'suffixedexp') was already read by the caller. +** +** assignment -> suffixedexp restassign +** restassign -> ',' suffixedexp restassign | '=' explist +*/ +static void restassign (LexState *ls, struct LHS_assign *lh, int nvars) { expdesc e; check_condition(ls, vkisvar(lh->v.k), "syntax error"); - if (testnext(ls, ',')) { /* assignment -> ',' suffixedexp assignment */ + if (testnext(ls, ',')) { /* restassign -> ',' suffixedexp restassign */ struct LHS_assign nv; nv.prev = lh; suffixedexp(ls, &nv.v); if (!vkisindexed(nv.v.k)) check_conflict(ls, lh, &nv.v); enterlevel(ls); /* control recursion depth */ - assignment(ls, &nv, nvars+1); + restassign(ls, &nv, nvars+1); leavelevel(ls); } - else { /* assignment -> '=' explist */ + else { /* restassign -> '=' explist */ int nexps; checknext(ls, '='); nexps = explist(ls, &e); @@ -1627,7 +1679,7 @@ static void exprstat (LexState *ls) { suffixedexp(ls, &v.v); if (ls->t.token == '=' || ls->t.token == ',') { /* stat -> assignment ? */ v.prev = NULL; - assignment(ls, &v, 1); + restassign(ls, &v, 1); } else { /* stat -> func */ Instruction *inst = &getinstruction(fs, &v.v); diff --git a/lparser.h b/lparser.h index 8b070b0e42..3d6bd97853 100644 --- a/lparser.h +++ b/lparser.h @@ -78,7 +78,7 @@ typedef struct expdesc { /* description of active local variable */ typedef struct Vardesc { - short idx; /* variable index in stack */ + short idx; /* index of the variable in the Proto's 'locvars' array */ } Vardesc; diff --git a/lvm.c b/lvm.c index 7d5ab9db85..ece7d77a80 100644 --- a/lvm.c +++ b/lvm.c @@ -583,7 +583,7 @@ void luaV_concat (lua_State *L, int total) { /* -** Main operation 'ra' = #rb'. +** Main operation 'ra = #rb'. */ void luaV_objlen (lua_State *L, StkId ra, const TValue *rb) { const TValue *tm; @@ -757,11 +757,13 @@ void luaV_finishOp (lua_State *L) { } break; } - case OP_TFORCALL: case OP_CALL: case OP_TAILCALL: - case OP_SETTABUP: case OP_SETTABLE: - case OP_SETI: case OP_SETFIELD: + default: { + /* only these other opcodes can yield */ + lua_assert(op == OP_TFORCALL || op == OP_CALL || + op == OP_TAILCALL || op == OP_SETTABUP || op == OP_SETTABLE || + op == OP_SETI || op == OP_SETFIELD); break; - default: lua_assert(0); + } } } From 84e32ad2ebd6bd160c1320456743a5b1d8f233e9 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 23 Nov 2018 12:23:45 -0200 Subject: [PATCH 0357/1145] Added opcodes for arithmetic with K operands Added opcodes for all seven arithmetic operators with K operands (that is, operands that are numbers in the array of constants of the function). They cover the cases of constant float operands (e.g., 'x + .0.0', 'x^0.5') and large integer operands (e.g., 'x % 10000'). --- lcode.c | 126 +++++++++++++++++++++++++++------------------- ldebug.c | 10 +++- ljumptab.h | 7 +++ lopcodes.c | 7 +++ lopcodes.h | 10 ++++ lopnames.h | 7 +++ ltests.h | 1 - lvm.c | 61 ++++++++++++++++++++++ testes/code.lua | 82 ++++++++++++++++++------------ testes/db.lua | 2 + testes/events.lua | 2 + 11 files changed, 228 insertions(+), 87 deletions(-) diff --git a/lcode.c b/lcode.c index 054b28fd1b..1872ede2ba 100644 --- a/lcode.c +++ b/lcode.c @@ -371,10 +371,6 @@ int luaK_codeABCk (FuncState *fs, OpCode o, int a, int b, int c, int k) { } -#define codeABsC(fs,o,a,b,c,k) luaK_codeABCk(fs,o,a,b,((c) + OFFSET_sC),k) - - - /* ** Format and emit an 'iABx' instruction. */ @@ -884,31 +880,46 @@ void luaK_exp2val (FuncState *fs, expdesc *e) { } +/* +** Try to make 'e' a K expression with an index in the range of R/K +** indices. Return true iff succeeded. +*/ +static int luaK_exp2K (FuncState *fs, expdesc *e) { + if (!hasjumps(e)) { + int info; + switch (e->k) { /* move constants to 'k' */ + case VTRUE: info = boolK(fs, 1); break; + case VFALSE: info = boolK(fs, 0); break; + case VNIL: info = nilK(fs); break; + case VKINT: info = luaK_intK(fs, e->u.ival); break; + case VKFLT: info = luaK_numberK(fs, e->u.nval); break; + case VK: info = e->u.info; break; + default: return 0; /* not a constant */ + } + if (info <= MAXINDEXRK) { /* does constant fit in 'argC'? */ + e->k = VK; /* make expression a 'K' expression */ + e->u.info = info; + return 1; + } + } + /* else, expression doesn't fit; leave it unchanged */ + return 0; +} + + /* ** Ensures final expression result is in a valid R/K index ** (that is, it is either in a register or in 'k' with an index ** in the range of R/K indices). -** Returns 1 if expression is K, 0 otherwise. +** Returns 1 iff expression is K. */ int luaK_exp2RK (FuncState *fs, expdesc *e) { - luaK_exp2val(fs, e); - switch (e->k) { /* move constants to 'k' */ - case VTRUE: e->u.info = boolK(fs, 1); goto vk; - case VFALSE: e->u.info = boolK(fs, 0); goto vk; - case VNIL: e->u.info = nilK(fs); goto vk; - case VKINT: e->u.info = luaK_intK(fs, e->u.ival); goto vk; - case VKFLT: e->u.info = luaK_numberK(fs, e->u.nval); goto vk; - case VK: - vk: - e->k = VK; - if (e->u.info <= MAXINDEXRK) /* constant fits in 'argC'? */ - return 1; - else break; - default: break; + if (luaK_exp2K(fs, e)) + return 1; + else { /* not a constant in the right range: put it in a register */ + luaK_exp2anyreg(fs, e); + return 0; } - /* not a constant in the right range: put it in a register */ - luaK_exp2anyreg(fs, e); - return 0; } @@ -1232,8 +1243,19 @@ static void codeunexpval (FuncState *fs, OpCode op, expdesc *e, int line) { } +/* +** Emit code for binary expressions that "produce values" +** (everything but logical operators 'and'/'or' and comparison +** operators). +** Expression to produce final result will be encoded in 'e1'. +** Because 'luaK_exp2anyreg' can free registers, its calls must be +** in "stack order" (that is, first on 'e2', which may have more +** recent registers to be released). +*/ static void finishbinexpval (FuncState *fs, expdesc *e1, expdesc *e2, - int pc, int line) { + OpCode op, int v2, int k, int line) { + int v1 = luaK_exp2anyreg(fs, e1); + int pc = luaK_codeABCk(fs, op, 0, v1, v2, k); freeexps(fs, e1, e2); e1->u.info = pc; e1->k = VRELOC; /* all those operations are relocatable */ @@ -1242,20 +1264,13 @@ static void finishbinexpval (FuncState *fs, expdesc *e1, expdesc *e2, /* -** Emit code for binary expressions that "produce values" -** (everything but logical operators 'and'/'or' and comparison -** operators). -** Expression to produce final result will be encoded in 'e1'. -** Because 'luaK_exp2anyreg' can free registers, its calls must be -** in "stack order" (that is, first on 'e2', which may have more -** recent registers to be released). +** Emit code for binary expressions that "produce values" over +** two registers. */ static void codebinexpval (FuncState *fs, OpCode op, expdesc *e1, expdesc *e2, int line) { int v2 = luaK_exp2anyreg(fs, e2); /* both operands are in registers */ - int v1 = luaK_exp2anyreg(fs, e1); - int pc = luaK_codeABC(fs, op, 0, v1, v2); /* generate opcode */ - finishbinexpval(fs, e1, e2, pc, line); + finishbinexpval(fs, e1, e2, op, v2, 0, line); } @@ -1264,41 +1279,48 @@ static void codebinexpval (FuncState *fs, OpCode op, */ static void codebini (FuncState *fs, OpCode op, expdesc *e1, expdesc *e2, int k, int line) { - int v2 = cast_int(e2->u.ival); /* immediate operand */ - int v1 = luaK_exp2anyreg(fs, e1); - int pc = codeABsC(fs, op, 0, v1, v2, k); /* generate opcode */ - finishbinexpval(fs, e1, e2, pc, line); + int v2 = cast_int(e2->u.ival) + OFFSET_sC; /* immediate operand */ + finishbinexpval(fs, e1, e2, op, v2, k, line); +} + + +static void swapexps (expdesc *e1, expdesc *e2) { + expdesc temp = *e1; *e1 = *e2; *e2 = temp; /* swap 'e1' and 'e2' */ } /* ** Code arithmetic operators ('+', '-', ...). If second operand is a ** constant in the proper range, use variant opcodes with immediate -** operands. +** operands or K operands. */ static void codearith (FuncState *fs, OpCode op, expdesc *e1, expdesc *e2, int flip, int line) { - if (!isSCint(e2)) - codebinexpval(fs, op, e1, e2, line); /* use standard operators */ - else /* use immediate operators */ + if (isSCint(e2)) /* immediate operand? */ codebini(fs, cast(OpCode, op - OP_ADD + OP_ADDI), e1, e2, flip, line); -} - - -static void swapexps (expdesc *e1, expdesc *e2) { - expdesc temp = *e1; *e1 = *e2; *e2 = temp; /* swap 'e1' and 'e2' */ + else if (tonumeral(e2, NULL) && luaK_exp2K(fs, e2)) { /* K operand? */ + int v2 = e2->u.info; /* K index */ + op = cast(OpCode, op - OP_ADD + OP_ADDK); + finishbinexpval(fs, e1, e2, op, v2, flip, line); + } + else { /* 'e2' is neither an immediate nor a K operand */ + if (flip) + swapexps(e1, e2); /* back to original order */ + codebinexpval(fs, op, e1, e2, line); /* use standard operators */ + } } /* ** Code commutative operators ('+', '*'). If first operand is a -** constant, change order of operands to use immediate operator. +** numeric constant, change order of operands to try to use an +** immediate or K operator. */ static void codecommutative (FuncState *fs, OpCode op, expdesc *e1, expdesc *e2, int line) { int flip = 0; - if (isSCint(e1)) { - swapexps(e1, e2); + if (tonumeral(e1, NULL)) { /* is first operand a numeric constant? */ + swapexps(e1, e2); /* change order */ flip = 1; } codearith(fs, op, e1, e2, flip, line); @@ -1312,7 +1334,7 @@ static void codecommutative (FuncState *fs, OpCode op, static void codebitwise (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2, int line) { int inv = 0; - int v1, v2, pc; + int v2; OpCode op; if (e1->k == VKINT && luaK_exp2RK(fs, e1)) { swapexps(e1, e2); /* 'e2' will be the constant operand */ @@ -1323,12 +1345,10 @@ static void codebitwise (FuncState *fs, BinOpr opr, codebinexpval(fs, op, e1, e2, line); /* all-register opcodes */ return; } - v1 = luaK_exp2anyreg(fs, e1); v2 = e2->u.info; /* index in K array */ op = cast(OpCode, opr - OPR_BAND + OP_BANDK); lua_assert(ttisinteger(&fs->f->k[v2])); - pc = luaK_codeABCk(fs, op, 0, v1, v2, inv); - finishbinexpval(fs, e1, e2, pc, line); + finishbinexpval(fs, e1, e2, op, v2, inv, line); } diff --git a/ldebug.c b/ldebug.c index ee1b87d901..766a52319f 100644 --- a/ldebug.c +++ b/ldebug.c @@ -610,12 +610,18 @@ static const char *funcnamefromcode (lua_State *L, CallInfo *ci, tm = TM_NEWINDEX; break; case OP_ADDI: case OP_SUBI: case OP_MULI: case OP_MODI: - case OP_POWI: case OP_DIVI: case OP_IDIVI: - case OP_BANDK: case OP_BORK: case OP_BXORK: { + case OP_POWI: case OP_DIVI: case OP_IDIVI: { int offset = GET_OPCODE(i) - OP_ADDI; /* ORDER OP */ tm = cast(TMS, offset + TM_ADD); /* ORDER TM */ break; } + case OP_ADDK: case OP_SUBK: case OP_MULK: case OP_MODK: + case OP_POWK: case OP_DIVK: case OP_IDIVK: + case OP_BANDK: case OP_BORK: case OP_BXORK: { + int offset = GET_OPCODE(i) - OP_ADDK; /* ORDER OP */ + tm = cast(TMS, offset + TM_ADD); /* ORDER TM */ + break; + } case OP_ADD: case OP_SUB: case OP_MUL: case OP_MOD: case OP_POW: case OP_DIV: case OP_IDIV: case OP_BAND: case OP_BOR: case OP_BXOR: case OP_SHL: case OP_SHR: { diff --git a/ljumptab.h b/ljumptab.h index 6767e95b18..0af997d03d 100644 --- a/ljumptab.h +++ b/ljumptab.h @@ -51,6 +51,13 @@ static void *disptab[] = { &&L_OP_POWI, &&L_OP_DIVI, &&L_OP_IDIVI, +&&L_OP_ADDK, +&&L_OP_SUBK, +&&L_OP_MULK, +&&L_OP_MODK, +&&L_OP_POWK, +&&L_OP_DIVK, +&&L_OP_IDIVK, &&L_OP_BANDK, &&L_OP_BORK, &&L_OP_BXORK, diff --git a/lopcodes.c b/lopcodes.c index 11a73c2957..3f0d551a99 100644 --- a/lopcodes.c +++ b/lopcodes.c @@ -45,6 +45,13 @@ LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = { ,opmode(0, 0, 0, 1, iABC) /* OP_POWI */ ,opmode(0, 0, 0, 1, iABC) /* OP_DIVI */ ,opmode(0, 0, 0, 1, iABC) /* OP_IDIVI */ + ,opmode(0, 0, 0, 1, iABC) /* OP_ADDK */ + ,opmode(0, 0, 0, 1, iABC) /* OP_SUBK */ + ,opmode(0, 0, 0, 1, iABC) /* OP_MULK */ + ,opmode(0, 0, 0, 1, iABC) /* OP_MODK */ + ,opmode(0, 0, 0, 1, iABC) /* OP_POWK */ + ,opmode(0, 0, 0, 1, iABC) /* OP_DIVK */ + ,opmode(0, 0, 0, 1, iABC) /* OP_IDIVK */ ,opmode(0, 0, 0, 1, iABC) /* OP_BANDK */ ,opmode(0, 0, 0, 1, iABC) /* OP_BORK */ ,opmode(0, 0, 0, 1, iABC) /* OP_BXORK */ diff --git a/lopcodes.h b/lopcodes.h index 7bb83a1e6e..d7403caf4b 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -221,6 +221,14 @@ OP_POWI,/* A B sC R(A) := R(B) ^ C */ OP_DIVI,/* A B sC R(A) := R(B) / C */ OP_IDIVI,/* A B sC R(A) := R(B) // C */ +OP_ADDK,/* A B C R(A) := R(B) + K(C) */ +OP_SUBK,/* A B C R(A) := R(B) - K(C) */ +OP_MULK,/* A B C R(A) := R(B) * K(C) */ +OP_MODK,/* A B C R(A) := R(B) % K(C) */ +OP_POWK,/* A B C R(A) := R(B) ^ K(C) */ +OP_DIVK,/* A B C R(A) := R(B) / K(C) */ +OP_IDIVK,/* A B C R(A) := R(B) // K(C) */ + OP_BANDK,/* A B C R(A) := R(B) & K(C):integer */ OP_BORK,/* A B C R(A) := R(B) | K(C):integer */ OP_BXORK,/* A B C R(A) := R(B) ~ K(C):integer */ @@ -235,11 +243,13 @@ OP_MOD,/* A B C R(A) := R(B) % R(C) */ OP_POW,/* A B C R(A) := R(B) ^ R(C) */ OP_DIV,/* A B C R(A) := R(B) / R(C) */ OP_IDIV,/* A B C R(A) := R(B) // R(C) */ + OP_BAND,/* A B C R(A) := R(B) & R(C) */ OP_BOR,/* A B C R(A) := R(B) | R(C) */ OP_BXOR,/* A B C R(A) := R(B) ~ R(C) */ OP_SHL,/* A B C R(A) := R(B) << R(C) */ OP_SHR,/* A B C R(A) := R(B) >> R(C) */ + OP_UNM,/* A B R(A) := -R(B) */ OP_BNOT,/* A B R(A) := ~R(B) */ OP_NOT,/* A B R(A) := not R(B) */ diff --git a/lopnames.h b/lopnames.h index b2c4fe21fb..96d901ac68 100644 --- a/lopnames.h +++ b/lopnames.h @@ -36,6 +36,13 @@ static const char *const opnames[] = { "POWI", "DIVI", "IDIVI", + "ADDK", + "SUBK", + "MULK", + "MODK", + "POWK", + "DIVK", + "IDIVK", "BANDK", "BORK", "BXORK", diff --git a/ltests.h b/ltests.h index d44974a444..9d409c8d43 100644 --- a/ltests.h +++ b/ltests.h @@ -119,7 +119,6 @@ LUA_API void *debug_realloc (void *ud, void *block, #undef LUAL_BUFFERSIZE #define LUAL_BUFFERSIZE 23 #define MINSTRTABSIZE 2 -#define MAXINDEXRK 1 #define MAXIWTHABS 3 diff --git a/lvm.c b/lvm.c index ece7d77a80..27ef68cfe6 100644 --- a/lvm.c +++ b/lvm.c @@ -855,6 +855,39 @@ void luaV_finishOp (lua_State *L) { else op_arithf_aux(L, v1, v2, fop, tm); } +/* +** Arithmetic operations with K operands. +*/ +#define op_arithK(L,iop,fop,tm,flip) { \ + TValue *v1 = vRB(i); \ + TValue *v2 = KC(i); \ + if (ttisinteger(v1) && ttisinteger(v2)) { \ + lua_Integer i1 = ivalue(v1); lua_Integer i2 = ivalue(v2); \ + setivalue(s2v(ra), iop(L, i1, i2)); \ + } \ + else { \ + lua_Number n1; lua_Number n2; \ + if (tonumberns(v1, n1) && tonumberns(v2, n2)) { \ + setfltvalue(s2v(ra), fop(L, n1, n2)); \ + } \ + else \ + Protect(luaT_trybinassocTM(L, v1, v2, ra, flip, tm)); } } + + +/* +** Arithmetic operations with K operands for floats. +*/ +#define op_arithfK(L,fop,tm) { \ + TValue *v1 = vRB(i); \ + TValue *v2 = KC(i); \ + lua_Number n1; lua_Number n2; \ + if (tonumberns(v1, n1) && tonumberns(v2, n2)) { \ + setfltvalue(s2v(ra), fop(L, n1, n2)); \ + } \ + else \ + Protect(luaT_trybinTM(L, v1, v2, ra, tm)); } + + /* ** Bitwise operations with constant operand. */ @@ -1219,6 +1252,34 @@ void luaV_execute (lua_State *L, CallInfo *ci) { op_arithI(L, luaV_idiv, luai_numidiv, TM_IDIV, 0); vmbreak; } + vmcase(OP_ADDK) { + op_arithK(L, l_addi, luai_numadd, TM_ADD, GETARG_k(i)); + vmbreak; + } + vmcase(OP_SUBK) { + op_arithK(L, l_subi, luai_numsub, TM_SUB, 0); + vmbreak; + } + vmcase(OP_MULK) { + op_arithK(L, l_muli, luai_nummul, TM_MUL, GETARG_k(i)); + vmbreak; + } + vmcase(OP_MODK) { + op_arithK(L, luaV_mod, luaV_modf, TM_MOD, 0); + vmbreak; + } + vmcase(OP_POWK) { + op_arithfK(L, luai_numpow, TM_POW); + vmbreak; + } + vmcase(OP_DIVK) { + op_arithfK(L, luai_numdiv, TM_DIV); + vmbreak; + } + vmcase(OP_IDIVK) { + op_arithK(L, luaV_idiv, luai_numidiv, TM_IDIV, 0); + vmbreak; + } vmcase(OP_ADD) { op_arith(L, l_addi, luai_numadd, TM_ADD); vmbreak; diff --git a/testes/code.lua b/testes/code.lua index 4d44fa6aa3..834ff5e23d 100644 --- a/testes/code.lua +++ b/testes/code.lua @@ -40,6 +40,7 @@ checkKlist(foo, {3.78/4, -3.78/4, -3.79/4}) -- testing opcodes +-- check that 'f' opcodes match '...' function check (f, ...) local arg = {...} local c = T.listcode(f) @@ -52,9 +53,19 @@ function check (f, ...) end +-- check that 'f' opcodes match '...' and that 'f(p) == r'. +function checkR (f, p, r, ...) + local r1 = f(p) + assert(r == r1 and math.type(r) == math.type(r1)) + check(f, ...) +end + + +-- check that 'a' and 'b' has the same opcodes function checkequal (a, b) a = T.listcode(a) b = T.listcode(b) + assert(#a == #b) for i = 1, #a do a[i] = string.gsub(a[i], '%b()', '') -- remove line number b[i] = string.gsub(b[i], '%b()', '') -- remove line number @@ -165,65 +176,64 @@ end, -- equalities -check(function (a) if a == 1 then return 2 end end, +checkR(function (a) if a == 1 then return 2 end end, 1, 2, 'EQI', 'JMP', 'LOADI', 'RETURN1') -check(function (a) if -4.0 == a then return 2 end end, +checkR(function (a) if -4.0 == a then return 2 end end, -4, 2, 'EQI', 'JMP', 'LOADI', 'RETURN1') -check(function (a) if a == "hi" then return 2 end end, +checkR(function (a) if a == "hi" then return 2 end end, 10, nil, 'EQK', 'JMP', 'LOADI', 'RETURN1') -check(function (a) if a == 10000 then return 2 end end, +checkR(function (a) if a == 10000 then return 2 end end, 1, nil, 'EQK', 'JMP', 'LOADI', 'RETURN1') -- number too large -check(function (a) if -10000 == a then return 2 end end, +checkR(function (a) if -10000 == a then return 2 end end, -10000, 2, 'EQK', 'JMP', 'LOADI', 'RETURN1') -- number too large -- comparisons -check(function (a) if -10 <= a then return 2 end end, +checkR(function (a) if -10 <= a then return 2 end end, -10, 2, 'GEI', 'JMP', 'LOADI', 'RETURN1') -check(function (a) if 128.0 > a then return 2 end end, +checkR(function (a) if 128.0 > a then return 2 end end, 129, nil, 'LTI', 'JMP', 'LOADI', 'RETURN1') -check(function (a) if -127.0 < a then return 2 end end, +checkR(function (a) if -127.0 < a then return 2 end end, -127, nil, 'GTI', 'JMP', 'LOADI', 'RETURN1') -check(function (a) if 10 < a then return 2 end end, +checkR(function (a) if 10 < a then return 2 end end, 11, 2, 'GTI', 'JMP', 'LOADI', 'RETURN1') -check(function (a) if 129 < a then return 2 end end, +checkR(function (a) if 129 < a then return 2 end end, 130, 2, 'LOADI', 'LT', 'JMP', 'LOADI', 'RETURN1') -check(function (a) if a >= 23.0 then return 2 end end, +checkR(function (a) if a >= 23.0 then return 2 end end, 25, 2, 'GEI', 'JMP', 'LOADI', 'RETURN1') -check(function (a) if a >= 23.1 then return 2 end end, +checkR(function (a) if a >= 23.1 then return 2 end end, 0, nil, 'LOADK', 'LE', 'JMP', 'LOADI', 'RETURN1') -check(function (a) if a > 2300.0 then return 2 end end, +checkR(function (a) if a > 2300.0 then return 2 end end, 0, nil, 'LOADF', 'LT', 'JMP', 'LOADI', 'RETURN1') -- constant folding local function checkK (func, val) check(func, 'LOADK', 'RETURN1') - local k = T.listk(func) - assert(#k == 1 and k[1] == val and math.type(k[1]) == math.type(val)) + checkKlist(func, {val}) assert(func() == val) end local function checkI (func, val) check(func, 'LOADI', 'RETURN1') - assert(#T.listk(func) == 0) + checkKlist(func, {}) assert(func() == val) end local function checkF (func, val) check(func, 'LOADF', 'RETURN1') - assert(#T.listk(func) == 0) + checkKlist(func, {}) assert(func() == val) end @@ -258,20 +268,30 @@ checkK(function () return -65536.0 end, -(sbx + 1.0)) -- immediate operands -check(function (x) return x + 1 end, 'ADDI', 'RETURN1') -check(function (x) return 128 + x end, 'ADDI', 'RETURN1') -check(function (x) return x * -127 end, 'MULI', 'RETURN1') -check(function (x) return 20 * x end, 'MULI', 'RETURN1') -check(function (x) return x ^ -2 end, 'POWI', 'RETURN1') -check(function (x) return x / 40 end, 'DIVI', 'RETURN1') -check(function (x) return x // 1 end, 'IDIVI', 'RETURN1') -check(function (x) return x % (100 - 10) end, 'MODI', 'RETURN1') -check(function (x) return 1 << x end, 'SHLI', 'RETURN1') -check(function (x) return x << 2 end, 'SHRI', 'RETURN1') -check(function (x) return x >> 2 end, 'SHRI', 'RETURN1') -check(function (x) return x & 1 end, 'BANDK', 'RETURN1') -check(function (x) return 10 | x end, 'BORK', 'RETURN1') -check(function (x) return -10 ~ x end, 'BXORK', 'RETURN1') +checkR(function (x) return x + 1 end, 10, 11, 'ADDI', 'RETURN1') +checkR(function (x) return 128 + x end, 0.0, 128.0, 'ADDI', 'RETURN1') +checkR(function (x) return x * -127 end, -1.0, 127.0, 'MULI', 'RETURN1') +checkR(function (x) return 20 * x end, 2, 40, 'MULI', 'RETURN1') +checkR(function (x) return x ^ -2 end, 2, 0.25, 'POWI', 'RETURN1') +checkR(function (x) return x / 40 end, 40, 1.0, 'DIVI', 'RETURN1') +checkR(function (x) return x // 1 end, 10.0, 10.0, 'IDIVI', 'RETURN1') +checkR(function (x) return x % (100 - 10) end, 91, 1, 'MODI', 'RETURN1') +checkR(function (x) return 1 << x end, 3, 8, 'SHLI', 'RETURN1') +checkR(function (x) return x << 2 end, 10, 40, 'SHRI', 'RETURN1') +checkR(function (x) return x >> 2 end, 8, 2, 'SHRI', 'RETURN1') +checkR(function (x) return x & 1 end, 9, 1, 'BANDK', 'RETURN1') +checkR(function (x) return 10 | x end, 1, 11, 'BORK', 'RETURN1') +checkR(function (x) return -10 ~ x end, -1, 9, 'BXORK', 'RETURN1') + +-- K operands in arithmetic operations +checkR(function (x) return x + 0.0 end, 1, 1.0, 'ADDK', 'RETURN1') +-- check(function (x) return 128 + x end, 'ADDK', 'RETURN1') +checkR(function (x) return x * -10000 end, 2, -20000, 'MULK', 'RETURN1') +-- check(function (x) return 20 * x end, 'MULK', 'RETURN1') +checkR(function (x) return x ^ 0.5 end, 4, 2.0, 'POWK', 'RETURN1') +checkR(function (x) return x / 2.0 end, 4, 2.0, 'DIVK', 'RETURN1') +checkR(function (x) return x // 10000 end, 10000, 1, 'IDIVK', 'RETURN1') +checkR(function (x) return x % (100.0 - 10) end, 91, 1.0, 'MODK', 'RETURN1') -- no foldings (and immediate operands) check(function () return -0.0 end, 'LOADF', 'UNM', 'RETURN1') diff --git a/testes/db.lua b/testes/db.lua index 9da682102e..5b243c3993 100644 --- a/testes/db.lua +++ b/testes/db.lua @@ -794,6 +794,8 @@ assert(a[3] == "index" and a^3 == "pow" and a..a == "concat") assert(a/3 == "div" and 3%a == "mod") assert(a+3 == "add" and 3-a == "sub" and a*3 == "mul" and -a == "unm" and #a == "len" and a&3 == "band") +assert(a + 30000 == "add" and a - 3.0 == "sub" and a * 3.0 == "mul" and + -a == "unm" and #a == "len" and a & 3 == "band") assert(a|3 == "bor" and 3~a == "bxor" and a<<3 == "shift" and a>>1 == "shift") assert (a==b and a.op == "eq") diff --git a/testes/events.lua b/testes/events.lua index b071c2a314..ac630d895c 100644 --- a/testes/events.lua +++ b/testes/events.lua @@ -144,6 +144,8 @@ t.__bnot = f("bnot") -- when the constant table is very small. assert(b+5 == b) assert(cap[0] == "add" and cap[1] == b and cap[2] == 5 and cap[3]==undef) +assert(5.2 + b == 5.2) +assert(cap[0] == "add" and cap[1] == 5.2 and cap[2] == b and cap[3]==undef) assert(b+'5' == b) assert(cap[0] == "add" and cap[1] == b and cap[2] == '5' and cap[3]==undef) assert(5+b == 5) From 7e63d3da0240325db4011f5d2f2e8abfb5d60288 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Sat, 24 Nov 2018 11:59:15 -0200 Subject: [PATCH 0358/1145] Some bugs with stack reallocation by 'luaF_close' (Long time without testing with '-DHARDSTACKTESTS'...) With the introduction of to-be-closed variables, calls to 'luaF_close' can move the stack, but some call sites where keeping pointers to the stack without correcting them. --- ldo.c | 9 ++++++--- ljumptab.h | 2 +- ltests.c | 2 +- lvm.c | 21 +++++++++------------ 4 files changed, 17 insertions(+), 17 deletions(-) diff --git a/ldo.c b/ldo.c index b7a76ef6f7..2762fefa0b 100644 --- a/ldo.c +++ b/ldo.c @@ -383,8 +383,10 @@ static void moveresults (lua_State *L, StkId res, int nres, int wanted) { wanted = nres; /* we want all results */ break; default: /* multiple results (or to-be-closed variables) */ - if (hastocloseCfunc(wanted)) { - luaF_close(L, res, LUA_OK); + if (hastocloseCfunc(wanted)) { /* to-be-closed variables? */ + ptrdiff_t savedres = savestack(L, res); + luaF_close(L, res, LUA_OK); /* may change the stack */ + res = restorestack(L, savedres); wanted = codeNresults(wanted); /* correct value */ if (wanted == LUA_MULTRET) wanted = nres; @@ -590,7 +592,8 @@ static int recover (lua_State *L, int status) { if (ci == NULL) return 0; /* no recovery point */ /* "finish" luaD_pcall */ oldtop = restorestack(L, ci->u2.funcidx); - luaF_close(L, oldtop, status); + luaF_close(L, oldtop, status); /* may change the stack */ + oldtop = restorestack(L, ci->u2.funcidx); luaD_seterrorobj(L, status, oldtop); L->ci = ci; L->allowhook = getoah(ci->callstatus); /* restore original 'allowhook' */ diff --git a/ljumptab.h b/ljumptab.h index 0af997d03d..9fa72a73c8 100644 --- a/ljumptab.h +++ b/ljumptab.h @@ -16,7 +16,7 @@ #define vmbreak vmfetch(); vmdispatch(GET_OPCODE(i)); -static void *disptab[] = { +static void *disptab[NUM_OPCODES] = { #if 0 ** you can update the following list with this command: diff --git a/ltests.c b/ltests.c index c5c1040ad6..63d423e08e 100644 --- a/ltests.c +++ b/ltests.c @@ -142,7 +142,7 @@ void *debug_realloc (void *ud, void *b, size_t oldsize, size_t size) { freeblock(mc, block); return NULL; } - if (mc->countlimit != ~0UL && size > 0) { /* count limit in use? */ + if (mc->countlimit != ~0UL && size != oldsize) { /* count limit in use? */ if (mc->countlimit == 0) return NULL; /* fake a memory allocation error */ mc->countlimit--; diff --git a/lvm.c b/lvm.c index 27ef68cfe6..a6dcc9ffe9 100644 --- a/lvm.c +++ b/lvm.c @@ -946,6 +946,9 @@ void luaV_finishOp (lua_State *L) { #define updatebase(ci) (base = ci->func + 1) +#define updatestack(ci) { if (trap) { updatebase(ci); ra = RA(i); } } + + /* ** Execute a jump instruction. The 'updatetrap' allows signals to stop ** tight loops. (Without it, the local copy of 'trap' could never change.) @@ -1557,24 +1560,21 @@ void luaV_execute (lua_State *L, CallInfo *ci) { L->top = ra + b; else /* previous instruction set top */ b = cast_int(L->top - ra); - savepc(ci); if (TESTARG_k(i)) { int nparams1 = GETARG_C(i); if (nparams1) /* vararg function? */ delta = ci->u.l.nextraargs + nparams1; - luaF_close(L, base, LUA_OK); /* close upvalues from current call */ + /* close upvalues from current call */ + ProtectNT(luaF_close(L, base, LUA_OK)); + updatestack(ci); } if (!ttisfunction(s2v(ra))) { /* not a function? */ luaD_tryfuncTM(L, ra); /* try '__call' metamethod */ b++; /* there is now one extra argument */ } if (!ttisLclosure(s2v(ra))) { /* C function? */ - luaD_call(L, ra, LUA_MULTRET); /* call it */ - updatetrap(ci); - if (trap) { /* stack may have been relocated */ - updatebase(ci); - ra = RA(i); - } + ProtectNT(luaD_call(L, ra, LUA_MULTRET)); /* call it */ + updatestack(ci); /* stack may have been relocated */ ci->func -= delta; luaD_poscall(L, ci, cast_int(L->top - ra)); return; @@ -1739,10 +1739,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { memcpy(ra + 4, ra, 3 * sizeof(*ra)); L->top = ra + 4 + 3; Protect(luaD_call(L, ra + 4, GETARG_C(i))); /* do the call */ - if (trap) { /* stack may have changed? */ - updatebase(ci); /* keep 'base' correct */ - ra = RA(i); /* keep 'ra' correct for next instruction */ - } + updatestack(ci); /* stack may have changed */ i = *(pc++); /* go to next instruction */ ra += 2; /* adjust for next instruction */ lua_assert(GET_OPCODE(i) == OP_TFORLOOP && ra == RA(i)); From 7696c6474fe51ed59fee324e78c1233af74febdd Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 26 Nov 2018 14:16:17 -0200 Subject: [PATCH 0359/1145] Auxiliary buffer cannot close box with 'lua_remove' To remove a to-be-closed variable from the stack in the C API a function must use 'lua_settop' or 'lua_pop'. Previous implementation of 'luaL_pushresult' was not closing the box. (This commit also added tests to check that box is being closed "as soon as possible".) --- lapi.c | 4 ++-- lauxlib.c | 12 +++++----- testes/locals.lua | 56 +++++++++++++++++++++++++++++++++-------------- 3 files changed, 49 insertions(+), 23 deletions(-) diff --git a/lapi.c b/lapi.c index da866a660b..147ed0ff0f 100644 --- a/lapi.c +++ b/lapi.c @@ -1213,8 +1213,8 @@ LUA_API void lua_toclose (lua_State *L, int idx) { lua_lock(L); o = index2stack(L, idx); nresults = L->ci->nresults; - api_check(L, L->openupval == NULL || uplevel(L->openupval) < o, - "there is an already marked index below"); + api_check(L, L->openupval == NULL || uplevel(L->openupval) <= o, + "marked index below or equal new one"); luaF_newtbcupval(L, o); /* create new to-be-closed upvalue */ if (!hastocloseCfunc(nresults)) /* function not marked yet? */ L->ci->nresults = codeNresults(nresults); /* mark it */ diff --git a/lauxlib.c b/lauxlib.c index 6069ed1b24..fd4acbd109 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -524,8 +524,8 @@ static size_t newbuffsize (luaL_Buffer *B, size_t sz) { /* ** Returns a pointer to a free area with at least 'sz' bytes in buffer -** 'B'. 'boxidx' is the position in the stack where the buffer's box is -** or should be. +** 'B'. 'boxidx' is the relative position in the stack where the +** buffer's box is or should be. */ static char *prepbuffsize (luaL_Buffer *B, size_t sz, int boxidx) { if (B->size - B->n >= sz) /* enough space? */ @@ -538,8 +538,10 @@ static char *prepbuffsize (luaL_Buffer *B, size_t sz, int boxidx) { if (buffonstack(B)) /* buffer already has a box? */ newbuff = (char *)resizebox(L, boxidx, newsize); /* resize it */ else { /* no box yet */ + lua_pushnil(L); /* reserve slot for final result */ newbox(L); /* create a new box */ - lua_insert(L, boxidx); /* move box to its intended position */ + /* move box (and slot) to its intended position */ + lua_rotate(L, boxidx - 1, 2); lua_toclose(L, boxidx); newbuff = (char *)resizebox(L, boxidx, newsize); memcpy(newbuff, B->b, B->n * sizeof(char)); /* copy original content */ @@ -576,8 +578,8 @@ LUALIB_API void luaL_pushresult (luaL_Buffer *B) { lua_State *L = B->L; lua_pushlstring(L, B->b, B->n); if (buffonstack(B)) { - resizebox(L, -2, 0); /* delete old buffer */ - lua_remove(L, -2); /* remove its header from the stack */ + lua_copy(L, -1, -3); /* move string to reserved slot */ + lua_pop(L, 2); /* pop string and box (closing the box) */ } } diff --git a/testes/locals.lua b/testes/locals.lua index 8766b3d5ce..90a8b8456e 100644 --- a/testes/locals.lua +++ b/testes/locals.lua @@ -242,6 +242,7 @@ do assert(a == 1 and b == 2 and c == 3 and X == 20 and Y == 10 and d == nil) end + do -- errors in __close local log = {} local function foo (err) @@ -264,7 +265,9 @@ do -- errors in __close and #log == 4) end + if rawget(_G, "T") then + -- memory error inside closing function local function foo () local *toclose y = function () T.alloccount() end @@ -296,7 +299,7 @@ if rawget(_G, "T") then local function test () local *toclose x = enter(0) -- set a memory limit -- creation of previous upvalue will raise a memory error - os.exit(false) -- should not run + assert(false) -- should not run end local _, msg = pcall(test) @@ -326,33 +329,54 @@ if rawget(_G, "T") then do -- testing 'toclose' in C string buffer - local s = string.rep("a", 10000) + collectgarbage() + local s = string.rep('a', 10000) -- large string + local m = T.totalmem() + collectgarbage("stop") + s = string.upper(s) -- allocate buffer + new string (10K each) + -- ensure buffer was deallocated + assert(T.totalmem() - m <= 11000) + collectgarbage("restart") + end + + do -- now some tests for freeing buffer in case of errors + local lim = 10000 -- some size larger than the static buffer + local extra = 2000 -- some extra memory (for callinfo, etc.) + + local s = string.rep("a", lim) + + -- concat this table needs two buffer resizes (one for each 's') local a = {s, s} - -- ensure proper initialization (stack space, metatable) - table.concat(a) - collectgarbage(); collectgarbage() + collectgarbage() - local m = T.totalmem() + m = T.totalmem() + collectgarbage("stop") + + -- error in the first buffer allocation + T. totalmem(m + extra) + assert(not pcall(table.concat, a)) + -- first buffer was not even allocated + assert(T.totalmem() - m <= extra) -- error in the second buffer allocation - T.alloccount(3) + T. totalmem(m + lim + extra) assert(not pcall(table.concat, a)) - T.alloccount() -- first buffer was released by 'toclose' - assert(T.totalmem() - m <= 5000) + assert(T.totalmem() - m <= extra) -- error in creation of final string - T.alloccount(4) + T.totalmem(m + 2 * lim + extra) assert(not pcall(table.concat, a)) - T.alloccount() -- second buffer was released by 'toclose' - assert(T.totalmem() - m <= 5000) + assert(T.totalmem() - m <= extra) - -- userdata, upvalue, buffer, buffer, string - T.alloccount(5) - assert(#table.concat(a) == 20000) - T.alloccount() + -- userdata, upvalue, buffer, buffer, final string + T.totalmem(m + 4*lim + extra) + assert(#table.concat(a) == 2*lim) + + T.totalmem(0) -- remove memory limit + collectgarbage("restart") print'+' end From 6d04537ea660fd12fc16c328366b701fabaf4919 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 29 Nov 2018 16:02:44 -0200 Subject: [PATCH 0360/1145] A to-be-closed variable must have a closable value (or be nil) It is an error for a to-be-closed variable to have a non-closable non-nil value when it is being closed. This situation does not seem to be useful and often hints to an error. (Particularly in the C API, it is easy to change a to-be-closed index by mistake.) --- lapi.c | 2 +- ldebug.c | 18 ++++++++++-------- ldebug.h | 2 ++ lfunc.c | 6 ++++++ lvm.c | 7 +++---- manual/manual.of | 25 ++++++++++++++++--------- testes/api.lua | 21 ++++++++++++++------- testes/db.lua | 20 ++++++++++---------- testes/locals.lua | 21 +++++++++++++++++++++ 9 files changed, 83 insertions(+), 39 deletions(-) diff --git a/lapi.c b/lapi.c index 147ed0ff0f..9cabe7ca4e 100644 --- a/lapi.c +++ b/lapi.c @@ -1299,7 +1299,7 @@ static const char *aux_upvalue (TValue *fi, int n, TValue **val, *val = f->upvals[n-1]->v; if (owner) *owner = obj2gco(f->upvals[n - 1]); name = p->upvalues[n-1].name; - return (name == NULL) ? "(*no name)" : getstr(name); + return (name == NULL) ? "(no name)" : getstr(name); } default: return NULL; /* not a closure */ } diff --git a/ldebug.c b/ldebug.c index 766a52319f..bd471e0c08 100644 --- a/ldebug.c +++ b/ldebug.c @@ -192,15 +192,14 @@ static const char *findvararg (CallInfo *ci, int n, StkId *pos) { int nextra = ci->u.l.nextraargs; if (n <= nextra) { *pos = ci->func - nextra + (n - 1); - return "(*vararg)"; /* generic name for any vararg */ + return "(vararg)"; /* generic name for any vararg */ } } return NULL; /* no such vararg */ } -static const char *findlocal (lua_State *L, CallInfo *ci, int n, - StkId *pos) { +const char *luaG_findlocal (lua_State *L, CallInfo *ci, int n, StkId *pos) { StkId base = ci->func + 1; const char *name = NULL; if (isLua(ci)) { @@ -211,12 +210,15 @@ static const char *findlocal (lua_State *L, CallInfo *ci, int n, } if (name == NULL) { /* no 'standard' name? */ StkId limit = (ci == L->ci) ? L->top : ci->next->func; - if (limit - base >= n && n > 0) /* is 'n' inside 'ci' stack? */ - name = "(*temporary)"; /* generic name for any valid slot */ + if (limit - base >= n && n > 0) { /* is 'n' inside 'ci' stack? */ + /* generic name for any valid slot */ + name = isLua(ci) ? "(temporary)" : "(C temporary)"; + } else return NULL; /* no name */ } - *pos = base + (n - 1); + if (pos) + *pos = base + (n - 1); return name; } @@ -232,7 +234,7 @@ LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) { } else { /* active function; get information through 'ar' */ StkId pos = NULL; /* to avoid warnings */ - name = findlocal(L, ar->i_ci, n, &pos); + name = luaG_findlocal(L, ar->i_ci, n, &pos); if (name) { setobjs2s(L, L->top, pos); api_incr_top(L); @@ -247,7 +249,7 @@ LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) { StkId pos = NULL; /* to avoid warnings */ const char *name; lua_lock(L); - name = findlocal(L, ar->i_ci, n, &pos); + name = luaG_findlocal(L, ar->i_ci, n, &pos); if (name) { setobjs2s(L, pos, L->top - 1); L->top--; /* pop value */ diff --git a/ldebug.h b/ldebug.h index f080711d59..1fe0efab08 100644 --- a/ldebug.h +++ b/ldebug.h @@ -22,6 +22,8 @@ #define ABSLINEINFO (-0x80) LUAI_FUNC int luaG_getfuncline (const Proto *f, int pc); +LUAI_FUNC const char *luaG_findlocal (lua_State *L, CallInfo *ci, int n, + StkId *pos); LUAI_FUNC l_noret luaG_typeerror (lua_State *L, const TValue *o, const char *opname); LUAI_FUNC l_noret luaG_forerror (lua_State *L, const TValue *o, diff --git a/lfunc.c b/lfunc.c index aa6ce58f17..11d2850f76 100644 --- a/lfunc.c +++ b/lfunc.c @@ -14,6 +14,7 @@ #include "lua.h" +#include "ldebug.h" #include "ldo.h" #include "lfunc.h" #include "lgc.h" @@ -140,6 +141,11 @@ static int closeupval (lua_State *L, TValue *uv, StkId level, int status) { if (likely(status == LUA_OK)) { if (prepclosingmethod(L, uv, &G(L)->nilvalue)) /* something to call? */ callclose(L, NULL); /* call closing method */ + else if (!ttisnil(uv)) { /* non-closable non-nil value? */ + const char *vname = luaG_findlocal(L, L->ci, level - L->ci->func, NULL); + if (vname == NULL) vname = "?"; + luaG_runerror(L, "attempt to close non-closable variable '%s'", vname); + } } else { /* there was an error */ /* save error message and set stack top to 'level + 1' */ diff --git a/lvm.c b/lvm.c index a6dcc9ffe9..50d967c6b8 100644 --- a/lvm.c +++ b/lvm.c @@ -1427,7 +1427,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { } vmcase(OP_CLOSE) { L->top = ra + 1; /* everything is free after this slot */ - ProtectNT(luaF_close(L, ra, LUA_OK)); + Protect(luaF_close(L, ra, LUA_OK)); vmbreak; } vmcase(OP_TBC) { @@ -1717,9 +1717,8 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_TFORPREP) { - /* is 'toclose' a function or has a '__close' metamethod? */ - if (ttisfunction(s2v(ra + 3)) || - !ttisnil(luaT_gettmbyobj(L, s2v(ra + 3), TM_CLOSE))) { + /* is 'toclose' not nil? */ + if (!ttisnil(s2v(ra + 3))) { /* create to-be-closed upvalue for it */ halfProtect(luaF_newtbcupval(L, ra + 3)); } diff --git a/manual/manual.of b/manual/manual.of index f891c33e94..3902f2f359 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -1063,11 +1063,16 @@ which start with @T{0x} or @T{0X}. Hexadecimal constants also accept an optional fractional part plus an optional binary exponent, marked by a letter @Char{p} or @Char{P}. + A numeric constant with a radix point or an exponent denotes a float; otherwise, -if its value fits in an integer, -it denotes an integer. +if its value fits in an integer or it is a hexadecimal constant, +it denotes an integer; +otherwise (that is, a decimal integer numeral that overflows), +it denotes a float. +(Hexadecimal integer numerals that overflow @emph{wrap around}; +they always denote an integer value.) Examples of valid integer constants are @verbatim{ 3 345 0xff 0xBEBADA @@ -1542,7 +1547,8 @@ If the value of the variable when it goes out of scope is a function, that function is called; otherwise, if the value has a @idx{__close} metamethod, that metamethod is called; -otherwise, nothing is done. +otherwise, if the value is @nil, nothing is done; +otherwise, an error is raised. In the function case, if the scope is being closed by an error, the error object is passed as an argument to the function; @@ -1665,7 +1671,7 @@ If both operands are integers, the operation is performed over integers and the result is an integer. Otherwise, if both operands are numbers, then they are converted to floats, -the operation is performed following the usual rules +the operation is performed following the machine's rules for floating-point arithmetic (usually the @x{IEEE 754} standard), and the result is a float. @@ -4998,7 +5004,7 @@ This call leaves the final string on the top of the stack. } -If you know beforehand the total size of the resulting string, +If you know beforehand the maximum size of the resulting string, you can use the buffer like this: @itemize{ @@ -5012,7 +5018,8 @@ size @id{sz} with a call @T{luaL_buffinitsize(L, &b, sz)}.} @item{ Finish by calling @T{luaL_pushresultsize(&b, sz)}, where @id{sz} is the total size of the resulting string -copied into that space. +copied into that space (which may be smaller than or +equal to the preallocated size). } } @@ -5028,8 +5035,8 @@ when you call a buffer operation, the stack is at the same level it was immediately after the previous buffer operation. (The only exception to this rule is @Lid{luaL_addvalue}.) -After calling @Lid{luaL_pushresult} the stack is back to its -level when the buffer was initialized, +After calling @Lid{luaL_pushresult}, +the stack is back to its level when the buffer was initialized, plus the final string on its top. } @@ -7118,7 +7125,7 @@ empty string as a match immediately after another match. As an example, consider the results of the following code: @verbatim{ -> string.gsub("abc", "()a*()", print) +> string.gsub("abc", "()a*()", print); --> 1 2 --> 3 3 --> 4 4 diff --git a/testes/api.lua b/testes/api.lua index ed857fd055..6f35e13249 100644 --- a/testes/api.lua +++ b/testes/api.lua @@ -366,7 +366,7 @@ do -- "argerror" without frames assert(T.checkpanic("loadstring 4") == "bad argument #4 (string expected, got no value)") - + -- memory error T.totalmem(T.totalmem()+10000) -- set low memory limit (+10k) @@ -987,12 +987,12 @@ do local a, b = T.testC([[ call 0 1 # create resource - pushint 34 + pushnil toclose -2 # mark call result to be closed - toclose -1 # mark number to be closed (will be ignored) + toclose -1 # mark nil to be closed (will be ignored) return 2 ]], newresource) - assert(a[1] == 11 and b == 34) + assert(a[1] == 11 and b == nil) assert(#openresource == 0) -- was closed -- repeat the test, but calling function in a 'multret' context @@ -1005,7 +1005,7 @@ do assert(#openresource == 0) -- was closed -- error - local a, b = pcall(T.testC, [[ + local a, b = pcall(T.makeCfunc[[ call 0 1 # create resource toclose -1 # mark it to be closed error # resource is the error object @@ -1038,6 +1038,13 @@ do ]], newresource, check) assert(a == 3) -- no extra items left in the stack + -- non-closable value + local a, b = pcall(T.makeCfunc[[ + pushint 32 + toclose -1 + ]]) + assert(not a and string.find(b, "(C temporary)")) + end @@ -1249,9 +1256,9 @@ do -- closing state with no extra memory T.closestate(L) T.alloccount() end - + do -- garbage collection with no extra memory - local L = T.newstate() + local L = T.newstate() T.loadlib(L) local res = (T.doremote(L, [[ _ENV = require"_G" diff --git a/testes/db.lua b/testes/db.lua index 5b243c3993..976962b02f 100644 --- a/testes/db.lua +++ b/testes/db.lua @@ -214,14 +214,14 @@ local function foo (a, ...) local t = table.pack(...) for i = 1, t.n do local n, v = debug.getlocal(1, -i) - assert(n == "(*vararg)" and v == t[i]) + assert(n == "(vararg)" and v == t[i]) end assert(not debug.getlocal(1, -(t.n + 1))) assert(not debug.setlocal(1, -(t.n + 1), 30)) if t.n > 0 then (function (x) - assert(debug.setlocal(2, -1, x) == "(*vararg)") - assert(debug.setlocal(2, -t.n, x) == "(*vararg)") + assert(debug.setlocal(2, -1, x) == "(vararg)") + assert(debug.setlocal(2, -t.n, x) == "(vararg)") end)(430) assert(... == 430) end @@ -328,9 +328,9 @@ assert(a[f] and a[g] and a[assert] and a[debug.getlocal] and not a[print]) -- tests for manipulating non-registered locals (C and Lua temporaries) local n, v = debug.getlocal(0, 1) -assert(v == 0 and n == "(*temporary)") +assert(v == 0 and n == "(C temporary)") local n, v = debug.getlocal(0, 2) -assert(v == 2 and n == "(*temporary)") +assert(v == 2 and n == "(C temporary)") assert(not debug.getlocal(0, 3)) assert(not debug.getlocal(0, 0)) @@ -607,7 +607,7 @@ co = load[[ local a = 0 -- 'A' should be visible to debugger only after its complete definition debug.sethook(function (e, l) - if l == 3 then a = a + 1; assert(debug.getlocal(2, 1) == "(*temporary)") + if l == 3 then a = a + 1; assert(debug.getlocal(2, 1) == "(temporary)") elseif l == 4 then a = a + 1; assert(debug.getlocal(2, 1) == "A") end end, "l") @@ -875,15 +875,15 @@ local debug = require'debug' local a = 12 -- a local variable local n, v = debug.getlocal(1, 1) -assert(n == "(*temporary)" and v == debug) -- unkown name but known value +assert(n == "(temporary)" and v == debug) -- unkown name but known value n, v = debug.getlocal(1, 2) -assert(n == "(*temporary)" and v == 12) -- unkown name but known value +assert(n == "(temporary)" and v == 12) -- unkown name but known value -- a function with an upvalue local f = function () local x; return a end n, v = debug.getupvalue(f, 1) -assert(n == "(*no name)" and v == 12) -assert(debug.setupvalue(f, 1, 13) == "(*no name)") +assert(n == "(no name)" and v == 12) +assert(debug.setupvalue(f, 1, 13) == "(no name)") assert(a == 13) local t = debug.getinfo(f) diff --git a/testes/locals.lua b/testes/locals.lua index 90a8b8456e..24681dd9eb 100644 --- a/testes/locals.lua +++ b/testes/locals.lua @@ -266,6 +266,27 @@ do -- errors in __close end +do + + -- errors due to non-closable values + local function foo () + local *toclose x = 34 + end + local stat, msg = pcall(foo) + assert(not stat and string.find(msg, "variable 'x'")) + + + -- with other errors, non-closable values are ignored + local function foo () + local *toclose x = 34 + local *toclose y = function () error(32) end + end + local stat, msg = pcall(foo) + assert(not stat and msg == 32) + +end + + if rawget(_G, "T") then -- memory error inside closing function From 28d829c86712ce5bc453feccc5129a32f78d80c0 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 4 Dec 2018 15:01:42 -0200 Subject: [PATCH 0361/1145] Calls cannot be tail in the scope of a to-be-closed variable A to-be-closed variable must be closed when a block ends, so even a 'return foo()' cannot directly returns the results of 'foo'; the function must close the scope before returning. --- lparser.c | 5 ++++- lvm.c | 2 +- manual/manual.of | 17 ++++++++--------- testes/locals.lua | 13 ++++++++----- 4 files changed, 21 insertions(+), 16 deletions(-) diff --git a/lparser.c b/lparser.c index e525e57816..eed8bffd93 100644 --- a/lparser.c +++ b/lparser.c @@ -53,6 +53,7 @@ typedef struct BlockCnt { lu_byte nactvar; /* # active locals outside the block */ lu_byte upval; /* true if some variable in the block is an upvalue */ lu_byte isloop; /* true if 'block' is a loop */ + lu_byte insidetbc; /* true if inside the scope of a to-be-closed var. */ } BlockCnt; @@ -510,6 +511,7 @@ static void enterblock (FuncState *fs, BlockCnt *bl, lu_byte isloop) { bl->firstlabel = fs->ls->dyd->label.n; bl->firstgoto = fs->ls->dyd->gt.n; bl->upval = 0; + bl->insidetbc = (fs->bl != NULL && fs->bl->insidetbc); bl->previous = fs->bl; fs->bl = bl; lua_assert(fs->freereg == fs->nactvar); @@ -1631,6 +1633,7 @@ static void tocloselocalstat (LexState *ls) { checknext(ls, '='); exp1(ls, 0); markupval(fs, fs->nactvar); + fs->bl->insidetbc = 1; /* in the scope of a to-be-closed variable */ adjustlocalvars(ls, 1); luaK_codeABC(fs, OP_TBC, fs->nactvar - 1, 0, 0); } @@ -1701,7 +1704,7 @@ static void retstat (LexState *ls) { nret = explist(ls, &e); /* optional return values */ if (hasmultret(e.k)) { luaK_setmultret(fs, &e); - if (e.k == VCALL && nret == 1) { /* tail call? */ + if (e.k == VCALL && nret == 1 && !fs->bl->insidetbc) { /* tail call? */ SET_OPCODE(getinstruction(fs,&e), OP_TAILCALL); lua_assert(GETARG_A(getinstruction(fs,&e)) == fs->nactvar); } diff --git a/lvm.c b/lvm.c index 50d967c6b8..fc8722a84f 100644 --- a/lvm.c +++ b/lvm.c @@ -1565,7 +1565,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { if (nparams1) /* vararg function? */ delta = ci->u.l.nextraargs + nparams1; /* close upvalues from current call */ - ProtectNT(luaF_close(L, base, LUA_OK)); + luaF_close(L, base, -1); /* (no to-be-closed vars. here) */ updatestack(ci); } if (!ttisfunction(s2v(ra))) { /* not a function? */ diff --git a/manual/manual.of b/manual/manual.of index 3902f2f359..8b5e5d93aa 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -1538,9 +1538,6 @@ except that its value is @emph{closed} whenever the variable goes out of scope, including normal block termination, exiting its block by @Rw{break}/@Rw{goto}/@Rw{return}, or exiting by an error. -If a block ends in a tail call @see{functioncall}, -all variables of the caller function go out of scope -before the start of the callee function. To \emph{close} a value has the following meaning here: If the value of the variable when it goes out of scope is a function, @@ -2038,8 +2035,8 @@ A call of the form @T{f'@rep{string}'} is syntactic sugar for @T{f('@rep{string}')}; that is, the argument list is a single literal string. -A call of the form @T{return @rep{functioncall}} is called -a @def{tail call}. +A call of the form @T{return @rep{functioncall}} not in the +scope of a to-be-closed variable is called a @def{tail call}. Lua implements @def{proper tail calls} (or @emph{proper tail recursion}): in a tail call, @@ -2049,13 +2046,15 @@ a program can execute. However, a tail call erases any debug information about the calling function. Note that a tail call only happens with a particular syntax, -where the @Rw{return} has one single function call as argument; -this syntax makes the calling function return exactly -the returns of the called function. +where the @Rw{return} has one single function call as argument, +and it is outside the scope of any to-be-closed variable. +This syntax makes the calling function return exactly +the returns of the called function, +without any intervening action. So, none of the following examples are tail calls: @verbatim{ return (f(x)) -- results adjusted to 1 -return 2 * f(x) +return 2 * f(x) -- result multiplied by 2 return x, f(x) -- additional results f(x); return -- results discarded return x or f(x) -- results adjusted to 1 diff --git a/testes/locals.lua b/testes/locals.lua index 24681dd9eb..340af61c0e 100644 --- a/testes/locals.lua +++ b/testes/locals.lua @@ -225,21 +225,24 @@ end do - -- to-be-closed variables must be closed in tail calls + -- calls cannot be tail in the scope of to-be-closed variables local X, Y local function foo () local *toclose _ = function () Y = 10 end - assert(X == 20 and Y == nil) + assert(X == true and Y == nil) -- 'X' not closed yet return 1,2,3 end local function bar () - local *toclose _ = function () X = 20 end - return foo() + local *toclose _ = function () X = false end + X = true + do + return foo() -- not a tail call! + end end local a, b, c, d = bar() - assert(a == 1 and b == 2 and c == 3 and X == 20 and Y == 10 and d == nil) + assert(a == 1 and b == 2 and c == 3 and X == false and Y == 10 and d == nil) end From 46beca5bed8a7700b18100fe48a78373be5055f9 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 10 Dec 2018 13:46:03 -0200 Subject: [PATCH 0362/1145] Better error messages for some polymorphic functions New auxiliary functions/macros 'luaL_argexpected'/'luaL_typeerror' ease the creation of error messages such as bad argument #2 to 'setmetatable' (nil or table expected, got boolean) (The novelty being the "got boolean" part...) --- lauxlib.c | 6 +++--- lauxlib.h | 5 +++++ lbaselib.c | 7 +++---- lcorolib.c | 2 +- ldblib.c | 3 +-- lstrlib.c | 4 ++-- manual/manual.of | 26 ++++++++++++++++++++++++++ 7 files changed, 41 insertions(+), 12 deletions(-) diff --git a/lauxlib.c b/lauxlib.c index fd4acbd109..769586b691 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -185,7 +185,7 @@ LUALIB_API int luaL_argerror (lua_State *L, int arg, const char *extramsg) { } -static int typeerror (lua_State *L, int arg, const char *tname) { +int luaL_typeerror (lua_State *L, int arg, const char *tname) { const char *msg; const char *typearg; /* name for the type of the actual argument */ if (luaL_getmetafield(L, arg, "__name") == LUA_TSTRING) @@ -200,7 +200,7 @@ static int typeerror (lua_State *L, int arg, const char *tname) { static void tag_error (lua_State *L, int arg, int tag) { - typeerror(L, arg, lua_typename(L, tag)); + luaL_typeerror(L, arg, lua_typename(L, tag)); } @@ -339,7 +339,7 @@ LUALIB_API void *luaL_testudata (lua_State *L, int ud, const char *tname) { LUALIB_API void *luaL_checkudata (lua_State *L, int ud, const char *tname) { void *p = luaL_testudata(L, ud, tname); - if (p == NULL) typeerror(L, ud, tname); + luaL_argexpected(L, p != NULL, ud, tname); return p; } diff --git a/lauxlib.h b/lauxlib.h index 9ec0f5317b..e5d378aebe 100644 --- a/lauxlib.h +++ b/lauxlib.h @@ -48,6 +48,7 @@ LUALIB_API int (luaL_getmetafield) (lua_State *L, int obj, const char *e); LUALIB_API int (luaL_callmeta) (lua_State *L, int obj, const char *e); LUALIB_API const char *(luaL_tolstring) (lua_State *L, int idx, size_t *len); LUALIB_API int (luaL_argerror) (lua_State *L, int arg, const char *extramsg); +LUALIB_API int (luaL_typeerror) (lua_State *L, int arg, const char *tname); LUALIB_API const char *(luaL_checklstring) (lua_State *L, int arg, size_t *l); LUALIB_API const char *(luaL_optlstring) (lua_State *L, int arg, @@ -126,6 +127,10 @@ LUALIB_API void (luaL_requiref) (lua_State *L, const char *modname, #define luaL_argcheck(L, cond,arg,extramsg) \ ((void)((cond) || luaL_argerror(L, (arg), (extramsg)))) + +#define luaL_argexpected(L,cond,arg,tname) \ + ((void)((cond) || luaL_typeerror(L, (arg), (tname)))) + #define luaL_checkstring(L,n) (luaL_checklstring(L, (n), NULL)) #define luaL_optstring(L,n,d) (luaL_optlstring(L, (n), (d), NULL)) diff --git a/lbaselib.c b/lbaselib.c index e776c2a25b..201c93e325 100644 --- a/lbaselib.c +++ b/lbaselib.c @@ -125,8 +125,7 @@ static int luaB_getmetatable (lua_State *L) { static int luaB_setmetatable (lua_State *L) { int t = lua_type(L, 2); luaL_checktype(L, 1, LUA_TTABLE); - luaL_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2, - "nil or table expected"); + luaL_argexpected(L, t == LUA_TNIL || t == LUA_TTABLE, 2, "nil or table"); if (luaL_getmetafield(L, 1, "__metatable") != LUA_TNIL) return luaL_error(L, "cannot change a protected metatable"); lua_settop(L, 2); @@ -145,8 +144,8 @@ static int luaB_rawequal (lua_State *L) { static int luaB_rawlen (lua_State *L) { int t = lua_type(L, 1); - luaL_argcheck(L, t == LUA_TTABLE || t == LUA_TSTRING, 1, - "table or string expected"); + luaL_argexpected(L, t == LUA_TTABLE || t == LUA_TSTRING, 1, + "table or string"); lua_pushinteger(L, lua_rawlen(L, 1)); return 1; } diff --git a/lcorolib.c b/lcorolib.c index 9038f9fb5d..34462b5356 100644 --- a/lcorolib.c +++ b/lcorolib.c @@ -20,7 +20,7 @@ static lua_State *getco (lua_State *L) { lua_State *co = lua_tothread(L, 1); - luaL_argcheck(L, co, 1, "thread expected"); + luaL_argexpected(L, co, 1, "thread"); return co; } diff --git a/ldblib.c b/ldblib.c index 200108426e..ada35250c3 100644 --- a/ldblib.c +++ b/ldblib.c @@ -55,8 +55,7 @@ static int db_getmetatable (lua_State *L) { static int db_setmetatable (lua_State *L) { int t = lua_type(L, 2); - luaL_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2, - "nil or table expected"); + luaL_argexpected(L, t == LUA_TNIL || t == LUA_TTABLE, 2, "nil or table"); lua_settop(L, 2); lua_setmetatable(L, 1); return 1; /* return 1st argument */ diff --git a/lstrlib.c b/lstrlib.c index a635e9d4d6..e9c60c0f3e 100644 --- a/lstrlib.c +++ b/lstrlib.c @@ -857,9 +857,9 @@ static int str_gsub (lua_State *L) { lua_Integer n = 0; /* replacement count */ MatchState ms; luaL_Buffer b; - luaL_argcheck(L, tr == LUA_TNUMBER || tr == LUA_TSTRING || + luaL_argexpected(L, tr == LUA_TNUMBER || tr == LUA_TSTRING || tr == LUA_TFUNCTION || tr == LUA_TTABLE, 3, - "string/function/table expected"); + "string/function/table"); luaL_buffinit(L, &b); if (anchor) { p++; lp--; /* skip anchor character */ diff --git a/manual/manual.of b/manual/manual.of index 8b5e5d93aa..0e8e3d72db 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -4979,6 +4979,19 @@ This function never returns. } +@APIEntry{ +void luaL_argexpected (lua_State *L, + int cond, + int arg, + const char *tname);| +@apii{0,0,v} + +Checks whether @id{cond} is true. +If it is not, raises an error about the type of the argument @id{arg} +with a standard message @seeF{luaL_typeerror}. + +} + @APIEntry{typedef struct luaL_Buffer luaL_Buffer;| Type for a @def{string buffer}. @@ -5713,6 +5726,19 @@ to start the traceback. } +@APIEntry{const char *luaL_typeerror (lua_State *L, + int arg, + const char *tname);| +@apii{0,0,v} + +Raises a type error for argument @id{arg} +of the @N{C function} that called it, +using a standard message; +@id{tname} is a @Q{name} for the expected type. +This function never returns. + +} + @APIEntry{const char *luaL_typename (lua_State *L, int index);| @apii{0,0,-} From 51316f9df7aacb54633a3e9b910a070590ac6259 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 11 Dec 2018 11:34:47 -0200 Subject: [PATCH 0363/1145] 'math.rand()' uses higher bits to produce float value The call 'math.rand()' converts the higher bits of the internal unsigned integer random to a float, instead of its lower bits. That ensures that Lua compiled with different float precisions always generates equal (up to the available precision) random numbers when given the same seed. --- lmathlib.c | 58 ++++++++++++++++++++++++++++++++----------------- testes/math.lua | 14 +++++++----- 2 files changed, 46 insertions(+), 26 deletions(-) diff --git a/lmathlib.c b/lmathlib.c index e8e88e7a5b..f2bfff9b14 100644 --- a/lmathlib.c +++ b/lmathlib.c @@ -323,14 +323,18 @@ static Rand64 nextrand (Rand64 *state) { /* ** Convert bits from a random integer into a float in the -** interval [0,1). +** interval [0,1), getting the higher FIG bits from the +** random unsigned integer and converting that to a float. */ -#define maskFIG (~(~(Rand64)1 << (FIGS - 1))) /* use FIGS bits */ -#define shiftFIG \ - (l_mathop(0.5) / ((Rand64)1 << (FIGS - 1))) /* 2^(-FIGS) */ + +/* must throw out the extra (64 - FIGS) bits */ +#define shift64_FIG (64 - FIGS) + +/* to scale to [0, 1), multiply by scaleFIG = 2^(-FIGS) */ +#define scaleFIG (l_mathop(0.5) / ((Rand64)1 << (FIGS - 1))) static lua_Number I2d (Rand64 x) { - return (lua_Number)(x & maskFIG) * shiftFIG; + return (lua_Number)(trim64(x) >> shift64_FIG) * scaleFIG; } /* convert a 'Rand64' to a 'lua_Unsigned' */ @@ -449,35 +453,49 @@ static Rand64 nextrand (Rand64 *state) { /* an unsigned 1 with proper type */ #define UONE ((lu_int32)1) + #if FIGS <= 32 -#define maskHI 0 /* do not need bits from higher half */ -#define maskLOW (~(~UONE << (FIGS - 1))) /* use FIGS bits */ -#define shiftFIG (l_mathop(0.5) / (UONE << (FIGS - 1))) /* 2^(-FIGS) */ +/* 2^(-FIGS) */ +#define scaleFIG (l_mathop(0.5) / (UONE << (FIGS - 1))) + +/* +** get up to 32 bits from higher half, shifting right to +** throw out the extra bits. +*/ +static lua_Number I2d (Rand64 x) { + lua_Number h = (lua_Number)(trim32(x.h) >> (32 - FIGS)); + return h * scaleFIG; +} #else /* 32 < FIGS <= 64 */ /* must take care to not shift stuff by more than 31 slots */ -/* use FIGS - 32 bits from higher half */ -#define maskHI (~(~UONE << (FIGS - 33))) +/* 2^(-FIGS) = 1.0 / 2^30 / 2^3 / 2^(FIGS-33) */ +#define scaleFIG \ + ((lua_Number)1.0 / (UONE << 30) / 8.0 / (UONE << (FIGS - 33))) -/* use 32 bits from lower half */ -#define maskLOW (~(~UONE << 31)) - -/* 2^(-FIGS) == (1 / 2^33) / 2^(FIGS-33) */ -#define shiftFIG ((lua_Number)(1.0 / 8589934592.0) / (UONE << (FIGS - 33))) +/* +** use FIGS - 32 bits from lower half, throwing out the other +** (32 - (FIGS - 32)) = (64 - FIGS) bits +*/ +#define shiftLOW (64 - FIGS) -#endif +/* +** higher 32 bits go after those (FIGS - 32) bits: shiftHI = 2^(FIGS - 32) +*/ +#define shiftHI ((lua_Number)(UONE << (FIGS - 33)) * 2.0) -#define twoto32 l_mathop(4294967296.0) /* 2^32 */ static lua_Number I2d (Rand64 x) { - lua_Number h = (lua_Number)(x.h & maskHI); - lua_Number l = (lua_Number)(x.l & maskLOW); - return (h * twoto32 + l) * shiftFIG; + lua_Number h = (lua_Number)trim32(x.h) * shiftHI; + lua_Number l = (lua_Number)(trim32(x.l) >> shiftLOW); + return (h + l) * scaleFIG; } +#endif + /* convert a 'Rand64' to a 'lua_Unsigned' */ static lua_Unsigned I2UInt (Rand64 x) { diff --git a/testes/math.lua b/testes/math.lua index 7c780e592a..dc5b84f680 100644 --- a/testes/math.lua +++ b/testes/math.lua @@ -823,17 +823,19 @@ do assert(random(0) == res) math.randomseed(1007, 0) - -- using lower bits to generate random floats; (the '% 2^32' converts + -- using higher bits to generate random floats; (the '% 2^32' converts -- 32-bit integers to floats as unsigned) local res if floatbits <= 32 then - -- get all bits from the lower half - res = (l & ~(~0 << floatbits)) % 2^32 + -- get all bits from the higher half + res = (h >> (32 - floatbits)) % 2^32 else - -- get 32 bits from the lower half and the rest from the higher half - res = ((h & ~(~0 << (floatbits - 32))) % 2^32) * 2^32 + (l % 2^32) + -- get 32 bits from the higher half and the rest from the lower half + res = (h % 2^32) * 2^(floatbits - 32) + ((l >> (64 - floatbits)) % 2^32) end - assert(random() * 2^floatbits == res) + local rand = random() + assert(eq(rand, 0x0.7a7040a5a323c9d6, 2^-floatbits)) + assert(rand * 2^floatbits == res) end math.randomseed(0, os.time()) From 3b06f983ae0e57b90cdeb500c84bb524e5c3635b Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 11 Dec 2018 11:54:14 -0200 Subject: [PATCH 0364/1145] Details - in 'luaB_tonumber', do not need to "checkany" when argument is a number. - in 'lua_resume', the call to 'luaD_rawrunprotected' cannot return a status equal to -1. --- lbaselib.c | 2 +- ldo.c | 29 +++++++++++++---------------- 2 files changed, 14 insertions(+), 17 deletions(-) diff --git a/lbaselib.c b/lbaselib.c index 201c93e325..7c0ebfecd3 100644 --- a/lbaselib.c +++ b/lbaselib.c @@ -68,7 +68,6 @@ static const char *b_str2int (const char *s, int base, lua_Integer *pn) { static int luaB_tonumber (lua_State *L) { if (lua_isnoneornil(L, 2)) { /* standard conversion? */ - luaL_checkany(L, 1); if (lua_type(L, 1) == LUA_TNUMBER) { /* already a number? */ lua_settop(L, 1); /* yes; return it */ return 1; @@ -79,6 +78,7 @@ static int luaB_tonumber (lua_State *L) { if (s != NULL && lua_stringtonumber(L, s) == l + 1) return 1; /* successful conversion to number */ /* else not a number */ + luaL_checkany(L, 1); /* (but there must be some parameter) */ } } else { diff --git a/ldo.c b/ldo.c index 2762fefa0b..bdd7fb6db2 100644 --- a/ldo.c +++ b/ldo.c @@ -678,22 +678,19 @@ LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs, L->nny = 0; /* allow yields */ api_checknelems(L, (L->status == LUA_OK) ? nargs + 1 : nargs); status = luaD_rawrunprotected(L, resume, &nargs); - if (unlikely(status == -1)) /* error calling 'lua_resume'? */ - status = LUA_ERRRUN; - else { /* continue running after recoverable errors */ - while (errorstatus(status) && recover(L, status)) { - /* unroll continuation */ - status = luaD_rawrunprotected(L, unroll, &status); - } - if (likely(!errorstatus(status))) - lua_assert(status == L->status); /* normal end or yield */ - else { /* unrecoverable error */ - status = luaF_close(L, L->stack, status); /* close all upvalues */ - L->status = cast_byte(status); /* mark thread as 'dead' */ - luaD_seterrorobj(L, status, L->stack + 1); /* push error message */ - L->ci = &L->base_ci; /* back to the original C level */ - L->ci->top = L->top; - } + /* continue running after recoverable errors */ + while (errorstatus(status) && recover(L, status)) { + /* unroll continuation */ + status = luaD_rawrunprotected(L, unroll, &status); + } + if (likely(!errorstatus(status))) + lua_assert(status == L->status); /* normal end or yield */ + else { /* unrecoverable error */ + status = luaF_close(L, L->stack, status); /* close all upvalues */ + L->status = cast_byte(status); /* mark thread as 'dead' */ + luaD_seterrorobj(L, status, L->stack + 1); /* push error message */ + L->ci = &L->base_ci; /* back to the original C level */ + L->ci->top = L->top; } *nresults = (status == LUA_YIELD) ? L->ci->u2.nyield : cast_int(L->top - (L->ci->func + 1)); From fdc25a1ebfe9968dcec390dd556375105aa0be40 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 13 Dec 2018 13:07:53 -0200 Subject: [PATCH 0365/1145] New functions 'lua_resetthread' and 'coroutine.kill' New functions to reset/kill a thread/coroutine, mainly (only?) to close any pending to-be-closed variable. ('lua_resetthread' also allows a thread to be reused...) --- lcorolib.c | 58 +++++++++++++++++++++++++++++++++++--------- ldo.c | 4 +++ lfunc.c | 18 ++++++++------ lfunc.h | 11 +++++++++ lstate.c | 27 +++++++++++++++++++-- ltests.c | 3 +++ lua.h | 1 + lvm.c | 2 +- manual/manual.of | 34 +++++++++++++++++++++++--- testes/coroutine.lua | 45 ++++++++++++++++++++++++++++++++++ testes/main.lua | 24 ++++++++++++++---- 11 files changed, 195 insertions(+), 32 deletions(-) diff --git a/lcorolib.c b/lcorolib.c index 34462b5356..cdb5fedc93 100644 --- a/lcorolib.c +++ b/lcorolib.c @@ -107,29 +107,40 @@ static int luaB_yield (lua_State *L) { } -static int luaB_costatus (lua_State *L) { - lua_State *co = getco(L); - if (L == co) lua_pushliteral(L, "running"); +#define COS_RUN 0 +#define COS_DEAD 1 +#define COS_YIELD 2 +#define COS_NORM 3 + + +static const char *statname[] = {"running", "dead", "suspended", "normal"}; + + +static int auxstatus (lua_State *L, lua_State *co) { + if (L == co) return COS_RUN; else { switch (lua_status(co)) { case LUA_YIELD: - lua_pushliteral(L, "suspended"); - break; + return COS_YIELD; case LUA_OK: { lua_Debug ar; - if (lua_getstack(co, 0, &ar) > 0) /* does it have frames? */ - lua_pushliteral(L, "normal"); /* it is running */ + if (lua_getstack(co, 0, &ar)) /* does it have frames? */ + return COS_NORM; /* it is running */ else if (lua_gettop(co) == 0) - lua_pushliteral(L, "dead"); + return COS_DEAD; else - lua_pushliteral(L, "suspended"); /* initial state */ - break; + return COS_YIELD; /* initial state */ } default: /* some error occurred */ - lua_pushliteral(L, "dead"); - break; + return COS_DEAD; } } +} + + +static int luaB_costatus (lua_State *L) { + lua_State *co = getco(L); + lua_pushstring(L, statname[auxstatus(L, co)]); return 1; } @@ -147,6 +158,28 @@ static int luaB_corunning (lua_State *L) { } +static int luaB_kill (lua_State *L) { + lua_State *co = getco(L); + int status = auxstatus(L, co); + switch (status) { + case COS_DEAD: case COS_YIELD: { + status = lua_resetthread(co); + if (status == LUA_OK) { + lua_pushboolean(L, 1); + return 1; + } + else { + lua_pushboolean(L, 0); + lua_xmove(co, L, 1); /* copy error message */ + return 2; + } + } + default: /* normal or running coroutine */ + return luaL_error(L, "cannot kill a %s coroutine", statname[status]); + } +} + + static const luaL_Reg co_funcs[] = { {"create", luaB_cocreate}, {"resume", luaB_coresume}, @@ -155,6 +188,7 @@ static const luaL_Reg co_funcs[] = { {"wrap", luaB_cowrap}, {"yield", luaB_yield}, {"isyieldable", luaB_yieldable}, + {"kill", luaB_kill}, {NULL, NULL} }; diff --git a/ldo.c b/ldo.c index bdd7fb6db2..056fef0cae 100644 --- a/ldo.c +++ b/ldo.c @@ -98,6 +98,10 @@ void luaD_seterrorobj (lua_State *L, int errcode, StkId oldtop) { setsvalue2s(L, oldtop, luaS_newliteral(L, "error in error handling")); break; } + case CLOSEPROTECT: { + setnilvalue(s2v(oldtop)); /* no error message */ + break; + } default: { setobjs2s(L, oldtop, L->top - 1); /* error message on current top */ break; diff --git a/lfunc.c b/lfunc.c index 11d2850f76..bdf3cd2598 100644 --- a/lfunc.c +++ b/lfunc.c @@ -127,17 +127,18 @@ static int prepclosingmethod (lua_State *L, TValue *obj, TValue *err) { /* -** Prepare and call a closing method. If status is OK, code is -** still inside the original protected call, and so any error -** will be handled there. Otherwise, a previous error already -** activated original protected call, and so the call to the -** closing method must be protected here. +** Prepare and call a closing method. If status is OK, code is still +** inside the original protected call, and so any error will be handled +** there. Otherwise, a previous error already activated original +** protected call, and so the call to the closing method must be +** protected here. (A status = CLOSEPROTECT behaves like a previous +** error, to also run the closing method in protected mode). ** If status is OK, the call to the closing method will be pushed ** at the top of the stack. Otherwise, values are pushed after ** the 'level' of the upvalue being closed, as everything after ** that won't be used again. */ -static int closeupval (lua_State *L, TValue *uv, StkId level, int status) { +static int callclosemth (lua_State *L, TValue *uv, StkId level, int status) { if (likely(status == LUA_OK)) { if (prepclosingmethod(L, uv, &G(L)->nilvalue)) /* something to call? */ callclose(L, NULL); /* call closing method */ @@ -207,9 +208,10 @@ int luaF_close (lua_State *L, StkId level, int status) { if (!iswhite(uv)) gray2black(uv); /* closed upvalues cannot be gray */ luaC_barrier(L, uv, slot); - if (status >= 0 && uv->tt == LUA_TUPVALTBC) { /* must be closed? */ + if (uv->tt == LUA_TUPVALTBC && status != NOCLOSINGMETH) { + /* must run closing method */ ptrdiff_t levelrel = savestack(L, level); - status = closeupval(L, uv->v, upl, status); /* may realloc. the stack */ + status = callclosemth(L, uv->v, upl, status); /* may change the stack */ level = restorestack(L, levelrel); } } diff --git a/lfunc.h b/lfunc.h index c9fe13148a..0ed79c48ab 100644 --- a/lfunc.h +++ b/lfunc.h @@ -42,6 +42,17 @@ #define MAXMISS 10 +/* +** Special "status" for 'luaF_close' +*/ + +/* close upvalues without running their closing methods */ +#define NOCLOSINGMETH (-1) + +/* close upvalues running all closing methods in protected mode */ +#define CLOSEPROTECT (-2) + + LUAI_FUNC Proto *luaF_newproto (lua_State *L); LUAI_FUNC CClosure *luaF_newCclosure (lua_State *L, int nelems); LUAI_FUNC LClosure *luaF_newLclosure (lua_State *L, int nelems); diff --git a/lstate.c b/lstate.c index 9d39995964..5ee024fcab 100644 --- a/lstate.c +++ b/lstate.c @@ -258,7 +258,7 @@ static void preinit_thread (lua_State *L, global_State *g) { static void close_state (lua_State *L) { global_State *g = G(L); - luaF_close(L, L->stack, -1); /* close all upvalues for this thread */ + luaF_close(L, L->stack, CLOSEPROTECT); /* close all upvalues */ luaC_freeallobjects(L); /* collect all objects */ if (ttisnil(&g->nilvalue)) /* closing a fully built state? */ luai_userstateclose(L); @@ -301,7 +301,7 @@ LUA_API lua_State *lua_newthread (lua_State *L) { void luaE_freethread (lua_State *L, lua_State *L1) { LX *l = fromstate(L1); - luaF_close(L1, L1->stack, -1); /* close all upvalues for this thread */ + luaF_close(L1, L1->stack, NOCLOSINGMETH); /* close all upvalues */ lua_assert(L1->openupval == NULL); luai_userstatefree(L, L1); freestack(L1); @@ -309,6 +309,29 @@ void luaE_freethread (lua_State *L, lua_State *L1) { } +int lua_resetthread (lua_State *L) { + CallInfo *ci; + int status; + lua_lock(L); + ci = &L->base_ci; + status = luaF_close(L, L->stack, CLOSEPROTECT); + setnilvalue(s2v(L->stack)); /* 'function' entry for basic 'ci' */ + if (status != CLOSEPROTECT) /* real errors? */ + luaD_seterrorobj(L, status, L->stack + 1); + else { + status = LUA_OK; + L->top = L->stack + 1; + } + ci->callstatus = CIST_C; + ci->func = L->stack; + ci->top = L->top + LUA_MINSTACK; + L->ci = ci; + L->status = status; + lua_unlock(L); + return status; +} + + LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { int i; lua_State *L; diff --git a/ltests.c b/ltests.c index 63d423e08e..a38a8926f0 100644 --- a/ltests.c +++ b/ltests.c @@ -1366,6 +1366,9 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) { else if EQ("newthread") { lua_newthread(L1); } + else if EQ("resetthread") { + lua_pushinteger(L1, lua_resetthread(L1)); + } else if EQ("newuserdata") { lua_newuserdata(L1, getnum); } diff --git a/lua.h b/lua.h index 6aa184d18b..95ce5a2e9b 100644 --- a/lua.h +++ b/lua.h @@ -147,6 +147,7 @@ extern const char lua_ident[]; LUA_API lua_State *(lua_newstate) (lua_Alloc f, void *ud); LUA_API void (lua_close) (lua_State *L); LUA_API lua_State *(lua_newthread) (lua_State *L); +LUA_API int (lua_resetthread) (lua_State *L); LUA_API lua_CFunction (lua_atpanic) (lua_State *L, lua_CFunction panicf); diff --git a/lvm.c b/lvm.c index fc8722a84f..652095dcd0 100644 --- a/lvm.c +++ b/lvm.c @@ -1565,7 +1565,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { if (nparams1) /* vararg function? */ delta = ci->u.l.nextraargs + nparams1; /* close upvalues from current call */ - luaF_close(L, base, -1); /* (no to-be-closed vars. here) */ + luaF_close(L, base, LUA_OK); updatestack(ci); } if (!ttisfunction(s2v(ra))) { /* not a function? */ diff --git a/manual/manual.of b/manual/manual.of index 0e8e3d72db..862d032bcd 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -3927,6 +3927,19 @@ and then pops the top element. } +@APIEntry{int lua_resetthread (lua_State *L);| +@apii{0,?,-} + +Resets a thread, cleaning its call stack and closing all pending +to-be-closed variables. +Returns a status code: +@Lid{LUA_OK} for no errors in closing methods, +or an error status otherwise. +In case of error, +leave the error object on the stack, + +} + @APIEntry{int lua_resume (lua_State *L, lua_State *from, int nargs, int *nresults);| @apii{?,?,-} @@ -3948,11 +3961,8 @@ or returned by the body function. @Lid{LUA_OK} if the coroutine finishes its execution without errors, or an error code in case of errors @seeC{lua_pcall}. - In case of errors, -the stack is not unwound, -so you can use the debug API over it. -The error object is on the top of the stack. +the error object is on the top of the stack. To resume a coroutine, you remove all results from the last @Lid{lua_yield}, @@ -6285,6 +6295,17 @@ it is not inside a non-yieldable @N{C function}. } +@LibEntry{coroutine.kill(co)| + +Kills coroutine @id{co}, +closing all its pending to-be-closed variables +and putting the coroutine in a dead state. +In case of error closing some variable, +returns @false plus the error object; +otherwise returns @true. + +} + @LibEntry{coroutine.resume (co [, val1, @Cdots])| Starts or continues the execution of coroutine @id{co}. @@ -8648,6 +8669,11 @@ has been removed. When needed, this metamethod must be explicitly defined. } +@item{ +When a coroutine finishes with an error, +its stack is unwound (to run any pending closing methods). +} + } } diff --git a/testes/coroutine.lua b/testes/coroutine.lua index 7d42eadde0..5674a4dd14 100644 --- a/testes/coroutine.lua +++ b/testes/coroutine.lua @@ -119,6 +119,51 @@ end assert(#a == 25 and a[#a] == 97) x, a = nil + +-- coroutine kill +do + -- ok to kill a dead coroutine + local co = coroutine.create(print) + assert(coroutine.resume(co, "testing 'coroutine.kill'")) + assert(coroutine.status(co) == "dead") + assert(coroutine.kill(co)) + + -- cannot kill the running coroutine + local st, msg = pcall(coroutine.kill, coroutine.running()) + assert(not st and string.find(msg, "running")) + + local main = coroutine.running() + + -- cannot kill a "normal" coroutine + ;(coroutine.wrap(function () + local st, msg = pcall(coroutine.kill, main) + assert(not st and string.find(msg, "normal")) + end))() + + -- to-be-closed variables in coroutines + local X + co = coroutine.create(function () + local *toclose x = function (err) assert(err == nil); X = false end + X = true + coroutine.yield() + end) + coroutine.resume(co) + assert(X) + assert(coroutine.kill(co)) + assert(not X and coroutine.status(co) == "dead") + + -- error killing a coroutine + co = coroutine.create(function() + local *toclose x = function (err) assert(err == nil); error(111) end + coroutine.yield() + end) + coroutine.resume(co) + local st, msg = coroutine.kill(co) + assert(not st and coroutine.status(co) == "dead" and msg == 111) + +end + + -- yielding across C boundaries co = coroutine.wrap(function() diff --git a/testes/main.lua b/testes/main.lua index c7bde0d962..b9dcab1c12 100644 --- a/testes/main.lua +++ b/testes/main.lua @@ -254,15 +254,15 @@ NoRun("error object is a table value", [[lua %s]], prog) -- chunk broken in many lines -s = [=[ -- -function f ( x ) +s = [=[ -- +function f ( x ) local a = [[ xuxu ]] local b = "\ xuxu\n" if x == 11 then return 1 + 12 , 2 + 20 end --[[ test multiple returns ]] - return x + 1 + return x + 1 --\\ end return( f( 100 ) ) @@ -272,10 +272,10 @@ s = string.gsub(s, ' ', '\n\n') -- change all spaces for newlines prepfile(s) RUN([[lua -e"_PROMPT='' _PROMPT2=''" -i < %s > %s]], prog, out) checkprogout("101\n13\t22\n\n") - + prepfile[[#comment in 1st line without \n at the end]] RUN('lua %s', prog) - + prepfile[[#test line number when file starts with comment line debug = require"debug" print(debug.getinfo(1).currentline) @@ -306,6 +306,20 @@ NoRun("", "lua %s", prog) -- no message prepfile("os.exit(false, true)") NoRun("", "lua %s", prog) -- no message + +-- to-be-closed variables in main chunk +prepfile[[ + local *toclose x = function (err) + assert(err == 120) + print("Ok") + end + local *toclose e1 = function () error(120) end + os.exit(true, true) +]] +RUN('lua %s > %s', prog, out) +checkprogout("Ok") + + -- remove temporary files assert(os.remove(prog)) assert(os.remove(otherprog)) From 57f5b81da9f1f23380d20f164012e10c5f4fef94 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 14 Dec 2018 13:12:01 -0200 Subject: [PATCH 0366/1145] Bug: Long brackets with a huge number of '=' causes overflow A long bracket with too many equal signs can overflow the 'int' used for the counting and some arithmetic done on the value. Changing the counter to 'size_t' avoids that. (Because what is counted goes to a buffer, an overflow in the counter will first raise a buffer-overflow error.) --- bugs | 19 +++++++++++++++++++ llex.c | 30 ++++++++++++++++-------------- 2 files changed, 35 insertions(+), 14 deletions(-) diff --git a/bugs b/bugs index d7a717c38f..a965025b66 100644 --- a/bugs +++ b/bugs @@ -4017,6 +4017,25 @@ patch = [[ +--[=[ +Bug{ +what = [[Long brackets with a huge number of '=' overflow some +internal buffer arithmetic]], +report = [[Marco, 2018/12/12]], +since = [[5.1]], +fix = nil, +example = [[ +local eqs = string.rep("=", 0x3ffffffe) +local code = "return [" .. eqs .. "[a]" .. eqs .. "]" +print(#assert(load(code))()) +]], +patch = [[ +]] +} +]=] + + + --[=[ Bug{ diff --git a/llex.c b/llex.c index 4a25607cf3..38c6d92d03 100644 --- a/llex.c +++ b/llex.c @@ -244,12 +244,12 @@ static int read_numeral (LexState *ls, SemInfo *seminfo) { /* -** skip a sequence '[=*[' or ']=*]'; if sequence is well formed, return -** its number of '='s; otherwise, return a negative number (-1 iff there -** are no '='s after initial bracket) +** reads a sequence '[=*[' or ']=*]', leaving the last bracket. +** If sequence is well formed, return its number of '='s + 2; otherwise, +** return 1 if there is no '='s or 0 otherwise (an unfinished '[==...'). */ -static int skip_sep (LexState *ls) { - int count = 0; +static size_t skip_sep (LexState *ls) { + size_t count = 0; int s = ls->current; lua_assert(s == '[' || s == ']'); save_and_next(ls); @@ -257,11 +257,13 @@ static int skip_sep (LexState *ls) { save_and_next(ls); count++; } - return (ls->current == s) ? count : (-count) - 1; + return (ls->current == s) ? count + 2 + : (count == 0) ? 1 + : 0; } -static void read_long_string (LexState *ls, SemInfo *seminfo, int sep) { +static void read_long_string (LexState *ls, SemInfo *seminfo, size_t sep) { int line = ls->linenumber; /* initial line (for error message) */ save_and_next(ls); /* skip 2nd '[' */ if (currIsNewline(ls)) /* string starts with a newline? */ @@ -295,8 +297,8 @@ static void read_long_string (LexState *ls, SemInfo *seminfo, int sep) { } } endloop: if (seminfo) - seminfo->ts = luaX_newstring(ls, luaZ_buffer(ls->buff) + (2 + sep), - luaZ_bufflen(ls->buff) - 2*(2 + sep)); + seminfo->ts = luaX_newstring(ls, luaZ_buffer(ls->buff) + sep, + luaZ_bufflen(ls->buff) - 2 * sep); } @@ -444,9 +446,9 @@ static int llex (LexState *ls, SemInfo *seminfo) { /* else is a comment */ next(ls); if (ls->current == '[') { /* long comment? */ - int sep = skip_sep(ls); + size_t sep = skip_sep(ls); luaZ_resetbuffer(ls->buff); /* 'skip_sep' may dirty the buffer */ - if (sep >= 0) { + if (sep >= 2) { read_long_string(ls, NULL, sep); /* skip long comment */ luaZ_resetbuffer(ls->buff); /* previous call may dirty the buff. */ break; @@ -458,12 +460,12 @@ static int llex (LexState *ls, SemInfo *seminfo) { break; } case '[': { /* long string or simply '[' */ - int sep = skip_sep(ls); - if (sep >= 0) { + size_t sep = skip_sep(ls); + if (sep >= 2) { read_long_string(ls, seminfo, sep); return TK_STRING; } - else if (sep != -1) /* '[=...' missing second bracket */ + else if (sep == 0) /* '[=...' missing second bracket? */ lexerror(ls, "invalid long string delimiter", TK_STRING); return '['; } From 2258f3133b27a6a3db703644311e0a59b6c7b0f6 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 14 Dec 2018 13:49:02 -0200 Subject: [PATCH 0367/1145] Added file 'testes/heavy.lua' This file is not part of the regular tests. It tests error conditions that demand too much memory or too much time to create: * string with too many characters * control structure with body too large * chunk with too many lines * identifier with too many characters * chunks with too many instructions * function with too many constants * too many strings internalized * table with too many entries In machines with limited memory (less than 150 GB), many tests run up to a "not enough memory" error. We need some memory (~256 GB) to run all tests up to their intrinsic limits. --- testes/heavy.lua | 173 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 173 insertions(+) create mode 100644 testes/heavy.lua diff --git a/testes/heavy.lua b/testes/heavy.lua new file mode 100644 index 0000000000..4731c7472f --- /dev/null +++ b/testes/heavy.lua @@ -0,0 +1,173 @@ +-- $Id: heavy.lua,v 1.7 2017/12/29 15:42:15 roberto Exp $ +-- See Copyright Notice in file all.lua + +local function teststring () + print("creating a string too long") + do + local a = "x" + local st, msg = pcall(function () + while true do + a = a .. a.. a.. a.. a.. a.. a.. a.. a.. a + .. a .. a.. a.. a.. a.. a.. a.. a.. a.. a + .. a .. a.. a.. a.. a.. a.. a.. a.. a.. a + .. a .. a.. a.. a.. a.. a.. a.. a.. a.. a + .. a .. a.. a.. a.. a.. a.. a.. a.. a.. a + .. a .. a.. a.. a.. a.. a.. a.. a.. a.. a + .. a .. a.. a.. a.. a.. a.. a.. a.. a.. a + .. a .. a.. a.. a.. a.. a.. a.. a.. a.. a + .. a .. a.. a.. a.. a.. a.. a.. a.. a.. a + .. a .. a.. a.. a.. a.. a.. a.. a.. a.. a + print(string.format("string with %d bytes", #a)) + end + end) + assert(not st and + (string.find(msg, "string length overflow") or + string.find(msg, "not enough memory"))) + print("string length overflow with " .. #a * 100) + end + print('+') +end + +local function loadrep (x, what) + local p = 1<<20 + local s = string.rep(x, p) + local count = 0 + local function f() + count = count + p + if count % (0x80*p) == 0 then + io.stderr:write("(", count // 2^20, " M)") + end + return s + end + local st, msg = load(f, "=big") + print("\nmemory: ", collectgarbage'count' * 1024) + msg = string.match(msg, "^[^\n]+") -- get only first line + print(string.format("total: 0x%x %s ('%s')", count, what, msg)) + return st, msg +end + + +function controlstruct () + print("control structure too long") + local lim = ((1 << 24) - 2) // 3 + local s = string.rep("a = a + 1\n", lim) + s = "while true do " .. s .. "end" + assert(load(s)) + print("ok with " .. lim .. " lines") + lim = lim + 3 + s = string.rep("a = a + 1\n", lim) + s = "while true do " .. s .. "end" + local st, msg = load(s) + assert(not st and string.find(msg, "too long")) + print(msg) +end + + +function manylines () + print("loading chunk with too many lines") + local st, msg = loadrep("\n", "lines") + assert(not st and string.find(msg, "too many lines")) + print('+') +end + + +function hugeid () + print("loading chunk with huge identifier") + local st, msg = loadrep("a", "chars") + assert(not st and + (string.find(msg, "lexical element too long") or + string.find(msg, "not enough memory"))) + print('+') +end + +function toomanyinst () + print("loading chunk with too many instructions") + local st, msg = loadrep("a = 10; ", "instructions") + print('+') +end + + +local function loadrepfunc (prefix, f) + local count = -1 + local function aux () + count = count + 1 + if count == 0 then + return prefix + else + if count % (0x100000) == 0 then + io.stderr:write("(", count // 2^20, " M)") + end + return f(count) + end + end + local st, msg = load(aux, "k") + print("\nmemory: ", collectgarbage'count' * 1024) + msg = string.match(msg, "^[^\n]+") -- get only first line + print("expected error: ", msg) +end + + +function toomanyconst () + print("loading function with too many constants") + loadrepfunc("function foo () return {0,", + function (n) + -- convert 'n' to a string in the format [["...",]], + -- where '...' is a kind of number in base 128 + -- (in a range that does not include either the double quote + -- and the escape.) + return string.char(34, + ((n // 128^0) & 127) + 128, + ((n // 128^1) & 127) + 128, + ((n // 128^2) & 127) + 128, + ((n // 128^3) & 127) + 128, + ((n // 128^4) & 127) + 128, + 34, 44) + end) +end + + +function toomanystr () + local a = {} + local st, msg = pcall(function () + for i = 1, math.huge do + if i % (0x100000) == 0 then + io.stderr:write("(", i // 2^20, " M)") + end + a[i] = string.pack("I", i) + end + end) + local size = #a + a = collectgarbage'count' + print("\nmemory:", a * 1024) + print("expected error:", msg) + print("size:", size) +end + + +function toomanyidx () + local a = {} + local st, msg = pcall(function () + for i = 1, math.huge do + if i % (0x100000) == 0 then + io.stderr:write("(", i // 2^20, " M)") + end + a[i] = i + end + end) + print("\nmemory: ", collectgarbage'count' * 1024) + print("expected error: ", msg) + print("size:", #a) +end + + + +-- teststring() +-- controlstruct() +-- manylines() +-- hugeid() +-- toomanyinst() +-- toomanyconst() +-- toomanystr() +toomanyidx() + +print "OK" From af6d9f31165a13c34c0601f37ca5a67c365d1d01 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 17 Dec 2018 13:56:22 -0200 Subject: [PATCH 0368/1145] Details A few details in the makefile and in the manual. (In particular, it updates the dependency lists in the makefile.) --- ltests.c | 3 ++- makefile | 23 ++++++++++++----------- manual/manual.of | 16 ++++++++-------- 3 files changed, 22 insertions(+), 20 deletions(-) diff --git a/ltests.c b/ltests.c index a38a8926f0..0b5ed90b22 100644 --- a/ltests.c +++ b/ltests.c @@ -156,7 +156,8 @@ void *debug_realloc (void *ud, void *b, size_t oldsize, size_t size) { size_t realsize = sizeof(Header) + size + MARKSIZE; if (realsize < size) return NULL; /* arithmetic overflow! */ newblock = cast(Header *, malloc(realsize)); /* alloc a new block */ - if (newblock == NULL) return NULL; /* really out of memory? */ + if (newblock == NULL) + return NULL; /* really out of memory? */ if (block) { memcpy(newblock + 1, block + 1, commonsize); /* copy old contents */ freeblock(mc, block); /* erase (and check) old copy */ diff --git a/makefile b/makefile index 1263f7fdcf..02a0814fb7 100644 --- a/makefile +++ b/makefile @@ -14,10 +14,11 @@ CWARNSCPP= \ -Wredundant-decls \ -Wdisabled-optimization \ -Wdouble-promotion \ - -Wstrict-aliasing=3 \ - -Wno-aggressive-loop-optimizations \ - -Wlogical-op \ - -Werror \ + #-Wno-aggressive-loop-optimizations \ + #-Wlogical-op \ + #-Wfatal-errors \ + #-Wstrict-aliasing=3 \ + # -Werror \ # -pedantic # warns if we use jump tables \ # the next warnings generate too much noise, so they are disabled # -Wconversion -Wno-sign-conversion \ @@ -39,6 +40,7 @@ CWARNS= $(CWARNSCPP) $(CWARNSC) # -DEXTERNMEMCHECK -DHARDSTACKTESTS -DHARDMEMTESTS -DTRACEMEM='"tempmem"' +# -DMAXINDEXRK=1 # -g -DLUA_USER_H='"ltests.h"' # -pg -malign-double # -DLUA_USE_CTYPE -DLUA_USE_APICHECK @@ -50,7 +52,6 @@ TESTS= -DLUA_USER_H='"ltests.h"' -O0 # LOCAL = $(TESTS) $(CWARNS) -g - # enable Linux goodies MYCFLAGS= $(LOCAL) -std=c99 -DLUA_USE_LINUX -DLUA_USE_READLINE MYLDFLAGS= $(LOCAL) -Wl,-E @@ -58,7 +59,7 @@ MYLIBS= -ldl -lreadline CC= gcc -CFLAGS= -Wall -O2 $(MYCFLAGS) -Wfatal-errors -fno-stack-protector -fno-common +CFLAGS= -Wall -O2 $(MYCFLAGS) -fno-stack-protector -fno-common -march=native AR= ar rc RANLIB= ranlib RM= rm -f @@ -122,7 +123,7 @@ echo: @echo "MYLIBS = $(MYLIBS)" @echo "DL = $(DL)" -$(ALL_O): makefile +$(ALL_O): makefile ltests.h # DO NOT EDIT # automatically made with 'gcc -MM l*.c' @@ -146,8 +147,8 @@ ldo.o: ldo.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \ lparser.h lstring.h ltable.h lundump.h lvm.h ldump.o: ldump.c lprefix.h lua.h luaconf.h lobject.h llimits.h lstate.h \ ltm.h lzio.h lmem.h lundump.h -lfunc.o: lfunc.c lprefix.h lua.h luaconf.h lfunc.h lobject.h llimits.h \ - lgc.h lstate.h ltm.h lzio.h lmem.h +lfunc.o: lfunc.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \ + llimits.h ltm.h lzio.h lmem.h ldo.h lfunc.h lgc.h lgc.o: lgc.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \ llimits.h ltm.h lzio.h lmem.h ldo.h lfunc.h lgc.h lstring.h ltable.h linit.o: linit.c lprefix.h lua.h luaconf.h lualib.h lauxlib.h @@ -178,8 +179,8 @@ ltable.o: ltable.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \ ltablib.o: ltablib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h ltests.o: ltests.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \ lobject.h ltm.h lzio.h lmem.h lauxlib.h lcode.h llex.h lopcodes.h \ - lparser.h lctype.h ldebug.h ldo.h lfunc.h lstring.h lgc.h ltable.h \ - lualib.h + lparser.h lctype.h ldebug.h ldo.h lfunc.h lopnames.h lstring.h lgc.h \ + ltable.h lualib.h ltm.o: ltm.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \ llimits.h ltm.h lzio.h lmem.h ldo.h lgc.h lstring.h ltable.h lvm.h lua.o: lua.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h diff --git a/manual/manual.of b/manual/manual.of index 862d032bcd..044bd09c90 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -1027,8 +1027,7 @@ Any kind of end-of-line sequence or newline followed by carriage return) is converted to a simple newline. -For convenience, -when the opening long bracket is immediately followed by a newline, +When the opening long bracket is immediately followed by a newline, the newline is not included in the string. As an example, in a system using ASCII (in which @Char{a} is coded @N{as 97}, @@ -1048,7 +1047,7 @@ alo Any byte in a literal string not explicitly affected by the previous rules represents itself. However, Lua opens files for parsing in text mode, -and the system file functions may have problems with +and the system's file functions may have problems with some control characters. So, it is safer to represent non-text data as a quoted literal with @@ -1782,19 +1781,19 @@ These operators always result in @false or @true. Equality (@T{==}) first compares the type of its operands. If the types are different, then the result is @false. Otherwise, the values of the operands are compared. -Strings are compared in the obvious way. +Strings are equal if they have the same content. Numbers are equal if they denote the same mathematical value. Tables, userdata, and threads are compared by reference: two objects are considered equal only if they are the same object. Every time you create a new object -(a table, userdata, or thread), +(a table, a userdata, or a thread), this new object is different from any previously existing object. -A closure is always equal to itself. -Closures with any detectable difference +A function is always equal to itself. +Functions with any detectable difference (different behavior, different definition) are always different. -Closures created at different times but with no detectable differences +Functions created at different times but with no detectable differences may be classified as equal or not (depending on internal cashing details). @@ -4324,6 +4323,7 @@ The unsigned version of @Lid{lua_Integer}. Returns the pseudo-index that represents the @id{i}-th upvalue of the running function @see{c-closure}. +@id{i} must be in the range @M{[1,256]}. } From 5d7dec552044303abc0d469d430c1fbec3c7b635 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 17 Dec 2018 15:19:56 -0200 Subject: [PATCH 0369/1145] Added directory 'testes/libs/P1' to the repository This directory is used for some tests. As standard Lua has no command to create directories, it must be present before running tests. --- testes/libs/P1/dummy | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 testes/libs/P1/dummy diff --git a/testes/libs/P1/dummy b/testes/libs/P1/dummy new file mode 100644 index 0000000000..b0468a0a3f --- /dev/null +++ b/testes/libs/P1/dummy @@ -0,0 +1,2 @@ +# This is a dummy file just to make git keep the otherwise empty +# directory 'P1' in the repository. From 662506476b3b9bf651de064884a00c8dbce6e281 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 19 Dec 2018 13:15:14 -0200 Subject: [PATCH 0370/1145] 'all' script automatically 'make's everything The script 'all', to run all tests, automatically ensures that the Lua interpreter and the test C libraries (in 'testes/libs/') are updated with any changes in 'luaconf.h'. --- all | 4 +++- makefile | 1 + testes/libs/makefile | 11 ++++++----- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/all b/all index 12acaf3650..8f78ee4d5b 100755 --- a/all +++ b/all @@ -1,4 +1,6 @@ -cd testes +make -s -j +cd testes/libs; make -s +cd .. # back to directory 'testes' ulimit -S -s 2000 if { ../lua all.lua; } then echo -e "\n\n final OK!!!!\n\n" diff --git a/makefile b/makefile index 02a0814fb7..3bd319bea9 100644 --- a/makefile +++ b/makefile @@ -90,6 +90,7 @@ ALL_O= $(CORE_O) $(LUA_O) $(LUAC_O) $(AUX_O) $(LIB_O) ALL_A= $(CORE_T) all: $(ALL_T) + touch all o: $(ALL_O) diff --git a/testes/libs/makefile b/testes/libs/makefile index 9925fb005b..acff4848c2 100644 --- a/testes/libs/makefile +++ b/testes/libs/makefile @@ -9,18 +9,19 @@ CFLAGS = -Wall -std=gnu99 -O2 -I$(LUA_DIR) -fPIC -shared # libraries used by the tests all: lib1.so lib11.so lib2.so lib21.so lib2-v2.so + touch all -lib1.so: lib1.c +lib1.so: lib1.c $(LUA_DIR)/luaconf.h $(LUA_DIR)/ltests.h $(CC) $(CFLAGS) -o lib1.so lib1.c -lib11.so: lib11.c +lib11.so: lib11.c $(LUA_DIR)/luaconf.h $(LUA_DIR)/ltests.h $(CC) $(CFLAGS) -o lib11.so lib11.c -lib2.so: lib2.c +lib2.so: lib2.c $(LUA_DIR)/luaconf.h $(LUA_DIR)/ltests.h $(CC) $(CFLAGS) -o lib2.so lib2.c -lib21.so: lib21.c +lib21.so: lib21.c $(LUA_DIR)/luaconf.h $(LUA_DIR)/ltests.h $(CC) $(CFLAGS) -o lib21.so lib21.c lib2-v2.so: lib2.so - mv lib2.so ./lib2-v2.so + cp lib2.so ./lib2-v2.so From da37ac9c7894186a0e2e0e6f1f5f00b825fd1555 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 27 Dec 2018 14:19:53 -0200 Subject: [PATCH 0371/1145] Detail Slightly better error message for invalid conversions in 'string.format'. --- lstrlib.c | 3 +-- testes/strings.lua | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/lstrlib.c b/lstrlib.c index e9c60c0f3e..dde868c082 100644 --- a/lstrlib.c +++ b/lstrlib.c @@ -1180,8 +1180,7 @@ static int str_format (lua_State *L) { break; } default: { /* also treat cases 'pnLlh' */ - return luaL_error(L, "invalid option '%%%c' to 'format'", - *(strfrmt - 1)); + return luaL_error(L, "invalid conversion '%s' to 'format'", form); } } lua_assert(nb < MAX_ITEM); diff --git a/testes/strings.lua b/testes/strings.lua index 1260dbcc26..587a0e06d3 100644 --- a/testes/strings.lua +++ b/testes/strings.lua @@ -302,7 +302,7 @@ check("%100.3d", "too long") check("%1"..aux..".3d", "too long") check("%1.100d", "too long") check("%10.1"..aux.."004d", "too long") -check("%t", "invalid option") +check("%t", "invalid conversion") check("%"..aux.."d", "repeated flags") check("%d %d", "no value") From ba7da13ec5938f978c37d63aa40a3e340b301f79 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 27 Dec 2018 14:32:29 -0200 Subject: [PATCH 0372/1145] Changes in the control of C-stack overflow * unification of the 'nny' and 'nCcalls' counters; * external C functions ('lua_CFunction') count more "slots" in the C stack (to allow for their possible use of buffers) * added a new test script specific for C-stack overflows. (Most of those tests were already present, but concentrating them in a single script easies the task of checking whether 'LUAI_MAXCCALLS' is adequate in a system.) --- lapi.c | 4 +-- ldo.c | 40 ++++++++++++---------------- llimits.h | 6 +++-- lparser.c | 8 +++--- lstate.c | 49 ++++++++++++++++++++++------------ lstate.h | 52 ++++++++++++++++++++++++++++++------- ltests.h | 2 +- luaconf.h | 4 +-- testes/all.lua | 1 + testes/coroutine.lua | 4 +-- testes/cstack.lua | 62 ++++++++++++++++++++++++++++++++++++++++++++ testes/pm.lua | 12 --------- 12 files changed, 170 insertions(+), 74 deletions(-) create mode 100644 testes/cstack.lua diff --git a/lapi.c b/lapi.c index 9cabe7ca4e..2d10bb73f7 100644 --- a/lapi.c +++ b/lapi.c @@ -956,7 +956,7 @@ LUA_API void lua_callk (lua_State *L, int nargs, int nresults, api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread"); checkresults(L, nargs, nresults); func = L->top - (nargs+1); - if (k != NULL && L->nny == 0) { /* need to prepare continuation? */ + if (k != NULL && yieldable(L)) { /* need to prepare continuation? */ L->ci->u.c.k = k; /* save continuation */ L->ci->u.c.ctx = ctx; /* save context */ luaD_call(L, func, nresults); /* do the call */ @@ -1004,7 +1004,7 @@ LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc, func = savestack(L, o); } c.func = L->top - (nargs+1); /* function to be called */ - if (k == NULL || L->nny > 0) { /* no continuation or no yieldable? */ + if (k == NULL || !yieldable(L)) { /* no continuation or no yieldable? */ c.nresults = nresults; /* do a 'conventional' protected call */ status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func); } diff --git a/ldo.c b/ldo.c index 056fef0cae..f8d8f11cfb 100644 --- a/ldo.c +++ b/ldo.c @@ -138,7 +138,7 @@ l_noret luaD_throw (lua_State *L, int errcode) { int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) { - unsigned short oldnCcalls = L->nCcalls - L->nci; + l_uint32 oldnCcalls = L->nCcalls - L->nci; struct lua_longjmp lj; lua_assert(L->nCcalls >= L->nci); lj.status = LUA_OK; @@ -513,12 +513,17 @@ void luaD_call (lua_State *L, StkId func, int nresults) { /* -** Similar to 'luaD_call', but does not allow yields during the call +** Similar to 'luaD_call', but does not allow yields during the call. +** If there is a stack overflow, freeing all CI structures will +** force the subsequent call to invoke 'luaE_extendCI', which then +** will raise any errors. */ void luaD_callnoyield (lua_State *L, StkId func, int nResults) { - L->nny++; + incXCcalls(L); + if (getCcalls(L) >= LUAI_MAXCCALLS) /* possible stack overflow? */ + luaE_freeCI(L); luaD_call(L, func, nResults); - L->nny--; + decXCcalls(L); } @@ -530,7 +535,7 @@ static void finishCcall (lua_State *L, int status) { CallInfo *ci = L->ci; int n; /* must have a continuation and must be able to call it */ - lua_assert(ci->u.c.k != NULL && L->nny == 0); + lua_assert(ci->u.c.k != NULL && yieldable(L)); /* error status can only happen in a protected call */ lua_assert((ci->callstatus & CIST_YPCALL) || status == LUA_YIELD); if (ci->callstatus & CIST_YPCALL) { /* was inside a pcall? */ @@ -601,7 +606,6 @@ static int recover (lua_State *L, int status) { luaD_seterrorobj(L, status, oldtop); L->ci = ci; L->allowhook = getoah(ci->callstatus); /* restore original 'allowhook' */ - L->nny = 0; /* should be zero to be yieldable */ luaD_shrinkstack(L); L->errfunc = ci->u.c.old_errfunc; return 1; /* continue running the coroutine */ @@ -622,13 +626,6 @@ static int resume_error (lua_State *L, const char *msg, int narg) { } -/* -** "Cost" in the C stack for a coroutine invocation. -*/ -#if !defined(LUAL_COROCSTK) -#define LUAL_COROCSTK 3 -#endif - /* ** Do the work for 'lua_resume' in protected mode. Most of the work ** depends on the status of the coroutine: initial state, suspended @@ -664,7 +661,6 @@ static void resume (lua_State *L, void *ud) { LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs, int *nresults) { int status; - unsigned short oldnny = L->nny; /* save "number of non-yieldable" calls */ lua_lock(L); if (L->status == LUA_OK) { /* may be starting a coroutine */ if (L->ci != &L->base_ci) /* not in base level? */ @@ -675,11 +671,10 @@ LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs, if (from == NULL) L->nCcalls = 1; else /* correct 'nCcalls' for this thread */ - L->nCcalls = from->nCcalls - from->nci + L->nci + LUAL_COROCSTK; + L->nCcalls = getCcalls(from) - from->nci + L->nci + CSTACKCF; if (L->nCcalls >= LUAI_MAXCCALLS) return resume_error(L, "C stack overflow", nargs); luai_userstateresume(L, nargs); - L->nny = 0; /* allow yields */ api_checknelems(L, (L->status == LUA_OK) ? nargs + 1 : nargs); status = luaD_rawrunprotected(L, resume, &nargs); /* continue running after recoverable errors */ @@ -698,14 +693,13 @@ LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs, } *nresults = (status == LUA_YIELD) ? L->ci->u2.nyield : cast_int(L->top - (L->ci->func + 1)); - L->nny = oldnny; /* restore 'nny' */ lua_unlock(L); return status; } LUA_API int lua_isyieldable (lua_State *L) { - return (L->nny == 0); + return yieldable(L); } @@ -715,7 +709,7 @@ LUA_API int lua_yieldk (lua_State *L, int nresults, lua_KContext ctx, luai_userstateyield(L, nresults); lua_lock(L); api_checknelems(L, nresults); - if (unlikely(L->nny > 0)) { + if (unlikely(!yieldable(L))) { if (L != G(L)->mainthread) luaG_runerror(L, "attempt to yield across a C-call boundary"); else @@ -741,7 +735,7 @@ LUA_API int lua_yieldk (lua_State *L, int nresults, lua_KContext ctx, /* ** Call the C function 'func' in protected mode, restoring basic -** thread information ('allowhook', 'nny', etc.) and in particular +** thread information ('allowhook', etc.) and in particular ** its stack level in case of errors. */ int luaD_pcall (lua_State *L, Pfunc func, void *u, @@ -749,7 +743,6 @@ int luaD_pcall (lua_State *L, Pfunc func, void *u, int status; CallInfo *old_ci = L->ci; lu_byte old_allowhooks = L->allowhook; - unsigned short old_nny = L->nny; ptrdiff_t old_errfunc = L->errfunc; L->errfunc = ef; status = luaD_rawrunprotected(L, func, u); @@ -757,7 +750,6 @@ int luaD_pcall (lua_State *L, Pfunc func, void *u, StkId oldtop = restorestack(L, old_top); L->ci = old_ci; L->allowhook = old_allowhooks; - L->nny = old_nny; status = luaF_close(L, oldtop, status); oldtop = restorestack(L, old_top); /* previous call may change stack */ luaD_seterrorobj(L, status, oldtop); @@ -811,7 +803,7 @@ int luaD_protectedparser (lua_State *L, ZIO *z, const char *name, const char *mode) { struct SParser p; int status; - L->nny++; /* cannot yield during parsing */ + incnny(L); /* cannot yield during parsing */ p.z = z; p.name = name; p.mode = mode; p.dyd.actvar.arr = NULL; p.dyd.actvar.size = 0; p.dyd.gt.arr = NULL; p.dyd.gt.size = 0; @@ -822,7 +814,7 @@ int luaD_protectedparser (lua_State *L, ZIO *z, const char *name, luaM_freearray(L, p.dyd.actvar.arr, p.dyd.actvar.size); luaM_freearray(L, p.dyd.gt.arr, p.dyd.gt.size); luaM_freearray(L, p.dyd.label.arr, p.dyd.label.size); - L->nny--; + decnny(L); return status; } diff --git a/llimits.h b/llimits.h index 8ab58b5ce9..9d35d1c774 100644 --- a/llimits.h +++ b/llimits.h @@ -184,11 +184,13 @@ typedef LUAI_UACINT l_uacInt; ** must be an unsigned with (at least) 4 bytes (see details in lopcodes.h) */ #if LUAI_BITSINT >= 32 -typedef unsigned int Instruction; +typedef unsigned int l_uint32; #else -typedef unsigned long Instruction; +typedef unsigned long l_uint32; #endif +typedef l_uint32 Instruction; + /* diff --git a/lparser.c b/lparser.c index eed8bffd93..3887958ef7 100644 --- a/lparser.c +++ b/lparser.c @@ -367,10 +367,12 @@ static void adjust_assign (LexState *ls, int nvars, int nexps, expdesc *e) { } -#define enterlevel(ls) luaE_incCcalls((ls)->L) - +/* +** Macros to limit the maximum recursion depth while parsing +*/ +#define enterlevel(ls) luaE_enterCcall((ls)->L) -#define leavelevel(ls) ((ls)->L->nCcalls--) +#define leavelevel(ls) luaE_exitCcall((ls)->L) /* diff --git a/lstate.c b/lstate.c index 5ee024fcab..04a9e0649e 100644 --- a/lstate.c +++ b/lstate.c @@ -99,24 +99,42 @@ void luaE_setdebt (global_State *g, l_mem debt) { /* ** Increment count of "C calls" and check for overflows. In case of ** a stack overflow, check appropriate error ("regular" overflow or -** overflow while handling stack overflow). If 'nCalls' is larger than -** LUAI_MAXCCALLS (which means it is handling a "regular" overflow) but -** smaller than 9/8 of LUAI_MAXCCALLS, does not report an error (to -** allow overflow handling to work) +** overflow while handling stack overflow). +** If 'nCcalls' is larger than LUAI_MAXCCALLS but smaller than +** LUAI_MAXCCALLS + CSTACKCF (plus 2 to avoid by-one errors), it means +** it has just entered the "overflow zone", so the function raises an +** overflow error. +** If 'nCcalls' is larger than LUAI_MAXCCALLS + CSTACKCF + 2 +** (which means it is already handling an overflow) but smaller than +** 9/8 of LUAI_MAXCCALLS, does not report an error (to allow message +** handling to work). +** Otherwise, report a stack overflow while handling a stack overflow +** (probably caused by a repeating error in the message handling +** function). */ -void luaE_incCcalls (lua_State *L) { - if (++L->nCcalls >= LUAI_MAXCCALLS) { - if (L->nCcalls == LUAI_MAXCCALLS) - luaG_runerror(L, "C stack overflow"); - else if (L->nCcalls >= (LUAI_MAXCCALLS + (LUAI_MAXCCALLS>>3))) - luaD_throw(L, LUA_ERRERR); /* error while handing stack error */ +void luaE_enterCcall (lua_State *L) { + int ncalls = getCcalls(L); + L->nCcalls++; + if (ncalls >= LUAI_MAXCCALLS) { /* possible overflow? */ + luaE_freeCI(L); /* release unused CIs */ + ncalls = getCcalls(L); /* update call count */ + if (ncalls >= LUAI_MAXCCALLS) { /* still overflow? */ + if (ncalls <= LUAI_MAXCCALLS + CSTACKCF + 2) { + /* no error before increments; raise the error now */ + L->nCcalls += (CSTACKCF + 4); /* avoid raising it again */ + luaG_runerror(L, "C stack overflow"); + } + else if (ncalls >= (LUAI_MAXCCALLS + (LUAI_MAXCCALLS >> 3))) + luaD_throw(L, LUA_ERRERR); /* error while handling stack error */ + } } } CallInfo *luaE_extendCI (lua_State *L) { CallInfo *ci; - luaE_incCcalls(L); + lua_assert(L->ci->next == NULL); + luaE_enterCcall(L); ci = luaM_new(L, CallInfo); lua_assert(L->ci->next == NULL); L->ci->next = ci; @@ -135,13 +153,13 @@ void luaE_freeCI (lua_State *L) { CallInfo *ci = L->ci; CallInfo *next = ci->next; ci->next = NULL; - L->nCcalls -= L->nci; /* to subtract removed elements from 'nCcalls' */ + L->nCcalls -= L->nci; /* subtract removed elements from 'nCcalls' */ while ((ci = next) != NULL) { next = ci->next; luaM_free(L, ci); L->nci--; } - L->nCcalls += L->nci; /* to subtract removed elements from 'nCcalls' */ + L->nCcalls += L->nci; /* adjust result */ } @@ -151,7 +169,7 @@ void luaE_freeCI (lua_State *L) { void luaE_shrinkCI (lua_State *L) { CallInfo *ci = L->ci; CallInfo *next2; /* next's next */ - L->nCcalls -= L->nci; /* to subtract removed elements from 'nCcalls' */ + L->nCcalls -= L->nci; /* subtract removed elements from 'nCcalls' */ /* while there are two nexts */ while (ci->next != NULL && (next2 = ci->next->next) != NULL) { luaM_free(L, ci->next); /* free next */ @@ -160,7 +178,7 @@ void luaE_shrinkCI (lua_State *L) { next2->previous = ci; ci = next2; /* keep next's next */ } - L->nCcalls += L->nci; /* to subtract removed elements from 'nCcalls' */ + L->nCcalls += L->nci; /* adjust result */ } @@ -250,7 +268,6 @@ static void preinit_thread (lua_State *L, global_State *g) { L->allowhook = 1; resethookcount(L); L->openupval = NULL; - L->nny = 1; L->status = LUA_OK; L->errfunc = 0; } diff --git a/lstate.h b/lstate.h index ce33770748..b069b3901a 100644 --- a/lstate.h +++ b/lstate.h @@ -15,7 +15,6 @@ /* - ** Some notes about garbage-collected objects: All objects in Lua must ** be kept somehow accessible until being freed, so all objects always ** belong to one (and only one) of these lists, using field 'next' of @@ -43,26 +42,58 @@ ** 'weak': tables with weak values to be cleared; ** 'ephemeron': ephemeron tables with white->white entries; ** 'allweak': tables with weak keys and/or weak values to be cleared. - */ -/* +/* ** About 'nCcalls': each thread in Lua (a lua_State) keeps a count of -** how many "C calls" it has in the C stack, to avoid C-stack overflow. +** how many "C calls" it can do in the C stack, to avoid C-stack overflow. ** This count is very rough approximation; it considers only recursive ** functions inside the interpreter, as non-recursive calls can be ** considered using a fixed (although unknown) amount of stack space. ** +** The count itself has two parts: the lower part is the count itself; +** the higher part counts the number of non-yieldable calls in the stack. +** +** Because calls to external C functions can use of unkown amount +** of space (e.g., functions using an auxiliary buffer), calls +** to these functions add more than one to the count. +** ** The proper count also includes the number of CallInfo structures ** allocated by Lua, as a kind of "potential" calls. So, when Lua ** calls a function (and "consumes" one CallInfo), it needs neither to ** increment nor to check 'nCcalls', as its use of C stack is already ** accounted for. - */ +/* number of "C stack slots" used by an external C function */ +#define CSTACKCF 10 + +/* true if this thread does not have non-yieldable calls in the stack */ +#define yieldable(L) (((L)->nCcalls & 0xffff0000) == 0) + +/* real number of C calls */ +#define getCcalls(L) ((L)->nCcalls & 0xffff) + + +/* Increment the number of non-yieldable calls */ +#define incnny(L) ((L)->nCcalls += 0x10000) + +/* Decrement the number of non-yieldable calls */ +#define decnny(L) ((L)->nCcalls -= 0x10000) + +/* Increment the number of non-yieldable calls and nCcalls */ +#define incXCcalls(L) ((L)->nCcalls += 0x10000 + CSTACKCF) + +/* Decrement the number of non-yieldable calls and nCcalls */ +#define decXCcalls(L) ((L)->nCcalls -= 0x10000 + CSTACKCF) + + + + + + struct lua_longjmp; /* defined in ldo.c */ @@ -208,8 +239,9 @@ typedef struct global_State { */ struct lua_State { CommonHeader; - unsigned short nci; /* number of items in 'ci' list */ lu_byte status; + lu_byte allowhook; + unsigned short nci; /* number of items in 'ci' list */ StkId top; /* first free slot in the stack */ global_State *l_G; CallInfo *ci; /* call info for current function */ @@ -223,13 +255,11 @@ struct lua_State { CallInfo base_ci; /* CallInfo for first level (C calling Lua) */ volatile lua_Hook hook; ptrdiff_t errfunc; /* current error handling function (stack index) */ + l_uint32 nCcalls; /* number of allowed nested C calls - 'nci' */ int stacksize; int basehookcount; int hookcount; - unsigned short nny; /* number of non-yieldable calls in stack */ - unsigned short nCcalls; /* number of nested C calls + 'nny' */ l_signalT hookmask; - lu_byte allowhook; }; @@ -283,8 +313,10 @@ LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1); LUAI_FUNC CallInfo *luaE_extendCI (lua_State *L); LUAI_FUNC void luaE_freeCI (lua_State *L); LUAI_FUNC void luaE_shrinkCI (lua_State *L); -LUAI_FUNC void luaE_incCcalls (lua_State *L); +LUAI_FUNC void luaE_enterCcall (lua_State *L); + +#define luaE_exitCcall(L) ((L)->nCcalls--) #endif diff --git a/ltests.h b/ltests.h index 9d409c8d43..997e1c4b08 100644 --- a/ltests.h +++ b/ltests.h @@ -31,7 +31,7 @@ /* compiled with -O0, Lua uses a lot of C stack space... */ #undef LUAI_MAXCCALLS -#define LUAI_MAXCCALLS 200 +#define LUAI_MAXCCALLS 400 /* to avoid warnings, and to make sure value is really unused */ #define UNUSED(x) (x=0, (void)(x)) diff --git a/luaconf.h b/luaconf.h index ff7085131a..0fc161a4b3 100644 --- a/luaconf.h +++ b/luaconf.h @@ -695,14 +695,14 @@ /* @@ LUAL_BUFFERSIZE is the buffer size used by the lauxlib buffer system. ** CHANGE it if it uses too much C-stack space. (For long double, -** 'string.format("%.99f", -1e4932)' needs 5034 bytes, so a +** 'string.format("%.99f", -1e4932)' needs 5052 bytes, so a ** smaller buffer would force a memory allocation for each call to ** 'string.format'.) */ #if LUA_FLOAT_TYPE == LUA_FLOAT_LONGDOUBLE #define LUAL_BUFFERSIZE 8192 #else -#define LUAL_BUFFERSIZE ((int)(0x80 * sizeof(void*) * sizeof(lua_Integer))) +#define LUAL_BUFFERSIZE ((int)(16 * sizeof(void*) * sizeof(lua_Number))) #endif /* diff --git a/testes/all.lua b/testes/all.lua index 26d2497659..84ba80a625 100644 --- a/testes/all.lua +++ b/testes/all.lua @@ -172,6 +172,7 @@ if not _G._soft then assert(f() == 'b') assert(f() == 'a') end +dofile('cstack.lua') dofile('nextvar.lua') dofile('pm.lua') dofile('utf8.lua') diff --git a/testes/coroutine.lua b/testes/coroutine.lua index 5674a4dd14..ca30011ff5 100644 --- a/testes/coroutine.lua +++ b/testes/coroutine.lua @@ -107,7 +107,7 @@ function filter (p, g) end) end -local x = gen(100) +local x = gen(80) local a = {} while 1 do local n = x() @@ -116,7 +116,7 @@ while 1 do x = filter(n, x) end -assert(#a == 25 and a[#a] == 97) +assert(#a == 22 and a[#a] == 79) x, a = nil diff --git a/testes/cstack.lua b/testes/cstack.lua new file mode 100644 index 0000000000..9e5bbaefd7 --- /dev/null +++ b/testes/cstack.lua @@ -0,0 +1,62 @@ +-- $Id: testes/cstack.lua $ +-- See Copyright Notice in file all.lua + +print"testing C-stack overflow detection" + +-- Segmentation faults in these tests probably result from a C-stack +-- overflow. To avoid these errors, recompile Lua with a smaller +-- value for the constant 'LUAI_MAXCCALLS' or else ensure a larger +-- stack for the program. + +local function checkerror (msg, f, ...) + local s, err = pcall(f, ...) + assert(not s and string.find(err, msg)) +end + + +do -- simple recursion + local count = 0 + local function foo () + count = count + 1 + foo() + end + checkerror("stack overflow", foo) + print(" maximum recursion: " .. count) +end + + +-- bug since 2.5 (C-stack overflow in recursion inside pattern matching) +do + local function f (size) + local s = string.rep("a", size) + local p = string.rep(".?", size) + return string.match(s, p) + end + local m = f(80) + assert(#m == 80) + checkerror("too complex", f, 200000) +end + + +-- testing stack-overflow in recursive 'gsub' +do + local count = 0 + local function foo () + count = count + 1 + string.gsub("a", ".", foo) + end + checkerror("stack overflow", foo) + print(" maximum 'gsub' nest (calls): " .. count) + + -- can be done with metamethods, too + count = 0 + local t = setmetatable({}, {__index = foo}) + foo = function () + count = count + 1 + string.gsub("a", ".", t) + end + checkerror("stack overflow", foo) + print(" maximum 'gsub' nest (metamethods): " .. count) +end + +print'OK' diff --git a/testes/pm.lua b/testes/pm.lua index cdcf3becb8..1afaccf694 100644 --- a/testes/pm.lua +++ b/testes/pm.lua @@ -237,18 +237,6 @@ checkerror("invalid capture index %%0", string.gsub, "alo", "(%0)", "a") checkerror("invalid capture index %%1", string.gsub, "alo", "(%1)", "a") checkerror("invalid use of '%%'", string.gsub, "alo", ".", "%x") --- bug since 2.5 (C-stack overflow) -do - local function f (size) - local s = string.rep("a", size) - local p = string.rep(".?", size) - return pcall(string.match, s, p) - end - local r, m = f(80) - assert(r and #m == 80) - r, m = f(200000) - assert(not r and string.find(m, "too complex")) -end if not _soft then print("big strings") From 437a5b07d415e1a74160ddfd804017171d6cc5cb Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 28 Dec 2018 15:42:34 -0200 Subject: [PATCH 0373/1145] Added a warning system to Lua The warning system is just a way for Lua to emit warnings, messages to the programmer that do not interfere with the running program. --- lapi.c | 18 ++++++++++++++++ lauxlib.c | 28 +++++++++++++++++++++++- lbaselib.c | 8 +++++++ lstate.c | 2 ++ lstate.h | 2 ++ ltests.c | 33 +++++++++++++++++++++++++++- lua.h | 14 ++++++++++++ manual/manual.of | 56 +++++++++++++++++++++++++++++++++++++++++------- testes/all.lua | 13 ++++++----- testes/api.lua | 14 ++++++++++++ 10 files changed, 173 insertions(+), 15 deletions(-) diff --git a/lapi.c b/lapi.c index 2d10bb73f7..0f0166e58a 100644 --- a/lapi.c +++ b/lapi.c @@ -1267,6 +1267,24 @@ LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud) { } +void lua_setwarnf (lua_State *L, lua_WarnFunction f, void *ud) { + lua_lock(L); + G(L)->ud_warn = ud; + G(L)->warnf = f; + lua_unlock(L); +} + + +void lua_warning (lua_State *L, const char *msg) { + lua_WarnFunction wf = G(L)->warnf; + lua_lock(L); + if (wf != NULL) + wf(&G(L)->ud_warn, msg); + lua_unlock(L); +} + + + LUA_API void *lua_newuserdatauv (lua_State *L, size_t size, int nuvalue) { Udata *u; lua_lock(L); diff --git a/lauxlib.c b/lauxlib.c index 769586b691..abf923f80a 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -986,9 +986,35 @@ static int panic (lua_State *L) { } +/* +** checks whether 'message' ends with end-of-line +** (and therefore is the last part of a warning) +*/ +static int islast (const char *message) { + size_t len = strlen(message); + return (len > 0 && message[len - 1] == '\n'); +} + + +/* +** Emit a warning. If '*pud' is NULL, previous message was to be +** continued by the current one. +*/ +static void warnf (void **pud, const char *message) { + if (*pud == NULL) /* previous message was not the last? */ + lua_writestringerror("%s", message); + else /* start a new warning */ + lua_writestringerror("Lua warning: %s", message); + *pud = (islast(message)) ? pud : NULL; +} + + LUALIB_API lua_State *luaL_newstate (void) { lua_State *L = lua_newstate(l_alloc, NULL); - if (L) lua_atpanic(L, &panic); + if (L) { + lua_atpanic(L, &panic); + lua_setwarnf(L, warnf, L); + } return L; } diff --git a/lbaselib.c b/lbaselib.c index 7c0ebfecd3..26683a1dcb 100644 --- a/lbaselib.c +++ b/lbaselib.c @@ -43,6 +43,13 @@ static int luaB_print (lua_State *L) { } +static int luaB_warn (lua_State *L) { + const char *msg = luaL_checkstring(L, 1); + lua_warning(L, msg); + return 0; +} + + #define SPACECHARS " \f\n\r\t\v" static const char *b_str2int (const char *s, int base, lua_Integer *pn) { @@ -482,6 +489,7 @@ static const luaL_Reg base_funcs[] = { {"pairs", luaB_pairs}, {"pcall", luaB_pcall}, {"print", luaB_print}, + {"warn", luaB_warn}, {"rawequal", luaB_rawequal}, {"rawlen", luaB_rawlen}, {"rawget", luaB_rawget}, diff --git a/lstate.c b/lstate.c index 04a9e0649e..b3e9ec6016 100644 --- a/lstate.c +++ b/lstate.c @@ -365,6 +365,8 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { L->next = NULL; g->frealloc = f; g->ud = ud; + g->warnf = NULL; + g->ud_warn = NULL; g->mainthread = L; g->seed = luai_makeseed(L); g->gcrunning = 0; /* no GC while building state */ diff --git a/lstate.h b/lstate.h index b069b3901a..f379325640 100644 --- a/lstate.h +++ b/lstate.h @@ -231,6 +231,8 @@ typedef struct global_State { TString *tmname[TM_N]; /* array with tag-method names */ struct Table *mt[LUA_NUMTAGS]; /* metatables for basic types */ TString *strcache[STRCACHE_N][STRCACHE_M]; /* cache for strings in API */ + lua_WarnFunction warnf; /* warning function */ + void *ud_warn; /* auxiliary data to 'warnf' */ } global_State; diff --git a/ltests.c b/ltests.c index 0b5ed90b22..5ea8b0804c 100644 --- a/ltests.c +++ b/ltests.c @@ -63,10 +63,36 @@ static void pushobject (lua_State *L, const TValue *o) { } +static void badexit (void) { + /* avoid assertion failures when exiting */ + l_memcontrol.numblocks = l_memcontrol.total = 0; + exit(EXIT_FAILURE); +} + + static int tpanic (lua_State *L) { fprintf(stderr, "PANIC: unprotected error in call to Lua API (%s)\n", lua_tostring(L, -1)); - return (exit(EXIT_FAILURE), 0); /* do not return to Lua */ + return (badexit(), 0); /* do not return to Lua */ +} + + +static int islast (const char *message) { + size_t len = strlen(message); + return (len > 0 && message[len - 1] == '\n'); +} + + +static void warnf (void **pud, const char *msg) { + if (*pud == NULL) /* continuation line? */ + printf("%s", msg); /* print it */ + else if (msg[0] == '*') /* expected warning? */ + printf("Expected Lua warning: %s", msg + 1); /* print without the star */ + else { /* a real warning; should not happen during tests */ + fprintf(stderr, "Warning in test mode (%s), aborting...\n", msg); + badexit(); + } + *pud = islast(msg) ? pud : NULL; } @@ -1405,6 +1431,10 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) { const char *msg = getstring; printf("%s\n", msg); } + else if EQ("warning") { + const char *msg = getstring; + lua_warning(L1, msg); + } else if EQ("pushbool") { lua_pushboolean(L1, getnum); } @@ -1743,6 +1773,7 @@ static void checkfinalmem (void) { int luaB_opentests (lua_State *L) { void *ud; lua_atpanic(L, &tpanic); + lua_setwarnf(L, &warnf, L); atexit(checkfinalmem); lua_assert(lua_getallocf(L, &ud) == debug_realloc); lua_assert(ud == cast_voidp(&l_memcontrol)); diff --git a/lua.h b/lua.h index 95ce5a2e9b..a6f8268b8e 100644 --- a/lua.h +++ b/lua.h @@ -126,6 +126,13 @@ typedef int (*lua_Writer) (lua_State *L, const void *p, size_t sz, void *ud); typedef void * (*lua_Alloc) (void *ud, void *ptr, size_t osize, size_t nsize); +/* +** Type for warning functions +*/ +typedef void (*lua_WarnFunction) (void **pud, const char *msg); + + + /* ** generic extra include file @@ -299,6 +306,13 @@ LUA_API int (lua_isyieldable) (lua_State *L); #define lua_yield(L,n) lua_yieldk(L, (n), 0, NULL) +/* +** Warning-related functions +*/ +LUA_API void (lua_setwarnf) (lua_State *L, lua_WarnFunction f, void *ud); +LUA_API void (lua_warning) (lua_State *L, const char *msg); + + /* ** garbage-collection function and options */ diff --git a/manual/manual.of b/manual/manual.of index 044bd09c90..196ea1efe4 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -1795,7 +1795,7 @@ Functions with any detectable difference (different behavior, different definition) are always different. Functions created at different times but with no detectable differences may be classified as equal or not -(depending on internal cashing details). +(depending on internal caching details). You can change the way that Lua compares tables and userdata by using the @idx{__eq} metamethod @see{metatable}. @@ -4033,6 +4033,16 @@ for the @Q{newindex} event @see{metatable}. } +@APIEntry{int lua_setiuservalue (lua_State *L, int index, int n);| +@apii{1,0,-} + +Pops a value from the stack and sets it as +the new @id{n}-th user value associated to the +full userdata at the given index. +Returns 0 if the userdata does not have that value. + +} + @APIEntry{void lua_setmetatable (lua_State *L, int index);| @apii{1,0,-} @@ -4066,13 +4076,13 @@ If @id{index} @N{is 0}, then all stack elements are removed. } -@APIEntry{int lua_setiuservalue (lua_State *L, int index, int n);| -@apii{1,0,-} +@APIEntry{void lua_setwarnf (lua_State *L, lua_WarnFunction f, void *ud);| +@apii{0,0,-} -Pops a value from the stack and sets it as -the new @id{n}-th user value associated to the -full userdata at the given index. -Returns 0 if the userdata does not have that value. +Sets the @x{warning function} to be used by Lua to emit warnings +@see{lua_WarnFunction}. +The @id{ud} parameter initializes the slot @id{pud} passed to +the warning function. } @@ -4334,6 +4344,30 @@ Returns the version number of this core. } +@APIEntry{ +typedef void (*lua_WarnFunction) (void **pud, const char *msg);| + +The type of @x{warning function}s, called by Lua to emit warnings. +The first parameter is the address of a writable slot, +constant for a given Lua state and +initialized by @Lid{lua_setwarnf}. +The second parameter is the warning message. +This function should assume that +a message not ending with an end-of-line will be +continued by the message in the next call. + +} + +@APIEntry{ +void lua_warning (lua_State *L, const char *msg);| +@apii{0,0,-} + +Emits a warning with the given message. +A message not ending with an end-of-line should be +continued in another call to this function. + +} + @APIEntry{ typedef int (*lua_Writer) (lua_State *L, const void* p, @@ -4345,7 +4379,7 @@ Every time it produces another piece of chunk, @Lid{lua_dump} calls the writer, passing along the buffer to be written (@id{p}), its size (@id{sz}), -and the @id{data} parameter supplied to @Lid{lua_dump}. +and the @id{ud} parameter supplied to @Lid{lua_dump}. The writer returns an error code: @N{0 means} no errors; @@ -6261,6 +6295,12 @@ The current value of this variable is @St{Lua 5.4}. } +@LibEntry{warn (message)| + +Emits a warning with the given message. + +} + @LibEntry{xpcall (f, msgh [, arg1, @Cdots])| This function is similar to @Lid{pcall}, diff --git a/testes/all.lua b/testes/all.lua index 84ba80a625..bde4195e62 100644 --- a/testes/all.lua +++ b/testes/all.lua @@ -5,8 +5,8 @@ local version = "Lua 5.4" if _VERSION ~= version then - io.stderr:write("\nThis test suite is for ", version, ", not for ", _VERSION, - "\nExiting tests\n") + warn(string.format( + "This test suite is for %s, not for %s\nExiting tests\n", version, _VERSION)) return end @@ -190,11 +190,10 @@ assert(dofile('verybig.lua', true) == 10); collectgarbage() dofile('files.lua') if #msgs > 0 then - print("\ntests not performed:") + warn("*tests not performed:\n ") for i=1,#msgs do - print(msgs[i]) + warn(msgs[i]); warn("\n ") end - print() end -- no test module should define 'debug' @@ -220,6 +219,10 @@ local _G, showmem, print, format, clock, time, difftime, assert, open = local fname = T and "time-debug.txt" or "time.txt" local lasttime + +warn("*This is "); warn("an expected"); warn(" warning\n") +warn("*This is"); warn(" another one\n") + if not usertests then -- open file with time of last performed test local f = io.open(fname) diff --git a/testes/api.lua b/testes/api.lua index 6f35e13249..b4d6386602 100644 --- a/testes/api.lua +++ b/testes/api.lua @@ -111,6 +111,20 @@ do -- testing 'rotate' tcheck(t, {10, 20, 30, 40}) end + +-- testing warnings +T.testC([[ + warning "*This " + warning "warning " + warning "should be in a" + warning " single line +" + warning "*This should be " + warning "another warning +" +]]) + + -- testing message handlers do local f = T.makeCfunc[[ From c6f7181e910b6b2ff1346b5486a31be87b1da5af Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 1 Jan 2019 12:14:56 -0200 Subject: [PATCH 0374/1145] No more LUA_ERRGCMM errors Errors in finalizers (__gc metamethods) are never propagated. Instead, they generate a warning. --- lapi.c | 4 +-- lgc.c | 28 ++++++++-------- lstate.c | 7 ++++ lstate.h | 1 + ltests.c | 61 +++++++++++++++++++++++++-------- lua.h | 3 +- manual/manual.of | 26 +++++---------- testes/all.lua | 11 +++--- testes/api.lua | 28 +++++----------- testes/gc.lua | 87 ++++++++++++++++++++++++++++-------------------- 10 files changed, 145 insertions(+), 111 deletions(-) diff --git a/lapi.c b/lapi.c index 0f0166e58a..8ff7bfbd30 100644 --- a/lapi.c +++ b/lapi.c @@ -1276,10 +1276,8 @@ void lua_setwarnf (lua_State *L, lua_WarnFunction f, void *ud) { void lua_warning (lua_State *L, const char *msg) { - lua_WarnFunction wf = G(L)->warnf; lua_lock(L); - if (wf != NULL) - wf(&G(L)->ud_warn, msg); + luaE_warning(L, msg); lua_unlock(L); } diff --git a/lgc.c b/lgc.c index 95a8ad5b97..0c3386e745 100644 --- a/lgc.c +++ b/lgc.c @@ -824,7 +824,7 @@ static void dothecall (lua_State *L, void *ud) { } -static void GCTM (lua_State *L, int propagateerrors) { +static void GCTM (lua_State *L) { global_State *g = G(L); const TValue *tm; TValue v; @@ -845,15 +845,13 @@ static void GCTM (lua_State *L, int propagateerrors) { L->ci->callstatus &= ~CIST_FIN; /* not running a finalizer anymore */ L->allowhook = oldah; /* restore hooks */ g->gcrunning = running; /* restore state */ - if (status != LUA_OK && propagateerrors) { /* error while running __gc? */ - if (status == LUA_ERRRUN) { /* is there an error object? */ - const char *msg = (ttisstring(s2v(L->top - 1))) - ? svalue(s2v(L->top - 1)) - : "no message"; - luaO_pushfstring(L, "error in __gc metamethod (%s)", msg); - status = LUA_ERRGCMM; /* error in __gc metamethod */ - } - luaD_throw(L, status); /* re-throw error */ + if (status != LUA_OK) { /* error while running __gc? */ + const char *msg = (ttisstring(s2v(L->top - 1))) + ? svalue(s2v(L->top - 1)) + : "error object is not a string"; + luaE_warning(L, "error in __gc metamethod ("); + luaE_warning(L, msg); + luaE_warning(L, ")\n"); } } } @@ -866,7 +864,7 @@ static int runafewfinalizers (lua_State *L, int n) { global_State *g = G(L); int i; for (i = 0; i < n && g->tobefnz; i++) - GCTM(L, 1); /* call one finalizer */ + GCTM(L); /* call one finalizer */ return i; } @@ -874,10 +872,10 @@ static int runafewfinalizers (lua_State *L, int n) { /* ** call all pending finalizers */ -static void callallpendingfinalizers (lua_State *L, int propagateerrors) { +static void callallpendingfinalizers (lua_State *L) { global_State *g = G(L); while (g->tobefnz) - GCTM(L, propagateerrors); + GCTM(L); } @@ -1124,7 +1122,7 @@ static void finishgencycle (lua_State *L, global_State *g) { checkSizes(L, g); g->gcstate = GCSpropagate; /* skip restart */ if (!g->gcemergency) - callallpendingfinalizers(L, 1); + callallpendingfinalizers(L); } @@ -1334,7 +1332,7 @@ void luaC_freeallobjects (lua_State *L) { luaC_changemode(L, KGC_INC); separatetobefnz(g, 1); /* separate all objects with finalizers */ lua_assert(g->finobj == NULL); - callallpendingfinalizers(L, 0); + callallpendingfinalizers(L); deletelist(L, g->allgc, obj2gco(g->mainthread)); deletelist(L, g->finobj, NULL); deletelist(L, g->fixedgc, NULL); /* collect fixed objects */ diff --git a/lstate.c b/lstate.c index b3e9ec6016..7f6475a881 100644 --- a/lstate.c +++ b/lstate.c @@ -409,3 +409,10 @@ LUA_API void lua_close (lua_State *L) { } +void luaE_warning (lua_State *L, const char *msg) { + lua_WarnFunction wf = G(L)->warnf; + if (wf != NULL) + wf(&G(L)->ud_warn, msg); +} + + diff --git a/lstate.h b/lstate.h index f379325640..05a74dda85 100644 --- a/lstate.h +++ b/lstate.h @@ -316,6 +316,7 @@ LUAI_FUNC CallInfo *luaE_extendCI (lua_State *L); LUAI_FUNC void luaE_freeCI (lua_State *L); LUAI_FUNC void luaE_shrinkCI (lua_State *L); LUAI_FUNC void luaE_enterCcall (lua_State *L); +LUAI_FUNC void luaE_warning (lua_State *L, const char *msg); #define luaE_exitCcall(L) ((L)->nCcalls--) diff --git a/ltests.c b/ltests.c index 5ea8b0804c..0cb6d3a7b4 100644 --- a/ltests.c +++ b/ltests.c @@ -63,7 +63,11 @@ static void pushobject (lua_State *L, const TValue *o) { } -static void badexit (void) { +static void badexit (const char *fmt, ...) { + va_list argp; + va_start(argp, fmt); + vfprintf(stderr, fmt, argp); + va_end(argp); /* avoid assertion failures when exiting */ l_memcontrol.numblocks = l_memcontrol.total = 0; exit(EXIT_FAILURE); @@ -71,9 +75,9 @@ static void badexit (void) { static int tpanic (lua_State *L) { - fprintf(stderr, "PANIC: unprotected error in call to Lua API (%s)\n", - lua_tostring(L, -1)); - return (badexit(), 0); /* do not return to Lua */ + return (badexit("PANIC: unprotected error in call to Lua API (%s)\n", + lua_tostring(L, -1)), + 0); /* do not return to Lua */ } @@ -83,16 +87,47 @@ static int islast (const char *message) { } +/* +** Warning function for tests. Fist, it concatenates all parts of +** a warning in buffer 'buff'. Then: +** messages starting with '#' are shown on standard output (used to +** test explicit warnings); +** messages containing '@' are stored in global '_WARN' (used to test +** errors that generate warnings); +** other messages abort the tests (they represent real warning conditions; +** the standard tests should not generate these conditions unexpectedly). +*/ static void warnf (void **pud, const char *msg) { - if (*pud == NULL) /* continuation line? */ - printf("%s", msg); /* print it */ - else if (msg[0] == '*') /* expected warning? */ - printf("Expected Lua warning: %s", msg + 1); /* print without the star */ - else { /* a real warning; should not happen during tests */ - fprintf(stderr, "Warning in test mode (%s), aborting...\n", msg); - badexit(); - } - *pud = islast(msg) ? pud : NULL; + static char buff[200]; /* should be enough for tests... */ + static int cont = 0; /* message to be continued */ + if (cont) { /* continuation? */ + if (strlen(msg) >= sizeof(buff) - strlen(buff)) + badexit("warnf-buffer overflow"); + strcat(buff, msg); /* add new message to current warning */ + } + else { /* new warning */ + if (strlen(msg) >= sizeof(buff)) + badexit("warnf-buffer overflow"); + strcpy(buff, msg); /* start a new warning */ + } + if (!islast(msg)) /* message not finished yet? */ + cont = 1; /* wait for more */ + else { /* handle message */ + cont = 0; /* prepare for next message */ + if (buff[0] == '#') /* expected warning? */ + printf("Expected Lua warning: %s", buff); /* print it */ + else if (strchr(buff, '@') != NULL) { /* warning for test purposes? */ + lua_State *L = cast(lua_State *, *pud); + lua_unlock(L); + lua_pushstring(L, buff); + lua_setglobal(L, "_WARN"); /* assign message to global '_WARN' */ + lua_lock(L); + return; + } + else { /* a real warning; should not happen during tests */ + badexit("Unexpected warning in test mode: %s\naborting...\n", buff); + } + } } diff --git a/lua.h b/lua.h index a6f8268b8e..b777624e69 100644 --- a/lua.h +++ b/lua.h @@ -51,8 +51,7 @@ #define LUA_ERRRUN 2 #define LUA_ERRSYNTAX 3 #define LUA_ERRMEM 4 -#define LUA_ERRGCMM 5 -#define LUA_ERRERR 6 +#define LUA_ERRERR 5 typedef struct lua_State lua_State; diff --git a/manual/manual.of b/manual/manual.of index 196ea1efe4..d64f0f1a43 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -722,8 +722,6 @@ Lua calls the finalizers of all objects marked for finalization, following the reverse order that they were marked. If any finalizer marks objects for collection during that phase, these marks have no effect. -If any finalizer raises an error during that phase, -its execution is interrupted but the error is ignored. Finalizers cannot yield. @@ -2645,8 +2643,7 @@ by looking only at its arguments The third field, @T{x}, tells whether the function may raise errors: @Char{-} means the function never raises any error; -@Char{m} means the function may raise out-of-memory errors -and errors running a finalizer; +@Char{m} means the function may raise only out-of-memory errors; @Char{v} means the function may raise the errors explained in the text; @Char{e} means the function can run arbitrary Lua code, either directly or through metamethods, @@ -3364,12 +3361,6 @@ syntax error during precompilation;} @item{@Lid{LUA_ERRMEM}| @x{memory allocation (out-of-memory) error};} -@item{@Lid{LUA_ERRGCMM}| -error while running a @idx{__gc} metamethod. -(This error has no relation with the chunk being loaded. -It is generated by the garbage collector.) -} - } The @id{lua_load} function uses a user-supplied @id{reader} function @@ -3564,13 +3555,6 @@ For such errors, Lua does not call the @x{message handler}. error while running the @x{message handler}. } -@item{@defid{LUA_ERRGCMM}| -error while running a @idx{__gc} metamethod. -For such errors, Lua does not call the @x{message handler} -(as this kind of error typically has no relation -with the function being called). -} - } } @@ -6298,6 +6282,8 @@ The current value of this variable is @St{Lua 5.4}. @LibEntry{warn (message)| Emits a warning with the given message. +Note that messages not ending with an end-of-line +are assumed to be continued by the message in the next call. } @@ -8773,6 +8759,12 @@ so there is no need to check whether they are using the same address space.) } +@item{ +The constant @Lid{LUA_ERRGCMM} was removed. +Errors in finalizers are never propagated; +instead, they generate a warning. +} + } } diff --git a/testes/all.lua b/testes/all.lua index bde4195e62..506afad287 100644 --- a/testes/all.lua +++ b/testes/all.lua @@ -190,12 +190,17 @@ assert(dofile('verybig.lua', true) == 10); collectgarbage() dofile('files.lua') if #msgs > 0 then - warn("*tests not performed:\n ") + warn("#tests not performed:\n ") for i=1,#msgs do warn(msgs[i]); warn("\n ") end + warn("\n") end +print("(there should be two warnings now)") +warn("#This is "); warn("an expected"); warn(" warning\n") +warn("#This is"); warn(" another one\n") + -- no test module should define 'debug' assert(debug == nil) @@ -219,10 +224,6 @@ local _G, showmem, print, format, clock, time, difftime, assert, open = local fname = T and "time-debug.txt" or "time.txt" local lasttime - -warn("*This is "); warn("an expected"); warn(" warning\n") -warn("*This is"); warn(" another one\n") - if not usertests then -- open file with time of last performed test local f = io.open(fname) diff --git a/testes/api.lua b/testes/api.lua index b4d6386602..893a36cb75 100644 --- a/testes/api.lua +++ b/testes/api.lua @@ -114,13 +114,12 @@ end -- testing warnings T.testC([[ - warning "*This " - warning "warning " - warning "should be in a" - warning " single line + warning "#This shold be a" + warning " single " + warning "warning " - warning "*This should be " - warning "another warning + warning "#This should be " + warning "another one " ]]) @@ -896,24 +895,15 @@ do -- testing errors during GC a[i] = T.newuserdata(i) -- creates several udata end for i=1,20,2 do -- mark half of them to raise errors during GC - debug.setmetatable(a[i], {__gc = function (x) error("error inside gc") end}) + debug.setmetatable(a[i], + {__gc = function (x) error("@expected error in gc") end}) end for i=2,20,2 do -- mark the other half to count and to create more garbage debug.setmetatable(a[i], {__gc = function (x) load("A=A+1")() end}) end + a = nil _G.A = 0 - a = 0 - while 1 do - local stat, msg = pcall(collectgarbage) - if stat then - break -- stop when no more errors - else - a = a + 1 - assert(string.find(msg, "__gc")) - end - end - assert(a == 10) -- number of errors - + collectgarbage() assert(A == 10) -- number of normal collections collectgarbage("restart") end diff --git a/testes/gc.lua b/testes/gc.lua index 8b9179c858..84e8ffb700 100644 --- a/testes/gc.lua +++ b/testes/gc.lua @@ -353,40 +353,36 @@ GC() -- testing errors during GC -do -collectgarbage("stop") -- stop collection -local u = {} -local s = {}; setmetatable(s, {__mode = 'k'}) -setmetatable(u, {__gc = function (o) - local i = s[o] - s[i] = true - assert(not s[i - 1]) -- check proper finalization order - if i == 8 then error("here") end -- error during GC -end}) - -for i = 6, 10 do - local n = setmetatable({}, getmetatable(u)) - s[n] = i -end - -assert(not pcall(collectgarbage)) -for i = 8, 10 do assert(s[i]) end - -for i = 1, 5 do - local n = setmetatable({}, getmetatable(u)) - s[n] = i -end +if T then + collectgarbage("stop") -- stop collection + local u = {} + local s = {}; setmetatable(s, {__mode = 'k'}) + setmetatable(u, {__gc = function (o) + local i = s[o] + s[i] = true + assert(not s[i - 1]) -- check proper finalization order + if i == 8 then error("@expected@") end -- error during GC + end}) + + for i = 6, 10 do + local n = setmetatable({}, getmetatable(u)) + s[n] = i + end -collectgarbage() -for i = 1, 10 do assert(s[i]) end + collectgarbage() + assert(string.find(_WARN, "error in __gc metamethod")) + assert(string.match(_WARN, "@(.-)@") == "expected") + for i = 8, 10 do assert(s[i]) end -getmetatable(u).__gc = false + for i = 1, 5 do + local n = setmetatable({}, getmetatable(u)) + s[n] = i + end + collectgarbage() + for i = 1, 10 do assert(s[i]) end --- __gc errors with non-string messages -setmetatable({}, {__gc = function () error{} end}) -local a, b = pcall(collectgarbage) -assert(not a and type(b) == "string" and string.find(b, "error in __gc")) + getmetatable(u).__gc = false end print '+' @@ -478,9 +474,11 @@ end -- errors during collection -u = setmetatable({}, {__gc = function () error "!!!" end}) -u = nil -assert(not pcall(collectgarbage)) +if T then + u = setmetatable({}, {__gc = function () error "@expected error" end}) + u = nil + collectgarbage() +end if not _soft then @@ -645,11 +643,26 @@ do end -- create several objects to raise errors when collected while closing state -do - local mt = {__gc = function (o) return o + 1 end} - for i = 1,10 do +if T then + local error, assert, warn, find = error, assert, warn, string.find + local n = 0 + local lastmsg + local mt = {__gc = function (o) + n = n + 1 + assert(n == o[1]) + if n == 1 then + _WARN = nil + elseif n == 2 then + assert(find(_WARN, "@expected warning")) + lastmsg = _WARN -- get message from previous error (first 'o') + else + assert(lastmsg == _WARN) -- subsequent error messages are equal + end + error"@expected warning" + end} + for i = 10, 1, -1 do -- create object and preserve it until the end - table.insert(___Glob, setmetatable({}, mt)) + table.insert(___Glob, setmetatable({i}, mt)) end end From 4ace93ca6502dd1da38d5c06fa099d229e791ba8 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 4 Jan 2019 13:09:47 -0200 Subject: [PATCH 0375/1145] No more to-be-closed functions To-be-closed variables must contain objects with '__toclose' metamethods (or nil). Functions were removed for several reasons: * Functions interact badly with sandboxes. If a sandbox raises an error to interrupt a script, a to-be-closed function still can hijack control and continue running arbitrary sandboxed code. * Functions interact badly with coroutines. If a coroutine yields and is never resumed again, its to-be-closed functions will never run. To-be-closed objects, on the other hand, will still be closed, provided they have appropriate finalizers. * If you really need a function, it is easy to create a dummy object to run that function in its '__toclose' metamethod. This comit also adds closing of variables in case of panic. --- ldo.c | 1 + lfunc.c | 24 ++++++++------------ ltests.c | 18 +++++++++------ manual/manual.of | 34 ++++++++++++---------------- testes/api.lua | 19 +++++++++++++++- testes/coroutine.lua | 13 +++++++++-- testes/goto.lua | 2 +- testes/locals.lua | 54 +++++++++++++++++++++++++------------------- 8 files changed, 97 insertions(+), 68 deletions(-) diff --git a/ldo.c b/ldo.c index f8d8f11cfb..077109c4ab 100644 --- a/ldo.c +++ b/ldo.c @@ -118,6 +118,7 @@ l_noret luaD_throw (lua_State *L, int errcode) { } else { /* thread has no error handler */ global_State *g = G(L); + errcode = luaF_close(L, L->stack, errcode); /* close all upvalues */ L->status = cast_byte(errcode); /* mark it as dead */ if (g->mainthread->errorJmp) { /* main thread has a handler? */ setobjs2s(L, g->mainthread->top++, L->top - 1); /* copy error obj. */ diff --git a/lfunc.c b/lfunc.c index bdf3cd2598..362b798cba 100644 --- a/lfunc.c +++ b/lfunc.c @@ -100,28 +100,23 @@ UpVal *luaF_findupval (lua_State *L, StkId level) { static void callclose (lua_State *L, void *ud) { UNUSED(ud); - luaD_callnoyield(L, L->top - 2, 0); + luaD_callnoyield(L, L->top - 3, 0); } /* -** Prepare closing method plus its argument for object 'obj' with +** Prepare closing method plus its arguments for object 'obj' with ** error message 'err'. (This function assumes EXTRA_STACK.) */ static int prepclosingmethod (lua_State *L, TValue *obj, TValue *err) { StkId top = L->top; - if (ttisfunction(obj)) { /* object to-be-closed is a function? */ - setobj2s(L, top, obj); /* push function */ - setobj2s(L, top + 1, err); /* push error msg. as argument */ - } - else { /* try '__close' metamethod */ - const TValue *tm = luaT_gettmbyobj(L, obj, TM_CLOSE); - if (ttisnil(tm)) /* no metamethod? */ - return 0; /* nothing to call */ - setobj2s(L, top, tm); /* will call metamethod... */ - setobj2s(L, top + 1, obj); /* with 'self' as the argument */ - } - L->top = top + 2; /* add function and argument */ + const TValue *tm = luaT_gettmbyobj(L, obj, TM_CLOSE); + if (ttisnil(tm)) /* no metamethod? */ + return 0; /* nothing to call */ + setobj2s(L, top, tm); /* will call metamethod... */ + setobj2s(L, top + 1, obj); /* with 'self' as the 1st argument */ + setobj2s(L, top + 2, err); /* and error msg. as 2nd argument */ + L->top = top + 3; /* add function and arguments */ return 1; } @@ -156,6 +151,7 @@ static int callclosemth (lua_State *L, TValue *uv, StkId level, int status) { if (newstatus != LUA_OK) /* another error when closing? */ status = newstatus; /* this will be the new error */ } + /* else no metamethod; ignore this case and keep original error */ } return status; } diff --git a/ltests.c b/ltests.c index 0cb6d3a7b4..36a974aeb6 100644 --- a/ltests.c +++ b/ltests.c @@ -1201,8 +1201,8 @@ static const char *const delimits = " \t\n,;"; static void skip (const char **pc) { for (;;) { if (**pc != '\0' && strchr(delimits, **pc)) (*pc)++; - else if (**pc == '#') { - while (**pc != '\n' && **pc != '\0') (*pc)++; + else if (**pc == '#') { /* comment? */ + while (**pc != '\n' && **pc != '\0') (*pc)++; /* until end-of-line */ } else break; } @@ -1544,18 +1544,22 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) { } else if EQ("setfield") { int t = getindex; - lua_setfield(L1, t, getstring); + const char *s = getstring; + lua_setfield(L1, t, s); } else if EQ("setglobal") { - lua_setglobal(L1, getstring); + const char *s = getstring; + lua_setglobal(L1, s); } else if EQ("sethook") { int mask = getnum; int count = getnum; - sethookaux(L1, mask, count, getstring); + const char *s = getstring; + sethookaux(L1, mask, count, s); } else if EQ("setmetatable") { - lua_setmetatable(L1, getindex); + int idx = getindex; + lua_setmetatable(L1, idx); } else if EQ("settable") { lua_settable(L1, getindex); @@ -1620,7 +1624,7 @@ static struct X { int x; } x; return lua_yieldk(L1, nres, i, Cfunck); } else if EQ("toclose") { - lua_toclose(L, getnum); + lua_toclose(L1, getnum); } else luaL_error(L, "unknown instruction %s", buff); } diff --git a/manual/manual.of b/manual/manual.of index d64f0f1a43..b9ab1ebe52 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -1536,33 +1536,29 @@ goes out of scope, including normal block termination, exiting its block by @Rw{break}/@Rw{goto}/@Rw{return}, or exiting by an error. -To \emph{close} a value has the following meaning here: -If the value of the variable when it goes out of scope is a function, -that function is called; -otherwise, if the value has a @idx{__close} metamethod, -that metamethod is called; -otherwise, if the value is @nil, nothing is done; -otherwise, an error is raised. -In the function case, -if the scope is being closed by an error, -the error object is passed as an argument to the function; -if there is no error, the function gets @nil. -In the metamethod case, -the value itself always is passed as an argument to the metamethod. +Here, to \emph{close} a value means +to call its @idx{__close} metamethod. +If the value is @nil, it is ignored; +otherwise, +if it does not have a @idx{__close} metamethod, +an error is raised. +When calling the metamethod, +the value itself is passed as the first argument +and the error object (if any) is passed as a second argument; +if there was no error, the second argument is @nil. If several to-be-closed variables go out of scope at the same event, they are closed in the reverse order that they were declared. -If there is any error while running a closing function, +If there is any error while running a closing method, that error is handled like an error in the regular code where the variable was defined; in particular, -the other pending closing functions will still be called. +the other pending closing methods will still be called. If a coroutine yields inside a block and is never resumed again, the variables visible at that block will never go out of scope, and therefore they will not be closed. -Similarly, if a script is interrupted by an unprotected error, -its to-be-closed variables will not be closed. +(You should use finalizers to handle this case.) } @@ -3002,7 +2998,7 @@ and therefore never returns } @APIEntry{int lua_gc (lua_State *L, int what, int data);| -@apii{0,0,v} +@apii{0,0,-} Controls the garbage collector. @@ -3056,8 +3052,6 @@ returns a boolean that tells whether the collector is running For more details about these options, see @Lid{collectgarbage}. -This function may raise errors when calling finalizers. - } @APIEntry{lua_Alloc lua_getallocf (lua_State *L, void **ud);| diff --git a/testes/api.lua b/testes/api.lua index 893a36cb75..9904dadfcc 100644 --- a/testes/api.lua +++ b/testes/api.lua @@ -396,6 +396,23 @@ do assert(string.find(msg, "stack overflow")) end + -- exit in panic still close to-be-closed variables + assert(T.checkpanic([[ + pushstring "return {__close = function () Y = 'ho'; end}" + newtable + loadstring -2 + call 0 1 + setmetatable -2 + toclose -1 + pushstring "hi" + error + ]], + [[ + getglobal Y + concat 2 # concat original error with global Y + ]]) == "hiho") + + end -- testing deep C stack @@ -1115,7 +1132,7 @@ end) testamem("to-be-closed variables", function() local flag do - local *toclose x = function () flag = true end + local *toclose x = setmetatable({}, {__close = function () flag = true end}) flag = false local x = {} end diff --git a/testes/coroutine.lua b/testes/coroutine.lua index ca30011ff5..a4321bedaf 100644 --- a/testes/coroutine.lua +++ b/testes/coroutine.lua @@ -142,8 +142,15 @@ do -- to-be-closed variables in coroutines local X + + local function func2close (f) + return setmetatable({}, {__close = f}) + end + co = coroutine.create(function () - local *toclose x = function (err) assert(err == nil); X = false end + local *toclose x = func2close(function (self, err) + assert(err == nil); X = false + end) X = true coroutine.yield() end) @@ -154,7 +161,9 @@ do -- error killing a coroutine co = coroutine.create(function() - local *toclose x = function (err) assert(err == nil); error(111) end + local *toclose x = func2close(function (self, err) + assert(err == nil); error(111) + end) coroutine.yield() end) coroutine.resume(co) diff --git a/testes/goto.lua b/testes/goto.lua index 5d863a428d..f3dcfd4a3e 100644 --- a/testes/goto.lua +++ b/testes/goto.lua @@ -258,7 +258,7 @@ do ::L2:: goto L3 ::L1:: do - local *toclose a = function () X = true end + local *toclose a = setmetatable({}, {__close = function () X = true end}) assert(X == nil) if a then goto L2 end -- jumping back out of scope of 'a' end diff --git a/testes/locals.lua b/testes/locals.lua index 340af61c0e..de47ae3182 100644 --- a/testes/locals.lua +++ b/testes/locals.lua @@ -177,13 +177,19 @@ print"testing to-be-closed variables" local function stack(n) n = ((n == 0) or stack(n - 1)) end +local function func2close (f) + return setmetatable({}, {__close = f}) +end + do local a = {} do local *toclose x = setmetatable({"x"}, {__close = function (self) a[#a + 1] = self[1] end}) - local *toclose y = function (x) assert(x == nil); a[#a + 1] = "y" end + local *toclose y = func2close(function (self, err) + assert(err == nil); a[#a + 1] = "y" + end) a[#a + 1] = "in" end a[#a + 1] = "out" @@ -193,7 +199,7 @@ end do local X = false - local function closescope () stack(10); X = true end + local closescope = func2close(function () stack(10); X = true end) -- closing functions do not corrupt returning values local function foo (x) @@ -228,13 +234,13 @@ do -- calls cannot be tail in the scope of to-be-closed variables local X, Y local function foo () - local *toclose _ = function () Y = 10 end + local *toclose _ = func2close(function () Y = 10 end) assert(X == true and Y == nil) -- 'X' not closed yet return 1,2,3 end local function bar () - local *toclose _ = function () X = false end + local *toclose _ = func2close(function () X = false end) X = true do return foo() -- not a tail call! @@ -249,11 +255,15 @@ end do -- errors in __close local log = {} local function foo (err) - local *toclose x = function (msg) log[#log + 1] = msg; error(1) end - local *toclose x1 = function (msg) log[#log + 1] = msg; end - local *toclose gc = function () collectgarbage() end - local *toclose y = function (msg) log[#log + 1] = msg; error(2) end - local *toclose z = function (msg) log[#log + 1] = msg or 10; error(3) end + local *toclose x = + func2close(function (self, msg) log[#log + 1] = msg; error(1) end) + local *toclose x1 = + func2close(function (self, msg) log[#log + 1] = msg; end) + local *toclose gc = func2close(function () collectgarbage() end) + local *toclose y = + func2close(function (self, msg) log[#log + 1] = msg; error(2) end) + local *toclose z = + func2close(function (self, msg) log[#log + 1] = msg or 10; error(3) end) if err then error(4) end end local stat, msg = pcall(foo, false) @@ -282,7 +292,7 @@ do -- with other errors, non-closable values are ignored local function foo () local *toclose x = 34 - local *toclose y = function () error(32) end + local *toclose y = func2close(function () error(32) end) end local stat, msg = pcall(foo) assert(not stat and msg == 32) @@ -294,7 +304,7 @@ if rawget(_G, "T") then -- memory error inside closing function local function foo () - local *toclose y = function () T.alloccount() end + local *toclose y = func2close(function () T.alloccount() end) local *toclose x = setmetatable({}, {__close = function () T.alloccount(0); local x = {} -- force a memory error end}) @@ -308,12 +318,12 @@ if rawget(_G, "T") then local _, msg = pcall(foo) assert(msg == "not enough memory") - local function close (msg) + local close = func2close(function (self, msg) T.alloccount() assert(msg == "not enough memory") - end + end) - -- set a memory limit and return a closing function to remove the limit + -- set a memory limit and return a closing object to remove the limit local function enter (count) stack(10) -- reserve some stack space T.alloccount(count) @@ -336,13 +346,13 @@ if rawget(_G, "T") then -- repeat test with extra closing upvalues local function test () - local *toclose xxx = function (msg) + local *toclose xxx = func2close(function (self, msg) assert(msg == "not enough memory"); error(1000) -- raise another error - end - local *toclose xx = function (msg) + end) + local *toclose xx = func2close(function (self, msg) assert(msg == "not enough memory"); - end + end) local *toclose x = enter(0) -- set a memory limit -- creation of previous upvalue will raise a memory error os.exit(false) -- should not run @@ -413,9 +423,9 @@ do local x = false local y = false local co = coroutine.create(function () - local *toclose xv = function () x = true end + local *toclose xv = func2close(function () x = true end) do - local *toclose yv = function () y = true end + local *toclose yv = func2close(function () y = true end) coroutine.yield(100) -- yield doesn't close variable end coroutine.yield(200) -- yield doesn't close variable @@ -453,9 +463,7 @@ do end, nil, -- state nil, -- control variable - function () -- closing function - numopen = numopen - 1 - end + func2close(function () numopen = numopen - 1 end) -- closing function end local s = 0 From 264659bd53e92969a1e17d65c0266597cde24b5d Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 8 Jan 2019 14:22:32 -0200 Subject: [PATCH 0376/1145] Optional 'init' argument to 'string.gmatch' The function 'string.gmatch' now has an optional 'init' argument, similar to 'string.find' and 'string.match'. Moreover, there was some reorganization in the manipulation of indices in the string library. This commit also includes small janitorial work in the manual and in comments in the interpreter loop. --- lstrlib.c | 76 +++++++++++++++++++++++++++++++--------------- lvm.c | 14 ++++----- manual/manual.of | 42 +++++++++++++------------ testes/pm.lua | 29 ++++++++++++++++++ testes/strings.lua | 5 +++ testes/tpack.lua | 2 -- 6 files changed, 116 insertions(+), 52 deletions(-) diff --git a/lstrlib.c b/lstrlib.c index dde868c082..41ebc52371 100644 --- a/lstrlib.c +++ b/lstrlib.c @@ -60,23 +60,50 @@ static int str_len (lua_State *L) { } -/* translate a relative string position: negative means back from end */ -static lua_Integer posrelat (lua_Integer pos, size_t len) { - if (pos >= 0) return pos; - else if (0u - (size_t)pos > len) return 0; - else return (lua_Integer)len + pos + 1; +/* +** translate a relative initial string position +** (negative means back from end): clip result to [1, inf). +** The length of any string in Lua must fit in a lua_Integer, +** so there are no overflows in the casts. +** The inverted comparison avoids a possible overflow +** computing '-pos'. +*/ +static size_t posrelatI (lua_Integer pos, size_t len) { + if (pos > 0) + return (size_t)pos; + else if (pos == 0) + return 1; + else if (pos < -(lua_Integer)len) /* inverted comparison */ + return 1; /* clip to 1 */ + else return len + (size_t)pos + 1; +} + + +/* +** Gets an optional ending string position from argument 'arg', +** with default value 'def'. +** Negative means back from end: clip result to [0, len] +*/ +static size_t getendpos (lua_State *L, int arg, lua_Integer def, + size_t len) { + lua_Integer pos = luaL_optinteger(L, arg, def); + if (pos > (lua_Integer)len) + return len; + else if (pos >= 0) + return (size_t)pos; + else if (pos < -(lua_Integer)len) + return 0; + else return len + (size_t)pos + 1; } static int str_sub (lua_State *L) { size_t l; const char *s = luaL_checklstring(L, 1, &l); - lua_Integer start = posrelat(luaL_checkinteger(L, 2), l); - lua_Integer end = posrelat(luaL_optinteger(L, 3, -1), l); - if (start < 1) start = 1; - if (end > (lua_Integer)l) end = l; + size_t start = posrelatI(luaL_checkinteger(L, 2), l); + size_t end = getendpos(L, 3, -1, l); if (start <= end) - lua_pushlstring(L, s + start - 1, (size_t)(end - start) + 1); + lua_pushlstring(L, s + start - 1, (end - start) + 1); else lua_pushliteral(L, ""); return 1; } @@ -149,11 +176,10 @@ static int str_rep (lua_State *L) { static int str_byte (lua_State *L) { size_t l; const char *s = luaL_checklstring(L, 1, &l); - lua_Integer posi = posrelat(luaL_optinteger(L, 2, 1), l); - lua_Integer pose = posrelat(luaL_optinteger(L, 3, posi), l); + lua_Integer pi = luaL_optinteger(L, 2, 1); + size_t posi = posrelatI(pi, l); + size_t pose = getendpos(L, 3, pi, l); int n, i; - if (posi < 1) posi = 1; - if (pose > (lua_Integer)l) pose = l; if (posi > pose) return 0; /* empty interval; return no values */ if (pose - posi >= INT_MAX) /* arithmetic overflow? */ return luaL_error(L, "string slice too long"); @@ -171,8 +197,8 @@ static int str_char (lua_State *L) { luaL_Buffer b; char *p = luaL_buffinitsize(L, &b, n); for (i=1; i<=n; i++) { - lua_Integer c = luaL_checkinteger(L, i); - luaL_argcheck(L, uchar(c) == c, i, "value out of range"); + lua_Unsigned c = (lua_Unsigned)luaL_checkinteger(L, i); + luaL_argcheck(L, c <= (lua_Unsigned)UCHAR_MAX, i, "value out of range"); p[i - 1] = uchar(c); } luaL_pushresultsize(&b, n); @@ -695,16 +721,15 @@ static int str_find_aux (lua_State *L, int find) { size_t ls, lp; const char *s = luaL_checklstring(L, 1, &ls); const char *p = luaL_checklstring(L, 2, &lp); - lua_Integer init = posrelat(luaL_optinteger(L, 3, 1), ls); - if (init < 1) init = 1; - else if (init > (lua_Integer)ls + 1) { /* start after string's end? */ + size_t init = posrelatI(luaL_optinteger(L, 3, 1), ls) - 1; + if (init > ls) { /* start after string's end? */ lua_pushnil(L); /* cannot find anything */ return 1; } /* explicit request or no special characters? */ if (find && (lua_toboolean(L, 4) || nospecials(p, lp))) { /* do a plain search */ - const char *s2 = lmemfind(s + init - 1, ls - (size_t)init + 1, p, lp); + const char *s2 = lmemfind(s + init, ls - init, p, lp); if (s2) { lua_pushinteger(L, (s2 - s) + 1); lua_pushinteger(L, (s2 - s) + lp); @@ -713,7 +738,7 @@ static int str_find_aux (lua_State *L, int find) { } else { MatchState ms; - const char *s1 = s + init - 1; + const char *s1 = s + init; int anchor = (*p == '^'); if (anchor) { p++; lp--; /* skip anchor character */ @@ -777,11 +802,14 @@ static int gmatch (lua_State *L) { size_t ls, lp; const char *s = luaL_checklstring(L, 1, &ls); const char *p = luaL_checklstring(L, 2, &lp); + size_t init = posrelatI(luaL_optinteger(L, 3, 1), ls) - 1; GMatchState *gm; - lua_settop(L, 2); /* keep them on closure to avoid being collected */ + lua_settop(L, 2); /* keep strings on closure to avoid being collected */ gm = (GMatchState *)lua_newuserdatauv(L, sizeof(GMatchState), 0); + if (init > ls) /* start after string's end? */ + init = ls + 1; /* avoid overflows in 's + init' */ prepstate(&gm->ms, L, s, ls, p, lp); - gm->src = s; gm->p = p; gm->lastmatch = NULL; + gm->src = s + init; gm->p = p; gm->lastmatch = NULL; lua_pushcclosure(L, gmatch_aux, 3); return 1; } @@ -1572,7 +1600,7 @@ static int str_unpack (lua_State *L) { const char *fmt = luaL_checkstring(L, 1); size_t ld; const char *data = luaL_checklstring(L, 2, &ld); - size_t pos = (size_t)posrelat(luaL_optinteger(L, 3, 1), ld) - 1; + size_t pos = posrelatI(luaL_optinteger(L, 3, 1), ld) - 1; int n = 0; /* number of results */ luaL_argcheck(L, pos <= ld, 3, "initial position out of string"); initheader(L, &h); diff --git a/lvm.c b/lvm.c index 652095dcd0..23e7ff700c 100644 --- a/lvm.c +++ b/lvm.c @@ -991,7 +991,8 @@ void luaV_finishOp (lua_State *L) { /* ** Protect code that will finish the loop (returns) or can only raise -** errors. +** errors. (That is, it will not return to the interpreter main loop +** after changing the stack or hooks.) */ #define halfProtect(exp) (savepc(L), (exp)) @@ -1607,7 +1608,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { L->top = ra; halfProtect(luaD_poscall(L, ci, 0)); /* no hurry... */ } - else { + else { /* do the 'poscall' here */ int nres = ci->nresults; L->ci = ci->previous; /* back to caller */ L->top = base - 1; @@ -1621,7 +1622,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { L->top = ra + 1; halfProtect(luaD_poscall(L, ci, 1)); /* no hurry... */ } - else { + else { /* do the 'poscall' here */ int nres = ci->nresults; L->ci = ci->previous; /* back to caller */ if (nres == 0) @@ -1652,8 +1653,8 @@ void luaV_execute (lua_State *L, CallInfo *ci) { lua_Integer ilimit, initv; int stopnow; if (unlikely(!forlimit(plimit, &ilimit, 1, &stopnow))) { - savestate(L, ci); /* for the error message */ - luaG_forerror(L, plimit, "limit"); + savestate(L, ci); /* for the error message */ + luaG_forerror(L, plimit, "limit"); } initv = (stopnow ? 0 : ivalue(init)); setivalue(plimit, ilimit); @@ -1717,8 +1718,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_TFORPREP) { - /* is 'toclose' not nil? */ - if (!ttisnil(s2v(ra + 3))) { + if (!ttisnil(s2v(ra + 3))) { /* is 'toclose' not nil? */ /* create to-be-closed upvalue for it */ halfProtect(luaF_newtbcupval(L, ra + 3)); } diff --git a/manual/manual.of b/manual/manual.of index b9ab1ebe52..421d04de87 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -83,25 +83,10 @@ it usually represents the absence of a useful value. The type @emph{boolean} has two values, @false and @true. Both @nil and @false make a condition false; any other value makes it true. -The type @emph{number} represents both -integer numbers and real (floating-point) numbers. -The type @emph{string} represents immutable sequences of bytes. -@index{eight-bit clean} -Lua is 8-bit clean: -strings can contain any 8-bit value, -including @x{embedded zeros} (@Char{\0}). -Lua is also encoding-agnostic; -it makes no assumptions about the contents of a string. -The type @emph{number} uses two internal representations, -or two @x{subtypes}, -one called @def{integer} and the other called @def{float}. -Lua has explicit rules about when each representation is used, -but it also converts between them automatically as needed @see{coercion}. -Therefore, -the programmer may choose to mostly ignore the difference -between integers and floats -or to assume complete control over the representation of each number. +The type @emph{number} represents both +integer numbers and real (floating-point) numbers, +using two @x{subtypes}: @def{integer} and @def{float}. Standard Lua uses 64-bit integers and double-precision (64-bit) floats, but you can also compile Lua so that it uses 32-bit integers and/or single-precision (32-bit) floats. @@ -110,6 +95,22 @@ is particularly attractive for small machines and embedded systems. (See macro @id{LUA_32BITS} in file @id{luaconf.h}.) +Lua has explicit rules about when each subtype is used, +but it also converts between them automatically as needed @see{coercion}. +Therefore, +the programmer may choose to mostly ignore the difference +between integers and floats +or to assume complete control over the representation of each number. + +The type @emph{string} represents immutable sequences of bytes. +@index{eight-bit clean} +Lua is 8-bit clean: +strings can contain any 8-bit value, +including @x{embedded zeros} (@Char{\0}). +Lua is also encoding-agnostic; +it makes no assumptions about the contents of a string. +The length of any string in Lua must fit in a Lua integer. + Lua can call (and manipulate) functions written in Lua and functions written in C @see{functioncall}. Both are represented by the type @emph{function}. @@ -6788,13 +6789,16 @@ the string argument should not contain @x{embedded zeros}. } -@LibEntry{string.gmatch (s, pattern)| +@LibEntry{string.gmatch (s, pattern [, init])| Returns an iterator function that, each time it is called, returns the next captures from @id{pattern} @see{pm} over the string @id{s}. If @id{pattern} specifies no captures, then the whole match is produced in each call. +A third, optional numeric argument @id{init} specifies +where to start the search; +its default value @N{is 1} and can be negative. As an example, the following loop will iterate over all the words from string @id{s}, diff --git a/testes/pm.lua b/testes/pm.lua index 1afaccf694..8cc8772e87 100644 --- a/testes/pm.lua +++ b/testes/pm.lua @@ -297,6 +297,35 @@ for k,v in pairs(t) do assert(k+1 == v+0); a=a+1 end assert(a == 3) +do -- init parameter in gmatch + local s = 0 + for k in string.gmatch("10 20 30", "%d+", 3) do + s = s + tonumber(k) + end + assert(s == 50) + + s = 0 + for k in string.gmatch("11 21 31", "%d+", -4) do + s = s + tonumber(k) + end + assert(s == 32) + + -- there is an empty string at the end of the subject + s = 0 + for k in string.gmatch("11 21 31", "%w*", 9) do + s = s + 1 + end + assert(s == 1) + + -- there are no empty strings after the end of the subject + s = 0 + for k in string.gmatch("11 21 31", "%w*", 10) do + s = s + 1 + end + assert(s == 0) +end + + -- tests for `%f' (`frontiers') assert(string.gsub("aaa aa a aaa a", "%f[%w]a", "x") == "xaa xa x xaa x") diff --git a/testes/strings.lua b/testes/strings.lua index 587a0e06d3..884809249a 100644 --- a/testes/strings.lua +++ b/testes/strings.lua @@ -94,6 +94,11 @@ assert(string.char(string.byte("\xe4l\0 assert(string.char(string.byte("\xe4l\0u", 1, 0)) == "") assert(string.char(string.byte("\xe4l\0u", -10, 100)) == "\xe4l\0u") +checkerror("out of range", string.char, 256) +checkerror("out of range", string.char, -1) +checkerror("out of range", string.char, math.maxinteger) +checkerror("out of range", string.char, math.mininteger) + assert(string.upper("ab\0c") == "AB\0C") assert(string.lower("\0ABCc%$") == "\0abcc%$") assert(string.rep('teste', 0) == '') diff --git a/testes/tpack.lua b/testes/tpack.lua index 4c5fc7f74d..2b9953f831 100644 --- a/testes/tpack.lua +++ b/testes/tpack.lua @@ -314,9 +314,7 @@ do -- testing initial position for i = 1, #x + 1 do assert(unpack("c0", x, i) == "") end - checkerror("out of string", unpack, "c0", x, 0) checkerror("out of string", unpack, "c0", x, #x + 2) - checkerror("out of string", unpack, "c0", x, -(#x + 1)) end From 2c32bff60987d38a60a58d4f0123f3783da60a63 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 30 Jan 2019 11:44:42 -0200 Subject: [PATCH 0377/1145] After a "bad collections", avoid switching back back to generational After a major bad collection (one that collects too few objects), next collection will be major again. In that case, avoid switching back to generational mode (as it will have to switch again to incremental to do next major collection). --- lapi.c | 6 +- lgc.c | 158 ++++++++++++++++++++++++++++++++++++-------------- lgc.h | 9 ++- lstate.c | 4 +- lstate.h | 1 + testes/gc.lua | 6 ++ 6 files changed, 134 insertions(+), 50 deletions(-) diff --git a/lapi.c b/lapi.c index 8ff7bfbd30..4026497e00 100644 --- a/lapi.c +++ b/lapi.c @@ -1141,22 +1141,21 @@ LUA_API int lua_gc (lua_State *L, int what, ...) { break; } case LUA_GCGEN: { - int oldmode = g->gckind; int minormul = va_arg(argp, int); int majormul = va_arg(argp, int); + res = isdecGCmodegen(g) ? LUA_GCGEN : LUA_GCINC; if (minormul != 0) g->genminormul = minormul; if (majormul != 0) setgcparam(g->genmajormul, majormul); luaC_changemode(L, KGC_GEN); - res = (oldmode == KGC_GEN) ? LUA_GCGEN : LUA_GCINC; break; } case LUA_GCINC: { - int oldmode = g->gckind; int pause = va_arg(argp, int); int stepmul = va_arg(argp, int); int stepsize = va_arg(argp, int); + res = isdecGCmodegen(g) ? LUA_GCGEN : LUA_GCINC; if (pause != 0) setgcparam(g->gcpause, pause); if (stepmul != 0) @@ -1164,7 +1163,6 @@ LUA_API int lua_gc (lua_State *L, int what, ...) { if (stepsize != 0) g->gcstepsize = stepsize; luaC_changemode(L, KGC_INC); - res = (oldmode == KGC_GEN) ? LUA_GCGEN : LUA_GCINC; break; } default: res = -1; /* invalid option */ diff --git a/lgc.c b/lgc.c index 0c3386e745..bf0460d549 100644 --- a/lgc.c +++ b/lgc.c @@ -101,6 +101,7 @@ static void reallymarkobject (global_State *g, GCObject *o); static lu_mem atomic (lua_State *L); +static void entersweep (lua_State *L); /* @@ -1162,15 +1163,7 @@ static void youngcollection (lua_State *L, global_State *g) { } -/* -** Enter generational mode. Must go until the end of an atomic cycle -** to ensure that all threads are in the gray list. Then, turn all -** objects into old and finishes the collection. -*/ -static void entergen (lua_State *L, global_State *g) { - luaC_runtilstate(L, bitmask(GCSpause)); /* prepare to start a new cycle */ - luaC_runtilstate(L, bitmask(GCSpropagate)); /* start new cycle */ - atomic(L); +static void atomic2gen (lua_State *L, global_State *g) { /* sweep all elements making them old */ sweep2old(L, &g->allgc); /* everything alive now is old */ @@ -1183,15 +1176,31 @@ static void entergen (lua_State *L, global_State *g) { sweep2old(L, &g->tobefnz); g->gckind = KGC_GEN; + g->lastatomic = 0; g->GCestimate = gettotalbytes(g); /* base for memory control */ finishgencycle(L, g); } +/* +** Enter generational mode. Must go until the end of an atomic cycle +** to ensure that all threads and weak tables are in the gray lists. +** Then, turn all objects into old and finishes the collection. +*/ +static lu_mem entergen (lua_State *L, global_State *g) { + lu_mem numobjs; + luaC_runtilstate(L, bitmask(GCSpause)); /* prepare to start a new cycle */ + luaC_runtilstate(L, bitmask(GCSpropagate)); /* start new cycle */ + numobjs = atomic(L); /* propagates all and then do the atomic stuff */ + atomic2gen(L, g); + return numobjs; +} + + /* ** Enter incremental mode. Turn all objects white, make all ** intermediate lists point to NULL (to avoid invalid pointers), -** and go to pause state. +** and go to the pause state. */ static void enterinc (global_State *g) { whitelist(g, g->allgc); @@ -1201,6 +1210,7 @@ static void enterinc (global_State *g) { g->finobjrold = g->finobjold = g->finobjsur = NULL; g->gcstate = GCSpause; g->gckind = KGC_INC; + g->lastatomic = 0; } @@ -1215,54 +1225,114 @@ void luaC_changemode (lua_State *L, int newmode) { else enterinc(g); /* entering incremental mode */ } + g->lastatomic = 0; } /* ** Does a full collection in generational mode. */ -static void fullgen (lua_State *L, global_State *g) { +static lu_mem fullgen (lua_State *L, global_State *g) { enterinc(g); - entergen(L, g); + return entergen(L, g); +} + + +/* +** Set debt for the next minor collection, which will happen when +** memory grows 'genminormul'%. +*/ +static void setminordebt (global_State *g) { + luaE_setdebt(g, -(cast(l_mem, (gettotalbytes(g) / 100)) * g->genminormul)); } /* -** Does a generational "step". If memory grows 'genmajormul'% larger -** than last major collection (kept in 'g->GCestimate'), does a major -** collection. Otherwise, does a minor collection and set debt to make -** another collection when memory grows 'genminormul'% larger. -** When it does a major collection, it then checks whether it could -** reclaim at least ?? memory. If not, it sets a long pause for the -** next collection. (Therefore, the next collection will be a major -** one, too.) +** Does a major collection after last collection was a "bad collection". +** +** When the program is building a big struture, it allocates lots of +** memory but generates very little garbage. In those scenarios, +** the generational mode just wastes time doing small collections, and +** major collections are frequently what we call a "bad collection", a +** collection that frees too few objects. To avoid the cost of switching +** between generational mode and the incremental mode needed for full +** (major) collections, the collector tries to stay in incremental mode +** after a bad collection, and to switch back to generational mode only +** after a "good" collection (one that traverses less than 9/8 objects +** of the previous one). +** The collector must choose whether to stay in incremental mode or to +** switch back to generational mode before sweeping. At this point, it +** does not know the real memory in use, so it cannot use memory to +** decide whether to return to generational mode. Instead, it uses the +** number of objects traversed (returned by 'atomic') as a proxy. The +** field 'g->lastatomic' keeps this count from the last collection. +** ('g->lastatomic != 0' also means that the last collection was bad.) +*/ +static void stepgenfull (lua_State *L, global_State *g) { + lu_mem newatomic; /* count of traversed objects */ + lu_mem lastatomic = g->lastatomic; /* count from last collection */ + if (g->gckind == KGC_GEN) /* still in generational mode? */ + enterinc(g); /* enter incremental mode */ + luaC_runtilstate(L, bitmask(GCSpropagate)); /* start new cycle */ + newatomic = atomic(L); /* mark everybody */ + if (newatomic < lastatomic + (lastatomic >> 3)) { /* good collection? */ + atomic2gen(L, g); /* return to generational mode */ + setminordebt(g); + } + else { /* another bad collection; stay in incremental mode */ + g->GCestimate = gettotalbytes(g); /* first estimate */; + entersweep(L); + luaC_runtilstate(L, bitmask(GCSpause)); /* finish collection */ + setpause(g); + g->lastatomic = newatomic; + } +} + + +/* +** Does a generational "step". +** Usually, this means doing a minor collection and setting the debt to +** make another collection when memory grows 'genminormul'% larger. +** +** However, there are exceptions. If memory grows 'genmajormul'% +** larger than it was at the end of the last major collection (kept +** in 'g->GCestimate'), the function does a major collection. At the +** end, it checks whether the major collection was able to free a +** decent amount of memory (at least half the growth in memory since +** previous major collection). If so, the collector keeps its state, +** and the next collection will probably be minor again. Otherwise, +** we have what we call a "bad collection". In that case, set the field +** 'g->lastatomic' to signal that fact, so that the next collection will +** go to 'stepgenfull'. +** ** 'GCdebt <= 0' means an explicit call to GC step with "size" zero; -** in that case, always do a minor collection. +** in that case, do a minor collection. */ static void genstep (lua_State *L, global_State *g) { - lu_mem majorbase = g->GCestimate; /* memory after last major collection */ - lu_mem majorinc = (majorbase / 100) * getgcparam(g->genmajormul); - lu_mem memnew = gettotalbytes(g); - if (g->GCdebt > 0 && memnew > majorbase + majorinc) { - fullgen(L, g); - memnew = gettotalbytes(g); - if (memnew < majorbase + (majorinc / 2)) { - /* collected at least half of memory growth since last major - collection; go back to minor collections */ - luaE_setdebt(g, -(cast(l_mem, (memnew / 100)) * g->genminormul)); + if (g->lastatomic != 0) /* last collection was a bad one? */ + stepgenfull(L, g); /* do a full step */ + else { + lu_mem majorbase = g->GCestimate; /* memory after last major collection */ + lu_mem majorinc = (majorbase / 100) * getgcparam(g->genmajormul); + if (g->GCdebt > 0 && gettotalbytes(g) > majorbase + majorinc) { + lu_mem numobjs = fullgen(L, g); /* do a major collection */ + if (gettotalbytes(g) < majorbase + (majorinc / 2)) { + /* collected at least half of memory growth since last major + collection; keep doing minor collections */ + setminordebt(g); + } + else { /* bad collection */ + g->lastatomic = numobjs; /* signal that last collection was bad */ + setpause(g); /* do a long wait for next (major) collection */ + } } - else { - /* memory seems to be growing; do a long wait for next (major) - collection */ - setpause(g); + else { /* regular case; do a minor collection */ + youngcollection(L, g); + setminordebt(g); + g->GCestimate = majorbase; /* preserve base value */ } } - else { - youngcollection(L, g); - memnew = gettotalbytes(g); - luaE_setdebt(g, -(cast(l_mem, (memnew / 100)) * g->genminormul)); - g->GCestimate = majorbase; /* preserve base value */ - } + lua_assert(isdecGCmodegen(g)); } /* }====================================================== */ @@ -1493,10 +1563,10 @@ static void incstep (lua_State *L, global_State *g) { void luaC_step (lua_State *L) { global_State *g = G(L); if (g->gcrunning) { /* running? */ - if (g->gckind == KGC_INC) - incstep(L, g); - else + if(isdecGCmodegen(g)) genstep(L, g); + else + incstep(L, g); } } diff --git a/lgc.h b/lgc.h index 6b1c286153..9ba7ecb050 100644 --- a/lgc.h +++ b/lgc.h @@ -123,7 +123,7 @@ #define LUAI_GENMINORMUL 20 /* wait memory to double before starting new cycle */ -#define LUAI_GCPAUSE 200 /* 200% */ +#define LUAI_GCPAUSE 200 /* ** some gc parameters are stored divided by 4 to allow a maximum value @@ -138,6 +138,13 @@ #define LUAI_GCSTEPSIZE 13 /* 8 KB */ +/* +** Check whether the declared GC mode is generational. While in +** generational mode, the collector can go temporarily to incremental +** mode to improve performance. This is signaled by 'g->lastatomic != 0'. +*/ +#define isdecGCmodegen(g) (g->gckind == KGC_GEN || g->lastatomic != 0) + /* ** Does one step of collection when debt becomes positive. 'pre'/'pos' ** allows some adjustments to be done only when needed. macro diff --git a/lstate.c b/lstate.c index 7f6475a881..6c35ea1a6c 100644 --- a/lstate.c +++ b/lstate.c @@ -233,7 +233,8 @@ static void init_registry (lua_State *L, global_State *g) { /* ** open parts of the state that may cause memory-allocation errors. -** ('ttisnil(&g->nilvalue)'' flags that the state was completely build) +** ('g->nilvalue' being a nil value flags that the state was completely +** build.) */ static void f_luaopen (lua_State *L, void *ud) { global_State *g = G(L); @@ -386,6 +387,7 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { g->twups = NULL; g->totalbytes = sizeof(LG); g->GCdebt = 0; + g->lastatomic = 0; setivalue(&g->nilvalue, 0); /* to signal that state is not yet built */ setgcparam(g->gcpause, LUAI_GCPAUSE); setgcparam(g->gcstepmul, LUAI_GCMUL); diff --git a/lstate.h b/lstate.h index 05a74dda85..11bf18fde2 100644 --- a/lstate.h +++ b/lstate.h @@ -193,6 +193,7 @@ typedef struct global_State { l_mem totalbytes; /* number of bytes currently allocated - GCdebt */ l_mem GCdebt; /* bytes allocated not yet compensated by the collector */ lu_mem GCestimate; /* an estimate of the non-garbage memory in use */ + lu_mem lastatomic; /* see function 'genstep' in file 'lgc.c' */ stringtable strt; /* hash table for strings */ TValue l_registry; TValue nilvalue; /* a nil value */ diff --git a/testes/gc.lua b/testes/gc.lua index 84e8ffb700..05bf564e5c 100644 --- a/testes/gc.lua +++ b/testes/gc.lua @@ -11,6 +11,12 @@ collectgarbage() local oldmode = collectgarbage("incremental") +-- changing modes should return previous mode +assert(collectgarbage("generational") == "incremental") +assert(collectgarbage("generational") == "generational") +assert(collectgarbage("incremental") == "generational") +assert(collectgarbage("incremental") == "incremental") + local function gcinfo () return collectgarbage"count" * 1024 From cf71a5ddc742692fad813f89f1c9ef53e1ffde0f Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 13 Mar 2019 13:16:53 -0300 Subject: [PATCH 0378/1145] Details Several small improvements (code style, warnings, comments, more tests), in particular: - 'lua_topointer' extended to handle strings - raises an error in 'string.format("%10q")' ('%q' with modifiers) - in the manual for 'string.format', the term "option" replaced by "conversion specifier" (the term used by the C standard) --- lapi.c | 31 ++++++++++++++++++-------- lfunc.c | 3 ++- lopcodes.h | 15 ++++++------- lstrlib.c | 6 ++++-- ltests.c | 7 ++++-- manual/manual.of | 54 +++++++++++++++++++++++++++------------------- testes/api.lua | 21 ++++++++++++------ testes/strings.lua | 1 + 8 files changed, 87 insertions(+), 51 deletions(-) diff --git a/lapi.c b/lapi.c index 4026497e00..66d7564945 100644 --- a/lapi.c +++ b/lapi.c @@ -414,8 +414,7 @@ LUA_API lua_CFunction lua_tocfunction (lua_State *L, int idx) { } -LUA_API void *lua_touserdata (lua_State *L, int idx) { - const TValue *o = index2value(L, idx); +static void *touserdata (const TValue *o) { switch (ttype(o)) { case LUA_TUSERDATA: return getudatamem(uvalue(o)); case LUA_TLIGHTUSERDATA: return pvalue(o); @@ -424,23 +423,37 @@ LUA_API void *lua_touserdata (lua_State *L, int idx) { } +LUA_API void *lua_touserdata (lua_State *L, int idx) { + const TValue *o = index2value(L, idx); + return touserdata(o); +} + + LUA_API lua_State *lua_tothread (lua_State *L, int idx) { const TValue *o = index2value(L, idx); return (!ttisthread(o)) ? NULL : thvalue(o); } +/* +** Returns a pointer to the internal representation of an object. +** Note that ANSI C does not allow the conversion of a pointer to +** function to a 'void*', so the conversion here goes through +** a 'size_t'. (As the returned pointer is only informative, this +** conversion should not be a problem.) +*/ LUA_API const void *lua_topointer (lua_State *L, int idx) { const TValue *o = index2value(L, idx); switch (ttypetag(o)) { - case LUA_TTABLE: return hvalue(o); - case LUA_TLCL: return clLvalue(o); - case LUA_TCCL: return clCvalue(o); case LUA_TLCF: return cast_voidp(cast_sizet(fvalue(o))); - case LUA_TTHREAD: return thvalue(o); - case LUA_TUSERDATA: return getudatamem(uvalue(o)); - case LUA_TLIGHTUSERDATA: return pvalue(o); - default: return NULL; + case LUA_TUSERDATA: case LUA_TLIGHTUSERDATA: + return touserdata(o); + default: { + if (iscollectable(o)) + return gcvalue(o); + else + return NULL; + } } } diff --git a/lfunc.c b/lfunc.c index 362b798cba..3e044b65b2 100644 --- a/lfunc.c +++ b/lfunc.c @@ -138,7 +138,8 @@ static int callclosemth (lua_State *L, TValue *uv, StkId level, int status) { if (prepclosingmethod(L, uv, &G(L)->nilvalue)) /* something to call? */ callclose(L, NULL); /* call closing method */ else if (!ttisnil(uv)) { /* non-closable non-nil value? */ - const char *vname = luaG_findlocal(L, L->ci, level - L->ci->func, NULL); + int idx = cast_int(level - L->ci->func); + const char *vname = luaG_findlocal(L, L->ci, idx, NULL); if (vname == NULL) vname = "?"; luaG_runerror(L, "attempt to close non-closable variable '%s'", vname); } diff --git a/lopcodes.h b/lopcodes.h index d7403caf4b..3e100259b7 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -90,7 +90,6 @@ enum OpMode {iABC, iABx, iAsBx, iAx, isJ}; /* basic instruction formats */ #define MAXARG_B ((1<> 1) -#define MAXARG_Cx ((1<<(SIZE_C + 1))-1) /* creates a mask with 'n' 1 bits at position 'p' */ @@ -233,8 +232,8 @@ OP_BANDK,/* A B C R(A) := R(B) & K(C):integer */ OP_BORK,/* A B C R(A) := R(B) | K(C):integer */ OP_BXORK,/* A B C R(A) := R(B) ~ K(C):integer */ -OP_SHRI,/* A B C R(A) := R(B) >> C */ -OP_SHLI,/* A B C R(A) := C << R(B) */ +OP_SHRI,/* A B sC R(A) := R(B) >> C */ +OP_SHLI,/* A B sC R(A) := C << R(B) */ OP_ADD,/* A B C R(A) := R(B) + R(C) */ OP_SUB,/* A B C R(A) := R(B) - R(C) */ @@ -272,7 +271,7 @@ OP_GTI,/* A sB if ((R(A) > sB) ~= k) then pc++ */ OP_GEI,/* A sB if ((R(A) >= sB) ~= k) then pc++ */ OP_TEST,/* A if (not R(A) == k) then pc++ */ -OP_TESTSET,/* A B if (not R(B) == k) then R(A) := R(B) else pc++ */ +OP_TESTSET,/* A B if (not R(B) == k) then pc++ else R(A) := R(B) */ OP_CALL,/* A B C R(A), ... ,R(A+C-2) := R(A)(R(A+1), ... ,R(A+B-1)) */ OP_TAILCALL,/* A B C return R(A)(R(A+1), ... ,R(A+B-1)) */ @@ -305,15 +304,15 @@ OP_EXTRAARG/* Ax extra (larger) argument for previous opcode */ } OpCode; -#define NUM_OPCODES (cast_int(OP_EXTRAARG) + 1) +#define NUM_OPCODES ((int)(OP_EXTRAARG) + 1) /*=========================================================================== Notes: - (*) In OP_CALL, if (B == 0) then B = top. If (C == 0), then 'top' is - set to last_result+1, so next open instruction (OP_CALL, OP_RETURN*, - OP_SETLIST) may use 'top'. + (*) In OP_CALL, if (B == 0) then B = top - A. If (C == 0), then + 'top' is set to last_result+1, so next open instruction (OP_CALL, + OP_RETURN*, OP_SETLIST) may use 'top'. (*) In OP_VARARG, if (C == 0) then use actual number of varargs and set top (like in OP_CALL with C == 0). diff --git a/lstrlib.c b/lstrlib.c index 41ebc52371..ab4258e5b1 100644 --- a/lstrlib.c +++ b/lstrlib.c @@ -181,7 +181,7 @@ static int str_byte (lua_State *L) { size_t pose = getendpos(L, 3, pi, l); int n, i; if (posi > pose) return 0; /* empty interval; return no values */ - if (pose - posi >= INT_MAX) /* arithmetic overflow? */ + if (pose - posi >= (size_t)INT_MAX) /* arithmetic overflow? */ return luaL_error(L, "string slice too long"); n = (int)(pose - posi) + 1; luaL_checkstack(L, n, "string slice too long"); @@ -1159,7 +1159,7 @@ static int str_format (lua_State *L) { char *buff = luaL_prepbuffsize(&b, MAX_ITEM); /* to put formatted item */ int nb = 0; /* number of bytes in added item */ if (++arg > top) - luaL_argerror(L, arg, "no value"); + return luaL_argerror(L, arg, "no value"); strfrmt = scanformat(L, strfrmt, form); switch (*strfrmt++) { case 'c': { @@ -1186,6 +1186,8 @@ static int str_format (lua_State *L) { break; } case 'q': { + if (form[2] != '\0') /* modifiers? */ + return luaL_error(L, "specifier '%%q' cannot have modifiers"); addliteral(L, &b, arg); break; } diff --git a/ltests.c b/ltests.c index 36a974aeb6..233753820d 100644 --- a/ltests.c +++ b/ltests.c @@ -164,7 +164,7 @@ typedef union Header { Memcontrol l_memcontrol = - {0L, 0L, 0L, 0L, (~0L), {0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L}}; + {0UL, 0UL, 0UL, 0UL, (~0UL), {0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL}}; static void freeblock (Memcontrol *mc, Header *block) { @@ -1596,7 +1596,10 @@ static struct X { int x; } x; lua_pushnumber(L1, lua_tonumber(L1, getindex)); } else if EQ("topointer") { - lua_pushnumber(L1, cast_sizet(lua_topointer(L1, getindex))); + lua_pushlightuserdata(L1, cast_voidp(lua_topointer(L1, getindex))); + } + else if EQ("touserdata") { + lua_pushlightuserdata(L1, lua_touserdata(L1, getindex)); } else if EQ("tostring") { const char *s = lua_tostring(L1, getindex); diff --git a/manual/manual.of b/manual/manual.of index 421d04de87..9c8ab033ec 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -143,7 +143,7 @@ that is, @x{arrays} that can have as indices not only numbers, but any Lua value except @nil and @x{NaN}. (@emphx{Not a Number} is a special floating-point value used by the @x{IEEE 754} standard to represent -undefined or unrepresentable numerical results, such as @T{0/0}.) +undefined numerical results, such as @T{0/0}.) Tables can be @emph{heterogeneous}; that is, they can contain values of all types (except @nil). Any key with value @nil is not considered part of the table. @@ -670,8 +670,8 @@ are called when the garbage collector detects that the corresponding table or userdata is unreachable. Finalizers allow you to coordinate Lua's garbage collection with external resource management -(such as closing files, network or database connections, -or freeing your own memory). +such as closing files, network or database connections, +or freeing your own memory. For an object (table or userdata) to be finalized when collected, you must @emph{mark} it for finalization. @@ -1323,11 +1323,12 @@ labels in Lua are considered statements too: } A label is visible in the entire block where it is defined, -except -inside nested blocks where a label with the same name is defined and -inside nested functions. +except inside nested functions. A goto may jump to any visible label as long as it does not enter into the scope of a local variable. +A label should not be declared +where a label with the same name is visible, +even if this other label has been declared in an enclosing block. Labels and empty statements are called @def{void statements}, as they perform no actions. @@ -1537,7 +1538,7 @@ goes out of scope, including normal block termination, exiting its block by @Rw{break}/@Rw{goto}/@Rw{return}, or exiting by an error. -Here, to \emph{close} a value means +Here, to @emph{close} a value means to call its @idx{__close} metamethod. If the value is @nil, it is ignored; otherwise, @@ -4236,7 +4237,7 @@ indicates whether the operation succeeded. Converts the value at the given index to a generic @N{C pointer} (@T{void*}). -The value can be a userdata, a table, a thread, or a function; +The value can be a userdata, a table, a thread, a string, or a function; otherwise, @id{lua_topointer} returns @id{NULL}. Different objects will give different pointers. There is no way to convert the pointer back to its original value. @@ -6712,8 +6713,10 @@ to save space. Functions with upvalues have only their number of upvalues saved. When (re)loaded, -those upvalues receive fresh instances containing @nil. -(You can use the debug library to serialize +those upvalues receive fresh instances. +(See the @Lid{load} function for details about +how these upvalues are initialized. +You can use the debug library to serialize and reload the upvalues of a function in a way adequate to your needs.) @@ -6747,12 +6750,12 @@ after the two indices. Returns a formatted version of its variable number of arguments following the description given in its first argument (which must be a string). The format string follows the same rules as the @ANSI{sprintf}. -The only differences are that the options/modifiers +The only differences are that the conversion specifiers and modifiers @T{*}, @id{h}, @id{L}, @id{l}, @id{n}, and @id{p} are not supported -and that there is an extra option, @id{q}. +and that there is an extra specifier, @id{q}. -The @id{q} option formats booleans, nil, numbers, and strings +The specifier @id{q} formats booleans, nil, numbers, and strings in a way that the result is a valid constant in Lua source code. Booleans and nil are written in the obvious way (@id{true}, @id{false}, @id{nil}). @@ -6770,22 +6773,23 @@ may produce the string: "a string with \"quotes\" and \ new line" } +This specifier does not support modifiers (flags, width, length). -Options +The conversion specifiers @id{A}, @id{a}, @id{E}, @id{e}, @id{f}, @id{G}, and @id{g} all expect a number as argument. -Options @id{c}, @id{d}, +The specifiers @id{c}, @id{d}, @id{i}, @id{o}, @id{u}, @id{X}, and @id{x} expect an integer. When Lua is compiled with a C89 compiler, -options @id{A} and @id{a} (hexadecimal floats) -do not support any modifier (flags, width, length). +the specifiers @id{A} and @id{a} (hexadecimal floats) +do not support modifiers. -Option @id{s} expects a string; +The specifier @id{s} expects a string; if its argument is not a string, it is converted to one following the same rules of @Lid{tostring}. -If the option has any modifier (flags, width, length), -the string argument should not contain @x{embedded zeros}. +If the specifier has any modifier, +the corresponding string argument should not contain @x{embedded zeros}. } @@ -8009,8 +8013,8 @@ or there is any input from some special files } } -For the last two cases, @id{size} -specifies the size of the buffer, in bytes. +For the last two cases, +@id{size} is a hint for the size of the buffer, in bytes. The default is an appropriate size. } @@ -8698,6 +8702,12 @@ When a coroutine finishes with an error, its stack is unwound (to run any pending closing methods). } +@item{ +A label for a @Rw{goto} cannot be declared where a label with the same +name is visible, even if this other label is declared in an enclosing +block. +} + } } diff --git a/testes/api.lua b/testes/api.lua index 9904dadfcc..d034ea80ab 100644 --- a/testes/api.lua +++ b/testes/api.lua @@ -332,6 +332,7 @@ function to (s, x, n) return T.testC(string.format("%s %d; return 1", s, n), x) end +local null = T.pushuserdata(0) local hfunc = string.gmatch("", "") -- a "heavy C function" (with upvalues) assert(debug.getupvalue(hfunc, 1)) assert(to("tostring", {}) == nil) @@ -349,13 +350,19 @@ assert(to("tonumber", {}) == 0) assert(to("tonumber", "12") == 12) assert(to("tonumber", "s2") == 0) assert(to("tonumber", 1, 20) == 0) -assert(to("topointer", 10) == 0) -assert(to("topointer", true) == 0) -assert(to("topointer", T.pushuserdata(20)) == 20) -assert(to("topointer", io.read) ~= 0) -- light C function -assert(to("topointer", hfunc) ~= 0) -- "heavy" C function -assert(to("topointer", function () end) ~= 0) -- Lua function -assert(to("topointer", io.stdin) ~= 0) -- full userdata +assert(to("topointer", 10) == null) +assert(to("topointer", true) == null) +assert(to("topointer", nil) == null) +assert(to("topointer", "abc") ~= null) +assert(to("topointer", string.rep("x", 10)) == + to("topointer", string.rep("x", 10))) -- short strings +assert(to("topointer", string.rep("x", 300)) ~= + to("topointer", string.rep("x", 300))) -- long strings +assert(to("topointer", T.pushuserdata(20)) ~= null) +assert(to("topointer", io.read) ~= null) -- light C function +assert(to("topointer", hfunc) ~= null) -- "heavy" C function +assert(to("topointer", function () end) ~= null) -- Lua function +assert(to("topointer", io.stdin) ~= null) -- full userdata assert(to("func2num", 20) == 0) assert(to("func2num", T.pushuserdata(10)) == 0) assert(to("func2num", io.read) ~= 0) -- light C function diff --git a/testes/strings.lua b/testes/strings.lua index 884809249a..da53a87e7b 100644 --- a/testes/strings.lua +++ b/testes/strings.lua @@ -199,6 +199,7 @@ end assert(string.format("\0%s\0", "\0\0\1") == "\0\0\0\1\0") checkerror("contains zeros", string.format, "%10s", "\0") +checkerror("cannot have modifiers", string.format, "%10q", "1") -- format x tostring assert(string.format("%s %s", nil, true) == "nil true") From dfebe439db76b5c9fd9b4e3877857e2449c8ad87 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 13 Mar 2019 14:04:01 -0300 Subject: [PATCH 0379/1145] New conversion specifier '%p' for 'string.format' The call 'string.format("%p", val)' gives a Lua equivalent to the C API function 'lua_topointer'. --- lstrlib.c | 5 +++++ manual/manual.of | 13 ++++++++++--- testes/strings.lua | 16 ++++++++++++++++ 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/lstrlib.c b/lstrlib.c index ab4258e5b1..6230cd0c00 100644 --- a/lstrlib.c +++ b/lstrlib.c @@ -1185,6 +1185,11 @@ static int str_format (lua_State *L) { nb = l_sprintf(buff, MAX_ITEM, form, (LUAI_UACNUMBER)n); break; } + case 'p': { + const void *p = lua_topointer(L, arg); + nb = l_sprintf(buff, MAX_ITEM, form, p); + break; + } case 'q': { if (form[2] != '\0') /* modifiers? */ return luaL_error(L, "specifier '%%q' cannot have modifiers"); diff --git a/manual/manual.of b/manual/manual.of index 9c8ab033ec..2eb8773f5b 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -6751,8 +6751,7 @@ Returns a formatted version of its variable number of arguments following the description given in its first argument (which must be a string). The format string follows the same rules as the @ANSI{sprintf}. The only differences are that the conversion specifiers and modifiers -@T{*}, @id{h}, @id{L}, @id{l}, @id{n}, -and @id{p} are not supported +@T{*}, @id{h}, @id{L}, @id{l}, and @id{n} are not supported and that there is an extra specifier, @id{q}. The specifier @id{q} formats booleans, nil, numbers, and strings @@ -6791,6 +6790,14 @@ it is converted to one following the same rules of @Lid{tostring}. If the specifier has any modifier, the corresponding string argument should not contain @x{embedded zeros}. +The specifier @id{p} formats the pointer +returned by @Lid{lua_topointer}. +That gives a unique string identifier for tables, userdata, +threads, strings, and functions. +For other values (numbers, nil, booleans), +this specifier results in a string representing +the pointer @id{NULL}. + } @LibEntry{string.gmatch (s, pattern [, init])| @@ -8768,7 +8775,7 @@ address space.) } @item{ -The constant @Lid{LUA_ERRGCMM} was removed. +The constant @id{LUA_ERRGCMM} was removed. Errors in finalizers are never propagated; instead, they generate a warning. } diff --git a/testes/strings.lua b/testes/strings.lua index da53a87e7b..8bcbb39156 100644 --- a/testes/strings.lua +++ b/testes/strings.lua @@ -153,6 +153,22 @@ else -- compatible coercion assert(tostring(-1203 + 0.0) == "-1203") end +do -- tests for '%p' format + -- not much to test, as C does not specify what '%p' does. + -- ("The value of the pointer is converted to a sequence of printing + -- characters, in an implementation-defined manner.") + local null = string.format("%p", nil) + assert(string.format("%p", {}) ~= null) + assert(string.format("%p", 4) == null) + assert(string.format("%p", print) ~= null) + assert(string.format("%p", coroutine.running()) ~= null) + assert(string.format("%p", {}) ~= string.format("%p", {})) + assert(string.format("%p", string.rep("a", 10)) == + string.format("%p", string.rep("a", 10))) -- short strings + assert(string.format("%p", string.rep("a", 300)) ~= + string.format("%p", string.rep("a", 300))) -- long strings + assert(#string.format("%90p", {}) == 90) +end x = '"lo"\n\\' assert(string.format('%q%s', x, x) == '"\\"lo\\"\\\n\\\\""lo"\n\\') From c5feac2b5edf86aa9bdc4b8acd5380f2fe9e197f Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 13 Mar 2019 14:14:40 -0300 Subject: [PATCH 0380/1145] Strings inside Lua are not fully aligned Removed code to ensure that strings inside Lua (as returned by 'lua_tolstring') always start in fully aligned addresses. Since version 5.3 the documentation does not ensure that. --- lobject.h | 10 +--------- lstring.h | 2 +- 2 files changed, 2 insertions(+), 10 deletions(-) diff --git a/lobject.h b/lobject.h index df31a1a0d1..53e67932a0 100644 --- a/lobject.h +++ b/lobject.h @@ -350,21 +350,13 @@ typedef struct TString { } TString; -/* -** Ensures that address after this type is always fully aligned. -*/ -typedef union UTString { - LUAI_MAXALIGN; /* ensures maximum alignment for strings */ - TString tsv; -} UTString; - /* ** Get the actual string (array of bytes) from a 'TString'. ** (Access to 'extra' ensures that value is really a 'TString'.) */ #define getstr(ts) \ - check_exp(sizeof((ts)->extra), cast_charp((ts)) + sizeof(UTString)) + check_exp(sizeof((ts)->extra), cast_charp((ts)) + sizeof(TString)) /* get the actual string (array of bytes) from a Lua value */ diff --git a/lstring.h b/lstring.h index e7e4aedc48..b25502187b 100644 --- a/lstring.h +++ b/lstring.h @@ -19,7 +19,7 @@ #define MEMERRMSG "not enough memory" -#define sizelstring(l) (sizeof(union UTString) + ((l) + 1) * sizeof(char)) +#define sizelstring(l) (sizeof(TString) + ((l) + 1) * sizeof(char)) #define luaS_newliteral(L, s) (luaS_newlstr(L, "" s, \ (sizeof(s)/sizeof(char))-1)) From 9eca305e75010e30342486a4139846faf1b3eccb Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 13 Mar 2019 14:47:48 -0300 Subject: [PATCH 0381/1145] 'math.randomseed()' sets a somewhat random seed When called with no arguments, 'math.randomseed' uses time and ASLR to generate a somewhat random seed. the initial seed when Lua starts is generated this way. --- lmathlib.c | 31 +++++++++++++++++++++---------- manual/manual.of | 23 ++++++++++++++--------- testes/math.lua | 2 +- 3 files changed, 36 insertions(+), 20 deletions(-) diff --git a/lmathlib.c b/lmathlib.c index f2bfff9b14..e3ccc3ee58 100644 --- a/lmathlib.c +++ b/lmathlib.c @@ -301,7 +301,7 @@ static int math_type (lua_State *L) { /* rotate left 'x' by 'n' bits */ static Rand64 rotl (Rand64 x, int n) { - return (x << n) | (trim64(x) >> (64 - n)); + return (x << n) | (trim64(x) >> (64 - n)); } static Rand64 nextrand (Rand64 *state) { @@ -597,11 +597,27 @@ static void setseed (Rand64 *state, lua_Unsigned n1, lua_Unsigned n2) { } +/* +** Set a "random" seed. To get some randomness, use the current time +** and the address of 'L' (in case the machine does address space layout +** randomization). +*/ +static void randseed (lua_State *L, RanState *state) { + lua_Unsigned seed1 = (lua_Unsigned)time(NULL); + lua_Unsigned seed2 = (lua_Unsigned)(size_t)L; + setseed(state->s, seed1, seed2); +} + + static int math_randomseed (lua_State *L) { RanState *state = (RanState *)lua_touserdata(L, lua_upvalueindex(1)); - lua_Integer n1 = luaL_checkinteger(L, 1); - lua_Integer n2 = luaL_optinteger(L, 2, 0); - setseed(state->s, n1, n2); + if (lua_isnone(L, 1)) + randseed(L, state); + else { + lua_Integer n1 = luaL_checkinteger(L, 1); + lua_Integer n2 = luaL_optinteger(L, 2, 0); + setseed(state->s, n1, n2); + } return 0; } @@ -615,15 +631,10 @@ static const luaL_Reg randfuncs[] = { /* ** Register the random functions and initialize their state. -** To give some "randomness" to the initial seed, use the current time -** and the address of 'L' (in case the machine does address space layout -** randomization). */ static void setrandfunc (lua_State *L) { RanState *state = (RanState *)lua_newuserdatauv(L, sizeof(RanState), 0); - lua_Unsigned seed1 = (lua_Unsigned)time(NULL); - lua_Unsigned seed2 = (lua_Unsigned)(size_t)L; - setseed(state->s, seed1, seed2); + randseed(L, state); /* initialize with a "random" seed */ luaL_setfuncs(L, randfuncs, 1); } diff --git a/manual/manual.of b/manual/manual.of index 2eb8773f5b..b47fd86565 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -7643,14 +7643,10 @@ is equivalent to @T{math.random(1,n)}. The call @T{math.random(0)} produces an integer with all bits (pseudo)random. -Lua initializes its pseudo-random generator with -a weak attempt for ``randomness'', +Lua initializes its pseudo-random generator with the equivalent of +a call to @Lid{math.randomseed} with no arguments, so that @id{math.random} should generate different sequences of results each time the program runs. -To ensure a required level of randomness to the initial state -(or contrarily, to have a deterministic sequence, -for instance when debugging a program), -you should call @Lid{math.randomseed} explicitly. The results from this function have good statistical qualities, but they are not cryptographically secure. @@ -7660,14 +7656,23 @@ some number of previous results.) } -@LibEntry{math.randomseed (x [, y])| +@LibEntry{math.randomseed ([x [, y]])| -The integer parameters @id{x} and @id{y} are -concatenated into a 128-bit @Q{seed} that +When called with at least one argument, +the integer parameters @id{x} and @id{y} are +concatenated into a 128-bit @emphx{seed} that is used to reinitialize the pseudo-random generator; equal seeds produce equal sequences of numbers. The default for @id{y} is zero. +When called with no arguments, +Lua generates a seed with +a weak attempt for randomness. +To ensure a required level of randomness to the initial state +(or contrarily, to have a deterministic sequence, +for instance when debugging a program), +you should call @Lid{math.randomseed} with explicit arguments. + } @LibEntry{math.sin (x)| diff --git a/testes/math.lua b/testes/math.lua index dc5b84f680..b010ff6c3a 100644 --- a/testes/math.lua +++ b/testes/math.lua @@ -838,7 +838,7 @@ do assert(rand * 2^floatbits == res) end -math.randomseed(0, os.time()) +math.randomseed() do -- test random for floats local randbits = math.min(floatbits, 64) -- at most 64 random bits From b56d4e570a60a8e84df8288c3122eb5bb5c20af6 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 14 Mar 2019 15:30:54 -0300 Subject: [PATCH 0382/1145] Changes in the warning system - The warning functions get an extra parameter that tells whether message is to be continued (instead of using end-of-lines as a signal). - The user data for the warning function is a regular value, instead of a writable slot inside the Lua state. --- lapi.c | 4 ++-- lauxlib.c | 34 ++++++++++++--------------- lbaselib.c | 2 +- lgc.c | 8 +++---- lstate.c | 4 ++-- lstate.h | 2 +- ltests.c | 60 ++++++++++++++++++------------------------------ lua.h | 4 ++-- manual/manual.of | 26 ++++++++++----------- testes/all.lua | 10 ++++---- testes/api.lua | 12 ++++------ testes/gc.lua | 11 +++++---- 12 files changed, 79 insertions(+), 98 deletions(-) diff --git a/lapi.c b/lapi.c index 66d7564945..06396ad3bc 100644 --- a/lapi.c +++ b/lapi.c @@ -1286,9 +1286,9 @@ void lua_setwarnf (lua_State *L, lua_WarnFunction f, void *ud) { } -void lua_warning (lua_State *L, const char *msg) { +void lua_warning (lua_State *L, const char *msg, int tocont) { lua_lock(L); - luaE_warning(L, msg); + luaE_warning(L, msg, tocont); lua_unlock(L); } diff --git a/lauxlib.c b/lauxlib.c index abf923f80a..e9c02d3660 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -987,33 +987,29 @@ static int panic (lua_State *L) { /* -** checks whether 'message' ends with end-of-line -** (and therefore is the last part of a warning) +** Emit a warning. '*previoustocont' signals whether previous message +** was to be continued by the current one. */ -static int islast (const char *message) { - size_t len = strlen(message); - return (len > 0 && message[len - 1] == '\n'); -} - - -/* -** Emit a warning. If '*pud' is NULL, previous message was to be -** continued by the current one. -*/ -static void warnf (void **pud, const char *message) { - if (*pud == NULL) /* previous message was not the last? */ - lua_writestringerror("%s", message); - else /* start a new warning */ - lua_writestringerror("Lua warning: %s", message); - *pud = (islast(message)) ? pud : NULL; +static void warnf (void *ud, const char *message, int tocont) { + int *previoustocont = (int *)ud; + if (!*previoustocont) /* previous message was the last? */ + lua_writestringerror("%s", "Lua warning: "); /* start a new warning */ + lua_writestringerror("%s", message); /* write message */ + if (!tocont) /* is this the last part? */ + lua_writestringerror("%s", "\n"); /* finish message with end-of-line */ + *previoustocont = tocont; } LUALIB_API lua_State *luaL_newstate (void) { lua_State *L = lua_newstate(l_alloc, NULL); if (L) { + int *previoustocont; /* space for warning state */ lua_atpanic(L, &panic); - lua_setwarnf(L, warnf, L); + previoustocont = (int *)lua_newuserdatauv(L, sizeof(int), 0); + luaL_ref(L, LUA_REGISTRYINDEX); /* make sure it won't be collected */ + *previoustocont = 0; /* next message starts a new warning */ + lua_setwarnf(L, warnf, previoustocont); } return L; } diff --git a/lbaselib.c b/lbaselib.c index 26683a1dcb..d4b619a530 100644 --- a/lbaselib.c +++ b/lbaselib.c @@ -45,7 +45,7 @@ static int luaB_print (lua_State *L) { static int luaB_warn (lua_State *L) { const char *msg = luaL_checkstring(L, 1); - lua_warning(L, msg); + lua_warning(L, msg, lua_toboolean(L, 2)); return 0; } diff --git a/lgc.c b/lgc.c index bf0460d549..c834319bb8 100644 --- a/lgc.c +++ b/lgc.c @@ -832,7 +832,7 @@ static void GCTM (lua_State *L) { lua_assert(!g->gcemergency); setgcovalue(L, &v, udata2finalize(g)); tm = luaT_gettmbyobj(L, &v, TM_GC); - if (tm != NULL && ttisfunction(tm)) { /* is there a finalizer? */ + if (ttisfunction(tm)) { /* is the finalizer a function? */ int status; lu_byte oldah = L->allowhook; int running = g->gcrunning; @@ -850,9 +850,9 @@ static void GCTM (lua_State *L) { const char *msg = (ttisstring(s2v(L->top - 1))) ? svalue(s2v(L->top - 1)) : "error object is not a string"; - luaE_warning(L, "error in __gc metamethod ("); - luaE_warning(L, msg); - luaE_warning(L, ")\n"); + luaE_warning(L, "error in __gc metamethod (", 1); + luaE_warning(L, msg, 1); + luaE_warning(L, ")", 0); } } } diff --git a/lstate.c b/lstate.c index 6c35ea1a6c..f5579a6613 100644 --- a/lstate.c +++ b/lstate.c @@ -411,10 +411,10 @@ LUA_API void lua_close (lua_State *L) { } -void luaE_warning (lua_State *L, const char *msg) { +void luaE_warning (lua_State *L, const char *msg, int tocont) { lua_WarnFunction wf = G(L)->warnf; if (wf != NULL) - wf(&G(L)->ud_warn, msg); + wf(G(L)->ud_warn, msg, tocont); } diff --git a/lstate.h b/lstate.h index 11bf18fde2..e35f89625b 100644 --- a/lstate.h +++ b/lstate.h @@ -317,7 +317,7 @@ LUAI_FUNC CallInfo *luaE_extendCI (lua_State *L); LUAI_FUNC void luaE_freeCI (lua_State *L); LUAI_FUNC void luaE_shrinkCI (lua_State *L); LUAI_FUNC void luaE_enterCcall (lua_State *L); -LUAI_FUNC void luaE_warning (lua_State *L, const char *msg); +LUAI_FUNC void luaE_warning (lua_State *L, const char *msg, int tocont); #define luaE_exitCcall(L) ((L)->nCcalls--) diff --git a/ltests.c b/ltests.c index 233753820d..40de22927b 100644 --- a/ltests.c +++ b/ltests.c @@ -63,11 +63,8 @@ static void pushobject (lua_State *L, const TValue *o) { } -static void badexit (const char *fmt, ...) { - va_list argp; - va_start(argp, fmt); - vfprintf(stderr, fmt, argp); - va_end(argp); +static void badexit (const char *fmt, const char *s) { + fprintf(stderr, fmt, s); /* avoid assertion failures when exiting */ l_memcontrol.numblocks = l_memcontrol.total = 0; exit(EXIT_FAILURE); @@ -81,52 +78,35 @@ static int tpanic (lua_State *L) { } -static int islast (const char *message) { - size_t len = strlen(message); - return (len > 0 && message[len - 1] == '\n'); -} - - /* ** Warning function for tests. Fist, it concatenates all parts of ** a warning in buffer 'buff'. Then: -** messages starting with '#' are shown on standard output (used to +** - messages starting with '#' are shown on standard output (used to ** test explicit warnings); -** messages containing '@' are stored in global '_WARN' (used to test +** - messages containing '@' are stored in global '_WARN' (used to test ** errors that generate warnings); -** other messages abort the tests (they represent real warning conditions; -** the standard tests should not generate these conditions unexpectedly). +** - other messages abort the tests (they represent real warning +** conditions; the standard tests should not generate these conditions +** unexpectedly). */ -static void warnf (void **pud, const char *msg) { - static char buff[200]; /* should be enough for tests... */ - static int cont = 0; /* message to be continued */ - if (cont) { /* continuation? */ - if (strlen(msg) >= sizeof(buff) - strlen(buff)) - badexit("warnf-buffer overflow"); - strcat(buff, msg); /* add new message to current warning */ - } - else { /* new warning */ - if (strlen(msg) >= sizeof(buff)) - badexit("warnf-buffer overflow"); - strcpy(buff, msg); /* start a new warning */ - } - if (!islast(msg)) /* message not finished yet? */ - cont = 1; /* wait for more */ - else { /* handle message */ - cont = 0; /* prepare for next message */ +static void warnf (void *ud, const char *msg, int tocont) { + static char buff[200] = ""; /* should be enough for tests... */ + if (strlen(msg) >= sizeof(buff) - strlen(buff)) + badexit("%s", "warnf-buffer overflow"); + strcat(buff, msg); /* add new message to current warning */ + if (!tocont) { /* message finished? */ if (buff[0] == '#') /* expected warning? */ - printf("Expected Lua warning: %s", buff); /* print it */ + printf("Expected Lua warning: %s\n", buff); /* print it */ else if (strchr(buff, '@') != NULL) { /* warning for test purposes? */ - lua_State *L = cast(lua_State *, *pud); + lua_State *L = cast(lua_State *, ud); lua_unlock(L); lua_pushstring(L, buff); lua_setglobal(L, "_WARN"); /* assign message to global '_WARN' */ lua_lock(L); - return; } - else { /* a real warning; should not happen during tests */ + else /* a real warning; should not happen during tests */ badexit("Unexpected warning in test mode: %s\naborting...\n", buff); - } + buff[0] = '\0'; /* prepare buffer for next warning */ } } @@ -1466,9 +1446,13 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) { const char *msg = getstring; printf("%s\n", msg); } + else if EQ("warningC") { + const char *msg = getstring; + lua_warning(L1, msg, 1); + } else if EQ("warning") { const char *msg = getstring; - lua_warning(L1, msg); + lua_warning(L1, msg, 0); } else if EQ("pushbool") { lua_pushboolean(L1, getnum); diff --git a/lua.h b/lua.h index b777624e69..09611db574 100644 --- a/lua.h +++ b/lua.h @@ -128,7 +128,7 @@ typedef void * (*lua_Alloc) (void *ud, void *ptr, size_t osize, size_t nsize); /* ** Type for warning functions */ -typedef void (*lua_WarnFunction) (void **pud, const char *msg); +typedef void (*lua_WarnFunction) (void *ud, const char *msg, int tocont); @@ -309,7 +309,7 @@ LUA_API int (lua_isyieldable) (lua_State *L); ** Warning-related functions */ LUA_API void (lua_setwarnf) (lua_State *L, lua_WarnFunction f, void *ud); -LUA_API void (lua_warning) (lua_State *L, const char *msg); +LUA_API void (lua_warning) (lua_State *L, const char *msg, int tocont); /* diff --git a/manual/manual.of b/manual/manual.of index b47fd86565..63e78f9592 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -4061,7 +4061,7 @@ If @id{index} @N{is 0}, then all stack elements are removed. Sets the @x{warning function} to be used by Lua to emit warnings @see{lua_WarnFunction}. -The @id{ud} parameter initializes the slot @id{pud} passed to +The @id{ud} parameter sets the value @id{ud} passed to the warning function. } @@ -4325,25 +4325,24 @@ Returns the version number of this core. } @APIEntry{ -typedef void (*lua_WarnFunction) (void **pud, const char *msg);| +typedef void (*lua_WarnFunction) (void *ud, const char *msg, int tocont);| The type of @x{warning function}s, called by Lua to emit warnings. -The first parameter is the address of a writable slot, -constant for a given Lua state and -initialized by @Lid{lua_setwarnf}. +The first parameter is an opaque pointer +set by @Lid{lua_setwarnf}. The second parameter is the warning message. -This function should assume that -a message not ending with an end-of-line will be -continued by the message in the next call. +The third parameter is a boolean that +indicates whether the message is +to be continued by the message in the next call. } @APIEntry{ -void lua_warning (lua_State *L, const char *msg);| +void lua_warning (lua_State *L, const char *msg, int tocont);| @apii{0,0,-} Emits a warning with the given message. -A message not ending with an end-of-line should be +A message in a call with @id{tocont} true should be continued in another call to this function. } @@ -6275,11 +6274,12 @@ The current value of this variable is @St{Lua 5.4}. } -@LibEntry{warn (message)| +@LibEntry{warn (message [, tocont])| Emits a warning with the given message. -Note that messages not ending with an end-of-line -are assumed to be continued by the message in the next call. +A message in a call with @id{tocont} true should be +continued in another call to this function. +The default for @id{tocont} is false. } diff --git a/testes/all.lua b/testes/all.lua index 506afad287..8d727b6b21 100644 --- a/testes/all.lua +++ b/testes/all.lua @@ -6,7 +6,7 @@ local version = "Lua 5.4" if _VERSION ~= version then warn(string.format( - "This test suite is for %s, not for %s\nExiting tests\n", version, _VERSION)) + "This test suite is for %s, not for %s\nExiting tests", version, _VERSION)) return end @@ -190,16 +190,16 @@ assert(dofile('verybig.lua', true) == 10); collectgarbage() dofile('files.lua') if #msgs > 0 then - warn("#tests not performed:\n ") + warn("#tests not performed:", true) for i=1,#msgs do - warn(msgs[i]); warn("\n ") + warn("\n ", true); warn(msgs[i], true) end warn("\n") end print("(there should be two warnings now)") -warn("#This is "); warn("an expected"); warn(" warning\n") -warn("#This is"); warn(" another one\n") +warn("#This is ", true); warn("an expected", true); warn(" warning") +warn("#This is", true); warn(" another one") -- no test module should define 'debug' assert(debug == nil) diff --git a/testes/api.lua b/testes/api.lua index d034ea80ab..08672e8abe 100644 --- a/testes/api.lua +++ b/testes/api.lua @@ -114,13 +114,11 @@ end -- testing warnings T.testC([[ - warning "#This shold be a" - warning " single " - warning "warning -" - warning "#This should be " - warning "another one -" + warningC "#This shold be a" + warningC " single " + warning "warning" + warningC "#This should be " + warning "another one" ]]) diff --git a/testes/gc.lua b/testes/gc.lua index 05bf564e5c..91e78a4822 100644 --- a/testes/gc.lua +++ b/testes/gc.lua @@ -18,6 +18,8 @@ assert(collectgarbage("incremental") == "generational") assert(collectgarbage("incremental") == "incremental") +local function nop () end + local function gcinfo () return collectgarbage"count" * 1024 end @@ -388,7 +390,7 @@ if T then collectgarbage() for i = 1, 10 do assert(s[i]) end - getmetatable(u).__gc = false + getmetatable(u).__gc = nil end print '+' @@ -604,8 +606,8 @@ if T then collectgarbage("stop") local x = T.newuserdata(0) local y = T.newuserdata(0) - debug.setmetatable(y, {__gc = true}) -- bless the new udata before... - debug.setmetatable(x, {__gc = true}) -- ...the old one + debug.setmetatable(y, {__gc = nop}) -- bless the new udata before... + debug.setmetatable(x, {__gc = nop}) -- ...the old one assert(T.gccolor(y) == "white") T.checkmemory() collectgarbage("restart") @@ -631,6 +633,7 @@ if T then assert(T.totalmem("thread") == t + 1) end + -- create an object to be collected when state is closed do local setmetatable,assert,type,print,getmetatable = @@ -650,7 +653,7 @@ end -- create several objects to raise errors when collected while closing state if T then - local error, assert, warn, find = error, assert, warn, string.find + local error, assert, find = error, assert, string.find local n = 0 local lastmsg local mt = {__gc = function (o) From 8fa4f1380b9a203bfdf002c2e9e9e13ebb8384c1 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 14 Mar 2019 15:53:42 -0300 Subject: [PATCH 0383/1145] Finalizers must be callable Non-function __gc metamethods are not ignored; if present, the metamethod will be called even if it is not a function. --- lgc.c | 2 +- manual/manual.of | 23 +++++++++++++++++++---- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/lgc.c b/lgc.c index c834319bb8..8cb3e9fad5 100644 --- a/lgc.c +++ b/lgc.c @@ -832,7 +832,7 @@ static void GCTM (lua_State *L) { lua_assert(!g->gcemergency); setgcovalue(L, &v, udata2finalize(g)); tm = luaT_gettmbyobj(L, &v, TM_GC); - if (ttisfunction(tm)) { /* is the finalizer a function? */ + if (!notm(tm)) { /* is there a finalizer? */ int status; lu_byte oldah = L->allowhook; int running = g->gcrunning; diff --git a/manual/manual.of b/manual/manual.of index 63e78f9592..1e4ca8575e 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -689,10 +689,8 @@ After the collection, Lua goes through that list. For each object in the list, it checks the object's @idx{__gc} metamethod: -If it is a function, -Lua calls it with the object as its single argument; -if the metamethod is not a function, -Lua simply ignores it. +If it is present, +Lua calls it with the object as its single argument. At the end of each garbage-collection cycle, the finalizers for objects are called in @@ -726,6 +724,9 @@ these marks have no effect. Finalizers cannot yield. +Any error while running a finalizer generates a warning; +it is not propagated. + } @sect3{weak-table| @title{Weak Tables} @@ -7279,6 +7280,11 @@ that is, with maximum alignment of 1 (no alignment) and native endianness. +Native endianness assumes that the whole system is +either big or little endian. +The packing functions will not emulate correctly the behavior +of mixed-endian formats. + Alignment works as follows: For each option, the format gets extra padding until the data starts @@ -7288,6 +7294,7 @@ this minimum must be a power of 2. Options @St{c} and @St{z} are not aligned; option @St{s} follows the alignment of its starting integer. + All padding is filled with zeros by @Lid{string.pack} (and ignored by @Lid{string.unpack}). @@ -8720,6 +8727,14 @@ name is visible, even if this other label is declared in an enclosing block. } +@item{ +When finalizing an object, +Lua does not ignore @idx{__gc} metamethods that are not functions. +Any value will be called, if present. +(Non-callable values will generate a warning, +like any other error when calling a finalizer.) +} + } } From 1e0c73d5b643707335b06abd2546a83d9439d14c Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 15 Mar 2019 13:14:17 -0300 Subject: [PATCH 0384/1145] Changes in the validation of UTF-8 All UTF-8 encoding functionality (including the escape sequence '\u') accepts all values from the original UTF-8 specification (with sequences of up to six bytes). By default, the decoding functions in the UTF-8 library do not accept invalid Unicode code points, such as surrogates. A new parameter 'nonstrict' makes them accept all code points up to (2^31)-1, as in the original UTF-8 specification. --- llex.c | 2 +- lobject.c | 6 +-- lutf8lib.c | 76 ++++++++++++++++++++++++------------- manual/manual.of | 43 +++++++++++++++++++-- testes/literals.lua | 17 ++++++--- testes/utf8.lua | 92 +++++++++++++++++++++++++++++---------------- 6 files changed, 164 insertions(+), 72 deletions(-) diff --git a/llex.c b/llex.c index 38c6d92d03..1539f5258c 100644 --- a/llex.c +++ b/llex.c @@ -335,7 +335,7 @@ static unsigned long readutf8esc (LexState *ls) { while ((save_and_next(ls), lisxdigit(ls->current))) { i++; r = (r << 4) + luaO_hexavalue(ls->current); - esccheck(ls, r <= 0x10FFFF, "UTF-8 value too large"); + esccheck(ls, r <= 0x7FFFFFFFu, "UTF-8 value too large"); } esccheck(ls, ls->current == '}', "missing '}'"); next(ls); /* skip '}' */ diff --git a/lobject.c b/lobject.c index 3ce052c29a..5d340de65e 100644 --- a/lobject.c +++ b/lobject.c @@ -343,7 +343,7 @@ size_t luaO_str2num (const char *s, TValue *o) { int luaO_utf8esc (char *buff, unsigned long x) { int n = 1; /* number of bytes put in buffer (backwards) */ - lua_assert(x <= 0x10FFFF); + lua_assert(x <= 0x7FFFFFFFu); if (x < 0x80) /* ascii? */ buff[UTF8BUFFSZ - 1] = cast_char(x); else { /* need continuation bytes */ @@ -435,9 +435,9 @@ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) { pushstr(L, buff, l); break; } - case 'U': { /* an 'int' as a UTF-8 sequence */ + case 'U': { /* a 'long' as a UTF-8 sequence */ char buff[UTF8BUFFSZ]; - int l = luaO_utf8esc(buff, cast(long, va_arg(argp, long))); + int l = luaO_utf8esc(buff, va_arg(argp, long)); pushstr(L, buff + UTF8BUFFSZ - l, l); break; } diff --git a/lutf8lib.c b/lutf8lib.c index dc95b285c9..ec711c9a10 100644 --- a/lutf8lib.c +++ b/lutf8lib.c @@ -21,12 +21,14 @@ #include "lualib.h" -#define MAXUNICODE 0x10FFFF +#define MAXUNICODE 0x10FFFFu + +#define MAXUTF 0x7FFFFFFFu /* -** Integer type for decoded UTF-8 values; MAXUNICODE needs 21 bits. +** Integer type for decoded UTF-8 values; MAXUTF needs 31 bits. */ -#if LUAI_BITSINT >= 21 +#if LUAI_BITSINT >= 31 typedef unsigned int utfint; #else typedef unsigned long utfint; @@ -46,38 +48,46 @@ static lua_Integer u_posrelat (lua_Integer pos, size_t len) { /* -** Decode one UTF-8 sequence, returning NULL if byte sequence is invalid. +** Decode one UTF-8 sequence, returning NULL if byte sequence is +** invalid. The array 'limits' stores the minimum value for each +** sequence length, to check for overlong representations. Its first +** entry forces an error for non-ascii bytes with no continuation +** bytes (count == 0). */ -static const char *utf8_decode (const char *o, utfint *val) { - static const unsigned int limits[] = {0xFF, 0x7F, 0x7FF, 0xFFFF}; - const unsigned char *s = (const unsigned char *)o; - unsigned int c = s[0]; +static const char *utf8_decode (const char *s, utfint *val, int strict) { + static const utfint limits[] = + {~(utfint)0, 0x80, 0x800, 0x10000u, 0x200000u, 0x4000000u}; + unsigned int c = (unsigned char)s[0]; utfint res = 0; /* final result */ if (c < 0x80) /* ascii? */ res = c; else { int count = 0; /* to count number of continuation bytes */ - while (c & 0x40) { /* still have continuation bytes? */ - int cc = s[++count]; /* read next byte */ + for (; c & 0x40; c <<= 1) { /* while it needs continuation bytes... */ + unsigned int cc = (unsigned char)s[++count]; /* read next byte */ if ((cc & 0xC0) != 0x80) /* not a continuation byte? */ return NULL; /* invalid byte sequence */ res = (res << 6) | (cc & 0x3F); /* add lower 6 bits from cont. byte */ - c <<= 1; /* to test next bit */ } res |= ((utfint)(c & 0x7F) << (count * 5)); /* add first byte */ - if (count > 3 || res > MAXUNICODE || res <= limits[count]) + if (count > 5 || res > MAXUTF || res < limits[count]) return NULL; /* invalid byte sequence */ s += count; /* skip continuation bytes read */ } + if (strict) { + /* check for invalid code points; too large or surrogates */ + if (res > MAXUNICODE || (0xD800u <= res && res <= 0xDFFFu)) + return NULL; + } if (val) *val = res; - return (const char *)s + 1; /* +1 to include first byte */ + return s + 1; /* +1 to include first byte */ } /* -** utf8len(s [, i [, j]]) --> number of characters that start in the -** range [i,j], or nil + current position if 's' is not well formed in -** that interval +** utf8len(s [, i [, j [, nonstrict]]]) --> number of characters that +** start in the range [i,j], or nil + current position if 's' is not +** well formed in that interval */ static int utflen (lua_State *L) { lua_Integer n = 0; /* counter for the number of characters */ @@ -85,12 +95,13 @@ static int utflen (lua_State *L) { const char *s = luaL_checklstring(L, 1, &len); lua_Integer posi = u_posrelat(luaL_optinteger(L, 2, 1), len); lua_Integer posj = u_posrelat(luaL_optinteger(L, 3, -1), len); + int nonstrict = lua_toboolean(L, 4); luaL_argcheck(L, 1 <= posi && --posi <= (lua_Integer)len, 2, "initial position out of string"); luaL_argcheck(L, --posj < (lua_Integer)len, 3, "final position out of string"); while (posi <= posj) { - const char *s1 = utf8_decode(s + posi, NULL); + const char *s1 = utf8_decode(s + posi, NULL, !nonstrict); if (s1 == NULL) { /* conversion error? */ lua_pushnil(L); /* return nil ... */ lua_pushinteger(L, posi + 1); /* ... and current position */ @@ -105,14 +116,15 @@ static int utflen (lua_State *L) { /* -** codepoint(s, [i, [j]]) -> returns codepoints for all characters -** that start in the range [i,j] +** codepoint(s, [i, [j [, nonstrict]]]) -> returns codepoints for all +** characters that start in the range [i,j] */ static int codepoint (lua_State *L) { size_t len; const char *s = luaL_checklstring(L, 1, &len); lua_Integer posi = u_posrelat(luaL_optinteger(L, 2, 1), len); lua_Integer pose = u_posrelat(luaL_optinteger(L, 3, posi), len); + int nonstrict = lua_toboolean(L, 4); int n; const char *se; luaL_argcheck(L, posi >= 1, 2, "out of range"); @@ -126,7 +138,7 @@ static int codepoint (lua_State *L) { se = s + pose; /* string end */ for (s += posi - 1; s < se;) { utfint code; - s = utf8_decode(s, &code); + s = utf8_decode(s, &code, !nonstrict); if (s == NULL) return luaL_error(L, "invalid UTF-8 code"); lua_pushinteger(L, code); @@ -137,8 +149,8 @@ static int codepoint (lua_State *L) { static void pushutfchar (lua_State *L, int arg) { - lua_Integer code = luaL_checkinteger(L, arg); - luaL_argcheck(L, 0 <= code && code <= MAXUNICODE, arg, "value out of range"); + lua_Unsigned code = (lua_Unsigned)luaL_checkinteger(L, arg); + luaL_argcheck(L, code <= MAXUTF, arg, "value out of range"); lua_pushfstring(L, "%U", (long)code); } @@ -209,7 +221,7 @@ static int byteoffset (lua_State *L) { } -static int iter_aux (lua_State *L) { +static int iter_aux (lua_State *L, int strict) { size_t len; const char *s = luaL_checklstring(L, 1, &len); lua_Integer n = lua_tointeger(L, 2) - 1; @@ -223,8 +235,8 @@ static int iter_aux (lua_State *L) { return 0; /* no more codepoints */ else { utfint code; - const char *next = utf8_decode(s + n, &code); - if (next == NULL || iscont(next)) + const char *next = utf8_decode(s + n, &code, strict); + if (next == NULL) return luaL_error(L, "invalid UTF-8 code"); lua_pushinteger(L, n + 1); lua_pushinteger(L, code); @@ -233,9 +245,19 @@ static int iter_aux (lua_State *L) { } +static int iter_auxstrict (lua_State *L) { + return iter_aux(L, 1); +} + +static int iter_auxnostrict (lua_State *L) { + return iter_aux(L, 0); +} + + static int iter_codes (lua_State *L) { + int nonstrict = lua_toboolean(L, 2); luaL_checkstring(L, 1); - lua_pushcfunction(L, iter_aux); + lua_pushcfunction(L, nonstrict ? iter_auxnostrict : iter_auxstrict); lua_pushvalue(L, 1); lua_pushinteger(L, 0); return 3; @@ -243,7 +265,7 @@ static int iter_codes (lua_State *L) { /* pattern to match a single UTF-8 character */ -#define UTF8PATT "[\0-\x7F\xC2-\xF4][\x80-\xBF]*" +#define UTF8PATT "[\0-\x7F\xC2-\xFD][\x80-\xBF]*" static const luaL_Reg funcs[] = { diff --git a/manual/manual.of b/manual/manual.of index 1e4ca8575e..8a8ebad5df 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -1004,6 +1004,8 @@ the escape sequence @T{\u{@rep{XXX}}} (note the mandatory enclosing brackets), where @rep{XXX} is a sequence of one or more hexadecimal digits representing the character code point. +This code point can be any value smaller than @M{2@sp{31}}. +(Lua uses the original UTF-8 specification here.) Literal strings can also be defined using a long format enclosed by @def{long brackets}. @@ -6899,6 +6901,7 @@ x = string.gsub("$name-$version.tar.gz", "%$(%w+)", t) } @LibEntry{string.len (s)| + Receives a string and returns its length. The empty string @T{""} has length 0. Embedded zeros are counted, @@ -6907,6 +6910,7 @@ so @T{"a\000bc\000"} has length 5. } @LibEntry{string.lower (s)| + Receives a string and returns a copy of this string with all uppercase letters changed to lowercase. All other characters are left unchanged. @@ -6915,6 +6919,7 @@ The definition of what an uppercase letter is depends on the current locale. } @LibEntry{string.match (s, pattern [, init])| + Looks for the first @emph{match} of @id{pattern} @see{pm} in the string @id{s}. If it finds one, then @id{match} returns @@ -6946,6 +6951,7 @@ The format string cannot have the variable-length options } @LibEntry{string.rep (s, n [, sep])| + Returns a string that is the concatenation of @id{n} copies of the string @id{s} separated by the string @id{sep}. The default value for @id{sep} is the empty string @@ -6958,11 +6964,13 @@ with a single call to this function.) } @LibEntry{string.reverse (s)| + Returns a string that is the string @id{s} reversed. } @LibEntry{string.sub (s, i [, j])| + Returns the substring of @id{s} that starts at @id{i} and continues until @id{j}; @id{i} and @id{j} can be negative. @@ -6998,6 +7006,7 @@ this function also returns the index of the first unread byte in @id{s}. } @LibEntry{string.upper (s)| + Receives a string and returns a copy of this string with all lowercase letters changed to uppercase. All other characters are left unchanged. @@ -7318,8 +7327,24 @@ or one plus the length of the subject string. As in the string library, negative indices count from the end of the string. +Functions that create byte sequences +accept all values up to @T{0x7FFFFFFF}, +as defined in the original UTF-8 specification; +that implies byte sequences of up to six bytes. + +Functions that interpret byte sequences only accept +valid sequences (well formed and not overlong). +By default, they only accept byte sequences +that result in valid Unicode code points, +rejecting values larger than @T{10FFFF} and surrogates. +A boolean argument @id{nonstrict}, when available, +lifts these checks, +so that all values up to @T{0x7FFFFFFF} are accepted. +(Not well formed and overlong sequences are still rejected.) + @LibEntry{utf8.char (@Cdots)| + Receives zero or more integers, converts each one to its corresponding UTF-8 byte sequence and returns a string with the concatenation of all these sequences. @@ -7327,14 +7352,15 @@ and returns a string with the concatenation of all these sequences. } @LibEntry{utf8.charpattern| -The pattern (a string, not a function) @St{[\0-\x7F\xC2-\xF4][\x80-\xBF]*} + +The pattern (a string, not a function) @St{[\0-\x7F\xC2-\xFD][\x80-\xBF]*} @see{pm}, which matches exactly one UTF-8 byte sequence, assuming that the subject is a valid UTF-8 string. } -@LibEntry{utf8.codes (s)| +@LibEntry{utf8.codes (s [, nonstrict])| Returns values so that the construction @verbatim{ @@ -7347,7 +7373,8 @@ It raises an error if it meets any invalid byte sequence. } -@LibEntry{utf8.codepoint (s [, i [, j]])| +@LibEntry{utf8.codepoint (s [, i [, j [, nonstrict]]])| + Returns the codepoints (as integers) from all characters in @id{s} that start between byte position @id{i} and @id{j} (both included). The default for @id{i} is 1 and for @id{j} is @id{i}. @@ -7355,7 +7382,8 @@ It raises an error if it meets any invalid byte sequence. } -@LibEntry{utf8.len (s [, i [, j]])| +@LibEntry{utf8.len (s [, i [, j [, nonstrict]]])| + Returns the number of UTF-8 characters in string @id{s} that start between positions @id{i} and @id{j} (both inclusive). The default for @id{i} is @num{1} and for @id{j} is @num{-1}. @@ -7365,6 +7393,7 @@ returns a false value plus the position of the first invalid byte. } @LibEntry{utf8.offset (s, n [, i])| + Returns the position (in bytes) where the encoding of the @id{n}-th character of @id{s} (counting from position @id{i}) starts. @@ -8755,6 +8784,12 @@ You can enclose the call in parentheses if you need to discard these extra results. } +@item{ +By default, the decoding functions in the @Lid{utf8} library +do not accept surrogates as valid code points. +An extra parameter in these functions makes them more permissive. +} + } } diff --git a/testes/literals.lua b/testes/literals.lua index 76c08f12b6..fc45d4adf4 100644 --- a/testes/literals.lua +++ b/testes/literals.lua @@ -56,16 +56,23 @@ assert("abc\z assert("\u{0}\u{00000000}\x00\0" == string.char(0, 0, 0, 0)) -- limits for 1-byte sequences -assert("\u{0}\u{7F}" == "\x00\z\x7F") +assert("\u{0}\u{7F}" == "\x00\x7F") -- limits for 2-byte sequences -assert("\u{80}\u{7FF}" == "\xC2\x80\z\xDF\xBF") +assert("\u{80}\u{7FF}" == "\xC2\x80\xDF\xBF") -- limits for 3-byte sequences -assert("\u{800}\u{FFFF}" == "\xE0\xA0\x80\z\xEF\xBF\xBF") +assert("\u{800}\u{FFFF}" == "\xE0\xA0\x80\xEF\xBF\xBF") -- limits for 4-byte sequences -assert("\u{10000}\u{10FFFF}" == "\xF0\x90\x80\x80\z\xF4\x8F\xBF\xBF") +assert("\u{10000}\u{1FFFFF}" == "\xF0\x90\x80\x80\xF7\xBF\xBF\xBF") + +-- limits for 5-byte sequences +assert("\u{200000}\u{3FFFFFF}" == "\xF8\x88\x80\x80\x80\xFB\xBF\xBF\xBF\xBF") + +-- limits for 6-byte sequences +assert("\u{4000000}\u{7FFFFFFF}" == + "\xFC\x84\x80\x80\x80\x80\xFD\xBF\xBF\xBF\xBF\xBF") -- Error in escape sequences @@ -94,7 +101,7 @@ lexerror([["xyz\300"]], [[\300"]]) lexerror([[" \256"]], [[\256"]]) -- errors in UTF-8 sequences -lexerror([["abc\u{110000}"]], [[abc\u{110000]]) -- too large +lexerror([["abc\u{100000000}"]], [[abc\u{100000000]]) -- too large lexerror([["abc\u11r"]], [[abc\u1]]) -- missing '{' lexerror([["abc\u"]], [[abc\u"]]) -- missing '{' lexerror([["abc\u{11r"]], [[abc\u{11r]]) -- missing '}' diff --git a/testes/utf8.lua b/testes/utf8.lua index 4b6a57fd49..86ec1b00f1 100644 --- a/testes/utf8.lua +++ b/testes/utf8.lua @@ -21,62 +21,59 @@ local justone = "^" .. utf8.charpattern .. "$" -- 't' is the list of codepoints of 's' local function checksyntax (s, t) + -- creates a string "return '\u{t[1]}...\u{t[n]}'" local ts = {"return '"} for i = 1, #t do ts[i + 1] = string.format("\\u{%x}", t[i]) end ts[#t + 2] = "'" ts = table.concat(ts) + -- its execution should result in 's' assert(assert(load(ts))() == s) end assert(utf8.offset("alo", 5) == nil) assert(utf8.offset("alo", -4) == nil) --- 't' is the list of codepoints of 's' -local function check (s, t) - local l = utf8.len(s) +-- 'check' makes several tests over the validity of string 's'. +-- 't' is the list of codepoints of 's'. +local function check (s, t, nonstrict) + local l = utf8.len(s, 1, -1, nonstrict) assert(#t == l and len(s) == l) - assert(utf8.char(table.unpack(t)) == s) + assert(utf8.char(table.unpack(t)) == s) -- 't' and 's' are equivalent assert(utf8.offset(s, 0) == 1) checksyntax(s, t) - local t1 = {utf8.codepoint(s, 1, -1)} + -- creates new table with all codepoints of 's' + local t1 = {utf8.codepoint(s, 1, -1, nonstrict)} assert(#t == #t1) - for i = 1, #t do assert(t[i] == t1[i]) end + for i = 1, #t do assert(t[i] == t1[i]) end -- 't' is equal to 't1' - for i = 1, l do + for i = 1, l do -- for all codepoints local pi = utf8.offset(s, i) -- position of i-th char local pi1 = utf8.offset(s, 2, pi) -- position of next char assert(string.find(string.sub(s, pi, pi1 - 1), justone)) assert(utf8.offset(s, -1, pi1) == pi) assert(utf8.offset(s, i - l - 1) == pi) - assert(pi1 - pi == #utf8.char(utf8.codepoint(s, pi))) - for j = pi, pi1 - 1 do + assert(pi1 - pi == #utf8.char(utf8.codepoint(s, pi, pi, nonstrict))) + for j = pi, pi1 - 1 do assert(utf8.offset(s, 0, j) == pi) end for j = pi + 1, pi1 - 1 do assert(not utf8.len(s, j)) end - assert(utf8.len(s, pi, pi) == 1) - assert(utf8.len(s, pi, pi1 - 1) == 1) - assert(utf8.len(s, pi) == l - i + 1) - assert(utf8.len(s, pi1) == l - i) - assert(utf8.len(s, 1, pi) == i) + assert(utf8.len(s, pi, pi, nonstrict) == 1) + assert(utf8.len(s, pi, pi1 - 1, nonstrict) == 1) + assert(utf8.len(s, pi, -1, nonstrict) == l - i + 1) + assert(utf8.len(s, pi1, -1, nonstrict) == l - i) + assert(utf8.len(s, 1, pi, -1, nonstrict) == i) end local i = 0 - for p, c in utf8.codes(s) do + for p, c in utf8.codes(s, nonstrict) do i = i + 1 assert(c == t[i] and p == utf8.offset(s, i)) - assert(utf8.codepoint(s, p) == c) - end - assert(i == #t) - - i = 0 - for p, c in utf8.codes(s) do - i = i + 1 - assert(c == t[i] and p == utf8.offset(s, i)) + assert(utf8.codepoint(s, p, p, nonstrict) == c) end assert(i == #t) @@ -105,13 +102,17 @@ do -- error indication in utf8.len check("\xF4\x9F\xBF\xBF", 1) end --- error in utf8.codes -checkerror("invalid UTF%-8 code", - function () - local s = "ab\xff" - for c in utf8.codes(s) do assert(c) end - end) - +-- errors in utf8.codes +do + local function errorcodes (s) + checkerror("invalid UTF%-8 code", + function () + for c in utf8.codes(s) do assert(c) end + end) + end + errorcodes("ab\xff") + errorcodes("\u{110000}") +end -- error in initial position for offset checkerror("position out of range", utf8.offset, "abc", 1, 5) @@ -141,14 +142,22 @@ do assert(#t == 0) checkerror("out of range", utf8.codepoint, s, -(#s + 1), 1) checkerror("out of range", utf8.codepoint, s, 1, #s + 1) + -- surrogates + assert(utf8.codepoint("\u{D7FF}") == 0xD800 - 1) + assert(utf8.codepoint("\u{E000}") == 0xDFFF + 1) + assert(utf8.codepoint("\u{D800}", 1, 1, true) == 0xD800) + assert(utf8.codepoint("\u{DFFF}", 1, 1, true) == 0xDFFF) + assert(utf8.codepoint("\u{7FFFFFFF}", 1, 1, true) == 0x7FFFFFFF) end assert(utf8.char() == "") -assert(utf8.char(97, 98, 99) == "abc") +assert(utf8.char(0, 97, 98, 99, 1) == "\0abc\1") assert(utf8.codepoint(utf8.char(0x10FFFF)) == 0x10FFFF) +assert(utf8.codepoint(utf8.char(0x7FFFFFFF), 1, 1, true) == (1<<31) - 1) -checkerror("value out of range", utf8.char, 0x10FFFF + 1) +checkerror("value out of range", utf8.char, 0x7FFFFFFF + 1) +checkerror("value out of range", utf8.char, -1) local function invalid (s) checkerror("invalid UTF%-8 code", utf8.codepoint, s) @@ -158,6 +167,10 @@ end -- UTF-8 representation for 0x11ffff (value out of valid range) invalid("\xF4\x9F\xBF\xBF") +-- surrogates +invalid("\u{D800}") +invalid("\u{DFFF}") + -- overlong sequences invalid("\xC0\x80") -- zero invalid("\xC1\xBF") -- 0x7F (should be coded in 1 byte) @@ -183,6 +196,21 @@ s = "\0 \x7F\z s = string.gsub(s, " ", "") check(s, {0,0x7F, 0x80,0x7FF, 0x800,0xFFFF, 0x10000,0x10FFFF}) +do + -- original UTF-8 values + local s = "\u{4000000}\u{7FFFFFFF}" + assert(#s == 12) + check(s, {0x4000000, 0x7FFFFFFF}, true) + + s = "\u{200000}\u{3FFFFFF}" + assert(#s == 10) + check(s, {0x200000, 0x3FFFFFF}, true) + + s = "\u{10000}\u{1fffff}" + assert(#s == 8) + check(s, {0x10000, 0x1FFFFF}, true) +end + x = "日本語a-4\0éó" check(x, {26085, 26412, 35486, 97, 45, 52, 0, 233, 243}) From 9b37a4695ebf50b37b5b4fb279ae948f23b5b6a0 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 19 Mar 2019 10:53:18 -0300 Subject: [PATCH 0385/1145] New semantics for the integer 'for' loop The numerical 'for' loop over integers now uses a precomputed counter to control its number of iteractions. This change eliminates several weird cases caused by overflows (wrap-around) in the control variable. (It also ensures that every integer loop halts.) Also, the special opcodes for the usual case of step==1 were removed. (The new code is already somewhat complex for the usual case, but efficient.) --- ljumptab.h | 2 - lopcodes.c | 2 - lopcodes.h | 4 -- lopnames.h | 2 - lparser.c | 42 +++++-------- lvm.c | 147 +++++++++++++++++++++++---------------------- manual/manual.of | 124 +++++++++++++++++--------------------- testes/code.lua | 4 +- testes/db.lua | 2 +- testes/nextvar.lua | 73 ++++++++++++++++++++-- 10 files changed, 215 insertions(+), 187 deletions(-) diff --git a/ljumptab.h b/ljumptab.h index 9fa72a73c8..fa4277cc86 100644 --- a/ljumptab.h +++ b/ljumptab.h @@ -99,8 +99,6 @@ static void *disptab[NUM_OPCODES] = { &&L_OP_RETURN, &&L_OP_RETURN0, &&L_OP_RETURN1, -&&L_OP_FORLOOP1, -&&L_OP_FORPREP1, &&L_OP_FORLOOP, &&L_OP_FORPREP, &&L_OP_TFORPREP, diff --git a/lopcodes.c b/lopcodes.c index 3f0d551a99..c35a0aaf4c 100644 --- a/lopcodes.c +++ b/lopcodes.c @@ -93,8 +93,6 @@ LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = { ,opmode(0, 1, 0, 0, iABC) /* OP_RETURN */ ,opmode(0, 0, 0, 0, iABC) /* OP_RETURN0 */ ,opmode(0, 0, 0, 0, iABC) /* OP_RETURN1 */ - ,opmode(0, 0, 0, 1, iABx) /* OP_FORLOOP1 */ - ,opmode(0, 0, 0, 1, iABx) /* OP_FORPREP1 */ ,opmode(0, 0, 0, 1, iABx) /* OP_FORLOOP */ ,opmode(0, 0, 0, 1, iABx) /* OP_FORPREP */ ,opmode(0, 0, 0, 0, iABx) /* OP_TFORPREP */ diff --git a/lopcodes.h b/lopcodes.h index 3e100259b7..f867a01bb3 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -280,10 +280,6 @@ OP_RETURN,/* A B C return R(A), ... ,R(A+B-2) (see note) */ OP_RETURN0,/* return */ OP_RETURN1,/* A return R(A) */ -OP_FORLOOP1,/* A Bx R(A)++; - if R(A) <= R(A+1) then { pc-=Bx; R(A+3)=R(A) } */ -OP_FORPREP1,/* A Bx R(A)--; pc+=Bx */ - OP_FORLOOP,/* A Bx R(A)+=R(A+2); if R(A) fs, &e); lua_assert(e.k == VNONRELOC); - return res; } @@ -1403,31 +1399,29 @@ static void fixforjump (FuncState *fs, int pc, int dest, int back) { /* -** Generate code for a 'for' loop. 'kind' can be zero (a common for -** loop), one (a basic for loop, with integer values and increment of -** 1), or two (a generic for loop). +** Generate code for a 'for' loop. */ -static void forbody (LexState *ls, int base, int line, int nvars, int kind) { +static void forbody (LexState *ls, int base, int line, int nvars, int isgen) { /* forbody -> DO block */ - static OpCode forprep[3] = {OP_FORPREP, OP_FORPREP1, OP_TFORPREP}; - static OpCode forloop[3] = {OP_FORLOOP, OP_FORLOOP1, OP_TFORLOOP}; + static OpCode forprep[2] = {OP_FORPREP, OP_TFORPREP}; + static OpCode forloop[2] = {OP_FORLOOP, OP_TFORLOOP}; BlockCnt bl; FuncState *fs = ls->fs; int prep, endfor; checknext(ls, TK_DO); - prep = luaK_codeABx(fs, forprep[kind], base, 0); + prep = luaK_codeABx(fs, forprep[isgen], base, 0); enterblock(fs, &bl, 0); /* scope for declared variables */ adjustlocalvars(ls, nvars); luaK_reserveregs(fs, nvars); block(ls); leaveblock(fs); /* end of scope for declared variables */ fixforjump(fs, prep, luaK_getlabel(fs), 0); - if (kind == 2) { /* generic for? */ + if (isgen) { /* generic for? */ luaK_codeABC(fs, OP_TFORCALL, base, 0, nvars); luaK_fixline(fs, line); base += 2; /* base for 'OP_TFORLOOP' (skips function and state) */ } - endfor = luaK_codeABx(fs, forloop[kind], base, 0); + endfor = luaK_codeABx(fs, forloop[isgen], base, 0); fixforjump(fs, endfor, prep + 1, 1); luaK_fixline(fs, line); } @@ -1437,26 +1431,22 @@ static void fornum (LexState *ls, TString *varname, int line) { /* fornum -> NAME = exp,exp[,exp] forbody */ FuncState *fs = ls->fs; int base = fs->freereg; - int basicfor = 1; /* true if it is a "basic" 'for' (integer + 1) */ new_localvarliteral(ls, "(for index)"); new_localvarliteral(ls, "(for limit)"); new_localvarliteral(ls, "(for step)"); new_localvar(ls, varname); checknext(ls, '='); - if (!exp1(ls, 0)) /* initial value not an integer? */ - basicfor = 0; /* not a basic 'for' */ + exp1(ls); /* initial value */ checknext(ls, ','); - exp1(ls, 0); /* limit */ - if (testnext(ls, ',')) { - if (!exp1(ls, 1)) /* optional step not 1? */ - basicfor = 0; /* not a basic 'for' */ - } + exp1(ls); /* limit */ + if (testnext(ls, ',')) + exp1(ls); /* optional step */ else { /* default step = 1 */ luaK_int(fs, fs->freereg, 1); luaK_reserveregs(fs, 1); } adjustlocalvars(ls, 3); /* control variables */ - forbody(ls, base, line, 1, basicfor); + forbody(ls, base, line, 1, 0); } @@ -1484,7 +1474,7 @@ static void forlist (LexState *ls, TString *indexname) { adjust_assign(ls, 4, explist(ls, &e), &e); adjustlocalvars(ls, 4); /* control variables */ luaK_checkstack(fs, 3); /* extra space to call generator */ - forbody(ls, base, line, nvars - 4, 2); + forbody(ls, base, line, nvars - 4, 1); } @@ -1633,7 +1623,7 @@ static void tocloselocalstat (LexState *ls) { luaO_pushfstring(ls->L, "unknown attribute '%s'", getstr(attr))); new_localvar(ls, str_checkname(ls)); checknext(ls, '='); - exp1(ls, 0); + exp1(ls); markupval(fs, fs->nactvar); fs->bl->insidetbc = 1; /* in the scope of a to-be-closed variable */ adjustlocalvars(ls, 1); diff --git a/lvm.c b/lvm.c index 23e7ff700c..75b05f003c 100644 --- a/lvm.c +++ b/lvm.c @@ -148,35 +148,34 @@ int luaV_tointeger (const TValue *obj, lua_Integer *p, int mode) { /* ** Try to convert a 'for' limit to an integer, preserving the semantics -** of the loop. (The following explanation assumes a non-negative step; +** of the loop. (The following explanation assumes a positive step; ** it is valid for negative steps mutatis mutandis.) ** If the limit is an integer or can be converted to an integer, ** rounding down, that is it. -** Otherwise, check whether the limit can be converted to a float. If -** the number is too large, it is OK to set the limit as LUA_MAXINTEGER, -** which means no limit. If the number is too negative, the loop -** should not run, because any initial integer value is larger than the -** limit. So, it sets the limit to LUA_MININTEGER. 'stopnow' corrects -** the extreme case when the initial value is LUA_MININTEGER, in which -** case the LUA_MININTEGER limit would still run the loop once. +** Otherwise, check whether the limit can be converted to a float. If +** the float is too large, clip it to LUA_MAXINTEGER. If the float +** is too negative, the loop should not run, because any initial +** integer value is greater than such limit; so, it sets 'stopnow'. +** (For this latter case, no integer limit would be correct; even a +** limit of LUA_MININTEGER would run the loop once for an initial +** value equal to LUA_MININTEGER.) */ -static int forlimit (const TValue *obj, lua_Integer *p, lua_Integer step, +static int forlimit (const TValue *lim, lua_Integer *p, lua_Integer step, int *stopnow) { *stopnow = 0; /* usually, let loops run */ - if (ttisinteger(obj)) - *p = ivalue(obj); - else if (!luaV_tointeger(obj, p, (step < 0 ? 2 : 1))) { + if (!luaV_tointeger(lim, p, (step < 0 ? 2 : 1))) { /* not coercible to in integer */ - lua_Number n; /* try to convert to float */ - if (!tonumber(obj, &n)) /* cannot convert to float? */ + lua_Number flim; /* try to convert to float */ + if (!tonumber(lim, &flim)) /* cannot convert to float? */ return 0; /* not a number */ - if (luai_numlt(0, n)) { /* if true, float is larger than max integer */ - *p = LUA_MAXINTEGER; - if (step < 0) *stopnow = 1; + /* 'flim' is a float out of integer bounds */ + if (luai_numlt(0, flim)) { /* if it is positive, it is too large */ + *p = LUA_MAXINTEGER; /* truncate */ + if (step < 0) *stopnow = 1; /* initial value must be less than it */ } - else { /* float is less than min integer */ - *p = LUA_MININTEGER; - if (step >= 0) *stopnow = 1; + else { /* it is less than min integer */ + *p = LUA_MININTEGER; /* truncate */ + if (step > 0) *stopnow = 1; /* initial value must be greater than it */ } } return 1; @@ -1636,85 +1635,87 @@ void luaV_execute (lua_State *L, CallInfo *ci) { } return; } - vmcase(OP_FORLOOP1) { - lua_Integer idx = intop(+, ivalue(s2v(ra)), 1); /* increment index */ - lua_Integer limit = ivalue(s2v(ra + 1)); - if (idx <= limit) { - pc -= GETARG_Bx(i); /* jump back */ - chgivalue(s2v(ra), idx); /* update internal index... */ - setivalue(s2v(ra + 3), idx); /* ...and external index */ - } - updatetrap(ci); - vmbreak; - } - vmcase(OP_FORPREP1) { - TValue *init = s2v(ra); - TValue *plimit = s2v(ra + 1); - lua_Integer ilimit, initv; - int stopnow; - if (unlikely(!forlimit(plimit, &ilimit, 1, &stopnow))) { - savestate(L, ci); /* for the error message */ - luaG_forerror(L, plimit, "limit"); - } - initv = (stopnow ? 0 : ivalue(init)); - setivalue(plimit, ilimit); - setivalue(init, intop(-, initv, 1)); - pc += GETARG_Bx(i); - vmbreak; - } vmcase(OP_FORLOOP) { - if (ttisinteger(s2v(ra))) { /* integer loop? */ - lua_Integer step = ivalue(s2v(ra + 2)); - lua_Integer idx = intop(+, ivalue(s2v(ra)), step); /* new index */ - lua_Integer limit = ivalue(s2v(ra + 1)); - if ((0 < step) ? (idx <= limit) : (limit <= idx)) { + if (ttisinteger(s2v(ra + 2))) { /* integer loop? */ + lua_Unsigned count = l_castS2U(ivalue(s2v(ra))); + if (count > 0) { /* still more iterations? */ + lua_Integer step = ivalue(s2v(ra + 2)); + lua_Integer idx = ivalue(s2v(ra + 3)); + idx = intop(+, idx, step); /* add step to index */ + chgivalue(s2v(ra), count - 1); /* update counter... */ + setivalue(s2v(ra + 3), idx); /* ...and index */ pc -= GETARG_Bx(i); /* jump back */ - chgivalue(s2v(ra), idx); /* update internal index... */ - setivalue(s2v(ra + 3), idx); /* ...and external index */ } } else { /* floating loop */ lua_Number step = fltvalue(s2v(ra + 2)); lua_Number limit = fltvalue(s2v(ra + 1)); - lua_Number idx = fltvalue(s2v(ra)); + lua_Number idx = fltvalue(s2v(ra + 3)); idx = luai_numadd(L, idx, step); /* inc. index */ if (luai_numlt(0, step) ? luai_numle(idx, limit) : luai_numle(limit, idx)) { + setfltvalue(s2v(ra + 3), idx); /* update index */ pc -= GETARG_Bx(i); /* jump back */ - chgfltvalue(s2v(ra), idx); /* update internal index... */ - setfltvalue(s2v(ra + 3), idx); /* ...and external index */ } } - updatetrap(ci); + updatetrap(ci); /* allows a signal to break the loop */ vmbreak; } vmcase(OP_FORPREP) { - TValue *init = s2v(ra); + TValue *pinit = s2v(ra); TValue *plimit = s2v(ra + 1); TValue *pstep = s2v(ra + 2); lua_Integer ilimit; int stopnow; - if (ttisinteger(init) && ttisinteger(pstep) && + savestate(L, ci); /* in case of errors */ + if (ttisinteger(pinit) && ttisinteger(pstep) && forlimit(plimit, &ilimit, ivalue(pstep), &stopnow)) { - /* all values are integer */ - lua_Integer initv = (stopnow ? 0 : ivalue(init)); - setivalue(plimit, ilimit); - setivalue(init, intop(-, initv, ivalue(pstep))); + /* integer loop */ + lua_Integer init = ivalue(pinit); + lua_Integer step = ivalue(pstep); + setivalue(s2v(ra + 3), init); /* control variable */ + if (step == 0) + luaG_runerror(L, "'for' step is zero"); + else if (stopnow) + pc += GETARG_Bx(i) + 1; /* skip the loop */ + else if (step > 0) { /* ascending loop? */ + if (init > ilimit) + pc += GETARG_Bx(i) + 1; /* skip the loop */ + else { + lua_Unsigned count = l_castS2U(ilimit) - l_castS2U(init); + if (step != 1) /* avoid division in the too common case */ + count /= l_castS2U(step); + setivalue(s2v(ra), count); + } + } + else { /* descending loop */ + if (init < ilimit) + pc += GETARG_Bx(i) + 1; /* skip the loop */ + else { + lua_Unsigned count = l_castS2U(init) - l_castS2U(ilimit); + count /= -l_castS2U(step); + setivalue(s2v(ra), count); + } + } } else { /* try making all values floats */ - lua_Number ninit; lua_Number nlimit; lua_Number nstep; - savestate(L, ci); /* in case of errors */ - if (unlikely(!tonumber(plimit, &nlimit))) + lua_Number init; lua_Number flimit; lua_Number step; + if (unlikely(!tonumber(plimit, &flimit))) luaG_forerror(L, plimit, "limit"); - setfltvalue(plimit, nlimit); - if (unlikely(!tonumber(pstep, &nstep))) + setfltvalue(plimit, flimit); + if (unlikely(!tonumber(pstep, &step))) luaG_forerror(L, pstep, "step"); - setfltvalue(pstep, nstep); - if (unlikely(!tonumber(init, &ninit))) - luaG_forerror(L, init, "initial value"); - setfltvalue(init, luai_numsub(L, ninit, nstep)); + setfltvalue(pstep, step); + if (unlikely(!tonumber(pinit, &init))) + luaG_forerror(L, pinit, "initial value"); + if (step == 0) + luaG_runerror(L, "'for' step is zero"); + if (luai_numlt(0, step) ? luai_numlt(flimit, init) + : luai_numlt(init, flimit)) + pc += GETARG_Bx(i) + 1; /* skip the loop */ + else + setfltvalue(s2v(ra + 3), init); /* control variable */ } - pc += GETARG_Bx(i); vmbreak; } vmcase(OP_TFORPREP) { diff --git a/manual/manual.of b/manual/manual.of index 8a8ebad5df..b7ced443e9 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -594,7 +594,7 @@ controls how long the collector waits before starting a new cycle. The collector starts a new cycle when the use of memory hits @M{n%} of the use after the previous collection. Larger values make the collector less aggressive. -Values smaller than 100 mean the collector will not wait to +Values less than 100 mean the collector will not wait to start a new cycle. A value of 200 means that the collector waits for the total memory in use to double before starting a new cycle. @@ -608,7 +608,7 @@ how many elements it marks or sweeps for each kilobyte of memory allocated. Larger values make the collector more aggressive but also increase the size of each incremental step. -You should not use values smaller than 100, +You should not use values less than 100, because they make the collector too slow and can result in the collector never finishing a cycle. The default value is 100; the maximum value is 1000. @@ -1004,7 +1004,7 @@ the escape sequence @T{\u{@rep{XXX}}} (note the mandatory enclosing brackets), where @rep{XXX} is a sequence of one or more hexadecimal digits representing the character code point. -This code point can be any value smaller than @M{2@sp{31}}. +This code point can be any value less than @M{2@sp{31}}. (Lua uses the original UTF-8 specification here.) Literal strings can also be defined using a long format @@ -1370,73 +1370,49 @@ because now @Rw{return} is the last statement in its (inner) block. The @Rw{for} statement has two forms: one numerical and one generic. +@sect4{@title{The numerical @Rw{for} loop} + The numerical @Rw{for} loop repeats a block of code while a -control variable runs through an arithmetic progression. +control variable goes through an arithmetic progression. It has the following syntax: @Produc{ @producname{stat}@producbody{@Rw{for} @bnfNter{Name} @bnfter{=} exp @bnfter{,} exp @bnfopt{@bnfter{,} exp} @Rw{do} block @Rw{end}} } -The @emph{block} is repeated for @emph{name} starting at the value of -the first @emph{exp}, until it passes the second @emph{exp} by steps of the -third @emph{exp}. -More precisely, a @Rw{for} statement like -@verbatim{ -for v = @rep{e1}, @rep{e2}, @rep{e3} do @rep{block} end -} -is equivalent to the code: -@verbatim{ -do - local @rep{var}, @rep{limit}, @rep{step} = tonumber(@rep{e1}), tonumber(@rep{e2}), tonumber(@rep{e3}) - if not (@rep{var} and @rep{limit} and @rep{step}) then error() end - @rep{var} = @rep{var} - @rep{step} - while true do - @rep{var} = @rep{var} + @rep{step} - if (@rep{step} >= 0 and @rep{var} > @rep{limit}) or (@rep{step} < 0 and @rep{var} < @rep{limit}) then - break - end - local v = @rep{var} - @rep{block} - end -end -} - -Note the following: -@itemize{ - -@item{ -All three control expressions are evaluated only once, -before the loop starts. -They must all result in numbers. -} - -@item{ -@T{@rep{var}}, @T{@rep{limit}}, and @T{@rep{step}} are invisible variables. -The names shown here are for explanatory purposes only. -} - -@item{ -If the third expression (the step) is absent, -then a step @N{of 1} is used. -} - -@item{ -You can use @Rw{break} and @Rw{goto} to exit a @Rw{for} loop. -} - -@item{ -The loop variable @T{v} is local to the loop body. +The given identifier (@bnfNter{Name}) defines the control variable, +which is local to the loop body (@emph{block}). + +The loop starts by evaluating once the three control expressions; +they must all result in numbers. +Their values are called respectively +the @emph{initial value}, the @emph{limit}, and the @emph{step}. +If the step is absent, it defaults @N{to 1}. +Then the loop body is repeated with the value of the control variable +going through an arithmetic progression, +starting at the initial value, +with a common difference given by the step, +until that value passes the limit. +A negative step makes a decreasing sequence; +a step equal to zero raises an error. +If the initial value is already greater than the limit +(or less than, if the step is negative), the body is not executed. + +If both the initial value and the step are integers, +the loop is done with integers; +in this case, the range of the control variable is limited +by the range of integers. +Otherwise, the loop is done with floats. +(Beware of floating-point accuracy in this case.) + +You should not change the value of the control variable +during the loop. If you need its value after the loop, assign it to another variable before exiting the loop. -} -@item{ -The values in @rep{var}, @rep{limit}, and @rep{step} -can be integers or floats. -All operations on them respect the usual rules in Lua. } -} +@sect4{@title{The generic @Rw{for} loop} + The generic @Rw{for} statement works over functions, called @def{iterators}. @@ -1499,6 +1475,8 @@ then assign them to other variables before breaking or exiting the loop. } +} + @sect3{funcstat| @title{Function Calls as Statements} To allow possible side-effects, function calls can be executed as statements: @@ -1819,7 +1797,7 @@ A comparison @T{a > b} is translated to @T{b < a} and @T{a >= b} is translated to @T{b <= a}. Following the @x{IEEE 754} standard, -@x{NaN} is considered neither smaller than, +@x{NaN} is considered neither less than, nor equal to, nor greater than any value (including itself). } @@ -2171,7 +2149,7 @@ then the function returns with no results. @index{multiple return} There is a system-dependent limit on the number of values that a function may return. -This limit is guaranteed to be larger than 1000. +This limit is guaranteed to be greater than 1000. The @emphx{colon} syntax is used for defining @def{methods}, @@ -2367,7 +2345,7 @@ but it also can be any positive index after the stack top within the space allocated for the stack, that is, indices up to the stack size. (Note that 0 is never an acceptable index.) -Indices to upvalues @see{c-closure} larger than the real number +Indices to upvalues @see{c-closure} greater than the real number of upvalues in the current @N{C function} are also acceptable (but invalid). Except when noted otherwise, functions in the API work with acceptable indices. @@ -2879,7 +2857,7 @@ Ensures that the stack has space for at least @id{n} extra slots (that is, that you can safely push up to @id{n} values into it). It returns false if it cannot fulfill the request, either because it would cause the stack -to be larger than a fixed maximum size +to be greater than a fixed maximum size (typically at least several thousand elements) or because it cannot allocate memory for the extra space. This function never shrinks the stack; @@ -4053,7 +4031,7 @@ for the @Q{newindex} event @see{metatable}. Accepts any index, @N{or 0}, and sets the stack top to this index. -If the new top is larger than the old one, +If the new top is greater than the old one, then the new elements are filled with @nil. If @id{index} @N{is 0}, then all stack elements are removed. @@ -5056,7 +5034,7 @@ size @id{sz} with a call @T{luaL_buffinitsize(L, &b, sz)}.} @item{ Finish by calling @T{luaL_pushresultsize(&b, sz)}, where @id{sz} is the total size of the resulting string -copied into that space (which may be smaller than or +copied into that space (which may be less than or equal to the preallocated size). } @@ -7336,7 +7314,7 @@ Functions that interpret byte sequences only accept valid sequences (well formed and not overlong). By default, they only accept byte sequences that result in valid Unicode code points, -rejecting values larger than @T{10FFFF} and surrogates. +rejecting values greater than @T{10FFFF} and surrogates. A boolean argument @id{nonstrict}, when available, lifts these checks, so that all values up to @T{0x7FFFFFFF} are accepted. @@ -7572,7 +7550,7 @@ returns the arc tangent of @id{y}. @LibEntry{math.ceil (x)| -Returns the smallest integral value larger than or equal to @id{x}. +Returns the smallest integral value greater than or equal to @id{x}. } @@ -7597,7 +7575,7 @@ Returns the value @M{e@sp{x}} @LibEntry{math.floor (x)| -Returns the largest integral value smaller than or equal to @id{x}. +Returns the largest integral value less than or equal to @id{x}. } @@ -7611,7 +7589,7 @@ that rounds the quotient towards zero. (integer/float) @LibEntry{math.huge| The float value @idx{HUGE_VAL}, -a value larger than any other numeric value. +a value greater than any other numeric value. } @@ -8352,7 +8330,7 @@ of the given thread: @N{level 1} is the function that called @id{getinfo} (except for tail calls, which do not count on the stack); and so on. -If @id{f} is a number larger than the number of active functions, +If @id{f} is a number greater than the number of active functions, then @id{getinfo} returns @nil. The returned table can contain all the fields returned by @Lid{lua_getinfo}, @@ -8745,6 +8723,12 @@ has been removed. When needed, this metamethod must be explicitly defined. } +@item{ +The semantics of the numerical @Rw{for} loop +over integers changed in some details. +In particular, the control variable never wraps around. +} + @item{ When a coroutine finishes with an error, its stack is unwound (to run any pending closing methods). diff --git a/testes/code.lua b/testes/code.lua index 834ff5e23d..128ca2cb8b 100644 --- a/testes/code.lua +++ b/testes/code.lua @@ -303,9 +303,9 @@ check(function (x) return x & 2.0 end, 'LOADF', 'BAND', 'RETURN1') -- basic 'for' loops check(function () for i = -10, 10.5 do end end, -'LOADI', 'LOADK', 'LOADI', 'FORPREP1', 'FORLOOP1', 'RETURN0') +'LOADI', 'LOADK', 'LOADI', 'FORPREP', 'FORLOOP', 'RETURN0') check(function () for i = 0xfffffff, 10.0, 1 do end end, -'LOADK', 'LOADF', 'LOADI', 'FORPREP1', 'FORLOOP1', 'RETURN0') +'LOADK', 'LOADF', 'LOADI', 'FORPREP', 'FORLOOP', 'RETURN0') -- bug in constant folding for 5.1 check(function () return -nil end, 'LOADNIL', 'UNM', 'RETURN1') diff --git a/testes/db.lua b/testes/db.lua index 976962b02f..0858dd2016 100644 --- a/testes/db.lua +++ b/testes/db.lua @@ -162,7 +162,7 @@ test([[for i,v in pairs{'a','b'} do end ]], {1,2,1,2,1,3}) -test([[for i=1,4 do a=1 end]], {1,1,1,1,1}) +test([[for i=1,4 do a=1 end]], {1,1,1,1}, true) do -- testing line info/trace with large gaps in source diff --git a/testes/nextvar.lua b/testes/nextvar.lua index d2306ed1ff..e769ccdd46 100644 --- a/testes/nextvar.lua +++ b/testes/nextvar.lua @@ -76,7 +76,7 @@ while a < lim do a = math.ceil(a*1.3) end - + local function check (t, na, nh) local a, h = T.querytab(t) if a ~= na or h ~= nh then @@ -100,7 +100,7 @@ local s = 'return {' for i=1,lim do s = s..i..',' local s = s - for k=0,lim do + for k=0,lim do local t = load(s..'}', '')() assert(#t == i) check(t, fb(i), mp2(k)) @@ -279,7 +279,7 @@ do -- clear global table end --- +-- local function checknext (a) local b = {} @@ -500,7 +500,7 @@ else --[ mt.__newindex = nil mt.__len = nil local tab2 = {} - local u2 = T.newuserdata(0) + local u2 = T.newuserdata(0) debug.setmetatable(u2, {__newindex = function (_, k, v) tab2[k] = v end}) table.move(u, 1, 4, 1, u2) assert(#tab2 == 4 and tab2[1] == tab[1] and tab2[4] == tab[4]) @@ -601,6 +601,69 @@ do -- checking types end + +do -- testing other strange cases for numeric 'for' + + local function checkfor (from, to, step, t) + local c = 0 + for i = from, to, step do + c = c + 1 + assert(i == t[c]) + end + assert(c == #t) + end + + local maxi = math.maxinteger + local mini = math.mininteger + + checkfor(mini, maxi, maxi, {mini, -1, maxi - 1}) + + checkfor(mini, math.huge, maxi, {mini, -1, maxi - 1}) + + checkfor(maxi, mini, mini, {maxi, -1}) + + checkfor(maxi, mini, -maxi, {maxi, 0, -maxi}) + + checkfor(maxi, -math.huge, mini, {maxi, -1}) + + checkfor(maxi, mini, 1, {}) + checkfor(mini, maxi, -1, {}) + + checkfor(maxi - 6, maxi, 3, {maxi - 6, maxi - 3, maxi}) + checkfor(mini + 4, mini, -2, {mini + 4, mini + 2, mini}) + + local step = maxi // 10 + local c = mini + for i = mini, maxi, step do + assert(i == c) + c = c + step + end + + c = maxi + for i = maxi, mini, -step do + assert(i == c) + c = c - step + end + + checkfor(maxi, maxi, maxi, {maxi}) + checkfor(maxi, maxi, mini, {maxi}) + checkfor(mini, mini, maxi, {mini}) + checkfor(mini, mini, mini, {mini}) +end + + +checkerror("'for' step is zero", function () + for i = 1, 10, 0 do end +end) + +checkerror("'for' step is zero", function () + for i = 1, -10, 0 do end +end) + +checkerror("'for' step is zero", function () + for i = 1.0, -10, 0.0 do end +end) + collectgarbage() @@ -657,7 +720,7 @@ a[3] = 30 -- testing ipairs with metamethods a = {n=10} setmetatable(a, { __index = function (t,k) - if k <= t.n then return k * 10 end + if k <= t.n then return k * 10 end end}) i = 0 for k,v in ipairs(a) do From 39bb3cf2422603d854ee12529cc3419dc735802a Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 19 Mar 2019 11:15:49 -0300 Subject: [PATCH 0386/1145] Name 'nonstrict' in the UTF-8 library changed to 'lax' It is not a good idea to use negative words to describe boolean values. (When we negate that boolean we create a double negative...) --- lutf8lib.c | 18 +++++++++--------- manual/manual.of | 8 ++++---- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/lutf8lib.c b/lutf8lib.c index ec711c9a10..16cd68ad68 100644 --- a/lutf8lib.c +++ b/lutf8lib.c @@ -85,7 +85,7 @@ static const char *utf8_decode (const char *s, utfint *val, int strict) { /* -** utf8len(s [, i [, j [, nonstrict]]]) --> number of characters that +** utf8len(s [, i [, j [, lax]]]) --> number of characters that ** start in the range [i,j], or nil + current position if 's' is not ** well formed in that interval */ @@ -95,13 +95,13 @@ static int utflen (lua_State *L) { const char *s = luaL_checklstring(L, 1, &len); lua_Integer posi = u_posrelat(luaL_optinteger(L, 2, 1), len); lua_Integer posj = u_posrelat(luaL_optinteger(L, 3, -1), len); - int nonstrict = lua_toboolean(L, 4); + int lax = lua_toboolean(L, 4); luaL_argcheck(L, 1 <= posi && --posi <= (lua_Integer)len, 2, "initial position out of string"); luaL_argcheck(L, --posj < (lua_Integer)len, 3, "final position out of string"); while (posi <= posj) { - const char *s1 = utf8_decode(s + posi, NULL, !nonstrict); + const char *s1 = utf8_decode(s + posi, NULL, !lax); if (s1 == NULL) { /* conversion error? */ lua_pushnil(L); /* return nil ... */ lua_pushinteger(L, posi + 1); /* ... and current position */ @@ -116,7 +116,7 @@ static int utflen (lua_State *L) { /* -** codepoint(s, [i, [j [, nonstrict]]]) -> returns codepoints for all +** codepoint(s, [i, [j [, lax]]]) -> returns codepoints for all ** characters that start in the range [i,j] */ static int codepoint (lua_State *L) { @@ -124,7 +124,7 @@ static int codepoint (lua_State *L) { const char *s = luaL_checklstring(L, 1, &len); lua_Integer posi = u_posrelat(luaL_optinteger(L, 2, 1), len); lua_Integer pose = u_posrelat(luaL_optinteger(L, 3, posi), len); - int nonstrict = lua_toboolean(L, 4); + int lax = lua_toboolean(L, 4); int n; const char *se; luaL_argcheck(L, posi >= 1, 2, "out of range"); @@ -138,7 +138,7 @@ static int codepoint (lua_State *L) { se = s + pose; /* string end */ for (s += posi - 1; s < se;) { utfint code; - s = utf8_decode(s, &code, !nonstrict); + s = utf8_decode(s, &code, !lax); if (s == NULL) return luaL_error(L, "invalid UTF-8 code"); lua_pushinteger(L, code); @@ -249,15 +249,15 @@ static int iter_auxstrict (lua_State *L) { return iter_aux(L, 1); } -static int iter_auxnostrict (lua_State *L) { +static int iter_auxlax (lua_State *L) { return iter_aux(L, 0); } static int iter_codes (lua_State *L) { - int nonstrict = lua_toboolean(L, 2); + int lax = lua_toboolean(L, 2); luaL_checkstring(L, 1); - lua_pushcfunction(L, nonstrict ? iter_auxnostrict : iter_auxstrict); + lua_pushcfunction(L, lax ? iter_auxlax : iter_auxstrict); lua_pushvalue(L, 1); lua_pushinteger(L, 0); return 3; diff --git a/manual/manual.of b/manual/manual.of index b7ced443e9..fc2550e084 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -7315,7 +7315,7 @@ valid sequences (well formed and not overlong). By default, they only accept byte sequences that result in valid Unicode code points, rejecting values greater than @T{10FFFF} and surrogates. -A boolean argument @id{nonstrict}, when available, +A boolean argument @id{lax}, when available, lifts these checks, so that all values up to @T{0x7FFFFFFF} are accepted. (Not well formed and overlong sequences are still rejected.) @@ -7338,7 +7338,7 @@ assuming that the subject is a valid UTF-8 string. } -@LibEntry{utf8.codes (s [, nonstrict])| +@LibEntry{utf8.codes (s [, lax])| Returns values so that the construction @verbatim{ @@ -7351,7 +7351,7 @@ It raises an error if it meets any invalid byte sequence. } -@LibEntry{utf8.codepoint (s [, i [, j [, nonstrict]]])| +@LibEntry{utf8.codepoint (s [, i [, j [, lax]]])| Returns the codepoints (as integers) from all characters in @id{s} that start between byte position @id{i} and @id{j} (both included). @@ -7360,7 +7360,7 @@ It raises an error if it meets any invalid byte sequence. } -@LibEntry{utf8.len (s [, i [, j [, nonstrict]]])| +@LibEntry{utf8.len (s [, i [, j [, lax]]])| Returns the number of UTF-8 characters in string @id{s} that start between positions @id{i} and @id{j} (both inclusive). From f53eabeed855081fa38e9af5cf7c977915f5213f Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 19 Mar 2019 15:31:08 -0300 Subject: [PATCH 0387/1145] Small changes in the header of binary files - LUAC_VERSION is equal to LUA_VERSION_NUM, and it is stored as an int. - 'sizeof(int)' and 'sizeof(size_t)' removed from the header, as the binary format does not depend on these sizes. (It uses its own serialization for unsigned integer values.) --- ldump.c | 4 +--- lundump.c | 38 +++++++++++++++++++++++--------------- lundump.h | 3 +-- testes/calls.lua | 20 +++++++++----------- 4 files changed, 34 insertions(+), 31 deletions(-) diff --git a/ldump.c b/ldump.c index aca6245bf4..c447557682 100644 --- a/ldump.c +++ b/ldump.c @@ -198,11 +198,9 @@ static void DumpFunction (const Proto *f, TString *psource, DumpState *D) { static void DumpHeader (DumpState *D) { DumpLiteral(LUA_SIGNATURE, D); - DumpByte(LUAC_VERSION, D); + DumpInt(LUAC_VERSION, D); DumpByte(LUAC_FORMAT, D); DumpLiteral(LUAC_DATA, D); - DumpByte(sizeof(int), D); - DumpByte(sizeof(size_t), D); DumpByte(sizeof(Instruction), D); DumpByte(sizeof(lua_Integer), D); DumpByte(sizeof(lua_Number), D); diff --git a/lundump.c b/lundump.c index 00ff6deffb..a600493367 100644 --- a/lundump.c +++ b/lundump.c @@ -10,6 +10,7 @@ #include "lprefix.h" +#include #include #include "lua.h" @@ -37,7 +38,7 @@ typedef struct { static l_noret error (LoadState *S, const char *why) { - luaO_pushfstring(S->L, "%s: %s precompiled chunk", S->name, why); + luaO_pushfstring(S->L, "%s: bad binary format (%s)", S->name, why); luaD_throw(S->L, LUA_ERRSYNTAX); } @@ -50,7 +51,7 @@ static l_noret error (LoadState *S, const char *why) { static void LoadBlock (LoadState *S, void *b, size_t size) { if (luaZ_read(S->Z, b, size) != 0) - error(S, "truncated"); + error(S, "truncated chunk"); } @@ -60,24 +61,32 @@ static void LoadBlock (LoadState *S, void *b, size_t size) { static lu_byte LoadByte (LoadState *S) { int b = zgetc(S->Z); if (b == EOZ) - error(S, "truncated"); + error(S, "truncated chunk"); return cast_byte(b); } -static size_t LoadSize (LoadState *S) { +static size_t LoadUnsigned (LoadState *S, size_t limit) { size_t x = 0; int b; + limit >>= 7; do { b = LoadByte(S); + if (x >= limit) + error(S, "integer overflow"); x = (x << 7) | (b & 0x7f); } while ((b & 0x80) == 0); return x; } +static size_t LoadSize (LoadState *S) { + return LoadUnsigned(S, ~(size_t)0); +} + + static int LoadInt (LoadState *S) { - return cast_int(LoadSize(S)); + return cast_int(LoadUnsigned(S, INT_MAX)); } @@ -255,28 +264,27 @@ static void checkliteral (LoadState *S, const char *s, const char *msg) { static void fchecksize (LoadState *S, size_t size, const char *tname) { if (LoadByte(S) != size) - error(S, luaO_pushfstring(S->L, "%s size mismatch in", tname)); + error(S, luaO_pushfstring(S->L, "%s size mismatch", tname)); } #define checksize(S,t) fchecksize(S,sizeof(t),#t) static void checkHeader (LoadState *S) { - checkliteral(S, LUA_SIGNATURE + 1, "not a"); /* 1st char already checked */ - if (LoadByte(S) != LUAC_VERSION) - error(S, "version mismatch in"); + /* 1st char already checked */ + checkliteral(S, LUA_SIGNATURE + 1, "not a binary chunk"); + if (LoadInt(S) != LUAC_VERSION) + error(S, "version mismatch"); if (LoadByte(S) != LUAC_FORMAT) - error(S, "format mismatch in"); - checkliteral(S, LUAC_DATA, "corrupted"); - checksize(S, int); - checksize(S, size_t); + error(S, "format mismatch"); + checkliteral(S, LUAC_DATA, "corrupted chunk"); checksize(S, Instruction); checksize(S, lua_Integer); checksize(S, lua_Number); if (LoadInteger(S) != LUAC_INT) - error(S, "endianness mismatch in"); + error(S, "integer format mismatch"); if (LoadNumber(S) != LUAC_NUM) - error(S, "float format mismatch in"); + error(S, "float format mismatch"); } diff --git a/lundump.h b/lundump.h index 739c7fcde0..5b05fed471 100644 --- a/lundump.h +++ b/lundump.h @@ -18,8 +18,7 @@ #define LUAC_INT 0x5678 #define LUAC_NUM cast_num(370.5) -#define MYINT(s) (s[0]-'0') -#define LUAC_VERSION (MYINT(LUA_VERSION_MAJOR)*16+MYINT(LUA_VERSION_MINOR)) +#define LUAC_VERSION LUA_VERSION_NUM #define LUAC_FORMAT 0 /* this is the official format */ /* load one chunk; from lundump.c */ diff --git a/testes/calls.lua b/testes/calls.lua index f5108a470e..941493b131 100644 --- a/testes/calls.lua +++ b/testes/calls.lua @@ -397,17 +397,15 @@ assert((function (a) return a end)() == nil) print("testing binary chunks") do - local header = string.pack("c4BBc6BBBBBj", - "\27Lua", -- signature - 5*16 + 4, -- version 5.4 - 0, -- format - "\x19\x93\r\n\x1a\n", -- data - string.packsize("i"), -- sizeof(int) - string.packsize("T"), -- sizeof(size_t) - 4, -- size of instruction - string.packsize("j"), -- sizeof(lua integer) - string.packsize("n"), -- sizeof(lua number) - 0x5678 -- LUAC_INT + local header = string.pack("c4BBBc6BBBj", + "\27Lua", -- signature + (504 >> 7) & 0x7f, (504 & 0x7f) | 0x80, -- version 5.4 (504) + 0, -- format + "\x19\x93\r\n\x1a\n", -- data + 4, -- size of instruction + string.packsize("j"), -- sizeof(lua integer) + string.packsize("n"), -- sizeof(lua number) + 0x5678 -- LUAC_INT -- LUAC_NUM may not have a unique binary representation (padding...) ) local c = string.dump(function () local a = 1; local b = 3; return a+b*3 end) From 682054920ddc434fd4a7f8cc78027dbb03f47f00 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 21 Mar 2019 16:01:55 -0300 Subject: [PATCH 0388/1145] Details in the implementation of the integer 'for' loop Changed some implementation details; in particular, it is back using an internal variable to keep the index, with the control variable being only a copy of that internal variable. (The direct use of the control variable demands a check of its type for each access, which offsets the gains from the use of a single variable.) --- lvm.c | 87 +++++++++++++++++++++++----------------------- testes/nextvar.lua | 6 ++++ 2 files changed, 49 insertions(+), 44 deletions(-) diff --git a/lvm.c b/lvm.c index 75b05f003c..31b4dc36fc 100644 --- a/lvm.c +++ b/lvm.c @@ -150,35 +150,36 @@ int luaV_tointeger (const TValue *obj, lua_Integer *p, int mode) { ** Try to convert a 'for' limit to an integer, preserving the semantics ** of the loop. (The following explanation assumes a positive step; ** it is valid for negative steps mutatis mutandis.) +** Return true if the loop must not run. ** If the limit is an integer or can be converted to an integer, -** rounding down, that is it. +** rounding down, that is the limit. ** Otherwise, check whether the limit can be converted to a float. If ** the float is too large, clip it to LUA_MAXINTEGER. If the float ** is too negative, the loop should not run, because any initial -** integer value is greater than such limit; so, it sets 'stopnow'. +** integer value is greater than such limit; so, it returns true to +** signal that. ** (For this latter case, no integer limit would be correct; even a ** limit of LUA_MININTEGER would run the loop once for an initial ** value equal to LUA_MININTEGER.) */ -static int forlimit (const TValue *lim, lua_Integer *p, lua_Integer step, - int *stopnow) { - *stopnow = 0; /* usually, let loops run */ +static int forlimit (lua_State *L, lua_Integer init, const TValue *lim, + lua_Integer *p, lua_Integer step) { if (!luaV_tointeger(lim, p, (step < 0 ? 2 : 1))) { /* not coercible to in integer */ lua_Number flim; /* try to convert to float */ if (!tonumber(lim, &flim)) /* cannot convert to float? */ - return 0; /* not a number */ - /* 'flim' is a float out of integer bounds */ + luaG_forerror(L, lim, "limit"); + /* else 'flim' is a float out of integer bounds */ if (luai_numlt(0, flim)) { /* if it is positive, it is too large */ + if (step < 0) return 1; /* initial value must be less than it */ *p = LUA_MAXINTEGER; /* truncate */ - if (step < 0) *stopnow = 1; /* initial value must be less than it */ } else { /* it is less than min integer */ + if (step > 0) return 1; /* initial value must be greater than it */ *p = LUA_MININTEGER; /* truncate */ - if (step > 0) *stopnow = 1; /* initial value must be greater than it */ } } - return 1; + return (step > 0 ? init > *p : init < *p); /* not to run? */ } @@ -1637,24 +1638,26 @@ void luaV_execute (lua_State *L, CallInfo *ci) { } vmcase(OP_FORLOOP) { if (ttisinteger(s2v(ra + 2))) { /* integer loop? */ - lua_Unsigned count = l_castS2U(ivalue(s2v(ra))); + lua_Unsigned count = l_castS2U(ivalue(s2v(ra + 1))); if (count > 0) { /* still more iterations? */ lua_Integer step = ivalue(s2v(ra + 2)); - lua_Integer idx = ivalue(s2v(ra + 3)); + lua_Integer idx = ivalue(s2v(ra)); /* internal index */ + chgivalue(s2v(ra + 1), count - 1); /* update counter */ idx = intop(+, idx, step); /* add step to index */ - chgivalue(s2v(ra), count - 1); /* update counter... */ - setivalue(s2v(ra + 3), idx); /* ...and index */ + chgivalue(s2v(ra), idx); /* update internal index */ + setivalue(s2v(ra + 3), idx); /* and control variable */ pc -= GETARG_Bx(i); /* jump back */ } } else { /* floating loop */ lua_Number step = fltvalue(s2v(ra + 2)); lua_Number limit = fltvalue(s2v(ra + 1)); - lua_Number idx = fltvalue(s2v(ra + 3)); - idx = luai_numadd(L, idx, step); /* inc. index */ + lua_Number idx = fltvalue(s2v(ra)); + idx = luai_numadd(L, idx, step); /* increment index */ if (luai_numlt(0, step) ? luai_numle(idx, limit) : luai_numle(limit, idx)) { - setfltvalue(s2v(ra + 3), idx); /* update index */ + chgfltvalue(s2v(ra), idx); /* update internal index */ + setfltvalue(s2v(ra + 3), idx); /* and control variable */ pc -= GETARG_Bx(i); /* jump back */ } } @@ -1665,56 +1668,52 @@ void luaV_execute (lua_State *L, CallInfo *ci) { TValue *pinit = s2v(ra); TValue *plimit = s2v(ra + 1); TValue *pstep = s2v(ra + 2); - lua_Integer ilimit; - int stopnow; savestate(L, ci); /* in case of errors */ - if (ttisinteger(pinit) && ttisinteger(pstep) && - forlimit(plimit, &ilimit, ivalue(pstep), &stopnow)) { - /* integer loop */ + if (ttisinteger(pinit) && ttisinteger(pstep)) { /* integer loop? */ lua_Integer init = ivalue(pinit); lua_Integer step = ivalue(pstep); - setivalue(s2v(ra + 3), init); /* control variable */ + lua_Integer limit; if (step == 0) luaG_runerror(L, "'for' step is zero"); - else if (stopnow) + setivalue(s2v(ra + 3), init); /* control variable */ + if (forlimit(L, init, plimit, &limit, step)) pc += GETARG_Bx(i) + 1; /* skip the loop */ - else if (step > 0) { /* ascending loop? */ - if (init > ilimit) - pc += GETARG_Bx(i) + 1; /* skip the loop */ - else { - lua_Unsigned count = l_castS2U(ilimit) - l_castS2U(init); + else { /* prepare loop counter */ + lua_Unsigned count; + if (step > 0) { /* ascending loop? */ + count = l_castS2U(limit) - l_castS2U(init); if (step != 1) /* avoid division in the too common case */ count /= l_castS2U(step); - setivalue(s2v(ra), count); } - } - else { /* descending loop */ - if (init < ilimit) - pc += GETARG_Bx(i) + 1; /* skip the loop */ - else { - lua_Unsigned count = l_castS2U(init) - l_castS2U(ilimit); + else { /* step < 0; descending loop */ + count = l_castS2U(init) - l_castS2U(limit); count /= -l_castS2U(step); - setivalue(s2v(ra), count); } + /* store the counter in place of the limit (which won't be + needed anymore */ + setivalue(plimit, l_castU2S(count)); } } else { /* try making all values floats */ - lua_Number init; lua_Number flimit; lua_Number step; - if (unlikely(!tonumber(plimit, &flimit))) + lua_Number init; lua_Number limit; lua_Number step; + if (unlikely(!tonumber(plimit, &limit))) luaG_forerror(L, plimit, "limit"); - setfltvalue(plimit, flimit); if (unlikely(!tonumber(pstep, &step))) luaG_forerror(L, pstep, "step"); - setfltvalue(pstep, step); if (unlikely(!tonumber(pinit, &init))) luaG_forerror(L, pinit, "initial value"); if (step == 0) luaG_runerror(L, "'for' step is zero"); - if (luai_numlt(0, step) ? luai_numlt(flimit, init) - : luai_numlt(init, flimit)) + if (luai_numlt(0, step) ? luai_numlt(limit, init) + : luai_numlt(init, limit)) pc += GETARG_Bx(i) + 1; /* skip the loop */ - else + else { + /* make sure internal values are all float */ + setfltvalue(plimit, limit); + setfltvalue(pstep, step); + setfltvalue(s2v(ra), init); /* internal index */ setfltvalue(s2v(ra + 3), init); /* control variable */ + } } vmbreak; } diff --git a/testes/nextvar.lua b/testes/nextvar.lua index e769ccdd46..87a6bfa81c 100644 --- a/testes/nextvar.lua +++ b/testes/nextvar.lua @@ -544,6 +544,12 @@ do a = 0; for i=1.0, 0.99999, -1 do a=a+1 end; assert(a==1) end +do -- changing the control variable + local a + a = 0; for i = 1, 10 do a = a + 1; i = "x" end; assert(a == 10) + a = 0; for i = 10.0, 1, -1 do a = a + 1; i = "x" end; assert(a == 10) +end + -- conversion a = 0; for i="10","1","-2" do a=a+1 end; assert(a==5) From 23e6bac8a0bbb9e5df43cbc0b7634b6d1395b0ff Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 22 Mar 2019 13:37:17 -0300 Subject: [PATCH 0389/1145] Keep correct type for immediate operands in comparisons When calling metamethods for things like 'a < 3.0', which generates the opcode OP_LTI, the C register tells that the operand was converted to an integer, so that it can be corrected to float when calling a metamethod. This commit also includes some other stuff: - file 'onelua.c' added to the project - opcode OP_PREPVARARG renamed to OP_VARARGPREP - comparison opcodes rewritten through macros --- lcode.c | 42 ++++++++++-------- ljumptab.h | 2 +- llimits.h | 2 + lopcodes.c | 2 +- lopcodes.h | 5 ++- lopnames.h | 2 +- lparser.c | 2 +- ltm.c | 8 +++- ltm.h | 2 +- lvm.c | 96 +++++++++++++++++++---------------------- onelua.c | 107 ++++++++++++++++++++++++++++++++++++++++++++++ testes/db.lua | 2 +- testes/events.lua | 99 +++++++++++++++++++----------------------- 13 files changed, 237 insertions(+), 134 deletions(-) create mode 100644 onelua.c diff --git a/lcode.c b/lcode.c index 1872ede2ba..1e36e5842a 100644 --- a/lcode.c +++ b/lcode.c @@ -179,8 +179,8 @@ void luaK_ret (FuncState *fs, int first, int nret) { ** Code a "conditional jump", that is, a test or comparison opcode ** followed by a jump. Return jump position. */ -static int condjump (FuncState *fs, OpCode op, int A, int B, int k) { - luaK_codeABCk(fs, op, A, B, 0, k); +static int condjump (FuncState *fs, OpCode op, int A, int B, int C, int k) { + luaK_codeABCk(fs, op, A, B, C, k); return luaK_jump(fs); } @@ -799,7 +799,7 @@ static int need_value (FuncState *fs, int list) { /* ** Ensures final expression result (which includes results from its -** jump ** lists) is in register 'reg'. +** jump lists) is in register 'reg'. ** If expression has jumps, need to patch these jumps either to ** its final position or to "load" instructions (for those tests ** that do not produce values). @@ -814,8 +814,9 @@ static void exp2reg (FuncState *fs, expdesc *e, int reg) { int p_t = NO_JUMP; /* position of an eventual LOAD true */ if (need_value(fs, e->t) || need_value(fs, e->f)) { int fj = (e->k == VJMP) ? NO_JUMP : luaK_jump(fs); - p_f = code_loadbool(fs, reg, 0, 1); - p_t = code_loadbool(fs, reg, 1, 0); + p_f = code_loadbool(fs, reg, 0, 1); /* load false and skip next i. */ + p_t = code_loadbool(fs, reg, 1, 0); /* load true */ + /* jump around these booleans if 'e' is not a test */ luaK_patchtohere(fs, fj); } final = luaK_getlabel(fs); @@ -1005,13 +1006,13 @@ static int jumponcond (FuncState *fs, expdesc *e, int cond) { Instruction ie = getinstruction(fs, e); if (GET_OPCODE(ie) == OP_NOT) { removelastinstruction(fs); /* remove previous OP_NOT */ - return condjump(fs, OP_TEST, GETARG_B(ie), 0, !cond); + return condjump(fs, OP_TEST, GETARG_B(ie), 0, 0, !cond); } /* else go through */ } discharge2anyreg(fs, e); freeexp(fs, e); - return condjump(fs, OP_TESTSET, NO_REG, e->u.info, cond); + return condjump(fs, OP_TESTSET, NO_REG, e->u.info, 0, cond); } @@ -1139,13 +1140,15 @@ static int isSCint (expdesc *e) { /* ** Check whether expression 'e' is a literal integer or float in -** proper range to fit in register sC +** proper range to fit in a register (sB or sC). */ -static int isSCnumber (expdesc *e, lua_Integer *i) { +static int isSCnumber (expdesc *e, lua_Integer *i, int *isfloat) { if (e->k == VKINT) *i = e->u.ival; else if (!(e->k == VKFLT && floatI(e->u.nval, i))) return 0; /* not a number */ + else + *isfloat = 1; if (!hasjumps(e) && fitsC(*i)) { *i += OFFSET_sC; return 1; @@ -1372,21 +1375,20 @@ static void codeshift (FuncState *fs, OpCode op, /* -** Emit code for order comparisons. -** When the first operand A is an integral value in the proper range, -** change (A < B) to (B > A) and (A <= B) to (B >= A) so that -** it can use an immediate operand. +** Emit code for order comparisons. When using an immediate operand, +** 'isfloat' tells whether the original value was a float. */ static void codeorder (FuncState *fs, OpCode op, expdesc *e1, expdesc *e2) { int r1, r2; lua_Integer im; - if (isSCnumber(e2, &im)) { + int isfloat = 0; + if (isSCnumber(e2, &im, &isfloat)) { /* use immediate operand */ r1 = luaK_exp2anyreg(fs, e1); r2 = cast_int(im); op = cast(OpCode, (op - OP_LT) + OP_LTI); } - else if (isSCnumber(e1, &im)) { + else if (isSCnumber(e1, &im, &isfloat)) { /* transform (A < B) to (B > A) and (A <= B) to (B >= A) */ r1 = luaK_exp2anyreg(fs, e2); r2 = cast_int(im); @@ -1397,7 +1399,7 @@ static void codeorder (FuncState *fs, OpCode op, expdesc *e1, expdesc *e2) { r2 = luaK_exp2anyreg(fs, e2); } freeexps(fs, e1, e2); - e1->u.info = condjump(fs, op, r1, r2, 1); + e1->u.info = condjump(fs, op, r1, r2, isfloat, 1); e1->k = VJMP; } @@ -1409,13 +1411,14 @@ static void codeorder (FuncState *fs, OpCode op, expdesc *e1, expdesc *e2) { static void codeeq (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) { int r1, r2; lua_Integer im; + int isfloat = 0; /* not needed here, but kept for symmetry */ OpCode op; if (e1->k != VNONRELOC) { lua_assert(e1->k == VK || e1->k == VKINT || e1->k == VKFLT); swapexps(e1, e2); } r1 = luaK_exp2anyreg(fs, e1); /* 1nd expression must be in register */ - if (isSCnumber(e2, &im)) { + if (isSCnumber(e2, &im, &isfloat)) { op = OP_EQI; r2 = cast_int(im); /* immediate operand */ } @@ -1428,7 +1431,7 @@ static void codeeq (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) { r2 = luaK_exp2anyreg(fs, e2); } freeexps(fs, e1, e2); - e1->u.info = condjump(fs, op, r1, r2, (opr == OPR_EQ)); + e1->u.info = condjump(fs, op, r1, r2, isfloat, (opr == OPR_EQ)); e1->k = VJMP; } @@ -1489,7 +1492,8 @@ void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) { case OPR_LT: case OPR_LE: case OPR_GT: case OPR_GE: { lua_Integer dummy; - if (!isSCnumber(v, &dummy)) + int dummy2; + if (!isSCnumber(v, &dummy, &dummy2)) luaK_exp2anyreg(fs, v); /* else keep numeral, which may be an immediate operand */ break; diff --git a/ljumptab.h b/ljumptab.h index fa4277cc86..2d4cf28bf6 100644 --- a/ljumptab.h +++ b/ljumptab.h @@ -107,7 +107,7 @@ static void *disptab[NUM_OPCODES] = { &&L_OP_SETLIST, &&L_OP_CLOSURE, &&L_OP_VARARG, -&&L_OP_PREPVARARG, +&&L_OP_VARARGPREP, &&L_OP_EXTRAARG }; diff --git a/llimits.h b/llimits.h index 9d35d1c774..155bb16081 100644 --- a/llimits.h +++ b/llimits.h @@ -325,6 +325,8 @@ typedef l_uint32 Instruction; #define luai_numeq(a,b) ((a)==(b)) #define luai_numlt(a,b) ((a)<(b)) #define luai_numle(a,b) ((a)<=(b)) +#define luai_numgt(a,b) ((a)>(b)) +#define luai_numge(a,b) ((a)>=(b)) #define luai_numisnan(a) (!luai_numeq((a), (a))) #endif diff --git a/lopcodes.c b/lopcodes.c index c35a0aaf4c..23c3a6e451 100644 --- a/lopcodes.c +++ b/lopcodes.c @@ -101,7 +101,7 @@ LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = { ,opmode(0, 1, 0, 0, iABC) /* OP_SETLIST */ ,opmode(0, 0, 0, 1, iABx) /* OP_CLOSURE */ ,opmode(1, 0, 0, 1, iABC) /* OP_VARARG */ - ,opmode(0, 0, 0, 1, iABC) /* OP_PREPVARARG */ + ,opmode(0, 0, 0, 1, iABC) /* OP_VARARGPREP */ ,opmode(0, 0, 0, 0, iAx) /* OP_EXTRAARG */ }; diff --git a/lopcodes.h b/lopcodes.h index f867a01bb3..bbdd68978b 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -294,7 +294,7 @@ OP_CLOSURE,/* A Bx R(A) := closure(KPROTO[Bx]) */ OP_VARARG,/* A C R(A), R(A+1), ..., R(A+C-2) = vararg */ -OP_PREPVARARG,/*A (adjust vararg parameters) */ +OP_VARARGPREP,/*A (adjust vararg parameters) */ OP_EXTRAARG/* Ax extra (larger) argument for previous opcode */ } OpCode; @@ -331,6 +331,9 @@ OP_EXTRAARG/* Ax extra (larger) argument for previous opcode */ C > 0 means the function is vararg and (C - 1) is its number of fixed parameters. + (*) In comparisons with an immediate operand, C signals whether the + original operand was a float. + ===========================================================================*/ diff --git a/lopnames.h b/lopnames.h index dfca34d1f9..28535fe226 100644 --- a/lopnames.h +++ b/lopnames.h @@ -92,7 +92,7 @@ static const char *const opnames[] = { "SETLIST", "CLOSURE", "VARARG", - "PREPVARARG", + "VARARGPREP", "EXTRAARG", NULL }; diff --git a/lparser.c b/lparser.c index 8ffd97423a..4c2ddbfe50 100644 --- a/lparser.c +++ b/lparser.c @@ -817,7 +817,7 @@ static void constructor (LexState *ls, expdesc *t) { static void setvararg (FuncState *fs, int nparams) { fs->f->is_vararg = 1; - luaK_codeABC(fs, OP_PREPVARARG, nparams, 0, 0); + luaK_codeABC(fs, OP_VARARGPREP, nparams, 0, 0); } diff --git a/ltm.c b/ltm.c index 23a97a62cc..c4fd762bc1 100644 --- a/ltm.c +++ b/ltm.c @@ -205,9 +205,13 @@ int luaT_callorderTM (lua_State *L, const TValue *p1, const TValue *p2, int luaT_callorderiTM (lua_State *L, const TValue *p1, int v2, - int inv, TMS event) { + int inv, int isfloat, TMS event) { TValue aux; const TValue *p2; - setivalue(&aux, v2); + if (isfloat) { + setfltvalue(&aux, cast_num(v2)); + } + else + setivalue(&aux, v2); if (inv) { /* arguments were exchanged? */ p2 = p1; p1 = &aux; /* correct them */ } diff --git a/ltm.h b/ltm.h index fad47842a1..e308fb80e0 100644 --- a/ltm.h +++ b/ltm.h @@ -82,7 +82,7 @@ LUAI_FUNC void luaT_trybiniTM (lua_State *L, const TValue *p1, lua_Integer i2, LUAI_FUNC int luaT_callorderTM (lua_State *L, const TValue *p1, const TValue *p2, TMS event); LUAI_FUNC int luaT_callorderiTM (lua_State *L, const TValue *p1, int v2, - int inv, TMS event); + int inv, int isfloat, TMS event); LUAI_FUNC void luaT_adjustvarargs (lua_State *L, int nfixparams, struct CallInfo *ci, const Proto *p); diff --git a/lvm.c b/lvm.c index 31b4dc36fc..47bc67c94d 100644 --- a/lvm.c +++ b/lvm.c @@ -772,11 +772,10 @@ void luaV_finishOp (lua_State *L) { /* ** {================================================================== -** Macros for arithmetic/bitwise opcodes in 'luaV_execute' +** Macros for arithmetic/bitwise/comparison opcodes in 'luaV_execute' ** =================================================================== */ - #define l_addi(L,a,b) intop(+, a, b) #define l_subi(L,a,b) intop(-, a, b) #define l_muli(L,a,b) intop(*, a, b) @@ -784,6 +783,11 @@ void luaV_finishOp (lua_State *L) { #define l_bor(L,a,b) intop(|, a, b) #define l_bxor(L,a,b) intop(^, a, b) +#define l_lti(a,b) (a < b) +#define l_lei(a,b) (a <= b) +#define l_gti(a,b) (a > b) +#define l_gei(a,b) (a >= b) + /* ** Auxiliary macro for arithmetic operations over floats and others @@ -916,6 +920,36 @@ void luaV_finishOp (lua_State *L) { else \ Protect(luaT_trybinTM(L, v1, v2, ra, tm)); } + +/* +** Order operations with register operands. +*/ +#define op_order(L,opi,opf,other) { \ + TValue *rb = vRB(i); \ + if (ttisinteger(s2v(ra)) && ttisinteger(rb)) \ + cond = opi(ivalue(s2v(ra)), ivalue(rb)); \ + else if (ttisnumber(s2v(ra)) && ttisnumber(rb)) \ + cond = opf(s2v(ra), rb); \ + else \ + Protect(cond = other(L, s2v(ra), rb)); \ + docondjump(); } + + +/* +** Order operations with immediate operand. +*/ +#define op_orderI(L,opi,opf,inv,tm) { \ + int im = GETARG_sB(i); \ + if (ttisinteger(s2v(ra))) \ + cond = opi(ivalue(s2v(ra)), im); \ + else if (ttisfloat(s2v(ra))) \ + cond = opf(fltvalue(s2v(ra)), cast_num(im)); \ + else { \ + int isf = GETARG_C(i); \ + Protect(cond = luaT_callorderiTM(L, s2v(ra), im, inv, isf, tm)); \ + } \ + docondjump(); } + /* }================================================================== */ @@ -1034,7 +1068,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { pc = ci->u.l.savedpc; if (trap) { if (cl->p->is_vararg) - trap = 0; /* hooks will start after PREPVARARG instruction */ + trap = 0; /* hooks will start after VARARGPREP instruction */ else if (pc == cl->p->code) /* first instruction (not resuming)? */ luaD_hookcall(L, ci); ci->u.l.trap = 1; /* there may be other hooks */ @@ -1447,25 +1481,11 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_LT) { - TValue *rb = vRB(i); - if (ttisinteger(s2v(ra)) && ttisinteger(rb)) - cond = (ivalue(s2v(ra)) < ivalue(rb)); - else if (ttisnumber(s2v(ra)) && ttisnumber(rb)) - cond = LTnum(s2v(ra), rb); - else - Protect(cond = lessthanothers(L, s2v(ra), rb)); - docondjump(); + op_order(L, l_lti, LTnum, lessthanothers); vmbreak; } vmcase(OP_LE) { - TValue *rb = vRB(i); - if (ttisinteger(s2v(ra)) && ttisinteger(rb)) - cond = (ivalue(s2v(ra)) <= ivalue(rb)); - else if (ttisnumber(s2v(ra)) && ttisnumber(rb)) - cond = LEnum(s2v(ra), rb); - else - Protect(cond = lessequalothers(L, s2v(ra), rb)); - docondjump(); + op_order(L, l_lei, LEnum, lessequalothers); vmbreak; } vmcase(OP_EQK) { @@ -1487,47 +1507,19 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_LTI) { - int im = GETARG_sB(i); - if (ttisinteger(s2v(ra))) - cond = (ivalue(s2v(ra)) < im); - else if (ttisfloat(s2v(ra))) - cond = luai_numlt(fltvalue(s2v(ra)), cast_num(im)); - else - Protect(cond = luaT_callorderiTM(L, s2v(ra), im, 0, TM_LT)); - docondjump(); + op_orderI(L, l_lti, luai_numlt, 0, TM_LT); vmbreak; } vmcase(OP_LEI) { - int im = GETARG_sB(i); - if (ttisinteger(s2v(ra))) - cond = (ivalue(s2v(ra)) <= im); - else if (ttisfloat(s2v(ra))) - cond = luai_numle(fltvalue(s2v(ra)), cast_num(im)); - else - Protect(cond = luaT_callorderiTM(L, s2v(ra), im, 0, TM_LE)); - docondjump(); + op_orderI(L, l_lei, luai_numle, 0, TM_LE); vmbreak; } vmcase(OP_GTI) { - int im = GETARG_sB(i); - if (ttisinteger(s2v(ra))) - cond = (im < ivalue(s2v(ra))); - else if (ttisfloat(s2v(ra))) - cond = luai_numlt(cast_num(im), fltvalue(s2v(ra))); - else - Protect(cond = luaT_callorderiTM(L, s2v(ra), im, 1, TM_LT)); - docondjump(); + op_orderI(L, l_gti, luai_numgt, 1, TM_LT); vmbreak; } vmcase(OP_GEI) { - int im = GETARG_sB(i); - if (ttisinteger(s2v(ra))) - cond = (im <= ivalue(s2v(ra))); - else if (ttisfloat(s2v(ra))) - cond = luai_numle(cast_num(im), fltvalue(s2v(ra))); - else - Protect(cond = luaT_callorderiTM(L, s2v(ra), im, 1, TM_LE)); - docondjump(); + op_orderI(L, l_gei, luai_numge, 1, TM_LE); vmbreak; } vmcase(OP_TEST) { @@ -1787,7 +1779,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { Protect(luaT_getvarargs(L, ci, ra, n)); vmbreak; } - vmcase(OP_PREPVARARG) { + vmcase(OP_VARARGPREP) { luaT_adjustvarargs(L, GETARG_A(i), ci, cl->p); updatetrap(ci); if (trap) { diff --git a/onelua.c b/onelua.c new file mode 100644 index 0000000000..3c605981f0 --- /dev/null +++ b/onelua.c @@ -0,0 +1,107 @@ +/* +* one.c -- Lua core, libraries, and interpreter in a single file +*/ + +/* default is to build the full interpreter */ +#ifndef MAKE_LIB +#ifndef MAKE_LUAC +#ifndef MAKE_LUA +#define MAKE_LUA +#endif +#endif +#endif + +/* choose suitable platform-specific features */ +/* some of these may need extra libraries such as -ldl -lreadline -lncurses */ +#if 0 +#define LUA_USE_LINUX +#define LUA_USE_MACOSX +#define LUA_USE_POSIX +#define LUA_ANSI +#endif + +/* no need to change anything below this line ----------------------------- */ + +#include "lprefix.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +/* setup for luaconf.h */ +#define LUA_CORE +#define LUA_LIB +#define ltable_c +#define lvm_c +#include "luaconf.h" + +/* do not export internal symbols */ +#undef LUAI_FUNC +#undef LUAI_DDEC +#undef LUAI_DDEF +#define LUAI_FUNC static +#define LUAI_DDEC(def) /* empty */ +#define LUAI_DDEF static + +/* core -- used by all */ +#include "lzio.c" +#include "lctype.c" +#include "lopcodes.c" +#include "lmem.c" +#include "lundump.c" +#include "ldump.c" +#include "lstate.c" +#include "lgc.c" +#include "llex.c" +#include "lcode.c" +#include "lparser.c" +#include "ldebug.c" +#include "lfunc.c" +#include "lobject.c" +#include "ltm.c" +#include "lstring.c" +#include "ltable.c" +#include "ldo.c" +#include "lvm.c" +#include "lapi.c" + +/* auxiliary library -- used by all */ +#include "lauxlib.c" + +/* standard library -- not used by luac */ +#ifndef MAKE_LUAC +#include "lbaselib.c" +#include "lcorolib.c" +#include "ldblib.c" +#include "liolib.c" +#include "lmathlib.c" +#include "loadlib.c" +#include "loslib.c" +#include "lstrlib.c" +#include "ltablib.c" +#include "lutf8lib.c" +#include "linit.c" +#endif + +/* lua */ +#ifdef MAKE_LUA +#include "lua.c" +#endif + +/* luac */ +#ifdef MAKE_LUAC +#include "luac.c" +#endif diff --git a/testes/db.lua b/testes/db.lua index 0858dd2016..95275fb41d 100644 --- a/testes/db.lua +++ b/testes/db.lua @@ -162,7 +162,7 @@ test([[for i,v in pairs{'a','b'} do end ]], {1,2,1,2,1,3}) -test([[for i=1,4 do a=1 end]], {1,1,1,1}, true) +test([[for i=1,4 do a=1 end]], {1,1,1,1}) do -- testing line info/trace with large gaps in source diff --git a/testes/events.lua b/testes/events.lua index ac630d895c..cf68d1e99c 100644 --- a/testes/events.lua +++ b/testes/events.lua @@ -138,64 +138,55 @@ t.__bxor = f("bxor") t.__shl = f("shl") t.__shr = f("shr") t.__bnot = f("bnot") +t.__lt = f("lt") +t.__le = f("le") + + +local function checkcap (t) + assert(#cap + 1 == #t) + for i = 1, #t do + assert(cap[i - 1] == t[i]) + assert(math.type(cap[i - 1]) == math.type(t[i])) + end +end -- Some tests are done inside small anonymous functions to ensure -- that constants go to constant table even in debug compilation, -- when the constant table is very small. -assert(b+5 == b) -assert(cap[0] == "add" and cap[1] == b and cap[2] == 5 and cap[3]==undef) -assert(5.2 + b == 5.2) -assert(cap[0] == "add" and cap[1] == 5.2 and cap[2] == b and cap[3]==undef) -assert(b+'5' == b) -assert(cap[0] == "add" and cap[1] == b and cap[2] == '5' and cap[3]==undef) -assert(5+b == 5) -assert(cap[0] == "add" and cap[1] == 5 and cap[2] == b and cap[3]==undef) -assert('5'+b == '5') -assert(cap[0] == "add" and cap[1] == '5' and cap[2] == b and cap[3]==undef) -b=b-3; assert(getmetatable(b) == t) -assert(cap[0] == "sub" and cap[1] == b and cap[2] == 3 and cap[3]==undef) -assert(5-a == 5) -assert(cap[0] == "sub" and cap[1] == 5 and cap[2] == a and cap[3]==undef) -assert('5'-a == '5') -assert(cap[0] == "sub" and cap[1] == '5' and cap[2] == a and cap[3]==undef) -assert(a*a == a) -assert(cap[0] == "mul" and cap[1] == a and cap[2] == a and cap[3]==undef) -assert(a/0 == a) -assert(cap[0] == "div" and cap[1] == a and cap[2] == 0 and cap[3]==undef) -assert(a%2 == a) -assert(cap[0] == "mod" and cap[1] == a and cap[2] == 2 and cap[3]==undef) -assert(a // (1/0) == a) -assert(cap[0] == "idiv" and cap[1] == a and cap[2] == 1/0 and cap[3]==undef) -;(function () assert(a & "hi" == a) end)() -assert(cap[0] == "band" and cap[1] == a and cap[2] == "hi" and cap[3]==undef) -;(function () assert(10 & a == 10) end)() -assert(cap[0] == "band" and cap[1] == 10 and cap[2] == a and cap[3]==undef) -;(function () assert(a | 10 == a) end)() -assert(cap[0] == "bor" and cap[1] == a and cap[2] == 10 and cap[3]==undef) -assert(a | "hi" == a) -assert(cap[0] == "bor" and cap[1] == a and cap[2] == "hi" and cap[3]==undef) -assert("hi" ~ a == "hi") -assert(cap[0] == "bxor" and cap[1] == "hi" and cap[2] == a and cap[3]==undef) -;(function () assert(10 ~ a == 10) end)() -assert(cap[0] == "bxor" and cap[1] == 10 and cap[2] == a and cap[3]==undef) -assert(-a == a) -assert(cap[0] == "unm" and cap[1] == a) -assert(a^4 == a) -assert(cap[0] == "pow" and cap[1] == a and cap[2] == 4 and cap[3]==undef) -assert(a^'4' == a) -assert(cap[0] == "pow" and cap[1] == a and cap[2] == '4' and cap[3]==undef) -assert(4^a == 4) -assert(cap[0] == "pow" and cap[1] == 4 and cap[2] == a and cap[3]==undef) -assert('4'^a == '4') -assert(cap[0] == "pow" and cap[1] == '4' and cap[2] == a and cap[3]==undef) -assert(#a == a) -assert(cap[0] == "len" and cap[1] == a) -assert(~a == a) -assert(cap[0] == "bnot" and cap[1] == a) -assert(a << 3 == a) -assert(cap[0] == "shl" and cap[1] == a and cap[2] == 3) -assert(1.5 >> a == 1.5) -assert(cap[0] == "shr" and cap[1] == 1.5 and cap[2] == a) +assert(b+5 == b); checkcap{"add", b, 5} +assert(5.2 + b == 5.2); checkcap{"add", 5.2, b} +assert(b+'5' == b); checkcap{"add", b, '5'} +assert(5+b == 5); checkcap{"add", 5, b} +assert('5'+b == '5'); checkcap{"add", '5', b} +b=b-3; assert(getmetatable(b) == t); checkcap{"sub", b, 3} +assert(5-a == 5); checkcap{"sub", 5, a} +assert('5'-a == '5'); checkcap{"sub", '5', a} +assert(a*a == a); checkcap{"mul", a, a} +assert(a/0 == a); checkcap{"div", a, 0} +assert(a/0.0 == a); checkcap{"div", a, 0.0} +assert(a%2 == a); checkcap{"mod", a, 2} +assert(a // (1/0) == a); checkcap{"idiv", a, 1/0} +;(function () assert(a & "hi" == a) end)(); checkcap{"band", a, "hi"} +;(function () assert(10 & a == 10) end)(); checkcap{"band", 10, a} +;(function () assert(a | 10 == a) end)(); checkcap{"bor", a, 10} +assert(a | "hi" == a); checkcap{"bor", a, "hi"} +assert("hi" ~ a == "hi"); checkcap{"bxor", "hi", a} +;(function () assert(10 ~ a == 10) end)(); checkcap{"bxor", 10, a} +assert(-a == a); checkcap{"unm", a, a} +assert(a^4.0 == a); checkcap{"pow", a, 4.0} +assert(a^'4' == a); checkcap{"pow", a, '4'} +assert(4^a == 4); checkcap{"pow", 4, a} +assert('4'^a == '4'); checkcap{"pow", '4', a} +assert(#a == a); checkcap{"len", a, a} +assert(~a == a); checkcap{"bnot", a, a} +assert(a << 3 == a); checkcap{"shl", a, 3} +assert(1.5 >> a == 1.5); checkcap{"shr", 1.5, a} + +-- for comparsion operators, all results are true +assert(5.0 > a); checkcap{"lt", a, 5.0} +assert(a >= 10); checkcap{"le", 10, a} +assert(a <= -10.0); checkcap{"le", a, -10.0} +assert(a < -10); checkcap{"lt", a, -10} -- test for rawlen From 7ceb2154ed69170f3e47f7a5a840e543c7c6ed3d Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 25 Mar 2019 10:38:56 -0300 Subject: [PATCH 0390/1145] Fixed small bugs/issues - In 'readutf8esc' (llex.c), the overflow check must be done before shifting the accumulator. It was working because tests were using 64-bit longs. Failed with 32-bit longs. - In OP_FORPREP (lvm.c), avoid negating an unsigned value. Visual Studio gives a warning for that operation, despite being well defined in ISO C. - In 'luaV_execute' (lvm.c), 'cond' can be defined only when needed, like all other variables. --- llex.c | 2 +- lvm.c | 12 ++++++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/llex.c b/llex.c index 1539f5258c..b0bab37736 100644 --- a/llex.c +++ b/llex.c @@ -334,8 +334,8 @@ static unsigned long readutf8esc (LexState *ls) { r = gethexa(ls); /* must have at least one digit */ while ((save_and_next(ls), lisxdigit(ls->current))) { i++; + esccheck(ls, r <= (0x7FFFFFFFu >> 4), "UTF-8 value too large"); r = (r << 4) + luaO_hexavalue(ls->current); - esccheck(ls, r <= 0x7FFFFFFFu, "UTF-8 value too large"); } esccheck(ls, ls->current == '}', "missing '}'"); next(ls); /* skip '}' */ diff --git a/lvm.c b/lvm.c index 47bc67c94d..d0358143e0 100644 --- a/lvm.c +++ b/lvm.c @@ -925,6 +925,7 @@ void luaV_finishOp (lua_State *L) { ** Order operations with register operands. */ #define op_order(L,opi,opf,other) { \ + int cond; \ TValue *rb = vRB(i); \ if (ttisinteger(s2v(ra)) && ttisinteger(rb)) \ cond = opi(ivalue(s2v(ra)), ivalue(rb)); \ @@ -939,6 +940,7 @@ void luaV_finishOp (lua_State *L) { ** Order operations with immediate operand. */ #define op_orderI(L,opi,opf,inv,tm) { \ + int cond; \ int im = GETARG_sB(i); \ if (ttisinteger(s2v(ra))) \ cond = opi(ivalue(s2v(ra)), im); \ @@ -1076,7 +1078,6 @@ void luaV_execute (lua_State *L, CallInfo *ci) { base = ci->func + 1; /* main loop of interpreter */ for (;;) { - int cond; /* flag for conditional jumps */ Instruction i; /* instruction being executed */ StkId ra; /* instruction's A register */ vmfetch(); @@ -1475,6 +1476,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_EQ) { + int cond; TValue *rb = vRB(i); Protect(cond = luaV_equalobj(L, s2v(ra), rb)); docondjump(); @@ -1491,11 +1493,12 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmcase(OP_EQK) { TValue *rb = KB(i); /* basic types do not use '__eq'; we can use raw equality */ - cond = luaV_equalobj(NULL, s2v(ra), rb); + int cond = luaV_equalobj(NULL, s2v(ra), rb); docondjump(); vmbreak; } vmcase(OP_EQI) { + int cond; int im = GETARG_sB(i); if (ttisinteger(s2v(ra))) cond = (ivalue(s2v(ra)) == im); @@ -1523,7 +1526,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_TEST) { - cond = !l_isfalse(s2v(ra)); + int cond = !l_isfalse(s2v(ra)); docondjump(); vmbreak; } @@ -1679,7 +1682,8 @@ void luaV_execute (lua_State *L, CallInfo *ci) { } else { /* step < 0; descending loop */ count = l_castS2U(init) - l_castS2U(limit); - count /= -l_castS2U(step); + /* 'step+1' avoids negating 'mininteger' */ + count /= l_castS2U(-(step + 1)) + 1u; } /* store the counter in place of the limit (which won't be needed anymore */ From f9b0cf0e2ee35c5444959f77e95f3f07376b9e3e Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 25 Mar 2019 14:00:09 -0300 Subject: [PATCH 0391/1145] Year in copyright notice updated to 2019 --- lua.h | 4 ++-- manual/2html | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lua.h b/lua.h index 09611db574..7c4a13bceb 100644 --- a/lua.h +++ b/lua.h @@ -25,7 +25,7 @@ #define LUA_VERSION "Lua " LUA_VERSION_MAJOR "." LUA_VERSION_MINOR #define LUA_RELEASE LUA_VERSION "." LUA_VERSION_RELEASE -#define LUA_COPYRIGHT LUA_RELEASE " Copyright (C) 1994-2018 Lua.org, PUC-Rio" +#define LUA_COPYRIGHT LUA_RELEASE " Copyright (C) 1994-2019 Lua.org, PUC-Rio" #define LUA_AUTHORS "R. Ierusalimschy, L. H. de Figueiredo, W. Celes" @@ -487,7 +487,7 @@ struct lua_Debug { /****************************************************************************** -* Copyright (C) 1994-2018 Lua.org, PUC-Rio. +* Copyright (C) 1994-2019 Lua.org, PUC-Rio. * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the diff --git a/manual/2html b/manual/2html index 04b2c61ed0..605c6e59f5 100755 --- a/manual/2html +++ b/manual/2html @@ -30,7 +30,7 @@ by Roberto Ierusalimschy, Luiz Henrique de Figueiredo, Waldemar Celes

Copyright -© 2018 Lua.org, PUC-Rio. All rights reserved. +© 2019 Lua.org, PUC-Rio. All rights reserved.


From 0443ad9e288825b6e4441eb11104bcdb4ff4593a Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 25 Mar 2019 14:12:06 -0300 Subject: [PATCH 0392/1145] LUAI_MAXCCALLS renamed LUAI_MAXCSTACK The limit LUAI_MAXCCALLS was renamed LUAI_MAXCSTACK, which better represents its meaning. Moreover, its definition was moved to 'luaconf.h', given its importance now that Lua does not use a "stackless" implementation. --- ldo.c | 4 ++-- llimits.h | 9 --------- lstate.c | 16 ++++++++-------- ltests.h | 4 ++-- luaconf.h | 15 +++++++++++++++ 5 files changed, 27 insertions(+), 21 deletions(-) diff --git a/ldo.c b/ldo.c index 077109c4ab..2a98c3977a 100644 --- a/ldo.c +++ b/ldo.c @@ -521,7 +521,7 @@ void luaD_call (lua_State *L, StkId func, int nresults) { */ void luaD_callnoyield (lua_State *L, StkId func, int nResults) { incXCcalls(L); - if (getCcalls(L) >= LUAI_MAXCCALLS) /* possible stack overflow? */ + if (getCcalls(L) >= LUAI_MAXCSTACK) /* possible stack overflow? */ luaE_freeCI(L); luaD_call(L, func, nResults); decXCcalls(L); @@ -673,7 +673,7 @@ LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs, L->nCcalls = 1; else /* correct 'nCcalls' for this thread */ L->nCcalls = getCcalls(from) - from->nci + L->nci + CSTACKCF; - if (L->nCcalls >= LUAI_MAXCCALLS) + if (L->nCcalls >= LUAI_MAXCSTACK) return resume_error(L, "C stack overflow", nargs); luai_userstateresume(L, nargs); api_checknelems(L, (L->status == LUA_OK) ? nargs + 1 : nargs); diff --git a/llimits.h b/llimits.h index 155bb16081..3df873dbc2 100644 --- a/llimits.h +++ b/llimits.h @@ -168,15 +168,6 @@ typedef LUAI_UACINT l_uacInt; #endif -/* -** maximum depth for nested C calls and syntactical nested non-terminals -** in a program. (Value must fit in an unsigned short int. It must also -** be compatible with the size of the C stack.) -*/ -#if !defined(LUAI_MAXCCALLS) -#define LUAI_MAXCCALLS 2200 -#endif - /* diff --git a/lstate.c b/lstate.c index f5579a6613..463a47d2bf 100644 --- a/lstate.c +++ b/lstate.c @@ -100,13 +100,13 @@ void luaE_setdebt (global_State *g, l_mem debt) { ** Increment count of "C calls" and check for overflows. In case of ** a stack overflow, check appropriate error ("regular" overflow or ** overflow while handling stack overflow). -** If 'nCcalls' is larger than LUAI_MAXCCALLS but smaller than -** LUAI_MAXCCALLS + CSTACKCF (plus 2 to avoid by-one errors), it means +** If 'nCcalls' is larger than LUAI_MAXCSTACK but smaller than +** LUAI_MAXCSTACK + CSTACKCF (plus 2 to avoid by-one errors), it means ** it has just entered the "overflow zone", so the function raises an ** overflow error. -** If 'nCcalls' is larger than LUAI_MAXCCALLS + CSTACKCF + 2 +** If 'nCcalls' is larger than LUAI_MAXCSTACK + CSTACKCF + 2 ** (which means it is already handling an overflow) but smaller than -** 9/8 of LUAI_MAXCCALLS, does not report an error (to allow message +** 9/8 of LUAI_MAXCSTACK, does not report an error (to allow message ** handling to work). ** Otherwise, report a stack overflow while handling a stack overflow ** (probably caused by a repeating error in the message handling @@ -115,16 +115,16 @@ void luaE_setdebt (global_State *g, l_mem debt) { void luaE_enterCcall (lua_State *L) { int ncalls = getCcalls(L); L->nCcalls++; - if (ncalls >= LUAI_MAXCCALLS) { /* possible overflow? */ + if (ncalls >= LUAI_MAXCSTACK) { /* possible overflow? */ luaE_freeCI(L); /* release unused CIs */ ncalls = getCcalls(L); /* update call count */ - if (ncalls >= LUAI_MAXCCALLS) { /* still overflow? */ - if (ncalls <= LUAI_MAXCCALLS + CSTACKCF + 2) { + if (ncalls >= LUAI_MAXCSTACK) { /* still overflow? */ + if (ncalls <= LUAI_MAXCSTACK + CSTACKCF + 2) { /* no error before increments; raise the error now */ L->nCcalls += (CSTACKCF + 4); /* avoid raising it again */ luaG_runerror(L, "C stack overflow"); } - else if (ncalls >= (LUAI_MAXCCALLS + (LUAI_MAXCCALLS >> 3))) + else if (ncalls >= (LUAI_MAXCSTACK + (LUAI_MAXCSTACK >> 3))) luaD_throw(L, LUA_ERRERR); /* error while handling stack error */ } } diff --git a/ltests.h b/ltests.h index 997e1c4b08..a22c98e17f 100644 --- a/ltests.h +++ b/ltests.h @@ -30,8 +30,8 @@ /* compiled with -O0, Lua uses a lot of C stack space... */ -#undef LUAI_MAXCCALLS -#define LUAI_MAXCCALLS 400 +#undef LUAI_MAXCSTACK +#define LUAI_MAXCSTACK 400 /* to avoid warnings, and to make sure value is really unused */ #define UNUSED(x) (x=0, (void)(x)) diff --git a/luaconf.h b/luaconf.h index 0fc161a4b3..5c714d4e53 100644 --- a/luaconf.h +++ b/luaconf.h @@ -27,6 +27,21 @@ ** ===================================================================== */ +/* +@@ LUAI_MAXCSTACK defines the maximum depth for nested calls and +** also limits the maximum depth of other recursive algorithms in +** the implementation, such as syntactic analysis. A value too +** large may allow the interpreter to crash (C-stack overflow). +** The default value seems ok for regular machines, but may be +** too high for restricted hardware. +** The test file 'cstack.lua' may help finding a good limit. +** (It will crash with a limit too high.) +*/ +#if !defined(LUAI_MAXCSTACK) +#define LUAI_MAXCSTACK 2200 +#endif + + /* @@ LUA_32BITS enables Lua with 32-bit integers and 32-bit floats. You ** can also define LUA_32BITS in the make file, but changing here you From d12262068d689eacc452a459a021df0ad8f6d46c Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 27 Mar 2019 14:56:10 -0300 Subject: [PATCH 0393/1145] Small optimizations in range checks Checks of the form '1 <= x && x <= M' were rewritten in the form '(unsigned)x - 1 < (unsigned)M', which is usually more efficient. (Other similar checks have similar translations.) Although some compilers do these optimizations, that does not happen for all compilers or all cases. --- lapi.c | 10 ++++++---- ltable.c | 10 +++++----- ltablib.c | 8 ++++++-- testes/utf8.lua | 3 +++ 4 files changed, 20 insertions(+), 11 deletions(-) diff --git a/lapi.c b/lapi.c index 06396ad3bc..661fdb145f 100644 --- a/lapi.c +++ b/lapi.c @@ -936,8 +936,8 @@ LUA_API int lua_setiuservalue (lua_State *L, int idx, int n) { api_checknelems(L, 1); o = index2value(L, idx); api_check(L, ttisfulluserdata(o), "full userdata expected"); - if (!(0 < n && n <= uvalue(o)->nuvalue)) - res = 0; + if (!(cast_uint(n) - 1u < cast_uint(uvalue(o)->nuvalue))) + res = 0; /* 'n' not in [1, uvalue(o)->nuvalue] */ else { setobj(L, &uvalue(o)->uv[n - 1].uv, s2v(L->top - 1)); luaC_barrierback(L, gcvalue(o), s2v(L->top - 1)); @@ -1313,7 +1313,8 @@ static const char *aux_upvalue (TValue *fi, int n, TValue **val, switch (ttypetag(fi)) { case LUA_TCCL: { /* C closure */ CClosure *f = clCvalue(fi); - if (!(1 <= n && n <= f->nupvalues)) return NULL; + if (!(cast_uint(n) - 1u < cast_uint(f->nupvalues))) + return NULL; /* 'n' not in [1, f->nupvalues] */ *val = &f->upvalue[n-1]; if (owner) *owner = obj2gco(f); return ""; @@ -1322,7 +1323,8 @@ static const char *aux_upvalue (TValue *fi, int n, TValue **val, LClosure *f = clLvalue(fi); TString *name; Proto *p = f->p; - if (!(1 <= n && n <= p->sizeupvalues)) return NULL; + if (!(cast_uint(n) - 1u < cast_uint(p->sizeupvalues))) + return NULL; /* 'n' not in [1, p->sizeupvalues] */ *val = f->upvals[n-1]->v; if (owner) *owner = obj2gco(f->upvals[n - 1]); name = p->upvalues[n-1].name; diff --git a/ltable.c b/ltable.c index e12381b2d8..628c640c2b 100644 --- a/ltable.c +++ b/ltable.c @@ -48,8 +48,8 @@ /* ** MAXASIZE is the maximum size of the array part. It is the minimum -** between 2^MAXABITS and the maximum size such that, measured in bytes, -** it fits in a 'size_t'. +** between 2^MAXABITS and the maximum size that, measured in bytes, +** fits in a 'size_t'. */ #define MAXASIZE luaM_limitN(1u << MAXABITS, TValue) @@ -269,7 +269,7 @@ static const TValue *getgeneric (Table *t, const TValue *key) { ** the array part of a table, 0 otherwise. */ static unsigned int arrayindex (lua_Integer k) { - if (0 < k && l_castS2U(k) <= MAXASIZE) + if (l_castS2U(k) - 1u < MAXASIZE) /* 'k' in [1, MAXASIZE]? */ return cast_uint(k); /* 'key' is an appropriate array index */ else return 0; @@ -286,7 +286,7 @@ static unsigned int findindex (lua_State *L, Table *t, TValue *key, unsigned int i; if (ttisnil(key)) return 0; /* first iteration */ i = ttisinteger(key) ? arrayindex(ivalue(key)) : 0; - if (i != 0 && i <= asize) /* is 'key' inside array part? */ + if (i - 1u < asize) /* is 'key' inside array part? */ return i; /* yes; that's the index */ else { const TValue *n = getgeneric(t, key); @@ -678,7 +678,7 @@ TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) { ** changing the real size of the array). */ const TValue *luaH_getint (Table *t, lua_Integer key) { - if (l_castS2U(key) - 1u < t->alimit) /* (1 <= key && key <= t->alimit)? */ + if (l_castS2U(key) - 1u < t->alimit) /* 'key' in [1, t->alimit]? */ return &t->array[key - 1]; else if (!limitequalsasize(t) && /* key still may be in the array part? */ (l_castS2U(key) == t->alimit + 1 || diff --git a/ltablib.c b/ltablib.c index 29c53e9483..a9169f9e3e 100644 --- a/ltablib.c +++ b/ltablib.c @@ -69,7 +69,9 @@ static int tinsert (lua_State *L) { case 3: { lua_Integer i; pos = luaL_checkinteger(L, 2); /* 2nd argument is the position */ - luaL_argcheck(L, 1 <= pos && pos <= e, 2, "position out of bounds"); + /* check whether 'pos' is in [1, e] */ + luaL_argcheck(L, (lua_Unsigned)pos - 1u < (lua_Unsigned)e, 2, + "position out of bounds"); for (i = e; i > pos; i--) { /* move up elements */ lua_geti(L, 1, i - 1); lua_seti(L, 1, i); /* t[i] = t[i - 1] */ @@ -89,7 +91,9 @@ static int tremove (lua_State *L) { lua_Integer size = aux_getn(L, 1, TAB_RW); lua_Integer pos = luaL_optinteger(L, 2, size); if (pos != size) /* validate 'pos' if given */ - luaL_argcheck(L, 1 <= pos && pos <= size + 1, 1, "position out of bounds"); + /* check whether 'pos' is in [1, size + 1] */ + luaL_argcheck(L, (lua_Unsigned)pos - 1u <= (lua_Unsigned)size, 1, + "position out of bounds"); lua_geti(L, 1, pos); /* result = t[pos] */ for ( ; pos < size; pos++) { lua_geti(L, 1, pos + 1); diff --git a/testes/utf8.lua b/testes/utf8.lua index 86ec1b00f1..b3b7687fb2 100644 --- a/testes/utf8.lua +++ b/testes/utf8.lua @@ -123,6 +123,9 @@ checkerror("continuation byte", utf8.offset, "𦧺", 1, 2) checkerror("continuation byte", utf8.offset, "𦧺", 1, 2) checkerror("continuation byte", utf8.offset, "\x80", 1) +-- error in indices for len +checkerror("out of string", utf8.len, "abc", 0, 2) +checkerror("out of string", utf8.len, "abc", 1, 4) local s = "hello World" From 38425e069243fe6d991f2e99b4bba192af3563c7 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 1 Apr 2019 14:22:07 -0300 Subject: [PATCH 0394/1145] Avoid moving the collector while in 'GCSenteratomic' state The 'GCSenteratomic' is just an auxiliary state for transitioning to 'GCSatomic'. All GC traversals should be done either on the 'GCSpropagate' state or the 'GCSatomic' state. --- lgc.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lgc.c b/lgc.c index 8cb3e9fad5..e9189ac31f 100644 --- a/lgc.c +++ b/lgc.c @@ -1423,6 +1423,7 @@ static lu_mem atomic (lua_State *L) { /* registry and global metatables may be changed by API */ markvalue(g, &g->l_registry); markmt(g); /* mark global metatables */ + work += propagateall(g); /* empties 'gray' list */ /* remark occasional upvalues of (maybe) dead threads */ work += remarkupvals(g); work += propagateall(g); /* propagate changes */ @@ -1486,8 +1487,7 @@ static lu_mem singlestep (lua_State *L) { return propagatemark(g); /* traverse one gray object */ } case GCSenteratomic: { - lu_mem work = propagateall(g); /* make sure gray list is empty */ - work += atomic(L); /* work is what was traversed by 'atomic' */ + lu_mem work = atomic(L); /* work is what was traversed by 'atomic' */ entersweep(L); g->GCestimate = gettotalbytes(g); /* first estimate */; return work; From 5ca1075b714e825006e8ba4f8e7ea5544879bb41 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 4 Apr 2019 11:45:26 -0300 Subject: [PATCH 0395/1145] Added field 'srclen' to structure 'lua_Debug' This new field gets the length of 'source' in the same structure. Unlike the other strings in that structure, 'source' can be relatively large, and Lua already has its length readily available. --- ldblib.c | 3 ++- ldebug.c | 14 +++++++++++--- llimits.h | 4 ++++ lobject.c | 27 ++++++++++++--------------- lobject.h | 2 +- lua.h | 1 + 6 files changed, 31 insertions(+), 20 deletions(-) diff --git a/ldblib.c b/ldblib.c index ada35250c3..d045a82e22 100644 --- a/ldblib.c +++ b/ldblib.c @@ -167,7 +167,8 @@ static int db_getinfo (lua_State *L) { return luaL_argerror(L, arg+2, "invalid option"); lua_newtable(L); /* table to collect results */ if (strchr(options, 'S')) { - settabss(L, "source", ar.source); + lua_pushlstring(L, ar.source, ar.srclen); + lua_setfield(L, -2, "source"); settabss(L, "short_src", ar.short_src); settabsi(L, "linedefined", ar.linedefined); settabsi(L, "lastlinedefined", ar.lastlinedefined); diff --git a/ldebug.c b/ldebug.c index bd471e0c08..6cd4e071e8 100644 --- a/ldebug.c +++ b/ldebug.c @@ -262,18 +262,26 @@ LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) { static void funcinfo (lua_Debug *ar, Closure *cl) { if (noLuaClosure(cl)) { ar->source = "=[C]"; + ar->srclen = LL("=[C]"); ar->linedefined = -1; ar->lastlinedefined = -1; ar->what = "C"; } else { const Proto *p = cl->l.p; - ar->source = p->source ? getstr(p->source) : "=?"; + if (p->source) { + ar->source = getstr(p->source); + ar->srclen = tsslen(p->source); + } + else { + ar->source = "=?"; + ar->srclen = LL("=?"); + } ar->linedefined = p->linedefined; ar->lastlinedefined = p->lastlinedefined; ar->what = (ar->linedefined == 0) ? "main" : "Lua"; } - luaO_chunkid(ar->short_src, ar->source, LUA_IDSIZE); + luaO_chunkid(ar->short_src, ar->source, ar->srclen); } @@ -750,7 +758,7 @@ const char *luaG_addinfo (lua_State *L, const char *msg, TString *src, int line) { char buff[LUA_IDSIZE]; if (src) - luaO_chunkid(buff, getstr(src), LUA_IDSIZE); + luaO_chunkid(buff, getstr(src), tsslen(src)); else { /* no source available; use "?" instead */ buff[0] = '?'; buff[1] = '\0'; } diff --git a/llimits.h b/llimits.h index 3df873dbc2..cc983972ef 100644 --- a/llimits.h +++ b/llimits.h @@ -65,6 +65,10 @@ typedef signed char ls_byte; #define ispow2(x) (((x) & ((x) - 1)) == 0) +/* number of chars of a literal string without the ending \0 */ +#define LL(x) (sizeof(x)/sizeof(char) - 1) + + /* ** conversion of pointer to unsigned integer: ** this is for hashing only; there is no problem if the integer diff --git a/lobject.c b/lobject.c index 5d340de65e..67c37124f3 100644 --- a/lobject.c +++ b/lobject.c @@ -473,45 +473,42 @@ const char *luaO_pushfstring (lua_State *L, const char *fmt, ...) { } -/* number of chars of a literal string without the ending \0 */ -#define LL(x) (sizeof(x)/sizeof(char) - 1) - #define RETS "..." #define PRE "[string \"" #define POS "\"]" #define addstr(a,b,l) ( memcpy(a,b,(l) * sizeof(char)), a += (l) ) -void luaO_chunkid (char *out, const char *source, size_t bufflen) { - size_t l = strlen(source); +void luaO_chunkid (char *out, const char *source, size_t srclen) { + size_t bufflen = LUA_IDSIZE; /* free space in buffer */ if (*source == '=') { /* 'literal' source */ - if (l <= bufflen) /* small enough? */ - memcpy(out, source + 1, l * sizeof(char)); + if (srclen <= bufflen) /* small enough? */ + memcpy(out, source + 1, srclen * sizeof(char)); else { /* truncate it */ addstr(out, source + 1, bufflen - 1); *out = '\0'; } } else if (*source == '@') { /* file name */ - if (l <= bufflen) /* small enough? */ - memcpy(out, source + 1, l * sizeof(char)); + if (srclen <= bufflen) /* small enough? */ + memcpy(out, source + 1, srclen * sizeof(char)); else { /* add '...' before rest of name */ addstr(out, RETS, LL(RETS)); bufflen -= LL(RETS); - memcpy(out, source + 1 + l - bufflen, bufflen * sizeof(char)); + memcpy(out, source + 1 + srclen - bufflen, bufflen * sizeof(char)); } } else { /* string; format as [string "source"] */ const char *nl = strchr(source, '\n'); /* find first new line (if any) */ addstr(out, PRE, LL(PRE)); /* add prefix */ bufflen -= LL(PRE RETS POS) + 1; /* save space for prefix+suffix+'\0' */ - if (l < bufflen && nl == NULL) { /* small one-line source? */ - addstr(out, source, l); /* keep it */ + if (srclen < bufflen && nl == NULL) { /* small one-line source? */ + addstr(out, source, srclen); /* keep it */ } else { - if (nl != NULL) l = nl - source; /* stop at first newline */ - if (l > bufflen) l = bufflen; - addstr(out, source, l); + if (nl != NULL) srclen = nl - source; /* stop at first newline */ + if (srclen > bufflen) srclen = bufflen; + addstr(out, source, srclen); addstr(out, RETS, LL(RETS)); } memcpy(out, POS, (LL(POS) + 1) * sizeof(char)); diff --git a/lobject.h b/lobject.h index 53e67932a0..403b6047ba 100644 --- a/lobject.h +++ b/lobject.h @@ -747,7 +747,7 @@ LUAI_FUNC void luaO_tostring (lua_State *L, TValue *obj); LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp); LUAI_FUNC const char *luaO_pushfstring (lua_State *L, const char *fmt, ...); -LUAI_FUNC void luaO_chunkid (char *out, const char *source, size_t len); +LUAI_FUNC void luaO_chunkid (char *out, const char *source, size_t srclen); #endif diff --git a/lua.h b/lua.h index 7c4a13bceb..ec31c7819f 100644 --- a/lua.h +++ b/lua.h @@ -469,6 +469,7 @@ struct lua_Debug { const char *namewhat; /* (n) 'global', 'local', 'field', 'method' */ const char *what; /* (S) 'Lua', 'C', 'main', 'tail' */ const char *source; /* (S) */ + size_t srclen; /* (S) */ int currentline; /* (l) */ int linedefined; /* (S) */ int lastlinedefined; /* (S) */ From 8004798b0374744208b102bb4cbcf12f904ea120 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 4 Apr 2019 16:31:24 -0300 Subject: [PATCH 0396/1145] Fixed wrong error message in 'return math.seed(0)' Bug introduced in commit 28d829c8: OP_TAILCALL might raise an error without saving 'pc'. (This commit also fixes a detail in 'testes/uf8.lua'.) --- lvm.c | 10 ++++++---- testes/errors.lua | 4 ++++ testes/utf8.lua | 2 +- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/lvm.c b/lvm.c index d0358143e0..45788a010e 100644 --- a/lvm.c +++ b/lvm.c @@ -1556,20 +1556,22 @@ void luaV_execute (lua_State *L, CallInfo *ci) { L->top = ra + b; else /* previous instruction set top */ b = cast_int(L->top - ra); + savepc(ci); /* some calls here can raise errors */ if (TESTARG_k(i)) { int nparams1 = GETARG_C(i); if (nparams1) /* vararg function? */ delta = ci->u.l.nextraargs + nparams1; - /* close upvalues from current call */ - luaF_close(L, base, LUA_OK); - updatestack(ci); + /* close upvalues from current call; the compiler ensures + that there are no to-be-closed variables here */ + luaF_close(L, base, NOCLOSINGMETH); } if (!ttisfunction(s2v(ra))) { /* not a function? */ luaD_tryfuncTM(L, ra); /* try '__call' metamethod */ b++; /* there is now one extra argument */ } if (!ttisLclosure(s2v(ra))) { /* C function? */ - ProtectNT(luaD_call(L, ra, LUA_MULTRET)); /* call it */ + luaD_call(L, ra, LUA_MULTRET); /* call it */ + updatetrap(ci); updatestack(ci); /* stack may have been relocated */ ci->func -= delta; luaD_poscall(L, ci, cast_int(L->top - ra)); diff --git a/testes/errors.lua b/testes/errors.lua index 74975e3143..0b12410eda 100644 --- a/testes/errors.lua +++ b/testes/errors.lua @@ -99,6 +99,10 @@ assert(not string.find(doit"a={13}; local bbbb=1; a[bbbb](3)", "'bbbb'")) checkmessage("a={13}; local bbbb=1; a[bbbb](3)", "number") checkmessage("a=(1)..{}", "a table value") +-- tail calls +checkmessage("local a={}; return a.bbbb(3)", "field 'bbbb'") +checkmessage("a={}; do local a=1 end; return a:bbbb(3)", "method 'bbbb'") + checkmessage("a = #print", "length of a function value") checkmessage("a = #3", "length of a number value") diff --git a/testes/utf8.lua b/testes/utf8.lua index b3b7687fb2..acbb181d24 100644 --- a/testes/utf8.lua +++ b/testes/utf8.lua @@ -66,7 +66,7 @@ local function check (s, t, nonstrict) assert(utf8.len(s, pi, pi1 - 1, nonstrict) == 1) assert(utf8.len(s, pi, -1, nonstrict) == l - i + 1) assert(utf8.len(s, pi1, -1, nonstrict) == l - i) - assert(utf8.len(s, 1, pi, -1, nonstrict) == i) + assert(utf8.len(s, 1, pi, nonstrict) == i) end local i = 0 From 65d1aa7a779b30bf5b0e7b968b3980b702b08b2c Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 9 Apr 2019 18:40:39 -0300 Subject: [PATCH 0397/1145] Syntax should not allow numbers touching identifiers Code like 'a = 1print()' should not be accepted. --- llex.c | 2 ++ testes/literals.lua | 9 +++++++++ 2 files changed, 11 insertions(+) diff --git a/llex.c b/llex.c index b0bab37736..bb81cec430 100644 --- a/llex.c +++ b/llex.c @@ -228,6 +228,8 @@ static int read_numeral (LexState *ls, SemInfo *seminfo) { save_and_next(ls); else break; } + if (lislalnum(ls->current)) /* is numeral touching an alpha num? */ + save_and_next(ls); /* force an error */ save(ls, '\0'); if (luaO_str2num(luaZ_buffer(ls->buff), &obj) == 0) /* format error? */ lexerror(ls, "malformed number", TK_FLT); diff --git a/testes/literals.lua b/testes/literals.lua index fc45d4adf4..27f9377df1 100644 --- a/testes/literals.lua +++ b/testes/literals.lua @@ -306,4 +306,13 @@ assert(not load"a = 'non-ending string\n'") assert(not load"a = '\\345'") assert(not load"a = [=x]") +local function malformednum (n, exp) + local s, msg = load("return " .. n) + assert(not s and string.find(msg, exp)) +end + +malformednum("0xe-", "near ") +malformednum("0xep-p", "malformed number") +malformednum("1print()", "malformed number") + print('OK') From 0f028b9008097f00aa3953a3425c72e7ae2b4c98 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 9 Apr 2019 18:44:13 -0300 Subject: [PATCH 0398/1145] Corrected tests around non-portable 'isdst' in dates The field 'isdst' in date tables may not be present; portable tests should not assume it is. --- testes/files.lua | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/testes/files.lua b/testes/files.lua index 34fcf85134..0a05cf6046 100644 --- a/testes/files.lua +++ b/testes/files.lua @@ -777,7 +777,7 @@ local t = os.time() D = os.date("*t", t) load(os.date([[assert(D.year==%Y and D.month==%m and D.day==%d and D.hour==%H and D.min==%M and D.sec==%S and - D.wday==%w+1 and D.yday==%j and type(D.isdst) == 'boolean')]], t))() + D.wday==%w+1 and D.yday==%j)]], t))() checkerr("invalid conversion specifier", os.date, "%") checkerr("invalid conversion specifier", os.date, "%9") @@ -827,12 +827,16 @@ end D = os.date("!*t", t) load(os.date([[!assert(D.year==%Y and D.month==%m and D.day==%d and D.hour==%H and D.min==%M and D.sec==%S and - D.wday==%w+1 and D.yday==%j and type(D.isdst) == 'boolean')]], t))() + D.wday==%w+1 and D.yday==%j)]], t))() do local D = os.date("*t") local t = os.time(D) - assert(type(D.isdst) == 'boolean') + if not D.isdst then + print("no daylight saving information") + else + assert(type(D.isdst) == 'boolean') + end D.isdst = nil local t1 = os.time(D) assert(t == t1) -- if isdst is absent uses correct default From 979ad95eb114d8d43f928b184f051ac0cfacedf7 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 10 Apr 2019 12:41:56 -0300 Subject: [PATCH 0399/1145] Thorough revision of the reference manual --- manual/manual.of | 692 ++++++++++++++++++++++++----------------------- 1 file changed, 351 insertions(+), 341 deletions(-) diff --git a/manual/manual.of b/manual/manual.of index fc2550e084..9f1ef631c5 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -15,7 +15,7 @@ Lua is dynamically typed, runs by interpreting bytecode with a register-based virtual machine, and has automatic memory management with -incremental garbage collection, +a generational garbage collection, making it ideal for configuration, scripting, and rapid prototyping. @@ -79,7 +79,7 @@ There are eight @x{basic types} in Lua: @def{thread}, and @def{table}. The type @emph{nil} has one single value, @nil, whose main property is to be different from any other value; -it usually represents the absence of a useful value. +it often represents the absence of a useful value. The type @emph{boolean} has two values, @false and @true. Both @nil and @false make a condition false; any other value makes it true. @@ -130,7 +130,8 @@ the programmer can define operations for full userdata values @see{metatable}. Userdata values cannot be created or modified in Lua, only through the @N{C API}. -This guarantees the integrity of data owned by the host program. +This guarantees the integrity of data owned by +the host program and @N{C libraries}. The type @def{thread} represents independent threads of execution and it is used to implement coroutines @see{coroutine}. @@ -146,7 +147,7 @@ used by the @x{IEEE 754} standard to represent undefined numerical results, such as @T{0/0}.) Tables can be @emph{heterogeneous}; that is, they can contain values of all types (except @nil). -Any key with value @nil is not considered part of the table. +Any key associated to the value @nil is not considered part of the table. Conversely, any key that is not part of a table has an associated value @nil. @@ -176,14 +177,10 @@ In particular, floats with integral values are equal to their respective integers (e.g., @T{1.0 == 1}). To avoid ambiguities, -any float with integral value used as a key -is converted to its respective integer. +any float used as a key that is equal to an integer +is converted to that integer. For instance, if you write @T{a[2.0] = true}, -the actual key inserted into the table will be the -integer @T{2}. -(On the other hand, -2 and @St{2} are different Lua values and therefore -denote different table entries.) +the actual key inserted into the table will be the integer @T{2}. Tables, functions, threads, and (full) userdata values are @emph{objects}: @@ -194,13 +191,13 @@ always manipulate references to such values; these operations do not imply any kind of copy. The library function @Lid{type} returns a string describing the type -of a given value @see{predefined}. +of a given value @seeF{type}. } @sect2{globalenv| @title{Environments and the Global Environment} -As will be discussed in @refsec{variables} and @refsec{assignment}, +As we will discuss further in @refsec{variables} and @refsec{assignment}, any reference to a free name (that is, a name not bound to any declaration) @id{var} is syntactically translated to @T{_ENV.var}. @@ -222,14 +219,15 @@ Any table used as the value of @id{_ENV} is called an @def{environment}. Lua keeps a distinguished environment called the @def{global environment}. This value is kept at a special index in the C registry @see{registry}. In Lua, the global variable @Lid{_G} is initialized with this same value. -(@Lid{_G} is never used internally.) +(@Lid{_G} is never used internally, +so changing its value will affect only your own code.) When Lua loads a chunk, the default value for its @id{_ENV} upvalue is the global environment @seeF{load}. Therefore, by default, free names in Lua code refer to entries in the global environment -(and, therefore, they are also called @def{global variables}). +and, therefore, they are also called @def{global variables}. Moreover, all standard libraries are loaded in the global environment and some functions there operate on that environment. You can use @Lid{load} (or @Lid{loadfile}) @@ -284,6 +282,12 @@ Lua breaks it and returns an appropriate message. It is not called for memory-allocation errors nor for errors while running finalizers.) +Lua also offers a system of @emph{warnings} @seeF{warn}. +Unlike errors, warnings do not interfere +in any way with program execution. +They typically only generate a message to the user, +although this behavior can be adapted from C @see{lua_setwarnf}. + } @sect2{metatable| @title{Metatables and Metamethods} @@ -310,20 +314,14 @@ metamethods should be function values. You can query the metatable of any value using the @Lid{getmetatable} function. Lua queries metamethods in metatables using a raw access @seeF{rawget}. -So, to retrieve the metamethod for event @id{ev} in object @id{o}, -Lua does the equivalent to the following code: -@verbatim{ -rawget(getmetatable(@rep{o}) or {}, "__@rep{ev}") -} You can replace the metatable of tables using the @Lid{setmetatable} function. -You cannot change the metatable of other types from Lua code -(except by using the @link{debuglib|debug library}); -you should use the @N{C API} for that. +You cannot change the metatable of other types from Lua code, +except by using the @link{debuglib|debug library}. -Tables and full userdata have individual metatables -(although multiple tables and userdata can share their metatables). +Tables and full userdata have individual metatables, +although multiple tables and userdata can share their metatables. Values of all other types share one single metatable per type; that is, there is one single metatable for all numbers, one for all strings, etc. @@ -351,8 +349,7 @@ Each operation is identified by its corresponding key. @item{@idx{__add}| the addition (@T{+}) operation. -If any operand for an addition is not a number -(nor a string coercible to a number), +If any operand for an addition is not a number, Lua will try to call a metamethod. First, Lua will check the first operand (even if it is valid). If that operand does not define a metamethod for @idx{__add}, @@ -406,7 +403,7 @@ the bitwise AND (@T{&}) operation. Behavior similar to the addition operation, except that Lua will try a metamethod if any operand is neither an integer -nor a value coercible to an integer @see{coercion}. +nor a float coercible to an integer @see{coercion}. } @item{@idx{__bor}| @@ -493,8 +490,8 @@ and the result of the call is the result of the operation. If it is a table, the final result is the result of indexing this table with @id{key}. -(This indexing is regular, not raw, -and therefore can trigger another metamethod.) +This indexing is regular, not raw, +and therefore can trigger another metamethod. } @item{@idx{__newindex}| @@ -510,8 +507,8 @@ If it is a function, it is called with @id{table}, @id{key}, and @id{value} as arguments. If it is a table, Lua does an indexing assignment to this table with the same key and value. -(This assignment is regular, not raw, -and therefore can trigger another metamethod.) +This assignment is regular, not raw, +and therefore can trigger another metamethod. Whenever there is a @idx{__newindex} metamethod, Lua does not perform the primitive assignment. @@ -530,7 +527,7 @@ the metamethod is called with @id{func} as its first argument, followed by the arguments of the original call (@id{args}). All results of the call are the result of the operation. -(This is the only metamethod that allows multiple results.) +This is the only metamethod that allows multiple results. } } @@ -566,17 +563,17 @@ incremental and generational. The default GC mode with the default parameters are adequate for most uses. -Programs that waste a large proportion of its time +However, programs that waste a large proportion of their time allocating and freeing memory can benefit from other settings. Keep in mind that the GC behavior is non-portable both across platforms and across different Lua releases; therefore, optimal settings are also non-portable. You can change the GC mode and parameters by calling -@Lid{lua_gc} in C +@Lid{lua_gc} @N{in C} or @Lid{collectgarbage} in Lua. You can also use these functions to control -the collector directly (e.g., stop and restart it). +the collector directly (e.g., to stop and restart it). @sect3{@title{Incremental Garbage Collection} @@ -601,7 +598,7 @@ to double before starting a new cycle. The default value is 200; the maximum value is 1000. The garbage-collector step multiplier -controls the relative speed of the collector relative to +controls the speed of the collector relative to memory allocation, that is, how many elements it marks or sweeps for each @@ -623,7 +620,7 @@ bytes between steps and perform equivalent work during the step. A large value (e.g., 60) makes the collector a stop-the-world (non-incremental) collector. The default value is 13, -which makes for steps of approximately @N{8 Kbytes}. +which means steps of approximately @N{8 Kbytes}. } @@ -669,8 +666,8 @@ These metamethods, called @def{finalizers}, are called when the garbage collector detects that the corresponding table or userdata is unreachable. Finalizers allow you to coordinate Lua's garbage collection -with external resource management -such as closing files, network or database connections, +with external resource management such as closing files, +network or database connections, or freeing your own memory. For an object (table or userdata) to be finalized when collected, @@ -682,7 +679,7 @@ Note that if you set a metatable without a @idx{__gc} field and later create that field in the metatable, the object will not be marked for finalization. -When a marked object becomes garbage, +When a marked object becomes unreachable, it is not collected immediately by the garbage collector. Instead, Lua puts it in a list. After the collection, @@ -693,7 +690,7 @@ If it is present, Lua calls it with the object as its single argument. At the end of each garbage-collection cycle, -the finalizers for objects are called in +the finalizers are called in the reverse order that the objects were marked for finalization, among those collected in that cycle; that is, the first finalizer to be called is the one associated @@ -723,9 +720,16 @@ If any finalizer marks objects for collection during that phase, these marks have no effect. Finalizers cannot yield. +Except for that, they can do anything, +such as raise errors, create new objects, +or even run the garbage collector. +However, because they can run in unpredictable times, +it is good practice to restrict each finalizer +to the minimum necessary to properly release +its associated resource. Any error while running a finalizer generates a warning; -it is not propagated. +the error is not propagated. } @@ -773,8 +777,10 @@ are not subject to garbage collection, and therefore are not removed from weak tables (unless their associated values are collected). Although strings are subject to garbage collection, -they do not have an explicit construction, -and therefore are not removed from weak tables. +they do not have an explicit construction and +their equality is by value; +they behave more like values than like objects. +Therefore, they are not removed from weak tables. Resurrected objects (that is, objects being finalized @@ -828,7 +834,7 @@ In case of normal termination, @Lid{coroutine.resume} returns @true, plus any values returned by the coroutine main function. In case of errors, @Lid{coroutine.resume} returns @false -plus an error object. +plus the error object. A coroutine yields by calling @Lid{coroutine.yield}. When a coroutine yields, @@ -922,7 +928,7 @@ at the end of this manual. Lua is a @x{free-form} language. It ignores spaces and comments between lexical elements (@x{tokens}), -except as delimiters between @x{names} and @x{keywords}. +except as delimiters between two tokens. In source code, Lua recognizes as spaces the standard ASCII white-space characters space, form feed, newline, @@ -1001,11 +1007,12 @@ it must be expressed using exactly three digits.) The @x{UTF-8} encoding of a @x{Unicode} character can be inserted in a literal string with the escape sequence @T{\u{@rep{XXX}}} -(note the mandatory enclosing brackets), +(with mandatory enclosing brackets), where @rep{XXX} is a sequence of one or more hexadecimal digits representing the character code point. This code point can be any value less than @M{2@sp{31}}. -(Lua uses the original UTF-8 specification here.) +(Lua uses the original UTF-8 specification here, +which is not restricted to valid Unicode code points.) Literal strings can also be defined using a long format enclosed by @def{long brackets}. @@ -1028,9 +1035,9 @@ Any kind of end-of-line sequence (carriage return, newline, carriage return followed by newline, or newline followed by carriage return) is converted to a simple newline. - When the opening long bracket is immediately followed by a newline, the newline is not included in the string. + As an example, in a system using ASCII (in which @Char{a} is coded @N{as 97}, newline is coded @N{as 10}, and @Char{1} is coded @N{as 49}), @@ -1052,7 +1059,7 @@ However, Lua opens files for parsing in text mode, and the system's file functions may have problems with some control characters. So, it is safer to represent -non-text data as a quoted literal with +binary data as a quoted literal with explicit escape sequences for the non-text characters. A @def{numeric constant} (or @def{numeral}) @@ -1106,7 +1113,7 @@ which is a particular kind of local variable): @Produc{ @producname{var}@producbody{@bnfNter{Name}} } -@bnfNter{Name} denotes identifiers, as defined in @See{lexical}. +@bnfNter{Name} denotes identifiers @see{lexical}. Any variable name is assumed to be global unless explicitly declared as a local @see{localvar}. @@ -1139,9 +1146,9 @@ the variable @id{_ENV} itself is never global @see{globalenv}. @sect2{stats| @title{Statements} Lua supports an almost conventional set of @x{statements}, -similar to those in Pascal or C. +similar to those in other conventional languages. This set includes -assignments, control structures, function calls, +blocks, assignments, control structures, function calls, and variable declarations. @sect3{@title{Blocks} @@ -1159,7 +1166,7 @@ or write two semicolons in sequence: @producname{stat}@producbody{@bnfter{;}} } -Function calls and assignments +Both function calls and assignments can start with an open parenthesis. This possibility leads to an ambiguity in Lua's grammar. Consider the following fragment: @@ -1167,7 +1174,7 @@ Consider the following fragment: a = b + c (print or io.write)('done') } -The grammar could see it in two ways: +The grammar could see this fragment in two ways: @verbatim{ a = b + c(print or io.write)('done') @@ -1223,7 +1230,7 @@ and then Lua executes the compiled code with an interpreter for the virtual machine. Chunks can also be precompiled into binary form; -see program @idx{luac} and function @Lid{string.dump} for details. +see the program @idx{luac} and the function @Lid{string.dump} for details. Programs in source and compiled forms are interchangeable; Lua automatically detects the file type and acts accordingly @seeF{load}. @@ -1249,7 +1256,7 @@ the list of variables.@index{adjustment} If there are more values than needed, the excess values are thrown away. If there are fewer values than needed, -the list is extended with as many @nil's as needed. +the list is extended with @nil's. If the list of expressions ends with a function call, then all values returned by that call enter the list of values, before the adjustment @@ -1306,7 +1313,7 @@ The @x{condition expression} of a control structure can return any value. Both @false and @nil test false. All values different from @nil and @false test true. -(In particular, the number 0 and the empty string also test true). +In particular, the number 0 and the empty string also test true. In the @Rw{repeat}@En@Rw{until} loop, the inner block does not end at the @Rw{until} keyword, @@ -1347,7 +1354,7 @@ A @Rw{break} ends the innermost enclosing loop. The @Rw{return} statement is used to return values from a function or a chunk -(which is an anonymous function). +(which is handled as an anonymous function). @index{return statement} Functions can return more than one value, so the syntax for the @Rw{return} statement is @@ -1357,7 +1364,7 @@ so the syntax for the @Rw{return} statement is The @Rw{return} statement can only be written as the last statement of a block. -If it is really necessary to @Rw{return} in the middle of a block, +If it is necessary to @Rw{return} in the middle of a block, then an explicit inner block can be used, as in the idiom @T{do return end}, because now @Rw{return} is the last statement in its (inner) block. @@ -1395,11 +1402,12 @@ until that value passes the limit. A negative step makes a decreasing sequence; a step equal to zero raises an error. If the initial value is already greater than the limit -(or less than, if the step is negative), the body is not executed. +(or less than, if the step is negative), +the body is not executed. If both the initial value and the step are integers, the loop is done with integers; -in this case, the range of the control variable is limited +in this case, the range of the control variable is clipped by the range of integers. Otherwise, the loop is done with floats. (Beware of floating-point accuracy in this case.) @@ -1426,52 +1434,37 @@ The generic @Rw{for} loop has the following syntax: } A @Rw{for} statement like @verbatim{ -for @rep{var_1}, @Cdots, @rep{var_n} in @rep{explist} do @rep{block} end +for @rep{var_1}, @Cdots, @rep{var_n} in @rep{explist} do @rep{body} end } -is equivalent to the code: -@verbatim{ -do - local @rep{f}, @rep{s}, @rep{var} - local *toclose @rep{tbc} = nil - @rep{f}, @rep{s}, @rep{var}, @rep{tbc} = @rep{explist} - while true do - local @rep{var_1}, @Cdots, @rep{var_n} = @rep{f}(@rep{s}, @rep{var}) - if @rep{var_1} == nil then break end - @rep{var} = @rep{var_1} - @rep{block} - end -end -} -Note the following: -@itemize{ +works as follows. -@item{ -@T{@rep{explist}} is evaluated only once. -Its results are an @emph{iterator} function, +The names @rep{var_i} declare loop variables local to the loop body. +The first of these variables is the @emph{control variable}. + +The loop starts by evaluating @rep{explist} +to produce four values: +an @emph{iterator function}, a @emph{state}, -an initial value for the first @emph{iterator variable}, -and a to-be-closed variable @see{to-be-closed}, +an initial value for the control variable, +and a @emph{closing value}. + +Then, at each iteration, +Lua calls the iterator function with two arguments: +the state and the control variable. +The results from this call are then assigned to the loop variables, +following the rules of multiple assignments @see{assignment}. +If the control variable becomes @nil, +the loop terminates. +Otherwise, the body is executed and the loop goes +to the next iteration. + +The closing value behaves like a +to-be-closed variable @see{to-be-closed}, which can be used to release resources when the loop ends. -} - -@item{ -@T{@rep{f}}, @T{@rep{s}}, @T{@rep{var}}, and @T{@rep{tbc}} -are invisible variables. -The names are here for explanatory purposes only. -} - -@item{ -You can use @Rw{break} to exit a @Rw{for} loop. -} +Otherwise, it does not interfere with the loop. -@item{ -The loop variables @T{@rep{var_i}} are local to the loop; -you cannot use their values after the @Rw{for} ends. -If you need these values, -then assign them to other variables before breaking or exiting the loop. -} - -} +You should not change the value of the control variable +during the loop. } @@ -1541,7 +1534,8 @@ the other pending closing methods will still be called. If a coroutine yields inside a block and is never resumed again, the variables visible at that block will never go out of scope, and therefore they will not be closed. -(You should use finalizers to handle this case.) +(You should use finalizers to handle this case, +or else call @Lid{coroutine.kill} to close the variables.) } @@ -1659,7 +1653,7 @@ so that it works for non-integer exponents too. Floor division (@T{//}) is a division that rounds the quotient towards minus infinity, -that is, the floor of the division of its operands. +resulting in the floor of the division of its operands. Modulo is defined as the remainder of a division that rounds the quotient towards minus infinity (floor division). @@ -1725,21 +1719,30 @@ it is in the range of integer representation). If it does, that representation is the result. Otherwise, the conversion fails. -The string library uses metamethods that try to coerce -strings to numbers in all arithmetic operations. -Any string operator is converted to an integer or a float, +Several places in Lua coerce strings to numbers when necessary. +A string is converted to an integer or a float following its syntax and the rules of the Lua lexer. (The string may have also leading and trailing spaces and a sign.) All conversions from strings to numbers accept both a dot and the current locale mark as the radix character. (The Lua lexer, however, accepts only a dot.) +If the string is not a valid numeral, +the conversion fails. +If necessary, the result of this first step is then converted +to the required number subtype following the previous rules +for conversions between floats and integers. + +The string library uses metamethods that try to coerce +strings to numbers in all arithmetic operations. +If the conversion fails, +the library calls the metamethod of the other operand +(if present) or it raises an error. The conversion from numbers to strings uses a non-specified human-readable format. -For complete control over how numbers are converted to strings, -use the @id{format} function from the string library -@seeF{string.format}. +To convert numbers to strings in any specific way, +use the function @Lid{string.format}. } @@ -1758,7 +1761,7 @@ These operators always result in @false or @true. Equality (@T{==}) first compares the type of its operands. If the types are different, then the result is @false. Otherwise, the values of the operands are compared. -Strings are equal if they have the same content. +Strings are equal if they have the same byte content. Numbers are equal if they denote the same mathematical value. Tables, userdata, and threads @@ -1787,8 +1790,8 @@ The operator @T{~=} is exactly the negation of equality (@T{==}). The order operators work as follows. If both arguments are numbers, -then they are compared according to their mathematical values -(regardless of their subtypes). +then they are compared according to their mathematical values, +regardless of their subtypes. Otherwise, if both arguments are strings, then their values are compared according to the current locale. Otherwise, Lua tries to call the @idx{__lt} or the @idx{__le} @@ -1797,8 +1800,8 @@ A comparison @T{a > b} is translated to @T{b < a} and @T{a >= b} is translated to @T{b <= a}. Following the @x{IEEE 754} standard, -@x{NaN} is considered neither less than, -nor equal to, nor greater than any value (including itself). +the special value @x{NaN} is considered neither less than, +nor equal to, nor greater than any value, including itself. } @@ -1836,8 +1839,9 @@ false or nil --> nil @sect3{concat| @title{Concatenation} The string @x{concatenation} operator in Lua is denoted by two dots (@Char{..}). -If both operands are strings or numbers, then they are converted to -strings according to the rules described in @See{coercion}. +If both operands are strings or numbers, +then the numbers are converted to strings +in a non-specified format @see{coercion}. Otherwise, the @idx{__concat} metamethod is called @see{metatable}. } @@ -1846,9 +1850,9 @@ Otherwise, the @idx{__concat} metamethod is called @see{metatable}. The length operator is denoted by the unary prefix operator @T{#}. -The length of a string is its number of bytes -(that is, the usual meaning of string length when each -character is one byte). +The length of a string is its number of bytes. +(That is the usual meaning of string length when each +character is one byte.) The length operator applied on a table returns a @x{border} in that table. @@ -1867,8 +1871,10 @@ For instance, the table @T{{10, 20, 30, 40, 50}} is a sequence, as it has only one border (5). The table @T{{10, 20, 30, nil, 50}} has two borders (3 and 5), and therefore it is not a sequence. +(The @nil at index 4 is called a @emphx{hole}.) The table @T{{nil, 20, 30, nil, nil, 60, nil}} -has three borders (0, 3, and 6), +has three borders (0, 3, and 6) and three holes +(at indices 1, 4, and 5), so it is not a sequence, too. The table @T{{}} is a sequence with border 0. Note that non-natural keys do not interfere @@ -1936,10 +1942,10 @@ Each field of the form @T{[exp1] = exp2} adds to the new table an entry with key @id{exp1} and value @id{exp2}. A field of the form @T{name = exp} is equivalent to @T{["name"] = exp}. -Finally, fields of the form @id{exp} are equivalent to +Fields of the form @id{exp} are equivalent to @T{[i] = exp}, where @id{i} are consecutive integers -starting with 1. -Fields in the other formats do not affect this counting. +starting with 1; +fields in the other formats do not affect this counting. For example, @verbatim{ a = { [f(1)] = g; "x", "y"; x = 1, f(x), [30] = 23; 45 } @@ -1982,8 +1988,9 @@ first @bnfNter{prefixexp} and @bnfNter{args} are evaluated. If the value of @bnfNter{prefixexp} has type @emph{function}, then this function is called with the given arguments. -Otherwise, the @bnfNter{prefixexp} @idx{__call} metamethod is called, -having as first argument the value of @bnfNter{prefixexp}, +Otherwise, if present, +the @bnfNter{prefixexp} @idx{__call} metamethod is called: +its first argument is the value of @bnfNter{prefixexp}, followed by the original call arguments @see{metatable}. @@ -1991,7 +1998,7 @@ The form @Produc{ @producname{functioncall}@producbody{prefixexp @bnfter{:} @bnfNter{Name} args} } -can be used to call @Q{methods}. +can be used to emulate methods. A call @T{v:name(@rep{args})} is syntactic sugar for @T{v.name(v,@rep{args})}, except that @id{v} is evaluated only once. @@ -2014,7 +2021,7 @@ that is, the argument list is a single literal string. A call of the form @T{return @rep{functioncall}} not in the scope of a to-be-closed variable is called a @def{tail call}. Lua implements @def{proper tail calls} -(or @emph{proper tail recursion}): +(or @def{proper tail recursion}): in a tail call, the called function reuses the stack entry of the calling function. Therefore, there is no limit on the number of nested tail calls that @@ -2086,10 +2093,11 @@ contains references to @id{f}.) A function definition is an executable expression, whose value has type @emph{function}. When Lua precompiles a chunk, -all its function bodies are precompiled too. +all its function bodies are precompiled too, +but they are not created yet. Then, whenever Lua executes the function definition, the function is @emph{instantiated} (or @emph{closed}). -This function instance (or @emphx{closure}) +This function instance, or @emphx{closure}, is the final value of the expression. Parameters act as local variables that are @@ -2152,8 +2160,8 @@ that a function may return. This limit is guaranteed to be greater than 1000. The @emphx{colon} syntax -is used for defining @def{methods}, -that is, functions that have an implicit extra parameter @idx{self}. +is used to emulate @def{methods}, +adding an implicit extra parameter @idx{self} to the function. Thus, the statement @verbatim{ function t.a.b.c:f (@rep{params}) @rep{body} end @@ -2282,8 +2290,8 @@ For convenience, most query operations in the API do not follow a strict stack discipline. Instead, they can refer to any element in the stack by using an @emph{index}:@index{index (API stack)} -A positive index represents an absolute stack position -(starting @N{at 1}); +A positive index represents an absolute stack position, +starting @N{at 1} as the bottom of the stack; a negative index represents an offset relative to the top of the stack. More specifically, if the stack has @rep{n} elements, then @N{index 1} represents the first element @@ -2353,7 +2361,7 @@ functions in the API work with acceptable indices. Acceptable indices serve to avoid extra tests against the stack top when querying the stack. For instance, a @N{C function} can query its third argument -without the need to first check whether there is a third argument, +without the need to check whether there is a third argument, that is, without the need to check whether 3 is a valid index. For functions that can be called with acceptable indices, @@ -2385,7 +2393,8 @@ current function which is one plus the maximum number of upvalues in a closure), produces an acceptable but invalid index. -A @N{C closure} can also change the values of its corresponding upvalues. +A @N{C closure} can also change the values +of its corresponding upvalues. } @@ -2394,7 +2403,7 @@ A @N{C closure} can also change the values of its corresponding upvalues. Lua provides a @def{registry}, a predefined table that can be used by any @N{C code} to store whatever Lua values it needs to store. -The registry table is always located at pseudo-index +The registry table is always accessible at pseudo-index @defid{LUA_REGISTRYINDEX}. Any @N{C library} can store data into this table, but it must take care to choose keys @@ -2410,7 +2419,8 @@ uppercase letters are reserved for Lua. The integer keys in the registry are used by the reference mechanism @seeC{luaL_ref} and by some predefined values. -Therefore, integer keys must not be used for other purposes. +Therefore, integer keys in the registry +must not be used for other purposes. When you create a new Lua state, its registry comes with some predefined values. @@ -2435,15 +2445,16 @@ the @x{global environment}. Internally, Lua uses the C @id{longjmp} facility to handle errors. (Lua will use exceptions if you compile it as C++; search for @id{LUAI_THROW} in the source code for details.) -When Lua faces any error -(such as a @x{memory allocation error} or a type error) +When Lua faces any error, +such as a @x{memory allocation error} or a type error, it @emph{raises} an error; that is, it does a long jump. A @emphx{protected environment} uses @id{setjmp} to set a recovery point; any error jumps to the most recent active recovery point. -Inside a @N{C function} you can raise an error by calling @Lid{lua_error}. +Inside a @N{C function} you can raise an error explicitly +by calling @Lid{lua_error}. Most functions in the API can raise an error, for instance due to a @x{memory allocation error}. @@ -2503,9 +2514,9 @@ the @emph{original function}. This original function then calls one of those three functions in the C API, which we will call the @emph{callee function}, that then yields the current thread. -(This can happen when the callee function is @Lid{lua_yieldk}, +This can happen when the callee function is @Lid{lua_yieldk}, or when the callee function is either @Lid{lua_callk} or @Lid{lua_pcallk} -and the function called by them yields.) +and the function called by them yields. Suppose the running thread yields while executing the callee function. After the thread resumes, @@ -2566,11 +2577,11 @@ you can do the equivalent work directly inside the original function.) Besides the Lua state, the continuation function has two other parameters: -the final status of the call plus the context value (@id{ctx}) that +the final status of the call and the context value (@id{ctx}) that was passed originally to @Lid{lua_pcallk}. -(Lua does not use this context value; +Lua does not use this context value; it only passes this value from the original function to the -continuation function.) +continuation function. For @Lid{lua_pcallk}, the status is the same value that would be returned by @Lid{lua_pcallk}, except that it is @Lid{LUA_YIELD} when being executed after a yield @@ -2617,8 +2628,8 @@ A field in the form @T{x|y} means the function can push (or pop) depending on the situation; an interrogation mark @Char{?} means that we cannot know how many elements the function pops/pushes -by looking only at its arguments -(e.g., they may depend on what is on the stack). +by looking only at its arguments. +(For instance, they may depend on what is on the stack.) The third field, @T{x}, tells whether the function may raise errors: @Char{-} means the function never raises any error; @@ -2673,11 +2684,11 @@ Lua assumes the following behavior from the allocator function: When @id{nsize} is zero, the allocator must behave like @id{free} -and return @id{NULL}. +and then return @id{NULL}. When @id{nsize} is not zero, the allocator must behave like @id{realloc}. -The allocator returns @id{NULL} +In particular, the allocator returns @id{NULL} if and only if it cannot fulfill the request. Here is a simple implementation for the @x{allocator function}. @@ -2752,9 +2763,9 @@ in direct order; that is, the first argument is pushed first. Finally you call @Lid{lua_call}; @id{nargs} is the number of arguments that you pushed onto the stack. -All arguments and the function value are popped from the stack -when the function is called. -The function results are pushed onto the stack when the function returns. +When the function returns, +all arguments and the function value are popped +and the function results are pushed onto the stack. The number of results is adjusted to @id{nresults}, unless @id{nresults} is @defid{LUA_MULTRET}. In this case, all results from the function are pushed; @@ -2819,7 +2830,7 @@ The first argument (if any) is at index 1 and its last argument is at index @T{lua_gettop(L)}. To return values to Lua, a @N{C function} just pushes them onto the stack, in direct order (the first result is pushed first), -and returns the number of results. +and returns in C the number of results. Any other value in the stack below the results will be properly discarded by Lua. Like a Lua function, a @N{C function} called by Lua can also return @@ -2853,8 +2864,8 @@ static int foo (lua_State *L) { @APIEntry{int lua_checkstack (lua_State *L, int n);| @apii{0,0,-} -Ensures that the stack has space for at least @id{n} extra slots -(that is, that you can safely push up to @id{n} values into it). +Ensures that the stack has space for at least @id{n} extra slots, +that is, that you can safely push up to @id{n} values into it. It returns false if it cannot fulfill the request, either because it would cause the stack to be greater than a fixed maximum size @@ -2872,6 +2883,7 @@ it is left unchanged. Destroys all objects in the given Lua state (calling the corresponding garbage-collection metamethods, if any) and frees all dynamic memory used by this state. + On several platforms, you may not need to call this function, because all resources are naturally released when the host program ends. On the other hand, long-running programs that create multiple states, @@ -2934,7 +2946,7 @@ will have as a sequence; parameter @id{nrec} is a hint for how many other elements the table will have. Lua may use these hints to preallocate memory for the new table. -This preallocation is useful for performance when you know in advance +This preallocation may help performance when you know in advance how many elements the table will have. Otherwise you can use the function @Lid{lua_newtable}. @@ -3369,11 +3381,11 @@ Other upvalues are initialized with @nil. @APIEntry{lua_State *lua_newstate (lua_Alloc f, void *ud);| @apii{0,0,-} -Creates a new thread running in a new, independent state. -Returns @id{NULL} if it cannot create the thread or the state +Creates a new independent state and returns its main thread. +Returns @id{NULL} if it cannot create the state (due to lack of memory). The argument @id{f} is the @x{allocator function}; -Lua does all memory allocation for this state +Lua will do all memory allocation for this state through this function @seeF{lua_Alloc}. The second argument, @id{ud}, is an opaque pointer that Lua passes to the allocator in every call. @@ -3407,7 +3419,7 @@ like any Lua object. @apii{0,1,m} This function creates and pushes on the stack a new full userdata, -with @id{nuvalue} associated Lua values (called @id{user values}) +with @id{nuvalue} associated Lua values, called @id{user values}, plus an associated block of raw memory with @id{size} bytes. (The user values can be set and read with the functions @Lid{lua_setiuservalue} and @Lid{lua_getiuservalue}.) @@ -3420,12 +3432,12 @@ The function returns the address of the block of memory. @apii{1,2|0,v} Pops a key from the stack, -and pushes a key@En{}value pair from the table at the given index -(the @Q{next} pair after the given key). +and pushes a key@En{}value pair from the table at the given index, +the @Q{next} pair after the given key. If there are no more elements in the table, -then @Lid{lua_next} returns 0 (and pushes nothing). +then @Lid{lua_next} returns 0 and pushes nothing. -A typical traversal looks like this: +A typical table traversal looks like this: @verbatim{ /* table is in the stack at index 't' */ lua_pushnil(L); /* first key */ @@ -3440,7 +3452,7 @@ while (lua_next(L, t) != 0) { } While traversing a table, -do not call @Lid{lua_tolstring} directly on a key, +avoid calling @Lid{lua_tolstring} directly on a key, unless you know that the key is actually a string. Recall that @Lid{lua_tolstring} may change the value at the given index; @@ -3465,15 +3477,14 @@ but that can be changed to a single float or a long double. @APIEntry{int lua_numbertointeger (lua_Number n, lua_Integer *p);| -Converts a Lua float to a Lua integer. -This macro assumes that @id{n} has an integral value. +Tries to convert a Lua float to a Lua integer; +the float @id{n} must have an integral value. If that value is within the range of Lua integers, it is converted to an integer and assigned to @T{*p}. The macro results in a boolean indicating whether the conversion was successful. (Note that this range test can be tricky to do -correctly without this macro, -due to roundings.) +correctly without this macro, due to rounding.) This macro may evaluate its arguments more than once. @@ -3503,7 +3514,7 @@ Otherwise, @id{msgh} is the stack index of a @emph{message handler}. (This index cannot be a pseudo-index.) In case of runtime errors, -this function will be called with the error object +this handler will be called with the error object and its return value will be the object returned on the stack by @Lid{lua_pcall}. @@ -3580,11 +3591,12 @@ and return its results @seeC{lua_CFunction}. When a @N{C function} is created, it is possible to associate some values with it, -thus creating a @x{@N{C closure}} @see{c-closure}; -these values are then accessible to the function whenever it is called. -To associate values with a @N{C function}, -first these values must be pushed onto the stack -(when there are multiple values, the first value is pushed first). +the so called upvalues; +these upvalues are then accessible to the function whenever it is called. +This association is called a @x{@N{C closure}} @see{c-closure}. +To create a @N{C closure}, +first the initial values for its upvalues must be pushed onto the stack. +(When there are multiple upvalues, the first value is pushed first.) Then @Lid{lua_pushcclosure} is called to create and push the @N{C function} onto the stack, with the argument @id{n} telling how many values will be @@ -3604,6 +3616,7 @@ In that case, it never raises a memory error. @apii{0,1,-} Pushes a @N{C function} onto the stack. +This function is equivalent to @Lid{lua_pushcclosure} with no upvalues. } @@ -3626,7 +3639,7 @@ The conversion specifiers can only be @Char{%s} (inserts a zero-terminated string, with no size restrictions), @Char{%f} (inserts a @Lid{lua_Number}), @Char{%I} (inserts a @Lid{lua_Integer}), -@Char{%p} (inserts a pointer as a hexadecimal numeral), +@Char{%p} (inserts a pointer), @Char{%d} (inserts an @T{int}), @Char{%c} (inserts an @T{int} as a one-byte character), and @Char{%U} (inserts a @T{long int} as a @x{UTF-8} byte sequence). @@ -3670,6 +3683,7 @@ light userdata with the same @N{C address}. This macro is equivalent to @Lid{lua_pushstring}, but should be used only when @id{s} is a literal string. +(Lua may optimize this case.) } @@ -3678,7 +3692,7 @@ but should be used only when @id{s} is a literal string. Pushes the string pointed to by @id{s} with size @id{len} onto the stack. -Lua makes (or reuses) an internal copy of the given string, +Lua will make or reuse an internal copy of the given string, so the memory at @id{s} can be freed or reused immediately after the function returns. The string can contain any binary data, @@ -3707,7 +3721,7 @@ Pushes a float with value @id{n} onto the stack. Pushes the zero-terminated string pointed to by @id{s} onto the stack. -Lua makes (or reuses) an internal copy of the given string, +Lua will make or reuse an internal copy of the given string, so the memory at @id{s} can be freed or reused immediately after the function returns. @@ -3749,7 +3763,7 @@ instead of a variable number of arguments. Returns 1 if the two values in indices @id{index1} and @id{index2} are primitively equal -(that is, without calling the @idx{__eq} metamethod). +(that is, equal without calling the @idx{__eq} metamethod). Otherwise @N{returns 0}. Also @N{returns 0} if any of the indices are not valid. @@ -3796,8 +3810,8 @@ for strings, this is the string length; for tables, this is the result of the length operator (@Char{#}) with no metamethods; for userdata, this is the size of the block of memory allocated -for the userdata; -for other values, it @N{is 0}. +for the userdata. +For other values, this call @N{returns 0}. } @@ -3842,8 +3856,8 @@ typedef const char * (*lua_Reader) (lua_State *L, size_t *size);| The reader function used by @Lid{lua_load}. -Every time it needs another piece of the chunk, -@Lid{lua_load} calls the reader, +Every time @Lid{lua_load} needs another piece of the chunk, +it calls the reader, passing along its @id{data} parameter. The reader must return a pointer to a block of memory with a new piece of the chunk @@ -3912,9 +3926,9 @@ then you call @Lid{lua_resume}, with @id{nargs} being the number of arguments. This call returns when the coroutine suspends or finishes its execution. When it returns, -@id{nresults} is updated and +@id{*nresults} is updated and the top of the stack contains -the @id{nresults} values passed to @Lid{lua_yield} +the @id{*nresults} values passed to @Lid{lua_yield} or returned by the body function. @Lid{lua_resume} returns @Lid{LUA_YIELD} if the coroutine yields, @@ -3924,10 +3938,8 @@ or an error code in case of errors @seeC{lua_pcall}. In case of errors, the error object is on the top of the stack. -To resume a coroutine, -you remove all results from the last @Lid{lua_yield}, -put on its stack only the values to -be passed as results from @id{yield}, +To resume a coroutine, you clear its stack, +push only the values to be passed as results from @id{yield}, and then call @Lid{lua_resume}. The parameter @id{from} represents the coroutine that is resuming @id{L}. @@ -4066,12 +4078,12 @@ which creates a Lua state from scratch. Returns the status of the thread @id{L}. -The status can be 0 (@Lid{LUA_OK}) for a normal thread, +The status can be @Lid{LUA_OK} for a normal thread, an error code if the thread finished the execution of a @Lid{lua_resume} with an error, or @defid{LUA_YIELD} if the thread is suspended. -You can only call functions in threads with status @Lid{LUA_OK}. +You can call functions only in threads with status @Lid{LUA_OK}. You can resume threads with status @Lid{LUA_OK} (to start a new coroutine) or @Lid{LUA_YIELD} (to resume a coroutine). @@ -4127,7 +4139,7 @@ Like a to-be-closed variable in Lua, the value at that index in the stack will be closed when it goes out of scope. Here, in the context of a C function, -to go out of scope means that the running function returns (to Lua), +to go out of scope means that the running function returns to Lua, there is an error, or the index is removed from the stack through @Lid{lua_settop} or @Lid{lua_pop}. @@ -4250,7 +4262,7 @@ otherwise, the function returns @id{NULL}. If the value at the given index is a full userdata, returns its memory-block address. If the value is a light userdata, -returns its pointer. +returns its value (a pointer). Otherwise, returns @id{NULL}. } @@ -4259,7 +4271,7 @@ Otherwise, returns @id{NULL}. @apii{0,0,-} Returns the type of the value in the given valid index, -or @id{LUA_TNONE} for a non-valid (but acceptable) index. +or @id{LUA_TNONE} for a non-valid but acceptable index. The types returned by @Lid{lua_type} are coded by the following constants defined in @id{lua.h}: @defid{LUA_TNIL}, @@ -4335,8 +4347,8 @@ typedef int (*lua_Writer) (lua_State *L, void* ud);| The type of the writer function used by @Lid{lua_dump}. -Every time it produces another piece of chunk, -@Lid{lua_dump} calls the writer, +Every time @Lid{lua_dump} produces another piece of chunk, +it calls the writer, passing along the buffer to be written (@id{p}), its size (@id{sz}), and the @id{ud} parameter supplied to @Lid{lua_dump}. @@ -4414,7 +4426,7 @@ of the (Lua) function that triggered the hook. This function can raise an error if it is called from a thread with a pending C call with no continuation function -(what is called a @emphx{C-call boundary}, +(what is called a @emphx{C-call boundary}), or it is called from a thread that is not running inside a resume (typically the main thread). @@ -4439,6 +4451,7 @@ typedef struct lua_Debug { const char *namewhat; /* (n) */ const char *what; /* (S) */ const char *source; /* (S) */ + size_t srclen; /* (S) */ int currentline; /* (l) */ int linedefined; /* (S) */ int lastlinedefined; /* (S) */ @@ -4459,13 +4472,13 @@ information about a function or an activation record. @Lid{lua_getstack} fills only the private part of this structure, for later use. To fill the other fields of @Lid{lua_Debug} with useful information, -call @Lid{lua_getinfo}. +you must call @Lid{lua_getinfo}. The fields of @Lid{lua_Debug} have the following meaning: @description{ @item{@id{source}| -the name of the chunk that created the function. +the source of the chunk that created the function. If @T{source} starts with a @Char{@At}, it means that the function was defined in a file where the file name follows the @Char{@At}. @@ -4476,6 +4489,10 @@ the function was defined in a string where @T{source} is that string. } +@item{@id{srclen}| +The length of the string @id{source}. +} + @item{@id{short_src}| a @Q{printable} version of @T{source}, to be used in error messages. } @@ -4694,9 +4711,9 @@ of the function executing at a given level. @N{Level 0} is the current running function, whereas level @M{n+1} is the function that has called level @M{n} (except for tail calls, which do not count on the stack). -When there are no errors, @Lid{lua_getstack} returns 1; -when called with a level greater than the stack depth, -it returns 0. +When called with a level greater than the stack depth, +@Lid{lua_getstack} returns 0; +otherwise it returns 1. } @@ -4716,10 +4733,6 @@ as a name for all upvalues. upvalues are the external local variables that the function uses, and that are consequently included in its closure.) -Upvalues have no particular order, -as they are active through the whole function. -They are numbered in an arbitrary order. - } @APIEntry{typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar);| @@ -4780,24 +4793,22 @@ before the function gets its arguments. @item{The return hook| is called when the interpreter returns from a function. The hook is called just before Lua leaves the function. -There is no standard way to access the values -to be returned by the function. } @item{The line hook| is called when the interpreter is about to start the execution of a new line of code, or when it jumps back in the code (even to the same line). -(This event only happens while Lua is executing a Lua function.) +This event only happens while Lua is executing a Lua function. } @item{The count hook| is called after the interpreter executes every @T{count} instructions. -(This event only happens while Lua is executing a Lua function.) +This event only happens while Lua is executing a Lua function. } } -A hook is disabled by setting @id{mask} to zero. +Hooks are disabled by setting @id{mask} to zero. } @@ -4813,7 +4824,7 @@ Returns @id{NULL} (and pops nothing) when the index is greater than the number of active local variables. -Parameters @id{ar} and @id{n} are as in function @Lid{lua_getlocal}. +Parameters @id{ar} and @id{n} are as in the function @Lid{lua_getlocal}. } @@ -4828,7 +4839,8 @@ It also pops the value from the stack. Returns @id{NULL} (and pops nothing) when the index @id{n} is greater than the number of upvalues. -Parameters @id{funcindex} and @id{n} are as in function @Lid{lua_getupvalue}. +Parameters @id{funcindex} and @id{n} are as in +the function @Lid{lua_getupvalue}. } @@ -4844,7 +4856,8 @@ Lua closures that share an upvalue (that is, that access a same external local variable) will return identical ids for those upvalue indices. -Parameters @id{funcindex} and @id{n} are as in function @Lid{lua_getupvalue}, +Parameters @id{funcindex} and @id{n} are as in +the function @Lid{lua_getupvalue}, but @id{n} cannot be greater than the number of upvalues. } @@ -5086,7 +5099,7 @@ this function calls this field passing the object as its only argument. In this case this function returns true and pushes onto the stack the value returned by the call. If there is no metatable or no metamethod, -this function returns false (without pushing any value on the stack). +this function returns false without pushing any value on the stack. } @@ -5103,7 +5116,7 @@ of any type (including @nil) at position @id{arg}. Checks whether the function argument @id{arg} is an integer (or can be converted to an integer) -and returns this integer cast to a @Lid{lua_Integer}. +and returns this integer. } @@ -5112,7 +5125,7 @@ and returns this integer cast to a @Lid{lua_Integer}. Checks whether the function argument @id{arg} is a string and returns this string; -if @id{l} is not @id{NULL} fills @T{*l} +if @id{l} is not @id{NULL} fills its referent with the string's length. This function uses @Lid{lua_tolstring} to get its result, @@ -5124,7 +5137,7 @@ so all conversions and caveats of that function apply here. @apii{0,0,v} Checks whether the function argument @id{arg} is a number -and returns this number. +and returns this number converted to a @id{lua_Number}. } @@ -5300,8 +5313,8 @@ const char *luaL_gsub (lua_State *L, const char *r);| @apii{0,1,m} -Creates a copy of string @id{s} by replacing -any occurrence of the string @id{p} +Creates a copy of string @id{s}, +replacing any occurrence of the string @id{p} with the string @id{r}. Pushes the resulting string on the stack and returns it. @@ -5314,7 +5327,7 @@ Returns the @Q{length} of the value at the given index as a number; it is equivalent to the @Char{#} operator in Lua @see{len-op}. Raises an error if the result of the operation is not an integer. -(This case only can happen through metamethods.) +(This case can only happen through metamethods.) } @@ -5345,7 +5358,7 @@ buffer pointed to by @id{buff} with size @id{sz}. This function returns the same results as @Lid{lua_load}. @id{name} is the chunk name, used for debug information and error messages. -The string @id{mode} works as in function @Lid{lua_load}. +The string @id{mode} works as in the function @Lid{lua_load}. } @@ -5368,7 +5381,7 @@ If @id{filename} is @id{NULL}, then it loads from the standard input. The first line in the file is ignored if it starts with a @T{#}. -The string @id{mode} works as in function @Lid{lua_load}. +The string @id{mode} works as in the function @Lid{lua_load}. This function returns the same results as @Lid{lua_load}, but it has an extra error code @defid{LUA_ERRFILE} @@ -5399,7 +5412,7 @@ it does not run it. @apii{0,1,m} Creates a new table and registers there -the functions in list @id{l}. +the functions in the list @id{l}. It is implemented as the following macro: @verbatim{ @@ -5437,7 +5450,8 @@ adds to the registry the pair @T{[tname] = new table}, and returns 1. (The entry @idx{__name} is used by some error-reporting functions.) -In both cases pushes onto the stack the final value associated +In both cases, +the function pushes onto the stack the final value associated with @id{tname} in the registry. } @@ -5447,10 +5461,9 @@ with @id{tname} in the registry. Creates a new Lua state. It calls @Lid{lua_newstate} with an -allocator based on the @N{standard C} @id{realloc} function -and then sets a panic function @see{C-error} that prints -an error message to the standard error output in case of fatal -errors. +allocator based on the @N{standard C} allocation functions +and then sets a warning function and a panic function @see{C-error} +that print messages to the standard error output. Returns the new state, or @id{NULL} if there is a @x{memory allocation error}. @@ -5488,7 +5501,7 @@ lua_Integer luaL_optinteger (lua_State *L, @apii{0,0,v} If the function argument @id{arg} is an integer -(or convertible to an integer), +(or it is convertible to an integer), returns this integer. If this argument is absent or is @nil, returns @id{d}. @@ -5510,7 +5523,7 @@ returns @id{d}. Otherwise, raises an error. If @id{l} is not @id{NULL}, -fills the position @T{*l} with the result's length. +fills its referent with the result's length. If the result is @id{NULL} (only possible when returning @id{d} and @T{d == NULL}), its length is considered zero. @@ -5524,7 +5537,7 @@ so all conversions and caveats of that function apply here. @apii{0,0,v} If the function argument @id{arg} is a number, -returns this number. +returns this number as a @id{lua_Number}. If this argument is absent or is @nil, returns @id{d}. Otherwise, raises an error. @@ -5588,11 +5601,11 @@ in the table at index @id{t}, for the object on the top of the stack (and pops the object). A reference is a unique integer key. -As long as you do not manually add integer keys into table @id{t}, +As long as you do not manually add integer keys into the table @id{t}, @Lid{luaL_ref} ensures the uniqueness of the key it returns. -You can retrieve an object referred by reference @id{r} +You can retrieve an object referred by the reference @id{r} by calling @T{lua_rawgeti(L, t, r)}. -Function @Lid{luaL_unref} frees a reference and its associated object. +The function @Lid{luaL_unref} frees a reference. If the object on the top of the stack is @nil, @Lid{luaL_ref} returns the constant @defid{LUA_REFNIL}. @@ -5623,12 +5636,12 @@ void luaL_requiref (lua_State *L, const char *modname, @apii{0,1,e} If @T{package.loaded[modname]} is not true, -calls function @id{openf} with string @id{modname} as an argument +calls the function @id{openf} with the string @id{modname} as an argument and sets the call result to @T{package.loaded[modname]}, as if that function has been called through @Lid{require}. If @id{glb} is true, -also stores the module into global @id{modname}. +also stores the module into the global @id{modname}. Leaves a copy of the module on the stack. @@ -5666,8 +5679,8 @@ typedef struct luaL_Stream { } luaL_Stream; | -The standard representation for @x{file handles}, -which is used by the standard I/O library. +The standard representation for @x{file handles} +used by the standard I/O library. A file handle is implemented as a full userdata, with a metatable called @id{LUA_FILEHANDLE} @@ -5677,14 +5690,14 @@ The metatable is created by the I/O library This userdata must start with the structure @id{luaL_Stream}; it can contain other data after this initial structure. -Field @id{f} points to the corresponding C stream +The field @id{f} points to the corresponding C stream (or it can be @id{NULL} to indicate an incompletely created handle). -Field @id{closef} points to a Lua function +The field @id{closef} points to a Lua function that will be called to close the stream when the handle is closed or collected; this function receives the file handle as its sole argument and -must return either @true (in case of success) -or @nil plus an error message (in case of error). +must return either @true, in case of success, +or @nil plus an error message, in case of error. Once Lua calls this field, it changes the field value to @id{NULL} to signal that the handle is closed. @@ -5723,7 +5736,7 @@ void luaL_traceback (lua_State *L, lua_State *L1, const char *msg, @apii{0,1,m} Creates and pushes a traceback of the stack @id{L1}. -If @id{msg} is not @id{NULL} it is appended +If @id{msg} is not @id{NULL}, it is appended at the beginning of the traceback. The @id{level} parameter tells at which level to start the traceback. @@ -5735,7 +5748,7 @@ to start the traceback. const char *tname);| @apii{0,0,v} -Raises a type error for argument @id{arg} +Raises a type error for the argument @id{arg} of the @N{C function} that called it, using a standard message; @id{tname} is a @Q{name} for the expected type. @@ -5753,7 +5766,7 @@ Returns the name of the type of the value at the given index. @APIEntry{void luaL_unref (lua_State *L, int t, int ref);| @apii{0,0,-} -Releases reference @id{ref} from the table at index @id{t} +Releases the reference @id{ref} from the table at index @id{t} @seeC{luaL_ref}. The entry is removed from the table, so that the referred object can be collected. @@ -5787,15 +5800,15 @@ This function is used to build a prefix for error messages. @C{-------------------------------------------------------------------------} -@sect1{libraries| @title{Standard Libraries} +@sect1{libraries| @title{The Standard Libraries} The standard Lua libraries provide useful functions -that are implemented directly through the @N{C API}. +that are implemented @N{in C} through the @N{C API}. Some of these functions provide essential services to the language (e.g., @Lid{type} and @Lid{getmetatable}); -others provide access to @Q{outside} services (e.g., I/O); +others provide access to outside services (e.g., I/O); and others could be implemented in Lua itself, -but are quite useful or have critical performance requirements that +but that for different reasons deserve an implementation in C (e.g., @Lid{table.sort}). All libraries are implemented through the official @N{C API} @@ -5844,7 +5857,7 @@ the host program can open them individually by using @defid{luaopen_package} (for the package library), @defid{luaopen_coroutine} (for the coroutine library), @defid{luaopen_string} (for the string library), -@defid{luaopen_utf8} (for the UTF8 library), +@defid{luaopen_utf8} (for the UTF-8 library), @defid{luaopen_table} (for the table library), @defid{luaopen_math} (for the mathematical library), @defid{luaopen_io} (for the I/O library), @@ -5896,8 +5909,7 @@ restarts automatic execution of the garbage collector. returns the total memory in use by Lua in Kbytes. The value has a fractional part, so that it multiplied by 1024 -gives the exact number of bytes in use by Lua -(except for overflows). +gives the exact number of bytes in use by Lua. } @item{@St{step}| @@ -5938,6 +5950,8 @@ returns a boolean that tells whether the collector is running } } +See @See{GC} for more details about garbage collection +and some of these options. } @@ -5947,14 +5961,15 @@ When called without arguments, @id{dofile} executes the contents of the standard input (@id{stdin}). Returns all values returned by the chunk. In case of errors, @id{dofile} propagates the error -to its caller (that is, @id{dofile} does not run in protected mode). +to its caller. +(That is, @id{dofile} does not run in protected mode.) } @LibEntry{error (message [, level])| Terminates the last protected function called and returns @id{message} as the error object. -Function @id{error} never returns. +This function never returns. Usually, @id{error} adds some information about the error position at the beginning of the message, if the message is a string. @@ -6066,7 +6081,7 @@ if no file name is given. Allows a program to traverse all fields of a table. Its first argument is a table and its second argument is an index in this table. -@id{next} returns the next index of the table +A call to @id{next} returns the next index of the table and its associated value. When called with @nil as its second argument, @id{next} returns an initial index @@ -6112,7 +6127,7 @@ the table during its traversal. @LibEntry{pcall (f [, arg1, @Cdots])| -Calls function @id{f} with +Calls the function @id{f} with the given arguments in @def{protected mode}. This means that any error @N{inside @T{f}} is not propagated; instead, @id{pcall} catches the error @@ -6121,7 +6136,7 @@ Its first result is the status code (a boolean), which is true if the call succeeds without errors. In such case, @id{pcall} also returns all results from the call, after this first result. -In case of any error, @id{pcall} returns @false plus the error message. +In case of any error, @id{pcall} returns @false plus the error object. } @@ -6184,8 +6199,6 @@ and @id{select} returns the total number of extra arguments it received. @LibEntry{setmetatable (table, metatable)| Sets the metatable for the given table. -(To change the metatable of other types from Lua code, -you must use the @link{debuglib|debug library}.) If @id{metatable} is @nil, removes the metatable of the given table. If the original metatable has a @idx{__metatable} field, @@ -6193,6 +6206,9 @@ raises an error. This function returns @id{table}. +To change the metatable of other types from Lua code, +you must use the @link{debuglib|debug library}. + } @LibEntry{tonumber (e [, base])| @@ -6206,7 +6222,7 @@ otherwise, it returns @nil. The conversion of strings can result in integers or floats, according to the lexical conventions of Lua @see{lexical}. -(The string may have leading and trailing spaces and a sign.) +The string may have leading and trailing spaces and a sign. When called with @id{base}, then @id{e} must be a string to be interpreted as @@ -6298,7 +6314,7 @@ it is not inside a non-yieldable @N{C function}. } -@LibEntry{coroutine.kill(co)| +@LibEntry{coroutine.kill (co)| Kills coroutine @id{co}, closing all its pending to-be-closed variables @@ -6339,7 +6355,7 @@ true when the running coroutine is the main one. @LibEntry{coroutine.status (co)| -Returns the status of coroutine @id{co}, as a string: +Returns the status of the coroutine @id{co}, as a string: @T{"running"}, if the coroutine is running (that is, it called @id{status}); @T{"suspended"}, if the coroutine is suspended in a call to @id{yield}, @@ -6353,7 +6369,7 @@ or if it has stopped with an error. @LibEntry{coroutine.wrap (f)| -Creates a new coroutine, with body @id{f}. +Creates a new coroutine, with body @id{f}; @id{f} must be a function. Returns a function that resumes the coroutine each time it is called. Any arguments passed to the function behave as the @@ -6379,7 +6395,7 @@ The package library provides basic facilities for loading modules in Lua. It exports one function directly in the global environment: @Lid{require}. -Everything else is exported in a table @defid{package}. +Everything else is exported in the table @defid{package}. @LibEntry{require (modname)| @@ -6729,7 +6745,8 @@ after the two indices. @LibEntry{string.format (formatstring, @Cdots)| Returns a formatted version of its variable number of arguments -following the description given in its first argument (which must be a string). +following the description given in its first argument, +which must be a string. The format string follows the same rules as the @ANSI{sprintf}. The only differences are that the conversion specifiers and modifiers @T{*}, @id{h}, @id{L}, @id{l}, and @id{n} are not supported @@ -6830,9 +6847,9 @@ If @id{repl} is a string, then its value is used for replacement. The @N{character @T{%}} works as an escape character: any sequence in @id{repl} of the form @T{%@rep{d}}, with @rep{d} between 1 and 9, -stands for the value of the @rep{d}-th captured substring. -The sequence @T{%0} stands for the whole match. -The sequence @T{%%} stands for a @N{single @T{%}}. +stands for the value of the @rep{d}-th captured substring; +the sequence @T{%0} stands for the whole match; +the sequence @T{%%} stands for a @N{single @T{%}}. If @id{repl} is a table, then the table is queried for every match, using the first capture as the key. @@ -6899,7 +6916,7 @@ The definition of what an uppercase letter is depends on the current locale. @LibEntry{string.match (s, pattern [, init])| Looks for the first @emph{match} of -@id{pattern} @see{pm} in the string @id{s}. +the @id{pattern} @see{pm} in the string @id{s}. If it finds one, then @id{match} returns the captures from the pattern; otherwise it returns @nil. @@ -6914,7 +6931,7 @@ its default value @N{is 1} and can be negative. @LibEntry{string.pack (fmt, v1, v2, @Cdots)| Returns a binary string containing the values @id{v1}, @id{v2}, etc. -packed (that is, serialized in binary form) +serialized in binary form (packed) according to the format string @id{fmt} @see{pack}. } @@ -7042,8 +7059,7 @@ represents the character @rep{x}. This is the standard way to escape the magic characters. Any non-alphanumeric character (including all punctuation characters, even the non-magical) -can be preceded by a @Char{%} -when used to represent itself in a pattern. +can be preceded by a @Char{%} to represent itself in a pattern. } @item{@T{[@rep{set}]}| @@ -7099,19 +7115,19 @@ which matches any single character in the class; @item{ a single character class followed by @Char{*}, -which matches zero or more repetitions of characters in the class. +which matches sequences of zero or more characters in the class. These repetition items will always match the longest possible sequence; } @item{ a single character class followed by @Char{+}, -which matches one or more repetitions of characters in the class. +which matches sequences of one or more characters in the class. These repetition items will always match the longest possible sequence; } @item{ a single character class followed by @Char{-}, -which also matches zero or more repetitions of characters in the class. +which also matches sequences of zero or more characters in the class. Unlike @Char{*}, these repetition items will always match the shortest possible sequence; } @@ -7172,7 +7188,7 @@ that match captures are stored (@emph{captured}) for future use. Captures are numbered according to their left parentheses. For instance, in the pattern @T{"(a*(.)%w(%s*))"}, the part of the string matching @T{"a*(.)%w(%s*)"} is -stored as the first capture (and therefore has @N{number 1}); +stored as the first capture, and therefore has @N{number 1}; the character matching @St{.} is captured with @N{number 2}, and the part matching @St{%s*} has @N{number 3}. @@ -7188,7 +7204,7 @@ The function @Lid{string.gsub} and the iterator @Lid{string.gmatch} match multiple occurrences of the given pattern in the subject. For these functions, a new match is considered valid only -if it ends at least one byte after the previous match. +if it ends at least one byte after the end of the previous match. In other words, the pattern machine never accepts the empty string as a match immediately after another match. As an example, @@ -7253,14 +7269,16 @@ according to option @id{op} (A @St{[@rep{n}]} means an optional integral numeral.) Except for padding, spaces, and configurations (options @St{xX <=>!}), -each option corresponds to an argument (in @Lid{string.pack}) -or a result (in @Lid{string.unpack}). +each option corresponds to an argument in @Lid{string.pack} +or a result in @Lid{string.unpack}. For options @St{!@rep{n}}, @St{s@rep{n}}, @St{i@rep{n}}, and @St{I@rep{n}}, @id{n} can be any integer between 1 and 16. All integral options check overflows; @Lid{string.pack} checks whether the given value fits in the given size; @Lid{string.unpack} checks whether the read value fits in a Lua integer. +For the unsigned options, +Lua integers are treated as unsigned values too. Any format string starts as if prefixed by @St{!1=}, that is, @@ -7283,7 +7301,7 @@ option @St{s} follows the alignment of its starting integer. All padding is filled with zeros by @Lid{string.pack} -(and ignored by @Lid{string.unpack}). +and ignored by @Lid{string.unpack}. } @@ -7344,7 +7362,7 @@ Returns values so that the construction @verbatim{ for p, c in utf8.codes(s) do @rep{body} end } -will iterate over all characters in string @id{s}, +will iterate over all UTF-8 characters in string @id{s}, with @id{p} being the position (in bytes) and @id{c} the code point of each character. It raises an error if it meets any invalid byte sequence. @@ -7353,7 +7371,7 @@ It raises an error if it meets any invalid byte sequence. @LibEntry{utf8.codepoint (s [, i [, j [, lax]]])| -Returns the codepoints (as integers) from all characters in @id{s} +Returns the code points (as integers) from all characters in @id{s} that start between byte position @id{i} and @id{j} (both included). The default for @id{i} is 1 and for @id{j} is @id{i}. It raises an error if it meets any invalid byte sequence. @@ -7423,13 +7441,13 @@ shifting up the elements @T{list[pos], list[pos+1], @Cdots, list[#list]}. The default value for @id{pos} is @T{#list+1}, so that a call @T{table.insert(t,x)} inserts @id{x} at the end -of list @id{t}. +of the list @id{t}. } @LibEntry{table.move (a1, f, e, t [,a2])| -Moves elements from table @id{a1} to table @id{a2}, +Moves elements from the table @id{a1} to the table @id{a2}, performing the equivalent to the following multiple assignment: @T{a2[t],@Cdots = a1[f],@Cdots,a1[e]}. @@ -7463,13 +7481,13 @@ or @T{#list + 1}. The default value for @id{pos} is @T{#list}, so that a call @T{table.remove(l)} removes the last element -of list @id{l}. +of the list @id{l}. } @LibEntry{table.sort (list [, comp])| -Sorts list elements in a given order, @emph{in-place}, +Sorts the list elements in a given order, @emph{in-place}, from @T{list[1]} to @T{list[#list]}. If @id{comp} is given, then it must be a function that receives two list elements @@ -7511,8 +7529,8 @@ It provides all its functions and constants inside the table @defid{math}. Functions with the annotation @St{integer/float} give integer results for integer arguments and float results for float (or mixed) arguments. -Rounding functions -(@Lid{math.ceil}, @Lid{math.floor}, and @Lid{math.modf}) +the rounding functions +@Lid{math.ceil}, @Lid{math.floor}, and @Lid{math.modf} return an integer when the result fits in the range of an integer, or a float otherwise. @@ -7540,7 +7558,7 @@ Returns the arc sine of @id{x} (in radians). Returns the arc tangent of @T{y/x} (in radians), but uses the signs of both arguments to find the quadrant of the result. -(It also handles correctly the case of @id{x} being zero.) +It also handles correctly the case of @id{x} being zero. The default value for @id{x} is 1, so that the call @T{math.atan(y)} @@ -7604,7 +7622,7 @@ The default for @id{base} is @M{e} @LibEntry{math.max (x, @Cdots)| Returns the argument with the maximum value, -according to the Lua operator @T{<}. (integer/float) +according to the Lua operator @T{<}. } @@ -7616,7 +7634,7 @@ An integer with the maximum value for an integer. @LibEntry{math.min (x, @Cdots)| Returns the argument with the minimum value, -according to the Lua operator @T{<}. (integer/float) +according to the Lua operator @T{<}. } @@ -7674,7 +7692,7 @@ some number of previous results.) When called with at least one argument, the integer parameters @id{x} and @id{y} are -concatenated into a 128-bit @emphx{seed} that +joined into a 128-bit @emphx{seed} that is used to reinitialize the pseudo-random generator; equal seeds produce equal sequences of numbers. The default for @id{y} is zero. @@ -7741,7 +7759,7 @@ The I/O library provides two different styles for file manipulation. The first one uses implicit file handles; that is, there are operations to set a default input file and a default output file, -and all input/output operations are over these default files. +and all input/output operations are done over these default files. The second style uses explicit file handles. When using implicit file handles, @@ -7750,18 +7768,19 @@ When using explicit file handles, the operation @Lid{io.open} returns a file handle and then all operations are supplied as methods of the file handle. +The metatable for file handles provides metamethods +for @idx{__gc} and @idx{__close} that try +to close the file when called. + The table @id{io} also provides three predefined file handles with their usual meanings from C: @defid{io.stdin}, @defid{io.stdout}, and @defid{io.stderr}. The I/O library never closes these files. -The metatable for file handles provides metamethods -for @idx{__gc} and @idx{__close} that try -to close the file when called. Unless otherwise stated, -all I/O functions return @nil on failure -(plus an error message as a second result and -a system-dependent error code as a third result) +all I/O functions return @nil on failure, +plus an error message as a second result and +a system-dependent error code as a third result, and some value different from @nil on success. On non-POSIX systems, the computation of the error message and error code @@ -7854,7 +7873,7 @@ Similar to @Lid{io.input}, but operates over the default output file. This function is system dependent and is not available on all platforms. -Starts program @id{prog} in a separated process and returns +Starts the program @id{prog} in a separated process and returns a file handle that you can use to read data from this program (if @id{mode} is @T{"r"}, the default) or to write data to this program @@ -8097,10 +8116,9 @@ If @id{format} is not @St{*t}, then @id{date} returns the date as a string, formatted according to the same rules as the @ANSI{strftime}. -When called without arguments, -@id{date} returns a reasonable date and time representation that depends on -the host system and on the current locale. -(More specifically, @T{os.date()} is equivalent to @T{os.date("%c")}.) +If @id{format} is absent, it defaults to @St{%c}, +which gives a reasonable date and time representation +using the current locale. On non-POSIX systems, this function may be not @x{thread safe} @@ -8314,8 +8332,8 @@ within any function and so have no direct access to local variables. Returns the current hook settings of the thread, as three values: the current hook function, the current hook mask, -and the current hook count -(as set by the @Lid{debug.sethook} function). +and the current hook count, +as set by the @Lid{debug.sethook} function. } @@ -8359,7 +8377,7 @@ about the @Lid{print} function. This function returns the name and the value of the local variable with index @id{local} of the function at level @id{f} of the stack. This function accesses not only explicit local variables, -but also parameters, temporaries, etc. +but also parameters and temporary values. The first parameter or local variable has @N{index 1}, and so on, following the order that they are declared in the code, @@ -8416,7 +8434,7 @@ to the userdata @id{u} plus a boolean, @LibEntry{debug.sethook ([thread,] hook, mask [, count])| -Sets the given function as a hook. +Sets the given function as the debug hook. The string @id{mask} and the number @id{count} describe when the hook will be called. The string mask may have any combination of the following characters, @@ -8435,16 +8453,15 @@ When called without arguments, When the hook is called, its first parameter is a string describing the event that has triggered its call: -@T{"call"} (or @T{"tail call"}), -@T{"return"}, +@T{"call"}, @T{"tail call"}, @T{"return"}, @T{"line"}, and @T{"count"}. For line events, the hook also gets the new line number as its second parameter. Inside a hook, you can call @id{getinfo} with @N{level 2} to get more information about -the running function -(@N{level 0} is the @id{getinfo} function, -and @N{level 1} is the hook function). +the running function. +(@N{Level 0} is the @id{getinfo} function, +and @N{level 1} is the hook function.) } @@ -8542,7 +8559,7 @@ An interpreter for Lua as a standalone language, called simply @id{lua}, is provided with the standard distribution. The @x{standalone interpreter} includes -all standard libraries, including the debug library. +all standard libraries. Its usage is: @verbatim{ lua [options] [script [args]] @@ -8564,7 +8581,7 @@ When called without arguments, when the standard input (@id{stdin}) is a terminal, and as @T{lua -} otherwise. -When called without option @T{-E}, +When called without the option @T{-E}, the interpreter checks for an environment variable @defid{LUA_INIT_5_4} (or @defid{LUA_INIT} if the versioned name is not defined) before running any argument. @@ -8572,7 +8589,7 @@ If the variable content has the format @T{@At@rep{filename}}, then @id{lua} executes the file. Otherwise, @id{lua} executes the string itself. -When called with option @T{-E}, +When called with the option @T{-E}, besides ignoring @id{LUA_INIT}, Lua also ignores the values of @id{LUA_PATH} and @id{LUA_CPATH}, @@ -8619,8 +8636,8 @@ will print @St{-e}. If there is a script, the script is called with arguments @T{arg[1]}, @Cdots, @T{arg[#arg]}. -(Like all chunks in Lua, -the script is compiled as a vararg function.) +Like all chunks in Lua, +the script is compiled as a vararg function. In interactive mode, Lua repeatedly prompts and waits for a line. @@ -8645,6 +8662,7 @@ has a metamethod @idx{__tostring}, the interpreter calls this metamethod to produce the final message. Otherwise, the interpreter converts the error object to a string and adds a stack traceback to it. +Warnings are simply printed in the standard error output. When finishing normally, the interpreter closes its main Lua state @@ -8654,22 +8672,21 @@ calling @Lid{os.exit} to terminate. To allow the use of Lua as a script interpreter in Unix systems, -the standalone interpreter skips -the first line of a chunk if it starts with @T{#}. +Lua skips the first line of a file chunk if it starts with @T{#}. Therefore, Lua scripts can be made into executable programs by using @T{chmod +x} and @N{the @T{#!}} form, as in @verbatim{ #!/usr/local/bin/lua } -(Of course, +Of course, the location of the Lua interpreter may be different in your machine. If @id{lua} is in your @id{PATH}, then @verbatim{ #!/usr/bin/env lua } -is a more portable solution.) +is a more portable solution. } @@ -8688,7 +8705,7 @@ do not imply source-code changes in a program, such as the numeric values for constants or the implementation of functions as macros. Therefore, -you should not assume that binaries are compatible between +you should never assume that binaries are compatible between different Lua versions. Always recompile clients of the Lua API when using a new version. @@ -8700,7 +8717,7 @@ precompiled chunks are not compatible between different Lua versions. The standard paths in the official distribution may change between versions. -@sect2{@title{Changes in the Language} +@sect2{@title{Incompatibilities in the Language} @itemize{ @item{ @@ -8752,7 +8769,7 @@ like any other error when calling a finalizer.) } -@sect2{@title{Changes in the Libraries} +@sect2{@title{Incompatibilities in the Libraries} @itemize{ @item{ @@ -8761,13 +8778,6 @@ now starts with a somewhat random seed. Moreover, it uses a different algorithm. } -@item{ -The function @Lid{io.lines} now returns three extra values, -besides the iterator function. -You can enclose the call in parentheses if you need to -discard these extra results. -} - @item{ By default, the decoding functions in the @Lid{utf8} library do not accept surrogates as valid code points. @@ -8778,7 +8788,7 @@ An extra parameter in these functions makes them more permissive. } -@sect2{@title{Changes in the API} +@sect2{@title{Incompatibilities in the API} @itemize{ @@ -8792,8 +8802,8 @@ which have an extra argument. For compatibility, the old names still work as macros assuming one single user value. -Note, however, that the call @T{lua_newuserdatauv(L,size,0)} -produces a smaller userdata. +Note, however, that userdata with zero user values +are more efficient memory-wise. } @item{ @@ -8807,10 +8817,10 @@ those values were the entire stack.) @item{ The function @Lid{lua_version} returns the version number, instead of an address of the version number. -(The Lua core should work correctly with libraries using their +The Lua core should work correctly with libraries using their own static copies of the same core, so there is no need to check whether they are using the same -address space.) +address space. } @item{ From 8ba4523cccf59093543cec988b07957193d55692 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 10 Apr 2019 12:58:14 -0300 Subject: [PATCH 0400/1145] 'print' does not call 'tostring' to format its arguments --- lbaselib.c | 16 +++++----------- manual/manual.of | 13 +++++++++++-- testes/calls.lua | 15 --------------- 3 files changed, 16 insertions(+), 28 deletions(-) diff --git a/lbaselib.c b/lbaselib.c index d4b619a530..83f61e6d11 100644 --- a/lbaselib.c +++ b/lbaselib.c @@ -24,18 +24,12 @@ static int luaB_print (lua_State *L) { int n = lua_gettop(L); /* number of arguments */ int i; - lua_getglobal(L, "tostring"); - for (i=1; i<=n; i++) { - const char *s; + for (i = 1; i <= n; i++) { /* for each argument */ size_t l; - lua_pushvalue(L, -1); /* function to be called */ - lua_pushvalue(L, i); /* value to print */ - lua_call(L, 1, 1); - s = lua_tolstring(L, -1, &l); /* get result */ - if (s == NULL) - return luaL_error(L, "'tostring' must return a string to 'print'"); - if (i>1) lua_writestring("\t", 1); - lua_writestring(s, l); + const char *s = luaL_tolstring(L, i, &l); /* convert it to string */ + if (i > 1) /* not the first element? */ + lua_writestring("\t", 1); /* add a tab before it */ + lua_writestring(s, l); /* print it */ lua_pop(L, 1); /* pop result */ } lua_writeline(); diff --git a/manual/manual.of b/manual/manual.of index 9f1ef631c5..6da2e49432 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -6143,8 +6143,10 @@ In case of any error, @id{pcall} returns @false plus the error object. @LibEntry{print (@Cdots)| Receives any number of arguments and prints their values to @id{stdout}, -using the @Lid{tostring} function to convert each argument to a string. -@id{print} is not intended for formatted output, +converting each argument to a string +following the same rules of @Lid{tostring}. + +The function @id{print} is not intended for formatted output, but only as a quick way to show a value, for instance for debugging. For complete control over the output, @@ -8772,6 +8774,13 @@ like any other error when calling a finalizer.) @sect2{@title{Incompatibilities in the Libraries} @itemize{ +@item{ +The function @Lid{print} does not call @Lid{tostring} +to format its arguments; +instead, it has this functionality hardwired. +You should use @id{__tostring} to modify how values are printed. +} + @item{ The pseudo-random number generator used by the function @Lid{math.random} now starts with a somewhat random seed. diff --git a/testes/calls.lua b/testes/calls.lua index 941493b131..56a12ae670 100644 --- a/testes/calls.lua +++ b/testes/calls.lua @@ -21,21 +21,6 @@ assert(type(f) == 'function') assert(not pcall(type)) -do -- test error in 'print' too... - local tostring = _ENV.tostring - - _ENV.tostring = nil - local st, msg = pcall(print, 1) - assert(st == false and string.find(msg, "attempt to call a nil value")) - - _ENV.tostring = function () return {} end - local st, msg = pcall(print, 1) - assert(st == false and string.find(msg, "must return a string")) - - _ENV.tostring = tostring -end - - -- testing local-function recursion fact = false do From a93e0144479f1eb0ac19b8c31862f4cbc2fbe1c4 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 10 Apr 2019 13:23:14 -0300 Subject: [PATCH 0401/1145] Added an optional parameter to 'coroutine.isyieldable' --- lcorolib.c | 3 ++- manual/manual.of | 7 ++++--- testes/coroutine.lua | 7 +++++-- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/lcorolib.c b/lcorolib.c index cdb5fedc93..f7c9e165f5 100644 --- a/lcorolib.c +++ b/lcorolib.c @@ -146,7 +146,8 @@ static int luaB_costatus (lua_State *L) { static int luaB_yieldable (lua_State *L) { - lua_pushboolean(L, lua_isyieldable(L)); + lua_State *co = lua_isnone(L, 1) ? L : getco(L); + lua_pushboolean(L, lua_isyieldable(co)); return 1; } diff --git a/manual/manual.of b/manual/manual.of index 6da2e49432..fea6922e6e 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -6307,11 +6307,12 @@ an object with type @T{"thread"}. } -@LibEntry{coroutine.isyieldable ()| +@LibEntry{coroutine.isyieldable ([co])| -Returns true when the running coroutine can yield. +Returns true when the coroutine @id{co} can yield. +The default for @id{co} is the running coroutine. -A running coroutine is yieldable if it is not the main thread and +A coroutine is yieldable if it is not the main thread and it is not inside a non-yieldable @N{C function}. } diff --git a/testes/coroutine.lua b/testes/coroutine.lua index a4321bedaf..35ff27fb7f 100644 --- a/testes/coroutine.lua +++ b/testes/coroutine.lua @@ -10,7 +10,7 @@ local f local main, ismain = coroutine.running() assert(type(main) == "thread" and ismain) assert(not coroutine.resume(main)) -assert(not coroutine.isyieldable()) +assert(not coroutine.isyieldable(main) and not coroutine.isyieldable()) assert(not pcall(coroutine.yield)) @@ -38,7 +38,7 @@ function foo (a, ...) assert(coroutine.resume(f) == false) assert(coroutine.status(f) == "running") local arg = {...} - assert(coroutine.isyieldable()) + assert(coroutine.isyieldable(x)) for i=1,#arg do _G.x = {coroutine.yield(table.unpack(arg[i]))} end @@ -46,14 +46,17 @@ function foo (a, ...) end f = coroutine.create(foo) +assert(coroutine.isyieldable(f)) assert(type(f) == "thread" and coroutine.status(f) == "suspended") assert(string.find(tostring(f), "thread")) local s,a,b,c,d s,a,b,c,d = coroutine.resume(f, {1,2,3}, {}, {1}, {'a', 'b', 'c'}) +assert(coroutine.isyieldable(f)) assert(s and a == nil and coroutine.status(f) == "suspended") s,a,b,c,d = coroutine.resume(f) eqtab(_G.x, {}) assert(s and a == 1 and b == nil) +assert(coroutine.isyieldable(f)) s,a,b,c,d = coroutine.resume(f, 1, 2, 3) eqtab(_G.x, {1, 2, 3}) assert(s and a == 'a' and b == 'b' and c == 'c' and d == nil) From b0810c51c3f075cc8a309bfb3c1714ac42b0f020 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 11 Apr 2019 11:29:16 -0300 Subject: [PATCH 0402/1145] Small optimizations in 'string.gsub' Avoid creating extra strings when possible: - avoid creating new resulting string when subject was not modified (instead, return the subject itself); - avoid creating strings representing the captured substrings when handling replacements like '%1' (instead, add the substring directly to the buffer). --- lstrlib.c | 130 ++++++++++++++++++++++++++++++++------------------ testes/gc.lua | 2 +- testes/pm.lua | 30 ++++++++++++ 3 files changed, 115 insertions(+), 47 deletions(-) diff --git a/lstrlib.c b/lstrlib.c index 6230cd0c00..53ed80a3bf 100644 --- a/lstrlib.c +++ b/lstrlib.c @@ -660,25 +660,46 @@ static const char *lmemfind (const char *s1, size_t l1, } -static void push_onecapture (MatchState *ms, int i, const char *s, - const char *e) { +/* +** get information about the i-th capture. If there are no captures +** and 'i==0', return information about the whole match, which +** is the range 's'..'e'. If the capture is a string, return +** its length and put its address in '*cap'. If it is an integer +** (a position), push it on the stack and return CAP_POSITION. +*/ +static size_t get_onecapture (MatchState *ms, int i, const char *s, + const char *e, const char **cap) { if (i >= ms->level) { - if (i == 0) /* ms->level == 0, too */ - lua_pushlstring(ms->L, s, e - s); /* add whole match */ - else + if (i != 0) luaL_error(ms->L, "invalid capture index %%%d", i + 1); + *cap = s; + return e - s; } else { - ptrdiff_t l = ms->capture[i].len; - if (l == CAP_UNFINISHED) luaL_error(ms->L, "unfinished capture"); - if (l == CAP_POSITION) + ptrdiff_t capl = ms->capture[i].len; + *cap = ms->capture[i].init; + if (capl == CAP_UNFINISHED) + luaL_error(ms->L, "unfinished capture"); + else if (capl == CAP_POSITION) lua_pushinteger(ms->L, (ms->capture[i].init - ms->src_init) + 1); - else - lua_pushlstring(ms->L, ms->capture[i].init, l); + return capl; } } +/* +** Push the i-th capture on the stack. +*/ +static void push_onecapture (MatchState *ms, int i, const char *s, + const char *e) { + const char *cap; + ptrdiff_t l = get_onecapture(ms, i, s, e, &cap); + if (l != CAP_POSITION) + lua_pushlstring(ms->L, cap, l); + /* else position was already pushed */ +} + + static int push_captures (MatchState *ms, const char *s, const char *e) { int i; int nlevels = (ms->level == 0 && s) ? 1 : ms->level; @@ -817,60 +838,72 @@ static int gmatch (lua_State *L) { static void add_s (MatchState *ms, luaL_Buffer *b, const char *s, const char *e) { - size_t l, i; + size_t l; lua_State *L = ms->L; const char *news = lua_tolstring(L, 3, &l); - for (i = 0; i < l; i++) { - if (news[i] != L_ESC) - luaL_addchar(b, news[i]); - else { - i++; /* skip ESC */ - if (!isdigit(uchar(news[i]))) { - if (news[i] != L_ESC) - luaL_error(L, "invalid use of '%c' in replacement string", L_ESC); - luaL_addchar(b, news[i]); - } - else if (news[i] == '0') - luaL_addlstring(b, s, e - s); - else { - push_onecapture(ms, news[i] - '1', s, e); - luaL_tolstring(L, -1, NULL); /* if number, convert it to string */ - lua_remove(L, -2); /* remove original value */ - luaL_addvalue(b); /* add capture to accumulated result */ - } + const char *p; + while ((p = (char *)memchr(news, L_ESC, l)) != NULL) { + luaL_addlstring(b, news, p - news); + p++; /* skip ESC */ + if (*p == L_ESC) /* '%%' */ + luaL_addchar(b, *p); + else if (*p == '0') /* '%0' */ + luaL_addlstring(b, s, e - s); + else if (isdigit(uchar(*p))) { /* '%n' */ + const char *cap; + ptrdiff_t resl = get_onecapture(ms, *p - '1', s, e, &cap); + if (resl == CAP_POSITION) + luaL_addvalue(b); /* add position to accumulated result */ + else + luaL_addlstring(b, cap, resl); } + else + luaL_error(L, "invalid use of '%c' in replacement string", L_ESC); + l -= p + 1 - news; + news = p + 1; } + luaL_addlstring(b, news, l); } -static void add_value (MatchState *ms, luaL_Buffer *b, const char *s, - const char *e, int tr) { +/* +** Add the replacement value to the string buffer 'b'. +** Return true if the original string was changed. (Function calls and +** table indexing resulting in nil or false do not change the subject.) +*/ +static int add_value (MatchState *ms, luaL_Buffer *b, const char *s, + const char *e, int tr) { lua_State *L = ms->L; switch (tr) { - case LUA_TFUNCTION: { + case LUA_TFUNCTION: { /* call the function */ int n; - lua_pushvalue(L, 3); - n = push_captures(ms, s, e); - lua_call(L, n, 1); + lua_pushvalue(L, 3); /* push the function */ + n = push_captures(ms, s, e); /* all captures as arguments */ + lua_call(L, n, 1); /* call it */ break; } - case LUA_TTABLE: { - push_onecapture(ms, 0, s, e); + case LUA_TTABLE: { /* index the table */ + push_onecapture(ms, 0, s, e); /* first capture is the index */ lua_gettable(L, 3); break; } default: { /* LUA_TNUMBER or LUA_TSTRING */ - add_s(ms, b, s, e); - return; + add_s(ms, b, s, e); /* add value to the buffer */ + return 1; /* something changed */ } } if (!lua_toboolean(L, -1)) { /* nil or false? */ - lua_pop(L, 1); - lua_pushlstring(L, s, e - s); /* keep original text */ + lua_pop(L, 1); /* remove value */ + luaL_addlstring(b, s, e - s); /* keep original text */ + return 0; /* no changes */ } else if (!lua_isstring(L, -1)) - luaL_error(L, "invalid replacement value (a %s)", luaL_typename(L, -1)); - luaL_addvalue(b); /* add result to accumulator */ + return luaL_error(L, "invalid replacement value (a %s)", + luaL_typename(L, -1)); + else { + luaL_addvalue(b); /* add result to accumulator */ + return 1; /* something changed */ + } } @@ -883,6 +916,7 @@ static int str_gsub (lua_State *L) { lua_Integer max_s = luaL_optinteger(L, 4, srcl + 1); /* max replacements */ int anchor = (*p == '^'); lua_Integer n = 0; /* replacement count */ + int changed = 0; /* change flag */ MatchState ms; luaL_Buffer b; luaL_argexpected(L, tr == LUA_TNUMBER || tr == LUA_TSTRING || @@ -898,7 +932,7 @@ static int str_gsub (lua_State *L) { reprepstate(&ms); /* (re)prepare state for new match */ if ((e = match(&ms, src, p)) != NULL && e != lastmatch) { /* match? */ n++; - add_value(&ms, &b, src, e, tr); /* add replacement to buffer */ + changed = add_value(&ms, &b, src, e, tr) | changed; src = lastmatch = e; } else if (src < ms.src_end) /* otherwise, skip one character */ @@ -906,8 +940,12 @@ static int str_gsub (lua_State *L) { else break; /* end of subject */ if (anchor) break; } - luaL_addlstring(&b, src, ms.src_end-src); - luaL_pushresult(&b); + if (!changed) /* no changes? */ + lua_pushvalue(L, 1); /* return original string */ + else { /* something changed */ + luaL_addlstring(&b, src, ms.src_end-src); + luaL_pushresult(&b); /* create and return new string */ + } lua_pushinteger(L, n); /* number of substitutions */ return 2; } diff --git a/testes/gc.lua b/testes/gc.lua index 91e78a4822..6d24e0d82d 100644 --- a/testes/gc.lua +++ b/testes/gc.lua @@ -113,7 +113,7 @@ do contCreate = 0 while contCreate <= limit do a = contCreate .. "b"; - a = string.gsub(a, '(%d%d*)', string.upper) + a = string.gsub(a, '(%d%d*)', "%1 %1") a = "a" contCreate = contCreate+1 end diff --git a/testes/pm.lua b/testes/pm.lua index 8cc8772e87..4d87fad210 100644 --- a/testes/pm.lua +++ b/testes/pm.lua @@ -387,5 +387,35 @@ assert(string.match("abc\0\0\0", "%\0%\0?") == "\0\0") assert(string.find("abc\0\0","\0.") == 4) assert(string.find("abcx\0\0abc\0abc","x\0\0abc\0a.") == 4) + +do -- test reuse of original string in gsub + local s = string.rep("a", 100) + local r = string.gsub(s, "b", "c") -- no match + assert(string.format("%p", s) == string.format("%p", r)) + + r = string.gsub(s, ".", {x = "y"}) -- no substitutions + assert(string.format("%p", s) == string.format("%p", r)) + + local count = 0 + r = string.gsub(s, ".", function (x) + assert(x == "a") + count = count + 1 + return nil -- no substitution + end) + r = string.gsub(r, ".", {b = 'x'}) -- "a" is not a key; no subst. + assert(count == 100) + assert(string.format("%p", s) == string.format("%p", r)) + + count = 0 + r = string.gsub(s, ".", function (x) + assert(x == "a") + count = count + 1 + return x -- substitution... + end) + assert(count == 100) + -- no reuse in this case + assert(r == s and string.format("%p", s) ~= string.format("%p", r)) +end + print('OK') From 2d3f09544895b422eeecf89e0d108da8b8fcdfca Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 12 Apr 2019 11:48:24 -0300 Subject: [PATCH 0403/1145] Avoid using large buffers in 'string.format' The result of "string.format("%.99f", -1e308) is 410 characters long, but all other formats have much smaller limits (at most 99 plus a fex extras). This commit avoids 'string.format' asking for a buffer ~400 chars large when ~100 will do. --- lstrlib.c | 40 ++++++++++++++++++++++++++++------------ luaconf.h | 9 +-------- 2 files changed, 29 insertions(+), 20 deletions(-) diff --git a/lstrlib.c b/lstrlib.c index 53ed80a3bf..563d5ca52c 100644 --- a/lstrlib.c +++ b/lstrlib.c @@ -1038,13 +1038,23 @@ static int lua_number2strx (lua_State *L, char *buff, int sz, /* -** Maximum size of each formatted item. This maximum size is produced +** Maximum size for items formatted with '%f'. This size is produced ** by format('%.99f', -maxfloat), and is equal to 99 + 3 ('-', '.', ** and '\0') + number of decimal digits to represent maxfloat (which -** is maximum exponent + 1). (99+3+1 then rounded to 120 for "extra -** expenses", such as locale-dependent stuff) +** is maximum exponent + 1). (99+3+1, adding some extra, 110) */ -#define MAX_ITEM (120 + l_mathlim(MAX_10_EXP)) +#define MAX_ITEMF (110 + l_mathlim(MAX_10_EXP)) + + +/* +** All formats except '%f' do not need that large limit. The other +** float formats use exponents, so that they fit in the 99 limit for +** significant digits; 's' for large strings and 'q' add items directly +** to the buffer; all integer formats also fit in the 99 limit. The +** worst case are floats: they may need 99 significant digits, plus +** '0x', '-', '.', 'e+XXXX', and '\0'. Adding some extra, 120. +*/ +#define MAX_ITEM 120 /* valid flags in a format specification */ @@ -1194,38 +1204,44 @@ static int str_format (lua_State *L) { luaL_addchar(&b, *strfrmt++); /* %% */ else { /* format item */ char form[MAX_FORMAT]; /* to store the format ('%...') */ - char *buff = luaL_prepbuffsize(&b, MAX_ITEM); /* to put formatted item */ + int maxitem = MAX_ITEM; + char *buff = luaL_prepbuffsize(&b, maxitem); /* to put formatted item */ int nb = 0; /* number of bytes in added item */ if (++arg > top) return luaL_argerror(L, arg, "no value"); strfrmt = scanformat(L, strfrmt, form); switch (*strfrmt++) { case 'c': { - nb = l_sprintf(buff, MAX_ITEM, form, (int)luaL_checkinteger(L, arg)); + nb = l_sprintf(buff, maxitem, form, (int)luaL_checkinteger(L, arg)); break; } case 'd': case 'i': case 'o': case 'u': case 'x': case 'X': { lua_Integer n = luaL_checkinteger(L, arg); addlenmod(form, LUA_INTEGER_FRMLEN); - nb = l_sprintf(buff, MAX_ITEM, form, (LUAI_UACINT)n); + nb = l_sprintf(buff, maxitem, form, (LUAI_UACINT)n); break; } case 'a': case 'A': addlenmod(form, LUA_NUMBER_FRMLEN); - nb = lua_number2strx(L, buff, MAX_ITEM, form, + nb = lua_number2strx(L, buff, maxitem, form, luaL_checknumber(L, arg)); break; case 'e': case 'E': case 'f': case 'g': case 'G': { lua_Number n = luaL_checknumber(L, arg); + if (*(strfrmt - 1) == 'f' && l_mathop(fabs)(n) >= 1e100) { + /* 'n' needs more than 99 digits */ + maxitem = MAX_ITEMF; /* extra space for '%f' */ + buff = luaL_prepbuffsize(&b, maxitem); + } addlenmod(form, LUA_NUMBER_FRMLEN); - nb = l_sprintf(buff, MAX_ITEM, form, (LUAI_UACNUMBER)n); + nb = l_sprintf(buff, maxitem, form, (LUAI_UACNUMBER)n); break; } case 'p': { const void *p = lua_topointer(L, arg); - nb = l_sprintf(buff, MAX_ITEM, form, p); + nb = l_sprintf(buff, maxitem, form, p); break; } case 'q': { @@ -1246,7 +1262,7 @@ static int str_format (lua_State *L) { luaL_addvalue(&b); /* keep entire string */ } else { /* format the string into 'buff' */ - nb = l_sprintf(buff, MAX_ITEM, form, s); + nb = l_sprintf(buff, maxitem, form, s); lua_pop(L, 1); /* remove result from 'luaL_tolstring' */ } } @@ -1256,7 +1272,7 @@ static int str_format (lua_State *L) { return luaL_error(L, "invalid conversion '%s' to 'format'", form); } } - lua_assert(nb < MAX_ITEM); + lua_assert(nb < maxitem); luaL_addsize(&b, nb); } } diff --git a/luaconf.h b/luaconf.h index 5c714d4e53..76a6161675 100644 --- a/luaconf.h +++ b/luaconf.h @@ -709,16 +709,9 @@ /* @@ LUAL_BUFFERSIZE is the buffer size used by the lauxlib buffer system. -** CHANGE it if it uses too much C-stack space. (For long double, -** 'string.format("%.99f", -1e4932)' needs 5052 bytes, so a -** smaller buffer would force a memory allocation for each call to -** 'string.format'.) */ -#if LUA_FLOAT_TYPE == LUA_FLOAT_LONGDOUBLE -#define LUAL_BUFFERSIZE 8192 -#else #define LUAL_BUFFERSIZE ((int)(16 * sizeof(void*) * sizeof(lua_Number))) -#endif + /* @@ LUAI_MAXALIGN defines fields that, when used in a union, ensure From ed2872cd3bf6d352f36bbd34529738a60b0b51eb Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 17 Apr 2019 14:57:29 -0300 Subject: [PATCH 0404/1145] 'require' returns where module was found The function 'require' returns the *loader data* as a second result. For file searchers, this data is the path where they found the module. --- loadlib.c | 23 +++++++++++++++++------ lundump.c | 4 ++-- manual/manual.of | 38 +++++++++++++++++++++++++++----------- testes/attrib.lua | 35 ++++++++++++++++++++--------------- 4 files changed, 66 insertions(+), 34 deletions(-) diff --git a/loadlib.c b/loadlib.c index a6ce30d4f3..4cf9aec31f 100644 --- a/loadlib.c +++ b/loadlib.c @@ -576,9 +576,14 @@ static int searcher_Croot (lua_State *L) { static int searcher_preload (lua_State *L) { const char *name = luaL_checkstring(L, 1); lua_getfield(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE); - if (lua_getfield(L, -1, name) == LUA_TNIL) /* not found? */ + if (lua_getfield(L, -1, name) == LUA_TNIL) { /* not found? */ lua_pushfstring(L, "\n\tno field package.preload['%s']", name); - return 1; + return 1; + } + else { + lua_pushliteral(L, ":preload:"); + return 2; + } } @@ -620,17 +625,23 @@ static int ll_require (lua_State *L) { /* else must load package */ lua_pop(L, 1); /* remove 'getfield' result */ findloader(L, name); - lua_pushstring(L, name); /* pass name as argument to module loader */ - lua_insert(L, -2); /* name is 1st argument (before search data) */ + lua_rotate(L, -2, 1); /* function <-> loader data */ + lua_pushvalue(L, 1); /* name is 1st argument to module loader */ + lua_pushvalue(L, -3); /* loader data is 2nd argument */ + /* stack: ...; loader data; loader function; mod. name; loader data */ lua_call(L, 2, 1); /* run loader to load module */ + /* stack: ...; loader data; result from loader */ if (!lua_isnil(L, -1)) /* non-nil return? */ lua_setfield(L, 2, name); /* LOADED[name] = returned value */ + else + lua_pop(L, 1); /* pop nil */ if (lua_getfield(L, 2, name) == LUA_TNIL) { /* module set no value? */ lua_pushboolean(L, 1); /* use true as result */ - lua_pushvalue(L, -1); /* extra copy to be returned */ + lua_copy(L, -1, -2); /* replace loader result */ lua_setfield(L, 2, name); /* LOADED[name] = true */ } - return 1; + lua_rotate(L, -2, 1); /* loader data <-> module result */ + return 2; /* return module result and loader data */ } /* }====================================================== */ diff --git a/lundump.c b/lundump.c index a600493367..c1cff9e1ab 100644 --- a/lundump.c +++ b/lundump.c @@ -271,8 +271,8 @@ static void fchecksize (LoadState *S, size_t size, const char *tname) { #define checksize(S,t) fchecksize(S,sizeof(t),#t) static void checkHeader (LoadState *S) { - /* 1st char already checked */ - checkliteral(S, LUA_SIGNATURE + 1, "not a binary chunk"); + /* skip 1st char (already read and checked) */ + checkliteral(S, &LUA_SIGNATURE[1], "not a binary chunk"); if (LoadInt(S) != LUAC_VERSION) error(S, "version mismatch"); if (LoadByte(S) != LUAC_FORMAT) diff --git a/manual/manual.of b/manual/manual.of index fea6922e6e..24ac45ae80 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -6408,11 +6408,15 @@ The function starts by looking into the @Lid{package.loaded} table to determine whether @id{modname} is already loaded. If it is, then @id{require} returns the value stored at @T{package.loaded[modname]}. +(The absence of a second result in this case +signals that this call did not have to load the module.) Otherwise, it tries to find a @emph{loader} for the module. To find a loader, -@id{require} is guided by the @Lid{package.searchers} sequence. -By changing this sequence, +@id{require} is guided by the table @Lid{package.searchers}. +Each item in this table is a search function, +that searches for the module in a particular way. +By changing this table, we can change how @id{require} looks for a module. The following explanation is based on the default configuration for @Lid{package.searchers}. @@ -6429,9 +6433,14 @@ it tries an @emph{all-in-one} loader @seeF{package.searchers}. Once a loader is found, @id{require} calls the loader with two arguments: -@id{modname} and an extra value dependent on how it got the loader. -(If the loader came from a file, -this extra value is the file name.) +@id{modname} and an extra value, +a @emph{loader data}, +also returned by the searcher. +The loader data can be any value useful to the module; +for the default searchers, +it indicates where the loader was found. +(For instance, if the loader came from a file, +this extra value is the file path.) If the loader returns any non-nil value, @id{require} assigns the returned value to @T{package.loaded[modname]}. If the loader does not return a non-nil value and @@ -6439,6 +6448,9 @@ has not assigned any value to @T{package.loaded[modname]}, then @id{require} assigns @Rw{true} to this entry. In any case, @id{require} returns the final value of @T{package.loaded[modname]}. +Besides that value, @id{require} also returns as a second result +the loader data returned by the searcher, +which indicates how @id{require} found the module. If there is any error loading or running the module, or if it cannot find any loader for the module, @@ -6558,16 +6570,20 @@ table used by @Lid{require}. @LibEntry{package.searchers| -A table used by @Lid{require} to control how to load modules. +A table used by @Lid{require} to control how to find modules. Each entry in this table is a @def{searcher function}. When looking for a module, @Lid{require} calls each of these searchers in ascending order, with the module name (the argument given to @Lid{require}) as its sole argument. -The function can return another function (the module @def{loader}) -plus an extra value that will be passed to that loader, -or a string explaining why it did not find that module +If the searcher finds the module, +it returns another function, the module @def{loader}, +plus an extra value, a @emph{loader data}, +that will be passed to that loader and +returned as a second result by @Lid{require}. +If it cannot find the module, +it returns a string explaining why (or @nil if it has nothing to say). Lua initializes this table with four searcher functions. @@ -6617,9 +6633,9 @@ into one single library, with each submodule keeping its original open function. All searchers except the first one (preload) return as the extra value -the file name where the module was found, +the file path where the module was found, as returned by @Lid{package.searchpath}. -The first searcher returns no extra value. +The first searcher always returns the string @St{:preload:}. } diff --git a/testes/attrib.lua b/testes/attrib.lua index dcafd6345d..4adb42e0d2 100644 --- a/testes/attrib.lua +++ b/testes/attrib.lua @@ -122,12 +122,13 @@ local oldpath = package.path package.path = string.gsub("D/?.lua;D/?.lc;D/?;D/??x?;D/L", "D/", DIR) -local try = function (p, n, r) +local try = function (p, n, r, ext) NAME = nil - local rr = require(p) + local rr, x = require(p) assert(NAME == n) assert(REQUIRED == p) assert(rr == r) + assert(ext == x) end a = require"names" @@ -143,27 +144,27 @@ assert(package.searchpath("C", package.path) == D"C.lua") assert(require"C" == 25) assert(require"C" == 25) AA = nil -try('B', 'B.lua', true) +try('B', 'B.lua', true, "libs/B.lua") assert(package.loaded.B) assert(require"B" == true) assert(package.loaded.A) assert(require"C" == 25) package.loaded.A = nil -try('B', nil, true) -- should not reload package -try('A', 'A.lua', true) +try('B', nil, true, nil) -- should not reload package +try('A', 'A.lua', true, "libs/A.lua") package.loaded.A = nil os.remove(D'A.lua') AA = {} -try('A', 'A.lc', AA) -- now must find second option +try('A', 'A.lc', AA, "libs/A.lc") -- now must find second option assert(package.searchpath("A", package.path) == D"A.lc") assert(require("A") == AA) AA = false -try('K', 'L', false) -- default option -try('K', 'L', false) -- default option (should reload it) +try('K', 'L', false, "libs/L") -- default option +try('K', 'L', false, "libs/L") -- default option (should reload it) assert(rawget(_G, "_REQUIREDNAME") == nil) AA = "x" -try("X", "XXxX", AA) +try("X", "XXxX", AA, "libs/XXxX") removefiles(files) @@ -183,14 +184,16 @@ files = { createfiles(files, "_ENV = {}\n", "\nreturn _ENV\n") AA = 0 -local m = assert(require"P1") +local m, ext = assert(require"P1") +assert(ext == "libs/P1/init.lua") assert(AA == 0 and m.AA == 10) assert(require"P1" == m) assert(require"P1" == m) assert(package.searchpath("P1.xuxu", package.path) == D"P1/xuxu.lua") -m.xuxu = assert(require"P1.xuxu") +m.xuxu, ext = assert(require"P1.xuxu") assert(AA == 0 and m.xuxu.AA == 20) +assert(ext == "libs/P1/xuxu.lua") assert(require"P1.xuxu" == m.xuxu) assert(require"P1.xuxu" == m.xuxu) assert(require"P1" == m and m.AA == 10) @@ -267,15 +270,17 @@ else -- test C modules with prefixes in names package.cpath = DC"?" - local lib2 = require"lib2-v2" + local lib2, ext = require"lib2-v2" + assert(string.find(ext, "libs/lib2-v2", 1, true)) -- check correct access to global environment and correct -- parameters assert(_ENV.x == "lib2-v2" and _ENV.y == DC"lib2-v2") assert(lib2.id("x") == "x") -- test C submodules - local fs = require"lib1.sub" + local fs, ext = require"lib1.sub" assert(_ENV.x == "lib1.sub" and _ENV.y == DC"lib1") + assert(string.find(ext, "libs/lib1", 1, true)) assert(fs.id(45) == 45) end @@ -293,10 +298,10 @@ do return _ENV end - local pl = require"pl" + local pl, ext = require"pl" assert(require"pl" == pl) assert(pl.xuxu(10) == 30) - assert(pl[1] == "pl" and pl[2] == nil) + assert(pl[1] == "pl" and pl[2] == ":preload:" and ext == ":preload:") package = p assert(type(package.path) == "string") From 20b161e2859837e4f7fb1c19440ad7efe1588f1f Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 22 Apr 2019 12:31:29 -0300 Subject: [PATCH 0405/1145] Small correction in test about 'isdst' The field 'isdst' can be false, so we cannot test its absence with 'if not D.isdst'; we must compare with nil for a correct test. --- testes/files.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/testes/files.lua b/testes/files.lua index 0a05cf6046..38d3a6693b 100644 --- a/testes/files.lua +++ b/testes/files.lua @@ -832,7 +832,7 @@ load(os.date([[!assert(D.year==%Y and D.month==%m and D.day==%d and do local D = os.date("*t") local t = os.time(D) - if not D.isdst then + if D.isdst == nil then print("no daylight saving information") else assert(type(D.isdst) == 'boolean') From 3da34a5fa70a51f0cf06d677a4f07b470693260c Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 24 Apr 2019 14:01:20 -0300 Subject: [PATCH 0406/1145] Revamp of 'lua_pushfstring' / 'luaO_pushvfstring' The function 'luaO_pushvfstring' now uses an internal buffer to concatenate small strings, instead of pushing all pieces on the stack. This avoids the creation of several small Lua strings for each piece of the result. (For instance, a format like "n: '%d'" used to create three intermediate strings: "n: '", the numeral, and "'". Now it creates none.) --- lobject.c | 144 +++++++++++++++++++++++++++++++++------------ ltests.c | 9 +++ luaconf.h | 7 --- testes/strings.lua | 61 +++++++++++++++++++ 4 files changed, 177 insertions(+), 44 deletions(-) diff --git a/lobject.c b/lobject.c index 67c37124f3..123f0e570f 100644 --- a/lobject.c +++ b/lobject.c @@ -364,85 +364,154 @@ int luaO_utf8esc (char *buff, unsigned long x) { /* -** Convert a number object to a string +** Convert a number object to a string, adding it to a buffer */ -void luaO_tostring (lua_State *L, TValue *obj) { - char buff[MAXNUMBER2STR]; +static size_t tostringbuff (TValue *obj, char *buff) { size_t len; lua_assert(ttisnumber(obj)); if (ttisinteger(obj)) - len = lua_integer2str(buff, sizeof(buff), ivalue(obj)); + len = lua_integer2str(buff, MAXNUMBER2STR, ivalue(obj)); else { - len = lua_number2str(buff, sizeof(buff), fltvalue(obj)); + len = lua_number2str(buff, MAXNUMBER2STR, fltvalue(obj)); if (buff[strspn(buff, "-0123456789")] == '\0') { /* looks like an int? */ buff[len++] = lua_getlocaledecpoint(); buff[len++] = '0'; /* adds '.0' to result */ } } + return len; +} + + +/* +** Convert a number object to a Lua string, replacing the value at 'obj' +*/ +void luaO_tostring (lua_State *L, TValue *obj) { + char buff[MAXNUMBER2STR]; + size_t len = tostringbuff(obj, buff); setsvalue(L, obj, luaS_newlstr(L, buff, len)); } +/* size for buffer used by 'luaO_pushvfstring' */ +#define BUFVFS 400 + +/* buffer used by 'luaO_pushvfstring' */ +typedef struct BuffFS { + int blen; /* length of partial string in 'buff' */ + char buff[BUFVFS]; /* holds last part of the result */ +} BuffFS; + + static void pushstr (lua_State *L, const char *str, size_t l) { setsvalue2s(L, L->top, luaS_newlstr(L, str, l)); L->top++; } +/* +** empty the buffer into the stack +*/ +static void clearbuff (lua_State *L, BuffFS *buff) { + pushstr(L, buff->buff, buff->blen); /* push buffer */ + buff->blen = 0; /* buffer now is empty */ +} + + +/* +** Add 'str' to the buffer. It buffer has no enough space, +** empty the buffer. If string is still larger than the buffer, +** push the string directly to the stack. Return number of items +** pushed. +*/ +static int addstr2buff (lua_State *L, BuffFS *buff, const char *str, + size_t slen) { + int pushed = 0; /* number of items pushed to the stack */ + lua_assert(buff->blen <= BUFVFS); + if (slen > BUFVFS - cast_sizet(buff->blen)) { /* string does not fit? */ + clearbuff(L, buff); + pushed = 1; + if (slen >= BUFVFS) { /* string still does not fit into buffer? */ + pushstr(L, str, slen); /* push string */ + return 2; + } + } + memcpy(buff->buff + buff->blen, str, slen); /* add string to buffer */ + buff->blen += slen; + return pushed; +} + + +/* +** Add a number to the buffer; return number of strings pushed into +** the stack. (At most one, to free buffer space.) +*/ +static int addnum2buff (lua_State *L, BuffFS *buff, TValue *num) { + char numbuff[MAXNUMBER2STR]; + size_t len = tostringbuff(num, numbuff); /* format number into 'numbuff' */ + return addstr2buff(L, buff, numbuff, len); +} + + /* ** this function handles only '%d', '%c', '%f', '%p', and '%s' conventional formats, plus Lua-specific '%I' and '%U' */ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) { - int n = 0; /* number of strings in the stack to concatenate */ - const char *e; /* points to next conversion specifier */ + BuffFS buff; /* holds last part of the result */ + int pushed = 0; /* number of strings in the stack to concatenate */ + const char *e; /* points to next '%' */ + buff.blen = 0; while ((e = strchr(fmt, '%')) != NULL) { - pushstr(L, fmt, e - fmt); /* string up to conversion specifier */ - switch (*(e+1)) { + pushed += addstr2buff(L, &buff, fmt, e - fmt); /* add 'fmt' up to '%' */ + switch (*(e + 1)) { /* conversion specifier */ case 's': { /* zero-terminated string */ const char *s = va_arg(argp, char *); if (s == NULL) s = "(null)"; - pushstr(L, s, strlen(s)); + pushed += addstr2buff(L, &buff, s, strlen(s)); break; } case 'c': { /* an 'int' as a character */ - char buff = cast_char(va_arg(argp, int)); - if (lisprint(cast_uchar(buff))) - pushstr(L, &buff, 1); - else /* non-printable character; print its code */ - luaO_pushfstring(L, "<\\%d>", cast_uchar(buff)); + /* if non-printable character, print its code */ + char bf[10]; + int c = va_arg(argp, int); + int l = (lisprint(c)) ? l_sprintf(bf, sizeof(bf), "%c", c) + : l_sprintf(bf, sizeof(bf), "<\\%u>", c); + pushed += addstr2buff(L, &buff, bf, l); break; } case 'd': { /* an 'int' */ - setivalue(s2v(L->top), va_arg(argp, int)); - goto top2str; + TValue num; + setivalue(&num, va_arg(argp, int)); + pushed += addnum2buff(L, &buff, &num); + break; } case 'I': { /* a 'lua_Integer' */ - setivalue(s2v(L->top), cast(lua_Integer, va_arg(argp, l_uacInt))); - goto top2str; + TValue num; + setivalue(&num, cast(lua_Integer, va_arg(argp, l_uacInt))); + pushed += addnum2buff(L, &buff, &num); + break; } case 'f': { /* a 'lua_Number' */ - setfltvalue(s2v(L->top), cast_num(va_arg(argp, l_uacNumber))); - top2str: /* convert the top element to a string */ - L->top++; - luaO_tostring(L, s2v(L->top - 1)); + TValue num; + setfltvalue(&num, cast_num(va_arg(argp, l_uacNumber))); + pushed += addnum2buff(L, &buff, &num); break; } case 'p': { /* a pointer */ - char buff[4*sizeof(void *) + 8]; /* should be enough space for a '%p' */ + char bf[3 * sizeof(void*) + 8]; /* should be enough space for '%p' */ void *p = va_arg(argp, void *); - int l = lua_pointer2str(buff, sizeof(buff), p); - pushstr(L, buff, l); + int l = l_sprintf(bf, sizeof(bf), "%p", p); + pushed += addstr2buff(L, &buff, bf, l); break; } case 'U': { /* a 'long' as a UTF-8 sequence */ - char buff[UTF8BUFFSZ]; - int l = luaO_utf8esc(buff, va_arg(argp, long)); - pushstr(L, buff + UTF8BUFFSZ - l, l); + char bf[UTF8BUFFSZ]; + int l = luaO_utf8esc(bf, va_arg(argp, long)); + pushed += addstr2buff(L, &buff, bf + UTF8BUFFSZ - l, l); break; } case '%': { - pushstr(L, "%", 1); + pushed += addstr2buff(L, &buff, "%", 1); break; } default: { @@ -450,15 +519,16 @@ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) { *(e + 1)); } } - n += 2; - if (L->top + 2 > L->stack_last) { /* no free stack space? */ - luaV_concat(L, n); - n = 1; + if (pushed > 1 && L->top + 2 > L->stack_last) { /* no free stack space? */ + luaV_concat(L, pushed); /* join all partial results into one */ + pushed = 1; } - fmt = e + 2; + fmt = e + 2; /* skip '%' and the specifier */ } - pushstr(L, fmt, strlen(fmt)); - if (n > 0) luaV_concat(L, n + 1); + pushed += addstr2buff(L, &buff, fmt, strlen(fmt)); /* rest of 'fmt' */ + clearbuff(L, &buff); /* empty buffer into the stack */ + if (pushed > 0) + luaV_concat(L, pushed + 1); /* join all partial results */ return svalue(s2v(L->top - 1)); } diff --git a/ltests.c b/ltests.c index 40de22927b..7d441d1ac2 100644 --- a/ltests.c +++ b/ltests.c @@ -1481,6 +1481,15 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) { else if EQ("pushvalue") { lua_pushvalue(L1, getindex); } + else if EQ("pushfstringI") { + lua_pushfstring(L1, lua_tostring(L, -2), (int)lua_tointeger(L, -1)); + } + else if EQ("pushfstringS") { + lua_pushfstring(L1, lua_tostring(L, -2), lua_tostring(L, -1)); + } + else if EQ("pushfstringP") { + lua_pushfstring(L1, lua_tostring(L, -2), lua_topointer(L, -1)); + } else if EQ("rawgeti") { int t = getindex; lua_rawgeti(L1, t, getnum); diff --git a/luaconf.h b/luaconf.h index 76a6161675..019f2eb68a 100644 --- a/luaconf.h +++ b/luaconf.h @@ -578,13 +578,6 @@ #endif -/* -@@ lua_pointer2str converts a pointer to a readable string in a -** non-specified way. -*/ -#define lua_pointer2str(buff,sz,p) l_sprintf(buff,sz,"%p",p) - - /* @@ lua_number2strx converts a float to a hexadecimal numeric string. ** In C99, 'sprintf' (with format specifiers '%a'/'%A') does that. diff --git a/testes/strings.lua b/testes/strings.lua index 8bcbb39156..66c1176db5 100644 --- a/testes/strings.lua +++ b/testes/strings.lua @@ -400,5 +400,66 @@ do assert(co() == "2") end + +if T==nil then + (Message or print) + ("\n >>> testC not active: skipping 'pushfstring' tests <<<\n") +else + + print"testing 'pushfstring'" + + -- formats %U, %f, %I already tested elsewhere + + local blen = 400 -- internal buffer length in 'luaO_pushfstring' + + local function callpfs (op, fmt, n) + local x = {T.testC("pushfstring" .. op .. "; return *", fmt, n)} + -- stack has code, 'fmt', 'n', and result from operation + assert(#x == 4) -- make sure nothing else was left in the stack + return x[4] + end + + local function testpfs (op, fmt, n) + assert(callpfs(op, fmt, n) == string.format(fmt, n)) + end + + testpfs("I", "", 0) + testpfs("I", string.rep("a", blen - 1), 0) + testpfs("I", string.rep("a", blen), 0) + testpfs("I", string.rep("a", blen + 1), 0) + + local str = string.rep("ab", blen) .. "%d" .. string.rep("d", blen / 2) + testpfs("I", str, 2^14) + testpfs("I", str, -2^15) + + str = "%d" .. string.rep("cd", blen) + testpfs("I", str, 2^14) + testpfs("I", str, -2^15) + + str = string.rep("c", blen - 2) .. "%d" + testpfs("I", str, 2^14) + testpfs("I", str, -2^15) + + for l = 12, 14 do + local str1 = string.rep("a", l) + for i = 0, 500, 13 do + for j = 0, 500, 13 do + str = string.rep("a", i) .. "%s" .. string.rep("d", j) + testpfs("S", str, str1) + testpfs("S", str, str) + end + end + end + + str = "abc %c def" + testpfs("I", str, string.byte("A")) + -- non-printable character + assert(callpfs("I", str, 255) == "abc <\\255> def") + + str = string.rep("a", blen - 1) .. "%p" .. string.rep("cd", blen) + testpfs("P", str, {}) +end + + print('OK') From c65605151c5a335baf0a9ea251b19df5b2d3a905 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 24 Apr 2019 14:41:41 -0300 Subject: [PATCH 0407/1145] New function 'luaL_addgsub' Added a new function 'luaL_addgsub', similar to 'luaL_gsub' but that adds its result directly to a preexisting buffer, avoiding the creation of one extra intermediate string. Also added two simple macros, 'luaL_bufflen' and 'luaL_buffaddr', to query the current length and the contents address of a buffer. --- lauxlib.c | 20 +++++++++++++------- lauxlib.h | 16 ++++++++++++---- manual/manual.of | 35 +++++++++++++++++++++++++++++++++-- 3 files changed, 58 insertions(+), 13 deletions(-) diff --git a/lauxlib.c b/lauxlib.c index e9c02d3660..dfe501a733 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -951,18 +951,24 @@ LUALIB_API void luaL_requiref (lua_State *L, const char *modname, } -LUALIB_API const char *luaL_gsub (lua_State *L, const char *s, const char *p, - const char *r) { +LUALIB_API void luaL_addgsub (luaL_Buffer *b, const char *s, + const char *p, const char *r) { const char *wild; size_t l = strlen(p); - luaL_Buffer b; - luaL_buffinit(L, &b); while ((wild = strstr(s, p)) != NULL) { - luaL_addlstring(&b, s, wild - s); /* push prefix */ - luaL_addstring(&b, r); /* push replacement in place of pattern */ + luaL_addlstring(b, s, wild - s); /* push prefix */ + luaL_addstring(b, r); /* push replacement in place of pattern */ s = wild + l; /* continue after 'p' */ } - luaL_addstring(&b, s); /* push last suffix */ + luaL_addstring(b, s); /* push last suffix */ +} + + +LUALIB_API const char *luaL_gsub (lua_State *L, const char *s, + const char *p, const char *r) { + luaL_Buffer b; + luaL_buffinit(L, &b); + luaL_addgsub(&b, s, p, r); luaL_pushresult(&b); return lua_tostring(L, -1); } diff --git a/lauxlib.h b/lauxlib.h index e5d378aebe..f68f6af18c 100644 --- a/lauxlib.h +++ b/lauxlib.h @@ -19,6 +19,8 @@ #define LUA_GNAME "_G" +typedef struct luaL_Buffer luaL_Buffer; + /* extra error code for 'luaL_loadfilex' */ #define LUA_ERRFILE (LUA_ERRERR+1) @@ -99,8 +101,10 @@ LUALIB_API lua_State *(luaL_newstate) (void); LUALIB_API lua_Integer (luaL_len) (lua_State *L, int idx); -LUALIB_API const char *(luaL_gsub) (lua_State *L, const char *s, const char *p, - const char *r); +LUALIB_API void luaL_addgsub (luaL_Buffer *b, const char *s, + const char *p, const char *r); +LUALIB_API const char *(luaL_gsub) (lua_State *L, const char *s, + const char *p, const char *r); LUALIB_API void (luaL_setfuncs) (lua_State *L, const luaL_Reg *l, int nup); @@ -155,7 +159,7 @@ LUALIB_API void (luaL_requiref) (lua_State *L, const char *modname, ** ======================================================= */ -typedef struct luaL_Buffer { +struct luaL_Buffer { char *b; /* buffer address */ size_t size; /* buffer size */ size_t n; /* number of characters in buffer */ @@ -164,7 +168,11 @@ typedef struct luaL_Buffer { LUAI_MAXALIGN; /* ensure maximum alignment for buffer */ char b[LUAL_BUFFERSIZE]; /* initial buffer */ } init; -} luaL_Buffer; +}; + + +#define luaL_bufflen(bf) ((bf)->n) +#define luaL_buffaddr(bf) ((bf)->b) #define luaL_addchar(B,c) \ diff --git a/manual/manual.of b/manual/manual.of index 24ac45ae80..5f26570893 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -591,7 +591,7 @@ controls how long the collector waits before starting a new cycle. The collector starts a new cycle when the use of memory hits @M{n%} of the use after the previous collection. Larger values make the collector less aggressive. -Values less than 100 mean the collector will not wait to +Values equal to or less than 100 mean the collector will not wait to start a new cycle. A value of 200 means that the collector waits for the total memory in use to double before starting a new cycle. @@ -4928,6 +4928,18 @@ Adds the byte @id{c} to the buffer @id{B} } +@APIEntry{ +const void luaL_addgsub (luaL_Buffer *B, const char *s, + const char *p, const char *r);| +@apii{0,0,m} + +Adds a copy of the string @id{s} to the buffer @id{B}, +replacing any occurrence of the string @id{p} +with the string @id{r}. +@seeC{luaL_Buffer}. + +} + @APIEntry{void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l);| @apii{?,?,m} @@ -5070,6 +5082,15 @@ plus the final string on its top. } +@APIEntry{char *luaL_buffaddr (luaL_Buffer *B);| +@apii{0,0,-} + +Returns the address of the current contents of buffer @id{B}. +Note that any addition to the buffer may invalidate this address. +@seeC{luaL_Buffer}. + +} + @APIEntry{void luaL_buffinit (lua_State *L, luaL_Buffer *B);| @apii{0,0,-} @@ -5080,6 +5101,14 @@ the buffer must be declared as a variable } +@APIEntry{size_t luaL_bufflen (luaL_Buffer *B);| +@apii{0,0,-} + +Returns the length of the current contents of buffer @id{B}. +@seeC{luaL_Buffer}. + +} + @APIEntry{char *luaL_buffinitsize (lua_State *L, luaL_Buffer *B, size_t sz);| @apii{?,?,m} @@ -5935,6 +5964,7 @@ This option can be followed by three numbers: the garbage-collector pause, the step multiplier, and the step size. +A zero means to not change that value. } @item{@St{generational}| @@ -5942,6 +5972,7 @@ Change the collector mode to generational. This option can be followed by two numbers: the garbage-collector minor multiplier and the major multiplier. +A zero means to not change that value. } @item{@St{isrunning}| @@ -6552,7 +6583,7 @@ the value of the environment variable @defid{LUA_PATH_5_4} or the environment variable @defid{LUA_PATH} or with a default path defined in @id{luaconf.h}, if those environment variables are not defined. -Any @St{;;} in the value of the environment variable +A @St{;;} in the value of the environment variable is replaced by the default path. } From 969b8c1f14f69c1406f00df3b5f375a9efb7b78f Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 26 Apr 2019 11:13:01 -0300 Subject: [PATCH 0408/1145] Fixed bug with to-be-closed variables in base C level To-be-closed variables in C use 'ci.nresults' to code that there is a variable to be closed in that function. The intialization of the base C level (the one "running" when calling API functions outside any Lua call) did not initialize 'ci.nresults', creating (correct) warnings in valgrind. --- lstate.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lstate.c b/lstate.c index 463a47d2bf..387cd362dc 100644 --- a/lstate.c +++ b/lstate.c @@ -196,6 +196,8 @@ static void stack_init (lua_State *L1, lua_State *L) { ci->next = ci->previous = NULL; ci->callstatus = CIST_C; ci->func = L1->top; + ci->u.c.k = NULL; + ci->nresults = 0; setnilvalue(s2v(L1->top)); /* 'function' entry for this 'ci' */ L1->top++; ci->top = L1->top + LUA_MINSTACK; From b36e26f51b117df98f0f5376f352c2381df3025f Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 26 Apr 2019 11:24:39 -0300 Subject: [PATCH 0409/1145] Some more small improvements to 'luaO_pushvfstring' Details: - counter 'pushed' moved to the struct 'BuffFS' - new auxiliar function 'getbuff' to build strings directly on the buffer. --- lobject.c | 122 +++++++++++++++++++++++++-------------------- testes/strings.lua | 3 ++ 2 files changed, 71 insertions(+), 54 deletions(-) diff --git a/lobject.c b/lobject.c index 123f0e570f..58eecd4a36 100644 --- a/lobject.c +++ b/lobject.c @@ -392,126 +392,144 @@ void luaO_tostring (lua_State *L, TValue *obj) { } -/* size for buffer used by 'luaO_pushvfstring' */ +/* size for buffer space used by 'luaO_pushvfstring' */ #define BUFVFS 400 /* buffer used by 'luaO_pushvfstring' */ typedef struct BuffFS { - int blen; /* length of partial string in 'buff' */ - char buff[BUFVFS]; /* holds last part of the result */ + int pushed; /* number of string pieces already on the stack */ + int blen; /* length of partial string in 'space' */ + char space[BUFVFS]; /* holds last part of the result */ } BuffFS; -static void pushstr (lua_State *L, const char *str, size_t l) { +/* +** Push given string to the stack, as part of the buffer. If the stack +** is almost full, join all partial strings in the stack into one. +*/ +static void pushstr (lua_State *L, BuffFS *buff, const char *str, size_t l) { setsvalue2s(L, L->top, luaS_newlstr(L, str, l)); L->top++; + buff->pushed++; + if (buff->pushed > 1 && L->top + 2 > L->stack_last) { + luaV_concat(L, buff->pushed); /* join all partial results into one */ + buff->pushed = 1; + } } /* -** empty the buffer into the stack +** empty the buffer space into the stack */ static void clearbuff (lua_State *L, BuffFS *buff) { - pushstr(L, buff->buff, buff->blen); /* push buffer */ - buff->blen = 0; /* buffer now is empty */ + pushstr(L, buff, buff->space, buff->blen); /* push buffer contents */ + buff->blen = 0; /* space now is empty */ } /* -** Add 'str' to the buffer. It buffer has no enough space, -** empty the buffer. If string is still larger than the buffer, -** push the string directly to the stack. Return number of items -** pushed. +** Get a space of size 'sz' in the buffer. If buffer has not enough +** space, empty it. 'sz' must fit in an empty space. */ -static int addstr2buff (lua_State *L, BuffFS *buff, const char *str, - size_t slen) { - int pushed = 0; /* number of items pushed to the stack */ - lua_assert(buff->blen <= BUFVFS); - if (slen > BUFVFS - cast_sizet(buff->blen)) { /* string does not fit? */ +static char *getbuff (lua_State *L, BuffFS *buff, size_t sz) { + lua_assert(buff->blen <= BUFVFS); lua_assert(sz <= BUFVFS); + if (sz > BUFVFS - cast_sizet(buff->blen)) /* string does not fit? */ clearbuff(L, buff); - pushed = 1; - if (slen >= BUFVFS) { /* string still does not fit into buffer? */ - pushstr(L, str, slen); /* push string */ - return 2; - } + return buff->space + buff->blen; +} + + +#define addsize(b,sz) ((b)->blen += (sz)) + + +/* +** Add 'str' to the buffer. If string is larger than the buffer space, +** push the string directly to the stack. +*/ +static void addstr2buff (lua_State *L, BuffFS *buff, const char *str, + size_t slen) { + if (slen <= BUFVFS) { /* does string fit into buffer? */ + char *bf = getbuff(L, buff, slen); + memcpy(bf, str, slen); /* add string to buffer */ + addsize(buff, slen); + } + else { /* string larger than buffer */ + clearbuff(L, buff); /* string comes after buffer's content */ + pushstr(L, buff, str, slen); /* push string */ } - memcpy(buff->buff + buff->blen, str, slen); /* add string to buffer */ - buff->blen += slen; - return pushed; } /* -** Add a number to the buffer; return number of strings pushed into -** the stack. (At most one, to free buffer space.) +** Add a number to the buffer. */ -static int addnum2buff (lua_State *L, BuffFS *buff, TValue *num) { - char numbuff[MAXNUMBER2STR]; +static void addnum2buff (lua_State *L, BuffFS *buff, TValue *num) { + char *numbuff = getbuff(L, buff, MAXNUMBER2STR); size_t len = tostringbuff(num, numbuff); /* format number into 'numbuff' */ - return addstr2buff(L, buff, numbuff, len); + addsize(buff, len); } /* -** this function handles only '%d', '%c', '%f', '%p', and '%s' +** this function handles only '%d', '%c', '%f', '%p', '%s', and '%%' conventional formats, plus Lua-specific '%I' and '%U' */ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) { BuffFS buff; /* holds last part of the result */ - int pushed = 0; /* number of strings in the stack to concatenate */ const char *e; /* points to next '%' */ - buff.blen = 0; + buff.pushed = buff.blen = 0; while ((e = strchr(fmt, '%')) != NULL) { - pushed += addstr2buff(L, &buff, fmt, e - fmt); /* add 'fmt' up to '%' */ + addstr2buff(L, &buff, fmt, e - fmt); /* add 'fmt' up to '%' */ switch (*(e + 1)) { /* conversion specifier */ case 's': { /* zero-terminated string */ const char *s = va_arg(argp, char *); if (s == NULL) s = "(null)"; - pushed += addstr2buff(L, &buff, s, strlen(s)); + addstr2buff(L, &buff, s, strlen(s)); break; } case 'c': { /* an 'int' as a character */ /* if non-printable character, print its code */ - char bf[10]; + char *bf = getbuff(L, &buff, 10); int c = va_arg(argp, int); - int l = (lisprint(c)) ? l_sprintf(bf, sizeof(bf), "%c", c) - : l_sprintf(bf, sizeof(bf), "<\\%u>", c); - pushed += addstr2buff(L, &buff, bf, l); + int len = (lisprint(c)) ? l_sprintf(bf, 10, "%c", c) + : l_sprintf(bf, 10, "<\\%u>", c); + addsize(&buff, len); break; } case 'd': { /* an 'int' */ TValue num; setivalue(&num, va_arg(argp, int)); - pushed += addnum2buff(L, &buff, &num); + addnum2buff(L, &buff, &num); break; } case 'I': { /* a 'lua_Integer' */ TValue num; setivalue(&num, cast(lua_Integer, va_arg(argp, l_uacInt))); - pushed += addnum2buff(L, &buff, &num); + addnum2buff(L, &buff, &num); break; } case 'f': { /* a 'lua_Number' */ TValue num; setfltvalue(&num, cast_num(va_arg(argp, l_uacNumber))); - pushed += addnum2buff(L, &buff, &num); + addnum2buff(L, &buff, &num); break; } case 'p': { /* a pointer */ - char bf[3 * sizeof(void*) + 8]; /* should be enough space for '%p' */ + const int sz = 3 * sizeof(void*) + 8; /* enough space for '%p' */ + char *bf = getbuff(L, &buff, sz); void *p = va_arg(argp, void *); - int l = l_sprintf(bf, sizeof(bf), "%p", p); - pushed += addstr2buff(L, &buff, bf, l); + int len = l_sprintf(bf, sz, "%p", p); + addsize(&buff, len); break; } case 'U': { /* a 'long' as a UTF-8 sequence */ char bf[UTF8BUFFSZ]; - int l = luaO_utf8esc(bf, va_arg(argp, long)); - pushed += addstr2buff(L, &buff, bf + UTF8BUFFSZ - l, l); + int len = luaO_utf8esc(bf, va_arg(argp, long)); + addstr2buff(L, &buff, bf + UTF8BUFFSZ - len, len); break; } case '%': { - pushed += addstr2buff(L, &buff, "%", 1); + addstr2buff(L, &buff, "%", 1); break; } default: { @@ -519,16 +537,12 @@ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) { *(e + 1)); } } - if (pushed > 1 && L->top + 2 > L->stack_last) { /* no free stack space? */ - luaV_concat(L, pushed); /* join all partial results into one */ - pushed = 1; - } fmt = e + 2; /* skip '%' and the specifier */ } - pushed += addstr2buff(L, &buff, fmt, strlen(fmt)); /* rest of 'fmt' */ + addstr2buff(L, &buff, fmt, strlen(fmt)); /* rest of 'fmt' */ clearbuff(L, &buff); /* empty buffer into the stack */ - if (pushed > 0) - luaV_concat(L, pushed + 1); /* join all partial results */ + if (buff.pushed > 1) + luaV_concat(L, buff.pushed); /* join all partial results */ return svalue(s2v(L->top - 1)); } diff --git a/testes/strings.lua b/testes/strings.lua index 66c1176db5..bc123d1ab4 100644 --- a/testes/strings.lua +++ b/testes/strings.lua @@ -458,6 +458,9 @@ else str = string.rep("a", blen - 1) .. "%p" .. string.rep("cd", blen) testpfs("P", str, {}) + + str = string.rep("%%", 3 * blen) .. "%p" .. string.rep("%%", 2 * blen) + testpfs("P", str, {}) end From b14609032cf328dea48b0803f3e585e223283b3d Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 3 May 2019 10:14:25 -0300 Subject: [PATCH 0410/1145] Avoid the creation of too many strings in 'package' Both when setting a path and searching for a file ('searchpath'), this commit reduces the number of intermediate strings created in Lua. (For setting a path the change is not relevant, because this is done only twice when loading the module. Anyway, it is a nice example of how to use auxlib buffers to manipulate strings in the C API.) --- lgc.h | 2 +- llimits.h | 2 +- loadlib.c | 89 ++++++++++++++++++++++++++++++++----------------- testes/main.lua | 27 ++++++++++----- 4 files changed, 79 insertions(+), 41 deletions(-) diff --git a/lgc.h b/lgc.h index 9ba7ecb050..b972472f8d 100644 --- a/lgc.h +++ b/lgc.h @@ -127,7 +127,7 @@ /* ** some gc parameters are stored divided by 4 to allow a maximum value -** larger than 1000 in a 'lu_byte'. +** up to 1023 in a 'lu_byte'. */ #define getgcparam(p) ((p) * 4) #define setgcparam(p,v) ((p) = (v) / 4) diff --git a/llimits.h b/llimits.h index cc983972ef..febf7555cb 100644 --- a/llimits.h +++ b/llimits.h @@ -39,7 +39,7 @@ typedef signed char ls_byte; /* maximum value for size_t */ #define MAX_SIZET ((size_t)(~(size_t)0)) -/* maximum size visible for Lua (must be representable in a lua_Integer */ +/* maximum size visible for Lua (must be representable in a lua_Integer) */ #define MAX_SIZE (sizeof(size_t) < sizeof(lua_Integer) ? MAX_SIZET \ : (size_t)(LUA_MAXINTEGER)) diff --git a/loadlib.c b/loadlib.c index 4cf9aec31f..ff73a4599f 100644 --- a/loadlib.c +++ b/loadlib.c @@ -290,22 +290,33 @@ static int noenv (lua_State *L) { static void setpath (lua_State *L, const char *fieldname, const char *envname, const char *dft) { + const char *dftmark; const char *nver = lua_pushfstring(L, "%s%s", envname, LUA_VERSUFFIX); - const char *path = getenv(nver); /* use versioned name */ - if (path == NULL) /* no environment variable? */ + const char *path = getenv(nver); /* try versioned name */ + if (path == NULL) /* no versioned environment variable? */ path = getenv(envname); /* try unversioned name */ if (path == NULL || noenv(L)) /* no environment variable? */ lua_pushstring(L, dft); /* use default */ - else { - /* replace ";;" by ";AUXMARK;" and then AUXMARK by default path */ - path = luaL_gsub(L, path, LUA_PATH_SEP LUA_PATH_SEP, - LUA_PATH_SEP AUXMARK LUA_PATH_SEP); - luaL_gsub(L, path, AUXMARK, dft); - lua_remove(L, -2); /* remove result from 1st 'gsub' */ + else if ((dftmark = strstr(path, LUA_PATH_SEP LUA_PATH_SEP)) == NULL) + lua_pushstring(L, path); /* nothing to change */ + else { /* path contains a ";;": insert default path in its place */ + size_t len = strlen(path); + luaL_Buffer b; + luaL_buffinit(L, &b); + if (path < dftmark) { /* is there a prefix before ';;'? */ + luaL_addlstring(&b, path, dftmark - path); /* add it */ + luaL_addchar(&b, *LUA_PATH_SEP); + } + luaL_addstring(&b, dft); /* add default */ + if (dftmark < path + len - 2) { /* is there a sufix after ';;'? */ + luaL_addchar(&b, *LUA_PATH_SEP); + luaL_addlstring(&b, dftmark + 2, (path + len - 2) - dftmark); + } + luaL_pushresult(&b); } setprogdir(L); lua_setfield(L, -3, fieldname); /* package[fieldname] = path value */ - lua_pop(L, 1); /* pop versioned variable name */ + lua_pop(L, 1); /* pop versioned variable name ('nver') */ } /* }================================================================== */ @@ -421,17 +432,26 @@ static int readable (const char *filename) { } -static const char *pushnextfilename (lua_State *L, const char *path) { - const char *l; - if (*path == *LUA_PATH_SEP) - path++; /* skip separator */ - if (*path == '\0') +/* +** Get the next name in '*path' = 'name1;name2;name3;...', changing +** the ending ';' to '\0' to create a zero-terminated string. Return +** NULL when list ends. +*/ +static const char *getnextfilename (char **path, char *end) { + char *sep; + char *name = *path; + if (name == end) return NULL; /* no more names */ - l = strchr(path, *LUA_PATH_SEP); /* find next separator */ - if (l == NULL) /* no more separators? */ - l = path + strlen(path); /* go until the end */ - lua_pushlstring(L, path, l - path); /* file name */ - return l; /* rest of the path */ + else if (*name == '\0') { /* from previous iteration? */ + *name = *LUA_PATH_SEP; /* restore separator */ + name++; /* skip it */ + } + sep = strchr(name, *LUA_PATH_SEP); /* find next separator */ + if (sep == NULL) /* separator not found? */ + sep = end; /* name goes until the end */ + *sep = '\0'; /* finish file name */ + *path = sep; /* will start next search from here */ + return name; } @@ -442,12 +462,12 @@ static const char *pushnextfilename (lua_State *L, const char *path) { ** no file 'blublu.so' */ static void pusherrornotfound (lua_State *L, const char *path) { - if (*path == *LUA_PATH_SEP) - path++; /* skip separator */ - lua_pushstring(L, "\n\tno file '"); - luaL_gsub(L, path, LUA_PATH_SEP, "'\n\tno file '"); - lua_pushstring(L, "'"); - lua_concat(L, 3); + luaL_Buffer b; + luaL_buffinit(L, &b); + luaL_addstring(&b, "\n\tno file '"); + luaL_addgsub(&b, path, LUA_PATH_SEP, "'\n\tno file '"); + luaL_addstring(&b, "'"); + luaL_pushresult(&b); } @@ -455,17 +475,24 @@ static const char *searchpath (lua_State *L, const char *name, const char *path, const char *sep, const char *dirsep) { + luaL_Buffer buff; + char *pathname; /* path with name inserted */ + char *endpathname; /* its end */ + const char *filename; /* separator is non-empty and appears in 'name'? */ if (*sep != '\0' && strchr(name, *sep) != NULL) name = luaL_gsub(L, name, sep, dirsep); /* replace it by 'dirsep' */ - /* replace marks ('?') in 'path' by the file name */ - path = luaL_gsub(L, path, LUA_PATH_MARK, name); - while ((path = pushnextfilename(L, path)) != NULL) { - const char *filename = lua_tostring(L, -1); + luaL_buffinit(L, &buff); + /* add path to the buffer, replacing marks ('?') with the file name */ + luaL_addgsub(&buff, path, LUA_PATH_MARK, name); + luaL_addchar(&buff, '\0'); + pathname = luaL_buffaddr(&buff); /* writable list of file names */ + endpathname = pathname + luaL_bufflen(&buff) - 1; + while ((filename = getnextfilename(&pathname, endpathname)) != NULL) { if (readable(filename)) /* does file exist and is readable? */ - return filename; /* return that file name */ - lua_pop(L, 1); /* else remove file name */ + return lua_pushstring(L, filename); /* save and return name */ } + luaL_pushresult(&buff); /* push path to create error message */ pusherrornotfound(L, lua_tostring(L, -1)); /* create error message */ return NULL; /* not found */ } diff --git a/testes/main.lua b/testes/main.lua index b9dcab1c12..aab490c884 100644 --- a/testes/main.lua +++ b/testes/main.lua @@ -142,12 +142,18 @@ do prepfile("print(package.path, package.cpath)") RUN('env LUA_INIT="error(10)" LUA_PATH=xxx LUA_CPATH=xxx lua -E %s > %s', prog, out) + local output = getoutput() + defaultpath = string.match(output, "^(.-)\t") + defaultCpath = string.match(output, "\t(.-)$") + + -- running with an empty environment + RUN('env -i lua %s > %s', prog, out) local out = getoutput() - defaultpath = string.match(out, "^(.-)\t") - defaultCpath = string.match(out, "\t(.-)$") + assert(defaultpath == string.match(output, "^(.-)\t")) + assert(defaultCpath == string.match(output, "\t(.-)$")) end --- paths did not changed +-- paths did not change assert(not string.find(defaultpath, "xxx") and string.find(defaultpath, "lua") and not string.find(defaultCpath, "xxx") and @@ -160,15 +166,20 @@ local function convert (p) RUN('env LUA_PATH="%s" lua %s > %s', p, prog, out) local expected = getoutput() expected = string.sub(expected, 1, -2) -- cut final end of line - assert(string.gsub(p, ";;", ";"..defaultpath..";") == expected) + if string.find(p, ";;") then + p = string.gsub(p, ";;", ";"..defaultpath..";") + p = string.gsub(p, "^;", "") -- remove ';' at the beginning + p = string.gsub(p, ";$", "") -- remove ';' at the end + end + assert(p == expected) end convert(";") convert(";;") -convert(";;;") -convert(";;;;") -convert(";;;;;") -convert(";;a;;;bc") +convert("a;;b") +convert(";;b") +convert("a;;") +convert("a;b;;c") -- test -l over multiple libraries From 7c5786479c1d617ec7c133f2c2b955726436267a Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 3 May 2019 10:18:44 -0300 Subject: [PATCH 0411/1145] A few more improvements in 'luaO_pushvfstring' - 'L' added to the 'BuffFS' structure - '%c' does not handle control characters (it is not its business. This now is done by the lexer, who is the one in charge of that kind of errors.) - avoid the direct use of 'l_sprintf' in the Lua kernel --- llex.c | 5 +++- lobject.c | 66 ++++++++++++++++++++++++++-------------------- luaconf.h | 13 ++++++--- testes/strings.lua | 3 +-- 4 files changed, 52 insertions(+), 35 deletions(-) diff --git a/llex.c b/llex.c index bb81cec430..226127f69a 100644 --- a/llex.c +++ b/llex.c @@ -82,7 +82,10 @@ void luaX_init (lua_State *L) { const char *luaX_token2str (LexState *ls, int token) { if (token < FIRST_RESERVED) { /* single-byte symbols? */ lua_assert(token == cast_uchar(token)); - return luaO_pushfstring(ls->L, "'%c'", token); + if (lisprint(token)) + return luaO_pushfstring(ls->L, "'%c'", token); + else /* control character */ + return luaO_pushfstring(ls->L, "'<\\%d>'", token); } else { const char *s = luaX_tokens[token - FIRST_RESERVED]; diff --git a/lobject.c b/lobject.c index 58eecd4a36..ce14059f47 100644 --- a/lobject.c +++ b/lobject.c @@ -392,11 +392,20 @@ void luaO_tostring (lua_State *L, TValue *obj) { } + + +/* +** {================================================================== +** 'luaO_pushvfstring' +** =================================================================== +*/ + /* size for buffer space used by 'luaO_pushvfstring' */ #define BUFVFS 400 /* buffer used by 'luaO_pushvfstring' */ typedef struct BuffFS { + lua_State *L; int pushed; /* number of string pieces already on the stack */ int blen; /* length of partial string in 'space' */ char space[BUFVFS]; /* holds last part of the result */ @@ -407,7 +416,8 @@ typedef struct BuffFS { ** Push given string to the stack, as part of the buffer. If the stack ** is almost full, join all partial strings in the stack into one. */ -static void pushstr (lua_State *L, BuffFS *buff, const char *str, size_t l) { +static void pushstr (BuffFS *buff, const char *str, size_t l) { + lua_State *L = buff->L; setsvalue2s(L, L->top, luaS_newlstr(L, str, l)); L->top++; buff->pushed++; @@ -421,8 +431,8 @@ static void pushstr (lua_State *L, BuffFS *buff, const char *str, size_t l) { /* ** empty the buffer space into the stack */ -static void clearbuff (lua_State *L, BuffFS *buff) { - pushstr(L, buff, buff->space, buff->blen); /* push buffer contents */ +static void clearbuff (BuffFS *buff) { + pushstr(buff, buff->space, buff->blen); /* push buffer contents */ buff->blen = 0; /* space now is empty */ } @@ -431,10 +441,10 @@ static void clearbuff (lua_State *L, BuffFS *buff) { ** Get a space of size 'sz' in the buffer. If buffer has not enough ** space, empty it. 'sz' must fit in an empty space. */ -static char *getbuff (lua_State *L, BuffFS *buff, size_t sz) { +static char *getbuff (BuffFS *buff, size_t sz) { lua_assert(buff->blen <= BUFVFS); lua_assert(sz <= BUFVFS); if (sz > BUFVFS - cast_sizet(buff->blen)) /* string does not fit? */ - clearbuff(L, buff); + clearbuff(buff); return buff->space + buff->blen; } @@ -446,16 +456,15 @@ static char *getbuff (lua_State *L, BuffFS *buff, size_t sz) { ** Add 'str' to the buffer. If string is larger than the buffer space, ** push the string directly to the stack. */ -static void addstr2buff (lua_State *L, BuffFS *buff, const char *str, - size_t slen) { +static void addstr2buff (BuffFS *buff, const char *str, size_t slen) { if (slen <= BUFVFS) { /* does string fit into buffer? */ - char *bf = getbuff(L, buff, slen); + char *bf = getbuff(buff, slen); memcpy(bf, str, slen); /* add string to buffer */ addsize(buff, slen); } else { /* string larger than buffer */ - clearbuff(L, buff); /* string comes after buffer's content */ - pushstr(L, buff, str, slen); /* push string */ + clearbuff(buff); /* string comes after buffer's content */ + pushstr(buff, str, slen); /* push string */ } } @@ -463,8 +472,8 @@ static void addstr2buff (lua_State *L, BuffFS *buff, const char *str, /* ** Add a number to the buffer. */ -static void addnum2buff (lua_State *L, BuffFS *buff, TValue *num) { - char *numbuff = getbuff(L, buff, MAXNUMBER2STR); +static void addnum2buff (BuffFS *buff, TValue *num) { + char *numbuff = getbuff(buff, MAXNUMBER2STR); size_t len = tostringbuff(num, numbuff); /* format number into 'numbuff' */ addsize(buff, len); } @@ -478,58 +487,55 @@ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) { BuffFS buff; /* holds last part of the result */ const char *e; /* points to next '%' */ buff.pushed = buff.blen = 0; + buff.L = L; while ((e = strchr(fmt, '%')) != NULL) { - addstr2buff(L, &buff, fmt, e - fmt); /* add 'fmt' up to '%' */ + addstr2buff(&buff, fmt, e - fmt); /* add 'fmt' up to '%' */ switch (*(e + 1)) { /* conversion specifier */ case 's': { /* zero-terminated string */ const char *s = va_arg(argp, char *); if (s == NULL) s = "(null)"; - addstr2buff(L, &buff, s, strlen(s)); + addstr2buff(&buff, s, strlen(s)); break; } case 'c': { /* an 'int' as a character */ - /* if non-printable character, print its code */ - char *bf = getbuff(L, &buff, 10); - int c = va_arg(argp, int); - int len = (lisprint(c)) ? l_sprintf(bf, 10, "%c", c) - : l_sprintf(bf, 10, "<\\%u>", c); - addsize(&buff, len); + char c = cast_uchar(va_arg(argp, int)); + addstr2buff(&buff, &c, sizeof(char)); break; } case 'd': { /* an 'int' */ TValue num; setivalue(&num, va_arg(argp, int)); - addnum2buff(L, &buff, &num); + addnum2buff(&buff, &num); break; } case 'I': { /* a 'lua_Integer' */ TValue num; setivalue(&num, cast(lua_Integer, va_arg(argp, l_uacInt))); - addnum2buff(L, &buff, &num); + addnum2buff(&buff, &num); break; } case 'f': { /* a 'lua_Number' */ TValue num; setfltvalue(&num, cast_num(va_arg(argp, l_uacNumber))); - addnum2buff(L, &buff, &num); + addnum2buff(&buff, &num); break; } case 'p': { /* a pointer */ const int sz = 3 * sizeof(void*) + 8; /* enough space for '%p' */ - char *bf = getbuff(L, &buff, sz); + char *bf = getbuff(&buff, sz); void *p = va_arg(argp, void *); - int len = l_sprintf(bf, sz, "%p", p); + int len = lua_pointer2str(bf, sz, p); addsize(&buff, len); break; } case 'U': { /* a 'long' as a UTF-8 sequence */ char bf[UTF8BUFFSZ]; int len = luaO_utf8esc(bf, va_arg(argp, long)); - addstr2buff(L, &buff, bf + UTF8BUFFSZ - len, len); + addstr2buff(&buff, bf + UTF8BUFFSZ - len, len); break; } case '%': { - addstr2buff(L, &buff, "%", 1); + addstr2buff(&buff, "%", 1); break; } default: { @@ -539,8 +545,8 @@ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) { } fmt = e + 2; /* skip '%' and the specifier */ } - addstr2buff(L, &buff, fmt, strlen(fmt)); /* rest of 'fmt' */ - clearbuff(L, &buff); /* empty buffer into the stack */ + addstr2buff(&buff, fmt, strlen(fmt)); /* rest of 'fmt' */ + clearbuff(&buff); /* empty buffer into the stack */ if (buff.pushed > 1) luaV_concat(L, buff.pushed); /* join all partial results */ return svalue(s2v(L->top - 1)); @@ -556,6 +562,8 @@ const char *luaO_pushfstring (lua_State *L, const char *fmt, ...) { return msg; } +/* }================================================================== */ + #define RETS "..." #define PRE "[string \"" diff --git a/luaconf.h b/luaconf.h index 019f2eb68a..9240d9c80e 100644 --- a/luaconf.h +++ b/luaconf.h @@ -376,7 +376,7 @@ @@ lua_number2str converts a float to a string. @@ l_mathop allows the addition of an 'l' or 'f' to all math operations. @@ l_floor takes the floor of a float. -@@ lua_str2number converts a decimal numeric string to a number. +@@ lua_str2number converts a decimal numeral to a number. */ @@ -568,7 +568,7 @@ /* -@@ lua_strx2number converts a hexadecimal numeric string to a number. +@@ lua_strx2number converts a hexadecimal numeral to a number. ** In C99, 'strtod' does that conversion. Otherwise, you can ** leave 'lua_strx2number' undefined and Lua will provide its own ** implementation. @@ -579,7 +579,14 @@ /* -@@ lua_number2strx converts a float to a hexadecimal numeric string. +@@ lua_pointer2str converts a pointer to a readable string in a +** non-specified way. +*/ +#define lua_pointer2str(buff,sz,p) l_sprintf(buff,sz,"%p",p) + + +/* +@@ lua_number2strx converts a float to a hexadecimal numeral. ** In C99, 'sprintf' (with format specifiers '%a'/'%A') does that. ** Otherwise, you can leave 'lua_number2strx' undefined and Lua will ** provide its own implementation. diff --git a/testes/strings.lua b/testes/strings.lua index bc123d1ab4..3e32f2c476 100644 --- a/testes/strings.lua +++ b/testes/strings.lua @@ -453,8 +453,7 @@ else str = "abc %c def" testpfs("I", str, string.byte("A")) - -- non-printable character - assert(callpfs("I", str, 255) == "abc <\\255> def") + testpfs("I", str, 255) str = string.rep("a", blen - 1) .. "%p" .. string.rep("cd", blen) testpfs("P", str, {}) From 01bded3d8cd88a2d7f472b45f706565f1a9ef3b1 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 3 May 2019 10:36:19 -0300 Subject: [PATCH 0412/1145] File 'lib2-v2.so' generated from its own source Instead of being a copy of 'lib2.so', 'lib2-v2.so' has its own source file ('lib22.c'), so that the test can distinguish both libraries. --- testes/attrib.lua | 2 +- testes/libs/lib22.c | 25 +++++++++++++++++++++++++ testes/libs/makefile | 4 ++-- 3 files changed, 28 insertions(+), 3 deletions(-) create mode 100644 testes/libs/lib22.c diff --git a/testes/attrib.lua b/testes/attrib.lua index 4adb42e0d2..b1a4a1994e 100644 --- a/testes/attrib.lua +++ b/testes/attrib.lua @@ -275,7 +275,7 @@ else -- check correct access to global environment and correct -- parameters assert(_ENV.x == "lib2-v2" and _ENV.y == DC"lib2-v2") - assert(lib2.id("x") == "x") + assert(lib2.id("x") == true) -- a different "id" implementation -- test C submodules local fs, ext = require"lib1.sub" diff --git a/testes/libs/lib22.c b/testes/libs/lib22.c new file mode 100644 index 0000000000..8e6565022e --- /dev/null +++ b/testes/libs/lib22.c @@ -0,0 +1,25 @@ +#include "lua.h" +#include "lauxlib.h" + +static int id (lua_State *L) { + lua_pushboolean(L, 1); + lua_insert(L, 1); + return lua_gettop(L); +} + + +static const struct luaL_Reg funcs[] = { + {"id", id}, + {NULL, NULL} +}; + + +LUAMOD_API int luaopen_lib2 (lua_State *L) { + lua_settop(L, 2); + lua_setglobal(L, "y"); /* y gets 2nd parameter */ + lua_setglobal(L, "x"); /* x gets 1st parameter */ + luaL_newlib(L, funcs); + return 1; +} + + diff --git a/testes/libs/makefile b/testes/libs/makefile index acff4848c2..698f8984f5 100644 --- a/testes/libs/makefile +++ b/testes/libs/makefile @@ -23,5 +23,5 @@ lib2.so: lib2.c $(LUA_DIR)/luaconf.h $(LUA_DIR)/ltests.h lib21.so: lib21.c $(LUA_DIR)/luaconf.h $(LUA_DIR)/ltests.h $(CC) $(CFLAGS) -o lib21.so lib21.c -lib2-v2.so: lib2.so - cp lib2.so ./lib2-v2.so +lib2-v2.so: lib21.c $(LUA_DIR)/luaconf.h $(LUA_DIR)/ltests.h + $(CC) $(CFLAGS) -o lib2-v2.so lib22.c From 389116d8abcc96db3cfe2f3cc25789c089fe12d6 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 9 May 2019 11:13:45 -0300 Subject: [PATCH 0413/1145] Coroutines do not unwind the stack in case of errors Back to how it was, a coroutine does not unwind its stack in case of errors (and therefore do not close its to-be-closed variables). This allows the stack to be examined after the error. The program can use 'coroutine.kill' to close the variables. The function created by 'coroutine.wrap', however, closes the coroutine's variables in case of errors, as it is impossible to examine the stack any way. --- lcorolib.c | 12 +++++++++++- ldo.c | 4 +--- manual/manual.of | 33 +++++++++++++++++++++++++-------- testes/coroutine.lua | 8 ++++++-- testes/db.lua | 14 ++++++++++---- testes/locals.lua | 35 +++++++++++++++++++++++++++-------- 6 files changed, 80 insertions(+), 26 deletions(-) diff --git a/lcorolib.c b/lcorolib.c index f7c9e165f5..a21880d5e0 100644 --- a/lcorolib.c +++ b/lcorolib.c @@ -25,6 +25,10 @@ static lua_State *getco (lua_State *L) { } +/* +** Resumes a coroutine. Returns the number of results for non-error +** cases or -1 for errors. +*/ static int auxresume (lua_State *L, lua_State *co, int narg) { int status, nres; if (!lua_checkstack(co, narg)) { @@ -74,8 +78,14 @@ static int luaB_auxwrap (lua_State *L) { lua_State *co = lua_tothread(L, lua_upvalueindex(1)); int r = auxresume(L, co, lua_gettop(L)); if (r < 0) { + int stat = lua_status(co); + if (stat != LUA_OK && stat != LUA_YIELD) { + stat = lua_resetthread(co); /* close variables in case of errors */ + if (stat != LUA_OK) /* error closing variables? */ + lua_xmove(co, L, 1); /* get new error object */ + } if (lua_type(L, -1) == LUA_TSTRING) { /* error object is a string? */ - luaL_where(L, 1); /* add extra info */ + luaL_where(L, 1); /* add extra info, if available */ lua_insert(L, -2); lua_concat(L, 2); } diff --git a/ldo.c b/ldo.c index 2a98c3977a..d474c0a02c 100644 --- a/ldo.c +++ b/ldo.c @@ -686,10 +686,8 @@ LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs, if (likely(!errorstatus(status))) lua_assert(status == L->status); /* normal end or yield */ else { /* unrecoverable error */ - status = luaF_close(L, L->stack, status); /* close all upvalues */ L->status = cast_byte(status); /* mark thread as 'dead' */ - luaD_seterrorobj(L, status, L->stack + 1); /* push error message */ - L->ci = &L->base_ci; /* back to the original C level */ + luaD_seterrorobj(L, status, L->top); /* push error message */ L->ci->top = L->top; } *nresults = (status == LUA_YIELD) ? L->ci->u2.nyield diff --git a/manual/manual.of b/manual/manual.of index 5f26570893..cf44b4f2ec 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -286,7 +286,7 @@ Lua also offers a system of @emph{warnings} @seeF{warn}. Unlike errors, warnings do not interfere in any way with program execution. They typically only generate a message to the user, -although this behavior can be adapted from C @see{lua_setwarnf}. +although this behavior can be adapted from C @seeC{lua_setwarnf}. } @@ -835,6 +835,9 @@ In case of normal termination, plus any values returned by the coroutine main function. In case of errors, @Lid{coroutine.resume} returns @false plus the error object. +In this case, the coroutine does not unwind its stack, +so that it is possible to inspect it after the error +with the debug API. A coroutine yields by calling @Lid{coroutine.yield}. When a coroutine yields, @@ -858,8 +861,10 @@ go as extra arguments to @Lid{coroutine.resume}. @Lid{coroutine.wrap} returns all the values returned by @Lid{coroutine.resume}, except the first one (the boolean error code). Unlike @Lid{coroutine.resume}, -@Lid{coroutine.wrap} does not catch errors; -any error is propagated to the caller. +the function created by @Lid{coroutine.wrap} +propagates any error to the caller. +In this case, +the function also kills the coroutine @seeF{coroutine.kill}. As an example of how coroutines work, consider the following code: @@ -1534,8 +1539,15 @@ the other pending closing methods will still be called. If a coroutine yields inside a block and is never resumed again, the variables visible at that block will never go out of scope, and therefore they will not be closed. -(You should use finalizers to handle this case, -or else call @Lid{coroutine.kill} to close the variables.) +Similarly, if a coroutine ends with an error, +it does not unwind its stack, +so it does not close any variable. +You should either use finalizers +or call @Lid{coroutine.kill} to close the variables in these cases. +However, note that if the coroutine was created +through @Lid{coroutine.wrap}, +then its corresponding function will close all variables +in case of errors. } @@ -6406,11 +6418,12 @@ or if it has stopped with an error. Creates a new coroutine, with body @id{f}; @id{f} must be a function. Returns a function that resumes the coroutine each time it is called. -Any arguments passed to the function behave as the +Any arguments passed to this function behave as the extra arguments to @id{resume}. -Returns the same values returned by @id{resume}, +The function returns the same values returned by @id{resume}, except the first boolean. -In case of error, propagates the error. +In case of error, +the function kills the coroutine and propagates the error. } @@ -6668,6 +6681,10 @@ the file path where the module was found, as returned by @Lid{package.searchpath}. The first searcher always returns the string @St{:preload:}. +Searchers should raise no errors and have no side effects in Lua. +(They may have side effects in C, +for instance by linking the application with a library.) + } @LibEntry{package.searchpath (name, path [, sep [, rep]])| diff --git a/testes/coroutine.lua b/testes/coroutine.lua index 35ff27fb7f..9dd501e7fb 100644 --- a/testes/coroutine.lua +++ b/testes/coroutine.lua @@ -346,9 +346,13 @@ do local st, res = coroutine.resume(B) assert(st == true and res == false) - A = coroutine.wrap(function() return pcall(A, 1) end) + local X = false + A = coroutine.wrap(function() + local *toclose _ = setmetatable({}, {__close = function () X = true end}) + return pcall(A, 1) + end) st, res = A() - assert(not st and string.find(res, "non%-suspended")) + assert(not st and string.find(res, "non%-suspended") and X == true) end diff --git a/testes/db.lua b/testes/db.lua index 95275fb41d..3d94f77612 100644 --- a/testes/db.lua +++ b/testes/db.lua @@ -734,18 +734,24 @@ a, b = coroutine.resume(co, 100) assert(a and b == 30) --- check traceback of suspended coroutines +-- check traceback of suspended (or dead with error) coroutines + +function f(i) + if i == 0 then error(i) + else coroutine.yield(); f(i-1) + end +end -function f(i) coroutine.yield(i == 0); f(i - 1) end co = coroutine.create(function (x) f(x) end) a, b = coroutine.resume(co, 3) t = {"'coroutine.yield'", "'f'", "in function <"} -repeat +while coroutine.status(co) == "suspended" do checktraceback(co, t) a, b = coroutine.resume(co) table.insert(t, 2, "'f'") -- one more recursive call to 'f' -until b +end +t[1] = "'error'" checktraceback(co, t) diff --git a/testes/locals.lua b/testes/locals.lua index de47ae3182..814d1b165c 100644 --- a/testes/locals.lua +++ b/testes/locals.lua @@ -417,12 +417,13 @@ if rawget(_G, "T") then end --- to-be-closed variables in coroutines +print "to-be-closed variables in coroutines" + do - -- an error in a coroutine closes variables + -- an error in a wrapped coroutine closes variables local x = false local y = false - local co = coroutine.create(function () + local co = coroutine.wrap(function () local *toclose xv = func2close(function () x = true end) do local *toclose yv = func2close(function () y = true end) @@ -432,14 +433,31 @@ do error(23) -- error does end) - local a, b = coroutine.resume(co) - assert(a and b == 100 and not x and not y) - a, b = coroutine.resume(co) - assert(a and b == 200 and not x and y) - a, b = coroutine.resume(co) + local b = co() + assert(b == 100 and not x and not y) + b = co() + assert(b == 200 and not x and y) + local a, b = pcall(co) assert(not a and b == 23 and x and y) end + +do + -- error in a wrapped coroutine raising errors when closing a variable + local x = false + local co = coroutine.wrap(function () + local *toclose xv = func2close(function () error("XXX") end) + coroutine.yield(100) + error(200) + end) + assert(co() == 100) + local st, msg = pcall(co) +print(msg) + -- should get last error raised + assert(not st and string.find(msg, "%w+%.%w+:%d+: XXX")) +end + + -- a suspended coroutine should not close its variables when collected local co co = coroutine.wrap(function() @@ -449,6 +467,7 @@ co = coroutine.wrap(function() end) co() -- start coroutine assert(co == nil) -- eventually it will be collected +collectgarbage() -- to-be-closed variables in generic for loops From 3f253f116e8e292977a1bded964544fb35b3d1e3 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 9 May 2019 11:32:20 -0300 Subject: [PATCH 0414/1145] Test for dead coroutine moved to 'lua_resume' The test for dead coroutines done in the 'coro' library was moved to 'lua_resume', in the kernel, which already does other similar tests. --- lcorolib.c | 4 ---- ldo.c | 2 ++ 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/lcorolib.c b/lcorolib.c index a21880d5e0..3fc9fb1c1f 100644 --- a/lcorolib.c +++ b/lcorolib.c @@ -35,10 +35,6 @@ static int auxresume (lua_State *L, lua_State *co, int narg) { lua_pushliteral(L, "too many arguments to resume"); return -1; /* error flag */ } - if (lua_status(co) == LUA_OK && lua_gettop(co) == 0) { - lua_pushliteral(L, "cannot resume dead coroutine"); - return -1; /* error flag */ - } lua_xmove(L, co, narg); status = lua_resume(co, L, narg, &nres); if (status == LUA_OK || status == LUA_YIELD) { diff --git a/ldo.c b/ldo.c index d474c0a02c..e9a88e9e50 100644 --- a/ldo.c +++ b/ldo.c @@ -666,6 +666,8 @@ LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs, if (L->status == LUA_OK) { /* may be starting a coroutine */ if (L->ci != &L->base_ci) /* not in base level? */ return resume_error(L, "cannot resume non-suspended coroutine", nargs); + else if (L->top - (L->ci->func + 1) == nargs) /* no function? */ + return resume_error(L, "cannot resume dead coroutine", nargs); } else if (L->status != LUA_YIELD) return resume_error(L, "cannot resume dead coroutine", nargs); From d881325c2fcbb6d2c434ec403b0bbe51ac200c7b Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 9 May 2019 12:10:31 -0300 Subject: [PATCH 0415/1145] Flag for to-be-closed variables changed to '' The flag for to-be-closed variables was changed from '*toclose' to ''. Several people found confusing the old syntax and the new one has a clear terminator, making it more flexible for future changes. --- lparser.c | 3 ++- manual/manual.of | 4 ++-- testes/api.lua | 3 ++- testes/coroutine.lua | 6 +++--- testes/files.lua | 6 +++--- testes/goto.lua | 2 +- testes/locals.lua | 50 ++++++++++++++++++++++---------------------- testes/main.lua | 4 ++-- 8 files changed, 40 insertions(+), 38 deletions(-) diff --git a/lparser.c b/lparser.c index 4c2ddbfe50..4e6c27fe00 100644 --- a/lparser.c +++ b/lparser.c @@ -1621,6 +1621,7 @@ static void tocloselocalstat (LexState *ls) { if (strcmp(getstr(attr), "toclose") != 0) luaK_semerror(ls, luaO_pushfstring(ls->L, "unknown attribute '%s'", getstr(attr))); + testnext(ls, '>'); new_localvar(ls, str_checkname(ls)); checknext(ls, '='); exp1(ls); @@ -1634,7 +1635,7 @@ static void tocloselocalstat (LexState *ls) { static void localstat (LexState *ls) { /* stat -> LOCAL NAME {',' NAME} ['=' explist] | LOCAL *toclose NAME '=' exp */ - if (testnext(ls, '*')) + if (testnext(ls, '<')) tocloselocalstat(ls); else commonlocalstat(ls); diff --git a/manual/manual.of b/manual/manual.of index cf44b4f2ec..54a0787927 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -1509,7 +1509,7 @@ A local variable can be declared as a @def{to-be-closed} variable, with the following syntax: @Produc{ @producname{stat}@producbody{ - @Rw{local} @bnfter{*} @bnfter{toclose} Name @bnfter{=} exp + @Rw{local} @bnfter{<} @bnfter{toclose} @bnfter{>} Name @bnfter{=} exp }} A to-be-closed variable behaves like a normal local variable, except that its value is @emph{closed} whenever the variable @@ -8949,7 +8949,7 @@ and @bnfNter{LiteralString}, see @See{lexical}.) @OrNL @Rw{function} funcname funcbody @OrNL @Rw{local} @Rw{function} @bnfNter{Name} funcbody @OrNL @Rw{local} namelist @bnfopt{@bnfter{=} explist} -@OrNL @Rw{local} @bnfter{*} @bnfter{toclose} Name @bnfter{=} exp +@OrNL @Rw{local} @bnfter{<} @bnfter{toclose} @bnfter{>} Name @bnfter{=} exp } @producname{retstat}@producbody{@Rw{return} diff --git a/testes/api.lua b/testes/api.lua index 08672e8abe..bcc04dac54 100644 --- a/testes/api.lua +++ b/testes/api.lua @@ -1137,7 +1137,8 @@ end) testamem("to-be-closed variables", function() local flag do - local *toclose x = setmetatable({}, {__close = function () flag = true end}) + local x = + setmetatable({}, {__close = function () flag = true end}) flag = false local x = {} end diff --git a/testes/coroutine.lua b/testes/coroutine.lua index 9dd501e7fb..db6d074ee1 100644 --- a/testes/coroutine.lua +++ b/testes/coroutine.lua @@ -151,7 +151,7 @@ do end co = coroutine.create(function () - local *toclose x = func2close(function (self, err) + local x = func2close(function (self, err) assert(err == nil); X = false end) X = true @@ -164,7 +164,7 @@ do -- error killing a coroutine co = coroutine.create(function() - local *toclose x = func2close(function (self, err) + local x = func2close(function (self, err) assert(err == nil); error(111) end) coroutine.yield() @@ -348,7 +348,7 @@ do local X = false A = coroutine.wrap(function() - local *toclose _ = setmetatable({}, {__close = function () X = true end}) + local _ = setmetatable({}, {__close = function () X = true end}) return pcall(A, 1) end) st, res = A() diff --git a/testes/files.lua b/testes/files.lua index 38d3a6693b..eb100fe137 100644 --- a/testes/files.lua +++ b/testes/files.lua @@ -125,7 +125,7 @@ do -- closing file by scope local F = nil do - local *toclose f = assert(io.open(file, "w")) + local f = assert(io.open(file, "w")) F = f end assert(tostring(F) == "file (closed)") @@ -135,7 +135,7 @@ assert(os.remove(file)) do -- test writing/reading numbers - local *toclose f = assert(io.open(file, "w")) + local f = assert(io.open(file, "w")) f:write(maxint, '\n') f:write(string.format("0X%x\n", maxint)) f:write("0xABCp-3", '\n') @@ -158,7 +158,7 @@ assert(os.remove(file)) -- testing multiple arguments to io.read do - local *toclose f = assert(io.open(file, "w")) + local f = assert(io.open(file, "w")) f:write[[ a line another line diff --git a/testes/goto.lua b/testes/goto.lua index f3dcfd4a3e..c9e480734d 100644 --- a/testes/goto.lua +++ b/testes/goto.lua @@ -258,7 +258,7 @@ do ::L2:: goto L3 ::L1:: do - local *toclose a = setmetatable({}, {__close = function () X = true end}) + local a = setmetatable({}, {__close = function () X = true end}) assert(X == nil) if a then goto L2 end -- jumping back out of scope of 'a' end diff --git a/testes/locals.lua b/testes/locals.lua index 814d1b165c..c176f50663 100644 --- a/testes/locals.lua +++ b/testes/locals.lua @@ -185,9 +185,9 @@ end do local a = {} do - local *toclose x = setmetatable({"x"}, {__close = function (self) + local x = setmetatable({"x"}, {__close = function (self) a[#a + 1] = self[1] end}) - local *toclose y = func2close(function (self, err) + local y = func2close(function (self, err) assert(err == nil); a[#a + 1] = "y" end) a[#a + 1] = "in" @@ -203,7 +203,7 @@ do -- closing functions do not corrupt returning values local function foo (x) - local *toclose _ = closescope + local _ = closescope return x, X, 23 end @@ -212,7 +212,7 @@ do X = false foo = function (x) - local *toclose _ = closescope + local _ = closescope local y = 15 return y end @@ -221,7 +221,7 @@ do X = false foo = function () - local *toclose x = closescope + local x = closescope return x end @@ -234,13 +234,13 @@ do -- calls cannot be tail in the scope of to-be-closed variables local X, Y local function foo () - local *toclose _ = func2close(function () Y = 10 end) + local _ = func2close(function () Y = 10 end) assert(X == true and Y == nil) -- 'X' not closed yet return 1,2,3 end local function bar () - local *toclose _ = func2close(function () X = false end) + local _ = func2close(function () X = false end) X = true do return foo() -- not a tail call! @@ -255,14 +255,14 @@ end do -- errors in __close local log = {} local function foo (err) - local *toclose x = + local x = func2close(function (self, msg) log[#log + 1] = msg; error(1) end) - local *toclose x1 = + local x1 = func2close(function (self, msg) log[#log + 1] = msg; end) - local *toclose gc = func2close(function () collectgarbage() end) - local *toclose y = + local gc = func2close(function () collectgarbage() end) + local y = func2close(function (self, msg) log[#log + 1] = msg; error(2) end) - local *toclose z = + local z = func2close(function (self, msg) log[#log + 1] = msg or 10; error(3) end) if err then error(4) end end @@ -283,7 +283,7 @@ do -- errors due to non-closable values local function foo () - local *toclose x = 34 + local x = 34 end local stat, msg = pcall(foo) assert(not stat and string.find(msg, "variable 'x'")) @@ -291,8 +291,8 @@ do -- with other errors, non-closable values are ignored local function foo () - local *toclose x = 34 - local *toclose y = func2close(function () error(32) end) + local x = 34 + local y = func2close(function () error(32) end) end local stat, msg = pcall(foo) assert(not stat and msg == 32) @@ -304,8 +304,8 @@ if rawget(_G, "T") then -- memory error inside closing function local function foo () - local *toclose y = func2close(function () T.alloccount() end) - local *toclose x = setmetatable({}, {__close = function () + local y = func2close(function () T.alloccount() end) + local x = setmetatable({}, {__close = function () T.alloccount(0); local x = {} -- force a memory error end}) error("a") -- common error inside the function's body @@ -331,7 +331,7 @@ if rawget(_G, "T") then end local function test () - local *toclose x = enter(0) -- set a memory limit + local x = enter(0) -- set a memory limit -- creation of previous upvalue will raise a memory error assert(false) -- should not run end @@ -346,14 +346,14 @@ if rawget(_G, "T") then -- repeat test with extra closing upvalues local function test () - local *toclose xxx = func2close(function (self, msg) + local xxx = func2close(function (self, msg) assert(msg == "not enough memory"); error(1000) -- raise another error end) - local *toclose xx = func2close(function (self, msg) + local xx = func2close(function (self, msg) assert(msg == "not enough memory"); end) - local *toclose x = enter(0) -- set a memory limit + local x = enter(0) -- set a memory limit -- creation of previous upvalue will raise a memory error os.exit(false) -- should not run end @@ -424,9 +424,9 @@ do local x = false local y = false local co = coroutine.wrap(function () - local *toclose xv = func2close(function () x = true end) + local xv = func2close(function () x = true end) do - local *toclose yv = func2close(function () y = true end) + local yv = func2close(function () y = true end) coroutine.yield(100) -- yield doesn't close variable end coroutine.yield(200) -- yield doesn't close variable @@ -446,7 +446,7 @@ do -- error in a wrapped coroutine raising errors when closing a variable local x = false local co = coroutine.wrap(function () - local *toclose xv = func2close(function () error("XXX") end) + local xv = func2close(function () error("XXX") end) coroutine.yield(100) error(200) end) @@ -461,7 +461,7 @@ end -- a suspended coroutine should not close its variables when collected local co co = coroutine.wrap(function() - local *toclose x = function () os.exit(false) end -- should not run + local x = function () os.exit(false) end -- should not run co = nil coroutine.yield() end) diff --git a/testes/main.lua b/testes/main.lua index aab490c884..47d84d4ced 100644 --- a/testes/main.lua +++ b/testes/main.lua @@ -320,11 +320,11 @@ NoRun("", "lua %s", prog) -- no message -- to-be-closed variables in main chunk prepfile[[ - local *toclose x = function (err) + local x = function (err) assert(err == 120) print("Ok") end - local *toclose e1 = function () error(120) end + local e1 = function () error(120) end os.exit(true, true) ]] RUN('lua %s > %s', prog, out) From 49c42f3615bd876657bf697e3bf040ce796ae238 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 13 May 2019 14:24:10 -0300 Subject: [PATCH 0416/1145] Some improvements in 'luaconf.h' Added '#if !defined' in some definitions to allow external definitions; more comments; other small changes. --- llimits.h | 11 ++++--- luaconf.h | 88 +++++++++++++++++++++++++++++++++++------------------- lutf8lib.c | 2 +- 3 files changed, 65 insertions(+), 36 deletions(-) diff --git a/llimits.h b/llimits.h index febf7555cb..950e7e6402 100644 --- a/llimits.h +++ b/llimits.h @@ -14,6 +14,11 @@ #include "lua.h" + +/* minimum number of bits in an integer */ +#define LUAI_BITSINT (LUAI_IS32INT ? 32 : 16) + + /* ** 'lu_mem' and 'l_mem' are unsigned/signed integers big enough to count ** the total memory used by Lua (in bytes). Usually, 'size_t' and @@ -22,7 +27,7 @@ #if defined(LUAI_MEM) /* { external definitions? */ typedef LUAI_UMEM lu_mem; typedef LUAI_MEM l_mem; -#elif LUAI_BITSINT >= 32 /* }{ */ +#elif LUAI_IS32INT /* }{ */ typedef size_t lu_mem; typedef ptrdiff_t l_mem; #else /* 16-bit ints */ /* }{ */ @@ -172,13 +177,11 @@ typedef LUAI_UACINT l_uacInt; #endif - - /* ** type for virtual-machine instructions; ** must be an unsigned with (at least) 4 bytes (see details in lopcodes.h) */ -#if LUAI_BITSINT >= 32 +#if LUAI_IS32INT typedef unsigned int l_uint32; #else typedef unsigned long l_uint32; diff --git a/luaconf.h b/luaconf.h index 9240d9c80e..4647ba1785 100644 --- a/luaconf.h +++ b/luaconf.h @@ -14,6 +14,16 @@ /* ** =================================================================== +** General Configuration File for Lua +** +** Some definitions here can be changed externally, through the +** compiler (e.g., with '-D' options). Those are protected by +** '#if !defined' guards. However, several other definitions should +** be changed directly here, either because they affect the Lua +** ABI (by making the changes here, you ensure that all software +** connected to Lua, such as C libraries, will be compiled with the +** same configuration); or because they are seldom changed. +** ** Search for "@@" to find all configurable definitions. ** =================================================================== */ @@ -22,8 +32,7 @@ /* ** {==================================================================== ** System Configuration: macros to adapt (if needed) Lua to some -** particular platform, for instance compiling it with 32-bit numbers or -** restricting it to C89. +** particular platform, for instance restricting it to C89. ** ===================================================================== */ @@ -42,15 +51,6 @@ #endif -/* -@@ LUA_32BITS enables Lua with 32-bit integers and 32-bit floats. You -** can also define LUA_32BITS in the make file, but changing here you -** ensure that all software connected to Lua will be compiled with the -** same configuration. -*/ -/* #define LUA_32BITS */ - - /* @@ LUA_USE_C89 controls the use of non-ISO-C89 features. ** Define it if you want Lua to avoid the use of a few C99 features @@ -86,33 +86,42 @@ /* -@@ LUA_C89_NUMBERS ensures that Lua uses the largest types available for -** C89 ('long' and 'double'); Windows always has '__int64', so it does -** not need to use this case. +@@ LUAI_IS32INT is true iff 'int' has (at least) 32 bits. +** (the use of two shifts avoids undefined shifts) */ -#if defined(LUA_USE_C89) && !defined(LUA_USE_WINDOWS) -#define LUA_C89_NUMBERS -#endif +#define LUAI_IS32INT (((UINT_MAX >> 15) >> 15) >= 3) + +/* }================================================================== */ /* -@@ LUAI_BITSINT defines the (minimum) number of bits in an 'int'. +** {================================================================== +** Configuration for Number types. +** =================================================================== */ -/* avoid undefined shifts */ -#if ((INT_MAX >> 15) >> 15) >= 1 -#define LUAI_BITSINT 32 -#else -/* 'int' always must have at least 16 bits */ -#define LUAI_BITSINT 16 + +/* +@@ LUA_32BITS enables Lua with 32-bit integers and 32-bit floats. +*/ +/* #define LUA_32BITS */ + + +/* +@@ LUA_C89_NUMBERS ensures that Lua uses the largest types available for +** C89 ('long' and 'double'); Windows always has '__int64', so it does +** not need to use this case. +*/ +#if defined(LUA_USE_C89) && !defined(LUA_USE_WINDOWS) +#define LUA_C89_NUMBERS #endif /* @@ LUA_INT_TYPE defines the type for Lua integers. @@ LUA_FLOAT_TYPE defines the type for Lua floats. -** Lua should work fine with any mix of these options (if supported -** by your C compiler). The usual configurations are 64-bit integers +** Lua should work fine with any mix of these options supported +** by your C compiler. The usual configurations are 64-bit integers ** and 'double' (the default), 32-bit integers and 'float' (for ** restricted platforms), and 'long'/'double' (for C compilers not ** compliant with C99, which may not have support for 'long long'). @@ -132,7 +141,7 @@ /* ** 32-bit integers and 'float' */ -#if LUAI_BITSINT >= 32 /* use 'int' if big enough */ +#if LUAI_IS32INT /* use 'int' if big enough */ #define LUA_INT_TYPE LUA_INT_INT #else /* otherwise use 'long' */ #define LUA_INT_TYPE LUA_INT_LONG @@ -164,7 +173,6 @@ - /* ** {================================================================== ** Configuration for Paths. @@ -192,6 +200,7 @@ ** hierarchy or if you want to install your libraries in ** non-conventional directories. */ + #define LUA_VDIR LUA_VERSION_MAJOR "." LUA_VERSION_MINOR #if defined(_WIN32) /* { */ /* @@ -201,27 +210,40 @@ #define LUA_LDIR "!\\lua\\" #define LUA_CDIR "!\\" #define LUA_SHRDIR "!\\..\\share\\lua\\" LUA_VDIR "\\" + +#if !defined(LUA_PATH_DEFAULT) #define LUA_PATH_DEFAULT \ LUA_LDIR"?.lua;" LUA_LDIR"?\\init.lua;" \ LUA_CDIR"?.lua;" LUA_CDIR"?\\init.lua;" \ LUA_SHRDIR"?.lua;" LUA_SHRDIR"?\\init.lua;" \ ".\\?.lua;" ".\\?\\init.lua" +#endif + +#if !defined(LUA_CPATH_DEFAULT) #define LUA_CPATH_DEFAULT \ LUA_CDIR"?.dll;" \ LUA_CDIR"..\\lib\\lua\\" LUA_VDIR "\\?.dll;" \ LUA_CDIR"loadall.dll;" ".\\?.dll" +#endif #else /* }{ */ #define LUA_ROOT "/usr/local/" #define LUA_LDIR LUA_ROOT "share/lua/" LUA_VDIR "/" #define LUA_CDIR LUA_ROOT "lib/lua/" LUA_VDIR "/" + +#if !defined(LUA_PATH_DEFAULT) #define LUA_PATH_DEFAULT \ LUA_LDIR"?.lua;" LUA_LDIR"?/init.lua;" \ LUA_CDIR"?.lua;" LUA_CDIR"?/init.lua;" \ "./?.lua;" "./?/init.lua" +#endif + +#if !defined(LUA_CPATH_DEFAULT) #define LUA_CPATH_DEFAULT \ LUA_CDIR"?.so;" LUA_CDIR"loadall.so;" "./?.so" +#endif + #endif /* } */ @@ -230,12 +252,16 @@ ** CHANGE it if your machine does not use "/" as the directory separator ** and is not Windows. (On Windows Lua automatically uses "\".) */ +#if !defined(LUA_DIRSEP) + #if defined(_WIN32) #define LUA_DIRSEP "\\" #else #define LUA_DIRSEP "/" #endif +#endif + /* }================================================================== */ @@ -632,7 +658,7 @@ /* @@ lua_getlocaledecpoint gets the locale "radix character" (decimal point). ** Change that if you do not want to use C locales. (Code using this -** macro must include header 'locale.h'.) +** macro must include the header 'locale.h'.) */ #if !defined(lua_getlocaledecpoint) #define lua_getlocaledecpoint() (localeconv()->decimal_point[0]) @@ -673,7 +699,7 @@ ** {================================================================== ** Macros that affect the API and must be stable (that is, must be the ** same when you compile Lua and when you compile code that links to -** Lua). You probably do not want/need to change them. +** Lua). ** ===================================================================== */ @@ -684,7 +710,7 @@ ** space (and to reserve some numbers for pseudo-indices). ** (It must fit into max(size_t)/32.) */ -#if LUAI_BITSINT >= 32 +#if LUAI_IS32INT #define LUAI_MAXSTACK 1000000 #else #define LUAI_MAXSTACK 15000 diff --git a/lutf8lib.c b/lutf8lib.c index 16cd68ad68..9786b60ce0 100644 --- a/lutf8lib.c +++ b/lutf8lib.c @@ -28,7 +28,7 @@ /* ** Integer type for decoded UTF-8 values; MAXUTF needs 31 bits. */ -#if LUAI_BITSINT >= 31 +#if ((UINT_MAX >> 15) >> 15) >= 1 typedef unsigned int utfint; #else typedef unsigned long utfint; From 279c3a6961c60252f0368fdea889caf977f85fe0 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 13 May 2019 16:17:21 -0300 Subject: [PATCH 0417/1145] A few changes in tests about number of bits in integers - The preprocessor must work with at least 'long', and therefore must do shifts of up to 31 bits correctly. - Whenever possible, use unsigned types in shifts. --- llimits.h | 4 ---- lmathlib.c | 8 ++++---- lopcodes.h | 16 +++++++++++----- ltable.c | 4 ++-- luaconf.h | 3 +-- lutf8lib.c | 2 +- 6 files changed, 19 insertions(+), 18 deletions(-) diff --git a/llimits.h b/llimits.h index 950e7e6402..2b52c83bde 100644 --- a/llimits.h +++ b/llimits.h @@ -15,10 +15,6 @@ #include "lua.h" -/* minimum number of bits in an integer */ -#define LUAI_BITSINT (LUAI_IS32INT ? 32 : 16) - - /* ** 'lu_mem' and 'l_mem' are unsigned/signed integers big enough to count ** the total memory used by Lua (in bytes). Usually, 'size_t' and diff --git a/lmathlib.c b/lmathlib.c index e3ccc3ee58..3454c41fe1 100644 --- a/lmathlib.c +++ b/lmathlib.c @@ -266,7 +266,7 @@ static int math_type (lua_State *L) { /* try to find an integer type with at least 64 bits */ -#if (LONG_MAX >> 31 >> 31) >= 1 +#if (ULONG_MAX >> 31 >> 31) >= 3 /* 'long' has at least 64 bits */ #define Rand64 unsigned long @@ -276,7 +276,7 @@ static int math_type (lua_State *L) { /* there is a 'long long' type (which must have at least 64 bits) */ #define Rand64 unsigned long long -#elif (LUA_MAXINTEGER >> 31 >> 31) >= 1 +#elif (LUA_MAXINTEGER >> 30 >> 30) >= 7 /* 'lua_Integer' has at least 64 bits */ #define Rand64 lua_Unsigned @@ -347,7 +347,7 @@ static lua_Number I2d (Rand64 x) { #else /* no 'Rand64' }{ */ /* get an integer with at least 32 bits */ -#if (INT_MAX >> 30) >= 1 +#if LUAI_IS32INT typedef unsigned int lu_int32; #else typedef unsigned long lu_int32; @@ -538,7 +538,7 @@ static lua_Unsigned project (lua_Unsigned ran, lua_Unsigned n, lim |= (lim >> 4); lim |= (lim >> 8); lim |= (lim >> 16); -#if (LUA_MAXINTEGER >> 30 >> 1) > 0 +#if (LUA_MAXINTEGER >> 30) >= 3 lim |= (lim >> 32); /* integer type has more than 32 bits */ #endif } diff --git a/lopcodes.h b/lopcodes.h index bbdd68978b..a314dcd18c 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -57,12 +57,18 @@ enum OpMode {iABC, iABx, iAsBx, iAx, isJ}; /* basic instruction formats */ #define POS_sJ POS_A + /* ** limits for opcode arguments. -** we use (signed) int to manipulate most arguments, -** so they must fit in LUAI_BITSINT-1 bits (-1 for sign) +** we use (signed) 'int' to manipulate most arguments, +** so they must fit in ints. */ -#if SIZE_Bx < LUAI_BITSINT-1 + +/* Check whether type 'int' has at least 'b' bits ('b' < 32) */ +#define L_INTHASBITS(b) ((UINT_MAX >> ((b) - 1)) >= 1) + + +#if L_INTHASBITS(SIZE_Bx) #define MAXARG_Bx ((1<>1) /* 'sBx' is signed */ -#if SIZE_Ax < LUAI_BITSINT-1 +#if L_INTHASBITS(SIZE_Ax) #define MAXARG_Ax ((1<> 4); size |= (size >> 8); size |= (size >> 16); -#if (INT_MAX >> 30 >> 1) > 0 - size |= (size >> 32); /* int has more than 32 bits */ +#if (UINT_MAX >> 30) > 3 + size |= (size >> 32); /* unsigned int has more than 32 bits */ #endif size++; lua_assert(ispow2(size) && size/2 < t->alimit && t->alimit < size); diff --git a/luaconf.h b/luaconf.h index 4647ba1785..e6271b8028 100644 --- a/luaconf.h +++ b/luaconf.h @@ -87,9 +87,8 @@ /* @@ LUAI_IS32INT is true iff 'int' has (at least) 32 bits. -** (the use of two shifts avoids undefined shifts) */ -#define LUAI_IS32INT (((UINT_MAX >> 15) >> 15) >= 3) +#define LUAI_IS32INT ((UINT_MAX >> 30) >= 3) /* }================================================================== */ diff --git a/lutf8lib.c b/lutf8lib.c index 9786b60ce0..b4b787e7f2 100644 --- a/lutf8lib.c +++ b/lutf8lib.c @@ -28,7 +28,7 @@ /* ** Integer type for decoded UTF-8 values; MAXUTF needs 31 bits. */ -#if ((UINT_MAX >> 15) >> 15) >= 1 +#if (UINT_MAX >> 30) >= 1 typedef unsigned int utfint; #else typedef unsigned long utfint; From 0b63d79b36790febd4c081bf8d6737df27529f8d Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 13 May 2019 16:20:40 -0300 Subject: [PATCH 0418/1145] Details - 'luaL_setfuncs' avoids creating closures for placeholders. - Fixed some warnings about unused values in comma expressions. - Comments. --- lauxlib.c | 10 +++++++--- ldo.c | 2 +- liolib.c | 2 +- ltablib.c | 2 +- ltests.c | 8 +++----- 5 files changed, 13 insertions(+), 11 deletions(-) diff --git a/lauxlib.c b/lauxlib.c index dfe501a733..89e53dc193 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -898,9 +898,13 @@ LUALIB_API void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) { luaL_checkstack(L, nup, "too many upvalues"); for (; l->name != NULL; l++) { /* fill the table with given functions */ int i; - for (i = 0; i < nup; i++) /* copy upvalues to the top */ - lua_pushvalue(L, -nup); - lua_pushcclosure(L, l->func, nup); /* closure with those upvalues */ + if (l->func == NULL) /* place holder? */ + lua_pushboolean(L, 0); + else { + for (i = 0; i < nup; i++) /* copy upvalues to the top */ + lua_pushvalue(L, -nup); + lua_pushcclosure(L, l->func, nup); /* closure with those upvalues */ + } lua_setfield(L, -(nup + 2), l->name); } lua_pop(L, nup); /* remove upvalues */ diff --git a/ldo.c b/ldo.c index e9a88e9e50..e7e76a652a 100644 --- a/ldo.c +++ b/ldo.c @@ -669,7 +669,7 @@ LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs, else if (L->top - (L->ci->func + 1) == nargs) /* no function? */ return resume_error(L, "cannot resume dead coroutine", nargs); } - else if (L->status != LUA_YIELD) + else if (L->status != LUA_YIELD) /* ended with errors? */ return resume_error(L, "cannot resume dead coroutine", nargs); if (from == NULL) L->nCcalls = 1; diff --git a/liolib.c b/liolib.c index 7d6d51e672..fa6a093925 100644 --- a/liolib.c +++ b/liolib.c @@ -39,7 +39,7 @@ /* Check whether 'mode' matches '[rwa]%+?[L_MODEEXT]*' */ static int l_checkmode (const char *mode) { return (*mode != '\0' && strchr("rwa", *(mode++)) != NULL && - (*mode != '+' || (++mode, 1)) && /* skip if char is '+' */ + (*mode != '+' || ((void)(++mode), 1)) && /* skip if char is '+' */ (strspn(mode, L_MODEEXT) == strlen(mode))); /* check extensions */ } diff --git a/ltablib.c b/ltablib.c index a9169f9e3e..48a6bdfe27 100644 --- a/ltablib.c +++ b/ltablib.c @@ -299,7 +299,7 @@ static IdxT partition (lua_State *L, IdxT lo, IdxT up) { /* loop invariant: a[lo .. i] <= P <= a[j .. up] */ for (;;) { /* next loop: repeat ++i while a[i] < P */ - while (lua_geti(L, 1, ++i), sort_comp(L, -1, -2)) { + while ((void)lua_geti(L, 1, ++i), sort_comp(L, -1, -2)) { if (i == up - 1) /* a[i] < P but a[up - 1] == P ?? */ luaL_error(L, "invalid order function for sorting"); lua_pop(L, 1); /* remove a[i] */ diff --git a/ltests.c b/ltests.c index 7d441d1ac2..f786eeb3c2 100644 --- a/ltests.c +++ b/ltests.c @@ -51,9 +51,8 @@ static int runC (lua_State *L, lua_State *L1, const char *pc); static void setnameval (lua_State *L, const char *name, int val) { - lua_pushstring(L, name); lua_pushinteger(L, val); - lua_settable(L, -3); + lua_setfield(L, -2, name); } @@ -710,12 +709,11 @@ static void printstack (lua_State *L) { static int get_limits (lua_State *L) { - lua_createtable(L, 0, 5); - setnameval(L, "BITS_INT", LUAI_BITSINT); + lua_createtable(L, 0, 6); + setnameval(L, "IS32INT", LUAI_IS32INT); setnameval(L, "MAXARG_Ax", MAXARG_Ax); setnameval(L, "MAXARG_Bx", MAXARG_Bx); setnameval(L, "OFFSET_sBx", OFFSET_sBx); - setnameval(L, "BITS_INT", LUAI_BITSINT); setnameval(L, "LFPF", LFIELDS_PER_FLUSH); setnameval(L, "NUM_OPCODES", NUM_OPCODES); return 1; From 347d6961ac14213264c7176e3d125c9ba8475b01 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 14 May 2019 11:10:24 -0300 Subject: [PATCH 0419/1145] Define LUA_MAXUNSIGNED as a preprocessor constant The previous definition of LUA_MAXUNSIGNED used a typecast, making it unsuitable for constant expressions in the preprocessor. --- lmathlib.c | 4 ++-- luaconf.h | 9 ++++++++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/lmathlib.c b/lmathlib.c index 3454c41fe1..f6f0b4265f 100644 --- a/lmathlib.c +++ b/lmathlib.c @@ -276,7 +276,7 @@ static int math_type (lua_State *L) { /* there is a 'long long' type (which must have at least 64 bits) */ #define Rand64 unsigned long long -#elif (LUA_MAXINTEGER >> 30 >> 30) >= 7 +#elif (LUA_MAXUNSIGNED >> 31 >> 31) >= 3 /* 'lua_Integer' has at least 64 bits */ #define Rand64 lua_Unsigned @@ -538,7 +538,7 @@ static lua_Unsigned project (lua_Unsigned ran, lua_Unsigned n, lim |= (lim >> 4); lim |= (lim >> 8); lim |= (lim >> 16); -#if (LUA_MAXINTEGER >> 30) >= 3 +#if (LUA_MAXUNSIGNED >> 31) >= 3 lim |= (lim >> 32); /* integer type has more than 32 bits */ #endif } diff --git a/luaconf.h b/luaconf.h index e6271b8028..66dca6bfcb 100644 --- a/luaconf.h +++ b/luaconf.h @@ -515,7 +515,6 @@ */ #define LUA_UNSIGNED unsigned LUAI_UACINT -#define LUA_MAXUNSIGNED (~(lua_Unsigned)0) #define LUA_UNSIGNEDBITS (sizeof(LUA_UNSIGNED) * CHAR_BIT) @@ -530,6 +529,8 @@ #define LUA_MAXINTEGER INT_MAX #define LUA_MININTEGER INT_MIN +#define LUA_MAXUNSIGNED UINT_MAX + #elif LUA_INT_TYPE == LUA_INT_LONG /* }{ long */ #define LUA_INTEGER long @@ -538,6 +539,8 @@ #define LUA_MAXINTEGER LONG_MAX #define LUA_MININTEGER LONG_MIN +#define LUA_MAXUNSIGNED ULONG_MAX + #elif LUA_INT_TYPE == LUA_INT_LONGLONG /* }{ long long */ /* use presence of macro LLONG_MAX as proxy for C99 compliance */ @@ -550,6 +553,8 @@ #define LUA_MAXINTEGER LLONG_MAX #define LUA_MININTEGER LLONG_MIN +#define LUA_MAXUNSIGNED ULLONG_MAX + #elif defined(LUA_USE_WINDOWS) /* }{ */ /* in Windows, can use specific Windows types */ @@ -559,6 +564,8 @@ #define LUA_MAXINTEGER _I64_MAX #define LUA_MININTEGER _I64_MIN +#define LUA_MAXUNSIGNED _UI64_MAX + #else /* }{ */ #error "Compiler does not support 'long long'. Use option '-DLUA_32BITS' \ From d9f40e3f6fb61650240c47d548bee69b24b07859 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 17 May 2019 11:11:44 -0300 Subject: [PATCH 0420/1145] First implementation for 'const' variables A variable can be declared const, which means it cannot be assigned to, with the syntax 'local name = exp'. --- lcode.c | 36 ++++++++------ lparser.c | 109 ++++++++++++++++++++++++++++++++++-------- lparser.h | 13 +++-- manual/manual.of | 28 +++++++---- testes/constructs.lua | 61 ++++++++++++++++++++++- testes/files.lua | 8 ++-- testes/math.lua | 6 +-- 7 files changed, 205 insertions(+), 56 deletions(-) diff --git a/lcode.c b/lcode.c index 1e36e5842a..eccb8380a9 100644 --- a/lcode.c +++ b/lcode.c @@ -678,11 +678,12 @@ void luaK_setoneret (FuncState *fs, expdesc *e) { void luaK_dischargevars (FuncState *fs, expdesc *e) { switch (e->k) { case VLOCAL: { /* already in a register */ + e->u.info = e->u.var.idx; e->k = VNONRELOC; /* becomes a non-relocatable value */ break; } case VUPVAL: { /* move value to some (pending) register */ - e->u.info = luaK_codeABC(fs, OP_GETUPVAL, 0, e->u.info, 0); + e->u.info = luaK_codeABC(fs, OP_GETUPVAL, 0, e->u.var.idx, 0); e->k = VRELOC; break; } @@ -938,12 +939,12 @@ void luaK_storevar (FuncState *fs, expdesc *var, expdesc *ex) { switch (var->k) { case VLOCAL: { freeexp(fs, ex); - exp2reg(fs, ex, var->u.info); /* compute 'ex' into proper place */ + exp2reg(fs, ex, var->u.var.idx); /* compute 'ex' into proper place */ return; } case VUPVAL: { int e = luaK_exp2anyreg(fs, ex); - luaK_codeABC(fs, OP_SETUPVAL, e, var->u.info, 0); + luaK_codeABC(fs, OP_SETUPVAL, e, var->u.var.idx, 0); break; } case VINDEXUP: { @@ -1165,25 +1166,30 @@ static int isSCnumber (expdesc *e, lua_Integer *i, int *isfloat) { ** values in registers. */ void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) { - lua_assert(!hasjumps(t) && (vkisinreg(t->k) || t->k == VUPVAL)); + lua_assert(!hasjumps(t) && + (t->k == VLOCAL || t->k == VNONRELOC || t->k == VUPVAL)); if (t->k == VUPVAL && !isKstr(fs, k)) /* upvalue indexed by non string? */ luaK_exp2anyreg(fs, t); /* put it in a register */ - t->u.ind.t = t->u.info; /* register or upvalue index */ if (t->k == VUPVAL) { + t->u.ind.t = t->u.var.idx; /* upvalue index */ t->u.ind.idx = k->u.info; /* literal string */ t->k = VINDEXUP; } - else if (isKstr(fs, k)) { - t->u.ind.idx = k->u.info; /* literal string */ - t->k = VINDEXSTR; - } - else if (isCint(k)) { - t->u.ind.idx = cast_int(k->u.ival); /* integer constant in proper range */ - t->k = VINDEXI; - } else { - t->u.ind.idx = luaK_exp2anyreg(fs, k); /* register */ - t->k = VINDEXED; + /* register index of the table */ + t->u.ind.t = (t->k == VLOCAL) ? t->u.var.idx: t->u.info; + if (isKstr(fs, k)) { + t->u.ind.idx = k->u.info; /* literal string */ + t->k = VINDEXSTR; + } + else if (isCint(k)) { + t->u.ind.idx = cast_int(k->u.ival); /* int. constant in proper range */ + t->k = VINDEXI; + } + else { + t->u.ind.idx = luaK_exp2anyreg(fs, k); /* register */ + t->k = VINDEXED; + } } } diff --git a/lparser.c b/lparser.c index 4e6c27fe00..7c23710ad2 100644 --- a/lparser.c +++ b/lparser.c @@ -156,6 +156,13 @@ static void init_exp (expdesc *e, expkind k, int i) { } +static void init_var (expdesc *e, expkind k, int i) { + e->f = e->t = NO_JUMP; + e->k = k; + e->u.var.idx = i; +} + + static void codestring (LexState *ls, expdesc *e, TString *s) { init_exp(e, VK, luaK_stringK(ls->fs, s)); } @@ -187,31 +194,82 @@ static int registerlocalvar (LexState *ls, TString *varname) { /* ** Create a new local variable with the given 'name'. */ -static void new_localvar (LexState *ls, TString *name) { +static Vardesc *new_localvar (LexState *ls, TString *name) { FuncState *fs = ls->fs; Dyndata *dyd = ls->dyd; + Vardesc *var; int reg = registerlocalvar(ls, name); checklimit(fs, dyd->actvar.n + 1 - fs->firstlocal, MAXVARS, "local variables"); luaM_growvector(ls->L, dyd->actvar.arr, dyd->actvar.n + 1, dyd->actvar.size, Vardesc, MAX_INT, "local variables"); - dyd->actvar.arr[dyd->actvar.n++].idx = cast(short, reg); + var = &dyd->actvar.arr[dyd->actvar.n++]; + var->idx = cast(short, reg); + var->name = name; + var->ro = 0; + return var; } #define new_localvarliteral(ls,v) \ new_localvar(ls, luaX_newstring(ls, "" v, (sizeof(v)/sizeof(char)) - 1)); + +/* +** Return the "variable description" (Vardesc) of a given +** variable +*/ +static Vardesc *getlocalvardesc (FuncState *fs, int i) { + return &fs->ls->dyd->actvar.arr[fs->firstlocal + i]; +} + /* ** Get the debug-information entry for current variable 'i'. */ static LocVar *getlocvar (FuncState *fs, int i) { - int idx = fs->ls->dyd->actvar.arr[fs->firstlocal + i].idx; + int idx = getlocalvardesc(fs, i)->idx; lua_assert(idx < fs->nlocvars); return &fs->f->locvars[idx]; } +/* +** Return the "variable description" (Vardesc) of a given +** variable or upvalue +*/ +static Vardesc *getvardesc (FuncState *fs, expdesc *e) { + if (e->k == VLOCAL) + return getlocalvardesc(fs, e->u.var.idx); + else if (e->k != VUPVAL) + return NULL; /* not a local variable */ + else { /* upvalue: must go up all levels up to the original local */ + int idx = e->u.var.idx; + for (;;) { + Upvaldesc *up = &fs->f->upvalues[idx]; + fs = fs->prev; /* must look at the previous level */ + idx = up->idx; /* at this index */ + if (fs == NULL) { /* no more levels? (can happen only with _ENV) */ + lua_assert(strcmp(getstr(up->name), LUA_ENV) == 0); + return NULL; + } + else if (up->instack) /* got to the original level? */ + return getlocalvardesc(fs, idx); + /* else repeat for previous level */ + } + } +} + + +static void check_readonly (LexState *ls, expdesc *e) { + Vardesc *vardesc = getvardesc(ls->fs, e); + if (vardesc && vardesc->ro) { /* is variable local and const? */ + const char *msg = luaO_pushfstring(ls->L, + "assignment to const variable '%s'", getstr(vardesc->name)); + luaK_semerror(ls, msg); /* error */ + } +} + + /* ** Start the scope for the last 'nvars' created variables. ** (debug info.) @@ -259,7 +317,7 @@ static int newupvalue (FuncState *fs, TString *name, expdesc *v) { while (oldsize < f->sizeupvalues) f->upvalues[oldsize++].name = NULL; f->upvalues[fs->nups].instack = (v->k == VLOCAL); - f->upvalues[fs->nups].idx = cast_byte(v->u.info); + f->upvalues[fs->nups].idx = cast_byte(v->u.var.idx); f->upvalues[fs->nups].name = name; luaC_objbarrier(fs->ls->L, f, name); return fs->nups++; @@ -304,7 +362,7 @@ static void singlevaraux (FuncState *fs, TString *n, expdesc *var, int base) { else { int v = searchvar(fs, n); /* look up locals at current level */ if (v >= 0) { /* found? */ - init_exp(var, VLOCAL, v); /* variable is local */ + init_var(var, VLOCAL, v); /* variable is local */ if (!base) markupval(fs, v); /* local will be used as an upval */ } @@ -317,7 +375,7 @@ static void singlevaraux (FuncState *fs, TString *n, expdesc *var, int base) { /* else was LOCAL or UPVAL */ idx = newupvalue(fs, n, var); /* will be a new upvalue */ } - init_exp(var, VUPVAL, idx); /* new or old upvalue */ + init_var(var, VUPVAL, idx); /* new or old upvalue */ } } } @@ -1199,20 +1257,20 @@ static void check_conflict (LexState *ls, struct LHS_assign *lh, expdesc *v) { for (; lh; lh = lh->prev) { /* check all previous assignments */ if (vkisindexed(lh->v.k)) { /* assignment to table field? */ if (lh->v.k == VINDEXUP) { /* is table an upvalue? */ - if (v->k == VUPVAL && lh->v.u.ind.t == v->u.info) { + if (v->k == VUPVAL && lh->v.u.ind.t == v->u.var.idx) { conflict = 1; /* table is the upvalue being assigned now */ lh->v.k = VINDEXSTR; lh->v.u.ind.t = extra; /* assignment will use safe copy */ } } else { /* table is a register */ - if (v->k == VLOCAL && lh->v.u.ind.t == v->u.info) { + if (v->k == VLOCAL && lh->v.u.ind.t == v->u.var.idx) { conflict = 1; /* table is the local being assigned now */ lh->v.u.ind.t = extra; /* assignment will use safe copy */ } /* is index the local being assigned? */ if (lh->v.k == VINDEXED && v->k == VLOCAL && - lh->v.u.ind.idx == v->u.info) { + lh->v.u.ind.idx == v->u.var.idx) { conflict = 1; lh->v.u.ind.idx = extra; /* previous assignment will use safe copy */ } @@ -1222,7 +1280,7 @@ static void check_conflict (LexState *ls, struct LHS_assign *lh, expdesc *v) { 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_codeABC(fs, op, extra, v->u.var.idx, 0); luaK_reserveregs(fs, 1); } } @@ -1237,6 +1295,7 @@ static void check_conflict (LexState *ls, struct LHS_assign *lh, expdesc *v) { static void restassign (LexState *ls, struct LHS_assign *lh, int nvars) { expdesc e; check_condition(ls, vkisvar(lh->v.k), "syntax error"); + check_readonly(ls, &lh->v); if (testnext(ls, ',')) { /* restassign -> ',' suffixedexp restassign */ struct LHS_assign nv; nv.prev = lh; @@ -1615,20 +1674,30 @@ static void commonlocalstat (LexState *ls) { } -static void tocloselocalstat (LexState *ls) { +static void tocloselocalstat (LexState *ls, Vardesc *var) { FuncState *fs = ls->fs; + var->ro = 1; /* to-be-closed variables are always read-only */ + markupval(fs, fs->nactvar); + fs->bl->insidetbc = 1; /* in the scope of a to-be-closed variable */ + luaK_codeABC(fs, OP_TBC, fs->nactvar - 1, 0, 0); +} + + +static void attriblocalstat (LexState *ls) { + Vardesc *var; TString *attr = str_checkname(ls); - if (strcmp(getstr(attr), "toclose") != 0) - luaK_semerror(ls, - luaO_pushfstring(ls->L, "unknown attribute '%s'", getstr(attr))); testnext(ls, '>'); - new_localvar(ls, str_checkname(ls)); + var = new_localvar(ls, str_checkname(ls)); checknext(ls, '='); exp1(ls); - markupval(fs, fs->nactvar); - fs->bl->insidetbc = 1; /* in the scope of a to-be-closed variable */ adjustlocalvars(ls, 1); - luaK_codeABC(fs, OP_TBC, fs->nactvar - 1, 0, 0); + if (strcmp(getstr(attr), "const") == 0) + var->ro = 1; /* set variable as read-only */ + else if (strcmp(getstr(attr), "toclose") == 0) + tocloselocalstat(ls, var); + else + luaK_semerror(ls, + luaO_pushfstring(ls->L, "unknown attribute '%s'", getstr(attr))); } @@ -1636,7 +1705,7 @@ static void localstat (LexState *ls) { /* stat -> LOCAL NAME {',' NAME} ['=' explist] | LOCAL *toclose NAME '=' exp */ if (testnext(ls, '<')) - tocloselocalstat(ls); + attriblocalstat(ls); else commonlocalstat(ls); } @@ -1801,7 +1870,7 @@ static void mainfunc (LexState *ls, FuncState *fs) { expdesc v; open_func(ls, fs, &bl); setvararg(fs, 0); /* main function is always declared vararg */ - init_exp(&v, VLOCAL, 0); /* create and... */ + init_var(&v, VLOCAL, 0); /* create and... */ newupvalue(fs, ls->envn, &v); /* ...set environment upvalue */ luaX_next(ls); /* read first token */ statlist(ls); /* parse main body */ diff --git a/lparser.h b/lparser.h index 3d6bd97853..3b5d399fd8 100644 --- a/lparser.h +++ b/lparser.h @@ -33,8 +33,8 @@ typedef enum { VKINT, /* integer constant; nval = numerical integer value */ VNONRELOC, /* expression has its value in a fixed register; info = result register */ - VLOCAL, /* local variable; info = local register */ - VUPVAL, /* upvalue variable; info = index of upvalue in 'upvalues' */ + VLOCAL, /* local variable; var.idx = local register */ + VUPVAL, /* upvalue variable; var.idx = index of upvalue in 'upvalues' */ VINDEXED, /* indexed variable; ind.t = table register; ind.idx = key's R index */ @@ -58,7 +58,7 @@ typedef enum { #define vkisvar(k) (VLOCAL <= (k) && (k) <= VINDEXSTR) #define vkisindexed(k) (VINDEXED <= (k) && (k) <= VINDEXSTR) -#define vkisinreg(k) ((k) == VNONRELOC || (k) == VLOCAL) + typedef struct expdesc { expkind k; @@ -70,15 +70,20 @@ typedef struct expdesc { short idx; /* index (R or "long" K) */ lu_byte t; /* table (register or upvalue) */ } ind; + struct { /* for local variables and upvalues */ + lu_byte idx; /* index of the variable */ + } var; } u; int t; /* patch list of 'exit when true' */ int f; /* patch list of 'exit when false' */ } expdesc; -/* description of active local variable */ +/* description of an active local variable */ typedef struct Vardesc { + TString *name; short idx; /* index of the variable in the Proto's 'locvars' array */ + lu_byte ro; /* true if variable is 'const' */ } Vardesc; diff --git a/manual/manual.of b/manual/manual.of index 54a0787927..6cac8c6cad 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -1488,13 +1488,24 @@ Function calls are explained in @See{functioncall}. @sect3{localvar| @title{Local Declarations} @x{Local variables} can be declared anywhere inside a block. -The declaration can include an initial assignment: +The declaration can include an initialization: @Produc{ @producname{stat}@producbody{@Rw{local} namelist @bnfopt{@bnfter{=} explist}} -} +@producname{stat}@producbody{ + @Rw{local} @bnfter{<} Name @bnfter{>} Name @bnfter{=} exp +}} If present, an initial assignment has the same semantics of a multiple assignment @see{assignment}. Otherwise, all variables are initialized with @nil. +The second syntax declares a local with a given attribute, +which is the name between the angle brackets. +In this case, there must be an initialization. +There are two possible attributes: +@id{const}, which declares a @x{constant variable}, +that is, a variable that cannot be assigned to +after its initialization; +and @id{toclose}, wich declares a to-be-closed variable @see{to-be-closed}. + A chunk is also a block @see{chunks}, and so local variables can be declared in a chunk outside any explicit block. @@ -1506,12 +1517,12 @@ The visibility rules for local variables are explained in @See{visibility}. @sect3{to-be-closed| @title{To-be-closed Variables} A local variable can be declared as a @def{to-be-closed} variable, -with the following syntax: +using the identifier @id{toclose} as its attribute: @Produc{ @producname{stat}@producbody{ - @Rw{local} @bnfter{<} @bnfter{toclose} @bnfter{>} Name @bnfter{=} exp + @Rw{local} @bnfter{<} @id{toclose} @bnfter{>} Name @bnfter{=} exp }} -A to-be-closed variable behaves like a normal local variable, +A to-be-closed variable behaves like a constant local variable, except that its value is @emph{closed} whenever the variable goes out of scope, including normal block termination, exiting its block by @Rw{break}/@Rw{goto}/@Rw{return}, @@ -7603,7 +7614,7 @@ or a float otherwise. @LibEntry{math.abs (x)| -Returns the absolute value of @id{x}. (integer/float) +Returns the maximum value between @id{x} and @id{-x}. (integer/float) } @@ -8042,7 +8053,8 @@ following the lexical conventions of Lua. This format always reads the longest input sequence that is a valid prefix for a numeral; if that prefix does not form a valid numeral -(e.g., an empty string, @St{0x}, or @St{3.4e-}), +(e.g., an empty string, @St{0x}, or @St{3.4e-}) +or it is too long (more than 200 characters), it is discarded and the format returns @nil. } @@ -8949,7 +8961,7 @@ and @bnfNter{LiteralString}, see @See{lexical}.) @OrNL @Rw{function} funcname funcbody @OrNL @Rw{local} @Rw{function} @bnfNter{Name} funcbody @OrNL @Rw{local} namelist @bnfopt{@bnfter{=} explist} -@OrNL @Rw{local} @bnfter{<} @bnfter{toclose} @bnfter{>} Name @bnfter{=} exp +@OrNL @Rw{local} @bnfter{<} Name @bnfter{>} Name @bnfter{=} exp } @producname{retstat}@producbody{@Rw{return} diff --git a/testes/constructs.lua b/testes/constructs.lua index a83df79eaf..b91e0979ec 100644 --- a/testes/constructs.lua +++ b/testes/constructs.lua @@ -59,6 +59,41 @@ assert((x>y) and x or y == 2); assert(1234567890 == tonumber('1234567890') and 1234567890+1 == 1234567891) +do -- testing operators with diffent kinds of constants + -- operands to consider: + -- * fit in register + -- * constant doesn't fit in register + -- * floats with integral values + local operand = {3, 100, 5.0, -10, -5.0, 10000, -10000} + local operator = {"+", "-", "*", "/", "//", "%", "^", + "&", "|", "^", "<<", ">>", + "==", "~=", "<", ">", "<=", ">=",} + for _, op in ipairs(operator) do + local f = assert(load(string.format([[return function (x,y) + return x %s y + end]], op)))(); + for _, o1 in ipairs(operand) do + for _, o2 in ipairs(operand) do + local gab = f(o1, o2) + + _ENV.XX = o1 + code = string.format("return XX %s %s", op, o2) + res = assert(load(code))() + assert(res == gab) + + _ENV.XX = o2 + local code = string.format("return (%s) %s XX", o1, op) + local res = assert(load(code))() + assert(res == gab) + + code = string.format("return (%s) %s %s", o1, op, o2) + res = assert(load(code))() + assert(res == gab) + end + end + end +end + -- silly loops repeat until 1; repeat until true; @@ -175,6 +210,28 @@ assert(a==1 and b==nil) print'+'; +do -- testing constants + local prog = [[local x = 10]] + checkload(prog, "unknown attribute 'XXX'") + + checkload([[local xxx = 20; xxx = 10]], + ":1: assignment to const variable 'xxx'") + + checkload([[ + local xx; + local xxx = 20; + local yyy; + local function foo () + local abc = xx + yyy + xxx; + return function () return function () xxx = yyy end end + end + ]], ":6: assignment to const variable 'xxx'") + + checkload([[ + local x = nil + x = io.open() + ]], ":2: assignment to const variable 'x'") +end f = [[ return function ( a , b , c , d , e ) @@ -245,12 +302,12 @@ print('testing short-circuit optimizations (' .. _ENV.GLOB1 .. ')') -- operators with their respective values -local binops = { +local binops = { {" and ", function (a,b) if not a then return a else return b end end}, {" or ", function (a,b) if a then return a else return b end end}, } -local cases = {} +local cases = {} -- creates all combinations of '(cases[i] op cases[n-i])' plus -- 'not(cases[i] op cases[n-i])' (syntax + value) diff --git a/testes/files.lua b/testes/files.lua index eb100fe137..54931c14c1 100644 --- a/testes/files.lua +++ b/testes/files.lua @@ -144,7 +144,7 @@ do f:write(string.format("0x%X\n", -maxint)) f:write("-0xABCp-3", '\n') assert(f:close()) - f = assert(io.open(file, "r")) + local f = assert(io.open(file, "r")) assert(f:read("n") == maxint) assert(f:read("n") == maxint) assert(f:read("n") == 0xABCp-3) @@ -170,18 +170,18 @@ three ]] local l1, l2, l3, l4, n1, n2, c, dummy assert(f:close()) - f = assert(io.open(file, "r")) + local f = assert(io.open(file, "r")) l1, l2, n1, n2, dummy = f:read("l", "L", "n", "n") assert(l1 == "a line" and l2 == "another line\n" and n1 == 1234 and n2 == 3.45 and dummy == nil) assert(f:close()) - f = assert(io.open(file, "r")) + local f = assert(io.open(file, "r")) l1, l2, n1, n2, c, l3, l4, dummy = f:read(7, "l", "n", "n", 1, "l", "l") assert(l1 == "a line\n" and l2 == "another line" and c == '\n' and n1 == 1234 and n2 == 3.45 and l3 == "one" and l4 == "two" and dummy == nil) assert(f:close()) - f = assert(io.open(file, "r")) + local f = assert(io.open(file, "r")) -- second item failing l1, n1, n2, dummy = f:read("l", "n", "n", "l") assert(l1 == "a line" and n1 == nil) diff --git a/testes/math.lua b/testes/math.lua index b010ff6c3a..c45a91ad5c 100644 --- a/testes/math.lua +++ b/testes/math.lua @@ -3,10 +3,10 @@ print("testing numbers and math lib") -local minint = math.mininteger -local maxint = math.maxinteger +local minint = math.mininteger +local maxint = math.maxinteger -local intbits = math.floor(math.log(maxint, 2) + 0.5) + 1 +local intbits = math.floor(math.log(maxint, 2) + 0.5) + 1 assert((1 << intbits) == 0) assert(minint == 1 << (intbits - 1)) From b293ae0577bebaca7169cb4f041b800641d5e2c4 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 28 May 2019 15:46:49 -0300 Subject: [PATCH 0421/1145] Details - new error message for "attempt to assign to const variable" - note in the manual about compatibility options - comments - small changes in 'read_line' and 'pushstr' --- liolib.c | 10 +++++----- lobject.c | 4 ++-- lparser.c | 2 +- lstate.h | 16 ++++++++++++++++ luaconf.h | 14 +++++++++----- manual/manual.of | 13 ++++++++----- testes/constructs.lua | 6 +++--- testes/locals.lua | 1 - testes/strings.lua | 3 ++- 9 files changed, 46 insertions(+), 23 deletions(-) diff --git a/liolib.c b/liolib.c index fa6a093925..1484676d47 100644 --- a/liolib.c +++ b/liolib.c @@ -504,17 +504,17 @@ static int test_eof (lua_State *L, FILE *f) { static int read_line (lua_State *L, FILE *f, int chop) { luaL_Buffer b; - int c = '\0'; + int c; luaL_buffinit(L, &b); - while (c != EOF && c != '\n') { /* repeat until end of line */ - char *buff = luaL_prepbuffer(&b); /* preallocate buffer */ + do { /* may need to read several chunks to get whole line */ + char *buff = luaL_prepbuffer(&b); /* preallocate buffer space */ int i = 0; l_lockfile(f); /* no memory errors can happen inside the lock */ while (i < LUAL_BUFFERSIZE && (c = l_getc(f)) != EOF && c != '\n') - buff[i++] = c; + buff[i++] = c; /* read up to end of line or buffer limit */ l_unlockfile(f); luaL_addsize(&b, i); - } + } while (c != EOF && c != '\n'); /* repeat until end of line */ if (!chop && c == '\n') /* want a newline and have one? */ luaL_addchar(&b, c); /* add ending newline to result */ luaL_pushresult(&b); /* close buffer */ diff --git a/lobject.c b/lobject.c index ce14059f47..979a68896e 100644 --- a/lobject.c +++ b/lobject.c @@ -419,9 +419,9 @@ typedef struct BuffFS { static void pushstr (BuffFS *buff, const char *str, size_t l) { lua_State *L = buff->L; setsvalue2s(L, L->top, luaS_newlstr(L, str, l)); - L->top++; + L->top++; /* may use one extra slot */ buff->pushed++; - if (buff->pushed > 1 && L->top + 2 > L->stack_last) { + if (buff->pushed > 1 && L->top + 1 >= L->stack_last) { luaV_concat(L, buff->pushed); /* join all partial results into one */ buff->pushed = 1; } diff --git a/lparser.c b/lparser.c index 7c23710ad2..045efd93ff 100644 --- a/lparser.c +++ b/lparser.c @@ -264,7 +264,7 @@ static void check_readonly (LexState *ls, expdesc *e) { Vardesc *vardesc = getvardesc(ls->fs, e); if (vardesc && vardesc->ro) { /* is variable local and const? */ const char *msg = luaO_pushfstring(ls->L, - "assignment to const variable '%s'", getstr(vardesc->name)); + "attempt to assign to const variable '%s'", getstr(vardesc->name)); luaK_semerror(ls, msg); /* error */ } } diff --git a/lstate.h b/lstate.h index e35f89625b..3bd5297334 100644 --- a/lstate.h +++ b/lstate.h @@ -26,6 +26,22 @@ ** 'fixedgc': all objects that are not to be collected (currently ** only small strings, such as reserved words). ** +** For the generational collector, some of these lists have marks for +** generations. Each mark points to the first element in the list for +** that particular generation; that generation goes until the next mark. +** +** 'allgc' -> 'survival': new objects; +** 'survival' -> 'old': objects that survived one collection; +** 'old' -> 'reallyold': objects that became old in last collection; +** 'reallyold' -> NULL: objects old for more than one cycle. +** +** 'finobj' -> 'finobjsur': new objects marked for finalization; +** 'finobjsur' -> 'finobjold': survived """"; +** 'finobjold' -> 'finobjrold': just old """"; +** 'finobjrold' -> NULL: really old """". +*/ + +/* ** Moreover, there is another set of lists that control gray objects. ** These lists are linked by fields 'gclist'. (All objects that ** can become gray have such a field. The field is not the same diff --git a/luaconf.h b/luaconf.h index 66dca6bfcb..39840e395e 100644 --- a/luaconf.h +++ b/luaconf.h @@ -344,8 +344,8 @@ /* @@ LUA_COMPAT_MATHLIB controls the presence of several deprecated ** functions in the mathematical library. -** (These functions were already officially removed in 5.3, but -** nevertheless they are available by default there.) +** (These functions were already officially removed in 5.3; +** nevertheless they are still available here.) */ #define LUA_COMPAT_MATHLIB @@ -353,23 +353,25 @@ @@ LUA_COMPAT_APIINTCASTS controls the presence of macros for ** manipulating other integer types (lua_pushunsigned, lua_tounsigned, ** luaL_checkint, luaL_checklong, etc.) +** (These macros were also officially removed in 5.3, but they are still +** available here.) */ #define LUA_COMPAT_APIINTCASTS + /* @@ LUA_COMPAT_LT_LE controls the emulation of the '__le' metamethod ** using '__lt'. */ #define LUA_COMPAT_LT_LE -#endif /* } */ - - /* @@ The following macros supply trivial compatibility for some ** changes in the API. The macros themselves document how to ** change your code to avoid using them. +** (Once more, these macros were officially removed in 5.3, but they are +** still available here.) */ #define lua_strlen(L,i) lua_rawlen(L, (i)) @@ -378,6 +380,8 @@ #define lua_equal(L,idx1,idx2) lua_compare(L,(idx1),(idx2),LUA_OPEQ) #define lua_lessthan(L,idx1,idx2) lua_compare(L,(idx1),(idx2),LUA_OPLT) +#endif /* } */ + /* }================================================================== */ diff --git a/manual/manual.of b/manual/manual.of index 6cac8c6cad..ff69cd2c36 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -8774,10 +8774,18 @@ is a more portable solution. Here we list the incompatibilities that you may find when moving a program from @N{Lua 5.3} to @N{Lua 5.4}. + You can avoid some incompatibilities by compiling Lua with appropriate options (see file @id{luaconf.h}). However, all these compatibility options will be removed in the future. +More often than not, +compatibility issues arise when these compatibility options +are removed. +So, whenever you have the chance, +you should try to test your code with a version of Lua compiled +with all compatibility options turned off. +That will ease transitions to newer versions of Lua. Lua versions can always change the C API in ways that do not imply source-code changes in a program, @@ -8825,11 +8833,6 @@ over integers changed in some details. In particular, the control variable never wraps around. } -@item{ -When a coroutine finishes with an error, -its stack is unwound (to run any pending closing methods). -} - @item{ A label for a @Rw{goto} cannot be declared where a label with the same name is visible, even if this other label is declared in an enclosing diff --git a/testes/constructs.lua b/testes/constructs.lua index b91e0979ec..fe4db2cbc9 100644 --- a/testes/constructs.lua +++ b/testes/constructs.lua @@ -215,7 +215,7 @@ do -- testing constants checkload(prog, "unknown attribute 'XXX'") checkload([[local xxx = 20; xxx = 10]], - ":1: assignment to const variable 'xxx'") + ":1: attempt to assign to const variable 'xxx'") checkload([[ local xx; @@ -225,12 +225,12 @@ do -- testing constants local abc = xx + yyy + xxx; return function () return function () xxx = yyy end end end - ]], ":6: assignment to const variable 'xxx'") + ]], ":6: attempt to assign to const variable 'xxx'") checkload([[ local x = nil x = io.open() - ]], ":2: assignment to const variable 'x'") + ]], ":2: attempt to assign to const variable 'x'") end f = [[ diff --git a/testes/locals.lua b/testes/locals.lua index c176f50663..e59ab95a32 100644 --- a/testes/locals.lua +++ b/testes/locals.lua @@ -452,7 +452,6 @@ do end) assert(co() == 100) local st, msg = pcall(co) -print(msg) -- should get last error raised assert(not st and string.find(msg, "%w+%.%w+:%d+: XXX")) end diff --git a/testes/strings.lua b/testes/strings.lua index 3e32f2c476..1b2b570e0e 100644 --- a/testes/strings.lua +++ b/testes/strings.lua @@ -3,7 +3,8 @@ print('testing strings and string library') -local maxi, mini = math.maxinteger, math.mininteger +local maxi = math.maxinteger +local mini = math.mininteger local function checkerror (msg, f, ...) From 2b8b53864c6b8655aa7198699884075b3e2f15fa Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 28 May 2019 15:50:40 -0300 Subject: [PATCH 0422/1145] Improvements in 'luaL_traceback' 'luaL_traceback' changed to use an aux buffer instead of concats. This should reduce the quantity of garbage it generates (in the form of intermediate strings) while producing a trackback. It also added information about the number of levels skipped when skipping levels in a trace. --- lauxlib.c | 53 +++++++++++++++++++++++++++++------------------------ 1 file changed, 29 insertions(+), 24 deletions(-) diff --git a/lauxlib.c b/lauxlib.c index 89e53dc193..d49ef573c3 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -46,8 +46,8 @@ /* -** search for 'objidx' in table at index -1. -** return 1 + string at top if find a good name. +** Search for 'objidx' in table at index -1. ('objidx' must be an +** absolute index.) Return 1 + string at top if it found a good name. */ static int findfield (lua_State *L, int objidx, int level) { if (level == 0 || !lua_istable(L, -1)) @@ -60,10 +60,10 @@ static int findfield (lua_State *L, int objidx, int level) { return 1; } else if (findfield(L, objidx, level - 1)) { /* try recursively */ - lua_remove(L, -2); /* remove table (but keep name) */ - lua_pushliteral(L, "."); - lua_insert(L, -2); /* place '.' between the two names */ - lua_concat(L, 3); + /* stack: lib_name, lib_table, field_name (top) */ + lua_pushliteral(L, "."); /* place '.' between the two names */ + lua_replace(L, -3); /* (in the slot ocupied by table) */ + lua_concat(L, 3); /* lib_name.field_name */ return 1; } } @@ -86,8 +86,8 @@ static int pushglobalfuncname (lua_State *L, lua_Debug *ar) { lua_pushstring(L, name + 3); /* push name without prefix */ lua_remove(L, -2); /* remove original name */ } - lua_copy(L, -1, top + 1); /* move name to proper place */ - lua_pop(L, 2); /* remove pushed values */ + lua_copy(L, -1, top + 1); /* copy name to proper place */ + lua_settop(L, top + 1); /* remove table "loaded" an name copy */ return 1; } else { @@ -130,32 +130,37 @@ static int lastlevel (lua_State *L) { LUALIB_API void luaL_traceback (lua_State *L, lua_State *L1, const char *msg, int level) { + luaL_Buffer b; lua_Debug ar; - int top = lua_gettop(L); int last = lastlevel(L1); - int n1 = (last - level > LEVELS1 + LEVELS2) ? LEVELS1 : -1; - if (msg) - lua_pushfstring(L, "%s\n", msg); - luaL_checkstack(L, 10, NULL); - lua_pushliteral(L, "stack traceback:"); + int limit2show = (last - level > LEVELS1 + LEVELS2) ? LEVELS1 : -1; + luaL_buffinit(L, &b); + if (msg) { + luaL_addstring(&b, msg); + luaL_addchar(&b, '\n'); + } + luaL_addstring(&b, "stack traceback:"); while (lua_getstack(L1, level++, &ar)) { - if (n1-- == 0) { /* too many levels? */ - lua_pushliteral(L, "\n\t..."); /* add a '...' */ - level = last - LEVELS2 + 1; /* and skip to last ones */ + if (limit2show-- == 0) { /* too many levels? */ + int n = last - level - LEVELS2 + 1; /* number of levels to skip */ + lua_pushfstring(L, "\n\t...\t(skipping %d levels)", n); + luaL_addvalue(&b); /* add warning about skip */ + level += n; /* and skip to last levels */ } else { lua_getinfo(L1, "Slnt", &ar); - lua_pushfstring(L, "\n\t%s:", ar.short_src); - if (ar.currentline > 0) - lua_pushfstring(L, "%d:", ar.currentline); - lua_pushliteral(L, " in "); + if (ar.currentline <= 0) + lua_pushfstring(L, "\n\t%s: in ", ar.short_src); + else + lua_pushfstring(L, "\n\t%s:%d: in ", ar.short_src, ar.currentline); + luaL_addvalue(&b); pushfuncname(L, &ar); + luaL_addvalue(&b); if (ar.istailcall) - lua_pushliteral(L, "\n\t(...tail calls...)"); - lua_concat(L, lua_gettop(L) - top); + luaL_addstring(&b, "\n\t(...tail calls...)"); } } - lua_concat(L, lua_gettop(L) - top); + luaL_pushresult(&b); } /* }====================================================== */ From 7d0f41df41e9c513e7282356541b54beaf9ed20d Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 3 Jun 2019 11:34:32 -0300 Subject: [PATCH 0423/1145] Improvements in 'testes/cstack.lua' - tests show progress in real time, so that we can see maximum stack levels even if test crashes. - new test for recursion continuing into message handler. --- testes/cstack.lua | 44 ++++++++++++++++++++++++++++++++------------ 1 file changed, 32 insertions(+), 12 deletions(-) diff --git a/testes/cstack.lua b/testes/cstack.lua index 9e5bbaefd7..3cb1e4ce23 100644 --- a/testes/cstack.lua +++ b/testes/cstack.lua @@ -13,20 +13,40 @@ local function checkerror (msg, f, ...) assert(not s and string.find(err, msg)) end +local count +local back = string.rep("\b", 8) +local function progress () + count = count + 1 + local n = string.format("%-8d", count) + io.stderr:write(back, n) +end + -do -- simple recursion - local count = 0 +do print("testing simple recursion:") + count = 0 local function foo () - count = count + 1 + progress() foo() end checkerror("stack overflow", foo) - print(" maximum recursion: " .. count) + print("\tfinal count: ", count) +end + + +do print("testing stack overflow in message handling") + count = 0 + local function loop (x, y, z) + progress() + return 1 + loop(x, y, z) + end + local res, msg = xpcall(loop, loop) + assert(msg == "error in error handling") + print("\tfinal count: ", count) end -- bug since 2.5 (C-stack overflow in recursion inside pattern matching) -do +do print("testing recursion inside pattern matching") local function f (size) local s = string.rep("a", size) local p = string.rep(".?", size) @@ -38,25 +58,25 @@ do end --- testing stack-overflow in recursive 'gsub' -do - local count = 0 +do print("testing stack-overflow in recursive 'gsub'") + count = 0 local function foo () - count = count + 1 + progress() string.gsub("a", ".", foo) end checkerror("stack overflow", foo) - print(" maximum 'gsub' nest (calls): " .. count) + print("\tfinal count: ", count) - -- can be done with metamethods, too + print("testing stack-overflow in recursive 'gsub' with metatables") count = 0 local t = setmetatable({}, {__index = foo}) foo = function () count = count + 1 + progress(count) string.gsub("a", ".", t) end checkerror("stack overflow", foo) - print(" maximum 'gsub' nest (metamethods): " .. count) + print("\tfinal count: ", count) end print'OK' From 2c68e66570206aa1496f9c76fcf2a1a0f550d692 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 3 Jun 2019 11:36:42 -0300 Subject: [PATCH 0424/1145] Details Several small changes from feedback on 5.4 alhpa rc1 (warnings, typos in the manual, and the like) --- ldebug.c | 5 +++-- lgc.c | 4 ++-- llex.c | 4 ++-- lobject.c | 18 +++++++++--------- lstring.c | 4 ++-- ltablib.c | 2 +- lvm.c | 4 ++-- manual/manual.of | 5 ++--- 8 files changed, 23 insertions(+), 23 deletions(-) diff --git a/ldebug.c b/ldebug.c index 6cd4e071e8..acaa653ad6 100644 --- a/ldebug.c +++ b/ldebug.c @@ -373,6 +373,7 @@ static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar, ar->ftransfer = ci->u2.transferinfo.ftransfer; ar->ntransfer = ci->u2.transferinfo.ntransfer; } + break; } case 'L': case 'f': /* handled by lua_getinfo */ @@ -525,8 +526,8 @@ static const char *gxf (const Proto *p, int pc, Instruction i, int isup) { } - const char *getobjname (const Proto *p, int lastpc, int reg, - const char **name) { +static const char *getobjname (const Proto *p, int lastpc, int reg, + const char **name) { int pc; *name = luaF_getlocalname(p, reg + 1, lastpc); if (*name) /* is a local? */ diff --git a/lgc.c b/lgc.c index e9189ac31f..69d8a186a0 100644 --- a/lgc.c +++ b/lgc.c @@ -484,8 +484,8 @@ static lu_mem traversetable (global_State *g, Table *h) { const TValue *mode = gfasttm(g, h->metatable, TM_MODE); markobjectN(g, h->metatable); if (mode && ttisstring(mode) && /* is there a weak mode? */ - ((weakkey = strchr(svalue(mode), 'k')), - (weakvalue = strchr(svalue(mode), 'v')), + (cast_void(weakkey = strchr(svalue(mode), 'k')), + cast_void(weakvalue = strchr(svalue(mode), 'v')), (weakkey || weakvalue))) { /* is really weak? */ black2gray(h); /* keep table gray */ if (!weakkey) /* strong keys? */ diff --git a/llex.c b/llex.c index 226127f69a..d99d9015b6 100644 --- a/llex.c +++ b/llex.c @@ -29,7 +29,7 @@ -#define next(ls) (ls->current = zgetc(ls->z)) +#define next(ls) (ls->current = zgetc(ls->z)) @@ -337,7 +337,7 @@ static unsigned long readutf8esc (LexState *ls) { save_and_next(ls); /* skip 'u' */ esccheck(ls, ls->current == '{', "missing '{'"); r = gethexa(ls); /* must have at least one digit */ - while ((save_and_next(ls), lisxdigit(ls->current))) { + while (cast_void(save_and_next(ls)), lisxdigit(ls->current)) { i++; esccheck(ls, r <= (0x7FFFFFFFu >> 4), "UTF-8 value too large"); r = (r << 4) + luaO_hexavalue(ls->current); diff --git a/lobject.c b/lobject.c index 979a68896e..2c265f964c 100644 --- a/lobject.c +++ b/lobject.c @@ -366,8 +366,8 @@ int luaO_utf8esc (char *buff, unsigned long x) { /* ** Convert a number object to a string, adding it to a buffer */ -static size_t tostringbuff (TValue *obj, char *buff) { - size_t len; +static int tostringbuff (TValue *obj, char *buff) { + int len; lua_assert(ttisnumber(obj)); if (ttisinteger(obj)) len = lua_integer2str(buff, MAXNUMBER2STR, ivalue(obj)); @@ -387,7 +387,7 @@ static size_t tostringbuff (TValue *obj, char *buff) { */ void luaO_tostring (lua_State *L, TValue *obj) { char buff[MAXNUMBER2STR]; - size_t len = tostringbuff(obj, buff); + int len = tostringbuff(obj, buff); setsvalue(L, obj, luaS_newlstr(L, buff, len)); } @@ -439,11 +439,11 @@ static void clearbuff (BuffFS *buff) { /* ** Get a space of size 'sz' in the buffer. If buffer has not enough -** space, empty it. 'sz' must fit in an empty space. +** space, empty it. 'sz' must fit in an empty buffer. */ -static char *getbuff (BuffFS *buff, size_t sz) { +static char *getbuff (BuffFS *buff, int sz) { lua_assert(buff->blen <= BUFVFS); lua_assert(sz <= BUFVFS); - if (sz > BUFVFS - cast_sizet(buff->blen)) /* string does not fit? */ + if (sz > BUFVFS - buff->blen) /* not enough space? */ clearbuff(buff); return buff->space + buff->blen; } @@ -458,9 +458,9 @@ static char *getbuff (BuffFS *buff, size_t sz) { */ static void addstr2buff (BuffFS *buff, const char *str, size_t slen) { if (slen <= BUFVFS) { /* does string fit into buffer? */ - char *bf = getbuff(buff, slen); + char *bf = getbuff(buff, cast_int(slen)); memcpy(bf, str, slen); /* add string to buffer */ - addsize(buff, slen); + addsize(buff, cast_int(slen)); } else { /* string larger than buffer */ clearbuff(buff); /* string comes after buffer's content */ @@ -474,7 +474,7 @@ static void addstr2buff (BuffFS *buff, const char *str, size_t slen) { */ static void addnum2buff (BuffFS *buff, TValue *num) { char *numbuff = getbuff(buff, MAXNUMBER2STR); - size_t len = tostringbuff(num, numbuff); /* format number into 'numbuff' */ + int len = tostringbuff(num, numbuff); /* format number into 'numbuff' */ addsize(buff, len); } diff --git a/lstring.c b/lstring.c index c52539a6d5..6ba798d9eb 100644 --- a/lstring.c +++ b/lstring.c @@ -121,8 +121,8 @@ void luaS_clearcache (global_State *g) { int i, j; for (i = 0; i < STRCACHE_N; i++) for (j = 0; j < STRCACHE_M; j++) { - if (iswhite(g->strcache[i][j])) /* will entry be collected? */ - g->strcache[i][j] = g->memerrmsg; /* replace it with something fixed */ + if (iswhite(g->strcache[i][j])) /* will entry be collected? */ + g->strcache[i][j] = g->memerrmsg; /* replace it with something fixed */ } } diff --git a/ltablib.c b/ltablib.c index 48a6bdfe27..7e7a101211 100644 --- a/ltablib.c +++ b/ltablib.c @@ -306,7 +306,7 @@ static IdxT partition (lua_State *L, IdxT lo, IdxT up) { } /* after the loop, a[i] >= P and a[lo .. i - 1] < P */ /* next loop: repeat --j while P < a[j] */ - while (lua_geti(L, 1, --j), sort_comp(L, -3, -1)) { + while ((void)lua_geti(L, 1, --j), sort_comp(L, -3, -1)) { if (j < i) /* j < i but a[j] > P ?? */ luaL_error(L, "invalid order function for sorting"); lua_pop(L, 1); /* remove a[j] */ diff --git a/lvm.c b/lvm.c index 45788a010e..d700079186 100644 --- a/lvm.c +++ b/lvm.c @@ -1151,7 +1151,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { TValue *rc = vRC(i); lua_Unsigned n; if (ttisinteger(rc) /* fast track for integers? */ - ? (n = ivalue(rc), luaV_fastgeti(L, rb, n, slot)) + ? (cast_void(n = ivalue(rc)), luaV_fastgeti(L, rb, n, slot)) : luaV_fastget(L, rb, rc, slot, luaH_get)) { setobj2s(L, ra, slot); } @@ -1204,7 +1204,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { TValue *rc = RKC(i); /* value */ lua_Unsigned n; if (ttisinteger(rb) /* fast track for integers? */ - ? (n = ivalue(rb), luaV_fastgeti(L, s2v(ra), n, slot)) + ? (cast_void(n = ivalue(rb)), luaV_fastgeti(L, s2v(ra), n, slot)) : luaV_fastget(L, s2v(ra), rb, slot, luaH_get)) { luaV_finishfastset(L, s2v(ra), slot, rc); } diff --git a/manual/manual.of b/manual/manual.of index ff69cd2c36..687a5b8972 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -1504,7 +1504,7 @@ There are two possible attributes: @id{const}, which declares a @x{constant variable}, that is, a variable that cannot be assigned to after its initialization; -and @id{toclose}, wich declares a to-be-closed variable @see{to-be-closed}. +and @id{toclose}, which declares a to-be-closed variable @see{to-be-closed}. A chunk is also a block @see{chunks}, @@ -1549,7 +1549,7 @@ the other pending closing methods will still be called. If a coroutine yields inside a block and is never resumed again, the variables visible at that block will never go out of scope, -and therefore they will not be closed. +and therefore they will never be closed. Similarly, if a coroutine ends with an error, it does not unwind its stack, so it does not close any variable. @@ -3432,7 +3432,6 @@ The new thread returned by this function shares with the original thread its global environment, but has an independent execution stack. -There is no explicit function to close or to destroy a thread. Threads are subject to garbage collection, like any Lua object. From 4a3fd8488d617aa633f6b8be85e662653b100a59 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 3 Jun 2019 12:13:13 -0300 Subject: [PATCH 0425/1145] bug in 5.4 alpha rc1: to-be-closed x vararg functions Closing methods must be run before correcting 'ci->func' when exiting a vararg function, to get correct debug information (e.g., in case of errors). --- lvm.c | 2 +- testes/locals.lua | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/lvm.c b/lvm.c index d700079186..5d0709ef94 100644 --- a/lvm.c +++ b/lvm.c @@ -1593,9 +1593,9 @@ void luaV_execute (lua_State *L, CallInfo *ci) { savepc(ci); if (TESTARG_k(i)) { int nparams1 = GETARG_C(i); + luaF_close(L, base, LUA_OK); /* there may be open upvalues */ if (nparams1) /* vararg function? */ ci->func -= ci->u.l.nextraargs + nparams1; - luaF_close(L, base, LUA_OK); /* there may be open upvalues */ } luaD_poscall(L, ci, n); return; diff --git a/testes/locals.lua b/testes/locals.lua index e59ab95a32..7834d7dac5 100644 --- a/testes/locals.lua +++ b/testes/locals.lua @@ -276,6 +276,15 @@ do -- errors in __close assert(msg == 1) assert(log[1] == 4 and log[2] == 3 and log[3] == 2 and log[4] == 2 and #log == 4) + + -- error in toclose in vararg function + function foo (...) + local x123 = 10 + end + + local st, msg = pcall(foo) + assert(string.find(msg, "'x123'")) + end From 514d94274853e6f0dfd6bb2ffa2e1fc64db926dd Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 3 Jun 2019 13:11:20 -0300 Subject: [PATCH 0426/1145] 'coroutine.kill' renamed 'coroutine.close' --- lcorolib.c | 6 +++--- manual/manual.of | 29 +++++++++++++++-------------- testes/coroutine.lua | 22 +++++++++++----------- 3 files changed, 29 insertions(+), 28 deletions(-) diff --git a/lcorolib.c b/lcorolib.c index 3fc9fb1c1f..156839e6e5 100644 --- a/lcorolib.c +++ b/lcorolib.c @@ -165,7 +165,7 @@ static int luaB_corunning (lua_State *L) { } -static int luaB_kill (lua_State *L) { +static int luaB_close (lua_State *L) { lua_State *co = getco(L); int status = auxstatus(L, co); switch (status) { @@ -182,7 +182,7 @@ static int luaB_kill (lua_State *L) { } } default: /* normal or running coroutine */ - return luaL_error(L, "cannot kill a %s coroutine", statname[status]); + return luaL_error(L, "cannot close a %s coroutine", statname[status]); } } @@ -195,7 +195,7 @@ static const luaL_Reg co_funcs[] = { {"wrap", luaB_cowrap}, {"yield", luaB_yield}, {"isyieldable", luaB_yieldable}, - {"kill", luaB_kill}, + {"close", luaB_close}, {NULL, NULL} }; diff --git a/manual/manual.of b/manual/manual.of index 687a5b8972..eb4e671d9c 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -864,7 +864,7 @@ Unlike @Lid{coroutine.resume}, the function created by @Lid{coroutine.wrap} propagates any error to the caller. In this case, -the function also kills the coroutine @seeF{coroutine.kill}. +the function also closes the coroutine @seeF{coroutine.close}. As an example of how coroutines work, consider the following code: @@ -1554,7 +1554,7 @@ Similarly, if a coroutine ends with an error, it does not unwind its stack, so it does not close any variable. You should either use finalizers -or call @Lid{coroutine.kill} to close the variables in these cases. +or call @Lid{coroutine.close} to close the variables in these cases. However, note that if the coroutine was created through @Lid{coroutine.wrap}, then its corresponding function will close all variables @@ -6351,6 +6351,18 @@ which come inside the table @defid{coroutine}. See @See{coroutine} for a general description of coroutines. +@LibEntry{coroutine.close (co)| + +Closes coroutine @id{co}, +that is, +closes all its pending to-be-closed variables +and puts the coroutine in a dead state. +In case of error closing some variable, +returns @false plus the error object; +otherwise returns @true. + +} + @LibEntry{coroutine.create (f)| Creates a new coroutine, with body @id{f}. @@ -6370,17 +6382,6 @@ it is not inside a non-yieldable @N{C function}. } -@LibEntry{coroutine.kill (co)| - -Kills coroutine @id{co}, -closing all its pending to-be-closed variables -and putting the coroutine in a dead state. -In case of error closing some variable, -returns @false plus the error object; -otherwise returns @true. - -} - @LibEntry{coroutine.resume (co [, val1, @Cdots])| Starts or continues the execution of coroutine @id{co}. @@ -6433,7 +6434,7 @@ extra arguments to @id{resume}. The function returns the same values returned by @id{resume}, except the first boolean. In case of error, -the function kills the coroutine and propagates the error. +the function closes the coroutine and propagates the error. } diff --git a/testes/coroutine.lua b/testes/coroutine.lua index db6d074ee1..198a58701d 100644 --- a/testes/coroutine.lua +++ b/testes/coroutine.lua @@ -123,23 +123,23 @@ assert(#a == 22 and a[#a] == 79) x, a = nil --- coroutine kill +-- coroutine closing do - -- ok to kill a dead coroutine + -- ok to close a dead coroutine local co = coroutine.create(print) - assert(coroutine.resume(co, "testing 'coroutine.kill'")) + assert(coroutine.resume(co, "testing 'coroutine.close'")) assert(coroutine.status(co) == "dead") - assert(coroutine.kill(co)) + assert(coroutine.close(co)) - -- cannot kill the running coroutine - local st, msg = pcall(coroutine.kill, coroutine.running()) + -- cannot close the running coroutine + local st, msg = pcall(coroutine.close, coroutine.running()) assert(not st and string.find(msg, "running")) local main = coroutine.running() - -- cannot kill a "normal" coroutine + -- cannot close a "normal" coroutine ;(coroutine.wrap(function () - local st, msg = pcall(coroutine.kill, main) + local st, msg = pcall(coroutine.close, main) assert(not st and string.find(msg, "normal")) end))() @@ -159,10 +159,10 @@ do end) coroutine.resume(co) assert(X) - assert(coroutine.kill(co)) + assert(coroutine.close(co)) assert(not X and coroutine.status(co) == "dead") - -- error killing a coroutine + -- error closing a coroutine co = coroutine.create(function() local x = func2close(function (self, err) assert(err == nil); error(111) @@ -170,7 +170,7 @@ do coroutine.yield() end) coroutine.resume(co) - local st, msg = coroutine.kill(co) + local st, msg = coroutine.close(co) assert(not st and coroutine.status(co) == "dead" and msg == 111) end From 14edd364c3abcb758e74c68a2bdd4ddaeefdae2a Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 4 Jun 2019 11:22:21 -0300 Subject: [PATCH 0427/1145] Function 'warn' is vararg Instead of a 'tocont' flag, the function 'warn' in Lua now receives all message pieces as multiple arguments in a single call. Besides being simpler to use, this implementation ensures that Lua code cannot create unfinished warnings. --- lbaselib.c | 15 +++++++++++++-- manual/manual.of | 8 +++----- testes/all.lua | 22 ++++++++++------------ testes/main.lua | 18 +++++++++++++++++- 4 files changed, 43 insertions(+), 20 deletions(-) diff --git a/lbaselib.c b/lbaselib.c index 83f61e6d11..4724e75990 100644 --- a/lbaselib.c +++ b/lbaselib.c @@ -37,9 +37,20 @@ static int luaB_print (lua_State *L) { } +/* +** Creates a warning with all given arguments. +** Check first for errors; otherwise an error may interrupt +** the composition of a warning, leaving it unfinished. +*/ static int luaB_warn (lua_State *L) { - const char *msg = luaL_checkstring(L, 1); - lua_warning(L, msg, lua_toboolean(L, 2)); + int n = lua_gettop(L); /* number of arguments */ + int i; + luaL_checkstring(L, 1); /* at least one argument */ + for (i = 2; i <= n; i++) + luaL_checkstring(L, i); /* make sure all arguments are strings */ + for (i = 1; i <= n; i++) /* compose warning */ + lua_warning(L, lua_tostring(L, i), 1); + lua_warning(L, "", 0); /* close warning */ return 0; } diff --git a/manual/manual.of b/manual/manual.of index eb4e671d9c..4c9c20b293 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -6326,12 +6326,10 @@ The current value of this variable is @St{Lua 5.4}. } -@LibEntry{warn (message [, tocont])| +@LibEntry{warn (msg1, @Cdots)| -Emits a warning with the given message. -A message in a call with @id{tocont} true should be -continued in another call to this function. -The default for @id{tocont} is false. +Emits a warning with a message composed by the concatenation +of all its arguments (which should be strings). } diff --git a/testes/all.lua b/testes/all.lua index 8d727b6b21..2e6fe0381c 100644 --- a/testes/all.lua +++ b/testes/all.lua @@ -5,8 +5,8 @@ local version = "Lua 5.4" if _VERSION ~= version then - warn(string.format( - "This test suite is for %s, not for %s\nExiting tests", version, _VERSION)) + warn("This test suite is for ", version, + ", not for ", _VERSION, "\nExiting tests") return end @@ -190,16 +190,13 @@ assert(dofile('verybig.lua', true) == 10); collectgarbage() dofile('files.lua') if #msgs > 0 then - warn("#tests not performed:", true) - for i=1,#msgs do - warn("\n ", true); warn(msgs[i], true) - end - warn("\n") + local m = table.concat(msgs, "\n ") + warn("#tests not performed:\n ", m, "\n") end print("(there should be two warnings now)") -warn("#This is ", true); warn("an expected", true); warn(" warning") -warn("#This is", true); warn(" another one") +warn("#This is ", "an expected", " warning") +warn("#This is", " another one") -- no test module should define 'debug' assert(debug == nil) @@ -216,9 +213,10 @@ _G.showmem = showmem end --) -local _G, showmem, print, format, clock, time, difftime, assert, open = +local _G, showmem, print, format, clock, time, difftime, + assert, open, warn = _G, showmem, print, string.format, os.clock, os.time, os.difftime, - assert, io.open + assert, io.open, warn -- file with time of last performed test local fname = T and "time-debug.txt" or "time.txt" @@ -262,7 +260,7 @@ if not usertests then local diff = (clocktime - lasttime) / lasttime local tolerance = 0.05 -- 5% if (diff >= tolerance or diff <= -tolerance) then - print(format("WARNING: time difference from previous test: %+.1f%%", + warn(format("#time difference from previous test: %+.1f%%", diff * 100)) end assert(open(fname, "w")):write(clocktime):close() diff --git a/testes/main.lua b/testes/main.lua index 47d84d4ced..4c09645a3c 100644 --- a/testes/main.lua +++ b/testes/main.lua @@ -347,10 +347,26 @@ NoRun("syntax error", "lua -e a") NoRun("'-l' needs argument", "lua -l") -if T then -- auxiliary library? +if T then -- test library? print("testing 'not enough memory' to create a state") NoRun("not enough memory", "env MEMLIMIT=100 lua") + + -- testing 'warn' + warn("@123", "456", "789") + assert(_WARN == "@123456789") end + +do + -- 'warn' must get at least one argument + local st, msg = pcall(warn) + assert(string.find(msg, "string expected")) + + -- 'warn' does not leave unfinished warning in case of errors + -- (message would appear in next warning) + st, msg = pcall(warn, "SHOULD NOT APPEAR", {}) + assert(string.find(msg, "string expected")) +end + print('+') print('testing Ctrl C') From b4d5dff8ec4f1c8a44db66d368e95d359b04aea7 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 5 Jun 2019 13:16:25 -0300 Subject: [PATCH 0428/1145] Multiple errors in '__toclose' report the first one When there are multiple errors when closing objects, the error reported by the protected call is the first one, for two reasons: First, other errors may be caused by this one; second, the first error is handled in the original execution context, and therefore has the full traceback. --- lcorolib.c | 7 ++----- lfunc.c | 9 ++++++--- manual/manual.of | 21 +++++++++++++++------ testes/coroutine.lua | 10 +++++++++- testes/locals.lua | 37 ++++++++++++++++++++++++------------- 5 files changed, 56 insertions(+), 28 deletions(-) diff --git a/lcorolib.c b/lcorolib.c index 156839e6e5..4d47ea28ac 100644 --- a/lcorolib.c +++ b/lcorolib.c @@ -75,11 +75,8 @@ static int luaB_auxwrap (lua_State *L) { int r = auxresume(L, co, lua_gettop(L)); if (r < 0) { int stat = lua_status(co); - if (stat != LUA_OK && stat != LUA_YIELD) { - stat = lua_resetthread(co); /* close variables in case of errors */ - if (stat != LUA_OK) /* error closing variables? */ - lua_xmove(co, L, 1); /* get new error object */ - } + if (stat != LUA_OK && stat != LUA_YIELD) + lua_resetthread(co); /* close variables in case of errors */ if (lua_type(L, -1) == LUA_TSTRING) { /* error object is a string? */ luaL_where(L, 1); /* add extra info, if available */ lua_insert(L, -2); diff --git a/lfunc.c b/lfunc.c index 3e044b65b2..551149927f 100644 --- a/lfunc.c +++ b/lfunc.c @@ -144,13 +144,16 @@ static int callclosemth (lua_State *L, TValue *uv, StkId level, int status) { luaG_runerror(L, "attempt to close non-closable variable '%s'", vname); } } - else { /* there was an error */ + else { /* must close the object in protected mode */ + ptrdiff_t oldtop = savestack(L, level + 1); /* save error message and set stack top to 'level + 1' */ luaD_seterrorobj(L, status, level); if (prepclosingmethod(L, uv, s2v(level))) { /* something to call? */ - int newstatus = luaD_pcall(L, callclose, NULL, savestack(L, level), 0); - if (newstatus != LUA_OK) /* another error when closing? */ + int newstatus = luaD_pcall(L, callclose, NULL, oldtop, 0); + if (newstatus != LUA_OK && status == CLOSEPROTECT) /* first error? */ status = newstatus; /* this will be the new error */ + else /* leave original error (or nil) on top */ + L->top = restorestack(L, oldtop); } /* else no metamethod; ignore this case and keep original error */ } diff --git a/manual/manual.of b/manual/manual.of index 4c9c20b293..2c0957b9db 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -1541,11 +1541,17 @@ if there was no error, the second argument is @nil. If several to-be-closed variables go out of scope at the same event, they are closed in the reverse order that they were declared. + If there is any error while running a closing method, that error is handled like an error in the regular code where the variable was defined; in particular, the other pending closing methods will still be called. +After an error, +other errors in closing methods +interrupt the respective method, +but are otherwise ignored; +the error reported is the original one. If a coroutine yields inside a block and is never resumed again, the variables visible at that block will never go out of scope, @@ -1553,11 +1559,12 @@ and therefore they will never be closed. Similarly, if a coroutine ends with an error, it does not unwind its stack, so it does not close any variable. -You should either use finalizers -or call @Lid{coroutine.close} to close the variables in these cases. -However, note that if the coroutine was created +In both cases, +you should either use finalizers +or call @Lid{coroutine.close} to close the variables. +However, if the coroutine was created through @Lid{coroutine.wrap}, -then its corresponding function will close all variables +then its corresponding function will close the coroutine in case of errors. } @@ -3932,7 +3939,7 @@ Returns a status code: @Lid{LUA_OK} for no errors in closing methods, or an error status otherwise. In case of error, -leave the error object on the stack, +leaves the error object on the top of the stack, } @@ -6355,6 +6362,7 @@ Closes coroutine @id{co}, that is, closes all its pending to-be-closed variables and puts the coroutine in a dead state. +The given coroutine must be dead or suspended. In case of error closing some variable, returns @false plus the error object; otherwise returns @true. @@ -6412,7 +6420,8 @@ true when the running coroutine is the main one. Returns the status of the coroutine @id{co}, as a string: @T{"running"}, -if the coroutine is running (that is, it called @id{status}); +if the coroutine is running +(that is, it is the one that called @id{status}); @T{"suspended"}, if the coroutine is suspended in a call to @id{yield}, or if it has not started running yet; @T{"normal"} if the coroutine is active but not running diff --git a/testes/coroutine.lua b/testes/coroutine.lua index 198a58701d..f2c0da8bdf 100644 --- a/testes/coroutine.lua +++ b/testes/coroutine.lua @@ -163,15 +163,23 @@ do assert(not X and coroutine.status(co) == "dead") -- error closing a coroutine + local x = 0 co = coroutine.create(function() + local y = func2close(function (self,err) + if (err ~= 111) then os.exit(false) end -- should not happen + x = 200 + error(200) + end) local x = func2close(function (self, err) assert(err == nil); error(111) end) coroutine.yield() end) coroutine.resume(co) + assert(x == 0) local st, msg = coroutine.close(co) - assert(not st and coroutine.status(co) == "dead" and msg == 111) + assert(st == false and coroutine.status(co) == "dead" and msg == 111) + assert(x == 200) end diff --git a/testes/locals.lua b/testes/locals.lua index 7834d7dac5..dccda28fe9 100644 --- a/testes/locals.lua +++ b/testes/locals.lua @@ -267,14 +267,14 @@ do -- errors in __close if err then error(4) end end local stat, msg = pcall(foo, false) - assert(msg == 1) - assert(log[1] == 10 and log[2] == 3 and log[3] == 2 and log[4] == 2 + assert(msg == 3) + assert(log[1] == 10 and log[2] == 3 and log[3] == 3 and log[4] == 3 and #log == 4) log = {} local stat, msg = pcall(foo, true) - assert(msg == 1) - assert(log[1] == 4 and log[2] == 3 and log[3] == 2 and log[4] == 2 + assert(msg == 4) + assert(log[1] == 4 and log[2] == 4 and log[3] == 4 and log[4] == 4 and #log == 4) -- error in toclose in vararg function @@ -317,7 +317,7 @@ if rawget(_G, "T") then local x = setmetatable({}, {__close = function () T.alloccount(0); local x = {} -- force a memory error end}) - error("a") -- common error inside the function's body + error(1000) -- common error inside the function's body end stack(5) -- ensure a minimal number of CI structures @@ -325,7 +325,7 @@ if rawget(_G, "T") then -- despite memory error, 'y' will be executed and -- memory limit will be lifted local _, msg = pcall(foo) - assert(msg == "not enough memory") + assert(msg == 1000) local close = func2close(function (self, msg) T.alloccount() @@ -368,8 +368,7 @@ if rawget(_G, "T") then end local _, msg = pcall(test) - assert(msg == 1000) - + assert(msg == "not enough memory") -- reported error is the first one do -- testing 'toclose' in C string buffer collectgarbage() @@ -453,15 +452,27 @@ end do -- error in a wrapped coroutine raising errors when closing a variable - local x = false + local x = 0 local co = coroutine.wrap(function () - local xv = func2close(function () error("XXX") end) + local xx = func2close(function () x = x + 1; error("YYY") end) + local xv = func2close(function () x = x + 1; error("XXX") end) coroutine.yield(100) error(200) end) - assert(co() == 100) - local st, msg = pcall(co) - -- should get last error raised + assert(co() == 100); assert(x == 0) + local st, msg = pcall(co); assert(x == 2) + assert(not st and msg == 200) -- should get first error raised + + x = 0 + co = coroutine.wrap(function () + local xx = func2close(function () x = x + 1; error("YYY") end) + local xv = func2close(function () x = x + 1; error("XXX") end) + coroutine.yield(100) + return 200 + end) + assert(co() == 100); assert(x == 0) + local st, msg = pcall(co); assert(x == 2) + -- should get first error raised assert(not st and string.find(msg, "%w+%.%w+:%d+: XXX")) end From 6aeaeb5656a006ad95b35dd7482798fdc5f02f5e Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 5 Jun 2019 13:21:16 -0300 Subject: [PATCH 0429/1145] Detail in makefile --- makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/makefile b/makefile index 3bd319bea9..cb6cece86d 100644 --- a/makefile +++ b/makefile @@ -40,7 +40,7 @@ CWARNS= $(CWARNSCPP) $(CWARNSC) # -DEXTERNMEMCHECK -DHARDSTACKTESTS -DHARDMEMTESTS -DTRACEMEM='"tempmem"' -# -DMAXINDEXRK=1 +# -DMAXINDEXRK=1 -DLUA_COMPAT_5_3 # -g -DLUA_USER_H='"ltests.h"' # -pg -malign-double # -DLUA_USE_CTYPE -DLUA_USE_APICHECK From f39e8c06d61078467b3f32499728ed4e9b7b06bc Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 6 Jun 2019 12:51:41 -0300 Subject: [PATCH 0430/1145] Updated the documentation for the API function 'lua_gc' --- manual/manual.of | 115 +++++++++++++++++++++++++---------------------- 1 file changed, 62 insertions(+), 53 deletions(-) diff --git a/manual/manual.of b/manual/manual.of index 2c0957b9db..fd49b404b1 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -575,7 +575,7 @@ or @Lid{collectgarbage} in Lua. You can also use these functions to control the collector directly (e.g., to stop and restart it). -@sect3{@title{Incremental Garbage Collection} +@sect3{incmode| @title{Incremental Garbage Collection} In incremental mode, each GC cycle performs a mark-and-sweep collection in small steps @@ -624,7 +624,7 @@ which means steps of approximately @N{8 Kbytes}. } -@sect3{@title{Generational Garbage Collection} +@sect3{genmode| @title{Generational Garbage Collection} In generational mode, the collector does frequent @emph{minor} collections, @@ -633,17 +633,7 @@ If after a minor collection the use of memory is still above a limit, the collector does a @emph{major} collection, which traverses all objects. The generational mode uses two parameters: -the @def{major multiplier} and the @def{the minor multiplier}. - -The major multiplier controls the frequency of major collections. -For a major multiplier @M{x}, -a new major collection will be done when memory -grows @M{x%} larger than the memory in use after the previous major -collection. -For instance, for a multiplier of 100, -the collector will do a major collection when the use of memory -gets larger than twice the use after the previous collection. -The default value is 100; the maximum value is 1000. +the @def{minor multiplier} and the @def{the major multiplier}. The minor multiplier controls the frequency of minor collections. For a minor multiplier @M{x}, @@ -655,6 +645,16 @@ the collector will do a minor collection when the use of memory gets 20% larger than the use after the previous major collection. The default value is 20; the maximum value is 200. +The major multiplier controls the frequency of major collections. +For a major multiplier @M{x}, +a new major collection will be done when memory +grows @M{x%} larger than the memory in use after the previous major +collection. +For instance, for a multiplier of 100, +the collector will do a major collection when the use of memory +gets larger than twice the use after the previous collection. +The default value is 100; the maximum value is 1000. + } @sect3{finalizers| @title{Garbage-Collection Metamethods} @@ -3022,55 +3022,58 @@ and therefore never returns } -@APIEntry{int lua_gc (lua_State *L, int what, int data);| +@APIEntry{int lua_gc (lua_State *L, int what, ...);| @apii{0,0,-} Controls the garbage collector. This function performs several tasks, -according to the value of the parameter @id{what}: +according to the value of the parameter @id{what}. +For options that need extra arguments, +they are listed after the option. @description{ -@item{@id{LUA_GCSTOP}| -stops the garbage collector. +@item{@id{LUA_GCCOLLECT}| +Performs a full garbage-collection cycle. } -@item{@id{LUA_GCRESTART}| -restarts the garbage collector. +@item{@id{LUA_GCSTOP}| +Stops the garbage collector. } -@item{@id{LUA_GCCOLLECT}| -performs a full garbage-collection cycle. +@item{@id{LUA_GCRESTART}| +Restarts the garbage collector. } @item{@id{LUA_GCCOUNT}| -returns the current amount of memory (in Kbytes) in use by Lua. +Returns the current amount of memory (in Kbytes) in use by Lua. } @item{@id{LUA_GCCOUNTB}| -returns the remainder of dividing the current amount of bytes of +Returns the remainder of dividing the current amount of bytes of memory in use by Lua by 1024. } -@item{@id{LUA_GCSTEP}| -performs an incremental step of garbage collection. +@item{@id{LUA_GCSTEP} @T{(int stepsize)}| +Performs an incremental step of garbage collection, +corresponding to the allocation of @id{stepsize} Kbytes. } -@item{@id{LUA_GCSETPAUSE}| -sets @id{data} as the new value -for the @emph{pause} of the collector @see{GC} -and returns the previous value of the pause. +@item{@id{LUA_GCISRUNNING}| +Returns a boolean that tells whether the collector is running +(i.e., not stopped). } -@item{@id{LUA_GCSETSTEPMUL}| -sets @id{data} as the new value for the @emph{step multiplier} of -the collector @see{GC} -and returns the previous value of the step multiplier. +@item{@id{LUA_GCINC} (int pause, int stepmul, stepsize)| +Changes the collector to incremental mode +with the given parameters @see{incmode}. +Returns the previous mode (@id{LUA_GCGEN} or @id{LUA_GCINC}). } -@item{@id{LUA_GCISRUNNING}| -returns a boolean that tells whether the collector is running -(i.e., not stopped). +@item{@id{LUA_GCGEN} (int minormul, int majormul)| +Changes the collector to generational mode +with the given parameters @see{genmode}. +Returns the previous mode (@id{LUA_GCGEN} or @id{LUA_GCINC}). } } @@ -5949,42 +5952,41 @@ It performs different functions according to its first argument, @id{opt}: @description{ @item{@St{collect}| -performs a full garbage-collection cycle. +Performs a full garbage-collection cycle. This is the default option. } @item{@St{stop}| -stops automatic execution of the garbage collector. +Stops automatic execution of the garbage collector. The collector will run only when explicitly invoked, until a call to restart it. } @item{@St{restart}| -restarts automatic execution of the garbage collector. +Restarts automatic execution of the garbage collector. } @item{@St{count}| -returns the total memory in use by Lua in Kbytes. +Returns the total memory in use by Lua in Kbytes. The value has a fractional part, so that it multiplied by 1024 gives the exact number of bytes in use by Lua. } @item{@St{step}| -performs a garbage-collection step. +Performs a garbage-collection step. The step @Q{size} is controlled by @id{arg}. With a zero value, the collector will perform one basic (indivisible) step. For non-zero values, the collector will perform as if that amount of memory -(in KBytes) had been allocated by Lua. +(in Kbytes) had been allocated by Lua. Returns @true if the step finished a collection cycle. } -@item{@St{setpause}| -sets @id{arg} as the new value for the @emph{pause} of -the collector @see{GC}. -Returns the previous value for @emph{pause}. +@item{@St{isrunning}| +Returns a boolean that tells whether the collector is running +(i.e., not stopped). } @item{@St{incremental}| @@ -5992,7 +5994,7 @@ Change the collector mode to incremental. This option can be followed by three numbers: the garbage-collector pause, the step multiplier, -and the step size. +and the step size @see{incmode}. A zero means to not change that value. } @@ -6000,15 +6002,10 @@ A zero means to not change that value. Change the collector mode to generational. This option can be followed by two numbers: the garbage-collector minor multiplier -and the major multiplier. +and the major multiplier @see{genmode}. A zero means to not change that value. } -@item{@St{isrunning}| -returns a boolean that tells whether the collector is running -(i.e., not stopped). -} - } See @See{GC} for more details about garbage collection and some of these options. @@ -8880,6 +8877,12 @@ do not accept surrogates as valid code points. An extra parameter in these functions makes them more permissive. } +@item{ +The options @St{setpause} and @St{setstepmul} +of the function @Lid{collectgarbage} are deprecated. +You should use the new option @St{incremental} to set them. +} + } } @@ -8925,6 +8928,12 @@ Errors in finalizers are never propagated; instead, they generate a warning. } +@item{ +The options @idx{LUA_GCSETPAUSE} and @idx{LUA_GCSETSTEPMUL} +of the function @Lid{lua_gc} are deprecated. +You should use the new option @id{LUA_GCINC} to set them. +} + } } From d2a9b4ffb86de29a201843edddfc0153a1846f96 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 10 Jun 2019 13:59:19 -0300 Subject: [PATCH 0431/1145] Detail in the manual More precision describing the variables that won't be closed if a coroutine yields forever. --- manual/manual.of | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/manual/manual.of b/manual/manual.of index fd49b404b1..725b12ad85 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -1553,9 +1553,11 @@ interrupt the respective method, but are otherwise ignored; the error reported is the original one. -If a coroutine yields inside a block and is never resumed again, -the variables visible at that block will never go out of scope, +If a coroutine yields and is never resumed again, +some variables may never go out of scope, and therefore they will never be closed. +(These variables are the ones created inside the coroutine +and in scope at the point where the coroutine yielded.) Similarly, if a coroutine ends with an error, it does not unwind its stack, so it does not close any variable. @@ -2245,9 +2247,9 @@ Consider the following example: @verbatim{ a = {} local x = 20 -for i=1,10 do +for i = 1, 10 do local y = 0 - a[i] = function () y=y+1; return x+y end + a[i] = function () y = y + 1; return x + y end end } The loop creates ten closures @@ -6815,7 +6817,6 @@ A value of @true as a fourth, optional argument @id{plain} turns off the pattern matching facilities, so the function does a plain @Q{find substring} operation, with no characters in @id{pattern} being considered magic. -Note that if @id{plain} is given, then @id{init} must be given as well. If the pattern has captures, then in a successful match From 3cd9b56ae6002b4ef28d2467abd119606ae625d3 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 12 Jun 2019 10:31:38 -0300 Subject: [PATCH 0432/1145] Revamp around 'L->nCcalls' count The field 'L->nCcalls' now counts downwards, so that the C-stack limits do not depend on the stack size. --- ldo.c | 13 ++++++------- lstate.c | 53 +++++++++++++++++++++++++------------------------- lstate.h | 51 ++++++++++++++++++++++++++++++++---------------- testes/all.lua | 14 +++++++++++++ 4 files changed, 80 insertions(+), 51 deletions(-) diff --git a/ldo.c b/ldo.c index e7e76a652a..0ad3120ba0 100644 --- a/ldo.c +++ b/ldo.c @@ -139,9 +139,8 @@ l_noret luaD_throw (lua_State *L, int errcode) { int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) { - l_uint32 oldnCcalls = L->nCcalls - L->nci; + l_uint32 oldnCcalls = L->nCcalls + L->nci; struct lua_longjmp lj; - lua_assert(L->nCcalls >= L->nci); lj.status = LUA_OK; lj.previous = L->errorJmp; /* chain new error handler */ L->errorJmp = &lj; @@ -149,7 +148,7 @@ int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) { (*f)(L, ud); ); L->errorJmp = lj.previous; /* restore old error handler */ - L->nCcalls = oldnCcalls + L->nci; + L->nCcalls = oldnCcalls - L->nci; return lj.status; } @@ -521,7 +520,7 @@ void luaD_call (lua_State *L, StkId func, int nresults) { */ void luaD_callnoyield (lua_State *L, StkId func, int nResults) { incXCcalls(L); - if (getCcalls(L) >= LUAI_MAXCSTACK) /* possible stack overflow? */ + if (getCcalls(L) <= CSTACKERR) /* possible stack overflow? */ luaE_freeCI(L); luaD_call(L, func, nResults); decXCcalls(L); @@ -672,10 +671,10 @@ LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs, else if (L->status != LUA_YIELD) /* ended with errors? */ return resume_error(L, "cannot resume dead coroutine", nargs); if (from == NULL) - L->nCcalls = 1; + L->nCcalls = LUAI_MAXCSTACK; else /* correct 'nCcalls' for this thread */ - L->nCcalls = getCcalls(from) - from->nci + L->nci + CSTACKCF; - if (L->nCcalls >= LUAI_MAXCSTACK) + L->nCcalls = getCcalls(from) + from->nci - L->nci - CSTACKCF; + if (L->nCcalls <= CSTACKERR) return resume_error(L, "C stack overflow", nargs); luai_userstateresume(L, nargs); api_checknelems(L, (L->status == LUA_OK) ? nargs + 1 : nargs); diff --git a/lstate.c b/lstate.c index 387cd362dc..296cec2ad1 100644 --- a/lstate.c +++ b/lstate.c @@ -97,35 +97,34 @@ void luaE_setdebt (global_State *g, l_mem debt) { /* -** Increment count of "C calls" and check for overflows. In case of +** Decrement count of "C calls" and check for overflows. In case of ** a stack overflow, check appropriate error ("regular" overflow or -** overflow while handling stack overflow). -** If 'nCcalls' is larger than LUAI_MAXCSTACK but smaller than -** LUAI_MAXCSTACK + CSTACKCF (plus 2 to avoid by-one errors), it means -** it has just entered the "overflow zone", so the function raises an -** overflow error. -** If 'nCcalls' is larger than LUAI_MAXCSTACK + CSTACKCF + 2 -** (which means it is already handling an overflow) but smaller than -** 9/8 of LUAI_MAXCSTACK, does not report an error (to allow message -** handling to work). -** Otherwise, report a stack overflow while handling a stack overflow -** (probably caused by a repeating error in the message handling -** function). +** overflow while handling stack overflow). If 'nCcalls' is smaller +** than CSTACKERR but larger than CSTACKMARK, it means it has just +** entered the "overflow zone", so the function raises an overflow +** error. If 'nCcalls' is smaller than CSTACKMARK (which means it is +** already handling an overflow) but larger than CSTACKERRMARK, does +** not report an error (to allow message handling to work). Otherwise, +** report a stack overflow while handling a stack overflow (probably +** caused by a repeating error in the message handling function). */ + void luaE_enterCcall (lua_State *L) { int ncalls = getCcalls(L); - L->nCcalls++; - if (ncalls >= LUAI_MAXCSTACK) { /* possible overflow? */ + L->nCcalls--; + if (ncalls <= CSTACKERR) { /* possible overflow? */ luaE_freeCI(L); /* release unused CIs */ ncalls = getCcalls(L); /* update call count */ - if (ncalls >= LUAI_MAXCSTACK) { /* still overflow? */ - if (ncalls <= LUAI_MAXCSTACK + CSTACKCF + 2) { - /* no error before increments; raise the error now */ - L->nCcalls += (CSTACKCF + 4); /* avoid raising it again */ - luaG_runerror(L, "C stack overflow"); - } - else if (ncalls >= (LUAI_MAXCSTACK + (LUAI_MAXCSTACK >> 3))) + if (ncalls <= CSTACKERR) { /* still overflow? */ + if (ncalls <= CSTACKERRMARK) /* below error-handling zone? */ luaD_throw(L, LUA_ERRERR); /* error while handling stack error */ + else if (ncalls >= CSTACKMARK) { + /* not in error-handling zone; raise the error now */ + L->nCcalls = (CSTACKMARK - 1); /* enter error-handling zone */ + luaG_runerror(L, "C stack overflow1"); + } + /* else stack is in the error-handling zone; + allow message handler to work */ } } } @@ -153,13 +152,13 @@ void luaE_freeCI (lua_State *L) { CallInfo *ci = L->ci; CallInfo *next = ci->next; ci->next = NULL; - L->nCcalls -= L->nci; /* subtract removed elements from 'nCcalls' */ + L->nCcalls += L->nci; /* add removed elements back to 'nCcalls' */ while ((ci = next) != NULL) { next = ci->next; luaM_free(L, ci); L->nci--; } - L->nCcalls += L->nci; /* adjust result */ + L->nCcalls -= L->nci; /* adjust result */ } @@ -169,7 +168,7 @@ void luaE_freeCI (lua_State *L) { void luaE_shrinkCI (lua_State *L) { CallInfo *ci = L->ci; CallInfo *next2; /* next's next */ - L->nCcalls -= L->nci; /* subtract removed elements from 'nCcalls' */ + L->nCcalls += L->nci; /* add removed elements back to 'nCcalls' */ /* while there are two nexts */ while (ci->next != NULL && (next2 = ci->next->next) != NULL) { luaM_free(L, ci->next); /* free next */ @@ -178,7 +177,7 @@ void luaE_shrinkCI (lua_State *L) { next2->previous = ci; ci = next2; /* keep next's next */ } - L->nCcalls += L->nci; /* adjust result */ + L->nCcalls -= L->nci; /* adjust result */ } @@ -264,7 +263,7 @@ static void preinit_thread (lua_State *L, global_State *g) { L->stacksize = 0; L->twups = L; /* thread has no upvalues */ L->errorJmp = NULL; - L->nCcalls = 0; + L->nCcalls = LUAI_MAXCSTACK + CSTACKERR; L->hook = NULL; L->hookmask = 0; L->basehookcount = 0; diff --git a/lstate.h b/lstate.h index 3bd5297334..858da5bec8 100644 --- a/lstate.h +++ b/lstate.h @@ -64,28 +64,45 @@ /* ** About 'nCcalls': each thread in Lua (a lua_State) keeps a count of -** how many "C calls" it can do in the C stack, to avoid C-stack overflow. -** This count is very rough approximation; it considers only recursive -** functions inside the interpreter, as non-recursive calls can be -** considered using a fixed (although unknown) amount of stack space. +** how many "C calls" it still can do in the C stack, to avoid C-stack +** overflow. This count is very rough approximation; it considers only +** recursive functions inside the interpreter, as non-recursive calls +** can be considered using a fixed (although unknown) amount of stack +** space. ** -** The count itself has two parts: the lower part is the count itself; -** the higher part counts the number of non-yieldable calls in the stack. +** The count has two parts: the lower part is the count itself; the +** higher part counts the number of non-yieldable calls in the stack. +** (They are together so that we can change both with one instruction.) ** ** Because calls to external C functions can use of unkown amount ** of space (e.g., functions using an auxiliary buffer), calls -** to these functions add more than one to the count. +** to these functions add more than one to the count (see CSTACKCF). ** -** The proper count also includes the number of CallInfo structures -** allocated by Lua, as a kind of "potential" calls. So, when Lua -** calls a function (and "consumes" one CallInfo), it needs neither to -** increment nor to check 'nCcalls', as its use of C stack is already -** accounted for. +** The proper count excludes the number of CallInfo structures allocated +** by Lua, as a kind of "potential" calls. So, when Lua calls a function +** (and "consumes" one CallInfo), it needs neither to decrement nor to +** check 'nCcalls', as its use of C stack is already accounted for. */ /* number of "C stack slots" used by an external C function */ #define CSTACKCF 10 + +/* +** The C-stack size is sliced in the following zones: +** - larger than CSTACKERR: normal stack; +** - [CSTACKMARK, CSTACKERR]: buffer zone to signal a stack overflow; +** - [CSTACKCF, CSTACKERRMARK]: error-handling zone; +** - below CSTACKERRMARK: buffer zone to signal overflow during overflow; +** (Because the counter can be decremented CSTACKCF at once, we need +** the so called "buffer zones", with at least that size, to properly +** detect a change from one zone to the next.) +*/ +#define CSTACKERR (8 * CSTACKCF) +#define CSTACKMARK (CSTACKERR - (CSTACKCF + 2)) +#define CSTACKERRMARK (CSTACKCF + 2) + + /* true if this thread does not have non-yieldable calls in the stack */ #define yieldable(L) (((L)->nCcalls & 0xffff0000) == 0) @@ -99,11 +116,11 @@ /* Decrement the number of non-yieldable calls */ #define decnny(L) ((L)->nCcalls -= 0x10000) -/* Increment the number of non-yieldable calls and nCcalls */ -#define incXCcalls(L) ((L)->nCcalls += 0x10000 + CSTACKCF) +/* Increment the number of non-yieldable calls and decrement nCcalls */ +#define incXCcalls(L) ((L)->nCcalls += 0x10000 - CSTACKCF) -/* Decrement the number of non-yieldable calls and nCcalls */ -#define decXCcalls(L) ((L)->nCcalls -= 0x10000 + CSTACKCF) +/* Decrement the number of non-yieldable calls and increment nCcalls */ +#define decXCcalls(L) ((L)->nCcalls -= 0x10000 - CSTACKCF) @@ -336,7 +353,7 @@ LUAI_FUNC void luaE_enterCcall (lua_State *L); LUAI_FUNC void luaE_warning (lua_State *L, const char *msg, int tocont); -#define luaE_exitCcall(L) ((L)->nCcalls--) +#define luaE_exitCcall(L) ((L)->nCcalls++) #endif diff --git a/testes/all.lua b/testes/all.lua index 2e6fe0381c..72121e8d08 100644 --- a/testes/all.lua +++ b/testes/all.lua @@ -95,6 +95,8 @@ local function F (m) end end +local Cstacklevel + local showmem if not T then local max = 0 @@ -104,6 +106,7 @@ if not T then print(format(" ---- total memory: %s, max memory: %s ----\n", F(m), F(max))) end + Cstacklevel = function () return 0 end -- no info about stack level else showmem = function () T.checkmemory() @@ -117,9 +120,16 @@ else T.totalmem"string", T.totalmem"table", T.totalmem"function", T.totalmem"userdata", T.totalmem"thread")) end + + Cstacklevel = function () + local _, _, ncalls, nci = T.stacklevel() + return ncalls + nci -- number of free slots in the C stack + end end +local Cstack = Cstacklevel() + -- -- redefine dofile to run files through dump/undump -- @@ -211,6 +221,10 @@ debug.sethook(function (a) assert(type(a) == 'string') end, "cr") -- to survive outside block _G.showmem = showmem + +assert(Cstack == Cstacklevel(), + "should be at the same C-stack level it was when started the tests") + end --) local _G, showmem, print, format, clock, time, difftime, From be73f72fcc944a8ebae2c60d2ce84139acb011b9 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 18 Jun 2019 16:52:22 -0300 Subject: [PATCH 0433/1145] New function 'setCstacklimit' Added new functions to dynamically set the C-stack limit ('lua_setCstacklimit' in the C-API, 'debug.setCstacklimit' in Lua). --- ldblib.c | 12 ++++++++++++ ldo.c | 7 ++++--- lstate.c | 28 +++++++++++++++++++++++++-- lstate.h | 5 +++++ ltests.h | 2 +- lua.h | 1 + manual/manual.of | 42 +++++++++++++++++++++++++++++++++++++++++ testes/cstack.lua | 48 +++++++++++++++++++++++++++++++++++++++++++++++ testes/errors.lua | 16 ++++++++++------ 9 files changed, 149 insertions(+), 12 deletions(-) diff --git a/ldblib.c b/ldblib.c index d045a82e22..513a13cb6e 100644 --- a/ldblib.c +++ b/ldblib.c @@ -437,6 +437,17 @@ static int db_traceback (lua_State *L) { } +static int db_setCstacklimit (lua_State *L) { + int limit = (int)luaL_checkinteger(L, 1); + int res = lua_setCstacklimit(L, limit); + if (res == 0) + lua_pushboolean(L, 0); + else + lua_pushinteger(L, res); + return 1; +} + + static const luaL_Reg dblib[] = { {"debug", db_debug}, {"getuservalue", db_getuservalue}, @@ -454,6 +465,7 @@ static const luaL_Reg dblib[] = { {"setmetatable", db_setmetatable}, {"setupvalue", db_setupvalue}, {"traceback", db_traceback}, + {"setCstacklimit", db_setCstacklimit}, {NULL, NULL} }; diff --git a/ldo.c b/ldo.c index 0ad3120ba0..1a327ffd46 100644 --- a/ldo.c +++ b/ldo.c @@ -139,7 +139,8 @@ l_noret luaD_throw (lua_State *L, int errcode) { int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) { - l_uint32 oldnCcalls = L->nCcalls + L->nci; + global_State *g = G(L); + l_uint32 oldnCcalls = g->Cstacklimit - (L->nCcalls + L->nci); struct lua_longjmp lj; lj.status = LUA_OK; lj.previous = L->errorJmp; /* chain new error handler */ @@ -148,7 +149,7 @@ int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) { (*f)(L, ud); ); L->errorJmp = lj.previous; /* restore old error handler */ - L->nCcalls = oldnCcalls - L->nci; + L->nCcalls = g->Cstacklimit - oldnCcalls - L->nci; return lj.status; } @@ -671,7 +672,7 @@ LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs, else if (L->status != LUA_YIELD) /* ended with errors? */ return resume_error(L, "cannot resume dead coroutine", nargs); if (from == NULL) - L->nCcalls = LUAI_MAXCSTACK; + L->nCcalls = CSTACKTHREAD; else /* correct 'nCcalls' for this thread */ L->nCcalls = getCcalls(from) + from->nci - L->nci - CSTACKCF; if (L->nCcalls <= CSTACKERR) diff --git a/lstate.c b/lstate.c index 296cec2ad1..92d5165af5 100644 --- a/lstate.c +++ b/lstate.c @@ -96,6 +96,29 @@ void luaE_setdebt (global_State *g, l_mem debt) { } +LUA_API int lua_setCstacklimit (lua_State *L, unsigned int limit) { + global_State *g = G(L); + int ccalls; + luaE_freeCI(L); /* release unused CIs */ + ccalls = getCcalls(L); + if (limit >= 40000) + return 0; /* out of bounds */ + limit += CSTACKERR; + if (L != g-> mainthread) + return 0; /* only main thread can change the C stack */ + else if (ccalls <= CSTACKERR) + return 0; /* handling overflow */ + else { + int diff = limit - g->Cstacklimit; + if (ccalls + diff <= CSTACKERR) + return 0; /* new limit would cause an overflow */ + g->Cstacklimit = limit; /* set new limit */ + L->nCcalls += diff; /* correct 'nCcalls' */ + return limit - diff - CSTACKERR; /* success; return previous limit */ + } +} + + /* ** Decrement count of "C calls" and check for overflows. In case of ** a stack overflow, check appropriate error ("regular" overflow or @@ -121,7 +144,7 @@ void luaE_enterCcall (lua_State *L) { else if (ncalls >= CSTACKMARK) { /* not in error-handling zone; raise the error now */ L->nCcalls = (CSTACKMARK - 1); /* enter error-handling zone */ - luaG_runerror(L, "C stack overflow1"); + luaG_runerror(L, "C stack overflow"); } /* else stack is in the error-handling zone; allow message handler to work */ @@ -263,7 +286,7 @@ static void preinit_thread (lua_State *L, global_State *g) { L->stacksize = 0; L->twups = L; /* thread has no upvalues */ L->errorJmp = NULL; - L->nCcalls = LUAI_MAXCSTACK + CSTACKERR; + L->nCcalls = CSTACKTHREAD; L->hook = NULL; L->hookmask = 0; L->basehookcount = 0; @@ -365,6 +388,7 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { preinit_thread(L, g); g->allgc = obj2gco(L); /* by now, only object is the main thread */ L->next = NULL; + g->Cstacklimit = L->nCcalls = LUAI_MAXCSTACK; g->frealloc = f; g->ud = ud; g->warnf = NULL; diff --git a/lstate.h b/lstate.h index 858da5bec8..d3a64f941f 100644 --- a/lstate.h +++ b/lstate.h @@ -103,6 +103,10 @@ #define CSTACKERRMARK (CSTACKCF + 2) +/* initial limit for the C-stack of threads */ +#define CSTACKTHREAD (2 * CSTACKERR) + + /* true if this thread does not have non-yieldable calls in the stack */ #define yieldable(L) (((L)->nCcalls & 0xffff0000) == 0) @@ -267,6 +271,7 @@ typedef struct global_State { TString *strcache[STRCACHE_N][STRCACHE_M]; /* cache for strings in API */ lua_WarnFunction warnf; /* warning function */ void *ud_warn; /* auxiliary data to 'warnf' */ + unsigned int Cstacklimit; /* current limit for the C stack */ } global_State; diff --git a/ltests.h b/ltests.h index a22c98e17f..0ae6afc49e 100644 --- a/ltests.h +++ b/ltests.h @@ -31,7 +31,7 @@ /* compiled with -O0, Lua uses a lot of C stack space... */ #undef LUAI_MAXCSTACK -#define LUAI_MAXCSTACK 400 +#define LUAI_MAXCSTACK (400 + CSTACKERR) /* to avoid warnings, and to make sure value is really unused */ #define UNUSED(x) (x=0, (void)(x)) diff --git a/lua.h b/lua.h index ec31c7819f..d3575fd9c9 100644 --- a/lua.h +++ b/lua.h @@ -462,6 +462,7 @@ LUA_API lua_Hook (lua_gethook) (lua_State *L); LUA_API int (lua_gethookmask) (lua_State *L); LUA_API int (lua_gethookcount) (lua_State *L); +LUA_API int (lua_setCstacklimit) (lua_State *L, unsigned int limit); struct lua_Debug { int event; diff --git a/manual/manual.of b/manual/manual.of index 725b12ad85..e941695678 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -4803,6 +4803,20 @@ calling @Lid{lua_yield} with @id{nresults} equal to zero } +@APIEntry{int (lua_setCstacklimit) (lua_State *L, unsigned int limit);| +@apii{0,0,-} + +Sets a new limit for the C stack. +This limit controls how deeply nested calls can go in Lua, +with the intent of avoiding a stack overflow. +Returns the old limit in case of success, +or zero in case of error. +For more details about this function, +see @Lid{debug.setCstacklimit}, +its equivalent in the standard library. + +} + @APIEntry{void lua_sethook (lua_State *L, lua_Hook f, int mask, int count);| @apii{0,0,-} @@ -8516,6 +8530,34 @@ to the userdata @id{u} plus a boolean, } +@LibEntry{debug.setCstacklimit (limit)| + +Sets a new limit for the C stack. +This limit controls how deeply nested calls can go in Lua, +with the intent of avoiding a stack overflow. +A limit too small restricts recursive calls pointlessly; +a limit too large exposes the interpreter to stack-overflow crashes. +Unfortunately, there is no way to know a priori +the maximum safe limit for a platform. + +Each call made from Lua code counts one unit. +Other operations (e.g., calls made from C to Lua or resuming a coroutine) +may have a higher cost. + +This function has the following restrictions: +@description{ +@item{It can only be called from the main coroutine (thread);} +@item{It cannot be called while handling a stack-overflow error;} +@item{@id{limit} must be less than 40000;} +@item{@id{limit} cannot be less than the amount of C stack in use.} +} +In case of success, +this function returns the old limit. +In case of error, +it returns @false. + +} + @LibEntry{debug.sethook ([thread,] hook, mask [, count])| Sets the given function as the debug hook. diff --git a/testes/cstack.lua b/testes/cstack.lua index 3cb1e4ce23..c7aa740f01 100644 --- a/testes/cstack.lua +++ b/testes/cstack.lua @@ -1,8 +1,14 @@ -- $Id: testes/cstack.lua $ -- See Copyright Notice in file all.lua +local debug = require "debug" + print"testing C-stack overflow detection" +local origlimit = debug.setCstacklimit(400) +print("current stack limit: " .. origlimit) +debug.setCstacklimit(origlimit) + -- Segmentation faults in these tests probably result from a C-stack -- overflow. To avoid these errors, recompile Lua with a smaller -- value for the constant 'LUAI_MAXCCALLS' or else ensure a larger @@ -79,4 +85,46 @@ do print("testing stack-overflow in recursive 'gsub'") print("\tfinal count: ", count) end + +do print("testing changes in C-stack limit") + + assert(not debug.setCstacklimit(0)) -- limit too small + assert(not debug.setCstacklimit(50000)) -- limit too large + local co = coroutine.wrap (function () + return debug.setCstacklimit(400) + end) + assert(co() == false) -- cannot change C stack inside coroutine + + local n + local function foo () n = n + 1; foo () end + + local function check () + n = 0 + pcall(foo) + return n + end + + assert(debug.setCstacklimit(400) == origlimit) + local lim400 = check() + -- a very low limit (given that the several calls to arive here) + local lowlimit = 38 + assert(debug.setCstacklimit(lowlimit) == 400) + assert(check() < lowlimit - 30) + assert(debug.setCstacklimit(600) == lowlimit) + local lim600 = check() + assert(lim600 == lim400 + 200) + + + -- 'setCstacklimit' works inside protected calls. (The new stack + -- limit is kept when 'pcall' returns.) + assert(pcall(function () + assert(debug.setCstacklimit(400) == 600) + assert(check() <= lim400) + end)) + + assert(check() == lim400) + assert(debug.setCstacklimit(origlimit) == 400) -- restore original limit +end + + print'OK' diff --git a/testes/errors.lua b/testes/errors.lua index 0b12410eda..6e7b8004be 100644 --- a/testes/errors.lua +++ b/testes/errors.lua @@ -523,9 +523,13 @@ end -- testing syntax limits -local function testrep (init, rep, close, repc) +local function testrep (init, rep, close, repc, finalresult) local s = init .. string.rep(rep, 100) .. close .. string.rep(repc, 100) - assert(load(s)) -- 100 levels is OK + local res, msg = load(s) + assert(res) -- 100 levels is OK + if (finalresult) then + assert(res() == finalresult) + end s = init .. string.rep(rep, 10000) local res, msg = load(s) -- 10000 levels not ok assert(not res and (string.find(msg, "too many registers") or @@ -534,14 +538,14 @@ end testrep("local a; a", ",a", "= 1", ",1") -- multiple assignment testrep("local a; a=", "{", "0", "}") -testrep("local a; a=", "(", "2", ")") -testrep("local a; ", "a(", "2", ")") +testrep("return ", "(", "2", ")", 2) +testrep("local function a (x) return x end; return ", "a(", "2.2", ")", 2.2) testrep("", "do ", "", " end") testrep("", "while a do ", "", " end") testrep("local a; ", "if a then else ", "", " end") testrep("", "function foo () ", "", " end") -testrep("local a; a=", "a..", "a", "") -testrep("local a; a=", "a^", "a", "") +testrep("local a = ''; return ", "a..", "'a'", "", "a") +testrep("local a = 1; return ", "a^", "a", "", 1) checkmessage("a = f(x" .. string.rep(",x", 260) .. ")", "too many registers") From 1d70708a784980bfeee142d3ed95f8df9e1b1a4a Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 21 Jun 2019 09:34:49 -0300 Subject: [PATCH 0434/1145] Fixed bug [5.4 alpha] for errors in finalizers Fixes the bug related in [1] (Lua can crash after raising an error in a finalizer), following the lead in [2]. [1] http://lua-users.org/lists/lua-l/2019-06/msg00448.html [2] http://lua-users.org/lists/lua-l/2019-06/msg00450.html --- lgc.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lgc.c b/lgc.c index 69d8a186a0..8444566ccb 100644 --- a/lgc.c +++ b/lgc.c @@ -838,21 +838,21 @@ static void GCTM (lua_State *L) { int running = g->gcrunning; L->allowhook = 0; /* stop debug hooks during GC metamethod */ g->gcrunning = 0; /* avoid GC steps */ - setobj2s(L, L->top, tm); /* push finalizer... */ - setobj2s(L, L->top + 1, &v); /* ... and its argument */ - L->top += 2; /* and (next line) call the finalizer */ + setobj2s(L, L->top++, tm); /* push finalizer... */ + setobj2s(L, L->top++, &v); /* ... and its argument */ L->ci->callstatus |= CIST_FIN; /* will run a finalizer */ status = luaD_pcall(L, dothecall, NULL, savestack(L, L->top - 2), 0); L->ci->callstatus &= ~CIST_FIN; /* not running a finalizer anymore */ L->allowhook = oldah; /* restore hooks */ g->gcrunning = running; /* restore state */ - if (status != LUA_OK) { /* error while running __gc? */ + if (unlikely(status != LUA_OK)) { /* error while running __gc? */ const char *msg = (ttisstring(s2v(L->top - 1))) ? svalue(s2v(L->top - 1)) : "error object is not a string"; luaE_warning(L, "error in __gc metamethod (", 1); luaE_warning(L, msg, 1); luaE_warning(L, ")", 0); + L->top--; /* pops error object */ } } } From 20a9853e0279903d255846108ffe320826dddcca Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 21 Jun 2019 10:00:50 -0300 Subject: [PATCH 0435/1145] Cleaning macros in 'luaV_execute' Ensure that operation macros, such as 'luai_numdiv' and 'luai_numidiv', operate only on variables, or at most at 's2v(ra)'. ('s2v' is a nop, a cast from pointer to pointer.) --- lvm.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/lvm.c b/lvm.c index 5d0709ef94..b05a887d0c 100644 --- a/lvm.c +++ b/lvm.c @@ -797,7 +797,8 @@ void luaV_finishOp (lua_State *L) { #define op_arithfI_aux(L,v1,imm,fop,tm,flip) { \ lua_Number nb; \ if (tonumberns(v1, nb)) { \ - setfltvalue(s2v(ra), fop(L, nb, cast_num(imm))); \ + lua_Number fimm = cast_num(imm); \ + setfltvalue(s2v(ra), fop(L, nb, fimm)); \ } \ else \ Protect(luaT_trybiniTM(L, v1, imm, flip, ra, tm)); } @@ -819,7 +820,8 @@ void luaV_finishOp (lua_State *L) { TValue *v1 = vRB(i); \ int imm = GETARG_sC(i); \ if (ttisinteger(v1)) { \ - setivalue(s2v(ra), iop(L, ivalue(v1), imm)); \ + lua_Integer iv1 = ivalue(v1); \ + setivalue(s2v(ra), iop(L, iv1, imm)); \ } \ else op_arithfI_aux(L, v1, imm, fop, tm, flip); } @@ -927,8 +929,11 @@ void luaV_finishOp (lua_State *L) { #define op_order(L,opi,opf,other) { \ int cond; \ TValue *rb = vRB(i); \ - if (ttisinteger(s2v(ra)) && ttisinteger(rb)) \ - cond = opi(ivalue(s2v(ra)), ivalue(rb)); \ + if (ttisinteger(s2v(ra)) && ttisinteger(rb)) { \ + lua_Integer ia = ivalue(s2v(ra)); \ + lua_Integer ib = ivalue(rb); \ + cond = opi(ia, ib); \ + } \ else if (ttisnumber(s2v(ra)) && ttisnumber(rb)) \ cond = opf(s2v(ra), rb); \ else \ @@ -944,8 +949,11 @@ void luaV_finishOp (lua_State *L) { int im = GETARG_sB(i); \ if (ttisinteger(s2v(ra))) \ cond = opi(ivalue(s2v(ra)), im); \ - else if (ttisfloat(s2v(ra))) \ - cond = opf(fltvalue(s2v(ra)), cast_num(im)); \ + else if (ttisfloat(s2v(ra))) { \ + lua_Number fa = fltvalue(s2v(ra)); \ + lua_Number fim = cast_num(im); \ + cond = opf(fa, fim); \ + } \ else { \ int isf = GETARG_C(i); \ Protect(cond = luaT_callorderiTM(L, s2v(ra), im, inv, isf, tm)); \ From e4b02ca8e48b499c57dd3e5882d18145db60fd4c Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 21 Jun 2019 10:16:57 -0300 Subject: [PATCH 0436/1145] Structure 'Vardesc' does not need a 'name' field Removed the field 'name' from the structure 'Vardesc', as the name of the local variable is already available in the prototype of the function, through the index 'idx'. --- lparser.c | 25 +++++++++++++------------ lparser.h | 1 - 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/lparser.c b/lparser.c index 045efd93ff..3c68f2d7c3 100644 --- a/lparser.c +++ b/lparser.c @@ -205,7 +205,6 @@ static Vardesc *new_localvar (LexState *ls, TString *name) { dyd->actvar.size, Vardesc, MAX_INT, "local variables"); var = &dyd->actvar.arr[dyd->actvar.n++]; var->idx = cast(short, reg); - var->name = name; var->ro = 0; return var; } @@ -235,25 +234,25 @@ static LocVar *getlocvar (FuncState *fs, int i) { /* ** Return the "variable description" (Vardesc) of a given -** variable or upvalue +** local variable and update 'fs' to point to the function +** where that variable was defined. Return NULL if expression +** is neither a local variable nor an upvalue. */ -static Vardesc *getvardesc (FuncState *fs, expdesc *e) { +static Vardesc *getvardesc (FuncState **fs, expdesc *e) { if (e->k == VLOCAL) - return getlocalvardesc(fs, e->u.var.idx); + return getlocalvardesc(*fs, e->u.var.idx); else if (e->k != VUPVAL) return NULL; /* not a local variable */ else { /* upvalue: must go up all levels up to the original local */ int idx = e->u.var.idx; for (;;) { - Upvaldesc *up = &fs->f->upvalues[idx]; - fs = fs->prev; /* must look at the previous level */ + Upvaldesc *up = &(*fs)->f->upvalues[idx]; + *fs = (*fs)->prev; /* must look at the previous level */ idx = up->idx; /* at this index */ - if (fs == NULL) { /* no more levels? (can happen only with _ENV) */ - lua_assert(strcmp(getstr(up->name), LUA_ENV) == 0); + if (*fs == NULL) /* no more levels? (can happen only with _ENV) */ return NULL; - } else if (up->instack) /* got to the original level? */ - return getlocalvardesc(fs, idx); + return getlocalvardesc(*fs, idx); /* else repeat for previous level */ } } @@ -261,10 +260,12 @@ static Vardesc *getvardesc (FuncState *fs, expdesc *e) { static void check_readonly (LexState *ls, expdesc *e) { - Vardesc *vardesc = getvardesc(ls->fs, e); + FuncState *fs = ls->fs; + Vardesc *vardesc = getvardesc(&fs, e); if (vardesc && vardesc->ro) { /* is variable local and const? */ const char *msg = luaO_pushfstring(ls->L, - "attempt to assign to const variable '%s'", getstr(vardesc->name)); + "attempt to assign to const variable '%s'", + getstr(fs->f->locvars[vardesc->idx].varname)); luaK_semerror(ls, msg); /* error */ } } diff --git a/lparser.h b/lparser.h index 3b5d399fd8..228d4a5cb5 100644 --- a/lparser.h +++ b/lparser.h @@ -81,7 +81,6 @@ typedef struct expdesc { /* description of an active local variable */ typedef struct Vardesc { - TString *name; short idx; /* index of the variable in the Proto's 'locvars' array */ lu_byte ro; /* true if variable is 'const' */ } Vardesc; From 6b9490bd72738c02b0d0962fdce6e1763d53e124 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 21 Jun 2019 10:21:07 -0300 Subject: [PATCH 0437/1145] Details in tests - Added a test for calling 'debug.traceback' after yields inside hooks. (Lua 5.3 seems to have a bug there.) - Removed test "repeat test with '__open' metamethod instead of a function", as the previous test already uses the '__open' metamethod. (It changed when functions were removed as possible to-be-closed variables). --- testes/coroutine.lua | 4 ++++ testes/locals.lua | 24 ++++++------------------ 2 files changed, 10 insertions(+), 18 deletions(-) diff --git a/testes/coroutine.lua b/testes/coroutine.lua index f2c0da8bdf..e04207c857 100644 --- a/testes/coroutine.lua +++ b/testes/coroutine.lua @@ -426,6 +426,10 @@ else while A==0 or B==0 do -- A ~= 0 when 'x' finishes (similar for 'B','y') if A==0 then turn = "A"; assert(T.resume(x)) end if B==0 then turn = "B"; assert(T.resume(y)) end + + -- check that traceback works correctly after yields inside hooks + debug.traceback(x) + debug.traceback(y) end assert(B // A == 7) -- fact(7) // fact(6) diff --git a/testes/locals.lua b/testes/locals.lua index dccda28fe9..a41b6f0ed4 100644 --- a/testes/locals.lua +++ b/testes/locals.lua @@ -517,27 +517,15 @@ do end assert(s == 35 and numopen == 0) - -- repeat test with '__open' metamethod instead of a function - local function open (x) - numopen = numopen + 1 - local state = setmetatable({x}, - {__close = function () numopen = numopen - 1 end}) - return - function (t) -- iteraction function - t[1] = t[1] - 1 - if t[1] > 0 then return t[1] end - end, - state, - nil, - state -- to-be-closed - end - local s = 0 for i in open(10) do - if (i < 5) then break end - s = s + i + for j in open(10) do + if i + j < 5 then goto endloop end + s = s + i + end end - assert(s == 35 and numopen == 0) + ::endloop:: + assert(s == 375 and numopen == 0) end print('OK') From 05ba2880491fa1ecfe78d8a3542edcd6220e4022 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 21 Jun 2019 10:46:41 -0300 Subject: [PATCH 0438/1145] Added script 'packtests' to the project The script 'packtests' creates the 'tar.gz' to deploy the test suite for Lua. --- testes/packtests | 52 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100755 testes/packtests diff --git a/testes/packtests b/testes/packtests new file mode 100755 index 0000000000..f7bca93dc2 --- /dev/null +++ b/testes/packtests @@ -0,0 +1,52 @@ +NAME="lua-5.4.0-alpha-tests" + +ln -s . $NAME +ln -s .. ltests + +tar --create --gzip --no-recursion --file=$NAME.tar.gz \ +$NAME/all.lua \ +$NAME/api.lua \ +$NAME/attrib.lua \ +$NAME/big.lua \ +$NAME/bitwise.lua \ +$NAME/bwcoercion.lua \ +$NAME/calls.lua \ +$NAME/closure.lua \ +$NAME/code.lua \ +$NAME/constructs.lua \ +$NAME/coroutine.lua \ +$NAME/cstack.lua \ +$NAME/db.lua \ +$NAME/errors.lua \ +$NAME/events.lua \ +$NAME/files.lua \ +$NAME/gc.lua \ +$NAME/gengc.lua \ +$NAME/goto.lua \ +$NAME/heavy.lua \ +$NAME/literals.lua \ +$NAME/locals.lua \ +$NAME/main.lua \ +$NAME/math.lua \ +$NAME/nextvar.lua \ +$NAME/pm.lua \ +$NAME/sort.lua \ +$NAME/strings.lua \ +$NAME/tpack.lua \ +$NAME/utf8.lua \ +$NAME/vararg.lua \ +$NAME/verybig.lua \ +$NAME/libs/makefile \ +$NAME/libs/P1 \ +$NAME/libs/lib1.c \ +$NAME/libs/lib11.c \ +$NAME/libs/lib2.c \ +$NAME/libs/lib21.c \ +$NAME/libs/lib22.c \ +$NAME/ltests/ltests.h \ +$NAME/ltests/ltests.c + +\rm -I $NAME +\rm -I ltests + + From 4487c28ced3dcf47c3ee19b6f6eeb0089ec64ba5 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 25 Jun 2019 17:38:58 -0300 Subject: [PATCH 0439/1145] A few more tests for table access in the API Added tests where the table being accessed is also the index or value in the operation. --- ltests.c | 16 ++++++++++++++++ testes/api.lua | 48 +++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/ltests.c b/ltests.c index f786eeb3c2..dc83065747 100644 --- a/ltests.c +++ b/ltests.c @@ -1488,6 +1488,10 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) { else if EQ("pushfstringP") { lua_pushfstring(L1, lua_tostring(L, -2), lua_topointer(L, -1)); } + else if EQ("rawget") { + int t = getindex; + lua_rawget(L1, t); + } else if EQ("rawgeti") { int t = getindex; lua_rawgeti(L1, t, getnum); @@ -1496,6 +1500,14 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) { int t = getindex; lua_rawgetp(L1, t, cast_voidp(cast_sizet(getnum))); } + else if EQ("rawset") { + int t = getindex; + lua_rawset(L1, t); + } + else if EQ("rawseti") { + int t = getindex; + lua_rawseti(L1, t, getnum); + } else if EQ("rawsetp") { int t = getindex; lua_rawsetp(L1, t, cast_voidp(cast_sizet(getnum))); @@ -1538,6 +1550,10 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) { const char *s = getstring; lua_setfield(L1, t, s); } + else if EQ("seti") { + int t = getindex; + lua_seti(L1, t, getnum); + } else if EQ("setglobal") { const char *s = getstring; lua_setglobal(L1, s); diff --git a/testes/api.lua b/testes/api.lua index bcc04dac54..8f4e89ac86 100644 --- a/testes/api.lua +++ b/testes/api.lua @@ -498,7 +498,53 @@ do -- getp/setp local a = {} T.testC("rawsetp 2 1", a, 20) assert(a[T.pushuserdata(1)] == 20) - assert(T.testC("rawgetp 2 1; return 1", a) == 20) + assert(T.testC("rawgetp -1 1; return 1", a) == 20) +end + + +do -- using the table itself as index + local a = {} + a[a] = 10 + local prog = "gettable -1; return *" + local res = {T.testC(prog, a)} + assert(#res == 2 and res[1] == prog and res[2] == 10) + + local prog = "settable -2; return *" + local res = {T.testC(prog, a, 20)} + assert(a[a] == 20) + assert(#res == 1 and res[1] == prog) + + -- raw + a[a] = 10 + local prog = "rawget -1; return *" + local res = {T.testC(prog, a)} + assert(#res == 2 and res[1] == prog and res[2] == 10) + + local prog = "rawset -2; return *" + local res = {T.testC(prog, a, 20)} + assert(a[a] == 20) + assert(#res == 1 and res[1] == prog) + + -- using the table as the value to set + local prog = "rawset -1; return *" + local res = {T.testC(prog, 30, a)} + assert(a[30] == a) + assert(#res == 1 and res[1] == prog) + + local prog = "settable -1; return *" + local res = {T.testC(prog, 40, a)} + assert(a[40] == a) + assert(#res == 1 and res[1] == prog) + + local prog = "rawseti -1 100; return *" + local res = {T.testC(prog, a)} + assert(a[100] == a) + assert(#res == 1 and res[1] == prog) + + local prog = "seti -1 200; return *" + local res = {T.testC(prog, a)} + assert(a[200] == a) + assert(#res == 1 and res[1] == prog) end a = {x=0, y=12} From c1a63c45f8ec5932993c8cec40d3c5ec0743349c Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 25 Jun 2019 17:45:50 -0300 Subject: [PATCH 0440/1145] '__call' metamethod can be any callable object Removed the restriction that a '__call' metamethod must be an actual function. --- ldo.c | 28 ++++++++++++++-------------- testes/calls.lua | 17 +++++++++++++++++ 2 files changed, 31 insertions(+), 14 deletions(-) diff --git a/ldo.c b/ldo.c index 1a327ffd46..288aef1352 100644 --- a/ldo.c +++ b/ldo.c @@ -348,18 +348,18 @@ static StkId rethook (lua_State *L, CallInfo *ci, StkId firstres, int nres) { /* -** Check whether __call metafield of 'func' is a function. If so, put -** it in stack below original 'func' so that 'luaD_call' can call -** it. Raise an error if __call metafield is not a function. +** Check whether 'func' has a '__call' metafield. If so, put it in the +** stack, below original 'func', so that 'luaD_call' can call it. Raise +** an error if there is no '__call' metafield. */ void luaD_tryfuncTM (lua_State *L, StkId func) { const TValue *tm = luaT_gettmbyobj(L, s2v(func), TM_CALL); StkId p; - if (unlikely(!ttisfunction(tm))) - luaG_typeerror(L, s2v(func), "call"); - for (p = L->top; p > func; p--) + if (unlikely(ttisnil(tm))) + luaG_typeerror(L, s2v(func), "call"); /* nothing to call */ + for (p = L->top; p > func; p--) /* open space for metamethod */ setobjs2s(L, p, p-1); - L->top++; /* assume EXTRA_STACK */ + L->top++; /* stack space pre-allocated by the caller */ setobj2s(L, func, tm); /* metamethod is the new function to be called */ } @@ -457,13 +457,13 @@ void luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, int narg1) { */ void luaD_call (lua_State *L, StkId func, int nresults) { lua_CFunction f; - TValue *funcv = s2v(func); - switch (ttypetag(funcv)) { + retry: + switch (ttypetag(s2v(func))) { case LUA_TCCL: /* C closure */ - f = clCvalue(funcv)->f; + f = clCvalue(s2v(func))->f; goto Cfunc; case LUA_TLCF: /* light C function */ - f = fvalue(funcv); + f = fvalue(s2v(func)); Cfunc: { int n; /* number of returns */ CallInfo *ci; @@ -487,7 +487,7 @@ void luaD_call (lua_State *L, StkId func, int nresults) { } case LUA_TLCL: { /* Lua function */ CallInfo *ci; - Proto *p = clLvalue(funcv)->p; + Proto *p = clLvalue(s2v(func))->p; int narg = cast_int(L->top - func) - 1; /* number of real arguments */ int nfixparams = p->numparams; int fsize = p->maxstacksize; /* frame size */ @@ -505,9 +505,9 @@ void luaD_call (lua_State *L, StkId func, int nresults) { break; } default: { /* not a function */ + checkstackp(L, 1, func); /* space for metamethod */ luaD_tryfuncTM(L, func); /* try to get '__call' metamethod */ - luaD_call(L, func, nresults); /* now it must be a function */ - break; + goto retry; /* try again with metamethod */ } } } diff --git a/testes/calls.lua b/testes/calls.lua index 56a12ae670..739a624fb8 100644 --- a/testes/calls.lua +++ b/testes/calls.lua @@ -151,6 +151,23 @@ end print('+') +do -- testing chains of '__call' + local N = 20 + local u = table.pack + for i = 1, N do + u = setmetatable({i}, {__call = u}) + end + + local Res = u("a", "b", "c") + + assert(Res.n == N + 3) + for i = 1, N do + assert(Res[i][1] == i) + end + assert(Res[N + 1] == "a" and Res[N + 2] == "b" and Res[N + 3] == "c") +end + + a = nil (function (x) a=x end)(23) assert(a == 23 and (function (x) return x*2 end)(20) == 40) From 8b7cfee26b71e66de2cef9f8db9d9e18f5439afd Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 26 Jun 2019 13:26:36 -0300 Subject: [PATCH 0441/1145] Small changes around C-stack limit - Better documentation in 'testes/cstack.lua' about using 'debug.setCstacklimit' to find a good limit. - Constant LUAI_MAXCSTACK gets added CSTACKERR (extra stack for error handling), so that it is compatible with the argument to 'debug.setCstacklimit'. --- lstate.c | 2 +- ltests.h | 2 +- luaconf.h | 2 +- testes/cstack.lua | 28 +++++++++++++++++++++------- 4 files changed, 24 insertions(+), 10 deletions(-) diff --git a/lstate.c b/lstate.c index 92d5165af5..d4bc53eb3b 100644 --- a/lstate.c +++ b/lstate.c @@ -388,7 +388,7 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { preinit_thread(L, g); g->allgc = obj2gco(L); /* by now, only object is the main thread */ L->next = NULL; - g->Cstacklimit = L->nCcalls = LUAI_MAXCSTACK; + g->Cstacklimit = L->nCcalls = LUAI_MAXCSTACK + CSTACKERR; g->frealloc = f; g->ud = ud; g->warnf = NULL; diff --git a/ltests.h b/ltests.h index 0ae6afc49e..8c8de47648 100644 --- a/ltests.h +++ b/ltests.h @@ -31,7 +31,7 @@ /* compiled with -O0, Lua uses a lot of C stack space... */ #undef LUAI_MAXCSTACK -#define LUAI_MAXCSTACK (400 + CSTACKERR) +#define LUAI_MAXCSTACK 400 /* to avoid warnings, and to make sure value is really unused */ #define UNUSED(x) (x=0, (void)(x)) diff --git a/luaconf.h b/luaconf.h index 39840e395e..72018855e7 100644 --- a/luaconf.h +++ b/luaconf.h @@ -47,7 +47,7 @@ ** (It will crash with a limit too high.) */ #if !defined(LUAI_MAXCSTACK) -#define LUAI_MAXCSTACK 2200 +#define LUAI_MAXCSTACK 2000 #endif diff --git a/testes/cstack.lua b/testes/cstack.lua index c7aa740f01..2a55ce21a3 100644 --- a/testes/cstack.lua +++ b/testes/cstack.lua @@ -4,15 +4,29 @@ local debug = require "debug" print"testing C-stack overflow detection" +print"If this test craches, see its file ('cstack.lua')" + +-- Segmentation faults in these tests probably result from a C-stack +-- overflow. To avoid these errors, you can use the function +-- 'debug.setCstacklimit' to set a smaller limit for the use of +-- C stack by Lua. After finding a reliable limit, you might want +-- to recompile Lua with this limit as the value for +-- the constant 'LUAI_MAXCCALLS', which defines the default limit. +-- (The default limit is printed by this test.) +-- Alternatively, you can ensure a larger stack for the program. + +-- For Linux, a limit up to 30_000 seems Ok. Windows cannot go much +-- higher than 2_000. + local origlimit = debug.setCstacklimit(400) -print("current stack limit: " .. origlimit) -debug.setCstacklimit(origlimit) +print("default stack limit: " .. origlimit) + +-- change this value for different limits for this test suite +local currentlimit = origlimit +debug.setCstacklimit(currentlimit) +print("current stack limit: " .. currentlimit) --- Segmentation faults in these tests probably result from a C-stack --- overflow. To avoid these errors, recompile Lua with a smaller --- value for the constant 'LUAI_MAXCCALLS' or else ensure a larger --- stack for the program. local function checkerror (msg, f, ...) local s, err = pcall(f, ...) @@ -104,7 +118,7 @@ do print("testing changes in C-stack limit") return n end - assert(debug.setCstacklimit(400) == origlimit) + assert(debug.setCstacklimit(400) == currentlimit) local lim400 = check() -- a very low limit (given that the several calls to arive here) local lowlimit = 38 From 924bed7297d5ea16a78ec07e7acc64afad951aa8 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 1 Jul 2019 12:25:00 -0300 Subject: [PATCH 0442/1145] Methods separated from metamethods in 'io' In the 'io' library, changed the use of the metatable also as its own "method table", so that metamethods cannot be accessed as if they were methods. (For instance, 'io.stdin.__gc' does not result in the finalizer metamethod anymore.) --- liolib.c | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/liolib.c b/liolib.c index 1484676d47..2c0c6a3b70 100644 --- a/liolib.c +++ b/liolib.c @@ -742,14 +742,23 @@ static const luaL_Reg iolib[] = { /* ** methods for file handles */ -static const luaL_Reg flib[] = { - {"close", f_close}, - {"flush", f_flush}, - {"lines", f_lines}, +static const luaL_Reg meth[] = { {"read", f_read}, + {"write", f_write}, + {"lines", f_lines}, + {"flush", f_flush}, {"seek", f_seek}, + {"close", f_close}, {"setvbuf", f_setvbuf}, - {"write", f_write}, + {NULL, NULL} +}; + + +/* +** metamethods for file handles +*/ +static const luaL_Reg metameth[] = { + {"__index", NULL}, /* place holder */ {"__gc", f_gc}, {"__close", f_gc}, {"__tostring", f_tostring}, @@ -758,11 +767,12 @@ static const luaL_Reg flib[] = { static void createmeta (lua_State *L) { - luaL_newmetatable(L, LUA_FILEHANDLE); /* create metatable for file handles */ - lua_pushvalue(L, -1); /* push metatable */ - lua_setfield(L, -2, "__index"); /* metatable.__index = metatable */ - luaL_setfuncs(L, flib, 0); /* add file methods to new metatable */ - lua_pop(L, 1); /* pop new metatable */ + luaL_newmetatable(L, LUA_FILEHANDLE); /* metatable for file handles */ + luaL_setfuncs(L, metameth, 0); /* add metamethods to new metatable */ + luaL_newlibtable(L, meth); /* create method table */ + luaL_setfuncs(L, meth, 0); /* add file methods to method table */ + lua_setfield(L, -2, "__index"); /* metatable.__index = method table */ + lua_pop(L, 1); /* pop metatable */ } From 8eca21c2e85625390a2a3b08c231e75e315980b0 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 1 Jul 2019 12:42:31 -0300 Subject: [PATCH 0443/1145] First take on constant propagation --- lcode.c | 62 ++++++++++++++++++++++++++++++++++++++++++------------- lcode.h | 1 + lparser.c | 34 +++++++++++++++++++----------- lparser.h | 2 ++ 4 files changed, 73 insertions(+), 26 deletions(-) diff --git a/lcode.c b/lcode.c index eccb8380a9..1005f1b7a1 100644 --- a/lcode.c +++ b/lcode.c @@ -52,7 +52,7 @@ l_noret luaK_semerror (LexState *ls, const char *msg) { ** If expression is a numeric constant, fills 'v' with its value ** and returns 1. Otherwise, returns 0. */ -static int tonumeral(const expdesc *e, TValue *v) { +int luaK_tonumeral (FuncState *fs, const expdesc *e, TValue *v) { if (hasjumps(e)) return 0; /* not a numeral */ switch (e->k) { @@ -62,11 +62,41 @@ static int tonumeral(const expdesc *e, TValue *v) { case VKFLT: if (v) setfltvalue(v, e->u.nval); return 1; + case VUPVAL: { /* may be a constant */ + Vardesc *vd = luaY_getvardesc(&fs, e); + if (v && vd && !ttisnil(&vd->val)) { + setobj(fs->ls->L, v, &vd->val); + return 1; + } /* else */ + } /* FALLTHROUGH */ default: return 0; } } +/* +** If expression 'e' is a constant, change 'e' to represent +** the constant value. +*/ +static int const2exp (FuncState *fs, expdesc *e) { + Vardesc *vd = luaY_getvardesc(&fs, e); + if (vd) { + TValue *v = &vd->val; + switch (ttypetag(v)) { + case LUA_TNUMINT: + e->k = VKINT; + e->u.ival = ivalue(v); + return 1; + case LUA_TNUMFLT: + e->k = VKFLT; + e->u.nval = fltvalue(v); + return 1; + } + } + return 0; +} + + /* ** Return the previous instruction of the current code. If there ** may be a jump target between the current instruction and the @@ -683,8 +713,10 @@ void luaK_dischargevars (FuncState *fs, expdesc *e) { break; } case VUPVAL: { /* move value to some (pending) register */ - e->u.info = luaK_codeABC(fs, OP_GETUPVAL, 0, e->u.var.idx, 0); - e->k = VRELOC; + if (!const2exp(fs, e)) { + e->u.info = luaK_codeABC(fs, OP_GETUPVAL, 0, e->u.var.idx, 0); + e->k = VRELOC; + } break; } case VINDEXUP: { @@ -1218,9 +1250,11 @@ static int validop (int op, TValue *v1, TValue *v2) { ** (In this case, 'e1' has the final result.) */ static int constfolding (FuncState *fs, int op, expdesc *e1, - const expdesc *e2) { + const expdesc *e2) { TValue v1, v2, res; - if (!tonumeral(e1, &v1) || !tonumeral(e2, &v2) || !validop(op, &v1, &v2)) + if (!luaK_tonumeral(fs, e1, &v1) || + !luaK_tonumeral(fs, e2, &v2) || + !validop(op, &v1, &v2)) return 0; /* non-numeric operands or not safe to fold */ luaO_rawarith(fs->ls->L, op, &v1, &v2, &res); /* does operation */ if (ttisinteger(&res)) { @@ -1307,7 +1341,7 @@ static void codearith (FuncState *fs, OpCode op, expdesc *e1, expdesc *e2, int flip, int line) { if (isSCint(e2)) /* immediate operand? */ codebini(fs, cast(OpCode, op - OP_ADD + OP_ADDI), e1, e2, flip, line); - else if (tonumeral(e2, NULL) && luaK_exp2K(fs, e2)) { /* K operand? */ + else if (luaK_tonumeral(fs, e2, NULL) && luaK_exp2K(fs, e2)) { /* K operand? */ int v2 = e2->u.info; /* K index */ op = cast(OpCode, op - OP_ADD + OP_ADDK); finishbinexpval(fs, e1, e2, op, v2, flip, line); @@ -1328,7 +1362,7 @@ static void codearith (FuncState *fs, OpCode op, static void codecommutative (FuncState *fs, OpCode op, expdesc *e1, expdesc *e2, int line) { int flip = 0; - if (tonumeral(e1, NULL)) { /* is first operand a numeric constant? */ + if (luaK_tonumeral(fs, e1, NULL)) { /* is first operand a numeric constant? */ swapexps(e1, e2); /* change order */ flip = 1; } @@ -1451,7 +1485,7 @@ void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e, int line) { case OPR_MINUS: case OPR_BNOT: /* use 'ef' as fake 2nd operand */ if (constfolding(fs, op + LUA_OPUNM, e, &ef)) break; - /* FALLTHROUGH */ + /* else */ /* FALLTHROUGH */ case OPR_LEN: codeunexpval(fs, cast(OpCode, op + OP_UNM), e, line); break; @@ -1466,6 +1500,7 @@ void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e, int line) { ** 2nd operand. */ void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) { + luaK_dischargevars(fs, v); switch (op) { case OPR_AND: { luaK_goiftrue(fs, v); /* go ahead only if 'v' is true */ @@ -1484,13 +1519,13 @@ void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) { case OPR_MOD: case OPR_POW: case OPR_BAND: case OPR_BOR: case OPR_BXOR: case OPR_SHL: case OPR_SHR: { - if (!tonumeral(v, NULL)) + if (!luaK_tonumeral(fs, v, NULL)) luaK_exp2anyreg(fs, v); /* else keep numeral, which may be folded with 2nd operand */ break; } case OPR_EQ: case OPR_NE: { - if (!tonumeral(v, NULL)) + if (!luaK_tonumeral(fs, v, NULL)) luaK_exp2RK(fs, v); /* else keep numeral, which may be an immediate operand */ break; @@ -1535,17 +1570,16 @@ static void codeconcat (FuncState *fs, expdesc *e1, expdesc *e2, int line) { */ void luaK_posfix (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2, int line) { + luaK_dischargevars(fs, e2); switch (opr) { case OPR_AND: { - lua_assert(e1->t == NO_JUMP); /* list closed by 'luK_infix' */ - luaK_dischargevars(fs, e2); + lua_assert(e1->t == NO_JUMP); /* list closed by 'luaK_infix' */ luaK_concat(fs, &e2->f, e1->f); *e1 = *e2; break; } case OPR_OR: { - lua_assert(e1->f == NO_JUMP); /* list closed by 'luK_infix' */ - luaK_dischargevars(fs, e2); + lua_assert(e1->f == NO_JUMP); /* list closed by 'luaK_infix' */ luaK_concat(fs, &e2->t, e1->t); *e1 = *e2; break; diff --git a/lcode.h b/lcode.h index 0758f88dee..c49532957c 100644 --- a/lcode.h +++ b/lcode.h @@ -51,6 +51,7 @@ typedef enum UnOpr { OPR_MINUS, OPR_BNOT, OPR_NOT, OPR_LEN, OPR_NOUNOPR } UnOpr; #define luaK_jumpto(fs,t) luaK_patchlist(fs, luaK_jump(fs), t) +LUAI_FUNC int luaK_tonumeral (FuncState *fs, const expdesc *e, TValue *v); LUAI_FUNC int luaK_codeABx (FuncState *fs, OpCode o, int A, unsigned int Bx); LUAI_FUNC int luaK_codeAsBx (FuncState *fs, OpCode o, int A, int Bx); LUAI_FUNC int luaK_codeABCk (FuncState *fs, OpCode o, int A, diff --git a/lparser.c b/lparser.c index 3c68f2d7c3..875f7d043d 100644 --- a/lparser.c +++ b/lparser.c @@ -206,6 +206,7 @@ static Vardesc *new_localvar (LexState *ls, TString *name) { var = &dyd->actvar.arr[dyd->actvar.n++]; var->idx = cast(short, reg); var->ro = 0; + setnilvalue(&var->val); return var; } @@ -238,7 +239,7 @@ static LocVar *getlocvar (FuncState *fs, int i) { ** where that variable was defined. Return NULL if expression ** is neither a local variable nor an upvalue. */ -static Vardesc *getvardesc (FuncState **fs, expdesc *e) { +Vardesc *luaY_getvardesc (FuncState **fs, const expdesc *e) { if (e->k == VLOCAL) return getlocalvardesc(*fs, e->u.var.idx); else if (e->k != VUPVAL) @@ -261,7 +262,7 @@ static Vardesc *getvardesc (FuncState **fs, expdesc *e) { static void check_readonly (LexState *ls, expdesc *e) { FuncState *fs = ls->fs; - Vardesc *vardesc = getvardesc(&fs, e); + Vardesc *vardesc = luaY_getvardesc(&fs, e); if (vardesc && vardesc->ro) { /* is variable local and const? */ const char *msg = luaO_pushfstring(ls->L, "attempt to assign to const variable '%s'", @@ -1678,20 +1679,13 @@ static void commonlocalstat (LexState *ls) { static void tocloselocalstat (LexState *ls, Vardesc *var) { FuncState *fs = ls->fs; var->ro = 1; /* to-be-closed variables are always read-only */ - markupval(fs, fs->nactvar); + markupval(fs, fs->nactvar + 1); fs->bl->insidetbc = 1; /* in the scope of a to-be-closed variable */ - luaK_codeABC(fs, OP_TBC, fs->nactvar - 1, 0, 0); + luaK_codeABC(fs, OP_TBC, fs->nactvar, 0, 0); } -static void attriblocalstat (LexState *ls) { - Vardesc *var; - TString *attr = str_checkname(ls); - testnext(ls, '>'); - var = new_localvar(ls, str_checkname(ls)); - checknext(ls, '='); - exp1(ls); - adjustlocalvars(ls, 1); +static void checkattrib (LexState *ls, TString *attr, Vardesc *var) { if (strcmp(getstr(attr), "const") == 0) var->ro = 1; /* set variable as read-only */ else if (strcmp(getstr(attr), "toclose") == 0) @@ -1702,6 +1696,22 @@ static void attriblocalstat (LexState *ls) { } +static void attriblocalstat (LexState *ls) { + FuncState *fs = ls->fs; + Vardesc *var; + expdesc e; + TString *attr = str_checkname(ls); + testnext(ls, '>'); + var = new_localvar(ls, str_checkname(ls)); + checknext(ls, '='); + expr(ls, &e); + checkattrib(ls, attr, var); + luaK_tonumeral(fs, &e, &var->val); + luaK_exp2nextreg(fs, &e); + adjustlocalvars(ls, 1); +} + + static void localstat (LexState *ls) { /* stat -> LOCAL NAME {',' NAME} ['=' explist] | LOCAL *toclose NAME '=' exp */ diff --git a/lparser.h b/lparser.h index 228d4a5cb5..b708de25b3 100644 --- a/lparser.h +++ b/lparser.h @@ -81,6 +81,7 @@ typedef struct expdesc { /* description of an active local variable */ typedef struct Vardesc { + TValue val; /* constant value (if variable is 'const') */ short idx; /* index of the variable in the Proto's 'locvars' array */ lu_byte ro; /* true if variable is 'const' */ } Vardesc; @@ -143,6 +144,7 @@ typedef struct FuncState { } FuncState; +LUAI_FUNC Vardesc *luaY_getvardesc (FuncState **fs, const expdesc *e); LUAI_FUNC LClosure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, Dyndata *dyd, const char *name, int firstchar); From 4d46289331395a845c5de1f6c0e0fe873c50db4f Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 3 Jul 2019 14:18:07 -0300 Subject: [PATCH 0444/1145] Local attributes can be used in list of local variables The syntax for local attributes ('const'/'toclose') was unified with the regular syntax for local variables, so that we can have variables with attributes in local definitions with multiple names; for instance: local f, err = io.open(fname) This new syntax does not implement constant propagation, yet. This commit also has some small improvements to the manual. --- lparser.c | 90 +++++++++++++++++++++-------------------------- manual/manual.of | 60 +++++++++++++++++-------------- testes/locals.lua | 34 ++++++++++++++---- 3 files changed, 103 insertions(+), 81 deletions(-) diff --git a/lparser.c b/lparser.c index 875f7d043d..52486e08d8 100644 --- a/lparser.c +++ b/lparser.c @@ -1656,13 +1656,50 @@ static void localfunc (LexState *ls) { } -static void commonlocalstat (LexState *ls) { - /* stat -> LOCAL NAME {',' NAME} ['=' explist] */ +static int getlocalattribute (LexState *ls) { + /* ATTRIB -> ['<' Name '>'] */ + if (testnext(ls, '<')) { + const char *attr = getstr(str_checkname(ls)); + checknext(ls, '>'); + if (strcmp(attr, "const") == 0) + return 1; /* read-only variable */ + else if (strcmp(attr, "toclose") == 0) + return 2; /* to-be-closed variable */ + else + luaK_semerror(ls, + luaO_pushfstring(ls->L, "unknown attribute '%s'", attr)); + } + return 0; +} + + +static void checktoclose (LexState *ls, int toclose) { + if (toclose != -1) { /* is there a to-be-closed variable? */ + FuncState *fs = ls->fs; + markupval(fs, fs->nactvar + toclose + 1); + fs->bl->insidetbc = 1; /* in the scope of a to-be-closed variable */ + luaK_codeABC(fs, OP_TBC, fs->nactvar + toclose, 0, 0); + } +} + + +static void localstat (LexState *ls) { + /* stat -> LOCAL ATTRIB NAME {',' ATTRIB NAME} ['=' explist] */ + int toclose = -1; /* index of to-be-closed variable (if any) */ int nvars = 0; int nexps; expdesc e; do { - new_localvar(ls, str_checkname(ls)); + int kind = getlocalattribute(ls); + Vardesc *var = new_localvar(ls, str_checkname(ls)); + if (kind != 0) { /* is there an attribute? */ + var->ro = 1; /* all attributes make variable read-only */ + if (kind == 2) { /* to-be-closed? */ + if (toclose != -1) /* one already present? */ + luaK_semerror(ls, "multiple to-be-closed variables in local list"); + toclose = nvars; + } + } nvars++; } while (testnext(ls, ',')); if (testnext(ls, '=')) @@ -1672,56 +1709,11 @@ static void commonlocalstat (LexState *ls) { nexps = 0; } adjust_assign(ls, nvars, nexps, &e); + checktoclose(ls, toclose); adjustlocalvars(ls, nvars); } -static void tocloselocalstat (LexState *ls, Vardesc *var) { - FuncState *fs = ls->fs; - var->ro = 1; /* to-be-closed variables are always read-only */ - markupval(fs, fs->nactvar + 1); - fs->bl->insidetbc = 1; /* in the scope of a to-be-closed variable */ - luaK_codeABC(fs, OP_TBC, fs->nactvar, 0, 0); -} - - -static void checkattrib (LexState *ls, TString *attr, Vardesc *var) { - if (strcmp(getstr(attr), "const") == 0) - var->ro = 1; /* set variable as read-only */ - else if (strcmp(getstr(attr), "toclose") == 0) - tocloselocalstat(ls, var); - else - luaK_semerror(ls, - luaO_pushfstring(ls->L, "unknown attribute '%s'", getstr(attr))); -} - - -static void attriblocalstat (LexState *ls) { - FuncState *fs = ls->fs; - Vardesc *var; - expdesc e; - TString *attr = str_checkname(ls); - testnext(ls, '>'); - var = new_localvar(ls, str_checkname(ls)); - checknext(ls, '='); - expr(ls, &e); - checkattrib(ls, attr, var); - luaK_tonumeral(fs, &e, &var->val); - luaK_exp2nextreg(fs, &e); - adjustlocalvars(ls, 1); -} - - -static void localstat (LexState *ls) { - /* stat -> LOCAL NAME {',' NAME} ['=' explist] - | LOCAL *toclose NAME '=' exp */ - if (testnext(ls, '<')) - attriblocalstat(ls); - else - commonlocalstat(ls); -} - - static int funcname (LexState *ls, expdesc *v) { /* funcname -> NAME {fieldsel} [':' NAME] */ int ismethod = 0; diff --git a/manual/manual.of b/manual/manual.of index e941695678..136e902252 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -1399,23 +1399,30 @@ they must all result in numbers. Their values are called respectively the @emph{initial value}, the @emph{limit}, and the @emph{step}. If the step is absent, it defaults @N{to 1}. -Then the loop body is repeated with the value of the control variable + +If both the initial value and the step are integers, +the loop is done with integers; +note that the limit may not be an integer. +Otherwise, the loop is done with floats. +(Beware of floating-point accuracy in this case.) + +After that initialization, +the loop body is repeated with the value of the control variable going through an arithmetic progression, starting at the initial value, -with a common difference given by the step, -until that value passes the limit. +with a common difference given by the step. A negative step makes a decreasing sequence; a step equal to zero raises an error. +The loop continues while the value is less than +or equal to the limit +(greater than or equal to for a negative step). If the initial value is already greater than the limit (or less than, if the step is negative), the body is not executed. -If both the initial value and the step are integers, -the loop is done with integers; -in this case, the range of the control variable is clipped -by the range of integers. -Otherwise, the loop is done with floats. -(Beware of floating-point accuracy in this case.) +For integer loops, +the control variable never wraps around; +instead, the loop ends in case of an overflow. You should not change the value of the control variable during the loop. @@ -1490,22 +1497,25 @@ Function calls are explained in @See{functioncall}. @x{Local variables} can be declared anywhere inside a block. The declaration can include an initialization: @Produc{ -@producname{stat}@producbody{@Rw{local} namelist @bnfopt{@bnfter{=} explist}} -@producname{stat}@producbody{ - @Rw{local} @bnfter{<} Name @bnfter{>} Name @bnfter{=} exp -}} +@producname{stat}@producbody{@Rw{local} attnamelist @bnfopt{@bnfter{=} explist}} +@producname{attnamelist}@producbody{ + attrib @bnfNter{Name} @bnfrep{@bnfter{,} attrib @bnfNter{Name}}} +} If present, an initial assignment has the same semantics of a multiple assignment @see{assignment}. Otherwise, all variables are initialized with @nil. -The second syntax declares a local with a given attribute, -which is the name between the angle brackets. -In this case, there must be an initialization. + +Each variable name may be preceded by an attribute +(a name between angle brackets): +@Produc{ +@producname{attrib}@producbody{@bnfopt{@bnfter{<} @bnfNter{Name} @bnfter{>}}} +} There are two possible attributes: @id{const}, which declares a @x{constant variable}, that is, a variable that cannot be assigned to after its initialization; and @id{toclose}, which declares a to-be-closed variable @see{to-be-closed}. - +A list of variables can contain at most one to-be-closed variable. A chunk is also a block @see{chunks}, and so local variables can be declared in a chunk outside any explicit block. @@ -1516,12 +1526,6 @@ The visibility rules for local variables are explained in @See{visibility}. @sect3{to-be-closed| @title{To-be-closed Variables} -A local variable can be declared as a @def{to-be-closed} variable, -using the identifier @id{toclose} as its attribute: -@Produc{ -@producname{stat}@producbody{ - @Rw{local} @bnfter{<} @id{toclose} @bnfter{>} Name @bnfter{=} exp -}} A to-be-closed variable behaves like a constant local variable, except that its value is @emph{closed} whenever the variable goes out of scope, including normal block termination, @@ -8215,7 +8219,7 @@ then @id{date} returns the date as a string, formatted according to the same rules as the @ANSI{strftime}. If @id{format} is absent, it defaults to @St{%c}, -which gives a reasonable date and time representation +which gives a human-readable date and time representation using the current locale. On non-POSIX systems, @@ -9022,10 +9026,14 @@ and @bnfNter{LiteralString}, see @See{lexical}.) @OrNL @Rw{for} namelist @Rw{in} explist @Rw{do} block @Rw{end} @OrNL @Rw{function} funcname funcbody @OrNL @Rw{local} @Rw{function} @bnfNter{Name} funcbody -@OrNL @Rw{local} namelist @bnfopt{@bnfter{=} explist} -@OrNL @Rw{local} @bnfter{<} Name @bnfter{>} Name @bnfter{=} exp +@OrNL @Rw{local} attnamelist @bnfopt{@bnfter{=} explist} } +@producname{attnamelist}@producbody{ + attrib @bnfNter{Name} @bnfrep{@bnfter{,} attrib @bnfNter{Name}}} + +@producname{attrib}@producbody{@bnfopt{@bnfter{<} @bnfNter{Name} @bnfter{>}}} + @producname{retstat}@producbody{@Rw{return} @bnfopt{explist} @bnfopt{@bnfter{;}}} diff --git a/testes/locals.lua b/testes/locals.lua index a41b6f0ed4..50230a2747 100644 --- a/testes/locals.lua +++ b/testes/locals.lua @@ -173,12 +173,32 @@ end assert(x==20) +do -- constants + local a, b, c = 10, 20, 30 + b = a + c + b -- 'b' is not constant + assert(a == 10 and b == 60 and c == 30) + local function checkro (code, name) + local st, msg = load(code) + local gab = string.format("attempt to assign to const variable '%s'", name) + assert(not st and string.find(msg, gab)) + end + checkro("local x, y, z = 10, 20, 30; x = 11; y = 12", "y") + checkro("local x, y, z = 10, 20, 30; x = 11", "x") + checkro("local x, y, z = 10, 20, 30; y = 10; z = 11", "z") +end + + print"testing to-be-closed variables" local function stack(n) n = ((n == 0) or stack(n - 1)) end -local function func2close (f) - return setmetatable({}, {__close = f}) +local function func2close (f, x, y) + local obj = setmetatable({}, {__close = f}) + if x then + return x, obj, y + else + return obj + end end @@ -187,10 +207,11 @@ do do local x = setmetatable({"x"}, {__close = function (self) a[#a + 1] = self[1] end}) - local y = func2close(function (self, err) - assert(err == nil); a[#a + 1] = "y" - end) + local w, y, z = func2close(function (self, err) + assert(err == nil); a[#a + 1] = "y" + end, 10, 20) a[#a + 1] = "in" + assert(w == 10 and z == 20) end a[#a + 1] = "out" assert(a[1] == "in" and a[2] == "y" and a[3] == "x" and a[4] == "out") @@ -199,7 +220,8 @@ end do local X = false - local closescope = func2close(function () stack(10); X = true end) + local x, closescope = func2close(function () stack(10); X = true end, 100) + assert(x == 100); x = 101; -- 'x' is not read-only -- closing functions do not corrupt returning values local function foo (x) From e888976bc6ba5592fb8ab8ecc04a8f63e217aa74 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 5 Jul 2019 15:03:15 -0300 Subject: [PATCH 0445/1145] Details (typos in comments) --- lauxlib.c | 2 +- lgc.c | 2 +- liolib.c | 2 +- loadlib.c | 2 +- lopcodes.h | 6 +++--- lstate.h | 8 ++++---- ltablib.c | 2 +- ltm.c | 12 ++++++------ luaconf.h | 2 +- 9 files changed, 19 insertions(+), 19 deletions(-) diff --git a/lauxlib.c b/lauxlib.c index d49ef573c3..e3a7a5778b 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -62,7 +62,7 @@ static int findfield (lua_State *L, int objidx, int level) { else if (findfield(L, objidx, level - 1)) { /* try recursively */ /* stack: lib_name, lib_table, field_name (top) */ lua_pushliteral(L, "."); /* place '.' between the two names */ - lua_replace(L, -3); /* (in the slot ocupied by table) */ + lua_replace(L, -3); /* (in the slot occupied by table) */ lua_concat(L, 3); /* lib_name.field_name */ return 1; } diff --git a/lgc.c b/lgc.c index 8444566ccb..aa6921bcb2 100644 --- a/lgc.c +++ b/lgc.c @@ -1250,7 +1250,7 @@ static void setminordebt (global_State *g) { /* ** Does a major collection after last collection was a "bad collection". ** -** When the program is building a big struture, it allocates lots of +** When the program is building a big structure, it allocates lots of ** memory but generates very little garbage. In those scenarios, ** the generational mode just wastes time doing small collections, and ** major collections are frequently what we call a "bad collection", a diff --git a/liolib.c b/liolib.c index 2c0c6a3b70..83fbb1727f 100644 --- a/liolib.c +++ b/liolib.c @@ -338,7 +338,7 @@ static int io_readline (lua_State *L); #define MAXARGLINE 250 /* -** Auxiliar function to create the iteration function for 'lines'. +** Auxiliary function to create the iteration function for 'lines'. ** The iteration function is a closure over 'io_readline', with ** the following upvalues: ** 1) The file being read (first value in the stack) diff --git a/loadlib.c b/loadlib.c index ff73a4599f..b72dd88537 100644 --- a/loadlib.c +++ b/loadlib.c @@ -308,7 +308,7 @@ static void setpath (lua_State *L, const char *fieldname, luaL_addchar(&b, *LUA_PATH_SEP); } luaL_addstring(&b, dft); /* add default */ - if (dftmark < path + len - 2) { /* is there a sufix after ';;'? */ + if (dftmark < path + len - 2) { /* is there a suffix after ';;'? */ luaL_addchar(&b, *LUA_PATH_SEP); luaL_addlstring(&b, dftmark + 2, (path + len - 2) - dftmark); } diff --git a/lopcodes.h b/lopcodes.h index a314dcd18c..7bbbb0e56b 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -286,9 +286,9 @@ OP_RETURN,/* A B C return R(A), ... ,R(A+B-2) (see note) */ OP_RETURN0,/* return */ OP_RETURN1,/* A return R(A) */ -OP_FORLOOP,/* A Bx R(A)+=R(A+2); - if R(A) ; + if not to run then pc+=Bx+1; */ OP_TFORPREP,/* A Bx create upvalue for R(A + 3); pc+=Bx */ OP_TFORCALL,/* A C R(A+4), ... ,R(A+3+C) := R(A)(R(A+1), R(A+2)); */ diff --git a/lstate.h b/lstate.h index d3a64f941f..2a95dd163c 100644 --- a/lstate.h +++ b/lstate.h @@ -74,7 +74,7 @@ ** higher part counts the number of non-yieldable calls in the stack. ** (They are together so that we can change both with one instruction.) ** -** Because calls to external C functions can use of unkown amount +** Because calls to external C functions can use an unknown amount ** of space (e.g., functions using an auxiliary buffer), calls ** to these functions add more than one to the count (see CSTACKCF). ** @@ -185,9 +185,9 @@ typedef struct CallInfo { union { int funcidx; /* called-function index */ int nyield; /* number of values yielded */ - struct { /* info about transfered values (for call/return hooks) */ - unsigned short ftransfer; /* offset of first value transfered */ - unsigned short ntransfer; /* number of values transfered */ + struct { /* info about transferred values (for call/return hooks) */ + unsigned short ftransfer; /* offset of first value transferred */ + unsigned short ntransfer; /* number of values transferred */ } transferinfo; } u2; short nresults; /* expected number of results from this function */ diff --git a/ltablib.c b/ltablib.c index 7e7a101211..d344a47e9a 100644 --- a/ltablib.c +++ b/ltablib.c @@ -338,7 +338,7 @@ static IdxT choosePivot (IdxT lo, IdxT up, unsigned int rnd) { /* -** QuickSort algorithm (recursive function) +** Quicksort algorithm (recursive function) */ static void auxsort (lua_State *L, IdxT lo, IdxT up, unsigned int rnd) { diff --git a/ltm.c b/ltm.c index c4fd762bc1..247394447c 100644 --- a/ltm.c +++ b/ltm.c @@ -168,8 +168,8 @@ void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2, void luaT_trybinassocTM (lua_State *L, const TValue *p1, const TValue *p2, - StkId res, int inv, TMS event) { - if (inv) + StkId res, int flip, TMS event) { + if (flip) luaT_trybinTM(L, p2, p1, res, event); else luaT_trybinTM(L, p1, p2, res, event); @@ -177,10 +177,10 @@ void luaT_trybinassocTM (lua_State *L, const TValue *p1, const TValue *p2, void luaT_trybiniTM (lua_State *L, const TValue *p1, lua_Integer i2, - int inv, StkId res, TMS event) { + int flip, StkId res, TMS event) { TValue aux; setivalue(&aux, i2); - luaT_trybinassocTM(L, p1, &aux, res, inv, event); + luaT_trybinassocTM(L, p1, &aux, res, flip, event); } @@ -205,14 +205,14 @@ int luaT_callorderTM (lua_State *L, const TValue *p1, const TValue *p2, int luaT_callorderiTM (lua_State *L, const TValue *p1, int v2, - int inv, int isfloat, TMS event) { + int flip, int isfloat, TMS event) { TValue aux; const TValue *p2; if (isfloat) { setfltvalue(&aux, cast_num(v2)); } else setivalue(&aux, v2); - if (inv) { /* arguments were exchanged? */ + if (flip) { /* arguments were exchanged? */ p2 = p1; p1 = &aux; /* correct them */ } else diff --git a/luaconf.h b/luaconf.h index 72018855e7..8f13743bd0 100644 --- a/luaconf.h +++ b/luaconf.h @@ -493,7 +493,7 @@ @@ LUA_UNSIGNED is the unsigned version of LUA_INTEGER. ** @@ LUAI_UACINT is the result of a 'default argument promotion' -@@ over a lUA_INTEGER. +@@ over a LUA_INTEGER. @@ LUA_INTEGER_FRMLEN is the length modifier for reading/writing integers. @@ LUA_INTEGER_FMT is the format for writing integers. @@ LUA_MAXINTEGER is the maximum value for a LUA_INTEGER. From 54f7b46c1e8a0188e1649046a3a72522f2d769f4 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 9 Jul 2019 10:43:17 -0300 Subject: [PATCH 0446/1145] New implementation for constants VLOCAL expressions keep a reference to their corresponding 'Vardesc', and 'Upvaldesc' (for upvalues) has a field 'ro' (read-only). So, it is easier to check whether a variable is read-only. The decoupling in VLOCAL between 'vidx' ('Vardesc' index) and 'sidx' (stack index) should also help the forthcoming implementation of compile-time constant propagation. --- lcode.c | 60 ++++-------------- lcode.h | 1 - ldump.c | 1 + lobject.h | 1 + lparser.c | 158 +++++++++++++++++++++++++--------------------- lparser.h | 16 +++-- lundump.c | 1 + testes/locals.lua | 18 ++++-- 8 files changed, 125 insertions(+), 131 deletions(-) diff --git a/lcode.c b/lcode.c index 1005f1b7a1..cb6ea0dc05 100644 --- a/lcode.c +++ b/lcode.c @@ -52,7 +52,7 @@ l_noret luaK_semerror (LexState *ls, const char *msg) { ** If expression is a numeric constant, fills 'v' with its value ** and returns 1. Otherwise, returns 0. */ -int luaK_tonumeral (FuncState *fs, const expdesc *e, TValue *v) { +static int tonumeral (const expdesc *e, TValue *v) { if (hasjumps(e)) return 0; /* not a numeral */ switch (e->k) { @@ -62,41 +62,11 @@ int luaK_tonumeral (FuncState *fs, const expdesc *e, TValue *v) { case VKFLT: if (v) setfltvalue(v, e->u.nval); return 1; - case VUPVAL: { /* may be a constant */ - Vardesc *vd = luaY_getvardesc(&fs, e); - if (v && vd && !ttisnil(&vd->val)) { - setobj(fs->ls->L, v, &vd->val); - return 1; - } /* else */ - } /* FALLTHROUGH */ default: return 0; } } -/* -** If expression 'e' is a constant, change 'e' to represent -** the constant value. -*/ -static int const2exp (FuncState *fs, expdesc *e) { - Vardesc *vd = luaY_getvardesc(&fs, e); - if (vd) { - TValue *v = &vd->val; - switch (ttypetag(v)) { - case LUA_TNUMINT: - e->k = VKINT; - e->u.ival = ivalue(v); - return 1; - case LUA_TNUMFLT: - e->k = VKFLT; - e->u.nval = fltvalue(v); - return 1; - } - } - return 0; -} - - /* ** Return the previous instruction of the current code. If there ** may be a jump target between the current instruction and the @@ -708,15 +678,13 @@ void luaK_setoneret (FuncState *fs, expdesc *e) { void luaK_dischargevars (FuncState *fs, expdesc *e) { switch (e->k) { case VLOCAL: { /* already in a register */ - e->u.info = e->u.var.idx; + e->u.info = e->u.var.sidx; e->k = VNONRELOC; /* becomes a non-relocatable value */ break; } case VUPVAL: { /* move value to some (pending) register */ - if (!const2exp(fs, e)) { - e->u.info = luaK_codeABC(fs, OP_GETUPVAL, 0, e->u.var.idx, 0); - e->k = VRELOC; - } + e->u.info = luaK_codeABC(fs, OP_GETUPVAL, 0, e->u.info, 0); + e->k = VRELOC; break; } case VINDEXUP: { @@ -971,12 +939,12 @@ void luaK_storevar (FuncState *fs, expdesc *var, expdesc *ex) { switch (var->k) { case VLOCAL: { freeexp(fs, ex); - exp2reg(fs, ex, var->u.var.idx); /* compute 'ex' into proper place */ + exp2reg(fs, ex, var->u.var.sidx); /* compute 'ex' into proper place */ return; } case VUPVAL: { int e = luaK_exp2anyreg(fs, ex); - luaK_codeABC(fs, OP_SETUPVAL, e, var->u.var.idx, 0); + luaK_codeABC(fs, OP_SETUPVAL, e, var->u.info, 0); break; } case VINDEXUP: { @@ -1203,13 +1171,13 @@ void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) { if (t->k == VUPVAL && !isKstr(fs, k)) /* upvalue indexed by non string? */ luaK_exp2anyreg(fs, t); /* put it in a register */ if (t->k == VUPVAL) { - t->u.ind.t = t->u.var.idx; /* upvalue index */ + t->u.ind.t = t->u.info; /* upvalue index */ t->u.ind.idx = k->u.info; /* literal string */ t->k = VINDEXUP; } else { /* register index of the table */ - t->u.ind.t = (t->k == VLOCAL) ? t->u.var.idx: t->u.info; + t->u.ind.t = (t->k == VLOCAL) ? t->u.var.sidx: t->u.info; if (isKstr(fs, k)) { t->u.ind.idx = k->u.info; /* literal string */ t->k = VINDEXSTR; @@ -1252,9 +1220,7 @@ static int validop (int op, TValue *v1, TValue *v2) { static int constfolding (FuncState *fs, int op, expdesc *e1, const expdesc *e2) { TValue v1, v2, res; - if (!luaK_tonumeral(fs, e1, &v1) || - !luaK_tonumeral(fs, e2, &v2) || - !validop(op, &v1, &v2)) + if (!tonumeral(e1, &v1) || !tonumeral(e2, &v2) || !validop(op, &v1, &v2)) return 0; /* non-numeric operands or not safe to fold */ luaO_rawarith(fs->ls->L, op, &v1, &v2, &res); /* does operation */ if (ttisinteger(&res)) { @@ -1341,7 +1307,7 @@ static void codearith (FuncState *fs, OpCode op, expdesc *e1, expdesc *e2, int flip, int line) { if (isSCint(e2)) /* immediate operand? */ codebini(fs, cast(OpCode, op - OP_ADD + OP_ADDI), e1, e2, flip, line); - else if (luaK_tonumeral(fs, e2, NULL) && luaK_exp2K(fs, e2)) { /* K operand? */ + else if (tonumeral(e2, NULL) && luaK_exp2K(fs, e2)) { /* K operand? */ int v2 = e2->u.info; /* K index */ op = cast(OpCode, op - OP_ADD + OP_ADDK); finishbinexpval(fs, e1, e2, op, v2, flip, line); @@ -1362,7 +1328,7 @@ static void codearith (FuncState *fs, OpCode op, static void codecommutative (FuncState *fs, OpCode op, expdesc *e1, expdesc *e2, int line) { int flip = 0; - if (luaK_tonumeral(fs, e1, NULL)) { /* is first operand a numeric constant? */ + if (tonumeral(e1, NULL)) { /* is first operand a numeric constant? */ swapexps(e1, e2); /* change order */ flip = 1; } @@ -1519,13 +1485,13 @@ void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) { case OPR_MOD: case OPR_POW: case OPR_BAND: case OPR_BOR: case OPR_BXOR: case OPR_SHL: case OPR_SHR: { - if (!luaK_tonumeral(fs, v, NULL)) + if (!tonumeral(v, NULL)) luaK_exp2anyreg(fs, v); /* else keep numeral, which may be folded with 2nd operand */ break; } case OPR_EQ: case OPR_NE: { - if (!luaK_tonumeral(fs, v, NULL)) + if (!tonumeral(v, NULL)) luaK_exp2RK(fs, v); /* else keep numeral, which may be an immediate operand */ break; diff --git a/lcode.h b/lcode.h index c49532957c..0758f88dee 100644 --- a/lcode.h +++ b/lcode.h @@ -51,7 +51,6 @@ typedef enum UnOpr { OPR_MINUS, OPR_BNOT, OPR_NOT, OPR_LEN, OPR_NOUNOPR } UnOpr; #define luaK_jumpto(fs,t) luaK_patchlist(fs, luaK_jump(fs), t) -LUAI_FUNC int luaK_tonumeral (FuncState *fs, const expdesc *e, TValue *v); LUAI_FUNC int luaK_codeABx (FuncState *fs, OpCode o, int A, unsigned int Bx); LUAI_FUNC int luaK_codeAsBx (FuncState *fs, OpCode o, int A, int Bx); LUAI_FUNC int luaK_codeABCk (FuncState *fs, OpCode o, int A, diff --git a/ldump.c b/ldump.c index c447557682..3d5b7b325f 100644 --- a/ldump.c +++ b/ldump.c @@ -149,6 +149,7 @@ static void DumpUpvalues (const Proto *f, DumpState *D) { for (i = 0; i < n; i++) { DumpByte(f->upvalues[i].instack, D); DumpByte(f->upvalues[i].idx, D); + DumpByte(f->upvalues[i].ro, D); } } diff --git a/lobject.h b/lobject.h index 403b6047ba..64366a94d2 100644 --- a/lobject.h +++ b/lobject.h @@ -460,6 +460,7 @@ typedef struct Upvaldesc { TString *name; /* upvalue name (for debug information) */ lu_byte instack; /* whether it is in stack (register) */ lu_byte idx; /* index of upvalue (in stack or in outer function's list) */ + lu_byte ro; /* true if upvalue is read-only (const) */ } Upvaldesc; diff --git a/lparser.c b/lparser.c index 52486e08d8..1551cda990 100644 --- a/lparser.c +++ b/lparser.c @@ -156,13 +156,6 @@ static void init_exp (expdesc *e, expkind k, int i) { } -static void init_var (expdesc *e, expkind k, int i) { - e->f = e->t = NO_JUMP; - e->k = k; - e->u.var.idx = i; -} - - static void codestring (LexState *ls, expdesc *e, TString *s) { init_exp(e, VK, luaK_stringK(ls->fs, s)); } @@ -177,16 +170,15 @@ static void codename (LexState *ls, expdesc *e) { ** Register a new local variable in the active 'Proto' (for debug ** information). */ -static int registerlocalvar (LexState *ls, TString *varname) { - FuncState *fs = ls->fs; +static int registerlocalvar (lua_State *L, FuncState *fs, TString *varname) { Proto *f = fs->f; int oldsize = f->sizelocvars; - luaM_growvector(ls->L, f->locvars, fs->nlocvars, f->sizelocvars, + luaM_growvector(L, f->locvars, fs->nlocvars, f->sizelocvars, LocVar, SHRT_MAX, "local variables"); while (oldsize < f->sizelocvars) f->locvars[oldsize++].varname = NULL; f->locvars[fs->nlocvars].varname = varname; - luaC_objbarrier(ls->L, f, varname); + luaC_objbarrier(L, f, varname); return fs->nlocvars++; } @@ -195,18 +187,19 @@ static int registerlocalvar (LexState *ls, TString *varname) { ** Create a new local variable with the given 'name'. */ static Vardesc *new_localvar (LexState *ls, TString *name) { + lua_State *L = ls->L; FuncState *fs = ls->fs; Dyndata *dyd = ls->dyd; Vardesc *var; - int reg = registerlocalvar(ls, name); + int reg = registerlocalvar(L, fs, name); checklimit(fs, dyd->actvar.n + 1 - fs->firstlocal, MAXVARS, "local variables"); - luaM_growvector(ls->L, dyd->actvar.arr, dyd->actvar.n + 1, - dyd->actvar.size, Vardesc, MAX_INT, "local variables"); + luaM_growvector(L, dyd->actvar.arr, dyd->actvar.n + 1, + dyd->actvar.size, Vardesc, USHRT_MAX, "local variables"); var = &dyd->actvar.arr[dyd->actvar.n++]; - var->idx = cast(short, reg); + var->pidx = cast(short, reg); var->ro = 0; - setnilvalue(&var->val); + setnilvalue(var); return var; } @@ -223,50 +216,47 @@ static Vardesc *getlocalvardesc (FuncState *fs, int i) { return &fs->ls->dyd->actvar.arr[fs->firstlocal + i]; } + /* ** Get the debug-information entry for current variable 'i'. */ -static LocVar *getlocvar (FuncState *fs, int i) { - int idx = getlocalvardesc(fs, i)->idx; +static LocVar *localdebuginfo (FuncState *fs, int i) { + int idx = getlocalvardesc(fs, i)->pidx; lua_assert(idx < fs->nlocvars); return &fs->f->locvars[idx]; } -/* -** Return the "variable description" (Vardesc) of a given -** local variable and update 'fs' to point to the function -** where that variable was defined. Return NULL if expression -** is neither a local variable nor an upvalue. -*/ -Vardesc *luaY_getvardesc (FuncState **fs, const expdesc *e) { - if (e->k == VLOCAL) - return getlocalvardesc(*fs, e->u.var.idx); - else if (e->k != VUPVAL) - return NULL; /* not a local variable */ - else { /* upvalue: must go up all levels up to the original local */ - int idx = e->u.var.idx; - for (;;) { - Upvaldesc *up = &(*fs)->f->upvalues[idx]; - *fs = (*fs)->prev; /* must look at the previous level */ - idx = up->idx; /* at this index */ - if (*fs == NULL) /* no more levels? (can happen only with _ENV) */ - return NULL; - else if (up->instack) /* got to the original level? */ - return getlocalvardesc(*fs, idx); - /* else repeat for previous level */ - } - } +static void init_var (FuncState *fs, expdesc *e, int i) { + e->f = e->t = NO_JUMP; + e->k = VLOCAL; + e->u.var.vidx = i; + e->u.var.sidx = getlocalvardesc(fs, i)->sidx; } static void check_readonly (LexState *ls, expdesc *e) { FuncState *fs = ls->fs; - Vardesc *vardesc = luaY_getvardesc(&fs, e); - if (vardesc && vardesc->ro) { /* is variable local and const? */ + TString *varname = NULL; /* to be set if variable is const */ + switch (e->k) { + case VLOCAL: { + Vardesc *vardesc = getlocalvardesc(fs, e->u.var.vidx); + if (vardesc->ro) + varname = fs->f->locvars[vardesc->pidx].varname; + break; + } + case VUPVAL: { + Upvaldesc *up = &fs->f->upvalues[e->u.info]; + if (up->ro) + varname = up->name; + break; + } + default: + return; /* other cases cannot be read-only */ + } + if (varname) { const char *msg = luaO_pushfstring(ls->L, - "attempt to assign to const variable '%s'", - getstr(fs->f->locvars[vardesc->idx].varname)); + "attempt to assign to const variable '%s'", getstr(varname)); luaK_semerror(ls, msg); /* error */ } } @@ -274,13 +264,15 @@ static void check_readonly (LexState *ls, expdesc *e) { /* ** Start the scope for the last 'nvars' created variables. -** (debug info.) */ static void adjustlocalvars (LexState *ls, int nvars) { FuncState *fs = ls->fs; - fs->nactvar = cast_byte(fs->nactvar + nvars); - for (; nvars; nvars--) { - getlocvar(fs, fs->nactvar - nvars)->startpc = fs->pc; + int i; + for (i = 0; i < nvars; i++) { + int varidx = fs->nactvar++; + Vardesc *var = getlocalvardesc(fs, varidx); + var->sidx = varidx; + fs->f->locvars[var->pidx].startpc = fs->pc; } } @@ -292,7 +284,7 @@ static void adjustlocalvars (LexState *ls, int nvars) { static void removevars (FuncState *fs, int tolevel) { fs->ls->dyd->actvar.n -= (fs->nactvar - tolevel); while (fs->nactvar > tolevel) - getlocvar(fs, --fs->nactvar)->endpc = fs->pc; + localdebuginfo(fs, --fs->nactvar)->endpc = fs->pc; } @@ -310,7 +302,7 @@ static int searchupvalue (FuncState *fs, TString *name) { } -static int newupvalue (FuncState *fs, TString *name, expdesc *v) { +static Upvaldesc *allocupvalue (FuncState *fs) { Proto *f = fs->f; int oldsize = f->sizeupvalues; checklimit(fs, fs->nups + 1, MAXUPVAL, "upvalues"); @@ -318,11 +310,28 @@ static int newupvalue (FuncState *fs, TString *name, expdesc *v) { Upvaldesc, MAXUPVAL, "upvalues"); while (oldsize < f->sizeupvalues) f->upvalues[oldsize++].name = NULL; - f->upvalues[fs->nups].instack = (v->k == VLOCAL); - f->upvalues[fs->nups].idx = cast_byte(v->u.var.idx); - f->upvalues[fs->nups].name = name; - luaC_objbarrier(fs->ls->L, f, name); - return fs->nups++; + return &f->upvalues[fs->nups++]; +} + + +static int newupvalue (FuncState *fs, TString *name, expdesc *v) { + Upvaldesc *up = allocupvalue(fs); + FuncState *prev = fs->prev; + if (v->k == VLOCAL) { + up->instack = 1; + up->idx = v->u.var.sidx; + up->ro = getlocalvardesc(prev, v->u.var.vidx)->ro; + lua_assert(eqstr(name, localdebuginfo(prev, v->u.var.vidx)->varname)); + } + else { + up->instack = 0; + up->idx = cast_byte(v->u.info); + up->ro = prev->f->upvalues[v->u.info].ro; + lua_assert(eqstr(name, prev->f->upvalues[v->u.info].name)); + } + up->name = name; + luaC_objbarrier(fs->ls->L, fs->f, name); + return fs->nups - 1; } @@ -333,7 +342,7 @@ static int newupvalue (FuncState *fs, TString *name, expdesc *v) { static int searchvar (FuncState *fs, TString *n) { int i; for (i = cast_int(fs->nactvar) - 1; i >= 0; i--) { - if (eqstr(n, getlocvar(fs, i)->varname)) + if (eqstr(n, localdebuginfo(fs, i)->varname)) return i; } return -1; /* not found */ @@ -364,9 +373,9 @@ static void singlevaraux (FuncState *fs, TString *n, expdesc *var, int base) { else { int v = searchvar(fs, n); /* look up locals at current level */ if (v >= 0) { /* found? */ - init_var(var, VLOCAL, v); /* variable is local */ + init_var(fs, var, v); /* variable is local */ if (!base) - markupval(fs, v); /* local will be used as an upval */ + markupval(fs, var->u.var.sidx); /* local will be used as an upval */ } else { /* not found as local at current level; try upvalues */ int idx = searchupvalue(fs, n); /* try existing upvalues */ @@ -377,7 +386,7 @@ static void singlevaraux (FuncState *fs, TString *n, expdesc *var, int base) { /* else was LOCAL or UPVAL */ idx = newupvalue(fs, n, var); /* will be a new upvalue */ } - init_var(var, VUPVAL, idx); /* new or old upvalue */ + init_exp(var, VUPVAL, idx); /* new or old upvalue */ } } } @@ -440,7 +449,7 @@ static void adjust_assign (LexState *ls, int nvars, int nexps, expdesc *e) { ** local variable. */ static l_noret jumpscopeerror (LexState *ls, Labeldesc *gt) { - const char *varname = getstr(getlocvar(ls->fs, gt->nactvar)->varname); + const char *varname = getstr(localdebuginfo(ls->fs, gt->nactvar)->varname); const char *msg = " at line %d jumps into the scope of local '%s'"; msg = luaO_pushfstring(ls->L, msg, getstr(gt->name), gt->line, varname); luaK_semerror(ls, msg); /* raise the error */ @@ -1259,20 +1268,20 @@ static void check_conflict (LexState *ls, struct LHS_assign *lh, expdesc *v) { for (; lh; lh = lh->prev) { /* check all previous assignments */ if (vkisindexed(lh->v.k)) { /* assignment to table field? */ if (lh->v.k == VINDEXUP) { /* is table an upvalue? */ - if (v->k == VUPVAL && lh->v.u.ind.t == v->u.var.idx) { + if (v->k == VUPVAL && lh->v.u.ind.t == v->u.info) { conflict = 1; /* table is the upvalue being assigned now */ lh->v.k = VINDEXSTR; lh->v.u.ind.t = extra; /* assignment will use safe copy */ } } else { /* table is a register */ - if (v->k == VLOCAL && lh->v.u.ind.t == v->u.var.idx) { + if (v->k == VLOCAL && lh->v.u.ind.t == v->u.var.sidx) { conflict = 1; /* table is the local being assigned now */ lh->v.u.ind.t = extra; /* assignment will use safe copy */ } /* is index the local being assigned? */ if (lh->v.k == VINDEXED && v->k == VLOCAL && - lh->v.u.ind.idx == v->u.var.idx) { + lh->v.u.ind.idx == v->u.var.sidx) { conflict = 1; lh->v.u.ind.idx = extra; /* previous assignment will use safe copy */ } @@ -1281,14 +1290,16 @@ static void check_conflict (LexState *ls, struct LHS_assign *lh, expdesc *v) { } 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.var.idx, 0); + if (v->k == VLOCAL) + luaK_codeABC(fs, OP_MOVE, extra, v->u.var.sidx, 0); + else + luaK_codeABC(fs, OP_GETUPVAL, extra, v->u.info, 0); luaK_reserveregs(fs, 1); } } /* -** Parse and compile a mulitple assignment. The first "variable" +** Parse and compile a multiple assignment. The first "variable" ** (a 'suffixedexp') was already read by the caller. ** ** assignment -> suffixedexp restassign @@ -1652,7 +1663,7 @@ static void localfunc (LexState *ls) { adjustlocalvars(ls, 1); /* enter its scope */ body(ls, &b, 0, ls->linenumber); /* function created in next register */ /* debug information will only see the variable after this point! */ - getlocvar(fs, b.u.info)->startpc = fs->pc; + localdebuginfo(fs, b.u.info)->startpc = fs->pc; } @@ -1870,11 +1881,14 @@ static void statement (LexState *ls) { */ static void mainfunc (LexState *ls, FuncState *fs) { BlockCnt bl; - expdesc v; + Upvaldesc *env; open_func(ls, fs, &bl); setvararg(fs, 0); /* main function is always declared vararg */ - init_var(&v, VLOCAL, 0); /* create and... */ - newupvalue(fs, ls->envn, &v); /* ...set environment upvalue */ + env = allocupvalue(fs); /* ...set environment upvalue */ + env->instack = 1; + env->idx = 0; + env->ro = 0; + env->name = ls->envn; luaX_next(ls); /* read first token */ statlist(ls); /* parse main body */ check(ls, TK_EOS); diff --git a/lparser.h b/lparser.h index b708de25b3..cc2ec14d4c 100644 --- a/lparser.h +++ b/lparser.h @@ -33,8 +33,9 @@ typedef enum { VKINT, /* integer constant; nval = numerical integer value */ VNONRELOC, /* expression has its value in a fixed register; info = result register */ - VLOCAL, /* local variable; var.idx = local register */ - VUPVAL, /* upvalue variable; var.idx = index of upvalue in 'upvalues' */ + VLOCAL, /* local variable; var.ridx = local register; + var.vidx = index in 'actvar.arr' */ + VUPVAL, /* upvalue variable; info = index of upvalue in 'upvalues' */ VINDEXED, /* indexed variable; ind.t = table register; ind.idx = key's R index */ @@ -70,8 +71,9 @@ typedef struct expdesc { short idx; /* index (R or "long" K) */ lu_byte t; /* table (register or upvalue) */ } ind; - struct { /* for local variables and upvalues */ - lu_byte idx; /* index of the variable */ + struct { /* for local variables */ + lu_byte sidx; /* index in the stack */ + unsigned short vidx; /* index in 'actvar.arr' */ } var; } u; int t; /* patch list of 'exit when true' */ @@ -81,9 +83,10 @@ typedef struct expdesc { /* description of an active local variable */ typedef struct Vardesc { - TValue val; /* constant value (if variable is 'const') */ - short idx; /* index of the variable in the Proto's 'locvars' array */ + TValuefields; /* constant value (if variable is 'const') */ lu_byte ro; /* true if variable is 'const' */ + lu_byte sidx; /* index of the variable in the stack */ + short pidx; /* index of the variable in the Proto's 'locvars' array */ } Vardesc; @@ -144,7 +147,6 @@ typedef struct FuncState { } FuncState; -LUAI_FUNC Vardesc *luaY_getvardesc (FuncState **fs, const expdesc *e); LUAI_FUNC LClosure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, Dyndata *dyd, const char *name, int firstchar); diff --git a/lundump.c b/lundump.c index c1cff9e1ab..5c0e94d67b 100644 --- a/lundump.c +++ b/lundump.c @@ -203,6 +203,7 @@ static void LoadUpvalues (LoadState *S, Proto *f) { for (i = 0; i < n; i++) { f->upvalues[i].instack = LoadByte(S); f->upvalues[i].idx = LoadByte(S); + f->upvalues[i].ro = LoadByte(S); } } diff --git a/testes/locals.lua b/testes/locals.lua index 50230a2747..0de00a98c1 100644 --- a/testes/locals.lua +++ b/testes/locals.lua @@ -177,14 +177,24 @@ do -- constants local a, b, c = 10, 20, 30 b = a + c + b -- 'b' is not constant assert(a == 10 and b == 60 and c == 30) - local function checkro (code, name) + local function checkro (name, code) local st, msg = load(code) local gab = string.format("attempt to assign to const variable '%s'", name) assert(not st and string.find(msg, gab)) end - checkro("local x, y, z = 10, 20, 30; x = 11; y = 12", "y") - checkro("local x, y, z = 10, 20, 30; x = 11", "x") - checkro("local x, y, z = 10, 20, 30; y = 10; z = 11", "z") + checkro("y", "local x, y, z = 10, 20, 30; x = 11; y = 12") + checkro("x", "local x, y, z = 10, 20, 30; x = 11") + checkro("z", "local x, y, z = 10, 20, 30; y = 10; z = 11") + + checkro("z", [[ + local a, z, b = 10; + function foo() a = 20; z = 32; end + ]]) + + checkro("var1", [[ + local a, var1 = 10; + function foo() a = 20; z = function () var1 = 12; end end + ]]) end From 3d296304ef14ac9a6d1fa9357541ddd9bb54722f Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 10 Jul 2019 14:00:22 -0300 Subject: [PATCH 0447/1145] Towards constant propagation This commit detaches the number of active variables from the number of variables in the stack, during compilation. Soon, compile-time constants will be propagated and therefore will not exist during run time (in the stack). --- lcode.c | 4 +- lparser.c | 113 ++++++++++++++++++++++++++++++++++++------------------ lparser.h | 12 ++++-- 3 files changed, 87 insertions(+), 42 deletions(-) diff --git a/lcode.c b/lcode.c index cb6ea0dc05..837253f4ea 100644 --- a/lcode.c +++ b/lcode.c @@ -458,7 +458,7 @@ void luaK_reserveregs (FuncState *fs, int n) { ) */ static void freereg (FuncState *fs, int reg) { - if (reg >= fs->nactvar) { + if (reg >= luaY_nvarstack(fs)) { fs->freereg--; lua_assert(reg == fs->freereg); } @@ -850,7 +850,7 @@ int luaK_exp2anyreg (FuncState *fs, expdesc *e) { if (e->k == VNONRELOC) { /* expression already has a register? */ if (!hasjumps(e)) /* no jumps? */ return e->u.info; /* result is already in a register */ - if (e->u.info >= fs->nactvar) { /* reg. is not a local? */ + if (e->u.info >= luaY_nvarstack(fs)) { /* reg. is not a local? */ exp2reg(fs, e, e->u.info); /* put final result in it */ return e->u.info; } diff --git a/lparser.c b/lparser.c index 1551cda990..c4626ba165 100644 --- a/lparser.c +++ b/lparser.c @@ -173,13 +173,13 @@ static void codename (LexState *ls, expdesc *e) { static int registerlocalvar (lua_State *L, FuncState *fs, TString *varname) { Proto *f = fs->f; int oldsize = f->sizelocvars; - luaM_growvector(L, f->locvars, fs->nlocvars, f->sizelocvars, + luaM_growvector(L, f->locvars, fs->ndebugvars, f->sizelocvars, LocVar, SHRT_MAX, "local variables"); while (oldsize < f->sizelocvars) f->locvars[oldsize++].varname = NULL; - f->locvars[fs->nlocvars].varname = varname; + f->locvars[fs->ndebugvars].varname = varname; luaC_objbarrier(L, f, varname); - return fs->nlocvars++; + return fs->ndebugvars++; } @@ -193,12 +193,13 @@ static Vardesc *new_localvar (LexState *ls, TString *name) { Vardesc *var; int reg = registerlocalvar(L, fs, name); checklimit(fs, dyd->actvar.n + 1 - fs->firstlocal, - MAXVARS, "local variables"); + MAXVARS, "local variables"); luaM_growvector(L, dyd->actvar.arr, dyd->actvar.n + 1, dyd->actvar.size, Vardesc, USHRT_MAX, "local variables"); var = &dyd->actvar.arr[dyd->actvar.n++]; var->pidx = cast(short, reg); var->ro = 0; + var->name = name; setnilvalue(var); return var; } @@ -217,13 +218,42 @@ static Vardesc *getlocalvardesc (FuncState *fs, int i) { } +/* +** Convert 'nvar' (number of active variables at some point) to +** number of variables in the stack at that point. +*/ +static int stacklevel (FuncState *fs, int nvar) { + while (nvar > 0) { + Vardesc *vd = getlocalvardesc(fs, nvar - 1); + if (vdinstack(vd)) /* is in the stack? */ + return vd->sidx + 1; + else + nvar--; /* try previous variable */ + } + return 0; /* no variables */ +} + + +/* +** Return the number of variables in the stack for function 'fs' +*/ +int luaY_nvarstack (FuncState *fs) { + return stacklevel(fs, fs->nactvar); +} + + /* ** Get the debug-information entry for current variable 'i'. */ static LocVar *localdebuginfo (FuncState *fs, int i) { - int idx = getlocalvardesc(fs, i)->pidx; - lua_assert(idx < fs->nlocvars); - return &fs->f->locvars[idx]; + Vardesc *vd = getlocalvardesc(fs, i); + if (!vdinstack(vd)) + return NULL; /* no debug info. for constants */ + else { + int idx = vd->pidx; + lua_assert(idx < fs->ndebugvars); + return &fs->f->locvars[idx]; + } } @@ -242,7 +272,7 @@ static void check_readonly (LexState *ls, expdesc *e) { case VLOCAL: { Vardesc *vardesc = getlocalvardesc(fs, e->u.var.vidx); if (vardesc->ro) - varname = fs->f->locvars[vardesc->pidx].varname; + varname = vardesc->name; break; } case VUPVAL: { @@ -267,11 +297,12 @@ static void check_readonly (LexState *ls, expdesc *e) { */ static void adjustlocalvars (LexState *ls, int nvars) { FuncState *fs = ls->fs; + int stklevel = luaY_nvarstack(fs); int i; for (i = 0; i < nvars; i++) { int varidx = fs->nactvar++; Vardesc *var = getlocalvardesc(fs, varidx); - var->sidx = varidx; + var->sidx = stklevel++; fs->f->locvars[var->pidx].startpc = fs->pc; } } @@ -283,8 +314,11 @@ static void adjustlocalvars (LexState *ls, int nvars) { */ static void removevars (FuncState *fs, int tolevel) { fs->ls->dyd->actvar.n -= (fs->nactvar - tolevel); - while (fs->nactvar > tolevel) - localdebuginfo(fs, --fs->nactvar)->endpc = fs->pc; + while (fs->nactvar > tolevel) { + LocVar *var = localdebuginfo(fs, --fs->nactvar); + if (var) /* does it have debug information? */ + var->endpc = fs->pc; + } } @@ -321,7 +355,7 @@ static int newupvalue (FuncState *fs, TString *name, expdesc *v) { up->instack = 1; up->idx = v->u.var.sidx; up->ro = getlocalvardesc(prev, v->u.var.vidx)->ro; - lua_assert(eqstr(name, localdebuginfo(prev, v->u.var.vidx)->varname)); + lua_assert(eqstr(name, getlocalvardesc(prev, v->u.var.vidx)->name)); } else { up->instack = 0; @@ -342,7 +376,7 @@ static int newupvalue (FuncState *fs, TString *name, expdesc *v) { static int searchvar (FuncState *fs, TString *n) { int i; for (i = cast_int(fs->nactvar) - 1; i >= 0; i--) { - if (eqstr(n, localdebuginfo(fs, i)->varname)) + if (eqstr(n, getlocalvardesc(fs, i)->name)) return i; } return -1; /* not found */ @@ -375,7 +409,7 @@ static void singlevaraux (FuncState *fs, TString *n, expdesc *var, int base) { if (v >= 0) { /* found? */ init_var(fs, var, v); /* variable is local */ if (!base) - markupval(fs, var->u.var.sidx); /* local will be used as an upval */ + markupval(fs, var->u.var.vidx); /* local will be used as an upval */ } else { /* not found as local at current level; try upvalues */ int idx = searchupvalue(fs, n); /* try existing upvalues */ @@ -449,7 +483,7 @@ static void adjust_assign (LexState *ls, int nvars, int nexps, expdesc *e) { ** local variable. */ static l_noret jumpscopeerror (LexState *ls, Labeldesc *gt) { - const char *varname = getstr(localdebuginfo(ls->fs, gt->nactvar)->varname); + const char *varname = getstr(getlocalvardesc(ls->fs, gt->nactvar)->name); const char *msg = " at line %d jumps into the scope of local '%s'"; msg = luaO_pushfstring(ls->L, msg, getstr(gt->name), gt->line, varname); luaK_semerror(ls, msg); /* raise the error */ @@ -552,7 +586,7 @@ static int createlabel (LexState *ls, TString *name, int line, ll->arr[l].nactvar = fs->bl->nactvar; } if (solvegotos(ls, &ll->arr[l])) { /* need close? */ - luaK_codeABC(fs, OP_CLOSE, fs->nactvar, 0, 0); + luaK_codeABC(fs, OP_CLOSE, luaY_nvarstack(fs), 0, 0); return 1; } return 0; @@ -568,10 +602,10 @@ static void movegotosout (FuncState *fs, BlockCnt *bl) { /* correct pending gotos to current block */ for (i = bl->firstgoto; i < gl->n; i++) { /* for each pending goto */ Labeldesc *gt = &gl->arr[i]; - if (gt->nactvar > bl->nactvar) { /* leaving a variable scope? */ - gt->nactvar = bl->nactvar; /* update goto level */ + /* leaving a variable scope? */ + if (stacklevel(fs, gt->nactvar) > stacklevel(fs, bl->nactvar)) gt->close |= bl->upval; /* jump may need a close */ - } + gt->nactvar = bl->nactvar; /* update goto level */ } } @@ -585,7 +619,7 @@ static void enterblock (FuncState *fs, BlockCnt *bl, lu_byte isloop) { bl->insidetbc = (fs->bl != NULL && fs->bl->insidetbc); bl->previous = fs->bl; fs->bl = bl; - lua_assert(fs->freereg == fs->nactvar); + lua_assert(fs->freereg == luaY_nvarstack(fs)); } @@ -610,14 +644,15 @@ static void leaveblock (FuncState *fs) { BlockCnt *bl = fs->bl; LexState *ls = fs->ls; int hasclose = 0; + int stklevel = stacklevel(fs, bl->nactvar); /* level outside the block */ if (bl->isloop) /* fix pending breaks? */ hasclose = createlabel(ls, luaS_newliteral(ls->L, "break"), 0, 0); if (!hasclose && bl->previous && bl->upval) - luaK_codeABC(fs, OP_CLOSE, bl->nactvar, 0, 0); + luaK_codeABC(fs, OP_CLOSE, stklevel, 0, 0); fs->bl = bl->previous; removevars(fs, bl->nactvar); lua_assert(bl->nactvar == fs->nactvar); - fs->freereg = fs->nactvar; /* free registers */ + fs->freereg = stklevel; /* free registers */ ls->dyd->label.n = bl->firstlabel; /* remove local labels */ if (bl->previous) /* inner block? */ movegotosout(fs, bl); /* update pending gotos to outer block */ @@ -675,7 +710,7 @@ static void open_func (LexState *ls, FuncState *fs, BlockCnt *bl) { fs->nabslineinfo = 0; fs->np = 0; fs->nups = 0; - fs->nlocvars = 0; + fs->ndebugvars = 0; fs->nactvar = 0; fs->needclose = 0; fs->firstlocal = ls->dyd->actvar.n; @@ -691,7 +726,7 @@ static void close_func (LexState *ls) { lua_State *L = ls->L; FuncState *fs = ls->fs; Proto *f = fs->f; - luaK_ret(fs, fs->nactvar, 0); /* final return */ + luaK_ret(fs, luaY_nvarstack(fs), 0); /* final return */ leaveblock(fs); lua_assert(fs->bl == NULL); luaK_finish(fs); @@ -701,7 +736,7 @@ static void close_func (LexState *ls) { fs->nabslineinfo, AbsLineInfo); luaM_shrinkvector(L, f->k, f->sizek, fs->nk, TValue); luaM_shrinkvector(L, f->p, f->sizep, fs->np, Proto *); - luaM_shrinkvector(L, f->locvars, f->sizelocvars, fs->nlocvars, LocVar); + luaM_shrinkvector(L, f->locvars, f->sizelocvars, fs->ndebugvars, LocVar); luaM_shrinkvector(L, f->upvalues, f->sizeupvalues, fs->nups, Upvaldesc); ls->fs = fs->prev; luaC_checkGC(L); @@ -1356,8 +1391,9 @@ static void gotostat (LexState *ls) { newgotoentry(ls, name, line, luaK_jump(fs)); else { /* found a label */ /* backward jump; will be resolved here */ - if (fs->nactvar > lb->nactvar) /* leaving the scope of some variable? */ - luaK_codeABC(fs, OP_CLOSE, lb->nactvar, 0, 0); + int lblevel = stacklevel(fs, lb->nactvar); /* label level */ + if (luaY_nvarstack(fs) > lblevel) /* leaving the scope of a variable? */ + luaK_codeABC(fs, OP_CLOSE, lblevel, 0, 0); /* create jump and link it to the label */ luaK_patchlist(fs, luaK_jump(fs), lb->pc); } @@ -1432,7 +1468,7 @@ static void repeatstat (LexState *ls, int line) { if (bl2.upval) { /* upvalues? */ int exit = luaK_jump(fs); /* normal exit must jump over fix */ luaK_patchtohere(fs, condexit); /* repetition must close upvalues */ - luaK_codeABC(fs, OP_CLOSE, bl2.nactvar, 0, 0); + luaK_codeABC(fs, OP_CLOSE, stacklevel(fs, bl2.nactvar), 0, 0); condexit = luaK_jump(fs); /* repeat after closing upvalues */ luaK_patchtohere(fs, exit); /* normal exit comes to here */ } @@ -1532,7 +1568,6 @@ static void forlist (LexState *ls, TString *indexname) { /* create control variables */ new_localvarliteral(ls, "(for generator)"); new_localvarliteral(ls, "(for state)"); - markupval(fs, fs->nactvar); /* state may create an upvalue */ new_localvarliteral(ls, "(for control)"); new_localvarliteral(ls, "(for toclose)"); /* create declared variables */ @@ -1545,6 +1580,7 @@ static void forlist (LexState *ls, TString *indexname) { line = ls->linenumber; adjust_assign(ls, 4, explist(ls, &e), &e); adjustlocalvars(ls, 4); /* control variables */ + markupval(fs, luaY_nvarstack(fs)); /* state may create an upvalue */ luaK_checkstack(fs, 3); /* extra space to call generator */ forbody(ls, base, line, nvars - 4, 1); } @@ -1587,7 +1623,8 @@ static int issinglejump (LexState *ls, TString **label, int *target) { TString *lname = ls->lookahead.seminfo.ts; /* label's id */ Labeldesc *lb = findlabel(ls, lname); if (lb) { /* a backward jump? */ - if (ls->fs->nactvar > lb->nactvar) /* needs to close variables? */ + /* does it need to close variables? */ + if (luaY_nvarstack(ls->fs) > stacklevel(ls->fs, lb->nactvar)) return 0; /* not a single jump; cannot optimize */ *target = lb->pc; } @@ -1659,11 +1696,12 @@ static void ifstat (LexState *ls, int line) { static void localfunc (LexState *ls) { expdesc b; FuncState *fs = ls->fs; + int fvar = fs->nactvar; /* function's variable index */ new_localvar(ls, str_checkname(ls)); /* new local variable */ adjustlocalvars(ls, 1); /* enter its scope */ body(ls, &b, 0, ls->linenumber); /* function created in next register */ /* debug information will only see the variable after this point! */ - localdebuginfo(fs, b.u.info)->startpc = fs->pc; + localdebuginfo(fs, fvar)->startpc = fs->pc; } @@ -1687,9 +1725,10 @@ static int getlocalattribute (LexState *ls) { static void checktoclose (LexState *ls, int toclose) { if (toclose != -1) { /* is there a to-be-closed variable? */ FuncState *fs = ls->fs; - markupval(fs, fs->nactvar + toclose + 1); + int level = luaY_nvarstack(fs) + toclose; + markupval(fs, level + 1); fs->bl->insidetbc = 1; /* in the scope of a to-be-closed variable */ - luaK_codeABC(fs, OP_TBC, fs->nactvar + toclose, 0, 0); + luaK_codeABC(fs, OP_TBC, level, 0, 0); } } @@ -1773,7 +1812,7 @@ static void retstat (LexState *ls) { FuncState *fs = ls->fs; expdesc e; int nret; /* number of values being returned */ - int first = fs->nactvar; /* first slot to be returned */ + int first = luaY_nvarstack(fs); /* first slot to be returned */ if (block_follow(ls, 1) || ls->t.token == ';') nret = 0; /* return no values */ else { @@ -1782,7 +1821,7 @@ static void retstat (LexState *ls) { luaK_setmultret(fs, &e); if (e.k == VCALL && nret == 1 && !fs->bl->insidetbc) { /* tail call? */ SET_OPCODE(getinstruction(fs,&e), OP_TAILCALL); - lua_assert(GETARG_A(getinstruction(fs,&e)) == fs->nactvar); + lua_assert(GETARG_A(getinstruction(fs,&e)) == luaY_nvarstack(fs)); } nret = LUA_MULTRET; /* return all values */ } @@ -1867,8 +1906,8 @@ static void statement (LexState *ls) { } } lua_assert(ls->fs->f->maxstacksize >= ls->fs->freereg && - ls->fs->freereg >= ls->fs->nactvar); - ls->fs->freereg = ls->fs->nactvar; /* free registers */ + ls->fs->freereg >= luaY_nvarstack(ls->fs)); + ls->fs->freereg = luaY_nvarstack(ls->fs); /* free registers */ leavelevel(ls); } diff --git a/lparser.h b/lparser.h index cc2ec14d4c..7d43a813ec 100644 --- a/lparser.h +++ b/lparser.h @@ -83,19 +83,24 @@ typedef struct expdesc { /* description of an active local variable */ typedef struct Vardesc { - TValuefields; /* constant value (if variable is 'const') */ + TValuefields; /* constant value (if it is a compile-time constant) */ lu_byte ro; /* true if variable is 'const' */ lu_byte sidx; /* index of the variable in the stack */ short pidx; /* index of the variable in the Proto's 'locvars' array */ + TString *name; /* variable name */ } Vardesc; +/* check whether Vardesc is in the stack (not a compile-time constant) */ +#define vdinstack(vd) (ttisnil(vd)) + + /* description of pending goto statements and label statements */ typedef struct Labeldesc { TString *name; /* label identifier */ int pc; /* position in code */ int line; /* line where it appeared */ - lu_byte nactvar; /* local level where it appears in current block */ + lu_byte nactvar; /* number of active variables in that position */ lu_byte close; /* goto that escapes upvalues */ } Labeldesc; @@ -138,7 +143,7 @@ typedef struct FuncState { int nabslineinfo; /* number of elements in 'abslineinfo' */ int firstlocal; /* index of first local var (in Dyndata array) */ int firstlabel; /* index of first label (in 'dyd->label->arr') */ - short nlocvars; /* number of elements in 'f->locvars' */ + short ndebugvars; /* number of elements in 'f->locvars' */ lu_byte nactvar; /* number of active local variables */ lu_byte nups; /* number of upvalues */ lu_byte freereg; /* first free register */ @@ -147,6 +152,7 @@ typedef struct FuncState { } FuncState; +LUAI_FUNC int luaY_nvarstack (FuncState *fs); LUAI_FUNC LClosure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, Dyndata *dyd, const char *name, int firstchar); From be8445d7e4b6122620c428877b51a27d464253d5 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 10 Jul 2019 14:58:31 -0300 Subject: [PATCH 0448/1145] Details In the generic for loop, it is simpler for OP_TFORLOOP to use the same 'ra' as OP_TFORCALL. Moreover, the internal names of the loop temporaries "(for ...)" don't need to leak internal details (even because the numerical for loop doesn't have a fixed role for each of its temporaries). --- lparser.c | 13 ++++++------- lvm.c | 5 ++--- testes/files.lua | 8 +++++--- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/lparser.c b/lparser.c index c4626ba165..7f282bf911 100644 --- a/lparser.c +++ b/lparser.c @@ -1527,7 +1527,6 @@ static void forbody (LexState *ls, int base, int line, int nvars, int isgen) { if (isgen) { /* generic for? */ luaK_codeABC(fs, OP_TFORCALL, base, 0, nvars); luaK_fixline(fs, line); - base += 2; /* base for 'OP_TFORLOOP' (skips function and state) */ } endfor = luaK_codeABx(fs, forloop[isgen], base, 0); fixforjump(fs, endfor, prep + 1, 1); @@ -1539,9 +1538,9 @@ static void fornum (LexState *ls, TString *varname, int line) { /* fornum -> NAME = exp,exp[,exp] forbody */ FuncState *fs = ls->fs; int base = fs->freereg; - new_localvarliteral(ls, "(for index)"); - new_localvarliteral(ls, "(for limit)"); - new_localvarliteral(ls, "(for step)"); + new_localvarliteral(ls, "(for state)"); + new_localvarliteral(ls, "(for state)"); + new_localvarliteral(ls, "(for state)"); new_localvar(ls, varname); checknext(ls, '='); exp1(ls); /* initial value */ @@ -1566,10 +1565,10 @@ static void forlist (LexState *ls, TString *indexname) { int line; int base = fs->freereg; /* create control variables */ - new_localvarliteral(ls, "(for generator)"); new_localvarliteral(ls, "(for state)"); - new_localvarliteral(ls, "(for control)"); - new_localvarliteral(ls, "(for toclose)"); + new_localvarliteral(ls, "(for state)"); + new_localvarliteral(ls, "(for state)"); + new_localvarliteral(ls, "(for state)"); /* create declared variables */ new_localvar(ls, indexname); while (testnext(ls, ',')) { diff --git a/lvm.c b/lvm.c index b05a887d0c..a52f186fae 100644 --- a/lvm.c +++ b/lvm.c @@ -1746,14 +1746,13 @@ void luaV_execute (lua_State *L, CallInfo *ci) { Protect(luaD_call(L, ra + 4, GETARG_C(i))); /* do the call */ updatestack(ci); /* stack may have changed */ i = *(pc++); /* go to next instruction */ - ra += 2; /* adjust for next instruction */ lua_assert(GET_OPCODE(i) == OP_TFORLOOP && ra == RA(i)); goto l_tforloop; } vmcase(OP_TFORLOOP) { l_tforloop: - if (!ttisnil(s2v(ra + 2))) { /* continue loop? */ - setobjs2s(L, ra, ra + 2); /* save control variable */ + if (!ttisnil(s2v(ra + 4))) { /* continue loop? */ + setobjs2s(L, ra + 2, ra + 4); /* save control variable */ pc -= GETARG_Bx(i); /* jump back */ } vmbreak; diff --git a/testes/files.lua b/testes/files.lua index 54931c14c1..c8f23d18da 100644 --- a/testes/files.lua +++ b/testes/files.lua @@ -427,10 +427,12 @@ do -- testing closing file in line iteration -- get the to-be-closed variable from a loop local function gettoclose (lv) lv = lv + 1 - for i = 1, math.maxinteger do + local stvar = 0 -- to-be-closed is 4th state variable in the loop + for i = 1, 1000 do local n, v = debug.getlocal(lv, i) - if n == "(for toclose)" then - return v + if n == "(for state)" then + stvar = stvar + 1 + if stvar == 4 then return v end end end end From f6aab3ec1f111cd8d968bdcb7ca800e93b819d24 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 12 Jul 2019 11:38:42 -0300 Subject: [PATCH 0449/1145] First implementation of constant propagation Local constant variables initialized with compile-time constants are optimized away from the code. --- lcode.c | 56 ++++++++++++++++++++- lcode.h | 1 + ldump.c | 2 +- lobject.h | 2 +- lparser.c | 113 ++++++++++++++++++++++++------------------ lparser.h | 27 ++++++---- lundump.c | 5 +- manual/manual.of | 31 +++++++----- testes/code.lua | 89 +++++++++++++++++++++------------ testes/constructs.lua | 24 +++++++-- testes/locals.lua | 2 +- testes/math.lua | 16 +++--- 12 files changed, 249 insertions(+), 119 deletions(-) diff --git a/lcode.c b/lcode.c index 837253f4ea..74ff47de52 100644 --- a/lcode.c +++ b/lcode.c @@ -67,6 +67,30 @@ static int tonumeral (const expdesc *e, TValue *v) { } +/* +** If expression is a constant, fills 'v' with its value +** and returns 1. Otherwise, returns 0. +*/ +int luaK_exp2const (FuncState *fs, const expdesc *e, TValue *v) { + if (hasjumps(e)) + return 0; /* not a constant */ + switch (e->k) { + case VFALSE: case VTRUE: + setbvalue(v, e->k == VTRUE); + return 1; + case VNIL: + setnilvalue(v); + return 1; + case VK: { + TValue *k = &fs->f->k[e->u.info]; + setobj(fs->ls->L, v, k); + return 1; + } + default: return tonumeral(e, v); + } +} + + /* ** Return the previous instruction of the current code. If there ** may be a jump target between the current instruction and the @@ -629,6 +653,31 @@ static void luaK_float (FuncState *fs, int reg, lua_Number f) { } +/* +** Convert a constant in 'v' into an expression description 'e' +*/ +static void const2exp (FuncState *fs, TValue *v, expdesc *e) { + switch (ttypetag(v)) { + case LUA_TNUMINT: + e->k = VKINT; e->u.ival = ivalue(v); + break; + case LUA_TNUMFLT: + e->k = VKFLT; e->u.nval = fltvalue(v); + break; + case LUA_TBOOLEAN: + e->k = bvalue(v) ? VTRUE : VFALSE; + break; + case LUA_TNIL: + e->k = VNIL; + break; + case LUA_TSHRSTR: case LUA_TLNGSTR: + e->k = VK; e->u.info = luaK_stringK(fs, tsvalue(v)); + break; + default: lua_assert(0); + } +} + + /* ** Fix an expression to return the number of results 'nresults'. ** Either 'e' is a multi-ret expression (function call or vararg) @@ -677,6 +726,11 @@ void luaK_setoneret (FuncState *fs, expdesc *e) { */ void luaK_dischargevars (FuncState *fs, expdesc *e) { switch (e->k) { + case VCONST: { + TValue *val = &fs->ls->dyd->actvar.arr[e->u.info].k; + const2exp(fs, val, e); + break; + } case VLOCAL: { /* already in a register */ e->u.info = e->u.var.sidx; e->k = VNONRELOC; /* becomes a non-relocatable value */ @@ -1074,7 +1128,6 @@ void luaK_goiffalse (FuncState *fs, expdesc *e) { ** Code 'not e', doing constant folding. */ static void codenot (FuncState *fs, expdesc *e) { - luaK_dischargevars(fs, e); switch (e->k) { case VNIL: case VFALSE: { e->k = VTRUE; /* true == not nil == not false */ @@ -1447,6 +1500,7 @@ static void codeeq (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) { */ void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e, int line) { static const expdesc ef = {VKINT, {0}, NO_JUMP, NO_JUMP}; + luaK_dischargevars(fs, e); switch (op) { case OPR_MINUS: case OPR_BNOT: /* use 'ef' as fake 2nd operand */ if (constfolding(fs, op + LUA_OPUNM, e, &ef)) diff --git a/lcode.h b/lcode.h index 0758f88dee..a15b687502 100644 --- a/lcode.h +++ b/lcode.h @@ -56,6 +56,7 @@ LUAI_FUNC int luaK_codeAsBx (FuncState *fs, OpCode o, int A, int Bx); LUAI_FUNC int luaK_codeABCk (FuncState *fs, OpCode o, int A, int B, int C, int k); LUAI_FUNC int luaK_isKint (expdesc *e); +LUAI_FUNC int luaK_exp2const (FuncState *fs, const expdesc *e, TValue *v); LUAI_FUNC void luaK_fixline (FuncState *fs, int line); LUAI_FUNC void luaK_nil (FuncState *fs, int from, int n); LUAI_FUNC void luaK_reserveregs (FuncState *fs, int n); diff --git a/ldump.c b/ldump.c index 3d5b7b325f..9b501729e2 100644 --- a/ldump.c +++ b/ldump.c @@ -149,7 +149,7 @@ static void DumpUpvalues (const Proto *f, DumpState *D) { for (i = 0; i < n; i++) { DumpByte(f->upvalues[i].instack, D); DumpByte(f->upvalues[i].idx, D); - DumpByte(f->upvalues[i].ro, D); + DumpByte(f->upvalues[i].kind, D); } } diff --git a/lobject.h b/lobject.h index 64366a94d2..2f95bcb572 100644 --- a/lobject.h +++ b/lobject.h @@ -460,7 +460,7 @@ typedef struct Upvaldesc { TString *name; /* upvalue name (for debug information) */ lu_byte instack; /* whether it is in stack (register) */ lu_byte idx; /* index of upvalue (in stack or in outer function's list) */ - lu_byte ro; /* true if upvalue is read-only (const) */ + lu_byte kind; /* kind of corresponding variable */ } Upvaldesc; diff --git a/lparser.c b/lparser.c index 7f282bf911..79df0217d9 100644 --- a/lparser.c +++ b/lparser.c @@ -170,15 +170,16 @@ static void codename (LexState *ls, expdesc *e) { ** Register a new local variable in the active 'Proto' (for debug ** information). */ -static int registerlocalvar (lua_State *L, FuncState *fs, TString *varname) { +static int registerlocalvar (LexState *ls, FuncState *fs, TString *varname) { Proto *f = fs->f; int oldsize = f->sizelocvars; - luaM_growvector(L, f->locvars, fs->ndebugvars, f->sizelocvars, + luaM_growvector(ls->L, f->locvars, fs->ndebugvars, f->sizelocvars, LocVar, SHRT_MAX, "local variables"); while (oldsize < f->sizelocvars) f->locvars[oldsize++].varname = NULL; f->locvars[fs->ndebugvars].varname = varname; - luaC_objbarrier(L, f, varname); + f->locvars[fs->ndebugvars].startpc = fs->pc; + luaC_objbarrier(ls->L, f, varname); return fs->ndebugvars++; } @@ -191,16 +192,13 @@ static Vardesc *new_localvar (LexState *ls, TString *name) { FuncState *fs = ls->fs; Dyndata *dyd = ls->dyd; Vardesc *var; - int reg = registerlocalvar(L, fs, name); checklimit(fs, dyd->actvar.n + 1 - fs->firstlocal, MAXVARS, "local variables"); luaM_growvector(L, dyd->actvar.arr, dyd->actvar.n + 1, dyd->actvar.size, Vardesc, USHRT_MAX, "local variables"); var = &dyd->actvar.arr[dyd->actvar.n++]; - var->pidx = cast(short, reg); - var->ro = 0; - var->name = name; - setnilvalue(var); + var->vd.kind = VDKREG; /* default is a regular variable */ + var->vd.name = name; return var; } @@ -225,8 +223,8 @@ static Vardesc *getlocalvardesc (FuncState *fs, int i) { static int stacklevel (FuncState *fs, int nvar) { while (nvar > 0) { Vardesc *vd = getlocalvardesc(fs, nvar - 1); - if (vdinstack(vd)) /* is in the stack? */ - return vd->sidx + 1; + if (vd->vd.kind != RDKCTC) /* is in the stack? */ + return vd->vd.sidx + 1; else nvar--; /* try previous variable */ } @@ -247,10 +245,10 @@ int luaY_nvarstack (FuncState *fs) { */ static LocVar *localdebuginfo (FuncState *fs, int i) { Vardesc *vd = getlocalvardesc(fs, i); - if (!vdinstack(vd)) + if (vd->vd.kind == RDKCTC) return NULL; /* no debug info. for constants */ else { - int idx = vd->pidx; + int idx = vd->vd.pidx; lua_assert(idx < fs->ndebugvars); return &fs->f->locvars[idx]; } @@ -261,7 +259,7 @@ static void init_var (FuncState *fs, expdesc *e, int i) { e->f = e->t = NO_JUMP; e->k = VLOCAL; e->u.var.vidx = i; - e->u.var.sidx = getlocalvardesc(fs, i)->sidx; + e->u.var.sidx = getlocalvardesc(fs, i)->vd.sidx; } @@ -269,15 +267,19 @@ static void check_readonly (LexState *ls, expdesc *e) { FuncState *fs = ls->fs; TString *varname = NULL; /* to be set if variable is const */ switch (e->k) { + case VCONST: { + varname = ls->dyd->actvar.arr[e->u.info].vd.name; + break; + } case VLOCAL: { Vardesc *vardesc = getlocalvardesc(fs, e->u.var.vidx); - if (vardesc->ro) - varname = vardesc->name; + if (vardesc->vd.kind != VDKREG) /* not a regular variable? */ + varname = vardesc->vd.name; break; } case VUPVAL: { Upvaldesc *up = &fs->f->upvalues[e->u.info]; - if (up->ro) + if (up->kind != VDKREG) varname = up->name; break; } @@ -302,8 +304,8 @@ static void adjustlocalvars (LexState *ls, int nvars) { for (i = 0; i < nvars; i++) { int varidx = fs->nactvar++; Vardesc *var = getlocalvardesc(fs, varidx); - var->sidx = stklevel++; - fs->f->locvars[var->pidx].startpc = fs->pc; + var->vd.sidx = stklevel++; + var->vd.pidx = registerlocalvar(ls, fs, var->vd.name); } } @@ -354,13 +356,13 @@ static int newupvalue (FuncState *fs, TString *name, expdesc *v) { if (v->k == VLOCAL) { up->instack = 1; up->idx = v->u.var.sidx; - up->ro = getlocalvardesc(prev, v->u.var.vidx)->ro; - lua_assert(eqstr(name, getlocalvardesc(prev, v->u.var.vidx)->name)); + up->kind = getlocalvardesc(prev, v->u.var.vidx)->vd.kind; + lua_assert(eqstr(name, getlocalvardesc(prev, v->u.var.vidx)->vd.name)); } else { up->instack = 0; up->idx = cast_byte(v->u.info); - up->ro = prev->f->upvalues[v->u.info].ro; + up->kind = prev->f->upvalues[v->u.info].kind; lua_assert(eqstr(name, prev->f->upvalues[v->u.info].name)); } up->name = name; @@ -373,11 +375,17 @@ static int newupvalue (FuncState *fs, TString *name, expdesc *v) { ** Look for an active local variable with the name 'n' in the ** function 'fs'. */ -static int searchvar (FuncState *fs, TString *n) { +static int searchvar (FuncState *fs, TString *n, expdesc *var) { int i; for (i = cast_int(fs->nactvar) - 1; i >= 0; i--) { - if (eqstr(n, getlocalvardesc(fs, i)->name)) - return i; + Vardesc *vd = getlocalvardesc(fs, i); + if (eqstr(n, vd->vd.name)) { /* found? */ + if (vd->vd.kind == RDKCTC) /* compile-time constant? */ + init_exp(var, VCONST, fs->firstlocal + i); + else /* real variable */ + init_var(fs, var, i); + return var->k; + } } return -1; /* not found */ } @@ -405,20 +413,19 @@ static void singlevaraux (FuncState *fs, TString *n, expdesc *var, int base) { if (fs == NULL) /* no more levels? */ init_exp(var, VVOID, 0); /* default is global */ else { - int v = searchvar(fs, n); /* look up locals at current level */ + int v = searchvar(fs, n, var); /* look up locals at current level */ if (v >= 0) { /* found? */ - init_var(fs, var, v); /* variable is local */ - if (!base) + if (v == VLOCAL && !base) markupval(fs, var->u.var.vidx); /* local will be used as an upval */ } else { /* not found as local at current level; try upvalues */ int idx = searchupvalue(fs, n); /* try existing upvalues */ if (idx < 0) { /* not found? */ singlevaraux(fs->prev, n, var, 0); /* try upper levels */ - if (var->k == VVOID) /* not found? */ - return; /* it is a global */ - /* else was LOCAL or UPVAL */ - idx = newupvalue(fs, n, var); /* will be a new upvalue */ + if (var->k == VLOCAL || var->k == VUPVAL) /* local or upvalue? */ + idx = newupvalue(fs, n, var); /* will be a new upvalue */ + else /* it is a global or a constant */ + return; /* don't need to do anything at this level */ } init_exp(var, VUPVAL, idx); /* new or old upvalue */ } @@ -483,7 +490,7 @@ static void adjust_assign (LexState *ls, int nvars, int nexps, expdesc *e) { ** local variable. */ static l_noret jumpscopeerror (LexState *ls, Labeldesc *gt) { - const char *varname = getstr(getlocalvardesc(ls->fs, gt->nactvar)->name); + const char *varname = getstr(getlocalvardesc(ls->fs, gt->nactvar)->vd.name); const char *msg = " at line %d jumps into the scope of local '%s'"; msg = luaO_pushfstring(ls->L, msg, getstr(gt->name), gt->line, varname); luaK_semerror(ls, msg); /* raise the error */ @@ -1710,21 +1717,20 @@ static int getlocalattribute (LexState *ls) { const char *attr = getstr(str_checkname(ls)); checknext(ls, '>'); if (strcmp(attr, "const") == 0) - return 1; /* read-only variable */ + return RDKCONST; /* read-only variable */ else if (strcmp(attr, "toclose") == 0) - return 2; /* to-be-closed variable */ + return RDKTOCLOSE; /* to-be-closed variable */ else luaK_semerror(ls, luaO_pushfstring(ls->L, "unknown attribute '%s'", attr)); } - return 0; + return VDKREG; } -static void checktoclose (LexState *ls, int toclose) { - if (toclose != -1) { /* is there a to-be-closed variable? */ +static void checktoclose (LexState *ls, int level) { + if (level != -1) { /* is there a to-be-closed variable? */ FuncState *fs = ls->fs; - int level = luaY_nvarstack(fs) + toclose; markupval(fs, level + 1); fs->bl->insidetbc = 1; /* in the scope of a to-be-closed variable */ luaK_codeABC(fs, OP_TBC, level, 0, 0); @@ -1734,20 +1740,20 @@ static void checktoclose (LexState *ls, int toclose) { static void localstat (LexState *ls) { /* stat -> LOCAL ATTRIB NAME {',' ATTRIB NAME} ['=' explist] */ + FuncState *fs = ls->fs; int toclose = -1; /* index of to-be-closed variable (if any) */ + Vardesc *var; /* last variable */ int nvars = 0; int nexps; expdesc e; do { int kind = getlocalattribute(ls); - Vardesc *var = new_localvar(ls, str_checkname(ls)); - if (kind != 0) { /* is there an attribute? */ - var->ro = 1; /* all attributes make variable read-only */ - if (kind == 2) { /* to-be-closed? */ - if (toclose != -1) /* one already present? */ - luaK_semerror(ls, "multiple to-be-closed variables in local list"); - toclose = nvars; - } + var = new_localvar(ls, str_checkname(ls)); + var->vd.kind = kind; + if (kind == RDKTOCLOSE) { /* to-be-closed? */ + if (toclose != -1) /* one already present? */ + luaK_semerror(ls, "multiple to-be-closed variables in local list"); + toclose = luaY_nvarstack(fs) + nvars; } nvars++; } while (testnext(ls, ',')); @@ -1757,9 +1763,18 @@ static void localstat (LexState *ls) { e.k = VVOID; nexps = 0; } - adjust_assign(ls, nvars, nexps, &e); + if (nvars == nexps && /* no adjustments? */ + var->vd.kind == RDKCONST && /* last variable is const? */ + luaK_exp2const(fs, &e, &var->k)) { /* compile-time constant? */ + var->vd.kind = RDKCTC; /* variable is a compile-time constant */ + adjustlocalvars(ls, nvars - 1); /* exclude last variable */ + fs->nactvar++; /* but count it */ + } + else { + adjust_assign(ls, nvars, nexps, &e); + adjustlocalvars(ls, nvars); + } checktoclose(ls, toclose); - adjustlocalvars(ls, nvars); } @@ -1925,7 +1940,7 @@ static void mainfunc (LexState *ls, FuncState *fs) { env = allocupvalue(fs); /* ...set environment upvalue */ env->instack = 1; env->idx = 0; - env->ro = 0; + env->kind = VDKREG; env->name = ls->envn; luaX_next(ls); /* read first token */ statlist(ls); /* parse main body */ diff --git a/lparser.h b/lparser.h index 7d43a813ec..d9b734bf8e 100644 --- a/lparser.h +++ b/lparser.h @@ -34,8 +34,9 @@ typedef enum { VNONRELOC, /* expression has its value in a fixed register; info = result register */ VLOCAL, /* local variable; var.ridx = local register; - var.vidx = index in 'actvar.arr' */ + var.vidx = relative index in 'actvar.arr' */ VUPVAL, /* upvalue variable; info = index of upvalue in 'upvalues' */ + VCONST, /* compile-time constant; info = absolute index in 'actvar.arr' */ VINDEXED, /* indexed variable; ind.t = table register; ind.idx = key's R index */ @@ -81,19 +82,25 @@ typedef struct expdesc { } expdesc; +/* kinds of variables */ +#define VDKREG 0 /* regular */ +#define RDKCONST 1 /* constant */ +#define RDKTOCLOSE 2 /* to-be-closed */ +#define RDKCTC 3 /* compile-time constant */ + /* description of an active local variable */ -typedef struct Vardesc { - TValuefields; /* constant value (if it is a compile-time constant) */ - lu_byte ro; /* true if variable is 'const' */ - lu_byte sidx; /* index of the variable in the stack */ - short pidx; /* index of the variable in the Proto's 'locvars' array */ - TString *name; /* variable name */ +typedef union Vardesc { + struct { + TValuefields; /* constant value (if it is a compile-time constant) */ + lu_byte kind; + lu_byte sidx; /* index of the variable in the stack */ + short pidx; /* index of the variable in the Proto's 'locvars' array */ + TString *name; /* variable name */ + } vd; + TValue k; /* constant value (if any) */ } Vardesc; -/* check whether Vardesc is in the stack (not a compile-time constant) */ -#define vdinstack(vd) (ttisnil(vd)) - /* description of pending goto statements and label statements */ typedef struct Labeldesc { diff --git a/lundump.c b/lundump.c index 5c0e94d67b..8f2a490c86 100644 --- a/lundump.c +++ b/lundump.c @@ -198,12 +198,11 @@ static void LoadUpvalues (LoadState *S, Proto *f) { n = LoadInt(S); f->upvalues = luaM_newvectorchecked(S->L, n, Upvaldesc); f->sizeupvalues = n; - for (i = 0; i < n; i++) - f->upvalues[i].name = NULL; for (i = 0; i < n; i++) { + f->upvalues[i].name = NULL; f->upvalues[i].instack = LoadByte(S); f->upvalues[i].idx = LoadByte(S); - f->upvalues[i].ro = LoadByte(S); + f->upvalues[i].kind = LoadByte(S); } } diff --git a/manual/manual.of b/manual/manual.of index 136e902252..61fcdaa3c8 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -223,7 +223,7 @@ In Lua, the global variable @Lid{_G} is initialized with this same value. so changing its value will affect only your own code.) When Lua loads a chunk, -the default value for its @id{_ENV} upvalue +the default value for its @id{_ENV} variable is the global environment @seeF{load}. Therefore, by default, free names in Lua code refer to entries in the global environment @@ -233,7 +233,7 @@ and some functions there operate on that environment. You can use @Lid{load} (or @Lid{loadfile}) to load a chunk with a different environment. (In C, you have to load the chunk and then change the value -of its first upvalue.) +of its first upvalue; see @See{lua_setupvalue}.) } @@ -1224,7 +1224,7 @@ As such, chunks can define local variables, receive arguments, and return values. Moreover, such anonymous function is compiled as in the scope of an external local variable called @id{_ENV} @see{globalenv}. -The resulting function always has @id{_ENV} as its only upvalue, +The resulting function always has @id{_ENV} as its only external variable, even if it does not use that variable. A chunk can be stored in a file or in a string inside the host program. @@ -2241,8 +2241,8 @@ and so the second @id{x} refers to the outside variable. Because of the @x{lexical scoping} rules, local variables can be freely accessed by functions defined inside their scope. -A local variable used by an inner function is called -an @def{upvalue}, or @emphx{external local variable}, +A local variable used by an inner function is called an @def{upvalue} +(or @emphx{external local variable}, or simply @emphx{external variable}) inside the inner function. Notice that each execution of a @Rw{local} statement @@ -4765,11 +4765,7 @@ and returns its name. Returns @id{NULL} (and pushes nothing) when the index @id{n} is greater than the number of upvalues. -For @N{C functions}, this function uses the empty string @T{""} -as a name for all upvalues. -(For Lua functions, -upvalues are the external local variables that the function uses, -and that are consequently included in its closure.) +See @Lid{debug.getupvalue} for more information about upvalues. } @@ -8485,6 +8481,8 @@ The first parameter or local variable has @N{index 1}, and so on, following the order that they are declared in the code, counting only the variables that are active in the current scope of the function. +Compile-time constants may not appear in this listing, +if they were optimized away by the compiler. Negative indices refer to vararg arguments; @num{-1} is the first vararg argument. The function returns @nil if there is no variable with the given index, @@ -8520,8 +8518,15 @@ This function returns the name and the value of the upvalue with index @id{up} of the function @id{f}. The function returns @nil if there is no upvalue with the given index. -Variable names starting with @Char{(} (open parenthesis) @C{)} -represent variables with no known names +(For Lua functions, +upvalues are the external local variables that the function uses, +and that are consequently included in its closure.) + +For @N{C functions}, this function uses the empty string @T{""} +as a name for all upvalues. + +Variable name @Char{?} (interrogation mark) +represents variables with no known names (variables from chunks saved without debug information). } @@ -8626,6 +8631,8 @@ The function returns @nil if there is no upvalue with the given index. Otherwise, it returns the name of the upvalue. +See @Lid{debug.getupvalue} for more information about upvalues. + } @LibEntry{debug.setuservalue (udata, value, n)| diff --git a/testes/code.lua b/testes/code.lua index 128ca2cb8b..49d682f8eb 100644 --- a/testes/code.lua +++ b/testes/code.lua @@ -7,6 +7,22 @@ if T==nil then end print "testing code generation and optimizations" +-- to test constant propagation +local k0 = 0 +local k1 = 1 +local k3 = 3 +local k6 = k3 + (k3 << k0) +local kFF0 = 0xFF0 +local k3_78 = 3.78 +local x, k3_78_4 = 10, k3_78 / 4 +assert(x == 10) + +local kx = "x" + +local kTrue = true +local kFalse = false + +local kNil = nil -- this code gave an error for the code checker do @@ -27,12 +43,12 @@ end local function foo () local a - a = 3; + a = k3; a = 0; a = 0.0; a = -7 + 7 - a = 3.78/4; a = 3.78/4 - a = -3.78/4; a = 3.78/4; a = -3.78/4 + a = k3_78/4; a = k3_78_4 + a = -k3_78/4; a = k3_78/4; a = -3.78/4 a = -3.79/4; a = 0.0; a = -0; - a = 3; a = 3.0; a = 3; a = 3.0 + a = k3; a = 3.0; a = 3; a = 3.0 end checkKlist(foo, {3.78/4, -3.78/4, -3.79/4}) @@ -86,10 +102,11 @@ end, 'CLOSURE', 'NEWTABLE', 'GETTABUP', 'CALL', 'SETLIST', 'CALL', 'RETURN') -- sequence of LOADNILs check(function () + local kNil = nil local a,b,c local d; local e; local f,g,h; - d = nil; d=nil; b=nil; a=nil; c=nil; + d = nil; d=nil; b=nil; a=kNil; c=nil; end, 'LOADNIL', 'RETURN0') check(function () @@ -109,7 +126,7 @@ check (function (a,b,c) return a end, 'RETURN1') -- infinite loops -check(function () while true do local a = -1 end end, +check(function () while kTrue do local a = -1 end end, 'LOADI', 'JMP', 'RETURN0') check(function () while 1 do local a = -1 end end, @@ -125,9 +142,9 @@ check(function (a,b,c,d) return a..b..c..d end, -- not check(function () return not not nil end, 'LOADBOOL', 'RETURN1') -check(function () return not not false end, 'LOADBOOL', 'RETURN1') +check(function () return not not kFalse end, 'LOADBOOL', 'RETURN1') check(function () return not not true end, 'LOADBOOL', 'RETURN1') -check(function () return not not 1 end, 'LOADBOOL', 'RETURN1') +check(function () return not not k3 end, 'LOADBOOL', 'RETURN1') -- direct access to locals check(function () @@ -144,7 +161,8 @@ end, -- direct access to constants check(function () local a,b - a.x = 3.2 + local c = kNil + a[kx] = 3.2 a.x = b a[b] = 'x' end, @@ -152,8 +170,9 @@ end, -- "get/set table" with numeric indices check(function (a) + local k255 = 255 a[1] = a[100] - a[255] = a[256] + a[k255] = a[256] a[256] = 5 end, 'GETI', 'SETI', @@ -170,7 +189,7 @@ end, check(function () local a,b - a[true] = false + a[kTrue] = false end, 'LOADNIL', 'LOADBOOL', 'SETTABLE', 'RETURN0') @@ -238,37 +257,39 @@ local function checkF (func, val) end checkF(function () return 0.0 end, 0.0) -checkI(function () return 0 end, 0) -checkI(function () return -0//1 end, 0) +checkI(function () return k0 end, 0) +checkI(function () return -k0//1 end, 0) checkK(function () return 3^-1 end, 1/3) checkK(function () return (1 + 1)^(50 + 50) end, 2^100) checkK(function () return (-2)^(31 - 2) end, -0x20000000 + 0.0) -checkF(function () return (-3^0 + 5) // 3.0 end, 1.0) -checkI(function () return -3 % 5 end, 2) +checkF(function () return (-k3^0 + 5) // 3.0 end, 1.0) +checkI(function () return -k3 % 5 end, 2) checkF(function () return -((2.0^8 + -(-1)) % 8)/2 * 4 - 3 end, -5.0) checkF(function () return -((2^8 + -(-1)) % 8)//2 * 4 - 3 end, -7.0) checkI(function () return 0xF0.0 | 0xCC.0 ~ 0xAA & 0xFD end, 0xF4) -checkI(function () return ~(~0xFF0 | 0xFF0) end, 0) +checkI(function () return ~(~kFF0 | kFF0) end, 0) checkI(function () return ~~-1024.0 end, -1024) -checkI(function () return ((100 << 6) << -4) >> 2 end, 100) +checkI(function () return ((100 << k6) << -4) >> 2 end, 100) -- borders around MAXARG_sBx ((((1 << 17) - 1) >> 1) == 65535) local a = 17; local sbx = ((1 << a) - 1) >> 1 -- avoid folding -checkI(function () return 65535 end, sbx) -checkI(function () return -65535 end, -sbx) -checkI(function () return 65536 end, sbx + 1) -checkK(function () return 65537 end, sbx + 2) -checkK(function () return -65536 end, -(sbx + 1)) +local border = 65535 +checkI(function () return border end, sbx) +checkI(function () return -border end, -sbx) +checkI(function () return border + 1 end, sbx + 1) +checkK(function () return border + 2 end, sbx + 2) +checkK(function () return -(border + 1) end, -(sbx + 1)) -checkF(function () return 65535.0 end, sbx + 0.0) -checkF(function () return -65535.0 end, -sbx + 0.0) -checkF(function () return 65536.0 end, (sbx + 1.0)) -checkK(function () return 65537.0 end, (sbx + 2.0)) -checkK(function () return -65536.0 end, -(sbx + 1.0)) +local border = 65535.0 +checkF(function () return border end, sbx + 0.0) +checkF(function () return -border end, -sbx + 0.0) +checkF(function () return border + 1 end, (sbx + 1.0)) +checkK(function () return border + 2 end, (sbx + 2.0)) +checkK(function () return -(border + 1) end, -(sbx + 1.0)) -- immediate operands -checkR(function (x) return x + 1 end, 10, 11, 'ADDI', 'RETURN1') +checkR(function (x) return x + k1 end, 10, 11, 'ADDI', 'RETURN1') checkR(function (x) return 128 + x end, 0.0, 128.0, 'ADDI', 'RETURN1') checkR(function (x) return x * -127 end, -1.0, 127.0, 'MULI', 'RETURN1') checkR(function (x) return 20 * x end, 2, 40, 'MULI', 'RETURN1') @@ -276,7 +297,7 @@ checkR(function (x) return x ^ -2 end, 2, 0.25, 'POWI', 'RETURN1') checkR(function (x) return x / 40 end, 40, 1.0, 'DIVI', 'RETURN1') checkR(function (x) return x // 1 end, 10.0, 10.0, 'IDIVI', 'RETURN1') checkR(function (x) return x % (100 - 10) end, 91, 1, 'MODI', 'RETURN1') -checkR(function (x) return 1 << x end, 3, 8, 'SHLI', 'RETURN1') +checkR(function (x) return k1 << x end, 3, 8, 'SHLI', 'RETURN1') checkR(function (x) return x << 2 end, 10, 40, 'SHRI', 'RETURN1') checkR(function (x) return x >> 2 end, 8, 2, 'SHRI', 'RETURN1') checkR(function (x) return x & 1 end, 9, 1, 'BANDK', 'RETURN1') @@ -295,7 +316,7 @@ checkR(function (x) return x % (100.0 - 10) end, 91, 1.0, 'MODK', 'RETURN1') -- no foldings (and immediate operands) check(function () return -0.0 end, 'LOADF', 'UNM', 'RETURN1') -check(function () return 3/0 end, 'LOADI', 'DIVI', 'RETURN1') +check(function () return k3/0 end, 'LOADI', 'DIVI', 'RETURN1') check(function () return 0%0 end, 'LOADI', 'MODI', 'RETURN1') check(function () return -4//0 end, 'LOADI', 'IDIVI', 'RETURN1') check(function (x) return x >> 2.0 end, 'LOADF', 'SHR', 'RETURN1') @@ -335,7 +356,7 @@ end, do -- tests for table access in upvalues local t - check(function () t.x = t.y end, 'GETTABUP', 'SETTABUP') + check(function () t[kx] = t.y end, 'GETTABUP', 'SETTABUP') check(function (a) t[a()] = t[a()] end, 'MOVE', 'CALL', 'GETUPVAL', 'MOVE', 'CALL', 'GETUPVAL', 'GETTABLE', 'SETTABLE') @@ -379,6 +400,12 @@ function (a) end ) +checkequal(function () return 6 or true or nil end, + function () return k6 or kTrue or kNil end) + +checkequal(function () return 6 and true or nil end, + function () return k6 and kTrue or kNil end) + print 'OK' diff --git a/testes/constructs.lua b/testes/constructs.lua index fe4db2cbc9..8a549e10ad 100644 --- a/testes/constructs.lua +++ b/testes/constructs.lua @@ -287,7 +287,7 @@ a,b = F(nil)==nil; assert(a == true and b == nil) ------------------------------------------------------------------ -- sometimes will be 0, sometimes will not... -_ENV.GLOB1 = math.floor(os.time()) % 2 +_ENV.GLOB1 = math.random(0, 1) -- basic expressions with their respective values local basiccases = { @@ -298,6 +298,26 @@ local basiccases = { {"(0==_ENV.GLOB1)", 0 == _ENV.GLOB1}, } +local prog + +if _ENV.GLOB1 == 0 then + basiccases[2][1] = "F" -- constant false + + prog = [[ + local F = false + if %s then IX = true end + return %s +]] +else + basiccases[4][1] = "k10" -- constant 10 + + prog = [[ + local k10 = 10 + if %s then IX = true end + return %s + ]] +end + print('testing short-circuit optimizations (' .. _ENV.GLOB1 .. ')') @@ -337,8 +357,6 @@ cases[1] = basiccases for i = 2, level do cases[i] = createcases(i) end print("+") -local prog = [[if %s then IX = true end; return %s]] - local i = 0 for n = 1, level do for _, v in pairs(cases[n]) do diff --git a/testes/locals.lua b/testes/locals.lua index 0de00a98c1..1b82dd7fe7 100644 --- a/testes/locals.lua +++ b/testes/locals.lua @@ -324,7 +324,7 @@ do -- errors due to non-closable values local function foo () - local x = 34 + local x = {} end local stat, msg = pcall(foo) assert(not stat and string.find(msg, "variable 'x'")) diff --git a/testes/math.lua b/testes/math.lua index c45a91ad5c..befce12e30 100644 --- a/testes/math.lua +++ b/testes/math.lua @@ -270,7 +270,7 @@ else end do - local NaN = 0/0 + local NaN = 0/0 assert(not (NaN < 0)) assert(not (NaN > minint)) assert(not (NaN <= -9)) @@ -767,7 +767,8 @@ assert(a == '10' and b == '20') do print("testing -0 and NaN") - local mz, z = -0.0, 0.0 + local mz = -0.0 + local z = 0.0 assert(mz == z) assert(1/mz < 0 and 0 < 1/z) local a = {[mz] = 1} @@ -775,17 +776,18 @@ do a[z] = 2 assert(a[z] == 2 and a[mz] == 2) local inf = math.huge * 2 + 1 - mz, z = -1/inf, 1/inf + local mz = -1/inf + local z = 1/inf assert(mz == z) assert(1/mz < 0 and 0 < 1/z) - local NaN = inf - inf + local NaN = inf - inf assert(NaN ~= NaN) assert(not (NaN < NaN)) assert(not (NaN <= NaN)) assert(not (NaN > NaN)) assert(not (NaN >= NaN)) assert(not (0 < NaN) and not (NaN < 0)) - local NaN1 = 0/0 + local NaN1 = 0/0 assert(NaN ~= NaN1 and not (NaN <= NaN1) and not (NaN1 <= NaN)) local a = {} assert(not pcall(rawset, a, NaN, 1)) @@ -814,8 +816,8 @@ end -- the first call after seed 1007 should return 0x7a7040a5a323c9d6 do -- all computations assume at most 32-bit integers - local h = 0x7a7040a5 -- higher half - local l = 0xa323c9d6 -- lower half + local h = 0x7a7040a5 -- higher half + local l = 0xa323c9d6 -- lower half math.randomseed(1007) -- get the low 'intbits' of the 64-bit expected result From 1fb4d539254b67e7e35ed698250c66d1edff0e08 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 12 Jul 2019 16:13:50 -0300 Subject: [PATCH 0450/1145] OP_NEWTABLE keeps exact size of arrays OP_NEWTABLE is followed by an OP_EXTRAARG, so that it can keep the exact size of the array part of the table to be created. (Functions 'luaO_int2fb'/'luaO_fb2int' were removed.) --- lcode.c | 6 +++--- lcode.h | 1 + lobject.c | 26 ----------------------- lobject.h | 2 -- lopcodes.h | 10 ++++++++- lparser.c | 37 +++++++++++++++++++++++---------- ltests.c | 9 -------- lvm.c | 6 +++++- testes/code.lua | 6 ++++-- testes/nextvar.lua | 52 +++++++++++++++++----------------------------- 10 files changed, 67 insertions(+), 88 deletions(-) diff --git a/lcode.c b/lcode.c index 74ff47de52..1ff32ed723 100644 --- a/lcode.c +++ b/lcode.c @@ -430,7 +430,7 @@ static int codesJ (FuncState *fs, OpCode o, int sj, int k) { /* ** Emit an "extra argument" instruction (format 'iAx') */ -static int codeextraarg (FuncState *fs, int a) { +int luaK_codeextraarg (FuncState *fs, int a) { lua_assert(a <= MAXARG_Ax); return luaK_code(fs, CREATE_Ax(OP_EXTRAARG, a)); } @@ -446,7 +446,7 @@ static int luaK_codek (FuncState *fs, int reg, int k) { return luaK_codeABx(fs, OP_LOADK, reg, k); else { int p = luaK_codeABx(fs, OP_LOADKX, reg, 0); - codeextraarg(fs, k); + luaK_codeextraarg(fs, k); return p; } } @@ -1687,7 +1687,7 @@ void luaK_setlist (FuncState *fs, int base, int nelems, int tostore) { luaK_codeABC(fs, OP_SETLIST, base, b, c); else if (c <= MAXARG_Ax) { luaK_codeABC(fs, OP_SETLIST, base, b, 0); - codeextraarg(fs, c); + luaK_codeextraarg(fs, c); } else luaX_syntaxerror(fs->ls, "constructor too long"); diff --git a/lcode.h b/lcode.h index a15b687502..a924722cf0 100644 --- a/lcode.h +++ b/lcode.h @@ -55,6 +55,7 @@ LUAI_FUNC int luaK_codeABx (FuncState *fs, OpCode o, int A, unsigned int Bx); LUAI_FUNC int luaK_codeAsBx (FuncState *fs, OpCode o, int A, int Bx); LUAI_FUNC int luaK_codeABCk (FuncState *fs, OpCode o, int A, int B, int C, int k); +LUAI_FUNC int luaK_codeextraarg (FuncState *fs, int a); LUAI_FUNC int luaK_isKint (expdesc *e); LUAI_FUNC int luaK_exp2const (FuncState *fs, const expdesc *e, TValue *v); LUAI_FUNC void luaK_fixline (FuncState *fs, int line); diff --git a/lobject.c b/lobject.c index 2c265f964c..b4efae4f33 100644 --- a/lobject.c +++ b/lobject.c @@ -29,32 +29,6 @@ #include "lvm.h" -/* -** converts an integer to a "floating point byte", represented as -** (eeeeexxx), where the real value is (1xxx) * 2^(eeeee - 1) if -** eeeee != 0 and (xxx) otherwise. -*/ -int luaO_int2fb (unsigned int x) { - int e = 0; /* exponent */ - if (x < 8) return x; - while (x >= (8 << 4)) { /* coarse steps */ - x = (x + 0xf) >> 4; /* x = ceil(x / 16) */ - e += 4; - } - while (x >= (8 << 1)) { /* fine steps */ - x = (x + 1) >> 1; /* x = ceil(x / 2) */ - e++; - } - return ((e+1) << 3) | (cast_int(x) - 8); -} - - -/* converts back */ -int luaO_fb2int (int x) { - return (x < 8) ? x : ((x & 7) + 8) << ((x >> 3) - 1); -} - - /* ** Computes ceil(log2(x)) */ diff --git a/lobject.h b/lobject.h index 2f95bcb572..95f8e1881f 100644 --- a/lobject.h +++ b/lobject.h @@ -734,8 +734,6 @@ typedef struct Table { /* size of buffer for 'luaO_utf8esc' function */ #define UTF8BUFFSZ 8 -LUAI_FUNC int luaO_int2fb (unsigned int x); -LUAI_FUNC int luaO_fb2int (int x); LUAI_FUNC int luaO_utf8esc (char *buff, unsigned long x); LUAI_FUNC int luaO_ceillog2 (unsigned int x); LUAI_FUNC int luaO_rawarith (lua_State *L, int op, const TValue *p1, diff --git a/lopcodes.h b/lopcodes.h index 7bbbb0e56b..0b23fa6fe9 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -324,7 +324,8 @@ OP_EXTRAARG/* Ax extra (larger) argument for previous opcode */ (*) In OP_SETLIST, if (B == 0) then real B = 'top'; if (C == 0) then next 'instruction' is EXTRAARG(real C). - (*) In OP_LOADKX, the next 'instruction' is always EXTRAARG. + (*) In OP_LOADKX and OP_NEWTABLE, the next 'instruction' is always + EXTRAARG. (*) For comparisons, k specifies what condition the test should accept (true or false). @@ -375,4 +376,11 @@ LUAI_DDEC(const lu_byte luaP_opmodes[NUM_OPCODES];) #define LFIELDS_PER_FLUSH 50 +/* +** In OP_NEWTABLE, array sizes smaller than LIMTABSZ are represented +** directly in R(B). Otherwise, array size is given by +** (R(B) - LIMTABSZ) + EXTRAARG * LFIELDS_PER_FLUSH +*/ +#define LIMTABSZ (MAXARG_B - LFIELDS_PER_FLUSH) + #endif diff --git a/lparser.c b/lparser.c index 79df0217d9..193e50f1df 100644 --- a/lparser.c +++ b/lparser.c @@ -811,16 +811,16 @@ static void yindex (LexState *ls, expdesc *v) { */ -struct ConsControl { +typedef struct ConsControl { expdesc v; /* last list item read */ expdesc *t; /* table descriptor */ int nh; /* total number of 'record' elements */ int na; /* total number of array elements */ int tostore; /* number of array elements pending to be stored */ -}; +} ConsControl; -static void recfield (LexState *ls, struct ConsControl *cc) { +static void recfield (LexState *ls, ConsControl *cc) { /* recfield -> (NAME | '['exp']') = exp */ FuncState *fs = ls->fs; int reg = ls->fs->freereg; @@ -841,7 +841,7 @@ static void recfield (LexState *ls, struct ConsControl *cc) { } -static void closelistfield (FuncState *fs, struct ConsControl *cc) { +static void closelistfield (FuncState *fs, ConsControl *cc) { if (cc->v.k == VVOID) return; /* there is no list item */ luaK_exp2nextreg(fs, &cc->v); cc->v.k = VVOID; @@ -852,7 +852,7 @@ static void closelistfield (FuncState *fs, struct ConsControl *cc) { } -static void lastlistfield (FuncState *fs, struct ConsControl *cc) { +static void lastlistfield (FuncState *fs, ConsControl *cc) { if (cc->tostore == 0) return; if (hasmultret(cc->v.k)) { luaK_setmultret(fs, &cc->v); @@ -867,16 +867,15 @@ static void lastlistfield (FuncState *fs, struct ConsControl *cc) { } -static void listfield (LexState *ls, struct ConsControl *cc) { +static void listfield (LexState *ls, ConsControl *cc) { /* listfield -> exp */ expr(ls, &cc->v); - checklimit(ls->fs, cc->na, MAX_INT, "items in a constructor"); cc->na++; cc->tostore++; } -static void field (LexState *ls, struct ConsControl *cc) { +static void field (LexState *ls, ConsControl *cc) { /* field -> listfield | recfield */ switch(ls->t.token) { case TK_NAME: { /* may be 'listfield' or 'recfield' */ @@ -898,13 +897,30 @@ static void field (LexState *ls, struct ConsControl *cc) { } +static void settablesize (FuncState *fs, ConsControl *cc, int pc) { + Instruction *inst = &fs->f->code[pc]; + int rc = (cc->nh == 0) ? 0 : luaO_ceillog2(cc->nh) + 1; + int rb = cc->na; + int extra = 0; + if (rb >= LIMTABSZ) { + extra = rb / LFIELDS_PER_FLUSH; + rb = rb % LFIELDS_PER_FLUSH + LIMTABSZ; + checklimit(fs, extra, MAXARG_Ax, "items in a constructor"); + } + SETARG_C(*inst, rc); /* set initial table size */ + SETARG_B(*inst, rb); /* set initial array size */ + SETARG_Ax(*(inst + 1), extra); +} + + static void constructor (LexState *ls, expdesc *t) { /* constructor -> '{' [ field { sep field } [sep] ] '}' sep -> ',' | ';' */ FuncState *fs = ls->fs; int line = ls->linenumber; int pc = luaK_codeABC(fs, OP_NEWTABLE, 0, 0, 0); - struct ConsControl cc; + ConsControl cc; + luaK_codeextraarg(fs, 0); cc.na = cc.nh = cc.tostore = 0; cc.t = t; init_exp(t, VRELOC, pc); @@ -919,8 +935,7 @@ static void constructor (LexState *ls, expdesc *t) { } while (testnext(ls, ',') || testnext(ls, ';')); check_match(ls, '}', '{', line); lastlistfield(fs, &cc); - SETARG_B(fs->f->code[pc], luaO_int2fb(cc.na)); /* set initial array size */ - SETARG_C(fs->f->code[pc], luaO_int2fb(cc.nh)); /* set initial table size */ + settablesize(fs, &cc, pc); } /* }====================================================================== */ diff --git a/ltests.c b/ltests.c index dc83065747..cb8c422a8e 100644 --- a/ltests.c +++ b/ltests.c @@ -1103,14 +1103,6 @@ static int doremote (lua_State *L) { } -static int int2fb_aux (lua_State *L) { - int b = luaO_int2fb((unsigned int)luaL_checkinteger(L, 1)); - lua_pushinteger(L, b); - lua_pushinteger(L, (unsigned int)luaO_fb2int(b)); - return 2; -} - - static int log2_aux (lua_State *L) { unsigned int x = (unsigned int)luaL_checkinteger(L, 1); lua_pushinteger(L, luaO_ceillog2(x)); @@ -1780,7 +1772,6 @@ static const struct luaL_Reg tests_funcs[] = { {"pobj", gc_printobj}, {"getref", getref}, {"hash", hash_query}, - {"int2fb", int2fb_aux}, {"log2", log2_aux}, {"limits", get_limits}, {"listcode", listcode}, diff --git a/lvm.c b/lvm.c index a52f186fae..4011819dae 100644 --- a/lvm.c +++ b/lvm.c @@ -1250,11 +1250,15 @@ void luaV_execute (lua_State *L, CallInfo *ci) { int b = GETARG_B(i); int c = GETARG_C(i); Table *t; + c = (c == 0) ? 0 : 1 << (c - 1); /* size is 2^c */ + if (b >= LIMTABSZ) + b += LFIELDS_PER_FLUSH * GETARG_Ax(*pc) - LIMTABSZ; + pc++; /* skip extra argument */ L->top = ci->top; /* correct top in case of GC */ t = luaH_new(L); /* memory allocation */ sethvalue2s(L, ra, t); if (b != 0 || c != 0) - luaH_resize(L, t, luaO_fb2int(b), luaO_fb2int(c)); /* idem */ + luaH_resize(L, t, b, c); /* idem */ checkGC(L, ra + 1); vmbreak; } diff --git a/testes/code.lua b/testes/code.lua index 49d682f8eb..b2702c61bc 100644 --- a/testes/code.lua +++ b/testes/code.lua @@ -93,11 +93,13 @@ end -- some basic instructions check(function () -- function does not create upvalues (function () end){f()} -end, 'CLOSURE', 'NEWTABLE', 'GETTABUP', 'CALL', 'SETLIST', 'CALL', 'RETURN0') +end, 'CLOSURE', 'NEWTABLE', 'EXTRAARG', 'GETTABUP', 'CALL', + 'SETLIST', 'CALL', 'RETURN0') check(function (x) -- function creates upvalues (function () return x end){f()} -end, 'CLOSURE', 'NEWTABLE', 'GETTABUP', 'CALL', 'SETLIST', 'CALL', 'RETURN') +end, 'CLOSURE', 'NEWTABLE', 'EXTRAARG', 'GETTABUP', 'CALL', + 'SETLIST', 'CALL', 'RETURN') -- sequence of LOADNILs diff --git a/testes/nextvar.lua b/testes/nextvar.lua index 87a6bfa81c..bdc9fc2999 100644 --- a/testes/nextvar.lua +++ b/testes/nextvar.lua @@ -49,33 +49,13 @@ if not T then else --[ -- testing table sizes -local function log2 (x) return math.log(x, 2) end local function mp2 (n) -- minimum power of 2 >= n - local mp = 2^math.ceil(log2(n)) + local mp = 2^math.ceil(math.log(n, 2)) assert(n == 0 or (mp/2 < n and n <= mp)) return mp end -local function fb (n) - local r, nn = T.int2fb(n) - assert(r < 256) - return nn -end - --- test fb function -for a = 1, 10000 do -- all numbers up to 10^4 - local n = fb(a) - assert(a <= n and n <= a*1.125) -end -local a = 1024 -- plus a few up to 2 ^30 -local lim = 2^30 -while a < lim do - local n = fb(a) - assert(a <= n and n <= a*1.125) - a = math.ceil(a*1.3) -end - local function check (t, na, nh) local a, h = T.querytab(t) @@ -95,24 +75,30 @@ end -- testing constructor sizes -local lim = 40 -local s = 'return {' -for i=1,lim do - s = s..i..',' - local s = s - for k=0,lim do - local t = load(s..'}', '')() - assert(#t == i) - check(t, fb(i), mp2(k)) - s = string.format('%sa%d=%d,', s, k, k) +local sizes = {0, 1, 2, 3, 4, 5, 7, 8, 9, 15, 16, 17, + 30, 31, 32, 33, 34, 500, 1000} + +for _, sa in ipairs(sizes) do -- 'sa' is size of the array part + local arr = {"return {"} + -- array part + for i = 1, sa do arr[1 + i] = "1," end + for _, sh in ipairs(sizes) do -- 'sh' is size of the hash part + for j = 1, sh do -- hash part + arr[1 + sa + j] = string.format('k%x=%d,', j, j) + end + arr[1 + sa + sh + 1] = "}" + local prog = table.concat(arr) + local t = assert(load(prog))() + assert(#t == sa) + check(t, sa, mp2(sh)) end end -- tests with unknown number of elements local a = {} -for i=1,lim do a[i] = i end -- build auxiliary table -for k=0,lim do +for i=1,sizes[#sizes] do a[i] = i end -- build auxiliary table +for k in ipairs(sizes) do local a = {table.unpack(a,1,k)} assert(#a == k) check(a, k, 0) From dd6d8db49acda5d5353a0a9c42485d9b4bde419d Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 12 Jul 2019 16:47:02 -0300 Subject: [PATCH 0451/1145] Reordering of instructions in the main loop The instructions in the main interpreter loop were reordered to the same order of their enumeration in 'lopcodes.h'. --- lvm.c | 88 +++++++++++++++++++++++++++++------------------------------ 1 file changed, 44 insertions(+), 44 deletions(-) diff --git a/lvm.c b/lvm.c index 4011819dae..ec65ea780f 100644 --- a/lvm.c +++ b/lvm.c @@ -1097,11 +1097,6 @@ void luaV_execute (lua_State *L, CallInfo *ci) { setobjs2s(L, ra, RB(i)); vmbreak; } - vmcase(OP_LOADK) { - TValue *rb = k + GETARG_Bx(i); - setobj2s(L, ra, rb); - vmbreak; - } vmcase(OP_LOADI) { lua_Integer b = GETARG_sBx(i); setivalue(s2v(ra), b); @@ -1112,6 +1107,11 @@ void luaV_execute (lua_State *L, CallInfo *ci) { setfltvalue(s2v(ra), cast_num(b)); vmbreak; } + vmcase(OP_LOADK) { + TValue *rb = k + GETARG_Bx(i); + setobj2s(L, ra, rb); + vmbreak; + } vmcase(OP_LOADKX) { TValue *rb; rb = k + GETARG_Ax(*pc); pc++; @@ -1331,6 +1331,45 @@ void luaV_execute (lua_State *L, CallInfo *ci) { op_arithK(L, luaV_idiv, luai_numidiv, TM_IDIV, 0); vmbreak; } + vmcase(OP_BANDK) { + op_bitwiseK(L, l_band, TM_BAND); + vmbreak; + } + vmcase(OP_BORK) { + op_bitwiseK(L, l_bor, TM_BOR); + vmbreak; + } + vmcase(OP_BXORK) { + op_bitwiseK(L, l_bxor, TM_BXOR); + vmbreak; + } + vmcase(OP_SHRI) { + TValue *rb = vRB(i); + int ic = GETARG_sC(i); + lua_Integer ib; + if (tointegerns(rb, &ib)) { + setivalue(s2v(ra), luaV_shiftl(ib, -ic)); + } + else { + TMS ev = TM_SHR; + if (TESTARG_k(i)) { + ic = -ic; ev = TM_SHL; + } + Protect(luaT_trybiniTM(L, rb, ic, 0, ra, ev)); + } + vmbreak; + } + vmcase(OP_SHLI) { + TValue *rb = vRB(i); + int ic = GETARG_sC(i); + lua_Integer ib; + if (tointegerns(rb, &ib)) { + setivalue(s2v(ra), luaV_shiftl(ic, ib)); + } + else + Protect(luaT_trybiniTM(L, rb, ic, 1, ra, TM_SHL)); + vmbreak; + } vmcase(OP_ADD) { op_arith(L, l_addi, luai_numadd, TM_ADD); vmbreak; @@ -1359,18 +1398,6 @@ void luaV_execute (lua_State *L, CallInfo *ci) { op_arith(L, luaV_idiv, luai_numidiv, TM_IDIV); vmbreak; } - vmcase(OP_BANDK) { - op_bitwiseK(L, l_band, TM_BAND); - vmbreak; - } - vmcase(OP_BORK) { - op_bitwiseK(L, l_bor, TM_BOR); - vmbreak; - } - vmcase(OP_BXORK) { - op_bitwiseK(L, l_bxor, TM_BXOR); - vmbreak; - } vmcase(OP_BAND) { op_bitwise(L, l_band, TM_BAND); vmbreak; @@ -1383,33 +1410,6 @@ void luaV_execute (lua_State *L, CallInfo *ci) { op_bitwise(L, l_bxor, TM_BXOR); vmbreak; } - vmcase(OP_SHRI) { - TValue *rb = vRB(i); - int ic = GETARG_sC(i); - lua_Integer ib; - if (tointegerns(rb, &ib)) { - setivalue(s2v(ra), luaV_shiftl(ib, -ic)); - } - else { - TMS ev = TM_SHR; - if (TESTARG_k(i)) { - ic = -ic; ev = TM_SHL; - } - Protect(luaT_trybiniTM(L, rb, ic, 0, ra, ev)); - } - vmbreak; - } - vmcase(OP_SHLI) { - TValue *rb = vRB(i); - int ic = GETARG_sC(i); - lua_Integer ib; - if (tointegerns(rb, &ib)) { - setivalue(s2v(ra), luaV_shiftl(ic, ib)); - } - else - Protect(luaT_trybiniTM(L, rb, ic, 1, ra, TM_SHL)); - vmbreak; - } vmcase(OP_SHR) { TValue *rb = vRB(i); TValue *rc = vRC(i); From 758c1ef445ab27d89bace746111add04083a8e20 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 15 Jul 2019 14:59:35 -0300 Subject: [PATCH 0452/1145] Unification of size representation in OP_NEWTABLE and OP_SETLIST Opcodes OP_NEWTABLE and OP_SETLIST use the same representation to store the size of the array part of a table. This new representation can go up to 2^33 (8 + 25 bits). --- lcode.c | 40 ++++++++++++++++++++++++++++------------ lcode.h | 4 +++- lopcodes.h | 23 ++++++++++------------- lparser.c | 29 +++++++---------------------- lvm.c | 28 ++++++++++++++-------------- testes/nextvar.lua | 28 ++++++++++++++++++---------- 6 files changed, 80 insertions(+), 72 deletions(-) diff --git a/lcode.c b/lcode.c index 1ff32ed723..a0d7757acf 100644 --- a/lcode.c +++ b/lcode.c @@ -372,7 +372,7 @@ static void removelastinstruction (FuncState *fs) { ** Emit instruction 'i', checking for array sizes and saving also its ** line information. Return 'i' position. */ -static int luaK_code (FuncState *fs, Instruction i) { +int luaK_code (FuncState *fs, Instruction i) { Proto *f = fs->f; /* put new instruction in code array */ luaM_growvector(fs->ls->L, f->code, fs->pc, f->sizecode, Instruction, @@ -430,7 +430,7 @@ static int codesJ (FuncState *fs, OpCode o, int sj, int k) { /* ** Emit an "extra argument" instruction (format 'iAx') */ -int luaK_codeextraarg (FuncState *fs, int a) { +static int codeextraarg (FuncState *fs, int a) { lua_assert(a <= MAXARG_Ax); return luaK_code(fs, CREATE_Ax(OP_EXTRAARG, a)); } @@ -446,7 +446,7 @@ static int luaK_codek (FuncState *fs, int reg, int k) { return luaK_codeABx(fs, OP_LOADK, reg, k); else { int p = luaK_codeABx(fs, OP_LOADKX, reg, 0); - luaK_codeextraarg(fs, k); + codeextraarg(fs, k); return p; } } @@ -1672,6 +1672,22 @@ void luaK_fixline (FuncState *fs, int line) { } +void luaK_settablesize (FuncState *fs, int pc, int ra, int rc, int rb) { + Instruction *inst = &fs->f->code[pc]; + int extra = 0; + int k = 0; + if (rb != 0) + rb = luaO_ceillog2(rb) + 1; /* hash size */ + if (rc > MAXARG_C) { /* does it need the extra argument? */ + extra = rc / (MAXARG_C + 1); + rc %= (MAXARG_C + 1); + k = 1; + } + *inst = CREATE_ABCk(OP_NEWTABLE, ra, rb, rc, k); + *(inst + 1) = CREATE_Ax(OP_EXTRAARG, extra); +} + + /* ** Emit a SETLIST instruction. ** 'base' is register that keeps table; @@ -1680,17 +1696,17 @@ void luaK_fixline (FuncState *fs, int line) { ** table (or LUA_MULTRET to add up to stack top). */ void luaK_setlist (FuncState *fs, int base, int nelems, int tostore) { - int c = (nelems - 1)/LFIELDS_PER_FLUSH + 1; - int b = (tostore == LUA_MULTRET) ? 0 : tostore; lua_assert(tostore != 0 && tostore <= LFIELDS_PER_FLUSH); - if (c <= MAXARG_C) - luaK_codeABC(fs, OP_SETLIST, base, b, c); - else if (c <= MAXARG_Ax) { - luaK_codeABC(fs, OP_SETLIST, base, b, 0); - luaK_codeextraarg(fs, c); + if (tostore == LUA_MULTRET) + tostore = 0; + if (nelems <= MAXARG_C) + luaK_codeABC(fs, OP_SETLIST, base, tostore, nelems); + else { + int extra = nelems / (MAXARG_C + 1); + nelems %= (MAXARG_C + 1); + luaK_codeABCk(fs, OP_SETLIST, base, tostore, nelems, 1); + codeextraarg(fs, extra); } - else - luaX_syntaxerror(fs->ls, "constructor too long"); fs->freereg = base + 1; /* free registers with list values */ } diff --git a/lcode.h b/lcode.h index a924722cf0..0c12bb648b 100644 --- a/lcode.h +++ b/lcode.h @@ -51,11 +51,11 @@ typedef enum UnOpr { OPR_MINUS, OPR_BNOT, OPR_NOT, OPR_LEN, OPR_NOUNOPR } UnOpr; #define luaK_jumpto(fs,t) luaK_patchlist(fs, luaK_jump(fs), t) +LUAI_FUNC int luaK_code (FuncState *fs, Instruction i); LUAI_FUNC int luaK_codeABx (FuncState *fs, OpCode o, int A, unsigned int Bx); LUAI_FUNC int luaK_codeAsBx (FuncState *fs, OpCode o, int A, int Bx); LUAI_FUNC int luaK_codeABCk (FuncState *fs, OpCode o, int A, int B, int C, int k); -LUAI_FUNC int luaK_codeextraarg (FuncState *fs, int a); LUAI_FUNC int luaK_isKint (expdesc *e); LUAI_FUNC int luaK_exp2const (FuncState *fs, const expdesc *e, TValue *v); LUAI_FUNC void luaK_fixline (FuncState *fs, int line); @@ -87,6 +87,8 @@ LUAI_FUNC void luaK_prefix (FuncState *fs, UnOpr op, expdesc *v, int line); LUAI_FUNC void luaK_infix (FuncState *fs, BinOpr op, expdesc *v); LUAI_FUNC void luaK_posfix (FuncState *fs, BinOpr op, expdesc *v1, expdesc *v2, int line); +LUAI_FUNC void luaK_settablesize (FuncState *fs, int pc, + int ra, int rb, int rc); LUAI_FUNC void luaK_setlist (FuncState *fs, int base, int nelems, int tostore); LUAI_FUNC void luaK_finish (FuncState *fs); LUAI_FUNC l_noret luaK_semerror (LexState *ls, const char *msg); diff --git a/lopcodes.h b/lopcodes.h index 0b23fa6fe9..371cb3ae16 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -214,7 +214,7 @@ OP_SETTABLE,/* A B C R(A)[R(B)] := RK(C) */ OP_SETI,/* A B C R(A)[B] := RK(C) */ OP_SETFIELD,/* A B C R(A)[K(B):string] := RK(C) */ -OP_NEWTABLE,/* A B C R(A) := {} (size = B,C) */ +OP_NEWTABLE,/* A B C R(A) := {} */ OP_SELF,/* A B C R(A+1) := R(B); R(A) := R(B)[RK(C):string] */ @@ -321,12 +321,17 @@ OP_EXTRAARG/* Ax extra (larger) argument for previous opcode */ (*) In OP_RETURN, if (B == 0) then return up to 'top'. - (*) In OP_SETLIST, if (B == 0) then real B = 'top'; if (C == 0) then - next 'instruction' is EXTRAARG(real C). - - (*) In OP_LOADKX and OP_NEWTABLE, the next 'instruction' is always + (*) In OP_LOADKX and OP_NEWTABLE, the next instruction is always EXTRAARG. + (*) In OP_SETLIST, if (B == 0) then real B = 'top'; if k, then + real C = EXTRAARG _ C (the bits of EXTRAARG concatenated with the + bits of C). + + (*) In OP_NEWTABLE, B is log2 of the hash size (which is always a + power of 2) plus 1, or zero for size zero. If not k, the array size + is C. Otherwise, the array size is EXTRAARG _ C. + (*) For comparisons, k specifies what condition the test should accept (true or false). @@ -375,12 +380,4 @@ LUAI_DDEC(const lu_byte luaP_opmodes[NUM_OPCODES];) /* number of list items to accumulate before a SETLIST instruction */ #define LFIELDS_PER_FLUSH 50 - -/* -** In OP_NEWTABLE, array sizes smaller than LIMTABSZ are represented -** directly in R(B). Otherwise, array size is given by -** (R(B) - LIMTABSZ) + EXTRAARG * LFIELDS_PER_FLUSH -*/ -#define LIMTABSZ (MAXARG_B - LFIELDS_PER_FLUSH) - #endif diff --git a/lparser.c b/lparser.c index 193e50f1df..ea81000613 100644 --- a/lparser.c +++ b/lparser.c @@ -815,7 +815,7 @@ typedef struct ConsControl { expdesc v; /* last list item read */ expdesc *t; /* table descriptor */ int nh; /* total number of 'record' elements */ - int na; /* total number of array elements */ + int na; /* number of array elements already stored */ int tostore; /* number of array elements pending to be stored */ } ConsControl; @@ -847,6 +847,7 @@ static void closelistfield (FuncState *fs, ConsControl *cc) { cc->v.k = VVOID; if (cc->tostore == LFIELDS_PER_FLUSH) { luaK_setlist(fs, cc->t->u.info, cc->na, cc->tostore); /* flush */ + cc->na += cc->tostore; cc->tostore = 0; /* no more items pending */ } } @@ -864,13 +865,13 @@ static void lastlistfield (FuncState *fs, ConsControl *cc) { luaK_exp2nextreg(fs, &cc->v); luaK_setlist(fs, cc->t->u.info, cc->na, cc->tostore); } + cc->na += cc->tostore; } static void listfield (LexState *ls, ConsControl *cc) { /* listfield -> exp */ expr(ls, &cc->v); - cc->na++; cc->tostore++; } @@ -897,22 +898,6 @@ static void field (LexState *ls, ConsControl *cc) { } -static void settablesize (FuncState *fs, ConsControl *cc, int pc) { - Instruction *inst = &fs->f->code[pc]; - int rc = (cc->nh == 0) ? 0 : luaO_ceillog2(cc->nh) + 1; - int rb = cc->na; - int extra = 0; - if (rb >= LIMTABSZ) { - extra = rb / LFIELDS_PER_FLUSH; - rb = rb % LFIELDS_PER_FLUSH + LIMTABSZ; - checklimit(fs, extra, MAXARG_Ax, "items in a constructor"); - } - SETARG_C(*inst, rc); /* set initial table size */ - SETARG_B(*inst, rb); /* set initial array size */ - SETARG_Ax(*(inst + 1), extra); -} - - static void constructor (LexState *ls, expdesc *t) { /* constructor -> '{' [ field { sep field } [sep] ] '}' sep -> ',' | ';' */ @@ -920,12 +905,12 @@ static void constructor (LexState *ls, expdesc *t) { int line = ls->linenumber; int pc = luaK_codeABC(fs, OP_NEWTABLE, 0, 0, 0); ConsControl cc; - luaK_codeextraarg(fs, 0); + luaK_code(fs, 0); /* space for extra arg. */ cc.na = cc.nh = cc.tostore = 0; cc.t = t; - init_exp(t, VRELOC, pc); + init_exp(t, VNONRELOC, fs->freereg); /* table will be at stack top */ + luaK_reserveregs(fs, 1); init_exp(&cc.v, VVOID, 0); /* no value (yet) */ - luaK_exp2nextreg(ls->fs, t); /* fix it at stack top */ checknext(ls, '{'); do { lua_assert(cc.v.k == VVOID || cc.tostore > 0); @@ -935,7 +920,7 @@ static void constructor (LexState *ls, expdesc *t) { } while (testnext(ls, ',') || testnext(ls, ';')); check_match(ls, '}', '{', line); lastlistfield(fs, &cc); - settablesize(fs, &cc, pc); + luaK_settablesize(fs, pc, t->u.info, cc.na, cc.nh); } /* }====================================================================== */ diff --git a/lvm.c b/lvm.c index ec65ea780f..d365bcdd86 100644 --- a/lvm.c +++ b/lvm.c @@ -1247,18 +1247,19 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_NEWTABLE) { - int b = GETARG_B(i); - int c = GETARG_C(i); + int b = GETARG_B(i); /* log2(hash size) + 1 */ + int c = GETARG_C(i); /* array size */ Table *t; - c = (c == 0) ? 0 : 1 << (c - 1); /* size is 2^c */ - if (b >= LIMTABSZ) - b += LFIELDS_PER_FLUSH * GETARG_Ax(*pc) - LIMTABSZ; + if (b > 0) + b = 1 << (b - 1); /* size is 2^(b - 1) */ + if (TESTARG_k(i)) + c += GETARG_Ax(*pc) * (MAXARG_C + 1); pc++; /* skip extra argument */ L->top = ci->top; /* correct top in case of GC */ t = luaH_new(L); /* memory allocation */ sethvalue2s(L, ra, t); if (b != 0 || c != 0) - luaH_resize(L, t, b, c); /* idem */ + luaH_resize(L, t, c, b); /* idem */ checkGC(L, ra + 1); vmbreak; } @@ -1763,18 +1764,17 @@ void luaV_execute (lua_State *L, CallInfo *ci) { } vmcase(OP_SETLIST) { int n = GETARG_B(i); - int c = GETARG_C(i); - unsigned int last; - Table *h; + unsigned int last = GETARG_C(i); + Table *h = hvalue(s2v(ra)); if (n == 0) - n = cast_int(L->top - ra) - 1; + n = cast_int(L->top - ra) - 1; /* get up to the top */ else L->top = ci->top; /* correct top in case of GC */ - if (c == 0) { - c = GETARG_Ax(*pc); pc++; + last += n; + if (TESTARG_k(i)) { + last += GETARG_Ax(*pc) * (MAXARG_C + 1); + pc++; } - h = hvalue(s2v(ra)); - last = ((c-1)*LFIELDS_PER_FLUSH) + n; if (last > luaH_realasize(h)) /* needs more space? */ luaH_resizearray(L, h, last); /* preallocate it at once */ for (; n > 0; n--) { diff --git a/testes/nextvar.lua b/testes/nextvar.lua index bdc9fc2999..a7fe625e10 100644 --- a/testes/nextvar.lua +++ b/testes/nextvar.lua @@ -80,15 +80,23 @@ local sizes = {0, 1, 2, 3, 4, 5, 7, 8, 9, 15, 16, 17, for _, sa in ipairs(sizes) do -- 'sa' is size of the array part local arr = {"return {"} - -- array part - for i = 1, sa do arr[1 + i] = "1," end + for i = 1, sa do arr[1 + i] = "1," end -- build array part for _, sh in ipairs(sizes) do -- 'sh' is size of the hash part - for j = 1, sh do -- hash part + for j = 1, sh do -- build hash part arr[1 + sa + j] = string.format('k%x=%d,', j, j) end arr[1 + sa + sh + 1] = "}" local prog = table.concat(arr) - local t = assert(load(prog))() + local f = assert(load(prog)) + f() -- call once to ensure stack space + -- make sure table is not resized after being created + if sa == 0 or sh == 0 then + T.alloccount(2); -- header + array or hash part + else + T.alloccount(3); -- header + array part + hash part + end + local t = f() + T.alloccount(); assert(#t == sa) check(t, sa, mp2(sh)) end @@ -99,12 +107,12 @@ end local a = {} for i=1,sizes[#sizes] do a[i] = i end -- build auxiliary table for k in ipairs(sizes) do - local a = {table.unpack(a,1,k)} - assert(#a == k) - check(a, k, 0) - a = {1,2,3,table.unpack(a,1,k)} - check(a, k+3, 0) - assert(#a == k + 3) + local t = {table.unpack(a,1,k)} + assert(#t == k) + check(t, k, 0) + t = {1,2,3,table.unpack(a,1,k)} + check(t, k+3, 0) + assert(#t == k + 3) end From 298f383ffcc30d0799fbca0293175f647fe6bccf Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 16 Jul 2019 14:13:22 -0300 Subject: [PATCH 0453/1145] Avoid setting the stack top below upvalues to be closed When leaving a scope, the new stack top should be set only after closing any upvalue, to avoid manipulating values in an "invalid" part of the stack. --- lapi.c | 15 ++++++++------- lfunc.c | 1 + lvm.c | 6 ++++-- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/lapi.c b/lapi.c index 661fdb145f..0f81107faf 100644 --- a/lapi.c +++ b/lapi.c @@ -171,19 +171,20 @@ LUA_API int lua_gettop (lua_State *L) { LUA_API void lua_settop (lua_State *L, int idx) { StkId func = L->ci->func; + int diff; /* difference for new top */ lua_lock(L); if (idx >= 0) { - StkId newtop = (func + 1) + idx; - api_check(L, idx <= L->stack_last - (func + 1), "new top too large"); - while (L->top < newtop) - setnilvalue(s2v(L->top++)); - L->top = newtop; + api_check(L, idx <= L->ci->top - (func + 1), "new top too large"); + diff = (func + 1) + idx - L->top; + for (; diff > 0; diff--) + setnilvalue(s2v(L->top++)); /* clear new slots */ } else { api_check(L, -(idx+1) <= (L->top - (func + 1)), "invalid new top"); - L->top += idx+1; /* 'subtract' index (index is negative) */ + diff = idx + 1; /* will "subtract" index (as it is negative) */ } - luaF_close(L, L->top, LUA_OK); + luaF_close(L, L->top + diff, LUA_OK); + L->top += diff; /* correct top only after closing any upvalue */ lua_unlock(L); } diff --git a/lfunc.c b/lfunc.c index 551149927f..68d0632aa9 100644 --- a/lfunc.c +++ b/lfunc.c @@ -202,6 +202,7 @@ int luaF_close (lua_State *L, StkId level, int status) { while ((uv = L->openupval) != NULL && uplevel(uv) >= level) { StkId upl = uplevel(uv); TValue *slot = &uv->u.value; /* new position for value */ + lua_assert(upl < L->top); luaF_unlinkupval(uv); setobj(L, slot, uv->v); /* move value to upvalue slot */ uv->v = slot; /* now current value lives here */ diff --git a/lvm.c b/lvm.c index d365bcdd86..9838500b60 100644 --- a/lvm.c +++ b/lvm.c @@ -1601,15 +1601,17 @@ void luaV_execute (lua_State *L, CallInfo *ci) { int n = GETARG_B(i) - 1; /* number of results */ if (n < 0) /* not fixed? */ n = cast_int(L->top - ra); /* get what is available */ - else - L->top = ra + n; /* set call for 'luaD_poscall' */ savepc(ci); if (TESTARG_k(i)) { int nparams1 = GETARG_C(i); + if (L->top < ci->top) + L->top = ci->top; luaF_close(L, base, LUA_OK); /* there may be open upvalues */ + updatestack(ci); if (nparams1) /* vararg function? */ ci->func -= ci->u.l.nextraargs + nparams1; } + L->top = ra + n; /* set call for 'luaD_poscall' */ luaD_poscall(L, ci, n); return; } From c220b0a5d099372e58e517b9f13eaa7bb0bec45c Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 16 Jul 2019 15:17:47 -0300 Subject: [PATCH 0454/1145] '__close' method may be called again in case of error An error in a closing method may be caused by a lack of resources, such as memory or stack space, and the error may free enough resources (by unwinding the stack) to allow the method to work if called again. If the closing method is already running after some error (including its own), it is not called again. --- lfunc.c | 22 ++++++++++++---------- manual/manual.of | 11 ++++++----- testes/locals.lua | 21 ++++++++++++++------- 3 files changed, 32 insertions(+), 22 deletions(-) diff --git a/lfunc.c b/lfunc.c index 68d0632aa9..cd85cc1f0b 100644 --- a/lfunc.c +++ b/lfunc.c @@ -133,7 +133,8 @@ static int prepclosingmethod (lua_State *L, TValue *obj, TValue *err) { ** the 'level' of the upvalue being closed, as everything after ** that won't be used again. */ -static int callclosemth (lua_State *L, TValue *uv, StkId level, int status) { +static int callclosemth (lua_State *L, StkId level, int status) { + TValue *uv = s2v(level); /* value being closed */ if (likely(status == LUA_OK)) { if (prepclosingmethod(L, uv, &G(L)->nilvalue)) /* something to call? */ callclose(L, NULL); /* call closing method */ @@ -145,9 +146,10 @@ static int callclosemth (lua_State *L, TValue *uv, StkId level, int status) { } } else { /* must close the object in protected mode */ - ptrdiff_t oldtop = savestack(L, level + 1); - /* save error message and set stack top to 'level + 1' */ - luaD_seterrorobj(L, status, level); + ptrdiff_t oldtop; + level++; /* space for error message */ + oldtop = savestack(L, level + 1); /* top will be after that */ + luaD_seterrorobj(L, status, level); /* set error message */ if (prepclosingmethod(L, uv, s2v(level))) { /* something to call? */ int newstatus = luaD_pcall(L, callclose, NULL, oldtop, 0); if (newstatus != LUA_OK && status == CLOSEPROTECT) /* first error? */ @@ -203,18 +205,18 @@ int luaF_close (lua_State *L, StkId level, int status) { StkId upl = uplevel(uv); TValue *slot = &uv->u.value; /* new position for value */ lua_assert(upl < L->top); + if (uv->tt == LUA_TUPVALTBC && status != NOCLOSINGMETH) { + /* must run closing method */ + ptrdiff_t levelrel = savestack(L, level); + status = callclosemth(L, upl, status); /* may change the stack */ + level = restorestack(L, levelrel); + } luaF_unlinkupval(uv); setobj(L, slot, uv->v); /* move value to upvalue slot */ uv->v = slot; /* now current value lives here */ if (!iswhite(uv)) gray2black(uv); /* closed upvalues cannot be gray */ luaC_barrier(L, uv, slot); - if (uv->tt == LUA_TUPVALTBC && status != NOCLOSINGMETH) { - /* must run closing method */ - ptrdiff_t levelrel = savestack(L, level); - status = callclosemth(L, uv->v, upl, status); /* may change the stack */ - level = restorestack(L, levelrel); - } } return status; } diff --git a/manual/manual.of b/manual/manual.of index 61fcdaa3c8..3d2fb4fbaf 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -1548,14 +1548,15 @@ they are closed in the reverse order that they were declared. If there is any error while running a closing method, that error is handled like an error in the regular code -where the variable was defined; -in particular, -the other pending closing methods will still be called. +where the variable was defined. +However, Lua may call the method one more time. + After an error, -other errors in closing methods +the other pending closing methods will still be called. +Errors in these methods interrupt the respective method, but are otherwise ignored; -the error reported is the original one. +the error reported is only the original one. If a coroutine yields and is never resumed again, some variables may never go out of scope, diff --git a/testes/locals.lua b/testes/locals.lua index 1b82dd7fe7..73267d028f 100644 --- a/testes/locals.lua +++ b/testes/locals.lua @@ -295,18 +295,23 @@ do -- errors in __close local y = func2close(function (self, msg) log[#log + 1] = msg; error(2) end) local z = - func2close(function (self, msg) log[#log + 1] = msg or 10; error(3) end) + func2close(function (self, msg) + log[#log + 1] = (msg or 10) + 1; + error(3) + end) if err then error(4) end end local stat, msg = pcall(foo, false) assert(msg == 3) - assert(log[1] == 10 and log[2] == 3 and log[3] == 3 and log[4] == 3 - and #log == 4) + -- 'z' close is called twice + assert(log[1] == 11 and log[2] == 4 and log[3] == 3 and log[4] == 3 + and log[5] == 3 and #log == 5) log = {} local stat, msg = pcall(foo, true) assert(msg == 4) - assert(log[1] == 4 and log[2] == 4 and log[3] == 4 and log[4] == 4 + -- 'z' close is called once + assert(log[1] == 5 and log[2] == 4 and log[3] == 4 and log[4] == 4 and #log == 4) -- error in toclose in vararg function @@ -495,15 +500,17 @@ do local st, msg = pcall(co); assert(x == 2) assert(not st and msg == 200) -- should get first error raised - x = 0 + local x = 0 + local y = 0 co = coroutine.wrap(function () - local xx = func2close(function () x = x + 1; error("YYY") end) + local xx = func2close(function () y = y + 1; error("YYY") end) local xv = func2close(function () x = x + 1; error("XXX") end) coroutine.yield(100) return 200 end) assert(co() == 100); assert(x == 0) - local st, msg = pcall(co); assert(x == 2) + local st, msg = pcall(co) + assert(x == 2 and y == 1) -- first close is called twice -- should get first error raised assert(not st and string.find(msg, "%w+%.%w+:%d+: XXX")) end From 4846f7e3bb1397142ab0de808ae59c08db9832a6 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 16 Jul 2019 15:44:37 -0300 Subject: [PATCH 0455/1145] Micro optimization in OP_RETURN and OP_TAILCALL Many functions are vararg but create no upvalues, so it is better to separate the tests for these two kinds of "extra work". --- lcode.c | 8 ++++---- lopcodes.h | 7 +++---- lvm.c | 13 ++++++------- 3 files changed, 13 insertions(+), 15 deletions(-) diff --git a/lcode.c b/lcode.c index a0d7757acf..e57ad28461 100644 --- a/lcode.c +++ b/lcode.c @@ -1745,10 +1745,10 @@ void luaK_finish (FuncState *fs) { SET_OPCODE(*pc, OP_RETURN); } /* FALLTHROUGH */ case OP_RETURN: case OP_TAILCALL: { - if (fs->needclose || p->is_vararg) { - SETARG_C(*pc, p->is_vararg ? p->numparams + 1 : 0); - SETARG_k(*pc, 1); /* signal that there is extra work */ - } + if (fs->needclose) + SETARG_k(*pc, 1); /* signal that it needs to close */ + if (p->is_vararg) + SETARG_C(*pc, p->numparams + 1); /* signal that it is vararg */ break; } case OP_JMP: { diff --git a/lopcodes.h b/lopcodes.h index 371cb3ae16..26b1850d5d 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -338,10 +338,9 @@ OP_EXTRAARG/* Ax extra (larger) argument for previous opcode */ (*) All 'skips' (pc++) assume that next instruction is a jump. (*) In instructions OP_RETURN/OP_TAILCALL, 'k' specifies that the - function either builds upvalues, which may need to be closed, or is - vararg, which must be corrected before returning. When 'k' is true, - C > 0 means the function is vararg and (C - 1) is its number of - fixed parameters. + function builds upvalues, which may need to be closed. C > 0 means + the function is vararg, so that its 'func' must be corrected before + returning; in this case, (C - 1) is its number of fixed parameters. (*) In comparisons with an immediate operand, C signals whether the original operand was a float. diff --git a/lvm.c b/lvm.c index 9838500b60..7e6f148d70 100644 --- a/lvm.c +++ b/lvm.c @@ -1564,16 +1564,15 @@ void luaV_execute (lua_State *L, CallInfo *ci) { } vmcase(OP_TAILCALL) { int b = GETARG_B(i); /* number of arguments + 1 (function) */ - int delta = 0; /* virtual 'func' - real 'func' (vararg functions) */ + int nparams1 = GETARG_C(i); + /* delat is virtual 'func' - real 'func' (vararg functions) */ + int delta = (nparams1) ? ci->u.l.nextraargs + nparams1 : 0; if (b != 0) L->top = ra + b; else /* previous instruction set top */ b = cast_int(L->top - ra); savepc(ci); /* some calls here can raise errors */ if (TESTARG_k(i)) { - int nparams1 = GETARG_C(i); - if (nparams1) /* vararg function? */ - delta = ci->u.l.nextraargs + nparams1; /* close upvalues from current call; the compiler ensures that there are no to-be-closed variables here */ luaF_close(L, base, NOCLOSINGMETH); @@ -1599,18 +1598,18 @@ void luaV_execute (lua_State *L, CallInfo *ci) { } vmcase(OP_RETURN) { int n = GETARG_B(i) - 1; /* number of results */ + int nparams1 = GETARG_C(i); if (n < 0) /* not fixed? */ n = cast_int(L->top - ra); /* get what is available */ savepc(ci); if (TESTARG_k(i)) { - int nparams1 = GETARG_C(i); if (L->top < ci->top) L->top = ci->top; luaF_close(L, base, LUA_OK); /* there may be open upvalues */ updatestack(ci); - if (nparams1) /* vararg function? */ - ci->func -= ci->u.l.nextraargs + nparams1; } + if (nparams1) /* vararg function? */ + ci->func -= ci->u.l.nextraargs + nparams1; L->top = ra + n; /* set call for 'luaD_poscall' */ luaD_poscall(L, ci, n); return; From d6af81084df569bc8e3bd0949ad6fc0b40c8468d Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 17 Jul 2019 14:26:56 -0300 Subject: [PATCH 0456/1145] New kind of expression VKSTR String literal expressions have their own kind VKSTR, instead of the generic VK. This allows strings to "cross" functions without entering their constant tables (e.g., if they are used only by some nested function). --- lcode.c | 35 +++++++++++++++++++++++++---------- lcode.h | 1 - lparser.c | 14 ++++++++------ lparser.h | 5 ++++- testes/code.lua | 17 +++++++++++++++++ 5 files changed, 54 insertions(+), 18 deletions(-) diff --git a/lcode.c b/lcode.c index e57ad28461..40efcff307 100644 --- a/lcode.c +++ b/lcode.c @@ -81,9 +81,8 @@ int luaK_exp2const (FuncState *fs, const expdesc *e, TValue *v) { case VNIL: setnilvalue(v); return 1; - case VK: { - TValue *k = &fs->f->k[e->u.info]; - setobj(fs->ls->L, v, k); + case VKSTR: { + setsvalue(fs->ls->L, v, e->u.strval); return 1; } default: return tonumeral(e, v); @@ -561,7 +560,7 @@ static int addk (FuncState *fs, TValue *key, TValue *v) { /* ** Add a string to list of constants and return its index. */ -int luaK_stringK (FuncState *fs, TString *s) { +static int stringK (FuncState *fs, TString *s) { TValue o; setsvalue(fs->ls->L, &o, s); return addk(fs, &o, &o); /* use string itself as key */ @@ -656,7 +655,7 @@ static void luaK_float (FuncState *fs, int reg, lua_Number f) { /* ** Convert a constant in 'v' into an expression description 'e' */ -static void const2exp (FuncState *fs, TValue *v, expdesc *e) { +static void const2exp (TValue *v, expdesc *e) { switch (ttypetag(v)) { case LUA_TNUMINT: e->k = VKINT; e->u.ival = ivalue(v); @@ -671,7 +670,7 @@ static void const2exp (FuncState *fs, TValue *v, expdesc *e) { e->k = VNIL; break; case LUA_TSHRSTR: case LUA_TLNGSTR: - e->k = VK; e->u.info = luaK_stringK(fs, tsvalue(v)); + e->k = VKSTR; e->u.strval = tsvalue(v); break; default: lua_assert(0); } @@ -696,6 +695,16 @@ void luaK_setreturns (FuncState *fs, expdesc *e, int nresults) { } +/* +** Convert a VKSTR to a VK +*/ +static void str2K (FuncState *fs, expdesc *e) { + lua_assert(e->k == VKSTR); + e->u.info = stringK(fs, e->u.strval); + e->k = VK; +} + + /* ** Fix an expression to return one result. ** If expression is not a multi-ret expression (function call or @@ -728,7 +737,7 @@ void luaK_dischargevars (FuncState *fs, expdesc *e) { switch (e->k) { case VCONST: { TValue *val = &fs->ls->dyd->actvar.arr[e->u.info].k; - const2exp(fs, val, e); + const2exp(val, e); break; } case VLOCAL: { /* already in a register */ @@ -789,6 +798,9 @@ static void discharge2reg (FuncState *fs, expdesc *e, int reg) { luaK_codeABC(fs, OP_LOADBOOL, reg, e->k == VTRUE, 0); break; } + case VKSTR: { + str2K(fs, e); + } /* FALLTHROUGH */ case VK: { luaK_codek(fs, reg, e->u.info); break; @@ -949,6 +961,7 @@ static int luaK_exp2K (FuncState *fs, expdesc *e) { case VNIL: info = nilK(fs); break; case VKINT: info = luaK_intK(fs, e->u.ival); break; case VKFLT: info = luaK_numberK(fs, e->u.nval); break; + case VKSTR: info = stringK(fs, e->u.strval); break; case VK: info = e->u.info; break; default: return 0; /* not a constant */ } @@ -1083,7 +1096,7 @@ void luaK_goiftrue (FuncState *fs, expdesc *e) { pc = e->u.info; /* save jump position */ break; } - case VK: case VKFLT: case VKINT: case VTRUE: { + case VK: case VKFLT: case VKINT: case VKSTR: case VTRUE: { pc = NO_JUMP; /* always true; do nothing */ break; } @@ -1133,7 +1146,7 @@ static void codenot (FuncState *fs, expdesc *e) { e->k = VTRUE; /* true == not nil == not false */ break; } - case VK: case VKFLT: case VKINT: case VTRUE: { + case VK: case VKFLT: case VKINT: case VKSTR: case VTRUE: { e->k = VFALSE; /* false == not "x" == not 0.5 == not 1 == not true */ break; } @@ -1219,9 +1232,11 @@ static int isSCnumber (expdesc *e, lua_Integer *i, int *isfloat) { ** values in registers. */ void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) { + if (k->k == VKSTR) + str2K(fs, k); lua_assert(!hasjumps(t) && (t->k == VLOCAL || t->k == VNONRELOC || t->k == VUPVAL)); - if (t->k == VUPVAL && !isKstr(fs, k)) /* upvalue indexed by non string? */ + if (t->k == VUPVAL && !isKstr(fs, k)) /* upvalue indexed by non 'Kstr'? */ luaK_exp2anyreg(fs, t); /* put it in a register */ if (t->k == VUPVAL) { t->u.ind.t = t->u.info; /* upvalue index */ diff --git a/lcode.h b/lcode.h index 0c12bb648b..8cecd538f6 100644 --- a/lcode.h +++ b/lcode.h @@ -62,7 +62,6 @@ LUAI_FUNC void luaK_fixline (FuncState *fs, int line); LUAI_FUNC void luaK_nil (FuncState *fs, int from, int n); LUAI_FUNC void luaK_reserveregs (FuncState *fs, int n); LUAI_FUNC void luaK_checkstack (FuncState *fs, int n); -LUAI_FUNC int luaK_stringK (FuncState *fs, TString *s); LUAI_FUNC void luaK_int (FuncState *fs, int reg, lua_Integer n); LUAI_FUNC void luaK_dischargevars (FuncState *fs, expdesc *e); LUAI_FUNC int luaK_exp2anyreg (FuncState *fs, expdesc *e); diff --git a/lparser.c b/lparser.c index ea81000613..b70c609e21 100644 --- a/lparser.c +++ b/lparser.c @@ -156,13 +156,15 @@ static void init_exp (expdesc *e, expkind k, int i) { } -static void codestring (LexState *ls, expdesc *e, TString *s) { - init_exp(e, VK, luaK_stringK(ls->fs, s)); +static void codestring (expdesc *e, TString *s) { + e->f = e->t = NO_JUMP; + e->k = VKSTR; + e->u.strval = s; } static void codename (LexState *ls, expdesc *e) { - codestring(ls, e, str_checkname(ls)); + codestring(e, str_checkname(ls)); } @@ -445,7 +447,7 @@ static void singlevar (LexState *ls, expdesc *var) { expdesc key; singlevaraux(fs, ls->envn, var, 1); /* get environment variable */ lua_assert(var->k != VVOID); /* this one must exist */ - codestring(ls, &key, varname); /* key is variable name */ + codestring(&key, varname); /* key is variable name */ luaK_indexed(fs, var, &key); /* env[varname] */ } } @@ -1019,7 +1021,7 @@ static void funcargs (LexState *ls, expdesc *f, int line) { break; } case TK_STRING: { /* funcargs -> STRING */ - codestring(ls, &args, ls->t.seminfo.ts); + codestring(&args, ls->t.seminfo.ts); luaX_next(ls); /* must use 'seminfo' before 'next' */ break; } @@ -1127,7 +1129,7 @@ static void simpleexp (LexState *ls, expdesc *v) { break; } case TK_STRING: { - codestring(ls, v, ls->t.seminfo.ts); + codestring(v, ls->t.seminfo.ts); break; } case TK_NIL: { diff --git a/lparser.h b/lparser.h index d9b734bf8e..f528f0130c 100644 --- a/lparser.h +++ b/lparser.h @@ -30,7 +30,9 @@ typedef enum { VFALSE, /* constant false */ VK, /* constant in 'k'; info = index of constant in 'k' */ VKFLT, /* floating constant; nval = numerical float value */ - VKINT, /* integer constant; nval = numerical integer value */ + VKINT, /* integer constant; ival = numerical integer value */ + VKSTR, /* string constant; strval = TString address; + (string is fixed by the lexer) */ VNONRELOC, /* expression has its value in a fixed register; info = result register */ VLOCAL, /* local variable; var.ridx = local register; @@ -67,6 +69,7 @@ typedef struct expdesc { union { lua_Integer ival; /* for VKINT */ lua_Number nval; /* for VKFLT */ + TString *strval; /* for VKSTR */ int info; /* for generic use */ struct { /* for indexed variables */ short idx; /* index (R or "long" K) */ diff --git a/testes/code.lua b/testes/code.lua index b2702c61bc..b50914583d 100644 --- a/testes/code.lua +++ b/testes/code.lua @@ -409,5 +409,22 @@ checkequal(function () return 6 and true or nil end, function () return k6 and kTrue or kNil end) +do -- string constants + local function f1 () + local k = "00000000000000000000000000000000000000000000000000" + return function () + return function () return k end + end + end + + local f2 = f1() + local f3 = f2() + assert(f3() == string.rep("0", 50)) + checkK(f3, f3()) + -- string is not needed by other functions + assert(T.listk(f1)[1] == nil) + assert(T.listk(f2)[1] == nil) +end + print 'OK' From 8082906c059f2b1473de4363ca57fe19b52f281f Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 17 Jul 2019 14:50:42 -0300 Subject: [PATCH 0457/1145] Fixed small issue with constant propagation Constants directly assigned to other constants were not propagating: For instance, in local k1 = 10 local k2 = k1 'k2' were not treated as a compile-time constant. --- lcode.c | 18 +++++++++++++++--- testes/code.lua | 10 ++++++---- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/lcode.c b/lcode.c index 40efcff307..c2b5fc6d19 100644 --- a/lcode.c +++ b/lcode.c @@ -67,6 +67,15 @@ static int tonumeral (const expdesc *e, TValue *v) { } +/* +** Get the constant value from a constant expression +*/ +static TValue *const2val (FuncState *fs, const expdesc *e) { + lua_assert(e->k == VCONST); + return &fs->ls->dyd->actvar.arr[e->u.info].k; +} + + /* ** If expression is a constant, fills 'v' with its value ** and returns 1. Otherwise, returns 0. @@ -85,6 +94,10 @@ int luaK_exp2const (FuncState *fs, const expdesc *e, TValue *v) { setsvalue(fs->ls->L, v, e->u.strval); return 1; } + case VCONST: { + setobj(fs->ls->L, v, const2val(fs, e)); + return 1; + } default: return tonumeral(e, v); } } @@ -730,14 +743,13 @@ void luaK_setoneret (FuncState *fs, expdesc *e) { /* -** Ensure that expression 'e' is not a variable. +** Ensure that expression 'e' is not a variable (nor a constant). ** (Expression still may have jump lists.) */ void luaK_dischargevars (FuncState *fs, expdesc *e) { switch (e->k) { case VCONST: { - TValue *val = &fs->ls->dyd->actvar.arr[e->u.info].k; - const2exp(val, e); + const2exp(const2val(fs, e), e); break; } case VLOCAL: { /* already in a register */ diff --git a/testes/code.lua b/testes/code.lua index b50914583d..57923b1410 100644 --- a/testes/code.lua +++ b/testes/code.lua @@ -8,7 +8,8 @@ end print "testing code generation and optimizations" -- to test constant propagation -local k0 = 0 +local k0aux = 0 +local k0 = k0aux local k1 = 1 local k3 = 3 local k6 = k3 + (k3 << k0) @@ -410,8 +411,9 @@ checkequal(function () return 6 and true or nil end, do -- string constants + local k0 = "00000000000000000000000000000000000000000000000000" local function f1 () - local k = "00000000000000000000000000000000000000000000000000" + local k = k0 return function () return function () return k end end @@ -419,8 +421,8 @@ do -- string constants local f2 = f1() local f3 = f2() - assert(f3() == string.rep("0", 50)) - checkK(f3, f3()) + assert(f3() == k0) + checkK(f3, k0) -- string is not needed by other functions assert(T.listk(f1)[1] == nil) assert(T.listk(f2)[1] == nil) From 9c28ed05c95cb6854d917ac3e3ed7be9ae109480 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 17 Jul 2019 15:22:11 -0300 Subject: [PATCH 0458/1145] Calls 'luaF_close' in 'lua_settop' only when needed In 'lua_settop', avoid calling 'luaF_close' when increasing the stack or when the function has no to-be-closed variables. --- lapi.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/lapi.c b/lapi.c index 0f81107faf..0ea3dc0f22 100644 --- a/lapi.c +++ b/lapi.c @@ -170,12 +170,13 @@ LUA_API int lua_gettop (lua_State *L) { LUA_API void lua_settop (lua_State *L, int idx) { - StkId func = L->ci->func; - int diff; /* difference for new top */ + CallInfo *ci = L->ci; + StkId func = ci->func; + ptrdiff_t diff; /* difference for new top */ lua_lock(L); if (idx >= 0) { - api_check(L, idx <= L->ci->top - (func + 1), "new top too large"); - diff = (func + 1) + idx - L->top; + api_check(L, idx <= ci->top - (func + 1), "new top too large"); + diff = ((func + 1) + idx) - L->top; for (; diff > 0; diff--) setnilvalue(s2v(L->top++)); /* clear new slots */ } @@ -183,7 +184,8 @@ LUA_API void lua_settop (lua_State *L, int idx) { api_check(L, -(idx+1) <= (L->top - (func + 1)), "invalid new top"); diff = idx + 1; /* will "subtract" index (as it is negative) */ } - luaF_close(L, L->top + diff, LUA_OK); + if (diff < 0 && hastocloseCfunc(ci->nresults)) + luaF_close(L, L->top + diff, LUA_OK); L->top += diff; /* correct top only after closing any upvalue */ lua_unlock(L); } From 4eefef07ab1c136f901d816822c79336fa89336d Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 17 Jul 2019 16:00:24 -0300 Subject: [PATCH 0459/1145] 'math.randomseed()' returns the seeds it used A call to 'math.randomseed()' returns the two components of the seed it set, so that they can be used to set that same seed again. --- lmathlib.c | 9 +++++++-- manual/manual.of | 3 +++ testes/all.lua | 11 ++++++++--- testes/math.lua | 11 +++++++++-- 4 files changed, 27 insertions(+), 7 deletions(-) diff --git a/lmathlib.c b/lmathlib.c index f6f0b4265f..1d310b2da4 100644 --- a/lmathlib.c +++ b/lmathlib.c @@ -605,20 +605,24 @@ static void setseed (Rand64 *state, lua_Unsigned n1, lua_Unsigned n2) { static void randseed (lua_State *L, RanState *state) { lua_Unsigned seed1 = (lua_Unsigned)time(NULL); lua_Unsigned seed2 = (lua_Unsigned)(size_t)L; + lua_pushinteger(L, seed1); + lua_pushinteger(L, seed2); setseed(state->s, seed1, seed2); } static int math_randomseed (lua_State *L) { RanState *state = (RanState *)lua_touserdata(L, lua_upvalueindex(1)); - if (lua_isnone(L, 1)) + if (lua_isnone(L, 1)) { randseed(L, state); + return 2; /* return seeds */ + } else { lua_Integer n1 = luaL_checkinteger(L, 1); lua_Integer n2 = luaL_optinteger(L, 2, 0); setseed(state->s, n1, n2); + return 0; } - return 0; } @@ -635,6 +639,7 @@ static const luaL_Reg randfuncs[] = { static void setrandfunc (lua_State *L) { RanState *state = (RanState *)lua_newuserdatauv(L, sizeof(RanState), 0); randseed(L, state); /* initialize with a "random" seed */ + lua_pop(L, 2); /* remove pushed seeds */ luaL_setfuncs(L, randfuncs, 1); } diff --git a/manual/manual.of b/manual/manual.of index 3d2fb4fbaf..7f2596faf8 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -7798,6 +7798,9 @@ The default for @id{y} is zero. When called with no arguments, Lua generates a seed with a weak attempt for randomness. +In this case, +the call returns the two seed components that were used. + To ensure a required level of randomness to the initial state (or contrarily, to have a deterministic sequence, for instance when debugging a program), diff --git a/testes/all.lua b/testes/all.lua index 72121e8d08..bf27f10686 100644 --- a/testes/all.lua +++ b/testes/all.lua @@ -37,8 +37,6 @@ end -- tests should require debug when needed debug = nil -require"bwcoercion" - if usertests then T = nil -- no "internal" tests for user tests @@ -46,7 +44,6 @@ else T = rawget(_G, "T") -- avoid problems with 'strict' module end -math.randomseed(0) --[=[ example of a long [comment], @@ -54,6 +51,14 @@ math.randomseed(0) ]=] +print("\n\tStarting Tests") + +do + -- set random seed + local random_x, random_y = math.randomseed() + print(string.format("random seeds: %d, %d", random_x, random_y)) +end + print("current path:\n****" .. package.path .. "****\n") diff --git a/testes/math.lua b/testes/math.lua index befce12e30..0c297e7411 100644 --- a/testes/math.lua +++ b/testes/math.lua @@ -815,7 +815,7 @@ end -- low-level!! For the current implementation of random in Lua, -- the first call after seed 1007 should return 0x7a7040a5a323c9d6 do - -- all computations assume at most 32-bit integers + -- all computations should work with 32-bit integers local h = 0x7a7040a5 -- higher half local l = 0xa323c9d6 -- lower half @@ -840,7 +840,14 @@ do assert(rand * 2^floatbits == res) end -math.randomseed() +do + -- testing return of 'randomseed' + local x, y = math.randomseed() + local res = math.random(0) + math.randomseed(x, y) -- should repeat the state + assert(math.random(0) == res) + -- keep the random seed for following tests +end do -- test random for floats local randbits = math.min(floatbits, 64) -- at most 64 random bits From 024a6071cac749504e0b26a915bda4f52c41a892 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 18 Jul 2019 11:26:03 -0300 Subject: [PATCH 0460/1145] Small bug with stack reallocation OP_RETURN must update trap before updating stack. (Bug detected with -DHARDSTACKTESTS). Also, in 'luaF_close', do not create a variable with 'uplevel(uv)', as the stack may change and invalidate this value. (This is not a bug, but could become one if 'upl' was used again.) --- lfunc.c | 7 +++---- lvm.c | 9 ++++++--- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/lfunc.c b/lfunc.c index cd85cc1f0b..c07e9b35e9 100644 --- a/lfunc.c +++ b/lfunc.c @@ -202,13 +202,12 @@ void luaF_unlinkupval (UpVal *uv) { int luaF_close (lua_State *L, StkId level, int status) { UpVal *uv; while ((uv = L->openupval) != NULL && uplevel(uv) >= level) { - StkId upl = uplevel(uv); TValue *slot = &uv->u.value; /* new position for value */ - lua_assert(upl < L->top); + lua_assert(uplevel(uv) < L->top); if (uv->tt == LUA_TUPVALTBC && status != NOCLOSINGMETH) { - /* must run closing method */ + /* must run closing method, which may change the stack */ ptrdiff_t levelrel = savestack(L, level); - status = callclosemth(L, upl, status); /* may change the stack */ + status = callclosemth(L, uplevel(uv), status); level = restorestack(L, levelrel); } luaF_unlinkupval(uv); diff --git a/lvm.c b/lvm.c index 7e6f148d70..c1b6749da1 100644 --- a/lvm.c +++ b/lvm.c @@ -1574,8 +1574,10 @@ void luaV_execute (lua_State *L, CallInfo *ci) { savepc(ci); /* some calls here can raise errors */ if (TESTARG_k(i)) { /* close upvalues from current call; the compiler ensures - that there are no to-be-closed variables here */ + that there are no to-be-closed variables here, so this + call cannot change the stack */ luaF_close(L, base, NOCLOSINGMETH); + lua_assert(base == ci->func + 1); } if (!ttisfunction(s2v(ra))) { /* not a function? */ luaD_tryfuncTM(L, ra); /* try '__call' metamethod */ @@ -1602,10 +1604,11 @@ void luaV_execute (lua_State *L, CallInfo *ci) { if (n < 0) /* not fixed? */ n = cast_int(L->top - ra); /* get what is available */ savepc(ci); - if (TESTARG_k(i)) { + if (TESTARG_k(i)) { /* may there be open upvalues? */ if (L->top < ci->top) L->top = ci->top; - luaF_close(L, base, LUA_OK); /* there may be open upvalues */ + luaF_close(L, base, LUA_OK); + updatetrap(ci); updatestack(ci); } if (nparams1) /* vararg function? */ From d36a31e6739bcd39c84f637344227af87cfd0ee5 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 18 Jul 2019 14:58:15 -0300 Subject: [PATCH 0461/1145] Reviving HARDMEMTESTS This commit brings a new implementation for HARDMEMTESTS, which forces an emergency GC whenever possible. It also fixes some issues detected with this option: - A small bug in lvm.c: a closure could be collected by an emergency GC while being initialized. - Some tests: a memory address can be immediatly reused after a GC; for instance, two consecutive '{}' expressions can return exactly the same address, if the first one is not anchored. --- lmem.c | 23 ++++++++++++++++------- lvm.c | 9 ++++++--- testes/api.lua | 7 +++++-- testes/strings.lua | 11 ++++++++--- 4 files changed, 35 insertions(+), 15 deletions(-) diff --git a/lmem.c b/lmem.c index 53f8dcb9d6..0186a86b4a 100644 --- a/lmem.c +++ b/lmem.c @@ -23,14 +23,25 @@ #if defined(HARDMEMTESTS) -#define hardtest(L,os,s) /* force a GC whenever possible */ \ - if ((s) > (os) && (G(L))->gcrunning) luaC_fullgc(L, 1); +/* +** First allocation will fail whenever not building initial state +** and not shrinking a block. (This fail will trigger 'tryagain' and +** a full GC cycle at every alocation.) +*/ +static void *firsttry (global_State *g, void *block, size_t os, size_t ns) { + if (ttisnil(&g->nilvalue) && ns > os) + return NULL; /* fail */ + else /* normal allocation */ + return (*g->frealloc)(g->ud, block, os, ns); +} #else -#define hardtest(L,os,s) ((void)0) +#define firsttry(g,block,os,ns) ((*g->frealloc)(g->ud, block, os, ns)) #endif + + /* ** About the realloc function: ** void * frealloc (void *ud, void *ptr, size_t osize, size_t nsize); @@ -138,8 +149,7 @@ void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) { void *newblock; global_State *g = G(L); lua_assert((osize == 0) == (block == NULL)); - hardtest(L, osize, nsize); - newblock = (*g->frealloc)(g->ud, block, osize, nsize); + newblock = firsttry(g, block, osize, nsize); if (unlikely(newblock == NULL && nsize > 0)) { if (nsize > osize) /* not shrinking a block? */ newblock = tryagain(L, block, osize, nsize); @@ -162,12 +172,11 @@ void *luaM_saferealloc_ (lua_State *L, void *block, size_t osize, void *luaM_malloc_ (lua_State *L, size_t size, int tag) { - hardtest(L, 0, size); if (size == 0) return NULL; /* that's all */ else { global_State *g = G(L); - void *newblock = (*g->frealloc)(g->ud, NULL, tag, size); + void *newblock = firsttry(g, NULL, tag, size); if (unlikely(newblock == NULL)) { newblock = tryagain(L, NULL, tag, size); if (newblock == NULL) diff --git a/lvm.c b/lvm.c index c1b6749da1..d9bd0ab323 100644 --- a/lvm.c +++ b/lvm.c @@ -1038,7 +1038,10 @@ void luaV_finishOp (lua_State *L) { ** errors. (That is, it will not return to the interpreter main loop ** after changing the stack or hooks.) */ -#define halfProtect(exp) (savepc(L), (exp)) +#define halfProtect(exp) (savestate(L,ci), (exp)) + +/* idem, but without changing the stack */ +#define halfProtectNT(exp) (savepc(L), (exp)) #define checkGC(L,c) \ @@ -1620,7 +1623,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmcase(OP_RETURN0) { if (L->hookmask) { L->top = ra; - halfProtect(luaD_poscall(L, ci, 0)); /* no hurry... */ + halfProtectNT(luaD_poscall(L, ci, 0)); /* no hurry... */ } else { /* do the 'poscall' here */ int nres = ci->nresults; @@ -1634,7 +1637,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmcase(OP_RETURN1) { if (L->hookmask) { L->top = ra + 1; - halfProtect(luaD_poscall(L, ci, 1)); /* no hurry... */ + halfProtectNT(luaD_poscall(L, ci, 1)); /* no hurry... */ } else { /* do the 'poscall' here */ int nres = ci->nresults; diff --git a/testes/api.lua b/testes/api.lua index 8f4e89ac86..5da036414b 100644 --- a/testes/api.lua +++ b/testes/api.lua @@ -354,8 +354,11 @@ assert(to("topointer", nil) == null) assert(to("topointer", "abc") ~= null) assert(to("topointer", string.rep("x", 10)) == to("topointer", string.rep("x", 10))) -- short strings -assert(to("topointer", string.rep("x", 300)) ~= - to("topointer", string.rep("x", 300))) -- long strings +do -- long strings + local s1 = string.rep("x", 300) + local s2 = string.rep("x", 300) + assert(to("topointer", s1) ~= to("topointer", s2)) +end assert(to("topointer", T.pushuserdata(20)) ~= null) assert(to("topointer", io.read) ~= null) -- light C function assert(to("topointer", hfunc) ~= null) -- "heavy" C function diff --git a/testes/strings.lua b/testes/strings.lua index 1b2b570e0e..2540fdefc0 100644 --- a/testes/strings.lua +++ b/testes/strings.lua @@ -163,11 +163,16 @@ do -- tests for '%p' format assert(string.format("%p", 4) == null) assert(string.format("%p", print) ~= null) assert(string.format("%p", coroutine.running()) ~= null) - assert(string.format("%p", {}) ~= string.format("%p", {})) + do + local t1 = {}; local t2 = {} + assert(string.format("%p", t1) ~= string.format("%p", t2)) + end assert(string.format("%p", string.rep("a", 10)) == string.format("%p", string.rep("a", 10))) -- short strings - assert(string.format("%p", string.rep("a", 300)) ~= - string.format("%p", string.rep("a", 300))) -- long strings + do -- long strings + local s1 = string.rep("a", 300); local s2 = string.rep("a", 300) + assert(string.format("%p", s1) ~= string.format("%p", s2)) + end assert(#string.format("%90p", {}) == 90) end From 3c1d415bd3fef686b27f853bdf3eaf1f0a9bb0be Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 18 Jul 2019 15:31:22 -0300 Subject: [PATCH 0462/1145] Details - Macro 'checkliveness' (for debug) always uses 'L', to avoid warnings. - Some old 'while' changed to 'for' in 'testes/gc.lua'. - In 'testes/libs/makefile', do not make files depend on 'ltests.h', which may not even exist. --- lobject.h | 10 +++++----- testes/gc.lua | 22 +++++++--------------- testes/libs/makefile | 10 +++++----- 3 files changed, 17 insertions(+), 25 deletions(-) diff --git a/lobject.h b/lobject.h index 95f8e1881f..f21e8a9176 100644 --- a/lobject.h +++ b/lobject.h @@ -89,8 +89,8 @@ typedef struct TValue { #define righttt(obj) (ttypetag(obj) == gcvalue(obj)->tt) #define checkliveness(L,obj) \ - lua_longassert(!iscollectable(obj) || \ - (righttt(obj) && (L == NULL || !isdead(G(L),gcvalue(obj))))) + ((void)L, lua_longassert(!iscollectable(obj) || \ + (righttt(obj) && (L == NULL || !isdead(G(L),gcvalue(obj)))))) /* Macros to set values */ @@ -100,7 +100,7 @@ typedef struct TValue { #define setobj(L,obj1,obj2) \ { TValue *io1=(obj1); const TValue *io2=(obj2); \ io1->value_ = io2->value_; io1->tt_ = io2->tt_; \ - (void)L; checkliveness(L,io1); lua_assert(!isreallyempty(io1)); } + checkliveness(L,io1); lua_assert(!isreallyempty(io1)); } /* ** different types of assignments, according to destination @@ -651,14 +651,14 @@ typedef union Node { #define setnodekey(L,node,obj) \ { Node *n_=(node); const TValue *io_=(obj); \ n_->u.key_val = io_->value_; n_->u.key_tt = io_->tt_; \ - (void)L; checkliveness(L,io_); } + checkliveness(L,io_); } /* copy a value from a key */ #define getnodekey(L,obj,node) \ { TValue *io_=(obj); const Node *n_=(node); \ io_->value_ = n_->u.key_val; io_->tt_ = n_->u.key_tt; \ - (void)L; checkliveness(L,io_); } + checkliveness(L,io_); } /* diff --git a/testes/gc.lua b/testes/gc.lua index 6d24e0d82d..9ea054c1ad 100644 --- a/testes/gc.lua +++ b/testes/gc.lua @@ -99,35 +99,28 @@ local function GC() GC1(); GC2() end do print("creating many objects") - local contCreate = 0 - local limit = 5000 - while contCreate <= limit do + for i = 1, limit do local a = {}; a = nil - contCreate = contCreate+1 end local a = "a" - contCreate = 0 - while contCreate <= limit do - a = contCreate .. "b"; + for i = 1, limit do + a = i .. "b"; a = string.gsub(a, '(%d%d*)', "%1 %1") a = "a" - contCreate = contCreate+1 end - contCreate = 0 a = {} function a:test () - while contCreate <= limit do - load(string.format("function temp(a) return 'a%d' end", contCreate), "")() - assert(temp() == string.format('a%d', contCreate)) - contCreate = contCreate+1 + for i = 1, limit do + load(string.format("function temp(a) return 'a%d' end", i), "")() + assert(temp() == string.format('a%d', i)) end end @@ -166,9 +159,8 @@ print('long strings') x = "01234567890123456789012345678901234567890123456789012345678901234567890123456789" assert(string.len(x)==80) s = '' -n = 0 k = math.min(300, (math.maxinteger // 80) // 2) -while n < k do s = s..x; n=n+1; j=tostring(n) end +for n = 1, k do s = s..x; j=tostring(n) end assert(string.len(s) == k*80) s = string.sub(s, 1, 10000) s, i = string.gsub(s, '(%d%d%d%d)', '') diff --git a/testes/libs/makefile b/testes/libs/makefile index 698f8984f5..a133092084 100644 --- a/testes/libs/makefile +++ b/testes/libs/makefile @@ -11,17 +11,17 @@ CFLAGS = -Wall -std=gnu99 -O2 -I$(LUA_DIR) -fPIC -shared all: lib1.so lib11.so lib2.so lib21.so lib2-v2.so touch all -lib1.so: lib1.c $(LUA_DIR)/luaconf.h $(LUA_DIR)/ltests.h +lib1.so: lib1.c $(LUA_DIR)/luaconf.h $(CC) $(CFLAGS) -o lib1.so lib1.c -lib11.so: lib11.c $(LUA_DIR)/luaconf.h $(LUA_DIR)/ltests.h +lib11.so: lib11.c $(LUA_DIR)/luaconf.h $(CC) $(CFLAGS) -o lib11.so lib11.c -lib2.so: lib2.c $(LUA_DIR)/luaconf.h $(LUA_DIR)/ltests.h +lib2.so: lib2.c $(LUA_DIR)/luaconf.h $(CC) $(CFLAGS) -o lib2.so lib2.c -lib21.so: lib21.c $(LUA_DIR)/luaconf.h $(LUA_DIR)/ltests.h +lib21.so: lib21.c $(LUA_DIR)/luaconf.h $(CC) $(CFLAGS) -o lib21.so lib21.c -lib2-v2.so: lib21.c $(LUA_DIR)/luaconf.h $(LUA_DIR)/ltests.h +lib2-v2.so: lib21.c $(LUA_DIR)/luaconf.h $(CC) $(CFLAGS) -o lib2-v2.so lib22.c From 9cdf6b7082c49e6bf7daf8c7c4c649bcaacf9fad Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 19 Jul 2019 09:43:35 -0300 Subject: [PATCH 0463/1145] Some details in 'lmem.c' and 'lgc.c' - Several new comments in 'lmem.c'. - Both 'luaM_growaux_' and 'luaM_shrinkvector_' use 'luaM_saferealloc_' to check for errors. Moreover, the use of 'luaM_saferealloc_' makes 'luaM_shrinkvector_' try again if shrink fails (which can happen now). - In 'checkSizes', save old debt only when needed. --- lgc.c | 7 ++++--- lmem.c | 62 +++++++++++++++++++++++++++++++++++----------------------- 2 files changed, 42 insertions(+), 27 deletions(-) diff --git a/lgc.c b/lgc.c index aa6921bcb2..6562c92817 100644 --- a/lgc.c +++ b/lgc.c @@ -794,10 +794,11 @@ static GCObject **sweeptolive (lua_State *L, GCObject **p) { */ static void checkSizes (lua_State *L, global_State *g) { if (!g->gcemergency) { - l_mem olddebt = g->GCdebt; - if (g->strt.nuse < g->strt.size / 4) /* string table too big? */ + if (g->strt.nuse < g->strt.size / 4) { /* string table too big? */ + l_mem olddebt = g->GCdebt; luaS_resize(L, g->strt.size / 2); - g->GCestimate += g->GCdebt - olddebt; /* correct estimate */ + g->GCestimate += g->GCdebt - olddebt; /* correct estimate */ + } } } diff --git a/lmem.c b/lmem.c index 0186a86b4a..b1d646a58c 100644 --- a/lmem.c +++ b/lmem.c @@ -44,23 +44,35 @@ static void *firsttry (global_State *g, void *block, size_t os, size_t ns) { /* ** About the realloc function: -** void * frealloc (void *ud, void *ptr, size_t osize, size_t nsize); +** void *frealloc (void *ud, void *ptr, size_t osize, size_t nsize); ** ('osize' is the old size, 'nsize' is the new size) ** -** * frealloc(ud, NULL, x, s) creates a new block of size 's' (no -** matter 'x'). +** - frealloc(ud, p, x, 0) frees the block 'p' and returns NULL. +** Particularly, frealloc(ud, NULL, 0, 0) does nothing, +** which is equivalent to free(NULL) in ISO C. ** -** * frealloc(ud, p, x, 0) frees the block 'p' -** (in this specific case, frealloc must return NULL); -** particularly, frealloc(ud, NULL, 0, 0) does nothing -** (which is equivalent to free(NULL) in ISO C) +** - frealloc(ud, NULL, x, s) creates a new block of size 's' +** (no matter 'x'). Returns NULL if it cannot create the new block. ** -** frealloc returns NULL if it cannot create or reallocate the area -** (any reallocation to an equal or smaller size cannot fail!) +** - otherwise, frealloc(ud, b, x, y) reallocates the block 'b' from +** size 'x' to size 'y'. Returns NULL if it cannot reallocate the +** block to the new size. */ + +/* +** {================================================================== +** Functions to allocate/deallocate arrays for the Parser +** =================================================================== +*/ + +/* +** Minimum size for arrays during parsing, to avoid overhead of +** reallocating to size 1, then 2, and then 4. All these arrays +** will be reallocated to exact sizes or erased when parsing ends. +*/ #define MINSIZEARRAY 4 @@ -82,32 +94,32 @@ void *luaM_growaux_ (lua_State *L, void *block, int nelems, int *psize, } lua_assert(nelems + 1 <= size && size <= limit); /* 'limit' ensures that multiplication will not overflow */ - newblock = luaM_realloc_(L, block, cast_sizet(*psize) * size_elems, - cast_sizet(size) * size_elems); - if (unlikely(newblock == NULL)) - luaM_error(L); + newblock = luaM_saferealloc_(L, block, cast_sizet(*psize) * size_elems, + cast_sizet(size) * size_elems); *psize = size; /* update only when everything else is OK */ return newblock; } +/* +** In prototypes, the size of the array is also its number of +** elements (to save memory). So, if it cannot shrink an array +** to its number of elements, the only option is to raise an +** error. +*/ void *luaM_shrinkvector_ (lua_State *L, void *block, int *size, int final_n, int size_elem) { - global_State *g = G(L); void *newblock; size_t oldsize = cast_sizet((*size) * size_elem); size_t newsize = cast_sizet(final_n * size_elem); lua_assert(newsize <= oldsize); - newblock = (*g->frealloc)(g->ud, block, oldsize, newsize); - if (unlikely(newblock == NULL && final_n > 0)) /* allocation failed? */ - luaM_error(L); - else { - g->GCdebt += newsize - oldsize; - *size = final_n; - return newblock; - } + newblock = luaM_saferealloc_(L, block, oldsize, newsize); + *size = final_n; + return newblock; } +/* }================================================================== */ + l_noret luaM_toobig (lua_State *L) { luaG_runerror(L, "memory allocation error: block too big"); @@ -143,7 +155,9 @@ static void *tryagain (lua_State *L, void *block, /* -** generic allocation routine. +** Generic allocation routine. +** If allocation fails while shrinking a block, do not try again; the +** GC shrinks some blocks and it is not reentrant. */ void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) { void *newblock; @@ -154,7 +168,7 @@ void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) { if (nsize > osize) /* not shrinking a block? */ newblock = tryagain(L, block, osize, nsize); if (newblock == NULL) /* still no memory? */ - return NULL; + return NULL; /* do not update 'GCdebt' */ } lua_assert((nsize == 0) == (newblock == NULL)); g->GCdebt = (g->GCdebt + nsize) - osize; From dc07719b0dbc4f2df0f42e34e18be1e0ac4fa2c3 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 19 Jul 2019 11:12:31 -0300 Subject: [PATCH 0464/1145] Tag LUA_TUPVALTBC replaced by a flag It is simpler to signal a to-be-closed upvalue with a boolean flag, instead of using a different tag. --- lfunc.c | 15 ++++++++------- lgc.c | 6 ++---- lobject.h | 4 +--- lstate.h | 3 +-- ltests.c | 3 +-- 5 files changed, 13 insertions(+), 18 deletions(-) diff --git a/lfunc.c b/lfunc.c index c07e9b35e9..9f91ad4f51 100644 --- a/lfunc.c +++ b/lfunc.c @@ -59,14 +59,15 @@ void luaF_initupvals (lua_State *L, LClosure *cl) { /* -** Create a new upvalue with the given tag at the given level, -** and link it to the list of open upvalues of 'L' after entry 'prev'. +** Create a new upvalue at the given level, and link it to the list of +** open upvalues of 'L' after entry 'prev'. **/ -static UpVal *newupval (lua_State *L, int tag, StkId level, UpVal **prev) { - GCObject *o = luaC_newobj(L, tag, sizeof(UpVal)); +static UpVal *newupval (lua_State *L, int tbc, StkId level, UpVal **prev) { + GCObject *o = luaC_newobj(L, LUA_TUPVAL, sizeof(UpVal)); UpVal *uv = gco2upv(o); UpVal *next = *prev; uv->v = s2v(level); /* current value lives in the stack */ + uv->tbc = tbc; uv->u.open.next = next; /* link it to list of open upvalues */ uv->u.open.previous = prev; if (next) @@ -94,7 +95,7 @@ UpVal *luaF_findupval (lua_State *L, StkId level) { pp = &p->u.open.next; } /* not found: create a new upvalue after 'pp' */ - return newupval(L, LUA_TUPVAL, level, pp); + return newupval(L, 0, level, pp); } @@ -170,7 +171,7 @@ static int callclosemth (lua_State *L, StkId level, int status) { static void trynewtbcupval (lua_State *L, void *ud) { StkId level = cast(StkId, ud); lua_assert(L->openupval == NULL || uplevel(L->openupval) < level); - newupval(L, LUA_TUPVALTBC, level, &L->openupval); + newupval(L, 1, level, &L->openupval); } @@ -204,7 +205,7 @@ int luaF_close (lua_State *L, StkId level, int status) { while ((uv = L->openupval) != NULL && uplevel(uv) >= level) { TValue *slot = &uv->u.value; /* new position for value */ lua_assert(uplevel(uv) < L->top); - if (uv->tt == LUA_TUPVALTBC && status != NOCLOSINGMETH) { + if (uv->tbc && status != NOCLOSINGMETH) { /* must run closing method, which may change the stack */ ptrdiff_t levelrel = savestack(L, level); status = callclosemth(L, uplevel(uv), status); diff --git a/lgc.c b/lgc.c index 6562c92817..c5babfed68 100644 --- a/lgc.c +++ b/lgc.c @@ -273,8 +273,7 @@ static void reallymarkobject (global_State *g, GCObject *o) { gray2black(o); break; } - case LUA_TUPVAL: - case LUA_TUPVALTBC: { + case LUA_TUPVAL: { UpVal *uv = gco2upv(o); if (!upisopen(uv)) /* open upvalues are kept gray */ gray2black(o); @@ -571,7 +570,7 @@ static int traversethread (global_State *g, lua_State *th) { for (; o < th->top; o++) /* mark live elements in the stack */ markvalue(g, s2v(o)); for (uv = th->openupval; uv != NULL; uv = uv->u.open.next) { - if (uv->tt == LUA_TUPVALTBC) /* to be closed? */ + if (uv->tbc) /* to be closed? */ markobject(g, uv); /* cannot be collected */ } if (g->gcstate == GCSatomic) { /* final traversal? */ @@ -706,7 +705,6 @@ static void freeobj (lua_State *L, GCObject *o) { luaF_freeproto(L, gco2p(o)); break; case LUA_TUPVAL: - case LUA_TUPVALTBC: freeupval(L, gco2upv(o)); break; case LUA_TLCL: diff --git a/lobject.h b/lobject.h index f21e8a9176..a22148c087 100644 --- a/lobject.h +++ b/lobject.h @@ -568,6 +568,7 @@ typedef struct Proto { */ typedef struct UpVal { CommonHeader; + lu_byte tbc; /* true if it represents a to-be-closed variable */ TValue *v; /* points to stack or to its own value */ union { struct { /* (when open) */ @@ -579,9 +580,6 @@ typedef struct UpVal { } UpVal; -/* variant for "To Be Closed" upvalues */ -#define LUA_TUPVALTBC (LUA_TUPVAL | (1 << 4)) - #define ClosureHeader \ CommonHeader; lu_byte nupvalues; GCObject *gclist diff --git a/lstate.h b/lstate.h index 2a95dd163c..03448b82bc 100644 --- a/lstate.h +++ b/lstate.h @@ -335,8 +335,7 @@ union GCUnion { #define gco2t(o) check_exp((o)->tt == LUA_TTABLE, &((cast_u(o))->h)) #define gco2p(o) check_exp((o)->tt == LUA_TPROTO, &((cast_u(o))->p)) #define gco2th(o) check_exp((o)->tt == LUA_TTHREAD, &((cast_u(o))->th)) -#define gco2upv(o) \ - check_exp(novariant((o)->tt) == LUA_TUPVAL, &((cast_u(o))->upv)) +#define gco2upv(o) check_exp((o)->tt == LUA_TUPVAL, &((cast_u(o))->upv)) /* diff --git a/ltests.c b/ltests.c index cb8c422a8e..09876ee7e6 100644 --- a/ltests.c +++ b/ltests.c @@ -397,8 +397,7 @@ static void checkrefs (global_State *g, GCObject *o) { checkudata(g, gco2u(o)); break; } - case LUA_TUPVAL: - case LUA_TUPVALTBC: { + case LUA_TUPVAL: { checkvalref(g, o, gco2upv(o)->v); break; } From 440a5ee78c8592230780310c9c8d8c2ba1700cb1 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 19 Jul 2019 12:13:00 -0300 Subject: [PATCH 0465/1145] Fixed bug for emergency collection in upvalue creation When creating an upvalue, an emergency collection can collect the previous upvalue where the new one would be linked. The following code can trigger the bug, using valgrind on Lua compiled with the -DHARDMEMTESTS option: local x; local y (function () return y end)(); (function () return x end)() --- lfunc.c | 14 ++++++++------ lfunc.h | 2 +- lvm.c | 2 +- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/lfunc.c b/lfunc.c index 9f91ad4f51..f7edf56b11 100644 --- a/lfunc.c +++ b/lfunc.c @@ -82,20 +82,22 @@ static UpVal *newupval (lua_State *L, int tbc, StkId level, UpVal **prev) { /* -** Find and reuse, or create if it does not exist, a regular upvalue -** at the given level. +** Find and reuse, or create if it does not exist, an upvalue +** at the given level and set it to the given slot. */ -UpVal *luaF_findupval (lua_State *L, StkId level) { +void luaF_setupval (lua_State *L, StkId level, UpVal **slot) { UpVal **pp = &L->openupval; UpVal *p; lua_assert(isintwups(L) || L->openupval == NULL); while ((p = *pp) != NULL && uplevel(p) >= level) { /* search for it */ + *slot = p; if (uplevel(p) == level && !isdead(G(L), p)) /* corresponding upvalue? */ - return p; /* return it */ + return; /* found it */ pp = &p->u.open.next; } - /* not found: create a new upvalue after 'pp' */ - return newupval(L, 0, level, pp); + /* not found: create a new upvalue after 'pp' (which is + anchored in 'slot', in case of an emergency collection) */ + *slot = newupval(L, 0, level, pp); } diff --git a/lfunc.h b/lfunc.h index 0ed79c48ab..72fc913ab7 100644 --- a/lfunc.h +++ b/lfunc.h @@ -57,7 +57,7 @@ LUAI_FUNC Proto *luaF_newproto (lua_State *L); LUAI_FUNC CClosure *luaF_newCclosure (lua_State *L, int nelems); LUAI_FUNC LClosure *luaF_newLclosure (lua_State *L, int nelems); LUAI_FUNC void luaF_initupvals (lua_State *L, LClosure *cl); -LUAI_FUNC UpVal *luaF_findupval (lua_State *L, StkId level); +LUAI_FUNC void luaF_setupval (lua_State *L, StkId level, UpVal **slot); LUAI_FUNC void luaF_newtbcupval (lua_State *L, StkId level); LUAI_FUNC int luaF_close (lua_State *L, StkId level, int status); LUAI_FUNC void luaF_unlinkupval (UpVal *uv); diff --git a/lvm.c b/lvm.c index d9bd0ab323..d3e05199e7 100644 --- a/lvm.c +++ b/lvm.c @@ -697,7 +697,7 @@ static void pushclosure (lua_State *L, Proto *p, UpVal **encup, StkId base, setclLvalue2s(L, ra, ncl); /* anchor new closure in stack */ for (i = 0; i < nup; i++) { /* fill in its upvalues */ if (uv[i].instack) /* upvalue refers to local variable? */ - ncl->upvals[i] = luaF_findupval(L, base + uv[i].idx); + luaF_setupval(L, base + uv[i].idx, &ncl->upvals[i]); else /* get upvalue from enclosing function */ ncl->upvals[i] = encup[uv[i].idx]; luaC_objbarrier(L, ncl, ncl->upvals[i]); From 3c0d3c6fbeea18f257102c62a01b036c7a5c5161 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 19 Jul 2019 13:14:06 -0300 Subject: [PATCH 0466/1145] Avoid using addresses of static variables as unique keys The addresses of static variables may be different for different instances of Lua, making these instances incompatible if they use these addresses as unique keys in the registry (or other tables). --- ldblib.c | 18 ++++++++---------- loadlib.c | 11 +++++------ testes/db.lua | 4 ++++ 3 files changed, 17 insertions(+), 16 deletions(-) diff --git a/ldblib.c b/ldblib.c index 513a13cb6e..641583955e 100644 --- a/ldblib.c +++ b/ldblib.c @@ -21,10 +21,10 @@ /* -** The hook table at registry[&HOOKKEY] maps threads to their current -** hook function. (We only need the unique address of 'HOOKKEY'.) +** The hook table at registry[HOOKKEY] maps threads to their current +** hook function. */ -static const int HOOKKEY = 0; +static const char* HOOKKEY = "_HOOKKEY"; /* @@ -314,7 +314,7 @@ static int db_upvaluejoin (lua_State *L) { static void hookf (lua_State *L, lua_Debug *ar) { static const char *const hooknames[] = {"call", "return", "line", "count", "tail call"}; - lua_rawgetp(L, LUA_REGISTRYINDEX, &HOOKKEY); + lua_getfield(L, LUA_REGISTRYINDEX, HOOKKEY); lua_pushthread(L); if (lua_rawget(L, -2) == LUA_TFUNCTION) { /* is there a hook function? */ lua_pushstring(L, hooknames[(int)ar->event]); /* push event name */ @@ -367,14 +367,12 @@ static int db_sethook (lua_State *L) { count = (int)luaL_optinteger(L, arg + 3, 0); func = hookf; mask = makemask(smask, count); } - if (lua_rawgetp(L, LUA_REGISTRYINDEX, &HOOKKEY) == LUA_TNIL) { - lua_createtable(L, 0, 2); /* create a hook table */ - lua_pushvalue(L, -1); - lua_rawsetp(L, LUA_REGISTRYINDEX, &HOOKKEY); /* set it in position */ + if (!luaL_getsubtable(L, LUA_REGISTRYINDEX, HOOKKEY)) { + /* table just created; initialize it */ lua_pushstring(L, "k"); lua_setfield(L, -2, "__mode"); /** hooktable.__mode = "k" */ lua_pushvalue(L, -1); - lua_setmetatable(L, -2); /* setmetatable(hooktable) = hooktable */ + lua_setmetatable(L, -2); /* metatable(hooktable) = hooktable */ } checkstack(L, L1, 1); lua_pushthread(L1); lua_xmove(L1, L, 1); /* key (thread) */ @@ -396,7 +394,7 @@ static int db_gethook (lua_State *L) { else if (hook != hookf) /* external hook? */ lua_pushliteral(L, "external hook"); else { /* hook table must exist */ - lua_rawgetp(L, LUA_REGISTRYINDEX, &HOOKKEY); + lua_getfield(L, LUA_REGISTRYINDEX, HOOKKEY); checkstack(L, L1, 1); lua_pushthread(L1); lua_xmove(L1, L, 1); lua_rawget(L, -2); /* 1st result = hooktable[L1] */ diff --git a/loadlib.c b/loadlib.c index b72dd88537..9ef0027866 100644 --- a/loadlib.c +++ b/loadlib.c @@ -56,10 +56,10 @@ /* -** unique key for table in the registry that keeps handles +** key for table in the registry that keeps handles ** for all loaded C libraries */ -static const int CLIBS = 0; +static const char *CLIBS = "_CLIBS"; #define LIB_FAIL "open" @@ -327,7 +327,7 @@ static void setpath (lua_State *L, const char *fieldname, */ static void *checkclib (lua_State *L, const char *path) { void *plib; - lua_rawgetp(L, LUA_REGISTRYINDEX, &CLIBS); + lua_getfield(L, LUA_REGISTRYINDEX, CLIBS); lua_getfield(L, -1, path); plib = lua_touserdata(L, -1); /* plib = CLIBS[path] */ lua_pop(L, 2); /* pop CLIBS table and 'plib' */ @@ -340,7 +340,7 @@ static void *checkclib (lua_State *L, const char *path) { ** registry.CLIBS[#CLIBS + 1] = plib -- also keep a list of all libraries */ static void addtoclib (lua_State *L, const char *path, void *plib) { - lua_rawgetp(L, LUA_REGISTRYINDEX, &CLIBS); + lua_getfield(L, LUA_REGISTRYINDEX, CLIBS); lua_pushlightuserdata(L, plib); lua_pushvalue(L, -1); lua_setfield(L, -3, path); /* CLIBS[path] = plib */ @@ -716,12 +716,11 @@ static void createsearcherstable (lua_State *L) { ** setting a finalizer to close all libraries when closing state. */ static void createclibstable (lua_State *L) { - lua_newtable(L); /* create CLIBS table */ + luaL_getsubtable(L, LUA_REGISTRYINDEX, CLIBS); /* create CLIBS table */ lua_createtable(L, 0, 1); /* create metatable for CLIBS */ lua_pushcfunction(L, gctm); lua_setfield(L, -2, "__gc"); /* set finalizer for CLIBS table */ lua_setmetatable(L, -2); - lua_rawsetp(L, LUA_REGISTRYINDEX, &CLIBS); /* set CLIBS table in registry */ } diff --git a/testes/db.lua b/testes/db.lua index 3d94f77612..a64a1130b2 100644 --- a/testes/db.lua +++ b/testes/db.lua @@ -255,6 +255,10 @@ do -- test hook presence in debug info end +-- hook table has weak keys +assert(getmetatable(debug.getregistry()._HOOKKEY).__mode == 'k') + + a = {}; L = nil local glob = 1 local oldglob = glob From 2f22c6bb79d209a55b3fc8e0b2d9c9f89f038174 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 19 Jul 2019 13:31:53 -0300 Subject: [PATCH 0467/1145] 'math.randomseed' always returns the two seed components --- lmathlib.c | 14 +++++++------- manual/manual.of | 6 ++++-- testes/math.lua | 6 ++++-- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/lmathlib.c b/lmathlib.c index 1d310b2da4..752647e71b 100644 --- a/lmathlib.c +++ b/lmathlib.c @@ -586,7 +586,8 @@ static int math_random (lua_State *L) { } -static void setseed (Rand64 *state, lua_Unsigned n1, lua_Unsigned n2) { +static void setseed (lua_State *L, Rand64 *state, + lua_Unsigned n1, lua_Unsigned n2) { int i; state[0] = Int2I(n1); state[1] = Int2I(0xff); /* avoid a zero state */ @@ -594,6 +595,8 @@ static void setseed (Rand64 *state, lua_Unsigned n1, lua_Unsigned n2) { state[3] = Int2I(0); for (i = 0; i < 16; i++) nextrand(state); /* discard initial values to "spread" seed */ + lua_pushinteger(L, n1); + lua_pushinteger(L, n2); } @@ -605,9 +608,7 @@ static void setseed (Rand64 *state, lua_Unsigned n1, lua_Unsigned n2) { static void randseed (lua_State *L, RanState *state) { lua_Unsigned seed1 = (lua_Unsigned)time(NULL); lua_Unsigned seed2 = (lua_Unsigned)(size_t)L; - lua_pushinteger(L, seed1); - lua_pushinteger(L, seed2); - setseed(state->s, seed1, seed2); + setseed(L, state->s, seed1, seed2); } @@ -615,14 +616,13 @@ static int math_randomseed (lua_State *L) { RanState *state = (RanState *)lua_touserdata(L, lua_upvalueindex(1)); if (lua_isnone(L, 1)) { randseed(L, state); - return 2; /* return seeds */ } else { lua_Integer n1 = luaL_checkinteger(L, 1); lua_Integer n2 = luaL_optinteger(L, 2, 0); - setseed(state->s, n1, n2); - return 0; + setseed(L, state->s, n1, n2); } + return 2; /* return seeds */ } diff --git a/manual/manual.of b/manual/manual.of index 7f2596faf8..1646f1133f 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -7798,8 +7798,10 @@ The default for @id{y} is zero. When called with no arguments, Lua generates a seed with a weak attempt for randomness. -In this case, -the call returns the two seed components that were used. + +This function returns the two seed components +that were effectively used, +so that setting them again repeats the sequence. To ensure a required level of randomness to the initial state (or contrarily, to have a deterministic sequence, diff --git a/testes/math.lua b/testes/math.lua index 0c297e7411..d0aaa6a5fb 100644 --- a/testes/math.lua +++ b/testes/math.lua @@ -842,9 +842,11 @@ end do -- testing return of 'randomseed' - local x, y = math.randomseed() + local x, y = math.randomseed() local res = math.random(0) - math.randomseed(x, y) -- should repeat the state + x, y = math.randomseed(x, y) -- should repeat the state + assert(math.random(0) == res) + math.randomseed(x, y) -- again should repeat the state assert(math.random(0) == res) -- keep the random seed for following tests end From 9e6807c3c9d5036e999f636f936df07b72284442 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 22 Jul 2019 09:41:10 -0300 Subject: [PATCH 0468/1145] Do not collect open upvalues Open upvalues are kept alive together with their corresponding stack. This change makes a simpler and safer fix to the issue in commit 440a5ee78c8, about upvalues in the list of open upvalues being collected while others are being created. (That previous fix may not be correct.) --- lfunc.c | 15 +++++++-------- lfunc.h | 2 +- lgc.c | 6 ++---- lvm.c | 2 +- 4 files changed, 11 insertions(+), 14 deletions(-) diff --git a/lfunc.c b/lfunc.c index f7edf56b11..6f2f897fed 100644 --- a/lfunc.c +++ b/lfunc.c @@ -83,21 +83,20 @@ static UpVal *newupval (lua_State *L, int tbc, StkId level, UpVal **prev) { /* ** Find and reuse, or create if it does not exist, an upvalue -** at the given level and set it to the given slot. +** at the given level. */ -void luaF_setupval (lua_State *L, StkId level, UpVal **slot) { +UpVal *luaF_findupval (lua_State *L, StkId level) { UpVal **pp = &L->openupval; UpVal *p; lua_assert(isintwups(L) || L->openupval == NULL); while ((p = *pp) != NULL && uplevel(p) >= level) { /* search for it */ - *slot = p; - if (uplevel(p) == level && !isdead(G(L), p)) /* corresponding upvalue? */ - return; /* found it */ + lua_assert(!isdead(G(L), p)); + if (uplevel(p) == level) /* corresponding upvalue? */ + return p; /* return it */ pp = &p->u.open.next; } - /* not found: create a new upvalue after 'pp' (which is - anchored in 'slot', in case of an emergency collection) */ - *slot = newupval(L, 0, level, pp); + /* not found: create a new upvalue after 'pp' */ + return newupval(L, 0, level, pp); } diff --git a/lfunc.h b/lfunc.h index 72fc913ab7..0ed79c48ab 100644 --- a/lfunc.h +++ b/lfunc.h @@ -57,7 +57,7 @@ LUAI_FUNC Proto *luaF_newproto (lua_State *L); LUAI_FUNC CClosure *luaF_newCclosure (lua_State *L, int nelems); LUAI_FUNC LClosure *luaF_newLclosure (lua_State *L, int nelems); LUAI_FUNC void luaF_initupvals (lua_State *L, LClosure *cl); -LUAI_FUNC void luaF_setupval (lua_State *L, StkId level, UpVal **slot); +LUAI_FUNC UpVal *luaF_findupval (lua_State *L, StkId level); LUAI_FUNC void luaF_newtbcupval (lua_State *L, StkId level); LUAI_FUNC int luaF_close (lua_State *L, StkId level, int status); LUAI_FUNC void luaF_unlinkupval (UpVal *uv); diff --git a/lgc.c b/lgc.c index c5babfed68..b7220cf186 100644 --- a/lgc.c +++ b/lgc.c @@ -569,10 +569,8 @@ static int traversethread (global_State *g, lua_State *th) { th->openupval == NULL || isintwups(th)); for (; o < th->top; o++) /* mark live elements in the stack */ markvalue(g, s2v(o)); - for (uv = th->openupval; uv != NULL; uv = uv->u.open.next) { - if (uv->tbc) /* to be closed? */ - markobject(g, uv); /* cannot be collected */ - } + for (uv = th->openupval; uv != NULL; uv = uv->u.open.next) + markobject(g, uv); /* open upvalues cannot be collected */ if (g->gcstate == GCSatomic) { /* final traversal? */ StkId lim = th->stack + th->stacksize; /* real end of stack */ for (; o < lim; o++) /* clear not-marked stack slice */ diff --git a/lvm.c b/lvm.c index d3e05199e7..d9bd0ab323 100644 --- a/lvm.c +++ b/lvm.c @@ -697,7 +697,7 @@ static void pushclosure (lua_State *L, Proto *p, UpVal **encup, StkId base, setclLvalue2s(L, ra, ncl); /* anchor new closure in stack */ for (i = 0; i < nup; i++) { /* fill in its upvalues */ if (uv[i].instack) /* upvalue refers to local variable? */ - luaF_setupval(L, base + uv[i].idx, &ncl->upvals[i]); + ncl->upvals[i] = luaF_findupval(L, base + uv[i].idx); else /* get upvalue from enclosing function */ ncl->upvals[i] = encup[uv[i].idx]; luaC_objbarrier(L, ncl, ncl->upvals[i]); From 7f5c31cdcac5388b3c48a26112dfb6d2cadb7321 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 23 Jul 2019 12:46:33 -0300 Subject: [PATCH 0469/1145] Fixed bug in 'string.format' with option '%f' As an example, 'print(string.format("%.99f", 1e70))' may have a lot of garbage after the number. The old test to ensure that 'string.format("%.99f", n)' was not too large, 'fabs(n) < 1e100', assumes that the number will fit in the 99 bytes; but the 99 is not the space for the number, it is the added extra zeros. The option worked for smaller numbers because of the extra space added to MAX_ITEM. --- lstrlib.c | 14 ++++++-------- testes/strings.lua | 6 ++++++ 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/lstrlib.c b/lstrlib.c index 563d5ca52c..8c9e1a83b1 100644 --- a/lstrlib.c +++ b/lstrlib.c @@ -1227,16 +1227,14 @@ static int str_format (lua_State *L) { nb = lua_number2strx(L, buff, maxitem, form, luaL_checknumber(L, arg)); break; - case 'e': case 'E': case 'f': - case 'g': case 'G': { + case 'f': + maxitem = MAX_ITEMF; /* extra space for '%f' */ + buff = luaL_prepbuffsize(&b, maxitem); + /* FALLTHROUGH */ + case 'e': case 'E': case 'g': case 'G': { lua_Number n = luaL_checknumber(L, arg); - if (*(strfrmt - 1) == 'f' && l_mathop(fabs)(n) >= 1e100) { - /* 'n' needs more than 99 digits */ - maxitem = MAX_ITEMF; /* extra space for '%f' */ - buff = luaL_prepbuffsize(&b, maxitem); - } addlenmod(form, LUA_NUMBER_FRMLEN); - nb = l_sprintf(buff, maxitem, form, (LUAI_UACNUMBER)n); + nb = snprintf(buff, maxitem, form, (LUAI_UACNUMBER)n); break; } case 'p': { diff --git a/testes/strings.lua b/testes/strings.lua index 2540fdefc0..0e7874bf2b 100644 --- a/testes/strings.lua +++ b/testes/strings.lua @@ -255,6 +255,12 @@ do -- longest number that can be formatted local s = string.format('%.99f', -(10^i)) assert(string.len(s) >= i + 101) assert(tonumber(s) == -(10^i)) + + -- limit for floats + assert(10^38 < math.huge) + local s = string.format('%.99f', -(10^38)) + assert(string.len(s) >= 38 + 101) + assert(tonumber(s) == -(10^38)) end From 0eb6aa4013051c8c0148c09d8c85ee7cbdc96f42 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 24 Jul 2019 15:01:59 -0300 Subject: [PATCH 0470/1145] Some improvements in date/time functions - Range in date table extended to full 32 bits. - Easier support for times represented as floats. - Added more tests. --- loslib.c | 76 ++++++++++++++++++++++++++++++------------------ testes/files.lua | 65 ++++++++++++++++++++++++++++++++--------- 2 files changed, 100 insertions(+), 41 deletions(-) diff --git a/loslib.c b/loslib.c index 8809e5ea2a..7812d29bc3 100644 --- a/loslib.c +++ b/loslib.c @@ -58,18 +58,20 @@ ** =================================================================== */ -#if !defined(l_time_t) /* { */ /* ** type to represent time_t in Lua */ +#if !defined(LUA_NUMTIME) /* { */ + #define l_timet lua_Integer #define l_pushtime(L,t) lua_pushinteger(L,(lua_Integer)(t)) +#define l_gettime(L,arg) luaL_checkinteger(L, arg) -static time_t l_checktime (lua_State *L, int arg) { - lua_Integer t = luaL_checkinteger(L, arg); - luaL_argcheck(L, (time_t)t == t, arg, "time out-of-bounds"); - return (time_t)t; -} +#else /* }{ */ + +#define l_timet lua_Number +#define l_pushtime(L,t) lua_pushnumber(L,(lua_Number)(t)) +#define l_gettime(L,arg) luaL_checknumber(L, arg) #endif /* } */ @@ -193,11 +195,25 @@ static int os_clock (lua_State *L) { ** ======================================================= */ -static void setfield (lua_State *L, const char *key, int value) { - lua_pushinteger(L, value); +/* +** About the overflow check: an overflow cannot occurr when time +** is represented by a lua_Integer, because either lua_Integer is +** large enough to represent all int fields or it is not large enough +** to represent a time that cause a field to overflow. However, if +** times are represented as doubles and lua_Integer is int, then the +** time 0x1.e1853b0d184f6p+55 would cause an overflow when adding 1900 +** to compute the year. +*/ +static void setfield (lua_State *L, const char *key, int value, int delta) { + #if (defined(LUA_NUMTIME) && LUA_MAXINTEGER <= INT_MAX) + if (value > LUA_MAXINTEGER - delta) + luaL_error(L, "field '%s' is out-of-bound", key); + #endif + lua_pushinteger(L, (lua_Integer)value + delta); lua_setfield(L, -2, key); } + static void setboolfield (lua_State *L, const char *key, int value) { if (value < 0) /* undefined? */ return; /* does not set field */ @@ -210,14 +226,14 @@ static void setboolfield (lua_State *L, const char *key, int value) { ** Set all fields from structure 'tm' in the table on top of the stack */ static void setallfields (lua_State *L, struct tm *stm) { - setfield(L, "sec", stm->tm_sec); - setfield(L, "min", stm->tm_min); - setfield(L, "hour", stm->tm_hour); - setfield(L, "day", stm->tm_mday); - setfield(L, "month", stm->tm_mon + 1); - setfield(L, "year", stm->tm_year + 1900); - setfield(L, "wday", stm->tm_wday + 1); - setfield(L, "yday", stm->tm_yday + 1); + setfield(L, "year", stm->tm_year, 1900); + setfield(L, "month", stm->tm_mon, 1); + setfield(L, "day", stm->tm_mday, 0); + setfield(L, "hour", stm->tm_hour, 0); + setfield(L, "min", stm->tm_min, 0); + setfield(L, "sec", stm->tm_sec, 0); + setfield(L, "yday", stm->tm_yday, 1); + setfield(L, "wday", stm->tm_wday, 1); setboolfield(L, "isdst", stm->tm_isdst); } @@ -230,11 +246,6 @@ static int getboolfield (lua_State *L, const char *key) { } -/* maximum value for date fields (to avoid arithmetic overflows with 'int') */ -#if !defined(L_MAXDATEFIELD) -#define L_MAXDATEFIELD (INT_MAX / 2) -#endif - static int getfield (lua_State *L, const char *key, int d, int delta) { int isnum; int t = lua_getfield(L, -1, key); /* get field and its type */ @@ -247,7 +258,9 @@ static int getfield (lua_State *L, const char *key, int d, int delta) { res = d; } else { - if (!(-L_MAXDATEFIELD <= res && res <= L_MAXDATEFIELD)) + /* unsigned avoids overflow when lua_Integer has 32 bits */ + if (!(res >= 0 ? (lua_Unsigned)res <= (lua_Unsigned)INT_MAX + delta + : (lua_Integer)INT_MIN + delta <= res)) return luaL_error(L, "field '%s' is out-of-bound", key); res -= delta; } @@ -275,6 +288,13 @@ static const char *checkoption (lua_State *L, const char *conv, } +static time_t l_checktime (lua_State *L, int arg) { + l_timet t = l_gettime(L, arg); + luaL_argcheck(L, (time_t)t == t, arg, "time out-of-bounds"); + return (time_t)t; +} + + /* maximum size for an individual 'strftime' item */ #define SIZETIMEFMT 250 @@ -293,7 +313,7 @@ static int os_date (lua_State *L) { stm = l_localtime(&t, &tmr); if (stm == NULL) /* invalid date? */ return luaL_error(L, - "time result cannot be represented in this installation"); + "date result cannot be represented in this installation"); if (strcmp(s, "*t") == 0) { lua_createtable(L, 0, 9); /* 9 = number of fields */ setallfields(L, stm); @@ -329,12 +349,12 @@ static int os_time (lua_State *L) { struct tm ts; luaL_checktype(L, 1, LUA_TTABLE); lua_settop(L, 1); /* make sure table is at the top */ - ts.tm_sec = getfield(L, "sec", 0, 0); - ts.tm_min = getfield(L, "min", 0, 0); - ts.tm_hour = getfield(L, "hour", 12, 0); - ts.tm_mday = getfield(L, "day", -1, 0); - ts.tm_mon = getfield(L, "month", -1, 1); ts.tm_year = getfield(L, "year", -1, 1900); + ts.tm_mon = getfield(L, "month", -1, 1); + ts.tm_mday = getfield(L, "day", -1, 0); + ts.tm_hour = getfield(L, "hour", 12, 0); + ts.tm_min = getfield(L, "min", 0, 0); + ts.tm_sec = getfield(L, "sec", 0, 0); ts.tm_isdst = getboolfield(L, "isdst"); t = mktime(&ts); setallfields(L, &ts); /* update fields with normalized values */ diff --git a/testes/files.lua b/testes/files.lua index c8f23d18da..6e7bd9e243 100644 --- a/testes/files.lua +++ b/testes/files.lua @@ -775,11 +775,24 @@ assert(os.date(string.rep("%d", 1000), t) == string.rep(os.date("%d", t), 1000)) assert(os.date(string.rep("%", 200)) == string.rep("%", 100)) -local t = os.time() -D = os.date("*t", t) -load(os.date([[assert(D.year==%Y and D.month==%m and D.day==%d and - D.hour==%H and D.min==%M and D.sec==%S and - D.wday==%w+1 and D.yday==%j)]], t))() +local function checkDateTable (t) + _G.D = os.date("*t", t) + assert(os.time(D) == t) + load(os.date([[assert(D.year==%Y and D.month==%m and D.day==%d and + D.hour==%H and D.min==%M and D.sec==%S and + D.wday==%w+1 and D.yday==%j)]], t))() + _G.D = nil +end + +checkDateTable(os.time()) +if not _port then + -- assume that time_t can represent these values + checkDateTable(0) + checkDateTable(1) + checkDateTable(1000) + checkDateTable(0x7fffffff) + checkDateTable(0x80000000) +end checkerr("invalid conversion specifier", os.date, "%") checkerr("invalid conversion specifier", os.date, "%9") @@ -793,11 +806,24 @@ checkerr("not an integer", os.time, {year=1000, month=1, day=1, hour=1.5}) checkerr("missing", os.time, {hour = 12}) -- missing date + +if string.packsize("i") == 4 then -- 4-byte ints + checkerr("field 'year' is out-of-bound", os.time, + {year = -(1 << 31) + 1899, month = 1, day = 1}) +end + if not _port then -- test Posix-specific modifiers assert(type(os.date("%Ex")) == 'string') assert(type(os.date("%Oy")) == 'string') + -- test large dates (assume at least 4-byte ints and time_t) + local t0 = os.time{year = 1970, month = 1, day = 0} + local t1 = os.time{year = 1970, month = 1, day = 0, sec = (1 << 31) - 1} + assert(t1 - t0 == (1 << 31) - 1) + t0 = os.time{year = 1970, month = 1, day = 1} + t1 = os.time{year = 1970, month = 1, day = 1, sec = -(1 << 31)} + assert(t1 - t0 == -(1 << 31)) -- test out-of-range dates (at least for Unix) if maxint >= 2^62 then -- cannot do these tests in Small Lua @@ -812,25 +838,37 @@ if not _port then -- time_t has 8 bytes; an int year cannot represent a huge time print(" 8-byte time_t") checkerr("cannot be represented", os.date, "%Y", 2^60) - -- it should have no problems with year 4000 - assert(tonumber(os.time{year=4000, month=1, day=1})) + + -- this is the maximum year + assert(tonumber(os.time + {year=(1 << 31) + 1899, month=12, day=31, hour=23, min=59, sec=59})) + + -- this is too much + checkerr("represented", os.time, + {year=(1 << 31) + 1899, month=12, day=31, hour=23, min=59, sec=60}) end + + -- internal 'int' fields cannot hold these values + checkerr("field 'day' is out-of-bound", os.time, + {year = 0, month = 1, day = 2^32}) + + checkerr("field 'month' is out-of-bound", os.time, + {year = 0, month = -((1 << 31) + 1), day = 1}) + + checkerr("field 'year' is out-of-bound", os.time, + {year = (1 << 31) + 1900, month = 1, day = 1}) + else -- 8-byte ints -- assume time_t has 8 bytes too print(" 8-byte time_t") assert(tonumber(os.date("%Y", 2^60))) + -- but still cannot represent a huge year checkerr("cannot be represented", os.time, {year=2^60, month=1, day=1}) end end end - -D = os.date("!*t", t) -load(os.date([[!assert(D.year==%Y and D.month==%m and D.day==%d and - D.hour==%H and D.min==%M and D.sec==%S and - D.wday==%w+1 and D.yday==%j)]], t))() - do local D = os.date("*t") local t = os.time(D) @@ -844,6 +882,7 @@ do assert(t == t1) -- if isdst is absent uses correct default end +local D = os.date("*t") t = os.time(D) D.year = D.year-1; local t1 = os.time(D) From 9a37dc0ce64c51fd57f5e658a5af8f3671a26b0a Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 25 Jul 2019 13:55:29 -0300 Subject: [PATCH 0471/1145] Small corrections when setting 'L->top' - OP_NEWTABLE can use 'ra + 1' to set top (instead of ci->top); - OP_CLOSE doesn't need to set top ('Protect' already does that); - OP_TFORCALL must use 'ProtectNT', to preserve the top already set. (That was a small bug, because iterators could be called with extra parameters besides the state and the control variable.) - Comments and an extra test for the bug in previous item. --- lapi.h | 10 ++++++++++ lparser.c | 3 ++- lvm.c | 7 +++---- testes/nextvar.lua | 3 ++- 4 files changed, 17 insertions(+), 6 deletions(-) diff --git a/lapi.h b/lapi.h index 5a4206f1ae..f48d14fdf6 100644 --- a/lapi.h +++ b/lapi.h @@ -11,12 +11,22 @@ #include "llimits.h" #include "lstate.h" + +/* Increments 'L->top', checking for stack overflows */ #define api_incr_top(L) {L->top++; api_check(L, L->top <= L->ci->top, \ "stack overflow");} + +/* +** If a call returns too many multiple returns, the callee may not have +** stack space to accomodate all results. In this case, this macro +** increases its stack space ('L->ci->top'). +*/ #define adjustresults(L,nres) \ { if ((nres) <= LUA_MULTRET && L->ci->top < L->top) L->ci->top = L->top; } + +/* Ensure the stack has at least 'n' elements */ #define api_checknelems(L,n) api_check(L, (n) < (L->top - L->ci->func), \ "not enough elements in the stack") diff --git a/lparser.c b/lparser.c index b70c609e21..7447222beb 100644 --- a/lparser.c +++ b/lparser.c @@ -694,9 +694,10 @@ static Proto *addprototype (LexState *ls) { /* ** codes instruction to create new closure in parent function. -** The OP_CLOSURE instruction must use the last available register, +** The OP_CLOSURE instruction uses the last available register, ** so that, if it invokes the GC, the GC knows which registers ** are in use at that time. + */ static void codeclosure (LexState *ls, expdesc *v) { FuncState *fs = ls->fs->prev; diff --git a/lvm.c b/lvm.c index d9bd0ab323..26477c2cf9 100644 --- a/lvm.c +++ b/lvm.c @@ -1258,7 +1258,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { if (TESTARG_k(i)) c += GETARG_Ax(*pc) * (MAXARG_C + 1); pc++; /* skip extra argument */ - L->top = ci->top; /* correct top in case of GC */ + L->top = ra + 1; /* correct top in case of emergency GC */ t = luaH_new(L); /* memory allocation */ sethvalue2s(L, ra, t); if (b != 0 || c != 0) @@ -1478,7 +1478,6 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_CLOSE) { - L->top = ra + 1; /* everything is free after this slot */ Protect(luaF_close(L, ra, LUA_OK)); vmbreak; } @@ -1755,7 +1754,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { /* push function, state, and control variable */ memcpy(ra + 4, ra, 3 * sizeof(*ra)); L->top = ra + 4 + 3; - Protect(luaD_call(L, ra + 4, GETARG_C(i))); /* do the call */ + ProtectNT(luaD_call(L, ra + 4, GETARG_C(i))); /* do the call */ updatestack(ci); /* stack may have changed */ i = *(pc++); /* go to next instruction */ lua_assert(GET_OPCODE(i) == OP_TFORLOOP && ra == RA(i)); @@ -1776,7 +1775,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { if (n == 0) n = cast_int(L->top - ra) - 1; /* get up to the top */ else - L->top = ci->top; /* correct top in case of GC */ + L->top = ci->top; /* correct top in case of emergency GC */ last += n; if (TESTARG_k(i)) { last += GETARG_Ax(*pc) * (MAXARG_C + 1); diff --git a/testes/nextvar.lua b/testes/nextvar.lua index a7fe625e10..9d91963135 100644 --- a/testes/nextvar.lua +++ b/testes/nextvar.lua @@ -671,7 +671,8 @@ collectgarbage() local function f (n, p) local t = {}; for i=1,p do t[i] = i*10 end - return function (_,n) + return function (_, n, ...) + assert(select("#", ...) == 0) -- no extra arguments if n > 0 then n = n-1 return n, table.unpack(t) From e70f275f32a5339a86be6f8b9d08c20cb874b205 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 26 Jul 2019 13:27:43 -0300 Subject: [PATCH 0472/1145] Bug: 'Vardesc' array can be reallocated in 'localstat' A reference to a 'Vardesc*' (as done by 'localstat') can be invalidated by the creation of any new variable. --- lparser.c | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/lparser.c b/lparser.c index 7447222beb..f1d2ce853e 100644 --- a/lparser.c +++ b/lparser.c @@ -187,9 +187,10 @@ static int registerlocalvar (LexState *ls, FuncState *fs, TString *varname) { /* -** Create a new local variable with the given 'name'. +** Create a new local variable with the given 'name'. Return its index +** in the function. */ -static Vardesc *new_localvar (LexState *ls, TString *name) { +static int new_localvar (LexState *ls, TString *name, int kind) { lua_State *L = ls->L; FuncState *fs = ls->fs; Dyndata *dyd = ls->dyd; @@ -199,13 +200,14 @@ static Vardesc *new_localvar (LexState *ls, TString *name) { luaM_growvector(L, dyd->actvar.arr, dyd->actvar.n + 1, dyd->actvar.size, Vardesc, USHRT_MAX, "local variables"); var = &dyd->actvar.arr[dyd->actvar.n++]; - var->vd.kind = VDKREG; /* default is a regular variable */ + var->vd.kind = kind; var->vd.name = name; - return var; + return dyd->actvar.n - 1 - fs->firstlocal; } #define new_localvarliteral(ls,v) \ - new_localvar(ls, luaX_newstring(ls, "" v, (sizeof(v)/sizeof(char)) - 1)); + new_localvar(ls, \ + luaX_newstring(ls, "" v, (sizeof(v)/sizeof(char)) - 1), VDKREG); @@ -945,7 +947,7 @@ static void parlist (LexState *ls) { do { switch (ls->t.token) { case TK_NAME: { /* param -> NAME */ - new_localvar(ls, str_checkname(ls)); + new_localvar(ls, str_checkname(ls), VDKREG); nparams++; break; } @@ -1551,7 +1553,7 @@ static void fornum (LexState *ls, TString *varname, int line) { new_localvarliteral(ls, "(for state)"); new_localvarliteral(ls, "(for state)"); new_localvarliteral(ls, "(for state)"); - new_localvar(ls, varname); + new_localvar(ls, varname, VDKREG); checknext(ls, '='); exp1(ls); /* initial value */ checknext(ls, ','); @@ -1580,9 +1582,9 @@ static void forlist (LexState *ls, TString *indexname) { new_localvarliteral(ls, "(for state)"); new_localvarliteral(ls, "(for state)"); /* create declared variables */ - new_localvar(ls, indexname); + new_localvar(ls, indexname, VDKREG); while (testnext(ls, ',')) { - new_localvar(ls, str_checkname(ls)); + new_localvar(ls, str_checkname(ls), VDKREG); nvars++; } checknext(ls, TK_IN); @@ -1706,7 +1708,7 @@ static void localfunc (LexState *ls) { expdesc b; FuncState *fs = ls->fs; int fvar = fs->nactvar; /* function's variable index */ - new_localvar(ls, str_checkname(ls)); /* new local variable */ + new_localvar(ls, str_checkname(ls), VDKREG); /* new local variable */ adjustlocalvars(ls, 1); /* enter its scope */ body(ls, &b, 0, ls->linenumber); /* function created in next register */ /* debug information will only see the variable after this point! */ @@ -1746,13 +1748,13 @@ static void localstat (LexState *ls) { FuncState *fs = ls->fs; int toclose = -1; /* index of to-be-closed variable (if any) */ Vardesc *var; /* last variable */ + int ivar; /* index of last variable */ int nvars = 0; int nexps; expdesc e; do { int kind = getlocalattribute(ls); - var = new_localvar(ls, str_checkname(ls)); - var->vd.kind = kind; + ivar = new_localvar(ls, str_checkname(ls), kind); if (kind == RDKTOCLOSE) { /* to-be-closed? */ if (toclose != -1) /* one already present? */ luaK_semerror(ls, "multiple to-be-closed variables in local list"); @@ -1766,6 +1768,7 @@ static void localstat (LexState *ls) { e.k = VVOID; nexps = 0; } + var = getlocalvardesc(fs, ivar); /* get last variable */ if (nvars == nexps && /* no adjustments? */ var->vd.kind == RDKCONST && /* last variable is const? */ luaK_exp2const(fs, &e, &var->k)) { /* compile-time constant? */ From b80077b8f3e27a94c6afa895b41a9f8b52c42e61 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 26 Jul 2019 14:59:39 -0300 Subject: [PATCH 0473/1145] Change in the handling of 'L->top' when calling metamethods Instead of updating 'L->top' in every place that may call a metamethod, the metamethod functions themselves (luaT_trybinTM and luaT_callorderTM) correct the top. (When calling metamethods from the C API, however, the callers must preserve 'L->top'.) --- lapi.c | 2 ++ lobject.c | 2 ++ lopcodes.c | 2 +- ltm.c | 12 +++++++++--- ltm.h | 1 + lvm.c | 44 ++++++++++++++++++++++++-------------------- testes/api.lua | 17 +++++++++++++++++ testes/coroutine.lua | 2 +- testes/events.lua | 15 +++++++++++++-- testes/strings.lua | 7 +++++-- 10 files changed, 75 insertions(+), 29 deletions(-) diff --git a/lapi.c b/lapi.c index 0ea3dc0f22..a9ffad80f7 100644 --- a/lapi.c +++ b/lapi.c @@ -329,12 +329,14 @@ LUA_API int lua_compare (lua_State *L, int index1, int index2, int op) { o1 = index2value(L, index1); o2 = index2value(L, index2); if (isvalid(L, o1) && isvalid(L, o2)) { + ptrdiff_t top = savestack(L, L->top); switch (op) { case LUA_OPEQ: i = luaV_equalobj(L, o1, o2); break; case LUA_OPLT: i = luaV_lessthan(L, o1, o2); break; case LUA_OPLE: i = luaV_lessequal(L, o1, o2); break; default: api_check(L, 0, "invalid option"); } + L->top = restorestack(L, top); } lua_unlock(L); return i; diff --git a/lobject.c b/lobject.c index b4efae4f33..b376ab1583 100644 --- a/lobject.c +++ b/lobject.c @@ -127,7 +127,9 @@ void luaO_arith (lua_State *L, int op, const TValue *p1, const TValue *p2, StkId res) { if (!luaO_rawarith(L, op, p1, p2, s2v(res))) { /* could not perform raw operation; try metamethod */ + ptrdiff_t top = savestack(L, L->top); luaT_trybinTM(L, p1, p2, res, cast(TMS, (op - LUA_OPADD) + TM_ADD)); + L->top = restorestack(L, top); } } diff --git a/lopcodes.c b/lopcodes.c index 23c3a6e451..ee79578613 100644 --- a/lopcodes.c +++ b/lopcodes.c @@ -101,7 +101,7 @@ LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = { ,opmode(0, 1, 0, 0, iABC) /* OP_SETLIST */ ,opmode(0, 0, 0, 1, iABx) /* OP_CLOSURE */ ,opmode(1, 0, 0, 1, iABC) /* OP_VARARG */ - ,opmode(0, 0, 0, 1, iABC) /* OP_VARARGPREP */ + ,opmode(0, 1, 0, 1, iABC) /* OP_VARARGPREP */ ,opmode(0, 0, 0, 0, iAx) /* OP_EXTRAARG */ }; diff --git a/ltm.c b/ltm.c index 247394447c..19233a8758 100644 --- a/ltm.c +++ b/ltm.c @@ -147,11 +147,9 @@ static int callbinTM (lua_State *L, const TValue *p1, const TValue *p2, void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2, StkId res, TMS event) { + L->top = L->ci->top; if (!callbinTM(L, p1, p2, res, event)) { switch (event) { - case TM_CONCAT: - luaG_concaterror(L, p1, p2); - /* call never returns, but to avoid warnings: *//* FALLTHROUGH */ case TM_BAND: case TM_BOR: case TM_BXOR: case TM_SHL: case TM_SHR: case TM_BNOT: { if (ttisnumber(p1) && ttisnumber(p2)) @@ -167,6 +165,13 @@ void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2, } +void luaT_tryconcatTM (lua_State *L) { + StkId top = L->top; + if (!callbinTM(L, s2v(top - 2), s2v(top - 1), top - 2, TM_CONCAT)) + luaG_concaterror(L, s2v(top - 2), s2v(top - 1)); +} + + void luaT_trybinassocTM (lua_State *L, const TValue *p1, const TValue *p2, StkId res, int flip, TMS event) { if (flip) @@ -186,6 +191,7 @@ void luaT_trybiniTM (lua_State *L, const TValue *p1, lua_Integer i2, int luaT_callorderTM (lua_State *L, const TValue *p1, const TValue *p2, TMS event) { + L->top = L->ci->top; if (callbinTM(L, p1, p2, L->top, event)) /* try original event */ return !l_isfalse(s2v(L->top)); #if defined(LUA_COMPAT_LT_LE) diff --git a/ltm.h b/ltm.h index e308fb80e0..51dfe79354 100644 --- a/ltm.h +++ b/ltm.h @@ -75,6 +75,7 @@ LUAI_FUNC void luaT_callTMres (lua_State *L, const TValue *f, const TValue *p1, const TValue *p2, StkId p3); LUAI_FUNC void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2, StkId res, TMS event); +LUAI_FUNC void luaT_tryconcatTM (lua_State *L); LUAI_FUNC void luaT_trybinassocTM (lua_State *L, const TValue *p1, const TValue *p2, StkId res, int inv, TMS event); LUAI_FUNC void luaT_trybiniTM (lua_State *L, const TValue *p1, lua_Integer i2, diff --git a/lvm.c b/lvm.c index 26477c2cf9..f177ce6a21 100644 --- a/lvm.c +++ b/lvm.c @@ -515,8 +515,11 @@ int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2) { } if (tm == NULL) /* no TM? */ return 0; /* objects are different */ - luaT_callTMres(L, tm, t1, t2, L->top); /* call TM */ - return !l_isfalse(s2v(L->top)); + else { + L->top = L->ci->top; + luaT_callTMres(L, tm, t1, t2, L->top); /* call TM */ + return !l_isfalse(s2v(L->top)); + } } @@ -548,7 +551,7 @@ void luaV_concat (lua_State *L, int total) { int n = 2; /* number of elements handled in this pass (at least 2) */ if (!(ttisstring(s2v(top - 2)) || cvt2str(s2v(top - 2))) || !tostring(L, s2v(top - 1))) - luaT_trybinTM(L, s2v(top - 2), s2v(top - 1), top - 2, TM_CONCAT); + luaT_tryconcatTM(L); else if (isemptystr(s2v(top - 1))) /* second operand is empty? */ cast_void(tostring(L, s2v(top - 2))); /* result is first operand */ else if (isemptystr(s2v(top - 2))) { /* first operand is empty string? */ @@ -747,7 +750,7 @@ void luaV_finishOp (lua_State *L) { break; } case OP_CONCAT: { - StkId top = L->top - 1; /* top when 'luaT_trybinTM' was called */ + StkId top = L->top - 1; /* top when 'luaT_tryconcatTM' was called */ int a = GETARG_A(inst); /* first element to concatenate */ int total = cast_int(top - 1 - (base + a)); /* yet to concatenate */ setobjs2s(L, top - 2, top); /* put TM result in proper position */ @@ -801,7 +804,7 @@ void luaV_finishOp (lua_State *L) { setfltvalue(s2v(ra), fop(L, nb, fimm)); \ } \ else \ - Protect(luaT_trybiniTM(L, v1, imm, flip, ra, tm)); } + ProtectNT(luaT_trybiniTM(L, v1, imm, flip, ra, tm)); } /* @@ -836,7 +839,7 @@ void luaV_finishOp (lua_State *L) { setfltvalue(s2v(ra), fop(L, n1, n2)); \ } \ else \ - Protect(luaT_trybinTM(L, v1, v2, ra, tm)); } + ProtectNT(luaT_trybinTM(L, v1, v2, ra, tm)); } /* @@ -877,7 +880,7 @@ void luaV_finishOp (lua_State *L) { setfltvalue(s2v(ra), fop(L, n1, n2)); \ } \ else \ - Protect(luaT_trybinassocTM(L, v1, v2, ra, flip, tm)); } } + ProtectNT(luaT_trybinassocTM(L, v1, v2, ra, flip, tm)); } } /* @@ -891,7 +894,7 @@ void luaV_finishOp (lua_State *L) { setfltvalue(s2v(ra), fop(L, n1, n2)); \ } \ else \ - Protect(luaT_trybinTM(L, v1, v2, ra, tm)); } + ProtectNT(luaT_trybinTM(L, v1, v2, ra, tm)); } /* @@ -906,7 +909,7 @@ void luaV_finishOp (lua_State *L) { setivalue(s2v(ra), op(L, i1, i2)); \ } \ else \ - Protect(luaT_trybiniTM(L, v1, i2, TESTARG_k(i), ra, tm)); } + ProtectNT(luaT_trybiniTM(L, v1, i2, TESTARG_k(i), ra, tm)); } /* @@ -920,7 +923,7 @@ void luaV_finishOp (lua_State *L) { setivalue(s2v(ra), op(L, i1, i2)); \ } \ else \ - Protect(luaT_trybinTM(L, v1, v2, ra, tm)); } + ProtectNT(luaT_trybinTM(L, v1, v2, ra, tm)); } /* @@ -937,7 +940,7 @@ void luaV_finishOp (lua_State *L) { else if (ttisnumber(s2v(ra)) && ttisnumber(rb)) \ cond = opf(s2v(ra), rb); \ else \ - Protect(cond = other(L, s2v(ra), rb)); \ + ProtectNT(cond = other(L, s2v(ra), rb)); \ docondjump(); } @@ -956,7 +959,7 @@ void luaV_finishOp (lua_State *L) { } \ else { \ int isf = GETARG_C(i); \ - Protect(cond = luaT_callorderiTM(L, s2v(ra), im, inv, isf, tm)); \ + ProtectNT(cond = luaT_callorderiTM(L, s2v(ra), im, inv, isf, tm)); \ } \ docondjump(); } @@ -1094,7 +1097,8 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmfetch(); lua_assert(base == ci->func + 1); lua_assert(base <= L->top && L->top < L->stack + L->stacksize); - lua_assert(ci->top < L->stack + L->stacksize); + /* invalidate top for instructions not expecting it */ + lua_assert(isIT(i) || (L->top = base)); vmdispatch (GET_OPCODE(i)) { vmcase(OP_MOVE) { setobjs2s(L, ra, RB(i)); @@ -1359,7 +1363,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { if (TESTARG_k(i)) { ic = -ic; ev = TM_SHL; } - Protect(luaT_trybiniTM(L, rb, ic, 0, ra, ev)); + ProtectNT(luaT_trybiniTM(L, rb, ic, 0, ra, ev)); } vmbreak; } @@ -1371,7 +1375,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { setivalue(s2v(ra), luaV_shiftl(ic, ib)); } else - Protect(luaT_trybiniTM(L, rb, ic, 1, ra, TM_SHL)); + ProtectNT(luaT_trybiniTM(L, rb, ic, 1, ra, TM_SHL)); vmbreak; } vmcase(OP_ADD) { @@ -1422,7 +1426,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { setivalue(s2v(ra), luaV_shiftl(ib, -ic)); } else - Protect(luaT_trybinTM(L, rb, rc, ra, TM_SHR)); + ProtectNT(luaT_trybinTM(L, rb, rc, ra, TM_SHR)); vmbreak; } vmcase(OP_SHL) { @@ -1433,7 +1437,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { setivalue(s2v(ra), luaV_shiftl(ib, ic)); } else - Protect(luaT_trybinTM(L, rb, rc, ra, TM_SHL)); + ProtectNT(luaT_trybinTM(L, rb, rc, ra, TM_SHL)); vmbreak; } vmcase(OP_UNM) { @@ -1447,7 +1451,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { setfltvalue(s2v(ra), luai_numunm(L, nb)); } else - Protect(luaT_trybinTM(L, rb, rb, ra, TM_UNM)); + ProtectNT(luaT_trybinTM(L, rb, rb, ra, TM_UNM)); vmbreak; } vmcase(OP_BNOT) { @@ -1457,7 +1461,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { setivalue(s2v(ra), intop(^, ~l_castS2U(0), ib)); } else - Protect(luaT_trybinTM(L, rb, rb, ra, TM_BNOT)); + ProtectNT(luaT_trybinTM(L, rb, rb, ra, TM_BNOT)); vmbreak; } vmcase(OP_NOT) { @@ -1493,7 +1497,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmcase(OP_EQ) { int cond; TValue *rb = vRB(i); - Protect(cond = luaV_equalobj(L, s2v(ra), rb)); + ProtectNT(cond = luaV_equalobj(L, s2v(ra), rb)); docondjump(); vmbreak; } diff --git a/testes/api.lua b/testes/api.lua index 5da036414b..0966ed19b7 100644 --- a/testes/api.lua +++ b/testes/api.lua @@ -241,6 +241,23 @@ assert(a == 20 and b == false) a,b = T.testC("compare LE 5 -6, return 2", a1, 2, 2, a1, 2, 20) assert(a == 20 and b == true) + +do -- testing lessthan and lessequal with metamethods + local mt = {__lt = function (a,b) return a[1] < b[1] end, + __le = function (a,b) return a[1] <= b[1] end, + __eq = function (a,b) return a[1] == b[1] end} + local function O (x) + return setmetatable({x}, mt) + end + + local a, b = T.testC("compare LT 2 3; pushint 10; return 2", O(1), O(2)) + assert(a == true and b == 10) + local a, b = T.testC("compare LE 2 3; pushint 10; return 2", O(3), O(2)) + assert(a == false and b == 10) + local a, b = T.testC("compare EQ 2 3; pushint 10; return 2", O(3), O(3)) + assert(a == true and b == 10) +end + -- testing length local t = setmetatable({x = 20}, {__len = function (t) return t.x end}) a,b,c = T.testC([[ diff --git a/testes/coroutine.lua b/testes/coroutine.lua index e04207c857..00531d8e1f 100644 --- a/testes/coroutine.lua +++ b/testes/coroutine.lua @@ -809,7 +809,7 @@ assert(run(function () -- tests for coroutine API if T==nil then (Message or print)('\n >>> testC not active: skipping coroutine API tests <<<\n') - return + print "OK"; return end print('testing coroutine API') diff --git a/testes/events.lua b/testes/events.lua index cf68d1e99c..7fb54c9aec 100644 --- a/testes/events.lua +++ b/testes/events.lua @@ -217,9 +217,16 @@ t.__le = function (a,b,c) return a<=b, "dummy" end +t.__eq = function (a,b,c) + assert(c == nil) + if type(a) == 'table' then a = a.x end + if type(b) == 'table' then b = b.x end + return a == b, "dummy" +end + function Op(x) return setmetatable({x=x}, t) end -local function test () +local function test (a, b, c) assert(not(Op(1)= Op(1)) and not(1 >= Op(2)) and (Op(2) >= 1)) assert((Op('a')>=Op('a')) and not(Op('a')>=Op('b')) and (Op('b')>=Op('a'))) assert(('a' >= Op('a')) and not(Op('a') >= 'b') and (Op('b') >= Op('a'))) + assert(Op(1) == Op(1) and Op(1) ~= Op(2)) + assert(Op('a') == Op('a') and Op('a') ~= Op('b')) + assert(a == a and a ~= b) + assert(Op(3) == c) end -test() +test(Op(1), Op(2), Op(3)) -- test `partial order' diff --git a/testes/strings.lua b/testes/strings.lua index 0e7874bf2b..aa039c4fc2 100644 --- a/testes/strings.lua +++ b/testes/strings.lua @@ -167,8 +167,11 @@ do -- tests for '%p' format local t1 = {}; local t2 = {} assert(string.format("%p", t1) ~= string.format("%p", t2)) end - assert(string.format("%p", string.rep("a", 10)) == - string.format("%p", string.rep("a", 10))) -- short strings + do -- short strings + local s1 = string.rep("a", 10) + local s2 = string.rep("a", 10) + assert(string.format("%p", s1) == string.format("%p", s2)) + end do -- long strings local s1 = string.rep("a", 300); local s2 = string.rep("a", 300) assert(string.format("%p", s1) ~= string.format("%p", s2)) From 0d529138042563baf260366e19a7aa2c60a07174 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 30 Jul 2019 12:18:19 -0300 Subject: [PATCH 0474/1145] Change in the syntax of attributes Attributes changed to posfixed ('x ', instead of ' x'), and "toclose" renamed to "close". Posfixed attributes seem to make it clearer that it applies to only one variable when there are multiple variables. --- lparser.c | 25 ++++++++-------- testes/api.lua | 2 +- testes/code.lua | 36 +++++++++++----------- testes/constructs.lua | 16 +++++----- testes/coroutine.lua | 8 ++--- testes/files.lua | 14 ++++----- testes/goto.lua | 2 +- testes/locals.lua | 70 +++++++++++++++++++++---------------------- testes/main.lua | 4 +-- testes/math.lua | 24 +++++++-------- testes/strings.lua | 4 +-- 11 files changed, 103 insertions(+), 102 deletions(-) diff --git a/lparser.c b/lparser.c index f1d2ce853e..2dcd320cc4 100644 --- a/lparser.c +++ b/lparser.c @@ -190,7 +190,7 @@ static int registerlocalvar (LexState *ls, FuncState *fs, TString *varname) { ** Create a new local variable with the given 'name'. Return its index ** in the function. */ -static int new_localvar (LexState *ls, TString *name, int kind) { +static int new_localvar (LexState *ls, TString *name) { lua_State *L = ls->L; FuncState *fs = ls->fs; Dyndata *dyd = ls->dyd; @@ -200,14 +200,14 @@ static int new_localvar (LexState *ls, TString *name, int kind) { luaM_growvector(L, dyd->actvar.arr, dyd->actvar.n + 1, dyd->actvar.size, Vardesc, USHRT_MAX, "local variables"); var = &dyd->actvar.arr[dyd->actvar.n++]; - var->vd.kind = kind; + var->vd.kind = VDKREG; /* default */ var->vd.name = name; return dyd->actvar.n - 1 - fs->firstlocal; } #define new_localvarliteral(ls,v) \ new_localvar(ls, \ - luaX_newstring(ls, "" v, (sizeof(v)/sizeof(char)) - 1), VDKREG); + luaX_newstring(ls, "" v, (sizeof(v)/sizeof(char)) - 1)); @@ -947,7 +947,7 @@ static void parlist (LexState *ls) { do { switch (ls->t.token) { case TK_NAME: { /* param -> NAME */ - new_localvar(ls, str_checkname(ls), VDKREG); + new_localvar(ls, str_checkname(ls)); nparams++; break; } @@ -1553,7 +1553,7 @@ static void fornum (LexState *ls, TString *varname, int line) { new_localvarliteral(ls, "(for state)"); new_localvarliteral(ls, "(for state)"); new_localvarliteral(ls, "(for state)"); - new_localvar(ls, varname, VDKREG); + new_localvar(ls, varname); checknext(ls, '='); exp1(ls); /* initial value */ checknext(ls, ','); @@ -1582,9 +1582,9 @@ static void forlist (LexState *ls, TString *indexname) { new_localvarliteral(ls, "(for state)"); new_localvarliteral(ls, "(for state)"); /* create declared variables */ - new_localvar(ls, indexname, VDKREG); + new_localvar(ls, indexname); while (testnext(ls, ',')) { - new_localvar(ls, str_checkname(ls), VDKREG); + new_localvar(ls, str_checkname(ls)); nvars++; } checknext(ls, TK_IN); @@ -1708,7 +1708,7 @@ static void localfunc (LexState *ls) { expdesc b; FuncState *fs = ls->fs; int fvar = fs->nactvar; /* function's variable index */ - new_localvar(ls, str_checkname(ls), VDKREG); /* new local variable */ + new_localvar(ls, str_checkname(ls)); /* new local variable */ adjustlocalvars(ls, 1); /* enter its scope */ body(ls, &b, 0, ls->linenumber); /* function created in next register */ /* debug information will only see the variable after this point! */ @@ -1723,7 +1723,7 @@ static int getlocalattribute (LexState *ls) { checknext(ls, '>'); if (strcmp(attr, "const") == 0) return RDKCONST; /* read-only variable */ - else if (strcmp(attr, "toclose") == 0) + else if (strcmp(attr, "close") == 0) return RDKTOCLOSE; /* to-be-closed variable */ else luaK_semerror(ls, @@ -1748,13 +1748,14 @@ static void localstat (LexState *ls) { FuncState *fs = ls->fs; int toclose = -1; /* index of to-be-closed variable (if any) */ Vardesc *var; /* last variable */ - int ivar; /* index of last variable */ + int ivar, kind; /* index and kind of last variable */ int nvars = 0; int nexps; expdesc e; do { - int kind = getlocalattribute(ls); - ivar = new_localvar(ls, str_checkname(ls), kind); + ivar = new_localvar(ls, str_checkname(ls)); + kind = getlocalattribute(ls); + getlocalvardesc(fs, ivar)->vd.kind = kind; if (kind == RDKTOCLOSE) { /* to-be-closed? */ if (toclose != -1) /* one already present? */ luaK_semerror(ls, "multiple to-be-closed variables in local list"); diff --git a/testes/api.lua b/testes/api.lua index 0966ed19b7..3f7f7596a3 100644 --- a/testes/api.lua +++ b/testes/api.lua @@ -1203,7 +1203,7 @@ end) testamem("to-be-closed variables", function() local flag do - local x = + local x = setmetatable({}, {__close = function () flag = true end}) flag = false local x = {} diff --git a/testes/code.lua b/testes/code.lua index 57923b1410..3b768f334a 100644 --- a/testes/code.lua +++ b/testes/code.lua @@ -8,22 +8,22 @@ end print "testing code generation and optimizations" -- to test constant propagation -local k0aux = 0 -local k0 = k0aux -local k1 = 1 -local k3 = 3 -local k6 = k3 + (k3 << k0) -local kFF0 = 0xFF0 -local k3_78 = 3.78 -local x, k3_78_4 = 10, k3_78 / 4 +local k0aux = 0 +local k0 = k0aux +local k1 = 1 +local k3 = 3 +local k6 = k3 + (k3 << k0) +local kFF0 = 0xFF0 +local k3_78 = 3.78 +local x, k3_78_4 = 10, k3_78 / 4 assert(x == 10) -local kx = "x" +local kx = "x" -local kTrue = true -local kFalse = false +local kTrue = true +local kFalse = false -local kNil = nil +local kNil = nil -- this code gave an error for the code checker do @@ -105,7 +105,7 @@ end, 'CLOSURE', 'NEWTABLE', 'EXTRAARG', 'GETTABUP', 'CALL', -- sequence of LOADNILs check(function () - local kNil = nil + local kNil = nil local a,b,c local d; local e; local f,g,h; @@ -173,7 +173,7 @@ end, -- "get/set table" with numeric indices check(function (a) - local k255 = 255 + local k255 = 255 a[1] = a[100] a[k255] = a[256] a[256] = 5 @@ -276,14 +276,14 @@ checkI(function () return ((100 << k6) << -4) >> 2 end, 100) -- borders around MAXARG_sBx ((((1 << 17) - 1) >> 1) == 65535) local a = 17; local sbx = ((1 << a) - 1) >> 1 -- avoid folding -local border = 65535 +local border = 65535 checkI(function () return border end, sbx) checkI(function () return -border end, -sbx) checkI(function () return border + 1 end, sbx + 1) checkK(function () return border + 2 end, sbx + 2) checkK(function () return -(border + 1) end, -(sbx + 1)) -local border = 65535.0 +local border = 65535.0 checkF(function () return border end, sbx + 0.0) checkF(function () return -border end, -sbx + 0.0) checkF(function () return border + 1 end, (sbx + 1.0)) @@ -411,9 +411,9 @@ checkequal(function () return 6 and true or nil end, do -- string constants - local k0 = "00000000000000000000000000000000000000000000000000" + local k0 = "00000000000000000000000000000000000000000000000000" local function f1 () - local k = k0 + local k = k0 return function () return function () return k end end diff --git a/testes/constructs.lua b/testes/constructs.lua index 8a549e10ad..a74a8c0412 100644 --- a/testes/constructs.lua +++ b/testes/constructs.lua @@ -211,15 +211,15 @@ assert(a==1 and b==nil) print'+'; do -- testing constants - local prog = [[local x = 10]] + local prog = [[local x = 10]] checkload(prog, "unknown attribute 'XXX'") - checkload([[local xxx = 20; xxx = 10]], + checkload([[local xxx = 20; xxx = 10]], ":1: attempt to assign to const variable 'xxx'") checkload([[ local xx; - local xxx = 20; + local xxx = 20; local yyy; local function foo () local abc = xx + yyy + xxx; @@ -228,7 +228,7 @@ do -- testing constants ]], ":6: attempt to assign to const variable 'xxx'") checkload([[ - local x = nil + local x = nil x = io.open() ]], ":2: attempt to assign to const variable 'x'") end @@ -304,7 +304,7 @@ if _ENV.GLOB1 == 0 then basiccases[2][1] = "F" -- constant false prog = [[ - local F = false + local F = false if %s then IX = true end return %s ]] @@ -312,7 +312,7 @@ else basiccases[4][1] = "k10" -- constant 10 prog = [[ - local k10 = 10 + local k10 = 10 if %s then IX = true end return %s ]] @@ -322,12 +322,12 @@ print('testing short-circuit optimizations (' .. _ENV.GLOB1 .. ')') -- operators with their respective values -local binops = { +local binops = { {" and ", function (a,b) if not a then return a else return b end end}, {" or ", function (a,b) if a then return a else return b end end}, } -local cases = {} +local cases = {} -- creates all combinations of '(cases[i] op cases[n-i])' plus -- 'not(cases[i] op cases[n-i])' (syntax + value) diff --git a/testes/coroutine.lua b/testes/coroutine.lua index 00531d8e1f..457374cab1 100644 --- a/testes/coroutine.lua +++ b/testes/coroutine.lua @@ -151,7 +151,7 @@ do end co = coroutine.create(function () - local x = func2close(function (self, err) + local x = func2close(function (self, err) assert(err == nil); X = false end) X = true @@ -165,12 +165,12 @@ do -- error closing a coroutine local x = 0 co = coroutine.create(function() - local y = func2close(function (self,err) + local y = func2close(function (self,err) if (err ~= 111) then os.exit(false) end -- should not happen x = 200 error(200) end) - local x = func2close(function (self, err) + local x = func2close(function (self, err) assert(err == nil); error(111) end) coroutine.yield() @@ -356,7 +356,7 @@ do local X = false A = coroutine.wrap(function() - local _ = setmetatable({}, {__close = function () X = true end}) + local _ = setmetatable({}, {__close = function () X = true end}) return pcall(A, 1) end) st, res = A() diff --git a/testes/files.lua b/testes/files.lua index 6e7bd9e243..585e5948d3 100644 --- a/testes/files.lua +++ b/testes/files.lua @@ -125,7 +125,7 @@ do -- closing file by scope local F = nil do - local f = assert(io.open(file, "w")) + local f = assert(io.open(file, "w")) F = f end assert(tostring(F) == "file (closed)") @@ -135,7 +135,7 @@ assert(os.remove(file)) do -- test writing/reading numbers - local f = assert(io.open(file, "w")) + local f = assert(io.open(file, "w")) f:write(maxint, '\n') f:write(string.format("0X%x\n", maxint)) f:write("0xABCp-3", '\n') @@ -144,7 +144,7 @@ do f:write(string.format("0x%X\n", -maxint)) f:write("-0xABCp-3", '\n') assert(f:close()) - local f = assert(io.open(file, "r")) + local f = assert(io.open(file, "r")) assert(f:read("n") == maxint) assert(f:read("n") == maxint) assert(f:read("n") == 0xABCp-3) @@ -158,7 +158,7 @@ assert(os.remove(file)) -- testing multiple arguments to io.read do - local f = assert(io.open(file, "w")) + local f = assert(io.open(file, "w")) f:write[[ a line another line @@ -170,18 +170,18 @@ three ]] local l1, l2, l3, l4, n1, n2, c, dummy assert(f:close()) - local f = assert(io.open(file, "r")) + local f = assert(io.open(file, "r")) l1, l2, n1, n2, dummy = f:read("l", "L", "n", "n") assert(l1 == "a line" and l2 == "another line\n" and n1 == 1234 and n2 == 3.45 and dummy == nil) assert(f:close()) - local f = assert(io.open(file, "r")) + local f = assert(io.open(file, "r")) l1, l2, n1, n2, c, l3, l4, dummy = f:read(7, "l", "n", "n", 1, "l", "l") assert(l1 == "a line\n" and l2 == "another line" and c == '\n' and n1 == 1234 and n2 == 3.45 and l3 == "one" and l4 == "two" and dummy == nil) assert(f:close()) - local f = assert(io.open(file, "r")) + local f = assert(io.open(file, "r")) -- second item failing l1, n1, n2, dummy = f:read("l", "n", "n", "l") assert(l1 == "a line" and n1 == nil) diff --git a/testes/goto.lua b/testes/goto.lua index c9e480734d..4ac6d7d089 100644 --- a/testes/goto.lua +++ b/testes/goto.lua @@ -258,7 +258,7 @@ do ::L2:: goto L3 ::L1:: do - local a = setmetatable({}, {__close = function () X = true end}) + local a = setmetatable({}, {__close = function () X = true end}) assert(X == nil) if a then goto L2 end -- jumping back out of scope of 'a' end diff --git a/testes/locals.lua b/testes/locals.lua index 73267d028f..3b145ca332 100644 --- a/testes/locals.lua +++ b/testes/locals.lua @@ -174,7 +174,7 @@ assert(x==20) do -- constants - local a, b, c = 10, 20, 30 + local a, b, c = 10, 20, 30 b = a + c + b -- 'b' is not constant assert(a == 10 and b == 60 and c == 30) local function checkro (name, code) @@ -182,17 +182,17 @@ do -- constants local gab = string.format("attempt to assign to const variable '%s'", name) assert(not st and string.find(msg, gab)) end - checkro("y", "local x, y, z = 10, 20, 30; x = 11; y = 12") - checkro("x", "local x, y, z = 10, 20, 30; x = 11") - checkro("z", "local x, y, z = 10, 20, 30; y = 10; z = 11") + checkro("y", "local x, y , z = 10, 20, 30; x = 11; y = 12") + checkro("x", "local x , y, z = 10, 20, 30; x = 11") + checkro("z", "local x , y, z = 10, 20, 30; y = 10; z = 11") checkro("z", [[ - local a, z, b = 10; + local a, z , b = 10; function foo() a = 20; z = 32; end ]]) checkro("var1", [[ - local a, var1 = 10; + local a, var1 = 10; function foo() a = 20; z = function () var1 = 12; end end ]]) end @@ -215,9 +215,9 @@ end do local a = {} do - local x = setmetatable({"x"}, {__close = function (self) + local x = setmetatable({"x"}, {__close = function (self) a[#a + 1] = self[1] end}) - local w, y, z = func2close(function (self, err) + local w, y , z = func2close(function (self, err) assert(err == nil); a[#a + 1] = "y" end, 10, 20) a[#a + 1] = "in" @@ -235,7 +235,7 @@ do -- closing functions do not corrupt returning values local function foo (x) - local _ = closescope + local _ = closescope return x, X, 23 end @@ -244,7 +244,7 @@ do X = false foo = function (x) - local _ = closescope + local _ = closescope local y = 15 return y end @@ -253,7 +253,7 @@ do X = false foo = function () - local x = closescope + local x = closescope return x end @@ -266,13 +266,13 @@ do -- calls cannot be tail in the scope of to-be-closed variables local X, Y local function foo () - local _ = func2close(function () Y = 10 end) + local _ = func2close(function () Y = 10 end) assert(X == true and Y == nil) -- 'X' not closed yet return 1,2,3 end local function bar () - local _ = func2close(function () X = false end) + local _ = func2close(function () X = false end) X = true do return foo() -- not a tail call! @@ -287,14 +287,14 @@ end do -- errors in __close local log = {} local function foo (err) - local x = + local x = func2close(function (self, msg) log[#log + 1] = msg; error(1) end) - local x1 = + local x1 = func2close(function (self, msg) log[#log + 1] = msg; end) - local gc = func2close(function () collectgarbage() end) - local y = + local gc = func2close(function () collectgarbage() end) + local y = func2close(function (self, msg) log[#log + 1] = msg; error(2) end) - local z = + local z = func2close(function (self, msg) log[#log + 1] = (msg or 10) + 1; error(3) @@ -316,7 +316,7 @@ do -- errors in __close -- error in toclose in vararg function function foo (...) - local x123 = 10 + local x123 = 10 end local st, msg = pcall(foo) @@ -329,7 +329,7 @@ do -- errors due to non-closable values local function foo () - local x = {} + local x = {} end local stat, msg = pcall(foo) assert(not stat and string.find(msg, "variable 'x'")) @@ -337,8 +337,8 @@ do -- with other errors, non-closable values are ignored local function foo () - local x = 34 - local y = func2close(function () error(32) end) + local x = 34 + local y = func2close(function () error(32) end) end local stat, msg = pcall(foo) assert(not stat and msg == 32) @@ -350,8 +350,8 @@ if rawget(_G, "T") then -- memory error inside closing function local function foo () - local y = func2close(function () T.alloccount() end) - local x = setmetatable({}, {__close = function () + local y = func2close(function () T.alloccount() end) + local x = setmetatable({}, {__close = function () T.alloccount(0); local x = {} -- force a memory error end}) error(1000) -- common error inside the function's body @@ -377,7 +377,7 @@ if rawget(_G, "T") then end local function test () - local x = enter(0) -- set a memory limit + local x = enter(0) -- set a memory limit -- creation of previous upvalue will raise a memory error assert(false) -- should not run end @@ -392,14 +392,14 @@ if rawget(_G, "T") then -- repeat test with extra closing upvalues local function test () - local xxx = func2close(function (self, msg) + local xxx = func2close(function (self, msg) assert(msg == "not enough memory"); error(1000) -- raise another error end) - local xx = func2close(function (self, msg) + local xx = func2close(function (self, msg) assert(msg == "not enough memory"); end) - local x = enter(0) -- set a memory limit + local x = enter(0) -- set a memory limit -- creation of previous upvalue will raise a memory error os.exit(false) -- should not run end @@ -469,9 +469,9 @@ do local x = false local y = false local co = coroutine.wrap(function () - local xv = func2close(function () x = true end) + local xv = func2close(function () x = true end) do - local yv = func2close(function () y = true end) + local yv = func2close(function () y = true end) coroutine.yield(100) -- yield doesn't close variable end coroutine.yield(200) -- yield doesn't close variable @@ -491,8 +491,8 @@ do -- error in a wrapped coroutine raising errors when closing a variable local x = 0 local co = coroutine.wrap(function () - local xx = func2close(function () x = x + 1; error("YYY") end) - local xv = func2close(function () x = x + 1; error("XXX") end) + local xx = func2close(function () x = x + 1; error("YYY") end) + local xv = func2close(function () x = x + 1; error("XXX") end) coroutine.yield(100) error(200) end) @@ -503,8 +503,8 @@ do local x = 0 local y = 0 co = coroutine.wrap(function () - local xx = func2close(function () y = y + 1; error("YYY") end) - local xv = func2close(function () x = x + 1; error("XXX") end) + local xx = func2close(function () y = y + 1; error("YYY") end) + local xv = func2close(function () x = x + 1; error("XXX") end) coroutine.yield(100) return 200 end) @@ -519,7 +519,7 @@ end -- a suspended coroutine should not close its variables when collected local co co = coroutine.wrap(function() - local x = function () os.exit(false) end -- should not run + local x = function () os.exit(false) end -- should not run co = nil coroutine.yield() end) diff --git a/testes/main.lua b/testes/main.lua index 4c09645a3c..da2a928858 100644 --- a/testes/main.lua +++ b/testes/main.lua @@ -320,11 +320,11 @@ NoRun("", "lua %s", prog) -- no message -- to-be-closed variables in main chunk prepfile[[ - local x = function (err) + local x = function (err) assert(err == 120) print("Ok") end - local e1 = function () error(120) end + local e1 = function () error(120) end os.exit(true, true) ]] RUN('lua %s > %s', prog, out) diff --git a/testes/math.lua b/testes/math.lua index d0aaa6a5fb..bad4390188 100644 --- a/testes/math.lua +++ b/testes/math.lua @@ -3,10 +3,10 @@ print("testing numbers and math lib") -local minint = math.mininteger -local maxint = math.maxinteger +local minint = math.mininteger +local maxint = math.maxinteger -local intbits = math.floor(math.log(maxint, 2) + 0.5) + 1 +local intbits = math.floor(math.log(maxint, 2) + 0.5) + 1 assert((1 << intbits) == 0) assert(minint == 1 << (intbits - 1)) @@ -270,7 +270,7 @@ else end do - local NaN = 0/0 + local NaN = 0/0 assert(not (NaN < 0)) assert(not (NaN > minint)) assert(not (NaN <= -9)) @@ -767,8 +767,8 @@ assert(a == '10' and b == '20') do print("testing -0 and NaN") - local mz = -0.0 - local z = 0.0 + local mz = -0.0 + local z = 0.0 assert(mz == z) assert(1/mz < 0 and 0 < 1/z) local a = {[mz] = 1} @@ -776,18 +776,18 @@ do a[z] = 2 assert(a[z] == 2 and a[mz] == 2) local inf = math.huge * 2 + 1 - local mz = -1/inf - local z = 1/inf + local mz = -1/inf + local z = 1/inf assert(mz == z) assert(1/mz < 0 and 0 < 1/z) - local NaN = inf - inf + local NaN = inf - inf assert(NaN ~= NaN) assert(not (NaN < NaN)) assert(not (NaN <= NaN)) assert(not (NaN > NaN)) assert(not (NaN >= NaN)) assert(not (0 < NaN) and not (NaN < 0)) - local NaN1 = 0/0 + local NaN1 = 0/0 assert(NaN ~= NaN1 and not (NaN <= NaN1) and not (NaN1 <= NaN)) local a = {} assert(not pcall(rawset, a, NaN, 1)) @@ -816,8 +816,8 @@ end -- the first call after seed 1007 should return 0x7a7040a5a323c9d6 do -- all computations should work with 32-bit integers - local h = 0x7a7040a5 -- higher half - local l = 0xa323c9d6 -- lower half + local h = 0x7a7040a5 -- higher half + local l = 0xa323c9d6 -- lower half math.randomseed(1007) -- get the low 'intbits' of the 64-bit expected result diff --git a/testes/strings.lua b/testes/strings.lua index aa039c4fc2..2e0e160f23 100644 --- a/testes/strings.lua +++ b/testes/strings.lua @@ -3,8 +3,8 @@ print('testing strings and string library') -local maxi = math.maxinteger -local mini = math.mininteger +local maxi = math.maxinteger +local mini = math.mininteger local function checkerror (msg, f, ...) From 35b4efc270db2418bc2cac6671575a45028061c3 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 30 Jul 2019 13:48:40 -0300 Subject: [PATCH 0475/1145] Fixed test in 'main.lua' The test "to-be-closed variables in main chunk" was broken, as it used the removed feature of functions as to-be-closed values. The error was not detected because its expected result had no lines to be checked (due to missing new lines). --- testes/main.lua | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/testes/main.lua b/testes/main.lua index da2a928858..b224b54e6d 100644 --- a/testes/main.lua +++ b/testes/main.lua @@ -43,6 +43,8 @@ local function getoutput () end local function checkprogout (s) + -- expected result must end with new line + assert(string.sub(s, -1) == "\n") local t = getoutput() for line in string.gmatch(s, ".-\n") do assert(string.find(t, line, 1, true)) @@ -292,7 +294,7 @@ debug = require"debug" print(debug.getinfo(1).currentline) ]] RUN('lua %s > %s', prog, out) -checkprogout('3') +checkprogout('3\n') -- close Lua with an open file prepfile(string.format([[io.output(%q); io.write('alo')]], out)) @@ -320,15 +322,16 @@ NoRun("", "lua %s", prog) -- no message -- to-be-closed variables in main chunk prepfile[[ - local x = function (err) - assert(err == 120) - print("Ok") - end - local e1 = function () error(120) end + local x = setmetatable({}, + {__close = function (self, err) + assert(err == nil) + print("Ok") + end}) + local e1 = setmetatable({}, {__close = function () print(120) end}) os.exit(true, true) ]] RUN('lua %s > %s', prog, out) -checkprogout("Ok") +checkprogout("120\nOk\n") -- remove temporary files From f645d3157372c73573dff221c5b26691cb0e7d56 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 31 Jul 2019 10:43:51 -0300 Subject: [PATCH 0476/1145] To-be-closed variables must be closed on initialization When initializing a to-be-closed variable, check whether it has a '__close' metamethod (or is a false value) and raise an error if if it hasn't. This produces more accurate error messages. (The check before closing still need to be done: in the C API, the value is not constant; and the object may lose its '__close' metamethod during the block.) --- lfunc.c | 49 +++++++++++++++++++++++++++++++---------------- ltests.c | 3 +++ lvm.c | 6 ++---- manual/manual.of | 19 ++++++++++-------- testes/api.lua | 11 ++++++++++- testes/locals.lua | 23 +++++++++++----------- 6 files changed, 70 insertions(+), 41 deletions(-) diff --git a/lfunc.c b/lfunc.c index 6f2f897fed..8f39f6b076 100644 --- a/lfunc.c +++ b/lfunc.c @@ -123,12 +123,24 @@ static int prepclosingmethod (lua_State *L, TValue *obj, TValue *err) { } +/* +** Raise an error with message 'msg', inserting the name of the +** local variable at position 'level' in the stack. +*/ +static void varerror (lua_State *L, StkId level, const char *msg) { + int idx = cast_int(level - L->ci->func); + const char *vname = luaG_findlocal(L, L->ci, idx, NULL); + if (vname == NULL) vname = "?"; + luaG_runerror(L, msg, vname); +} + + /* ** Prepare and call a closing method. If status is OK, code is still ** inside the original protected call, and so any error will be handled -** there. Otherwise, a previous error already activated original +** there. Otherwise, a previous error already activated the original ** protected call, and so the call to the closing method must be -** protected here. (A status = CLOSEPROTECT behaves like a previous +** protected here. (A status == CLOSEPROTECT behaves like a previous ** error, to also run the closing method in protected mode). ** If status is OK, the call to the closing method will be pushed ** at the top of the stack. Otherwise, values are pushed after @@ -140,12 +152,8 @@ static int callclosemth (lua_State *L, StkId level, int status) { if (likely(status == LUA_OK)) { if (prepclosingmethod(L, uv, &G(L)->nilvalue)) /* something to call? */ callclose(L, NULL); /* call closing method */ - else if (!ttisnil(uv)) { /* non-closable non-nil value? */ - int idx = cast_int(level - L->ci->func); - const char *vname = luaG_findlocal(L, L->ci, idx, NULL); - if (vname == NULL) vname = "?"; - luaG_runerror(L, "attempt to close non-closable variable '%s'", vname); - } + else if (!l_isfalse(uv)) /* non-closable non-false value? */ + varerror(L, level, "attempt to close non-closable variable '%s'"); } else { /* must close the object in protected mode */ ptrdiff_t oldtop; @@ -170,9 +178,7 @@ static int callclosemth (lua_State *L, StkId level, int status) { ** (can raise a memory-allocation error) */ static void trynewtbcupval (lua_State *L, void *ud) { - StkId level = cast(StkId, ud); - lua_assert(L->openupval == NULL || uplevel(L->openupval) < level); - newupval(L, 1, level, &L->openupval); + newupval(L, 1, cast(StkId, ud), &L->openupval); } @@ -182,13 +188,22 @@ static void trynewtbcupval (lua_State *L, void *ud) { ** as there is no upvalue to call it later. */ void luaF_newtbcupval (lua_State *L, StkId level) { - int status = luaD_rawrunprotected(L, trynewtbcupval, level); - if (unlikely(status != LUA_OK)) { /* memory error creating upvalue? */ - lua_assert(status == LUA_ERRMEM); - luaD_seterrorobj(L, LUA_ERRMEM, level + 1); /* save error message */ - if (prepclosingmethod(L, s2v(level), s2v(level + 1))) + TValue *obj = s2v(level); + lua_assert(L->openupval == NULL || uplevel(L->openupval) < level); + if (!l_isfalse(obj)) { /* false doesn't need to be closed */ + int status; + const TValue *tm = luaT_gettmbyobj(L, obj, TM_CLOSE); + if (ttisnil(tm)) /* no metamethod? */ + varerror(L, level, "variable '%s' got a non-closable value"); + status = luaD_rawrunprotected(L, trynewtbcupval, level); + if (unlikely(status != LUA_OK)) { /* memory error creating upvalue? */ + lua_assert(status == LUA_ERRMEM); + luaD_seterrorobj(L, LUA_ERRMEM, level + 1); /* save error message */ + /* next call must succeed, as object is closable */ + prepclosingmethod(L, s2v(level), s2v(level + 1)); callclose(L, NULL); /* call closing method */ - luaD_throw(L, LUA_ERRMEM); /* throw memory error */ + luaD_throw(L, LUA_ERRMEM); /* throw memory error */ + } } } diff --git a/ltests.c b/ltests.c index 09876ee7e6..21273ea9a0 100644 --- a/ltests.c +++ b/ltests.c @@ -1572,6 +1572,9 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) { else if EQ("error") { lua_error(L1); } + else if EQ("abort") { + abort(); + } else if EQ("throw") { #if defined(__cplusplus) static struct X { int x; } x; diff --git a/lvm.c b/lvm.c index f177ce6a21..1cfc10358f 100644 --- a/lvm.c +++ b/lvm.c @@ -1739,10 +1739,8 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_TFORPREP) { - if (!ttisnil(s2v(ra + 3))) { /* is 'toclose' not nil? */ - /* create to-be-closed upvalue for it */ - halfProtect(luaF_newtbcupval(L, ra + 3)); - } + /* create to-be-closed upvalue (if needed) */ + halfProtect(luaF_newtbcupval(L, ra + 3)); pc += GETARG_Bx(i); i = *(pc++); /* go to next instruction */ lua_assert(GET_OPCODE(i) == OP_TFORCALL && ra == RA(i)); diff --git a/manual/manual.of b/manual/manual.of index 1646f1133f..8eebe9cb9e 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -1534,15 +1534,17 @@ or exiting by an error. Here, to @emph{close} a value means to call its @idx{__close} metamethod. -If the value is @nil, it is ignored; -otherwise, -if it does not have a @idx{__close} metamethod, -an error is raised. When calling the metamethod, the value itself is passed as the first argument -and the error object (if any) is passed as a second argument; +and the error object that caused the exit (if any) +is passed as a second argument; if there was no error, the second argument is @nil. +The value assigned to a to-be-closed variable +must have a @idx{__close} metamethod +or be a false value. +(@nil and @false are ignored as to-be-closed values.) + If several to-be-closed variables go out of scope at the same event, they are closed in the reverse order that they were declared. @@ -2917,8 +2919,9 @@ it is left unchanged. @APIEntry{void lua_close (lua_State *L);| @apii{0,0,-} -Destroys all objects in the given Lua state -(calling the corresponding garbage-collection metamethods, if any) +Close all active to-be-closed variables in the main thread, +release all objects in the given Lua state +(calling the corresponding garbage-collection metamethods, if any), and frees all dynamic memory used by this state. On several platforms, you may not need to call this function, @@ -4186,7 +4189,7 @@ An index marked as to-be-closed should not be removed from the stack by any other function in the API except @Lid{lua_settop} or @Lid{lua_pop}. This function should not be called for an index -that is equal to or below an already marked to-be-closed index. +that is equal to or below an active to-be-closed index. This function can raise an out-of-memory error. In that case, the value in the given index is immediately closed, diff --git a/testes/api.lua b/testes/api.lua index 3f7f7596a3..f6915c3e06 100644 --- a/testes/api.lua +++ b/testes/api.lua @@ -1096,7 +1096,7 @@ do assert(type(a[1]) == "string" and a[2][1] == 11) assert(#openresource == 0) -- was closed - -- error + -- closing by error local a, b = pcall(T.makeCfunc[[ call 0 1 # create resource toclose -1 # mark it to be closed @@ -1105,6 +1105,15 @@ do assert(a == false and b[1] == 11) assert(#openresource == 0) -- was closed + -- non-closable value + local a, b = pcall(T.makeCfunc[[ + newtable # create non-closable object + toclose -1 # mark it to be closed (shoud raise an error) + abort # will not be executed + ]]) + assert(a == false and + string.find(b, "non%-closable value")) + local function check (n) assert(#openresource == n) end diff --git a/testes/locals.lua b/testes/locals.lua index 3b145ca332..6eb1ba0e1b 100644 --- a/testes/locals.lua +++ b/testes/locals.lua @@ -215,11 +215,13 @@ end do local a = {} do + local b = false -- not to be closed local x = setmetatable({"x"}, {__close = function (self) a[#a + 1] = self[1] end}) local w, y , z = func2close(function (self, err) assert(err == nil); a[#a + 1] = "y" end, 10, 20) + local c = nil -- not to be closed a[#a + 1] = "in" assert(w == 10 and z == 20) end @@ -325,24 +327,22 @@ do -- errors in __close end -do - - -- errors due to non-closable values +do -- errors due to non-closable values local function foo () local x = {} + os.exit(false) -- should not run end local stat, msg = pcall(foo) - assert(not stat and string.find(msg, "variable 'x'")) + assert(not stat and + string.find(msg, "variable 'x' got a non%-closable value")) - - -- with other errors, non-closable values are ignored local function foo () - local x = 34 - local y = func2close(function () error(32) end) + local xyz = setmetatable({}, {__close = print}) + getmetatable(xyz).__close = nil -- remove metamethod end local stat, msg = pcall(foo) - assert(not stat and msg == 32) - + assert(not stat and + string.find(msg, "attempt to close non%-closable variable 'xyz'")) end @@ -519,7 +519,8 @@ end -- a suspended coroutine should not close its variables when collected local co co = coroutine.wrap(function() - local x = function () os.exit(false) end -- should not run + -- should not run + local x = func2close(function () os.exit(false) end) co = nil coroutine.yield() end) From fe040633a1d64af4c19acc4707adb47413a3cd4a Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 31 Jul 2019 11:22:39 -0300 Subject: [PATCH 0477/1145] Tracebacks recognize metamethods '__close' --- ldebug.c | 3 +++ testes/locals.lua | 21 +++++++++++++++++---- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/ldebug.c b/ldebug.c index acaa653ad6..9593039bf0 100644 --- a/ldebug.c +++ b/ldebug.c @@ -651,6 +651,9 @@ static const char *funcnamefromcode (lua_State *L, CallInfo *ci, case OP_SHRI: case OP_SHLI: *name = "shift"; return "metamethod"; + case OP_CLOSE: case OP_RETURN: + *name = "close"; + return "metamethod"; default: return NULL; /* cannot find a reasonable name */ } diff --git a/testes/locals.lua b/testes/locals.lua index 6eb1ba0e1b..99fa79cd45 100644 --- a/testes/locals.lua +++ b/testes/locals.lua @@ -316,14 +316,27 @@ do -- errors in __close assert(log[1] == 5 and log[2] == 4 and log[3] == 4 and log[4] == 4 and #log == 4) + -- error leaving a block + local function foo (...) + do + local x1 = func2close(function () error("Y") end) + local x123 = func2close(function () error("X") end) + end + end + + local st, msg = xpcall(foo, debug.traceback) + assert(string.match(msg, "^[^ ]* X")) + assert(string.find(msg, "in metamethod 'close'")) + -- error in toclose in vararg function - function foo (...) - local x123 = 10 + local function foo (...) + local x123 = func2close(function () error("X") end) end - local st, msg = pcall(foo) - assert(string.find(msg, "'x123'")) + local st, msg = xpcall(foo, debug.traceback) + assert(string.match(msg, "^[^ ]* X")) + assert(string.find(msg, "in metamethod 'close'")) end From 223bb04090344b1972dc2a7910a54b46210f0d40 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 31 Jul 2019 11:41:59 -0300 Subject: [PATCH 0478/1145] Correction in the documentation of 'io.lines' The loop does not end on end of file, but when the iterator function fails to read a value. (In particular, the format "a" never fails, so a loop with 'io.lines(fname, "a")' never ends.) --- liolib.c | 2 +- manual/manual.of | 10 ++++------ 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/liolib.c b/liolib.c index 83fbb1727f..56507d5ea5 100644 --- a/liolib.c +++ b/liolib.c @@ -624,7 +624,7 @@ static int io_readline (lua_State *L) { lua_pushvalue(L, lua_upvalueindex(3 + i)); n = g_read(L, p->f, 2); /* 'n' is number of results */ lua_assert(n > 0); /* should return at least a nil */ - if (lua_toboolean(L, -n)) /* read at least one value? */ + if (!lua_isnil(L, -n)) /* read at least one value? */ return n; /* return them */ else { /* first result is nil: EOF or error */ if (n > 1) { /* is there error information? */ diff --git a/manual/manual.of b/manual/manual.of index 8eebe9cb9e..c1ee8eb79b 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -7926,8 +7926,8 @@ instead of returning an error code. Opens the given file name in read mode and returns an iterator function that works like @T{file:lines(@Cdots)} over the opened file. -When the iterator function detects the end of file, -it returns no values (to finish the loop) and automatically closes the file. +When the iterator function fails to read any value, +it automatically closes the file. Besides the iterator function, @id{io.lines} returns three other values: two @nil values as placeholders, @@ -7941,7 +7941,8 @@ to @T{io.input():lines("l")}; that is, it iterates over the lines of the default input file. In this case, the iterator does not close the file when the loop ends. -In case of errors this function raises the error, +In case of errors opening the file, +this function raises the error, instead of returning an error code. } @@ -8053,9 +8054,6 @@ starting at the current position. Unlike @Lid{io.lines}, this function does not close the file when the loop ends. -In case of errors this function raises the error, -instead of returning an error code. - } @LibEntry{file:read (@Cdots)| From 35a28a58b38fb90cbe7c9596d60af2b3c1bd52a3 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 1 Aug 2019 14:11:33 -0300 Subject: [PATCH 0479/1145] Details - removed rule about RCS from makefile - comments and nitpicking in 'llex.c' --- llex.c | 20 +++++++++++++------- makefile | 1 - 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/llex.c b/llex.c index d99d9015b6..f88057fe73 100644 --- a/llex.c +++ b/llex.c @@ -211,8 +211,16 @@ static int check_next2 (LexState *ls, const char *set) { /* LUA_NUMBER */ /* -** this function is quite liberal in what it accepts, as 'luaO_str2num' -** will reject ill-formed numerals. +** This function is quite liberal in what it accepts, as 'luaO_str2num' +** will reject ill-formed numerals. Roughly, it accepts the following +** pattern: +** +** %d(%x|%.|([Ee][+-]?))* | 0[Xx](%x|%.|([Pp][+-]?))* +** +** The only tricky part is to accept [+-] only after a valid exponent +** mark, to avoid reading '3-4' or '0xe+1' as a single number. +** +** The caller might have already read an initial dot. */ static int read_numeral (LexState *ls, SemInfo *seminfo) { TValue obj; @@ -223,15 +231,13 @@ static int read_numeral (LexState *ls, SemInfo *seminfo) { if (first == '0' && check_next2(ls, "xX")) /* hexadecimal? */ expo = "Pp"; for (;;) { - if (check_next2(ls, expo)) /* exponent part? */ + if (check_next2(ls, expo)) /* exponent mark? */ check_next2(ls, "-+"); /* optional exponent sign */ - if (lisxdigit(ls->current)) - save_and_next(ls); - else if (ls->current == '.') + else if (lisxdigit(ls->current) || ls->current == '.') /* '%x|%.' */ save_and_next(ls); else break; } - if (lislalnum(ls->current)) /* is numeral touching an alpha num? */ + if (lislalpha(ls->current)) /* is numeral touching a letter? */ save_and_next(ls); /* force an error */ save(ls, '\0'); if (luaO_str2num(luaZ_buffer(ls->buff), &obj) == 0) /* format error? */ diff --git a/makefile b/makefile index cb6cece86d..cf238aeb22 100644 --- a/makefile +++ b/makefile @@ -107,7 +107,6 @@ $(LUAC_T): $(LUAC_O) $(CORE_T) $(CC) -o $@ $(MYLDFLAGS) $(LUAC_O) $(CORE_T) $(LIBS) $(MYLIBS) clean: - rcsclean -u $(RM) $(ALL_T) $(ALL_O) depend: From 09b4e527a008687a2fd90202b5e7b2f175d8ebed Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 12 Aug 2019 11:26:08 -0300 Subject: [PATCH 0480/1145] Detail in the manual (method 'file:setvbuf') ANSI C is vague about 'setvbuf'; most details are implementation defined. So, the manual cannot give any guaranties, either. --- manual/manual.of | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/manual/manual.of b/manual/manual.of index c1ee8eb79b..ff27a7d42d 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -8142,24 +8142,12 @@ end of the file, and returns its size. @LibEntry{file:setvbuf (mode [, size])| -Sets the buffering mode for an output file. +Sets the buffering mode for a file. There are three available modes: @description{ - -@item{@St{no}| -no buffering; the result of any output operation appears immediately. -} - -@item{@St{full}| -full buffering; output operation is performed only -when the buffer is full or when -you explicitly @T{flush} the file @seeF{io.flush}. -} - -@item{@St{line}| -line buffering; output is buffered until a newline is output -or there is any input from some special files -(such as a terminal device). +@item{@St{no}| no buffering.} +@item{@St{full}| full buffering.} +@item{@St{line}| line buffering.} } } @@ -8167,6 +8155,10 @@ For the last two cases, @id{size} is a hint for the size of the buffer, in bytes. The default is an appropriate size. +The specific behavior of each mode is non portable; +check the underlying @ANSI{setvbuf} in your platform for +more details. + } @LibEntry{file:write (@Cdots)| From f64a1b175a5fa65434a073e6d071b32bb7b0ab69 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 12 Aug 2019 15:32:44 -0300 Subject: [PATCH 0481/1145] Small optimization in 'convergeephemerons' When converging marks on ephemeron tables, change the direction the tables are traversed at each iteration, to try to avoid bad-case scenarios with linked lists of entries in a table. --- lgc.c | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/lgc.c b/lgc.c index b7220cf186..75670c0a6a 100644 --- a/lgc.c +++ b/lgc.c @@ -413,13 +413,13 @@ static void traverseweakvalue (global_State *g, Table *h) { ** (in the atomic phase). In generational mode, it (like all visited ** tables) must be kept in some gray list for post-processing. */ -static int traverseephemeron (global_State *g, Table *h) { +static int traverseephemeron (global_State *g, Table *h, int inv) { int marked = 0; /* true if an object is marked in this traversal */ int hasclears = 0; /* true if table has white keys */ int hasww = 0; /* true if table has entry "white-key -> white-value" */ - Node *n, *limit = gnodelast(h); unsigned int i; unsigned int asize = luaH_realasize(h); + unsigned int nsize = sizenode(h); /* traverse array part */ for (i = 0; i < asize; i++) { if (valiswhite(&h->array[i])) { @@ -427,8 +427,10 @@ static int traverseephemeron (global_State *g, Table *h) { reallymarkobject(g, gcvalue(&h->array[i])); } } - /* traverse hash part */ - for (n = gnode(h, 0); n < limit; n++) { + /* traverse hash part; if 'inv', traverse descending + (see 'convergeephemerons') */ + for (i = 0; i < nsize; i++) { + Node *n = inv ? gnode(h, nsize - 1 - i) : gnode(h, i); if (isempty(gval(n))) /* entry is empty? */ clearkey(n); /* clear its key */ else if (iscleared(g, gckeyN(n))) { /* key is not marked (yet)? */ @@ -490,7 +492,7 @@ static lu_mem traversetable (global_State *g, Table *h) { if (!weakkey) /* strong keys? */ traverseweakvalue(g, h); else if (!weakvalue) /* strong values? */ - traverseephemeron(g, h); + traverseephemeron(g, h, 0); else /* all weak */ linkgclist(h, g->allweak); /* nothing to traverse now */ } @@ -620,21 +622,30 @@ static lu_mem propagateall (global_State *g) { } +/* +** Traverse all ephemeron tables propagating marks from keys to values. +** Repeat until it converges, that is, nothing new is marked. 'dir' +** inverts the direction of the traversals, trying to speed up +** convergence on chains in the same table. +** +*/ static void convergeephemerons (global_State *g) { int changed; + int dir = 0; do { GCObject *w; GCObject *next = g->ephemeron; /* get ephemeron list */ g->ephemeron = NULL; /* tables may return to this list when traversed */ changed = 0; - while ((w = next) != NULL) { - next = gco2t(w)->gclist; - if (traverseephemeron(g, gco2t(w))) { /* traverse marked some value? */ + while ((w = next) != NULL) { /* for each ephemeron table */ + next = gco2t(w)->gclist; /* list is rebuilt during loop */ + if (traverseephemeron(g, gco2t(w), dir)) { /* marked some value? */ propagateall(g); /* propagate changes */ changed = 1; /* will have to revisit all ephemeron tables */ } } - } while (changed); + dir = !dir; /* invert direction next time */ + } while (changed); /* repeat until no more changes */ } /* }====================================================== */ From a1d8eb27431c02c4529be1efd92143ad65434f3a Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 15 Aug 2019 13:44:36 -0300 Subject: [PATCH 0482/1145] Added control messages to warnings Added the concept of control messages to the warning system, plus the implementation of the controls "@on"/"@off" to turn warnings on/off. Moreover, the warning system in the test library adds some other controls to ease the test of warnings. --- lauxlib.c | 34 +++++++++++++++++-------- lbaselib.c | 4 +-- ltests.c | 64 +++++++++++++++++++++++++++++++++++------------- lua.c | 36 ++++++++++++++++++--------- manual/manual.of | 29 ++++++++++++++++------ testes/all.lua | 4 +++ testes/api.lua | 2 ++ testes/gc.lua | 8 +++++- testes/main.lua | 29 ++++++++++++++++++++++ 9 files changed, 161 insertions(+), 49 deletions(-) diff --git a/lauxlib.c b/lauxlib.c index e3a7a5778b..ba1980b7c4 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -1002,29 +1002,43 @@ static int panic (lua_State *L) { /* -** Emit a warning. '*previoustocont' signals whether previous message -** was to be continued by the current one. +** Emit a warning. '*warnstate' means: +** 0 - warning system is off; +** 1 - ready to start a new message; +** 2 - previous message is to be continued. */ static void warnf (void *ud, const char *message, int tocont) { - int *previoustocont = (int *)ud; - if (!*previoustocont) /* previous message was the last? */ + int *warnstate = (int *)ud; + if (*warnstate != 2 && !tocont && *message == '@') { /* control message? */ + if (strcmp(message + 1, "off") == 0) + *warnstate = 0; + else if (strcmp(message + 1, "on") == 0) + *warnstate = 1; + return; + } + else if (*warnstate == 0) /* warnings off? */ + return; + if (*warnstate == 1) /* previous message was the last? */ lua_writestringerror("%s", "Lua warning: "); /* start a new warning */ lua_writestringerror("%s", message); /* write message */ - if (!tocont) /* is this the last part? */ + if (tocont) /* not the last part? */ + *warnstate = 2; /* to be continued */ + else { /* last part */ lua_writestringerror("%s", "\n"); /* finish message with end-of-line */ - *previoustocont = tocont; + *warnstate = 1; /* ready to start a new message */ + } } LUALIB_API lua_State *luaL_newstate (void) { lua_State *L = lua_newstate(l_alloc, NULL); if (L) { - int *previoustocont; /* space for warning state */ + int *warnstate; /* space for warning state */ lua_atpanic(L, &panic); - previoustocont = (int *)lua_newuserdatauv(L, sizeof(int), 0); + warnstate = (int *)lua_newuserdatauv(L, sizeof(int), 0); luaL_ref(L, LUA_REGISTRYINDEX); /* make sure it won't be collected */ - *previoustocont = 0; /* next message starts a new warning */ - lua_setwarnf(L, warnf, previoustocont); + *warnstate = 1; /* next message starts a new warning */ + lua_setwarnf(L, warnf, warnstate); } return L; } diff --git a/lbaselib.c b/lbaselib.c index 4724e75990..c68e6d3893 100644 --- a/lbaselib.c +++ b/lbaselib.c @@ -48,9 +48,9 @@ static int luaB_warn (lua_State *L) { luaL_checkstring(L, 1); /* at least one argument */ for (i = 2; i <= n; i++) luaL_checkstring(L, i); /* make sure all arguments are strings */ - for (i = 1; i <= n; i++) /* compose warning */ + for (i = 1; i < n; i++) /* compose warning */ lua_warning(L, lua_tostring(L, i), 1); - lua_warning(L, "", 0); /* close warning */ + lua_warning(L, lua_tostring(L, n), 0); /* close warning */ return 0; } diff --git a/ltests.c b/ltests.c index 21273ea9a0..fd55fc31a8 100644 --- a/ltests.c +++ b/ltests.c @@ -79,32 +79,62 @@ static int tpanic (lua_State *L) { /* ** Warning function for tests. Fist, it concatenates all parts of -** a warning in buffer 'buff'. Then: -** - messages starting with '#' are shown on standard output (used to -** test explicit warnings); -** - messages containing '@' are stored in global '_WARN' (used to test -** errors that generate warnings); +** a warning in buffer 'buff'. Then, it has three modes: +** - 0.normal: messages starting with '#' are shown on standard output; ** - other messages abort the tests (they represent real warning ** conditions; the standard tests should not generate these conditions -** unexpectedly). +** unexpectedly); +** - 1.allow: all messages are shown; +** - 2.store: all warnings go to the global '_WARN'; */ static void warnf (void *ud, const char *msg, int tocont) { static char buff[200] = ""; /* should be enough for tests... */ + static int onoff = 1; + static int mode = 0; /* start in normal mode */ + static int lasttocont = 0; + if (!lasttocont && !tocont && *msg == '@') { /* control message? */ + if (buff[0] != '\0') + badexit("Control warning during warning: %s\naborting...\n", msg); + if (strcmp(msg + 1, "off") == 0) + onoff = 0; + else if (strcmp(msg + 1, "on") == 0) + onoff = 1; + else if (strcmp(msg + 1, "normal") == 0) + mode = 0; + else if (strcmp(msg + 1, "allow") == 0) + mode = 1; + else if (strcmp(msg + 1, "store") == 0) + mode = 2; + else + badexit("Invalid control warning in test mode: %s\naborting...\n", msg); + return; + } + lasttocont = tocont; if (strlen(msg) >= sizeof(buff) - strlen(buff)) badexit("%s", "warnf-buffer overflow"); strcat(buff, msg); /* add new message to current warning */ if (!tocont) { /* message finished? */ - if (buff[0] == '#') /* expected warning? */ - printf("Expected Lua warning: %s\n", buff); /* print it */ - else if (strchr(buff, '@') != NULL) { /* warning for test purposes? */ - lua_State *L = cast(lua_State *, ud); - lua_unlock(L); - lua_pushstring(L, buff); - lua_setglobal(L, "_WARN"); /* assign message to global '_WARN' */ - lua_lock(L); - } - else /* a real warning; should not happen during tests */ - badexit("Unexpected warning in test mode: %s\naborting...\n", buff); + switch (mode) { + case 0: { /* normal */ + if (buff[0] != '#' && onoff) /* unexpected warning? */ + badexit("Unexpected warning in test mode: %s\naborting...\n", buff); + /* else */ /* FALLTHROUGH */ + } + case 1: { /* allow */ + if (onoff) + fprintf(stderr, "Lua warning: %s\n", buff); /* print warning */ + break; + } + case 2: { /* store */ + lua_State *L = cast(lua_State *, ud); + lua_unlock(L); + lua_pushstring(L, buff); + lua_setglobal(L, "_WARN"); /* assign message to global '_WARN' */ + lua_lock(L); + buff[0] = '\0'; /* prepare buffer for next warning */ + break; + } + } buff[0] = '\0'; /* prepare buffer for next warning */ } } diff --git a/lua.c b/lua.c index fa534ba2b4..d13e203b07 100644 --- a/lua.c +++ b/lua.c @@ -73,6 +73,7 @@ static void print_usage (const char *badoption) { " -l name require library 'name' into global 'name'\n" " -v show version information\n" " -E ignore environment variables\n" + " -q turn warnings off\n" " -- stop handling options\n" " - stop handling options and execute stdin\n" , @@ -259,14 +260,18 @@ static int collectargs (char **argv, int *first) { case '\0': /* '-' */ return args; /* script "name" is '-' */ case 'E': - if (argv[i][2] != '\0') /* extra characters after 1st? */ + if (argv[i][2] != '\0') /* extra characters? */ return has_error; /* invalid option */ args |= has_E; break; + case 'q': + if (argv[i][2] != '\0') /* extra characters? */ + return has_error; /* invalid option */ + break; case 'i': args |= has_i; /* (-i implies -v) *//* FALLTHROUGH */ case 'v': - if (argv[i][2] != '\0') /* extra characters after 1st? */ + if (argv[i][2] != '\0') /* extra characters? */ return has_error; /* invalid option */ args |= has_v; break; @@ -289,7 +294,8 @@ static int collectargs (char **argv, int *first) { /* -** Processes options 'e' and 'l', which involve running Lua code. +** Processes options 'e' and 'l', which involve running Lua code, and +** 'q', which also affects the state. ** Returns 0 if some code raises an error. */ static int runargs (lua_State *L, char **argv, int n) { @@ -297,15 +303,21 @@ static int runargs (lua_State *L, char **argv, int n) { for (i = 1; i < n; i++) { int option = argv[i][1]; lua_assert(argv[i][0] == '-'); /* already checked */ - if (option == 'e' || option == 'l') { - int status; - const char *extra = argv[i] + 2; /* both options need an argument */ - if (*extra == '\0') extra = argv[++i]; - lua_assert(extra != NULL); - status = (option == 'e') - ? dostring(L, extra, "=(command line)") - : dolibrary(L, extra); - if (status != LUA_OK) return 0; + switch (option) { + case 'e': case 'l': { + int status; + const char *extra = argv[i] + 2; /* both options need an argument */ + if (*extra == '\0') extra = argv[++i]; + lua_assert(extra != NULL); + status = (option == 'e') + ? dostring(L, extra, "=(command line)") + : dolibrary(L, extra); + if (status != LUA_OK) return 0; + break; + } + case 'q': + lua_warning(L, "@off", 0); /* no warnings */ + break; } } return 1; diff --git a/manual/manual.of b/manual/manual.of index ff27a7d42d..8c71c61384 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -4370,6 +4370,8 @@ The third parameter is a boolean that indicates whether the message is to be continued by the message in the next call. +See @Lid{warn} for more details about warnings. + } @APIEntry{ @@ -4380,6 +4382,8 @@ Emits a warning with the given message. A message in a call with @id{tocont} true should be continued in another call to this function. +See @Lid{warn} for more details about warnings. + } @APIEntry{ @@ -6355,6 +6359,16 @@ The current value of this variable is @St{Lua 5.4}. Emits a warning with a message composed by the concatenation of all its arguments (which should be strings). +By convention, +a one-piece message starting with @Char{@At} +is intended to be a @emph{control message}, +which is a message to the warning system itself. +In particular, the standard warning function in Lua +recognizes the control messages @St{@At{}off}, +to stop the emission of warnings, +and @St{@At{}on}, to (re)start the emission; +it ignores unknown control messages. + } @LibEntry{xpcall (f, msgh [, arg1, @Cdots])| @@ -7293,7 +7307,7 @@ stored as the first capture, and therefore has @N{number 1}; the character matching @St{.} is captured with @N{number 2}, and the part matching @St{%s*} has @N{number 3}. -As a special case, the empty capture @T{()} captures +As a special case, the capture @T{()} captures the current string position (a number). For instance, if we apply the pattern @T{"()aa()"} on the string @T{"flaaap"}, there will be two captures: @N{3 and 5}. @@ -7858,7 +7872,6 @@ they are compared as @x{unsigned integers}. } - @sect2{iolib| @title{Input and Output Facilities} The I/O library provides two different styles for file manipulation. @@ -8150,7 +8163,6 @@ There are three available modes: @item{@St{line}| line buffering.} } -} For the last two cases, @id{size} is a hint for the size of the buffer, in bytes. The default is an appropriate size. @@ -8708,6 +8720,7 @@ The options are: @item{@T{-i}| enters interactive mode after running @rep{script};} @item{@T{-v}| prints version information;} @item{@T{-E}| ignores environment variables;} +@item{@T{-q}| turn warnings off;} @item{@T{--}| stops handling options;} @item{@T{-}| executes @id{stdin} as a file and stops handling options.} } @@ -8733,12 +8746,13 @@ setting the values of @Lid{package.path} and @Lid{package.cpath} with the default paths defined in @id{luaconf.h}. -All options are handled in order, except @T{-i} and @T{-E}. +The options @T{-e}, @T{-l}, and @T{-q} are handled in +the order they appear. For instance, an invocation like @verbatim{ -$ lua -e'a=1' -e 'print(a)' script.lua +$ lua -e 'a=1' -llib1 script.lua } -will first set @id{a} to 1, then print the value of @id{a}, +will first set @id{a} to 1, then require the library @id{lib1}, and finally run the file @id{script.lua} with no arguments. (Here @T{$} is the shell prompt. Your prompt may be different.) @@ -8798,7 +8812,8 @@ has a metamethod @idx{__tostring}, the interpreter calls this metamethod to produce the final message. Otherwise, the interpreter converts the error object to a string and adds a stack traceback to it. -Warnings are simply printed in the standard error output. +When warnings are on, +they are simply printed in the standard error output. When finishing normally, the interpreter closes its main Lua state diff --git a/testes/all.lua b/testes/all.lua index bf27f10686..5d698d4bf3 100644 --- a/testes/all.lua +++ b/testes/all.lua @@ -209,6 +209,10 @@ if #msgs > 0 then warn("#tests not performed:\n ", m, "\n") end +warn("@off") +warn("******** THIS WARNING SHOULD NOT APPEAR **********") +warn("******** THIS WARNING ALSO SHOULD NOT APPEAR **********") +warn("@on") print("(there should be two warnings now)") warn("#This is ", "an expected", " warning") warn("#This is", " another one") diff --git a/testes/api.lua b/testes/api.lua index f6915c3e06..4f9d671779 100644 --- a/testes/api.lua +++ b/testes/api.lua @@ -977,6 +977,7 @@ assert(t[7] == nil) ------------------------------------------------------------------------- do -- testing errors during GC + warn("@off") collectgarbage("stop") local a = {} for i=1,20 do @@ -994,6 +995,7 @@ do -- testing errors during GC collectgarbage() assert(A == 10) -- number of normal collections collectgarbage("restart") + warn("@on") end ------------------------------------------------------------------------- -- test for userdata vals diff --git a/testes/gc.lua b/testes/gc.lua index 9ea054c1ad..6bdc98ca1d 100644 --- a/testes/gc.lua +++ b/testes/gc.lua @@ -369,6 +369,7 @@ if T then s[n] = i end + warn("@store") collectgarbage() assert(string.find(_WARN, "error in __gc metamethod")) assert(string.match(_WARN, "@(.-)@") == "expected") @@ -383,6 +384,7 @@ if T then for i = 1, 10 do assert(s[i]) end getmetatable(u).__gc = nil + warn("@normal") end print '+' @@ -475,9 +477,11 @@ end -- errors during collection if T then + warn("@store") u = setmetatable({}, {__gc = function () error "@expected error" end}) u = nil collectgarbage() + warn("@normal") end @@ -645,7 +649,7 @@ end -- create several objects to raise errors when collected while closing state if T then - local error, assert, find = error, assert, string.find + local error, assert, find, warn = error, assert, string.find, warn local n = 0 local lastmsg local mt = {__gc = function (o) @@ -659,7 +663,9 @@ if T then else assert(lastmsg == _WARN) -- subsequent error messages are equal end + warn("@store") error"@expected warning" + warn("@normal") end} for i = 10, 1, -1 do -- create object and preserve it until the end diff --git a/testes/main.lua b/testes/main.lua index b224b54e6d..0ef4822d7f 100644 --- a/testes/main.lua +++ b/testes/main.lua @@ -221,6 +221,28 @@ assert(string.find(getoutput(), "error calling 'print'")) RUN('echo "io.stderr:write(1000)\ncont" | lua -e "require\'debug\'.debug()" 2> %s', out) checkout("lua_debug> 1000lua_debug> ") +-- test warnings +RUN('echo "io.stderr:write(1); warn[[XXX]]" | lua -q 2> %s', out) +checkout("1") + +prepfile[[ +warn("@allow") -- unknown control, ignored +warn("@off", "XXX", "@off") -- these are not control messages +warn("@off") -- this one is +warn("@on", "YYY", "@on") -- not control, but warn is off +warn("@off") -- keep it off +warn("@on") -- restart warnings +warn("", "@on") -- again, no control, real warning +warn("@on") -- keep it "started" +warn("Z", "Z", "Z") -- common warning +]] +RUN('lua %s 2> %s', prog, out) +checkout[[ +Lua warning: @offXXX@off +Lua warning: @on +Lua warning: ZZZ +]] + -- test many arguments prepfile[[print(({...})[30])]] RUN('lua %s %s > %s', prog, string.rep(" a", 30), out) @@ -355,8 +377,15 @@ if T then -- test library? NoRun("not enough memory", "env MEMLIMIT=100 lua") -- testing 'warn' + warn("@store") warn("@123", "456", "789") assert(_WARN == "@123456789") + + warn("zip", "", " ", "zap") + assert(_WARN == "zip zap") + warn("ZIP", "", " ", "ZAP") + assert(_WARN == "ZIP ZAP") + warn("@normal") end do From ca13be9af784b7288d3a07d9b5bccb329086e885 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 16 Aug 2019 09:51:54 -0300 Subject: [PATCH 0483/1145] Supressed errors in '__close' generate warnings --- lauxlib.c | 4 +- lfunc.c | 6 +- lgc.c | 7 +- lstate.c | 16 +++++ lstate.h | 1 + ltests.c | 10 +-- manual/manual.of | 2 +- testes/all.lua | 4 +- testes/coroutine.lua | 5 +- testes/locals.lua | 152 ++++++++++++++++++++++++++++++++++++------- 10 files changed, 164 insertions(+), 43 deletions(-) diff --git a/lauxlib.c b/lauxlib.c index ba1980b7c4..014e7052d9 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -1010,9 +1010,9 @@ static int panic (lua_State *L) { static void warnf (void *ud, const char *message, int tocont) { int *warnstate = (int *)ud; if (*warnstate != 2 && !tocont && *message == '@') { /* control message? */ - if (strcmp(message + 1, "off") == 0) + if (strcmp(message, "@off") == 0) *warnstate = 0; - else if (strcmp(message + 1, "on") == 0) + else if (strcmp(message, "@on") == 0) *warnstate = 1; return; } diff --git a/lfunc.c b/lfunc.c index 8f39f6b076..1e61f03f58 100644 --- a/lfunc.c +++ b/lfunc.c @@ -164,8 +164,12 @@ static int callclosemth (lua_State *L, StkId level, int status) { int newstatus = luaD_pcall(L, callclose, NULL, oldtop, 0); if (newstatus != LUA_OK && status == CLOSEPROTECT) /* first error? */ status = newstatus; /* this will be the new error */ - else /* leave original error (or nil) on top */ + else { + if (newstatus != LUA_OK) /* supressed error? */ + luaE_warnerror(L, "__close metamethod"); + /* leave original error (or nil) on top */ L->top = restorestack(L, oldtop); + } } /* else no metamethod; ignore this case and keep original error */ } diff --git a/lgc.c b/lgc.c index 75670c0a6a..f24074f920 100644 --- a/lgc.c +++ b/lgc.c @@ -854,12 +854,7 @@ static void GCTM (lua_State *L) { L->allowhook = oldah; /* restore hooks */ g->gcrunning = running; /* restore state */ if (unlikely(status != LUA_OK)) { /* error while running __gc? */ - const char *msg = (ttisstring(s2v(L->top - 1))) - ? svalue(s2v(L->top - 1)) - : "error object is not a string"; - luaE_warning(L, "error in __gc metamethod (", 1); - luaE_warning(L, msg, 1); - luaE_warning(L, ")", 0); + luaE_warnerror(L, "__gc metamethod"); L->top--; /* pops error object */ } } diff --git a/lstate.c b/lstate.c index d4bc53eb3b..86cd5fb824 100644 --- a/lstate.c +++ b/lstate.c @@ -443,3 +443,19 @@ void luaE_warning (lua_State *L, const char *msg, int tocont) { } +/* +** Generate a warning from an error message +*/ +void luaE_warnerror (lua_State *L, const char *where) { + TValue *errobj = s2v(L->top - 1); /* error object */ + const char *msg = (ttisstring(errobj)) + ? svalue(errobj) + : "error object is not a string"; + /* produce warning "error in %s (%s)" (where, msg) */ + luaE_warning(L, "error in ", 1); + luaE_warning(L, where, 1); + luaE_warning(L, " (", 1); + luaE_warning(L, msg, 1); + luaE_warning(L, ")", 0); +} + diff --git a/lstate.h b/lstate.h index 03448b82bc..638c1e5c0d 100644 --- a/lstate.h +++ b/lstate.h @@ -355,6 +355,7 @@ LUAI_FUNC void luaE_freeCI (lua_State *L); LUAI_FUNC void luaE_shrinkCI (lua_State *L); LUAI_FUNC void luaE_enterCcall (lua_State *L); LUAI_FUNC void luaE_warning (lua_State *L, const char *msg, int tocont); +LUAI_FUNC void luaE_warnerror (lua_State *L, const char *where); #define luaE_exitCcall(L) ((L)->nCcalls++) diff --git a/ltests.c b/ltests.c index fd55fc31a8..b460d01894 100644 --- a/ltests.c +++ b/ltests.c @@ -95,15 +95,15 @@ static void warnf (void *ud, const char *msg, int tocont) { if (!lasttocont && !tocont && *msg == '@') { /* control message? */ if (buff[0] != '\0') badexit("Control warning during warning: %s\naborting...\n", msg); - if (strcmp(msg + 1, "off") == 0) + if (strcmp(msg, "@off") == 0) onoff = 0; - else if (strcmp(msg + 1, "on") == 0) + else if (strcmp(msg, "@on") == 0) onoff = 1; - else if (strcmp(msg + 1, "normal") == 0) + else if (strcmp(msg, "@normal") == 0) mode = 0; - else if (strcmp(msg + 1, "allow") == 0) + else if (strcmp(msg, "@allow") == 0) mode = 1; - else if (strcmp(msg + 1, "store") == 0) + else if (strcmp(msg, "@store") == 0) mode = 2; else badexit("Invalid control warning in test mode: %s\naborting...\n", msg); diff --git a/manual/manual.of b/manual/manual.of index 8c71c61384..bb6ae8840e 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -1556,7 +1556,7 @@ However, Lua may call the method one more time. After an error, the other pending closing methods will still be called. Errors in these methods -interrupt the respective method, +interrupt the respective method and generate a warning, but are otherwise ignored; the error reported is only the original one. diff --git a/testes/all.lua b/testes/all.lua index 5d698d4bf3..42809b9a01 100644 --- a/testes/all.lua +++ b/testes/all.lua @@ -209,12 +209,12 @@ if #msgs > 0 then warn("#tests not performed:\n ", m, "\n") end +print("(there should be two warnings now)") +warn("#This is ", "an expected", " warning") warn("@off") warn("******** THIS WARNING SHOULD NOT APPEAR **********") warn("******** THIS WARNING ALSO SHOULD NOT APPEAR **********") warn("@on") -print("(there should be two warnings now)") -warn("#This is ", "an expected", " warning") warn("#This is", " another one") -- no test module should define 'debug' diff --git a/testes/coroutine.lua b/testes/coroutine.lua index 457374cab1..79c72a9dc6 100644 --- a/testes/coroutine.lua +++ b/testes/coroutine.lua @@ -168,7 +168,7 @@ do local y = func2close(function (self,err) if (err ~= 111) then os.exit(false) end -- should not happen x = 200 - error(200) + error("200") end) local x = func2close(function (self, err) assert(err == nil); error(111) @@ -177,7 +177,10 @@ do end) coroutine.resume(co) assert(x == 0) + _WARN = nil; warn("@off"); warn("@store") local st, msg = coroutine.close(co) + warn("@on"); warn("@normal") + assert(_WARN == nil or string.find(_WARN, "200")) assert(st == false and coroutine.status(co) == "dead" and msg == 111) assert(x == 200) diff --git a/testes/locals.lua b/testes/locals.lua index 99fa79cd45..595e107af5 100644 --- a/testes/locals.lua +++ b/testes/locals.lua @@ -286,57 +286,149 @@ do end -do -- errors in __close - local log = {} - local function foo (err) +-- auxiliary functions for testing warnings in '__close' +local function prepwarn () + warn("@off") -- do not show (lots of) warnings + if not T then + _WARN = "OFF" -- signal that warnings are not being captured + else + warn("@store") -- to test the warnings + end +end + + +local function endwarn () + assert(T or _WARN == "OFF") + warn("@on") -- back to normal + warn("@normal") + _WARN = nil +end + + +local function checkwarn (msg) + assert(_WARN == "OFF" or string.find(_WARN, msg)) +end + + +do print("testing errors in __close") + + prepwarn() + + -- original error is in __close + local function foo () + local x = - func2close(function (self, msg) log[#log + 1] = msg; error(1) end) + func2close(function (self, msg) + assert(string.find(msg, "@z")) + error("@x") + end) + local x1 = - func2close(function (self, msg) log[#log + 1] = msg; end) + func2close(function (self, msg) + checkwarn("@y") + assert(string.find(msg, "@z")) + end) + local gc = func2close(function () collectgarbage() end) + local y = - func2close(function (self, msg) log[#log + 1] = msg; error(2) end) + func2close(function (self, msg) + assert(string.find(msg, "@z")) -- error in 'z' + error("@y") + end) + + local first = true local z = + -- 'z' close is called twice func2close(function (self, msg) - log[#log + 1] = (msg or 10) + 1; - error(3) + if first then + assert(msg == nil) + first = false + else + assert(string.find(msg, "@z")) -- own error + end + error("@z") end) - if err then error(4) end + + return 200 end + local stat, msg = pcall(foo, false) - assert(msg == 3) - -- 'z' close is called twice - assert(log[1] == 11 and log[2] == 4 and log[3] == 3 and log[4] == 3 - and log[5] == 3 and #log == 5) + assert(string.find(msg, "@z")) + checkwarn("@x") + + + -- original error not in __close + local function foo () + + local x = + func2close(function (self, msg) + assert(msg == 4) + end) + + local x1 = + func2close(function (self, msg) + checkwarn("@y") + assert(msg == 4) + error("@x1") + end) + + local gc = func2close(function () collectgarbage() end) + + local y = + func2close(function (self, msg) + assert(msg == 4) -- error in body + error("@y") + end) + + local first = true + local z = + func2close(function (self, msg) + checkwarn("@z") + -- 'z' close is called once + assert(first and msg == 4) + first = false + error("@z") + end) + + error(4) -- original error + end - log = {} local stat, msg = pcall(foo, true) assert(msg == 4) - -- 'z' close is called once - assert(log[1] == 5 and log[2] == 4 and log[3] == 4 and log[4] == 4 - and #log == 4) + checkwarn("@x1") -- last error -- error leaving a block local function foo (...) do - local x1 = func2close(function () error("Y") end) - local x123 = func2close(function () error("X") end) + local x1 = + func2close(function () + checkwarn("@X") + error("@Y") + end) + + local x123 = + func2close(function () + error("@X") + end) end + os.exit(false) -- should not run end local st, msg = xpcall(foo, debug.traceback) - assert(string.match(msg, "^[^ ]* X")) + assert(string.match(msg, "^[^ ]* @X")) assert(string.find(msg, "in metamethod 'close'")) -- error in toclose in vararg function local function foo (...) - local x123 = func2close(function () error("X") end) + local x123 = func2close(function () error("@X") end) end local st, msg = xpcall(foo, debug.traceback) - assert(string.match(msg, "^[^ ]* X")) + assert(string.match(msg, "^[^ ]* @X")) assert(string.find(msg, "in metamethod 'close'")) + endwarn() end @@ -361,6 +453,8 @@ end if rawget(_G, "T") then + warn("@off") + -- memory error inside closing function local function foo () local y = func2close(function () T.alloccount() end) @@ -437,7 +531,7 @@ if rawget(_G, "T") then local s = string.rep("a", lim) - -- concat this table needs two buffer resizes (one for each 's') + -- concat this table needs two buffer resizes (one for each 's') local a = {s, s} collectgarbage() @@ -472,6 +566,8 @@ if rawget(_G, "T") then print'+' end + + warn("@on") end @@ -501,17 +597,20 @@ end do + prepwarn() + -- error in a wrapped coroutine raising errors when closing a variable local x = 0 local co = coroutine.wrap(function () - local xx = func2close(function () x = x + 1; error("YYY") end) - local xv = func2close(function () x = x + 1; error("XXX") end) + local xx = func2close(function () x = x + 1; error("@YYY") end) + local xv = func2close(function () x = x + 1; error("@XXX") end) coroutine.yield(100) error(200) end) assert(co() == 100); assert(x == 0) local st, msg = pcall(co); assert(x == 2) assert(not st and msg == 200) -- should get first error raised + checkwarn("@YYY") local x = 0 local y = 0 @@ -526,6 +625,9 @@ do assert(x == 2 and y == 1) -- first close is called twice -- should get first error raised assert(not st and string.find(msg, "%w+%.%w+:%d+: XXX")) + checkwarn("YYY") + + endwarn() end From b96b0b5abbf40cbdbed7952bf35a5a27ddf75928 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 16 Aug 2019 14:58:02 -0300 Subject: [PATCH 0484/1145] Added macro 'luaL_pushfail' The macro 'luaL_pushfail' documents all places in the standard libraries that return nil to signal some kind of failure. It is defined as 'lua_pushnil'. The manual also got a notation (@fail) to document those returns. The tests were changed to be agnostic regarding whether 'fail' is 'nil' or 'false'. --- lauxlib.c | 6 +-- lauxlib.h | 4 ++ lbaselib.c | 6 +-- ldblib.c | 14 ++++--- liolib.c | 10 ++--- lmathlib.c | 4 +- loadlib.c | 8 ++-- lstrlib.c | 4 +- lutf8lib.c | 4 +- manual/2html | 1 + manual/manual.of | 87 ++++++++++++++++++++++---------------- testes/api.lua | 2 +- testes/db.lua | 6 +-- testes/errors.lua | 10 ++--- testes/files.lua | 30 ++++++------- testes/literals.lua | 2 +- testes/math.lua | 100 ++++++++++++++++++++++---------------------- testes/pm.lua | 22 +++++----- testes/strings.lua | 6 +-- testes/utf8.lua | 4 +- 20 files changed, 176 insertions(+), 154 deletions(-) diff --git a/lauxlib.c b/lauxlib.c index 014e7052d9..5a040ac645 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -249,7 +249,7 @@ LUALIB_API int luaL_fileresult (lua_State *L, int stat, const char *fname) { return 1; } else { - lua_pushnil(L); + luaL_pushfail(L); if (fname) lua_pushfstring(L, "%s: %s", fname, strerror(en)); else @@ -291,10 +291,10 @@ LUALIB_API int luaL_execresult (lua_State *L, int stat) { if (*what == 'e' && stat == 0) /* successful termination? */ lua_pushboolean(L, 1); else - lua_pushnil(L); + luaL_pushfail(L); lua_pushstring(L, what); lua_pushinteger(L, stat); - return 3; /* return true/nil,what,code */ + return 3; /* return true/fail,what,code */ } } diff --git a/lauxlib.h b/lauxlib.h index f68f6af18c..c50cdf4d03 100644 --- a/lauxlib.h +++ b/lauxlib.h @@ -153,6 +153,10 @@ LUALIB_API void (luaL_requiref) (lua_State *L, const char *modname, #define luaL_loadbuffer(L,s,sz,n) luaL_loadbufferx(L,s,sz,n,NULL) +/* push the value used to represent failure/error */ +#define luaL_pushfail(L) lua_pushnil(L) + + /* ** {====================================================== ** Generic Buffer manipulation diff --git a/lbaselib.c b/lbaselib.c index c68e6d3893..747fd45a2f 100644 --- a/lbaselib.c +++ b/lbaselib.c @@ -106,7 +106,7 @@ static int luaB_tonumber (lua_State *L) { return 1; } /* else not a number */ } /* else not a number */ - lua_pushnil(L); /* not a number */ + luaL_pushfail(L); /* not a number */ return 1; } @@ -308,9 +308,9 @@ static int load_aux (lua_State *L, int status, int envidx) { return 1; } else { /* error (message is on top of the stack) */ - lua_pushnil(L); + luaL_pushfail(L); lua_insert(L, -2); /* put before error message */ - return 2; /* return nil plus error message */ + return 2; /* return fail plus error message */ } } diff --git a/ldblib.c b/ldblib.c index 641583955e..9f7aad5129 100644 --- a/ldblib.c +++ b/ldblib.c @@ -65,7 +65,7 @@ static int db_setmetatable (lua_State *L) { static int db_getuservalue (lua_State *L) { int n = (int)luaL_optinteger(L, 2, 1); if (lua_type(L, 1) != LUA_TUSERDATA) - lua_pushnil(L); + luaL_pushfail(L); else if (lua_getiuservalue(L, 1, n) != LUA_TNONE) { lua_pushboolean(L, 1); return 2; @@ -80,7 +80,7 @@ static int db_setuservalue (lua_State *L) { luaL_checkany(L, 2); lua_settop(L, 2); if (!lua_setiuservalue(L, 1, n)) - lua_pushnil(L); + luaL_pushfail(L); return 1; } @@ -159,7 +159,7 @@ static int db_getinfo (lua_State *L) { } else { /* stack level */ if (!lua_getstack(L1, (int)luaL_checkinteger(L, arg + 1), &ar)) { - lua_pushnil(L); /* level out of range */ + luaL_pushfail(L); /* level out of range */ return 1; } } @@ -223,7 +223,7 @@ static int db_getlocal (lua_State *L) { return 2; } else { - lua_pushnil(L); /* no name (nor value) */ + luaL_pushfail(L); /* no name (nor value) */ return 1; } } @@ -389,8 +389,10 @@ static int db_gethook (lua_State *L) { char buff[5]; int mask = lua_gethookmask(L1); lua_Hook hook = lua_gethook(L1); - if (hook == NULL) /* no hook? */ - lua_pushnil(L); + if (hook == NULL) { /* no hook? */ + luaL_pushfail(L); + return 1; + } else if (hook != hookf) /* external hook? */ lua_pushliteral(L, "external hook"); else { /* hook table must exist */ diff --git a/liolib.c b/liolib.c index 56507d5ea5..d8b0a6f9de 100644 --- a/liolib.c +++ b/liolib.c @@ -153,7 +153,7 @@ static int io_type (lua_State *L) { luaL_checkany(L, 1); p = (LStream *)luaL_testudata(L, 1, LUA_FILEHANDLE); if (p == NULL) - lua_pushnil(L); /* not a file */ + luaL_pushfail(L); /* not a file */ else if (isclosed(p)) lua_pushliteral(L, "closed file"); else @@ -593,7 +593,7 @@ static int g_read (lua_State *L, FILE *f, int first) { return luaL_fileresult(L, 0, NULL); if (!success) { lua_pop(L, 1); /* remove last result */ - lua_pushnil(L); /* push nil instead */ + luaL_pushfail(L); /* push nil instead */ } return n - first; } @@ -624,9 +624,9 @@ static int io_readline (lua_State *L) { lua_pushvalue(L, lua_upvalueindex(3 + i)); n = g_read(L, p->f, 2); /* 'n' is number of results */ lua_assert(n > 0); /* should return at least a nil */ - if (!lua_isnil(L, -n)) /* read at least one value? */ + if (lua_toboolean(L, -n)) /* read at least one value? */ return n; /* return them */ - else { /* first result is nil: EOF or error */ + else { /* first result is false: EOF or error */ if (n > 1) { /* is there error information? */ /* 2nd result is error message */ return luaL_error(L, "%s", lua_tostring(L, -n + 1)); @@ -782,7 +782,7 @@ static void createmeta (lua_State *L) { static int io_noclose (lua_State *L) { LStream *p = tolstream(L); p->closef = &io_noclose; /* keep file opened */ - lua_pushnil(L); + luaL_pushfail(L); lua_pushliteral(L, "cannot close standard file"); return 2; } diff --git a/lmathlib.c b/lmathlib.c index 752647e71b..f49eb318eb 100644 --- a/lmathlib.c +++ b/lmathlib.c @@ -77,7 +77,7 @@ static int math_toint (lua_State *L) { lua_pushinteger(L, n); else { luaL_checkany(L, 1); - lua_pushnil(L); /* value is not convertible to integer */ + luaL_pushfail(L); /* value is not convertible to integer */ } return 1; } @@ -235,7 +235,7 @@ static int math_type (lua_State *L) { lua_pushstring(L, (lua_isinteger(L, 1)) ? "integer" : "float"); else { luaL_checkany(L, 1); - lua_pushnil(L); + luaL_pushfail(L); } return 1; } diff --git a/loadlib.c b/loadlib.c index 9ef0027866..d7a3fb23cb 100644 --- a/loadlib.c +++ b/loadlib.c @@ -408,10 +408,10 @@ static int ll_loadlib (lua_State *L) { if (stat == 0) /* no errors? */ return 1; /* return the loaded function */ else { /* error; error message is on stack top */ - lua_pushnil(L); + luaL_pushfail(L); lua_insert(L, -2); lua_pushstring(L, (stat == ERRLIB) ? LIB_FAIL : "init"); - return 3; /* return nil, error message, and where */ + return 3; /* return fail, error message, and where */ } } @@ -505,9 +505,9 @@ static int ll_searchpath (lua_State *L) { luaL_optstring(L, 4, LUA_DIRSEP)); if (f != NULL) return 1; else { /* error message is on top of the stack */ - lua_pushnil(L); + luaL_pushfail(L); lua_insert(L, -2); - return 2; /* return nil + error message */ + return 2; /* return fail + error message */ } } diff --git a/lstrlib.c b/lstrlib.c index 8c9e1a83b1..7f4a01849b 100644 --- a/lstrlib.c +++ b/lstrlib.c @@ -744,7 +744,7 @@ static int str_find_aux (lua_State *L, int find) { const char *p = luaL_checklstring(L, 2, &lp); size_t init = posrelatI(luaL_optinteger(L, 3, 1), ls) - 1; if (init > ls) { /* start after string's end? */ - lua_pushnil(L); /* cannot find anything */ + luaL_pushfail(L); /* cannot find anything */ return 1; } /* explicit request or no special characters? */ @@ -779,7 +779,7 @@ static int str_find_aux (lua_State *L, int find) { } } while (s1++ < ms.src_end && !anchor); } - lua_pushnil(L); /* not found */ + luaL_pushfail(L); /* not found */ return 1; } diff --git a/lutf8lib.c b/lutf8lib.c index b4b787e7f2..e63a5a7453 100644 --- a/lutf8lib.c +++ b/lutf8lib.c @@ -103,7 +103,7 @@ static int utflen (lua_State *L) { while (posi <= posj) { const char *s1 = utf8_decode(s + posi, NULL, !lax); if (s1 == NULL) { /* conversion error? */ - lua_pushnil(L); /* return nil ... */ + luaL_pushfail(L); /* return fail ... */ lua_pushinteger(L, posi + 1); /* ... and current position */ return 2; } @@ -216,7 +216,7 @@ static int byteoffset (lua_State *L) { if (n == 0) /* did it find given character? */ lua_pushinteger(L, posi + 1); else /* no such character */ - lua_pushnil(L); + luaL_pushfail(L); return 1; } diff --git a/manual/2html b/manual/2html index 605c6e59f5..a300f8d486 100755 --- a/manual/2html +++ b/manual/2html @@ -324,6 +324,7 @@ N = function (s) return (string.gsub(s, " ", " ")) end, NE = id, -- tag"foreignphrase", num = id, ["nil"] = fixed(Tag.b"nil"), +fail = fixed(Tag.b"fail"), Open = fixed"{", part = section("h1", true), Pat = compose(verbfixed, prepos("'", "'")), diff --git a/manual/manual.of b/manual/manual.of index bb6ae8840e..53073a5464 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -4058,12 +4058,15 @@ Returns 0 if the userdata does not have that value. } -@APIEntry{void lua_setmetatable (lua_State *L, int index);| +@APIEntry{int lua_setmetatable (lua_State *L, int index);| @apii{1,0,-} Pops a table from the stack and sets it as the new metatable for the value at the given index. +(For historical reasons, this function returns an @id{int}, +which now is always 1.) + } @APIEntry{void lua_settable (lua_State *L, int index);| @@ -5782,7 +5785,7 @@ that will be called to close the stream when the handle is closed or collected; this function receives the file handle as its sole argument and must return either @true, in case of success, -or @nil plus an error message, in case of error. +or a false value plus an error message, in case of error. Once Lua calls this field, it changes the field value to @id{NULL} to signal that the handle is closed. @@ -5904,6 +5907,14 @@ to its expected parameters. For instance, a function documented as @T{foo(arg)} should not be called without an argument. +The notation @fail means a return value representing +some kind of failure or the absence of a better value to return. +Currently, @fail is equal to @nil, +but that may change in future versions. +The recommendation is to test the success of these functions +with @T{(not status)}, instead of @T{(status == nil)}. + + Currently, Lua has the following standard libraries: @itemize{ @@ -6108,8 +6119,8 @@ with previous results. A return of an empty string, @nil, or no value signals the end of the chunk. If there are no syntactic errors, -returns the compiled chunk as a function; -otherwise, returns @nil plus the error message. +@id{load} returns the compiled chunk as a function; +otherwise, it returns @fail plus the error message. When you load a main chunk, the resulting function will always have exactly one upvalue, @@ -6301,7 +6312,7 @@ When called with no @id{base}, If the argument is already a number or a string convertible to a number, then @id{tonumber} returns this number; -otherwise, it returns @nil. +otherwise, it returns @fail. The conversion of strings can result in integers or floats, according to the lexical conventions of Lua @see{lexical}. @@ -6315,7 +6326,7 @@ In bases @N{above 10}, the letter @Char{A} (in either upper or lower case) @N{represents 10}, @Char{B} @N{represents 11}, and so forth, with @Char{Z} representing 35. If the string @id{e} is not a valid numeral in the given base, -the function returns @nil. +the function returns @fail. } @@ -6762,7 +6773,7 @@ will try to open the files Returns the resulting name of the first file that it can open in read mode (after closing the file), -or @nil plus an error message if none succeeds. +or @fail plus an error message if none succeeds. (This error message lists all file names it tried to open.) } @@ -6841,7 +6852,7 @@ Looks for the first match of @id{pattern} @see{pm} in the string @id{s}. If it finds a match, then @id{find} returns the indices @N{of @T{s}} where this occurrence starts and ends; -otherwise, it returns @nil. +otherwise, it returns @fail. A third, optional numeric argument @id{init} specifies where to start the search; its default value @N{is 1} and can be negative. @@ -7034,7 +7045,7 @@ Looks for the first @emph{match} of the @id{pattern} @see{pm} in the string @id{s}. If it finds one, then @id{match} returns the captures from the pattern; -otherwise it returns @nil. +otherwise it returns @fail. If @id{pattern} specifies no captures, then the whole match is returned. A third, optional numeric argument @id{init} specifies @@ -7499,7 +7510,7 @@ Returns the number of UTF-8 characters in string @id{s} that start between positions @id{i} and @id{j} (both inclusive). The default for @id{i} is @num{1} and for @id{j} is @num{-1}. If it finds any invalid byte sequence, -returns a false value plus the position of the first invalid byte. +returns @fail plus the position of the first invalid byte. } @@ -7515,7 +7526,7 @@ so that @T{utf8.offset(s, -n)} gets the offset of the @id{n}-th character from the end of the string. If the specified character is neither in the subject nor right after its end, -the function returns @nil. +the function returns @fail. As a special case, when @id{n} is 0 the function returns the start of the encoding @@ -7850,7 +7861,7 @@ Returns the tangent of @id{x} (assumed to be in radians). If the value @id{x} is convertible to an integer, returns that integer. -Otherwise, returns @nil. +Otherwise, returns @fail. } @@ -7858,7 +7869,7 @@ Otherwise, returns @nil. Returns @St{integer} if @id{x} is an integer, @St{float} if it is a float, -or @nil if @id{x} is not a number. +or @fail if @id{x} is not a number. } @@ -7897,10 +7908,10 @@ three predefined file handles with their usual meanings from C: The I/O library never closes these files. Unless otherwise stated, -all I/O functions return @nil on failure, +all I/O functions return @fail on failure, plus an error message as a second result and a system-dependent error code as a third result, -and some value different from @nil on success. +and some non-false value on success. On non-POSIX systems, the computation of the error message and error code in case of errors @@ -8021,7 +8032,7 @@ and it is automatically removed when the program ends. Checks whether @id{obj} is a valid file handle. Returns the string @T{"file"} if @id{obj} is an open file handle, @T{"closed file"} if @id{obj} is a closed file handle, -or @nil if @id{obj} is not a file handle. +or @fail if @id{obj} is not a file handle. } @@ -8075,7 +8086,7 @@ Reads the file @id{file}, according to the given formats, which specify what to read. For each format, the function returns a string or a number with the characters read, -or @nil if it cannot read data with the specified format. +or @fail if it cannot read data with the specified format. (In this latter case, the function does not read subsequent formats.) When called without arguments, @@ -8094,31 +8105,32 @@ is a valid prefix for a numeral; if that prefix does not form a valid numeral (e.g., an empty string, @St{0x}, or @St{3.4e-}) or it is too long (more than 200 characters), -it is discarded and the format returns @nil. +it is discarded and the format returns @fail. } @item{@St{a}| reads the whole file, starting at the current position. -On end of file, it returns the empty string. +On end of file, it returns the empty string; +this format never fails. } @item{@St{l}| reads the next line skipping the end of line, -returning @nil on end of file. +returning @fail on end of file. This is the default format. } @item{@St{L}| reads the next line keeping the end-of-line character (if present), -returning @nil on end of file. +returning @fail on end of file. } @item{@emph{number}| reads a string with up to this number of bytes, -returning @nil on end of file. +returning @fail on end of file. If @id{number} is zero, it reads nothing and returns an empty string, -or @nil on end of file. +or @fail on end of file. } } @@ -8139,7 +8151,7 @@ specified by the string @id{whence}, as follows: } In case of success, @id{seek} returns the final file position, measured in bytes from the beginning of the file. -If @id{seek} fails, it returns @nil, +If @id{seek} fails, it returns @fail, plus a string describing the error. The default value for @id{whence} is @T{"cur"}, @@ -8179,7 +8191,6 @@ Writes the value of each of its arguments to @id{file}. The arguments must be strings or numbers. In case of success, this function returns @id{file}. -Otherwise it returns @nil plus a string describing the error. } @@ -8251,7 +8262,7 @@ This function is equivalent to the @ANSI{system}. It passes @id{command} to be executed by an operating system shell. Its first result is @true if the command terminated successfully, -or @nil otherwise. +or @fail otherwise. After this first result the function returns a string plus a number, as follows: @@ -8293,7 +8304,7 @@ closes the Lua state before exiting. @LibEntry{os.getenv (varname)| Returns the value of the process environment variable @id{varname}, -or @nil if the variable is not defined. +or @fail if the variable is not defined. } @@ -8301,7 +8312,7 @@ or @nil if the variable is not defined. Deletes the file (or empty directory, on @x{POSIX} systems) with the given name. -If this function fails, it returns @nil, +If this function fails, it returns @fail plus a string describing the error and the error code. Otherwise, it returns true. @@ -8310,7 +8321,7 @@ Otherwise, it returns true. @LibEntry{os.rename (oldname, newname)| Renames the file or directory named @id{oldname} to @id{newname}. -If this function fails, it returns @nil, +If this function fails, it returns @fail, plus a string describing the error and the error code. Otherwise, it returns true. @@ -8325,7 +8336,7 @@ Sets the current locale of the program. @T{"monetary"}, @T{"numeric"}, or @T{"time"}; the default category is @T{"all"}. The function returns the name of the new locale, -or @nil if the request cannot be honored. +or @fail if the request cannot be honored. If @id{locale} is the empty string, the current locale is set to an implementation-defined native locale. @@ -8444,6 +8455,8 @@ the current hook function, the current hook mask, and the current hook count, as set by the @Lid{debug.sethook} function. +Returns @fail if there is no active hook. + } @LibEntry{debug.getinfo ([thread,] f [, what])| @@ -8458,7 +8471,7 @@ of the given thread: (except for tail calls, which do not count on the stack); and so on. If @id{f} is a number greater than the number of active functions, -then @id{getinfo} returns @nil. +then @id{getinfo} returns @fail. The returned table can contain all the fields returned by @Lid{lua_getinfo}, with the string @id{what} describing which fields to fill in. @@ -8496,7 +8509,8 @@ Compile-time constants may not appear in this listing, if they were optimized away by the compiler. Negative indices refer to vararg arguments; @num{-1} is the first vararg argument. -The function returns @nil if there is no variable with the given index, +The function returns @fail +if there is no variable with the given index, and raises an error when called with a level out of range. (You can call @Lid{debug.getinfo} to check whether the level is valid.) @@ -8527,7 +8541,8 @@ Returns the registry table @see{registry}. This function returns the name and the value of the upvalue with index @id{up} of the function @id{f}. -The function returns @nil if there is no upvalue with the given index. +The function returns @fail +if there is no upvalue with the given index. (For Lua functions, upvalues are the external local variables that the function uses, @@ -8615,7 +8630,7 @@ and @N{level 1} is the hook function.) This function assigns the value @id{value} to the local variable with index @id{local} of the function at level @id{level} of the stack. -The function returns @nil if there is no local +The function returns @fail if there is no local variable with the given index, and raises an error when called with a @id{level} out of range. (You can call @id{getinfo} to check whether the level is valid.) @@ -8638,7 +8653,7 @@ Returns @id{value}. This function assigns the value @id{value} to the upvalue with index @id{up} of the function @id{f}. -The function returns @nil if there is no upvalue +The function returns @fail if there is no upvalue with the given index. Otherwise, it returns the name of the upvalue. @@ -8653,7 +8668,7 @@ the @id{n}-th user value associated to the given @id{udata}. @id{udata} must be a full userdata. Returns @id{udata}, -or @nil if the userdata does not have that value. +or @fail if the userdata does not have that value. } diff --git a/testes/api.lua b/testes/api.lua index 4f9d671779..b268063314 100644 --- a/testes/api.lua +++ b/testes/api.lua @@ -698,7 +698,7 @@ for k, v in ipairs(t) do assert(v1 == v and p) end -assert(debug.getuservalue(4) == nil) +assert(not debug.getuservalue(4)) debug.setuservalue(b, function () return 10 end, 10) collectgarbage() -- function should not be collected diff --git a/testes/db.lua b/testes/db.lua index a64a1130b2..c43243a6a3 100644 --- a/testes/db.lua +++ b/testes/db.lua @@ -351,12 +351,12 @@ assert(g(0,0) == 30) debug.sethook(nil); -assert(debug.gethook() == nil) +assert(not debug.gethook()) -- minimal tests for setuservalue/getuservalue do - assert(debug.setuservalue(io.stdin, 10) == nil) + assert(not debug.setuservalue(io.stdin, 10)) local a, b = debug.getuservalue(io.stdin, 10) assert(a == nil and not b) end @@ -414,7 +414,7 @@ end, "c") a:f(1,2,3,4,5) assert(X.self == a and X.a == 1 and X.b == 2 and X.c == nil) assert(XX == 12) -assert(debug.gethook() == nil) +assert(not debug.gethook()) -- testing access to local variables in return hook (bug in 5.2) diff --git a/testes/errors.lua b/testes/errors.lua index 6e7b8004be..f9623b1daf 100644 --- a/testes/errors.lua +++ b/testes/errors.lua @@ -18,7 +18,7 @@ end local function doit (s) local f, msg = load(s) - if f == nil then return msg end + if not f then return msg end local cond, msg = pcall(f) return (not cond) and msg end @@ -312,8 +312,8 @@ end local function lineerror (s, l) local err,msg = pcall(load(s)) - local line = string.match(msg, ":(%d+):") - assert(tonumber(line) == l) + local line = tonumber(string.match(msg, ":(%d+):")) + assert(line == l or (not line and not l)) end lineerror("local a\n for i=1,'a' do \n print(i) \n end", 2) @@ -359,7 +359,7 @@ local p = [[ g() ]] X=3;lineerror((p), 3) -X=0;lineerror((p), nil) +X=0;lineerror((p), false) X=1;lineerror((p), 2) X=2;lineerror((p), 1) @@ -510,7 +510,7 @@ checksyntax("a\1a = 1", "", "<\\1>", 1) checksyntax("\255a = 1", "", "<\\255>", 1) doit('I = load("a=9+"); a=3') -assert(a==3 and I == nil) +assert(a==3 and not I) print('+') lim = 1000 diff --git a/testes/files.lua b/testes/files.lua index 585e5948d3..677c0dc2df 100644 --- a/testes/files.lua +++ b/testes/files.lua @@ -184,7 +184,7 @@ three local f = assert(io.open(file, "r")) -- second item failing l1, n1, n2, dummy = f:read("l", "n", "n", "l") - assert(l1 == "a line" and n1 == nil) + assert(l1 == "a line" and not n1) end assert(os.remove(file)) @@ -228,7 +228,7 @@ assert(f:read("n") == 0Xdeadbeefdeadbeef); assert(f:read(2) == "x\n") assert(f:read("n") == 0x1.13aP3); assert(f:read(1) == "e") do -- attempt to read too long number - assert(f:read("n") == nil) -- fails + assert(not f:read("n")) -- fails local s = f:read("L") -- read rest of line assert(string.find(s, "^00*\n$")) -- lots of 0's left end @@ -314,13 +314,13 @@ assert(io.read() == "fourth_line") assert(io.read() == "") -- empty line assert(io.read('n') == 3450) assert(io.read(1) == '\n') -assert(io.read(0) == nil) -- end of file -assert(io.read(1) == nil) -- end of file -assert(io.read(30000) == nil) -- end of file +assert(not io.read(0)) -- end of file +assert(not io.read(1)) -- end of file +assert(not io.read(30000)) -- end of file assert(({io.read(1)})[2] == undef) -assert(io.read() == nil) -- end of file +assert(not io.read()) -- end of file assert(({io.read()})[2] == undef) -assert(io.read('n') == nil) -- end of file +assert(not io.read('n')) -- end of file assert(({io.read('n')})[2] == undef) assert(io.read('a') == '') -- end of file (OK for 'a') assert(io.read('a') == '') -- end of file (OK for 'a') @@ -356,7 +356,7 @@ assert(io.read(string.len(t)) == t) assert(io.read(1) == ' ') assert(io.read(0)) assert(io.read('a') == ';end of file\n') -assert(io.read(0) == nil) +assert(not io.read(0)) assert(io.close(io.input())) @@ -364,7 +364,7 @@ assert(io.close(io.input())) do local function ismsg (m) -- error message is not a code number - return (type(m) == "string" and tonumber(m) == nil) + return (type(m) == "string" and not tonumber(m)) end -- read @@ -393,7 +393,7 @@ assert(io.read"L" == "\n") assert(io.read"L" == "\n") assert(io.read"L" == "line\n") assert(io.read"L" == "other") -assert(io.read"L" == nil) +assert(not io.read"L") io.input():close() local f = assert(io.open(file)) @@ -462,7 +462,7 @@ end -- test for multipe arguments in 'lines' io.output(file); io.write"0123456789\n":close() for a,b in io.lines(file, 1, 1) do - if a == "\n" then assert(b == nil) + if a == "\n" then assert(not b) else assert(tonumber(a) == tonumber(b) - 1) end end @@ -473,13 +473,13 @@ end for a,b,c in io.lines(file, "a", 0, 1) do if a == "" then break end - assert(a == "0123456789\n" and b == nil and c == nil) + assert(a == "0123456789\n" and not b and not c) end collectgarbage() -- to close file in previous iteration io.output(file); io.write"00\n10\n20\n30\n40\n":close() for a, b in io.lines(file, "n", "n") do - if a == 40 then assert(b == nil) + if a == 40 then assert(not b) else assert(a == b - 10) end end @@ -654,7 +654,7 @@ and the rest of the file io.input(file) local _,a,b,c,d,e,h,__ = io.read(1, 'n', 'n', 'l', 'l', 'l', 'a', 10) assert(io.close(io.input())) -assert(_ == ' ' and __ == nil) +assert(_ == ' ' and not __) assert(type(a) == 'number' and a==123.4 and b==-56e-2) assert(d=='second line' and e=='third line') assert(h==[[ @@ -706,7 +706,7 @@ if not _soft then io.input():seek('set', 0) y = io.read() -- huge line assert(x == y..'\n'..io.read()) - assert(io.read() == nil) + assert(not io.read()) io.close(io.input()) assert(os.remove(file)) x = nil; y = nil diff --git a/testes/literals.lua b/testes/literals.lua index 27f9377df1..e101eabfd8 100644 --- a/testes/literals.lua +++ b/testes/literals.lua @@ -281,7 +281,7 @@ if os.setlocale("pt_BR") or os.setlocale("ptb") then assert(" 0x.1 " + " 0x,1" + "-0X.1\t" == 0x0.1) - assert(tonumber"inf" == nil and tonumber"NAN" == nil) + assert(not tonumber"inf" and not tonumber"NAN") assert(assert(load(string.format("return %q", 4.51)))() == 4.51) diff --git a/testes/math.lua b/testes/math.lua index bad4390188..c7dc82851b 100644 --- a/testes/math.lua +++ b/testes/math.lua @@ -39,7 +39,7 @@ do end assert(math.type(0) == "integer" and math.type(0.0) == "float" - and math.type("10") == nil) + and not math.type("10")) local function checkerror (msg, f, ...) @@ -381,17 +381,17 @@ assert(tonumber(1/0) == 1/0) -- 'tonumber' with strings assert(tonumber("0") == 0) -assert(tonumber("") == nil) -assert(tonumber(" ") == nil) -assert(tonumber("-") == nil) -assert(tonumber(" -0x ") == nil) -assert(tonumber{} == nil) +assert(not tonumber("")) +assert(not tonumber(" ")) +assert(not tonumber("-")) +assert(not tonumber(" -0x ")) +assert(not tonumber{}) assert(tonumber'+0.01' == 1/100 and tonumber'+.01' == 0.01 and tonumber'.01' == 0.01 and tonumber'-1.' == -1 and tonumber'+1.' == 1) -assert(tonumber'+ 0.01' == nil and tonumber'+.e1' == nil and - tonumber'1e' == nil and tonumber'1.0e+' == nil and - tonumber'.' == nil) +assert(not tonumber'+ 0.01' and not tonumber'+.e1' and + not tonumber'1e' and not tonumber'1.0e+' and + not tonumber'.') assert(tonumber('-012') == -010-2) assert(tonumber('-1.2e2') == - - -120) @@ -445,45 +445,45 @@ local function f (...) end end -assert(f(tonumber('fFfa', 15)) == nil) -assert(f(tonumber('099', 8)) == nil) -assert(f(tonumber('1\0', 2)) == nil) -assert(f(tonumber('', 8)) == nil) -assert(f(tonumber(' ', 9)) == nil) -assert(f(tonumber(' ', 9)) == nil) -assert(f(tonumber('0xf', 10)) == nil) +assert(not f(tonumber('fFfa', 15))) +assert(not f(tonumber('099', 8))) +assert(not f(tonumber('1\0', 2))) +assert(not f(tonumber('', 8))) +assert(not f(tonumber(' ', 9))) +assert(not f(tonumber(' ', 9))) +assert(not f(tonumber('0xf', 10))) -assert(f(tonumber('inf')) == nil) -assert(f(tonumber(' INF ')) == nil) -assert(f(tonumber('Nan')) == nil) -assert(f(tonumber('nan')) == nil) +assert(not f(tonumber('inf'))) +assert(not f(tonumber(' INF '))) +assert(not f(tonumber('Nan'))) +assert(not f(tonumber('nan'))) -assert(f(tonumber(' ')) == nil) -assert(f(tonumber('')) == nil) -assert(f(tonumber('1 a')) == nil) -assert(f(tonumber('1 a', 2)) == nil) -assert(f(tonumber('1\0')) == nil) -assert(f(tonumber('1 \0')) == nil) -assert(f(tonumber('1\0 ')) == nil) -assert(f(tonumber('e1')) == nil) -assert(f(tonumber('e 1')) == nil) -assert(f(tonumber(' 3.4.5 ')) == nil) +assert(not f(tonumber(' '))) +assert(not f(tonumber(''))) +assert(not f(tonumber('1 a'))) +assert(not f(tonumber('1 a', 2))) +assert(not f(tonumber('1\0'))) +assert(not f(tonumber('1 \0'))) +assert(not f(tonumber('1\0 '))) +assert(not f(tonumber('e1'))) +assert(not f(tonumber('e 1'))) +assert(not f(tonumber(' 3.4.5 '))) -- testing 'tonumber' for invalid hexadecimal formats -assert(tonumber('0x') == nil) -assert(tonumber('x') == nil) -assert(tonumber('x3') == nil) -assert(tonumber('0x3.3.3') == nil) -- two decimal points -assert(tonumber('00x2') == nil) -assert(tonumber('0x 2') == nil) -assert(tonumber('0 x2') == nil) -assert(tonumber('23x') == nil) -assert(tonumber('- 0xaa') == nil) -assert(tonumber('-0xaaP ') == nil) -- no exponent -assert(tonumber('0x0.51p') == nil) -assert(tonumber('0x5p+-2') == nil) +assert(not tonumber('0x')) +assert(not tonumber('x')) +assert(not tonumber('x3')) +assert(not tonumber('0x3.3.3')) -- two decimal points +assert(not tonumber('00x2')) +assert(not tonumber('0x 2')) +assert(not tonumber('0 x2')) +assert(not tonumber('23x')) +assert(not tonumber('- 0xaa')) +assert(not tonumber('-0xaaP ')) -- no exponent +assert(not tonumber('0x0.51p')) +assert(not tonumber('0x5p+-2')) -- testing hexadecimal numerals @@ -705,19 +705,19 @@ do -- testing floor & ceil assert(eqT(math.tointeger(maxint), maxint)) assert(eqT(math.tointeger(maxint .. ""), maxint)) assert(eqT(math.tointeger(minint + 0.0), minint)) - assert(math.tointeger(0.0 - minint) == nil) - assert(math.tointeger(math.pi) == nil) - assert(math.tointeger(-math.pi) == nil) + assert(not math.tointeger(0.0 - minint)) + assert(not math.tointeger(math.pi)) + assert(not math.tointeger(-math.pi)) assert(math.floor(math.huge) == math.huge) assert(math.ceil(math.huge) == math.huge) - assert(math.tointeger(math.huge) == nil) + assert(not math.tointeger(math.huge)) assert(math.floor(-math.huge) == -math.huge) assert(math.ceil(-math.huge) == -math.huge) - assert(math.tointeger(-math.huge) == nil) + assert(not math.tointeger(-math.huge)) assert(math.tointeger("34.0") == 34) - assert(math.tointeger("34.3") == nil) - assert(math.tointeger({}) == nil) - assert(math.tointeger(0/0) == nil) -- NaN + assert(not math.tointeger("34.3")) + assert(not math.tointeger({})) + assert(not math.tointeger(0/0)) -- NaN end diff --git a/testes/pm.lua b/testes/pm.lua index 4d87fad210..94bb63ca5e 100644 --- a/testes/pm.lua +++ b/testes/pm.lua @@ -28,10 +28,10 @@ a,b = string.find('a\0a\0a\0a\0\0ab', '\0ab', 2); -- finds at the end assert(a == 9 and b == 11); a,b = string.find('a\0a\0a\0a\0\0ab', 'b') -- last position assert(a == 11 and b == 11) -assert(string.find('a\0a\0a\0a\0\0ab', 'b\0') == nil) -- check ending -assert(string.find('', '\0') == nil) +assert(not string.find('a\0a\0a\0a\0\0ab', 'b\0')) -- check ending +assert(not string.find('', '\0')) assert(string.find('alo123alo', '12') == 4) -assert(string.find('alo123alo', '^12') == nil) +assert(not string.find('alo123alo', '^12')) assert(string.match("aaab", ".*b") == "aaab") assert(string.match("aaa", ".*a") == "aaa") @@ -57,17 +57,17 @@ assert(f('aaa', 'ab*a') == 'aa') assert(f('aba', 'ab*a') == 'aba') assert(f('aaab', 'a+') == 'aaa') assert(f('aaa', '^.+$') == 'aaa') -assert(f('aaa', 'b+') == nil) -assert(f('aaa', 'ab+a') == nil) +assert(not f('aaa', 'b+')) +assert(not f('aaa', 'ab+a')) assert(f('aba', 'ab+a') == 'aba') assert(f('a$a', '.$') == 'a') assert(f('a$a', '.%$') == 'a$') assert(f('a$a', '.$.') == 'a$a') -assert(f('a$a', '$$') == nil) -assert(f('a$b', 'a$') == nil) +assert(not f('a$a', '$$')) +assert(not f('a$b', 'a$')) assert(f('a$a', '$') == '') assert(f('', 'b*') == '') -assert(f('aaa', 'bb*') == nil) +assert(not f('aaa', 'bb*')) assert(f('aaab', 'a-') == '') assert(f('aaa', '^.-$') == 'aaa') assert(f('aabaaabaaabaaaba', 'b.*b') == 'baaabaaabaaab') @@ -101,7 +101,7 @@ end assert(f1('alo alx 123 b\0o b\0o', '(..*) %1') == "b\0o b\0o") assert(f1('axz123= 4= 4 34', '(.+)=(.*)=%2 %1') == '3= 4= 4 3') assert(f1('=======', '^(=*)=%1$') == '=======') -assert(string.match('==========', '^([=]*)=%1$') == nil) +assert(not string.match('==========', '^([=]*)=%1$')) local function range (i, j) if i <= j then @@ -135,7 +135,7 @@ print('+'); assert(string.match("alo xyzK", "(%w+)K") == "xyz") assert(string.match("254 K", "(%d*)K") == "") assert(string.match("alo ", "(%w*)$") == "") -assert(string.match("alo ", "(%w+)$") == nil) +assert(not string.match("alo ", "(%w+)$")) assert(string.find("(lo)", "%(") == 1) local a, b, c, d, e = string.match("lo alo", "^(((.).).* (%w*))$") assert(a == 'lo alo' and b == 'l' and c == '' and d == 'alo' and e == nil) @@ -209,7 +209,7 @@ assert(s == r and t[1] == 1 and t[3] == 3 and t[7] == 4 and t[13] == 4) function isbalanced (s) - return string.find(string.gsub(s, "%b()", ""), "[()]") == nil + return not string.find(string.gsub(s, "%b()", ""), "[()]") end assert(isbalanced("(9 ((8))(\0) 7) \0\0 a b ()(c)() a")) diff --git a/testes/strings.lua b/testes/strings.lua index 2e0e160f23..97875ec0da 100644 --- a/testes/strings.lua +++ b/testes/strings.lua @@ -56,13 +56,13 @@ a,b = string.find("123456789", "345") assert(string.sub("123456789", a, b) == "345") assert(string.find("1234567890123456789", "345", 3) == 3) assert(string.find("1234567890123456789", "345", 4) == 13) -assert(string.find("1234567890123456789", "346", 4) == nil) +assert(not string.find("1234567890123456789", "346", 4)) assert(string.find("1234567890123456789", ".45", -9) == 13) -assert(string.find("abcdefg", "\0", 5, 1) == nil) +assert(not string.find("abcdefg", "\0", 5, 1)) assert(string.find("", "") == 1) assert(string.find("", "", 1) == 1) assert(not string.find("", "", 2)) -assert(string.find('', 'aaa', 1) == nil) +assert(not string.find('', 'aaa', 1)) assert(('alo(.)alo'):find('(.)', 1, 1) == 4) assert(string.len("") == 0) diff --git a/testes/utf8.lua b/testes/utf8.lua index acbb181d24..5954f6e89e 100644 --- a/testes/utf8.lua +++ b/testes/utf8.lua @@ -30,8 +30,8 @@ local function checksyntax (s, t) assert(assert(load(ts))() == s) end -assert(utf8.offset("alo", 5) == nil) -assert(utf8.offset("alo", -4) == nil) +assert(not utf8.offset("alo", 5)) +assert(not utf8.offset("alo", -4)) -- 'check' makes several tests over the validity of string 's'. -- 't' is the list of codepoints of 's'. From 45948e7e55c753cf0e370b251ac1d4e7f3aabedd Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 16 Aug 2019 16:11:21 -0300 Subject: [PATCH 0485/1145] Manual corrected with the new syntax for attributes Commit 0d529138042, with the change in the syntax of attributes, did not update the manual accordingly. --- manual/manual.of | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/manual/manual.of b/manual/manual.of index 53073a5464..a3990d33fa 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -1499,13 +1499,13 @@ The declaration can include an initialization: @Produc{ @producname{stat}@producbody{@Rw{local} attnamelist @bnfopt{@bnfter{=} explist}} @producname{attnamelist}@producbody{ - attrib @bnfNter{Name} @bnfrep{@bnfter{,} attrib @bnfNter{Name}}} + @bnfNter{Name} attrib @bnfrep{@bnfter{,} @bnfNter{Name} attrib}} } If present, an initial assignment has the same semantics of a multiple assignment @see{assignment}. Otherwise, all variables are initialized with @nil. -Each variable name may be preceded by an attribute +Each variable name may be postfixed by an attribute (a name between angle brackets): @Produc{ @producname{attrib}@producbody{@bnfopt{@bnfter{<} @bnfNter{Name} @bnfter{>}}} @@ -1514,7 +1514,7 @@ There are two possible attributes: @id{const}, which declares a @x{constant variable}, that is, a variable that cannot be assigned to after its initialization; -and @id{toclose}, which declares a to-be-closed variable @see{to-be-closed}. +and @id{close}, which declares a to-be-closed variable @see{to-be-closed}. A list of variables can contain at most one to-be-closed variable. A chunk is also a block @see{chunks}, @@ -1569,7 +1569,7 @@ Similarly, if a coroutine ends with an error, it does not unwind its stack, so it does not close any variable. In both cases, -you should either use finalizers +you can either use finalizers or call @Lid{coroutine.close} to close the variables. However, if the coroutine was created through @Lid{coroutine.wrap}, @@ -9066,7 +9066,7 @@ and @bnfNter{LiteralString}, see @See{lexical}.) } @producname{attnamelist}@producbody{ - attrib @bnfNter{Name} @bnfrep{@bnfter{,} attrib @bnfNter{Name}}} + @bnfNter{Name} attrib @bnfrep{@bnfter{,} @bnfNter{Name} attrib}} @producname{attrib}@producbody{@bnfopt{@bnfter{<} @bnfNter{Name} @bnfter{>}}} From 9405472565cb4b0cb0c339d65babdef4d4cb7abd Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Sun, 18 Aug 2019 17:29:46 -0300 Subject: [PATCH 0486/1145] Improvement in warn-mode '@store' (for testing) When using warn-mode '@store', from the test library, the tests ensure not only that the expected warnings were issued, but also that there was no extra warnings. --- ltests.c | 28 ++++++++++++++++++++-------- testes/coroutine.lua | 11 ++++++++--- testes/gc.lua | 6 +++--- testes/locals.lua | 35 +++++++++++++++++++++-------------- testes/main.lua | 6 +++--- 5 files changed, 55 insertions(+), 31 deletions(-) diff --git a/ltests.c b/ltests.c index b460d01894..95c41fd9e6 100644 --- a/ltests.c +++ b/ltests.c @@ -62,8 +62,10 @@ static void pushobject (lua_State *L, const TValue *o) { } -static void badexit (const char *fmt, const char *s) { - fprintf(stderr, fmt, s); +static void badexit (const char *fmt, const char *s1, const char *s2) { + fprintf(stderr, fmt, s1); + if (s2) + fprintf(stderr, "extra info: %s\n", s2); /* avoid assertion failures when exiting */ l_memcontrol.numblocks = l_memcontrol.total = 0; exit(EXIT_FAILURE); @@ -72,7 +74,7 @@ static void badexit (const char *fmt, const char *s) { static int tpanic (lua_State *L) { return (badexit("PANIC: unprotected error in call to Lua API (%s)\n", - lua_tostring(L, -1)), + lua_tostring(L, -1), NULL), 0); /* do not return to Lua */ } @@ -88,13 +90,14 @@ static int tpanic (lua_State *L) { ** - 2.store: all warnings go to the global '_WARN'; */ static void warnf (void *ud, const char *msg, int tocont) { + lua_State *L = cast(lua_State *, ud); static char buff[200] = ""; /* should be enough for tests... */ static int onoff = 1; static int mode = 0; /* start in normal mode */ static int lasttocont = 0; if (!lasttocont && !tocont && *msg == '@') { /* control message? */ if (buff[0] != '\0') - badexit("Control warning during warning: %s\naborting...\n", msg); + badexit("Control warning during warning: %s\naborting...\n", msg, buff); if (strcmp(msg, "@off") == 0) onoff = 0; else if (strcmp(msg, "@on") == 0) @@ -106,18 +109,28 @@ static void warnf (void *ud, const char *msg, int tocont) { else if (strcmp(msg, "@store") == 0) mode = 2; else - badexit("Invalid control warning in test mode: %s\naborting...\n", msg); + badexit("Invalid control warning in test mode: %s\naborting...\n", + msg, NULL); return; } lasttocont = tocont; if (strlen(msg) >= sizeof(buff) - strlen(buff)) - badexit("%s", "warnf-buffer overflow"); + badexit("warnf-buffer overflow (%s)\n", msg, buff); strcat(buff, msg); /* add new message to current warning */ if (!tocont) { /* message finished? */ + lua_unlock(L); + if (lua_getglobal(L, "_WARN") == LUA_TNIL) + lua_pop(L, 1); /* ok, no previous unexpected warning */ + else { + badexit("Unhandled warning in store mode: %s\naborting...\n", + lua_tostring(L, -1), buff); + } + lua_lock(L); switch (mode) { case 0: { /* normal */ if (buff[0] != '#' && onoff) /* unexpected warning? */ - badexit("Unexpected warning in test mode: %s\naborting...\n", buff); + badexit("Unexpected warning in test mode: %s\naborting...\n", + buff, NULL); /* else */ /* FALLTHROUGH */ } case 1: { /* allow */ @@ -126,7 +139,6 @@ static void warnf (void *ud, const char *msg, int tocont) { break; } case 2: { /* store */ - lua_State *L = cast(lua_State *, ud); lua_unlock(L); lua_pushstring(L, buff); lua_setglobal(L, "_WARN"); /* assign message to global '_WARN' */ diff --git a/testes/coroutine.lua b/testes/coroutine.lua index 79c72a9dc6..4fc23261f1 100644 --- a/testes/coroutine.lua +++ b/testes/coroutine.lua @@ -177,10 +177,15 @@ do end) coroutine.resume(co) assert(x == 0) - _WARN = nil; warn("@off"); warn("@store") + -- with test library, use 'store' mode to check warnings + warn(not T and "@off" or "@store") local st, msg = coroutine.close(co) - warn("@on"); warn("@normal") - assert(_WARN == nil or string.find(_WARN, "200")) + if not T then + warn("@on") + else -- test library + assert(string.find(_WARN, "200")); _WARN = nil + warn("@normal") + end assert(st == false and coroutine.status(co) == "dead" and msg == 111) assert(x == 200) diff --git a/testes/gc.lua b/testes/gc.lua index 6bdc98ca1d..34854d6f3f 100644 --- a/testes/gc.lua +++ b/testes/gc.lua @@ -372,7 +372,7 @@ if T then warn("@store") collectgarbage() assert(string.find(_WARN, "error in __gc metamethod")) - assert(string.match(_WARN, "@(.-)@") == "expected") + assert(string.match(_WARN, "@(.-)@") == "expected"); _WARN = nil for i = 8, 10 do assert(s[i]) end for i = 1, 5 do @@ -481,6 +481,7 @@ if T then u = setmetatable({}, {__gc = function () error "@expected error" end}) u = nil collectgarbage() + assert(string.find(_WARN, "@expected error")); _WARN = nil warn("@normal") end @@ -663,9 +664,8 @@ if T then else assert(lastmsg == _WARN) -- subsequent error messages are equal end - warn("@store") + warn("@store"); _WARN = nil error"@expected warning" - warn("@normal") end} for i = 10, 1, -1 do -- create object and preserve it until the end diff --git a/testes/locals.lua b/testes/locals.lua index 595e107af5..b4de523dc7 100644 --- a/testes/locals.lua +++ b/testes/locals.lua @@ -288,9 +288,8 @@ end -- auxiliary functions for testing warnings in '__close' local function prepwarn () - warn("@off") -- do not show (lots of) warnings - if not T then - _WARN = "OFF" -- signal that warnings are not being captured + if not T then -- no test library? + warn("@off") -- do not show (lots of) warnings else warn("@store") -- to test the warnings end @@ -298,15 +297,20 @@ end local function endwarn () - assert(T or _WARN == "OFF") - warn("@on") -- back to normal - warn("@normal") - _WARN = nil + if not T then + warn("@on") -- back to normal + else + assert(_WARN == nil) + warn("@normal") + end end local function checkwarn (msg) - assert(_WARN == "OFF" or string.find(_WARN, msg)) + if T then + assert(string.find(_WARN, msg)) + _WARN = nil -- reset variable to check next warning + end end @@ -333,7 +337,8 @@ do print("testing errors in __close") local y = func2close(function (self, msg) - assert(string.find(msg, "@z")) -- error in 'z' + assert(string.find(msg, "@z")) -- first error in 'z' + checkwarn("@z") -- second error in 'z' generated a warning error("@y") end) @@ -377,14 +382,14 @@ do print("testing errors in __close") local y = func2close(function (self, msg) - assert(msg == 4) -- error in body + assert(msg == 4) -- error in body + checkwarn("@z") error("@y") end) local first = true local z = func2close(function (self, msg) - checkwarn("@z") -- 'z' close is called once assert(first and msg == 4) first = false @@ -418,16 +423,18 @@ do print("testing errors in __close") local st, msg = xpcall(foo, debug.traceback) assert(string.match(msg, "^[^ ]* @X")) assert(string.find(msg, "in metamethod 'close'")) + checkwarn("@Y") -- error in toclose in vararg function local function foo (...) - local x123 = func2close(function () error("@X") end) + local x123 = func2close(function () error("@x123") end) end local st, msg = xpcall(foo, debug.traceback) - assert(string.match(msg, "^[^ ]* @X")) - + assert(string.match(msg, "^[^ ]* @x123")) assert(string.find(msg, "in metamethod 'close'")) + checkwarn("@x123") -- from second call to close 'x123' + endwarn() end diff --git a/testes/main.lua b/testes/main.lua index 0ef4822d7f..362203622e 100644 --- a/testes/main.lua +++ b/testes/main.lua @@ -379,12 +379,12 @@ if T then -- test library? -- testing 'warn' warn("@store") warn("@123", "456", "789") - assert(_WARN == "@123456789") + assert(_WARN == "@123456789"); _WARN = nil warn("zip", "", " ", "zap") - assert(_WARN == "zip zap") + assert(_WARN == "zip zap"); _WARN = nil warn("ZIP", "", " ", "ZAP") - assert(_WARN == "ZIP ZAP") + assert(_WARN == "ZIP ZAP"); _WARN = nil warn("@normal") end From 5bc47fe83087e0686f4639d031801837846e4c65 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 19 Aug 2019 14:41:48 -0300 Subject: [PATCH 0487/1145] Detail (extra test for warnings when closing state) --- testes/main.lua | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/testes/main.lua b/testes/main.lua index 362203622e..5d2652cb71 100644 --- a/testes/main.lua +++ b/testes/main.lua @@ -243,6 +243,17 @@ Lua warning: @on Lua warning: ZZZ ]] +prepfile[[ +warn("@allow") +-- create two objects to be finalized when closing state +-- the errors in the finalizers must generate warnings +u1 = setmetatable({}, {__gc = function () error("XYZ") end}) +u2 = setmetatable({}, {__gc = function () error("ZYX") end}) +]] +RUN('lua %s 2> %s', prog, out) +checkprogout("ZYX)\nXYZ)\n") + + -- test many arguments prepfile[[print(({...})[30])]] RUN('lua %s %s > %s', prog, string.rep(" a", 30), out) From be78aeae4c429d7d68af3a3e1b0cf8e52fcff160 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 20 Aug 2019 13:42:26 -0300 Subject: [PATCH 0488/1145] Default for warnings changed to "off" Warnings are mostly a tool to help developers (e.g., by showing hidden error messages); regular users usually don't need to see them. --- all | 2 +- lauxlib.c | 2 +- ltests.c | 2 +- lua.c | 10 +++++----- manual/manual.of | 4 ++-- testes/all.lua | 5 +++-- testes/coroutine.lua | 1 + testes/gc.lua | 2 +- testes/locals.lua | 1 + testes/main.lua | 11 +++++++---- 10 files changed, 23 insertions(+), 17 deletions(-) diff --git a/all b/all index 8f78ee4d5b..2a8cb92f9e 100755 --- a/all +++ b/all @@ -2,7 +2,7 @@ make -s -j cd testes/libs; make -s cd .. # back to directory 'testes' ulimit -S -s 2000 -if { ../lua all.lua; } then +if { ../lua -W all.lua; } then echo -e "\n\n final OK!!!!\n\n" else echo -e "\n\n >>>> BUG!!!!\n\n" diff --git a/lauxlib.c b/lauxlib.c index 5a040ac645..b6740b17ac 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -1037,7 +1037,7 @@ LUALIB_API lua_State *luaL_newstate (void) { lua_atpanic(L, &panic); warnstate = (int *)lua_newuserdatauv(L, sizeof(int), 0); luaL_ref(L, LUA_REGISTRYINDEX); /* make sure it won't be collected */ - *warnstate = 1; /* next message starts a new warning */ + *warnstate = 0; /* default is warnings off */ lua_setwarnf(L, warnf, warnstate); } return L; diff --git a/ltests.c b/ltests.c index 95c41fd9e6..0d4ec938e1 100644 --- a/ltests.c +++ b/ltests.c @@ -92,7 +92,7 @@ static int tpanic (lua_State *L) { static void warnf (void *ud, const char *msg, int tocont) { lua_State *L = cast(lua_State *, ud); static char buff[200] = ""; /* should be enough for tests... */ - static int onoff = 1; + static int onoff = 0; static int mode = 0; /* start in normal mode */ static int lasttocont = 0; if (!lasttocont && !tocont && *msg == '@') { /* control message? */ diff --git a/lua.c b/lua.c index d13e203b07..18f53c3041 100644 --- a/lua.c +++ b/lua.c @@ -73,7 +73,7 @@ static void print_usage (const char *badoption) { " -l name require library 'name' into global 'name'\n" " -v show version information\n" " -E ignore environment variables\n" - " -q turn warnings off\n" + " -W turn warnings on\n" " -- stop handling options\n" " - stop handling options and execute stdin\n" , @@ -264,7 +264,7 @@ static int collectargs (char **argv, int *first) { return has_error; /* invalid option */ args |= has_E; break; - case 'q': + case 'W': if (argv[i][2] != '\0') /* extra characters? */ return has_error; /* invalid option */ break; @@ -295,7 +295,7 @@ static int collectargs (char **argv, int *first) { /* ** Processes options 'e' and 'l', which involve running Lua code, and -** 'q', which also affects the state. +** 'W', which also affects the state. ** Returns 0 if some code raises an error. */ static int runargs (lua_State *L, char **argv, int n) { @@ -315,8 +315,8 @@ static int runargs (lua_State *L, char **argv, int n) { if (status != LUA_OK) return 0; break; } - case 'q': - lua_warning(L, "@off", 0); /* no warnings */ + case 'W': + lua_warning(L, "@on", 0); /* warnings on */ break; } } diff --git a/manual/manual.of b/manual/manual.of index a3990d33fa..292d1e515c 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -8735,7 +8735,7 @@ The options are: @item{@T{-i}| enters interactive mode after running @rep{script};} @item{@T{-v}| prints version information;} @item{@T{-E}| ignores environment variables;} -@item{@T{-q}| turn warnings off;} +@item{@T{-W}| turn warnings on;} @item{@T{--}| stops handling options;} @item{@T{-}| executes @id{stdin} as a file and stops handling options.} } @@ -8761,7 +8761,7 @@ setting the values of @Lid{package.path} and @Lid{package.cpath} with the default paths defined in @id{luaconf.h}. -The options @T{-e}, @T{-l}, and @T{-q} are handled in +The options @T{-e}, @T{-l}, and @T{-W} are handled in the order they appear. For instance, an invocation like @verbatim{ diff --git a/testes/all.lua b/testes/all.lua index 42809b9a01..db074dd8b4 100644 --- a/testes/all.lua +++ b/testes/all.lua @@ -5,8 +5,8 @@ local version = "Lua 5.4" if _VERSION ~= version then - warn("This test suite is for ", version, - ", not for ", _VERSION, "\nExiting tests") + io.stderr:write("This test suite is for ", version, + ", not for ", _VERSION, "\nExiting tests") return end @@ -210,6 +210,7 @@ if #msgs > 0 then end print("(there should be two warnings now)") +warn("@on") warn("#This is ", "an expected", " warning") warn("@off") warn("******** THIS WARNING SHOULD NOT APPEAR **********") diff --git a/testes/coroutine.lua b/testes/coroutine.lua index 4fc23261f1..79bbf2ea2a 100644 --- a/testes/coroutine.lua +++ b/testes/coroutine.lua @@ -163,6 +163,7 @@ do assert(not X and coroutine.status(co) == "dead") -- error closing a coroutine + warn("@on") local x = 0 co = coroutine.create(function() local y = func2close(function (self,err) diff --git a/testes/gc.lua b/testes/gc.lua index 34854d6f3f..bb4e349308 100644 --- a/testes/gc.lua +++ b/testes/gc.lua @@ -369,7 +369,7 @@ if T then s[n] = i end - warn("@store") + warn("@on"); warn("@store") collectgarbage() assert(string.find(_WARN, "error in __gc metamethod")) assert(string.match(_WARN, "@(.-)@") == "expected"); _WARN = nil diff --git a/testes/locals.lua b/testes/locals.lua index b4de523dc7..58ad18cc55 100644 --- a/testes/locals.lua +++ b/testes/locals.lua @@ -313,6 +313,7 @@ local function checkwarn (msg) end end +warn("@on") do print("testing errors in __close") diff --git a/testes/main.lua b/testes/main.lua index 5d2652cb71..de14a08891 100644 --- a/testes/main.lua +++ b/testes/main.lua @@ -221,8 +221,11 @@ assert(string.find(getoutput(), "error calling 'print'")) RUN('echo "io.stderr:write(1000)\ncont" | lua -e "require\'debug\'.debug()" 2> %s', out) checkout("lua_debug> 1000lua_debug> ") --- test warnings -RUN('echo "io.stderr:write(1); warn[[XXX]]" | lua -q 2> %s', out) + +print("testing warnings") + +-- no warnings by default +RUN('echo "io.stderr:write(1); warn[[XXX]]" | lua 2> %s', out) checkout("1") prepfile[[ @@ -236,7 +239,7 @@ warn("", "@on") -- again, no control, real warning warn("@on") -- keep it "started" warn("Z", "Z", "Z") -- common warning ]] -RUN('lua %s 2> %s', prog, out) +RUN('lua -W %s 2> %s', prog, out) checkout[[ Lua warning: @offXXX@off Lua warning: @on @@ -250,7 +253,7 @@ warn("@allow") u1 = setmetatable({}, {__gc = function () error("XYZ") end}) u2 = setmetatable({}, {__gc = function () error("ZYX") end}) ]] -RUN('lua %s 2> %s', prog, out) +RUN('lua -W %s 2> %s', prog, out) checkprogout("ZYX)\nXYZ)\n") From 3df5624ff432b340fe122988fe6d025ad3217946 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 21 Aug 2019 12:19:47 -0300 Subject: [PATCH 0489/1145] Fixed bug when yiedling inside OP_ADDK opcode The family of opcodes OP_ADDK (arithmetic operators with K constant) were not being handled in 'luaV_finishOp', which completes their task after an yield. --- lvm.c | 3 +++ testes/coroutine.lua | 11 +++++++++++ 2 files changed, 14 insertions(+) diff --git a/lvm.c b/lvm.c index 1cfc10358f..303954f058 100644 --- a/lvm.c +++ b/lvm.c @@ -720,6 +720,9 @@ void luaV_finishOp (lua_State *L) { case OP_ADDI: case OP_SUBI: case OP_MULI: case OP_DIVI: case OP_IDIVI: case OP_MODI: case OP_POWI: + case OP_ADDK: case OP_SUBK: + case OP_MULK: case OP_DIVK: case OP_IDIVK: + case OP_MODK: case OP_POWK: case OP_ADD: case OP_SUB: case OP_MUL: case OP_DIV: case OP_IDIV: case OP_BANDK: case OP_BORK: case OP_BXORK: diff --git a/testes/coroutine.lua b/testes/coroutine.lua index 79bbf2ea2a..26ed1f6a76 100644 --- a/testes/coroutine.lua +++ b/testes/coroutine.lua @@ -724,6 +724,17 @@ assert(run(function () return a / b end, {"div"}) == 10/12) assert(run(function () return a % b end, {"mod"}) == 10) assert(run(function () return a // b end, {"idiv"}) == 0) +-- repeat tests with larger constants (to use 'K' opcodes) +local a1000 = new(1000) + +assert(run(function () return a1000 + 1000 end, {"add"}) == 2000) +assert(run(function () return a1000 - 25000 end, {"sub"}) == -24000) +assert(run(function () return 2000 * a end, {"mul"}) == 20000) +assert(run(function () return a1000 / 1000 end, {"div"}) == 1) +assert(run(function () return a1000 % 600 end, {"mod"}) == 400) +assert(run(function () return a1000 // 500 end, {"idiv"}) == 2) + + assert(run(function () return a % b end, {"mod"}) == 10) From 643188d6e58dfd3270d689230867289347260b74 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 27 Aug 2019 10:28:09 -0300 Subject: [PATCH 0490/1145] Fixed missing case in 'luaV_finishOp' A metamethod call like '1 << a' was not being properly resumed if it got yielded. --- lvm.c | 2 +- testes/coroutine.lua | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/lvm.c b/lvm.c index 303954f058..907417e3aa 100644 --- a/lvm.c +++ b/lvm.c @@ -727,7 +727,7 @@ void luaV_finishOp (lua_State *L) { case OP_MUL: case OP_DIV: case OP_IDIV: case OP_BANDK: case OP_BORK: case OP_BXORK: case OP_BAND: case OP_BOR: case OP_BXOR: - case OP_SHRI: case OP_SHL: case OP_SHR: + case OP_SHLI: case OP_SHRI: case OP_SHL: case OP_SHR: case OP_MOD: case OP_POW: case OP_UNM: case OP_BNOT: case OP_LEN: case OP_GETTABUP: case OP_GETTABLE: case OP_GETI: diff --git a/testes/coroutine.lua b/testes/coroutine.lua index 26ed1f6a76..48f4c5bfd3 100644 --- a/testes/coroutine.lua +++ b/testes/coroutine.lua @@ -747,6 +747,12 @@ assert(run(function () return a >> b end, {"shr"}) == 10 >> 12) assert(run(function () return 10 & b end, {"band"}) == 10 & 12) assert(run(function () return a | 2 end, {"bor"}) == 10 | 2) assert(run(function () return a ~ 2 end, {"bxor"}) == 10 ~ 2) +assert(run(function () return a >> 2 end, {"shr"}) == 10 >> 2) +assert(run(function () return 1 >> a end, {"shr"}) == 1 >> 10) +assert(run(function () return a << 2 end, {"shl"}) == 10 << 2) +assert(run(function () return 1 << a end, {"shl"}) == 1 << 10) +assert(run(function () return a ~ 2 end, {"bxor"}) == 10 ~ 2) + assert(run(function () return a..b end, {"concat"}) == "1012") From df13f259487459f3a28d31d76c890aa6c2d061e0 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 27 Aug 2019 13:59:39 -0300 Subject: [PATCH 0491/1145] First version of OP_MMBIN opcodes In arithmetic/bitwise operators, the call to metamethods is made in a separate opcode following the main one. (The main opcode skips this next one when the operation succeeds.) This change reduces slightly the size of the binary and the complexity of the arithmetic/bitwise opcodes. It also simplfies the treatment of errors and yeld/resume in these operations, as there are much fewer cases to consider. (Only OP_MMBIN/OP_MMBINI/OP_MMBINK, instead of all variants of all arithmetic/bitwise operators.) --- lcode.c | 46 ++++++++++++++---------- ldebug.c | 21 ++++------- ljumptab.h | 3 ++ lopcodes.c | 3 ++ lopcodes.h | 4 +++ lopnames.h | 3 ++ ltm.c | 4 +-- ltm.h | 2 +- lvm.c | 85 ++++++++++++++++++++++++-------------------- testes/code.lua | 63 +++++++++++++++++--------------- testes/coroutine.lua | 2 +- 11 files changed, 132 insertions(+), 104 deletions(-) diff --git a/lcode.c b/lcode.c index c2b5fc6d19..a1b27a00b9 100644 --- a/lcode.c +++ b/lcode.c @@ -1337,18 +1337,20 @@ static void codeunexpval (FuncState *fs, OpCode op, expdesc *e, int line) { ** (everything but logical operators 'and'/'or' and comparison ** operators). ** Expression to produce final result will be encoded in 'e1'. -** Because 'luaK_exp2anyreg' can free registers, its calls must be -** in "stack order" (that is, first on 'e2', which may have more -** recent registers to be released). */ static void finishbinexpval (FuncState *fs, expdesc *e1, expdesc *e2, - OpCode op, int v2, int k, int line) { + OpCode op, int v2, int k, int line, + OpCode mmop, TMS event) { int v1 = luaK_exp2anyreg(fs, e1); int pc = luaK_codeABCk(fs, op, 0, v1, v2, k); freeexps(fs, e1, e2); e1->u.info = pc; e1->k = VRELOC; /* all those operations are relocatable */ luaK_fixline(fs, line); +if (event != TM_SHL && event != TM_SHR) { + luaK_codeABCk(fs, mmop, v1, v2, event, k); /* to call metamethod */ + luaK_fixline(fs, line); +} } @@ -1359,7 +1361,9 @@ static void finishbinexpval (FuncState *fs, expdesc *e1, expdesc *e2, static void codebinexpval (FuncState *fs, OpCode op, expdesc *e1, expdesc *e2, int line) { int v2 = luaK_exp2anyreg(fs, e2); /* both operands are in registers */ - finishbinexpval(fs, e1, e2, op, v2, 0, line); + lua_assert(OP_ADD <= op && op <= OP_SHR); + finishbinexpval(fs, e1, e2, op, v2, 0, line, OP_MMBIN, + cast(TMS, (op - OP_ADD) + TM_ADD)); } @@ -1367,9 +1371,10 @@ static void codebinexpval (FuncState *fs, OpCode op, ** Code binary operators ('+', '-', ...) with immediate operands. */ static void codebini (FuncState *fs, OpCode op, - expdesc *e1, expdesc *e2, int k, int line) { + expdesc *e1, expdesc *e2, int k, int line, + TMS event) { int v2 = cast_int(e2->u.ival) + OFFSET_sC; /* immediate operand */ - finishbinexpval(fs, e1, e2, op, v2, k, line); + finishbinexpval(fs, e1, e2, op, v2, k, line, OP_MMBINI, event); } @@ -1383,16 +1388,18 @@ static void swapexps (expdesc *e1, expdesc *e2) { ** constant in the proper range, use variant opcodes with immediate ** operands or K operands. */ -static void codearith (FuncState *fs, OpCode op, +static void codearith (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2, int flip, int line) { + TMS event = cast(TMS, opr + TM_ADD); if (isSCint(e2)) /* immediate operand? */ - codebini(fs, cast(OpCode, op - OP_ADD + OP_ADDI), e1, e2, flip, line); + codebini(fs, cast(OpCode, opr + OP_ADDI), e1, e2, flip, line, event); else if (tonumeral(e2, NULL) && luaK_exp2K(fs, e2)) { /* K operand? */ int v2 = e2->u.info; /* K index */ - op = cast(OpCode, op - OP_ADD + OP_ADDK); - finishbinexpval(fs, e1, e2, op, v2, flip, line); + OpCode op = cast(OpCode, opr + OP_ADDK); + finishbinexpval(fs, e1, e2, op, v2, flip, line, OP_MMBINK, event); } else { /* 'e2' is neither an immediate nor a K operand */ + OpCode op = cast(OpCode, opr + OP_ADD); if (flip) swapexps(e1, e2); /* back to original order */ codebinexpval(fs, op, e1, e2, line); /* use standard operators */ @@ -1405,7 +1412,7 @@ static void codearith (FuncState *fs, OpCode op, ** numeric constant, change order of operands to try to use an ** immediate or K operator. */ -static void codecommutative (FuncState *fs, OpCode op, +static void codecommutative (FuncState *fs, BinOpr op, expdesc *e1, expdesc *e2, int line) { int flip = 0; if (tonumeral(e1, NULL)) { /* is first operand a numeric constant? */ @@ -1430,14 +1437,15 @@ static void codebitwise (FuncState *fs, BinOpr opr, inv = 1; } else if (!(e2->k == VKINT && luaK_exp2RK(fs, e2))) { /* no constants? */ - op = cast(OpCode, opr - OPR_BAND + OP_BAND); + op = cast(OpCode, opr + OP_ADD); codebinexpval(fs, op, e1, e2, line); /* all-register opcodes */ return; } v2 = e2->u.info; /* index in K array */ - op = cast(OpCode, opr - OPR_BAND + OP_BANDK); + op = cast(OpCode, opr + OP_ADDK); lua_assert(ttisinteger(&fs->f->k[v2])); - finishbinexpval(fs, e1, e2, op, v2, inv, line); + finishbinexpval(fs, e1, e2, op, v2, inv, line, OP_MMBINK, + cast(TMS, opr + TM_ADD)); } @@ -1453,7 +1461,7 @@ static void codeshift (FuncState *fs, OpCode op, changedir = 1; e2->u.ival = -(e2->u.ival); } - codebini(fs, OP_SHRI, e1, e2, changedir, line); + codebini(fs, OP_SHRI, e1, e2, changedir, line, TM_SHL); } else codebinexpval(fs, op, e1, e2, line); @@ -1638,13 +1646,13 @@ void luaK_posfix (FuncState *fs, BinOpr opr, } case OPR_ADD: case OPR_MUL: { if (!constfolding(fs, opr + LUA_OPADD, e1, e2)) - codecommutative(fs, cast(OpCode, opr + OP_ADD), e1, e2, line); + codecommutative(fs, opr, e1, e2, line); break; } case OPR_SUB: case OPR_DIV: case OPR_IDIV: case OPR_MOD: case OPR_POW: { if (!constfolding(fs, opr + LUA_OPADD, e1, e2)) - codearith(fs, cast(OpCode, opr + OP_ADD), e1, e2, 0, line); + codearith(fs, opr, e1, e2, 0, line); break; } case OPR_BAND: case OPR_BOR: case OPR_BXOR: { @@ -1656,7 +1664,7 @@ void luaK_posfix (FuncState *fs, BinOpr opr, if (!constfolding(fs, LUA_OPSHL, e1, e2)) { if (isSCint(e1)) { swapexps(e1, e2); - codebini(fs, OP_SHLI, e1, e2, 1, line); + codebini(fs, OP_SHLI, e1, e2, 1, line, TM_SHL); } else codeshift(fs, OP_SHL, e1, e2, line); diff --git a/ldebug.c b/ldebug.c index 9593039bf0..4e1dc6b9f7 100644 --- a/ldebug.c +++ b/ldebug.c @@ -471,6 +471,10 @@ static int findsetreg (const Proto *p, int lastpc, int reg) { int pc; int setreg = -1; /* keep last instruction that changed 'reg' */ int jmptarget = 0; /* any code before this address is conditional */ + if (GET_OPCODE(p->code[lastpc]) == OP_MMBIN || + GET_OPCODE(p->code[lastpc]) == OP_MMBINI || + GET_OPCODE(p->code[lastpc]) == OP_MMBINK) + lastpc--; for (pc = 0; pc < lastpc; pc++) { Instruction i = p->code[pc]; OpCode op = GET_OPCODE(i); @@ -620,22 +624,11 @@ static const char *funcnamefromcode (lua_State *L, CallInfo *ci, case OP_SETTABUP: case OP_SETTABLE: case OP_SETI: case OP_SETFIELD: tm = TM_NEWINDEX; break; - case OP_ADDI: case OP_SUBI: case OP_MULI: case OP_MODI: - case OP_POWI: case OP_DIVI: case OP_IDIVI: { - int offset = GET_OPCODE(i) - OP_ADDI; /* ORDER OP */ - tm = cast(TMS, offset + TM_ADD); /* ORDER TM */ - break; - } - case OP_ADDK: case OP_SUBK: case OP_MULK: case OP_MODK: - case OP_POWK: case OP_DIVK: case OP_IDIVK: - case OP_BANDK: case OP_BORK: case OP_BXORK: { - int offset = GET_OPCODE(i) - OP_ADDK; /* ORDER OP */ - tm = cast(TMS, offset + TM_ADD); /* ORDER TM */ + case OP_MMBIN: case OP_MMBINI: case OP_MMBINK: { + tm = cast(TMS, GETARG_C(i)); break; } - case OP_ADD: case OP_SUB: case OP_MUL: case OP_MOD: - case OP_POW: case OP_DIV: case OP_IDIV: case OP_BAND: - case OP_BOR: case OP_BXOR: case OP_SHL: case OP_SHR: { + case OP_SHL: case OP_SHR: { int offset = GET_OPCODE(i) - OP_ADD; /* ORDER OP */ tm = cast(TMS, offset + TM_ADD); /* ORDER TM */ break; diff --git a/ljumptab.h b/ljumptab.h index 2d4cf28bf6..1832c809a8 100644 --- a/ljumptab.h +++ b/ljumptab.h @@ -75,6 +75,9 @@ static void *disptab[NUM_OPCODES] = { &&L_OP_BXOR, &&L_OP_SHL, &&L_OP_SHR, +&&L_OP_MMBIN, +&&L_OP_MMBINI, +&&L_OP_MMBINK, &&L_OP_UNM, &&L_OP_BNOT, &&L_OP_NOT, diff --git a/lopcodes.c b/lopcodes.c index ee79578613..aede93a552 100644 --- a/lopcodes.c +++ b/lopcodes.c @@ -69,6 +69,9 @@ LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = { ,opmode(0, 0, 0, 1, iABC) /* OP_BXOR */ ,opmode(0, 0, 0, 1, iABC) /* OP_SHL */ ,opmode(0, 0, 0, 1, iABC) /* OP_SHR */ + ,opmode(0, 0, 0, 0, iABC) /* OP_MMBIN */ + ,opmode(0, 0, 0, 0, iABC) /* OP_MMBINI*/ + ,opmode(0, 0, 0, 0, iABC) /* OP_MMBINK*/ ,opmode(0, 0, 0, 1, iABC) /* OP_UNM */ ,opmode(0, 0, 0, 1, iABC) /* OP_BNOT */ ,opmode(0, 0, 0, 1, iABC) /* OP_NOT */ diff --git a/lopcodes.h b/lopcodes.h index 26b1850d5d..fd578c68a3 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -255,6 +255,10 @@ OP_BXOR,/* A B C R(A) := R(B) ~ R(C) */ OP_SHL,/* A B C R(A) := R(B) << R(C) */ OP_SHR,/* A B C R(A) := R(B) >> R(C) */ +OP_MMBIN,/* A B C call B metamethod for previous bin. operation */ +OP_MMBINI,/* A B C call B metamethod for previous binI. operation */ +OP_MMBINK,/* A B C call B metamethod for previous binK. operation */ + OP_UNM,/* A B R(A) := -R(B) */ OP_BNOT,/* A B R(A) := ~R(B) */ OP_NOT,/* A B R(A) := not R(B) */ diff --git a/lopnames.h b/lopnames.h index 28535fe226..0fc1da1f26 100644 --- a/lopnames.h +++ b/lopnames.h @@ -60,6 +60,9 @@ static const char *const opnames[] = { "BXOR", "SHL", "SHR", + "MMBIN", + "MMBINI", + "MMBINK", "UNM", "BNOT", "NOT", diff --git a/ltm.c b/ltm.c index 19233a8758..991e62c157 100644 --- a/ltm.c +++ b/ltm.c @@ -173,7 +173,7 @@ void luaT_tryconcatTM (lua_State *L) { void luaT_trybinassocTM (lua_State *L, const TValue *p1, const TValue *p2, - StkId res, int flip, TMS event) { + int flip, StkId res, TMS event) { if (flip) luaT_trybinTM(L, p2, p1, res, event); else @@ -185,7 +185,7 @@ void luaT_trybiniTM (lua_State *L, const TValue *p1, lua_Integer i2, int flip, StkId res, TMS event) { TValue aux; setivalue(&aux, i2); - luaT_trybinassocTM(L, p1, &aux, res, flip, event); + luaT_trybinassocTM(L, p1, &aux, flip, res, event); } diff --git a/ltm.h b/ltm.h index 51dfe79354..9621e68ec0 100644 --- a/ltm.h +++ b/ltm.h @@ -77,7 +77,7 @@ LUAI_FUNC void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2, StkId res, TMS event); LUAI_FUNC void luaT_tryconcatTM (lua_State *L); LUAI_FUNC void luaT_trybinassocTM (lua_State *L, const TValue *p1, - const TValue *p2, StkId res, int inv, TMS event); + const TValue *p2, int inv, StkId res, TMS event); LUAI_FUNC void luaT_trybiniTM (lua_State *L, const TValue *p1, lua_Integer i2, int inv, StkId res, TMS event); LUAI_FUNC int luaT_callorderTM (lua_State *L, const TValue *p1, diff --git a/lvm.c b/lvm.c index 907417e3aa..a9e8455e76 100644 --- a/lvm.c +++ b/lvm.c @@ -717,18 +717,11 @@ void luaV_finishOp (lua_State *L) { Instruction inst = *(ci->u.l.savedpc - 1); /* interrupted instruction */ OpCode op = GET_OPCODE(inst); switch (op) { /* finish its execution */ - case OP_ADDI: case OP_SUBI: - case OP_MULI: case OP_DIVI: case OP_IDIVI: - case OP_MODI: case OP_POWI: - case OP_ADDK: case OP_SUBK: - case OP_MULK: case OP_DIVK: case OP_IDIVK: - case OP_MODK: case OP_POWK: - case OP_ADD: case OP_SUB: - case OP_MUL: case OP_DIV: case OP_IDIV: - case OP_BANDK: case OP_BORK: case OP_BXORK: - case OP_BAND: case OP_BOR: case OP_BXOR: + case OP_MMBIN: case OP_MMBINI: case OP_MMBINK: { + setobjs2s(L, base + GETARG_A(*(ci->u.l.savedpc - 2)), --L->top); + break; + } case OP_SHLI: case OP_SHRI: case OP_SHL: case OP_SHR: - case OP_MOD: case OP_POW: case OP_UNM: case OP_BNOT: case OP_LEN: case OP_GETTABUP: case OP_GETTABLE: case OP_GETI: case OP_GETFIELD: case OP_SELF: { @@ -804,10 +797,8 @@ void luaV_finishOp (lua_State *L) { lua_Number nb; \ if (tonumberns(v1, nb)) { \ lua_Number fimm = cast_num(imm); \ - setfltvalue(s2v(ra), fop(L, nb, fimm)); \ - } \ - else \ - ProtectNT(luaT_trybiniTM(L, v1, imm, flip, ra, tm)); } + pc++; setfltvalue(s2v(ra), fop(L, nb, fimm)); \ + }} /* @@ -827,7 +818,7 @@ void luaV_finishOp (lua_State *L) { int imm = GETARG_sC(i); \ if (ttisinteger(v1)) { \ lua_Integer iv1 = ivalue(v1); \ - setivalue(s2v(ra), iop(L, iv1, imm)); \ + pc++; setivalue(s2v(ra), iop(L, iv1, imm)); \ } \ else op_arithfI_aux(L, v1, imm, fop, tm, flip); } @@ -839,10 +830,8 @@ void luaV_finishOp (lua_State *L) { #define op_arithf_aux(L,v1,v2,fop,tm) { \ lua_Number n1; lua_Number n2; \ if (tonumberns(v1, n1) && tonumberns(v2, n2)) { \ - setfltvalue(s2v(ra), fop(L, n1, n2)); \ - } \ - else \ - ProtectNT(luaT_trybinTM(L, v1, v2, ra, tm)); } + pc++; setfltvalue(s2v(ra), fop(L, n1, n2)); \ + }} /* @@ -862,7 +851,7 @@ void luaV_finishOp (lua_State *L) { TValue *v2 = vRC(i); \ if (ttisinteger(v1) && ttisinteger(v2)) { \ lua_Integer i1 = ivalue(v1); lua_Integer i2 = ivalue(v2); \ - setivalue(s2v(ra), iop(L, i1, i2)); \ + pc++; setivalue(s2v(ra), iop(L, i1, i2)); \ } \ else op_arithf_aux(L, v1, v2, fop, tm); } @@ -875,15 +864,13 @@ void luaV_finishOp (lua_State *L) { TValue *v2 = KC(i); \ if (ttisinteger(v1) && ttisinteger(v2)) { \ lua_Integer i1 = ivalue(v1); lua_Integer i2 = ivalue(v2); \ - setivalue(s2v(ra), iop(L, i1, i2)); \ + pc++; setivalue(s2v(ra), iop(L, i1, i2)); \ } \ else { \ lua_Number n1; lua_Number n2; \ if (tonumberns(v1, n1) && tonumberns(v2, n2)) { \ - setfltvalue(s2v(ra), fop(L, n1, n2)); \ - } \ - else \ - ProtectNT(luaT_trybinassocTM(L, v1, v2, ra, flip, tm)); } } + pc++; setfltvalue(s2v(ra), fop(L, n1, n2)); \ + }}} /* @@ -894,10 +881,8 @@ void luaV_finishOp (lua_State *L) { TValue *v2 = KC(i); \ lua_Number n1; lua_Number n2; \ if (tonumberns(v1, n1) && tonumberns(v2, n2)) { \ - setfltvalue(s2v(ra), fop(L, n1, n2)); \ - } \ - else \ - ProtectNT(luaT_trybinTM(L, v1, v2, ra, tm)); } + pc++; setfltvalue(s2v(ra), fop(L, n1, n2)); \ + }} /* @@ -909,10 +894,8 @@ void luaV_finishOp (lua_State *L) { lua_Integer i1; \ lua_Integer i2 = ivalue(v2); \ if (tointegerns(v1, &i1)) { \ - setivalue(s2v(ra), op(L, i1, i2)); \ - } \ - else \ - ProtectNT(luaT_trybiniTM(L, v1, i2, TESTARG_k(i), ra, tm)); } + pc++; setivalue(s2v(ra), op(L, i1, i2)); \ + }} /* @@ -923,10 +906,8 @@ void luaV_finishOp (lua_State *L) { TValue *v2 = vRC(i); \ lua_Integer i1; lua_Integer i2; \ if (tointegerns(v1, &i1) && tointegerns(v2, &i2)) { \ - setivalue(s2v(ra), op(L, i1, i2)); \ - } \ - else \ - ProtectNT(luaT_trybinTM(L, v1, v2, ra, tm)); } + pc++; setivalue(s2v(ra), op(L, i1, i2)); \ + }} /* @@ -1443,6 +1424,33 @@ void luaV_execute (lua_State *L, CallInfo *ci) { ProtectNT(luaT_trybinTM(L, rb, rc, ra, TM_SHL)); vmbreak; } + vmcase(OP_MMBIN) { + Instruction pi = *(pc - 2); /* original arith. expression */ + TValue *rb = vRB(i); + TMS tm = (TMS)GETARG_C(i); + StkId result = RA(pi); + lua_assert(OP_ADD <= GET_OPCODE(pi) && GET_OPCODE(pi) <= OP_SHR); + ProtectNT(luaT_trybinTM(L, s2v(ra), rb, result, tm)); + vmbreak; + } + vmcase(OP_MMBINI) { + Instruction pi = *(pc - 2); /* original arith. expression */ + int imm = GETARG_sB(i); + TMS tm = (TMS)GETARG_C(i); + int flip = GETARG_k(i); + StkId result = RA(pi); + ProtectNT(luaT_trybiniTM(L, s2v(ra), imm, flip, result, tm)); + vmbreak; + } + vmcase(OP_MMBINK) { + Instruction pi = *(pc - 2); /* original arith. expression */ + TValue *imm = KB(i); + TMS tm = (TMS)GETARG_C(i); + int flip = GETARG_k(i); + StkId result = RA(pi); + ProtectNT(luaT_trybinassocTM(L, s2v(ra), imm, flip, result, tm)); + vmbreak; + } vmcase(OP_UNM) { TValue *rb = vRB(i); lua_Number nb; @@ -1826,4 +1834,3 @@ void luaV_execute (lua_State *L, CallInfo *ci) { } /* }================================================================== */ - diff --git a/testes/code.lua b/testes/code.lua index 3b768f334a..fcb5c30969 100644 --- a/testes/code.lua +++ b/testes/code.lua @@ -156,9 +156,9 @@ check(function () c.x, a[b] = -((a + d/b - a[b]) ^ a.x), b end, 'LOADNIL', - 'MUL', - 'DIV', 'ADD', 'GETTABLE', 'SUB', 'GETFIELD', 'POW', - 'UNM', 'SETTABLE', 'SETFIELD', 'RETURN0') + 'MUL', 'MMBIN', + 'DIV', 'MMBIN', 'ADD', 'MMBIN', 'GETTABLE', 'SUB', 'MMBIN', + 'GETFIELD', 'POW', 'MMBIN', 'UNM', 'SETTABLE', 'SETFIELD', 'RETURN0') -- direct access to constants @@ -188,7 +188,7 @@ check(function () b = a/a b = 5-4 end, - 'LOADNIL', 'SUB', 'DIV', 'LOADI', 'RETURN0') + 'LOADNIL', 'SUB', 'MMBIN', 'DIV', 'MMBIN', 'LOADI', 'RETURN0') check(function () local a,b @@ -292,38 +292,45 @@ checkK(function () return -(border + 1) end, -(sbx + 1.0)) -- immediate operands -checkR(function (x) return x + k1 end, 10, 11, 'ADDI', 'RETURN1') -checkR(function (x) return 128 + x end, 0.0, 128.0, 'ADDI', 'RETURN1') -checkR(function (x) return x * -127 end, -1.0, 127.0, 'MULI', 'RETURN1') -checkR(function (x) return 20 * x end, 2, 40, 'MULI', 'RETURN1') -checkR(function (x) return x ^ -2 end, 2, 0.25, 'POWI', 'RETURN1') -checkR(function (x) return x / 40 end, 40, 1.0, 'DIVI', 'RETURN1') -checkR(function (x) return x // 1 end, 10.0, 10.0, 'IDIVI', 'RETURN1') -checkR(function (x) return x % (100 - 10) end, 91, 1, 'MODI', 'RETURN1') +checkR(function (x) return x + k1 end, 10, 11, 'ADDI', 'MMBINI', 'RETURN1') +checkR(function (x) return 128 + x end, 0.0, 128.0, + 'ADDI', 'MMBINI', 'RETURN1') +checkR(function (x) return x * -127 end, -1.0, 127.0, + 'MULI', 'MMBINI', 'RETURN1') +checkR(function (x) return 20 * x end, 2, 40, 'MULI', 'MMBINI', 'RETURN1') +checkR(function (x) return x ^ -2 end, 2, 0.25, 'POWI', 'MMBINI', 'RETURN1') +checkR(function (x) return x / 40 end, 40, 1.0, 'DIVI', 'MMBINI', 'RETURN1') +checkR(function (x) return x // 1 end, 10.0, 10.0, + 'IDIVI', 'MMBINI', 'RETURN1') +checkR(function (x) return x % (100 - 10) end, 91, 1, + 'MODI', 'MMBINI', 'RETURN1') checkR(function (x) return k1 << x end, 3, 8, 'SHLI', 'RETURN1') checkR(function (x) return x << 2 end, 10, 40, 'SHRI', 'RETURN1') checkR(function (x) return x >> 2 end, 8, 2, 'SHRI', 'RETURN1') -checkR(function (x) return x & 1 end, 9, 1, 'BANDK', 'RETURN1') -checkR(function (x) return 10 | x end, 1, 11, 'BORK', 'RETURN1') -checkR(function (x) return -10 ~ x end, -1, 9, 'BXORK', 'RETURN1') +checkR(function (x) return x & 1 end, 9, 1, 'BANDK', 'MMBINK', 'RETURN1') +checkR(function (x) return 10 | x end, 1, 11, 'BORK', 'MMBINK', 'RETURN1') +checkR(function (x) return -10 ~ x end, -1, 9, 'BXORK', 'MMBINK', 'RETURN1') -- K operands in arithmetic operations -checkR(function (x) return x + 0.0 end, 1, 1.0, 'ADDK', 'RETURN1') --- check(function (x) return 128 + x end, 'ADDK', 'RETURN1') -checkR(function (x) return x * -10000 end, 2, -20000, 'MULK', 'RETURN1') --- check(function (x) return 20 * x end, 'MULK', 'RETURN1') -checkR(function (x) return x ^ 0.5 end, 4, 2.0, 'POWK', 'RETURN1') -checkR(function (x) return x / 2.0 end, 4, 2.0, 'DIVK', 'RETURN1') -checkR(function (x) return x // 10000 end, 10000, 1, 'IDIVK', 'RETURN1') -checkR(function (x) return x % (100.0 - 10) end, 91, 1.0, 'MODK', 'RETURN1') +checkR(function (x) return x + 0.0 end, 1, 1.0, 'ADDK', 'MMBINK', 'RETURN1') +-- check(function (x) return 128 + x end, 'ADDK', 'MMBINK', 'RETURN1') +checkR(function (x) return x * -10000 end, 2, -20000, + 'MULK', 'MMBINK', 'RETURN1') +-- check(function (x) return 20 * x end, 'MULK', 'MMBINK', 'RETURN1') +checkR(function (x) return x ^ 0.5 end, 4, 2.0, 'POWK', 'MMBINK', 'RETURN1') +checkR(function (x) return x / 2.0 end, 4, 2.0, 'DIVK', 'MMBINK', 'RETURN1') +checkR(function (x) return x // 10000 end, 10000, 1, + 'IDIVK', 'MMBINK', 'RETURN1') +checkR(function (x) return x % (100.0 - 10) end, 91, 1.0, + 'MODK', 'MMBINK', 'RETURN1') -- no foldings (and immediate operands) check(function () return -0.0 end, 'LOADF', 'UNM', 'RETURN1') -check(function () return k3/0 end, 'LOADI', 'DIVI', 'RETURN1') -check(function () return 0%0 end, 'LOADI', 'MODI', 'RETURN1') -check(function () return -4//0 end, 'LOADI', 'IDIVI', 'RETURN1') +check(function () return k3/0 end, 'LOADI', 'DIVI', 'MMBINI', 'RETURN1') +check(function () return 0%0 end, 'LOADI', 'MODI', 'MMBINI', 'RETURN1') +check(function () return -4//0 end, 'LOADI', 'IDIVI', 'MMBINI', 'RETURN1') check(function (x) return x >> 2.0 end, 'LOADF', 'SHR', 'RETURN1') -check(function (x) return x & 2.0 end, 'LOADF', 'BAND', 'RETURN1') +check(function (x) return x & 2.0 end, 'LOADF', 'BAND', 'MMBIN', 'RETURN1') -- basic 'for' loops check(function () for i = -10, 10.5 do end end, @@ -379,7 +386,7 @@ check(function (a, b) if b then break else a = a + 1 end end end, -'TEST', 'JMP', 'TEST', 'JMP', 'ADDI', 'JMP', 'RETURN0') +'TEST', 'JMP', 'TEST', 'JMP', 'ADDI', 'MMBINI', 'JMP', 'RETURN0') checkequal( function (a) while a < 10 do a = a + 1 end end, diff --git a/testes/coroutine.lua b/testes/coroutine.lua index 48f4c5bfd3..81d848a387 100644 --- a/testes/coroutine.lua +++ b/testes/coroutine.lua @@ -751,7 +751,7 @@ assert(run(function () return a >> 2 end, {"shr"}) == 10 >> 2) assert(run(function () return 1 >> a end, {"shr"}) == 1 >> 10) assert(run(function () return a << 2 end, {"shl"}) == 10 << 2) assert(run(function () return 1 << a end, {"shl"}) == 1 << 10) -assert(run(function () return a ~ 2 end, {"bxor"}) == 10 ~ 2) +assert(run(function () return 2 ~ a end, {"bxor"}) == 2 ~ 10) assert(run(function () return a..b end, {"concat"}) == "1012") From 46b84580d6d7890f4ba813f312e52514fffc38a7 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 28 Aug 2019 09:58:03 -0300 Subject: [PATCH 0492/1145] Use of 'MMBIN' opcodes extended to shift operators Plus, this commit removes useless 'tm' parameters in 'op_*' macros. --- lcode.c | 26 ++++++------ ldebug.c | 8 ---- lvm.c | 102 +++++++++++++++++++----------------------------- testes/code.lua | 8 ++-- testes/db.lua | 3 +- 5 files changed, 60 insertions(+), 87 deletions(-) diff --git a/lcode.c b/lcode.c index a1b27a00b9..2c08409b77 100644 --- a/lcode.c +++ b/lcode.c @@ -1339,19 +1339,23 @@ static void codeunexpval (FuncState *fs, OpCode op, expdesc *e, int line) { ** Expression to produce final result will be encoded in 'e1'. */ static void finishbinexpval (FuncState *fs, expdesc *e1, expdesc *e2, - OpCode op, int v2, int k, int line, + OpCode op, int v2, int flip, int line, OpCode mmop, TMS event) { int v1 = luaK_exp2anyreg(fs, e1); - int pc = luaK_codeABCk(fs, op, 0, v1, v2, k); + int pc = luaK_codeABCk(fs, op, 0, v1, v2, flip); freeexps(fs, e1, e2); e1->u.info = pc; e1->k = VRELOC; /* all those operations are relocatable */ luaK_fixline(fs, line); -if (event != TM_SHL && event != TM_SHR) { - luaK_codeABCk(fs, mmop, v1, v2, event, k); /* to call metamethod */ + if (op == OP_SHRI && flip) { + /* For the metamethod, undo the "changedir" did by 'codeshift' */ + event = TM_SHL; + v2 = -(v2 - OFFSET_sC) + OFFSET_sC; + flip = 0; + } + luaK_codeABCk(fs, mmop, v1, v2, event, flip); /* to call metamethod */ luaK_fixline(fs, line); } -} /* @@ -1371,10 +1375,10 @@ static void codebinexpval (FuncState *fs, OpCode op, ** Code binary operators ('+', '-', ...) with immediate operands. */ static void codebini (FuncState *fs, OpCode op, - expdesc *e1, expdesc *e2, int k, int line, + expdesc *e1, expdesc *e2, int flip, int line, TMS event) { int v2 = cast_int(e2->u.ival) + OFFSET_sC; /* immediate operand */ - finishbinexpval(fs, e1, e2, op, v2, k, line, OP_MMBINI, event); + finishbinexpval(fs, e1, e2, op, v2, flip, line, OP_MMBINI, event); } @@ -1429,12 +1433,12 @@ static void codecommutative (FuncState *fs, BinOpr op, */ static void codebitwise (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2, int line) { - int inv = 0; + int flip = 0; int v2; OpCode op; if (e1->k == VKINT && luaK_exp2RK(fs, e1)) { swapexps(e1, e2); /* 'e2' will be the constant operand */ - inv = 1; + flip = 1; } else if (!(e2->k == VKINT && luaK_exp2RK(fs, e2))) { /* no constants? */ op = cast(OpCode, opr + OP_ADD); @@ -1444,7 +1448,7 @@ static void codebitwise (FuncState *fs, BinOpr opr, v2 = e2->u.info; /* index in K array */ op = cast(OpCode, opr + OP_ADDK); lua_assert(ttisinteger(&fs->f->k[v2])); - finishbinexpval(fs, e1, e2, op, v2, inv, line, OP_MMBINK, + finishbinexpval(fs, e1, e2, op, v2, flip, line, OP_MMBINK, cast(TMS, opr + TM_ADD)); } @@ -1461,7 +1465,7 @@ static void codeshift (FuncState *fs, OpCode op, changedir = 1; e2->u.ival = -(e2->u.ival); } - codebini(fs, OP_SHRI, e1, e2, changedir, line, TM_SHL); + codebini(fs, OP_SHRI, e1, e2, changedir, line, TM_SHR); } else codebinexpval(fs, op, e1, e2, line); diff --git a/ldebug.c b/ldebug.c index 4e1dc6b9f7..aef52e1595 100644 --- a/ldebug.c +++ b/ldebug.c @@ -628,11 +628,6 @@ static const char *funcnamefromcode (lua_State *L, CallInfo *ci, tm = cast(TMS, GETARG_C(i)); break; } - case OP_SHL: case OP_SHR: { - int offset = GET_OPCODE(i) - OP_ADD; /* ORDER OP */ - tm = cast(TMS, offset + TM_ADD); /* ORDER TM */ - break; - } case OP_UNM: tm = TM_UNM; break; case OP_BNOT: tm = TM_BNOT; break; case OP_LEN: tm = TM_LEN; break; @@ -641,9 +636,6 @@ static const char *funcnamefromcode (lua_State *L, CallInfo *ci, case OP_LT: case OP_LE: case OP_LTI: case OP_LEI: *name = "order"; /* '<=' can call '__lt', etc. */ return "metamethod"; - case OP_SHRI: case OP_SHLI: - *name = "shift"; - return "metamethod"; case OP_CLOSE: case OP_RETURN: *name = "close"; return "metamethod"; diff --git a/lvm.c b/lvm.c index a9e8455e76..71f6ae0d81 100644 --- a/lvm.c +++ b/lvm.c @@ -674,6 +674,8 @@ lua_Number luaV_modf (lua_State *L, lua_Number m, lua_Number n) { /* ** Shift left operation. (Shift right just negates 'y'.) */ +#define luaV_shiftr(x,y) luaV_shiftl(x,-(y)) + lua_Integer luaV_shiftl (lua_Integer x, lua_Integer y) { if (y < 0) { /* shift right? */ if (y <= -NBITS) return 0; @@ -721,7 +723,6 @@ void luaV_finishOp (lua_State *L) { setobjs2s(L, base + GETARG_A(*(ci->u.l.savedpc - 2)), --L->top); break; } - case OP_SHLI: case OP_SHRI: case OP_SHL: case OP_SHR: case OP_UNM: case OP_BNOT: case OP_LEN: case OP_GETTABUP: case OP_GETTABLE: case OP_GETI: case OP_GETFIELD: case OP_SELF: { @@ -778,9 +779,9 @@ void luaV_finishOp (lua_State *L) { #define l_addi(L,a,b) intop(+, a, b) #define l_subi(L,a,b) intop(-, a, b) #define l_muli(L,a,b) intop(*, a, b) -#define l_band(L,a,b) intop(&, a, b) -#define l_bor(L,a,b) intop(|, a, b) -#define l_bxor(L,a,b) intop(^, a, b) +#define l_band(a,b) intop(&, a, b) +#define l_bor(a,b) intop(|, a, b) +#define l_bxor(a,b) intop(^, a, b) #define l_lti(a,b) (a < b) #define l_lei(a,b) (a <= b) @@ -827,7 +828,7 @@ void luaV_finishOp (lua_State *L) { ** Auxiliary function for arithmetic operations over floats and others ** with two register operands. */ -#define op_arithf_aux(L,v1,v2,fop,tm) { \ +#define op_arithf_aux(L,v1,v2,fop) { \ lua_Number n1; lua_Number n2; \ if (tonumberns(v1, n1) && tonumberns(v2, n2)) { \ pc++; setfltvalue(s2v(ra), fop(L, n1, n2)); \ @@ -837,29 +838,29 @@ void luaV_finishOp (lua_State *L) { /* ** Arithmetic operations over floats and others with register operands. */ -#define op_arithf(L,fop,tm) { \ +#define op_arithf(L,fop) { \ TValue *v1 = vRB(i); \ TValue *v2 = vRC(i); \ - op_arithf_aux(L, v1, v2, fop, tm); } + op_arithf_aux(L, v1, v2, fop); } /* ** Arithmetic operations with register operands. */ -#define op_arith(L,iop,fop,tm) { \ +#define op_arith(L,iop,fop) { \ TValue *v1 = vRB(i); \ TValue *v2 = vRC(i); \ if (ttisinteger(v1) && ttisinteger(v2)) { \ lua_Integer i1 = ivalue(v1); lua_Integer i2 = ivalue(v2); \ pc++; setivalue(s2v(ra), iop(L, i1, i2)); \ } \ - else op_arithf_aux(L, v1, v2, fop, tm); } + else op_arithf_aux(L, v1, v2, fop); } /* ** Arithmetic operations with K operands. */ -#define op_arithK(L,iop,fop,tm,flip) { \ +#define op_arithK(L,iop,fop,flip) { \ TValue *v1 = vRB(i); \ TValue *v2 = KC(i); \ if (ttisinteger(v1) && ttisinteger(v2)) { \ @@ -876,7 +877,7 @@ void luaV_finishOp (lua_State *L) { /* ** Arithmetic operations with K operands for floats. */ -#define op_arithfK(L,fop,tm) { \ +#define op_arithfK(L,fop) { \ TValue *v1 = vRB(i); \ TValue *v2 = KC(i); \ lua_Number n1; lua_Number n2; \ @@ -888,25 +889,25 @@ void luaV_finishOp (lua_State *L) { /* ** Bitwise operations with constant operand. */ -#define op_bitwiseK(L,op,tm) { \ +#define op_bitwiseK(L,op) { \ TValue *v1 = vRB(i); \ TValue *v2 = KC(i); \ lua_Integer i1; \ lua_Integer i2 = ivalue(v2); \ if (tointegerns(v1, &i1)) { \ - pc++; setivalue(s2v(ra), op(L, i1, i2)); \ + pc++; setivalue(s2v(ra), op(i1, i2)); \ }} /* ** Bitwise operations with register operands. */ -#define op_bitwise(L,op,tm) { \ +#define op_bitwise(L,op) { \ TValue *v1 = vRB(i); \ TValue *v2 = vRC(i); \ lua_Integer i1; lua_Integer i2; \ if (tointegerns(v1, &i1) && tointegerns(v2, &i2)) { \ - pc++; setivalue(s2v(ra), op(L, i1, i2)); \ + pc++; setivalue(s2v(ra), op(i1, i2)); \ }} @@ -1296,43 +1297,43 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_ADDK) { - op_arithK(L, l_addi, luai_numadd, TM_ADD, GETARG_k(i)); + op_arithK(L, l_addi, luai_numadd, GETARG_k(i)); vmbreak; } vmcase(OP_SUBK) { - op_arithK(L, l_subi, luai_numsub, TM_SUB, 0); + op_arithK(L, l_subi, luai_numsub, 0); vmbreak; } vmcase(OP_MULK) { - op_arithK(L, l_muli, luai_nummul, TM_MUL, GETARG_k(i)); + op_arithK(L, l_muli, luai_nummul, GETARG_k(i)); vmbreak; } vmcase(OP_MODK) { - op_arithK(L, luaV_mod, luaV_modf, TM_MOD, 0); + op_arithK(L, luaV_mod, luaV_modf, 0); vmbreak; } vmcase(OP_POWK) { - op_arithfK(L, luai_numpow, TM_POW); + op_arithfK(L, luai_numpow); vmbreak; } vmcase(OP_DIVK) { - op_arithfK(L, luai_numdiv, TM_DIV); + op_arithfK(L, luai_numdiv); vmbreak; } vmcase(OP_IDIVK) { - op_arithK(L, luaV_idiv, luai_numidiv, TM_IDIV, 0); + op_arithK(L, luaV_idiv, luai_numidiv, 0); vmbreak; } vmcase(OP_BANDK) { - op_bitwiseK(L, l_band, TM_BAND); + op_bitwiseK(L, l_band); vmbreak; } vmcase(OP_BORK) { - op_bitwiseK(L, l_bor, TM_BOR); + op_bitwiseK(L, l_bor); vmbreak; } vmcase(OP_BXORK) { - op_bitwiseK(L, l_bxor, TM_BXOR); + op_bitwiseK(L, l_bxor); vmbreak; } vmcase(OP_SHRI) { @@ -1340,14 +1341,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { int ic = GETARG_sC(i); lua_Integer ib; if (tointegerns(rb, &ib)) { - setivalue(s2v(ra), luaV_shiftl(ib, -ic)); - } - else { - TMS ev = TM_SHR; - if (TESTARG_k(i)) { - ic = -ic; ev = TM_SHL; - } - ProtectNT(luaT_trybiniTM(L, rb, ic, 0, ra, ev)); + pc++; setivalue(s2v(ra), luaV_shiftl(ib, -ic)); } vmbreak; } @@ -1356,72 +1350,56 @@ void luaV_execute (lua_State *L, CallInfo *ci) { int ic = GETARG_sC(i); lua_Integer ib; if (tointegerns(rb, &ib)) { - setivalue(s2v(ra), luaV_shiftl(ic, ib)); + pc++; setivalue(s2v(ra), luaV_shiftl(ic, ib)); } - else - ProtectNT(luaT_trybiniTM(L, rb, ic, 1, ra, TM_SHL)); vmbreak; } vmcase(OP_ADD) { - op_arith(L, l_addi, luai_numadd, TM_ADD); + op_arith(L, l_addi, luai_numadd); vmbreak; } vmcase(OP_SUB) { - op_arith(L, l_subi, luai_numsub, TM_SUB); + op_arith(L, l_subi, luai_numsub); vmbreak; } vmcase(OP_MUL) { - op_arith(L, l_muli, luai_nummul, TM_MUL); + op_arith(L, l_muli, luai_nummul); vmbreak; } vmcase(OP_MOD) { - op_arith(L, luaV_mod, luaV_modf, TM_MOD); + op_arith(L, luaV_mod, luaV_modf); vmbreak; } vmcase(OP_POW) { - op_arithf(L, luai_numpow, TM_POW); + op_arithf(L, luai_numpow); vmbreak; } vmcase(OP_DIV) { /* float division (always with floats) */ - op_arithf(L, luai_numdiv, TM_DIV); + op_arithf(L, luai_numdiv); vmbreak; } vmcase(OP_IDIV) { /* floor division */ - op_arith(L, luaV_idiv, luai_numidiv, TM_IDIV); + op_arith(L, luaV_idiv, luai_numidiv); vmbreak; } vmcase(OP_BAND) { - op_bitwise(L, l_band, TM_BAND); + op_bitwise(L, l_band); vmbreak; } vmcase(OP_BOR) { - op_bitwise(L, l_bor, TM_BOR); + op_bitwise(L, l_bor); vmbreak; } vmcase(OP_BXOR) { - op_bitwise(L, l_bxor, TM_BXOR); + op_bitwise(L, l_bxor); vmbreak; } vmcase(OP_SHR) { - TValue *rb = vRB(i); - TValue *rc = vRC(i); - lua_Integer ib; lua_Integer ic; - if (tointegerns(rb, &ib) && tointegerns(rc, &ic)) { - setivalue(s2v(ra), luaV_shiftl(ib, -ic)); - } - else - ProtectNT(luaT_trybinTM(L, rb, rc, ra, TM_SHR)); + op_bitwise(L, luaV_shiftr); vmbreak; } vmcase(OP_SHL) { - TValue *rb = vRB(i); - TValue *rc = vRC(i); - lua_Integer ib; lua_Integer ic; - if (tointegerns(rb, &ib) && tointegerns(rc, &ic)) { - setivalue(s2v(ra), luaV_shiftl(ib, ic)); - } - else - ProtectNT(luaT_trybinTM(L, rb, rc, ra, TM_SHL)); + op_bitwise(L, luaV_shiftl); vmbreak; } vmcase(OP_MMBIN) { diff --git a/testes/code.lua b/testes/code.lua index fcb5c30969..96560166e0 100644 --- a/testes/code.lua +++ b/testes/code.lua @@ -304,9 +304,9 @@ checkR(function (x) return x // 1 end, 10.0, 10.0, 'IDIVI', 'MMBINI', 'RETURN1') checkR(function (x) return x % (100 - 10) end, 91, 1, 'MODI', 'MMBINI', 'RETURN1') -checkR(function (x) return k1 << x end, 3, 8, 'SHLI', 'RETURN1') -checkR(function (x) return x << 2 end, 10, 40, 'SHRI', 'RETURN1') -checkR(function (x) return x >> 2 end, 8, 2, 'SHRI', 'RETURN1') +checkR(function (x) return k1 << x end, 3, 8, 'SHLI', 'MMBINI', 'RETURN1') +checkR(function (x) return x << 2 end, 10, 40, 'SHRI', 'MMBINI', 'RETURN1') +checkR(function (x) return x >> 2 end, 8, 2, 'SHRI', 'MMBINI', 'RETURN1') checkR(function (x) return x & 1 end, 9, 1, 'BANDK', 'MMBINK', 'RETURN1') checkR(function (x) return 10 | x end, 1, 11, 'BORK', 'MMBINK', 'RETURN1') checkR(function (x) return -10 ~ x end, -1, 9, 'BXORK', 'MMBINK', 'RETURN1') @@ -329,7 +329,7 @@ check(function () return -0.0 end, 'LOADF', 'UNM', 'RETURN1') check(function () return k3/0 end, 'LOADI', 'DIVI', 'MMBINI', 'RETURN1') check(function () return 0%0 end, 'LOADI', 'MODI', 'MMBINI', 'RETURN1') check(function () return -4//0 end, 'LOADI', 'IDIVI', 'MMBINI', 'RETURN1') -check(function (x) return x >> 2.0 end, 'LOADF', 'SHR', 'RETURN1') +check(function (x) return x >> 2.0 end, 'LOADF', 'SHR', 'MMBIN', 'RETURN1') check(function (x) return x & 2.0 end, 'LOADF', 'BAND', 'MMBIN', 'RETURN1') -- basic 'for' loops diff --git a/testes/db.lua b/testes/db.lua index c43243a6a3..074a6d0be5 100644 --- a/testes/db.lua +++ b/testes/db.lua @@ -806,8 +806,7 @@ assert(a+3 == "add" and 3-a == "sub" and a*3 == "mul" and -a == "unm" and #a == "len" and a&3 == "band") assert(a + 30000 == "add" and a - 3.0 == "sub" and a * 3.0 == "mul" and -a == "unm" and #a == "len" and a & 3 == "band") -assert(a|3 == "bor" and 3~a == "bxor" and a<<3 == "shift" and - a>>1 == "shift") +assert(a|3 == "bor" and 3~a == "bxor" and a<<3 == "shl" and a>>1 == "shr") assert (a==b and a.op == "eq") assert (a>=b and a.op == "order") assert (a>b and a.op == "order") From 72a094bda7d71050a91a88474d67d39aa2bc1c46 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 29 Aug 2019 12:52:37 -0300 Subject: [PATCH 0493/1145] Undo change in the handling of 'L->top' (commit b80077b8f3) With MMBIN instructions, there are fewer opcodes that need to update 'L->top', so that change does not seem to pay for the increased complexity. --- lapi.c | 2 -- lobject.c | 2 -- ltm.c | 2 -- lvm.c | 19 +++++++++---------- 4 files changed, 9 insertions(+), 16 deletions(-) diff --git a/lapi.c b/lapi.c index a9ffad80f7..0ea3dc0f22 100644 --- a/lapi.c +++ b/lapi.c @@ -329,14 +329,12 @@ LUA_API int lua_compare (lua_State *L, int index1, int index2, int op) { o1 = index2value(L, index1); o2 = index2value(L, index2); if (isvalid(L, o1) && isvalid(L, o2)) { - ptrdiff_t top = savestack(L, L->top); switch (op) { case LUA_OPEQ: i = luaV_equalobj(L, o1, o2); break; case LUA_OPLT: i = luaV_lessthan(L, o1, o2); break; case LUA_OPLE: i = luaV_lessequal(L, o1, o2); break; default: api_check(L, 0, "invalid option"); } - L->top = restorestack(L, top); } lua_unlock(L); return i; diff --git a/lobject.c b/lobject.c index b376ab1583..b4efae4f33 100644 --- a/lobject.c +++ b/lobject.c @@ -127,9 +127,7 @@ void luaO_arith (lua_State *L, int op, const TValue *p1, const TValue *p2, StkId res) { if (!luaO_rawarith(L, op, p1, p2, s2v(res))) { /* could not perform raw operation; try metamethod */ - ptrdiff_t top = savestack(L, L->top); luaT_trybinTM(L, p1, p2, res, cast(TMS, (op - LUA_OPADD) + TM_ADD)); - L->top = restorestack(L, top); } } diff --git a/ltm.c b/ltm.c index 991e62c157..1e32d86a5a 100644 --- a/ltm.c +++ b/ltm.c @@ -147,7 +147,6 @@ static int callbinTM (lua_State *L, const TValue *p1, const TValue *p2, void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2, StkId res, TMS event) { - L->top = L->ci->top; if (!callbinTM(L, p1, p2, res, event)) { switch (event) { case TM_BAND: case TM_BOR: case TM_BXOR: @@ -191,7 +190,6 @@ void luaT_trybiniTM (lua_State *L, const TValue *p1, lua_Integer i2, int luaT_callorderTM (lua_State *L, const TValue *p1, const TValue *p2, TMS event) { - L->top = L->ci->top; if (callbinTM(L, p1, p2, L->top, event)) /* try original event */ return !l_isfalse(s2v(L->top)); #if defined(LUA_COMPAT_LT_LE) diff --git a/lvm.c b/lvm.c index 71f6ae0d81..46150ef0ad 100644 --- a/lvm.c +++ b/lvm.c @@ -516,7 +516,6 @@ int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2) { if (tm == NULL) /* no TM? */ return 0; /* objects are different */ else { - L->top = L->ci->top; luaT_callTMres(L, tm, t1, t2, L->top); /* call TM */ return !l_isfalse(s2v(L->top)); } @@ -925,7 +924,7 @@ void luaV_finishOp (lua_State *L) { else if (ttisnumber(s2v(ra)) && ttisnumber(rb)) \ cond = opf(s2v(ra), rb); \ else \ - ProtectNT(cond = other(L, s2v(ra), rb)); \ + Protect(cond = other(L, s2v(ra), rb)); \ docondjump(); } @@ -944,7 +943,7 @@ void luaV_finishOp (lua_State *L) { } \ else { \ int isf = GETARG_C(i); \ - ProtectNT(cond = luaT_callorderiTM(L, s2v(ra), im, inv, isf, tm)); \ + Protect(cond = luaT_callorderiTM(L, s2v(ra), im, inv, isf, tm)); \ } \ docondjump(); } @@ -989,7 +988,7 @@ void luaV_finishOp (lua_State *L) { /* for test instructions, execute the jump instruction that follows it */ -#define donextjump(ci) { i = *pc; dojump(ci, i, 1); } +#define donextjump(ci) { Instruction ni = *pc; dojump(ci, ni, 1); } /* ** do a conditional jump: skip next instruction if 'cond' is not what @@ -1408,7 +1407,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { TMS tm = (TMS)GETARG_C(i); StkId result = RA(pi); lua_assert(OP_ADD <= GET_OPCODE(pi) && GET_OPCODE(pi) <= OP_SHR); - ProtectNT(luaT_trybinTM(L, s2v(ra), rb, result, tm)); + Protect(luaT_trybinTM(L, s2v(ra), rb, result, tm)); vmbreak; } vmcase(OP_MMBINI) { @@ -1417,7 +1416,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { TMS tm = (TMS)GETARG_C(i); int flip = GETARG_k(i); StkId result = RA(pi); - ProtectNT(luaT_trybiniTM(L, s2v(ra), imm, flip, result, tm)); + Protect(luaT_trybiniTM(L, s2v(ra), imm, flip, result, tm)); vmbreak; } vmcase(OP_MMBINK) { @@ -1426,7 +1425,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { TMS tm = (TMS)GETARG_C(i); int flip = GETARG_k(i); StkId result = RA(pi); - ProtectNT(luaT_trybinassocTM(L, s2v(ra), imm, flip, result, tm)); + Protect(luaT_trybinassocTM(L, s2v(ra), imm, flip, result, tm)); vmbreak; } vmcase(OP_UNM) { @@ -1440,7 +1439,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { setfltvalue(s2v(ra), luai_numunm(L, nb)); } else - ProtectNT(luaT_trybinTM(L, rb, rb, ra, TM_UNM)); + Protect(luaT_trybinTM(L, rb, rb, ra, TM_UNM)); vmbreak; } vmcase(OP_BNOT) { @@ -1450,7 +1449,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { setivalue(s2v(ra), intop(^, ~l_castS2U(0), ib)); } else - ProtectNT(luaT_trybinTM(L, rb, rb, ra, TM_BNOT)); + Protect(luaT_trybinTM(L, rb, rb, ra, TM_BNOT)); vmbreak; } vmcase(OP_NOT) { @@ -1486,7 +1485,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmcase(OP_EQ) { int cond; TValue *rb = vRB(i); - ProtectNT(cond = luaV_equalobj(L, s2v(ra), rb)); + Protect(cond = luaV_equalobj(L, s2v(ra), rb)); docondjump(); vmbreak; } From 4518e5df24600cacdb3bab75d640348d28e8b769 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 6 Sep 2019 14:14:11 -0300 Subject: [PATCH 0494/1145] Added macro 'testMMMode' Macro 'testMMMode' checks whether opcode is an MM opcode. --- ldebug.c | 8 +-- lopcodes.c | 176 ++++++++++++++++++++++++++--------------------------- lopcodes.h | 5 +- 3 files changed, 95 insertions(+), 94 deletions(-) diff --git a/ldebug.c b/ldebug.c index aef52e1595..6e16b0fb10 100644 --- a/ldebug.c +++ b/ldebug.c @@ -465,16 +465,14 @@ static int filterpc (int pc, int jmptarget) { /* -** try to find last instruction before 'lastpc' that modified register 'reg' +** Try to find last instruction before 'lastpc' that modified register 'reg'. */ static int findsetreg (const Proto *p, int lastpc, int reg) { int pc; int setreg = -1; /* keep last instruction that changed 'reg' */ int jmptarget = 0; /* any code before this address is conditional */ - if (GET_OPCODE(p->code[lastpc]) == OP_MMBIN || - GET_OPCODE(p->code[lastpc]) == OP_MMBINI || - GET_OPCODE(p->code[lastpc]) == OP_MMBINK) - lastpc--; + if (testMMMode(GET_OPCODE(p->code[lastpc]))) + lastpc--; /* previous instruction was not actually executed */ for (pc = 0; pc < lastpc; pc++) { Instruction i = p->code[pc]; OpCode op = GET_OPCODE(i); diff --git a/lopcodes.c b/lopcodes.c index aede93a552..619592cee8 100644 --- a/lopcodes.c +++ b/lopcodes.c @@ -18,93 +18,93 @@ /* ORDER OP */ LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = { -/* OT IT T A mode opcode */ - opmode(0, 0, 0, 1, iABC) /* OP_MOVE */ - ,opmode(0, 0, 0, 1, iAsBx) /* OP_LOADI */ - ,opmode(0, 0, 0, 1, iAsBx) /* OP_LOADF */ - ,opmode(0, 0, 0, 1, iABx) /* OP_LOADK */ - ,opmode(0, 0, 0, 1, iABx) /* OP_LOADKX */ - ,opmode(0, 0, 0, 1, iABC) /* OP_LOADBOOL */ - ,opmode(0, 0, 0, 1, iABC) /* OP_LOADNIL */ - ,opmode(0, 0, 0, 1, iABC) /* OP_GETUPVAL */ - ,opmode(0, 0, 0, 0, iABC) /* OP_SETUPVAL */ - ,opmode(0, 0, 0, 1, iABC) /* OP_GETTABUP */ - ,opmode(0, 0, 0, 1, iABC) /* OP_GETTABLE */ - ,opmode(0, 0, 0, 1, iABC) /* OP_GETI */ - ,opmode(0, 0, 0, 1, iABC) /* OP_GETFIELD */ - ,opmode(0, 0, 0, 0, iABC) /* OP_SETTABUP */ - ,opmode(0, 0, 0, 0, iABC) /* OP_SETTABLE */ - ,opmode(0, 0, 0, 0, iABC) /* OP_SETI */ - ,opmode(0, 0, 0, 0, iABC) /* OP_SETFIELD */ - ,opmode(0, 0, 0, 1, iABC) /* OP_NEWTABLE */ - ,opmode(0, 0, 0, 1, iABC) /* OP_SELF */ - ,opmode(0, 0, 0, 1, iABC) /* OP_ADDI */ - ,opmode(0, 0, 0, 1, iABC) /* OP_SUBI */ - ,opmode(0, 0, 0, 1, iABC) /* OP_MULI */ - ,opmode(0, 0, 0, 1, iABC) /* OP_MODI */ - ,opmode(0, 0, 0, 1, iABC) /* OP_POWI */ - ,opmode(0, 0, 0, 1, iABC) /* OP_DIVI */ - ,opmode(0, 0, 0, 1, iABC) /* OP_IDIVI */ - ,opmode(0, 0, 0, 1, iABC) /* OP_ADDK */ - ,opmode(0, 0, 0, 1, iABC) /* OP_SUBK */ - ,opmode(0, 0, 0, 1, iABC) /* OP_MULK */ - ,opmode(0, 0, 0, 1, iABC) /* OP_MODK */ - ,opmode(0, 0, 0, 1, iABC) /* OP_POWK */ - ,opmode(0, 0, 0, 1, iABC) /* OP_DIVK */ - ,opmode(0, 0, 0, 1, iABC) /* OP_IDIVK */ - ,opmode(0, 0, 0, 1, iABC) /* OP_BANDK */ - ,opmode(0, 0, 0, 1, iABC) /* OP_BORK */ - ,opmode(0, 0, 0, 1, iABC) /* OP_BXORK */ - ,opmode(0, 0, 0, 1, iABC) /* OP_SHRI */ - ,opmode(0, 0, 0, 1, iABC) /* OP_SHLI */ - ,opmode(0, 0, 0, 1, iABC) /* OP_ADD */ - ,opmode(0, 0, 0, 1, iABC) /* OP_SUB */ - ,opmode(0, 0, 0, 1, iABC) /* OP_MUL */ - ,opmode(0, 0, 0, 1, iABC) /* OP_MOD */ - ,opmode(0, 0, 0, 1, iABC) /* OP_POW */ - ,opmode(0, 0, 0, 1, iABC) /* OP_DIV */ - ,opmode(0, 0, 0, 1, iABC) /* OP_IDIV */ - ,opmode(0, 0, 0, 1, iABC) /* OP_BAND */ - ,opmode(0, 0, 0, 1, iABC) /* OP_BOR */ - ,opmode(0, 0, 0, 1, iABC) /* OP_BXOR */ - ,opmode(0, 0, 0, 1, iABC) /* OP_SHL */ - ,opmode(0, 0, 0, 1, iABC) /* OP_SHR */ - ,opmode(0, 0, 0, 0, iABC) /* OP_MMBIN */ - ,opmode(0, 0, 0, 0, iABC) /* OP_MMBINI*/ - ,opmode(0, 0, 0, 0, iABC) /* OP_MMBINK*/ - ,opmode(0, 0, 0, 1, iABC) /* OP_UNM */ - ,opmode(0, 0, 0, 1, iABC) /* OP_BNOT */ - ,opmode(0, 0, 0, 1, iABC) /* OP_NOT */ - ,opmode(0, 0, 0, 1, iABC) /* OP_LEN */ - ,opmode(0, 0, 0, 1, iABC) /* OP_CONCAT */ - ,opmode(0, 0, 0, 0, iABC) /* OP_CLOSE */ - ,opmode(0, 0, 0, 0, iABC) /* OP_TBC */ - ,opmode(0, 0, 0, 0, isJ) /* OP_JMP */ - ,opmode(0, 0, 1, 0, iABC) /* OP_EQ */ - ,opmode(0, 0, 1, 0, iABC) /* OP_LT */ - ,opmode(0, 0, 1, 0, iABC) /* OP_LE */ - ,opmode(0, 0, 1, 0, iABC) /* OP_EQK */ - ,opmode(0, 0, 1, 0, iABC) /* OP_EQI */ - ,opmode(0, 0, 1, 0, iABC) /* OP_LTI */ - ,opmode(0, 0, 1, 0, iABC) /* OP_LEI */ - ,opmode(0, 0, 1, 0, iABC) /* OP_GTI */ - ,opmode(0, 0, 1, 0, iABC) /* OP_GEI */ - ,opmode(0, 0, 1, 0, iABC) /* OP_TEST */ - ,opmode(0, 0, 1, 1, iABC) /* OP_TESTSET */ - ,opmode(1, 1, 0, 1, iABC) /* OP_CALL */ - ,opmode(1, 1, 0, 1, iABC) /* OP_TAILCALL */ - ,opmode(0, 1, 0, 0, iABC) /* OP_RETURN */ - ,opmode(0, 0, 0, 0, iABC) /* OP_RETURN0 */ - ,opmode(0, 0, 0, 0, iABC) /* OP_RETURN1 */ - ,opmode(0, 0, 0, 1, iABx) /* OP_FORLOOP */ - ,opmode(0, 0, 0, 1, iABx) /* OP_FORPREP */ - ,opmode(0, 0, 0, 0, iABx) /* OP_TFORPREP */ - ,opmode(0, 0, 0, 0, iABC) /* OP_TFORCALL */ - ,opmode(0, 0, 0, 1, iABx) /* OP_TFORLOOP */ - ,opmode(0, 1, 0, 0, iABC) /* OP_SETLIST */ - ,opmode(0, 0, 0, 1, iABx) /* OP_CLOSURE */ - ,opmode(1, 0, 0, 1, iABC) /* OP_VARARG */ - ,opmode(0, 1, 0, 1, iABC) /* OP_VARARGPREP */ - ,opmode(0, 0, 0, 0, iAx) /* OP_EXTRAARG */ +/* MM OT IT T A mode opcode */ + opmode(0, 0, 0, 0, 1, iABC) /* OP_MOVE */ + ,opmode(0, 0, 0, 0, 1, iAsBx) /* OP_LOADI */ + ,opmode(0, 0, 0, 0, 1, iAsBx) /* OP_LOADF */ + ,opmode(0, 0, 0, 0, 1, iABx) /* OP_LOADK */ + ,opmode(0, 0, 0, 0, 1, iABx) /* OP_LOADKX */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_LOADBOOL */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_LOADNIL */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_GETUPVAL */ + ,opmode(0, 0, 0, 0, 0, iABC) /* OP_SETUPVAL */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_GETTABUP */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_GETTABLE */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_GETI */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_GETFIELD */ + ,opmode(0, 0, 0, 0, 0, iABC) /* OP_SETTABUP */ + ,opmode(0, 0, 0, 0, 0, iABC) /* OP_SETTABLE */ + ,opmode(0, 0, 0, 0, 0, iABC) /* OP_SETI */ + ,opmode(0, 0, 0, 0, 0, iABC) /* OP_SETFIELD */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_NEWTABLE */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_SELF */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_ADDI */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_SUBI */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_MULI */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_MODI */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_POWI */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_DIVI */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_IDIVI */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_ADDK */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_SUBK */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_MULK */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_MODK */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_POWK */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_DIVK */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_IDIVK */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_BANDK */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_BORK */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_BXORK */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_SHRI */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_SHLI */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_ADD */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_SUB */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_MUL */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_MOD */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_POW */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_DIV */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_IDIV */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_BAND */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_BOR */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_BXOR */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_SHL */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_SHR */ + ,opmode(1, 0, 0, 0, 0, iABC) /* OP_MMBIN */ + ,opmode(1, 0, 0, 0, 0, iABC) /* OP_MMBINI*/ + ,opmode(1, 0, 0, 0, 0, iABC) /* OP_MMBINK*/ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_UNM */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_BNOT */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_NOT */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_LEN */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_CONCAT */ + ,opmode(0, 0, 0, 0, 0, iABC) /* OP_CLOSE */ + ,opmode(0, 0, 0, 0, 0, iABC) /* OP_TBC */ + ,opmode(0, 0, 0, 0, 0, isJ) /* OP_JMP */ + ,opmode(0, 0, 0, 1, 0, iABC) /* OP_EQ */ + ,opmode(0, 0, 0, 1, 0, iABC) /* OP_LT */ + ,opmode(0, 0, 0, 1, 0, iABC) /* OP_LE */ + ,opmode(0, 0, 0, 1, 0, iABC) /* OP_EQK */ + ,opmode(0, 0, 0, 1, 0, iABC) /* OP_EQI */ + ,opmode(0, 0, 0, 1, 0, iABC) /* OP_LTI */ + ,opmode(0, 0, 0, 1, 0, iABC) /* OP_LEI */ + ,opmode(0, 0, 0, 1, 0, iABC) /* OP_GTI */ + ,opmode(0, 0, 0, 1, 0, iABC) /* OP_GEI */ + ,opmode(0, 0, 0, 1, 0, iABC) /* OP_TEST */ + ,opmode(0, 0, 0, 1, 1, iABC) /* OP_TESTSET */ + ,opmode(0, 1, 1, 0, 1, iABC) /* OP_CALL */ + ,opmode(0, 1, 1, 0, 1, iABC) /* OP_TAILCALL */ + ,opmode(0, 0, 1, 0, 0, iABC) /* OP_RETURN */ + ,opmode(0, 0, 0, 0, 0, iABC) /* OP_RETURN0 */ + ,opmode(0, 0, 0, 0, 0, iABC) /* OP_RETURN1 */ + ,opmode(0, 0, 0, 0, 1, iABx) /* OP_FORLOOP */ + ,opmode(0, 0, 0, 0, 1, iABx) /* OP_FORPREP */ + ,opmode(0, 0, 0, 0, 0, iABx) /* OP_TFORPREP */ + ,opmode(0, 0, 0, 0, 0, iABC) /* OP_TFORCALL */ + ,opmode(0, 0, 0, 0, 1, iABx) /* OP_TFORLOOP */ + ,opmode(0, 0, 1, 0, 0, iABC) /* OP_SETLIST */ + ,opmode(0, 0, 0, 0, 1, iABx) /* OP_CLOSURE */ + ,opmode(0, 1, 0, 0, 1, iABC) /* OP_VARARG */ + ,opmode(0, 0, 1, 0, 1, iABC) /* OP_VARARGPREP */ + ,opmode(0, 0, 0, 0, 0, iAx) /* OP_EXTRAARG */ }; diff --git a/lopcodes.h b/lopcodes.h index fd578c68a3..c5e9cf81e2 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -359,6 +359,7 @@ OP_EXTRAARG/* Ax extra (larger) argument for previous opcode */ ** bit 4: operator is a test (next instruction must be a jump) ** bit 5: instruction uses 'L->top' set by previous instruction (when B == 0) ** bit 6: instruction sets 'L->top' for next instruction (when C == 0) +** bit 7: instruction is an MM instruction (call a metamethod) */ LUAI_DDEC(const lu_byte luaP_opmodes[NUM_OPCODES];) @@ -368,6 +369,7 @@ LUAI_DDEC(const lu_byte luaP_opmodes[NUM_OPCODES];) #define testTMode(m) (luaP_opmodes[m] & (1 << 4)) #define testITMode(m) (luaP_opmodes[m] & (1 << 5)) #define testOTMode(m) (luaP_opmodes[m] & (1 << 6)) +#define testMMMode(m) (luaP_opmodes[m] & (1 << 7)) /* "out top" (set top for next instruction) */ #define isOT(i) \ @@ -377,7 +379,8 @@ LUAI_DDEC(const lu_byte luaP_opmodes[NUM_OPCODES];) /* "in top" (uses top from previous instruction) */ #define isIT(i) (testITMode(GET_OPCODE(i)) && GETARG_B(i) == 0) -#define opmode(ot,it,t,a,m) (((ot)<<6) | ((it)<<5) | ((t)<<4) | ((a)<<3) | (m)) +#define opmode(mm,ot,it,t,a,m) \ + (((mm) << 7) | ((ot) << 6) | ((it) << 5) | ((t) << 4) | ((a) << 3) | (m)) /* number of list items to accumulate before a SETLIST instruction */ From 91dad09f65984048ae43c8894d18acb785c7092b Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 10 Sep 2019 13:20:03 -0300 Subject: [PATCH 0495/1145] Removed arithmetic opcodes with immediate operand The difference in performance between immediate operands and K operands does not seem to justify all those extra opcodes. We only keep OP_ADDI, due to its ubiquity and because the difference is a little more relevant. (Later, OP_SUBI will be implemented by OP_ADDI, negating the constant.) --- lcode.c | 16 ++++++++-------- ljumptab.h | 6 ------ lopcodes.c | 6 ------ lopcodes.h | 6 ------ lopnames.h | 6 ------ lvm.c | 24 ------------------------ testes/code.lua | 18 +++++++++--------- 7 files changed, 17 insertions(+), 65 deletions(-) diff --git a/lcode.c b/lcode.c index 2c08409b77..841806fd9c 100644 --- a/lcode.c +++ b/lcode.c @@ -1342,7 +1342,7 @@ static void finishbinexpval (FuncState *fs, expdesc *e1, expdesc *e2, OpCode op, int v2, int flip, int line, OpCode mmop, TMS event) { int v1 = luaK_exp2anyreg(fs, e1); - int pc = luaK_codeABCk(fs, op, 0, v1, v2, flip); + int pc = luaK_codeABCk(fs, op, 0, v1, v2, 0); freeexps(fs, e1, e2); e1->u.info = pc; e1->k = VRELOC; /* all those operations are relocatable */ @@ -1372,7 +1372,7 @@ static void codebinexpval (FuncState *fs, OpCode op, /* -** Code binary operators ('+', '-', ...) with immediate operands. +** Code binary operators with immediate operands. */ static void codebini (FuncState *fs, OpCode op, expdesc *e1, expdesc *e2, int flip, int line, @@ -1389,15 +1389,12 @@ static void swapexps (expdesc *e1, expdesc *e2) { /* ** Code arithmetic operators ('+', '-', ...). If second operand is a -** constant in the proper range, use variant opcodes with immediate -** operands or K operands. +** constant in the proper range, use variant opcodes with K operands. */ static void codearith (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2, int flip, int line) { TMS event = cast(TMS, opr + TM_ADD); - if (isSCint(e2)) /* immediate operand? */ - codebini(fs, cast(OpCode, opr + OP_ADDI), e1, e2, flip, line, event); - else if (tonumeral(e2, NULL) && luaK_exp2K(fs, e2)) { /* K operand? */ + if (tonumeral(e2, NULL) && luaK_exp2K(fs, e2)) { /* K operand? */ int v2 = e2->u.info; /* K index */ OpCode op = cast(OpCode, opr + OP_ADDK); finishbinexpval(fs, e1, e2, op, v2, flip, line, OP_MMBINK, event); @@ -1423,7 +1420,10 @@ static void codecommutative (FuncState *fs, BinOpr op, swapexps(e1, e2); /* change order */ flip = 1; } - codearith(fs, op, e1, e2, flip, line); + if (op == OPR_ADD && isSCint(e2)) /* immediate operand? */ + codebini(fs, cast(OpCode, OP_ADDI), e1, e2, flip, line, TM_ADD); + else + codearith(fs, op, e1, e2, flip, line); } diff --git a/ljumptab.h b/ljumptab.h index 1832c809a8..37fe1e25d2 100644 --- a/ljumptab.h +++ b/ljumptab.h @@ -45,12 +45,6 @@ static void *disptab[NUM_OPCODES] = { &&L_OP_NEWTABLE, &&L_OP_SELF, &&L_OP_ADDI, -&&L_OP_SUBI, -&&L_OP_MULI, -&&L_OP_MODI, -&&L_OP_POWI, -&&L_OP_DIVI, -&&L_OP_IDIVI, &&L_OP_ADDK, &&L_OP_SUBK, &&L_OP_MULK, diff --git a/lopcodes.c b/lopcodes.c index 619592cee8..90d4cd1aae 100644 --- a/lopcodes.c +++ b/lopcodes.c @@ -39,12 +39,6 @@ LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = { ,opmode(0, 0, 0, 0, 1, iABC) /* OP_NEWTABLE */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_SELF */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_ADDI */ - ,opmode(0, 0, 0, 0, 1, iABC) /* OP_SUBI */ - ,opmode(0, 0, 0, 0, 1, iABC) /* OP_MULI */ - ,opmode(0, 0, 0, 0, 1, iABC) /* OP_MODI */ - ,opmode(0, 0, 0, 0, 1, iABC) /* OP_POWI */ - ,opmode(0, 0, 0, 0, 1, iABC) /* OP_DIVI */ - ,opmode(0, 0, 0, 0, 1, iABC) /* OP_IDIVI */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_ADDK */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_SUBK */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_MULK */ diff --git a/lopcodes.h b/lopcodes.h index c5e9cf81e2..d07a6e2d47 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -219,12 +219,6 @@ OP_NEWTABLE,/* A B C R(A) := {} */ OP_SELF,/* A B C R(A+1) := R(B); R(A) := R(B)[RK(C):string] */ OP_ADDI,/* A B sC R(A) := R(B) + C */ -OP_SUBI,/* A B sC R(A) := R(B) - C */ -OP_MULI,/* A B sC R(A) := R(B) * C */ -OP_MODI,/* A B sC R(A) := R(B) % C */ -OP_POWI,/* A B sC R(A) := R(B) ^ C */ -OP_DIVI,/* A B sC R(A) := R(B) / C */ -OP_IDIVI,/* A B sC R(A) := R(B) // C */ OP_ADDK,/* A B C R(A) := R(B) + K(C) */ OP_SUBK,/* A B C R(A) := R(B) - K(C) */ diff --git a/lopnames.h b/lopnames.h index 0fc1da1f26..de34730153 100644 --- a/lopnames.h +++ b/lopnames.h @@ -30,12 +30,6 @@ static const char *const opnames[] = { "NEWTABLE", "SELF", "ADDI", - "SUBI", - "MULI", - "MODI", - "POWI", - "DIVI", - "IDIVI", "ADDK", "SUBK", "MULK", diff --git a/lvm.c b/lvm.c index 46150ef0ad..e22a0da806 100644 --- a/lvm.c +++ b/lvm.c @@ -1271,30 +1271,6 @@ void luaV_execute (lua_State *L, CallInfo *ci) { op_arithI(L, l_addi, luai_numadd, TM_ADD, GETARG_k(i)); vmbreak; } - vmcase(OP_SUBI) { - op_arithI(L, l_subi, luai_numsub, TM_SUB, 0); - vmbreak; - } - vmcase(OP_MULI) { - op_arithI(L, l_muli, luai_nummul, TM_MUL, GETARG_k(i)); - vmbreak; - } - vmcase(OP_MODI) { - op_arithI(L, luaV_mod, luaV_modf, TM_MOD, 0); - vmbreak; - } - vmcase(OP_POWI) { - op_arithfI(L, luai_numpow, TM_POW); - vmbreak; - } - vmcase(OP_DIVI) { - op_arithfI(L, luai_numdiv, TM_DIV); - vmbreak; - } - vmcase(OP_IDIVI) { - op_arithI(L, luaV_idiv, luai_numidiv, TM_IDIV, 0); - vmbreak; - } vmcase(OP_ADDK) { op_arithK(L, l_addi, luai_numadd, GETARG_k(i)); vmbreak; diff --git a/testes/code.lua b/testes/code.lua index 96560166e0..642dfa68d3 100644 --- a/testes/code.lua +++ b/testes/code.lua @@ -296,14 +296,14 @@ checkR(function (x) return x + k1 end, 10, 11, 'ADDI', 'MMBINI', 'RETURN1') checkR(function (x) return 128 + x end, 0.0, 128.0, 'ADDI', 'MMBINI', 'RETURN1') checkR(function (x) return x * -127 end, -1.0, 127.0, - 'MULI', 'MMBINI', 'RETURN1') -checkR(function (x) return 20 * x end, 2, 40, 'MULI', 'MMBINI', 'RETURN1') -checkR(function (x) return x ^ -2 end, 2, 0.25, 'POWI', 'MMBINI', 'RETURN1') -checkR(function (x) return x / 40 end, 40, 1.0, 'DIVI', 'MMBINI', 'RETURN1') + 'MULK', 'MMBINK', 'RETURN1') +checkR(function (x) return 20 * x end, 2, 40, 'MULK', 'MMBINK', 'RETURN1') +checkR(function (x) return x ^ -2 end, 2, 0.25, 'POWK', 'MMBINK', 'RETURN1') +checkR(function (x) return x / 40 end, 40, 1.0, 'DIVK', 'MMBINK', 'RETURN1') checkR(function (x) return x // 1 end, 10.0, 10.0, - 'IDIVI', 'MMBINI', 'RETURN1') + 'IDIVK', 'MMBINK', 'RETURN1') checkR(function (x) return x % (100 - 10) end, 91, 1, - 'MODI', 'MMBINI', 'RETURN1') + 'MODK', 'MMBINK', 'RETURN1') checkR(function (x) return k1 << x end, 3, 8, 'SHLI', 'MMBINI', 'RETURN1') checkR(function (x) return x << 2 end, 10, 40, 'SHRI', 'MMBINI', 'RETURN1') checkR(function (x) return x >> 2 end, 8, 2, 'SHRI', 'MMBINI', 'RETURN1') @@ -326,9 +326,9 @@ checkR(function (x) return x % (100.0 - 10) end, 91, 1.0, -- no foldings (and immediate operands) check(function () return -0.0 end, 'LOADF', 'UNM', 'RETURN1') -check(function () return k3/0 end, 'LOADI', 'DIVI', 'MMBINI', 'RETURN1') -check(function () return 0%0 end, 'LOADI', 'MODI', 'MMBINI', 'RETURN1') -check(function () return -4//0 end, 'LOADI', 'IDIVI', 'MMBINI', 'RETURN1') +check(function () return k3/0 end, 'LOADI', 'DIVK', 'MMBINK', 'RETURN1') +check(function () return 0%0 end, 'LOADI', 'MODK', 'MMBINK', 'RETURN1') +check(function () return -4//0 end, 'LOADI', 'IDIVK', 'MMBINK', 'RETURN1') check(function (x) return x >> 2.0 end, 'LOADF', 'SHR', 'MMBIN', 'RETURN1') check(function (x) return x & 2.0 end, 'LOADF', 'BAND', 'MMBIN', 'RETURN1') From 40d8832ee05096f9aea8eb54d1cdccf2646aecd0 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 11 Sep 2019 10:20:10 -0300 Subject: [PATCH 0496/1145] Simplification in the call to 'constfolding' --- lcode.c | 26 +++++++++++--------------- lcode.h | 12 ++++++++++-- 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/lcode.c b/lcode.c index 841806fd9c..c48bc41eeb 100644 --- a/lcode.c +++ b/lcode.c @@ -1630,6 +1630,8 @@ static void codeconcat (FuncState *fs, expdesc *e1, expdesc *e2, int line) { void luaK_posfix (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2, int line) { luaK_dischargevars(fs, e2); + if (foldbinop(opr) && constfolding(fs, opr + LUA_OPADD, e1, e2)) + return; /* done by folding */ switch (opr) { case OPR_AND: { lua_assert(e1->t == NO_JUMP); /* list closed by 'luaK_infix' */ @@ -1649,35 +1651,29 @@ void luaK_posfix (FuncState *fs, BinOpr opr, break; } case OPR_ADD: case OPR_MUL: { - if (!constfolding(fs, opr + LUA_OPADD, e1, e2)) - codecommutative(fs, opr, e1, e2, line); + codecommutative(fs, opr, e1, e2, line); break; } case OPR_SUB: case OPR_DIV: case OPR_IDIV: case OPR_MOD: case OPR_POW: { - if (!constfolding(fs, opr + LUA_OPADD, e1, e2)) - codearith(fs, opr, e1, e2, 0, line); + codearith(fs, opr, e1, e2, 0, line); break; } case OPR_BAND: case OPR_BOR: case OPR_BXOR: { - if (!constfolding(fs, opr + LUA_OPADD, e1, e2)) - codebitwise(fs, opr, e1, e2, line); + codebitwise(fs, opr, e1, e2, line); break; } case OPR_SHL: { - if (!constfolding(fs, LUA_OPSHL, e1, e2)) { - if (isSCint(e1)) { - swapexps(e1, e2); - codebini(fs, OP_SHLI, e1, e2, 1, line, TM_SHL); - } - else - codeshift(fs, OP_SHL, e1, e2, line); + if (isSCint(e1)) { + swapexps(e1, e2); + codebini(fs, OP_SHLI, e1, e2, 1, line, TM_SHL); } + else + codeshift(fs, OP_SHL, e1, e2, line); break; } case OPR_SHR: { - if (!constfolding(fs, LUA_OPSHR, e1, e2)) - codeshift(fs, OP_SHR, e1, e2, line); + codeshift(fs, OP_SHR, e1, e2, line); break; } case OPR_EQ: case OPR_NE: { diff --git a/lcode.h b/lcode.h index 8cecd538f6..49c913abb4 100644 --- a/lcode.h +++ b/lcode.h @@ -24,19 +24,27 @@ ** grep "ORDER OPR" if you change these enums (ORDER OP) */ typedef enum BinOpr { + /* arithmetic operators */ OPR_ADD, OPR_SUB, OPR_MUL, OPR_MOD, OPR_POW, - OPR_DIV, - OPR_IDIV, + OPR_DIV, OPR_IDIV, + /* bitwise operators */ OPR_BAND, OPR_BOR, OPR_BXOR, OPR_SHL, OPR_SHR, + /* string operator */ OPR_CONCAT, + /* comparison operators */ OPR_EQ, OPR_LT, OPR_LE, OPR_NE, OPR_GT, OPR_GE, + /* logical operators */ OPR_AND, OPR_OR, OPR_NOBINOPR } BinOpr; +/* true if operation is foldable (that is, it is arithmetic or bitwise) */ +#define foldbinop(op) ((op) <= OPR_SHR) + + #define luaK_codeABC(fs,o,a,b,c) luaK_codeABCk(fs,o,a,b,c,0) From 6b2e202df55f3d1f3c670eab65981db6e125c758 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 19 Sep 2019 14:29:21 -0300 Subject: [PATCH 0497/1145] Janitorial work in 'lcode.c' --- lcode.c | 58 +++++++++++++++++++++++------------------------------- lopcodes.h | 13 +++++++----- 2 files changed, 33 insertions(+), 38 deletions(-) diff --git a/lcode.c b/lcode.c index c48bc41eeb..053b66b238 100644 --- a/lcode.c +++ b/lcode.c @@ -627,7 +627,7 @@ static int nilK (FuncState *fs) { /* ** Check whether 'i' can be stored in an 'sC' operand. -** Equivalent to (0 <= i + OFFSET_sC && i + OFFSET_sC <= MAXARG_C) +** Equivalent to (0 <= int2sC(i) && int2sC(i) <= MAXARG_C) ** but without risk of overflows in the addition. */ static int fitsC (lua_Integer i) { @@ -651,14 +651,9 @@ void luaK_int (FuncState *fs, int reg, lua_Integer i) { } -static int floatI (lua_Number f, lua_Integer *fi) { - return (luaV_flttointeger(f, fi, 0) && fitsBx(*fi)); -} - - static void luaK_float (FuncState *fs, int reg, lua_Number f) { lua_Integer fi; - if (floatI(f, &fi)) + if (luaV_flttointeger(f, &fi, 0) && fitsBx(fi)) luaK_codeAsBx(fs, OP_LOADF, reg, cast_int(fi)); else luaK_codek(fs, reg, luaK_numberK(fs, f)); @@ -1221,15 +1216,16 @@ static int isSCint (expdesc *e) { ** Check whether expression 'e' is a literal integer or float in ** proper range to fit in a register (sB or sC). */ -static int isSCnumber (expdesc *e, lua_Integer *i, int *isfloat) { +static int isSCnumber (expdesc *e, int *pi, int *isfloat) { + lua_Integer i; if (e->k == VKINT) - *i = e->u.ival; - else if (!(e->k == VKFLT && floatI(e->u.nval, i))) - return 0; /* not a number */ - else + i = e->u.ival; + else if (e->k == VKFLT && luaV_flttointeger(e->u.nval, &i, 0)) *isfloat = 1; - if (!hasjumps(e) && fitsC(*i)) { - *i += OFFSET_sC; + else + return 0; /* not a number */ + if (!hasjumps(e) && fitsC(i)) { + *pi = int2sC(cast_int(i)); return 1; } else @@ -1347,12 +1343,6 @@ static void finishbinexpval (FuncState *fs, expdesc *e1, expdesc *e2, e1->u.info = pc; e1->k = VRELOC; /* all those operations are relocatable */ luaK_fixline(fs, line); - if (op == OP_SHRI && flip) { - /* For the metamethod, undo the "changedir" did by 'codeshift' */ - event = TM_SHL; - v2 = -(v2 - OFFSET_sC) + OFFSET_sC; - flip = 0; - } luaK_codeABCk(fs, mmop, v1, v2, event, flip); /* to call metamethod */ luaK_fixline(fs, line); } @@ -1377,7 +1367,8 @@ static void codebinexpval (FuncState *fs, OpCode op, static void codebini (FuncState *fs, OpCode op, expdesc *e1, expdesc *e2, int flip, int line, TMS event) { - int v2 = cast_int(e2->u.ival) + OFFSET_sC; /* immediate operand */ + int v2 = int2sC(cast_int(e2->u.ival)); /* immediate operand */ + lua_assert(e2->k == VKINT); finishbinexpval(fs, e1, e2, op, v2, flip, line, OP_MMBINI, event); } @@ -1460,12 +1451,14 @@ static void codebitwise (FuncState *fs, BinOpr opr, static void codeshift (FuncState *fs, OpCode op, expdesc *e1, expdesc *e2, int line) { if (isSCint(e2)) { - int changedir = 0; - if (op == OP_SHL) { - changedir = 1; - e2->u.ival = -(e2->u.ival); + if (op == OP_SHR) + codebini(fs, OP_SHRI, e1, e2, 0, line, TM_SHR); + else { + int offset = cast_int(e2->u.ival); + finishbinexpval(fs, e1, e2, OP_SHRI, int2sC(offset), + 0, line, OP_MMBINI, TM_SHL); + SETARG_C(fs->f->code[fs->pc - 2], int2sC(-offset)); } - codebini(fs, OP_SHRI, e1, e2, changedir, line, TM_SHR); } else codebinexpval(fs, op, e1, e2, line); @@ -1478,18 +1471,18 @@ static void codeshift (FuncState *fs, OpCode op, */ static void codeorder (FuncState *fs, OpCode op, expdesc *e1, expdesc *e2) { int r1, r2; - lua_Integer im; + int im; int isfloat = 0; if (isSCnumber(e2, &im, &isfloat)) { /* use immediate operand */ r1 = luaK_exp2anyreg(fs, e1); - r2 = cast_int(im); + r2 = im; op = cast(OpCode, (op - OP_LT) + OP_LTI); } else if (isSCnumber(e1, &im, &isfloat)) { /* transform (A < B) to (B > A) and (A <= B) to (B >= A) */ r1 = luaK_exp2anyreg(fs, e2); - r2 = cast_int(im); + r2 = im; op = (op == OP_LT) ? OP_GTI : OP_GEI; } else { /* regular case, compare two registers */ @@ -1508,7 +1501,7 @@ static void codeorder (FuncState *fs, OpCode op, expdesc *e1, expdesc *e2) { */ static void codeeq (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) { int r1, r2; - lua_Integer im; + int im; int isfloat = 0; /* not needed here, but kept for symmetry */ OpCode op; if (e1->k != VNONRELOC) { @@ -1518,7 +1511,7 @@ static void codeeq (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) { r1 = luaK_exp2anyreg(fs, e1); /* 1nd expression must be in register */ if (isSCnumber(e2, &im, &isfloat)) { op = OP_EQI; - r2 = cast_int(im); /* immediate operand */ + r2 = im; /* immediate operand */ } else if (luaK_exp2RK(fs, e2)) { /* 1st expression is constant? */ op = OP_EQK; @@ -1591,8 +1584,7 @@ void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) { } case OPR_LT: case OPR_LE: case OPR_GT: case OPR_GE: { - lua_Integer dummy; - int dummy2; + int dummy, dummy2; if (!isSCnumber(v, &dummy, &dummy2)) luaK_exp2anyreg(fs, v); /* else keep numeral, which may be an immediate operand */ diff --git a/lopcodes.h b/lopcodes.h index d07a6e2d47..95241702c2 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -97,6 +97,9 @@ enum OpMode {iABC, iABx, iAsBx, iAx, isJ}; /* basic instruction formats */ #define MAXARG_C ((1<> 1) +#define int2sC(i) ((i) + OFFSET_sC) +#define sC2int(i) ((i) - OFFSET_sC) + /* creates a mask with 'n' 1 bits at position 'p' */ #define MASK1(n,p) ((~((~(Instruction)0)<<(n)))<<(p)) @@ -123,11 +126,11 @@ enum OpMode {iABC, iABx, iAsBx, iAx, isJ}; /* basic instruction formats */ #define SETARG_A(i,v) setarg(i, v, POS_A, SIZE_A) #define GETARG_B(i) check_exp(checkopm(i, iABC), getarg(i, POS_B, SIZE_B)) -#define GETARG_sB(i) (GETARG_B(i) - OFFSET_sC) +#define GETARG_sB(i) sC2int(GETARG_B(i)) #define SETARG_B(i,v) setarg(i, v, POS_B, SIZE_B) #define GETARG_C(i) check_exp(checkopm(i, iABC), getarg(i, POS_C, SIZE_C)) -#define GETARG_sC(i) (GETARG_C(i) - OFFSET_sC) +#define GETARG_sC(i) sC2int(GETARG_C(i)) #define SETARG_C(i,v) setarg(i, v, POS_C, SIZE_C) #define TESTARG_k(i) (cast_int(((i) & (1u << POS_k)))) @@ -249,9 +252,9 @@ OP_BXOR,/* A B C R(A) := R(B) ~ R(C) */ OP_SHL,/* A B C R(A) := R(B) << R(C) */ OP_SHR,/* A B C R(A) := R(B) >> R(C) */ -OP_MMBIN,/* A B C call B metamethod for previous bin. operation */ -OP_MMBINI,/* A B C call B metamethod for previous binI. operation */ -OP_MMBINK,/* A B C call B metamethod for previous binK. operation */ +OP_MMBIN,/* A B C call C metamethod over R(A) and R(B) */ +OP_MMBINI,/* A B C call C metamethod over R(A) and B */ +OP_MMBINK,/* A B C call C metamethod over R(A) and K(B) */ OP_UNM,/* A B R(A) := -R(B) */ OP_BNOT,/* A B R(A) := ~R(B) */ From 03cde80b58ea7f112f1b7a35c037893093b59f2e Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 24 Sep 2019 14:31:06 -0300 Subject: [PATCH 0498/1145] 'setCstacklimit' renamed to 'setcstacklimit' Function names in the API use only lowercase letters. --- ldblib.c | 6 +++--- lstate.c | 2 +- lua.h | 2 +- manual/manual.of | 12 ++++++------ testes/cstack.lua | 24 ++++++++++++------------ 5 files changed, 23 insertions(+), 23 deletions(-) diff --git a/ldblib.c b/ldblib.c index 9f7aad5129..a9a84c506b 100644 --- a/ldblib.c +++ b/ldblib.c @@ -437,9 +437,9 @@ static int db_traceback (lua_State *L) { } -static int db_setCstacklimit (lua_State *L) { +static int db_setcstacklimit (lua_State *L) { int limit = (int)luaL_checkinteger(L, 1); - int res = lua_setCstacklimit(L, limit); + int res = lua_setcstacklimit(L, limit); if (res == 0) lua_pushboolean(L, 0); else @@ -465,7 +465,7 @@ static const luaL_Reg dblib[] = { {"setmetatable", db_setmetatable}, {"setupvalue", db_setupvalue}, {"traceback", db_traceback}, - {"setCstacklimit", db_setCstacklimit}, + {"setcstacklimit", db_setcstacklimit}, {NULL, NULL} }; diff --git a/lstate.c b/lstate.c index 86cd5fb824..1c33dcfcf1 100644 --- a/lstate.c +++ b/lstate.c @@ -96,7 +96,7 @@ void luaE_setdebt (global_State *g, l_mem debt) { } -LUA_API int lua_setCstacklimit (lua_State *L, unsigned int limit) { +LUA_API int lua_setcstacklimit (lua_State *L, unsigned int limit) { global_State *g = G(L); int ccalls; luaE_freeCI(L); /* release unused CIs */ diff --git a/lua.h b/lua.h index d3575fd9c9..bd2631e5ce 100644 --- a/lua.h +++ b/lua.h @@ -462,7 +462,7 @@ LUA_API lua_Hook (lua_gethook) (lua_State *L); LUA_API int (lua_gethookmask) (lua_State *L); LUA_API int (lua_gethookcount) (lua_State *L); -LUA_API int (lua_setCstacklimit) (lua_State *L, unsigned int limit); +LUA_API int (lua_setcstacklimit) (lua_State *L, unsigned int limit); struct lua_Debug { int event; diff --git a/manual/manual.of b/manual/manual.of index 292d1e515c..cca4ca08a6 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -4814,7 +4814,7 @@ calling @Lid{lua_yield} with @id{nresults} equal to zero } -@APIEntry{int (lua_setCstacklimit) (lua_State *L, unsigned int limit);| +@APIEntry{int (lua_setcstacklimit) (lua_State *L, unsigned int limit);| @apii{0,0,-} Sets a new limit for the C stack. @@ -4823,7 +4823,7 @@ with the intent of avoiding a stack overflow. Returns the old limit in case of success, or zero in case of error. For more details about this function, -see @Lid{debug.setCstacklimit}, +see @Lid{debug.setcstacklimit}, its equivalent in the standard library. } @@ -8565,7 +8565,7 @@ to the userdata @id{u} plus a boolean, } -@LibEntry{debug.setCstacklimit (limit)| +@LibEntry{debug.setcstacklimit (limit)| Sets a new limit for the C stack. This limit controls how deeply nested calls can go in Lua, @@ -8586,10 +8586,10 @@ This function has the following restrictions: @item{@id{limit} must be less than 40000;} @item{@id{limit} cannot be less than the amount of C stack in use.} } -In case of success, -this function returns the old limit. -In case of error, +If a call does not respect some restriction, it returns @false. +Otherwise, +the call returns the old limit. } diff --git a/testes/cstack.lua b/testes/cstack.lua index 2a55ce21a3..486abc1df5 100644 --- a/testes/cstack.lua +++ b/testes/cstack.lua @@ -8,7 +8,7 @@ print"If this test craches, see its file ('cstack.lua')" -- Segmentation faults in these tests probably result from a C-stack -- overflow. To avoid these errors, you can use the function --- 'debug.setCstacklimit' to set a smaller limit for the use of +-- 'debug.setcstacklimit' to set a smaller limit for the use of -- C stack by Lua. After finding a reliable limit, you might want -- to recompile Lua with this limit as the value for -- the constant 'LUAI_MAXCCALLS', which defines the default limit. @@ -19,12 +19,12 @@ print"If this test craches, see its file ('cstack.lua')" -- higher than 2_000. -local origlimit = debug.setCstacklimit(400) +local origlimit = debug.setcstacklimit(400) print("default stack limit: " .. origlimit) -- change this value for different limits for this test suite local currentlimit = origlimit -debug.setCstacklimit(currentlimit) +debug.setcstacklimit(currentlimit) print("current stack limit: " .. currentlimit) @@ -102,10 +102,10 @@ end do print("testing changes in C-stack limit") - assert(not debug.setCstacklimit(0)) -- limit too small - assert(not debug.setCstacklimit(50000)) -- limit too large + assert(not debug.setcstacklimit(0)) -- limit too small + assert(not debug.setcstacklimit(50000)) -- limit too large local co = coroutine.wrap (function () - return debug.setCstacklimit(400) + return debug.setcstacklimit(400) end) assert(co() == false) -- cannot change C stack inside coroutine @@ -118,26 +118,26 @@ do print("testing changes in C-stack limit") return n end - assert(debug.setCstacklimit(400) == currentlimit) + assert(debug.setcstacklimit(400) == currentlimit) local lim400 = check() -- a very low limit (given that the several calls to arive here) local lowlimit = 38 - assert(debug.setCstacklimit(lowlimit) == 400) + assert(debug.setcstacklimit(lowlimit) == 400) assert(check() < lowlimit - 30) - assert(debug.setCstacklimit(600) == lowlimit) + assert(debug.setcstacklimit(600) == lowlimit) local lim600 = check() assert(lim600 == lim400 + 200) - -- 'setCstacklimit' works inside protected calls. (The new stack + -- 'setcstacklimit' works inside protected calls. (The new stack -- limit is kept when 'pcall' returns.) assert(pcall(function () - assert(debug.setCstacklimit(400) == 600) + assert(debug.setcstacklimit(400) == 600) assert(check() <= lim400) end)) assert(check() == lim400) - assert(debug.setCstacklimit(origlimit) == 400) -- restore original limit + assert(debug.setcstacklimit(origlimit) == 400) -- restore original limit end From 6ef366644f7c3c21cfb17434835edf4ebf970d6d Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 24 Sep 2019 14:34:52 -0300 Subject: [PATCH 0499/1145] Subtraction of small constant integers optimized with OP_ADDI --- lcode.c | 68 ++++++++++++++++++++++++++++------------------ testes/bitwise.lua | 6 ++-- testes/code.lua | 1 + testes/locals.lua | 2 +- 4 files changed, 46 insertions(+), 31 deletions(-) diff --git a/lcode.c b/lcode.c index 053b66b238..bc86793af6 100644 --- a/lcode.c +++ b/lcode.c @@ -1212,6 +1212,15 @@ static int isSCint (expdesc *e) { } +/* +** Check whether expression 'e' and its negation are literal integers +** in proper range to fit in register sC +*/ +static int isSCintN (expdesc *e) { + return luaK_isKint(e) && fitsC(e->u.ival) && fitsC(-e->u.ival); +} + + /* ** Check whether expression 'e' is a literal integer or float in ** proper range to fit in a register (sB or sC). @@ -1373,6 +1382,18 @@ static void codebini (FuncState *fs, OpCode op, } +/* Code binary operators negating the immediate operand for the +** opcode. For the metamethod, 'v2' must keep its original value. +*/ +static void finishbinexpneg (FuncState *fs, expdesc *e1, expdesc *e2, + OpCode op, int line, TMS event) { + int v2 = cast_int(e2->u.ival); + finishbinexpval(fs, e1, e2, op, int2sC(-v2), 0, line, OP_MMBINI, event); + /* correct metamethod argument */ + SETARG_B(fs->f->code[fs->pc - 1], int2sC(v2)); +} + + static void swapexps (expdesc *e1, expdesc *e2) { expdesc temp = *e1; *e1 = *e2; *e2 = temp; /* swap 'e1' and 'e2' */ } @@ -1444,27 +1465,6 @@ static void codebitwise (FuncState *fs, BinOpr opr, } -/* -** Code shift operators. If second operand is constant, use immediate -** operand (negating it if shift is in the other direction). -*/ -static void codeshift (FuncState *fs, OpCode op, - expdesc *e1, expdesc *e2, int line) { - if (isSCint(e2)) { - if (op == OP_SHR) - codebini(fs, OP_SHRI, e1, e2, 0, line, TM_SHR); - else { - int offset = cast_int(e2->u.ival); - finishbinexpval(fs, e1, e2, OP_SHRI, int2sC(offset), - 0, line, OP_MMBINI, TM_SHL); - SETARG_C(fs->f->code[fs->pc - 2], int2sC(-offset)); - } - } - else - codebinexpval(fs, op, e1, e2, line); -} - - /* ** Emit code for order comparisons. When using an immediate operand, ** 'isfloat' tells whether the original value was a float. @@ -1646,8 +1646,15 @@ void luaK_posfix (FuncState *fs, BinOpr opr, codecommutative(fs, opr, e1, e2, line); break; } - case OPR_SUB: case OPR_DIV: - case OPR_IDIV: case OPR_MOD: case OPR_POW: { + case OPR_SUB: { + if (isSCintN(e2)) { /* subtracting a small integer constant? */ + /* code it as (r1 + -I) */ + finishbinexpneg(fs, e1, e2, OP_ADDI, line, TM_SUB); + break; + } + /* ELSE *//* FALLTHROUGH */ + } + case OPR_DIV: case OPR_IDIV: case OPR_MOD: case OPR_POW: { codearith(fs, opr, e1, e2, 0, line); break; } @@ -1658,14 +1665,21 @@ void luaK_posfix (FuncState *fs, BinOpr opr, case OPR_SHL: { if (isSCint(e1)) { swapexps(e1, e2); - codebini(fs, OP_SHLI, e1, e2, 1, line, TM_SHL); + codebini(fs, OP_SHLI, e1, e2, 1, line, TM_SHL); /* I << r2 */ + } + else if (isSCintN(e2)) { /* shifting by a small integer constant? */ + /* code it as (r1 >> -I) */ + finishbinexpneg(fs, e1, e2, OP_SHRI, line, TM_SHL); } - else - codeshift(fs, OP_SHL, e1, e2, line); + else /* regular case (two registers) */ + codebinexpval(fs, OP_SHL, e1, e2, line); break; } case OPR_SHR: { - codeshift(fs, OP_SHR, e1, e2, line); + if (isSCintN(e2)) + codebini(fs, OP_SHRI, e1, e2, 0, line, TM_SHR); /* r1 >> I */ + else /* regular case (two registers) */ + codebinexpval(fs, OP_SHR, e1, e2, line); break; } case OPR_EQ: case OPR_NE: { diff --git a/testes/bitwise.lua b/testes/bitwise.lua index af542e7f68..59781f5dfa 100644 --- a/testes/bitwise.lua +++ b/testes/bitwise.lua @@ -60,9 +60,9 @@ assert("1234.0" << "5.0" == 1234 * 32) assert("0xffff.0" ~ "0xAAAA" == 0x5555) assert(~"0x0.000p4" == -1) -assert("7" .. 3 << 1 == 146) -assert(10 >> 1 .. "9" == 0) -assert(10 | 1 .. "9" == 27) +assert(("7" .. 3) << 1 == 146) +assert(0xffffffff >> (1 .. "9") == 0x1fff) +assert(10 | (1 .. "9") == 27) do local st, msg = pcall(function () return 4 & "a" end) diff --git a/testes/code.lua b/testes/code.lua index 642dfa68d3..ab531fa8ed 100644 --- a/testes/code.lua +++ b/testes/code.lua @@ -293,6 +293,7 @@ checkK(function () return -(border + 1) end, -(sbx + 1.0)) -- immediate operands checkR(function (x) return x + k1 end, 10, 11, 'ADDI', 'MMBINI', 'RETURN1') +checkR(function (x) return x - 127 end, 10, -117, 'ADDI', 'MMBINI', 'RETURN1') checkR(function (x) return 128 + x end, 0.0, 128.0, 'ADDI', 'MMBINI', 'RETURN1') checkR(function (x) return x * -127 end, -1.0, 127.0, diff --git a/testes/locals.lua b/testes/locals.lua index 58ad18cc55..b769575f8a 100644 --- a/testes/locals.lua +++ b/testes/locals.lua @@ -82,7 +82,7 @@ assert(c.a == nil) f() assert(c.a == 3) --- old test for limits for special instructions (now just a generic test) +-- old test for limits for special instructions do local i = 2 local p = 4 -- p == 2^i From 89f6a85f034b2535e43e421991098fa05a92cd60 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 24 Sep 2019 14:43:32 -0300 Subject: [PATCH 0500/1145] Details in the makefile (warning options) --- makefile | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/makefile b/makefile index cf238aeb22..ba3219bcb5 100644 --- a/makefile +++ b/makefile @@ -6,6 +6,7 @@ # Warnings valid for both C and C++ CWARNSCPP= \ + -fmax-errors=5 \ -Wextra \ -Wshadow \ -Wsign-compare \ @@ -14,14 +15,13 @@ CWARNSCPP= \ -Wredundant-decls \ -Wdisabled-optimization \ -Wdouble-promotion \ - #-Wno-aggressive-loop-optimizations \ - #-Wlogical-op \ - #-Wfatal-errors \ - #-Wstrict-aliasing=3 \ + -Wlogical-op \ + -Wno-aggressive-loop-optimizations \ + # the next warnings might be useful sometimes, + # but usually they generate too much noise # -Werror \ # -pedantic # warns if we use jump tables \ - # the next warnings generate too much noise, so they are disabled - # -Wconversion -Wno-sign-conversion \ + # -Wconversion \ # -Wsign-conversion \ # -Wstrict-overflow=2 \ # -Wformat=2 \ @@ -46,10 +46,10 @@ CWARNS= $(CWARNSCPP) $(CWARNSC) # -DLUA_USE_CTYPE -DLUA_USE_APICHECK # ('-ftrapv' for runtime checks of integer overflows) # -fsanitize=undefined -ftrapv -fno-inline -TESTS= -DLUA_USER_H='"ltests.h"' -O0 +# TESTS= -DLUA_USER_H='"ltests.h"' -O0 -g -# LOCAL = $(TESTS) $(CWARNS) -g +LOCAL = $(TESTS) $(CWARNS) # enable Linux goodies From b2a580bdb1982e45bb37f95b78c2dafec6efa7a6 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 1 Oct 2019 17:24:37 -0300 Subject: [PATCH 0501/1145] Janitorial work - Several details in 'lcode.c' - A few more tests for code generation - Bug in assert in 'lcode.c' ("=" x "==") - Comments in 'lopcodes.h' and 'ltable.c' --- lcode.c | 61 +++++++++++++++++++++++-------------------------- lopcodes.h | 8 +++---- ltable.c | 44 +++++++++++++++++++---------------- testes/code.lua | 7 ++++-- 4 files changed, 62 insertions(+), 58 deletions(-) diff --git a/lcode.c b/lcode.c index bc86793af6..abb8a811ff 100644 --- a/lcode.c +++ b/lcode.c @@ -359,12 +359,12 @@ static void removelastlineinfo (FuncState *fs) { Proto *f = fs->f; int pc = fs->pc - 1; /* last instruction coded */ if (f->lineinfo[pc] != ABSLINEINFO) { /* relative line info? */ - fs->previousline -= f->lineinfo[pc]; /* last line saved */ - fs->iwthabs--; + fs->previousline -= f->lineinfo[pc]; /* correct last line saved */ + fs->iwthabs--; /* undo previous increment */ } else { /* absolute line information */ + lua_assert(f->abslineinfo[fs->nabslineinfo - 1].pc == pc); fs->nabslineinfo--; /* remove it */ - lua_assert(f->abslineinfo[fs->nabslineinfo].pc = pc); fs->iwthabs = MAXIWTHABS + 1; /* force next line info to be absolute */ } } @@ -626,12 +626,12 @@ static int nilK (FuncState *fs) { /* -** Check whether 'i' can be stored in an 'sC' operand. -** Equivalent to (0 <= int2sC(i) && int2sC(i) <= MAXARG_C) -** but without risk of overflows in the addition. +** Check whether 'i' can be stored in an 'sC' operand. Equivalent to +** (0 <= int2sC(i) && int2sC(i) <= MAXARG_C) but without risk of +** overflows in the hidden addition inside 'int2sC'. */ static int fitsC (lua_Integer i) { - return (-OFFSET_sC <= i && i <= MAXARG_C - OFFSET_sC); + return (l_castS2U(i) + OFFSET_sC <= cast_uint(MAXARG_C)); } @@ -1212,15 +1212,6 @@ static int isSCint (expdesc *e) { } -/* -** Check whether expression 'e' and its negation are literal integers -** in proper range to fit in register sC -*/ -static int isSCintN (expdesc *e) { - return luaK_isKint(e) && fitsC(e->u.ival) && fitsC(-e->u.ival); -} - - /* ** Check whether expression 'e' is a literal integer or float in ** proper range to fit in a register (sB or sC). @@ -1382,15 +1373,25 @@ static void codebini (FuncState *fs, OpCode op, } -/* Code binary operators negating the immediate operand for the -** opcode. For the metamethod, 'v2' must keep its original value. +/* Try to code a binary operator negating its second operand. +** For the metamethod, 2nd operand must keep its original value. */ -static void finishbinexpneg (FuncState *fs, expdesc *e1, expdesc *e2, +static int finishbinexpneg (FuncState *fs, expdesc *e1, expdesc *e2, OpCode op, int line, TMS event) { - int v2 = cast_int(e2->u.ival); - finishbinexpval(fs, e1, e2, op, int2sC(-v2), 0, line, OP_MMBINI, event); - /* correct metamethod argument */ - SETARG_B(fs->f->code[fs->pc - 1], int2sC(v2)); + if (!luaK_isKint(e2)) + return 0; /* not an integer constant */ + else { + lua_Integer i2 = e2->u.ival; + if (!(fitsC(i2) && fitsC(-i2))) + return 0; /* not in the proper range */ + else { /* operating a small integer constant */ + int v2 = cast_int(i2); + finishbinexpval(fs, e1, e2, op, int2sC(-v2), 0, line, OP_MMBINI, event); + /* correct metamethod argument */ + SETARG_B(fs->f->code[fs->pc - 1], int2sC(v2)); + return 1; /* successfully coded */ + } + } } @@ -1647,11 +1648,8 @@ void luaK_posfix (FuncState *fs, BinOpr opr, break; } case OPR_SUB: { - if (isSCintN(e2)) { /* subtracting a small integer constant? */ - /* code it as (r1 + -I) */ - finishbinexpneg(fs, e1, e2, OP_ADDI, line, TM_SUB); - break; - } + if (finishbinexpneg(fs, e1, e2, OP_ADDI, line, TM_SUB)) + break; /* coded as (r1 + -I) */ /* ELSE *//* FALLTHROUGH */ } case OPR_DIV: case OPR_IDIV: case OPR_MOD: case OPR_POW: { @@ -1667,16 +1665,15 @@ void luaK_posfix (FuncState *fs, BinOpr opr, swapexps(e1, e2); codebini(fs, OP_SHLI, e1, e2, 1, line, TM_SHL); /* I << r2 */ } - else if (isSCintN(e2)) { /* shifting by a small integer constant? */ - /* code it as (r1 >> -I) */ - finishbinexpneg(fs, e1, e2, OP_SHRI, line, TM_SHL); + else if (finishbinexpneg(fs, e1, e2, OP_SHRI, line, TM_SHL)) { + /* coded as (r1 >> -I) */; } else /* regular case (two registers) */ codebinexpval(fs, OP_SHL, e1, e2, line); break; } case OPR_SHR: { - if (isSCintN(e2)) + if (isSCint(e2)) codebini(fs, OP_SHRI, e1, e2, 0, line, TM_SHR); /* r1 >> I */ else /* regular case (two registers) */ codebinexpval(fs, OP_SHR, e1, e2, line); diff --git a/lopcodes.h b/lopcodes.h index 95241702c2..443a71e97c 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -221,7 +221,7 @@ OP_NEWTABLE,/* A B C R(A) := {} */ OP_SELF,/* A B C R(A+1) := R(B); R(A) := R(B)[RK(C):string] */ -OP_ADDI,/* A B sC R(A) := R(B) + C */ +OP_ADDI,/* A B sC R(A) := R(B) + sC */ OP_ADDK,/* A B C R(A) := R(B) + K(C) */ OP_SUBK,/* A B C R(A) := R(B) - K(C) */ @@ -235,8 +235,8 @@ OP_BANDK,/* A B C R(A) := R(B) & K(C):integer */ OP_BORK,/* A B C R(A) := R(B) | K(C):integer */ OP_BXORK,/* A B C R(A) := R(B) ~ K(C):integer */ -OP_SHRI,/* A B sC R(A) := R(B) >> C */ -OP_SHLI,/* A B sC R(A) := C << R(B) */ +OP_SHRI,/* A B sC R(A) := R(B) >> sC */ +OP_SHLI,/* A B sC R(A) := sC << R(B) */ OP_ADD,/* A B C R(A) := R(B) + R(C) */ OP_SUB,/* A B C R(A) := R(B) - R(C) */ @@ -253,7 +253,7 @@ OP_SHL,/* A B C R(A) := R(B) << R(C) */ OP_SHR,/* A B C R(A) := R(B) >> R(C) */ OP_MMBIN,/* A B C call C metamethod over R(A) and R(B) */ -OP_MMBINI,/* A B C call C metamethod over R(A) and B */ +OP_MMBINI,/* A sB C call C metamethod over R(A) and sB */ OP_MMBINK,/* A B C call C metamethod over R(A) and K(B) */ OP_UNM,/* A B R(A) := -R(B) */ diff --git a/ltable.c b/ltable.c index d8ff3d80c2..5561d45ebc 100644 --- a/ltable.c +++ b/ltable.c @@ -833,39 +833,41 @@ static unsigned int binsearch (const TValue *array, unsigned int i, ** and 'maxinteger' if t[maxinteger] is present.) ** (In the next explanation, we use Lua indices, that is, with base 1. ** The code itself uses base 0 when indexing the array part of the table.) -** The code starts with 'limit', a position in the array part that may -** be a boundary. +** The code starts with 'limit = t->alimit', a position in the array +** part that may be a boundary. +** ** (1) If 't[limit]' is empty, there must be a boundary before it. -** As a common case (e.g., after 't[#t]=nil'), check whether 'hint-1' +** As a common case (e.g., after 't[#t]=nil'), check whether 'limit-1' ** is present. If so, it is a boundary. Otherwise, do a binary search ** between 0 and limit to find a boundary. In both cases, try to -** use this boundary as the new 'limit', as a hint for the next call. +** use this boundary as the new 'alimit', as a hint for the next call. +** ** (2) If 't[limit]' is not empty and the array has more elements ** after 'limit', try to find a boundary there. Again, try first ** the special case (which should be quite frequent) where 'limit+1' ** is empty, so that 'limit' is a boundary. Otherwise, check the -** last element of the array part (set it as a new limit). If it is empty, -** there must be a boundary between the old limit (present) and the new -** limit (absent), which is found with a binary search. (This boundary -** always can be a new limit.) +** last element of the array part. If it is empty, there must be a +** boundary between the old limit (present) and the last element +** (absent), which is found with a binary search. (This boundary always +** can be a new limit.) +** ** (3) The last case is when there are no elements in the array part ** (limit == 0) or its last element (the new limit) is present. -** In this case, must check the hash part. If there is no hash part, -** the boundary is 0. Otherwise, if 'limit+1' is absent, 'limit' is -** a boundary. Finally, if 'limit+1' is present, call 'hash_search' -** to find a boundary in the hash part of the table. (In those -** cases, the boundary is not inside the array part, and therefore -** cannot be used as a new limit.) +** In this case, must check the hash part. If there is no hash part +** or 'limit+1' is absent, 'limit' is a boundary. Otherwise, call +** 'hash_search' to find a boundary in the hash part of the table. +** (In those cases, the boundary is not inside the array part, and +** therefore cannot be used as a new limit.) */ lua_Unsigned luaH_getn (Table *t) { unsigned int limit = t->alimit; - if (limit > 0 && isempty(&t->array[limit - 1])) { - /* (1) there must be a boundary before 'limit' */ + if (limit > 0 && isempty(&t->array[limit - 1])) { /* (1)? */ + /* there must be a boundary before 'limit' */ if (limit >= 2 && !isempty(&t->array[limit - 2])) { /* 'limit - 1' is a boundary; can it be a new limit? */ if (ispow2realasize(t) && !ispow2(limit - 1)) { t->alimit = limit - 1; - setnorealasize(t); + setnorealasize(t); /* now 'alimit' is not the real size */ } return limit - 1; } @@ -880,8 +882,8 @@ lua_Unsigned luaH_getn (Table *t) { } } /* 'limit' is zero or present in table */ - if (!limitequalsasize(t)) { - /* (2) 'limit' > 0 and array has more elements after 'limit' */ + if (!limitequalsasize(t)) { /* (2)? */ + /* 'limit' > 0 and array has more elements after 'limit' */ if (isempty(&t->array[limit])) /* 'limit + 1' is empty? */ return limit; /* this is the boundary */ /* else, try last element in the array */ @@ -899,7 +901,7 @@ lua_Unsigned luaH_getn (Table *t) { lua_assert(limit == luaH_realasize(t) && (limit == 0 || !isempty(&t->array[limit - 1]))); if (isdummy(t) || isempty(luaH_getint(t, cast(lua_Integer, limit + 1)))) - return limit; /* 'limit + 1' is absent... */ + return limit; /* 'limit + 1' is absent */ else /* 'limit + 1' is also present */ return hash_search(t, limit); } @@ -908,6 +910,8 @@ lua_Unsigned luaH_getn (Table *t) { #if defined(LUA_DEBUG) +/* export these functions for the test library */ + Node *luaH_mainposition (const Table *t, const TValue *key) { return mainpositionTV(t, key); } diff --git a/testes/code.lua b/testes/code.lua index ab531fa8ed..e12f3f9118 100644 --- a/testes/code.lua +++ b/testes/code.lua @@ -306,8 +306,10 @@ checkR(function (x) return x // 1 end, 10.0, 10.0, checkR(function (x) return x % (100 - 10) end, 91, 1, 'MODK', 'MMBINK', 'RETURN1') checkR(function (x) return k1 << x end, 3, 8, 'SHLI', 'MMBINI', 'RETURN1') -checkR(function (x) return x << 2 end, 10, 40, 'SHRI', 'MMBINI', 'RETURN1') -checkR(function (x) return x >> 2 end, 8, 2, 'SHRI', 'MMBINI', 'RETURN1') +checkR(function (x) return x << 127 end, 10, 0, 'SHRI', 'MMBINI', 'RETURN1') +checkR(function (x) return x << -127 end, 10, 0, 'SHRI', 'MMBINI', 'RETURN1') +checkR(function (x) return x >> 128 end, 8, 0, 'SHRI', 'MMBINI', 'RETURN1') +checkR(function (x) return x >> -127 end, 8, 0, 'SHRI', 'MMBINI', 'RETURN1') checkR(function (x) return x & 1 end, 9, 1, 'BANDK', 'MMBINK', 'RETURN1') checkR(function (x) return 10 | x end, 1, 11, 'BORK', 'MMBINK', 'RETURN1') checkR(function (x) return -10 ~ x end, -1, 9, 'BXORK', 'MMBINK', 'RETURN1') @@ -331,6 +333,7 @@ check(function () return k3/0 end, 'LOADI', 'DIVK', 'MMBINK', 'RETURN1') check(function () return 0%0 end, 'LOADI', 'MODK', 'MMBINK', 'RETURN1') check(function () return -4//0 end, 'LOADI', 'IDIVK', 'MMBINK', 'RETURN1') check(function (x) return x >> 2.0 end, 'LOADF', 'SHR', 'MMBIN', 'RETURN1') +check(function (x) return x << 128 end, 'LOADI', 'SHL', 'MMBIN', 'RETURN1') check(function (x) return x & 2.0 end, 'LOADF', 'BAND', 'MMBIN', 'RETURN1') -- basic 'for' loops From b98d41db99969f6336c32cb67274093b9a548d39 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 2 Oct 2019 17:04:06 -0300 Subject: [PATCH 0502/1145] Script 'packtests' gets Lua version as a parameter --- testes/packtests | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/testes/packtests b/testes/packtests index f7bca93dc2..c19b74bf3f 100755 --- a/testes/packtests +++ b/testes/packtests @@ -1,4 +1,4 @@ -NAME="lua-5.4.0-alpha-tests" +NAME=$1"-tests" ln -s . $NAME ln -s .. ltests @@ -49,4 +49,6 @@ $NAME/ltests/ltests.c \rm -I $NAME \rm -I ltests +echo ${NAME}.tar.gz" created" + From 7bd1e53753de7176eb0b23f2bf19ad2235dec826 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 4 Oct 2019 16:17:04 -0300 Subject: [PATCH 0503/1145] Fixed a warning and other minor issues Fixed some minor issues from the feedback for 5.4-beta rc1. --- lcode.c | 4 ++-- lcorolib.c | 3 ++- ldblib.c | 2 +- lgc.c | 2 +- loadlib.c | 2 +- lparser.c | 4 ++-- 6 files changed, 9 insertions(+), 8 deletions(-) diff --git a/lcode.c b/lcode.c index abb8a811ff..3e4c5b497e 100644 --- a/lcode.c +++ b/lcode.c @@ -1650,8 +1650,8 @@ void luaK_posfix (FuncState *fs, BinOpr opr, case OPR_SUB: { if (finishbinexpneg(fs, e1, e2, OP_ADDI, line, TM_SUB)) break; /* coded as (r1 + -I) */ - /* ELSE *//* FALLTHROUGH */ - } + /* ELSE */ + } /* FALLTHROUGH */ case OPR_DIV: case OPR_IDIV: case OPR_MOD: case OPR_POW: { codearith(fs, opr, e1, e2, 0, line); break; diff --git a/lcorolib.c b/lcorolib.c index 4d47ea28ac..7d6e585b1d 100644 --- a/lcorolib.c +++ b/lcorolib.c @@ -116,7 +116,8 @@ static int luaB_yield (lua_State *L) { #define COS_NORM 3 -static const char *statname[] = {"running", "dead", "suspended", "normal"}; +static const char *const statname[] = + {"running", "dead", "suspended", "normal"}; static int auxstatus (lua_State *L, lua_State *co) { diff --git a/ldblib.c b/ldblib.c index a9a84c506b..77018986b7 100644 --- a/ldblib.c +++ b/ldblib.c @@ -24,7 +24,7 @@ ** The hook table at registry[HOOKKEY] maps threads to their current ** hook function. */ -static const char* HOOKKEY = "_HOOKKEY"; +static const char *const HOOKKEY = "_HOOKKEY"; /* diff --git a/lgc.c b/lgc.c index f24074f920..cf62a45c0f 100644 --- a/lgc.c +++ b/lgc.c @@ -998,7 +998,7 @@ static void sweep2old (lua_State *L, GCObject **p) { */ static GCObject **sweepgen (lua_State *L, global_State *g, GCObject **p, GCObject *limit) { - static lu_byte nextage[] = { + static const lu_byte nextage[] = { G_SURVIVAL, /* from G_NEW */ G_OLD1, /* from G_SURVIVAL */ G_OLD1, /* from G_OLD0 */ diff --git a/loadlib.c b/loadlib.c index d7a3fb23cb..689767f3a3 100644 --- a/loadlib.c +++ b/loadlib.c @@ -59,7 +59,7 @@ ** key for table in the registry that keeps handles ** for all loaded C libraries */ -static const char *CLIBS = "_CLIBS"; +static const char *const CLIBS = "_CLIBS"; #define LIB_FAIL "open" diff --git a/lparser.c b/lparser.c index 2dcd320cc4..8c81203901 100644 --- a/lparser.c +++ b/lparser.c @@ -1523,8 +1523,8 @@ static void fixforjump (FuncState *fs, int pc, int dest, int back) { */ static void forbody (LexState *ls, int base, int line, int nvars, int isgen) { /* forbody -> DO block */ - static OpCode forprep[2] = {OP_FORPREP, OP_TFORPREP}; - static OpCode forloop[2] = {OP_FORLOOP, OP_TFORLOOP}; + static const OpCode forprep[2] = {OP_FORPREP, OP_TFORPREP}; + static const OpCode forloop[2] = {OP_FORLOOP, OP_TFORLOOP}; BlockCnt bl; FuncState *fs = ls->fs; int prep, endfor; From 6a10f03ff81606e567c6891a90d70066a03c686e Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 8 Oct 2019 10:26:02 -0300 Subject: [PATCH 0504/1145] Makefile compiles the Lua compiler with '-Os' The performance of the Lua compiler is not critical for Lua performance, but it is a big component in the source. So, it makes sense to trade speed for size in this component. --- makefile | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/makefile b/makefile index ba3219bcb5..2c68f45473 100644 --- a/makefile +++ b/makefile @@ -106,6 +106,16 @@ $(LUA_T): $(LUA_O) $(CORE_T) $(LUAC_T): $(LUAC_O) $(CORE_T) $(CC) -o $@ $(MYLDFLAGS) $(LUAC_O) $(CORE_T) $(LIBS) $(MYLIBS) +llex.o: + $(CC) $(CFLAGS) -Os -c llex.c + +lparser.o: + $(CC) $(CFLAGS) -Os -c lparser.c + +lcode.o: + $(CC) $(CFLAGS) -Os -c lcode.c + + clean: $(RM) $(ALL_T) $(ALL_O) From 6a84c329005ab7fc3f17283feda3f41010728288 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 8 Oct 2019 10:29:38 -0300 Subject: [PATCH 0505/1145] No coercion string->number in arithmetic with LUA_NOCVTS2N --- lstrlib.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/lstrlib.c b/lstrlib.c index 7f4a01849b..48acb8bfcf 100644 --- a/lstrlib.c +++ b/lstrlib.c @@ -233,6 +233,17 @@ static int str_dump (lua_State *L) { ** ======================================================= */ +#if defined(LUA_NOCVTS2N) /* { */ + +/* no coercion from strings to numbers */ + +static const luaL_Reg stringmetamethods[] = { + {"__index", NULL}, /* placeholder */ + {NULL, NULL} +}; + +#else /* }{ */ + static int tonum (lua_State *L, int arg) { if (lua_type(L, arg) == LUA_TNUMBER) { /* already a number? */ lua_pushvalue(L, arg); @@ -311,6 +322,8 @@ static const luaL_Reg stringmetamethods[] = { {NULL, NULL} }; +#endif /* } */ + /* }====================================================== */ /* From 6c0e44464b9eef4be42e2c8181aabfb3301617ad Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 8 Oct 2019 10:34:43 -0300 Subject: [PATCH 0506/1145] Improvements in the manual around metamethods --- manual/manual.of | 77 ++++++++++++++++++++++++++---------------------- 1 file changed, 42 insertions(+), 35 deletions(-) diff --git a/manual/manual.of b/manual/manual.of index cca4ca08a6..df7b74b466 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -295,9 +295,9 @@ although this behavior can be adapted from C @seeC{lua_setwarnf}. Every value in Lua can have a @emph{metatable}. This @def{metatable} is an ordinary Lua table that defines the behavior of the original value -under certain special operations. +under certain events. You can change several aspects of the behavior -of operations over a value by setting specific fields in its metatable. +of a value by setting specific fields in its metatable. For instance, when a non-numeric value is the operand of an addition, Lua checks for a function in the field @St{__add} of the value's metatable. If it finds one, @@ -306,7 +306,7 @@ Lua calls this function to perform the addition. The key for each event in a metatable is a string with the event name prefixed by two underscores; the corresponding values are called @def{metamethods}. -In the previous example, the key is @St{__add} +In the previous example, the key is the string @St{__add} and the metamethod is the function that performs the addition. Unless stated otherwise, metamethods should be function values. @@ -328,22 +328,10 @@ one for all strings, etc. By default, a value has no metatable, but the string library sets a metatable for the string type @see{strlib}. -A metatable controls how an object behaves in -arithmetic operations, bitwise operations, -order comparisons, concatenation, length operation, calls, and indexing. -A metatable also can define a function to be called -when a userdata or a table is @link{GC|garbage collected}. - -For the unary operators (negation, length, and bitwise NOT), -the metamethod is computed and called with a dummy second operand, -equal to the first one. -This extra operand is only to simplify Lua's internals -(by making these operators behave like a binary operation) -and may be removed in future versions. -(For most uses this extra operand is irrelevant.) - -A detailed list of events controlled by metatables is given next. -Each operation is identified by its corresponding key. +A detailed list of operations controlled by metatables is given next. +Each event is identified by its corresponding key. +By convention, all metatable keys used by Lua are composed by +two underscores followed by lowercase Latin letters. @description{ @@ -351,16 +339,16 @@ Each operation is identified by its corresponding key. the addition (@T{+}) operation. If any operand for an addition is not a number, Lua will try to call a metamethod. -First, Lua will check the first operand (even if it is valid). -If that operand does not define a metamethod for @idx{__add}, +It starts by checking the first operand (even if it is a number); +if that operand does not define a metamethod for @idx{__add}, then Lua will check the second operand. If Lua can find a metamethod, it calls the metamethod with the two operands as arguments, and the result of the call (adjusted to one value) is the result of the operation. -Otherwise, -it raises an error. +Otherwise, if no metamethod is found, +Lua raises an error. } @item{@idx{__sub}| @@ -467,7 +455,7 @@ the less than (@T{<}) operation. Behavior similar to the addition operation, except that Lua will try a metamethod only when the values being compared are neither both numbers nor both strings. -The result of the call is always converted to a boolean. +Moreover, the result of the call is always converted to a boolean. } @item{@idx{__le}| @@ -512,9 +500,9 @@ and therefore can trigger another metamethod. Whenever there is a @idx{__newindex} metamethod, Lua does not perform the primitive assignment. -(If necessary, +If needed, the metamethod itself can call @Lid{rawset} -to do the assignment.) +to do the assignment. } @item{@idx{__call}| @@ -526,16 +514,29 @@ If present, the metamethod is called with @id{func} as its first argument, followed by the arguments of the original call (@id{args}). All results of the call -are the result of the operation. +are the results of the operation. This is the only metamethod that allows multiple results. } } -It is a good practice to add all needed metamethods to a table -before setting it as a metatable of some object. -In particular, the @idx{__gc} metamethod works only when this order -is followed @see{finalizers}. +In addition to the previous list, +the interpreter also respects the following keys in metatables: +@idx{__gc} @see{finalizers}, +@idx{__close} @see{to-be-closed}, +@idx{__mode} @see{weak-table}, +and @idx{__name}. +(The entry @idx{__name}, +when it contains a string, +is used by some error-reporting functions to build error messages.) + +For the unary operators (negation, length, and bitwise NOT), +the metamethod is computed and called with a dummy second operand, +equal to the first one. +This extra operand is only to simplify Lua's internals +(by making these operators behave like a binary operation) +and may be removed in future versions. +For most uses this extra operand is irrelevant. Because metatables are regular tables, they can contain arbitrary fields, @@ -544,6 +545,13 @@ Some functions in the standard library (e.g., @Lid{tostring}) use other fields in metatables for their own purposes. +It is a good practice to add all needed metamethods to a table +before setting it as a metatable of some object. +In particular, the @idx{__gc} metamethod works only when this order +is followed @see{finalizers}. +It is also a good practice to set the metatable of an object +right after its creation. + } @sect2{GC| @title{Garbage Collection} @@ -1012,7 +1020,7 @@ it must be expressed using exactly three digits.) The @x{UTF-8} encoding of a @x{Unicode} character can be inserted in a literal string with the escape sequence @T{\u{@rep{XXX}}} -(with mandatory enclosing brackets), +(with mandatory enclosing braces), where @rep{XXX} is a sequence of one or more hexadecimal digits representing the character code point. This code point can be any value less than @M{2@sp{31}}. @@ -5536,7 +5544,6 @@ creates a new table to be used as a metatable for userdata, adds to this new table the pair @T{__name = tname}, adds to the registry the pair @T{[tname] = new table}, and returns 1. -(The entry @idx{__name} is used by some error-reporting functions.) In both cases, the function pushes onto the stack the final value associated @@ -5911,7 +5918,7 @@ The notation @fail means a return value representing some kind of failure or the absence of a better value to return. Currently, @fail is equal to @nil, but that may change in future versions. -The recommendation is to test the success of these functions +The recommendation is to always test the success of these functions with @T{(not status)}, instead of @T{(status == nil)}. @@ -8587,7 +8594,7 @@ This function has the following restrictions: @item{@id{limit} cannot be less than the amount of C stack in use.} } If a call does not respect some restriction, -it returns @false. +it returns a falsy value. Otherwise, the call returns the old limit. From 6055a039b57b405adc268672caeb682ef2a551ee Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 17 Oct 2019 13:02:07 -0300 Subject: [PATCH 0507/1145] Easy redefinition of valid flags for 'string.format' --- lstrlib.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/lstrlib.c b/lstrlib.c index 48acb8bfcf..a5f60b63d0 100644 --- a/lstrlib.c +++ b/lstrlib.c @@ -1071,7 +1071,10 @@ static int lua_number2strx (lua_State *L, char *buff, int sz, /* valid flags in a format specification */ -#define FLAGS "-+ #0" +#if !defined(L_FMTFLAGS) +#define L_FMTFLAGS "-+ #0" +#endif + /* ** maximum size of each format specification (such as "%-099.99d") @@ -1169,8 +1172,8 @@ static void addliteral (lua_State *L, luaL_Buffer *b, int arg) { static const char *scanformat (lua_State *L, const char *strfrmt, char *form) { const char *p = strfrmt; - while (*p != '\0' && strchr(FLAGS, *p) != NULL) p++; /* skip flags */ - if ((size_t)(p - strfrmt) >= sizeof(FLAGS)/sizeof(char)) + while (*p != '\0' && strchr(L_FMTFLAGS, *p) != NULL) p++; /* skip flags */ + if ((size_t)(p - strfrmt) >= sizeof(L_FMTFLAGS)/sizeof(char)) luaL_error(L, "invalid format (repeated flags)"); if (isdigit(uchar(*p))) p++; /* skip width */ if (isdigit(uchar(*p))) p++; /* (2 digits at most) */ From 6e1aec7a677a9891f2f8ca57e039d9984fdc69bc Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 17 Oct 2019 13:09:17 -0300 Subject: [PATCH 0508/1145] Larger C-stack limits for new threads New threads were being created with very small C-stack limits. This is not a problem for coroutines, because 'lua_resume' sets a new limit, but not all threads are coroutines. --- lstate.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lstate.c b/lstate.c index 1c33dcfcf1..4864a97992 100644 --- a/lstate.c +++ b/lstate.c @@ -286,7 +286,6 @@ static void preinit_thread (lua_State *L, global_State *g) { L->stacksize = 0; L->twups = L; /* thread has no upvalues */ L->errorJmp = NULL; - L->nCcalls = CSTACKTHREAD; L->hook = NULL; L->hookmask = 0; L->basehookcount = 0; @@ -327,6 +326,7 @@ LUA_API lua_State *lua_newthread (lua_State *L) { setthvalue2s(L, L->top, L1); api_incr_top(L); preinit_thread(L1, g); + L1->nCcalls = getCcalls(L); L1->hookmask = L->hookmask; L1->basehookcount = L->basehookcount; L1->hook = L->hook; From e592f94a643de0bf8d62f6c6128fe752c673f5ac Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 22 Oct 2019 14:08:22 -0300 Subject: [PATCH 0509/1145] Details (mostly comments) --- lgc.c | 1 + lobject.h | 31 +++++++++++++++++++++++-------- ltable.c | 3 +++ ltests.c | 2 +- 4 files changed, 28 insertions(+), 9 deletions(-) diff --git a/lgc.c b/lgc.c index cf62a45c0f..db519c6dfd 100644 --- a/lgc.c +++ b/lgc.c @@ -1565,6 +1565,7 @@ static void incstep (lua_State *L, global_State *g) { */ void luaC_step (lua_State *L) { global_State *g = G(L); + lua_assert(!g->gcemergency); if (g->gcrunning) { /* running? */ if(isdecGCmodegen(g)) genstep(L, g); diff --git a/lobject.h b/lobject.h index a22148c087..0c38affee7 100644 --- a/lobject.h +++ b/lobject.h @@ -17,11 +17,12 @@ /* -** Extra tags for non-values +** Extra tags for collectable non-values */ #define LUA_TUPVAL LUA_NUMTAGS /* upvalues */ #define LUA_TPROTO (LUA_NUMTAGS+1) /* function prototypes */ + /* ** number of all possible tags (including LUA_TNONE) */ @@ -30,7 +31,7 @@ /* ** tags for Tagged Values have the following use of bits: -** bits 0-3: actual tag (a LUA_T* value) +** bits 0-3: actual tag (a LUA_T* constant) ** bits 4-5: variant bits ** bit 6: whether value is collectable */ @@ -86,24 +87,35 @@ typedef struct TValue { /* Macros for internal tests */ + +/* collectable object has the same tag as the original value */ #define righttt(obj) (ttypetag(obj) == gcvalue(obj)->tt) +/* +** Any value being manipulated by the program either is non +** collectable, or the collectable object has the right tag +** and it is not dead. +*/ #define checkliveness(L,obj) \ ((void)L, lua_longassert(!iscollectable(obj) || \ (righttt(obj) && (L == NULL || !isdead(G(L),gcvalue(obj)))))) /* Macros to set values */ + +/* set a value's tag */ #define settt_(o,t) ((o)->tt_=(t)) +/* main macro to copy values (from 'obj1' to 'obj2') */ #define setobj(L,obj1,obj2) \ { TValue *io1=(obj1); const TValue *io2=(obj2); \ - io1->value_ = io2->value_; io1->tt_ = io2->tt_; \ - checkliveness(L,io1); lua_assert(!isreallyempty(io1)); } + io1->value_ = io2->value_; settt_(io1, io2->tt_); \ + checkliveness(L,io1); lua_assert(!isnonstrictnil(io1)); } /* -** different types of assignments, according to destination +** Different types of assignments, according to source and destination. +** (They are mostly equal now, but may be different in the future.) */ /* from stack to stack */ @@ -118,13 +130,16 @@ typedef struct TValue { #define setobj2t setobj - +/* +** Entries in the Lua stack +*/ typedef union StackValue { TValue val; } StackValue; -typedef StackValue *StkId; /* index to stack elements */ +/* index to stack elements */ +typedef StackValue *StkId; /* convert a 'StackValue' to a 'TValue' */ #define s2v(o) (&(o)->val) @@ -166,7 +181,7 @@ typedef StackValue *StkId; /* index to stack elements */ /* ** macro to detect non-standard nils (used only in assertions) */ -#define isreallyempty(v) (ttisnil(v) && !ttisstrictnil(v)) +#define isnonstrictnil(v) (ttisnil(v) && !ttisstrictnil(v)) /* diff --git a/ltable.c b/ltable.c index 5561d45ebc..4c7ae994c0 100644 --- a/ltable.c +++ b/ltable.c @@ -155,6 +155,9 @@ static Node *mainposition (const Table *t, int ktt, const Value *kvl) { } +/* +** Returns the main position of an element given as a 'TValue' +*/ static Node *mainpositionTV (const Table *t, const TValue *key) { return mainposition(t, rawtt(key), valraw(key)); } diff --git a/ltests.c b/ltests.c index 0d4ec938e1..bb5dad5475 100644 --- a/ltests.c +++ b/ltests.c @@ -80,7 +80,7 @@ static int tpanic (lua_State *L) { /* -** Warning function for tests. Fist, it concatenates all parts of +** Warning function for tests. First, it concatenates all parts of ** a warning in buffer 'buff'. Then, it has three modes: ** - 0.normal: messages starting with '#' are shown on standard output; ** - other messages abort the tests (they represent real warning From b8cdea01908f8d436e9d5a73d640645ea52d5f65 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 22 Oct 2019 14:10:54 -0300 Subject: [PATCH 0510/1145] Changed definition of macro 'l_isfalse' The old definition did one test for nil, but three tests for the all too common booleans (and two tests for other values); this definition does two tests for all values. --- lobject.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lobject.h b/lobject.h index 0c38affee7..7d30b46f98 100644 --- a/lobject.h +++ b/lobject.h @@ -216,7 +216,7 @@ typedef StackValue *StkId; #define bvalueraw(v) ((v).b) -#define l_isfalse(o) (ttisnil(o) || (ttisboolean(o) && bvalue(o) == 0)) +#define l_isfalse(o) (ttisboolean(o) ? bvalue(o) == 0 : ttisnil(o)) #define setbvalue(obj,x) \ { TValue *io=(obj); val_(io).b=(x); settt_(io, LUA_TBOOLEAN); } From 6e285e539274920830eeec5cd2dde5eeca5863e4 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 23 Oct 2019 10:31:02 -0300 Subject: [PATCH 0511/1145] More pious implementation of 'string.dump' In 'str__dump', the call to 'lua_dump' assumes the function is on the top of the stack, but the manual allows 'luaL_buffinit' to push stuff on the stack (although the current implementation does not). So, the call to 'luaL_buffinit' must come after the call to 'lua_dump'. --- lstrlib.c | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/lstrlib.c b/lstrlib.c index a5f60b63d0..946461a8cc 100644 --- a/lstrlib.c +++ b/lstrlib.c @@ -206,22 +206,38 @@ static int str_char (lua_State *L) { } -static int writer (lua_State *L, const void *b, size_t size, void *B) { - (void)L; - luaL_addlstring((luaL_Buffer *) B, (const char *)b, size); +/* +** Buffer to store the result of 'string.dump'. It must be initialized +** after the call to 'lua_dump', to ensure that the function is on the +** top of the stack when 'lua_dump' is called. ('luaL_buffinit' might +** push stuff.) +*/ +struct str_Writer { + int init; /* true iff buffer has been initialized */ + luaL_Buffer B; +}; + + +static int writer (lua_State *L, const void *b, size_t size, void *ud) { + struct str_Writer *state = (struct str_Writer *)ud; + if (!state->init) { + state->init = 1; + luaL_buffinit(L, &state->B); + } + luaL_addlstring(&state->B, (const char *)b, size); return 0; } static int str_dump (lua_State *L) { - luaL_Buffer b; + struct str_Writer state; int strip = lua_toboolean(L, 2); luaL_checktype(L, 1, LUA_TFUNCTION); - lua_settop(L, 1); - luaL_buffinit(L,&b); - if (lua_dump(L, writer, &b, strip) != 0) + lua_settop(L, 1); /* ensure function is on the top of the stack */ + state.init = 0; + if (lua_dump(L, writer, &state, strip) != 0) return luaL_error(L, "unable to dump given function"); - luaL_pushresult(&b); + luaL_pushresult(&state.B); return 1; } From 4c32d9300c77a70b7c20b2eebda40e97541e3f01 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 23 Oct 2019 10:41:47 -0300 Subject: [PATCH 0512/1145] Several enhancements in the manual --- manual/manual.of | 97 +++++++++++++++++++++++++++--------------------- 1 file changed, 54 insertions(+), 43 deletions(-) diff --git a/manual/manual.of b/manual/manual.of index df7b74b466..3d79e7e247 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -82,7 +82,8 @@ whose main property is to be different from any other value; it often represents the absence of a useful value. The type @emph{boolean} has two values, @false and @true. Both @nil and @false make a condition false; -any other value makes it true. +they are collectively called @def{false values}. +Any other value makes a condition true. The type @emph{number} represents both integer numbers and real (floating-point) numbers, @@ -278,9 +279,9 @@ so, an error inside the message handler will call the message handler again. If this loop goes on for too long, Lua breaks it and returns an appropriate message. -(The message handler is called only for regular runtime errors. +The message handler is called only for regular runtime errors. It is not called for memory-allocation errors -nor for errors while running finalizers.) +nor for errors while running finalizers or other message handlers. Lua also offers a system of @emph{warnings} @seeF{warn}. Unlike errors, warnings do not interfere @@ -467,7 +468,7 @@ Behavior similar to the less than operation. The indexing access operation @T{table[key]}. This event happens when @id{table} is not a table or when @id{key} is not present in @id{table}. -The metamethod is looked up in @id{table}. +The metamethod is looked up in the metatable of @id{table}. Despite the name, the metamethod for this event can be either a function or a table. @@ -528,7 +529,7 @@ the interpreter also respects the following keys in metatables: and @idx{__name}. (The entry @idx{__name}, when it contains a string, -is used by some error-reporting functions to build error messages.) +may be used by @Lid{tostring} and in error messages.) For the unary operators (negation, length, and bitwise NOT), the metamethod is computed and called with a dummy second operand, @@ -638,7 +639,7 @@ In generational mode, the collector does frequent @emph{minor} collections, which traverses only objects recently created. If after a minor collection the use of memory is still above a limit, -the collector does a @emph{major} collection, +the collector does a stop-the-world @emph{major} collection, which traverses all objects. The generational mode uses two parameters: the @def{minor multiplier} and the @def{the major multiplier}. @@ -943,7 +944,7 @@ Lua is a @x{free-form} language. It ignores spaces and comments between lexical elements (@x{tokens}), except as delimiters between two tokens. In source code, -Lua recognizes as spaces the standard ASCII white-space +Lua recognizes as spaces the standard ASCII whitespace characters space, form feed, newline, carriage return, horizontal tab, and vertical tab. @@ -998,7 +999,7 @@ and @Char{\'} (apostrophe [single quote]). A backslash followed by a line break results in a newline in the string. The escape sequence @Char{\z} skips the following span -of white-space characters, +of whitespace characters, including line breaks; it is particularly useful to break and indent a long literal string into multiple lines without adding the newlines and spaces @@ -1769,7 +1770,7 @@ Otherwise, the conversion fails. Several places in Lua coerce strings to numbers when necessary. A string is converted to an integer or a float following its syntax and the rules of the Lua lexer. -(The string may have also leading and trailing spaces and a sign.) +(The string may have also leading and trailing whitespaces and a sign.) All conversions from strings to numbers accept both a dot and the current locale mark as the radix character. @@ -2182,7 +2183,7 @@ function r() return 1,2,3 end Then, we have the following mapping from arguments to parameters and to the vararg expression: @verbatim{ -CALL PARAMETERS +CALL PARAMETERS f(3) a=3, b=nil f(3, 4) a=3, b=4 @@ -2802,9 +2803,13 @@ Sets a new panic function and returns the old one @see{C-error}. @apii{nargs+1,nresults,e} Calls a function. +Like regular Lua calls, +@id{lua_call} respects the @idx{__call} metamethod. +So, here the word @Q{function} +means any callable value. To do a call you must use the following protocol: -first, the value to be called is pushed onto the stack; +first, the function to be called is pushed onto the stack; then, the arguments to the call are pushed in direct order; that is, the first argument is pushed first. @@ -2812,7 +2817,7 @@ Finally you call @Lid{lua_call}; @id{nargs} is the number of arguments that you pushed onto the stack. When the function returns, all arguments and the function value are popped -and the function results are pushed onto the stack. +and the call results are pushed onto the stack. The number of results is adjusted to @id{nresults}, unless @id{nresults} is @defid{LUA_MULTRET}. In this case, all results from the function are pushed; @@ -2824,8 +2829,6 @@ so that after the call the last result is on the top of the stack. Any error while calling and running the function is propagated upwards (with a @id{longjmp}). -Like regular Lua calls, -this function respects the @idx{__call} metamethod. The following example shows how the host program can do the equivalent to this Lua code: @@ -3971,7 +3974,8 @@ leaves the error object on the top of the stack, Starts and resumes a coroutine in the given thread @id{L}. To start a coroutine, -you push onto the thread stack the main function plus any arguments; +you push the main function plus any arguments +onto the empty stack of the thread. then you call @Lid{lua_resume}, with @id{nargs} being the number of arguments. This call returns when the coroutine suspends or finishes its execution. @@ -3988,8 +3992,9 @@ or an error code in case of errors @seeC{lua_pcall}. In case of errors, the error object is on the top of the stack. -To resume a coroutine, you clear its stack, -push only the values to be passed as results from @id{yield}, +To resume a coroutine, +you remove the @id{*nresults} yielded values from its stack, +push the values to be passed as results from @id{yield}, and then call @Lid{lua_resume}. The parameter @id{from} represents the coroutine that is resuming @id{L}. @@ -4152,7 +4157,7 @@ and returns the total size of the string, that is, its length plus one. The conversion can result in an integer or a float, according to the lexical conventions of Lua @see{lexical}. -The string may have leading and trailing spaces and a sign. +The string may have leading and trailing whitespaces and a sign. If the string is not a valid numeral, returns 0 and pushes nothing. (Note that the result can be used as a boolean, @@ -5791,7 +5796,7 @@ The field @id{closef} points to a Lua function that will be called to close the stream when the handle is closed or collected; this function receives the file handle as its sole argument and -must return either @true, in case of success, +must return either a true value, in case of success, or a false value plus an error message, in case of error. Once Lua calls this field, it changes the field value to @id{NULL} @@ -5914,12 +5919,12 @@ to its expected parameters. For instance, a function documented as @T{foo(arg)} should not be called without an argument. -The notation @fail means a return value representing -some kind of failure or the absence of a better value to return. -Currently, @fail is equal to @nil, +The notation @fail means a false value representing +some kind of failure. +(Currently, @fail is equal to @nil, but that may change in future versions. The recommendation is to always test the success of these functions -with @T{(not status)}, instead of @T{(status == nil)}. +with @T{(not status)}, instead of @T{(status == nil)}.) Currently, Lua has the following standard libraries: @@ -6338,19 +6343,25 @@ the function returns @fail. } @LibEntry{tostring (v)| + Receives a value of any type and converts it to a string in a human-readable format. -(For complete control of how numbers are converted, -use @Lid{string.format}.) If the metatable of @id{v} has a @idx{__tostring} field, then @id{tostring} calls the corresponding value with @id{v} as argument, and uses the result of the call as its result. +Otherwise, if the metatable of @id{v} has a @idx{__name} field +with a string value, +@id{tostring} may use that string in its final result. + +For complete control of how numbers are converted, +use @Lid{string.format}. } @LibEntry{type (v)| + Returns the type of its only argument, coded as a string. The possible results of this function are @St{nil} (a string, not the value @nil), @@ -6599,7 +6610,8 @@ Default is @Char{-}.} @LibEntry{package.cpath| -The path used by @Lid{require} to search for a @N{C loader}. +A string with the path used by @Lid{require} +to search for a @N{C loader}. Lua initializes the @N{C path} @Lid{package.cpath} in the same way it initializes the Lua path @Lid{package.path}, @@ -6656,7 +6668,8 @@ plus other Unix systems that support the @id{dlfcn} standard). @LibEntry{package.path| -The path used by @Lid{require} to search for a Lua loader. +A string with the path used by @Lid{require} +to search for a Lua loader. At start-up, Lua initializes this variable with the value of the environment variable @defid{LUA_PATH_5_4} or @@ -7397,7 +7410,7 @@ coded as an unsigned integer with @id{n} bytes @item{@T{X@rep{op}}|an empty item that aligns according to option @id{op} (which is otherwise ignored)} -@item{@Char{ }|(empty space) ignored} +@item{@Char{ }|(space) ignored} } (A @St{[@rep{n}]} means an optional integral numeral.) Except for padding, spaces, and configurations @@ -8106,7 +8119,7 @@ The available formats are @item{@St{n}| reads a numeral and returns it as a float or an integer, following the lexical conventions of Lua. -(The numeral may have leading spaces and a sign.) +(The numeral may have leading whitespaces and a sign.) This format always reads the longest input sequence that is a valid prefix for a numeral; if that prefix does not form a valid numeral @@ -8594,7 +8607,7 @@ This function has the following restrictions: @item{@id{limit} cannot be less than the amount of C stack in use.} } If a call does not respect some restriction, -it returns a falsy value. +it returns a false value. Otherwise, the call returns the old limit. @@ -8736,15 +8749,15 @@ lua [options] [script [args]] } The options are: @description{ -@item{@T{-e @rep{stat}}| executes string @rep{stat};} -@item{@T{-l @rep{mod}}| @Q{requires} @rep{mod} and assigns the +@item{@T{-e @rep{stat}}| execute string @rep{stat};} +@item{@T{-i}| enter interactive mode after running @rep{script};} +@item{@T{-l @rep{mod}}| @Q{require} @rep{mod} and assign the result to global @rep{mod};} -@item{@T{-i}| enters interactive mode after running @rep{script};} -@item{@T{-v}| prints version information;} -@item{@T{-E}| ignores environment variables;} +@item{@T{-v}| print version information;} +@item{@T{-E}| ignore environment variables;} @item{@T{-W}| turn warnings on;} -@item{@T{--}| stops handling options;} -@item{@T{-}| executes @id{stdin} as a file and stops handling options.} +@item{@T{--}| stop handling options;} +@item{@T{-}| execute @id{stdin} as a file and stop handling options.} } After handling its options, @id{lua} runs the given @emph{script}. When called without arguments, @@ -8761,12 +8774,10 @@ then @id{lua} executes the file. Otherwise, @id{lua} executes the string itself. When called with the option @T{-E}, -besides ignoring @id{LUA_INIT}, -Lua also ignores -the values of @id{LUA_PATH} and @id{LUA_CPATH}, -setting the values of -@Lid{package.path} and @Lid{package.cpath} -with the default paths defined in @id{luaconf.h}. +Lua does not consult any environment variables. +In particular, +the values of @Lid{package.path} and @Lid{package.cpath} +are set with the default paths defined in @id{luaconf.h}. The options @T{-e}, @T{-l}, and @T{-W} are handled in the order they appear. From b93f3b00bb76cddbf600eb399849fb0c01d197fd Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 23 Oct 2019 11:10:19 -0300 Subject: [PATCH 0513/1145] Added function 'luaL_buffsub' --- lauxlib.h | 2 ++ manual/manual.of | 26 +++++++++++++++++--------- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/lauxlib.h b/lauxlib.h index c50cdf4d03..b34b3805c4 100644 --- a/lauxlib.h +++ b/lauxlib.h @@ -185,6 +185,8 @@ struct luaL_Buffer { #define luaL_addsize(B,s) ((B)->n += (s)) +#define luaL_buffsub(B,s) ((B)->n -= (s)) + LUALIB_API void (luaL_buffinit) (lua_State *L, luaL_Buffer *B); LUALIB_API char *(luaL_prepbuffsize) (luaL_Buffer *B, size_t sz); LUALIB_API void (luaL_addlstring) (luaL_Buffer *B, const char *s, size_t l); diff --git a/manual/manual.of b/manual/manual.of index 3d79e7e247..00ab4cd519 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -5005,10 +5005,9 @@ const void luaL_addgsub (luaL_Buffer *B, const char *s, const char *p, const char *r);| @apii{0,0,m} -Adds a copy of the string @id{s} to the buffer @id{B}, +Adds a copy of the string @id{s} to the buffer @id{B} @seeC{luaL_Buffer}, replacing any occurrence of the string @id{p} with the string @id{r}. -@seeC{luaL_Buffer}. } @@ -5025,7 +5024,7 @@ The string can contain @x{embedded zeros}. @APIEntry{void luaL_addsize (luaL_Buffer *B, size_t n);| @apii{?,?,-} -Adds to the buffer @id{B} @seeC{luaL_Buffer} +Adds to the buffer @id{B} a string of length @id{n} previously copied to the buffer area @seeC{luaL_prepbuffer}. @@ -5157,26 +5156,26 @@ plus the final string on its top. @APIEntry{char *luaL_buffaddr (luaL_Buffer *B);| @apii{0,0,-} -Returns the address of the current contents of buffer @id{B}. -Note that any addition to the buffer may invalidate this address. +Returns the address of the current contents of buffer @id{B} @seeC{luaL_Buffer}. +Note that any addition to the buffer may invalidate this address. } @APIEntry{void luaL_buffinit (lua_State *L, luaL_Buffer *B);| @apii{0,0,-} -Initializes a buffer @id{B}. -This function does not allocate any space; -the buffer must be declared as a variable +Initializes a buffer @id{B} @seeC{luaL_Buffer}. +This function does not allocate any space; +the buffer must be declared as a variable. } @APIEntry{size_t luaL_bufflen (luaL_Buffer *B);| @apii{0,0,-} -Returns the length of the current contents of buffer @id{B}. +Returns the length of the current contents of buffer @id{B} @seeC{luaL_Buffer}. } @@ -5189,6 +5188,15 @@ Equivalent to the sequence } +@APIEntry{void luaL_buffsub (luaL_Buffer *B, int n);| +@apii{0,0,-} + +Removes @id{n} bytes from the the buffer @id{B} +@seeC{luaL_Buffer}. +The buffer must have at least that many bytes. + +} + @APIEntry{int luaL_callmeta (lua_State *L, int obj, const char *e);| @apii{0,0|1,e} From ba9cd0d25a022cf61c0b747d8e26f9ba81201112 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 24 Oct 2019 10:49:44 -0300 Subject: [PATCH 0514/1145] Change in the prefix of messages from searchers The initial "\n\t" to properly indent a searcher message is being added by 'findloader' when building the error message, instead of being included in the original message by each searcher itself. --- loadlib.c | 14 +++++++++----- testes/attrib.lua | 23 +++++++++++++++++++++++ 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/loadlib.c b/loadlib.c index 689767f3a3..56167f64e0 100644 --- a/loadlib.c +++ b/loadlib.c @@ -458,13 +458,13 @@ static const char *getnextfilename (char **path, char *end) { /* ** Given a path such as ";blabla.so;blublu.so", pushes the string ** -** no file 'blabla.so' +** no file 'blabla.so' ** no file 'blublu.so' */ static void pusherrornotfound (lua_State *L, const char *path) { luaL_Buffer b; luaL_buffinit(L, &b); - luaL_addstring(&b, "\n\tno file '"); + luaL_addstring(&b, "no file '"); luaL_addgsub(&b, path, LUA_PATH_SEP, "'\n\tno file '"); luaL_addstring(&b, "'"); luaL_pushresult(&b); @@ -591,7 +591,7 @@ static int searcher_Croot (lua_State *L) { if (stat != ERRFUNC) return checkload(L, 0, filename); /* real error */ else { /* open function not found */ - lua_pushfstring(L, "\n\tno module '%s' in file '%s'", name, filename); + lua_pushfstring(L, "no module '%s' in file '%s'", name, filename); return 1; } } @@ -604,7 +604,7 @@ static int searcher_preload (lua_State *L) { const char *name = luaL_checkstring(L, 1); lua_getfield(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE); if (lua_getfield(L, -1, name) == LUA_TNIL) { /* not found? */ - lua_pushfstring(L, "\n\tno field package.preload['%s']", name); + lua_pushfstring(L, "no field package.preload['%s']", name); return 1; } else { @@ -623,8 +623,10 @@ static void findloader (lua_State *L, const char *name) { luaL_buffinit(L, &msg); /* iterate over available searchers to find a loader */ for (i = 1; ; i++) { + luaL_addstring(&msg, "\n\t"); /* error-message prefix */ if (lua_rawgeti(L, 3, i) == LUA_TNIL) { /* no more searchers? */ lua_pop(L, 1); /* remove nil */ + luaL_buffsub(&msg, 2); /* remove prefix */ luaL_pushresult(&msg); /* create error message */ luaL_error(L, "module '%s' not found:%s", name, lua_tostring(L, -1)); } @@ -636,8 +638,10 @@ static void findloader (lua_State *L, const char *name) { lua_pop(L, 1); /* remove extra return */ luaL_addvalue(&msg); /* concatenate error message */ } - else + else { /* no error message */ lua_pop(L, 2); /* remove both returns */ + luaL_buffsub(&msg, 2); /* remove prefix */ + } } } diff --git a/testes/attrib.lua b/testes/attrib.lua index b1a4a1994e..76a447c848 100644 --- a/testes/attrib.lua +++ b/testes/attrib.lua @@ -47,6 +47,29 @@ do package.path = oldpath end + +do print"testing 'require' message" + local oldpath = package.path + local oldcpath = package.cpath + + package.path = "?.lua;?/?" + package.cpath = "?.so;?/init" + + local st, msg = pcall(require, 'XXX') + + local expected = [[module 'XXX' not found: + no field package.preload['XXX'] + no file 'XXX.lua' + no file 'XXX/XXX' + no file 'XXX.so' + no file 'XXX/init']] + + assert(msg == expected) + + package.path = oldpath + package.cpath = oldcpath +end + print('+') From c12983cf8afac4c4c757b84aaddab1935a931641 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 25 Oct 2019 17:41:40 -0300 Subject: [PATCH 0515/1145] Fixed warnings from Keil compiler --- lcode.c | 2 +- lvm.c | 9 +++------ 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/lcode.c b/lcode.c index 3e4c5b497e..2432b34610 100644 --- a/lcode.c +++ b/lcode.c @@ -110,7 +110,7 @@ int luaK_exp2const (FuncState *fs, const expdesc *e, TValue *v) { ** optimizations). */ static Instruction *previousinstruction (FuncState *fs) { - static const Instruction invalidinstruction = -1; + static const Instruction invalidinstruction = ~(Instruction)0; if (fs->pc > fs->lasttarget) return &fs->f->code[fs->pc - 1]; /* previous instruction */ else diff --git a/lvm.c b/lvm.c index e22a0da806..5407d14488 100644 --- a/lvm.c +++ b/lvm.c @@ -1561,12 +1561,9 @@ void luaV_execute (lua_State *L, CallInfo *ci) { luaD_poscall(L, ci, cast_int(L->top - ra)); return; } - else { /* Lua tail call */ - ci->func -= delta; - luaD_pretailcall(L, ci, ra, b); /* prepare call frame */ - goto tailcall; - } - vmbreak; + ci->func -= delta; + luaD_pretailcall(L, ci, ra, b); /* prepare call frame */ + goto tailcall; } vmcase(OP_RETURN) { int n = GETARG_B(i) - 1; /* number of results */ From 7d526e75a7f45a2593e874d97c7fdfa0e45cc013 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 28 Oct 2019 15:58:07 -0300 Subject: [PATCH 0516/1145] Fixed bug in tail calls of __call chains A tail call of a __call chain (a __call metamethod that itself is also not a function) was being perfomed as a regular call. --- lvm.c | 3 ++- testes/calls.lua | 25 ++++++++++++++++++++++++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/lvm.c b/lvm.c index 5407d14488..2c96c58d15 100644 --- a/lvm.c +++ b/lvm.c @@ -1549,9 +1549,10 @@ void luaV_execute (lua_State *L, CallInfo *ci) { luaF_close(L, base, NOCLOSINGMETH); lua_assert(base == ci->func + 1); } - if (!ttisfunction(s2v(ra))) { /* not a function? */ + while (!ttisfunction(s2v(ra))) { /* not a function? */ luaD_tryfuncTM(L, ra); /* try '__call' metamethod */ b++; /* there is now one extra argument */ + checkstackp(L, 1, ra); } if (!ttisLclosure(s2v(ra))) { /* C function? */ luaD_call(L, ra, LUA_MULTRET); /* call it */ diff --git a/testes/calls.lua b/testes/calls.lua index 739a624fb8..0141ffa4aa 100644 --- a/testes/calls.lua +++ b/testes/calls.lua @@ -107,7 +107,9 @@ end deep(10) deep(180) --- testing tail calls + +print"testing tail calls" + function deep (n) if n>0 then return deep(n-1) else return 101 end end assert(deep(30000) == 101) a = {} @@ -148,6 +150,27 @@ do -- tail calls x varargs assert(X == 10 and Y == 20 and #A == 1 and A[1] == 30) end + + +do -- tail calls x chain of __call + local n = 10000 -- depth + + local function foo () + if n == 0 then return 1023 + else n = n - 1; return foo() + end + end + + -- build a chain of __call metamethods ending in function 'foo' + for i = 1, 100 do + foo = setmetatable({}, {__call = foo}) + end + + -- call the first one as a tail call in a new coroutine + -- (to ensure stack is not preallocated) + assert(coroutine.wrap(function() return foo() end)() == 1023) +end + print('+') From bdcfae2e1c860e0726259a6195dbb5d6c2b1e23f Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 30 Oct 2019 16:46:11 -0300 Subject: [PATCH 0517/1145] File 'bugs' no longer tracked by git The file 'bugs' reports bugs in several different versions (corresponding to different branches in the repository), without a clear division of "this bugs belongs to this version". So, it doesn't make sense to track it along with one (or many) versions. --- bugs | 4052 ---------------------------------------------------------- 1 file changed, 4052 deletions(-) delete mode 100644 bugs diff --git a/bugs b/bugs deleted file mode 100644 index a965025b66..0000000000 --- a/bugs +++ /dev/null @@ -1,4052 +0,0 @@ ---[=[ -** lua.stx / llex.c -Tue Dec 2 10:45:48 EDT 1997 ->> BUG: "lastline" was not reset on function entry, so debug information ->> started only in the 2nd line of a function. - - - -================================================================= ---- Version 3.1 alpha - -** lua.c -Thu Jan 15 14:34:58 EDT 1998 ->> must include "stdlib.h" (for "exit()"). - -** lbuiltin.c / lobject.h -Thu Jan 15 14:34:58 EDT 1998 ->> MAX_WORD may be bigger than MAX_INT -(by lhf) - -** llex.c -Mon Jan 19 18:17:18 EDT 1998 ->> wrong line number (+1) in error report when file starts with "#..." - -** lstrlib.c -Tue Jan 27 15:27:49 EDT 1998 ->> formats like "%020d" were considered too big (3 digits); moreover, ->> some sistems limit printf to at most 500 chars, so we can limit sizes ->> to 2 digits (99). - -** lapi.c -Tue Jan 27 17:12:36 EDT 1998 ->> "lua_getstring" may create a new string, so should check GC - -** lstring.c / ltable.c -Wed Jan 28 14:48:12 EDT 1998 ->> tables can become full of "empty" slots, and keep growing without limits. - -** lstrlib.c -Mon Mar 9 15:26:09 EST 1998 ->> gsub('a', '(b?)%1*' ...) loops (because the capture is empty). - -** lstrlib.c -Mon May 18 19:20:00 EST 1998 ->> arguments for "format" 'x', 'X', 'o' and 'u' must be unsigned int. - - - -================================================================= ---- Version 3.1 - -** liolib.c / lauxlib.c -Mon Sep 7 15:57:02 EST 1998 ->> function "luaL_argerror" prints wrong argument number (from a user's point -of view) when functions have upvalues. - -** lstrlib.c -Tue Nov 10 17:29:36 EDT 1998 ->> gsub/strfind do not check whether captures are properly finished. -(by roberto/tomas) - -** lbuiltin.c -Fri Dec 18 11:22:55 EDT 1998 ->> "tonumber" goes crazy with negative numbers in other bases (not 10), -because "strtol" returns long, not unsigned long. -(by Visual C++) - -** lstrlib.c -Mon Jan 4 10:41:40 EDT 1999 ->> "format" does not check size of format item (such as "%00000...00000d"). - -** lapi.c -Wed Feb 3 14:40:21 EDT 1999 ->> getlocal cannot return the local itself, since lua_isstring and -lua_isnumber can modify it. - -** lstrlib.c -Thu Feb 4 17:08:50 EDT 1999 ->> format "%s" may break limit of "sprintf" on some machines. -(by Marcelo Sales) - -** lzio.c -Thu Mar 4 11:49:37 EST 1999 ->> file stream cannot call fread after EOF. -(by lhf) - - - -================================================================= ---- Version 3.2 (beta) - -** lstrlib.c -Fri Apr 30 11:10:20 EST 1999 ->> '$' at end of pattern was matching regular '$', too. -(by anna; since 2.5) - -** lbuiltin.c -Fri May 21 17:15:11 EST 1999 ->> foreach, foreachi, foreachvar points to function in stack when stack -can be reallocated. -(by tomas; since 3.2 beta) - -** lparser.c -Wed Jun 16 10:32:46 EST 1999 ->> cannot assign to unlimited variables, because it causes overflow in -the number of returns of a function. -(since 3.1) - - - -================================================================= ---- Version 3.2 - -** lmathlib.c -Wed Aug 18 11:28:38 EST 1999 ->> random(0) and random(x,0) are wrong (0 is read as no argument!). -(by Dave Bollinger; since 3.1) - -** lparser.c -Thu Sep 2 10:07:20 EST 1999 ->> in the (old) expression << ls->fs->f->consts[checkname(ls)] >>, checkname -could realloc f->consts. -(by Supratik Champati; since 3.2 beta) - -** lobject.c / lbuiltin.c -Wed Sep 8 17:41:54 EST 1999 ->> tonumber'e1' and tonumber(' ', x), for x!=10, gave 0 instead of nil. -(since 3.1) - -** lstrlib.c -Thu Nov 11 14:36:30 EDT 1999 ->> `strfind' does not handle \0 in plain search. -(by Jon Kleiser; since 3.1) - -** lparser.c -Wed Dec 29 16:05:43 EDT 1999 ->> return gives wrong line in debug information -(by lhf; since 3.2 [at least]) - -** ldo.c -Thu Dec 30 16:39:33 EDT 1999 ->> cannot reopen stdin (for binary mode) -(by lhf & roberto; since 3.1) - -** lapi.c -Thu Mar 2 09:41:53 EST 2000 ->> lua_settable should check stack space (it could call a T.M.) -(by lhf & celes; since 3.2; it was already fixed by fixed stack) - -** lparser.c -Mon Apr 3 09:59:06 EST 2000 ->> '%' should be in expfollow -(by Edgar Toernig; since 3.1; it was already fixed) - -** lbuiltin.c -Mon Apr 3 10:05:05 EST 2000 ->> tostring() without arguments gives seg. fault. -(by Edgar Toernig; since 3.0) - - - -================================================================= ---- Version 4.0 alpha - -Tested with full test suites (as locked in Mon Apr 24 14:23:11 EST 2000) -in the following platforms: -* Linux - gcc, g++ -* AIX - gcc -* Solaris - gcc, cc -* IRIX - cc, cc-purify -* Windows - Visual C++ (.c e .cpp, warning level=4) - - -** lstrlib.c -Tue May 2 15:27:58 EST 2000 ->> `strfind' gets wrong subject length when there is an offset -(by Jon Kleiser; since 4.0a) - -** lparser.c -Fri May 12 15:11:12 EST 2000 ->> first element in a list constructor is not adjusted to one value ->> (e.g. «a = {gsub('a','a','')}») -(by Tomas; since 4.0a) - -** lparser.c -Wed May 24 14:50:16 EST 2000 ->> record-constructor starting with an upvalue name gets an error ->> (e.g. «local a; function f() x = {a=1} end») -(by Edgar Toernig; since 3.1) - -** lparser.c -Tue Aug 29 15:56:05 EST 2000 ->> error message for `for' uses `while' -(since 4.0a; already corrected) - -** lgc.c -Tue Aug 29 15:57:41 EST 2000 ->> gc tag method for nil could call line hook -(by ry; since ?) - - - -================================================================= ---- Version 4.0 Beta - -** liolib.c -Fri Sep 22 15:12:37 EST 2000 ->> `read("*w")' should return nil at EOF -(by roberto; since 4.0b) - -** lvm.c -Mon Sep 25 11:47:48 EST 2000 ->> lua_gettable does not get key from stack top -(by Philip Yi; since 4.0b) - -** lgc.c -Mon Sep 25 11:50:48 EST 2000 ->> GC may crash when checking locked C closures -(by Philip Yi; since 4.0b) - -** lapi.c -Wed Sep 27 09:50:19 EST 2000 ->> lua_tag should return LUA_NOTAG for non-valid indices -(by Paul Hankin; since 4.0b) - -** llex.h / llex.c / lparser.c -Wed Sep 27 13:39:45 EST 2000 ->> parser overwrites semantic information when looking ahead ->> (e.g. «a = {print'foo'}») -(by Edgar Toernig; since 4.0b, deriving from previous bug) - -** liolib.c -Thu Oct 26 10:50:46 EDT 2000 ->> in function `read_file', realloc() doesn't free the buffer if it can't ->> allocate new memory -(by Mauro Vezzosi; since 4.0b) - - - -================================================================= ---- Version 4.0 - -** lparser.c -Wed Nov 29 09:51:44 EDT 2000 ->> parser does not accept a `;' after a `return' -(by lhf; since 4.0b) - -** liolib.c -Fri Dec 22 15:30:42 EDT 2000 ->> when `read' fails it must return nil (and not no value) -(by cassino; since at least 3.1) - -** lstring.c/lapi.c -Thu Feb 1 11:55:45 EDT 2001 ->> lua_pushuserdata(L, NULL) is buggy -(by Edgar Toernig; since 4.0) - -** ldo.c -Fri Feb 2 14:06:40 EDT 2001 ->> «while 1 dostring[[print('hello\n')]] end» never reclaims memory -(by Andrew Paton; since 4.0b) - -** lbaselib.c -Tue Feb 6 11:57:13 EDT 2001 ->> ESC (which starts precompiled code) in C is \33, not \27 -(by Edgar Toernig and lhf; since 4.0b) - -** lparser.c -Tue Jul 10 16:59:18 EST 2001 ->> error message for `%a' gave wrong line number -(by Leonardo Constantino; since 4.0) - -** lbaselib.c -Fri Dec 21 15:21:05 EDT 2001 ->> seg. fault when rawget/rawset get extra arguments -(by Eric Mauger; since 4.0b) - -** lvm.c -Wed Jun 19 13:28:20 EST 2002 ->> line hook gets wrong `ar' -(by Daniel C. Sinclair; since 4.0.b) - -** ldo.c -Wed Jun 19 13:31:49 EST 2002 ->> `protectedparser' may run GC, and then collect `filename' ->> (in function `parse_file') -(by Alex Bilyk; since 4.0) - - - - -================================================================= ---- Version 5.0 alpha - -** lgc.c -Fri Aug 30 13:49:14 EST 2002 ->> GC metamethod stored in a weak metatable being collected together with ->> userdata may not be cleared properly -(by Roberto; since 5.0a) - -** lapi.c -Thu Nov 21 11:00:00 EST 2002 ->> ULONG_MAX>>10 may not fit into an int -(by Jeff Petkau; since 4.0) - -** lparser.c -Fri Dec 6 17:06:40 UTC 2002 ->> scope of generic for variables is not sound -(by Gavin Wraith; since 5.0a) - - - - -================================================================= ---- Version 5.0 beta -** lbaselib.c -Fri Dec 20 09:53:19 UTC 2002 ->> `resume' was checking the wrong value for stack overflow -(by Maik Zimmermann; since 5.0b) - -** ldo.c -Thu Jan 23 11:29:06 UTC 2003 ->> error during garbage collection in luaD_protectedparser is not being ->> protected -(by Benoit Germain; since 5.0a) - -** ldo.c (and others) -Fri Feb 28 14:20:33 EST 2003 ->> GC metamethod calls could mess C/Lua stack syncronization -(by Roberto; since 5.0b) - -** lzio.h/zlio.c -Thu Mar 20 11:40:12 EST 2003 ->> zio mixes a 255 as first char in a buffer with EOZ -(by lhf; since 5.0a) - - - ---]=] ------------------------------------------------------------------ --- Lua 5.0 (final) - -Bug{ -what = [[lua_closethread exists only in the manual]], -report = [[by Nguyen Binh, 28/04/2003]], -patch = [[no patch; the manual is wrong]], -} - - -Bug{ -what = [[attempt to resume a running coroutine crashes Lua]], -example = [[ -function co_func (current_co) - coroutine.resume(co) -end -co = coroutine.create(co_func) -coroutine.resume(co) -coroutine.resume(co) --> seg. fault -]], -report = [[by Alex Bilyk, 09/05/2003]], -patch = [[ -* ldo.c: -325,326c325 -< if (nargs >= L->top - L->base) -< luaG_runerror(L, "cannot resume dead coroutine"); ---- -> lua_assert(nargs < L->top - L->base); -329c328,329 -< else if (ci->state & CI_YIELD) { /* inside a yield? */ ---- -> else { /* inside a yield */ -> lua_assert(ci->state & CI_YIELD); -344,345d343 -< else -< luaG_runerror(L, "cannot resume non-suspended coroutine"); -351a350,358 -> static int resume_error (lua_State *L, const char *msg) { -> L->top = L->ci->base; -> setsvalue2s(L->top, luaS_new(L, msg)); -> incr_top(L); -> lua_unlock(L); -> return LUA_ERRRUN; -> } -> -> -355a363,368 -> if (L->ci == L->base_ci) { -> if (nargs >= L->top - L->base) -> return resume_error(L, "cannot resume dead coroutine"); -> } -> else if (!(L->ci->state & CI_YIELD)) /* not inside a yield? */ -> return resume_error(L, "cannot resume non-suspended coroutine"); -]], -} - - -Bug{ -what = [[file:close cannot be called without a file. (results in seg fault)]], -example = [[ -> io.stdin.close() -- correct call shold be io.stdin:close() -]], -report = [[by Tuomo Valkonen, 27/05/2003]], -patch = [[ -* liolib.c: -161c161 -< if (lua_isnone(L, 1)) { ---- -> if (lua_isnone(L, 1) && lua_type(L, lua_upvalueindex(1)) == LUA_TTABLE) { -]], --}} -} - - -Bug{ -what = [[C functions also may have stacks larger than current top]], -example = [[ -Must recompile lua with a change in lua.c and with lua_assert defined: -* lua.c: -381a382 -> lua_checkstack(l, 1000); -]], -report = [[Alex Bilyk, 09/06/2003]], -patch = [[ -* lgc.c: -247c247 -< if (!(ci->state & CI_C) && lim < ci->top) ---- -> if (lim < ci->top) -]], -} - - -Bug{ -what = [[`pc' address is invalidated when a coroutine is suspended]], -example = [[ -function g(x) - coroutine.yield(x) -end - -function f (i) - debug.sethook(print, "l") - for j=1,1000 do - g(i+j) - end -end - -co = coroutine.wrap(f) -co(10) -pcall(co) -pcall(co) -]], -report = [[Nick Trout, 07/07/2003]], -patch = [[ -* lvm.c: -402,403c402,403 -< L->ci->u.l.pc = &pc; -< if (L->hookmask & LUA_MASKCALL) ---- -> if (L->hookmask & LUA_MASKCALL) { -> L->ci->u.l.pc = &pc; -404a405 -> } -405a407 -> L->ci->u.l.pc = &pc; -676,678c678 -< lua_assert(ci->u.l.pc == &pc && -< ttisfunction(ci->base - 1) && -< (ci->state & CI_SAVEDPC)); ---- -> lua_assert(ttisfunction(ci->base - 1) && (ci->state & CI_SAVEDPC)); -]] -} - - -Bug{ -what = [[userdata to be collected still counts into new GC threshold, -increasing memory consumption]], -report = [[Roberto, 25/07/2003]], -example = [[ -a = newproxy(true) -getmetatable(a).__gc = function () end -for i=1,10000000 do - newproxy(a) - if math.mod(i, 10000) == 0 then print(gcinfo()) end -end -]], -patch = [[ -* lgc.h: -18c18 -< void luaC_separateudata (lua_State *L); ---- -> size_t luaC_separateudata (lua_State *L); - -* lgc.c: -113c113,114 -< void luaC_separateudata (lua_State *L) { ---- -> size_t luaC_separateudata (lua_State *L) { -> size_t deadmem = 0; -127a129 -> deadmem += sizeudata(gcotou(curr)->uv.len); -136a139 -> return deadmem; -390c393 -< static void checkSizes (lua_State *L) { ---- -> static void checkSizes (lua_State *L, size_t deadmem) { -400c403 -< G(L)->GCthreshold = 2*G(L)->nblocks; /* new threshold */ ---- -> G(L)->GCthreshold = 2*G(L)->nblocks - deadmem; /* new threshold */ -454c457,458 -< static void mark (lua_State *L) { ---- -> static size_t mark (lua_State *L) { -> size_t deadmem; -467c471 -< luaC_separateudata(L); /* separate userdata to be preserved */ ---- -> deadmem = luaC_separateudata(L); /* separate userdata to be preserved */ -475a480 -> return deadmem; -480c485 -< mark(L); ---- -> size_t deadmem = mark(L); -482c487 -< checkSizes(L); ---- -> checkSizes(L, deadmem); -]] -} - -Bug{ -what=[[IBM AS400 (OS400) has sizeof(void *)==16, and a `%p' may generate -up to 60 characters in a `printf'. That causes a buffer overflow in -`tostring'.]], - -report = [[David Burgess, 25/08/2003]], - -example = [[print{}; (in an AS400 machine)]], - -patch = [[ -* liolib.c: -178c178 -< char buff[32]; ---- -> char buff[128]; - -* lbaselib.c: -327c327 -< char buff[64]; ---- -> char buff[128]; -]] -} - - -Bug{ -what = [[syntax `local function' does not increment stack size]], - -report = [[Rici Lake, 26/09/2003]], - -example = [[ --- must run this with precompiled code -local a,b,c -local function d () end -]], - -patch = [[ -* lparser.c: -1143a1144 -> FuncState *fs = ls->fs; -1145c1146,1147 -< init_exp(&v, VLOCAL, ls->fs->freereg++); ---- -> init_exp(&v, VLOCAL, fs->freereg); -> luaK_reserveregs(fs, 1); -1148c1150,1152 -< luaK_storevar(ls->fs, &v, &b); ---- -> luaK_storevar(fs, &v, &b); -> /* debug information will only see the variable after this point! */ -> getlocvar(fs, fs->nactvar - 1).startpc = fs->pc; -]], - -} - - -Bug{ - -what = [[count hook may be called without being set]], - -report = [[Andreas Stenius, 06/10/2003]], - -example = [[ -set your hooks with - - lua_sethook(L, my_hook, LUA_MASKLINE | LUA_MASKRET, 1); - -(It is weird to use a count > 0 without setting the count hook, -but it is not wrong.) -]], - -patch = [[ -* lvm.c: -69c69 -< if (mask > LUA_MASKLINE) { /* instruction-hook set? */ ---- -> if (mask & LUA_MASKCOUNT) { /* instruction-hook set? */ -]], - -} - - -Bug{ - -what = [[`dofile' eats one return value when called without arguments]], - -report = [[Frederico Abraham, 15/01/2004]], - -example = [[ -a,b = dofile() --< here you enter `return 1,2,3 ' -print(a,b) --> 2 3 (should be 1 and 2) -]], - -patch = [[ -* lbaselib.c: -313a314 -> int n = lua_gettop(L); -317c318 -< return lua_gettop(L) - 1; ---- -> return lua_gettop(L) - n; -]], - -} - - - ------------------------------------------------------------------ --- Lua 5.0.2 - -Bug{ -what = [[string concatenation may cause arithmetic overflow, leading -to a buffer overflow]], - -report = [[Rici Lake, 20/05/2004]], - -example = [[ -longs = string.rep("\0", 2^25) -function catter(i) - return assert(loadstring( - string.format("return function(a) return a%s end", - string.rep("..a", i-1))))() -end -rep129 = catter(129) -rep129(longs) -]], - -patch = [[ -* lvm.c: -@@ -321,15 +321,15 @@ - luaG_concaterror(L, top-2, top-1); - } else if (tsvalue(top-1)->tsv.len > 0) { /* if len=0, do nothing */ - /* at least two string values; get as many as possible */ -- lu_mem tl = cast(lu_mem, tsvalue(top-1)->tsv.len) + -- cast(lu_mem, tsvalue(top-2)->tsv.len); -+ size_t tl = tsvalue(top-1)->tsv.len; - char *buffer; - int i; -- while (n < total && tostring(L, top-n-1)) { /* collect total length */ -- tl += tsvalue(top-n-1)->tsv.len; -- n++; -+ /* collect total length */ -+ for (n = 1; n < total && tostring(L, top-n-1); n++) { -+ size_t l = tsvalue(top-n-1)->tsv.len; -+ if (l >= MAX_SIZET - tl) luaG_runerror(L, "string length overflow"); -+ tl += l; - } -- if (tl > MAX_SIZET) luaG_runerror(L, "string size overflow"); - buffer = luaZ_openspace(L, &G(L)->buff, tl); - tl = 0; - for (i=n; i>0; i--) { /* concat all strings */ -]] -} - - -Bug{ -what = [[lua_getupvalue and setupvalue do not check for index too small]], - -report = [[Mike Pall, ?/2004]], - -example = [[debug.getupvalue(function() end, 0)]], - -patch = [[ -* lapi.c -941c941 -< if (n > f->c.nupvalues) return NULL; ---- -> if (!(1 <= n && n <= f->c.nupvalues)) return NULL; -947c947 -< if (n > p->sizeupvalues) return NULL; ---- -> if (!(1 <= n && n <= p->sizeupvalues)) return NULL; -]] -} - - -Bug{ -what = [[values holded in open upvalues of suspended threads may be -incorrectly collected]], - -report = [[Spencer Schumann, 31/12/2004]], - -example = [[ -local thread_id = 0 -local threads = {} - -function fn(thread) - thread_id = thread_id + 1 - threads[thread_id] = function() - thread = nil - end - coroutine.yield() -end - -while true do - local thread = coroutine.create(fn) - coroutine.resume(thread, thread) -end -]], - -patch = [[ -* lgc.c: -221,224c221,222 -< if (!u->marked) { -< markobject(st, &u->value); -< u->marked = 1; -< } ---- -> markobject(st, u->v); -> u->marked = 1; -]], -} - - -Bug{ -what = [[rawset/rawget do not ignore extra arguments]], - -report = [[Romulo Bahiense, 11/03/2005]], - -example = [[ -a = {} -rawset(a, 1, 2, 3) -print(a[1], a[2]) -- should be 2 and nil -]], - -patch = [[ -* lbaselib.c: -175a176 -> lua_settop(L, 2); -183a185 -> lua_settop(L, 3); -]], -} - - -Bug{ -what = [[weak tables that survive one collection are never collected]], - -report = [[Chromix, 02/01/2006]], - -example = [[ -a = {} -print(gcinfo()) -for i = 1, 10000 do - a[i] = setmetatable({}, {__mode = "v"}) -end -collectgarbage() -a = nil -collectgarbage() -print(gcinfo()) -]], - -patch = [[ -* lgc.c -@@ -366,7 +366,7 @@ - GCObject *curr; - int count = 0; /* number of collected items */ - while ((curr = *p) != NULL) { -- if (curr->gch.marked > limit) { -+ if ((curr->gch.marked & ~(KEYWEAK | VALUEWEAK)) > limit) { - unmark(curr); - p = &curr->gch.next; - } -]], - -} - - -Bug{ -what = [[Some "not not exp" may not result in boolean values]], -report = [[]], -since = [[4.0]], -example = [[ --- should print false, but prints nil -print(not not (nil and 4)) -]], -patch = [[]], -} - - -Bug{ -what = [[On some machines, closing a "piped file" (created with io.popen) -may crash Lua]], -report = [[]], -since = [[5.0]], -example = [[ --- only on some machines - f = io.popen("ls") - f:close() -]], -patch = [[]], -} - - - ------------------------------------------------------------------ --- Lua 5.1 - -Bug{ -what = [[In 16-bit machines, expressions and/or with numeric constants as the -right operand may result in weird values]], - -report = [[Andreas Stenius/Kein-Hong Man, 15/03/2006]], - -example = [[ -print(false or 0) -- on 16-bit machines -]], - -patch = [[ -* lcode.c: -@@ -731,17 +731,15 @@ - case OPR_AND: { - lua_assert(e1->t == NO_JUMP); /* list must be closed */ - luaK_dischargevars(fs, e2); -- luaK_concat(fs, &e1->f, e2->f); -- e1->k = e2->k; e1->u.s.info = e2->u.s.info; -- e1->u.s.aux = e2->u.s.aux; e1->t = e2->t; -+ luaK_concat(fs, &e2->f, e1->f); -+ *e1 = *e2; - break; - } - case OPR_OR: { - lua_assert(e1->f == NO_JUMP); /* list must be closed */ - luaK_dischargevars(fs, e2); -- luaK_concat(fs, &e1->t, e2->t); -- e1->k = e2->k; e1->u.s.info = e2->u.s.info; -- e1->u.s.aux = e2->u.s.aux; e1->f = e2->f; -+ luaK_concat(fs, &e2->t, e1->t); -+ *e1 = *e2; - break; - } -]], - -} - - -Bug{ -what = [[luaL_checkudata may produce wrong error message]], - -report = [[Greg Falcon, 21/03/2006]], - -example = [[ -getmetatable(io.stdin).__gc() - --> bad argument #1 to '__gc' (FILE* expected, got table) -]], - -patch = [[ -* lauxlib.c: -@@ -123,11 +123,17 @@ - - LUALIB_API void *luaL_checkudata (lua_State *L, int ud, const char *tname) { - void *p = lua_touserdata(L, ud); -- lua_getfield(L, LUA_REGISTRYINDEX, tname); /* get correct metatable */ -- if (p == NULL || !lua_getmetatable(L, ud) || !lua_rawequal(L, -1, -2)) -- luaL_typerror(L, ud, tname); -- lua_pop(L, 2); /* remove both metatables */ -- return p; -+ if (p != NULL) { /* value is a userdata? */ -+ if (lua_getmetatable(L, ud)) { /* does it have a metatable? */ -+ lua_getfield(L, LUA_REGISTRYINDEX, tname); /* get correct metatable */ -+ if (lua_rawequal(L, -1, -2)) { /* does it have the correct mt? */ -+ lua_pop(L, 2); /* remove both metatables */ -+ return p; -+ } -+ } -+ } -+ luaL_typerror(L, ud, tname); /* else error */ -+ return NULL; /* to avoid warnings */ - } -]] - -} - - -Bug{ -what = [[ -In Windows, -when Lua is used in an application that also uses DirectX, -it may present an erractic behavior. -THIS IS NOT A LUA BUG! -The problem is that DirectX violates an ABI that Lua depends on.]], - -patch = [[ -The simplest solution is to use DirectX with -the D3DCREATE_FPU_PRESERVE flag. - -Otherwise, you can change the definition of lua_number2int, -in luaconf.h, to this one: -#define lua_number2int(i,d) __asm fld d __asm fistp i -]], - -} - - -Bug{ -what = [[option '%q' in string.format does not handle '\r' correctly.]], - -example = [[ -local s = "a string with \r and \n and \r\n and \n\r" -local c = string.format("return %q", s) -assert(assert(loadstring(c))() == s) -]], - -patch = [[ -* lstrlib.c: -@@ -703,6 +703,10 @@ - luaL_addchar(b, *s); - break; - } -+ case '\r': { -+ luaL_addlstring(b, "\\r", 2); -+ break; -+ } - case '\0': { - luaL_addlstring(b, "\\000", 4); - break; -]], - -} - - -Bug{ -what = [[lua_dostring/lua_dofile should return any values returned -by the chunk]], - -patch = [[ -* lauxlib.h: -@@ -108,9 +108,11 @@ - - #define luaL_typename(L,i) lua_typename(L, lua_type(L,(i))) - --#define luaL_dofile(L, fn) (luaL_loadfile(L, fn) || lua_pcall(L, 0, 0, 0)) -+#define luaL_dofile(L, fn) \ -+ (luaL_loadfile(L, fn) || lua_pcall(L, 0, LUA_MULTRET, 0)) - --#define luaL_dostring(L, s) (luaL_loadstring(L, s) || lua_pcall(L, 0, 0, 0))+#define luaL_dostring(L, s) \ -+ (luaL_loadstring(L, s) || lua_pcall(L, 0, LUA_MULTRET, 0)) - - #define luaL_getmetatable(L,n) (lua_getfield(L, LUA_REGISTRYINDEX, (n))) -]], - -} - - -Bug{ - -what = [[garbage collector does not compensate enough for finalizers]], - -patch = [[ -lgc.c: -@@ -322,4 +322,6 @@ - --static void propagateall (global_State *g) { -- while (g->gray) propagatemark(g); -+static size_t propagateall (global_State *g) { -+ size_t m = 0; -+ while (g->gray) m += propagatemark(g); -+ return m; - } -@@ -542,3 +544,3 @@ - marktmu(g); /* mark `preserved' userdata */ -- propagateall(g); /* remark, to propagate `preserveness' */ -+ udsize += propagateall(g); /* remark, to propagate `preserveness' */ - cleartable(g->weak); /* remove collected objects from weak tables */ -@@ -592,2 +594,4 @@ - GCTM(L); -+ if (g->estimate > GCFINALIZECOST) -+ g->estimate -= GCFINALIZECOST; -]] -} - - -Bug{ - -what = [[debug hooks may get wrong when mixed with coroutines]], - -report = [[by Ivko Stanilov, 03/06/2006]], - -example = [[ -co = coroutine.create(function (a,b) - coroutine.yield(a, b) - return b, "end" -end) - -debug.sethook(co, function() end, "lcr") -coroutine.resume(co, 100, 2000) -coroutine.resume(co, 100, 2000) -]], - -patch = [[ -* ldo.c: -@@ -389,6 +389,7 @@ - return; - } - else { /* resuming from previous yield */ -+ L->status = 0; - if (!f_isLua(ci)) { /* `common' yield? */ - /* finish interrupted execution of `OP_CALL' */ - lua_assert(GET_OPCODE(*((ci-1)->savedpc - 1)) == OP_CALL || -@@ -399,7 +400,6 @@ - else /* yielded inside a hook: just continue its execution */ - L->base = L->ci->base; - } -- L->status = 0; - luaV_execute(L, cast_int(L->ci - L->base_ci)); - } -]], - -} - - - ------------------------------------------------------------------ --- Lua 5.1.1 - -Bug{ -what = [[list constructors have wrong limit]], - -report = [[by Norman Ramsey, June 2006]], - -since = "5.1", - -example = [[ -a = {} -a[1] = "x={1" -for i = 2, 2^20 do - a[i] = 1 -end -a[#a + 1] = "}" -s = table.concat(a, ",") -assert(loadstring(s))() -print(#x) -]], - -patch = [[ -* lparser.c: -@@ -489,7 +489,7 @@ - - static void listfield (LexState *ls, struct ConsControl *cc) { - expr(ls, &cc->v); -- luaY_checklimit(ls->fs, cc->na, MAXARG_Bx, "items in a constructor"); -+ luaY_checklimit(ls->fs, cc->na, MAX_INT, "items in a constructor"); - cc->na++; - cc->tostore++; - } -]], - -} - - -Bug{ -what = [[wrong message error in some cases involving closures]], - -report = [[Shmuel Zeigerman, on 07/2006]], - -since = "5.1", - -example = [[ -local Var -local function main() - NoSuchName (function() Var=0 end) -end -main() ---> lua5.1: temp:3: attempt to call upvalue 'Var' (a nil value) -]], - -patch = [[ -*ldebug.c: -@@ -435,14 +435,16 @@ - break; - } - case OP_CLOSURE: { -- int nup; -+ int nup, j; - check(b < pt->sizep); - nup = pt->p[b]->nups; - check(pc + nup < pt->sizecode); -- for (; nup>0; nup--) { -- OpCode op1 = GET_OPCODE(pt->code[pc+nup]); -+ for (j = 1; j <= nup; j++) { -+ OpCode op1 = GET_OPCODE(pt->code[pc + j]); - check(op1 == OP_GETUPVAL || op1 == OP_MOVE); - } -+ if (reg != NO_REG) /* tracing? */ -+ pc += nup; /* do not 'execute' these pseudo-instructions */ - break; - } - case OP_VARARG: { -]], - -} - - -Bug{ -what = [[string.format("%") may read past the string]], -report = [[Roberto, on 09/2006]], -since = [[5.0]], -example = [[print(string.format("%"))]], -patch = [[ -*lstrlib.c: -@@ -723,7 +723,7 @@ - - static const char *scanformat (lua_State *L, const char *strfrmt, char *form) { const char *p = strfrmt; -- while (strchr(FLAGS, *p)) p++; /* skip flags */ -+ while (*p != '\0' && strchr(FLAGS, *p) != NULL) p++; /* skip flags */ - if ((size_t)(p - strfrmt) >= sizeof(FLAGS)) - luaL_error(L, "invalid format (repeated flags)"); - if (isdigit(uchar(*p))) p++; /* skip width */ -]], -} - - -Bug{ -what = [[os.date throws an error when result is the empty string]], -report = [[]], -since = [[4.0]], -example = [[print(os.date(""))]], -patch = [[ -*loslib.c: -@@ -148,7 +148,18 @@ - else { -- char b[256]; -- if (strftime(b, sizeof(b), s, stm)) -- lua_pushstring(L, b); -- else -- return luaL_error(L, LUA_QL("date") " format too long"); -+ char cc[3]; -+ luaL_Buffer b; -+ cc[0] = '%'; cc[2] = '\0'; -+ luaL_buffinit(L, &b); -+ for (; *s; s++) { -+ if (*s != '%' || *(s + 1) == '\0') /* no conversion specifier? */ -+ luaL_addchar(&b, *s); -+ else { -+ size_t reslen; -+ char buff[200]; /* should be big enough for any conversion result */ -+ cc[1] = *(++s); -+ reslen = strftime(buff, sizeof(buff), cc, stm); -+ luaL_addlstring(&b, buff, reslen); -+ } -+ } -+ luaL_pushresult(&b); - } -]], -} - - -Bug{ -what = [[setfenv accepts invalid 1st argument]], -report = [[Doug Rogers, on 02/2007]], -since = [[5.0]], -example = [[setfenv(nil, {}) -- should throw an error]], -patch = [[ -*lbaselib.c: -@@ -116,3 +116,3 @@ - --static void getfunc (lua_State *L) { -+static void getfunc (lua_State *L, int opt) { - if (lua_isfunction(L, 1)) lua_pushvalue(L, 1); -@@ -120,3 +120,3 @@ - lua_Debug ar; -- int level = luaL_optint(L, 1, 1); -+ int level = opt ? luaL_optint(L, 1, 1) : luaL_checkint(L, 1); - luaL_argcheck(L, level >= 0, 1, "level must be non-negative"); -@@ -133,3 +133,3 @@ - static int luaB_getfenv (lua_State *L) { -- getfunc(L); -+ getfunc(L, 1); - if (lua_iscfunction(L, -1)) /* is a C function? */ -@@ -144,3 +144,3 @@ - luaL_checktype(L, 2, LUA_TTABLE); -- getfunc(L); -+ getfunc(L, 0); - lua_pushvalue(L, 2); -]], -} - - -Bug{ -what = [[wrong code for arithmetic expressions in some specific scenarios]], -report = [[Thierry Grellier, on 01/2007]], -since = [[5.1]], -example = [[ --- use a large number of names (almost 256) -v1=1; v2=1; v3=1; v4=1; v5=1; v6=1; v7=1; v8=1; v9=1; -v10=1; v11=1; v12=1; v13=1; v14=1; v15=1; v16=1; v17=1; -v18=1; v19=1; v20=1; v21=1; v22=1; v23=1; v24=1; v25=1; -v26=1; v27=1; v28=1; v29=1; v30=1; v31=1; v32=1; v33=1; -v34=1; v35=1; v36=1; v37=1; v38=1; v39=1; v40=1; v41=1; -v42=1; v43=1; v44=1; v45=1; v46=1; v47=1; v48=1; v49=1; -v50=1; v51=1; v52=1; v53=1; v54=1; v55=1; v56=1; v57=1; -v58=1; v59=1; v60=1; v61=1; v62=1; v63=1; v64=1; v65=1; -v66=1; v67=1; v68=1; v69=1; v70=1; v71=1; v72=1; v73=1; -v74=1; v75=1; v76=1; v77=1; v78=1; v79=1; v80=1; v81=1; -v82=1; v83=1; v84=1; v85=1; v86=1; v87=1; v88=1; v89=1; -v90=1; v91=1; v92=1; v93=1; v94=1; v95=1; v96=1; v97=1; -v98=1; v99=1; v100=1; v101=1; v102=1; v103=1; v104=1; v105=1; -v106=1; v107=1; v108=1; v109=1; v110=1; v111=1; v112=1; v113=1; -v114=1; v115=1; v116=1; v117=1; v118=1; v119=1; v120=1; v121=1; -v122=1; v123=1; v124=1; v125=1; v126=1; v127=1; v128=1; v129=1; -v130=1; v131=1; v132=1; v133=1; v134=1; v135=1; v136=1; v137=1; -v138=1; v139=1; v140=1; v141=1; v142=1; v143=1; v144=1; v145=1; -v146=1; v147=1; v148=1; v149=1; v150=1; v151=1; v152=1; v153=1; -v154=1; v155=1; v156=1; v157=1; v158=1; v159=1; v160=1; v161=1; -v162=1; v163=1; v164=1; v165=1; v166=1; v167=1; v168=1; v169=1; -v170=1; v171=1; v172=1; v173=1; v174=1; v175=1; v176=1; v177=1; -v178=1; v179=1; v180=1; v181=1; v182=1; v183=1; v184=1; v185=1; -v186=1; v187=1; v188=1; v189=1; v190=1; v191=1; v192=1; v193=1; -v194=1; v195=1; v196=1; v197=1; v198=1; v199=1; v200=1; v201=1; -v202=1; v203=1; v204=1; v205=1; v206=1; v207=1; v208=1; v209=1; -v210=1; v211=1; v212=1; v213=1; v214=1; v215=1; v216=1; v217=1; -v218=1; v219=1; v220=1; v221=1; v222=1; v223=1; v224=1; v225=1; -v226=1; v227=1; v228=1; v229=1; v230=1; v231=1; v232=1; v233=1; -v234=1; v235=1; v236=1; v237=1; v238=1; v239=1; v240=1; v241=1; -v242=1; v243=1; v244=1; v245=1; v246=1; v247=1; v248=1; v249=1; -v250=1; -v251={k1 = 1}; -v252=1; -print(2 * v251.k1, v251.k1 * 2); -- 2 2, OK -v253=1; -print(2 * v251.k1, v251.k1 * 2); -- 1 2, ??? -]], -patch = [[ -*lcode.c: -@@ -657,10 +657,16 @@ - if (constfolding(op, e1, e2)) - return; - else { -- int o1 = luaK_exp2RK(fs, e1); - int o2 = (op != OP_UNM && op != OP_LEN) ? luaK_exp2RK(fs, e2) : 0; -- freeexp(fs, e2); -- freeexp(fs, e1); -+ int o1 = luaK_exp2RK(fs, e1); -+ if (o1 > o2) { -+ freeexp(fs, e1); -+ freeexp(fs, e2); -+ } -+ else { -+ freeexp(fs, e2); -+ freeexp(fs, e1); -+ } - e1->u.s.info = luaK_codeABC(fs, op, 0, o1, o2); - e1->k = VRELOCABLE; - } -@@ -718,10 +724,15 @@ - luaK_exp2nextreg(fs, v); /* operand must be on the `stack' */ - break; - } -- default: { -+ case OPR_ADD: case OPR_SUB: case OPR_MUL: case OPR_DIV: -+ case OPR_MOD: case OPR_POW: { - if (!isnumeral(v)) luaK_exp2RK(fs, v); - break; - } -+ default: { -+ luaK_exp2RK(fs, v); -+ break; -+ } - } - } -]], -} - -Bug{ -what = [[assignment of nil to parameter may be optimized away]], -report = [[Thomas Lauer, on 03/2007]], -since = [[5.1]], -example = [[ -function f (a) - a=nil - return a -end - -print(f("test")) -]], -patch = [[ -*lcode.c: -@@ -35,16 +35,20 @@ - void luaK_nil (FuncState *fs, int from, int n) { - Instruction *previous; - if (fs->pc > fs->lasttarget) { /* no jumps to current position? */ -- if (fs->pc == 0) /* function start? */ -- return; /* positions are already clean */ -- previous = &fs->f->code[fs->pc-1]; -- if (GET_OPCODE(*previous) == OP_LOADNIL) { -- int pfrom = GETARG_A(*previous); -- int pto = GETARG_B(*previous); -- if (pfrom <= from && from <= pto+1) { /* can connect both? */ -- if (from+n-1 > pto) -- SETARG_B(*previous, from+n-1); -- return; -+ if (fs->pc == 0) { /* function start? */ -+ if (from >= fs->nactvar) -+ return; /* positions are already clean */ -+ } -+ else { -+ previous = &fs->f->code[fs->pc-1]; -+ if (GET_OPCODE(*previous) == OP_LOADNIL) { -+ int pfrom = GETARG_A(*previous); -+ int pto = GETARG_B(*previous); -+ if (pfrom <= from && from <= pto+1) { /* can connect both? */ -+ if (from+n-1 > pto) -+ SETARG_B(*previous, from+n-1); -+ return; -+ } - } - } - } -]], -} - - -Bug{ -what = [[__concat metamethod converts numbers to strings]], -report = [[Paul Winwood, on 12/2006]], -since = [[5.0]], -example = [[ -a = {} -setmetatable(a, {__concat = function (a,b) print(type(a), type(b)) end}) -a = 4 .. a -]], -patch = [[ -*lvm.c: -@@ -281,10 +281,12 @@ - do { - StkId top = L->base + last + 1; - int n = 2; /* number of elements handled in this pass (at least 2) */ -- if (!tostring(L, top-2) || !tostring(L, top-1)) { -+ if (!(ttisstring(top-2) || ttisnumber(top-2)) || !tostring(L, top-1)) { - if (!call_binTM(L, top-2, top-1, top-2, TM_CONCAT)) - luaG_concaterror(L, top-2, top-1); -- } else if (tsvalue(top-1)->len > 0) { /* if len=0, do nothing */ -+ } else if (tsvalue(top-1)->len == 0) /* second op is empty? */ -+ (void)tostring(L, top - 2); /* result is first op (as string) */ -+ else { - /* at least two string values; get as many as possible */ - size_t tl = tsvalue(top-1)->len; - char *buffer; -]], -} - - -Bug{ -what = [[As a library, loadlib.c should not access Lua internals -(via lobject.h)]], -report = [[Jérôme Vuarand, on 03/2007]], -since = [[5.0]], -example = [[the bug has no effect on external behavior]], -patch = [[remove the '#include "lobject.h" and use -'lua_pushfstring' instead of 'luaO_pushfstring']], -} - - - ------------------------------------------------------------------ --- Lua 5.1.2 - -Bug{ -what = [[Lua may close standard files, -which then may be used by C]], -report = [[David Manura/Ross Berteig, on 04/2007]], -since = [[]], -example = [[ -io.close(io.stderr) --- in some systems, following attempts to write to 'stderr' may crash -a = a + 1 -]], -patch = [[ -]], -} - -Bug{ -what = [[code generated for "-nil", "-true", and "-false" is wrong]], -report = [[David Manura/Rici Lake, on 04/2007]], -since = [[5.1]], -example = [[print(-nil)]], -patch = [[ -lcode.c: -@@ -699,7 +699,7 @@ - e2.t = e2.f = NO_JUMP; e2.k = VKNUM; e2.u.nval = 0; - switch (op) { - case OPR_MINUS: { -- if (e->k == VK) -+ if (!isnumeral(e)) - luaK_exp2anyreg(fs, e); /* cannot operate on non-numeric constants */ - codearith(fs, OP_UNM, e, &e2); - break; -]], -} - -Bug{ -what = [[Count hook may be called without being set.]], -report = [[Mike Pall, on 05/2007]], -since = [[?]], -example = [[]], -patch = [[ -lvm.c: -@@ -61,11 +61,9 @@ - lu_byte mask = L->hookmask; - const Instruction *oldpc = L->savedpc; - L->savedpc = pc; -- if (mask > LUA_MASKLINE) { /* instruction-hook set? */ -- if (L->hookcount == 0) { -- resethookcount(L); -- luaD_callhook(L, LUA_HOOKCOUNT, -1); -- } -+ if ((mask & LUA_MASKCOUNT) && L->hookcount == 0) { -+ resethookcount(L); -+ luaD_callhook(L, LUA_HOOKCOUNT, -1); - } - if (mask & LUA_MASKLINE) { - Proto *p = ci_func(L->ci)->l.p; -]], -} - -Bug{ -what = [[recursive coroutines may overflow C stack]], -report = [[ , on ]], -since = [[5.0]], -example = [[ -a = function(a) coroutine.wrap(a)(a) end -a(a) -]], -patch = [[The 'nCcalls' counter should be shared by all threads. -(That is, it should be declared in the 'global_State' structure, -not in 'lua_State'.) -]], -} - -Bug{ -what = [[wrong error message in some concatenations]], -report = [[Alex Davies, on 05/2007]], -since = [[5.1.2]], -example = [[a = nil; a = (1)..a]], -patch = [[ -ldebug.c: -@@ -563,8 +563,8 @@ - - - void luaG_concaterror (lua_State *L, StkId p1, StkId p2) { -- if (ttisstring(p1)) p1 = p2; -- lua_assert(!ttisstring(p1)); -+ if (ttisstring(p1) || ttisnumber(p1)) p1 = p2; -+ lua_assert(!ttisstring(p1) && !ttisnumber(p1)); - luaG_typeerror(L, p1, "concatenate"); - } - -]], -} - -Bug{ -what = [[Very small numbers all collide in the hash function. -(This creates only performance problems; the behavoir is correct.)]], -report = [[, on ]], -since = [[5.0]], -example = [[]], -patch = [[ -ltable.c: -87,88c87,88 -< n += 1; /* normalize number (avoid -0) */ -< lua_assert(sizeof(a) <= sizeof(n)); ---- -> if (luai_numeq(n, 0)) /* avoid problems with -0 */ -> return gnode(t, 0); -]], -} - -Bug{ -what = [[Too many variables in an assignment may cause a -C stack overflow]], -report = [[Mike Pall, on 07/2007]], -since = [[5.0]], -example = [[ -$ ulimit -s 1024 # Reduce C stack to 1MB for quicker results -$ lua -e 'local s = "a,"; for i=1,18 do s = s..s end print(loadstring("local a;"..s.."a=nil", ""))' -]], -patch = [[ -lparser.c: -@@ -938,6 +938,8 @@ - primaryexp(ls, &nv.v); - if (nv.v.k == VLOCAL) - check_conflict(ls, lh, &nv.v); -+ luaY_checklimit(ls->fs, nvars, LUAI_MAXCCALLS - ls->L->nCcalls, -+ "variable names"); - assignment(ls, &nv, nvars+1); - } - else { /* assignment -> `=' explist1 */ -]], -} - -Bug{ -what = [[An error in a module loaded through the '-l' option -shows no traceback]], -report = [[David Manura, on 08/2007]], -since = [[5.1]], -example = [[lua -ltemp (assuming temp.lua has an error)]], -patch = [[ -lua.c: -@@ -144,7 +144,7 @@ - static int dolibrary (lua_State *L, const char *name) { - lua_getglobal(L, "require"); - lua_pushstring(L, name); -- return report(L, lua_pcall(L, 1, 0, 0)); -+ return report(L, docall(L, 1, 1)); - } -]], -} - -Bug{ -what = [['gsub' may go wild when wrongly called without its third -argument and with a large subject]], -report = [[Florian Berger, on 10/2007]], -since = [[5.1]], -example = [[ -x = string.rep('a', 10000) .. string.rep('b', 10000) -print(#string.gsub(x, 'b')) -]], -patch = [[ -lstrlib.c: -@@ -631,6 +631,2 @@ - } -- default: { -- luaL_argerror(L, 3, "string/function/table expected"); -- return; -- } - } -@@ -650,2 +646,3 @@ - const char *p = luaL_checkstring(L, 2); -+ int tr = lua_type(L, 3); - int max_s = luaL_optint(L, 4, srcl+1); -@@ -655,2 +652,5 @@ - luaL_Buffer b; -+ luaL_argcheck(L, tr == LUA_TNUMBER || tr == LUA_TSTRING || -+ tr == LUA_TFUNCTION || tr == LUA_TTABLE, 3, -+ "string/function/table expected"); - luaL_buffinit(L, &b); -]], -} - -Bug{ -what = [[table.remove removes last element of a table when given -an out-of-bound index]], -report = [[Patrick Donnelly, on 11/2007]], -since = [[5.0]], -example = [[ -a = {1,2,3} -table.remove(a, 4) -print(a[3]) --> nil (should be 3) -]], -patch = [[ -ltablib.c: -@@ -118,7 +118,8 @@ - static int tremove (lua_State *L) { - int e = aux_getn(L, 1); - int pos = luaL_optint(L, 2, e); -- if (e == 0) return 0; /* table is `empty' */ -+ if (!(1 <= pos && pos <= e)) /* position is outside bounds? */ -+ return 0; /* nothing to remove */ - luaL_setn(L, 1, e - 1); /* t.n = n-1 */ - lua_rawgeti(L, 1, pos); /* result = t[pos] */ - for ( ;pos debug.setfenv(3, {}) -]], -patch = [[ -lapi.c: -@@ -749,7 +749,7 @@ - res = 0; - break; - } -- luaC_objbarrier(L, gcvalue(o), hvalue(L->top - 1)); -+ if (res) luaC_objbarrier(L, gcvalue(o), hvalue(L->top - 1)); - L->top--; - lua_unlock(L); - return res; -]], -} - -Bug{ -what = [[stand-alone interpreter shows incorrect error message -when the "message" is a coroutine]], -report = [[Patrick Donnelly, on 17/12/2007]], -since = [[5.1]], -example = [[> error(coroutine.create(function() end))]], -patch = [[ -lua.c: -@@ -74,6 +74,8 @@ - - - static int traceback (lua_State *L) { -+ if (!lua_isstring(L, 1)) /* 'message' not a string? */ -+ return 1; /* keep it intact */ - lua_getfield(L, LUA_GLOBALSINDEX, "debug"); - if (!lua_istable(L, -1)) { - lua_pop(L, 1); - -]], -} - -Bug{ -what = [[debug.sethook/gethook may overflow the thread's stack]], -report = [[Ivko Stanilov, on 2008/01/04]], -since = [[5.1]], -example = [[ -a = coroutine.create(function() yield() end) -coroutine.resume(a) -debug.sethook(a) -- may overflow the stack of 'a' -]], -patch = [[ -ldblib.c: -@@ -268,12 +268,11 @@ - count = luaL_optint(L, arg+3, 0); - func = hookf; mask = makemask(smask, count); - } -- gethooktable(L1); -- lua_pushlightuserdata(L1, L1); -+ gethooktable(L); -+ lua_pushlightuserdata(L, L1); - lua_pushvalue(L, arg+1); -- lua_xmove(L, L1, 1); -- lua_rawset(L1, -3); /* set new hook */ -- lua_pop(L1, 1); /* remove hook table */ -+ lua_rawset(L, -3); /* set new hook */ -+ lua_pop(L, 1); /* remove hook table */ - lua_sethook(L1, func, mask, count); /* set hooks */ - return 0; - } -@@ -288,11 +287,10 @@ - if (hook != NULL && hook != hookf) /* external hook? */ - lua_pushliteral(L, "external hook"); - else { -- gethooktable(L1); -- lua_pushlightuserdata(L1, L1); -- lua_rawget(L1, -2); /* get hook */ -- lua_remove(L1, -2); /* remove hook table */ -- lua_xmove(L1, L, 1); -+ gethooktable(L); -+ lua_pushlightuserdata(L, L1); -+ lua_rawget(L, -2); /* get hook */ -+ lua_remove(L, -2); /* remove hook table */ - } - lua_pushstring(L, unmakemask(mask, buff)); - lua_pushinteger(L, lua_gethookcount(L1)); -]] -} - - - ------------------------------------------------------------------ --- Lua 5.1.3 - -Bug{ -what = [[LUAI_MAXCSTACK must be smaller than -LUA_REGISTRYINDEX]], -report = [[Patrick Donnelly, on 2008/02/11]], -since = [[5.1.3]], -example = [[ -j = 1e4 -co = coroutine.create(function() - t = {} - for i = 1, j do t[i] = i end - return unpack(t) -end) -print(coroutine.resume(co)) -]], -patch = [[ -luaconf.h: -443c443,444 -< ** functions to consume unlimited stack space. ---- -> ** functions to consume unlimited stack space. (must be smaller than -> ** -LUA_REGISTRYINDEX) -445,446c446 -< #define LUAI_MCS_AUX ((int)(INT_MAX / (4*sizeof(LUA_NUMBER)))) -< #define LUAI_MAXCSTACK (LUAI_MCS_AUX > SHRT_MAX ? SHRT_MAX : LUAI_MCS_AUX) ---- -> #define LUAI_MAXCSTACK 8000 -]], -} - -Bug{ -what = [[coroutine.resume pushes element without ensuring stack size]], -report = [[on 2008/02/11]], -since = [[5.0]], -example = [[(this bug cannot be detected without internal assertions)]], -patch = [[ -lbaselib.c: -@@ -526,7 +526,7 @@ - status = lua_resume(co, narg); - if (status == 0 || status == LUA_YIELD) { - int nres = lua_gettop(co); -- if (!lua_checkstack(L, nres)) -+ if (!lua_checkstack(L, nres + 1)) - luaL_error(L, "too many results to resume"); - lua_xmove(co, L, nres); /* move yielded values */ - return nres; -]], -} - -Bug{ -what = [[lua_checkstack may have arithmetic overflow for large 'size']], -report = [[Patrick Donnelly, on 2008/02/12]], -since = [[5.0]], -example = [[ -print(unpack({1,2,3}, 0, 2^31-3)) -]], -patch = [[ ---- lapi.c 2008/01/03 15:20:39 2.55.1.3 -+++ lapi.c 2008/02/14 16:05:21 -@@ -93,15 +93,14 @@ - - - LUA_API int lua_checkstack (lua_State *L, int size) { -- int res; -+ int res = 1; - lua_lock(L); -- if ((L->top - L->base + size) > LUAI_MAXCSTACK) -+ if (size > LUAI_MAXCSTACK || (L->top - L->base + size) > LUAI_MAXCSTACK) - res = 0; /* stack overflow */ -- else { -+ else if (size > 0) { - luaD_checkstack(L, size); - if (L->ci->top < L->top + size) - L->ci->top = L->top + size; -- res = 1; - } - lua_unlock(L); - return res; -]], -} - -Bug{ -what = [[unpack with maximum indices may crash due to arithmetic overflow]], -report = [[Patrick Donnelly, on 2008/02/12]], -since = [[5.1]], -example = [[ -print(unpack({1,2,3}, 2^31-1, 2^31-1)) -]], -patch = [[ ---- lbaselib.c 2008/02/11 16:24:24 1.191.1.5 -+++ lbaselib.c 2008/02/14 16:10:25 -@@ -344,10 +344,12 @@ - luaL_checktype(L, 1, LUA_TTABLE); - i = luaL_optint(L, 2, 1); - e = luaL_opt(L, luaL_checkint, 3, luaL_getn(L, 1)); -+ if (i > e) return 0; /* empty range */ - n = e - i + 1; /* number of elements */ -- if (n <= 0) return 0; /* empty range */ -- luaL_checkstack(L, n, "table too big to unpack"); -- for (; i<=e; i++) /* push arg[i...e] */ -+ if (n <= 0 || !lua_checkstack(L, n)) /* n <= 0 means arith. overflow */ -+ return luaL_error(L, "too many results to unpack"); -+ lua_rawgeti(L, 1, i); /* push arg[i] (avoiding overflow problems) */ -+ while (i++ < e) /* push arg[i + 1...e] */ - lua_rawgeti(L, 1, i); - return n; - } -]], -} - -Bug{ -what = [[The validator for precompiled code has several flaws that -allow malicious binary code to crash the application]], -report = [[Peter Cawley, on 2008/03/24]], -since = [[5.0]], -example = [[ -a = string.dump(function()return;end) -a = a:gsub(string.char(30,37,122,128), string.char(34,0,0), 1) -loadstring(a)() -]], -patch = [[ ---- ldebug.c 2007/12/28 15:32:23 2.29.1.3 -+++ ldebug.c 2008/04/04 15:15:40 -@@ -275,12 +275,12 @@ - - static int precheck (const Proto *pt) { - check(pt->maxstacksize <= MAXSTACK); -- lua_assert(pt->numparams+(pt->is_vararg & VARARG_HASARG) <= pt->maxstacksize); -- lua_assert(!(pt->is_vararg & VARARG_NEEDSARG) || -+ check(pt->numparams+(pt->is_vararg & VARARG_HASARG) <= pt->maxstacksize); -+ check(!(pt->is_vararg & VARARG_NEEDSARG) || - (pt->is_vararg & VARARG_HASARG)); - check(pt->sizeupvalues <= pt->nups); - check(pt->sizelineinfo == pt->sizecode || pt->sizelineinfo == 0); -- check(GET_OPCODE(pt->code[pt->sizecode-1]) == OP_RETURN); -+ check(pt->sizecode > 0 && GET_OPCODE(pt->code[pt->sizecode-1]) == OP_RETURN); - return 1; - } - -@@ -363,7 +363,11 @@ - } - switch (op) { - case OP_LOADBOOL: { -- check(c == 0 || pc+2 < pt->sizecode); /* check its jump */ -+ if (c == 1) { /* does it jump? */ -+ check(pc+2 < pt->sizecode); /* check its jump */ -+ check(GET_OPCODE(pt->code[pc+1]) != OP_SETLIST || -+ GETARG_C(pt->code[pc+1]) != 0); -+ } - break; - } - case OP_LOADNIL: { -@@ -428,7 +432,10 @@ - } - case OP_SETLIST: { - if (b > 0) checkreg(pt, a + b); -- if (c == 0) pc++; -+ if (c == 0) { -+ pc++; -+ check(pc < pt->sizecode - 1); -+ } - break; - } - case OP_CLOSURE: { -]], -} - -Bug{ -what = [[maliciously crafted precompiled code can blow the C stack]], -report = [[Greg Falcon, on 2008/03/25]], -since = [[5.0]], -example = [[ -function crash(depth) - local init = '\27\76\117\97\81\0\1\4\4\4\8\0\7\0\0\0\61\115\116' .. - '\100\105\110\0\1\0\0\0\1\0\0\0\0\0\0\2\2\0\0\0\36' .. - '\0\0\0\30\0\128\0\0\0\0\0\1\0\0\0\0\0\0\0\1\0\0\0' .. - '\1\0\0\0\0\0\0\2' - local mid = '\1\0\0\0\30\0\128\0\0\0\0\0\0\0\0\0\1\0\0\0\1\0\0\0\0' - local fin = '\0\0\0\0\0\0\0\2\0\0\0\1\0\0\0\1\0\0\0\1\0\0\0\2\0' .. - '\0\0\97\0\1\0\0\0\1\0\0\0\0\0\0\0' - local lch = '\2\0\0\0\36\0\0\0\30\0\128\0\0\0\0\0\1\0\0\0\0\0\0' .. - '\0\1\0\0\0\1\0\0\0\0\0\0\2' - local rch = '\0\0\0\0\0\0\0\2\0\0\0\1\0\0\0\1\0\0\0\1\0\0\0\2\0' .. - '\0\0\97\0\1\0\0\0\1' - for i=1,depth do lch,rch = lch..lch,rch..rch end - loadstring(init .. lch .. mid .. rch .. fin) -end -for i=1,25 do print(i); crash(i) end -]], -patch = [[ ---- lundump.c 2008/04/04 16:00:45 2.7.1.3 -+++ lundump.c 2008/04/04 19:51:41 2.7.1.4 -@@ -161,7 +161,9 @@ - - static Proto* LoadFunction(LoadState* S, TString* p) - { -- Proto* f=luaF_newproto(S->L); -+ Proto* f; -+ if (++S->L->nCcalls > LUAI_MAXCCALLS) error(S,"code too deep"); -+ f=luaF_newproto(S->L); - setptvalue2s(S->L,S->L->top,f); incr_top(S->L); - f->source=LoadString(S); if (f->source==NULL) f->source=p; - f->linedefined=LoadInt(S); -@@ -175,6 +177,7 @@ - LoadDebug(S,f); - IF (!luaG_checkcode(f), "bad code"); - S->L->top--; -+ S->L->nCcalls--; - return f; - } -]], -} - -Bug{ -what = [[code validator may reject (maliciously crafted) correct code]], -report = [[Greg Falcon, on 2008/03/26]], -since = [[5.0]], -example = [[ -z={} -for i=1,27290 do z[i]='1,' end -z = 'if 1+1==2 then local a={' .. table.concat(z) .. '} end' -func = loadstring(z) -print(loadstring(string.dump(func))) -]], -patch = [[ ---- ldebug.c 2008/04/04 15:30:05 2.29.1.4 -+++ ldebug.c 2008/04/04 15:47:10 -@@ -346,9 +346,18 @@ - int dest = pc+1+b; - check(0 <= dest && dest < pt->sizecode); - if (dest > 0) { -- /* cannot jump to a setlist count */ -- Instruction d = pt->code[dest-1]; -- check(!(GET_OPCODE(d) == OP_SETLIST && GETARG_C(d) == 0)); -+ int j; -+ /* check that it does not jump to a setlist count; this -+ is tricky, because the count from a previous setlist may -+ have the same value of an invalid setlist; so, we must -+ go all the way back to the first of them (if any) */ -+ for (j = 0; j < dest; j++) { -+ Instruction d = pt->code[dest-1-j]; -+ if (!(GET_OPCODE(d) == OP_SETLIST && GETARG_C(d) == 0)) break; -+ } -+ /* if 'j' is even, previous value is not a setlist (even if -+ it looks like one) */ -+ check((j&1) == 0); - } - } - break; -]], -} - -Bug{ -what = [[maliciously crafted precompiled code can inject invalid boolean -values into Lua code]], -report = [[Greg Falcon, on 2008/03/27]], -since = [[5.0]], -example = [[ -maybe = string.dump(function() return ({[true]=true})[true] end) -maybe = maybe:gsub('\1\1','\1\2') -maybe = loadstring(maybe)() -assert(type(maybe) == "boolean" and maybe ~= true and maybe ~= false) -]], -patch = [[ ---- lundump.c 2008/01/18 16:39:11 2.7.1.2 -+++ lundump.c 2008/04/04 15:50:39 -@@ -115,7 +115,7 @@ - setnilvalue(o); - break; - case LUA_TBOOLEAN: -- setbvalue(o,LoadChar(S)); -+ setbvalue(o,LoadChar(S)!=0); - break; - case LUA_TNUMBER: - setnvalue(o,LoadNumber(S)); -]], -} - - -Bug{ -what = [['string.byte' gets confused with some out-of-range negative indices]], -report = [[Mike Pall, on 2008/06/03]], -since = [[5.1]], -example = [[ -print(string.byte("abc", -5)) --> 97 98 99 (should print nothing) -]], -patch = [[ ---- lstrlib.c 2007/12/28 15:32:23 1.132.1.3 -+++ lstrlib.c 2008/07/05 11:53:42 -@@ -35,7 +35,8 @@ - - static ptrdiff_t posrelat (ptrdiff_t pos, size_t len) { - /* relative string position: negative means back from end */ -- return (pos>=0) ? pos : (ptrdiff_t)len+pos+1; -+ if (pos < 0) pos += (ptrdiff_t)len + 1; -+ return (pos >= 0) ? pos : 0; - } - - -]], -} - - -Bug{ -what = [[user-requested GC step may loop forever]], -report = [[Makoto Hamanaka, on 2008/07/01]], -since = [[5.1]], -example = [[ -collectgarbage("setpause", 100) -- small value -collectgarbage("setstepmul", 2000) -- large value -collectgarbage("step",0) -]], -patch = [[ ---- lapi.c 2008/02/14 16:46:39 2.55.1.4 -+++ lapi.c 2008/07/04 18:34:48 -@@ -929,10 +929,13 @@ - g->GCthreshold = g->totalbytes - a; - else - g->GCthreshold = 0; -- while (g->GCthreshold <= g->totalbytes) -+ while (g->GCthreshold <= g->totalbytes) { - luaC_step(L); -- if (g->gcstate == GCSpause) /* end of cycle? */ -- res = 1; /* signal it */ -+ if (g->gcstate == GCSpause) { /* end of cycle? */ -+ res = 1; /* signal it */ -+ break; -+ } -+ } - break; - } - case LUA_GCSETPAUSE: { -]], -} - - -Bug{ -what = [['module' may change the environment of a C function]], -report = [[Peter Cawley, on 2008/07/16]], -since = [[5.1]], -example = [[ -pcall(module, "xuxu") -assert(debug.getfenv(pcall) == xuxu) -]], -patch = [[ ---- loadlib.c 2007/12/28 14:58:43 1.52.1.2 -+++ loadlib.c 2008/08/05 19:39:00 -@@ -506,8 +506,11 @@ - - static void setfenv (lua_State *L) { - lua_Debug ar; -- lua_getstack(L, 1, &ar); -- lua_getinfo(L, "f", &ar); -+ if (lua_getstack(L, 1, &ar) == 0 || -+ lua_getinfo(L, "f", &ar) == 0 || /* get calling function */ -+ lua_iscfunction(L, -1)) -+ luaL_error(L, "function " LUA_QL("module") -+ " not called from a Lua function"); - lua_pushvalue(L, -2); - lua_setfenv(L, -2); - lua_pop(L, 1); -]], -} - - -Bug{ -what = [[internal macro 'svalue' is wrong]], -report = [[Martijn van Buul, on 2008/08/04]], -since = [[5.1]], -example = [[ -/* in luaconf.h */ -#define LUAI_USER_ALIGNMENT_T union { char b[32]; } -]], -patch = [[ ---- lobject.h 2007/12/27 13:02:25 2.20.1.1 -+++ lobject.h 2008/08/05 19:40:48 -@@ -210,3 +210,3 @@ - #define getstr(ts) cast(const char *, (ts) + 1) --#define svalue(o) getstr(tsvalue(o)) -+#define svalue(o) getstr(rawtsvalue(o)) - -]], -} - - ------------------------------------------------------------------ --- Lua 5.1.4 - -Bug{ -what = [[malicious zero-length string in binary code may segfault Lua]], -report = [[Peter Cawley, on 2008/09/01]], -since = [[5.1]], -example = [[ -loadstring(('').dump(function()X''end):gsub('\2%z%z%zX','\0\0\0'))() -]], -patch = [[ -]], -} - - -Bug{ -what = [[wrong code generation for some particular boolean expressions]], -report = [[Brian Kelley, on 2009/04/15]], -since = [[5.0]], -example = [[ -print(((1 or false) and true) or false) --> 1 --- should be 'true' -]], -patch = [[ ---- lcode.c 2007/12/28 15:32:23 2.25.1.3 -+++ lcode.c 2009/06/15 14:07:34 -@@ -544,15 +544,18 @@ - pc = NO_JUMP; /* always true; do nothing */ - break; - } -- case VFALSE: { -- pc = luaK_jump(fs); /* always jump */ -- break; -- } - case VJMP: { - invertjump(fs, e); - pc = e->u.s.info; - break; - } -+ case VFALSE: { -+ if (!hasjumps(e)) { -+ pc = luaK_jump(fs); /* always jump */ -+ break; -+ } -+ /* else go through */ -+ } - default: { - pc = jumponcond(fs, e, 0); - break; -@@ -572,14 +575,17 @@ - pc = NO_JUMP; /* always false; do nothing */ - break; - } -- case VTRUE: { -- pc = luaK_jump(fs); /* always jump */ -- break; -- } - case VJMP: { - pc = e->u.s.info; - break; - } -+ case VTRUE: { -+ if (!hasjumps(e)) { -+ pc = luaK_jump(fs); /* always jump */ -+ break; -+ } -+ /* else go through */ -+ } - default: { - pc = jumponcond(fs, e, 1); - break; -]], -} - -Bug{ -what = [['luaV_settable' may invalidate a reference to a table and try -to reuse it]], -report = [[Mark Feldman, on 2009/06/27]], -since = [[5.0]], -example = [[ -grandparent = {} -grandparent.__newindex = function(s,_,_) print(s) end - -parent = {} -parent.__newindex = parent -setmetatable(parent, grandparent) - -child = setmetatable({}, parent) -child.foo = 10 --> (crash on some machines) -]], -patch = [[ ---- lvm.c 2007/12/28 15:32:23 2.63.1.3 -+++ lvm.c 2009/07/01 20:36:59 -@@ -133,6 +133,7 @@ - - void luaV_settable (lua_State *L, const TValue *t, TValue *key, StkId val) { - int loop; -+ TValue temp; - for (loop = 0; loop < MAXTAGLOOP; loop++) { - const TValue *tm; - if (ttistable(t)) { /* `t' is a table? */ -@@ -152,7 +153,9 @@ - callTM(L, tm, t, key, val); - return; - } -- t = tm; /* else repeat with `tm' */ -+ /* else repeat with `tm' */ -+ setobj(L, &temp, tm); /* avoid pointing inside table (may rehash) */ -+ t = &temp; - } - luaG_runerror(L, "loop in settable"); - } -]], -} - -Bug{ -what = [[smart use of varargs may create functions that return too -many arguments and overflow the stack of C functions]], -report = [[Patrick Donnelly, on 2008/12/10]], -since = [[]], -example = [[ -local function lunpack(i, ...) - if i == 0 then return ... - else - return lunpack(i-1, 1, ...) - end -end - -Now, if C calls lunpack(n) with a huge n, it may end with -too many values in its stack and confuse its stack indices. -]], -patch = [[ -]], -} - -Bug{ -what = [['debug.getfenv' does not check whether it has an argument]], -report = [[Patrick Donnelly, 2009/07/30]], -since = [[5.1]], -example = [[debug.getfenv() -- should raise an error]], -patch = [[ ---- ldblib.c 2008/01/21 13:11:21 1.104.1.3 -+++ ldblib.c 2009/08/04 18:43:12 -@@ -45,6 +45,7 @@ - - - static int db_getfenv (lua_State *L) { -+ luaL_checkany(L, 1); - lua_getfenv(L, 1); - return 1; - } -]], -} - -Bug{ -what = [[GC may get stuck during a parser and avoids proper resizing of -the string table, -making its lists grow too much and degrading performance]], -report = [[Sean Conner, 2009/11/10]], -since = [[5.1]], -example = [[See http://lua-users.org/lists/lua-l/2009-11/msg00463.html]], -patch = [[ ---- llex.c 2007/12/27 13:02:25 2.20.1.1 -+++ llex.c 2009/11/23 14:49:40 -@@ -118,8 +118,10 @@ - lua_State *L = ls->L; - TString *ts = luaS_newlstr(L, str, l); - TValue *o = luaH_setstr(L, ls->fs->h, ts); /* entry for `str' */ -- if (ttisnil(o)) -+ if (ttisnil(o)) { - setbvalue(o, 1); /* make sure `str' will not be collected */ -+ luaC_checkGC(L); -+ } - return ts; - } - -]] -} - -Bug{ -what = [['string.format' may get buffer as an argument when there are -missing arguments and format string is too long]], -report = [[Roberto I., 2010/04/12]], -since = [[5.0]], -example = [[ -x = string.rep("x", 10000) .. "%d" -print(string.format(x)) -- gives wrong error message -]], -patch = [[ ---- lstrlib.c 2008/07/11 17:27:21 1.132.1.4 -+++ lstrlib.c 2010/05/14 15:12:53 -@@ -754,6 +754,7 @@ - - - static int str_format (lua_State *L) { -+ int top = lua_gettop(L); - int arg = 1; - size_t sfl; - const char *strfrmt = luaL_checklstring(L, arg, &sfl); -@@ -768,7 +769,8 @@ - else { /* format item */ - char form[MAX_FORMAT]; /* to store the format (`%...') */ - char buff[MAX_ITEM]; /* to store the formatted item */ -- arg++; -+ if (++arg > top) -+ luaL_argerror(L, arg, "no value"); - strfrmt = scanformat(L, strfrmt, form); - switch (*strfrmt++) { - case 'c': { -]] -} - -Bug{ -what = [['io.read(op, "*n")' may return garbage if second read fails]], -report = [[Roberto I., 2010/04/12]], -since = [[5.0]], -example = [[ -print(io.read("*n", "*n")) --<< enter "10 hi" ---> file (0x884420) nil -]], -patch = [[ ---- liolib.c 2008/01/18 17:47:43 2.73.1.3 -+++ liolib.c 2010/05/14 15:29:29 -@@ -276,7 +276,10 @@ - lua_pushnumber(L, d); - return 1; - } -- else return 0; /* read fails */ -+ else { -+ lua_pushnil(L); /* "result" to be removed */ -+ return 0; /* read fails */ -+ } - } - - -]] -} - -Bug{ -what = [[wrong code generation for some particular boolean expressions]], -report = [[Thierry Van Elsuwe, 2011/01/20]], -since = [[5.0]], -example = [[ -print((('hi' or true) and true) or true) ---> hi (should be true) -print(((nil and nil) or false) and true) ---> nil (should be false) -]], -patch = [[ ---- lcode.c 2009/06/15 14:12:25 2.25.1.4 -+++ lcode.c 2011/01/31 14:44:25 -@@ -549,13 +549,6 @@ - pc = e->u.s.info; - break; - } -- case VFALSE: { -- if (!hasjumps(e)) { -- pc = luaK_jump(fs); /* always jump */ -- break; -- } -- /* else go through */ -- } - default: { - pc = jumponcond(fs, e, 0); - break; -@@ -579,13 +572,6 @@ - pc = e->u.s.info; - break; - } -- case VTRUE: { -- if (!hasjumps(e)) { -- pc = luaK_jump(fs); /* always jump */ -- break; -- } -- /* else go through */ -- } - default: { - pc = jumponcond(fs, e, 1); - break; -]] -} - -Bug{ -what = [[__newindex metamethod may not work if metatable is its own -metatable]], -report = [[Cuero Bugot, 2011/08/09]], -since = [[5.1]], -example = [[ -meta={} -setmetatable(meta, meta) -meta.__newindex = function(t, key, value) print("set") end -o = setmetatable({}, meta) -o.x = 10 -- should print 'set' -]], -patch = [[ ---- lvm.c 2009/07/01 21:10:33 2.63.1.4 -+++ lvm.c 2011/08/17 20:36:28 -@@ -142,6 +142,7 @@ - if (!ttisnil(oldval) || /* result is no nil? */ - (tm = fasttm(L, h->metatable, TM_NEWINDEX)) == NULL) { /* or no TM? */ - setobj2t(L, oldval, val); -+ h->flags = 0; - luaC_barriert(L, h, val); - return; - } -]] -} - -Bug{ -what = [[parser may collect a prototype while building it]], -report = [[Ingo van Lil, 2011/10/13]], -since = [[5.1.4 (caused by patch 5.1.4-6)]], -example = nil, -patch = [[ ---- lparser.c 2007/12/28 15:32:23 2.42.1.3 -+++ lparser.c 2011/10/17 13:10:43 -@@ -374,9 +374,9 @@ - lua_assert(luaG_checkcode(f)); - lua_assert(fs->bl == NULL); - ls->fs = fs->prev; -- L->top -= 2; /* remove table and prototype from the stack */ - /* last token read was anchored in defunct function; must reanchor it */ - if (fs) anchor_token(ls); -+ L->top -= 2; /* remove table and prototype from the stack */ - } - - -]] -} - - -Bug{ -what = [[When loading a file, -Lua may call the reader function again after it returned end of input -]], -report = [[Chris Howie, 2013/06/05]], -since = [[5.1]], -fix = [[5.2]], -example = [[ -load(function () print("called"); return nil end) ---> called ---> called (should be called only once!) -]], -patch = [[ ---- lzio.h 2007/12/27 13:02:25 1.21.1.1 -+++ lzio.h 2013/07/04 13:55:59 -@@ -59,6 +59,7 @@ - lua_Reader reader; - void* data; /* additional data */ - lua_State *L; /* Lua state (for reader) */ -+ int eoz; /* true if reader has no more data */ - }; - - ---- lzio.c 2007/12/27 13:02:25 1.31.1.1 -+++ lzio.c 2013/07/04 13:53:06 -@@ -22,10 +22,14 @@ - size_t size; - lua_State *L = z->L; - const char *buff; -+ if (z->eoz) return EOZ; - lua_unlock(L); - buff = z->reader(L, z->data, &size); - lua_lock(L); -- if (buff == NULL || size == 0) return EOZ; -+ if (buff == NULL || size == 0) { -+ z->eoz = 1; /* avoid calling reader function next time */ -+ return EOZ; -+ } - z->n = size - 1; - z->p = buff; - return char2int(*(z->p++)); -@@ -51,6 +55,7 @@ - z->data = data; - z->n = 0; - z->p = NULL; -+ z->eoz = 0; - } -]] -} - - ------------------------------------------------------------------ --- Lua 5.2.0 - -Bug{ -what = [[memory hoarding when creating Lua hooks for coroutines]], -report = [[Arseny Vakhrushev, 2012/01/16]], -since = [[5.1]], -fix = [[5.2.1]], -example = [[ -collectgarbage(); print(collectgarbage'count' * 1024) - -for i = 1, 100 do - local co = coroutine.create(function () end) - local x = {} - for j=1,1000 do x[j] = j end - debug.sethook(co, function () return x end, 'l') -end - -collectgarbage(); print(collectgarbage'count' * 1024) --- value should back to near the original level -]], -patch = [[ --- For 5.2 - ---- ldblib.c 2011/10/24 14:54:05 1.131 -+++ ldblib.c 2012/01/18 02:36:59 -@@ -253,14 +253,15 @@ - } - - --#define gethooktable(L) luaL_getsubtable(L, LUA_REGISTRYINDEX, HOOKKEY); -+#define gethooktable(L) luaL_getsubtable(L, LUA_REGISTRYINDEX, HOOKKEY) - - - static void hookf (lua_State *L, lua_Debug *ar) { - static const char *const hooknames[] = - {"call", "return", "line", "count", "tail call"}; - gethooktable(L); -- lua_rawgetp(L, -1, L); -+ lua_pushthread(L); -+ lua_rawget(L, -2); - if (lua_isfunction(L, -1)) { - lua_pushstring(L, hooknames[(int)ar->event]); - if (ar->currentline >= 0) -@@ -306,10 +307,15 @@ - count = luaL_optint(L, arg+3, 0); - func = hookf; mask = makemask(smask, count); - } -- gethooktable(L); -+ if (gethooktable(L) == 0) { /* creating hook table? */ -+ lua_pushstring(L, "k"); -+ lua_setfield(L, -2, "__mode"); /** hooktable.__mode = "k" */ -+ lua_pushvalue(L, -1); -+ lua_setmetatable(L, -2); /* setmetatable(hooktable) = hooktable */ -+ } -+ lua_pushthread(L1); lua_xmove(L1, L, 1); - lua_pushvalue(L, arg+1); -- lua_rawsetp(L, -2, L1); /* set new hook */ -- lua_pop(L, 1); /* remove hook table */ -+ lua_rawset(L, -3); /* set new hook */ - lua_sethook(L1, func, mask, count); /* set hooks */ - return 0; - } -@@ -325,7 +331,8 @@ - lua_pushliteral(L, "external hook"); - else { - gethooktable(L); -- lua_rawgetp(L, -1, L1); /* get hook */ -+ lua_pushthread(L1); lua_xmove(L1, L, 1); -+ lua_rawget(L, -2); /* get hook */ - lua_remove(L, -2); /* remove hook table */ - } - lua_pushstring(L, unmakemask(mask, buff)); -]] -} - -Bug{ -what = [[Lexical gets confused with some combination of arithmetic -operators and hexadecimal numbers]], -report = [[Alexandra Barros, 2012/01/17]], -since = [[5.2.0]], -fix = [[5.2.1]], -example = [[print(0xE+1)]], -patch = [[ ---- llex.c 2011/11/30 12:43:51 2.59 -+++ llex.c 2012/01/20 18:22:50 -@@ -223,12 +223,19 @@ - - /* LUA_NUMBER */ - static void read_numeral (LexState *ls, SemInfo *seminfo) { -+ const char *expo = "Ee"; -+ int first = ls->current; - lua_assert(lisdigit(ls->current)); -- do { -- save_and_next(ls); -- if (check_next(ls, "EePp")) /* exponent part? */ -+ save_and_next(ls); -+ if (first == '0' && check_next(ls, "Xx")) /* hexadecimal? */ -+ expo = "Pp"; -+ for (;;) { -+ if (check_next(ls, expo)) /* exponent part? */ - check_next(ls, "+-"); /* optional exponent sign */ -- } while (lislalnum(ls->current) || ls->current == '.'); -+ if (lisxdigit(ls->current) || ls->current == '.') -+ save_and_next(ls); -+ else break; -+ } - save(ls, '\0'); - buffreplace(ls, '.', ls->decpoint); /* follow locale for decimal point */ - if (!buff2d(ls->buff, &seminfo->r)) /* format error? */ -]] -} - -Bug{ -what = [[Finalizers may call functions from a dynamic library after -the library has been unloaded]], -report = [[Josh Haberman, 2012/04/08]], -since = [[5.1]], -fix = [[5.2.1]], -example = [[ -local u = setmetatable({}, {__gc = function () foo() end}) -local m = require 'mod' -- 'mod' may be any dynamic library written in C -foo = m.foo -- 'foo' may be any function from 'mod' --- end program; it crashes -]], -patch = [[ -loadlib.c: -95c95 -< #define LIBPREFIX "LOADLIB: " ---- -> #define CLIBS "_CLIBS" -251,266c251,256 -< -< static void **ll_register (lua_State *L, const char *path) { -< void **plib; -< lua_pushfstring(L, "%s%s", LIBPREFIX, path); -< lua_gettable(L, LUA_REGISTRYINDEX); /* check library in registry? */ -< if (!lua_isnil(L, -1)) /* is there an entry? */ -< plib = (void **)lua_touserdata(L, -1); -< else { /* no entry yet; create one */ -< lua_pop(L, 1); /* remove result from gettable */ -< plib = (void **)lua_newuserdata(L, sizeof(const void *)); -< *plib = NULL; -< luaL_setmetatable(L, "_LOADLIB"); -< lua_pushfstring(L, "%s%s", LIBPREFIX, path); -< lua_pushvalue(L, -2); -< lua_settable(L, LUA_REGISTRYINDEX); -< } ---- -> static void *ll_checkclib (lua_State *L, const char *path) { -> void *plib; -> lua_getfield(L, LUA_REGISTRYINDEX, CLIBS); -> lua_getfield(L, -1, path); -> plib = lua_touserdata(L, -1); /* plib = CLIBS[path] */ -> lua_pop(L, 2); /* pop CLIBS table and 'plib' */ -270a261,270 -> static void ll_addtoclib (lua_State *L, const char *path, void *plib) { -> lua_getfield(L, LUA_REGISTRYINDEX, CLIBS); -> lua_pushlightuserdata(L, plib); -> lua_pushvalue(L, -1); -> lua_setfield(L, -3, path); /* CLIBS[path] = plib */ -> lua_rawseti(L, -2, luaL_len(L, -2) + 1); /* CLIBS[#CLIBS + 1] = plib */ -> lua_pop(L, 1); /* pop CLIBS table */ -> } -> -> -272,273c272,273 -< ** __gc tag method: calls library's `ll_unloadlib' function with the lib -< ** handle ---- -> ** __gc tag method for CLIBS table: calls 'll_unloadlib' for all lib -> ** handles in list CLIBS -276,278c276,281 -< void **lib = (void **)luaL_checkudata(L, 1, "_LOADLIB"); -< if (*lib) ll_unloadlib(*lib); -< *lib = NULL; /* mark library as closed */ ---- -> int n = luaL_len(L, 1); -> for (; n >= 1; n--) { /* for each handle, in reverse order */ -> lua_rawgeti(L, 1, n); /* get handle CLIBS[n] */ -> ll_unloadlib(lua_touserdata(L, -1)); -> lua_pop(L, 1); /* pop handle */ -> } -284,286c287,292 -< void **reg = ll_register(L, path); -< if (*reg == NULL) *reg = ll_load(L, path, *sym == '*'); -< if (*reg == NULL) return ERRLIB; /* unable to load library */ ---- -> void *reg = ll_checkclib(L, path); /* check loaded C libraries */ -> if (reg == NULL) { /* must load library? */ -> reg = ll_load(L, path, *sym == '*'); -> if (reg == NULL) return ERRLIB; /* unable to load library */ -> ll_addtoclib(L, path, reg); -> } -292c298 -< lua_CFunction f = ll_sym(L, *reg, sym); ---- -> lua_CFunction f = ll_sym(L, reg, sym); -675,676c681,683 -< /* create new type _LOADLIB */ -< luaL_newmetatable(L, "_LOADLIB"); ---- -> /* create table CLIBS to keep track of loaded C libraries */ -> luaL_getsubtable(L, LUA_REGISTRYINDEX, CLIBS); -> lua_createtable(L, 0, 1); /* metatable for CLIBS */ -678a686 -> lua_setmetatable(L, -2); -]] -} - -Bug{ -what = [[wrong handling of 'nCcalls' in coroutines]], -report = [[Alexander Gavrilov, 2012/04/18]], -since = [[5.2.0]], -fix = [[5.2.1]], -example = [[ -coroutine.wrap(function() - print(pcall(pcall,pcall,pcall,pcall,pcall,error,3)) -end)() -]], -patch = [[ ---- ldo.c 2011/11/29 15:55:08 2.102 -+++ ldo.c 2012/04/26 20:38:32 -@@ -402,8 +402,6 @@ - int n; - lua_assert(ci->u.c.k != NULL); /* must have a continuation */ - lua_assert(L->nny == 0); -- /* finish 'luaD_call' */ -- L->nCcalls--; - /* finish 'lua_callk' */ - adjustresults(L, ci->nresults); - /* call continuation function */ -@@ -513,7 +511,6 @@ - api_checknelems(L, n); - firstArg = L->top - n; /* yield results come from continuation */ - } -- L->nCcalls--; /* finish 'luaD_call' */ - luaD_poscall(L, firstArg); /* finish 'luaD_precall' */ - } - unroll(L, NULL); -]] -} - -Bug{ -what = [[Internal Lua values may escape through the debug API]], -report = [[Dan Tull, 2012/04/20]], -since = [[5.1]], -fix = [[5.2.1]], -example = [[ --- for Lua 5.1 -local firsttime = true -local function foo () - if firsttime then - firsttime = false - return "a = 1" - else - for i = 1, 10 do - print(debug.getlocal(2, i)) - end - end -end - -print(load(foo)) -- prints some lines and then seg. fault. -]], -patch = [[ -]] -} - -Bug{ -what = [[Problems when yielding from debug hooks]], -report = [[Erik Cassel, 2012/06/05]], -since = [[5.2.0]], -fix = [[5.2.1]], -example = [[ -Set, in C, a line hook that simply yields, -and then call any Lua function. -You get an infinite loop of yields. -]], -patch = [[ -]] -} - - ------------------------------------------------------------------ --- Lua 5.2.1 - -Bug{ -what = [[Some patterns can overflow the C stack, due to recursion]], -report = [[Tim Starling, 2012/07/08]], -since = [[2.5]], -fix = [[5.2.2]], -example = [[print(string.find(string.rep("a", 2^20), string.rep(".?", 2^20)))]], -patch = [[ -]] -} - - -Bug{ -what = [['pcall' may not restore previous error function when -inside coroutines]], -report = [[Alexander Gavrilov, 2012/06/12]], -since = [[5.2.0]], -fix = [[5.2.2]], -example = [[ -function errfunc(x) - return 'errfunc' -end - -function test(do_yield) - print(do_yield and "yielding" or "not yielding") - pcall(function() -- this pcall sets errfunc back to none - if do_yield then - coroutine.yield() -- stops errfunc from being restored - end - end) - error('fail!') -end - -coro = coroutine.wrap(function() - print(xpcall(test, errfunc, false)) - print(xpcall(test, errfunc, true)) - print(xpcall(test, errfunc, false)) -end) - -coro() ---> not yielding ---> false errfunc ---> yielding -coro() ---> false temp:12: fail! <<<< should be 'errfunc' too ---> not yielding ---> false errfunc -]], -patch = [[ ---- ldo.c 2012/08/28 18:30:45 2.107 -+++ ldo.c 2012/09/23 15:49:55 -@@ -403,7 +403,11 @@ - int n; - lua_assert(ci->u.c.k != NULL); /* must have a continuation */ - lua_assert(L->nny == 0); -- /* finish 'lua_callk' */ -+ if (ci->callstatus & CIST_YPCALL) { /* was inside a pcall? */ -+ ci->callstatus &= ~CIST_YPCALL; /* finish 'lua_pcall' */ -+ L->errfunc = ci->u.c.old_errfunc; -+ } -+ /* finish 'lua_callk'/'lua_pcall' */ - adjustresults(L, ci->nresults); - /* call continuation function */ - if (!(ci->callstatus & CIST_STAT)) /* no call status? */ -]] -} - -Bug{ -what = [[Check for garbage collector in function calls does not cover -all paths]], -report = [[Roberto, 2012/08/15]], -since = [[5.2.1]], -fix = [[5.2.2]], -example = [[ -See -http://lua-users.org/lists/lua-l/2012-08/msg00149.html -]], -patch = [[ -@@ -311,6 +311,7 @@ - ci->top = L->top + LUA_MINSTACK; - lua_assert(ci->top <= L->stack_last); - ci->callstatus = 0; -+ luaC_checkGC(L); /* stack grow uses memory */ - if (L->hookmask & LUA_MASKCALL) - luaD_hook(L, LUA_HOOKCALL, -1); - lua_unlock(L); -@@ -338,6 +339,7 @@ - ci->u.l.savedpc = p->code; /* starting point */ - ci->callstatus = CIST_LUA; - L->top = ci->top; -+ luaC_checkGC(L); /* stack grow uses memory */ - if (L->hookmask & LUA_MASKCALL) - callhook(L, ci); - return 0; -@@ -393,7 +395,6 @@ - luaV_execute(L); /* call it */ - if (!allowyield) L->nny--; - L->nCcalls--; -- luaC_checkGC(L); - } -]] -} - -Bug{ -what = [[load/loadfile returns wrong result when given an environment -for a binary chunk with no upvalues]], -report = [[Vladimir Strakh, 2012/11/28]], -since = [[5.2.0]], -fix = [[5.2.2]], -example = [[ -f = load(string.dump(function () return 1 end), nil, "b", {}) -print(type(f)) --> table (whould be a function) -]], -patch = [[ ---- lbaselib.c 2012/04/27 14:13:19 1.274 -+++ lbaselib.c 2012/12/03 20:08:15 -@@ -244,5 +244,11 @@ - --static int load_aux (lua_State *L, int status) { -- if (status == LUA_OK) -+static int load_aux (lua_State *L, int status, int envidx) { -+ if (status == LUA_OK) { -+ if (envidx != 0) { /* 'env' parameter? */ -+ lua_pushvalue(L, envidx); /* environment for loaded function */ -+ if (!lua_setupvalue(L, -2, 1)) /* set it as 1st upvalue */ -+ lua_pop(L, 1); /* remove 'env' if not used by previous call */ -+ } - return 1; -+ } - else { -@@ -258,9 +264,5 @@ - const char *mode = luaL_optstring(L, 2, NULL); -- int env = !lua_isnone(L, 3); /* 'env' parameter? */ -+ int env = (!lua_isnone(L, 3) ? 3 : 0); /* 'env' index or 0 if no 'env' */ - int status = luaL_loadfilex(L, fname, mode); -- if (status == LUA_OK && env) { /* 'env' parameter? */ -- lua_pushvalue(L, 3); -- lua_setupvalue(L, -2, 1); /* set it as 1st upvalue of loaded chunk */ -- } -- return load_aux(L, status); -+ return load_aux(L, status, env); - } -@@ -309,5 +311,5 @@ - size_t l; -- int top = lua_gettop(L); - const char *s = lua_tolstring(L, 1, &l); - const char *mode = luaL_optstring(L, 3, "bt"); -+ int env = (!lua_isnone(L, 4) ? 4 : 0); /* 'env' index or 0 if no 'env' */ - if (s != NULL) { /* loading a string? */ -@@ -322,7 +324,3 @@ - } -- if (status == LUA_OK && top >= 4) { /* is there an 'env' argument */ -- lua_pushvalue(L, 4); /* environment for loaded function */ -- lua_setupvalue(L, -2, 1); /* set it as 1st upvalue */ -- } -- return load_aux(L, status); -+ return load_aux(L, status, env); - } -]] -} - -Bug{ -what = [[Lua does not check memory use when creating error messages]], -report = [[John Dunn, 2012/09/24]], -since = [[5.2.0]], -fix = nil, -example = [[ -local code = "function test()\n bob.joe.larry = 23\n end" - -load(code)() - --- memory will grow steadly -for i = 1, math.huge do - pcall(test) - if i % 100000 == 0 then - io.write(collectgarbage'count'*1024, "\n") - end -end -]], -patch = [[ -]] -} - - - - - ------------------------------------------------------------------ --- Lua 5.2.2 - - -Bug{ -what = [[stack overflow in vararg functions with many fixed -parameters called with few arguments]], -report = [[云风, 2013/04/17]], -since = [[5.1]], -fix = [[5.2.3]], -example = [[ -function f(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, - p11, p12, p13, p14, p15, p16, p17, p18, p19, p20, - p21, p22, p23, p24, p25, p26, p27, p28, p29, p30, - p31, p32, p33, p34, p35, p36, p37, p38, p39, p40, - p41, p42, p43, p44, p45, p46, p48, p49, p50, ...) - local a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14 -end - -f() -- seg. fault (on some machines) -]], -patch = [[ ---- ldo.c 2012/10/01 14:05:04 2.108 -+++ ldo.c 2013/04/19 20:56:06 -@@ -324,7 +324,7 @@ - case LUA_TLCL: { /* Lua function: prepare its call */ - StkId base; - Proto *p = clLvalue(func)->p; -- luaD_checkstack(L, p->maxstacksize); -+ luaD_checkstack(L, p->maxstacksize + p->numparams); - func = restorestack(L, funcr); - n = cast_int(L->top - func) - 1; /* number of real arguments */ - for (; n < p->numparams; n++) -]], -} - -Bug{ -what = [[garbage collector can trigger too many times in recursive loops]], -report = [[Roberto, 2013/04/25]], -since = [[5.2.2]], -fix = [[5.2.3]], -example = [[ -function f() f() end -f() -- it takes too long before a "stack overflow" error -]], -patch = [[ ---- lgc.c 2013/04/12 18:48:47 2.140.1.1 -+++ lgc.c 2013/04/25 21:30:20 -@@ -495,2 +495,3 @@ - static lu_mem traversestack (global_State *g, lua_State *th) { -+ int n = 0; - StkId o = th->stack; -@@ -505,3 +506,9 @@ - } -- return sizeof(lua_State) + sizeof(TValue) * th->stacksize; -+ else { /* count call infos to compute size */ -+ CallInfo *ci; -+ for (ci = &th->base_ci; ci != th->ci; ci = ci->next) -+ n++; -+ } -+ return sizeof(lua_State) + sizeof(TValue) * th->stacksize + -+ sizeof(CallInfo) * n; - } -]] -} - --- [[]] -Bug{ -what = [[Wrong assert when reporting concatenation errors -(manifests only when Lua is compiled in debug mode)]], -report = [[Roberto, 2013/05/05]], -since = [[?]], -fix = [[5.2.3]], -example = [[ --- only with Lua compiled in debug mode -print({} .. 2) -]], -patch = [[ ---- ldebug.c 2013/04/12 18:48:47 2.90.1.1 -+++ ldebug.c 2013/05/05 14:38:30 -@@ -519,5 +519,5 @@ - l_noret luaG_concaterror (lua_State *L, StkId p1, StkId p2) { - if (ttisstring(p1) || ttisnumber(p1)) p1 = p2; -- lua_assert(!ttisstring(p1) && !ttisnumber(p2)); -+ lua_assert(!ttisstring(p1) && !ttisnumber(p1)); - luaG_typeerror(L, p1, "concatenate"); - } -]] -} - -Bug{ -what = [[Wrong error message in some short-cut expressions]], -report = [[Egor Skriptunoff, 2013/05/10]], -since = [[5.0]], -fix = [[5.2.3]], -example = [[ -> a,b,c = true,true,true -> (a and b or c)('', '') -stdin:1: attempt to call a boolean value (global 'c') - - (It should be global 'b' instead of 'c'.) -]], -patch = [[ ---- ldebug.c 2013/05/06 17:20:22 2.90.1.2 -+++ ldebug.c 2013/05/14 19:52:48 -@@ -327,12 +327,20 @@ - } - - -+static int filterpc (int pc, int jmptarget) { -+ if (pc < jmptarget) /* is code conditional (inside a jump)? */ -+ return -1; /* cannot know who sets that register */ -+ else return pc; /* current position sets that register */ -+} -+ -+ - /* - ** try to find last instruction before 'lastpc' that modified register 'reg' - */ - static int findsetreg (Proto *p, int lastpc, int reg) { - int pc; - int setreg = -1; /* keep last instruction that changed 'reg' */ -+ int jmptarget = 0; /* any code before this address is conditional */ - for (pc = 0; pc < lastpc; pc++) { - Instruction i = p->code[pc]; - OpCode op = GET_OPCODE(i); -@@ -341,33 +349,38 @@ - case OP_LOADNIL: { - int b = GETARG_B(i); - if (a <= reg && reg <= a + b) /* set registers from 'a' to 'a+b' */ -- setreg = pc; -+ setreg = filterpc(pc, jmptarget); - break; - } - case OP_TFORCALL: { -- if (reg >= a + 2) setreg = pc; /* affect all regs above its base */ -+ if (reg >= a + 2) /* affect all regs above its base */ -+ setreg = filterpc(pc, jmptarget); - break; - } - case OP_CALL: - case OP_TAILCALL: { -- if (reg >= a) setreg = pc; /* affect all registers above base */ -+ if (reg >= a) /* affect all registers above base */ -+ setreg = filterpc(pc, jmptarget); - break; - } - case OP_JMP: { - int b = GETARG_sBx(i); - int dest = pc + 1 + b; - /* jump is forward and do not skip `lastpc'? */ -- if (pc < dest && dest <= lastpc) -- pc += b; /* do the jump */ -+ if (pc < dest && dest <= lastpc) { -+ if (dest > jmptarget) -+ jmptarget = dest; /* update 'jmptarget' */ -+ } - break; - } - case OP_TEST: { -- if (reg == a) setreg = pc; /* jumped code can change 'a' */ -+ if (reg == a) /* jumped code can change 'a' */ -+ setreg = filterpc(pc, jmptarget); - break; - } - default: - if (testAMode(op) && reg == a) /* any instruction that set A */ -- setreg = pc; -+ setreg = filterpc(pc, jmptarget); - break; - } - } -]] -} - -Bug{ -what = [[luac listings choke on long strings]], -report = [[Ashwin Hirschi, 2013/07/03]], -since = [[5.1.2]], -fix = [[5.2.3]], -example = [[ --- When you call 'luac -l' over this chunk, it chokes the output -s="Lorem ipsum dolor sit amet, consectetur, " -]], -patch = [[ ---- luac.c 2011-11-29 15:46:33 -0200 1.69 -+++ luac.c 2013-07-03 21:26:01 -0300 -@@ -251,7 +251,7 @@ - static void PrintConstant(const Proto* f, int i) - { - const TValue* o=&f->k[i]; -- switch (ttype(o)) -+ switch (ttypenv(o)) - { - case LUA_TNIL: - printf("nil"); -]] -} - -Bug{ -what = [[GC can collect a long string still in use during parser]], -report = [[Roberto, 2013/08/30]], -since = [[5.2]], -fix = [[5.2.3]], -example = [[This bug is very difficult to happen (and to reproduce), -because it depends on the GC running in a very specific way when -parsing a source code with long (larger than 40 characters) identifiers.]], -patch = [[ ---- ltable.h 2013/04/12 18:48:47 2.16.1.1 -+++ ltable.h 2013/08/30 15:34:24 -@@ -18,4 +18,8 @@ - #define invalidateTMcache(t) ((t)->flags = 0) - -+/* returns the key, given the value of a table entry */ -+#define keyfromval(v) \ -+ (gkey(cast(Node *, cast(char *, (v)) - offsetof(Node, i_val)))) -+ - - LUAI_FUNC const TValue *luaH_getint (Table *t, int key); - ---- llex.c 2013/04/12 18:48:47 2.63.1.1 -+++ llex.c 2013/08/30 15:34:59 -@@ -134,4 +134,7 @@ - luaC_checkGC(L); - } -+ else { /* string already present */ -+ ts = rawtsvalue(keyfromval(o)); /* re-use value previously stored */ -+ } - L->top--; /* remove string from stack */ - return ts; -]] -} - - -Bug{ -what = [[Call to macro 'luai_userstateclose' should be done only -after the calls to __gc methods.]], -report = [[Jean-Luc Jumpertz, 2013/09/02]], -since = [[ ]], -fix = nil, -example = [[No example]], -patch = [[ ---- lstate.c 2013/04/12 18:48:47 2.99.1.1 -+++ lstate.c 2013/11/08 17:39:57 -@@ -194,2 +194,4 @@ - g->gcrunning = 1; /* allow gc */ -+ g->version = lua_version(NULL); -+ luai_userstateopen(L); - } -@@ -224,2 +226,4 @@ - luaC_freeallobjects(L); /* collect all objects */ -+ if (g->version) /* closing a fully built state? */ -+ luai_userstateclose(L); - luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size); -@@ -289,3 +293,3 @@ - g->panic = NULL; -- g->version = lua_version(NULL); -+ g->version = NULL; - g->gcstate = GCSpause; -@@ -308,4 +312,2 @@ - } -- else -- luai_userstateopen(L); - return L; -@@ -317,3 +319,2 @@ - lua_lock(L); -- luai_userstateclose(L); - close_state(L); -]] -} - - -Bug{ -what = [[Resuming the running coroutine makes it unyieldable]], -report = [[Florian Nücke, 2013/10/28]], -since = [[5.2]], -fix = [[5.2.3]], -example = [[ --- should print 'true' -print(coroutine.resume(coroutine.create(function() - coroutine.resume(coroutine.running()) - coroutine.yield() -end))) -]], -patch = [[ ---- ldo.c 2013/04/19 21:03:23 2.108.1.2 -+++ ldo.c 2013/11/08 18:20:57 -@@ -536,2 +536,3 @@ - int status; -+ int oldnny = L->nny; /* save 'nny' */ - lua_lock(L); -@@ -557,3 +558,3 @@ - } -- L->nny = 1; /* do not allow yields */ -+ L->nny = oldnny; /* restore 'nny' */ - L->nCcalls--; -]] -} - - - ------------------------------------------------------------------ --- Lua 5.2.3 - -Bug{ -what = [[compiler can optimize away overflow check in 'table.unpack']], -report = [[Paige DePol, 2014/03/30]], -since = [[5.1 (at least)]], -fix = nil, -example = [[ -> unpack({}, 0, 2^31 - 1) -(segfaults on some platforms with some compiler options) -]], -patch = [[ ---- ltablib.c 2013/04/12 18:48:47 1.65.1.1 -+++ ltablib.c 2014/05/07 16:32:55 1.65.1.2 -@@ -134,13 +135,14 @@ - - - static int unpack (lua_State *L) { -- int i, e, n; -+ int i, e; -+ unsigned int n; - luaL_checktype(L, 1, LUA_TTABLE); - i = luaL_optint(L, 2, 1); - e = luaL_opt(L, luaL_checkint, 3, luaL_len(L, 1)); - if (i > e) return 0; /* empty range */ -- n = e - i + 1; /* number of elements */ -- if (n <= 0 || !lua_checkstack(L, n)) /* n <= 0 means arith. overflow */ -+ n = (unsigned int)e - (unsigned int)i; /* number of elements minus 1 */ -+ if (n > (INT_MAX - 10) || !lua_checkstack(L, ++n)) - return luaL_error(L, "too many results to unpack"); - lua_rawgeti(L, 1, i); /* push arg[i] (avoiding overflow problems) */ - while (i++ < e) /* push arg[i + 1...e] */ -]] -} - -Bug{ -what = [[Ephemeron table can wrongly collect entry with strong key]], -report = [[Jörg Richter, 2014/08/22]], -since = [[5.2]], -fix = nil, -example = [[ -(This bug is very hard to reproduce, -because it depends on a specific interleaving of -events between the incremental collector and the program.) -]], -patch = [[ ---- lgc.c 2013/04/26 18:22:05 2.140.1.2 -+++ lgc.c 2014/09/01 13:24:33 -@@ -403,7 +403,7 @@ - reallymarkobject(g, gcvalue(gval(n))); /* mark it now */ - } - } -- if (prop) -+ if (g->gcstate != GCSatomic || prop) - linktable(h, &g->ephemeron); /* have to propagate again */ - else if (hasclears) /* does table have white keys? */ - linktable(h, &g->allweak); /* may have to clean white keys */ -]] -} - -Bug{ -what = [[Chunk with too many lines can seg. fault]], -report = [[Roberto, 2014/11/14]], -since = [[5.1 (at least)]], -fix = nil, -example = [[ --- the cause of the bug is the use of an unitialized variable, so --- it cannot be reproduced reliably -local s = string.rep("\n", 2^24) -print(load(function () return s end)) -]], -patch = [[ ---- llex.c 2013/08/30 15:49:41 2.63.1.2 -+++ llex.c 2015/02/09 17:05:31 -@@ -153,5 +153,5 @@ - next(ls); /* skip `\n\r' or `\r\n' */ - if (++ls->linenumber >= MAX_INT) -- luaX_syntaxerror(ls, "chunk has too many lines"); -+ lexerror(ls, "chunk has too many lines", 0); - } - -]] -} - - ------------------------------------------------------------------ --- Lua 5.3.0 - -Bug{ -what = [['string.format("%f")' can cause a buffer overflow -(only when 'lua_Number' is long double!)]], -report = [[Roberto, 2015/01/13]], -since = [[5.3]], -fix = nil, -example = [[string.format("%.99f", 1e4000) -- when floats are long double]], -patch = [[ ---- lstrlib.c 2014/12/11 14:03:07 1.221 -+++ lstrlib.c 2015/02/23 19:01:42 -@@ -800,3 +800,4 @@ - /* maximum size of each formatted item (> len(format('%99.99f', -1e308))) */ --#define MAX_ITEM 512 -+#define MAX_ITEM \ -+ (sizeof(lua_Number) <= 4 ? 150 : sizeof(lua_Number) <= 8 ? 450 : 5050) - -]] -} - -Bug{ -what = [['debug.getlocal' on a coroutine suspended in a hook -can crash the interpreter]], -report = [[云风, 2015/02/11]], -since = [[5.2]], -fix = nil, -example = [[see http://lua-users.org/lists/lua-l/2015-02/msg00146.html]], -patch = [[ ---- ldebug.c 2015/01/02 12:52:22 2.110 -+++ ldebug.c 2015/02/13 16:03:23 -@@ -49,4 +49,14 @@ - - -+static void swapextra (lua_State *L) { -+ if (L->status == LUA_YIELD) { -+ CallInfo *ci = L->ci; /* get function that yielded */ -+ StkId temp = ci->func; /* exchange its 'func' and 'extra' values */ -+ ci->func = restorestack(L, ci->extra); -+ ci->extra = savestack(L, temp); -+ } -+} -+ -+ - /* - ** this function can be called asynchronous (e.g. during a signal) -@@ -145,4 +155,5 @@ - const char *name; - lua_lock(L); -+ swapextra(L); - if (ar == NULL) { /* information about non-active function? */ - if (!isLfunction(L->top - 1)) /* not a Lua function? */ -@@ -159,4 +170,5 @@ - } - } -+ swapextra(L); - lua_unlock(L); - return name; -@@ -166,10 +178,13 @@ - LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) { - StkId pos = 0; /* to avoid warnings */ -- const char *name = findlocal(L, ar->i_ci, n, &pos); -+ const char *name; - lua_lock(L); -+ swapextra(L); -+ name = findlocal(L, ar->i_ci, n, &pos); - if (name) { - setobjs2s(L, pos, L->top - 1); - L->top--; /* pop value */ - } -+ swapextra(L); - lua_unlock(L); - return name; -@@ -271,4 +286,5 @@ - StkId func; - lua_lock(L); -+ swapextra(L); - if (*what == '>') { - ci = NULL; -@@ -289,4 +305,5 @@ - api_incr_top(L); - } -+ swapextra(L); - if (strchr(what, 'L')) - collectvalidlines(L, cl); -]] -} - - -Bug{ -what = [[suspended '__le' metamethod can give wrong result]], -report = [[Eric Zhong, 2015/04/07]], -since = [[5.2]], -fix = nil, - -example = [[ -mt = {__le = function (a,b) coroutine.yield("yield"); return a.x <= b.x end} -t1 = setmetatable({x=1}, mt) -t2 = {x=2} -co = coroutine.wrap(function (a,b) return t2 <= t1 end) -co() -print(co()) --> true (should be false) -]], - -patch = [[ ---- lstate.h 2014/10/30 18:53:28 2.119 -+++ lstate.h 2015/04/13 15:58:40 -@@ -94,6 +94,7 @@ - #define CIST_YPCALL (1<<4) /* call is a yieldable protected call */ - #define CIST_TAIL (1<<5) /* call was tail called */ - #define CIST_HOOKYIELD (1<<6) /* last hook called yielded */ -+#define CIST_LEQ (1<<7) /* using __lt for __le */ - - #define isLua(ci) ((ci)->callstatus & CIST_LUA) - ---- lvm.c 2014/12/27 20:30:38 2.232 -+++ lvm.c 2015/04/13 15:51:30 -@@ -292,9 +292,14 @@ - return l_strcmp(tsvalue(l), tsvalue(r)) <= 0; - else if ((res = luaT_callorderTM(L, l, r, TM_LE)) >= 0) /* first try 'le' */ - return res; -- else if ((res = luaT_callorderTM(L, r, l, TM_LT)) < 0) /* else try 'lt' */ -- luaG_ordererror(L, l, r); -- return !res; -+ else { /* try 'lt': */ -+ L->ci->callstatus |= CIST_LEQ; /* mark it is doing 'lt' for 'le' */ -+ res = luaT_callorderTM(L, r, l, TM_LT); -+ L->ci->callstatus ^= CIST_LEQ; /* clear mark */ -+ if (res < 0) -+ luaG_ordererror(L, l, r); -+ return !res; /* result is negated */ -+ } - } - - -@@ -553,11 +558,11 @@ - case OP_LE: case OP_LT: case OP_EQ: { - int res = !l_isfalse(L->top - 1); - L->top--; -- /* metamethod should not be called when operand is K */ -- lua_assert(!ISK(GETARG_B(inst))); -- if (op == OP_LE && /* "<=" using "<" instead? */ -- ttisnil(luaT_gettmbyobj(L, base + GETARG_B(inst), TM_LE))) -- res = !res; /* invert result */ -+ if (ci->callstatus & CIST_LEQ) { /* "<=" using "<" instead? */ -+ lua_assert(op == OP_LE); -+ ci->callstatus ^= CIST_LEQ; /* clear mark */ -+ res = !res; /* negate result */ -+ } - lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_JMP); - if (res != GETARG_A(inst)) /* condition failed? */ - ci->u.l.savedpc++; /* skip jump instruction */ -]] -} - - -Bug{ -what = [[return hook may not see correct values for - active local variables when function returns]], -report = [[Philipp Janda/Peng Yi, 2015/05/19]], -since = [[5.0]], -fix = nil, -example = [[ -see messasge http://lua-users.org/lists/lua-l/2015-05/msg00376.html]], -patch = [[ -]] -} - - - ------------------------------------------------------------------ --- Lua 5.3.1 - -Bug{ -what = [['io.lines' does not check maximum number of options]], -report = [[Patrick Donnell, 2015/07/10]], -since = [[5.3.0]], -fix = nil, -example = [[ --- can segfault in some machines -t ={}; for i = 1, 253 do t[i] = 1 end -io.lines("someexistingfile", table.unpack(t))() -]], -patch = [[ ---- liolib.c 2015/07/07 17:03:34 2.146 -+++ liolib.c 2015/07/15 14:40:28 2.147 -@@ -318,8 +318,15 @@ - static int io_readline (lua_State *L); - - -+/* -+** maximum number of arguments to 'f:lines'/'io.lines' (it + 3 must fit -+** in the limit for upvalues of a closure) -+*/ -+#define MAXARGLINE 250 -+ - static void aux_lines (lua_State *L, int toclose) { - int n = lua_gettop(L) - 1; /* number of arguments to read */ -+ luaL_argcheck(L, n <= MAXARGLINE, MAXARGLINE + 2, "too many arguments"); - lua_pushinteger(L, n); /* number of arguments to read */ - lua_pushboolean(L, toclose); /* close/not close file when finished */ - lua_rotate(L, 2, 2); /* move 'n' and 'toclose' to their positions */ -]] -} - - ------------------------------------------------------------------ --- Lua 5.3.2 - -Bug{ -what = [[Metatable may access its own dealocated field when -it has a self reference in __newindex]], -report = [[actboy168@gmail.com, 2016/01/01]], -since = [[5.3.2]], -fix = nil, -example = [[ -local mt = {} -mt.__newindex = mt -local t = setmetatable({}, mt) -t[1] = 1 -- will segfault on some machines -]], -patch = [[ ---- lvm.c 2015/11/23 11:30:45 2.265 -+++ lvm.c 2016/01/01 14:34:12 -@@ -190,18 +190,19 @@ - for (loop = 0; loop < MAXTAGLOOP; loop++) { - const TValue *tm; - if (oldval != NULL) { -- lua_assert(ttistable(t) && ttisnil(oldval)); -+ Table *h = hvalue(t); /* save 't' table */ -+ lua_assert(ttisnil(oldval)); - /* must check the metamethod */ -- if ((tm = fasttm(L, hvalue(t)->metatable, TM_NEWINDEX)) == NULL && -+ if ((tm = fasttm(L, h->metatable, TM_NEWINDEX)) == NULL && - /* no metamethod; is there a previous entry in the table? */ - (oldval != luaO_nilobject || - /* no previous entry; must create one. (The next test is - always true; we only need the assignment.) */ -- (oldval = luaH_newkey(L, hvalue(t), key), 1))) { -+ (oldval = luaH_newkey(L, h, key), 1))) { - /* no metamethod and (now) there is an entry with given key */ - setobj2t(L, cast(TValue *, oldval), val); -- invalidateTMcache(hvalue(t)); -- luaC_barrierback(L, hvalue(t), val); -+ invalidateTMcache(h); -+ luaC_barrierback(L, h, val); - return; - } - /* else will try the metamethod */ -]] -} - - -Bug{ -what = [[label between local definitions can mix-up their initializations]], -report = [[Karel Tuma, 2016/03/01]], -since = [[5.2]], -fix = nil, -example = [[ -do - local k = 0 - local x - ::foo:: - local y -- should be reset to nil after goto, but it is not - assert(not y) - y = true - k = k + 1 - if k < 2 then goto foo end -end -]], -patch = [[ ---- lparser.c 2015/11/02 16:09:30 2.149 -+++ lparser.c 2016/03/03 12:03:37 -@@ -1226,7 +1226,7 @@ - checkrepeated(fs, ll, label); /* check for repeated labels */ - checknext(ls, TK_DBCOLON); /* skip double colon */ - /* create new entry for this label */ -- l = newlabelentry(ls, ll, label, line, fs->pc); -+ l = newlabelentry(ls, ll, label, line, luaK_getlabel(fs)); - skipnoopstat(ls); /* skip other no-op statements */ - if (block_follow(ls, 0)) { /* label is last no-op statement in the block? */ - /* assume that locals are already out of scope */ -]] -} - - -Bug{ -what = [['gmatch' iterator fails when called from a coroutine different -from the one that created it]], -report = [[Nagaev Boris, 2016/03/18]], -since = [[5.3.2]], -fix = nil, -example = [[ -local f = string.gmatch("1 2 3 4 5", "%d+") -print(f()) --> 1 -co = coroutine.wrap(f) -print(co()) --> ??? (should be 2) -]], -patch = [[ ---- lstrlib.c 2015/11/25 16:28:17 1.239 -+++ lstrlib.c 2016/04/11 15:29:41 -@@ -688,6 +688,7 @@ - static int gmatch_aux (lua_State *L) { - GMatchState *gm = (GMatchState *)lua_touserdata(L, lua_upvalueindex(3)); - const char *src; -+ gm->ms.L = L; - for (src = gm->src; src <= gm->ms.src_end; src++) { - const char *e; - reprepstate(&gm->ms); -]] -} - - ------------------------------------------------------------------ --- Lua 5.3.3 - - -Bug{ -what = [[expression list with four or more expressions in -a 'for' loop can crash the interpreter]], -report = [[Marco Schöpl, 2016/06/17]], -since = [[5.2]], -fix = nil, -example = [[ --- the next loop will probably crash the interpreter -repeat until load "for _ in _,_,_,_ do local function _() end" -]], -patch = [[ ---- lparser.c 2016/05/13 19:10:16 2.153 -+++ lparser.c 2016/06/17 19:52:48 -@@ -323,6 +323,8 @@ - luaK_nil(fs, reg, extra); - } - } -+ if (nexps > nvars) -+ ls->fs->freereg -= nexps - nvars; /* remove extra values */ - } - - -@@ -1160,11 +1162,8 @@ - int nexps; - checknext(ls, '='); - nexps = explist(ls, &e); -- if (nexps != nvars) { -+ if (nexps != nvars) - adjust_assign(ls, nvars, nexps, &e); -- if (nexps > nvars) -- ls->fs->freereg -= nexps - nvars; /* remove extra values */ -- } - else { - luaK_setoneret(ls->fs, &e); /* close last expression */ - luaK_storevar(ls->fs, &lh->v, &e); -]] -} - - -Bug{ -what = [[Checking a format for 'os.date' may read pass the format string]], -report = [[Nagaev Boris, 2016/07/10]], -since = [[5.3.3]], -fix = nil, -example = [[ -This bug does not seem to happen with regular compilers. -It needs an "interceptor" 'memcmp' function that continues -reading memory after a difference is found.]], -patch = [[ -2c2 -< ** $Id: bugs,v 1.160 2018/05/24 20:25:14 roberto Exp roberto $ ---- -> ** $Id: bugs,v 1.160 2018/05/24 20:25:14 roberto Exp roberto $ -263c263,264 -< for (option = LUA_STRFTIMEOPTIONS; *option != '\0'; option += oplen) { ---- -> int convlen = (int)strlen(conv); -> for (option = LUA_STRFTIMEOPTIONS; *option != '\0' && oplen <= convlen; option += oplen) { -]] -} - - -Bug{ -what = [[Lua can generate wrong code in functions with too many constants]], -report = [[Marco Schöpl, 2016/07/17]], -since = [[5.3.3]], -fix = nil, -example = [[See http://lua-users.org/lists/lua-l/2016-07/msg00303.html]], -patch = [[ ---- lcode.c 2016/06/20 19:12:46 2.110 -+++ lcode.c 2016/07/18 15:43:41 -@@ -1018,8 +1018,8 @@ - */ - static void codebinexpval (FuncState *fs, OpCode op, - expdesc *e1, expdesc *e2, int line) { -- int rk1 = luaK_exp2RK(fs, e1); /* both operands are "RK" */ -- int rk2 = luaK_exp2RK(fs, e2); -+ int rk2 = luaK_exp2RK(fs, e2); /* both operands are "RK" */ -+ int rk1 = luaK_exp2RK(fs, e1); - freeexps(fs, e1, e2); - e1->u.info = luaK_codeABC(fs, op, 0, rk1, rk2); /* generate opcode */ - e1->k = VRELOCABLE; /* all those operations are relocatable */ -]] -} - - -Bug{ -what = [[When a coroutine tries to resume a non-suspended coroutine, -it can do some mess (and break C assertions) before detecting the error]], -report = [[Marco Schöpl, 2016/07/20]], -since = [[ ]], -fix = nil, -example = [[ --- with C assertions on -A = coroutine.running() -B = coroutine.create(function() coroutine.resume(A) end) -coroutine.resume(B) - --- or -A = coroutine.wrap(function() pcall(A, _) end) -A() -]], -patch = [[ -]] -} - - ------------------------------------------------------------------ --- Lua 5.3.4 - - -Bug{ -what = [[Wrong code for a goto followed by a label inside an 'if']], -report = [[云风, 2017/04/13]], -since = [[5.2]], -fix = nil, -example = [[ --- should print 32323232..., but prints only '3' -if true then - goto LBL - ::loop:: - print(2) - ::LBL:: - print(3) - goto loop -end -]], -patch = [[ ---- lparser.c 2017/04/19 17:20:42 2.155.1.1 -+++ lparser.c 2017/04/29 18:11:40 2.155.1.2 -@@ -1392,7 +1392,7 @@ - luaK_goiffalse(ls->fs, &v); /* will jump to label if condition is true */ - enterblock(fs, &bl, 0); /* must enter block before 'goto' */ - gotostat(ls, v.t); /* handle goto/break */ -- skipnoopstat(ls); /* skip other no-op statements */ -+ while (testnext(ls, ';')) {} /* skip semicolons */ - if (block_follow(ls, 0)) { /* 'goto' is the entire block? */ - leaveblock(fs); - return; /* and that is it */ -]] -} - - -Bug{ -what = [[Lua crashes when building sequences with more than 2^30 elements.]], -report = [[Viacheslav Usov, 2017/05/11]], -since = [[ ]], -fix = nil, -example = [[ --- crashes if machine has enough memory -local t = {} -for i = 1, 0x7fffffff do - t[i] = i -end -]], -patch = [[ ---- ltable.c 2017/04/19 17:20:42 2.118.1.1 -+++ ltable.c 2018/05/24 18:34:38 -@@ -223,7 +223,9 @@ - unsigned int na = 0; /* number of elements to go to array part */ - unsigned int optimal = 0; /* optimal size for array part */ - /* loop while keys can fill more than half of total size */ -- for (i = 0, twotoi = 1; *pna > twotoi / 2; i++, twotoi *= 2) { -+ for (i = 0, twotoi = 1; -+ twotoi > 0 && *pna > twotoi / 2; -+ i++, twotoi *= 2) { - if (nums[i] > 0) { - a += nums[i]; - if (a > twotoi/2) { /* more than half elements present? */ -]] -} - - -Bug{ -what = [[Table length computation overflows for sequences larger than -2^31 elements.]], -report = [[Viacheslav Usov, 2017/05/12]], -since = [[ ]], -fix = nil, -example = [[ --- on a machine with enough memory -local t = {} -for i = 1, 2147483681 do - t[i] = i -end -print(#t) -]], -patch = [[ ---- ltable.h 2017/04/19 17:20:42 2.23.1.1 -+++ ltable.h 2018/05/24 19:31:50 -@@ -56,3 +56,3 @@ - LUAI_FUNC int luaH_next (lua_State *L, Table *t, StkId key); --LUAI_FUNC int luaH_getn (Table *t); -+LUAI_FUNC lua_Unsigned luaH_getn (Table *t); - ---- ltable.c 2018/05/24 19:22:37 2.118.1.2 -+++ ltable.c 2018/05/24 19:25:05 -@@ -614,4 +614,4 @@ - --static int unbound_search (Table *t, unsigned int j) { -- unsigned int i = j; /* i is zero or a present index */ -+static lua_Unsigned unbound_search (Table *t, lua_Unsigned j) { -+ lua_Unsigned i = j; /* i is zero or a present index */ - j++; -@@ -620,3 +620,3 @@ - i = j; -- if (j > cast(unsigned int, MAX_INT)/2) { /* overflow? */ -+ if (j > l_castS2U(LUA_MAXINTEGER) / 2) { /* overflow? */ - /* table was built with bad purposes: resort to linear search */ -@@ -630,3 +630,3 @@ - while (j - i > 1) { -- unsigned int m = (i+j)/2; -+ lua_Unsigned m = (i+j)/2; - if (ttisnil(luaH_getint(t, m))) j = m; -@@ -642,3 +642,3 @@ - */ --int luaH_getn (Table *t) { -+lua_Unsigned luaH_getn (Table *t) { - unsigned int j = t->sizearray; -]] -} - - -Bug{ -what = [[Lua does not check GC when creating error messages]], -report = [[Viacheslav Usov, 2017/07/06]], -since = [[5.3.2]], -fix = nil, -example = [[ -function test() - bob.joe.larry = 23 -end - --- memory will grow steadly -for i = 1, math.huge do - pcall(test) - if i % 100000 == 0 then - io.write(collectgarbage'count'*1024, "\n") - end -end -]], -patch = [[ ---- ldebug.c 2017/04/19 17:20:42 2.121.1.1 -+++ ldebug.c 2017/07/10 17:08:39 -@@ -653,6 +653,7 @@ - CallInfo *ci = L->ci; - const char *msg; - va_list argp; -+ luaC_checkGC(L); /* error message uses memory */ - va_start(argp, fmt); - msg = luaO_pushvfstring(L, fmt, argp); /* format message */ - va_end(argp); -]] -} - - -Bug{ -what = [[dead keys with nil values can stay in weak tables]], -report = [[云风 Cloud Wu, 2017/08/15]], -since = [[5.2]], -fix = nil, -example = [[ --- The following chunk, under a memory checker like valgrind, --- produces a memory access violation. - -local a = setmetatable({}, {__mode = 'kv'}) - -a['ABCDEFGHIJKLMNOPQRSTUVWXYZ' .. 'abcdefghijklmnopqrstuvwxyz'] = {} -a[next(a)] = nil -collectgarbage() -print(a['BCDEFGHIJKLMNOPQRSTUVWXYZ' .. 'abcdefghijklmnopqrstuvwxyz']) -]], -patch = [[ ---- lgc.c 2016/12/22 13:08:50 2.215 -+++ lgc.c 2017/08/31 16:08:23 -@@ -643,8 +643,9 @@ - for (n = gnode(h, 0); n < limit; n++) { - if (!ttisnil(gval(n)) && (iscleared(g, gkey(n)))) { - setnilvalue(gval(n)); /* remove value ... */ -- removeentry(n); /* and remove entry from table */ - } -+ if (ttisnil(gval(n))) /* is entry empty? */ -+ removeentry(n); /* remove entry from table */ - } - } - } -]] -} - - -Bug{ -what = [['lua_pushcclosure' should not call the garbage collector when -'n' is zero.]], -report = [[Andrew Gierth, 2017/12/05]], -since = [[5.3.3]], -fix = nil, -example = [[ ]], -patch = [[ ---- lapi.c 2017/04/19 17:13:00 2.259.1.1 -+++ lapi.c 2017/12/06 18:14:45 -@@ -533,6 +533,7 @@ - lua_lock(L); - if (n == 0) { - setfvalue(L->top, fn); -+ api_incr_top(L); - } - else { - CClosure *cl; -@@ -546,9 +547,9 @@ - /* does not need barrier because closure is white */ - } - setclCvalue(L, L->top, cl); -+ api_incr_top(L); -+ luaC_checkGC(L); - } -- api_incr_top(L); -- luaC_checkGC(L); - lua_unlock(L); - } -]] -} - - -Bug{ -what = [[memory-allocation error when resizing a table can leave it -in an inconsistent state.]], -report = [[Roberto, 2017/12/08]], -since = [[5.0]], -fix = nil, -example = [[ -local a = {x = 1, y = 1, z = 1} -a[1] = 10 -- goes to the hash part (which has 4 slots) -print(a[1]) --> 10 - --- assume that the 2nd memory allocation from now fails -pcall(rawset, a, 2, 20) -- forces a rehash - --- a[1] now exists both in the array part (because the array part --- grew) and in the hash part (because the allocation of the hash --- part failed, keeping it as it was). --- This makes the following traversal goes forever... -for k,v in pairs(a) do print(k,v) end -]], -patch = [[ ---- ltable.c 2018/05/24 19:39:05 2.118.1.3 -+++ ltable.c 2018/06/04 16:00:25 -@@ -332,17 +332,34 @@ - } - - -+typedef struct { -+ Table *t; -+ unsigned int nhsize; -+} AuxsetnodeT; -+ -+ -+static void auxsetnode (lua_State *L, void *ud) { -+ AuxsetnodeT *asn = cast(AuxsetnodeT *, ud); -+ setnodevector(L, asn->t, asn->nhsize); -+} -+ -+ - void luaH_resize (lua_State *L, Table *t, unsigned int nasize, - unsigned int nhsize) { - unsigned int i; - int j; -+ AuxsetnodeT asn; - unsigned int oldasize = t->sizearray; - int oldhsize = allocsizenode(t); - Node *nold = t->node; /* save old hash ... */ - if (nasize > oldasize) /* array part must grow? */ - setarrayvector(L, t, nasize); - /* create new hash part with appropriate size */ -- setnodevector(L, t, nhsize); -+ asn.t = t; asn.nhsize = nhsize; -+ if (luaD_rawrunprotected(L, auxsetnode, &asn) != LUA_OK) { /* mem. error? */ -+ setarrayvector(L, t, oldasize); /* array back to its original size */ -+ luaD_throw(L, LUA_ERRMEM); /* rethrow memory error */ -+ } - if (nasize < oldasize) { /* array part must shrink? */ - t->sizearray = nasize; - /* re-insert elements from vanishing slice */ -]] -} - - - ---[=[ -Bug{ -what = [[Long brackets with a huge number of '=' overflow some -internal buffer arithmetic]], -report = [[Marco, 2018/12/12]], -since = [[5.1]], -fix = nil, -example = [[ -local eqs = string.rep("=", 0x3ffffffe) -local code = "return [" .. eqs .. "[a]" .. eqs .. "]" -print(#assert(load(code))()) -]], -patch = [[ -]] -} -]=] - - - - ---[=[ -Bug{ -what = [[ ]], -report = [[ ]], -since = [[ ]], -fix = nil, -example = [[ ]], -patch = [[ -]] -} -]=] - - From 1499680f9e3d694462ac645ed8162f310509c64c Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 7 Nov 2019 10:57:57 -0300 Subject: [PATCH 0518/1145] Comments in 'lopcodes.h' Both 'R' and 'K' are arrays, so the comments should use square brackets to index them. --- lopcodes.h | 184 ++++++++++++++++++++++++++--------------------------- 1 file changed, 92 insertions(+), 92 deletions(-) diff --git a/lopcodes.h b/lopcodes.h index 443a71e97c..dbef6a656b 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -183,9 +183,9 @@ enum OpMode {iABC, iABx, iAsBx, iAx, isJ}; /* basic instruction formats */ /* -** R(x) - register -** K(x) - constant (in constant table) -** RK(x) == if k(i) then K(x) else R(x) +** R[x] - register +** K[x] - constant (in constant table) +** RK(x) == if k(i) then K[x] else R[x] */ @@ -197,109 +197,109 @@ typedef enum { /*---------------------------------------------------------------------- name args description ------------------------------------------------------------------------*/ -OP_MOVE,/* A B R(A) := R(B) */ -OP_LOADI,/* A sBx R(A) := sBx */ -OP_LOADF,/* A sBx R(A) := (lua_Number)sBx */ -OP_LOADK,/* A Bx R(A) := K(Bx) */ -OP_LOADKX,/* A R(A) := K(extra arg) */ -OP_LOADBOOL,/* A B C R(A) := (Bool)B; if (C) pc++ */ -OP_LOADNIL,/* A B R(A), R(A+1), ..., R(A+B) := nil */ -OP_GETUPVAL,/* A B R(A) := UpValue[B] */ -OP_SETUPVAL,/* A B UpValue[B] := R(A) */ - -OP_GETTABUP,/* A B C R(A) := UpValue[B][K(C):string] */ -OP_GETTABLE,/* A B C R(A) := R(B)[R(C)] */ -OP_GETI,/* A B C R(A) := R(B)[C] */ -OP_GETFIELD,/* A B C R(A) := R(B)[K(C):string] */ - -OP_SETTABUP,/* A B C UpValue[A][K(B):string] := RK(C) */ -OP_SETTABLE,/* A B C R(A)[R(B)] := RK(C) */ -OP_SETI,/* A B C R(A)[B] := RK(C) */ -OP_SETFIELD,/* A B C R(A)[K(B):string] := RK(C) */ - -OP_NEWTABLE,/* A B C R(A) := {} */ - -OP_SELF,/* A B C R(A+1) := R(B); R(A) := R(B)[RK(C):string] */ - -OP_ADDI,/* A B sC R(A) := R(B) + sC */ - -OP_ADDK,/* A B C R(A) := R(B) + K(C) */ -OP_SUBK,/* A B C R(A) := R(B) - K(C) */ -OP_MULK,/* A B C R(A) := R(B) * K(C) */ -OP_MODK,/* A B C R(A) := R(B) % K(C) */ -OP_POWK,/* A B C R(A) := R(B) ^ K(C) */ -OP_DIVK,/* A B C R(A) := R(B) / K(C) */ -OP_IDIVK,/* A B C R(A) := R(B) // K(C) */ - -OP_BANDK,/* A B C R(A) := R(B) & K(C):integer */ -OP_BORK,/* A B C R(A) := R(B) | K(C):integer */ -OP_BXORK,/* A B C R(A) := R(B) ~ K(C):integer */ - -OP_SHRI,/* A B sC R(A) := R(B) >> sC */ -OP_SHLI,/* A B sC R(A) := sC << R(B) */ - -OP_ADD,/* A B C R(A) := R(B) + R(C) */ -OP_SUB,/* A B C R(A) := R(B) - R(C) */ -OP_MUL,/* A B C R(A) := R(B) * R(C) */ -OP_MOD,/* A B C R(A) := R(B) % R(C) */ -OP_POW,/* A B C R(A) := R(B) ^ R(C) */ -OP_DIV,/* A B C R(A) := R(B) / R(C) */ -OP_IDIV,/* A B C R(A) := R(B) // R(C) */ - -OP_BAND,/* A B C R(A) := R(B) & R(C) */ -OP_BOR,/* A B C R(A) := R(B) | R(C) */ -OP_BXOR,/* A B C R(A) := R(B) ~ R(C) */ -OP_SHL,/* A B C R(A) := R(B) << R(C) */ -OP_SHR,/* A B C R(A) := R(B) >> R(C) */ - -OP_MMBIN,/* A B C call C metamethod over R(A) and R(B) */ -OP_MMBINI,/* A sB C call C metamethod over R(A) and sB */ -OP_MMBINK,/* A B C call C metamethod over R(A) and K(B) */ - -OP_UNM,/* A B R(A) := -R(B) */ -OP_BNOT,/* A B R(A) := ~R(B) */ -OP_NOT,/* A B R(A) := not R(B) */ -OP_LEN,/* A B R(A) := length of R(B) */ - -OP_CONCAT,/* A B R(A) := R(A).. ... ..R(A + B - 1) */ - -OP_CLOSE,/* A close all upvalues >= R(A) */ +OP_MOVE,/* A B R[A] := R[B] */ +OP_LOADI,/* A sBx R[A] := sBx */ +OP_LOADF,/* A sBx R[A] := (lua_Number)sBx */ +OP_LOADK,/* A Bx R[A] := K[Bx] */ +OP_LOADKX,/* A R[A] := K[extra arg] */ +OP_LOADBOOL,/* A B C R[A] := (Bool)B; if (C) pc++ */ +OP_LOADNIL,/* A B R[A], R[A+1], ..., R[A+B] := nil */ +OP_GETUPVAL,/* A B R[A] := UpValue[B] */ +OP_SETUPVAL,/* A B UpValue[B] := R[A] */ + +OP_GETTABUP,/* A B C R[A] := UpValue[B][K[C]:string] */ +OP_GETTABLE,/* A B C R[A] := R[B][R[C]] */ +OP_GETI,/* A B C R[A] := R[B][C] */ +OP_GETFIELD,/* A B C R[A] := R[B][K[C]:string] */ + +OP_SETTABUP,/* A B C UpValue[A][K[B]:string] := RK(C) */ +OP_SETTABLE,/* A B C R[A][R[B]] := RK(C) */ +OP_SETI,/* A B C R[A][B] := RK(C) */ +OP_SETFIELD,/* A B C R[A][K[B]:string] := RK(C) */ + +OP_NEWTABLE,/* A B C R[A] := {} */ + +OP_SELF,/* A B C R[A+1] := R[B]; R[A] := R[B][RK(C):string] */ + +OP_ADDI,/* A B sC R[A] := R[B] + sC */ + +OP_ADDK,/* A B C R[A] := R[B] + K[C] */ +OP_SUBK,/* A B C R[A] := R[B] - K[C] */ +OP_MULK,/* A B C R[A] := R[B] * K[C] */ +OP_MODK,/* A B C R[A] := R[B] % K[C] */ +OP_POWK,/* A B C R[A] := R[B] ^ K[C] */ +OP_DIVK,/* A B C R[A] := R[B] / K[C] */ +OP_IDIVK,/* A B C R[A] := R[B] // K[C] */ + +OP_BANDK,/* A B C R[A] := R[B] & K[C]:integer */ +OP_BORK,/* A B C R[A] := R[B] | K[C]:integer */ +OP_BXORK,/* A B C R[A] := R[B] ~ K[C]:integer */ + +OP_SHRI,/* A B sC R[A] := R[B] >> sC */ +OP_SHLI,/* A B sC R[A] := sC << R[B] */ + +OP_ADD,/* A B C R[A] := R[B] + R[C] */ +OP_SUB,/* A B C R[A] := R[B] - R[C] */ +OP_MUL,/* A B C R[A] := R[B] * R[C] */ +OP_MOD,/* A B C R[A] := R[B] % R[C] */ +OP_POW,/* A B C R[A] := R[B] ^ R[C] */ +OP_DIV,/* A B C R[A] := R[B] / R[C] */ +OP_IDIV,/* A B C R[A] := R[B] // R[C] */ + +OP_BAND,/* A B C R[A] := R[B] & R[C] */ +OP_BOR,/* A B C R[A] := R[B] | R[C] */ +OP_BXOR,/* A B C R[A] := R[B] ~ R[C] */ +OP_SHL,/* A B C R[A] := R[B] << R[C] */ +OP_SHR,/* A B C R[A] := R[B] >> R[C] */ + +OP_MMBIN,/* A B C call C metamethod over R[A] and R[B] */ +OP_MMBINI,/* A sB C call C metamethod over R[A] and sB */ +OP_MMBINK,/* A B C call C metamethod over R[A] and K[B] */ + +OP_UNM,/* A B R[A] := -R[B] */ +OP_BNOT,/* A B R[A] := ~R[B] */ +OP_NOT,/* A B R[A] := not R[B] */ +OP_LEN,/* A B R[A] := length of R[B] */ + +OP_CONCAT,/* A B R[A] := R[A].. ... ..R[A + B - 1] */ + +OP_CLOSE,/* A close all upvalues >= R[A] */ OP_TBC,/* A mark variable A "to be closed" */ -OP_JMP,/* k sJ pc += sJ (k is used in code generation) */ -OP_EQ,/* A B if ((R(A) == R(B)) ~= k) then pc++ */ -OP_LT,/* A B if ((R(A) < R(B)) ~= k) then pc++ */ -OP_LE,/* A B if ((R(A) <= R(B)) ~= k) then pc++ */ +OP_JMP,/* sJ pc += sJ */ +OP_EQ,/* A B if ((R[A] == R[B]) ~= k) then pc++ */ +OP_LT,/* A B if ((R[A] < R[B]) ~= k) then pc++ */ +OP_LE,/* A B if ((R[A] <= R[B]) ~= k) then pc++ */ -OP_EQK,/* A B if ((R(A) == K(B)) ~= k) then pc++ */ -OP_EQI,/* A sB if ((R(A) == sB) ~= k) then pc++ */ -OP_LTI,/* A sB if ((R(A) < sB) ~= k) then pc++ */ -OP_LEI,/* A sB if ((R(A) <= sB) ~= k) then pc++ */ -OP_GTI,/* A sB if ((R(A) > sB) ~= k) then pc++ */ -OP_GEI,/* A sB if ((R(A) >= sB) ~= k) then pc++ */ +OP_EQK,/* A B if ((R[A] == K[B]) ~= k) then pc++ */ +OP_EQI,/* A sB if ((R[A] == sB) ~= k) then pc++ */ +OP_LTI,/* A sB if ((R[A] < sB) ~= k) then pc++ */ +OP_LEI,/* A sB if ((R[A] <= sB) ~= k) then pc++ */ +OP_GTI,/* A sB if ((R[A] > sB) ~= k) then pc++ */ +OP_GEI,/* A sB if ((R[A] >= sB) ~= k) then pc++ */ -OP_TEST,/* A if (not R(A) == k) then pc++ */ -OP_TESTSET,/* A B if (not R(B) == k) then pc++ else R(A) := R(B) */ +OP_TEST,/* A if (not R[A] == k) then pc++ */ +OP_TESTSET,/* A B if (not R[B] == k) then pc++ else R[A] := R[B] */ -OP_CALL,/* A B C R(A), ... ,R(A+C-2) := R(A)(R(A+1), ... ,R(A+B-1)) */ -OP_TAILCALL,/* A B C return R(A)(R(A+1), ... ,R(A+B-1)) */ +OP_CALL,/* A B C R[A], ... ,R[A+C-2] := R[A](R[A+1], ... ,R[A+B-1]) */ +OP_TAILCALL,/* A B C return R[A](R[A+1], ... ,R[A+B-1]) */ -OP_RETURN,/* A B C return R(A), ... ,R(A+B-2) (see note) */ +OP_RETURN,/* A B C return R[A], ... ,R[A+B-2] (see note) */ OP_RETURN0,/* return */ -OP_RETURN1,/* A return R(A) */ +OP_RETURN1,/* A return R[A] */ OP_FORLOOP,/* A Bx update counters; if loop continues then pc-=Bx; */ OP_FORPREP,/* A Bx ; if not to run then pc+=Bx+1; */ -OP_TFORPREP,/* A Bx create upvalue for R(A + 3); pc+=Bx */ -OP_TFORCALL,/* A C R(A+4), ... ,R(A+3+C) := R(A)(R(A+1), R(A+2)); */ -OP_TFORLOOP,/* A Bx if R(A+2) ~= nil then { R(A)=R(A+2); pc -= Bx } */ +OP_TFORPREP,/* A Bx create upvalue for R[A + 3]; pc+=Bx */ +OP_TFORCALL,/* A C R[A+4], ... ,R[A+3+C] := R[A](R[A+1], R[A+2]); */ +OP_TFORLOOP,/* A Bx if R[A+2] ~= nil then { R[A]=R[A+2]; pc -= Bx } */ -OP_SETLIST,/* A B C R(A)[(C-1)*FPF+i] := R(A+i), 1 <= i <= B */ +OP_SETLIST,/* A B C R[A][(C-1)*FPF+i] := R[A+i], 1 <= i <= B */ -OP_CLOSURE,/* A Bx R(A) := closure(KPROTO[Bx]) */ +OP_CLOSURE,/* A Bx R[A] := closure(KPROTO[Bx]) */ -OP_VARARG,/* A C R(A), R(A+1), ..., R(A+C-2) = vararg */ +OP_VARARG,/* A C R[A], R[A+1], ..., R[A+C-2] = vararg */ OP_VARARGPREP,/*A (adjust vararg parameters) */ From 679dc72c08a7c563a0c3f463332d6f22d573a106 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 8 Nov 2019 15:45:55 -0300 Subject: [PATCH 0519/1145] Using 'metavalues' for "metamethods" that are not methods Several "metamethods" are not required to be methods (functions), so it seems clearer not to call them metamethods. The manual now uses the word 'metavalue' for those values. --- manual/manual.of | 47 ++++++++++++++++++++++++++--------------------- 1 file changed, 26 insertions(+), 21 deletions(-) diff --git a/manual/manual.of b/manual/manual.of index 00ab4cd519..7b5b9385ec 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -306,11 +306,14 @@ Lua calls this function to perform the addition. The key for each event in a metatable is a string with the event name prefixed by two underscores; -the corresponding values are called @def{metamethods}. +the corresponding value is called a @def{metavalue}. +For most events, the metavalue must be a function, +which is then called a @def{metamethod}. In the previous example, the key is the string @St{__add} and the metamethod is the function that performs the addition. Unless stated otherwise, -metamethods should be function values. +a metamethod may in fact be any @x{callable value}, +which is either a function or a value with a @id{__call} metamethod. You can query the metatable of any value using the @Lid{getmetatable} function. @@ -468,19 +471,19 @@ Behavior similar to the less than operation. The indexing access operation @T{table[key]}. This event happens when @id{table} is not a table or when @id{key} is not present in @id{table}. -The metamethod is looked up in the metatable of @id{table}. +The metavalue is looked up in the metatable of @id{table}. -Despite the name, -the metamethod for this event can be either a function or a table. +The metavalue for this event can be either a function, a table, +or any value with an @id{__index} metavalue. If it is a function, it is called with @id{table} and @id{key} as arguments, and the result of the call (adjusted to one value) is the result of the operation. -If it is a table, -the final result is the result of indexing this table with @id{key}. +Otherwise, +the final result is the result of indexing this metavalue with @id{key}. This indexing is regular, not raw, -and therefore can trigger another metamethod. +and therefore can trigger another @id{__index} metavalue. } @item{@idx{__newindex}| @@ -488,18 +491,20 @@ The indexing assignment @T{table[key] = value}. Like the index event, this event happens when @id{table} is not a table or when @id{key} is not present in @id{table}. -The metamethod is looked up in @id{table}. +The metavalue is looked up in the metatable of @id{table}. Like with indexing, -the metamethod for this event can be either a function or a table. +the metavalue for this event can be either a function, a table, +or any value with an @id{__newindex} metavalue. If it is a function, it is called with @id{table}, @id{key}, and @id{value} as arguments. -If it is a table, -Lua does an indexing assignment to this table with the same key and value. +Otherwise, +Lua repeats the indexing assignment over this metavalue +with the same key and value. This assignment is regular, not raw, -and therefore can trigger another metamethod. +and therefore can trigger another @id{__newindex} metavalue. -Whenever there is a @idx{__newindex} metamethod, +Whenever a @idx{__newindex} metavalue is invoked, Lua does not perform the primitive assignment. If needed, the metamethod itself can call @Lid{rawset} @@ -760,7 +765,7 @@ In any case, if either the key or the value is collected, the whole pair is removed from the table. The weakness of a table is controlled by the @idx{__mode} field of its metatable. -This field, if present, must be one of the following strings: +This metavalue, if present, must be one of the following strings: @St{k}, for a table with weak keys; @St{v}, for a table with weak values; or @St{kv}, for a table with both weak keys and values. @@ -3836,7 +3841,7 @@ Similar to @Lid{lua_gettable}, but does a raw access Pushes onto the stack the value @T{t[n]}, where @id{t} is the table at the given index. The access is raw, -that is, it does not invoke the @idx{__index} metamethod. +that is, it does not use the @idx{__index} metavalue. Returns the type of the pushed value. @@ -3849,7 +3854,7 @@ Pushes onto the stack the value @T{t[k]}, where @id{t} is the table at the given index and @id{k} is the pointer @id{p} represented as a light userdata. The access is raw; -that is, it does not invoke the @idx{__index} metamethod. +that is, it does not use the @idx{__index} metavalue. Returns the type of the pushed value. @@ -3885,7 +3890,7 @@ and @id{v} is the value on the top of the stack. This function pops the value from the stack. The assignment is raw, -that is, it does not invoke the @idx{__newindex} metamethod. +that is, it does not use the @idx{__newindex} metavalue. } @@ -3899,7 +3904,7 @@ and @id{v} is the value on the top of the stack. This function pops the value from the stack. The assignment is raw, -that is, it does not invoke @idx{__newindex} metamethod. +that is, it does not use the @idx{__newindex} metavalue. } @@ -6275,7 +6280,7 @@ Returns a boolean. @LibEntry{rawget (table, index)| Gets the real value of @T{table[index]}, -without invoking the @idx{__index} metamethod. +without using the @idx{__index} metavalue. @id{table} must be a table; @id{index} may be any value. @@ -6291,7 +6296,7 @@ Returns an integer. @LibEntry{rawset (table, index, value)| Sets the real value of @T{table[index]} to @id{value}, -without invoking the @idx{__newindex} metamethod. +without using the @idx{__newindex} metavalue. @id{table} must be a table, @id{index} any value different from @nil and @x{NaN}, and @id{value} any Lua value. From 5f83fb658206d195e54d3574b989ce5285a5b18f Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 18 Nov 2019 14:54:06 -0300 Subject: [PATCH 0520/1145] Details --- lfunc.c | 14 +++++++------- lfunc.h | 4 ++-- lopcodes.h | 4 ++-- lparser.h | 2 +- lvm.c | 2 +- manual/manual.of | 5 +++-- 6 files changed, 16 insertions(+), 15 deletions(-) diff --git a/lfunc.c b/lfunc.c index 1e61f03f58..0ef7328435 100644 --- a/lfunc.c +++ b/lfunc.c @@ -24,20 +24,20 @@ -CClosure *luaF_newCclosure (lua_State *L, int n) { - GCObject *o = luaC_newobj(L, LUA_TCCL, sizeCclosure(n)); +CClosure *luaF_newCclosure (lua_State *L, int nupvals) { + GCObject *o = luaC_newobj(L, LUA_TCCL, sizeCclosure(nupvals)); CClosure *c = gco2ccl(o); - c->nupvalues = cast_byte(n); + c->nupvalues = cast_byte(nupvals); return c; } -LClosure *luaF_newLclosure (lua_State *L, int n) { - GCObject *o = luaC_newobj(L, LUA_TLCL, sizeLclosure(n)); +LClosure *luaF_newLclosure (lua_State *L, int nupvals) { + GCObject *o = luaC_newobj(L, LUA_TLCL, sizeLclosure(nupvals)); LClosure *c = gco2lcl(o); c->p = NULL; - c->nupvalues = cast_byte(n); - while (n--) c->upvals[n] = NULL; + c->nupvalues = cast_byte(nupvals); + while (nupvals--) c->upvals[nupvals] = NULL; return c; } diff --git a/lfunc.h b/lfunc.h index 0ed79c48ab..8d6f965cfc 100644 --- a/lfunc.h +++ b/lfunc.h @@ -54,8 +54,8 @@ LUAI_FUNC Proto *luaF_newproto (lua_State *L); -LUAI_FUNC CClosure *luaF_newCclosure (lua_State *L, int nelems); -LUAI_FUNC LClosure *luaF_newLclosure (lua_State *L, int nelems); +LUAI_FUNC CClosure *luaF_newCclosure (lua_State *L, int nupvals); +LUAI_FUNC LClosure *luaF_newLclosure (lua_State *L, int nupvals); LUAI_FUNC void luaF_initupvals (lua_State *L, LClosure *cl); LUAI_FUNC UpVal *luaF_findupval (lua_State *L, StkId level); LUAI_FUNC void luaF_newtbcupval (lua_State *L, StkId level); diff --git a/lopcodes.h b/lopcodes.h index dbef6a656b..382dec05fc 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -19,7 +19,7 @@ 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 iABC C(8) | B(8) |k| A(8) | Op(7) | iABx Bx(17) | A(8) | Op(7) | -iAsB sBx (signed)(17) | A(8) | Op(7) | +iAsBx sBx (signed)(17) | A(8) | Op(7) | iAx Ax(25) | Op(7) | isJ sJ(25) | Op(7) | @@ -133,7 +133,7 @@ enum OpMode {iABC, iABx, iAsBx, iAx, isJ}; /* basic instruction formats */ #define GETARG_sC(i) sC2int(GETARG_C(i)) #define SETARG_C(i,v) setarg(i, v, POS_C, SIZE_C) -#define TESTARG_k(i) (cast_int(((i) & (1u << POS_k)))) +#define TESTARG_k(i) check_exp(checkopm(i, iABC), (cast_int(((i) & (1u << POS_k))))) #define GETARG_k(i) check_exp(checkopm(i, iABC), getarg(i, POS_k, 1)) #define SETARG_k(i,v) setarg(i, v, POS_k, 1) diff --git a/lparser.h b/lparser.h index f528f0130c..f544492e81 100644 --- a/lparser.h +++ b/lparser.h @@ -35,7 +35,7 @@ typedef enum { (string is fixed by the lexer) */ VNONRELOC, /* expression has its value in a fixed register; info = result register */ - VLOCAL, /* local variable; var.ridx = local register; + VLOCAL, /* local variable; var.sidx = stack index (local register); var.vidx = relative index in 'actvar.arr' */ VUPVAL, /* upvalue variable; info = index of upvalue in 'upvalues' */ VCONST, /* compile-time constant; info = absolute index in 'actvar.arr' */ diff --git a/lvm.c b/lvm.c index 2c96c58d15..93cb8bc881 100644 --- a/lvm.c +++ b/lvm.c @@ -1082,7 +1082,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { lua_assert(base == ci->func + 1); lua_assert(base <= L->top && L->top < L->stack + L->stacksize); /* invalidate top for instructions not expecting it */ - lua_assert(isIT(i) || (L->top = base)); + lua_assert(isIT(i) || (cast_void(L->top = base), 1)); vmdispatch (GET_OPCODE(i)) { vmcase(OP_MOVE) { setobjs2s(L, ra, RB(i)); diff --git a/manual/manual.of b/manual/manual.of index 7b5b9385ec..61d4afacc1 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -8237,7 +8237,8 @@ This library is implemented through table @defid{os}. @LibEntry{os.clock ()| Returns an approximation of the amount in seconds of CPU time -used by the program. +used by the program, +as returned by the underlying @ANSI{clock}. } @@ -8336,7 +8337,7 @@ closes the Lua state before exiting. @LibEntry{os.getenv (varname)| -Returns the value of the process environment variable @id{varname}, +Returns the value of the process environment variable @id{varname} or @fail if the variable is not defined. } From 6f1c033d72af8fe65bb67e17a242314b6aeb182f Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 22 Nov 2019 11:07:47 -0300 Subject: [PATCH 0521/1145] More generic pattern when testing 'string.format' The result of 'string.format("%a", 0.0)' can have multiple zeros after the dot. --- testes/strings.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/testes/strings.lua b/testes/strings.lua index 97875ec0da..f2f61413a2 100644 --- a/testes/strings.lua +++ b/testes/strings.lua @@ -309,8 +309,8 @@ do print("testing 'format %a %A'") matchhexa(n) end - assert(string.find(string.format("%A", 0.0), "^0X0%.?0?P%+?0$")) - assert(string.find(string.format("%a", -0.0), "^%-0x0%.?0?p%+?0$")) + assert(string.find(string.format("%A", 0.0), "^0X0%.?0*P%+?0$")) + assert(string.find(string.format("%a", -0.0), "^%-0x0%.?0*p%+?0$")) if not _port then -- test inf, -inf, NaN, and -0.0 assert(string.find(string.format("%a", 1/0), "^inf")) From 508a705c1c08d883473199d6ba019d186c28b9df Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 28 Nov 2019 18:10:26 -0300 Subject: [PATCH 0522/1145] Removed some wrong comments Both 'tonumber' and 'tointeger' cannot change the out parameter when the conversion fails. --- lapi.c | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/lapi.c b/lapi.c index 0ea3dc0f22..ffc4d5e534 100644 --- a/lapi.c +++ b/lapi.c @@ -350,23 +350,21 @@ LUA_API size_t lua_stringtonumber (lua_State *L, const char *s) { LUA_API lua_Number lua_tonumberx (lua_State *L, int idx, int *pisnum) { - lua_Number n; + lua_Number n = 0; const TValue *o = index2value(L, idx); int isnum = tonumber(o, &n); - if (!isnum) - n = 0; /* call to 'tonumber' may change 'n' even if it fails */ - if (pisnum) *pisnum = isnum; + if (pisnum) + *pisnum = isnum; return n; } LUA_API lua_Integer lua_tointegerx (lua_State *L, int idx, int *pisnum) { - lua_Integer res; + lua_Integer res = 0; const TValue *o = index2value(L, idx); int isnum = tointeger(o, &res); - if (!isnum) - res = 0; /* call to 'tointeger' may change 'n' even if it fails */ - if (pisnum) *pisnum = isnum; + if (pisnum) + *pisnum = isnum; return res; } From 81f2401c6dc6afc819787a0b651f9e4be241e942 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 4 Dec 2019 16:51:53 -0300 Subject: [PATCH 0523/1145] Code reorganization for opcodes OP_FORPREP and OP_FORLOOP Parts of the code for opcodes OP_FORPREP and OP_FORLOOP were moved to functions outside the interpreter loop. --- lvm.c | 191 +++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 116 insertions(+), 75 deletions(-) diff --git a/lvm.c b/lvm.c index 93cb8bc881..dc479f0ad9 100644 --- a/lvm.c +++ b/lvm.c @@ -80,6 +80,21 @@ #endif +/* +** Try to convert a value from string to a number value. +** If the value is not a string or is a string not representing +** a valid numeral (or if coercions from strings to numbers +** are disabled via macro 'cvt2num'), do not modify 'result' +** and return 0. +*/ +static int l_strton (const TValue *obj, TValue *result) { + lua_assert(obj != result); + if (!cvt2num(obj)) /* is object not a string? */ + return 0; + else + return (luaO_str2num(svalue(obj), result) == vslen(obj) + 1); +} + /* ** Try to convert a value to a float. The float case is already handled @@ -91,8 +106,7 @@ int luaV_tonumber_ (const TValue *obj, lua_Number *n) { *n = cast_num(ivalue(obj)); return 1; } - else if (cvt2num(obj) && /* string coercible to number? */ - luaO_str2num(svalue(obj), &v) == vslen(obj) + 1) { + else if (l_strton(obj, &v)) { /* string coercible to number? */ *n = nvalue(&v); /* convert result of 'luaO_str2num' to a float */ return 1; } @@ -111,7 +125,7 @@ int luaV_flttointeger (lua_Number n, lua_Integer *p, int mode) { lua_Number f = l_floor(n); if (n != f) { /* not an integral value? */ if (mode == 0) return 0; /* fails if mode demands integral value */ - else if (mode > 1) /* needs ceil? */ + else if (mode == 2) /* needs ceil? */ f += 1; /* convert floor to ceil (remember: n != f) */ } return lua_numbertointeger(f, p); @@ -140,27 +154,27 @@ int luaV_tointegerns (const TValue *obj, lua_Integer *p, int mode) { */ int luaV_tointeger (const TValue *obj, lua_Integer *p, int mode) { TValue v; - if (cvt2num(obj) && luaO_str2num(svalue(obj), &v) == vslen(obj) + 1) - obj = &v; /* change string to its corresponding number */ + if (l_strton(obj, &v)) /* does 'obj' point to a numerical string? */ + obj = &v; /* change it to point to its corresponding number */ return luaV_tointegerns(obj, p, mode); } /* ** Try to convert a 'for' limit to an integer, preserving the semantics -** of the loop. (The following explanation assumes a positive step; -** it is valid for negative steps mutatis mutandis.) -** Return true if the loop must not run. +** of the loop. Return true if the loop must not run; otherwise, '*p' +** gets the integer limit. +** (The following explanation assumes a positive step; it is valid for +** negative steps mutatis mutandis.) ** If the limit is an integer or can be converted to an integer, ** rounding down, that is the limit. ** Otherwise, check whether the limit can be converted to a float. If ** the float is too large, clip it to LUA_MAXINTEGER. If the float ** is too negative, the loop should not run, because any initial -** integer value is greater than such limit; so, it returns true to -** signal that. -** (For this latter case, no integer limit would be correct; even a -** limit of LUA_MININTEGER would run the loop once for an initial -** value equal to LUA_MININTEGER.) +** integer value is greater than such limit; so, the function returns +** true to signal that. (For this latter case, no integer limit would be +** correct; even a limit of LUA_MININTEGER would run the loop once for +** an initial value equal to LUA_MININTEGER.) */ static int forlimit (lua_State *L, lua_Integer init, const TValue *lim, lua_Integer *p, lua_Integer step) { @@ -183,6 +197,91 @@ static int forlimit (lua_State *L, lua_Integer init, const TValue *lim, } +/* +** Prepare a numerical for loop (opcode OP_FORPREP). +** Return true to skip the loop. Otherwise, +** after preparation, stack will be as follows: +** ra : internal index (safe copy of the control variable) +** ra + 1 : loop counter (integer loops) or limit (float loops) +** ra + 2 : step +** ra + 3 : control variable +*/ +static int forprep (lua_State *L, StkId ra) { + TValue *pinit = s2v(ra); + TValue *plimit = s2v(ra + 1); + TValue *pstep = s2v(ra + 2); + if (ttisinteger(pinit) && ttisinteger(pstep)) { /* integer loop? */ + lua_Integer init = ivalue(pinit); + lua_Integer step = ivalue(pstep); + lua_Integer limit; + if (step == 0) + luaG_runerror(L, "'for' step is zero"); + setivalue(s2v(ra + 3), init); /* control variable */ + if (forlimit(L, init, plimit, &limit, step)) + return 1; /* skip the loop */ + else { /* prepare loop counter */ + lua_Unsigned count; + if (step > 0) { /* ascending loop? */ + count = l_castS2U(limit) - l_castS2U(init); + if (step != 1) /* avoid division in the too common case */ + count /= l_castS2U(step); + } + else { /* step < 0; descending loop */ + count = l_castS2U(init) - l_castS2U(limit); + /* 'step+1' avoids negating 'mininteger' */ + count /= l_castS2U(-(step + 1)) + 1u; + } + /* store the counter in place of the limit (which won't be + needed anymore */ + setivalue(plimit, l_castU2S(count)); + } + } + else { /* try making all values floats */ + lua_Number init; lua_Number limit; lua_Number step; + if (unlikely(!tonumber(plimit, &limit))) + luaG_forerror(L, plimit, "limit"); + if (unlikely(!tonumber(pstep, &step))) + luaG_forerror(L, pstep, "step"); + if (unlikely(!tonumber(pinit, &init))) + luaG_forerror(L, pinit, "initial value"); + if (step == 0) + luaG_runerror(L, "'for' step is zero"); + if (luai_numlt(0, step) ? luai_numlt(limit, init) + : luai_numlt(init, limit)) + return 1; /* skip the loop */ + else { + /* make sure internal values are all floats */ + setfltvalue(plimit, limit); + setfltvalue(pstep, step); + setfltvalue(s2v(ra), init); /* internal index */ + setfltvalue(s2v(ra + 3), init); /* control variable */ + } + } + return 0; +} + + +/* +** Execute a step of a float numerical for loop, returning +** true iff the loop must continue. (The integer case is +** written online with opcode OP_FORLOOP, for performance.) +*/ +static int floatforloop (StkId ra) { + lua_Number step = fltvalue(s2v(ra + 2)); + lua_Number limit = fltvalue(s2v(ra + 1)); + lua_Number idx = fltvalue(s2v(ra)); /* internal index */ + idx = luai_numadd(L, idx, step); /* increment index */ + if (luai_numlt(0, step) ? luai_numle(idx, limit) + : luai_numle(limit, idx)) { + chgfltvalue(s2v(ra), idx); /* update internal index */ + setfltvalue(s2v(ra + 3), idx); /* and control variable */ + return 1; /* jump back */ + } + else + return 0; /* finish the loop */ +} + + /* ** Finish the table access 'val = t[key]'. ** if 'slot' is NULL, 't' is not a table; otherwise, 'slot' points to @@ -1631,73 +1730,15 @@ void luaV_execute (lua_State *L, CallInfo *ci) { pc -= GETARG_Bx(i); /* jump back */ } } - else { /* floating loop */ - lua_Number step = fltvalue(s2v(ra + 2)); - lua_Number limit = fltvalue(s2v(ra + 1)); - lua_Number idx = fltvalue(s2v(ra)); - idx = luai_numadd(L, idx, step); /* increment index */ - if (luai_numlt(0, step) ? luai_numle(idx, limit) - : luai_numle(limit, idx)) { - chgfltvalue(s2v(ra), idx); /* update internal index */ - setfltvalue(s2v(ra + 3), idx); /* and control variable */ - pc -= GETARG_Bx(i); /* jump back */ - } - } + else if (floatforloop(ra)) /* float loop */ + pc -= GETARG_Bx(i); /* jump back */ updatetrap(ci); /* allows a signal to break the loop */ vmbreak; } vmcase(OP_FORPREP) { - TValue *pinit = s2v(ra); - TValue *plimit = s2v(ra + 1); - TValue *pstep = s2v(ra + 2); savestate(L, ci); /* in case of errors */ - if (ttisinteger(pinit) && ttisinteger(pstep)) { /* integer loop? */ - lua_Integer init = ivalue(pinit); - lua_Integer step = ivalue(pstep); - lua_Integer limit; - if (step == 0) - luaG_runerror(L, "'for' step is zero"); - setivalue(s2v(ra + 3), init); /* control variable */ - if (forlimit(L, init, plimit, &limit, step)) - pc += GETARG_Bx(i) + 1; /* skip the loop */ - else { /* prepare loop counter */ - lua_Unsigned count; - if (step > 0) { /* ascending loop? */ - count = l_castS2U(limit) - l_castS2U(init); - if (step != 1) /* avoid division in the too common case */ - count /= l_castS2U(step); - } - else { /* step < 0; descending loop */ - count = l_castS2U(init) - l_castS2U(limit); - /* 'step+1' avoids negating 'mininteger' */ - count /= l_castS2U(-(step + 1)) + 1u; - } - /* store the counter in place of the limit (which won't be - needed anymore */ - setivalue(plimit, l_castU2S(count)); - } - } - else { /* try making all values floats */ - lua_Number init; lua_Number limit; lua_Number step; - if (unlikely(!tonumber(plimit, &limit))) - luaG_forerror(L, plimit, "limit"); - if (unlikely(!tonumber(pstep, &step))) - luaG_forerror(L, pstep, "step"); - if (unlikely(!tonumber(pinit, &init))) - luaG_forerror(L, pinit, "initial value"); - if (step == 0) - luaG_runerror(L, "'for' step is zero"); - if (luai_numlt(0, step) ? luai_numlt(limit, init) - : luai_numlt(init, limit)) - pc += GETARG_Bx(i) + 1; /* skip the loop */ - else { - /* make sure internal values are all float */ - setfltvalue(plimit, limit); - setfltvalue(pstep, step); - setfltvalue(s2v(ra), init); /* internal index */ - setfltvalue(s2v(ra + 3), init); /* control variable */ - } - } + if (forprep(L, ra)) + pc += GETARG_Bx(i) + 1; /* skip the loop */ vmbreak; } vmcase(OP_TFORPREP) { From e174f43807d46a7c0a9ab5eeb3fc4434bcb0091f Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 5 Dec 2019 12:57:40 -0300 Subject: [PATCH 0524/1145] Manual a little more clear about string->number coersions --- manual/manual.of | 41 +++++++++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/manual/manual.of b/manual/manual.of index 61d4afacc1..d5b4a572da 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -1406,10 +1406,9 @@ It has the following syntax: exp @bnfter{,} exp @bnfopt{@bnfter{,} exp} @Rw{do} block @Rw{end}} } The given identifier (@bnfNter{Name}) defines the control variable, -which is local to the loop body (@emph{block}). +which is a new variable local to the loop body (@emph{block}). -The loop starts by evaluating once the three control expressions; -they must all result in numbers. +The loop starts by evaluating once the three control expressions. Their values are called respectively the @emph{initial value}, the @emph{limit}, and the @emph{step}. If the step is absent, it defaults @N{to 1}. @@ -1417,8 +1416,9 @@ If the step is absent, it defaults @N{to 1}. If both the initial value and the step are integers, the loop is done with integers; note that the limit may not be an integer. -Otherwise, the loop is done with floats. -(Beware of floating-point accuracy in this case.) +Otherwise, the three values are converted to +floats and the loop is done with floats. +Beware of floating-point accuracy in this case. After that initialization, the loop body is repeated with the value of the control variable @@ -1773,9 +1773,24 @@ If it does, that representation is the result. Otherwise, the conversion fails. Several places in Lua coerce strings to numbers when necessary. +In particular, +the string library sets metamethods that try to coerce +strings to numbers in all arithmetic operations. +If the conversion fails, +the library calls the metamethod of the other operand +(if present) or it raises an error. +Note that bitwise operators do not do this coercion. + +Nonetheless, it is always a good practice not to rely on these +implicit coercions, as they are not always applied; +in particular, @T{"1"==1} is false and @T{"1"<1} raises an error +@see{rel-ops}. +These coercions exist mainly for compatibility and may be removed +in future versions of the language. + A string is converted to an integer or a float following its syntax and the rules of the Lua lexer. -(The string may have also leading and trailing whitespaces and a sign.) +The string may have also leading and trailing whitespaces and a sign. All conversions from strings to numbers accept both a dot and the current locale mark as the radix character. @@ -1783,15 +1798,9 @@ as the radix character. If the string is not a valid numeral, the conversion fails. If necessary, the result of this first step is then converted -to the required number subtype following the previous rules +to a specific number subtype following the previous rules for conversions between floats and integers. -The string library uses metamethods that try to coerce -strings to numbers in all arithmetic operations. -If the conversion fails, -the library calls the metamethod of the other operand -(if present) or it raises an error. - The conversion from numbers to strings uses a non-specified human-readable format. To convert numbers to strings in any specific way, @@ -7687,8 +7696,8 @@ This library provides basic mathematical functions. It provides all its functions and constants inside the table @defid{math}. Functions with the annotation @St{integer/float} give integer results for integer arguments -and float results for float (or mixed) arguments. -the rounding functions +and float results for non-integer arguments. +The rounding functions @Lid{math.ceil}, @Lid{math.floor}, and @Lid{math.modf} return an integer when the result fits in the range of an integer, or a float otherwise. @@ -7843,7 +7852,7 @@ The results from this function have good statistical qualities, but they are not cryptographically secure. (For instance, there are no guarantees that it is hard to predict future results based on the observation of -some number of previous results.) +some previous results.) } From 490ecfcaa1f25fcc17f9dcb0ed7216da54a391e3 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 5 Dec 2019 12:59:42 -0300 Subject: [PATCH 0525/1145] Better comments about the use of 'k' in opcodes --- lopcodes.h | 42 +++++++++++++++++++++++------------------- lvm.c | 22 +++++++++++----------- 2 files changed, 34 insertions(+), 30 deletions(-) diff --git a/lopcodes.h b/lopcodes.h index 382dec05fc..aec9dcbc84 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -217,7 +217,7 @@ OP_SETTABLE,/* A B C R[A][R[B]] := RK(C) */ OP_SETI,/* A B C R[A][B] := RK(C) */ OP_SETFIELD,/* A B C R[A][K[B]:string] := RK(C) */ -OP_NEWTABLE,/* A B C R[A] := {} */ +OP_NEWTABLE,/* A B C k R[A] := {} */ OP_SELF,/* A B C R[A+1] := R[B]; R[A] := R[B][RK(C):string] */ @@ -253,8 +253,8 @@ OP_SHL,/* A B C R[A] := R[B] << R[C] */ OP_SHR,/* A B C R[A] := R[B] >> R[C] */ OP_MMBIN,/* A B C call C metamethod over R[A] and R[B] */ -OP_MMBINI,/* A sB C call C metamethod over R[A] and sB */ -OP_MMBINK,/* A B C call C metamethod over R[A] and K[B] */ +OP_MMBINI,/* A sB C k call C metamethod over R[A] and sB */ +OP_MMBINK,/* A B C k call C metamethod over R[A] and K[B] */ OP_UNM,/* A B R[A] := -R[B] */ OP_BNOT,/* A B R[A] := ~R[B] */ @@ -266,24 +266,24 @@ OP_CONCAT,/* A B R[A] := R[A].. ... ..R[A + B - 1] */ OP_CLOSE,/* A close all upvalues >= R[A] */ OP_TBC,/* A mark variable A "to be closed" */ OP_JMP,/* sJ pc += sJ */ -OP_EQ,/* A B if ((R[A] == R[B]) ~= k) then pc++ */ -OP_LT,/* A B if ((R[A] < R[B]) ~= k) then pc++ */ -OP_LE,/* A B if ((R[A] <= R[B]) ~= k) then pc++ */ +OP_EQ,/* A B k if ((R[A] == R[B]) ~= k) then pc++ */ +OP_LT,/* A B k if ((R[A] < R[B]) ~= k) then pc++ */ +OP_LE,/* A B k if ((R[A] <= R[B]) ~= k) then pc++ */ -OP_EQK,/* A B if ((R[A] == K[B]) ~= k) then pc++ */ -OP_EQI,/* A sB if ((R[A] == sB) ~= k) then pc++ */ -OP_LTI,/* A sB if ((R[A] < sB) ~= k) then pc++ */ -OP_LEI,/* A sB if ((R[A] <= sB) ~= k) then pc++ */ -OP_GTI,/* A sB if ((R[A] > sB) ~= k) then pc++ */ -OP_GEI,/* A sB if ((R[A] >= sB) ~= k) then pc++ */ +OP_EQK,/* A B k if ((R[A] == K[B]) ~= k) then pc++ */ +OP_EQI,/* A sB k if ((R[A] == sB) ~= k) then pc++ */ +OP_LTI,/* A sB k if ((R[A] < sB) ~= k) then pc++ */ +OP_LEI,/* A sB k if ((R[A] <= sB) ~= k) then pc++ */ +OP_GTI,/* A sB k if ((R[A] > sB) ~= k) then pc++ */ +OP_GEI,/* A sB k if ((R[A] >= sB) ~= k) then pc++ */ -OP_TEST,/* A if (not R[A] == k) then pc++ */ -OP_TESTSET,/* A B if (not R[B] == k) then pc++ else R[A] := R[B] */ +OP_TEST,/* A k if (not R[A] == k) then pc++ */ +OP_TESTSET,/* A B k if (not R[B] == k) then pc++ else R[A] := R[B] */ OP_CALL,/* A B C R[A], ... ,R[A+C-2] := R[A](R[A+1], ... ,R[A+B-1]) */ -OP_TAILCALL,/* A B C return R[A](R[A+1], ... ,R[A+B-1]) */ +OP_TAILCALL,/* A B C k return R[A](R[A+1], ... ,R[A+B-1]) */ -OP_RETURN,/* A B C return R[A], ... ,R[A+B-2] (see note) */ +OP_RETURN,/* A B C k return R[A], ... ,R[A+B-2] (see note) */ OP_RETURN0,/* return */ OP_RETURN1,/* A return R[A] */ @@ -295,7 +295,7 @@ OP_TFORPREP,/* A Bx create upvalue for R[A + 3]; pc+=Bx */ OP_TFORCALL,/* A C R[A+4], ... ,R[A+3+C] := R[A](R[A+1], R[A+2]); */ OP_TFORLOOP,/* A Bx if R[A+2] ~= nil then { R[A]=R[A+2]; pc -= Bx } */ -OP_SETLIST,/* A B C R[A][(C-1)*FPF+i] := R[A+i], 1 <= i <= B */ +OP_SETLIST,/* A B C k R[A][(C-1)*FPF+i] := R[A+i], 1 <= i <= B */ OP_CLOSURE,/* A Bx R[A] := closure(KPROTO[Bx]) */ @@ -323,7 +323,7 @@ OP_EXTRAARG/* Ax extra (larger) argument for previous opcode */ (*) In OP_RETURN, if (B == 0) then return up to 'top'. (*) In OP_LOADKX and OP_NEWTABLE, the next instruction is always - EXTRAARG. + OP_EXTRAARG. (*) In OP_SETLIST, if (B == 0) then real B = 'top'; if k, then real C = EXTRAARG _ C (the bits of EXTRAARG concatenated with the @@ -336,6 +336,9 @@ OP_EXTRAARG/* Ax extra (larger) argument for previous opcode */ (*) For comparisons, k specifies what condition the test should accept (true or false). + (*) In OP_MMBINI/OP_MMBINK, k means the arguments were flipped + (the constant is the first operand). + (*) All 'skips' (pc++) assume that next instruction is a jump. (*) In instructions OP_RETURN/OP_TAILCALL, 'k' specifies that the @@ -344,7 +347,8 @@ OP_EXTRAARG/* Ax extra (larger) argument for previous opcode */ returning; in this case, (C - 1) is its number of fixed parameters. (*) In comparisons with an immediate operand, C signals whether the - original operand was a float. + original operand was a float. (It must be corrected in case of + metamethods.) ===========================================================================*/ diff --git a/lvm.c b/lvm.c index dc479f0ad9..d70ac7ace8 100644 --- a/lvm.c +++ b/lvm.c @@ -890,9 +890,9 @@ void luaV_finishOp (lua_State *L) { /* ** Auxiliary macro for arithmetic operations over floats and others ** with immediate operand. 'fop' is the float operation; 'tm' is the -** corresponding metamethod; 'flip' is true if operands were flipped. +** corresponding metamethod. */ -#define op_arithfI_aux(L,v1,imm,fop,tm,flip) { \ +#define op_arithfI_aux(L,v1,imm,fop,tm) { \ lua_Number nb; \ if (tonumberns(v1, nb)) { \ lua_Number fimm = cast_num(imm); \ @@ -912,14 +912,14 @@ void luaV_finishOp (lua_State *L) { ** Arithmetic operations with immediate operands. 'iop' is the integer ** operation. */ -#define op_arithI(L,iop,fop,tm,flip) { \ +#define op_arithI(L,iop,fop,tm) { \ TValue *v1 = vRB(i); \ int imm = GETARG_sC(i); \ if (ttisinteger(v1)) { \ lua_Integer iv1 = ivalue(v1); \ pc++; setivalue(s2v(ra), iop(L, iv1, imm)); \ } \ - else op_arithfI_aux(L, v1, imm, fop, tm, flip); } + else op_arithfI_aux(L, v1, imm, fop, tm); } /* @@ -958,7 +958,7 @@ void luaV_finishOp (lua_State *L) { /* ** Arithmetic operations with K operands. */ -#define op_arithK(L,iop,fop,flip) { \ +#define op_arithK(L,iop,fop) { \ TValue *v1 = vRB(i); \ TValue *v2 = KC(i); \ if (ttisinteger(v1) && ttisinteger(v2)) { \ @@ -1367,23 +1367,23 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_ADDI) { - op_arithI(L, l_addi, luai_numadd, TM_ADD, GETARG_k(i)); + op_arithI(L, l_addi, luai_numadd, TM_ADD); vmbreak; } vmcase(OP_ADDK) { - op_arithK(L, l_addi, luai_numadd, GETARG_k(i)); + op_arithK(L, l_addi, luai_numadd); vmbreak; } vmcase(OP_SUBK) { - op_arithK(L, l_subi, luai_numsub, 0); + op_arithK(L, l_subi, luai_numsub); vmbreak; } vmcase(OP_MULK) { - op_arithK(L, l_muli, luai_nummul, GETARG_k(i)); + op_arithK(L, l_muli, luai_nummul); vmbreak; } vmcase(OP_MODK) { - op_arithK(L, luaV_mod, luaV_modf, 0); + op_arithK(L, luaV_mod, luaV_modf); vmbreak; } vmcase(OP_POWK) { @@ -1395,7 +1395,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_IDIVK) { - op_arithK(L, luaV_idiv, luai_numidiv, 0); + op_arithK(L, luaV_idiv, luai_numidiv); vmbreak; } vmcase(OP_BANDK) { From 2d92102dee88a81711dca8e8ea3ef0ea9d732283 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 5 Dec 2019 13:31:07 -0300 Subject: [PATCH 0526/1145] 'l_mathlim' renamed to 'l_floatatt' That macro is applied to float attributes, not to limits. --- lmathlib.c | 2 +- lstrlib.c | 4 ++-- luaconf.h | 8 ++++---- lvm.c | 2 +- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/lmathlib.c b/lmathlib.c index f49eb318eb..7197fc5987 100644 --- a/lmathlib.c +++ b/lmathlib.c @@ -249,7 +249,7 @@ static int math_type (lua_State *L) { */ /* number of binary digits in the mantissa of a float */ -#define FIGS l_mathlim(MANT_DIG) +#define FIGS l_floatatt(MANT_DIG) #if FIGS > 64 /* there are only 64 random bits; use them all */ diff --git a/lstrlib.c b/lstrlib.c index 946461a8cc..586e0d787d 100644 --- a/lstrlib.c +++ b/lstrlib.c @@ -1004,7 +1004,7 @@ static int str_gsub (lua_State *L) { ** to nibble boundaries by making what is left after that first digit a ** multiple of 4. */ -#define L_NBFD ((l_mathlim(MANT_DIG) - 1)%4 + 1) +#define L_NBFD ((l_floatatt(MANT_DIG) - 1)%4 + 1) /* @@ -1072,7 +1072,7 @@ static int lua_number2strx (lua_State *L, char *buff, int sz, ** and '\0') + number of decimal digits to represent maxfloat (which ** is maximum exponent + 1). (99+3+1, adding some extra, 110) */ -#define MAX_ITEMF (110 + l_mathlim(MAX_10_EXP)) +#define MAX_ITEMF (110 + l_floatatt(MAX_10_EXP)) /* diff --git a/luaconf.h b/luaconf.h index 8f13743bd0..bdf927e77e 100644 --- a/luaconf.h +++ b/luaconf.h @@ -398,7 +398,7 @@ @@ LUA_NUMBER is the floating-point type used by Lua. @@ LUAI_UACNUMBER is the result of a 'default argument promotion' @@ over a floating number. -@@ l_mathlim(x) corrects limit name 'x' to the proper float type +@@ l_floatatt(x) corrects float attribute 'x' to the proper float type ** by prefixing it with one of FLT/DBL/LDBL. @@ LUA_NUMBER_FRMLEN is the length modifier for writing floats. @@ LUA_NUMBER_FMT is the format for writing floats. @@ -437,7 +437,7 @@ #define LUA_NUMBER float -#define l_mathlim(n) (FLT_##n) +#define l_floatatt(n) (FLT_##n) #define LUAI_UACNUMBER double @@ -453,7 +453,7 @@ #define LUA_NUMBER long double -#define l_mathlim(n) (LDBL_##n) +#define l_floatatt(n) (LDBL_##n) #define LUAI_UACNUMBER long double @@ -468,7 +468,7 @@ #define LUA_NUMBER double -#define l_mathlim(n) (DBL_##n) +#define l_floatatt(n) (DBL_##n) #define LUAI_UACNUMBER double diff --git a/lvm.c b/lvm.c index d70ac7ace8..db7b0eed0d 100644 --- a/lvm.c +++ b/lvm.c @@ -55,7 +55,7 @@ */ /* number of bits in the mantissa of a float */ -#define NBM (l_mathlim(MANT_DIG)) +#define NBM (l_floatatt(MANT_DIG)) /* ** Check whether some integers may not fit in a float, testing whether From d30569c06407529cc6e99f4a35ae5f9bfe6fa940 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 5 Dec 2019 14:14:29 -0300 Subject: [PATCH 0527/1145] Using an enumeration for float->integer coercion modes --- lcode.c | 4 ++-- ltable.c | 4 ++-- lvm.c | 25 +++++++++++-------------- lvm.h | 19 +++++++++++++++---- 4 files changed, 30 insertions(+), 22 deletions(-) diff --git a/lcode.c b/lcode.c index 2432b34610..25ddfb7aac 100644 --- a/lcode.c +++ b/lcode.c @@ -653,7 +653,7 @@ void luaK_int (FuncState *fs, int reg, lua_Integer i) { static void luaK_float (FuncState *fs, int reg, lua_Number f) { lua_Integer fi; - if (luaV_flttointeger(f, &fi, 0) && fitsBx(fi)) + if (luaV_flttointeger(f, &fi, F2Ieq) && fitsBx(fi)) luaK_codeAsBx(fs, OP_LOADF, reg, cast_int(fi)); else luaK_codek(fs, reg, luaK_numberK(fs, f)); @@ -1220,7 +1220,7 @@ static int isSCnumber (expdesc *e, int *pi, int *isfloat) { lua_Integer i; if (e->k == VKINT) i = e->u.ival; - else if (e->k == VKFLT && luaV_flttointeger(e->u.nval, &i, 0)) + else if (e->k == VKFLT && luaV_flttointeger(e->u.nval, &i, F2Ieq)) *isfloat = 1; else return 0; /* not a number */ diff --git a/ltable.c b/ltable.c index 4c7ae994c0..cc3c3dd4c1 100644 --- a/ltable.c +++ b/ltable.c @@ -626,7 +626,7 @@ TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) { else if (ttisfloat(key)) { lua_Number f = fltvalue(key); lua_Integer k; - if (luaV_flttointeger(f, &k, 0)) { /* does key fit in an integer? */ + if (luaV_flttointeger(f, &k, F2Ieq)) { /* does key fit in an integer? */ setivalue(&aux, k); key = &aux; /* insert it as an integer */ } @@ -745,7 +745,7 @@ const TValue *luaH_get (Table *t, const TValue *key) { case LUA_TNIL: return &absentkey; case LUA_TNUMFLT: { lua_Integer k; - if (luaV_flttointeger(fltvalue(key), &k, 0)) /* index is an integral? */ + if (luaV_flttointeger(fltvalue(key), &k, F2Ieq)) /* integral index? */ return luaH_getint(t, k); /* use specialized version */ /* else... */ } /* FALLTHROUGH */ diff --git a/lvm.c b/lvm.c index db7b0eed0d..576a945c69 100644 --- a/lvm.c +++ b/lvm.c @@ -116,16 +116,13 @@ int luaV_tonumber_ (const TValue *obj, lua_Number *n) { /* -** try to convert a float to an integer, rounding according to 'mode': -** mode == 0: accepts only integral values -** mode == 1: takes the floor of the number -** mode == 2: takes the ceil of the number +** try to convert a float to an integer, rounding according to 'mode'. */ -int luaV_flttointeger (lua_Number n, lua_Integer *p, int mode) { +int luaV_flttointeger (lua_Number n, lua_Integer *p, F2Imod mode) { lua_Number f = l_floor(n); if (n != f) { /* not an integral value? */ - if (mode == 0) return 0; /* fails if mode demands integral value */ - else if (mode == 2) /* needs ceil? */ + if (mode == F2Ieq) return 0; /* fails if mode demands integral value */ + else if (mode == F2Iceil) /* needs ceil? */ f += 1; /* convert floor to ceil (remember: n != f) */ } return lua_numbertointeger(f, p); @@ -137,7 +134,7 @@ int luaV_flttointeger (lua_Number n, lua_Integer *p, int mode) { ** without string coercion. ** ("Fast track" handled by macro 'tointegerns'.) */ -int luaV_tointegerns (const TValue *obj, lua_Integer *p, int mode) { +int luaV_tointegerns (const TValue *obj, lua_Integer *p, F2Imod mode) { if (ttisfloat(obj)) return luaV_flttointeger(fltvalue(obj), p, mode); else if (ttisinteger(obj)) { @@ -152,7 +149,7 @@ int luaV_tointegerns (const TValue *obj, lua_Integer *p, int mode) { /* ** try to convert a value to an integer. */ -int luaV_tointeger (const TValue *obj, lua_Integer *p, int mode) { +int luaV_tointeger (const TValue *obj, lua_Integer *p, F2Imod mode) { TValue v; if (l_strton(obj, &v)) /* does 'obj' point to a numerical string? */ obj = &v; /* change it to point to its corresponding number */ @@ -178,7 +175,7 @@ int luaV_tointeger (const TValue *obj, lua_Integer *p, int mode) { */ static int forlimit (lua_State *L, lua_Integer init, const TValue *lim, lua_Integer *p, lua_Integer step) { - if (!luaV_tointeger(lim, p, (step < 0 ? 2 : 1))) { + if (!luaV_tointeger(lim, p, (step < 0 ? F2Iceil : F2Ifloor))) { /* not coercible to in integer */ lua_Number flim; /* try to convert to float */ if (!tonumber(lim, &flim)) /* cannot convert to float? */ @@ -417,7 +414,7 @@ static int LTintfloat (lua_Integer i, lua_Number f) { return luai_numlt(cast_num(i), f); /* compare them as floats */ else { /* i < f <=> i < ceil(f) */ lua_Integer fi; - if (luaV_flttointeger(f, &fi, 2)) /* fi = ceil(f) */ + if (luaV_flttointeger(f, &fi, F2Iceil)) /* fi = ceil(f) */ return i < fi; /* compare them as integers */ else /* 'f' is either greater or less than all integers */ return f > 0; /* greater? */ @@ -434,7 +431,7 @@ static int LEintfloat (lua_Integer i, lua_Number f) { return luai_numle(cast_num(i), f); /* compare them as floats */ else { /* i <= f <=> i <= floor(f) */ lua_Integer fi; - if (luaV_flttointeger(f, &fi, 1)) /* fi = floor(f) */ + if (luaV_flttointeger(f, &fi, F2Ifloor)) /* fi = floor(f) */ return i <= fi; /* compare them as integers */ else /* 'f' is either greater or less than all integers */ return f > 0; /* greater? */ @@ -451,7 +448,7 @@ static int LTfloatint (lua_Number f, lua_Integer i) { return luai_numlt(f, cast_num(i)); /* compare them as floats */ else { /* f < i <=> floor(f) < i */ lua_Integer fi; - if (luaV_flttointeger(f, &fi, 1)) /* fi = floor(f) */ + if (luaV_flttointeger(f, &fi, F2Ifloor)) /* fi = floor(f) */ return fi < i; /* compare them as integers */ else /* 'f' is either greater or less than all integers */ return f < 0; /* less? */ @@ -468,7 +465,7 @@ static int LEfloatint (lua_Number f, lua_Integer i) { return luai_numle(f, cast_num(i)); /* compare them as floats */ else { /* f <= i <=> ceil(f) <= i */ lua_Integer fi; - if (luaV_flttointeger(f, &fi, 2)) /* fi = ceil(f) */ + if (luaV_flttointeger(f, &fi, F2Iceil)) /* fi = ceil(f) */ return fi <= i; /* compare them as integers */ else /* 'f' is either greater or less than all integers */ return f < 0; /* less? */ diff --git a/lvm.h b/lvm.h index 7e8ec7155e..71038572c1 100644 --- a/lvm.h +++ b/lvm.h @@ -33,10 +33,20 @@ ** integral values) */ #if !defined(LUA_FLOORN2I) -#define LUA_FLOORN2I 0 +#define LUA_FLOORN2I F2Ieq #endif +/* +** Rounding modes for float->integer coercion + */ +typedef enum { + F2Ieq, /* no rounding; accepts only integral values */ + F2Ifloor, /* takes the floor of the number */ + F2Iceil, /* takes the ceil of the number */ +} F2Imod; + + /* convert an object to a float (including string coercion) */ #define tonumber(o,n) \ (ttisfloat(o) ? (*(n) = fltvalue(o), 1) : luaV_tonumber_(o,n)) @@ -104,9 +114,10 @@ LUAI_FUNC int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2); LUAI_FUNC int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r); LUAI_FUNC int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r); LUAI_FUNC int luaV_tonumber_ (const TValue *obj, lua_Number *n); -LUAI_FUNC int luaV_tointeger (const TValue *obj, lua_Integer *p, int mode); -LUAI_FUNC int luaV_tointegerns (const TValue *obj, lua_Integer *p, int mode); -LUAI_FUNC int luaV_flttointeger (lua_Number n, lua_Integer *p, int mode); +LUAI_FUNC int luaV_tointeger (const TValue *obj, lua_Integer *p, F2Imod mode); +LUAI_FUNC int luaV_tointegerns (const TValue *obj, lua_Integer *p, + F2Imod mode); +LUAI_FUNC int luaV_flttointeger (lua_Number n, lua_Integer *p, F2Imod mode); LUAI_FUNC void luaV_finishget (lua_State *L, const TValue *t, TValue *key, StkId val, const TValue *slot); LUAI_FUNC void luaV_finishset (lua_State *L, const TValue *t, TValue *key, From 95735bda46278a4bc0966d8a3c9079dd0072c51e Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 5 Dec 2019 14:51:58 -0300 Subject: [PATCH 0528/1145] Simplifications in 'op_arith*' family of macros --- lvm.c | 74 ++++++++++++++++++++++------------------------------------- 1 file changed, 27 insertions(+), 47 deletions(-) diff --git a/lvm.c b/lvm.c index 576a945c69..b8d6e8281a 100644 --- a/lvm.c +++ b/lvm.c @@ -884,39 +884,22 @@ void luaV_finishOp (lua_State *L) { #define l_gei(a,b) (a >= b) -/* -** Auxiliary macro for arithmetic operations over floats and others -** with immediate operand. 'fop' is the float operation; 'tm' is the -** corresponding metamethod. -*/ -#define op_arithfI_aux(L,v1,imm,fop,tm) { \ - lua_Number nb; \ - if (tonumberns(v1, nb)) { \ - lua_Number fimm = cast_num(imm); \ - pc++; setfltvalue(s2v(ra), fop(L, nb, fimm)); \ - }} - - -/* -** Arithmetic operations over floats and others with immediate operand. -*/ -#define op_arithfI(L,fop,tm) { \ - TValue *v1 = vRB(i); \ - int imm = GETARG_sC(i); \ - op_arithfI_aux(L, v1, imm, fop, tm, 0); } - /* ** Arithmetic operations with immediate operands. 'iop' is the integer -** operation. +** operation, 'fop' is the float operation. */ -#define op_arithI(L,iop,fop,tm) { \ +#define op_arithI(L,iop,fop) { \ TValue *v1 = vRB(i); \ int imm = GETARG_sC(i); \ if (ttisinteger(v1)) { \ lua_Integer iv1 = ivalue(v1); \ pc++; setivalue(s2v(ra), iop(L, iv1, imm)); \ } \ - else op_arithfI_aux(L, v1, imm, fop, tm); } + else if (ttisfloat(v1)) { \ + lua_Number nb = fltvalue(v1); \ + lua_Number fimm = cast_num(imm); \ + pc++; setfltvalue(s2v(ra), fop(L, nb, fimm)); \ + }} /* @@ -940,11 +923,18 @@ void luaV_finishOp (lua_State *L) { /* -** Arithmetic operations with register operands. +** Arithmetic operations with K operands for floats. */ -#define op_arith(L,iop,fop) { \ +#define op_arithfK(L,fop) { \ TValue *v1 = vRB(i); \ - TValue *v2 = vRC(i); \ + TValue *v2 = KC(i); \ + op_arithf_aux(L, v1, v2, fop); } + + +/* +** Arithmetic operations over integers and floats. +*/ +#define op_arith_aux(L,v1,v2,iop,fop) { \ if (ttisinteger(v1) && ttisinteger(v2)) { \ lua_Integer i1 = ivalue(v1); lua_Integer i2 = ivalue(v2); \ pc++; setivalue(s2v(ra), iop(L, i1, i2)); \ @@ -953,32 +943,21 @@ void luaV_finishOp (lua_State *L) { /* -** Arithmetic operations with K operands. +** Arithmetic operations with register operands. */ -#define op_arithK(L,iop,fop) { \ +#define op_arith(L,iop,fop) { \ TValue *v1 = vRB(i); \ - TValue *v2 = KC(i); \ - if (ttisinteger(v1) && ttisinteger(v2)) { \ - lua_Integer i1 = ivalue(v1); lua_Integer i2 = ivalue(v2); \ - pc++; setivalue(s2v(ra), iop(L, i1, i2)); \ - } \ - else { \ - lua_Number n1; lua_Number n2; \ - if (tonumberns(v1, n1) && tonumberns(v2, n2)) { \ - pc++; setfltvalue(s2v(ra), fop(L, n1, n2)); \ - }}} + TValue *v2 = vRC(i); \ + op_arith_aux(L, v1, v2, iop, fop); } /* -** Arithmetic operations with K operands for floats. +** Arithmetic operations with K operands. */ -#define op_arithfK(L,fop) { \ +#define op_arithK(L,iop,fop) { \ TValue *v1 = vRB(i); \ TValue *v2 = KC(i); \ - lua_Number n1; lua_Number n2; \ - if (tonumberns(v1, n1) && tonumberns(v2, n2)) { \ - pc++; setfltvalue(s2v(ra), fop(L, n1, n2)); \ - }} + op_arith_aux(L, v1, v2, iop, fop); } /* @@ -1025,7 +1004,8 @@ void luaV_finishOp (lua_State *L) { /* -** Order operations with immediate operand. +** Order operations with immediate operand. (Immediate operand is +** always small enough to have an exact representation as a float.) */ #define op_orderI(L,opi,opf,inv,tm) { \ int cond; \ @@ -1364,7 +1344,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_ADDI) { - op_arithI(L, l_addi, luai_numadd, TM_ADD); + op_arithI(L, l_addi, luai_numadd); vmbreak; } vmcase(OP_ADDK) { From 1e0ad018cef2a8e771787f126ce2150028749411 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 10 Dec 2019 13:50:20 -0300 Subject: [PATCH 0529/1145] Comment about LUA_COMPAT_LT_LE moved to proper place --- ltm.c | 9 +++++++++ lvm.c | 9 +++------ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/ltm.c b/ltm.c index 1e32d86a5a..ca46f04e43 100644 --- a/ltm.c +++ b/ltm.c @@ -188,6 +188,15 @@ void luaT_trybiniTM (lua_State *L, const TValue *p1, lua_Integer i2, } +/* +** Calls an order tag method. +** For lessequal, LUA_COMPAT_LT_LE keeps compatibility with old +** behavior: if there is no '__le', try '__lt', based on l <= r iff +** !(r < l) (assuming a total order). If the metamethod yields during +** this substitution, the continuation has to know about it (to negate +** the result of rtop, event)) /* try original event */ diff --git a/lvm.c b/lvm.c index b8d6e8281a..78c0ebe7cc 100644 --- a/lvm.c +++ b/lvm.c @@ -541,11 +541,6 @@ int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r) { /* ** return 'l <= r' for non-numbers. -** If it needs a metamethod and there is no '__le', try '__lt', based -** on l <= r iff !(r < l) (assuming a total order). If the metamethod -** yields during this substitution, the continuation has to know about -** it (to negate the result of r Date: Fri, 13 Dec 2019 14:02:42 -0300 Subject: [PATCH 0530/1145] Small correction in assertion --- lapi.c | 2 +- llimits.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lapi.c b/lapi.c index ffc4d5e534..8b48f7f5af 100644 --- a/lapi.c +++ b/lapi.c @@ -230,7 +230,7 @@ LUA_API void lua_copy (lua_State *L, int fromidx, int toidx) { lua_lock(L); fr = index2value(L, fromidx); to = index2value(L, toidx); - api_check(l, isvalid(L, to), "invalid index"); + api_check(L, isvalid(L, to), "invalid index"); setobj(L, to, fr); if (isupvalue(toidx)) /* function upvalue? */ luaC_barrier(L, clCvalue(s2v(L->ci->func)), fr); diff --git a/llimits.h b/llimits.h index 2b52c83bde..b86d345256 100644 --- a/llimits.h +++ b/llimits.h @@ -99,7 +99,7 @@ typedef LUAI_UACINT l_uacInt; ** assertion for checking API calls */ #if !defined(luai_apicheck) -#define luai_apicheck(l,e) lua_assert(e) +#define luai_apicheck(l,e) ((void)l, lua_assert(e)) #endif #define api_check(l,e,msg) luai_apicheck(l,(e) && msg) From e0cbaa50fa7e97a8b7404041a59caac47b3949a5 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 17 Dec 2019 10:49:55 -0300 Subject: [PATCH 0531/1145] Added test for NULL in string.format("%p") ISO C states that standard library functions should not be called with NULL arguments, unless stated otherwise. 'sprintf' does not state otherwise, and it doesn't hurt to be on the safe side. --- lstrlib.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lstrlib.c b/lstrlib.c index 586e0d787d..e47a1d8d97 100644 --- a/lstrlib.c +++ b/lstrlib.c @@ -1271,6 +1271,8 @@ static int str_format (lua_State *L) { } case 'p': { const void *p = lua_topointer(L, arg); + if (p == NULL) + p = "(null)"; /* NULL not a valid parameter in ISO C 'printf' */ nb = l_sprintf(buff, maxitem, form, p); break; } From e0ab13c62f2c1af0af955f173beb3ea6473e8064 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 17 Dec 2019 14:24:30 -0300 Subject: [PATCH 0532/1145] Easy way to allow Unicode characters in identifiers For those that want to try it... --- lctype.c | 41 +++++++++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/lctype.c b/lctype.c index 4eaad167ed..9542280942 100644 --- a/lctype.c +++ b/lctype.c @@ -16,6 +16,15 @@ #include + +#if defined (LUA_UCID) /* accept UniCode IDentifiers? */ +/* consider all non-ascii codepoints to be alphabetic */ +#define NONA 0x01 +#else +#define NONA 0x00 /* default */ +#endif + + LUAI_DDEF const lu_byte luai_ctype_[UCHAR_MAX + 2] = { 0x00, /* EOZ */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0. */ @@ -34,22 +43,22 @@ LUAI_DDEF const lu_byte luai_ctype_[UCHAR_MAX + 2] = { 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, /* 7. */ 0x05, 0x05, 0x05, 0x04, 0x04, 0x04, 0x04, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 8. */ - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 9. */ - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* a. */ - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* b. */ - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* c. */ - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* d. */ - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* e. */ - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* f. */ - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, /* 8. */ + NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, + NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, /* 9. */ + NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, + NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, /* a. */ + NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, + NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, /* b. */ + NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, + 0x00, 0x00, NONA, NONA, NONA, NONA, NONA, NONA, /* c. */ + NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, + NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, /* d. */ + NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, + NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, /* e. */ + NONA, NONA, NONA, NONA, NONA, NONA, NONA, NONA, + NONA, NONA, NONA, NONA, NONA, 0x00, 0x00, 0x00, /* f. */ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; #endif /* } */ From c646e57fd6307bd891e4e50ef5d6ee56b34e4cac Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 17 Dec 2019 15:45:13 -0300 Subject: [PATCH 0533/1145] Joined common code in 'lua_rawset' and 'lua_rawsetp' --- lapi.c | 31 ++++++++++++++----------------- testes/api.lua | 6 ++++-- testes/strings.lua | 7 +++++-- 3 files changed, 23 insertions(+), 21 deletions(-) diff --git a/lapi.c b/lapi.c index 8b48f7f5af..b49d45c95a 100644 --- a/lapi.c +++ b/lapi.c @@ -848,42 +848,39 @@ LUA_API void lua_seti (lua_State *L, int idx, lua_Integer n) { } -LUA_API void lua_rawset (lua_State *L, int idx) { +static void aux_rawset (lua_State *L, int idx, TValue *key, int n) { Table *t; TValue *slot; lua_lock(L); - api_checknelems(L, 2); + api_checknelems(L, n); t = gettable(L, idx); - slot = luaH_set(L, t, s2v(L->top - 2)); + slot = luaH_set(L, t, key); setobj2t(L, slot, s2v(L->top - 1)); + L->top -= n; invalidateTMcache(t); luaC_barrierback(L, obj2gco(t), s2v(L->top - 1)); - L->top -= 2; lua_unlock(L); } -LUA_API void lua_rawseti (lua_State *L, int idx, lua_Integer n) { - Table *t; - lua_lock(L); - api_checknelems(L, 1); - t = gettable(L, idx); - luaH_setint(L, t, n, s2v(L->top - 1)); - luaC_barrierback(L, obj2gco(t), s2v(L->top - 1)); - L->top--; - lua_unlock(L); +LUA_API void lua_rawset (lua_State *L, int idx) { + aux_rawset(L, idx, s2v(L->top - 2), 2); } LUA_API void lua_rawsetp (lua_State *L, int idx, const void *p) { + TValue k; + setpvalue(&k, cast_voidp(p)); + aux_rawset(L, idx, &k, 1); +} + + +LUA_API void lua_rawseti (lua_State *L, int idx, lua_Integer n) { Table *t; - TValue k, *slot; lua_lock(L); api_checknelems(L, 1); t = gettable(L, idx); - setpvalue(&k, cast_voidp(p)); - slot = luaH_set(L, t, &k); - setobj2t(L, slot, s2v(L->top - 1)); + luaH_setint(L, t, n, s2v(L->top - 1)); luaC_barrierback(L, obj2gco(t), s2v(L->top - 1)); L->top--; lua_unlock(L); diff --git a/testes/api.lua b/testes/api.lua index b268063314..b5657416df 100644 --- a/testes/api.lua +++ b/testes/api.lua @@ -516,9 +516,11 @@ print"+" do -- getp/setp local a = {} - T.testC("rawsetp 2 1", a, 20) + local a1 = T.testC("rawsetp 2 1; return 1", a, 20) + assert(a == a1) assert(a[T.pushuserdata(1)] == 20) - assert(T.testC("rawgetp -1 1; return 1", a) == 20) + local a1, res = T.testC("rawgetp -1 1; return 2", a) + assert(a == a1 and res == 20) end diff --git a/testes/strings.lua b/testes/strings.lua index f2f61413a2..2ce3ebc338 100644 --- a/testes/strings.lua +++ b/testes/strings.lua @@ -161,18 +161,21 @@ do -- tests for '%p' format local null = string.format("%p", nil) assert(string.format("%p", {}) ~= null) assert(string.format("%p", 4) == null) + assert(string.format("%p", true) == null) assert(string.format("%p", print) ~= null) assert(string.format("%p", coroutine.running()) ~= null) + assert(string.format("%p", io.stdin) ~= null) + assert(string.format("%p", io.stdin) == string.format("%p", io.stdin)) do local t1 = {}; local t2 = {} assert(string.format("%p", t1) ~= string.format("%p", t2)) end - do -- short strings + do -- short strings are internalized local s1 = string.rep("a", 10) local s2 = string.rep("a", 10) assert(string.format("%p", s1) == string.format("%p", s2)) end - do -- long strings + do -- long strings aren't internalized local s1 = string.rep("a", 300); local s2 = string.rep("a", 300) assert(string.format("%p", s1) ~= string.format("%p", s2)) end From d7bb8df8414f71a290c8a4b1c9f7c6fe839a94df Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 27 Dec 2019 10:38:53 -0300 Subject: [PATCH 0534/1145] Copyright year changed to 2020 --- lua.h | 4 ++-- manual/2html | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lua.h b/lua.h index bd2631e5ce..0ae9e7c9b3 100644 --- a/lua.h +++ b/lua.h @@ -25,7 +25,7 @@ #define LUA_VERSION "Lua " LUA_VERSION_MAJOR "." LUA_VERSION_MINOR #define LUA_RELEASE LUA_VERSION "." LUA_VERSION_RELEASE -#define LUA_COPYRIGHT LUA_RELEASE " Copyright (C) 1994-2019 Lua.org, PUC-Rio" +#define LUA_COPYRIGHT LUA_RELEASE " Copyright (C) 1994-2020 Lua.org, PUC-Rio" #define LUA_AUTHORS "R. Ierusalimschy, L. H. de Figueiredo, W. Celes" @@ -489,7 +489,7 @@ struct lua_Debug { /****************************************************************************** -* Copyright (C) 1994-2019 Lua.org, PUC-Rio. +* Copyright (C) 1994-2020 Lua.org, PUC-Rio. * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the diff --git a/manual/2html b/manual/2html index a300f8d486..ea3957b9eb 100755 --- a/manual/2html +++ b/manual/2html @@ -30,7 +30,7 @@ by Roberto Ierusalimschy, Luiz Henrique de Figueiredo, Waldemar Celes

Copyright -© 2019 Lua.org, PUC-Rio. All rights reserved. +© 2020 Lua.org, PUC-Rio. All rights reserved.


From bd1b87c5790c0c6fe23f76aa360e879922e1e738 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 30 Dec 2019 11:45:08 -0300 Subject: [PATCH 0535/1145] Comments (mosty typos) --- lapi.h | 2 +- lcode.c | 2 +- lfunc.c | 2 +- lmem.c | 2 +- loslib.c | 2 +- ltests.c | 4 ++-- testes/api.lua | 4 ++-- testes/attrib.lua | 2 +- testes/big.lua | 2 +- testes/coroutine.lua | 2 +- testes/cstack.lua | 17 ++++++++++++----- testes/events.lua | 2 +- testes/gc.lua | 2 +- testes/locals.lua | 2 +- testes/math.lua | 2 +- 15 files changed, 28 insertions(+), 21 deletions(-) diff --git a/lapi.h b/lapi.h index f48d14fdf6..41216b2709 100644 --- a/lapi.h +++ b/lapi.h @@ -19,7 +19,7 @@ /* ** If a call returns too many multiple returns, the callee may not have -** stack space to accomodate all results. In this case, this macro +** stack space to accommodate all results. In this case, this macro ** increases its stack space ('L->ci->top'). */ #define adjustresults(L,nres) \ diff --git a/lcode.c b/lcode.c index 25ddfb7aac..4fc97e2bc8 100644 --- a/lcode.c +++ b/lcode.c @@ -1509,7 +1509,7 @@ static void codeeq (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) { lua_assert(e1->k == VK || e1->k == VKINT || e1->k == VKFLT); swapexps(e1, e2); } - r1 = luaK_exp2anyreg(fs, e1); /* 1nd expression must be in register */ + r1 = luaK_exp2anyreg(fs, e1); /* 1st expression must be in register */ if (isSCnumber(e2, &im, &isfloat)) { op = OP_EQI; r2 = im; /* immediate operand */ diff --git a/lfunc.c b/lfunc.c index 0ef7328435..60689a7a8b 100644 --- a/lfunc.c +++ b/lfunc.c @@ -165,7 +165,7 @@ static int callclosemth (lua_State *L, StkId level, int status) { if (newstatus != LUA_OK && status == CLOSEPROTECT) /* first error? */ status = newstatus; /* this will be the new error */ else { - if (newstatus != LUA_OK) /* supressed error? */ + if (newstatus != LUA_OK) /* suppressed error? */ luaE_warnerror(L, "__close metamethod"); /* leave original error (or nil) on top */ L->top = restorestack(L, oldtop); diff --git a/lmem.c b/lmem.c index b1d646a58c..65bfa5249f 100644 --- a/lmem.c +++ b/lmem.c @@ -26,7 +26,7 @@ /* ** First allocation will fail whenever not building initial state ** and not shrinking a block. (This fail will trigger 'tryagain' and -** a full GC cycle at every alocation.) +** a full GC cycle at every allocation.) */ static void *firsttry (global_State *g, void *block, size_t os, size_t ns) { if (ttisnil(&g->nilvalue) && ns > os) diff --git a/loslib.c b/loslib.c index 7812d29bc3..29449e4057 100644 --- a/loslib.c +++ b/loslib.c @@ -196,7 +196,7 @@ static int os_clock (lua_State *L) { */ /* -** About the overflow check: an overflow cannot occurr when time +** About the overflow check: an overflow cannot occur when time ** is represented by a lua_Integer, because either lua_Integer is ** large enough to represent all int fields or it is not large enough ** to represent a time that cause a field to overflow. However, if diff --git a/ltests.c b/ltests.c index bb5dad5475..e9b28b1433 100644 --- a/ltests.c +++ b/ltests.c @@ -476,7 +476,7 @@ static void checkrefs (global_State *g, GCObject *o) { /* ** Check consistency of an object: ** - Dead objects can only happen in the 'allgc' list during a sweep -** phase (controled by the caller through 'maybedead'). +** phase (controlled by the caller through 'maybedead'). ** - During pause, all objects must be white. ** - In generational mode: ** * objects must be old enough for their lists ('listage'). @@ -783,7 +783,7 @@ static int mem_query (lua_State *L) { return 1; } } - return luaL_error(L, "unkown type '%s'", t); + return luaL_error(L, "unknown type '%s'", t); } } diff --git a/testes/api.lua b/testes/api.lua index b5657416df..9447e42aef 100644 --- a/testes/api.lua +++ b/testes/api.lua @@ -805,7 +805,7 @@ F = function (x) if A ~= nil then assert(type(A) == "userdata") assert(T.udataval(A) == B) - debug.getmetatable(A) -- just acess it + debug.getmetatable(A) -- just access it end A = x -- ressucita userdata B = udval @@ -1112,7 +1112,7 @@ do -- non-closable value local a, b = pcall(T.makeCfunc[[ newtable # create non-closable object - toclose -1 # mark it to be closed (shoud raise an error) + toclose -1 # mark it to be closed (should raise an error) abort # will not be executed ]]) assert(a == false and diff --git a/testes/attrib.lua b/testes/attrib.lua index 76a447c848..b1076c768a 100644 --- a/testes/attrib.lua +++ b/testes/attrib.lua @@ -28,7 +28,7 @@ do local path = table.concat(t, ";") -- use that path in a search local s, err = package.searchpath("xuxu", path) - -- search fails; check that message has an occurence of + -- search fails; check that message has an occurrence of -- '??????????' with ? replaced by xuxu and at least 'max' lines assert(not s and string.find(err, string.rep("xuxu", 10)) and diff --git a/testes/big.lua b/testes/big.lua index 150d15dc06..39e293ef19 100644 --- a/testes/big.lua +++ b/testes/big.lua @@ -66,7 +66,7 @@ assert(repstrings * ssize > 2.0^32) -- it should be larger than maximum size local longs = string.rep("\0", ssize) -- create one long string --- create function to concatentate 'repstrings' copies of its argument +-- create function to concatenate 'repstrings' copies of its argument local rep = assert(load( "local a = ...; return " .. string.rep("a", repstrings, ".."))) diff --git a/testes/coroutine.lua b/testes/coroutine.lua index 81d848a387..73333c14d2 100644 --- a/testes/coroutine.lua +++ b/testes/coroutine.lua @@ -763,7 +763,7 @@ assert(run(function() return "a" .. "b" .. a .. "c" .. c .. b .. "x" end, {"concat", "concat", "concat"}) == "ab10chello12x") -do -- a few more tests for comparsion operators +do -- a few more tests for comparison operators local mt1 = { __le = function (a,b) coroutine.yield(10) diff --git a/testes/cstack.lua b/testes/cstack.lua index 486abc1df5..cd74fd281c 100644 --- a/testes/cstack.lua +++ b/testes/cstack.lua @@ -4,7 +4,7 @@ local debug = require "debug" print"testing C-stack overflow detection" -print"If this test craches, see its file ('cstack.lua')" +print"If this test crashes, see its file ('cstack.lua')" -- Segmentation faults in these tests probably result from a C-stack -- overflow. To avoid these errors, you can use the function @@ -19,10 +19,13 @@ print"If this test craches, see its file ('cstack.lua')" -- higher than 2_000. +-- get and print original limit local origlimit = debug.setcstacklimit(400) print("default stack limit: " .. origlimit) --- change this value for different limits for this test suite +-- Do the tests using the original limit. Or else you may want to change +-- 'currentlimit' to lower values to avoid a seg. fault or to higher +-- values to check whether they are reliable. local currentlimit = origlimit debug.setcstacklimit(currentlimit) print("current stack limit: " .. currentlimit) @@ -33,12 +36,14 @@ local function checkerror (msg, f, ...) assert(not s and string.find(err, msg)) end +-- auxiliary function to keep 'count' on the screen even if the program +-- crashes. local count local back = string.rep("\b", 8) local function progress () count = count + 1 local n = string.format("%-8d", count) - io.stderr:write(back, n) + io.stderr:write(back, n) -- erase previous value and write new one end @@ -46,7 +51,7 @@ do print("testing simple recursion:") count = 0 local function foo () progress() - foo() + foo() -- do recursive calls until a stack error (or crash) end checkerror("stack overflow", foo) print("\tfinal count: ", count) @@ -118,9 +123,11 @@ do print("testing changes in C-stack limit") return n end + -- set limit to 400 assert(debug.setcstacklimit(400) == currentlimit) local lim400 = check() - -- a very low limit (given that the several calls to arive here) + -- set a very low limit (given that there are already several active + -- calls to arrive here) local lowlimit = 38 assert(debug.setcstacklimit(lowlimit) == 400) assert(check() < lowlimit - 30) diff --git a/testes/events.lua b/testes/events.lua index 7fb54c9aec..d0abe1d428 100644 --- a/testes/events.lua +++ b/testes/events.lua @@ -182,7 +182,7 @@ assert(~a == a); checkcap{"bnot", a, a} assert(a << 3 == a); checkcap{"shl", a, 3} assert(1.5 >> a == 1.5); checkcap{"shr", 1.5, a} --- for comparsion operators, all results are true +-- for comparison operators, all results are true assert(5.0 > a); checkcap{"lt", a, 5.0} assert(a >= 10); checkcap{"le", 10, a} assert(a <= -10.0); checkcap{"le", a, -10.0} diff --git a/testes/gc.lua b/testes/gc.lua index bb4e349308..91915c0b81 100644 --- a/testes/gc.lua +++ b/testes/gc.lua @@ -640,7 +640,7 @@ do assert(getmetatable(o) == tt) -- create new objects during GC local a = 'xuxu'..(10+3)..'joao', {} - ___Glob = o -- ressurect object! + ___Glob = o -- ressurrect object! setmetatable({}, tt) -- creates a new one with same metatable print(">>> closing state " .. "<<<\n") end diff --git a/testes/locals.lua b/testes/locals.lua index b769575f8a..4f103be94a 100644 --- a/testes/locals.lua +++ b/testes/locals.lua @@ -114,7 +114,7 @@ if rawget(_G, "T") then local t = T.querytab(a) for k,_ in pairs(a) do a[k] = undef end - collectgarbage() -- restore GC and collect dead fiels in `a' + collectgarbage() -- restore GC and collect dead fields in 'a' for i=0,t-1 do local k = querytab(a, i) assert(k == nil or type(k) == 'number' or k == 'alo') diff --git a/testes/math.lua b/testes/math.lua index c7dc82851b..7248787b48 100644 --- a/testes/math.lua +++ b/testes/math.lua @@ -758,7 +758,7 @@ do -- testing max/min assert(eqT(math.min(maxint, maxint - 1), maxint - 1)) assert(eqT(math.min(maxint - 2, maxint, maxint - 1), maxint - 2)) end --- testing implicit convertions +-- testing implicit conversions local a,b = '10', '20' assert(a*b == 200 and a+b == 30 and a-b == -10 and a/b == 0.5 and -b == -20) From e3c83835e7b396ab7db538fb3b052f02d7807dee Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 30 Dec 2019 14:53:38 -0300 Subject: [PATCH 0536/1145] Fixed bug in 'aux_rawset' In 'aux_rawset', top must be decremented after the barrier, which refers to top-1. (Bug introduced in commit c646e57fd.) --- lapi.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lapi.c b/lapi.c index b49d45c95a..073baa4d69 100644 --- a/lapi.c +++ b/lapi.c @@ -856,9 +856,9 @@ static void aux_rawset (lua_State *L, int idx, TValue *key, int n) { t = gettable(L, idx); slot = luaH_set(L, t, key); setobj2t(L, slot, s2v(L->top - 1)); - L->top -= n; invalidateTMcache(t); luaC_barrierback(L, obj2gco(t), s2v(L->top - 1)); + L->top -= n; lua_unlock(L); } From 5ff408d2189c6c24fdf8908db4a31432bbdd6f15 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 6 Jan 2020 11:38:31 -0300 Subject: [PATCH 0537/1145] Changed internal representation of booleans Instead of an explicit value (field 'b'), true and false use different tag variants. This avoids reading an extra field and results in more direct code. (Most code that uses booleans needs to distinguish between true and false anyway.) --- lapi.c | 5 ++++- lcode.c | 50 ++++++++++++++++++++++++++++++++++--------------- ldebug.c | 2 +- ldump.c | 5 +---- ljumptab.h | 3 ++- llex.c | 2 +- lobject.h | 17 ++++++++++------- lopcodes.c | 3 ++- lopcodes.h | 3 ++- lopnames.h | 3 ++- ltable.c | 10 +++++----- lundump.c | 7 +++++-- lvm.c | 19 ++++++++++++------- testes/code.lua | 10 +++++----- 14 files changed, 87 insertions(+), 52 deletions(-) diff --git a/lapi.c b/lapi.c index 073baa4d69..0e99abefd3 100644 --- a/lapi.c +++ b/lapi.c @@ -574,7 +574,10 @@ LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) { LUA_API void lua_pushboolean (lua_State *L, int b) { lua_lock(L); - setbvalue(s2v(L->top), (b != 0)); /* ensure that true is 1 */ + if (b) + setbtvalue(s2v(L->top)); + else + setbfvalue(s2v(L->top)); api_incr_top(L); lua_unlock(L); } diff --git a/lcode.c b/lcode.c index 4fc97e2bc8..72a8820bea 100644 --- a/lcode.c +++ b/lcode.c @@ -84,8 +84,11 @@ int luaK_exp2const (FuncState *fs, const expdesc *e, TValue *v) { if (hasjumps(e)) return 0; /* not a constant */ switch (e->k) { - case VFALSE: case VTRUE: - setbvalue(v, e->k == VTRUE); + case VFALSE: + setbfvalue(v); + return 1; + case VTRUE: + setbtvalue(v); return 1; case VNIL: setnilvalue(v); @@ -604,11 +607,21 @@ static int luaK_numberK (FuncState *fs, lua_Number r) { /* -** Add a boolean to list of constants and return its index. +** Add a false to list of constants and return its index. */ -static int boolK (FuncState *fs, int b) { +static int boolF (FuncState *fs) { TValue o; - setbvalue(&o, b); + setbfvalue(&o); + return addk(fs, &o, &o); /* use boolean itself as key */ +} + + +/* +** Add a true to list of constants and return its index. +*/ +static int boolT (FuncState *fs) { + TValue o; + setbtvalue(&o); return addk(fs, &o, &o); /* use boolean itself as key */ } @@ -671,8 +684,11 @@ static void const2exp (TValue *v, expdesc *e) { case LUA_TNUMFLT: e->k = VKFLT; e->u.nval = fltvalue(v); break; - case LUA_TBOOLEAN: - e->k = bvalue(v) ? VTRUE : VFALSE; + case LUA_TFALSE: + e->k = VFALSE; + break; + case LUA_TTRUE: + e->k = VTRUE; break; case LUA_TNIL: e->k = VNIL; @@ -801,8 +817,12 @@ static void discharge2reg (FuncState *fs, expdesc *e, int reg) { luaK_nil(fs, reg, 1); break; } - case VFALSE: case VTRUE: { - luaK_codeABC(fs, OP_LOADBOOL, reg, e->k == VTRUE, 0); + case VFALSE: { + luaK_codeABC(fs, OP_LOADFALSE, reg, 0, 0); + break; + } + case VTRUE: { + luaK_codeABC(fs, OP_LOADTRUE, reg, 0, 0); break; } case VKSTR: { @@ -852,9 +872,9 @@ static void discharge2anyreg (FuncState *fs, expdesc *e) { } -static int code_loadbool (FuncState *fs, int A, int b, int jump) { +static int code_loadbool (FuncState *fs, int A, OpCode op, int jump) { luaK_getlabel(fs); /* those instructions may be jump targets */ - return luaK_codeABC(fs, OP_LOADBOOL, A, b, jump); + return luaK_codeABC(fs, op, A, jump, 0); } @@ -888,8 +908,8 @@ static void exp2reg (FuncState *fs, expdesc *e, int reg) { int p_t = NO_JUMP; /* position of an eventual LOAD true */ if (need_value(fs, e->t) || need_value(fs, e->f)) { int fj = (e->k == VJMP) ? NO_JUMP : luaK_jump(fs); - p_f = code_loadbool(fs, reg, 0, 1); /* load false and skip next i. */ - p_t = code_loadbool(fs, reg, 1, 0); /* load true */ + p_f = code_loadbool(fs, reg, OP_LOADFALSE, 1); /* skip next inst. */ + p_t = code_loadbool(fs, reg, OP_LOADTRUE, 0); /* jump around these booleans if 'e' is not a test */ luaK_patchtohere(fs, fj); } @@ -963,8 +983,8 @@ static int luaK_exp2K (FuncState *fs, expdesc *e) { if (!hasjumps(e)) { int info; switch (e->k) { /* move constants to 'k' */ - case VTRUE: info = boolK(fs, 1); break; - case VFALSE: info = boolK(fs, 0); break; + case VTRUE: info = boolT(fs); break; + case VFALSE: info = boolF(fs); break; case VNIL: info = nilK(fs); break; case VKINT: info = luaK_intK(fs, e->u.ival); break; case VKFLT: info = luaK_numberK(fs, e->u.nval); break; diff --git a/ldebug.c b/ldebug.c index 6e16b0fb10..c229f759f5 100644 --- a/ldebug.c +++ b/ldebug.c @@ -306,7 +306,7 @@ static void collectvalidlines (lua_State *L, Closure *f) { Table *t = luaH_new(L); /* new table to store active lines */ sethvalue2s(L, L->top, t); /* push it on stack */ api_incr_top(L); - setbvalue(&v, 1); /* boolean 'true' to be the value of all indices */ + setbtvalue(&v); /* boolean 'true' to be the value of all indices */ for (i = 0; i < p->sizelineinfo; i++) { /* for all lines with code */ currentline = nextline(p, currentline, i); luaH_setint(L, t, currentline, &v); /* table[line] = true */ diff --git a/ldump.c b/ldump.c index 9b501729e2..93cadbcc80 100644 --- a/ldump.c +++ b/ldump.c @@ -113,10 +113,7 @@ static void DumpConstants (const Proto *f, DumpState *D) { const TValue *o = &f->k[i]; DumpByte(ttypetag(o), D); switch (ttypetag(o)) { - case LUA_TNIL: - break; - case LUA_TBOOLEAN: - DumpByte(bvalue(o), D); + case LUA_TNIL: case LUA_TFALSE: case LUA_TTRUE: break; case LUA_TNUMFLT: DumpNumber(fltvalue(o), D); diff --git a/ljumptab.h b/ljumptab.h index 37fe1e25d2..22e9575f7e 100644 --- a/ljumptab.h +++ b/ljumptab.h @@ -30,7 +30,8 @@ static void *disptab[NUM_OPCODES] = { &&L_OP_LOADF, &&L_OP_LOADK, &&L_OP_LOADKX, -&&L_OP_LOADBOOL, +&&L_OP_LOADFALSE, +&&L_OP_LOADTRUE, &&L_OP_LOADNIL, &&L_OP_GETUPVAL, &&L_OP_SETUPVAL, diff --git a/llex.c b/llex.c index f88057fe73..90a7951f70 100644 --- a/llex.c +++ b/llex.c @@ -136,7 +136,7 @@ TString *luaX_newstring (LexState *ls, const char *str, size_t l) { if (isempty(o)) { /* not in use yet? */ /* boolean value does not need GC barrier; table is not a metatable, so it does not need to invalidate cache */ - setbvalue(o, 1); /* t[string] = true */ + setbtvalue(o); /* t[string] = true */ luaC_checkGC(L); } else { /* string already present */ diff --git a/lobject.h b/lobject.h index 7d30b46f98..a529ceba6c 100644 --- a/lobject.h +++ b/lobject.h @@ -44,7 +44,6 @@ typedef union Value { struct GCObject *gc; /* collectable objects */ void *p; /* light userdata */ - int b; /* booleans */ lua_CFunction f; /* light C functions */ lua_Integer i; /* integer numbers */ lua_Number n; /* float numbers */ @@ -210,16 +209,20 @@ typedef StackValue *StkId; ** =================================================================== */ -#define ttisboolean(o) checktag((o), LUA_TBOOLEAN) -#define bvalue(o) check_exp(ttisboolean(o), val_(o).b) +#define LUA_TFALSE (LUA_TBOOLEAN | (1 << 4)) +#define LUA_TTRUE (LUA_TBOOLEAN | (2 << 4)) -#define bvalueraw(v) ((v).b) +#define ttisboolean(o) checktype((o), LUA_TBOOLEAN) +#define ttisfalse(o) checktag((o), LUA_TFALSE) +#define ttistrue(o) checktag((o), LUA_TTRUE) -#define l_isfalse(o) (ttisboolean(o) ? bvalue(o) == 0 : ttisnil(o)) -#define setbvalue(obj,x) \ - { TValue *io=(obj); val_(io).b=(x); settt_(io, LUA_TBOOLEAN); } +#define l_isfalse(o) (ttisfalse(o) || ttisnil(o)) + + +#define setbfvalue(obj) settt_(obj, LUA_TFALSE) +#define setbtvalue(obj) settt_(obj, LUA_TTRUE) /* }================================================================== */ diff --git a/lopcodes.c b/lopcodes.c index 90d4cd1aae..f5347a3ceb 100644 --- a/lopcodes.c +++ b/lopcodes.c @@ -24,7 +24,8 @@ LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = { ,opmode(0, 0, 0, 0, 1, iAsBx) /* OP_LOADF */ ,opmode(0, 0, 0, 0, 1, iABx) /* OP_LOADK */ ,opmode(0, 0, 0, 0, 1, iABx) /* OP_LOADKX */ - ,opmode(0, 0, 0, 0, 1, iABC) /* OP_LOADBOOL */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_LOADFALSE */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_LOADTRUE */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_LOADNIL */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_GETUPVAL */ ,opmode(0, 0, 0, 0, 0, iABC) /* OP_SETUPVAL */ diff --git a/lopcodes.h b/lopcodes.h index aec9dcbc84..f512f15af4 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -202,7 +202,8 @@ OP_LOADI,/* A sBx R[A] := sBx */ OP_LOADF,/* A sBx R[A] := (lua_Number)sBx */ OP_LOADK,/* A Bx R[A] := K[Bx] */ OP_LOADKX,/* A R[A] := K[extra arg] */ -OP_LOADBOOL,/* A B C R[A] := (Bool)B; if (C) pc++ */ +OP_LOADFALSE,/* A B R[A] := false; if (B) pc++ */ +OP_LOADTRUE,/* A R[A] := true */ OP_LOADNIL,/* A B R[A], R[A+1], ..., R[A+B] := nil */ OP_GETUPVAL,/* A B R[A] := UpValue[B] */ OP_SETUPVAL,/* A B UpValue[B] := R[A] */ diff --git a/lopnames.h b/lopnames.h index de34730153..a2097a74fd 100644 --- a/lopnames.h +++ b/lopnames.h @@ -15,7 +15,8 @@ static const char *const opnames[] = { "LOADF", "LOADK", "LOADKX", - "LOADBOOL", + "LOADFALSE", + "LOADTRUE", "LOADNIL", "GETUPVAL", "SETUPVAL", diff --git a/ltable.c b/ltable.c index cc3c3dd4c1..ebd45dda83 100644 --- a/ltable.c +++ b/ltable.c @@ -143,8 +143,10 @@ static Node *mainposition (const Table *t, int ktt, const Value *kvl) { return hashstr(t, tsvalueraw(*kvl)); case LUA_TLNGSTR: return hashpow2(t, luaS_hashlongstr(tsvalueraw(*kvl))); - case LUA_TBOOLEAN: - return hashboolean(t, bvalueraw(*kvl)); + case LUA_TFALSE: + return hashboolean(t, 0); + case LUA_TTRUE: + return hashboolean(t, 1); case LUA_TLIGHTUSERDATA: return hashpointer(t, pvalueraw(*kvl)); case LUA_TLCF: @@ -175,14 +177,12 @@ static int equalkey (const TValue *k1, const Node *n2) { if (rawtt(k1) != keytt(n2)) /* not the same variants? */ return 0; /* cannot be same key */ switch (ttypetag(k1)) { - case LUA_TNIL: + case LUA_TNIL: case LUA_TFALSE: case LUA_TTRUE: return 1; case LUA_TNUMINT: return (ivalue(k1) == keyival(n2)); case LUA_TNUMFLT: return luai_numeq(fltvalue(k1), fltvalueraw(keyval(n2))); - case LUA_TBOOLEAN: - return bvalue(k1) == bvalueraw(keyval(n2)); case LUA_TLIGHTUSERDATA: return pvalue(k1) == pvalueraw(keyval(n2)); case LUA_TLCF: diff --git a/lundump.c b/lundump.c index 8f2a490c86..45e0b637d3 100644 --- a/lundump.c +++ b/lundump.c @@ -160,8 +160,11 @@ static void LoadConstants (LoadState *S, Proto *f) { case LUA_TNIL: setnilvalue(o); break; - case LUA_TBOOLEAN: - setbvalue(o, LoadByte(S)); + case LUA_TFALSE: + setbfvalue(o); + break; + case LUA_TTRUE: + setbtvalue(o); break; case LUA_TNUMFLT: setfltvalue(o, LoadNumber(S)); diff --git a/lvm.c b/lvm.c index 78c0ebe7cc..656def818f 100644 --- a/lvm.c +++ b/lvm.c @@ -577,10 +577,9 @@ int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2) { } /* values have same type and same variant */ switch (ttypetag(t1)) { - case LUA_TNIL: return 1; + case LUA_TNIL: case LUA_TFALSE: case LUA_TTRUE: return 1; case LUA_TNUMINT: return (ivalue(t1) == ivalue(t2)); case LUA_TNUMFLT: return luai_numeq(fltvalue(t1), fltvalue(t2)); - case LUA_TBOOLEAN: return bvalue(t1) == bvalue(t2); /* true must be 1! */ case LUA_TLIGHTUSERDATA: return pvalue(t1) == pvalue(t2); case LUA_TLCF: return fvalue(t1) == fvalue(t2); case LUA_TSHRSTR: return eqshrstr(tsvalue(t1), tsvalue(t2)); @@ -1182,9 +1181,13 @@ void luaV_execute (lua_State *L, CallInfo *ci) { setobj2s(L, ra, rb); vmbreak; } - vmcase(OP_LOADBOOL) { - setbvalue(s2v(ra), GETARG_B(i)); - if (GETARG_C(i)) pc++; /* skip next instruction (if C) */ + vmcase(OP_LOADFALSE) { + setbfvalue(s2v(ra)); + if (GETARG_B(i)) pc++; /* if B, skip next instruction */ + vmbreak; + } + vmcase(OP_LOADTRUE) { + setbtvalue(s2v(ra)); vmbreak; } vmcase(OP_LOADNIL) { @@ -1503,8 +1506,10 @@ void luaV_execute (lua_State *L, CallInfo *ci) { } vmcase(OP_NOT) { TValue *rb = vRB(i); - int nrb = l_isfalse(rb); /* next assignment may change this value */ - setbvalue(s2v(ra), nrb); + if (l_isfalse(rb)) + setbtvalue(s2v(ra)); + else + setbfvalue(s2v(ra)); vmbreak; } vmcase(OP_LEN) { diff --git a/testes/code.lua b/testes/code.lua index e12f3f9118..34b046688d 100644 --- a/testes/code.lua +++ b/testes/code.lua @@ -144,10 +144,10 @@ check(function (a,b,c,d) return a..b..c..d end, 'MOVE', 'MOVE', 'MOVE', 'MOVE', 'CONCAT', 'RETURN1') -- not -check(function () return not not nil end, 'LOADBOOL', 'RETURN1') -check(function () return not not kFalse end, 'LOADBOOL', 'RETURN1') -check(function () return not not true end, 'LOADBOOL', 'RETURN1') -check(function () return not not k3 end, 'LOADBOOL', 'RETURN1') +check(function () return not not nil end, 'LOADFALSE', 'RETURN1') +check(function () return not not kFalse end, 'LOADFALSE', 'RETURN1') +check(function () return not not true end, 'LOADTRUE', 'RETURN1') +check(function () return not not k3 end, 'LOADTRUE', 'RETURN1') -- direct access to locals check(function () @@ -194,7 +194,7 @@ check(function () local a,b a[kTrue] = false end, - 'LOADNIL', 'LOADBOOL', 'SETTABLE', 'RETURN0') + 'LOADNIL', 'LOADTRUE', 'SETTABLE', 'RETURN0') -- equalities From 69c7139ff88bf26e05d80bf19d0351e5c88d13a3 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 6 Jan 2020 14:50:36 -0300 Subject: [PATCH 0538/1145] New macro 'makevariant' to codify variant tags --- lobject.h | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/lobject.h b/lobject.h index a529ceba6c..62e4d05fb5 100644 --- a/lobject.h +++ b/lobject.h @@ -36,6 +36,9 @@ ** bit 6: whether value is collectable */ +/* add variant bits to a type */ +#define makevariant(t,v) ((t) | ((v) << 4)) + /* @@ -165,13 +168,13 @@ typedef StackValue *StkId; ** Variant tag, used only in tables to signal an empty slot ** (which might be different from a slot containing nil) */ -#define LUA_TEMPTY (LUA_TNIL | (1 << 4)) +#define LUA_TEMPTY makevariant(LUA_TNIL, 1) /* ** Variant used only in the value returned for a key not found in a ** table (absent key). */ -#define LUA_TABSTKEY (LUA_TNIL | (2 << 4)) +#define LUA_TABSTKEY makevariant(LUA_TNIL, 2) #define isabstkey(v) checktag((v), LUA_TABSTKEY) @@ -210,8 +213,8 @@ typedef StackValue *StkId; */ -#define LUA_TFALSE (LUA_TBOOLEAN | (1 << 4)) -#define LUA_TTRUE (LUA_TBOOLEAN | (2 << 4)) +#define LUA_TFALSE makevariant(LUA_TBOOLEAN, 1) +#define LUA_TTRUE makevariant(LUA_TBOOLEAN, 2) #define ttisboolean(o) checktype((o), LUA_TBOOLEAN) #define ttisfalse(o) checktag((o), LUA_TFALSE) @@ -292,8 +295,8 @@ typedef struct GCObject { */ /* Variant tags for numbers */ -#define LUA_TNUMFLT (LUA_TNUMBER | (1 << 4)) /* float numbers */ -#define LUA_TNUMINT (LUA_TNUMBER | (2 << 4)) /* integer numbers */ +#define LUA_TNUMINT makevariant(LUA_TNUMBER, 1) /* integer numbers */ +#define LUA_TNUMFLT makevariant(LUA_TNUMBER, 2) /* float numbers */ #define ttisnumber(o) checktype((o), LUA_TNUMBER) #define ttisfloat(o) checktag((o), LUA_TNUMFLT) @@ -329,8 +332,8 @@ typedef struct GCObject { */ /* Variant tags for strings */ -#define LUA_TSHRSTR (LUA_TSTRING | (1 << 4)) /* short strings */ -#define LUA_TLNGSTR (LUA_TSTRING | (2 << 4)) /* long strings */ +#define LUA_TSHRSTR makevariant(LUA_TSTRING, 1) /* short strings */ +#define LUA_TLNGSTR makevariant(LUA_TSTRING, 2) /* long strings */ #define ttisstring(o) checktype((o), LUA_TSTRING) #define ttisshrstring(o) checktag((o), ctb(LUA_TSHRSTR)) @@ -546,9 +549,9 @@ typedef struct Proto { */ /* Variant tags for functions */ -#define LUA_TLCL (LUA_TFUNCTION | (1 << 4)) /* Lua closure */ -#define LUA_TLCF (LUA_TFUNCTION | (2 << 4)) /* light C function */ -#define LUA_TCCL (LUA_TFUNCTION | (3 << 4)) /* C closure */ +#define LUA_TLCL makevariant(LUA_TFUNCTION, 1) /* Lua closure */ +#define LUA_TLCF makevariant(LUA_TFUNCTION, 2) /* light C function */ +#define LUA_TCCL makevariant(LUA_TFUNCTION, 3) /* C closure */ #define ttisfunction(o) checktype(o, LUA_TFUNCTION) #define ttisclosure(o) ((rawtt(o) & 0x1F) == LUA_TLCL) From 46c3587a6feb28e1ee4a32aabe463b0ecb9e8f5e Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 31 Jan 2020 11:09:53 -0300 Subject: [PATCH 0539/1145] Clearer distinction between types and tags LUA_T* represents only types; tags (types + Variants) are represented by LUA_V* constants. --- lapi.c | 22 ++++----- lcode.c | 12 ++--- ldebug.c | 2 +- ldo.c | 6 +-- ldump.c | 18 +++---- lfunc.c | 10 ++-- lgc.c | 58 +++++++++++------------ liolib.c | 4 +- lobject.h | 136 ++++++++++++++++++++++++++++++----------------------- lopcodes.h | 10 ++-- lstate.c | 4 +- lstate.h | 14 +++--- lstring.c | 10 ++-- lstring.h | 4 +- ltable.c | 46 +++++++++--------- ltests.c | 38 +++++++-------- ltests.h | 2 +- ltm.c | 2 +- ltm.h | 2 +- lua.h | 4 +- lundump.c | 14 +++--- lvm.c | 24 +++++----- 22 files changed, 230 insertions(+), 212 deletions(-) diff --git a/lapi.c b/lapi.c index 0e99abefd3..3e24781e0e 100644 --- a/lapi.c +++ b/lapi.c @@ -262,7 +262,7 @@ LUA_API int lua_type (lua_State *L, int idx) { LUA_API const char *lua_typename (lua_State *L, int t) { UNUSED(L); - api_check(L, LUA_TNONE <= t && t < LUA_NUMTAGS, "invalid tag"); + api_check(L, LUA_TNONE <= t && t < LUA_NUMTYPES, "invalid type"); return ttypename(t); } @@ -397,10 +397,10 @@ LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) { LUA_API lua_Unsigned lua_rawlen (lua_State *L, int idx) { const TValue *o = index2value(L, idx); switch (ttypetag(o)) { - case LUA_TSHRSTR: return tsvalue(o)->shrlen; - case LUA_TLNGSTR: return tsvalue(o)->u.lnglen; - case LUA_TUSERDATA: return uvalue(o)->len; - case LUA_TTABLE: return luaH_getn(hvalue(o)); + case LUA_VSHRSTR: return tsvalue(o)->shrlen; + case LUA_VLNGSTR: return tsvalue(o)->u.lnglen; + case LUA_VUSERDATA: return uvalue(o)->len; + case LUA_VTABLE: return luaH_getn(hvalue(o)); default: return 0; } } @@ -446,8 +446,8 @@ LUA_API lua_State *lua_tothread (lua_State *L, int idx) { LUA_API const void *lua_topointer (lua_State *L, int idx) { const TValue *o = index2value(L, idx); switch (ttypetag(o)) { - case LUA_TLCF: return cast_voidp(cast_sizet(fvalue(o))); - case LUA_TUSERDATA: case LUA_TLIGHTUSERDATA: + case LUA_VLCF: return cast_voidp(cast_sizet(fvalue(o))); + case LUA_VUSERDATA: case LUA_VLIGHTUSERDATA: return touserdata(o); default: { if (iscollectable(o)) @@ -1312,7 +1312,7 @@ LUA_API void *lua_newuserdatauv (lua_State *L, size_t size, int nuvalue) { static const char *aux_upvalue (TValue *fi, int n, TValue **val, GCObject **owner) { switch (ttypetag(fi)) { - case LUA_TCCL: { /* C closure */ + case LUA_VCCL: { /* C closure */ CClosure *f = clCvalue(fi); if (!(cast_uint(n) - 1u < cast_uint(f->nupvalues))) return NULL; /* 'n' not in [1, f->nupvalues] */ @@ -1320,7 +1320,7 @@ static const char *aux_upvalue (TValue *fi, int n, TValue **val, if (owner) *owner = obj2gco(f); return ""; } - case LUA_TLCL: { /* Lua closure */ + case LUA_VLCL: { /* Lua closure */ LClosure *f = clLvalue(fi); TString *name; Proto *p = f->p; @@ -1383,10 +1383,10 @@ static UpVal **getupvalref (lua_State *L, int fidx, int n, LClosure **pf) { LUA_API void *lua_upvalueid (lua_State *L, int fidx, int n) { TValue *fi = index2value(L, fidx); switch (ttypetag(fi)) { - case LUA_TLCL: { /* lua closure */ + case LUA_VLCL: { /* lua closure */ return *getupvalref(L, fidx, n, NULL); } - case LUA_TCCL: { /* C closure */ + case LUA_VCCL: { /* C closure */ CClosure *f = clCvalue(fi); api_check(L, 1 <= n && n <= f->nupvalues, "invalid upvalue index"); return &f->upvalue[n - 1]; diff --git a/lcode.c b/lcode.c index 72a8820bea..332fdd0084 100644 --- a/lcode.c +++ b/lcode.c @@ -678,22 +678,22 @@ static void luaK_float (FuncState *fs, int reg, lua_Number f) { */ static void const2exp (TValue *v, expdesc *e) { switch (ttypetag(v)) { - case LUA_TNUMINT: + case LUA_VNUMINT: e->k = VKINT; e->u.ival = ivalue(v); break; - case LUA_TNUMFLT: + case LUA_VNUMFLT: e->k = VKFLT; e->u.nval = fltvalue(v); break; - case LUA_TFALSE: + case LUA_VFALSE: e->k = VFALSE; break; - case LUA_TTRUE: + case LUA_VTRUE: e->k = VTRUE; break; - case LUA_TNIL: + case LUA_VNIL: e->k = VNIL; break; - case LUA_TSHRSTR: case LUA_TLNGSTR: + case LUA_VSHRSTR: case LUA_VLNGSTR: e->k = VKSTR; e->u.strval = tsvalue(v); break; default: lua_assert(0); diff --git a/ldebug.c b/ldebug.c index c229f759f5..a1913c5928 100644 --- a/ldebug.c +++ b/ldebug.c @@ -31,7 +31,7 @@ -#define noLuaClosure(f) ((f) == NULL || (f)->c.tt == LUA_TCCL) +#define noLuaClosure(f) ((f) == NULL || (f)->c.tt == LUA_VCCL) /* Active Lua function (given call info) */ diff --git a/ldo.c b/ldo.c index 288aef1352..64fe2915e4 100644 --- a/ldo.c +++ b/ldo.c @@ -459,10 +459,10 @@ void luaD_call (lua_State *L, StkId func, int nresults) { lua_CFunction f; retry: switch (ttypetag(s2v(func))) { - case LUA_TCCL: /* C closure */ + case LUA_VCCL: /* C closure */ f = clCvalue(s2v(func))->f; goto Cfunc; - case LUA_TLCF: /* light C function */ + case LUA_VLCF: /* light C function */ f = fvalue(s2v(func)); Cfunc: { int n; /* number of returns */ @@ -485,7 +485,7 @@ void luaD_call (lua_State *L, StkId func, int nresults) { luaD_poscall(L, ci, n); break; } - case LUA_TLCL: { /* Lua function */ + case LUA_VLCL: { /* Lua function */ CallInfo *ci; Proto *p = clLvalue(s2v(func))->p; int narg = cast_int(L->top - func) - 1; /* number of real arguments */ diff --git a/ldump.c b/ldump.c index 93cadbcc80..4d29b94e11 100644 --- a/ldump.c +++ b/ldump.c @@ -111,21 +111,21 @@ static void DumpConstants (const Proto *f, DumpState *D) { DumpInt(n, D); for (i = 0; i < n; i++) { const TValue *o = &f->k[i]; - DumpByte(ttypetag(o), D); - switch (ttypetag(o)) { - case LUA_TNIL: case LUA_TFALSE: case LUA_TTRUE: - break; - case LUA_TNUMFLT: + int tt = ttypetag(o); + DumpByte(tt, D); + switch (tt) { + case LUA_VNUMFLT: DumpNumber(fltvalue(o), D); break; - case LUA_TNUMINT: + case LUA_VNUMINT: DumpInteger(ivalue(o), D); break; - case LUA_TSHRSTR: - case LUA_TLNGSTR: + case LUA_VSHRSTR: + case LUA_VLNGSTR: DumpString(tsvalue(o), D); break; - default: lua_assert(0); + default: + lua_assert(tt == LUA_VNIL || tt == LUA_VFALSE || tt == LUA_VTRUE); } } } diff --git a/lfunc.c b/lfunc.c index 60689a7a8b..10100e5aca 100644 --- a/lfunc.c +++ b/lfunc.c @@ -25,7 +25,7 @@ CClosure *luaF_newCclosure (lua_State *L, int nupvals) { - GCObject *o = luaC_newobj(L, LUA_TCCL, sizeCclosure(nupvals)); + GCObject *o = luaC_newobj(L, LUA_VCCL, sizeCclosure(nupvals)); CClosure *c = gco2ccl(o); c->nupvalues = cast_byte(nupvals); return c; @@ -33,7 +33,7 @@ CClosure *luaF_newCclosure (lua_State *L, int nupvals) { LClosure *luaF_newLclosure (lua_State *L, int nupvals) { - GCObject *o = luaC_newobj(L, LUA_TLCL, sizeLclosure(nupvals)); + GCObject *o = luaC_newobj(L, LUA_VLCL, sizeLclosure(nupvals)); LClosure *c = gco2lcl(o); c->p = NULL; c->nupvalues = cast_byte(nupvals); @@ -48,7 +48,7 @@ LClosure *luaF_newLclosure (lua_State *L, int nupvals) { void luaF_initupvals (lua_State *L, LClosure *cl) { int i; for (i = 0; i < cl->nupvalues; i++) { - GCObject *o = luaC_newobj(L, LUA_TUPVAL, sizeof(UpVal)); + GCObject *o = luaC_newobj(L, LUA_VUPVAL, sizeof(UpVal)); UpVal *uv = gco2upv(o); uv->v = &uv->u.value; /* make it closed */ setnilvalue(uv->v); @@ -63,7 +63,7 @@ void luaF_initupvals (lua_State *L, LClosure *cl) { ** open upvalues of 'L' after entry 'prev'. **/ static UpVal *newupval (lua_State *L, int tbc, StkId level, UpVal **prev) { - GCObject *o = luaC_newobj(L, LUA_TUPVAL, sizeof(UpVal)); + GCObject *o = luaC_newobj(L, LUA_VUPVAL, sizeof(UpVal)); UpVal *uv = gco2upv(o); UpVal *next = *prev; uv->v = s2v(level); /* current value lives in the stack */ @@ -243,7 +243,7 @@ int luaF_close (lua_State *L, StkId level, int status) { Proto *luaF_newproto (lua_State *L) { - GCObject *o = luaC_newobj(L, LUA_TPROTO, sizeof(Proto)); + GCObject *o = luaC_newobj(L, LUA_VPROTO, sizeof(Proto)); Proto *f = gco2p(o); f->k = NULL; f->sizek = 0; diff --git a/lgc.c b/lgc.c index db519c6dfd..e788843cdd 100644 --- a/lgc.c +++ b/lgc.c @@ -119,12 +119,12 @@ static void entersweep (lua_State *L); static GCObject **getgclist (GCObject *o) { switch (o->tt) { - case LUA_TTABLE: return &gco2t(o)->gclist; - case LUA_TLCL: return &gco2lcl(o)->gclist; - case LUA_TCCL: return &gco2ccl(o)->gclist; - case LUA_TTHREAD: return &gco2th(o)->gclist; - case LUA_TPROTO: return &gco2p(o)->gclist; - case LUA_TUSERDATA: { + case LUA_VTABLE: return &gco2t(o)->gclist; + case LUA_VLCL: return &gco2lcl(o)->gclist; + case LUA_VCCL: return &gco2ccl(o)->gclist; + case LUA_VTHREAD: return &gco2th(o)->gclist; + case LUA_VPROTO: return &gco2p(o)->gclist; + case LUA_VUSERDATA: { Udata *u = gco2u(o); lua_assert(u->nuvalue > 0); return &u->gclist; @@ -268,19 +268,19 @@ GCObject *luaC_newobj (lua_State *L, int tt, size_t sz) { static void reallymarkobject (global_State *g, GCObject *o) { white2gray(o); switch (o->tt) { - case LUA_TSHRSTR: - case LUA_TLNGSTR: { + case LUA_VSHRSTR: + case LUA_VLNGSTR: { gray2black(o); break; } - case LUA_TUPVAL: { + case LUA_VUPVAL: { UpVal *uv = gco2upv(o); if (!upisopen(uv)) /* open upvalues are kept gray */ gray2black(o); markvalue(g, uv->v); /* mark its content */ break; } - case LUA_TUSERDATA: { + case LUA_VUSERDATA: { Udata *u = gco2u(o); if (u->nuvalue == 0) { /* no user values? */ markobjectN(g, u->metatable); /* mark its metatable */ @@ -289,8 +289,8 @@ static void reallymarkobject (global_State *g, GCObject *o) { } /* else... */ } /* FALLTHROUGH */ - case LUA_TLCL: case LUA_TCCL: case LUA_TTABLE: - case LUA_TTHREAD: case LUA_TPROTO: { + case LUA_VLCL: case LUA_VCCL: case LUA_VTABLE: + case LUA_VTHREAD: case LUA_VPROTO: { linkobjgclist(o, g->gray); break; } @@ -598,12 +598,12 @@ static lu_mem propagatemark (global_State *g) { gray2black(o); g->gray = *getgclist(o); /* remove from 'gray' list */ switch (o->tt) { - case LUA_TTABLE: return traversetable(g, gco2t(o)); - case LUA_TUSERDATA: return traverseudata(g, gco2u(o)); - case LUA_TLCL: return traverseLclosure(g, gco2lcl(o)); - case LUA_TCCL: return traverseCclosure(g, gco2ccl(o)); - case LUA_TPROTO: return traverseproto(g, gco2p(o)); - case LUA_TTHREAD: { + case LUA_VTABLE: return traversetable(g, gco2t(o)); + case LUA_VUSERDATA: return traverseudata(g, gco2u(o)); + case LUA_VLCL: return traverseLclosure(g, gco2lcl(o)); + case LUA_VCCL: return traverseCclosure(g, gco2ccl(o)); + case LUA_VPROTO: return traverseproto(g, gco2p(o)); + case LUA_VTHREAD: { lua_State *th = gco2th(o); linkgclist(th, g->grayagain); /* insert into 'grayagain' list */ black2gray(o); @@ -710,34 +710,34 @@ static void freeupval (lua_State *L, UpVal *uv) { static void freeobj (lua_State *L, GCObject *o) { switch (o->tt) { - case LUA_TPROTO: + case LUA_VPROTO: luaF_freeproto(L, gco2p(o)); break; - case LUA_TUPVAL: + case LUA_VUPVAL: freeupval(L, gco2upv(o)); break; - case LUA_TLCL: + case LUA_VLCL: luaM_freemem(L, o, sizeLclosure(gco2lcl(o)->nupvalues)); break; - case LUA_TCCL: + case LUA_VCCL: luaM_freemem(L, o, sizeCclosure(gco2ccl(o)->nupvalues)); break; - case LUA_TTABLE: + case LUA_VTABLE: luaH_free(L, gco2t(o)); break; - case LUA_TTHREAD: + case LUA_VTHREAD: luaE_freethread(L, gco2th(o)); break; - case LUA_TUSERDATA: { + case LUA_VUSERDATA: { Udata *u = gco2u(o); luaM_freemem(L, o, sizeudata(u->nuvalue, u->len)); break; } - case LUA_TSHRSTR: + case LUA_VSHRSTR: luaS_remove(L, gco2ts(o)); /* remove it from hash table */ luaM_freemem(L, o, sizelstring(gco2ts(o)->shrlen)); break; - case LUA_TLNGSTR: + case LUA_VLNGSTR: luaM_freemem(L, o, sizelstring(gco2ts(o)->u.lnglen)); break; default: lua_assert(0); @@ -1049,7 +1049,7 @@ static GCObject **correctgraylist (GCObject **p) { GCObject *curr; while ((curr = *p) != NULL) { switch (curr->tt) { - case LUA_TTABLE: case LUA_TUSERDATA: { + case LUA_VTABLE: case LUA_VUSERDATA: { GCObject **next = getgclist(curr); if (getage(curr) == G_TOUCHED1) { /* touched in this cycle? */ lua_assert(isgray(curr)); @@ -1069,7 +1069,7 @@ static GCObject **correctgraylist (GCObject **p) { } break; } - case LUA_TTHREAD: { + case LUA_VTHREAD: { lua_State *th = gco2th(curr); lua_assert(!isblack(th)); if (iswhite(th)) /* new object? */ diff --git a/liolib.c b/liolib.c index d8b0a6f9de..08d18397f9 100644 --- a/liolib.c +++ b/liolib.c @@ -215,7 +215,7 @@ static int f_close (lua_State *L) { static int io_close (lua_State *L) { if (lua_isnone(L, 1)) /* no argument? */ - lua_getfield(L, LUA_REGISTRYINDEX, IO_OUTPUT); /* use standard output */ + lua_getfield(L, LUA_REGISTRYINDEX, IO_OUTPUT); /* use default output */ return f_close(L); } @@ -296,7 +296,7 @@ static FILE *getiofile (lua_State *L, const char *findex) { lua_getfield(L, LUA_REGISTRYINDEX, findex); p = (LStream *)lua_touserdata(L, -1); if (isclosed(p)) - luaL_error(L, "standard %s file is closed", findex + IOPREF_LEN); + luaL_error(L, "default %s file is closed", findex + IOPREF_LEN); return p->f; } diff --git a/lobject.h b/lobject.h index 62e4d05fb5..32542294b6 100644 --- a/lobject.h +++ b/lobject.h @@ -17,16 +17,16 @@ /* -** Extra tags for collectable non-values +** Extra types for collectable non-values */ -#define LUA_TUPVAL LUA_NUMTAGS /* upvalues */ -#define LUA_TPROTO (LUA_NUMTAGS+1) /* function prototypes */ +#define LUA_TUPVAL LUA_NUMTYPES /* upvalues */ +#define LUA_TPROTO (LUA_NUMTYPES+1) /* function prototypes */ /* -** number of all possible tags (including LUA_TNONE) +** number of all possible types (including LUA_TNONE) */ -#define LUA_TOTALTAGS (LUA_TPROTO + 2) +#define LUA_TOTALTYPES (LUA_TPROTO + 2) /* @@ -154,30 +154,28 @@ typedef StackValue *StkId; ** =================================================================== */ -/* macro to test for (any kind of) nil */ -#define ttisnil(v) checktype((v), LUA_TNIL) +/* Standard nil */ +#define LUA_VNIL makevariant(LUA_TNIL, 1) -/* macro to test for a "pure" nil */ -#define ttisstrictnil(o) checktag((o), LUA_TNIL) +/* Empty slot (which might be different from a slot containing nil) */ +#define LUA_VEMPTY makevariant(LUA_TNIL, 2) +/* Value returned for a key not found in a table (absent key) */ +#define LUA_VABSTKEY makevariant(LUA_TNIL, 3) -#define setnilvalue(obj) settt_(obj, LUA_TNIL) +/* macro to test for (any kind of) nil */ +#define ttisnil(v) checktype((v), LUA_TNIL) -/* -** Variant tag, used only in tables to signal an empty slot -** (which might be different from a slot containing nil) -*/ -#define LUA_TEMPTY makevariant(LUA_TNIL, 1) -/* -** Variant used only in the value returned for a key not found in a -** table (absent key). -*/ -#define LUA_TABSTKEY makevariant(LUA_TNIL, 2) +/* macro to test for a standard nil */ +#define ttisstrictnil(o) checktag((o), LUA_VNIL) + + +#define setnilvalue(obj) settt_(obj, LUA_VNIL) -#define isabstkey(v) checktag((v), LUA_TABSTKEY) +#define isabstkey(v) checktag((v), LUA_VABSTKEY) /* @@ -195,11 +193,11 @@ typedef StackValue *StkId; /* macro defining a value corresponding to an absent key */ -#define ABSTKEYCONSTANT {NULL}, LUA_TABSTKEY +#define ABSTKEYCONSTANT {NULL}, LUA_VABSTKEY /* mark an entry as empty */ -#define setempty(v) settt_(v, LUA_TEMPTY) +#define setempty(v) settt_(v, LUA_VEMPTY) @@ -213,19 +211,19 @@ typedef StackValue *StkId; */ -#define LUA_TFALSE makevariant(LUA_TBOOLEAN, 1) -#define LUA_TTRUE makevariant(LUA_TBOOLEAN, 2) +#define LUA_VFALSE makevariant(LUA_TBOOLEAN, 1) +#define LUA_VTRUE makevariant(LUA_TBOOLEAN, 2) #define ttisboolean(o) checktype((o), LUA_TBOOLEAN) -#define ttisfalse(o) checktag((o), LUA_TFALSE) -#define ttistrue(o) checktag((o), LUA_TTRUE) +#define ttisfalse(o) checktag((o), LUA_VFALSE) +#define ttistrue(o) checktag((o), LUA_VTRUE) #define l_isfalse(o) (ttisfalse(o) || ttisnil(o)) -#define setbfvalue(obj) settt_(obj, LUA_TFALSE) -#define setbtvalue(obj) settt_(obj, LUA_TTRUE) +#define setbfvalue(obj) settt_(obj, LUA_VFALSE) +#define setbtvalue(obj) settt_(obj, LUA_VTRUE) /* }================================================================== */ @@ -236,13 +234,15 @@ typedef StackValue *StkId; ** =================================================================== */ -#define ttisthread(o) checktag((o), ctb(LUA_TTHREAD)) +#define LUA_VTHREAD makevariant(LUA_TTHREAD, 1) + +#define ttisthread(o) checktag((o), ctb(LUA_VTHREAD)) #define thvalue(o) check_exp(ttisthread(o), gco2th(val_(o).gc)) #define setthvalue(L,obj,x) \ { TValue *io = (obj); lua_State *x_ = (x); \ - val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TTHREAD)); \ + val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VTHREAD)); \ checkliveness(L,io); } #define setthvalue2s(L,o,t) setthvalue(L,s2v(o),t) @@ -295,12 +295,12 @@ typedef struct GCObject { */ /* Variant tags for numbers */ -#define LUA_TNUMINT makevariant(LUA_TNUMBER, 1) /* integer numbers */ -#define LUA_TNUMFLT makevariant(LUA_TNUMBER, 2) /* float numbers */ +#define LUA_VNUMINT makevariant(LUA_TNUMBER, 1) /* integer numbers */ +#define LUA_VNUMFLT makevariant(LUA_TNUMBER, 2) /* float numbers */ #define ttisnumber(o) checktype((o), LUA_TNUMBER) -#define ttisfloat(o) checktag((o), LUA_TNUMFLT) -#define ttisinteger(o) checktag((o), LUA_TNUMINT) +#define ttisfloat(o) checktag((o), LUA_VNUMFLT) +#define ttisinteger(o) checktag((o), LUA_VNUMINT) #define nvalue(o) check_exp(ttisnumber(o), \ (ttisinteger(o) ? cast_num(ivalue(o)) : fltvalue(o))) @@ -311,13 +311,13 @@ typedef struct GCObject { #define ivalueraw(v) ((v).i) #define setfltvalue(obj,x) \ - { TValue *io=(obj); val_(io).n=(x); settt_(io, LUA_TNUMFLT); } + { TValue *io=(obj); val_(io).n=(x); settt_(io, LUA_VNUMFLT); } #define chgfltvalue(obj,x) \ { TValue *io=(obj); lua_assert(ttisfloat(io)); val_(io).n=(x); } #define setivalue(obj,x) \ - { TValue *io=(obj); val_(io).i=(x); settt_(io, LUA_TNUMINT); } + { TValue *io=(obj); val_(io).i=(x); settt_(io, LUA_VNUMINT); } #define chgivalue(obj,x) \ { TValue *io=(obj); lua_assert(ttisinteger(io)); val_(io).i=(x); } @@ -332,12 +332,12 @@ typedef struct GCObject { */ /* Variant tags for strings */ -#define LUA_TSHRSTR makevariant(LUA_TSTRING, 1) /* short strings */ -#define LUA_TLNGSTR makevariant(LUA_TSTRING, 2) /* long strings */ +#define LUA_VSHRSTR makevariant(LUA_TSTRING, 1) /* short strings */ +#define LUA_VLNGSTR makevariant(LUA_TSTRING, 2) /* long strings */ #define ttisstring(o) checktype((o), LUA_TSTRING) -#define ttisshrstring(o) checktag((o), ctb(LUA_TSHRSTR)) -#define ttislngstring(o) checktag((o), ctb(LUA_TLNGSTR)) +#define ttisshrstring(o) checktag((o), ctb(LUA_VSHRSTR)) +#define ttislngstring(o) checktag((o), ctb(LUA_VLNGSTR)) #define tsvalueraw(v) (gco2ts((v).gc)) @@ -384,7 +384,7 @@ typedef struct TString { #define svalue(o) getstr(tsvalue(o)) /* get string length from 'TString *s' */ -#define tsslen(s) ((s)->tt == LUA_TSHRSTR ? (s)->shrlen : (s)->u.lnglen) +#define tsslen(s) ((s)->tt == LUA_VSHRSTR ? (s)->shrlen : (s)->u.lnglen) /* get string length from 'TValue *o' */ #define vslen(o) tsslen(tsvalue(o)) @@ -398,8 +398,16 @@ typedef struct TString { ** =================================================================== */ -#define ttislightuserdata(o) checktag((o), LUA_TLIGHTUSERDATA) -#define ttisfulluserdata(o) checktype((o), LUA_TUSERDATA) + +/* +** Light userdata should be a variant of userdata, but for compatibility +** reasons they are also different types. +*/ +#define LUA_VLIGHTUSERDATA makevariant(LUA_TLIGHTUSERDATA, 1) +#define LUA_VUSERDATA makevariant(LUA_TUSERDATA, 1) + +#define ttislightuserdata(o) checktag((o), LUA_VLIGHTUSERDATA) +#define ttisfulluserdata(o) checktag((o), ctb(LUA_VUSERDATA)) #define pvalue(o) check_exp(ttislightuserdata(o), val_(o).p) #define uvalue(o) check_exp(ttisfulluserdata(o), gco2u(val_(o).gc)) @@ -407,11 +415,11 @@ typedef struct TString { #define pvalueraw(v) ((v).p) #define setpvalue(obj,x) \ - { TValue *io=(obj); val_(io).p=(x); settt_(io, LUA_TLIGHTUSERDATA); } + { TValue *io=(obj); val_(io).p=(x); settt_(io, LUA_VLIGHTUSERDATA); } #define setuvalue(L,obj,x) \ { TValue *io = (obj); Udata *x_ = (x); \ - val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TUSERDATA)); \ + val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VUSERDATA)); \ checkliveness(L,io); } @@ -474,6 +482,9 @@ typedef struct Udata0 { ** =================================================================== */ +#define LUA_VPROTO makevariant(LUA_TPROTO, 1) + + /* ** Description of an upvalue for function prototypes */ @@ -548,16 +559,19 @@ typedef struct Proto { ** =================================================================== */ +#define LUA_VUPVAL makevariant(LUA_TUPVAL, 1) + + /* Variant tags for functions */ -#define LUA_TLCL makevariant(LUA_TFUNCTION, 1) /* Lua closure */ -#define LUA_TLCF makevariant(LUA_TFUNCTION, 2) /* light C function */ -#define LUA_TCCL makevariant(LUA_TFUNCTION, 3) /* C closure */ +#define LUA_VLCL makevariant(LUA_TFUNCTION, 1) /* Lua closure */ +#define LUA_VLCF makevariant(LUA_TFUNCTION, 2) /* light C function */ +#define LUA_VCCL makevariant(LUA_TFUNCTION, 3) /* C closure */ #define ttisfunction(o) checktype(o, LUA_TFUNCTION) -#define ttisclosure(o) ((rawtt(o) & 0x1F) == LUA_TLCL) -#define ttisLclosure(o) checktag((o), ctb(LUA_TLCL)) -#define ttislcf(o) checktag((o), LUA_TLCF) -#define ttisCclosure(o) checktag((o), ctb(LUA_TCCL)) +#define ttisclosure(o) ((rawtt(o) & 0x1F) == LUA_VLCL) +#define ttisLclosure(o) checktag((o), ctb(LUA_VLCL)) +#define ttislcf(o) checktag((o), LUA_VLCF) +#define ttisCclosure(o) checktag((o), ctb(LUA_VCCL)) #define isLfunction(o) ttisLclosure(o) @@ -570,17 +584,17 @@ typedef struct Proto { #define setclLvalue(L,obj,x) \ { TValue *io = (obj); LClosure *x_ = (x); \ - val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TLCL)); \ + val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VLCL)); \ checkliveness(L,io); } #define setclLvalue2s(L,o,cl) setclLvalue(L,s2v(o),cl) #define setfvalue(obj,x) \ - { TValue *io=(obj); val_(io).f=(x); settt_(io, LUA_TLCF); } + { TValue *io=(obj); val_(io).f=(x); settt_(io, LUA_VLCF); } #define setclCvalue(L,obj,x) \ { TValue *io = (obj); CClosure *x_ = (x); \ - val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TCCL)); \ + val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VCCL)); \ checkliveness(L,io); } @@ -636,13 +650,15 @@ typedef union Closure { ** =================================================================== */ -#define ttistable(o) checktag((o), ctb(LUA_TTABLE)) +#define LUA_VTABLE makevariant(LUA_TTABLE, 1) + +#define ttistable(o) checktag((o), ctb(LUA_VTABLE)) #define hvalue(o) check_exp(ttistable(o), gco2t(val_(o).gc)) #define sethvalue(L,obj,x) \ { TValue *io = (obj); Table *x_ = (x); \ - val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_TTABLE)); \ + val_(io).gc = obj2gco(x_); settt_(io, ctb(LUA_VTABLE)); \ checkliveness(L,io); } #define sethvalue2s(L,o,h) sethvalue(L,s2v(o),h) @@ -713,9 +729,9 @@ typedef struct Table { #define keyval(node) ((node)->u.key_val) #define keyisnil(node) (keytt(node) == LUA_TNIL) -#define keyisinteger(node) (keytt(node) == LUA_TNUMINT) +#define keyisinteger(node) (keytt(node) == LUA_VNUMINT) #define keyival(node) (keyval(node).i) -#define keyisshrstr(node) (keytt(node) == ctb(LUA_TSHRSTR)) +#define keyisshrstr(node) (keytt(node) == ctb(LUA_VSHRSTR)) #define keystrval(node) (gco2ts(keyval(node).gc)) #define setnilkey(node) (keytt(node) = LUA_TNIL) diff --git a/lopcodes.h b/lopcodes.h index f512f15af4..8fd52d1887 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -17,11 +17,11 @@ 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 -iABC C(8) | B(8) |k| A(8) | Op(7) | -iABx Bx(17) | A(8) | Op(7) | -iAsBx sBx (signed)(17) | A(8) | Op(7) | -iAx Ax(25) | Op(7) | -isJ sJ(25) | Op(7) | +iABC C(8) | B(8) |k| A(8) | Op(7) | +iABx Bx(17) | A(8) | Op(7) | +iAsBx sBx (signed)(17) | A(8) | Op(7) | +iAx Ax(25) | Op(7) | +isJ sJ(25) | Op(7) | A signed argument is represented in excess K: the represented value is the written unsigned value minus K, where K is half the maximum for the diff --git a/lstate.c b/lstate.c index 4864a97992..7770e314f2 100644 --- a/lstate.c +++ b/lstate.c @@ -318,7 +318,7 @@ LUA_API lua_State *lua_newthread (lua_State *L) { /* create new thread */ L1 = &cast(LX *, luaM_newobject(L, LUA_TTHREAD, sizeof(LX)))->l; L1->marked = luaC_white(g); - L1->tt = LUA_TTHREAD; + L1->tt = LUA_VTHREAD; /* link it on list 'allgc' */ L1->next = g->allgc; g->allgc = obj2gco(L1); @@ -382,7 +382,7 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { if (l == NULL) return NULL; L = &l->l.l; g = &l->g; - L->tt = LUA_TTHREAD; + L->tt = LUA_VTHREAD; g->currentwhite = bitmask(WHITE0BIT); L->marked = luaC_white(g); preinit_thread(L, g); diff --git a/lstate.h b/lstate.h index 638c1e5c0d..df9148eb74 100644 --- a/lstate.h +++ b/lstate.h @@ -327,15 +327,15 @@ union GCUnion { /* macros to convert a GCObject into a specific value */ #define gco2ts(o) \ check_exp(novariant((o)->tt) == LUA_TSTRING, &((cast_u(o))->ts)) -#define gco2u(o) check_exp((o)->tt == LUA_TUSERDATA, &((cast_u(o))->u)) -#define gco2lcl(o) check_exp((o)->tt == LUA_TLCL, &((cast_u(o))->cl.l)) -#define gco2ccl(o) check_exp((o)->tt == LUA_TCCL, &((cast_u(o))->cl.c)) +#define gco2u(o) check_exp((o)->tt == LUA_VUSERDATA, &((cast_u(o))->u)) +#define gco2lcl(o) check_exp((o)->tt == LUA_VLCL, &((cast_u(o))->cl.l)) +#define gco2ccl(o) check_exp((o)->tt == LUA_VCCL, &((cast_u(o))->cl.c)) #define gco2cl(o) \ check_exp(novariant((o)->tt) == LUA_TFUNCTION, &((cast_u(o))->cl)) -#define gco2t(o) check_exp((o)->tt == LUA_TTABLE, &((cast_u(o))->h)) -#define gco2p(o) check_exp((o)->tt == LUA_TPROTO, &((cast_u(o))->p)) -#define gco2th(o) check_exp((o)->tt == LUA_TTHREAD, &((cast_u(o))->th)) -#define gco2upv(o) check_exp((o)->tt == LUA_TUPVAL, &((cast_u(o))->upv)) +#define gco2t(o) check_exp((o)->tt == LUA_VTABLE, &((cast_u(o))->h)) +#define gco2p(o) check_exp((o)->tt == LUA_VPROTO, &((cast_u(o))->p)) +#define gco2th(o) check_exp((o)->tt == LUA_VTHREAD, &((cast_u(o))->th)) +#define gco2upv(o) check_exp((o)->tt == LUA_VUPVAL, &((cast_u(o))->upv)) /* diff --git a/lstring.c b/lstring.c index 6ba798d9eb..a6ffbdd002 100644 --- a/lstring.c +++ b/lstring.c @@ -43,7 +43,7 @@ */ int luaS_eqlngstr (TString *a, TString *b) { size_t len = a->u.lnglen; - lua_assert(a->tt == LUA_TLNGSTR && b->tt == LUA_TLNGSTR); + lua_assert(a->tt == LUA_VLNGSTR && b->tt == LUA_VLNGSTR); return (a == b) || /* same instance or... */ ((len == b->u.lnglen) && /* equal length and ... */ (memcmp(getstr(a), getstr(b), len) == 0)); /* equal contents */ @@ -60,7 +60,7 @@ unsigned int luaS_hash (const char *str, size_t l, unsigned int seed) { unsigned int luaS_hashlongstr (TString *ts) { - lua_assert(ts->tt == LUA_TLNGSTR); + lua_assert(ts->tt == LUA_VLNGSTR); if (ts->extra == 0) { /* no hash? */ ts->hash = luaS_hash(getstr(ts), ts->u.lnglen, ts->hash); ts->extra = 1; /* now it has its hash */ @@ -165,7 +165,7 @@ static TString *createstrobj (lua_State *L, size_t l, int tag, unsigned int h) { TString *luaS_createlngstrobj (lua_State *L, size_t l) { - TString *ts = createstrobj(L, l, LUA_TLNGSTR, G(L)->seed); + TString *ts = createstrobj(L, l, LUA_VLNGSTR, G(L)->seed); ts->u.lnglen = l; return ts; } @@ -215,7 +215,7 @@ static TString *internshrstr (lua_State *L, const char *str, size_t l) { growstrtab(L, tb); list = &tb->hash[lmod(h, tb->size)]; /* rehash with new size */ } - ts = createstrobj(L, l, LUA_TSHRSTR, h); + ts = createstrobj(L, l, LUA_VSHRSTR, h); memcpy(getstr(ts), str, l * sizeof(char)); ts->shrlen = cast_byte(l); ts->u.hnext = *list; @@ -271,7 +271,7 @@ Udata *luaS_newudata (lua_State *L, size_t s, int nuvalue) { GCObject *o; if (unlikely(s > MAX_SIZE - udatamemoffset(nuvalue))) luaM_toobig(L); - o = luaC_newobj(L, LUA_TUSERDATA, sizeudata(nuvalue, s)); + o = luaC_newobj(L, LUA_VUSERDATA, sizeudata(nuvalue, s)); u = gco2u(o); u->len = s; u->nuvalue = nuvalue; diff --git a/lstring.h b/lstring.h index b25502187b..c23d6874f5 100644 --- a/lstring.h +++ b/lstring.h @@ -28,13 +28,13 @@ /* ** test whether a string is a reserved word */ -#define isreserved(s) ((s)->tt == LUA_TSHRSTR && (s)->extra > 0) +#define isreserved(s) ((s)->tt == LUA_VSHRSTR && (s)->extra > 0) /* ** equality for short strings, which are always internalized */ -#define eqshrstr(a,b) check_exp((a)->tt == LUA_TSHRSTR, (a) == (b)) +#define eqshrstr(a,b) check_exp((a)->tt == LUA_VSHRSTR, (a) == (b)) LUAI_FUNC unsigned int luaS_hash (const char *str, size_t l, unsigned int seed); diff --git a/ltable.c b/ltable.c index ebd45dda83..d7eb69a2e1 100644 --- a/ltable.c +++ b/ltable.c @@ -88,8 +88,8 @@ #define dummynode (&dummynode_) static const Node dummynode_ = { - {{NULL}, LUA_TEMPTY, /* value's value and type */ - LUA_TNIL, 0, {NULL}} /* key type, next, and key value */ + {{NULL}, LUA_VEMPTY, /* value's value and type */ + LUA_VNIL, 0, {NULL}} /* key type, next, and key value */ }; @@ -135,21 +135,21 @@ static int l_hashfloat (lua_Number n) { */ static Node *mainposition (const Table *t, int ktt, const Value *kvl) { switch (withvariant(ktt)) { - case LUA_TNUMINT: + case LUA_VNUMINT: return hashint(t, ivalueraw(*kvl)); - case LUA_TNUMFLT: + case LUA_VNUMFLT: return hashmod(t, l_hashfloat(fltvalueraw(*kvl))); - case LUA_TSHRSTR: + case LUA_VSHRSTR: return hashstr(t, tsvalueraw(*kvl)); - case LUA_TLNGSTR: + case LUA_VLNGSTR: return hashpow2(t, luaS_hashlongstr(tsvalueraw(*kvl))); - case LUA_TFALSE: + case LUA_VFALSE: return hashboolean(t, 0); - case LUA_TTRUE: + case LUA_VTRUE: return hashboolean(t, 1); - case LUA_TLIGHTUSERDATA: + case LUA_VLIGHTUSERDATA: return hashpointer(t, pvalueraw(*kvl)); - case LUA_TLCF: + case LUA_VLCF: return hashpointer(t, fvalueraw(*kvl)); default: return hashpointer(t, gcvalueraw(*kvl)); @@ -177,17 +177,17 @@ static int equalkey (const TValue *k1, const Node *n2) { if (rawtt(k1) != keytt(n2)) /* not the same variants? */ return 0; /* cannot be same key */ switch (ttypetag(k1)) { - case LUA_TNIL: case LUA_TFALSE: case LUA_TTRUE: + case LUA_VNIL: case LUA_VFALSE: case LUA_VTRUE: return 1; - case LUA_TNUMINT: + case LUA_VNUMINT: return (ivalue(k1) == keyival(n2)); - case LUA_TNUMFLT: + case LUA_VNUMFLT: return luai_numeq(fltvalue(k1), fltvalueraw(keyval(n2))); - case LUA_TLIGHTUSERDATA: + case LUA_VLIGHTUSERDATA: return pvalue(k1) == pvalueraw(keyval(n2)); - case LUA_TLCF: + case LUA_VLCF: return fvalue(k1) == fvalueraw(keyval(n2)); - case LUA_TLNGSTR: + case LUA_VLNGSTR: return luaS_eqlngstr(tsvalue(k1), keystrval(n2)); default: return gcvalue(k1) == gcvalueraw(keyval(n2)); @@ -580,7 +580,7 @@ static void rehash (lua_State *L, Table *t, const TValue *ek) { Table *luaH_new (lua_State *L) { - GCObject *o = luaC_newobj(L, LUA_TTABLE, sizeof(Table)); + GCObject *o = luaC_newobj(L, LUA_VTABLE, sizeof(Table)); Table *t = gco2t(o); t->metatable = NULL; t->flags = cast_byte(~0); @@ -710,7 +710,7 @@ const TValue *luaH_getint (Table *t, lua_Integer key) { */ const TValue *luaH_getshortstr (Table *t, TString *key) { Node *n = hashstr(t, key); - lua_assert(key->tt == LUA_TSHRSTR); + lua_assert(key->tt == LUA_VSHRSTR); for (;;) { /* check whether 'key' is somewhere in the chain */ if (keyisshrstr(n) && eqshrstr(keystrval(n), key)) return gval(n); /* that's it */ @@ -725,7 +725,7 @@ const TValue *luaH_getshortstr (Table *t, TString *key) { const TValue *luaH_getstr (Table *t, TString *key) { - if (key->tt == LUA_TSHRSTR) + if (key->tt == LUA_VSHRSTR) return luaH_getshortstr(t, key); else { /* for long strings, use generic case */ TValue ko; @@ -740,10 +740,10 @@ const TValue *luaH_getstr (Table *t, TString *key) { */ const TValue *luaH_get (Table *t, const TValue *key) { switch (ttypetag(key)) { - case LUA_TSHRSTR: return luaH_getshortstr(t, tsvalue(key)); - case LUA_TNUMINT: return luaH_getint(t, ivalue(key)); - case LUA_TNIL: return &absentkey; - case LUA_TNUMFLT: { + case LUA_VSHRSTR: return luaH_getshortstr(t, tsvalue(key)); + case LUA_VNUMINT: return luaH_getint(t, ivalue(key)); + case LUA_VNIL: return &absentkey; + case LUA_VNUMFLT: { lua_Integer k; if (luaV_flttointeger(fltvalue(key), &k, F2Ieq)) /* integral index? */ return luaH_getint(t, k); /* use specialized version */ diff --git a/ltests.c b/ltests.c index e9b28b1433..acabc6b67f 100644 --- a/ltests.c +++ b/ltests.c @@ -303,7 +303,7 @@ static void printobj (global_State *g, GCObject *o) { ttypename(novariant(o->tt)), (void *)o, isdead(g,o) ? 'd' : isblack(o) ? 'b' : iswhite(o) ? 'w' : 'g', "ns01oTt"[getage(o)], o->marked); - if (o->tt == LUA_TSHRSTR || o->tt == LUA_TLNGSTR) + if (o->tt == LUA_VSHRSTR || o->tt == LUA_VLNGSTR) printf(" '%s'", getstr(gco2ts(o))); } @@ -435,36 +435,36 @@ static void checkstack (global_State *g, lua_State *L1) { static void checkrefs (global_State *g, GCObject *o) { switch (o->tt) { - case LUA_TUSERDATA: { + case LUA_VUSERDATA: { checkudata(g, gco2u(o)); break; } - case LUA_TUPVAL: { + case LUA_VUPVAL: { checkvalref(g, o, gco2upv(o)->v); break; } - case LUA_TTABLE: { + case LUA_VTABLE: { checktable(g, gco2t(o)); break; } - case LUA_TTHREAD: { + case LUA_VTHREAD: { checkstack(g, gco2th(o)); break; } - case LUA_TLCL: { + case LUA_VLCL: { checkLclosure(g, gco2lcl(o)); break; } - case LUA_TCCL: { + case LUA_VCCL: { checkCclosure(g, gco2ccl(o)); break; } - case LUA_TPROTO: { + case LUA_VPROTO: { checkproto(g, gco2p(o)); break; } - case LUA_TSHRSTR: - case LUA_TLNGSTR: { + case LUA_VSHRSTR: + case LUA_VLNGSTR: { lua_assert(!isgray(o)); /* strings are never gray */ break; } @@ -497,8 +497,8 @@ static void checkobject (global_State *g, GCObject *o, int maybedead, lua_assert(isblack(o) || getage(o) == G_TOUCHED1 || getage(o) == G_OLD0 || - o->tt == LUA_TTHREAD || - (o->tt == LUA_TUPVAL && upisopen(gco2upv(o)))); + o->tt == LUA_VTHREAD || + (o->tt == LUA_VUPVAL && upisopen(gco2upv(o)))); } } checkrefs(g, o); @@ -511,11 +511,11 @@ static void checkgraylist (global_State *g, GCObject *o) { while (o) { lua_assert(isgray(o) || getage(o) == G_TOUCHED2); switch (o->tt) { - case LUA_TTABLE: o = gco2t(o)->gclist; break; - case LUA_TLCL: o = gco2lcl(o)->gclist; break; - case LUA_TCCL: o = gco2ccl(o)->gclist; break; - case LUA_TTHREAD: o = gco2th(o)->gclist; break; - case LUA_TPROTO: o = gco2p(o)->gclist; break; + case LUA_VTABLE: o = gco2t(o)->gclist; break; + case LUA_VLCL: o = gco2lcl(o)->gclist; break; + case LUA_VCCL: o = gco2ccl(o)->gclist; break; + case LUA_VTHREAD: o = gco2th(o)->gclist; break; + case LUA_VPROTO: o = gco2p(o)->gclist; break; default: lua_assert(0); /* other objects cannot be in a gray list */ } } @@ -570,7 +570,7 @@ int lua_checkmemory (lua_State *L) { /* check 'fixedgc' list */ for (o = g->fixedgc; o != NULL; o = o->next) { - lua_assert(o->tt == LUA_TSHRSTR && isgray(o) && getage(o) == G_OLD); + lua_assert(o->tt == LUA_VSHRSTR && isgray(o) && getage(o) == G_OLD); } /* check 'allgc' list */ @@ -584,7 +584,7 @@ int lua_checkmemory (lua_State *L) { for (o = g->tobefnz; o != NULL; o = o->next) { checkobject(g, o, 0, G_NEW); lua_assert(tofinalize(o)); - lua_assert(o->tt == LUA_TUSERDATA || o->tt == LUA_TTABLE); + lua_assert(o->tt == LUA_VUSERDATA || o->tt == LUA_VTABLE); } return 0; } diff --git a/ltests.h b/ltests.h index 8c8de47648..7328aacae6 100644 --- a/ltests.h +++ b/ltests.h @@ -61,7 +61,7 @@ typedef struct Memcontrol { unsigned long maxmem; unsigned long memlimit; unsigned long countlimit; - unsigned long objcount[LUA_NUMTAGS]; + unsigned long objcount[LUA_NUMTYPES]; } Memcontrol; LUA_API Memcontrol l_memcontrol; diff --git a/ltm.c b/ltm.c index ca46f04e43..ae60983f25 100644 --- a/ltm.c +++ b/ltm.c @@ -27,7 +27,7 @@ static const char udatatypename[] = "userdata"; -LUAI_DDEF const char *const luaT_typenames_[LUA_TOTALTAGS] = { +LUAI_DDEF const char *const luaT_typenames_[LUA_TOTALTYPES] = { "no value", "nil", "boolean", udatatypename, "number", "string", "table", "function", udatatypename, "thread", diff --git a/ltm.h b/ltm.h index 9621e68ec0..99b545e766 100644 --- a/ltm.h +++ b/ltm.h @@ -59,7 +59,7 @@ typedef enum { #define ttypename(x) luaT_typenames_[(x) + 1] -LUAI_DDEC(const char *const luaT_typenames_[LUA_TOTALTAGS];) +LUAI_DDEC(const char *const luaT_typenames_[LUA_TOTALTYPES];) LUAI_FUNC const char *luaT_objtypename (lua_State *L, const TValue *o); diff --git a/lua.h b/lua.h index 0ae9e7c9b3..b348c147fc 100644 --- a/lua.h +++ b/lua.h @@ -72,7 +72,7 @@ typedef struct lua_State lua_State; #define LUA_TUSERDATA 7 #define LUA_TTHREAD 8 -#define LUA_NUMTAGS 9 +#define LUA_NUMTYPES 9 @@ -412,6 +412,8 @@ LUA_API void (lua_toclose) (lua_State *L, int idx); #define lua_getuservalue(L,idx) lua_getiuservalue(L,idx,1) #define lua_setuservalue(L,idx) lua_setiuservalue(L,idx,1) +#define LUA_NUMTAGS LUA_NUMTYPES + /* }============================================================== */ /* diff --git a/lundump.c b/lundump.c index 45e0b637d3..1fa322f68f 100644 --- a/lundump.c +++ b/lundump.c @@ -157,23 +157,23 @@ static void LoadConstants (LoadState *S, Proto *f) { TValue *o = &f->k[i]; int t = LoadByte(S); switch (t) { - case LUA_TNIL: + case LUA_VNIL: setnilvalue(o); break; - case LUA_TFALSE: + case LUA_VFALSE: setbfvalue(o); break; - case LUA_TTRUE: + case LUA_VTRUE: setbtvalue(o); break; - case LUA_TNUMFLT: + case LUA_VNUMFLT: setfltvalue(o, LoadNumber(S)); break; - case LUA_TNUMINT: + case LUA_VNUMINT: setivalue(o, LoadInteger(S)); break; - case LUA_TSHRSTR: - case LUA_TLNGSTR: + case LUA_VSHRSTR: + case LUA_VLNGSTR: setsvalue2n(S->L, o, LoadString(S)); break; default: lua_assert(0); diff --git a/lvm.c b/lvm.c index 656def818f..9c1ad47e30 100644 --- a/lvm.c +++ b/lvm.c @@ -577,14 +577,14 @@ int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2) { } /* values have same type and same variant */ switch (ttypetag(t1)) { - case LUA_TNIL: case LUA_TFALSE: case LUA_TTRUE: return 1; - case LUA_TNUMINT: return (ivalue(t1) == ivalue(t2)); - case LUA_TNUMFLT: return luai_numeq(fltvalue(t1), fltvalue(t2)); - case LUA_TLIGHTUSERDATA: return pvalue(t1) == pvalue(t2); - case LUA_TLCF: return fvalue(t1) == fvalue(t2); - case LUA_TSHRSTR: return eqshrstr(tsvalue(t1), tsvalue(t2)); - case LUA_TLNGSTR: return luaS_eqlngstr(tsvalue(t1), tsvalue(t2)); - case LUA_TUSERDATA: { + case LUA_VNIL: case LUA_VFALSE: case LUA_VTRUE: return 1; + case LUA_VNUMINT: return (ivalue(t1) == ivalue(t2)); + case LUA_VNUMFLT: return luai_numeq(fltvalue(t1), fltvalue(t2)); + case LUA_VLIGHTUSERDATA: return pvalue(t1) == pvalue(t2); + case LUA_VLCF: return fvalue(t1) == fvalue(t2); + case LUA_VSHRSTR: return eqshrstr(tsvalue(t1), tsvalue(t2)); + case LUA_VLNGSTR: return luaS_eqlngstr(tsvalue(t1), tsvalue(t2)); + case LUA_VUSERDATA: { if (uvalue(t1) == uvalue(t2)) return 1; else if (L == NULL) return 0; tm = fasttm(L, uvalue(t1)->metatable, TM_EQ); @@ -592,7 +592,7 @@ int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2) { tm = fasttm(L, uvalue(t2)->metatable, TM_EQ); break; /* will try TM */ } - case LUA_TTABLE: { + case LUA_VTABLE: { if (hvalue(t1) == hvalue(t2)) return 1; else if (L == NULL) return 0; tm = fasttm(L, hvalue(t1)->metatable, TM_EQ); @@ -680,18 +680,18 @@ void luaV_concat (lua_State *L, int total) { void luaV_objlen (lua_State *L, StkId ra, const TValue *rb) { const TValue *tm; switch (ttypetag(rb)) { - case LUA_TTABLE: { + case LUA_VTABLE: { Table *h = hvalue(rb); tm = fasttm(L, h->metatable, TM_LEN); if (tm) break; /* metamethod? break switch to call it */ setivalue(s2v(ra), luaH_getn(h)); /* else primitive len */ return; } - case LUA_TSHRSTR: { + case LUA_VSHRSTR: { setivalue(s2v(ra), tsvalue(rb)->shrlen); return; } - case LUA_TLNGSTR: { + case LUA_VLNGSTR: { setivalue(s2v(ra), tsvalue(rb)->u.lnglen); return; } From 28ef7061bbcce39590c97a2ad662e0b60f7adab5 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 31 Jan 2020 13:13:28 -0300 Subject: [PATCH 0540/1145] Tag values don't need to be different from type values Variants can use zero for first variant. --- lobject.h | 37 +++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/lobject.h b/lobject.h index 32542294b6..428145a4a7 100644 --- a/lobject.h +++ b/lobject.h @@ -155,13 +155,13 @@ typedef StackValue *StkId; */ /* Standard nil */ -#define LUA_VNIL makevariant(LUA_TNIL, 1) +#define LUA_VNIL makevariant(LUA_TNIL, 0) /* Empty slot (which might be different from a slot containing nil) */ -#define LUA_VEMPTY makevariant(LUA_TNIL, 2) +#define LUA_VEMPTY makevariant(LUA_TNIL, 1) /* Value returned for a key not found in a table (absent key) */ -#define LUA_VABSTKEY makevariant(LUA_TNIL, 3) +#define LUA_VABSTKEY makevariant(LUA_TNIL, 2) /* macro to test for (any kind of) nil */ @@ -211,8 +211,8 @@ typedef StackValue *StkId; */ -#define LUA_VFALSE makevariant(LUA_TBOOLEAN, 1) -#define LUA_VTRUE makevariant(LUA_TBOOLEAN, 2) +#define LUA_VFALSE makevariant(LUA_TBOOLEAN, 0) +#define LUA_VTRUE makevariant(LUA_TBOOLEAN, 1) #define ttisboolean(o) checktype((o), LUA_TBOOLEAN) #define ttisfalse(o) checktag((o), LUA_VFALSE) @@ -234,7 +234,7 @@ typedef StackValue *StkId; ** =================================================================== */ -#define LUA_VTHREAD makevariant(LUA_TTHREAD, 1) +#define LUA_VTHREAD makevariant(LUA_TTHREAD, 0) #define ttisthread(o) checktag((o), ctb(LUA_VTHREAD)) @@ -295,8 +295,8 @@ typedef struct GCObject { */ /* Variant tags for numbers */ -#define LUA_VNUMINT makevariant(LUA_TNUMBER, 1) /* integer numbers */ -#define LUA_VNUMFLT makevariant(LUA_TNUMBER, 2) /* float numbers */ +#define LUA_VNUMINT makevariant(LUA_TNUMBER, 0) /* integer numbers */ +#define LUA_VNUMFLT makevariant(LUA_TNUMBER, 1) /* float numbers */ #define ttisnumber(o) checktype((o), LUA_TNUMBER) #define ttisfloat(o) checktag((o), LUA_VNUMFLT) @@ -332,8 +332,8 @@ typedef struct GCObject { */ /* Variant tags for strings */ -#define LUA_VSHRSTR makevariant(LUA_TSTRING, 1) /* short strings */ -#define LUA_VLNGSTR makevariant(LUA_TSTRING, 2) /* long strings */ +#define LUA_VSHRSTR makevariant(LUA_TSTRING, 0) /* short strings */ +#define LUA_VLNGSTR makevariant(LUA_TSTRING, 1) /* long strings */ #define ttisstring(o) checktype((o), LUA_TSTRING) #define ttisshrstring(o) checktag((o), ctb(LUA_VSHRSTR)) @@ -403,8 +403,9 @@ typedef struct TString { ** Light userdata should be a variant of userdata, but for compatibility ** reasons they are also different types. */ -#define LUA_VLIGHTUSERDATA makevariant(LUA_TLIGHTUSERDATA, 1) -#define LUA_VUSERDATA makevariant(LUA_TUSERDATA, 1) +#define LUA_VLIGHTUSERDATA makevariant(LUA_TLIGHTUSERDATA, 0) + +#define LUA_VUSERDATA makevariant(LUA_TUSERDATA, 0) #define ttislightuserdata(o) checktag((o), LUA_VLIGHTUSERDATA) #define ttisfulluserdata(o) checktag((o), ctb(LUA_VUSERDATA)) @@ -482,7 +483,7 @@ typedef struct Udata0 { ** =================================================================== */ -#define LUA_VPROTO makevariant(LUA_TPROTO, 1) +#define LUA_VPROTO makevariant(LUA_TPROTO, 0) /* @@ -559,13 +560,13 @@ typedef struct Proto { ** =================================================================== */ -#define LUA_VUPVAL makevariant(LUA_TUPVAL, 1) +#define LUA_VUPVAL makevariant(LUA_TUPVAL, 0) /* Variant tags for functions */ -#define LUA_VLCL makevariant(LUA_TFUNCTION, 1) /* Lua closure */ -#define LUA_VLCF makevariant(LUA_TFUNCTION, 2) /* light C function */ -#define LUA_VCCL makevariant(LUA_TFUNCTION, 3) /* C closure */ +#define LUA_VLCL makevariant(LUA_TFUNCTION, 0) /* Lua closure */ +#define LUA_VLCF makevariant(LUA_TFUNCTION, 1) /* light C function */ +#define LUA_VCCL makevariant(LUA_TFUNCTION, 2) /* C closure */ #define ttisfunction(o) checktype(o, LUA_TFUNCTION) #define ttisclosure(o) ((rawtt(o) & 0x1F) == LUA_VLCL) @@ -650,7 +651,7 @@ typedef union Closure { ** =================================================================== */ -#define LUA_VTABLE makevariant(LUA_TTABLE, 1) +#define LUA_VTABLE makevariant(LUA_TTABLE, 0) #define ttistable(o) checktag((o), ctb(LUA_VTABLE)) From 9b7987a9d1471ba94764286b28e0998f73deb46a Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 11 Feb 2020 11:12:33 -0300 Subject: [PATCH 0541/1145] OP_LOADFALSE broken in two instructions --- lcode.c | 8 ++++---- ljumptab.h | 1 + lopcodes.c | 1 + lopcodes.h | 3 ++- lopnames.h | 1 + lvm.c | 6 +++++- 6 files changed, 14 insertions(+), 6 deletions(-) diff --git a/lcode.c b/lcode.c index 332fdd0084..35e0527f4b 100644 --- a/lcode.c +++ b/lcode.c @@ -872,9 +872,9 @@ static void discharge2anyreg (FuncState *fs, expdesc *e) { } -static int code_loadbool (FuncState *fs, int A, OpCode op, int jump) { +static int code_loadbool (FuncState *fs, int A, OpCode op) { luaK_getlabel(fs); /* those instructions may be jump targets */ - return luaK_codeABC(fs, op, A, jump, 0); + return luaK_codeABC(fs, op, A, 0, 0); } @@ -908,8 +908,8 @@ static void exp2reg (FuncState *fs, expdesc *e, int reg) { int p_t = NO_JUMP; /* position of an eventual LOAD true */ if (need_value(fs, e->t) || need_value(fs, e->f)) { int fj = (e->k == VJMP) ? NO_JUMP : luaK_jump(fs); - p_f = code_loadbool(fs, reg, OP_LOADFALSE, 1); /* skip next inst. */ - p_t = code_loadbool(fs, reg, OP_LOADTRUE, 0); + p_f = code_loadbool(fs, reg, OP_LFALSESKIP); /* skip next inst. */ + p_t = code_loadbool(fs, reg, OP_LOADTRUE); /* jump around these booleans if 'e' is not a test */ luaK_patchtohere(fs, fj); } diff --git a/ljumptab.h b/ljumptab.h index 22e9575f7e..0edd79d5be 100644 --- a/ljumptab.h +++ b/ljumptab.h @@ -31,6 +31,7 @@ static void *disptab[NUM_OPCODES] = { &&L_OP_LOADK, &&L_OP_LOADKX, &&L_OP_LOADFALSE, +&&L_OP_LFALSESKIP, &&L_OP_LOADTRUE, &&L_OP_LOADNIL, &&L_OP_GETUPVAL, diff --git a/lopcodes.c b/lopcodes.c index f5347a3ceb..4e983e0812 100644 --- a/lopcodes.c +++ b/lopcodes.c @@ -25,6 +25,7 @@ LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = { ,opmode(0, 0, 0, 0, 1, iABx) /* OP_LOADK */ ,opmode(0, 0, 0, 0, 1, iABx) /* OP_LOADKX */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_LOADFALSE */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_LFALSESKIP */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_LOADTRUE */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_LOADNIL */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_GETUPVAL */ diff --git a/lopcodes.h b/lopcodes.h index 8fd52d1887..d755870fe4 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -202,7 +202,8 @@ OP_LOADI,/* A sBx R[A] := sBx */ OP_LOADF,/* A sBx R[A] := (lua_Number)sBx */ OP_LOADK,/* A Bx R[A] := K[Bx] */ OP_LOADKX,/* A R[A] := K[extra arg] */ -OP_LOADFALSE,/* A B R[A] := false; if (B) pc++ */ +OP_LOADFALSE,/* A R[A] := false */ +OP_LFALSESKIP,/*A R[A] := false; pc++ */ OP_LOADTRUE,/* A R[A] := true */ OP_LOADNIL,/* A B R[A], R[A+1], ..., R[A+B] := nil */ OP_GETUPVAL,/* A B R[A] := UpValue[B] */ diff --git a/lopnames.h b/lopnames.h index a2097a74fd..f20147e3da 100644 --- a/lopnames.h +++ b/lopnames.h @@ -16,6 +16,7 @@ static const char *const opnames[] = { "LOADK", "LOADKX", "LOADFALSE", + "LFALSESKIP", "LOADTRUE", "LOADNIL", "GETUPVAL", diff --git a/lvm.c b/lvm.c index 9c1ad47e30..d802379c8c 100644 --- a/lvm.c +++ b/lvm.c @@ -1183,7 +1183,11 @@ void luaV_execute (lua_State *L, CallInfo *ci) { } vmcase(OP_LOADFALSE) { setbfvalue(s2v(ra)); - if (GETARG_B(i)) pc++; /* if B, skip next instruction */ + vmbreak; + } + vmcase(OP_LFALSESKIP) { + setbfvalue(s2v(ra)); + pc++; /* skip next instruction */ vmbreak; } vmcase(OP_LOADTRUE) { From 6eb53b752617fae9e1329bfe2cfecdcbb593c398 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 27 Feb 2020 12:59:22 -0300 Subject: [PATCH 0542/1145] Details Several details in code (e.g., moving a variable to the most inner scope that encloses its uses), comments, parameter names, extra tests. --- lauxlib.c | 2 +- lcode.c | 15 +++++---------- lcode.h | 2 +- ldblib.c | 4 ++-- ldebug.c | 6 +++--- lmathlib.c | 28 +++++++++++++++------------- ltests.c | 12 +++++++----- ltests.h | 3 --- lvm.c | 13 +++++++------ testes/db.lua | 5 +++++ testes/events.lua | 1 + testes/math.lua | 4 ++++ testes/nextvar.lua | 2 +- 13 files changed, 52 insertions(+), 45 deletions(-) diff --git a/lauxlib.c b/lauxlib.c index b6740b17ac..723590946b 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -902,10 +902,10 @@ LUALIB_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len) { LUALIB_API void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) { luaL_checkstack(L, nup, "too many upvalues"); for (; l->name != NULL; l++) { /* fill the table with given functions */ - int i; if (l->func == NULL) /* place holder? */ lua_pushboolean(L, 0); else { + int i; for (i = 0; i < nup; i++) /* copy upvalues to the top */ lua_pushvalue(L, -nup); lua_pushcclosure(L, l->func, nup); /* closure with those upvalues */ diff --git a/lcode.c b/lcode.c index 35e0527f4b..83a6d0648d 100644 --- a/lcode.c +++ b/lcode.c @@ -1730,17 +1730,12 @@ void luaK_fixline (FuncState *fs, int line) { } -void luaK_settablesize (FuncState *fs, int pc, int ra, int rc, int rb) { +void luaK_settablesize (FuncState *fs, int pc, int ra, int asize, int hsize) { Instruction *inst = &fs->f->code[pc]; - int extra = 0; - int k = 0; - if (rb != 0) - rb = luaO_ceillog2(rb) + 1; /* hash size */ - if (rc > MAXARG_C) { /* does it need the extra argument? */ - extra = rc / (MAXARG_C + 1); - rc %= (MAXARG_C + 1); - k = 1; - } + int rb = (hsize != 0) ? luaO_ceillog2(hsize) + 1 : 0; /* hash size */ + int extra = asize / (MAXARG_C + 1); /* higher bits of array size */ + int rc = asize % (MAXARG_C + 1); /* lower bits of array size */ + int k = (extra > 0); /* true iff needs extra argument */ *inst = CREATE_ABCk(OP_NEWTABLE, ra, rb, rc, k); *(inst + 1) = CREATE_Ax(OP_EXTRAARG, extra); } diff --git a/lcode.h b/lcode.h index 49c913abb4..3265824452 100644 --- a/lcode.h +++ b/lcode.h @@ -95,7 +95,7 @@ LUAI_FUNC void luaK_infix (FuncState *fs, BinOpr op, expdesc *v); LUAI_FUNC void luaK_posfix (FuncState *fs, BinOpr op, expdesc *v1, expdesc *v2, int line); LUAI_FUNC void luaK_settablesize (FuncState *fs, int pc, - int ra, int rb, int rc); + int ra, int asize, int hsize); LUAI_FUNC void luaK_setlist (FuncState *fs, int base, int nelems, int tostore); LUAI_FUNC void luaK_finish (FuncState *fs); LUAI_FUNC l_noret luaK_semerror (LexState *ls, const char *msg); diff --git a/ldblib.c b/ldblib.c index 77018986b7..745cfd279f 100644 --- a/ldblib.c +++ b/ldblib.c @@ -202,8 +202,6 @@ static int db_getinfo (lua_State *L) { static int db_getlocal (lua_State *L) { int arg; lua_State *L1 = getthread(L, &arg); - lua_Debug ar; - const char *name; int nvar = (int)luaL_checkinteger(L, arg + 2); /* local-variable index */ if (lua_isfunction(L, arg + 1)) { /* function argument? */ lua_pushvalue(L, arg + 1); /* push function */ @@ -211,6 +209,8 @@ static int db_getlocal (lua_State *L) { return 1; /* return only name (there is no value) */ } else { /* stack-level argument */ + lua_Debug ar; + const char *name; int level = (int)luaL_checkinteger(L, arg + 1); if (!lua_getstack(L1, level, &ar)) /* out of range? */ return luaL_argerror(L, arg+1, "level out of range"); diff --git a/ldebug.c b/ldebug.c index a1913c5928..eaac16f734 100644 --- a/ldebug.c +++ b/ldebug.c @@ -101,7 +101,7 @@ int luaG_getfuncline (const Proto *f, int pc) { } -static int currentline (CallInfo *ci) { +static int getcurrentline (CallInfo *ci) { return luaG_getfuncline(ci_func(ci)->p, currentpc(ci)); } @@ -339,7 +339,7 @@ static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar, break; } case 'l': { - ar->currentline = (ci && isLua(ci)) ? currentline(ci) : -1; + ar->currentline = (ci && isLua(ci)) ? getcurrentline(ci) : -1; break; } case 'u': { @@ -775,7 +775,7 @@ l_noret luaG_runerror (lua_State *L, const char *fmt, ...) { msg = luaO_pushvfstring(L, fmt, argp); /* format message */ va_end(argp); if (isLua(ci)) /* if Lua function, add source:line information */ - luaG_addinfo(L, msg, ci_func(ci)->p->source, currentline(ci)); + luaG_addinfo(L, msg, ci_func(ci)->p->source, getcurrentline(ci)); luaG_errormsg(L); } diff --git a/lmathlib.c b/lmathlib.c index 7197fc5987..63f6036aaf 100644 --- a/lmathlib.c +++ b/lmathlib.c @@ -522,16 +522,18 @@ typedef struct { ** Project the random integer 'ran' into the interval [0, n]. ** Because 'ran' has 2^B possible values, the projection can only be ** uniform when the size of the interval is a power of 2 (exact -** division). To get a uniform projection into [0, n], we first compute -** 'lim', the smallest Mersenne number not smaller than 'n'. We then -** project 'ran' into the interval [0, lim]. If the result is inside -** [0, n], we are done. Otherwise, we try with another 'ran', until we -** have a result inside the interval. +** division). Otherwise, to get a uniform projection into [0, n], we +** first compute 'lim', the smallest Mersenne number not smaller than +** 'n'. We then project 'ran' into the interval [0, lim]. If the result +** is inside [0, n], we are done. Otherwise, we try with another 'ran', +** until we have a result inside the interval. */ static lua_Unsigned project (lua_Unsigned ran, lua_Unsigned n, RanState *state) { - lua_Unsigned lim = n; - if ((lim & (lim + 1)) > 0) { /* 'lim + 1' is not a power of 2? */ + if ((n & (n + 1)) == 0) /* is 'n + 1' a power of 2? */ + return ran & n; /* no bias */ + else { + lua_Unsigned lim = n; /* compute the smallest (2^b - 1) not smaller than 'n' */ lim |= (lim >> 1); lim |= (lim >> 2); @@ -541,13 +543,13 @@ static lua_Unsigned project (lua_Unsigned ran, lua_Unsigned n, #if (LUA_MAXUNSIGNED >> 31) >= 3 lim |= (lim >> 32); /* integer type has more than 32 bits */ #endif + lua_assert((lim & (lim + 1)) == 0 /* 'lim + 1' is a power of 2, */ + && lim >= n /* not smaller than 'n', */ + && (lim >> 1) < n); /* and it is the smallest one */ + while ((ran &= lim) > n) /* project 'ran' into [0..lim] */ + ran = I2UInt(nextrand(state->s)); /* not inside [0..n]? try again */ + return ran; } - lua_assert((lim & (lim + 1)) == 0 /* 'lim + 1' is a power of 2, */ - && lim >= n /* not smaller than 'n', */ - && (lim == 0 || (lim >> 1) < n)); /* and it is the smallest one */ - while ((ran &= lim) > n) /* project 'ran' into [0..lim] */ - ran = I2UInt(nextrand(state->s)); /* not inside [0..n]? try again */ - return ran; } diff --git a/ltests.c b/ltests.c index acabc6b67f..76a6ea9b3a 100644 --- a/ltests.c +++ b/ltests.c @@ -419,17 +419,19 @@ static void checkstack (global_State *g, lua_State *L1) { CallInfo *ci; UpVal *uv; lua_assert(!isdead(g, L1)); + if (L1->stack == NULL) { /* incomplete thread? */ + lua_assert(L1->stacksize == 0 && L1->openupval == NULL && + L1->ci == NULL); + return; + } for (uv = L1->openupval; uv != NULL; uv = uv->u.open.next) lua_assert(upisopen(uv)); /* must be open */ for (ci = L1->ci; ci != NULL; ci = ci->previous) { lua_assert(ci->top <= L1->stack_last); lua_assert(lua_checkpc(ci)); } - if (L1->stack) { /* complete thread? */ - for (o = L1->stack; o < L1->stack_last + EXTRA_STACK; o++) - checkliveness(L1, s2v(o)); /* entire stack must have valid values */ - } - else lua_assert(L1->stacksize == 0); + for (o = L1->stack; o < L1->stack_last + EXTRA_STACK; o++) + checkliveness(L1, s2v(o)); /* entire stack must have valid values */ } diff --git a/ltests.h b/ltests.h index 7328aacae6..db0a2a0d27 100644 --- a/ltests.h +++ b/ltests.h @@ -25,9 +25,6 @@ #define lua_assert(c) assert(c) -/* include opcode names */ -#define LUAI_DEFOPNAMES - /* compiled with -O0, Lua uses a lot of C stack space... */ #undef LUAI_MAXCSTACK diff --git a/lvm.c b/lvm.c index d802379c8c..e7781dbf25 100644 --- a/lvm.c +++ b/lvm.c @@ -980,11 +980,11 @@ void luaV_finishOp (lua_State *L) { /* -** Order operations with register operands. 'opf' actually works +** Order operations with register operands. 'opn' actually works ** for all numbers, but the fast track improves performance for ** integers. */ -#define op_order(L,opi,opf,other) { \ +#define op_order(L,opi,opn,other) { \ int cond; \ TValue *rb = vRB(i); \ if (ttisinteger(s2v(ra)) && ttisinteger(rb)) { \ @@ -993,7 +993,7 @@ void luaV_finishOp (lua_State *L) { cond = opi(ia, ib); \ } \ else if (ttisnumber(s2v(ra)) && ttisnumber(rb)) \ - cond = opf(s2v(ra), rb); \ + cond = opn(s2v(ra), rb); \ else \ Protect(cond = other(L, s2v(ra), rb)); \ docondjump(); } @@ -1323,8 +1323,9 @@ void luaV_execute (lua_State *L, CallInfo *ci) { Table *t; if (b > 0) b = 1 << (b - 1); /* size is 2^(b - 1) */ - if (TESTARG_k(i)) - c += GETARG_Ax(*pc) * (MAXARG_C + 1); + lua_assert((!TESTARG_k(i)) == (GETARG_Ax(*pc) == 0)); + if (TESTARG_k(i)) /* non-zero extra argument? */ + c += GETARG_Ax(*pc) * (MAXARG_C + 1); /* add it to size */ pc++; /* skip extra argument */ L->top = ra + 1; /* correct top in case of emergency GC */ t = luaH_new(L); /* memory allocation */ @@ -1558,7 +1559,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmcase(OP_EQK) { TValue *rb = KB(i); /* basic types do not use '__eq'; we can use raw equality */ - int cond = luaV_equalobj(NULL, s2v(ra), rb); + int cond = luaV_rawequalobj(s2v(ra), rb); docondjump(); vmbreak; } diff --git a/testes/db.lua b/testes/db.lua index 074a6d0be5..941283f7a9 100644 --- a/testes/db.lua +++ b/testes/db.lua @@ -649,6 +649,11 @@ t = debug.getinfo(1) -- main assert(t.isvararg == true and t.nparams == 0 and t.nups == 1 and debug.getupvalue(t.func, 1) == "_ENV") +t = debug.getinfo(math.sin) -- C function +assert(t.isvararg == true and t.nparams == 0 and t.nups == 0) + +t = debug.getinfo(string.gmatch("abc", "a")) -- C closure +assert(t.isvararg == true and t.nparams == 0 and t.nups > 0) diff --git a/testes/events.lua b/testes/events.lua index d0abe1d428..8a01330e1b 100644 --- a/testes/events.lua +++ b/testes/events.lua @@ -325,6 +325,7 @@ else assert(u1 == u3 and u3 == u1 and u1 ~= u2) assert(u2 == u1 and u2 == u3 and u3 == u2) assert(u2 ~= {}) -- different types cannot be equal + assert(rawequal(u1, u1) and not rawequal(u1, u3)) local mirror = {} debug.setmetatable(u3, {__index = mirror, __newindex = mirror}) diff --git a/testes/math.lua b/testes/math.lua index 7248787b48..930221e362 100644 --- a/testes/math.lua +++ b/testes/math.lua @@ -960,7 +960,10 @@ do aux(-10,0) aux(1, 6) aux(1, 2) + aux(1, 13) + aux(1, 31) aux(1, 32) + aux(1, 33) aux(-10, 10) aux(-10,-10) -- unit set aux(minint, minint) -- unit set @@ -998,6 +1001,7 @@ do end aux(0, maxint) aux(1, maxint) + aux(3, maxint // 3) aux(minint, -1) aux(minint // 2, maxint // 2) aux(minint, maxint) diff --git a/testes/nextvar.lua b/testes/nextvar.lua index 9d91963135..73af77dde6 100644 --- a/testes/nextvar.lua +++ b/testes/nextvar.lua @@ -76,7 +76,7 @@ end -- testing constructor sizes local sizes = {0, 1, 2, 3, 4, 5, 7, 8, 9, 15, 16, 17, - 30, 31, 32, 33, 34, 500, 1000} + 30, 31, 32, 33, 34, 254, 255, 256, 500, 1000} for _, sa in ipairs(sizes) do -- 'sa' is size of the array part local arr = {"return {"} From e8a52281d9cba1c8dd49a06e7523d7e39794ecc1 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 27 Feb 2020 15:17:44 -0300 Subject: [PATCH 0543/1145] Code style in 'ldump'/'lundump'. - function names start with lower case; - state is always the first parameter. --- ldump.c | 152 +++++++++++++++++++++++++++--------------------------- lundump.c | 146 +++++++++++++++++++++++++-------------------------- 2 files changed, 149 insertions(+), 149 deletions(-) diff --git a/ldump.c b/ldump.c index 4d29b94e11..fbadbcc99e 100644 --- a/ldump.c +++ b/ldump.c @@ -29,15 +29,15 @@ typedef struct { /* -** All high-level dumps go through DumpVector; you can change it to +** All high-level dumps go through dumpVector; you can change it to ** change the endianness of the result */ -#define DumpVector(v,n,D) DumpBlock(v,(n)*sizeof((v)[0]),D) +#define dumpVector(D,v,n) dumpBlock(D,v,(n)*sizeof((v)[0])) -#define DumpLiteral(s,D) DumpBlock(s, sizeof(s) - sizeof(char), D) +#define dumpLiteral(D, s) dumpBlock(D,s,sizeof(s) - sizeof(char)) -static void DumpBlock (const void *b, size_t size, DumpState *D) { +static void dumpBlock (DumpState *D, const void *b, size_t size) { if (D->status == 0 && size > 0) { lua_unlock(D->L); D->status = (*D->writer)(D->L, b, size, D->data); @@ -46,19 +46,19 @@ static void DumpBlock (const void *b, size_t size, DumpState *D) { } -#define DumpVar(x,D) DumpVector(&x,1,D) +#define dumpVar(D,x) dumpVector(D,&x,1) -static void DumpByte (int y, DumpState *D) { +static void dumpByte (DumpState *D, int y) { lu_byte x = (lu_byte)y; - DumpVar(x, D); + dumpVar(D, x); } -/* DumpInt Buff Size */ +/* dumpInt Buff Size */ #define DIBS ((sizeof(size_t) * 8 / 7) + 1) -static void DumpSize (size_t x, DumpState *D) { +static void dumpSize (DumpState *D, size_t x) { lu_byte buff[DIBS]; int n = 0; do { @@ -66,63 +66,63 @@ static void DumpSize (size_t x, DumpState *D) { x >>= 7; } while (x != 0); buff[DIBS - 1] |= 0x80; /* mark last byte */ - DumpVector(buff + DIBS - n, n, D); + dumpVector(D, buff + DIBS - n, n); } -static void DumpInt (int x, DumpState *D) { - DumpSize(x, D); +static void dumpInt (DumpState *D, int x) { + dumpSize(D, x); } -static void DumpNumber (lua_Number x, DumpState *D) { - DumpVar(x, D); +static void dumpNumber (DumpState *D, lua_Number x) { + dumpVar(D, x); } -static void DumpInteger (lua_Integer x, DumpState *D) { - DumpVar(x, D); +static void dumpInteger (DumpState *D, lua_Integer x) { + dumpVar(D, x); } -static void DumpString (const TString *s, DumpState *D) { +static void dumpString (DumpState *D, const TString *s) { if (s == NULL) - DumpSize(0, D); + dumpSize(D, 0); else { size_t size = tsslen(s); const char *str = getstr(s); - DumpSize(size + 1, D); - DumpVector(str, size, D); + dumpSize(D, size + 1); + dumpVector(D, str, size); } } -static void DumpCode (const Proto *f, DumpState *D) { - DumpInt(f->sizecode, D); - DumpVector(f->code, f->sizecode, D); +static void dumpCode (DumpState *D, const Proto *f) { + dumpInt(D, f->sizecode); + dumpVector(D, f->code, f->sizecode); } -static void DumpFunction(const Proto *f, TString *psource, DumpState *D); +static void dumpFunction(DumpState *D, const Proto *f, TString *psource); -static void DumpConstants (const Proto *f, DumpState *D) { +static void dumpConstants (DumpState *D, const Proto *f) { int i; int n = f->sizek; - DumpInt(n, D); + dumpInt(D, n); for (i = 0; i < n; i++) { const TValue *o = &f->k[i]; int tt = ttypetag(o); - DumpByte(tt, D); + dumpByte(D, tt); switch (tt) { case LUA_VNUMFLT: - DumpNumber(fltvalue(o), D); + dumpNumber(D, fltvalue(o)); break; case LUA_VNUMINT: - DumpInteger(ivalue(o), D); + dumpInteger(D, ivalue(o)); break; case LUA_VSHRSTR: case LUA_VLNGSTR: - DumpString(tsvalue(o), D); + dumpString(D, tsvalue(o)); break; default: lua_assert(tt == LUA_VNIL || tt == LUA_VFALSE || tt == LUA_VTRUE); @@ -131,79 +131,79 @@ static void DumpConstants (const Proto *f, DumpState *D) { } -static void DumpProtos (const Proto *f, DumpState *D) { +static void dumpProtos (DumpState *D, const Proto *f) { int i; int n = f->sizep; - DumpInt(n, D); + dumpInt(D, n); for (i = 0; i < n; i++) - DumpFunction(f->p[i], f->source, D); + dumpFunction(D, f->p[i], f->source); } -static void DumpUpvalues (const Proto *f, DumpState *D) { +static void dumpUpvalues (DumpState *D, const Proto *f) { int i, n = f->sizeupvalues; - DumpInt(n, D); + dumpInt(D, n); for (i = 0; i < n; i++) { - DumpByte(f->upvalues[i].instack, D); - DumpByte(f->upvalues[i].idx, D); - DumpByte(f->upvalues[i].kind, D); + dumpByte(D, f->upvalues[i].instack); + dumpByte(D, f->upvalues[i].idx); + dumpByte(D, f->upvalues[i].kind); } } -static void DumpDebug (const Proto *f, DumpState *D) { +static void dumpDebug (DumpState *D, const Proto *f) { int i, n; n = (D->strip) ? 0 : f->sizelineinfo; - DumpInt(n, D); - DumpVector(f->lineinfo, n, D); + dumpInt(D, n); + dumpVector(D, f->lineinfo, n); n = (D->strip) ? 0 : f->sizeabslineinfo; - DumpInt(n, D); + dumpInt(D, n); for (i = 0; i < n; i++) { - DumpInt(f->abslineinfo[i].pc, D); - DumpInt(f->abslineinfo[i].line, D); + dumpInt(D, f->abslineinfo[i].pc); + dumpInt(D, f->abslineinfo[i].line); } n = (D->strip) ? 0 : f->sizelocvars; - DumpInt(n, D); + dumpInt(D, n); for (i = 0; i < n; i++) { - DumpString(f->locvars[i].varname, D); - DumpInt(f->locvars[i].startpc, D); - DumpInt(f->locvars[i].endpc, D); + dumpString(D, f->locvars[i].varname); + dumpInt(D, f->locvars[i].startpc); + dumpInt(D, f->locvars[i].endpc); } n = (D->strip) ? 0 : f->sizeupvalues; - DumpInt(n, D); + dumpInt(D, n); for (i = 0; i < n; i++) - DumpString(f->upvalues[i].name, D); + dumpString(D, f->upvalues[i].name); } -static void DumpFunction (const Proto *f, TString *psource, DumpState *D) { +static void dumpFunction (DumpState *D, const Proto *f, TString *psource) { if (D->strip || f->source == psource) - DumpString(NULL, D); /* no debug info or same source as its parent */ + dumpString(D, NULL); /* no debug info or same source as its parent */ else - DumpString(f->source, D); - DumpInt(f->linedefined, D); - DumpInt(f->lastlinedefined, D); - DumpByte(f->numparams, D); - DumpByte(f->is_vararg, D); - DumpByte(f->maxstacksize, D); - DumpCode(f, D); - DumpConstants(f, D); - DumpUpvalues(f, D); - DumpProtos(f, D); - DumpDebug(f, D); + dumpString(D, f->source); + dumpInt(D, f->linedefined); + dumpInt(D, f->lastlinedefined); + dumpByte(D, f->numparams); + dumpByte(D, f->is_vararg); + dumpByte(D, f->maxstacksize); + dumpCode(D, f); + dumpConstants(D, f); + dumpUpvalues(D, f); + dumpProtos(D, f); + dumpDebug(D, f); } -static void DumpHeader (DumpState *D) { - DumpLiteral(LUA_SIGNATURE, D); - DumpInt(LUAC_VERSION, D); - DumpByte(LUAC_FORMAT, D); - DumpLiteral(LUAC_DATA, D); - DumpByte(sizeof(Instruction), D); - DumpByte(sizeof(lua_Integer), D); - DumpByte(sizeof(lua_Number), D); - DumpInteger(LUAC_INT, D); - DumpNumber(LUAC_NUM, D); +static void dumpHeader (DumpState *D) { + dumpLiteral(D, LUA_SIGNATURE); + dumpInt(D, LUAC_VERSION); + dumpByte(D, LUAC_FORMAT); + dumpLiteral(D, LUAC_DATA); + dumpByte(D, sizeof(Instruction)); + dumpByte(D, sizeof(lua_Integer)); + dumpByte(D, sizeof(lua_Number)); + dumpInteger(D, LUAC_INT); + dumpNumber(D, LUAC_NUM); } @@ -218,9 +218,9 @@ int luaU_dump(lua_State *L, const Proto *f, lua_Writer w, void *data, D.data = data; D.strip = strip; D.status = 0; - DumpHeader(&D); - DumpByte(f->sizeupvalues, &D); - DumpFunction(f, NULL, &D); + dumpHeader(&D); + dumpByte(&D, f->sizeupvalues); + dumpFunction(&D, f, NULL); return D.status; } diff --git a/lundump.c b/lundump.c index 1fa322f68f..17364999ba 100644 --- a/lundump.c +++ b/lundump.c @@ -44,21 +44,21 @@ static l_noret error (LoadState *S, const char *why) { /* -** All high-level loads go through LoadVector; you can change it to +** All high-level loads go through loadVector; you can change it to ** adapt to the endianness of the input */ -#define LoadVector(S,b,n) LoadBlock(S,b,(n)*sizeof((b)[0])) +#define loadVector(S,b,n) loadBlock(S,b,(n)*sizeof((b)[0])) -static void LoadBlock (LoadState *S, void *b, size_t size) { +static void loadBlock (LoadState *S, void *b, size_t size) { if (luaZ_read(S->Z, b, size) != 0) error(S, "truncated chunk"); } -#define LoadVar(S,x) LoadVector(S,&x,1) +#define loadVar(S,x) loadVector(S,&x,1) -static lu_byte LoadByte (LoadState *S) { +static lu_byte loadByte (LoadState *S) { int b = zgetc(S->Z); if (b == EOZ) error(S, "truncated chunk"); @@ -66,12 +66,12 @@ static lu_byte LoadByte (LoadState *S) { } -static size_t LoadUnsigned (LoadState *S, size_t limit) { +static size_t loadUnsigned (LoadState *S, size_t limit) { size_t x = 0; int b; limit >>= 7; do { - b = LoadByte(S); + b = loadByte(S); if (x >= limit) error(S, "integer overflow"); x = (x << 7) | (b & 0x7f); @@ -80,45 +80,45 @@ static size_t LoadUnsigned (LoadState *S, size_t limit) { } -static size_t LoadSize (LoadState *S) { - return LoadUnsigned(S, ~(size_t)0); +static size_t loadSize (LoadState *S) { + return loadUnsigned(S, ~(size_t)0); } -static int LoadInt (LoadState *S) { - return cast_int(LoadUnsigned(S, INT_MAX)); +static int loadInt (LoadState *S) { + return cast_int(loadUnsigned(S, INT_MAX)); } -static lua_Number LoadNumber (LoadState *S) { +static lua_Number loadNumber (LoadState *S) { lua_Number x; - LoadVar(S, x); + loadVar(S, x); return x; } -static lua_Integer LoadInteger (LoadState *S) { +static lua_Integer loadInteger (LoadState *S) { lua_Integer x; - LoadVar(S, x); + loadVar(S, x); return x; } /* -** Load a nullable string +** Load a nullable string. */ -static TString *LoadStringN (LoadState *S) { - size_t size = LoadSize(S); +static TString *loadStringN (LoadState *S) { + size_t size = loadSize(S); if (size == 0) return NULL; else if (--size <= LUAI_MAXSHORTLEN) { /* short string? */ char buff[LUAI_MAXSHORTLEN]; - LoadVector(S, buff, size); + loadVector(S, buff, size); return luaS_newlstr(S->L, buff, size); } else { /* long string */ TString *ts = luaS_createlngstrobj(S->L, size); - LoadVector(S, getstr(ts), size); /* load directly in final place */ + loadVector(S, getstr(ts), size); /* load directly in final place */ return ts; } } @@ -127,35 +127,35 @@ static TString *LoadStringN (LoadState *S) { /* ** Load a non-nullable string. */ -static TString *LoadString (LoadState *S) { - TString *st = LoadStringN(S); +static TString *loadString (LoadState *S) { + TString *st = loadStringN(S); if (st == NULL) error(S, "bad format for constant string"); return st; } -static void LoadCode (LoadState *S, Proto *f) { - int n = LoadInt(S); +static void loadCode (LoadState *S, Proto *f) { + int n = loadInt(S); f->code = luaM_newvectorchecked(S->L, n, Instruction); f->sizecode = n; - LoadVector(S, f->code, n); + loadVector(S, f->code, n); } -static void LoadFunction(LoadState *S, Proto *f, TString *psource); +static void loadFunction(LoadState *S, Proto *f, TString *psource); -static void LoadConstants (LoadState *S, Proto *f) { +static void loadConstants (LoadState *S, Proto *f) { int i; - int n = LoadInt(S); + int n = loadInt(S); f->k = luaM_newvectorchecked(S->L, n, TValue); f->sizek = n; for (i = 0; i < n; i++) setnilvalue(&f->k[i]); for (i = 0; i < n; i++) { TValue *o = &f->k[i]; - int t = LoadByte(S); + int t = loadByte(S); switch (t) { case LUA_VNIL: setnilvalue(o); @@ -167,14 +167,14 @@ static void LoadConstants (LoadState *S, Proto *f) { setbtvalue(o); break; case LUA_VNUMFLT: - setfltvalue(o, LoadNumber(S)); + setfltvalue(o, loadNumber(S)); break; case LUA_VNUMINT: - setivalue(o, LoadInteger(S)); + setivalue(o, loadInteger(S)); break; case LUA_VSHRSTR: case LUA_VLNGSTR: - setsvalue2n(S->L, o, LoadString(S)); + setsvalue2n(S->L, o, loadString(S)); break; default: lua_assert(0); } @@ -182,91 +182,91 @@ static void LoadConstants (LoadState *S, Proto *f) { } -static void LoadProtos (LoadState *S, Proto *f) { +static void loadProtos (LoadState *S, Proto *f) { int i; - int n = LoadInt(S); + int n = loadInt(S); f->p = luaM_newvectorchecked(S->L, n, Proto *); f->sizep = n; for (i = 0; i < n; i++) f->p[i] = NULL; for (i = 0; i < n; i++) { f->p[i] = luaF_newproto(S->L); - LoadFunction(S, f->p[i], f->source); + loadFunction(S, f->p[i], f->source); } } -static void LoadUpvalues (LoadState *S, Proto *f) { +static void loadUpvalues (LoadState *S, Proto *f) { int i, n; - n = LoadInt(S); + n = loadInt(S); f->upvalues = luaM_newvectorchecked(S->L, n, Upvaldesc); f->sizeupvalues = n; for (i = 0; i < n; i++) { f->upvalues[i].name = NULL; - f->upvalues[i].instack = LoadByte(S); - f->upvalues[i].idx = LoadByte(S); - f->upvalues[i].kind = LoadByte(S); + f->upvalues[i].instack = loadByte(S); + f->upvalues[i].idx = loadByte(S); + f->upvalues[i].kind = loadByte(S); } } -static void LoadDebug (LoadState *S, Proto *f) { +static void loadDebug (LoadState *S, Proto *f) { int i, n; - n = LoadInt(S); + n = loadInt(S); f->lineinfo = luaM_newvectorchecked(S->L, n, ls_byte); f->sizelineinfo = n; - LoadVector(S, f->lineinfo, n); - n = LoadInt(S); + loadVector(S, f->lineinfo, n); + n = loadInt(S); f->abslineinfo = luaM_newvectorchecked(S->L, n, AbsLineInfo); f->sizeabslineinfo = n; for (i = 0; i < n; i++) { - f->abslineinfo[i].pc = LoadInt(S); - f->abslineinfo[i].line = LoadInt(S); + f->abslineinfo[i].pc = loadInt(S); + f->abslineinfo[i].line = loadInt(S); } - n = LoadInt(S); + n = loadInt(S); f->locvars = luaM_newvectorchecked(S->L, n, LocVar); f->sizelocvars = n; for (i = 0; i < n; i++) f->locvars[i].varname = NULL; for (i = 0; i < n; i++) { - f->locvars[i].varname = LoadStringN(S); - f->locvars[i].startpc = LoadInt(S); - f->locvars[i].endpc = LoadInt(S); + f->locvars[i].varname = loadStringN(S); + f->locvars[i].startpc = loadInt(S); + f->locvars[i].endpc = loadInt(S); } - n = LoadInt(S); + n = loadInt(S); for (i = 0; i < n; i++) - f->upvalues[i].name = LoadStringN(S); + f->upvalues[i].name = loadStringN(S); } -static void LoadFunction (LoadState *S, Proto *f, TString *psource) { - f->source = LoadStringN(S); +static void loadFunction (LoadState *S, Proto *f, TString *psource) { + f->source = loadStringN(S); if (f->source == NULL) /* no source in dump? */ f->source = psource; /* reuse parent's source */ - f->linedefined = LoadInt(S); - f->lastlinedefined = LoadInt(S); - f->numparams = LoadByte(S); - f->is_vararg = LoadByte(S); - f->maxstacksize = LoadByte(S); - LoadCode(S, f); - LoadConstants(S, f); - LoadUpvalues(S, f); - LoadProtos(S, f); - LoadDebug(S, f); + f->linedefined = loadInt(S); + f->lastlinedefined = loadInt(S); + f->numparams = loadByte(S); + f->is_vararg = loadByte(S); + f->maxstacksize = loadByte(S); + loadCode(S, f); + loadConstants(S, f); + loadUpvalues(S, f); + loadProtos(S, f); + loadDebug(S, f); } static void checkliteral (LoadState *S, const char *s, const char *msg) { char buff[sizeof(LUA_SIGNATURE) + sizeof(LUAC_DATA)]; /* larger than both */ size_t len = strlen(s); - LoadVector(S, buff, len); + loadVector(S, buff, len); if (memcmp(s, buff, len) != 0) error(S, msg); } static void fchecksize (LoadState *S, size_t size, const char *tname) { - if (LoadByte(S) != size) + if (loadByte(S) != size) error(S, luaO_pushfstring(S->L, "%s size mismatch", tname)); } @@ -276,23 +276,23 @@ static void fchecksize (LoadState *S, size_t size, const char *tname) { static void checkHeader (LoadState *S) { /* skip 1st char (already read and checked) */ checkliteral(S, &LUA_SIGNATURE[1], "not a binary chunk"); - if (LoadInt(S) != LUAC_VERSION) + if (loadInt(S) != LUAC_VERSION) error(S, "version mismatch"); - if (LoadByte(S) != LUAC_FORMAT) + if (loadByte(S) != LUAC_FORMAT) error(S, "format mismatch"); checkliteral(S, LUAC_DATA, "corrupted chunk"); checksize(S, Instruction); checksize(S, lua_Integer); checksize(S, lua_Number); - if (LoadInteger(S) != LUAC_INT) + if (loadInteger(S) != LUAC_INT) error(S, "integer format mismatch"); - if (LoadNumber(S) != LUAC_NUM) + if (loadNumber(S) != LUAC_NUM) error(S, "float format mismatch"); } /* -** load precompiled chunk +** Load precompiled chunk. */ LClosure *luaU_undump(lua_State *L, ZIO *Z, const char *name) { LoadState S; @@ -306,11 +306,11 @@ LClosure *luaU_undump(lua_State *L, ZIO *Z, const char *name) { S.L = L; S.Z = Z; checkHeader(&S); - cl = luaF_newLclosure(L, LoadByte(&S)); + cl = luaF_newLclosure(L, loadByte(&S)); setclLvalue2s(L, L->top, cl); luaD_inctop(L); cl->p = luaF_newproto(L); - LoadFunction(&S, cl->p, NULL); + loadFunction(&S, cl->p, NULL); lua_assert(cl->nupvalues == cl->p->sizeupvalues); luai_verifycode(L, buff, cl->p); return cl; From 92594f09395800f6f085ca7501ffd1f7aef25e22 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 28 Feb 2020 10:37:14 -0300 Subject: [PATCH 0544/1145] Corrected direct use of 'snprintf' in 'lstrlib.c' --- lstrlib.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lstrlib.c b/lstrlib.c index e47a1d8d97..28df2d4574 100644 --- a/lstrlib.c +++ b/lstrlib.c @@ -1266,7 +1266,7 @@ static int str_format (lua_State *L) { case 'e': case 'E': case 'g': case 'G': { lua_Number n = luaL_checknumber(L, arg); addlenmod(form, LUA_NUMBER_FRMLEN); - nb = snprintf(buff, maxitem, form, (LUAI_UACNUMBER)n); + nb = l_sprintf(buff, maxitem, form, (LUAI_UACNUMBER)n); break; } case 'p': { From e4607523234f16ed9ed0436340b9315377dbfe7f Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 2 Mar 2020 13:24:06 -0300 Subject: [PATCH 0545/1145] Fixed "conceptual" bug in 'luaK_setreturns' This function was computing invalid instruction addresses when the expression was not a multi-return instruction. (Virtually all machines don't raise errors when computing an invalid address, as long as the address is not accessed, but this computation is undefined behavior in ISO C.) --- lcode.c | 7 +++---- lparser.c | 3 ++- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lcode.c b/lcode.c index 83a6d0648d..6f241c9476 100644 --- a/lcode.c +++ b/lcode.c @@ -703,19 +703,18 @@ static void const2exp (TValue *v, expdesc *e) { /* ** Fix an expression to return the number of results 'nresults'. -** Either 'e' is a multi-ret expression (function call or vararg) -** or 'nresults' is LUA_MULTRET (as any expression can satisfy that). +** 'e' must be a multi-ret expression (function call or vararg). */ void luaK_setreturns (FuncState *fs, expdesc *e, int nresults) { Instruction *pc = &getinstruction(fs, e); if (e->k == VCALL) /* expression is an open function call? */ SETARG_C(*pc, nresults + 1); - else if (e->k == VVARARG) { + else { + lua_assert(e->k == VVARARG); SETARG_C(*pc, nresults + 1); SETARG_A(*pc, fs->freereg); luaK_reserveregs(fs, 1); } - else lua_assert(nresults == LUA_MULTRET); } diff --git a/lparser.c b/lparser.c index 8c81203901..b0dbb65c6f 100644 --- a/lparser.c +++ b/lparser.c @@ -1014,7 +1014,8 @@ static void funcargs (LexState *ls, expdesc *f, int line) { args.k = VVOID; else { explist(ls, &args); - luaK_setmultret(fs, &args); + if (hasmultret(args.k)) + luaK_setmultret(fs, &args); } check_match(ls, ')', '(', line); break; From 513559cc4760392b6fa33754c516683ef49dba22 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 16 Mar 2020 14:13:13 -0300 Subject: [PATCH 0546/1145] Fixed bug in 'string.format("%p")' The string "(null)" used for non-collectable values must be printed as a string, not as a pointer. (Bug introduced in commit e0cbaa50fa7). --- lstrlib.c | 6 ++++-- ltests.c | 3 +-- testes/strings.lua | 18 ++++++++++++++---- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/lstrlib.c b/lstrlib.c index 28df2d4574..2ba8bde47c 100644 --- a/lstrlib.c +++ b/lstrlib.c @@ -1271,8 +1271,10 @@ static int str_format (lua_State *L) { } case 'p': { const void *p = lua_topointer(L, arg); - if (p == NULL) - p = "(null)"; /* NULL not a valid parameter in ISO C 'printf' */ + if (p == NULL) { /* avoid calling 'printf' with argument NULL */ + p = "(null)"; /* result */ + form[strlen(form) - 1] = 's'; /* format it as a string */ + } nb = l_sprintf(buff, maxitem, form, p); break; } diff --git a/ltests.c b/ltests.c index 76a6ea9b3a..7e6d86104f 100644 --- a/ltests.c +++ b/ltests.c @@ -131,8 +131,7 @@ static void warnf (void *ud, const char *msg, int tocont) { if (buff[0] != '#' && onoff) /* unexpected warning? */ badexit("Unexpected warning in test mode: %s\naborting...\n", buff, NULL); - /* else */ /* FALLTHROUGH */ - } + } /* FALLTHROUGH */ case 1: { /* allow */ if (onoff) fprintf(stderr, "Lua warning: %s\n", buff); /* print warning */ diff --git a/testes/strings.lua b/testes/strings.lua index 2ce3ebc338..4a10857e75 100644 --- a/testes/strings.lua +++ b/testes/strings.lua @@ -158,28 +158,38 @@ do -- tests for '%p' format -- not much to test, as C does not specify what '%p' does. -- ("The value of the pointer is converted to a sequence of printing -- characters, in an implementation-defined manner.") - local null = string.format("%p", nil) - assert(string.format("%p", {}) ~= null) + local null = "(null)" -- nulls are formatted by Lua assert(string.format("%p", 4) == null) assert(string.format("%p", true) == null) + assert(string.format("%p", nil) == null) + assert(string.format("%p", {}) ~= null) assert(string.format("%p", print) ~= null) assert(string.format("%p", coroutine.running()) ~= null) assert(string.format("%p", io.stdin) ~= null) assert(string.format("%p", io.stdin) == string.format("%p", io.stdin)) + assert(string.format("%p", print) == string.format("%p", print)) + assert(string.format("%p", print) ~= string.format("%p", assert)) + + assert(#string.format("%90p", {}) == 90) + assert(#string.format("%-60p", {}) == 60) + assert(string.format("%10p", false) == string.rep(" ", 10 - #null) .. null) + assert(string.format("%-12p", 1.5) == null .. string.rep(" ", 12 - #null)) + do local t1 = {}; local t2 = {} assert(string.format("%p", t1) ~= string.format("%p", t2)) end + do -- short strings are internalized local s1 = string.rep("a", 10) - local s2 = string.rep("a", 10) + local s2 = string.rep("aa", 5) assert(string.format("%p", s1) == string.format("%p", s2)) end + do -- long strings aren't internalized local s1 = string.rep("a", 300); local s2 = string.rep("a", 300) assert(string.format("%p", s1) ~= string.format("%p", s2)) end - assert(#string.format("%90p", {}) == 90) end x = '"lo"\n\\' From 7288528a1e081d101a1bc19346a974088b6b8315 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 1 Apr 2020 10:52:41 -0300 Subject: [PATCH 0547/1145] Short strings always use all bytes in the hash Collisions in short strings occurr just by their existence, when internalizing them. (Collisions in long strings is caused/controlled by the program, when adding them as keys to the same table.) --- lstate.c | 2 +- lstring.c | 12 +++++++----- lstring.h | 3 ++- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/lstate.c b/lstate.c index 7770e314f2..8fba70d76c 100644 --- a/lstate.c +++ b/lstate.c @@ -76,7 +76,7 @@ static unsigned int luai_makeseed (lua_State *L) { addbuff(buff, p, &h); /* local variable */ addbuff(buff, p, &lua_newstate); /* public function */ lua_assert(p == sizeof(buff)); - return luaS_hash(buff, p, h); + return luaS_hash(buff, p, h, 1); } #endif diff --git a/lstring.c b/lstring.c index a6ffbdd002..6f1574731b 100644 --- a/lstring.c +++ b/lstring.c @@ -23,7 +23,7 @@ /* -** Lua will use at most ~(2^LUAI_HASHLIMIT) bytes from a string to +** Lua will use at most ~(2^LUAI_HASHLIMIT) bytes from a long string to ** compute its hash */ #if !defined(LUAI_HASHLIMIT) @@ -50,9 +50,9 @@ int luaS_eqlngstr (TString *a, TString *b) { } -unsigned int luaS_hash (const char *str, size_t l, unsigned int seed) { +unsigned int luaS_hash (const char *str, size_t l, unsigned int seed, + size_t step) { unsigned int h = seed ^ cast_uint(l); - size_t step = (l >> LUAI_HASHLIMIT) + 1; for (; l >= step; l -= step) h ^= ((h<<5) + (h>>2) + cast_byte(str[l - 1])); return h; @@ -62,7 +62,9 @@ unsigned int luaS_hash (const char *str, size_t l, unsigned int seed) { unsigned int luaS_hashlongstr (TString *ts) { lua_assert(ts->tt == LUA_VLNGSTR); if (ts->extra == 0) { /* no hash? */ - ts->hash = luaS_hash(getstr(ts), ts->u.lnglen, ts->hash); + size_t len = ts->u.lnglen; + size_t step = (len >> LUAI_HASHLIMIT) + 1; + ts->hash = luaS_hash(getstr(ts), len, ts->hash, step); ts->extra = 1; /* now it has its hash */ } return ts->hash; @@ -199,7 +201,7 @@ static TString *internshrstr (lua_State *L, const char *str, size_t l) { TString *ts; global_State *g = G(L); stringtable *tb = &g->strt; - unsigned int h = luaS_hash(str, l, g->seed); + unsigned int h = luaS_hash(str, l, g->seed, 1); TString **list = &tb->hash[lmod(h, tb->size)]; lua_assert(str != NULL); /* otherwise 'memcmp'/'memcpy' are undefined */ for (ts = *list; ts != NULL; ts = ts->u.hnext) { diff --git a/lstring.h b/lstring.h index c23d6874f5..56896867c9 100644 --- a/lstring.h +++ b/lstring.h @@ -37,7 +37,8 @@ #define eqshrstr(a,b) check_exp((a)->tt == LUA_VSHRSTR, (a) == (b)) -LUAI_FUNC unsigned int luaS_hash (const char *str, size_t l, unsigned int seed); +LUAI_FUNC unsigned int luaS_hash (const char *str, size_t l, + unsigned int seed, size_t step); LUAI_FUNC unsigned int luaS_hashlongstr (TString *ts); LUAI_FUNC int luaS_eqlngstr (TString *a, TString *b); LUAI_FUNC void luaS_resize (lua_State *L, int newsize); From 7ccc6d8290143009d2bab8f4330bbf443fc25846 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 10 Apr 2020 15:44:48 -0300 Subject: [PATCH 0548/1145] Improvements in the manual Several small improvements, in particular a new subsection consolidating all status codes in the API. --- manual/manual.of | 110 +++++++++++++++++++++++++++-------------------- 1 file changed, 64 insertions(+), 46 deletions(-) diff --git a/manual/manual.of b/manual/manual.of index d5b4a572da..3ba82b09a8 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -96,6 +96,14 @@ is particularly attractive for small machines and embedded systems. (See macro @id{LUA_32BITS} in file @id{luaconf.h}.) +Unless stated otherwise, +any overflow when manipulating integer values @def{wrap around}, +according to the usual rules of two-complement arithmetic. +(In other words, +the actual result is the unique representable integer +that is equal modulo @M{2@sp{n}} to the mathematical result, +where @M{n} is the number of bits of the integer type.) + Lua has explicit rules about when each subtype is used, but it also converts between them automatically as needed @see{coercion}. Therefore, @@ -1098,8 +1106,11 @@ if its value fits in an integer or it is a hexadecimal constant, it denotes an integer; otherwise (that is, a decimal integer numeral that overflows), it denotes a float. -(Hexadecimal integer numerals that overflow @emph{wrap around}; -they always denote an integer value.) +Hexadecimal numerals with neither a radix point nor an exponent +always denote an integer value; +if the value overflows, it @emph{wraps around} +to fit into a valid integer. + Examples of valid integer constants are @verbatim{ 3 345 0xff 0xBEBADA @@ -1712,12 +1723,7 @@ Modulo is defined as the remainder of a division that rounds the quotient towards minus infinity (floor division). In case of overflows in integer arithmetic, -all operations @emphx{wrap around}, -according to the usual rules of two-complement arithmetic. -(In other words, -they return the unique representable integer -that is equal modulo @M{2@sp{n}} to the mathematical result, -where @M{n} is the number of bits of the integer type.) +all operations @emphx{wrap around}. } @sect3{bitwise| @title{Bitwise Operators} @@ -2364,9 +2370,8 @@ and (that is, the element at @N{the top}) and index @M{-n} represents the first element. -} -@sect2{stacksize| @title{Stack Size} +@sect3{stacksize| @title{Stack Size} When you interact with the Lua API, you are responsible for ensuring consistency. @@ -2391,7 +2396,7 @@ you should use @Lid{lua_checkstack}. } -@sect2{@title{Valid and Acceptable Indices} +@sect3{@title{Valid and Acceptable Indices} Any function in the API that receives stack indices works only with @emphx{valid indices} or @emphx{acceptable indices}. @@ -2433,6 +2438,8 @@ which behaves like a nil value. } +} + @sect2{c-closure| @title{C Closures} When a @N{C function} is created, @@ -2552,6 +2559,33 @@ However, there is no guarantee about stack space. To push anything on the stack, the panic function must first check the available space @see{stacksize}. + +@sect3{statuscodes|@title{Status Codes} + +Several functions that report errors in the API use the following +status codes to indicate different kinds of errors or other conditions: +@description{ + +@item{@defid{LUA_OK} (0)| no errors.} + +@item{@defid{LUA_ERRRUN}| a runtime error.} + +@item{@defid{LUA_ERRMEM}| +@x{memory allocation error}. +For such errors, Lua does not call the @x{message handler}. +} + +@item{@defid{LUA_ERRERR}| error while running the @x{message handler}.} + +@item{@defid{LUA_ERRSYNTAX}| syntax error during precompilation.} + +@item{@defid{LUA_YIELD}| the thread (coroutine) yields.} + +} +These constants are defined in the header file @id{lua.h}. + +} + } @sect2{continuations|@title{Handling Yields in C} @@ -3407,19 +3441,6 @@ If there are no errors, function on top of the stack. Otherwise, it pushes an error message. -The return values of @id{lua_load} are: -@description{ - -@item{@Lid{LUA_OK}| no errors;} - -@item{@defid{LUA_ERRSYNTAX}| -syntax error during precompilation;} - -@item{@Lid{LUA_ERRMEM}| -@x{memory allocation (out-of-memory) error};} - -} - The @id{lua_load} function uses a user-supplied @id{reader} function to read the chunk @seeC{lua_Reader}. The @id{data} argument is an opaque value passed to the reader function. @@ -3437,6 +3458,11 @@ a @id{NULL} value is equivalent to the string @St{bt}. so the reader function must always leave the stack unmodified when returning. +@id{lua_load} can return +@Lid{LUA_OK}, @Lid{LUA_ERRSYNTAX}, or @Lid{LUA_ERRMEM}. +The function may also return other values corresponding to +errors raised by the read function @see{statuscodes}. + If the resulting function has upvalues, its first upvalue is set to the value of the @x{global environment} stored at index @id{LUA_RIDX_GLOBALS} in the registry @see{registry}. @@ -3590,25 +3616,8 @@ information to the error object, such as a stack traceback. Such information cannot be gathered after the return of @Lid{lua_pcall}, since by then the stack has unwound. -The @Lid{lua_pcall} function returns one of the following constants -(defined in @id{lua.h}): -@description{ - -@item{@defid{LUA_OK} (0)| -success.} - -@item{@defid{LUA_ERRRUN}| -a runtime error. -} - -@item{@defid{LUA_ERRMEM}| -@x{memory allocation error}. -For such errors, Lua does not call the @x{message handler}. -} - -@item{@defid{LUA_ERRERR}| -error while running the @x{message handler}. -} +The @Lid{lua_pcall} function returns one of the following status codes: +@Lid{LUA_OK}, @Lid{LUA_ERRRUN}, @Lid{LUA_ERRMEM}, or @Lid{LUA_ERRERR}. } @@ -3624,7 +3633,7 @@ int lua_pcallk (lua_State *L, @apii{nargs + 1,nresults|1,-} This function behaves exactly like @Lid{lua_pcall}, -but allows the called function to yield @see{continuations}. +except that it allows the called function to yield @see{continuations}. } @@ -4002,7 +4011,7 @@ or returned by the body function. @Lid{LUA_YIELD} if the coroutine yields, @Lid{LUA_OK} if the coroutine finishes its execution without errors, -or an error code in case of errors @seeC{lua_pcall}. +or an error code in case of errors @see{statuscodes}. In case of errors, the error object is on the top of the stack. @@ -4153,7 +4162,7 @@ Returns the status of the thread @id{L}. The status can be @Lid{LUA_OK} for a normal thread, an error code if the thread finished the execution of a @Lid{lua_resume} with an error, -or @defid{LUA_YIELD} if the thread is suspended. +or @Lid{LUA_YIELD} if the thread is suspended. You can call functions only in threads with status @Lid{LUA_OK}. You can resume threads with status @Lid{LUA_OK} @@ -6263,6 +6272,7 @@ which is true if the call succeeds without errors. In such case, @id{pcall} also returns all results from the call, after this first result. In case of any error, @id{pcall} returns @false plus the error object. +Note that errors caught by @id{pcall} do not call a message handler. } @@ -8949,6 +8959,14 @@ For instance, the result of @T{"1" + "2"} now is an integer, not a float. } +@item{ +Literal decimal integer constants that overflow are read as floats, +instead of wrapping around. +You can use hexadecimal notation for such constants if you +want the old bevhavior +(reading them as integers with wrap around). +} + @item{ The use of the @idx{__lt} metamethod to emulate @id{__le} has been removed. From 9e0a8475cdd53af664b807c4f0c4d53088a7faf2 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 13 Apr 2020 13:42:40 -0300 Subject: [PATCH 0549/1145] Added 'simplesect' sections to the manual 'simplesect' encloses the introductory text of sections with subsections, so that each section either is all text or is all subsections. (This commit also corrects a small brace error in the manual and extra spaces/tabs in some other files.) --- lauxlib.h | 2 +- lgc.c | 2 +- lopcodes.h | 2 +- lutf8lib.c | 2 +- manual/manual.of | 54 ++++++++++++++++++++++++++++++++++++++++++++++-- 5 files changed, 56 insertions(+), 6 deletions(-) diff --git a/lauxlib.h b/lauxlib.h index b34b3805c4..59fef6af13 100644 --- a/lauxlib.h +++ b/lauxlib.h @@ -16,7 +16,7 @@ /* global table */ -#define LUA_GNAME "_G" +#define LUA_GNAME "_G" typedef struct luaL_Buffer luaL_Buffer; diff --git a/lgc.c b/lgc.c index e788843cdd..f26c921a96 100644 --- a/lgc.c +++ b/lgc.c @@ -152,7 +152,7 @@ static GCObject **getgclist (GCObject *o) { ** and its key is not marked, mark its entry as dead. This allows the ** collection of the key, but keeps its entry in the table (its removal ** could break a chain). The main feature of a dead key is that it must -** be different from any other value, to do not disturb searches. +** be different from any other value, to do not disturb searches. ** Other places never manipulate dead keys, because its associated empty ** value is enough to signal that the entry is logically empty. */ diff --git a/lopcodes.h b/lopcodes.h index d755870fe4..d3a3f08e69 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -195,7 +195,7 @@ enum OpMode {iABC, iABx, iAsBx, iAx, isJ}; /* basic instruction formats */ typedef enum { /*---------------------------------------------------------------------- -name args description + name args description ------------------------------------------------------------------------*/ OP_MOVE,/* A B R[A] := R[B] */ OP_LOADI,/* A sBx R[A] := sBx */ diff --git a/lutf8lib.c b/lutf8lib.c index e63a5a7453..3b36a60e10 100644 --- a/lutf8lib.c +++ b/lutf8lib.c @@ -29,7 +29,7 @@ ** Integer type for decoded UTF-8 values; MAXUTF needs 31 bits. */ #if (UINT_MAX >> 30) >= 1 -typedef unsigned int utfint; +typedef unsigned int utfint; #else typedef unsigned long utfint; #endif diff --git a/manual/manual.of b/manual/manual.of index 3ba82b09a8..b237ad46fc 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -59,8 +59,12 @@ see Roberto's book, @emphx{Programming in Lua}. @C{-------------------------------------------------------------------------} @sect1{basic| @title{Basic Concepts} +@simplesect{ + This section describes the basic concepts of the language. +} + @sect2{TypesSec| @title{Values and Types} Lua is a dynamically typed language. @@ -570,6 +574,8 @@ right after its creation. @sect2{GC| @title{Garbage Collection} +@simplesect{ + Lua performs automatic memory management. This means that you do not have to worry about allocating memory for new objects @@ -597,6 +603,8 @@ or @Lid{collectgarbage} in Lua. You can also use these functions to control the collector directly (e.g., to stop and restart it). +} + @sect3{incmode| @title{Incremental Garbage Collection} In incremental mode, @@ -934,6 +942,8 @@ and @Lid{lua_yield}. @C{-------------------------------------------------------------------------} @sect1{language| @title{The Language} +@simplesect{ + This section describes the lexis, the syntax, and the semantics of Lua. In other words, this section describes @@ -951,6 +961,8 @@ and other terminal symbols are shown like @bnfter{=}. The complete syntax of Lua can be found in @refsec{BNF} at the end of this manual. +} + @sect2{lexical| @title{Lexical Conventions} Lua is a @x{free-form} language. @@ -1175,12 +1187,16 @@ the variable @id{_ENV} itself is never global @see{globalenv}. @sect2{stats| @title{Statements} +@simplesect{ + Lua supports an almost conventional set of @x{statements}, similar to those in other conventional languages. This set includes blocks, assignments, control structures, function calls, and variable declarations. +} + @sect3{@title{Blocks} A @x{block} is a list of statements, @@ -1607,6 +1623,8 @@ in case of errors. @sect2{expressions| @title{Expressions} +@simplesect{ + The basic expressions in Lua are the following: @Produc{ @producname{exp}@producbody{prefixexp} @@ -1681,6 +1699,7 @@ even if @id{f} returns several values. (The value of @T{(f(x,y,z))} is the first value returned by @id{f} or @nil if @id{f} does not return any values.) +} @sect3{arith| @title{Arithmetic Operators} @@ -2301,6 +2320,8 @@ while all of them share the same @id{x}. @C{-------------------------------------------------------------------------} @sect1{API| @title{The Application Program Interface} +@simplesect{ + @index{C API} This section describes the @N{C API} for Lua, that is, the set of @N{C functions} available to the host program to communicate @@ -2337,9 +2358,13 @@ every function in the library, except to @Lid{lua_newstate}, which creates a Lua state from scratch and returns a pointer to the @emph{main thread} in the new state. +} + @sect2{@title{The Stack} +@simplesect{ + Lua uses a @emph{virtual stack} to pass values to and from C. Each element in this stack represents a Lua value (@nil, number, string, etc.). @@ -2370,6 +2395,7 @@ and (that is, the element at @N{the top}) and index @M{-n} represents the first element. +} @sect3{stacksize| @title{Stack Size} @@ -2511,6 +2537,8 @@ the @x{global environment}. @sect2{C-error|@title{Error Handling in C} +@simplesect{ + Internally, Lua uses the C @id{longjmp} facility to handle errors. (Lua will use exceptions if you compile it as C++; search for @id{LUAI_THROW} in the source code for details.) @@ -2559,6 +2587,8 @@ However, there is no guarantee about stack space. To push anything on the stack, the panic function must first check the available space @see{stacksize}. +} + @sect3{statuscodes|@title{Status Codes} @@ -3621,8 +3651,6 @@ The @Lid{lua_pcall} function returns one of the following status codes: } -} - @APIEntry{ int lua_pcallk (lua_State *L, int nargs, @@ -4975,6 +5003,8 @@ refer to the @id{n2}-th upvalue of the Lua closure at index @id{funcindex2}. @C{-------------------------------------------------------------------------} @sect1{@title{The Auxiliary Library} +@simplesect{ + @index{lauxlib.h} The @def{auxiliary library} provides several convenient functions to interface C with Lua. @@ -5009,6 +5039,9 @@ you should not use these functions for other stack values. Functions called @id{luaL_check*} always raise an error if the check is not satisfied. +} + + @sect2{@title{Functions and Types} Here we list all functions and types from the auxiliary library @@ -5933,6 +5966,8 @@ This function is used to build a prefix for error messages. @C{-------------------------------------------------------------------------} @sect1{libraries| @title{The Standard Libraries} +@simplesect{ + The standard Lua libraries provide useful functions that are implemented @N{in C} through the @N{C API}. Some of these functions provide essential services to the language @@ -6004,6 +6039,9 @@ the host program can open them individually by using and @defid{luaopen_debug} (for the debug library). These functions are declared in @defid{lualib.h}. +} + + @sect2{predefined| @title{Basic Functions} The basic library provides core functions to Lua. @@ -6834,6 +6872,8 @@ or @fail plus an error message if none succeeds. @sect2{strlib| @title{String Manipulation} +@simplesect{ + This library provides generic functions for string manipulation, such as finding and extracting substrings, and pattern matching. When indexing a string in Lua, the first character is at @N{position 1} @@ -7187,9 +7227,13 @@ The definition of what a lowercase letter is depends on the current locale. } +} + @sect3{pm| @title{Patterns} +@simplesect{ + Patterns in Lua are described by regular strings, which are interpreted as patterns by the pattern-matching functions @Lid{string.find}, @@ -7199,6 +7243,8 @@ and @Lid{string.match}. This section describes the syntax and the meaning (that is, what they match) of these strings. +} + @sect4{@title{Character Class:} A @def{character class} is used to represent a set of characters. The following combinations are allowed in describing a character class: @@ -8910,6 +8956,8 @@ is a more portable solution. @sect1{incompat| @title{Incompatibilities with the Previous Version} +@simplesect{ + Here we list the incompatibilities that you may find when moving a program from @N{Lua 5.3} to @N{Lua 5.4}. @@ -8942,6 +8990,8 @@ precompiled chunks are not compatible between different Lua versions. The standard paths in the official distribution may change between versions. +} + @sect2{@title{Incompatibilities in the Language} @itemize{ From cac075a1221f373053196dbb24bdcff4609b8241 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 23 Apr 2020 14:39:34 -0300 Subject: [PATCH 0550/1145] Small issue in 'exprstat' The code should not compute an instruction address before checking that it exists. (Virtually no machine complains of computing an invalid address, as long as the address is not used, but for ISO C that is undefined behavior.) --- lparser.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lparser.c b/lparser.c index b0dbb65c6f..27daa926cd 100644 --- a/lparser.c +++ b/lparser.c @@ -1822,8 +1822,9 @@ static void exprstat (LexState *ls) { restassign(ls, &v, 1); } else { /* stat -> func */ - Instruction *inst = &getinstruction(fs, &v.v); + Instruction *inst; check_condition(ls, v.v.k == VCALL, "syntax error"); + inst = &getinstruction(fs, &v.v); SETARG_C(*inst, 1); /* call statement uses no results */ } } From 0ddc0f47bd2a03678e1afbc384550aecb55a318f Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 23 Apr 2020 14:48:15 -0300 Subject: [PATCH 0551/1145] Several details about 5.4.0 rc1 Corrected several small details: added 'const', adjusts in tabs x spaces, removed unused #includes and #defines, misspellings, etc. --- ljumptab.h | 2 +- lmathlib.c | 2 +- loadlib.c | 2 -- lobject.h | 3 +-- lopcodes.c | 2 -- lopcodes.h | 20 ++++++++++---------- lopnames.h | 3 +++ loslib.c | 2 +- lprefix.h | 2 +- ltable.h | 2 +- lvm.h | 4 ++-- manual/manual.of | 2 +- 12 files changed, 22 insertions(+), 24 deletions(-) diff --git a/ljumptab.h b/ljumptab.h index 0edd79d5be..8306f250cc 100644 --- a/ljumptab.h +++ b/ljumptab.h @@ -16,7 +16,7 @@ #define vmbreak vmfetch(); vmdispatch(GET_OPCODE(i)); -static void *disptab[NUM_OPCODES] = { +static const void *const disptab[NUM_OPCODES] = { #if 0 ** you can update the following list with this command: diff --git a/lmathlib.c b/lmathlib.c index 63f6036aaf..86def470c4 100644 --- a/lmathlib.c +++ b/lmathlib.c @@ -328,7 +328,7 @@ static Rand64 nextrand (Rand64 *state) { */ /* must throw out the extra (64 - FIGS) bits */ -#define shift64_FIG (64 - FIGS) +#define shift64_FIG (64 - FIGS) /* to scale to [0, 1), multiply by scaleFIG = 2^(-FIGS) */ #define scaleFIG (l_mathop(0.5) / ((Rand64)1 << (FIGS - 1))) diff --git a/loadlib.c b/loadlib.c index 56167f64e0..9d86b15802 100644 --- a/loadlib.c +++ b/loadlib.c @@ -269,8 +269,6 @@ static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) { #endif -#define AUXMARK "\1" /* auxiliary mark */ - /* ** return registry.LUA_NOENV as a boolean diff --git a/lobject.h b/lobject.h index 428145a4a7..2d63c00152 100644 --- a/lobject.h +++ b/lobject.h @@ -356,8 +356,7 @@ typedef struct GCObject { /* -** Header for string value; string bytes follow the end of this structure -** (aligned according to 'UTString'; see next). +** Header for string value; string bytes follow the end of this structure. */ typedef struct TString { CommonHeader; diff --git a/lopcodes.c b/lopcodes.c index 4e983e0812..c67aa227c5 100644 --- a/lopcodes.c +++ b/lopcodes.c @@ -10,8 +10,6 @@ #include "lprefix.h" -#include - #include "lopcodes.h" diff --git a/lopcodes.h b/lopcodes.h index d3a3f08e69..122e5d21f2 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -201,9 +201,9 @@ OP_MOVE,/* A B R[A] := R[B] */ OP_LOADI,/* A sBx R[A] := sBx */ OP_LOADF,/* A sBx R[A] := (lua_Number)sBx */ OP_LOADK,/* A Bx R[A] := K[Bx] */ -OP_LOADKX,/* A R[A] := K[extra arg] */ -OP_LOADFALSE,/* A R[A] := false */ -OP_LFALSESKIP,/*A R[A] := false; pc++ */ +OP_LOADKX,/* A R[A] := K[extra arg] */ +OP_LOADFALSE,/* A R[A] := false */ +OP_LFALSESKIP,/*A R[A] := false; pc++ */ OP_LOADTRUE,/* A R[A] := true */ OP_LOADNIL,/* A B R[A], R[A+1], ..., R[A+B] := nil */ OP_GETUPVAL,/* A B R[A] := UpValue[B] */ @@ -263,11 +263,11 @@ OP_BNOT,/* A B R[A] := ~R[B] */ OP_NOT,/* A B R[A] := not R[B] */ OP_LEN,/* A B R[A] := length of R[B] */ -OP_CONCAT,/* A B R[A] := R[A].. ... ..R[A + B - 1] */ +OP_CONCAT,/* A B R[A] := R[A].. ... ..R[A + B - 1] */ OP_CLOSE,/* A close all upvalues >= R[A] */ OP_TBC,/* A mark variable A "to be closed" */ -OP_JMP,/* sJ pc += sJ */ +OP_JMP,/* sJ pc += sJ */ OP_EQ,/* A B k if ((R[A] == R[B]) ~= k) then pc++ */ OP_LT,/* A B k if ((R[A] < R[B]) ~= k) then pc++ */ OP_LE,/* A B k if ((R[A] <= R[B]) ~= k) then pc++ */ @@ -279,15 +279,15 @@ OP_LEI,/* A sB k if ((R[A] <= sB) ~= k) then pc++ */ OP_GTI,/* A sB k if ((R[A] > sB) ~= k) then pc++ */ OP_GEI,/* A sB k if ((R[A] >= sB) ~= k) then pc++ */ -OP_TEST,/* A k if (not R[A] == k) then pc++ */ +OP_TEST,/* A k if (not R[A] == k) then pc++ */ OP_TESTSET,/* A B k if (not R[B] == k) then pc++ else R[A] := R[B] */ OP_CALL,/* A B C R[A], ... ,R[A+C-2] := R[A](R[A+1], ... ,R[A+B-1]) */ OP_TAILCALL,/* A B C k return R[A](R[A+1], ... ,R[A+B-1]) */ OP_RETURN,/* A B C k return R[A], ... ,R[A+B-2] (see note) */ -OP_RETURN0,/* return */ -OP_RETURN1,/* A return R[A] */ +OP_RETURN0,/* return */ +OP_RETURN1,/* A return R[A] */ OP_FORLOOP,/* A Bx update counters; if loop continues then pc-=Bx; */ OP_FORPREP,/* A Bx ; @@ -301,9 +301,9 @@ OP_SETLIST,/* A B C k R[A][(C-1)*FPF+i] := R[A+i], 1 <= i <= B */ OP_CLOSURE,/* A Bx R[A] := closure(KPROTO[Bx]) */ -OP_VARARG,/* A C R[A], R[A+1], ..., R[A+C-2] = vararg */ +OP_VARARG,/* A C R[A], R[A+1], ..., R[A+C-2] = vararg */ -OP_VARARGPREP,/*A (adjust vararg parameters) */ +OP_VARARGPREP,/*A (adjust vararg parameters) */ OP_EXTRAARG/* Ax extra (larger) argument for previous opcode */ } OpCode; diff --git a/lopnames.h b/lopnames.h index f20147e3da..965cec9bf2 100644 --- a/lopnames.h +++ b/lopnames.h @@ -7,6 +7,9 @@ #if !defined(lopnames_h) #define lopnames_h +#include + + /* ORDER OP */ static const char *const opnames[] = { diff --git a/loslib.c b/loslib.c index 29449e4057..5e0fafb479 100644 --- a/loslib.c +++ b/loslib.c @@ -91,7 +91,7 @@ /* ISO C definitions */ #define l_gmtime(t,r) ((void)(r)->tm_sec, gmtime(t)) -#define l_localtime(t,r) ((void)(r)->tm_sec, localtime(t)) +#define l_localtime(t,r) ((void)(r)->tm_sec, localtime(t)) #endif /* } */ diff --git a/lprefix.h b/lprefix.h index dd14767ed2..484f2ad6fb 100644 --- a/lprefix.h +++ b/lprefix.h @@ -33,7 +33,7 @@ /* ** Windows stuff */ -#if defined(_WIN32) /* { */ +#if defined(_WIN32) /* { */ #if !defined(_CRT_SECURE_NO_WARNINGS) #define _CRT_SECURE_NO_WARNINGS /* avoid warnings about ISO C functions */ diff --git a/ltable.h b/ltable.h index 9565833f64..ebd7f8ec3e 100644 --- a/ltable.h +++ b/ltable.h @@ -27,7 +27,7 @@ /* returns the Node, given the value of a table entry */ -#define nodefromval(v) cast(Node *, (v)) +#define nodefromval(v) cast(Node *, (v)) LUAI_FUNC const TValue *luaH_getint (Table *t, lua_Integer key); diff --git a/lvm.h b/lvm.h index 71038572c1..2d4ac160fe 100644 --- a/lvm.h +++ b/lvm.h @@ -41,9 +41,9 @@ ** Rounding modes for float->integer coercion */ typedef enum { - F2Ieq, /* no rounding; accepts only integral values */ + F2Ieq, /* no rounding; accepts only integral values */ F2Ifloor, /* takes the floor of the number */ - F2Iceil, /* takes the ceil of the number */ + F2Iceil /* takes the ceil of the number */ } F2Imod; diff --git a/manual/manual.of b/manual/manual.of index b237ad46fc..9eeb94aa57 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -9013,7 +9013,7 @@ not a float. Literal decimal integer constants that overflow are read as floats, instead of wrapping around. You can use hexadecimal notation for such constants if you -want the old bevhavior +want the old behavior (reading them as integers with wrap around). } From a901c505abffd13e96a7d304393bb759aad87e59 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 29 Apr 2020 16:37:25 -0300 Subject: [PATCH 0552/1145] Fixed warning about casts between function pointers gcc now warns (with -Wextra) about casts between pointers to different function types. The type 'void(*)(void)' works as a 'void*' for function pointers, cleaning the warning. --- loadlib.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/loadlib.c b/loadlib.c index 9d86b15802..ddfecca986 100644 --- a/loadlib.c +++ b/loadlib.c @@ -67,6 +67,13 @@ static const char *const CLIBS = "_CLIBS"; #define setprogdir(L) ((void)0) +/* +** Special type equivalent to '(void*)' for functions in gcc +** (to supress warnings when converting function pointers) +*/ +typedef void (*voidf)(void); + + /* ** system-dependent functions */ @@ -206,7 +213,7 @@ static void *lsys_load (lua_State *L, const char *path, int seeglb) { static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) { - lua_CFunction f = (lua_CFunction)GetProcAddress((HMODULE)lib, sym); + lua_CFunction f = (lua_CFunction)(voidf)GetProcAddress((HMODULE)lib, sym); if (f == NULL) pusherror(L); return f; } From 948fb628d9d7242acfe1c3fe1f099ef228e38ead Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 30 Apr 2020 17:29:27 -0300 Subject: [PATCH 0553/1145] Details When in test mode (#include "tests.h"), force Lua to use its own implementation of 'lua_strx2number' and 'lua_number2strx' to test them. --- ltests.h | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/ltests.h b/ltests.h index db0a2a0d27..02331ebc7f 100644 --- a/ltests.h +++ b/ltests.h @@ -118,18 +118,22 @@ LUA_API void *debug_realloc (void *ud, void *block, #define MINSTRTABSIZE 2 #define MAXIWTHABS 3 +#define STRCACHE_N 23 +#define STRCACHE_M 5 + +#undef LUAI_USER_ALIGNMENT_T +#define LUAI_USER_ALIGNMENT_T union { char b[sizeof(void*) * 8]; } + /* make stack-overflow tests run faster */ #undef LUAI_MAXSTACK #define LUAI_MAXSTACK 50000 -#undef LUAI_USER_ALIGNMENT_T -#define LUAI_USER_ALIGNMENT_T union { char b[sizeof(void*) * 8]; } - +/* force Lua to use its own implementations */ +#undef lua_strx2number +#undef lua_number2strx -#define STRCACHE_N 23 -#define STRCACHE_M 5 #endif From 9a6f47f0edfded799f7cb6fd719bb0071b326100 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 4 May 2020 14:17:15 -0300 Subject: [PATCH 0554/1145] C-Stack test does not assume minimum of 400 slots --- testes/cstack.lua | 43 ++++++++++++++++++++++++++----------------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/testes/cstack.lua b/testes/cstack.lua index cd74fd281c..e3e14f7495 100644 --- a/testes/cstack.lua +++ b/testes/cstack.lua @@ -20,13 +20,14 @@ print"If this test crashes, see its file ('cstack.lua')" -- get and print original limit -local origlimit = debug.setcstacklimit(400) +local origlimit = debug.setcstacklimit(400) print("default stack limit: " .. origlimit) + -- Do the tests using the original limit. Or else you may want to change -- 'currentlimit' to lower values to avoid a seg. fault or to higher -- values to check whether they are reliable. -local currentlimit = origlimit +local currentlimit = origlimit debug.setcstacklimit(currentlimit) print("current stack limit: " .. currentlimit) @@ -107,12 +108,16 @@ end do print("testing changes in C-stack limit") + -- Just an alternative limit, different from the current one + -- (smaller to avoid stack overflows) + local alterlimit = currentlimit * 8 // 10 + assert(not debug.setcstacklimit(0)) -- limit too small assert(not debug.setcstacklimit(50000)) -- limit too large local co = coroutine.wrap (function () - return debug.setcstacklimit(400) + return debug.setcstacklimit(alterlimit) end) - assert(co() == false) -- cannot change C stack inside coroutine + assert(not co()) -- cannot change C stack inside coroutine local n local function foo () n = n + 1; foo () end @@ -123,28 +128,32 @@ do print("testing changes in C-stack limit") return n end - -- set limit to 400 - assert(debug.setcstacklimit(400) == currentlimit) - local lim400 = check() + -- set limit to 'alterlimit' + assert(debug.setcstacklimit(alterlimit) == currentlimit) + local limalter = check() -- set a very low limit (given that there are already several active -- calls to arrive here) - local lowlimit = 38 - assert(debug.setcstacklimit(lowlimit) == 400) - assert(check() < lowlimit - 30) - assert(debug.setcstacklimit(600) == lowlimit) - local lim600 = check() - assert(lim600 == lim400 + 200) + local lowlimit = 38 + assert(debug.setcstacklimit(lowlimit) == alterlimit) + -- usable limit is much lower, due to active calls + local actuallow = check() + assert(actuallow < lowlimit - 30) + -- now, add 'lowlimit' extra slots, which should all be available + assert(debug.setcstacklimit(lowlimit + lowlimit) == lowlimit) + local lim2 = check() + assert(lim2 == actuallow + lowlimit) -- 'setcstacklimit' works inside protected calls. (The new stack -- limit is kept when 'pcall' returns.) assert(pcall(function () - assert(debug.setcstacklimit(400) == 600) - assert(check() <= lim400) + assert(debug.setcstacklimit(alterlimit) == lowlimit * 2) + assert(check() <= limalter) end)) - assert(check() == lim400) - assert(debug.setcstacklimit(origlimit) == 400) -- restore original limit + assert(check() == limalter) + -- restore original limit + assert(debug.setcstacklimit(origlimit) == alterlimit) end From 61a4e64a6667bedaa882571c48a173ef5a4ba73b Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 6 May 2020 14:19:08 -0300 Subject: [PATCH 0555/1145] Back to old encoding of versions in binary files (Undoing part of commit f53eabeed8.) It is better to keep this encoding stable, so that all Lua versions can read at least the version of a binary file. --- ldump.c | 2 +- loadlib.c | 2 +- lundump.c | 2 +- lundump.h | 7 ++++++- testes/calls.lua | 4 ++-- 5 files changed, 11 insertions(+), 6 deletions(-) diff --git a/ldump.c b/ldump.c index fbadbcc99e..f848b669cb 100644 --- a/ldump.c +++ b/ldump.c @@ -196,7 +196,7 @@ static void dumpFunction (DumpState *D, const Proto *f, TString *psource) { static void dumpHeader (DumpState *D) { dumpLiteral(D, LUA_SIGNATURE); - dumpInt(D, LUAC_VERSION); + dumpByte(D, LUAC_VERSION); dumpByte(D, LUAC_FORMAT); dumpLiteral(D, LUAC_DATA); dumpByte(D, sizeof(Instruction)); diff --git a/loadlib.c b/loadlib.c index ddfecca986..c0ec9a131b 100644 --- a/loadlib.c +++ b/loadlib.c @@ -69,7 +69,7 @@ static const char *const CLIBS = "_CLIBS"; /* ** Special type equivalent to '(void*)' for functions in gcc -** (to supress warnings when converting function pointers) +** (to suppress warnings when converting function pointers) */ typedef void (*voidf)(void); diff --git a/lundump.c b/lundump.c index 17364999ba..d6b249d58c 100644 --- a/lundump.c +++ b/lundump.c @@ -276,7 +276,7 @@ static void fchecksize (LoadState *S, size_t size, const char *tname) { static void checkHeader (LoadState *S) { /* skip 1st char (already read and checked) */ checkliteral(S, &LUA_SIGNATURE[1], "not a binary chunk"); - if (loadInt(S) != LUAC_VERSION) + if (loadByte(S) != LUAC_VERSION) error(S, "version mismatch"); if (loadByte(S) != LUAC_FORMAT) error(S, "format mismatch"); diff --git a/lundump.h b/lundump.h index 5b05fed471..2df6923e24 100644 --- a/lundump.h +++ b/lundump.h @@ -18,7 +18,12 @@ #define LUAC_INT 0x5678 #define LUAC_NUM cast_num(370.5) -#define LUAC_VERSION LUA_VERSION_NUM +/* +** Encode major-minor version in one byte, one nibble for each +*/ +#define MYINT(s) (s[0]-'0') /* assume one-digit numbers */ +#define LUAC_VERSION (MYINT(LUA_VERSION_MAJOR)*16+MYINT(LUA_VERSION_MINOR)) + #define LUAC_FORMAT 0 /* this is the official format */ /* load one chunk; from lundump.c */ diff --git a/testes/calls.lua b/testes/calls.lua index 0141ffa4aa..1701f155f6 100644 --- a/testes/calls.lua +++ b/testes/calls.lua @@ -422,9 +422,9 @@ assert((function (a) return a end)() == nil) print("testing binary chunks") do - local header = string.pack("c4BBBc6BBBj", + local header = string.pack("c4BBc6BBBj", "\27Lua", -- signature - (504 >> 7) & 0x7f, (504 & 0x7f) | 0x80, -- version 5.4 (504) + 0x54, -- version 5.4 (0x54) 0, -- format "\x19\x93\r\n\x1a\n", -- data 4, -- size of instruction From 0be57b9b6d1a4ea8d41c9c9b9b434b4749ccbb27 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 7 May 2020 14:52:19 -0300 Subject: [PATCH 0556/1145] Details in comments --- lauxlib.c | 2 +- lundump.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lauxlib.c b/lauxlib.c index 723590946b..a5e9e4b5d1 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -87,7 +87,7 @@ static int pushglobalfuncname (lua_State *L, lua_Debug *ar) { lua_remove(L, -2); /* remove original name */ } lua_copy(L, -1, top + 1); /* copy name to proper place */ - lua_settop(L, top + 1); /* remove table "loaded" an name copy */ + lua_settop(L, top + 1); /* remove table "loaded" and name copy */ return 1; } else { diff --git a/lundump.h b/lundump.h index 2df6923e24..f3748a9980 100644 --- a/lundump.h +++ b/lundump.h @@ -21,7 +21,7 @@ /* ** Encode major-minor version in one byte, one nibble for each */ -#define MYINT(s) (s[0]-'0') /* assume one-digit numbers */ +#define MYINT(s) (s[0]-'0') /* assume one-digit numerals */ #define LUAC_VERSION (MYINT(LUA_VERSION_MAJOR)*16+MYINT(LUA_VERSION_MINOR)) #define LUAC_FORMAT 0 /* this is the official format */ From 9514abc2da3525ef4314a8fcf70982ad07319e51 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 19 May 2020 12:42:20 -0300 Subject: [PATCH 0557/1145] Cleaner definition for 'TString' Use a variable-sized array to store string contents at the end of a structure 'TString', instead of raw memory. --- lobject.h | 7 +++---- lstring.h | 6 +++++- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/lobject.h b/lobject.h index 2d63c00152..04a81d3de4 100644 --- a/lobject.h +++ b/lobject.h @@ -356,7 +356,7 @@ typedef struct GCObject { /* -** Header for string value; string bytes follow the end of this structure. +** Header for a string value. */ typedef struct TString { CommonHeader; @@ -367,16 +367,15 @@ typedef struct TString { size_t lnglen; /* length for long strings */ struct TString *hnext; /* linked list for hash table */ } u; + char contents[1]; } TString; /* ** Get the actual string (array of bytes) from a 'TString'. -** (Access to 'extra' ensures that value is really a 'TString'.) */ -#define getstr(ts) \ - check_exp(sizeof((ts)->extra), cast_charp((ts)) + sizeof(TString)) +#define getstr(ts) ((ts)->contents) /* get the actual string (array of bytes) from a Lua value */ diff --git a/lstring.h b/lstring.h index 56896867c9..a413a9d3a0 100644 --- a/lstring.h +++ b/lstring.h @@ -19,7 +19,11 @@ #define MEMERRMSG "not enough memory" -#define sizelstring(l) (sizeof(TString) + ((l) + 1) * sizeof(char)) +/* +** Size of a TString: Size of the header plus space for the string +** itself (including final '\0'). +*/ +#define sizelstring(l) (offsetof(TString, contents) + ((l) + 1) * sizeof(char)) #define luaS_newliteral(L, s) (luaS_newlstr(L, "" s, \ (sizeof(s)/sizeof(char))-1)) From 17dbaa8639505c9ad1a9946591f5960123fbd741 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 22 May 2020 11:40:34 -0300 Subject: [PATCH 0558/1145] Improvements in the handling of signals Added 'volatile' to 'l_signalT' variables plus some minor changes. --- ldebug.c | 20 +++++++++++--------- ldo.c | 10 +++++----- lstate.c | 9 +++++---- lstate.h | 4 ++-- lua.c | 3 ++- 5 files changed, 25 insertions(+), 21 deletions(-) diff --git a/ldebug.c b/ldebug.c index eaac16f734..afdc2b74ab 100644 --- a/ldebug.c +++ b/ldebug.c @@ -107,13 +107,15 @@ static int getcurrentline (CallInfo *ci) { /* -** This function can be called asynchronously (e.g. during a signal), -** under "reasonable" assumptions. A new 'ci' is completely linked -** in the list before it becomes part of the "active" list, and -** we assume that pointers are atomic (see comment in next function). -** (If we traverse one more item, there is no problem. If we traverse -** one less item, the worst that can happen is that the signal will -** not interrupt the script.) +** Set 'trap' for all active Lua frames. +** This function can be called during a signal, under "reasonable" +** assumptions. A new 'ci' is completely linked in the list before it +** becomes part of the "active" list, and we assume that pointers are +** atomic; see comment in next function. +** (A compiler doing interprocedural optimizations could, theoretically, +** reorder memory writes in such a way that the list could be +** temporarily broken while inserting a new element. We simply assume it +** has no good reasons to do that.) */ static void settraps (CallInfo *ci) { for (; ci != NULL; ci = ci->previous) @@ -123,8 +125,8 @@ static void settraps (CallInfo *ci) { /* -** This function can be called asynchronously (e.g. during a signal), -** under "reasonable" assumptions. +** This function can be called during a signal, under "reasonable" +** assumptions. ** Fields 'oldpc', 'basehookcount', and 'hookcount' (set by ** 'resethookcount') are for debug only, and it is no problem if they ** get arbitrary values (causes at most one wrong hook call). 'hookmask' diff --git a/ldo.c b/ldo.c index 64fe2915e4..c563b1d9b3 100644 --- a/ldo.c +++ b/ldo.c @@ -422,7 +422,7 @@ void luaD_poscall (lua_State *L, CallInfo *ci, int nres) { -#define next_ci(L) (L->ci = (L->ci->next ? L->ci->next : luaE_extendCI(L))) +#define next_ci(L) (L->ci->next ? L->ci->next : luaE_extendCI(L)) /* @@ -466,13 +466,13 @@ void luaD_call (lua_State *L, StkId func, int nresults) { f = fvalue(s2v(func)); Cfunc: { int n; /* number of returns */ - CallInfo *ci; + CallInfo *ci = next_ci(L); checkstackp(L, LUA_MINSTACK, func); /* ensure minimum stack size */ - ci = next_ci(L); ci->nresults = nresults; ci->callstatus = CIST_C; ci->top = L->top + LUA_MINSTACK; ci->func = func; + L->ci = ci; lua_assert(ci->top <= L->stack_last); if (L->hookmask & LUA_MASKCALL) { int narg = cast_int(L->top - func) - 1; @@ -486,18 +486,18 @@ void luaD_call (lua_State *L, StkId func, int nresults) { break; } case LUA_VLCL: { /* Lua function */ - CallInfo *ci; + CallInfo *ci = next_ci(L); Proto *p = clLvalue(s2v(func))->p; int narg = cast_int(L->top - func) - 1; /* number of real arguments */ int nfixparams = p->numparams; int fsize = p->maxstacksize; /* frame size */ checkstackp(L, fsize, func); - ci = next_ci(L); ci->nresults = nresults; ci->u.l.savedpc = p->code; /* starting point */ ci->callstatus = 0; ci->top = func + 1 + fsize; ci->func = func; + L->ci = ci; for (; narg < nfixparams; narg++) setnilvalue(s2v(L->top++)); /* complete missing arguments */ lua_assert(ci->top <= L->stack_last); diff --git a/lstate.c b/lstate.c index 8fba70d76c..42a48436a8 100644 --- a/lstate.c +++ b/lstate.c @@ -190,14 +190,15 @@ void luaE_freeCI (lua_State *L) { */ void luaE_shrinkCI (lua_State *L) { CallInfo *ci = L->ci; + CallInfo *next; CallInfo *next2; /* next's next */ L->nCcalls += L->nci; /* add removed elements back to 'nCcalls' */ /* while there are two nexts */ - while (ci->next != NULL && (next2 = ci->next->next) != NULL) { - luaM_free(L, ci->next); /* free next */ - L->nci--; - ci->next = next2; /* remove 'next' from the list */ + while ((next = ci->next) != NULL && (next2 = next->next) != NULL) { + ci->next = next2; /* remove next from the list */ next2->previous = ci; + luaM_free(L, next); /* free next */ + L->nci--; ci = next2; /* keep next's next */ } L->nCcalls -= L->nci; /* adjust result */ diff --git a/lstate.h b/lstate.h index df9148eb74..2e8bd6c486 100644 --- a/lstate.h +++ b/lstate.h @@ -173,7 +173,7 @@ typedef struct CallInfo { union { struct { /* only for Lua functions */ const Instruction *savedpc; - l_signalT trap; + volatile l_signalT trap; int nextraargs; /* # of extra arguments in vararg functions */ } l; struct { /* only for C functions */ @@ -300,7 +300,7 @@ struct lua_State { int stacksize; int basehookcount; int hookcount; - l_signalT hookmask; + volatile l_signalT hookmask; }; diff --git a/lua.c b/lua.c index 18f53c3041..454ce12f36 100644 --- a/lua.c +++ b/lua.c @@ -54,8 +54,9 @@ static void lstop (lua_State *L, lua_Debug *ar) { ** interpreter. */ static void laction (int i) { + int flag = LUA_MASKCALL | LUA_MASKRET | LUA_MASKLINE | LUA_MASKCOUNT; signal(i, SIG_DFL); /* if another SIGINT happens, terminate process */ - lua_sethook(globalL, lstop, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1); + lua_sethook(globalL, lstop, flag, 1); } From efcf24be0c22cba57b298161bf4ab0561fd3c08e Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 22 May 2020 15:39:29 -0300 Subject: [PATCH 0559/1145] 'luaL_execresult' does not assume -1 status as error ISO C is silent about the return of 'system'. Windows sets 'errno' in case of errors. Linux has several different error cases, with different return values. ISO C allows 'system' to set 'errno' even if there are no errors. Here we assume that a status==0 is success (which is the case on several platforms), otherwise it is an error. If there is an error number, gives the error based on it. (The worst a spurious 'errno' can do is to generate a bad error message.) Otherwise uses the normal results. --- lauxlib.c | 2 +- liolib.c | 1 + loslib.c | 6 ++++-- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/lauxlib.c b/lauxlib.c index a5e9e4b5d1..f2ba704f31 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -284,7 +284,7 @@ LUALIB_API int luaL_fileresult (lua_State *L, int stat, const char *fname) { LUALIB_API int luaL_execresult (lua_State *L, int stat) { const char *what = "exit"; /* type of termination */ - if (stat == -1) /* error? */ + if (stat != 0 && errno != 0) /* error with an 'errno'? */ return luaL_fileresult(L, 0, NULL); else { l_inspectstat(stat, what); /* interpret result */ diff --git a/liolib.c b/liolib.c index 08d18397f9..7ac3444393 100644 --- a/liolib.c +++ b/liolib.c @@ -270,6 +270,7 @@ static int io_open (lua_State *L) { */ static int io_pclose (lua_State *L) { LStream *p = tolstream(L); + errno = 0; return luaL_execresult(L, l_pclose(L, p->f)); } diff --git a/loslib.c b/loslib.c index 5e0fafb479..e65e188bd7 100644 --- a/loslib.c +++ b/loslib.c @@ -10,6 +10,7 @@ #include "lprefix.h" +#include #include #include #include @@ -138,10 +139,11 @@ - static int os_execute (lua_State *L) { const char *cmd = luaL_optstring(L, 1, NULL); - int stat = system(cmd); + int stat; + errno = 0; + stat = system(cmd); if (cmd != NULL) return luaL_execresult(L, stat); else { From aa8d4a782d88738b3ea921cde5a450656da8fa63 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 27 May 2020 11:46:47 -0300 Subject: [PATCH 0560/1145] Details (more uniformity in error messages) --- lauxlib.c | 2 +- lutf8lib.c | 10 +++++----- testes/utf8.lua | 18 +++++++++--------- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/lauxlib.c b/lauxlib.c index f2ba704f31..e6d741688d 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -476,7 +476,7 @@ static void *resizebox (lua_State *L, int idx, size_t newsize) { UBox *box = (UBox *)lua_touserdata(L, idx); void *temp = allocf(ud, box->box, box->bsize, newsize); if (temp == NULL && newsize > 0) /* allocation error? */ - luaL_error(L, "not enough memory for buffer allocation"); + luaL_error(L, "not enough memory"); box->box = temp; box->bsize = newsize; return temp; diff --git a/lutf8lib.c b/lutf8lib.c index 3b36a60e10..901d985f8d 100644 --- a/lutf8lib.c +++ b/lutf8lib.c @@ -97,9 +97,9 @@ static int utflen (lua_State *L) { lua_Integer posj = u_posrelat(luaL_optinteger(L, 3, -1), len); int lax = lua_toboolean(L, 4); luaL_argcheck(L, 1 <= posi && --posi <= (lua_Integer)len, 2, - "initial position out of string"); + "initial position out of bounds"); luaL_argcheck(L, --posj < (lua_Integer)len, 3, - "final position out of string"); + "final position out of bounds"); while (posi <= posj) { const char *s1 = utf8_decode(s + posi, NULL, !lax); if (s1 == NULL) { /* conversion error? */ @@ -127,8 +127,8 @@ static int codepoint (lua_State *L) { int lax = lua_toboolean(L, 4); int n; const char *se; - luaL_argcheck(L, posi >= 1, 2, "out of range"); - luaL_argcheck(L, pose <= (lua_Integer)len, 3, "out of range"); + luaL_argcheck(L, posi >= 1, 2, "out of bounds"); + luaL_argcheck(L, pose <= (lua_Integer)len, 3, "out of bounds"); if (posi > pose) return 0; /* empty interval; return no values */ if (pose - posi >= INT_MAX) /* (lua_Integer -> int) overflow? */ return luaL_error(L, "string slice too long"); @@ -187,7 +187,7 @@ static int byteoffset (lua_State *L) { lua_Integer posi = (n >= 0) ? 1 : len + 1; posi = u_posrelat(luaL_optinteger(L, 3, posi), len); luaL_argcheck(L, 1 <= posi && --posi <= (lua_Integer)len, 3, - "position out of range"); + "position out of bounds"); if (n == 0) { /* find beginning of current byte sequence */ while (posi > 0 && iscont(s + posi)) posi--; diff --git a/testes/utf8.lua b/testes/utf8.lua index 5954f6e89e..6010d1ad39 100644 --- a/testes/utf8.lua +++ b/testes/utf8.lua @@ -115,17 +115,17 @@ do end -- error in initial position for offset -checkerror("position out of range", utf8.offset, "abc", 1, 5) -checkerror("position out of range", utf8.offset, "abc", 1, -4) -checkerror("position out of range", utf8.offset, "", 1, 2) -checkerror("position out of range", utf8.offset, "", 1, -1) +checkerror("position out of bounds", utf8.offset, "abc", 1, 5) +checkerror("position out of bounds", utf8.offset, "abc", 1, -4) +checkerror("position out of bounds", utf8.offset, "", 1, 2) +checkerror("position out of bounds", utf8.offset, "", 1, -1) checkerror("continuation byte", utf8.offset, "𦧺", 1, 2) checkerror("continuation byte", utf8.offset, "𦧺", 1, 2) checkerror("continuation byte", utf8.offset, "\x80", 1) -- error in indices for len -checkerror("out of string", utf8.len, "abc", 0, 2) -checkerror("out of string", utf8.len, "abc", 1, 4) +checkerror("out of bounds", utf8.len, "abc", 0, 2) +checkerror("out of bounds", utf8.len, "abc", 1, 4) local s = "hello World" @@ -140,11 +140,11 @@ do local t = {utf8.codepoint(s,1,#s - 1)} assert(#t == 3 and t[1] == 225 and t[2] == 233 and t[3] == 237) checkerror("invalid UTF%-8 code", utf8.codepoint, s, 1, #s) - checkerror("out of range", utf8.codepoint, s, #s + 1) + checkerror("out of bounds", utf8.codepoint, s, #s + 1) t = {utf8.codepoint(s, 4, 3)} assert(#t == 0) - checkerror("out of range", utf8.codepoint, s, -(#s + 1), 1) - checkerror("out of range", utf8.codepoint, s, 1, #s + 1) + checkerror("out of bounds", utf8.codepoint, s, -(#s + 1), 1) + checkerror("out of bounds", utf8.codepoint, s, 1, #s + 1) -- surrogates assert(utf8.codepoint("\u{D7FF}") == 0xD800 - 1) assert(utf8.codepoint("\u{E000}") == 0xDFFF + 1) From 50523b107d5bcc8069b1aec4b5b11b3fcc87da8d Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 29 May 2020 10:41:32 -0300 Subject: [PATCH 0561/1145] Improvements in the manual - more consistent nomenclature for error handling - more precise definition for dead objects - added algorithm used by 'math.random' - added luaL_pushfail - some other minor changes --- manual/manual.of | 131 +++++++++++++++++++++++++++++++---------------- 1 file changed, 86 insertions(+), 45 deletions(-) diff --git a/manual/manual.of b/manual/manual.of index 9eeb94aa57..2eadbda07e 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -252,33 +252,47 @@ of its first upvalue; see @See{lua_setupvalue}.) @sect2{error| @title{Error Handling} +Several operations in Lua can @emph{raise} an error. +An error interrupts the normal flow of the program, +which can continue by @emph{catching} the error. + +Lua code can explicitly raise an error by calling the +@Lid{error} function. +(This function never returns.) + +To catch errors in Lua, +you can do a @def{protected call}, +using @Lid{pcall} (or @Lid{xpcall}). +The function @Lid{pcall} calls a given function in @def{protected mode}. +Any error while running the function stops its execution, +and control returns immediately to @id{pcall}, +which returns a status code. + Because Lua is an embedded extension language, -all Lua actions start from @N{C code} in the host program -calling a function from the Lua library. +Lua code starts running by a call +from @N{C code} in the host program. (When you use Lua standalone, the @id{lua} application is the host program.) -Whenever an error occurs during +Usually, this call is protected; +so, when an otherwise unprotected error occurs during the compilation or execution of a Lua chunk, control returns to the host, -which can take appropriate measures -(such as printing an error message). - -Lua code can explicitly generate an error by calling the -@Lid{error} function. -If you need to catch errors in Lua, -you can use @Lid{pcall} or @Lid{xpcall} -to call a given function in @emphx{protected mode}. +which can take appropriate measures, +such as printing an error message. Whenever there is an error, -an @def{error object} (also called an @def{error message}) +an @def{error object} is propagated with information about the error. Lua itself only generates errors whose error object is a string, but programs may generate errors with any value as the error object. It is up to the Lua program or its host to handle such error objects. +For historical reasons, +an error object is often called an @def{error message}, +even though it does not have to be a string. -When you use @Lid{xpcall} or @Lid{lua_pcall}, +When you use @Lid{xpcall} (or @Lid{lua_pcall}, in C) you may give a @def{message handler} to be called in case of errors. This function is called with the original error object @@ -581,11 +595,30 @@ This means that you do not have to worry about allocating memory for new objects or freeing it when the objects are no longer needed. Lua manages memory automatically by running -a @def{garbage collector} to collect all @emph{dead objects} -(that is, objects that are no longer accessible from Lua). +a @def{garbage collector} to collect all @emph{dead} objects. All memory used by Lua is subject to automatic management: strings, tables, userdata, functions, threads, internal structures, etc. +An object is considered @def{dead} +as soon as the collector can be sure the object +will not be accessed again in the normal execution of the program. +(@Q{Normal execution} here excludes finalizers, +which can resurrect dead objects @see{finalizers}, +and excludes also operations using the debug library.) +Note that the time when the collector can be sure that an object +is dead may not coincide with the programmer's expectations. +The only guarantees are that Lua will not collect an object +that may still be accessed in the normal execution of the program, +and it will eventually collect an object +that is inaccessible from Lua. +(Here, +@emph{inaccessible from Lua} means that neither a variable nor +another live object refer to the object.) +Because Lua has no knowledge about @N{C code}, +it never collects objects accessible through the registry @see{registry}, +which includes the global environment @see{globalenv}. + + The garbage collector (GC) in Lua can work in two modes: incremental and generational. @@ -694,7 +727,7 @@ and, using the @N{C API}, for full userdata @see{metatable}. These metamethods, called @def{finalizers}, are called when the garbage collector detects that the -corresponding table or userdata is unreachable. +corresponding table or userdata is dead. Finalizers allow you to coordinate Lua's garbage collection with external resource management such as closing files, network or database connections, @@ -709,7 +742,7 @@ Note that if you set a metatable without a @idx{__gc} field and later create that field in the metatable, the object will not be marked for finalization. -When a marked object becomes unreachable, +When a marked object becomes dead, it is not collected immediately by the garbage collector. Instead, Lua puts it in a list. After the collection, @@ -738,10 +771,10 @@ However, if the finalizer stores the object in some global place then the resurrection is permanent. Moreover, if the finalizer marks a finalizing object for finalization again, its finalizer will be called again in the next cycle where the -object is unreachable. +object is dead. In any case, the object memory is freed only in a GC cycle where -the object is unreachable and not marked for finalization. +the object is dead and not marked for finalization. When you close a state @seeF{lua_close}, Lua calls the finalizers of all objects marked for finalization, @@ -2611,6 +2644,9 @@ For such errors, Lua does not call the @x{message handler}. @item{@defid{LUA_YIELD}| the thread (coroutine) yields.} +@item{@defid{LUA_ERRFILE}| a file-related error; +e.g., it cannot open or read the file.} + } These constants are defined in the header file @id{lua.h}. @@ -3113,7 +3149,7 @@ This function does not pop the Lua function from the stack. @APIEntry{int lua_error (lua_State *L);| @apii{1,0,v} -Generates a Lua error, +Raises a Lua error, using the value on the top of the stack as the error object. This function does a long jump, and therefore never returns @@ -4125,8 +4161,9 @@ Returns 0 if the userdata does not have that value. @APIEntry{int lua_setmetatable (lua_State *L, int index);| @apii{1,0,-} -Pops a table from the stack and -sets it as the new metatable for the value at the given index. +Pops a table or @nil from the stack and +sets that value as the new metatable for the value at the given index. +(@nil means no metatable.) (For historical reasons, this function returns an @id{int}, which now is always 1.) @@ -4596,7 +4633,7 @@ If @T{source} starts with a @Char{@At}, it means that the function was defined in a file where the file name follows the @Char{@At}. If @T{source} starts with a @Char{=}, -the remainder of its contents describe the source in a user-dependent manner. +the remainder of its contents describes the source in a user-dependent manner. Otherwise, the function was defined in a string where @T{source} is that string. @@ -5212,7 +5249,7 @@ plus the final string on its top. @APIEntry{char *luaL_buffaddr (luaL_Buffer *B);| @apii{0,0,-} -Returns the address of the current contents of buffer @id{B} +Returns the address of the current content of buffer @id{B} @seeC{luaL_Buffer}. Note that any addition to the buffer may invalidate this address. @@ -5231,7 +5268,7 @@ the buffer must be declared as a variable. @APIEntry{size_t luaL_bufflen (luaL_Buffer *B);| @apii{0,0,-} -Returns the length of the current contents of buffer @id{B} +Returns the length of the current content of buffer @id{B} @seeC{luaL_Buffer}. } @@ -5384,8 +5421,8 @@ It is defined as the following macro: @verbatim{ (luaL_loadfile(L, filename) || lua_pcall(L, 0, LUA_MULTRET, 0)) } -It returns false if there are no errors -or true in case of errors. +It returns @Lid{LUA_OK} if there are no errors, +or an error code in case of errors @see{statuscodes}. } @@ -5397,8 +5434,8 @@ It is defined as the following macro: @verbatim{ (luaL_loadstring(L, str) || lua_pcall(L, 0, LUA_MULTRET, 0)) } -It returns false if there are no errors -or true in case of errors. +It returns @Lid{LUA_OK} if there are no errors, +or an error code in case of errors @see{statuscodes}. } @@ -5548,10 +5585,8 @@ The first line in the file is ignored if it starts with a @T{#}. The string @id{mode} works as in the function @Lid{lua_load}. -This function returns the same results as @Lid{lua_load}, -but it has an extra error code @defid{LUA_ERRFILE} -for file-related errors -(e.g., it cannot open or read the file). +This function returns the same results as @Lid{lua_load} +or @Lid{LUA_ERRFILE} for file-related errors. As @Lid{lua_load}, this function only loads the chunk; it does not run it. @@ -5742,6 +5777,13 @@ it to the buffer. } +@APIEntry{void luaL_pushfail (lua_State *L);| +@apii{0,1,-} + +Pushes the @fail value onto the stack. + +} + @APIEntry{void luaL_pushresult (luaL_Buffer *B);| @apii{?,1,m} @@ -6052,7 +6094,7 @@ implementations for some of its facilities. @LibEntry{assert (v [, message])| -Calls @Lid{error} if +Raises an error if the value of its argument @id{v} is false (i.e., @nil or @false); otherwise, returns all its arguments. In case of error, @@ -6129,9 +6171,9 @@ and some of these options. } @LibEntry{dofile ([filename])| -Opens the named file and executes its contents as a Lua chunk. +Opens the named file and executes its content as a Lua chunk. When called without arguments, -@id{dofile} executes the contents of the standard input (@id{stdin}). +@id{dofile} executes the content of the standard input (@id{stdin}). Returns all values returned by the chunk. In case of errors, @id{dofile} propagates the error to its caller. @@ -6140,8 +6182,7 @@ to its caller. } @LibEntry{error (message [, level])| -Terminates the last protected function called -and returns @id{message} as the error object. +Raises an error @see{error} with @{message} as the error object. This function never returns. Usually, @id{error} adds some information about the error position @@ -6301,7 +6342,7 @@ the table during its traversal. @LibEntry{pcall (f [, arg1, @Cdots])| Calls the function @id{f} with -the given arguments in @def{protected mode}. +the given arguments in @emphx{protected mode}. This means that any error @N{inside @T{f}} is not propagated; instead, @id{pcall} catches the error and returns a status code. @@ -7899,17 +7940,17 @@ is equivalent to @T{math.random(1,n)}. The call @T{math.random(0)} produces an integer with all bits (pseudo)random. +This function uses the @idx{xoshiro256**} algorithm to produce +pseudo-random 64-bit integers, +which are the results of calls with @N{argument 0}. +Other results (ranges and floats) +are unbiased extracted from these integers. + Lua initializes its pseudo-random generator with the equivalent of a call to @Lid{math.randomseed} with no arguments, so that @id{math.random} should generate different sequences of results each time the program runs. -The results from this function have good statistical qualities, -but they are not cryptographically secure. -(For instance, there are no guarantees that it is hard -to predict future results based on the observation of -some previous results.) - } @LibEntry{math.randomseed ([x [, y]])| From 63295f1f7fa052fabcb4d69d49203cf33a7deef0 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 1 Jun 2020 15:07:58 -0300 Subject: [PATCH 0562/1145] Fixed two bugs in to-be-closed variables x constants The parser were mixing compiler indices of variables with stack indices, so that when a to-be-closed variable was used inside the scope of compile-time constants (which may be optimized away), it might be closed in the wrong place. (See new tests for examples.) Besides fixing the bugs, this commit also changed comments and variable names to avoid that kind of confusion and added tests. --- lparser.c | 64 ++++++++++++++++++++++++++--------------------- lparser.h | 4 +-- testes/locals.lua | 37 +++++++++++++++++++++++++++ 3 files changed, 75 insertions(+), 30 deletions(-) diff --git a/lparser.c b/lparser.c index 27daa926cd..37102b7213 100644 --- a/lparser.c +++ b/lparser.c @@ -212,27 +212,28 @@ static int new_localvar (LexState *ls, TString *name) { /* -** Return the "variable description" (Vardesc) of a given -** variable +** Return the "variable description" (Vardesc) of a given variable. +** (Unless noted otherwise, all variables are referred to by their +** compiler indices.) */ -static Vardesc *getlocalvardesc (FuncState *fs, int i) { - return &fs->ls->dyd->actvar.arr[fs->firstlocal + i]; +static Vardesc *getlocalvardesc (FuncState *fs, int vidx) { + return &fs->ls->dyd->actvar.arr[fs->firstlocal + vidx]; } /* -** Convert 'nvar' (number of active variables at some point) to -** number of variables in the stack at that point. +** Convert 'nvar', a compiler index level, to it corresponding +** stack index level. For that, search for the highest variable +** below that level that is in the stack and uses its stack +** index ('sidx'). */ static int stacklevel (FuncState *fs, int nvar) { - while (nvar > 0) { - Vardesc *vd = getlocalvardesc(fs, nvar - 1); + while (nvar-- > 0) { + Vardesc *vd = getlocalvardesc(fs, nvar); /* get variable */ if (vd->vd.kind != RDKCTC) /* is in the stack? */ return vd->vd.sidx + 1; - else - nvar--; /* try previous variable */ } - return 0; /* no variables */ + return 0; /* no variables in the stack */ } @@ -245,10 +246,10 @@ int luaY_nvarstack (FuncState *fs) { /* -** Get the debug-information entry for current variable 'i'. +** Get the debug-information entry for current variable 'vidx'. */ -static LocVar *localdebuginfo (FuncState *fs, int i) { - Vardesc *vd = getlocalvardesc(fs, i); +static LocVar *localdebuginfo (FuncState *fs, int vidx) { + Vardesc *vd = getlocalvardesc(fs, vidx); if (vd->vd.kind == RDKCTC) return NULL; /* no debug info. for constants */ else { @@ -259,14 +260,20 @@ static LocVar *localdebuginfo (FuncState *fs, int i) { } -static void init_var (FuncState *fs, expdesc *e, int i) { +/* +** Create an expression representing variable 'vidx' +*/ +static void init_var (FuncState *fs, expdesc *e, int vidx) { e->f = e->t = NO_JUMP; e->k = VLOCAL; - e->u.var.vidx = i; - e->u.var.sidx = getlocalvardesc(fs, i)->vd.sidx; + e->u.var.vidx = vidx; + e->u.var.sidx = getlocalvardesc(fs, vidx)->vd.sidx; } +/* +** Raises an error if variable described by 'e' is read only +*/ static void check_readonly (LexState *ls, expdesc *e) { FuncState *fs = ls->fs; TString *varname = NULL; /* to be set if variable is const */ @@ -306,8 +313,8 @@ static void adjustlocalvars (LexState *ls, int nvars) { int stklevel = luaY_nvarstack(fs); int i; for (i = 0; i < nvars; i++) { - int varidx = fs->nactvar++; - Vardesc *var = getlocalvardesc(fs, varidx); + int vidx = fs->nactvar++; + Vardesc *var = getlocalvardesc(fs, vidx); var->vd.sidx = stklevel++; var->vd.pidx = registerlocalvar(ls, fs, var->vd.name); } @@ -377,7 +384,8 @@ static int newupvalue (FuncState *fs, TString *name, expdesc *v) { /* ** Look for an active local variable with the name 'n' in the -** function 'fs'. +** function 'fs'. If found, initialize 'var' with it and return +** its expression kind; otherwise return -1. */ static int searchvar (FuncState *fs, TString *n, expdesc *var) { int i; @@ -1592,7 +1600,7 @@ static void forlist (LexState *ls, TString *indexname) { line = ls->linenumber; adjust_assign(ls, 4, explist(ls, &e), &e); adjustlocalvars(ls, 4); /* control variables */ - markupval(fs, luaY_nvarstack(fs)); /* state may create an upvalue */ + markupval(fs, fs->nactvar); /* last control var. must be closed */ luaK_checkstack(fs, 3); /* extra space to call generator */ forbody(ls, base, line, nvars - 4, 1); } @@ -1730,7 +1738,7 @@ static int getlocalattribute (LexState *ls) { luaK_semerror(ls, luaO_pushfstring(ls->L, "unknown attribute '%s'", attr)); } - return VDKREG; + return VDKREG; /* regular variable */ } @@ -1739,7 +1747,7 @@ static void checktoclose (LexState *ls, int level) { FuncState *fs = ls->fs; markupval(fs, level + 1); fs->bl->insidetbc = 1; /* in the scope of a to-be-closed variable */ - luaK_codeABC(fs, OP_TBC, level, 0, 0); + luaK_codeABC(fs, OP_TBC, stacklevel(fs, level), 0, 0); } } @@ -1749,18 +1757,18 @@ static void localstat (LexState *ls) { FuncState *fs = ls->fs; int toclose = -1; /* index of to-be-closed variable (if any) */ Vardesc *var; /* last variable */ - int ivar, kind; /* index and kind of last variable */ + int vidx, kind; /* index and kind of last variable */ int nvars = 0; int nexps; expdesc e; do { - ivar = new_localvar(ls, str_checkname(ls)); + vidx = new_localvar(ls, str_checkname(ls)); kind = getlocalattribute(ls); - getlocalvardesc(fs, ivar)->vd.kind = kind; + getlocalvardesc(fs, vidx)->vd.kind = kind; if (kind == RDKTOCLOSE) { /* to-be-closed? */ if (toclose != -1) /* one already present? */ luaK_semerror(ls, "multiple to-be-closed variables in local list"); - toclose = luaY_nvarstack(fs) + nvars; + toclose = fs->nactvar + nvars; } nvars++; } while (testnext(ls, ',')); @@ -1770,7 +1778,7 @@ static void localstat (LexState *ls) { e.k = VVOID; nexps = 0; } - var = getlocalvardesc(fs, ivar); /* get last variable */ + var = getlocalvardesc(fs, vidx); /* get last variable */ if (nvars == nexps && /* no adjustments? */ var->vd.kind == RDKCONST && /* last variable is const? */ luaK_exp2const(fs, &e, &var->k)) { /* compile-time constant? */ diff --git a/lparser.h b/lparser.h index f544492e81..618cb0106f 100644 --- a/lparser.h +++ b/lparser.h @@ -77,7 +77,7 @@ typedef struct expdesc { } ind; struct { /* for local variables */ lu_byte sidx; /* index in the stack */ - unsigned short vidx; /* index in 'actvar.arr' */ + unsigned short vidx; /* compiler index (in 'actvar.arr') */ } var; } u; int t; /* patch list of 'exit when true' */ @@ -125,7 +125,7 @@ typedef struct Labellist { /* dynamic structures used by the parser */ typedef struct Dyndata { - struct { /* list of active local variables */ + struct { /* list of all active local variables */ Vardesc *arr; int n; int size; diff --git a/testes/locals.lua b/testes/locals.lua index 4f103be94a..0e5e0c743c 100644 --- a/testes/locals.lua +++ b/testes/locals.lua @@ -264,6 +264,43 @@ do end +-- testing to-be-closed x compile-time constants +-- (there were some bugs here in Lua 5.4-rc3, due to a confusion +-- between compile levels and stack levels of variables) +do + local flag = false + local x = setmetatable({}, + {__close = function() assert(flag == false); flag = true end}) + local y = nil + local z = nil + do + local a = x + end + assert(flag) -- 'x' must be closed here +end + +do + -- similar problem, but with implicit close in for loops + local flag = false + local x = setmetatable({}, + {__close = function () assert(flag == false); flag = true end}) + -- return an empty iterator, nil, nil, and 'x' to be closed + local function a () + return (function () return nil end), nil, nil, x + end + local v = 1 + local w = 1 + local x = 1 + local y = 1 + local z = 1 + for k in a() do + a = k + end -- ending the loop must close 'x' + assert(flag) -- 'x' must be closed here +end + + + do -- calls cannot be tail in the scope of to-be-closed variables local X, Y From 364e569945c044fd18c70ee1bc851364534aef97 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 9 Jun 2020 16:12:01 -0300 Subject: [PATCH 0563/1145] Avoid calling 'fprintf' with NULL MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Avoid undefined behavior in calls like «fprintf("%s", NULL)». ('lua_writestringerror' is implemented as 'fprintf', and 'lua_tostring' can return NULL if object is not a string.) --- lauxlib.c | 4 +++- ldblib.c | 2 +- ltests.c | 4 +++- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/lauxlib.c b/lauxlib.c index e6d741688d..e3d9be37f0 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -995,8 +995,10 @@ static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) { static int panic (lua_State *L) { + const char *msg = lua_tostring(L, -1); + if (msg == NULL) msg = "error object is not a string"; lua_writestringerror("PANIC: unprotected error in call to Lua API (%s)\n", - lua_tostring(L, -1)); + msg); return 0; /* return to Lua to abort */ } diff --git a/ldblib.c b/ldblib.c index 745cfd279f..59eb8f0ea7 100644 --- a/ldblib.c +++ b/ldblib.c @@ -417,7 +417,7 @@ static int db_debug (lua_State *L) { return 0; if (luaL_loadbuffer(L, buffer, strlen(buffer), "=(debug command)") || lua_pcall(L, 0, 0, 0)) - lua_writestringerror("%s\n", lua_tostring(L, -1)); + lua_writestringerror("%s\n", luaL_tolstring(L, -1, NULL)); lua_settop(L, 0); /* remove eventual returns */ } } diff --git a/ltests.c b/ltests.c index 7e6d86104f..314505c346 100644 --- a/ltests.c +++ b/ltests.c @@ -73,8 +73,10 @@ static void badexit (const char *fmt, const char *s1, const char *s2) { static int tpanic (lua_State *L) { + const char *msg = lua_tostring(L, -1); + if (msg == NULL) msg = "error object is not a string"; return (badexit("PANIC: unprotected error in call to Lua API (%s)\n", - lua_tostring(L, -1), NULL), + msg, NULL), 0); /* do not return to Lua */ } From 69e84805e48b0253007bd0daf481ce7955367d73 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 10 Jun 2020 16:39:37 -0300 Subject: [PATCH 0564/1145] Details --- makefile | 5 ++--- manual/manual.of | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/makefile b/makefile index 2c68f45473..9be2392526 100644 --- a/makefile +++ b/makefile @@ -1,6 +1,5 @@ -# makefile for building Lua -# see INSTALL for installation instructions -# see ../Makefile and luaconf.h for further customization +# Developer's makefile for building Lua +# see luaconf.h for further customization # == CHANGE THE SETTINGS BELOW TO SUIT YOUR ENVIRONMENT ======================= diff --git a/manual/manual.of b/manual/manual.of index 2eadbda07e..4d1794fc9e 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -5780,7 +5780,7 @@ it to the buffer. @APIEntry{void luaL_pushfail (lua_State *L);| @apii{0,1,-} -Pushes the @fail value onto the stack. +Pushes the @fail value onto the stack @see{libraries}. } From d49b2887282b86a5e6f40a386511aa8040f3c7b0 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 15 Jun 2020 11:58:59 -0300 Subject: [PATCH 0565/1145] 'luaE_shrinkCI' shouldn't remove first free CallInfo Due to emergency collections, 'luaE_shrinkCI' can be called while Lua is building a new CallInfo, which for a while is still a free CallInfo. --- lstate.c | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/lstate.c b/lstate.c index 42a48436a8..d2e924d5c5 100644 --- a/lstate.c +++ b/lstate.c @@ -186,20 +186,26 @@ void luaE_freeCI (lua_State *L) { /* -** free half of the CallInfo structures not in use by a thread +** free half of the CallInfo structures not in use by a thread, +** keeping the first one. */ void luaE_shrinkCI (lua_State *L) { - CallInfo *ci = L->ci; + CallInfo *ci = L->ci->next; /* first free CallInfo */ CallInfo *next; - CallInfo *next2; /* next's next */ + if (ci == NULL) + return; /* no extra elements */ L->nCcalls += L->nci; /* add removed elements back to 'nCcalls' */ - /* while there are two nexts */ - while ((next = ci->next) != NULL && (next2 = next->next) != NULL) { + while ((next = ci->next) != NULL) { /* two extra elements? */ + CallInfo *next2 = next->next; /* next's next */ ci->next = next2; /* remove next from the list */ - next2->previous = ci; - luaM_free(L, next); /* free next */ L->nci--; - ci = next2; /* keep next's next */ + luaM_free(L, next); /* free next */ + if (next2 == NULL) + break; /* no more elements */ + else { + next2->previous = ci; + ci = next2; /* continue */ + } } L->nCcalls -= L->nci; /* adjust result */ } From 993c58fde3a85c27f52f094002ec57dabca81028 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 15 Jun 2020 12:01:36 -0300 Subject: [PATCH 0566/1145] In 'lua_checkmemory', userdata can be gray, too Since commit ca6fe7449a74, userdata with uservalues can be gray and can belong to gray lists ('gclist'). --- ltests.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/ltests.c b/ltests.c index 314505c346..63ad449875 100644 --- a/ltests.c +++ b/ltests.c @@ -519,6 +519,10 @@ static void checkgraylist (global_State *g, GCObject *o) { case LUA_VCCL: o = gco2ccl(o)->gclist; break; case LUA_VTHREAD: o = gco2th(o)->gclist; break; case LUA_VPROTO: o = gco2p(o)->gclist; break; + case LUA_VUSERDATA: + lua_assert(gco2u(o)->nuvalue > 0); + o = gco2u(o)->gclist; + break; default: lua_assert(0); /* other objects cannot be in a gray list */ } } From 6d7cd31feec58011a593cf732274a33dcc1bcb53 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 16 Jun 2020 09:54:20 -0300 Subject: [PATCH 0567/1145] Fixed missing GC barriers in compiler and undump While building a new prototype, the GC needs barriers for every object (strings and nested prototypes) that is attached to the new prototype. --- lparser.c | 3 +++ lundump.c | 33 +++++++++++++++++++-------------- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/lparser.c b/lparser.c index 37102b7213..bc7d9a4f2d 100644 --- a/lparser.c +++ b/lparser.c @@ -737,6 +737,7 @@ static void open_func (LexState *ls, FuncState *fs, BlockCnt *bl) { fs->firstlabel = ls->dyd->label.n; fs->bl = NULL; f->source = ls->source; + luaC_objbarrier(ls->L, f, f->source); f->maxstacksize = 2; /* registers 0/1 are always valid */ enterblock(fs, bl, 0); } @@ -1959,6 +1960,7 @@ static void mainfunc (LexState *ls, FuncState *fs) { env->idx = 0; env->kind = VDKREG; env->name = ls->envn; + luaC_objbarrier(ls->L, fs->f, env->name); luaX_next(ls); /* read first token */ statlist(ls); /* parse main body */ check(ls, TK_EOS); @@ -1977,6 +1979,7 @@ LClosure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, sethvalue2s(L, L->top, lexstate.h); /* anchor it */ luaD_inctop(L); funcstate.f = cl->p = luaF_newproto(L); + luaC_objbarrier(L, cl, cl->p); funcstate.f->source = luaS_new(L, name); /* create and anchor TString */ luaC_objbarrier(L, funcstate.f, funcstate.f->source); lexstate.buff = buff; diff --git a/lundump.c b/lundump.c index d6b249d58c..77ba1955f2 100644 --- a/lundump.c +++ b/lundump.c @@ -105,30 +105,33 @@ static lua_Integer loadInteger (LoadState *S) { /* -** Load a nullable string. +** Load a nullable string into prototype 'p'. */ -static TString *loadStringN (LoadState *S) { +static TString *loadStringN (LoadState *S, Proto *p) { + lua_State *L = S->L; + TString *ts; size_t size = loadSize(S); - if (size == 0) + if (size == 0) /* no string? */ return NULL; else if (--size <= LUAI_MAXSHORTLEN) { /* short string? */ char buff[LUAI_MAXSHORTLEN]; - loadVector(S, buff, size); - return luaS_newlstr(S->L, buff, size); + loadVector(S, buff, size); /* load string into buffer */ + ts = luaS_newlstr(L, buff, size); /* create string */ } else { /* long string */ - TString *ts = luaS_createlngstrobj(S->L, size); + ts = luaS_createlngstrobj(L, size); /* create string */ loadVector(S, getstr(ts), size); /* load directly in final place */ - return ts; } + luaC_objbarrier(L, p, ts); + return ts; } /* -** Load a non-nullable string. +** Load a non-nullable string into prototype 'p'. */ -static TString *loadString (LoadState *S) { - TString *st = loadStringN(S); +static TString *loadString (LoadState *S, Proto *p) { + TString *st = loadStringN(S, p); if (st == NULL) error(S, "bad format for constant string"); return st; @@ -174,7 +177,7 @@ static void loadConstants (LoadState *S, Proto *f) { break; case LUA_VSHRSTR: case LUA_VLNGSTR: - setsvalue2n(S->L, o, loadString(S)); + setsvalue2n(S->L, o, loadString(S, f)); break; default: lua_assert(0); } @@ -191,6 +194,7 @@ static void loadProtos (LoadState *S, Proto *f) { f->p[i] = NULL; for (i = 0; i < n; i++) { f->p[i] = luaF_newproto(S->L); + luaC_objbarrier(S->L, f, f->p[i]); loadFunction(S, f->p[i], f->source); } } @@ -229,18 +233,18 @@ static void loadDebug (LoadState *S, Proto *f) { for (i = 0; i < n; i++) f->locvars[i].varname = NULL; for (i = 0; i < n; i++) { - f->locvars[i].varname = loadStringN(S); + f->locvars[i].varname = loadStringN(S, f); f->locvars[i].startpc = loadInt(S); f->locvars[i].endpc = loadInt(S); } n = loadInt(S); for (i = 0; i < n; i++) - f->upvalues[i].name = loadStringN(S); + f->upvalues[i].name = loadStringN(S, f); } static void loadFunction (LoadState *S, Proto *f, TString *psource) { - f->source = loadStringN(S); + f->source = loadStringN(S, f); if (f->source == NULL) /* no source in dump? */ f->source = psource; /* reuse parent's source */ f->linedefined = loadInt(S); @@ -310,6 +314,7 @@ LClosure *luaU_undump(lua_State *L, ZIO *Z, const char *name) { setclLvalue2s(L, L->top, cl); luaD_inctop(L); cl->p = luaF_newproto(L); + luaC_objbarrier(L, cl, cl->p); loadFunction(&S, cl->p, NULL); lua_assert(cl->nupvalues == cl->p->sizeupvalues); luai_verifycode(L, buff, cl->p); From a304199836ef37af6912a1da6f9b6cad33466a84 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 17 Jun 2020 10:36:42 -0300 Subject: [PATCH 0568/1145] Detail in 'lua_resetthread' 'lua_resetthread' should reset the CallInfo list before calling 'luaF_close'. luaF_close can call functions, and those functions should not run with dead functions still in the CallInfo list. --- lstate.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/lstate.c b/lstate.c index d2e924d5c5..4434211a96 100644 --- a/lstate.c +++ b/lstate.c @@ -362,19 +362,18 @@ int lua_resetthread (lua_State *L) { CallInfo *ci; int status; lua_lock(L); - ci = &L->base_ci; - status = luaF_close(L, L->stack, CLOSEPROTECT); + L->ci = ci = &L->base_ci; /* unwind CallInfo list */ setnilvalue(s2v(L->stack)); /* 'function' entry for basic 'ci' */ + ci->func = L->stack; + ci->callstatus = CIST_C; + status = luaF_close(L, L->stack, CLOSEPROTECT); if (status != CLOSEPROTECT) /* real errors? */ luaD_seterrorobj(L, status, L->stack + 1); else { status = LUA_OK; L->top = L->stack + 1; } - ci->callstatus = CIST_C; - ci->func = L->stack; ci->top = L->top + LUA_MINSTACK; - L->ci = ci; L->status = status; lua_unlock(L); return status; From c33b1728aeb7dfeec4013562660e07d32697aa6b Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 18 Jun 2020 11:07:27 -0300 Subject: [PATCH 0569/1145] Details Added as incompatibility, in the manual, the extra return of 'io.lines'. --- lundump.c | 4 ++-- manual/manual.of | 11 +++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/lundump.c b/lundump.c index 77ba1955f2..4243678a72 100644 --- a/lundump.c +++ b/lundump.c @@ -26,7 +26,7 @@ #if !defined(luai_verifycode) -#define luai_verifycode(L,b,f) /* empty */ +#define luai_verifycode(L,f) /* empty */ #endif @@ -317,7 +317,7 @@ LClosure *luaU_undump(lua_State *L, ZIO *Z, const char *name) { luaC_objbarrier(L, cl, cl->p); loadFunction(&S, cl->p, NULL); lua_assert(cl->nupvalues == cl->p->sizeupvalues); - luai_verifycode(L, buff, cl->p); + luai_verifycode(L, cl->p); return cl; } diff --git a/manual/manual.of b/manual/manual.of index 4d1794fc9e..9c275d1501 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -9116,6 +9116,17 @@ of the function @Lid{collectgarbage} are deprecated. You should use the new option @St{incremental} to set them. } +@item{ +The function @Lid{io.lines} now returns four values, +instead of just one. +That can be a problem when it is used as the sole +argument to another function that has optional parameters, +such as in @T{load(io.lines(filename, "L"))}. +To fix that issue, +you can wrap the call into parentheses, +to adjust its number of results to one. +} + } } From 422ce50d2e8856ed789d1359c673122dbb0088ea Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 30 Jun 2020 15:36:26 -0300 Subject: [PATCH 0570/1145] Fixed detail in 'loadUpvalues' In 'lundump.c', when loading the upvalues of a function, there can be a read error if the chunk is truncated. In that case, the creation of the error message can trigger an emergency collection while the prototype is still anchored. So, the prototype must be GC consistent before loading the upvales, which implies that it the 'name' fields must be filled with NULL before the reading. --- lapi.c | 1 + lundump.c | 9 ++++++++- testes/calls.lua | 21 +++++++++++++++------ 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/lapi.c b/lapi.c index 3e24781e0e..184b8dd7c4 100644 --- a/lapi.c +++ b/lapi.c @@ -563,6 +563,7 @@ LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) { while (n--) { setobj2n(L, &cl->upvalue[n], s2v(L->top + n)); /* does not need barrier because closure is white */ + lua_assert(iswhite(cl)); } setclCvalue(L, s2v(L->top), cl); api_incr_top(L); diff --git a/lundump.c b/lundump.c index 4243678a72..cb124d6fa3 100644 --- a/lundump.c +++ b/lundump.c @@ -200,13 +200,20 @@ static void loadProtos (LoadState *S, Proto *f) { } +/* +** Load the upvalues for a function. The names must be filled first, +** because the filling of the other fields can raise read errors and +** the creation of the error message can call an emergency collection; +** in that case all prototypes must be consistent for the GC. +*/ static void loadUpvalues (LoadState *S, Proto *f) { int i, n; n = loadInt(S); f->upvalues = luaM_newvectorchecked(S->L, n, Upvaldesc); f->sizeupvalues = n; - for (i = 0; i < n; i++) { + for (i = 0; i < n; i++) /* make array valid for GC */ f->upvalues[i].name = NULL; + for (i = 0; i < n; i++) { /* following calls can raise errors */ f->upvalues[i].instack = loadByte(S); f->upvalues[i].idx = loadByte(S); f->upvalues[i].kind = loadByte(S); diff --git a/testes/calls.lua b/testes/calls.lua index 1701f155f6..decf41760d 100644 --- a/testes/calls.lua +++ b/testes/calls.lua @@ -422,20 +422,30 @@ assert((function (a) return a end)() == nil) print("testing binary chunks") do - local header = string.pack("c4BBc6BBBj", + local header = string.pack("c4BBc6BBB", "\27Lua", -- signature 0x54, -- version 5.4 (0x54) 0, -- format "\x19\x93\r\n\x1a\n", -- data 4, -- size of instruction string.packsize("j"), -- sizeof(lua integer) - string.packsize("n"), -- sizeof(lua number) - 0x5678 -- LUAC_INT - -- LUAC_NUM may not have a unique binary representation (padding...) + string.packsize("n") -- sizeof(lua number) ) - local c = string.dump(function () local a = 1; local b = 3; return a+b*3 end) + local c = string.dump(function () + local a = 1; local b = 3; + local f = function () return a + b + _ENV.c; end -- upvalues + local s1 = "a constant" + local s2 = "another constant" + return a + b * 3 + end) + assert(assert(load(c))() == 10) + + -- check header assert(string.sub(c, 1, #header) == header) + -- check LUAC_INT and LUAC_NUM + local ci, cn = string.unpack("jn", c, #header + 1) + assert(ci == 0x5678 and cn == 370.5) -- corrupted header for i = 1, #header do @@ -451,7 +461,6 @@ do local st, msg = load(string.sub(c, 1, i)) assert(not st and string.find(msg, "truncated")) end - assert(assert(load(c))() == 10) end print('OK') From 56a165bf0f061f7aff744fafd44691a2beb4b035 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 2 Jul 2020 16:55:23 -0300 Subject: [PATCH 0571/1145] Added '.gitignore' to the repository --- .gitignore | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000..735661eaaf --- /dev/null +++ b/.gitignore @@ -0,0 +1,12 @@ +.gitattributes + +*.so +*.o +*.a + +manual/manual.html + +testes/time.txt +testes/time-debug.txt + +testes/libs/all From e96385adede47a1abf160a41565ec742d3d4e413 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 3 Jul 2020 11:36:56 -0300 Subject: [PATCH 0572/1145] Simplification and smaller buffers for 'lua_pushfstring' The function 'lua_pushfstring' is seldom called with large strings, there is no need to optimize too much for that cases. --- lobject.c | 26 ++++++++++++++++---------- testes/strings.lua | 2 +- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/lobject.c b/lobject.c index b4efae4f33..2a28ebd4f0 100644 --- a/lobject.c +++ b/lobject.c @@ -215,7 +215,7 @@ static lua_Number lua_strx2number (const char *s, char **endptr) { /* }====================================================== */ -/* maximum length of a numeral */ +/* maximum length of a numeral to be converted to a number */ #if !defined (L_MAXLENNUM) #define L_MAXLENNUM 200 #endif @@ -333,8 +333,15 @@ int luaO_utf8esc (char *buff, unsigned long x) { } -/* maximum length of the conversion of a number to a string */ -#define MAXNUMBER2STR 50 +/* +** Maximum length of the conversion of a number to a string. Must be +** enough to accommodate both LUA_INTEGER_FMT and LUA_NUMBER_FMT. +** (For a long long int, this is 19 digits plus a sign and a final '\0', +** adding to 21. For a long double, it can go to a sign, 33 digits, +** the dot, an exponent letter, an exponent sign, 5 exponent digits, +** and a final '\0', adding to 43.) +*/ +#define MAXNUMBER2STR 44 /* @@ -375,7 +382,7 @@ void luaO_tostring (lua_State *L, TValue *obj) { */ /* size for buffer space used by 'luaO_pushvfstring' */ -#define BUFVFS 400 +#define BUFVFS 200 /* buffer used by 'luaO_pushvfstring' */ typedef struct BuffFS { @@ -387,16 +394,16 @@ typedef struct BuffFS { /* -** Push given string to the stack, as part of the buffer. If the stack -** is almost full, join all partial strings in the stack into one. +** Push given string to the stack, as part of the buffer, and +** join the partial strings in the stack into one. */ static void pushstr (BuffFS *buff, const char *str, size_t l) { lua_State *L = buff->L; setsvalue2s(L, L->top, luaS_newlstr(L, str, l)); L->top++; /* may use one extra slot */ buff->pushed++; - if (buff->pushed > 1 && L->top + 1 >= L->stack_last) { - luaV_concat(L, buff->pushed); /* join all partial results into one */ + if (buff->pushed > 1) { + luaV_concat(L, buff->pushed); /* join partial results into one */ buff->pushed = 1; } } @@ -521,8 +528,7 @@ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) { } addstr2buff(&buff, fmt, strlen(fmt)); /* rest of 'fmt' */ clearbuff(&buff); /* empty buffer into the stack */ - if (buff.pushed > 1) - luaV_concat(L, buff.pushed); /* join all partial results */ + lua_assert(buff.pushed == 1); return svalue(s2v(L->top - 1)); } diff --git a/testes/strings.lua b/testes/strings.lua index 4a10857e75..2fa4a89ff2 100644 --- a/testes/strings.lua +++ b/testes/strings.lua @@ -438,7 +438,7 @@ else -- formats %U, %f, %I already tested elsewhere - local blen = 400 -- internal buffer length in 'luaO_pushfstring' + local blen = 200 -- internal buffer length in 'luaO_pushfstring' local function callpfs (op, fmt, n) local x = {T.testC("pushfstring" .. op .. "; return *", fmt, n)} From ae809e9fd132ab867741a6a777450f9bc0d49be4 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 3 Jul 2020 11:54:58 -0300 Subject: [PATCH 0573/1145] 'luaV_concat' can "concat" one single value Several of its callers needed that case and had to do the check themselves. --- lapi.c | 8 +++----- lobject.c | 6 ++---- lvm.c | 9 ++++----- 3 files changed, 9 insertions(+), 14 deletions(-) diff --git a/lapi.c b/lapi.c index 184b8dd7c4..bbba88a3f4 100644 --- a/lapi.c +++ b/lapi.c @@ -1239,14 +1239,12 @@ LUA_API void lua_toclose (lua_State *L, int idx) { LUA_API void lua_concat (lua_State *L, int n) { lua_lock(L); api_checknelems(L, n); - if (n >= 2) { + if (n > 0) luaV_concat(L, n); - } - else if (n == 0) { /* push empty string */ - setsvalue2s(L, L->top, luaS_newlstr(L, "", 0)); + else { /* nothing to concatenate */ + setsvalue2s(L, L->top, luaS_newlstr(L, "", 0)); /* push empty string */ api_incr_top(L); } - /* else n == 1; nothing to do */ luaC_checkGC(L); lua_unlock(L); } diff --git a/lobject.c b/lobject.c index 2a28ebd4f0..223bbd0c6c 100644 --- a/lobject.c +++ b/lobject.c @@ -402,10 +402,8 @@ static void pushstr (BuffFS *buff, const char *str, size_t l) { setsvalue2s(L, L->top, luaS_newlstr(L, str, l)); L->top++; /* may use one extra slot */ buff->pushed++; - if (buff->pushed > 1) { - luaV_concat(L, buff->pushed); /* join partial results into one */ - buff->pushed = 1; - } + luaV_concat(L, buff->pushed); /* join partial results into one */ + buff->pushed = 1; } diff --git a/lvm.c b/lvm.c index e7781dbf25..ccbfbab501 100644 --- a/lvm.c +++ b/lvm.c @@ -634,7 +634,8 @@ static void copy2buff (StkId top, int n, char *buff) { ** from 'L->top - total' up to 'L->top - 1'. */ void luaV_concat (lua_State *L, int total) { - lua_assert(total >= 2); + if (total == 1) + return; /* "all" values already concatenated */ do { StkId top = L->top; int n = 2; /* number of elements handled in this pass (at least 2) */ @@ -840,10 +841,8 @@ void luaV_finishOp (lua_State *L) { int a = GETARG_A(inst); /* first element to concatenate */ int total = cast_int(top - 1 - (base + a)); /* yet to concatenate */ setobjs2s(L, top - 2, top); /* put TM result in proper position */ - if (total > 1) { /* are there elements to concat? */ - L->top = top - 1; /* top is one after last element (at top-2) */ - luaV_concat(L, total); /* concat them (may yield again) */ - } + L->top = top - 1; /* top is one after last element (at top-2) */ + luaV_concat(L, total); /* concat them (may yield again) */ break; } default: { From 0280407fc54f9b6225139c5ac27326f98f0cf043 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 3 Jul 2020 13:02:41 -0300 Subject: [PATCH 0574/1145] Details Comments in makefile and function 'l_str2d'. --- lobject.c | 21 +++++++++++++-------- makefile | 19 ++++++++++--------- 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/lobject.c b/lobject.c index 223bbd0c6c..f8ea917a85 100644 --- a/lobject.c +++ b/lobject.c @@ -220,32 +220,37 @@ static lua_Number lua_strx2number (const char *s, char **endptr) { #define L_MAXLENNUM 200 #endif +/* +** Convert string 's' to a Lua number (put in 'result'). Return NULL on +** fail or the address of the ending '\0' on success. ('mode' == 'x') +** means a hexadecimal numeral. +*/ static const char *l_str2dloc (const char *s, lua_Number *result, int mode) { char *endptr; *result = (mode == 'x') ? lua_strx2number(s, &endptr) /* try to convert */ : lua_str2number(s, &endptr); if (endptr == s) return NULL; /* nothing recognized? */ while (lisspace(cast_uchar(*endptr))) endptr++; /* skip trailing spaces */ - return (*endptr == '\0') ? endptr : NULL; /* OK if no trailing characters */ + return (*endptr == '\0') ? endptr : NULL; /* OK iff no trailing chars */ } /* -** Convert string 's' to a Lua number (put in 'result'). Return NULL -** on fail or the address of the ending '\0' on success. -** 'pmode' points to (and 'mode' contains) special things in the string: -** - 'x'/'X' means a hexadecimal numeral -** - 'n'/'N' means 'inf' or 'nan' (which should be rejected) -** - '.' just optimizes the search for the common case (nothing special) +** Convert string 's' to a Lua number (put in 'result') handling the +** current locale. ** This function accepts both the current locale or a dot as the radix ** mark. If the conversion fails, it may mean number has a dot but ** locale accepts something else. In that case, the code copies 's' ** to a buffer (because 's' is read-only), changes the dot to the ** current locale radix mark, and tries to convert again. +** The variable 'mode' checks for special characters in the string: +** - 'n' means 'inf' or 'nan' (which should be rejected) +** - 'x' means a hexadecimal numeral +** - '.' just optimizes the search for the common case (no special chars) */ static const char *l_str2d (const char *s, lua_Number *result) { const char *endptr; - const char *pmode = strpbrk(s, ".xXnN"); + const char *pmode = strpbrk(s, ".xXnN"); /* look for special chars */ int mode = pmode ? ltolower(cast_uchar(*pmode)) : 0; if (mode == 'n') /* reject 'inf' and 'nan' */ return NULL; diff --git a/makefile b/makefile index 9be2392526..baa6e673b4 100644 --- a/makefile +++ b/makefile @@ -37,10 +37,15 @@ CWARNSC= -Wdeclaration-after-statement \ CWARNS= $(CWARNSCPP) $(CWARNSC) +# Some useful compiler options for internal tests: +# -DHARDSTACKTESTS forces a reallocation of the stack at every point where +# the stack can be reallocated. +# -DHARDMEMTESTS forces an emergency collection at every single allocation. +# -DEXTERNMEMCHECK removes internal consistency checking of blocks being +# deallocated (useful when an external tool like valgrind does the check). +# -DMAXINDEXRK=k limits range of constants in RK instruction operands. +# -DLUA_COMPAT_5_3 -# -DEXTERNMEMCHECK -DHARDSTACKTESTS -DHARDMEMTESTS -DTRACEMEM='"tempmem"' -# -DMAXINDEXRK=1 -DLUA_COMPAT_5_3 -# -g -DLUA_USER_H='"ltests.h"' # -pg -malign-double # -DLUA_USE_CTYPE -DLUA_USE_APICHECK # ('-ftrapv' for runtime checks of integer overflows) @@ -81,11 +86,9 @@ LIB_O= lbaselib.o ldblib.o liolib.o lmathlib.o loslib.o ltablib.o lstrlib.o \ LUA_T= lua LUA_O= lua.o -# LUAC_T= luac -# LUAC_O= luac.o print.o -ALL_T= $(CORE_T) $(LUA_T) $(LUAC_T) -ALL_O= $(CORE_O) $(LUA_O) $(LUAC_O) $(AUX_O) $(LIB_O) +ALL_T= $(CORE_T) $(LUA_T) +ALL_O= $(CORE_O) $(LUA_O) $(AUX_O) $(LIB_O) ALL_A= $(CORE_T) all: $(ALL_T) @@ -102,8 +105,6 @@ $(CORE_T): $(CORE_O) $(AUX_O) $(LIB_O) $(LUA_T): $(LUA_O) $(CORE_T) $(CC) -o $@ $(MYLDFLAGS) $(LUA_O) $(CORE_T) $(LIBS) $(MYLIBS) $(DL) -$(LUAC_T): $(LUAC_O) $(CORE_T) - $(CC) -o $@ $(MYLDFLAGS) $(LUAC_O) $(CORE_T) $(LIBS) $(MYLIBS) llex.o: $(CC) $(CFLAGS) -Os -c llex.c From bfcf06d91a87b7ffb8c83e290db0cb6176a167f8 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Sat, 4 Jul 2020 16:40:18 -0300 Subject: [PATCH 0575/1145] Avoid memory allocation in some functions from 'ltests.c' To allow their use in memory tests, some functions in 'ltests.c' should never allocate memory. To avoid this allocation, the library registers the strings used for status codes, and keeps the variable '_WARN' always defined (with false instead of nil). --- ltests.c | 27 ++++++++++++++++++++------- testes/coroutine.lua | 2 +- testes/gc.lua | 8 ++++---- testes/locals.lua | 4 ++-- testes/main.lua | 6 +++--- 5 files changed, 30 insertions(+), 17 deletions(-) diff --git a/ltests.c b/ltests.c index 63ad449875..164b5a2591 100644 --- a/ltests.c +++ b/ltests.c @@ -121,7 +121,8 @@ static void warnf (void *ud, const char *msg, int tocont) { strcat(buff, msg); /* add new message to current warning */ if (!tocont) { /* message finished? */ lua_unlock(L); - if (lua_getglobal(L, "_WARN") == LUA_TNIL) + lua_getglobal(L, "_WARN"); + if (!lua_toboolean(L, -1)) lua_pop(L, 1); /* ok, no previous unexpected warning */ else { badexit("Unhandled warning in store mode: %s\naborting...\n", @@ -1282,10 +1283,19 @@ static int getindex_aux (lua_State *L, lua_State *L1, const char **pc) { } -static void pushcode (lua_State *L, int code) { - static const char *const codes[] = {"OK", "YIELD", "ERRRUN", - "ERRSYNTAX", MEMERRMSG, "ERRGCMM", "ERRERR"}; - lua_pushstring(L, codes[code]); +static const char *const statcodes[] = {"OK", "YIELD", "ERRRUN", + "ERRSYNTAX", MEMERRMSG, "ERRGCMM", "ERRERR"}; + +/* +** Avoid these stat codes from being collected, to avoid possible +** memory error when pushing them. +*/ +static void regcodes (lua_State *L) { + unsigned int i; + for (i = 0; i < sizeof(statcodes) / sizeof(statcodes[0]); i++) { + lua_pushboolean(L, 1); + lua_setfield(L, LUA_REGISTRYINDEX, statcodes[i]); + } } @@ -1508,7 +1518,7 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) { lua_pushnumber(L1, (lua_Number)getnum); } else if EQ("pushstatus") { - pushcode(L1, status); + lua_pushstring(L1, statcodes[status]); } else if EQ("pushstring") { lua_pushstring(L1, getstring); @@ -1710,7 +1720,7 @@ static int Cfunc (lua_State *L) { static int Cfunck (lua_State *L, int status, lua_KContext ctx) { - pushcode(L, status); + lua_pushstring(L, statcodes[status]); lua_setglobal(L, "status"); lua_pushinteger(L, ctx); lua_setglobal(L, "ctx"); @@ -1865,6 +1875,9 @@ int luaB_opentests (lua_State *L) { void *ud; lua_atpanic(L, &tpanic); lua_setwarnf(L, &warnf, L); + lua_pushboolean(L, 0); + lua_setglobal(L, "_WARN"); /* _WARN = false */ + regcodes(L); atexit(checkfinalmem); lua_assert(lua_getallocf(L, &ud) == debug_realloc); lua_assert(ud == cast_voidp(&l_memcontrol)); diff --git a/testes/coroutine.lua b/testes/coroutine.lua index 73333c14d2..0a4c2ef354 100644 --- a/testes/coroutine.lua +++ b/testes/coroutine.lua @@ -184,7 +184,7 @@ do if not T then warn("@on") else -- test library - assert(string.find(_WARN, "200")); _WARN = nil + assert(string.find(_WARN, "200")); _WARN = false warn("@normal") end assert(st == false and coroutine.status(co) == "dead" and msg == 111) diff --git a/testes/gc.lua b/testes/gc.lua index 91915c0b81..80850f9252 100644 --- a/testes/gc.lua +++ b/testes/gc.lua @@ -372,7 +372,7 @@ if T then warn("@on"); warn("@store") collectgarbage() assert(string.find(_WARN, "error in __gc metamethod")) - assert(string.match(_WARN, "@(.-)@") == "expected"); _WARN = nil + assert(string.match(_WARN, "@(.-)@") == "expected"); _WARN = false for i = 8, 10 do assert(s[i]) end for i = 1, 5 do @@ -481,7 +481,7 @@ if T then u = setmetatable({}, {__gc = function () error "@expected error" end}) u = nil collectgarbage() - assert(string.find(_WARN, "@expected error")); _WARN = nil + assert(string.find(_WARN, "@expected error")); _WARN = false warn("@normal") end @@ -657,14 +657,14 @@ if T then n = n + 1 assert(n == o[1]) if n == 1 then - _WARN = nil + _WARN = false elseif n == 2 then assert(find(_WARN, "@expected warning")) lastmsg = _WARN -- get message from previous error (first 'o') else assert(lastmsg == _WARN) -- subsequent error messages are equal end - warn("@store"); _WARN = nil + warn("@store"); _WARN = false error"@expected warning" end} for i = 10, 1, -1 do diff --git a/testes/locals.lua b/testes/locals.lua index 0e5e0c743c..f5e962447c 100644 --- a/testes/locals.lua +++ b/testes/locals.lua @@ -337,7 +337,7 @@ local function endwarn () if not T then warn("@on") -- back to normal else - assert(_WARN == nil) + assert(_WARN == false) warn("@normal") end end @@ -346,7 +346,7 @@ end local function checkwarn (msg) if T then assert(string.find(_WARN, msg)) - _WARN = nil -- reset variable to check next warning + _WARN = false -- reset variable to check next warning end end diff --git a/testes/main.lua b/testes/main.lua index de14a08891..d2d602de5d 100644 --- a/testes/main.lua +++ b/testes/main.lua @@ -393,12 +393,12 @@ if T then -- test library? -- testing 'warn' warn("@store") warn("@123", "456", "789") - assert(_WARN == "@123456789"); _WARN = nil + assert(_WARN == "@123456789"); _WARN = false warn("zip", "", " ", "zap") - assert(_WARN == "zip zap"); _WARN = nil + assert(_WARN == "zip zap"); _WARN = false warn("ZIP", "", " ", "ZAP") - assert(_WARN == "ZIP ZAP"); _WARN = nil + assert(_WARN == "ZIP ZAP"); _WARN = false warn("@normal") end From b57574d6fb9071b2f8f261b32c9378ed72db7023 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 6 Jul 2020 12:09:44 -0300 Subject: [PATCH 0576/1145] Keep memory errors as memory errors Allow memory errors to be raised through the API (throwing the error with the memory error message); error in external allocations raises a memory error; memory errors in coroutines are re-raised as memory errors. --- lapi.c | 8 +++++- lauxlib.c | 6 ++-- lcorolib.c | 9 +++--- testes/api.lua | 75 +++++++++++++++++++++++++++++++++++++++----------- 4 files changed, 75 insertions(+), 23 deletions(-) diff --git a/lapi.c b/lapi.c index bbba88a3f4..16eb170d81 100644 --- a/lapi.c +++ b/lapi.c @@ -1195,9 +1195,15 @@ LUA_API int lua_gc (lua_State *L, int what, ...) { LUA_API int lua_error (lua_State *L) { + TValue *errobj; lua_lock(L); + errobj = s2v(L->top - 1); api_checknelems(L, 1); - luaG_errormsg(L); + /* error object is the memory error message? */ + if (ttisshrstring(errobj) && eqshrstr(tsvalue(errobj), G(L)->memerrmsg)) + luaM_error(L); /* raise a memory error */ + else + luaG_errormsg(L); /* raise a regular error */ /* code unreachable; will unlock when control actually leaves the kernel */ return 0; /* to avoid warnings */ } diff --git a/lauxlib.c b/lauxlib.c index e3d9be37f0..cbe9ed31c3 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -475,8 +475,10 @@ static void *resizebox (lua_State *L, int idx, size_t newsize) { lua_Alloc allocf = lua_getallocf(L, &ud); UBox *box = (UBox *)lua_touserdata(L, idx); void *temp = allocf(ud, box->box, box->bsize, newsize); - if (temp == NULL && newsize > 0) /* allocation error? */ - luaL_error(L, "not enough memory"); + if (temp == NULL && newsize > 0) { /* allocation error? */ + lua_pushliteral(L, "not enough memory"); + lua_error(L); /* raise a memory error */ + } box->box = temp; box->bsize = newsize; return temp; diff --git a/lcorolib.c b/lcorolib.c index 7d6e585b1d..c165031d28 100644 --- a/lcorolib.c +++ b/lcorolib.c @@ -73,11 +73,12 @@ static int luaB_coresume (lua_State *L) { static int luaB_auxwrap (lua_State *L) { lua_State *co = lua_tothread(L, lua_upvalueindex(1)); int r = auxresume(L, co, lua_gettop(L)); - if (r < 0) { + if (r < 0) { /* error? */ int stat = lua_status(co); - if (stat != LUA_OK && stat != LUA_YIELD) - lua_resetthread(co); /* close variables in case of errors */ - if (lua_type(L, -1) == LUA_TSTRING) { /* error object is a string? */ + if (stat != LUA_OK && stat != LUA_YIELD) /* error in the coroutine? */ + lua_resetthread(co); /* close its tbc variables */ + if (stat != LUA_ERRMEM && /* not a memory error and ... */ + lua_type(L, -1) == LUA_TSTRING) { /* ... error object is a string? */ luaL_where(L, 1); /* add extra info, if available */ lua_insert(L, -2); lua_concat(L, 2); diff --git a/testes/api.lua b/testes/api.lua index 9447e42aef..95551481da 100644 --- a/testes/api.lua +++ b/testes/api.lua @@ -11,6 +11,9 @@ local debug = require "debug" local pack = table.pack +-- standard error message for memory errors +local MEMERRMSG = "not enough memory" + function tcheck (t1, t2) assert(t1.n == (t2.n or #t2) + 1) for i = 2, t1.n do assert(t1[i] == t2[i - 1]) end @@ -408,7 +411,7 @@ do -- memory error T.totalmem(T.totalmem()+10000) -- set low memory limit (+10k) - assert(T.checkpanic("newuserdata 20000") == "not enough memory") + assert(T.checkpanic("newuserdata 20000") == MEMERRMSG) T.totalmem(0) -- restore high limit -- stack error @@ -1153,40 +1156,74 @@ do end -------------------------------------------------------------------------- --- testing memory limits -------------------------------------------------------------------------- +--[[ +** {================================================================== +** Testing memory limits +** =================================================================== +--]] + print("memory-allocation errors") checkerr("block too big", T.newuserdata, math.maxinteger) collectgarbage() local f = load"local a={}; for i=1,100000 do a[i]=i end" T.alloccount(10) -checkerr("not enough memory", f) +checkerr(MEMERRMSG, f) T.alloccount() -- remove limit + +-- test memory errors; increase limit for maximum memory by steps, +-- o that we get memory errors in all allocations of a given +-- task, until there is enough memory to complete the task without +-- errors. +function testbytes (s, f) + collectgarbage() + local M = T.totalmem() + local oldM = M + local a,b = nil + while true do + collectgarbage(); collectgarbage() + T.totalmem(M) + a, b = T.testC("pcall 0 1 0; pushstatus; return 2", f) + T.totalmem(0) -- remove limit + if a and b == "OK" then break end -- stop when no more errors + if b ~= "OK" and b ~= MEMERRMSG then -- not a memory error? + error(a, 0) -- propagate it + end + M = M + 7 -- increase memory limit + end + print(string.format("minimum memory for %s: %d bytes", s, M - oldM)) + return a +end + -- test memory errors; increase limit for number of allocations one -- by one, so that we get memory errors in all allocations of a given -- task, until there is enough allocations to complete the task without -- errors. -function testamem (s, f) - collectgarbage(); collectgarbage() +function testalloc (s, f) + collectgarbage() local M = 0 local a,b = nil while true do + collectgarbage(); collectgarbage() T.alloccount(M) - a, b = pcall(f) + a, b = T.testC("pcall 0 1 0; pushstatus; return 2", f) T.alloccount() -- remove limit - if a and b then break end -- stop when no more errors - if not a and not -- `real' error? - (string.find(b, "memory") or string.find(b, "overflow")) then - error(b, 0) -- propagate it + if a and b == "OK" then break end -- stop when no more errors + if b ~= "OK" and b ~= MEMERRMSG then -- not a memory error? + error(a, 0) -- propagate it end M = M + 1 -- increase allocation limit end - print(string.format("limit for %s: %d allocations", s, M)) - return b + print(string.format("minimum allocations for %s: %d allocations", s, M)) + return a +end + + +local function testamem (s, f) + testalloc(s, f) + return testbytes(s, f) end @@ -1196,8 +1233,11 @@ assert(b == 10) -- testing memory errors when creating a new state -b = testamem("state creation", T.newstate) -T.closestate(b); -- close new state +testamem("state creation", function () + local st = T.newstate() + if st then T.closestate(st) end -- close new state + return st +end) testamem("empty-table creation", function () return {} @@ -1345,6 +1385,9 @@ testamem("growing stack", function () return foo(100) end) +-- }================================================================== + + do -- testing failing in 'lua_checkstack' local res = T.testC([[rawcheckstack 500000; return 1]]) assert(res == false) From 6298903e35217ab69c279056f925fb72900ce0b7 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 6 Jul 2020 12:11:54 -0300 Subject: [PATCH 0577/1145] Keep minimum size when shrinking a stack When shrinking a stack (during GC), do not make it smaller than the initial stack size. --- ldo.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/ldo.c b/ldo.c index c563b1d9b3..a89ac01049 100644 --- a/ldo.c +++ b/ldo.c @@ -245,13 +245,12 @@ static int stackinuse (lua_State *L) { void luaD_shrinkstack (lua_State *L) { int inuse = stackinuse(L); - int goodsize = inuse + (inuse / 8) + 2*EXTRA_STACK; + int goodsize = inuse + BASIC_STACK_SIZE; if (goodsize > LUAI_MAXSTACK) goodsize = LUAI_MAXSTACK; /* respect stack limit */ /* if thread is currently not handling a stack overflow and its good size is smaller than current size, shrink its stack */ - if (inuse <= (LUAI_MAXSTACK - EXTRA_STACK) && - goodsize < L->stacksize) + if (inuse <= (LUAI_MAXSTACK - EXTRA_STACK) && goodsize < L->stacksize) luaD_reallocstack(L, goodsize, 0); /* ok if that fails */ else /* don't change stack */ condmovestack(L,{},{}); /* (change only for debugging) */ From d39ea8b3ce684728c1ad5005192766d39d2e8baa Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 6 Jul 2020 13:54:01 -0300 Subject: [PATCH 0578/1145] Make sure that main thread is non yieldable Main thread must be non yieldable even at "level 0" (bare API), outside the 'pcall' from 'lua.c'. --- lstate.c | 1 + ltests.c | 7 +++++-- testes/coroutine.lua | 14 ++++++++++++-- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/lstate.c b/lstate.c index 4434211a96..b1f487ffc3 100644 --- a/lstate.c +++ b/lstate.c @@ -395,6 +395,7 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { g->allgc = obj2gco(L); /* by now, only object is the main thread */ L->next = NULL; g->Cstacklimit = L->nCcalls = LUAI_MAXCSTACK + CSTACKERR; + incnny(L); /* main thread is always non yieldable */ g->frealloc = f; g->ud = ud; g->warnf = NULL; diff --git a/ltests.c b/ltests.c index 164b5a2591..0513354c33 100644 --- a/ltests.c +++ b/ltests.c @@ -145,7 +145,6 @@ static void warnf (void *ud, const char *msg, int tocont) { lua_pushstring(L, buff); lua_setglobal(L, "_WARN"); /* assign message to global '_WARN' */ lua_lock(L); - buff[0] = '\0'; /* prepare buffer for next warning */ break; } } @@ -749,11 +748,12 @@ static int listlocals (lua_State *L) { static void printstack (lua_State *L) { int i; int n = lua_gettop(L); + printf("stack: >>\n"); for (i = 1; i <= n; i++) { printf("%3d: %s\n", i, luaL_tolstring(L, i, NULL)); lua_pop(L, 1); } - printf("\n"); + printf("<<\n"); } @@ -1678,6 +1678,9 @@ static struct X { int x; } x; if (n == 0) n = lua_gettop(fs); lua_xmove(fs, ts, n); } + else if EQ("isyieldable") { + lua_pushboolean(L1, lua_isyieldable(lua_tothread(L1, getindex))); + } else if EQ("yield") { return lua_yield(L1, getnum); } diff --git a/testes/coroutine.lua b/testes/coroutine.lua index 0a4c2ef354..955f677652 100644 --- a/testes/coroutine.lua +++ b/testes/coroutine.lua @@ -407,7 +407,8 @@ assert(_G.f() == 12) if not T then - (Message or print)('\n >>> testC not active: skipping yield/hook tests <<<\n') + (Message or print) + ('\n >>> testC not active: skipping coroutine API tests <<<\n') else print "testing yields inside hooks" @@ -564,8 +565,17 @@ else c == "ERRRUN" and d == 4) - -- using a main thread as a coroutine + -- using a main thread as a coroutine (dubious use!) local state = T.newstate() + + -- check that yielddable is working correctly + assert(T.testC(state, "newthread; isyieldable -1; remove 1; return 1")) + + -- main thread is not yieldable + assert(not T.testC(state, "rawgeti R 1; isyieldable -1; remove 1; return 1")) + + T.testC(state, "settop 0") + T.loadlib(state) assert(T.doremote(state, [[ From 314c6057b785cd94ac88905ccfce61724107d66b Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 6 Jul 2020 14:06:47 -0300 Subject: [PATCH 0579/1145] Avoid any code before locks in the API For consistency in the C API, avoid any initializations before callling lua_lock. --- lapi.c | 26 +++++++++++++++++--------- ldo.c | 3 ++- lstate.c | 5 +++-- 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/lapi.c b/lapi.c index 16eb170d81..9048245f36 100644 --- a/lapi.c +++ b/lapi.c @@ -97,8 +97,9 @@ static StkId index2stack (lua_State *L, int idx) { LUA_API int lua_checkstack (lua_State *L, int n) { int res; - CallInfo *ci = L->ci; + CallInfo *ci; lua_lock(L); + ci = L->ci; api_check(L, n >= 0, "negative 'n'"); if (L->stack_last - L->top > n) /* stack large enough? */ res = 1; /* yes; check is OK */ @@ -170,10 +171,12 @@ LUA_API int lua_gettop (lua_State *L) { LUA_API void lua_settop (lua_State *L, int idx) { - CallInfo *ci = L->ci; - StkId func = ci->func; + CallInfo *ci; + StkId func; ptrdiff_t diff; /* difference for new top */ lua_lock(L); + ci = L->ci; + func = ci->func; if (idx >= 0) { api_check(L, idx <= ci->top - (func + 1), "new top too large"); diff = ((func + 1) + idx) - L->top; @@ -376,20 +379,22 @@ LUA_API int lua_toboolean (lua_State *L, int idx) { LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) { - TValue *o = index2value(L, idx); + TValue *o; + lua_lock(L); + o = index2value(L, idx); if (!ttisstring(o)) { if (!cvt2str(o)) { /* not convertible? */ if (len != NULL) *len = 0; + lua_unlock(L); return NULL; } - lua_lock(L); /* 'luaO_tostring' may create a new string */ luaO_tostring(L, o); luaC_checkGC(L); o = index2value(L, idx); /* previous call may reallocate the stack */ - lua_unlock(L); } if (len != NULL) *len = vslen(o); + lua_unlock(L); return svalue(o); } @@ -625,8 +630,9 @@ static int auxgetstr (lua_State *L, const TValue *t, const char *k) { LUA_API int lua_getglobal (lua_State *L, const char *name) { - Table *reg = hvalue(&G(L)->l_registry); + Table *reg; lua_lock(L); + reg = hvalue(&G(L)->l_registry); return auxgetstr(L, luaH_getint(reg, LUA_RIDX_GLOBALS), name); } @@ -805,8 +811,9 @@ static void auxsetstr (lua_State *L, const TValue *t, const char *k) { LUA_API void lua_setglobal (lua_State *L, const char *name) { - Table *reg = hvalue(&G(L)->l_registry); + Table *reg; lua_lock(L); /* unlock done in 'auxsetstr' */ + reg = hvalue(&G(L)->l_registry); auxsetstr(L, luaH_getint(reg, LUA_RIDX_GLOBALS), name); } @@ -1094,8 +1101,9 @@ LUA_API int lua_status (lua_State *L) { LUA_API int lua_gc (lua_State *L, int what, ...) { va_list argp; int res = 0; - global_State *g = G(L); + global_State *g; lua_lock(L); + g = G(L); va_start(argp, what); switch (what) { case LUA_GCSTOP: { diff --git a/ldo.c b/ldo.c index a89ac01049..66217a4bca 100644 --- a/ldo.c +++ b/ldo.c @@ -705,9 +705,10 @@ LUA_API int lua_isyieldable (lua_State *L) { LUA_API int lua_yieldk (lua_State *L, int nresults, lua_KContext ctx, lua_KFunction k) { - CallInfo *ci = L->ci; + CallInfo *ci; luai_userstateyield(L, nresults); lua_lock(L); + ci = L->ci; api_checknelems(L, nresults); if (unlikely(!yieldable(L))) { if (L != G(L)->mainthread) diff --git a/lstate.c b/lstate.c index b1f487ffc3..28853dc7b5 100644 --- a/lstate.c +++ b/lstate.c @@ -318,9 +318,10 @@ static void close_state (lua_State *L) { LUA_API lua_State *lua_newthread (lua_State *L) { - global_State *g = G(L); + global_State *g; lua_State *L1; lua_lock(L); + g = G(L); luaC_checkGC(L); /* create new thread */ L1 = &cast(LX *, luaM_newobject(L, LUA_TTHREAD, sizeof(LX)))->l; @@ -437,8 +438,8 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { LUA_API void lua_close (lua_State *L) { - L = G(L)->mainthread; /* only the main thread can be closed */ lua_lock(L); + L = G(L)->mainthread; /* only the main thread can be closed */ close_state(L); } From eb41999461b6f428186c55abd95f4ce1a76217d5 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 7 Jul 2020 18:03:48 -0300 Subject: [PATCH 0580/1145] Fixed bugs of stack reallocation x GC Macro 'checkstackGC' was doing a GC step after resizing the stack; the GC could shrink the stack and undo the resize. Moreover, macro 'checkstackp' also does a GC step, which could remove the preallocated CallInfo when calling a function. (Its name has been changed to 'checkstackGCp' to emphasize that it calls the GC.) --- ldo.c | 13 +++++++------ ldo.h | 6 ++++-- ltm.c | 4 ++-- lvm.c | 2 +- 4 files changed, 14 insertions(+), 11 deletions(-) diff --git a/ldo.c b/ldo.c index 66217a4bca..e3db1f7468 100644 --- a/ldo.c +++ b/ldo.c @@ -465,13 +465,13 @@ void luaD_call (lua_State *L, StkId func, int nresults) { f = fvalue(s2v(func)); Cfunc: { int n; /* number of returns */ - CallInfo *ci = next_ci(L); - checkstackp(L, LUA_MINSTACK, func); /* ensure minimum stack size */ + CallInfo *ci; + checkstackGCp(L, LUA_MINSTACK, func); /* ensure minimum stack size */ + L->ci = ci = next_ci(L); ci->nresults = nresults; ci->callstatus = CIST_C; ci->top = L->top + LUA_MINSTACK; ci->func = func; - L->ci = ci; lua_assert(ci->top <= L->stack_last); if (L->hookmask & LUA_MASKCALL) { int narg = cast_int(L->top - func) - 1; @@ -485,12 +485,13 @@ void luaD_call (lua_State *L, StkId func, int nresults) { break; } case LUA_VLCL: { /* Lua function */ - CallInfo *ci = next_ci(L); + CallInfo *ci; Proto *p = clLvalue(s2v(func))->p; int narg = cast_int(L->top - func) - 1; /* number of real arguments */ int nfixparams = p->numparams; int fsize = p->maxstacksize; /* frame size */ - checkstackp(L, fsize, func); + checkstackGCp(L, fsize, func); + L->ci = ci = next_ci(L); ci->nresults = nresults; ci->u.l.savedpc = p->code; /* starting point */ ci->callstatus = 0; @@ -504,7 +505,7 @@ void luaD_call (lua_State *L, StkId func, int nresults) { break; } default: { /* not a function */ - checkstackp(L, 1, func); /* space for metamethod */ + checkstackGCp(L, 1, func); /* space for metamethod */ luaD_tryfuncTM(L, func); /* try to get '__call' metamethod */ goto retry; /* try again with metamethod */ } diff --git a/ldo.h b/ldo.h index 7760f853b2..6c6cb28554 100644 --- a/ldo.h +++ b/ldo.h @@ -17,6 +17,8 @@ ** Macro to check stack size and grow stack if needed. Parameters ** 'pre'/'pos' allow the macro to preserve a pointer into the ** stack across reallocations, doing the work only when needed. +** It also allows the running of one GC step when the stack is +** reallocated. ** 'condmovestack' is used in heavy tests to force a stack reallocation ** at every check. */ @@ -35,7 +37,7 @@ /* macro to check stack size, preserving 'p' */ -#define checkstackp(L,n,p) \ +#define checkstackGCp(L,n,p) \ luaD_checkstackaux(L, n, \ ptrdiff_t t__ = savestack(L, p); /* save 'p' */ \ luaC_checkGC(L), /* stack grow uses memory */ \ @@ -44,7 +46,7 @@ /* macro to check stack size and GC */ #define checkstackGC(L,fsize) \ - luaD_checkstackaux(L, (fsize), (void)0, luaC_checkGC(L)) + luaD_checkstackaux(L, (fsize), luaC_checkGC(L), (void)0) /* type of protected functions, to be ran by 'runprotected' */ diff --git a/ltm.c b/ltm.c index ae60983f25..4770f96bb6 100644 --- a/ltm.c +++ b/ltm.c @@ -240,7 +240,7 @@ void luaT_adjustvarargs (lua_State *L, int nfixparams, CallInfo *ci, int actual = cast_int(L->top - ci->func) - 1; /* number of arguments */ int nextra = actual - nfixparams; /* number of extra arguments */ ci->u.l.nextraargs = nextra; - checkstackGC(L, p->maxstacksize + 1); + luaD_checkstack(L, p->maxstacksize + 1); /* copy function to the top of the stack */ setobjs2s(L, L->top++, ci->func); /* move fixed parameters to the top of the stack */ @@ -259,7 +259,7 @@ void luaT_getvarargs (lua_State *L, CallInfo *ci, StkId where, int wanted) { int nextra = ci->u.l.nextraargs; if (wanted < 0) { wanted = nextra; /* get all extra arguments available */ - checkstackp(L, nextra, where); /* ensure stack space */ + checkstackGCp(L, nextra, where); /* ensure stack space */ L->top = where + nextra; /* next instruction will need top */ } for (i = 0; i < wanted && i < nextra; i++) diff --git a/lvm.c b/lvm.c index ccbfbab501..d78d6be29a 100644 --- a/lvm.c +++ b/lvm.c @@ -1634,7 +1634,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { while (!ttisfunction(s2v(ra))) { /* not a function? */ luaD_tryfuncTM(L, ra); /* try '__call' metamethod */ b++; /* there is now one extra argument */ - checkstackp(L, 1, ra); + checkstackGCp(L, 1, ra); } if (!ttisLclosure(s2v(ra))) { /* C function? */ luaD_call(L, ra, LUA_MULTRET); /* call it */ From 31b8c2d4380a762d1ed6a7faee74a1d107f86014 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 8 Jul 2020 12:02:56 -0300 Subject: [PATCH 0581/1145] Fixed bug of access violation in finalizers Errors in finalizers need a valid 'pc' to produce an error message, even if the error is not propagated. Therefore, calls to the GC (which may call finalizers) inside luaV_execute must save the 'pc'. --- lvm.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lvm.c b/lvm.c index d78d6be29a..66d451b0c6 100644 --- a/lvm.c +++ b/lvm.c @@ -1101,9 +1101,9 @@ void luaV_finishOp (lua_State *L) { /* idem, but without changing the stack */ #define halfProtectNT(exp) (savepc(L), (exp)) - +/* 'c' is the limit of live values in the stack */ #define checkGC(L,c) \ - { luaC_condGC(L, L->top = (c), /* limit of live values */ \ + { luaC_condGC(L, (savepc(L), L->top = (c)), \ updatetrap(ci)); \ luai_threadyield(L); } @@ -1791,8 +1791,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_VARARGPREP) { - luaT_adjustvarargs(L, GETARG_A(i), ci, cl->p); - updatetrap(ci); + ProtectNT(luaT_adjustvarargs(L, GETARG_A(i), ci, cl->p)); if (trap) { luaD_hookcall(L, ci); L->oldpc = pc + 1; /* next opcode will be seen as a "new" line */ From 56ec4322817b0e9aef6084c278dcf24fda7bed1c Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 8 Jul 2020 15:36:48 -0300 Subject: [PATCH 0582/1145] Change in macro HARDMEMTESTS for testing GC Macro HARDMEMTESTS broke in two: HARDMEMTESTS forces a full GC cycle at every point where the GC can run. New macro EMERGENCYGCTESTS forces an emergency collection at every memory allocation. --- lmem.c | 2 +- makefile | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/lmem.c b/lmem.c index 65bfa5249f..43739bffd1 100644 --- a/lmem.c +++ b/lmem.c @@ -22,7 +22,7 @@ #include "lstate.h" -#if defined(HARDMEMTESTS) +#if defined(EMERGENCYGCTESTS) /* ** First allocation will fail whenever not building initial state ** and not shrinking a block. (This fail will trigger 'tryagain' and diff --git a/makefile b/makefile index baa6e673b4..ecc4291975 100644 --- a/makefile +++ b/makefile @@ -40,7 +40,9 @@ CWARNS= $(CWARNSCPP) $(CWARNSC) # Some useful compiler options for internal tests: # -DHARDSTACKTESTS forces a reallocation of the stack at every point where # the stack can be reallocated. -# -DHARDMEMTESTS forces an emergency collection at every single allocation. +# -DHARDMEMTESTS forces a full collection at all points where the collector +# can run. +# -DEMERGENCYGCTESTS forces an emergency collection at every single allocation. # -DEXTERNMEMCHECK removes internal consistency checking of blocks being # deallocated (useful when an external tool like valgrind does the check). # -DMAXINDEXRK=k limits range of constants in RK instruction operands. From 6f5bd5072dff07679c390eecfeaa9d20cc45a9ef Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 8 Jul 2020 15:51:55 -0300 Subject: [PATCH 0583/1145] Macro LUAI_ASSERT eases turning assertions on --- llimits.h | 10 +++++++++- ltests.h | 4 +--- makefile | 1 + 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/llimits.h b/llimits.h index b86d345256..48c97f9597 100644 --- a/llimits.h +++ b/llimits.h @@ -84,7 +84,15 @@ typedef LUAI_UACNUMBER l_uacNumber; typedef LUAI_UACINT l_uacInt; -/* internal assertions for in-house debugging */ +/* +** Internal assertions for in-house debugging +*/ +#if defined LUAI_ASSERT +#undef NDEBUG +#include +#define lua_assert(c) assert(c) +#endif + #if defined(lua_assert) #define check_exp(c,e) (lua_assert(c), (e)) /* to avoid problems with conditions too long */ diff --git a/ltests.h b/ltests.h index 02331ebc7f..2b4498cc54 100644 --- a/ltests.h +++ b/ltests.h @@ -20,9 +20,7 @@ /* turn on assertions */ -#undef NDEBUG -#include -#define lua_assert(c) assert(c) +#define LUAI_ASSERT diff --git a/makefile b/makefile index ecc4291975..7af55332c6 100644 --- a/makefile +++ b/makefile @@ -38,6 +38,7 @@ CWARNSC= -Wdeclaration-after-statement \ CWARNS= $(CWARNSCPP) $(CWARNSC) # Some useful compiler options for internal tests: +# -DLUAI_ASSERT turns on all assertions inside Lua. # -DHARDSTACKTESTS forces a reallocation of the stack at every point where # the stack can be reallocated. # -DHARDMEMTESTS forces a full collection at all points where the collector From 127e7a6c8942b362aa3c6627f44d660a4fb75312 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 10 Jul 2020 14:13:50 -0300 Subject: [PATCH 0584/1145] Fixed bug of old finalized objects in the GC When an object aged OLD1 is finalized, it is moved from the list 'finobj' to the *beginning* of the list 'allgc'. So, this part of the list (and not only the survival list) must be visited by 'markold'. --- lgc.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/lgc.c b/lgc.c index f26c921a96..f7fd7a59ab 100644 --- a/lgc.c +++ b/lgc.c @@ -1131,16 +1131,14 @@ static void finishgencycle (lua_State *L, global_State *g) { /* -** Does a young collection. First, mark 'OLD1' objects. (Only survival -** and "recent old" lists can contain 'OLD1' objects. New lists cannot -** contain 'OLD1' objects, at most 'OLD0' objects that were already -** visited when marked old.) Then does the atomic step. Then, -** sweep all lists and advance pointers. Finally, finish the collection. +** Does a young collection. First, mark 'OLD1' objects. Then does the +** atomic step. Then, sweep all lists and advance pointers. Finally, +** finish the collection. */ static void youngcollection (lua_State *L, global_State *g) { GCObject **psurvival; /* to point to first non-dead survival object */ lua_assert(g->gcstate == GCSpropagate); - markold(g, g->survival, g->reallyold); + markold(g, g->allgc, g->reallyold); markold(g, g->finobj, g->finobjrold); atomic(L); From 0f1cd0eba99ea6d383e75b9ae488d00ad541c210 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 13 Jul 2020 13:37:01 -0300 Subject: [PATCH 0585/1145] Added test for fix 127e7a6c894 --- testes/gengc.lua | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/testes/gengc.lua b/testes/gengc.lua index b02f471b36..4e80dd7eaf 100644 --- a/testes/gengc.lua +++ b/testes/gengc.lua @@ -37,6 +37,33 @@ do end +do -- bug in 5.4.0 +-- When an object aged OLD1 is finalized, it is moved from the list +-- 'finobj' to the *beginning* of the list 'allgc', but that part of the +-- list was not being visited by 'markold'. + local A = {} + A[1] = false -- old anchor for object + + -- obj finalizer + local function gcf (obj) + A[1] = obj -- anchor object + assert(not T or T.gcage(obj) == "old1") + obj = nil -- remove it from the stack + collectgarbage("step", 0) -- do a young collection + print(getmetatable(A[1]).x) -- metatable was collected + end + + collectgarbage() -- make A old + local obj = {} -- create a new object + collectgarbage("step", 0) -- make it a survival + assert(not T or T.gcage(obj) == "survival") + setmetatable(obj, {__gc = gcf, x = "ok"}) -- create its metatable + assert(not T or T.gcage(getmetatable(obj)) == "new") + obj = nil -- clear object + collectgarbage("step", 0) -- will call obj's finalizer +end + + if T == nil then (Message or print)('\n >>> testC not active: \z skipping some generational tests <<<\n') @@ -72,6 +99,9 @@ do assert(debug.getuservalue(U).x[1] == 234) end +-- just to make sure +assert(collectgarbage'isrunning') + -- just to make sure From e1d8770f12542d34a3e32b825c95b93f8a341ee1 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 13 Jul 2020 13:39:02 -0300 Subject: [PATCH 0586/1145] Fixed bug: wrong stack limit when entering a coroutine When entering a coroutine, the computation of nCcalls added 'from->nci' to correct for preallocated CallInfos, but 'nci' includes also the Callinfos already used. --- ldo.c | 2 +- testes/cstack.lua | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/ldo.c b/ldo.c index e3db1f7468..4c976a1487 100644 --- a/ldo.c +++ b/ldo.c @@ -674,7 +674,7 @@ LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs, if (from == NULL) L->nCcalls = CSTACKTHREAD; else /* correct 'nCcalls' for this thread */ - L->nCcalls = getCcalls(from) + from->nci - L->nci - CSTACKCF; + L->nCcalls = getCcalls(from) - L->nci - CSTACKCF; if (L->nCcalls <= CSTACKERR) return resume_error(L, "C stack overflow", nargs); luai_userstateresume(L, nargs); diff --git a/testes/cstack.lua b/testes/cstack.lua index e3e14f7495..4e37b98829 100644 --- a/testes/cstack.lua +++ b/testes/cstack.lua @@ -105,6 +105,22 @@ do print("testing stack-overflow in recursive 'gsub'") print("\tfinal count: ", count) end +do -- bug in 5.4.0 + print("testing limits in coroutines inside deep calls") + count = 0 + local lim = 1000 + local function stack (n) + progress() + if n > 0 then return stack(n - 1) + 1 + else coroutine.wrap(function () + stack(lim) + end)() + end + end + + print(xpcall(stack, function () return "ok" end, lim)) +end + do print("testing changes in C-stack limit") From 1ecfbfa1a1debd2258decdf7c1954ac6f9761699 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 15 Jul 2020 16:01:03 -0300 Subject: [PATCH 0587/1145] Fixed bug: invalid mode can crash 'io.popen' --- liolib.c | 7 +++++++ testes/files.lua | 15 +++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/liolib.c b/liolib.c index 7ac3444393..60ab1bfab6 100644 --- a/liolib.c +++ b/liolib.c @@ -52,6 +52,12 @@ static int l_checkmode (const char *mode) { ** ======================================================= */ +#if !defined(l_checkmodep) +/* By default, Lua accepts only "r" or "w" as mode */ +#define l_checkmodep(m) ((m[0] == 'r' || m[0] == 'w') && m[1] == '\0') +#endif + + #if !defined(l_popen) /* { */ #if defined(LUA_USE_POSIX) /* { */ @@ -279,6 +285,7 @@ static int io_popen (lua_State *L) { const char *filename = luaL_checkstring(L, 1); const char *mode = luaL_optstring(L, 2, "r"); LStream *p = newprefile(L); + luaL_argcheck(L, l_checkmodep(mode), 2, "invalid mode"); p->f = l_popen(L, filename, mode); p->closef = &io_pclose; return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1; diff --git a/testes/files.lua b/testes/files.lua index 677c0dc2df..16cf9b6a94 100644 --- a/testes/files.lua +++ b/testes/files.lua @@ -721,6 +721,21 @@ if not _port then progname = '"' .. arg[i + 1] .. '"' end print("testing popen/pclose and execute") + -- invalid mode for popen + checkerr("invalid mode", io.popen, "cat", "") + checkerr("invalid mode", io.popen, "cat", "r+") + checkerr("invalid mode", io.popen, "cat", "rw") + do -- basic tests for popen + local file = os.tmpname() + local f = assert(io.popen("cat - > " .. file, "w")) + f:write("a line") + assert(f:close()) + local f = assert(io.popen("cat - < " .. file, "r")) + assert(f:read("a") == "a line") + assert(f:close()) + assert(os.remove(file)) + end + local tests = { -- command, what, code {"ls > /dev/null", "ok"}, From a2195644d89812e5b157ce7bac35543e06db05e3 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 17 Jul 2020 11:01:05 -0300 Subject: [PATCH 0588/1145] Fixed bug: invalid 'oldpc' when returning to a function The field 'L->oldpc' is not always updated when control returns to a function; an invalid value can seg. fault when computing 'changedline'. (One example is an error in a finalizer; control can return to 'luaV_execute' without executing 'luaD_poscall'.) Instead of trying to fix all possible corner cases, it seems safer to be resilient to invalid values for 'oldpc'. Valid but wrong values at most cause an extra call to a line hook. --- ldebug.c | 41 +++++++++++++++++++++++++---------------- ldebug.h | 5 +++++ ldo.c | 6 +++--- lstate.c | 1 + lstate.h | 2 +- lvm.c | 2 +- 6 files changed, 36 insertions(+), 21 deletions(-) diff --git a/ldebug.c b/ldebug.c index afdc2b74ab..0c4439c185 100644 --- a/ldebug.c +++ b/ldebug.c @@ -33,10 +33,8 @@ #define noLuaClosure(f) ((f) == NULL || (f)->c.tt == LUA_VCCL) - -/* Active Lua function (given call info) */ -#define ci_func(ci) (clLvalue(s2v((ci)->func))) - +/* inverse of 'pcRel' */ +#define invpcRel(pc, p) ((p)->code + (pc) + 1) static const char *funcnamefromcode (lua_State *L, CallInfo *ci, const char **name); @@ -127,20 +125,18 @@ static void settraps (CallInfo *ci) { /* ** This function can be called during a signal, under "reasonable" ** assumptions. -** Fields 'oldpc', 'basehookcount', and 'hookcount' (set by -** 'resethookcount') are for debug only, and it is no problem if they -** get arbitrary values (causes at most one wrong hook call). 'hookmask' -** is an atomic value. We assume that pointers are atomic too (e.g., gcc -** ensures that for all platforms where it runs). Moreover, 'hook' is -** always checked before being called (see 'luaD_hook'). +** Fields 'basehookcount' and 'hookcount' (set by 'resethookcount') +** are for debug only, and it is no problem if they get arbitrary +** values (causes at most one wrong hook call). 'hookmask' is an atomic +** value. We assume that pointers are atomic too (e.g., gcc ensures that +** for all platforms where it runs). Moreover, 'hook' is always checked +** before being called (see 'luaD_hook'). */ LUA_API void lua_sethook (lua_State *L, lua_Hook func, int mask, int count) { if (func == NULL || mask == 0) { /* turn off hooks? */ mask = 0; func = NULL; } - if (isLua(L->ci)) - L->oldpc = L->ci->u.l.savedpc; L->hook = func; L->basehookcount = count; resethookcount(L); @@ -795,10 +791,24 @@ static int changedline (const Proto *p, int oldpc, int newpc) { } +/* +** Traces the execution of a Lua function. Called before the execution +** of each opcode, when debug is on. 'L->oldpc' stores the last +** instruction traced, to detect line changes. When entering a new +** function, 'npci' will be zero and will test as a new line without +** the need for 'oldpc'; so, 'oldpc' does not need to be initialized +** before. Some exceptional conditions may return to a function without +** updating 'oldpc'. In that case, 'oldpc' may be invalid; if so, it is +** reset to zero. (A wrong but valid 'oldpc' at most causes an extra +** call to a line hook.) +*/ int luaG_traceexec (lua_State *L, const Instruction *pc) { CallInfo *ci = L->ci; lu_byte mask = L->hookmask; + const Proto *p = ci_func(ci)->p; int counthook; + /* 'L->oldpc' may be invalid; reset it in this case */ + int oldpc = (L->oldpc < p->sizecode) ? L->oldpc : 0; if (!(mask & (LUA_MASKLINE | LUA_MASKCOUNT))) { /* no hooks? */ ci->u.l.trap = 0; /* don't need to stop again */ return 0; /* turn off 'trap' */ @@ -819,15 +829,14 @@ int luaG_traceexec (lua_State *L, const Instruction *pc) { if (counthook) luaD_hook(L, LUA_HOOKCOUNT, -1, 0, 0); /* call count hook */ if (mask & LUA_MASKLINE) { - const Proto *p = ci_func(ci)->p; int npci = pcRel(pc, p); if (npci == 0 || /* call linehook when enter a new function, */ - pc <= L->oldpc || /* when jump back (loop), or when */ - changedline(p, pcRel(L->oldpc, p), npci)) { /* enter new line */ + pc <= invpcRel(oldpc, p) || /* when jump back (loop), or when */ + changedline(p, oldpc, npci)) { /* enter new line */ int newline = luaG_getfuncline(p, npci); luaD_hook(L, LUA_HOOKLINE, newline, 0, 0); /* call line hook */ } - L->oldpc = pc; /* 'pc' of last call to line hook */ + L->oldpc = npci; /* 'pc' of last call to line hook */ } if (L->status == LUA_YIELD) { /* did hook yield? */ if (counthook) diff --git a/ldebug.h b/ldebug.h index 1fe0efab08..a0a584862e 100644 --- a/ldebug.h +++ b/ldebug.h @@ -13,6 +13,11 @@ #define pcRel(pc, p) (cast_int((pc) - (p)->code) - 1) + +/* Active Lua function (given call info) */ +#define ci_func(ci) (clLvalue(s2v((ci)->func))) + + #define resethookcount(L) (L->hookcount = L->basehookcount) /* diff --git a/ldo.c b/ldo.c index 4c976a1487..98dd9fbbfa 100644 --- a/ldo.c +++ b/ldo.c @@ -327,7 +327,7 @@ static StkId rethook (lua_State *L, CallInfo *ci, StkId firstres, int nres) { ptrdiff_t oldtop = savestack(L, L->top); /* hook may change top */ int delta = 0; if (isLuacode(ci)) { - Proto *p = clLvalue(s2v(ci->func))->p; + Proto *p = ci_func(ci)->p; if (p->is_vararg) delta = ci->u.l.nextraargs + p->numparams + 1; if (L->top < ci->top) @@ -340,8 +340,8 @@ static StkId rethook (lua_State *L, CallInfo *ci, StkId firstres, int nres) { luaD_hook(L, LUA_HOOKRET, -1, ftransfer, nres); /* call it */ ci->func -= delta; } - if (isLua(ci->previous)) - L->oldpc = ci->previous->u.l.savedpc; /* update 'oldpc' */ + if (isLua(ci = ci->previous)) + L->oldpc = pcRel(ci->u.l.savedpc, ci_func(ci)->p); /* update 'oldpc' */ return restorestack(L, oldtop); } diff --git a/lstate.c b/lstate.c index 28853dc7b5..06fa13d72c 100644 --- a/lstate.c +++ b/lstate.c @@ -301,6 +301,7 @@ static void preinit_thread (lua_State *L, global_State *g) { L->openupval = NULL; L->status = LUA_OK; L->errfunc = 0; + L->oldpc = 0; } diff --git a/lstate.h b/lstate.h index 2e8bd6c486..0c545ec5b9 100644 --- a/lstate.h +++ b/lstate.h @@ -286,7 +286,6 @@ struct lua_State { StkId top; /* first free slot in the stack */ global_State *l_G; CallInfo *ci; /* call info for current function */ - const Instruction *oldpc; /* last pc traced */ StkId stack_last; /* last free slot in the stack */ StkId stack; /* stack base */ UpVal *openupval; /* list of open upvalues in this stack */ @@ -297,6 +296,7 @@ struct lua_State { volatile lua_Hook hook; ptrdiff_t errfunc; /* current error handling function (stack index) */ l_uint32 nCcalls; /* number of allowed nested C calls - 'nci' */ + int oldpc; /* last pc traced */ int stacksize; int basehookcount; int hookcount; diff --git a/lvm.c b/lvm.c index 66d451b0c6..08681af1b8 100644 --- a/lvm.c +++ b/lvm.c @@ -1794,7 +1794,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { ProtectNT(luaT_adjustvarargs(L, GETARG_A(i), ci, cl->p)); if (trap) { luaD_hookcall(L, ci); - L->oldpc = pc + 1; /* next opcode will be seen as a "new" line */ + L->oldpc = 1; /* next opcode will be seen as a "new" line */ } updatebase(ci); /* function has new base after adjustment */ vmbreak; From 34affe7a63fc5d842580a9f23616d057e17dfe27 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 17 Jul 2020 14:54:26 -0300 Subject: [PATCH 0589/1145] Fixed bug: 'luaD_callnoyield' called twice in a row In luaD_callnoyield, when there is a possible stack overflow, it zeros the number of CallInfos to force a check when calling the function. However, if the "function" is not a function, the code will raise an error before checking the stack. Then, the error handling calls luaD_callnoyield again and nCcalls is decremented again, crossing the stack redzone without raising an error. (This loop can only happens once, because the error handler must be a function. But once is enough to cross the redzone.) --- ldo.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/ldo.c b/ldo.c index 98dd9fbbfa..5473815a18 100644 --- a/ldo.c +++ b/ldo.c @@ -515,14 +515,13 @@ void luaD_call (lua_State *L, StkId func, int nresults) { /* ** Similar to 'luaD_call', but does not allow yields during the call. -** If there is a stack overflow, freeing all CI structures will -** force the subsequent call to invoke 'luaE_extendCI', which then -** will raise any errors. */ void luaD_callnoyield (lua_State *L, StkId func, int nResults) { incXCcalls(L); - if (getCcalls(L) <= CSTACKERR) /* possible stack overflow? */ - luaE_freeCI(L); + if (getCcalls(L) <= CSTACKERR) { /* possible C stack overflow? */ + luaE_exitCcall(L); /* to compensate decrement in next call */ + luaE_enterCcall(L); /* check properly */ + } luaD_call(L, func, nResults); decXCcalls(L); } From a6da1472c0c5e05ff249325f979531ad51533110 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 27 Jul 2020 10:26:20 -0300 Subject: [PATCH 0590/1145] Fixed bug: barriers cannot be active during sweep Barriers cannot be active during sweep, even in generational mode. (Although gen. mode is not incremental, it can hit a barrier when deleting a thread and closing its upvalues.) The colors of objects are being changed during sweep and, therefore, cannot be trusted. --- lgc.c | 48 ++++++++++++++++++++++++++++++++---------------- testes/gengc.lua | 28 +++++++++++++++++++++++++++- 2 files changed, 59 insertions(+), 17 deletions(-) diff --git a/lgc.c b/lgc.c index f7fd7a59ab..64d1334b4f 100644 --- a/lgc.c +++ b/lgc.c @@ -181,14 +181,17 @@ static int iscleared (global_State *g, const GCObject *o) { /* -** barrier that moves collector forward, that is, mark the white object -** 'v' being pointed by the black object 'o'. (If in sweep phase, clear -** the black object to white [sweep it] to avoid other barrier calls for -** this same object.) In the generational mode, 'v' must also become -** old, if 'o' is old; however, it cannot be changed directly to OLD, -** because it may still point to non-old objects. So, it is marked as -** OLD0. In the next cycle it will become OLD1, and in the next it -** will finally become OLD (regular old). +** Barrier that moves collector forward, that is, marks the white object +** 'v' being pointed by the black object 'o'. In the generational +** mode, 'v' must also become old, if 'o' is old; however, it cannot +** be changed directly to OLD, because it may still point to non-old +** objects. So, it is marked as OLD0. In the next cycle it will become +** OLD1, and in the next it will finally become OLD (regular old). By +** then, any object it points to will also be old. If called in the +** incremental sweep phase, it clears the black object to white (sweep +** it) to avoid other barrier calls for this same object. (That cannot +** be done is generational mode, as its sweep does not distinguish +** whites from deads.) */ void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v) { global_State *g = G(L); @@ -202,7 +205,8 @@ void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v) { } else { /* sweep phase */ lua_assert(issweepphase(g)); - makewhite(g, o); /* mark main obj. as white to avoid other barriers */ + if (g->gckind == KGC_INC) /* incremental mode? */ + makewhite(g, o); /* mark 'o' as white to avoid other barriers */ } } @@ -324,10 +328,15 @@ static lu_mem markbeingfnz (global_State *g) { /* -** Mark all values stored in marked open upvalues from non-marked threads. -** (Values from marked threads were already marked when traversing the -** thread.) Remove from the list threads that no longer have upvalues and -** not-marked threads. +** For each non-marked thread, simulates a barrier between each open +** upvalue and its value. (If the thread is collected, the value will be +** assigned to the upvalue, but then it can be too late for the barrier +** to act. The "barrier" does not need to check colors: A non-marked +** thread must be young; upvalues cannot be older than their threads; so +** any visited upvalue must be young too.) Also removes the thread from +** the list, as it was already visited. Removes also threads with no +** upvalues, as they have nothing to be checked. (If the thread gets an +** upvalue later, it will be linked in the list again.) */ static int remarkupvals (global_State *g) { lua_State *thread; @@ -340,9 +349,11 @@ static int remarkupvals (global_State *g) { p = &thread->twups; /* keep marked thread with upvalues in the list */ else { /* thread is not marked or without upvalues */ UpVal *uv; + lua_assert(!isold(thread) || thread->openupval == NULL); *p = thread->twups; /* remove thread from the list */ thread->twups = thread; /* mark that it is out of list */ for (uv = thread->openupval; uv != NULL; uv = uv->u.open.next) { + lua_assert(getage(uv) <= getage(thread)); work++; if (!iswhite(uv)) /* upvalue already visited? */ markvalue(g, uv->v); /* mark its value */ @@ -995,6 +1006,9 @@ static void sweep2old (lua_State *L, GCObject **p) { ** during the sweep. So, any white object must be dead.) For ** non-dead objects, advance their ages and clear the color of ** new objects. (Old objects keep their colors.) +** The ages of G_TOUCHED1 and G_TOUCHED2 objects will advance +** in 'correctgraylist'. (That function will also remove objects +** turned white here from any gray list.) */ static GCObject **sweepgen (lua_State *L, global_State *g, GCObject **p, GCObject *limit) { @@ -1055,16 +1069,16 @@ static GCObject **correctgraylist (GCObject **p) { lua_assert(isgray(curr)); gray2black(curr); /* make it black, for next barrier */ changeage(curr, G_TOUCHED1, G_TOUCHED2); - p = next; /* go to next element */ + p = next; /* keep it in the list and go to next element */ } - else { /* not touched in this cycle */ + else { /* everything else is removed */ + /* white objects are simply removed */ if (!iswhite(curr)) { /* not white? */ lua_assert(isold(curr)); if (getage(curr) == G_TOUCHED2) /* advance from G_TOUCHED2... */ changeage(curr, G_TOUCHED2, G_OLD); /* ... to G_OLD */ gray2black(curr); /* make it black */ } - /* else, object is white: just remove it from this list */ *p = *next; /* remove 'curr' from gray list */ } break; @@ -1143,6 +1157,7 @@ static void youngcollection (lua_State *L, global_State *g) { atomic(L); /* sweep nursery and get a pointer to its last live element */ + g->gcstate = GCSswpallgc; psurvival = sweepgen(L, g, &g->allgc, g->survival); /* sweep 'survival' and 'old' */ sweepgen(L, g, psurvival, g->reallyold); @@ -1166,6 +1181,7 @@ static void youngcollection (lua_State *L, global_State *g) { static void atomic2gen (lua_State *L, global_State *g) { /* sweep all elements making them old */ + g->gcstate = GCSswpallgc; sweep2old(L, &g->allgc); /* everything alive now is old */ g->reallyold = g->old = g->survival = g->allgc; diff --git a/testes/gengc.lua b/testes/gengc.lua index 4e80dd7eaf..7a7dabdd4b 100644 --- a/testes/gengc.lua +++ b/testes/gengc.lua @@ -57,13 +57,39 @@ do -- bug in 5.4.0 local obj = {} -- create a new object collectgarbage("step", 0) -- make it a survival assert(not T or T.gcage(obj) == "survival") - setmetatable(obj, {__gc = gcf, x = "ok"}) -- create its metatable + setmetatable(obj, {__gc = gcf, x = "+"}) -- create its metatable assert(not T or T.gcage(getmetatable(obj)) == "new") obj = nil -- clear object collectgarbage("step", 0) -- will call obj's finalizer end +do -- another bug in 5.4.0 + local old = {10} + collectgarbage() -- make 'old' old + local co = coroutine.create( + function () + local x = nil + local f = function () + return x[1] + end + x = coroutine.yield(f) + coroutine.yield() + end + ) + local _, f = coroutine.resume(co) -- create closure over 'x' in coroutine + collectgarbage("step", 0) -- make upvalue a survival + old[1] = {"hello"} -- 'old' go to grayagain as 'touched1' + coroutine.resume(co, {123}) -- its value will be new + co = nil + collectgarbage("step", 0) -- hit the barrier + assert(f() == 123 and old[1][1] == "hello") + collectgarbage("step", 0) -- run the collector once more + -- make sure old[1] was not collected + assert(f() == 123 and old[1][1] == "hello") +end + + if T == nil then (Message or print)('\n >>> testC not active: \z skipping some generational tests <<<\n') From 8c7c9ea06502b8caa5224bf74c90a8885dbe0d42 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 27 Jul 2020 11:24:03 -0300 Subject: [PATCH 0591/1145] Function 'printobj' in 'ltests.c' made public It helps to have this function available for debugging. --- ltests.c | 4 ++++ ltests.h | 8 +++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/ltests.c b/ltests.c index 0513354c33..a4e5d28216 100644 --- a/ltests.c +++ b/ltests.c @@ -309,6 +309,10 @@ static void printobj (global_State *g, GCObject *o) { } +void lua_printobj (lua_State *L, struct GCObject *o) { + printobj(G(L), o); +} + static int testobjref (global_State *g, GCObject *f, GCObject *t) { int r1 = testobjref1(g, f, t); if (!r1) { diff --git a/ltests.h b/ltests.h index 2b4498cc54..1a2d8d28b3 100644 --- a/ltests.h +++ b/ltests.h @@ -72,7 +72,13 @@ extern void *l_Trick; /* ** Function to traverse and check all memory used by Lua */ -int lua_checkmemory (lua_State *L); +LUAI_FUNC int lua_checkmemory (lua_State *L); + +/* +** Function to print an object GC-friendly +*/ +struct GCObject; +LUAI_FUNC void lua_printobj (lua_State *L, struct GCObject *o); /* test for lock/unlock */ From d2c2e32e8a0f649099de0e9d04b5a72037b7b138 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 27 Jul 2020 11:39:42 -0300 Subject: [PATCH 0592/1145] All objects are kept 'new' in incremental GC Small changes to ensure that all objects are kept 'new' in incremental GC (except for fixed strings, which are always old) and to make that fact clearer. --- lfunc.c | 5 +++-- lgc.c | 38 +++++++++++++++++++++----------------- 2 files changed, 24 insertions(+), 19 deletions(-) diff --git a/lfunc.c b/lfunc.c index 10100e5aca..f8c3c4467e 100644 --- a/lfunc.c +++ b/lfunc.c @@ -234,9 +234,10 @@ int luaF_close (lua_State *L, StkId level, int status) { luaF_unlinkupval(uv); setobj(L, slot, uv->v); /* move value to upvalue slot */ uv->v = slot; /* now current value lives here */ - if (!iswhite(uv)) + if (!iswhite(uv)) { /* neither white nor dead? */ gray2black(uv); /* closed upvalues cannot be gray */ - luaC_barrier(L, uv, slot); + luaC_barrier(L, uv, slot); + } } return status; } diff --git a/lgc.c b/lgc.c index 64d1334b4f..3591c699a5 100644 --- a/lgc.c +++ b/lgc.c @@ -60,13 +60,16 @@ #define PAUSEADJ 100 -/* mask to erase all color bits (plus gen. related stuff) */ -#define maskcolors (~(bitmask(BLACKBIT) | WHITEBITS | AGEBITS)) +/* mask to erase all color bits */ +#define maskcolors (~(bitmask(BLACKBIT) | WHITEBITS)) +/* mask to erase all GC bits */ +#define maskgcbits (maskcolors & ~AGEBITS) -/* macro to erase all color bits then sets only the current white bit */ + +/* macro to erase all color bits then set only the current white bit */ #define makewhite(g,x) \ - (x->marked = cast_byte((x->marked & maskcolors) | luaC_white(g))) + (x->marked = cast_byte((x->marked & maskcolors) | luaC_white(g))) #define white2gray(x) resetbits(x->marked, WHITEBITS) #define black2gray(x) resetbit(x->marked, BLACKBIT) @@ -218,11 +221,12 @@ void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v) { void luaC_barrierback_ (lua_State *L, GCObject *o) { global_State *g = G(L); lua_assert(isblack(o) && !isdead(g, o)); - lua_assert(g->gckind != KGC_GEN || (isold(o) && getage(o) != G_TOUCHED1)); + lua_assert((g->gckind == KGC_GEN) == (isold(o) && getage(o) != G_TOUCHED1)); if (getage(o) != G_TOUCHED2) /* not already in gray list? */ linkobjgclist(o, g->grayagain); /* link it in 'grayagain' */ black2gray(o); /* make object gray (again) */ - setage(o, G_TOUCHED1); /* touched in current cycle */ + if (isold(o)) /* generational mode? */ + setage(o, G_TOUCHED1); /* touched in current cycle */ } @@ -341,7 +345,7 @@ static lu_mem markbeingfnz (global_State *g) { static int remarkupvals (global_State *g) { lua_State *thread; lua_State **p = &g->twups; - int work = 0; + int work = 0; /* estimate of how much work was done here */ while ((thread = *p) != NULL) { work++; lua_assert(!isblack(thread)); /* threads are never black */ @@ -777,7 +781,7 @@ static GCObject **sweeplist (lua_State *L, GCObject **p, int countin, freeobj(L, curr); /* erase 'curr' */ } else { /* change mark to 'white' */ - curr->marked = cast_byte((marked & maskcolors) | white); + curr->marked = cast_byte((marked & maskgcbits) | white); p = &curr->next; /* go to next element */ } } @@ -976,10 +980,6 @@ void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt) { static void setpause (global_State *g); -/* mask to erase all color bits, not changing gen-related stuff */ -#define maskgencolors (~(bitmask(BLACKBIT) | WHITEBITS)) - - /* ** Sweep a list of objects, deleting dead ones and turning ** the non dead to old (without changing their colors). @@ -1030,9 +1030,12 @@ static GCObject **sweepgen (lua_State *L, global_State *g, GCObject **p, freeobj(L, curr); /* erase 'curr' */ } else { /* correct mark and age */ - if (getage(curr) == G_NEW) - curr->marked = cast_byte((curr->marked & maskgencolors) | white); - setage(curr, nextage[getage(curr)]); + if (getage(curr) == G_NEW) { /* new objects go back to white */ + int marked = curr->marked & maskgcbits; /* erase GC bits */ + curr->marked = cast_byte(marked | G_SURVIVAL | white); + } + else /* all other objects will be old, and so keep their color */ + setage(curr, nextage[getage(curr)]); p = &curr->next; /* go to next element */ } } @@ -1042,12 +1045,13 @@ static GCObject **sweepgen (lua_State *L, global_State *g, GCObject **p, /* ** Traverse a list making all its elements white and clearing their -** age. +** age. In incremental mode, all objects are 'new' all the time, +** except for fixed strings (which are always old). */ static void whitelist (global_State *g, GCObject *p) { int white = luaC_white(g); for (; p != NULL; p = p->next) - p->marked = cast_byte((p->marked & maskcolors) | white); + p->marked = cast_byte((p->marked & maskgcbits) | white); } From a585eae6e7ada1ca9271607a4f48dfb17868ab7b Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 27 Jul 2020 12:01:38 -0300 Subject: [PATCH 0593/1145] Fixed bug: Negation overflow in getlocal/setlocal --- ldebug.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ldebug.c b/ldebug.c index 0c4439c185..9ff7edeb25 100644 --- a/ldebug.c +++ b/ldebug.c @@ -188,8 +188,8 @@ static const char *upvalname (const Proto *p, int uv) { static const char *findvararg (CallInfo *ci, int n, StkId *pos) { if (clLvalue(s2v(ci->func))->p->is_vararg) { int nextra = ci->u.l.nextraargs; - if (n <= nextra) { - *pos = ci->func - nextra + (n - 1); + if (n >= -nextra) { /* 'n' is negative */ + *pos = ci->func - nextra - (n + 1); return "(vararg)"; /* generic name for any vararg */ } } @@ -202,7 +202,7 @@ const char *luaG_findlocal (lua_State *L, CallInfo *ci, int n, StkId *pos) { const char *name = NULL; if (isLua(ci)) { if (n < 0) /* access to vararg values? */ - return findvararg(ci, -n, pos); + return findvararg(ci, n, pos); else name = luaF_getlocalname(ci_func(ci)->p, n, currentpc(ci)); } From ae5b5ba529753c7a653901ffc29b5ea24c3fdf3a Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 27 Jul 2020 13:23:05 -0300 Subject: [PATCH 0594/1145] Fixed bug: line hooks in stripped functions Line-hook handling was accessing debug info. without checking whether it was present. --- ldebug.c | 4 +++- testes/db.lua | 19 ++++++++++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/ldebug.c b/ldebug.c index 9ff7edeb25..8cb00e51a1 100644 --- a/ldebug.c +++ b/ldebug.c @@ -783,11 +783,13 @@ l_noret luaG_runerror (lua_State *L, const char *fmt, ...) { ** previous instruction 'oldpc'. */ static int changedline (const Proto *p, int oldpc, int newpc) { + if (p->lineinfo == NULL) /* no debug information? */ + return 0; while (oldpc++ < newpc) { if (p->lineinfo[oldpc] != 0) return (luaG_getfuncline(p, oldpc - 1) != luaG_getfuncline(p, newpc)); } - return 0; /* no line changes in the way */ + return 0; /* no line changes between positions */ } diff --git a/testes/db.lua b/testes/db.lua index 941283f7a9..5377f6ec0e 100644 --- a/testes/db.lua +++ b/testes/db.lua @@ -884,7 +884,7 @@ end print("testing debug functions on chunk without debug info") -prog = [[-- program to be loaded without debug information +prog = [[-- program to be loaded without debug information (strip) local debug = require'debug' local a = 12 -- a local variable @@ -927,6 +927,23 @@ local f = assert(load(string.dump(load(prog), true))) assert(f() == 13) +do -- bug in 5.4.0: line hooks in stripped code + local function foo () + local a = 1 + local b = 2 + return b + end + + local s = load(string.dump(foo, true)) + local line = true + debug.sethook(function (e, l) + assert(e == "line") + line = l + end, "l") + assert(s() == 2); debug.sethook(nil) + assert(line == nil) -- hook called withoug debug info for 1st instruction +end + do -- tests for 'source' in binary dumps local prog = [[ return function (x) From 663f83f647f9199541ce1b60a6496b4124b4fdd3 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 28 Jul 2020 15:51:07 -0300 Subject: [PATCH 0595/1145] Same changes around 'correctgraylist' Instead of adding all tables and userdata back to the 'grayagain' list to be checked by 'correctgraylist', the collector adds only the objects that will remain in that list (objects aged TOUCHED1). This commit also rewrites 'correctgraylist' with a clearer logic. --- lgc.c | 119 ++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 66 insertions(+), 53 deletions(-) diff --git a/lgc.c b/lgc.c index 3591c699a5..faa9c90241 100644 --- a/lgc.c +++ b/lgc.c @@ -389,6 +389,32 @@ static void restartcollection (global_State *g) { ** ======================================================= */ + +/* +** Check whether object 'o' should be kept in the 'grayagain' list for +** post-processing by 'correctgraylist'. (It could put all old objects +** in the list and leave all the work to 'correctgraylist', but it is +** more efficient to avoid adding elements that will be removed.) Only +** TOUCHED1 objects need to be in the list. TOUCHED2 doesn't need to go +** back to a gray list, but then it must become OLD. (That is what +** 'correctgraylist' does when it finds a TOUCHED2 object.) +** It is defined as a macro because 'gclist' is not a unique field in +** different collectable objects. +*/ +#define genlink(g,o) genlink_(g, obj2gco(o), &(o)->gclist) + +static void genlink_ (global_State *g, GCObject *o, GCObject **pnext) { + lua_assert(isblack(o)); + if (getage(o) == G_TOUCHED1) { /* touched in this cycle? */ + *pnext = g->grayagain; /* link it back in 'grayagain' */ + g->grayagain = o; + black2gray(o); + } /* everything else do not need to be linked back */ + else if (getage(o) == G_TOUCHED2) + changeage(o, G_TOUCHED2, G_OLD); /* advance age */ +} + + /* ** Traverse a table with weak values and link it to proper list. During ** propagate phase, keep it in 'grayagain' list, to be revisited in the @@ -425,8 +451,9 @@ static void traverseweakvalue (global_State *g, Table *h) { ** the atomic phase, if table has any white->white entry, it has to ** be revisited during ephemeron convergence (as that key may turn ** black). Otherwise, if it has any white key, table has to be cleared -** (in the atomic phase). In generational mode, it (like all visited -** tables) must be kept in some gray list for post-processing. +** (in the atomic phase). In generational mode, some tables +** must be kept in some gray list for post-processing; this is done +** by 'genlink'. */ static int traverseephemeron (global_State *g, Table *h, int inv) { int marked = 0; /* true if an object is marked in this traversal */ @@ -465,10 +492,10 @@ static int traverseephemeron (global_State *g, Table *h, int inv) { linkgclist(h, g->ephemeron); /* have to propagate again */ else if (hasclears) /* table has white keys? */ linkgclist(h, g->allweak); /* may have to clean white keys */ - else if (g->gckind == KGC_GEN) - linkgclist(h, g->grayagain); /* keep it in some list */ - else - gray2black(h); + else { + gray2black(h); /* 'genlink' expects black objects */ + genlink(g, h); /* check whether collector still needs to see it */ + } return marked; } @@ -488,10 +515,7 @@ static void traversestrongtable (global_State *g, Table *h) { markvalue(g, gval(n)); } } - if (g->gckind == KGC_GEN) { - linkgclist(h, g->grayagain); /* keep it in some gray list */ - black2gray(h); - } + genlink(g, h); } @@ -503,7 +527,7 @@ static lu_mem traversetable (global_State *g, Table *h) { (cast_void(weakkey = strchr(svalue(mode), 'k')), cast_void(weakvalue = strchr(svalue(mode), 'v')), (weakkey || weakvalue))) { /* is really weak? */ - black2gray(h); /* keep table gray */ + black2gray(h); /* turn it back to gray, as it probably goes to a list */ if (!weakkey) /* strong keys? */ traverseweakvalue(g, h); else if (!weakvalue) /* strong values? */ @@ -522,10 +546,7 @@ static int traverseudata (global_State *g, Udata *u) { markobjectN(g, u->metatable); /* mark its metatable */ for (i = 0; i < u->nuvalue; i++) markvalue(g, &u->uv[i].uv); - if (g->gckind == KGC_GEN) { - linkgclist(u, g->grayagain); /* keep it in some gray list */ - black2gray(u); - } + genlink(g, u); return 1 + u->nuvalue; } @@ -1006,9 +1027,10 @@ static void sweep2old (lua_State *L, GCObject **p) { ** during the sweep. So, any white object must be dead.) For ** non-dead objects, advance their ages and clear the color of ** new objects. (Old objects keep their colors.) -** The ages of G_TOUCHED1 and G_TOUCHED2 objects will advance -** in 'correctgraylist'. (That function will also remove objects -** turned white here from any gray list.) +** The ages of G_TOUCHED1 and G_TOUCHED2 objects cannot be advanced +** here, because these old-generation objects are usually not swept +** here. They will all be advanced in 'correctgraylist'. That function +** will also remove objects turned white here from any gray list. */ static GCObject **sweepgen (lua_State *L, global_State *g, GCObject **p, GCObject *limit) { @@ -1056,48 +1078,39 @@ static void whitelist (global_State *g, GCObject *p) { /* -** Correct a list of gray objects. +** Correct a list of gray objects. Return pointer to where rest of the +** list should be linked. ** Because this correction is done after sweeping, young objects might ** be turned white and still be in the list. They are only removed. -** For tables and userdata, advance 'touched1' to 'touched2'; 'touched2' -** objects become regular old and are removed from the list. -** For threads, just remove white ones from the list. +** 'TOUCHED1' objects are advanced to 'TOUCHED2' and remain on the list; +** Non-white threads also remain on the list; 'TOUCHED2' objects become +** regular old; they and anything else are removed from the list. */ static GCObject **correctgraylist (GCObject **p) { GCObject *curr; while ((curr = *p) != NULL) { - switch (curr->tt) { - case LUA_VTABLE: case LUA_VUSERDATA: { - GCObject **next = getgclist(curr); - if (getage(curr) == G_TOUCHED1) { /* touched in this cycle? */ - lua_assert(isgray(curr)); - gray2black(curr); /* make it black, for next barrier */ - changeage(curr, G_TOUCHED1, G_TOUCHED2); - p = next; /* keep it in the list and go to next element */ - } - else { /* everything else is removed */ - /* white objects are simply removed */ - if (!iswhite(curr)) { /* not white? */ - lua_assert(isold(curr)); - if (getage(curr) == G_TOUCHED2) /* advance from G_TOUCHED2... */ - changeage(curr, G_TOUCHED2, G_OLD); /* ... to G_OLD */ - gray2black(curr); /* make it black */ - } - *p = *next; /* remove 'curr' from gray list */ - } - break; - } - case LUA_VTHREAD: { - lua_State *th = gco2th(curr); - lua_assert(!isblack(th)); - if (iswhite(th)) /* new object? */ - *p = th->gclist; /* remove from gray list */ - else /* old threads remain gray */ - p = &th->gclist; /* go to next element */ - break; - } - default: lua_assert(0); /* nothing more could be gray here */ + GCObject **next = getgclist(curr); + if (iswhite(curr)) + goto remove; /* remove all white objects */ + else if (getage(curr) == G_TOUCHED1) { /* touched in this cycle? */ + lua_assert(isgray(curr)); + gray2black(curr); /* make it black, for next barrier */ + changeage(curr, G_TOUCHED1, G_TOUCHED2); + goto remain; /* keep it in the list and go to next element */ + } + else if (curr->tt == LUA_VTHREAD) { + lua_assert(isgray(curr)); + goto remain; /* keep non-white threads on the list */ + } + else { /* everything else is removed */ + lua_assert(isold(curr)); /* young objects should be white */ + if (getage(curr) == G_TOUCHED2) /* advance from G_TOUCHED2... */ + changeage(curr, G_TOUCHED2, G_OLD); /* ... to G_OLD */ + gray2black(curr); /* make object black */ + goto remove; } + remove: *p = *next; continue; + remain: p = next; continue; } return p; } From 71f70df3271f6e8ae9e8efcaef3be19f8d37c161 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 29 Jul 2020 09:58:25 -0300 Subject: [PATCH 0596/1145] OLD1 ages advanced by 'markold' Objects aged OLD1 have their ages advanced by 'markold', which has to visit them anyway. So, the GC doesn't need to "sweep" the old1 list. --- lgc.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/lgc.c b/lgc.c index faa9c90241..eec73871b4 100644 --- a/lgc.c +++ b/lgc.c @@ -1131,7 +1131,7 @@ static void correctgraylists (global_State *g) { /* -** Mark 'OLD1' objects when starting a new young collection. +** Mark black 'OLD1' objects when starting a new young collection. ** Gray objects are already in some gray list, and so will be visited ** in the atomic step. */ @@ -1140,6 +1140,7 @@ static void markold (global_State *g, GCObject *from, GCObject *to) { for (p = from; p != to; p = p->next) { if (getage(p) == G_OLD1) { lua_assert(!iswhite(p)); + changeage(p, G_OLD1, G_OLD); /* now they are old */ if (isblack(p)) { black2gray(p); /* should be '2white', but gray works too */ reallymarkobject(g, p); @@ -1176,16 +1177,16 @@ static void youngcollection (lua_State *L, global_State *g) { /* sweep nursery and get a pointer to its last live element */ g->gcstate = GCSswpallgc; psurvival = sweepgen(L, g, &g->allgc, g->survival); - /* sweep 'survival' and 'old' */ - sweepgen(L, g, psurvival, g->reallyold); + /* sweep 'survival' */ + sweepgen(L, g, psurvival, g->old); g->reallyold = g->old; g->old = *psurvival; /* 'survival' survivals are old now */ g->survival = g->allgc; /* all news are survivals */ /* repeat for 'finobj' lists */ psurvival = sweepgen(L, g, &g->finobj, g->finobjsur); - /* sweep 'survival' and 'old' */ - sweepgen(L, g, psurvival, g->finobjrold); + /* sweep 'survival' */ + sweepgen(L, g, psurvival, g->finobjold); g->finobjrold = g->finobjold; g->finobjold = *psurvival; /* 'survival' survivals are old now */ g->finobjsur = g->finobj; /* all news are survivals */ From b4c353434f28f3e9d4c45e61d42b4fd07d76cad2 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 29 Jul 2020 11:34:08 -0300 Subject: [PATCH 0597/1145] Details The fields 'old' and 'finobjold' were renamed 'old1' and 'finobjold1', respectively, to make clearer the main ages of their elements. --- lgc.c | 30 +++++++++++++++--------------- lstate.c | 4 ++-- lstate.h | 19 +++++++++++++------ ltests.c | 4 ++-- 4 files changed, 32 insertions(+), 25 deletions(-) diff --git a/lgc.c b/lgc.c index eec73871b4..347b6778a5 100644 --- a/lgc.c +++ b/lgc.c @@ -932,15 +932,15 @@ static GCObject **findlast (GCObject **p) { /* ** Move all unreachable objects (or 'all' objects) that need ** finalization from list 'finobj' to list 'tobefnz' (to be finalized). -** (Note that objects after 'finobjold' cannot be white, so they -** don't need to be traversed. In incremental mode, 'finobjold' is NULL, +** (Note that objects after 'finobjold1' cannot be white, so they +** don't need to be traversed. In incremental mode, 'finobjold1' is NULL, ** so the whole list is traversed.) */ static void separatetobefnz (global_State *g, int all) { GCObject *curr; GCObject **p = &g->finobj; GCObject **lastnext = findlast(&g->tobefnz); - while ((curr = *p) != g->finobjold) { /* traverse all finalizable objects */ + while ((curr = *p) != g->finobjold1) { /* traverse all finalizable objects */ lua_assert(tofinalize(curr)); if (!(iswhite(curr) || all)) /* not being collected? */ p = &curr->next; /* don't bother with it */ @@ -975,8 +975,8 @@ void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt) { else { /* correct pointers into 'allgc' list, if needed */ if (o == g->survival) g->survival = o->next; - if (o == g->old) - g->old = o->next; + if (o == g->old1) + g->old1 = o->next; if (o == g->reallyold) g->reallyold = o->next; } @@ -1178,17 +1178,17 @@ static void youngcollection (lua_State *L, global_State *g) { g->gcstate = GCSswpallgc; psurvival = sweepgen(L, g, &g->allgc, g->survival); /* sweep 'survival' */ - sweepgen(L, g, psurvival, g->old); - g->reallyold = g->old; - g->old = *psurvival; /* 'survival' survivals are old now */ + sweepgen(L, g, psurvival, g->old1); + g->reallyold = g->old1; + g->old1 = *psurvival; /* 'survival' survivals are old now */ g->survival = g->allgc; /* all news are survivals */ /* repeat for 'finobj' lists */ psurvival = sweepgen(L, g, &g->finobj, g->finobjsur); /* sweep 'survival' */ - sweepgen(L, g, psurvival, g->finobjold); - g->finobjrold = g->finobjold; - g->finobjold = *psurvival; /* 'survival' survivals are old now */ + sweepgen(L, g, psurvival, g->finobjold1); + g->finobjrold = g->finobjold1; + g->finobjold1 = *psurvival; /* 'survival' survivals are old now */ g->finobjsur = g->finobj; /* all news are survivals */ sweepgen(L, g, &g->tobefnz, NULL); @@ -1202,11 +1202,11 @@ static void atomic2gen (lua_State *L, global_State *g) { g->gcstate = GCSswpallgc; sweep2old(L, &g->allgc); /* everything alive now is old */ - g->reallyold = g->old = g->survival = g->allgc; + g->reallyold = g->old1 = g->survival = g->allgc; /* repeat for 'finobj' lists */ sweep2old(L, &g->finobj); - g->finobjrold = g->finobjold = g->finobjsur = g->finobj; + g->finobjrold = g->finobjold1 = g->finobjsur = g->finobj; sweep2old(L, &g->tobefnz); @@ -1239,10 +1239,10 @@ static lu_mem entergen (lua_State *L, global_State *g) { */ static void enterinc (global_State *g) { whitelist(g, g->allgc); - g->reallyold = g->old = g->survival = NULL; + g->reallyold = g->old1 = g->survival = NULL; whitelist(g, g->finobj); whitelist(g, g->tobefnz); - g->finobjrold = g->finobjold = g->finobjsur = NULL; + g->finobjrold = g->finobjold1 = g->finobjsur = NULL; g->gcstate = GCSpause; g->gckind = KGC_INC; g->lastatomic = 0; diff --git a/lstate.c b/lstate.c index 06fa13d72c..38a2b45a87 100644 --- a/lstate.c +++ b/lstate.c @@ -413,8 +413,8 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { g->gckind = KGC_INC; g->gcemergency = 0; g->finobj = g->tobefnz = g->fixedgc = NULL; - g->survival = g->old = g->reallyold = NULL; - g->finobjsur = g->finobjold = g->finobjrold = NULL; + g->survival = g->old1 = g->reallyold = NULL; + g->finobjsur = g->finobjold1 = g->finobjrold = NULL; g->sweepgc = NULL; g->gray = g->grayagain = NULL; g->weak = g->ephemeron = g->allweak = NULL; diff --git a/lstate.h b/lstate.h index 0c545ec5b9..c02b4c8b6b 100644 --- a/lstate.h +++ b/lstate.h @@ -32,13 +32,20 @@ ** ** 'allgc' -> 'survival': new objects; ** 'survival' -> 'old': objects that survived one collection; -** 'old' -> 'reallyold': objects that became old in last collection; +** 'old1' -> 'reallyold': objects that became old in last collection; ** 'reallyold' -> NULL: objects old for more than one cycle. ** ** 'finobj' -> 'finobjsur': new objects marked for finalization; -** 'finobjsur' -> 'finobjold': survived """"; -** 'finobjold' -> 'finobjrold': just old """"; +** 'finobjsur' -> 'finobjold1': survived """"; +** 'finobjold1' -> 'finobjrold': just old """"; ** 'finobjrold' -> NULL: really old """". +** +** All lists can contain elements older than their main ages, due +** to 'luaC_checkfinalizer' and 'udata2finalize', which move +** objects between the normal lists and the "marked for finalization" +** lists. Moreover, barriers can age young objects in young lists as +** OLD0, which then become OLD1. However, a list never contains +** elements younger than their main ages. */ /* @@ -257,10 +264,10 @@ typedef struct global_State { GCObject *fixedgc; /* list of objects not to be collected */ /* fields for generational collector */ GCObject *survival; /* start of objects that survived one GC cycle */ - GCObject *old; /* start of old objects */ - GCObject *reallyold; /* old objects with more than one cycle */ + GCObject *old1; /* start of old1 objects */ + GCObject *reallyold; /* objects more than one cycle old ("really old") */ GCObject *finobjsur; /* list of survival objects with finalizers */ - GCObject *finobjold; /* list of old objects with finalizers */ + GCObject *finobjold1; /* list of old1 objects with finalizers */ GCObject *finobjrold; /* list of really old objects with finalizers */ struct lua_State *twups; /* list of threads with open upvalues */ lua_CFunction panic; /* to be called in unprotected errors */ diff --git a/ltests.c b/ltests.c index a4e5d28216..a13714d6de 100644 --- a/ltests.c +++ b/ltests.c @@ -586,10 +586,10 @@ int lua_checkmemory (lua_State *L) { /* check 'allgc' list */ maybedead = (GCSatomic < g->gcstate && g->gcstate <= GCSswpallgc); - checklist(g, maybedead, 0, g->allgc, g->survival, g->old, g->reallyold); + checklist(g, maybedead, 0, g->allgc, g->survival, g->old1, g->reallyold); /* check 'finobj' list */ - checklist(g, 0, 1, g->finobj, g->finobjsur, g->finobjold, g->finobjrold); + checklist(g, 0, 1, g->finobj, g->finobjsur, g->finobjold1, g->finobjrold); /* check 'tobefnz' list */ for (o = g->tobefnz; o != NULL; o = o->next) { From 0dc5deca1c0182a4a3db2fcfd7bc721f27fb352b Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 29 Jul 2020 17:05:47 -0300 Subject: [PATCH 0598/1145] Optimization in 'markold' OLD1 objects can be potentially anywhere in the 'allgc' list (up to 'reallyold'), but frequently they are all after 'old1' (natural evolution of survivals) or do not exist at all (when all objects die young). So, instead of 'markold' starts looking for them always from the start of 'allgc', the collector keeps an extra pointer, 'firstold1', that points to the first OLD1 object in the 'allgc' list, or is NULL if there are no OLD1 objects in that list. --- lgc.c | 59 ++++++++++++++++++++++++++++++++++-------------- lstate.c | 2 +- lstate.h | 18 ++++++++++++++- testes/gengc.lua | 16 +++++++++++++ 4 files changed, 76 insertions(+), 19 deletions(-) diff --git a/lgc.c b/lgc.c index 347b6778a5..9973c9dbcf 100644 --- a/lgc.c +++ b/lgc.c @@ -859,6 +859,8 @@ static GCObject *udata2finalize (global_State *g) { resetbit(o->marked, FINALIZEDBIT); /* object is "normal" again */ if (issweepphase(g)) makewhite(g, o); /* "sweep" object */ + else if (getage(o) == G_OLD1) + g->firstold1 = o; /* it is the first OLD1 object in the list */ return o; } @@ -956,6 +958,27 @@ static void separatetobefnz (global_State *g, int all) { } +/* +** If pointer 'p' points to 'o', move it to the next element. +*/ +static void checkpointer (GCObject **p, GCObject *o) { + if (o == *p) + *p = o->next; +} + + +/* +** Correct pointers to objects inside 'allgc' list when +** object 'o' is being removed from the list. +*/ +static void correctpointers (global_State *g, GCObject *o) { + checkpointer(&g->survival, o); + checkpointer(&g->old1, o); + checkpointer(&g->reallyold, o); + checkpointer(&g->firstold1, o); +} + + /* ** if object 'o' has a finalizer, remove it from 'allgc' list (must ** search the list to find it) and link it in 'finobj' list. @@ -972,14 +995,8 @@ void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt) { if (g->sweepgc == &o->next) /* should not remove 'sweepgc' object */ g->sweepgc = sweeptolive(L, g->sweepgc); /* change 'sweepgc' */ } - else { /* correct pointers into 'allgc' list, if needed */ - if (o == g->survival) - g->survival = o->next; - if (o == g->old1) - g->old1 = o->next; - if (o == g->reallyold) - g->reallyold = o->next; - } + else + correctpointers(g, o); /* search for pointer pointing to 'o' */ for (p = &g->allgc; *p != o; p = &(*p)->next) { /* empty */ } *p = o->next; /* remove 'o' from 'allgc' list */ @@ -1033,7 +1050,7 @@ static void sweep2old (lua_State *L, GCObject **p) { ** will also remove objects turned white here from any gray list. */ static GCObject **sweepgen (lua_State *L, global_State *g, GCObject **p, - GCObject *limit) { + GCObject *limit, GCObject **pfirstold1) { static const lu_byte nextage[] = { G_SURVIVAL, /* from G_NEW */ G_OLD1, /* from G_SURVIVAL */ @@ -1056,8 +1073,11 @@ static GCObject **sweepgen (lua_State *L, global_State *g, GCObject **p, int marked = curr->marked & maskgcbits; /* erase GC bits */ curr->marked = cast_byte(marked | G_SURVIVAL | white); } - else /* all other objects will be old, and so keep their color */ + else { /* all other objects will be old, and so keep their color */ setage(curr, nextage[getage(curr)]); + if (getage(curr) == G_OLD1 && *pfirstold1 == NULL) + *pfirstold1 = curr; /* first OLD1 object in the list */ + } p = &curr->next; /* go to next element */ } } @@ -1169,30 +1189,34 @@ static void finishgencycle (lua_State *L, global_State *g) { */ static void youngcollection (lua_State *L, global_State *g) { GCObject **psurvival; /* to point to first non-dead survival object */ + GCObject *dummy; /* dummy out parameter to 'sweepgen' */ lua_assert(g->gcstate == GCSpropagate); - markold(g, g->allgc, g->reallyold); + if (g->firstold1) { /* are there OLD1 objects? */ + markold(g, g->firstold1, g->reallyold); /* mark them */ + g->firstold1 = NULL; /* no more OLD1 objects (for now) */ + } markold(g, g->finobj, g->finobjrold); atomic(L); /* sweep nursery and get a pointer to its last live element */ g->gcstate = GCSswpallgc; - psurvival = sweepgen(L, g, &g->allgc, g->survival); + psurvival = sweepgen(L, g, &g->allgc, g->survival, &g->firstold1); /* sweep 'survival' */ - sweepgen(L, g, psurvival, g->old1); + sweepgen(L, g, psurvival, g->old1, &g->firstold1); g->reallyold = g->old1; g->old1 = *psurvival; /* 'survival' survivals are old now */ g->survival = g->allgc; /* all news are survivals */ /* repeat for 'finobj' lists */ - psurvival = sweepgen(L, g, &g->finobj, g->finobjsur); + dummy = NULL; /* no 'firstold1' optimization for 'finobj' lists */ + psurvival = sweepgen(L, g, &g->finobj, g->finobjsur, &dummy); /* sweep 'survival' */ - sweepgen(L, g, psurvival, g->finobjold1); + sweepgen(L, g, psurvival, g->finobjold1, &dummy); g->finobjrold = g->finobjold1; g->finobjold1 = *psurvival; /* 'survival' survivals are old now */ g->finobjsur = g->finobj; /* all news are survivals */ - sweepgen(L, g, &g->tobefnz, NULL); - + sweepgen(L, g, &g->tobefnz, NULL, &dummy); finishgencycle(L, g); } @@ -1203,6 +1227,7 @@ static void atomic2gen (lua_State *L, global_State *g) { sweep2old(L, &g->allgc); /* everything alive now is old */ g->reallyold = g->old1 = g->survival = g->allgc; + g->firstold1 = NULL; /* there are no OLD1 objects anywhere */ /* repeat for 'finobj' lists */ sweep2old(L, &g->finobj); diff --git a/lstate.c b/lstate.c index 38a2b45a87..86b3761ffc 100644 --- a/lstate.c +++ b/lstate.c @@ -413,7 +413,7 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { g->gckind = KGC_INC; g->gcemergency = 0; g->finobj = g->tobefnz = g->fixedgc = NULL; - g->survival = g->old1 = g->reallyold = NULL; + g->firstold1 = g->survival = g->old1 = g->reallyold = NULL; g->finobjsur = g->finobjold1 = g->finobjrold = NULL; g->sweepgc = NULL; g->gray = g->grayagain = NULL; diff --git a/lstate.h b/lstate.h index c02b4c8b6b..697d73b27e 100644 --- a/lstate.h +++ b/lstate.h @@ -46,6 +46,15 @@ ** lists. Moreover, barriers can age young objects in young lists as ** OLD0, which then become OLD1. However, a list never contains ** elements younger than their main ages. +** +** The generational collector also uses a pointer 'firstold1', which +** points to the first OLD1 object in the list. It is used to optimize +** 'markold'. (Potentially OLD1 objects can be anywhere between 'allgc' +** and 'reallyold', but often the list has no OLD1 objects or they are +** after 'old1'.) Note the difference between it and 'old1': +** 'firstold1': no OLD1 objects before this point; there can be all +** ages after it. +** 'old1': no objects younger than OLD1 after this point. */ /* @@ -54,7 +63,7 @@ ** can become gray have such a field. The field is not the same ** in all objects, but it always has this name.) Any gray object ** must belong to one of these lists, and all objects in these lists -** must be gray: +** must be gray (with one exception explained below): ** ** 'gray': regular gray objects, still waiting to be visited. ** 'grayagain': objects that must be revisited at the atomic phase. @@ -65,6 +74,12 @@ ** 'weak': tables with weak values to be cleared; ** 'ephemeron': ephemeron tables with white->white entries; ** 'allweak': tables with weak keys and/or weak values to be cleared. +** +** The exception to that "gray rule" is the TOUCHED2 objects in +** generational mode. Those objects stay in a gray list (because they +** must be visited again at the end of the cycle), but they are marked +** black (because assignments to them must activate barriers, to move +** them back to TOUCHED1). */ @@ -266,6 +281,7 @@ typedef struct global_State { GCObject *survival; /* start of objects that survived one GC cycle */ GCObject *old1; /* start of old1 objects */ GCObject *reallyold; /* objects more than one cycle old ("really old") */ + GCObject *firstold1; /* first OLD1 object in the list (if any) */ GCObject *finobjsur; /* list of survival objects with finalizers */ GCObject *finobjold1; /* list of old1 objects with finalizers */ GCObject *finobjrold; /* list of really old objects with finalizers */ diff --git a/testes/gengc.lua b/testes/gengc.lua index 7a7dabdd4b..93b5afd7e3 100644 --- a/testes/gengc.lua +++ b/testes/gengc.lua @@ -37,6 +37,22 @@ do end +do + -- ensure that 'firstold1' is corrected when object is removed from + -- the 'allgc' list + local function foo () end + local old = {10} + collectgarbage() -- make 'old' old + assert(not T or T.gcage(old) == "old") + setmetatable(old, {}) -- new table becomes OLD0 (barrier) + assert(not T or T.gcage(getmetatable(old)) == "old0") + collectgarbage("step", 0) -- new table becomes OLD1 and firstold1 + assert(not T or T.gcage(getmetatable(old)) == "old1") + setmetatable(getmetatable(old), {__gc = foo}) -- get it out of allgc list + collectgarbage("step", 0) -- should not seg. fault +end + + do -- bug in 5.4.0 -- When an object aged OLD1 is finalized, it is moved from the list -- 'finobj' to the *beginning* of the list 'allgc', but that part of the From b9b554e0f68726b19274209ea6ce910b7e9f5fbf Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 3 Aug 2020 13:22:57 -0300 Subject: [PATCH 0599/1145] Clearer handling of gray lists when entering generational mode When entering generational mode, all objects are old. So, the only objects that need to be in a gray list are threads, which can be assigned without barriers. Changes in anything else (e.g., weak tables) will trigger barriers that, if needed, will add the object to a gray list. --- lgc.c | 36 ++++++++++++++++++++++++++++------ ltests.c | 60 ++++++++++++++++++++++++++++++++++++++++++++------------ ltests.h | 1 + 3 files changed, 79 insertions(+), 18 deletions(-) diff --git a/lgc.c b/lgc.c index 9973c9dbcf..5e8c02d346 100644 --- a/lgc.c +++ b/lgc.c @@ -368,12 +368,17 @@ static int remarkupvals (global_State *g) { } +static void cleargraylists (global_State *g) { + g->gray = g->grayagain = NULL; + g->weak = g->allweak = g->ephemeron = NULL; +} + + /* ** mark root set and reset all gray lists, to start a new collection */ static void restartcollection (global_State *g) { - g->gray = g->grayagain = NULL; - g->weak = g->allweak = g->ephemeron = NULL; + cleargraylists(g); markobject(g, g->mainthread); markvalue(g, &g->l_registry); markmt(g); @@ -1019,19 +1024,30 @@ static void setpause (global_State *g); /* -** Sweep a list of objects, deleting dead ones and turning -** the non dead to old (without changing their colors). +** Sweep a list of objects to enter generational mode. Deletes dead +** objects and turns the non dead to old. All non-dead threads---which +** are now old---must be in a gray list. Everything else is not in a +** gray list. +** */ static void sweep2old (lua_State *L, GCObject **p) { GCObject *curr; + global_State *g = G(L); while ((curr = *p) != NULL) { if (iswhite(curr)) { /* is 'curr' dead? */ - lua_assert(isdead(G(L), curr)); + lua_assert(isdead(g, curr)); *p = curr->next; /* remove 'curr' from list */ freeobj(L, curr); /* erase 'curr' */ } else { /* all surviving objects become old */ setage(curr, G_OLD); + if (curr->tt == LUA_VTHREAD) { /* threads must be watched */ + lua_State *th = gco2th(curr); + linkgclist(th, g->grayagain); /* insert into 'grayagain' list */ + black2gray(th); /* OK if already gray */ + } + else /* everything else is black */ + gray2black(curr); /* OK if already black */ p = &curr->next; /* go to next element */ } } @@ -1221,7 +1237,14 @@ static void youngcollection (lua_State *L, global_State *g) { } +/* +** Clears all gray lists, sweeps objects, and prepare sublists to enter +** generational mode. The sweeps remove dead objects and turn all +** surviving objects to old. Threads go back to 'grayagain'; everything +** else is turned black (not in any gray list). +*/ static void atomic2gen (lua_State *L, global_State *g) { + cleargraylists(g); /* sweep all elements making them old */ g->gcstate = GCSswpallgc; sweep2old(L, &g->allgc); @@ -1244,7 +1267,8 @@ static void atomic2gen (lua_State *L, global_State *g) { /* ** Enter generational mode. Must go until the end of an atomic cycle -** to ensure that all threads and weak tables are in the gray lists. +** to ensure that all objects are correctly marked and weak tables +** are cleared. ** Then, turn all objects into old and finishes the collection. */ static lu_mem entergen (lua_State *L, global_State *g) { diff --git a/ltests.c b/ltests.c index a13714d6de..c04217813c 100644 --- a/ltests.c +++ b/ltests.c @@ -186,7 +186,8 @@ typedef union Header { Memcontrol l_memcontrol = - {0UL, 0UL, 0UL, 0UL, (~0UL), {0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL}}; + {0, 0UL, 0UL, 0UL, 0UL, (~0UL), + {0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL}}; static void freeblock (Memcontrol *mc, Header *block) { @@ -225,6 +226,10 @@ void *debug_realloc (void *ud, void *b, size_t oldsize, size_t size) { freeblock(mc, block); return NULL; } + if (mc->failnext) { + mc->failnext = 0; + return NULL; /* fake a single memory allocation error */ + } if (mc->countlimit != ~0UL && size != oldsize) { /* count limit in use? */ if (mc->countlimit == 0) return NULL; /* fake a memory allocation error */ @@ -513,10 +518,12 @@ static void checkobject (global_State *g, GCObject *o, int maybedead, } -static void checkgraylist (global_State *g, GCObject *o) { +static lu_mem checkgraylist (global_State *g, GCObject *o) { + int total = 0; /* count number of elements in the list */ ((void)g); /* better to keep it available if we need to print an object */ while (o) { lua_assert(isgray(o) || getage(o) == G_TOUCHED2); + total++; switch (o->tt) { case LUA_VTABLE: o = gco2t(o)->gclist; break; case LUA_VLCL: o = gco2lcl(o)->gclist; break; @@ -530,40 +537,54 @@ static void checkgraylist (global_State *g, GCObject *o) { default: lua_assert(0); /* other objects cannot be in a gray list */ } } + return total; } /* ** Check objects in gray lists. */ -static void checkgrays (global_State *g) { - if (!keepinvariant(g)) return; - checkgraylist(g, g->gray); - checkgraylist(g, g->grayagain); - checkgraylist(g, g->weak); - checkgraylist(g, g->ephemeron); +static lu_mem checkgrays (global_State *g) { + int total = 0; /* count number of elements in all lists */ + if (!keepinvariant(g)) return total; + total += checkgraylist(g, g->gray); + total += checkgraylist(g, g->grayagain); + total += checkgraylist(g, g->weak); + total += checkgraylist(g, g->allweak); + total += checkgraylist(g, g->ephemeron); + return total; } -static void checklist (global_State *g, int maybedead, int tof, +/* Increment 't' if 'o' should be in a gray list */ +#define incifingray(o,t) \ + if (isgray(o) || getage(o) == G_TOUCHED2) (t)++ + +static lu_mem checklist (global_State *g, int maybedead, int tof, GCObject *newl, GCObject *survival, GCObject *old, GCObject *reallyold) { GCObject *o; + lu_mem total = 0; /* number of object that should be in gray lists */ for (o = newl; o != survival; o = o->next) { checkobject(g, o, maybedead, G_NEW); + incifingray(o, total); lua_assert(!tof == !tofinalize(o)); } for (o = survival; o != old; o = o->next) { checkobject(g, o, 0, G_SURVIVAL); + incifingray(o, total); lua_assert(!tof == !tofinalize(o)); } for (o = old; o != reallyold; o = o->next) { checkobject(g, o, 0, G_OLD1); + incifingray(o, total); lua_assert(!tof == !tofinalize(o)); } for (o = reallyold; o != NULL; o = o->next) { checkobject(g, o, 0, G_OLD); + incifingray(o, total); lua_assert(!tof == !tofinalize(o)); } + return total; } @@ -571,13 +592,15 @@ int lua_checkmemory (lua_State *L) { global_State *g = G(L); GCObject *o; int maybedead; + lu_mem totalin; /* total of objects that are in gray lists */ + lu_mem totalshould; /* total of objects that should be in gray lists */ if (keepinvariant(g)) { lua_assert(!iswhite(g->mainthread)); lua_assert(!iswhite(gcvalue(&g->l_registry))); } lua_assert(!isdead(g, gcvalue(&g->l_registry))); lua_assert(g->sweepgc == NULL || issweepphase(g)); - checkgrays(g); + totalin = checkgrays(g); /* check 'fixedgc' list */ for (o = g->fixedgc; o != NULL; o = o->next) { @@ -586,17 +609,22 @@ int lua_checkmemory (lua_State *L) { /* check 'allgc' list */ maybedead = (GCSatomic < g->gcstate && g->gcstate <= GCSswpallgc); - checklist(g, maybedead, 0, g->allgc, g->survival, g->old1, g->reallyold); + totalshould = checklist(g, maybedead, 0, g->allgc, + g->survival, g->old1, g->reallyold); /* check 'finobj' list */ - checklist(g, 0, 1, g->finobj, g->finobjsur, g->finobjold1, g->finobjrold); + totalshould += checklist(g, 0, 1, g->finobj, + g->finobjsur, g->finobjold1, g->finobjrold); /* check 'tobefnz' list */ for (o = g->tobefnz; o != NULL; o = o->next) { checkobject(g, o, 0, G_NEW); + incifingray(o, totalshould); lua_assert(tofinalize(o)); lua_assert(o->tt == LUA_VUSERDATA || o->tt == LUA_VTABLE); } + if (keepinvariant(g)) + lua_assert(totalin == totalshould); return 0; } @@ -807,6 +835,13 @@ static int alloc_count (lua_State *L) { l_memcontrol.countlimit = luaL_checkinteger(L, 1); return 0; } + + +static int alloc_failnext (lua_State *L) { + UNUSED(L); + l_memcontrol.failnext = 1; + return 0; +} static int settrick (lua_State *L) { @@ -1864,6 +1899,7 @@ static const struct luaL_Reg tests_funcs[] = { {"makeCfunc", makeCfunc}, {"totalmem", mem_query}, {"alloccount", alloc_count}, + {"allocfailnext", alloc_failnext}, {"trick", settrick}, {"udataval", udataval}, {"unref", unref}, diff --git a/ltests.h b/ltests.h index 1a2d8d28b3..e9219e2973 100644 --- a/ltests.h +++ b/ltests.h @@ -51,6 +51,7 @@ /* memory-allocator control variables */ typedef struct Memcontrol { + int failnext; unsigned long numblocks; unsigned long total; unsigned long maxmem; From 9cf3299fafa41718e3cb260cc94d1d29bba6335b Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 3 Aug 2020 14:28:13 -0300 Subject: [PATCH 0600/1145] Threads don't need to always go to 'grayagain' In incremental mode, threads don't need to be visited again once visited in the atomic phase. In generational mode (where all visits are in the atomic phase), only old threads need to be kept in the 'grayagain' list for the next cycle. --- lgc.c | 47 +++++++++++++++++++++++++++++------------------ 1 file changed, 29 insertions(+), 18 deletions(-) diff --git a/lgc.c b/lgc.c index 5e8c02d346..717f80e829 100644 --- a/lgc.c +++ b/lgc.c @@ -348,8 +348,7 @@ static int remarkupvals (global_State *g) { int work = 0; /* estimate of how much work was done here */ while ((thread = *p) != NULL) { work++; - lua_assert(!isblack(thread)); /* threads are never black */ - if (isgray(thread) && thread->openupval != NULL) + if (!iswhite(thread) && thread->openupval != NULL) p = &thread->twups; /* keep marked thread with upvalues in the list */ else { /* thread is not marked or without upvalues */ UpVal *uv; @@ -600,12 +599,23 @@ static int traverseLclosure (global_State *g, LClosure *cl) { /* ** Traverse a thread, marking the elements in the stack up to its top -** and cleaning the rest of the stack in the final traversal. -** That ensures that the entire stack have valid (non-dead) objects. +** and cleaning the rest of the stack in the final traversal. That +** ensures that the entire stack have valid (non-dead) objects. +** Threads have no barriers. In gen. mode, old threads must be visited +** at every cycle, because they might point to young objects. In inc. +** mode, the thread can still be modified before the end of the cycle, +** and therefore it must be visited again in the atomic phase. To ensure +** these visits, threads must return to a gray list if they are not new +** (which can only happen in generational mode) or if the traverse is in +** the propagate phase (which can only happen in incremental mode). */ static int traversethread (global_State *g, lua_State *th) { UpVal *uv; StkId o = th->stack; + if (isold(th) || g->gcstate == GCSpropagate) { + linkgclist(th, g->grayagain); /* insert into 'grayagain' list */ + black2gray(th); + } if (o == NULL) return 1; /* stack not completely built yet */ lua_assert(g->gcstate == GCSatomic || @@ -644,12 +654,7 @@ static lu_mem propagatemark (global_State *g) { case LUA_VLCL: return traverseLclosure(g, gco2lcl(o)); case LUA_VCCL: return traverseCclosure(g, gco2ccl(o)); case LUA_VPROTO: return traverseproto(g, gco2p(o)); - case LUA_VTHREAD: { - lua_State *th = gco2th(o); - linkgclist(th, g->grayagain); /* insert into 'grayagain' list */ - black2gray(o); - return traversethread(g, th); - } + case LUA_VTHREAD: return traversethread(g, gco2th(o)); default: lua_assert(0); return 0; } } @@ -1028,7 +1033,6 @@ static void setpause (global_State *g); ** objects and turns the non dead to old. All non-dead threads---which ** are now old---must be in a gray list. Everything else is not in a ** gray list. -** */ static void sweep2old (lua_State *L, GCObject **p) { GCObject *curr; @@ -1139,10 +1143,16 @@ static GCObject **correctgraylist (GCObject **p) { goto remain; /* keep non-white threads on the list */ } else { /* everything else is removed */ - lua_assert(isold(curr)); /* young objects should be white */ - if (getage(curr) == G_TOUCHED2) /* advance from G_TOUCHED2... */ - changeage(curr, G_TOUCHED2, G_OLD); /* ... to G_OLD */ - gray2black(curr); /* make object black */ + lua_assert(isold(curr)); /* young objects should be white here */ + if (getage(curr) == G_TOUCHED2) { /* advance from TOUCHED2... */ + changeage(curr, G_TOUCHED2, G_OLD); /* ... to OLD */ + lua_assert(isblack(curr)); /* TOUCHED2 objects are always black */ + } + else { + /* everything else in a gray list should be gray */ + lua_assert(isgray(curr)); + gray2black(curr); /* make object black (to be removed) */ + } goto remove; } remove: *p = *next; continue; @@ -1207,11 +1217,12 @@ static void youngcollection (lua_State *L, global_State *g) { GCObject **psurvival; /* to point to first non-dead survival object */ GCObject *dummy; /* dummy out parameter to 'sweepgen' */ lua_assert(g->gcstate == GCSpropagate); - if (g->firstold1) { /* are there OLD1 objects? */ + if (g->firstold1) { /* are there regular OLD1 objects? */ markold(g, g->firstold1, g->reallyold); /* mark them */ g->firstold1 = NULL; /* no more OLD1 objects (for now) */ } markold(g, g->finobj, g->finobjrold); + markold(g, g->tobefnz, NULL); atomic(L); /* sweep nursery and get a pointer to its last live element */ @@ -1268,8 +1279,8 @@ static void atomic2gen (lua_State *L, global_State *g) { /* ** Enter generational mode. Must go until the end of an atomic cycle ** to ensure that all objects are correctly marked and weak tables -** are cleared. -** Then, turn all objects into old and finishes the collection. +** are cleared. Then, turn all objects into old and finishes the +** collection. */ static lu_mem entergen (lua_State *L, global_State *g) { lu_mem numobjs; From 68109afcdb59a7eeb75bacf055abce3e56a53645 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 3 Aug 2020 16:05:10 -0300 Subject: [PATCH 0601/1145] Detail (in asserts) Macro 'checkconsistency' replaced by the similar 'checkliveness". --- lgc.c | 5 +---- lobject.h | 3 ++- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/lgc.c b/lgc.c index 717f80e829..cb820f9c3d 100644 --- a/lgc.c +++ b/lgc.c @@ -80,16 +80,13 @@ #define keyiswhite(n) (keyiscollectable(n) && iswhite(gckey(n))) -#define checkconsistency(obj) \ - lua_longassert(!iscollectable(obj) || righttt(obj)) - /* ** Protected access to objects in values */ #define gcvalueN(o) (iscollectable(o) ? gcvalue(o) : NULL) -#define markvalue(g,o) { checkconsistency(o); \ +#define markvalue(g,o) { checkliveness(g->mainthread,o); \ if (valiswhite(o)) reallymarkobject(g,gcvalue(o)); } #define markkey(g, n) { if keyiswhite(n) reallymarkobject(g,gckey(n)); } diff --git a/lobject.h b/lobject.h index 04a81d3de4..1620223090 100644 --- a/lobject.h +++ b/lobject.h @@ -96,7 +96,8 @@ typedef struct TValue { /* ** Any value being manipulated by the program either is non ** collectable, or the collectable object has the right tag -** and it is not dead. +** and it is not dead. The option 'L == NULL' allows other +** macros using this one to be used where L is not available. */ #define checkliveness(L,obj) \ ((void)L, lua_longassert(!iscollectable(obj) || \ From 7c3cb71fa48fbe84d9d9c664eb646446fb80898b Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 7 Aug 2020 11:21:44 -0300 Subject: [PATCH 0602/1145] Free bit 7 of GC 'marked' field Tables were using this bit to indicate their array sizes were real ('isrealasize'), but this bit can be useful for tests. Instead, they can use bit 7 of their 'flag' field for that purpose. (There are only six fast-access metamethods.) This 'flag' field only exists in tables, so this use does not affect other types. --- lgc.h | 3 +-- lobject.h | 6 +++--- ltable.c | 2 +- ltable.h | 7 ++++++- ltm.h | 9 +++++++++ testes/events.lua | 11 +++++++++++ 6 files changed, 31 insertions(+), 7 deletions(-) diff --git a/lgc.h b/lgc.h index b972472f8d..f571fd2b33 100644 --- a/lgc.h +++ b/lgc.h @@ -69,8 +69,7 @@ /* ** Layout for bit use in 'marked' field. First three bits are -** used for object "age" in generational mode. Last bit is free -** to be used by respective objects. +** used for object "age" in generational mode. */ #define WHITE0BIT 3 /* object is white (type 0) */ #define WHITE1BIT 4 /* object is white (type 1) */ diff --git a/lobject.h b/lobject.h index 1620223090..a9d45785ec 100644 --- a/lobject.h +++ b/lobject.h @@ -704,9 +704,9 @@ typedef union Node { */ #define BITRAS (1 << 7) -#define isrealasize(t) (!((t)->marked & BITRAS)) -#define setrealasize(t) ((t)->marked &= cast_byte(~BITRAS)) -#define setnorealasize(t) ((t)->marked |= BITRAS) +#define isrealasize(t) (!((t)->flags & BITRAS)) +#define setrealasize(t) ((t)->flags &= cast_byte(~BITRAS)) +#define setnorealasize(t) ((t)->flags |= BITRAS) typedef struct Table { diff --git a/ltable.c b/ltable.c index d7eb69a2e1..5a0d066faa 100644 --- a/ltable.c +++ b/ltable.c @@ -583,7 +583,7 @@ Table *luaH_new (lua_State *L) { GCObject *o = luaC_newobj(L, LUA_VTABLE, sizeof(Table)); Table *t = gco2t(o); t->metatable = NULL; - t->flags = cast_byte(~0); + t->flags = cast_byte(maskflags); /* table has no metamethod fields */ t->array = NULL; t->alimit = 0; setnodevector(L, t, 0); diff --git a/ltable.h b/ltable.h index ebd7f8ec3e..c0060f4b6e 100644 --- a/ltable.h +++ b/ltable.h @@ -15,7 +15,12 @@ #define gnext(n) ((n)->u.next) -#define invalidateTMcache(t) ((t)->flags = 0) +/* +** Clear all bits of fast-access metamethods, which means that the table +** may have any of these metamethods. (First access that fails after the +** clearing will set the bit again.) +*/ +#define invalidateTMcache(t) ((t)->flags &= ~maskflags) /* true when 't' is using 'dummynode' as its hash part */ diff --git a/ltm.h b/ltm.h index 99b545e766..73b833c605 100644 --- a/ltm.h +++ b/ltm.h @@ -45,6 +45,15 @@ typedef enum { } TMS; +/* +** Mask with 1 in all fast-access methods. A 1 in any of these bits +** in the flag of a (meta)table means the metatable does not have the +** corresponding metamethod field. (Bit 7 of the flag is used for +** 'isrealasize'.) +*/ +#define maskflags (~(~0u << (TM_EQ + 1))) + + /* ** Test whether there is no tagmethod. ** (Because tagmethods use raw accesses, the result may be an "empty" nil.) diff --git a/testes/events.lua b/testes/events.lua index 8a01330e1b..17a7366449 100644 --- a/testes/events.lua +++ b/testes/events.lua @@ -305,6 +305,17 @@ t[Set{1,3,5}] = 1 assert(t[Set{1,3,5}] == undef) +do -- test invalidating flags + local mt = {__eq = true} + local a = setmetatable({10}, mt) + local b = setmetatable({10}, mt) + mt.__eq = nil + assert(a ~= b) -- no metamethod + mt.__eq = function (x,y) return x[1] == y[1] end + assert(a == b) -- must use metamethod now +end + + if not T then (Message or print)('\n >>> testC not active: skipping tests for \z userdata <<<\n') From f13dc59416afa8fc93bb3d784d1a73e49e1b5b09 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 7 Aug 2020 14:45:20 -0300 Subject: [PATCH 0603/1145] Better tests for gray lists Test uses an extra bit in 'marked' to mark all elements in gray lists and then check against elements colored gray. --- lgc.h | 5 ++++- ltests.c | 40 +++++++++++++++++++++++++++++++--------- testes/nextvar.lua | 2 ++ 3 files changed, 37 insertions(+), 10 deletions(-) diff --git a/lgc.h b/lgc.h index f571fd2b33..0508cd1dab 100644 --- a/lgc.h +++ b/lgc.h @@ -69,13 +69,16 @@ /* ** Layout for bit use in 'marked' field. First three bits are -** used for object "age" in generational mode. +** used for object "age" in generational mode. Last bit is used +** by tests. */ #define WHITE0BIT 3 /* object is white (type 0) */ #define WHITE1BIT 4 /* object is white (type 1) */ #define BLACKBIT 5 /* object is black */ #define FINALIZEDBIT 6 /* object has been marked for finalization */ +#define TESTBIT 7 + #define WHITEBITS bit2mask(WHITE0BIT, WHITE1BIT) diff --git a/ltests.c b/ltests.c index c04217813c..04e8a00a4d 100644 --- a/ltests.c +++ b/ltests.c @@ -522,7 +522,11 @@ static lu_mem checkgraylist (global_State *g, GCObject *o) { int total = 0; /* count number of elements in the list */ ((void)g); /* better to keep it available if we need to print an object */ while (o) { - lua_assert(isgray(o) || getage(o) == G_TOUCHED2); + lua_assert(!!isgray(o) ^ (getage(o) == G_TOUCHED2)); + //lua_assert(isgray(o) || getage(o) == G_TOUCHED2); + lua_assert(!testbit(o->marked, TESTBIT)); + if (keepinvariant(g)) + l_setbit(o->marked, TESTBIT); /* mark that object is in a gray list */ total++; switch (o->tt) { case LUA_VTABLE: o = gco2t(o)->gclist; break; @@ -556,9 +560,27 @@ static lu_mem checkgrays (global_State *g) { } -/* Increment 't' if 'o' should be in a gray list */ -#define incifingray(o,t) \ - if (isgray(o) || getage(o) == G_TOUCHED2) (t)++ +/* +** Check whether 'o' should be in a gray list. If so, increment +** 'count' and check its TESTBIT. (It must have been previously set by +** 'checkgraylist'.) +*/ +static void incifingray (global_State *g, GCObject *o, lu_mem *count) { + if (!keepinvariant(g)) + return; /* gray lists not being kept in these phases */ + if (o->tt == LUA_VUPVAL) { + /* only open upvalues can be gray */ + lua_assert(!isgray(o) || upisopen(gco2upv(o))); + return; /* upvalues are never in gray lists */ + } + /* these are the ones that must be in gray lists */ + if (isgray(o) || getage(o) == G_TOUCHED2) { + (*count)++; + lua_assert(testbit(o->marked, TESTBIT)); + resetbit(o->marked, TESTBIT); /* prepare for next cycle */ + } +} + static lu_mem checklist (global_State *g, int maybedead, int tof, GCObject *newl, GCObject *survival, GCObject *old, GCObject *reallyold) { @@ -566,22 +588,22 @@ static lu_mem checklist (global_State *g, int maybedead, int tof, lu_mem total = 0; /* number of object that should be in gray lists */ for (o = newl; o != survival; o = o->next) { checkobject(g, o, maybedead, G_NEW); - incifingray(o, total); + incifingray(g, o, &total); lua_assert(!tof == !tofinalize(o)); } for (o = survival; o != old; o = o->next) { checkobject(g, o, 0, G_SURVIVAL); - incifingray(o, total); + incifingray(g, o, &total); lua_assert(!tof == !tofinalize(o)); } for (o = old; o != reallyold; o = o->next) { checkobject(g, o, 0, G_OLD1); - incifingray(o, total); + incifingray(g, o, &total); lua_assert(!tof == !tofinalize(o)); } for (o = reallyold; o != NULL; o = o->next) { checkobject(g, o, 0, G_OLD); - incifingray(o, total); + incifingray(g, o, &total); lua_assert(!tof == !tofinalize(o)); } return total; @@ -619,7 +641,7 @@ int lua_checkmemory (lua_State *L) { /* check 'tobefnz' list */ for (o = g->tobefnz; o != NULL; o = o->next) { checkobject(g, o, 0, G_NEW); - incifingray(o, totalshould); + incifingray(g, o, &totalshould); lua_assert(tofinalize(o)); lua_assert(o->tt == LUA_VUSERDATA || o->tt == LUA_VTABLE); } diff --git a/testes/nextvar.lua b/testes/nextvar.lua index 73af77dde6..a16d557b67 100644 --- a/testes/nextvar.lua +++ b/testes/nextvar.lua @@ -88,6 +88,7 @@ for _, sa in ipairs(sizes) do -- 'sa' is size of the array part arr[1 + sa + sh + 1] = "}" local prog = table.concat(arr) local f = assert(load(prog)) + collectgarbage("stop") f() -- call once to ensure stack space -- make sure table is not resized after being created if sa == 0 or sh == 0 then @@ -97,6 +98,7 @@ for _, sa in ipairs(sizes) do -- 'sa' is size of the array part end local t = f() T.alloccount(); + collectgarbage("restart") assert(#t == sa) check(t, sa, mp2(sh)) end From 65141832d29824ebfbf26e8af9e6e4b8549518d4 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 7 Aug 2020 14:53:38 -0300 Subject: [PATCH 0604/1145] Open upvalues should be gray when entering gen. mode Open upvalues are never black; so, when entering generational mode, they must be colored gray, not black. --- lgc.c | 21 +++++++++++++-------- lstate.h | 14 ++++++++------ 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/lgc.c b/lgc.c index cb820f9c3d..7d62fafbbb 100644 --- a/lgc.c +++ b/lgc.c @@ -267,21 +267,22 @@ GCObject *luaC_newobj (lua_State *L, int tt, size_t sz) { ** Mark an object. Userdata, strings, and closed upvalues are visited ** and turned black here. Other objects are marked gray and added ** to appropriate list to be visited (and turned black) later. (Open -** upvalues are already linked in 'headuv' list. They are kept gray -** to avoid barriers, as their values will be revisited by the thread.) +** upvalues are already indirectly linked through the 'twups' list. They +** are kept gray to avoid barriers, as their values will be revisited by +** the thread or by 'remarkupvals'.) */ static void reallymarkobject (global_State *g, GCObject *o) { white2gray(o); switch (o->tt) { case LUA_VSHRSTR: case LUA_VLNGSTR: { - gray2black(o); + gray2black(o); /* nothing to visit */ break; } case LUA_VUPVAL: { UpVal *uv = gco2upv(o); if (!upisopen(uv)) /* open upvalues are kept gray */ - gray2black(o); + gray2black(o); /* closed upvalues are visited here */ markvalue(g, uv->v); /* mark its content */ break; } @@ -296,7 +297,7 @@ static void reallymarkobject (global_State *g, GCObject *o) { } /* FALLTHROUGH */ case LUA_VLCL: case LUA_VCCL: case LUA_VTABLE: case LUA_VTHREAD: case LUA_VPROTO: { - linkobjgclist(o, g->gray); + linkobjgclist(o, g->gray); /* to be visited later */ break; } default: lua_assert(0); break; @@ -355,8 +356,10 @@ static int remarkupvals (global_State *g) { for (uv = thread->openupval; uv != NULL; uv = uv->u.open.next) { lua_assert(getage(uv) <= getage(thread)); work++; - if (!iswhite(uv)) /* upvalue already visited? */ + if (!iswhite(uv)) { /* upvalue already visited? */ + lua_assert(upisopen(uv) && isgray(uv)); markvalue(g, uv->v); /* mark its value */ + } } } } @@ -1028,8 +1031,8 @@ static void setpause (global_State *g); /* ** Sweep a list of objects to enter generational mode. Deletes dead ** objects and turns the non dead to old. All non-dead threads---which -** are now old---must be in a gray list. Everything else is not in a -** gray list. +** are now old---must be in a gray list. Everything else is not in a +** gray list. Open upvalues are also kept gray. */ static void sweep2old (lua_State *L, GCObject **p) { GCObject *curr; @@ -1047,6 +1050,8 @@ static void sweep2old (lua_State *L, GCObject **p) { linkgclist(th, g->grayagain); /* insert into 'grayagain' list */ black2gray(th); /* OK if already gray */ } + else if (curr->tt == LUA_VUPVAL && upisopen(gco2upv(curr))) + black2gray(curr); /* open upvalues are always gray */ else /* everything else is black */ gray2black(curr); /* OK if already black */ p = &curr->next; /* go to next element */ diff --git a/lstate.h b/lstate.h index 697d73b27e..1b6bcdf8f7 100644 --- a/lstate.h +++ b/lstate.h @@ -63,7 +63,7 @@ ** can become gray have such a field. The field is not the same ** in all objects, but it always has this name.) Any gray object ** must belong to one of these lists, and all objects in these lists -** must be gray (with one exception explained below): +** must be gray (with two exceptions explained below): ** ** 'gray': regular gray objects, still waiting to be visited. ** 'grayagain': objects that must be revisited at the atomic phase. @@ -75,11 +75,13 @@ ** 'ephemeron': ephemeron tables with white->white entries; ** 'allweak': tables with weak keys and/or weak values to be cleared. ** -** The exception to that "gray rule" is the TOUCHED2 objects in -** generational mode. Those objects stay in a gray list (because they -** must be visited again at the end of the cycle), but they are marked -** black (because assignments to them must activate barriers, to move -** them back to TOUCHED1). +** The exceptions to that "gray rule" are: +** - TOUCHED2 objects in generational mode stay in a gray list (because +** they must be visited again at the end of the cycle), but they are +** marked black because assignments to them must activate barriers (to +** move them back to TOUCHED1). +** - Open upvales are kept gray to avoid barriers, but they stay out +** of gray lists. (They don't even have a 'gclist' field.) */ From f7ce7e5faae69fcab0126d8bfd34b685f1dcb019 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 13 Aug 2020 14:31:27 -0300 Subject: [PATCH 0605/1145] TOUCHED2 objects are not always black This commit fixes a bug introduced in commit 9cf3299fa. TOUCHED2 objects are always black while the mutator runs, but they can become temporarily gray inside a minor collection (e.g., if the object is a weak table). --- lgc.c | 10 ++-------- testes/gengc.lua | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/lgc.c b/lgc.c index 7d62fafbbb..28c3171597 100644 --- a/lgc.c +++ b/lgc.c @@ -1146,15 +1146,9 @@ static GCObject **correctgraylist (GCObject **p) { } else { /* everything else is removed */ lua_assert(isold(curr)); /* young objects should be white here */ - if (getage(curr) == G_TOUCHED2) { /* advance from TOUCHED2... */ + if (getage(curr) == G_TOUCHED2) /* advance from TOUCHED2... */ changeage(curr, G_TOUCHED2, G_OLD); /* ... to OLD */ - lua_assert(isblack(curr)); /* TOUCHED2 objects are always black */ - } - else { - /* everything else in a gray list should be gray */ - lua_assert(isgray(curr)); - gray2black(curr); /* make object black (to be removed) */ - } + gray2black(curr); /* make object black (to be removed) */ goto remove; } remove: *p = *next; continue; diff --git a/testes/gengc.lua b/testes/gengc.lua index 93b5afd7e3..3d4f67f8bc 100644 --- a/testes/gengc.lua +++ b/testes/gengc.lua @@ -106,6 +106,23 @@ do -- another bug in 5.4.0 end +do -- bug introduced in commit 9cf3299fa + local t = setmetatable({}, {__mode = "kv"}) -- all-weak table + collectgarbage() -- full collection + assert(not T or T.gcage(t) == "old") + t[1] = {10} + assert(not T or (T.gcage(t) == "touched1" and T.gccolor(t) == "gray")) + collectgarbage("step", 0) -- minor collection + assert(not T or (T.gcage(t) == "touched2" and T.gccolor(t) == "black")) + collectgarbage("step", 0) -- minor collection + assert(not T or T.gcage(t) == "old") -- t should be black, but it was gray + t[1] = {10} -- no barrier here, so t was still old + collectgarbage("step", 0) -- minor collection + -- t, being old, is ignored by the collection, so it is not cleared + assert(t[1] == nil) -- fails with the bug +end + + if T == nil then (Message or print)('\n >>> testC not active: \z skipping some generational tests <<<\n') From f849885a4b49f2d766e6befc67475c05240023eb Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 13 Aug 2020 15:23:21 -0300 Subject: [PATCH 0606/1145] Small changes in macros that change GC colors - Macro 'gray2black' was renamed 'nw2black' (Non-White to black), as it was already being used on objects that could be already black. - Macros 'white2gray' and 'black2gray' were unified in 'set2gray'; no reason to have two macros when one will do and, again, 'black2gray' was already being used on objects that could be already gray. Moreover, macros 'maskcolors' and 'maskgcbits' were negated to have ones in the described bits, instead of zeros. (This naming seems more intuitive.) --- lfunc.c | 2 +- lgc.c | 54 +++++++++++++++++++++++++++--------------------------- lgc.h | 23 ++++++++++++----------- 3 files changed, 40 insertions(+), 39 deletions(-) diff --git a/lfunc.c b/lfunc.c index f8c3c4467e..88d45328b1 100644 --- a/lfunc.c +++ b/lfunc.c @@ -235,7 +235,7 @@ int luaF_close (lua_State *L, StkId level, int status) { setobj(L, slot, uv->v); /* move value to upvalue slot */ uv->v = slot; /* now current value lives here */ if (!iswhite(uv)) { /* neither white nor dead? */ - gray2black(uv); /* closed upvalues cannot be gray */ + nw2black(uv); /* closed upvalues cannot be gray */ luaC_barrier(L, uv, slot); } } diff --git a/lgc.c b/lgc.c index 28c3171597..be125dd70c 100644 --- a/lgc.c +++ b/lgc.c @@ -60,19 +60,19 @@ #define PAUSEADJ 100 -/* mask to erase all color bits */ -#define maskcolors (~(bitmask(BLACKBIT) | WHITEBITS)) +/* mask with all color bits */ +#define maskcolors (bitmask(BLACKBIT) | WHITEBITS) -/* mask to erase all GC bits */ -#define maskgcbits (maskcolors & ~AGEBITS) +/* mask with all GC bits */ +#define maskgcbits (maskcolors | AGEBITS) /* macro to erase all color bits then set only the current white bit */ #define makewhite(g,x) \ - (x->marked = cast_byte((x->marked & maskcolors) | luaC_white(g))) + (x->marked = cast_byte((x->marked & ~maskcolors) | luaC_white(g))) -#define white2gray(x) resetbits(x->marked, WHITEBITS) -#define black2gray(x) resetbit(x->marked, BLACKBIT) +/* make an object gray (neither white nor black) */ +#define set2gray(x) resetbits(x->marked, maskcolors) #define valiswhite(x) (iscollectable(x) && iswhite(gcvalue(x))) @@ -221,7 +221,7 @@ void luaC_barrierback_ (lua_State *L, GCObject *o) { lua_assert((g->gckind == KGC_GEN) == (isold(o) && getage(o) != G_TOUCHED1)); if (getage(o) != G_TOUCHED2) /* not already in gray list? */ linkobjgclist(o, g->grayagain); /* link it in 'grayagain' */ - black2gray(o); /* make object gray (again) */ + set2gray(o); /* make object gray (again) */ if (isold(o)) /* generational mode? */ setage(o, G_TOUCHED1); /* touched in current cycle */ } @@ -230,7 +230,7 @@ void luaC_barrierback_ (lua_State *L, GCObject *o) { void luaC_fix (lua_State *L, GCObject *o) { global_State *g = G(L); lua_assert(g->allgc == o); /* object must be 1st in 'allgc' list! */ - white2gray(o); /* they will be gray forever */ + set2gray(o); /* they will be gray forever */ setage(o, G_OLD); /* and old forever */ g->allgc = o->next; /* remove object from 'allgc' list */ o->next = g->fixedgc; /* link it to 'fixedgc' list */ @@ -272,17 +272,17 @@ GCObject *luaC_newobj (lua_State *L, int tt, size_t sz) { ** the thread or by 'remarkupvals'.) */ static void reallymarkobject (global_State *g, GCObject *o) { - white2gray(o); + set2gray(o); switch (o->tt) { case LUA_VSHRSTR: case LUA_VLNGSTR: { - gray2black(o); /* nothing to visit */ + nw2black(o); /* nothing to visit */ break; } case LUA_VUPVAL: { UpVal *uv = gco2upv(o); if (!upisopen(uv)) /* open upvalues are kept gray */ - gray2black(o); /* closed upvalues are visited here */ + nw2black(o); /* closed upvalues are visited here */ markvalue(g, uv->v); /* mark its content */ break; } @@ -290,7 +290,7 @@ static void reallymarkobject (global_State *g, GCObject *o) { Udata *u = gco2u(o); if (u->nuvalue == 0) { /* no user values? */ markobjectN(g, u->metatable); /* mark its metatable */ - gray2black(o); /* nothing else to mark */ + nw2black(o); /* nothing else to mark */ break; } /* else... */ @@ -412,7 +412,7 @@ static void genlink_ (global_State *g, GCObject *o, GCObject **pnext) { if (getage(o) == G_TOUCHED1) { /* touched in this cycle? */ *pnext = g->grayagain; /* link it back in 'grayagain' */ g->grayagain = o; - black2gray(o); + set2gray(o); } /* everything else do not need to be linked back */ else if (getage(o) == G_TOUCHED2) changeage(o, G_TOUCHED2, G_OLD); /* advance age */ @@ -497,7 +497,7 @@ static int traverseephemeron (global_State *g, Table *h, int inv) { else if (hasclears) /* table has white keys? */ linkgclist(h, g->allweak); /* may have to clean white keys */ else { - gray2black(h); /* 'genlink' expects black objects */ + nw2black(h); /* 'genlink' expects black objects */ genlink(g, h); /* check whether collector still needs to see it */ } return marked; @@ -531,7 +531,7 @@ static lu_mem traversetable (global_State *g, Table *h) { (cast_void(weakkey = strchr(svalue(mode), 'k')), cast_void(weakvalue = strchr(svalue(mode), 'v')), (weakkey || weakvalue))) { /* is really weak? */ - black2gray(h); /* turn it back to gray, as it probably goes to a list */ + set2gray(h); /* turn it back to gray, as it probably goes to a list */ if (!weakkey) /* strong keys? */ traverseweakvalue(g, h); else if (!weakvalue) /* strong values? */ @@ -614,7 +614,7 @@ static int traversethread (global_State *g, lua_State *th) { StkId o = th->stack; if (isold(th) || g->gcstate == GCSpropagate) { linkgclist(th, g->grayagain); /* insert into 'grayagain' list */ - black2gray(th); + set2gray(th); } if (o == NULL) return 1; /* stack not completely built yet */ @@ -646,7 +646,7 @@ static int traversethread (global_State *g, lua_State *th) { */ static lu_mem propagatemark (global_State *g) { GCObject *o = g->gray; - gray2black(o); + nw2black(o); g->gray = *getgclist(o); /* remove from 'gray' list */ switch (o->tt) { case LUA_VTABLE: return traversetable(g, gco2t(o)); @@ -812,7 +812,7 @@ static GCObject **sweeplist (lua_State *L, GCObject **p, int countin, freeobj(L, curr); /* erase 'curr' */ } else { /* change mark to 'white' */ - curr->marked = cast_byte((marked & maskgcbits) | white); + curr->marked = cast_byte((marked & ~maskgcbits) | white); p = &curr->next; /* go to next element */ } } @@ -1048,12 +1048,12 @@ static void sweep2old (lua_State *L, GCObject **p) { if (curr->tt == LUA_VTHREAD) { /* threads must be watched */ lua_State *th = gco2th(curr); linkgclist(th, g->grayagain); /* insert into 'grayagain' list */ - black2gray(th); /* OK if already gray */ + set2gray(th); } else if (curr->tt == LUA_VUPVAL && upisopen(gco2upv(curr))) - black2gray(curr); /* open upvalues are always gray */ + set2gray(curr); /* open upvalues are always gray */ else /* everything else is black */ - gray2black(curr); /* OK if already black */ + nw2black(curr); p = &curr->next; /* go to next element */ } } @@ -1092,7 +1092,7 @@ static GCObject **sweepgen (lua_State *L, global_State *g, GCObject **p, } else { /* correct mark and age */ if (getage(curr) == G_NEW) { /* new objects go back to white */ - int marked = curr->marked & maskgcbits; /* erase GC bits */ + int marked = curr->marked & ~maskgcbits; /* erase GC bits */ curr->marked = cast_byte(marked | G_SURVIVAL | white); } else { /* all other objects will be old, and so keep their color */ @@ -1115,7 +1115,7 @@ static GCObject **sweepgen (lua_State *L, global_State *g, GCObject **p, static void whitelist (global_State *g, GCObject *p) { int white = luaC_white(g); for (; p != NULL; p = p->next) - p->marked = cast_byte((p->marked & maskgcbits) | white); + p->marked = cast_byte((p->marked & ~maskgcbits) | white); } @@ -1136,7 +1136,7 @@ static GCObject **correctgraylist (GCObject **p) { goto remove; /* remove all white objects */ else if (getage(curr) == G_TOUCHED1) { /* touched in this cycle? */ lua_assert(isgray(curr)); - gray2black(curr); /* make it black, for next barrier */ + nw2black(curr); /* make it black, for next barrier */ changeage(curr, G_TOUCHED1, G_TOUCHED2); goto remain; /* keep it in the list and go to next element */ } @@ -1148,7 +1148,7 @@ static GCObject **correctgraylist (GCObject **p) { lua_assert(isold(curr)); /* young objects should be white here */ if (getage(curr) == G_TOUCHED2) /* advance from TOUCHED2... */ changeage(curr, G_TOUCHED2, G_OLD); /* ... to OLD */ - gray2black(curr); /* make object black (to be removed) */ + nw2black(curr); /* make object black (to be removed) */ goto remove; } remove: *p = *next; continue; @@ -1184,7 +1184,7 @@ static void markold (global_State *g, GCObject *from, GCObject *to) { lua_assert(!iswhite(p)); changeage(p, G_OLD1, G_OLD); /* now they are old */ if (isblack(p)) { - black2gray(p); /* should be '2white', but gray works too */ + set2gray(p); /* should be '2white', but gray works too */ reallymarkobject(g, p); } } diff --git a/lgc.h b/lgc.h index 0508cd1dab..073e2a4029 100644 --- a/lgc.h +++ b/lgc.h @@ -12,16 +12,16 @@ #include "lstate.h" /* -** Collectable objects may have one of three colors: white, which -** means the object is not marked; gray, which means the -** object is marked, but its references may be not marked; and -** black, which means that the object and all its references are marked. -** The main invariant of the garbage collector, while marking objects, -** is that a black object can never point to a white one. Moreover, -** any gray object must be in a "gray list" (gray, grayagain, weak, -** allweak, ephemeron) so that it can be visited again before finishing -** the collection cycle. These lists have no meaning when the invariant -** is not being enforced (e.g., sweep phase). +** Collectable objects may have one of three colors: white, which means +** the object is not marked; gray, which means the object is marked, but +** its references may be not marked; and black, which means that the +** object and all its references are marked. The main invariant of the +** garbage collector, while marking objects, is that a black object can +** never point to a white one. Moreover, any gray object must be in a +** "gray list" (gray, grayagain, weak, allweak, ephemeron) so that it +** can be visited again before finishing the collection cycle. (Open +** upvalues are an exception to this rule.) These lists have no meaning +** when the invariant is not being enforced (e.g., sweep phase). */ @@ -96,7 +96,8 @@ #define isdead(g,v) isdeadm(otherwhite(g), (v)->marked) #define changewhite(x) ((x)->marked ^= WHITEBITS) -#define gray2black(x) l_setbit((x)->marked, BLACKBIT) +#define nw2black(x) \ + check_exp(!iswhite(x), l_setbit((x)->marked, BLACKBIT)) #define luaC_white(g) cast_byte((g)->currentwhite & WHITEBITS) From 8a89da07baf43f0dea4b06a7b2780302d75cb61c Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 17 Aug 2020 15:38:51 -0300 Subject: [PATCH 0607/1145] Better control of gray objects Avoid turning an object to gray except at the moment it is inserted in a gray list or in the explicit exceptional cases such as open upvalues and fixed strings. --- lgc.c | 94 +++++++++++++++++++++++++++++++---------------------------- 1 file changed, 50 insertions(+), 44 deletions(-) diff --git a/lgc.c b/lgc.c index be125dd70c..4a7bcaed37 100644 --- a/lgc.c +++ b/lgc.c @@ -75,6 +75,11 @@ #define set2gray(x) resetbits(x->marked, maskcolors) +/* make an object black (coming from any color) */ +#define set2black(x) \ + (x->marked = cast_byte((x->marked & ~WHITEBITS) | bitmask(BLACKBIT))) + + #define valiswhite(x) (iscollectable(x) && iswhite(gcvalue(x))) #define keyiswhite(n) (keyiscollectable(n) && iswhite(gckey(n))) @@ -135,15 +140,23 @@ static GCObject **getgclist (GCObject *o) { /* -** Link a collectable object 'o' with a known type into list pointed by 'p'. +** Link a collectable object 'o' with a known type into the list 'p'. +** (Must be a macro to access the 'gclist' field in different types.) */ -#define linkgclist(o,p) ((o)->gclist = (p), (p) = obj2gco(o)) +#define linkgclist(o,p) linkgclist_(obj2gco(o), &(o)->gclist, &(p)) + +static void linkgclist_ (GCObject *o, GCObject **pnext, GCObject **list) { + lua_assert(!isgray(o)); /* cannot be in a gray list */ + *pnext = *list; + *list = o; + set2gray(o); /* now it is */ +} /* -** Link a generic collectable object 'o' into list pointed by 'p'. +** Link a generic collectable object 'o' into the list 'p'. */ -#define linkobjgclist(o,p) (*getgclist(o) = (p), (p) = obj2gco(o)) +#define linkobjgclist(o,p) linkgclist_(obj2gco(o), getgclist(o), &(p)) @@ -219,9 +232,10 @@ void luaC_barrierback_ (lua_State *L, GCObject *o) { global_State *g = G(L); lua_assert(isblack(o) && !isdead(g, o)); lua_assert((g->gckind == KGC_GEN) == (isold(o) && getage(o) != G_TOUCHED1)); - if (getage(o) != G_TOUCHED2) /* not already in gray list? */ - linkobjgclist(o, g->grayagain); /* link it in 'grayagain' */ - set2gray(o); /* make object gray (again) */ + if (getage(o) == G_TOUCHED2) /* already in gray list? */ + set2gray(o); /* make it gray to become touched1 */ + else /* link it in 'grayagain' and paint it gray */ + linkobjgclist(o, g->grayagain); if (isold(o)) /* generational mode? */ setage(o, G_TOUCHED1); /* touched in current cycle */ } @@ -264,25 +278,30 @@ GCObject *luaC_newobj (lua_State *L, int tt, size_t sz) { /* -** Mark an object. Userdata, strings, and closed upvalues are visited -** and turned black here. Other objects are marked gray and added -** to appropriate list to be visited (and turned black) later. (Open -** upvalues are already indirectly linked through the 'twups' list. They -** are kept gray to avoid barriers, as their values will be revisited by -** the thread or by 'remarkupvals'.) +** Mark an object. Userdata with no user values, strings, and closed +** upvalues are visited and turned black here. Open upvalues are +** already indirectly linked through their respective threads in the +** 'twups' list, so they don't go to the gray list; nevertheless, they +** are kept gray to avoid barriers, as their values will be revisited +** by the thread or by 'remarkupvals'. Other objects are added to the +** gray list to be visited (and turned black) later. Both userdata and +** upvalues can call this function recursively, but this recursion goes +** for at most two levels: An upvalue cannot refer to another upvalue +** (only closures can), and a userdata's metatable must be a table. */ static void reallymarkobject (global_State *g, GCObject *o) { - set2gray(o); switch (o->tt) { case LUA_VSHRSTR: case LUA_VLNGSTR: { - nw2black(o); /* nothing to visit */ + set2black(o); /* nothing to visit */ break; } case LUA_VUPVAL: { UpVal *uv = gco2upv(o); - if (!upisopen(uv)) /* open upvalues are kept gray */ - nw2black(o); /* closed upvalues are visited here */ + if (upisopen(uv)) + set2gray(uv); /* open upvalues are kept gray */ + else + set2black(o); /* closed upvalues are visited here */ markvalue(g, uv->v); /* mark its content */ break; } @@ -290,7 +309,7 @@ static void reallymarkobject (global_State *g, GCObject *o) { Udata *u = gco2u(o); if (u->nuvalue == 0) { /* no user values? */ markobjectN(g, u->metatable); /* mark its metatable */ - nw2black(o); /* nothing else to mark */ + set2black(o); /* nothing else to mark */ break; } /* else... */ @@ -402,17 +421,11 @@ static void restartcollection (global_State *g) { ** TOUCHED1 objects need to be in the list. TOUCHED2 doesn't need to go ** back to a gray list, but then it must become OLD. (That is what ** 'correctgraylist' does when it finds a TOUCHED2 object.) -** It is defined as a macro because 'gclist' is not a unique field in -** different collectable objects. */ -#define genlink(g,o) genlink_(g, obj2gco(o), &(o)->gclist) - -static void genlink_ (global_State *g, GCObject *o, GCObject **pnext) { +static void genlink (global_State *g, GCObject *o) { lua_assert(isblack(o)); if (getage(o) == G_TOUCHED1) { /* touched in this cycle? */ - *pnext = g->grayagain; /* link it back in 'grayagain' */ - g->grayagain = o; - set2gray(o); + linkobjgclist(o, g->grayagain); /* link it back in 'grayagain' */ } /* everything else do not need to be linked back */ else if (getage(o) == G_TOUCHED2) changeage(o, G_TOUCHED2, G_OLD); /* advance age */ @@ -496,10 +509,8 @@ static int traverseephemeron (global_State *g, Table *h, int inv) { linkgclist(h, g->ephemeron); /* have to propagate again */ else if (hasclears) /* table has white keys? */ linkgclist(h, g->allweak); /* may have to clean white keys */ - else { - nw2black(h); /* 'genlink' expects black objects */ - genlink(g, h); /* check whether collector still needs to see it */ - } + else + genlink(g, obj2gco(h)); /* check whether collector still needs to see it */ return marked; } @@ -519,7 +530,7 @@ static void traversestrongtable (global_State *g, Table *h) { markvalue(g, gval(n)); } } - genlink(g, h); + genlink(g, obj2gco(h)); } @@ -531,7 +542,6 @@ static lu_mem traversetable (global_State *g, Table *h) { (cast_void(weakkey = strchr(svalue(mode), 'k')), cast_void(weakvalue = strchr(svalue(mode), 'v')), (weakkey || weakvalue))) { /* is really weak? */ - set2gray(h); /* turn it back to gray, as it probably goes to a list */ if (!weakkey) /* strong keys? */ traverseweakvalue(g, h); else if (!weakvalue) /* strong values? */ @@ -550,7 +560,7 @@ static int traverseudata (global_State *g, Udata *u) { markobjectN(g, u->metatable); /* mark its metatable */ for (i = 0; i < u->nuvalue; i++) markvalue(g, &u->uv[i].uv); - genlink(g, u); + genlink(g, obj2gco(u)); return 1 + u->nuvalue; } @@ -612,10 +622,8 @@ static int traverseLclosure (global_State *g, LClosure *cl) { static int traversethread (global_State *g, lua_State *th) { UpVal *uv; StkId o = th->stack; - if (isold(th) || g->gcstate == GCSpropagate) { + if (isold(th) || g->gcstate == GCSpropagate) linkgclist(th, g->grayagain); /* insert into 'grayagain' list */ - set2gray(th); - } if (o == NULL) return 1; /* stack not completely built yet */ lua_assert(g->gcstate == GCSatomic || @@ -641,8 +649,7 @@ static int traversethread (global_State *g, lua_State *th) { /* -** traverse one gray object, turning it to black (except for threads, -** which are always gray). +** traverse one gray object, turning it to black. */ static lu_mem propagatemark (global_State *g) { GCObject *o = g->gray; @@ -684,8 +691,10 @@ static void convergeephemerons (global_State *g) { g->ephemeron = NULL; /* tables may return to this list when traversed */ changed = 0; while ((w = next) != NULL) { /* for each ephemeron table */ - next = gco2t(w)->gclist; /* list is rebuilt during loop */ - if (traverseephemeron(g, gco2t(w), dir)) { /* marked some value? */ + Table *h = gco2t(w); + next = h->gclist; /* list is rebuilt during loop */ + nw2black(h); /* out of the list (for now) */ + if (traverseephemeron(g, h, dir)) { /* marked some value? */ propagateall(g); /* propagate changes */ changed = 1; /* will have to revisit all ephemeron tables */ } @@ -1048,7 +1057,6 @@ static void sweep2old (lua_State *L, GCObject **p) { if (curr->tt == LUA_VTHREAD) { /* threads must be watched */ lua_State *th = gco2th(curr); linkgclist(th, g->grayagain); /* insert into 'grayagain' list */ - set2gray(th); } else if (curr->tt == LUA_VUPVAL && upisopen(gco2upv(curr))) set2gray(curr); /* open upvalues are always gray */ @@ -1183,10 +1191,8 @@ static void markold (global_State *g, GCObject *from, GCObject *to) { if (getage(p) == G_OLD1) { lua_assert(!iswhite(p)); changeage(p, G_OLD1, G_OLD); /* now they are old */ - if (isblack(p)) { - set2gray(p); /* should be '2white', but gray works too */ + if (isblack(p)) reallymarkobject(g, p); - } } } } From 6bc0f13505bf5d4f613d725fe008c79e72f83ddf Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 18 Aug 2020 14:42:11 -0300 Subject: [PATCH 0608/1145] Fixed bug of long strings in binary chunks When "undumping" a long string, the function 'loadVector' can call the reader function, which can run the garbage collector, which can collect the string being read. So, the string must be anchored during the call to 'loadVector'. --- lundump.c | 3 +++ testes/calls.lua | 16 +++++++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/lundump.c b/lundump.c index cb124d6fa3..5aa55c4457 100644 --- a/lundump.c +++ b/lundump.c @@ -120,7 +120,10 @@ static TString *loadStringN (LoadState *S, Proto *p) { } else { /* long string */ ts = luaS_createlngstrobj(L, size); /* create string */ + setsvalue2s(L, L->top, ts); /* anchor it ('loadVector' can GC) */ + luaD_inctop(L); loadVector(S, getstr(ts), size); /* load directly in final place */ + L->top--; /* pop string */ } luaC_objbarrier(L, p, ts); return ts; diff --git a/testes/calls.lua b/testes/calls.lua index decf41760d..ff72d8f6cf 100644 --- a/testes/calls.lua +++ b/testes/calls.lua @@ -317,6 +317,16 @@ f = load(string.dump(function () return 1 end), nil, "b", {}) assert(type(f) == "function" and f() == 1) +do -- another bug (in 5.4.0) + -- loading a binary long string interrupted by GC cycles + local f = string.dump(function () + return '01234567890123456789012345678901234567890123456789' + end) + f = load(read1(f)) + assert(f() == '01234567890123456789012345678901234567890123456789') +end + + x = string.dump(load("x = 1; return x")) a = assert(load(read1(x), nil, "b")) assert(a() == 1 and _G.x == 1) @@ -358,8 +368,12 @@ x = [[ end end ]] +a = assert(load(read1(x), "read", "t")) +assert(a()(2)(3)(10) == 15) -a = assert(load(read1(x))) +-- repeat the test loading a binary chunk +x = string.dump(a) +a = assert(load(read1(x), "read", "b")) assert(a()(2)(3)(10) == 15) From 8496112a18c887449d37e5aadbb6daec34ae54c0 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 3 Sep 2020 09:52:43 -0300 Subject: [PATCH 0609/1145] Better documentation for 'lctype.h' The old documentation said that 'ltolower' only works for alphabetic characters. However, 'l_str2d' uses it (correctly) also on dots ('.'). --- lctype.h | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/lctype.h b/lctype.h index cbff4d7e19..864e190188 100644 --- a/lctype.h +++ b/lctype.h @@ -13,7 +13,7 @@ /* ** WARNING: the functions defined here do not necessarily correspond ** to the similar functions in the standard C ctype.h. They are -** optimized for the specific needs of Lua +** optimized for the specific needs of Lua. */ #if !defined(LUA_USE_CTYPE) @@ -61,13 +61,19 @@ #define lisprint(c) testprop(c, MASK(PRINTBIT)) #define lisxdigit(c) testprop(c, MASK(XDIGITBIT)) + /* -** this 'ltolower' only works for alphabetic characters +** In ASCII, this 'ltolower' is correct for alphabetic characters and +** for '.'. That is enough for Lua needs. ('check_exp' ensures that +** the character either is an upper-case letter or is unchanged by +** the transformation, which holds for lower-case letters and '.'.) */ -#define ltolower(c) ((c) | ('A' ^ 'a')) +#define ltolower(c) \ + check_exp(('A' <= (c) && (c) <= 'Z') || (c) == ((c) | ('A' ^ 'a')), \ + (c) | ('A' ^ 'a')) -/* two more entries for 0 and -1 (EOZ) */ +/* one entry for each character and for -1 (EOZ) */ LUAI_DDEC(const lu_byte luai_ctype_[UCHAR_MAX + 2];) From 613513d09f89834bea1810bc09b9296560328d92 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 9 Sep 2020 17:28:25 -0300 Subject: [PATCH 0610/1145] Better documentation for the GC of strings in the C API Plus some other small changes. --- manual/manual.of | 78 +++++++++++++++++++++++++++++++++++------------- 1 file changed, 58 insertions(+), 20 deletions(-) diff --git a/manual/manual.of b/manual/manual.of index 9c275d1501..c37f306145 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -2450,7 +2450,7 @@ When you call a Lua function without a fixed number of results @seeF{lua_call}, Lua ensures that the stack has enough space for all results, but it does not ensure any extra space. -So, before pushing anything in the stack after such a call +So, before pushing anything on the stack after such a call you should use @Lid{lua_checkstack}. } @@ -2497,6 +2497,39 @@ which behaves like a nil value. } +@sect3{constchar|@title{Pointers to strings} + +Several functions in the API return pointers (@T{const char*}) +to Lua strings in the stack. +(See @Lid{lua_pushfstring}, @Lid{lua_pushlstring}, +@Lid{lua_pushstring}, and @Lid{lua_tolstring}. +See also @Lid{luaL_checklstring}, @Lid{luaL_checkstring}, +and @Lid{luaL_tolstring} in the auxiliary library.) + +In general, +Lua's garbage collection can free or move internal memory +and then invalidate pointers to internal strings. +To allow a safe use of these pointers, +The API guarantees that any pointer to a string in a stack index +is valid while the value at that index is neither modified nor popped. +When the index is a pseudo-index (referring to an upvalue), +the pointer is valid while the corresponding call is active and +the corresponding upvalue is not modified. + +Some functions in the debug interface +also return pointers to strings, +namely @Lid{lua_getlocal}, @Lid{lua_getupvalue}, +@Lid{lua_setlocal}, and @Lid{lua_setupvalue}. +For these functions, the pointer is guaranteed to +be valid while the caller function is active and +the given closure (if one was given) is in the stack. + +Except for these guarantees, +the garbage collector is free to invalidate +any pointer to internal strings. + +} + } @sect2{c-closure| @title{C Closures} @@ -2791,7 +2824,7 @@ depending on the situation; an interrogation mark @Char{?} means that we cannot know how many elements the function pops/pushes by looking only at its arguments. -(For instance, they may depend on what is on the stack.) +(For instance, they may depend on what is in the stack.) The third field, @T{x}, tells whether the function may raise errors: @Char{-} means the function never raises any error; @@ -3584,6 +3617,10 @@ plus an associated block of raw memory with @id{size} bytes. @Lid{lua_setiuservalue} and @Lid{lua_getiuservalue}.) The function returns the address of the block of memory. +Lua ensures that this address is valid as long as +the corresponding userdata is alive @see{GC}. +Moreover, if the userdata is marked for finalization @see{finalizers}, +its address is valid at least until the call to its finalizer. } @@ -3764,7 +3801,7 @@ This function is equivalent to @Lid{lua_pushcclosure} with no upvalues. @apii{0,1,v} Pushes onto the stack a formatted string -and returns a pointer to this string. +and returns a pointer to this string @see{constchar}. It is similar to the @ANSI{sprintf}, but has two important differences. First, @@ -3838,7 +3875,7 @@ the function returns. The string can contain any binary data, including @x{embedded zeros}. -Returns a pointer to the internal copy of the string. +Returns a pointer to the internal copy of the string @see{constchar}. } @@ -3865,7 +3902,7 @@ Lua will make or reuse an internal copy of the given string, so the memory at @id{s} can be freed or reused immediately after the function returns. -Returns a pointer to the internal copy of the string. +Returns a pointer to the internal copy of the string @see{constchar}. If @id{s} is @id{NULL}, pushes @nil and returns @id{NULL}. @@ -4277,7 +4314,7 @@ otherwise, returns @id{NULL}. } @APIEntry{void lua_toclose (lua_State *L, int index);| -@apii{0,0,v} +@apii{0,0,m} Marks the given index in the stack as a to-be-closed @Q{variable} @see{to-be-closed}. @@ -4295,10 +4332,16 @@ by any other function in the API except @Lid{lua_settop} or @Lid{lua_pop}. This function should not be called for an index that is equal to or below an active to-be-closed index. -This function can raise an out-of-memory error. -In that case, the value in the given index is immediately closed, +In the case of an out-of-memory error, +the value in the given index is immediately closed, as if it was already marked. +Note that, both in case of errors and of a regular return, +by the time the @idx{__close} metamethod runs, +the @N{C stack} was already unwound, +so that any automatic C variable declared in the calling function +will be out of scope. + } @APIEntry{lua_Integer lua_tointeger (lua_State *L, int index);| @@ -4338,15 +4381,11 @@ then @id{lua_tolstring} also when @id{lua_tolstring} is applied to keys during a table traversal.) @id{lua_tolstring} returns a pointer -to a string inside the Lua state. +to a string inside the Lua state @see{constchar}. This string always has a zero (@Char{\0}) after its last character (as @N{in C}), but can contain other zeros in its body. -Because Lua has garbage collection, -there is no guarantee that the pointer returned by @id{lua_tolstring} -will be valid after the corresponding Lua value is removed from the stack. - } @APIEntry{lua_Number lua_tonumber (lua_State *L, int index);| @@ -4708,7 +4747,7 @@ true if the function is a vararg function } @item{@id{ftransfer}| -the index on the stack of the first value being @Q{transferred}, +the index in the stack of the first value being @Q{transferred}, that is, parameters in a call or return values in a return. (The other values are in consecutive indices.) Using this index, you can access and modify these values @@ -4860,7 +4899,7 @@ an identification of the @emph{activation record} of the function executing at a given level. @N{Level 0} is the current running function, whereas level @M{n+1} is the function that has called level @M{n} -(except for tail calls, which do not count on the stack). +(except for tail calls, which do not count in the stack). When called with a level greater than the stack depth, @Lid{lua_getstack} returns 0; otherwise it returns 1. @@ -4947,8 +4986,7 @@ For each event, the hook is called as explained below: @description{ @item{The call hook| is called when the interpreter calls a function. -The hook is called just after Lua enters the new function, -before the function gets its arguments. +The hook is called just after Lua enters the new function. } @item{The return hook| is called when the interpreter returns from a function. @@ -5038,7 +5076,7 @@ refer to the @id{n2}-th upvalue of the Lua closure at index @id{funcindex2}. @C{-------------------------------------------------------------------------} -@sect1{@title{The Auxiliary Library} +@sect1{auxlib|@title{The Auxiliary Library} @simplesect{ @@ -5925,7 +5963,7 @@ it returns @id{NULL} instead of raising an error. Converts any Lua value at the given index to a @N{C string} in a reasonable format. The resulting string is pushed onto the stack and also -returned by the function. +returned by the function @see{constchar}. If @id{len} is not @id{NULL}, the function also sets @T{*len} with the string length. @@ -8608,7 +8646,7 @@ which means the function running at level @id{f} of the call stack of the given thread: @N{level 0} is the current function (@id{getinfo} itself); @N{level 1} is the function that called @id{getinfo} -(except for tail calls, which do not count on the stack); +(except for tail calls, which do not count in the stack); and so on. If @id{f} is a number greater than the number of active functions, then @id{getinfo} returns @fail. From b6888a158b43c7391e2e4837cf4ae91e8c5e8371 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 15 Sep 2020 14:27:10 -0300 Subject: [PATCH 0611/1145] New release number (5.4.1) --- lua.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua.h b/lua.h index b348c147fc..08c6a64a10 100644 --- a/lua.h +++ b/lua.h @@ -18,7 +18,7 @@ #define LUA_VERSION_MAJOR "5" #define LUA_VERSION_MINOR "4" -#define LUA_VERSION_RELEASE "0" +#define LUA_VERSION_RELEASE "1" #define LUA_VERSION_NUM 504 #define LUA_VERSION_RELEASE_NUM (LUA_VERSION_NUM * 100 + 0) From 98ec7995912af0aaaf9a97c5bc1be17e9b601af9 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 15 Sep 2020 14:29:52 -0300 Subject: [PATCH 0612/1145] Detail Code for multi-character tokens can start right after maximum char. --- llex.c | 1 - llex.h | 8 +++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/llex.c b/llex.c index 90a7951f70..3d6b2b97ac 100644 --- a/llex.c +++ b/llex.c @@ -81,7 +81,6 @@ void luaX_init (lua_State *L) { const char *luaX_token2str (LexState *ls, int token) { if (token < FIRST_RESERVED) { /* single-byte symbols? */ - lua_assert(token == cast_uchar(token)); if (lisprint(token)) return luaO_pushfstring(ls->L, "'%c'", token); else /* control character */ diff --git a/llex.h b/llex.h index d1a4cba7c2..389d2f8635 100644 --- a/llex.h +++ b/llex.h @@ -7,11 +7,17 @@ #ifndef llex_h #define llex_h +#include + #include "lobject.h" #include "lzio.h" -#define FIRST_RESERVED 257 +/* +** Single-char tokens (terminal symbols) are represented by their own +** numeric code. Other tokens start at the following value. +*/ +#define FIRST_RESERVED (UCHAR_MAX + 1) #if !defined(LUA_ENV) From e51564d1bee5aa8b411328d7d3d75906dfc0a260 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 16 Sep 2020 14:57:51 -0300 Subject: [PATCH 0613/1145] Details in comments and documentation --- lstate.h | 11 +++++++++++ manual/manual.of | 10 ++++++++++ 2 files changed, 21 insertions(+) diff --git a/lstate.h b/lstate.h index 1b6bcdf8f7..c1c38204a6 100644 --- a/lstate.h +++ b/lstate.h @@ -334,6 +334,12 @@ struct lua_State { /* ** Union of all collectable objects (only for conversions) +** ISO C99, 6.5.2.3 p.5: +** "if a union contains several structures that share a common initial +** sequence [...], and if the union object currently contains one +** of these structures, it is permitted to inspect the common initial +** part of any of them anywhere that a declaration of the complete type +** of the union is visible." */ union GCUnion { GCObject gc; /* common header */ @@ -347,6 +353,11 @@ union GCUnion { }; +/* +** ISO C99, 6.7.2.1 p.14: +** "A pointer to a union object, suitably converted, points to each of +** its members [...], and vice versa." +*/ #define cast_u(o) cast(union GCUnion *, (o)) /* macros to convert a GCObject into a specific value */ diff --git a/manual/manual.of b/manual/manual.of index c37f306145..ff89139954 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -6813,6 +6813,16 @@ As such, it is only available on some platforms (Windows, Linux, Mac OS X, Solaris, BSD, plus other Unix systems that support the @id{dlfcn} standard). +This function is inherently insecure, +as it allows Lua to call any function in any readable dynamic +library in the system. +(Lua calls any function assuming the function +has a proper prototype and respects a proper protocol +@see{lua_CFunction}. +Therefore, +calling an arbitrary function in an arbitrary dynamic library +more often than not results in an access violation.) + } @LibEntry{package.path| From fbaf040f5effae90214887d10bfac710b836f281 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 25 Sep 2020 10:49:29 -0300 Subject: [PATCH 0614/1145] Details in the manual --- manual/manual.of | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/manual/manual.of b/manual/manual.of index ff89139954..8b34b5bd2a 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -2441,7 +2441,8 @@ to ensure that the stack has enough space for pushing new elements. Whenever Lua calls C, it ensures that the stack has space for -at least @defid{LUA_MINSTACK} extra slots. +at least @defid{LUA_MINSTACK} extra elements; +that is, you can safely push up to @id{LUA_MINSTACK} values into it. @id{LUA_MINSTACK} is defined as 20, so that usually you do not have to worry about stack space unless your code has loops pushing elements onto the stack. @@ -3061,7 +3062,7 @@ static int foo (lua_State *L) { @APIEntry{int lua_checkstack (lua_State *L, int n);| @apii{0,0,-} -Ensures that the stack has space for at least @id{n} extra slots, +Ensures that the stack has space for at least @id{n} extra elements, that is, that you can safely push up to @id{n} values into it. It returns false if it cannot fulfill the request, either because it would cause the stack @@ -3069,7 +3070,7 @@ to be greater than a fixed maximum size (typically at least several thousand elements) or because it cannot allocate memory for the extra space. This function never shrinks the stack; -if the stack already has space for the extra slots, +if the stack already has space for the extra elements, it is left unchanged. } @@ -6313,9 +6314,11 @@ It may be the string @St{b} (only @x{binary chunk}s), or @St{bt} (both binary and text). The default is @St{bt}. -Lua does not check the consistency of binary chunks. -Maliciously crafted binary chunks can crash -the interpreter. +It is safe to load malformed binary chunks; +@id{load} signals an appropriate error. +However, +Lua does not check the consistency of the code inside binary chunks; +running maliciously crafted bytecode can crash the interpreter. } From 51c29ec0c47da5f7359218281f6416ffd95f468f Mon Sep 17 00:00:00 2001 From: lhf Date: Mon, 28 Sep 2020 14:34:44 +0000 Subject: [PATCH 0615/1145] Create README.md --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000000..5893dc9aa1 --- /dev/null +++ b/README.md @@ -0,0 +1,7 @@ +# Lua + +This is the repository of Lua development code, as seen by the Lua team. It contains the full history of all commits but is mirrored irregularly. For complete information about Lua, visit [Lua.org](https://www.lua.org/). + +Please **do not** send pull requests. To report issues and send patches, post a message to the [Lua mailing list](https://www.lua.org/lua-l.html). + +Download official Lua releases from [Lua.org](https://www.lua.org/download.html). From f83de8e34e24e30acf277f60de62a33bd51d1ddd Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 30 Sep 2020 15:10:12 -0300 Subject: [PATCH 0616/1145] Wrong cast in 'str_unpack' --- lstrlib.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lstrlib.c b/lstrlib.c index 2ba8bde47c..a30ec5af29 100644 --- a/lstrlib.c +++ b/lstrlib.c @@ -1738,7 +1738,7 @@ static int str_unpack (lua_State *L) { break; } case Kzstr: { - size_t len = (int)strlen(data + pos); + size_t len = strlen(data + pos); luaL_argcheck(L, pos + len < ld, 2, "unfinished string for format 'z'"); lua_pushlstring(L, data + pos, len); From 5d8ce05b3f6fad79e37ed21c1076e47a322472c6 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 21 Sep 2020 10:31:03 -0300 Subject: [PATCH 0617/1145] Back to a stackless implementation A "with stack" implementation gains too little in performance to be worth all the noise from C-stack overflows. This commit is almost a sketch, to test performance. There are several pending stuff: - review control of C-stack overflow and error messages; - what to do with setcstacklimit; - review comments; - review unroll of Lua calls. --- ldo.c | 51 ++++++++++++++++++++++++++++------------------- ldo.h | 1 + lparser.c | 12 ++++++----- lstate.c | 43 ++------------------------------------- lstate.h | 7 ------- ltests.h | 5 ----- luaconf.h | 8 ++++---- lvm.c | 27 ++++++++++++++++++------- testes/all.lua | 4 ++-- testes/cstack.lua | 2 ++ testes/errors.lua | 7 +++---- 11 files changed, 72 insertions(+), 95 deletions(-) diff --git a/ldo.c b/ldo.c index 5473815a18..dc3cc9fde6 100644 --- a/ldo.c +++ b/ldo.c @@ -139,8 +139,7 @@ l_noret luaD_throw (lua_State *L, int errcode) { int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) { - global_State *g = G(L); - l_uint32 oldnCcalls = g->Cstacklimit - (L->nCcalls + L->nci); + l_uint32 oldnCcalls = L->nCcalls; struct lua_longjmp lj; lj.status = LUA_OK; lj.previous = L->errorJmp; /* chain new error handler */ @@ -149,7 +148,7 @@ int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) { (*f)(L, ud); ); L->errorJmp = lj.previous; /* restore old error handler */ - L->nCcalls = g->Cstacklimit - oldnCcalls - L->nci; + L->nCcalls = oldnCcalls; return lj.status; } @@ -348,7 +347,7 @@ static StkId rethook (lua_State *L, CallInfo *ci, StkId firstres, int nres) { /* ** Check whether 'func' has a '__call' metafield. If so, put it in the -** stack, below original 'func', so that 'luaD_call' can call it. Raise +** stack, below original 'func', so that 'luaD_precall' can call it. Raise ** an error if there is no '__call' metafield. */ void luaD_tryfuncTM (lua_State *L, StkId func) { @@ -454,7 +453,7 @@ void luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, int narg1) { ** When returns, all the results are on the stack, starting at the original ** function position. */ -void luaD_call (lua_State *L, StkId func, int nresults) { +int luaD_precall (lua_State *L, StkId func, int nresults) { lua_CFunction f; retry: switch (ttypetag(s2v(func))) { @@ -482,7 +481,7 @@ void luaD_call (lua_State *L, StkId func, int nresults) { lua_lock(L); api_checknelems(L, n); luaD_poscall(L, ci, n); - break; + return 1; } case LUA_VLCL: { /* Lua function */ CallInfo *ci; @@ -501,8 +500,7 @@ void luaD_call (lua_State *L, StkId func, int nresults) { for (; narg < nfixparams; narg++) setnilvalue(s2v(L->top++)); /* complete missing arguments */ lua_assert(ci->top <= L->stack_last); - luaV_execute(L, ci); /* run the function */ - break; + return 0; } default: { /* not a function */ checkstackGCp(L, 1, func); /* space for metamethod */ @@ -513,17 +511,32 @@ void luaD_call (lua_State *L, StkId func, int nresults) { } +static void stackerror (lua_State *L) { + if (getCcalls(L) == LUAI_MAXCCALLS) + luaG_runerror(L, "C stack overflow"); + else if (getCcalls(L) >= (LUAI_MAXCCALLS + (LUAI_MAXCCALLS>>3))) + luaD_throw(L, LUA_ERRERR); /* error while handing stack error */ +} + + +void luaD_call (lua_State *L, StkId func, int nResults) { + L->nCcalls++; + if (getCcalls(L) >= LUAI_MAXCCALLS) + stackerror(L); + if (!luaD_precall(L, func, nResults)) /* is a Lua function? */ + luaV_execute(L, L->ci); /* call it */ + L->nCcalls--; +} + + + /* ** Similar to 'luaD_call', but does not allow yields during the call. */ void luaD_callnoyield (lua_State *L, StkId func, int nResults) { - incXCcalls(L); - if (getCcalls(L) <= CSTACKERR) { /* possible C stack overflow? */ - luaE_exitCcall(L); /* to compensate decrement in next call */ - luaE_enterCcall(L); /* check properly */ - } + incnny(L); luaD_call(L, func, nResults); - decXCcalls(L); + decnny(L); } @@ -638,7 +651,8 @@ static void resume (lua_State *L, void *ud) { StkId firstArg = L->top - n; /* first argument */ CallInfo *ci = L->ci; if (L->status == LUA_OK) { /* starting a coroutine? */ - luaD_call(L, firstArg - 1, LUA_MULTRET); + if (!luaD_precall(L, firstArg - 1, LUA_MULTRET)) /* Lua function? */ + luaV_execute(L, L->ci); /* call it */ } else { /* resuming from previous yield */ lua_assert(L->status == LUA_YIELD); @@ -670,11 +684,8 @@ LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs, } else if (L->status != LUA_YIELD) /* ended with errors? */ return resume_error(L, "cannot resume dead coroutine", nargs); - if (from == NULL) - L->nCcalls = CSTACKTHREAD; - else /* correct 'nCcalls' for this thread */ - L->nCcalls = getCcalls(from) - L->nci - CSTACKCF; - if (L->nCcalls <= CSTACKERR) + L->nCcalls = (from) ? getCcalls(from) + 1 : 1; + if (getCcalls(L) >= LUAI_MAXCCALLS) return resume_error(L, "C stack overflow", nargs); luai_userstateresume(L, nargs); api_checknelems(L, (L->status == LUA_OK) ? nargs + 1 : nargs); diff --git a/ldo.h b/ldo.h index 6c6cb28554..7d032117bb 100644 --- a/ldo.h +++ b/ldo.h @@ -59,6 +59,7 @@ LUAI_FUNC void luaD_hook (lua_State *L, int event, int line, int fTransfer, int nTransfer); LUAI_FUNC void luaD_hookcall (lua_State *L, CallInfo *ci); LUAI_FUNC void luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, int n); +LUAI_FUNC int luaD_precall (lua_State *L, StkId func, int nResults); LUAI_FUNC void luaD_call (lua_State *L, StkId func, int nResults); LUAI_FUNC void luaD_callnoyield (lua_State *L, StkId func, int nResults); LUAI_FUNC void luaD_tryfuncTM (lua_State *L, StkId func); diff --git a/lparser.c b/lparser.c index bc7d9a4f2d..502a9b2df3 100644 --- a/lparser.c +++ b/lparser.c @@ -489,12 +489,14 @@ static void adjust_assign (LexState *ls, int nvars, int nexps, expdesc *e) { } -/* -** Macros to limit the maximum recursion depth while parsing -*/ -#define enterlevel(ls) luaE_enterCcall((ls)->L) +static void enterlevel (LexState *ls) { + lua_State *L = ls->L; + L->nCcalls++; + checklimit(ls->fs, getCcalls(L), LUAI_MAXCCALLS, "C levels"); +} + -#define leavelevel(ls) luaE_exitCcall((ls)->L) +#define leavelevel(ls) ((ls)->L->nCcalls--) /* diff --git a/lstate.c b/lstate.c index 86b3761ffc..8cda307260 100644 --- a/lstate.c +++ b/lstate.c @@ -119,44 +119,9 @@ LUA_API int lua_setcstacklimit (lua_State *L, unsigned int limit) { } -/* -** Decrement count of "C calls" and check for overflows. In case of -** a stack overflow, check appropriate error ("regular" overflow or -** overflow while handling stack overflow). If 'nCcalls' is smaller -** than CSTACKERR but larger than CSTACKMARK, it means it has just -** entered the "overflow zone", so the function raises an overflow -** error. If 'nCcalls' is smaller than CSTACKMARK (which means it is -** already handling an overflow) but larger than CSTACKERRMARK, does -** not report an error (to allow message handling to work). Otherwise, -** report a stack overflow while handling a stack overflow (probably -** caused by a repeating error in the message handling function). -*/ - -void luaE_enterCcall (lua_State *L) { - int ncalls = getCcalls(L); - L->nCcalls--; - if (ncalls <= CSTACKERR) { /* possible overflow? */ - luaE_freeCI(L); /* release unused CIs */ - ncalls = getCcalls(L); /* update call count */ - if (ncalls <= CSTACKERR) { /* still overflow? */ - if (ncalls <= CSTACKERRMARK) /* below error-handling zone? */ - luaD_throw(L, LUA_ERRERR); /* error while handling stack error */ - else if (ncalls >= CSTACKMARK) { - /* not in error-handling zone; raise the error now */ - L->nCcalls = (CSTACKMARK - 1); /* enter error-handling zone */ - luaG_runerror(L, "C stack overflow"); - } - /* else stack is in the error-handling zone; - allow message handler to work */ - } - } -} - - CallInfo *luaE_extendCI (lua_State *L) { CallInfo *ci; lua_assert(L->ci->next == NULL); - luaE_enterCcall(L); ci = luaM_new(L, CallInfo); lua_assert(L->ci->next == NULL); L->ci->next = ci; @@ -175,13 +140,11 @@ void luaE_freeCI (lua_State *L) { CallInfo *ci = L->ci; CallInfo *next = ci->next; ci->next = NULL; - L->nCcalls += L->nci; /* add removed elements back to 'nCcalls' */ while ((ci = next) != NULL) { next = ci->next; luaM_free(L, ci); L->nci--; } - L->nCcalls -= L->nci; /* adjust result */ } @@ -194,7 +157,6 @@ void luaE_shrinkCI (lua_State *L) { CallInfo *next; if (ci == NULL) return; /* no extra elements */ - L->nCcalls += L->nci; /* add removed elements back to 'nCcalls' */ while ((next = ci->next) != NULL) { /* two extra elements? */ CallInfo *next2 = next->next; /* next's next */ ci->next = next2; /* remove next from the list */ @@ -207,7 +169,6 @@ void luaE_shrinkCI (lua_State *L) { ci = next2; /* continue */ } } - L->nCcalls -= L->nci; /* adjust result */ } @@ -335,7 +296,7 @@ LUA_API lua_State *lua_newthread (lua_State *L) { setthvalue2s(L, L->top, L1); api_incr_top(L); preinit_thread(L1, g); - L1->nCcalls = getCcalls(L); + L1->nCcalls = 0; L1->hookmask = L->hookmask; L1->basehookcount = L->basehookcount; L1->hook = L->hook; @@ -396,7 +357,7 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { preinit_thread(L, g); g->allgc = obj2gco(L); /* by now, only object is the main thread */ L->next = NULL; - g->Cstacklimit = L->nCcalls = LUAI_MAXCSTACK + CSTACKERR; + g->Cstacklimit = L->nCcalls = 0; incnny(L); /* main thread is always non yieldable */ g->frealloc = f; g->ud = ud; diff --git a/lstate.h b/lstate.h index c1c38204a6..983aa0d528 100644 --- a/lstate.h +++ b/lstate.h @@ -144,12 +144,6 @@ /* Decrement the number of non-yieldable calls */ #define decnny(L) ((L)->nCcalls -= 0x10000) -/* Increment the number of non-yieldable calls and decrement nCcalls */ -#define incXCcalls(L) ((L)->nCcalls += 0x10000 - CSTACKCF) - -/* Decrement the number of non-yieldable calls and increment nCcalls */ -#define decXCcalls(L) ((L)->nCcalls -= 0x10000 - CSTACKCF) - @@ -389,7 +383,6 @@ LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1); LUAI_FUNC CallInfo *luaE_extendCI (lua_State *L); LUAI_FUNC void luaE_freeCI (lua_State *L); LUAI_FUNC void luaE_shrinkCI (lua_State *L); -LUAI_FUNC void luaE_enterCcall (lua_State *L); LUAI_FUNC void luaE_warning (lua_State *L, const char *msg, int tocont); LUAI_FUNC void luaE_warnerror (lua_State *L, const char *where); diff --git a/ltests.h b/ltests.h index e9219e2973..f8c4466f8d 100644 --- a/ltests.h +++ b/ltests.h @@ -23,11 +23,6 @@ #define LUAI_ASSERT - -/* compiled with -O0, Lua uses a lot of C stack space... */ -#undef LUAI_MAXCSTACK -#define LUAI_MAXCSTACK 400 - /* to avoid warnings, and to make sure value is really unused */ #define UNUSED(x) (x=0, (void)(x)) diff --git a/luaconf.h b/luaconf.h index bdf927e77e..229413d2f1 100644 --- a/luaconf.h +++ b/luaconf.h @@ -36,8 +36,8 @@ ** ===================================================================== */ -/* -@@ LUAI_MAXCSTACK defines the maximum depth for nested calls and +/* >>> move back to llimits.h +@@ LUAI_MAXCCALLS defines the maximum depth for nested calls and ** also limits the maximum depth of other recursive algorithms in ** the implementation, such as syntactic analysis. A value too ** large may allow the interpreter to crash (C-stack overflow). @@ -46,8 +46,8 @@ ** The test file 'cstack.lua' may help finding a good limit. ** (It will crash with a limit too high.) */ -#if !defined(LUAI_MAXCSTACK) -#define LUAI_MAXCSTACK 2000 +#if !defined(LUAI_MAXCCALLS) +#define LUAI_MAXCCALLS 200 #endif diff --git a/lvm.c b/lvm.c index 08681af1b8..a232e1e79e 100644 --- a/lvm.c +++ b/lvm.c @@ -229,7 +229,7 @@ static int forprep (lua_State *L, StkId ra) { count /= l_castS2U(-(step + 1)) + 1u; } /* store the counter in place of the limit (which won't be - needed anymore */ + needed anymore) */ setivalue(plimit, l_castU2S(count)); } } @@ -1124,6 +1124,7 @@ void luaV_finishOp (lua_State *L) { void luaV_execute (lua_State *L, CallInfo *ci) { + const CallInfo *origci = ci; LClosure *cl; TValue *k; StkId base; @@ -1611,7 +1612,13 @@ void luaV_execute (lua_State *L, CallInfo *ci) { if (b != 0) /* fixed number of arguments? */ L->top = ra + b; /* top signals number of arguments */ /* else previous instruction set top */ - ProtectNT(luaD_call(L, ra, nresults)); + savepc(L); /* in case of errors */ + if (luaD_precall(L, ra, nresults)) + updatetrap(ci); /* C call; nothing else to be done */ + else { /* Lua call: run function in this same invocation */ + ci = L->ci; + goto tailcall; + } vmbreak; } vmcase(OP_TAILCALL) { @@ -1637,12 +1644,12 @@ void luaV_execute (lua_State *L, CallInfo *ci) { checkstackGCp(L, 1, ra); } if (!ttisLclosure(s2v(ra))) { /* C function? */ - luaD_call(L, ra, LUA_MULTRET); /* call it */ + luaD_precall(L, ra, LUA_MULTRET); /* call it */ updatetrap(ci); updatestack(ci); /* stack may have been relocated */ ci->func -= delta; luaD_poscall(L, ci, cast_int(L->top - ra)); - return; + goto ret; } ci->func -= delta; luaD_pretailcall(L, ci, ra, b); /* prepare call frame */ @@ -1665,7 +1672,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { ci->func -= ci->u.l.nextraargs + nparams1; L->top = ra + n; /* set call for 'luaD_poscall' */ luaD_poscall(L, ci, n); - return; + goto ret; } vmcase(OP_RETURN0) { if (L->hookmask) { @@ -1679,7 +1686,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { while (nres-- > 0) setnilvalue(s2v(L->top++)); /* all results are nil */ } - return; + goto ret; } vmcase(OP_RETURN1) { if (L->hookmask) { @@ -1698,7 +1705,13 @@ void luaV_execute (lua_State *L, CallInfo *ci) { setnilvalue(s2v(L->top++)); } } - return; + ret: + if (ci == origci) + return; + else { + ci = ci->previous; + goto tailcall; + } } vmcase(OP_FORLOOP) { if (ttisinteger(s2v(ra + 2))) { /* integer loop? */ diff --git a/testes/all.lua b/testes/all.lua index db074dd8b4..a4feeec1b9 100644 --- a/testes/all.lua +++ b/testes/all.lua @@ -127,8 +127,8 @@ else end Cstacklevel = function () - local _, _, ncalls, nci = T.stacklevel() - return ncalls + nci -- number of free slots in the C stack + local _, _, ncalls = T.stacklevel() + return ncalls -- number of C calls end end diff --git a/testes/cstack.lua b/testes/cstack.lua index 4e37b98829..c1177f3b74 100644 --- a/testes/cstack.lua +++ b/testes/cstack.lua @@ -1,6 +1,8 @@ -- $Id: testes/cstack.lua $ -- See Copyright Notice in file all.lua +do return end + local debug = require "debug" print"testing C-stack overflow detection" diff --git a/testes/errors.lua b/testes/errors.lua index f9623b1daf..88918df714 100644 --- a/testes/errors.lua +++ b/testes/errors.lua @@ -530,10 +530,9 @@ local function testrep (init, rep, close, repc, finalresult) if (finalresult) then assert(res() == finalresult) end - s = init .. string.rep(rep, 10000) - local res, msg = load(s) -- 10000 levels not ok - assert(not res and (string.find(msg, "too many registers") or - string.find(msg, "stack overflow"))) + s = init .. string.rep(rep, 500) + local res, msg = load(s) -- 500 levels not ok + assert(not res and string.find(msg, "too many")) end testrep("local a; a", ",a", "= 1", ",1") -- multiple assignment From 287b302acb8d925178e9edb800f0a8d18c7d35f6 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 23 Sep 2020 10:18:01 -0300 Subject: [PATCH 0618/1145] Revision of stackless implementation - more organized handling of 'nCcalls' - comments - deprecation of 'setcstacklimit' --- all | 2 +- ldblib.c | 5 +- ldo.c | 52 +++++++++--------- llimits.h | 11 ++++ lparser.c | 6 +- lstate.c | 45 ++++++++------- lstate.h | 56 ++++--------------- luaconf.h | 15 ----- lvm.c | 6 +- manual/manual.of | 72 ++++++------------------ testes/cstack.lua | 137 ++++++++++------------------------------------ testes/errors.lua | 3 +- 12 files changed, 127 insertions(+), 283 deletions(-) diff --git a/all b/all index 2a8cb92f9e..039f6095f6 100755 --- a/all +++ b/all @@ -1,7 +1,7 @@ make -s -j cd testes/libs; make -s cd .. # back to directory 'testes' -ulimit -S -s 2000 +ulimit -S -s 1000 if { ../lua -W all.lua; } then echo -e "\n\n final OK!!!!\n\n" else diff --git a/ldblib.c b/ldblib.c index 59eb8f0ea7..26058b5082 100644 --- a/ldblib.c +++ b/ldblib.c @@ -440,10 +440,7 @@ static int db_traceback (lua_State *L) { static int db_setcstacklimit (lua_State *L) { int limit = (int)luaL_checkinteger(L, 1); int res = lua_setcstacklimit(L, limit); - if (res == 0) - lua_pushboolean(L, 0); - else - lua_pushinteger(L, res); + lua_pushinteger(L, res); return 1; } diff --git a/ldo.c b/ldo.c index dc3cc9fde6..0a6a716927 100644 --- a/ldo.c +++ b/ldo.c @@ -448,10 +448,11 @@ void luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, int narg1) { /* -** Call a function (C or Lua). The function to be called is at *func. -** The arguments are on the stack, right after the function. -** When returns, all the results are on the stack, starting at the original -** function position. +** Prepares the call to a function (C or Lua). For C functions, also do +** the call. The function to be called is at '*func'. The arguments are +** on the stack, right after the function. Returns true if the call was +** made (it was a C function). When returns true, all the results are +** on the stack, starting at the original function position. */ int luaD_precall (lua_State *L, StkId func, int nresults) { lua_CFunction f; @@ -511,32 +512,34 @@ int luaD_precall (lua_State *L, StkId func, int nresults) { } -static void stackerror (lua_State *L) { - if (getCcalls(L) == LUAI_MAXCCALLS) - luaG_runerror(L, "C stack overflow"); - else if (getCcalls(L) >= (LUAI_MAXCCALLS + (LUAI_MAXCCALLS>>3))) - luaD_throw(L, LUA_ERRERR); /* error while handing stack error */ -} - - -void luaD_call (lua_State *L, StkId func, int nResults) { - L->nCcalls++; +/* +** Call a function (C or Lua). 'inc' can be 1 (increment number +** of recursive invocations in the C stack) or nyci (the same plus +** increment number of non-yieldable calls). +*/ +static void docall (lua_State *L, StkId func, int nResults, int inc) { + L->nCcalls += inc; if (getCcalls(L) >= LUAI_MAXCCALLS) - stackerror(L); + luaE_checkcstack(L); if (!luaD_precall(L, func, nResults)) /* is a Lua function? */ luaV_execute(L, L->ci); /* call it */ - L->nCcalls--; + L->nCcalls -= inc; } +/* +** External interface for 'docall' +*/ +void luaD_call (lua_State *L, StkId func, int nResults) { + return docall(L, func, nResults, 1); +} + /* ** Similar to 'luaD_call', but does not allow yields during the call. */ void luaD_callnoyield (lua_State *L, StkId func, int nResults) { - incnny(L); - luaD_call(L, func, nResults); - decnny(L); + return docall(L, func, nResults, nyci); } @@ -650,13 +653,12 @@ static void resume (lua_State *L, void *ud) { int n = *(cast(int*, ud)); /* number of arguments */ StkId firstArg = L->top - n; /* first argument */ CallInfo *ci = L->ci; - if (L->status == LUA_OK) { /* starting a coroutine? */ - if (!luaD_precall(L, firstArg - 1, LUA_MULTRET)) /* Lua function? */ - luaV_execute(L, L->ci); /* call it */ - } + if (L->status == LUA_OK) /* starting a coroutine? */ + docall(L, firstArg - 1, LUA_MULTRET, 1); /* just call its body */ else { /* resuming from previous yield */ lua_assert(L->status == LUA_YIELD); L->status = LUA_OK; /* mark that it is running (again) */ + luaE_incCstack(L); /* control the C stack */ if (isLua(ci)) /* yielded inside a hook? */ luaV_execute(L, ci); /* just continue running Lua code */ else { /* 'common' yield */ @@ -684,9 +686,7 @@ LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs, } else if (L->status != LUA_YIELD) /* ended with errors? */ return resume_error(L, "cannot resume dead coroutine", nargs); - L->nCcalls = (from) ? getCcalls(from) + 1 : 1; - if (getCcalls(L) >= LUAI_MAXCCALLS) - return resume_error(L, "C stack overflow", nargs); + L->nCcalls = (from) ? getCcalls(from) : 0; luai_userstateresume(L, nargs); api_checknelems(L, (L->status == LUA_OK) ? nargs + 1 : nargs); status = luaD_rawrunprotected(L, resume, &nargs); diff --git a/llimits.h b/llimits.h index 48c97f9597..d6866d7c1f 100644 --- a/llimits.h +++ b/llimits.h @@ -234,6 +234,17 @@ typedef l_uint32 Instruction; #endif +/* +** Maximum depth for nested C calls, syntactical nested non-terminals, +** and other features implemented through recursion in C. (Value must +** fit in a 16-bit unsigned integer. It must also be compatible with +** the size of the C stack.) +*/ +#if !defined(LUAI_MAXCCALLS) +#define LUAI_MAXCCALLS 200 +#endif + + /* ** macros that are executed whenever program enters the Lua core ** ('lua_lock') and leaves the core ('lua_unlock') diff --git a/lparser.c b/lparser.c index 502a9b2df3..bcdcfb6d7d 100644 --- a/lparser.c +++ b/lparser.c @@ -489,11 +489,7 @@ static void adjust_assign (LexState *ls, int nvars, int nexps, expdesc *e) { } -static void enterlevel (LexState *ls) { - lua_State *L = ls->L; - L->nCcalls++; - checklimit(ls->fs, getCcalls(L), LUAI_MAXCCALLS, "C levels"); -} +#define enterlevel(ls) luaE_incCstack(ls->L) #define leavelevel(ls) ((ls)->L->nCcalls--) diff --git a/lstate.c b/lstate.c index 8cda307260..bd1b512000 100644 --- a/lstate.c +++ b/lstate.c @@ -97,25 +97,8 @@ void luaE_setdebt (global_State *g, l_mem debt) { LUA_API int lua_setcstacklimit (lua_State *L, unsigned int limit) { - global_State *g = G(L); - int ccalls; - luaE_freeCI(L); /* release unused CIs */ - ccalls = getCcalls(L); - if (limit >= 40000) - return 0; /* out of bounds */ - limit += CSTACKERR; - if (L != g-> mainthread) - return 0; /* only main thread can change the C stack */ - else if (ccalls <= CSTACKERR) - return 0; /* handling overflow */ - else { - int diff = limit - g->Cstacklimit; - if (ccalls + diff <= CSTACKERR) - return 0; /* new limit would cause an overflow */ - g->Cstacklimit = limit; /* set new limit */ - L->nCcalls += diff; /* correct 'nCcalls' */ - return limit - diff - CSTACKERR; /* success; return previous limit */ - } + UNUSED(L); UNUSED(limit); + return LUAI_MAXCCALLS; /* warning?? */ } @@ -172,6 +155,28 @@ void luaE_shrinkCI (lua_State *L) { } +/* +** Called when 'getCcalls(L)' larger or equal to LUAI_MAXCCALLS. +** If equal, raises an overflow error. If value is larger than +** LUAI_MAXCCALLS (which means it is handling an overflow) but +** not much larger, does not report an error (to allow overflow +** handling to work). +*/ +void luaE_checkcstack (lua_State *L) { + if (getCcalls(L) == LUAI_MAXCCALLS) + luaG_runerror(L, "C stack overflow"); + else if (getCcalls(L) >= (LUAI_MAXCCALLS / 10 * 11)) + luaD_throw(L, LUA_ERRERR); /* error while handing stack error */ +} + + +LUAI_FUNC void luaE_incCstack (lua_State *L) { + L->nCcalls++; + if (getCcalls(L) >= LUAI_MAXCCALLS) + luaE_checkcstack(L); +} + + static void stack_init (lua_State *L1, lua_State *L) { int i; CallInfo *ci; /* initialize stack array */ @@ -357,7 +362,7 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { preinit_thread(L, g); g->allgc = obj2gco(L); /* by now, only object is the main thread */ L->next = NULL; - g->Cstacklimit = L->nCcalls = 0; + L->nCcalls = 0; incnny(L); /* main thread is always non yieldable */ g->frealloc = f; g->ud = ud; diff --git a/lstate.h b/lstate.h index 983aa0d528..a05db376a4 100644 --- a/lstate.h +++ b/lstate.h @@ -87,48 +87,12 @@ /* -** About 'nCcalls': each thread in Lua (a lua_State) keeps a count of -** how many "C calls" it still can do in the C stack, to avoid C-stack -** overflow. This count is very rough approximation; it considers only -** recursive functions inside the interpreter, as non-recursive calls -** can be considered using a fixed (although unknown) amount of stack -** space. -** -** The count has two parts: the lower part is the count itself; the -** higher part counts the number of non-yieldable calls in the stack. -** (They are together so that we can change both with one instruction.) -** -** Because calls to external C functions can use an unknown amount -** of space (e.g., functions using an auxiliary buffer), calls -** to these functions add more than one to the count (see CSTACKCF). -** -** The proper count excludes the number of CallInfo structures allocated -** by Lua, as a kind of "potential" calls. So, when Lua calls a function -** (and "consumes" one CallInfo), it needs neither to decrement nor to -** check 'nCcalls', as its use of C stack is already accounted for. -*/ - -/* number of "C stack slots" used by an external C function */ -#define CSTACKCF 10 - - -/* -** The C-stack size is sliced in the following zones: -** - larger than CSTACKERR: normal stack; -** - [CSTACKMARK, CSTACKERR]: buffer zone to signal a stack overflow; -** - [CSTACKCF, CSTACKERRMARK]: error-handling zone; -** - below CSTACKERRMARK: buffer zone to signal overflow during overflow; -** (Because the counter can be decremented CSTACKCF at once, we need -** the so called "buffer zones", with at least that size, to properly -** detect a change from one zone to the next.) +** About 'nCcalls': This count has two parts: the lower 16 bits counts +** the number of recursive invocations in the C stack; the higher +** 16 bits counts the number of non-yieldable calls in the stack. +** (They are together so that we can change and save both with one +** instruction.) */ -#define CSTACKERR (8 * CSTACKCF) -#define CSTACKMARK (CSTACKERR - (CSTACKCF + 2)) -#define CSTACKERRMARK (CSTACKCF + 2) - - -/* initial limit for the C-stack of threads */ -#define CSTACKTHREAD (2 * CSTACKERR) /* true if this thread does not have non-yieldable calls in the stack */ @@ -144,7 +108,8 @@ /* Decrement the number of non-yieldable calls */ #define decnny(L) ((L)->nCcalls -= 0x10000) - +/* Non-yieldable call increment */ +#define nyci (0x10000 | 1) @@ -290,7 +255,6 @@ typedef struct global_State { TString *strcache[STRCACHE_N][STRCACHE_M]; /* cache for strings in API */ lua_WarnFunction warnf; /* warning function */ void *ud_warn; /* auxiliary data to 'warnf' */ - unsigned int Cstacklimit; /* current limit for the C stack */ } global_State; @@ -314,7 +278,7 @@ struct lua_State { CallInfo base_ci; /* CallInfo for first level (C calling Lua) */ volatile lua_Hook hook; ptrdiff_t errfunc; /* current error handling function (stack index) */ - l_uint32 nCcalls; /* number of allowed nested C calls - 'nci' */ + l_uint32 nCcalls; /* number of nested (non-yieldable | C) calls */ int oldpc; /* last pc traced */ int stacksize; int basehookcount; @@ -383,11 +347,11 @@ LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1); LUAI_FUNC CallInfo *luaE_extendCI (lua_State *L); LUAI_FUNC void luaE_freeCI (lua_State *L); LUAI_FUNC void luaE_shrinkCI (lua_State *L); +LUAI_FUNC void luaE_checkcstack (lua_State *L); +LUAI_FUNC void luaE_incCstack (lua_State *L); LUAI_FUNC void luaE_warning (lua_State *L, const char *msg, int tocont); LUAI_FUNC void luaE_warnerror (lua_State *L, const char *where); -#define luaE_exitCcall(L) ((L)->nCcalls++) - #endif diff --git a/luaconf.h b/luaconf.h index 229413d2f1..d9cf18ca1d 100644 --- a/luaconf.h +++ b/luaconf.h @@ -36,21 +36,6 @@ ** ===================================================================== */ -/* >>> move back to llimits.h -@@ LUAI_MAXCCALLS defines the maximum depth for nested calls and -** also limits the maximum depth of other recursive algorithms in -** the implementation, such as syntactic analysis. A value too -** large may allow the interpreter to crash (C-stack overflow). -** The default value seems ok for regular machines, but may be -** too high for restricted hardware. -** The test file 'cstack.lua' may help finding a good limit. -** (It will crash with a limit too high.) -*/ -#if !defined(LUAI_MAXCCALLS) -#define LUAI_MAXCCALLS 200 -#endif - - /* @@ LUA_USE_C89 controls the use of non-ISO-C89 features. ** Define it if you want Lua to avoid the use of a few C99 features diff --git a/lvm.c b/lvm.c index a232e1e79e..eadf66bf8a 100644 --- a/lvm.c +++ b/lvm.c @@ -1124,7 +1124,7 @@ void luaV_finishOp (lua_State *L) { void luaV_execute (lua_State *L, CallInfo *ci) { - const CallInfo *origci = ci; + CallInfo * const origci = ci; LClosure *cl; TValue *k; StkId base; @@ -1624,7 +1624,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmcase(OP_TAILCALL) { int b = GETARG_B(i); /* number of arguments + 1 (function) */ int nparams1 = GETARG_C(i); - /* delat is virtual 'func' - real 'func' (vararg functions) */ + /* delta is virtual 'func' - real 'func' (vararg functions) */ int delta = (nparams1) ? ci->u.l.nextraargs + nparams1 : 0; if (b != 0) L->top = ra + b; @@ -1648,7 +1648,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { updatetrap(ci); updatestack(ci); /* stack may have been relocated */ ci->func -= delta; - luaD_poscall(L, ci, cast_int(L->top - ra)); + luaD_poscall(L, ci, cast_int(L->top - ra)); /* finish caller */ goto ret; } ci->func -= delta; diff --git a/manual/manual.of b/manual/manual.of index 8b34b5bd2a..86631bbcc2 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -2436,8 +2436,16 @@ When you interact with the Lua API, you are responsible for ensuring consistency. In particular, @emph{you are responsible for controlling stack overflow}. -You can use the function @Lid{lua_checkstack} -to ensure that the stack has enough space for pushing new elements. +When you call any API function, +you must ensure the stack has enough room to accommodate the results. + +There is one exception to the above rule: +When you call a Lua function +without a fixed number of results @seeF{lua_call}, +Lua ensures that the stack has enough space for all results. +However, it does not ensure any extra space. +So, before pushing anything on the stack after such a call +you should use @Lid{lua_checkstack}. Whenever Lua calls C, it ensures that the stack has space for @@ -2446,13 +2454,9 @@ that is, you can safely push up to @id{LUA_MINSTACK} values into it. @id{LUA_MINSTACK} is defined as 20, so that usually you do not have to worry about stack space unless your code has loops pushing elements onto the stack. - -When you call a Lua function -without a fixed number of results @seeF{lua_call}, -Lua ensures that the stack has enough space for all results, -but it does not ensure any extra space. -So, before pushing anything on the stack after such a call -you should use @Lid{lua_checkstack}. +Whenever necessary, +you can use the function @Lid{lua_checkstack} +to ensure that the stack has enough space for pushing new elements. } @@ -2695,7 +2699,7 @@ Therefore, if a @N{C function} @id{foo} calls an API function and this API function yields (directly or indirectly by calling another function that yields), Lua cannot return to @id{foo} any more, -because the @id{longjmp} removes its frame from the C stack. +because the @id{longjmp} removes its frame from the @N{C stack}. To avoid this kind of problem, Lua raises an error whenever it tries to yield across an API call, @@ -2719,7 +2723,7 @@ After the thread resumes, it eventually will finish running the callee function. However, the callee function cannot return to the original function, -because its frame in the C stack was destroyed by the yield. +because its frame in the @N{C stack} was destroyed by the yield. Instead, Lua calls a @def{continuation function}, which was given as an argument to the callee function. As the name implies, @@ -2841,7 +2845,7 @@ and therefore may raise any errors. Converts the @x{acceptable index} @id{idx} into an equivalent @x{absolute index} -(that is, one that does not depend on the stack top). +(that is, one that does not depend on the stack size). } @@ -4340,7 +4344,7 @@ as if it was already marked. Note that, both in case of errors and of a regular return, by the time the @idx{__close} metamethod runs, the @N{C stack} was already unwound, -so that any automatic C variable declared in the calling function +so that any automatic @N{C variable} declared in the calling function will be out of scope. } @@ -4955,20 +4959,6 @@ calling @Lid{lua_yield} with @id{nresults} equal to zero } -@APIEntry{int (lua_setcstacklimit) (lua_State *L, unsigned int limit);| -@apii{0,0,-} - -Sets a new limit for the C stack. -This limit controls how deeply nested calls can go in Lua, -with the intent of avoiding a stack overflow. -Returns the old limit in case of success, -or zero in case of error. -For more details about this function, -see @Lid{debug.setcstacklimit}, -its equivalent in the standard library. - -} - @APIEntry{void lua_sethook (lua_State *L, lua_Hook f, int mask, int count);| @apii{0,0,-} @@ -8756,34 +8746,6 @@ to the userdata @id{u} plus a boolean, } -@LibEntry{debug.setcstacklimit (limit)| - -Sets a new limit for the C stack. -This limit controls how deeply nested calls can go in Lua, -with the intent of avoiding a stack overflow. -A limit too small restricts recursive calls pointlessly; -a limit too large exposes the interpreter to stack-overflow crashes. -Unfortunately, there is no way to know a priori -the maximum safe limit for a platform. - -Each call made from Lua code counts one unit. -Other operations (e.g., calls made from C to Lua or resuming a coroutine) -may have a higher cost. - -This function has the following restrictions: -@description{ -@item{It can only be called from the main coroutine (thread);} -@item{It cannot be called while handling a stack-overflow error;} -@item{@id{limit} must be less than 40000;} -@item{@id{limit} cannot be less than the amount of C stack in use.} -} -If a call does not respect some restriction, -it returns a false value. -Otherwise, -the call returns the old limit. - -} - @LibEntry{debug.sethook ([thread,] hook, mask [, count])| Sets the given function as the debug hook. diff --git a/testes/cstack.lua b/testes/cstack.lua index c1177f3b74..5767adf630 100644 --- a/testes/cstack.lua +++ b/testes/cstack.lua @@ -1,75 +1,29 @@ -- $Id: testes/cstack.lua $ -- See Copyright Notice in file all.lua -do return end - -local debug = require "debug" print"testing C-stack overflow detection" -print"If this test crashes, see its file ('cstack.lua')" -- Segmentation faults in these tests probably result from a C-stack --- overflow. To avoid these errors, you can use the function --- 'debug.setcstacklimit' to set a smaller limit for the use of --- C stack by Lua. After finding a reliable limit, you might want --- to recompile Lua with this limit as the value for --- the constant 'LUAI_MAXCCALLS', which defines the default limit. --- (The default limit is printed by this test.) +-- overflow. To avoid these errors, you should set a smaller limit for +-- the use of C stack by Lua, by changing the constant 'LUAI_MAXCCALLS'. -- Alternatively, you can ensure a larger stack for the program. --- For Linux, a limit up to 30_000 seems Ok. Windows cannot go much --- higher than 2_000. - - --- get and print original limit -local origlimit = debug.setcstacklimit(400) -print("default stack limit: " .. origlimit) - - --- Do the tests using the original limit. Or else you may want to change --- 'currentlimit' to lower values to avoid a seg. fault or to higher --- values to check whether they are reliable. -local currentlimit = origlimit -debug.setcstacklimit(currentlimit) -print("current stack limit: " .. currentlimit) - local function checkerror (msg, f, ...) local s, err = pcall(f, ...) assert(not s and string.find(err, msg)) end --- auxiliary function to keep 'count' on the screen even if the program --- crashes. -local count -local back = string.rep("\b", 8) -local function progress () - count = count + 1 - local n = string.format("%-8d", count) - io.stderr:write(back, n) -- erase previous value and write new one -end - - -do print("testing simple recursion:") - count = 0 - local function foo () - progress() - foo() -- do recursive calls until a stack error (or crash) - end - checkerror("stack overflow", foo) - print("\tfinal count: ", count) -end - - do print("testing stack overflow in message handling") - count = 0 + local count = 0 local function loop (x, y, z) - progress() + count = count + 1 return 1 + loop(x, y, z) end local res, msg = xpcall(loop, loop) assert(msg == "error in error handling") - print("\tfinal count: ", count) + print("final count: ", count) end @@ -82,97 +36,66 @@ do print("testing recursion inside pattern matching") end local m = f(80) assert(#m == 80) - checkerror("too complex", f, 200000) + checkerror("too complex", f, 2000) end do print("testing stack-overflow in recursive 'gsub'") - count = 0 + local count = 0 local function foo () - progress() + count = count + 1 string.gsub("a", ".", foo) end checkerror("stack overflow", foo) - print("\tfinal count: ", count) + print("final count: ", count) print("testing stack-overflow in recursive 'gsub' with metatables") - count = 0 + local count = 0 local t = setmetatable({}, {__index = foo}) foo = function () count = count + 1 - progress(count) string.gsub("a", ".", t) end checkerror("stack overflow", foo) - print("\tfinal count: ", count) + print("final count: ", count) end + do -- bug in 5.4.0 print("testing limits in coroutines inside deep calls") - count = 0 + local count = 0 local lim = 1000 local function stack (n) - progress() if n > 0 then return stack(n - 1) + 1 else coroutine.wrap(function () + count = count + 1 stack(lim) end)() end end - print(xpcall(stack, function () return "ok" end, lim)) + local st, msg = xpcall(stack, function () return "ok" end, lim) + assert(not st and msg == "ok") + print("final count: ", count) end -do print("testing changes in C-stack limit") +do + print("nesting of resuming yielded coroutines") + local count = 0 - -- Just an alternative limit, different from the current one - -- (smaller to avoid stack overflows) - local alterlimit = currentlimit * 8 // 10 - - assert(not debug.setcstacklimit(0)) -- limit too small - assert(not debug.setcstacklimit(50000)) -- limit too large - local co = coroutine.wrap (function () - return debug.setcstacklimit(alterlimit) - end) - assert(not co()) -- cannot change C stack inside coroutine - - local n - local function foo () n = n + 1; foo () end - - local function check () - n = 0 - pcall(foo) - return n + local function body () + coroutine.yield() + local f = coroutine.wrap(body) + f(); -- start new coroutine (will stop in previous yield) + count = count + 1 + f() -- call it recursively end - -- set limit to 'alterlimit' - assert(debug.setcstacklimit(alterlimit) == currentlimit) - local limalter = check() - -- set a very low limit (given that there are already several active - -- calls to arrive here) - local lowlimit = 38 - assert(debug.setcstacklimit(lowlimit) == alterlimit) - -- usable limit is much lower, due to active calls - local actuallow = check() - assert(actuallow < lowlimit - 30) - -- now, add 'lowlimit' extra slots, which should all be available - assert(debug.setcstacklimit(lowlimit + lowlimit) == lowlimit) - local lim2 = check() - assert(lim2 == actuallow + lowlimit) - - - -- 'setcstacklimit' works inside protected calls. (The new stack - -- limit is kept when 'pcall' returns.) - assert(pcall(function () - assert(debug.setcstacklimit(alterlimit) == lowlimit * 2) - assert(check() <= limalter) - end)) - - assert(check() == limalter) - -- restore original limit - assert(debug.setcstacklimit(origlimit) == alterlimit) + local f = coroutine.wrap(body) + f() + assert(not pcall(f)) + print("final count: ", count) end - print'OK' diff --git a/testes/errors.lua b/testes/errors.lua index 88918df714..f975b3dd2e 100644 --- a/testes/errors.lua +++ b/testes/errors.lua @@ -532,7 +532,8 @@ local function testrep (init, rep, close, repc, finalresult) end s = init .. string.rep(rep, 500) local res, msg = load(s) -- 500 levels not ok - assert(not res and string.find(msg, "too many")) + assert(not res and (string.find(msg, "too many") or + string.find(msg, "overflow"))) end testrep("local a; a", ",a", "= 1", ",1") -- multiple assignment From 490d42b5f89563a94994505c75e24086b0a487e6 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 24 Sep 2020 13:26:51 -0300 Subject: [PATCH 0619/1145] Correct handling of 'luaV_execute' invocations The previous stackless implementations marked all 'luaV_execute' invocations as fresh. However, re-entering 'luaV_execute' when resuming a coroutine should not be a fresh invocation. (It works because 'unroll' called 'luaV_execute' for each call entry, but it was slower than letting 'luaV_execute' finish all non-fresh invocations.) --- ldo.c | 25 ++++++++++++++----------- ldo.h | 2 +- lstate.c | 2 +- lstate.h | 15 ++++++++------- lvm.c | 25 +++++++++++++------------ 5 files changed, 37 insertions(+), 32 deletions(-) diff --git a/ldo.c b/ldo.c index 0a6a716927..052c57a9a3 100644 --- a/ldo.c +++ b/ldo.c @@ -449,12 +449,13 @@ void luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, int narg1) { /* ** Prepares the call to a function (C or Lua). For C functions, also do -** the call. The function to be called is at '*func'. The arguments are -** on the stack, right after the function. Returns true if the call was -** made (it was a C function). When returns true, all the results are -** on the stack, starting at the original function position. +** the call. The function to be called is at '*func'. The arguments +** are on the stack, right after the function. Returns the CallInfo +** to be executed, if it was a Lua function. Otherwise (a C function) +** returns NULL, with all the results on the stack, starting at the +** original function position. */ -int luaD_precall (lua_State *L, StkId func, int nresults) { +CallInfo *luaD_precall (lua_State *L, StkId func, int nresults) { lua_CFunction f; retry: switch (ttypetag(s2v(func))) { @@ -482,7 +483,7 @@ int luaD_precall (lua_State *L, StkId func, int nresults) { lua_lock(L); api_checknelems(L, n); luaD_poscall(L, ci, n); - return 1; + return NULL; } case LUA_VLCL: { /* Lua function */ CallInfo *ci; @@ -494,14 +495,13 @@ int luaD_precall (lua_State *L, StkId func, int nresults) { L->ci = ci = next_ci(L); ci->nresults = nresults; ci->u.l.savedpc = p->code; /* starting point */ - ci->callstatus = 0; ci->top = func + 1 + fsize; ci->func = func; L->ci = ci; for (; narg < nfixparams; narg++) setnilvalue(s2v(L->top++)); /* complete missing arguments */ lua_assert(ci->top <= L->stack_last); - return 0; + return ci; } default: { /* not a function */ checkstackGCp(L, 1, func); /* space for metamethod */ @@ -518,11 +518,14 @@ int luaD_precall (lua_State *L, StkId func, int nresults) { ** increment number of non-yieldable calls). */ static void docall (lua_State *L, StkId func, int nResults, int inc) { + CallInfo *ci; L->nCcalls += inc; - if (getCcalls(L) >= LUAI_MAXCCALLS) + if (unlikely(getCcalls(L) >= LUAI_MAXCCALLS)) luaE_checkcstack(L); - if (!luaD_precall(L, func, nResults)) /* is a Lua function? */ - luaV_execute(L, L->ci); /* call it */ + if ((ci = luaD_precall(L, func, nResults)) != NULL) { /* Lua function? */ + ci->callstatus = CIST_FRESH; /* mark that it is a "fresh" execute */ + luaV_execute(L, ci); /* call it */ + } L->nCcalls -= inc; } diff --git a/ldo.h b/ldo.h index 7d032117bb..4d30d072ed 100644 --- a/ldo.h +++ b/ldo.h @@ -59,7 +59,7 @@ LUAI_FUNC void luaD_hook (lua_State *L, int event, int line, int fTransfer, int nTransfer); LUAI_FUNC void luaD_hookcall (lua_State *L, CallInfo *ci); LUAI_FUNC void luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, int n); -LUAI_FUNC int luaD_precall (lua_State *L, StkId func, int nResults); +LUAI_FUNC CallInfo *luaD_precall (lua_State *L, StkId func, int nResults); LUAI_FUNC void luaD_call (lua_State *L, StkId func, int nResults); LUAI_FUNC void luaD_callnoyield (lua_State *L, StkId func, int nResults); LUAI_FUNC void luaD_tryfuncTM (lua_State *L, StkId func); diff --git a/lstate.c b/lstate.c index bd1b512000..13c1ff0f70 100644 --- a/lstate.c +++ b/lstate.c @@ -172,7 +172,7 @@ void luaE_checkcstack (lua_State *L) { LUAI_FUNC void luaE_incCstack (lua_State *L) { L->nCcalls++; - if (getCcalls(L) >= LUAI_MAXCCALLS) + if (unlikely(getCcalls(L) >= LUAI_MAXCCALLS)) luaE_checkcstack(L); } diff --git a/lstate.h b/lstate.h index a05db376a4..5573898c24 100644 --- a/lstate.h +++ b/lstate.h @@ -183,14 +183,15 @@ typedef struct CallInfo { */ #define CIST_OAH (1<<0) /* original value of 'allowhook' */ #define CIST_C (1<<1) /* call is running a C function */ -#define CIST_HOOKED (1<<2) /* call is running a debug hook */ -#define CIST_YPCALL (1<<3) /* call is a yieldable protected call */ -#define CIST_TAIL (1<<4) /* call was tail called */ -#define CIST_HOOKYIELD (1<<5) /* last hook called yielded */ -#define CIST_FIN (1<<6) /* call is running a finalizer */ -#define CIST_TRAN (1<<7) /* 'ci' has transfer information */ +#define CIST_FRESH (1<<2) /* call is on a fresh "luaV_execute" frame */ +#define CIST_HOOKED (1<<3) /* call is running a debug hook */ +#define CIST_YPCALL (1<<4) /* call is a yieldable protected call */ +#define CIST_TAIL (1<<5) /* call was tail called */ +#define CIST_HOOKYIELD (1<<6) /* last hook called yielded */ +#define CIST_FIN (1<<7) /* call is running a finalizer */ +#define CIST_TRAN (1<<8) /* 'ci' has transfer information */ #if defined(LUA_COMPAT_LT_LE) -#define CIST_LEQ (1<<8) /* using __lt for __le */ +#define CIST_LEQ (1<<9) /* using __lt for __le */ #endif /* active function is a Lua function */ diff --git a/lvm.c b/lvm.c index eadf66bf8a..51b22d8123 100644 --- a/lvm.c +++ b/lvm.c @@ -1124,7 +1124,6 @@ void luaV_finishOp (lua_State *L) { void luaV_execute (lua_State *L, CallInfo *ci) { - CallInfo * const origci = ci; LClosure *cl; TValue *k; StkId base; @@ -1133,7 +1132,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { #if LUA_USE_JUMPTABLE #include "ljumptab.h" #endif - tailcall: + execute: trap = L->hookmask; cl = clLvalue(s2v(ci->func)); k = cl->p->k; @@ -1607,17 +1606,19 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_CALL) { + CallInfo *newci; int b = GETARG_B(i); int nresults = GETARG_C(i) - 1; if (b != 0) /* fixed number of arguments? */ L->top = ra + b; /* top signals number of arguments */ /* else previous instruction set top */ savepc(L); /* in case of errors */ - if (luaD_precall(L, ra, nresults)) + if ((newci = luaD_precall(L, ra, nresults)) == NULL) updatetrap(ci); /* C call; nothing else to be done */ else { /* Lua call: run function in this same invocation */ - ci = L->ci; - goto tailcall; + ci = newci; + ci->callstatus = 0; /* call re-uses 'luaV_execute' */ + goto execute; } vmbreak; } @@ -1647,13 +1648,13 @@ void luaV_execute (lua_State *L, CallInfo *ci) { luaD_precall(L, ra, LUA_MULTRET); /* call it */ updatetrap(ci); updatestack(ci); /* stack may have been relocated */ - ci->func -= delta; + ci->func -= delta; /* restore 'func' (if vararg) */ luaD_poscall(L, ci, cast_int(L->top - ra)); /* finish caller */ - goto ret; + goto ret; /* caller returns after the tail call */ } - ci->func -= delta; + ci->func -= delta; /* restore 'func' (if vararg) */ luaD_pretailcall(L, ci, ra, b); /* prepare call frame */ - goto tailcall; + goto execute; /* execute the callee */ } vmcase(OP_RETURN) { int n = GETARG_B(i) - 1; /* number of results */ @@ -1706,11 +1707,11 @@ void luaV_execute (lua_State *L, CallInfo *ci) { } } ret: - if (ci == origci) - return; + if (ci->callstatus & CIST_FRESH) + return; /* end this frame */ else { ci = ci->previous; - goto tailcall; + goto execute; /* continue running caller in this frame */ } } vmcase(OP_FORLOOP) { From 0085db4596df99b43fc245f71ee444da68cb830b Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 28 Sep 2020 10:14:06 -0300 Subject: [PATCH 0620/1145] Avoid GCs when testing stack overflow A GC step may invoke some finalizer, which may error and emit a warning due to stack overflfow. --- testes/errors.lua | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/testes/errors.lua b/testes/errors.lua index f975b3dd2e..422c1128d5 100644 --- a/testes/errors.lua +++ b/testes/errors.lua @@ -386,25 +386,33 @@ if not _soft then collectgarbage() print"testing stack overflow" C = 0 - local l = debug.getinfo(1, "l").currentline; function y () C=C+1; y() end + -- get line where stack overflow will happen + local l = debug.getinfo(1, "l").currentline + 1 + local function auxy () C=C+1; auxy() end -- produce a stack overflow + function y () + collectgarbage("stop") -- avoid running finalizers without stack space + auxy() + collectgarbage("restart") + end local function checkstackmessage (m) + print("(expected stack overflow after " .. C .. " calls)") + C = 0 -- prepare next count return (string.find(m, "stack overflow")) end -- repeated stack overflows (to check stack recovery) assert(checkstackmessage(doit('y()'))) - print('+') assert(checkstackmessage(doit('y()'))) - print('+') assert(checkstackmessage(doit('y()'))) - print('+') -- error lines in stack overflow - C = 0 local l1 local function g(x) - l1 = debug.getinfo(x, "l").currentline; y() + l1 = debug.getinfo(x, "l").currentline + 2 + collectgarbage("stop") -- avoid running finalizers without stack space + auxy() + collectgarbage("restart") end local _, stackmsg = xpcall(g, debug.traceback, 1) print('+') From fb172d0a929432856983a51d4139f705d4c01365 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 5 Oct 2020 10:38:41 -0300 Subject: [PATCH 0621/1145] No need for 'volatile' in string.pack/unpack Type punning an address to 'char *' should be always safe. --- lstrlib.c | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/lstrlib.c b/lstrlib.c index a30ec5af29..940a14ca53 100644 --- a/lstrlib.c +++ b/lstrlib.c @@ -1365,7 +1365,6 @@ typedef union Ftypes { float f; double d; lua_Number n; - char buff[5 * sizeof(lua_Number)]; /* enough for any float type */ } Ftypes; @@ -1535,12 +1534,10 @@ static void packint (luaL_Buffer *b, lua_Unsigned n, ** Copy 'size' bytes from 'src' to 'dest', correcting endianness if ** given 'islittle' is different from native endianness. */ -static void copywithendian (volatile char *dest, volatile const char *src, +static void copywithendian (char *dest, const char *src, int size, int islittle) { - if (islittle == nativeendian.little) { - while (size-- != 0) - *(dest++) = *(src++); - } + if (islittle == nativeendian.little) + memcpy(dest, src, size); else { dest += size - 1; while (size-- != 0) @@ -1584,14 +1581,14 @@ static int str_pack (lua_State *L) { break; } case Kfloat: { /* floating-point options */ - volatile Ftypes u; + Ftypes u; char *buff = luaL_prepbuffsize(&b, size); lua_Number n = luaL_checknumber(L, arg); /* get argument */ if (size == sizeof(u.f)) u.f = (float)n; /* copy it into 'u' */ else if (size == sizeof(u.d)) u.d = (double)n; else u.n = n; /* move 'u' to final result, correcting endianness if needed */ - copywithendian(buff, u.buff, size, h.islittle); + copywithendian(buff, (char *)&u, size, h.islittle); luaL_addsize(&b, size); break; } @@ -1717,9 +1714,9 @@ static int str_unpack (lua_State *L) { break; } case Kfloat: { - volatile Ftypes u; + Ftypes u; lua_Number num; - copywithendian(u.buff, data + pos, size, h.islittle); + copywithendian((char *)&u, data + pos, size, h.islittle); if (size == sizeof(u.f)) num = (lua_Number)u.f; else if (size == sizeof(u.d)) num = (lua_Number)u.d; else num = u.n; From 9ecd446141f572252a57cb33d2bba6aa00d96a55 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 6 Oct 2020 13:37:41 -0300 Subject: [PATCH 0622/1145] Avoid shrinking stacks to often Shrink a stack only when the final stack size can be at most 2/3 the previous size with half of its entries empty. This commit also improves the clarity of 'luaD_growstack'. --- ldo.c | 54 +++++++++++++++++++++++++++++++++-------------- testes/cstack.lua | 50 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 87 insertions(+), 17 deletions(-) diff --git a/ldo.c b/ldo.c index 052c57a9a3..755db69379 100644 --- a/ldo.c +++ b/ldo.c @@ -207,50 +207,72 @@ int luaD_reallocstack (lua_State *L, int newsize, int raiseerror) { */ int luaD_growstack (lua_State *L, int n, int raiseerror) { int size = L->stacksize; - int newsize = 2 * size; /* tentative new size */ - if (unlikely(size > LUAI_MAXSTACK)) { /* need more space after extra size? */ + if (unlikely(size > LUAI_MAXSTACK)) { + /* if stack is larger than maximum, thread is already using the + extra space reserved for errors, that is, thread is handling + a stack error; cannot grow further than that. */ + lua_assert(L->stacksize == ERRORSTACKSIZE); if (raiseerror) luaD_throw(L, LUA_ERRERR); /* error inside message handler */ - else return 0; + return 0; /* if not 'raiseerror', just signal it */ } else { + int newsize = 2 * size; /* tentative new size */ int needed = cast_int(L->top - L->stack) + n + EXTRA_STACK; if (newsize > LUAI_MAXSTACK) /* cannot cross the limit */ newsize = LUAI_MAXSTACK; if (newsize < needed) /* but must respect what was asked for */ newsize = needed; - if (unlikely(newsize > LUAI_MAXSTACK)) { /* stack overflow? */ + if (likely(newsize <= LUAI_MAXSTACK)) + return luaD_reallocstack(L, newsize, raiseerror); + else { /* stack overflow */ /* add extra size to be able to handle the error message */ luaD_reallocstack(L, ERRORSTACKSIZE, raiseerror); if (raiseerror) luaG_runerror(L, "stack overflow"); - else return 0; + return 0; } - } /* else no errors */ - return luaD_reallocstack(L, newsize, raiseerror); + } } static int stackinuse (lua_State *L) { CallInfo *ci; + int res; StkId lim = L->top; for (ci = L->ci; ci != NULL; ci = ci->previous) { if (lim < ci->top) lim = ci->top; } lua_assert(lim <= L->stack_last); - return cast_int(lim - L->stack) + 1; /* part of stack in use */ + res = cast_int(lim - L->stack) + 1; /* part of stack in use */ + if (res < LUA_MINSTACK) + res = LUA_MINSTACK; /* ensure a minimum size */ + return res; } +/* +** If stack size is more than 3 times the current use, reduce that size +** to twice the current use. (So, the final stack size is at most 2/3 the +** previous size, and half of its entries are empty.) +** As a particular case, if stack was handling a stack overflow and now +** it is not, 'max' (limited by LUAI_MAXSTACK) will be smaller than +** 'stacksize' (equal to ERRORSTACKSIZE in this case), and so the stack +** will be reduced to a "regular" size. +*/ void luaD_shrinkstack (lua_State *L) { int inuse = stackinuse(L); - int goodsize = inuse + BASIC_STACK_SIZE; - if (goodsize > LUAI_MAXSTACK) - goodsize = LUAI_MAXSTACK; /* respect stack limit */ + int nsize = inuse * 2; /* proposed new size */ + int max = inuse * 3; /* maximum "reasonable" size */ + if (max > LUAI_MAXSTACK) { + max = LUAI_MAXSTACK; /* respect stack limit */ + if (nsize > LUAI_MAXSTACK) + nsize = LUAI_MAXSTACK; + } /* if thread is currently not handling a stack overflow and its - good size is smaller than current size, shrink its stack */ - if (inuse <= (LUAI_MAXSTACK - EXTRA_STACK) && goodsize < L->stacksize) - luaD_reallocstack(L, goodsize, 0); /* ok if that fails */ + size is larger than maximum "reasonable" size, shrink it */ + if (inuse <= (LUAI_MAXSTACK - EXTRA_STACK) && L->stacksize > max) + luaD_reallocstack(L, nsize, 0); /* ok if that fails */ else /* don't change stack */ condmovestack(L,{},{}); /* (change only for debugging) */ luaE_shrinkCI(L); /* shrink CI list */ @@ -625,7 +647,7 @@ static int recover (lua_State *L, int status) { luaD_seterrorobj(L, status, oldtop); L->ci = ci; L->allowhook = getoah(ci->callstatus); /* restore original 'allowhook' */ - luaD_shrinkstack(L); + luaD_shrinkstack(L); /* restore stack size in case of overflow */ L->errfunc = ci->u.c.old_errfunc; return 1; /* continue running the coroutine */ } @@ -768,7 +790,7 @@ int luaD_pcall (lua_State *L, Pfunc func, void *u, status = luaF_close(L, oldtop, status); oldtop = restorestack(L, old_top); /* previous call may change stack */ luaD_seterrorobj(L, status, oldtop); - luaD_shrinkstack(L); + luaD_shrinkstack(L); /* restore stack size in case of overflow */ } L->errfunc = old_errfunc; return status; diff --git a/testes/cstack.lua b/testes/cstack.lua index 5767adf630..8ac48e8940 100644 --- a/testes/cstack.lua +++ b/testes/cstack.lua @@ -2,7 +2,7 @@ -- See Copyright Notice in file all.lua -print"testing C-stack overflow detection" +print"testing stack overflow detection" -- Segmentation faults in these tests probably result from a C-stack -- overflow. To avoid these errors, you should set a smaller limit for @@ -98,4 +98,52 @@ do print("final count: ", count) end + +if T then + print("testing stack recovery") + local N = 0 -- trace number of calls + local LIM = -1 -- will store N just before stack overflow + + -- trace stack size; after stack overflow, it should be + -- the maximum allowed stack size. + local stack1 + local dummy + + local function err(msg) + assert(string.find(msg, "stack overflow")) + local _, stacknow = T.stacklevel() + assert(stacknow == stack1 + 200) + end + + -- When LIM==-1, the 'if' is not executed, so this function only + -- counts and stores the stack limits up to overflow. Then, LIM + -- becomes N, and then the 'if' code is run when the stack is + -- full. Then, there is a stack overflow inside 'xpcall', after which + -- the stack must have been restored back to its maximum normal size. + local function f() + dummy, stack1 = T.stacklevel() + if N == LIM then + xpcall(f, err) + local _, stacknow = T.stacklevel() + assert(stacknow == stack1) + return + end + N = N + 1 + f() + end + + local topB, sizeB -- top and size Before overflow + local topA, sizeA -- top and size After overflow + topB, sizeB = T.stacklevel() + xpcall(f, err) + topA, sizeA = T.stacklevel() + -- sizes should be comparable + assert(topA == topB and sizeA < sizeB * 2) + print(string.format("maximum stack size: %d", stack1)) + LIM = N -- will stop recursion at maximum level + N = 0 -- to count again + f() + print"+" +end + print'OK' From 5aa36e894f5a0348dfd19bd9cdcdd27ce8aa5f05 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 6 Oct 2020 15:50:24 -0300 Subject: [PATCH 0623/1145] No more field 'lua_State.stacksize' The stack size is derived from 'stack_last', when needed. Moreover, the handling of stack sizes is more consistent, always excluding the extra space except when allocating/deallocating the array. --- ldo.c | 19 +++++++++---------- lgc.c | 5 ++--- lstate.c | 8 +++----- lstate.h | 13 ++++++++++--- ltests.c | 8 ++++---- lvm.c | 2 +- 6 files changed, 29 insertions(+), 26 deletions(-) diff --git a/ldo.c b/ldo.c index 755db69379..3202490e33 100644 --- a/ldo.c +++ b/ldo.c @@ -182,10 +182,10 @@ static void correctstack (lua_State *L, StkId oldstack, StkId newstack) { int luaD_reallocstack (lua_State *L, int newsize, int raiseerror) { - int lim = L->stacksize; - StkId newstack = luaM_reallocvector(L, L->stack, lim, newsize, StackValue); + int lim = stacksize(L); + StkId newstack = luaM_reallocvector(L, L->stack, + lim + EXTRA_STACK, newsize + EXTRA_STACK, StackValue); lua_assert(newsize <= LUAI_MAXSTACK || newsize == ERRORSTACKSIZE); - lua_assert(L->stack_last - L->stack == L->stacksize - EXTRA_STACK); if (unlikely(newstack == NULL)) { /* reallocation failed? */ if (raiseerror) luaM_error(L); @@ -195,8 +195,7 @@ int luaD_reallocstack (lua_State *L, int newsize, int raiseerror) { setnilvalue(s2v(newstack + lim)); /* erase new segment */ correctstack(L, L->stack, newstack); L->stack = newstack; - L->stacksize = newsize; - L->stack_last = L->stack + newsize - EXTRA_STACK; + L->stack_last = L->stack + newsize; return 1; } @@ -206,19 +205,19 @@ int luaD_reallocstack (lua_State *L, int newsize, int raiseerror) { ** is true, raises any error; otherwise, return 0 in case of errors. */ int luaD_growstack (lua_State *L, int n, int raiseerror) { - int size = L->stacksize; + int size = stacksize(L); if (unlikely(size > LUAI_MAXSTACK)) { /* if stack is larger than maximum, thread is already using the extra space reserved for errors, that is, thread is handling a stack error; cannot grow further than that. */ - lua_assert(L->stacksize == ERRORSTACKSIZE); + lua_assert(stacksize(L) == ERRORSTACKSIZE); if (raiseerror) luaD_throw(L, LUA_ERRERR); /* error inside message handler */ return 0; /* if not 'raiseerror', just signal it */ } else { int newsize = 2 * size; /* tentative new size */ - int needed = cast_int(L->top - L->stack) + n + EXTRA_STACK; + int needed = cast_int(L->top - L->stack) + n; if (newsize > LUAI_MAXSTACK) /* cannot cross the limit */ newsize = LUAI_MAXSTACK; if (newsize < needed) /* but must respect what was asked for */ @@ -257,7 +256,7 @@ static int stackinuse (lua_State *L) { ** previous size, and half of its entries are empty.) ** As a particular case, if stack was handling a stack overflow and now ** it is not, 'max' (limited by LUAI_MAXSTACK) will be smaller than -** 'stacksize' (equal to ERRORSTACKSIZE in this case), and so the stack +** stacksize (equal to ERRORSTACKSIZE in this case), and so the stack ** will be reduced to a "regular" size. */ void luaD_shrinkstack (lua_State *L) { @@ -271,7 +270,7 @@ void luaD_shrinkstack (lua_State *L) { } /* if thread is currently not handling a stack overflow and its size is larger than maximum "reasonable" size, shrink it */ - if (inuse <= (LUAI_MAXSTACK - EXTRA_STACK) && L->stacksize > max) + if (inuse <= LUAI_MAXSTACK && stacksize(L) > max) luaD_reallocstack(L, nsize, 0); /* ok if that fails */ else /* don't change stack */ condmovestack(L,{},{}); /* (change only for debugging) */ diff --git a/lgc.c b/lgc.c index 4a7bcaed37..3b8d0ed6d4 100644 --- a/lgc.c +++ b/lgc.c @@ -633,8 +633,7 @@ static int traversethread (global_State *g, lua_State *th) { for (uv = th->openupval; uv != NULL; uv = uv->u.open.next) markobject(g, uv); /* open upvalues cannot be collected */ if (g->gcstate == GCSatomic) { /* final traversal? */ - StkId lim = th->stack + th->stacksize; /* real end of stack */ - for (; o < lim; o++) /* clear not-marked stack slice */ + for (; o < th->stack_last; o++) /* clear not-marked stack slice */ setnilvalue(s2v(o)); /* 'remarkupvals' may have removed thread from 'twups' list */ if (!isintwups(th) && th->openupval != NULL) { @@ -644,7 +643,7 @@ static int traversethread (global_State *g, lua_State *th) { } else if (!g->gcemergency) luaD_shrinkstack(th); /* do not change stack in emergency cycle */ - return 1 + th->stacksize; + return 1 + stacksize(th); } diff --git a/lstate.c b/lstate.c index 13c1ff0f70..76df6a20d9 100644 --- a/lstate.c +++ b/lstate.c @@ -180,12 +180,11 @@ LUAI_FUNC void luaE_incCstack (lua_State *L) { static void stack_init (lua_State *L1, lua_State *L) { int i; CallInfo *ci; /* initialize stack array */ - L1->stack = luaM_newvector(L, BASIC_STACK_SIZE, StackValue); - L1->stacksize = BASIC_STACK_SIZE; + L1->stack = luaM_newvector(L, BASIC_STACK_SIZE + EXTRA_STACK, StackValue); for (i = 0; i < BASIC_STACK_SIZE; i++) setnilvalue(s2v(L1->stack + i)); /* erase new stack */ L1->top = L1->stack; - L1->stack_last = L1->stack + L1->stacksize - EXTRA_STACK; + L1->stack_last = L1->stack + BASIC_STACK_SIZE; /* initialize first ci */ ci = &L1->base_ci; ci->next = ci->previous = NULL; @@ -206,7 +205,7 @@ static void freestack (lua_State *L) { L->ci = &L->base_ci; /* free the entire 'ci' list */ luaE_freeCI(L); lua_assert(L->nci == 0); - luaM_freearray(L, L->stack, L->stacksize); /* free stack array */ + luaM_freearray(L, L->stack, stacksize(L) + EXTRA_STACK); /* free stack */ } @@ -256,7 +255,6 @@ static void preinit_thread (lua_State *L, global_State *g) { L->stack = NULL; L->ci = NULL; L->nci = 0; - L->stacksize = 0; L->twups = L; /* thread has no upvalues */ L->errorJmp = NULL; L->hook = NULL; diff --git a/lstate.h b/lstate.h index 5573898c24..cbcf07e203 100644 --- a/lstate.h +++ b/lstate.h @@ -127,12 +127,20 @@ struct lua_longjmp; /* defined in ldo.c */ #endif -/* extra stack space to handle TM calls and some other extras */ +/* +** Extra stack space to handle TM calls and some other extras. This +** space is not included in 'stack_last'. It is used only to avoid stack +** checks, either because the element will be promptly popped or because +** there will be a stack check soon after the push. Function frames +** never use this extra space, so it does not need to be kept clean. +*/ #define EXTRA_STACK 5 #define BASIC_STACK_SIZE (2*LUA_MINSTACK) +#define stacksize(th) cast_int((th)->stack_last - (th)->stack) + /* kinds of Garbage Collection */ #define KGC_INC 0 /* incremental gc */ @@ -270,7 +278,7 @@ struct lua_State { StkId top; /* first free slot in the stack */ global_State *l_G; CallInfo *ci; /* call info for current function */ - StkId stack_last; /* last free slot in the stack */ + StkId stack_last; /* end of stack (last element + 1) */ StkId stack; /* stack base */ UpVal *openupval; /* list of open upvalues in this stack */ GCObject *gclist; @@ -281,7 +289,6 @@ struct lua_State { ptrdiff_t errfunc; /* current error handling function (stack index) */ l_uint32 nCcalls; /* number of nested (non-yieldable | C) calls */ int oldpc; /* last pc traced */ - int stacksize; int basehookcount; int hookcount; volatile l_signalT hookmask; diff --git a/ltests.c b/ltests.c index 04e8a00a4d..9945615993 100644 --- a/ltests.c +++ b/ltests.c @@ -430,17 +430,17 @@ static void checkstack (global_State *g, lua_State *L1) { UpVal *uv; lua_assert(!isdead(g, L1)); if (L1->stack == NULL) { /* incomplete thread? */ - lua_assert(L1->stacksize == 0 && L1->openupval == NULL && - L1->ci == NULL); + lua_assert(L1->openupval == NULL && L1->ci == NULL); return; } for (uv = L1->openupval; uv != NULL; uv = uv->u.open.next) lua_assert(upisopen(uv)); /* must be open */ + lua_assert(L1->top <= L1->stack_last); for (ci = L1->ci; ci != NULL; ci = ci->previous) { lua_assert(ci->top <= L1->stack_last); lua_assert(lua_checkpc(ci)); } - for (o = L1->stack; o < L1->stack_last + EXTRA_STACK; o++) + for (o = L1->stack; o < L1->stack_last; o++) checkliveness(L1, s2v(o)); /* entire stack must have valid values */ } @@ -969,7 +969,7 @@ static int hash_query (lua_State *L) { static int stacklevel (lua_State *L) { unsigned long a = 0; lua_pushinteger(L, (L->top - L->stack)); - lua_pushinteger(L, (L->stack_last - L->stack)); + lua_pushinteger(L, stacksize(L)); lua_pushinteger(L, L->nCcalls); lua_pushinteger(L, L->nci); lua_pushinteger(L, (unsigned long)&a); diff --git a/lvm.c b/lvm.c index 51b22d8123..72d3e69520 100644 --- a/lvm.c +++ b/lvm.c @@ -1151,7 +1151,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { StkId ra; /* instruction's A register */ vmfetch(); lua_assert(base == ci->func + 1); - lua_assert(base <= L->top && L->top < L->stack + L->stacksize); + lua_assert(base <= L->top && L->top < L->stack_last); /* invalidate top for instructions not expecting it */ lua_assert(isIT(i) || (cast_void(L->top = base), 1)); vmdispatch (GET_OPCODE(i)) { From 171dcd7d745566e69c61845599705707500a104e Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 7 Oct 2020 10:42:00 -0300 Subject: [PATCH 0624/1145] 'recover' finish of 'luaD_pcall' should follow the original --- ldo.c | 6 +++--- testes/coroutine.lua | 26 ++++++++++++++++++++++---- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/ldo.c b/ldo.c index 3202490e33..5729b19024 100644 --- a/ldo.c +++ b/ldo.c @@ -641,11 +641,11 @@ static int recover (lua_State *L, int status) { if (ci == NULL) return 0; /* no recovery point */ /* "finish" luaD_pcall */ oldtop = restorestack(L, ci->u2.funcidx); - luaF_close(L, oldtop, status); /* may change the stack */ - oldtop = restorestack(L, ci->u2.funcidx); - luaD_seterrorobj(L, status, oldtop); L->ci = ci; L->allowhook = getoah(ci->callstatus); /* restore original 'allowhook' */ + status = luaF_close(L, oldtop, status); /* may change the stack */ + oldtop = restorestack(L, ci->u2.funcidx); + luaD_seterrorobj(L, status, oldtop); luaD_shrinkstack(L); /* restore stack size in case of overflow */ L->errfunc = ci->u.c.old_errfunc; return 1; /* continue running the coroutine */ diff --git a/testes/coroutine.lua b/testes/coroutine.lua index 955f677652..5b9271510e 100644 --- a/testes/coroutine.lua +++ b/testes/coroutine.lua @@ -124,6 +124,11 @@ x, a = nil -- coroutine closing + +local function func2close (f) + return setmetatable({}, {__close = f}) +end + do -- ok to close a dead coroutine local co = coroutine.create(print) @@ -146,10 +151,6 @@ do -- to-be-closed variables in coroutines local X - local function func2close (f) - return setmetatable({}, {__close = f}) - end - co = coroutine.create(function () local x = func2close(function (self, err) assert(err == nil); X = false @@ -192,6 +193,23 @@ do end +do + -- versus pcall in coroutines + local X = false + local Y = false + function foo () + local x = func2close(function (self, err) + Y = debug.getinfo(2) + X = err + end) + error(43) + end + co = coroutine.create(function () return pcall(foo) end) + local st1, st2, err = coroutine.resume(co) + assert(st1 and not st2 and err == 43) + assert(X == 43 and Y.name == "pcall") +end + -- yielding across C boundaries From c23cc86c542449db47bdb21e9550203309bef045 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 7 Oct 2020 11:45:23 -0300 Subject: [PATCH 0625/1145] Details - After converting a generic GCObject to a specific type ('gco2*'), avoid using the original GCObject (to reduce aliasing). - Small corrections in comments in 'lopcodes.h' - Added tests about who calls __close metamethods --- lfunc.c | 2 +- lgc.c | 30 +++++++++++++++++++----------- lopcodes.h | 4 ++-- testes/locals.lua | 23 +++++++++++++++++++++++ 4 files changed, 45 insertions(+), 14 deletions(-) diff --git a/lfunc.c b/lfunc.c index 88d45328b1..c4360f0950 100644 --- a/lfunc.c +++ b/lfunc.c @@ -53,7 +53,7 @@ void luaF_initupvals (lua_State *L, LClosure *cl) { uv->v = &uv->u.value; /* make it closed */ setnilvalue(uv->v); cl->upvals[i] = uv; - luaC_objbarrier(L, cl, o); + luaC_objbarrier(L, cl, uv); } } diff --git a/lgc.c b/lgc.c index 3b8d0ed6d4..03326df3cb 100644 --- a/lgc.c +++ b/lgc.c @@ -301,7 +301,7 @@ static void reallymarkobject (global_State *g, GCObject *o) { if (upisopen(uv)) set2gray(uv); /* open upvalues are kept gray */ else - set2black(o); /* closed upvalues are visited here */ + set2black(uv); /* closed upvalues are visited here */ markvalue(g, uv->v); /* mark its content */ break; } @@ -309,7 +309,7 @@ static void reallymarkobject (global_State *g, GCObject *o) { Udata *u = gco2u(o); if (u->nuvalue == 0) { /* no user values? */ markobjectN(g, u->metatable); /* mark its metatable */ - set2black(o); /* nothing else to mark */ + set2black(u); /* nothing else to mark */ break; } /* else... */ @@ -770,12 +770,16 @@ static void freeobj (lua_State *L, GCObject *o) { case LUA_VUPVAL: freeupval(L, gco2upv(o)); break; - case LUA_VLCL: - luaM_freemem(L, o, sizeLclosure(gco2lcl(o)->nupvalues)); + case LUA_VLCL: { + LClosure *cl = gco2lcl(o); + luaM_freemem(L, cl, sizeLclosure(cl->nupvalues)); break; - case LUA_VCCL: - luaM_freemem(L, o, sizeCclosure(gco2ccl(o)->nupvalues)); + } + case LUA_VCCL: { + CClosure *cl = gco2ccl(o); + luaM_freemem(L, cl, sizeCclosure(cl->nupvalues)); break; + } case LUA_VTABLE: luaH_free(L, gco2t(o)); break; @@ -787,13 +791,17 @@ static void freeobj (lua_State *L, GCObject *o) { luaM_freemem(L, o, sizeudata(u->nuvalue, u->len)); break; } - case LUA_VSHRSTR: - luaS_remove(L, gco2ts(o)); /* remove it from hash table */ - luaM_freemem(L, o, sizelstring(gco2ts(o)->shrlen)); + case LUA_VSHRSTR: { + TString *ts = gco2ts(o); + luaS_remove(L, ts); /* remove it from hash table */ + luaM_freemem(L, ts, sizelstring(ts->shrlen)); break; - case LUA_VLNGSTR: - luaM_freemem(L, o, sizelstring(gco2ts(o)->u.lnglen)); + } + case LUA_VLNGSTR: { + TString *ts = gco2ts(o); + luaM_freemem(L, ts, sizelstring(ts->u.lnglen)); break; + } default: lua_assert(0); } } diff --git a/lopcodes.h b/lopcodes.h index 122e5d21f2..120cdd9438 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -261,7 +261,7 @@ OP_MMBINK,/* A B C k call C metamethod over R[A] and K[B] */ OP_UNM,/* A B R[A] := -R[B] */ OP_BNOT,/* A B R[A] := ~R[B] */ OP_NOT,/* A B R[A] := not R[B] */ -OP_LEN,/* A B R[A] := length of R[B] */ +OP_LEN,/* A B R[A] := #R[B] (length operator) */ OP_CONCAT,/* A B R[A] := R[A].. ... ..R[A + B - 1] */ @@ -297,7 +297,7 @@ OP_TFORPREP,/* A Bx create upvalue for R[A + 3]; pc+=Bx */ OP_TFORCALL,/* A C R[A+4], ... ,R[A+3+C] := R[A](R[A+1], R[A+2]); */ OP_TFORLOOP,/* A Bx if R[A+2] ~= nil then { R[A]=R[A+2]; pc -= Bx } */ -OP_SETLIST,/* A B C k R[A][(C-1)*FPF+i] := R[A+i], 1 <= i <= B */ +OP_SETLIST,/* A B C k R[A][C+i] := R[A+i], 1 <= i <= B */ OP_CLOSURE,/* A Bx R[A] := closure(KPROTO[Bx]) */ diff --git a/testes/locals.lua b/testes/locals.lua index f5e962447c..df44b86f2f 100644 --- a/testes/locals.lua +++ b/testes/locals.lua @@ -246,6 +246,11 @@ do X = false foo = function (x) + local _ = func2close(function () + -- without errors, enclosing function should be still active when + -- __close is called + assert(debug.getinfo(2).name == "foo") + end) local _ = closescope local y = 15 return y @@ -343,6 +348,18 @@ local function endwarn () end +-- errors inside __close can generate a warning instead of an +-- error. This new 'assert' force them to appear. +local function assert(cond, msg) + if not cond then + local line = debug.getinfo(2).currentline or "?" + msg = string.format("assertion failed! line %d (%s)\n", line, msg or "") + io.stderr:write(msg) + os.exit(1) + end +end + + local function checkwarn (msg) if T then assert(string.find(_WARN, msg)) @@ -406,11 +423,15 @@ do print("testing errors in __close") local x = func2close(function (self, msg) + -- after error, 'foo' was discarded, so caller now + -- must be 'pcall' + assert(debug.getinfo(2).name == "pcall") assert(msg == 4) end) local x1 = func2close(function (self, msg) + assert(debug.getinfo(2).name == "pcall") checkwarn("@y") assert(msg == 4) error("@x1") @@ -420,6 +441,7 @@ do print("testing errors in __close") local y = func2close(function (self, msg) + assert(debug.getinfo(2).name == "pcall") assert(msg == 4) -- error in body checkwarn("@z") error("@y") @@ -428,6 +450,7 @@ do print("testing errors in __close") local first = true local z = func2close(function (self, msg) + assert(debug.getinfo(2).name == "pcall") -- 'z' close is called once assert(first and msg == 4) first = false From 9a89fb1c9dfeda4640780111f9e9437f08cfad88 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 12 Oct 2020 10:02:37 -0300 Subject: [PATCH 0626/1145] Hash always use all characters in a long string Hashes for long strings are computed only when they are used as keys in a table, not a too common case. And, in that case, it is to easy to force collisions changing only the characters which are not part of the hash. --- lstate.c | 2 +- lstring.c | 20 ++++---------------- lstring.h | 3 +-- ltests.c | 1 - 4 files changed, 6 insertions(+), 20 deletions(-) diff --git a/lstate.c b/lstate.c index 76df6a20d9..4227429247 100644 --- a/lstate.c +++ b/lstate.c @@ -76,7 +76,7 @@ static unsigned int luai_makeseed (lua_State *L) { addbuff(buff, p, &h); /* local variable */ addbuff(buff, p, &lua_newstate); /* public function */ lua_assert(p == sizeof(buff)); - return luaS_hash(buff, p, h, 1); + return luaS_hash(buff, p, h); } #endif diff --git a/lstring.c b/lstring.c index 6f1574731b..138871c70d 100644 --- a/lstring.c +++ b/lstring.c @@ -22,16 +22,6 @@ #include "lstring.h" -/* -** Lua will use at most ~(2^LUAI_HASHLIMIT) bytes from a long string to -** compute its hash -*/ -#if !defined(LUAI_HASHLIMIT) -#define LUAI_HASHLIMIT 5 -#endif - - - /* ** Maximum size for string table. */ @@ -50,10 +40,9 @@ int luaS_eqlngstr (TString *a, TString *b) { } -unsigned int luaS_hash (const char *str, size_t l, unsigned int seed, - size_t step) { +unsigned int luaS_hash (const char *str, size_t l, unsigned int seed) { unsigned int h = seed ^ cast_uint(l); - for (; l >= step; l -= step) + for (; l > 0; l--) h ^= ((h<<5) + (h>>2) + cast_byte(str[l - 1])); return h; } @@ -63,8 +52,7 @@ unsigned int luaS_hashlongstr (TString *ts) { lua_assert(ts->tt == LUA_VLNGSTR); if (ts->extra == 0) { /* no hash? */ size_t len = ts->u.lnglen; - size_t step = (len >> LUAI_HASHLIMIT) + 1; - ts->hash = luaS_hash(getstr(ts), len, ts->hash, step); + ts->hash = luaS_hash(getstr(ts), len, ts->hash); ts->extra = 1; /* now it has its hash */ } return ts->hash; @@ -201,7 +189,7 @@ static TString *internshrstr (lua_State *L, const char *str, size_t l) { TString *ts; global_State *g = G(L); stringtable *tb = &g->strt; - unsigned int h = luaS_hash(str, l, g->seed, 1); + unsigned int h = luaS_hash(str, l, g->seed); TString **list = &tb->hash[lmod(h, tb->size)]; lua_assert(str != NULL); /* otherwise 'memcmp'/'memcpy' are undefined */ for (ts = *list; ts != NULL; ts = ts->u.hnext) { diff --git a/lstring.h b/lstring.h index a413a9d3a0..450c2390d1 100644 --- a/lstring.h +++ b/lstring.h @@ -41,8 +41,7 @@ #define eqshrstr(a,b) check_exp((a)->tt == LUA_VSHRSTR, (a) == (b)) -LUAI_FUNC unsigned int luaS_hash (const char *str, size_t l, - unsigned int seed, size_t step); +LUAI_FUNC unsigned int luaS_hash (const char *str, size_t l, unsigned int seed); LUAI_FUNC unsigned int luaS_hashlongstr (TString *ts); LUAI_FUNC int luaS_eqlngstr (TString *a, TString *b); LUAI_FUNC void luaS_resize (lua_State *L, int newsize); diff --git a/ltests.c b/ltests.c index 9945615993..7e3a389a73 100644 --- a/ltests.c +++ b/ltests.c @@ -523,7 +523,6 @@ static lu_mem checkgraylist (global_State *g, GCObject *o) { ((void)g); /* better to keep it available if we need to print an object */ while (o) { lua_assert(!!isgray(o) ^ (getage(o) == G_TOUCHED2)); - //lua_assert(isgray(o) || getage(o) == G_TOUCHED2); lua_assert(!testbit(o->marked, TESTBIT)); if (keepinvariant(g)) l_setbit(o->marked, TESTBIT); /* mark that object is in a gray list */ From 30528049f1d11ea2854a6431e8e8524f83206559 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 12 Oct 2020 14:51:28 -0300 Subject: [PATCH 0627/1145] 'lua_upvalueid' returns NULL on invalid upvalue index --- .gitignore | 3 +++ lapi.c | 19 +++++++++++++------ ldblib.c | 24 ++++++++++++++++-------- testes/closure.lua | 2 +- 4 files changed, 33 insertions(+), 15 deletions(-) diff --git a/.gitignore b/.gitignore index 735661eaaf..ae2899e088 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,6 @@ testes/time.txt testes/time-debug.txt testes/libs/all + +temp +lua diff --git a/lapi.c b/lapi.c index 9048245f36..c824da27cb 100644 --- a/lapi.c +++ b/lapi.c @@ -1383,13 +1383,16 @@ LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) { static UpVal **getupvalref (lua_State *L, int fidx, int n, LClosure **pf) { + static const UpVal *const nullup = NULL; LClosure *f; TValue *fi = index2value(L, fidx); api_check(L, ttisLclosure(fi), "Lua function expected"); f = clLvalue(fi); - api_check(L, (1 <= n && n <= f->p->sizeupvalues), "invalid upvalue index"); if (pf) *pf = f; - return &f->upvals[n - 1]; /* get its upvalue pointer */ + if (1 <= n && n <= f->p->sizeupvalues) + return &f->upvals[n - 1]; /* get its upvalue pointer */ + else + return (UpVal**)&nullup; } @@ -1401,11 +1404,14 @@ LUA_API void *lua_upvalueid (lua_State *L, int fidx, int n) { } case LUA_VCCL: { /* C closure */ CClosure *f = clCvalue(fi); - api_check(L, 1 <= n && n <= f->nupvalues, "invalid upvalue index"); - return &f->upvalue[n - 1]; - } + if (1 <= n && n <= f->nupvalues) + return &f->upvalue[n - 1]; + /* else */ + } /* FALLTHROUGH */ + case LUA_VLCF: + return NULL; /* light C functions have no upvalues */ default: { - api_check(L, 0, "closure expected"); + api_check(L, 0, "function expected"); return NULL; } } @@ -1417,6 +1423,7 @@ LUA_API void lua_upvaluejoin (lua_State *L, int fidx1, int n1, LClosure *f1; UpVal **up1 = getupvalref(L, fidx1, n1, &f1); UpVal **up2 = getupvalref(L, fidx2, n2, NULL); + api_check(L, *up1 != NULL && *up2 != NULL, "invalid upvalue index"); *up1 = *up2; luaC_objbarrier(L, f1, *up1); } diff --git a/ldblib.c b/ldblib.c index 26058b5082..5a326adedb 100644 --- a/ldblib.c +++ b/ldblib.c @@ -281,25 +281,33 @@ static int db_setupvalue (lua_State *L) { ** Check whether a given upvalue from a given closure exists and ** returns its index */ -static int checkupval (lua_State *L, int argf, int argnup) { +static void *checkupval (lua_State *L, int argf, int argnup, int *pnup) { + void *id; int nup = (int)luaL_checkinteger(L, argnup); /* upvalue index */ luaL_checktype(L, argf, LUA_TFUNCTION); /* closure */ - luaL_argcheck(L, (lua_getupvalue(L, argf, nup) != NULL), argnup, - "invalid upvalue index"); - return nup; + id = lua_upvalueid(L, argf, nup); + if (pnup) { + luaL_argcheck(L, id != NULL, argnup, "invalid upvalue index"); + *pnup = nup; + } + return id; } static int db_upvalueid (lua_State *L) { - int n = checkupval(L, 1, 2); - lua_pushlightuserdata(L, lua_upvalueid(L, 1, n)); + void *id = checkupval(L, 1, 2, NULL); + if (id != NULL) + lua_pushlightuserdata(L, id); + else + luaL_pushfail(L); return 1; } static int db_upvaluejoin (lua_State *L) { - int n1 = checkupval(L, 1, 2); - int n2 = checkupval(L, 3, 4); + int n1, n2; + checkupval(L, 1, 2, &n1); + checkupval(L, 3, 4, &n2); luaL_argcheck(L, !lua_iscfunction(L, 1), 1, "Lua function expected"); luaL_argcheck(L, !lua_iscfunction(L, 3), 3, "Lua function expected"); lua_upvaluejoin(L, 1, n1, 3, n2); diff --git a/testes/closure.lua b/testes/closure.lua index cdeaebaa02..c245367777 100644 --- a/testes/closure.lua +++ b/testes/closure.lua @@ -242,7 +242,7 @@ end assert(debug.upvalueid(foo1, 1)) assert(debug.upvalueid(foo1, 2)) -assert(not pcall(debug.upvalueid, foo1, 3)) +assert(not debug.upvalueid(foo1, 3)) assert(debug.upvalueid(foo1, 1) == debug.upvalueid(foo2, 2)) assert(debug.upvalueid(foo1, 2) == debug.upvalueid(foo2, 1)) assert(debug.upvalueid(foo3, 1)) From 849b2ecbd28793408d21ebd734525ab56ae5ca1e Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 12 Oct 2020 14:52:39 -0300 Subject: [PATCH 0628/1145] New release number (5.4.2) --- lua.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua.h b/lua.h index 08c6a64a10..c9d64d7f21 100644 --- a/lua.h +++ b/lua.h @@ -18,7 +18,7 @@ #define LUA_VERSION_MAJOR "5" #define LUA_VERSION_MINOR "4" -#define LUA_VERSION_RELEASE "1" +#define LUA_VERSION_RELEASE "2" #define LUA_VERSION_NUM 504 #define LUA_VERSION_RELEASE_NUM (LUA_VERSION_NUM * 100 + 0) From 52c86797608f1bf927be5bab1e9b97b7d35bdf2c Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 14 Oct 2020 15:46:58 -0300 Subject: [PATCH 0629/1145] Fixed bug of keys removed from tables vs 'next' Fixed the bug that a key removed from a table might not be found again by 'next'. (This is needed to allow keys to be removed during a traversal.) This bug was introduced in commit 73ec04fc. --- lgc.c | 17 ++++++++--------- lobject.h | 18 ++++++++++-------- ltable.c | 27 +++++++++++++++++---------- testes/nextvar.lua | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 67 insertions(+), 27 deletions(-) diff --git a/lgc.c b/lgc.c index 03326df3cb..5dba56fc3f 100644 --- a/lgc.c +++ b/lgc.c @@ -161,18 +161,17 @@ static void linkgclist_ (GCObject *o, GCObject **pnext, GCObject **list) { /* -** Clear keys for empty entries in tables. If entry is empty -** and its key is not marked, mark its entry as dead. This allows the -** collection of the key, but keeps its entry in the table (its removal -** could break a chain). The main feature of a dead key is that it must -** be different from any other value, to do not disturb searches. -** Other places never manipulate dead keys, because its associated empty -** value is enough to signal that the entry is logically empty. +** Clear keys for empty entries in tables. If entry is empty, mark its +** entry as dead. This allows the collection of the key, but keeps its +** entry in the table: its removal could break a chain and could break +** a table traversal. Other places never manipulate dead keys, because +** its associated empty value is enough to signal that the entry is +** logically empty. */ static void clearkey (Node *n) { lua_assert(isempty(gval(n))); - if (keyiswhite(n)) - setdeadkey(n); /* unused and unmarked key; remove it */ + if (keyiscollectable(n)) + setdeadkey(n); /* unused key; remove it */ } diff --git a/lobject.h b/lobject.h index a9d45785ec..1cc8e757bf 100644 --- a/lobject.h +++ b/lobject.h @@ -21,10 +21,12 @@ */ #define LUA_TUPVAL LUA_NUMTYPES /* upvalues */ #define LUA_TPROTO (LUA_NUMTYPES+1) /* function prototypes */ +#define LUA_TDEADKEY (LUA_NUMTYPES+2) /* removed keys in tables */ + /* -** number of all possible types (including LUA_TNONE) +** number of all possible types (including LUA_TNONE but excluding DEADKEY) */ #define LUA_TOTALTYPES (LUA_TPROTO + 2) @@ -555,7 +557,7 @@ typedef struct Proto { /* ** {================================================================== -** Closures +** Functions ** =================================================================== */ @@ -743,13 +745,13 @@ typedef struct Table { /* -** Use a "nil table" to mark dead keys in a table. Those keys serve -** to keep space for removed entries, which may still be part of -** chains. Note that the 'keytt' does not have the BIT_ISCOLLECTABLE -** set, so these values are considered not collectable and are different -** from any valid value. +** Dead keys in tables have the tag DEADKEY but keep their original +** gcvalue. This distinguishes them from regular keys but allows them to +** be found when searched in a special way. ('next' needs that to find +** keys removed from a table during a traversal.) */ -#define setdeadkey(n) (keytt(n) = LUA_TTABLE, gckey(n) = NULL) +#define setdeadkey(node) (keytt(node) = LUA_TDEADKEY) +#define keyisdead(node) (keytt(node) == LUA_TDEADKEY) /* }================================================================== */ diff --git a/ltable.c b/ltable.c index 5a0d066faa..38bee1dcf4 100644 --- a/ltable.c +++ b/ltable.c @@ -172,11 +172,17 @@ static Node *mainpositionTV (const Table *t, const TValue *key) { ** be equal to floats. It is assumed that 'eqshrstr' is simply ** pointer equality, so that short strings are handled in the ** default case. -*/ -static int equalkey (const TValue *k1, const Node *n2) { - if (rawtt(k1) != keytt(n2)) /* not the same variants? */ +** A true 'deadok' means to accept dead keys as equal to their original +** values, which can only happen if the original key was collectable. +** All dead values are compared in the default case, by pointer +** identity. (Note that dead long strings are also compared by +** identity). +*/ +static int equalkey (const TValue *k1, const Node *n2, int deadok) { + if ((rawtt(k1) != keytt(n2)) && /* not the same variants? */ + !(deadok && keyisdead(n2) && iscollectable(k1))) return 0; /* cannot be same key */ - switch (ttypetag(k1)) { + switch (keytt(n2)) { case LUA_VNIL: case LUA_VFALSE: case LUA_VTRUE: return 1; case LUA_VNUMINT: @@ -187,7 +193,7 @@ static int equalkey (const TValue *k1, const Node *n2) { return pvalue(k1) == pvalueraw(keyval(n2)); case LUA_VLCF: return fvalue(k1) == fvalueraw(keyval(n2)); - case LUA_VLNGSTR: + case ctb(LUA_VLNGSTR): return luaS_eqlngstr(tsvalue(k1), keystrval(n2)); default: return gcvalue(k1) == gcvalueraw(keyval(n2)); @@ -251,11 +257,12 @@ static unsigned int setlimittosize (Table *t) { /* ** "Generic" get version. (Not that generic: not valid for integers, ** which may be in array part, nor for floats with integral values.) +** See explanation about 'deadok' in function 'equalkey'. */ -static const TValue *getgeneric (Table *t, const TValue *key) { +static const TValue *getgeneric (Table *t, const TValue *key, int deadok) { Node *n = mainpositionTV(t, key); for (;;) { /* check whether 'key' is somewhere in the chain */ - if (equalkey(key, n)) + if (equalkey(key, n, deadok)) return gval(n); /* that's it */ else { int nx = gnext(n); @@ -292,7 +299,7 @@ static unsigned int findindex (lua_State *L, Table *t, TValue *key, if (i - 1u < asize) /* is 'key' inside array part? */ return i; /* yes; that's the index */ else { - const TValue *n = getgeneric(t, key); + const TValue *n = getgeneric(t, key, 1); if (unlikely(isabstkey(n))) luaG_runerror(L, "invalid key to 'next'"); /* key not found */ i = cast_int(nodefromval(n) - gnode(t, 0)); /* key index in hash table */ @@ -730,7 +737,7 @@ const TValue *luaH_getstr (Table *t, TString *key) { else { /* for long strings, use generic case */ TValue ko; setsvalue(cast(lua_State *, NULL), &ko, key); - return getgeneric(t, &ko); + return getgeneric(t, &ko, 0); } } @@ -750,7 +757,7 @@ const TValue *luaH_get (Table *t, const TValue *key) { /* else... */ } /* FALLTHROUGH */ default: - return getgeneric(t, key); + return getgeneric(t, key, 0); } } diff --git a/testes/nextvar.lua b/testes/nextvar.lua index a16d557b67..29cb05d573 100644 --- a/testes/nextvar.lua +++ b/testes/nextvar.lua @@ -359,6 +359,38 @@ end assert(n == 5) +do + print("testing next x GC of deleted keys") + -- bug in 5.4.1 + local co = coroutine.wrap(function (t) + for k, v in pairs(t) do + local k1 = next(t) -- all previous keys were deleted + assert(k == k1) -- current key is the first in the table + t[k] = nil + local expected = (type(k) == "table" and k[1] or + type(k) == "function" and k() or + string.sub(k, 1, 1)) + assert(expected == v) + coroutine.yield(v) + end + end) + local t = {} + t[{1}] = 1 -- add several unanchored, collectable keys + t[{2}] = 2 + t[string.rep("a", 50)] = "a" -- long string + t[string.rep("b", 50)] = "b" + t[{3}] = 3 + t[string.rep("c", 10)] = "c" -- short string + t[function () return 10 end] = 10 + local count = 7 + while co(t) do + collectgarbage("collect") -- collect dead keys + count = count - 1 + end + assert(count == 0 and next(t) == nil) -- traversed the whole table +end + + local function test (a) assert(not pcall(table.insert, a, 2, 20)); table.insert(a, 10); table.insert(a, 2, 20); From f07de225762ee0f2d5b411b948b3c6e28e0695d3 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 19 Oct 2020 15:43:59 -0300 Subject: [PATCH 0630/1145] Fixed compiler option -DHARDSTACKTESTS to commit 5aa36e8 --- llimits.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/llimits.h b/llimits.h index d6866d7c1f..a76c13ed65 100644 --- a/llimits.h +++ b/llimits.h @@ -355,7 +355,7 @@ typedef l_uint32 Instruction; #else /* realloc stack keeping its size */ #define condmovestack(L,pre,pos) \ - { int sz_ = (L)->stacksize; pre; luaD_reallocstack((L), sz_, 0); pos; } + { int sz_ = stacksize(L); pre; luaD_reallocstack((L), sz_, 0); pos; } #endif #if !defined(HARDMEMTESTS) From e4a38eb0e828e9589c391171e2e1904a3b9698e7 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 19 Oct 2020 15:55:25 -0300 Subject: [PATCH 0631/1145] Fixed wrong trace of vararg functions Trace of vararg functions was skipping an instruction when returning from a call. (Bug introduced by commit 5d8ce05b3.) --- lvm.c | 45 +++++++++++++++++++++++++-------------------- testes/db.lua | 10 ++++++++++ 2 files changed, 35 insertions(+), 20 deletions(-) diff --git a/lvm.c b/lvm.c index 72d3e69520..aa3b22bf73 100644 --- a/lvm.c +++ b/lvm.c @@ -1092,15 +1092,11 @@ void luaV_finishOp (lua_State *L) { #define ProtectNT(exp) (savepc(L), (exp), updatetrap(ci)) /* -** Protect code that will finish the loop (returns) or can only raise -** errors. (That is, it will not return to the interpreter main loop -** after changing the stack or hooks.) +** Protect code that can only raise errors. (That is, it cannnot change +** the stack or hooks.) */ #define halfProtect(exp) (savestate(L,ci), (exp)) -/* idem, but without changing the stack */ -#define halfProtectNT(exp) (savepc(L), (exp)) - /* 'c' is the limit of live values in the stack */ #define checkGC(L,c) \ { luaC_condGC(L, (savepc(L), L->top = (c)), \ @@ -1132,17 +1128,20 @@ void luaV_execute (lua_State *L, CallInfo *ci) { #if LUA_USE_JUMPTABLE #include "ljumptab.h" #endif - execute: + startfunc: trap = L->hookmask; + returning: /* trap already set */ cl = clLvalue(s2v(ci->func)); k = cl->p->k; pc = ci->u.l.savedpc; if (trap) { - if (cl->p->is_vararg) - trap = 0; /* hooks will start after VARARGPREP instruction */ - else if (pc == cl->p->code) /* first instruction (not resuming)? */ - luaD_hookcall(L, ci); - ci->u.l.trap = 1; /* there may be other hooks */ + if (pc == cl->p->code) { /* first instruction (not resuming)? */ + if (cl->p->is_vararg) + trap = 0; /* hooks will start after VARARGPREP instruction */ + else /* check 'call' hook */ + luaD_hookcall(L, ci); + } + ci->u.l.trap = 1; /* assume trap is on, for now */ } base = ci->func + 1; /* main loop of interpreter */ @@ -1615,10 +1614,10 @@ void luaV_execute (lua_State *L, CallInfo *ci) { savepc(L); /* in case of errors */ if ((newci = luaD_precall(L, ra, nresults)) == NULL) updatetrap(ci); /* C call; nothing else to be done */ - else { /* Lua call: run function in this same invocation */ + else { /* Lua call: run function in this same C frame */ ci = newci; ci->callstatus = 0; /* call re-uses 'luaV_execute' */ - goto execute; + goto startfunc; } vmbreak; } @@ -1631,7 +1630,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { L->top = ra + b; else /* previous instruction set top */ b = cast_int(L->top - ra); - savepc(ci); /* some calls here can raise errors */ + savepc(ci); /* several calls here can raise errors */ if (TESTARG_k(i)) { /* close upvalues from current call; the compiler ensures that there are no to-be-closed variables here, so this @@ -1650,11 +1649,12 @@ void luaV_execute (lua_State *L, CallInfo *ci) { updatestack(ci); /* stack may have been relocated */ ci->func -= delta; /* restore 'func' (if vararg) */ luaD_poscall(L, ci, cast_int(L->top - ra)); /* finish caller */ + updatetrap(ci); /* 'luaD_poscall' can change hooks */ goto ret; /* caller returns after the tail call */ } ci->func -= delta; /* restore 'func' (if vararg) */ luaD_pretailcall(L, ci, ra, b); /* prepare call frame */ - goto execute; /* execute the callee */ + goto startfunc; /* execute the callee */ } vmcase(OP_RETURN) { int n = GETARG_B(i) - 1; /* number of results */ @@ -1673,12 +1673,15 @@ void luaV_execute (lua_State *L, CallInfo *ci) { ci->func -= ci->u.l.nextraargs + nparams1; L->top = ra + n; /* set call for 'luaD_poscall' */ luaD_poscall(L, ci, n); + updatetrap(ci); /* 'luaD_poscall' can change hooks */ goto ret; } vmcase(OP_RETURN0) { if (L->hookmask) { L->top = ra; - halfProtectNT(luaD_poscall(L, ci, 0)); /* no hurry... */ + savepc(ci); + luaD_poscall(L, ci, 0); /* no hurry... */ + trap = 1; } else { /* do the 'poscall' here */ int nres = ci->nresults; @@ -1692,7 +1695,9 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmcase(OP_RETURN1) { if (L->hookmask) { L->top = ra + 1; - halfProtectNT(luaD_poscall(L, ci, 1)); /* no hurry... */ + savepc(ci); + luaD_poscall(L, ci, 1); /* no hurry... */ + trap = 1; } else { /* do the 'poscall' here */ int nres = ci->nresults; @@ -1706,12 +1711,12 @@ void luaV_execute (lua_State *L, CallInfo *ci) { setnilvalue(s2v(L->top++)); } } - ret: + ret: /* return from a Lua function */ if (ci->callstatus & CIST_FRESH) return; /* end this frame */ else { ci = ci->previous; - goto execute; /* continue running caller in this frame */ + goto returning; /* continue running caller in this frame */ } } vmcase(OP_FORLOOP) { diff --git a/testes/db.lua b/testes/db.lua index 5377f6ec0e..fdb0da4a1c 100644 --- a/testes/db.lua +++ b/testes/db.lua @@ -119,6 +119,16 @@ else end ]], {2,3,4,7}) +test([[ +local function foo() +end +foo() +A = 1 +A = 2 +A = 3 +]], {2, 3, 2, 4, 5, 6}) + + test([[-- if nil then a=1 From d742a193e57029d973aff0a5eb04d8ddd03fa0ff Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 22 Oct 2020 15:54:46 -0300 Subject: [PATCH 0632/1145] Comments --- llex.c | 25 +++++++++++++------------ lparser.c | 8 ++++---- ltable.c | 27 +++++++++++++++++---------- 3 files changed, 34 insertions(+), 26 deletions(-) diff --git a/llex.c b/llex.c index 3d6b2b97ac..4b8dec9985 100644 --- a/llex.c +++ b/llex.c @@ -254,9 +254,10 @@ static int read_numeral (LexState *ls, SemInfo *seminfo) { /* -** reads a sequence '[=*[' or ']=*]', leaving the last bracket. -** If sequence is well formed, return its number of '='s + 2; otherwise, -** return 1 if there is no '='s or 0 otherwise (an unfinished '[==...'). +** read a sequence '[=*[' or ']=*]', leaving the last bracket. If +** sequence is well formed, return its number of '='s + 2; otherwise, +** return 1 if it is a single bracket (no '='s and no 2nd bracket); +** otherwise (an unfinished '[==...') return 0. */ static size_t skip_sep (LexState *ls) { size_t count = 0; @@ -481,34 +482,34 @@ static int llex (LexState *ls, SemInfo *seminfo) { } case '=': { next(ls); - if (check_next1(ls, '=')) return TK_EQ; + if (check_next1(ls, '=')) return TK_EQ; /* '==' */ else return '='; } case '<': { next(ls); - if (check_next1(ls, '=')) return TK_LE; - else if (check_next1(ls, '<')) return TK_SHL; + if (check_next1(ls, '=')) return TK_LE; /* '<=' */ + else if (check_next1(ls, '<')) return TK_SHL; /* '<<' */ else return '<'; } case '>': { next(ls); - if (check_next1(ls, '=')) return TK_GE; - else if (check_next1(ls, '>')) return TK_SHR; + if (check_next1(ls, '=')) return TK_GE; /* '>=' */ + else if (check_next1(ls, '>')) return TK_SHR; /* '>>' */ else return '>'; } case '/': { next(ls); - if (check_next1(ls, '/')) return TK_IDIV; + if (check_next1(ls, '/')) return TK_IDIV; /* '//' */ else return '/'; } case '~': { next(ls); - if (check_next1(ls, '=')) return TK_NE; + if (check_next1(ls, '=')) return TK_NE; /* '~=' */ else return '~'; } case ':': { next(ls); - if (check_next1(ls, ':')) return TK_DBCOLON; + if (check_next1(ls, ':')) return TK_DBCOLON; /* '::' */ else return ':'; } case '"': case '\'': { /* short literal strings */ @@ -547,7 +548,7 @@ static int llex (LexState *ls, SemInfo *seminfo) { return TK_NAME; } } - else { /* single-char tokens (+ - / ...) */ + else { /* single-char tokens ('+', '*', '%', '{', '}', ...) */ int c = ls->current; next(ls); return c; diff --git a/lparser.c b/lparser.c index bcdcfb6d7d..fb7a1264eb 100644 --- a/lparser.c +++ b/lparser.c @@ -945,7 +945,7 @@ static void setvararg (FuncState *fs, int nparams) { static void parlist (LexState *ls) { - /* parlist -> [ param { ',' param } ] */ + /* parlist -> [ {NAME ','} (NAME | '...') ] */ FuncState *fs = ls->fs; Proto *f = fs->f; int nparams = 0; @@ -953,12 +953,12 @@ static void parlist (LexState *ls) { if (ls->t.token != ')') { /* is 'parlist' not empty? */ do { switch (ls->t.token) { - case TK_NAME: { /* param -> NAME */ + case TK_NAME: { new_localvar(ls, str_checkname(ls)); nparams++; break; } - case TK_DOTS: { /* param -> '...' */ + case TK_DOTS: { luaX_next(ls); isvararg = 1; break; @@ -1752,7 +1752,7 @@ static void checktoclose (LexState *ls, int level) { static void localstat (LexState *ls) { - /* stat -> LOCAL ATTRIB NAME {',' ATTRIB NAME} ['=' explist] */ + /* stat -> LOCAL NAME ATTRIB { ',' NAME ATTRIB } ['=' explist] */ FuncState *fs = ls->fs; int toclose = -1; /* index of to-be-closed variable (if any) */ Vardesc *var; /* last variable */ diff --git a/ltable.c b/ltable.c index 38bee1dcf4..7e7cbed97c 100644 --- a/ltable.c +++ b/ltable.c @@ -166,17 +166,24 @@ static Node *mainpositionTV (const Table *t, const TValue *key) { /* -** Check whether key 'k1' is equal to the key in node 'n2'. -** This equality is raw, so there are no metamethods. Floats -** with integer values have been normalized, so integers cannot -** be equal to floats. It is assumed that 'eqshrstr' is simply -** pointer equality, so that short strings are handled in the -** default case. +** Check whether key 'k1' is equal to the key in node 'n2'. This +** equality is raw, so there are no metamethods. Floats with integer +** values have been normalized, so integers cannot be equal to +** floats. It is assumed that 'eqshrstr' is simply pointer equality, so +** that short strings are handled in the default case. ** A true 'deadok' means to accept dead keys as equal to their original -** values, which can only happen if the original key was collectable. -** All dead values are compared in the default case, by pointer -** identity. (Note that dead long strings are also compared by -** identity). +** values. All dead keys are compared in the default case, by pointer +** identity. (Only collectable objects can produce dead keys.) Note that +** dead long strings are also compared by identity. +** Once a key is dead, its corresponding value may be collected, and +** then another value can be created with the same address. If this +** other value is given to 'next', 'equalkey' will signal a false +** positive. In a regular traversal, this situation should never happen, +** as all keys given to 'next' came from the table itself, and therefore +** could not have been collected. Outside a regular traversal, we +** have garbage in, garbage out. What is relevant is that this false +** positive does not break anything. (In particular, 'next' will return +** some other valid item on the table or nil.) */ static int equalkey (const TValue *k1, const Node *n2, int deadok) { if ((rawtt(k1) != keytt(n2)) && /* not the same variants? */ From 69b71a69197de0cb6f7f58f5d7c55d9a9a6e529d Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 26 Oct 2020 11:15:51 -0300 Subject: [PATCH 0633/1145] _PROMPT can have non-string values 'get_prompt' uses 'luaL_tolstring' to convert _PROMPT or _PROMPT2 value to a string. That conversion may invoke a '__tostring' metamethod. --- lua.c | 16 ++++++++++------ testes/main.lua | 27 +++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 6 deletions(-) diff --git a/lua.c b/lua.c index 454ce12f36..b5b884b6d0 100644 --- a/lua.c +++ b/lua.c @@ -416,14 +416,18 @@ static int handle_luainit (lua_State *L) { /* -** Returns the string to be used as a prompt by the interpreter. +** Return the string to be used as a prompt by the interpreter. Leave +** the string (or nil, if using the default value) on the stack, to keep +** it anchored. */ static const char *get_prompt (lua_State *L, int firstline) { - const char *p; - lua_getglobal(L, firstline ? "_PROMPT" : "_PROMPT2"); - p = lua_tostring(L, -1); - if (p == NULL) p = (firstline ? LUA_PROMPT : LUA_PROMPT2); - return p; + if (lua_getglobal(L, firstline ? "_PROMPT" : "_PROMPT2") == LUA_TNIL) + return (firstline ? LUA_PROMPT : LUA_PROMPT2); /* use the default */ + else { /* apply 'tostring' over the value */ + const char *p = luaL_tolstring(L, -1, NULL); + lua_remove(L, -2); /* remove original value */ + return p; + } } /* mark in error messages for incomplete statements */ diff --git a/testes/main.lua b/testes/main.lua index d2d602de5d..56959abd96 100644 --- a/testes/main.lua +++ b/testes/main.lua @@ -287,6 +287,33 @@ RUN([[lua "-e_PROMPT='%s'" -i < %s > %s]], prompt, prog, out) local t = getoutput() assert(string.find(t, prompt .. ".*" .. prompt .. ".*" .. prompt)) +-- using the prompt default +prepfile[[ -- +a = 2 +]] +RUN([[lua -i < %s > %s]], prog, out) +local t = getoutput() +prompt = "> " -- the default +assert(string.find(t, prompt .. ".*" .. prompt .. ".*" .. prompt)) + + +-- non-string prompt +prompt = + "local C = 0;\z + _PROMPT=setmetatable({},{__tostring = function () \z + C = C + 1; return C end})" +prepfile[[ -- +a = 2 +]] +RUN([[lua -e "%s" -i < %s > %s]], prompt, prog, out) +local t = getoutput() +assert(string.find(t, [[ +1 -- +2a = 2 +3 +]], 1, true)) + + -- test for error objects prepfile[[ debug = require "debug" From 94cbe4651156a84dd9114d7daaa61acd050adbe0 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 30 Oct 2020 10:18:54 -0300 Subject: [PATCH 0634/1145] Details - small corrections in the manual - ldo.c: 'docall' -> 'ccall' ('docall' already used in 'lua.c') - comments --- lcode.c | 14 +++++++++----- ldo.c | 16 ++++++++-------- lparser.h | 5 +++-- manual/manual.of | 13 ++++++++++--- 4 files changed, 30 insertions(+), 18 deletions(-) diff --git a/lcode.c b/lcode.c index 6f241c9476..14d41f1a7e 100644 --- a/lcode.c +++ b/lcode.c @@ -753,7 +753,7 @@ void luaK_setoneret (FuncState *fs, expdesc *e) { /* -** Ensure that expression 'e' is not a variable (nor a constant). +** Ensure that expression 'e' is not a variable (nor a ). ** (Expression still may have jump lists.) */ void luaK_dischargevars (FuncState *fs, expdesc *e) { @@ -805,8 +805,8 @@ void luaK_dischargevars (FuncState *fs, expdesc *e) { /* -** Ensures expression value is in register 'reg' (and therefore -** 'e' will become a non-relocatable expression). +** Ensure expression value is in register 'reg', making 'e' a +** non-relocatable expression. ** (Expression still may have jump lists.) */ static void discharge2reg (FuncState *fs, expdesc *e, int reg) { @@ -860,7 +860,8 @@ static void discharge2reg (FuncState *fs, expdesc *e, int reg) { /* -** Ensures expression value is in any register. +** Ensure expression value is in a register, making 'e' a +** non-relocatable expression. ** (Expression still may have jump lists.) */ static void discharge2anyreg (FuncState *fs, expdesc *e) { @@ -946,8 +947,11 @@ int luaK_exp2anyreg (FuncState *fs, expdesc *e) { exp2reg(fs, e, e->u.info); /* put final result in it */ return e->u.info; } + /* else expression has jumps and cannot change its register + to hold the jump values, because it is a local variable. + Go through to the default case. */ } - luaK_exp2nextreg(fs, e); /* otherwise, use next available register */ + luaK_exp2nextreg(fs, e); /* default: use next available register */ return e->u.info; } diff --git a/ldo.c b/ldo.c index 5729b19024..a60972b209 100644 --- a/ldo.c +++ b/ldo.c @@ -534,11 +534,11 @@ CallInfo *luaD_precall (lua_State *L, StkId func, int nresults) { /* -** Call a function (C or Lua). 'inc' can be 1 (increment number -** of recursive invocations in the C stack) or nyci (the same plus -** increment number of non-yieldable calls). +** Call a function (C or Lua) through C. 'inc' can be 1 (increment +** number of recursive invocations in the C stack) or nyci (the same +** plus increment number of non-yieldable calls). */ -static void docall (lua_State *L, StkId func, int nResults, int inc) { +static void ccall (lua_State *L, StkId func, int nResults, int inc) { CallInfo *ci; L->nCcalls += inc; if (unlikely(getCcalls(L) >= LUAI_MAXCCALLS)) @@ -552,10 +552,10 @@ static void docall (lua_State *L, StkId func, int nResults, int inc) { /* -** External interface for 'docall' +** External interface for 'ccall' */ void luaD_call (lua_State *L, StkId func, int nResults) { - return docall(L, func, nResults, 1); + ccall(L, func, nResults, 1); } @@ -563,7 +563,7 @@ void luaD_call (lua_State *L, StkId func, int nResults) { ** Similar to 'luaD_call', but does not allow yields during the call. */ void luaD_callnoyield (lua_State *L, StkId func, int nResults) { - return docall(L, func, nResults, nyci); + ccall(L, func, nResults, nyci); } @@ -678,7 +678,7 @@ static void resume (lua_State *L, void *ud) { StkId firstArg = L->top - n; /* first argument */ CallInfo *ci = L->ci; if (L->status == LUA_OK) /* starting a coroutine? */ - docall(L, firstArg - 1, LUA_MULTRET, 1); /* just call its body */ + ccall(L, firstArg - 1, LUA_MULTRET, 1); /* just call its body */ else { /* resuming from previous yield */ lua_assert(L->status == LUA_YIELD); L->status = LUA_OK; /* mark that it is running (again) */ diff --git a/lparser.h b/lparser.h index 618cb0106f..2e6dae72f2 100644 --- a/lparser.h +++ b/lparser.h @@ -23,7 +23,7 @@ /* kinds of variables/expressions */ typedef enum { - VVOID, /* when 'expdesc' describes the last expression a list, + VVOID, /* when 'expdesc' describes the last expression of a list, this kind means an empty list (so, no expression) */ VNIL, /* constant nil */ VTRUE, /* constant true */ @@ -38,7 +38,8 @@ typedef enum { VLOCAL, /* local variable; var.sidx = stack index (local register); var.vidx = relative index in 'actvar.arr' */ VUPVAL, /* upvalue variable; info = index of upvalue in 'upvalues' */ - VCONST, /* compile-time constant; info = absolute index in 'actvar.arr' */ + VCONST, /* compile-time variable; + info = absolute index in 'actvar.arr' */ VINDEXED, /* indexed variable; ind.t = table register; ind.idx = key's R index */ diff --git a/manual/manual.of b/manual/manual.of index 86631bbcc2..606771f40f 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -2516,7 +2516,8 @@ Lua's garbage collection can free or move internal memory and then invalidate pointers to internal strings. To allow a safe use of these pointers, The API guarantees that any pointer to a string in a stack index -is valid while the value at that index is neither modified nor popped. +is valid while the string value at that index is not removed from the stack. +(It can be moved to another index, though.) When the index is a pseudo-index (referring to an upvalue), the pointer is valid while the corresponding call is active and the corresponding upvalue is not modified. @@ -3744,10 +3745,13 @@ except that it allows the called function to yield @see{continuations}. } @APIEntry{void lua_pop (lua_State *L, int n);| -@apii{n,0,-} +@apii{n,0,e} Pops @id{n} elements from the stack. +This function can run arbitrary code when removing an index +marked as to-be-closed from the stack. + } @APIEntry{void lua_pushboolean (lua_State *L, int b);| @@ -4227,7 +4231,7 @@ for the @Q{newindex} event @see{metatable}. } @APIEntry{void lua_settop (lua_State *L, int index);| -@apii{?,?,-} +@apii{?,?,e} Accepts any index, @N{or 0}, and sets the stack top to this index. @@ -4235,6 +4239,9 @@ If the new top is greater than the old one, then the new elements are filled with @nil. If @id{index} @N{is 0}, then all stack elements are removed. +This function can run arbitrary code when removing an index +marked as to-be-closed from the stack. + } @APIEntry{void lua_setwarnf (lua_State *L, lua_WarnFunction f, void *ud);| From 58216600eba27d472de33dbb89e2f3e629bf8a59 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 3 Nov 2020 16:34:36 -0300 Subject: [PATCH 0635/1145] 'luaL_newstate' should not allocate extra memory The allocation of a userdata for the state of the warn system can cause a panic if it fails; 'luaL_ref' also can fail. This commit re-implements the warn system so that it does not need an explicit state. Instead, the system uses different functions to represent the different states. --- lauxlib.c | 72 ++++++++++++++++++++++++++++++++++++------------------- lobject.c | 2 +- ltests.c | 2 +- 3 files changed, 50 insertions(+), 26 deletions(-) diff --git a/lauxlib.c b/lauxlib.c index cbe9ed31c3..73504389e1 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -283,10 +283,10 @@ LUALIB_API int luaL_fileresult (lua_State *L, int stat, const char *fname) { LUALIB_API int luaL_execresult (lua_State *L, int stat) { - const char *what = "exit"; /* type of termination */ if (stat != 0 && errno != 0) /* error with an 'errno'? */ return luaL_fileresult(L, 0, NULL); else { + const char *what = "exit"; /* type of termination */ l_inspectstat(stat, what); /* interpret result */ if (*what == 'e' && stat == 0) /* successful termination? */ lua_pushboolean(L, 1); @@ -1006,43 +1006,67 @@ static int panic (lua_State *L) { /* -** Emit a warning. '*warnstate' means: -** 0 - warning system is off; -** 1 - ready to start a new message; -** 2 - previous message is to be continued. +** Warning functions: +** warnfoff: warning system is off +** warnfon: ready to start a new message +** warnfcont: previous message is to be continued */ -static void warnf (void *ud, const char *message, int tocont) { - int *warnstate = (int *)ud; - if (*warnstate != 2 && !tocont && *message == '@') { /* control message? */ - if (strcmp(message, "@off") == 0) - *warnstate = 0; - else if (strcmp(message, "@on") == 0) - *warnstate = 1; - return; +static void warnfoff (void *ud, const char *message, int tocont); +static void warnfon (void *ud, const char *message, int tocont); +static void warnfcont (void *ud, const char *message, int tocont); + + +/* +** Check whether message is a control message. If so, execute the +** control or ignore it if unknown. +*/ +static int checkcontrol (lua_State *L, const char *message, int tocont) { + if (tocont || *(message++) != '@') /* not a control message? */ + return 0; + else { + if (strcmp(message, "off") == 0) + lua_setwarnf(L, warnfoff, L); /* turn warnings off */ + else if (strcmp(message, "on") == 0) + lua_setwarnf(L, warnfon, L); /* turn warnings on */ + return 1; /* it was a control message */ } - else if (*warnstate == 0) /* warnings off? */ - return; - if (*warnstate == 1) /* previous message was the last? */ - lua_writestringerror("%s", "Lua warning: "); /* start a new warning */ +} + + +static void warnfoff (void *ud, const char *message, int tocont) { + checkcontrol((lua_State *)ud, message, tocont); +} + + +/* +** Writes the message and handle 'tocont', finishing the message +** if needed and setting the next warn function. +*/ +static void warnfcont (void *ud, const char *message, int tocont) { + lua_State *L = (lua_State *)ud; lua_writestringerror("%s", message); /* write message */ if (tocont) /* not the last part? */ - *warnstate = 2; /* to be continued */ + lua_setwarnf(L, warnfcont, L); /* to be continued */ else { /* last part */ lua_writestringerror("%s", "\n"); /* finish message with end-of-line */ - *warnstate = 1; /* ready to start a new message */ + lua_setwarnf(L, warnfon, L); /* next call is a new message */ } } +static void warnfon (void *ud, const char *message, int tocont) { + if (checkcontrol((lua_State *)ud, message, tocont)) /* control message? */ + return; /* nothing else to be done */ + lua_writestringerror("%s", "Lua warning: "); /* start a new warning */ + warnfcont(ud, message, tocont); /* finish processing */ +} + + LUALIB_API lua_State *luaL_newstate (void) { lua_State *L = lua_newstate(l_alloc, NULL); if (L) { - int *warnstate; /* space for warning state */ lua_atpanic(L, &panic); - warnstate = (int *)lua_newuserdatauv(L, sizeof(int), 0); - luaL_ref(L, LUA_REGISTRYINDEX); /* make sure it won't be collected */ - *warnstate = 0; /* default is warnings off */ - lua_setwarnf(L, warnf, warnstate); + lua_setwarnf(L, warnfoff, L); /* default is warnings off */ } return L; } diff --git a/lobject.c b/lobject.c index f8ea917a85..0e504be03e 100644 --- a/lobject.c +++ b/lobject.c @@ -258,7 +258,7 @@ static const char *l_str2d (const char *s, lua_Number *result) { if (endptr == NULL) { /* failed? may be a different locale */ char buff[L_MAXLENNUM + 1]; const char *pdot = strchr(s, '.'); - if (strlen(s) > L_MAXLENNUM || pdot == NULL) + if (pdot == NULL || strlen(s) > L_MAXLENNUM) return NULL; /* string too long or no dot; fail */ strcpy(buff, s); /* copy string to buffer */ buff[pdot - s] = lua_getlocaledecpoint(); /* correct decimal point */ diff --git a/ltests.c b/ltests.c index 7e3a389a73..2020131ff9 100644 --- a/ltests.c +++ b/ltests.c @@ -863,7 +863,7 @@ static int alloc_failnext (lua_State *L) { l_memcontrol.failnext = 1; return 0; } - + static int settrick (lua_State *L) { if (ttisnil(obj_at(L, 1))) From d28265256110a0c5437247d443ddedc2a7aab116 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Sun, 8 Nov 2020 11:52:26 -0300 Subject: [PATCH 0636/1145] Bug when growing a stack When a stack grows, its extra area can be in use, and it becomes part of the common area. So, the extra area must be kept correct all the times. (Bug introduced by commit 5aa36e894f5.) --- ldo.c | 2 +- lgc.c | 4 ++-- lstate.c | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/ldo.c b/ldo.c index a60972b209..4b55c31c2d 100644 --- a/ldo.c +++ b/ldo.c @@ -192,7 +192,7 @@ int luaD_reallocstack (lua_State *L, int newsize, int raiseerror) { else return 0; /* do not raise an error */ } for (; lim < newsize; lim++) - setnilvalue(s2v(newstack + lim)); /* erase new segment */ + setnilvalue(s2v(newstack + lim + EXTRA_STACK)); /* erase new segment */ correctstack(L, L->stack, newstack); L->stack = newstack; L->stack_last = L->stack + newsize; diff --git a/lgc.c b/lgc.c index 5dba56fc3f..bab9beb12b 100644 --- a/lgc.c +++ b/lgc.c @@ -632,8 +632,8 @@ static int traversethread (global_State *g, lua_State *th) { for (uv = th->openupval; uv != NULL; uv = uv->u.open.next) markobject(g, uv); /* open upvalues cannot be collected */ if (g->gcstate == GCSatomic) { /* final traversal? */ - for (; o < th->stack_last; o++) /* clear not-marked stack slice */ - setnilvalue(s2v(o)); + for (; o < th->stack_last + EXTRA_STACK; o++) + setnilvalue(s2v(o)); /* clear dead stack slice */ /* 'remarkupvals' may have removed thread from 'twups' list */ if (!isintwups(th) && th->openupval != NULL) { th->twups = g->twups; /* link it back to the list */ diff --git a/lstate.c b/lstate.c index 4227429247..1c7b8791da 100644 --- a/lstate.c +++ b/lstate.c @@ -181,7 +181,7 @@ static void stack_init (lua_State *L1, lua_State *L) { int i; CallInfo *ci; /* initialize stack array */ L1->stack = luaM_newvector(L, BASIC_STACK_SIZE + EXTRA_STACK, StackValue); - for (i = 0; i < BASIC_STACK_SIZE; i++) + for (i = 0; i < BASIC_STACK_SIZE + EXTRA_STACK; i++) setnilvalue(s2v(L1->stack + i)); /* erase new stack */ L1->top = L1->stack; L1->stack_last = L1->stack + BASIC_STACK_SIZE; From ab1aca94e83d2eff1605ea1854df023c814cef21 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 11 Nov 2020 14:41:41 -0300 Subject: [PATCH 0637/1145] =?UTF-8?q?Removed=20optimization=20for=20=C2=AB?= =?UTF-8?q?if=20...=20then=20goto=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit That optimization was too complex and caused some weird traces when debugging. The more common case «if ... then break» was kept. --- lparser.c | 50 ++++++------------------------------------------- testes/code.lua | 22 ---------------------- 2 files changed, 6 insertions(+), 66 deletions(-) diff --git a/lparser.c b/lparser.c index fb7a1264eb..77813a74e9 100644 --- a/lparser.c +++ b/lparser.c @@ -1623,59 +1623,21 @@ static void forstat (LexState *ls, int line) { } -/* -** Check whether next instruction is a single jump (a 'break', a 'goto' -** to a forward label, or a 'goto' to a backward label with no variable -** to close). If so, set the name of the 'label' it is jumping to -** ("break" for a 'break') or to where it is jumping to ('target') and -** return true. If not a single jump, leave input unchanged, to be -** handled as a regular statement. -*/ -static int issinglejump (LexState *ls, TString **label, int *target) { - if (testnext(ls, TK_BREAK)) { /* a break? */ - *label = luaS_newliteral(ls->L, "break"); - return 1; - } - else if (ls->t.token != TK_GOTO || luaX_lookahead(ls) != TK_NAME) - return 0; /* not a valid goto */ - else { - TString *lname = ls->lookahead.seminfo.ts; /* label's id */ - Labeldesc *lb = findlabel(ls, lname); - if (lb) { /* a backward jump? */ - /* does it need to close variables? */ - if (luaY_nvarstack(ls->fs) > stacklevel(ls->fs, lb->nactvar)) - return 0; /* not a single jump; cannot optimize */ - *target = lb->pc; - } - else /* jump forward */ - *label = lname; - luaX_next(ls); /* skip goto */ - luaX_next(ls); /* skip name */ - return 1; - } -} - - static void test_then_block (LexState *ls, int *escapelist) { /* test_then_block -> [IF | ELSEIF] cond THEN block */ BlockCnt bl; - int line; FuncState *fs = ls->fs; - TString *jlb = NULL; - int target = NO_JUMP; expdesc v; int jf; /* instruction to skip 'then' code (if condition is false) */ luaX_next(ls); /* skip IF or ELSEIF */ expr(ls, &v); /* read condition */ checknext(ls, TK_THEN); - line = ls->linenumber; - if (issinglejump(ls, &jlb, &target)) { /* 'if x then goto' ? */ - luaK_goiffalse(ls->fs, &v); /* will jump to label if condition is true */ + if (ls->t.token == TK_BREAK) { /* 'if x then break' ? */ + int line = ls->linenumber; + luaK_goiffalse(ls->fs, &v); /* will jump if condition is true */ + luaX_next(ls); /* skip 'break' */ enterblock(fs, &bl, 0); /* must enter block before 'goto' */ - if (jlb != NULL) /* forward jump? */ - newgotoentry(ls, jlb, line, v.t); /* will be resolved later */ - else /* backward jump */ - luaK_patchlist(fs, v.t, target); /* jump directly to 'target' */ + newgotoentry(ls, luaS_newliteral(ls->L, "break"), line, v.t); while (testnext(ls, ';')) {} /* skip semicolons */ if (block_follow(ls, 0)) { /* jump is the entire block? */ leaveblock(fs); @@ -1684,7 +1646,7 @@ static void test_then_block (LexState *ls, int *escapelist) { else /* must skip over 'then' part if condition is false */ jf = luaK_jump(fs); } - else { /* regular case (not a jump) */ + else { /* regular case (not a break) */ luaK_goiftrue(ls->fs, &v); /* skip over block if condition is false */ enterblock(fs, &bl, 0); jf = v.f; diff --git a/testes/code.lua b/testes/code.lua index 34b046688d..1f971cd775 100644 --- a/testes/code.lua +++ b/testes/code.lua @@ -392,28 +392,6 @@ check(function (a, b) end, 'TEST', 'JMP', 'TEST', 'JMP', 'ADDI', 'MMBINI', 'JMP', 'RETURN0') -checkequal( -function (a) while a < 10 do a = a + 1 end end, -function (a) - ::loop:: - if not (a < 10) then goto exit end - a = a + 1 - goto loop -::exit:: -end -) - -checkequal( -function (a) repeat local x = a + 1; a = x until a > 0 end, -function (a) - ::loop:: do - local x = a + 1 - a = x - end - if not (a > 0) then goto loop end -end -) - checkequal(function () return 6 or true or nil end, function () return k6 or kTrue or kNil end) From 2f4162bc473b995117e95c88230f637ca3e1c866 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 11 Nov 2020 15:10:51 -0300 Subject: [PATCH 0638/1145] Compiler optimization back to '-O2' Undo commit 6a10f03ff. Compiler performance is important, too. --- makefile | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/makefile b/makefile index 7af55332c6..9e537dcc90 100644 --- a/makefile +++ b/makefile @@ -109,16 +109,6 @@ $(LUA_T): $(LUA_O) $(CORE_T) $(CC) -o $@ $(MYLDFLAGS) $(LUA_O) $(CORE_T) $(LIBS) $(MYLIBS) $(DL) -llex.o: - $(CC) $(CFLAGS) -Os -c llex.c - -lparser.o: - $(CC) $(CFLAGS) -Os -c lparser.c - -lcode.o: - $(CC) $(CFLAGS) -Os -c lcode.c - - clean: $(RM) $(ALL_T) $(ALL_O) From 9d067ab73b6befa0a5418f1df35c711f6c6918b3 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 13 Nov 2020 09:59:07 -0300 Subject: [PATCH 0639/1145] Optimization for 'n^2' Squares are much more common than other exponentiations, and 'n*n' is much more efficient than 'pow'. --- llimits.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/llimits.h b/llimits.h index a76c13ed65..d03948314f 100644 --- a/llimits.h +++ b/llimits.h @@ -326,7 +326,8 @@ typedef l_uint32 Instruction; /* exponentiation */ #if !defined(luai_numpow) -#define luai_numpow(L,a,b) ((void)L, l_mathop(pow)(a,b)) +#define luai_numpow(L,a,b) \ + ((void)L, (b == 2) ? (a)*(a) : l_mathop(pow)(a,b)) #endif /* the others are quite standard operations */ From 131e3fd814a6e818b412407a222186aab08f3525 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 24 Nov 2020 14:41:50 -0300 Subject: [PATCH 0640/1145] Avoid using 'signal' when 'sigaction' is available The semantics of 'signal' varies a lot among different implementations; 'sigaction' ensures a more consistent behavior. --- lua.c | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/lua.c b/lua.c index b5b884b6d0..46b48dba92 100644 --- a/lua.c +++ b/lua.c @@ -37,6 +37,26 @@ static lua_State *globalL = NULL; static const char *progname = LUA_PROGNAME; +#if defined(LUA_USE_POSIX) /* { */ + +/* +** Use 'sigaction' when available. +*/ +static void setsignal (int sig, void (*handler)(int)) { + struct sigaction sa; + sa.sa_handler = handler; + sa.sa_flags = 0; + sigemptyset(&sa.sa_mask); /* do not mask any signal */ + sigaction(sig, &sa, NULL); +} + +#else /* }{ */ + +#define setsignal signal + +#endif /* } */ + + /* ** Hook set by signal function to stop the interpreter. */ @@ -55,7 +75,7 @@ static void lstop (lua_State *L, lua_Debug *ar) { */ static void laction (int i) { int flag = LUA_MASKCALL | LUA_MASKRET | LUA_MASKLINE | LUA_MASKCOUNT; - signal(i, SIG_DFL); /* if another SIGINT happens, terminate process */ + setsignal(i, SIG_DFL); /* if another SIGINT happens, terminate process */ lua_sethook(globalL, lstop, flag, 1); } @@ -135,9 +155,9 @@ static int docall (lua_State *L, int narg, int nres) { lua_pushcfunction(L, msghandler); /* push message handler */ lua_insert(L, base); /* put it under function and args */ globalL = L; /* to be available to 'laction' */ - signal(SIGINT, laction); /* set C-signal handler */ + setsignal(SIGINT, laction); /* set C-signal handler */ status = lua_pcall(L, narg, nres, base); - signal(SIGINT, SIG_DFL); /* reset C-signal handler */ + setsignal(SIGINT, SIG_DFL); /* reset C-signal handler */ lua_remove(L, base); /* remove message handler from the stack */ return status; } From 65d2294454ab68d164154c7ce05caa50bd97d143 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 26 Nov 2020 18:23:40 -0300 Subject: [PATCH 0641/1145] Changed access to global table in the registry The global table is always in the array part of the registry; we can use this fact to make its access slightly more efficient. --- lapi.c | 25 +++++++++++++++++-------- lstate.c | 9 +++------ 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/lapi.c b/lapi.c index c824da27cb..9fffcc1613 100644 --- a/lapi.c +++ b/lapi.c @@ -629,11 +629,21 @@ static int auxgetstr (lua_State *L, const TValue *t, const char *k) { } +/* +** Get the global table in the registry. Since all predefined +** indices in the registry were inserted right when the registry +** was created and never removed, they must always be in the array +** part of the registry. +*/ +#define getGtable(L) \ + (&hvalue(&G(L)->l_registry)->array[LUA_RIDX_GLOBALS - 1]) + + LUA_API int lua_getglobal (lua_State *L, const char *name) { - Table *reg; + const TValue *G; lua_lock(L); - reg = hvalue(&G(L)->l_registry); - return auxgetstr(L, luaH_getint(reg, LUA_RIDX_GLOBALS), name); + G = getGtable(L); + return auxgetstr(L, G, name); } @@ -811,10 +821,10 @@ static void auxsetstr (lua_State *L, const TValue *t, const char *k) { LUA_API void lua_setglobal (lua_State *L, const char *name) { - Table *reg; + const TValue *G; lua_lock(L); /* unlock done in 'auxsetstr' */ - reg = hvalue(&G(L)->l_registry); - auxsetstr(L, luaH_getint(reg, LUA_RIDX_GLOBALS), name); + G = getGtable(L); + auxsetstr(L, G, name); } @@ -1063,8 +1073,7 @@ LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data, LClosure *f = clLvalue(s2v(L->top - 1)); /* get newly created function */ if (f->nupvalues >= 1) { /* does it have an upvalue? */ /* get global table from registry */ - Table *reg = hvalue(&G(L)->l_registry); - const TValue *gt = luaH_getint(reg, LUA_RIDX_GLOBALS); + const TValue *gt = getGtable(L); /* set global table as 1st upvalue of 'f' (may be LUA_ENV) */ setobj(L, f->upvals[0]->v, gt); luaC_barrier(L, f->upvals[0], gt); diff --git a/lstate.c b/lstate.c index 1c7b8791da..1596b51cf0 100644 --- a/lstate.c +++ b/lstate.c @@ -213,17 +213,14 @@ static void freestack (lua_State *L) { ** Create registry table and its predefined values */ static void init_registry (lua_State *L, global_State *g) { - TValue temp; /* create registry */ Table *registry = luaH_new(L); sethvalue(L, &g->l_registry, registry); luaH_resize(L, registry, LUA_RIDX_LAST, 0); /* registry[LUA_RIDX_MAINTHREAD] = L */ - setthvalue(L, &temp, L); /* temp = L */ - luaH_setint(L, registry, LUA_RIDX_MAINTHREAD, &temp); - /* registry[LUA_RIDX_GLOBALS] = table of globals */ - sethvalue(L, &temp, luaH_new(L)); /* temp = new table (global table) */ - luaH_setint(L, registry, LUA_RIDX_GLOBALS, &temp); + setthvalue(L, ®istry->array[LUA_RIDX_MAINTHREAD - 1], L); + /* registry[LUA_RIDX_GLOBALS] = new table (table of globals) */ + sethvalue(L, ®istry->array[LUA_RIDX_GLOBALS - 1], luaH_new(L)); } From d9d2904f09a8039522dfd6f118d4e37bffd5bdf6 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 2 Dec 2020 15:13:13 -0300 Subject: [PATCH 0642/1145] Details Names in the parser and other details that do not change actual code. --- README.md | 2 +- lcode.c | 6 +++--- ldblib.c | 4 ++-- lparser.c | 46 +++++++++++++++++++++++----------------------- lparser.h | 6 +++--- 5 files changed, 32 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index 5893dc9aa1..5bc0ee77c4 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,6 @@ This is the repository of Lua development code, as seen by the Lua team. It contains the full history of all commits but is mirrored irregularly. For complete information about Lua, visit [Lua.org](https://www.lua.org/). -Please **do not** send pull requests. To report issues and send patches, post a message to the [Lua mailing list](https://www.lua.org/lua-l.html). +Please **do not** send pull requests. To report issues, post a message to the [Lua mailing list](https://www.lua.org/lua-l.html). Download official Lua releases from [Lua.org](https://www.lua.org/download.html). diff --git a/lcode.c b/lcode.c index 14d41f1a7e..97e427b244 100644 --- a/lcode.c +++ b/lcode.c @@ -763,7 +763,7 @@ void luaK_dischargevars (FuncState *fs, expdesc *e) { break; } case VLOCAL: { /* already in a register */ - e->u.info = e->u.var.sidx; + e->u.info = e->u.var.ridx; e->k = VNONRELOC; /* becomes a non-relocatable value */ break; } @@ -1036,7 +1036,7 @@ void luaK_storevar (FuncState *fs, expdesc *var, expdesc *ex) { switch (var->k) { case VLOCAL: { freeexp(fs, ex); - exp2reg(fs, ex, var->u.var.sidx); /* compute 'ex' into proper place */ + exp2reg(fs, ex, var->u.var.ridx); /* compute 'ex' into proper place */ return; } case VUPVAL: { @@ -1276,7 +1276,7 @@ void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) { } else { /* register index of the table */ - t->u.ind.t = (t->k == VLOCAL) ? t->u.var.sidx: t->u.info; + t->u.ind.t = (t->k == VLOCAL) ? t->u.var.ridx: t->u.info; if (isKstr(fs, k)) { t->u.ind.idx = k->u.info; /* literal string */ t->k = VINDEXSTR; diff --git a/ldblib.c b/ldblib.c index 5a326adedb..15593bfbd1 100644 --- a/ldblib.c +++ b/ldblib.c @@ -377,7 +377,7 @@ static int db_sethook (lua_State *L) { } if (!luaL_getsubtable(L, LUA_REGISTRYINDEX, HOOKKEY)) { /* table just created; initialize it */ - lua_pushstring(L, "k"); + lua_pushliteral(L, "k"); lua_setfield(L, -2, "__mode"); /** hooktable.__mode = "k" */ lua_pushvalue(L, -1); lua_setmetatable(L, -2); /* metatable(hooktable) = hooktable */ @@ -420,7 +420,7 @@ static int db_debug (lua_State *L) { for (;;) { char buffer[250]; lua_writestringerror("%s", "lua_debug> "); - if (fgets(buffer, sizeof(buffer), stdin) == 0 || + if (fgets(buffer, sizeof(buffer), stdin) == NULL || strcmp(buffer, "cont\n") == 0) return 0; if (luaL_loadbuffer(L, buffer, strlen(buffer), "=(debug command)") || diff --git a/lparser.c b/lparser.c index 77813a74e9..249ba9a40b 100644 --- a/lparser.c +++ b/lparser.c @@ -222,26 +222,26 @@ static Vardesc *getlocalvardesc (FuncState *fs, int vidx) { /* -** Convert 'nvar', a compiler index level, to it corresponding -** stack index level. For that, search for the highest variable -** below that level that is in the stack and uses its stack -** index ('sidx'). +** Convert 'nvar', a compiler index level, to its corresponding +** register. For that, search for the highest variable below that level +** that is in a register and uses its register index ('ridx') plus one. */ -static int stacklevel (FuncState *fs, int nvar) { +static int reglevel (FuncState *fs, int nvar) { while (nvar-- > 0) { - Vardesc *vd = getlocalvardesc(fs, nvar); /* get variable */ - if (vd->vd.kind != RDKCTC) /* is in the stack? */ - return vd->vd.sidx + 1; + Vardesc *vd = getlocalvardesc(fs, nvar); /* get previous variable */ + if (vd->vd.kind != RDKCTC) /* is in a register? */ + return vd->vd.ridx + 1; } - return 0; /* no variables in the stack */ + return 0; /* no variables in registers */ } /* -** Return the number of variables in the stack for function 'fs' +** Return the number of variables in the register stack for the given +** function. */ int luaY_nvarstack (FuncState *fs) { - return stacklevel(fs, fs->nactvar); + return reglevel(fs, fs->nactvar); } @@ -267,7 +267,7 @@ static void init_var (FuncState *fs, expdesc *e, int vidx) { e->f = e->t = NO_JUMP; e->k = VLOCAL; e->u.var.vidx = vidx; - e->u.var.sidx = getlocalvardesc(fs, vidx)->vd.sidx; + e->u.var.ridx = getlocalvardesc(fs, vidx)->vd.ridx; } @@ -310,12 +310,12 @@ static void check_readonly (LexState *ls, expdesc *e) { */ static void adjustlocalvars (LexState *ls, int nvars) { FuncState *fs = ls->fs; - int stklevel = luaY_nvarstack(fs); + int reglevel = luaY_nvarstack(fs); int i; for (i = 0; i < nvars; i++) { int vidx = fs->nactvar++; Vardesc *var = getlocalvardesc(fs, vidx); - var->vd.sidx = stklevel++; + var->vd.ridx = reglevel++; var->vd.pidx = registerlocalvar(ls, fs, var->vd.name); } } @@ -366,7 +366,7 @@ static int newupvalue (FuncState *fs, TString *name, expdesc *v) { FuncState *prev = fs->prev; if (v->k == VLOCAL) { up->instack = 1; - up->idx = v->u.var.sidx; + up->idx = v->u.var.ridx; up->kind = getlocalvardesc(prev, v->u.var.vidx)->vd.kind; lua_assert(eqstr(name, getlocalvardesc(prev, v->u.var.vidx)->vd.name)); } @@ -620,7 +620,7 @@ static void movegotosout (FuncState *fs, BlockCnt *bl) { for (i = bl->firstgoto; i < gl->n; i++) { /* for each pending goto */ Labeldesc *gt = &gl->arr[i]; /* leaving a variable scope? */ - if (stacklevel(fs, gt->nactvar) > stacklevel(fs, bl->nactvar)) + if (reglevel(fs, gt->nactvar) > reglevel(fs, bl->nactvar)) gt->close |= bl->upval; /* jump may need a close */ gt->nactvar = bl->nactvar; /* update goto level */ } @@ -661,7 +661,7 @@ static void leaveblock (FuncState *fs) { BlockCnt *bl = fs->bl; LexState *ls = fs->ls; int hasclose = 0; - int stklevel = stacklevel(fs, bl->nactvar); /* level outside the block */ + int stklevel = reglevel(fs, bl->nactvar); /* level outside the block */ if (bl->isloop) /* fix pending breaks? */ hasclose = createlabel(ls, luaS_newliteral(ls->L, "break"), 0, 0); if (!hasclose && bl->previous && bl->upval) @@ -1330,13 +1330,13 @@ static void check_conflict (LexState *ls, struct LHS_assign *lh, expdesc *v) { } } else { /* table is a register */ - if (v->k == VLOCAL && lh->v.u.ind.t == v->u.var.sidx) { + if (v->k == VLOCAL && lh->v.u.ind.t == v->u.var.ridx) { conflict = 1; /* table is the local being assigned now */ lh->v.u.ind.t = extra; /* assignment will use safe copy */ } /* is index the local being assigned? */ if (lh->v.k == VINDEXED && v->k == VLOCAL && - lh->v.u.ind.idx == v->u.var.sidx) { + lh->v.u.ind.idx == v->u.var.ridx) { conflict = 1; lh->v.u.ind.idx = extra; /* previous assignment will use safe copy */ } @@ -1346,7 +1346,7 @@ static void check_conflict (LexState *ls, struct LHS_assign *lh, expdesc *v) { if (conflict) { /* copy upvalue/local value to a temporary (in position 'extra') */ if (v->k == VLOCAL) - luaK_codeABC(fs, OP_MOVE, extra, v->u.var.sidx, 0); + luaK_codeABC(fs, OP_MOVE, extra, v->u.var.ridx, 0); else luaK_codeABC(fs, OP_GETUPVAL, extra, v->u.info, 0); luaK_reserveregs(fs, 1); @@ -1411,7 +1411,7 @@ static void gotostat (LexState *ls) { newgotoentry(ls, name, line, luaK_jump(fs)); else { /* found a label */ /* backward jump; will be resolved here */ - int lblevel = stacklevel(fs, lb->nactvar); /* label level */ + int lblevel = reglevel(fs, lb->nactvar); /* label level */ if (luaY_nvarstack(fs) > lblevel) /* leaving the scope of a variable? */ luaK_codeABC(fs, OP_CLOSE, lblevel, 0, 0); /* create jump and link it to the label */ @@ -1488,7 +1488,7 @@ static void repeatstat (LexState *ls, int line) { if (bl2.upval) { /* upvalues? */ int exit = luaK_jump(fs); /* normal exit must jump over fix */ luaK_patchtohere(fs, condexit); /* repetition must close upvalues */ - luaK_codeABC(fs, OP_CLOSE, stacklevel(fs, bl2.nactvar), 0, 0); + luaK_codeABC(fs, OP_CLOSE, reglevel(fs, bl2.nactvar), 0, 0); condexit = luaK_jump(fs); /* repeat after closing upvalues */ luaK_patchtohere(fs, exit); /* normal exit comes to here */ } @@ -1708,7 +1708,7 @@ static void checktoclose (LexState *ls, int level) { FuncState *fs = ls->fs; markupval(fs, level + 1); fs->bl->insidetbc = 1; /* in the scope of a to-be-closed variable */ - luaK_codeABC(fs, OP_TBC, stacklevel(fs, level), 0, 0); + luaK_codeABC(fs, OP_TBC, reglevel(fs, level), 0, 0); } } diff --git a/lparser.h b/lparser.h index 2e6dae72f2..5e4500f181 100644 --- a/lparser.h +++ b/lparser.h @@ -35,7 +35,7 @@ typedef enum { (string is fixed by the lexer) */ VNONRELOC, /* expression has its value in a fixed register; info = result register */ - VLOCAL, /* local variable; var.sidx = stack index (local register); + VLOCAL, /* local variable; var.ridx = register index; var.vidx = relative index in 'actvar.arr' */ VUPVAL, /* upvalue variable; info = index of upvalue in 'upvalues' */ VCONST, /* compile-time variable; @@ -77,7 +77,7 @@ typedef struct expdesc { lu_byte t; /* table (register or upvalue) */ } ind; struct { /* for local variables */ - lu_byte sidx; /* index in the stack */ + lu_byte ridx; /* register holding the variable */ unsigned short vidx; /* compiler index (in 'actvar.arr') */ } var; } u; @@ -97,7 +97,7 @@ typedef union Vardesc { struct { TValuefields; /* constant value (if it is a compile-time constant) */ lu_byte kind; - lu_byte sidx; /* index of the variable in the stack */ + lu_byte ridx; /* register holding the variable */ short pidx; /* index of the variable in the Proto's 'locvars' array */ TString *name; /* variable name */ } vd; From d41c36bf67d6628bccd91697e7f88e55d40d3970 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 3 Dec 2020 10:39:38 -0300 Subject: [PATCH 0643/1145] 'lua_assert' moved from 'lualib.h' to 'lauxlib.h' The macro is useful also in 'lauxlib.c', which does not include 'lualib.h'. Also, the definition was corrected to be "on" when LUAI_ASSERT is defined. --- lauxlib.h | 12 ++++++++++++ lualib.h | 6 ------ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/lauxlib.h b/lauxlib.h index 59fef6af13..df3de4f82a 100644 --- a/lauxlib.h +++ b/lauxlib.h @@ -157,6 +157,18 @@ LUALIB_API void (luaL_requiref) (lua_State *L, const char *modname, #define luaL_pushfail(L) lua_pushnil(L) +/* +** Internal assertions for in-house debugging +*/ +#if defined LUAI_ASSERT +#include +#define lua_assert(c) assert(c) +#else +#define lua_assert(x) ((void)0) +#endif + + + /* ** {====================================================== ** Generic Buffer manipulation diff --git a/lualib.h b/lualib.h index eb08b530a6..2625529076 100644 --- a/lualib.h +++ b/lualib.h @@ -49,10 +49,4 @@ LUAMOD_API int (luaopen_package) (lua_State *L); LUALIB_API void (luaL_openlibs) (lua_State *L); - -#if !defined(lua_assert) -#define lua_assert(x) ((void)0) -#endif - - #endif From c36ced53c92088748c278764acb68dfb66f353c9 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 3 Dec 2020 12:00:32 -0300 Subject: [PATCH 0644/1145] Avoid "bad programming habits" in the reference system References were using both 0 indices and nils as values in arrays. Both do not fit in the concept of a sequence, which is the kind of use that guides all Lua optimizations. --- lauxlib.c | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/lauxlib.c b/lauxlib.c index 73504389e1..074ff08cd4 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -639,10 +639,14 @@ LUALIB_API char *luaL_buffinitsize (lua_State *L, luaL_Buffer *B, size_t sz) { ** ======================================================= */ -/* index of free-list header */ -#define freelist 0 - +/* index of free-list header (after the predefined values) */ +#define freelist (LUA_RIDX_LAST + 1) +/* +** The previously freed references form a linked list: +** t[freelist] is the index of a first free index, or zero if list is +** empty; t[t[freelist]] is the index of the second element; etc. +*/ LUALIB_API int luaL_ref (lua_State *L, int t) { int ref; if (lua_isnil(L, -1)) { @@ -650,9 +654,16 @@ LUALIB_API int luaL_ref (lua_State *L, int t) { return LUA_REFNIL; /* 'nil' has a unique fixed reference */ } t = lua_absindex(L, t); - lua_rawgeti(L, t, freelist); /* get first free element */ - ref = (int)lua_tointeger(L, -1); /* ref = t[freelist] */ - lua_pop(L, 1); /* remove it from stack */ + if (lua_rawgeti(L, t, freelist) == LUA_TNIL) { /* first access? */ + ref = 0; /* list is empty */ + lua_pushinteger(L, 0); /* initialize as an empty list */ + lua_rawseti(L, t, freelist); /* ref = t[freelist] = 0 */ + } + else { /* already initialized */ + lua_assert(lua_isinteger(L, -1)); + ref = (int)lua_tointeger(L, -1); /* ref = t[freelist] */ + } + lua_pop(L, 1); /* remove element from stack */ if (ref != 0) { /* any free element? */ lua_rawgeti(L, t, ref); /* remove it from list */ lua_rawseti(L, t, freelist); /* (t[freelist] = t[ref]) */ @@ -668,6 +679,7 @@ LUALIB_API void luaL_unref (lua_State *L, int t, int ref) { if (ref >= 0) { t = lua_absindex(L, t); lua_rawgeti(L, t, freelist); + lua_assert(lua_isinteger(L, -1)); lua_rawseti(L, t, ref); /* t[ref] = t[freelist] */ lua_pushinteger(L, ref); lua_rawseti(L, t, freelist); /* t[freelist] = ref */ From 754ca0060fcac9829cfb90dd68d96cbe14aa84f7 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 3 Dec 2020 12:09:50 -0300 Subject: [PATCH 0645/1145] n Windows, 'popen' accepts "[rw][bt]?" as valid modes Added the modifiers 'b' and 't' to valid modes for 'popen' in Windows. --- liolib.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/liolib.c b/liolib.c index 60ab1bfab6..79516724c7 100644 --- a/liolib.c +++ b/liolib.c @@ -52,12 +52,6 @@ static int l_checkmode (const char *mode) { ** ======================================================= */ -#if !defined(l_checkmodep) -/* By default, Lua accepts only "r" or "w" as mode */ -#define l_checkmodep(m) ((m[0] == 'r' || m[0] == 'w') && m[1] == '\0') -#endif - - #if !defined(l_popen) /* { */ #if defined(LUA_USE_POSIX) /* { */ @@ -70,6 +64,12 @@ static int l_checkmode (const char *mode) { #define l_popen(L,c,m) (_popen(c,m)) #define l_pclose(L,file) (_pclose(file)) +#if !defined(l_checkmodep) +/* Windows accepts "[rw][bt]?" as valid modes */ +#define l_checkmodep(m) ((m[0] == 'r' || m[0] == 'w') && \ + (m[1] == '\0' || ((m[1] == 'b' || m[1] == 't') && m[2] == '\0'))) +#endif + #else /* }{ */ /* ISO C definitions */ @@ -83,6 +83,12 @@ static int l_checkmode (const char *mode) { #endif /* } */ + +#if !defined(l_checkmodep) +/* By default, Lua accepts only "r" or "w" as valid modes */ +#define l_checkmodep(m) ((m[0] == 'r' || m[0] == 'w') && m[1] == '\0') +#endif + /* }====================================================== */ From f15589f3b0da477e5dda8863cbf4c0b36469e36d Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 3 Dec 2020 12:11:15 -0300 Subject: [PATCH 0646/1145] Added test cases for error messages about goto/label --- testes/errors.lua | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/testes/errors.lua b/testes/errors.lua index 422c1128d5..a3f0702167 100644 --- a/testes/errors.lua +++ b/testes/errors.lua @@ -67,6 +67,27 @@ checksyntax([[ ]], "'}' expected (to close '{' at line 1)", "", 3) +do -- testing errors in goto/break + local function checksyntax (prog, msg, line) + local st, err = load(prog) + assert(string.find(err, "line " .. line)) + assert(string.find(err, msg, 1, true)) + end + + checksyntax([[ + ::A:: a = 1 + ::A:: + ]], "label 'A' already defined", 1) + + checksyntax([[ + a = 1 + goto A + do ::A:: end + ]], "no visible label 'A'", 2) + +end + + if not T then (Message or print) ('\n >>> testC not active: skipping memory message test <<<\n') From 23051e830a8b212f831443eb888e93e30fa8bb19 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 4 Dec 2020 11:08:42 -0300 Subject: [PATCH 0647/1145] Changes in the API of 'luaH_set' and related functions Functions to set values in a table (luaH_set, luaH_newkey, etc.) receive the new value, instead of returning a slot where to put the value. --- lapi.c | 4 +--- lcode.c | 8 ++++++-- llex.c | 31 +++++++++++++++++-------------- ltable.c | 42 +++++++++++++++++++++++++++--------------- ltable.h | 8 ++++++-- lvm.c | 5 +---- 6 files changed, 58 insertions(+), 40 deletions(-) diff --git a/lapi.c b/lapi.c index 9fffcc1613..03e756d645 100644 --- a/lapi.c +++ b/lapi.c @@ -871,12 +871,10 @@ LUA_API void lua_seti (lua_State *L, int idx, lua_Integer n) { static void aux_rawset (lua_State *L, int idx, TValue *key, int n) { Table *t; - TValue *slot; lua_lock(L); api_checknelems(L, n); t = gettable(L, idx); - slot = luaH_set(L, t, key); - setobj2t(L, slot, s2v(L->top - 1)); + luaH_set(L, t, key, s2v(L->top - 1)); invalidateTMcache(t); luaC_barrierback(L, obj2gco(t), s2v(L->top - 1)); L->top -= n; diff --git a/lcode.c b/lcode.c index 97e427b244..d8d353fe4e 100644 --- a/lcode.c +++ b/lcode.c @@ -545,11 +545,14 @@ static void freeexps (FuncState *fs, expdesc *e1, expdesc *e2) { ** and try to reuse constants. Because some values should not be used ** as keys (nil cannot be a key, integer keys can collapse with float ** keys), the caller must provide a useful 'key' for indexing the cache. +** Note that all functions share the same table, so entering or exiting +** a function can make some indices wrong. */ static int addk (FuncState *fs, TValue *key, TValue *v) { + TValue val; lua_State *L = fs->ls->L; Proto *f = fs->f; - TValue *idx = luaH_set(L, fs->ls->h, key); /* index scanner table */ + const TValue *idx = luaH_get(fs->ls->h, key); /* query scanner table */ int k, oldsize; if (ttisinteger(idx)) { /* is there an index there? */ k = cast_int(ivalue(idx)); @@ -563,7 +566,8 @@ static int addk (FuncState *fs, TValue *key, TValue *v) { k = fs->nk; /* numerical value does not need GC barrier; table has no metatable, so it does not need to invalidate cache */ - setivalue(idx, k); + setivalue(&val, k); + luaH_finishset(L, fs->ls->h, key, idx, &val); luaM_growvector(L, f->k, k, f->sizek, TValue, MAXARG_Ax, "constants"); while (oldsize < f->sizek) setnilvalue(&f->k[oldsize++]); setobj(L, &f->k[k], v); diff --git a/llex.c b/llex.c index 4b8dec9985..e99151787a 100644 --- a/llex.c +++ b/llex.c @@ -122,26 +122,29 @@ l_noret luaX_syntaxerror (LexState *ls, const char *msg) { /* -** creates a new string and anchors it in scanner's table so that -** it will not be collected until the end of the compilation -** (by that time it should be anchored somewhere) +** Creates a new string and anchors it in scanner's table so that it +** will not be collected until the end of the compilation; by that time +** it should be anchored somewhere. It also internalizes long strings, +** ensuring there is only one copy of each unique string. The table +** here is used as a set: the string enters as the key, while its value +** is irrelevant. We use the string itself as the value only because it +** is a TValue readly available. Later, the code generation can change +** this value. */ TString *luaX_newstring (LexState *ls, const char *str, size_t l) { lua_State *L = ls->L; - TValue *o; /* entry for 'str' */ TString *ts = luaS_newlstr(L, str, l); /* create new string */ - setsvalue2s(L, L->top++, ts); /* temporarily anchor it in stack */ - o = luaH_set(L, ls->h, s2v(L->top - 1)); - if (isempty(o)) { /* not in use yet? */ - /* boolean value does not need GC barrier; - table is not a metatable, so it does not need to invalidate cache */ - setbtvalue(o); /* t[string] = true */ + const TValue *o = luaH_getstr(ls->h, ts); + if (!ttisnil(o)) /* string already present? */ + ts = keystrval(nodefromval(o)); /* get saved copy */ + else { /* not in use yet */ + TValue *stv = s2v(L->top++); /* reserve stack space for string */ + setsvalue(L, stv, ts); /* temporarily anchor the string */ + luaH_finishset(L, ls->h, stv, o, stv); /* t[string] = string */ + /* table is not a metatable, so it does not need to invalidate cache */ luaC_checkGC(L); + L->top--; /* remove string from stack */ } - else { /* string already present */ - ts = keystrval(nodefromval(o)); /* re-use value previously stored */ - } - L->top--; /* remove string from stack */ return ts; } diff --git a/ltable.c b/ltable.c index 7e7cbed97c..e9410f99bd 100644 --- a/ltable.c +++ b/ltable.c @@ -485,7 +485,7 @@ static void reinsert (lua_State *L, Table *ot, Table *t) { already present in the table */ TValue k; getnodekey(L, &k, old); - setobjt2t(L, luaH_set(L, t, &k), gval(old)); + luaH_set(L, t, &k, gval(old)); } } } @@ -632,7 +632,7 @@ static Node *getfreepos (Table *t) { ** put new key in its main position; otherwise (colliding node is in its main ** position), new key goes to an empty position. */ -TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) { +void luaH_newkey (lua_State *L, Table *t, const TValue *key, TValue *value) { Node *mp; TValue aux; if (unlikely(ttisnil(key))) @@ -654,7 +654,8 @@ TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) { if (f == NULL) { /* cannot find a free place? */ rehash(L, t, key); /* grow table */ /* whatever called 'newkey' takes care of TM cache */ - return luaH_set(L, t, key); /* insert key into grown table */ + luaH_set(L, t, key, value); /* insert key into grown table */ + return; } lua_assert(!isdummy(t)); othern = mainposition(t, keytt(mp), &keyval(mp)); @@ -682,7 +683,7 @@ TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) { setnodekey(L, mp, key); luaC_barrierback(L, obj2gco(t), key); lua_assert(isempty(gval(mp))); - return gval(mp); + setobj2t(L, gval(mp), value); } @@ -769,29 +770,40 @@ const TValue *luaH_get (Table *t, const TValue *key) { } +/* +** Finish a raw "set table" operation, where 'slot' is where the value +** should have been (the result of a previous "get table"). +** Beware: when using this function you probably need to check a GC +** barrier and invalidate the TM cache. +*/ +void luaH_finishset (lua_State *L, Table *t, const TValue *key, + const TValue *slot, TValue *value) { + if (isabstkey(slot)) + luaH_newkey(L, t, key, value); + else + setobj2t(L, cast(TValue *, slot), value); +} + + /* ** beware: when using this function you probably need to check a GC ** barrier and invalidate the TM cache. */ -TValue *luaH_set (lua_State *L, Table *t, const TValue *key) { - const TValue *p = luaH_get(t, key); - if (!isabstkey(p)) - return cast(TValue *, p); - else return luaH_newkey(L, t, key); +void luaH_set (lua_State *L, Table *t, const TValue *key, TValue *value) { + const TValue *slot = luaH_get(t, key); + luaH_finishset(L, t, key, slot, value); } void luaH_setint (lua_State *L, Table *t, lua_Integer key, TValue *value) { const TValue *p = luaH_getint(t, key); - TValue *cell; - if (!isabstkey(p)) - cell = cast(TValue *, p); - else { + if (isabstkey(p)) { TValue k; setivalue(&k, key); - cell = luaH_newkey(L, t, &k); + luaH_newkey(L, t, &k, value); } - setobj2t(L, cell, value); + else + setobj2t(L, cast(TValue *, p), value); } diff --git a/ltable.h b/ltable.h index c0060f4b6e..7bbbcb213f 100644 --- a/ltable.h +++ b/ltable.h @@ -41,8 +41,12 @@ LUAI_FUNC void luaH_setint (lua_State *L, Table *t, lua_Integer key, LUAI_FUNC const TValue *luaH_getshortstr (Table *t, TString *key); LUAI_FUNC const TValue *luaH_getstr (Table *t, TString *key); LUAI_FUNC const TValue *luaH_get (Table *t, const TValue *key); -LUAI_FUNC TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key); -LUAI_FUNC TValue *luaH_set (lua_State *L, Table *t, const TValue *key); +LUAI_FUNC void luaH_newkey (lua_State *L, Table *t, const TValue *key, + TValue *value); +LUAI_FUNC void luaH_set (lua_State *L, Table *t, const TValue *key, + TValue *value); +LUAI_FUNC void luaH_finishset (lua_State *L, Table *t, const TValue *key, + const TValue *slot, TValue *value); LUAI_FUNC Table *luaH_new (lua_State *L); LUAI_FUNC void luaH_resize (lua_State *L, Table *t, unsigned int nasize, unsigned int nhsize); diff --git a/lvm.c b/lvm.c index aa3b22bf73..ccebdbe05e 100644 --- a/lvm.c +++ b/lvm.c @@ -337,10 +337,7 @@ void luaV_finishset (lua_State *L, const TValue *t, TValue *key, lua_assert(isempty(slot)); /* slot must be empty */ tm = fasttm(L, h->metatable, TM_NEWINDEX); /* get metamethod */ if (tm == NULL) { /* no metamethod? */ - if (isabstkey(slot)) /* no previous entry? */ - slot = luaH_newkey(L, h, key); /* create one */ - /* no metamethod and (now) there is an entry with given key */ - setobj2t(L, cast(TValue *, slot), val); /* set its new value */ + luaH_finishset(L, h, key, slot, val); /* set new value */ invalidateTMcache(h); luaC_barrierback(L, obj2gco(h), val); return; From e2ea3b31c94bb3e1da27c233661cb2a16699c685 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 7 Dec 2020 11:17:30 -0300 Subject: [PATCH 0648/1145] Details (do not affect regular code) * Avoids multiple definitions of 'lua_assert' in test file. * Smaller C-stack limit in test mode. * Note in the manual about the use of false * Extra test for constant reuse. --- lauxlib.h | 10 +++++++--- ltests.h | 5 +++++ manual/manual.of | 5 +++++ testes/code.lua | 14 ++++++++++++++ 4 files changed, 31 insertions(+), 3 deletions(-) diff --git a/lauxlib.h b/lauxlib.h index df3de4f82a..65714911c1 100644 --- a/lauxlib.h +++ b/lauxlib.h @@ -160,11 +160,15 @@ LUALIB_API void (luaL_requiref) (lua_State *L, const char *modname, /* ** Internal assertions for in-house debugging */ +#if !defined(lua_assert) + #if defined LUAI_ASSERT -#include -#define lua_assert(c) assert(c) + #include + #define lua_assert(c) assert(c) #else -#define lua_assert(x) ((void)0) + #define lua_assert(c) ((void)0) +#endif + #endif diff --git a/ltests.h b/ltests.h index f8c4466f8d..cb3a0b4804 100644 --- a/ltests.h +++ b/ltests.h @@ -130,6 +130,11 @@ LUA_API void *debug_realloc (void *ud, void *block, #define LUAI_MAXSTACK 50000 +/* test mode uses more stack space */ +#undef LUAI_MAXCCALLS +#define LUAI_MAXCCALLS 180 + + /* force Lua to use its own implementations */ #undef lua_strx2number #undef lua_number2strx diff --git a/manual/manual.of b/manual/manual.of index 606771f40f..771bace03e 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -88,6 +88,11 @@ The type @emph{boolean} has two values, @false and @true. Both @nil and @false make a condition false; they are collectively called @def{false values}. Any other value makes a condition true. +Despite its name, +@false is frequently used as an alternative to @nil, +with the key difference that @false behaves +like a regular value in a table, +while a @nil in a table represents an absent key. The type @emph{number} represents both integer numbers and real (floating-point) numbers, diff --git a/testes/code.lua b/testes/code.lua index 1f971cd775..4e00309f7f 100644 --- a/testes/code.lua +++ b/testes/code.lua @@ -55,6 +55,20 @@ end checkKlist(foo, {3.78/4, -3.78/4, -3.79/4}) +foo = function (f, a) + f(100 * 1000) + f(100.0 * 1000) + f(-100 * 1000) + f(-100 * 1000.0) + f(100000) + f(100000.0) + f(-100000) + f(-100000.0) + end + +checkKlist(foo, {100000, 100000.0, -100000, -100000.0}) + + -- testing opcodes -- check that 'f' opcodes match '...' From 748d6d4e7a1ac247071f6354f2700d1d0ee46b24 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 8 Dec 2020 11:54:21 -0300 Subject: [PATCH 0649/1145] Review of asserts in 'ltests.c' The module 'ltests.c' must work correctly with asserts off, too. --- ltests.c | 126 ++++++++++++++++++++++++++++++------------------------- 1 file changed, 70 insertions(+), 56 deletions(-) diff --git a/ltests.c b/ltests.c index 2020131ff9..6920dd69f7 100644 --- a/ltests.c +++ b/ltests.c @@ -272,11 +272,15 @@ void *debug_realloc (void *ud, void *b, size_t oldsize, size_t size) { /* -** {====================================================== -** Functions to check memory consistency -** ======================================================= +** {===================================================================== +** Functions to check memory consistency. +** Most of these checks are done through asserts, so this code does +** not make sense with asserts off. For this reason, it uses 'assert' +** directly, instead of 'lua_assert'. +** ====================================================================== */ +#include /* ** Check GC invariants. For incremental mode, a black object cannot @@ -330,13 +334,23 @@ static int testobjref (global_State *g, GCObject *f, GCObject *t) { return r1; } -#define checkobjref(g,f,t) \ - { if (t) lua_longassert(testobjref(g,f,obj2gco(t))); } + +static void checkobjref (global_State *g, GCObject *f, GCObject *t) { + assert(testobjref(g, f, t)); +} + + +/* +** Version where 't' can be NULL. In that case, it should not apply the +** macro 'obj2gco' over the object. ('t' may have several types, so this +** definition must be a macro.) Most checks need this version, because +** the check may run while an object is still being created. +*/ +#define checkobjrefN(g,f,t) { if (t) checkobjref(g,f,obj2gco(t)); } static void checkvalref (global_State *g, GCObject *f, const TValue *t) { - lua_assert(!iscollectable(t) || - (righttt(t) && testobjref(g, f, gcvalue(t)))); + assert(!iscollectable(t) || (righttt(t) && testobjref(g, f, gcvalue(t)))); } @@ -345,14 +359,14 @@ static void checktable (global_State *g, Table *h) { unsigned int asize = luaH_realasize(h); Node *n, *limit = gnode(h, sizenode(h)); GCObject *hgc = obj2gco(h); - checkobjref(g, hgc, h->metatable); + checkobjrefN(g, hgc, h->metatable); for (i = 0; i < asize; i++) checkvalref(g, hgc, &h->array[i]); for (n = gnode(h, 0); n < limit; n++) { if (!isempty(gval(n))) { TValue k; getnodekey(g->mainthread, &k, n); - lua_assert(!keyisnil(n)); + assert(!keyisnil(n)); checkvalref(g, hgc, &k); checkvalref(g, hgc, gval(n)); } @@ -363,30 +377,26 @@ static void checktable (global_State *g, Table *h) { static void checkudata (global_State *g, Udata *u) { int i; GCObject *hgc = obj2gco(u); - checkobjref(g, hgc, u->metatable); + checkobjrefN(g, hgc, u->metatable); for (i = 0; i < u->nuvalue; i++) checkvalref(g, hgc, &u->uv[i].uv); } -/* -** All marks are conditional because a GC may happen while the -** prototype is still being created -*/ static void checkproto (global_State *g, Proto *f) { int i; GCObject *fgc = obj2gco(f); - checkobjref(g, fgc, f->source); + checkobjrefN(g, fgc, f->source); for (i=0; isizek; i++) { - if (ttisstring(f->k + i)) - checkobjref(g, fgc, tsvalue(f->k + i)); + if (iscollectable(f->k + i)) + checkobjref(g, fgc, gcvalue(f->k + i)); } for (i=0; isizeupvalues; i++) - checkobjref(g, fgc, f->upvalues[i].name); + checkobjrefN(g, fgc, f->upvalues[i].name); for (i=0; isizep; i++) - checkobjref(g, fgc, f->p[i]); + checkobjrefN(g, fgc, f->p[i]); for (i=0; isizelocvars; i++) - checkobjref(g, fgc, f->locvars[i].varname); + checkobjrefN(g, fgc, f->locvars[i].varname); } @@ -401,11 +411,11 @@ static void checkCclosure (global_State *g, CClosure *cl) { static void checkLclosure (global_State *g, LClosure *cl) { GCObject *clgc = obj2gco(cl); int i; - checkobjref(g, clgc, cl->p); + checkobjrefN(g, clgc, cl->p); for (i=0; inupvalues; i++) { UpVal *uv = cl->upvals[i]; if (uv) { - checkobjref(g, clgc, uv); + checkobjrefN(g, clgc, uv); if (!upisopen(uv)) checkvalref(g, obj2gco(uv), uv->v); } @@ -428,17 +438,17 @@ static void checkstack (global_State *g, lua_State *L1) { StkId o; CallInfo *ci; UpVal *uv; - lua_assert(!isdead(g, L1)); + assert(!isdead(g, L1)); if (L1->stack == NULL) { /* incomplete thread? */ - lua_assert(L1->openupval == NULL && L1->ci == NULL); + assert(L1->openupval == NULL && L1->ci == NULL); return; } for (uv = L1->openupval; uv != NULL; uv = uv->u.open.next) - lua_assert(upisopen(uv)); /* must be open */ - lua_assert(L1->top <= L1->stack_last); + assert(upisopen(uv)); /* must be open */ + assert(L1->top <= L1->stack_last); for (ci = L1->ci; ci != NULL; ci = ci->previous) { - lua_assert(ci->top <= L1->stack_last); - lua_assert(lua_checkpc(ci)); + assert(ci->top <= L1->stack_last); + assert(lua_checkpc(ci)); } for (o = L1->stack; o < L1->stack_last; o++) checkliveness(L1, s2v(o)); /* entire stack must have valid values */ @@ -477,10 +487,10 @@ static void checkrefs (global_State *g, GCObject *o) { } case LUA_VSHRSTR: case LUA_VLNGSTR: { - lua_assert(!isgray(o)); /* strings are never gray */ + assert(!isgray(o)); /* strings are never gray */ break; } - default: lua_assert(0); + default: assert(0); } } @@ -499,14 +509,14 @@ static void checkrefs (global_State *g, GCObject *o) { static void checkobject (global_State *g, GCObject *o, int maybedead, int listage) { if (isdead(g, o)) - lua_assert(maybedead); + assert(maybedead); else { - lua_assert(g->gcstate != GCSpause || iswhite(o)); + assert(g->gcstate != GCSpause || iswhite(o)); if (g->gckind == KGC_GEN) { /* generational mode? */ - lua_assert(getage(o) >= listage); - lua_assert(!iswhite(o) || !isold(o)); + assert(getage(o) >= listage); + assert(!iswhite(o) || !isold(o)); if (isold(o)) { - lua_assert(isblack(o) || + assert(isblack(o) || getage(o) == G_TOUCHED1 || getage(o) == G_OLD0 || o->tt == LUA_VTHREAD || @@ -522,8 +532,8 @@ static lu_mem checkgraylist (global_State *g, GCObject *o) { int total = 0; /* count number of elements in the list */ ((void)g); /* better to keep it available if we need to print an object */ while (o) { - lua_assert(!!isgray(o) ^ (getage(o) == G_TOUCHED2)); - lua_assert(!testbit(o->marked, TESTBIT)); + assert(!!isgray(o) ^ (getage(o) == G_TOUCHED2)); + assert(!testbit(o->marked, TESTBIT)); if (keepinvariant(g)) l_setbit(o->marked, TESTBIT); /* mark that object is in a gray list */ total++; @@ -534,10 +544,10 @@ static lu_mem checkgraylist (global_State *g, GCObject *o) { case LUA_VTHREAD: o = gco2th(o)->gclist; break; case LUA_VPROTO: o = gco2p(o)->gclist; break; case LUA_VUSERDATA: - lua_assert(gco2u(o)->nuvalue > 0); + assert(gco2u(o)->nuvalue > 0); o = gco2u(o)->gclist; break; - default: lua_assert(0); /* other objects cannot be in a gray list */ + default: assert(0); /* other objects cannot be in a gray list */ } } return total; @@ -569,13 +579,13 @@ static void incifingray (global_State *g, GCObject *o, lu_mem *count) { return; /* gray lists not being kept in these phases */ if (o->tt == LUA_VUPVAL) { /* only open upvalues can be gray */ - lua_assert(!isgray(o) || upisopen(gco2upv(o))); + assert(!isgray(o) || upisopen(gco2upv(o))); return; /* upvalues are never in gray lists */ } /* these are the ones that must be in gray lists */ if (isgray(o) || getage(o) == G_TOUCHED2) { (*count)++; - lua_assert(testbit(o->marked, TESTBIT)); + assert(testbit(o->marked, TESTBIT)); resetbit(o->marked, TESTBIT); /* prepare for next cycle */ } } @@ -588,22 +598,22 @@ static lu_mem checklist (global_State *g, int maybedead, int tof, for (o = newl; o != survival; o = o->next) { checkobject(g, o, maybedead, G_NEW); incifingray(g, o, &total); - lua_assert(!tof == !tofinalize(o)); + assert(!tof == !tofinalize(o)); } for (o = survival; o != old; o = o->next) { checkobject(g, o, 0, G_SURVIVAL); incifingray(g, o, &total); - lua_assert(!tof == !tofinalize(o)); + assert(!tof == !tofinalize(o)); } for (o = old; o != reallyold; o = o->next) { checkobject(g, o, 0, G_OLD1); incifingray(g, o, &total); - lua_assert(!tof == !tofinalize(o)); + assert(!tof == !tofinalize(o)); } for (o = reallyold; o != NULL; o = o->next) { checkobject(g, o, 0, G_OLD); incifingray(g, o, &total); - lua_assert(!tof == !tofinalize(o)); + assert(!tof == !tofinalize(o)); } return total; } @@ -616,16 +626,16 @@ int lua_checkmemory (lua_State *L) { lu_mem totalin; /* total of objects that are in gray lists */ lu_mem totalshould; /* total of objects that should be in gray lists */ if (keepinvariant(g)) { - lua_assert(!iswhite(g->mainthread)); - lua_assert(!iswhite(gcvalue(&g->l_registry))); + assert(!iswhite(g->mainthread)); + assert(!iswhite(gcvalue(&g->l_registry))); } - lua_assert(!isdead(g, gcvalue(&g->l_registry))); - lua_assert(g->sweepgc == NULL || issweepphase(g)); + assert(!isdead(g, gcvalue(&g->l_registry))); + assert(g->sweepgc == NULL || issweepphase(g)); totalin = checkgrays(g); /* check 'fixedgc' list */ for (o = g->fixedgc; o != NULL; o = o->next) { - lua_assert(o->tt == LUA_VSHRSTR && isgray(o) && getage(o) == G_OLD); + assert(o->tt == LUA_VSHRSTR && isgray(o) && getage(o) == G_OLD); } /* check 'allgc' list */ @@ -641,11 +651,11 @@ int lua_checkmemory (lua_State *L) { for (o = g->tobefnz; o != NULL; o = o->next) { checkobject(g, o, 0, G_NEW); incifingray(g, o, &totalshould); - lua_assert(tofinalize(o)); - lua_assert(o->tt == LUA_VUSERDATA || o->tt == LUA_VTABLE); + assert(tofinalize(o)); + assert(o->tt == LUA_VUSERDATA || o->tt == LUA_VTABLE); } if (keepinvariant(g)) - lua_assert(totalin == totalshould); + assert(totalin == totalshould); return 0; } @@ -1042,6 +1052,7 @@ static int tref (lua_State *L) { luaL_checkany(L, 1); lua_pushvalue(L, 1); lua_pushinteger(L, luaL_ref(L, LUA_REGISTRYINDEX)); + (void)level; /* to avoid warnings */ lua_assert(lua_gettop(L) == level+1); /* +1 for result */ return 1; } @@ -1049,6 +1060,7 @@ static int tref (lua_State *L) { static int getref (lua_State *L) { int level = lua_gettop(L); lua_rawgeti(L, LUA_REGISTRYINDEX, luaL_checkinteger(L, 1)); + (void)level; /* to avoid warnings */ lua_assert(lua_gettop(L) == level+1); return 1; } @@ -1056,6 +1068,7 @@ static int getref (lua_State *L) { static int unref (lua_State *L) { int level = lua_gettop(L); luaL_unref(L, LUA_REGISTRYINDEX, cast_int(luaL_checkinteger(L, 1))); + (void)level; /* to avoid warnings */ lua_assert(lua_gettop(L) == level); return 0; } @@ -1724,6 +1737,7 @@ static struct X { int x; } x; else if EQ("tostring") { const char *s = lua_tostring(L1, getindex); const char *s1 = lua_pushstring(L1, s); + (void)s1; /* to avoid warnings */ lua_longassert((s == NULL && s1 == NULL) || strcmp(s, s1) == 0); } else if EQ("type") { @@ -1937,15 +1951,15 @@ static void checkfinalmem (void) { int luaB_opentests (lua_State *L) { void *ud; + lua_Alloc f = lua_getallocf(L, &ud); lua_atpanic(L, &tpanic); lua_setwarnf(L, &warnf, L); lua_pushboolean(L, 0); lua_setglobal(L, "_WARN"); /* _WARN = false */ regcodes(L); atexit(checkfinalmem); - lua_assert(lua_getallocf(L, &ud) == debug_realloc); - lua_assert(ud == cast_voidp(&l_memcontrol)); - lua_setallocf(L, lua_getallocf(L, NULL), ud); + lua_assert(f == debug_realloc && ud == cast_voidp(&l_memcontrol)); + lua_setallocf(L, f, ud); /* exercise this function */ luaL_newlib(L, tests_funcs); return 1; } From e1ceea56740ea119e4ead68c4389407024da523d Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 16 Dec 2020 11:02:40 -0300 Subject: [PATCH 0650/1145] Cleaner definition for macro 'ttisclosure' --- lobject.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lobject.h b/lobject.h index 1cc8e757bf..470b17d5f5 100644 --- a/lobject.h +++ b/lobject.h @@ -570,10 +570,11 @@ typedef struct Proto { #define LUA_VCCL makevariant(LUA_TFUNCTION, 2) /* C closure */ #define ttisfunction(o) checktype(o, LUA_TFUNCTION) -#define ttisclosure(o) ((rawtt(o) & 0x1F) == LUA_VLCL) #define ttisLclosure(o) checktag((o), ctb(LUA_VLCL)) #define ttislcf(o) checktag((o), LUA_VLCF) #define ttisCclosure(o) checktag((o), ctb(LUA_VCCL)) +#define ttisclosure(o) (ttisLclosure(o) || ttisCclosure(o)) + #define isLfunction(o) ttisLclosure(o) From b17178b27a55bd5eeb51538bec935972fd58f1ea Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 16 Dec 2020 11:23:51 -0300 Subject: [PATCH 0651/1145] Cleaner handling of floats in pack/unpack --- lstrlib.c | 70 ++++++++++++++++++++++++++++++++----------------------- 1 file changed, 41 insertions(+), 29 deletions(-) diff --git a/lstrlib.c b/lstrlib.c index 940a14ca53..c7242ea4c5 100644 --- a/lstrlib.c +++ b/lstrlib.c @@ -1358,16 +1358,6 @@ struct cD { #define MAXALIGN (offsetof(struct cD, u)) -/* -** Union for serializing floats -*/ -typedef union Ftypes { - float f; - double d; - lua_Number n; -} Ftypes; - - /* ** information to pack/unpack stuff */ @@ -1384,7 +1374,9 @@ typedef struct Header { typedef enum KOption { Kint, /* signed integers */ Kuint, /* unsigned integers */ - Kfloat, /* floating-point numbers */ + Kfloat, /* single-precision floating-point numbers */ + Knumber, /* Lua "native" floating-point numbers */ + Kdouble, /* double-precision floating-point numbers */ Kchar, /* fixed-length strings */ Kstring, /* strings with prefixed length */ Kzstr, /* zero-terminated strings */ @@ -1453,8 +1445,8 @@ static KOption getoption (Header *h, const char **fmt, int *size) { case 'J': *size = sizeof(lua_Integer); return Kuint; case 'T': *size = sizeof(size_t); return Kuint; case 'f': *size = sizeof(float); return Kfloat; - case 'd': *size = sizeof(double); return Kfloat; - case 'n': *size = sizeof(lua_Number); return Kfloat; + case 'n': *size = sizeof(lua_Number); return Knumber; + case 'd': *size = sizeof(double); return Kdouble; case 'i': *size = getnumlimit(h, fmt, sizeof(int)); return Kint; case 'I': *size = getnumlimit(h, fmt, sizeof(int)); return Kuint; case 's': *size = getnumlimit(h, fmt, sizeof(size_t)); return Kstring; @@ -1580,15 +1572,27 @@ static int str_pack (lua_State *L) { packint(&b, (lua_Unsigned)n, h.islittle, size, 0); break; } - case Kfloat: { /* floating-point options */ - Ftypes u; - char *buff = luaL_prepbuffsize(&b, size); - lua_Number n = luaL_checknumber(L, arg); /* get argument */ - if (size == sizeof(u.f)) u.f = (float)n; /* copy it into 'u' */ - else if (size == sizeof(u.d)) u.d = (double)n; - else u.n = n; - /* move 'u' to final result, correcting endianness if needed */ - copywithendian(buff, (char *)&u, size, h.islittle); + case Kfloat: { /* C float */ + float f = (float)luaL_checknumber(L, arg); /* get argument */ + char *buff = luaL_prepbuffsize(&b, sizeof(f)); + /* move 'f' to final result, correcting endianness if needed */ + copywithendian(buff, (char *)&f, sizeof(f), h.islittle); + luaL_addsize(&b, size); + break; + } + case Knumber: { /* Lua float */ + lua_Number f = luaL_checknumber(L, arg); /* get argument */ + char *buff = luaL_prepbuffsize(&b, sizeof(f)); + /* move 'f' to final result, correcting endianness if needed */ + copywithendian(buff, (char *)&f, sizeof(f), h.islittle); + luaL_addsize(&b, size); + break; + } + case Kdouble: { /* C double */ + double f = (double)luaL_checknumber(L, arg); /* get argument */ + char *buff = luaL_prepbuffsize(&b, sizeof(f)); + /* move 'f' to final result, correcting endianness if needed */ + copywithendian(buff, (char *)&f, sizeof(f), h.islittle); luaL_addsize(&b, size); break; } @@ -1714,13 +1718,21 @@ static int str_unpack (lua_State *L) { break; } case Kfloat: { - Ftypes u; - lua_Number num; - copywithendian((char *)&u, data + pos, size, h.islittle); - if (size == sizeof(u.f)) num = (lua_Number)u.f; - else if (size == sizeof(u.d)) num = (lua_Number)u.d; - else num = u.n; - lua_pushnumber(L, num); + float f; + copywithendian((char *)&f, data + pos, sizeof(f), h.islittle); + lua_pushnumber(L, (lua_Number)f); + break; + } + case Knumber: { + lua_Number f; + copywithendian((char *)&f, data + pos, sizeof(f), h.islittle); + lua_pushnumber(L, f); + break; + } + case Kdouble: { + double f; + copywithendian((char *)&f, data + pos, sizeof(f), h.islittle); + lua_pushnumber(L, (lua_Number)f); break; } case Kchar: { From 409256b7849ec5ab3296cb0ab9eba3d65955d5ea Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 18 Dec 2020 11:22:42 -0300 Subject: [PATCH 0652/1145] 'coroutine.close'/'lua_resetthread' report original errors Besides errors in closing methods, 'coroutine.close' and 'lua_resetthread' also consider the original error that stopped the thread, if any. --- lstate.c | 8 +++++--- manual/manual.of | 10 +++++++--- testes/coroutine.lua | 10 +++++++++- testes/cstack.lua | 4 ++++ testes/locals.lua | 23 +++++++++++++++-------- 5 files changed, 40 insertions(+), 15 deletions(-) diff --git a/lstate.c b/lstate.c index 1596b51cf0..96187c668c 100644 --- a/lstate.c +++ b/lstate.c @@ -323,14 +323,16 @@ void luaE_freethread (lua_State *L, lua_State *L1) { int lua_resetthread (lua_State *L) { CallInfo *ci; - int status; + int status = L->status; lua_lock(L); L->ci = ci = &L->base_ci; /* unwind CallInfo list */ setnilvalue(s2v(L->stack)); /* 'function' entry for basic 'ci' */ ci->func = L->stack; ci->callstatus = CIST_C; - status = luaF_close(L, L->stack, CLOSEPROTECT); - if (status != CLOSEPROTECT) /* real errors? */ + if (status == LUA_OK || status == LUA_YIELD) + status = CLOSEPROTECT; /* run closing methods in protected mode */ + status = luaF_close(L, L->stack, status); + if (status != CLOSEPROTECT) /* errors? */ luaD_seterrorobj(L, status, L->stack + 1); else { status = LUA_OK; diff --git a/manual/manual.of b/manual/manual.of index 771bace03e..164e359a93 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -4098,10 +4098,12 @@ and then pops the top element. Resets a thread, cleaning its call stack and closing all pending to-be-closed variables. Returns a status code: -@Lid{LUA_OK} for no errors in closing methods, +@Lid{LUA_OK} for no errors in the thread +(either the original error that stopped the thread or +errors in closing methods), or an error status otherwise. In case of error, -leaves the error object on the top of the stack, +leaves the error object on the top of the stack. } @@ -6577,7 +6579,9 @@ that is, closes all its pending to-be-closed variables and puts the coroutine in a dead state. The given coroutine must be dead or suspended. -In case of error closing some variable, +In case of error +(either the original error that stopped the coroutine or +errors in closing methods), returns @false plus the error object; otherwise returns @true. diff --git a/testes/coroutine.lua b/testes/coroutine.lua index 5b9271510e..aaf565fb16 100644 --- a/testes/coroutine.lua +++ b/testes/coroutine.lua @@ -134,7 +134,8 @@ do local co = coroutine.create(print) assert(coroutine.resume(co, "testing 'coroutine.close'")) assert(coroutine.status(co) == "dead") - assert(coroutine.close(co)) + local st, msg = coroutine.close(co) + assert(st and msg == nil) -- cannot close the running coroutine local st, msg = pcall(coroutine.close, coroutine.running()) @@ -151,6 +152,13 @@ do -- to-be-closed variables in coroutines local X + -- closing a coroutine after an error + local co = coroutine.create(error) + local st, msg = coroutine.resume(co, 100) + assert(not st and msg == 100) + st, msg = coroutine.close(co) + assert(not st and msg == 100) + co = coroutine.create(function () local x = func2close(function (self, err) assert(err == nil); X = false diff --git a/testes/cstack.lua b/testes/cstack.lua index 8ac48e8940..7bd5506342 100644 --- a/testes/cstack.lua +++ b/testes/cstack.lua @@ -135,14 +135,18 @@ if T then local topB, sizeB -- top and size Before overflow local topA, sizeA -- top and size After overflow topB, sizeB = T.stacklevel() + collectgarbage("stop") -- __gc should not be called with a full stack xpcall(f, err) + collectgarbage("restart") topA, sizeA = T.stacklevel() -- sizes should be comparable assert(topA == topB and sizeA < sizeB * 2) print(string.format("maximum stack size: %d", stack1)) LIM = N -- will stop recursion at maximum level N = 0 -- to count again + collectgarbage("stop") -- __gc should not be called with a full stack f() + collectgarbage("restart") print"+" end diff --git a/testes/locals.lua b/testes/locals.lua index df44b86f2f..e2f6f35c96 100644 --- a/testes/locals.lua +++ b/testes/locals.lua @@ -362,7 +362,7 @@ end local function checkwarn (msg) if T then - assert(string.find(_WARN, msg)) + assert(_WARN and string.find(_WARN, msg)) _WARN = false -- reset variable to check next warning end end @@ -670,10 +670,13 @@ do -- error in a wrapped coroutine raising errors when closing a variable local x = 0 local co = coroutine.wrap(function () - local xx = func2close(function () x = x + 1; error("@YYY") end) + local xx = func2close(function () + x = x + 1; + checkwarn("@XXX"); error("@YYY") + end) local xv = func2close(function () x = x + 1; error("@XXX") end) - coroutine.yield(100) - error(200) + coroutine.yield(100) + error(200) end) assert(co() == 100); assert(x == 0) local st, msg = pcall(co); assert(x == 2) @@ -683,10 +686,14 @@ do local x = 0 local y = 0 co = coroutine.wrap(function () - local xx = func2close(function () y = y + 1; error("YYY") end) - local xv = func2close(function () x = x + 1; error("XXX") end) - coroutine.yield(100) - return 200 + local xx = func2close(function () + y = y + 1; checkwarn("XXX"); error("YYY") + end) + local xv = func2close(function () + x = x + 1; error("XXX") + end) + coroutine.yield(100) + return 200 end) assert(co() == 100); assert(x == 0) local st, msg = pcall(co) From f9d29b0c442447ebe429bcaad1e2b4bf13c5dc93 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 21 Dec 2020 15:21:45 -0300 Subject: [PATCH 0653/1145] Upvalues removed from 'openupval' before being closed Undo commit c220b0a5d0: '__close' is not called again in case of errors. (Upvalue is removed from the list before the call.) The common error that justified that change was C stack overflows, which are much rarer with the stackless implementation. --- lfunc.c | 30 ++++++++++++++++++++++-------- manual/manual.of | 1 - testes/locals.lua | 26 +++++++++----------------- 3 files changed, 31 insertions(+), 26 deletions(-) diff --git a/lfunc.c b/lfunc.c index c4360f0950..6608592b60 100644 --- a/lfunc.c +++ b/lfunc.c @@ -220,24 +220,38 @@ void luaF_unlinkupval (UpVal *uv) { } +/* +** Close all upvalues up to the given stack level. 'status' indicates +** how/why the function was called: +** - LUA_OK: regular code exiting the scope of a variable; may raise +** an error due to errors in __close metamethods; +** - CLOSEPROTECT: finishing a thread; run all metamethods in protected +** mode; +** - NOCLOSINGMETH: close upvalues without running __close metamethods; +** - other values: error status from previous errors, to be propagated. +** +** Returns the resulting status, either the original status or an error +** in a closing method. +*/ int luaF_close (lua_State *L, StkId level, int status) { UpVal *uv; - while ((uv = L->openupval) != NULL && uplevel(uv) >= level) { + StkId upl; /* stack index pointed by 'uv' */ + while ((uv = L->openupval) != NULL && (upl = uplevel(uv)) >= level) { TValue *slot = &uv->u.value; /* new position for value */ lua_assert(uplevel(uv) < L->top); - if (uv->tbc && status != NOCLOSINGMETH) { - /* must run closing method, which may change the stack */ - ptrdiff_t levelrel = savestack(L, level); - status = callclosemth(L, uplevel(uv), status); - level = restorestack(L, levelrel); - } - luaF_unlinkupval(uv); + luaF_unlinkupval(uv); /* remove upvalue from 'openupval' list */ setobj(L, slot, uv->v); /* move value to upvalue slot */ uv->v = slot; /* now current value lives here */ if (!iswhite(uv)) { /* neither white nor dead? */ nw2black(uv); /* closed upvalues cannot be gray */ luaC_barrier(L, uv, slot); } + if (uv->tbc && status != NOCLOSINGMETH) { + /* must run closing method, which may change the stack */ + ptrdiff_t levelrel = savestack(L, level); + status = callclosemth(L, upl, status); + level = restorestack(L, levelrel); + } } return status; } diff --git a/manual/manual.of b/manual/manual.of index 164e359a93..5d0c35cfbd 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -1630,7 +1630,6 @@ they are closed in the reverse order that they were declared. If there is any error while running a closing method, that error is handled like an error in the regular code where the variable was defined. -However, Lua may call the method one more time. After an error, the other pending closing methods will still be called. diff --git a/testes/locals.lua b/testes/locals.lua index e2f6f35c96..d32a9a3e80 100644 --- a/testes/locals.lua +++ b/testes/locals.lua @@ -392,21 +392,13 @@ do print("testing errors in __close") local y = func2close(function (self, msg) - assert(string.find(msg, "@z")) -- first error in 'z' - checkwarn("@z") -- second error in 'z' generated a warning + assert(string.find(msg, "@z")) -- error in 'z' error("@y") end) - local first = true local z = - -- 'z' close is called twice func2close(function (self, msg) - if first then - assert(msg == nil) - first = false - else - assert(string.find(msg, "@z")) -- own error - end + assert(msg == nil) error("@z") end) @@ -468,8 +460,8 @@ do print("testing errors in __close") local function foo (...) do local x1 = - func2close(function () - checkwarn("@X") + func2close(function (self, msg) + assert(string.find(msg, "@X")) error("@Y") end) @@ -494,8 +486,6 @@ do print("testing errors in __close") local st, msg = xpcall(foo, debug.traceback) assert(string.match(msg, "^[^ ]* @x123")) assert(string.find(msg, "in metamethod 'close'")) - checkwarn("@x123") -- from second call to close 'x123' - endwarn() end @@ -686,8 +676,10 @@ do local x = 0 local y = 0 co = coroutine.wrap(function () - local xx = func2close(function () - y = y + 1; checkwarn("XXX"); error("YYY") + local xx = func2close(function (_, err) + y = y + 1; + assert(string.find(err, "XXX")) + error("YYY") end) local xv = func2close(function () x = x + 1; error("XXX") @@ -697,7 +689,7 @@ do end) assert(co() == 100); assert(x == 0) local st, msg = pcall(co) - assert(x == 2 and y == 1) -- first close is called twice + assert(x == 1 and y == 1) -- should get first error raised assert(not st and string.find(msg, "%w+%.%w+:%d+: XXX")) checkwarn("YYY") From 0ceada8da92135717d31a3954b5b89a954f9e71a Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 22 Dec 2020 10:54:25 -0300 Subject: [PATCH 0654/1145] Report last error in closing methods When there are multiple errors around closing methods, report the last error instead of the original. --- lcorolib.c | 7 +++- lfunc.c | 10 ++--- manual/manual.of | 5 --- testes/coroutine.lua | 15 ++----- testes/locals.lua | 99 +++++++++++--------------------------------- 5 files changed, 35 insertions(+), 101 deletions(-) diff --git a/lcorolib.c b/lcorolib.c index c165031d28..ed7c58b2b2 100644 --- a/lcorolib.c +++ b/lcorolib.c @@ -75,8 +75,11 @@ static int luaB_auxwrap (lua_State *L) { int r = auxresume(L, co, lua_gettop(L)); if (r < 0) { /* error? */ int stat = lua_status(co); - if (stat != LUA_OK && stat != LUA_YIELD) /* error in the coroutine? */ - lua_resetthread(co); /* close its tbc variables */ + if (stat != LUA_OK && stat != LUA_YIELD) { /* error in the coroutine? */ + stat = lua_resetthread(co); /* close its tbc variables */ + lua_assert(stat != LUA_OK); + lua_xmove(co, L, 1); /* copy error message */ + } if (stat != LUA_ERRMEM && /* not a memory error and ... */ lua_type(L, -1) == LUA_TSTRING) { /* ... error object is a string? */ luaL_where(L, 1); /* add extra info, if available */ diff --git a/lfunc.c b/lfunc.c index 6608592b60..bfbf270bb8 100644 --- a/lfunc.c +++ b/lfunc.c @@ -162,14 +162,10 @@ static int callclosemth (lua_State *L, StkId level, int status) { luaD_seterrorobj(L, status, level); /* set error message */ if (prepclosingmethod(L, uv, s2v(level))) { /* something to call? */ int newstatus = luaD_pcall(L, callclose, NULL, oldtop, 0); - if (newstatus != LUA_OK && status == CLOSEPROTECT) /* first error? */ - status = newstatus; /* this will be the new error */ - else { - if (newstatus != LUA_OK) /* suppressed error? */ - luaE_warnerror(L, "__close metamethod"); - /* leave original error (or nil) on top */ + if (newstatus != LUA_OK) /* new error? */ + status = newstatus; /* this will be the error now */ + else /* leave original error (or nil) on top */ L->top = restorestack(L, oldtop); - } } /* else no metamethod; ignore this case and keep original error */ } diff --git a/manual/manual.of b/manual/manual.of index 5d0c35cfbd..c538525847 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -1630,13 +1630,8 @@ they are closed in the reverse order that they were declared. If there is any error while running a closing method, that error is handled like an error in the regular code where the variable was defined. - After an error, the other pending closing methods will still be called. -Errors in these methods -interrupt the respective method and generate a warning, -but are otherwise ignored; -the error reported is only the original one. If a coroutine yields and is never resumed again, some variables may never go out of scope, diff --git a/testes/coroutine.lua b/testes/coroutine.lua index aaf565fb16..0a970e985f 100644 --- a/testes/coroutine.lua +++ b/testes/coroutine.lua @@ -172,13 +172,12 @@ do assert(not X and coroutine.status(co) == "dead") -- error closing a coroutine - warn("@on") local x = 0 co = coroutine.create(function() local y = func2close(function (self,err) - if (err ~= 111) then os.exit(false) end -- should not happen + assert(err == 111) x = 200 - error("200") + error(200) end) local x = func2close(function (self, err) assert(err == nil); error(111) @@ -187,16 +186,8 @@ do end) coroutine.resume(co) assert(x == 0) - -- with test library, use 'store' mode to check warnings - warn(not T and "@off" or "@store") local st, msg = coroutine.close(co) - if not T then - warn("@on") - else -- test library - assert(string.find(_WARN, "200")); _WARN = false - warn("@normal") - end - assert(st == false and coroutine.status(co) == "dead" and msg == 111) + assert(st == false and coroutine.status(co) == "dead" and msg == 200) assert(x == 200) end diff --git a/testes/locals.lua b/testes/locals.lua index d32a9a3e80..84e0b03a09 100644 --- a/testes/locals.lua +++ b/testes/locals.lua @@ -232,7 +232,11 @@ end do local X = false - local x, closescope = func2close(function () stack(10); X = true end, 100) + local x, closescope = func2close(function (_, msg) + stack(10); + assert(msg == nil) + X = true + end, 100) assert(x == 100); x = 101; -- 'x' is not read-only -- closing functions do not corrupt returning values @@ -246,10 +250,11 @@ do X = false foo = function (x) - local _ = func2close(function () + local _ = func2close(function (_, msg) -- without errors, enclosing function should be still active when -- __close is called assert(debug.getinfo(2).name == "foo") + assert(msg == nil) end) local _ = closescope local y = 15 @@ -328,64 +333,20 @@ do end --- auxiliary functions for testing warnings in '__close' -local function prepwarn () - if not T then -- no test library? - warn("@off") -- do not show (lots of) warnings - else - warn("@store") -- to test the warnings - end -end - - -local function endwarn () - if not T then - warn("@on") -- back to normal - else - assert(_WARN == false) - warn("@normal") - end -end - - --- errors inside __close can generate a warning instead of an --- error. This new 'assert' force them to appear. -local function assert(cond, msg) - if not cond then - local line = debug.getinfo(2).currentline or "?" - msg = string.format("assertion failed! line %d (%s)\n", line, msg or "") - io.stderr:write(msg) - os.exit(1) - end -end - - -local function checkwarn (msg) - if T then - assert(_WARN and string.find(_WARN, msg)) - _WARN = false -- reset variable to check next warning - end -end - -warn("@on") - do print("testing errors in __close") - prepwarn() - -- original error is in __close local function foo () local x = func2close(function (self, msg) - assert(string.find(msg, "@z")) + assert(string.find(msg, "@y")) error("@x") end) local x1 = func2close(function (self, msg) - checkwarn("@y") - assert(string.find(msg, "@z")) + assert(string.find(msg, "@y")) end) local gc = func2close(function () collectgarbage() end) @@ -406,8 +367,7 @@ do print("testing errors in __close") end local stat, msg = pcall(foo, false) - assert(string.find(msg, "@z")) - checkwarn("@x") + assert(string.find(msg, "@x")) -- original error not in __close @@ -418,14 +378,13 @@ do print("testing errors in __close") -- after error, 'foo' was discarded, so caller now -- must be 'pcall' assert(debug.getinfo(2).name == "pcall") - assert(msg == 4) + assert(string.find(msg, "@x1")) end) local x1 = func2close(function (self, msg) assert(debug.getinfo(2).name == "pcall") - checkwarn("@y") - assert(msg == 4) + assert(string.find(msg, "@y")) error("@x1") end) @@ -434,8 +393,7 @@ do print("testing errors in __close") local y = func2close(function (self, msg) assert(debug.getinfo(2).name == "pcall") - assert(msg == 4) -- error in body - checkwarn("@z") + assert(string.find(msg, "@z")) error("@y") end) @@ -453,8 +411,7 @@ do print("testing errors in __close") end local stat, msg = pcall(foo, true) - assert(msg == 4) - checkwarn("@x1") -- last error + assert(string.find(msg, "@x1")) -- error leaving a block local function foo (...) @@ -466,7 +423,8 @@ do print("testing errors in __close") end) local x123 = - func2close(function () + func2close(function (_, msg) + assert(msg == nil) error("@X") end) end @@ -474,9 +432,7 @@ do print("testing errors in __close") end local st, msg = xpcall(foo, debug.traceback) - assert(string.match(msg, "^[^ ]* @X")) - assert(string.find(msg, "in metamethod 'close'")) - checkwarn("@Y") + assert(string.match(msg, "^[^ ]* @Y")) -- error in toclose in vararg function local function foo (...) @@ -486,7 +442,6 @@ do print("testing errors in __close") local st, msg = xpcall(foo, debug.traceback) assert(string.match(msg, "^[^ ]* @x123")) assert(string.find(msg, "in metamethod 'close'")) - endwarn() end @@ -511,8 +466,6 @@ end if rawget(_G, "T") then - warn("@off") - -- memory error inside closing function local function foo () local y = func2close(function () T.alloccount() end) @@ -527,7 +480,7 @@ if rawget(_G, "T") then -- despite memory error, 'y' will be executed and -- memory limit will be lifted local _, msg = pcall(foo) - assert(msg == 1000) + assert(msg == "not enough memory") local close = func2close(function (self, msg) T.alloccount() @@ -570,7 +523,7 @@ if rawget(_G, "T") then end local _, msg = pcall(test) - assert(msg == "not enough memory") -- reported error is the first one + assert(msg == 1000) do -- testing 'toclose' in C string buffer collectgarbage() @@ -625,7 +578,6 @@ if rawget(_G, "T") then print'+' end - warn("@on") end @@ -655,14 +607,14 @@ end do - prepwarn() -- error in a wrapped coroutine raising errors when closing a variable local x = 0 local co = coroutine.wrap(function () - local xx = func2close(function () + local xx = func2close(function (_, msg) x = x + 1; - checkwarn("@XXX"); error("@YYY") + assert(string.find(msg, "@XXX")) + error("@YYY") end) local xv = func2close(function () x = x + 1; error("@XXX") end) coroutine.yield(100) @@ -670,8 +622,7 @@ do end) assert(co() == 100); assert(x == 0) local st, msg = pcall(co); assert(x == 2) - assert(not st and msg == 200) -- should get first error raised - checkwarn("@YYY") + assert(not st and string.find(msg, "@YYY")) -- should get error raised local x = 0 local y = 0 @@ -691,10 +642,8 @@ do local st, msg = pcall(co) assert(x == 1 and y == 1) -- should get first error raised - assert(not st and string.find(msg, "%w+%.%w+:%d+: XXX")) - checkwarn("YYY") + assert(not st and string.find(msg, "%w+%.%w+:%d+: YYY")) - endwarn() end From 7af27ef59da4051914d93d8b63efac663b64765a Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 28 Dec 2020 11:40:30 -0300 Subject: [PATCH 0655/1145] Cleaner handling of errors in '__close' metamethods Instead of protecting each individual metamethod call, protect the entire call to 'luaF_close'. --- lapi.c | 2 +- ldo.c | 59 +++++++++++++++++++++++++++++++++++--------- ldo.h | 1 + lfunc.c | 75 +++++++++++++++++--------------------------------------- lfunc.h | 6 ++--- lstate.c | 10 ++++---- lvm.c | 2 +- 7 files changed, 80 insertions(+), 75 deletions(-) diff --git a/lapi.c b/lapi.c index 03e756d645..00e95a11a5 100644 --- a/lapi.c +++ b/lapi.c @@ -188,7 +188,7 @@ LUA_API void lua_settop (lua_State *L, int idx) { diff = idx + 1; /* will "subtract" index (as it is negative) */ } if (diff < 0 && hastocloseCfunc(ci->nresults)) - luaF_close(L, L->top + diff, LUA_OK); + luaF_close(L, L->top + diff, CLOSEKTOP); L->top += diff; /* correct top only after closing any upvalue */ lua_unlock(L); } diff --git a/ldo.c b/ldo.c index 4b55c31c2d..59391f7b65 100644 --- a/ldo.c +++ b/ldo.c @@ -98,11 +98,12 @@ void luaD_seterrorobj (lua_State *L, int errcode, StkId oldtop) { setsvalue2s(L, oldtop, luaS_newliteral(L, "error in error handling")); break; } - case CLOSEPROTECT: { + case LUA_OK: { /* special case only for closing upvalues */ setnilvalue(s2v(oldtop)); /* no error message */ break; } default: { + lua_assert(errcode >= LUA_ERRRUN); /* real error */ setobjs2s(L, oldtop, L->top - 1); /* error message on current top */ break; } @@ -118,7 +119,7 @@ l_noret luaD_throw (lua_State *L, int errcode) { } else { /* thread has no error handler */ global_State *g = G(L); - errcode = luaF_close(L, L->stack, errcode); /* close all upvalues */ + errcode = luaD_closeprotected(L, 0, errcode); /* close all upvalues */ L->status = cast_byte(errcode); /* mark it as dead */ if (g->mainthread->errorJmp) { /* main thread has a handler? */ setobjs2s(L, g->mainthread->top++, L->top - 1); /* copy error obj. */ @@ -409,7 +410,7 @@ static void moveresults (lua_State *L, StkId res, int nres, int wanted) { default: /* multiple results (or to-be-closed variables) */ if (hastocloseCfunc(wanted)) { /* to-be-closed variables? */ ptrdiff_t savedres = savestack(L, res); - luaF_close(L, res, LUA_OK); /* may change the stack */ + luaF_close(L, res, CLOSEKTOP); /* may change the stack */ res = restorestack(L, savedres); wanted = codeNresults(wanted); /* correct value */ if (wanted == LUA_MULTRET) @@ -636,16 +637,13 @@ static CallInfo *findpcall (lua_State *L) { ** 'luaD_pcall'. If there is no recover point, returns zero. */ static int recover (lua_State *L, int status) { - StkId oldtop; CallInfo *ci = findpcall(L); if (ci == NULL) return 0; /* no recovery point */ /* "finish" luaD_pcall */ - oldtop = restorestack(L, ci->u2.funcidx); L->ci = ci; L->allowhook = getoah(ci->callstatus); /* restore original 'allowhook' */ - status = luaF_close(L, oldtop, status); /* may change the stack */ - oldtop = restorestack(L, ci->u2.funcidx); - luaD_seterrorobj(L, status, oldtop); + status = luaD_closeprotected(L, ci->u2.funcidx, status); + luaD_seterrorobj(L, status, restorestack(L, ci->u2.funcidx)); luaD_shrinkstack(L); /* restore stack size in case of overflow */ L->errfunc = ci->u.c.old_errfunc; return 1; /* continue running the coroutine */ @@ -769,6 +767,45 @@ LUA_API int lua_yieldk (lua_State *L, int nresults, lua_KContext ctx, } +/* +** Auxiliary structure to call 'luaF_close' in protected mode. +*/ +struct CloseP { + StkId level; + int status; +}; + + +/* +** Auxiliary function to call 'luaF_close' in protected mode. +*/ +static void closepaux (lua_State *L, void *ud) { + struct CloseP *pcl = cast(struct CloseP *, ud); + luaF_close(L, pcl->level, pcl->status); +} + + +/* +** Calls 'luaF_close' in protected mode. Return the original status +** or, in case of errors, the new status. +*/ +int luaD_closeprotected (lua_State *L, ptrdiff_t level, int status) { + CallInfo *old_ci = L->ci; + lu_byte old_allowhooks = L->allowhook; + for (;;) { /* keep closing upvalues until no more errors */ + struct CloseP pcl; + pcl.level = restorestack(L, level); pcl.status = status; + status = luaD_rawrunprotected(L, &closepaux, &pcl); + if (likely(status == LUA_OK)) /* no more errors? */ + return pcl.status; + else { /* an error occurred; restore saved state and repeat */ + L->ci = old_ci; + L->allowhook = old_allowhooks; + } + } +} + + /* ** Call the C function 'func' in protected mode, restoring basic ** thread information ('allowhook', etc.) and in particular @@ -783,12 +820,10 @@ int luaD_pcall (lua_State *L, Pfunc func, void *u, L->errfunc = ef; status = luaD_rawrunprotected(L, func, u); if (unlikely(status != LUA_OK)) { /* an error occurred? */ - StkId oldtop = restorestack(L, old_top); L->ci = old_ci; L->allowhook = old_allowhooks; - status = luaF_close(L, oldtop, status); - oldtop = restorestack(L, old_top); /* previous call may change stack */ - luaD_seterrorobj(L, status, oldtop); + status = luaD_closeprotected(L, old_top, status); + luaD_seterrorobj(L, status, restorestack(L, old_top)); luaD_shrinkstack(L); /* restore stack size in case of overflow */ } L->errfunc = old_errfunc; diff --git a/ldo.h b/ldo.h index 4d30d072ed..c7721d62da 100644 --- a/ldo.h +++ b/ldo.h @@ -63,6 +63,7 @@ LUAI_FUNC CallInfo *luaD_precall (lua_State *L, StkId func, int nResults); LUAI_FUNC void luaD_call (lua_State *L, StkId func, int nResults); LUAI_FUNC void luaD_callnoyield (lua_State *L, StkId func, int nResults); LUAI_FUNC void luaD_tryfuncTM (lua_State *L, StkId func); +LUAI_FUNC int luaD_closeprotected (lua_State *L, ptrdiff_t level, int status); LUAI_FUNC int luaD_pcall (lua_State *L, Pfunc func, void *u, ptrdiff_t oldtop, ptrdiff_t ef); LUAI_FUNC void luaD_poscall (lua_State *L, CallInfo *ci, int nres); diff --git a/lfunc.c b/lfunc.c index bfbf270bb8..ae68487c29 100644 --- a/lfunc.c +++ b/lfunc.c @@ -100,12 +100,6 @@ UpVal *luaF_findupval (lua_State *L, StkId level) { } -static void callclose (lua_State *L, void *ud) { - UNUSED(ud); - luaD_callnoyield(L, L->top - 3, 0); -} - - /* ** Prepare closing method plus its arguments for object 'obj' with ** error message 'err'. (This function assumes EXTRA_STACK.) @@ -136,40 +130,25 @@ static void varerror (lua_State *L, StkId level, const char *msg) { /* -** Prepare and call a closing method. If status is OK, code is still -** inside the original protected call, and so any error will be handled -** there. Otherwise, a previous error already activated the original -** protected call, and so the call to the closing method must be -** protected here. (A status == CLOSEPROTECT behaves like a previous -** error, to also run the closing method in protected mode). -** If status is OK, the call to the closing method will be pushed -** at the top of the stack. Otherwise, values are pushed after -** the 'level' of the upvalue being closed, as everything after -** that won't be used again. +** Prepare and call a closing method. +** If status is CLOSEKTOP, the call to the closing method will be pushed +** at the top of the stack. Otherwise, values can be pushed right after +** the 'level' of the upvalue being closed, as everything after that +** won't be used again. */ -static int callclosemth (lua_State *L, StkId level, int status) { +static void callclosemth (lua_State *L, StkId level, int status) { TValue *uv = s2v(level); /* value being closed */ - if (likely(status == LUA_OK)) { - if (prepclosingmethod(L, uv, &G(L)->nilvalue)) /* something to call? */ - callclose(L, NULL); /* call closing method */ - else if (!l_isfalse(uv)) /* non-closable non-false value? */ - varerror(L, level, "attempt to close non-closable variable '%s'"); - } - else { /* must close the object in protected mode */ - ptrdiff_t oldtop; - level++; /* space for error message */ - oldtop = savestack(L, level + 1); /* top will be after that */ - luaD_seterrorobj(L, status, level); /* set error message */ - if (prepclosingmethod(L, uv, s2v(level))) { /* something to call? */ - int newstatus = luaD_pcall(L, callclose, NULL, oldtop, 0); - if (newstatus != LUA_OK) /* new error? */ - status = newstatus; /* this will be the error now */ - else /* leave original error (or nil) on top */ - L->top = restorestack(L, oldtop); - } - /* else no metamethod; ignore this case and keep original error */ + TValue *errobj; + if (status == CLOSEKTOP) + errobj = &G(L)->nilvalue; /* error object is nil */ + else { /* 'luaD_seterrorobj' will set top to level + 2 */ + errobj = s2v(level + 1); /* error object goes after 'uv' */ + luaD_seterrorobj(L, status, level + 1); /* set error object */ } - return status; + if (prepclosingmethod(L, uv, errobj)) /* something to call? */ + luaD_callnoyield(L, L->top - 3, 0); /* call method */ + else if (!l_isfalse(uv)) /* non-closable non-false value? */ + varerror(L, level, "attempt to close non-closable variable '%s'"); } @@ -201,7 +180,7 @@ void luaF_newtbcupval (lua_State *L, StkId level) { luaD_seterrorobj(L, LUA_ERRMEM, level + 1); /* save error message */ /* next call must succeed, as object is closable */ prepclosingmethod(L, s2v(level), s2v(level + 1)); - callclose(L, NULL); /* call closing method */ + luaD_callnoyield(L, L->top - 3, 0); /* call method */ luaD_throw(L, LUA_ERRMEM); /* throw memory error */ } } @@ -217,19 +196,11 @@ void luaF_unlinkupval (UpVal *uv) { /* -** Close all upvalues up to the given stack level. 'status' indicates -** how/why the function was called: -** - LUA_OK: regular code exiting the scope of a variable; may raise -** an error due to errors in __close metamethods; -** - CLOSEPROTECT: finishing a thread; run all metamethods in protected -** mode; -** - NOCLOSINGMETH: close upvalues without running __close metamethods; -** - other values: error status from previous errors, to be propagated. -** -** Returns the resulting status, either the original status or an error -** in a closing method. +** Close all upvalues up to the given stack level. A 'status' equal +** to NOCLOSINGMETH closes upvalues without running any __close +** metamethods. */ -int luaF_close (lua_State *L, StkId level, int status) { +void luaF_close (lua_State *L, StkId level, int status) { UpVal *uv; StkId upl; /* stack index pointed by 'uv' */ while ((uv = L->openupval) != NULL && (upl = uplevel(uv)) >= level) { @@ -243,13 +214,11 @@ int luaF_close (lua_State *L, StkId level, int status) { luaC_barrier(L, uv, slot); } if (uv->tbc && status != NOCLOSINGMETH) { - /* must run closing method, which may change the stack */ ptrdiff_t levelrel = savestack(L, level); - status = callclosemth(L, upl, status); + callclosemth(L, upl, status); /* may change the stack */ level = restorestack(L, levelrel); } } - return status; } diff --git a/lfunc.h b/lfunc.h index 8d6f965cfc..40de46365e 100644 --- a/lfunc.h +++ b/lfunc.h @@ -49,8 +49,8 @@ /* close upvalues without running their closing methods */ #define NOCLOSINGMETH (-1) -/* close upvalues running all closing methods in protected mode */ -#define CLOSEPROTECT (-2) +/* special status to close upvalues preserving the top of the stack */ +#define CLOSEKTOP (-2) LUAI_FUNC Proto *luaF_newproto (lua_State *L); @@ -59,7 +59,7 @@ LUAI_FUNC LClosure *luaF_newLclosure (lua_State *L, int nupvals); LUAI_FUNC void luaF_initupvals (lua_State *L, LClosure *cl); LUAI_FUNC UpVal *luaF_findupval (lua_State *L, StkId level); LUAI_FUNC void luaF_newtbcupval (lua_State *L, StkId level); -LUAI_FUNC int luaF_close (lua_State *L, StkId level, int status); +LUAI_FUNC void luaF_close (lua_State *L, StkId level, int status); LUAI_FUNC void luaF_unlinkupval (UpVal *uv); LUAI_FUNC void luaF_freeproto (lua_State *L, Proto *f); LUAI_FUNC const char *luaF_getlocalname (const Proto *func, int local_number, diff --git a/lstate.c b/lstate.c index 96187c668c..e01a7e7b4b 100644 --- a/lstate.c +++ b/lstate.c @@ -268,7 +268,7 @@ static void preinit_thread (lua_State *L, global_State *g) { static void close_state (lua_State *L) { global_State *g = G(L); - luaF_close(L, L->stack, CLOSEPROTECT); /* close all upvalues */ + luaD_closeprotected(L, 0, LUA_OK); /* close all upvalues */ luaC_freeallobjects(L); /* collect all objects */ if (ttisnil(&g->nilvalue)) /* closing a fully built state? */ luai_userstateclose(L); @@ -329,10 +329,10 @@ int lua_resetthread (lua_State *L) { setnilvalue(s2v(L->stack)); /* 'function' entry for basic 'ci' */ ci->func = L->stack; ci->callstatus = CIST_C; - if (status == LUA_OK || status == LUA_YIELD) - status = CLOSEPROTECT; /* run closing methods in protected mode */ - status = luaF_close(L, L->stack, status); - if (status != CLOSEPROTECT) /* errors? */ + if (status == LUA_YIELD) + status = LUA_OK; + status = luaD_closeprotected(L, 0, status); + if (status != LUA_OK) /* errors? */ luaD_seterrorobj(L, status, L->stack + 1); else { status = LUA_OK; diff --git a/lvm.c b/lvm.c index ccebdbe05e..a6f04606dd 100644 --- a/lvm.c +++ b/lvm.c @@ -1662,7 +1662,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { if (TESTARG_k(i)) { /* may there be open upvalues? */ if (L->top < ci->top) L->top = ci->top; - luaF_close(L, base, LUA_OK); + luaF_close(L, base, CLOSEKTOP); updatetrap(ci); updatestack(ci); } From 6188f3a654c0380db08eb40a5465ce8e71c784f5 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 28 Dec 2020 16:34:07 -0300 Subject: [PATCH 0656/1145] Reset thread before panicking Before panicking, it is simpler to reset the thread instead of closing its variables and adjust the top manually. --- ldo.c | 6 +----- lstate.c | 22 +++++++++++++--------- lstate.h | 1 + 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/ldo.c b/ldo.c index 59391f7b65..d39edab003 100644 --- a/ldo.c +++ b/ldo.c @@ -119,17 +119,13 @@ l_noret luaD_throw (lua_State *L, int errcode) { } else { /* thread has no error handler */ global_State *g = G(L); - errcode = luaD_closeprotected(L, 0, errcode); /* close all upvalues */ - L->status = cast_byte(errcode); /* mark it as dead */ + errcode = luaE_resetthread(L, errcode); /* close all upvalues */ if (g->mainthread->errorJmp) { /* main thread has a handler? */ setobjs2s(L, g->mainthread->top++, L->top - 1); /* copy error obj. */ luaD_throw(g->mainthread, errcode); /* re-throw in main thread */ } else { /* no handler at all; abort */ if (g->panic) { /* panic function? */ - luaD_seterrorobj(L, errcode, L->top); /* assume EXTRA_STACK */ - if (L->ci->top < L->top) - L->ci->top = L->top; /* pushing msg. can break this invariant */ lua_unlock(L); g->panic(L); /* call panic function (last chance to jump out) */ } diff --git a/lstate.c b/lstate.c index e01a7e7b4b..a6ef82a310 100644 --- a/lstate.c +++ b/lstate.c @@ -321,11 +321,8 @@ void luaE_freethread (lua_State *L, lua_State *L1) { } -int lua_resetthread (lua_State *L) { - CallInfo *ci; - int status = L->status; - lua_lock(L); - L->ci = ci = &L->base_ci; /* unwind CallInfo list */ +int luaE_resetthread (lua_State *L, int status) { + CallInfo *ci = L->ci = &L->base_ci; /* unwind CallInfo list */ setnilvalue(s2v(L->stack)); /* 'function' entry for basic 'ci' */ ci->func = L->stack; ci->callstatus = CIST_C; @@ -334,12 +331,19 @@ int lua_resetthread (lua_State *L) { status = luaD_closeprotected(L, 0, status); if (status != LUA_OK) /* errors? */ luaD_seterrorobj(L, status, L->stack + 1); - else { - status = LUA_OK; + else L->top = L->stack + 1; - } ci->top = L->top + LUA_MINSTACK; - L->status = status; + L->status = cast_byte(status); + luaD_reallocstack(L, cast_int(ci->top - L->stack), 0); + return status; +} + + +LUA_API int lua_resetthread (lua_State *L) { + int status; + lua_lock(L); + status = luaE_resetthread(L, L->status); lua_unlock(L); return status; } diff --git a/lstate.h b/lstate.h index cbcf07e203..38a6c9b6f2 100644 --- a/lstate.h +++ b/lstate.h @@ -359,6 +359,7 @@ LUAI_FUNC void luaE_checkcstack (lua_State *L); LUAI_FUNC void luaE_incCstack (lua_State *L); LUAI_FUNC void luaE_warning (lua_State *L, const char *msg, int tocont); LUAI_FUNC void luaE_warnerror (lua_State *L, const char *where); +LUAI_FUNC int luaE_resetthread (lua_State *L, int status); #endif From 59e565d9555c07e82808d8c1db8f4f4d159b5e5c Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 29 Dec 2020 10:23:02 -0300 Subject: [PATCH 0657/1145] No need to recheck close method before calling it A to-be-closed variable is constant and it must have a close metamethod when it is created. A program has to go out of its way (e.g., by changing the variable's metamethod) to invalidate that check. So, it is not worth to test that again. If the program tampers with the metamethod, Lua will raise a regular error when attempting to call it. --- lfunc.c | 44 +++++++++++++++++++------------------------- testes/locals.lua | 46 ++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 63 insertions(+), 27 deletions(-) diff --git a/lfunc.c b/lfunc.c index ae68487c29..a8030afa7e 100644 --- a/lfunc.c +++ b/lfunc.c @@ -101,31 +101,32 @@ UpVal *luaF_findupval (lua_State *L, StkId level) { /* -** Prepare closing method plus its arguments for object 'obj' with -** error message 'err'. (This function assumes EXTRA_STACK.) +** Call closing method for object 'obj' with error message 'err'. +** (This function assumes EXTRA_STACK.) */ -static int prepclosingmethod (lua_State *L, TValue *obj, TValue *err) { +static void callclosemethod (lua_State *L, TValue *obj, TValue *err) { StkId top = L->top; const TValue *tm = luaT_gettmbyobj(L, obj, TM_CLOSE); - if (ttisnil(tm)) /* no metamethod? */ - return 0; /* nothing to call */ setobj2s(L, top, tm); /* will call metamethod... */ setobj2s(L, top + 1, obj); /* with 'self' as the 1st argument */ setobj2s(L, top + 2, err); /* and error msg. as 2nd argument */ L->top = top + 3; /* add function and arguments */ - return 1; + luaD_callnoyield(L, top, 0); /* call method */ } /* -** Raise an error with message 'msg', inserting the name of the -** local variable at position 'level' in the stack. +** Check whether 'obj' has a close metamethod and raise an error +** if not. */ -static void varerror (lua_State *L, StkId level, const char *msg) { - int idx = cast_int(level - L->ci->func); - const char *vname = luaG_findlocal(L, L->ci, idx, NULL); - if (vname == NULL) vname = "?"; - luaG_runerror(L, msg, vname); +static void checkclosemth (lua_State *L, StkId level, const TValue *obj) { + const TValue *tm = luaT_gettmbyobj(L, obj, TM_CLOSE); + if (ttisnil(tm)) { /* no metamethod? */ + int idx = cast_int(level - L->ci->func); /* variable index */ + const char *vname = luaG_findlocal(L, L->ci, idx, NULL); + if (vname == NULL) vname = "?"; + luaG_runerror(L, "variable '%s' got a non-closable value", vname); + } } @@ -136,7 +137,7 @@ static void varerror (lua_State *L, StkId level, const char *msg) { ** the 'level' of the upvalue being closed, as everything after that ** won't be used again. */ -static void callclosemth (lua_State *L, StkId level, int status) { +static void prepcallclosemth (lua_State *L, StkId level, int status) { TValue *uv = s2v(level); /* value being closed */ TValue *errobj; if (status == CLOSEKTOP) @@ -145,10 +146,7 @@ static void callclosemth (lua_State *L, StkId level, int status) { errobj = s2v(level + 1); /* error object goes after 'uv' */ luaD_seterrorobj(L, status, level + 1); /* set error object */ } - if (prepclosingmethod(L, uv, errobj)) /* something to call? */ - luaD_callnoyield(L, L->top - 3, 0); /* call method */ - else if (!l_isfalse(uv)) /* non-closable non-false value? */ - varerror(L, level, "attempt to close non-closable variable '%s'"); + callclosemethod(L, uv, errobj); } @@ -171,16 +169,12 @@ void luaF_newtbcupval (lua_State *L, StkId level) { lua_assert(L->openupval == NULL || uplevel(L->openupval) < level); if (!l_isfalse(obj)) { /* false doesn't need to be closed */ int status; - const TValue *tm = luaT_gettmbyobj(L, obj, TM_CLOSE); - if (ttisnil(tm)) /* no metamethod? */ - varerror(L, level, "variable '%s' got a non-closable value"); + checkclosemth(L, level, obj); status = luaD_rawrunprotected(L, trynewtbcupval, level); if (unlikely(status != LUA_OK)) { /* memory error creating upvalue? */ lua_assert(status == LUA_ERRMEM); luaD_seterrorobj(L, LUA_ERRMEM, level + 1); /* save error message */ - /* next call must succeed, as object is closable */ - prepclosingmethod(L, s2v(level), s2v(level + 1)); - luaD_callnoyield(L, L->top - 3, 0); /* call method */ + callclosemethod(L, s2v(level), s2v(level + 1)); luaD_throw(L, LUA_ERRMEM); /* throw memory error */ } } @@ -215,7 +209,7 @@ void luaF_close (lua_State *L, StkId level, int status) { } if (uv->tbc && status != NOCLOSINGMETH) { ptrdiff_t levelrel = savestack(L, level); - callclosemth(L, upl, status); /* may change the stack */ + prepcallclosemth(L, upl, status); /* may change the stack */ level = restorestack(L, levelrel); } } diff --git a/testes/locals.lua b/testes/locals.lua index 84e0b03a09..1b43609b35 100644 --- a/testes/locals.lua +++ b/testes/locals.lua @@ -459,8 +459,50 @@ do -- errors due to non-closable values getmetatable(xyz).__close = nil -- remove metamethod end local stat, msg = pcall(foo) - assert(not stat and - string.find(msg, "attempt to close non%-closable variable 'xyz'")) + assert(not stat and string.find(msg, "attempt to call a nil value")) +end + + +do -- tbc inside close methods + local track = {} + local function foo () + local x = func2close(function () + local xx = func2close(function (_, msg) + assert(msg == nil) + track[#track + 1] = "xx" + end) + track[#track + 1] = "x" + end) + track[#track + 1] = "foo" + return 20, 30, 40 + end + local a, b, c, d = foo() + assert(a == 20 and b == 30 and c == 40 and d == nil) + assert(track[1] == "foo" and track[2] == "x" and track[3] == "xx") + + -- again, with errors + local track = {} + local function foo () + local x0 = func2close(function (_, msg) + assert(msg == 202) + track[#track + 1] = "x0" + end) + local x = func2close(function () + local xx = func2close(function (_, msg) + assert(msg == 101) + track[#track + 1] = "xx" + error(202) + end) + track[#track + 1] = "x" + error(101) + end) + track[#track + 1] = "foo" + return 20, 30, 40 + end + local st, msg = pcall(foo) + assert(not st and msg == 202) + assert(track[1] == "foo" and track[2] == "x" and track[3] == "xx" and + track[4] == "x0") end From 4bd10b6fe81c0a56eb9e01e24fba10e655966870 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 29 Dec 2020 13:15:54 -0300 Subject: [PATCH 0658/1145] Better error messages for calling non-callable objects When available, use the calling code to find a suitable name for what was being called; this is particularly useful for errors of non-callable metamethods. This commit also improved the debug information for order metamethods. --- ldebug.c | 23 +++++++++++++++++------ ldebug.h | 1 + ldo.c | 2 +- testes/db.lua | 6 ++++-- testes/errors.lua | 14 +++++++++++++- testes/locals.lua | 17 ++++++++++++++++- 6 files changed, 52 insertions(+), 11 deletions(-) diff --git a/ldebug.c b/ldebug.c index 8cb00e51a1..819550d76c 100644 --- a/ldebug.c +++ b/ldebug.c @@ -629,12 +629,10 @@ static const char *funcnamefromcode (lua_State *L, CallInfo *ci, case OP_LEN: tm = TM_LEN; break; case OP_CONCAT: tm = TM_CONCAT; break; case OP_EQ: tm = TM_EQ; break; - case OP_LT: case OP_LE: case OP_LTI: case OP_LEI: - *name = "order"; /* '<=' can call '__lt', etc. */ - return "metamethod"; - case OP_CLOSE: case OP_RETURN: - *name = "close"; - return "metamethod"; + /* no cases for OP_EQI and OP_EQK, as they don't call metamethods */ + case OP_LT: case OP_LTI: case OP_GTI: tm = TM_LT; break; + case OP_LE: case OP_LEI: case OP_GEI: tm = TM_LE; break; + case OP_CLOSE: case OP_RETURN: tm = TM_CLOSE; break; default: return NULL; /* cannot find a reasonable name */ } @@ -697,6 +695,19 @@ l_noret luaG_typeerror (lua_State *L, const TValue *o, const char *op) { } +l_noret luaG_callerror (lua_State *L, const TValue *o) { + CallInfo *ci = L->ci; + const char *name = NULL; /* to avoid warnings */ + const char *what = (isLua(ci)) ? funcnamefromcode(L, ci, &name) : NULL; + if (what != NULL) { + const char *t = luaT_objtypename(L, o); + luaG_runerror(L, "%s '%s' is not callable (a %s value)", what, name, t); + } + else + luaG_typeerror(L, o, "call"); +} + + l_noret luaG_forerror (lua_State *L, const TValue *o, const char *what) { luaG_runerror(L, "bad 'for' %s (number expected, got %s)", what, luaT_objtypename(L, o)); diff --git a/ldebug.h b/ldebug.h index a0a584862e..55b3ae090b 100644 --- a/ldebug.h +++ b/ldebug.h @@ -31,6 +31,7 @@ LUAI_FUNC const char *luaG_findlocal (lua_State *L, CallInfo *ci, int n, StkId *pos); LUAI_FUNC l_noret luaG_typeerror (lua_State *L, const TValue *o, const char *opname); +LUAI_FUNC l_noret luaG_callerror (lua_State *L, const TValue *o); LUAI_FUNC l_noret luaG_forerror (lua_State *L, const TValue *o, const char *what); LUAI_FUNC l_noret luaG_concaterror (lua_State *L, const TValue *p1, diff --git a/ldo.c b/ldo.c index d39edab003..5e3828f494 100644 --- a/ldo.c +++ b/ldo.c @@ -372,7 +372,7 @@ void luaD_tryfuncTM (lua_State *L, StkId func) { const TValue *tm = luaT_gettmbyobj(L, s2v(func), TM_CALL); StkId p; if (unlikely(ttisnil(tm))) - luaG_typeerror(L, s2v(func), "call"); /* nothing to call */ + luaG_callerror(L, s2v(func)); /* nothing to call */ for (p = L->top; p > func; p--) /* open space for metamethod */ setobjs2s(L, p, p-1); L->top++; /* stack space pre-allocated by the caller */ diff --git a/testes/db.lua b/testes/db.lua index fdb0da4a1c..ce559ad959 100644 --- a/testes/db.lua +++ b/testes/db.lua @@ -823,8 +823,10 @@ assert(a + 30000 == "add" and a - 3.0 == "sub" and a * 3.0 == "mul" and -a == "unm" and #a == "len" and a & 3 == "band") assert(a|3 == "bor" and 3~a == "bxor" and a<<3 == "shl" and a>>1 == "shr") assert (a==b and a.op == "eq") -assert (a>=b and a.op == "order") -assert (a>b and a.op == "order") +assert (a>=b and a.op == "le") +assert ("x">=a and a.op == "le") +assert (a>b and a.op == "lt") +assert (a>10 and a.op == "lt") assert(~a == "bnot") do -- testing for-iterator name diff --git a/testes/errors.lua b/testes/errors.lua index a3f0702167..4249f5703f 100644 --- a/testes/errors.lua +++ b/testes/errors.lua @@ -24,8 +24,9 @@ local function doit (s) end -local function checkmessage (prog, msg) +local function checkmessage (prog, msg, debug) local m = doit(prog) + if debug then print(m) end assert(string.find(m, msg, 1, true)) end @@ -120,6 +121,17 @@ assert(not string.find(doit"a={13}; local bbbb=1; a[bbbb](3)", "'bbbb'")) checkmessage("a={13}; local bbbb=1; a[bbbb](3)", "number") checkmessage("a=(1)..{}", "a table value") +-- calls +checkmessage("local a; a(13)", "local 'a'") +checkmessage([[ + local a = setmetatable({}, {__add = 34}) + a = a + 1 +]], "metamethod 'add'") +checkmessage([[ + local a = setmetatable({}, {__lt = {}}) + a = a > a +]], "metamethod 'lt'") + -- tail calls checkmessage("local a={}; return a.bbbb(3)", "field 'bbbb'") checkmessage("a={}; do local a=1 end; return a:bbbb(3)", "method 'bbbb'") diff --git a/testes/locals.lua b/testes/locals.lua index 1b43609b35..add023ca5d 100644 --- a/testes/locals.lua +++ b/testes/locals.lua @@ -459,7 +459,22 @@ do -- errors due to non-closable values getmetatable(xyz).__close = nil -- remove metamethod end local stat, msg = pcall(foo) - assert(not stat and string.find(msg, "attempt to call a nil value")) + assert(not stat and string.find(msg, "metamethod 'close'")) + + local function foo () + local a1 = func2close(function (_, msg) + assert(string.find(msg, "number value")) + error(12) + end) + local a2 = setmetatable({}, {__close = print}) + local a3 = func2close(function (_, msg) + assert(msg == nil) + error(123) + end) + getmetatable(a2).__close = 4 -- invalidate metamethod + end + local stat, msg = pcall(foo) + assert(not stat and msg == 12) end From 553b37ce4ff758d8cf80d48a21287526c92221c6 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 29 Dec 2020 13:38:47 -0300 Subject: [PATCH 0659/1145] Do not insert nil values into tables --- ltable.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ltable.c b/ltable.c index e9410f99bd..e98bab7114 100644 --- a/ltable.c +++ b/ltable.c @@ -647,6 +647,8 @@ void luaH_newkey (lua_State *L, Table *t, const TValue *key, TValue *value) { else if (unlikely(luai_numisnan(f))) luaG_runerror(L, "table index is NaN"); } + if (ttisnil(value)) + return; /* do not insert nil values */ mp = mainpositionTV(t, key); if (!isempty(gval(mp)) || isdummy(t)) { /* main position is taken? */ Node *othern; From ce101dcaf73ff6d610593230d41b63c163a91519 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 30 Dec 2020 11:20:22 -0300 Subject: [PATCH 0660/1145] Handles '__close' errors in coroutines in "coroutine style" Errors in '__close' metamethods in coroutines are handled by the same logic that handles other errors, through 'recover'. --- ldo.c | 66 ++++++++++++++++++++++++++++++-------------- testes/coroutine.lua | 41 +++++++++++++++++++++++++-- 2 files changed, 85 insertions(+), 22 deletions(-) diff --git a/ldo.c b/ldo.c index 5e3828f494..ba0c93b894 100644 --- a/ldo.c +++ b/ldo.c @@ -103,7 +103,7 @@ void luaD_seterrorobj (lua_State *L, int errcode, StkId oldtop) { break; } default: { - lua_assert(errcode >= LUA_ERRRUN); /* real error */ + lua_assert(errorstatus(errcode)); /* real error */ setobjs2s(L, oldtop, L->top - 1); /* error message on current top */ break; } @@ -593,15 +593,11 @@ static void finishCcall (lua_State *L, int status) { /* ** Executes "full continuation" (everything in the stack) of a ** previously interrupted coroutine until the stack is empty (or another -** interruption long-jumps out of the loop). If the coroutine is -** recovering from an error, 'ud' points to the error status, which must -** be passed to the first continuation function (otherwise the default -** status is LUA_YIELD). +** interruption long-jumps out of the loop). */ static void unroll (lua_State *L, void *ud) { CallInfo *ci; - if (ud != NULL) /* error status? */ - finishCcall(L, *(int *)ud); /* finish 'lua_pcallk' callee */ + UNUSED(ud); while ((ci = L->ci) != &L->base_ci) { /* something in the stack */ if (!isLua(ci)) /* C function? */ finishCcall(L, LUA_YIELD); /* complete its execution */ @@ -628,21 +624,36 @@ static CallInfo *findpcall (lua_State *L) { /* -** Recovers from an error in a coroutine. Finds a recover point (if -** there is one) and completes the execution of the interrupted -** 'luaD_pcall'. If there is no recover point, returns zero. +** Auxiliary structure to call 'recover' in protected mode. */ -static int recover (lua_State *L, int status) { - CallInfo *ci = findpcall(L); - if (ci == NULL) return 0; /* no recovery point */ +struct RecoverS { + int status; + CallInfo *ci; +}; + + +/* +** Recovers from an error in a coroutine: completes the execution of the +** interrupted 'luaD_pcall', completes the interrupted C function which +** called 'lua_pcallk', and continues running the coroutine. If there is +** an error in 'luaF_close', this function will be called again and the +** coroutine will continue from where it left. +*/ +static void recover (lua_State *L, void *ud) { + struct RecoverS *r = cast(struct RecoverS *, ud); + int status = r->status; + CallInfo *ci = r->ci; /* recover point */ + StkId func = restorestack(L, ci->u2.funcidx); /* "finish" luaD_pcall */ L->ci = ci; L->allowhook = getoah(ci->callstatus); /* restore original 'allowhook' */ - status = luaD_closeprotected(L, ci->u2.funcidx, status); - luaD_seterrorobj(L, status, restorestack(L, ci->u2.funcidx)); + luaF_close(L, func, status); /* may change the stack */ + func = restorestack(L, ci->u2.funcidx); + luaD_seterrorobj(L, status, func); luaD_shrinkstack(L); /* restore stack size in case of overflow */ L->errfunc = ci->u.c.old_errfunc; - return 1; /* continue running the coroutine */ + finishCcall(L, status); /* finish 'lua_pcallk' callee */ + unroll(L, NULL); /* continue running the coroutine */ } @@ -692,6 +703,24 @@ static void resume (lua_State *L, void *ud) { } } + +/* +** Calls 'recover' in protected mode, repeating while there are +** recoverable errors, that is, errors inside a protected call. (Any +** error interrupts 'recover', and this loop protects it again so it +** can continue.) Stops with a normal end (status == LUA_OK), an yield +** (status == LUA_YIELD), or an unprotected error ('findpcall' doesn't +** find a recover point). +*/ +static int p_recover (lua_State *L, int status) { + struct RecoverS r; + r.status = status; + while (errorstatus(status) && (r.ci = findpcall(L)) != NULL) + r.status = luaD_rawrunprotected(L, recover, &r); + return r.status; +} + + LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs, int *nresults) { int status; @@ -709,10 +738,7 @@ LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs, api_checknelems(L, (L->status == LUA_OK) ? nargs + 1 : nargs); status = luaD_rawrunprotected(L, resume, &nargs); /* continue running after recoverable errors */ - while (errorstatus(status) && recover(L, status)) { - /* unroll continuation */ - status = luaD_rawrunprotected(L, unroll, &status); - } + status = p_recover(L, status); if (likely(!errorstatus(status))) lua_assert(status == L->status); /* normal end or yield */ else { /* unrecoverable error */ diff --git a/testes/coroutine.lua b/testes/coroutine.lua index 0a970e985f..fbeabd07f9 100644 --- a/testes/coroutine.lua +++ b/testes/coroutine.lua @@ -123,7 +123,7 @@ assert(#a == 22 and a[#a] == 79) x, a = nil --- coroutine closing +print("to-be-closed variables in coroutines") local function func2close (f) return setmetatable({}, {__close = f}) @@ -189,7 +189,6 @@ do local st, msg = coroutine.close(co) assert(st == false and coroutine.status(co) == "dead" and msg == 200) assert(x == 200) - end do @@ -207,6 +206,44 @@ do local st1, st2, err = coroutine.resume(co) assert(st1 and not st2 and err == 43) assert(X == 43 and Y.name == "pcall") + + -- recovering from errors in __close metamethods + local track = {} + + local function h (o) + local hv = o + return 1 + end + + local function foo () + local x = func2close(function(_,msg) + track[#track + 1] = msg or false + error(20) + end) + local y = func2close(function(_,msg) + track[#track + 1] = msg or false + return 1000 + end) + local z = func2close(function(_,msg) + track[#track + 1] = msg or false + error(10) + end) + coroutine.yield(1) + h(func2close(function(_,msg) + track[#track + 1] = msg or false + error(2) + end)) + end + + local co = coroutine.create(pcall) + + local st, res = coroutine.resume(co, foo) -- call 'foo' protected + assert(st and res == 1) -- yield 1 + local st, res1, res2 = coroutine.resume(co) -- continue + assert(coroutine.status(co) == "dead") + assert(st and not res1 and res2 == 20) -- last error (20) + assert(track[1] == false and track[2] == 2 and track[3] == 10 and + track[4] == 10) end From cc1692515e2a6aabc6d07159e7926656e38eda53 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 11 Jan 2021 15:03:01 -0300 Subject: [PATCH 0661/1145] New API function 'lua_closeslot' Closing a to-be-closed variable with 'lua_settop' is too restrictive, as it erases all slots above the variable. Moreover, it adds side effects to 'lua_settop', which should be a fairly basic function. --- lapi.c | 19 ++++++++++++++++++- lauxlib.c | 8 +++----- ltests.c | 3 +++ lua.h | 3 ++- manual/manual.of | 33 +++++++++++++++++++++++---------- testes/api.lua | 29 +++++++++++++++++------------ 6 files changed, 66 insertions(+), 29 deletions(-) diff --git a/lapi.c b/lapi.c index 00e95a11a5..0f0e31afec 100644 --- a/lapi.c +++ b/lapi.c @@ -187,9 +187,26 @@ LUA_API void lua_settop (lua_State *L, int idx) { api_check(L, -(idx+1) <= (L->top - (func + 1)), "invalid new top"); diff = idx + 1; /* will "subtract" index (as it is negative) */ } +#if defined(LUA_COMPAT_5_4_0) if (diff < 0 && hastocloseCfunc(ci->nresults)) luaF_close(L, L->top + diff, CLOSEKTOP); - L->top += diff; /* correct top only after closing any upvalue */ +#endif + L->top += diff; + api_check(L, L->openupval == NULL || uplevel(L->openupval) < L->top, + "cannot pop an unclosed slot"); + lua_unlock(L); +} + + +LUA_API void lua_closeslot (lua_State *L, int idx) { + StkId level; + lua_lock(L); + level = index2stack(L, idx); + api_check(L, hastocloseCfunc(L->ci->nresults) && L->openupval != NULL && + uplevel(L->openupval) == level, + "no variable to close at given level"); + luaF_close(L, level, CLOSEKTOP); + setnilvalue(s2v(level)); lua_unlock(L); } diff --git a/lauxlib.c b/lauxlib.c index 074ff08cd4..e8fc486ecb 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -545,10 +545,8 @@ static char *prepbuffsize (luaL_Buffer *B, size_t sz, int boxidx) { if (buffonstack(B)) /* buffer already has a box? */ newbuff = (char *)resizebox(L, boxidx, newsize); /* resize it */ else { /* no box yet */ - lua_pushnil(L); /* reserve slot for final result */ newbox(L); /* create a new box */ - /* move box (and slot) to its intended position */ - lua_rotate(L, boxidx - 1, 2); + lua_insert(L, boxidx); /* move box to its intended position */ lua_toclose(L, boxidx); newbuff = (char *)resizebox(L, boxidx, newsize); memcpy(newbuff, B->b, B->n * sizeof(char)); /* copy original content */ @@ -585,8 +583,8 @@ LUALIB_API void luaL_pushresult (luaL_Buffer *B) { lua_State *L = B->L; lua_pushlstring(L, B->b, B->n); if (buffonstack(B)) { - lua_copy(L, -1, -3); /* move string to reserved slot */ - lua_pop(L, 2); /* pop string and box (closing the box) */ + lua_closeslot(L, -2); /* close the box */ + lua_remove(L, -2); /* remove box from the stack */ } } diff --git a/ltests.c b/ltests.c index 6920dd69f7..9c13338a8a 100644 --- a/ltests.c +++ b/ltests.c @@ -1766,6 +1766,9 @@ static struct X { int x; } x; else if EQ("toclose") { lua_toclose(L1, getnum); } + else if EQ("closeslot") { + lua_closeslot(L1, getnum); + } else luaL_error(L, "unknown instruction %s", buff); } return 0; diff --git a/lua.h b/lua.h index c9d64d7f21..aec70dacf3 100644 --- a/lua.h +++ b/lua.h @@ -347,7 +347,8 @@ LUA_API size_t (lua_stringtonumber) (lua_State *L, const char *s); LUA_API lua_Alloc (lua_getallocf) (lua_State *L, void **ud); LUA_API void (lua_setallocf) (lua_State *L, lua_Alloc f, void *ud); -LUA_API void (lua_toclose) (lua_State *L, int idx); +LUA_API void (lua_toclose) (lua_State *L, int idx); +LUA_API void (lua_closeslot) (lua_State *L, int idx); /* diff --git a/manual/manual.of b/manual/manual.of index c538525847..09297a6354 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -3095,6 +3095,18 @@ will probably need to close states as soon as they are not needed. } +@APIEntry{void lua_closeslot (lua_State *L, int index);| +@apii{0,0,e} + +Close the to-be-closed slot at the given index and set its value to @nil. +The index must be the last index previously marked to be closed +@see{lua_toclose} that is still active (that is, not closed yet). + +(Exceptionally, this function was introduced in release 5.4.3. +It is not present in previous 5.4 releases.) + +} + @APIEntry{int lua_compare (lua_State *L, int index1, int index2, int op);| @apii{0,0,e} @@ -3747,9 +3759,7 @@ except that it allows the called function to yield @see{continuations}. @apii{n,0,e} Pops @id{n} elements from the stack. - -This function can run arbitrary code when removing an index -marked as to-be-closed from the stack. +It is implemented as a macro over @Lid{lua_settop}. } @@ -4240,8 +4250,12 @@ If the new top is greater than the old one, then the new elements are filled with @nil. If @id{index} @N{is 0}, then all stack elements are removed. -This function can run arbitrary code when removing an index -marked as to-be-closed from the stack. +For compatibility reasons, +this function may close slots marked as to-be-closed @see{lua_toclose}, +and therefore it can run arbitrary code. +You should not rely on this behavior: +Instead, always close to-be-closed slots explicitly, +with @Lid{lua_closeslot}, before removing them from the stack. } @@ -4337,10 +4351,9 @@ when it goes out of scope. Here, in the context of a C function, to go out of scope means that the running function returns to Lua, there is an error, -or the index is removed from the stack through -@Lid{lua_settop} or @Lid{lua_pop}. -An index marked as to-be-closed should not be removed from the stack -by any other function in the API except @Lid{lua_settop} or @Lid{lua_pop}. +or there is a call to @Lid{lua_closeslot}. +An index marked as to-be-closed should neither be removed from the stack +nor modified before a corresponding call to @Lid{lua_closeslot}. This function should not be called for an index that is equal to or below an active to-be-closed index. @@ -4353,7 +4366,7 @@ Note that, both in case of errors and of a regular return, by the time the @idx{__close} metamethod runs, the @N{C stack} was already unwound, so that any automatic @N{C variable} declared in the calling function -will be out of scope. +(e.g., a buffer) will be out of scope. } diff --git a/testes/api.lua b/testes/api.lua index 95551481da..fb7e708566 100644 --- a/testes/api.lua +++ b/testes/api.lua @@ -507,10 +507,12 @@ function checkerrnopro (code, msg) end if not _soft then + collectgarbage("stop") -- avoid __gc with full stack checkerrnopro("pushnum 3; call 0 0", "attempt to call") print"testing stack overflow in unprotected thread" function f () f() end checkerrnopro("getglobal 'f'; call 0 0;", "stack overflow") + collectgarbage("restart") end print"+" @@ -1125,26 +1127,29 @@ do assert(#openresource == n) end - -- closing resources with 'settop' + -- closing resources with 'closeslot' + _ENV.xxx = true local a = T.testC([[ - pushvalue 2 - call 0 1 # create resource + pushvalue 2 # stack: S, NR, CH + call 0 1 # create resource; stack: S, NR, CH, R toclose -1 # mark it to be closed - pushvalue 2 - call 0 1 # create another resource + pushvalue 2 # stack: S, NR, CH, R, NR + call 0 1 # create another resource; stack: S, NR, CH, R, R toclose -1 # mark it to be closed - pushvalue 3 + pushvalue 3 # stack: S, NR, CH, R, R, CH pushint 2 # there should be two open resources - call 1 0 - pop 1 # pop second resource from the stack - pushvalue 3 + call 1 0 # stack: S, NR, CH, R, R + closeslot -1 # close second resource + pushvalue 3 # stack: S, NR, CH, R, R, CH pushint 1 # there should be one open resource - call 1 0 - pop 1 # pop second resource from the stack + call 1 0 # stack: S, NR, CH, R, R + closeslot 4 + setglobal "xxx" # previous op. erased the slot + pop 1 # pop other resource from the stack pushint * return 1 # return stack size ]], newresource, check) - assert(a == 3) -- no extra items left in the stack + assert(a == 3 and _ENV.xxx == nil) -- no extra items left in the stack -- non-closable value local a, b = pcall(T.makeCfunc[[ From b07fc10e91a5954254b47280aba287220c734a4b Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 13 Jan 2021 13:54:10 -0300 Subject: [PATCH 0662/1145] Allow yields inside '__close' metamethods Initial implementation to allow yields inside '__close' metamethods. This current version still does not allow a '__close' metamethod to yield when called due to an error. '__close' metamethods from C functions also are not allowed to yield. --- lapi.c | 4 +-- ldo.c | 6 ++-- lfunc.c | 20 ++++++----- lfunc.h | 2 +- lstate.c | 2 +- lvm.c | 10 ++++-- testes/locals.lua | 88 +++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 114 insertions(+), 18 deletions(-) diff --git a/lapi.c b/lapi.c index 0f0e31afec..3583e9c06d 100644 --- a/lapi.c +++ b/lapi.c @@ -189,7 +189,7 @@ LUA_API void lua_settop (lua_State *L, int idx) { } #if defined(LUA_COMPAT_5_4_0) if (diff < 0 && hastocloseCfunc(ci->nresults)) - luaF_close(L, L->top + diff, CLOSEKTOP); + luaF_close(L, L->top + diff, CLOSEKTOP, 0); #endif L->top += diff; api_check(L, L->openupval == NULL || uplevel(L->openupval) < L->top, @@ -205,7 +205,7 @@ LUA_API void lua_closeslot (lua_State *L, int idx) { api_check(L, hastocloseCfunc(L->ci->nresults) && L->openupval != NULL && uplevel(L->openupval) == level, "no variable to close at given level"); - luaF_close(L, level, CLOSEKTOP); + luaF_close(L, level, CLOSEKTOP, 0); setnilvalue(s2v(level)); lua_unlock(L); } diff --git a/ldo.c b/ldo.c index ba0c93b894..aa159cf0c4 100644 --- a/ldo.c +++ b/ldo.c @@ -406,7 +406,7 @@ static void moveresults (lua_State *L, StkId res, int nres, int wanted) { default: /* multiple results (or to-be-closed variables) */ if (hastocloseCfunc(wanted)) { /* to-be-closed variables? */ ptrdiff_t savedres = savestack(L, res); - luaF_close(L, res, CLOSEKTOP); /* may change the stack */ + luaF_close(L, res, CLOSEKTOP, 0); /* may change the stack */ res = restorestack(L, savedres); wanted = codeNresults(wanted); /* correct value */ if (wanted == LUA_MULTRET) @@ -647,7 +647,7 @@ static void recover (lua_State *L, void *ud) { /* "finish" luaD_pcall */ L->ci = ci; L->allowhook = getoah(ci->callstatus); /* restore original 'allowhook' */ - luaF_close(L, func, status); /* may change the stack */ + luaF_close(L, func, status, 0); /* may change the stack */ func = restorestack(L, ci->u2.funcidx); luaD_seterrorobj(L, status, func); luaD_shrinkstack(L); /* restore stack size in case of overflow */ @@ -803,7 +803,7 @@ struct CloseP { */ static void closepaux (lua_State *L, void *ud) { struct CloseP *pcl = cast(struct CloseP *, ud); - luaF_close(L, pcl->level, pcl->status); + luaF_close(L, pcl->level, pcl->status, 0); } diff --git a/lfunc.c b/lfunc.c index a8030afa7e..13e44d46ef 100644 --- a/lfunc.c +++ b/lfunc.c @@ -101,17 +101,21 @@ UpVal *luaF_findupval (lua_State *L, StkId level) { /* -** Call closing method for object 'obj' with error message 'err'. +** Call closing method for object 'obj' with error message 'err'. The +** boolean 'yy' controls whether the call is yieldable. ** (This function assumes EXTRA_STACK.) */ -static void callclosemethod (lua_State *L, TValue *obj, TValue *err) { +static void callclosemethod (lua_State *L, TValue *obj, TValue *err, int yy) { StkId top = L->top; const TValue *tm = luaT_gettmbyobj(L, obj, TM_CLOSE); setobj2s(L, top, tm); /* will call metamethod... */ setobj2s(L, top + 1, obj); /* with 'self' as the 1st argument */ setobj2s(L, top + 2, err); /* and error msg. as 2nd argument */ L->top = top + 3; /* add function and arguments */ - luaD_callnoyield(L, top, 0); /* call method */ + if (yy) + luaD_call(L, top, 0); + else + luaD_callnoyield(L, top, 0); } @@ -137,7 +141,7 @@ static void checkclosemth (lua_State *L, StkId level, const TValue *obj) { ** the 'level' of the upvalue being closed, as everything after that ** won't be used again. */ -static void prepcallclosemth (lua_State *L, StkId level, int status) { +static void prepcallclosemth (lua_State *L, StkId level, int status, int yy) { TValue *uv = s2v(level); /* value being closed */ TValue *errobj; if (status == CLOSEKTOP) @@ -146,7 +150,7 @@ static void prepcallclosemth (lua_State *L, StkId level, int status) { errobj = s2v(level + 1); /* error object goes after 'uv' */ luaD_seterrorobj(L, status, level + 1); /* set error object */ } - callclosemethod(L, uv, errobj); + callclosemethod(L, uv, errobj, yy); } @@ -174,7 +178,7 @@ void luaF_newtbcupval (lua_State *L, StkId level) { if (unlikely(status != LUA_OK)) { /* memory error creating upvalue? */ lua_assert(status == LUA_ERRMEM); luaD_seterrorobj(L, LUA_ERRMEM, level + 1); /* save error message */ - callclosemethod(L, s2v(level), s2v(level + 1)); + callclosemethod(L, s2v(level), s2v(level + 1), 0); luaD_throw(L, LUA_ERRMEM); /* throw memory error */ } } @@ -194,7 +198,7 @@ void luaF_unlinkupval (UpVal *uv) { ** to NOCLOSINGMETH closes upvalues without running any __close ** metamethods. */ -void luaF_close (lua_State *L, StkId level, int status) { +void luaF_close (lua_State *L, StkId level, int status, int yy) { UpVal *uv; StkId upl; /* stack index pointed by 'uv' */ while ((uv = L->openupval) != NULL && (upl = uplevel(uv)) >= level) { @@ -209,7 +213,7 @@ void luaF_close (lua_State *L, StkId level, int status) { } if (uv->tbc && status != NOCLOSINGMETH) { ptrdiff_t levelrel = savestack(L, level); - prepcallclosemth(L, upl, status); /* may change the stack */ + prepcallclosemth(L, upl, status, yy); /* may change the stack */ level = restorestack(L, levelrel); } } diff --git a/lfunc.h b/lfunc.h index 40de46365e..2e6df53592 100644 --- a/lfunc.h +++ b/lfunc.h @@ -59,7 +59,7 @@ LUAI_FUNC LClosure *luaF_newLclosure (lua_State *L, int nupvals); LUAI_FUNC void luaF_initupvals (lua_State *L, LClosure *cl); LUAI_FUNC UpVal *luaF_findupval (lua_State *L, StkId level); LUAI_FUNC void luaF_newtbcupval (lua_State *L, StkId level); -LUAI_FUNC void luaF_close (lua_State *L, StkId level, int status); +LUAI_FUNC void luaF_close (lua_State *L, StkId level, int status, int yy); LUAI_FUNC void luaF_unlinkupval (UpVal *uv); LUAI_FUNC void luaF_freeproto (lua_State *L, Proto *f); LUAI_FUNC const char *luaF_getlocalname (const Proto *func, int local_number, diff --git a/lstate.c b/lstate.c index a6ef82a310..92ccbf9b69 100644 --- a/lstate.c +++ b/lstate.c @@ -313,7 +313,7 @@ LUA_API lua_State *lua_newthread (lua_State *L) { void luaE_freethread (lua_State *L, lua_State *L1) { LX *l = fromstate(L1); - luaF_close(L1, L1->stack, NOCLOSINGMETH); /* close all upvalues */ + luaF_close(L1, L1->stack, NOCLOSINGMETH, 0); /* close all upvalues */ lua_assert(L1->openupval == NULL); luai_userstatefree(L, L1); freestack(L1); diff --git a/lvm.c b/lvm.c index a6f04606dd..d6c05bbd6b 100644 --- a/lvm.c +++ b/lvm.c @@ -842,6 +842,10 @@ void luaV_finishOp (lua_State *L) { luaV_concat(L, total); /* concat them (may yield again) */ break; } + case OP_CLOSE: case OP_RETURN: { /* yielded closing variables */ + ci->u.l.savedpc--; /* repeat instruction to close other vars. */ + break; + } default: { /* only these other opcodes can yield */ lua_assert(op == OP_TFORCALL || op == OP_CALL || @@ -1524,7 +1528,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_CLOSE) { - Protect(luaF_close(L, ra, LUA_OK)); + Protect(luaF_close(L, ra, LUA_OK, 1)); vmbreak; } vmcase(OP_TBC) { @@ -1632,7 +1636,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { /* close upvalues from current call; the compiler ensures that there are no to-be-closed variables here, so this call cannot change the stack */ - luaF_close(L, base, NOCLOSINGMETH); + luaF_close(L, base, NOCLOSINGMETH, 0); lua_assert(base == ci->func + 1); } while (!ttisfunction(s2v(ra))) { /* not a function? */ @@ -1662,7 +1666,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { if (TESTARG_k(i)) { /* may there be open upvalues? */ if (L->top < ci->top) L->top = ci->top; - luaF_close(L, base, CLOSEKTOP); + luaF_close(L, base, CLOSEKTOP, 1); updatetrap(ci); updatestack(ci); } diff --git a/testes/locals.lua b/testes/locals.lua index add023ca5d..c9c93ccff4 100644 --- a/testes/locals.lua +++ b/testes/locals.lua @@ -640,6 +640,94 @@ end print "to-be-closed variables in coroutines" +do + -- yielding inside closing metamethods + + local function checktable (t1, t2) + assert(#t1 == #t2) + for i = 1, #t1 do + assert(t1[i] == t2[i]) + end + end + + local trace = {} + local co = coroutine.wrap(function () + + trace[#trace + 1] = "nowX" + + -- will be closed after 'y' + local x = func2close(function (_, msg) + assert(msg == nil) + trace[#trace + 1] = "x1" + coroutine.yield("x") + trace[#trace + 1] = "x2" + end) + + return pcall(function () + do -- 'z' will be closed first + local z = func2close(function (_, msg) + assert(msg == nil) + trace[#trace + 1] = "z1" + coroutine.yield("z") + trace[#trace + 1] = "z2" + end) + end + + trace[#trace + 1] = "nowY" + + -- will be closed after 'z' + local y = func2close(function(_, msg) + assert(msg == nil) + trace[#trace + 1] = "y1" + coroutine.yield("y") + trace[#trace + 1] = "y2" + end) + + return 10, 20, 30 + end) + end) + + assert(co() == "z") + assert(co() == "y") + assert(co() == "x") + checktable({co()}, {true, 10, 20, 30}) + checktable(trace, {"nowX", "z1", "z2", "nowY", "y1", "y2", "x1", "x2"}) + +end + + +do + -- yielding inside closing metamethods after an error: + -- not yet implemented; raises an error + + local co = coroutine.wrap(function () + + local function foo (err) + + local x = func2close(function(_, msg) + assert(msg == err) + coroutine.yield("x") + return 100, 200 + end) + + if err then error(err) else return 10, 20 end + end + + coroutine.yield(pcall(foo, nil)) -- no error + return pcall(foo, 10) -- 'foo' will raise an error + end) + + local a, b = co() + assert(a == "x" and b == nil) -- yields inside 'x'; Ok + + local a, b, c = co() + assert(a and b == 10 and c == 20) -- returns from 'pcall(foo, nil)' + + local st, msg = co() -- error yielding after an error + assert(not st and string.find(msg, "attempt to yield")) +end + + do -- an error in a wrapped coroutine closes variables local x = false From 825ac8eca8e384d6ad2538b5670088c31e08a9d7 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 14 Jan 2021 13:26:55 -0300 Subject: [PATCH 0663/1145] Corrected documentation for 'table.sort' The sort function must define a (strict) weak order for a correct sorting. A partial order is not enough. --- manual/manual.of | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/manual/manual.of b/manual/manual.of index 09297a6354..2fe332a4d8 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -7821,19 +7821,19 @@ from @T{list[1]} to @T{list[#list]}. If @id{comp} is given, then it must be a function that receives two list elements and returns true when the first element must come -before the second in the final order -(so that, after the sort, -@T{i < j} implies @T{not comp(list[j],list[i])}). +before the second in the final order, +so that, after the sort, +@T{i <= j} implies @T{not comp(list[j],list[i])}. If @id{comp} is not given, then the standard Lua operator @T{<} is used instead. -Note that the @id{comp} function must define -a strict partial order over the elements in the list; -that is, it must be asymmetric and transitive. -Otherwise, no valid sort may be possible. +The @id{comp} function must define a consistent order; +more formally, the function must define a strict weak order. +(A weak order is similar to a total order, +but it can equate different elements for comparison purposes.) The sort algorithm is not stable: -elements considered equal by the given order +Different elements considered equal by the given order may have their relative positions changed by the sort. } From d0f34d91373fa265d4445e456e4a10ce206c1559 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 18 Jan 2021 11:40:45 -0300 Subject: [PATCH 0664/1145] Allow yields in '__close' metamethods ater errors Completes commit b07fc10e91a. '__close' metamethods can yield even when they are being called due to an error. '__close' metamethods from C functions are still not allowed to yield. --- ldo.c | 127 ++++++++++++++++++++++++---------------------- lstate.h | 22 ++++++-- testes/locals.lua | 48 +++++++++++++++--- 3 files changed, 126 insertions(+), 71 deletions(-) diff --git a/ldo.c b/ldo.c index aa159cf0c4..45cfd59242 100644 --- a/ldo.c +++ b/ldo.c @@ -565,25 +565,64 @@ void luaD_callnoyield (lua_State *L, StkId func, int nResults) { /* -** Completes the execution of an interrupted C function, calling its -** continuation function. +** Finish the job of 'lua_pcallk' after it was interrupted by an yield. +** (The caller, 'finishCcall', does the final call to 'adjustresults'.) +** The main job is to complete the 'luaD_pcall' called by 'lua_pcallk'. +** If a '__close' method yields here, eventually control will be back +** to 'finishCcall' (when that '__close' method finally returns) and +** 'finishpcallk' will run again and close any still pending '__close' +** methods. Similarly, if a '__close' method errs, 'precover' calls +** 'unroll' which calls ''finishCcall' and we are back here again, to +** close any pending '__close' methods. +** Note that, up to the call to 'luaF_close', the corresponding +** 'CallInfo' is not modified, so that this repeated run works like the +** first one (except that it has at least one less '__close' to do). In +** particular, field CIST_RECST preserves the error status across these +** multiple runs, changing only if there is a new error. */ -static void finishCcall (lua_State *L, int status) { - CallInfo *ci = L->ci; +static int finishpcallk (lua_State *L, CallInfo *ci) { + int status = getcistrecst(ci); /* get original status */ + if (status == LUA_OK) /* no error? */ + status = LUA_YIELD; /* was interrupted by an yield */ + else { /* error */ + StkId func = restorestack(L, ci->u2.funcidx); + L->allowhook = getoah(ci->callstatus); /* restore 'allowhook' */ + luaF_close(L, func, status, 1); /* can yield or raise an error */ + func = restorestack(L, ci->u2.funcidx); /* stack may be moved */ + luaD_seterrorobj(L, status, func); + luaD_shrinkstack(L); /* restore stack size in case of overflow */ + setcistrecst(ci, LUA_OK); /* clear original status */ + } + ci->callstatus &= ~CIST_YPCALL; + L->errfunc = ci->u.c.old_errfunc; + /* if it is here, there were errors or yields; unlike 'lua_pcallk', + do not change status */ + return status; +} + + +/* +** Completes the execution of a C function interrupted by an yield. +** The interruption must have happened while the function was +** executing 'lua_callk' or 'lua_pcallk'. In the second case, the +** call to 'finishpcallk' finishes the interrupted execution of +** 'lua_pcallk'. After that, it calls the continuation of the +** interrupted function and finally it completes the job of the +** 'luaD_call' that called the function. +** In the call to 'adjustresults', we do not know the number of +** results of the function called by 'lua_callk'/'lua_pcallk', +** so we are conservative and use LUA_MULTRET (always adjust). +*/ +static void finishCcall (lua_State *L, CallInfo *ci) { int n; + int status = LUA_YIELD; /* default if there were no errors */ /* must have a continuation and must be able to call it */ lua_assert(ci->u.c.k != NULL && yieldable(L)); - /* error status can only happen in a protected call */ - lua_assert((ci->callstatus & CIST_YPCALL) || status == LUA_YIELD); - if (ci->callstatus & CIST_YPCALL) { /* was inside a pcall? */ - ci->callstatus &= ~CIST_YPCALL; /* continuation is also inside it */ - L->errfunc = ci->u.c.old_errfunc; /* with the same error function */ - } - /* finish 'lua_callk'/'lua_pcall'; CIST_YPCALL and 'errfunc' already - handled */ - adjustresults(L, ci->nresults); + if (ci->callstatus & CIST_YPCALL) /* was inside a 'lua_pcallk'? */ + status = finishpcallk(L, ci); /* finish it */ + adjustresults(L, LUA_MULTRET); /* finish 'lua_callk' */ lua_unlock(L); - n = (*ci->u.c.k)(L, status, ci->u.c.ctx); /* call continuation function */ + n = (*ci->u.c.k)(L, status, ci->u.c.ctx); /* call continuation */ lua_lock(L); api_checknelems(L, n); luaD_poscall(L, ci, n); /* finish 'luaD_call' */ @@ -600,7 +639,7 @@ static void unroll (lua_State *L, void *ud) { UNUSED(ud); while ((ci = L->ci) != &L->base_ci) { /* something in the stack */ if (!isLua(ci)) /* C function? */ - finishCcall(L, LUA_YIELD); /* complete its execution */ + finishCcall(L, ci); /* complete its execution */ else { /* Lua function */ luaV_finishOp(L); /* finish interrupted instruction */ luaV_execute(L, ci); /* execute down to higher C 'boundary' */ @@ -623,40 +662,6 @@ static CallInfo *findpcall (lua_State *L) { } -/* -** Auxiliary structure to call 'recover' in protected mode. -*/ -struct RecoverS { - int status; - CallInfo *ci; -}; - - -/* -** Recovers from an error in a coroutine: completes the execution of the -** interrupted 'luaD_pcall', completes the interrupted C function which -** called 'lua_pcallk', and continues running the coroutine. If there is -** an error in 'luaF_close', this function will be called again and the -** coroutine will continue from where it left. -*/ -static void recover (lua_State *L, void *ud) { - struct RecoverS *r = cast(struct RecoverS *, ud); - int status = r->status; - CallInfo *ci = r->ci; /* recover point */ - StkId func = restorestack(L, ci->u2.funcidx); - /* "finish" luaD_pcall */ - L->ci = ci; - L->allowhook = getoah(ci->callstatus); /* restore original 'allowhook' */ - luaF_close(L, func, status, 0); /* may change the stack */ - func = restorestack(L, ci->u2.funcidx); - luaD_seterrorobj(L, status, func); - luaD_shrinkstack(L); /* restore stack size in case of overflow */ - L->errfunc = ci->u.c.old_errfunc; - finishCcall(L, status); /* finish 'lua_pcallk' callee */ - unroll(L, NULL); /* continue running the coroutine */ -} - - /* ** Signal an error in the call to 'lua_resume', not in the execution ** of the coroutine itself. (Such errors should not be handled by any @@ -705,19 +710,21 @@ static void resume (lua_State *L, void *ud) { /* -** Calls 'recover' in protected mode, repeating while there are -** recoverable errors, that is, errors inside a protected call. (Any -** error interrupts 'recover', and this loop protects it again so it -** can continue.) Stops with a normal end (status == LUA_OK), an yield +** Unrolls a coroutine in protected mode while there are recoverable +** errors, that is, errors inside a protected call. (Any error +** interrupts 'unroll', and this loop protects it again so it can +** continue.) Stops with a normal end (status == LUA_OK), an yield ** (status == LUA_YIELD), or an unprotected error ('findpcall' doesn't ** find a recover point). */ -static int p_recover (lua_State *L, int status) { - struct RecoverS r; - r.status = status; - while (errorstatus(status) && (r.ci = findpcall(L)) != NULL) - r.status = luaD_rawrunprotected(L, recover, &r); - return r.status; +static int precover (lua_State *L, int status) { + CallInfo *ci; + while (errorstatus(status) && (ci = findpcall(L)) != NULL) { + L->ci = ci; /* go down to recovery functions */ + setcistrecst(ci, status); /* status to finish 'pcall' */ + status = luaD_rawrunprotected(L, unroll, NULL); + } + return status; } @@ -738,7 +745,7 @@ LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs, api_checknelems(L, (L->status == LUA_OK) ? nargs + 1 : nargs); status = luaD_rawrunprotected(L, resume, &nargs); /* continue running after recoverable errors */ - status = p_recover(L, status); + status = precover(L, status); if (likely(!errorstatus(status))) lua_assert(status == L->status); /* normal end or yield */ else { /* unrecoverable error */ diff --git a/lstate.h b/lstate.h index 38a6c9b6f2..38248e578d 100644 --- a/lstate.h +++ b/lstate.h @@ -191,17 +191,33 @@ typedef struct CallInfo { */ #define CIST_OAH (1<<0) /* original value of 'allowhook' */ #define CIST_C (1<<1) /* call is running a C function */ -#define CIST_FRESH (1<<2) /* call is on a fresh "luaV_execute" frame */ +#define CIST_FRESH (1<<2) /* call is on a fresh "luaV_execute" frame */ #define CIST_HOOKED (1<<3) /* call is running a debug hook */ #define CIST_YPCALL (1<<4) /* call is a yieldable protected call */ #define CIST_TAIL (1<<5) /* call was tail called */ #define CIST_HOOKYIELD (1<<6) /* last hook called yielded */ -#define CIST_FIN (1<<7) /* call is running a finalizer */ +#define CIST_FIN (1<<7) /* call is running a finalizer */ #define CIST_TRAN (1<<8) /* 'ci' has transfer information */ +/* Bits 9-11 are used for CIST_RECST (see below) */ +#define CIST_RECST 9 #if defined(LUA_COMPAT_LT_LE) -#define CIST_LEQ (1<<9) /* using __lt for __le */ +#define CIST_LEQ (1<<12) /* using __lt for __le */ #endif + +/* +** Field CIST_RECST stores the "recover status", used to keep the error +** status while closing to-be-closed variables in coroutines, so that +** Lua can correctly resume after an yield from a __close method called +** because of an error. (Three bits are enough for error status.) +*/ +#define getcistrecst(ci) (((ci)->callstatus >> CIST_RECST) & 7) +#define setcistrecst(ci,st) \ + check_exp(((st) & 7) == (st), /* status must fit in three bits */ \ + ((ci)->callstatus = ((ci)->callstatus & ~(7 << CIST_RECST)) \ + | ((st) << CIST_RECST))) + + /* active function is a Lua function */ #define isLua(ci) (!((ci)->callstatus & CIST_C)) diff --git a/testes/locals.lua b/testes/locals.lua index c9c93ccff4..8506195ead 100644 --- a/testes/locals.lua +++ b/testes/locals.lua @@ -697,34 +697,66 @@ end do - -- yielding inside closing metamethods after an error: - -- not yet implemented; raises an error + -- yielding inside closing metamethods after an error local co = coroutine.wrap(function () local function foo (err) + local z = func2close(function(_, msg) + assert(msg == nil or msg == err + 20) + coroutine.yield("z") + return 100, 200 + end) + + local y = func2close(function(_, msg) + -- still gets the original error (if any) + assert(msg == err or (msg == nil and err == 1)) + coroutine.yield("y") + if err then error(err + 20) end -- creates or changes the error + end) + local x = func2close(function(_, msg) - assert(msg == err) + assert(msg == err or (msg == nil and err == 1)) coroutine.yield("x") return 100, 200 end) - if err then error(err) else return 10, 20 end + if err == 10 then error(err) else return 10, 20 end end coroutine.yield(pcall(foo, nil)) -- no error + coroutine.yield(pcall(foo, 1)) -- error in __close return pcall(foo, 10) -- 'foo' will raise an error end) - local a, b = co() + local a, b = co() -- first foo: no error assert(a == "x" and b == nil) -- yields inside 'x'; Ok - + a, b = co() + assert(a == "y" and b == nil) -- yields inside 'y'; Ok + a, b = co() + assert(a == "z" and b == nil) -- yields inside 'z'; Ok local a, b, c = co() assert(a and b == 10 and c == 20) -- returns from 'pcall(foo, nil)' - local st, msg = co() -- error yielding after an error - assert(not st and string.find(msg, "attempt to yield")) + local a, b = co() -- second foo: error in __close + assert(a == "x" and b == nil) -- yields inside 'x'; Ok + a, b = co() + assert(a == "y" and b == nil) -- yields inside 'y'; Ok + a, b = co() + assert(a == "z" and b == nil) -- yields inside 'z'; Ok + local st, msg = co() -- reports the error in 'y' + assert(not st and msg == 21) + + local a, b = co() -- third foo: error in function body + assert(a == "x" and b == nil) -- yields inside 'x'; Ok + a, b = co() + assert(a == "y" and b == nil) -- yields inside 'y'; Ok + a, b = co() + assert(a == "z" and b == nil) -- yields inside 'z'; Ok + local st, msg = co() -- gets final error + assert(not st and msg == 10 + 20) + end From 6ccd24eff58340c00db2877c4558a63c6b859442 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 19 Jan 2021 10:03:13 -0300 Subject: [PATCH 0665/1145] Simpler handling of errors when creating tbc variables New field 'lua_State.ptbc' keeps to-be-closed variable until its upvalue is created, so that it can be closed in case of a memory-allocation error. --- ldo.c | 1 + lfunc.c | 37 ++++++++++++++++--------------------- lstate.c | 4 ++-- lstate.h | 1 + manual/manual.of | 4 ---- testes/locals.lua | 13 +++++-------- 6 files changed, 25 insertions(+), 35 deletions(-) diff --git a/ldo.c b/ldo.c index 45cfd59242..9e3d6955a0 100644 --- a/ldo.c +++ b/ldo.c @@ -163,6 +163,7 @@ static void correctstack (lua_State *L, StkId oldstack, StkId newstack) { if (oldstack == newstack) return; /* stack address did not change */ L->top = (L->top - oldstack) + newstack; + lua_assert(L->ptbc == NULL); for (up = L->openupval; up != NULL; up = up->u.open.next) up->v = s2v((uplevel(up) - oldstack) + newstack); for (ci = L->ci; ci != NULL; ci = ci->previous) { diff --git a/lfunc.c b/lfunc.c index 13e44d46ef..81ac9f0aa8 100644 --- a/lfunc.c +++ b/lfunc.c @@ -155,32 +155,19 @@ static void prepcallclosemth (lua_State *L, StkId level, int status, int yy) { /* -** Try to create a to-be-closed upvalue -** (can raise a memory-allocation error) -*/ -static void trynewtbcupval (lua_State *L, void *ud) { - newupval(L, 1, cast(StkId, ud), &L->openupval); -} - - -/* -** Create a to-be-closed upvalue. If there is a memory error -** when creating the upvalue, the closing method must be called here, -** as there is no upvalue to call it later. +** Create a to-be-closed upvalue. If there is a memory allocation error, +** 'ptbc' keeps the object so it can be closed as soon as possible. +** (Since memory errors have no handler, that will happen before any +** stack reallocation.) */ void luaF_newtbcupval (lua_State *L, StkId level) { TValue *obj = s2v(level); lua_assert(L->openupval == NULL || uplevel(L->openupval) < level); if (!l_isfalse(obj)) { /* false doesn't need to be closed */ - int status; checkclosemth(L, level, obj); - status = luaD_rawrunprotected(L, trynewtbcupval, level); - if (unlikely(status != LUA_OK)) { /* memory error creating upvalue? */ - lua_assert(status == LUA_ERRMEM); - luaD_seterrorobj(L, LUA_ERRMEM, level + 1); /* save error message */ - callclosemethod(L, s2v(level), s2v(level + 1), 0); - luaD_throw(L, LUA_ERRMEM); /* throw memory error */ - } + L->ptbc = level; /* in case of allocation error */ + newupval(L, 1, level, &L->openupval); + L->ptbc = NULL; /* no errors */ } } @@ -196,11 +183,19 @@ void luaF_unlinkupval (UpVal *uv) { /* ** Close all upvalues up to the given stack level. A 'status' equal ** to NOCLOSINGMETH closes upvalues without running any __close -** metamethods. +** metamethods. If there is a pending to-be-closed value, close +** it before anything else. */ void luaF_close (lua_State *L, StkId level, int status, int yy) { UpVal *uv; StkId upl; /* stack index pointed by 'uv' */ + if (unlikely(status == LUA_ERRMEM && L->ptbc != NULL)) { + upl = L->ptbc; + L->ptbc = NULL; /* remove from "list" before closing */ + prepcallclosemth(L, upl, status, yy); + } + else + lua_assert(L->ptbc == NULL); /* must be empty for other status */ while ((uv = L->openupval) != NULL && (upl = uplevel(uv)) >= level) { TValue *slot = &uv->u.value; /* new position for value */ lua_assert(uplevel(uv) < L->top); diff --git a/lstate.c b/lstate.c index 92ccbf9b69..c708a1624b 100644 --- a/lstate.c +++ b/lstate.c @@ -253,6 +253,7 @@ static void preinit_thread (lua_State *L, global_State *g) { L->ci = NULL; L->nci = 0; L->twups = L; /* thread has no upvalues */ + L->nCcalls = 0; L->errorJmp = NULL; L->hook = NULL; L->hookmask = 0; @@ -263,6 +264,7 @@ static void preinit_thread (lua_State *L, global_State *g) { L->status = LUA_OK; L->errfunc = 0; L->oldpc = 0; + L->ptbc = NULL; } @@ -296,7 +298,6 @@ LUA_API lua_State *lua_newthread (lua_State *L) { setthvalue2s(L, L->top, L1); api_incr_top(L); preinit_thread(L1, g); - L1->nCcalls = 0; L1->hookmask = L->hookmask; L1->basehookcount = L->basehookcount; L1->hook = L->hook; @@ -363,7 +364,6 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { preinit_thread(L, g); g->allgc = obj2gco(L); /* by now, only object is the main thread */ L->next = NULL; - L->nCcalls = 0; incnny(L); /* main thread is always non yieldable */ g->frealloc = f; g->ud = ud; diff --git a/lstate.h b/lstate.h index 38248e578d..65d452643c 100644 --- a/lstate.h +++ b/lstate.h @@ -308,6 +308,7 @@ struct lua_State { int basehookcount; int hookcount; volatile l_signalT hookmask; + StkId ptbc; /* pending to-be-closed variable */ }; diff --git a/manual/manual.of b/manual/manual.of index 2fe332a4d8..89069281e1 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -4358,10 +4358,6 @@ nor modified before a corresponding call to @Lid{lua_closeslot}. This function should not be called for an index that is equal to or below an active to-be-closed index. -In the case of an out-of-memory error, -the value in the given index is immediately closed, -as if it was already marked. - Note that, both in case of errors and of a regular return, by the time the @idx{__close} metamethod runs, the @N{C stack} was already unwound, diff --git a/testes/locals.lua b/testes/locals.lua index 8506195ead..24a95d18e8 100644 --- a/testes/locals.lua +++ b/testes/locals.lua @@ -539,15 +539,17 @@ if rawget(_G, "T") then local _, msg = pcall(foo) assert(msg == "not enough memory") + local closemsg local close = func2close(function (self, msg) T.alloccount() - assert(msg == "not enough memory") + closemsg = msg end) -- set a memory limit and return a closing object to remove the limit local function enter (count) stack(10) -- reserve some stack space T.alloccount(count) + closemsg = nil return close end @@ -558,12 +560,7 @@ if rawget(_G, "T") then end local _, msg = pcall(test) - assert(msg == "not enough memory") - - -- now use metamethod for closing - close = setmetatable({}, {__close = function () - T.alloccount() - end}) + assert(msg == "not enough memory" and closemsg == "not enough memory") -- repeat test with extra closing upvalues local function test () @@ -580,7 +577,7 @@ if rawget(_G, "T") then end local _, msg = pcall(test) - assert(msg == 1000) + assert(msg == 1000 and closemsg == "not enough memory") do -- testing 'toclose' in C string buffer collectgarbage() From 0e9254dfa03d95c3aa2888cf78e9a30bc88d41bc Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 21 Jan 2021 10:27:22 -0300 Subject: [PATCH 0666/1145] Correct order of return hooks vs. close metamethods The return hook should be called only after closing variables (which are still part of the function). C functions were calling the hook before the metamethods. --- ldo.c | 24 +++++++++------ testes/locals.lua | 77 ++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 85 insertions(+), 16 deletions(-) diff --git a/ldo.c b/ldo.c index 9e3d6955a0..57d4f7d56c 100644 --- a/ldo.c +++ b/ldo.c @@ -341,7 +341,8 @@ void luaD_hookcall (lua_State *L, CallInfo *ci) { } -static StkId rethook (lua_State *L, CallInfo *ci, StkId firstres, int nres) { +static void rethook (lua_State *L, CallInfo *ci, int nres) { + StkId firstres = L->top - nres; /* index of first result */ ptrdiff_t oldtop = savestack(L, L->top); /* hook may change top */ int delta = 0; if (isLuacode(ci)) { @@ -360,7 +361,7 @@ static StkId rethook (lua_State *L, CallInfo *ci, StkId firstres, int nres) { } if (isLua(ci = ci->previous)) L->oldpc = pcRel(ci->u.l.savedpc, ci_func(ci)->p); /* update 'oldpc' */ - return restorestack(L, oldtop); + L->top = restorestack(L, oldtop); } @@ -397,7 +398,7 @@ static void moveresults (lua_State *L, StkId res, int nres, int wanted) { case 1: /* one value needed */ if (nres == 0) /* no results? */ setnilvalue(s2v(res)); /* adjust with nil */ - else + else /* at least one result */ setobjs2s(L, res, L->top - nres); /* move it to proper place */ L->top = res + 1; return; @@ -412,6 +413,8 @@ static void moveresults (lua_State *L, StkId res, int nres, int wanted) { wanted = codeNresults(wanted); /* correct value */ if (wanted == LUA_MULTRET) wanted = nres; + if (L->hookmask) /* if needed, call hook after '__close's */ + rethook(L, L->ci, nres); } break; } @@ -426,15 +429,18 @@ static void moveresults (lua_State *L, StkId res, int nres, int wanted) { /* -** Finishes a function call: calls hook if necessary, removes CallInfo, -** moves current number of results to proper place. +** Finishes a function call: calls hook if necessary, moves current +** number of results to proper place, and returns to previous call +** info. If function has to close variables, hook must be called after +** that. */ void luaD_poscall (lua_State *L, CallInfo *ci, int nres) { - if (L->hookmask) - L->top = rethook(L, ci, L->top - nres, nres); - L->ci = ci->previous; /* back to caller */ + int wanted = ci->nresults; + if (L->hookmask && !hastocloseCfunc(wanted)) + rethook(L, ci, nres); /* move results to proper place */ - moveresults(L, ci->func, nres, ci->nresults); + moveresults(L, ci->func, nres, wanted); + L->ci = ci->previous; /* back to caller (after closing variables) */ } diff --git a/testes/locals.lua b/testes/locals.lua index 24a95d18e8..a25b2b9fe6 100644 --- a/testes/locals.lua +++ b/testes/locals.lua @@ -521,6 +521,14 @@ do -- tbc inside close methods end +local function checktable (t1, t2) + assert(#t1 == #t2) + for i = 1, #t1 do + assert(t1[i] == t2[i]) + end +end + + if rawget(_G, "T") then -- memory error inside closing function @@ -632,6 +640,68 @@ if rawget(_G, "T") then print'+' end + + do + -- '__close' vs. return hooks in C functions + local trace = {} + + local function hook (event) + trace[#trace + 1] = event .. " " .. (debug.getinfo(2).name or "?") + end + + -- create tbc variables to be used by C function + local x = func2close(function (_,msg) + trace[#trace + 1] = "x" + end) + + local y = func2close(function (_,msg) + trace[#trace + 1] = "y" + end) + + debug.sethook(hook, "r") + local t = {T.testC([[ + toclose 2 # x + pushnum 10 + pushint 20 + toclose 3 # y + return 2 + ]], x, y)} + debug.sethook() + + -- hooks ran before return hook from 'testC' + checktable(trace, + {"return sethook", "y", "return ?", "x", "return ?", "return testC"}) + -- results are correct + checktable(t, {10, 20}) + end + +end + + +do -- '__close' vs. return hooks in Lua functions + local trace = {} + + local function hook (event) + trace[#trace + 1] = event .. " " .. debug.getinfo(2).name + end + + local function foo (...) + local x = func2close(function (_,msg) + trace[#trace + 1] = "x" + end) + + local y = func2close(function (_,msg) + debug.sethook(hook, "r") + end) + + return ... + end + + local t = {foo(10,20,30)} + debug.sethook() + checktable(t, {10, 20, 30}) + checktable(trace, + {"return sethook", "return close", "x", "return close", "return foo"}) end @@ -640,13 +710,6 @@ print "to-be-closed variables in coroutines" do -- yielding inside closing metamethods - local function checktable (t1, t2) - assert(#t1 == #t2) - for i = 1, #t1 do - assert(t1[i] == t2[i]) - end - end - local trace = {} local co = coroutine.wrap(function () From 1f81baffadad9d955b030a1a29b9b06042a66552 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 25 Jan 2021 10:39:18 -0300 Subject: [PATCH 0667/1145] Janitorial work Comments, code details, identation. --- lapi.c | 3 ++- lbaselib.c | 3 ++- ldo.c | 55 ++++++++++++++++++++++++++++++------------------------ lstate.h | 10 ++++++++++ 4 files changed, 45 insertions(+), 26 deletions(-) diff --git a/lapi.c b/lapi.c index 3583e9c06d..163533a2fc 100644 --- a/lapi.c +++ b/lapi.c @@ -74,7 +74,8 @@ static TValue *index2value (lua_State *L, int idx) { return &G(L)->nilvalue; /* it has no upvalues */ else { CClosure *func = clCvalue(s2v(ci->func)); - return (idx <= func->nupvalues) ? &func->upvalue[idx-1] : &G(L)->nilvalue; + return (idx <= func->nupvalues) ? &func->upvalue[idx-1] + : &G(L)->nilvalue; } } } diff --git a/lbaselib.c b/lbaselib.c index 747fd45a2f..60786b3de8 100644 --- a/lbaselib.c +++ b/lbaselib.c @@ -182,7 +182,8 @@ static int luaB_rawset (lua_State *L) { static int pushmode (lua_State *L, int oldmode) { - lua_pushstring(L, (oldmode == LUA_GCINC) ? "incremental" : "generational"); + lua_pushstring(L, (oldmode == LUA_GCINC) ? "incremental" + : "generational"); return 1; } diff --git a/ldo.c b/ldo.c index 57d4f7d56c..9076c0ede0 100644 --- a/ldo.c +++ b/ldo.c @@ -295,8 +295,8 @@ void luaD_hook (lua_State *L, int event, int line, if (hook && L->allowhook) { /* make sure there is a hook */ int mask = CIST_HOOKED; CallInfo *ci = L->ci; - ptrdiff_t top = savestack(L, L->top); - ptrdiff_t ci_top = savestack(L, ci->top); + ptrdiff_t top = savestack(L, L->top); /* preserve original 'top' */ + ptrdiff_t ci_top = savestack(L, ci->top); /* idem for 'ci->top' */ lua_Debug ar; ar.event = event; ar.currentline = line; @@ -307,7 +307,7 @@ void luaD_hook (lua_State *L, int event, int line, ci->u2.transferinfo.ntransfer = ntransfer; } luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */ - if (L->top + LUA_MINSTACK > ci->top) + if (ci->top < L->top + LUA_MINSTACK) ci->top = L->top + LUA_MINSTACK; L->allowhook = 0; /* cannot call hooks inside a hook */ ci->callstatus |= mask; @@ -329,39 +329,44 @@ void luaD_hook (lua_State *L, int event, int line, ** active. */ void luaD_hookcall (lua_State *L, CallInfo *ci) { - int hook = (ci->callstatus & CIST_TAIL) ? LUA_HOOKTAILCALL : LUA_HOOKCALL; - Proto *p; - if (!(L->hookmask & LUA_MASKCALL)) /* some other hook? */ - return; /* don't call hook */ - p = clLvalue(s2v(ci->func))->p; - L->top = ci->top; /* prepare top */ - ci->u.l.savedpc++; /* hooks assume 'pc' is already incremented */ - luaD_hook(L, hook, -1, 1, p->numparams); - ci->u.l.savedpc--; /* correct 'pc' */ + if (L->hookmask & LUA_MASKCALL) { /* is call hook on? */ + int event = (ci->callstatus & CIST_TAIL) ? LUA_HOOKTAILCALL + : LUA_HOOKCALL; + Proto *p = ci_func(ci)->p; + L->top = ci->top; /* prepare top */ + ci->u.l.savedpc++; /* hooks assume 'pc' is already incremented */ + luaD_hook(L, event, -1, 1, p->numparams); + ci->u.l.savedpc--; /* correct 'pc' */ + } } +/* +** Executes a call hook for Lua and C functions. This function is called +** whenever 'hookmask' is not zero, so it checks whether return hooks are +** active. +*/ static void rethook (lua_State *L, CallInfo *ci, int nres) { - StkId firstres = L->top - nres; /* index of first result */ - ptrdiff_t oldtop = savestack(L, L->top); /* hook may change top */ - int delta = 0; - if (isLuacode(ci)) { - Proto *p = ci_func(ci)->p; - if (p->is_vararg) - delta = ci->u.l.nextraargs + p->numparams + 1; - if (L->top < ci->top) - L->top = ci->top; /* correct top to run hook */ - } if (L->hookmask & LUA_MASKRET) { /* is return hook on? */ + StkId firstres = L->top - nres; /* index of first result */ + ptrdiff_t oldtop = savestack(L, L->top); /* hook may change top */ + int delta = 0; /* correction for vararg functions */ int ftransfer; + if (isLuacode(ci)) { + Proto *p = ci_func(ci)->p; + if (p->is_vararg) + delta = ci->u.l.nextraargs + p->numparams + 1; + if (L->top < ci->top) + L->top = ci->top; /* correct top to run hook */ + } ci->func += delta; /* if vararg, back to virtual 'func' */ ftransfer = cast(unsigned short, firstres - ci->func); luaD_hook(L, LUA_HOOKRET, -1, ftransfer, nres); /* call it */ ci->func -= delta; + L->top = restorestack(L, oldtop); } if (isLua(ci = ci->previous)) L->oldpc = pcRel(ci->u.l.savedpc, ci_func(ci)->p); /* update 'oldpc' */ - L->top = restorestack(L, oldtop); } @@ -420,7 +425,9 @@ static void moveresults (lua_State *L, StkId res, int nres, int wanted) { } firstresult = L->top - nres; /* index of first result */ /* move all results to correct place */ - for (i = 0; i < nres && i < wanted; i++) + if (nres > wanted) + nres = wanted; /* don't need more than that */ + for (i = 0; i < nres; i++) setobjs2s(L, res + i, firstresult + i); for (; i < wanted; i++) /* complete wanted number of results */ setnilvalue(s2v(res + i)); diff --git a/lstate.h b/lstate.h index 65d452643c..f3d791ab1d 100644 --- a/lstate.h +++ b/lstate.h @@ -156,6 +156,16 @@ typedef struct stringtable { /* ** Information about a call. +** About union 'u': +** - field 'l' is used only for Lua functions; +** - field 'c' is used only for C functions. +** About union 'u2': +** - field 'funcidx' is used only by C functions while doing a +** protected call; +** - field 'nyield' is used only while a function is "doing" an +** yield (from the yield until the next resume); +** - field 'transferinfo' is used only during call/returnhooks, +** before the function starts or after it ends. */ typedef struct CallInfo { StkId func; /* function index in the stack */ From 58aa09a0b91cf81779d6710d7f9d855bb9d3712f Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 26 Jan 2021 16:53:51 -0300 Subject: [PATCH 0668/1145] Small improvements in hooks - 'L->top' is set once in 'luaD_hook', instead of being set in 'luaD_hookcall' and 'rethook'; - resume discard arguments when returning after an yield inside a hook (arguments may interfere with the coroutine stack); - yield inside a hook asserts it has no arguments. --- ldo.c | 17 ++++++++--------- testes/coroutine.lua | 22 ++++++++++++++++++++++ 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/ldo.c b/ldo.c index 9076c0ede0..80c7980385 100644 --- a/ldo.c +++ b/ldo.c @@ -306,6 +306,8 @@ void luaD_hook (lua_State *L, int event, int line, ci->u2.transferinfo.ftransfer = ftransfer; ci->u2.transferinfo.ntransfer = ntransfer; } + if (isLua(ci) && L->top < ci->top) + L->top = ci->top; /* protect entire activation register */ luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */ if (ci->top < L->top + LUA_MINSTACK) ci->top = L->top + LUA_MINSTACK; @@ -333,7 +335,6 @@ void luaD_hookcall (lua_State *L, CallInfo *ci) { int event = (ci->callstatus & CIST_TAIL) ? LUA_HOOKTAILCALL : LUA_HOOKCALL; Proto *p = ci_func(ci)->p; - L->top = ci->top; /* prepare top */ ci->u.l.savedpc++; /* hooks assume 'pc' is already incremented */ luaD_hook(L, event, -1, 1, p->numparams); ci->u.l.savedpc--; /* correct 'pc' */ @@ -349,21 +350,17 @@ void luaD_hookcall (lua_State *L, CallInfo *ci) { static void rethook (lua_State *L, CallInfo *ci, int nres) { if (L->hookmask & LUA_MASKRET) { /* is return hook on? */ StkId firstres = L->top - nres; /* index of first result */ - ptrdiff_t oldtop = savestack(L, L->top); /* hook may change top */ int delta = 0; /* correction for vararg functions */ int ftransfer; - if (isLuacode(ci)) { + if (isLua(ci)) { Proto *p = ci_func(ci)->p; if (p->is_vararg) delta = ci->u.l.nextraargs + p->numparams + 1; - if (L->top < ci->top) - L->top = ci->top; /* correct top to run hook */ } ci->func += delta; /* if vararg, back to virtual 'func' */ ftransfer = cast(unsigned short, firstres - ci->func); luaD_hook(L, LUA_HOOKRET, -1, ftransfer, nres); /* call it */ ci->func -= delta; - L->top = restorestack(L, oldtop); } if (isLua(ci = ci->previous)) L->oldpc = pcRel(ci->u.l.savedpc, ci_func(ci)->p); /* update 'oldpc' */ @@ -707,8 +704,10 @@ static void resume (lua_State *L, void *ud) { lua_assert(L->status == LUA_YIELD); L->status = LUA_OK; /* mark that it is running (again) */ luaE_incCstack(L); /* control the C stack */ - if (isLua(ci)) /* yielded inside a hook? */ + if (isLua(ci)) { /* yielded inside a hook? */ + L->top = firstArg; /* discard arguments */ luaV_execute(L, ci); /* just continue running Lua code */ + } else { /* 'common' yield */ if (ci->u.c.k != NULL) { /* does it have a continuation function? */ lua_unlock(L); @@ -793,15 +792,15 @@ LUA_API int lua_yieldk (lua_State *L, int nresults, lua_KContext ctx, luaG_runerror(L, "attempt to yield from outside a coroutine"); } L->status = LUA_YIELD; + ci->u2.nyield = nresults; /* save number of results */ if (isLua(ci)) { /* inside a hook? */ lua_assert(!isLuacode(ci)); + api_check(L, nresults == 0, "hooks cannot yield values"); api_check(L, k == NULL, "hooks cannot continue after yielding"); - ci->u2.nyield = 0; /* no results */ } else { if ((ci->u.c.k = k) != NULL) /* is there a continuation? */ ci->u.c.ctx = ctx; /* save context */ - ci->u2.nyield = nresults; /* save number of results */ luaD_throw(L, LUA_YIELD); } lua_assert(ci->callstatus & CIST_HOOKED); /* must be inside a hook */ diff --git a/testes/coroutine.lua b/testes/coroutine.lua index fbeabd07f9..b36b76ea56 100644 --- a/testes/coroutine.lua +++ b/testes/coroutine.lua @@ -498,6 +498,28 @@ else assert(B // A == 7) -- fact(7) // fact(6) + do -- hooks vs. multiple values + local done + local function test (n) + done = false + return coroutine.wrap(function () + local a = {} + for i = 1, n do a[i] = i end + -- 'pushint' just to perturb the stack + T.sethook("pushint 10; yield 0", "", 1) -- yield at each op. + local a1 = {table.unpack(a)} -- must keep top between ops. + assert(#a1 == n) + for i = 1, n do assert(a[i] == i) end + done = true + end) + end + -- arguments to the coroutine are just to perturb its stack + local co = test(0); while not done do co(30) end + co = test(1); while not done do co(20, 10) end + co = test(3); while not done do co() end + co = test(100); while not done do co() end + end + local line = debug.getinfo(1, "l").currentline + 2 -- get line number local function foo () local x = 10 --<< this line is 'line' From 949187b049ce329c93d6639b91e244f2b208c807 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 28 Jan 2021 14:40:29 -0300 Subject: [PATCH 0669/1145] Optimizations for line hook The function 'changedline' tries harder to avoid calling 'luaG_getfuncline' plus small changes in the use of 'L->oldpc'. --- lcode.c | 9 --------- ldebug.c | 52 ++++++++++++++++++++++++++++++++-------------------- ldebug.h | 10 ++++++++++ ldo.c | 9 +++++---- 4 files changed, 47 insertions(+), 33 deletions(-) diff --git a/lcode.c b/lcode.c index d8d353fe4e..9741d7cd85 100644 --- a/lcode.c +++ b/lcode.c @@ -314,15 +314,6 @@ void luaK_patchtohere (FuncState *fs, int list) { } -/* -** MAXimum number of successive Instructions WiTHout ABSolute line -** information. -*/ -#if !defined(MAXIWTHABS) -#define MAXIWTHABS 120 -#endif - - /* limit for difference between lines in relative line info. */ #define LIMLINEDIFF 0x80 diff --git a/ldebug.c b/ldebug.c index 819550d76c..8dfa18cf96 100644 --- a/ldebug.c +++ b/ldebug.c @@ -33,8 +33,6 @@ #define noLuaClosure(f) ((f) == NULL || (f)->c.tt == LUA_VCCL) -/* inverse of 'pcRel' */ -#define invpcRel(pc, p) ((p)->code + (pc) + 1) static const char *funcnamefromcode (lua_State *L, CallInfo *ci, const char **name); @@ -791,16 +789,30 @@ l_noret luaG_runerror (lua_State *L, const char *fmt, ...) { /* ** Check whether new instruction 'newpc' is in a different line from -** previous instruction 'oldpc'. +** previous instruction 'oldpc'. More often than not, 'newpc' is only +** one or a few instructions after 'oldpc' (it must be after, see +** caller), so try to avoid calling 'luaG_getfuncline'. If they are +** too far apart, there is a good chance of a ABSLINEINFO in the way, +** so it goes directly to 'luaG_getfuncline'. */ static int changedline (const Proto *p, int oldpc, int newpc) { if (p->lineinfo == NULL) /* no debug information? */ return 0; - while (oldpc++ < newpc) { - if (p->lineinfo[oldpc] != 0) - return (luaG_getfuncline(p, oldpc - 1) != luaG_getfuncline(p, newpc)); + if (newpc - oldpc < MAXIWTHABS / 2) { /* not too far apart? */ + int delta = 0; /* line diference */ + int pc = oldpc; + for (;;) { + int lineinfo = p->lineinfo[++pc]; + if (lineinfo == ABSLINEINFO) + break; /* cannot compute delta; fall through */ + delta += lineinfo; + if (pc == newpc) + return (delta != 0); /* delta computed successfully */ + } } - return 0; /* no line changes between positions */ + /* either instructions are too far apart or there is an absolute line + info in the way; compute line difference explicitly */ + return (luaG_getfuncline(p, oldpc) != luaG_getfuncline(p, newpc)); } @@ -808,20 +820,19 @@ static int changedline (const Proto *p, int oldpc, int newpc) { ** Traces the execution of a Lua function. Called before the execution ** of each opcode, when debug is on. 'L->oldpc' stores the last ** instruction traced, to detect line changes. When entering a new -** function, 'npci' will be zero and will test as a new line without -** the need for 'oldpc'; so, 'oldpc' does not need to be initialized -** before. Some exceptional conditions may return to a function without -** updating 'oldpc'. In that case, 'oldpc' may be invalid; if so, it is -** reset to zero. (A wrong but valid 'oldpc' at most causes an extra -** call to a line hook.) +** function, 'npci' will be zero and will test as a new line whatever +** the value of 'oldpc'. Some exceptional conditions may return to +** a function without setting 'oldpc'. In that case, 'oldpc' may be +** invalid; if so, use zero as a valid value. (A wrong but valid 'oldpc' +** at most causes an extra call to a line hook.) +** This function is not "Protected" when called, so it should correct +** 'L->top' before calling anything that can run the GC. */ int luaG_traceexec (lua_State *L, const Instruction *pc) { CallInfo *ci = L->ci; lu_byte mask = L->hookmask; const Proto *p = ci_func(ci)->p; int counthook; - /* 'L->oldpc' may be invalid; reset it in this case */ - int oldpc = (L->oldpc < p->sizecode) ? L->oldpc : 0; if (!(mask & (LUA_MASKLINE | LUA_MASKCOUNT))) { /* no hooks? */ ci->u.l.trap = 0; /* don't need to stop again */ return 0; /* turn off 'trap' */ @@ -837,15 +848,16 @@ int luaG_traceexec (lua_State *L, const Instruction *pc) { ci->callstatus &= ~CIST_HOOKYIELD; /* erase mark */ return 1; /* do not call hook again (VM yielded, so it did not move) */ } - if (!isIT(*(ci->u.l.savedpc - 1))) - L->top = ci->top; /* prepare top */ + if (!isIT(*(ci->u.l.savedpc - 1))) /* top not being used? */ + L->top = ci->top; /* correct top */ if (counthook) luaD_hook(L, LUA_HOOKCOUNT, -1, 0, 0); /* call count hook */ if (mask & LUA_MASKLINE) { + /* 'L->oldpc' may be invalid; use zero in this case */ + int oldpc = (L->oldpc < p->sizecode) ? L->oldpc : 0; int npci = pcRel(pc, p); - if (npci == 0 || /* call linehook when enter a new function, */ - pc <= invpcRel(oldpc, p) || /* when jump back (loop), or when */ - changedline(p, oldpc, npci)) { /* enter new line */ + if (npci <= oldpc || /* call hook when jump back (loop), */ + changedline(p, oldpc, npci)) { /* or when enter new line */ int newline = luaG_getfuncline(p, npci); luaD_hook(L, LUA_HOOKLINE, newline, 0, 0); /* call line hook */ } diff --git a/ldebug.h b/ldebug.h index 55b3ae090b..8e912a8ec3 100644 --- a/ldebug.h +++ b/ldebug.h @@ -26,6 +26,16 @@ */ #define ABSLINEINFO (-0x80) + +/* +** MAXimum number of successive Instructions WiTHout ABSolute line +** information. +*/ +#if !defined(MAXIWTHABS) +#define MAXIWTHABS 120 +#endif + + LUAI_FUNC int luaG_getfuncline (const Proto *f, int pc); LUAI_FUNC const char *luaG_findlocal (lua_State *L, CallInfo *ci, int n, StkId *pos); diff --git a/ldo.c b/ldo.c index 80c7980385..e8cccccb0d 100644 --- a/ldo.c +++ b/ldo.c @@ -331,6 +331,7 @@ void luaD_hook (lua_State *L, int event, int line, ** active. */ void luaD_hookcall (lua_State *L, CallInfo *ci) { + L->oldpc = 0; /* set 'oldpc' for new function */ if (L->hookmask & LUA_MASKCALL) { /* is call hook on? */ int event = (ci->callstatus & CIST_TAIL) ? LUA_HOOKTAILCALL : LUA_HOOKCALL; @@ -343,9 +344,9 @@ void luaD_hookcall (lua_State *L, CallInfo *ci) { /* -** Executes a call hook for Lua and C functions. This function is called -** whenever 'hookmask' is not zero, so it checks whether return hooks are -** active. +** Executes a return hook for Lua and C functions and sets/corrects +** 'oldpc'. (Note that this correction is needed by the line hook, so it +** is done even when return hooks are off.) */ static void rethook (lua_State *L, CallInfo *ci, int nres) { if (L->hookmask & LUA_MASKRET) { /* is return hook on? */ @@ -363,7 +364,7 @@ static void rethook (lua_State *L, CallInfo *ci, int nres) { ci->func -= delta; } if (isLua(ci = ci->previous)) - L->oldpc = pcRel(ci->u.l.savedpc, ci_func(ci)->p); /* update 'oldpc' */ + L->oldpc = pcRel(ci->u.l.savedpc, ci_func(ci)->p); /* set 'oldpc' */ } From e500892e18e994781760819e33098322728796e8 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 2 Feb 2021 14:43:55 -0300 Subject: [PATCH 0670/1145] Optimization/simplification of 'getbaseline' By producing absolute line information at regular intervals, a simple division can compute the correct entry for a given instruction. --- lcode.c | 4 ++-- ldebug.c | 35 +++++++++++++++-------------------- ldebug.h | 4 ++-- 3 files changed, 19 insertions(+), 24 deletions(-) diff --git a/lcode.c b/lcode.c index 9741d7cd85..31f23f47a5 100644 --- a/lcode.c +++ b/lcode.c @@ -328,13 +328,13 @@ void luaK_patchtohere (FuncState *fs, int list) { static void savelineinfo (FuncState *fs, Proto *f, int line) { int linedif = line - fs->previousline; int pc = fs->pc - 1; /* last instruction coded */ - if (abs(linedif) >= LIMLINEDIFF || fs->iwthabs++ > MAXIWTHABS) { + if (abs(linedif) >= LIMLINEDIFF || fs->iwthabs++ >= MAXIWTHABS) { luaM_growvector(fs->ls->L, f->abslineinfo, fs->nabslineinfo, f->sizeabslineinfo, AbsLineInfo, MAX_INT, "lines"); f->abslineinfo[fs->nabslineinfo].pc = pc; f->abslineinfo[fs->nabslineinfo++].line = line; linedif = ABSLINEINFO; /* signal that there is absolute information */ - fs->iwthabs = 0; /* restart counter */ + fs->iwthabs = 1; /* restart counter */ } luaM_growvector(fs->ls->L, f->lineinfo, pc, f->sizelineinfo, ls_byte, MAX_INT, "opcodes"); diff --git a/ldebug.c b/ldebug.c index 8dfa18cf96..0038d1b331 100644 --- a/ldebug.c +++ b/ldebug.c @@ -46,10 +46,14 @@ static int currentpc (CallInfo *ci) { /* ** Get a "base line" to find the line corresponding to an instruction. -** For that, search the array of absolute line info for the largest saved -** instruction smaller or equal to the wanted instruction. A special -** case is when there is no absolute info or the instruction is before -** the first absolute one. +** Base lines are regularly placed at MAXIWTHABS intervals, so usually +** an integer division gets the right place. When the source file has +** large sequences of empty/comment lines, it may need extra entries, +** so the original estimate needs a correction. +** The assertion that the estimate is a lower bound for the correct base +** is valid as long as the debug info has been generated with the same +** value for MAXIWTHABS or smaller. (Previous releases use a little +** smaller value.) */ static int getbaseline (const Proto *f, int pc, int *basepc) { if (f->sizeabslineinfo == 0 || pc < f->abslineinfo[0].pc) { @@ -57,20 +61,11 @@ static int getbaseline (const Proto *f, int pc, int *basepc) { return f->linedefined; } else { - unsigned int i; - if (pc >= f->abslineinfo[f->sizeabslineinfo - 1].pc) - i = f->sizeabslineinfo - 1; /* instruction is after last saved one */ - else { /* binary search */ - unsigned int j = f->sizeabslineinfo - 1; /* pc < anchorlines[j] */ - i = 0; /* abslineinfo[i] <= pc */ - while (i < j - 1) { - unsigned int m = (j + i) / 2; - if (pc >= f->abslineinfo[m].pc) - i = m; - else - j = m; - } - } + int i = cast_uint(pc) / MAXIWTHABS - 1; /* get an estimate */ + /* estimate must be a lower bond of the correct base */ + lua_assert(i < f->sizeabslineinfo && f->abslineinfo[i].pc <= pc); + while (i + 1 < f->sizeabslineinfo && pc >= f->abslineinfo[i + 1].pc) + i++; /* low estimate; adjust it */ *basepc = f->abslineinfo[i].pc; return f->abslineinfo[i].line; } @@ -303,8 +298,8 @@ static void collectvalidlines (lua_State *L, Closure *f) { sethvalue2s(L, L->top, t); /* push it on stack */ api_incr_top(L); setbtvalue(&v); /* boolean 'true' to be the value of all indices */ - for (i = 0; i < p->sizelineinfo; i++) { /* for all lines with code */ - currentline = nextline(p, currentline, i); + for (i = 0; i < p->sizelineinfo; i++) { /* for all instructions */ + currentline = nextline(p, currentline, i); /* get its line */ luaH_setint(L, t, currentline, &v); /* table[line] = true */ } } diff --git a/ldebug.h b/ldebug.h index 8e912a8ec3..974960e99d 100644 --- a/ldebug.h +++ b/ldebug.h @@ -29,10 +29,10 @@ /* ** MAXimum number of successive Instructions WiTHout ABSolute line -** information. +** information. (A power of two allows fast divisions.) */ #if !defined(MAXIWTHABS) -#define MAXIWTHABS 120 +#define MAXIWTHABS 128 #endif From 2bfa13e520e53210b96ead88f49a9ca20c5a5d18 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 5 Feb 2021 11:00:28 -0300 Subject: [PATCH 0671/1145] Fixed some bugs around stack reallocation Long time without using HARDSTACKTESTS... --- lapi.c | 1 + ldo.c | 2 +- lfunc.c | 2 ++ lvm.c | 2 ++ 4 files changed, 6 insertions(+), 1 deletion(-) diff --git a/lapi.c b/lapi.c index 163533a2fc..27bf23da72 100644 --- a/lapi.c +++ b/lapi.c @@ -207,6 +207,7 @@ LUA_API void lua_closeslot (lua_State *L, int idx) { uplevel(L->openupval) == level, "no variable to close at given level"); luaF_close(L, level, CLOSEKTOP, 0); + level = index2stack(L, idx); /* stack may be moved */ setnilvalue(s2v(level)); lua_unlock(L); } diff --git a/ldo.c b/ldo.c index e8cccccb0d..65f0a7b9e7 100644 --- a/ldo.c +++ b/ldo.c @@ -412,12 +412,12 @@ static void moveresults (lua_State *L, StkId res, int nres, int wanted) { if (hastocloseCfunc(wanted)) { /* to-be-closed variables? */ ptrdiff_t savedres = savestack(L, res); luaF_close(L, res, CLOSEKTOP, 0); /* may change the stack */ - res = restorestack(L, savedres); wanted = codeNresults(wanted); /* correct value */ if (wanted == LUA_MULTRET) wanted = nres; if (L->hookmask) /* if needed, call hook after '__close's */ rethook(L, L->ci, nres); + res = restorestack(L, savedres); /* close and hook can move stack */ } break; } diff --git a/lfunc.c b/lfunc.c index 81ac9f0aa8..105590fc45 100644 --- a/lfunc.c +++ b/lfunc.c @@ -190,9 +190,11 @@ void luaF_close (lua_State *L, StkId level, int status, int yy) { UpVal *uv; StkId upl; /* stack index pointed by 'uv' */ if (unlikely(status == LUA_ERRMEM && L->ptbc != NULL)) { + ptrdiff_t levelrel = savestack(L, level); upl = L->ptbc; L->ptbc = NULL; /* remove from "list" before closing */ prepcallclosemth(L, upl, status, yy); + level = restorestack(L, levelrel); } else lua_assert(L->ptbc == NULL); /* must be empty for other status */ diff --git a/lvm.c b/lvm.c index d6c05bbd6b..e9b1dcddb4 100644 --- a/lvm.c +++ b/lvm.c @@ -1150,6 +1150,8 @@ void luaV_execute (lua_State *L, CallInfo *ci) { Instruction i; /* instruction being executed */ StkId ra; /* instruction's A register */ vmfetch(); +// low-level line tracing for debugging Lua +// printf("line: %d\n", luaG_getfuncline(cl->p, pcRel(pc, cl->p))); lua_assert(base == ci->func + 1); lua_assert(base <= L->top && L->top < L->stack_last); /* invalidate top for instructions not expecting it */ From dee6433a89b088a1f8da9531a92a2a2693e5dac7 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 5 Feb 2021 15:30:34 -0300 Subject: [PATCH 0672/1145] Forbid changing numerical types through compiler options 'luaconf.h' always defines options LUA_32BITS, LUA_C89_NUMBERS, LUA_INT_TYPE, and LUA_FLOAT_TYPE (using 0/1 for the first two), to avoid they being set through compiler options. (It is too easy to forget these options when compiling other software that interoperates with Lua.) --- luaconf.h | 83 ++++++++++++++++++++++++++++--------------------------- 1 file changed, 42 insertions(+), 41 deletions(-) diff --git a/luaconf.h b/luaconf.h index d9cf18ca1d..a44858c4d5 100644 --- a/luaconf.h +++ b/luaconf.h @@ -16,13 +16,13 @@ ** =================================================================== ** General Configuration File for Lua ** -** Some definitions here can be changed externally, through the -** compiler (e.g., with '-D' options). Those are protected by -** '#if !defined' guards. However, several other definitions should -** be changed directly here, either because they affect the Lua -** ABI (by making the changes here, you ensure that all software -** connected to Lua, such as C libraries, will be compiled with the -** same configuration); or because they are seldom changed. +** Some definitions here can be changed externally, through the compiler +** (e.g., with '-D' options): They are commented out or protected +** by '#if !defined' guards. However, several other definitions +** should be changed directly here, either because they affect the +** Lua ABI (by making the changes here, you ensure that all software +** connected to Lua, such as C libraries, will be compiled with the same +** configuration); or because they are seldom changed. ** ** Search for "@@" to find all configurable definitions. ** =================================================================== @@ -81,26 +81,12 @@ /* ** {================================================================== -** Configuration for Number types. +** Configuration for Number types. These options should not be +** set externally, because any other code connected to Lua must +** use the same configuration. ** =================================================================== */ -/* -@@ LUA_32BITS enables Lua with 32-bit integers and 32-bit floats. -*/ -/* #define LUA_32BITS */ - - -/* -@@ LUA_C89_NUMBERS ensures that Lua uses the largest types available for -** C89 ('long' and 'double'); Windows always has '__int64', so it does -** not need to use this case. -*/ -#if defined(LUA_USE_C89) && !defined(LUA_USE_WINDOWS) -#define LUA_C89_NUMBERS -#endif - - /* @@ LUA_INT_TYPE defines the type for Lua integers. @@ LUA_FLOAT_TYPE defines the type for Lua floats. @@ -121,7 +107,31 @@ #define LUA_FLOAT_DOUBLE 2 #define LUA_FLOAT_LONGDOUBLE 3 -#if defined(LUA_32BITS) /* { */ + +/* Default configuration ('long long' and 'double', for 64-bit Lua) */ +#define LUA_INT_DEFAULT LUA_INT_LONGLONG +#define LUA_FLOAT_DEFAULT LUA_FLOAT_DOUBLE + + +/* +@@ LUA_32BITS enables Lua with 32-bit integers and 32-bit floats. +*/ +#define LUA_32BITS 0 + + +/* +@@ LUA_C89_NUMBERS ensures that Lua uses the largest types available for +** C89 ('long' and 'double'); Windows always has '__int64', so it does +** not need to use this case. +*/ +#if defined(LUA_USE_C89) && !defined(LUA_USE_WINDOWS) +#define LUA_C89_NUMBERS 1 +#else +#define LUA_C89_NUMBERS 0 +#endif + + +#if LUA_32BITS /* { */ /* ** 32-bit integers and 'float' */ @@ -132,26 +142,21 @@ #endif #define LUA_FLOAT_TYPE LUA_FLOAT_FLOAT -#elif defined(LUA_C89_NUMBERS) /* }{ */ +#elif LUA_C89_NUMBERS /* }{ */ /* ** largest types available for C89 ('long' and 'double') */ #define LUA_INT_TYPE LUA_INT_LONG #define LUA_FLOAT_TYPE LUA_FLOAT_DOUBLE -#endif /* } */ +#else /* }{ */ +/* use defaults */ +#define LUA_INT_TYPE LUA_INT_DEFAULT +#define LUA_FLOAT_TYPE LUA_FLOAT_DEFAULT -/* -** default configuration for 64-bit Lua ('long long' and 'double') -*/ -#if !defined(LUA_INT_TYPE) -#define LUA_INT_TYPE LUA_INT_LONGLONG -#endif +#endif /* } */ -#if !defined(LUA_FLOAT_TYPE) -#define LUA_FLOAT_TYPE LUA_FLOAT_DOUBLE -#endif /* }================================================================== */ @@ -373,14 +378,13 @@ /* ** {================================================================== -** Configuration for Numbers. +** Configuration for Numbers (low-level part). ** Change these definitions if no predefined LUA_FLOAT_* / LUA_INT_* ** satisfy your needs. ** =================================================================== */ /* -@@ LUA_NUMBER is the floating-point type used by Lua. @@ LUAI_UACNUMBER is the result of a 'default argument promotion' @@ over a floating number. @@ l_floatatt(x) corrects float attribute 'x' to the proper float type @@ -473,10 +477,7 @@ /* -@@ LUA_INTEGER is the integer type used by Lua. -** @@ LUA_UNSIGNED is the unsigned version of LUA_INTEGER. -** @@ LUAI_UACINT is the result of a 'default argument promotion' @@ over a LUA_INTEGER. @@ LUA_INTEGER_FRMLEN is the length modifier for reading/writing integers. From c63e5d212bc5dec1b1c749e3f07b42cd83081826 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 5 Feb 2021 17:51:25 -0300 Subject: [PATCH 0673/1145] New macro 'completestate' --- lapi.c | 2 +- lmem.c | 4 ++-- lstate.c | 6 ++---- lstate.h | 6 ++++++ 4 files changed, 11 insertions(+), 7 deletions(-) diff --git a/lapi.c b/lapi.c index 27bf23da72..41e6b86dc9 100644 --- a/lapi.c +++ b/lapi.c @@ -39,7 +39,7 @@ const char lua_ident[] = /* -** Test for a valid index. +** Test for a valid index (one that is not the 'nilvalue'). ** '!ttisnil(o)' implies 'o != &G(L)->nilvalue', so it is not needed. ** However, it covers the most common cases in a faster way. */ diff --git a/lmem.c b/lmem.c index 43739bffd1..4822a0eaa3 100644 --- a/lmem.c +++ b/lmem.c @@ -29,7 +29,7 @@ ** a full GC cycle at every allocation.) */ static void *firsttry (global_State *g, void *block, size_t os, size_t ns) { - if (ttisnil(&g->nilvalue) && ns > os) + if (completestate(g) && ns > os) return NULL; /* fail */ else /* normal allocation */ return (*g->frealloc)(g->ud, block, os, ns); @@ -146,7 +146,7 @@ void luaM_free_ (lua_State *L, void *block, size_t osize) { static void *tryagain (lua_State *L, void *block, size_t osize, size_t nsize) { global_State *g = G(L); - if (ttisnil(&g->nilvalue)) { /* is state fully build? */ + if (completestate(g)) { /* is state fully build? */ luaC_fullgc(L, 1); /* try to free some memory... */ return (*g->frealloc)(g->ud, block, osize, nsize); /* try again */ } diff --git a/lstate.c b/lstate.c index c708a1624b..52336f44b4 100644 --- a/lstate.c +++ b/lstate.c @@ -226,8 +226,6 @@ static void init_registry (lua_State *L, global_State *g) { /* ** open parts of the state that may cause memory-allocation errors. -** ('g->nilvalue' being a nil value flags that the state was completely -** build.) */ static void f_luaopen (lua_State *L, void *ud) { global_State *g = G(L); @@ -238,7 +236,7 @@ static void f_luaopen (lua_State *L, void *ud) { luaT_init(L); luaX_init(L); g->gcrunning = 1; /* allow gc */ - setnilvalue(&g->nilvalue); + setnilvalue(&g->nilvalue); /* now state is complete */ luai_userstateopen(L); } @@ -272,7 +270,7 @@ static void close_state (lua_State *L) { global_State *g = G(L); luaD_closeprotected(L, 0, LUA_OK); /* close all upvalues */ luaC_freeallobjects(L); /* collect all objects */ - if (ttisnil(&g->nilvalue)) /* closing a fully built state? */ + if (completestate(g)) /* closing a fully built state? */ luai_userstateclose(L); luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size); freestack(L); diff --git a/lstate.h b/lstate.h index f3d791ab1d..5ef55355d0 100644 --- a/lstate.h +++ b/lstate.h @@ -324,6 +324,12 @@ struct lua_State { #define G(L) (L->l_G) +/* +** 'g->nilvalue' being a nil value flags that the state was completely +** build. +*/ +#define completestate(g) ttisnil(&g->nilvalue) + /* ** Union of all collectable objects (only for conversions) From 4e47f81188d37e29027158b76271d02a781242e2 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 9 Feb 2021 14:00:05 -0300 Subject: [PATCH 0674/1145] New implementation for to-be-closed variables To-be-closed variables are linked in their own list, embedded into the stack elements. (Due to alignment, this information does not change the size of the stack elements in most architectures.) This new list does not produce garbage and avoids memory errors when creating tbc variables. --- lapi.c | 9 +++---- ldo.c | 2 +- lfunc.c | 66 ++++++++++++++++++++++++----------------------- lfunc.h | 9 ++----- lobject.h | 10 ++++++- lstate.c | 15 ++++++----- lstate.h | 2 +- ltests.c | 1 + lvm.c | 6 ++--- testes/locals.lua | 49 +++++++++++++++++++++++++++++------ 10 files changed, 103 insertions(+), 66 deletions(-) diff --git a/lapi.c b/lapi.c index 41e6b86dc9..a9cf2fdb0a 100644 --- a/lapi.c +++ b/lapi.c @@ -192,9 +192,8 @@ LUA_API void lua_settop (lua_State *L, int idx) { if (diff < 0 && hastocloseCfunc(ci->nresults)) luaF_close(L, L->top + diff, CLOSEKTOP, 0); #endif + api_check(L, L->tbclist < L->top + diff, "cannot pop an unclosed slot"); L->top += diff; - api_check(L, L->openupval == NULL || uplevel(L->openupval) < L->top, - "cannot pop an unclosed slot"); lua_unlock(L); } @@ -203,8 +202,7 @@ LUA_API void lua_closeslot (lua_State *L, int idx) { StkId level; lua_lock(L); level = index2stack(L, idx); - api_check(L, hastocloseCfunc(L->ci->nresults) && L->openupval != NULL && - uplevel(L->openupval) == level, + api_check(L, hastocloseCfunc(L->ci->nresults) && L->tbclist == level, "no variable to close at given level"); luaF_close(L, level, CLOSEKTOP, 0); level = index2stack(L, idx); /* stack may be moved */ @@ -1266,8 +1264,7 @@ LUA_API void lua_toclose (lua_State *L, int idx) { lua_lock(L); o = index2stack(L, idx); nresults = L->ci->nresults; - api_check(L, L->openupval == NULL || uplevel(L->openupval) <= o, - "marked index below or equal new one"); + api_check(L, L->tbclist < o, "given index below or equal a marked one"); luaF_newtbcupval(L, o); /* create new to-be-closed upvalue */ if (!hastocloseCfunc(nresults)) /* function not marked yet? */ L->ci->nresults = codeNresults(nresults); /* mark it */ diff --git a/ldo.c b/ldo.c index 65f0a7b9e7..bc7212c61e 100644 --- a/ldo.c +++ b/ldo.c @@ -163,7 +163,7 @@ static void correctstack (lua_State *L, StkId oldstack, StkId newstack) { if (oldstack == newstack) return; /* stack address did not change */ L->top = (L->top - oldstack) + newstack; - lua_assert(L->ptbc == NULL); + L->tbclist = (L->tbclist - oldstack) + newstack; for (up = L->openupval; up != NULL; up = up->u.open.next) up->v = s2v((uplevel(up) - oldstack) + newstack); for (ci = L->ci; ci != NULL; ci = ci->previous) { diff --git a/lfunc.c b/lfunc.c index 105590fc45..b4c04bd0c3 100644 --- a/lfunc.c +++ b/lfunc.c @@ -120,11 +120,11 @@ static void callclosemethod (lua_State *L, TValue *obj, TValue *err, int yy) { /* -** Check whether 'obj' has a close metamethod and raise an error -** if not. +** Check whether object at given level has a close metamethod and raise +** an error if not. */ -static void checkclosemth (lua_State *L, StkId level, const TValue *obj) { - const TValue *tm = luaT_gettmbyobj(L, obj, TM_CLOSE); +static void checkclosemth (lua_State *L, StkId level) { + const TValue *tm = luaT_gettmbyobj(L, s2v(level), TM_CLOSE); if (ttisnil(tm)) { /* no metamethod? */ int idx = cast_int(level - L->ci->func); /* variable index */ const char *vname = luaG_findlocal(L, L->ci, idx, NULL); @@ -155,20 +155,21 @@ static void prepcallclosemth (lua_State *L, StkId level, int status, int yy) { /* -** Create a to-be-closed upvalue. If there is a memory allocation error, -** 'ptbc' keeps the object so it can be closed as soon as possible. -** (Since memory errors have no handler, that will happen before any -** stack reallocation.) +** Insert a variable in the list of to-be-closed variables. */ void luaF_newtbcupval (lua_State *L, StkId level) { - TValue *obj = s2v(level); - lua_assert(L->openupval == NULL || uplevel(L->openupval) < level); - if (!l_isfalse(obj)) { /* false doesn't need to be closed */ - checkclosemth(L, level, obj); - L->ptbc = level; /* in case of allocation error */ - newupval(L, 1, level, &L->openupval); - L->ptbc = NULL; /* no errors */ + lua_assert(level > L->tbclist); + if (l_isfalse(s2v(level))) + return; /* false doesn't need to be closed */ + checkclosemth(L, level); /* value must have a close method */ + while (level - L->tbclist > USHRT_MAX) { /* is delta too large? */ + L->tbclist += USHRT_MAX; /* create a dummy node at maximum delta */ + L->tbclist->tbclist.delta = USHRT_MAX; + L->tbclist->tbclist.isdummy = 1; } + level->tbclist.delta = level - L->tbclist; + level->tbclist.isdummy = 0; + L->tbclist = level; } @@ -181,23 +182,11 @@ void luaF_unlinkupval (UpVal *uv) { /* -** Close all upvalues up to the given stack level. A 'status' equal -** to NOCLOSINGMETH closes upvalues without running any __close -** metamethods. If there is a pending to-be-closed value, close -** it before anything else. +** Close all upvalues up to the given stack level. */ -void luaF_close (lua_State *L, StkId level, int status, int yy) { +void luaF_closeupval (lua_State *L, StkId level) { UpVal *uv; StkId upl; /* stack index pointed by 'uv' */ - if (unlikely(status == LUA_ERRMEM && L->ptbc != NULL)) { - ptrdiff_t levelrel = savestack(L, level); - upl = L->ptbc; - L->ptbc = NULL; /* remove from "list" before closing */ - prepcallclosemth(L, upl, status, yy); - level = restorestack(L, levelrel); - } - else - lua_assert(L->ptbc == NULL); /* must be empty for other status */ while ((uv = L->openupval) != NULL && (upl = uplevel(uv)) >= level) { TValue *slot = &uv->u.value; /* new position for value */ lua_assert(uplevel(uv) < L->top); @@ -208,9 +197,22 @@ void luaF_close (lua_State *L, StkId level, int status, int yy) { nw2black(uv); /* closed upvalues cannot be gray */ luaC_barrier(L, uv, slot); } - if (uv->tbc && status != NOCLOSINGMETH) { - ptrdiff_t levelrel = savestack(L, level); - prepcallclosemth(L, upl, status, yy); /* may change the stack */ + } +} + + +/* +** Close all upvalues and to-be-closed variables up to the given stack +** level. +*/ +void luaF_close (lua_State *L, StkId level, int status, int yy) { + ptrdiff_t levelrel = savestack(L, level); + luaF_closeupval(L, level); /* first, close the upvalues */ + while (L->tbclist >= level) { /* traverse tbc's down to that level */ + StkId tbc = L->tbclist; /* get variable index */ + L->tbclist -= tbc->tbclist.delta; /* remove it from list */ + if (!tbc->tbclist.isdummy) { /* not a dummy entry? */ + prepcallclosemth(L, tbc, status, yy); /* close variable */ level = restorestack(L, levelrel); } } diff --git a/lfunc.h b/lfunc.h index 2e6df53592..dc1cebccd1 100644 --- a/lfunc.h +++ b/lfunc.h @@ -42,15 +42,9 @@ #define MAXMISS 10 -/* -** Special "status" for 'luaF_close' -*/ - -/* close upvalues without running their closing methods */ -#define NOCLOSINGMETH (-1) /* special status to close upvalues preserving the top of the stack */ -#define CLOSEKTOP (-2) +#define CLOSEKTOP (-1) LUAI_FUNC Proto *luaF_newproto (lua_State *L); @@ -59,6 +53,7 @@ LUAI_FUNC LClosure *luaF_newLclosure (lua_State *L, int nupvals); LUAI_FUNC void luaF_initupvals (lua_State *L, LClosure *cl); LUAI_FUNC UpVal *luaF_findupval (lua_State *L, StkId level); LUAI_FUNC void luaF_newtbcupval (lua_State *L, StkId level); +LUAI_FUNC void luaF_closeupval (lua_State *L, StkId level); LUAI_FUNC void luaF_close (lua_State *L, StkId level, int status, int yy); LUAI_FUNC void luaF_unlinkupval (UpVal *uv); LUAI_FUNC void luaF_freeproto (lua_State *L, Proto *f); diff --git a/lobject.h b/lobject.h index 470b17d5f5..1a7a737250 100644 --- a/lobject.h +++ b/lobject.h @@ -136,10 +136,18 @@ typedef struct TValue { /* -** Entries in the Lua stack +** Entries in a Lua stack. Field 'tbclist' forms a list of all +** to-be-closed variables active in this stack. Dummy entries are +** used when the distance between two tbc variables does not fit +** in an unsigned short. */ typedef union StackValue { TValue val; + struct { + TValuefields; + lu_byte isdummy; + unsigned short delta; + } tbclist; } StackValue; diff --git a/lstate.c b/lstate.c index 52336f44b4..38078521e9 100644 --- a/lstate.c +++ b/lstate.c @@ -181,6 +181,7 @@ static void stack_init (lua_State *L1, lua_State *L) { int i; CallInfo *ci; /* initialize stack array */ L1->stack = luaM_newvector(L, BASIC_STACK_SIZE + EXTRA_STACK, StackValue); + L1->tbclist = L1->stack; for (i = 0; i < BASIC_STACK_SIZE + EXTRA_STACK; i++) setnilvalue(s2v(L1->stack + i)); /* erase new stack */ L1->top = L1->stack; @@ -262,16 +263,18 @@ static void preinit_thread (lua_State *L, global_State *g) { L->status = LUA_OK; L->errfunc = 0; L->oldpc = 0; - L->ptbc = NULL; } static void close_state (lua_State *L) { global_State *g = G(L); - luaD_closeprotected(L, 0, LUA_OK); /* close all upvalues */ - luaC_freeallobjects(L); /* collect all objects */ - if (completestate(g)) /* closing a fully built state? */ + if (!completestate(g)) /* closing a partially built state? */ + luaC_freeallobjects(L); /* jucst collect its objects */ + else { /* closing a fully built state */ + luaD_closeprotected(L, 1, LUA_OK); /* close all upvalues */ + luaC_freeallobjects(L); /* collect all objects */ luai_userstateclose(L); + } luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size); freestack(L); lua_assert(gettotalbytes(g) == sizeof(LG)); @@ -312,7 +315,7 @@ LUA_API lua_State *lua_newthread (lua_State *L) { void luaE_freethread (lua_State *L, lua_State *L1) { LX *l = fromstate(L1); - luaF_close(L1, L1->stack, NOCLOSINGMETH, 0); /* close all upvalues */ + luaF_closeupval(L1, L1->stack); /* close all upvalues */ lua_assert(L1->openupval == NULL); luai_userstatefree(L, L1); freestack(L1); @@ -327,7 +330,7 @@ int luaE_resetthread (lua_State *L, int status) { ci->callstatus = CIST_C; if (status == LUA_YIELD) status = LUA_OK; - status = luaD_closeprotected(L, 0, status); + status = luaD_closeprotected(L, 1, status); if (status != LUA_OK) /* errors? */ luaD_seterrorobj(L, status, L->stack + 1); else diff --git a/lstate.h b/lstate.h index 5ef55355d0..b6ade7c730 100644 --- a/lstate.h +++ b/lstate.h @@ -307,6 +307,7 @@ struct lua_State { StkId stack_last; /* end of stack (last element + 1) */ StkId stack; /* stack base */ UpVal *openupval; /* list of open upvalues in this stack */ + StkId tbclist; /* list of to-be-closed variables */ GCObject *gclist; struct lua_State *twups; /* list of threads with open upvalues */ struct lua_longjmp *errorJmp; /* current error recover point */ @@ -318,7 +319,6 @@ struct lua_State { int basehookcount; int hookcount; volatile l_signalT hookmask; - StkId ptbc; /* pending to-be-closed variable */ }; diff --git a/ltests.c b/ltests.c index 9c13338a8a..da95d0272c 100644 --- a/ltests.c +++ b/ltests.c @@ -446,6 +446,7 @@ static void checkstack (global_State *g, lua_State *L1) { for (uv = L1->openupval; uv != NULL; uv = uv->u.open.next) assert(upisopen(uv)); /* must be open */ assert(L1->top <= L1->stack_last); + assert(L1->tbclist <= L1->top); for (ci = L1->ci; ci != NULL; ci = ci->previous) { assert(ci->top <= L1->stack_last); assert(lua_checkpc(ci)); diff --git a/lvm.c b/lvm.c index e9b1dcddb4..1252ecbfae 100644 --- a/lvm.c +++ b/lvm.c @@ -1635,10 +1635,8 @@ void luaV_execute (lua_State *L, CallInfo *ci) { b = cast_int(L->top - ra); savepc(ci); /* several calls here can raise errors */ if (TESTARG_k(i)) { - /* close upvalues from current call; the compiler ensures - that there are no to-be-closed variables here, so this - call cannot change the stack */ - luaF_close(L, base, NOCLOSINGMETH, 0); + luaF_closeupval(L, base); /* close upvalues from current call */ + lua_assert(L->tbclist < base); /* no pending tbc variables */ lua_assert(base == ci->func + 1); } while (!ttisfunction(s2v(ra))) { /* not a function? */ diff --git a/testes/locals.lua b/testes/locals.lua index a25b2b9fe6..446ec13a1b 100644 --- a/testes/locals.lua +++ b/testes/locals.lua @@ -529,6 +529,40 @@ local function checktable (t1, t2) end +do -- test for tbc variable high in the stack + + -- function to force a stack overflow + local function overflow (n) + overflow(n + 1) + end + + -- error handler will create tbc variable handling a stack overflow, + -- high in the stack + local function errorh (m) + assert(string.find(m, "stack overflow")) + local x = func2close(function (o) o[1] = 10 end) + return x + end + + local flag + local st, obj + -- run test in a coroutine so as not to swell the main stack + local co = coroutine.wrap(function () + -- tbc variable down the stack + local y = func2close(function (obj, msg) + assert(msg == nil) + obj[1] = 100 + flag = obj + end) + collectgarbage("stop") + st, obj = xpcall(overflow, errorh, 0) + collectgarbage("restart") + end) + co() + assert(not st and obj[1] == 10 and flag[1] == 100) +end + + if rawget(_G, "T") then -- memory error inside closing function @@ -563,13 +597,13 @@ if rawget(_G, "T") then local function test () local x = enter(0) -- set a memory limit - -- creation of previous upvalue will raise a memory error - assert(false) -- should not run + local y = {} -- raise a memory error end local _, msg = pcall(test) assert(msg == "not enough memory" and closemsg == "not enough memory") + -- repeat test with extra closing upvalues local function test () local xxx = func2close(function (self, msg) @@ -580,8 +614,7 @@ if rawget(_G, "T") then assert(msg == "not enough memory"); end) local x = enter(0) -- set a memory limit - -- creation of previous upvalue will raise a memory error - os.exit(false) -- should not run + local y = {} -- raise a memory error end local _, msg = pcall(test) @@ -607,7 +640,7 @@ if rawget(_G, "T") then -- concat this table needs two buffer resizes (one for each 's') local a = {s, s} - collectgarbage() + collectgarbage(); collectgarbage() m = T.totalmem() collectgarbage("stop") @@ -630,7 +663,7 @@ if rawget(_G, "T") then -- second buffer was released by 'toclose' assert(T.totalmem() - m <= extra) - -- userdata, upvalue, buffer, buffer, final string + -- userdata, buffer, buffer, final string T.totalmem(m + 4*lim + extra) assert(#table.concat(a) == 2*lim) @@ -753,8 +786,8 @@ do checktable({co()}, {true, 10, 20, 30}) checktable(trace, {"nowX", "z1", "z2", "nowY", "y1", "y2", "x1", "x2"}) -end - +end + do -- yielding inside closing metamethods after an error From f79ccdca9bbe9d486d91a44a4464b99ce38de0e2 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 10 Feb 2021 14:11:51 -0300 Subject: [PATCH 0675/1145] Eases the use of clang in the makefile New definition in the makefile for warnings that are valid for gcc but not for clang (CWARNGCC). --- makefile | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/makefile b/makefile index 9e537dcc90..7cfcbfe198 100644 --- a/makefile +++ b/makefile @@ -5,7 +5,7 @@ # Warnings valid for both C and C++ CWARNSCPP= \ - -fmax-errors=5 \ + -Wfatal-errors \ -Wextra \ -Wshadow \ -Wsign-compare \ @@ -14,8 +14,6 @@ CWARNSCPP= \ -Wredundant-decls \ -Wdisabled-optimization \ -Wdouble-promotion \ - -Wlogical-op \ - -Wno-aggressive-loop-optimizations \ # the next warnings might be useful sometimes, # but usually they generate too much noise # -Werror \ @@ -26,6 +24,13 @@ CWARNSCPP= \ # -Wformat=2 \ # -Wcast-qual \ + +# Warnings for gcc, not valid for clang +CWARNGCC= \ + -Wlogical-op \ + -Wno-aggressive-loop-optimizations \ + + # The next warnings are neither valid nor needed for C++ CWARNSC= -Wdeclaration-after-statement \ -Wmissing-prototypes \ @@ -35,7 +40,7 @@ CWARNSC= -Wdeclaration-after-statement \ -Wold-style-definition \ -CWARNS= $(CWARNSCPP) $(CWARNSC) +CWARNS= $(CWARNSCPP) $(CWARNSC) $(CWARNGCC) # Some useful compiler options for internal tests: # -DLUAI_ASSERT turns on all assertions inside Lua. From bc970005ce2e258e29a5c315ea4e49f76a66586e Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 12 Feb 2021 13:36:30 -0300 Subject: [PATCH 0676/1145] '__close' methods can yield in the return of a C function When, inside a coroutine, a C function with to-be-closed slots return, the corresponding metamethods can yield. ('__close' metamethods called through 'lua_closeslot' still cannot yield, as there is no continuation to go when resuming.) --- lapi.h | 2 ++ ldo.c | 72 ++++++++++++++++++++++++++------------------ lstate.h | 12 +++++--- manual/manual.of | 3 ++ testes/locals.lua | 76 ++++++++++++++++++++++++++++++++++++++++++++++- 5 files changed, 131 insertions(+), 34 deletions(-) diff --git a/lapi.h b/lapi.h index 41216b2709..9e99cc4482 100644 --- a/lapi.h +++ b/lapi.h @@ -42,6 +42,8 @@ #define hastocloseCfunc(n) ((n) < LUA_MULTRET) +/* Map [-1, inf) (range of 'nresults') into (-inf, -2] */ #define codeNresults(n) (-(n) - 3) +#define decodeNresults(n) (-(n) - 3) #endif diff --git a/ldo.c b/ldo.c index bc7212c61e..5587b602fd 100644 --- a/ldo.c +++ b/ldo.c @@ -408,24 +408,27 @@ static void moveresults (lua_State *L, StkId res, int nres, int wanted) { case LUA_MULTRET: wanted = nres; /* we want all results */ break; - default: /* multiple results (or to-be-closed variables) */ + default: /* two/more results and/or to-be-closed variables */ if (hastocloseCfunc(wanted)) { /* to-be-closed variables? */ ptrdiff_t savedres = savestack(L, res); - luaF_close(L, res, CLOSEKTOP, 0); /* may change the stack */ - wanted = codeNresults(wanted); /* correct value */ - if (wanted == LUA_MULTRET) - wanted = nres; + L->ci->callstatus |= CIST_CLSRET; /* in case of yields */ + L->ci->u2.nres = nres; + luaF_close(L, res, CLOSEKTOP, 1); + L->ci->callstatus &= ~CIST_CLSRET; if (L->hookmask) /* if needed, call hook after '__close's */ rethook(L, L->ci, nres); res = restorestack(L, savedres); /* close and hook can move stack */ + wanted = decodeNresults(wanted); + if (wanted == LUA_MULTRET) + wanted = nres; /* we want all results */ } break; } + /* generic case */ firstresult = L->top - nres; /* index of first result */ - /* move all results to correct place */ - if (nres > wanted) - nres = wanted; /* don't need more than that */ - for (i = 0; i < nres; i++) + if (nres > wanted) /* extra results? */ + nres = wanted; /* don't need them */ + for (i = 0; i < nres; i++) /* move all results to correct place */ setobjs2s(L, res + i, firstresult + i); for (; i < wanted; i++) /* complete wanted number of results */ setnilvalue(s2v(res + i)); @@ -445,6 +448,9 @@ void luaD_poscall (lua_State *L, CallInfo *ci, int nres) { rethook(L, ci, nres); /* move results to proper place */ moveresults(L, ci->func, nres, wanted); + /* function cannot be in any of these cases when returning */ + lua_assert(!(ci->callstatus & + (CIST_HOOKED | CIST_YPCALL | CIST_FIN | CIST_TRAN | CIST_CLSRET))); L->ci = ci->previous; /* back to caller (after closing variables) */ } @@ -615,28 +621,36 @@ static int finishpcallk (lua_State *L, CallInfo *ci) { /* ** Completes the execution of a C function interrupted by an yield. -** The interruption must have happened while the function was -** executing 'lua_callk' or 'lua_pcallk'. In the second case, the -** call to 'finishpcallk' finishes the interrupted execution of -** 'lua_pcallk'. After that, it calls the continuation of the -** interrupted function and finally it completes the job of the -** 'luaD_call' that called the function. -** In the call to 'adjustresults', we do not know the number of -** results of the function called by 'lua_callk'/'lua_pcallk', -** so we are conservative and use LUA_MULTRET (always adjust). +** The interruption must have happened while the function was either +** closing its tbc variables in 'moveresults' or executing +** 'lua_callk'/'lua_pcallk'. In the first case, it just redoes +** 'luaD_poscall'. In the second case, the call to 'finishpcallk' +** finishes the interrupted execution of 'lua_pcallk'. After that, it +** calls the continuation of the interrupted function and finally it +** completes the job of the 'luaD_call' that called the function. In +** the call to 'adjustresults', we do not know the number of results +** of the function called by 'lua_callk'/'lua_pcallk', so we are +** conservative and use LUA_MULTRET (always adjust). */ static void finishCcall (lua_State *L, CallInfo *ci) { - int n; - int status = LUA_YIELD; /* default if there were no errors */ - /* must have a continuation and must be able to call it */ - lua_assert(ci->u.c.k != NULL && yieldable(L)); - if (ci->callstatus & CIST_YPCALL) /* was inside a 'lua_pcallk'? */ - status = finishpcallk(L, ci); /* finish it */ - adjustresults(L, LUA_MULTRET); /* finish 'lua_callk' */ - lua_unlock(L); - n = (*ci->u.c.k)(L, status, ci->u.c.ctx); /* call continuation */ - lua_lock(L); - api_checknelems(L, n); + int n; /* actual number of results from C function */ + if (ci->callstatus & CIST_CLSRET) { /* was returning? */ + lua_assert(hastocloseCfunc(ci->nresults)); + n = ci->u2.nres; /* just redo 'luaD_poscall' */ + /* don't need to reset CIST_CLSRET, as it will be set again anyway */ + } + else { + int status = LUA_YIELD; /* default if there were no errors */ + /* must have a continuation and must be able to call it */ + lua_assert(ci->u.c.k != NULL && yieldable(L)); + if (ci->callstatus & CIST_YPCALL) /* was inside a 'lua_pcallk'? */ + status = finishpcallk(L, ci); /* finish it */ + adjustresults(L, LUA_MULTRET); /* finish 'lua_callk' */ + lua_unlock(L); + n = (*ci->u.c.k)(L, status, ci->u.c.ctx); /* call continuation */ + lua_lock(L); + api_checknelems(L, n); + } luaD_poscall(L, ci, n); /* finish 'luaD_call' */ } diff --git a/lstate.h b/lstate.h index b6ade7c730..0322e2c665 100644 --- a/lstate.h +++ b/lstate.h @@ -164,6 +164,8 @@ typedef struct stringtable { ** protected call; ** - field 'nyield' is used only while a function is "doing" an ** yield (from the yield until the next resume); +** - field 'nres' is used only while closing tbc variables when +** returning from a C function; ** - field 'transferinfo' is used only during call/returnhooks, ** before the function starts or after it ends. */ @@ -186,6 +188,7 @@ typedef struct CallInfo { union { int funcidx; /* called-function index */ int nyield; /* number of values yielded */ + int nres; /* number of values returned */ struct { /* info about transferred values (for call/return hooks) */ unsigned short ftransfer; /* offset of first value transferred */ unsigned short ntransfer; /* number of values transferred */ @@ -203,15 +206,16 @@ typedef struct CallInfo { #define CIST_C (1<<1) /* call is running a C function */ #define CIST_FRESH (1<<2) /* call is on a fresh "luaV_execute" frame */ #define CIST_HOOKED (1<<3) /* call is running a debug hook */ -#define CIST_YPCALL (1<<4) /* call is a yieldable protected call */ +#define CIST_YPCALL (1<<4) /* doing a yieldable protected call */ #define CIST_TAIL (1<<5) /* call was tail called */ #define CIST_HOOKYIELD (1<<6) /* last hook called yielded */ #define CIST_FIN (1<<7) /* call is running a finalizer */ #define CIST_TRAN (1<<8) /* 'ci' has transfer information */ -/* Bits 9-11 are used for CIST_RECST (see below) */ -#define CIST_RECST 9 +#define CIST_CLSRET (1<<9) /* function is closing tbc variables */ +/* Bits 10-12 are used for CIST_RECST (see below) */ +#define CIST_RECST 10 #if defined(LUA_COMPAT_LT_LE) -#define CIST_LEQ (1<<12) /* using __lt for __le */ +#define CIST_LEQ (1<<13) /* using __lt for __le */ #endif diff --git a/manual/manual.of b/manual/manual.of index 89069281e1..e7040b2b10 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -3102,6 +3102,9 @@ Close the to-be-closed slot at the given index and set its value to @nil. The index must be the last index previously marked to be closed @see{lua_toclose} that is still active (that is, not closed yet). +A @Lid{__close} metamethod cannot yield +when called through this function. + (Exceptionally, this function was introduced in release 5.4.3. It is not present in previous 5.4 releases.) diff --git a/testes/locals.lua b/testes/locals.lua index 446ec13a1b..a93839dbc8 100644 --- a/testes/locals.lua +++ b/testes/locals.lua @@ -707,7 +707,6 @@ if rawget(_G, "T") then -- results are correct checktable(t, {10, 20}) end - end @@ -930,6 +929,81 @@ assert(co == nil) -- eventually it will be collected collectgarbage() +if rawget(_G, "T") then + print("to-be-closed variables x coroutines in C") + do + local token = 0 + local count = 0 + local f = T.makeCfunc[[ + toclose 1 + toclose 2 + return . + ]] + + local obj = func2close(function (_, msg) + count = count + 1 + token = coroutine.yield(count, token) + end) + + local co = coroutine.wrap(f) + local ct, res = co(obj, obj, 10, 20, 30, 3) -- will return 10, 20, 30 + -- initial token value, after closing 2nd obj + assert(ct == 1 and res == 0) + -- run until yield when closing 1st obj + ct, res = co(100) + assert(ct == 2 and res == 100) + res = {co(200)} -- run until end + assert(res[1] == 10 and res[2] == 20 and res[3] == 30 and res[4] == nil) + assert(token == 200) + end + + do + local f = T.makeCfunc[[ + toclose 1 + return . + ]] + + local obj = func2close(function () + local temp + local x = func2close(function () + coroutine.yield(temp) + return 1,2,3 -- to be ignored + end) + temp = coroutine.yield("closing obj") + return 1,2,3 -- to be ignored + end) + + local co = coroutine.wrap(f) + local res = co(obj, 10, 30, 1) -- will return only 30 + assert(res == "closing obj") + res = co("closing x") + assert(res == "closing x") + res = {co()} + assert(res[1] == 30 and res[2] == nil) + end + + do + -- still cannot yield inside 'closeslot' + local f = T.makeCfunc[[ + toclose 1 + closeslot 1 + ]] + local obj = func2close(coroutine.yield) + local co = coroutine.create(f) + local st, msg = coroutine.resume(co, obj) + assert(not st and string.find(msg, "attempt to yield across")) + + -- nor outside a coroutine + local f = T.makeCfunc[[ + toclose 1 + ]] + local st, msg = pcall(f, obj) + assert(not st and string.find(msg, "attempt to yield from outside")) + end +end + + + -- to-be-closed variables in generic for loops do local numopen = 0 From 38cc7d40a4bcb89314d212fdffd2ca8deebc3cb7 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 15 Feb 2021 10:38:09 -0300 Subject: [PATCH 0677/1145] Bug: cannot allow the call 'debug.getinfo(0, ">")' A 'what' argument starting with '>' indicates that there is a function in the C stack, which won't be there if the first argument is not a function. --- ldblib.c | 1 + testes/db.lua | 1 + 2 files changed, 2 insertions(+) diff --git a/ldblib.c b/ldblib.c index 15593bfbd1..de6e38b3ad 100644 --- a/ldblib.c +++ b/ldblib.c @@ -152,6 +152,7 @@ static int db_getinfo (lua_State *L) { lua_State *L1 = getthread(L, &arg); const char *options = luaL_optstring(L, arg+2, "flnSrtu"); checkstack(L, L1, 3); + luaL_argcheck(L, options[0] != '>', arg + 2, "invalid option '>'"); if (lua_isfunction(L, arg + 1)) { /* info about a function? */ options = lua_pushfstring(L, ">%s", options); /* add '>' to 'options' */ lua_pushvalue(L, arg + 1); /* move function to 'L1' stack */ diff --git a/testes/db.lua b/testes/db.lua index ce559ad959..d64952d9e2 100644 --- a/testes/db.lua +++ b/testes/db.lua @@ -31,6 +31,7 @@ end do assert(not pcall(debug.getinfo, print, "X")) -- invalid option + assert(not pcall(debug.getinfo, 0, ">")) -- invalid option assert(not debug.getinfo(1000)) -- out of range level assert(not debug.getinfo(-1)) -- out of range level local a = debug.getinfo(print) From c03c527fd207b4ad8f5a8e0f4f2c176bd227c979 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 15 Feb 2021 13:31:45 -0300 Subject: [PATCH 0678/1145] Bug: 'string.concat' error message uses wrong format --- ltablib.c | 2 +- testes/strings.lua | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/ltablib.c b/ltablib.c index d344a47e9a..c7f0e4dc51 100644 --- a/ltablib.c +++ b/ltablib.c @@ -146,7 +146,7 @@ static int tmove (lua_State *L) { static void addfield (lua_State *L, luaL_Buffer *b, lua_Integer i) { lua_geti(L, 1, i); if (!lua_isstring(L, -1)) - luaL_error(L, "invalid value (%s) at index %d in table for 'concat'", + luaL_error(L, "invalid value (%s) at index %I in table for 'concat'", luaL_typename(L, -1), i); luaL_addvalue(b); } diff --git a/testes/strings.lua b/testes/strings.lua index 2fa4a89ff2..61a06a2515 100644 --- a/testes/strings.lua +++ b/testes/strings.lua @@ -361,6 +361,9 @@ assert(load("return 1\n--comment without ending EOL")() == 1) checkerror("table expected", table.concat, 3) +checkerror("at index " .. maxi, table.concat, {}, " ", maxi, maxi) +-- '%' escapes following minus signal +checkerror("at index %" .. mini, table.concat, {}, " ", mini, mini) assert(table.concat{} == "") assert(table.concat({}, 'x') == "") assert(table.concat({'\0', '\0\1', '\0\1\2'}, '.\0.') == "\0.\0.\0\1.\0.\0\1\2") From 59c88f846d1dcd901a4420651aedf27816618923 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 24 Feb 2021 11:14:44 -0300 Subject: [PATCH 0679/1145] Broadening the use of branch hints More uses of macros 'likely'/'unlikely' (renamed to 'l_likely'/'l_unlikely'), both in range (extended to the libraries) and in scope (extended to hooks, stack growth). --- lauxlib.c | 22 +++++++++++----------- lauxlib.h | 8 ++++++-- lbaselib.c | 12 ++++++------ lcorolib.c | 10 +++++----- ldblib.c | 6 +++--- ldo.c | 24 ++++++++++++------------ ldo.h | 2 +- lgc.c | 2 +- liolib.c | 17 +++++++++-------- llimits.h | 16 ---------------- lmathlib.c | 5 +++-- lmem.c | 8 ++++---- loadlib.c | 17 ++++++++++------- loslib.c | 8 ++++---- lparser.c | 8 ++++---- lstate.c | 2 +- lstring.c | 8 ++++---- lstrlib.c | 41 ++++++++++++++++++++++------------------- ltable.c | 8 ++++---- ltablib.c | 9 +++++---- ltm.c | 5 +++-- luaconf.h | 20 ++++++++++++++++++++ lvm.c | 39 ++++++++++++++++++++------------------- lvm.h | 6 ++++-- 24 files changed, 162 insertions(+), 141 deletions(-) diff --git a/lauxlib.c b/lauxlib.c index e8fc486ecb..2610f90e56 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -190,7 +190,7 @@ LUALIB_API int luaL_argerror (lua_State *L, int arg, const char *extramsg) { } -int luaL_typeerror (lua_State *L, int arg, const char *tname) { +LUALIB_API int luaL_typeerror (lua_State *L, int arg, const char *tname) { const char *msg; const char *typearg; /* name for the type of the actual argument */ if (luaL_getmetafield(L, arg, "__name") == LUA_TSTRING) @@ -378,7 +378,7 @@ LUALIB_API int luaL_checkoption (lua_State *L, int arg, const char *def, ** but without 'msg'.) */ LUALIB_API void luaL_checkstack (lua_State *L, int space, const char *msg) { - if (!lua_checkstack(L, space)) { + if (l_unlikely(!lua_checkstack(L, space))) { if (msg) luaL_error(L, "stack overflow (%s)", msg); else @@ -388,20 +388,20 @@ LUALIB_API void luaL_checkstack (lua_State *L, int space, const char *msg) { LUALIB_API void luaL_checktype (lua_State *L, int arg, int t) { - if (lua_type(L, arg) != t) + if (l_unlikely(lua_type(L, arg) != t)) tag_error(L, arg, t); } LUALIB_API void luaL_checkany (lua_State *L, int arg) { - if (lua_type(L, arg) == LUA_TNONE) + if (l_unlikely(lua_type(L, arg) == LUA_TNONE)) luaL_argerror(L, arg, "value expected"); } LUALIB_API const char *luaL_checklstring (lua_State *L, int arg, size_t *len) { const char *s = lua_tolstring(L, arg, len); - if (!s) tag_error(L, arg, LUA_TSTRING); + if (l_unlikely(!s)) tag_error(L, arg, LUA_TSTRING); return s; } @@ -420,7 +420,7 @@ LUALIB_API const char *luaL_optlstring (lua_State *L, int arg, LUALIB_API lua_Number luaL_checknumber (lua_State *L, int arg) { int isnum; lua_Number d = lua_tonumberx(L, arg, &isnum); - if (!isnum) + if (l_unlikely(!isnum)) tag_error(L, arg, LUA_TNUMBER); return d; } @@ -442,7 +442,7 @@ static void interror (lua_State *L, int arg) { LUALIB_API lua_Integer luaL_checkinteger (lua_State *L, int arg) { int isnum; lua_Integer d = lua_tointegerx(L, arg, &isnum); - if (!isnum) { + if (l_unlikely(!isnum)) { interror(L, arg); } return d; @@ -475,7 +475,7 @@ static void *resizebox (lua_State *L, int idx, size_t newsize) { lua_Alloc allocf = lua_getallocf(L, &ud); UBox *box = (UBox *)lua_touserdata(L, idx); void *temp = allocf(ud, box->box, box->bsize, newsize); - if (temp == NULL && newsize > 0) { /* allocation error? */ + if (l_unlikely(temp == NULL && newsize > 0)) { /* allocation error? */ lua_pushliteral(L, "not enough memory"); lua_error(L); /* raise a memory error */ } @@ -521,7 +521,7 @@ static void newbox (lua_State *L) { */ static size_t newbuffsize (luaL_Buffer *B, size_t sz) { size_t newsize = B->size * 2; /* double buffer size */ - if (MAX_SIZET - sz < B->n) /* overflow in (B->n + sz)? */ + if (l_unlikely(MAX_SIZET - sz < B->n)) /* overflow in (B->n + sz)? */ return luaL_error(B->L, "buffer too large"); if (newsize < B->n + sz) /* double is not big enough? */ newsize = B->n + sz; @@ -861,7 +861,7 @@ LUALIB_API lua_Integer luaL_len (lua_State *L, int idx) { int isnum; lua_len(L, idx); l = lua_tointegerx(L, -1, &isnum); - if (!isnum) + if (l_unlikely(!isnum)) luaL_error(L, "object length is not an integer"); lua_pop(L, 1); /* remove object */ return l; @@ -1074,7 +1074,7 @@ static void warnfon (void *ud, const char *message, int tocont) { LUALIB_API lua_State *luaL_newstate (void) { lua_State *L = lua_newstate(l_alloc, NULL); - if (L) { + if (l_likely(L)) { lua_atpanic(L, &panic); lua_setwarnf(L, warnfoff, L); /* default is warnings off */ } diff --git a/lauxlib.h b/lauxlib.h index 65714911c1..9058e2621d 100644 --- a/lauxlib.h +++ b/lauxlib.h @@ -122,6 +122,10 @@ LUALIB_API void (luaL_requiref) (lua_State *L, const char *modname, ** =============================================================== */ +#if !defined(l_likely) +#define l_likely(x) x +#endif + #define luaL_newlibtable(L,l) \ lua_createtable(L, 0, sizeof(l)/sizeof((l)[0]) - 1) @@ -130,10 +134,10 @@ LUALIB_API void (luaL_requiref) (lua_State *L, const char *modname, (luaL_checkversion(L), luaL_newlibtable(L,l), luaL_setfuncs(L,l,0)) #define luaL_argcheck(L, cond,arg,extramsg) \ - ((void)((cond) || luaL_argerror(L, (arg), (extramsg)))) + ((void)(l_likely(cond) || luaL_argerror(L, (arg), (extramsg)))) #define luaL_argexpected(L,cond,arg,tname) \ - ((void)((cond) || luaL_typeerror(L, (arg), (tname)))) + ((void)(l_likely(cond) || luaL_typeerror(L, (arg), (tname)))) #define luaL_checkstring(L,n) (luaL_checklstring(L, (n), NULL)) #define luaL_optstring(L,n,d) (luaL_optlstring(L, (n), (d), NULL)) diff --git a/lbaselib.c b/lbaselib.c index 60786b3de8..83ad306d9c 100644 --- a/lbaselib.c +++ b/lbaselib.c @@ -138,7 +138,7 @@ static int luaB_setmetatable (lua_State *L) { int t = lua_type(L, 2); luaL_checktype(L, 1, LUA_TTABLE); luaL_argexpected(L, t == LUA_TNIL || t == LUA_TTABLE, 2, "nil or table"); - if (luaL_getmetafield(L, 1, "__metatable") != LUA_TNIL) + if (l_unlikely(luaL_getmetafield(L, 1, "__metatable") != LUA_TNIL)) return luaL_error(L, "cannot change a protected metatable"); lua_settop(L, 2); lua_setmetatable(L, 1); @@ -300,7 +300,7 @@ static int luaB_ipairs (lua_State *L) { static int load_aux (lua_State *L, int status, int envidx) { - if (status == LUA_OK) { + if (l_likely(status == LUA_OK)) { if (envidx != 0) { /* 'env' parameter? */ lua_pushvalue(L, envidx); /* environment for loaded function */ if (!lua_setupvalue(L, -2, 1)) /* set it as 1st upvalue */ @@ -356,7 +356,7 @@ static const char *generic_reader (lua_State *L, void *ud, size_t *size) { *size = 0; return NULL; } - else if (!lua_isstring(L, -1)) + else if (l_unlikely(!lua_isstring(L, -1))) luaL_error(L, "reader function must return a string"); lua_replace(L, RESERVEDSLOT); /* save string in reserved slot */ return lua_tolstring(L, RESERVEDSLOT, size); @@ -394,7 +394,7 @@ static int dofilecont (lua_State *L, int d1, lua_KContext d2) { static int luaB_dofile (lua_State *L) { const char *fname = luaL_optstring(L, 1, NULL); lua_settop(L, 1); - if (luaL_loadfile(L, fname) != LUA_OK) + if (l_unlikely(luaL_loadfile(L, fname) != LUA_OK)) return lua_error(L); lua_callk(L, 0, LUA_MULTRET, 0, dofilecont); return dofilecont(L, 0, 0); @@ -402,7 +402,7 @@ static int luaB_dofile (lua_State *L) { static int luaB_assert (lua_State *L) { - if (lua_toboolean(L, 1)) /* condition is true? */ + if (l_likely(lua_toboolean(L, 1))) /* condition is true? */ return lua_gettop(L); /* return all arguments */ else { /* error */ luaL_checkany(L, 1); /* there must be a condition */ @@ -438,7 +438,7 @@ static int luaB_select (lua_State *L) { ** ignored). */ static int finishpcall (lua_State *L, int status, lua_KContext extra) { - if (status != LUA_OK && status != LUA_YIELD) { /* error? */ + if (l_unlikely(status != LUA_OK && status != LUA_YIELD)) { /* error? */ lua_pushboolean(L, 0); /* first result (false) */ lua_pushvalue(L, -2); /* error message */ return 2; /* return false, msg */ diff --git a/lcorolib.c b/lcorolib.c index ed7c58b2b2..fedbebec39 100644 --- a/lcorolib.c +++ b/lcorolib.c @@ -31,14 +31,14 @@ static lua_State *getco (lua_State *L) { */ static int auxresume (lua_State *L, lua_State *co, int narg) { int status, nres; - if (!lua_checkstack(co, narg)) { + if (l_unlikely(!lua_checkstack(co, narg))) { lua_pushliteral(L, "too many arguments to resume"); return -1; /* error flag */ } lua_xmove(L, co, narg); status = lua_resume(co, L, narg, &nres); - if (status == LUA_OK || status == LUA_YIELD) { - if (!lua_checkstack(L, nres + 1)) { + if (l_likely(status == LUA_OK || status == LUA_YIELD)) { + if (l_unlikely(!lua_checkstack(L, nres + 1))) { lua_pop(co, nres); /* remove results anyway */ lua_pushliteral(L, "too many results to resume"); return -1; /* error flag */ @@ -57,7 +57,7 @@ static int luaB_coresume (lua_State *L) { lua_State *co = getco(L); int r; r = auxresume(L, co, lua_gettop(L) - 1); - if (r < 0) { + if (l_unlikely(r < 0)) { lua_pushboolean(L, 0); lua_insert(L, -2); return 2; /* return false + error message */ @@ -73,7 +73,7 @@ static int luaB_coresume (lua_State *L) { static int luaB_auxwrap (lua_State *L) { lua_State *co = lua_tothread(L, lua_upvalueindex(1)); int r = auxresume(L, co, lua_gettop(L)); - if (r < 0) { /* error? */ + if (l_unlikely(r < 0)) { /* error? */ int stat = lua_status(co); if (stat != LUA_OK && stat != LUA_YIELD) { /* error in the coroutine? */ stat = lua_resetthread(co); /* close its tbc variables */ diff --git a/ldblib.c b/ldblib.c index de6e38b3ad..6dcbaa9824 100644 --- a/ldblib.c +++ b/ldblib.c @@ -33,7 +33,7 @@ static const char *const HOOKKEY = "_HOOKKEY"; ** checked. */ static void checkstack (lua_State *L, lua_State *L1, int n) { - if (L != L1 && !lua_checkstack(L1, n)) + if (l_unlikely(L != L1 && !lua_checkstack(L1, n))) luaL_error(L, "stack overflow"); } @@ -213,7 +213,7 @@ static int db_getlocal (lua_State *L) { lua_Debug ar; const char *name; int level = (int)luaL_checkinteger(L, arg + 1); - if (!lua_getstack(L1, level, &ar)) /* out of range? */ + if (l_unlikely(!lua_getstack(L1, level, &ar))) /* out of range? */ return luaL_argerror(L, arg+1, "level out of range"); checkstack(L, L1, 1); name = lua_getlocal(L1, &ar, nvar); @@ -238,7 +238,7 @@ static int db_setlocal (lua_State *L) { lua_Debug ar; int level = (int)luaL_checkinteger(L, arg + 1); int nvar = (int)luaL_checkinteger(L, arg + 2); - if (!lua_getstack(L1, level, &ar)) /* out of range? */ + if (l_unlikely(!lua_getstack(L1, level, &ar))) /* out of range? */ return luaL_argerror(L, arg+1, "level out of range"); luaL_checkany(L, arg+3); lua_settop(L, arg+3); diff --git a/ldo.c b/ldo.c index 5587b602fd..7c9ce06ea3 100644 --- a/ldo.c +++ b/ldo.c @@ -184,7 +184,7 @@ int luaD_reallocstack (lua_State *L, int newsize, int raiseerror) { StkId newstack = luaM_reallocvector(L, L->stack, lim + EXTRA_STACK, newsize + EXTRA_STACK, StackValue); lua_assert(newsize <= LUAI_MAXSTACK || newsize == ERRORSTACKSIZE); - if (unlikely(newstack == NULL)) { /* reallocation failed? */ + if (l_unlikely(newstack == NULL)) { /* reallocation failed? */ if (raiseerror) luaM_error(L); else return 0; /* do not raise an error */ @@ -204,7 +204,7 @@ int luaD_reallocstack (lua_State *L, int newsize, int raiseerror) { */ int luaD_growstack (lua_State *L, int n, int raiseerror) { int size = stacksize(L); - if (unlikely(size > LUAI_MAXSTACK)) { + if (l_unlikely(size > LUAI_MAXSTACK)) { /* if stack is larger than maximum, thread is already using the extra space reserved for errors, that is, thread is handling a stack error; cannot grow further than that. */ @@ -220,7 +220,7 @@ int luaD_growstack (lua_State *L, int n, int raiseerror) { newsize = LUAI_MAXSTACK; if (newsize < needed) /* but must respect what was asked for */ newsize = needed; - if (likely(newsize <= LUAI_MAXSTACK)) + if (l_likely(newsize <= LUAI_MAXSTACK)) return luaD_reallocstack(L, newsize, raiseerror); else { /* stack overflow */ /* add extra size to be able to handle the error message */ @@ -376,7 +376,7 @@ static void rethook (lua_State *L, CallInfo *ci, int nres) { void luaD_tryfuncTM (lua_State *L, StkId func) { const TValue *tm = luaT_gettmbyobj(L, s2v(func), TM_CALL); StkId p; - if (unlikely(ttisnil(tm))) + if (l_unlikely(ttisnil(tm))) luaG_callerror(L, s2v(func)); /* nothing to call */ for (p = L->top; p > func; p--) /* open space for metamethod */ setobjs2s(L, p, p-1); @@ -444,7 +444,7 @@ static void moveresults (lua_State *L, StkId res, int nres, int wanted) { */ void luaD_poscall (lua_State *L, CallInfo *ci, int nres) { int wanted = ci->nresults; - if (L->hookmask && !hastocloseCfunc(wanted)) + if (l_unlikely(L->hookmask && !hastocloseCfunc(wanted))) rethook(L, ci, nres); /* move results to proper place */ moveresults(L, ci->func, nres, wanted); @@ -510,7 +510,7 @@ CallInfo *luaD_precall (lua_State *L, StkId func, int nresults) { ci->top = L->top + LUA_MINSTACK; ci->func = func; lua_assert(ci->top <= L->stack_last); - if (L->hookmask & LUA_MASKCALL) { + if (l_unlikely(L->hookmask & LUA_MASKCALL)) { int narg = cast_int(L->top - func) - 1; luaD_hook(L, LUA_HOOKCALL, -1, 1, narg); } @@ -556,7 +556,7 @@ CallInfo *luaD_precall (lua_State *L, StkId func, int nresults) { static void ccall (lua_State *L, StkId func, int nResults, int inc) { CallInfo *ci; L->nCcalls += inc; - if (unlikely(getCcalls(L) >= LUAI_MAXCCALLS)) + if (l_unlikely(getCcalls(L) >= LUAI_MAXCCALLS)) luaE_checkcstack(L); if ((ci = luaD_precall(L, func, nResults)) != NULL) { /* Lua function? */ ci->callstatus = CIST_FRESH; /* mark that it is a "fresh" execute */ @@ -600,7 +600,7 @@ void luaD_callnoyield (lua_State *L, StkId func, int nResults) { */ static int finishpcallk (lua_State *L, CallInfo *ci) { int status = getcistrecst(ci); /* get original status */ - if (status == LUA_OK) /* no error? */ + if (l_likely(status == LUA_OK)) /* no error? */ status = LUA_YIELD; /* was interrupted by an yield */ else { /* error */ StkId func = restorestack(L, ci->u2.funcidx); @@ -774,7 +774,7 @@ LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs, status = luaD_rawrunprotected(L, resume, &nargs); /* continue running after recoverable errors */ status = precover(L, status); - if (likely(!errorstatus(status))) + if (l_likely(!errorstatus(status))) lua_assert(status == L->status); /* normal end or yield */ else { /* unrecoverable error */ L->status = cast_byte(status); /* mark thread as 'dead' */ @@ -800,7 +800,7 @@ LUA_API int lua_yieldk (lua_State *L, int nresults, lua_KContext ctx, lua_lock(L); ci = L->ci; api_checknelems(L, nresults); - if (unlikely(!yieldable(L))) { + if (l_unlikely(!yieldable(L))) { if (L != G(L)->mainthread) luaG_runerror(L, "attempt to yield across a C-call boundary"); else @@ -853,7 +853,7 @@ int luaD_closeprotected (lua_State *L, ptrdiff_t level, int status) { struct CloseP pcl; pcl.level = restorestack(L, level); pcl.status = status; status = luaD_rawrunprotected(L, &closepaux, &pcl); - if (likely(status == LUA_OK)) /* no more errors? */ + if (l_likely(status == LUA_OK)) /* no more errors? */ return pcl.status; else { /* an error occurred; restore saved state and repeat */ L->ci = old_ci; @@ -876,7 +876,7 @@ int luaD_pcall (lua_State *L, Pfunc func, void *u, ptrdiff_t old_errfunc = L->errfunc; L->errfunc = ef; status = luaD_rawrunprotected(L, func, u); - if (unlikely(status != LUA_OK)) { /* an error occurred? */ + if (l_unlikely(status != LUA_OK)) { /* an error occurred? */ L->ci = old_ci; L->allowhook = old_allowhooks; status = luaD_closeprotected(L, old_top, status); diff --git a/ldo.h b/ldo.h index c7721d62da..6bf0ed86f7 100644 --- a/ldo.h +++ b/ldo.h @@ -23,7 +23,7 @@ ** at every check. */ #define luaD_checkstackaux(L,n,pre,pos) \ - if (L->stack_last - L->top <= (n)) \ + if (l_unlikely(L->stack_last - L->top <= (n))) \ { pre; luaD_growstack(L, n, 1); pos; } \ else { condmovestack(L,pre,pos); } diff --git a/lgc.c b/lgc.c index bab9beb12b..94e0486ef0 100644 --- a/lgc.c +++ b/lgc.c @@ -916,7 +916,7 @@ static void GCTM (lua_State *L) { L->ci->callstatus &= ~CIST_FIN; /* not running a finalizer anymore */ L->allowhook = oldah; /* restore hooks */ g->gcrunning = running; /* restore state */ - if (unlikely(status != LUA_OK)) { /* error while running __gc? */ + if (l_unlikely(status != LUA_OK)) { /* error while running __gc? */ luaE_warnerror(L, "__gc metamethod"); L->top--; /* pops error object */ } diff --git a/liolib.c b/liolib.c index 79516724c7..b08397da45 100644 --- a/liolib.c +++ b/liolib.c @@ -186,7 +186,7 @@ static int f_tostring (lua_State *L) { static FILE *tofile (lua_State *L) { LStream *p = tolstream(L); - if (isclosed(p)) + if (l_unlikely(isclosed(p))) luaL_error(L, "attempt to use a closed file"); lua_assert(p->f); return p->f; @@ -261,7 +261,7 @@ static LStream *newfile (lua_State *L) { static void opencheck (lua_State *L, const char *fname, const char *mode) { LStream *p = newfile(L); p->f = fopen(fname, mode); - if (p->f == NULL) + if (l_unlikely(p->f == NULL)) luaL_error(L, "cannot open file '%s' (%s)", fname, strerror(errno)); } @@ -309,7 +309,7 @@ static FILE *getiofile (lua_State *L, const char *findex) { LStream *p; lua_getfield(L, LUA_REGISTRYINDEX, findex); p = (LStream *)lua_touserdata(L, -1); - if (isclosed(p)) + if (l_unlikely(isclosed(p))) luaL_error(L, "default %s file is closed", findex + IOPREF_LEN); return p->f; } @@ -436,7 +436,7 @@ typedef struct { ** Add current char to buffer (if not out of space) and read next one */ static int nextc (RN *rn) { - if (rn->n >= L_MAXLENNUM) { /* buffer overflow? */ + if (l_unlikely(rn->n >= L_MAXLENNUM)) { /* buffer overflow? */ rn->buff[0] = '\0'; /* invalidate result */ return 0; /* fail */ } @@ -499,8 +499,8 @@ static int read_number (lua_State *L, FILE *f) { ungetc(rn.c, rn.f); /* unread look-ahead char */ l_unlockfile(rn.f); rn.buff[rn.n] = '\0'; /* finish string */ - if (lua_stringtonumber(L, rn.buff)) /* is this a valid number? */ - return 1; /* ok */ + if (l_likely(lua_stringtonumber(L, rn.buff))) + return 1; /* ok, it is a valid number */ else { /* invalid format */ lua_pushnil(L); /* "result" to be removed */ return 0; /* read fails */ @@ -676,7 +676,8 @@ static int g_write (lua_State *L, FILE *f, int arg) { status = status && (fwrite(s, sizeof(char), l, f) == l); } } - if (status) return 1; /* file handle already on stack top */ + if (l_likely(status)) + return 1; /* file handle already on stack top */ else return luaL_fileresult(L, status, NULL); } @@ -703,7 +704,7 @@ static int f_seek (lua_State *L) { luaL_argcheck(L, (lua_Integer)offset == p3, 3, "not an integer in proper range"); op = l_fseek(f, offset, mode[op]); - if (op) + if (l_unlikely(op)) return luaL_fileresult(L, 0, NULL); /* error */ else { lua_pushinteger(L, (lua_Integer)l_ftell(f)); diff --git a/llimits.h b/llimits.h index d03948314f..025f1c82cd 100644 --- a/llimits.h +++ b/llimits.h @@ -149,22 +149,6 @@ typedef LUAI_UACINT l_uacInt; #endif -/* -** macros to improve jump prediction (used mainly for error handling) -*/ -#if !defined(likely) - -#if defined(__GNUC__) -#define likely(x) (__builtin_expect(((x) != 0), 1)) -#define unlikely(x) (__builtin_expect(((x) != 0), 0)) -#else -#define likely(x) (x) -#define unlikely(x) (x) -#endif - -#endif - - /* ** non-return type */ diff --git a/lmathlib.c b/lmathlib.c index 86def470c4..5f5983a438 100644 --- a/lmathlib.c +++ b/lmathlib.c @@ -73,7 +73,7 @@ static int math_atan (lua_State *L) { static int math_toint (lua_State *L) { int valid; lua_Integer n = lua_tointegerx(L, 1, &valid); - if (valid) + if (l_likely(valid)) lua_pushinteger(L, n); else { luaL_checkany(L, 1); @@ -175,7 +175,8 @@ static int math_log (lua_State *L) { lua_Number base = luaL_checknumber(L, 2); #if !defined(LUA_USE_C89) if (base == l_mathop(2.0)) - res = l_mathop(log2)(x); else + res = l_mathop(log2)(x); + else #endif if (base == l_mathop(10.0)) res = l_mathop(log10)(x); diff --git a/lmem.c b/lmem.c index 4822a0eaa3..e90f991aff 100644 --- a/lmem.c +++ b/lmem.c @@ -83,7 +83,7 @@ void *luaM_growaux_ (lua_State *L, void *block, int nelems, int *psize, if (nelems + 1 <= size) /* does one extra element still fit? */ return block; /* nothing to be done */ if (size >= limit / 2) { /* cannot double it? */ - if (unlikely(size >= limit)) /* cannot grow even a little? */ + if (l_unlikely(size >= limit)) /* cannot grow even a little? */ luaG_runerror(L, "too many %s (limit is %d)", what, limit); size = limit; /* still have at least one free place */ } @@ -164,7 +164,7 @@ void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) { global_State *g = G(L); lua_assert((osize == 0) == (block == NULL)); newblock = firsttry(g, block, osize, nsize); - if (unlikely(newblock == NULL && nsize > 0)) { + if (l_unlikely(newblock == NULL && nsize > 0)) { if (nsize > osize) /* not shrinking a block? */ newblock = tryagain(L, block, osize, nsize); if (newblock == NULL) /* still no memory? */ @@ -179,7 +179,7 @@ void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) { void *luaM_saferealloc_ (lua_State *L, void *block, size_t osize, size_t nsize) { void *newblock = luaM_realloc_(L, block, osize, nsize); - if (unlikely(newblock == NULL && nsize > 0)) /* allocation failed? */ + if (l_unlikely(newblock == NULL && nsize > 0)) /* allocation failed? */ luaM_error(L); return newblock; } @@ -191,7 +191,7 @@ void *luaM_malloc_ (lua_State *L, size_t size, int tag) { else { global_State *g = G(L); void *newblock = firsttry(g, NULL, tag, size); - if (unlikely(newblock == NULL)) { + if (l_unlikely(newblock == NULL)) { newblock = tryagain(L, NULL, tag, size); if (newblock == NULL) luaM_error(L); diff --git a/loadlib.c b/loadlib.c index c0ec9a131b..6f9fa37366 100644 --- a/loadlib.c +++ b/loadlib.c @@ -132,14 +132,16 @@ static void lsys_unloadlib (void *lib) { static void *lsys_load (lua_State *L, const char *path, int seeglb) { void *lib = dlopen(path, RTLD_NOW | (seeglb ? RTLD_GLOBAL : RTLD_LOCAL)); - if (lib == NULL) lua_pushstring(L, dlerror()); + if (l_unlikely(lib == NULL)) + lua_pushstring(L, dlerror()); return lib; } static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) { lua_CFunction f = cast_func(dlsym(lib, sym)); - if (f == NULL) lua_pushstring(L, dlerror()); + if (l_unlikely(f == NULL)) + lua_pushstring(L, dlerror()); return f; } @@ -410,7 +412,7 @@ static int ll_loadlib (lua_State *L) { const char *path = luaL_checkstring(L, 1); const char *init = luaL_checkstring(L, 2); int stat = lookforfunc(L, path, init); - if (stat == 0) /* no errors? */ + if (l_likely(stat == 0)) /* no errors? */ return 1; /* return the loaded function */ else { /* error; error message is on stack top */ luaL_pushfail(L); @@ -523,14 +525,14 @@ static const char *findfile (lua_State *L, const char *name, const char *path; lua_getfield(L, lua_upvalueindex(1), pname); path = lua_tostring(L, -1); - if (path == NULL) + if (l_unlikely(path == NULL)) luaL_error(L, "'package.%s' must be a string", pname); return searchpath(L, name, path, ".", dirsep); } static int checkload (lua_State *L, int stat, const char *filename) { - if (stat) { /* module loaded successfully? */ + if (l_likely(stat)) { /* module loaded successfully? */ lua_pushstring(L, filename); /* will be 2nd argument to module */ return 2; /* return open function and file name */ } @@ -623,13 +625,14 @@ static void findloader (lua_State *L, const char *name) { int i; luaL_Buffer msg; /* to build error message */ /* push 'package.searchers' to index 3 in the stack */ - if (lua_getfield(L, lua_upvalueindex(1), "searchers") != LUA_TTABLE) + if (l_unlikely(lua_getfield(L, lua_upvalueindex(1), "searchers") + != LUA_TTABLE)) luaL_error(L, "'package.searchers' must be a table"); luaL_buffinit(L, &msg); /* iterate over available searchers to find a loader */ for (i = 1; ; i++) { luaL_addstring(&msg, "\n\t"); /* error-message prefix */ - if (lua_rawgeti(L, 3, i) == LUA_TNIL) { /* no more searchers? */ + if (l_unlikely(lua_rawgeti(L, 3, i) == LUA_TNIL)) { /* no more searchers? */ lua_pop(L, 1); /* remove nil */ luaL_buffsub(&msg, 2); /* remove prefix */ luaL_pushresult(&msg); /* create error message */ diff --git a/loslib.c b/loslib.c index e65e188bd7..3e20d622ba 100644 --- a/loslib.c +++ b/loslib.c @@ -170,7 +170,7 @@ static int os_tmpname (lua_State *L) { char buff[LUA_TMPNAMBUFSIZE]; int err; lua_tmpnam(buff, err); - if (err) + if (l_unlikely(err)) return luaL_error(L, "unable to generate a unique filename"); lua_pushstring(L, buff); return 1; @@ -208,7 +208,7 @@ static int os_clock (lua_State *L) { */ static void setfield (lua_State *L, const char *key, int value, int delta) { #if (defined(LUA_NUMTIME) && LUA_MAXINTEGER <= INT_MAX) - if (value > LUA_MAXINTEGER - delta) + if (l_unlikely(value > LUA_MAXINTEGER - delta)) luaL_error(L, "field '%s' is out-of-bound", key); #endif lua_pushinteger(L, (lua_Integer)value + delta); @@ -253,9 +253,9 @@ static int getfield (lua_State *L, const char *key, int d, int delta) { int t = lua_getfield(L, -1, key); /* get field and its type */ lua_Integer res = lua_tointegerx(L, -1, &isnum); if (!isnum) { /* field is not an integer? */ - if (t != LUA_TNIL) /* some other value? */ + if (l_unlikely(t != LUA_TNIL)) /* some other value? */ return luaL_error(L, "field '%s' is not an integer", key); - else if (d < 0) /* absent field; no default? */ + else if (l_unlikely(d < 0)) /* absent field; no default? */ return luaL_error(L, "field '%s' missing in date table", key); res = d; } diff --git a/lparser.c b/lparser.c index 249ba9a40b..284ef1f0c4 100644 --- a/lparser.c +++ b/lparser.c @@ -128,7 +128,7 @@ static void checknext (LexState *ls, int c) { ** in line 'where' (if that is not the current line). */ static void check_match (LexState *ls, int what, int who, int where) { - if (unlikely(!testnext(ls, what))) { + if (l_unlikely(!testnext(ls, what))) { if (where == ls->linenumber) /* all in the same line? */ error_expected(ls, what); /* do not need a complex message */ else { @@ -517,7 +517,7 @@ static void solvegoto (LexState *ls, int g, Labeldesc *label) { Labellist *gl = &ls->dyd->gt; /* list of goto's */ Labeldesc *gt = &gl->arr[g]; /* goto to be resolved */ lua_assert(eqstr(gt->name, label->name)); - if (unlikely(gt->nactvar < label->nactvar)) /* enter some scope? */ + if (l_unlikely(gt->nactvar < label->nactvar)) /* enter some scope? */ jumpscopeerror(ls, gt); luaK_patchlist(ls->fs, gt->pc, label->pc); for (i = g; i < gl->n - 1; i++) /* remove goto from pending list */ @@ -1435,7 +1435,7 @@ static void breakstat (LexState *ls) { */ static void checkrepeated (LexState *ls, TString *name) { Labeldesc *lb = findlabel(ls, name); - if (unlikely(lb != NULL)) { /* already defined? */ + if (l_unlikely(lb != NULL)) { /* already defined? */ const char *msg = "label '%s' already defined on line %d"; msg = luaO_pushfstring(ls->L, msg, getstr(name), lb->line); luaK_semerror(ls, msg); /* error */ @@ -1520,7 +1520,7 @@ static void fixforjump (FuncState *fs, int pc, int dest, int back) { int offset = dest - (pc + 1); if (back) offset = -offset; - if (unlikely(offset > MAXARG_Bx)) + if (l_unlikely(offset > MAXARG_Bx)) luaX_syntaxerror(fs->ls, "control structure too long"); SETARG_Bx(*jmp, offset); } diff --git a/lstate.c b/lstate.c index 38078521e9..04909db375 100644 --- a/lstate.c +++ b/lstate.c @@ -172,7 +172,7 @@ void luaE_checkcstack (lua_State *L) { LUAI_FUNC void luaE_incCstack (lua_State *L) { L->nCcalls++; - if (unlikely(getCcalls(L) >= LUAI_MAXCCALLS)) + if (l_unlikely(getCcalls(L) >= LUAI_MAXCCALLS)) luaE_checkcstack(L); } diff --git a/lstring.c b/lstring.c index 138871c70d..13dcaf4259 100644 --- a/lstring.c +++ b/lstring.c @@ -89,7 +89,7 @@ void luaS_resize (lua_State *L, int nsize) { if (nsize < osize) /* shrinking table? */ tablerehash(tb->hash, osize, nsize); /* depopulate shrinking part */ newvect = luaM_reallocvector(L, tb->hash, osize, nsize, TString*); - if (unlikely(newvect == NULL)) { /* reallocation failed? */ + if (l_unlikely(newvect == NULL)) { /* reallocation failed? */ if (nsize < osize) /* was it shrinking table? */ tablerehash(tb->hash, nsize, osize); /* restore to original size */ /* leave table as it was */ @@ -172,7 +172,7 @@ void luaS_remove (lua_State *L, TString *ts) { static void growstrtab (lua_State *L, stringtable *tb) { - if (unlikely(tb->nuse == MAX_INT)) { /* too many strings? */ + if (l_unlikely(tb->nuse == MAX_INT)) { /* too many strings? */ luaC_fullgc(L, 1); /* try to free some... */ if (tb->nuse == MAX_INT) /* still too many? */ luaM_error(L); /* cannot even create a message... */ @@ -223,7 +223,7 @@ TString *luaS_newlstr (lua_State *L, const char *str, size_t l) { return internshrstr(L, str, l); else { TString *ts; - if (unlikely(l >= (MAX_SIZE - sizeof(TString))/sizeof(char))) + if (l_unlikely(l >= (MAX_SIZE - sizeof(TString))/sizeof(char))) luaM_toobig(L); ts = luaS_createlngstrobj(L, l); memcpy(getstr(ts), str, l * sizeof(char)); @@ -259,7 +259,7 @@ Udata *luaS_newudata (lua_State *L, size_t s, int nuvalue) { Udata *u; int i; GCObject *o; - if (unlikely(s > MAX_SIZE - udatamemoffset(nuvalue))) + if (l_unlikely(s > MAX_SIZE - udatamemoffset(nuvalue))) luaM_toobig(L); o = luaC_newobj(L, LUA_VUSERDATA, sizeudata(nuvalue, s)); u = gco2u(o); diff --git a/lstrlib.c b/lstrlib.c index c7242ea4c5..47e5b27a6c 100644 --- a/lstrlib.c +++ b/lstrlib.c @@ -152,8 +152,9 @@ static int str_rep (lua_State *L) { const char *s = luaL_checklstring(L, 1, &l); lua_Integer n = luaL_checkinteger(L, 2); const char *sep = luaL_optlstring(L, 3, "", &lsep); - if (n <= 0) lua_pushliteral(L, ""); - else if (l + lsep < l || l + lsep > MAXSIZE / n) /* may overflow? */ + if (n <= 0) + lua_pushliteral(L, ""); + else if (l_unlikely(l + lsep < l || l + lsep > MAXSIZE / n)) return luaL_error(L, "resulting string too large"); else { size_t totallen = (size_t)n * l + (size_t)(n - 1) * lsep; @@ -181,7 +182,7 @@ static int str_byte (lua_State *L) { size_t pose = getendpos(L, 3, pi, l); int n, i; if (posi > pose) return 0; /* empty interval; return no values */ - if (pose - posi >= (size_t)INT_MAX) /* arithmetic overflow? */ + if (l_unlikely(pose - posi >= (size_t)INT_MAX)) /* arithmetic overflow? */ return luaL_error(L, "string slice too long"); n = (int)(pose - posi) + 1; luaL_checkstack(L, n, "string slice too long"); @@ -235,7 +236,7 @@ static int str_dump (lua_State *L) { luaL_checktype(L, 1, LUA_TFUNCTION); lua_settop(L, 1); /* ensure function is on the top of the stack */ state.init = 0; - if (lua_dump(L, writer, &state, strip) != 0) + if (l_unlikely(lua_dump(L, writer, &state, strip) != 0)) return luaL_error(L, "unable to dump given function"); luaL_pushresult(&state.B); return 1; @@ -275,7 +276,8 @@ static int tonum (lua_State *L, int arg) { static void trymt (lua_State *L, const char *mtname) { lua_settop(L, 2); /* back to the original arguments */ - if (lua_type(L, 2) == LUA_TSTRING || !luaL_getmetafield(L, 2, mtname)) + if (l_unlikely(lua_type(L, 2) == LUA_TSTRING || + !luaL_getmetafield(L, 2, mtname))) luaL_error(L, "attempt to %s a '%s' with a '%s'", mtname + 2, luaL_typename(L, -2), luaL_typename(L, -1)); lua_insert(L, -3); /* put metamethod before arguments */ @@ -383,7 +385,8 @@ static const char *match (MatchState *ms, const char *s, const char *p); static int check_capture (MatchState *ms, int l) { l -= '1'; - if (l < 0 || l >= ms->level || ms->capture[l].len == CAP_UNFINISHED) + if (l_unlikely(l < 0 || l >= ms->level || + ms->capture[l].len == CAP_UNFINISHED)) return luaL_error(ms->L, "invalid capture index %%%d", l + 1); return l; } @@ -400,14 +403,14 @@ static int capture_to_close (MatchState *ms) { static const char *classend (MatchState *ms, const char *p) { switch (*p++) { case L_ESC: { - if (p == ms->p_end) + if (l_unlikely(p == ms->p_end)) luaL_error(ms->L, "malformed pattern (ends with '%%')"); return p+1; } case '[': { if (*p == '^') p++; do { /* look for a ']' */ - if (p == ms->p_end) + if (l_unlikely(p == ms->p_end)) luaL_error(ms->L, "malformed pattern (missing ']')"); if (*(p++) == L_ESC && p < ms->p_end) p++; /* skip escapes (e.g. '%]') */ @@ -482,7 +485,7 @@ static int singlematch (MatchState *ms, const char *s, const char *p, static const char *matchbalance (MatchState *ms, const char *s, const char *p) { - if (p >= ms->p_end - 1) + if (l_unlikely(p >= ms->p_end - 1)) luaL_error(ms->L, "malformed pattern (missing arguments to '%%b')"); if (*s != *p) return NULL; else { @@ -565,7 +568,7 @@ static const char *match_capture (MatchState *ms, const char *s, int l) { static const char *match (MatchState *ms, const char *s, const char *p) { - if (ms->matchdepth-- == 0) + if (l_unlikely(ms->matchdepth-- == 0)) luaL_error(ms->L, "pattern too complex"); init: /* using goto's to optimize tail recursion */ if (p != ms->p_end) { /* end of pattern? */ @@ -599,7 +602,7 @@ static const char *match (MatchState *ms, const char *s, const char *p) { case 'f': { /* frontier? */ const char *ep; char previous; p += 2; - if (*p != '[') + if (l_unlikely(*p != '[')) luaL_error(ms->L, "missing '[' after '%%f' in pattern"); ep = classend(ms, p); /* points to what is next */ previous = (s == ms->src_init) ? '\0' : *(s - 1); @@ -699,7 +702,7 @@ static const char *lmemfind (const char *s1, size_t l1, static size_t get_onecapture (MatchState *ms, int i, const char *s, const char *e, const char **cap) { if (i >= ms->level) { - if (i != 0) + if (l_unlikely(i != 0)) luaL_error(ms->L, "invalid capture index %%%d", i + 1); *cap = s; return e - s; @@ -707,7 +710,7 @@ static size_t get_onecapture (MatchState *ms, int i, const char *s, else { ptrdiff_t capl = ms->capture[i].len; *cap = ms->capture[i].init; - if (capl == CAP_UNFINISHED) + if (l_unlikely(capl == CAP_UNFINISHED)) luaL_error(ms->L, "unfinished capture"); else if (capl == CAP_POSITION) lua_pushinteger(ms->L, (ms->capture[i].init - ms->src_init) + 1); @@ -926,7 +929,7 @@ static int add_value (MatchState *ms, luaL_Buffer *b, const char *s, luaL_addlstring(b, s, e - s); /* keep original text */ return 0; /* no changes */ } - else if (!lua_isstring(L, -1)) + else if (l_unlikely(!lua_isstring(L, -1))) return luaL_error(L, "invalid replacement value (a %s)", luaL_typename(L, -1)); else { @@ -1058,7 +1061,7 @@ static int lua_number2strx (lua_State *L, char *buff, int sz, for (i = 0; i < n; i++) buff[i] = toupper(uchar(buff[i])); } - else if (fmt[SIZELENMOD] != 'a') + else if (l_unlikely(fmt[SIZELENMOD] != 'a')) return luaL_error(L, "modifiers for format '%%a'/'%%A' not implemented"); return n; } @@ -1411,7 +1414,7 @@ static int getnum (const char **fmt, int df) { */ static int getnumlimit (Header *h, const char **fmt, int df) { int sz = getnum(fmt, df); - if (sz > MAXINTSIZE || sz <= 0) + if (l_unlikely(sz > MAXINTSIZE || sz <= 0)) return luaL_error(h->L, "integral size (%d) out of limits [1,%d]", sz, MAXINTSIZE); return sz; @@ -1452,7 +1455,7 @@ static KOption getoption (Header *h, const char **fmt, int *size) { case 's': *size = getnumlimit(h, fmt, sizeof(size_t)); return Kstring; case 'c': *size = getnum(fmt, -1); - if (*size == -1) + if (l_unlikely(*size == -1)) luaL_error(h->L, "missing size for format option 'c'"); return Kchar; case 'z': return Kzstr; @@ -1491,7 +1494,7 @@ static KOption getdetails (Header *h, size_t totalsize, else { if (align > h->maxalign) /* enforce maximum alignment */ align = h->maxalign; - if ((align & (align - 1)) != 0) /* is 'align' not a power of 2? */ + if (l_unlikely((align & (align - 1)) != 0)) /* not a power of 2? */ luaL_argerror(h->L, 1, "format asks for alignment not power of 2"); *ntoalign = (align - (int)(totalsize & (align - 1))) & (align - 1); } @@ -1683,7 +1686,7 @@ static lua_Integer unpackint (lua_State *L, const char *str, else if (size > SZINT) { /* must check unread bytes */ int mask = (!issigned || (lua_Integer)res >= 0) ? 0 : MC; for (i = limit; i < size; i++) { - if ((unsigned char)str[islittle ? i : size - 1 - i] != mask) + if (l_unlikely((unsigned char)str[islittle ? i : size - 1 - i] != mask)) luaL_error(L, "%d-byte integer does not fit into Lua Integer", size); } } diff --git a/ltable.c b/ltable.c index e98bab7114..b520cdf4ba 100644 --- a/ltable.c +++ b/ltable.c @@ -307,7 +307,7 @@ static unsigned int findindex (lua_State *L, Table *t, TValue *key, return i; /* yes; that's the index */ else { const TValue *n = getgeneric(t, key, 1); - if (unlikely(isabstkey(n))) + if (l_unlikely(isabstkey(n))) luaG_runerror(L, "invalid key to 'next'"); /* key not found */ i = cast_int(nodefromval(n) - gnode(t, 0)); /* key index in hash table */ /* hash elements are numbered after array ones */ @@ -541,7 +541,7 @@ void luaH_resize (lua_State *L, Table *t, unsigned int newasize, } /* allocate new array */ newarray = luaM_reallocvector(L, t->array, oldasize, newasize, TValue); - if (unlikely(newarray == NULL && newasize > 0)) { /* allocation failed? */ + if (l_unlikely(newarray == NULL && newasize > 0)) { /* allocation failed? */ freehash(L, &newt); /* release new hash part */ luaM_error(L); /* raise error (with array unchanged) */ } @@ -635,7 +635,7 @@ static Node *getfreepos (Table *t) { void luaH_newkey (lua_State *L, Table *t, const TValue *key, TValue *value) { Node *mp; TValue aux; - if (unlikely(ttisnil(key))) + if (l_unlikely(ttisnil(key))) luaG_runerror(L, "table index is nil"); else if (ttisfloat(key)) { lua_Number f = fltvalue(key); @@ -644,7 +644,7 @@ void luaH_newkey (lua_State *L, Table *t, const TValue *key, TValue *value) { setivalue(&aux, k); key = &aux; /* insert it as an integer */ } - else if (unlikely(luai_numisnan(f))) + else if (l_unlikely(luai_numisnan(f))) luaG_runerror(L, "table index is NaN"); } if (ttisnil(value)) diff --git a/ltablib.c b/ltablib.c index c7f0e4dc51..d80eb80154 100644 --- a/ltablib.c +++ b/ltablib.c @@ -145,7 +145,7 @@ static int tmove (lua_State *L) { static void addfield (lua_State *L, luaL_Buffer *b, lua_Integer i) { lua_geti(L, 1, i); - if (!lua_isstring(L, -1)) + if (l_unlikely(!lua_isstring(L, -1))) luaL_error(L, "invalid value (%s) at index %I in table for 'concat'", luaL_typename(L, -1), i); luaL_addvalue(b); @@ -196,7 +196,8 @@ static int tunpack (lua_State *L) { lua_Integer e = luaL_opt(L, luaL_checkinteger, 3, luaL_len(L, 1)); if (i > e) return 0; /* empty range */ n = (lua_Unsigned)e - i; /* number of elements minus 1 (avoid overflows) */ - if (n >= (unsigned int)INT_MAX || !lua_checkstack(L, (int)(++n))) + if (l_unlikely(n >= (unsigned int)INT_MAX || + !lua_checkstack(L, (int)(++n)))) return luaL_error(L, "too many results to unpack"); for (; i < e; i++) { /* push arg[i..e - 1] (to avoid overflows) */ lua_geti(L, 1, i); @@ -300,14 +301,14 @@ static IdxT partition (lua_State *L, IdxT lo, IdxT up) { for (;;) { /* next loop: repeat ++i while a[i] < P */ while ((void)lua_geti(L, 1, ++i), sort_comp(L, -1, -2)) { - if (i == up - 1) /* a[i] < P but a[up - 1] == P ?? */ + if (l_unlikely(i == up - 1)) /* a[i] < P but a[up - 1] == P ?? */ luaL_error(L, "invalid order function for sorting"); lua_pop(L, 1); /* remove a[i] */ } /* after the loop, a[i] >= P and a[lo .. i - 1] < P */ /* next loop: repeat --j while P < a[j] */ while ((void)lua_geti(L, 1, --j), sort_comp(L, -3, -1)) { - if (j < i) /* j < i but a[j] > P ?? */ + if (l_unlikely(j < i)) /* j < i but a[j] > P ?? */ luaL_error(L, "invalid order function for sorting"); lua_pop(L, 1); /* remove a[j] */ } diff --git a/ltm.c b/ltm.c index 4770f96bb6..b657b783a8 100644 --- a/ltm.c +++ b/ltm.c @@ -147,7 +147,7 @@ static int callbinTM (lua_State *L, const TValue *p1, const TValue *p2, void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2, StkId res, TMS event) { - if (!callbinTM(L, p1, p2, res, event)) { + if (l_unlikely(!callbinTM(L, p1, p2, res, event))) { switch (event) { case TM_BAND: case TM_BOR: case TM_BXOR: case TM_SHL: case TM_SHR: case TM_BNOT: { @@ -166,7 +166,8 @@ void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2, void luaT_tryconcatTM (lua_State *L) { StkId top = L->top; - if (!callbinTM(L, s2v(top - 2), s2v(top - 1), top - 2, TM_CONCAT)) + if (l_unlikely(!callbinTM(L, s2v(top - 2), s2v(top - 1), top - 2, + TM_CONCAT))) luaG_concaterror(L, s2v(top - 2), s2v(top - 1)); } diff --git a/luaconf.h b/luaconf.h index a44858c4d5..ae73e2fd66 100644 --- a/luaconf.h +++ b/luaconf.h @@ -660,6 +660,26 @@ #define lua_getlocaledecpoint() (localeconv()->decimal_point[0]) #endif + +/* +** macros to improve jump prediction, used mostly for error handling +** and debug facilities. +*/ +#if (defined(LUA_CORE) || defined(LUA_LIB)) && !defined(l_likely) + +#include +#if defined(__GNUC__) +#define l_likely(x) (__builtin_expect(((x) != 0), 1)) +#define l_unlikely(x) (__builtin_expect(((x) != 0), 0)) +#else +#define l_likely(x) (x) +#define l_unlikely(x) (x) +#endif + +#endif + + + /* }================================================================== */ diff --git a/lvm.c b/lvm.c index 1252ecbfae..728acd4604 100644 --- a/lvm.c +++ b/lvm.c @@ -235,11 +235,11 @@ static int forprep (lua_State *L, StkId ra) { } else { /* try making all values floats */ lua_Number init; lua_Number limit; lua_Number step; - if (unlikely(!tonumber(plimit, &limit))) + if (l_unlikely(!tonumber(plimit, &limit))) luaG_forerror(L, plimit, "limit"); - if (unlikely(!tonumber(pstep, &step))) + if (l_unlikely(!tonumber(pstep, &step))) luaG_forerror(L, pstep, "step"); - if (unlikely(!tonumber(pinit, &init))) + if (l_unlikely(!tonumber(pinit, &init))) luaG_forerror(L, pinit, "initial value"); if (step == 0) luaG_runerror(L, "'for' step is zero"); @@ -292,7 +292,7 @@ void luaV_finishget (lua_State *L, const TValue *t, TValue *key, StkId val, if (slot == NULL) { /* 't' is not a table? */ lua_assert(!ttistable(t)); tm = luaT_gettmbyobj(L, t, TM_INDEX); - if (unlikely(notm(tm))) + if (l_unlikely(notm(tm))) luaG_typeerror(L, t, "index"); /* no metamethod */ /* else will try the metamethod */ } @@ -346,7 +346,7 @@ void luaV_finishset (lua_State *L, const TValue *t, TValue *key, } else { /* not a table; check metamethod */ tm = luaT_gettmbyobj(L, t, TM_NEWINDEX); - if (unlikely(notm(tm))) + if (l_unlikely(notm(tm))) luaG_typeerror(L, t, "index"); } /* try the metamethod */ @@ -651,7 +651,7 @@ void luaV_concat (lua_State *L, int total) { /* collect total length and number of strings */ for (n = 1; n < total && tostring(L, s2v(top - n - 1)); n++) { size_t l = vslen(s2v(top - n - 1)); - if (unlikely(l >= (MAX_SIZE/sizeof(char)) - tl)) + if (l_unlikely(l >= (MAX_SIZE/sizeof(char)) - tl)) luaG_runerror(L, "string length overflow"); tl += l; } @@ -695,7 +695,7 @@ void luaV_objlen (lua_State *L, StkId ra, const TValue *rb) { } default: { /* try metamethod */ tm = luaT_gettmbyobj(L, rb, TM_LEN); - if (unlikely(notm(tm))) /* no metamethod? */ + if (l_unlikely(notm(tm))) /* no metamethod? */ luaG_typeerror(L, rb, "get length of"); break; } @@ -711,7 +711,7 @@ void luaV_objlen (lua_State *L, StkId ra, const TValue *rb) { ** otherwise 'floor(q) == trunc(q) - 1'. */ lua_Integer luaV_idiv (lua_State *L, lua_Integer m, lua_Integer n) { - if (unlikely(l_castS2U(n) + 1u <= 1u)) { /* special cases: -1 or 0 */ + if (l_unlikely(l_castS2U(n) + 1u <= 1u)) { /* special cases: -1 or 0 */ if (n == 0) luaG_runerror(L, "attempt to divide by zero"); return intop(-, 0, m); /* n==-1; avoid overflow with 0x80000...//-1 */ @@ -731,7 +731,7 @@ lua_Integer luaV_idiv (lua_State *L, lua_Integer m, lua_Integer n) { ** about luaV_idiv.) */ lua_Integer luaV_mod (lua_State *L, lua_Integer m, lua_Integer n) { - if (unlikely(l_castS2U(n) + 1u <= 1u)) { /* special cases: -1 or 0 */ + if (l_unlikely(l_castS2U(n) + 1u <= 1u)) { /* special cases: -1 or 0 */ if (n == 0) luaG_runerror(L, "attempt to perform 'n%%0'"); return 0; /* m % -1 == 0; avoid overflow with 0x80000...%-1 */ @@ -1049,7 +1049,8 @@ void luaV_finishOp (lua_State *L) { #define updatebase(ci) (base = ci->func + 1) -#define updatestack(ci) { if (trap) { updatebase(ci); ra = RA(i); } } +#define updatestack(ci) \ + { if (l_unlikely(trap)) { updatebase(ci); ra = RA(i); } } /* @@ -1107,7 +1108,7 @@ void luaV_finishOp (lua_State *L) { /* fetch an instruction and prepare its execution */ #define vmfetch() { \ - if (trap) { /* stack reallocation or hooks? */ \ + if (l_unlikely(trap)) { /* stack reallocation or hooks? */ \ trap = luaG_traceexec(L, pc); /* handle hooks */ \ updatebase(ci); /* correct stack */ \ } \ @@ -1135,7 +1136,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { cl = clLvalue(s2v(ci->func)); k = cl->p->k; pc = ci->u.l.savedpc; - if (trap) { + if (l_unlikely(trap)) { if (pc == cl->p->code) { /* first instruction (not resuming)? */ if (cl->p->is_vararg) trap = 0; /* hooks will start after VARARGPREP instruction */ @@ -1678,23 +1679,23 @@ void luaV_execute (lua_State *L, CallInfo *ci) { goto ret; } vmcase(OP_RETURN0) { - if (L->hookmask) { + if (l_unlikely(L->hookmask)) { L->top = ra; savepc(ci); luaD_poscall(L, ci, 0); /* no hurry... */ trap = 1; } else { /* do the 'poscall' here */ - int nres = ci->nresults; + int nres; L->ci = ci->previous; /* back to caller */ L->top = base - 1; - while (nres-- > 0) + for (nres = ci->nresults; l_unlikely(nres > 0); nres--) setnilvalue(s2v(L->top++)); /* all results are nil */ } goto ret; } vmcase(OP_RETURN1) { - if (L->hookmask) { + if (l_unlikely(L->hookmask)) { L->top = ra + 1; savepc(ci); luaD_poscall(L, ci, 1); /* no hurry... */ @@ -1708,8 +1709,8 @@ void luaV_execute (lua_State *L, CallInfo *ci) { else { setobjs2s(L, base - 1, ra); /* at least this result */ L->top = base; - while (--nres > 0) /* complete missing results */ - setnilvalue(s2v(L->top++)); + for (; l_unlikely(nres > 1); nres--) + setnilvalue(s2v(L->top++)); /* complete missing results */ } } ret: /* return from a Lua function */ @@ -1812,7 +1813,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { } vmcase(OP_VARARGPREP) { ProtectNT(luaT_adjustvarargs(L, GETARG_A(i), ci, cl->p)); - if (trap) { + if (l_unlikely(trap)) { /* previous "Protect" updated trap */ luaD_hookcall(L, ci); L->oldpc = 1; /* next opcode will be seen as a "new" line */ } diff --git a/lvm.h b/lvm.h index 2d4ac160fe..1bc16f3a50 100644 --- a/lvm.h +++ b/lvm.h @@ -60,12 +60,14 @@ typedef enum { /* convert an object to an integer (including string coercion) */ #define tointeger(o,i) \ - (ttisinteger(o) ? (*(i) = ivalue(o), 1) : luaV_tointeger(o,i,LUA_FLOORN2I)) + (l_likely(ttisinteger(o)) ? (*(i) = ivalue(o), 1) \ + : luaV_tointeger(o,i,LUA_FLOORN2I)) /* convert an object to an integer (without string coercion) */ #define tointegerns(o,i) \ - (ttisinteger(o) ? (*(i) = ivalue(o), 1) : luaV_tointegerns(o,i,LUA_FLOORN2I)) + (l_likely(ttisinteger(o)) ? (*(i) = ivalue(o), 1) \ + : luaV_tointegerns(o,i,LUA_FLOORN2I)) #define intop(op,v1,v2) l_castU2S(l_castS2U(v1) op l_castS2U(v2)) From 31925e4cc20018b2cf46664febd6347ce4a4b766 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 24 Feb 2021 11:30:46 -0300 Subject: [PATCH 0680/1145] Details Added documentation and asserts that constants for arithmetic opcodes must be numbers. --- lopcodes.h | 14 +++++++------- lvm.c | 4 ++-- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lopcodes.h b/lopcodes.h index 120cdd9438..d6a47e5af9 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -225,13 +225,13 @@ OP_SELF,/* A B C R[A+1] := R[B]; R[A] := R[B][RK(C):string] */ OP_ADDI,/* A B sC R[A] := R[B] + sC */ -OP_ADDK,/* A B C R[A] := R[B] + K[C] */ -OP_SUBK,/* A B C R[A] := R[B] - K[C] */ -OP_MULK,/* A B C R[A] := R[B] * K[C] */ -OP_MODK,/* A B C R[A] := R[B] % K[C] */ -OP_POWK,/* A B C R[A] := R[B] ^ K[C] */ -OP_DIVK,/* A B C R[A] := R[B] / K[C] */ -OP_IDIVK,/* A B C R[A] := R[B] // K[C] */ +OP_ADDK,/* A B C R[A] := R[B] + K[C]:number */ +OP_SUBK,/* A B C R[A] := R[B] - K[C]:number */ +OP_MULK,/* A B C R[A] := R[B] * K[C]:number */ +OP_MODK,/* A B C R[A] := R[B] % K[C]:number */ +OP_POWK,/* A B C R[A] := R[B] ^ K[C]:number */ +OP_DIVK,/* A B C R[A] := R[B] / K[C]:number */ +OP_IDIVK,/* A B C R[A] := R[B] // K[C]:number */ OP_BANDK,/* A B C R[A] := R[B] & K[C]:integer */ OP_BORK,/* A B C R[A] := R[B] | K[C]:integer */ diff --git a/lvm.c b/lvm.c index 728acd4604..c0a10d6c7c 100644 --- a/lvm.c +++ b/lvm.c @@ -921,7 +921,7 @@ void luaV_finishOp (lua_State *L) { */ #define op_arithfK(L,fop) { \ TValue *v1 = vRB(i); \ - TValue *v2 = KC(i); \ + TValue *v2 = KC(i); lua_assert(ttisnumber(v2)); \ op_arithf_aux(L, v1, v2, fop); } @@ -950,7 +950,7 @@ void luaV_finishOp (lua_State *L) { */ #define op_arithK(L,iop,fop) { \ TValue *v1 = vRB(i); \ - TValue *v2 = KC(i); \ + TValue *v2 = KC(i); lua_assert(ttisnumber(v2)); \ op_arith_aux(L, v1, v2, iop, fop); } From 5205f073c57ae4b69e90d35c02e3a1a1cca44eb4 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 24 Feb 2021 12:24:42 -0300 Subject: [PATCH 0681/1145] Don't use tointegerns when luaV_tointegerns will do Some places don't need the "fast path" macro tointegerns, either because speed is not essential (lcode.c) or because the value is not supposed to be an integer already (luaV_equalobj and luaG_tointerror). Moreover, luaV_equalobj should always use F2Ieq, even if Lua is compiled to "round to floor". --- lcode.c | 3 ++- ldebug.c | 2 +- lvm.c | 9 +++++++-- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/lcode.c b/lcode.c index 31f23f47a5..80d975cb85 100644 --- a/lcode.c +++ b/lcode.c @@ -1298,7 +1298,8 @@ static int validop (int op, TValue *v1, TValue *v2) { case LUA_OPBAND: case LUA_OPBOR: case LUA_OPBXOR: case LUA_OPSHL: case LUA_OPSHR: case LUA_OPBNOT: { /* conversion errors */ lua_Integer i; - return (tointegerns(v1, &i) && tointegerns(v2, &i)); + return (luaV_tointegerns(v1, &i, LUA_FLOORN2I) && + luaV_tointegerns(v2, &i, LUA_FLOORN2I)); } case LUA_OPDIV: case LUA_OPIDIV: case LUA_OPMOD: /* division by 0 */ return (nvalue(v2) != 0); diff --git a/ldebug.c b/ldebug.c index 0038d1b331..603c39fc12 100644 --- a/ldebug.c +++ b/ldebug.c @@ -726,7 +726,7 @@ l_noret luaG_opinterror (lua_State *L, const TValue *p1, */ l_noret luaG_tointerror (lua_State *L, const TValue *p1, const TValue *p2) { lua_Integer temp; - if (!tointegerns(p1, &temp)) + if (!luaV_tointegerns(p1, &temp, LUA_FLOORN2I)) p2 = p1; luaG_runerror(L, "number%s has no integer representation", varinfo(L, p2)); } diff --git a/lvm.c b/lvm.c index c0a10d6c7c..c9729bcca0 100644 --- a/lvm.c +++ b/lvm.c @@ -568,8 +568,13 @@ int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2) { if (ttype(t1) != ttype(t2) || ttype(t1) != LUA_TNUMBER) return 0; /* only numbers can be equal with different variants */ else { /* two numbers with different variants */ - lua_Integer i1, i2; /* compare them as integers */ - return (tointegerns(t1, &i1) && tointegerns(t2, &i2) && i1 == i2); + /* One of them is an integer. If the other does not have an + integer value, they cannot be equal; otherwise, compare their + integer values. */ + lua_Integer i1, i2; + return (luaV_tointegerns(t1, &i1, F2Ieq) && + luaV_tointegerns(t2, &i2, F2Ieq) && + i1 == i2); } } /* values have same type and same variant */ From e0260eb2d4085723302d637dd8f3fca339d18817 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 25 Feb 2021 13:39:36 -0300 Subject: [PATCH 0682/1145] Bug (kind of) in 'isinstack' The function 'isinstack' tried to work around the undefined behavior of subtracting two pointers that do not point to the same object, but the compiler killed to trick. (It optimizes out the safety check, because in a correct execution it will be always true.) --- ldebug.c | 16 ++++++++++------ testes/errors.lua | 7 +++++++ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/ldebug.c b/ldebug.c index 603c39fc12..8e3657a92b 100644 --- a/ldebug.c +++ b/ldebug.c @@ -638,14 +638,18 @@ static const char *funcnamefromcode (lua_State *L, CallInfo *ci, /* -** The subtraction of two potentially unrelated pointers is -** not ISO C, but it should not crash a program; the subsequent -** checks are ISO C and ensure a correct result. +** Check whether pointer 'o' points to some value in the stack +** frame of the current function. Because 'o' may not point to a +** value in this stack, we cannot compare it with the region +** boundaries (undefined behaviour in ISO C). */ static int isinstack (CallInfo *ci, const TValue *o) { - StkId base = ci->func + 1; - ptrdiff_t i = cast(StkId, o) - base; - return (0 <= i && i < (ci->top - base) && s2v(base + i) == o); + StkId pos; + for (pos = ci->func + 1; pos < ci->top; pos++) { + if (o == s2v(pos)) + return 1; + } + return 0; /* not found */ } diff --git a/testes/errors.lua b/testes/errors.lua index 4249f5703f..fd02806e37 100644 --- a/testes/errors.lua +++ b/testes/errors.lua @@ -191,6 +191,13 @@ checkmessage("a = 24 // 0", "divide by zero") checkmessage("a = 1 % 0", "'n%0'") +-- type error for an object which is neither in an upvalue nor a register. +-- The following code will try to index the value 10 that is stored in +-- the metatable, without moving it to a register. +checkmessage("local a = setmetatable({}, {__index = 10}).x", + "attempt to index a number value") + + -- numeric for loops checkmessage("for i = {}, 10 do end", "table") checkmessage("for i = io.stdin, 10 do end", "FILE") From 1537d6680bb66dc2484e11815bc2cd0e31ca39cc Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 26 Feb 2021 11:41:02 -0300 Subject: [PATCH 0683/1145] New control for reentrancy of emergency collections Instead of assuming that shrinking a block may be an emergency collection, use an explicit field ('gcstopem') to stop emergency collections while GC is working. --- lgc.c | 36 ++++++++++++++++++++++++------------ lmem.c | 25 ++++++++++++------------- lstate.c | 1 + lstate.h | 1 + testes/gc.lua | 8 ++++++++ 5 files changed, 46 insertions(+), 25 deletions(-) diff --git a/lgc.c b/lgc.c index 94e0486ef0..b360eed008 100644 --- a/lgc.c +++ b/lgc.c @@ -1575,52 +1575,64 @@ static int sweepstep (lua_State *L, global_State *g, static lu_mem singlestep (lua_State *L) { global_State *g = G(L); + lu_mem work; + lua_assert(!g->gcstopem); /* collector is not reentrant */ + g->gcstopem = 1; /* no emergency collections while collecting */ switch (g->gcstate) { case GCSpause: { restartcollection(g); g->gcstate = GCSpropagate; - return 1; + work = 1; + break; } case GCSpropagate: { if (g->gray == NULL) { /* no more gray objects? */ g->gcstate = GCSenteratomic; /* finish propagate phase */ - return 0; + work = 0; } else - return propagatemark(g); /* traverse one gray object */ + work = propagatemark(g); /* traverse one gray object */ + break; } case GCSenteratomic: { - lu_mem work = atomic(L); /* work is what was traversed by 'atomic' */ + work = atomic(L); /* work is what was traversed by 'atomic' */ entersweep(L); g->GCestimate = gettotalbytes(g); /* first estimate */; - return work; + break; } case GCSswpallgc: { /* sweep "regular" objects */ - return sweepstep(L, g, GCSswpfinobj, &g->finobj); + work = sweepstep(L, g, GCSswpfinobj, &g->finobj); + break; } case GCSswpfinobj: { /* sweep objects with finalizers */ - return sweepstep(L, g, GCSswptobefnz, &g->tobefnz); + work = sweepstep(L, g, GCSswptobefnz, &g->tobefnz); + break; } case GCSswptobefnz: { /* sweep objects to be finalized */ - return sweepstep(L, g, GCSswpend, NULL); + work = sweepstep(L, g, GCSswpend, NULL); + break; } case GCSswpend: { /* finish sweeps */ checkSizes(L, g); g->gcstate = GCScallfin; - return 0; + work = 0; + break; } case GCScallfin: { /* call remaining finalizers */ if (g->tobefnz && !g->gcemergency) { - int n = runafewfinalizers(L, GCFINMAX); - return n * GCFINALIZECOST; + g->gcstopem = 0; /* ok collections during finalizers */ + work = runafewfinalizers(L, GCFINMAX) * GCFINALIZECOST; } else { /* emergency mode or no more finalizers */ g->gcstate = GCSpause; /* finish collection */ - return 0; + work = 0; } + break; } default: lua_assert(0); return 0; } + g->gcstopem = 0; + return work; } diff --git a/lmem.c b/lmem.c index e90f991aff..9029d588c1 100644 --- a/lmem.c +++ b/lmem.c @@ -24,12 +24,12 @@ #if defined(EMERGENCYGCTESTS) /* -** First allocation will fail whenever not building initial state -** and not shrinking a block. (This fail will trigger 'tryagain' and -** a full GC cycle at every allocation.) +** First allocation will fail whenever not building initial state. +** (This fail will trigger 'tryagain' and a full GC cycle at every +** allocation.) */ static void *firsttry (global_State *g, void *block, size_t os, size_t ns) { - if (completestate(g) && ns > os) + if (completestate(g) && ns > 0) /* frees never fail */ return NULL; /* fail */ else /* normal allocation */ return (*g->frealloc)(g->ud, block, os, ns); @@ -138,15 +138,17 @@ void luaM_free_ (lua_State *L, void *block, size_t osize) { /* -** In case of allocation fail, this function will call the GC to try -** to free some memory and then try the allocation again. -** (It should not be called when shrinking a block, because then the -** interpreter may be in the middle of a collection step.) +** In case of allocation fail, this function will do an emergency +** collection to free some memory and then try the allocation again. +** The GC should not be called while state is not fully built, as the +** collector is not yet fully initialized. Also, it should not be called +** when 'gcstopem' is true, because then the interpreter is in the +** middle of a collection step. */ static void *tryagain (lua_State *L, void *block, size_t osize, size_t nsize) { global_State *g = G(L); - if (completestate(g)) { /* is state fully build? */ + if (completestate(g) && !g->gcstopem) { luaC_fullgc(L, 1); /* try to free some memory... */ return (*g->frealloc)(g->ud, block, osize, nsize); /* try again */ } @@ -156,8 +158,6 @@ static void *tryagain (lua_State *L, void *block, /* ** Generic allocation routine. -** If allocation fails while shrinking a block, do not try again; the -** GC shrinks some blocks and it is not reentrant. */ void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) { void *newblock; @@ -165,8 +165,7 @@ void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) { lua_assert((osize == 0) == (block == NULL)); newblock = firsttry(g, block, osize, nsize); if (l_unlikely(newblock == NULL && nsize > 0)) { - if (nsize > osize) /* not shrinking a block? */ - newblock = tryagain(L, block, osize, nsize); + newblock = tryagain(L, block, osize, nsize); if (newblock == NULL) /* still no memory? */ return NULL; /* do not update 'GCdebt' */ } diff --git a/lstate.c b/lstate.c index 04909db375..c5e3b437f1 100644 --- a/lstate.c +++ b/lstate.c @@ -379,6 +379,7 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { g->panic = NULL; g->gcstate = GCSpause; g->gckind = KGC_INC; + g->gcstopem = 0; g->gcemergency = 0; g->finobj = g->tobefnz = g->fixedgc = NULL; g->firstold1 = g->survival = g->old1 = g->reallyold = NULL; diff --git a/lstate.h b/lstate.h index 0322e2c665..c1283bb6b9 100644 --- a/lstate.h +++ b/lstate.h @@ -260,6 +260,7 @@ typedef struct global_State { lu_byte currentwhite; lu_byte gcstate; /* state of garbage collector */ lu_byte gckind; /* kind of GC running */ + lu_byte gcstopem; /* stops emergency collections */ lu_byte genminormul; /* control for minor generational collections */ lu_byte genmajormul; /* control for major generational collections */ lu_byte gcrunning; /* true if GC is running */ diff --git a/testes/gc.lua b/testes/gc.lua index 80850f9252..2332c939ad 100644 --- a/testes/gc.lua +++ b/testes/gc.lua @@ -676,6 +676,14 @@ end -- just to make sure assert(collectgarbage'isrunning') +do -- check that the collector is reentrant in incremental mode + setmetatable({}, {__gc = function () + collectgarbage() + end}) + collectgarbage() +end + + collectgarbage(oldmode) print('OK') From f9d857a81b87b695c1e3b34f1e7fe55884d1288f Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Sat, 27 Feb 2021 12:56:09 -0300 Subject: [PATCH 0684/1145] Stack reallocation done in two phases $he stack reallocation is done in two steps (allocation + free) because the correction of the pointers pointing into the stack must be done while both addresses (the old stack and the new one) are valid. In ISO C, any pointer use after the pointer has been deallocated is undefined behavior. The compiler option '-fsanitize=pointer-subtract' (plus what it needs to work) complained about the old implementation. --- ldo.c | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/ldo.c b/ldo.c index 7c9ce06ea3..7135079b12 100644 --- a/ldo.c +++ b/ldo.c @@ -160,8 +160,6 @@ int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) { static void correctstack (lua_State *L, StkId oldstack, StkId newstack) { CallInfo *ci; UpVal *up; - if (oldstack == newstack) - return; /* stack address did not change */ L->top = (L->top - oldstack) + newstack; L->tbclist = (L->tbclist - oldstack) + newstack; for (up = L->openupval; up != NULL; up = up->u.open.next) @@ -179,19 +177,35 @@ static void correctstack (lua_State *L, StkId oldstack, StkId newstack) { #define ERRORSTACKSIZE (LUAI_MAXSTACK + 200) +/* +** Reallocate the stack to a new size, correcting all pointers into +** it. (There are pointers to a stack from its upvalues, from its list +** of call infos, plus a few individual pointers.) The reallocation is +** done in two steps (allocation + free) because the correction must be +** done while both addresses (the old stack and the new one) are valid. +** (In ISO C, any pointer use after the pointer has been deallocated is +** undefined behavior.) +** In case of allocation error, raise an error or return false according +** to 'raiseerror'. +*/ int luaD_reallocstack (lua_State *L, int newsize, int raiseerror) { - int lim = stacksize(L); - StkId newstack = luaM_reallocvector(L, L->stack, - lim + EXTRA_STACK, newsize + EXTRA_STACK, StackValue); + int oldsize = stacksize(L); + int i; + StkId newstack = luaM_reallocvector(L, NULL, 0, + newsize + EXTRA_STACK, StackValue); lua_assert(newsize <= LUAI_MAXSTACK || newsize == ERRORSTACKSIZE); if (l_unlikely(newstack == NULL)) { /* reallocation failed? */ if (raiseerror) luaM_error(L); else return 0; /* do not raise an error */ } - for (; lim < newsize; lim++) - setnilvalue(s2v(newstack + lim + EXTRA_STACK)); /* erase new segment */ + /* number of elements to be copied to the new stack */ + i = ((oldsize <= newsize) ? oldsize : newsize) + EXTRA_STACK; + memcpy(newstack, L->stack, i * sizeof(StackValue)); + for (; i < newsize + EXTRA_STACK; i++) + setnilvalue(s2v(newstack + i)); /* erase new segment */ correctstack(L, L->stack, newstack); + luaM_freearray(L, L->stack, oldsize + EXTRA_STACK); L->stack = newstack; L->stack_last = L->stack + newsize; return 1; From 5276973224066e591b0f1a79c3b091d395848ac4 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 1 Mar 2021 13:54:29 -0300 Subject: [PATCH 0685/1145] New test module 'tracegc' New module easies the inclusion of GC tracing in individual test files. --- testes/all.lua | 14 ++------------ testes/cstack.lua | 12 ++++++++---- testes/locals.lua | 6 ++++-- testes/tracegc.lua | 40 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 54 insertions(+), 18 deletions(-) create mode 100644 testes/tracegc.lua diff --git a/testes/all.lua b/testes/all.lua index a4feeec1b9..a8e44024bc 100644 --- a/testes/all.lua +++ b/testes/all.lua @@ -154,18 +154,8 @@ end dofile('main.lua') -do - local next, setmetatable, stderr = next, setmetatable, io.stderr - -- track collections - local mt = {} - -- each time a table is collected, remark it for finalization - -- on next cycle - mt.__gc = function (o) - stderr:write'.' -- mark progress - local n = setmetatable(o, mt) -- remark it - end - local n = setmetatable({}, mt) -- create object -end +-- trace GC cycles +require"tracegc".start() report"gc.lua" local f = assert(loadfile('gc.lua')) diff --git a/testes/cstack.lua b/testes/cstack.lua index 7bd5506342..213d15d470 100644 --- a/testes/cstack.lua +++ b/testes/cstack.lua @@ -2,6 +2,8 @@ -- See Copyright Notice in file all.lua +local tracegc = require"tracegc" + print"testing stack overflow detection" -- Segmentation faults in these tests probably result from a C-stack @@ -21,7 +23,9 @@ do print("testing stack overflow in message handling") count = count + 1 return 1 + loop(x, y, z) end + tracegc.stop() -- __gc should not be called with a full stack local res, msg = xpcall(loop, loop) + tracegc.start() assert(msg == "error in error handling") print("final count: ", count) end @@ -135,18 +139,18 @@ if T then local topB, sizeB -- top and size Before overflow local topA, sizeA -- top and size After overflow topB, sizeB = T.stacklevel() - collectgarbage("stop") -- __gc should not be called with a full stack + tracegc.stop() -- __gc should not be called with a full stack xpcall(f, err) - collectgarbage("restart") + tracegc.start() topA, sizeA = T.stacklevel() -- sizes should be comparable assert(topA == topB and sizeA < sizeB * 2) print(string.format("maximum stack size: %d", stack1)) LIM = N -- will stop recursion at maximum level N = 0 -- to count again - collectgarbage("stop") -- __gc should not be called with a full stack + tracegc.stop() -- __gc should not be called with a full stack f() - collectgarbage("restart") + tracegc.start() print"+" end diff --git a/testes/locals.lua b/testes/locals.lua index a93839dbc8..2c67edbdb3 100644 --- a/testes/locals.lua +++ b/testes/locals.lua @@ -5,6 +5,8 @@ print('testing local variables and environments') local debug = require"debug" +local tracegc = require"tracegc" + -- bug in 5.1: @@ -554,9 +556,9 @@ do -- test for tbc variable high in the stack obj[1] = 100 flag = obj end) - collectgarbage("stop") + tracegc.stop() st, obj = xpcall(overflow, errorh, 0) - collectgarbage("restart") + tracegc.start() end) co() assert(not st and obj[1] == 10 and flag[1] == 100) diff --git a/testes/tracegc.lua b/testes/tracegc.lua new file mode 100644 index 0000000000..9c5c1b3f51 --- /dev/null +++ b/testes/tracegc.lua @@ -0,0 +1,40 @@ +-- track collections + +local M = {} + +-- import list +local setmetatable, stderr, collectgarbage = + setmetatable, io.stderr, collectgarbage + +_ENV = nil + +local active = false + + +-- each time a table is collected, remark it for finalization on next +-- cycle +local mt = {} +function mt.__gc (o) + stderr:write'.' -- mark progress + if active then + setmetatable(o, mt) -- remark object for finalization + end +end + + +function M.start () + if not active then + active = true + setmetatable({}, mt) -- create initial object + end +end + + +function M.stop () + if active then + active = false + collectgarbage() -- call finalizer for the last time + end +end + +return M From 9a2de786de5da862404995ddd3408f7ad3d54ee8 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 2 Mar 2021 11:35:40 -0300 Subject: [PATCH 0686/1145] Stack check in warning function for tests The warning function using for tests need to check the stack before pushing anything. (Warning functions are not expected to access a Lua state, therefore they have no preallocated stack space.) --- ltests.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ltests.c b/ltests.c index da95d0272c..a50f783047 100644 --- a/ltests.c +++ b/ltests.c @@ -121,6 +121,7 @@ static void warnf (void *ud, const char *msg, int tocont) { strcat(buff, msg); /* add new message to current warning */ if (!tocont) { /* message finished? */ lua_unlock(L); + luaL_checkstack(L, 1, "warn stack space"); lua_getglobal(L, "_WARN"); if (!lua_toboolean(L, -1)) lua_pop(L, 1); /* ok, no previous unexpected warning */ @@ -142,6 +143,7 @@ static void warnf (void *ud, const char *msg, int tocont) { } case 2: { /* store */ lua_unlock(L); + luaL_checkstack(L, 1, "warn stack space"); lua_pushstring(L, buff); lua_setglobal(L, "_WARN"); /* assign message to global '_WARN' */ lua_lock(L); From cf23a93d820558acdb8b1f0db85fdb94e709fee2 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 2 Mar 2021 11:39:42 -0300 Subject: [PATCH 0687/1145] Added assertions for proper use of string buffers --- lauxlib.c | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/lauxlib.c b/lauxlib.c index 2610f90e56..94835ef934 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -515,6 +515,15 @@ static void newbox (lua_State *L) { #define buffonstack(B) ((B)->b != (B)->init.b) +/* +** Whenever buffer is accessed, slot 'idx' must either be a box (which +** cannot be NULL) or it is a placeholder for the buffer. +*/ +#define checkbufferlevel(B,idx) \ + lua_assert(buffonstack(B) ? lua_touserdata(B->L, idx) != NULL \ + : lua_touserdata(B->L, idx) == (void*)B) + + /* ** Compute new size for buffer 'B', enough to accommodate extra 'sz' ** bytes. @@ -531,10 +540,11 @@ static size_t newbuffsize (luaL_Buffer *B, size_t sz) { /* ** Returns a pointer to a free area with at least 'sz' bytes in buffer -** 'B'. 'boxidx' is the relative position in the stack where the -** buffer's box is or should be. +** 'B'. 'boxidx' is the relative position in the stack where is the +** buffer's box or its placeholder. */ static char *prepbuffsize (luaL_Buffer *B, size_t sz, int boxidx) { + checkbufferlevel(B, boxidx); if (B->size - B->n >= sz) /* enough space? */ return B->b + B->n; else { @@ -545,6 +555,7 @@ static char *prepbuffsize (luaL_Buffer *B, size_t sz, int boxidx) { if (buffonstack(B)) /* buffer already has a box? */ newbuff = (char *)resizebox(L, boxidx, newsize); /* resize it */ else { /* no box yet */ + lua_remove(L, boxidx); /* remove placeholder */ newbox(L); /* create a new box */ lua_insert(L, boxidx); /* move box to its intended position */ lua_toclose(L, boxidx); @@ -581,11 +592,11 @@ LUALIB_API void luaL_addstring (luaL_Buffer *B, const char *s) { LUALIB_API void luaL_pushresult (luaL_Buffer *B) { lua_State *L = B->L; + checkbufferlevel(B, -1); lua_pushlstring(L, B->b, B->n); - if (buffonstack(B)) { + if (buffonstack(B)) lua_closeslot(L, -2); /* close the box */ - lua_remove(L, -2); /* remove box from the stack */ - } + lua_remove(L, -2); /* remove box or placeholder from the stack */ } @@ -620,6 +631,7 @@ LUALIB_API void luaL_buffinit (lua_State *L, luaL_Buffer *B) { B->b = B->init.b; B->n = 0; B->size = LUAL_BUFFERSIZE; + lua_pushlightuserdata(L, (void*)B); /* push placeholder */ } From b7eb21c1efbd33affb87479fc6055914fe9ab009 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 2 Mar 2021 13:50:00 -0300 Subject: [PATCH 0688/1145] Normalization of metamethod typography in the manual --- manual/manual.of | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/manual/manual.of b/manual/manual.of index e7040b2b10..2e15839a3d 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -331,7 +331,7 @@ under certain events. You can change several aspects of the behavior of a value by setting specific fields in its metatable. For instance, when a non-numeric value is the operand of an addition, -Lua checks for a function in the field @St{__add} of the value's metatable. +Lua checks for a function in the field @idx{__add} of the value's metatable. If it finds one, Lua calls this function to perform the addition. @@ -344,7 +344,7 @@ In the previous example, the key is the string @St{__add} and the metamethod is the function that performs the addition. Unless stated otherwise, a metamethod may in fact be any @x{callable value}, -which is either a function or a value with a @id{__call} metamethod. +which is either a function or a value with a @idx{__call} metamethod. You can query the metatable of any value using the @Lid{getmetatable} function. @@ -505,7 +505,7 @@ when @id{key} is not present in @id{table}. The metavalue is looked up in the metatable of @id{table}. The metavalue for this event can be either a function, a table, -or any value with an @id{__index} metavalue. +or any value with an @idx{__index} metavalue. If it is a function, it is called with @id{table} and @id{key} as arguments, and the result of the call @@ -514,7 +514,7 @@ is the result of the operation. Otherwise, the final result is the result of indexing this metavalue with @id{key}. This indexing is regular, not raw, -and therefore can trigger another @id{__index} metavalue. +and therefore can trigger another @idx{__index} metavalue. } @item{@idx{__newindex}| @@ -526,14 +526,14 @@ The metavalue is looked up in the metatable of @id{table}. Like with indexing, the metavalue for this event can be either a function, a table, -or any value with an @id{__newindex} metavalue. +or any value with an @idx{__newindex} metavalue. If it is a function, it is called with @id{table}, @id{key}, and @id{value} as arguments. Otherwise, Lua repeats the indexing assignment over this metavalue with the same key and value. This assignment is regular, not raw, -and therefore can trigger another @id{__newindex} metavalue. +and therefore can trigger another @idx{__newindex} metavalue. Whenever a @idx{__newindex} metavalue is invoked, Lua does not perform the primitive assignment. @@ -742,7 +742,7 @@ For an object (table or userdata) to be finalized when collected, you must @emph{mark} it for finalization. @index{mark (for finalization)} You mark an object for finalization when you set its metatable -and the metatable has a field indexed by the string @St{__gc}. +and the metatable has a @idx{__gc} metamethod. Note that if you set a metatable without a @idx{__gc} field and later create that field in the metatable, the object will not be marked for finalization. @@ -3102,7 +3102,7 @@ Close the to-be-closed slot at the given index and set its value to @nil. The index must be the last index previously marked to be closed @see{lua_toclose} that is still active (that is, not closed yet). -A @Lid{__close} metamethod cannot yield +A @idx{__close} metamethod cannot yield when called through this function. (Exceptionally, this function was introduced in release 5.4.3. @@ -9094,7 +9094,7 @@ want the old behavior } @item{ -The use of the @idx{__lt} metamethod to emulate @id{__le} +The use of the @idx{__lt} metamethod to emulate @idx{__le} has been removed. When needed, this metamethod must be explicitly defined. } @@ -9130,7 +9130,7 @@ like any other error when calling a finalizer.) The function @Lid{print} does not call @Lid{tostring} to format its arguments; instead, it has this functionality hardwired. -You should use @id{__tostring} to modify how values are printed. +You should use @idx{__tostring} to modify how values are printed. } @item{ From e7803f7dbcdc966ab1f9db143424ee811ab1a398 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 3 Mar 2021 09:44:20 -0300 Subject: [PATCH 0689/1145] New release number (5.4.3) --- lua.h | 6 +++--- manual/2html | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lua.h b/lua.h index aec70dacf3..820535b948 100644 --- a/lua.h +++ b/lua.h @@ -18,14 +18,14 @@ #define LUA_VERSION_MAJOR "5" #define LUA_VERSION_MINOR "4" -#define LUA_VERSION_RELEASE "2" +#define LUA_VERSION_RELEASE "3" #define LUA_VERSION_NUM 504 #define LUA_VERSION_RELEASE_NUM (LUA_VERSION_NUM * 100 + 0) #define LUA_VERSION "Lua " LUA_VERSION_MAJOR "." LUA_VERSION_MINOR #define LUA_RELEASE LUA_VERSION "." LUA_VERSION_RELEASE -#define LUA_COPYRIGHT LUA_RELEASE " Copyright (C) 1994-2020 Lua.org, PUC-Rio" +#define LUA_COPYRIGHT LUA_RELEASE " Copyright (C) 1994-2021 Lua.org, PUC-Rio" #define LUA_AUTHORS "R. Ierusalimschy, L. H. de Figueiredo, W. Celes" @@ -492,7 +492,7 @@ struct lua_Debug { /****************************************************************************** -* Copyright (C) 1994-2020 Lua.org, PUC-Rio. +* Copyright (C) 1994-2021 Lua.org, PUC-Rio. * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the diff --git a/manual/2html b/manual/2html index ea3957b9eb..f3244bf8a0 100755 --- a/manual/2html +++ b/manual/2html @@ -30,7 +30,7 @@ by Roberto Ierusalimschy, Luiz Henrique de Figueiredo, Waldemar Celes

Copyright -© 2020 Lua.org, PUC-Rio. All rights reserved. +© 2021 Lua.org, PUC-Rio. All rights reserved.


From f5df7f91f70234850484d26caf24e71e001e5304 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 5 Mar 2021 12:10:34 -0300 Subject: [PATCH 0690/1145] Wrong assertion in 'getbaseline' The assertion cannot compute 'f->abslineinfo[i]' when the initial estimate 'i' is -1. --- ldebug.c | 5 ++++- testes/errors.lua | 8 ++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/ldebug.c b/ldebug.c index 8e3657a92b..1feaab229f 100644 --- a/ldebug.c +++ b/ldebug.c @@ -50,6 +50,8 @@ static int currentpc (CallInfo *ci) { ** an integer division gets the right place. When the source file has ** large sequences of empty/comment lines, it may need extra entries, ** so the original estimate needs a correction. +** If the original estimate is -1, the initial 'if' ensures that the +** 'while' will run at least once. ** The assertion that the estimate is a lower bound for the correct base ** is valid as long as the debug info has been generated with the same ** value for MAXIWTHABS or smaller. (Previous releases use a little @@ -63,7 +65,8 @@ static int getbaseline (const Proto *f, int pc, int *basepc) { else { int i = cast_uint(pc) / MAXIWTHABS - 1; /* get an estimate */ /* estimate must be a lower bond of the correct base */ - lua_assert(i < f->sizeabslineinfo && f->abslineinfo[i].pc <= pc); + lua_assert(i < 0 || + (i < f->sizeabslineinfo && f->abslineinfo[i].pc <= pc)); while (i + 1 < f->sizeabslineinfo && pc >= f->abslineinfo[i + 1].pc) i++; /* low estimate; adjust it */ *basepc = f->abslineinfo[i].pc; diff --git a/testes/errors.lua b/testes/errors.lua index fd02806e37..a3d0676b15 100644 --- a/testes/errors.lua +++ b/testes/errors.lua @@ -420,6 +420,14 @@ if not b then end end]], 5) +do + -- Force a negative estimate for base line. Error in instruction 2 + -- (after VARARGPREP, GETGLOBAL), with first absolute line information + -- (forced by too many lines) in instruction 0. + local s = string.format("%s return __A.x", string.rep("\n", 300)) + lineerror(s, 301) +end + if not _soft then -- several tests that exaust the Lua stack From 511d53a826760dd11cd82947184583e2d094e2d2 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 9 Mar 2021 11:42:45 -0300 Subject: [PATCH 0691/1145] lua_settop/lua_pop closes to-be-closed variables The existence of 'lua_closeslot' is no reason for lua_pop not to close to-be-closed variables too. It is too error-prone for lua_pop not to close tbc variables being popped from the stack. --- lapi.c | 15 ++++++++------- manual/manual.of | 23 +++++++++++------------ testes/api.lua | 26 +++++++++++++++++++++++++- 3 files changed, 44 insertions(+), 20 deletions(-) diff --git a/lapi.c b/lapi.c index a9cf2fdb0a..f8f70cd008 100644 --- a/lapi.c +++ b/lapi.c @@ -173,7 +173,7 @@ LUA_API int lua_gettop (lua_State *L) { LUA_API void lua_settop (lua_State *L, int idx) { CallInfo *ci; - StkId func; + StkId func, newtop; ptrdiff_t diff; /* difference for new top */ lua_lock(L); ci = L->ci; @@ -188,12 +188,13 @@ LUA_API void lua_settop (lua_State *L, int idx) { api_check(L, -(idx+1) <= (L->top - (func + 1)), "invalid new top"); diff = idx + 1; /* will "subtract" index (as it is negative) */ } -#if defined(LUA_COMPAT_5_4_0) - if (diff < 0 && hastocloseCfunc(ci->nresults)) - luaF_close(L, L->top + diff, CLOSEKTOP, 0); -#endif - api_check(L, L->tbclist < L->top + diff, "cannot pop an unclosed slot"); - L->top += diff; + api_check(L, L->tbclist < L->top, "previous pop of an unclosed slot"); + newtop = L->top + diff; + if (diff < 0 && L->tbclist >= newtop) { + lua_assert(hastocloseCfunc(ci->nresults)); + luaF_close(L, newtop, CLOSEKTOP, 0); + } + L->top = newtop; /* correct top only after closing any upvalue */ lua_unlock(L); } diff --git a/manual/manual.of b/manual/manual.of index 2e15839a3d..c69970d2ad 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -4253,12 +4253,8 @@ If the new top is greater than the old one, then the new elements are filled with @nil. If @id{index} @N{is 0}, then all stack elements are removed. -For compatibility reasons, -this function may close slots marked as to-be-closed @see{lua_toclose}, -and therefore it can run arbitrary code. -You should not rely on this behavior: -Instead, always close to-be-closed slots explicitly, -with @Lid{lua_closeslot}, before removing them from the stack. +This function can run arbitrary code when removing an index +marked as to-be-closed from the stack. } @@ -4347,19 +4343,22 @@ otherwise, returns @id{NULL}. @apii{0,0,m} Marks the given index in the stack as a -to-be-closed @Q{variable} @see{to-be-closed}. +to-be-closed slot @see{to-be-closed}. Like a to-be-closed variable in Lua, -the value at that index in the stack will be closed +the value at that slot in the stack will be closed when it goes out of scope. Here, in the context of a C function, to go out of scope means that the running function returns to Lua, -there is an error, +or there is an error, +or the slot is removed from the stack through +@Lid{lua_settop} or @Lid{lua_pop}, or there is a call to @Lid{lua_closeslot}. -An index marked as to-be-closed should neither be removed from the stack -nor modified before a corresponding call to @Lid{lua_closeslot}. +A slot marked as to-be-closed should not be removed from the stack +by any other function in the API except @Lid{lua_settop} or @Lid{lua_pop}, +unless previously deactivated by @Lid{lua_closeslot}. This function should not be called for an index -that is equal to or below an active to-be-closed index. +that is equal to or below an active to-be-closed slot. Note that, both in case of errors and of a regular return, by the time the @idx{__close} metamethod runs, diff --git a/testes/api.lua b/testes/api.lua index fb7e708566..c1bcb4b7b4 100644 --- a/testes/api.lua +++ b/testes/api.lua @@ -1130,7 +1130,7 @@ do -- closing resources with 'closeslot' _ENV.xxx = true local a = T.testC([[ - pushvalue 2 # stack: S, NR, CH + pushvalue 2 # stack: S, NR, CH, NR call 0 1 # create resource; stack: S, NR, CH, R toclose -1 # mark it to be closed pushvalue 2 # stack: S, NR, CH, R, NR @@ -1151,6 +1151,30 @@ do ]], newresource, check) assert(a == 3 and _ENV.xxx == nil) -- no extra items left in the stack + -- closing resources with 'pop' + local a = T.testC([[ + pushvalue 2 # stack: S, NR, CH, NR + call 0 1 # create resource; stack: S, NR, CH, R + toclose -1 # mark it to be closed + pushvalue 2 # stack: S, NR, CH, R, NR + call 0 1 # create another resource; stack: S, NR, CH, R, R + toclose -1 # mark it to be closed + pushvalue 3 # stack: S, NR, CH, R, R, CH + pushint 2 # there should be two open resources + call 1 0 # stack: S, NR, CH, R, R + pop 1 # pop second resource + pushvalue 3 # stack: S, NR, CH, R, CH + pushint 1 # there should be one open resource + call 1 0 # stack: S, NR, CH, R + pop 1 # pop other resource from the stack + pushvalue 3 # stack: S, NR, CH, CH + pushint 0 # there should be no open resources + call 1 0 # stack: S, NR, CH + pushint * + return 1 # return stack size + ]], newresource, check) + assert(a == 3) -- no extra items left in the stack + -- non-closable value local a, b = pcall(T.makeCfunc[[ pushint 32 From a7b8b27dd39f45b9464ffc4226b0616c3ffe5ad7 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 9 Mar 2021 12:50:59 -0300 Subject: [PATCH 0692/1145] Uses of "likely" in macros active to all users The use of 'l_likely' in auxlib macros 'luaL_argcheck' and 'luaL_argexpected' should not be restricted to Lua's own code. For that, 'l_likely' was renamed to 'luai_likely' to be exported to external code. --- lauxlib.h | 9 +++------ luaconf.h | 18 ++++++++++++------ 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/lauxlib.h b/lauxlib.h index 9058e2621d..72f70e7d9d 100644 --- a/lauxlib.h +++ b/lauxlib.h @@ -12,6 +12,7 @@ #include #include +#include "luaconf.h" #include "lua.h" @@ -122,10 +123,6 @@ LUALIB_API void (luaL_requiref) (lua_State *L, const char *modname, ** =============================================================== */ -#if !defined(l_likely) -#define l_likely(x) x -#endif - #define luaL_newlibtable(L,l) \ lua_createtable(L, 0, sizeof(l)/sizeof((l)[0]) - 1) @@ -134,10 +131,10 @@ LUALIB_API void (luaL_requiref) (lua_State *L, const char *modname, (luaL_checkversion(L), luaL_newlibtable(L,l), luaL_setfuncs(L,l,0)) #define luaL_argcheck(L, cond,arg,extramsg) \ - ((void)(l_likely(cond) || luaL_argerror(L, (arg), (extramsg)))) + ((void)(luai_likely(cond) || luaL_argerror(L, (arg), (extramsg)))) #define luaL_argexpected(L,cond,arg,tname) \ - ((void)(l_likely(cond) || luaL_typeerror(L, (arg), (tname)))) + ((void)(luai_likely(cond) || luaL_typeerror(L, (arg), (tname)))) #define luaL_checkstring(L,n) (luaL_checklstring(L, (n), NULL)) #define luaL_optstring(L,n,d) (luaL_optlstring(L, (n), (d), NULL)) diff --git a/luaconf.h b/luaconf.h index ae73e2fd66..38e14eda6b 100644 --- a/luaconf.h +++ b/luaconf.h @@ -665,20 +665,26 @@ ** macros to improve jump prediction, used mostly for error handling ** and debug facilities. */ -#if (defined(LUA_CORE) || defined(LUA_LIB)) && !defined(l_likely) +#if !defined(luai_likely) -#include #if defined(__GNUC__) -#define l_likely(x) (__builtin_expect(((x) != 0), 1)) -#define l_unlikely(x) (__builtin_expect(((x) != 0), 0)) +#define luai_likely(x) (__builtin_expect(((x) != 0), 1)) +#define luai_unlikely(x) (__builtin_expect(((x) != 0), 0)) #else -#define l_likely(x) (x) -#define l_unlikely(x) (x) +#define luai_likely(x) (x) +#define luai_unlikely(x) (x) #endif #endif +#if defined(LUA_CORE) || defined(LUA_LIB) +/* shorter names for Lua's own use */ +#define l_likely(x) luai_likely(x) +#define l_unlikely(x) luai_unlikely(x) +#endif + + /* }================================================================== */ From 81c6021fb40a254d9a586b0cb53453bba8973d80 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 10 Mar 2021 10:27:19 -0300 Subject: [PATCH 0693/1145] New implementation for 'tbclist' - Fixes a bug, by removing dummy nodes together with the node itself. (The previous implementation could leave dummy nodes in frames which otherwise had no tbc variables, and therefore would not close variables; that could leave 'tbclist' pointing higher than 'top', which could dangle if the stack shrank.) - Computes MAXDELTA based on the type of delta, to ease changing its type if needed. - Instead of 'isdummy', uses 'delta==0' to signal dummy nodes. (Dummy nodes always have MAXDELTA for their real delta.) --- lfunc.c | 40 +++++++++++++++++++++++++++++----------- lobject.h | 5 +++-- 2 files changed, 32 insertions(+), 13 deletions(-) diff --git a/lfunc.c b/lfunc.c index b4c04bd0c3..f5889a21d1 100644 --- a/lfunc.c +++ b/lfunc.c @@ -154,6 +154,15 @@ static void prepcallclosemth (lua_State *L, StkId level, int status, int yy) { } +/* +** Maximum value for deltas in 'tbclist', dependent on the type +** of delta. (This macro assumes that an 'L' is in scope where it +** is used.) +*/ +#define MAXDELTA \ + ((256ul << ((sizeof(L->stack->tbclist.delta) - 1) * 8)) - 1) + + /* ** Insert a variable in the list of to-be-closed variables. */ @@ -162,13 +171,11 @@ void luaF_newtbcupval (lua_State *L, StkId level) { if (l_isfalse(s2v(level))) return; /* false doesn't need to be closed */ checkclosemth(L, level); /* value must have a close method */ - while (level - L->tbclist > USHRT_MAX) { /* is delta too large? */ - L->tbclist += USHRT_MAX; /* create a dummy node at maximum delta */ - L->tbclist->tbclist.delta = USHRT_MAX; - L->tbclist->tbclist.isdummy = 1; + while (cast_uint(level - L->tbclist) > MAXDELTA) { + L->tbclist += MAXDELTA; /* create a dummy node at maximum delta */ + L->tbclist->tbclist.delta = 0; } - level->tbclist.delta = level - L->tbclist; - level->tbclist.isdummy = 0; + level->tbclist.delta = cast(unsigned short, level - L->tbclist); L->tbclist = level; } @@ -201,6 +208,19 @@ void luaF_closeupval (lua_State *L, StkId level) { } +/* +** Remove firt element from the tbclist plus its dummy nodes. +*/ +static void poptbclist (lua_State *L) { + StkId tbc = L->tbclist; + lua_assert(tbc->tbclist.delta > 0); /* first element cannot be dummy */ + tbc -= tbc->tbclist.delta; + while (tbc > L->stack && tbc->tbclist.delta == 0) + tbc -= MAXDELTA; /* remove dummy nodes */ + L->tbclist = tbc; +} + + /* ** Close all upvalues and to-be-closed variables up to the given stack ** level. @@ -210,11 +230,9 @@ void luaF_close (lua_State *L, StkId level, int status, int yy) { luaF_closeupval(L, level); /* first, close the upvalues */ while (L->tbclist >= level) { /* traverse tbc's down to that level */ StkId tbc = L->tbclist; /* get variable index */ - L->tbclist -= tbc->tbclist.delta; /* remove it from list */ - if (!tbc->tbclist.isdummy) { /* not a dummy entry? */ - prepcallclosemth(L, tbc, status, yy); /* close variable */ - level = restorestack(L, levelrel); - } + poptbclist(L); /* remove it from list */ + prepcallclosemth(L, tbc, status, yy); /* close variable */ + level = restorestack(L, levelrel); } } diff --git a/lobject.h b/lobject.h index 1a7a737250..950bebbde2 100644 --- a/lobject.h +++ b/lobject.h @@ -139,13 +139,14 @@ typedef struct TValue { ** Entries in a Lua stack. Field 'tbclist' forms a list of all ** to-be-closed variables active in this stack. Dummy entries are ** used when the distance between two tbc variables does not fit -** in an unsigned short. +** in an unsigned short. They are represented by delta==0, and +** their real delta is always the maximum value that fits in +** that field. */ typedef union StackValue { TValue val; struct { TValuefields; - lu_byte isdummy; unsigned short delta; } tbclist; } StackValue; From 05b13651f96117674ba30dace03620658760acbd Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 10 Mar 2021 10:35:57 -0300 Subject: [PATCH 0694/1145] File 'tracegc.lua' added to 'packtests' --- testes/packtests | 1 + 1 file changed, 1 insertion(+) diff --git a/testes/packtests b/testes/packtests index c19b74bf3f..0dbb92fe5d 100755 --- a/testes/packtests +++ b/testes/packtests @@ -33,6 +33,7 @@ $NAME/pm.lua \ $NAME/sort.lua \ $NAME/strings.lua \ $NAME/tpack.lua \ +$NAME/tracegc.lua \ $NAME/utf8.lua \ $NAME/vararg.lua \ $NAME/verybig.lua \ From 014daf43cb3bf1ceb6af102c9294ec04abf9a6b6 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 12 Mar 2021 11:29:34 -0300 Subject: [PATCH 0695/1145] Details Comments and order of hashing macros in 'ltable.c'. --- ltable.c | 61 +++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 40 insertions(+), 21 deletions(-) diff --git a/ltable.c b/ltable.c index b520cdf4ba..33c1ab302e 100644 --- a/ltable.c +++ b/ltable.c @@ -68,20 +68,25 @@ #define MAXHSIZE luaM_limitN(1u << MAXHBITS, Node) +/* +** When the original hash value is good, hashing by a power of 2 +** avoids the cost of '%'. +*/ #define hashpow2(t,n) (gnode(t, lmod((n), sizenode(t)))) -#define hashstr(t,str) hashpow2(t, (str)->hash) -#define hashboolean(t,p) hashpow2(t, p) -#define hashint(t,i) hashpow2(t, i) - - /* -** for some types, it is better to avoid modulus by power of 2, as -** they tend to have many 2 factors. +** for other types, it is better to avoid modulo by power of 2, as +** they can have many 2 factors. */ #define hashmod(t,n) (gnode(t, ((n) % ((sizenode(t)-1)|1)))) +#define hashstr(t,str) hashpow2(t, (str)->hash) +#define hashboolean(t,p) hashpow2(t, p) + +#define hashint(t,i) hashpow2(t, i) + + #define hashpointer(t,p) hashmod(t, point2uint(p)) @@ -135,24 +140,38 @@ static int l_hashfloat (lua_Number n) { */ static Node *mainposition (const Table *t, int ktt, const Value *kvl) { switch (withvariant(ktt)) { - case LUA_VNUMINT: - return hashint(t, ivalueraw(*kvl)); - case LUA_VNUMFLT: - return hashmod(t, l_hashfloat(fltvalueraw(*kvl))); - case LUA_VSHRSTR: - return hashstr(t, tsvalueraw(*kvl)); - case LUA_VLNGSTR: - return hashpow2(t, luaS_hashlongstr(tsvalueraw(*kvl))); + case LUA_VNUMINT: { + lua_Integer key = ivalueraw(*kvl); + return hashint(t, key); + } + case LUA_VNUMFLT: { + lua_Number n = fltvalueraw(*kvl); + return hashmod(t, l_hashfloat(n)); + } + case LUA_VSHRSTR: { + TString *ts = tsvalueraw(*kvl); + return hashstr(t, ts); + } + case LUA_VLNGSTR: { + TString *ts = tsvalueraw(*kvl); + return hashpow2(t, luaS_hashlongstr(ts)); + } case LUA_VFALSE: return hashboolean(t, 0); case LUA_VTRUE: return hashboolean(t, 1); - case LUA_VLIGHTUSERDATA: - return hashpointer(t, pvalueraw(*kvl)); - case LUA_VLCF: - return hashpointer(t, fvalueraw(*kvl)); - default: - return hashpointer(t, gcvalueraw(*kvl)); + case LUA_VLIGHTUSERDATA: { + void *p = pvalueraw(*kvl); + return hashpointer(t, p); + } + case LUA_VLCF: { + lua_CFunction f = fvalueraw(*kvl); + return hashpointer(t, f); + } + default: { + GCObject *o = gcvalueraw(*kvl); + return hashpointer(t, o); + } } } From eadd8c7178c79c814ecca9652973a9b9dd4cc71b Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 12 Mar 2021 15:03:33 -0300 Subject: [PATCH 0696/1145] Added option LUA_NOBUILTIN This option allows external code to avoid the use of gcc builtin macro '__builtin_expect' in the Lua API. --- luaconf.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/luaconf.h b/luaconf.h index 38e14eda6b..e64d2ee392 100644 --- a/luaconf.h +++ b/luaconf.h @@ -663,11 +663,13 @@ /* ** macros to improve jump prediction, used mostly for error handling -** and debug facilities. +** and debug facilities. (Some macros in the Lua API use these macros. +** Define LUA_NOBUILTIN if you do not want '__builtin_expect' in your +** code.) */ #if !defined(luai_likely) -#if defined(__GNUC__) +#if defined(__GNUC__) && !defined(LUA_NOBUILTIN) #define luai_likely(x) (__builtin_expect(((x) != 0), 1)) #define luai_unlikely(x) (__builtin_expect(((x) != 0), 0)) #else From ba81adaad9a72530d1ac81149a1fdd154b010b06 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 29 Mar 2021 11:26:07 -0300 Subject: [PATCH 0697/1145] Next release number (5.4.4) --- lua.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lua.h b/lua.h index 820535b948..c3dbce1e5f 100644 --- a/lua.h +++ b/lua.h @@ -18,10 +18,10 @@ #define LUA_VERSION_MAJOR "5" #define LUA_VERSION_MINOR "4" -#define LUA_VERSION_RELEASE "3" +#define LUA_VERSION_RELEASE "4" #define LUA_VERSION_NUM 504 -#define LUA_VERSION_RELEASE_NUM (LUA_VERSION_NUM * 100 + 0) +#define LUA_VERSION_RELEASE_NUM (LUA_VERSION_NUM * 100 + 4) #define LUA_VERSION "Lua " LUA_VERSION_MAJOR "." LUA_VERSION_MINOR #define LUA_RELEASE LUA_VERSION "." LUA_VERSION_RELEASE From bef250eb8d44ba58fa04f82df7550a79b068d2d0 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 29 Mar 2021 11:47:12 -0300 Subject: [PATCH 0698/1145] Details Comments and small improvements in the manual. --- lobject.h | 2 +- lopcodes.h | 21 +++++++++++++++++---- lstate.c | 2 +- ltablib.c | 2 +- manual/manual.of | 21 ++++++++++++--------- 5 files changed, 32 insertions(+), 16 deletions(-) diff --git a/lobject.h b/lobject.h index 950bebbde2..a1b455436d 100644 --- a/lobject.h +++ b/lobject.h @@ -112,7 +112,7 @@ typedef struct TValue { #define settt_(o,t) ((o)->tt_=(t)) -/* main macro to copy values (from 'obj1' to 'obj2') */ +/* main macro to copy values (from 'obj2' to 'obj1') */ #define setobj(L,obj1,obj2) \ { TValue *io1=(obj1); const TValue *io2=(obj2); \ io1->value_ = io2->value_; settt_(io1, io2->tt_); \ diff --git a/lopcodes.h b/lopcodes.h index d6a47e5af9..7c27451596 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -190,7 +190,8 @@ enum OpMode {iABC, iABx, iAsBx, iAx, isJ}; /* basic instruction formats */ /* -** grep "ORDER OP" if you change these enums +** Grep "ORDER OP" if you change these enums. Opcodes marked with a (*) +** has extra descriptions in the notes after the enumeration. */ typedef enum { @@ -203,7 +204,7 @@ OP_LOADF,/* A sBx R[A] := (lua_Number)sBx */ OP_LOADK,/* A Bx R[A] := K[Bx] */ OP_LOADKX,/* A R[A] := K[extra arg] */ OP_LOADFALSE,/* A R[A] := false */ -OP_LFALSESKIP,/*A R[A] := false; pc++ */ +OP_LFALSESKIP,/*A R[A] := false; pc++ (*) */ OP_LOADTRUE,/* A R[A] := true */ OP_LOADNIL,/* A B R[A], R[A+1], ..., R[A+B] := nil */ OP_GETUPVAL,/* A B R[A] := UpValue[B] */ @@ -254,7 +255,7 @@ OP_BXOR,/* A B C R[A] := R[B] ~ R[C] */ OP_SHL,/* A B C R[A] := R[B] << R[C] */ OP_SHR,/* A B C R[A] := R[B] >> R[C] */ -OP_MMBIN,/* A B C call C metamethod over R[A] and R[B] */ +OP_MMBIN,/* A B C call C metamethod over R[A] and R[B] (*) */ OP_MMBINI,/* A sB C k call C metamethod over R[A] and sB */ OP_MMBINK,/* A B C k call C metamethod over R[A] and K[B] */ @@ -280,7 +281,7 @@ OP_GTI,/* A sB k if ((R[A] > sB) ~= k) then pc++ */ OP_GEI,/* A sB k if ((R[A] >= sB) ~= k) then pc++ */ OP_TEST,/* A k if (not R[A] == k) then pc++ */ -OP_TESTSET,/* A B k if (not R[B] == k) then pc++ else R[A] := R[B] */ +OP_TESTSET,/* A B k if (not R[B] == k) then pc++ else R[A] := R[B] (*) */ OP_CALL,/* A B C R[A], ... ,R[A+C-2] := R[A](R[A+1], ... ,R[A+B-1]) */ OP_TAILCALL,/* A B C k return R[A](R[A+1], ... ,R[A+B-1]) */ @@ -315,6 +316,18 @@ OP_EXTRAARG/* Ax extra (larger) argument for previous opcode */ /*=========================================================================== Notes: + + (*) Opcode OP_LFALSESKIP is used to convert a condition to a boolean + value, in a code equivalent to (not cond ? false : true). (It + produces false and skips the next instruction producing true.) + + (*) Opcodes OP_MMBIN and variants follow each arithmetic and + bitwise opcode. If the operation succeeds, it skips this next + opcode. Otherwise, this opcode calls the corresponding metamethod. + + (*) Opcode OP_TESTSET is used in short-circuit expressions that need + both to jump and to produce a value, such as (a = b or c). + (*) In OP_CALL, if (B == 0) then B = top - A. If (C == 0), then 'top' is set to last_result+1, so next open instruction (OP_CALL, OP_RETURN*, OP_SETLIST) may use 'top'. diff --git a/lstate.c b/lstate.c index c5e3b437f1..bfc590262b 100644 --- a/lstate.c +++ b/lstate.c @@ -269,7 +269,7 @@ static void preinit_thread (lua_State *L, global_State *g) { static void close_state (lua_State *L) { global_State *g = G(L); if (!completestate(g)) /* closing a partially built state? */ - luaC_freeallobjects(L); /* jucst collect its objects */ + luaC_freeallobjects(L); /* just collect its objects */ else { /* closing a fully built state */ luaD_closeprotected(L, 1, LUA_OK); /* close all upvalues */ luaC_freeallobjects(L); /* collect all objects */ diff --git a/ltablib.c b/ltablib.c index d80eb80154..dbfe250925 100644 --- a/ltablib.c +++ b/ltablib.c @@ -147,7 +147,7 @@ static void addfield (lua_State *L, luaL_Buffer *b, lua_Integer i) { lua_geti(L, 1, i); if (l_unlikely(!lua_isstring(L, -1))) luaL_error(L, "invalid value (%s) at index %I in table for 'concat'", - luaL_typename(L, -1), i); + luaL_typename(L, -1), (LUAI_UACINT)i); luaL_addvalue(b); } diff --git a/manual/manual.of b/manual/manual.of index c69970d2ad..2a837b5e21 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -5915,6 +5915,9 @@ previously pushed on the stack on top of the library table. These values are popped from the stack after the registration. +A function with a @id{NULL} value represents a placeholder, +which is filled with @false. + } @APIEntry{void luaL_setmetatable (lua_State *L, const char *tname);| @@ -6397,7 +6400,7 @@ This means that any error @N{inside @T{f}} is not propagated; instead, @id{pcall} catches the error and returns a status code. Its first result is the status code (a boolean), -which is true if the call succeeds without errors. +which is @true if the call succeeds without errors. In such case, @id{pcall} also returns all results from the call, after this first result. In case of any error, @id{pcall} returns @false plus the error object. @@ -6603,7 +6606,7 @@ an object with type @T{"thread"}. @LibEntry{coroutine.isyieldable ([co])| -Returns true when the coroutine @id{co} can yield. +Returns @true when the coroutine @id{co} can yield. The default for @id{co} is the running coroutine. A coroutine is yieldable if it is not the main thread and @@ -6635,7 +6638,7 @@ If there is any error, @LibEntry{coroutine.running ()| Returns the running coroutine plus a boolean, -true when the running coroutine is the main one. +@true when the running coroutine is the main one. } @@ -6730,7 +6733,7 @@ If the loader returns any non-nil value, @id{require} assigns the returned value to @T{package.loaded[modname]}. If the loader does not return a non-nil value and has not assigned any value to @T{package.loaded[modname]}, -then @id{require} assigns @Rw{true} to this entry. +then @id{require} assigns @true to this entry. In any case, @id{require} returns the final value of @T{package.loaded[modname]}. Besides that value, @id{require} also returns as a second result @@ -7051,7 +7054,7 @@ otherwise, it returns @fail. A third, optional numeric argument @id{init} specifies where to start the search; its default value @N{is 1} and can be negative. -A value of @true as a fourth, optional argument @id{plain} +A @true as a fourth, optional argument @id{plain} turns off the pattern matching facilities, so the function does a plain @Q{find substring} operation, with no characters in @id{pattern} being considered magic. @@ -8077,7 +8080,7 @@ or @fail if @id{x} is not a number. @LibEntry{math.ult (m, n)| Returns a boolean, -true if and only if integer @id{m} is below integer @id{n} when +@true if and only if integer @id{m} is below integer @id{n} when they are compared as @x{unsigned integers}. } @@ -8490,13 +8493,13 @@ When called without a @id{command}, @LibEntry{os.exit ([code [, close]])| Calls the @ANSI{exit} to terminate the host program. -If @id{code} is @Rw{true}, +If @id{code} is @true, the returned status is @idx{EXIT_SUCCESS}; -if @id{code} is @Rw{false}, +if @id{code} is @false, the returned status is @idx{EXIT_FAILURE}; if @id{code} is a number, the returned status is this number. -The default value for @id{code} is @Rw{true}. +The default value for @id{code} is @true. If the optional second argument @id{close} is true, closes the Lua state before exiting. From bf10593a3a912cd3cac69569c7474e687c0d0cd8 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 29 Mar 2021 12:57:32 -0300 Subject: [PATCH 0699/1145] Allow yields inside '__pairs' --- lbaselib.c | 7 ++++++- testes/nextvar.lua | 21 +++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/lbaselib.c b/lbaselib.c index 83ad306d9c..fd6687e64e 100644 --- a/lbaselib.c +++ b/lbaselib.c @@ -261,6 +261,11 @@ static int luaB_next (lua_State *L) { } +static int pairscont (lua_State *L, int status, lua_KContext k) { + (void)L; (void)status; (void)k; /* unused */ + return 3; +} + static int luaB_pairs (lua_State *L) { luaL_checkany(L, 1); if (luaL_getmetafield(L, 1, "__pairs") == LUA_TNIL) { /* no metamethod? */ @@ -270,7 +275,7 @@ static int luaB_pairs (lua_State *L) { } else { lua_pushvalue(L, 1); /* argument 'self' to metamethod */ - lua_call(L, 1, 3); /* get 3 values from metamethod */ + lua_callk(L, 1, 3, 0, pairscont); /* get 3 values from metamethod */ } return 3; } diff --git a/testes/nextvar.lua b/testes/nextvar.lua index 29cb05d573..076f6361bc 100644 --- a/testes/nextvar.lua +++ b/testes/nextvar.lua @@ -764,4 +764,25 @@ for k,v in ipairs(a) do end assert(i == a.n) + +-- testing yield inside __pairs +do + local t = setmetatable({10, 20, 30}, {__pairs = function (t) + local inc = coroutine.yield() + return function (t, i) + if i > 1 then return i - inc, t[i - inc] else return nil end + end, t, #t + 1 + end}) + + local res = {} + local co = coroutine.wrap(function () + for i,p in pairs(t) do res[#res + 1] = p end + end) + + co() -- start coroutine + co(1) -- continue after yield + assert(res[1] == 30 and res[2] == 20 and res[3] == 10 and #res == 3) + +end + print"OK" From 7fbe2158089898f09d741e991737f282e514ffaa Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 29 Mar 2021 15:47:18 -0300 Subject: [PATCH 0700/1145] New hash function for integer keys When integer keys do not form a sequence, it is better to use all their bits to compute their hashes. (The previous implementation was quite bad for integer keys with common lower bits, and disastrous for integer keys changing only in their upper 32 bits.) --- ltable.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/ltable.c b/ltable.c index 33c1ab302e..af8783688b 100644 --- a/ltable.c +++ b/ltable.c @@ -84,8 +84,6 @@ #define hashstr(t,str) hashpow2(t, (str)->hash) #define hashboolean(t,p) hashpow2(t, p) -#define hashint(t,i) hashpow2(t, i) - #define hashpointer(t,p) hashmod(t, point2uint(p)) @@ -101,6 +99,20 @@ static const Node dummynode_ = { static const TValue absentkey = {ABSTKEYCONSTANT}; +/* +** Hash for integers. To allow a good hash, use the remainder operator +** ('%'). If integer fits as a non-negative int, compute an int +** remainder, which is faster. Otherwise, use an unsigned-integer +** remainder, which uses all bits and ensures a non-negative result. +*/ +static Node *hashint (const Table *t, lua_Integer i) { + lua_Unsigned ui = l_castS2U(i); + if (ui <= (unsigned int)INT_MAX) + return hashmod(t, cast_int(ui)); + else + return hashmod(t, ui); +} + /* ** Hash for floating-point numbers. From 36de01d9885562444ae2e2a3e0b7e01b3fb8743b Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 30 Mar 2021 14:49:18 -0300 Subject: [PATCH 0701/1145] Changes in cache for function constants In 'lcode.c', when adding constants to the list of constants of a function, integers represent themselves in the cache and floats with integral values get a small delta to avoid collision with integers. (This change avoids creating artificial addresses; the old implementation converted integers to pointers to index the cache.) --- lcode.c | 34 ++++++++++++++++++++++++++-------- testes/code.lua | 14 ++++++++++++++ 2 files changed, 40 insertions(+), 8 deletions(-) diff --git a/lcode.c b/lcode.c index 80d975cb85..9cba24f9c1 100644 --- a/lcode.c +++ b/lcode.c @@ -10,6 +10,7 @@ #include "lprefix.h" +#include #include #include #include @@ -580,24 +581,41 @@ static int stringK (FuncState *fs, TString *s) { /* ** Add an integer to list of constants and return its index. -** Integers use userdata as keys to avoid collision with floats with -** same value; conversion to 'void*' is used only for hashing, so there -** are no "precision" problems. */ static int luaK_intK (FuncState *fs, lua_Integer n) { - TValue k, o; - setpvalue(&k, cast_voidp(cast_sizet(n))); + TValue o; setivalue(&o, n); - return addk(fs, &k, &o); + return addk(fs, &o, &o); /* use integer itself as key */ } /* -** Add a float to list of constants and return its index. +** Add a float to list of constants and return its index. Floats +** with integral values need a different key, to avoid collision +** with actual integers. To that, we add to the number its smaller +** power-of-two fraction that is still significant in its scale. +** For doubles, that would be 1/2^52. +** (This method is not bulletproof: there may be another float +** with that value, and for floats larger than 2^53 the result is +** still an integer. At worst, this only wastes an entry with +** a duplicate.) */ static int luaK_numberK (FuncState *fs, lua_Number r) { TValue o; + lua_Integer ik; setfltvalue(&o, r); - return addk(fs, &o, &o); /* use number itself as key */ + if (!luaV_flttointeger(r, &ik, F2Ieq)) /* not an integral value? */ + return addk(fs, &o, &o); /* use number itself as key */ + else { /* must build an alternative key */ + const int nbm = l_floatatt(MANT_DIG); + const lua_Number q = l_mathop(ldexp)(1.0, -nbm + 1); + const lua_Number k = (ik == 0) ? q : r + r*q; /* new key */ + TValue kv; + setfltvalue(&kv, k); + /* result is not an integral value, unless value is too large */ + lua_assert(!luaV_flttointeger(k, &ik, F2Ieq) || + l_mathop(fabs)(r) >= l_mathop(1e6)); + return addk(fs, &kv, &o); + } } diff --git a/testes/code.lua b/testes/code.lua index 4e00309f7f..543743fc07 100644 --- a/testes/code.lua +++ b/testes/code.lua @@ -69,6 +69,20 @@ foo = function (f, a) checkKlist(foo, {100000, 100000.0, -100000, -100000.0}) +-- floats x integers +foo = function (t, a) + t[a] = 1; t[a] = 1.0 + t[a] = 1; t[a] = 1.0 + t[a] = 2; t[a] = 2.0 + t[a] = 0; t[a] = 0.0 + t[a] = 1; t[a] = 1.0 + t[a] = 2; t[a] = 2.0 + t[a] = 0; t[a] = 0.0 +end + +checkKlist(foo, {1, 1.0, 2, 2.0, 0, 0.0}) + + -- testing opcodes -- check that 'f' opcodes match '...' From 47cffdc723c2e0c6dfaf62b7775ca1c1d338c0a4 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 7 Apr 2021 14:59:26 -0300 Subject: [PATCH 0702/1145] Bug: tbc variables in "for" loops don't avoid tail calls --- lparser.c | 21 +++++++++++++++------ testes/locals.lua | 23 +++++++++++++++++++++++ 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/lparser.c b/lparser.c index 284ef1f0c4..df9473c274 100644 --- a/lparser.c +++ b/lparser.c @@ -416,6 +416,17 @@ static void markupval (FuncState *fs, int level) { } +/* +** Mark that current block has a to-be-closed variable. +*/ +static void marktobeclosed (FuncState *fs) { + BlockCnt *bl = fs->bl; + bl->upval = 1; + bl->insidetbc = 1; + fs->needclose = 1; +} + + /* ** Find a variable with the given name 'n'. If it is an upvalue, add ** this upvalue into all intermediate functions. If it is a global, set @@ -1599,7 +1610,7 @@ static void forlist (LexState *ls, TString *indexname) { line = ls->linenumber; adjust_assign(ls, 4, explist(ls, &e), &e); adjustlocalvars(ls, 4); /* control variables */ - markupval(fs, fs->nactvar); /* last control var. must be closed */ + marktobeclosed(fs); /* last control var. must be closed */ luaK_checkstack(fs, 3); /* extra space to call generator */ forbody(ls, base, line, nvars - 4, 1); } @@ -1703,11 +1714,9 @@ static int getlocalattribute (LexState *ls) { } -static void checktoclose (LexState *ls, int level) { +static void checktoclose (FuncState *fs, int level) { if (level != -1) { /* is there a to-be-closed variable? */ - FuncState *fs = ls->fs; - markupval(fs, level + 1); - fs->bl->insidetbc = 1; /* in the scope of a to-be-closed variable */ + marktobeclosed(fs); luaK_codeABC(fs, OP_TBC, reglevel(fs, level), 0, 0); } } @@ -1751,7 +1760,7 @@ static void localstat (LexState *ls) { adjust_assign(ls, nvars, nexps, &e); adjustlocalvars(ls, nvars); } - checktoclose(ls, toclose); + checktoclose(fs, toclose); } diff --git a/testes/locals.lua b/testes/locals.lua index 2c67edbdb3..6aad5d253b 100644 --- a/testes/locals.lua +++ b/testes/locals.lua @@ -335,6 +335,29 @@ do end +do + -- bug in 5.4.3: previous condition (calls cannot be tail in the + -- scope of to-be-closed variables) must be valid for tbc variables + -- created by 'for' loops. + + local closed = false + + local function foo () + return function () return true end, 0, 0, + func2close(function () closed = true end) + end + + local function tail() return closed end + + local function foo1 () + for k in foo() do return tail() end + end + + assert(foo1() == false) + assert(closed == true) +end + + do print("testing errors in __close") -- original error is in __close From d205f3a4847bc8b835fda91f51ba1cf45b796baf Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Sat, 10 Apr 2021 10:19:21 -0300 Subject: [PATCH 0703/1145] Bug: Lua source should not use C99 comments ("//") --- lvm.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lvm.c b/lvm.c index c9729bcca0..16e01d6839 100644 --- a/lvm.c +++ b/lvm.c @@ -1156,8 +1156,10 @@ void luaV_execute (lua_State *L, CallInfo *ci) { Instruction i; /* instruction being executed */ StkId ra; /* instruction's A register */ vmfetch(); -// low-level line tracing for debugging Lua -// printf("line: %d\n", luaG_getfuncline(cl->p, pcRel(pc, cl->p))); + #if 0 + /* low-level line tracing for debugging Lua */ + printf("line: %d\n", luaG_getfuncline(cl->p, pcRel(pc, cl->p))); + #endif lua_assert(base == ci->func + 1); lua_assert(base <= L->top && L->top < L->stack_last); /* invalidate top for instructions not expecting it */ From 5148954eed7550dcac587fce0214b470442efa0d Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 12 Apr 2021 16:50:34 -0300 Subject: [PATCH 0704/1145] Align error messages for calling non-callable values 'pcall(foo)' message was "attempt to call a table value", while 'pcall(function () foo() end) message was "global 'foo' is not callable". --- ldebug.c | 48 +++++++++++++++++++++++++++++++++++++----------- 1 file changed, 37 insertions(+), 11 deletions(-) diff --git a/ldebug.c b/ldebug.c index 1feaab229f..433a875942 100644 --- a/ldebug.c +++ b/ldebug.c @@ -675,9 +675,21 @@ static const char *getupvalname (CallInfo *ci, const TValue *o, } +static const char *formatvarinfo (lua_State *L, const char *kind, + const char *name) { + if (kind == NULL) + return ""; /* no information */ + else + return luaO_pushfstring(L, " (%s '%s')", kind, name); +} + +/* +** Build a string with a "description" for the value 'o', such as +** "variable 'x'" or "upvalue 'y'". +*/ static const char *varinfo (lua_State *L, const TValue *o) { - const char *name = NULL; /* to avoid warnings */ CallInfo *ci = L->ci; + const char *name = NULL; /* to avoid warnings */ const char *kind = NULL; if (isLua(ci)) { kind = getupvalname(ci, o, &name); /* check whether 'o' is an upvalue */ @@ -685,26 +697,40 @@ static const char *varinfo (lua_State *L, const TValue *o) { kind = getobjname(ci_func(ci)->p, currentpc(ci), cast_int(cast(StkId, o) - (ci->func + 1)), &name); } - return (kind) ? luaO_pushfstring(L, " (%s '%s')", kind, name) : ""; + return formatvarinfo(L, kind, name); } -l_noret luaG_typeerror (lua_State *L, const TValue *o, const char *op) { +/* +** Raise a type error +*/ +static l_noret typeerror (lua_State *L, const TValue *o, const char *op, + const char *extra) { const char *t = luaT_objtypename(L, o); - luaG_runerror(L, "attempt to %s a %s value%s", op, t, varinfo(L, o)); + luaG_runerror(L, "attempt to %s a %s value%s", op, t, extra); } +/* +** Raise a type error with "standard" information about the faulty +** object 'o' (using 'varinfo'). +*/ +l_noret luaG_typeerror (lua_State *L, const TValue *o, const char *op) { + typeerror(L, o, op, varinfo(L, o)); +} + + +/* +** Raise an error for calling a non-callable object. Try to find +** a name for the object based on the code that made the call +** ('funcnamefromcode'); if it cannot get a name there, try 'varinfo'. +*/ l_noret luaG_callerror (lua_State *L, const TValue *o) { CallInfo *ci = L->ci; const char *name = NULL; /* to avoid warnings */ - const char *what = (isLua(ci)) ? funcnamefromcode(L, ci, &name) : NULL; - if (what != NULL) { - const char *t = luaT_objtypename(L, o); - luaG_runerror(L, "%s '%s' is not callable (a %s value)", what, name, t); - } - else - luaG_typeerror(L, o, "call"); + const char *kind = (isLua(ci)) ? funcnamefromcode(L, ci, &name) : NULL; + const char *extra = kind ? formatvarinfo(L, kind, name) : varinfo(L, o); + typeerror(L, o, "call", extra); } From 681297187ec45268e872b26753c441586c12bdd8 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 16 Apr 2021 15:41:44 -0300 Subject: [PATCH 0705/1145] Bug: yielding in '__close' mess up number of returns Yielding in a __close metamethod called when returning vararg results changes the top and so messes up the number of returned values. --- lstate.h | 2 +- lvm.c | 12 +++++++++- testes/locals.lua | 59 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 2 deletions(-) diff --git a/lstate.h b/lstate.h index c1283bb6b9..44cf939cb1 100644 --- a/lstate.h +++ b/lstate.h @@ -165,7 +165,7 @@ typedef struct stringtable { ** - field 'nyield' is used only while a function is "doing" an ** yield (from the yield until the next resume); ** - field 'nres' is used only while closing tbc variables when -** returning from a C function; +** returning from a function; ** - field 'transferinfo' is used only during call/returnhooks, ** before the function starts or after it ends. */ diff --git a/lvm.c b/lvm.c index 16e01d6839..e4b1903e70 100644 --- a/lvm.c +++ b/lvm.c @@ -847,10 +847,19 @@ void luaV_finishOp (lua_State *L) { luaV_concat(L, total); /* concat them (may yield again) */ break; } - case OP_CLOSE: case OP_RETURN: { /* yielded closing variables */ + case OP_CLOSE: { /* yielded closing variables */ ci->u.l.savedpc--; /* repeat instruction to close other vars. */ break; } + case OP_RETURN: { /* yielded closing variables */ + StkId ra = base + GETARG_A(inst); + /* adjust top to signal correct number of returns, in case the + return is "up to top" ('isIT') */ + L->top = ra + ci->u2.nres; + /* repeat instruction to close other vars. and complete the return */ + ci->u.l.savedpc--; + break; + } default: { /* only these other opcodes can yield */ lua_assert(op == OP_TFORCALL || op == OP_CALL || @@ -1672,6 +1681,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { n = cast_int(L->top - ra); /* get what is available */ savepc(ci); if (TESTARG_k(i)) { /* may there be open upvalues? */ + ci->u2.nres = n; /* save number of returns */ if (L->top < ci->top) L->top = ci->top; luaF_close(L, base, CLOSEKTOP, 1); diff --git a/testes/locals.lua b/testes/locals.lua index 6aad5d253b..6151f64d0c 100644 --- a/testes/locals.lua +++ b/testes/locals.lua @@ -813,6 +813,65 @@ do end +do + -- yielding inside closing metamethods while returning + -- (bug in 5.4.3) + + local extrares -- result from extra yield (if any) + + local function check (body, extra, ...) + local t = table.pack(...) -- expected returns + local co = coroutine.wrap(body) + if extra then + extrares = co() -- runs until first (extra) yield + end + local res = table.pack(co()) -- runs until yield inside '__close' + assert(res.n == 2 and res[2] == nil) + local res2 = table.pack(co()) -- runs until end of function + assert(res2.n == t.n) + for i = 1, #t do + if t[i] == "x" then + assert(res2[i] == res[1]) -- value that was closed + else + assert(res2[i] == t[i]) + end + end + end + + local function foo () + local x = func2close(coroutine.yield) + local extra = func2close(function (self) + assert(self == extrares) + coroutine.yield(100) + end) + extrares = extra + return table.unpack{10, x, 30} + end + check(foo, true, 10, "x", 30) + assert(extrares == 100) + + local function foo () + local x = func2close(coroutine.yield) + return + end + check(foo, false) + + local function foo () + local x = func2close(coroutine.yield) + local y, z = 20, 30 + return x + end + check(foo, false, "x") + + local function foo () + local x = func2close(coroutine.yield) + local extra = func2close(coroutine.yield) + return table.unpack({}, 1, 100) -- 100 nils + end + check(foo, true, table.unpack({}, 1, 100)) + +end + do -- yielding inside closing metamethods after an error From 1e6918d553e0d76148f7de0af63fdce326e049b8 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 24 May 2021 16:48:09 -0300 Subject: [PATCH 0706/1145] Details - Removed unused (and trivial) definition LUA_UNSIGNEDBITS - Alignment structure in pack/unpack moved to a narrower scope --- lstrlib.c | 17 +++++++---------- luaconf.h | 4 ---- 2 files changed, 7 insertions(+), 14 deletions(-) diff --git a/lstrlib.c b/lstrlib.c index 47e5b27a6c..74501f78a9 100644 --- a/lstrlib.c +++ b/lstrlib.c @@ -1352,15 +1352,6 @@ static const union { } nativeendian = {1}; -/* dummy structure to get native alignment requirements */ -struct cD { - char c; - union { double d; void *p; lua_Integer i; lua_Number n; } u; -}; - -#define MAXALIGN (offsetof(struct cD, u)) - - /* ** information to pack/unpack stuff */ @@ -1435,6 +1426,8 @@ static void initheader (lua_State *L, Header *h) { ** Read and classify next option. 'size' is filled with option's size. */ static KOption getoption (Header *h, const char **fmt, int *size) { + /* dummy structure to get native alignment requirements */ + struct cD { char c; union { LUAI_MAXALIGN; } u; }; int opt = *((*fmt)++); *size = 0; /* default */ switch (opt) { @@ -1465,7 +1458,11 @@ static KOption getoption (Header *h, const char **fmt, int *size) { case '<': h->islittle = 1; break; case '>': h->islittle = 0; break; case '=': h->islittle = nativeendian.little; break; - case '!': h->maxalign = getnumlimit(h, fmt, MAXALIGN); break; + case '!': { + const int maxalign = offsetof(struct cD, u); + h->maxalign = getnumlimit(h, fmt, maxalign); + break; + } default: luaL_error(h->L, "invalid format option '%c'", opt); } return Knop; diff --git a/luaconf.h b/luaconf.h index e64d2ee392..d42d14b7d5 100644 --- a/luaconf.h +++ b/luaconf.h @@ -485,7 +485,6 @@ @@ LUA_MAXINTEGER is the maximum value for a LUA_INTEGER. @@ LUA_MININTEGER is the minimum value for a LUA_INTEGER. @@ LUA_MAXUNSIGNED is the maximum value for a LUA_UNSIGNED. -@@ LUA_UNSIGNEDBITS is the number of bits in a LUA_UNSIGNED. @@ lua_integer2str converts an integer to a string. */ @@ -506,9 +505,6 @@ #define LUA_UNSIGNED unsigned LUAI_UACINT -#define LUA_UNSIGNEDBITS (sizeof(LUA_UNSIGNED) * CHAR_BIT) - - /* now the variable definitions */ #if LUA_INT_TYPE == LUA_INT_INT /* { int */ From fc6c74f1004b5f67d0503633ddb74174a3c8d6ad Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 24 May 2021 16:48:43 -0300 Subject: [PATCH 0707/1145] 'index2value' more robust 'index2value' accepts pseudo-indices also when called from a Lua function, through a hook. --- lapi.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/lapi.c b/lapi.c index f8f70cd008..3467891798 100644 --- a/lapi.c +++ b/lapi.c @@ -53,6 +53,10 @@ const char lua_ident[] = #define isupvalue(i) ((i) < LUA_REGISTRYINDEX) +/* +** Convert an acceptable index to a pointer to its respective value. +** Non-valid indices return the special nil value 'G(L)->nilvalue'. +*/ static TValue *index2value (lua_State *L, int idx) { CallInfo *ci = L->ci; if (idx > 0) { @@ -70,22 +74,26 @@ static TValue *index2value (lua_State *L, int idx) { else { /* upvalues */ idx = LUA_REGISTRYINDEX - idx; api_check(L, idx <= MAXUPVAL + 1, "upvalue index too large"); - if (ttislcf(s2v(ci->func))) /* light C function? */ - return &G(L)->nilvalue; /* it has no upvalues */ - else { + if (ttisCclosure(s2v(ci->func))) { /* C closure? */ CClosure *func = clCvalue(s2v(ci->func)); return (idx <= func->nupvalues) ? &func->upvalue[idx-1] : &G(L)->nilvalue; } + else { /* light C function or Lua function (through a hook)?) */ + api_check(L, ttislcf(s2v(ci->func)), "caller not a C function"); + return &G(L)->nilvalue; /* no upvalues */ + } } } - +/* +** Convert a valid actual index (not a pseudo-index) to its address. +*/ static StkId index2stack (lua_State *L, int idx) { CallInfo *ci = L->ci; if (idx > 0) { StkId o = ci->func + idx; - api_check(L, o < L->top, "unacceptable index"); + api_check(L, o < L->top, "invalid index"); return o; } else { /* non-positive index */ From c0ed74c1e130aa6d80e5ffc7b6e32b433aca1765 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 9 Jun 2021 13:24:49 -0300 Subject: [PATCH 0708/1145] Avoid the term "undefined behavior" in the manual --- manual/manual.of | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/manual/manual.of b/manual/manual.of index 2a837b5e21..8cf0abfcd1 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -6365,9 +6365,8 @@ The order in which the indices are enumerated is not specified, (To traverse a table in numerical order, use a numerical @Rw{for}.) -The behavior of @id{next} is undefined if, -during the traversal, -you assign any value to a non-existent field in the table. +You should not assign any value to a non-existent field in a table +during its traversal. You may however modify existing fields. In particular, you may set existing fields to nil. From 901d76009346d76996679c02deee708bf225e91e Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 11 Jun 2021 13:41:07 -0300 Subject: [PATCH 0709/1145] Simpler implementation for tail calls Tail calls handled by 'luaD_precall', like regular calls, to avoid code duplication. --- ldo.c | 48 ++++++++++++++++++++++++------------------------ ldo.h | 4 ++-- lvm.c | 20 +++++++------------- 3 files changed, 33 insertions(+), 39 deletions(-) diff --git a/ldo.c b/ldo.c index 7135079b12..a410461bdb 100644 --- a/ldo.c +++ b/ldo.c @@ -474,26 +474,16 @@ void luaD_poscall (lua_State *L, CallInfo *ci, int nres) { /* -** Prepare a function for a tail call, building its call info on top -** of the current call info. 'narg1' is the number of arguments plus 1 -** (so that it includes the function itself). +** In a tail call, move function and parameters to previous call frame. +** (This is done only when no more errors can occur before entering the +** new function, to keep debug information always consistent.) */ -void luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, int narg1) { - Proto *p = clLvalue(s2v(func))->p; - int fsize = p->maxstacksize; /* frame size */ - int nfixparams = p->numparams; +static void moveparams (lua_State *L, StkId prevf, StkId func, int narg) { int i; - for (i = 0; i < narg1; i++) /* move down function and arguments */ - setobjs2s(L, ci->func + i, func + i); - checkstackGC(L, fsize); - func = ci->func; /* moved-down function */ - for (; narg1 <= nfixparams; narg1++) - setnilvalue(s2v(func + narg1)); /* complete missing arguments */ - ci->top = func + 1 + fsize; /* top for new function */ - lua_assert(ci->top <= L->stack_last); - ci->u.l.savedpc = p->code; /* starting point */ - ci->callstatus |= CIST_TAIL; - L->top = func + narg1; /* set top */ + narg++; /* function itself will be moved, too */ + for (i = 0; i < narg; i++) /* move down function and arguments */ + setobjs2s(L, prevf + i, func + i); + L->top = prevf + narg; /* correct top */ } @@ -504,8 +494,12 @@ void luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, int narg1) { ** to be executed, if it was a Lua function. Otherwise (a C function) ** returns NULL, with all the results on the stack, starting at the ** original function position. +** For regular calls, 'delta1' is 0. For tail calls, 'delta1' is the +** 'delta' (correction of base for vararg functions) plus 1, so that it +** cannot be zero. Like 'moveparams', this correction can only be done +** when no more errors can occur in the call. */ -CallInfo *luaD_precall (lua_State *L, StkId func, int nresults) { +CallInfo *luaD_precall (lua_State *L, StkId func, int nresults, int delta1) { lua_CFunction f; retry: switch (ttypetag(s2v(func))) { @@ -542,12 +536,18 @@ CallInfo *luaD_precall (lua_State *L, StkId func, int nresults) { int nfixparams = p->numparams; int fsize = p->maxstacksize; /* frame size */ checkstackGCp(L, fsize, func); - L->ci = ci = next_ci(L); - ci->nresults = nresults; + if (delta1) { /* tail call? */ + ci = L->ci; /* reuse stack frame */ + ci->func -= delta1 - 1; /* correct 'func' */ + moveparams(L, ci->func, func, narg); + } + else { /* regular call */ + L->ci = ci = next_ci(L); /* new frame */ + ci->func = func; + ci->nresults = nresults; + } ci->u.l.savedpc = p->code; /* starting point */ ci->top = func + 1 + fsize; - ci->func = func; - L->ci = ci; for (; narg < nfixparams; narg++) setnilvalue(s2v(L->top++)); /* complete missing arguments */ lua_assert(ci->top <= L->stack_last); @@ -572,7 +572,7 @@ static void ccall (lua_State *L, StkId func, int nResults, int inc) { L->nCcalls += inc; if (l_unlikely(getCcalls(L) >= LUAI_MAXCCALLS)) luaE_checkcstack(L); - if ((ci = luaD_precall(L, func, nResults)) != NULL) { /* Lua function? */ + if ((ci = luaD_precall(L, func, nResults, 0)) != NULL) { /* Lua function? */ ci->callstatus = CIST_FRESH; /* mark that it is a "fresh" execute */ luaV_execute(L, ci); /* call it */ } diff --git a/ldo.h b/ldo.h index 6bf0ed86f7..6edc4450ac 100644 --- a/ldo.h +++ b/ldo.h @@ -58,8 +58,8 @@ LUAI_FUNC int luaD_protectedparser (lua_State *L, ZIO *z, const char *name, LUAI_FUNC void luaD_hook (lua_State *L, int event, int line, int fTransfer, int nTransfer); LUAI_FUNC void luaD_hookcall (lua_State *L, CallInfo *ci); -LUAI_FUNC void luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, int n); -LUAI_FUNC CallInfo *luaD_precall (lua_State *L, StkId func, int nResults); +LUAI_FUNC CallInfo *luaD_precall (lua_State *L, StkId func, int nresults, + int delta1); LUAI_FUNC void luaD_call (lua_State *L, StkId func, int nResults); LUAI_FUNC void luaD_callnoyield (lua_State *L, StkId func, int nResults); LUAI_FUNC void luaD_tryfuncTM (lua_State *L, StkId func); diff --git a/lvm.c b/lvm.c index e4b1903e70..485b9caa35 100644 --- a/lvm.c +++ b/lvm.c @@ -1632,11 +1632,11 @@ void luaV_execute (lua_State *L, CallInfo *ci) { L->top = ra + b; /* top signals number of arguments */ /* else previous instruction set top */ savepc(L); /* in case of errors */ - if ((newci = luaD_precall(L, ra, nresults)) == NULL) + if ((newci = luaD_precall(L, ra, nresults, 0)) == NULL) updatetrap(ci); /* C call; nothing else to be done */ else { /* Lua call: run function in this same C frame */ ci = newci; - ci->callstatus = 0; /* call re-uses 'luaV_execute' */ + ci->callstatus = 0; goto startfunc; } vmbreak; @@ -1648,21 +1648,18 @@ void luaV_execute (lua_State *L, CallInfo *ci) { int delta = (nparams1) ? ci->u.l.nextraargs + nparams1 : 0; if (b != 0) L->top = ra + b; - else /* previous instruction set top */ - b = cast_int(L->top - ra); + /* else previous instruction set top */ savepc(ci); /* several calls here can raise errors */ if (TESTARG_k(i)) { luaF_closeupval(L, base); /* close upvalues from current call */ lua_assert(L->tbclist < base); /* no pending tbc variables */ lua_assert(base == ci->func + 1); } - while (!ttisfunction(s2v(ra))) { /* not a function? */ - luaD_tryfuncTM(L, ra); /* try '__call' metamethod */ - b++; /* there is now one extra argument */ - checkstackGCp(L, 1, ra); + if (luaD_precall(L, ra, LUA_MULTRET, delta + 1)) { /* Lua function? */ + ci->callstatus |= CIST_TAIL; + goto startfunc; /* execute the callee */ } - if (!ttisLclosure(s2v(ra))) { /* C function? */ - luaD_precall(L, ra, LUA_MULTRET); /* call it */ + else { /* C function */ updatetrap(ci); updatestack(ci); /* stack may have been relocated */ ci->func -= delta; /* restore 'func' (if vararg) */ @@ -1670,9 +1667,6 @@ void luaV_execute (lua_State *L, CallInfo *ci) { updatetrap(ci); /* 'luaD_poscall' can change hooks */ goto ret; /* caller returns after the tail call */ } - ci->func -= delta; /* restore 'func' (if vararg) */ - luaD_pretailcall(L, ci, ra, b); /* prepare call frame */ - goto startfunc; /* execute the callee */ } vmcase(OP_RETURN) { int n = GETARG_B(i) - 1; /* number of results */ From 04e19712a5d48b84869f9942836ff8314fb0be8e Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 14 Jun 2021 13:28:21 -0300 Subject: [PATCH 0710/1145] C functions can be tail called, too A tail call to a C function can have the behavior of a "real" tail call, reusing the stack frame of the caller. --- ldo.c | 43 +++++++++++++++++++++++++------------------ lvm.c | 9 +-------- testes/coroutine.lua | 2 +- testes/errors.lua | 4 ++-- 4 files changed, 29 insertions(+), 29 deletions(-) diff --git a/ldo.c b/ldo.c index a410461bdb..38540561fd 100644 --- a/ldo.c +++ b/ldo.c @@ -478,12 +478,31 @@ void luaD_poscall (lua_State *L, CallInfo *ci, int nres) { ** (This is done only when no more errors can occur before entering the ** new function, to keep debug information always consistent.) */ -static void moveparams (lua_State *L, StkId prevf, StkId func, int narg) { +static void moveparams (lua_State *L, StkId prevf, StkId func) { int i; - narg++; /* function itself will be moved, too */ - for (i = 0; i < narg; i++) /* move down function and arguments */ + for (i = 0; func + i < L->top; i++) /* move down function and arguments */ setobjs2s(L, prevf + i, func + i); - L->top = prevf + narg; /* correct top */ + L->top = prevf + i; /* correct top */ +} + + +static CallInfo *prepCallInfo (lua_State *L, StkId func, int nresults, + int delta1, int mask) { + CallInfo *ci; + if (delta1) { /* tail call? */ + ci = L->ci; /* reuse stack frame */ + ci->func -= delta1 - 1; /* correct 'func' */ + + ci->callstatus |= mask | CIST_TAIL; + moveparams(L, ci->func, func); + } + else { /* regular call */ + ci = L->ci = next_ci(L); /* new frame */ + ci->func = func; + ci->nresults = nresults; + ci->callstatus = mask; + } + return ci; } @@ -512,11 +531,8 @@ CallInfo *luaD_precall (lua_State *L, StkId func, int nresults, int delta1) { int n; /* number of returns */ CallInfo *ci; checkstackGCp(L, LUA_MINSTACK, func); /* ensure minimum stack size */ - L->ci = ci = next_ci(L); - ci->nresults = nresults; - ci->callstatus = CIST_C; + ci = prepCallInfo(L, func, nresults, delta1, CIST_C); ci->top = L->top + LUA_MINSTACK; - ci->func = func; lua_assert(ci->top <= L->stack_last); if (l_unlikely(L->hookmask & LUA_MASKCALL)) { int narg = cast_int(L->top - func) - 1; @@ -536,16 +552,7 @@ CallInfo *luaD_precall (lua_State *L, StkId func, int nresults, int delta1) { int nfixparams = p->numparams; int fsize = p->maxstacksize; /* frame size */ checkstackGCp(L, fsize, func); - if (delta1) { /* tail call? */ - ci = L->ci; /* reuse stack frame */ - ci->func -= delta1 - 1; /* correct 'func' */ - moveparams(L, ci->func, func, narg); - } - else { /* regular call */ - L->ci = ci = next_ci(L); /* new frame */ - ci->func = func; - ci->nresults = nresults; - } + ci = prepCallInfo(L, func, nresults, delta1, 0); ci->u.l.savedpc = p->code; /* starting point */ ci->top = func + 1 + fsize; for (; narg < nfixparams; narg++) diff --git a/lvm.c b/lvm.c index 485b9caa35..62ff70da0a 100644 --- a/lvm.c +++ b/lvm.c @@ -1636,7 +1636,6 @@ void luaV_execute (lua_State *L, CallInfo *ci) { updatetrap(ci); /* C call; nothing else to be done */ else { /* Lua call: run function in this same C frame */ ci = newci; - ci->callstatus = 0; goto startfunc; } vmbreak; @@ -1655,16 +1654,10 @@ void luaV_execute (lua_State *L, CallInfo *ci) { lua_assert(L->tbclist < base); /* no pending tbc variables */ lua_assert(base == ci->func + 1); } - if (luaD_precall(L, ra, LUA_MULTRET, delta + 1)) { /* Lua function? */ - ci->callstatus |= CIST_TAIL; + if (luaD_precall(L, ra, LUA_MULTRET, delta + 1)) /* Lua function? */ goto startfunc; /* execute the callee */ - } else { /* C function */ updatetrap(ci); - updatestack(ci); /* stack may have been relocated */ - ci->func -= delta; /* restore 'func' (if vararg) */ - luaD_poscall(L, ci, cast_int(L->top - ra)); /* finish caller */ - updatetrap(ci); /* 'luaD_poscall' can change hooks */ goto ret; /* caller returns after the tail call */ } } diff --git a/testes/coroutine.lua b/testes/coroutine.lua index b36b76ea56..461e03770f 100644 --- a/testes/coroutine.lua +++ b/testes/coroutine.lua @@ -205,7 +205,7 @@ do co = coroutine.create(function () return pcall(foo) end) local st1, st2, err = coroutine.resume(co) assert(st1 and not st2 and err == 43) - assert(X == 43 and Y.name == "pcall") + assert(X == 43 and Y.what == "C") -- recovering from errors in __close metamethods local track = {} diff --git a/testes/errors.lua b/testes/errors.lua index a3d0676b15..825f37c294 100644 --- a/testes/errors.lua +++ b/testes/errors.lua @@ -26,7 +26,7 @@ end local function checkmessage (prog, msg, debug) local m = doit(prog) - if debug then print(m) end + if debug then print(m, msg) end assert(string.find(m, msg, 1, true)) end @@ -289,7 +289,7 @@ end]], "global 'insert'") checkmessage([[ -- tail call return math.sin("a") -]], "'sin'") +]], "sin") checkmessage([[collectgarbage("nooption")]], "invalid option") From 6a0dace25a4b5b77f0fa6911de2ba26ef0fdff2c Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Sun, 20 Jun 2021 15:36:36 -0300 Subject: [PATCH 0711/1145] Bug: 'local function' can assign to '' variables --- lparser.c | 1 + testes/locals.lua | 2 ++ 2 files changed, 3 insertions(+) diff --git a/lparser.c b/lparser.c index df9473c274..3abe3d7518 100644 --- a/lparser.c +++ b/lparser.c @@ -1785,6 +1785,7 @@ static void funcstat (LexState *ls, int line) { luaX_next(ls); /* skip FUNCTION */ ismethod = funcname(ls, &v); body(ls, &b, ismethod, line); + check_readonly(ls, &v); luaK_storevar(ls->fs, &v, &b); luaK_fixline(ls->fs, line); /* definition "happens" in the first line */ } diff --git a/testes/locals.lua b/testes/locals.lua index 6151f64d0c..62a88df57b 100644 --- a/testes/locals.lua +++ b/testes/locals.lua @@ -187,6 +187,8 @@ do -- constants checkro("y", "local x, y , z = 10, 20, 30; x = 11; y = 12") checkro("x", "local x , y, z = 10, 20, 30; x = 11") checkro("z", "local x , y, z = 10, 20, 30; y = 10; z = 11") + checkro("foo", "local foo = 10; function foo() end") + checkro("foo", "local foo = {}; function foo() end") checkro("z", [[ local a, z , b = 10; From dbdc74dc5502c2e05e1c1e2ac894943f418c8431 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 30 Jun 2021 12:53:21 -0300 Subject: [PATCH 0712/1145] Simplification in the parameters of 'luaD_precall' The parameters 'nresults' and 'delta1', in 'luaD_precall', were never meaningful simultaneously. So, they were combined in a single parameter 'retdel'. --- ldo.c | 19 +++++++++---------- ldo.h | 15 +++++++++++++-- lvm.c | 4 ++-- 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/ldo.c b/ldo.c index 38540561fd..93fcbb1afc 100644 --- a/ldo.c +++ b/ldo.c @@ -486,20 +486,19 @@ static void moveparams (lua_State *L, StkId prevf, StkId func) { } -static CallInfo *prepCallInfo (lua_State *L, StkId func, int nresults, - int delta1, int mask) { +static CallInfo *prepCallInfo (lua_State *L, StkId func, int retdel, + int mask) { CallInfo *ci; - if (delta1) { /* tail call? */ + if (isdelta(retdel)) { /* tail call? */ ci = L->ci; /* reuse stack frame */ - ci->func -= delta1 - 1; /* correct 'func' */ - + ci->func -= retdel2delta(retdel); /* correct 'func' */ ci->callstatus |= mask | CIST_TAIL; moveparams(L, ci->func, func); } else { /* regular call */ ci = L->ci = next_ci(L); /* new frame */ ci->func = func; - ci->nresults = nresults; + ci->nresults = retdel; ci->callstatus = mask; } return ci; @@ -518,7 +517,7 @@ static CallInfo *prepCallInfo (lua_State *L, StkId func, int nresults, ** cannot be zero. Like 'moveparams', this correction can only be done ** when no more errors can occur in the call. */ -CallInfo *luaD_precall (lua_State *L, StkId func, int nresults, int delta1) { +CallInfo *luaD_precall (lua_State *L, StkId func, int retdel) { lua_CFunction f; retry: switch (ttypetag(s2v(func))) { @@ -531,7 +530,7 @@ CallInfo *luaD_precall (lua_State *L, StkId func, int nresults, int delta1) { int n; /* number of returns */ CallInfo *ci; checkstackGCp(L, LUA_MINSTACK, func); /* ensure minimum stack size */ - ci = prepCallInfo(L, func, nresults, delta1, CIST_C); + ci = prepCallInfo(L, func, retdel, CIST_C); ci->top = L->top + LUA_MINSTACK; lua_assert(ci->top <= L->stack_last); if (l_unlikely(L->hookmask & LUA_MASKCALL)) { @@ -552,7 +551,7 @@ CallInfo *luaD_precall (lua_State *L, StkId func, int nresults, int delta1) { int nfixparams = p->numparams; int fsize = p->maxstacksize; /* frame size */ checkstackGCp(L, fsize, func); - ci = prepCallInfo(L, func, nresults, delta1, 0); + ci = prepCallInfo(L, func, retdel, 0); ci->u.l.savedpc = p->code; /* starting point */ ci->top = func + 1 + fsize; for (; narg < nfixparams; narg++) @@ -579,7 +578,7 @@ static void ccall (lua_State *L, StkId func, int nResults, int inc) { L->nCcalls += inc; if (l_unlikely(getCcalls(L) >= LUAI_MAXCCALLS)) luaE_checkcstack(L); - if ((ci = luaD_precall(L, func, nResults, 0)) != NULL) { /* Lua function? */ + if ((ci = luaD_precall(L, func, nResults)) != NULL) { /* Lua function? */ ci->callstatus = CIST_FRESH; /* mark that it is a "fresh" execute */ luaV_execute(L, ci); /* call it */ } diff --git a/ldo.h b/ldo.h index 6edc4450ac..49fbb492da 100644 --- a/ldo.h +++ b/ldo.h @@ -49,6 +49,18 @@ luaD_checkstackaux(L, (fsize), luaC_checkGC(L), (void)0) +/* +** 'luaD_precall' is used for regular calls, when it needs the +** number of results, and in tail calls, when it needs the 'delta' +** (correction of base for vararg functions). The argument 'retdel' +** codes these two options. A number of results is represented by +** itself, while a delta is represented by 'delta2retdel(delta)' +*/ +#define delta2retdel(d) (-(d) + LUA_MULTRET - 1) +#define retdel2delta(d) (-(d) + LUA_MULTRET - 1) +#define isdelta(rd) ((rd) < LUA_MULTRET) + + /* type of protected functions, to be ran by 'runprotected' */ typedef void (*Pfunc) (lua_State *L, void *ud); @@ -58,8 +70,7 @@ LUAI_FUNC int luaD_protectedparser (lua_State *L, ZIO *z, const char *name, LUAI_FUNC void luaD_hook (lua_State *L, int event, int line, int fTransfer, int nTransfer); LUAI_FUNC void luaD_hookcall (lua_State *L, CallInfo *ci); -LUAI_FUNC CallInfo *luaD_precall (lua_State *L, StkId func, int nresults, - int delta1); +LUAI_FUNC CallInfo *luaD_precall (lua_State *L, StkId func, int retdel); LUAI_FUNC void luaD_call (lua_State *L, StkId func, int nResults); LUAI_FUNC void luaD_callnoyield (lua_State *L, StkId func, int nResults); LUAI_FUNC void luaD_tryfuncTM (lua_State *L, StkId func); diff --git a/lvm.c b/lvm.c index 62ff70da0a..ec83f41597 100644 --- a/lvm.c +++ b/lvm.c @@ -1632,7 +1632,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { L->top = ra + b; /* top signals number of arguments */ /* else previous instruction set top */ savepc(L); /* in case of errors */ - if ((newci = luaD_precall(L, ra, nresults, 0)) == NULL) + if ((newci = luaD_precall(L, ra, nresults)) == NULL) updatetrap(ci); /* C call; nothing else to be done */ else { /* Lua call: run function in this same C frame */ ci = newci; @@ -1654,7 +1654,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { lua_assert(L->tbclist < base); /* no pending tbc variables */ lua_assert(base == ci->func + 1); } - if (luaD_precall(L, ra, LUA_MULTRET, delta + 1)) /* Lua function? */ + if (luaD_precall(L, ra, delta2retdel(delta))) /* Lua function? */ goto startfunc; /* execute the callee */ else { /* C function */ updatetrap(ci); From 8a32e0aa4afdfd575c329ce62baaf7a14888ed3b Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 21 Jul 2021 11:33:58 -0300 Subject: [PATCH 0713/1145] Correction on documentation of string-buffer operations All string-buffer operations can potentially change the stack in unspecified ways; the push/pop documentation in the manual should reflect that. --- manual/manual.of | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/manual/manual.of b/manual/manual.of index 8cf0abfcd1..67d3b7e159 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -5144,7 +5144,7 @@ Adds the byte @id{c} to the buffer @id{B} @APIEntry{ const void luaL_addgsub (luaL_Buffer *B, const char *s, const char *p, const char *r);| -@apii{0,0,m} +@apii{?,?,m} Adds a copy of the string @id{s} to the buffer @id{B} @seeC{luaL_Buffer}, replacing any occurrence of the string @id{p} @@ -5181,7 +5181,7 @@ to the buffer @id{B} } @APIEntry{void luaL_addvalue (luaL_Buffer *B);| -@apii{1,?,m} +@apii{?,?,m} Adds the value on the top of the stack to the buffer @id{B} @@ -5304,7 +5304,7 @@ Note that any addition to the buffer may invalidate this address. } @APIEntry{void luaL_buffinit (lua_State *L, luaL_Buffer *B);| -@apii{0,0,-} +@apii{0,?,-} Initializes a buffer @id{B} @seeC{luaL_Buffer}. @@ -5330,7 +5330,7 @@ Equivalent to the sequence } @APIEntry{void luaL_buffsub (luaL_Buffer *B, int n);| -@apii{0,0,-} +@apii{?,?,-} Removes @id{n} bytes from the the buffer @id{B} @seeC{luaL_Buffer}. From 62fb93442753cbfb828335cd172e71471dffd536 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 22 Jul 2021 13:44:53 -0300 Subject: [PATCH 0714/1145] Bug: Negation in 'luaV_shiftr' may overflow Negation of an unchecked lua_Integer overflows with mininteger. --- lvm.c | 2 +- testes/bitwise.lua | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/lvm.c b/lvm.c index ec83f41597..c84a665f5c 100644 --- a/lvm.c +++ b/lvm.c @@ -766,7 +766,7 @@ lua_Number luaV_modf (lua_State *L, lua_Number m, lua_Number n) { /* ** Shift left operation. (Shift right just negates 'y'.) */ -#define luaV_shiftr(x,y) luaV_shiftl(x,-(y)) +#define luaV_shiftr(x,y) luaV_shiftl(x,intop(-, 0, y)) lua_Integer luaV_shiftl (lua_Integer x, lua_Integer y) { if (y < 0) { /* shift right? */ diff --git a/testes/bitwise.lua b/testes/bitwise.lua index 59781f5dfa..9509f7f040 100644 --- a/testes/bitwise.lua +++ b/testes/bitwise.lua @@ -45,6 +45,11 @@ assert(-1 >> numbits == 0 and -1 << numbits == 0 and -1 << -numbits == 0) +assert(1 >> math.mininteger == 0) +assert(1 >> math.maxinteger == 0) +assert(1 << math.mininteger == 0) +assert(1 << math.maxinteger == 0) + assert((2^30 - 1) << 2^30 == 0) assert((2^30 - 1) >> 2^30 == 0) From 439e45a2f69549b674d6a6e2023e8debfa00a2b8 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 22 Jul 2021 13:48:43 -0300 Subject: [PATCH 0715/1145] Bug: luaL_tolstring may get confused with negative index When object has a '__name' metafield, 'luaL_tolstring' used the received index after pushing a string on the stack. --- lauxlib.c | 1 + ltests.c | 3 +++ testes/errors.lua | 16 ++++++++++++++++ 3 files changed, 20 insertions(+) diff --git a/lauxlib.c b/lauxlib.c index 94835ef934..8ed1da1122 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -881,6 +881,7 @@ LUALIB_API lua_Integer luaL_len (lua_State *L, int idx) { LUALIB_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len) { + idx = lua_absindex(L,idx); if (luaL_callmeta(L, idx, "__tostring")) { /* metafield? */ if (!lua_isstring(L, -1)) luaL_error(L, "'__tostring' must return a string"); diff --git a/ltests.c b/ltests.c index a50f783047..97834e3802 100644 --- a/ltests.c +++ b/ltests.c @@ -1743,6 +1743,9 @@ static struct X { int x; } x; (void)s1; /* to avoid warnings */ lua_longassert((s == NULL && s1 == NULL) || strcmp(s, s1) == 0); } + else if EQ("Ltolstring") { + luaL_tolstring(L1, getindex, NULL); + } else if EQ("type") { lua_pushstring(L1, luaL_typename(L1, getnum)); } diff --git a/testes/errors.lua b/testes/errors.lua index 825f37c294..a7dc479a2c 100644 --- a/testes/errors.lua +++ b/testes/errors.lua @@ -228,6 +228,22 @@ do -- named objects (field '__name') checkmessage("return {} < XX", "table with My Type") checkmessage("return XX < io.stdin", "My Type with FILE*") _G.XX = nil + + if T then -- extra tests for 'luaL_tolstring' + -- bug in 5.4.3; 'luaL_tolstring' with negative indices + local x = setmetatable({}, {__name="TABLE"}) + assert(T.testC("Ltolstring -1; return 1", x) == tostring(x)) + + local a, b = T.testC("pushint 10; Ltolstring -2; return 2", x) + assert(a == 10 and b == tostring(x)) + + setmetatable(x, {__tostring=function (o) + assert(o == x) + return "ABC" + end}) + a, b, c = T.testC("pushint 10; Ltolstring -2; return 3", x) + assert(a == x and b == 10 and c == "ABC") + end end -- global functions From e2c07dcbf7492e79e5825a6ca66d28e2e372f71e Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 11 Aug 2021 11:18:10 -0300 Subject: [PATCH 0716/1145] Improved documentation for 'lua_getinfo' --- manual/manual.of | 42 ++++++++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/manual/manual.of b/manual/manual.of index 67d3b7e159..664b5c1ef8 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -4685,7 +4685,10 @@ information about a function or an activation record. @Lid{lua_getstack} fills only the private part of this structure, for later use. To fill the other fields of @Lid{lua_Debug} with useful information, -you must call @Lid{lua_getinfo}. +you must call @Lid{lua_getinfo} with an appropriate parameter. +(Specifically, to get a field, +you must add the letter between parentheses in the field's comment +to the parameter @id{what} of @Lid{lua_getinfo}.) The fields of @Lid{lua_Debug} have the following meaning: @description{ @@ -4838,10 +4841,24 @@ printf("%d\n", ar.linedefined); Each character in the string @id{what} selects some fields of the structure @id{ar} to be filled or -a value to be pushed on the stack: +a value to be pushed on the stack. +(These characters are also documented in the declaration of +the structure @Lid{lua_Debug}, +between parentheses in the comments following each field.) @description{ -@item{@Char{n}| fills in the field @id{name} and @id{namewhat}; +@item{@Char{f}| +pushes onto the stack the function that is +running at the given level; +} + +@item{@Char{l}| fills in the field @id{currentline}; +} + +@item{@Char{n}| fills in the fields @id{name} and @id{namewhat}; +} + +@item{@Char{r}| fills in the fields @id{ftransfer} and @id{ntransfer}; } @item{@Char{S}| @@ -4849,9 +4866,6 @@ fills in the fields @id{source}, @id{short_src}, @id{linedefined}, @id{lastlinedefined}, and @id{what}; } -@item{@Char{l}| fills in the field @id{currentline}; -} - @item{@Char{t}| fills in the field @id{istailcall}; } @@ -4859,21 +4873,13 @@ fills in the fields @id{source}, @id{short_src}, @id{nups}, @id{nparams}, and @id{isvararg}; } -@item{@Char{f}| -pushes onto the stack the function that is -running at the given level; -} - @item{@Char{L}| -pushes onto the stack a table whose indices are the -numbers of the lines that are valid on the function. -(A @emph{valid line} is a line with some associated code, -that is, a line where you can put a break point. -Non-valid lines include empty lines and comments.) - +pushes onto the stack a table whose indices are +the lines on the function with some associated code, +that is, the lines where you can put a break point. +(Lines with no code include empty lines and comments.) If this option is given together with option @Char{f}, its table is pushed after the function. - This is the only option that can raise a memory error. } From 59acd79c05b78950fe03279d60b015aeed5348ab Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 11 Aug 2021 11:19:33 -0300 Subject: [PATCH 0717/1145] Added tests for string reuse by the scanner --- testes/errors.lua | 2 +- testes/literals.lua | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/testes/errors.lua b/testes/errors.lua index a7dc479a2c..55bdab829e 100644 --- a/testes/errors.lua +++ b/testes/errors.lua @@ -241,7 +241,7 @@ do -- named objects (field '__name') assert(o == x) return "ABC" end}) - a, b, c = T.testC("pushint 10; Ltolstring -2; return 3", x) + local a, b, c = T.testC("pushint 10; Ltolstring -2; return 3", x) assert(a == x and b == 10 and c == "ABC") end end diff --git a/testes/literals.lua b/testes/literals.lua index e101eabfd8..d5a769ed2e 100644 --- a/testes/literals.lua +++ b/testes/literals.lua @@ -208,6 +208,30 @@ a = nil b = nil +do -- reuse of long strings + + -- get the address of a string + local function getadd (s) return string.format("%p", s) end + + local s1 = "01234567890123456789012345678901234567890123456789" + local s2 = "01234567890123456789012345678901234567890123456789" + local s3 = "01234567890123456789012345678901234567890123456789" + local function foo() return s1 end + local function foo1() return s3 end + local function foo2() + return "01234567890123456789012345678901234567890123456789" + end + local a1 = getadd(s1) + assert(a1 == getadd(s2)) + assert(a1 == getadd(foo())) + assert(a1 == getadd(foo1())) + assert(a1 == getadd(foo2())) + + local sd = "0123456789" .. "0123456789012345678901234567890123456789" + assert(sd == s1 and getadd(sd) ~= a1) +end + + -- testing line ends prog = [[ a = 1 -- a comment From 65434b4d1b5509e95940939e28fd90d4558da12e Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 16 Aug 2021 13:57:19 -0300 Subject: [PATCH 0718/1145] Option '-l' can give a name for the global variable. Sintax for this option now is '-l [globname=]modname'. --- lua.c | 37 ++++++++++++++++++++++--------------- testes/main.lua | 5 +++++ 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/lua.c b/lua.c index 46b48dba92..0f19004444 100644 --- a/lua.c +++ b/lua.c @@ -89,14 +89,15 @@ static void print_usage (const char *badoption) { lua_writestringerror( "usage: %s [options] [script [args]]\n" "Available options are:\n" - " -e stat execute string 'stat'\n" - " -i enter interactive mode after executing 'script'\n" - " -l name require library 'name' into global 'name'\n" - " -v show version information\n" - " -E ignore environment variables\n" - " -W turn warnings on\n" - " -- stop handling options\n" - " - stop handling options and execute stdin\n" + " -e stat execute string 'stat'\n" + " -i enter interactive mode after executing 'script'\n" + " -l mod require library 'mod' into global 'mod'\n" + " -l g=mod require library 'mod' into global 'g'\n" + " -v show version information\n" + " -E ignore environment variables\n" + " -W turn warnings on\n" + " -- stop handling options\n" + " - stop handling options and execute stdin\n" , progname); } @@ -207,16 +208,22 @@ static int dostring (lua_State *L, const char *s, const char *name) { /* -** Calls 'require(name)' and stores the result in a global variable -** with the given name. +** Receives 'globname[=modname]' and runs 'globname = require(modname)'. */ -static int dolibrary (lua_State *L, const char *name) { +static int dolibrary (lua_State *L, char *globname) { int status; + char *modname = strchr(globname, '='); + if (modname == NULL) /* no explicit name? */ + modname = globname; /* module name is equal to global name */ + else { + *modname = '\0'; /* global name ends here */ + modname++; /* module name starts after the '=' */ + } lua_getglobal(L, "require"); - lua_pushstring(L, name); - status = docall(L, 1, 1); /* call 'require(name)' */ + lua_pushstring(L, modname); + status = docall(L, 1, 1); /* call 'require(modname)' */ if (status == LUA_OK) - lua_setglobal(L, name); /* global[name] = require return */ + lua_setglobal(L, globname); /* globname = require(modname) */ return report(L, status); } @@ -327,7 +334,7 @@ static int runargs (lua_State *L, char **argv, int n) { switch (option) { case 'e': case 'l': { int status; - const char *extra = argv[i] + 2; /* both options need an argument */ + char *extra = argv[i] + 2; /* both options need an argument */ if (*extra == '\0') extra = argv[++i]; lua_assert(extra != NULL); status = (option == 'e') diff --git a/testes/main.lua b/testes/main.lua index 56959abd96..52c779541c 100644 --- a/testes/main.lua +++ b/testes/main.lua @@ -190,6 +190,11 @@ prepfile(("print(a); print(_G['%s'].x)"):format(prog), otherprog) RUN('env LUA_PATH="?;;" lua -l %s -l%s -lstring -l io %s > %s', prog, otherprog, otherprog, out) checkout("1\n2\n15\n2\n15\n") +-- test explicit global names in -l +prepfile("print(str.upper'alo alo', m.max(10, 20))") +RUN("lua -l 'str=string' '-lm=math' -e 'print(m.sin(0))' %s > %s", prog, out) +checkout("0.0\nALO ALO\t20\n") + -- test 'arg' table local a = [[ assert(#arg == 3 and arg[1] == 'a' and From a393ac255493276b1ad9d7be057fe58ea824dd00 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 18 Aug 2021 10:46:18 -0300 Subject: [PATCH 0719/1145] Detail in 'testes/math.lua' Added a print with the random seeds used in the tests of 'random'. --- testes/math.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/testes/math.lua b/testes/math.lua index 930221e362..48c1efe159 100644 --- a/testes/math.lua +++ b/testes/math.lua @@ -849,6 +849,7 @@ do math.randomseed(x, y) -- again should repeat the state assert(math.random(0) == res) -- keep the random seed for following tests + print(string.format("random seeds: %d, %d", x, y)) end do -- test random for floats From 41871f1803770305f182f56cbd22a336c5236a19 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 18 Aug 2021 11:21:33 -0300 Subject: [PATCH 0720/1145] Undo simplification of tail calls (commit 901d760) Not that simpler and slower. --- ldo.c | 66 +++++++++++++++++++++++++++++------------------------------ ldo.h | 15 ++------------ lvm.c | 21 +++++++++++++++---- 3 files changed, 51 insertions(+), 51 deletions(-) diff --git a/ldo.c b/ldo.c index 93fcbb1afc..fa8d98b2c6 100644 --- a/ldo.c +++ b/ldo.c @@ -474,33 +474,36 @@ void luaD_poscall (lua_State *L, CallInfo *ci, int nres) { /* -** In a tail call, move function and parameters to previous call frame. -** (This is done only when no more errors can occur before entering the -** new function, to keep debug information always consistent.) +** Prepare a function for a tail call, building its call info on top +** of the current call info. 'narg1' is the number of arguments plus 1 +** (so that it includes the function itself). */ -static void moveparams (lua_State *L, StkId prevf, StkId func) { +void luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, int narg1) { + Proto *p = clLvalue(s2v(func))->p; + int fsize = p->maxstacksize; /* frame size */ + int nfixparams = p->numparams; int i; - for (i = 0; func + i < L->top; i++) /* move down function and arguments */ - setobjs2s(L, prevf + i, func + i); - L->top = prevf + i; /* correct top */ -} - - -static CallInfo *prepCallInfo (lua_State *L, StkId func, int retdel, - int mask) { - CallInfo *ci; - if (isdelta(retdel)) { /* tail call? */ - ci = L->ci; /* reuse stack frame */ - ci->func -= retdel2delta(retdel); /* correct 'func' */ - ci->callstatus |= mask | CIST_TAIL; - moveparams(L, ci->func, func); - } - else { /* regular call */ - ci = L->ci = next_ci(L); /* new frame */ - ci->func = func; - ci->nresults = retdel; - ci->callstatus = mask; - } + for (i = 0; i < narg1; i++) /* move down function and arguments */ + setobjs2s(L, ci->func + i, func + i); + checkstackGC(L, fsize); + func = ci->func; /* moved-down function */ + for (; narg1 <= nfixparams; narg1++) + setnilvalue(s2v(func + narg1)); /* complete missing arguments */ + ci->top = func + 1 + fsize; /* top for new function */ + lua_assert(ci->top <= L->stack_last); + ci->u.l.savedpc = p->code; /* starting point */ + ci->callstatus |= CIST_TAIL; + L->top = func + narg1; /* set top */ +} + + +static CallInfo *prepCallInfo (lua_State *L, StkId func, int nret, + int mask, StkId top) { + CallInfo *ci = L->ci = next_ci(L); /* new frame */ + ci->func = func; + ci->nresults = nret; + ci->callstatus = mask; + ci->top = top; return ci; } @@ -512,12 +515,8 @@ static CallInfo *prepCallInfo (lua_State *L, StkId func, int retdel, ** to be executed, if it was a Lua function. Otherwise (a C function) ** returns NULL, with all the results on the stack, starting at the ** original function position. -** For regular calls, 'delta1' is 0. For tail calls, 'delta1' is the -** 'delta' (correction of base for vararg functions) plus 1, so that it -** cannot be zero. Like 'moveparams', this correction can only be done -** when no more errors can occur in the call. */ -CallInfo *luaD_precall (lua_State *L, StkId func, int retdel) { +CallInfo *luaD_precall (lua_State *L, StkId func, int nresults) { lua_CFunction f; retry: switch (ttypetag(s2v(func))) { @@ -530,8 +529,8 @@ CallInfo *luaD_precall (lua_State *L, StkId func, int retdel) { int n; /* number of returns */ CallInfo *ci; checkstackGCp(L, LUA_MINSTACK, func); /* ensure minimum stack size */ - ci = prepCallInfo(L, func, retdel, CIST_C); - ci->top = L->top + LUA_MINSTACK; + L->ci = ci = prepCallInfo(L, func, nresults, CIST_C, + L->top + LUA_MINSTACK); lua_assert(ci->top <= L->stack_last); if (l_unlikely(L->hookmask & LUA_MASKCALL)) { int narg = cast_int(L->top - func) - 1; @@ -551,9 +550,8 @@ CallInfo *luaD_precall (lua_State *L, StkId func, int retdel) { int nfixparams = p->numparams; int fsize = p->maxstacksize; /* frame size */ checkstackGCp(L, fsize, func); - ci = prepCallInfo(L, func, retdel, 0); + L->ci = ci = prepCallInfo(L, func, nresults, 0, func + 1 + fsize); ci->u.l.savedpc = p->code; /* starting point */ - ci->top = func + 1 + fsize; for (; narg < nfixparams; narg++) setnilvalue(s2v(L->top++)); /* complete missing arguments */ lua_assert(ci->top <= L->stack_last); diff --git a/ldo.h b/ldo.h index 49fbb492da..6bf0ed86f7 100644 --- a/ldo.h +++ b/ldo.h @@ -49,18 +49,6 @@ luaD_checkstackaux(L, (fsize), luaC_checkGC(L), (void)0) -/* -** 'luaD_precall' is used for regular calls, when it needs the -** number of results, and in tail calls, when it needs the 'delta' -** (correction of base for vararg functions). The argument 'retdel' -** codes these two options. A number of results is represented by -** itself, while a delta is represented by 'delta2retdel(delta)' -*/ -#define delta2retdel(d) (-(d) + LUA_MULTRET - 1) -#define retdel2delta(d) (-(d) + LUA_MULTRET - 1) -#define isdelta(rd) ((rd) < LUA_MULTRET) - - /* type of protected functions, to be ran by 'runprotected' */ typedef void (*Pfunc) (lua_State *L, void *ud); @@ -70,7 +58,8 @@ LUAI_FUNC int luaD_protectedparser (lua_State *L, ZIO *z, const char *name, LUAI_FUNC void luaD_hook (lua_State *L, int event, int line, int fTransfer, int nTransfer); LUAI_FUNC void luaD_hookcall (lua_State *L, CallInfo *ci); -LUAI_FUNC CallInfo *luaD_precall (lua_State *L, StkId func, int retdel); +LUAI_FUNC void luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, int n); +LUAI_FUNC CallInfo *luaD_precall (lua_State *L, StkId func, int nResults); LUAI_FUNC void luaD_call (lua_State *L, StkId func, int nResults); LUAI_FUNC void luaD_callnoyield (lua_State *L, StkId func, int nResults); LUAI_FUNC void luaD_tryfuncTM (lua_State *L, StkId func); diff --git a/lvm.c b/lvm.c index c84a665f5c..df1dec8396 100644 --- a/lvm.c +++ b/lvm.c @@ -768,6 +768,7 @@ lua_Number luaV_modf (lua_State *L, lua_Number m, lua_Number n) { */ #define luaV_shiftr(x,y) luaV_shiftl(x,intop(-, 0, y)) + lua_Integer luaV_shiftl (lua_Integer x, lua_Integer y) { if (y < 0) { /* shift right? */ if (y <= -NBITS) return 0; @@ -1647,19 +1648,31 @@ void luaV_execute (lua_State *L, CallInfo *ci) { int delta = (nparams1) ? ci->u.l.nextraargs + nparams1 : 0; if (b != 0) L->top = ra + b; - /* else previous instruction set top */ + else /* previous instruction set top */ + b = cast_int(L->top - ra); savepc(ci); /* several calls here can raise errors */ if (TESTARG_k(i)) { luaF_closeupval(L, base); /* close upvalues from current call */ lua_assert(L->tbclist < base); /* no pending tbc variables */ lua_assert(base == ci->func + 1); } - if (luaD_precall(L, ra, delta2retdel(delta))) /* Lua function? */ - goto startfunc; /* execute the callee */ - else { /* C function */ + while (!ttisfunction(s2v(ra))) { /* not a function? */ + luaD_tryfuncTM(L, ra); /* try '__call' metamethod */ + b++; /* there is now one extra argument */ + checkstackGCp(L, 1, ra); + } + if (!ttisLclosure(s2v(ra))) { /* C function? */ + luaD_precall(L, ra, LUA_MULTRET); /* call it */ updatetrap(ci); + updatestack(ci); /* stack may have been relocated */ + ci->func -= delta; /* restore 'func' (if vararg) */ + luaD_poscall(L, ci, cast_int(L->top - ra)); /* finish caller */ + updatetrap(ci); /* 'luaD_poscall' can change hooks */ goto ret; /* caller returns after the tail call */ } + ci->func -= delta; /* restore 'func' (if vararg) */ + luaD_pretailcall(L, ci, ra, b); /* prepare call frame */ + goto startfunc; /* execute the callee */ } vmcase(OP_RETURN) { int n = GETARG_B(i) - 1; /* number of results */ From 91673a8ec0ae55e188a790bd2dfdc99246adf20e Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 18 Aug 2021 12:05:06 -0300 Subject: [PATCH 0721/1145] 'luaD_tryfuncTM' checks stack space by itself --- ldo.c | 7 ++++--- ldo.h | 2 +- lvm.c | 11 ++++++----- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/ldo.c b/ldo.c index fa8d98b2c6..889cb34b47 100644 --- a/ldo.c +++ b/ldo.c @@ -387,15 +387,17 @@ static void rethook (lua_State *L, CallInfo *ci, int nres) { ** stack, below original 'func', so that 'luaD_precall' can call it. Raise ** an error if there is no '__call' metafield. */ -void luaD_tryfuncTM (lua_State *L, StkId func) { +StkId luaD_tryfuncTM (lua_State *L, StkId func) { const TValue *tm = luaT_gettmbyobj(L, s2v(func), TM_CALL); StkId p; + checkstackGCp(L, 1, func); /* space for metamethod */ if (l_unlikely(ttisnil(tm))) luaG_callerror(L, s2v(func)); /* nothing to call */ for (p = L->top; p > func; p--) /* open space for metamethod */ setobjs2s(L, p, p-1); L->top++; /* stack space pre-allocated by the caller */ setobj2s(L, func, tm); /* metamethod is the new function to be called */ + return func; } @@ -558,8 +560,7 @@ CallInfo *luaD_precall (lua_State *L, StkId func, int nresults) { return ci; } default: { /* not a function */ - checkstackGCp(L, 1, func); /* space for metamethod */ - luaD_tryfuncTM(L, func); /* try to get '__call' metamethod */ + func = luaD_tryfuncTM(L, func); /* try to get '__call' metamethod */ goto retry; /* try again with metamethod */ } } diff --git a/ldo.h b/ldo.h index 6bf0ed86f7..9fb772fe3a 100644 --- a/ldo.h +++ b/ldo.h @@ -62,7 +62,7 @@ LUAI_FUNC void luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, int n); LUAI_FUNC CallInfo *luaD_precall (lua_State *L, StkId func, int nResults); LUAI_FUNC void luaD_call (lua_State *L, StkId func, int nResults); LUAI_FUNC void luaD_callnoyield (lua_State *L, StkId func, int nResults); -LUAI_FUNC void luaD_tryfuncTM (lua_State *L, StkId func); +LUAI_FUNC StkId luaD_tryfuncTM (lua_State *L, StkId func); LUAI_FUNC int luaD_closeprotected (lua_State *L, ptrdiff_t level, int status); LUAI_FUNC int luaD_pcall (lua_State *L, Pfunc func, void *u, ptrdiff_t oldtop, ptrdiff_t ef); diff --git a/lvm.c b/lvm.c index df1dec8396..29a211c677 100644 --- a/lvm.c +++ b/lvm.c @@ -1657,9 +1657,8 @@ void luaV_execute (lua_State *L, CallInfo *ci) { lua_assert(base == ci->func + 1); } while (!ttisfunction(s2v(ra))) { /* not a function? */ - luaD_tryfuncTM(L, ra); /* try '__call' metamethod */ + ra = luaD_tryfuncTM(L, ra); /* try '__call' metamethod */ b++; /* there is now one extra argument */ - checkstackGCp(L, 1, ra); } if (!ttisLclosure(s2v(ra))) { /* C function? */ luaD_precall(L, ra, LUA_MULTRET); /* call it */ @@ -1670,9 +1669,11 @@ void luaV_execute (lua_State *L, CallInfo *ci) { updatetrap(ci); /* 'luaD_poscall' can change hooks */ goto ret; /* caller returns after the tail call */ } - ci->func -= delta; /* restore 'func' (if vararg) */ - luaD_pretailcall(L, ci, ra, b); /* prepare call frame */ - goto startfunc; /* execute the callee */ + else { /* Lua function */ + ci->func -= delta; /* restore 'func' (if vararg) */ + luaD_pretailcall(L, ci, ra, b); /* prepare call frame */ + goto startfunc; /* execute the callee */ + } } vmcase(OP_RETURN) { int n = GETARG_B(i) - 1; /* number of results */ From 9db4bfed6bb9d5828c99c0f24749eedf54d70cc2 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 3 Sep 2021 13:14:56 -0300 Subject: [PATCH 0722/1145] Revamp of format validation in 'string.format' When calling 'sprintf', not all conversion specifiers accept all flags; some combinations are undefined behavior. --- lstrlib.c | 112 ++++++++++++++++++++++++++++++++++----------- manual/manual.of | 6 ++- testes/strings.lua | 36 +++++++++++---- 3 files changed, 118 insertions(+), 36 deletions(-) diff --git a/lstrlib.c b/lstrlib.c index 74501f78a9..e3b8df0f17 100644 --- a/lstrlib.c +++ b/lstrlib.c @@ -1090,13 +1090,31 @@ static int lua_number2strx (lua_State *L, char *buff, int sz, /* valid flags in a format specification */ -#if !defined(L_FMTFLAGS) -#define L_FMTFLAGS "-+ #0" +#if !defined(L_FMTFLAGSF) + +/* valid flags for a, A, e, E, f, F, g, and G conversions */ +#define L_FMTFLAGSF "-+#0 " + +/* valid flags for o, x, and X conversions */ +#define L_FMTFLAGSX "-#0" + +/* valid flags for d and i conversions */ +#define L_FMTFLAGSI "-+0 " + +/* valid flags for u conversions */ +#define L_FMTFLAGSU "-0" + +/* valid flags for c, p, and s conversions */ +#define L_FMTFLAGSC "-" + #endif /* -** maximum size of each format specification (such as "%-099.99d") +** Maximum size of each format specification (such as "%-099.99d"): +** Initial '%', flags (up to 5), width (2), period, precision (2), +** length modifier (8), conversion specifier, and final '\0', plus some +** extra. */ #define MAX_FORMAT 32 @@ -1189,25 +1207,53 @@ static void addliteral (lua_State *L, luaL_Buffer *b, int arg) { } -static const char *scanformat (lua_State *L, const char *strfrmt, char *form) { - const char *p = strfrmt; - while (*p != '\0' && strchr(L_FMTFLAGS, *p) != NULL) p++; /* skip flags */ - if ((size_t)(p - strfrmt) >= sizeof(L_FMTFLAGS)/sizeof(char)) - luaL_error(L, "invalid format (repeated flags)"); - if (isdigit(uchar(*p))) p++; /* skip width */ - if (isdigit(uchar(*p))) p++; /* (2 digits at most) */ - if (*p == '.') { - p++; - if (isdigit(uchar(*p))) p++; /* skip precision */ - if (isdigit(uchar(*p))) p++; /* (2 digits at most) */ +static const char *get2digits (const char *s) { + if (isdigit(uchar(*s))) { + s++; + if (isdigit(uchar(*s))) s++; /* (2 digits at most) */ + } + return s; +} + + +/* +** Chech whether a conversion specification is valid. When called, +** first character in 'form' must be '%' and last character must +** be a valid conversion specifier. 'flags' are the accepted flags; +** 'precision' signals whether to accept a precision. +*/ +static void checkformat (lua_State *L, const char *form, const char *flags, + int precision) { + const char *spec = form + 1; /* skip '%' */ + spec += strspn(spec, flags); /* skip flags */ + if (*spec != '0') { /* a width cannot start with '0' */ + spec = get2digits(spec); /* skip width */ + if (*spec == '.' && precision) { + spec++; + spec = get2digits(spec); /* skip precision */ + } } - if (isdigit(uchar(*p))) - luaL_error(L, "invalid format (width or precision too long)"); + if (!isalpha(uchar(*spec))) /* did not go to the end? */ + luaL_error(L, "invalid conversion specification: '%s'", form); +} + + +/* +** Get a conversion specification and copy it to 'form'. +** Return the address of its last character. +*/ +static const char *getformat (lua_State *L, const char *strfrmt, + char *form) { + /* spans flags, width, and precision ('0' is included as a flag) */ + size_t len = strspn(strfrmt, L_FMTFLAGSF "123456789."); + len++; /* adds following character (should be the specifier) */ + /* still needs space for '%', '\0', plus a length modifier */ + if (len >= MAX_FORMAT - 10) + luaL_error(L, "invalid format (too long)"); *(form++) = '%'; - memcpy(form, strfrmt, ((p - strfrmt) + 1) * sizeof(char)); - form += (p - strfrmt) + 1; - *form = '\0'; - return p; + memcpy(form, strfrmt, len * sizeof(char)); + *(form + len) = '\0'; + return strfrmt + len - 1; } @@ -1230,6 +1276,7 @@ static int str_format (lua_State *L) { size_t sfl; const char *strfrmt = luaL_checklstring(L, arg, &sfl); const char *strfrmt_end = strfrmt+sfl; + const char *flags; luaL_Buffer b; luaL_buffinit(L, &b); while (strfrmt < strfrmt_end) { @@ -1239,25 +1286,35 @@ static int str_format (lua_State *L) { luaL_addchar(&b, *strfrmt++); /* %% */ else { /* format item */ char form[MAX_FORMAT]; /* to store the format ('%...') */ - int maxitem = MAX_ITEM; - char *buff = luaL_prepbuffsize(&b, maxitem); /* to put formatted item */ - int nb = 0; /* number of bytes in added item */ + int maxitem = MAX_ITEM; /* maximum length for the result */ + char *buff = luaL_prepbuffsize(&b, maxitem); /* to put result */ + int nb = 0; /* number of bytes in result */ if (++arg > top) return luaL_argerror(L, arg, "no value"); - strfrmt = scanformat(L, strfrmt, form); + strfrmt = getformat(L, strfrmt, form); switch (*strfrmt++) { case 'c': { + checkformat(L, form, L_FMTFLAGSC, 0); nb = l_sprintf(buff, maxitem, form, (int)luaL_checkinteger(L, arg)); break; } case 'd': case 'i': - case 'o': case 'u': case 'x': case 'X': { + flags = L_FMTFLAGSI; + goto intcase; + case 'u': + flags = L_FMTFLAGSU; + goto intcase; + case 'o': case 'x': case 'X': + flags = L_FMTFLAGSX; + intcase: { lua_Integer n = luaL_checkinteger(L, arg); + checkformat(L, form, flags, 1); addlenmod(form, LUA_INTEGER_FRMLEN); nb = l_sprintf(buff, maxitem, form, (LUAI_UACINT)n); break; } case 'a': case 'A': + checkformat(L, form, L_FMTFLAGSF, 1); addlenmod(form, LUA_NUMBER_FRMLEN); nb = lua_number2strx(L, buff, maxitem, form, luaL_checknumber(L, arg)); @@ -1268,12 +1325,14 @@ static int str_format (lua_State *L) { /* FALLTHROUGH */ case 'e': case 'E': case 'g': case 'G': { lua_Number n = luaL_checknumber(L, arg); + checkformat(L, form, L_FMTFLAGSF, 1); addlenmod(form, LUA_NUMBER_FRMLEN); nb = l_sprintf(buff, maxitem, form, (LUAI_UACNUMBER)n); break; } case 'p': { const void *p = lua_topointer(L, arg); + checkformat(L, form, L_FMTFLAGSC, 0); if (p == NULL) { /* avoid calling 'printf' with argument NULL */ p = "(null)"; /* result */ form[strlen(form) - 1] = 's'; /* format it as a string */ @@ -1294,7 +1353,8 @@ static int str_format (lua_State *L) { luaL_addvalue(&b); /* keep entire string */ else { luaL_argcheck(L, l == strlen(s), arg, "string contains zeros"); - if (!strchr(form, '.') && l >= 100) { + checkformat(L, form, L_FMTFLAGSC, 1); + if (strchr(form, '.') == NULL && l >= 100) { /* no precision and string is too long to be formatted */ luaL_addvalue(&b); /* keep entire string */ } diff --git a/manual/manual.of b/manual/manual.of index 664b5c1ef8..ea9a030219 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -7078,8 +7078,10 @@ following the description given in its first argument, which must be a string. The format string follows the same rules as the @ANSI{sprintf}. The only differences are that the conversion specifiers and modifiers -@T{*}, @id{h}, @id{L}, @id{l}, and @id{n} are not supported +@id{F}, @id{n}, @T{*}, @id{h}, @id{L}, and @id{l} are not supported and that there is an extra specifier, @id{q}. +Both width and precision, when present, +are limited to two digits. The specifier @id{q} formats booleans, nil, numbers, and strings in a way that the result is a valid constant in Lua source code. @@ -7099,7 +7101,7 @@ may produce the string: "a string with \"quotes\" and \ new line" } -This specifier does not support modifiers (flags, width, length). +This specifier does not support modifiers (flags, width, precision). The conversion specifiers @id{A}, @id{a}, @id{E}, @id{e}, @id{f}, diff --git a/testes/strings.lua b/testes/strings.lua index 61a06a2515..184fa65151 100644 --- a/testes/strings.lua +++ b/testes/strings.lua @@ -202,13 +202,11 @@ assert(string.format("\0%c\0%c%x\0", string.byte("\xe4"), string.byte("b"), 140) "\0\xe4\0b8c\0") assert(string.format('') == "") assert(string.format("%c",34)..string.format("%c",48)..string.format("%c",90)..string.format("%c",100) == - string.format("%c%c%c%c", 34, 48, 90, 100)) + string.format("%1c%-c%-1c%c", 34, 48, 90, 100)) assert(string.format("%s\0 is not \0%s", 'not be', 'be') == 'not be\0 is not \0be') assert(string.format("%%%d %010d", 10, 23) == "%10 0000000023") assert(tonumber(string.format("%f", 10.3)) == 10.3) -x = string.format('"%-50s"', 'a') -assert(#x == 52) -assert(string.sub(x, 1, 4) == '"a ') +assert(string.format('"%-50s"', 'a') == '"a' .. string.rep(' ', 49) .. '"') assert(string.format("-%.20s.20s", string.rep("%", 2000)) == "-"..string.rep("%", 20)..".20s") @@ -237,7 +235,6 @@ end assert(string.format("\0%s\0", "\0\0\1") == "\0\0\0\1\0") checkerror("contains zeros", string.format, "%10s", "\0") -checkerror("cannot have modifiers", string.format, "%10q", "1") -- format x tostring assert(string.format("%s %s", nil, true) == "nil true") @@ -341,6 +338,21 @@ do print("testing 'format %a %A'") end +-- testing some flags (all these results are required by ISO C) +assert(string.format("%#12o", 10) == " 012") +assert(string.format("%#10x", 100) == " 0x64") +assert(string.format("%#-17X", 100) == "0X64 ") +assert(string.format("%013i", -100) == "-000000000100") +assert(string.format("%2.5d", -100) == "-00100") +assert(string.format("%.u", 0) == "") +assert(string.format("%+#014.0f", 100) == "+000000000100.") +assert(string.format("% 1.0E", 100) == " 1E+02") +assert(string.format("%-16c", 97) == "a ") +assert(string.format("%+.3G", 1.5) == "+1.5") +assert(string.format("% .1g", 2^10) == " 1e+03") +assert(string.format("%.0s", "alo") == "") +assert(string.format("%.s", "alo") == "") + -- errors in format local function check (fmt, msg) @@ -348,13 +360,21 @@ local function check (fmt, msg) end local aux = string.rep('0', 600) -check("%100.3d", "too long") +check("%100.3d", "invalid conversion") check("%1"..aux..".3d", "too long") -check("%1.100d", "too long") +check("%1.100d", "invalid conversion") check("%10.1"..aux.."004d", "too long") check("%t", "invalid conversion") -check("%"..aux.."d", "repeated flags") +check("%"..aux.."d", "too long") check("%d %d", "no value") +check("%010c", "invalid conversion") +check("%.10c", "invalid conversion") +check("%0.34s", "invalid conversion") +check("%#i", "invalid conversion") +check("%3.1p", "invalid conversion") +check("%0.s", "invalid conversion") +check("%10q", "cannot have modifiers") +check("%F", "invalid conversion") -- useless and not in C89 assert(load("return 1\n--comment without ending EOL")() == 1) From 2ff34717227b8046b0fdcb96206f11f5e888664e Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 15 Sep 2021 11:18:41 -0300 Subject: [PATCH 0723/1145] Using 'inline' in some functions According to ISO C, "making a function an inline function suggests that calls to the function be as fast as possible." (Not available in C89.) --- lapi.c | 12 +++++++----- ldo.c | 8 ++++---- llimits.h | 14 ++++++++++++++ lvm.c | 12 ++++++------ makefile | 1 + 5 files changed, 32 insertions(+), 15 deletions(-) diff --git a/lapi.c b/lapi.c index 3467891798..071a06f3db 100644 --- a/lapi.c +++ b/lapi.c @@ -86,10 +86,12 @@ static TValue *index2value (lua_State *L, int idx) { } } + + /* ** Convert a valid actual index (not a pseudo-index) to its address. */ -static StkId index2stack (lua_State *L, int idx) { +l_sinline StkId index2stack (lua_State *L, int idx) { CallInfo *ci = L->ci; if (idx > 0) { StkId o = ci->func + idx; @@ -226,7 +228,7 @@ LUA_API void lua_closeslot (lua_State *L, int idx) { ** Note that we move(copy) only the value inside the stack. ** (We do not move additional fields that may exist.) */ -static void reverse (lua_State *L, StkId from, StkId to) { +l_sinline void reverse (lua_State *L, StkId from, StkId to) { for (; from < to; from++, to--) { TValue temp; setobj(L, &temp, s2v(from)); @@ -446,7 +448,7 @@ LUA_API lua_CFunction lua_tocfunction (lua_State *L, int idx) { } -static void *touserdata (const TValue *o) { +l_sinline void *touserdata (const TValue *o) { switch (ttype(o)) { case LUA_TUSERDATA: return getudatamem(uvalue(o)); case LUA_TLIGHTUSERDATA: return pvalue(o); @@ -638,7 +640,7 @@ LUA_API int lua_pushthread (lua_State *L) { */ -static int auxgetstr (lua_State *L, const TValue *t, const char *k) { +l_sinline int auxgetstr (lua_State *L, const TValue *t, const char *k) { const TValue *slot; TString *str = luaS_new(L, k); if (luaV_fastget(L, t, str, slot, luaH_getstr)) { @@ -713,7 +715,7 @@ LUA_API int lua_geti (lua_State *L, int idx, lua_Integer n) { } -static int finishrawget (lua_State *L, const TValue *val) { +l_sinline int finishrawget (lua_State *L, const TValue *val) { if (isempty(val)) /* avoid copying empty items to the stack */ setnilvalue(s2v(L->top)); else diff --git a/ldo.c b/ldo.c index 889cb34b47..88b20f9536 100644 --- a/ldo.c +++ b/ldo.c @@ -407,7 +407,7 @@ StkId luaD_tryfuncTM (lua_State *L, StkId func) { ** expressions, multiple results for tail calls/single parameters) ** separated. */ -static void moveresults (lua_State *L, StkId res, int nres, int wanted) { +l_sinline void moveresults (lua_State *L, StkId res, int nres, int wanted) { StkId firstresult; int i; switch (wanted) { /* handle typical cases separately */ @@ -499,8 +499,8 @@ void luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, int narg1) { } -static CallInfo *prepCallInfo (lua_State *L, StkId func, int nret, - int mask, StkId top) { +l_sinline CallInfo *prepCallInfo (lua_State *L, StkId func, int nret, + int mask, StkId top) { CallInfo *ci = L->ci = next_ci(L); /* new frame */ ci->func = func; ci->nresults = nret; @@ -572,7 +572,7 @@ CallInfo *luaD_precall (lua_State *L, StkId func, int nresults) { ** number of recursive invocations in the C stack) or nyci (the same ** plus increment number of non-yieldable calls). */ -static void ccall (lua_State *L, StkId func, int nResults, int inc) { +l_sinline void ccall (lua_State *L, StkId func, int nResults, int inc) { CallInfo *ci; L->nCcalls += inc; if (l_unlikely(getCcalls(L) >= LUAI_MAXCCALLS)) diff --git a/llimits.h b/llimits.h index 025f1c82cd..6c56ba5a41 100644 --- a/llimits.h +++ b/llimits.h @@ -165,6 +165,20 @@ typedef LUAI_UACINT l_uacInt; #endif +/* +** Inline functions +*/ +#if !defined(LUA_USE_C89) +#define l_inline inline +#elif defined(__GNUC__) +#define l_inline __inline__ +#else +#define l_inline /* empty */ +#endif + +#define l_sinline static l_inline + + /* ** type for virtual-machine instructions; ** must be an unsigned with (at least) 4 bytes (see details in lopcodes.h) diff --git a/lvm.c b/lvm.c index 29a211c677..bdc8e6771c 100644 --- a/lvm.c +++ b/lvm.c @@ -406,7 +406,7 @@ static int l_strcmp (const TString *ls, const TString *rs) { ** from float to int.) ** When 'f' is NaN, comparisons must result in false. */ -static int LTintfloat (lua_Integer i, lua_Number f) { +l_sinline int LTintfloat (lua_Integer i, lua_Number f) { if (l_intfitsf(i)) return luai_numlt(cast_num(i), f); /* compare them as floats */ else { /* i < f <=> i < ceil(f) */ @@ -423,7 +423,7 @@ static int LTintfloat (lua_Integer i, lua_Number f) { ** Check whether integer 'i' is less than or equal to float 'f'. ** See comments on previous function. */ -static int LEintfloat (lua_Integer i, lua_Number f) { +l_sinline int LEintfloat (lua_Integer i, lua_Number f) { if (l_intfitsf(i)) return luai_numle(cast_num(i), f); /* compare them as floats */ else { /* i <= f <=> i <= floor(f) */ @@ -440,7 +440,7 @@ static int LEintfloat (lua_Integer i, lua_Number f) { ** Check whether float 'f' is less than integer 'i'. ** See comments on previous function. */ -static int LTfloatint (lua_Number f, lua_Integer i) { +l_sinline int LTfloatint (lua_Number f, lua_Integer i) { if (l_intfitsf(i)) return luai_numlt(f, cast_num(i)); /* compare them as floats */ else { /* f < i <=> floor(f) < i */ @@ -457,7 +457,7 @@ static int LTfloatint (lua_Number f, lua_Integer i) { ** Check whether float 'f' is less than or equal to integer 'i'. ** See comments on previous function. */ -static int LEfloatint (lua_Number f, lua_Integer i) { +l_sinline int LEfloatint (lua_Number f, lua_Integer i) { if (l_intfitsf(i)) return luai_numle(f, cast_num(i)); /* compare them as floats */ else { /* f <= i <=> ceil(f) <= i */ @@ -473,7 +473,7 @@ static int LEfloatint (lua_Number f, lua_Integer i) { /* ** Return 'l < r', for numbers. */ -static int LTnum (const TValue *l, const TValue *r) { +l_sinline int LTnum (const TValue *l, const TValue *r) { lua_assert(ttisnumber(l) && ttisnumber(r)); if (ttisinteger(l)) { lua_Integer li = ivalue(l); @@ -495,7 +495,7 @@ static int LTnum (const TValue *l, const TValue *r) { /* ** Return 'l <= r', for numbers. */ -static int LEnum (const TValue *l, const TValue *r) { +l_sinline int LEnum (const TValue *l, const TValue *r) { lua_assert(ttisnumber(l) && ttisnumber(r)); if (ttisinteger(l)) { lua_Integer li = ivalue(l); diff --git a/makefile b/makefile index 7cfcbfe198..d46e650cb9 100644 --- a/makefile +++ b/makefile @@ -14,6 +14,7 @@ CWARNSCPP= \ -Wredundant-decls \ -Wdisabled-optimization \ -Wdouble-promotion \ + -Wmissing-declarations \ # the next warnings might be useful sometimes, # but usually they generate too much noise # -Werror \ From deac067ed39a44c001599c0d15de09872496b2aa Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 22 Sep 2021 13:10:39 -0300 Subject: [PATCH 0724/1145] Avoid overflows when incrementing parameters in C Any C function can receive maxinteger as an integer argument, and therefore cannot increment it without some care (e.g., doing unsigned arithmetic as the core does). --- lauxlib.h | 8 ++++++++ lbaselib.c | 3 ++- ltablib.c | 3 ++- lutf8lib.c | 11 ++++------- testes/nextvar.lua | 17 +++++++++++++++++ testes/utf8.lua | 6 ++++++ 6 files changed, 39 insertions(+), 9 deletions(-) diff --git a/lauxlib.h b/lauxlib.h index 72f70e7d9d..6f9695e83e 100644 --- a/lauxlib.h +++ b/lauxlib.h @@ -154,6 +154,14 @@ LUALIB_API void (luaL_requiref) (lua_State *L, const char *modname, #define luaL_loadbuffer(L,s,sz,n) luaL_loadbufferx(L,s,sz,n,NULL) +/* +** Perform arithmetic operations on lua_Integer values with wrap-around +** semantics, as the Lua core does. +*/ +#define luaL_intop(op,v1,v2) \ + ((lua_Integer)((lua_Unsigned)(v1) op (lua_Unsigned)(v2))) + + /* push the value used to represent failure/error */ #define luaL_pushfail(L) lua_pushnil(L) diff --git a/lbaselib.c b/lbaselib.c index fd6687e64e..912c4cc63f 100644 --- a/lbaselib.c +++ b/lbaselib.c @@ -285,7 +285,8 @@ static int luaB_pairs (lua_State *L) { ** Traversal function for 'ipairs' */ static int ipairsaux (lua_State *L) { - lua_Integer i = luaL_checkinteger(L, 2) + 1; + lua_Integer i = luaL_checkinteger(L, 2); + i = luaL_intop(+, i, 1); lua_pushinteger(L, i); return (lua_geti(L, 1, i) == LUA_TNIL) ? 1 : 2; } diff --git a/ltablib.c b/ltablib.c index dbfe250925..868d78fd83 100644 --- a/ltablib.c +++ b/ltablib.c @@ -59,8 +59,9 @@ static void checktab (lua_State *L, int arg, int what) { static int tinsert (lua_State *L) { - lua_Integer e = aux_getn(L, 1, TAB_RW) + 1; /* first empty element */ lua_Integer pos; /* where to insert new element */ + lua_Integer e = aux_getn(L, 1, TAB_RW); + e = luaL_intop(+, e, 1); /* first empty element */ switch (lua_gettop(L)) { case 2: { /* called with only 2 arguments */ pos = e; /* insert new element at the end */ diff --git a/lutf8lib.c b/lutf8lib.c index 901d985f8d..e7bf098f6d 100644 --- a/lutf8lib.c +++ b/lutf8lib.c @@ -224,14 +224,11 @@ static int byteoffset (lua_State *L) { static int iter_aux (lua_State *L, int strict) { size_t len; const char *s = luaL_checklstring(L, 1, &len); - lua_Integer n = lua_tointeger(L, 2) - 1; - if (n < 0) /* first iteration? */ - n = 0; /* start from here */ - else if (n < (lua_Integer)len) { - n++; /* skip current byte */ - while (iscont(s + n)) n++; /* and its continuations */ + lua_Unsigned n = (lua_Unsigned)lua_tointeger(L, 2); + if (n < len) { + while (iscont(s + n)) n++; /* skip continuation bytes */ } - if (n >= (lua_Integer)len) + if (n >= len) /* (also handles original 'n' being negative) */ return 0; /* no more codepoints */ else { utfint code; diff --git a/testes/nextvar.lua b/testes/nextvar.lua index 076f6361bc..9e23e5720d 100644 --- a/testes/nextvar.lua +++ b/testes/nextvar.lua @@ -43,6 +43,14 @@ assert(i == 4) assert(type(ipairs{}) == 'function' and ipairs{} == ipairs{}) +do -- overflow (must wrap-around) + local f = ipairs{} + local k, v = f({[math.mininteger] = 10}, math.maxinteger) + assert(k == math.mininteger and v == 10) + k, v = f({[math.mininteger] = 10}, k) + assert(k == nil) +end + if not T then (Message or print) ('\n >>> testC not active: skipping tests for table sizes <<<\n') @@ -499,6 +507,15 @@ do -- testing table library with metamethods end +do -- testing overflow in table.insert (must wrap-around) + + local t = setmetatable({}, + {__len = function () return math.maxinteger end}) + table.insert(t, 20) + local k, v = next(t) + assert(k == math.mininteger and v == 20) +end + if not T then (Message or print) ('\n >>> testC not active: skipping tests for table library on non-tables <<<\n') diff --git a/testes/utf8.lua b/testes/utf8.lua index 6010d1ad39..461e223c7b 100644 --- a/testes/utf8.lua +++ b/testes/utf8.lua @@ -112,6 +112,12 @@ do end errorcodes("ab\xff") errorcodes("\u{110000}") + + -- calling interation function with invalid arguments + local f = utf8.codes("") + assert(f("", 2) == nil) + assert(f("", -1) == nil) + assert(f("", math.mininteger) == nil) end -- error in initial position for offset From 87a9573b2eb3f1da8e438f92ade994160d930b09 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 11 Oct 2021 13:49:13 -0300 Subject: [PATCH 0725/1145] Documentation Better explanation about the guaranties of multiple assignment in the manual. --- ldebug.c | 2 +- lstrlib.c | 2 +- lvm.c | 2 +- manual/manual.of | 12 ++++++++++-- 4 files changed, 13 insertions(+), 5 deletions(-) diff --git a/ldebug.c b/ldebug.c index 433a875942..dde4669e3a 100644 --- a/ldebug.c +++ b/ldebug.c @@ -64,7 +64,7 @@ static int getbaseline (const Proto *f, int pc, int *basepc) { } else { int i = cast_uint(pc) / MAXIWTHABS - 1; /* get an estimate */ - /* estimate must be a lower bond of the correct base */ + /* estimate must be a lower bound of the correct base */ lua_assert(i < 0 || (i < f->sizeabslineinfo && f->abslineinfo[i].pc <= pc)); while (i + 1 < f->sizeabslineinfo && pc >= f->abslineinfo[i + 1].pc) diff --git a/lstrlib.c b/lstrlib.c index e3b8df0f17..0b4fdbb7b5 100644 --- a/lstrlib.c +++ b/lstrlib.c @@ -1217,7 +1217,7 @@ static const char *get2digits (const char *s) { /* -** Chech whether a conversion specification is valid. When called, +** Check whether a conversion specification is valid. When called, ** first character in 'form' must be '%' and last character must ** be a valid conversion specifier. 'flags' are the accepted flags; ** 'precision' signals whether to accept a precision. diff --git a/lvm.c b/lvm.c index bdc8e6771c..49ed3ddfb9 100644 --- a/lvm.c +++ b/lvm.c @@ -1109,7 +1109,7 @@ void luaV_finishOp (lua_State *L) { #define ProtectNT(exp) (savepc(L), (exp), updatetrap(ci)) /* -** Protect code that can only raise errors. (That is, it cannnot change +** Protect code that can only raise errors. (That is, it cannot change ** the stack or hooks.) */ #define halfProtect(exp) (savestate(L,ci), (exp)) diff --git a/manual/manual.of b/manual/manual.of index ea9a030219..9e0b883558 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -1346,8 +1346,10 @@ then all values returned by that call enter the list of values, before the adjustment (except when the call is enclosed in parentheses; see @See{expressions}). -The assignment statement first evaluates all its expressions -and only then the assignments are performed. +If a variable is both assigned and read +inside a multiple assignment, +Lua ensures all reads get the value of the variable +before the assignment. Thus the code @verbatim{ i = 3 @@ -1367,6 +1369,12 @@ x, y, z = y, z, x } cyclically permutes the values of @id{x}, @id{y}, and @id{z}. +Note that this guarantee covers only accesses +syntactically inside the assignment statement. +If a function or a metamethod called during the assignment +changes the value of a variable, +Lua gives no guarantees about the order of that access. + An assignment to a global name @T{x = val} is equivalent to the assignment @T{_ENV.x = val} @see{globalenv}. From 0e5071b5fbcc244d9f8c4bae82e327ad59bccc3f Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 11 Oct 2021 13:52:26 -0300 Subject: [PATCH 0726/1145] Avoid taking the address of a 'TValue' field That structure can be packed in the future. --- lobject.h | 2 +- ltable.c | 18 +++++++++--------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/lobject.h b/lobject.h index a1b455436d..0e05b3e42e 100644 --- a/lobject.h +++ b/lobject.h @@ -68,7 +68,7 @@ typedef struct TValue { #define val_(o) ((o)->value_) -#define valraw(o) (&val_(o)) +#define valraw(o) (val_(o)) /* raw type tag of a TValue */ diff --git a/ltable.c b/ltable.c index af8783688b..c82286d4d8 100644 --- a/ltable.c +++ b/ltable.c @@ -150,22 +150,22 @@ static int l_hashfloat (lua_Number n) { ** and value in 'vkl') so that we can call it on keys inserted into ** nodes. */ -static Node *mainposition (const Table *t, int ktt, const Value *kvl) { +static Node *mainposition (const Table *t, int ktt, const Value kvl) { switch (withvariant(ktt)) { case LUA_VNUMINT: { - lua_Integer key = ivalueraw(*kvl); + lua_Integer key = ivalueraw(kvl); return hashint(t, key); } case LUA_VNUMFLT: { - lua_Number n = fltvalueraw(*kvl); + lua_Number n = fltvalueraw(kvl); return hashmod(t, l_hashfloat(n)); } case LUA_VSHRSTR: { - TString *ts = tsvalueraw(*kvl); + TString *ts = tsvalueraw(kvl); return hashstr(t, ts); } case LUA_VLNGSTR: { - TString *ts = tsvalueraw(*kvl); + TString *ts = tsvalueraw(kvl); return hashpow2(t, luaS_hashlongstr(ts)); } case LUA_VFALSE: @@ -173,15 +173,15 @@ static Node *mainposition (const Table *t, int ktt, const Value *kvl) { case LUA_VTRUE: return hashboolean(t, 1); case LUA_VLIGHTUSERDATA: { - void *p = pvalueraw(*kvl); + void *p = pvalueraw(kvl); return hashpointer(t, p); } case LUA_VLCF: { - lua_CFunction f = fvalueraw(*kvl); + lua_CFunction f = fvalueraw(kvl); return hashpointer(t, f); } default: { - GCObject *o = gcvalueraw(*kvl); + GCObject *o = gcvalueraw(kvl); return hashpointer(t, o); } } @@ -691,7 +691,7 @@ void luaH_newkey (lua_State *L, Table *t, const TValue *key, TValue *value) { return; } lua_assert(!isdummy(t)); - othern = mainposition(t, keytt(mp), &keyval(mp)); + othern = mainposition(t, keytt(mp), keyval(mp)); if (othern != mp) { /* is colliding node out of its main position? */ /* yes; move colliding node into free position */ while (othern + gnext(othern) != mp) /* find previous */ From 3699446aaf5c7a07af028b1ae43cf52d2d4dda59 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 18 Oct 2021 11:58:40 -0300 Subject: [PATCH 0727/1145] Removed goto's in 'luaD_precall' (plus a detail in lauxlib.h.) --- lauxlib.h | 2 +- ldo.c | 51 +++++++++++++++++++++++++++------------------------ 2 files changed, 28 insertions(+), 25 deletions(-) diff --git a/lauxlib.h b/lauxlib.h index 6f9695e83e..5b977e2a39 100644 --- a/lauxlib.h +++ b/lauxlib.h @@ -102,7 +102,7 @@ LUALIB_API lua_State *(luaL_newstate) (void); LUALIB_API lua_Integer (luaL_len) (lua_State *L, int idx); -LUALIB_API void luaL_addgsub (luaL_Buffer *b, const char *s, +LUALIB_API void (luaL_addgsub) (luaL_Buffer *b, const char *s, const char *p, const char *r); LUALIB_API const char *(luaL_gsub) (lua_State *L, const char *s, const char *p, const char *r); diff --git a/ldo.c b/ldo.c index 88b20f9536..0ac12e744e 100644 --- a/ldo.c +++ b/ldo.c @@ -510,6 +510,30 @@ l_sinline CallInfo *prepCallInfo (lua_State *L, StkId func, int nret, } +/* +** precall for C functions +*/ +l_sinline CallInfo *precallC (lua_State *L, StkId func, int nresults, + lua_CFunction f) { + int n; /* number of returns */ + CallInfo *ci; + checkstackGCp(L, LUA_MINSTACK, func); /* ensure minimum stack size */ + L->ci = ci = prepCallInfo(L, func, nresults, CIST_C, + L->top + LUA_MINSTACK); + lua_assert(ci->top <= L->stack_last); + if (l_unlikely(L->hookmask & LUA_MASKCALL)) { + int narg = cast_int(L->top - func) - 1; + luaD_hook(L, LUA_HOOKCALL, -1, 1, narg); + } + lua_unlock(L); + n = (*f)(L); /* do the actual call */ + lua_lock(L); + api_checknelems(L, n); + luaD_poscall(L, ci, n); + return NULL; +} + + /* ** Prepares the call to a function (C or Lua). For C functions, also do ** the call. The function to be called is at '*func'. The arguments @@ -519,32 +543,11 @@ l_sinline CallInfo *prepCallInfo (lua_State *L, StkId func, int nret, ** original function position. */ CallInfo *luaD_precall (lua_State *L, StkId func, int nresults) { - lua_CFunction f; - retry: switch (ttypetag(s2v(func))) { case LUA_VCCL: /* C closure */ - f = clCvalue(s2v(func))->f; - goto Cfunc; + return precallC(L, func, nresults, clCvalue(s2v(func))->f); case LUA_VLCF: /* light C function */ - f = fvalue(s2v(func)); - Cfunc: { - int n; /* number of returns */ - CallInfo *ci; - checkstackGCp(L, LUA_MINSTACK, func); /* ensure minimum stack size */ - L->ci = ci = prepCallInfo(L, func, nresults, CIST_C, - L->top + LUA_MINSTACK); - lua_assert(ci->top <= L->stack_last); - if (l_unlikely(L->hookmask & LUA_MASKCALL)) { - int narg = cast_int(L->top - func) - 1; - luaD_hook(L, LUA_HOOKCALL, -1, 1, narg); - } - lua_unlock(L); - n = (*f)(L); /* do the actual call */ - lua_lock(L); - api_checknelems(L, n); - luaD_poscall(L, ci, n); - return NULL; - } + return precallC(L, func, nresults, fvalue(s2v(func))); case LUA_VLCL: { /* Lua function */ CallInfo *ci; Proto *p = clLvalue(s2v(func))->p; @@ -561,7 +564,7 @@ CallInfo *luaD_precall (lua_State *L, StkId func, int nresults) { } default: { /* not a function */ func = luaD_tryfuncTM(L, func); /* try to get '__call' metamethod */ - goto retry; /* try again with metamethod */ + return luaD_precall(L, func, nresults); /* try again with metamethod */ } } } From 1fce5bea817de50e055a84c153a975f25bfcf493 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 29 Oct 2021 13:41:24 -0300 Subject: [PATCH 0728/1145] More uniform implementation for tail calls 'luaD_pretailcall' mimics 'luaD_precall', handling call metamethods and calling C functions directly. That makes the code in the interpreter loop simpler. This commit also goes back to emulating the tail call in 'luaD_precall' with a goto, as C compilers may not do proper tail calls and the C stack can overflow much sooner than the Lua stack (which grows as the metamethod is added to it). --- ldo.c | 81 ++++++++++++++++++++++++++++++++++++++--------------------- ldo.h | 2 +- lvm.c | 19 ++++---------- 3 files changed, 58 insertions(+), 44 deletions(-) diff --git a/ldo.c b/ldo.c index 0ac12e744e..d0edc8b4f4 100644 --- a/ldo.c +++ b/ldo.c @@ -475,30 +475,6 @@ void luaD_poscall (lua_State *L, CallInfo *ci, int nres) { #define next_ci(L) (L->ci->next ? L->ci->next : luaE_extendCI(L)) -/* -** Prepare a function for a tail call, building its call info on top -** of the current call info. 'narg1' is the number of arguments plus 1 -** (so that it includes the function itself). -*/ -void luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, int narg1) { - Proto *p = clLvalue(s2v(func))->p; - int fsize = p->maxstacksize; /* frame size */ - int nfixparams = p->numparams; - int i; - for (i = 0; i < narg1; i++) /* move down function and arguments */ - setobjs2s(L, ci->func + i, func + i); - checkstackGC(L, fsize); - func = ci->func; /* moved-down function */ - for (; narg1 <= nfixparams; narg1++) - setnilvalue(s2v(func + narg1)); /* complete missing arguments */ - ci->top = func + 1 + fsize; /* top for new function */ - lua_assert(ci->top <= L->stack_last); - ci->u.l.savedpc = p->code; /* starting point */ - ci->callstatus |= CIST_TAIL; - L->top = func + narg1; /* set top */ -} - - l_sinline CallInfo *prepCallInfo (lua_State *L, StkId func, int nret, int mask, StkId top) { CallInfo *ci = L->ci = next_ci(L); /* new frame */ @@ -513,7 +489,7 @@ l_sinline CallInfo *prepCallInfo (lua_State *L, StkId func, int nret, /* ** precall for C functions */ -l_sinline CallInfo *precallC (lua_State *L, StkId func, int nresults, +l_sinline int precallC (lua_State *L, StkId func, int nresults, lua_CFunction f) { int n; /* number of returns */ CallInfo *ci; @@ -530,7 +506,50 @@ l_sinline CallInfo *precallC (lua_State *L, StkId func, int nresults, lua_lock(L); api_checknelems(L, n); luaD_poscall(L, ci, n); - return NULL; + return n; +} + + +/* +** Prepare a function for a tail call, building its call info on top +** of the current call info. 'narg1' is the number of arguments plus 1 +** (so that it includes the function itself). Return the number of +** results, if it was a C function, or -1 for a Lua function. +*/ +int luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, + int narg1, int delta) { + retry: + switch (ttypetag(s2v(func))) { + case LUA_VCCL: /* C closure */ + return precallC(L, func, LUA_MULTRET, clCvalue(s2v(func))->f); + case LUA_VLCF: /* light C function */ + return precallC(L, func, LUA_MULTRET, fvalue(s2v(func))); + case LUA_VLCL: { /* Lua function */ + Proto *p = clLvalue(s2v(func))->p; + int fsize = p->maxstacksize; /* frame size */ + int nfixparams = p->numparams; + int i; + ci->func -= delta; /* restore 'func' (if vararg) */ + for (i = 0; i < narg1; i++) /* move down function and arguments */ + setobjs2s(L, ci->func + i, func + i); + checkstackGC(L, fsize); + func = ci->func; /* moved-down function */ + for (; narg1 <= nfixparams; narg1++) + setnilvalue(s2v(func + narg1)); /* complete missing arguments */ + ci->top = func + 1 + fsize; /* top for new function */ + lua_assert(ci->top <= L->stack_last); + ci->u.l.savedpc = p->code; /* starting point */ + ci->callstatus |= CIST_TAIL; + L->top = func + narg1; /* set top */ + return -1; + } + default: { /* not a function */ + func = luaD_tryfuncTM(L, func); /* try to get '__call' metamethod */ + /* return luaD_pretailcall(L, ci, func, narg1 + 1, delta); */ + narg1++; + goto retry; /* try again */ + } + } } @@ -543,11 +562,14 @@ l_sinline CallInfo *precallC (lua_State *L, StkId func, int nresults, ** original function position. */ CallInfo *luaD_precall (lua_State *L, StkId func, int nresults) { + retry: switch (ttypetag(s2v(func))) { case LUA_VCCL: /* C closure */ - return precallC(L, func, nresults, clCvalue(s2v(func))->f); + precallC(L, func, nresults, clCvalue(s2v(func))->f); + return NULL; case LUA_VLCF: /* light C function */ - return precallC(L, func, nresults, fvalue(s2v(func))); + precallC(L, func, nresults, fvalue(s2v(func))); + return NULL; case LUA_VLCL: { /* Lua function */ CallInfo *ci; Proto *p = clLvalue(s2v(func))->p; @@ -564,7 +586,8 @@ CallInfo *luaD_precall (lua_State *L, StkId func, int nresults) { } default: { /* not a function */ func = luaD_tryfuncTM(L, func); /* try to get '__call' metamethod */ - return luaD_precall(L, func, nresults); /* try again with metamethod */ + /* return luaD_precall(L, func, nresults); */ + goto retry; /* try again with metamethod */ } } } diff --git a/ldo.h b/ldo.h index 9fb772fe3a..911e67f660 100644 --- a/ldo.h +++ b/ldo.h @@ -58,7 +58,7 @@ LUAI_FUNC int luaD_protectedparser (lua_State *L, ZIO *z, const char *name, LUAI_FUNC void luaD_hook (lua_State *L, int event, int line, int fTransfer, int nTransfer); LUAI_FUNC void luaD_hookcall (lua_State *L, CallInfo *ci); -LUAI_FUNC void luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, int n); +LUAI_FUNC int luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, int narg1, int delta); LUAI_FUNC CallInfo *luaD_precall (lua_State *L, StkId func, int nResults); LUAI_FUNC void luaD_call (lua_State *L, StkId func, int nResults); LUAI_FUNC void luaD_callnoyield (lua_State *L, StkId func, int nResults); diff --git a/lvm.c b/lvm.c index 49ed3ddfb9..2ec3440031 100644 --- a/lvm.c +++ b/lvm.c @@ -1643,6 +1643,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { } vmcase(OP_TAILCALL) { int b = GETARG_B(i); /* number of arguments + 1 (function) */ + int n; /* number of results when calling a C function */ int nparams1 = GETARG_C(i); /* delta is virtual 'func' - real 'func' (vararg functions) */ int delta = (nparams1) ? ci->u.l.nextraargs + nparams1 : 0; @@ -1656,24 +1657,14 @@ void luaV_execute (lua_State *L, CallInfo *ci) { lua_assert(L->tbclist < base); /* no pending tbc variables */ lua_assert(base == ci->func + 1); } - while (!ttisfunction(s2v(ra))) { /* not a function? */ - ra = luaD_tryfuncTM(L, ra); /* try '__call' metamethod */ - b++; /* there is now one extra argument */ - } - if (!ttisLclosure(s2v(ra))) { /* C function? */ - luaD_precall(L, ra, LUA_MULTRET); /* call it */ - updatetrap(ci); - updatestack(ci); /* stack may have been relocated */ + if ((n = luaD_pretailcall(L, ci, ra, b, delta)) < 0) /* Lua function? */ + goto startfunc; /* execute the callee */ + else { /* C function? */ ci->func -= delta; /* restore 'func' (if vararg) */ - luaD_poscall(L, ci, cast_int(L->top - ra)); /* finish caller */ + luaD_poscall(L, ci, n); /* finish caller */ updatetrap(ci); /* 'luaD_poscall' can change hooks */ goto ret; /* caller returns after the tail call */ } - else { /* Lua function */ - ci->func -= delta; /* restore 'func' (if vararg) */ - luaD_pretailcall(L, ci, ra, b); /* prepare call frame */ - goto startfunc; /* execute the callee */ - } } vmcase(OP_RETURN) { int n = GETARG_B(i) - 1; /* number of results */ From 74d99057a5146755e737c479850f87fd0e3b6868 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 3 Nov 2021 15:04:18 -0300 Subject: [PATCH 0729/1145] Bug: C stack overflow with coroutines 'coroutine.resume' did not increment counter of C calls when continuing execution after a protected error (that is, while running 'precover'). --- ldo.c | 6 ++++-- testes/cstack.lua | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/ldo.c b/ldo.c index d0edc8b4f4..66f8903644 100644 --- a/ldo.c +++ b/ldo.c @@ -759,11 +759,10 @@ static void resume (lua_State *L, void *ud) { StkId firstArg = L->top - n; /* first argument */ CallInfo *ci = L->ci; if (L->status == LUA_OK) /* starting a coroutine? */ - ccall(L, firstArg - 1, LUA_MULTRET, 1); /* just call its body */ + ccall(L, firstArg - 1, LUA_MULTRET, 0); /* just call its body */ else { /* resuming from previous yield */ lua_assert(L->status == LUA_YIELD); L->status = LUA_OK; /* mark that it is running (again) */ - luaE_incCstack(L); /* control the C stack */ if (isLua(ci)) { /* yielded inside a hook? */ L->top = firstArg; /* discard arguments */ luaV_execute(L, ci); /* just continue running Lua code */ @@ -814,6 +813,9 @@ LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs, else if (L->status != LUA_YIELD) /* ended with errors? */ return resume_error(L, "cannot resume dead coroutine", nargs); L->nCcalls = (from) ? getCcalls(from) : 0; + if (getCcalls(L) >= LUAI_MAXCCALLS) + return resume_error(L, "C stack overflow", nargs); + L->nCcalls++; luai_userstateresume(L, nargs); api_checknelems(L, (L->status == LUA_OK) ? nargs + 1 : nargs); status = luaD_rawrunprotected(L, resume, &nargs); diff --git a/testes/cstack.lua b/testes/cstack.lua index 213d15d470..ca76c8729c 100644 --- a/testes/cstack.lua +++ b/testes/cstack.lua @@ -103,6 +103,20 @@ do end +do -- bug in 5.4.2 + print("nesting coroutines running after recoverable errors") + local count = 0 + local function foo() + count = count + 1 + pcall(1) -- create an error + -- running now inside 'precover' ("protected recover") + coroutine.wrap(foo)() -- call another coroutine + end + checkerror("C stack overflow", foo) + print("final count: ", count) +end + + if T then print("testing stack recovery") local N = 0 -- trace number of calls From bfbff3703edae789fa5efa9bf174f8e7cff4ded8 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 8 Nov 2021 11:55:25 -0300 Subject: [PATCH 0730/1145] Bug: Wrong status in coroutine during reset When closing variables during 'coroutine.close' or 'lua_resetthread', the status of a coroutine must be set to LUA_OK; a coroutine should not run with any other status. (See assertion in 'lua_callk'.) After the reset, the status should be kept as normal, as any error was already reported. --- lcorolib.c | 4 ++-- lstate.c | 4 ++-- testes/coroutine.lua | 44 +++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 47 insertions(+), 5 deletions(-) diff --git a/lcorolib.c b/lcorolib.c index fedbebec39..785a1e81aa 100644 --- a/lcorolib.c +++ b/lcorolib.c @@ -78,7 +78,7 @@ static int luaB_auxwrap (lua_State *L) { if (stat != LUA_OK && stat != LUA_YIELD) { /* error in the coroutine? */ stat = lua_resetthread(co); /* close its tbc variables */ lua_assert(stat != LUA_OK); - lua_xmove(co, L, 1); /* copy error message */ + lua_xmove(co, L, 1); /* move error message to the caller */ } if (stat != LUA_ERRMEM && /* not a memory error and ... */ lua_type(L, -1) == LUA_TSTRING) { /* ... error object is a string? */ @@ -179,7 +179,7 @@ static int luaB_close (lua_State *L) { } else { lua_pushboolean(L, 0); - lua_xmove(co, L, 1); /* copy error message */ + lua_xmove(co, L, 1); /* move error message */ return 2; } } diff --git a/lstate.c b/lstate.c index bfc590262b..5cb0847c8c 100644 --- a/lstate.c +++ b/lstate.c @@ -166,7 +166,7 @@ void luaE_checkcstack (lua_State *L) { if (getCcalls(L) == LUAI_MAXCCALLS) luaG_runerror(L, "C stack overflow"); else if (getCcalls(L) >= (LUAI_MAXCCALLS / 10 * 11)) - luaD_throw(L, LUA_ERRERR); /* error while handing stack error */ + luaD_throw(L, LUA_ERRERR); /* error while handling stack error */ } @@ -330,13 +330,13 @@ int luaE_resetthread (lua_State *L, int status) { ci->callstatus = CIST_C; if (status == LUA_YIELD) status = LUA_OK; + L->status = LUA_OK; /* so it can run __close metamethods */ status = luaD_closeprotected(L, 1, status); if (status != LUA_OK) /* errors? */ luaD_seterrorobj(L, status, L->stack + 1); else L->top = L->stack + 1; ci->top = L->top + LUA_MINSTACK; - L->status = cast_byte(status); luaD_reallocstack(L, cast_int(ci->top - L->stack), 0); return status; } diff --git a/testes/coroutine.lua b/testes/coroutine.lua index 461e03770f..76c9d6e635 100644 --- a/testes/coroutine.lua +++ b/testes/coroutine.lua @@ -136,6 +136,10 @@ do assert(coroutine.status(co) == "dead") local st, msg = coroutine.close(co) assert(st and msg == nil) + -- also ok to close it again + st, msg = coroutine.close(co) + assert(st and msg == nil) + -- cannot close the running coroutine local st, msg = pcall(coroutine.close, coroutine.running()) @@ -149,6 +153,22 @@ do assert(not st and string.find(msg, "normal")) end))() + -- cannot close a coroutine while closing it + do + local co + co = coroutine.create( + function() + local x = func2close(function() + coroutine.close(co) -- try to close it again + end) + coroutine.yield(20) + end) + local st, msg = coroutine.resume(co) + assert(st and msg == 20) + st, msg = coroutine.close(co) + assert(not st and string.find(msg, "running coroutine")) + end + -- to-be-closed variables in coroutines local X @@ -158,6 +178,9 @@ do assert(not st and msg == 100) st, msg = coroutine.close(co) assert(not st and msg == 100) + -- after closing, no more errors + st, msg = coroutine.close(co) + assert(st and msg == nil) co = coroutine.create(function () local x = func2close(function (self, err) @@ -189,6 +212,9 @@ do local st, msg = coroutine.close(co) assert(st == false and coroutine.status(co) == "dead" and msg == 200) assert(x == 200) + -- after closing, no more errors + st, msg = coroutine.close(co) + assert(st and msg == nil) end do @@ -419,7 +445,7 @@ do local X = false A = coroutine.wrap(function() - local _ = setmetatable({}, {__close = function () X = true end}) + local _ = func2close(function () X = true end) return pcall(A, 1) end) st, res = A() @@ -427,6 +453,22 @@ do end +-- bug in 5.4.1 +do + -- coroutine ran close metamethods with invalid status during a + -- reset. + local co + co = coroutine.wrap(function() + local x = func2close(function() return pcall(co) end) + error(111) + end) + local st, errobj = pcall(co) + assert(not st and errobj == 111) + st, errobj = pcall(co) + assert(not st and string.find(errobj, "dead coroutine")) +end + + -- attempt to resume 'normal' coroutine local co1, co2 co1 = coroutine.create(function () return co2() end) From e8deac5a41ffd644aaa78fda6d4bd5caa72cb077 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 10 Nov 2021 15:07:14 -0300 Subject: [PATCH 0731/1145] Avoid OP_VARARGPREP for active lines when building the table 'activelines' for a vararg function, this first instruction does not make the first line active. --- ldebug.c | 9 ++++++++- testes/db.lua | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/ldebug.c b/ldebug.c index dde4669e3a..30a28828d2 100644 --- a/ldebug.c +++ b/ldebug.c @@ -301,7 +301,14 @@ static void collectvalidlines (lua_State *L, Closure *f) { sethvalue2s(L, L->top, t); /* push it on stack */ api_incr_top(L); setbtvalue(&v); /* boolean 'true' to be the value of all indices */ - for (i = 0; i < p->sizelineinfo; i++) { /* for all instructions */ + if (!p->is_vararg) /* regular function? */ + i = 0; /* consider all instructions */ + else { /* vararg function */ + lua_assert(p->code[0] == OP_VARARGPREP); + currentline = nextline(p, currentline, 0); + i = 1; /* skip first instruction (OP_VARARGPREP) */ + } + for (; i < p->sizelineinfo; i++) { /* for each instruction */ currentline = nextline(p, currentline, i); /* get its line */ luaH_setint(L, t, currentline, &v); /* table[line] = true */ } diff --git a/testes/db.lua b/testes/db.lua index d64952d9e2..11dfd26c28 100644 --- a/testes/db.lua +++ b/testes/db.lua @@ -195,6 +195,49 @@ do -- testing line info/trace with large gaps in source end end + +do -- testing active lines + local function checkactivelines (f, lines) + local t = debug.getinfo(f, "SL") + for _, l in pairs(lines) do + l = l + t.linedefined + assert(t.activelines[l]) + t.activelines[l] = undef + end + assert(next(t.activelines) == nil) -- no extra lines + end + + checkactivelines(function (...) -- vararg function + -- 1st line is empty + -- 2nd line is empty + -- 3th line is empty + local a = 20 + -- 5th line is empty + local b = 30 + -- 7th line is empty + end, {4, 6, 8}) + + checkactivelines(function (a) + -- 1st line is empty + -- 2nd line is empty + local a = 20 + local b = 30 + -- 5th line is empty + end, {3, 4, 6}) + + checkactivelines(function (...) end, {0}) + + checkactivelines(function (a, b) + end, {1}) + + for _, n in pairs{0, 1, 2, 10, 50, 100, 1000, 10000} do + checkactivelines( + load(string.format("%s return 1", string.rep("\n", n))), + {n + 1}) + end + +end + print'+' -- invalid levels in [gs]etlocal From 6b3e116d44eed387aa93126c48eae8a64b38bfc2 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 16 Nov 2021 14:35:06 -0300 Subject: [PATCH 0732/1145] Corrected bug in 'luaD_tryfuncTM' The pointer to the metamethod can be invalidated by a finalizer that can run during a GC in 'checkstackGCp'. (This commit also fixes a detail in the manual.) Bug introduced in commit 91673a8ec. --- ldo.c | 3 ++- manual/manual.of | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/ldo.c b/ldo.c index 66f8903644..f282a773ea 100644 --- a/ldo.c +++ b/ldo.c @@ -388,9 +388,10 @@ static void rethook (lua_State *L, CallInfo *ci, int nres) { ** an error if there is no '__call' metafield. */ StkId luaD_tryfuncTM (lua_State *L, StkId func) { - const TValue *tm = luaT_gettmbyobj(L, s2v(func), TM_CALL); + const TValue *tm; StkId p; checkstackGCp(L, 1, func); /* space for metamethod */ + tm = luaT_gettmbyobj(L, s2v(func), TM_CALL); /* (after previous GC) */ if (l_unlikely(ttisnil(tm))) luaG_callerror(L, s2v(func)); /* nothing to call */ for (p = L->top; p > func; p--) /* open space for metamethod */ diff --git a/manual/manual.of b/manual/manual.of index 9e0b883558..c9e62b49c5 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -6247,7 +6247,7 @@ to its caller. } @LibEntry{error (message [, level])| -Raises an error @see{error} with @{message} as the error object. +Raises an error @see{error} with @id{message} as the error object. This function never returns. Usually, @id{error} adds some information about the error position From 48835c76c8df62fab4827a9835b351718d20df4b Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 25 Nov 2021 10:11:05 -0300 Subject: [PATCH 0733/1145] Wrong assert in 'collectvalidlines' --- ldebug.c | 2 +- testes/db.lua | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/ldebug.c b/ldebug.c index 30a28828d2..dc5f78c656 100644 --- a/ldebug.c +++ b/ldebug.c @@ -304,7 +304,7 @@ static void collectvalidlines (lua_State *L, Closure *f) { if (!p->is_vararg) /* regular function? */ i = 0; /* consider all instructions */ else { /* vararg function */ - lua_assert(p->code[0] == OP_VARARGPREP); + lua_assert(GET_OPCODE(p->code[0]) == OP_VARARGPREP); currentline = nextline(p, currentline, 0); i = 1; /* skip first instruction (OP_VARARGPREP) */ } diff --git a/testes/db.lua b/testes/db.lua index 11dfd26c28..e06997248e 100644 --- a/testes/db.lua +++ b/testes/db.lua @@ -225,7 +225,7 @@ do -- testing active lines -- 5th line is empty end, {3, 4, 6}) - checkactivelines(function (...) end, {0}) + checkactivelines(function (a, b, ...) end, {0}) checkactivelines(function (a, b) end, {1}) From ad3942adba574c9d008c99ce2785a5af19d146bf Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 25 Nov 2021 11:07:17 -0300 Subject: [PATCH 0734/1145] Main 'mainposition' replaced by 'mainpositionTV' Handle values in table keys as the special cases they are, and not the other way around. --- ltable.c | 35 ++++++++++++++++------------------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/ltable.c b/ltable.c index c82286d4d8..1b1cd2415b 100644 --- a/ltable.c +++ b/ltable.c @@ -146,26 +146,24 @@ static int l_hashfloat (lua_Number n) { /* ** returns the 'main' position of an element in a table (that is, -** the index of its hash value). The key comes broken (tag in 'ktt' -** and value in 'vkl') so that we can call it on keys inserted into -** nodes. +** the index of its hash value). */ -static Node *mainposition (const Table *t, int ktt, const Value kvl) { - switch (withvariant(ktt)) { +static Node *mainpositionTV (const Table *t, const TValue *key) { + switch (ttypetag(key)) { case LUA_VNUMINT: { - lua_Integer key = ivalueraw(kvl); - return hashint(t, key); + lua_Integer i = ivalue(key); + return hashint(t, i); } case LUA_VNUMFLT: { - lua_Number n = fltvalueraw(kvl); + lua_Number n = fltvalue(key); return hashmod(t, l_hashfloat(n)); } case LUA_VSHRSTR: { - TString *ts = tsvalueraw(kvl); + TString *ts = tsvalue(key); return hashstr(t, ts); } case LUA_VLNGSTR: { - TString *ts = tsvalueraw(kvl); + TString *ts = tsvalue(key); return hashpow2(t, luaS_hashlongstr(ts)); } case LUA_VFALSE: @@ -173,26 +171,25 @@ static Node *mainposition (const Table *t, int ktt, const Value kvl) { case LUA_VTRUE: return hashboolean(t, 1); case LUA_VLIGHTUSERDATA: { - void *p = pvalueraw(kvl); + void *p = pvalue(key); return hashpointer(t, p); } case LUA_VLCF: { - lua_CFunction f = fvalueraw(kvl); + lua_CFunction f = fvalue(key); return hashpointer(t, f); } default: { - GCObject *o = gcvalueraw(kvl); + GCObject *o = gcvalue(key); return hashpointer(t, o); } } } -/* -** Returns the main position of an element given as a 'TValue' -*/ -static Node *mainpositionTV (const Table *t, const TValue *key) { - return mainposition(t, rawtt(key), valraw(key)); +l_sinline Node *mainpositionfromnode (const Table *t, Node *nd) { + TValue key; + getnodekey(cast(lua_State *, NULL), &key, nd); + return mainpositionTV(t, &key); } @@ -691,7 +688,7 @@ void luaH_newkey (lua_State *L, Table *t, const TValue *key, TValue *value) { return; } lua_assert(!isdummy(t)); - othern = mainposition(t, keytt(mp), keyval(mp)); + othern = mainpositionfromnode(t, mp); if (othern != mp) { /* is colliding node out of its main position? */ /* yes; move colliding node into free position */ while (othern + gnext(othern) != mp) /* find previous */ From 1de95e97ef65632a88e08b6184bd9d1ceba7ec2f Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 10 Dec 2021 10:53:54 -0300 Subject: [PATCH 0735/1145] Bug: Lua stack still active when closing a state --- lstate.c | 1 + 1 file changed, 1 insertion(+) diff --git a/lstate.c b/lstate.c index 5cb0847c8c..547a7a0148 100644 --- a/lstate.c +++ b/lstate.c @@ -271,6 +271,7 @@ static void close_state (lua_State *L) { if (!completestate(g)) /* closing a partially built state? */ luaC_freeallobjects(L); /* just collect its objects */ else { /* closing a fully built state */ + L->ci = &L->base_ci; /* unwind CallInfo list */ luaD_closeprotected(L, 1, LUA_OK); /* close all upvalues */ luaC_freeallobjects(L); /* collect all objects */ luai_userstateclose(L); From 0bfc572e51d9035a615ef6e9523f736c9ffa8e57 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 13 Dec 2021 10:41:17 -0300 Subject: [PATCH 0736/1145] Bug: GC is not reentrant As the GC is not reentrant, finalizers should not be able to invoke it. --- lapi.c | 17 +++++++++-------- lbaselib.c | 19 +++++++++++++++++-- lgc.c | 11 +++++++---- lgc.h | 9 +++++++++ lstate.c | 4 ++-- lstate.h | 2 +- manual/manual.of | 11 ++++++----- testes/api.lua | 5 ++--- testes/gc.lua | 6 ++++-- 9 files changed, 57 insertions(+), 27 deletions(-) diff --git a/lapi.c b/lapi.c index 071a06f3db..3585ac436a 100644 --- a/lapi.c +++ b/lapi.c @@ -1136,18 +1136,19 @@ LUA_API int lua_status (lua_State *L) { LUA_API int lua_gc (lua_State *L, int what, ...) { va_list argp; int res = 0; - global_State *g; + global_State *g = G(L); + if (g->gcstp & GCSTPGC) /* internal stop? */ + return -1; /* all options are invalid when stopped */ lua_lock(L); - g = G(L); va_start(argp, what); switch (what) { case LUA_GCSTOP: { - g->gcrunning = 0; + g->gcstp = GCSTPUSR; /* stopeed by the user */ break; } case LUA_GCRESTART: { luaE_setdebt(g, 0); - g->gcrunning = 1; + g->gcstp = 0; /* (GCSTPGC must be already zero here) */ break; } case LUA_GCCOLLECT: { @@ -1166,8 +1167,8 @@ LUA_API int lua_gc (lua_State *L, int what, ...) { case LUA_GCSTEP: { int data = va_arg(argp, int); l_mem debt = 1; /* =1 to signal that it did an actual step */ - lu_byte oldrunning = g->gcrunning; - g->gcrunning = 1; /* allow GC to run */ + lu_byte oldstp = g->gcstp; + g->gcstp = 0; /* allow GC to run (GCSTPGC must be zero here) */ if (data == 0) { luaE_setdebt(g, 0); /* do a basic step */ luaC_step(L); @@ -1177,7 +1178,7 @@ LUA_API int lua_gc (lua_State *L, int what, ...) { luaE_setdebt(g, debt); luaC_checkGC(L); } - g->gcrunning = oldrunning; /* restore previous state */ + g->gcstp = oldstp; /* restore previous state */ if (debt > 0 && g->gcstate == GCSpause) /* end of cycle? */ res = 1; /* signal it */ break; @@ -1195,7 +1196,7 @@ LUA_API int lua_gc (lua_State *L, int what, ...) { break; } case LUA_GCISRUNNING: { - res = g->gcrunning; + res = gcrunning(g); break; } case LUA_GCGEN: { diff --git a/lbaselib.c b/lbaselib.c index 912c4cc63f..1d60c9dede 100644 --- a/lbaselib.c +++ b/lbaselib.c @@ -182,12 +182,20 @@ static int luaB_rawset (lua_State *L) { static int pushmode (lua_State *L, int oldmode) { - lua_pushstring(L, (oldmode == LUA_GCINC) ? "incremental" - : "generational"); + if (oldmode == -1) + luaL_pushfail(L); /* invalid call to 'lua_gc' */ + else + lua_pushstring(L, (oldmode == LUA_GCINC) ? "incremental" + : "generational"); return 1; } +/* +** check whether call to 'lua_gc' was valid (not inside a finalizer) +*/ +#define checkvalres(res) { if (res == -1) break; } + static int luaB_collectgarbage (lua_State *L) { static const char *const opts[] = {"stop", "restart", "collect", "count", "step", "setpause", "setstepmul", @@ -200,12 +208,14 @@ static int luaB_collectgarbage (lua_State *L) { case LUA_GCCOUNT: { int k = lua_gc(L, o); int b = lua_gc(L, LUA_GCCOUNTB); + checkvalres(k); lua_pushnumber(L, (lua_Number)k + ((lua_Number)b/1024)); return 1; } case LUA_GCSTEP: { int step = (int)luaL_optinteger(L, 2, 0); int res = lua_gc(L, o, step); + checkvalres(res); lua_pushboolean(L, res); return 1; } @@ -213,11 +223,13 @@ static int luaB_collectgarbage (lua_State *L) { case LUA_GCSETSTEPMUL: { int p = (int)luaL_optinteger(L, 2, 0); int previous = lua_gc(L, o, p); + checkvalres(previous); lua_pushinteger(L, previous); return 1; } case LUA_GCISRUNNING: { int res = lua_gc(L, o); + checkvalres(res); lua_pushboolean(L, res); return 1; } @@ -234,10 +246,13 @@ static int luaB_collectgarbage (lua_State *L) { } default: { int res = lua_gc(L, o); + checkvalres(res); lua_pushinteger(L, res); return 1; } } + luaL_pushfail(L); /* invalid call (inside a finalizer) */ + return 1; } diff --git a/lgc.c b/lgc.c index b360eed008..7d0b5e4f7b 100644 --- a/lgc.c +++ b/lgc.c @@ -906,16 +906,16 @@ static void GCTM (lua_State *L) { if (!notm(tm)) { /* is there a finalizer? */ int status; lu_byte oldah = L->allowhook; - int running = g->gcrunning; + int oldgcstp = g->gcstp; + g->gcstp = GCSTPGC; /* avoid GC steps */ L->allowhook = 0; /* stop debug hooks during GC metamethod */ - g->gcrunning = 0; /* avoid GC steps */ setobj2s(L, L->top++, tm); /* push finalizer... */ setobj2s(L, L->top++, &v); /* ... and its argument */ L->ci->callstatus |= CIST_FIN; /* will run a finalizer */ status = luaD_pcall(L, dothecall, NULL, savestack(L, L->top - 2), 0); L->ci->callstatus &= ~CIST_FIN; /* not running a finalizer anymore */ L->allowhook = oldah; /* restore hooks */ - g->gcrunning = running; /* restore state */ + g->gcstp = oldgcstp; /* restore state */ if (l_unlikely(status != LUA_OK)) { /* error while running __gc? */ luaE_warnerror(L, "__gc metamethod"); L->top--; /* pops error object */ @@ -1502,9 +1502,11 @@ static void deletelist (lua_State *L, GCObject *p, GCObject *limit) { */ void luaC_freeallobjects (lua_State *L) { global_State *g = G(L); + g->gcstp = GCSTPGC; luaC_changemode(L, KGC_INC); separatetobefnz(g, 1); /* separate all objects with finalizers */ lua_assert(g->finobj == NULL); + g->gcstp = 0; callallpendingfinalizers(L); deletelist(L, g->allgc, obj2gco(g->mainthread)); deletelist(L, g->finobj, NULL); @@ -1647,6 +1649,7 @@ void luaC_runtilstate (lua_State *L, int statesmask) { } + /* ** Performs a basic incremental step. The debt and step size are ** converted from bytes to "units of work"; then the function loops @@ -1678,7 +1681,7 @@ static void incstep (lua_State *L, global_State *g) { void luaC_step (lua_State *L) { global_State *g = G(L); lua_assert(!g->gcemergency); - if (g->gcrunning) { /* running? */ + if (gcrunning(g)) { /* running? */ if(isdecGCmodegen(g)) genstep(L, g); else diff --git a/lgc.h b/lgc.h index 073e2a4029..024a4328e8 100644 --- a/lgc.h +++ b/lgc.h @@ -148,6 +148,15 @@ */ #define isdecGCmodegen(g) (g->gckind == KGC_GEN || g->lastatomic != 0) + +/* +** Control when GC is running: +*/ +#define GCSTPUSR 1 /* bit true when GC stopped by user */ +#define GCSTPGC 2 /* bit true when GC stopped by itself */ +#define gcrunning(g) ((g)->gcstp == 0) + + /* ** Does one step of collection when debt becomes positive. 'pre'/'pos' ** allows some adjustments to be done only when needed. macro diff --git a/lstate.c b/lstate.c index 547a7a0148..1ffe1a0f71 100644 --- a/lstate.c +++ b/lstate.c @@ -236,7 +236,7 @@ static void f_luaopen (lua_State *L, void *ud) { luaS_init(L); luaT_init(L); luaX_init(L); - g->gcrunning = 1; /* allow gc */ + g->gcstp = 0; /* allow gc */ setnilvalue(&g->nilvalue); /* now state is complete */ luai_userstateopen(L); } @@ -373,7 +373,7 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { g->ud_warn = NULL; g->mainthread = L; g->seed = luai_makeseed(L); - g->gcrunning = 0; /* no GC while building state */ + g->gcstp = GCSTPGC; /* no GC while building state */ g->strt.size = g->strt.nuse = 0; g->strt.hash = NULL; setnilvalue(&g->l_registry); diff --git a/lstate.h b/lstate.h index 44cf939cb1..7886d8914b 100644 --- a/lstate.h +++ b/lstate.h @@ -263,7 +263,7 @@ typedef struct global_State { lu_byte gcstopem; /* stops emergency collections */ lu_byte genminormul; /* control for minor generational collections */ lu_byte genmajormul; /* control for major generational collections */ - lu_byte gcrunning; /* true if GC is running */ + lu_byte gcstp; /* control whether GC is running */ lu_byte gcemergency; /* true if this is an emergency collection */ lu_byte gcpause; /* size of pause between successive GCs */ lu_byte gcstepmul; /* GC "speed" */ diff --git a/manual/manual.of b/manual/manual.of index c9e62b49c5..c660215c0e 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -787,11 +787,8 @@ following the reverse order that they were marked. If any finalizer marks objects for collection during that phase, these marks have no effect. -Finalizers cannot yield. -Except for that, they can do anything, -such as raise errors, create new objects, -or even run the garbage collector. -However, because they can run in unpredictable times, +Finalizers cannot yield nor run the garbage collector. +Because they can run in unpredictable times, it is good practice to restrict each finalizer to the minimum necessary to properly release its associated resource. @@ -3276,6 +3273,8 @@ Returns the previous mode (@id{LUA_GCGEN} or @id{LUA_GCINC}). For more details about these options, see @Lid{collectgarbage}. +This function should not be called by a finalizer. + } @APIEntry{lua_Alloc lua_getallocf (lua_State *L, void **ud);| @@ -6233,6 +6232,8 @@ A zero means to not change that value. See @See{GC} for more details about garbage collection and some of these options. +This function should not be called by a finalizer. + } @LibEntry{dofile ([filename])| diff --git a/testes/api.lua b/testes/api.lua index c1bcb4b7b4..bd85a923c8 100644 --- a/testes/api.lua +++ b/testes/api.lua @@ -804,15 +804,14 @@ F = function (x) d = nil assert(debug.getmetatable(x).__gc == F) assert(load("table.insert({}, {})"))() -- create more garbage - collectgarbage() -- force a GC during GC - assert(debug.getmetatable(x).__gc == F) -- previous GC did not mess this? + assert(not collectgarbage()) -- GC during GC (no op) local dummy = {} -- create more garbage during GC if A ~= nil then assert(type(A) == "userdata") assert(T.udataval(A) == B) debug.getmetatable(A) -- just access it end - A = x -- ressucita userdata + A = x -- ressurect userdata B = udval return 1,2,3 end diff --git a/testes/gc.lua b/testes/gc.lua index 2332c939ad..d865cb28d7 100644 --- a/testes/gc.lua +++ b/testes/gc.lua @@ -676,11 +676,13 @@ end -- just to make sure assert(collectgarbage'isrunning') -do -- check that the collector is reentrant in incremental mode +do -- check that the collector is not reentrant in incremental mode + local res = true setmetatable({}, {__gc = function () - collectgarbage() + res = collectgarbage() end}) collectgarbage() + assert(not res) end From 066e0f93c4901e601d93e31fb700f8f66f95feb8 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 14 Dec 2021 12:50:05 -0300 Subject: [PATCH 0737/1145] Fix debug information about finalizers The flag CIST_FIN does not mark a finalizer, but the function that was running when the finalizer was called. (So, the function did not call the finalizer, but it looks that way in the stack.) --- ldebug.c | 54 +++++++++++++++++++++++++++++---------------------- lgc.c | 2 +- lstate.h | 2 +- testes/db.lua | 2 +- testes/gc.lua | 2 +- 5 files changed, 35 insertions(+), 27 deletions(-) diff --git a/ldebug.c b/ldebug.c index dc5f78c656..a716d95e22 100644 --- a/ldebug.c +++ b/ldebug.c @@ -34,8 +34,8 @@ #define noLuaClosure(f) ((f) == NULL || (f)->c.tt == LUA_VCCL) -static const char *funcnamefromcode (lua_State *L, CallInfo *ci, - const char **name); +static const char *funcnamefromcall (lua_State *L, CallInfo *ci, + const char **name); static int currentpc (CallInfo *ci) { @@ -317,15 +317,9 @@ static void collectvalidlines (lua_State *L, Closure *f) { static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) { - if (ci == NULL) /* no 'ci'? */ - return NULL; /* no info */ - else if (ci->callstatus & CIST_FIN) { /* is this a finalizer? */ - *name = "__gc"; - return "metamethod"; /* report it as such */ - } - /* calling function is a known Lua function? */ - else if (!(ci->callstatus & CIST_TAIL) && isLua(ci->previous)) - return funcnamefromcode(L, ci->previous, name); + /* calling function is a known function? */ + if (ci != NULL && !(ci->callstatus & CIST_TAIL)) + return funcnamefromcall(L, ci->previous, name); else return NULL; /* no way to find a name */ } @@ -597,16 +591,10 @@ static const char *getobjname (const Proto *p, int lastpc, int reg, ** Returns what the name is (e.g., "for iterator", "method", ** "metamethod") and sets '*name' to point to the name. */ -static const char *funcnamefromcode (lua_State *L, CallInfo *ci, - const char **name) { +static const char *funcnamefromcode (lua_State *L, const Proto *p, + int pc, const char **name) { TMS tm = (TMS)0; /* (initial value avoids warnings) */ - const Proto *p = ci_func(ci)->p; /* calling function */ - int pc = currentpc(ci); /* calling instruction index */ Instruction i = p->code[pc]; /* calling instruction */ - if (ci->callstatus & CIST_HOOKED) { /* was it called inside a hook? */ - *name = "?"; - return "hook"; - } switch (GET_OPCODE(i)) { case OP_CALL: case OP_TAILCALL: @@ -643,6 +631,26 @@ static const char *funcnamefromcode (lua_State *L, CallInfo *ci, return "metamethod"; } + +/* +** Try to find a name for a function based on how it was called. +*/ +static const char *funcnamefromcall (lua_State *L, CallInfo *ci, + const char **name) { + if (ci->callstatus & CIST_HOOKED) { /* was it called inside a hook? */ + *name = "?"; + return "hook"; + } + else if (ci->callstatus & CIST_FIN) { /* was it called as a finalizer? */ + *name = "__gc"; + return "metamethod"; /* report it as such */ + } + else if (isLua(ci)) + return funcnamefromcode(L, ci_func(ci)->p, currentpc(ci), name); + else + return NULL; +} + /* }====================================================== */ @@ -728,14 +736,14 @@ l_noret luaG_typeerror (lua_State *L, const TValue *o, const char *op) { /* -** Raise an error for calling a non-callable object. Try to find -** a name for the object based on the code that made the call -** ('funcnamefromcode'); if it cannot get a name there, try 'varinfo'. +** Raise an error for calling a non-callable object. Try to find a name +** for the object based on how it was called ('funcnamefromcall'); if it +** cannot get a name there, try 'varinfo'. */ l_noret luaG_callerror (lua_State *L, const TValue *o) { CallInfo *ci = L->ci; const char *name = NULL; /* to avoid warnings */ - const char *kind = (isLua(ci)) ? funcnamefromcode(L, ci, &name) : NULL; + const char *kind = funcnamefromcall(L, ci, &name); const char *extra = kind ? formatvarinfo(L, kind, name) : varinfo(L, o); typeerror(L, o, "call", extra); } diff --git a/lgc.c b/lgc.c index 7d0b5e4f7b..d3f5b5b7bb 100644 --- a/lgc.c +++ b/lgc.c @@ -917,7 +917,7 @@ static void GCTM (lua_State *L) { L->allowhook = oldah; /* restore hooks */ g->gcstp = oldgcstp; /* restore state */ if (l_unlikely(status != LUA_OK)) { /* error while running __gc? */ - luaE_warnerror(L, "__gc metamethod"); + luaE_warnerror(L, "__gc"); L->top--; /* pops error object */ } } diff --git a/lstate.h b/lstate.h index 7886d8914b..61e82cde72 100644 --- a/lstate.h +++ b/lstate.h @@ -209,7 +209,7 @@ typedef struct CallInfo { #define CIST_YPCALL (1<<4) /* doing a yieldable protected call */ #define CIST_TAIL (1<<5) /* call was tail called */ #define CIST_HOOKYIELD (1<<6) /* last hook called yielded */ -#define CIST_FIN (1<<7) /* call is running a finalizer */ +#define CIST_FIN (1<<7) /* function "called" a finalizer */ #define CIST_TRAN (1<<8) /* 'ci' has transfer information */ #define CIST_CLSRET (1<<9) /* function is closing tbc variables */ /* Bits 10-12 are used for CIST_RECST (see below) */ diff --git a/testes/db.lua b/testes/db.lua index e06997248e..f891e9b8dd 100644 --- a/testes/db.lua +++ b/testes/db.lua @@ -887,7 +887,7 @@ do -- testing debug info for finalizers -- create a piece of garbage with a finalizer setmetatable({}, {__gc = function () - local t = debug.getinfo(2) -- get callee information + local t = debug.getinfo(1) -- get function information assert(t.namewhat == "metamethod") name = t.name end}) diff --git a/testes/gc.lua b/testes/gc.lua index d865cb28d7..381c5548be 100644 --- a/testes/gc.lua +++ b/testes/gc.lua @@ -371,7 +371,7 @@ if T then warn("@on"); warn("@store") collectgarbage() - assert(string.find(_WARN, "error in __gc metamethod")) + assert(string.find(_WARN, "error in __gc")) assert(string.match(_WARN, "@(.-)@") == "expected"); _WARN = false for i = 8, 10 do assert(s[i]) end From cf613cdc6fa367257fc61c256f63d917350858b5 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 15 Dec 2021 11:29:07 -0300 Subject: [PATCH 0738/1145] Bug: finalizers can be called with an invalid stack The call to 'checkstackGC' can run finalizers, which will find an inconsistent CallInfo, as 'ci' is half updated at the point of call. --- ldo.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ldo.c b/ldo.c index f282a773ea..a48e35f9db 100644 --- a/ldo.c +++ b/ldo.c @@ -530,10 +530,10 @@ int luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, int fsize = p->maxstacksize; /* frame size */ int nfixparams = p->numparams; int i; + checkstackGCp(L, fsize - delta, func); ci->func -= delta; /* restore 'func' (if vararg) */ for (i = 0; i < narg1; i++) /* move down function and arguments */ setobjs2s(L, ci->func + i, func + i); - checkstackGC(L, fsize); func = ci->func; /* moved-down function */ for (; narg1 <= nfixparams; narg1++) setnilvalue(s2v(func + narg1)); /* complete missing arguments */ From 86ec152433baf8daf39f03a59c6842cbe33a179d Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 21 Dec 2021 07:39:25 -0300 Subject: [PATCH 0739/1145] Details correction in macro for hard tests + type in comment --- lapi.c | 2 +- llimits.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lapi.c b/lapi.c index 3585ac436a..5ee65792d2 100644 --- a/lapi.c +++ b/lapi.c @@ -1143,7 +1143,7 @@ LUA_API int lua_gc (lua_State *L, int what, ...) { va_start(argp, what); switch (what) { case LUA_GCSTOP: { - g->gcstp = GCSTPUSR; /* stopeed by the user */ + g->gcstp = GCSTPUSR; /* stopped by the user */ break; } case LUA_GCRESTART: { diff --git a/llimits.h b/llimits.h index 6c56ba5a41..52a32f92e3 100644 --- a/llimits.h +++ b/llimits.h @@ -361,7 +361,7 @@ typedef l_uint32 Instruction; #define condchangemem(L,pre,pos) ((void)0) #else #define condchangemem(L,pre,pos) \ - { if (G(L)->gcrunning) { pre; luaC_fullgc(L, 0); pos; } } + { if (gcrunning(G(L))) { pre; luaC_fullgc(L, 0); pos; } } #endif #endif From 597a53bbc681089d85b082b46c2e2428dec43b86 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 22 Dec 2021 09:00:52 -0300 Subject: [PATCH 0740/1145] Bug: finalizer calling exit can corrupt finalization order 'os.exit' can call lua_close again, separating new finalizers created after all previous finalizers were already separated. --- lgc.c | 10 +++++----- lgc.h | 1 + testes/main.lua | 28 ++++++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 5 deletions(-) diff --git a/lgc.c b/lgc.c index d3f5b5b7bb..42a73d813a 100644 --- a/lgc.c +++ b/lgc.c @@ -907,7 +907,7 @@ static void GCTM (lua_State *L) { int status; lu_byte oldah = L->allowhook; int oldgcstp = g->gcstp; - g->gcstp = GCSTPGC; /* avoid GC steps */ + g->gcstp |= GCSTPGC; /* avoid GC steps */ L->allowhook = 0; /* stop debug hooks during GC metamethod */ setobj2s(L, L->top++, tm); /* push finalizer... */ setobj2s(L, L->top++, &v); /* ... and its argument */ @@ -1011,7 +1011,8 @@ static void correctpointers (global_State *g, GCObject *o) { void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt) { global_State *g = G(L); if (tofinalize(o) || /* obj. is already marked... */ - gfasttm(g, mt, TM_GC) == NULL) /* or has no finalizer? */ + gfasttm(g, mt, TM_GC) == NULL || /* or has no finalizer... */ + (g->gcstp & GCSTPCLS)) /* or closing state? */ return; /* nothing to be done */ else { /* move 'o' to 'finobj' list */ GCObject **p; @@ -1502,14 +1503,13 @@ static void deletelist (lua_State *L, GCObject *p, GCObject *limit) { */ void luaC_freeallobjects (lua_State *L) { global_State *g = G(L); - g->gcstp = GCSTPGC; + g->gcstp = GCSTPCLS; /* no extra finalizers after here */ luaC_changemode(L, KGC_INC); separatetobefnz(g, 1); /* separate all objects with finalizers */ lua_assert(g->finobj == NULL); - g->gcstp = 0; callallpendingfinalizers(L); deletelist(L, g->allgc, obj2gco(g->mainthread)); - deletelist(L, g->finobj, NULL); + lua_assert(g->finobj == NULL); /* no new finalizers */ deletelist(L, g->fixedgc, NULL); /* collect fixed objects */ lua_assert(g->strt.nuse == 0); } diff --git a/lgc.h b/lgc.h index 024a4328e8..4a125634b9 100644 --- a/lgc.h +++ b/lgc.h @@ -154,6 +154,7 @@ */ #define GCSTPUSR 1 /* bit true when GC stopped by user */ #define GCSTPGC 2 /* bit true when GC stopped by itself */ +#define GCSTPCLS 4 /* bit true when closing Lua state */ #define gcrunning(g) ((g)->gcstp == 0) diff --git a/testes/main.lua b/testes/main.lua index 52c779541c..9def63860d 100644 --- a/testes/main.lua +++ b/testes/main.lua @@ -261,6 +261,34 @@ u2 = setmetatable({}, {__gc = function () error("ZYX") end}) RUN('lua -W %s 2> %s', prog, out) checkprogout("ZYX)\nXYZ)\n") +-- bug since 5.2: finalizer called when closing a state could +-- subvert finalization order +prepfile[[ +-- should be called last +print("creating 1") +setmetatable({}, {__gc = function () print(1) end}) + +print("creating 2") +setmetatable({}, {__gc = function () + print("2") + print("creating 3") + -- this finalizer should not be called, as object will be + -- created after 'lua_close' has been called + setmetatable({}, {__gc = function () print(3) end}) + print(collectgarbage()) -- cannot call collector here + os.exit(0, true) +end}) +]] +RUN('lua -W %s > %s', prog, out) +checkout[[ +creating 1 +creating 2 +2 +creating 3 +nil +1 +]] + -- test many arguments prepfile[[print(({...})[30])]] From 05ac2409ee9ea312124bf71dcc93711d652e265b Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Sun, 2 Jan 2022 07:11:08 -0300 Subject: [PATCH 0741/1145] New year (2022) --- lua.h | 4 ++-- manual/2html | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lua.h b/lua.h index c3dbce1e5f..e6618392cc 100644 --- a/lua.h +++ b/lua.h @@ -25,7 +25,7 @@ #define LUA_VERSION "Lua " LUA_VERSION_MAJOR "." LUA_VERSION_MINOR #define LUA_RELEASE LUA_VERSION "." LUA_VERSION_RELEASE -#define LUA_COPYRIGHT LUA_RELEASE " Copyright (C) 1994-2021 Lua.org, PUC-Rio" +#define LUA_COPYRIGHT LUA_RELEASE " Copyright (C) 1994-2022 Lua.org, PUC-Rio" #define LUA_AUTHORS "R. Ierusalimschy, L. H. de Figueiredo, W. Celes" @@ -492,7 +492,7 @@ struct lua_Debug { /****************************************************************************** -* Copyright (C) 1994-2021 Lua.org, PUC-Rio. +* Copyright (C) 1994-2022 Lua.org, PUC-Rio. * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the diff --git a/manual/2html b/manual/2html index f3244bf8a0..a4d860ddfd 100755 --- a/manual/2html +++ b/manual/2html @@ -30,7 +30,7 @@ by Roberto Ierusalimschy, Luiz Henrique de Figueiredo, Waldemar Celes

Copyright -© 2021 Lua.org, PUC-Rio. All rights reserved. +© 2022 Lua.org, PUC-Rio. All rights reserved.


From 8dd2c912d299b84566c6f6d659336edfa9b18e9b Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 3 Jan 2022 09:12:17 -0300 Subject: [PATCH 0742/1145] Detail Warnings with clang when using long double for Lua floats. --- lcode.c | 2 +- lmathlib.c | 4 ++-- lobject.c | 10 +++++----- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/lcode.c b/lcode.c index 9cba24f9c1..06425a1db8 100644 --- a/lcode.c +++ b/lcode.c @@ -607,7 +607,7 @@ static int luaK_numberK (FuncState *fs, lua_Number r) { return addk(fs, &o, &o); /* use number itself as key */ else { /* must build an alternative key */ const int nbm = l_floatatt(MANT_DIG); - const lua_Number q = l_mathop(ldexp)(1.0, -nbm + 1); + const lua_Number q = l_mathop(ldexp)(l_mathop(1.0), -nbm + 1); const lua_Number k = (ik == 0) ? q : r + r*q; /* new key */ TValue kv; setfltvalue(&kv, k); diff --git a/lmathlib.c b/lmathlib.c index 5f5983a438..e0c61a168d 100644 --- a/lmathlib.c +++ b/lmathlib.c @@ -475,7 +475,7 @@ static lua_Number I2d (Rand64 x) { /* 2^(-FIGS) = 1.0 / 2^30 / 2^3 / 2^(FIGS-33) */ #define scaleFIG \ - ((lua_Number)1.0 / (UONE << 30) / 8.0 / (UONE << (FIGS - 33))) + (l_mathop(1.0) / (UONE << 30) / l_mathop(8.0) / (UONE << (FIGS - 33))) /* ** use FIGS - 32 bits from lower half, throwing out the other @@ -486,7 +486,7 @@ static lua_Number I2d (Rand64 x) { /* ** higher 32 bits go after those (FIGS - 32) bits: shiftHI = 2^(FIGS - 32) */ -#define shiftHI ((lua_Number)(UONE << (FIGS - 33)) * 2.0) +#define shiftHI ((lua_Number)(UONE << (FIGS - 33)) * l_mathop(2.0)) static lua_Number I2d (Rand64 x) { diff --git a/lobject.c b/lobject.c index 0e504be03e..301aa900be 100644 --- a/lobject.c +++ b/lobject.c @@ -164,7 +164,7 @@ static int isneg (const char **s) { */ static lua_Number lua_strx2number (const char *s, char **endptr) { int dot = lua_getlocaledecpoint(); - lua_Number r = 0.0; /* result (accumulator) */ + lua_Number r = l_mathop(0.0); /* result (accumulator) */ int sigdig = 0; /* number of significant digits */ int nosigdig = 0; /* number of non-significant digits */ int e = 0; /* exponent correction */ @@ -174,7 +174,7 @@ static lua_Number lua_strx2number (const char *s, char **endptr) { while (lisspace(cast_uchar(*s))) s++; /* skip initial spaces */ neg = isneg(&s); /* check sign */ if (!(*s == '0' && (*(s + 1) == 'x' || *(s + 1) == 'X'))) /* check '0x' */ - return 0.0; /* invalid format (no '0x') */ + return l_mathop(0.0); /* invalid format (no '0x') */ for (s += 2; ; s++) { /* skip '0x' and read numeral */ if (*s == dot) { if (hasdot) break; /* second dot? stop loop */ @@ -184,14 +184,14 @@ static lua_Number lua_strx2number (const char *s, char **endptr) { if (sigdig == 0 && *s == '0') /* non-significant digit (zero)? */ nosigdig++; else if (++sigdig <= MAXSIGDIG) /* can read it without overflow? */ - r = (r * cast_num(16.0)) + luaO_hexavalue(*s); + r = (r * l_mathop(16.0)) + luaO_hexavalue(*s); else e++; /* too many digits; ignore, but still count for exponent */ if (hasdot) e--; /* decimal digit? correct exponent */ } else break; /* neither a dot nor a digit */ } if (nosigdig + sigdig == 0) /* no digits? */ - return 0.0; /* invalid format */ + return l_mathop(0.0); /* invalid format */ *endptr = cast_charp(s); /* valid up to here */ e *= 4; /* each digit multiplies/divides value by 2^4 */ if (*s == 'p' || *s == 'P') { /* exponent part? */ @@ -200,7 +200,7 @@ static lua_Number lua_strx2number (const char *s, char **endptr) { s++; /* skip 'p' */ neg1 = isneg(&s); /* sign */ if (!lisdigit(cast_uchar(*s))) - return 0.0; /* invalid; must have at least one digit */ + return l_mathop(0.0); /* invalid; must have at least one digit */ while (lisdigit(cast_uchar(*s))) /* read exponent */ exp1 = exp1 * 10 + *(s++) - '0'; if (neg1) exp1 = -exp1; From 5d708c3f9cae12820e415d4f89c9eacbe2ab964b Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 13 Jan 2022 08:15:03 -0300 Subject: [PATCH 0743/1145] Explanation of borders in the manual The explanation includes the limit case of maxinteger being a border. It also avoids the term "natural", which might include large floats with natural values. --- manual/manual.of | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/manual/manual.of b/manual/manual.of index c660215c0e..15f207fa28 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -1980,15 +1980,20 @@ character is one byte.) The length operator applied on a table returns a @x{border} in that table. -A @def{border} in a table @id{t} is any natural number +A @def{border} in a table @id{t} is any non-negative integer that satisfies the following condition: @verbatim{ -(border == 0 or t[border] ~= nil) and t[border + 1] == nil +(border == 0 or t[border] ~= nil) and +(t[border + 1] == nil or border == math.maxinteger) } In words, -a border is any (natural) index present in the table -that is followed by an absent index -(or zero, when index 1 is absent). +a border is any positive integer index present in the table +that is followed by an absent index, +plus two limit cases: +zero, when index 1 is absent; +and the maximum value for an integer, when that index is present. +Note that keys that are not positive integers +do not interfere with borders. A table with exactly one border is called a @def{sequence}. For instance, the table @T{{10, 20, 30, 40, 50}} is a sequence, @@ -1997,12 +2002,9 @@ The table @T{{10, 20, 30, nil, 50}} has two borders (3 and 5), and therefore it is not a sequence. (The @nil at index 4 is called a @emphx{hole}.) The table @T{{nil, 20, 30, nil, nil, 60, nil}} -has three borders (0, 3, and 6) and three holes -(at indices 1, 4, and 5), +has three borders (0, 3, and 6), so it is not a sequence, too. The table @T{{}} is a sequence with border 0. -Note that non-natural keys do not interfere -with whether a table is a sequence. When @id{t} is a sequence, @T{#t} returns its only border, @@ -2016,7 +2018,7 @@ the memory addresses of its non-numeric keys.) The computation of the length of a table has a guaranteed worst time of @M{O(log n)}, -where @M{n} is the largest natural key in the table. +where @M{n} is the largest integer key in the table. A program can modify the behavior of the length operator for any value but strings through the @idx{__len} metamethod @see{metatable}. From 25b143dd34fb587d1e35290c4b25bc08954800e2 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 7 Feb 2022 10:16:35 -0300 Subject: [PATCH 0744/1145] Bug: lua.c assumes that argv has at least one element --- lua.c | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/lua.c b/lua.c index 0f19004444..7f7dc2b22a 100644 --- a/lua.c +++ b/lua.c @@ -177,10 +177,11 @@ static void print_version (void) { ** to the script (everything after 'script') go to positive indices; ** other arguments (before the script name) go to negative indices. ** If there is no script name, assume interpreter's name as base. +** (If there is no interpreter's name either, 'script' is -1, so +** table sizes are zero.) */ static void createargtable (lua_State *L, char **argv, int argc, int script) { int i, narg; - if (script == argc) script = 0; /* no script name? */ narg = argc - (script + 1); /* number of positive indices */ lua_createtable(L, narg, script + 1); for (i = 0; i < argc; i++) { @@ -268,14 +269,23 @@ static int handle_script (lua_State *L, char **argv) { /* ** Traverses all arguments from 'argv', returning a mask with those -** needed before running any Lua code (or an error code if it finds -** any invalid argument). 'first' returns the first not-handled argument -** (either the script name or a bad argument in case of error). +** needed before running any Lua code or an error code if it finds any +** invalid argument. In case of error, 'first' is the index of the bad +** argument. Otherwise, 'first' is -1 if there is no program name, +** 0 if there is no script name, or the index of the script name. */ static int collectargs (char **argv, int *first) { int args = 0; int i; - for (i = 1; argv[i] != NULL; i++) { + if (argv[0] != NULL) { /* is there a program name? */ + if (argv[0][0]) /* not empty? */ + progname = argv[0]; /* save it */ + } + else { /* no program name */ + *first = -1; + return 0; + } + for (i = 1; argv[i] != NULL; i++) { /* handle arguments */ *first = i; if (argv[i][0] != '-') /* not an option? */ return args; /* stop handling options */ @@ -316,7 +326,7 @@ static int collectargs (char **argv, int *first) { return has_error; } } - *first = i; /* no script name */ + *first = 0; /* no script name */ return args; } @@ -609,8 +619,8 @@ static int pmain (lua_State *L) { char **argv = (char **)lua_touserdata(L, 2); int script; int args = collectargs(argv, &script); + int optlim = (script > 0) ? script : argc; /* first argv not an option */ luaL_checkversion(L); /* check that interpreter has correct version */ - if (argv[0] && argv[0][0]) progname = argv[0]; if (args == has_error) { /* bad arg? */ print_usage(argv[script]); /* 'script' has index of bad arg. */ return 0; @@ -628,14 +638,15 @@ static int pmain (lua_State *L) { if (handle_luainit(L) != LUA_OK) /* run LUA_INIT */ return 0; /* error running LUA_INIT */ } - if (!runargs(L, argv, script)) /* execute arguments -e and -l */ + if (!runargs(L, argv, optlim)) /* execute arguments -e and -l */ return 0; /* something failed */ - if (script < argc && /* execute main script (if there is one) */ - handle_script(L, argv + script) != LUA_OK) - return 0; + if (script > 0) { /* execute main script (if there is one) */ + if (handle_script(L, argv + script) != LUA_OK) + return 0; /* interrupt in case of error */ + } if (args & has_i) /* -i option? */ doREPL(L); /* do read-eval-print loop */ - else if (script == argc && !(args & (has_e | has_v))) { /* no arguments? */ + else if (script < 1 && !(args & (has_e | has_v))) { /* no active option? */ if (lua_stdin_is_tty()) { /* running in interactive mode? */ print_version(); doREPL(L); /* do read-eval-print loop */ From 1f3c6f4534c6411313361697d98d1145a1f030fa Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 15 Feb 2022 12:28:46 -0300 Subject: [PATCH 0745/1145] Bug: Lua can generate wrong code when _ENV is --- lparser.c | 1 + testes/attrib.lua | 10 ++++++++++ 2 files changed, 11 insertions(+) diff --git a/lparser.c b/lparser.c index 3abe3d7518..a5cd552578 100644 --- a/lparser.c +++ b/lparser.c @@ -468,6 +468,7 @@ static void singlevar (LexState *ls, expdesc *var) { expdesc key; singlevaraux(fs, ls->envn, var, 1); /* get environment variable */ lua_assert(var->k != VVOID); /* this one must exist */ + luaK_exp2anyregup(fs, var); /* but could be a constant */ codestring(&key, varname); /* key is variable name */ luaK_indexed(fs, var, &key); /* env[varname] */ } diff --git a/testes/attrib.lua b/testes/attrib.lua index b1076c768a..83821c069a 100644 --- a/testes/attrib.lua +++ b/testes/attrib.lua @@ -434,6 +434,16 @@ a.aVeryLongName012345678901234567890123456789012345678901234567890123456789 == 10) +do + -- _ENV constant + local function foo () + local _ENV = 11 + X = "hi" + end + local st, msg = pcall(foo) + assert(not st and string.find(msg, "number")) +end + -- test of large float/integer indices From 8426d9b4d4df1da3c5b2d759e509ae1c50a86667 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 18 Feb 2022 13:22:25 -0300 Subject: [PATCH 0746/1145] Avoid computing invalid addresses luaV_execute should compute 'ra' only when the instruction uses it. Computing an illegal address is undefined behavior even if the address is never dereferenced. --- lvm.c | 121 ++++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 89 insertions(+), 32 deletions(-) diff --git a/lvm.c b/lvm.c index 2ec3440031..f3a5662b61 100644 --- a/lvm.c +++ b/lvm.c @@ -898,6 +898,7 @@ void luaV_finishOp (lua_State *L) { ** operation, 'fop' is the float operation. */ #define op_arithI(L,iop,fop) { \ + StkId ra = RA(i); \ TValue *v1 = vRB(i); \ int imm = GETARG_sC(i); \ if (ttisinteger(v1)) { \ @@ -926,6 +927,7 @@ void luaV_finishOp (lua_State *L) { ** Arithmetic operations over floats and others with register operands. */ #define op_arithf(L,fop) { \ + StkId ra = RA(i); \ TValue *v1 = vRB(i); \ TValue *v2 = vRC(i); \ op_arithf_aux(L, v1, v2, fop); } @@ -935,6 +937,7 @@ void luaV_finishOp (lua_State *L) { ** Arithmetic operations with K operands for floats. */ #define op_arithfK(L,fop) { \ + StkId ra = RA(i); \ TValue *v1 = vRB(i); \ TValue *v2 = KC(i); lua_assert(ttisnumber(v2)); \ op_arithf_aux(L, v1, v2, fop); } @@ -944,6 +947,7 @@ void luaV_finishOp (lua_State *L) { ** Arithmetic operations over integers and floats. */ #define op_arith_aux(L,v1,v2,iop,fop) { \ + StkId ra = RA(i); \ if (ttisinteger(v1) && ttisinteger(v2)) { \ lua_Integer i1 = ivalue(v1); lua_Integer i2 = ivalue(v2); \ pc++; setivalue(s2v(ra), iop(L, i1, i2)); \ @@ -973,6 +977,7 @@ void luaV_finishOp (lua_State *L) { ** Bitwise operations with constant operand. */ #define op_bitwiseK(L,op) { \ + StkId ra = RA(i); \ TValue *v1 = vRB(i); \ TValue *v2 = KC(i); \ lua_Integer i1; \ @@ -986,6 +991,7 @@ void luaV_finishOp (lua_State *L) { ** Bitwise operations with register operands. */ #define op_bitwise(L,op) { \ + StkId ra = RA(i); \ TValue *v1 = vRB(i); \ TValue *v2 = vRC(i); \ lua_Integer i1; lua_Integer i2; \ @@ -1000,18 +1006,19 @@ void luaV_finishOp (lua_State *L) { ** integers. */ #define op_order(L,opi,opn,other) { \ - int cond; \ - TValue *rb = vRB(i); \ - if (ttisinteger(s2v(ra)) && ttisinteger(rb)) { \ - lua_Integer ia = ivalue(s2v(ra)); \ - lua_Integer ib = ivalue(rb); \ - cond = opi(ia, ib); \ - } \ - else if (ttisnumber(s2v(ra)) && ttisnumber(rb)) \ - cond = opn(s2v(ra), rb); \ - else \ - Protect(cond = other(L, s2v(ra), rb)); \ - docondjump(); } + StkId ra = RA(i); \ + int cond; \ + TValue *rb = vRB(i); \ + if (ttisinteger(s2v(ra)) && ttisinteger(rb)) { \ + lua_Integer ia = ivalue(s2v(ra)); \ + lua_Integer ib = ivalue(rb); \ + cond = opi(ia, ib); \ + } \ + else if (ttisnumber(s2v(ra)) && ttisnumber(rb)) \ + cond = opn(s2v(ra), rb); \ + else \ + Protect(cond = other(L, s2v(ra), rb)); \ + docondjump(); } /* @@ -1019,20 +1026,21 @@ void luaV_finishOp (lua_State *L) { ** always small enough to have an exact representation as a float.) */ #define op_orderI(L,opi,opf,inv,tm) { \ - int cond; \ - int im = GETARG_sB(i); \ - if (ttisinteger(s2v(ra))) \ - cond = opi(ivalue(s2v(ra)), im); \ - else if (ttisfloat(s2v(ra))) { \ - lua_Number fa = fltvalue(s2v(ra)); \ - lua_Number fim = cast_num(im); \ - cond = opf(fa, fim); \ - } \ - else { \ - int isf = GETARG_C(i); \ - Protect(cond = luaT_callorderiTM(L, s2v(ra), im, inv, isf, tm)); \ - } \ - docondjump(); } + StkId ra = RA(i); \ + int cond; \ + int im = GETARG_sB(i); \ + if (ttisinteger(s2v(ra))) \ + cond = opi(ivalue(s2v(ra)), im); \ + else if (ttisfloat(s2v(ra))) { \ + lua_Number fa = fltvalue(s2v(ra)); \ + lua_Number fim = cast_num(im); \ + cond = opf(fa, fim); \ + } \ + else { \ + int isf = GETARG_C(i); \ + Protect(cond = luaT_callorderiTM(L, s2v(ra), im, inv, isf, tm)); \ + } \ + docondjump(); } /* }================================================================== */ @@ -1128,7 +1136,6 @@ void luaV_finishOp (lua_State *L) { updatebase(ci); /* correct stack */ \ } \ i = *(pc++); \ - ra = RA(i); /* WARNING: any stack reallocation invalidates 'ra' */ \ } #define vmdispatch(o) switch(o) @@ -1164,7 +1171,6 @@ void luaV_execute (lua_State *L, CallInfo *ci) { /* main loop of interpreter */ for (;;) { Instruction i; /* instruction being executed */ - StkId ra; /* instruction's A register */ vmfetch(); #if 0 /* low-level line tracing for debugging Lua */ @@ -1176,44 +1182,53 @@ void luaV_execute (lua_State *L, CallInfo *ci) { lua_assert(isIT(i) || (cast_void(L->top = base), 1)); vmdispatch (GET_OPCODE(i)) { vmcase(OP_MOVE) { + StkId ra = RA(i); setobjs2s(L, ra, RB(i)); vmbreak; } vmcase(OP_LOADI) { + StkId ra = RA(i); lua_Integer b = GETARG_sBx(i); setivalue(s2v(ra), b); vmbreak; } vmcase(OP_LOADF) { + StkId ra = RA(i); int b = GETARG_sBx(i); setfltvalue(s2v(ra), cast_num(b)); vmbreak; } vmcase(OP_LOADK) { + StkId ra = RA(i); TValue *rb = k + GETARG_Bx(i); setobj2s(L, ra, rb); vmbreak; } vmcase(OP_LOADKX) { + StkId ra = RA(i); TValue *rb; rb = k + GETARG_Ax(*pc); pc++; setobj2s(L, ra, rb); vmbreak; } vmcase(OP_LOADFALSE) { + StkId ra = RA(i); setbfvalue(s2v(ra)); vmbreak; } vmcase(OP_LFALSESKIP) { + StkId ra = RA(i); setbfvalue(s2v(ra)); pc++; /* skip next instruction */ vmbreak; } vmcase(OP_LOADTRUE) { + StkId ra = RA(i); setbtvalue(s2v(ra)); vmbreak; } vmcase(OP_LOADNIL) { + StkId ra = RA(i); int b = GETARG_B(i); do { setnilvalue(s2v(ra++)); @@ -1221,17 +1236,20 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_GETUPVAL) { + StkId ra = RA(i); int b = GETARG_B(i); setobj2s(L, ra, cl->upvals[b]->v); vmbreak; } vmcase(OP_SETUPVAL) { + StkId ra = RA(i); UpVal *uv = cl->upvals[GETARG_B(i)]; setobj(L, uv->v, s2v(ra)); luaC_barrier(L, uv, s2v(ra)); vmbreak; } vmcase(OP_GETTABUP) { + StkId ra = RA(i); const TValue *slot; TValue *upval = cl->upvals[GETARG_B(i)]->v; TValue *rc = KC(i); @@ -1244,6 +1262,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_GETTABLE) { + StkId ra = RA(i); const TValue *slot; TValue *rb = vRB(i); TValue *rc = vRC(i); @@ -1258,6 +1277,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_GETI) { + StkId ra = RA(i); const TValue *slot; TValue *rb = vRB(i); int c = GETARG_C(i); @@ -1272,6 +1292,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_GETFIELD) { + StkId ra = RA(i); const TValue *slot; TValue *rb = vRB(i); TValue *rc = KC(i); @@ -1297,6 +1318,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_SETTABLE) { + StkId ra = RA(i); const TValue *slot; TValue *rb = vRB(i); /* key (table is in 'ra') */ TValue *rc = RKC(i); /* value */ @@ -1311,6 +1333,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_SETI) { + StkId ra = RA(i); const TValue *slot; int c = GETARG_B(i); TValue *rc = RKC(i); @@ -1325,6 +1348,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_SETFIELD) { + StkId ra = RA(i); const TValue *slot; TValue *rb = KB(i); TValue *rc = RKC(i); @@ -1337,6 +1361,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_NEWTABLE) { + StkId ra = RA(i); int b = GETARG_B(i); /* log2(hash size) + 1 */ int c = GETARG_C(i); /* array size */ Table *t; @@ -1355,6 +1380,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_SELF) { + StkId ra = RA(i); const TValue *slot; TValue *rb = vRB(i); TValue *rc = RKC(i); @@ -1412,6 +1438,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_SHRI) { + StkId ra = RA(i); TValue *rb = vRB(i); int ic = GETARG_sC(i); lua_Integer ib; @@ -1421,6 +1448,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_SHLI) { + StkId ra = RA(i); TValue *rb = vRB(i); int ic = GETARG_sC(i); lua_Integer ib; @@ -1478,6 +1506,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_MMBIN) { + StkId ra = RA(i); Instruction pi = *(pc - 2); /* original arith. expression */ TValue *rb = vRB(i); TMS tm = (TMS)GETARG_C(i); @@ -1487,6 +1516,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_MMBINI) { + StkId ra = RA(i); Instruction pi = *(pc - 2); /* original arith. expression */ int imm = GETARG_sB(i); TMS tm = (TMS)GETARG_C(i); @@ -1496,6 +1526,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_MMBINK) { + StkId ra = RA(i); Instruction pi = *(pc - 2); /* original arith. expression */ TValue *imm = KB(i); TMS tm = (TMS)GETARG_C(i); @@ -1505,6 +1536,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_UNM) { + StkId ra = RA(i); TValue *rb = vRB(i); lua_Number nb; if (ttisinteger(rb)) { @@ -1519,6 +1551,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_BNOT) { + StkId ra = RA(i); TValue *rb = vRB(i); lua_Integer ib; if (tointegerns(rb, &ib)) { @@ -1529,6 +1562,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_NOT) { + StkId ra = RA(i); TValue *rb = vRB(i); if (l_isfalse(rb)) setbtvalue(s2v(ra)); @@ -1537,10 +1571,12 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_LEN) { + StkId ra = RA(i); Protect(luaV_objlen(L, ra, vRB(i))); vmbreak; } vmcase(OP_CONCAT) { + StkId ra = RA(i); int n = GETARG_B(i); /* number of elements to concatenate */ L->top = ra + n; /* mark the end of concat operands */ ProtectNT(luaV_concat(L, n)); @@ -1548,10 +1584,12 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_CLOSE) { + StkId ra = RA(i); Protect(luaF_close(L, ra, LUA_OK, 1)); vmbreak; } vmcase(OP_TBC) { + StkId ra = RA(i); /* create new to-be-closed upvalue */ halfProtect(luaF_newtbcupval(L, ra)); vmbreak; @@ -1561,6 +1599,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_EQ) { + StkId ra = RA(i); int cond; TValue *rb = vRB(i); Protect(cond = luaV_equalobj(L, s2v(ra), rb)); @@ -1576,6 +1615,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_EQK) { + StkId ra = RA(i); TValue *rb = KB(i); /* basic types do not use '__eq'; we can use raw equality */ int cond = luaV_rawequalobj(s2v(ra), rb); @@ -1583,6 +1623,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_EQI) { + StkId ra = RA(i); int cond; int im = GETARG_sB(i); if (ttisinteger(s2v(ra))) @@ -1611,11 +1652,13 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_TEST) { + StkId ra = RA(i); int cond = !l_isfalse(s2v(ra)); docondjump(); vmbreak; } vmcase(OP_TESTSET) { + StkId ra = RA(i); TValue *rb = vRB(i); if (l_isfalse(rb) == GETARG_k(i)) pc++; @@ -1626,6 +1669,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_CALL) { + StkId ra = RA(i); CallInfo *newci; int b = GETARG_B(i); int nresults = GETARG_C(i) - 1; @@ -1642,6 +1686,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_TAILCALL) { + StkId ra = RA(i); int b = GETARG_B(i); /* number of arguments + 1 (function) */ int n; /* number of results when calling a C function */ int nparams1 = GETARG_C(i); @@ -1667,6 +1712,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { } } vmcase(OP_RETURN) { + StkId ra = RA(i); int n = GETARG_B(i) - 1; /* number of results */ int nparams1 = GETARG_C(i); if (n < 0) /* not fixed? */ @@ -1689,6 +1735,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { } vmcase(OP_RETURN0) { if (l_unlikely(L->hookmask)) { + StkId ra = RA(i); L->top = ra; savepc(ci); luaD_poscall(L, ci, 0); /* no hurry... */ @@ -1705,6 +1752,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { } vmcase(OP_RETURN1) { if (l_unlikely(L->hookmask)) { + StkId ra = RA(i); L->top = ra + 1; savepc(ci); luaD_poscall(L, ci, 1); /* no hurry... */ @@ -1716,6 +1764,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { if (nres == 0) L->top = base - 1; /* asked for no results */ else { + StkId ra = RA(i); setobjs2s(L, base - 1, ra); /* at least this result */ L->top = base; for (; l_unlikely(nres > 1); nres--) @@ -1731,6 +1780,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { } } vmcase(OP_FORLOOP) { + StkId ra = RA(i); if (ttisinteger(s2v(ra + 2))) { /* integer loop? */ lua_Unsigned count = l_castS2U(ivalue(s2v(ra + 1))); if (count > 0) { /* still more iterations? */ @@ -1749,12 +1799,14 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_FORPREP) { + StkId ra = RA(i); savestate(L, ci); /* in case of errors */ if (forprep(L, ra)) pc += GETARG_Bx(i) + 1; /* skip the loop */ vmbreak; } vmcase(OP_TFORPREP) { + StkId ra = RA(i); /* create to-be-closed upvalue (if needed) */ halfProtect(luaF_newtbcupval(L, ra + 3)); pc += GETARG_Bx(i); @@ -1763,7 +1815,8 @@ void luaV_execute (lua_State *L, CallInfo *ci) { goto l_tforcall; } vmcase(OP_TFORCALL) { - l_tforcall: + l_tforcall: { + StkId ra = RA(i); /* 'ra' has the iterator function, 'ra + 1' has the state, 'ra + 2' has the control variable, and 'ra + 3' has the to-be-closed variable. The call will use the stack after @@ -1777,16 +1830,18 @@ void luaV_execute (lua_State *L, CallInfo *ci) { i = *(pc++); /* go to next instruction */ lua_assert(GET_OPCODE(i) == OP_TFORLOOP && ra == RA(i)); goto l_tforloop; - } + }} vmcase(OP_TFORLOOP) { - l_tforloop: + l_tforloop: { + StkId ra = RA(i); if (!ttisnil(s2v(ra + 4))) { /* continue loop? */ setobjs2s(L, ra + 2, ra + 4); /* save control variable */ pc -= GETARG_Bx(i); /* jump back */ } vmbreak; - } + }} vmcase(OP_SETLIST) { + StkId ra = RA(i); int n = GETARG_B(i); unsigned int last = GETARG_C(i); Table *h = hvalue(s2v(ra)); @@ -1810,12 +1865,14 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_CLOSURE) { + StkId ra = RA(i); Proto *p = cl->p->p[GETARG_Bx(i)]; halfProtect(pushclosure(L, p, cl->upvals, base, ra)); checkGC(L, ra + 1); vmbreak; } vmcase(OP_VARARG) { + StkId ra = RA(i); int n = GETARG_C(i) - 1; /* required results */ Protect(luaT_getvarargs(L, ci, ra, n)); vmbreak; From f3cfd5bf2b11ba207c71344243892645157900b7 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 1 Apr 2022 13:55:44 -0300 Subject: [PATCH 0747/1145] Details Comments + manual + identation + asserts about stack limits that were not allowing the use of the full stack --- ldo.c | 8 ++++++-- loadlib.c | 9 +++++++-- lvm.c | 2 +- manual/manual.of | 4 +++- 4 files changed, 17 insertions(+), 6 deletions(-) diff --git a/ldo.c b/ldo.c index a48e35f9db..8e4faf0209 100644 --- a/ldo.c +++ b/ldo.c @@ -213,7 +213,7 @@ int luaD_reallocstack (lua_State *L, int newsize, int raiseerror) { /* -** Try to grow the stack by at least 'n' elements. when 'raiseerror' +** Try to grow the stack by at least 'n' elements. When 'raiseerror' ** is true, raises any error; otherwise, return 0 in case of errors. */ int luaD_growstack (lua_State *L, int n, int raiseerror) { @@ -247,6 +247,10 @@ int luaD_growstack (lua_State *L, int n, int raiseerror) { } +/* +** Compute how much of the stack is being used, by computing the +** maximum top of all call frames in the stack and the current top. +*/ static int stackinuse (lua_State *L) { CallInfo *ci; int res; @@ -254,7 +258,7 @@ static int stackinuse (lua_State *L) { for (ci = L->ci; ci != NULL; ci = ci->previous) { if (lim < ci->top) lim = ci->top; } - lua_assert(lim <= L->stack_last); + lua_assert(lim <= L->stack_last + EXTRA_STACK); res = cast_int(lim - L->stack) + 1; /* part of stack in use */ if (res < LUA_MINSTACK) res = LUA_MINSTACK; /* ensure a minimum size */ diff --git a/loadlib.c b/loadlib.c index 6f9fa37366..d792dffaa0 100644 --- a/loadlib.c +++ b/loadlib.c @@ -708,8 +708,13 @@ static const luaL_Reg ll_funcs[] = { static void createsearcherstable (lua_State *L) { - static const lua_CFunction searchers[] = - {searcher_preload, searcher_Lua, searcher_C, searcher_Croot, NULL}; + static const lua_CFunction searchers[] = { + searcher_preload, + searcher_Lua, + searcher_C, + searcher_Croot, + NULL + }; int i; /* create 'searchers' table */ lua_createtable(L, sizeof(searchers)/sizeof(searchers[0]) - 1, 0); diff --git a/lvm.c b/lvm.c index f3a5662b61..e8c2e9627c 100644 --- a/lvm.c +++ b/lvm.c @@ -1177,7 +1177,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { printf("line: %d\n", luaG_getfuncline(cl->p, pcRel(pc, cl->p))); #endif lua_assert(base == ci->func + 1); - lua_assert(base <= L->top && L->top < L->stack_last); + lua_assert(base <= L->top && L->top <= L->stack_last); /* invalidate top for instructions not expecting it */ lua_assert(isIT(i) || (cast_void(L->top = base), 1)); vmdispatch (GET_OPCODE(i)) { diff --git a/manual/manual.of b/manual/manual.of index 15f207fa28..bd648c6cad 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -3981,6 +3981,7 @@ Also @N{returns 0} if any of the indices are not valid. Similar to @Lid{lua_gettable}, but does a raw access (i.e., without metamethods). +The value at @id{index} must be a table. } @@ -4027,6 +4028,7 @@ For other values, this call @N{returns 0}. Similar to @Lid{lua_settable}, but does a raw assignment (i.e., without metamethods). +The value at @id{index} must be a table. } @@ -7280,7 +7282,7 @@ according to the format string @id{fmt} @see{pack}. @LibEntry{string.packsize (fmt)| -Returns the size of a string resulting from @Lid{string.pack} +Returns the length of a string resulting from @Lid{string.pack} with the given format. The format string cannot have the variable-length options @Char{s} or @Char{z} @see{pack}. From 295cde94545b00afc8624bd388db805504d356bd Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 7 Apr 2022 10:52:15 -0300 Subject: [PATCH 0748/1145] New release number (5.4.5) --- lua.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lua.h b/lua.h index e6618392cc..219784cc01 100644 --- a/lua.h +++ b/lua.h @@ -18,10 +18,10 @@ #define LUA_VERSION_MAJOR "5" #define LUA_VERSION_MINOR "4" -#define LUA_VERSION_RELEASE "4" +#define LUA_VERSION_RELEASE "5" #define LUA_VERSION_NUM 504 -#define LUA_VERSION_RELEASE_NUM (LUA_VERSION_NUM * 100 + 4) +#define LUA_VERSION_RELEASE_NUM (LUA_VERSION_NUM * 100 + 5) #define LUA_VERSION "Lua " LUA_VERSION_MAJOR "." LUA_VERSION_MINOR #define LUA_RELEASE LUA_VERSION "." LUA_VERSION_RELEASE From c764ca71a639f5585b5f466bea25dc42b855a4b0 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 25 Apr 2022 14:42:51 -0300 Subject: [PATCH 0749/1145] Bug: Wrong code generation in bitwise operations --- lcode.c | 16 ++++++++++------ ltests.h | 7 +++++++ testes/constructs.lua | 25 +++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 6 deletions(-) diff --git a/lcode.c b/lcode.c index 06425a1db8..cb724a0908 100644 --- a/lcode.c +++ b/lcode.c @@ -1391,7 +1391,10 @@ static void finishbinexpval (FuncState *fs, expdesc *e1, expdesc *e2, */ static void codebinexpval (FuncState *fs, OpCode op, expdesc *e1, expdesc *e2, int line) { - int v2 = luaK_exp2anyreg(fs, e2); /* both operands are in registers */ + int v2 = luaK_exp2anyreg(fs, e2); /* make sure 'e2' is in a register */ + /* 'e1' must be already in a register or it is a constant */ + lua_assert((VNIL <= e1->k && e1->k <= VKSTR) || + e1->k == VNONRELOC || e1->k == VRELOC); lua_assert(OP_ADD <= op && op <= OP_SHR); finishbinexpval(fs, e1, e2, op, v2, 0, line, OP_MMBIN, cast(TMS, (op - OP_ADD) + TM_ADD)); @@ -1478,7 +1481,7 @@ static void codecommutative (FuncState *fs, BinOpr op, /* -** Code bitwise operations; they are all associative, so the function +** Code bitwise operations; they are all commutative, so the function ** tries to put an integer constant as the 2nd operand (a K operand). */ static void codebitwise (FuncState *fs, BinOpr opr, @@ -1486,11 +1489,11 @@ static void codebitwise (FuncState *fs, BinOpr opr, int flip = 0; int v2; OpCode op; - if (e1->k == VKINT && luaK_exp2RK(fs, e1)) { + if (e1->k == VKINT && luaK_exp2K(fs, e1)) { swapexps(e1, e2); /* 'e2' will be the constant operand */ flip = 1; } - else if (!(e2->k == VKINT && luaK_exp2RK(fs, e2))) { /* no constants? */ + else if (!(e2->k == VKINT && luaK_exp2K(fs, e2))) { /* no constants? */ op = cast(OpCode, opr + OP_ADD); codebinexpval(fs, op, e1, e2, line); /* all-register opcodes */ return; @@ -1551,7 +1554,7 @@ static void codeeq (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) { op = OP_EQI; r2 = im; /* immediate operand */ } - else if (luaK_exp2RK(fs, e2)) { /* 1st expression is constant? */ + else if (luaK_exp2RK(fs, e2)) { /* 2nd expression is constant? */ op = OP_EQK; r2 = e2->u.info; /* constant index */ } @@ -1611,7 +1614,8 @@ void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) { case OPR_SHL: case OPR_SHR: { if (!tonumeral(v, NULL)) luaK_exp2anyreg(fs, v); - /* else keep numeral, which may be folded with 2nd operand */ + /* else keep numeral, which may be folded or used as an immediate + operand */ break; } case OPR_EQ: case OPR_NE: { diff --git a/ltests.h b/ltests.h index cb3a0b4804..ec520498bd 100644 --- a/ltests.h +++ b/ltests.h @@ -125,6 +125,13 @@ LUA_API void *debug_realloc (void *ud, void *block, #define LUAI_USER_ALIGNMENT_T union { char b[sizeof(void*) * 8]; } +/* +** This one is not compatible with tests for opcode optimizations, +** as it blocks some optimizations +#define MAXINDEXRK 0 +*/ + + /* make stack-overflow tests run faster */ #undef LUAI_MAXSTACK #define LUAI_MAXSTACK 50000 diff --git a/testes/constructs.lua b/testes/constructs.lua index a74a8c0412..0d9ec92d7d 100644 --- a/testes/constructs.lua +++ b/testes/constructs.lua @@ -103,6 +103,31 @@ do -- test old bug (first name could not be an `upvalue') local a; function f(x) x={a=1}; x={x=1}; x={G=1} end end + +do -- bug since 5.4.0 + -- create code with a table using more than 256 constants + local code = {"local x = {"} + for i = 1, 257 do + code[#code + 1] = i .. ".1," + end + code[#code + 1] = "};" + code = table.concat(code) + + -- add "ret" to the end of that code and checks that + -- it produces the expected value "val" + local function check (ret, val) + local code = code .. ret + code = load(code) + assert(code() == val) + end + + check("return (1 ~ (2 or 3))", 1 ~ 2) + check("return (1 | (2 or 3))", 1 | 2) + check("return (1 + (2 or 3))", 1 + 2) + check("return (1 << (2 or 3))", 1 << 2) +end + + function f (i) if type(i) ~= 'number' then return i,'jojo'; end; if i > 0 then return i, f(i-1); end; From 315639d3bbdff4f83d2ab55863141276cb882af0 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 6 May 2022 17:52:46 -0300 Subject: [PATCH 0750/1145] Factoring out common parts of 'codearith' and 'codebitwise' --- lcode.c | 58 ++++++++++++++++++++++++++++++++------------------------- 1 file changed, 33 insertions(+), 25 deletions(-) diff --git a/lcode.c b/lcode.c index cb724a0908..911dbd5f1e 100644 --- a/lcode.c +++ b/lcode.c @@ -1413,6 +1413,18 @@ static void codebini (FuncState *fs, OpCode op, } +/* +** Code binary operators with K operand. +*/ +static void codebinK (FuncState *fs, BinOpr opr, + expdesc *e1, expdesc *e2, int flip, int line) { + TMS event = cast(TMS, opr + TM_ADD); + int v2 = e2->u.info; /* K index */ + OpCode op = cast(OpCode, opr + OP_ADDK); + finishbinexpval(fs, e1, e2, op, v2, flip, line, OP_MMBINK, event); +} + + /* Try to code a binary operator negating its second operand. ** For the metamethod, 2nd operand must keep its original value. */ @@ -1440,24 +1452,28 @@ static void swapexps (expdesc *e1, expdesc *e2) { } +/* +** Code binary operators with no constant operand. +*/ +static void codebinNoK (FuncState *fs, BinOpr opr, + expdesc *e1, expdesc *e2, int flip, int line) { + OpCode op = cast(OpCode, opr + OP_ADD); + if (flip) + swapexps(e1, e2); /* back to original order */ + codebinexpval(fs, op, e1, e2, line); /* use standard operators */ +} + + /* ** Code arithmetic operators ('+', '-', ...). If second operand is a ** constant in the proper range, use variant opcodes with K operands. */ static void codearith (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2, int flip, int line) { - TMS event = cast(TMS, opr + TM_ADD); - if (tonumeral(e2, NULL) && luaK_exp2K(fs, e2)) { /* K operand? */ - int v2 = e2->u.info; /* K index */ - OpCode op = cast(OpCode, opr + OP_ADDK); - finishbinexpval(fs, e1, e2, op, v2, flip, line, OP_MMBINK, event); - } - else { /* 'e2' is neither an immediate nor a K operand */ - OpCode op = cast(OpCode, opr + OP_ADD); - if (flip) - swapexps(e1, e2); /* back to original order */ - codebinexpval(fs, op, e1, e2, line); /* use standard operators */ - } + if (tonumeral(e2, NULL) && luaK_exp2K(fs, e2)) /* K operand? */ + codebinK(fs, opr, e1, e2, flip, line); + else /* 'e2' is neither an immediate nor a K operand */ + codebinNoK(fs, opr, e1, e2, flip, line); } @@ -1487,22 +1503,14 @@ static void codecommutative (FuncState *fs, BinOpr op, static void codebitwise (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2, int line) { int flip = 0; - int v2; - OpCode op; - if (e1->k == VKINT && luaK_exp2K(fs, e1)) { + if (e1->k == VKINT) { swapexps(e1, e2); /* 'e2' will be the constant operand */ flip = 1; } - else if (!(e2->k == VKINT && luaK_exp2K(fs, e2))) { /* no constants? */ - op = cast(OpCode, opr + OP_ADD); - codebinexpval(fs, op, e1, e2, line); /* all-register opcodes */ - return; - } - v2 = e2->u.info; /* index in K array */ - op = cast(OpCode, opr + OP_ADDK); - lua_assert(ttisinteger(&fs->f->k[v2])); - finishbinexpval(fs, e1, e2, op, v2, flip, line, OP_MMBINK, - cast(TMS, opr + TM_ADD)); + if (e2->k == VKINT && luaK_exp2K(fs, e2)) /* K operand? */ + codebinK(fs, opr, e1, e2, flip, line); + else /* no constants */ + codebinNoK(fs, opr, e1, e2, flip, line); } From e435aaabef8e717e0812a16a82b56acd11fb34c1 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 10 May 2022 11:13:39 -0300 Subject: [PATCH 0751/1145] Details (identation and typos) --- ldo.h | 3 ++- manual/manual.of | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/ldo.h b/ldo.h index 911e67f660..4cbdb847d2 100644 --- a/ldo.h +++ b/ldo.h @@ -58,7 +58,8 @@ LUAI_FUNC int luaD_protectedparser (lua_State *L, ZIO *z, const char *name, LUAI_FUNC void luaD_hook (lua_State *L, int event, int line, int fTransfer, int nTransfer); LUAI_FUNC void luaD_hookcall (lua_State *L, CallInfo *ci); -LUAI_FUNC int luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, int narg1, int delta); +LUAI_FUNC int luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, + int narg1, int delta); LUAI_FUNC CallInfo *luaD_precall (lua_State *L, StkId func, int nResults); LUAI_FUNC void luaD_call (lua_State *L, StkId func, int nResults); LUAI_FUNC void luaD_callnoyield (lua_State *L, StkId func, int nResults); diff --git a/manual/manual.of b/manual/manual.of index bd648c6cad..30f92d6091 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -2521,7 +2521,7 @@ In general, Lua's garbage collection can free or move internal memory and then invalidate pointers to internal strings. To allow a safe use of these pointers, -The API guarantees that any pointer to a string in a stack index +the API guarantees that any pointer to a string in a stack index is valid while the string value at that index is not removed from the stack. (It can be moved to another index, though.) When the index is a pseudo-index (referring to an upvalue), @@ -5349,7 +5349,7 @@ Equivalent to the sequence @APIEntry{void luaL_buffsub (luaL_Buffer *B, int n);| @apii{?,?,-} -Removes @id{n} bytes from the the buffer @id{B} +Removes @id{n} bytes from the buffer @id{B} @seeC{luaL_Buffer}. The buffer must have at least that many bytes. From 42d40581dd919fb134c07027ca1ce0844c670daf Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 20 May 2022 13:14:33 -0300 Subject: [PATCH 0752/1145] Save stack space while handling errors Because error handling (luaG_errormsg) uses slots from EXTRA_STACK, and some errors can recur (e.g., string overflow while creating an error message in 'luaG_runerror', or a C-stack overflow before calling the message handler), the code should use stack slots with parsimony. This commit fixes the bug "Lua-stack overflow when C stack overflows while handling an error". --- ldebug.c | 5 ++++- lvm.c | 6 ++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/ldebug.c b/ldebug.c index a716d95e22..fa15eaf68e 100644 --- a/ldebug.c +++ b/ldebug.c @@ -824,8 +824,11 @@ l_noret luaG_runerror (lua_State *L, const char *fmt, ...) { va_start(argp, fmt); msg = luaO_pushvfstring(L, fmt, argp); /* format message */ va_end(argp); - if (isLua(ci)) /* if Lua function, add source:line information */ + if (isLua(ci)) { /* if Lua function, add source:line information */ luaG_addinfo(L, msg, ci_func(ci)->p->source, getcurrentline(ci)); + setobjs2s(L, L->top - 2, L->top - 1); /* remove 'msg' from the stack */ + L->top--; + } luaG_errormsg(L); } diff --git a/lvm.c b/lvm.c index e8c2e9627c..cd992aada8 100644 --- a/lvm.c +++ b/lvm.c @@ -656,8 +656,10 @@ void luaV_concat (lua_State *L, int total) { /* collect total length and number of strings */ for (n = 1; n < total && tostring(L, s2v(top - n - 1)); n++) { size_t l = vslen(s2v(top - n - 1)); - if (l_unlikely(l >= (MAX_SIZE/sizeof(char)) - tl)) + if (l_unlikely(l >= (MAX_SIZE/sizeof(char)) - tl)) { + L->top = top - total; /* pop strings to avoid wasting stack */ luaG_runerror(L, "string length overflow"); + } tl += l; } if (tl <= LUAI_MAXSHORTLEN) { /* is result a short string? */ @@ -672,7 +674,7 @@ void luaV_concat (lua_State *L, int total) { setsvalue2s(L, top - n, ts); /* create result */ } total -= n-1; /* got 'n' strings to create 1 new */ - L->top -= n-1; /* popped 'n' strings and pushed one */ + L->top = top - (n - 1); /* popped 'n' strings and pushed one */ } while (total > 1); /* repeat until only 1 result left */ } From 4a00f61276a9a38b0427fbae3dbbd86dfb5a0749 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 23 May 2022 10:38:03 -0300 Subject: [PATCH 0753/1145] 'lua_checkstack' doesn't need to check stack overflow 'luaD_growstack' already checks that. This commit also fixes an internal bug in 'luaD_growstack': a large 'n' could cause an arithmetic overflow when computing 'needed'. --- all | 2 +- lapi.c | 9 ++------- ldo.c | 15 +++++++-------- luaconf.h | 2 +- testes/coroutine.lua | 15 ++++++--------- 5 files changed, 17 insertions(+), 26 deletions(-) diff --git a/all b/all index 039f6095f6..86f38ac1c3 100755 --- a/all +++ b/all @@ -1,7 +1,7 @@ make -s -j cd testes/libs; make -s cd .. # back to directory 'testes' -ulimit -S -s 1000 +ulimit -S -s 1100 if { ../lua -W all.lua; } then echo -e "\n\n final OK!!!!\n\n" else diff --git a/lapi.c b/lapi.c index 5ee65792d2..352a385a31 100644 --- a/lapi.c +++ b/lapi.c @@ -114,13 +114,8 @@ LUA_API int lua_checkstack (lua_State *L, int n) { api_check(L, n >= 0, "negative 'n'"); if (L->stack_last - L->top > n) /* stack large enough? */ res = 1; /* yes; check is OK */ - else { /* no; need to grow stack */ - int inuse = cast_int(L->top - L->stack) + EXTRA_STACK; - if (inuse > LUAI_MAXSTACK - n) /* can grow without overflow? */ - res = 0; /* no */ - else /* try to grow stack */ - res = luaD_growstack(L, n, 0); - } + else /* need to grow stack */ + res = luaD_growstack(L, n, 0); if (res && ci->top < L->top + n) ci->top = L->top + n; /* adjust frame top */ lua_unlock(L); diff --git a/ldo.c b/ldo.c index 8e4faf0209..5aa6d59d48 100644 --- a/ldo.c +++ b/ldo.c @@ -227,7 +227,7 @@ int luaD_growstack (lua_State *L, int n, int raiseerror) { luaD_throw(L, LUA_ERRERR); /* error inside message handler */ return 0; /* if not 'raiseerror', just signal it */ } - else { + else if (n < LUAI_MAXSTACK) { /* avoids arithmetic overflows */ int newsize = 2 * size; /* tentative new size */ int needed = cast_int(L->top - L->stack) + n; if (newsize > LUAI_MAXSTACK) /* cannot cross the limit */ @@ -236,14 +236,13 @@ int luaD_growstack (lua_State *L, int n, int raiseerror) { newsize = needed; if (l_likely(newsize <= LUAI_MAXSTACK)) return luaD_reallocstack(L, newsize, raiseerror); - else { /* stack overflow */ - /* add extra size to be able to handle the error message */ - luaD_reallocstack(L, ERRORSTACKSIZE, raiseerror); - if (raiseerror) - luaG_runerror(L, "stack overflow"); - return 0; - } } + /* else stack overflow */ + /* add extra size to be able to handle the error message */ + luaD_reallocstack(L, ERRORSTACKSIZE, raiseerror); + if (raiseerror) + luaG_runerror(L, "stack overflow"); + return 0; } diff --git a/luaconf.h b/luaconf.h index d42d14b7d5..fcc0018b3a 100644 --- a/luaconf.h +++ b/luaconf.h @@ -728,7 +728,7 @@ ** CHANGE it if you need a different limit. This limit is arbitrary; ** its only purpose is to stop Lua from consuming unlimited stack ** space (and to reserve some numbers for pseudo-indices). -** (It must fit into max(size_t)/32.) +** (It must fit into max(size_t)/32 and max(int)/2.) */ #if LUAI_IS32INT #define LUAI_MAXSTACK 1000000 diff --git a/testes/coroutine.lua b/testes/coroutine.lua index 76c9d6e635..15fccc3083 100644 --- a/testes/coroutine.lua +++ b/testes/coroutine.lua @@ -741,20 +741,17 @@ _X() if not _soft then -- bug (stack overflow) - local j = 2^9 - local lim = 1000000 -- (C stack limit; assume 32-bit machine) - local t = {lim - 10, lim - 5, lim - 1, lim, lim + 1} + local lim = 1000000 -- stack limit; assume 32-bit machine + local t = {lim - 10, lim - 5, lim - 1, lim, lim + 1, lim + 5} for i = 1, #t do local j = t[i] - co = coroutine.create(function() - local t = {} - for i = 1, j do t[i] = i end - return table.unpack(t) + local co = coroutine.create(function() + return table.unpack({}, 1, j) end) local r, msg = coroutine.resume(co) - assert(not r) + -- must fail for unpacking larger than stack limit + assert(j < lim or not r) end - co = nil end From 603b2c64add5fbf4b7343525cf109af0c7077695 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 23 May 2022 17:50:47 -0300 Subject: [PATCH 0754/1145] 'luaV_concat' can use invalidated pointer to stack Bug introduced in commit 42d40581. --- lvm.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lvm.c b/lvm.c index cd992aada8..614df05573 100644 --- a/lvm.c +++ b/lvm.c @@ -643,7 +643,7 @@ void luaV_concat (lua_State *L, int total) { int n = 2; /* number of elements handled in this pass (at least 2) */ if (!(ttisstring(s2v(top - 2)) || cvt2str(s2v(top - 2))) || !tostring(L, s2v(top - 1))) - luaT_tryconcatTM(L); + luaT_tryconcatTM(L); /* may invalidate 'top' */ else if (isemptystr(s2v(top - 1))) /* second operand is empty? */ cast_void(tostring(L, s2v(top - 2))); /* result is first operand */ else if (isemptystr(s2v(top - 2))) { /* first operand is empty string? */ @@ -673,8 +673,8 @@ void luaV_concat (lua_State *L, int total) { } setsvalue2s(L, top - n, ts); /* create result */ } - total -= n-1; /* got 'n' strings to create 1 new */ - L->top = top - (n - 1); /* popped 'n' strings and pushed one */ + total -= n - 1; /* got 'n' strings to create one new */ + L->top -= n - 1; /* popped 'n' strings and pushed one */ } while (total > 1); /* repeat until only 1 result left */ } From 196bb94d66e727e0aec053a0276c3ad701500762 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 25 May 2022 17:41:39 -0300 Subject: [PATCH 0755/1145] Bug: 'lua_settop' may use an invalid pointer to stack --- lapi.c | 5 ++--- ldo.c | 12 ++++++------ lfunc.c | 5 +++-- lfunc.h | 2 +- testes/locals.lua | 22 ++++++++++++++++++++++ 5 files changed, 34 insertions(+), 12 deletions(-) diff --git a/lapi.c b/lapi.c index 352a385a31..5833c7b0a0 100644 --- a/lapi.c +++ b/lapi.c @@ -197,7 +197,7 @@ LUA_API void lua_settop (lua_State *L, int idx) { newtop = L->top + diff; if (diff < 0 && L->tbclist >= newtop) { lua_assert(hastocloseCfunc(ci->nresults)); - luaF_close(L, newtop, CLOSEKTOP, 0); + newtop = luaF_close(L, newtop, CLOSEKTOP, 0); } L->top = newtop; /* correct top only after closing any upvalue */ lua_unlock(L); @@ -210,8 +210,7 @@ LUA_API void lua_closeslot (lua_State *L, int idx) { level = index2stack(L, idx); api_check(L, hastocloseCfunc(L->ci->nresults) && L->tbclist == level, "no variable to close at given level"); - luaF_close(L, level, CLOSEKTOP, 0); - level = index2stack(L, idx); /* stack may be moved */ + level = luaF_close(L, level, CLOSEKTOP, 0); setnilvalue(s2v(level)); lua_unlock(L); } diff --git a/ldo.c b/ldo.c index 5aa6d59d48..13498905f6 100644 --- a/ldo.c +++ b/ldo.c @@ -430,14 +430,15 @@ l_sinline void moveresults (lua_State *L, StkId res, int nres, int wanted) { break; default: /* two/more results and/or to-be-closed variables */ if (hastocloseCfunc(wanted)) { /* to-be-closed variables? */ - ptrdiff_t savedres = savestack(L, res); L->ci->callstatus |= CIST_CLSRET; /* in case of yields */ L->ci->u2.nres = nres; - luaF_close(L, res, CLOSEKTOP, 1); + res = luaF_close(L, res, CLOSEKTOP, 1); L->ci->callstatus &= ~CIST_CLSRET; - if (L->hookmask) /* if needed, call hook after '__close's */ + if (L->hookmask) { /* if needed, call hook after '__close's */ + ptrdiff_t savedres = savestack(L, res); rethook(L, L->ci, nres); - res = restorestack(L, savedres); /* close and hook can move stack */ + res = restorestack(L, savedres); /* hook can move stack */ + } wanted = decodeNresults(wanted); if (wanted == LUA_MULTRET) wanted = nres; /* we want all results */ @@ -654,8 +655,7 @@ static int finishpcallk (lua_State *L, CallInfo *ci) { else { /* error */ StkId func = restorestack(L, ci->u2.funcidx); L->allowhook = getoah(ci->callstatus); /* restore 'allowhook' */ - luaF_close(L, func, status, 1); /* can yield or raise an error */ - func = restorestack(L, ci->u2.funcidx); /* stack may be moved */ + func = luaF_close(L, func, status, 1); /* can yield or raise an error */ luaD_seterrorobj(L, status, func); luaD_shrinkstack(L); /* restore stack size in case of overflow */ setcistrecst(ci, LUA_OK); /* clear original status */ diff --git a/lfunc.c b/lfunc.c index f5889a21d1..3ed65de2b5 100644 --- a/lfunc.c +++ b/lfunc.c @@ -223,9 +223,9 @@ static void poptbclist (lua_State *L) { /* ** Close all upvalues and to-be-closed variables up to the given stack -** level. +** level. Return restored 'level'. */ -void luaF_close (lua_State *L, StkId level, int status, int yy) { +StkId luaF_close (lua_State *L, StkId level, int status, int yy) { ptrdiff_t levelrel = savestack(L, level); luaF_closeupval(L, level); /* first, close the upvalues */ while (L->tbclist >= level) { /* traverse tbc's down to that level */ @@ -234,6 +234,7 @@ void luaF_close (lua_State *L, StkId level, int status, int yy) { prepcallclosemth(L, tbc, status, yy); /* close variable */ level = restorestack(L, levelrel); } + return level; } diff --git a/lfunc.h b/lfunc.h index dc1cebccd1..3d296971ec 100644 --- a/lfunc.h +++ b/lfunc.h @@ -54,7 +54,7 @@ LUAI_FUNC void luaF_initupvals (lua_State *L, LClosure *cl); LUAI_FUNC UpVal *luaF_findupval (lua_State *L, StkId level); LUAI_FUNC void luaF_newtbcupval (lua_State *L, StkId level); LUAI_FUNC void luaF_closeupval (lua_State *L, StkId level); -LUAI_FUNC void luaF_close (lua_State *L, StkId level, int status, int yy); +LUAI_FUNC StkId luaF_close (lua_State *L, StkId level, int status, int yy); LUAI_FUNC void luaF_unlinkupval (UpVal *uv); LUAI_FUNC void luaF_freeproto (lua_State *L, Proto *f); LUAI_FUNC const char *luaF_getlocalname (const Proto *func, int local_number, diff --git a/testes/locals.lua b/testes/locals.lua index 62a88df57b..ddb75054fc 100644 --- a/testes/locals.lua +++ b/testes/locals.lua @@ -592,6 +592,28 @@ end if rawget(_G, "T") then + do + -- bug in 5.4.3 + -- 'lua_settop' may use a pointer to stack invalidated by 'luaF_close' + + -- reduce stack size + collectgarbage(); collectgarbage(); collectgarbage() + + -- force a stack reallocation + local function loop (n) + if n < 400 then loop(n + 1) end + end + + -- close metamethod will reallocate the stack + local o = setmetatable({}, {__close = function () loop(0) end}) + + local script = [[toclose 2; settop 1; return 1]] + + assert(T.testC(script, o) == script) + + end + + -- memory error inside closing function local function foo () local y = func2close(function () T.alloccount() end) From d61b0c60287c38008d312ddd11724a15b1737f7b Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 26 May 2022 15:14:54 -0300 Subject: [PATCH 0756/1145] More checks and documentation for uses of EXTRA_STACK --- ldo.c | 7 ++++++- ldo.h | 7 +++++++ lobject.c | 34 ++++++++++++++++++++++------------ lobject.h | 2 ++ testes/calls.lua | 10 ++++++++++ 5 files changed, 47 insertions(+), 13 deletions(-) diff --git a/ldo.c b/ldo.c index 13498905f6..419b3db93f 100644 --- a/ldo.c +++ b/ldo.c @@ -602,12 +602,17 @@ CallInfo *luaD_precall (lua_State *L, StkId func, int nresults) { ** Call a function (C or Lua) through C. 'inc' can be 1 (increment ** number of recursive invocations in the C stack) or nyci (the same ** plus increment number of non-yieldable calls). +** This function can be called with some use of EXTRA_STACK, so it should +** check the stack before doing anything else. 'luaD_precall' already +** does that. */ l_sinline void ccall (lua_State *L, StkId func, int nResults, int inc) { CallInfo *ci; L->nCcalls += inc; - if (l_unlikely(getCcalls(L) >= LUAI_MAXCCALLS)) + if (l_unlikely(getCcalls(L) >= LUAI_MAXCCALLS)) { + checkstackp(L, 0, func); /* free any use of EXTRA_STACK */ luaE_checkcstack(L); + } if ((ci = luaD_precall(L, func, nResults)) != NULL) { /* Lua function? */ ci->callstatus = CIST_FRESH; /* mark that it is a "fresh" execute */ luaV_execute(L, ci); /* call it */ diff --git a/ldo.h b/ldo.h index 4cbdb847d2..4661aa0078 100644 --- a/ldo.h +++ b/ldo.h @@ -37,6 +37,13 @@ /* macro to check stack size, preserving 'p' */ +#define checkstackp(L,n,p) \ + luaD_checkstackaux(L, n, \ + ptrdiff_t t__ = savestack(L, p), /* save 'p' */ \ + p = restorestack(L, t__)) /* 'pos' part: restore 'p' */ + + +/* macro to check stack size and GC, preserving 'p' */ #define checkstackGCp(L,n,p) \ luaD_checkstackaux(L, n, \ ptrdiff_t t__ = savestack(L, p); /* save 'p' */ \ diff --git a/lobject.c b/lobject.c index 301aa900be..a2c006098b 100644 --- a/lobject.c +++ b/lobject.c @@ -386,29 +386,39 @@ void luaO_tostring (lua_State *L, TValue *obj) { ** =================================================================== */ -/* size for buffer space used by 'luaO_pushvfstring' */ -#define BUFVFS 200 +/* +** Size for buffer space used by 'luaO_pushvfstring'. It should be +** (LUA_IDSIZE + MAXNUMBER2STR) + a minimal space for basic messages, +** so that 'luaG_addinfo' can work directly on the buffer. +*/ +#define BUFVFS (LUA_IDSIZE + MAXNUMBER2STR + 95) /* buffer used by 'luaO_pushvfstring' */ typedef struct BuffFS { lua_State *L; - int pushed; /* number of string pieces already on the stack */ + int pushed; /* true if there is a part of the result on the stack */ int blen; /* length of partial string in 'space' */ char space[BUFVFS]; /* holds last part of the result */ } BuffFS; /* -** Push given string to the stack, as part of the buffer, and -** join the partial strings in the stack into one. +** Push given string to the stack, as part of the result, and +** join it to previous partial result if there is one. +** It may call 'luaV_concat' while using one slot from EXTRA_STACK. +** This call cannot invoke metamethods, as both operands must be +** strings. It can, however, raise an error if the result is too +** long. In that case, 'luaV_concat' frees the extra slot before +** raising the error. */ -static void pushstr (BuffFS *buff, const char *str, size_t l) { +static void pushstr (BuffFS *buff, const char *str, size_t lstr) { lua_State *L = buff->L; - setsvalue2s(L, L->top, luaS_newlstr(L, str, l)); - L->top++; /* may use one extra slot */ - buff->pushed++; - luaV_concat(L, buff->pushed); /* join partial results into one */ - buff->pushed = 1; + setsvalue2s(L, L->top, luaS_newlstr(L, str, lstr)); + L->top++; /* may use one slot from EXTRA_STACK */ + if (!buff->pushed) /* no previous string on the stack? */ + buff->pushed = 1; /* now there is one */ + else /* join previous string with new one */ + luaV_concat(L, 2); } @@ -454,7 +464,7 @@ static void addstr2buff (BuffFS *buff, const char *str, size_t slen) { /* -** Add a number to the buffer. +** Add a numeral to the buffer. */ static void addnum2buff (BuffFS *buff, TValue *num) { char *numbuff = getbuff(buff, MAXNUMBER2STR); diff --git a/lobject.h b/lobject.h index 0e05b3e42e..77cc606f52 100644 --- a/lobject.h +++ b/lobject.h @@ -52,6 +52,8 @@ typedef union Value { lua_CFunction f; /* light C functions */ lua_Integer i; /* integer numbers */ lua_Number n; /* float numbers */ + /* not used, but may avoid warnings for uninitialized value */ + lu_byte ub; } Value; diff --git a/testes/calls.lua b/testes/calls.lua index ff72d8f6cf..ee8cce7333 100644 --- a/testes/calls.lua +++ b/testes/calls.lua @@ -151,6 +151,16 @@ do -- tail calls x varargs end +do -- C-stack overflow while handling C-stack overflow + local function loop () + assert(pcall(loop)) + end + + local err, msg = xpcall(loop, loop) + assert(not err and string.find(msg, "error")) +end + + do -- tail calls x chain of __call local n = 10000 -- depth From c6cea857a4845940c833e39a149d20bb64a9af85 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 19 Aug 2022 14:10:18 -0300 Subject: [PATCH 0757/1145] Better documentation for 'multires' expressions Manual has a new section explaining multires expressions, lists of expressions, and adjustments. This commit also corrects some comments in the code. --- lauxlib.c | 5 +- lfunc.c | 2 +- manual/manual.of | 188 +++++++++++++++++++++++++++++------------------ 3 files changed, 120 insertions(+), 75 deletions(-) diff --git a/lauxlib.c b/lauxlib.c index 8ed1da1122..413d8f977e 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -526,7 +526,8 @@ static void newbox (lua_State *L) { /* ** Compute new size for buffer 'B', enough to accommodate extra 'sz' -** bytes. +** bytes. (The test for "double is not big enough" also gets the +** case when the multiplication by 2 overflows.) */ static size_t newbuffsize (luaL_Buffer *B, size_t sz) { size_t newsize = B->size * 2; /* double buffer size */ @@ -611,7 +612,7 @@ LUALIB_API void luaL_pushresultsize (luaL_Buffer *B, size_t sz) { ** box (if existent) is not on the top of the stack. So, instead of ** calling 'luaL_addlstring', it replicates the code using -2 as the ** last argument to 'prepbuffsize', signaling that the box is (or will -** be) bellow the string being added to the buffer. (Box creation can +** be) below the string being added to the buffer. (Box creation can ** trigger an emergency GC, so we should not remove the string from the ** stack before we have the space guaranteed.) */ diff --git a/lfunc.c b/lfunc.c index 3ed65de2b5..daba0abf5c 100644 --- a/lfunc.c +++ b/lfunc.c @@ -209,7 +209,7 @@ void luaF_closeupval (lua_State *L, StkId level) { /* -** Remove firt element from the tbclist plus its dummy nodes. +** Remove first element from the tbclist plus its dummy nodes. */ static void poptbclist (lua_State *L) { StkId tbc = L->tbclist; diff --git a/manual/manual.of b/manual/manual.of index 30f92d6091..ca7f993371 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -1333,19 +1333,11 @@ Expressions are discussed in @See{expressions}. Before the assignment, the list of values is @emph{adjusted} to the length of -the list of variables.@index{adjustment} -If there are more values than needed, -the excess values are thrown away. -If there are fewer values than needed, -the list is extended with @nil's. -If the list of expressions ends with a function call, -then all values returned by that call enter the list of values, -before the adjustment -(except when the call is enclosed in parentheses; see @See{expressions}). +the list of variables @see{multires}. If a variable is both assigned and read inside a multiple assignment, -Lua ensures all reads get the value of the variable +Lua ensures that all reads get the value of the variable before the assignment. Thus the code @verbatim{ @@ -1684,9 +1676,10 @@ function calls are explained in @See{functioncall}; table constructors are explained in @See{tableconstructor}. Vararg expressions, denoted by three dots (@Char{...}), can only be used when -directly inside a vararg function; +directly inside a variadic function; they are explained in @See{func-def}. + Binary operators comprise arithmetic operators @see{arith}, bitwise operators @see{bitwise}, relational operators @see{rel-ops}, logical operators @see{logic}, @@ -1696,47 +1689,8 @@ the unary bitwise NOT @see{bitwise}, the unary logical @Rw{not} @see{logic}, and the unary @def{length operator} @see{len-op}. -Both function calls and vararg expressions can result in multiple values. -If a function call is used as a statement @see{funcstat}, -then its return list is adjusted to zero elements, -thus discarding all returned values. -If an expression is used as the last (or the only) element -of a list of expressions, -then no adjustment is made -(unless the expression is enclosed in parentheses). -In all other contexts, -Lua adjusts the result list to one element, -either discarding all values except the first one -or adding a single @nil if there are no values. - -Here are some examples: -@verbatim{ -f() -- adjusted to 0 results -g(f(), x) -- f() is adjusted to 1 result -g(x, f()) -- g gets x plus all results from f() -a,b,c = f(), x -- f() is adjusted to 1 result (c gets nil) -a,b = ... -- a gets the first vararg argument, b gets - -- the second (both a and b can get nil if there - -- is no corresponding vararg argument) - -a,b,c = x, f() -- f() is adjusted to 2 results -a,b,c = f() -- f() is adjusted to 3 results -return f() -- returns all results from f() -return ... -- returns all received vararg arguments -return x,y,f() -- returns x, y, and all results from f() -{f()} -- creates a list with all results from f() -{...} -- creates a list with all vararg arguments -{f(), nil} -- f() is adjusted to 1 result } -Any expression enclosed in parentheses always results in only one value. -Thus, -@T{(f(x,y,z))} is always a single value, -even if @id{f} returns several values. -(The value of @T{(f(x,y,z))} is the first value returned by @id{f} -or @nil if @id{f} does not return any values.) - -} @sect3{arith| @title{Arithmetic Operators} @@ -1843,8 +1797,9 @@ the library calls the metamethod of the other operand (if present) or it raises an error. Note that bitwise operators do not do this coercion. -Nonetheless, it is always a good practice not to rely on these -implicit coercions, as they are not always applied; +It is always a good practice not to rely on the +implicit coercions from strings to numbers, +as they are not always applied; in particular, @T{"1"==1} is false and @T{"1"<1} raises an error @see{rel-ops}. These coercions exist mainly for compatibility and may be removed @@ -2095,9 +2050,9 @@ The order of the assignments in a constructor is undefined. (This order would be relevant only when there are repeated keys.) If the last field in the list has the form @id{exp} -and the expression is a function call or a vararg expression, +and the expression is a multires expression, then all values returned by this expression enter the list consecutively -@see{functioncall}. +@see{multires}. The field list can have an optional trailing separator, as a convenience for machine-generated code. @@ -2148,7 +2103,7 @@ A call of the form @T{return @rep{functioncall}} not in the scope of a to-be-closed variable is called a @def{tail call}. Lua implements @def{proper tail calls} (or @def{proper tail recursion}): -in a tail call, +In a tail call, the called function reuses the stack entry of the calling function. Therefore, there is no limit on the number of nested tail calls that a program can execute. @@ -2234,22 +2189,16 @@ initialized with the argument values: } When a Lua function is called, it adjusts its list of @x{arguments} to -the length of its list of parameters, -unless the function is a @def{vararg function}, +the length of its list of parameters @see{multires}, +unless the function is a @def{variadic function}, which is indicated by three dots (@Char{...}) at the end of its parameter list. -A vararg function does not adjust its argument list; +A variadic function does not adjust its argument list; instead, it collects all extra arguments and supplies them to the function through a @def{vararg expression}, which is also written as three dots. The value of this expression is a list of all actual extra arguments, -similar to a function with multiple results. -If a vararg expression is used inside another expression -or in the middle of a list of expressions, -then its return list is adjusted to one element. -If the expression is used as the last element of a list of expressions, -then no adjustment is made -(unless that last expression is enclosed in parentheses). +similar to a function with multiple results @see{multires}. As an example, consider the following definitions: @@ -2299,6 +2248,99 @@ t.a.b.c.f = function (self, @rep{params}) @rep{body} end } +@sect3{multires| @title{Lists of expressions, multiple results, +and adjustment} + +Both function calls and vararg expressions can result in multiple values. +These expressions are called @def{multires expressions}. + +When a multires expression is used as the last element +of a list of expressions, +all results from the expression are added to the +list of values produced by the list of expressions. +Note that a single expression +in a place that expects a list of expressions +is the last expression in that (singleton) list. + +These are the places where Lua expects a list of expressions: +@description{ + +@item{A @rw{return} statement, +for instance @T{return e1, e2, e3} @see{control}.} + +@item{A table constructor, +for instance @T{{e1, e2, e3}} @see{tableconstructor}.} + +@item{The arguments of a function call, +for instance @T{foo(e1, e2, e3)} @see{functioncall}.} + +@item{A multiple assignment, +for instance @T{a , b, c = e1, e2, e3} @see{assignment}.} + +@item{A local declaration, +for instance @T{local a , b, c = e1, e2, e3} @see{localvar}.} + +@item{The initial values in a generic @rw{for} loop, +for instance @T{for k in e1, e2, e3 do ... end} @see{for}.} + +} +In the last four cases, +the list of values from the list of expressions +must be @emph{adjusted} to a specific length: +the number of parameters in a call to a non-variadic function +@see{func-def}, +the number of variables in a multiple assignment or +a local declaration, +and exactly four for a generic @rw{for} loop. +The @def{adjustment} follows these rules: +If there are more values than needed, +the extra values are thrown away; +if there are fewer values than needed, +the list is extended with @nil's. +When the list of expressions ends with a multires expression, +all results from that expression enter the list of values +before the adjustment. + +When a multires expression is used +in a list of expressions without being the last element, +or in a place where the syntax expects a single expression, +Lua adjusts the result list of that expression to one element. +As a particular case, +the syntax expects a single expression inside a parenthesized expression; +therefore, adding parentheses around a multires expression +forces it to produce exactly one result. + +Here are some examples. +In all cases, when the construction needs +@Q{the n-th result} and there is no such result, +it uses a @nil. +@verbatim{ +print(x, f()) -- prints x and all results from f(). +print(x, (f())) -- prints x and the first result from f(). +print(f(), x) -- prints the first result from f() and x. +print(1 + f()) -- prints 1 added to the first result from f(). +x,y = ... -- x gets the first vararg argument, + -- y gets the second vararg argument. +x,y,z = w, f() -- x gets w, y gets the first result from f(), + -- z gets the second result from f(). +x,y,z = f() -- x gets the first result from f(), + -- y gets the second result from f(), + -- z gets the third result from f(). +x,y,z = f(), g() -- x gets the first result from f(), + -- y gets the first result from g(), + -- z gets the second result from g(). +x,y,z = (f()) -- x gets the first result from f(), y and z get nil. +return f() -- returns all results from f(). +return ... -- returns all received vararg arguments. +return (...) -- returns the first received vararg argument. +return x,y,f() -- returns x, y, and all results from f(). +{f()} -- creates a list with all results from f(). +{...} -- creates a list with all vararg arguments. +{f(), 5} -- creates a list with the first result from f() and 5. +} + +} + } @sect2{visibility| @title{Visibility Rules} @@ -4780,7 +4822,7 @@ the number of parameters of the function } @item{@id{isvararg}| -true if the function is a vararg function +true if the function is a variadic function (always true for @N{C functions}). } @@ -6017,9 +6059,7 @@ to start the traceback. } -@APIEntry{const char *luaL_typeerror (lua_State *L, - int arg, - const char *tname);| +@APIEntry{int luaL_typeerror (lua_State *L, int arg, const char *tname);| @apii{0,0,v} Raises a type error for the argument @id{arg} @@ -6816,6 +6856,8 @@ When you require a module @id{modname} and This variable is only a reference to the real table; assignments to this variable do not change the table used by @Lid{require}. +The real table is stored in the C registry @see{registry}, +indexed by the key @defid{LUA_LOADED_TABLE}, a string. } @@ -6883,6 +6925,8 @@ A table to store loaders for specific modules This variable is only a reference to the real table; assignments to this variable do not change the table used by @Lid{require}. +The real table is stored in the C registry @see{registry}, +indexed by the key @defid{LUA_PRELOAD_TABLE}, a string. } @@ -7904,9 +7948,9 @@ Returns the arc sine of @id{x} (in radians). @LibEntry{math.atan (y [, x])| -@index{atan2} +@index{atan} @index{atan2} Returns the arc tangent of @T{y/x} (in radians), -but uses the signs of both arguments to find the +using the signs of both arguments to find the quadrant of the result. It also handles correctly the case of @id{x} being zero. @@ -8997,7 +9041,7 @@ If there is a script, the script is called with arguments @T{arg[1]}, @Cdots, @T{arg[#arg]}. Like all chunks in Lua, -the script is compiled as a vararg function. +the script is compiled as a variadic function. In interactive mode, Lua repeatedly prompts and waits for a line. From a1f77a234a053da46b06d5d4be00ffb30d3eb45b Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 23 Aug 2022 16:06:23 -0300 Subject: [PATCH 0758/1145] Bug: set correct pause when (re)entering gen. collection. --- lgc.c | 63 +++++++++++++++++++++++++++++------------------------------ 1 file changed, 31 insertions(+), 32 deletions(-) diff --git a/lgc.c b/lgc.c index 42a73d813a..317ea45081 100644 --- a/lgc.c +++ b/lgc.c @@ -1041,7 +1041,25 @@ void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt) { ** ======================================================= */ -static void setpause (global_State *g); + +/* +** Set the "time" to wait before starting a new GC cycle; cycle will +** start when memory use hits the threshold of ('estimate' * pause / +** PAUSEADJ). (Division by 'estimate' should be OK: it cannot be zero, +** because Lua cannot even start with less than PAUSEADJ bytes). +*/ +static void setpause (global_State *g) { + l_mem threshold, debt; + int pause = getgcparam(g->gcpause); + l_mem estimate = g->GCestimate / PAUSEADJ; /* adjust 'estimate' */ + lua_assert(estimate > 0); + threshold = (pause < MAX_LMEM / estimate) /* overflow? */ + ? estimate * pause /* no overflow */ + : MAX_LMEM; /* overflow; truncate to maximum */ + debt = gettotalbytes(g) - threshold; + if (debt > 0) debt = 0; + luaE_setdebt(g, debt); +} /* @@ -1285,6 +1303,15 @@ static void atomic2gen (lua_State *L, global_State *g) { } +/* +** Set debt for the next minor collection, which will happen when +** memory grows 'genminormul'%. +*/ +static void setminordebt (global_State *g) { + luaE_setdebt(g, -(cast(l_mem, (gettotalbytes(g) / 100)) * g->genminormul)); +} + + /* ** Enter generational mode. Must go until the end of an atomic cycle ** to ensure that all objects are correctly marked and weak tables @@ -1297,6 +1324,7 @@ static lu_mem entergen (lua_State *L, global_State *g) { luaC_runtilstate(L, bitmask(GCSpropagate)); /* start new cycle */ numobjs = atomic(L); /* propagates all and then do the atomic stuff */ atomic2gen(L, g); + setminordebt(g); /* set debt assuming next cycle will be minor */ return numobjs; } @@ -1342,15 +1370,6 @@ static lu_mem fullgen (lua_State *L, global_State *g) { } -/* -** Set debt for the next minor collection, which will happen when -** memory grows 'genminormul'%. -*/ -static void setminordebt (global_State *g) { - luaE_setdebt(g, -(cast(l_mem, (gettotalbytes(g) / 100)) * g->genminormul)); -} - - /* ** Does a major collection after last collection was a "bad collection". ** @@ -1422,8 +1441,8 @@ static void genstep (lua_State *L, global_State *g) { lu_mem numobjs = fullgen(L, g); /* do a major collection */ if (gettotalbytes(g) < majorbase + (majorinc / 2)) { /* collected at least half of memory growth since last major - collection; keep doing minor collections */ - setminordebt(g); + collection; keep doing minor collections. */ + lua_assert(g->lastatomic == 0); } else { /* bad collection */ g->lastatomic = numobjs; /* signal that last collection was bad */ @@ -1449,26 +1468,6 @@ static void genstep (lua_State *L, global_State *g) { */ -/* -** Set the "time" to wait before starting a new GC cycle; cycle will -** start when memory use hits the threshold of ('estimate' * pause / -** PAUSEADJ). (Division by 'estimate' should be OK: it cannot be zero, -** because Lua cannot even start with less than PAUSEADJ bytes). -*/ -static void setpause (global_State *g) { - l_mem threshold, debt; - int pause = getgcparam(g->gcpause); - l_mem estimate = g->GCestimate / PAUSEADJ; /* adjust 'estimate' */ - lua_assert(estimate > 0); - threshold = (pause < MAX_LMEM / estimate) /* overflow? */ - ? estimate * pause /* no overflow */ - : MAX_LMEM; /* overflow; truncate to maximum */ - debt = gettotalbytes(g) - threshold; - if (debt > 0) debt = 0; - luaE_setdebt(g, debt); -} - - /* ** Enter first sweep phase. ** The call to 'sweeptolive' makes the pointer point to an object From 02060b7a37d88d4e92cf64a008c0651eae432c12 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 23 Aug 2022 16:08:53 -0300 Subject: [PATCH 0759/1145] Simpler handling of Byte Order Mark (BOM) --- lauxlib.c | 47 ++++++++++++++++++++++++++--------------------- testes/main.lua | 35 ++++++++++++++++++++++++++++++----- 2 files changed, 56 insertions(+), 26 deletions(-) diff --git a/lauxlib.c b/lauxlib.c index 413d8f977e..cba5df9b25 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -740,17 +740,18 @@ static int errfile (lua_State *L, const char *what, int fnameindex) { } -static int skipBOM (LoadF *lf) { - const char *p = "\xEF\xBB\xBF"; /* UTF-8 BOM mark */ - int c; - lf->n = 0; - do { - c = getc(lf->f); - if (c == EOF || c != *(const unsigned char *)p++) return c; - lf->buff[lf->n++] = c; /* to be read by the parser */ - } while (*p != '\0'); - lf->n = 0; /* prefix matched; discard it */ - return getc(lf->f); /* return next character */ +/* +** Skip an optional BOM at the start of a stream. If there is an +** incomplete BOM (the first character is correct but the rest is +** not), returns the first character anyway to force an error +** (as no chunk can start with 0xEF). +*/ +static int skipBOM (FILE *f) { + int c = getc(f); /* read first character */ + if (c == 0xEF && getc(f) == 0xBB && getc(f) == 0xBF) /* correct BOM? */ + return getc(f); /* ignore BOM and return next char */ + else /* no (valid) BOM */ + return c; /* return first character */ } @@ -761,13 +762,13 @@ static int skipBOM (LoadF *lf) { ** first "valid" character of the file (after the optional BOM and ** a first-line comment). */ -static int skipcomment (LoadF *lf, int *cp) { - int c = *cp = skipBOM(lf); +static int skipcomment (FILE *f, int *cp) { + int c = *cp = skipBOM(f); if (c == '#') { /* first line is a comment (Unix exec. file)? */ do { /* skip first line */ - c = getc(lf->f); + c = getc(f); } while (c != EOF && c != '\n'); - *cp = getc(lf->f); /* skip end-of-line, if present */ + *cp = getc(f); /* next character after comment, if present */ return 1; /* there was a comment */ } else return 0; /* no comment */ @@ -789,12 +790,16 @@ LUALIB_API int luaL_loadfilex (lua_State *L, const char *filename, lf.f = fopen(filename, "r"); if (lf.f == NULL) return errfile(L, "open", fnameindex); } - if (skipcomment(&lf, &c)) /* read initial portion */ - lf.buff[lf.n++] = '\n'; /* add line to correct line numbers */ - if (c == LUA_SIGNATURE[0] && filename) { /* binary file? */ - lf.f = freopen(filename, "rb", lf.f); /* reopen in binary mode */ - if (lf.f == NULL) return errfile(L, "reopen", fnameindex); - skipcomment(&lf, &c); /* re-read initial portion */ + lf.n = 0; + if (skipcomment(lf.f, &c)) /* read initial portion */ + lf.buff[lf.n++] = '\n'; /* add newline to correct line numbers */ + if (c == LUA_SIGNATURE[0]) { /* binary file? */ + lf.n = 0; /* remove possible newline */ + if (filename) { /* "real" file? */ + lf.f = freopen(filename, "rb", lf.f); /* reopen in binary mode */ + if (lf.f == NULL) return errfile(L, "reopen", fnameindex); + skipcomment(lf.f, &c); /* re-read initial portion */ + } } if (c != EOF) lf.buff[lf.n++] = c; /* 'c' is the first character of the stream */ diff --git a/testes/main.lua b/testes/main.lua index 9def63860d..9187420ef5 100644 --- a/testes/main.lua +++ b/testes/main.lua @@ -94,6 +94,33 @@ RUN('echo "print(10)\nprint(2)\n" | lua > %s', out) checkout("10\n2\n") +-- testing BOM +prepfile("\xEF\xBB\xBF") +RUN('lua %s > %s', prog, out) +checkout("") + +prepfile("\xEF\xBB\xBFprint(3)") +RUN('lua %s > %s', prog, out) +checkout("3\n") + +prepfile("\xEF\xBB\xBF# comment!!\nprint(3)") +RUN('lua %s > %s', prog, out) +checkout("3\n") + +-- bad BOMs +prepfile("\xEF") +NoRun("unexpected symbol", 'lua %s > %s', prog, out) + +prepfile("\xEF\xBB") +NoRun("unexpected symbol", 'lua %s > %s', prog, out) + +prepfile("\xEFprint(3)") +NoRun("unexpected symbol", 'lua %s > %s', prog, out) + +prepfile("\xEF\xBBprint(3)") +NoRun("unexpected symbol", 'lua %s > %s', prog, out) + + -- test option '-' RUN('echo "print(arg[1])" | lua - -h > %s', out) checkout("-h\n") @@ -385,12 +412,10 @@ checkprogout("101\n13\t22\n\n") prepfile[[#comment in 1st line without \n at the end]] RUN('lua %s', prog) -prepfile[[#test line number when file starts with comment line -debug = require"debug" -print(debug.getinfo(1).currentline) -]] +-- first-line comment with binary file +prepfile("#comment\n" .. string.dump(load("print(3)"))) RUN('lua %s > %s', prog, out) -checkprogout('3\n') +checkout('3\n') -- close Lua with an open file prepfile(string.format([[io.output(%q); io.write('alo')]], out)) From 997f11f54322883c3181225f29d101a597f31730 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 24 Aug 2022 17:36:47 -0300 Subject: [PATCH 0760/1145] Bug: 'break' may not properly close variable in a 'for' loop Function 'leaveblock' was generating "break" label before removing variables from the closing block. If 'createlabel' created a 'close' instruction (which it did when matching a goto/break that exited the scope of an upvalue), that instruction would use the wrong level. --- lparser.c | 16 ++++++++-------- testes/locals.lua | 20 ++++++++++++++++++++ 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/lparser.c b/lparser.c index a5cd552578..fe693b5718 100644 --- a/lparser.c +++ b/lparser.c @@ -674,19 +674,19 @@ static void leaveblock (FuncState *fs) { LexState *ls = fs->ls; int hasclose = 0; int stklevel = reglevel(fs, bl->nactvar); /* level outside the block */ - if (bl->isloop) /* fix pending breaks? */ + removevars(fs, bl->nactvar); /* remove block locals */ + lua_assert(bl->nactvar == fs->nactvar); /* back to level on entry */ + if (bl->isloop) /* has to fix pending breaks? */ hasclose = createlabel(ls, luaS_newliteral(ls->L, "break"), 0, 0); - if (!hasclose && bl->previous && bl->upval) + if (!hasclose && bl->previous && bl->upval) /* still need a 'close'? */ luaK_codeABC(fs, OP_CLOSE, stklevel, 0, 0); - fs->bl = bl->previous; - removevars(fs, bl->nactvar); - lua_assert(bl->nactvar == fs->nactvar); fs->freereg = stklevel; /* free registers */ ls->dyd->label.n = bl->firstlabel; /* remove local labels */ - if (bl->previous) /* inner block? */ - movegotosout(fs, bl); /* update pending gotos to outer block */ + fs->bl = bl->previous; /* current block now is previous one */ + if (bl->previous) /* was it a nested block? */ + movegotosout(fs, bl); /* update pending gotos to enclosing block */ else { - if (bl->firstgoto < ls->dyd->gt.n) /* pending gotos in outer block? */ + if (bl->firstgoto < ls->dyd->gt.n) /* still pending gotos? */ undefgoto(ls, &ls->dyd->gt.arr[bl->firstgoto]); /* error */ } } diff --git a/testes/locals.lua b/testes/locals.lua index ddb75054fc..d50beaa522 100644 --- a/testes/locals.lua +++ b/testes/locals.lua @@ -360,6 +360,26 @@ do end +do + -- bug in 5.4.4: 'break' may generate wrong 'close' instruction when + -- leaving a loop block. + + local closed = false + + local o1 = setmetatable({}, {__close=function() closed = true end}) + + local function test() + for k, v in next, {}, nil, o1 do + local function f() return k end -- create an upvalue + break + end + assert(closed) + end + + test() +end + + do print("testing errors in __close") -- original error is in __close From 69b77b6fde32cdf5dcaeeb92996bf6c4697e0b4f Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 6 Sep 2022 10:58:55 -0300 Subject: [PATCH 0761/1145] Changed the growth rate of string buffers The growth rate of string buffers was reduced from 2 to 1.5 (3/2). As string buffers start larger (256~1024 bytes), they don't need to grow that fast. Moreover, a lower rate allows multiplicative growth up to larger sizes (3/2 of the maximum). (After that, the growth becomes linear, which is mostly useless.) --- lauxlib.c | 8 ++++---- luaconf.h | 5 +++-- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/lauxlib.c b/lauxlib.c index cba5df9b25..4ca6c65488 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -526,14 +526,14 @@ static void newbox (lua_State *L) { /* ** Compute new size for buffer 'B', enough to accommodate extra 'sz' -** bytes. (The test for "double is not big enough" also gets the -** case when the multiplication by 2 overflows.) +** bytes. (The test for "not big enough" also gets the case when the +** computation of 'newsize' overflows.) */ static size_t newbuffsize (luaL_Buffer *B, size_t sz) { - size_t newsize = B->size * 2; /* double buffer size */ + size_t newsize = (B->size / 2) * 3; /* buffer size * 1.5 */ if (l_unlikely(MAX_SIZET - sz < B->n)) /* overflow in (B->n + sz)? */ return luaL_error(B->L, "buffer too large"); - if (newsize < B->n + sz) /* double is not big enough? */ + if (newsize < B->n + sz) /* not big enough? */ newsize = B->n + sz; return newsize; } diff --git a/luaconf.h b/luaconf.h index fcc0018b3a..e4650fbce8 100644 --- a/luaconf.h +++ b/luaconf.h @@ -747,14 +747,15 @@ /* @@ LUA_IDSIZE gives the maximum size for the description of the source -@@ of a function in debug information. +** of a function in debug information. ** CHANGE it if you want a different size. */ #define LUA_IDSIZE 60 /* -@@ LUAL_BUFFERSIZE is the buffer size used by the lauxlib buffer system. +@@ LUAL_BUFFERSIZE is the initial buffer size used by the lauxlib +** buffer system. */ #define LUAL_BUFFERSIZE ((int)(16 * sizeof(void*) * sizeof(lua_Number))) From cd56f222b735a6b2c5392c74904b6c744af2e549 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 7 Sep 2022 12:21:46 -0300 Subject: [PATCH 0762/1145] Corrected error message in 'table.remove' --- ltablib.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ltablib.c b/ltablib.c index 868d78fd83..e6bc4d04af 100644 --- a/ltablib.c +++ b/ltablib.c @@ -93,7 +93,7 @@ static int tremove (lua_State *L) { lua_Integer pos = luaL_optinteger(L, 2, size); if (pos != size) /* validate 'pos' if given */ /* check whether 'pos' is in [1, size + 1] */ - luaL_argcheck(L, (lua_Unsigned)pos - 1u <= (lua_Unsigned)size, 1, + luaL_argcheck(L, (lua_Unsigned)pos - 1u <= (lua_Unsigned)size, 2, "position out of bounds"); lua_geti(L, 1, pos); /* result = t[pos] */ for ( ; pos < size; pos++) { From 71bc69c2afaf49ab5f54f3443af9ae70dd1fed06 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 8 Sep 2022 17:21:02 -0300 Subject: [PATCH 0763/1145] Note in the manual about using '...' as an expression --- manual/manual.of | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/manual/manual.of b/manual/manual.of index ca7f993371..ade47b20c0 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -1144,7 +1144,9 @@ Lua also accepts @x{hexadecimal constants}, which start with @T{0x} or @T{0X}. Hexadecimal constants also accept an optional fractional part plus an optional binary exponent, -marked by a letter @Char{p} or @Char{P}. +marked by a letter @Char{p} or @Char{P} and written in decimal. +(For instance, @T{0x1.fp10} denotes 1984, +which is @M{0x1f / 16} multiplied by @M{2@sp{10}}.) A numeric constant with a radix point or an exponent denotes a float; @@ -2291,7 +2293,7 @@ the number of parameters in a call to a non-variadic function @see{func-def}, the number of variables in a multiple assignment or a local declaration, -and exactly four for a generic @rw{for} loop. +and exactly four values for a generic @rw{for} loop. The @def{adjustment} follows these rules: If there are more values than needed, the extra values are thrown away; @@ -2310,7 +2312,16 @@ the syntax expects a single expression inside a parenthesized expression; therefore, adding parentheses around a multires expression forces it to produce exactly one result. -Here are some examples. +We seldom need to use a vararg expression in a place +where the syntax expects a single expression. +(Usually it is simpler to add a regular parameter before +the variadic part and use that parameter.) +When there is such a need, +we recommend assigning the vararg expression +to a single variable and using that variable +in its place. + +Here are some examples of uses of mutlres expressions. In all cases, when the construction needs @Q{the n-th result} and there is no such result, it uses a @nil. @@ -2319,6 +2330,7 @@ print(x, f()) -- prints x and all results from f(). print(x, (f())) -- prints x and the first result from f(). print(f(), x) -- prints the first result from f() and x. print(1 + f()) -- prints 1 added to the first result from f(). +local x = ... -- x gets the first vararg argument. x,y = ... -- x gets the first vararg argument, -- y gets the second vararg argument. x,y,z = w, f() -- x gets w, y gets the first result from f(), @@ -2331,8 +2343,7 @@ x,y,z = f(), g() -- x gets the first result from f(), -- z gets the second result from g(). x,y,z = (f()) -- x gets the first result from f(), y and z get nil. return f() -- returns all results from f(). -return ... -- returns all received vararg arguments. -return (...) -- returns the first received vararg argument. +return x, ... -- returns x and all received vararg arguments. return x,y,f() -- returns x, y, and all results from f(). {f()} -- creates a list with all results from f(). {...} -- creates a list with all vararg arguments. From f8c4c4fcf2b2fed00b3c5b71c19cd64e539dee51 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 16 Sep 2022 17:05:22 -0300 Subject: [PATCH 0764/1145] New test for table rehash --- testes/nextvar.lua | 38 +++++++++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/testes/nextvar.lua b/testes/nextvar.lua index 9e23e5720d..0874e5bb22 100644 --- a/testes/nextvar.lua +++ b/testes/nextvar.lua @@ -9,6 +9,16 @@ local function checkerror (msg, f, ...) end +local function check (t, na, nh) + if not T then return end + local a, h = T.querytab(t) + if a ~= na or h ~= nh then + print(na, nh, a, h) + assert(nil) + end +end + + local a = {} -- make sure table has lots of space in hash part @@ -20,6 +30,25 @@ for i=1,100 do assert(#a == i) end + +do -- rehash moving elements from array to hash + local a = {} + for i = 1, 100 do a[i] = i end + check(a, 128, 0) + + for i = 5, 95 do a[i] = nil end + check(a, 128, 0) + + a.x = 1 -- force a re-hash + check(a, 4, 8) + + for i = 1, 4 do assert(a[i] == i) end + for i = 5, 95 do assert(a[i] == nil) end + for i = 96, 100 do assert(a[i] == i) end + assert(a.x == 1) +end + + -- testing ipairs local x = 0 for k,v in ipairs{10,20,30;x=12} do @@ -65,15 +94,6 @@ local function mp2 (n) -- minimum power of 2 >= n end -local function check (t, na, nh) - local a, h = T.querytab(t) - if a ~= na or h ~= nh then - print(na, nh, a, h) - assert(nil) - end -end - - -- testing C library sizes do local s = 0 From a1089b415a3f5c753aa1b40758ffdaf28d5701b0 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 23 Sep 2022 10:41:16 -0300 Subject: [PATCH 0765/1145] Bug: 'utf8.codes' accepts spurious continuation bytes --- lutf8lib.c | 27 ++++++++++++++++----------- testes/utf8.lua | 12 +++++++++++- 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/lutf8lib.c b/lutf8lib.c index e7bf098f6d..3a5b9bc38a 100644 --- a/lutf8lib.c +++ b/lutf8lib.c @@ -25,6 +25,9 @@ #define MAXUTF 0x7FFFFFFFu + +#define MSGInvalid "invalid UTF-8 code" + /* ** Integer type for decoded UTF-8 values; MAXUTF needs 31 bits. */ @@ -35,7 +38,8 @@ typedef unsigned long utfint; #endif -#define iscont(p) ((*(p) & 0xC0) == 0x80) +#define iscont(c) (((c) & 0xC0) == 0x80) +#define iscontp(p) iscont(*(p)) /* from strlib */ @@ -65,7 +69,7 @@ static const char *utf8_decode (const char *s, utfint *val, int strict) { int count = 0; /* to count number of continuation bytes */ for (; c & 0x40; c <<= 1) { /* while it needs continuation bytes... */ unsigned int cc = (unsigned char)s[++count]; /* read next byte */ - if ((cc & 0xC0) != 0x80) /* not a continuation byte? */ + if (!iscont(cc)) /* not a continuation byte? */ return NULL; /* invalid byte sequence */ res = (res << 6) | (cc & 0x3F); /* add lower 6 bits from cont. byte */ } @@ -140,7 +144,7 @@ static int codepoint (lua_State *L) { utfint code; s = utf8_decode(s, &code, !lax); if (s == NULL) - return luaL_error(L, "invalid UTF-8 code"); + return luaL_error(L, MSGInvalid); lua_pushinteger(L, code); n++; } @@ -190,16 +194,16 @@ static int byteoffset (lua_State *L) { "position out of bounds"); if (n == 0) { /* find beginning of current byte sequence */ - while (posi > 0 && iscont(s + posi)) posi--; + while (posi > 0 && iscontp(s + posi)) posi--; } else { - if (iscont(s + posi)) + if (iscontp(s + posi)) return luaL_error(L, "initial position is a continuation byte"); if (n < 0) { while (n < 0 && posi > 0) { /* move back */ do { /* find beginning of previous character */ posi--; - } while (posi > 0 && iscont(s + posi)); + } while (posi > 0 && iscontp(s + posi)); n++; } } @@ -208,7 +212,7 @@ static int byteoffset (lua_State *L) { while (n > 0 && posi < (lua_Integer)len) { do { /* find beginning of next character */ posi++; - } while (iscont(s + posi)); /* (cannot pass final '\0') */ + } while (iscontp(s + posi)); /* (cannot pass final '\0') */ n--; } } @@ -226,15 +230,15 @@ static int iter_aux (lua_State *L, int strict) { const char *s = luaL_checklstring(L, 1, &len); lua_Unsigned n = (lua_Unsigned)lua_tointeger(L, 2); if (n < len) { - while (iscont(s + n)) n++; /* skip continuation bytes */ + while (iscontp(s + n)) n++; /* go to next character */ } if (n >= len) /* (also handles original 'n' being negative) */ return 0; /* no more codepoints */ else { utfint code; const char *next = utf8_decode(s + n, &code, strict); - if (next == NULL) - return luaL_error(L, "invalid UTF-8 code"); + if (next == NULL || iscontp(next)) + return luaL_error(L, MSGInvalid); lua_pushinteger(L, n + 1); lua_pushinteger(L, code); return 2; @@ -253,7 +257,8 @@ static int iter_auxlax (lua_State *L) { static int iter_codes (lua_State *L) { int lax = lua_toboolean(L, 2); - luaL_checkstring(L, 1); + const char *s = luaL_checkstring(L, 1); + luaL_argcheck(L, !iscontp(s), 1, MSGInvalid); lua_pushcfunction(L, lax ? iter_auxlax : iter_auxstrict); lua_pushvalue(L, 1); lua_pushinteger(L, 0); diff --git a/testes/utf8.lua b/testes/utf8.lua index 461e223c7b..7472cfd052 100644 --- a/testes/utf8.lua +++ b/testes/utf8.lua @@ -97,9 +97,15 @@ do -- error indication in utf8.len assert(not a and b == p) end check("abc\xE3def", 4) - check("汉字\x80", #("汉字") + 1) check("\xF4\x9F\xBF", 1) check("\xF4\x9F\xBF\xBF", 1) + -- spurious continuation bytes + check("汉字\x80", #("汉字") + 1) + check("\x80hello", 1) + check("hel\x80lo", 4) + check("汉字\xBF", #("汉字") + 1) + check("\xBFhello", 1) + check("hel\xBFlo", 4) end -- errors in utf8.codes @@ -112,12 +118,16 @@ do end errorcodes("ab\xff") errorcodes("\u{110000}") + errorcodes("in\x80valid") + errorcodes("\xbfinvalid") + errorcodes("αλφ\xBFα") -- calling interation function with invalid arguments local f = utf8.codes("") assert(f("", 2) == nil) assert(f("", -1) == nil) assert(f("", math.mininteger) == nil) + end -- error in initial position for offset From cfbe378f906061ee56f91acfbdf569d0d3fb9556 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 23 Sep 2022 10:57:35 -0300 Subject: [PATCH 0766/1145] Small simplification in overflow check in 'getfield' Subtracting a small non-negative int from a non-negative int cannot overflow, and adding a non-negative int to INT_MIN cannot overflow. --- loslib.c | 4 +--- testes/files.lua | 9 +++++++++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/loslib.c b/loslib.c index 3e20d622ba..854dcf691e 100644 --- a/loslib.c +++ b/loslib.c @@ -260,9 +260,7 @@ static int getfield (lua_State *L, const char *key, int d, int delta) { res = d; } else { - /* unsigned avoids overflow when lua_Integer has 32 bits */ - if (!(res >= 0 ? (lua_Unsigned)res <= (lua_Unsigned)INT_MAX + delta - : (lua_Integer)INT_MIN + delta <= res)) + if (!(res >= 0 ? res - delta <= INT_MAX : INT_MIN + delta <= res)) return luaL_error(L, "field '%s' is out-of-bound", key); res -= delta; } diff --git a/testes/files.lua b/testes/files.lua index 16cf9b6a94..78f962e5a0 100644 --- a/testes/files.lua +++ b/testes/files.lua @@ -825,8 +825,17 @@ checkerr("missing", os.time, {hour = 12}) -- missing date if string.packsize("i") == 4 then -- 4-byte ints checkerr("field 'year' is out-of-bound", os.time, {year = -(1 << 31) + 1899, month = 1, day = 1}) + + checkerr("field 'year' is out-of-bound", os.time, + {year = -(1 << 31), month = 1, day = 1}) + + if math.maxinteger > 2^31 then -- larger lua_integer? + checkerr("field 'year' is out-of-bound", os.time, + {year = (1 << 31) + 1900, month = 1, day = 1}) + end end + if not _port then -- test Posix-specific modifiers assert(type(os.date("%Ex")) == 'string') From 26be27459b11feabed52cf40aaa76f86c7edc977 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 23 Sep 2022 11:08:10 -0300 Subject: [PATCH 0767/1145] Negation in constant folding of '>>' may overflow --- lobject.c | 2 +- lvm.c | 4 +--- lvm.h | 5 +++++ testes/bitwise.lua | 12 ++++++++++++ 4 files changed, 19 insertions(+), 4 deletions(-) diff --git a/lobject.c b/lobject.c index a2c006098b..03e2798ca5 100644 --- a/lobject.c +++ b/lobject.c @@ -62,7 +62,7 @@ static lua_Integer intarith (lua_State *L, int op, lua_Integer v1, case LUA_OPBOR: return intop(|, v1, v2); case LUA_OPBXOR: return intop(^, v1, v2); case LUA_OPSHL: return luaV_shiftl(v1, v2); - case LUA_OPSHR: return luaV_shiftl(v1, -v2); + case LUA_OPSHR: return luaV_shiftr(v1, v2); case LUA_OPUNM: return intop(-, 0, v1); case LUA_OPBNOT: return intop(^, ~l_castS2U(0), v1); default: lua_assert(0); return 0; diff --git a/lvm.c b/lvm.c index 614df05573..73a19ba9b0 100644 --- a/lvm.c +++ b/lvm.c @@ -765,12 +765,10 @@ lua_Number luaV_modf (lua_State *L, lua_Number m, lua_Number n) { /* number of bits in an integer */ #define NBITS cast_int(sizeof(lua_Integer) * CHAR_BIT) + /* ** Shift left operation. (Shift right just negates 'y'.) */ -#define luaV_shiftr(x,y) luaV_shiftl(x,intop(-, 0, y)) - - lua_Integer luaV_shiftl (lua_Integer x, lua_Integer y) { if (y < 0) { /* shift right? */ if (y <= -NBITS) return 0; diff --git a/lvm.h b/lvm.h index 1bc16f3a50..dba1ad2770 100644 --- a/lvm.h +++ b/lvm.h @@ -110,6 +110,11 @@ typedef enum { luaC_barrierback(L, gcvalue(t), v); } +/* +** Shift right is the same as shift left with a negative 'y' +*/ +#define luaV_shiftr(x,y) luaV_shiftl(x,intop(-, 0, y)) + LUAI_FUNC int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2); diff --git a/testes/bitwise.lua b/testes/bitwise.lua index 9509f7f040..dd0a1a9a39 100644 --- a/testes/bitwise.lua +++ b/testes/bitwise.lua @@ -38,6 +38,18 @@ d = d << 32 assert(a | b ~ c & d == 0xF4000000 << 32) assert(~~a == a and ~a == -1 ~ a and -d == ~d + 1) + +do -- constant folding + local code = string.format("return -1 >> %d", math.maxinteger) + assert(load(code)() == 0) + local code = string.format("return -1 >> %d", math.mininteger) + assert(load(code)() == 0) + local code = string.format("return -1 << %d", math.maxinteger) + assert(load(code)() == 0) + local code = string.format("return -1 << %d", math.mininteger) + assert(load(code)() == 0) +end + assert(-1 >> 1 == (1 << (numbits - 1)) - 1 and 1 << 31 == 0x80000000) assert(-1 >> (numbits - 1) == 1) assert(-1 >> numbits == 0 and From 7f12bf40c401ea465c792156be31bf4a38a7499f Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 19 Oct 2022 16:20:11 -0300 Subject: [PATCH 0768/1145] Portability issue in a test for 'string.format' --- testes/strings.lua | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/testes/strings.lua b/testes/strings.lua index 184fa65151..337c293761 100644 --- a/testes/strings.lua +++ b/testes/strings.lua @@ -346,13 +346,18 @@ assert(string.format("%013i", -100) == "-000000000100") assert(string.format("%2.5d", -100) == "-00100") assert(string.format("%.u", 0) == "") assert(string.format("%+#014.0f", 100) == "+000000000100.") -assert(string.format("% 1.0E", 100) == " 1E+02") assert(string.format("%-16c", 97) == "a ") assert(string.format("%+.3G", 1.5) == "+1.5") -assert(string.format("% .1g", 2^10) == " 1e+03") assert(string.format("%.0s", "alo") == "") assert(string.format("%.s", "alo") == "") +-- ISO C89 says that "The exponent always contains at least two digits", +-- but unlike ISO C99 it does not ensure that it contains "only as many +-- more digits as necessary". +assert(string.match(string.format("% 1.0E", 100), "^ 1E%+0+2$")) +assert(string.match(string.format("% .1g", 2^10), "^ 1e%+0+3$")) + + -- errors in format local function check (fmt, msg) From 14d2803e55f0bbc2b09890a3b30afbb063ec973d Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 19 Oct 2022 16:29:54 -0300 Subject: [PATCH 0769/1145] Details Some cast operations rewritten to use respective macros. --- ltable.c | 4 ++-- ltests.c | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/ltable.c b/ltable.c index 1b1cd2415b..d03e748651 100644 --- a/ltable.c +++ b/ltable.c @@ -107,7 +107,7 @@ static const TValue absentkey = {ABSTKEYCONSTANT}; */ static Node *hashint (const Table *t, lua_Integer i) { lua_Unsigned ui = l_castS2U(i); - if (ui <= (unsigned int)INT_MAX) + if (ui <= cast_uint(INT_MAX)) return hashmod(t, cast_int(ui)); else return hashmod(t, ui); @@ -488,7 +488,7 @@ static void setnodevector (lua_State *L, Table *t, unsigned int size) { luaG_runerror(L, "table overflow"); size = twoto(lsize); t->node = luaM_newvector(L, size, Node); - for (i = 0; i < (int)size; i++) { + for (i = 0; i < cast_int(size); i++) { Node *n = gnode(t, i); gnext(n) = 0; setnilkey(n); diff --git a/ltests.c b/ltests.c index 97834e3802..9a887f9817 100644 --- a/ltests.c +++ b/ltests.c @@ -533,7 +533,7 @@ static void checkobject (global_State *g, GCObject *o, int maybedead, static lu_mem checkgraylist (global_State *g, GCObject *o) { int total = 0; /* count number of elements in the list */ - ((void)g); /* better to keep it available if we need to print an object */ + cast_void(g); /* better to keep it if we need to print an object */ while (o) { assert(!!isgray(o) ^ (getage(o) == G_TOUCHED2)); assert(!testbit(o->marked, TESTBIT)); @@ -1055,7 +1055,7 @@ static int tref (lua_State *L) { luaL_checkany(L, 1); lua_pushvalue(L, 1); lua_pushinteger(L, luaL_ref(L, LUA_REGISTRYINDEX)); - (void)level; /* to avoid warnings */ + cast_void(level); /* to avoid warnings */ lua_assert(lua_gettop(L) == level+1); /* +1 for result */ return 1; } @@ -1063,7 +1063,7 @@ static int tref (lua_State *L) { static int getref (lua_State *L) { int level = lua_gettop(L); lua_rawgeti(L, LUA_REGISTRYINDEX, luaL_checkinteger(L, 1)); - (void)level; /* to avoid warnings */ + cast_void(level); /* to avoid warnings */ lua_assert(lua_gettop(L) == level+1); return 1; } @@ -1071,7 +1071,7 @@ static int getref (lua_State *L) { static int unref (lua_State *L) { int level = lua_gettop(L); luaL_unref(L, LUA_REGISTRYINDEX, cast_int(luaL_checkinteger(L, 1))); - (void)level; /* to avoid warnings */ + cast_void(level); /* to avoid warnings */ lua_assert(lua_gettop(L) == level); return 0; } @@ -1740,7 +1740,7 @@ static struct X { int x; } x; else if EQ("tostring") { const char *s = lua_tostring(L1, getindex); const char *s1 = lua_pushstring(L1, s); - (void)s1; /* to avoid warnings */ + cast_void(s1); /* to avoid warnings */ lua_longassert((s == NULL && s1 == NULL) || strcmp(s, s1) == 0); } else if EQ("Ltolstring") { From c954db39241a8b21d7b32b42b87a066b4708f969 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 19 Oct 2022 16:30:39 -0300 Subject: [PATCH 0770/1145] Option '-l g=mod' added to the manual Plus some other improvements in the manual. --- manual/manual.of | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/manual/manual.of b/manual/manual.of index ade47b20c0..10c16bd133 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -1423,9 +1423,6 @@ A label should not be declared where a label with the same name is visible, even if this other label has been declared in an enclosing block. -Labels and empty statements are called @def{void statements}, -as they perform no actions. - The @Rw{break} statement terminates the execution of a @Rw{while}, @Rw{repeat}, or @Rw{for} loop, skipping to the next statement after the loop: @@ -2361,6 +2358,7 @@ Lua is a lexically scoped language. The scope of a local variable begins at the first statement after its declaration and lasts until the last non-void statement of the innermost block that includes the declaration. +(@emph{Void statements} are labels and empty statements.) Consider the following example: @verbatim{ x = 10 -- global variable @@ -3165,8 +3163,7 @@ The index must be the last index previously marked to be closed A @idx{__close} metamethod cannot yield when called through this function. -(Exceptionally, this function was introduced in release 5.4.3. -It is not present in previous 5.4 releases.) +(This function was introduced in @N{release 5.4.3}.) } @@ -3713,7 +3710,7 @@ Pops a key from the stack, and pushes a key@En{}value pair from the table at the given index, the @Q{next} pair after the given key. If there are no more elements in the table, -then @Lid{lua_next} returns 0 and pushes nothing. +then @Lid{lua_next} @N{returns 0} and pushes nothing. A typical table traversal looks like this: @verbatim{ @@ -5539,8 +5536,8 @@ It is defined as the following macro: @verbatim{ (luaL_loadfile(L, filename) || lua_pcall(L, 0, LUA_MULTRET, 0)) } -It returns @Lid{LUA_OK} if there are no errors, -or an error code in case of errors @see{statuscodes}. +It @N{returns 0} (@Lid{LUA_OK}) if there are no errors, +or 1 in case of errors. } @@ -5552,8 +5549,8 @@ It is defined as the following macro: @verbatim{ (luaL_loadstring(L, str) || lua_pcall(L, 0, LUA_MULTRET, 0)) } -It returns @Lid{LUA_OK} if there are no errors, -or an error code in case of errors @see{statuscodes}. +It @N{returns 0} (@Lid{LUA_OK}) if there are no errors, +or 1 in case of errors. } @@ -8577,7 +8574,7 @@ the returned status is this number. The default value for @id{code} is @true. If the optional second argument @id{close} is true, -closes the Lua state before exiting. +the function closes the Lua state before exiting @seeF{lua_close}. } @@ -8985,12 +8982,16 @@ The options are: @item{@T{-i}| enter interactive mode after running @rep{script};} @item{@T{-l @rep{mod}}| @Q{require} @rep{mod} and assign the result to global @rep{mod};} +@item{@T{-l @rep{g=mod}}| @Q{require} @rep{mod} and assign the + result to global @rep{g};} @item{@T{-v}| print version information;} @item{@T{-E}| ignore environment variables;} @item{@T{-W}| turn warnings on;} @item{@T{--}| stop handling options;} @item{@T{-}| execute @id{stdin} as a file and stop handling options.} } +(The form @T{-l @rep{g=mod}} was introduced in @N{release 5.4.4}.) + After handling its options, @id{lua} runs the given @emph{script}. When called without arguments, @id{lua} behaves as @T{lua -v -i} From b85816b9a884cbe4cfd139a8e11ffc28ecead576 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 21 Oct 2022 09:18:13 -0300 Subject: [PATCH 0771/1145] Removed test function 'luaH_isdummy' It was not being used anywhere. --- ltable.c | 2 -- ltable.h | 1 - 2 files changed, 3 deletions(-) diff --git a/ltable.c b/ltable.c index d03e748651..cc7993e083 100644 --- a/ltable.c +++ b/ltable.c @@ -975,6 +975,4 @@ Node *luaH_mainposition (const Table *t, const TValue *key) { return mainpositionTV(t, key); } -int luaH_isdummy (const Table *t) { return isdummy(t); } - #endif diff --git a/ltable.h b/ltable.h index 7bbbcb213f..75dd9e26e0 100644 --- a/ltable.h +++ b/ltable.h @@ -59,7 +59,6 @@ LUAI_FUNC unsigned int luaH_realasize (const Table *t); #if defined(LUA_DEBUG) LUAI_FUNC Node *luaH_mainposition (const Table *t, const TValue *key); -LUAI_FUNC int luaH_isdummy (const Table *t); #endif From 1e64c1391f9a14115b5cc82066dbf545ae73ee27 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 25 Oct 2022 16:44:06 -0300 Subject: [PATCH 0772/1145] Bug: stack overflow with nesting of coroutine.close --- lcorolib.c | 4 ++-- lstate.c | 3 ++- ltests.c | 2 +- lua.h | 2 +- manual/manual.of | 7 ++++++- testes/cstack.lua | 26 ++++++++++++++++++++++++++ 6 files changed, 38 insertions(+), 6 deletions(-) diff --git a/lcorolib.c b/lcorolib.c index 785a1e81aa..40b880b14d 100644 --- a/lcorolib.c +++ b/lcorolib.c @@ -76,7 +76,7 @@ static int luaB_auxwrap (lua_State *L) { if (l_unlikely(r < 0)) { /* error? */ int stat = lua_status(co); if (stat != LUA_OK && stat != LUA_YIELD) { /* error in the coroutine? */ - stat = lua_resetthread(co); /* close its tbc variables */ + stat = lua_resetthread(co, L); /* close its tbc variables */ lua_assert(stat != LUA_OK); lua_xmove(co, L, 1); /* move error message to the caller */ } @@ -172,7 +172,7 @@ static int luaB_close (lua_State *L) { int status = auxstatus(L, co); switch (status) { case COS_DEAD: case COS_YIELD: { - status = lua_resetthread(co); + status = lua_resetthread(co, L); if (status == LUA_OK) { lua_pushboolean(L, 1); return 1; diff --git a/lstate.c b/lstate.c index 1ffe1a0f71..4b5c100088 100644 --- a/lstate.c +++ b/lstate.c @@ -343,9 +343,10 @@ int luaE_resetthread (lua_State *L, int status) { } -LUA_API int lua_resetthread (lua_State *L) { +LUA_API int lua_resetthread (lua_State *L, lua_State *from) { int status; lua_lock(L); + L->nCcalls = (from) ? getCcalls(from) : 0; status = luaE_resetthread(L, L->status); lua_unlock(L); return status; diff --git a/ltests.c b/ltests.c index 9a887f9817..734a96daec 100644 --- a/ltests.c +++ b/ltests.c @@ -1533,7 +1533,7 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) { lua_newthread(L1); } else if EQ("resetthread") { - lua_pushinteger(L1, lua_resetthread(L1)); + lua_pushinteger(L1, lua_resetthread(L1, L)); } else if EQ("newuserdata") { lua_newuserdata(L1, getnum); diff --git a/lua.h b/lua.h index 219784cc01..bfba4d1e1b 100644 --- a/lua.h +++ b/lua.h @@ -153,7 +153,7 @@ extern const char lua_ident[]; LUA_API lua_State *(lua_newstate) (lua_Alloc f, void *ud); LUA_API void (lua_close) (lua_State *L); LUA_API lua_State *(lua_newthread) (lua_State *L); -LUA_API int (lua_resetthread) (lua_State *L); +LUA_API int (lua_resetthread) (lua_State *L, lua_State *from); LUA_API lua_CFunction (lua_atpanic) (lua_State *L, lua_CFunction panicf); diff --git a/manual/manual.of b/manual/manual.of index 10c16bd133..6d19e251e5 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -4160,7 +4160,7 @@ and then pops the top element. } -@APIEntry{int lua_resetthread (lua_State *L);| +@APIEntry{int lua_resetthread (lua_State *L, lua_State *from);| @apii{0,?,-} Resets a thread, cleaning its call stack and closing all pending @@ -4173,6 +4173,11 @@ or an error status otherwise. In case of error, leaves the error object on the top of the stack. +The parameter @id{from} represents the coroutine that is resetting @id{L}. +If there is no such coroutine, +this parameter can be @id{NULL}. +(This parameter was introduced in @N{release 5.4.5}.) + } @APIEntry{int lua_resume (lua_State *L, lua_State *from, int nargs, diff --git a/testes/cstack.lua b/testes/cstack.lua index ca76c8729c..97afe9fd03 100644 --- a/testes/cstack.lua +++ b/testes/cstack.lua @@ -84,6 +84,32 @@ do -- bug in 5.4.0 end +do -- bug since 5.4.0 + local count = 0 + print("chain of 'coroutine.close'") + -- create N coroutines forming a list so that each one, when closed, + -- closes the previous one. (With a large enough N, previous Lua + -- versions crash in this test.) + local coro = false + for i = 1, 1000 do + local previous = coro + coro = coroutine.create(function() + local cc = setmetatable({}, {__close=function() + count = count + 1 + if previous then + assert(coroutine.close(previous)) + end + end}) + coroutine.yield() -- leaves 'cc' pending to be closed + end) + assert(coroutine.resume(coro)) -- start it and run until it yields + end + local st, msg = coroutine.close(coro) + assert(not st and string.find(msg, "C stack overflow")) + print("final count: ", count) +end + + do print("nesting of resuming yielded coroutines") local count = 0 From ba089bcb08a0efc6c26fb5c1e3c9d61c00cc012c Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 26 Oct 2022 10:15:09 -0300 Subject: [PATCH 0773/1145] Details Added comments in the makefile about other useful '-fsanitize' options. --- makefile | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/makefile b/makefile index d46e650cb9..ee56c67205 100644 --- a/makefile +++ b/makefile @@ -57,8 +57,13 @@ CWARNS= $(CWARNSCPP) $(CWARNSC) $(CWARNGCC) # -pg -malign-double # -DLUA_USE_CTYPE -DLUA_USE_APICHECK -# ('-ftrapv' for runtime checks of integer overflows) -# -fsanitize=undefined -ftrapv -fno-inline + +# The following options help detect "undefined behavior"s that seldom +# create problems; some are only available in newer gcc versions. To +# use some of them, we also have to define an enrivonment variable +# ASAN_OPTIONS="detect_invalid_pointer_pairs=2". +# -fsanitize=undefined +# -fsanitize=pointer-subtract -fsanitize=address -fsanitize=pointer-compare # TESTS= -DLUA_USER_H='"ltests.h"' -O0 -g From 413a393e6222482f46599e138bebac162610a572 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Sat, 29 Oct 2022 12:06:37 -0300 Subject: [PATCH 0774/1145] Stack indices changed to union's That will allow to change pointers to offsets while reallocating the stack. --- lapi.c | 235 +++++++++++++++++++++++++++--------------------------- lapi.h | 17 ++-- ldebug.c | 52 ++++++------ ldebug.h | 2 +- ldo.c | 125 ++++++++++++++--------------- ldo.h | 7 +- lfunc.c | 42 +++++----- lfunc.h | 4 +- lgc.c | 20 ++--- llex.c | 4 +- lobject.c | 6 +- lobject.h | 10 ++- lparser.c | 6 +- lstate.c | 42 +++++----- lstate.h | 14 ++-- ltests.c | 24 +++--- ltm.c | 38 ++++----- lundump.c | 6 +- lvm.c | 100 +++++++++++------------ 19 files changed, 384 insertions(+), 370 deletions(-) diff --git a/lapi.c b/lapi.c index 5833c7b0a0..34e64af142 100644 --- a/lapi.c +++ b/lapi.c @@ -60,27 +60,28 @@ const char lua_ident[] = static TValue *index2value (lua_State *L, int idx) { CallInfo *ci = L->ci; if (idx > 0) { - StkId o = ci->func + idx; - api_check(L, idx <= L->ci->top - (ci->func + 1), "unacceptable index"); - if (o >= L->top) return &G(L)->nilvalue; + StkId o = ci->func.p + idx; + api_check(L, idx <= ci->top.p - (ci->func.p + 1), "unacceptable index"); + if (o >= L->top.p) return &G(L)->nilvalue; else return s2v(o); } else if (!ispseudo(idx)) { /* negative index */ - api_check(L, idx != 0 && -idx <= L->top - (ci->func + 1), "invalid index"); - return s2v(L->top + idx); + api_check(L, idx != 0 && -idx <= L->top.p - (ci->func.p + 1), + "invalid index"); + return s2v(L->top.p + idx); } else if (idx == LUA_REGISTRYINDEX) return &G(L)->l_registry; else { /* upvalues */ idx = LUA_REGISTRYINDEX - idx; api_check(L, idx <= MAXUPVAL + 1, "upvalue index too large"); - if (ttisCclosure(s2v(ci->func))) { /* C closure? */ - CClosure *func = clCvalue(s2v(ci->func)); + if (ttisCclosure(s2v(ci->func.p))) { /* C closure? */ + CClosure *func = clCvalue(s2v(ci->func.p)); return (idx <= func->nupvalues) ? &func->upvalue[idx-1] : &G(L)->nilvalue; } else { /* light C function or Lua function (through a hook)?) */ - api_check(L, ttislcf(s2v(ci->func)), "caller not a C function"); + api_check(L, ttislcf(s2v(ci->func.p)), "caller not a C function"); return &G(L)->nilvalue; /* no upvalues */ } } @@ -94,14 +95,15 @@ static TValue *index2value (lua_State *L, int idx) { l_sinline StkId index2stack (lua_State *L, int idx) { CallInfo *ci = L->ci; if (idx > 0) { - StkId o = ci->func + idx; - api_check(L, o < L->top, "invalid index"); + StkId o = ci->func.p + idx; + api_check(L, o < L->top.p, "invalid index"); return o; } else { /* non-positive index */ - api_check(L, idx != 0 && -idx <= L->top - (ci->func + 1), "invalid index"); + api_check(L, idx != 0 && -idx <= L->top.p - (ci->func.p + 1), + "invalid index"); api_check(L, !ispseudo(idx), "invalid index"); - return L->top + idx; + return L->top.p + idx; } } @@ -112,12 +114,12 @@ LUA_API int lua_checkstack (lua_State *L, int n) { lua_lock(L); ci = L->ci; api_check(L, n >= 0, "negative 'n'"); - if (L->stack_last - L->top > n) /* stack large enough? */ + if (L->stack_last.p - L->top.p > n) /* stack large enough? */ res = 1; /* yes; check is OK */ else /* need to grow stack */ res = luaD_growstack(L, n, 0); - if (res && ci->top < L->top + n) - ci->top = L->top + n; /* adjust frame top */ + if (res && ci->top.p < L->top.p + n) + ci->top.p = L->top.p + n; /* adjust frame top */ lua_unlock(L); return res; } @@ -129,11 +131,11 @@ LUA_API void lua_xmove (lua_State *from, lua_State *to, int n) { lua_lock(to); api_checknelems(from, n); api_check(from, G(from) == G(to), "moving among independent states"); - api_check(from, to->ci->top - to->top >= n, "stack overflow"); - from->top -= n; + api_check(from, to->ci->top.p - to->top.p >= n, "stack overflow"); + from->top.p -= n; for (i = 0; i < n; i++) { - setobjs2s(to, to->top, from->top + i); - to->top++; /* stack already checked by previous 'api_check' */ + setobjs2s(to, to->top.p, from->top.p + i); + to->top.p++; /* stack already checked by previous 'api_check' */ } lua_unlock(to); } @@ -167,12 +169,12 @@ LUA_API lua_Number lua_version (lua_State *L) { LUA_API int lua_absindex (lua_State *L, int idx) { return (idx > 0 || ispseudo(idx)) ? idx - : cast_int(L->top - L->ci->func) + idx; + : cast_int(L->top.p - L->ci->func.p) + idx; } LUA_API int lua_gettop (lua_State *L) { - return cast_int(L->top - (L->ci->func + 1)); + return cast_int(L->top.p - (L->ci->func.p + 1)); } @@ -182,24 +184,24 @@ LUA_API void lua_settop (lua_State *L, int idx) { ptrdiff_t diff; /* difference for new top */ lua_lock(L); ci = L->ci; - func = ci->func; + func = ci->func.p; if (idx >= 0) { - api_check(L, idx <= ci->top - (func + 1), "new top too large"); - diff = ((func + 1) + idx) - L->top; + api_check(L, idx <= ci->top.p - (func + 1), "new top too large"); + diff = ((func + 1) + idx) - L->top.p; for (; diff > 0; diff--) - setnilvalue(s2v(L->top++)); /* clear new slots */ + setnilvalue(s2v(L->top.p++)); /* clear new slots */ } else { - api_check(L, -(idx+1) <= (L->top - (func + 1)), "invalid new top"); + api_check(L, -(idx+1) <= (L->top.p - (func + 1)), "invalid new top"); diff = idx + 1; /* will "subtract" index (as it is negative) */ } - api_check(L, L->tbclist < L->top, "previous pop of an unclosed slot"); - newtop = L->top + diff; - if (diff < 0 && L->tbclist >= newtop) { + api_check(L, L->tbclist.p < L->top.p, "previous pop of an unclosed slot"); + newtop = L->top.p + diff; + if (diff < 0 && L->tbclist.p >= newtop) { lua_assert(hastocloseCfunc(ci->nresults)); newtop = luaF_close(L, newtop, CLOSEKTOP, 0); } - L->top = newtop; /* correct top only after closing any upvalue */ + L->top.p = newtop; /* correct top only after closing any upvalue */ lua_unlock(L); } @@ -208,7 +210,7 @@ LUA_API void lua_closeslot (lua_State *L, int idx) { StkId level; lua_lock(L); level = index2stack(L, idx); - api_check(L, hastocloseCfunc(L->ci->nresults) && L->tbclist == level, + api_check(L, hastocloseCfunc(L->ci->nresults) && L->tbclist.p == level, "no variable to close at given level"); level = luaF_close(L, level, CLOSEKTOP, 0); setnilvalue(s2v(level)); @@ -239,7 +241,7 @@ l_sinline void reverse (lua_State *L, StkId from, StkId to) { LUA_API void lua_rotate (lua_State *L, int idx, int n) { StkId p, t, m; lua_lock(L); - t = L->top - 1; /* end of stack segment being rotated */ + t = L->top.p - 1; /* end of stack segment being rotated */ p = index2stack(L, idx); /* start of segment */ api_check(L, (n >= 0 ? n : -n) <= (t - p + 1), "invalid 'n'"); m = (n >= 0 ? t - n : p - n - 1); /* end of prefix */ @@ -258,7 +260,7 @@ LUA_API void lua_copy (lua_State *L, int fromidx, int toidx) { api_check(L, isvalid(L, to), "invalid index"); setobj(L, to, fr); if (isupvalue(toidx)) /* function upvalue? */ - luaC_barrier(L, clCvalue(s2v(L->ci->func)), fr); + luaC_barrier(L, clCvalue(s2v(L->ci->func.p)), fr); /* LUA_REGISTRYINDEX does not need gc barrier (collector revisits it before finishing collection) */ lua_unlock(L); @@ -267,7 +269,7 @@ LUA_API void lua_copy (lua_State *L, int fromidx, int toidx) { LUA_API void lua_pushvalue (lua_State *L, int idx) { lua_lock(L); - setobj2s(L, L->top, index2value(L, idx)); + setobj2s(L, L->top.p, index2value(L, idx)); api_incr_top(L); lua_unlock(L); } @@ -336,12 +338,12 @@ LUA_API void lua_arith (lua_State *L, int op) { api_checknelems(L, 2); /* all other operations expect two operands */ else { /* for unary operations, add fake 2nd operand */ api_checknelems(L, 1); - setobjs2s(L, L->top, L->top - 1); + setobjs2s(L, L->top.p, L->top.p - 1); api_incr_top(L); } /* first operand at top - 2, second at top - 1; result go to top - 2 */ - luaO_arith(L, op, s2v(L->top - 2), s2v(L->top - 1), L->top - 2); - L->top--; /* remove second operand */ + luaO_arith(L, op, s2v(L->top.p - 2), s2v(L->top.p - 1), L->top.p - 2); + L->top.p--; /* remove second operand */ lua_unlock(L); } @@ -367,7 +369,7 @@ LUA_API int lua_compare (lua_State *L, int index1, int index2, int op) { LUA_API size_t lua_stringtonumber (lua_State *L, const char *s) { - size_t sz = luaO_str2num(s, s2v(L->top)); + size_t sz = luaO_str2num(s, s2v(L->top.p)); if (sz != 0) api_incr_top(L); return sz; @@ -494,7 +496,7 @@ LUA_API const void *lua_topointer (lua_State *L, int idx) { LUA_API void lua_pushnil (lua_State *L) { lua_lock(L); - setnilvalue(s2v(L->top)); + setnilvalue(s2v(L->top.p)); api_incr_top(L); lua_unlock(L); } @@ -502,7 +504,7 @@ LUA_API void lua_pushnil (lua_State *L) { LUA_API void lua_pushnumber (lua_State *L, lua_Number n) { lua_lock(L); - setfltvalue(s2v(L->top), n); + setfltvalue(s2v(L->top.p), n); api_incr_top(L); lua_unlock(L); } @@ -510,7 +512,7 @@ LUA_API void lua_pushnumber (lua_State *L, lua_Number n) { LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) { lua_lock(L); - setivalue(s2v(L->top), n); + setivalue(s2v(L->top.p), n); api_incr_top(L); lua_unlock(L); } @@ -525,7 +527,7 @@ LUA_API const char *lua_pushlstring (lua_State *L, const char *s, size_t len) { TString *ts; lua_lock(L); ts = (len == 0) ? luaS_new(L, "") : luaS_newlstr(L, s, len); - setsvalue2s(L, L->top, ts); + setsvalue2s(L, L->top.p, ts); api_incr_top(L); luaC_checkGC(L); lua_unlock(L); @@ -536,11 +538,11 @@ LUA_API const char *lua_pushlstring (lua_State *L, const char *s, size_t len) { LUA_API const char *lua_pushstring (lua_State *L, const char *s) { lua_lock(L); if (s == NULL) - setnilvalue(s2v(L->top)); + setnilvalue(s2v(L->top.p)); else { TString *ts; ts = luaS_new(L, s); - setsvalue2s(L, L->top, ts); + setsvalue2s(L, L->top.p, ts); s = getstr(ts); /* internal copy's address */ } api_incr_top(L); @@ -577,7 +579,7 @@ LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) { LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) { lua_lock(L); if (n == 0) { - setfvalue(s2v(L->top), fn); + setfvalue(s2v(L->top.p), fn); api_incr_top(L); } else { @@ -586,13 +588,13 @@ LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) { api_check(L, n <= MAXUPVAL, "upvalue index too large"); cl = luaF_newCclosure(L, n); cl->f = fn; - L->top -= n; + L->top.p -= n; while (n--) { - setobj2n(L, &cl->upvalue[n], s2v(L->top + n)); + setobj2n(L, &cl->upvalue[n], s2v(L->top.p + n)); /* does not need barrier because closure is white */ lua_assert(iswhite(cl)); } - setclCvalue(L, s2v(L->top), cl); + setclCvalue(L, s2v(L->top.p), cl); api_incr_top(L); luaC_checkGC(L); } @@ -603,9 +605,9 @@ LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) { LUA_API void lua_pushboolean (lua_State *L, int b) { lua_lock(L); if (b) - setbtvalue(s2v(L->top)); + setbtvalue(s2v(L->top.p)); else - setbfvalue(s2v(L->top)); + setbfvalue(s2v(L->top.p)); api_incr_top(L); lua_unlock(L); } @@ -613,7 +615,7 @@ LUA_API void lua_pushboolean (lua_State *L, int b) { LUA_API void lua_pushlightuserdata (lua_State *L, void *p) { lua_lock(L); - setpvalue(s2v(L->top), p); + setpvalue(s2v(L->top.p), p); api_incr_top(L); lua_unlock(L); } @@ -621,7 +623,7 @@ LUA_API void lua_pushlightuserdata (lua_State *L, void *p) { LUA_API int lua_pushthread (lua_State *L) { lua_lock(L); - setthvalue(L, s2v(L->top), L); + setthvalue(L, s2v(L->top.p), L); api_incr_top(L); lua_unlock(L); return (G(L)->mainthread == L); @@ -638,16 +640,16 @@ l_sinline int auxgetstr (lua_State *L, const TValue *t, const char *k) { const TValue *slot; TString *str = luaS_new(L, k); if (luaV_fastget(L, t, str, slot, luaH_getstr)) { - setobj2s(L, L->top, slot); + setobj2s(L, L->top.p, slot); api_incr_top(L); } else { - setsvalue2s(L, L->top, str); + setsvalue2s(L, L->top.p, str); api_incr_top(L); - luaV_finishget(L, t, s2v(L->top - 1), L->top - 1, slot); + luaV_finishget(L, t, s2v(L->top.p - 1), L->top.p - 1, slot); } lua_unlock(L); - return ttype(s2v(L->top - 1)); + return ttype(s2v(L->top.p - 1)); } @@ -674,13 +676,13 @@ LUA_API int lua_gettable (lua_State *L, int idx) { TValue *t; lua_lock(L); t = index2value(L, idx); - if (luaV_fastget(L, t, s2v(L->top - 1), slot, luaH_get)) { - setobj2s(L, L->top - 1, slot); + if (luaV_fastget(L, t, s2v(L->top.p - 1), slot, luaH_get)) { + setobj2s(L, L->top.p - 1, slot); } else - luaV_finishget(L, t, s2v(L->top - 1), L->top - 1, slot); + luaV_finishget(L, t, s2v(L->top.p - 1), L->top.p - 1, slot); lua_unlock(L); - return ttype(s2v(L->top - 1)); + return ttype(s2v(L->top.p - 1)); } @@ -696,27 +698,27 @@ LUA_API int lua_geti (lua_State *L, int idx, lua_Integer n) { lua_lock(L); t = index2value(L, idx); if (luaV_fastgeti(L, t, n, slot)) { - setobj2s(L, L->top, slot); + setobj2s(L, L->top.p, slot); } else { TValue aux; setivalue(&aux, n); - luaV_finishget(L, t, &aux, L->top, slot); + luaV_finishget(L, t, &aux, L->top.p, slot); } api_incr_top(L); lua_unlock(L); - return ttype(s2v(L->top - 1)); + return ttype(s2v(L->top.p - 1)); } l_sinline int finishrawget (lua_State *L, const TValue *val) { if (isempty(val)) /* avoid copying empty items to the stack */ - setnilvalue(s2v(L->top)); + setnilvalue(s2v(L->top.p)); else - setobj2s(L, L->top, val); + setobj2s(L, L->top.p, val); api_incr_top(L); lua_unlock(L); - return ttype(s2v(L->top - 1)); + return ttype(s2v(L->top.p - 1)); } @@ -733,8 +735,8 @@ LUA_API int lua_rawget (lua_State *L, int idx) { lua_lock(L); api_checknelems(L, 1); t = gettable(L, idx); - val = luaH_get(t, s2v(L->top - 1)); - L->top--; /* remove key */ + val = luaH_get(t, s2v(L->top.p - 1)); + L->top.p--; /* remove key */ return finishrawget(L, val); } @@ -761,7 +763,7 @@ LUA_API void lua_createtable (lua_State *L, int narray, int nrec) { Table *t; lua_lock(L); t = luaH_new(L); - sethvalue2s(L, L->top, t); + sethvalue2s(L, L->top.p, t); api_incr_top(L); if (narray > 0 || nrec > 0) luaH_resize(L, t, narray, nrec); @@ -788,7 +790,7 @@ LUA_API int lua_getmetatable (lua_State *L, int objindex) { break; } if (mt != NULL) { - sethvalue2s(L, L->top, mt); + sethvalue2s(L, L->top.p, mt); api_incr_top(L); res = 1; } @@ -804,12 +806,12 @@ LUA_API int lua_getiuservalue (lua_State *L, int idx, int n) { o = index2value(L, idx); api_check(L, ttisfulluserdata(o), "full userdata expected"); if (n <= 0 || n > uvalue(o)->nuvalue) { - setnilvalue(s2v(L->top)); + setnilvalue(s2v(L->top.p)); t = LUA_TNONE; } else { - setobj2s(L, L->top, &uvalue(o)->uv[n - 1].uv); - t = ttype(s2v(L->top)); + setobj2s(L, L->top.p, &uvalue(o)->uv[n - 1].uv); + t = ttype(s2v(L->top.p)); } api_incr_top(L); lua_unlock(L); @@ -829,14 +831,14 @@ static void auxsetstr (lua_State *L, const TValue *t, const char *k) { TString *str = luaS_new(L, k); api_checknelems(L, 1); if (luaV_fastget(L, t, str, slot, luaH_getstr)) { - luaV_finishfastset(L, t, slot, s2v(L->top - 1)); - L->top--; /* pop value */ + luaV_finishfastset(L, t, slot, s2v(L->top.p - 1)); + L->top.p--; /* pop value */ } else { - setsvalue2s(L, L->top, str); /* push 'str' (to make it a TValue) */ + setsvalue2s(L, L->top.p, str); /* push 'str' (to make it a TValue) */ api_incr_top(L); - luaV_finishset(L, t, s2v(L->top - 1), s2v(L->top - 2), slot); - L->top -= 2; /* pop value and key */ + luaV_finishset(L, t, s2v(L->top.p - 1), s2v(L->top.p - 2), slot); + L->top.p -= 2; /* pop value and key */ } lua_unlock(L); /* lock done by caller */ } @@ -856,12 +858,12 @@ LUA_API void lua_settable (lua_State *L, int idx) { lua_lock(L); api_checknelems(L, 2); t = index2value(L, idx); - if (luaV_fastget(L, t, s2v(L->top - 2), slot, luaH_get)) { - luaV_finishfastset(L, t, slot, s2v(L->top - 1)); + if (luaV_fastget(L, t, s2v(L->top.p - 2), slot, luaH_get)) { + luaV_finishfastset(L, t, slot, s2v(L->top.p - 1)); } else - luaV_finishset(L, t, s2v(L->top - 2), s2v(L->top - 1), slot); - L->top -= 2; /* pop index and value */ + luaV_finishset(L, t, s2v(L->top.p - 2), s2v(L->top.p - 1), slot); + L->top.p -= 2; /* pop index and value */ lua_unlock(L); } @@ -879,14 +881,14 @@ LUA_API void lua_seti (lua_State *L, int idx, lua_Integer n) { api_checknelems(L, 1); t = index2value(L, idx); if (luaV_fastgeti(L, t, n, slot)) { - luaV_finishfastset(L, t, slot, s2v(L->top - 1)); + luaV_finishfastset(L, t, slot, s2v(L->top.p - 1)); } else { TValue aux; setivalue(&aux, n); - luaV_finishset(L, t, &aux, s2v(L->top - 1), slot); + luaV_finishset(L, t, &aux, s2v(L->top.p - 1), slot); } - L->top--; /* pop value */ + L->top.p--; /* pop value */ lua_unlock(L); } @@ -896,16 +898,16 @@ static void aux_rawset (lua_State *L, int idx, TValue *key, int n) { lua_lock(L); api_checknelems(L, n); t = gettable(L, idx); - luaH_set(L, t, key, s2v(L->top - 1)); + luaH_set(L, t, key, s2v(L->top.p - 1)); invalidateTMcache(t); - luaC_barrierback(L, obj2gco(t), s2v(L->top - 1)); - L->top -= n; + luaC_barrierback(L, obj2gco(t), s2v(L->top.p - 1)); + L->top.p -= n; lua_unlock(L); } LUA_API void lua_rawset (lua_State *L, int idx) { - aux_rawset(L, idx, s2v(L->top - 2), 2); + aux_rawset(L, idx, s2v(L->top.p - 2), 2); } @@ -921,9 +923,9 @@ LUA_API void lua_rawseti (lua_State *L, int idx, lua_Integer n) { lua_lock(L); api_checknelems(L, 1); t = gettable(L, idx); - luaH_setint(L, t, n, s2v(L->top - 1)); - luaC_barrierback(L, obj2gco(t), s2v(L->top - 1)); - L->top--; + luaH_setint(L, t, n, s2v(L->top.p - 1)); + luaC_barrierback(L, obj2gco(t), s2v(L->top.p - 1)); + L->top.p--; lua_unlock(L); } @@ -934,11 +936,11 @@ LUA_API int lua_setmetatable (lua_State *L, int objindex) { lua_lock(L); api_checknelems(L, 1); obj = index2value(L, objindex); - if (ttisnil(s2v(L->top - 1))) + if (ttisnil(s2v(L->top.p - 1))) mt = NULL; else { - api_check(L, ttistable(s2v(L->top - 1)), "table expected"); - mt = hvalue(s2v(L->top - 1)); + api_check(L, ttistable(s2v(L->top.p - 1)), "table expected"); + mt = hvalue(s2v(L->top.p - 1)); } switch (ttype(obj)) { case LUA_TTABLE: { @@ -962,7 +964,7 @@ LUA_API int lua_setmetatable (lua_State *L, int objindex) { break; } } - L->top--; + L->top.p--; lua_unlock(L); return 1; } @@ -978,11 +980,11 @@ LUA_API int lua_setiuservalue (lua_State *L, int idx, int n) { if (!(cast_uint(n) - 1u < cast_uint(uvalue(o)->nuvalue))) res = 0; /* 'n' not in [1, uvalue(o)->nuvalue] */ else { - setobj(L, &uvalue(o)->uv[n - 1].uv, s2v(L->top - 1)); - luaC_barrierback(L, gcvalue(o), s2v(L->top - 1)); + setobj(L, &uvalue(o)->uv[n - 1].uv, s2v(L->top.p - 1)); + luaC_barrierback(L, gcvalue(o), s2v(L->top.p - 1)); res = 1; } - L->top--; + L->top.p--; lua_unlock(L); return res; } @@ -994,7 +996,8 @@ LUA_API int lua_setiuservalue (lua_State *L, int idx, int n) { #define checkresults(L,na,nr) \ - api_check(L, (nr) == LUA_MULTRET || (L->ci->top - L->top >= (nr) - (na)), \ + api_check(L, (nr) == LUA_MULTRET \ + || (L->ci->top.p - L->top.p >= (nr) - (na)), \ "results from function overflow current stack size") @@ -1007,7 +1010,7 @@ LUA_API void lua_callk (lua_State *L, int nargs, int nresults, api_checknelems(L, nargs+1); api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread"); checkresults(L, nargs, nresults); - func = L->top - (nargs+1); + func = L->top.p - (nargs+1); if (k != NULL && yieldable(L)) { /* need to prepare continuation? */ L->ci->u.c.k = k; /* save continuation */ L->ci->u.c.ctx = ctx; /* save context */ @@ -1055,7 +1058,7 @@ LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc, api_check(L, ttisfunction(s2v(o)), "error handler must be a function"); func = savestack(L, o); } - c.func = L->top - (nargs+1); /* function to be called */ + c.func = L->top.p - (nargs+1); /* function to be called */ if (k == NULL || !yieldable(L)) { /* no continuation or no yieldable? */ c.nresults = nresults; /* do a 'conventional' protected call */ status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func); @@ -1090,12 +1093,12 @@ LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data, luaZ_init(L, &z, reader, data); status = luaD_protectedparser(L, &z, chunkname, mode); if (status == LUA_OK) { /* no errors? */ - LClosure *f = clLvalue(s2v(L->top - 1)); /* get newly created function */ + LClosure *f = clLvalue(s2v(L->top.p - 1)); /* get new function */ if (f->nupvalues >= 1) { /* does it have an upvalue? */ /* get global table from registry */ const TValue *gt = getGtable(L); /* set global table as 1st upvalue of 'f' (may be LUA_ENV) */ - setobj(L, f->upvals[0]->v, gt); + setobj(L, f->upvals[0]->v.p, gt); luaC_barrier(L, f->upvals[0], gt); } } @@ -1109,7 +1112,7 @@ LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data, int strip) { TValue *o; lua_lock(L); api_checknelems(L, 1); - o = s2v(L->top - 1); + o = s2v(L->top.p - 1); if (isLfunction(o)) status = luaU_dump(L, getproto(o), writer, data, strip); else @@ -1235,7 +1238,7 @@ LUA_API int lua_gc (lua_State *L, int what, ...) { LUA_API int lua_error (lua_State *L) { TValue *errobj; lua_lock(L); - errobj = s2v(L->top - 1); + errobj = s2v(L->top.p - 1); api_checknelems(L, 1); /* error object is the memory error message? */ if (ttisshrstring(errobj) && eqshrstr(tsvalue(errobj), G(L)->memerrmsg)) @@ -1253,12 +1256,12 @@ LUA_API int lua_next (lua_State *L, int idx) { lua_lock(L); api_checknelems(L, 1); t = gettable(L, idx); - more = luaH_next(L, t, L->top - 1); + more = luaH_next(L, t, L->top.p - 1); if (more) { api_incr_top(L); } else /* no more elements */ - L->top -= 1; /* remove key */ + L->top.p -= 1; /* remove key */ lua_unlock(L); return more; } @@ -1270,7 +1273,7 @@ LUA_API void lua_toclose (lua_State *L, int idx) { lua_lock(L); o = index2stack(L, idx); nresults = L->ci->nresults; - api_check(L, L->tbclist < o, "given index below or equal a marked one"); + api_check(L, L->tbclist.p < o, "given index below or equal a marked one"); luaF_newtbcupval(L, o); /* create new to-be-closed upvalue */ if (!hastocloseCfunc(nresults)) /* function not marked yet? */ L->ci->nresults = codeNresults(nresults); /* mark it */ @@ -1285,7 +1288,7 @@ LUA_API void lua_concat (lua_State *L, int n) { if (n > 0) luaV_concat(L, n); else { /* nothing to concatenate */ - setsvalue2s(L, L->top, luaS_newlstr(L, "", 0)); /* push empty string */ + setsvalue2s(L, L->top.p, luaS_newlstr(L, "", 0)); /* push empty string */ api_incr_top(L); } luaC_checkGC(L); @@ -1297,7 +1300,7 @@ LUA_API void lua_len (lua_State *L, int idx) { TValue *t; lua_lock(L); t = index2value(L, idx); - luaV_objlen(L, L->top, t); + luaV_objlen(L, L->top.p, t); api_incr_top(L); lua_unlock(L); } @@ -1342,7 +1345,7 @@ LUA_API void *lua_newuserdatauv (lua_State *L, size_t size, int nuvalue) { lua_lock(L); api_check(L, 0 <= nuvalue && nuvalue < USHRT_MAX, "invalid value"); u = luaS_newudata(L, size, nuvalue); - setuvalue(L, s2v(L->top), u); + setuvalue(L, s2v(L->top.p), u); api_incr_top(L); luaC_checkGC(L); lua_unlock(L); @@ -1368,7 +1371,7 @@ static const char *aux_upvalue (TValue *fi, int n, TValue **val, Proto *p = f->p; if (!(cast_uint(n) - 1u < cast_uint(p->sizeupvalues))) return NULL; /* 'n' not in [1, p->sizeupvalues] */ - *val = f->upvals[n-1]->v; + *val = f->upvals[n-1]->v.p; if (owner) *owner = obj2gco(f->upvals[n - 1]); name = p->upvalues[n-1].name; return (name == NULL) ? "(no name)" : getstr(name); @@ -1384,7 +1387,7 @@ LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) { lua_lock(L); name = aux_upvalue(index2value(L, funcindex), n, &val, NULL); if (name) { - setobj2s(L, L->top, val); + setobj2s(L, L->top.p, val); api_incr_top(L); } lua_unlock(L); @@ -1402,8 +1405,8 @@ LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) { api_checknelems(L, 1); name = aux_upvalue(fi, n, &val, &owner); if (name) { - L->top--; - setobj(L, val, s2v(L->top)); + L->top.p--; + setobj(L, val, s2v(L->top.p)); luaC_barrier(L, owner, val); } lua_unlock(L); diff --git a/lapi.h b/lapi.h index 9e99cc4482..a742427cdc 100644 --- a/lapi.h +++ b/lapi.h @@ -12,23 +12,26 @@ #include "lstate.h" -/* Increments 'L->top', checking for stack overflows */ -#define api_incr_top(L) {L->top++; api_check(L, L->top <= L->ci->top, \ - "stack overflow");} +/* Increments 'L->top.p', checking for stack overflows */ +#define api_incr_top(L) {L->top.p++; \ + api_check(L, L->top.p <= L->ci->top.p, \ + "stack overflow");} /* ** If a call returns too many multiple returns, the callee may not have ** stack space to accommodate all results. In this case, this macro -** increases its stack space ('L->ci->top'). +** increases its stack space ('L->ci->top.p'). */ #define adjustresults(L,nres) \ - { if ((nres) <= LUA_MULTRET && L->ci->top < L->top) L->ci->top = L->top; } + { if ((nres) <= LUA_MULTRET && L->ci->top.p < L->top.p) \ + L->ci->top.p = L->top.p; } /* Ensure the stack has at least 'n' elements */ -#define api_checknelems(L,n) api_check(L, (n) < (L->top - L->ci->func), \ - "not enough elements in the stack") +#define api_checknelems(L,n) \ + api_check(L, (n) < (L->top.p - L->ci->func.p), \ + "not enough elements in the stack") /* diff --git a/ldebug.c b/ldebug.c index fa15eaf68e..3fae5cf25d 100644 --- a/ldebug.c +++ b/ldebug.c @@ -182,10 +182,10 @@ static const char *upvalname (const Proto *p, int uv) { static const char *findvararg (CallInfo *ci, int n, StkId *pos) { - if (clLvalue(s2v(ci->func))->p->is_vararg) { + if (clLvalue(s2v(ci->func.p))->p->is_vararg) { int nextra = ci->u.l.nextraargs; if (n >= -nextra) { /* 'n' is negative */ - *pos = ci->func - nextra - (n + 1); + *pos = ci->func.p - nextra - (n + 1); return "(vararg)"; /* generic name for any vararg */ } } @@ -194,7 +194,7 @@ static const char *findvararg (CallInfo *ci, int n, StkId *pos) { const char *luaG_findlocal (lua_State *L, CallInfo *ci, int n, StkId *pos) { - StkId base = ci->func + 1; + StkId base = ci->func.p + 1; const char *name = NULL; if (isLua(ci)) { if (n < 0) /* access to vararg values? */ @@ -203,7 +203,7 @@ const char *luaG_findlocal (lua_State *L, CallInfo *ci, int n, StkId *pos) { name = luaF_getlocalname(ci_func(ci)->p, n, currentpc(ci)); } if (name == NULL) { /* no 'standard' name? */ - StkId limit = (ci == L->ci) ? L->top : ci->next->func; + StkId limit = (ci == L->ci) ? L->top.p : ci->next->func.p; if (limit - base >= n && n > 0) { /* is 'n' inside 'ci' stack? */ /* generic name for any valid slot */ name = isLua(ci) ? "(temporary)" : "(C temporary)"; @@ -221,16 +221,16 @@ LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) { const char *name; lua_lock(L); if (ar == NULL) { /* information about non-active function? */ - if (!isLfunction(s2v(L->top - 1))) /* not a Lua function? */ + if (!isLfunction(s2v(L->top.p - 1))) /* not a Lua function? */ name = NULL; else /* consider live variables at function start (parameters) */ - name = luaF_getlocalname(clLvalue(s2v(L->top - 1))->p, n, 0); + name = luaF_getlocalname(clLvalue(s2v(L->top.p - 1))->p, n, 0); } else { /* active function; get information through 'ar' */ StkId pos = NULL; /* to avoid warnings */ name = luaG_findlocal(L, ar->i_ci, n, &pos); if (name) { - setobjs2s(L, L->top, pos); + setobjs2s(L, L->top.p, pos); api_incr_top(L); } } @@ -245,8 +245,8 @@ LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) { lua_lock(L); name = luaG_findlocal(L, ar->i_ci, n, &pos); if (name) { - setobjs2s(L, pos, L->top - 1); - L->top--; /* pop value */ + setobjs2s(L, pos, L->top.p - 1); + L->top.p--; /* pop value */ } lua_unlock(L); return name; @@ -289,7 +289,7 @@ static int nextline (const Proto *p, int currentline, int pc) { static void collectvalidlines (lua_State *L, Closure *f) { if (noLuaClosure(f)) { - setnilvalue(s2v(L->top)); + setnilvalue(s2v(L->top.p)); api_incr_top(L); } else { @@ -298,7 +298,7 @@ static void collectvalidlines (lua_State *L, Closure *f) { const Proto *p = f->l.p; int currentline = p->linedefined; Table *t = luaH_new(L); /* new table to store active lines */ - sethvalue2s(L, L->top, t); /* push it on stack */ + sethvalue2s(L, L->top.p, t); /* push it on stack */ api_incr_top(L); setbtvalue(&v); /* boolean 'true' to be the value of all indices */ if (!p->is_vararg) /* regular function? */ @@ -388,20 +388,20 @@ LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) { lua_lock(L); if (*what == '>') { ci = NULL; - func = s2v(L->top - 1); + func = s2v(L->top.p - 1); api_check(L, ttisfunction(func), "function expected"); what++; /* skip the '>' */ - L->top--; /* pop function */ + L->top.p--; /* pop function */ } else { ci = ar->i_ci; - func = s2v(ci->func); + func = s2v(ci->func.p); lua_assert(ttisfunction(func)); } cl = ttisclosure(func) ? clvalue(func) : NULL; status = auxgetinfo(L, what, ar, cl, ci); if (strchr(what, 'f')) { - setobj2s(L, L->top, func); + setobj2s(L, L->top.p, func); api_incr_top(L); } if (strchr(what, 'L')) @@ -663,7 +663,7 @@ static const char *funcnamefromcall (lua_State *L, CallInfo *ci, */ static int isinstack (CallInfo *ci, const TValue *o) { StkId pos; - for (pos = ci->func + 1; pos < ci->top; pos++) { + for (pos = ci->func.p + 1; pos < ci->top.p; pos++) { if (o == s2v(pos)) return 1; } @@ -681,7 +681,7 @@ static const char *getupvalname (CallInfo *ci, const TValue *o, LClosure *c = ci_func(ci); int i; for (i = 0; i < c->nupvalues; i++) { - if (c->upvals[i]->v == o) { + if (c->upvals[i]->v.p == o) { *name = upvalname(c->p, i); return "upvalue"; } @@ -710,7 +710,7 @@ static const char *varinfo (lua_State *L, const TValue *o) { kind = getupvalname(ci, o, &name); /* check whether 'o' is an upvalue */ if (!kind && isinstack(ci, o)) /* no? try a register */ kind = getobjname(ci_func(ci)->p, currentpc(ci), - cast_int(cast(StkId, o) - (ci->func + 1)), &name); + cast_int(cast(StkId, o) - (ci->func.p + 1)), &name); } return formatvarinfo(L, kind, name); } @@ -807,10 +807,10 @@ l_noret luaG_errormsg (lua_State *L) { if (L->errfunc != 0) { /* is there an error handling function? */ StkId errfunc = restorestack(L, L->errfunc); lua_assert(ttisfunction(s2v(errfunc))); - setobjs2s(L, L->top, L->top - 1); /* move argument */ - setobjs2s(L, L->top - 1, errfunc); /* push function */ - L->top++; /* assume EXTRA_STACK */ - luaD_callnoyield(L, L->top - 2, 1); /* call it */ + setobjs2s(L, L->top.p, L->top.p - 1); /* move argument */ + setobjs2s(L, L->top.p - 1, errfunc); /* push function */ + L->top.p++; /* assume EXTRA_STACK */ + luaD_callnoyield(L, L->top.p - 2, 1); /* call it */ } luaD_throw(L, LUA_ERRRUN); } @@ -826,8 +826,8 @@ l_noret luaG_runerror (lua_State *L, const char *fmt, ...) { va_end(argp); if (isLua(ci)) { /* if Lua function, add source:line information */ luaG_addinfo(L, msg, ci_func(ci)->p->source, getcurrentline(ci)); - setobjs2s(L, L->top - 2, L->top - 1); /* remove 'msg' from the stack */ - L->top--; + setobjs2s(L, L->top.p - 2, L->top.p - 1); /* remove 'msg' */ + L->top.p--; } luaG_errormsg(L); } @@ -872,7 +872,7 @@ static int changedline (const Proto *p, int oldpc, int newpc) { ** invalid; if so, use zero as a valid value. (A wrong but valid 'oldpc' ** at most causes an extra call to a line hook.) ** This function is not "Protected" when called, so it should correct -** 'L->top' before calling anything that can run the GC. +** 'L->top.p' before calling anything that can run the GC. */ int luaG_traceexec (lua_State *L, const Instruction *pc) { CallInfo *ci = L->ci; @@ -895,7 +895,7 @@ int luaG_traceexec (lua_State *L, const Instruction *pc) { return 1; /* do not call hook again (VM yielded, so it did not move) */ } if (!isIT(*(ci->u.l.savedpc - 1))) /* top not being used? */ - L->top = ci->top; /* correct top */ + L->top.p = ci->top.p; /* correct top */ if (counthook) luaD_hook(L, LUA_HOOKCOUNT, -1, 0, 0); /* call count hook */ if (mask & LUA_MASKLINE) { diff --git a/ldebug.h b/ldebug.h index 974960e99d..2c3074c61b 100644 --- a/ldebug.h +++ b/ldebug.h @@ -15,7 +15,7 @@ /* Active Lua function (given call info) */ -#define ci_func(ci) (clLvalue(s2v((ci)->func))) +#define ci_func(ci) (clLvalue(s2v((ci)->func.p))) #define resethookcount(L) (L->hookcount = L->basehookcount) diff --git a/ldo.c b/ldo.c index 419b3db93f..d45c74dab5 100644 --- a/ldo.c +++ b/ldo.c @@ -104,11 +104,11 @@ void luaD_seterrorobj (lua_State *L, int errcode, StkId oldtop) { } default: { lua_assert(errorstatus(errcode)); /* real error */ - setobjs2s(L, oldtop, L->top - 1); /* error message on current top */ + setobjs2s(L, oldtop, L->top.p - 1); /* error message on current top */ break; } } - L->top = oldtop + 1; + L->top.p = oldtop + 1; } @@ -121,7 +121,7 @@ l_noret luaD_throw (lua_State *L, int errcode) { global_State *g = G(L); errcode = luaE_resetthread(L, errcode); /* close all upvalues */ if (g->mainthread->errorJmp) { /* main thread has a handler? */ - setobjs2s(L, g->mainthread->top++, L->top - 1); /* copy error obj. */ + setobjs2s(L, g->mainthread->top.p++, L->top.p - 1); /* copy error obj. */ luaD_throw(g->mainthread, errcode); /* re-throw in main thread */ } else { /* no handler at all; abort */ @@ -160,13 +160,13 @@ int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) { static void correctstack (lua_State *L, StkId oldstack, StkId newstack) { CallInfo *ci; UpVal *up; - L->top = (L->top - oldstack) + newstack; - L->tbclist = (L->tbclist - oldstack) + newstack; + L->top.p = (L->top.p - oldstack) + newstack; + L->tbclist.p = (L->tbclist.p - oldstack) + newstack; for (up = L->openupval; up != NULL; up = up->u.open.next) - up->v = s2v((uplevel(up) - oldstack) + newstack); + up->v.p = s2v((uplevel(up) - oldstack) + newstack); for (ci = L->ci; ci != NULL; ci = ci->previous) { - ci->top = (ci->top - oldstack) + newstack; - ci->func = (ci->func - oldstack) + newstack; + ci->top.p = (ci->top.p - oldstack) + newstack; + ci->func.p = (ci->func.p - oldstack) + newstack; if (isLua(ci)) ci->u.l.trap = 1; /* signal to update 'trap' in 'luaV_execute' */ } @@ -176,7 +176,6 @@ static void correctstack (lua_State *L, StkId oldstack, StkId newstack) { /* some space for error handling */ #define ERRORSTACKSIZE (LUAI_MAXSTACK + 200) - /* ** Reallocate the stack to a new size, correcting all pointers into ** it. (There are pointers to a stack from its upvalues, from its list @@ -201,13 +200,13 @@ int luaD_reallocstack (lua_State *L, int newsize, int raiseerror) { } /* number of elements to be copied to the new stack */ i = ((oldsize <= newsize) ? oldsize : newsize) + EXTRA_STACK; - memcpy(newstack, L->stack, i * sizeof(StackValue)); + memcpy(newstack, L->stack.p, i * sizeof(StackValue)); for (; i < newsize + EXTRA_STACK; i++) setnilvalue(s2v(newstack + i)); /* erase new segment */ - correctstack(L, L->stack, newstack); - luaM_freearray(L, L->stack, oldsize + EXTRA_STACK); - L->stack = newstack; - L->stack_last = L->stack + newsize; + correctstack(L, L->stack.p, newstack); + luaM_freearray(L, L->stack.p, oldsize + EXTRA_STACK); + L->stack.p = newstack; + L->stack_last.p = L->stack.p + newsize; return 1; } @@ -229,7 +228,7 @@ int luaD_growstack (lua_State *L, int n, int raiseerror) { } else if (n < LUAI_MAXSTACK) { /* avoids arithmetic overflows */ int newsize = 2 * size; /* tentative new size */ - int needed = cast_int(L->top - L->stack) + n; + int needed = cast_int(L->top.p - L->stack.p) + n; if (newsize > LUAI_MAXSTACK) /* cannot cross the limit */ newsize = LUAI_MAXSTACK; if (newsize < needed) /* but must respect what was asked for */ @@ -253,12 +252,12 @@ int luaD_growstack (lua_State *L, int n, int raiseerror) { static int stackinuse (lua_State *L) { CallInfo *ci; int res; - StkId lim = L->top; + StkId lim = L->top.p; for (ci = L->ci; ci != NULL; ci = ci->previous) { - if (lim < ci->top) lim = ci->top; + if (lim < ci->top.p) lim = ci->top.p; } - lua_assert(lim <= L->stack_last + EXTRA_STACK); - res = cast_int(lim - L->stack) + 1; /* part of stack in use */ + lua_assert(lim <= L->stack_last.p + EXTRA_STACK); + res = cast_int(lim - L->stack.p) + 1; /* part of stack in use */ if (res < LUA_MINSTACK) res = LUA_MINSTACK; /* ensure a minimum size */ return res; @@ -295,7 +294,7 @@ void luaD_shrinkstack (lua_State *L) { void luaD_inctop (lua_State *L) { luaD_checkstack(L, 1); - L->top++; + L->top.p++; } /* }================================================================== */ @@ -312,8 +311,8 @@ void luaD_hook (lua_State *L, int event, int line, if (hook && L->allowhook) { /* make sure there is a hook */ int mask = CIST_HOOKED; CallInfo *ci = L->ci; - ptrdiff_t top = savestack(L, L->top); /* preserve original 'top' */ - ptrdiff_t ci_top = savestack(L, ci->top); /* idem for 'ci->top' */ + ptrdiff_t top = savestack(L, L->top.p); /* preserve original 'top' */ + ptrdiff_t ci_top = savestack(L, ci->top.p); /* idem for 'ci->top' */ lua_Debug ar; ar.event = event; ar.currentline = line; @@ -323,11 +322,11 @@ void luaD_hook (lua_State *L, int event, int line, ci->u2.transferinfo.ftransfer = ftransfer; ci->u2.transferinfo.ntransfer = ntransfer; } - if (isLua(ci) && L->top < ci->top) - L->top = ci->top; /* protect entire activation register */ + if (isLua(ci) && L->top.p < ci->top.p) + L->top.p = ci->top.p; /* protect entire activation register */ luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */ - if (ci->top < L->top + LUA_MINSTACK) - ci->top = L->top + LUA_MINSTACK; + if (ci->top.p < L->top.p + LUA_MINSTACK) + ci->top.p = L->top.p + LUA_MINSTACK; L->allowhook = 0; /* cannot call hooks inside a hook */ ci->callstatus |= mask; lua_unlock(L); @@ -335,8 +334,8 @@ void luaD_hook (lua_State *L, int event, int line, lua_lock(L); lua_assert(!L->allowhook); L->allowhook = 1; - ci->top = restorestack(L, ci_top); - L->top = restorestack(L, top); + ci->top.p = restorestack(L, ci_top); + L->top.p = restorestack(L, top); ci->callstatus &= ~mask; } } @@ -367,7 +366,7 @@ void luaD_hookcall (lua_State *L, CallInfo *ci) { */ static void rethook (lua_State *L, CallInfo *ci, int nres) { if (L->hookmask & LUA_MASKRET) { /* is return hook on? */ - StkId firstres = L->top - nres; /* index of first result */ + StkId firstres = L->top.p - nres; /* index of first result */ int delta = 0; /* correction for vararg functions */ int ftransfer; if (isLua(ci)) { @@ -375,10 +374,10 @@ static void rethook (lua_State *L, CallInfo *ci, int nres) { if (p->is_vararg) delta = ci->u.l.nextraargs + p->numparams + 1; } - ci->func += delta; /* if vararg, back to virtual 'func' */ - ftransfer = cast(unsigned short, firstres - ci->func); + ci->func.p += delta; /* if vararg, back to virtual 'func' */ + ftransfer = cast(unsigned short, firstres - ci->func.p); luaD_hook(L, LUA_HOOKRET, -1, ftransfer, nres); /* call it */ - ci->func -= delta; + ci->func.p -= delta; } if (isLua(ci = ci->previous)) L->oldpc = pcRel(ci->u.l.savedpc, ci_func(ci)->p); /* set 'oldpc' */ @@ -397,9 +396,9 @@ StkId luaD_tryfuncTM (lua_State *L, StkId func) { tm = luaT_gettmbyobj(L, s2v(func), TM_CALL); /* (after previous GC) */ if (l_unlikely(ttisnil(tm))) luaG_callerror(L, s2v(func)); /* nothing to call */ - for (p = L->top; p > func; p--) /* open space for metamethod */ + for (p = L->top.p; p > func; p--) /* open space for metamethod */ setobjs2s(L, p, p-1); - L->top++; /* stack space pre-allocated by the caller */ + L->top.p++; /* stack space pre-allocated by the caller */ setobj2s(L, func, tm); /* metamethod is the new function to be called */ return func; } @@ -416,14 +415,14 @@ l_sinline void moveresults (lua_State *L, StkId res, int nres, int wanted) { int i; switch (wanted) { /* handle typical cases separately */ case 0: /* no values needed */ - L->top = res; + L->top.p = res; return; case 1: /* one value needed */ if (nres == 0) /* no results? */ setnilvalue(s2v(res)); /* adjust with nil */ else /* at least one result */ - setobjs2s(L, res, L->top - nres); /* move it to proper place */ - L->top = res + 1; + setobjs2s(L, res, L->top.p - nres); /* move it to proper place */ + L->top.p = res + 1; return; case LUA_MULTRET: wanted = nres; /* we want all results */ @@ -446,14 +445,14 @@ l_sinline void moveresults (lua_State *L, StkId res, int nres, int wanted) { break; } /* generic case */ - firstresult = L->top - nres; /* index of first result */ + firstresult = L->top.p - nres; /* index of first result */ if (nres > wanted) /* extra results? */ nres = wanted; /* don't need them */ for (i = 0; i < nres; i++) /* move all results to correct place */ setobjs2s(L, res + i, firstresult + i); for (; i < wanted; i++) /* complete wanted number of results */ setnilvalue(s2v(res + i)); - L->top = res + wanted; /* top points after the last result */ + L->top.p = res + wanted; /* top points after the last result */ } @@ -468,7 +467,7 @@ void luaD_poscall (lua_State *L, CallInfo *ci, int nres) { if (l_unlikely(L->hookmask && !hastocloseCfunc(wanted))) rethook(L, ci, nres); /* move results to proper place */ - moveresults(L, ci->func, nres, wanted); + moveresults(L, ci->func.p, nres, wanted); /* function cannot be in any of these cases when returning */ lua_assert(!(ci->callstatus & (CIST_HOOKED | CIST_YPCALL | CIST_FIN | CIST_TRAN | CIST_CLSRET))); @@ -483,10 +482,10 @@ void luaD_poscall (lua_State *L, CallInfo *ci, int nres) { l_sinline CallInfo *prepCallInfo (lua_State *L, StkId func, int nret, int mask, StkId top) { CallInfo *ci = L->ci = next_ci(L); /* new frame */ - ci->func = func; + ci->func.p = func; ci->nresults = nret; ci->callstatus = mask; - ci->top = top; + ci->top.p = top; return ci; } @@ -500,10 +499,10 @@ l_sinline int precallC (lua_State *L, StkId func, int nresults, CallInfo *ci; checkstackGCp(L, LUA_MINSTACK, func); /* ensure minimum stack size */ L->ci = ci = prepCallInfo(L, func, nresults, CIST_C, - L->top + LUA_MINSTACK); - lua_assert(ci->top <= L->stack_last); + L->top.p + LUA_MINSTACK); + lua_assert(ci->top.p <= L->stack_last.p); if (l_unlikely(L->hookmask & LUA_MASKCALL)) { - int narg = cast_int(L->top - func) - 1; + int narg = cast_int(L->top.p - func) - 1; luaD_hook(L, LUA_HOOKCALL, -1, 1, narg); } lua_unlock(L); @@ -535,17 +534,17 @@ int luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, int nfixparams = p->numparams; int i; checkstackGCp(L, fsize - delta, func); - ci->func -= delta; /* restore 'func' (if vararg) */ + ci->func.p -= delta; /* restore 'func' (if vararg) */ for (i = 0; i < narg1; i++) /* move down function and arguments */ - setobjs2s(L, ci->func + i, func + i); - func = ci->func; /* moved-down function */ + setobjs2s(L, ci->func.p + i, func + i); + func = ci->func.p; /* moved-down function */ for (; narg1 <= nfixparams; narg1++) setnilvalue(s2v(func + narg1)); /* complete missing arguments */ - ci->top = func + 1 + fsize; /* top for new function */ - lua_assert(ci->top <= L->stack_last); + ci->top.p = func + 1 + fsize; /* top for new function */ + lua_assert(ci->top.p <= L->stack_last.p); ci->u.l.savedpc = p->code; /* starting point */ ci->callstatus |= CIST_TAIL; - L->top = func + narg1; /* set top */ + L->top.p = func + narg1; /* set top */ return -1; } default: { /* not a function */ @@ -578,15 +577,15 @@ CallInfo *luaD_precall (lua_State *L, StkId func, int nresults) { case LUA_VLCL: { /* Lua function */ CallInfo *ci; Proto *p = clLvalue(s2v(func))->p; - int narg = cast_int(L->top - func) - 1; /* number of real arguments */ + int narg = cast_int(L->top.p - func) - 1; /* number of real arguments */ int nfixparams = p->numparams; int fsize = p->maxstacksize; /* frame size */ checkstackGCp(L, fsize, func); L->ci = ci = prepCallInfo(L, func, nresults, 0, func + 1 + fsize); ci->u.l.savedpc = p->code; /* starting point */ for (; narg < nfixparams; narg++) - setnilvalue(s2v(L->top++)); /* complete missing arguments */ - lua_assert(ci->top <= L->stack_last); + setnilvalue(s2v(L->top.p++)); /* complete missing arguments */ + lua_assert(ci->top.p <= L->stack_last.p); return ci; } default: { /* not a function */ @@ -748,8 +747,8 @@ static CallInfo *findpcall (lua_State *L) { ** coroutine error handler and should not kill the coroutine.) */ static int resume_error (lua_State *L, const char *msg, int narg) { - L->top -= narg; /* remove args from the stack */ - setsvalue2s(L, L->top, luaS_new(L, msg)); /* push error message */ + L->top.p -= narg; /* remove args from the stack */ + setsvalue2s(L, L->top.p, luaS_new(L, msg)); /* push error message */ api_incr_top(L); lua_unlock(L); return LUA_ERRRUN; @@ -765,7 +764,7 @@ static int resume_error (lua_State *L, const char *msg, int narg) { */ static void resume (lua_State *L, void *ud) { int n = *(cast(int*, ud)); /* number of arguments */ - StkId firstArg = L->top - n; /* first argument */ + StkId firstArg = L->top.p - n; /* first argument */ CallInfo *ci = L->ci; if (L->status == LUA_OK) /* starting a coroutine? */ ccall(L, firstArg - 1, LUA_MULTRET, 0); /* just call its body */ @@ -773,7 +772,7 @@ static void resume (lua_State *L, void *ud) { lua_assert(L->status == LUA_YIELD); L->status = LUA_OK; /* mark that it is running (again) */ if (isLua(ci)) { /* yielded inside a hook? */ - L->top = firstArg; /* discard arguments */ + L->top.p = firstArg; /* discard arguments */ luaV_execute(L, ci); /* just continue running Lua code */ } else { /* 'common' yield */ @@ -816,7 +815,7 @@ LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs, if (L->status == LUA_OK) { /* may be starting a coroutine */ if (L->ci != &L->base_ci) /* not in base level? */ return resume_error(L, "cannot resume non-suspended coroutine", nargs); - else if (L->top - (L->ci->func + 1) == nargs) /* no function? */ + else if (L->top.p - (L->ci->func.p + 1) == nargs) /* no function? */ return resume_error(L, "cannot resume dead coroutine", nargs); } else if (L->status != LUA_YIELD) /* ended with errors? */ @@ -834,11 +833,11 @@ LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs, lua_assert(status == L->status); /* normal end or yield */ else { /* unrecoverable error */ L->status = cast_byte(status); /* mark thread as 'dead' */ - luaD_seterrorobj(L, status, L->top); /* push error message */ - L->ci->top = L->top; + luaD_seterrorobj(L, status, L->top.p); /* push error message */ + L->ci->top.p = L->top.p; } *nresults = (status == LUA_YIELD) ? L->ci->u2.nyield - : cast_int(L->top - (L->ci->func + 1)); + : cast_int(L->top.p - (L->ci->func.p + 1)); lua_unlock(L); return status; } @@ -993,7 +992,7 @@ int luaD_protectedparser (lua_State *L, ZIO *z, const char *name, p.dyd.gt.arr = NULL; p.dyd.gt.size = 0; p.dyd.label.arr = NULL; p.dyd.label.size = 0; luaZ_initbuffer(L, &p.buff); - status = luaD_pcall(L, f_parser, &p, savestack(L, L->top), L->errfunc); + status = luaD_pcall(L, f_parser, &p, savestack(L, L->top.p), L->errfunc); luaZ_freebuffer(L, &p.buff); luaM_freearray(L, p.dyd.actvar.arr, p.dyd.actvar.size); luaM_freearray(L, p.dyd.gt.arr, p.dyd.gt.size); diff --git a/ldo.h b/ldo.h index 4661aa0078..1aa446ad09 100644 --- a/ldo.h +++ b/ldo.h @@ -8,6 +8,7 @@ #define ldo_h +#include "llimits.h" #include "lobject.h" #include "lstate.h" #include "lzio.h" @@ -23,7 +24,7 @@ ** at every check. */ #define luaD_checkstackaux(L,n,pre,pos) \ - if (l_unlikely(L->stack_last - L->top <= (n))) \ + if (l_unlikely(L->stack_last.p - L->top.p <= (n))) \ { pre; luaD_growstack(L, n, 1); pos; } \ else { condmovestack(L,pre,pos); } @@ -32,8 +33,8 @@ -#define savestack(L,p) ((char *)(p) - (char *)L->stack) -#define restorestack(L,n) ((StkId)((char *)L->stack + (n))) +#define savestack(L,pt) (cast_charp(pt) - cast_charp(L->stack.p)) +#define restorestack(L,n) cast(StkId, cast_charp(L->stack.p) + (n)) /* macro to check stack size, preserving 'p' */ diff --git a/lfunc.c b/lfunc.c index daba0abf5c..804bf9dcda 100644 --- a/lfunc.c +++ b/lfunc.c @@ -50,8 +50,8 @@ void luaF_initupvals (lua_State *L, LClosure *cl) { for (i = 0; i < cl->nupvalues; i++) { GCObject *o = luaC_newobj(L, LUA_VUPVAL, sizeof(UpVal)); UpVal *uv = gco2upv(o); - uv->v = &uv->u.value; /* make it closed */ - setnilvalue(uv->v); + uv->v.p = &uv->u.value; /* make it closed */ + setnilvalue(uv->v.p); cl->upvals[i] = uv; luaC_objbarrier(L, cl, uv); } @@ -66,7 +66,7 @@ static UpVal *newupval (lua_State *L, int tbc, StkId level, UpVal **prev) { GCObject *o = luaC_newobj(L, LUA_VUPVAL, sizeof(UpVal)); UpVal *uv = gco2upv(o); UpVal *next = *prev; - uv->v = s2v(level); /* current value lives in the stack */ + uv->v.p = s2v(level); /* current value lives in the stack */ uv->tbc = tbc; uv->u.open.next = next; /* link it to list of open upvalues */ uv->u.open.previous = prev; @@ -106,12 +106,12 @@ UpVal *luaF_findupval (lua_State *L, StkId level) { ** (This function assumes EXTRA_STACK.) */ static void callclosemethod (lua_State *L, TValue *obj, TValue *err, int yy) { - StkId top = L->top; + StkId top = L->top.p; const TValue *tm = luaT_gettmbyobj(L, obj, TM_CLOSE); setobj2s(L, top, tm); /* will call metamethod... */ setobj2s(L, top + 1, obj); /* with 'self' as the 1st argument */ setobj2s(L, top + 2, err); /* and error msg. as 2nd argument */ - L->top = top + 3; /* add function and arguments */ + L->top.p = top + 3; /* add function and arguments */ if (yy) luaD_call(L, top, 0); else @@ -126,7 +126,7 @@ static void callclosemethod (lua_State *L, TValue *obj, TValue *err, int yy) { static void checkclosemth (lua_State *L, StkId level) { const TValue *tm = luaT_gettmbyobj(L, s2v(level), TM_CLOSE); if (ttisnil(tm)) { /* no metamethod? */ - int idx = cast_int(level - L->ci->func); /* variable index */ + int idx = cast_int(level - L->ci->func.p); /* variable index */ const char *vname = luaG_findlocal(L, L->ci, idx, NULL); if (vname == NULL) vname = "?"; luaG_runerror(L, "variable '%s' got a non-closable value", vname); @@ -160,23 +160,23 @@ static void prepcallclosemth (lua_State *L, StkId level, int status, int yy) { ** is used.) */ #define MAXDELTA \ - ((256ul << ((sizeof(L->stack->tbclist.delta) - 1) * 8)) - 1) + ((256ul << ((sizeof(L->stack.p->tbclist.delta) - 1) * 8)) - 1) /* ** Insert a variable in the list of to-be-closed variables. */ void luaF_newtbcupval (lua_State *L, StkId level) { - lua_assert(level > L->tbclist); + lua_assert(level > L->tbclist.p); if (l_isfalse(s2v(level))) return; /* false doesn't need to be closed */ checkclosemth(L, level); /* value must have a close method */ - while (cast_uint(level - L->tbclist) > MAXDELTA) { - L->tbclist += MAXDELTA; /* create a dummy node at maximum delta */ - L->tbclist->tbclist.delta = 0; + while (cast_uint(level - L->tbclist.p) > MAXDELTA) { + L->tbclist.p += MAXDELTA; /* create a dummy node at maximum delta */ + L->tbclist.p->tbclist.delta = 0; } - level->tbclist.delta = cast(unsigned short, level - L->tbclist); - L->tbclist = level; + level->tbclist.delta = cast(unsigned short, level - L->tbclist.p); + L->tbclist.p = level; } @@ -196,10 +196,10 @@ void luaF_closeupval (lua_State *L, StkId level) { StkId upl; /* stack index pointed by 'uv' */ while ((uv = L->openupval) != NULL && (upl = uplevel(uv)) >= level) { TValue *slot = &uv->u.value; /* new position for value */ - lua_assert(uplevel(uv) < L->top); + lua_assert(uplevel(uv) < L->top.p); luaF_unlinkupval(uv); /* remove upvalue from 'openupval' list */ - setobj(L, slot, uv->v); /* move value to upvalue slot */ - uv->v = slot; /* now current value lives here */ + setobj(L, slot, uv->v.p); /* move value to upvalue slot */ + uv->v.p = slot; /* now current value lives here */ if (!iswhite(uv)) { /* neither white nor dead? */ nw2black(uv); /* closed upvalues cannot be gray */ luaC_barrier(L, uv, slot); @@ -212,12 +212,12 @@ void luaF_closeupval (lua_State *L, StkId level) { ** Remove first element from the tbclist plus its dummy nodes. */ static void poptbclist (lua_State *L) { - StkId tbc = L->tbclist; + StkId tbc = L->tbclist.p; lua_assert(tbc->tbclist.delta > 0); /* first element cannot be dummy */ tbc -= tbc->tbclist.delta; - while (tbc > L->stack && tbc->tbclist.delta == 0) + while (tbc > L->stack.p && tbc->tbclist.delta == 0) tbc -= MAXDELTA; /* remove dummy nodes */ - L->tbclist = tbc; + L->tbclist.p = tbc; } @@ -228,8 +228,8 @@ static void poptbclist (lua_State *L) { StkId luaF_close (lua_State *L, StkId level, int status, int yy) { ptrdiff_t levelrel = savestack(L, level); luaF_closeupval(L, level); /* first, close the upvalues */ - while (L->tbclist >= level) { /* traverse tbc's down to that level */ - StkId tbc = L->tbclist; /* get variable index */ + while (L->tbclist.p >= level) { /* traverse tbc's down to that level */ + StkId tbc = L->tbclist.p; /* get variable index */ poptbclist(L); /* remove it from list */ prepcallclosemth(L, tbc, status, yy); /* close variable */ level = restorestack(L, levelrel); diff --git a/lfunc.h b/lfunc.h index 3d296971ec..3be265efb5 100644 --- a/lfunc.h +++ b/lfunc.h @@ -29,10 +29,10 @@ #define MAXUPVAL 255 -#define upisopen(up) ((up)->v != &(up)->u.value) +#define upisopen(up) ((up)->v.p != &(up)->u.value) -#define uplevel(up) check_exp(upisopen(up), cast(StkId, (up)->v)) +#define uplevel(up) check_exp(upisopen(up), cast(StkId, (up)->v.p)) /* diff --git a/lgc.c b/lgc.c index 317ea45081..8e76ccd7a3 100644 --- a/lgc.c +++ b/lgc.c @@ -301,7 +301,7 @@ static void reallymarkobject (global_State *g, GCObject *o) { set2gray(uv); /* open upvalues are kept gray */ else set2black(uv); /* closed upvalues are visited here */ - markvalue(g, uv->v); /* mark its content */ + markvalue(g, uv->v.p); /* mark its content */ break; } case LUA_VUSERDATA: { @@ -376,7 +376,7 @@ static int remarkupvals (global_State *g) { work++; if (!iswhite(uv)) { /* upvalue already visited? */ lua_assert(upisopen(uv) && isgray(uv)); - markvalue(g, uv->v); /* mark its value */ + markvalue(g, uv->v.p); /* mark its value */ } } } @@ -620,19 +620,19 @@ static int traverseLclosure (global_State *g, LClosure *cl) { */ static int traversethread (global_State *g, lua_State *th) { UpVal *uv; - StkId o = th->stack; + StkId o = th->stack.p; if (isold(th) || g->gcstate == GCSpropagate) linkgclist(th, g->grayagain); /* insert into 'grayagain' list */ if (o == NULL) return 1; /* stack not completely built yet */ lua_assert(g->gcstate == GCSatomic || th->openupval == NULL || isintwups(th)); - for (; o < th->top; o++) /* mark live elements in the stack */ + for (; o < th->top.p; o++) /* mark live elements in the stack */ markvalue(g, s2v(o)); for (uv = th->openupval; uv != NULL; uv = uv->u.open.next) markobject(g, uv); /* open upvalues cannot be collected */ if (g->gcstate == GCSatomic) { /* final traversal? */ - for (; o < th->stack_last + EXTRA_STACK; o++) + for (; o < th->stack_last.p + EXTRA_STACK; o++) setnilvalue(s2v(o)); /* clear dead stack slice */ /* 'remarkupvals' may have removed thread from 'twups' list */ if (!isintwups(th) && th->openupval != NULL) { @@ -892,7 +892,7 @@ static GCObject *udata2finalize (global_State *g) { static void dothecall (lua_State *L, void *ud) { UNUSED(ud); - luaD_callnoyield(L, L->top - 2, 0); + luaD_callnoyield(L, L->top.p - 2, 0); } @@ -909,16 +909,16 @@ static void GCTM (lua_State *L) { int oldgcstp = g->gcstp; g->gcstp |= GCSTPGC; /* avoid GC steps */ L->allowhook = 0; /* stop debug hooks during GC metamethod */ - setobj2s(L, L->top++, tm); /* push finalizer... */ - setobj2s(L, L->top++, &v); /* ... and its argument */ + setobj2s(L, L->top.p++, tm); /* push finalizer... */ + setobj2s(L, L->top.p++, &v); /* ... and its argument */ L->ci->callstatus |= CIST_FIN; /* will run a finalizer */ - status = luaD_pcall(L, dothecall, NULL, savestack(L, L->top - 2), 0); + status = luaD_pcall(L, dothecall, NULL, savestack(L, L->top.p - 2), 0); L->ci->callstatus &= ~CIST_FIN; /* not running a finalizer anymore */ L->allowhook = oldah; /* restore hooks */ g->gcstp = oldgcstp; /* restore state */ if (l_unlikely(status != LUA_OK)) { /* error while running __gc? */ luaE_warnerror(L, "__gc"); - L->top--; /* pops error object */ + L->top.p--; /* pops error object */ } } } diff --git a/llex.c b/llex.c index e99151787a..b0dc0acc24 100644 --- a/llex.c +++ b/llex.c @@ -138,12 +138,12 @@ TString *luaX_newstring (LexState *ls, const char *str, size_t l) { if (!ttisnil(o)) /* string already present? */ ts = keystrval(nodefromval(o)); /* get saved copy */ else { /* not in use yet */ - TValue *stv = s2v(L->top++); /* reserve stack space for string */ + TValue *stv = s2v(L->top.p++); /* reserve stack space for string */ setsvalue(L, stv, ts); /* temporarily anchor the string */ luaH_finishset(L, ls->h, stv, o, stv); /* t[string] = string */ /* table is not a metatable, so it does not need to invalidate cache */ luaC_checkGC(L); - L->top--; /* remove string from stack */ + L->top.p--; /* remove string from stack */ } return ts; } diff --git a/lobject.c b/lobject.c index 03e2798ca5..f73ffc6d92 100644 --- a/lobject.c +++ b/lobject.c @@ -413,8 +413,8 @@ typedef struct BuffFS { */ static void pushstr (BuffFS *buff, const char *str, size_t lstr) { lua_State *L = buff->L; - setsvalue2s(L, L->top, luaS_newlstr(L, str, lstr)); - L->top++; /* may use one slot from EXTRA_STACK */ + setsvalue2s(L, L->top.p, luaS_newlstr(L, str, lstr)); + L->top.p++; /* may use one slot from EXTRA_STACK */ if (!buff->pushed) /* no previous string on the stack? */ buff->pushed = 1; /* now there is one */ else /* join previous string with new one */ @@ -542,7 +542,7 @@ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) { addstr2buff(&buff, fmt, strlen(fmt)); /* rest of 'fmt' */ clearbuff(&buff); /* empty buffer into the stack */ lua_assert(buff.pushed == 1); - return svalue(s2v(L->top - 1)); + return svalue(s2v(L->top.p - 1)); } diff --git a/lobject.h b/lobject.h index 77cc606f52..7af7bd892c 100644 --- a/lobject.h +++ b/lobject.h @@ -157,6 +157,12 @@ typedef union StackValue { /* index to stack elements */ typedef StackValue *StkId; + +typedef union { + StkId p; /* actual pointer */ +} StkIdRel; + + /* convert a 'StackValue' to a 'TValue' */ #define s2v(o) (&(o)->val) @@ -618,7 +624,9 @@ typedef struct Proto { typedef struct UpVal { CommonHeader; lu_byte tbc; /* true if it represents a to-be-closed variable */ - TValue *v; /* points to stack or to its own value */ + union { + TValue *p; /* points to stack or to its own value */ + } v; union { struct { /* (when open) */ struct UpVal *next; /* linked list */ diff --git a/lparser.c b/lparser.c index fe693b5718..24668c2485 100644 --- a/lparser.c +++ b/lparser.c @@ -1944,10 +1944,10 @@ LClosure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, LexState lexstate; FuncState funcstate; LClosure *cl = luaF_newLclosure(L, 1); /* create main closure */ - setclLvalue2s(L, L->top, cl); /* anchor it (to avoid being collected) */ + setclLvalue2s(L, L->top.p, cl); /* anchor it (to avoid being collected) */ luaD_inctop(L); lexstate.h = luaH_new(L); /* create table for scanner */ - sethvalue2s(L, L->top, lexstate.h); /* anchor it */ + sethvalue2s(L, L->top.p, lexstate.h); /* anchor it */ luaD_inctop(L); funcstate.f = cl->p = luaF_newproto(L); luaC_objbarrier(L, cl, cl->p); @@ -1961,7 +1961,7 @@ LClosure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, lua_assert(!funcstate.prev && funcstate.nups == 1 && !lexstate.fs); /* all scopes should be correctly finished */ lua_assert(dyd->actvar.n == 0 && dyd->gt.n == 0 && dyd->label.n == 0); - L->top--; /* remove scanner's table */ + L->top.p--; /* remove scanner's table */ return cl; /* closure is on the stack, too */ } diff --git a/lstate.c b/lstate.c index 4b5c100088..c63fe8675b 100644 --- a/lstate.c +++ b/lstate.c @@ -180,33 +180,33 @@ LUAI_FUNC void luaE_incCstack (lua_State *L) { static void stack_init (lua_State *L1, lua_State *L) { int i; CallInfo *ci; /* initialize stack array */ - L1->stack = luaM_newvector(L, BASIC_STACK_SIZE + EXTRA_STACK, StackValue); - L1->tbclist = L1->stack; + L1->stack.p = luaM_newvector(L, BASIC_STACK_SIZE + EXTRA_STACK, StackValue); + L1->tbclist.p = L1->stack.p; for (i = 0; i < BASIC_STACK_SIZE + EXTRA_STACK; i++) - setnilvalue(s2v(L1->stack + i)); /* erase new stack */ - L1->top = L1->stack; - L1->stack_last = L1->stack + BASIC_STACK_SIZE; + setnilvalue(s2v(L1->stack.p + i)); /* erase new stack */ + L1->top.p = L1->stack.p; + L1->stack_last.p = L1->stack.p + BASIC_STACK_SIZE; /* initialize first ci */ ci = &L1->base_ci; ci->next = ci->previous = NULL; ci->callstatus = CIST_C; - ci->func = L1->top; + ci->func.p = L1->top.p; ci->u.c.k = NULL; ci->nresults = 0; - setnilvalue(s2v(L1->top)); /* 'function' entry for this 'ci' */ - L1->top++; - ci->top = L1->top + LUA_MINSTACK; + setnilvalue(s2v(L1->top.p)); /* 'function' entry for this 'ci' */ + L1->top.p++; + ci->top.p = L1->top.p + LUA_MINSTACK; L1->ci = ci; } static void freestack (lua_State *L) { - if (L->stack == NULL) + if (L->stack.p == NULL) return; /* stack not completely built yet */ L->ci = &L->base_ci; /* free the entire 'ci' list */ luaE_freeCI(L); lua_assert(L->nci == 0); - luaM_freearray(L, L->stack, stacksize(L) + EXTRA_STACK); /* free stack */ + luaM_freearray(L, L->stack.p, stacksize(L) + EXTRA_STACK); /* free stack */ } @@ -248,7 +248,7 @@ static void f_luaopen (lua_State *L, void *ud) { */ static void preinit_thread (lua_State *L, global_State *g) { G(L) = g; - L->stack = NULL; + L->stack.p = NULL; L->ci = NULL; L->nci = 0; L->twups = L; /* thread has no upvalues */ @@ -297,7 +297,7 @@ LUA_API lua_State *lua_newthread (lua_State *L) { L1->next = g->allgc; g->allgc = obj2gco(L1); /* anchor it on L stack */ - setthvalue2s(L, L->top, L1); + setthvalue2s(L, L->top.p, L1); api_incr_top(L); preinit_thread(L1, g); L1->hookmask = L->hookmask; @@ -316,7 +316,7 @@ LUA_API lua_State *lua_newthread (lua_State *L) { void luaE_freethread (lua_State *L, lua_State *L1) { LX *l = fromstate(L1); - luaF_closeupval(L1, L1->stack); /* close all upvalues */ + luaF_closeupval(L1, L1->stack.p); /* close all upvalues */ lua_assert(L1->openupval == NULL); luai_userstatefree(L, L1); freestack(L1); @@ -326,19 +326,19 @@ void luaE_freethread (lua_State *L, lua_State *L1) { int luaE_resetthread (lua_State *L, int status) { CallInfo *ci = L->ci = &L->base_ci; /* unwind CallInfo list */ - setnilvalue(s2v(L->stack)); /* 'function' entry for basic 'ci' */ - ci->func = L->stack; + setnilvalue(s2v(L->stack.p)); /* 'function' entry for basic 'ci' */ + ci->func.p = L->stack.p; ci->callstatus = CIST_C; if (status == LUA_YIELD) status = LUA_OK; L->status = LUA_OK; /* so it can run __close metamethods */ status = luaD_closeprotected(L, 1, status); if (status != LUA_OK) /* errors? */ - luaD_seterrorobj(L, status, L->stack + 1); + luaD_seterrorobj(L, status, L->stack.p + 1); else - L->top = L->stack + 1; - ci->top = L->top + LUA_MINSTACK; - luaD_reallocstack(L, cast_int(ci->top - L->stack), 0); + L->top.p = L->stack.p + 1; + ci->top.p = L->top.p + LUA_MINSTACK; + luaD_reallocstack(L, cast_int(ci->top.p - L->stack.p), 0); return status; } @@ -427,7 +427,7 @@ void luaE_warning (lua_State *L, const char *msg, int tocont) { ** Generate a warning from an error message */ void luaE_warnerror (lua_State *L, const char *where) { - TValue *errobj = s2v(L->top - 1); /* error object */ + TValue *errobj = s2v(L->top.p - 1); /* error object */ const char *msg = (ttisstring(errobj)) ? svalue(errobj) : "error object is not a string"; diff --git a/lstate.h b/lstate.h index 61e82cde72..2e90781872 100644 --- a/lstate.h +++ b/lstate.h @@ -139,7 +139,7 @@ struct lua_longjmp; /* defined in ldo.c */ #define BASIC_STACK_SIZE (2*LUA_MINSTACK) -#define stacksize(th) cast_int((th)->stack_last - (th)->stack) +#define stacksize(th) cast_int((th)->stack_last.p - (th)->stack.p) /* kinds of Garbage Collection */ @@ -170,8 +170,8 @@ typedef struct stringtable { ** before the function starts or after it ends. */ typedef struct CallInfo { - StkId func; /* function index in the stack */ - StkId top; /* top for this function */ + StkIdRel func; /* function index in the stack */ + StkIdRel top; /* top for this function */ struct CallInfo *previous, *next; /* dynamic call link */ union { struct { /* only for Lua functions */ @@ -306,13 +306,13 @@ struct lua_State { lu_byte status; lu_byte allowhook; unsigned short nci; /* number of items in 'ci' list */ - StkId top; /* first free slot in the stack */ + StkIdRel top; /* first free slot in the stack */ global_State *l_G; CallInfo *ci; /* call info for current function */ - StkId stack_last; /* end of stack (last element + 1) */ - StkId stack; /* stack base */ + StkIdRel stack_last; /* end of stack (last element + 1) */ + StkIdRel stack; /* stack base */ UpVal *openupval; /* list of open upvalues in this stack */ - StkId tbclist; /* list of to-be-closed variables */ + StkIdRel tbclist; /* list of to-be-closed variables */ GCObject *gclist; struct lua_State *twups; /* list of threads with open upvalues */ struct lua_longjmp *errorJmp; /* current error recover point */ diff --git a/ltests.c b/ltests.c index 734a96daec..4a0a6af1fd 100644 --- a/ltests.c +++ b/ltests.c @@ -44,7 +44,7 @@ void *l_Trick = 0; -#define obj_at(L,k) s2v(L->ci->func + (k)) +#define obj_at(L,k) s2v(L->ci->func.p + (k)) static int runC (lua_State *L, lua_State *L1, const char *pc); @@ -57,7 +57,7 @@ static void setnameval (lua_State *L, const char *name, int val) { static void pushobject (lua_State *L, const TValue *o) { - setobj2s(L, L->top, o); + setobj2s(L, L->top.p, o); api_incr_top(L); } @@ -419,7 +419,7 @@ static void checkLclosure (global_State *g, LClosure *cl) { if (uv) { checkobjrefN(g, clgc, uv); if (!upisopen(uv)) - checkvalref(g, obj2gco(uv), uv->v); + checkvalref(g, obj2gco(uv), uv->v.p); } } } @@ -428,7 +428,7 @@ static void checkLclosure (global_State *g, LClosure *cl) { static int lua_checkpc (CallInfo *ci) { if (!isLua(ci)) return 1; else { - StkId f = ci->func; + StkId f = ci->func.p; Proto *p = clLvalue(s2v(f))->p; return p->code <= ci->u.l.savedpc && ci->u.l.savedpc <= p->code + p->sizecode; @@ -441,19 +441,19 @@ static void checkstack (global_State *g, lua_State *L1) { CallInfo *ci; UpVal *uv; assert(!isdead(g, L1)); - if (L1->stack == NULL) { /* incomplete thread? */ + if (L1->stack.p == NULL) { /* incomplete thread? */ assert(L1->openupval == NULL && L1->ci == NULL); return; } for (uv = L1->openupval; uv != NULL; uv = uv->u.open.next) assert(upisopen(uv)); /* must be open */ - assert(L1->top <= L1->stack_last); - assert(L1->tbclist <= L1->top); + assert(L1->top.p <= L1->stack_last.p); + assert(L1->tbclist.p <= L1->top.p); for (ci = L1->ci; ci != NULL; ci = ci->previous) { - assert(ci->top <= L1->stack_last); + assert(ci->top.p <= L1->stack_last.p); assert(lua_checkpc(ci)); } - for (o = L1->stack; o < L1->stack_last; o++) + for (o = L1->stack.p; o < L1->stack_last.p; o++) checkliveness(L1, s2v(o)); /* entire stack must have valid values */ } @@ -465,7 +465,7 @@ static void checkrefs (global_State *g, GCObject *o) { break; } case LUA_VUPVAL: { - checkvalref(g, o, gco2upv(o)->v); + checkvalref(g, o, gco2upv(o)->v.p); break; } case LUA_VTABLE: { @@ -980,7 +980,7 @@ static int hash_query (lua_State *L) { static int stacklevel (lua_State *L) { unsigned long a = 0; - lua_pushinteger(L, (L->top - L->stack)); + lua_pushinteger(L, (L->top.p - L->stack.p)); lua_pushinteger(L, stacksize(L)); lua_pushinteger(L, L->nCcalls); lua_pushinteger(L, L->nci); @@ -1040,7 +1040,7 @@ static int string_query (lua_State *L) { TString *ts; int n = 0; for (ts = tb->hash[s]; ts != NULL; ts = ts->u.hnext) { - setsvalue2s(L, L->top, ts); + setsvalue2s(L, L->top.p, ts); api_incr_top(L); n++; } diff --git a/ltm.c b/ltm.c index b657b783a8..07a060811d 100644 --- a/ltm.c +++ b/ltm.c @@ -102,12 +102,12 @@ const char *luaT_objtypename (lua_State *L, const TValue *o) { void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1, const TValue *p2, const TValue *p3) { - StkId func = L->top; + StkId func = L->top.p; setobj2s(L, func, f); /* push function (assume EXTRA_STACK) */ setobj2s(L, func + 1, p1); /* 1st argument */ setobj2s(L, func + 2, p2); /* 2nd argument */ setobj2s(L, func + 3, p3); /* 3rd argument */ - L->top = func + 4; + L->top.p = func + 4; /* metamethod may yield only when called from Lua code */ if (isLuacode(L->ci)) luaD_call(L, func, 0); @@ -119,18 +119,18 @@ void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1, void luaT_callTMres (lua_State *L, const TValue *f, const TValue *p1, const TValue *p2, StkId res) { ptrdiff_t result = savestack(L, res); - StkId func = L->top; + StkId func = L->top.p; setobj2s(L, func, f); /* push function (assume EXTRA_STACK) */ setobj2s(L, func + 1, p1); /* 1st argument */ setobj2s(L, func + 2, p2); /* 2nd argument */ - L->top += 3; + L->top.p += 3; /* metamethod may yield only when called from Lua code */ if (isLuacode(L->ci)) luaD_call(L, func, 1); else luaD_callnoyield(L, func, 1); res = restorestack(L, result); - setobjs2s(L, res, --L->top); /* move result to its place */ + setobjs2s(L, res, --L->top.p); /* move result to its place */ } @@ -165,7 +165,7 @@ void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2, void luaT_tryconcatTM (lua_State *L) { - StkId top = L->top; + StkId top = L->top.p; if (l_unlikely(!callbinTM(L, s2v(top - 2), s2v(top - 1), top - 2, TM_CONCAT))) luaG_concaterror(L, s2v(top - 2), s2v(top - 1)); @@ -200,15 +200,15 @@ void luaT_trybiniTM (lua_State *L, const TValue *p1, lua_Integer i2, */ int luaT_callorderTM (lua_State *L, const TValue *p1, const TValue *p2, TMS event) { - if (callbinTM(L, p1, p2, L->top, event)) /* try original event */ - return !l_isfalse(s2v(L->top)); + if (callbinTM(L, p1, p2, L->top.p, event)) /* try original event */ + return !l_isfalse(s2v(L->top.p)); #if defined(LUA_COMPAT_LT_LE) else if (event == TM_LE) { /* try '!(p2 < p1)' for '(p1 <= p2)' */ L->ci->callstatus |= CIST_LEQ; /* mark it is doing 'lt' for 'le' */ - if (callbinTM(L, p2, p1, L->top, TM_LT)) { + if (callbinTM(L, p2, p1, L->top.p, TM_LT)) { L->ci->callstatus ^= CIST_LEQ; /* clear mark */ - return l_isfalse(s2v(L->top)); + return l_isfalse(s2v(L->top.p)); } /* else error will remove this 'ci'; no need to clear mark */ } @@ -238,20 +238,20 @@ int luaT_callorderiTM (lua_State *L, const TValue *p1, int v2, void luaT_adjustvarargs (lua_State *L, int nfixparams, CallInfo *ci, const Proto *p) { int i; - int actual = cast_int(L->top - ci->func) - 1; /* number of arguments */ + int actual = cast_int(L->top.p - ci->func.p) - 1; /* number of arguments */ int nextra = actual - nfixparams; /* number of extra arguments */ ci->u.l.nextraargs = nextra; luaD_checkstack(L, p->maxstacksize + 1); /* copy function to the top of the stack */ - setobjs2s(L, L->top++, ci->func); + setobjs2s(L, L->top.p++, ci->func.p); /* move fixed parameters to the top of the stack */ for (i = 1; i <= nfixparams; i++) { - setobjs2s(L, L->top++, ci->func + i); - setnilvalue(s2v(ci->func + i)); /* erase original parameter (for GC) */ + setobjs2s(L, L->top.p++, ci->func.p + i); + setnilvalue(s2v(ci->func.p + i)); /* erase original parameter (for GC) */ } - ci->func += actual + 1; - ci->top += actual + 1; - lua_assert(L->top <= ci->top && ci->top <= L->stack_last); + ci->func.p += actual + 1; + ci->top.p += actual + 1; + lua_assert(L->top.p <= ci->top.p && ci->top.p <= L->stack_last.p); } @@ -261,10 +261,10 @@ void luaT_getvarargs (lua_State *L, CallInfo *ci, StkId where, int wanted) { if (wanted < 0) { wanted = nextra; /* get all extra arguments available */ checkstackGCp(L, nextra, where); /* ensure stack space */ - L->top = where + nextra; /* next instruction will need top */ + L->top.p = where + nextra; /* next instruction will need top */ } for (i = 0; i < wanted && i < nextra; i++) - setobjs2s(L, where + i, ci->func - nextra + i); + setobjs2s(L, where + i, ci->func.p - nextra + i); for (; i < wanted; i++) /* complete required results with nil */ setnilvalue(s2v(where + i)); } diff --git a/lundump.c b/lundump.c index 5aa55c4457..aba93f8280 100644 --- a/lundump.c +++ b/lundump.c @@ -120,10 +120,10 @@ static TString *loadStringN (LoadState *S, Proto *p) { } else { /* long string */ ts = luaS_createlngstrobj(L, size); /* create string */ - setsvalue2s(L, L->top, ts); /* anchor it ('loadVector' can GC) */ + setsvalue2s(L, L->top.p, ts); /* anchor it ('loadVector' can GC) */ luaD_inctop(L); loadVector(S, getstr(ts), size); /* load directly in final place */ - L->top--; /* pop string */ + L->top.p--; /* pop string */ } luaC_objbarrier(L, p, ts); return ts; @@ -321,7 +321,7 @@ LClosure *luaU_undump(lua_State *L, ZIO *Z, const char *name) { S.Z = Z; checkHeader(&S); cl = luaF_newLclosure(L, loadByte(&S)); - setclLvalue2s(L, L->top, cl); + setclLvalue2s(L, L->top.p, cl); luaD_inctop(L); cl->p = luaF_newproto(L); luaC_objbarrier(L, cl, cl->p); diff --git a/lvm.c b/lvm.c index 73a19ba9b0..2e84dc63c1 100644 --- a/lvm.c +++ b/lvm.c @@ -608,8 +608,8 @@ int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2) { if (tm == NULL) /* no TM? */ return 0; /* objects are different */ else { - luaT_callTMres(L, tm, t1, t2, L->top); /* call TM */ - return !l_isfalse(s2v(L->top)); + luaT_callTMres(L, tm, t1, t2, L->top.p); /* call TM */ + return !l_isfalse(s2v(L->top.p)); } } @@ -633,13 +633,13 @@ static void copy2buff (StkId top, int n, char *buff) { /* ** Main operation for concatenation: concat 'total' values in the stack, -** from 'L->top - total' up to 'L->top - 1'. +** from 'L->top.p - total' up to 'L->top.p - 1'. */ void luaV_concat (lua_State *L, int total) { if (total == 1) return; /* "all" values already concatenated */ do { - StkId top = L->top; + StkId top = L->top.p; int n = 2; /* number of elements handled in this pass (at least 2) */ if (!(ttisstring(s2v(top - 2)) || cvt2str(s2v(top - 2))) || !tostring(L, s2v(top - 1))) @@ -657,7 +657,7 @@ void luaV_concat (lua_State *L, int total) { for (n = 1; n < total && tostring(L, s2v(top - n - 1)); n++) { size_t l = vslen(s2v(top - n - 1)); if (l_unlikely(l >= (MAX_SIZE/sizeof(char)) - tl)) { - L->top = top - total; /* pop strings to avoid wasting stack */ + L->top.p = top - total; /* pop strings to avoid wasting stack */ luaG_runerror(L, "string length overflow"); } tl += l; @@ -674,7 +674,7 @@ void luaV_concat (lua_State *L, int total) { setsvalue2s(L, top - n, ts); /* create result */ } total -= n - 1; /* got 'n' strings to create one new */ - L->top -= n - 1; /* popped 'n' strings and pushed one */ + L->top.p -= n - 1; /* popped 'n' strings and pushed one */ } while (total > 1); /* repeat until only 1 result left */ } @@ -808,26 +808,26 @@ static void pushclosure (lua_State *L, Proto *p, UpVal **encup, StkId base, */ void luaV_finishOp (lua_State *L) { CallInfo *ci = L->ci; - StkId base = ci->func + 1; + StkId base = ci->func.p + 1; Instruction inst = *(ci->u.l.savedpc - 1); /* interrupted instruction */ OpCode op = GET_OPCODE(inst); switch (op) { /* finish its execution */ case OP_MMBIN: case OP_MMBINI: case OP_MMBINK: { - setobjs2s(L, base + GETARG_A(*(ci->u.l.savedpc - 2)), --L->top); + setobjs2s(L, base + GETARG_A(*(ci->u.l.savedpc - 2)), --L->top.p); break; } case OP_UNM: case OP_BNOT: case OP_LEN: case OP_GETTABUP: case OP_GETTABLE: case OP_GETI: case OP_GETFIELD: case OP_SELF: { - setobjs2s(L, base + GETARG_A(inst), --L->top); + setobjs2s(L, base + GETARG_A(inst), --L->top.p); break; } case OP_LT: case OP_LE: case OP_LTI: case OP_LEI: case OP_GTI: case OP_GEI: case OP_EQ: { /* note that 'OP_EQI'/'OP_EQK' cannot yield */ - int res = !l_isfalse(s2v(L->top - 1)); - L->top--; + int res = !l_isfalse(s2v(L->top.p - 1)); + L->top.p--; #if defined(LUA_COMPAT_LT_LE) if (ci->callstatus & CIST_LEQ) { /* "<=" using "<" instead? */ ci->callstatus ^= CIST_LEQ; /* clear mark */ @@ -840,11 +840,11 @@ void luaV_finishOp (lua_State *L) { break; } case OP_CONCAT: { - StkId top = L->top - 1; /* top when 'luaT_tryconcatTM' was called */ + StkId top = L->top.p - 1; /* top when 'luaT_tryconcatTM' was called */ int a = GETARG_A(inst); /* first element to concatenate */ int total = cast_int(top - 1 - (base + a)); /* yet to concatenate */ setobjs2s(L, top - 2, top); /* put TM result in proper position */ - L->top = top - 1; /* top is one after last element (at top-2) */ + L->top.p = top - 1; /* top is one after last element (at top-2) */ luaV_concat(L, total); /* concat them (may yield again) */ break; } @@ -856,7 +856,7 @@ void luaV_finishOp (lua_State *L) { StkId ra = base + GETARG_A(inst); /* adjust top to signal correct number of returns, in case the return is "up to top" ('isIT') */ - L->top = ra + ci->u2.nres; + L->top.p = ra + ci->u2.nres; /* repeat instruction to close other vars. and complete the return */ ci->u.l.savedpc--; break; @@ -1069,7 +1069,7 @@ void luaV_finishOp (lua_State *L) { #define updatetrap(ci) (trap = ci->u.l.trap) -#define updatebase(ci) (base = ci->func + 1) +#define updatebase(ci) (base = ci->func.p + 1) #define updatestack(ci) \ @@ -1104,7 +1104,7 @@ void luaV_finishOp (lua_State *L) { ** Whenever code can raise errors, the global 'pc' and the global ** 'top' must be correct to report occasional errors. */ -#define savestate(L,ci) (savepc(L), L->top = ci->top) +#define savestate(L,ci) (savepc(L), L->top.p = ci->top.p) /* @@ -1124,7 +1124,7 @@ void luaV_finishOp (lua_State *L) { /* 'c' is the limit of live values in the stack */ #define checkGC(L,c) \ - { luaC_condGC(L, (savepc(L), L->top = (c)), \ + { luaC_condGC(L, (savepc(L), L->top.p = (c)), \ updatetrap(ci)); \ luai_threadyield(L); } @@ -1155,7 +1155,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { startfunc: trap = L->hookmask; returning: /* trap already set */ - cl = clLvalue(s2v(ci->func)); + cl = clLvalue(s2v(ci->func.p)); k = cl->p->k; pc = ci->u.l.savedpc; if (l_unlikely(trap)) { @@ -1167,7 +1167,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { } ci->u.l.trap = 1; /* assume trap is on, for now */ } - base = ci->func + 1; + base = ci->func.p + 1; /* main loop of interpreter */ for (;;) { Instruction i; /* instruction being executed */ @@ -1176,10 +1176,10 @@ void luaV_execute (lua_State *L, CallInfo *ci) { /* low-level line tracing for debugging Lua */ printf("line: %d\n", luaG_getfuncline(cl->p, pcRel(pc, cl->p))); #endif - lua_assert(base == ci->func + 1); - lua_assert(base <= L->top && L->top <= L->stack_last); + lua_assert(base == ci->func.p + 1); + lua_assert(base <= L->top.p && L->top.p <= L->stack_last.p); /* invalidate top for instructions not expecting it */ - lua_assert(isIT(i) || (cast_void(L->top = base), 1)); + lua_assert(isIT(i) || (cast_void(L->top.p = base), 1)); vmdispatch (GET_OPCODE(i)) { vmcase(OP_MOVE) { StkId ra = RA(i); @@ -1238,20 +1238,20 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmcase(OP_GETUPVAL) { StkId ra = RA(i); int b = GETARG_B(i); - setobj2s(L, ra, cl->upvals[b]->v); + setobj2s(L, ra, cl->upvals[b]->v.p); vmbreak; } vmcase(OP_SETUPVAL) { StkId ra = RA(i); UpVal *uv = cl->upvals[GETARG_B(i)]; - setobj(L, uv->v, s2v(ra)); + setobj(L, uv->v.p, s2v(ra)); luaC_barrier(L, uv, s2v(ra)); vmbreak; } vmcase(OP_GETTABUP) { StkId ra = RA(i); const TValue *slot; - TValue *upval = cl->upvals[GETARG_B(i)]->v; + TValue *upval = cl->upvals[GETARG_B(i)]->v.p; TValue *rc = KC(i); TString *key = tsvalue(rc); /* key must be a string */ if (luaV_fastget(L, upval, key, slot, luaH_getshortstr)) { @@ -1306,7 +1306,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { } vmcase(OP_SETTABUP) { const TValue *slot; - TValue *upval = cl->upvals[GETARG_A(i)]->v; + TValue *upval = cl->upvals[GETARG_A(i)]->v.p; TValue *rb = KB(i); TValue *rc = RKC(i); TString *key = tsvalue(rb); /* key must be a string */ @@ -1371,7 +1371,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { if (TESTARG_k(i)) /* non-zero extra argument? */ c += GETARG_Ax(*pc) * (MAXARG_C + 1); /* add it to size */ pc++; /* skip extra argument */ - L->top = ra + 1; /* correct top in case of emergency GC */ + L->top.p = ra + 1; /* correct top in case of emergency GC */ t = luaH_new(L); /* memory allocation */ sethvalue2s(L, ra, t); if (b != 0 || c != 0) @@ -1578,9 +1578,9 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmcase(OP_CONCAT) { StkId ra = RA(i); int n = GETARG_B(i); /* number of elements to concatenate */ - L->top = ra + n; /* mark the end of concat operands */ + L->top.p = ra + n; /* mark the end of concat operands */ ProtectNT(luaV_concat(L, n)); - checkGC(L, L->top); /* 'luaV_concat' ensures correct top */ + checkGC(L, L->top.p); /* 'luaV_concat' ensures correct top */ vmbreak; } vmcase(OP_CLOSE) { @@ -1674,7 +1674,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { int b = GETARG_B(i); int nresults = GETARG_C(i) - 1; if (b != 0) /* fixed number of arguments? */ - L->top = ra + b; /* top signals number of arguments */ + L->top.p = ra + b; /* top signals number of arguments */ /* else previous instruction set top */ savepc(L); /* in case of errors */ if ((newci = luaD_precall(L, ra, nresults)) == NULL) @@ -1693,19 +1693,19 @@ void luaV_execute (lua_State *L, CallInfo *ci) { /* delta is virtual 'func' - real 'func' (vararg functions) */ int delta = (nparams1) ? ci->u.l.nextraargs + nparams1 : 0; if (b != 0) - L->top = ra + b; + L->top.p = ra + b; else /* previous instruction set top */ - b = cast_int(L->top - ra); + b = cast_int(L->top.p - ra); savepc(ci); /* several calls here can raise errors */ if (TESTARG_k(i)) { luaF_closeupval(L, base); /* close upvalues from current call */ - lua_assert(L->tbclist < base); /* no pending tbc variables */ - lua_assert(base == ci->func + 1); + lua_assert(L->tbclist.p < base); /* no pending tbc variables */ + lua_assert(base == ci->func.p + 1); } if ((n = luaD_pretailcall(L, ci, ra, b, delta)) < 0) /* Lua function? */ goto startfunc; /* execute the callee */ else { /* C function? */ - ci->func -= delta; /* restore 'func' (if vararg) */ + ci->func.p -= delta; /* restore 'func' (if vararg) */ luaD_poscall(L, ci, n); /* finish caller */ updatetrap(ci); /* 'luaD_poscall' can change hooks */ goto ret; /* caller returns after the tail call */ @@ -1716,19 +1716,19 @@ void luaV_execute (lua_State *L, CallInfo *ci) { int n = GETARG_B(i) - 1; /* number of results */ int nparams1 = GETARG_C(i); if (n < 0) /* not fixed? */ - n = cast_int(L->top - ra); /* get what is available */ + n = cast_int(L->top.p - ra); /* get what is available */ savepc(ci); if (TESTARG_k(i)) { /* may there be open upvalues? */ ci->u2.nres = n; /* save number of returns */ - if (L->top < ci->top) - L->top = ci->top; + if (L->top.p < ci->top.p) + L->top.p = ci->top.p; luaF_close(L, base, CLOSEKTOP, 1); updatetrap(ci); updatestack(ci); } if (nparams1) /* vararg function? */ - ci->func -= ci->u.l.nextraargs + nparams1; - L->top = ra + n; /* set call for 'luaD_poscall' */ + ci->func.p -= ci->u.l.nextraargs + nparams1; + L->top.p = ra + n; /* set call for 'luaD_poscall' */ luaD_poscall(L, ci, n); updatetrap(ci); /* 'luaD_poscall' can change hooks */ goto ret; @@ -1736,7 +1736,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmcase(OP_RETURN0) { if (l_unlikely(L->hookmask)) { StkId ra = RA(i); - L->top = ra; + L->top.p = ra; savepc(ci); luaD_poscall(L, ci, 0); /* no hurry... */ trap = 1; @@ -1744,16 +1744,16 @@ void luaV_execute (lua_State *L, CallInfo *ci) { else { /* do the 'poscall' here */ int nres; L->ci = ci->previous; /* back to caller */ - L->top = base - 1; + L->top.p = base - 1; for (nres = ci->nresults; l_unlikely(nres > 0); nres--) - setnilvalue(s2v(L->top++)); /* all results are nil */ + setnilvalue(s2v(L->top.p++)); /* all results are nil */ } goto ret; } vmcase(OP_RETURN1) { if (l_unlikely(L->hookmask)) { StkId ra = RA(i); - L->top = ra + 1; + L->top.p = ra + 1; savepc(ci); luaD_poscall(L, ci, 1); /* no hurry... */ trap = 1; @@ -1762,13 +1762,13 @@ void luaV_execute (lua_State *L, CallInfo *ci) { int nres = ci->nresults; L->ci = ci->previous; /* back to caller */ if (nres == 0) - L->top = base - 1; /* asked for no results */ + L->top.p = base - 1; /* asked for no results */ else { StkId ra = RA(i); setobjs2s(L, base - 1, ra); /* at least this result */ - L->top = base; + L->top.p = base; for (; l_unlikely(nres > 1); nres--) - setnilvalue(s2v(L->top++)); /* complete missing results */ + setnilvalue(s2v(L->top.p++)); /* complete missing results */ } } ret: /* return from a Lua function */ @@ -1824,7 +1824,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { */ /* push function, state, and control variable */ memcpy(ra + 4, ra, 3 * sizeof(*ra)); - L->top = ra + 4 + 3; + L->top.p = ra + 4 + 3; ProtectNT(luaD_call(L, ra + 4, GETARG_C(i))); /* do the call */ updatestack(ci); /* stack may have changed */ i = *(pc++); /* go to next instruction */ @@ -1846,9 +1846,9 @@ void luaV_execute (lua_State *L, CallInfo *ci) { unsigned int last = GETARG_C(i); Table *h = hvalue(s2v(ra)); if (n == 0) - n = cast_int(L->top - ra) - 1; /* get up to the top */ + n = cast_int(L->top.p - ra) - 1; /* get up to the top */ else - L->top = ci->top; /* correct top in case of emergency GC */ + L->top.p = ci->top.p; /* correct top in case of emergency GC */ last += n; if (TESTARG_k(i)) { last += GETARG_Ax(*pc) * (MAXARG_C + 1); From ee645472ebe153e2c6669c84a632297a8110bdb6 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 31 Oct 2022 15:06:20 -0300 Subject: [PATCH 0775/1145] Stack reallocation done with a single realloc To avoid the need of both the old and the new stack addresses valid at the same time, to correct the pointers to the stack, these pointers are changed to offsets before the reallocation and then changed back to pointers after the reallocation. --- ldo.c | 68 +++++++++++++++++++++++++++++++++++++------------------ lobject.h | 6 +++++ 2 files changed, 52 insertions(+), 22 deletions(-) diff --git a/ldo.c b/ldo.c index d45c74dab5..c30cde76f5 100644 --- a/ldo.c +++ b/ldo.c @@ -157,16 +157,38 @@ int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) { ** Stack reallocation ** =================================================================== */ -static void correctstack (lua_State *L, StkId oldstack, StkId newstack) { + + +/* +** Change all pointers to the stack into offsets. +*/ +static void relstack (lua_State *L) { + CallInfo *ci; + UpVal *up; + L->top.offset = savestack(L, L->top.p); + L->tbclist.offset = savestack(L, L->tbclist.p); + for (up = L->openupval; up != NULL; up = up->u.open.next) + up->v.offset = savestack(L, uplevel(up)); + for (ci = L->ci; ci != NULL; ci = ci->previous) { + ci->top.offset = savestack(L, ci->top.p); + ci->func.offset = savestack(L, ci->func.p); + } +} + + +/* +** Change back all offsets into pointers. +*/ +static void correctstack (lua_State *L) { CallInfo *ci; UpVal *up; - L->top.p = (L->top.p - oldstack) + newstack; - L->tbclist.p = (L->tbclist.p - oldstack) + newstack; + L->top.p = restorestack(L, L->top.offset); + L->tbclist.p = restorestack(L, L->tbclist.offset); for (up = L->openupval; up != NULL; up = up->u.open.next) - up->v.p = s2v((uplevel(up) - oldstack) + newstack); + up->v.p = s2v(restorestack(L, up->v.offset)); for (ci = L->ci; ci != NULL; ci = ci->previous) { - ci->top.p = (ci->top.p - oldstack) + newstack; - ci->func.p = (ci->func.p - oldstack) + newstack; + ci->top.p = restorestack(L, ci->top.offset); + ci->func.p = restorestack(L, ci->func.offset); if (isLua(ci)) ci->u.l.trap = 1; /* signal to update 'trap' in 'luaV_execute' */ } @@ -177,36 +199,38 @@ static void correctstack (lua_State *L, StkId oldstack, StkId newstack) { #define ERRORSTACKSIZE (LUAI_MAXSTACK + 200) /* -** Reallocate the stack to a new size, correcting all pointers into -** it. (There are pointers to a stack from its upvalues, from its list -** of call infos, plus a few individual pointers.) The reallocation is -** done in two steps (allocation + free) because the correction must be -** done while both addresses (the old stack and the new one) are valid. -** (In ISO C, any pointer use after the pointer has been deallocated is -** undefined behavior.) +** Reallocate the stack to a new size, correcting all pointers into it. +** In ISO C, any pointer use after the pointer has been deallocated is +** undefined behavior. So, before the reallocation, all pointers are +** changed to offsets, and after the reallocation they are changed back +** to pointers. As during the reallocation the pointers are invalid, the +** reallocation cannot run emergency collections. +** ** In case of allocation error, raise an error or return false according ** to 'raiseerror'. */ int luaD_reallocstack (lua_State *L, int newsize, int raiseerror) { int oldsize = stacksize(L); int i; - StkId newstack = luaM_reallocvector(L, NULL, 0, - newsize + EXTRA_STACK, StackValue); + StkId newstack; + int oldgcstop = G(L)->gcstopem; lua_assert(newsize <= LUAI_MAXSTACK || newsize == ERRORSTACKSIZE); + relstack(L); /* change pointers to offsets */ + G(L)->gcstopem = 1; /* stop emergency collection */ + newstack = luaM_reallocvector(L, L->stack.p, oldsize + EXTRA_STACK, + newsize + EXTRA_STACK, StackValue); + G(L)->gcstopem = oldgcstop; /* restore emergency collection */ if (l_unlikely(newstack == NULL)) { /* reallocation failed? */ + correctstack(L); /* change offsets back to pointers */ if (raiseerror) luaM_error(L); else return 0; /* do not raise an error */ } - /* number of elements to be copied to the new stack */ - i = ((oldsize <= newsize) ? oldsize : newsize) + EXTRA_STACK; - memcpy(newstack, L->stack.p, i * sizeof(StackValue)); - for (; i < newsize + EXTRA_STACK; i++) - setnilvalue(s2v(newstack + i)); /* erase new segment */ - correctstack(L, L->stack.p, newstack); - luaM_freearray(L, L->stack.p, oldsize + EXTRA_STACK); L->stack.p = newstack; + correctstack(L); /* change offsets back to pointers */ L->stack_last.p = L->stack.p + newsize; + for (i = oldsize + EXTRA_STACK; i < newsize + EXTRA_STACK; i++) + setnilvalue(s2v(newstack + i)); /* erase new segment */ return 1; } diff --git a/lobject.h b/lobject.h index 7af7bd892c..e7f58cbd4b 100644 --- a/lobject.h +++ b/lobject.h @@ -158,8 +158,13 @@ typedef union StackValue { typedef StackValue *StkId; +/* +** When reallocating the stack, change all pointers to the stack into +** proper offsets. +*/ typedef union { StkId p; /* actual pointer */ + ptrdiff_t offset; /* used while the stack is being reallocated */ } StkIdRel; @@ -626,6 +631,7 @@ typedef struct UpVal { lu_byte tbc; /* true if it represents a to-be-closed variable */ union { TValue *p; /* points to stack or to its own value */ + ptrdiff_t offset; /* used while the stack is being reallocated */ } v; union { struct { /* (when open) */ From 8047b2d03eaaeee44871a11f8d3a3135f2639b1a Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 1 Nov 2022 15:42:08 -0300 Subject: [PATCH 0776/1145] Tables have a 'lastfree' information only when needed Only tables with some minimum number of entries in their hash part have a 'lastfree' field, kept in a header before the node vector. --- lmem.h | 2 ++ lobject.h | 1 - ltable.c | 80 +++++++++++++++++++++++++++++++++++++--------- ltable.h | 14 ++++++-- ltests.c | 3 +- ltm.h | 4 +-- testes/nextvar.lua | 4 +-- 7 files changed, 84 insertions(+), 24 deletions(-) diff --git a/lmem.h b/lmem.h index 8c75a44beb..c5dada9cb3 100644 --- a/lmem.h +++ b/lmem.h @@ -63,6 +63,8 @@ #define luaM_newobject(L,tag,s) luaM_malloc_(L, (s), tag) +#define luaM_newblock(L, size) luaM_newvector(L, size, char) + #define luaM_growvector(L,v,nelems,size,t,limit,e) \ ((v)=cast(t *, luaM_growaux_(L,v,nelems,&(size),sizeof(t), \ luaM_limitN(limit,t),e))) diff --git a/lobject.h b/lobject.h index e7f58cbd4b..74a6fd1e25 100644 --- a/lobject.h +++ b/lobject.h @@ -744,7 +744,6 @@ typedef struct Table { unsigned int alimit; /* "limit" of 'array' array */ TValue *array; /* array part */ Node *node; - Node *lastfree; /* any free position is before this position */ struct Table *metatable; GCObject *gclist; } Table; diff --git a/ltable.c b/ltable.c index cc7993e083..485563f37f 100644 --- a/ltable.c +++ b/ltable.c @@ -39,6 +39,27 @@ #include "lvm.h" +/* +** Only tables with hash parts larget than LIMFORLAST has a 'lastfree' +** field that optimizes finding a free slot. Smaller tables do a +** complete search when looking for a free slot. +*/ +#define LLIMFORLAST 2 /* log2 of LIMTFORLAST */ +#define LIMFORLAST twoto(LLIMFORLAST) + +/* +** Union to store an int field ensuring that what follows it in +** memory is properly aligned to store a TValue. +*/ +typedef union { + int lastfree; + char padding[offsetof(struct { int i; TValue v; }, v)]; +} Limbox; + +#define haslastfree(t) ((t)->lsizenode > LLIMFORLAST) +#define getlastfree(t) (&((cast(Limbox *, (t)->node) - 1)->lastfree)) + + /* ** MAXABITS is the largest integer such that MAXASIZE fits in an ** unsigned int. @@ -367,8 +388,15 @@ int luaH_next (lua_State *L, Table *t, StkId key) { static void freehash (lua_State *L, Table *t) { - if (!isdummy(t)) - luaM_freearray(L, t->node, cast_sizet(sizenode(t))); + if (!isdummy(t)) { + size_t bsize = sizenode(t) * sizeof(Node); /* 'node' size in bytes */ + char *arr = cast_charp(t->node); + if (haslastfree(t)) { + bsize += sizeof(Limbox); + arr -= sizeof(Limbox); + } + luaM_freearray(L, arr, bsize); + } } @@ -479,7 +507,7 @@ static void setnodevector (lua_State *L, Table *t, unsigned int size) { if (size == 0) { /* no elements to hash part? */ t->node = cast(Node *, dummynode); /* use common 'dummynode' */ t->lsizenode = 0; - t->lastfree = NULL; /* signal that it is using dummy node */ + setdummy(t); /* signal that it is using dummy node */ } else { int i; @@ -487,15 +515,22 @@ static void setnodevector (lua_State *L, Table *t, unsigned int size) { if (lsize > MAXHBITS || (1u << lsize) > MAXHSIZE) luaG_runerror(L, "table overflow"); size = twoto(lsize); - t->node = luaM_newvector(L, size, Node); + if (lsize <= LLIMFORLAST) /* no 'lastfree' field? */ + t->node = luaM_newvector(L, size, Node); + else { + size_t bsize = size * sizeof(Node) + sizeof(Limbox); + char *node = luaM_newblock(L, bsize); + t->node = cast(Node *, node + sizeof(Limbox)); + *getlastfree(t) = size; /* all positions are free */ + } + t->lsizenode = cast_byte(lsize); + setnodummy(t); for (i = 0; i < cast_int(size); i++) { Node *n = gnode(t, i); gnext(n) = 0; setnilkey(n); setempty(gval(n)); } - t->lsizenode = cast_byte(lsize); - t->lastfree = gnode(t, size); /* all positions are free */ } } @@ -520,18 +555,21 @@ static void reinsert (lua_State *L, Table *ot, Table *t) { /* -** Exchange the hash part of 't1' and 't2'. +** Exchange the hash part of 't1' and 't2'. (In 'flags', only the +** dummy bit must be exchanged: The 'isrealasize' is not related +** to the hash part, and the metamethod bits do not change during +** a resize, so the "real" table can keep their values.) */ static void exchangehashpart (Table *t1, Table *t2) { lu_byte lsizenode = t1->lsizenode; Node *node = t1->node; - Node *lastfree = t1->lastfree; + int bitdummy1 = t1->flags & BITDUMMY; t1->lsizenode = t2->lsizenode; t1->node = t2->node; - t1->lastfree = t2->lastfree; + t1->flags = (t1->flags & NOTBITDUMMY) | (t2->flags & BITDUMMY); t2->lsizenode = lsizenode; t2->node = node; - t2->lastfree = lastfree; + t2->flags = (t2->flags & NOTBITDUMMY) | bitdummy1; } @@ -555,6 +593,7 @@ void luaH_resize (lua_State *L, Table *t, unsigned int newasize, unsigned int oldasize = setlimittosize(t); TValue *newarray; /* create new hash part with appropriate size into 'newt' */ + newt.flags = 0; setnodevector(L, &newt, nhsize); if (newasize < oldasize) { /* will array shrink? */ t->alimit = newasize; /* pretend array has new size... */ @@ -641,11 +680,22 @@ void luaH_free (lua_State *L, Table *t) { static Node *getfreepos (Table *t) { - if (!isdummy(t)) { - while (t->lastfree > t->node) { - t->lastfree--; - if (keyisnil(t->lastfree)) - return t->lastfree; + if (haslastfree(t)) { /* does it have 'lastfree' information? */ + /* look for a spot before 'lastfree', updating 'lastfree' */ + while (*getlastfree(t) > 0) { + Node *free = gnode(t, --(*getlastfree(t))); + if (keyisnil(free)) + return free; + } + } + else { /* no 'lastfree' information */ + if (!isdummy(t)) { + int i = sizenode(t); + while (i--) { /* do a linear search */ + Node *free = gnode(t, i); + if (keyisnil(free)) + return free; + } } } return NULL; /* could not find a free place */ diff --git a/ltable.h b/ltable.h index 75dd9e26e0..dce8c2f75c 100644 --- a/ltable.h +++ b/ltable.h @@ -23,8 +23,18 @@ #define invalidateTMcache(t) ((t)->flags &= ~maskflags) -/* true when 't' is using 'dummynode' as its hash part */ -#define isdummy(t) ((t)->lastfree == NULL) +/* +** Bit BITDUMMY set in 'flags' means the table is using the dummy node +** for its hash part. +*/ + +#define BITDUMMY (1 << 6) +#define NOTBITDUMMY cast_byte(~BITDUMMY) +#define isdummy(t) ((t)->flags & BITDUMMY) + +#define setnodummy(t) ((t)->flags &= NOTBITDUMMY) +#define setdummy(t) ((t)->flags |= BITDUMMY) + /* allocated size for hash nodes */ diff --git a/ltests.c b/ltests.c index 4a0a6af1fd..1caed04c9b 100644 --- a/ltests.c +++ b/ltests.c @@ -999,9 +999,8 @@ static int table_query (lua_State *L) { if (i == -1) { lua_pushinteger(L, asize); lua_pushinteger(L, allocsizenode(t)); - lua_pushinteger(L, isdummy(t) ? 0 : t->lastfree - t->node); lua_pushinteger(L, t->alimit); - return 4; + return 3; } else if ((unsigned int)i < asize) { lua_pushinteger(L, i); diff --git a/ltm.h b/ltm.h index 73b833c605..f387265585 100644 --- a/ltm.h +++ b/ltm.h @@ -48,8 +48,8 @@ typedef enum { /* ** Mask with 1 in all fast-access methods. A 1 in any of these bits ** in the flag of a (meta)table means the metatable does not have the -** corresponding metamethod field. (Bit 7 of the flag is used for -** 'isrealasize'.) +** corresponding metamethod field. (Bit 6 of the flag indicates that +** the table is using the dummy node; bit 7 is used for 'isrealasize'.) */ #define maskflags (~(~0u << (TM_EQ + 1))) diff --git a/testes/nextvar.lua b/testes/nextvar.lua index 0874e5bb22..80b3d05cd6 100644 --- a/testes/nextvar.lua +++ b/testes/nextvar.lua @@ -210,9 +210,9 @@ assert(T.querytab(a) == 64) -- array part has 64 elements a[32] = true; a[48] = true; -- binary search will find these ones a[51] = true -- binary search will miss this one assert(#a == 48) -- this will set the limit -assert(select(4, T.querytab(a)) == 48) -- this is the limit now +assert(select(3, T.querytab(a)) == 48) -- this is the limit now a[50] = true -- this will set a new limit -assert(select(4, T.querytab(a)) == 50) -- this is the limit now +assert(select(3, T.querytab(a)) == 50) -- this is the limit now -- but the size is larger (and still inside the array part) assert(#a == 51) From 9ede317c70ad82279f2e3962eb52904a17bf4b55 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 1 Nov 2022 17:14:01 -0300 Subject: [PATCH 0777/1145] Threads are created like other objects Using a version of 'luaC_newobj' that allows offsets (extra space before the object itself). --- lgc.c | 14 ++++++++++---- lgc.h | 2 ++ lstate.c | 12 ++++-------- 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/lgc.c b/lgc.c index 8e76ccd7a3..2e74990256 100644 --- a/lgc.c +++ b/lgc.c @@ -252,12 +252,13 @@ void luaC_fix (lua_State *L, GCObject *o) { /* -** create a new collectable object (with given type and size) and link -** it to 'allgc' list. +** create a new collectable object (with given type, size, and offset) +** and link it to 'allgc' list. */ -GCObject *luaC_newobj (lua_State *L, int tt, size_t sz) { +GCObject *luaC_newobjdt (lua_State *L, int tt, size_t sz, size_t offset) { global_State *g = G(L); - GCObject *o = cast(GCObject *, luaM_newobject(L, novariant(tt), sz)); + char *p = cast_charp(luaM_newobject(L, novariant(tt), sz)); + GCObject *o = cast(GCObject *, p + offset); o->marked = luaC_white(g); o->tt = tt; o->next = g->allgc; @@ -265,6 +266,11 @@ GCObject *luaC_newobj (lua_State *L, int tt, size_t sz) { return o; } + +GCObject *luaC_newobj (lua_State *L, int tt, size_t sz) { + return luaC_newobjdt(L, tt, sz, 0); +} + /* }====================================================== */ diff --git a/lgc.h b/lgc.h index 4a125634b9..c960e70647 100644 --- a/lgc.h +++ b/lgc.h @@ -190,6 +190,8 @@ LUAI_FUNC void luaC_step (lua_State *L); LUAI_FUNC void luaC_runtilstate (lua_State *L, int statesmask); LUAI_FUNC void luaC_fullgc (lua_State *L, int isemergency); LUAI_FUNC GCObject *luaC_newobj (lua_State *L, int tt, size_t sz); +LUAI_FUNC GCObject *luaC_newobjdt (lua_State *L, int tt, size_t sz, + size_t offset); LUAI_FUNC void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v); LUAI_FUNC void luaC_barrierback_ (lua_State *L, GCObject *o); LUAI_FUNC void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt); diff --git a/lstate.c b/lstate.c index c63fe8675b..1fbefb4b14 100644 --- a/lstate.c +++ b/lstate.c @@ -284,18 +284,14 @@ static void close_state (lua_State *L) { LUA_API lua_State *lua_newthread (lua_State *L) { - global_State *g; + global_State *g = G(L); + GCObject *o; lua_State *L1; lua_lock(L); - g = G(L); luaC_checkGC(L); /* create new thread */ - L1 = &cast(LX *, luaM_newobject(L, LUA_TTHREAD, sizeof(LX)))->l; - L1->marked = luaC_white(g); - L1->tt = LUA_VTHREAD; - /* link it on list 'allgc' */ - L1->next = g->allgc; - g->allgc = obj2gco(L1); + o = luaC_newobjdt(L, LUA_TTHREAD, sizeof(LX), offsetof(LX, l)); + L1 = gco2th(o); /* anchor it on L stack */ setthvalue2s(L, L->top.p, L1); api_incr_top(L); From 76953316d1283ab6324b59b914ef53a521408444 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 3 Nov 2022 16:37:13 -0300 Subject: [PATCH 0778/1145] Added a counter of the total number of existing objects It may simplify the control of the garbage collector. --- lgc.c | 2 ++ lstate.c | 2 ++ lstate.h | 1 + 3 files changed, 5 insertions(+) diff --git a/lgc.c b/lgc.c index 2e74990256..20e9b4aafe 100644 --- a/lgc.c +++ b/lgc.c @@ -259,6 +259,7 @@ GCObject *luaC_newobjdt (lua_State *L, int tt, size_t sz, size_t offset) { global_State *g = G(L); char *p = cast_charp(luaM_newobject(L, novariant(tt), sz)); GCObject *o = cast(GCObject *, p + offset); + g->totalobjs++; o->marked = luaC_white(g); o->tt = tt; o->next = g->allgc; @@ -768,6 +769,7 @@ static void freeupval (lua_State *L, UpVal *uv) { static void freeobj (lua_State *L, GCObject *o) { + G(L)->totalobjs--; switch (o->tt) { case LUA_VPROTO: luaF_freeproto(L, gco2p(o)); diff --git a/lstate.c b/lstate.c index 1fbefb4b14..3091a00e64 100644 --- a/lstate.c +++ b/lstate.c @@ -279,6 +279,7 @@ static void close_state (lua_State *L) { luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size); freestack(L); lua_assert(gettotalbytes(g) == sizeof(LG)); + lua_assert(g->totalobjs == 1); (*g->frealloc)(g->ud, fromstate(L), sizeof(LG), 0); /* free main block */ } @@ -387,6 +388,7 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { g->weak = g->ephemeron = g->allweak = NULL; g->twups = NULL; g->totalbytes = sizeof(LG); + g->totalobjs = 1; g->GCdebt = 0; g->lastatomic = 0; setivalue(&g->nilvalue, 0); /* to signal that state is not yet built */ diff --git a/lstate.h b/lstate.h index 2e90781872..62ad61c6b8 100644 --- a/lstate.h +++ b/lstate.h @@ -250,6 +250,7 @@ typedef struct global_State { lua_Alloc frealloc; /* function to reallocate memory */ void *ud; /* auxiliary data to 'frealloc' */ l_mem totalbytes; /* number of bytes currently allocated - GCdebt */ + l_mem totalobjs; /* total number of objects allocated */ l_mem GCdebt; /* bytes allocated not yet compensated by the collector */ lu_mem GCestimate; /* an estimate of the non-garbage memory in use */ lu_mem lastatomic; /* see function 'genstep' in file 'lgc.c' */ From be908a7d4d8130264ad67c5789169769f824c5d1 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 8 Nov 2022 10:15:10 -0300 Subject: [PATCH 0779/1145] Removed unused field 'UpVal.tbc' --- lfunc.c | 5 ++--- lobject.h | 1 - 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/lfunc.c b/lfunc.c index 804bf9dcda..0945f241de 100644 --- a/lfunc.c +++ b/lfunc.c @@ -62,12 +62,11 @@ void luaF_initupvals (lua_State *L, LClosure *cl) { ** Create a new upvalue at the given level, and link it to the list of ** open upvalues of 'L' after entry 'prev'. **/ -static UpVal *newupval (lua_State *L, int tbc, StkId level, UpVal **prev) { +static UpVal *newupval (lua_State *L, StkId level, UpVal **prev) { GCObject *o = luaC_newobj(L, LUA_VUPVAL, sizeof(UpVal)); UpVal *uv = gco2upv(o); UpVal *next = *prev; uv->v.p = s2v(level); /* current value lives in the stack */ - uv->tbc = tbc; uv->u.open.next = next; /* link it to list of open upvalues */ uv->u.open.previous = prev; if (next) @@ -96,7 +95,7 @@ UpVal *luaF_findupval (lua_State *L, StkId level) { pp = &p->u.open.next; } /* not found: create a new upvalue after 'pp' */ - return newupval(L, 0, level, pp); + return newupval(L, level, pp); } diff --git a/lobject.h b/lobject.h index e7f58cbd4b..556608e4aa 100644 --- a/lobject.h +++ b/lobject.h @@ -628,7 +628,6 @@ typedef struct Proto { */ typedef struct UpVal { CommonHeader; - lu_byte tbc; /* true if it represents a to-be-closed variable */ union { TValue *p; /* points to stack or to its own value */ ptrdiff_t offset; /* used while the stack is being reallocated */ From 9a77f57edc5cc24c2ab71d416b7481a5679e3869 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 23 Nov 2022 14:17:28 -0300 Subject: [PATCH 0780/1145] Stop GC while building initial state --- lua.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lua.c b/lua.c index 7f7dc2b22a..715430a0de 100644 --- a/lua.c +++ b/lua.c @@ -633,7 +633,8 @@ static int pmain (lua_State *L) { } luaL_openlibs(L); /* open standard libraries */ createargtable(L, argv, argc, script); /* create table 'arg' */ - lua_gc(L, LUA_GCGEN, 0, 0); /* GC in generational mode */ + lua_gc(L, LUA_GCRESTART); /* start GC... */ + lua_gc(L, LUA_GCGEN, 0, 0); /* ...in generational mode */ if (!(args & has_E)) { /* no option '-E'? */ if (handle_luainit(L) != LUA_OK) /* run LUA_INIT */ return 0; /* error running LUA_INIT */ @@ -665,6 +666,7 @@ int main (int argc, char **argv) { l_message(argv[0], "cannot create state: not enough memory"); return EXIT_FAILURE; } + lua_gc(L, LUA_GCSTOP); /* stop GC while buidling state */ lua_pushcfunction(L, &pmain); /* to call 'pmain' in protected mode */ lua_pushinteger(L, argc); /* 1st argument */ lua_pushlightuserdata(L, argv); /* 2nd argument */ From f356d5acdd9d8e8f7e9d1d7632c4657f945ff4f4 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 23 Nov 2022 17:17:20 -0300 Subject: [PATCH 0781/1145] First version of GC counting objects for control Still needs to review generational mode. --- lapi.c | 9 +- lgc.c | 253 ++++++++++++++++++++++++++---------------------------- lgc.h | 6 +- llimits.h | 2 +- lmem.c | 8 +- lstate.c | 15 ++-- lstate.h | 10 ++- ltests.c | 11 +++ lua.c | 4 +- 9 files changed, 162 insertions(+), 156 deletions(-) diff --git a/lapi.c b/lapi.c index 34e64af142..3c620d4b52 100644 --- a/lapi.c +++ b/lapi.c @@ -1154,11 +1154,11 @@ LUA_API int lua_gc (lua_State *L, int what, ...) { } case LUA_GCCOUNT: { /* GC values are expressed in Kbytes: #bytes/2^10 */ - res = cast_int(gettotalbytes(g) >> 10); + res = cast_int(g->totalbytes >> 10); break; } case LUA_GCCOUNTB: { - res = cast_int(gettotalbytes(g) & 0x3ff); + res = cast_int(g->totalbytes & 0x3ff); break; } case LUA_GCSTEP: { @@ -1171,7 +1171,7 @@ LUA_API int lua_gc (lua_State *L, int what, ...) { luaC_step(L); } else { /* add 'data' to total debt */ - debt = cast(l_mem, data) * 1024 + g->GCdebt; + debt = data + g->GCdebt; luaE_setdebt(g, debt); luaC_checkGC(L); } @@ -1217,7 +1217,8 @@ LUA_API int lua_gc (lua_State *L, int what, ...) { if (stepmul != 0) setgcparam(g->gcstepmul, stepmul); if (stepsize != 0) - g->gcstepsize = stepsize; + g->gcstepsize = (stepsize <= log2maxs(l_mem)) ? stepsize + : log2maxs(l_mem); luaC_changemode(L, KGC_INC); break; } diff --git a/lgc.c b/lgc.c index 20e9b4aafe..9c5cc2e3d5 100644 --- a/lgc.c +++ b/lgc.c @@ -19,6 +19,7 @@ #include "ldo.h" #include "lfunc.h" #include "lgc.h" +#include "llex.h" #include "lmem.h" #include "lobject.h" #include "lstate.h" @@ -32,7 +33,7 @@ ** (Large enough to dissipate fixed overheads but small enough ** to allow small steps for the collector.) */ -#define GCSWEEPMAX 100 +#define GCSWEEPMAX 20 /* ** Maximum number of finalizers to call in each single step. @@ -46,19 +47,6 @@ #define GCFINALIZECOST 50 -/* -** The equivalent, in bytes, of one unit of "work" (visiting a slot, -** sweeping an object, etc.) -*/ -#define WORK2MEM sizeof(TValue) - - -/* -** macro to adjust 'pause': 'pause' is actually used like -** 'pause / PAUSEADJ' (value chosen by tests) -*/ -#define PAUSEADJ 100 - /* mask with all color bits */ #define maskcolors (bitmask(BLACKBIT) | WHITEBITS) @@ -105,7 +93,7 @@ #define markobjectN(g,t) { if (t) markobject(g,t); } static void reallymarkobject (global_State *g, GCObject *o); -static lu_mem atomic (lua_State *L); +static l_mem atomic (lua_State *L); static void entersweep (lua_State *L); @@ -259,7 +247,7 @@ GCObject *luaC_newobjdt (lua_State *L, int tt, size_t sz, size_t offset) { global_State *g = G(L); char *p = cast_charp(luaM_newobject(L, novariant(tt), sz)); GCObject *o = cast(GCObject *, p + offset); - g->totalobjs++; + g->GCdebt++; o->marked = luaC_white(g); o->tt = tt; o->next = g->allgc; @@ -268,6 +256,9 @@ GCObject *luaC_newobjdt (lua_State *L, int tt, size_t sz, size_t offset) { } +/* +** create a new collectable object with no offset. +*/ GCObject *luaC_newobj (lua_State *L, int tt, size_t sz) { return luaC_newobjdt(L, tt, sz, 0); } @@ -296,6 +287,7 @@ GCObject *luaC_newobj (lua_State *L, int tt, size_t sz) { ** (only closures can), and a userdata's metatable must be a table. */ static void reallymarkobject (global_State *g, GCObject *o) { + g->marked++; switch (o->tt) { case LUA_VSHRSTR: case LUA_VLNGSTR: { @@ -343,9 +335,9 @@ static void markmt (global_State *g) { /* ** mark all objects in list of being-finalized */ -static lu_mem markbeingfnz (global_State *g) { +static l_mem markbeingfnz (global_State *g) { GCObject *o; - lu_mem count = 0; + l_mem count = 0; for (o = g->tobefnz; o != NULL; o = o->next) { count++; markobject(g, o); @@ -365,12 +357,11 @@ static lu_mem markbeingfnz (global_State *g) { ** upvalues, as they have nothing to be checked. (If the thread gets an ** upvalue later, it will be linked in the list again.) */ -static int remarkupvals (global_State *g) { +static l_mem remarkupvals (global_State *g) { + l_mem work = 0; lua_State *thread; lua_State **p = &g->twups; - int work = 0; /* estimate of how much work was done here */ while ((thread = *p) != NULL) { - work++; if (!iswhite(thread) && thread->openupval != NULL) p = &thread->twups; /* keep marked thread with upvalues in the list */ else { /* thread is not marked or without upvalues */ @@ -380,13 +371,13 @@ static int remarkupvals (global_State *g) { thread->twups = thread; /* mark that it is out of list */ for (uv = thread->openupval; uv != NULL; uv = uv->u.open.next) { lua_assert(getage(uv) <= getage(thread)); - work++; if (!iswhite(uv)) { /* upvalue already visited? */ lua_assert(upisopen(uv) && isgray(uv)); markvalue(g, uv->v.p); /* mark its value */ } } } + work++; } return work; } @@ -399,10 +390,15 @@ static void cleargraylists (global_State *g) { /* -** mark root set and reset all gray lists, to start a new collection +** mark root set and reset all gray lists, to start a new collection. +** 'marked' is initialized with the number of fixed objects in the state, +** to count the total number of live objects during a cycle. (That is +** the metafield names, plus the reserved words, plus "_ENV" plus the +** memory-error message.) */ static void restartcollection (global_State *g) { cleargraylists(g); + g->marked = TM_N + NUM_RESERVED + 2; markobject(g, g->mainthread); markvalue(g, &g->l_registry); markmt(g); @@ -540,7 +536,7 @@ static void traversestrongtable (global_State *g, Table *h) { } -static lu_mem traversetable (global_State *g, Table *h) { +static void traversetable (global_State *g, Table *h) { const char *weakkey, *weakvalue; const TValue *mode = gfasttm(g, h->metatable, TM_MODE); markobjectN(g, h->metatable); @@ -557,17 +553,15 @@ static lu_mem traversetable (global_State *g, Table *h) { } else /* not weak */ traversestrongtable(g, h); - return 1 + h->alimit + 2 * allocsizenode(h); } -static int traverseudata (global_State *g, Udata *u) { +static void traverseudata (global_State *g, Udata *u) { int i; markobjectN(g, u->metatable); /* mark its metatable */ for (i = 0; i < u->nuvalue; i++) markvalue(g, &u->uv[i].uv); genlink(g, obj2gco(u)); - return 1 + u->nuvalue; } @@ -576,7 +570,7 @@ static int traverseudata (global_State *g, Udata *u) { ** arrays can be larger than needed; the extra slots are filled with ** NULL, so the use of 'markobjectN') */ -static int traverseproto (global_State *g, Proto *f) { +static void traverseproto (global_State *g, Proto *f) { int i; markobjectN(g, f->source); for (i = 0; i < f->sizek; i++) /* mark literals */ @@ -587,29 +581,26 @@ static int traverseproto (global_State *g, Proto *f) { markobjectN(g, f->p[i]); for (i = 0; i < f->sizelocvars; i++) /* mark local-variable names */ markobjectN(g, f->locvars[i].varname); - return 1 + f->sizek + f->sizeupvalues + f->sizep + f->sizelocvars; } -static int traverseCclosure (global_State *g, CClosure *cl) { +static void traverseCclosure (global_State *g, CClosure *cl) { int i; for (i = 0; i < cl->nupvalues; i++) /* mark its upvalues */ markvalue(g, &cl->upvalue[i]); - return 1 + cl->nupvalues; } /* ** Traverse a Lua closure, marking its prototype and its upvalues. ** (Both can be NULL while closure is being created.) */ -static int traverseLclosure (global_State *g, LClosure *cl) { +static void traverseLclosure (global_State *g, LClosure *cl) { int i; markobjectN(g, cl->p); /* mark its prototype */ for (i = 0; i < cl->nupvalues; i++) { /* visit its upvalues */ UpVal *uv = cl->upvals[i]; markobjectN(g, uv); /* mark upvalue */ } - return 1 + cl->nupvalues; } @@ -625,13 +616,13 @@ static int traverseLclosure (global_State *g, LClosure *cl) { ** (which can only happen in generational mode) or if the traverse is in ** the propagate phase (which can only happen in incremental mode). */ -static int traversethread (global_State *g, lua_State *th) { +static void traversethread (global_State *g, lua_State *th) { UpVal *uv; StkId o = th->stack.p; if (isold(th) || g->gcstate == GCSpropagate) linkgclist(th, g->grayagain); /* insert into 'grayagain' list */ if (o == NULL) - return 1; /* stack not completely built yet */ + return; /* stack not completely built yet */ lua_assert(g->gcstate == GCSatomic || th->openupval == NULL || isintwups(th)); for (; o < th->top.p; o++) /* mark live elements in the stack */ @@ -649,34 +640,35 @@ static int traversethread (global_State *g, lua_State *th) { } else if (!g->gcemergency) luaD_shrinkstack(th); /* do not change stack in emergency cycle */ - return 1 + stacksize(th); } /* ** traverse one gray object, turning it to black. */ -static lu_mem propagatemark (global_State *g) { +static void propagatemark (global_State *g) { GCObject *o = g->gray; nw2black(o); g->gray = *getgclist(o); /* remove from 'gray' list */ switch (o->tt) { - case LUA_VTABLE: return traversetable(g, gco2t(o)); - case LUA_VUSERDATA: return traverseudata(g, gco2u(o)); - case LUA_VLCL: return traverseLclosure(g, gco2lcl(o)); - case LUA_VCCL: return traverseCclosure(g, gco2ccl(o)); - case LUA_VPROTO: return traverseproto(g, gco2p(o)); - case LUA_VTHREAD: return traversethread(g, gco2th(o)); - default: lua_assert(0); return 0; + case LUA_VTABLE: traversetable(g, gco2t(o)); break; + case LUA_VUSERDATA: traverseudata(g, gco2u(o)); break; + case LUA_VLCL: traverseLclosure(g, gco2lcl(o)); break; + case LUA_VCCL: traverseCclosure(g, gco2ccl(o)); break; + case LUA_VPROTO: traverseproto(g, gco2p(o)); break; + case LUA_VTHREAD: traversethread(g, gco2th(o)); break; + default: lua_assert(0); } } -static lu_mem propagateall (global_State *g) { - lu_mem tot = 0; - while (g->gray) - tot += propagatemark(g); - return tot; +static l_mem propagateall (global_State *g) { + l_mem work = 0; + while (g->gray) { + propagatemark(g); + work++; + } + return work; } @@ -685,10 +677,10 @@ static lu_mem propagateall (global_State *g) { ** Repeat until it converges, that is, nothing new is marked. 'dir' ** inverts the direction of the traversals, trying to speed up ** convergence on chains in the same table. -** */ -static void convergeephemerons (global_State *g) { +static l_mem convergeephemerons (global_State *g) { int changed; + l_mem work = 0; int dir = 0; do { GCObject *w; @@ -703,9 +695,11 @@ static void convergeephemerons (global_State *g) { propagateall(g); /* propagate changes */ changed = 1; /* will have to revisit all ephemeron tables */ } + work++; } dir = !dir; /* invert direction next time */ } while (changed); /* repeat until no more changes */ + return work; } /* }====================================================== */ @@ -721,7 +715,8 @@ static void convergeephemerons (global_State *g) { /* ** clear entries with unmarked keys from all weaktables in list 'l' */ -static void clearbykeys (global_State *g, GCObject *l) { +static l_mem clearbykeys (global_State *g, GCObject *l) { + l_mem work = 0; for (; l; l = gco2t(l)->gclist) { Table *h = gco2t(l); Node *limit = gnodelast(h); @@ -732,7 +727,9 @@ static void clearbykeys (global_State *g, GCObject *l) { if (isempty(gval(n))) /* is entry empty? */ clearkey(n); /* clear its key */ } + work++; } + return work; } @@ -740,7 +737,8 @@ static void clearbykeys (global_State *g, GCObject *l) { ** clear entries with unmarked values from all weaktables in list 'l' up ** to element 'f' */ -static void clearbyvalues (global_State *g, GCObject *l, GCObject *f) { +static l_mem clearbyvalues (global_State *g, GCObject *l, GCObject *f) { + l_mem work = 0; for (; l != f; l = gco2t(l)->gclist) { Table *h = gco2t(l); Node *n, *limit = gnodelast(h); @@ -757,7 +755,9 @@ static void clearbyvalues (global_State *g, GCObject *l, GCObject *f) { if (isempty(gval(n))) /* is entry empty? */ clearkey(n); /* clear its key */ } + work++; } + return work; } @@ -819,10 +819,9 @@ static void freeobj (lua_State *L, GCObject *o) { ** objects, where a dead object is one marked with the old (non current) ** white; change all non-dead objects back to white, preparing for next ** collection cycle. Return where to continue the traversal or NULL if -** list is finished. ('*countout' gets the number of elements traversed.) +** list is finished. */ -static GCObject **sweeplist (lua_State *L, GCObject **p, int countin, - int *countout) { +static GCObject **sweeplist (lua_State *L, GCObject **p, int countin) { global_State *g = G(L); int ow = otherwhite(g); int i; @@ -839,8 +838,6 @@ static GCObject **sweeplist (lua_State *L, GCObject **p, int countin, p = &curr->next; /* go to next element */ } } - if (countout) - *countout = i; /* number of elements traversed */ return (*p == NULL) ? NULL : p; } @@ -851,7 +848,7 @@ static GCObject **sweeplist (lua_State *L, GCObject **p, int countin, static GCObject **sweeptolive (lua_State *L, GCObject **p) { GCObject **old = p; do { - p = sweeplist(L, p, 1, NULL); + p = sweeplist(L, p, 1); } while (p == old); return p; } @@ -870,11 +867,8 @@ static GCObject **sweeptolive (lua_State *L, GCObject **p) { */ static void checkSizes (lua_State *L, global_State *g) { if (!g->gcemergency) { - if (g->strt.nuse < g->strt.size / 4) { /* string table too big? */ - l_mem olddebt = g->GCdebt; + if (g->strt.nuse < g->strt.size / 4) /* string table too big? */ luaS_resize(L, g->strt.size / 2); - g->GCestimate += g->GCdebt - olddebt; /* correct estimate */ - } } } @@ -935,12 +929,11 @@ static void GCTM (lua_State *L) { /* ** Call a few finalizers */ -static int runafewfinalizers (lua_State *L, int n) { +static void runafewfinalizers (lua_State *L, int n) { global_State *g = G(L); int i; for (i = 0; i < n && g->tobefnz; i++) GCTM(L); /* call one finalizer */ - return i; } @@ -1052,19 +1045,16 @@ void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt) { /* ** Set the "time" to wait before starting a new GC cycle; cycle will -** start when memory use hits the threshold of ('estimate' * pause / -** PAUSEADJ). (Division by 'estimate' should be OK: it cannot be zero, -** because Lua cannot even start with less than PAUSEADJ bytes). +** start when number of objects in use hits the threshold of +** approximately ('marked' * pause / 100). (A direct multiplication +** by 'pause' may overflow, and a direct division by 100 may undeflow +** to zero. So, the division is done in two steps. 8 * 12 is near 100 +** and the division by 8 is cheap.) */ static void setpause (global_State *g) { - l_mem threshold, debt; - int pause = getgcparam(g->gcpause); - l_mem estimate = g->GCestimate / PAUSEADJ; /* adjust 'estimate' */ - lua_assert(estimate > 0); - threshold = (pause < MAX_LMEM / estimate) /* overflow? */ - ? estimate * pause /* no overflow */ - : MAX_LMEM; /* overflow; truncate to maximum */ - debt = gettotalbytes(g) - threshold; + unsigned int pause = getgcparam(g->gcpause); + lu_mem threshold = g->marked / 8 * pause / 12; + l_mem debt = gettotalobjs(g) - threshold; if (debt > 0) debt = 0; luaE_setdebt(g, debt); } @@ -1306,17 +1296,17 @@ static void atomic2gen (lua_State *L, global_State *g) { g->gckind = KGC_GEN; g->lastatomic = 0; - g->GCestimate = gettotalbytes(g); /* base for memory control */ + g->GCestimate = gettotalobjs(g); /* base for memory control */ finishgencycle(L, g); } /* ** Set debt for the next minor collection, which will happen when -** memory grows 'genminormul'%. +** total number of objects grows 'genminormul'%. */ static void setminordebt (global_State *g) { - luaE_setdebt(g, -(cast(l_mem, (gettotalbytes(g) / 100)) * g->genminormul)); + luaE_setdebt(g, -(cast(l_mem, (gettotalobjs(g) / 100)) * g->genminormul)); } @@ -1326,14 +1316,12 @@ static void setminordebt (global_State *g) { ** are cleared. Then, turn all objects into old and finishes the ** collection. */ -static lu_mem entergen (lua_State *L, global_State *g) { - lu_mem numobjs; +static void entergen (lua_State *L, global_State *g) { luaC_runtilstate(L, bitmask(GCSpause)); /* prepare to start a new cycle */ luaC_runtilstate(L, bitmask(GCSpropagate)); /* start new cycle */ - numobjs = atomic(L); /* propagates all and then do the atomic stuff */ + atomic(L); /* propagates all and then do the atomic stuff */ atomic2gen(L, g); setminordebt(g); /* set debt assuming next cycle will be minor */ - return numobjs; } @@ -1372,9 +1360,9 @@ void luaC_changemode (lua_State *L, int newmode) { /* ** Does a full collection in generational mode. */ -static lu_mem fullgen (lua_State *L, global_State *g) { +static void fullgen (lua_State *L, global_State *g) { enterinc(g); - return entergen(L, g); + entergen(L, g); } @@ -1400,22 +1388,22 @@ static lu_mem fullgen (lua_State *L, global_State *g) { ** ('g->lastatomic != 0' also means that the last collection was bad.) */ static void stepgenfull (lua_State *L, global_State *g) { - lu_mem newatomic; /* count of traversed objects */ lu_mem lastatomic = g->lastatomic; /* count from last collection */ if (g->gckind == KGC_GEN) /* still in generational mode? */ enterinc(g); /* enter incremental mode */ luaC_runtilstate(L, bitmask(GCSpropagate)); /* start new cycle */ - newatomic = atomic(L); /* mark everybody */ - if (newatomic < lastatomic + (lastatomic >> 3)) { /* good collection? */ + g->marked = 0; + atomic(L); /* mark everybody */ + if (g->marked < lastatomic + (lastatomic >> 3)) { /* good collection? */ atomic2gen(L, g); /* return to generational mode */ setminordebt(g); } else { /* another bad collection; stay in incremental mode */ - g->GCestimate = gettotalbytes(g); /* first estimate */; + g->GCestimate = gettotalobjs(g); /* first estimate */; + g->lastatomic = g->marked; entersweep(L); luaC_runtilstate(L, bitmask(GCSpause)); /* finish collection */ setpause(g); - g->lastatomic = newatomic; } } @@ -1443,21 +1431,23 @@ static void genstep (lua_State *L, global_State *g) { if (g->lastatomic != 0) /* last collection was a bad one? */ stepgenfull(L, g); /* do a full step */ else { - lu_mem majorbase = g->GCestimate; /* memory after last major collection */ - lu_mem majorinc = (majorbase / 100) * getgcparam(g->genmajormul); - if (g->GCdebt > 0 && gettotalbytes(g) > majorbase + majorinc) { - lu_mem numobjs = fullgen(L, g); /* do a major collection */ - if (gettotalbytes(g) < majorbase + (majorinc / 2)) { - /* collected at least half of memory growth since last major + l_mem majorbase = g->GCestimate; /* objects after last major collection */ + l_mem majorinc = (majorbase / 100) * getgcparam(g->genmajormul); + if (g->GCdebt > 0 && gettotalobjs(g) > majorbase + majorinc) { + g->marked = 0; + fullgen(L, g); /* do a major collection */ + if (gettotalobjs(g) < majorbase + (majorinc / 2)) { + /* collected at least half of object growth since last major collection; keep doing minor collections. */ lua_assert(g->lastatomic == 0); } else { /* bad collection */ - g->lastatomic = numobjs; /* signal that last collection was bad */ + g->lastatomic = g->marked; /* signal that last collection was bad */ setpause(g); /* do a long wait for next (major) collection */ } } else { /* regular case; do a minor collection */ + g->marked = 0; youngcollection(L, g); setminordebt(g); g->GCestimate = majorbase; /* preserve base value */ @@ -1522,9 +1512,9 @@ void luaC_freeallobjects (lua_State *L) { } -static lu_mem atomic (lua_State *L) { +static l_mem atomic (lua_State *L) { + l_mem work = 0; global_State *g = G(L); - lu_mem work = 0; GCObject *origweak, *origall; GCObject *grayagain = g->grayagain; /* save original list */ g->grayagain = NULL; @@ -1541,50 +1531,47 @@ static lu_mem atomic (lua_State *L) { work += propagateall(g); /* propagate changes */ g->gray = grayagain; work += propagateall(g); /* traverse 'grayagain' list */ - convergeephemerons(g); + work += convergeephemerons(g); /* at this point, all strongly accessible objects are marked. */ /* Clear values from weak tables, before checking finalizers */ - clearbyvalues(g, g->weak, NULL); - clearbyvalues(g, g->allweak, NULL); + work += clearbyvalues(g, g->weak, NULL); + work += clearbyvalues(g, g->allweak, NULL); origweak = g->weak; origall = g->allweak; separatetobefnz(g, 0); /* separate objects to be finalized */ work += markbeingfnz(g); /* mark objects that will be finalized */ work += propagateall(g); /* remark, to propagate 'resurrection' */ - convergeephemerons(g); + work += convergeephemerons(g); /* at this point, all resurrected objects are marked. */ /* remove dead objects from weak tables */ - clearbykeys(g, g->ephemeron); /* clear keys from all ephemeron tables */ - clearbykeys(g, g->allweak); /* clear keys from all 'allweak' tables */ + work += clearbykeys(g, g->ephemeron); /* clear keys from all ephemeron */ + work += clearbykeys(g, g->allweak); /* clear keys from all 'allweak' */ /* clear values from resurrected weak tables */ - clearbyvalues(g, g->weak, origweak); - clearbyvalues(g, g->allweak, origall); + work += clearbyvalues(g, g->weak, origweak); + work += clearbyvalues(g, g->allweak, origall); luaS_clearcache(g); g->currentwhite = cast_byte(otherwhite(g)); /* flip current white */ lua_assert(g->gray == NULL); - return work; /* estimate of slots marked by 'atomic' */ + return work; } -static int sweepstep (lua_State *L, global_State *g, - int nextstate, GCObject **nextlist) { +static void sweepstep (lua_State *L, global_State *g, + int nextstate, GCObject **nextlist) { if (g->sweepgc) { l_mem olddebt = g->GCdebt; - int count; - g->sweepgc = sweeplist(L, g->sweepgc, GCSWEEPMAX, &count); + g->sweepgc = sweeplist(L, g->sweepgc, GCSWEEPMAX); g->GCestimate += g->GCdebt - olddebt; /* update estimate */ - return count; } else { /* enter next state */ g->gcstate = nextstate; g->sweepgc = nextlist; - return 0; /* no work done */ } } -static lu_mem singlestep (lua_State *L) { +static l_mem singlestep (lua_State *L) { global_State *g = G(L); - lu_mem work; + l_mem work; lua_assert(!g->gcstopem); /* collector is not reentrant */ g->gcstopem = 1; /* no emergency collections while collecting */ switch (g->gcstate) { @@ -1599,26 +1586,30 @@ static lu_mem singlestep (lua_State *L) { g->gcstate = GCSenteratomic; /* finish propagate phase */ work = 0; } - else - work = propagatemark(g); /* traverse one gray object */ + else { + propagatemark(g); /* traverse one gray object */ + work = 1; + } break; } case GCSenteratomic: { - work = atomic(L); /* work is what was traversed by 'atomic' */ + work = atomic(L); entersweep(L); - g->GCestimate = gettotalbytes(g); /* first estimate */; break; } case GCSswpallgc: { /* sweep "regular" objects */ - work = sweepstep(L, g, GCSswpfinobj, &g->finobj); + sweepstep(L, g, GCSswpfinobj, &g->finobj); + work = GCSWEEPMAX; break; } case GCSswpfinobj: { /* sweep objects with finalizers */ - work = sweepstep(L, g, GCSswptobefnz, &g->tobefnz); + sweepstep(L, g, GCSswptobefnz, &g->tobefnz); + work = GCSWEEPMAX; break; } case GCSswptobefnz: { /* sweep objects to be finalized */ - work = sweepstep(L, g, GCSswpend, NULL); + sweepstep(L, g, GCSswpend, NULL); + work = GCSWEEPMAX; break; } case GCSswpend: { /* finish sweeps */ @@ -1630,7 +1621,8 @@ static lu_mem singlestep (lua_State *L) { case GCScallfin: { /* call remaining finalizers */ if (g->tobefnz && !g->gcemergency) { g->gcstopem = 0; /* ok collections during finalizers */ - work = runafewfinalizers(L, GCFINMAX) * GCFINALIZECOST; + runafewfinalizers(L, GCFINMAX); + work = GCFINMAX * GCFINALIZECOST; } else { /* emergency mode or no more finalizers */ g->gcstate = GCSpause; /* finish collection */ @@ -1666,18 +1658,16 @@ void luaC_runtilstate (lua_State *L, int statesmask) { */ static void incstep (lua_State *L, global_State *g) { int stepmul = (getgcparam(g->gcstepmul) | 1); /* avoid division by 0 */ - l_mem debt = (g->GCdebt / WORK2MEM) * stepmul; - l_mem stepsize = (g->gcstepsize <= log2maxs(l_mem)) - ? ((cast(l_mem, 1) << g->gcstepsize) / WORK2MEM) * stepmul - : MAX_LMEM; /* overflow; keep maximum value */ + l_mem debt = (g->GCdebt / 100) * stepmul; + l_mem stepsize = cast(l_mem, 1) << g->gcstepsize; do { /* repeat until pause or enough "credit" (negative debt) */ - lu_mem work = singlestep(L); /* perform one single step */ + l_mem work = singlestep(L); /* perform one single step */ debt -= work; } while (debt > -stepsize && g->gcstate != GCSpause); if (g->gcstate == GCSpause) setpause(g); /* pause until next cycle */ else { - debt = (debt / stepmul) * WORK2MEM; /* convert 'work units' to bytes */ + debt = (debt / stepmul) * 100; /* apply step multiplier */ luaE_setdebt(g, debt); } } @@ -1710,9 +1700,8 @@ static void fullinc (lua_State *L, global_State *g) { /* finish any pending sweep phase to start a new cycle */ luaC_runtilstate(L, bitmask(GCSpause)); luaC_runtilstate(L, bitmask(GCScallfin)); /* run up to finalizers */ - /* estimate must be correct after a full GC cycle */ - lua_assert(g->GCestimate == gettotalbytes(g)); luaC_runtilstate(L, bitmask(GCSpause)); /* finish collection */ + /* estimate must be correct after a full GC cycle */ setpause(g); } diff --git a/lgc.h b/lgc.h index c960e70647..a4c54b47e5 100644 --- a/lgc.h +++ b/lgc.h @@ -135,10 +135,10 @@ #define getgcparam(p) ((p) * 4) #define setgcparam(p,v) ((p) = (v) / 4) -#define LUAI_GCMUL 100 +#define LUAI_GCMUL 300 -/* how much to allocate before next GC step (log2) */ -#define LUAI_GCSTEPSIZE 13 /* 8 KB */ +/* how many objects to allocate before next GC step (log2) */ +#define LUAI_GCSTEPSIZE 8 /* 256 objects */ /* diff --git a/llimits.h b/llimits.h index 52a32f92e3..ef108f5f9b 100644 --- a/llimits.h +++ b/llimits.h @@ -57,7 +57,7 @@ typedef signed char ls_byte; ** floor of the log2 of the maximum signed value for integral type 't'. ** (That is, maximum 'n' such that '2^n' fits in the given signed type.) */ -#define log2maxs(t) (sizeof(t) * 8 - 2) +#define log2maxs(t) cast_int(sizeof(t) * 8 - 2) /* diff --git a/lmem.c b/lmem.c index 9029d588c1..66e2b92ba7 100644 --- a/lmem.c +++ b/lmem.c @@ -133,7 +133,7 @@ void luaM_free_ (lua_State *L, void *block, size_t osize) { global_State *g = G(L); lua_assert((osize == 0) == (block == NULL)); (*g->frealloc)(g->ud, block, osize, 0); - g->GCdebt -= osize; + g->totalbytes -= osize; } @@ -167,10 +167,10 @@ void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) { if (l_unlikely(newblock == NULL && nsize > 0)) { newblock = tryagain(L, block, osize, nsize); if (newblock == NULL) /* still no memory? */ - return NULL; /* do not update 'GCdebt' */ + return NULL; /* do not update 'totalbytes' */ } lua_assert((nsize == 0) == (newblock == NULL)); - g->GCdebt = (g->GCdebt + nsize) - osize; + g->totalbytes += nsize - osize; return newblock; } @@ -195,7 +195,7 @@ void *luaM_malloc_ (lua_State *L, size_t size, int tag) { if (newblock == NULL) luaM_error(L); } - g->GCdebt += size; + g->totalbytes += size; return newblock; } } diff --git a/lstate.c b/lstate.c index 3091a00e64..9f61534254 100644 --- a/lstate.c +++ b/lstate.c @@ -83,15 +83,15 @@ static unsigned int luai_makeseed (lua_State *L) { /* -** set GCdebt to a new value keeping the value (totalbytes + GCdebt) -** invariant (and avoiding underflows in 'totalbytes') +** set GCdebt to a new value keeping the value (totalobjs + GCdebt) +** invariant (and avoiding underflows in 'totalobjs') */ void luaE_setdebt (global_State *g, l_mem debt) { - l_mem tb = gettotalbytes(g); + l_mem tb = gettotalobjs(g); lua_assert(tb > 0); if (debt < tb - MAX_LMEM) - debt = tb - MAX_LMEM; /* will make 'totalbytes == MAX_LMEM' */ - g->totalbytes = tb - debt; + debt = tb - MAX_LMEM; /* will make 'totalobjs == MAX_LMEM' */ + g->totalobjs = tb - debt; g->GCdebt = debt; } @@ -278,8 +278,8 @@ static void close_state (lua_State *L) { } luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size); freestack(L); - lua_assert(gettotalbytes(g) == sizeof(LG)); - lua_assert(g->totalobjs == 1); + lua_assert(g->totalbytes == sizeof(LG)); + lua_assert(gettotalobjs(g) == 1); (*g->frealloc)(g->ud, fromstate(L), sizeof(LG), 0); /* free main block */ } @@ -389,6 +389,7 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { g->twups = NULL; g->totalbytes = sizeof(LG); g->totalobjs = 1; + g->marked = 0; g->GCdebt = 0; g->lastatomic = 0; setivalue(&g->nilvalue, 0); /* to signal that state is not yet built */ diff --git a/lstate.h b/lstate.h index 62ad61c6b8..3ffd09b760 100644 --- a/lstate.h +++ b/lstate.h @@ -249,9 +249,10 @@ typedef struct CallInfo { typedef struct global_State { lua_Alloc frealloc; /* function to reallocate memory */ void *ud; /* auxiliary data to 'frealloc' */ - l_mem totalbytes; /* number of bytes currently allocated - GCdebt */ - l_mem totalobjs; /* total number of objects allocated */ + l_mem totalbytes; /* number of bytes currently allocated */ + l_mem totalobjs; /* total number of objects allocated - GCdebt */ l_mem GCdebt; /* bytes allocated not yet compensated by the collector */ + lu_mem marked; /* number of objects marked in a GC cycle */ lu_mem GCestimate; /* an estimate of the non-garbage memory in use */ lu_mem lastatomic; /* see function 'genstep' in file 'lgc.c' */ stringtable strt; /* hash table for strings */ @@ -386,8 +387,9 @@ union GCUnion { #define obj2gco(v) check_exp((v)->tt >= LUA_TSTRING, &(cast_u(v)->gc)) -/* actual number of total bytes allocated */ -#define gettotalbytes(g) cast(lu_mem, (g)->totalbytes + (g)->GCdebt) +/* actual number of total objects allocated */ +#define gettotalobjs(g) ((g)->totalobjs + (g)->GCdebt) + LUAI_FUNC void luaE_setdebt (global_State *g, l_mem debt); LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1); diff --git a/ltests.c b/ltests.c index 1caed04c9b..57530884c6 100644 --- a/ltests.c +++ b/ltests.c @@ -1027,6 +1027,16 @@ static int table_query (lua_State *L) { } +static int query_inc (lua_State *L) { + global_State *g = G(L); + lua_pushinteger(L, gettotalobjs(g)); + lua_pushinteger(L, g->GCdebt); + lua_pushinteger(L, getgcparam(g->gcpause)); + lua_pushinteger(L, getgcparam(g->gcstepmul)); + lua_pushinteger(L, cast(l_mem, 1) << g->gcstepsize); + return 5; +} + static int string_query (lua_State *L) { stringtable *tb = &G(L)->strt; int s = cast_int(luaL_optinteger(L, 1, 0)) - 1; @@ -1933,6 +1943,7 @@ static const struct luaL_Reg tests_funcs[] = { {"pushuserdata", pushuserdata}, {"querystr", string_query}, {"querytab", table_query}, + {"queryinc", query_inc}, {"ref", tref}, {"resume", coresume}, {"s2d", s2d}, diff --git a/lua.c b/lua.c index 7f7dc2b22a..715430a0de 100644 --- a/lua.c +++ b/lua.c @@ -633,7 +633,8 @@ static int pmain (lua_State *L) { } luaL_openlibs(L); /* open standard libraries */ createargtable(L, argv, argc, script); /* create table 'arg' */ - lua_gc(L, LUA_GCGEN, 0, 0); /* GC in generational mode */ + lua_gc(L, LUA_GCRESTART); /* start GC... */ + lua_gc(L, LUA_GCGEN, 0, 0); /* ...in generational mode */ if (!(args & has_E)) { /* no option '-E'? */ if (handle_luainit(L) != LUA_OK) /* run LUA_INIT */ return 0; /* error running LUA_INIT */ @@ -665,6 +666,7 @@ int main (int argc, char **argv) { l_message(argv[0], "cannot create state: not enough memory"); return EXIT_FAILURE; } + lua_gc(L, LUA_GCSTOP); /* stop GC while buidling state */ lua_pushcfunction(L, &pmain); /* to call 'pmain' in protected mode */ lua_pushinteger(L, argc); /* 1st argument */ lua_pushlightuserdata(L, argv); /* 2nd argument */ From ec61be9a7e828bfa366a35658b90f53b1ce39478 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 23 Nov 2022 17:29:03 -0300 Subject: [PATCH 0782/1145] 'l_mem' renamed to 'l_obj' to count objects --- lapi.c | 6 +++--- lgc.c | 50 +++++++++++++++++++++++++------------------------- llimits.h | 14 ++++++++------ lstate.c | 4 ++-- lstate.h | 10 +++++----- ltests.c | 16 ++++++++-------- 6 files changed, 51 insertions(+), 49 deletions(-) diff --git a/lapi.c b/lapi.c index 3c620d4b52..3876956d96 100644 --- a/lapi.c +++ b/lapi.c @@ -1163,7 +1163,7 @@ LUA_API int lua_gc (lua_State *L, int what, ...) { } case LUA_GCSTEP: { int data = va_arg(argp, int); - l_mem debt = 1; /* =1 to signal that it did an actual step */ + l_obj debt = 1; /* =1 to signal that it did an actual step */ lu_byte oldstp = g->gcstp; g->gcstp = 0; /* allow GC to run (GCSTPGC must be zero here) */ if (data == 0) { @@ -1217,8 +1217,8 @@ LUA_API int lua_gc (lua_State *L, int what, ...) { if (stepmul != 0) setgcparam(g->gcstepmul, stepmul); if (stepsize != 0) - g->gcstepsize = (stepsize <= log2maxs(l_mem)) ? stepsize - : log2maxs(l_mem); + g->gcstepsize = (stepsize <= log2maxs(l_obj)) ? stepsize + : log2maxs(l_obj); luaC_changemode(L, KGC_INC); break; } diff --git a/lgc.c b/lgc.c index 9c5cc2e3d5..0e4e5552e4 100644 --- a/lgc.c +++ b/lgc.c @@ -93,7 +93,7 @@ #define markobjectN(g,t) { if (t) markobject(g,t); } static void reallymarkobject (global_State *g, GCObject *o); -static l_mem atomic (lua_State *L); +static l_obj atomic (lua_State *L); static void entersweep (lua_State *L); @@ -335,9 +335,9 @@ static void markmt (global_State *g) { /* ** mark all objects in list of being-finalized */ -static l_mem markbeingfnz (global_State *g) { +static l_obj markbeingfnz (global_State *g) { GCObject *o; - l_mem count = 0; + l_obj count = 0; for (o = g->tobefnz; o != NULL; o = o->next) { count++; markobject(g, o); @@ -357,8 +357,8 @@ static l_mem markbeingfnz (global_State *g) { ** upvalues, as they have nothing to be checked. (If the thread gets an ** upvalue later, it will be linked in the list again.) */ -static l_mem remarkupvals (global_State *g) { - l_mem work = 0; +static l_obj remarkupvals (global_State *g) { + l_obj work = 0; lua_State *thread; lua_State **p = &g->twups; while ((thread = *p) != NULL) { @@ -662,8 +662,8 @@ static void propagatemark (global_State *g) { } -static l_mem propagateall (global_State *g) { - l_mem work = 0; +static l_obj propagateall (global_State *g) { + l_obj work = 0; while (g->gray) { propagatemark(g); work++; @@ -678,9 +678,9 @@ static l_mem propagateall (global_State *g) { ** inverts the direction of the traversals, trying to speed up ** convergence on chains in the same table. */ -static l_mem convergeephemerons (global_State *g) { +static l_obj convergeephemerons (global_State *g) { int changed; - l_mem work = 0; + l_obj work = 0; int dir = 0; do { GCObject *w; @@ -715,8 +715,8 @@ static l_mem convergeephemerons (global_State *g) { /* ** clear entries with unmarked keys from all weaktables in list 'l' */ -static l_mem clearbykeys (global_State *g, GCObject *l) { - l_mem work = 0; +static l_obj clearbykeys (global_State *g, GCObject *l) { + l_obj work = 0; for (; l; l = gco2t(l)->gclist) { Table *h = gco2t(l); Node *limit = gnodelast(h); @@ -737,8 +737,8 @@ static l_mem clearbykeys (global_State *g, GCObject *l) { ** clear entries with unmarked values from all weaktables in list 'l' up ** to element 'f' */ -static l_mem clearbyvalues (global_State *g, GCObject *l, GCObject *f) { - l_mem work = 0; +static l_obj clearbyvalues (global_State *g, GCObject *l, GCObject *f) { + l_obj work = 0; for (; l != f; l = gco2t(l)->gclist) { Table *h = gco2t(l); Node *n, *limit = gnodelast(h); @@ -1054,7 +1054,7 @@ void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt) { static void setpause (global_State *g) { unsigned int pause = getgcparam(g->gcpause); lu_mem threshold = g->marked / 8 * pause / 12; - l_mem debt = gettotalobjs(g) - threshold; + l_obj debt = gettotalobjs(g) - threshold; if (debt > 0) debt = 0; luaE_setdebt(g, debt); } @@ -1306,7 +1306,7 @@ static void atomic2gen (lua_State *L, global_State *g) { ** total number of objects grows 'genminormul'%. */ static void setminordebt (global_State *g) { - luaE_setdebt(g, -(cast(l_mem, (gettotalobjs(g) / 100)) * g->genminormul)); + luaE_setdebt(g, -(cast(l_obj, (gettotalobjs(g) / 100)) * g->genminormul)); } @@ -1431,8 +1431,8 @@ static void genstep (lua_State *L, global_State *g) { if (g->lastatomic != 0) /* last collection was a bad one? */ stepgenfull(L, g); /* do a full step */ else { - l_mem majorbase = g->GCestimate; /* objects after last major collection */ - l_mem majorinc = (majorbase / 100) * getgcparam(g->genmajormul); + l_obj majorbase = g->GCestimate; /* objects after last major collection */ + l_obj majorinc = (majorbase / 100) * getgcparam(g->genmajormul); if (g->GCdebt > 0 && gettotalobjs(g) > majorbase + majorinc) { g->marked = 0; fullgen(L, g); /* do a major collection */ @@ -1512,8 +1512,8 @@ void luaC_freeallobjects (lua_State *L) { } -static l_mem atomic (lua_State *L) { - l_mem work = 0; +static l_obj atomic (lua_State *L) { + l_obj work = 0; global_State *g = G(L); GCObject *origweak, *origall; GCObject *grayagain = g->grayagain; /* save original list */ @@ -1558,7 +1558,7 @@ static l_mem atomic (lua_State *L) { static void sweepstep (lua_State *L, global_State *g, int nextstate, GCObject **nextlist) { if (g->sweepgc) { - l_mem olddebt = g->GCdebt; + l_obj olddebt = g->GCdebt; g->sweepgc = sweeplist(L, g->sweepgc, GCSWEEPMAX); g->GCestimate += g->GCdebt - olddebt; /* update estimate */ } @@ -1569,9 +1569,9 @@ static void sweepstep (lua_State *L, global_State *g, } -static l_mem singlestep (lua_State *L) { +static l_obj singlestep (lua_State *L) { global_State *g = G(L); - l_mem work; + l_obj work; lua_assert(!g->gcstopem); /* collector is not reentrant */ g->gcstopem = 1; /* no emergency collections while collecting */ switch (g->gcstate) { @@ -1658,10 +1658,10 @@ void luaC_runtilstate (lua_State *L, int statesmask) { */ static void incstep (lua_State *L, global_State *g) { int stepmul = (getgcparam(g->gcstepmul) | 1); /* avoid division by 0 */ - l_mem debt = (g->GCdebt / 100) * stepmul; - l_mem stepsize = cast(l_mem, 1) << g->gcstepsize; + l_obj debt = (g->GCdebt / 100) * stepmul; + l_obj stepsize = cast(l_obj, 1) << g->gcstepsize; do { /* repeat until pause or enough "credit" (negative debt) */ - l_mem work = singlestep(L); /* perform one single step */ + l_obj work = singlestep(L); /* perform one single step */ debt -= work; } while (debt > -stepsize && g->gcstate != GCSpause); if (g->gcstate == GCSpause) diff --git a/llimits.h b/llimits.h index ef108f5f9b..525b36475f 100644 --- a/llimits.h +++ b/llimits.h @@ -16,19 +16,21 @@ /* -** 'lu_mem' and 'l_mem' are unsigned/signed integers big enough to count -** the total memory used by Lua (in bytes). Usually, 'size_t' and +** 'lu_mem' is an unsigned integer big enough to count the total memory +** used by Lua (in bytes). 'l_obj' is a signed integer big enough to +** count the total number of objects used by Lua. (It is negative due +** to the use of debt in several computations.) Usually, 'size_t' and ** 'ptrdiff_t' should work, but we use 'long' for 16-bit machines. */ #if defined(LUAI_MEM) /* { external definitions? */ typedef LUAI_UMEM lu_mem; -typedef LUAI_MEM l_mem; +typedef LUAI_MEM l_obj; #elif LUAI_IS32INT /* }{ */ typedef size_t lu_mem; -typedef ptrdiff_t l_mem; +typedef ptrdiff_t l_obj; #else /* 16-bit ints */ /* }{ */ typedef unsigned long lu_mem; -typedef long l_mem; +typedef long l_obj; #endif /* } */ @@ -47,7 +49,7 @@ typedef signed char ls_byte; #define MAX_LUMEM ((lu_mem)(~(lu_mem)0)) -#define MAX_LMEM ((l_mem)(MAX_LUMEM >> 1)) +#define MAX_LMEM ((l_obj)(MAX_LUMEM >> 1)) #define MAX_INT INT_MAX /* maximum value of an int */ diff --git a/lstate.c b/lstate.c index 9f61534254..01393c41dd 100644 --- a/lstate.c +++ b/lstate.c @@ -86,8 +86,8 @@ static unsigned int luai_makeseed (lua_State *L) { ** set GCdebt to a new value keeping the value (totalobjs + GCdebt) ** invariant (and avoiding underflows in 'totalobjs') */ -void luaE_setdebt (global_State *g, l_mem debt) { - l_mem tb = gettotalobjs(g); +void luaE_setdebt (global_State *g, l_obj debt) { + l_obj tb = gettotalobjs(g); lua_assert(tb > 0); if (debt < tb - MAX_LMEM) debt = tb - MAX_LMEM; /* will make 'totalobjs == MAX_LMEM' */ diff --git a/lstate.h b/lstate.h index 3ffd09b760..240550c23d 100644 --- a/lstate.h +++ b/lstate.h @@ -249,10 +249,10 @@ typedef struct CallInfo { typedef struct global_State { lua_Alloc frealloc; /* function to reallocate memory */ void *ud; /* auxiliary data to 'frealloc' */ - l_mem totalbytes; /* number of bytes currently allocated */ - l_mem totalobjs; /* total number of objects allocated - GCdebt */ - l_mem GCdebt; /* bytes allocated not yet compensated by the collector */ - lu_mem marked; /* number of objects marked in a GC cycle */ + lu_mem totalbytes; /* number of bytes currently allocated */ + l_obj totalobjs; /* total number of objects allocated - GCdebt */ + l_obj GCdebt; /* bytes allocated not yet compensated by the collector */ + l_obj marked; /* number of objects marked in a GC cycle */ lu_mem GCestimate; /* an estimate of the non-garbage memory in use */ lu_mem lastatomic; /* see function 'genstep' in file 'lgc.c' */ stringtable strt; /* hash table for strings */ @@ -391,7 +391,7 @@ union GCUnion { #define gettotalobjs(g) ((g)->totalobjs + (g)->GCdebt) -LUAI_FUNC void luaE_setdebt (global_State *g, l_mem debt); +LUAI_FUNC void luaE_setdebt (global_State *g, l_obj debt); LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1); LUAI_FUNC CallInfo *luaE_extendCI (lua_State *L); LUAI_FUNC void luaE_freeCI (lua_State *L); diff --git a/ltests.c b/ltests.c index 57530884c6..cc4bf58f1e 100644 --- a/ltests.c +++ b/ltests.c @@ -531,7 +531,7 @@ static void checkobject (global_State *g, GCObject *o, int maybedead, } -static lu_mem checkgraylist (global_State *g, GCObject *o) { +static l_obj checkgraylist (global_State *g, GCObject *o) { int total = 0; /* count number of elements in the list */ cast_void(g); /* better to keep it if we need to print an object */ while (o) { @@ -560,7 +560,7 @@ static lu_mem checkgraylist (global_State *g, GCObject *o) { /* ** Check objects in gray lists. */ -static lu_mem checkgrays (global_State *g) { +static l_obj checkgrays (global_State *g) { int total = 0; /* count number of elements in all lists */ if (!keepinvariant(g)) return total; total += checkgraylist(g, g->gray); @@ -577,7 +577,7 @@ static lu_mem checkgrays (global_State *g) { ** 'count' and check its TESTBIT. (It must have been previously set by ** 'checkgraylist'.) */ -static void incifingray (global_State *g, GCObject *o, lu_mem *count) { +static void incifingray (global_State *g, GCObject *o, l_obj *count) { if (!keepinvariant(g)) return; /* gray lists not being kept in these phases */ if (o->tt == LUA_VUPVAL) { @@ -594,10 +594,10 @@ static void incifingray (global_State *g, GCObject *o, lu_mem *count) { } -static lu_mem checklist (global_State *g, int maybedead, int tof, +static l_obj checklist (global_State *g, int maybedead, int tof, GCObject *newl, GCObject *survival, GCObject *old, GCObject *reallyold) { GCObject *o; - lu_mem total = 0; /* number of object that should be in gray lists */ + l_obj total = 0; /* number of object that should be in gray lists */ for (o = newl; o != survival; o = o->next) { checkobject(g, o, maybedead, G_NEW); incifingray(g, o, &total); @@ -626,8 +626,8 @@ int lua_checkmemory (lua_State *L) { global_State *g = G(L); GCObject *o; int maybedead; - lu_mem totalin; /* total of objects that are in gray lists */ - lu_mem totalshould; /* total of objects that should be in gray lists */ + l_obj totalin; /* total of objects that are in gray lists */ + l_obj totalshould; /* total of objects that should be in gray lists */ if (keepinvariant(g)) { assert(!iswhite(g->mainthread)); assert(!iswhite(gcvalue(&g->l_registry))); @@ -1033,7 +1033,7 @@ static int query_inc (lua_State *L) { lua_pushinteger(L, g->GCdebt); lua_pushinteger(L, getgcparam(g->gcpause)); lua_pushinteger(L, getgcparam(g->gcstepmul)); - lua_pushinteger(L, cast(l_mem, 1) << g->gcstepsize); + lua_pushinteger(L, cast(l_obj, 1) << g->gcstepsize); return 5; } From 152b51955aabb9dfb32302569fac810e999eda03 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 24 Nov 2022 10:20:15 -0300 Subject: [PATCH 0783/1145] Removed GC checks from function calls Function calls do not create new objects. (It may use memory with stack reallocation, but now that is irrelevant to the GC.) --- lapi.c | 5 +++-- ldo.c | 8 ++++---- ldo.h | 12 ------------ lgc.c | 3 ++- ltm.c | 2 +- 5 files changed, 10 insertions(+), 20 deletions(-) diff --git a/lapi.c b/lapi.c index 3876956d96..00bdd37a36 100644 --- a/lapi.c +++ b/lapi.c @@ -1286,13 +1286,14 @@ LUA_API void lua_toclose (lua_State *L, int idx) { LUA_API void lua_concat (lua_State *L, int n) { lua_lock(L); api_checknelems(L, n); - if (n > 0) + if (n > 0) { luaV_concat(L, n); + luaC_checkGC(L); + } else { /* nothing to concatenate */ setsvalue2s(L, L->top.p, luaS_newlstr(L, "", 0)); /* push empty string */ api_incr_top(L); } - luaC_checkGC(L); lua_unlock(L); } diff --git a/ldo.c b/ldo.c index c30cde76f5..54518affb5 100644 --- a/ldo.c +++ b/ldo.c @@ -416,7 +416,7 @@ static void rethook (lua_State *L, CallInfo *ci, int nres) { StkId luaD_tryfuncTM (lua_State *L, StkId func) { const TValue *tm; StkId p; - checkstackGCp(L, 1, func); /* space for metamethod */ + checkstackp(L, 1, func); /* space for metamethod */ tm = luaT_gettmbyobj(L, s2v(func), TM_CALL); /* (after previous GC) */ if (l_unlikely(ttisnil(tm))) luaG_callerror(L, s2v(func)); /* nothing to call */ @@ -521,7 +521,7 @@ l_sinline int precallC (lua_State *L, StkId func, int nresults, lua_CFunction f) { int n; /* number of returns */ CallInfo *ci; - checkstackGCp(L, LUA_MINSTACK, func); /* ensure minimum stack size */ + checkstackp(L, LUA_MINSTACK, func); /* ensure minimum stack size */ L->ci = ci = prepCallInfo(L, func, nresults, CIST_C, L->top.p + LUA_MINSTACK); lua_assert(ci->top.p <= L->stack_last.p); @@ -557,7 +557,7 @@ int luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, int fsize = p->maxstacksize; /* frame size */ int nfixparams = p->numparams; int i; - checkstackGCp(L, fsize - delta, func); + checkstackp(L, fsize - delta, func); ci->func.p -= delta; /* restore 'func' (if vararg) */ for (i = 0; i < narg1; i++) /* move down function and arguments */ setobjs2s(L, ci->func.p + i, func + i); @@ -604,7 +604,7 @@ CallInfo *luaD_precall (lua_State *L, StkId func, int nresults) { int narg = cast_int(L->top.p - func) - 1; /* number of real arguments */ int nfixparams = p->numparams; int fsize = p->maxstacksize; /* frame size */ - checkstackGCp(L, fsize, func); + checkstackp(L, fsize, func); L->ci = ci = prepCallInfo(L, func, nresults, 0, func + 1 + fsize); ci->u.l.savedpc = p->code; /* starting point */ for (; narg < nfixparams; narg++) diff --git a/ldo.h b/ldo.h index 1aa446ad09..b050fc08b5 100644 --- a/ldo.h +++ b/ldo.h @@ -44,18 +44,6 @@ p = restorestack(L, t__)) /* 'pos' part: restore 'p' */ -/* macro to check stack size and GC, preserving 'p' */ -#define checkstackGCp(L,n,p) \ - luaD_checkstackaux(L, n, \ - ptrdiff_t t__ = savestack(L, p); /* save 'p' */ \ - luaC_checkGC(L), /* stack grow uses memory */ \ - p = restorestack(L, t__)) /* 'pos' part: restore 'p' */ - - -/* macro to check stack size and GC */ -#define checkstackGC(L,fsize) \ - luaD_checkstackaux(L, (fsize), luaC_checkGC(L), (void)0) - /* type of protected functions, to be ran by 'runprotected' */ typedef void (*Pfunc) (lua_State *L, void *ud); diff --git a/lgc.c b/lgc.c index 0e4e5552e4..aa95c028fa 100644 --- a/lgc.c +++ b/lgc.c @@ -1700,8 +1700,9 @@ static void fullinc (lua_State *L, global_State *g) { /* finish any pending sweep phase to start a new cycle */ luaC_runtilstate(L, bitmask(GCSpause)); luaC_runtilstate(L, bitmask(GCScallfin)); /* run up to finalizers */ - luaC_runtilstate(L, bitmask(GCSpause)); /* finish collection */ /* estimate must be correct after a full GC cycle */ + lua_assert(g->marked == gettotalobjs(g)); + luaC_runtilstate(L, bitmask(GCSpause)); /* finish collection */ setpause(g); } diff --git a/ltm.c b/ltm.c index 07a060811d..8e0d22223e 100644 --- a/ltm.c +++ b/ltm.c @@ -260,7 +260,7 @@ void luaT_getvarargs (lua_State *L, CallInfo *ci, StkId where, int wanted) { int nextra = ci->u.l.nextraargs; if (wanted < 0) { wanted = nextra; /* get all extra arguments available */ - checkstackGCp(L, nextra, where); /* ensure stack space */ + checkstackp(L, nextra, where); /* ensure stack space */ L->top.p = where + nextra; /* next instruction will need top */ } for (i = 0; i < wanted && i < nextra; i++) From d324a0ccf9e2511baf182dd981a8ee9835cee925 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 29 Nov 2022 10:37:08 -0300 Subject: [PATCH 0784/1145] Simpler control for major collections --- lapi.c | 4 +- lgc.c | 190 ++++++++++++++++++++----------------------------------- lgc.h | 8 --- lstate.c | 1 - lstate.h | 8 +-- ltests.c | 2 +- 6 files changed, 77 insertions(+), 136 deletions(-) diff --git a/lapi.c b/lapi.c index 00bdd37a36..0cde72ccfe 100644 --- a/lapi.c +++ b/lapi.c @@ -1199,7 +1199,7 @@ LUA_API int lua_gc (lua_State *L, int what, ...) { case LUA_GCGEN: { int minormul = va_arg(argp, int); int majormul = va_arg(argp, int); - res = isdecGCmodegen(g) ? LUA_GCGEN : LUA_GCINC; + res = (g->gckind == KGC_INC) ? LUA_GCINC : LUA_GCGEN; if (minormul != 0) g->genminormul = minormul; if (majormul != 0) @@ -1211,7 +1211,7 @@ LUA_API int lua_gc (lua_State *L, int what, ...) { int pause = va_arg(argp, int); int stepmul = va_arg(argp, int); int stepsize = va_arg(argp, int); - res = isdecGCmodegen(g) ? LUA_GCGEN : LUA_GCINC; + res = (g->gckind == KGC_INC) ? LUA_GCINC : LUA_GCGEN; if (pause != 0) setgcparam(g->gcpause, pause); if (stepmul != 0) diff --git a/lgc.c b/lgc.c index aa95c028fa..1b24fda6eb 100644 --- a/lgc.c +++ b/lgc.c @@ -29,23 +29,18 @@ /* -** Maximum number of elements to sweep in each single step. -** (Large enough to dissipate fixed overheads but small enough -** to allow small steps for the collector.) +** Number of fixed (luaC_fix) objects in a Lua state: metafield names, +** plus reserved words, plus "_ENV", plus the memory-error message. */ -#define GCSWEEPMAX 20 - -/* -** Maximum number of finalizers to call in each single step. -*/ -#define GCFINMAX 10 +#define NFIXED (TM_N + NUM_RESERVED + 2) /* -** Cost of calling one finalizer. +** Maximum number of elements to sweep in each single step. +** (Large enough to dissipate fixed overheads but small enough +** to allow small steps for the collector.) */ -#define GCFINALIZECOST 50 - +#define GCSWEEPMAX 20 /* mask with all color bits */ @@ -205,7 +200,7 @@ void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v) { } else { /* sweep phase */ lua_assert(issweepphase(g)); - if (g->gckind == KGC_INC) /* incremental mode? */ + if (g->gckind != KGC_GEN) /* incremental mode? */ makewhite(g, o); /* mark 'o' as white to avoid other barriers */ } } @@ -398,7 +393,7 @@ static void cleargraylists (global_State *g) { */ static void restartcollection (global_State *g) { cleargraylists(g); - g->marked = TM_N + NUM_RESERVED + 2; + g->marked = NFIXED; markobject(g, g->mainthread); markvalue(g, &g->l_registry); markmt(g); @@ -926,17 +921,6 @@ static void GCTM (lua_State *L) { } -/* -** Call a few finalizers -*/ -static void runafewfinalizers (lua_State *L, int n) { - global_State *g = G(L); - int i; - for (i = 0; i < n && g->tobefnz; i++) - GCTM(L); /* call one finalizer */ -} - - /* ** call all pending finalizers */ @@ -1295,8 +1279,7 @@ static void atomic2gen (lua_State *L, global_State *g) { sweep2old(L, &g->tobefnz); g->gckind = KGC_GEN; - g->lastatomic = 0; - g->GCestimate = gettotalobjs(g); /* base for memory control */ + g->GClastmajor = gettotalobjs(g); /* base for memory control */ finishgencycle(L, g); } @@ -1306,7 +1289,7 @@ static void atomic2gen (lua_State *L, global_State *g) { ** total number of objects grows 'genminormul'%. */ static void setminordebt (global_State *g) { - luaE_setdebt(g, -(cast(l_obj, (gettotalobjs(g) / 100)) * g->genminormul)); + luaE_setdebt(g, -(gettotalobjs(g) / 100) * g->genminormul); } @@ -1338,7 +1321,6 @@ static void enterinc (global_State *g) { g->finobjrold = g->finobjold1 = g->finobjsur = NULL; g->gcstate = GCSpause; g->gckind = KGC_INC; - g->lastatomic = 0; } @@ -1347,13 +1329,18 @@ static void enterinc (global_State *g) { */ void luaC_changemode (lua_State *L, int newmode) { global_State *g = G(L); - if (newmode != g->gckind) { - if (newmode == KGC_GEN) /* entering generational mode? */ + if (newmode != g->gckind) { /* does it need to change? */ + if (newmode == KGC_INC) { /* entering incremental mode? */ + if (g->gckind == KGC_GENMAJOR) + g->gckind = KGC_INC; /* already incremental but in name */ + else + enterinc(g); /* entering incremental mode */ + } + else { + lua_assert(newmode == KGC_GEN); entergen(L, g); - else - enterinc(g); /* entering incremental mode */ + } } - g->lastatomic = 0; } @@ -1367,93 +1354,52 @@ static void fullgen (lua_State *L, global_State *g) { /* -** Does a major collection after last collection was a "bad collection". -** -** When the program is building a big structure, it allocates lots of -** memory but generates very little garbage. In those scenarios, -** the generational mode just wastes time doing small collections, and -** major collections are frequently what we call a "bad collection", a -** collection that frees too few objects. To avoid the cost of switching -** between generational mode and the incremental mode needed for full -** (major) collections, the collector tries to stay in incremental mode -** after a bad collection, and to switch back to generational mode only -** after a "good" collection (one that traverses less than 9/8 objects -** of the previous one). -** The collector must choose whether to stay in incremental mode or to -** switch back to generational mode before sweeping. At this point, it -** does not know the real memory in use, so it cannot use memory to -** decide whether to return to generational mode. Instead, it uses the -** number of objects traversed (returned by 'atomic') as a proxy. The -** field 'g->lastatomic' keeps this count from the last collection. -** ('g->lastatomic != 0' also means that the last collection was bad.) -*/ -static void stepgenfull (lua_State *L, global_State *g) { - lu_mem lastatomic = g->lastatomic; /* count from last collection */ - if (g->gckind == KGC_GEN) /* still in generational mode? */ - enterinc(g); /* enter incremental mode */ +** Does a major collector up to the atomic phase and then either +** returns to minor collections or stays doing major ones. If the +** number of objects collected this time (numobjs - marked) is more than +** half the number of objects created since the last major collection +** (numobjs - lastmajor), it goes back to minor collections. +*/ +static void genmajorstep (lua_State *L, global_State *g) { + l_obj lastmajor = g->GClastmajor; /* count from last collection */ + l_obj numobjs = gettotalobjs(g); /* current count */ luaC_runtilstate(L, bitmask(GCSpropagate)); /* start new cycle */ - g->marked = 0; atomic(L); /* mark everybody */ - if (g->marked < lastatomic + (lastatomic >> 3)) { /* good collection? */ + if ((numobjs - g->marked) > ((numobjs - lastmajor) >> 1)) { atomic2gen(L, g); /* return to generational mode */ setminordebt(g); } - else { /* another bad collection; stay in incremental mode */ - g->GCestimate = gettotalobjs(g); /* first estimate */; - g->lastatomic = g->marked; + else { /* bad collection; stay in major mode */ entersweep(L); luaC_runtilstate(L, bitmask(GCSpause)); /* finish collection */ setpause(g); + g->GClastmajor = gettotalobjs(g); } } /* -** Does a generational "step". -** Usually, this means doing a minor collection and setting the debt to -** make another collection when memory grows 'genminormul'% larger. -** -** However, there are exceptions. If memory grows 'genmajormul'% -** larger than it was at the end of the last major collection (kept -** in 'g->GCestimate'), the function does a major collection. At the -** end, it checks whether the major collection was able to free a -** decent amount of memory (at least half the growth in memory since -** previous major collection). If so, the collector keeps its state, -** and the next collection will probably be minor again. Otherwise, -** we have what we call a "bad collection". In that case, set the field -** 'g->lastatomic' to signal that fact, so that the next collection will -** go to 'stepgenfull'. -** -** 'GCdebt <= 0' means an explicit call to GC step with "size" zero; -** in that case, do a minor collection. +** Does a generational "step". If the total number of objects grew +** more than 'majormul'% since the last major collection, does a +** major collection. Otherwise, does a minor collection. The test +** ('GCdebt' > 0) avoids major collections when the step originated from +** 'collectgarbage("step")'. */ static void genstep (lua_State *L, global_State *g) { - if (g->lastatomic != 0) /* last collection was a bad one? */ - stepgenfull(L, g); /* do a full step */ - else { - l_obj majorbase = g->GCestimate; /* objects after last major collection */ - l_obj majorinc = (majorbase / 100) * getgcparam(g->genmajormul); - if (g->GCdebt > 0 && gettotalobjs(g) > majorbase + majorinc) { - g->marked = 0; - fullgen(L, g); /* do a major collection */ - if (gettotalobjs(g) < majorbase + (majorinc / 2)) { - /* collected at least half of object growth since last major - collection; keep doing minor collections. */ - lua_assert(g->lastatomic == 0); - } - else { /* bad collection */ - g->lastatomic = g->marked; /* signal that last collection was bad */ - setpause(g); /* do a long wait for next (major) collection */ - } - } - else { /* regular case; do a minor collection */ - g->marked = 0; - youngcollection(L, g); - setminordebt(g); - g->GCestimate = majorbase; /* preserve base value */ - } + l_obj majorbase = g->GClastmajor; /* count after last major collection */ + l_obj majorinc = (majorbase / 100) * getgcparam(g->genmajormul); + if (g->GCdebt > 0 && gettotalobjs(g) > majorbase + majorinc) { + /* do a major collection */ + enterinc(g); + g->gckind = KGC_GENMAJOR; + genmajorstep(L, g); + } + else { /* regular case; do a minor collection */ + g->marked = 0; + youngcollection(L, g); + setminordebt(g); + lua_assert(g->GClastmajor == majorbase); } - lua_assert(isdecGCmodegen(g)); } /* }====================================================== */ @@ -1557,11 +1503,8 @@ static l_obj atomic (lua_State *L) { static void sweepstep (lua_State *L, global_State *g, int nextstate, GCObject **nextlist) { - if (g->sweepgc) { - l_obj olddebt = g->GCdebt; + if (g->sweepgc) g->sweepgc = sweeplist(L, g->sweepgc, GCSWEEPMAX); - g->GCestimate += g->GCdebt - olddebt; /* update estimate */ - } else { /* enter next state */ g->gcstate = nextstate; g->sweepgc = nextlist; @@ -1618,11 +1561,11 @@ static l_obj singlestep (lua_State *L) { work = 0; break; } - case GCScallfin: { /* call remaining finalizers */ + case GCScallfin: { /* call finalizers */ if (g->tobefnz && !g->gcemergency) { g->gcstopem = 0; /* ok collections during finalizers */ - runafewfinalizers(L, GCFINMAX); - work = GCFINMAX * GCFINALIZECOST; + GCTM(L); /* call one finalizer */ + work = 1; } else { /* emergency mode or no more finalizers */ g->gcstate = GCSpause; /* finish collection */ @@ -1679,10 +1622,17 @@ void luaC_step (lua_State *L) { global_State *g = G(L); lua_assert(!g->gcemergency); if (gcrunning(g)) { /* running? */ - if(isdecGCmodegen(g)) - genstep(L, g); - else - incstep(L, g); + switch (g->gckind) { + case KGC_INC: + incstep(L, g); + break; + case KGC_GEN: + genstep(L, g); + break; + case KGC_GENMAJOR: + genmajorstep(L, g); + break; + } } } @@ -1700,7 +1650,7 @@ static void fullinc (lua_State *L, global_State *g) { /* finish any pending sweep phase to start a new cycle */ luaC_runtilstate(L, bitmask(GCSpause)); luaC_runtilstate(L, bitmask(GCScallfin)); /* run up to finalizers */ - /* estimate must be correct after a full GC cycle */ + /* 'marked' must be correct after a full GC cycle */ lua_assert(g->marked == gettotalobjs(g)); luaC_runtilstate(L, bitmask(GCSpause)); /* finish collection */ setpause(g); @@ -1716,10 +1666,10 @@ void luaC_fullgc (lua_State *L, int isemergency) { global_State *g = G(L); lua_assert(!g->gcemergency); g->gcemergency = isemergency; /* set flag */ - if (g->gckind == KGC_INC) - fullinc(L, g); - else + if (g->gckind == KGC_GEN) fullgen(L, g); + else + fullinc(L, g); g->gcemergency = 0; } diff --git a/lgc.h b/lgc.h index a4c54b47e5..3c8719695b 100644 --- a/lgc.h +++ b/lgc.h @@ -141,14 +141,6 @@ #define LUAI_GCSTEPSIZE 8 /* 256 objects */ -/* -** Check whether the declared GC mode is generational. While in -** generational mode, the collector can go temporarily to incremental -** mode to improve performance. This is signaled by 'g->lastatomic != 0'. -*/ -#define isdecGCmodegen(g) (g->gckind == KGC_GEN || g->lastatomic != 0) - - /* ** Control when GC is running: */ diff --git a/lstate.c b/lstate.c index 01393c41dd..a4379f935c 100644 --- a/lstate.c +++ b/lstate.c @@ -391,7 +391,6 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { g->totalobjs = 1; g->marked = 0; g->GCdebt = 0; - g->lastatomic = 0; setivalue(&g->nilvalue, 0); /* to signal that state is not yet built */ setgcparam(g->gcpause, LUAI_GCPAUSE); setgcparam(g->gcstepmul, LUAI_GCMUL); diff --git a/lstate.h b/lstate.h index 240550c23d..c290ff328b 100644 --- a/lstate.h +++ b/lstate.h @@ -145,12 +145,13 @@ struct lua_longjmp; /* defined in ldo.c */ /* kinds of Garbage Collection */ #define KGC_INC 0 /* incremental gc */ #define KGC_GEN 1 /* generational gc */ +#define KGC_GENMAJOR 2 /* generational in "major" mode */ typedef struct stringtable { - TString **hash; + TString **hash; /* array of buckets (linked lists of strings) */ int nuse; /* number of elements */ - int size; + int size; /* number of buckets */ } stringtable; @@ -253,8 +254,7 @@ typedef struct global_State { l_obj totalobjs; /* total number of objects allocated - GCdebt */ l_obj GCdebt; /* bytes allocated not yet compensated by the collector */ l_obj marked; /* number of objects marked in a GC cycle */ - lu_mem GCestimate; /* an estimate of the non-garbage memory in use */ - lu_mem lastatomic; /* see function 'genstep' in file 'lgc.c' */ + l_obj GClastmajor; /* objects at last major collection */ stringtable strt; /* hash table for strings */ TValue l_registry; TValue nilvalue; /* a nil value */ diff --git a/ltests.c b/ltests.c index cc4bf58f1e..3f67c23aa4 100644 --- a/ltests.c +++ b/ltests.c @@ -297,7 +297,7 @@ static int testobjref1 (global_State *g, GCObject *f, GCObject *t) { if (isdead(g,t)) return 0; if (issweepphase(g)) return 1; /* no invariants */ - else if (g->gckind == KGC_INC) + else if (g->gckind != KGC_GEN) return !(isblack(f) && iswhite(t)); /* basic incremental invariant */ else { /* generational mode */ if ((getage(f) == G_OLD && isblack(f)) && !isold(t)) From 82fae58e25b7e2be6390238ce60d5678b24dce44 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 2 Dec 2022 11:33:09 -0300 Subject: [PATCH 0785/1145] Details Parentheses and comments. --- lmathlib.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lmathlib.c b/lmathlib.c index e0c61a168d..d0b1e1e5d6 100644 --- a/lmathlib.c +++ b/lmathlib.c @@ -267,7 +267,7 @@ static int math_type (lua_State *L) { /* try to find an integer type with at least 64 bits */ -#if (ULONG_MAX >> 31 >> 31) >= 3 +#if ((ULONG_MAX >> 31) >> 31) >= 3 /* 'long' has at least 64 bits */ #define Rand64 unsigned long @@ -277,9 +277,9 @@ static int math_type (lua_State *L) { /* there is a 'long long' type (which must have at least 64 bits) */ #define Rand64 unsigned long long -#elif (LUA_MAXUNSIGNED >> 31 >> 31) >= 3 +#elif ((LUA_MAXUNSIGNED >> 31) >> 31) >= 3 -/* 'lua_Integer' has at least 64 bits */ +/* 'lua_Unsigned' has at least 64 bits */ #define Rand64 lua_Unsigned #endif @@ -500,12 +500,12 @@ static lua_Number I2d (Rand64 x) { /* convert a 'Rand64' to a 'lua_Unsigned' */ static lua_Unsigned I2UInt (Rand64 x) { - return ((lua_Unsigned)trim32(x.h) << 31 << 1) | (lua_Unsigned)trim32(x.l); + return (((lua_Unsigned)trim32(x.h) << 31) << 1) | (lua_Unsigned)trim32(x.l); } /* convert a 'lua_Unsigned' to a 'Rand64' */ static Rand64 Int2I (lua_Unsigned n) { - return packI((lu_int32)(n >> 31 >> 1), (lu_int32)n); + return packI((lu_int32)((n >> 31) >> 1), (lu_int32)n); } #endif /* } */ From 0270c204c235a495ce4702ac3891eb30752d0c8d Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 6 Dec 2022 12:02:34 -0300 Subject: [PATCH 0786/1145] Simplification in handling of GC debt Each incremental step has always the same size (stepsize), and the debt for next step also is always the same. --- lapi.c | 30 +++++++++++++++++------------- lgc.c | 12 +++++------- 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/lapi.c b/lapi.c index 0cde72ccfe..d6d7a8db41 100644 --- a/lapi.c +++ b/lapi.c @@ -1145,7 +1145,7 @@ LUA_API int lua_gc (lua_State *L, int what, ...) { } case LUA_GCRESTART: { luaE_setdebt(g, 0); - g->gcstp = 0; /* (GCSTPGC must be already zero here) */ + g->gcstp = 0; /* (bit GCSTPGC must be zero here) */ break; } case LUA_GCCOLLECT: { @@ -1162,21 +1162,25 @@ LUA_API int lua_gc (lua_State *L, int what, ...) { break; } case LUA_GCSTEP: { - int data = va_arg(argp, int); - l_obj debt = 1; /* =1 to signal that it did an actual step */ + int todo = va_arg(argp, int); /* work to be done */ + int didsomething = 0; lu_byte oldstp = g->gcstp; - g->gcstp = 0; /* allow GC to run (GCSTPGC must be zero here) */ - if (data == 0) { - luaE_setdebt(g, 0); /* do a basic step */ - luaC_step(L); - } - else { /* add 'data' to total debt */ - debt = data + g->GCdebt; - luaE_setdebt(g, debt); - luaC_checkGC(L); + g->gcstp = 0; /* allow GC to run (bit GCSTPGC must be zero here) */ + if (todo == 0) + todo = 1 << g->gcstepsize; /* standard step size */ + while (todo + g->GCdebt > 0) { /* enough to run a step? */ + todo += g->GCdebt; /* decrement 'todo' (debt is usually negative) */ + luaC_step(L); /* run one basic step */ + didsomething = 1; + if (g->gckind == KGC_GEN) /* minor collections? */ + todo = 0; /* doesn't make sense to repeat in this case */ + else if (g->gcstate == GCSpause) + break; /* don't run more than one cycle */ } + /* add remaining 'todo' to total debt */ + luaE_setdebt(g, todo + g->GCdebt); g->gcstp = oldstp; /* restore previous state */ - if (debt > 0 && g->gcstate == GCSpause) /* end of cycle? */ + if (didsomething && g->gcstate == GCSpause) /* end of cycle? */ res = 1; /* signal it */ break; } diff --git a/lgc.c b/lgc.c index 1b24fda6eb..c93b59942f 100644 --- a/lgc.c +++ b/lgc.c @@ -1037,7 +1037,7 @@ void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt) { */ static void setpause (global_State *g) { unsigned int pause = getgcparam(g->gcpause); - lu_mem threshold = g->marked / 8 * pause / 12; + l_obj threshold = g->marked / 8 * pause / 12; l_obj debt = gettotalobjs(g) - threshold; if (debt > 0) debt = 0; luaE_setdebt(g, debt); @@ -1600,18 +1600,16 @@ void luaC_runtilstate (lua_State *L, int statesmask) { ** controls when next step will be performed. */ static void incstep (lua_State *L, global_State *g) { - int stepmul = (getgcparam(g->gcstepmul) | 1); /* avoid division by 0 */ - l_obj debt = (g->GCdebt / 100) * stepmul; l_obj stepsize = cast(l_obj, 1) << g->gcstepsize; + l_obj work2do = stepsize * getgcparam(g->gcstepmul) / 100; do { /* repeat until pause or enough "credit" (negative debt) */ l_obj work = singlestep(L); /* perform one single step */ - debt -= work; - } while (debt > -stepsize && g->gcstate != GCSpause); + work2do -= work; + } while (work2do > 0 && g->gcstate != GCSpause); if (g->gcstate == GCSpause) setpause(g); /* pause until next cycle */ else { - debt = (debt / stepmul) * 100; /* apply step multiplier */ - luaE_setdebt(g, debt); + luaE_setdebt(g, -stepsize); } } From d738c8d18bcc5651109b3a46103d6aa983772e68 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 7 Dec 2022 15:12:52 -0300 Subject: [PATCH 0787/1145] New function 'luaL_openselectedlibs' Makes it easier to start Lua with only some standard libraries. --- linit.c | 51 ++++++++++++++++++++++---------------------- ltests.c | 24 ++++----------------- ltests.h | 4 ++-- lua.c | 6 +++++- lualib.h | 39 ++++++++++++++++++++++----------- testes/api.lua | 14 ++++++------ testes/coroutine.lua | 2 +- 7 files changed, 71 insertions(+), 69 deletions(-) diff --git a/linit.c b/linit.c index 69808f84f4..675fb65fbb 100644 --- a/linit.c +++ b/linit.c @@ -8,21 +8,6 @@ #define linit_c #define LUA_LIB -/* -** If you embed Lua in your program and need to open the standard -** libraries, call luaL_openlibs in your program. If you need a -** different set of libraries, copy this file to your project and edit -** it to suit your needs. -** -** You can also *preload* libraries, so that a later 'require' can -** open the library, which is already linked to the application. -** For that, do the following code: -** -** luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE); -** lua_pushcfunction(L, luaopen_modname); -** lua_setfield(L, -2, modname); -** lua_pop(L, 1); // remove PRELOAD table -*/ #include "lprefix.h" @@ -36,30 +21,44 @@ /* -** these libs are loaded by lua.c and are readily available to any Lua -** program +** Standard Libraries */ -static const luaL_Reg loadedlibs[] = { +static const luaL_Reg stdlibs[] = { {LUA_GNAME, luaopen_base}, {LUA_LOADLIBNAME, luaopen_package}, + {LUA_COLIBNAME, luaopen_coroutine}, - {LUA_TABLIBNAME, luaopen_table}, + {LUA_DBLIBNAME, luaopen_debug}, {LUA_IOLIBNAME, luaopen_io}, + {LUA_MATHLIBNAME, luaopen_math}, {LUA_OSLIBNAME, luaopen_os}, {LUA_STRLIBNAME, luaopen_string}, - {LUA_MATHLIBNAME, luaopen_math}, + {LUA_TABLIBNAME, luaopen_table}, {LUA_UTF8LIBNAME, luaopen_utf8}, - {LUA_DBLIBNAME, luaopen_debug}, + {NULL, NULL} }; -LUALIB_API void luaL_openlibs (lua_State *L) { +/* +** require selected standard libraries and add the others to the +** preload table. +*/ +LUALIB_API void luaL_openselectedlibs (lua_State *L, int what) { + int mask = 1; const luaL_Reg *lib; - /* "require" functions from 'loadedlibs' and set results to global table */ - for (lib = loadedlibs; lib->func; lib++) { - luaL_requiref(L, lib->name, lib->func, 1); - lua_pop(L, 1); /* remove lib */ + luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE); + for (lib = stdlibs; lib->func; (lib++, mask <<= 1)) { + if (what & mask) { /* selected? */ + luaL_requiref(L, lib->name, lib->func, 1); /* require library */ + lua_pop(L, 1); /* remove result from the stack */ + } + else { /* add library to PRELOAD table */ + lua_pushcfunction(L, lib->func); + lua_setfield(L, -2, lib->name); + } } + lua_assert((mask >> 1) == LUA_UTF8LIBK); + lua_pop(L, 1); // remove PRELOAD table } diff --git a/ltests.c b/ltests.c index 3f67c23aa4..d5d2ae6814 100644 --- a/ltests.c +++ b/ltests.c @@ -1178,31 +1178,15 @@ static lua_State *getstate (lua_State *L) { static int loadlib (lua_State *L) { - static const luaL_Reg libs[] = { - {LUA_GNAME, luaopen_base}, - {"coroutine", luaopen_coroutine}, - {"debug", luaopen_debug}, - {"io", luaopen_io}, - {"os", luaopen_os}, - {"math", luaopen_math}, - {"string", luaopen_string}, - {"table", luaopen_table}, - {"T", luaB_opentests}, - {NULL, NULL} - }; lua_State *L1 = getstate(L); - int i; - luaL_requiref(L1, "package", luaopen_package, 0); + int what = luaL_checkinteger(L, 2); + luaL_openselectedlibs(L1, what); + luaL_requiref(L1, "T", luaB_opentests, 0); lua_assert(lua_type(L1, -1) == LUA_TTABLE); /* 'requiref' should not reload module already loaded... */ - luaL_requiref(L1, "package", NULL, 1); /* seg. fault if it reloads */ + luaL_requiref(L1, "T", NULL, 1); /* seg. fault if it reloads */ /* ...but should return the same module */ lua_assert(lua_compare(L1, -1, -2, LUA_OPEQ)); - luaL_getsubtable(L1, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE); - for (i = 0; libs[i].name; i++) { - lua_pushcfunction(L1, libs[i].func); - lua_setfield(L1, -2, libs[i].name); - } return 0; } diff --git a/ltests.h b/ltests.h index ec520498bd..45d5beba16 100644 --- a/ltests.h +++ b/ltests.h @@ -103,8 +103,8 @@ LUA_API void *debug_realloc (void *ud, void *block, #if defined(lua_c) #define luaL_newstate() lua_newstate(debug_realloc, &l_memcontrol) -#define luaL_openlibs(L) \ - { (luaL_openlibs)(L); \ +#define luai_openlibs(L) \ + { luaL_openlibs(L); \ luaL_requiref(L, "T", luaB_opentests, 1); \ lua_pop(L, 1); } #endif diff --git a/lua.c b/lua.c index 715430a0de..af20754ed1 100644 --- a/lua.c +++ b/lua.c @@ -609,6 +609,10 @@ static void doREPL (lua_State *L) { /* }================================================================== */ +#if !defined(luai_openlibs) +#define luai_openlibs(L) luaL_openlibs(L) +#endif + /* ** Main body of stand-alone interpreter (to be called in protected mode). @@ -631,7 +635,7 @@ static int pmain (lua_State *L) { lua_pushboolean(L, 1); /* signal for libraries to ignore env. vars. */ lua_setfield(L, LUA_REGISTRYINDEX, "LUA_NOENV"); } - luaL_openlibs(L); /* open standard libraries */ + luai_openlibs(L); /* open standard libraries */ createargtable(L, argv, argc, script); /* create table 'arg' */ lua_gc(L, LUA_GCRESTART); /* start GC... */ lua_gc(L, LUA_GCGEN, 0, 0); /* ...in generational mode */ diff --git a/lualib.h b/lualib.h index 2625529076..e124cf1b85 100644 --- a/lualib.h +++ b/lualib.h @@ -14,39 +14,52 @@ /* version suffix for environment variable names */ #define LUA_VERSUFFIX "_" LUA_VERSION_MAJOR "_" LUA_VERSION_MINOR - +#define LUA_GK 1 LUAMOD_API int (luaopen_base) (lua_State *L); +#define LUA_LOADLIBNAME "package" +#define LUA_LOADLIBK (LUA_GK << 1) +LUAMOD_API int (luaopen_package) (lua_State *L); + + #define LUA_COLIBNAME "coroutine" +#define LUA_COLIBK (LUA_LOADLIBK << 1) LUAMOD_API int (luaopen_coroutine) (lua_State *L); -#define LUA_TABLIBNAME "table" -LUAMOD_API int (luaopen_table) (lua_State *L); +#define LUA_DBLIBNAME "debug" +#define LUA_DBLIBK (LUA_COLIBK << 1) +LUAMOD_API int (luaopen_debug) (lua_State *L); #define LUA_IOLIBNAME "io" +#define LUA_IOLIBK (LUA_DBLIBK << 1) LUAMOD_API int (luaopen_io) (lua_State *L); +#define LUA_MATHLIBNAME "math" +#define LUA_MATHLIBK (LUA_IOLIBK << 1) +LUAMOD_API int (luaopen_math) (lua_State *L); + #define LUA_OSLIBNAME "os" +#define LUA_OSLIBK (LUA_MATHLIBK << 1) LUAMOD_API int (luaopen_os) (lua_State *L); #define LUA_STRLIBNAME "string" +#define LUA_STRLIBK (LUA_OSLIBK << 1) LUAMOD_API int (luaopen_string) (lua_State *L); +#define LUA_TABLIBNAME "table" +#define LUA_TABLIBK (LUA_STRLIBK << 1) +LUAMOD_API int (luaopen_table) (lua_State *L); + #define LUA_UTF8LIBNAME "utf8" +#define LUA_UTF8LIBK (LUA_TABLIBK << 1) LUAMOD_API int (luaopen_utf8) (lua_State *L); -#define LUA_MATHLIBNAME "math" -LUAMOD_API int (luaopen_math) (lua_State *L); - -#define LUA_DBLIBNAME "debug" -LUAMOD_API int (luaopen_debug) (lua_State *L); - -#define LUA_LOADLIBNAME "package" -LUAMOD_API int (luaopen_package) (lua_State *L); +/* open selected libraries */ +LUALIB_API void (luaL_openselectedlibs) (lua_State *L, int what); -/* open all previous libraries */ -LUALIB_API void (luaL_openlibs) (lua_State *L); +/* open all libraries */ +#define luaL_openlibs(L) luaL_openselectedlibs(L, ~0) #endif diff --git a/testes/api.lua b/testes/api.lua index bd85a923c8..f8e36ae3af 100644 --- a/testes/api.lua +++ b/testes/api.lua @@ -1039,10 +1039,12 @@ assert(a == nil and b == 2) -- 2 == run-time error a, b, c = T.doremote(L1, "return a+") assert(a == nil and c == 3 and type(b) == "string") -- 3 == syntax error -T.loadlib(L1) +T.loadlib(L1, 2) -- load only 'package' a, b, c = T.doremote(L1, [[ string = require'string' - a = require'_G'; assert(a == _G and require("_G") == a) + local initialG = _G -- not loaded yet + local a = require'_G'; assert(a == _G and require("_G") == a) + assert(initialG == nil and io == nil) -- now we have 'assert' io = require'io'; assert(type(io.read) == "function") assert(require("io") == io) a = require'table'; assert(type(a.insert) == "function") @@ -1056,7 +1058,7 @@ T.closestate(L1); L1 = T.newstate() -T.loadlib(L1) +T.loadlib(L1, 0) T.doremote(L1, "a = {}") T.testC(L1, [[getglobal "a"; pushstring "x"; pushint 1; settable -3]]) @@ -1436,10 +1438,10 @@ end do -- garbage collection with no extra memory local L = T.newstate() - T.loadlib(L) + T.loadlib(L, 1 | 2) -- load _G and 'package' local res = (T.doremote(L, [[ - _ENV = require"_G" - local T = require"T" + _ENV = _G + assert(string == nil) local a = {} for i = 1, 1000 do a[i] = 'i' .. i end -- grow string table local stsize, stuse = T.querystr() diff --git a/testes/coroutine.lua b/testes/coroutine.lua index 15fccc3083..f05672a5f6 100644 --- a/testes/coroutine.lua +++ b/testes/coroutine.lua @@ -694,7 +694,7 @@ else T.testC(state, "settop 0") - T.loadlib(state) + T.loadlib(state, 1 | 2) -- load _G and 'package' assert(T.doremote(state, [[ coroutine = require'coroutine'; From fa2f294dd1269115bf9cf534dd38553e2f606801 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 9 Dec 2022 16:35:19 -0300 Subject: [PATCH 0788/1145] Reduce calls to 'luaC_step' when GC is stopped --- lgc.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/lgc.c b/lgc.c index 2e74990256..a3094ff571 100644 --- a/lgc.c +++ b/lgc.c @@ -1681,12 +1681,15 @@ static void incstep (lua_State *L, global_State *g) { } /* -** performs a basic GC step if collector is running +** Performs a basic GC step if collector is running. (If collector is +** not running, set a reasonable debt to avoid it being called at +** every single check.) */ void luaC_step (lua_State *L) { global_State *g = G(L); - lua_assert(!g->gcemergency); - if (gcrunning(g)) { /* running? */ + if (!gcrunning(g)) /* not running? */ + luaE_setdebt(g, -2000); + else { if(isdecGCmodegen(g)) genstep(L, g); else From 40565b4a089f44fdcb16f4ed0080b0ca3755e4aa Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 13 Dec 2022 11:55:14 -0300 Subject: [PATCH 0789/1145] Revamp of GC parameters More uniformity when handling GC parameters + avoid divisions by 100 when applying them. --- lapi.c | 30 +++++++++++++++--------------- lgc.c | 14 +++++--------- lgc.h | 44 ++++++++++++++++++++++++++++++++++---------- llimits.h | 2 +- lstate.c | 8 ++++---- ltests.c | 4 ++-- 6 files changed, 61 insertions(+), 41 deletions(-) diff --git a/lapi.c b/lapi.c index d6d7a8db41..8c70bd4ca2 100644 --- a/lapi.c +++ b/lapi.c @@ -1185,15 +1185,15 @@ LUA_API int lua_gc (lua_State *L, int what, ...) { break; } case LUA_GCSETPAUSE: { - int data = va_arg(argp, int); - res = getgcparam(g->gcpause); - setgcparam(g->gcpause, data); + unsigned int data = va_arg(argp, unsigned int); + res = applygcparam(g, gcpause, 100); + setgcparam(g, gcpause, data); break; } case LUA_GCSETSTEPMUL: { - int data = va_arg(argp, int); - res = getgcparam(g->gcstepmul); - setgcparam(g->gcstepmul, data); + unsigned int data = va_arg(argp, unsigned int); + res = applygcparam(g, gcstepmul, 100); + setgcparam(g, gcstepmul, data); break; } case LUA_GCISRUNNING: { @@ -1201,25 +1201,25 @@ LUA_API int lua_gc (lua_State *L, int what, ...) { break; } case LUA_GCGEN: { - int minormul = va_arg(argp, int); - int majormul = va_arg(argp, int); + unsigned int minormul = va_arg(argp, unsigned int); + unsigned int majormul = va_arg(argp, unsigned int); res = (g->gckind == KGC_INC) ? LUA_GCINC : LUA_GCGEN; if (minormul != 0) - g->genminormul = minormul; + setgcparam(g, genminormul, minormul); if (majormul != 0) - setgcparam(g->genmajormul, majormul); + setgcparam(g, genmajormul, majormul); luaC_changemode(L, KGC_GEN); break; } case LUA_GCINC: { - int pause = va_arg(argp, int); - int stepmul = va_arg(argp, int); - int stepsize = va_arg(argp, int); + unsigned int pause = va_arg(argp, unsigned int); + unsigned int stepmul = va_arg(argp, unsigned int); + unsigned int stepsize = va_arg(argp, unsigned int); res = (g->gckind == KGC_INC) ? LUA_GCINC : LUA_GCGEN; if (pause != 0) - setgcparam(g->gcpause, pause); + setgcparam(g, gcpause, pause); if (stepmul != 0) - setgcparam(g->gcstepmul, stepmul); + setgcparam(g, gcstepmul, stepmul); if (stepsize != 0) g->gcstepsize = (stepsize <= log2maxs(l_obj)) ? stepsize : log2maxs(l_obj); diff --git a/lgc.c b/lgc.c index c2b0535d2c..90a49091b7 100644 --- a/lgc.c +++ b/lgc.c @@ -1030,14 +1030,10 @@ void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt) { /* ** Set the "time" to wait before starting a new GC cycle; cycle will ** start when number of objects in use hits the threshold of -** approximately ('marked' * pause / 100). (A direct multiplication -** by 'pause' may overflow, and a direct division by 100 may undeflow -** to zero. So, the division is done in two steps. 8 * 12 is near 100 -** and the division by 8 is cheap.) +** approximately (marked * pause / 100). */ static void setpause (global_State *g) { - unsigned int pause = getgcparam(g->gcpause); - l_obj threshold = g->marked / 8 * pause / 12; + l_obj threshold = applygcparam(g, gcpause, g->marked); l_obj debt = gettotalobjs(g) - threshold; if (debt > 0) debt = 0; luaE_setdebt(g, debt); @@ -1289,7 +1285,7 @@ static void atomic2gen (lua_State *L, global_State *g) { ** total number of objects grows 'genminormul'%. */ static void setminordebt (global_State *g) { - luaE_setdebt(g, -(gettotalobjs(g) / 100) * g->genminormul); + luaE_setdebt(g, -applygcparam(g, genminormul, gettotalobjs(g))); } @@ -1387,7 +1383,7 @@ static void genmajorstep (lua_State *L, global_State *g) { */ static void genstep (lua_State *L, global_State *g) { l_obj majorbase = g->GClastmajor; /* count after last major collection */ - l_obj majorinc = (majorbase / 100) * getgcparam(g->genmajormul); + l_obj majorinc = applygcparam(g, genmajormul, majorbase); if (g->GCdebt > 0 && gettotalobjs(g) > majorbase + majorinc) { /* do a major collection */ enterinc(g); @@ -1601,7 +1597,7 @@ void luaC_runtilstate (lua_State *L, int statesmask) { */ static void incstep (lua_State *L, global_State *g) { l_obj stepsize = cast(l_obj, 1) << g->gcstepsize; - l_obj work2do = stepsize * getgcparam(g->gcstepmul) / 100; + l_obj work2do = applygcparam(g, gcstepmul, stepsize); do { /* repeat until pause or enough "credit" (negative debt) */ l_obj work = singlestep(L); /* perform one single step */ work2do -= work; diff --git a/lgc.h b/lgc.h index 3c8719695b..6d82690d67 100644 --- a/lgc.h +++ b/lgc.h @@ -8,6 +8,9 @@ #define lgc_h +#include + + #include "lobject.h" #include "lstate.h" @@ -122,20 +125,18 @@ /* Default Values for GC parameters */ -#define LUAI_GENMAJORMUL 100 -#define LUAI_GENMINORMUL 20 + +/* generational */ + +#define LUAI_GENMAJORMUL 100 /* major multiplier */ +#define LUAI_GENMINORMUL 20 /* minor multiplier */ + +/* incremental */ /* wait memory to double before starting new cycle */ #define LUAI_GCPAUSE 200 -/* -** some gc parameters are stored divided by 4 to allow a maximum value -** up to 1023 in a 'lu_byte'. -*/ -#define getgcparam(p) ((p) * 4) -#define setgcparam(p,v) ((p) = (v) / 4) - -#define LUAI_GCMUL 300 +#define LUAI_GCMUL 300 /* step multiplier */ /* how many objects to allocate before next GC step (log2) */ #define LUAI_GCSTEPSIZE 8 /* 256 objects */ @@ -149,6 +150,29 @@ #define GCSTPCLS 4 /* bit true when closing Lua state */ #define gcrunning(g) ((g)->gcstp == 0) +/* +** Macros to set and apply GC parameters. GC parameters are given in +** percentage points, but are stored as lu_byte. To reduce their +** values and avoid repeated divisions by 100, these macros store +** the original parameter multiplied by 2^n and divided by 100. +** To apply them, the value is divided by 2^n (a shift) and then +** multiplied by the stored parameter, yielding +** value / 2^n * (original parameter * 2^n / 100), or approximately +** (value * original parameter / 100). +** +** For most parameters, which are typically larger than 100%, 2^n is +** 16 (2^4), allowing maximum values up to 1599. For the minor +** multiplier, which is typically smaller, 2^n is 64 (2^6) to allow more +** precision. +*/ +#define gcparamshift(p) \ + (offsetof(global_State, p) == offsetof(global_State, genminormul) ? 6 : 4) + +#define setgcparam(g,p,v) \ + (g->p = (cast_uint(v) << gcparamshift(p)) / 100u) +#define applygcparam(g,p,v) (((v) >> gcparamshift(p)) * g->p) + + /* ** Does one step of collection when debt becomes positive. 'pre'/'pos' diff --git a/llimits.h b/llimits.h index 525b36475f..e494879108 100644 --- a/llimits.h +++ b/llimits.h @@ -18,7 +18,7 @@ /* ** 'lu_mem' is an unsigned integer big enough to count the total memory ** used by Lua (in bytes). 'l_obj' is a signed integer big enough to -** count the total number of objects used by Lua. (It is negative due +** count the total number of objects used by Lua. (It is signed due ** to the use of debt in several computations.) Usually, 'size_t' and ** 'ptrdiff_t' should work, but we use 'long' for 16-bit machines. */ diff --git a/lstate.c b/lstate.c index a4379f935c..b9897d9602 100644 --- a/lstate.c +++ b/lstate.c @@ -392,11 +392,11 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { g->marked = 0; g->GCdebt = 0; setivalue(&g->nilvalue, 0); /* to signal that state is not yet built */ - setgcparam(g->gcpause, LUAI_GCPAUSE); - setgcparam(g->gcstepmul, LUAI_GCMUL); + setgcparam(g, gcpause, LUAI_GCPAUSE); + setgcparam(g, gcstepmul, LUAI_GCMUL); g->gcstepsize = LUAI_GCSTEPSIZE; - setgcparam(g->genmajormul, LUAI_GENMAJORMUL); - g->genminormul = LUAI_GENMINORMUL; + setgcparam(g, genmajormul, LUAI_GENMAJORMUL); + setgcparam(g, genminormul, LUAI_GENMINORMUL); for (i=0; i < LUA_NUMTAGS; i++) g->mt[i] = NULL; if (luaD_rawrunprotected(L, f_luaopen, NULL) != LUA_OK) { /* memory allocation error: free partial state */ diff --git a/ltests.c b/ltests.c index d5d2ae6814..e2e0d983f2 100644 --- a/ltests.c +++ b/ltests.c @@ -1031,8 +1031,8 @@ static int query_inc (lua_State *L) { global_State *g = G(L); lua_pushinteger(L, gettotalobjs(g)); lua_pushinteger(L, g->GCdebt); - lua_pushinteger(L, getgcparam(g->gcpause)); - lua_pushinteger(L, getgcparam(g->gcstepmul)); + lua_pushinteger(L, applygcparam(g, gcpause, 100)); + lua_pushinteger(L, applygcparam(g, gcstepmul, 100)); lua_pushinteger(L, cast(l_obj, 1) << g->gcstepsize); return 5; } From 5d8b5b9290c932bdfd7dcc670a5af957bdd58392 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 13 Dec 2022 15:45:57 -0300 Subject: [PATCH 0790/1145] Changed signal of GC debt Positive debts seems more natural then negative ones. --- lapi.c | 8 ++++---- lgc.c | 16 ++++++++-------- lgc.h | 4 ++-- llimits.h | 7 ++----- lstate.c | 6 +++--- lstate.h | 6 +++--- 6 files changed, 22 insertions(+), 25 deletions(-) diff --git a/lapi.c b/lapi.c index 8c70bd4ca2..b2ac0c57cc 100644 --- a/lapi.c +++ b/lapi.c @@ -1168,8 +1168,8 @@ LUA_API int lua_gc (lua_State *L, int what, ...) { g->gcstp = 0; /* allow GC to run (bit GCSTPGC must be zero here) */ if (todo == 0) todo = 1 << g->gcstepsize; /* standard step size */ - while (todo + g->GCdebt > 0) { /* enough to run a step? */ - todo += g->GCdebt; /* decrement 'todo' (debt is usually negative) */ + while (todo >= g->GCdebt) { /* enough to run a step? */ + todo -= g->GCdebt; /* decrement 'todo' */ luaC_step(L); /* run one basic step */ didsomething = 1; if (g->gckind == KGC_GEN) /* minor collections? */ @@ -1177,8 +1177,8 @@ LUA_API int lua_gc (lua_State *L, int what, ...) { else if (g->gcstate == GCSpause) break; /* don't run more than one cycle */ } - /* add remaining 'todo' to total debt */ - luaE_setdebt(g, todo + g->GCdebt); + /* remove remaining 'todo' from total debt */ + luaE_setdebt(g, g->GCdebt - todo); g->gcstp = oldstp; /* restore previous state */ if (didsomething && g->gcstate == GCSpause) /* end of cycle? */ res = 1; /* signal it */ diff --git a/lgc.c b/lgc.c index 90a49091b7..278566500e 100644 --- a/lgc.c +++ b/lgc.c @@ -242,7 +242,7 @@ GCObject *luaC_newobjdt (lua_State *L, int tt, size_t sz, size_t offset) { global_State *g = G(L); char *p = cast_charp(luaM_newobject(L, novariant(tt), sz)); GCObject *o = cast(GCObject *, p + offset); - g->GCdebt++; + g->GCdebt--; o->marked = luaC_white(g); o->tt = tt; o->next = g->allgc; @@ -1034,8 +1034,8 @@ void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt) { */ static void setpause (global_State *g) { l_obj threshold = applygcparam(g, gcpause, g->marked); - l_obj debt = gettotalobjs(g) - threshold; - if (debt > 0) debt = 0; + l_obj debt = threshold - gettotalobjs(g); + if (debt < 0) debt = 0; luaE_setdebt(g, debt); } @@ -1285,7 +1285,7 @@ static void atomic2gen (lua_State *L, global_State *g) { ** total number of objects grows 'genminormul'%. */ static void setminordebt (global_State *g) { - luaE_setdebt(g, -applygcparam(g, genminormul, gettotalobjs(g))); + luaE_setdebt(g, applygcparam(g, genminormul, gettotalobjs(g))); } @@ -1378,13 +1378,13 @@ static void genmajorstep (lua_State *L, global_State *g) { ** Does a generational "step". If the total number of objects grew ** more than 'majormul'% since the last major collection, does a ** major collection. Otherwise, does a minor collection. The test -** ('GCdebt' > 0) avoids major collections when the step originated from +** ('GCdebt' != 0) avoids major collections when the step originated from ** 'collectgarbage("step")'. */ static void genstep (lua_State *L, global_State *g) { l_obj majorbase = g->GClastmajor; /* count after last major collection */ l_obj majorinc = applygcparam(g, genmajormul, majorbase); - if (g->GCdebt > 0 && gettotalobjs(g) > majorbase + majorinc) { + if (g->GCdebt != 0 && gettotalobjs(g) > majorbase + majorinc) { /* do a major collection */ enterinc(g); g->gckind = KGC_GENMAJOR; @@ -1605,7 +1605,7 @@ static void incstep (lua_State *L, global_State *g) { if (g->gcstate == GCSpause) setpause(g); /* pause until next cycle */ else { - luaE_setdebt(g, -stepsize); + luaE_setdebt(g, stepsize); } } @@ -1618,7 +1618,7 @@ void luaC_step (lua_State *L) { global_State *g = G(L); lua_assert(!g->gcemergency); if (!gcrunning(g)) /* not running? */ - luaE_setdebt(g, -2000); + luaE_setdebt(g, 2000); else { switch (g->gckind) { case KGC_INC: diff --git a/lgc.h b/lgc.h index 6d82690d67..c8f7c6e6e3 100644 --- a/lgc.h +++ b/lgc.h @@ -175,13 +175,13 @@ /* -** Does one step of collection when debt becomes positive. 'pre'/'pos' +** Does one step of collection when debt becomes zero. 'pre'/'pos' ** allows some adjustments to be done only when needed. macro ** 'condchangemem' is used only for heavy tests (forcing a full ** GC cycle on every opportunity) */ #define luaC_condGC(L,pre,pos) \ - { if (G(L)->GCdebt > 0) { pre; luaC_step(L); pos;}; \ + { if (G(L)->GCdebt <= 0) { pre; luaC_step(L); pos;}; \ condchangemem(L,pre,pos); } /* more often than not, 'pre'/'pos' are empty */ diff --git a/llimits.h b/llimits.h index e494879108..246dca8b4f 100644 --- a/llimits.h +++ b/llimits.h @@ -33,6 +33,8 @@ typedef unsigned long lu_mem; typedef long l_obj; #endif /* } */ +#define MAX_LOBJ cast(l_obj, ~cast(lu_mem, 0) >> 1) + /* chars used as small naturals (so that 'char' is reserved for characters) */ typedef unsigned char lu_byte; @@ -47,11 +49,6 @@ typedef signed char ls_byte; : (size_t)(LUA_MAXINTEGER)) -#define MAX_LUMEM ((lu_mem)(~(lu_mem)0)) - -#define MAX_LMEM ((l_obj)(MAX_LUMEM >> 1)) - - #define MAX_INT INT_MAX /* maximum value of an int */ diff --git a/lstate.c b/lstate.c index b9897d9602..bee3bf664c 100644 --- a/lstate.c +++ b/lstate.c @@ -89,9 +89,9 @@ static unsigned int luai_makeseed (lua_State *L) { void luaE_setdebt (global_State *g, l_obj debt) { l_obj tb = gettotalobjs(g); lua_assert(tb > 0); - if (debt < tb - MAX_LMEM) - debt = tb - MAX_LMEM; /* will make 'totalobjs == MAX_LMEM' */ - g->totalobjs = tb - debt; + if (debt > MAX_LOBJ - tb) + debt = MAX_LOBJ - tb; /* will make 'totalobjs == MAX_LMEM' */ + g->totalobjs = tb + debt; g->GCdebt = debt; } diff --git a/lstate.h b/lstate.h index c290ff328b..1aef2f758d 100644 --- a/lstate.h +++ b/lstate.h @@ -251,8 +251,8 @@ typedef struct global_State { lua_Alloc frealloc; /* function to reallocate memory */ void *ud; /* auxiliary data to 'frealloc' */ lu_mem totalbytes; /* number of bytes currently allocated */ - l_obj totalobjs; /* total number of objects allocated - GCdebt */ - l_obj GCdebt; /* bytes allocated not yet compensated by the collector */ + l_obj totalobjs; /* total number of objects allocated + GCdebt */ + l_obj GCdebt; /* objects counted but not yet allocated */ l_obj marked; /* number of objects marked in a GC cycle */ l_obj GClastmajor; /* objects at last major collection */ stringtable strt; /* hash table for strings */ @@ -388,7 +388,7 @@ union GCUnion { /* actual number of total objects allocated */ -#define gettotalobjs(g) ((g)->totalobjs + (g)->GCdebt) +#define gettotalobjs(g) ((g)->totalobjs - (g)->GCdebt) LUAI_FUNC void luaE_setdebt (global_State *g, l_obj debt); From 6aabf4b15e7637c2ab4133abf3df0a77f34b6005 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 14 Dec 2022 16:20:39 -0300 Subject: [PATCH 0791/1145] Details in some header files Identifier LUA_NUMTAGS was deprecated (changed to LUA_NUMTYPES) + better handling of some inclusion loops. --- lstate.h | 11 ++++++++--- ltm.h | 5 +++-- lua.h | 16 ++++++++++------ 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/lstate.h b/lstate.h index 2e90781872..8bf6600e34 100644 --- a/lstate.h +++ b/lstate.h @@ -9,6 +9,11 @@ #include "lua.h" + +/* Some header files included here need this definition */ +typedef struct CallInfo CallInfo; + + #include "lobject.h" #include "ltm.h" #include "lzio.h" @@ -169,7 +174,7 @@ typedef struct stringtable { ** - field 'transferinfo' is used only during call/returnhooks, ** before the function starts or after it ends. */ -typedef struct CallInfo { +struct CallInfo { StkIdRel func; /* function index in the stack */ StkIdRel top; /* top for this function */ struct CallInfo *previous, *next; /* dynamic call link */ @@ -196,7 +201,7 @@ typedef struct CallInfo { } u2; short nresults; /* expected number of results from this function */ unsigned short callstatus; -} CallInfo; +}; /* @@ -291,7 +296,7 @@ typedef struct global_State { struct lua_State *mainthread; TString *memerrmsg; /* message for memory-allocation errors */ TString *tmname[TM_N]; /* array with tag-method names */ - struct Table *mt[LUA_NUMTAGS]; /* metatables for basic types */ + struct Table *mt[LUA_NUMTYPES]; /* metatables for basic types */ TString *strcache[STRCACHE_N][STRCACHE_M]; /* cache for strings in API */ lua_WarnFunction warnf; /* warning function */ void *ud_warn; /* auxiliary data to 'warnf' */ diff --git a/ltm.h b/ltm.h index 73b833c605..c309e2ae10 100644 --- a/ltm.h +++ b/ltm.h @@ -9,6 +9,7 @@ #include "lobject.h" +#include "lstate.h" /* @@ -95,8 +96,8 @@ LUAI_FUNC int luaT_callorderiTM (lua_State *L, const TValue *p1, int v2, int inv, int isfloat, TMS event); LUAI_FUNC void luaT_adjustvarargs (lua_State *L, int nfixparams, - struct CallInfo *ci, const Proto *p); -LUAI_FUNC void luaT_getvarargs (lua_State *L, struct CallInfo *ci, + CallInfo *ci, const Proto *p); +LUAI_FUNC void luaT_getvarargs (lua_State *L, CallInfo *ci, StkId where, int wanted); diff --git a/lua.h b/lua.h index bfba4d1e1b..feb3dbc556 100644 --- a/lua.h +++ b/lua.h @@ -131,6 +131,16 @@ typedef void * (*lua_Alloc) (void *ud, void *ptr, size_t osize, size_t nsize); typedef void (*lua_WarnFunction) (void *ud, const char *msg, int tocont); +/* +** Type used by the debug API to collect debug information +*/ +typedef struct lua_Debug lua_Debug; + + +/* +** Functions to be called by the debugger in specific events +*/ +typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar); /* @@ -442,12 +452,6 @@ LUA_API void (lua_closeslot) (lua_State *L, int idx); #define LUA_MASKLINE (1 << LUA_HOOKLINE) #define LUA_MASKCOUNT (1 << LUA_HOOKCOUNT) -typedef struct lua_Debug lua_Debug; /* activation record */ - - -/* Functions to be called by the debugger in specific events */ -typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar); - LUA_API int (lua_getstack) (lua_State *L, int level, lua_Debug *ar); LUA_API int (lua_getinfo) (lua_State *L, const char *what, lua_Debug *ar); From 35e01ed21d018e1c6428ae0351d8597e53a620b3 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 15 Dec 2022 10:44:55 -0300 Subject: [PATCH 0792/1145] Small improvements in 'lmem.c' Added some auxiliary macros + fixed a bug in compilation option EMERGENCYGCTESTS. (It should not try to force an emergency collection when it cannot run one.) --- lmem.c | 68 +++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 41 insertions(+), 27 deletions(-) diff --git a/lmem.c b/lmem.c index 9029d588c1..9800a86fc0 100644 --- a/lmem.c +++ b/lmem.c @@ -22,25 +22,6 @@ #include "lstate.h" -#if defined(EMERGENCYGCTESTS) -/* -** First allocation will fail whenever not building initial state. -** (This fail will trigger 'tryagain' and a full GC cycle at every -** allocation.) -*/ -static void *firsttry (global_State *g, void *block, size_t os, size_t ns) { - if (completestate(g) && ns > 0) /* frees never fail */ - return NULL; /* fail */ - else /* normal allocation */ - return (*g->frealloc)(g->ud, block, os, ns); -} -#else -#define firsttry(g,block,os,ns) ((*g->frealloc)(g->ud, block, os, ns)) -#endif - - - - /* ** About the realloc function: @@ -60,6 +41,43 @@ static void *firsttry (global_State *g, void *block, size_t os, size_t ns) { */ +/* +** Macro to call the allocation function. +*/ +#define callfrealloc(g,block,os,ns) ((*g->frealloc)(g->ud, block, os, ns)) + + +/* +** When an allocation fails, it will try again after an emergency +** collection, except when it cannot run a collection. The GC should +** not be called while the state is not fully built, as the collector +** is not yet fully initialized. Also, it should not be called when +** 'gcstopem' is true, because then the interpreter is in the middle of +** a collection step. +*/ +#define cantryagain(g) (completestate(g) && !g->gcstopem) + + + + +#if defined(EMERGENCYGCTESTS) +/* +** First allocation will fail except when freeing a block (frees never +** fail) and when it cannot try again; this fail will trigger 'tryagain' +** and a full GC cycle at every allocation. +*/ +static void *firsttry (global_State *g, void *block, size_t os, size_t ns) { + if (ns > 0 && cantryagain(g)) + return NULL; /* fail */ + else /* normal allocation */ + return callfrealloc(g, block, os, ns); +} +#else +#define firsttry(g,block,os,ns) callfrealloc(g, block, os, ns) +#endif + + + /* @@ -132,7 +150,7 @@ l_noret luaM_toobig (lua_State *L) { void luaM_free_ (lua_State *L, void *block, size_t osize) { global_State *g = G(L); lua_assert((osize == 0) == (block == NULL)); - (*g->frealloc)(g->ud, block, osize, 0); + callfrealloc(g, block, osize, 0); g->GCdebt -= osize; } @@ -140,19 +158,15 @@ void luaM_free_ (lua_State *L, void *block, size_t osize) { /* ** In case of allocation fail, this function will do an emergency ** collection to free some memory and then try the allocation again. -** The GC should not be called while state is not fully built, as the -** collector is not yet fully initialized. Also, it should not be called -** when 'gcstopem' is true, because then the interpreter is in the -** middle of a collection step. */ static void *tryagain (lua_State *L, void *block, size_t osize, size_t nsize) { global_State *g = G(L); - if (completestate(g) && !g->gcstopem) { + if (cantryagain(g)) { luaC_fullgc(L, 1); /* try to free some memory... */ - return (*g->frealloc)(g->ud, block, osize, nsize); /* try again */ + return callfrealloc(g, block, osize, nsize); /* try again */ } - else return NULL; /* cannot free any memory without a full state */ + else return NULL; /* cannot run an emergency collection */ } From f874d37fa28037bf3d3300ef8c0740d13792404b Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 15 Dec 2022 14:18:03 -0300 Subject: [PATCH 0793/1145] Small change in barrier macros Reuse macros for objects when defining the macros for values. --- lgc.h | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/lgc.h b/lgc.h index c960e70647..538f6edccc 100644 --- a/lgc.h +++ b/lgc.h @@ -172,18 +172,19 @@ #define luaC_checkGC(L) luaC_condGC(L,(void)0,(void)0) -#define luaC_barrier(L,p,v) ( \ - (iscollectable(v) && isblack(p) && iswhite(gcvalue(v))) ? \ - luaC_barrier_(L,obj2gco(p),gcvalue(v)) : cast_void(0)) - -#define luaC_barrierback(L,p,v) ( \ - (iscollectable(v) && isblack(p) && iswhite(gcvalue(v))) ? \ - luaC_barrierback_(L,p) : cast_void(0)) - #define luaC_objbarrier(L,p,o) ( \ (isblack(p) && iswhite(o)) ? \ luaC_barrier_(L,obj2gco(p),obj2gco(o)) : cast_void(0)) +#define luaC_barrier(L,p,v) ( \ + iscollectable(v) ? luaC_objbarrier(L,p,gcvalue(v)) : cast_void(0)) + +#define luaC_objbarrierback(L,p,o) ( \ + (isblack(p) && iswhite(o)) ? luaC_barrierback_(L,p) : cast_void(0)) + +#define luaC_barrierback(L,p,v) ( \ + iscollectable(v) ? luaC_objbarrierback(L, p, gcvalue(v)) : cast_void(0)) + LUAI_FUNC void luaC_fix (lua_State *L, GCObject *o); LUAI_FUNC void luaC_freeallobjects (lua_State *L); LUAI_FUNC void luaC_step (lua_State *L); From d70a0c91ad42275af1f6f1b6e37c604442b3f0d1 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 15 Dec 2022 16:44:22 -0300 Subject: [PATCH 0794/1145] Dump/undump reuse strings A repeated string in a dump is represented as an index to its first occurence, instead of another copy of the string. --- lapi.c | 29 +++++++++++++++++++++++++---- ldump.c | 37 +++++++++++++++++++++++++++++++------ lstrlib.c | 1 + lundump.c | 20 +++++++++++++++++++- lundump.h | 2 +- 5 files changed, 77 insertions(+), 12 deletions(-) diff --git a/lapi.c b/lapi.c index b2ac0c57cc..a1eb7dc69e 100644 --- a/lapi.c +++ b/lapi.c @@ -1107,16 +1107,37 @@ LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data, } +/* +** Dump a function, calling 'writer' to write its parts. Because the +** writer can use the stack in unkown ways, this function should not +** push things on the stack, but it must anchor an auxiliary table +** used by 'luaU_dump'. To do so, it creates the table, anchors the +** function that is on the stack in the table, and substitutes the +** table for the function in the stack. +*/ + LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data, int strip) { int status; + StkId fstk; /* pointer to function */ TValue *o; lua_lock(L); api_checknelems(L, 1); - o = s2v(L->top.p - 1); - if (isLfunction(o)) - status = luaU_dump(L, getproto(o), writer, data, strip); - else + fstk = L->top.p - 1; + o = s2v(fstk); + if (!isLfunction(o)) status = 1; + else { + LClosure *f = clLvalue(o); + ptrdiff_t fidx = savestack(L, fstk); /* function index */ + Table *h = luaH_new(L); /* auxiliary table used by 'luaU_dump' */ + sethvalue2s(L, L->top.p, h); /* anchor it (luaH_set may call GC) */ + L->top.p++; /* (assume extra slot) */ + luaH_set(L, h, o, o); /* anchor function into table */ + setobjs2s(L, fstk, L->top.p - 1); /* move table over function */ + L->top.p--; /* stack back to initial size */ + status = luaU_dump(L, f->p, writer, data, strip, h); + setclLvalue2s(L, restorestack(L, fidx), f); /* put function back */ + } lua_unlock(L); return status; } diff --git a/ldump.c b/ldump.c index f848b669cb..70c7adc61d 100644 --- a/ldump.c +++ b/ldump.c @@ -14,8 +14,10 @@ #include "lua.h" +#include "lgc.h" #include "lobject.h" #include "lstate.h" +#include "ltable.h" #include "lundump.h" @@ -25,6 +27,8 @@ typedef struct { void *data; int strip; int status; + Table *h; /* table to track saved strings */ + lua_Integer nstr; /* counter to number saved strings */ } DumpState; @@ -85,14 +89,33 @@ static void dumpInteger (DumpState *D, lua_Integer x) { } -static void dumpString (DumpState *D, const TString *s) { +/* +** Dump a String. First dump its "size": size==0 means NULL; +** size==1 is followed by an index and means "reuse saved string with +** that index"; size>=2 is followed by the string contents with real +** size==size-2 and means that string, which will be saved with +** the next available index. +*/ +static void dumpString (DumpState *D, TString *s) { if (s == NULL) dumpSize(D, 0); else { - size_t size = tsslen(s); - const char *str = getstr(s); - dumpSize(D, size + 1); - dumpVector(D, str, size); + const TValue *idx = luaH_getstr(D->h, s); + if (ttisinteger(idx)) { /* string already saved? */ + dumpSize(D, 1); /* reuse a saved string */ + dumpInt(D, ivalue(idx)); /* index of saved string */ + } + else { /* must write and save the string */ + TValue key, value; /* to save the string in the hash */ + size_t size = tsslen(s); + dumpSize(D, size + 2); + dumpVector(D, getstr(s), size); + D->nstr++; /* one more saved string */ + setsvalue(D->L, &key, s); /* the string is the key */ + setivalue(&value, D->nstr); /* its index is the value */ + luaH_finishset(D->L, D->h, &key, idx, &value); /* h[s] = nstr */ + /* integer value does not need barrier */ + } } } @@ -211,13 +234,15 @@ static void dumpHeader (DumpState *D) { ** dump Lua function as precompiled chunk */ int luaU_dump(lua_State *L, const Proto *f, lua_Writer w, void *data, - int strip) { + int strip, Table *h) { DumpState D; D.L = L; D.writer = w; D.data = data; D.strip = strip; D.status = 0; + D.h = h; + D.nstr = 0; dumpHeader(&D); dumpByte(&D, f->sizeupvalues); dumpFunction(&D, f, NULL); diff --git a/lstrlib.c b/lstrlib.c index 0b4fdbb7b5..ce07d9bceb 100644 --- a/lstrlib.c +++ b/lstrlib.c @@ -239,6 +239,7 @@ static int str_dump (lua_State *L) { if (l_unlikely(lua_dump(L, writer, &state, strip) != 0)) return luaL_error(L, "unable to dump given function"); luaL_pushresult(&state.B); + lua_assert(lua_isfunction(L, 1)); /* lua_dump kept that value */ return 1; } diff --git a/lundump.c b/lundump.c index aba93f8280..4048fdeae1 100644 --- a/lundump.c +++ b/lundump.c @@ -21,6 +21,7 @@ #include "lmem.h" #include "lobject.h" #include "lstring.h" +#include "ltable.h" #include "lundump.h" #include "lzio.h" @@ -34,6 +35,8 @@ typedef struct { lua_State *L; ZIO *Z; const char *name; + Table *h; /* list for string reuse */ + lua_Integer nstr; /* number of strings in the list */ } LoadState; @@ -110,10 +113,16 @@ static lua_Integer loadInteger (LoadState *S) { static TString *loadStringN (LoadState *S, Proto *p) { lua_State *L = S->L; TString *ts; + TValue sv; size_t size = loadSize(S); if (size == 0) /* no string? */ return NULL; - else if (--size <= LUAI_MAXSHORTLEN) { /* short string? */ + else if (size == 1) { /* previously saved string? */ + int idx = loadInt(S); /* get its index */ + const TValue *stv = luaH_getint(S->h, idx); + return tsvalue(stv); + } + else if (size -= 2, size <= LUAI_MAXSHORTLEN) { /* short string? */ char buff[LUAI_MAXSHORTLEN]; loadVector(S, buff, size); /* load string into buffer */ ts = luaS_newlstr(L, buff, size); /* create string */ @@ -126,6 +135,10 @@ static TString *loadStringN (LoadState *S, Proto *p) { L->top.p--; /* pop string */ } luaC_objbarrier(L, p, ts); + S->nstr++; /* add string to list of saved strings */ + setsvalue(L, &sv, ts); + luaH_setint(L, S->h, S->nstr, &sv); + luaC_objbarrierback(L, obj2gco(S->h), ts); return ts; } @@ -323,11 +336,16 @@ LClosure *luaU_undump(lua_State *L, ZIO *Z, const char *name) { cl = luaF_newLclosure(L, loadByte(&S)); setclLvalue2s(L, L->top.p, cl); luaD_inctop(L); + S.h = luaH_new(L); /* create list of saved strings */ + S.nstr = 0; + sethvalue2s(L, L->top.p, S.h); /* anchor it */ + luaD_inctop(L); cl->p = luaF_newproto(L); luaC_objbarrier(L, cl, cl->p); loadFunction(&S, cl->p, NULL); lua_assert(cl->nupvalues == cl->p->sizeupvalues); luai_verifycode(L, cl->p); + L->top.p--; /* pop table */ return cl; } diff --git a/lundump.h b/lundump.h index f3748a9980..7def905b3f 100644 --- a/lundump.h +++ b/lundump.h @@ -31,6 +31,6 @@ LUAI_FUNC LClosure* luaU_undump (lua_State* L, ZIO* Z, const char* name); /* dump one chunk; from ldump.c */ LUAI_FUNC int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, - void* data, int strip); + void* data, int strip, Table *h); #endif From 7d6a97e42bc3328b9c5ec1dabbd7e280e81c3efd Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 20 Dec 2022 11:14:52 -0300 Subject: [PATCH 0795/1145] Dump doesn't need to reuse 'source' All strings are being reused now, including 'source'. --- ldump.c | 12 ++++++------ lundump.c | 10 ++++------ testes/calls.lua | 25 +++++++++++++++++++++++++ 3 files changed, 35 insertions(+), 12 deletions(-) diff --git a/ldump.c b/ldump.c index 70c7adc61d..a99d7ec565 100644 --- a/ldump.c +++ b/ldump.c @@ -126,7 +126,7 @@ static void dumpCode (DumpState *D, const Proto *f) { } -static void dumpFunction(DumpState *D, const Proto *f, TString *psource); +static void dumpFunction(DumpState *D, const Proto *f); static void dumpConstants (DumpState *D, const Proto *f) { int i; @@ -159,7 +159,7 @@ static void dumpProtos (DumpState *D, const Proto *f) { int n = f->sizep; dumpInt(D, n); for (i = 0; i < n; i++) - dumpFunction(D, f->p[i], f->source); + dumpFunction(D, f->p[i]); } @@ -199,9 +199,9 @@ static void dumpDebug (DumpState *D, const Proto *f) { } -static void dumpFunction (DumpState *D, const Proto *f, TString *psource) { - if (D->strip || f->source == psource) - dumpString(D, NULL); /* no debug info or same source as its parent */ +static void dumpFunction (DumpState *D, const Proto *f) { + if (D->strip) + dumpString(D, NULL); /* no debug info */ else dumpString(D, f->source); dumpInt(D, f->linedefined); @@ -245,7 +245,7 @@ int luaU_dump(lua_State *L, const Proto *f, lua_Writer w, void *data, D.nstr = 0; dumpHeader(&D); dumpByte(&D, f->sizeupvalues); - dumpFunction(&D, f, NULL); + dumpFunction(&D, f); return D.status; } diff --git a/lundump.c b/lundump.c index 4048fdeae1..3bff463f4a 100644 --- a/lundump.c +++ b/lundump.c @@ -162,7 +162,7 @@ static void loadCode (LoadState *S, Proto *f) { } -static void loadFunction(LoadState *S, Proto *f, TString *psource); +static void loadFunction(LoadState *S, Proto *f); static void loadConstants (LoadState *S, Proto *f) { @@ -211,7 +211,7 @@ static void loadProtos (LoadState *S, Proto *f) { for (i = 0; i < n; i++) { f->p[i] = luaF_newproto(S->L); luaC_objbarrier(S->L, f, f->p[i]); - loadFunction(S, f->p[i], f->source); + loadFunction(S, f->p[i]); } } @@ -266,10 +266,8 @@ static void loadDebug (LoadState *S, Proto *f) { } -static void loadFunction (LoadState *S, Proto *f, TString *psource) { +static void loadFunction (LoadState *S, Proto *f) { f->source = loadStringN(S, f); - if (f->source == NULL) /* no source in dump? */ - f->source = psource; /* reuse parent's source */ f->linedefined = loadInt(S); f->lastlinedefined = loadInt(S); f->numparams = loadByte(S); @@ -342,7 +340,7 @@ LClosure *luaU_undump(lua_State *L, ZIO *Z, const char *name) { luaD_inctop(L); cl->p = luaF_newproto(L); luaC_objbarrier(L, cl, cl->p); - loadFunction(&S, cl->p, NULL); + loadFunction(&S, cl->p); lua_assert(cl->nupvalues == cl->p->sizeupvalues); luai_verifycode(L, cl->p); L->top.p--; /* pop table */ diff --git a/testes/calls.lua b/testes/calls.lua index ee8cce7333..cd2696e8b0 100644 --- a/testes/calls.lua +++ b/testes/calls.lua @@ -487,5 +487,30 @@ do end end + +do -- check reuse of strings in dumps + local str = "|" .. string.rep("X", 50) .. "|" + local foo = load(string.format([[ + local str = "%s" + return { + function () return str end, + function () return str end, + function () return str end + } + ]], str)) + -- count occurrences of 'str' inside the dump + local dump = string.dump(foo) + local _, count = string.gsub(dump, str, {}) + -- there should be only two occurrences: + -- one inside the source, other the string itself. + assert(count == 2) + + if T then -- check reuse of strings in undump + local funcs = load(dump)() + assert(string.format("%p", T.listk(funcs[1])[1]) == + string.format("%p", T.listk(funcs[3])[1])) + end +end + print('OK') return deep From 540d8052265776451bb9f0ab4dee4ec860563cbe Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 20 Dec 2022 13:24:43 -0300 Subject: [PATCH 0796/1145] Towards Lua 5.5 --- lua.h | 8 +-- manual/manual.of | 123 ++--------------------------------------------- testes/all.lua | 2 +- testes/calls.lua | 2 +- testes/main.lua | 6 +-- 5 files changed, 12 insertions(+), 129 deletions(-) diff --git a/lua.h b/lua.h index feb3dbc556..cb32ec2251 100644 --- a/lua.h +++ b/lua.h @@ -17,11 +17,11 @@ #define LUA_VERSION_MAJOR "5" -#define LUA_VERSION_MINOR "4" -#define LUA_VERSION_RELEASE "5" +#define LUA_VERSION_MINOR "5" +#define LUA_VERSION_RELEASE "0" -#define LUA_VERSION_NUM 504 -#define LUA_VERSION_RELEASE_NUM (LUA_VERSION_NUM * 100 + 5) +#define LUA_VERSION_NUM 505 +#define LUA_VERSION_RELEASE_NUM (LUA_VERSION_NUM * 100 + 0) #define LUA_VERSION "Lua " LUA_VERSION_MAJOR "." LUA_VERSION_MINOR #define LUA_RELEASE LUA_VERSION "." LUA_VERSION_RELEASE diff --git a/manual/manual.of b/manual/manual.of index 6d19e251e5..416622c16b 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -1586,7 +1586,8 @@ Each variable name may be postfixed by an attribute @producname{attrib}@producbody{@bnfopt{@bnfter{<} @bnfNter{Name} @bnfter{>}}} } There are two possible attributes: -@id{const}, which declares a @x{constant variable}, +@id{const}, which declares a @emph{constant} or @emph{read-only} variable, +@index{constant variable} that is, a variable that cannot be assigned to after its initialization; and @id{close}, which declares a to-be-closed variable @see{to-be-closed}. @@ -9118,7 +9119,7 @@ is a more portable solution. @simplesect{ Here we list the incompatibilities that you may find when moving a program -from @N{Lua 5.3} to @N{Lua 5.4}. +from @N{Lua 5.4} to @N{Lua 5.5}. You can avoid some incompatibilities by compiling Lua with appropriate options (see file @id{luaconf.h}). @@ -9155,51 +9156,6 @@ change between versions. @itemize{ @item{ -The coercion of strings to numbers in -arithmetic and bitwise operations -has been removed from the core language. -The string library does a similar job -for arithmetic (but not for bitwise) operations -using the string metamethods. -However, unlike in previous versions, -the new implementation preserves the implicit type of the numeral -in the string. -For instance, the result of @T{"1" + "2"} now is an integer, -not a float. -} - -@item{ -Literal decimal integer constants that overflow are read as floats, -instead of wrapping around. -You can use hexadecimal notation for such constants if you -want the old behavior -(reading them as integers with wrap around). -} - -@item{ -The use of the @idx{__lt} metamethod to emulate @idx{__le} -has been removed. -When needed, this metamethod must be explicitly defined. -} - -@item{ -The semantics of the numerical @Rw{for} loop -over integers changed in some details. -In particular, the control variable never wraps around. -} - -@item{ -A label for a @Rw{goto} cannot be declared where a label with the same -name is visible, even if this other label is declared in an enclosing -block. -} - -@item{ -When finalizing an object, -Lua does not ignore @idx{__gc} metamethods that are not functions. -Any value will be called, if present. -(Non-callable values will generate a warning, -like any other error when calling a finalizer.) } } @@ -9210,39 +9166,6 @@ like any other error when calling a finalizer.) @itemize{ @item{ -The function @Lid{print} does not call @Lid{tostring} -to format its arguments; -instead, it has this functionality hardwired. -You should use @idx{__tostring} to modify how values are printed. -} - -@item{ -The pseudo-random number generator used by the function @Lid{math.random} -now starts with a somewhat random seed. -Moreover, it uses a different algorithm. -} - -@item{ -By default, the decoding functions in the @Lid{utf8} library -do not accept surrogates as valid code points. -An extra parameter in these functions makes them more permissive. -} - -@item{ -The options @St{setpause} and @St{setstepmul} -of the function @Lid{collectgarbage} are deprecated. -You should use the new option @St{incremental} to set them. -} - -@item{ -The function @Lid{io.lines} now returns four values, -instead of just one. -That can be a problem when it is used as the sole -argument to another function that has optional parameters, -such as in @T{load(io.lines(filename, "L"))}. -To fix that issue, -you can wrap the call into parentheses, -to adjust its number of results to one. } } @@ -9254,46 +9177,6 @@ to adjust its number of results to one. @itemize{ @item{ -Full userdata now has an arbitrary number of associated user values. -Therefore, the functions @id{lua_newuserdata}, -@id{lua_setuservalue}, and @id{lua_getuservalue} were -replaced by @Lid{lua_newuserdatauv}, -@Lid{lua_setiuservalue}, and @Lid{lua_getiuservalue}, -which have an extra argument. - -For compatibility, the old names still work as macros assuming -one single user value. -Note, however, that userdata with zero user values -are more efficient memory-wise. -} - -@item{ -The function @Lid{lua_resume} has an extra parameter. -This out parameter returns the number of values on -the top of the stack that were yielded or returned by the coroutine. -(In previous versions, -those values were the entire stack.) -} - -@item{ -The function @Lid{lua_version} returns the version number, -instead of an address of the version number. -The Lua core should work correctly with libraries using their -own static copies of the same core, -so there is no need to check whether they are using the same -address space. -} - -@item{ -The constant @id{LUA_ERRGCMM} was removed. -Errors in finalizers are never propagated; -instead, they generate a warning. -} - -@item{ -The options @idx{LUA_GCSETPAUSE} and @idx{LUA_GCSETSTEPMUL} -of the function @Lid{lua_gc} are deprecated. -You should use the new option @id{LUA_GCINC} to set them. } } diff --git a/testes/all.lua b/testes/all.lua index a8e44024bc..279694ae7b 100644 --- a/testes/all.lua +++ b/testes/all.lua @@ -3,7 +3,7 @@ -- See Copyright Notice at the end of this file -local version = "Lua 5.4" +local version = "Lua 5.5" if _VERSION ~= version then io.stderr:write("This test suite is for ", version, ", not for ", _VERSION, "\nExiting tests") diff --git a/testes/calls.lua b/testes/calls.lua index cd2696e8b0..ea384224e3 100644 --- a/testes/calls.lua +++ b/testes/calls.lua @@ -448,7 +448,7 @@ print("testing binary chunks") do local header = string.pack("c4BBc6BBB", "\27Lua", -- signature - 0x54, -- version 5.4 (0x54) + 0x55, -- version 5.5 (0x55) 0, -- format "\x19\x93\r\n\x1a\n", -- data 4, -- size of instruction diff --git a/testes/main.lua b/testes/main.lua index 9187420ef5..9c8be580dc 100644 --- a/testes/main.lua +++ b/testes/main.lua @@ -134,7 +134,7 @@ RUN('env LUA_INIT= LUA_PATH=x lua %s > %s', prog, out) checkout("x\n") -- test LUA_PATH_version -RUN('env LUA_INIT= LUA_PATH_5_4=y LUA_PATH=x lua %s > %s', prog, out) +RUN('env LUA_INIT= LUA_PATH_5_5=y LUA_PATH=x lua %s > %s', prog, out) checkout("y\n") -- test LUA_CPATH @@ -143,7 +143,7 @@ RUN('env LUA_INIT= LUA_CPATH=xuxu lua %s > %s', prog, out) checkout("xuxu\n") -- test LUA_CPATH_version -RUN('env LUA_INIT= LUA_CPATH_5_4=yacc LUA_CPATH=x lua %s > %s', prog, out) +RUN('env LUA_INIT= LUA_CPATH_5_5=yacc LUA_CPATH=x lua %s > %s', prog, out) checkout("yacc\n") -- test LUA_INIT (and its access to 'arg' table) @@ -153,7 +153,7 @@ checkout("3.2\n") -- test LUA_INIT_version prepfile("print(X)") -RUN('env LUA_INIT_5_4="X=10" LUA_INIT="X=3" lua %s > %s', prog, out) +RUN('env LUA_INIT_5_5="X=10" LUA_INIT="X=3" lua %s > %s', prog, out) checkout("10\n") -- test LUA_INIT for files From b2f7b3b79f3117885b265575f6c5dbf934757797 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 21 Dec 2022 12:04:59 -0300 Subject: [PATCH 0797/1145] Control variables in for loops are read only --- lparser.c | 30 +++++++++++++++++++----------- manual/manual.of | 16 ++++++---------- testes/attrib.lua | 2 +- testes/closure.lua | 19 ++++++++----------- testes/nextvar.lua | 10 ++++++---- 5 files changed, 40 insertions(+), 37 deletions(-) diff --git a/lparser.c b/lparser.c index 24668c2485..79b2317b75 100644 --- a/lparser.c +++ b/lparser.c @@ -187,10 +187,10 @@ static int registerlocalvar (LexState *ls, FuncState *fs, TString *varname) { /* -** Create a new local variable with the given 'name'. Return its index -** in the function. +** Create a new local variable with the given 'name' and given 'kind'. +** Return its index in the function. */ -static int new_localvar (LexState *ls, TString *name) { +static int new_localvarkind (LexState *ls, TString *name, int kind) { lua_State *L = ls->L; FuncState *fs = ls->fs; Dyndata *dyd = ls->dyd; @@ -200,11 +200,19 @@ static int new_localvar (LexState *ls, TString *name) { luaM_growvector(L, dyd->actvar.arr, dyd->actvar.n + 1, dyd->actvar.size, Vardesc, USHRT_MAX, "local variables"); var = &dyd->actvar.arr[dyd->actvar.n++]; - var->vd.kind = VDKREG; /* default */ + var->vd.kind = kind; /* default */ var->vd.name = name; return dyd->actvar.n - 1 - fs->firstlocal; } + +/* +** Create a new local variable with the given 'name' and regular kind. +*/ +static int new_localvar (LexState *ls, TString *name) { + return new_localvarkind(ls, name, VDKREG); +} + #define new_localvarliteral(ls,v) \ new_localvar(ls, \ luaX_newstring(ls, "" v, (sizeof(v)/sizeof(char)) - 1)); @@ -1573,7 +1581,7 @@ static void fornum (LexState *ls, TString *varname, int line) { new_localvarliteral(ls, "(for state)"); new_localvarliteral(ls, "(for state)"); new_localvarliteral(ls, "(for state)"); - new_localvar(ls, varname); + new_localvarkind(ls, varname, RDKCONST); /* control variable */ checknext(ls, '='); exp1(ls); /* initial value */ checknext(ls, ','); @@ -1601,8 +1609,8 @@ static void forlist (LexState *ls, TString *indexname) { new_localvarliteral(ls, "(for state)"); new_localvarliteral(ls, "(for state)"); new_localvarliteral(ls, "(for state)"); - /* create declared variables */ - new_localvar(ls, indexname); + new_localvarkind(ls, indexname, RDKCONST); /* control variable */ + /* other declared variables */ while (testnext(ls, ',')) { new_localvar(ls, str_checkname(ls)); nvars++; @@ -1728,14 +1736,14 @@ static void localstat (LexState *ls) { FuncState *fs = ls->fs; int toclose = -1; /* index of to-be-closed variable (if any) */ Vardesc *var; /* last variable */ - int vidx, kind; /* index and kind of last variable */ + int vidx; /* index of last variable */ int nvars = 0; int nexps; expdesc e; do { - vidx = new_localvar(ls, str_checkname(ls)); - kind = getlocalattribute(ls); - getlocalvardesc(fs, vidx)->vd.kind = kind; + TString *vname = str_checkname(ls); + int kind = getlocalattribute(ls); + vidx = new_localvarkind(ls, vname, kind); if (kind == RDKTOCLOSE) { /* to-be-closed? */ if (toclose != -1) /* one already present? */ luaK_semerror(ls, "multiple to-be-closed variables in local list"); diff --git a/manual/manual.of b/manual/manual.of index 416622c16b..73d2595177 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -1467,7 +1467,7 @@ It has the following syntax: exp @bnfter{,} exp @bnfopt{@bnfter{,} exp} @Rw{do} block @Rw{end}} } The given identifier (@bnfNter{Name}) defines the control variable, -which is a new variable local to the loop body (@emph{block}). +which is a new read-only variable local to the loop body (@emph{block}). The loop starts by evaluating once the three control expressions. Their values are called respectively @@ -1499,11 +1499,6 @@ For integer loops, the control variable never wraps around; instead, the loop ends in case of an overflow. -You should not change the value of the control variable -during the loop. -If you need its value after the loop, -assign it to another variable before exiting the loop. - } @sect4{@title{The generic @Rw{for} loop} @@ -1526,7 +1521,8 @@ for @rep{var_1}, @Cdots, @rep{var_n} in @rep{explist} do @rep{body} end works as follows. The names @rep{var_i} declare loop variables local to the loop body. -The first of these variables is the @emph{control variable}. +The first of these variables is the @emph{control variable}, +which is a read-only variable. The loop starts by evaluating @rep{explist} to produce four values: @@ -1550,9 +1546,6 @@ to-be-closed variable @see{to-be-closed}, which can be used to release resources when the loop ends. Otherwise, it does not interfere with the loop. -You should not change the value of the control variable -during the loop. - } } @@ -9156,6 +9149,9 @@ change between versions. @itemize{ @item{ +The control variable in @Rw{for} loops are read only. +If you need to change it, +declare a local variable with the same name in the loop body. } } diff --git a/testes/attrib.lua b/testes/attrib.lua index 83821c069a..fc427080fb 100644 --- a/testes/attrib.lua +++ b/testes/attrib.lua @@ -236,7 +236,7 @@ package.path = oldpath local fname = "file_does_not_exist2" local m, err = pcall(require, fname) for t in string.gmatch(package.path..";"..package.cpath, "[^;]+") do - t = string.gsub(t, "?", fname) + local t = string.gsub(t, "?", fname) assert(string.find(err, t, 1, true)) end diff --git a/testes/closure.lua b/testes/closure.lua index c245367777..27ec559654 100644 --- a/testes/closure.lua +++ b/testes/closure.lua @@ -60,32 +60,29 @@ end -- testing closures with 'for' control variable a = {} for i=1,10 do - a[i] = {set = function(x) i=x end, get = function () return i end} + a[i] = function () return i end if i == 3 then break end end assert(a[4] == undef) -a[1].set(10) -assert(a[2].get() == 2) -a[2].set('a') -assert(a[3].get() == 3) -assert(a[2].get() == 'a') +assert(a[2]() == 2) +assert(a[3]() == 3) a = {} local t = {"a", "b"} for i = 1, #t do local k = t[i] - a[i] = {set = function(x, y) i=x; k=y end, + a[i] = {set = function(x) k=x end, get = function () return i, k end} if i == 2 then break end end -a[1].set(10, 20) +a[1].set(10) local r,s = a[2].get() assert(r == 2 and s == 'b') r,s = a[1].get() -assert(r == 10 and s == 20) -a[2].set('a', 'b') +assert(r == 1 and s == 10) +a[2].set('a') r,s = a[2].get() -assert(r == "a" and s == "b") +assert(r == 2 and s == "a") -- testing closures with 'for' control variable x break diff --git a/testes/nextvar.lua b/testes/nextvar.lua index 80b3d05cd6..87910ef9f4 100644 --- a/testes/nextvar.lua +++ b/testes/nextvar.lua @@ -609,10 +609,12 @@ do a = 0; for i=1.0, 0.99999, -1 do a=a+1 end; assert(a==1) end -do -- changing the control variable - local a - a = 0; for i = 1, 10 do a = a + 1; i = "x" end; assert(a == 10) - a = 0; for i = 10.0, 1, -1 do a = a + 1; i = "x" end; assert(a == 10) +do -- attempt to change the control variable + local st, msg = load "for i = 1, 10 do i = 10 end" + assert(not st and string.find(msg, "assign to const variable 'i'")) + + local st, msg = load "for v, k in pairs{} do v = 10 end" + assert(not st and string.find(msg, "assign to const variable 'v'")) end -- conversion From 873588dc5f04bfc37006c3dc6ceb9a495ea503f2 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 21 Dec 2022 15:35:40 -0300 Subject: [PATCH 0798/1145] Simplification in opcodes for numerical 'for' As the control variable is read only, the code doesn't need to keep an internal copy of it. --- lparser.c | 4 ++-- lvm.c | 50 +++++++++++++++++++++++++------------------------- 2 files changed, 27 insertions(+), 27 deletions(-) diff --git a/lparser.c b/lparser.c index 79b2317b75..ff6f400928 100644 --- a/lparser.c +++ b/lparser.c @@ -1580,7 +1580,6 @@ static void fornum (LexState *ls, TString *varname, int line) { int base = fs->freereg; new_localvarliteral(ls, "(for state)"); new_localvarliteral(ls, "(for state)"); - new_localvarliteral(ls, "(for state)"); new_localvarkind(ls, varname, RDKCONST); /* control variable */ checknext(ls, '='); exp1(ls); /* initial value */ @@ -1592,7 +1591,8 @@ static void fornum (LexState *ls, TString *varname, int line) { luaK_int(fs, fs->freereg, 1); luaK_reserveregs(fs, 1); } - adjustlocalvars(ls, 3); /* control variables */ + adjustlocalvars(ls, 2); /* start scope for internal state variables */ + fs->freereg--; /* OP_FORPREP removes one register from the stack */ forbody(ls, base, line, 1, 0); } diff --git a/lvm.c b/lvm.c index 2e84dc63c1..bdfef4346e 100644 --- a/lvm.c +++ b/lvm.c @@ -196,12 +196,15 @@ static int forlimit (lua_State *L, lua_Integer init, const TValue *lim, /* ** Prepare a numerical for loop (opcode OP_FORPREP). +** Before execution, stack is as follows: +** ra : initial value +** ra + 1 : limit +** ra + 2 : step ** Return true to skip the loop. Otherwise, ** after preparation, stack will be as follows: -** ra : internal index (safe copy of the control variable) -** ra + 1 : loop counter (integer loops) or limit (float loops) -** ra + 2 : step -** ra + 3 : control variable +** ra : loop counter (integer loops) or limit (float loops) +** ra + 1 : step +** ra + 2 : control variable */ static int forprep (lua_State *L, StkId ra) { TValue *pinit = s2v(ra); @@ -213,7 +216,6 @@ static int forprep (lua_State *L, StkId ra) { lua_Integer limit; if (step == 0) luaG_runerror(L, "'for' step is zero"); - setivalue(s2v(ra + 3), init); /* control variable */ if (forlimit(L, init, plimit, &limit, step)) return 1; /* skip the loop */ else { /* prepare loop counter */ @@ -228,9 +230,10 @@ static int forprep (lua_State *L, StkId ra) { /* 'step+1' avoids negating 'mininteger' */ count /= l_castS2U(-(step + 1)) + 1u; } - /* store the counter in place of the limit (which won't be - needed anymore) */ - setivalue(plimit, l_castU2S(count)); + /* use 'chgivalue' for places that for sure had integers */ + chgivalue(s2v(ra), l_castU2S(count)); /* change init to count */ + setivalue(s2v(ra + 1), step); /* change limit to step */ + chgivalue(s2v(ra + 2), init); /* change step to init */ } } else { /* try making all values floats */ @@ -247,11 +250,10 @@ static int forprep (lua_State *L, StkId ra) { : luai_numlt(init, limit)) return 1; /* skip the loop */ else { - /* make sure internal values are all floats */ - setfltvalue(plimit, limit); - setfltvalue(pstep, step); - setfltvalue(s2v(ra), init); /* internal index */ - setfltvalue(s2v(ra + 3), init); /* control variable */ + /* make sure all values are floats */ + setfltvalue(s2v(ra), limit); + setfltvalue(s2v(ra + 1), step); + setfltvalue(s2v(ra + 2), init); /* control variable */ } } return 0; @@ -264,14 +266,13 @@ static int forprep (lua_State *L, StkId ra) { ** written online with opcode OP_FORLOOP, for performance.) */ static int floatforloop (StkId ra) { - lua_Number step = fltvalue(s2v(ra + 2)); - lua_Number limit = fltvalue(s2v(ra + 1)); - lua_Number idx = fltvalue(s2v(ra)); /* internal index */ + lua_Number step = fltvalue(s2v(ra + 1)); + lua_Number limit = fltvalue(s2v(ra)); + lua_Number idx = fltvalue(s2v(ra + 2)); /* control variable */ idx = luai_numadd(L, idx, step); /* increment index */ if (luai_numlt(0, step) ? luai_numle(idx, limit) : luai_numle(limit, idx)) { - chgfltvalue(s2v(ra), idx); /* update internal index */ - setfltvalue(s2v(ra + 3), idx); /* and control variable */ + chgfltvalue(s2v(ra + 2), idx); /* update control variable */ return 1; /* jump back */ } else @@ -1781,15 +1782,14 @@ void luaV_execute (lua_State *L, CallInfo *ci) { } vmcase(OP_FORLOOP) { StkId ra = RA(i); - if (ttisinteger(s2v(ra + 2))) { /* integer loop? */ - lua_Unsigned count = l_castS2U(ivalue(s2v(ra + 1))); + if (ttisinteger(s2v(ra + 1))) { /* integer loop? */ + lua_Unsigned count = l_castS2U(ivalue(s2v(ra))); if (count > 0) { /* still more iterations? */ - lua_Integer step = ivalue(s2v(ra + 2)); - lua_Integer idx = ivalue(s2v(ra)); /* internal index */ - chgivalue(s2v(ra + 1), count - 1); /* update counter */ + lua_Integer step = ivalue(s2v(ra + 1)); + lua_Integer idx = ivalue(s2v(ra + 2)); /* control variable */ + chgivalue(s2v(ra), count - 1); /* update counter */ idx = intop(+, idx, step); /* add step to index */ - chgivalue(s2v(ra), idx); /* update internal index */ - setivalue(s2v(ra + 3), idx); /* and control variable */ + chgivalue(s2v(ra + 2), idx); /* update control variable */ pc -= GETARG_Bx(i); /* jump back */ } } From 7d4c7ae2af686df4a9f3cc0c6110986d886d55e6 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 22 Dec 2022 13:52:53 -0300 Subject: [PATCH 0799/1145] Changes in opcodes for generic 'for' Again, as the control variable is read only, the code doesn't need to keep an internal copy of it. --- lparser.c | 23 +++++++++++------------ lvm.c | 40 +++++++++++++++++++++++++--------------- testes/files.lua | 4 ++-- 3 files changed, 38 insertions(+), 29 deletions(-) diff --git a/lparser.c b/lparser.c index ff6f400928..c0eb789064 100644 --- a/lparser.c +++ b/lparser.c @@ -1558,6 +1558,7 @@ static void forbody (LexState *ls, int base, int line, int nvars, int isgen) { int prep, endfor; checknext(ls, TK_DO); prep = luaK_codeABx(fs, forprep[isgen], base, 0); + fs->freereg--; /* both 'forprep' remove one register from the stack */ enterblock(fs, &bl, 0); /* scope for declared variables */ adjustlocalvars(ls, nvars); luaK_reserveregs(fs, nvars); @@ -1591,8 +1592,7 @@ static void fornum (LexState *ls, TString *varname, int line) { luaK_int(fs, fs->freereg, 1); luaK_reserveregs(fs, 1); } - adjustlocalvars(ls, 2); /* start scope for internal state variables */ - fs->freereg--; /* OP_FORPREP removes one register from the stack */ + adjustlocalvars(ls, 2); /* start scope for internal variables */ forbody(ls, base, line, 1, 0); } @@ -1601,14 +1601,13 @@ static void forlist (LexState *ls, TString *indexname) { /* forlist -> NAME {,NAME} IN explist forbody */ FuncState *fs = ls->fs; expdesc e; - int nvars = 5; /* gen, state, control, toclose, 'indexname' */ + int nvars = 4; /* function, state, closing, control */ int line; int base = fs->freereg; - /* create control variables */ - new_localvarliteral(ls, "(for state)"); - new_localvarliteral(ls, "(for state)"); - new_localvarliteral(ls, "(for state)"); - new_localvarliteral(ls, "(for state)"); + /* create internal variables */ + new_localvarliteral(ls, "(for state)"); /* iterator function */ + new_localvarliteral(ls, "(for state)"); /* state */ + new_localvarliteral(ls, "(for state)"); /* closing var. (after swap) */ new_localvarkind(ls, indexname, RDKCONST); /* control variable */ /* other declared variables */ while (testnext(ls, ',')) { @@ -1618,10 +1617,10 @@ static void forlist (LexState *ls, TString *indexname) { checknext(ls, TK_IN); line = ls->linenumber; adjust_assign(ls, 4, explist(ls, &e), &e); - adjustlocalvars(ls, 4); /* control variables */ - marktobeclosed(fs); /* last control var. must be closed */ - luaK_checkstack(fs, 3); /* extra space to call generator */ - forbody(ls, base, line, nvars - 4, 1); + adjustlocalvars(ls, 3); /* start scope for internal variables */ + marktobeclosed(fs); /* last internal var. must be closed */ + luaK_checkstack(fs, 2); /* extra space to call iterator */ + forbody(ls, base, line, nvars - 3, 1); } diff --git a/lvm.c b/lvm.c index bdfef4346e..8ab8753da2 100644 --- a/lvm.c +++ b/lvm.c @@ -1806,26 +1806,38 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_TFORPREP) { + /* before: 'ra' has the iterator function, 'ra + 1' has the state, + 'ra + 2' has the initial value for the control variable, and + 'ra + 3' has the closing variable. This opcode then swaps the + control and the closing variables and marks the closing variable + as to-be-closed. + */ StkId ra = RA(i); - /* create to-be-closed upvalue (if needed) */ - halfProtect(luaF_newtbcupval(L, ra + 3)); - pc += GETARG_Bx(i); - i = *(pc++); /* go to next instruction */ + TValue temp; /* to swap control and closing variables */ + setobj(L, &temp, s2v(ra + 3)); + setobjs2s(L, ra + 3, ra + 2); + setobj2s(L, ra + 2, &temp); + /* create to-be-closed upvalue (if closing var. is not nil) */ + halfProtect(luaF_newtbcupval(L, ra + 2)); + pc += GETARG_Bx(i); /* go to end of the loop */ + i = *(pc++); /* fetch next instruction */ lua_assert(GET_OPCODE(i) == OP_TFORCALL && ra == RA(i)); goto l_tforcall; } vmcase(OP_TFORCALL) { l_tforcall: { - StkId ra = RA(i); /* 'ra' has the iterator function, 'ra + 1' has the state, - 'ra + 2' has the control variable, and 'ra + 3' has the - to-be-closed variable. The call will use the stack after - these values (starting at 'ra + 4') + 'ra + 2' has the closing variable, and 'ra + 3' has the control + variable. The call will use the stack starting at 'ra + 3', + so that it preserves the first three values, and the first + return will be the new value for the control variable. */ - /* push function, state, and control variable */ - memcpy(ra + 4, ra, 3 * sizeof(*ra)); - L->top.p = ra + 4 + 3; - ProtectNT(luaD_call(L, ra + 4, GETARG_C(i))); /* do the call */ + StkId ra = RA(i); + setobjs2s(L, ra + 5, ra + 3); /* copy the control variable */ + setobjs2s(L, ra + 4, ra + 1); /* copy state */ + setobjs2s(L, ra + 3, ra); /* copy function */ + L->top.p = ra + 3 + 3; + ProtectNT(luaD_call(L, ra + 3, GETARG_C(i))); /* do the call */ updatestack(ci); /* stack may have changed */ i = *(pc++); /* go to next instruction */ lua_assert(GET_OPCODE(i) == OP_TFORLOOP && ra == RA(i)); @@ -1834,10 +1846,8 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmcase(OP_TFORLOOP) { l_tforloop: { StkId ra = RA(i); - if (!ttisnil(s2v(ra + 4))) { /* continue loop? */ - setobjs2s(L, ra + 2, ra + 4); /* save control variable */ + if (!ttisnil(s2v(ra + 3))) /* continue loop? */ pc -= GETARG_Bx(i); /* jump back */ - } vmbreak; }} vmcase(OP_SETLIST) { diff --git a/testes/files.lua b/testes/files.lua index 78f962e5a0..599644f32b 100644 --- a/testes/files.lua +++ b/testes/files.lua @@ -427,12 +427,12 @@ do -- testing closing file in line iteration -- get the to-be-closed variable from a loop local function gettoclose (lv) lv = lv + 1 - local stvar = 0 -- to-be-closed is 4th state variable in the loop + local stvar = 0 -- to-be-closed is 3th state variable in the loop for i = 1, 1000 do local n, v = debug.getlocal(lv, i) if n == "(for state)" then stvar = stvar + 1 - if stvar == 4 then return v end + if stvar == 3 then return v end end end end From 0825cf237d9d3505155f8b40bcf83ea1b135e8da Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 23 Dec 2022 11:28:11 -0300 Subject: [PATCH 0800/1145] Detail in make file for testes/libs Everything depends on the Lua version (as given by 'lua.h') --- testes/libs/makefile | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/testes/libs/makefile b/testes/libs/makefile index a133092084..9c0c4e3f7f 100644 --- a/testes/libs/makefile +++ b/testes/libs/makefile @@ -11,17 +11,17 @@ CFLAGS = -Wall -std=gnu99 -O2 -I$(LUA_DIR) -fPIC -shared all: lib1.so lib11.so lib2.so lib21.so lib2-v2.so touch all -lib1.so: lib1.c $(LUA_DIR)/luaconf.h +lib1.so: lib1.c $(LUA_DIR)/luaconf.h $(LUA_DIR)/lua.h $(CC) $(CFLAGS) -o lib1.so lib1.c -lib11.so: lib11.c $(LUA_DIR)/luaconf.h +lib11.so: lib11.c $(LUA_DIR)/luaconf.h $(LUA_DIR)/lua.h $(CC) $(CFLAGS) -o lib11.so lib11.c -lib2.so: lib2.c $(LUA_DIR)/luaconf.h +lib2.so: lib2.c $(LUA_DIR)/luaconf.h $(LUA_DIR)/lua.h $(CC) $(CFLAGS) -o lib2.so lib2.c -lib21.so: lib21.c $(LUA_DIR)/luaconf.h +lib21.so: lib21.c $(LUA_DIR)/luaconf.h $(LUA_DIR)/lua.h $(CC) $(CFLAGS) -o lib21.so lib21.c -lib2-v2.so: lib21.c $(LUA_DIR)/luaconf.h +lib2-v2.so: lib21.c $(LUA_DIR)/luaconf.h $(LUA_DIR)/lua.h $(CC) $(CFLAGS) -o lib2-v2.so lib22.c From 314745ed8438d1276c6c928d5f9d4be018dfadb6 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 28 Dec 2022 18:34:11 -0300 Subject: [PATCH 0801/1145] Avoid excessive name pollution in test files Test files are more polite regarding the use of globals when locals would do, and when globals are necessary deleting them after use. --- testes/all.lua | 1 + testes/api.lua | 112 +++++++++++++++++++++++------------------- testes/attrib.lua | 24 ++++----- testes/big.lua | 2 +- testes/calls.lua | 44 ++++++++++------- testes/closure.lua | 6 ++- testes/code.lua | 6 +-- testes/constructs.lua | 36 ++++++++------ testes/coroutine.lua | 56 +++++++++++++-------- testes/db.lua | 27 ++++++---- testes/errors.lua | 83 +++++++++++++++++-------------- testes/events.lua | 3 ++ testes/files.lua | 6 ++- testes/gc.lua | 42 +++++++++------- testes/literals.lua | 33 +++++++------ testes/locals.lua | 8 +-- testes/main.lua | 4 +- testes/math.lua | 6 +-- testes/nextvar.lua | 2 +- testes/pm.lua | 32 ++++++------ testes/sort.lua | 23 ++++----- testes/strings.lua | 6 +-- testes/tpack.lua | 2 +- testes/utf8.lua | 2 +- testes/vararg.lua | 18 +++---- testes/verybig.lua | 12 ++--- 26 files changed, 335 insertions(+), 261 deletions(-) diff --git a/testes/all.lua b/testes/all.lua index a8e44024bc..5df0ff9bca 100644 --- a/testes/all.lua +++ b/testes/all.lua @@ -163,6 +163,7 @@ f() dofile('db.lua') assert(dofile('calls.lua') == deep and deep) +_G.deep = nil olddofile('strings.lua') olddofile('literals.lua') dofile('tpack.lua') diff --git a/testes/api.lua b/testes/api.lua index bd85a923c8..752ff18ff3 100644 --- a/testes/api.lua +++ b/testes/api.lua @@ -14,7 +14,7 @@ local pack = table.pack -- standard error message for memory errors local MEMERRMSG = "not enough memory" -function tcheck (t1, t2) +local function tcheck (t1, t2) assert(t1.n == (t2.n or #t2) + 1) for i = 2, t1.n do assert(t1[i] == t2[i - 1]) end end @@ -28,7 +28,7 @@ end print('testing C API') -a = T.testC("pushvalue R; return 1") +local a = T.testC("pushvalue R; return 1") assert(a == debug.getregistry()) @@ -43,10 +43,10 @@ a = T.d2s(12458954321123.0) assert(a == string.pack("d", 12458954321123.0)) assert(T.s2d(a) == 12458954321123.0) -a,b,c = T.testC("pushnum 1; pushnum 2; pushnum 3; return 2") +local a,b,c = T.testC("pushnum 1; pushnum 2; pushnum 3; return 2") assert(a == 2 and b == 3 and not c) -f = T.makeCfunc("pushnum 1; pushnum 2; pushnum 3; return 2") +local f = T.makeCfunc("pushnum 1; pushnum 2; pushnum 3; return 2") a,b,c = f() assert(a == 2 and b == 3 and not c) @@ -61,7 +61,7 @@ assert(a==false and b==true and c==false) a,b,c = T.testC("gettop; return 2", 10, 20, 30, 40) assert(a == 40 and b == 5 and not c) -t = pack(T.testC("settop 5; return *", 2, 3)) +local t = pack(T.testC("settop 5; return *", 2, 3)) tcheck(t, {n=4,2,3}) t = pack(T.testC("settop 0; settop 15; return 10", 3, 1, 23)) @@ -166,16 +166,17 @@ end -- testing globals -_G.a = 14; _G.b = "a31" +_G.AA = 14; _G.BB = "a31" local a = {T.testC[[ - getglobal a; - getglobal b; - getglobal b; - setglobal a; + getglobal AA; + getglobal BB; + getglobal BB; + setglobal AA; return * ]]} -assert(a[2] == 14 and a[3] == "a31" and a[4] == nil and _G.a == "a31") +assert(a[2] == 14 and a[3] == "a31" and a[4] == nil and _G.AA == "a31") +_G.AA, _G.BB = nil -- testing arith assert(T.testC("pushnum 10; pushnum 20; arith /; return 1") == 0.5) @@ -199,13 +200,14 @@ a,b,c = T.testC([[pushnum 1; pushstring 10; arith _; pushstring 5; return 3]]) assert(a == 1 and b == -10 and c == "5") -mt = {__add = function (a,b) return setmetatable({a[1] + b[1]}, mt) end, +local mt = { + __add = function (a,b) return setmetatable({a[1] + b[1]}, mt) end, __mod = function (a,b) return setmetatable({a[1] % b[1]}, mt) end, __unm = function (a) return setmetatable({a[1]* 2}, mt) end} a,b,c = setmetatable({4}, mt), setmetatable({8}, mt), setmetatable({-3}, mt) -x,y,z = T.testC("arith +; return 2", 10, a, b) +local x,y,z = T.testC("arith +; return 2", 10, a, b) assert(x == 10 and y[1] == 12 and z == nil) assert(T.testC("arith %; return 1", a, c)[1] == 4%-3) assert(T.testC("arith _; arith +; arith %; return 1", b, a, c)[1] == @@ -312,9 +314,9 @@ assert(T.testC("concat 1; return 1", "xuxu") == "xuxu") -- testing lua_is -function B(x) return x and 1 or 0 end +local function B (x) return x and 1 or 0 end -function count (x, n) +local function count (x, n) n = n or 2 local prog = [[ isnumber %d; @@ -345,7 +347,7 @@ assert(count(nil, 15) == 100) -- testing lua_to... -function to (s, x, n) +local function to (s, x, n) n = n or 2 return T.testC(string.format("%s %d; return 1", s, n), x) end @@ -486,11 +488,12 @@ a = T.testC([[ pushvalue 3; insert -2; pcall 1 1 0; pcall 0 0 0; return 1 -]], "x=150", function (a) assert(a==nil); return 3 end) +]], "XX=150", function (a) assert(a==nil); return 3 end) -assert(type(a) == 'string' and x == 150) +assert(type(a) == 'string' and XX == 150) +_G.XX = nil -function check3(p, ...) +local function check3(p, ...) local arg = {...} assert(#arg == 3) assert(string.find(arg[3], p)) @@ -500,7 +503,7 @@ check3("%.", T.testC("loadfile 2; return *", ".")) check3("xxxx", T.testC("loadfile 2; return *", "xxxx")) -- test errors in non protected threads -function checkerrnopro (code, msg) +local function checkerrnopro (code, msg) local th = coroutine.create(function () end) -- create new thread local stt, err = pcall(T.testC, th, code) -- run code there assert(not stt and string.find(err, msg)) @@ -510,8 +513,9 @@ if not _soft then collectgarbage("stop") -- avoid __gc with full stack checkerrnopro("pushnum 3; call 0 0", "attempt to call") print"testing stack overflow in unprotected thread" - function f () f() end - checkerrnopro("getglobal 'f'; call 0 0;", "stack overflow") + function F () F() end + checkerrnopro("getglobal 'F'; call 0 0;", "stack overflow") + F = nil collectgarbage("restart") end print"+" @@ -588,7 +592,7 @@ assert(a[a] == "x") b = setmetatable({p = a}, {}) getmetatable(b).__index = function (t, i) return t.p[i] end -k, x = T.testC("gettable 3, return 2", 4, b, 20, 35, "x") +local k, x = T.testC("gettable 3, return 2", 4, b, 20, 35, "x") assert(x == 15 and k == 35) k = T.testC("getfield 2 y, return 1", b) assert(k == 12) @@ -748,8 +752,8 @@ local i = T.ref{} T.unref(i) assert(T.ref{} == i) -Arr = {} -Lim = 100 +local Arr = {} +local Lim = 100 for i=1,Lim do -- lock many objects Arr[i] = T.ref({}) end @@ -761,7 +765,7 @@ for i=1,Lim do -- unlock all them T.unref(Arr[i]) end -function printlocks () +local function printlocks () local f = T.makeCfunc("gettable R; return 1") local n = f("n") print("n", n) @@ -793,8 +797,8 @@ assert(type(T.getref(a)) == 'table') -- colect in cl the `val' of all collected userdata -tt = {} -cl = {n=0} +local tt = {} +local cl = {n=0} A = nil; B = nil local F F = function (x) @@ -817,6 +821,7 @@ F = function (x) end tt.__gc = F + -- test whether udate collection frees memory in the right time do collectgarbage(); @@ -853,9 +858,9 @@ end collectgarbage("stop") -- create 3 userdatas with tag `tt' -a = T.newuserdata(0); debug.setmetatable(a, tt); na = T.udataval(a) -b = T.newuserdata(0); debug.setmetatable(b, tt); nb = T.udataval(b) -c = T.newuserdata(0); debug.setmetatable(c, tt); nc = T.udataval(c) +a = T.newuserdata(0); debug.setmetatable(a, tt); local na = T.udataval(a) +b = T.newuserdata(0); debug.setmetatable(b, tt); local nb = T.udataval(b) +c = T.newuserdata(0); debug.setmetatable(c, tt); local nc = T.udataval(c) -- create userdata without meta table x = T.newuserdata(4) @@ -866,9 +871,9 @@ checkerr("FILE%* expected, got userdata", io.input, x) assert(debug.getmetatable(x) == nil and debug.getmetatable(y) == nil) -d=T.ref(a); -e=T.ref(b); -f=T.ref(c); +local d = T.ref(a); +local e = T.ref(b); +local f = T.ref(c); t = {T.getref(d), T.getref(e), T.getref(f)} assert(t[1] == a and t[2] == b and t[3] == c) @@ -888,7 +893,7 @@ tt=nil -- frees tt for GC A = nil b = nil T.unref(d); -n5 = T.newuserdata(0) +local n5 = T.newuserdata(0) debug.setmetatable(n5, {__gc=F}) n5 = T.udataval(n5) collectgarbage() @@ -959,11 +964,11 @@ print'+' -- testing changing hooks during hooks -_G.t = {} +_G.TT = {} T.sethook([[ # set a line hook after 3 count hooks sethook 4 0 ' - getglobal t; + getglobal TT; pushvalue -3; append -2 pushvalue -2; append -2 ']], "c", 3) @@ -973,12 +978,13 @@ a = 1 -- count hook (set line hook) a = 1 -- line hook a = 1 -- line hook debug.sethook() -t = _G.t +local t = _G.TT assert(t[1] == "line") -line = t[2] +local line = t[2] assert(t[3] == "line" and t[4] == line + 1) assert(t[5] == "line" and t[6] == line + 2) assert(t[7] == nil) +_G.TT = nil ------------------------------------------------------------------------- @@ -1003,6 +1009,7 @@ do -- testing errors during GC collectgarbage("restart") warn("@on") end +_G.A = nil ------------------------------------------------------------------------- -- test for userdata vals do @@ -1032,8 +1039,8 @@ assert(a == 'alo' and b == '3') T.doremote(L1, "_ERRORMESSAGE = nil") -- error: `sin' is not defined -a, _, b = T.doremote(L1, "return sin(1)") -assert(a == nil and b == 2) -- 2 == run-time error +a, b, c = T.doremote(L1, "return sin(1)") +assert(a == nil and c == 2) -- 2 == run-time error -- error: syntax error a, b, c = T.doremote(L1, "return a+") @@ -1204,7 +1211,7 @@ T.alloccount() -- remove limit -- o that we get memory errors in all allocations of a given -- task, until there is enough memory to complete the task without -- errors. -function testbytes (s, f) +local function testbytes (s, f) collectgarbage() local M = T.totalmem() local oldM = M @@ -1229,7 +1236,7 @@ end -- task, until there is enough allocations to complete the task without -- errors. -function testalloc (s, f) +local function testalloc (s, f) collectgarbage() local M = 0 local a,b = nil @@ -1296,12 +1303,12 @@ end) -- testing threads -- get main thread from registry (at index LUA_RIDX_MAINTHREAD == 1) -mt = T.testC("rawgeti R 1; return 1") +local mt = T.testC("rawgeti R 1; return 1") assert(type(mt) == "thread" and coroutine.running() == mt) -function expand (n,s) +local function expand (n,s) if n==0 then return "" end local e = string.rep("=", n) return string.format("T.doonnewstack([%s[ %s;\n collectgarbage(); %s]%s])\n", @@ -1311,9 +1318,10 @@ end G=0; collectgarbage(); a =collectgarbage("count") load(expand(20,"G=G+1"))() assert(G==20); collectgarbage(); -- assert(gcinfo() <= a+1) +G = nil testamem("running code on new thread", function () - return T.doonnewstack("x=1") == 0 -- try to create thread + return T.doonnewstack("local x=1") == 0 -- try to create thread end) @@ -1327,13 +1335,13 @@ end) local testprog = [[ local function foo () return end local t = {"x"} -a = "aaa" -for i = 1, #t do a=a..t[i] end +AA = "aaa" +for i = 1, #t do AA = AA .. t[i] end return true ]] -- testing memory x dofile -_G.a = nil +_G.AA = nil local t =os.tmpname() local f = assert(io.open(t, "w")) f:write(testprog) @@ -1343,7 +1351,7 @@ testamem("dofile", function () return a and a() end) assert(os.remove(t)) -assert(_G.a == "aaax") +assert(_G.AA == "aaax") -- other generic tests @@ -1360,6 +1368,8 @@ testamem("dump/undump", function () return a and a() end) +_G.AA = nil + local t = os.tmpname() testamem("file creation", function () local f = assert(io.open(t, 'w')) @@ -1381,7 +1391,7 @@ testamem("constructors", function () end) local a = 1 -close = nil +local close = nil testamem("closure creation", function () function close (b) return function (x) return b + x end diff --git a/testes/attrib.lua b/testes/attrib.lua index 83821c069a..458488a872 100644 --- a/testes/attrib.lua +++ b/testes/attrib.lua @@ -85,7 +85,7 @@ local DIR = "libs" .. dirsep -- prepend DIR to a name and correct directory separators local function D (x) - x = string.gsub(x, "/", dirsep) + local x = string.gsub(x, "/", dirsep) return DIR .. x end @@ -106,7 +106,7 @@ local function createfiles (files, preextras, posextras) end end -function removefiles (files) +local function removefiles (files) for n in pairs(files) do os.remove(D(n)) end @@ -154,10 +154,9 @@ local try = function (p, n, r, ext) assert(ext == x) end -a = require"names" +local a = require"names" assert(a[1] == "names" and a[2] == D"names.lua") -_G.a = nil local st, msg = pcall(require, "err") assert(not st and string.find(msg, "arithmetic") and B == 15) st, msg = pcall(require, "synerr") @@ -191,6 +190,7 @@ try("X", "XXxX", AA, "libs/XXxX") removefiles(files) +NAME, REQUIRED, AA, B = nil -- testing require of sub-packages @@ -223,7 +223,7 @@ assert(require"P1" == m and m.AA == 10) removefiles(files) - +AA = nil package.path = "" assert(not pcall(require, "file_does_not_exist")) @@ -305,6 +305,7 @@ else assert(_ENV.x == "lib1.sub" and _ENV.y == DC"lib1") assert(string.find(ext, "libs/lib1", 1, true)) assert(fs.id(45) == 45) + _ENV.x, _ENV.y = nil end _ENV = _G @@ -338,10 +339,10 @@ print("testing assignments, logical operators, and constructors") local res, res2 = 27 -a, b = 1, 2+3 +local a, b = 1, 2+3 assert(a==1 and b==5) a={} -function f() return 10, 11, 12 end +local function f() return 10, 11, 12 end a.x, b, a[1] = 1, 2, f() assert(a.x==1 and b==2 and a[1]==10) a[f()], b, a[f()+3] = f(), a, 'x' @@ -353,15 +354,15 @@ do local a,b,c a,b = 0, f(1) assert(a == 0 and b == 1) - A,b = 0, f(1) - assert(A == 0 and b == 1) + a,b = 0, f(1) + assert(a == 0 and b == 1) a,b,c = 0,5,f(4) assert(a==0 and b==5 and c==1) a,b,c = 0,5,f(0) assert(a==0 and b==5 and c==nil) end -a, b, c, d = 1 and nil, 1 or nil, (1 and (nil or 1)), 6 +local a, b, c, d = 1 and nil, 1 or nil, (1 and (nil or 1)), 6 assert(not a and b and c and d==6) d = 20 @@ -419,6 +420,7 @@ assert(not pcall(function () local a = {[nil] = 10} end)) assert(a[nil] == undef) a = nil +local a, b, c a = {10,9,8,7,6,5,4,3,2; [-3]='a', [f]=print, a='a', b='ab'} a, a.x, a.y = a, a[-3] assert(a[1]==10 and a[-3]==a.a and a[f]==print and a.x=='a' and not a.y) @@ -455,7 +457,7 @@ while maxint ~= (maxint + 0.0) or (maxint - 1) ~= (maxint - 1.0) do maxint = maxint // 2 end -maxintF = maxint + 0.0 -- float version +local maxintF = maxint + 0.0 -- float version assert(maxintF == maxint and math.type(maxintF) == "float" and maxintF >= 2.0^14) diff --git a/testes/big.lua b/testes/big.lua index 39e293ef19..46fd846674 100644 --- a/testes/big.lua +++ b/testes/big.lua @@ -32,7 +32,7 @@ setmetatable(env, { }) X = nil -co = coroutine.wrap(f) +local co = coroutine.wrap(f) assert(co() == 's') assert(co() == 'g') assert(co() == 'g') diff --git a/testes/calls.lua b/testes/calls.lua index ee8cce7333..a19385843b 100644 --- a/testes/calls.lua +++ b/testes/calls.lua @@ -16,7 +16,7 @@ assert(type(nil) == 'nil' and type(type) == 'function') assert(type(assert) == type(print)) -function f (x) return a:x (x) end +local function f (x) return a:x (x) end assert(type(f) == 'function') assert(not pcall(type)) @@ -33,10 +33,11 @@ do assert(fact(5) == 120) end assert(fact == false) +fact = nil -- testing declarations -a = {i = 10} -self = 20 +local a = {i = 10} +local self = 20 function a:x (x) return x+self.i end function a.y (x) return x+self end @@ -72,6 +73,8 @@ f(1,2, -- this one too 3,4) assert(t[1] == 1 and t[2] == 2 and t[3] == 3 and t[4] == 'a') +t = nil -- delete 't' + function fat(x) if x <= 1 then return 1 else return x*load("return fat(" .. x-1 .. ")", "")() @@ -80,26 +83,29 @@ end assert(load "load 'assert(fat(6)==720)' () ")() a = load('return fat(5), 3') -a,b = a() +local a,b = a() assert(a == 120 and b == 3) +fat = nil print('+') -function err_on_n (n) +local function err_on_n (n) if n==0 then error(); exit(1); else err_on_n (n-1); exit(1); end end do - function dummy (n) + local function dummy (n) if n > 0 then assert(not pcall(err_on_n, n)) dummy(n-1) end end + + dummy(10) end -dummy(10) +_G.deep = nil -- "declaration" (used by 'all.lua') function deep (n) if n>0 then deep(n-1) end @@ -209,7 +215,7 @@ assert(a == 23 and (function (x) return x*2 end)(20) == 40) -- testing closures -- fixed-point operator -Z = function (le) +local Z = function (le) local function a (f) return le(function (x) return f(f)(x) end) end @@ -219,14 +225,14 @@ Z = function (le) -- non-recursive factorial -F = function (f) +local F = function (f) return function (n) if n == 0 then return 1 else return n*f(n-1) end end end -fat = Z(F) +local fat = Z(F) assert(fat(0) == 1 and fat(4) == 24 and Z(F)(5)==5*Z(F)(4)) @@ -237,22 +243,21 @@ local function g (z) return f(z,z+1,z+2,z+3) end -f = g(10) +local f = g(10) assert(f(9, 16) == 10+11+12+13+10+9+16+10) -Z, F, f = nil print('+') -- testing multiple returns -function unlpack (t, i) +local function unlpack (t, i) i = i or 1 if (i <= #t) then return t[i], unlpack(t, i+1) end end -function equaltab (t1, t2) +local function equaltab (t1, t2) assert(#t1 == #t2) for i = 1, #t1 do assert(t1[i] == t2[i]) @@ -261,8 +266,8 @@ end local pack = function (...) return (table.pack(...)) end -function f() return 1,2,30,4 end -function ret2 (a,b) return a,b end +local function f() return 1,2,30,4 end +local function ret2 (a,b) return a,b end local a,b,c,d = unlpack{1,2,3} assert(a==1 and b==2 and c==3 and d==nil) @@ -291,7 +296,7 @@ table.sort({10,9,8,4,19,23,0,0}, function (a,b) return ay) and x or y == 2); x,y=2,1; assert((x>y) and x or y == 2); @@ -77,13 +78,13 @@ do -- testing operators with diffent kinds of constants local gab = f(o1, o2) _ENV.XX = o1 - code = string.format("return XX %s %s", op, o2) - res = assert(load(code))() + local code = string.format("return XX %s %s", op, o2) + local res = assert(load(code))() assert(res == gab) _ENV.XX = o2 - local code = string.format("return (%s) %s XX", o1, op) - local res = assert(load(code))() + code = string.format("return (%s) %s XX", o1, op) + res = assert(load(code))() assert(res == gab) code = string.format("return (%s) %s %s", o1, op, o2) @@ -92,6 +93,7 @@ do -- testing operators with diffent kinds of constants end end end + _ENV.XX = nil end @@ -100,7 +102,7 @@ repeat until 1; repeat until true; while false do end; while nil do end; do -- test old bug (first name could not be an `upvalue') - local a; function f(x) x={a=1}; x={x=1}; x={G=1} end + local a; local function f(x) x={a=1}; x={x=1}; x={G=1} end end @@ -128,7 +130,7 @@ do -- bug since 5.4.0 end -function f (i) +local function f (i) if type(i) ~= 'number' then return i,'jojo'; end; if i > 0 then return i, f(i-1); end; end @@ -154,10 +156,10 @@ end assert(f(3) == 'a' and f(12) == 'b' and f(26) == 'c' and f(100) == nil) for i=1,1000 do break; end; -n=100; -i=3; -t = {}; -a=nil +local n=100; +local i=3; +local t = {}; +local a=nil while not a do a=0; for i=1,n do for i=i,1,-1 do a=a+1; t[i]=1; end; end; end @@ -200,14 +202,14 @@ a={y=1} x = {a.y} assert(x[1] == 1) -function f(i) +local function f (i) while 1 do if i>0 then i=i-1; else return; end; end; end; -function g(i) +local function g(i) while 1 do if i>0 then i=i-1 else return end @@ -272,7 +274,7 @@ function g (a,b,c,d,e) if not (a>=b or c or d and e or nil) then return 0; else return 1; end; end -function h (a,b,c,d,e) +local function h (a,b,c,d,e) while (a>=b or c or (d and e) or nil) do return 1; end; return 0; end; @@ -300,7 +302,7 @@ do assert(a==2) end -function F(a) +local function F (a) assert(debug.getinfo(1, "n").name == 'F') return a,2,3 end @@ -393,6 +395,8 @@ for n = 1, level do if i % 60000 == 0 then print('+') end end end +IX = nil +_G.GLOB1 = nil ------------------------------------------------------------------ -- testing some syntax errors (chosen through 'gcov') diff --git a/testes/coroutine.lua b/testes/coroutine.lua index 15fccc3083..de7e46fbd3 100644 --- a/testes/coroutine.lua +++ b/testes/coroutine.lua @@ -30,7 +30,8 @@ local function eqtab (t1, t2) end _G.x = nil -- declare x -function foo (a, ...) +_G.f = nil -- declare f +local function foo (a, ...) local x, y = coroutine.running() assert(x == f and y == false) -- next call should not corrupt coroutine (but must fail, @@ -67,10 +68,11 @@ assert(coroutine.status(f) == "dead") s, a = coroutine.resume(f, "xuxu") assert(not s and string.find(a, "dead") and coroutine.status(f) == "dead") +_G.f = nil -- yields in tail calls local function foo (i) return coroutine.yield(i) end -f = coroutine.wrap(function () +local f = coroutine.wrap(function () for i=1,10 do assert(foo(i) == _G.x) end @@ -79,8 +81,10 @@ end) for i=1,10 do _G.x = i; assert(f(i) == i) end _G.x = 'xuxu'; assert(f('xuxu') == 'a') +_G.x = nil + -- recursive -function pf (n, i) +local function pf (n, i) coroutine.yield(n) pf(n*i, i+1) end @@ -93,14 +97,14 @@ for i=1,10 do end -- sieve -function gen (n) +local function gen (n) return coroutine.wrap(function () for i=2,n do coroutine.yield(i) end end) end -function filter (p, g) +local function filter (p, g) return coroutine.wrap(function () while 1 do local n = g() @@ -221,14 +225,14 @@ do -- versus pcall in coroutines local X = false local Y = false - function foo () + local function foo () local x = func2close(function (self, err) Y = debug.getinfo(2) X = err end) error(43) end - co = coroutine.create(function () return pcall(foo) end) + local co = coroutine.create(function () return pcall(foo) end) local st1, st2, err = coroutine.resume(co) assert(st1 and not st2 and err == 43) assert(X == 43 and Y.what == "C") @@ -275,7 +279,7 @@ end -- yielding across C boundaries -co = coroutine.wrap(function() +local co = coroutine.wrap(function() assert(not pcall(table.sort,{1,2,3}, coroutine.yield)) assert(coroutine.isyieldable()) coroutine.yield(20) @@ -303,15 +307,15 @@ local r1, r2, v = f1(nil) assert(r1 and not r2 and v[1] == (10 + 1)*10/2) -function f (a, b) a = coroutine.yield(a); error{a + b} end -function g(x) return x[1]*2 end +local function f (a, b) a = coroutine.yield(a); error{a + b} end +local function g(x) return x[1]*2 end co = coroutine.wrap(function () coroutine.yield(xpcall(f, g, 10, 20)) end) assert(co() == 10) -r, msg = co(100) +local r, msg = co(100) assert(not r and msg == 240) @@ -373,9 +377,10 @@ assert(not a and b == foo and coroutine.status(x) == "dead") a,b = coroutine.resume(x) assert(not a and string.find(b, "dead") and coroutine.status(x) == "dead") +goo = nil -- co-routines x for loop -function all (a, n, k) +local function all (a, n, k) if k == 0 then coroutine.yield(a) else for i=1,n do @@ -415,7 +420,7 @@ assert(f() == 43 and f() == 53) -- old bug: attempt to resume itself -function co_func (current_co) +local function co_func (current_co) assert(coroutine.running() == current_co) assert(coroutine.resume(current_co) == false) coroutine.yield(10, 20) @@ -491,15 +496,16 @@ a = nil -- access to locals of erroneous coroutines local x = coroutine.create (function () local a = 10 - _G.f = function () a=a+1; return a end + _G.F = function () a=a+1; return a end error('x') end) assert(not coroutine.resume(x)) -- overwrite previous position of local `a' assert(not coroutine.resume(x, 1, 1, 1, 1, 1, 1, 1)) -assert(_G.f() == 11) -assert(_G.f() == 12) +assert(_G.F() == 11) +assert(_G.F() == 12) +_G.F = nil if not T then @@ -510,7 +516,7 @@ else local turn - function fact (t, x) + local function fact (t, x) assert(turn == t) if x == 0 then return 1 else return x*fact(t, x-1) @@ -579,6 +585,7 @@ else _G.X = nil; co(); assert(_G.X == line + 2 and _G.XX == nil) _G.X = nil; co(); assert(_G.X == line + 3 and _G.XX == 20) assert(co() == 10) + _G.X = nil -- testing yields in count hook co = coroutine.wrap(function () @@ -656,6 +663,8 @@ else assert(X == 'a a a' and Y == 'OK') + X, Y = nil + -- resuming running coroutine C = coroutine.create(function () @@ -701,7 +710,7 @@ else X = function (x) coroutine.yield(x, 'BB'); return 'CC' end; return 'ok']])) - t = table.pack(T.testC(state, [[ + local t = table.pack(T.testC(state, [[ rawgeti R 1 # get main thread pushstring 'XX' getglobal X # get function for body @@ -730,13 +739,13 @@ end -- leaving a pending coroutine open -_X = coroutine.wrap(function () +_G.TO_SURVIVE = coroutine.wrap(function () local a = 10 local x = function () a = a+1 end coroutine.yield() end) -_X() +_G.TO_SURVIVE() if not _soft then @@ -935,7 +944,7 @@ assert(run(function () do local _ENV = _ENV f = function () AAA = BBB + 1; return AAA end end -g = new(10); g.k.BBB = 10; +local g = new(10); g.k.BBB = 10; debug.setupvalue(f, 1, g) assert(run(f, {"idx", "nidx", "idx"}) == 11) assert(g.k.AAA == 11) @@ -1075,6 +1084,8 @@ assert(#a == 3 and a[1] == a[2] and a[2] == a[3] and a[3] == 34) -- testing yields with continuations +local y + co = coroutine.wrap(function (...) return T.testC([[ # initial function yieldk 1 2 @@ -1127,6 +1138,9 @@ assert(x == "YIELD" and y == 4) assert(not pcall(co)) -- coroutine should be dead +_G.ctx = nil +_G.status = nil + -- bug in nCcalls local co = coroutine.wrap(function () diff --git a/testes/db.lua b/testes/db.lua index f891e9b8dd..02b96aca2e 100644 --- a/testes/db.lua +++ b/testes/db.lua @@ -16,7 +16,7 @@ end assert(not debug.gethook()) local testline = 19 -- line where 'test' is defined -function test (s, l, p) -- this must be line 19 +local function test (s, l, p) -- this must be line 19 collectgarbage() -- avoid gc during trace local function f (event, line) assert(event == 'line') @@ -50,7 +50,7 @@ end -- test file and string names truncation -a = "function f () end" +local a = "function f () end" local function dostring (s, x) return load(s, x)() end dostring(a) assert(debug.getinfo(f).short_src == string.format('[string "%s"]', a)) @@ -72,7 +72,8 @@ dostring(a, string.format("=%s", string.rep('x', 500))) assert(string.find(debug.getinfo(f).short_src, "^x*$")) dostring(a, "=") assert(debug.getinfo(f).short_src == "") -a = nil; f = nil; +_G.a = nil; _G.f = nil; +_G[string.rep("p", 400)] = nil repeat @@ -120,6 +121,7 @@ else end ]], {2,3,4,7}) + test([[ local function foo() end @@ -128,6 +130,7 @@ A = 1 A = 2 A = 3 ]], {2, 3, 2, 4, 5, 6}) +_G.A = nil test([[-- @@ -175,6 +178,8 @@ end test([[for i=1,4 do a=1 end]], {1,1,1,1}) +_G.a = nil + do -- testing line info/trace with large gaps in source @@ -194,6 +199,7 @@ do -- testing line info/trace with large gaps in source end end end +_G.a = nil do -- testing active lines @@ -287,7 +293,6 @@ foo(200, 3, 4) local a = {} for i = 1, (_soft and 100 or 1000) do a[i] = i end foo(table.unpack(a)) -a = nil @@ -307,13 +312,14 @@ do -- test hook presence in debug info debug.sethook() assert(count == 4) end +_ENV.a = nil -- hook table has weak keys assert(getmetatable(debug.getregistry()._HOOKKEY).__mode == 'k') -a = {}; L = nil +a = {}; local L = nil local glob = 1 local oldglob = glob debug.sethook(function (e,l) @@ -354,7 +360,7 @@ function foo() end; foo() -- set L -- check line counting inside strings and empty lines -_ = 'alo\ +local _ = 'alo\ alo' .. [[ ]] @@ -403,6 +409,7 @@ function g(a,b) return (a+1) + f() end assert(g(0,0) == 30) +_G.f, _G.g = nil debug.sethook(nil); assert(not debug.gethook()) @@ -446,7 +453,7 @@ local function collectlocals (level) end -X = nil +local X = nil a = {} function a:f (a, b, ...) local arg = {...}; local c = 13 end debug.sethook(function (e) @@ -469,6 +476,7 @@ a:f(1,2,3,4,5) assert(X.self == a and X.a == 1 and X.b == 2 and X.c == nil) assert(XX == 12) assert(not debug.gethook()) +_G.XX = nil -- testing access to local variables in return hook (bug in 5.2) @@ -593,6 +601,7 @@ end debug.sethook() +local g, g1 -- tests for tail calls local function f (x) @@ -638,7 +647,7 @@ h(false) debug.sethook() assert(b == 2) -- two tail calls -lim = _soft and 3000 or 30000 +local lim = _soft and 3000 or 30000 local function foo (x) if x==0 then assert(debug.getinfo(2).what == "main") @@ -940,7 +949,7 @@ end print("testing debug functions on chunk without debug info") -prog = [[-- program to be loaded without debug information (strip) +local prog = [[-- program to be loaded without debug information (strip) local debug = require'debug' local a = 12 -- a local variable diff --git a/testes/errors.lua b/testes/errors.lua index 55bdab829e..cf0ab5265d 100644 --- a/testes/errors.lua +++ b/testes/errors.lua @@ -114,12 +114,14 @@ checkmessage("a = {} | 1", "bitwise operation") checkmessage("a = {} < 1", "attempt to compare") checkmessage("a = {} <= 1", "attempt to compare") -checkmessage("a=1; bbbb=2; a=math.sin(3)+bbbb(3)", "global 'bbbb'") -checkmessage("a={}; do local a=1 end a:bbbb(3)", "method 'bbbb'") +checkmessage("aaa=1; bbbb=2; aaa=math.sin(3)+bbbb(3)", "global 'bbbb'") +checkmessage("aaa={}; do local aaa=1 end aaa:bbbb(3)", "method 'bbbb'") checkmessage("local a={}; a.bbbb(3)", "field 'bbbb'") -assert(not string.find(doit"a={13}; local bbbb=1; a[bbbb](3)", "'bbbb'")) -checkmessage("a={13}; local bbbb=1; a[bbbb](3)", "number") -checkmessage("a=(1)..{}", "a table value") +assert(not string.find(doit"aaa={13}; local bbbb=1; aaa[bbbb](3)", "'bbbb'")) +checkmessage("aaa={13}; local bbbb=1; aaa[bbbb](3)", "number") +checkmessage("aaa=(1)..{}", "a table value") + +_G.aaa, _G.bbbb = nil -- calls checkmessage("local a; a(13)", "local 'a'") @@ -134,12 +136,13 @@ checkmessage([[ -- tail calls checkmessage("local a={}; return a.bbbb(3)", "field 'bbbb'") -checkmessage("a={}; do local a=1 end; return a:bbbb(3)", "method 'bbbb'") +checkmessage("aaa={}; do local aaa=1 end; return aaa:bbbb(3)", "method 'bbbb'") + +checkmessage("aaa = #print", "length of a function value") +checkmessage("aaa = #3", "length of a number value") -checkmessage("a = #print", "length of a function value") -checkmessage("a = #3", "length of a number value") +_G.aaa = nil -aaa = nil checkmessage("aaa.bbb:ddd(9)", "global 'aaa'") checkmessage("local aaa={bbb=1}; aaa.bbb:ddd(9)", "field 'bbb'") checkmessage("local aaa={bbb={}}; aaa.bbb:ddd(9)", "method 'ddd'") @@ -152,15 +155,16 @@ checkmessage("local a,b,cc; (function () a.x = 1 end)()", "upvalue 'a'") checkmessage("local _ENV = {x={}}; a = a + 1", "global 'a'") -checkmessage("b=1; local aaa={}; x=aaa+b", "local 'aaa'") +checkmessage("BB=1; local aaa={}; x=aaa+BB", "local 'aaa'") checkmessage("aaa={}; x=3.3/aaa", "global 'aaa'") -checkmessage("aaa=2; b=nil;x=aaa*b", "global 'b'") +checkmessage("aaa=2; BB=nil;x=aaa*BB", "global 'BB'") checkmessage("aaa={}; x=-aaa", "global 'aaa'") -- short circuit -checkmessage("a=1; local a,bbbb=2,3; a = math.sin(1) and bbbb(3)", +checkmessage("aaa=1; local aaa,bbbb=2,3; aaa = math.sin(1) and bbbb(3)", "local 'bbbb'") -checkmessage("a=1; local a,bbbb=2,3; a = bbbb(1) or a(3)", "local 'bbbb'") +checkmessage("aaa=1; local aaa,bbbb=2,3; aaa = bbbb(1) or aaa(3)", + "local 'bbbb'") checkmessage("local a,b,c,f = 1,1,1; f((a and b) or c)", "local 'f'") checkmessage("local a,b,c = 1,1,1; ((a and b) or c)()", "call a number value") assert(not string.find(doit"aaa={}; x=(aaa or aaa)+(aaa and aaa)", "'aaa'")) @@ -187,8 +191,8 @@ checkmessage("return ~-3e40", "has no integer representation") checkmessage("return ~-3.009", "has no integer representation") checkmessage("return 3.009 & 1", "has no integer representation") checkmessage("return 34 >> {}", "table value") -checkmessage("a = 24 // 0", "divide by zero") -checkmessage("a = 1 % 0", "'n%0'") +checkmessage("aaa = 24 // 0", "divide by zero") +checkmessage("aaa = 1 % 0", "'n%0'") -- type error for an object which is neither in an upvalue nor a register. @@ -269,13 +273,13 @@ end -- tests for field accesses after RK limit local t = {} for i = 1, 1000 do - t[i] = "a = x" .. i + t[i] = "aaa = x" .. i end local s = table.concat(t, "; ") t = nil -checkmessage(s.."; a = bbb + 1", "global 'bbb'") -checkmessage("local _ENV=_ENV;"..s.."; a = bbb + 1", "global 'bbb'") -checkmessage(s.."; local t = {}; a = t.bbb + 1", "field 'bbb'") +checkmessage(s.."; aaa = bbb + 1", "global 'bbb'") +checkmessage("local _ENV=_ENV;"..s.."; aaa = bbb + 1", "global 'bbb'") +checkmessage(s.."; local t = {}; aaa = t.bbb + 1", "field 'bbb'") checkmessage(s.."; local t = {}; t:bbb()", "method 'bbb'") checkmessage([[aaa=9 @@ -324,14 +328,17 @@ main() ]], "global 'NoSuchName'") print'+' -a = {}; setmetatable(a, {__index = string}) -checkmessage("a:sub()", "bad self") +aaa = {}; setmetatable(aaa, {__index = string}) +checkmessage("aaa:sub()", "bad self") checkmessage("string.sub('a', {})", "#2") checkmessage("('a'):sub{}", "#1") checkmessage("table.sort({1,2,3}, table.sort)", "'table.sort'") checkmessage("string.gsub('s', 's', setmetatable)", "'setmetatable'") +_G.aaa = nil + + -- tests for errors in coroutines local function f (n) @@ -349,7 +356,7 @@ checkerr("yield across", f) -- testing size of 'source' info; size of buffer for that info is -- LUA_IDSIZE, declared as 60 in luaconf. Get one position for '\0'. -idsize = 60 - 1 +local idsize = 60 - 1 local function checksize (source) -- syntax error local _, msg = load("x", source) @@ -411,13 +418,14 @@ x local p = [[ function g() f() end - function f(x) error('a', X) end + function f(x) error('a', XX) end g() ]] -X=3;lineerror((p), 3) -X=0;lineerror((p), false) -X=1;lineerror((p), 2) -X=2;lineerror((p), 1) +XX=3;lineerror((p), 3) +XX=0;lineerror((p), false) +XX=1;lineerror((p), 2) +XX=2;lineerror((p), 1) +_G.XX, _G.g, _G.f = nil lineerror([[ @@ -449,11 +457,11 @@ if not _soft then -- several tests that exaust the Lua stack collectgarbage() print"testing stack overflow" - C = 0 + local C = 0 -- get line where stack overflow will happen local l = debug.getinfo(1, "l").currentline + 1 local function auxy () C=C+1; auxy() end -- produce a stack overflow - function y () + function YY () collectgarbage("stop") -- avoid running finalizers without stack space auxy() collectgarbage("restart") @@ -465,9 +473,11 @@ if not _soft then return (string.find(m, "stack overflow")) end -- repeated stack overflows (to check stack recovery) - assert(checkstackmessage(doit('y()'))) - assert(checkstackmessage(doit('y()'))) - assert(checkstackmessage(doit('y()'))) + assert(checkstackmessage(doit('YY()'))) + assert(checkstackmessage(doit('YY()'))) + assert(checkstackmessage(doit('YY()'))) + + _G.YY = nil -- error lines in stack overflow @@ -561,7 +571,7 @@ do end -- xpcall with arguments -a, b, c = xpcall(string.find, error, "alo", "al") +local a, b, c = xpcall(string.find, error, "alo", "al") assert(a and b == 1 and c == 2) a, b, c = xpcall(string.find, function (x) return {} end, true, "al") assert(not a and type(b) == "table" and c == nil) @@ -581,11 +591,12 @@ checksyntax("a\1a = 1", "", "<\\1>", 1) -- test 255 as first char in a chunk checksyntax("\255a = 1", "", "<\\255>", 1) -doit('I = load("a=9+"); a=3') -assert(a==3 and not I) +doit('I = load("a=9+"); aaa=3') +assert(_G.aaa==3 and not _G.I) +_G.I,_G.aaa = nil print('+') -lim = 1000 +local lim = 1000 if _soft then lim = 100 end for i=1,lim do doit('a = ') diff --git a/testes/events.lua b/testes/events.lua index 17a7366449..8d8563b952 100644 --- a/testes/events.lua +++ b/testes/events.lua @@ -420,6 +420,9 @@ assert(i == 3 and x[1] == 3 and x[3] == 5) assert(_G.X == 20) +_G.X, _G.B = nil + + print'+' local _g = _G diff --git a/testes/files.lua b/testes/files.lua index 78f962e5a0..be00bf3fd1 100644 --- a/testes/files.lua +++ b/testes/files.lua @@ -507,15 +507,17 @@ load((io.lines(file, 1)))() assert(_G.X == 4) load((io.lines(file, 3)))() assert(_G.X == 8) +_G.X = nil print('+') local x1 = "string\n\n\\com \"\"''coisas [[estranhas]] ]]'" io.output(file) -assert(io.write(string.format("x2 = %q\n-- comment without ending EOS", x1))) +assert(io.write(string.format("X2 = %q\n-- comment without ending EOS", x1))) io.close() assert(loadfile(file))() -assert(x1 == x2) +assert(x1 == _G.X2) +_G.X2 = nil print('+') assert(os.remove(file)) assert(not os.remove(file)) diff --git a/testes/gc.lua b/testes/gc.lua index 381c5548be..03093e34ff 100644 --- a/testes/gc.lua +++ b/testes/gc.lua @@ -125,7 +125,7 @@ do end a:test() - + _G.temp = nil end @@ -134,7 +134,7 @@ do local f = function () end end print("functions with errors") -prog = [[ +local prog = [[ do a = 10; function foo(x,y) @@ -153,22 +153,25 @@ do end end end +rawset(_G, "a", nil) +_G.x = nil -foo = nil -print('long strings') -x = "01234567890123456789012345678901234567890123456789012345678901234567890123456789" -assert(string.len(x)==80) -s = '' -k = math.min(300, (math.maxinteger // 80) // 2) -for n = 1, k do s = s..x; j=tostring(n) end -assert(string.len(s) == k*80) -s = string.sub(s, 1, 10000) -s, i = string.gsub(s, '(%d%d%d%d)', '') -assert(i==10000 // 4) -s = nil -x = nil - -assert(_G["while"] == 234) +do + foo = nil + print('long strings') + local x = "01234567890123456789012345678901234567890123456789012345678901234567890123456789" + assert(string.len(x)==80) + local s = '' + local k = math.min(300, (math.maxinteger // 80) // 2) + for n = 1, k do s = s..x; local j=tostring(n) end + assert(string.len(s) == k*80) + s = string.sub(s, 1, 10000) + local s, i = string.gsub(s, '(%d%d%d%d)', '') + assert(i==10000 // 4) + + assert(_G["while"] == 234) + _G["while"] = nil +end -- @@ -227,8 +230,8 @@ end print("clearing tables") -lim = 15 -a = {} +local lim = 15 +local a = {} -- fill a with `collectable' indices for i=1,lim do a[{}] = i end b = {} @@ -552,6 +555,7 @@ do for i=1,1000 do _ENV.a = {} end -- no collection during the loop until gcinfo() > 2 * x collectgarbage"restart" + _ENV.a = nil end diff --git a/testes/literals.lua b/testes/literals.lua index d5a769ed2e..30ab9ab115 100644 --- a/testes/literals.lua +++ b/testes/literals.lua @@ -10,6 +10,7 @@ local function dostring (x) return assert(load(x), "")() end dostring("x \v\f = \t\r 'a\0a' \v\f\f") assert(x == 'a\0a' and string.len(x) == 3) +_G.x = nil -- escape sequences assert('\n\"\'\\' == [[ @@ -129,16 +130,16 @@ end -- long variable names -var1 = string.rep('a', 15000) .. '1' -var2 = string.rep('a', 15000) .. '2' -prog = string.format([[ +local var1 = string.rep('a', 15000) .. '1' +local var2 = string.rep('a', 15000) .. '2' +local prog = string.format([[ %s = 5 %s = %s + 1 return function () return %s - %s end ]], var1, var2, var1, var1, var2) local f = dostring(prog) assert(_G[var1] == 5 and _G[var2] == 6 and f() == -1) -var1, var2, f = nil +_G[var1], _G[var2] = nil print('+') -- escapes -- @@ -150,13 +151,13 @@ assert([[ $debug]] == "\n $debug") assert([[ [ ]] ~= [[ ] ]]) -- long strings -- -b = "001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789" +local b = "001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789" assert(string.len(b) == 960) prog = [=[ print('+') -a1 = [["this is a 'string' with several 'quotes'"]] -a2 = "'quotes'" +local a1 = [["this is a 'string' with several 'quotes'"]] +local a2 = "'quotes'" assert(string.find(a1, a2) == 34) print('+') @@ -164,12 +165,13 @@ print('+') a1 = [==[temp = [[an arbitrary value]]; ]==] assert(load(a1))() assert(temp == 'an arbitrary value') +_G.temp = nil -- long strings -- -b = "001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789" +local b = "001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789" assert(string.len(b) == 960) print('+') -a = [[00123456789012345678901234567890123456789123456789012345678901234567890123456789 +local a = [[00123456789012345678901234567890123456789123456789012345678901234567890123456789 00123456789012345678901234567890123456789123456789012345678901234567890123456789 00123456789012345678901234567890123456789123456789012345678901234567890123456789 00123456789012345678901234567890123456789123456789012345678901234567890123456789 @@ -199,13 +201,11 @@ x = 1 ]=] print('+') -x = nil +_G.x = nil dostring(prog) assert(x) +_G.x = nil -prog = nil -a = nil -b = nil do -- reuse of long strings @@ -234,8 +234,8 @@ end -- testing line ends prog = [[ -a = 1 -- a comment -b = 2 +local a = 1 -- a comment +local b = 2 x = [=[ @@ -252,10 +252,11 @@ for _, n in pairs{"\n", "\r", "\n\r", "\r\n"} do assert(dostring(prog) == nn) assert(_G.x == "hi\n" and _G.y == "\nhello\r\n\n") end +_G.x, _G.y = nil -- testing comments and strings with long brackets -a = [==[]=]==] +local a = [==[]=]==] assert(a == "]=") a = [==[[===[[=[]]=][====[]]===]===]==] diff --git a/testes/locals.lua b/testes/locals.lua index d50beaa522..2c48546d5d 100644 --- a/testes/locals.lua +++ b/testes/locals.lua @@ -37,7 +37,7 @@ end f = nil local f -x = 1 +local x = 1 a = nil load('local a = {}')() @@ -152,7 +152,7 @@ local dummy local _ENV = (function (...) return ... end)(_G, dummy) -- { do local _ENV = {assert=assert}; assert(true) end -mt = {_G = _G} +local mt = {_G = _G} local foo,x A = false -- "declare" A do local _ENV = mt @@ -174,6 +174,8 @@ do local _ENV = {assert=assert, A=10}; end assert(x==20) +A = nil + do -- constants local a, b, c = 10, 20, 30 @@ -711,7 +713,7 @@ if rawget(_G, "T") then collectgarbage(); collectgarbage() - m = T.totalmem() + local m = T.totalmem() collectgarbage("stop") -- error in the first buffer allocation diff --git a/testes/main.lua b/testes/main.lua index 9187420ef5..f59badcf88 100644 --- a/testes/main.lua +++ b/testes/main.lua @@ -339,7 +339,7 @@ prepfile("a = [[b\nc\nd\ne]]\n=a") RUN([[lua -e"_PROMPT='' _PROMPT2=''" -i < %s > %s]], prog, out) checkprogout("b\nc\nd\ne\n\n") -prompt = "alo" +local prompt = "alo" prepfile[[ -- a = 2 ]] @@ -390,7 +390,7 @@ NoRun("error object is a table value", [[lua %s]], prog) -- chunk broken in many lines -s = [=[ -- +local s = [=[ -- function f ( x ) local a = [[ xuxu diff --git a/testes/math.lua b/testes/math.lua index 48c1efe159..0191f7ddad 100644 --- a/testes/math.lua +++ b/testes/math.lua @@ -50,7 +50,7 @@ end local msgf2i = "number.* has no integer representation" -- float equality -function eq (a,b,limit) +local function eq (a,b,limit) if not limit then if floatbits >= 50 then limit = 1E-11 else limit = 1E-5 @@ -62,7 +62,7 @@ end -- equality with types -function eqT (a,b) +local function eqT (a,b) return a == b and math.type(a) == math.type(b) end @@ -83,7 +83,7 @@ end do local x = -1 local mz = 0/x -- minus zero - t = {[0] = 10, 20, 30, 40, 50} + local t = {[0] = 10, 20, 30, 40, 50} assert(t[mz] == t[0] and t[-0] == t[0]) end diff --git a/testes/nextvar.lua b/testes/nextvar.lua index 0874e5bb22..02b7dea2ef 100644 --- a/testes/nextvar.lua +++ b/testes/nextvar.lua @@ -189,7 +189,7 @@ end -- size tests for vararg lim = 35 -function foo (n, ...) +local function foo (n, ...) local arg = {...} check(arg, n, 0) assert(select('#', ...) == n) diff --git a/testes/pm.lua b/testes/pm.lua index 94bb63ca5e..795596d412 100644 --- a/testes/pm.lua +++ b/testes/pm.lua @@ -9,12 +9,12 @@ local function checkerror (msg, f, ...) end -function f(s, p) +local function f (s, p) local i,e = string.find(s, p) if i then return string.sub(s, i, e) end end -a,b = string.find('', '') -- empty patterns are tricky +local a,b = string.find('', '') -- empty patterns are tricky assert(a == 1 and b == 0); a,b = string.find('alo', '') assert(a == 1 and b == 0) @@ -88,7 +88,7 @@ assert(f("alo alo", "%C+") == "alo alo") print('+') -function f1(s, p) +local function f1 (s, p) p = string.gsub(p, "%%([0-9])", function (s) return "%" .. (tonumber(s)+1) end) @@ -113,7 +113,7 @@ local abc = string.char(range(0, 127)) .. string.char(range(128, 255)); assert(string.len(abc) == 256) -function strset (p) +local function strset (p) local res = {s=''} string.gsub(abc, p, function (c) res.s = res.s .. c end) return res.s @@ -147,7 +147,7 @@ assert(string.gsub(' assert(string.gsub('alo lo ', ' +$', '') == 'alo lo') -- trim assert(string.gsub(' alo alo ', '^%s*(.-)%s*$', '%1') == 'alo alo') -- double trim assert(string.gsub('alo alo \n 123\n ', '%s+', ' ') == 'alo alo 123 ') -t = "ab d" +local t = "ab d" a, b = string.gsub(t, '(.)', '%1@') assert('@'..a == string.gsub(t, '', '@') and b == 5) a, b = string.gsub('abd', '(.)', '%0@', 2) @@ -184,6 +184,7 @@ do local function setglobal (n,v) rawset(_G, n, v) end string.gsub("a=roberto,roberto=a", "(%w+)=(%w%w*)", setglobal) assert(_G.a=="roberto" and _G.roberto=="a") + _G.a = nil; _G.roberto = nil end function f(a,b) return string.gsub(a,'.',b) end @@ -195,20 +196,21 @@ assert(string.gsub("alo $a='x'$ novamente $return a$", "$([^$]*)%$", dostring) == "alo novamente x") -x = string.gsub("$x=string.gsub('alo', '.', string.upper)$ assim vai para $return x$", +local x = string.gsub("$x=string.gsub('alo', '.', string.upper)$ assim vai para $return x$", "$([^$]*)%$", dostring) assert(x == ' assim vai para ALO') - -t = {} -s = 'a alo jose joao' -r = string.gsub(s, '()(%w+)()', function (a,w,b) - assert(string.len(w) == b-a); - t[a] = b-a; - end) +_G.a, _G.x = nil + +local t = {} +local s = 'a alo jose joao' +local r = string.gsub(s, '()(%w+)()', function (a,w,b) + assert(string.len(w) == b-a); + t[a] = b-a; + end) assert(s == r and t[1] == 1 and t[3] == 3 and t[7] == 4 and t[13] == 4) -function isbalanced (s) +local function isbalanced (s) return not string.find(string.gsub(s, "%b()", ""), "[()]") end @@ -251,7 +253,7 @@ if not _soft then end -- recursive nest of gsubs -function rev (s) +local function rev (s) return string.gsub(s, "(.)(.+)", function (c,s1) return rev(s1)..c end) end diff --git a/testes/sort.lua b/testes/sort.lua index ef405d9219..52919b8cd2 100644 --- a/testes/sort.lua +++ b/testes/sort.lua @@ -20,7 +20,7 @@ end checkerror("wrong number of arguments", table.insert, {}, 2, 3, 4) local x,y,z,a,n -a = {}; lim = _soft and 200 or 2000 +a = {}; local lim = _soft and 200 or 2000 for i=1, lim do a[i]=i end assert(select(lim, unpack(a)) == lim and select('#', unpack(a)) == lim) x = unpack(a) @@ -222,7 +222,7 @@ a = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", table.sort(a) check(a) -function perm (s, n) +local function perm (s, n) n = n or #s if n == 1 then local t = {unpack(s)} @@ -248,7 +248,7 @@ perm{1,2,3,3,5} perm{1,2,3,4,5,6} perm{2,2,3,3,5,6} -function timesort (a, n, func, msg, pre) +local function timesort (a, n, func, msg, pre) local x = os.clock() table.sort(a, func) x = (os.clock() - x) * 1000 @@ -257,7 +257,7 @@ function timesort (a, n, func, msg, pre) check(a, func) end -limit = 50000 +local limit = 50000 if _soft then limit = 5000 end a = {} @@ -274,7 +274,7 @@ for i=1,limit do a[i] = math.random() end -x = os.clock(); i=0 +local x = os.clock(); local i = 0 table.sort(a, function(x,y) i=i+1; return y64k)" -- template to create a very big test file -prog = [[$ +local prog = [[$ local a,b @@ -85,7 +85,7 @@ function b:xxx (a,b) return a+b end assert(b:xxx(10, 12) == 22) -- pushself with non-constant index b["xxx"] = undef -s = 0; n=0 +local s = 0; local n=0 for a,b in pairs(b) do s=s+b; n=n+1 end -- with 32-bit floats, exact value of 's' depends on summation order assert(81800000.0 < s and s < 81860000 and n == 70001) @@ -93,7 +93,7 @@ assert(81800000.0 < s and s < 81860000 and n == 70001) a = nil; b = nil print'+' -function f(x) b=x end +local function f(x) b=x end a = f{$3$} or 10 @@ -118,7 +118,7 @@ local function sig (x) return (x % 2 == 0) and '' or '-' end -F = { +local F = { function () -- $1$ for i=10,50009 do io.write('a', i, ' = ', sig(i), 5+((i-10)/2), ',\n') @@ -138,14 +138,14 @@ function () -- $3$ end, } -file = os.tmpname() +local file = os.tmpname() io.output(file) for s in string.gmatch(prog, "$([^$]+)") do local n = tonumber(s) if not n then io.write(s) else F[n]() end end io.close() -result = dofile(file) +local result = dofile(file) assert(os.remove(file)) print'OK' return result From 8dea54877a5e7b0a461b076e79fdc8b47d7e39e6 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 29 Dec 2022 15:41:07 -0300 Subject: [PATCH 0802/1145] Do not avoid major collections when GCdebt is zero 'collectgarbage("step")' (without an argument) does not have any special meaning, it means "do a step with some default size". --- lgc.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/lgc.c b/lgc.c index 278566500e..f68c5af029 100644 --- a/lgc.c +++ b/lgc.c @@ -9,7 +9,6 @@ #include "lprefix.h" -#include #include @@ -1377,14 +1376,12 @@ static void genmajorstep (lua_State *L, global_State *g) { /* ** Does a generational "step". If the total number of objects grew ** more than 'majormul'% since the last major collection, does a -** major collection. Otherwise, does a minor collection. The test -** ('GCdebt' != 0) avoids major collections when the step originated from -** 'collectgarbage("step")'. +** major collection. Otherwise, does a minor collection. */ static void genstep (lua_State *L, global_State *g) { l_obj majorbase = g->GClastmajor; /* count after last major collection */ l_obj majorinc = applygcparam(g, genmajormul, majorbase); - if (g->GCdebt != 0 && gettotalobjs(g) > majorbase + majorinc) { + if (gettotalobjs(g) > majorbase + majorinc && 0) { /* do a major collection */ enterinc(g); g->gckind = KGC_GENMAJOR; From d69789da1ccfa4db7c241de6b471d6b729f1561e Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 24 Jan 2023 15:04:17 -0300 Subject: [PATCH 0803/1145] Fix absence of 'system' in iOS Despite claiming to be ISO, the C library in some Apple platforms does not implement 'system'. --- loslib.c | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/loslib.c b/loslib.c index 854dcf691e..7eb05cafd4 100644 --- a/loslib.c +++ b/loslib.c @@ -138,12 +138,28 @@ /* }================================================================== */ +/* +** Despite claiming to be ISO, the C library in some Apple platforms +** does not implement 'system'. +*/ +#if !defined(l_system) && defined(__APPLE__) /* { */ +#include "TargetConditionals.h" +#if TARGET_OS_IOS || TARGET_OS_WATCH || TARGET_OS_TV +#define l_system(cmd) ((cmd) == NULL ? 0 : -1) +#endif +#endif /* } */ + +#if !defined(l_system) +#define l_system(cmd) system(cmd) /* default definition */ +#endif + + static int os_execute (lua_State *L) { const char *cmd = luaL_optstring(L, 1, NULL); int stat; errno = 0; - stat = system(cmd); + stat = l_system(cmd); if (cmd != NULL) return luaL_execresult(L, stat); else { From c888ae0aea030491f90b749260a156ce15635681 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 2 Feb 2023 13:37:11 -0300 Subject: [PATCH 0804/1145] Small changes in hash of pointers When converting from pointer to integer, use 'uintptr_t' if available; otherwise try 'uintmax_t', and use 'size_t' as last resource. --- llimits.h | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/llimits.h b/llimits.h index 52a32f92e3..251a27021b 100644 --- a/llimits.h +++ b/llimits.h @@ -71,11 +71,24 @@ typedef signed char ls_byte; /* -** conversion of pointer to unsigned integer: -** this is for hashing only; there is no problem if the integer -** cannot hold the whole pointer value +** conversion of pointer to unsigned integer: this is for hashing only; +** there is no problem if the integer cannot hold the whole pointer +** value. (In strict ISO C this may cause undefined behavior, but no +** actual machine seems to bother.) */ -#define point2uint(p) ((unsigned int)((size_t)(p) & UINT_MAX)) +#if !defined(LUA_USE_C89) && defined(__STDC_VERSION__) && \ + __STDC_VERSION__ >= 199901L +#include +#if defined(UINTPTR_MAX) /* even in C99 this type is optional */ +#define L_P2I uintptr_t +#else /* no 'intptr'? */ +#define L_P2I uintmax_t /* use the largerst available integer */ +#endif +#else /* C89 option */ +#define L_P2I size_t +#endif + +#define point2uint(p) ((unsigned int)((L_P2I)(p) & UINT_MAX)) From cf08915d62e338c987b71c078b148490510e9fe7 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 2 Feb 2023 13:43:41 -0300 Subject: [PATCH 0805/1145] New macro LUA_USE_IOS Do not try to detect automatically whether system is iOS; it is simpler and more reliable to let the programmer inform that. --- loslib.c | 17 +++++------------ luaconf.h | 6 ++++++ 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/loslib.c b/loslib.c index 7eb05cafd4..89ac06bc45 100644 --- a/loslib.c +++ b/loslib.c @@ -138,21 +138,14 @@ /* }================================================================== */ -/* -** Despite claiming to be ISO, the C library in some Apple platforms -** does not implement 'system'. -*/ -#if !defined(l_system) && defined(__APPLE__) /* { */ -#include "TargetConditionals.h" -#if TARGET_OS_IOS || TARGET_OS_WATCH || TARGET_OS_TV -#define l_system(cmd) ((cmd) == NULL ? 0 : -1) -#endif -#endif /* } */ - #if !defined(l_system) +#if defined(LUA_USE_IOS) +/* Despite claiming to be ISO C, iOS does not implement 'system'. */ +#define l_system(cmd) ((cmd) == NULL ? 0 : -1) +#else #define l_system(cmd) system(cmd) /* default definition */ #endif - +#endif static int os_execute (lua_State *L) { diff --git a/luaconf.h b/luaconf.h index e4650fbce8..137103edec 100644 --- a/luaconf.h +++ b/luaconf.h @@ -70,6 +70,12 @@ #endif +#if defined(LUA_USE_IOS) +#define LUA_USE_POSIX +#define LUA_USE_DLOPEN +#endif + + /* @@ LUAI_IS32INT is true iff 'int' has (at least) 32 bits. */ From 5e08b41567c5723c9f599d02a7511aa398f7c646 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 7 Feb 2023 10:48:39 -0300 Subject: [PATCH 0806/1145] Simpler definition for LUA_STRFTIMEOPTIONS There is no need for those intermediate definitions. --- loslib.c | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/loslib.c b/loslib.c index 89ac06bc45..ad5a927688 100644 --- a/loslib.c +++ b/loslib.c @@ -30,23 +30,14 @@ */ #if !defined(LUA_STRFTIMEOPTIONS) /* { */ -/* options for ANSI C 89 (only 1-char options) */ -#define L_STRFTIMEC89 "aAbBcdHIjmMpSUwWxXyYZ%" - -/* options for ISO C 99 and POSIX */ -#define L_STRFTIMEC99 "aAbBcCdDeFgGhHIjmMnprRStTuUVwWxXyYzZ%" \ - "||" "EcECExEXEyEY" "OdOeOHOIOmOMOSOuOUOVOwOWOy" /* two-char options */ - -/* options for Windows */ -#define L_STRFTIMEWIN "aAbBcdHIjmMpSUwWxXyYzZ%" \ - "||" "#c#x#d#H#I#j#m#M#S#U#w#W#y#Y" /* two-char options */ - #if defined(LUA_USE_WINDOWS) -#define LUA_STRFTIMEOPTIONS L_STRFTIMEWIN -#elif defined(LUA_USE_C89) -#define LUA_STRFTIMEOPTIONS L_STRFTIMEC89 +#define LUA_STRFTIMEOPTIONS "aAbBcdHIjmMpSUwWxXyYzZ%" \ + "||" "#c#x#d#H#I#j#m#M#S#U#w#W#y#Y" /* two-char options */ +#elif defined(LUA_USE_C89) /* ANSI C 89 (only 1-char options) */ +#define LUA_STRFTIMEOPTIONS "aAbBcdHIjmMpSUwWxXyYZ%" #else /* C99 specification */ -#define LUA_STRFTIMEOPTIONS L_STRFTIMEC99 +#define LUA_STRFTIMEOPTIONS "aAbBcCdDeFgGhHIjmMnprRStTuUVwWxXyYzZ%" \ + "||" "EcECExEXEyEY" "OdOeOHOIOmOMOSOuOUOVOwOWOy" /* two-char options */ #endif #endif /* } */ From 02bab9fc258fe1cbc6088b1bd61193499d058eff Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 8 Feb 2023 14:15:41 -0300 Subject: [PATCH 0807/1145] Bug: Wrong line in error message for arith. errors It also causes 'L->top' to be wrong when the error happens, triggering an 'assert'. --- lvm.c | 4 ++++ testes/errors.lua | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/lvm.c b/lvm.c index 2e84dc63c1..8493a770c5 100644 --- a/lvm.c +++ b/lvm.c @@ -1410,6 +1410,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_MODK) { + savestate(L, ci); /* in case of division by 0 */ op_arithK(L, luaV_mod, luaV_modf); vmbreak; } @@ -1422,6 +1423,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_IDIVK) { + savestate(L, ci); /* in case of division by 0 */ op_arithK(L, luaV_idiv, luai_numidiv); vmbreak; } @@ -1470,6 +1472,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_MOD) { + savestate(L, ci); /* in case of division by 0 */ op_arith(L, luaV_mod, luaV_modf); vmbreak; } @@ -1482,6 +1485,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_IDIV) { /* floor division */ + savestate(L, ci); /* in case of division by 0 */ op_arith(L, luaV_idiv, luai_numidiv); vmbreak; } diff --git a/testes/errors.lua b/testes/errors.lua index cf0ab5265d..bf6f389d26 100644 --- a/testes/errors.lua +++ b/testes/errors.lua @@ -444,6 +444,14 @@ if not b then end end]], 5) + +-- bug in 5.4.0 +lineerror([[ + local a = 0 + local b = 1 + local c = b % a +]], 3) + do -- Force a negative estimate for base line. Error in instruction 2 -- (after VARARGPREP, GETGLOBAL), with first absolute line information From 1de2f31694ddbc86b18e491c8aedc91791f512e2 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 9 Mar 2023 11:10:04 -0300 Subject: [PATCH 0808/1145] Corrected support for 16-bit systems We still need access to a 16-bit system to correctly test these changes. --- ldo.c | 14 +++++--------- lopcodes.h | 2 +- ltable.c | 2 ++ 3 files changed, 8 insertions(+), 10 deletions(-) diff --git a/ldo.c b/ldo.c index c30cde76f5..2a0017ca62 100644 --- a/ldo.c +++ b/ldo.c @@ -299,17 +299,13 @@ static int stackinuse (lua_State *L) { */ void luaD_shrinkstack (lua_State *L) { int inuse = stackinuse(L); - int nsize = inuse * 2; /* proposed new size */ - int max = inuse * 3; /* maximum "reasonable" size */ - if (max > LUAI_MAXSTACK) { - max = LUAI_MAXSTACK; /* respect stack limit */ - if (nsize > LUAI_MAXSTACK) - nsize = LUAI_MAXSTACK; - } + int max = (inuse > LUAI_MAXSTACK / 3) ? LUAI_MAXSTACK : inuse * 3; /* if thread is currently not handling a stack overflow and its size is larger than maximum "reasonable" size, shrink it */ - if (inuse <= LUAI_MAXSTACK && stacksize(L) > max) + if (inuse <= LUAI_MAXSTACK && stacksize(L) > max) { + int nsize = (inuse > LUAI_MAXSTACK / 2) ? LUAI_MAXSTACK : inuse * 2; luaD_reallocstack(L, nsize, 0); /* ok if that fails */ + } else /* don't change stack */ condmovestack(L,{},{}); /* (change only for debugging) */ luaE_shrinkCI(L); /* shrink CI list */ @@ -629,7 +625,7 @@ CallInfo *luaD_precall (lua_State *L, StkId func, int nresults) { ** check the stack before doing anything else. 'luaD_precall' already ** does that. */ -l_sinline void ccall (lua_State *L, StkId func, int nResults, int inc) { +l_sinline void ccall (lua_State *L, StkId func, int nResults, l_uint32 inc) { CallInfo *ci; L->nCcalls += inc; if (l_unlikely(getCcalls(L) >= LUAI_MAXCCALLS)) { diff --git a/lopcodes.h b/lopcodes.h index 7c27451596..4c55145399 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -21,7 +21,7 @@ iABC C(8) | B(8) |k| A(8) | Op(7) | iABx Bx(17) | A(8) | Op(7) | iAsBx sBx (signed)(17) | A(8) | Op(7) | iAx Ax(25) | Op(7) | -isJ sJ(25) | Op(7) | +isJ sJ (signed)(25) | Op(7) | A signed argument is represented in excess K: the represented value is the written unsigned value minus K, where K is half the maximum for the diff --git a/ltable.c b/ltable.c index cc7993e083..3c690c5f17 100644 --- a/ltable.c +++ b/ltable.c @@ -257,9 +257,11 @@ LUAI_FUNC unsigned int luaH_realasize (const Table *t) { size |= (size >> 2); size |= (size >> 4); size |= (size >> 8); +#if (UINT_MAX >> 14) > 3 /* unsigned int has more than 16 bits */ size |= (size >> 16); #if (UINT_MAX >> 30) > 3 size |= (size >> 32); /* unsigned int has more than 32 bits */ +#endif #endif size++; lua_assert(ispow2(size) && size/2 < t->alimit && t->alimit < size); From c4b71b7ba0dee419b5bda1ec297eca8e42c9f1d2 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 9 Mar 2023 11:12:11 -0300 Subject: [PATCH 0809/1145] Details Comments in 'onelua.c' --- onelua.c | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/onelua.c b/onelua.c index 3c605981f0..2a43496124 100644 --- a/onelua.c +++ b/onelua.c @@ -1,5 +1,14 @@ /* -* one.c -- Lua core, libraries, and interpreter in a single file +** Lua core, libraries, and interpreter in a single file. +** Compiling just this file generates a complete Lua stand-alone +** program: +** +** $ gcc -O2 -std=c99 -o lua onelua.c -lm +** +** or +** +** $ gcc -O2 -std=c89 -DLUA_USE_C89 -o lua onelua.c -lm +** */ /* default is to build the full interpreter */ @@ -11,8 +20,12 @@ #endif #endif -/* choose suitable platform-specific features */ -/* some of these may need extra libraries such as -ldl -lreadline -lncurses */ + +/* +** Choose suitable platform-specific features. Default is no +** platform-specific features. Some of these options may need extra +** libraries such as -ldl -lreadline -lncurses +*/ #if 0 #define LUA_USE_LINUX #define LUA_USE_MACOSX @@ -20,6 +33,7 @@ #define LUA_ANSI #endif + /* no need to change anything below this line ----------------------------- */ #include "lprefix.h" From ab859fe59b464a038a45552921cb2b23892343af Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 17 Mar 2023 15:52:09 -0300 Subject: [PATCH 0810/1145] Bug: Loading a corrupted binary file can segfault The size of the list of upvalue names are stored separated from the size of the list of upvalues, but they share the same array. --- ldump.c | 8 ++++++-- lundump.c | 2 ++ testes/calls.lua | 14 ++++++++++++++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/ldump.c b/ldump.c index f848b669cb..f231691b77 100644 --- a/ldump.c +++ b/ldump.c @@ -10,6 +10,7 @@ #include "lprefix.h" +#include #include #include "lua.h" @@ -55,8 +56,11 @@ static void dumpByte (DumpState *D, int y) { } -/* dumpInt Buff Size */ -#define DIBS ((sizeof(size_t) * 8 / 7) + 1) +/* +** 'dumpSize' buffer size: each byte can store up to 7 bits. (The "+6" +** rounds up the division.) +*/ +#define DIBS ((sizeof(size_t) * CHAR_BIT + 6) / 7) static void dumpSize (DumpState *D, size_t x) { lu_byte buff[DIBS]; diff --git a/lundump.c b/lundump.c index aba93f8280..02aed64fb6 100644 --- a/lundump.c +++ b/lundump.c @@ -248,6 +248,8 @@ static void loadDebug (LoadState *S, Proto *f) { f->locvars[i].endpc = loadInt(S); } n = loadInt(S); + if (n != 0) /* does it have debug information? */ + n = f->sizeupvalues; /* must be this many */ for (i = 0; i < n; i++) f->upvalues[i].name = loadStringN(S, f); } diff --git a/testes/calls.lua b/testes/calls.lua index a19385843b..2d562a24a8 100644 --- a/testes/calls.lua +++ b/testes/calls.lua @@ -342,6 +342,20 @@ do -- another bug (in 5.4.0) end +do -- another bug (since 5.2) + -- corrupted binary dump: list of upvalue names is larger than number + -- of upvalues, overflowing the array of upvalues. + local code = + "\x1b\x4c\x75\x61\x54\x00\x19\x93\x0d\x0a\x1a\x0a\x04\x08\x08\x78\x56\z + \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x28\x77\x40\x00\x86\x40\z + \x74\x65\x6d\x70\x81\x81\x01\x00\x02\x82\x48\x00\x02\x00\xc7\x00\x01\z + \x00\x80\x80\x80\x82\x00\x00\x80\x81\x82\x78\x80\x82\x81\x86\x40\x74\z + \x65\x6d\x70" + + assert(load(code)) -- segfaults in previous versions +end + + x = string.dump(load("x = 1; return x")) a = assert(load(read1(x), nil, "b")) assert(a() == 1 and _G.x == 1) From 5a04f1851e0d42b4bcbb0af103490bc964e985aa Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 20 Mar 2023 16:13:17 -0300 Subject: [PATCH 0811/1145] New function 'luaL_makeseed' This function unifies code from 'lua_newstate', 'math.randomseed', and 'table.sort' that tries to create a value with a minimum level of randomness. --- lauxlib.c | 50 +++++++++++++++++++++++++++++++++++++++++++++++- lauxlib.h | 2 ++ lmathlib.c | 24 +++++++---------------- lstate.c | 35 ++------------------------------- ltablib.c | 29 +++------------------------- ltests.c | 4 ++-- ltests.h | 3 ++- lua.h | 3 ++- manual/manual.of | 23 +++++++++++++++++----- 9 files changed, 87 insertions(+), 86 deletions(-) diff --git a/lauxlib.c b/lauxlib.c index 4ca6c65488..64b325d387 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -1091,8 +1091,56 @@ static void warnfon (void *ud, const char *message, int tocont) { } + +/* +** A function to compute an unsigned int with some level of +** randomness. Rely on Address Space Layout Randomization (if present), +** current time, and clock. +*/ +#if !defined(luai_makeseed) + +#include + + +/* +** Size of 'e' measured in number of 'unsigned int's. (In the weird +** case that the division truncates, we just lose some part of the +** value, no big deal.) +*/ +#define sof(e) (sizeof(e) / sizeof(unsigned int)) + + +#define addbuff(b,v) \ + (memcpy(b, &(v), sof(v) * sizeof(unsigned int)), b += sof(v)) + + +static unsigned int luai_makeseed (void) { + unsigned int buff[sof(void*) + sof(clock_t) + sof(time_t)]; + unsigned int res; + unsigned int *b = buff; + clock_t c = clock(); + time_t t = time(NULL); + void *h = buff; + addbuff(b, h); /* local variable's address */ + addbuff(b, c); /* clock */ + addbuff(b, t); /* time */ + res = buff[0]; + for (b = buff + 1; b < buff + sof(buff); b++) + res += *b; + return res; +} + +#endif + + +LUALIB_API unsigned int luaL_makeseed (lua_State *L) { + (void)L; /* unused */ + return luai_makeseed(); +} + + LUALIB_API lua_State *luaL_newstate (void) { - lua_State *L = lua_newstate(l_alloc, NULL); + lua_State *L = lua_newstate(l_alloc, NULL, luai_makeseed()); if (l_likely(L)) { lua_atpanic(L, &panic); lua_setwarnf(L, warnfoff, L); /* default is warnings off */ diff --git a/lauxlib.h b/lauxlib.h index 5b977e2a39..0ee9a57237 100644 --- a/lauxlib.h +++ b/lauxlib.h @@ -100,6 +100,8 @@ LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s); LUALIB_API lua_State *(luaL_newstate) (void); +LUALIB_API unsigned int luaL_makeseed (lua_State *L); + LUALIB_API lua_Integer (luaL_len) (lua_State *L, int idx); LUALIB_API void (luaL_addgsub) (luaL_Buffer *b, const char *s, diff --git a/lmathlib.c b/lmathlib.c index d0b1e1e5d6..f13cae4aa9 100644 --- a/lmathlib.c +++ b/lmathlib.c @@ -603,28 +603,18 @@ static void setseed (lua_State *L, Rand64 *state, } -/* -** Set a "random" seed. To get some randomness, use the current time -** and the address of 'L' (in case the machine does address space layout -** randomization). -*/ -static void randseed (lua_State *L, RanState *state) { - lua_Unsigned seed1 = (lua_Unsigned)time(NULL); - lua_Unsigned seed2 = (lua_Unsigned)(size_t)L; - setseed(L, state->s, seed1, seed2); -} - - static int math_randomseed (lua_State *L) { RanState *state = (RanState *)lua_touserdata(L, lua_upvalueindex(1)); + lua_Unsigned n1, n2; if (lua_isnone(L, 1)) { - randseed(L, state); + n1 = luaL_makeseed(L); + n2 = I2UInt(state->s[0]); } else { - lua_Integer n1 = luaL_checkinteger(L, 1); - lua_Integer n2 = luaL_optinteger(L, 2, 0); - setseed(L, state->s, n1, n2); + n1 = luaL_checkinteger(L, 1); + n2 = luaL_optinteger(L, 2, 0); } + setseed(L, state->s, n1, n2); return 2; /* return seeds */ } @@ -641,7 +631,7 @@ static const luaL_Reg randfuncs[] = { */ static void setrandfunc (lua_State *L) { RanState *state = (RanState *)lua_newuserdatauv(L, sizeof(RanState), 0); - randseed(L, state); /* initialize with a "random" seed */ + setseed(L, state->s, luaL_makeseed(L), 0); /* initialize with random seed */ lua_pop(L, 2); /* remove pushed seeds */ luaL_setfuncs(L, randfuncs, 1); } diff --git a/lstate.c b/lstate.c index bee3bf664c..1ce6b9a194 100644 --- a/lstate.c +++ b/lstate.c @@ -51,37 +51,6 @@ typedef struct LG { #define fromstate(L) (cast(LX *, cast(lu_byte *, (L)) - offsetof(LX, l))) -/* -** A macro to create a "random" seed when a state is created; -** the seed is used to randomize string hashes. -*/ -#if !defined(luai_makeseed) - -#include - -/* -** Compute an initial seed with some level of randomness. -** Rely on Address Space Layout Randomization (if present) and -** current time. -*/ -#define addbuff(b,p,e) \ - { size_t t = cast_sizet(e); \ - memcpy(b + p, &t, sizeof(t)); p += sizeof(t); } - -static unsigned int luai_makeseed (lua_State *L) { - char buff[3 * sizeof(size_t)]; - unsigned int h = cast_uint(time(NULL)); - int p = 0; - addbuff(buff, p, L); /* heap variable */ - addbuff(buff, p, &h); /* local variable */ - addbuff(buff, p, &lua_newstate); /* public function */ - lua_assert(p == sizeof(buff)); - return luaS_hash(buff, p, h); -} - -#endif - - /* ** set GCdebt to a new value keeping the value (totalobjs + GCdebt) ** invariant (and avoiding underflows in 'totalobjs') @@ -350,7 +319,7 @@ LUA_API int lua_resetthread (lua_State *L, lua_State *from) { } -LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { +LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud, unsigned int seed) { int i; lua_State *L; global_State *g; @@ -370,7 +339,7 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { g->warnf = NULL; g->ud_warn = NULL; g->mainthread = L; - g->seed = luai_makeseed(L); + g->seed = seed; g->gcstp = GCSTPGC; /* no GC while building state */ g->strt.size = g->strt.nuse = 0; g->strt.hash = NULL; diff --git a/ltablib.c b/ltablib.c index e6bc4d04af..8258445969 100644 --- a/ltablib.c +++ b/ltablib.c @@ -230,31 +230,8 @@ typedef unsigned int IdxT; ** of a partition. (If you don't want/need this "randomness", ~0 is a ** good choice.) */ -#if !defined(l_randomizePivot) /* { */ - -#include - -/* size of 'e' measured in number of 'unsigned int's */ -#define sof(e) (sizeof(e) / sizeof(unsigned int)) - -/* -** Use 'time' and 'clock' as sources of "randomness". Because we don't -** know the types 'clock_t' and 'time_t', we cannot cast them to -** anything without risking overflows. A safe way to use their values -** is to copy them to an array of a known type and use the array values. -*/ -static unsigned int l_randomizePivot (void) { - clock_t c = clock(); - time_t t = time(NULL); - unsigned int buff[sof(c) + sof(t)]; - unsigned int i, rnd = 0; - memcpy(buff, &c, sof(c) * sizeof(unsigned int)); - memcpy(buff + sof(c), &t, sof(t) * sizeof(unsigned int)); - for (i = 0; i < sof(buff); i++) - rnd += buff[i]; - return rnd; -} - +#if !defined(l_randomizePivot) +#define l_randomizePivot(L) luaL_makeseed(L) #endif /* } */ @@ -391,7 +368,7 @@ static void auxsort (lua_State *L, IdxT lo, IdxT up, up = p - 1; /* tail call for [lo .. p - 1] (lower interval) */ } if ((up - lo) / 128 > n) /* partition too imbalanced? */ - rnd = l_randomizePivot(); /* try a new randomization */ + rnd = l_randomizePivot(L); /* try a new randomization */ } /* tail call auxsort(L, lo, up, rnd) */ } diff --git a/ltests.c b/ltests.c index e2e0d983f2..456e83e156 100644 --- a/ltests.c +++ b/ltests.c @@ -1159,7 +1159,7 @@ static int num2int (lua_State *L) { static int newstate (lua_State *L) { void *ud; lua_Alloc f = lua_getallocf(L, &ud); - lua_State *L1 = lua_newstate(f, ud); + lua_State *L1 = lua_newstate(f, ud, 0); if (L1) { lua_atpanic(L1, tpanic); lua_pushlightuserdata(L, L1); @@ -1252,7 +1252,7 @@ static int checkpanic (lua_State *L) { lua_Alloc f = lua_getallocf(L, &ud); b.paniccode = luaL_optstring(L, 2, ""); b.L = L; - L1 = lua_newstate(f, ud); /* create new state */ + L1 = lua_newstate(f, ud, 0); /* create new state */ if (L1 == NULL) { /* error? */ lua_pushnil(L); return 1; diff --git a/ltests.h b/ltests.h index 45d5beba16..da773d6eed 100644 --- a/ltests.h +++ b/ltests.h @@ -102,7 +102,8 @@ LUA_API void *debug_realloc (void *ud, void *block, size_t osize, size_t nsize); #if defined(lua_c) -#define luaL_newstate() lua_newstate(debug_realloc, &l_memcontrol) +#define luaL_newstate() \ + lua_newstate(debug_realloc, &l_memcontrol, luaL_makeseed(NULL)) #define luai_openlibs(L) \ { luaL_openlibs(L); \ luaL_requiref(L, "T", luaB_opentests, 1); \ diff --git a/lua.h b/lua.h index cb32ec2251..e950dfb415 100644 --- a/lua.h +++ b/lua.h @@ -160,7 +160,8 @@ extern const char lua_ident[]; /* ** state manipulation */ -LUA_API lua_State *(lua_newstate) (lua_Alloc f, void *ud); +LUA_API lua_State *(lua_newstate) (lua_Alloc f, void *ud, + unsigned int seed); LUA_API void (lua_close) (lua_State *L); LUA_API lua_State *(lua_newthread) (lua_State *L); LUA_API int (lua_resetthread) (lua_State *L, lua_State *from); diff --git a/manual/manual.of b/manual/manual.of index 73d2595177..fdae76f24f 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -20,7 +20,7 @@ making it ideal for configuration, scripting, and rapid prototyping. Lua is implemented as a library, written in @emphx{clean C}, -the common subset of @N{Standard C} and C++. +the common subset of C and C++. The Lua distribution includes a host program called @id{lua}, which uses the Lua library to offer a complete, standalone Lua interpreter, @@ -2957,7 +2957,7 @@ static void *l_alloc (void *ud, void *ptr, size_t osize, return realloc(ptr, nsize); } } -Note that @N{Standard C} ensures +Note that @N{ISO C} ensures that @T{free(NULL)} has no effect and that @T{realloc(NULL,size)} is equivalent to @T{malloc(size)}. @@ -3644,7 +3644,8 @@ Other upvalues are initialized with @nil. } -@APIEntry{lua_State *lua_newstate (lua_Alloc f, void *ud);| +@APIEntry{lua_State *lua_newstate (lua_Alloc f, void *ud, + unsigned int seed);| @apii{0,0,-} Creates a new independent state and returns its main thread. @@ -3655,6 +3656,8 @@ Lua will do all memory allocation for this state through this function @seeF{lua_Alloc}. The second argument, @id{ud}, is an opaque pointer that Lua passes to the allocator in every call. +The third argument, @id{seed}, is a seed for the hashing of +strings when they are used as table keys. } @@ -5721,6 +5724,16 @@ it does not run it. } +@APIEntry{unsigned int luaL_makeseed (lua_State *L);| +@apii{0,0,-} + +Returns a value with a weak attempt for randomness. +(It produces that value based on the current date and time, +the current processor time, and the address of an internal variable, +in case the machine has Address Space Layout Randomization.) + +} + @APIEntry{void luaL_newlib (lua_State *L, const luaL_Reg l[]);| @apii{0,1,m} @@ -6892,7 +6905,7 @@ including if necessary a path and an extension. @id{funcname} must be the exact name exported by the @N{C library} (which may depend on the @N{C compiler} and linker used). -This function is not supported by @N{Standard C}. +This function is not supported by @N{ISO C}. As such, it is only available on some platforms (Windows, Linux, Mac OS X, Solaris, BSD, plus other Unix systems that support the @id{dlfcn} standard). @@ -8093,7 +8106,7 @@ different sequences of results each time the program runs. When called with at least one argument, the integer parameters @id{x} and @id{y} are -joined into a 128-bit @emphx{seed} that +joined into a @emphx{seed} that is used to reinitialize the pseudo-random generator; equal seeds produce equal sequences of numbers. The default for @id{y} is zero. From 86e8039a72646cd9192fd08a6f1771c90b872ff6 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 23 Mar 2023 16:01:16 -0300 Subject: [PATCH 0812/1145] Clock component removed from 'luaL_makeseed' 'clock' can be quite slow on some machines. --- lauxlib.c | 10 ++++------ lmathlib.c | 4 ++-- ltablib.c | 2 +- manual/manual.of | 4 ++-- 4 files changed, 9 insertions(+), 11 deletions(-) diff --git a/lauxlib.c b/lauxlib.c index 64b325d387..555a5976ef 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -1094,8 +1094,8 @@ static void warnfon (void *ud, const char *message, int tocont) { /* ** A function to compute an unsigned int with some level of -** randomness. Rely on Address Space Layout Randomization (if present), -** current time, and clock. +** randomness. Rely on Address Space Layout Randomization (if present) +** and the current time. */ #if !defined(luai_makeseed) @@ -1115,18 +1115,16 @@ static void warnfon (void *ud, const char *message, int tocont) { static unsigned int luai_makeseed (void) { - unsigned int buff[sof(void*) + sof(clock_t) + sof(time_t)]; + unsigned int buff[sof(void*) + sof(time_t)]; unsigned int res; unsigned int *b = buff; - clock_t c = clock(); time_t t = time(NULL); void *h = buff; addbuff(b, h); /* local variable's address */ - addbuff(b, c); /* clock */ addbuff(b, t); /* time */ res = buff[0]; for (b = buff + 1; b < buff + sof(buff); b++) - res += *b; + res ^= (res >> 3) + (res << 7) + *b; return res; } diff --git a/lmathlib.c b/lmathlib.c index f13cae4aa9..6d63950c81 100644 --- a/lmathlib.c +++ b/lmathlib.c @@ -607,8 +607,8 @@ static int math_randomseed (lua_State *L) { RanState *state = (RanState *)lua_touserdata(L, lua_upvalueindex(1)); lua_Unsigned n1, n2; if (lua_isnone(L, 1)) { - n1 = luaL_makeseed(L); - n2 = I2UInt(state->s[0]); + n1 = luaL_makeseed(L); /* "random" seed */ + n2 = I2UInt(nextrand(state->s)); /* in case seed is not that random... */ } else { n1 = luaL_checkinteger(L, 1); diff --git a/ltablib.c b/ltablib.c index 8258445969..44d55ef511 100644 --- a/ltablib.c +++ b/ltablib.c @@ -310,7 +310,7 @@ static IdxT partition (lua_State *L, IdxT lo, IdxT up) { */ static IdxT choosePivot (IdxT lo, IdxT up, unsigned int rnd) { IdxT r4 = (up - lo) / 4; /* range/4 */ - IdxT p = rnd % (r4 * 2) + (lo + r4); + IdxT p = (rnd ^ lo ^ up) % (r4 * 2) + (lo + r4); lua_assert(lo + r4 <= p && p <= up - r4); return p; } diff --git a/manual/manual.of b/manual/manual.of index fdae76f24f..446517ec37 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -5728,8 +5728,8 @@ it does not run it. @apii{0,0,-} Returns a value with a weak attempt for randomness. -(It produces that value based on the current date and time, -the current processor time, and the address of an internal variable, +(It produces that value based on the current date and time +and the address of an internal variable, in case the machine has Address Space Layout Randomization.) } From 94689ac3ad290caf3bada21c389a991f55391987 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 24 Mar 2023 15:52:03 -0300 Subject: [PATCH 0813/1145] More regularity in uses of enums in 'lcode.c' --- lcode.c | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/lcode.c b/lcode.c index 911dbd5f1e..236ce05ca7 100644 --- a/lcode.c +++ b/lcode.c @@ -1389,15 +1389,16 @@ static void finishbinexpval (FuncState *fs, expdesc *e1, expdesc *e2, ** Emit code for binary expressions that "produce values" over ** two registers. */ -static void codebinexpval (FuncState *fs, OpCode op, +static void codebinexpval (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2, int line) { + OpCode op = cast(OpCode, opr + OP_ADD); int v2 = luaK_exp2anyreg(fs, e2); /* make sure 'e2' is in a register */ /* 'e1' must be already in a register or it is a constant */ lua_assert((VNIL <= e1->k && e1->k <= VKSTR) || e1->k == VNONRELOC || e1->k == VRELOC); lua_assert(OP_ADD <= op && op <= OP_SHR); finishbinexpval(fs, e1, e2, op, v2, 0, line, OP_MMBIN, - cast(TMS, (op - OP_ADD) + TM_ADD)); + cast(TMS, opr + TM_ADD)); } @@ -1457,10 +1458,9 @@ static void swapexps (expdesc *e1, expdesc *e2) { */ static void codebinNoK (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2, int flip, int line) { - OpCode op = cast(OpCode, opr + OP_ADD); if (flip) swapexps(e1, e2); /* back to original order */ - codebinexpval(fs, op, e1, e2, line); /* use standard operators */ + codebinexpval(fs, opr, e1, e2, line); /* use standard operators */ } @@ -1490,7 +1490,7 @@ static void codecommutative (FuncState *fs, BinOpr op, flip = 1; } if (op == OPR_ADD && isSCint(e2)) /* immediate operand? */ - codebini(fs, cast(OpCode, OP_ADDI), e1, e2, flip, line, TM_ADD); + codebini(fs, OP_ADDI, e1, e2, flip, line, TM_ADD); else codearith(fs, op, e1, e2, flip, line); } @@ -1518,25 +1518,27 @@ static void codebitwise (FuncState *fs, BinOpr opr, ** Emit code for order comparisons. When using an immediate operand, ** 'isfloat' tells whether the original value was a float. */ -static void codeorder (FuncState *fs, OpCode op, expdesc *e1, expdesc *e2) { +static void codeorder (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) { int r1, r2; int im; int isfloat = 0; + OpCode op; if (isSCnumber(e2, &im, &isfloat)) { /* use immediate operand */ r1 = luaK_exp2anyreg(fs, e1); r2 = im; - op = cast(OpCode, (op - OP_LT) + OP_LTI); + op = cast(OpCode, (opr - OPR_LT) + OP_LTI); } else if (isSCnumber(e1, &im, &isfloat)) { /* transform (A < B) to (B > A) and (A <= B) to (B >= A) */ r1 = luaK_exp2anyreg(fs, e2); r2 = im; - op = (op == OP_LT) ? OP_GTI : OP_GEI; + op = cast(OpCode, (opr - OPR_LT) + OP_GTI); } else { /* regular case, compare two registers */ r1 = luaK_exp2anyreg(fs, e1); r2 = luaK_exp2anyreg(fs, e2); + op = cast(OpCode, (opr - OPR_EQ) + OP_EQ); } freeexps(fs, e1, e2); e1->u.info = condjump(fs, op, r1, r2, isfloat, 1); @@ -1579,16 +1581,16 @@ static void codeeq (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) { /* ** Apply prefix operation 'op' to expression 'e'. */ -void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e, int line) { +void luaK_prefix (FuncState *fs, UnOpr opr, expdesc *e, int line) { static const expdesc ef = {VKINT, {0}, NO_JUMP, NO_JUMP}; luaK_dischargevars(fs, e); - switch (op) { + switch (opr) { case OPR_MINUS: case OPR_BNOT: /* use 'ef' as fake 2nd operand */ - if (constfolding(fs, op + LUA_OPUNM, e, &ef)) + if (constfolding(fs, opr + LUA_OPUNM, e, &ef)) break; /* else */ /* FALLTHROUGH */ case OPR_LEN: - codeunexpval(fs, cast(OpCode, op + OP_UNM), e, line); + codeunexpval(fs, cast(OpCode, opr + OP_UNM), e, line); break; case OPR_NOT: codenot(fs, e); break; default: lua_assert(0); @@ -1718,14 +1720,14 @@ void luaK_posfix (FuncState *fs, BinOpr opr, /* coded as (r1 >> -I) */; } else /* regular case (two registers) */ - codebinexpval(fs, OP_SHL, e1, e2, line); + codebinexpval(fs, opr, e1, e2, line); break; } case OPR_SHR: { if (isSCint(e2)) codebini(fs, OP_SHRI, e1, e2, 0, line, TM_SHR); /* r1 >> I */ else /* regular case (two registers) */ - codebinexpval(fs, OP_SHR, e1, e2, line); + codebinexpval(fs, opr, e1, e2, line); break; } case OPR_EQ: case OPR_NE: { @@ -1733,15 +1735,13 @@ void luaK_posfix (FuncState *fs, BinOpr opr, break; } case OPR_LT: case OPR_LE: { - OpCode op = cast(OpCode, (opr - OPR_EQ) + OP_EQ); - codeorder(fs, op, e1, e2); + codeorder(fs, opr, e1, e2); break; } case OPR_GT: case OPR_GE: { /* '(a > b)' <=> '(b < a)'; '(a >= b)' <=> '(b <= a)' */ - OpCode op = cast(OpCode, (opr - OPR_NE) + OP_EQ); swapexps(e1, e2); - codeorder(fs, op, e1, e2); + codeorder(fs, (opr - OPR_NE) + OPR_EQ, e1, e2); break; } default: lua_assert(0); From 7ca8105a2ea7b6a0d7b55b59d273ccd271c35268 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 27 Mar 2023 16:29:39 -0300 Subject: [PATCH 0814/1145] More orderliness in casts of enumerations --- lcode.c | 55 +++++++++++++++++++++++++++++++++++++++++-------------- ldebug.c | 29 ++++++++++++++++------------- 2 files changed, 57 insertions(+), 27 deletions(-) diff --git a/lcode.c b/lcode.c index 236ce05ca7..1a371ca943 100644 --- a/lcode.c +++ b/lcode.c @@ -1351,6 +1351,35 @@ static int constfolding (FuncState *fs, int op, expdesc *e1, } +/* +** Convert a BinOpr to an OpCode (ORDER OPR - ORDER OP) +*/ +l_sinline OpCode binopr2op (BinOpr opr, BinOpr baser, OpCode base) { + lua_assert(baser <= opr && + ((baser == OPR_ADD && opr <= OPR_SHR) || + (baser == OPR_LT && opr <= OPR_LE))); + return cast(OpCode, (cast_int(opr) - cast_int(baser)) + cast_int(base)); +} + + +/* +** Convert a UnOpr to an OpCode (ORDER OPR - ORDER OP) +*/ +l_sinline OpCode unopr2op (UnOpr opr) { + return cast(OpCode, (cast_int(opr) - cast_int(OPR_MINUS)) + + cast_int(OP_UNM)); +} + + +/* +** Convert a BinOpr to a tag method (ORDER OPR - ORDER TM) +*/ +l_sinline TMS binopr2TM (BinOpr opr) { + lua_assert(OPR_ADD <= opr && opr <= OPR_SHR); + return cast(TMS, (cast_int(opr) - cast_int(OPR_ADD)) + cast_int(TM_ADD)); +} + + /* ** Emit code for unary expressions that "produce values" ** (everything but 'not'). @@ -1391,14 +1420,13 @@ static void finishbinexpval (FuncState *fs, expdesc *e1, expdesc *e2, */ static void codebinexpval (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2, int line) { - OpCode op = cast(OpCode, opr + OP_ADD); + OpCode op = binopr2op(opr, OPR_ADD, OP_ADD); int v2 = luaK_exp2anyreg(fs, e2); /* make sure 'e2' is in a register */ /* 'e1' must be already in a register or it is a constant */ lua_assert((VNIL <= e1->k && e1->k <= VKSTR) || e1->k == VNONRELOC || e1->k == VRELOC); lua_assert(OP_ADD <= op && op <= OP_SHR); - finishbinexpval(fs, e1, e2, op, v2, 0, line, OP_MMBIN, - cast(TMS, opr + TM_ADD)); + finishbinexpval(fs, e1, e2, op, v2, 0, line, OP_MMBIN, binopr2TM(opr)); } @@ -1419,9 +1447,9 @@ static void codebini (FuncState *fs, OpCode op, */ static void codebinK (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2, int flip, int line) { - TMS event = cast(TMS, opr + TM_ADD); + TMS event = binopr2TM(opr); int v2 = e2->u.info; /* K index */ - OpCode op = cast(OpCode, opr + OP_ADDK); + OpCode op = binopr2op(opr, OPR_ADD, OP_ADDK); finishbinexpval(fs, e1, e2, op, v2, flip, line, OP_MMBINK, event); } @@ -1527,18 +1555,18 @@ static void codeorder (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) { /* use immediate operand */ r1 = luaK_exp2anyreg(fs, e1); r2 = im; - op = cast(OpCode, (opr - OPR_LT) + OP_LTI); + op = binopr2op(opr, OPR_LT, OP_LTI); } else if (isSCnumber(e1, &im, &isfloat)) { /* transform (A < B) to (B > A) and (A <= B) to (B >= A) */ r1 = luaK_exp2anyreg(fs, e2); r2 = im; - op = cast(OpCode, (opr - OPR_LT) + OP_GTI); + op = binopr2op(opr, OPR_LT, OP_GTI); } else { /* regular case, compare two registers */ r1 = luaK_exp2anyreg(fs, e1); r2 = luaK_exp2anyreg(fs, e2); - op = cast(OpCode, (opr - OPR_EQ) + OP_EQ); + op = binopr2op(opr, OPR_LT, OP_LT); } freeexps(fs, e1, e2); e1->u.info = condjump(fs, op, r1, r2, isfloat, 1); @@ -1590,7 +1618,7 @@ void luaK_prefix (FuncState *fs, UnOpr opr, expdesc *e, int line) { break; /* else */ /* FALLTHROUGH */ case OPR_LEN: - codeunexpval(fs, cast(OpCode, opr + OP_UNM), e, line); + codeunexpval(fs, unopr2op(opr), e, line); break; case OPR_NOT: codenot(fs, e); break; default: lua_assert(0); @@ -1734,14 +1762,13 @@ void luaK_posfix (FuncState *fs, BinOpr opr, codeeq(fs, opr, e1, e2); break; } - case OPR_LT: case OPR_LE: { - codeorder(fs, opr, e1, e2); - break; - } case OPR_GT: case OPR_GE: { /* '(a > b)' <=> '(b < a)'; '(a >= b)' <=> '(b <= a)' */ swapexps(e1, e2); - codeorder(fs, (opr - OPR_NE) + OPR_EQ, e1, e2); + opr = cast(BinOpr, (opr - OPR_GT) + OPR_LT); + } /* FALLTHROUGH */ + case OPR_LT: case OPR_LE: { + codeorder(fs, opr, e1, e2); break; } default: lua_assert(0); diff --git a/ldebug.c b/ldebug.c index 3fae5cf25d..7a61a780eb 100644 --- a/ldebug.c +++ b/ldebug.c @@ -656,18 +656,19 @@ static const char *funcnamefromcall (lua_State *L, CallInfo *ci, /* -** Check whether pointer 'o' points to some value in the stack -** frame of the current function. Because 'o' may not point to a -** value in this stack, we cannot compare it with the region -** boundaries (undefined behaviour in ISO C). +** Check whether pointer 'o' points to some value in the stack frame of +** the current function and, if so, returns its index. Because 'o' may +** not point to a value in this stack, we cannot compare it with the +** region boundaries (undefined behaviour in ISO C). */ -static int isinstack (CallInfo *ci, const TValue *o) { - StkId pos; - for (pos = ci->func.p + 1; pos < ci->top.p; pos++) { - if (o == s2v(pos)) - return 1; +static int instack (CallInfo *ci, const TValue *o) { + int pos; + StkId base = ci->func.p + 1; + for (pos = 0; base + pos < ci->top.p; pos++) { + if (o == s2v(base + pos)) + return pos; } - return 0; /* not found */ + return -1; /* not found */ } @@ -708,9 +709,11 @@ static const char *varinfo (lua_State *L, const TValue *o) { const char *kind = NULL; if (isLua(ci)) { kind = getupvalname(ci, o, &name); /* check whether 'o' is an upvalue */ - if (!kind && isinstack(ci, o)) /* no? try a register */ - kind = getobjname(ci_func(ci)->p, currentpc(ci), - cast_int(cast(StkId, o) - (ci->func.p + 1)), &name); + if (!kind) { /* not an upvalue? */ + int reg = instack(ci, o); /* try a register */ + if (reg >= 0) /* is 'o' a register? */ + kind = getobjname(ci_func(ci)->p, currentpc(ci), reg, &name); + } } return formatvarinfo(L, kind, name); } From b5c65705ca78560cd2735778737122ea5f858bd0 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 31 Mar 2023 11:47:31 -0300 Subject: [PATCH 0815/1145] New year (2023) Also, small tweak in makefile. (-Wsign-compare is already enabled by -Wextra.) --- lua.h | 4 ++-- makefile | 3 +-- manual/2html | 2 +- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/lua.h b/lua.h index feb3dbc556..01927c6d98 100644 --- a/lua.h +++ b/lua.h @@ -25,7 +25,7 @@ #define LUA_VERSION "Lua " LUA_VERSION_MAJOR "." LUA_VERSION_MINOR #define LUA_RELEASE LUA_VERSION "." LUA_VERSION_RELEASE -#define LUA_COPYRIGHT LUA_RELEASE " Copyright (C) 1994-2022 Lua.org, PUC-Rio" +#define LUA_COPYRIGHT LUA_RELEASE " Copyright (C) 1994-2023 Lua.org, PUC-Rio" #define LUA_AUTHORS "R. Ierusalimschy, L. H. de Figueiredo, W. Celes" @@ -496,7 +496,7 @@ struct lua_Debug { /****************************************************************************** -* Copyright (C) 1994-2022 Lua.org, PUC-Rio. +* Copyright (C) 1994-2023 Lua.org, PUC-Rio. * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the diff --git a/makefile b/makefile index ee56c67205..38e21f1f57 100644 --- a/makefile +++ b/makefile @@ -8,7 +8,6 @@ CWARNSCPP= \ -Wfatal-errors \ -Wextra \ -Wshadow \ - -Wsign-compare \ -Wundef \ -Wwrite-strings \ -Wredundant-decls \ @@ -60,7 +59,7 @@ CWARNS= $(CWARNSCPP) $(CWARNSC) $(CWARNGCC) # The following options help detect "undefined behavior"s that seldom # create problems; some are only available in newer gcc versions. To -# use some of them, we also have to define an enrivonment variable +# use some of them, we also have to define an environment variable # ASAN_OPTIONS="detect_invalid_pointer_pairs=2". # -fsanitize=undefined # -fsanitize=pointer-subtract -fsanitize=address -fsanitize=pointer-compare diff --git a/manual/2html b/manual/2html index a4d860ddfd..43fd89133b 100755 --- a/manual/2html +++ b/manual/2html @@ -30,7 +30,7 @@ by Roberto Ierusalimschy, Luiz Henrique de Figueiredo, Waldemar Celes

Copyright -© 2022 Lua.org, PUC-Rio. All rights reserved. +© 2023 Lua.org, PUC-Rio. All rights reserved.


From e15f1f2bb7a38a3c94519294d031e48508d65006 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 18 Apr 2023 09:44:10 -0300 Subject: [PATCH 0816/1145] Details Typos in comments and details in the manual. --- ldebug.c | 4 ++-- llex.c | 2 +- llimits.h | 2 +- lparser.c | 8 ++++---- lstrlib.c | 2 +- lua.c | 2 +- manual/manual.of | 8 ++++---- 7 files changed, 14 insertions(+), 14 deletions(-) diff --git a/ldebug.c b/ldebug.c index 7a61a780eb..28b1caabf7 100644 --- a/ldebug.c +++ b/ldebug.c @@ -659,7 +659,7 @@ static const char *funcnamefromcall (lua_State *L, CallInfo *ci, ** Check whether pointer 'o' points to some value in the stack frame of ** the current function and, if so, returns its index. Because 'o' may ** not point to a value in this stack, we cannot compare it with the -** region boundaries (undefined behaviour in ISO C). +** region boundaries (undefined behavior in ISO C). */ static int instack (CallInfo *ci, const TValue *o) { int pos; @@ -848,7 +848,7 @@ static int changedline (const Proto *p, int oldpc, int newpc) { if (p->lineinfo == NULL) /* no debug information? */ return 0; if (newpc - oldpc < MAXIWTHABS / 2) { /* not too far apart? */ - int delta = 0; /* line diference */ + int delta = 0; /* line difference */ int pc = oldpc; for (;;) { int lineinfo = p->lineinfo[++pc]; diff --git a/llex.c b/llex.c index b0dc0acc24..5fc39a5cde 100644 --- a/llex.c +++ b/llex.c @@ -128,7 +128,7 @@ l_noret luaX_syntaxerror (LexState *ls, const char *msg) { ** ensuring there is only one copy of each unique string. The table ** here is used as a set: the string enters as the key, while its value ** is irrelevant. We use the string itself as the value only because it -** is a TValue readly available. Later, the code generation can change +** is a TValue readily available. Later, the code generation can change ** this value. */ TString *luaX_newstring (LexState *ls, const char *str, size_t l) { diff --git a/llimits.h b/llimits.h index 251a27021b..1c826f7be2 100644 --- a/llimits.h +++ b/llimits.h @@ -82,7 +82,7 @@ typedef signed char ls_byte; #if defined(UINTPTR_MAX) /* even in C99 this type is optional */ #define L_P2I uintptr_t #else /* no 'intptr'? */ -#define L_P2I uintmax_t /* use the largerst available integer */ +#define L_P2I uintmax_t /* use the largest available integer */ #endif #else /* C89 option */ #define L_P2I size_t diff --git a/lparser.c b/lparser.c index 24668c2485..b745f236f0 100644 --- a/lparser.c +++ b/lparser.c @@ -521,12 +521,12 @@ static l_noret jumpscopeerror (LexState *ls, Labeldesc *gt) { /* ** Solves the goto at index 'g' to given 'label' and removes it -** from the list of pending goto's. +** from the list of pending gotos. ** If it jumps into the scope of some variable, raises an error. */ static void solvegoto (LexState *ls, int g, Labeldesc *label) { int i; - Labellist *gl = &ls->dyd->gt; /* list of goto's */ + Labellist *gl = &ls->dyd->gt; /* list of gotos */ Labeldesc *gt = &gl->arr[g]; /* goto to be resolved */ lua_assert(eqstr(gt->name, label->name)); if (l_unlikely(gt->nactvar < label->nactvar)) /* enter some scope? */ @@ -580,7 +580,7 @@ static int newgotoentry (LexState *ls, TString *name, int line, int pc) { /* ** Solves forward jumps. Check whether new label 'lb' matches any ** pending gotos in current block and solves them. Return true -** if any of the goto's need to close upvalues. +** if any of the gotos need to close upvalues. */ static int solvegotos (LexState *ls, Labeldesc *lb) { Labellist *gl = &ls->dyd->gt; @@ -601,7 +601,7 @@ static int solvegotos (LexState *ls, Labeldesc *lb) { /* ** Create a new label with the given 'name' at the given 'line'. ** 'last' tells whether label is the last non-op statement in its -** block. Solves all pending goto's to this new label and adds +** block. Solves all pending gotos to this new label and adds ** a close instruction if necessary. ** Returns true iff it added a close instruction. */ diff --git a/lstrlib.c b/lstrlib.c index 0b4fdbb7b5..03167161df 100644 --- a/lstrlib.c +++ b/lstrlib.c @@ -570,7 +570,7 @@ static const char *match_capture (MatchState *ms, const char *s, int l) { static const char *match (MatchState *ms, const char *s, const char *p) { if (l_unlikely(ms->matchdepth-- == 0)) luaL_error(ms->L, "pattern too complex"); - init: /* using goto's to optimize tail recursion */ + init: /* using goto to optimize tail recursion */ if (p != ms->p_end) { /* end of pattern? */ switch (*p) { case '(': { /* start capture */ diff --git a/lua.c b/lua.c index 715430a0de..0ff8845453 100644 --- a/lua.c +++ b/lua.c @@ -666,7 +666,7 @@ int main (int argc, char **argv) { l_message(argv[0], "cannot create state: not enough memory"); return EXIT_FAILURE; } - lua_gc(L, LUA_GCSTOP); /* stop GC while buidling state */ + lua_gc(L, LUA_GCSTOP); /* stop GC while building state */ lua_pushcfunction(L, &pmain); /* to call 'pmain' in protected mode */ lua_pushinteger(L, argc); /* 1st argument */ lua_pushlightuserdata(L, argv); /* 2nd argument */ diff --git a/manual/manual.of b/manual/manual.of index 6d19e251e5..ac1d7e601c 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -20,7 +20,7 @@ making it ideal for configuration, scripting, and rapid prototyping. Lua is implemented as a library, written in @emphx{clean C}, -the common subset of @N{Standard C} and C++. +the common subset of @N{standard C} and C++. The Lua distribution includes a host program called @id{lua}, which uses the Lua library to offer a complete, standalone Lua interpreter, @@ -2963,7 +2963,7 @@ static void *l_alloc (void *ud, void *ptr, size_t osize, return realloc(ptr, nsize); } } -Note that @N{Standard C} ensures +Note that @N{ISO C} ensures that @T{free(NULL)} has no effect and that @T{realloc(NULL,size)} is equivalent to @T{malloc(size)}. @@ -5780,7 +5780,7 @@ with @id{tname} in the registry. Creates a new Lua state. It calls @Lid{lua_newstate} with an -allocator based on the @N{standard C} allocation functions +allocator based on the @N{ISO C} allocation functions and then sets a warning function and a panic function @see{C-error} that print messages to the standard error output. @@ -6898,7 +6898,7 @@ including if necessary a path and an extension. @id{funcname} must be the exact name exported by the @N{C library} (which may depend on the @N{C compiler} and linker used). -This function is not supported by @N{Standard C}. +This functionality is not supported by @N{ISO C}. As such, it is only available on some platforms (Windows, Linux, Mac OS X, Solaris, BSD, plus other Unix systems that support the @id{dlfcn} standard). From 6443185167c77adcc8552a3fee7edab7895db1a9 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 2 May 2023 16:41:43 -0300 Subject: [PATCH 0817/1145] "Emergency" new version 5.4.6 'lua_resetthread' is back to its original signature, to avoid incompatibilities in the ABI between releases of the same version. New function 'lua_closethread' added with the "correct" signature. --- lcorolib.c | 4 ++-- lstate.c | 10 +++++++++- ltests.c | 2 +- lua.h | 7 ++++--- manual/manual.of | 40 +++++++++++++++++++++++++--------------- 5 files changed, 41 insertions(+), 22 deletions(-) diff --git a/lcorolib.c b/lcorolib.c index 40b880b14d..c64adf08a8 100644 --- a/lcorolib.c +++ b/lcorolib.c @@ -76,7 +76,7 @@ static int luaB_auxwrap (lua_State *L) { if (l_unlikely(r < 0)) { /* error? */ int stat = lua_status(co); if (stat != LUA_OK && stat != LUA_YIELD) { /* error in the coroutine? */ - stat = lua_resetthread(co, L); /* close its tbc variables */ + stat = lua_closethread(co, L); /* close its tbc variables */ lua_assert(stat != LUA_OK); lua_xmove(co, L, 1); /* move error message to the caller */ } @@ -172,7 +172,7 @@ static int luaB_close (lua_State *L) { int status = auxstatus(L, co); switch (status) { case COS_DEAD: case COS_YIELD: { - status = lua_resetthread(co, L); + status = lua_closethread(co, L); if (status == LUA_OK) { lua_pushboolean(L, 1); return 1; diff --git a/lstate.c b/lstate.c index 1fbefb4b14..1e925e5ad4 100644 --- a/lstate.c +++ b/lstate.c @@ -339,7 +339,7 @@ int luaE_resetthread (lua_State *L, int status) { } -LUA_API int lua_resetthread (lua_State *L, lua_State *from) { +LUA_API int lua_closethread (lua_State *L, lua_State *from) { int status; lua_lock(L); L->nCcalls = (from) ? getCcalls(from) : 0; @@ -349,6 +349,14 @@ LUA_API int lua_resetthread (lua_State *L, lua_State *from) { } +/* +** Deprecated! Use 'lua_closethread' instead. +*/ +LUA_API int lua_resetthread (lua_State *L) { + return lua_closethread(L, NULL); +} + + LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { int i; lua_State *L; diff --git a/ltests.c b/ltests.c index 4a0a6af1fd..7d184c0d8e 100644 --- a/ltests.c +++ b/ltests.c @@ -1533,7 +1533,7 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) { lua_newthread(L1); } else if EQ("resetthread") { - lua_pushinteger(L1, lua_resetthread(L1, L)); + lua_pushinteger(L1, lua_resetthread(L1)); /* deprecated */ } else if EQ("newuserdata") { lua_newuserdata(L1, getnum); diff --git a/lua.h b/lua.h index 01927c6d98..fd16cf8050 100644 --- a/lua.h +++ b/lua.h @@ -18,10 +18,10 @@ #define LUA_VERSION_MAJOR "5" #define LUA_VERSION_MINOR "4" -#define LUA_VERSION_RELEASE "5" +#define LUA_VERSION_RELEASE "6" #define LUA_VERSION_NUM 504 -#define LUA_VERSION_RELEASE_NUM (LUA_VERSION_NUM * 100 + 5) +#define LUA_VERSION_RELEASE_NUM (LUA_VERSION_NUM * 100 + 6) #define LUA_VERSION "Lua " LUA_VERSION_MAJOR "." LUA_VERSION_MINOR #define LUA_RELEASE LUA_VERSION "." LUA_VERSION_RELEASE @@ -163,7 +163,8 @@ extern const char lua_ident[]; LUA_API lua_State *(lua_newstate) (lua_Alloc f, void *ud); LUA_API void (lua_close) (lua_State *L); LUA_API lua_State *(lua_newthread) (lua_State *L); -LUA_API int (lua_resetthread) (lua_State *L, lua_State *from); +LUA_API int (lua_closethread) (lua_State *L, lua_State *from); +LUA_API int (lua_resetthread) (lua_State *L); /* Deprecated! */ LUA_API lua_CFunction (lua_atpanic) (lua_State *L, lua_CFunction panicf); diff --git a/manual/manual.of b/manual/manual.of index ac1d7e601c..f8d8ddd40e 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -3167,6 +3167,27 @@ when called through this function. } +@APIEntry{int lua_closethread (lua_State *L, lua_State *from);| +@apii{0,?,-} + +Resets a thread, cleaning its call stack and closing all pending +to-be-closed variables. +Returns a status code: +@Lid{LUA_OK} for no errors in the thread +(either the original error that stopped the thread or +errors in closing methods), +or an error status otherwise. +In case of error, +leaves the error object on the top of the stack. + +The parameter @id{from} represents the coroutine that is resetting @id{L}. +If there is no such coroutine, +this parameter can be @id{NULL}. + +(This function was introduced in @N{release 5.4.6}.) + +} + @APIEntry{int lua_compare (lua_State *L, int index1, int index2, int op);| @apii{0,0,e} @@ -4160,23 +4181,12 @@ and then pops the top element. } -@APIEntry{int lua_resetthread (lua_State *L, lua_State *from);| +@APIEntry{int lua_resetthread (lua_State *L);| @apii{0,?,-} -Resets a thread, cleaning its call stack and closing all pending -to-be-closed variables. -Returns a status code: -@Lid{LUA_OK} for no errors in the thread -(either the original error that stopped the thread or -errors in closing methods), -or an error status otherwise. -In case of error, -leaves the error object on the top of the stack. - -The parameter @id{from} represents the coroutine that is resetting @id{L}. -If there is no such coroutine, -this parameter can be @id{NULL}. -(This parameter was introduced in @N{release 5.4.5}.) +This function is deprecated; +it is equivalent to @Lid{lua_closethread} with +@id{from} being @id{NULL}. } From 934e77a286aeb97ca02badf56956ccc78217e9d0 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 15 May 2023 10:07:25 -0300 Subject: [PATCH 0818/1145] Details - Better comments about short strings in opcodes. - luaH_newkey made static. --- lcode.c | 7 ++++--- lopcodes.h | 8 ++++---- ltable.c | 3 ++- ltable.h | 2 -- lvm.c | 8 ++++---- 5 files changed, 14 insertions(+), 14 deletions(-) diff --git a/lcode.c b/lcode.c index 1a371ca943..eade2806ea 100644 --- a/lcode.c +++ b/lcode.c @@ -1215,7 +1215,7 @@ static void codenot (FuncState *fs, expdesc *e) { /* -** Check whether expression 'e' is a small literal string +** Check whether expression 'e' is a short literal string */ static int isKstr (FuncState *fs, expdesc *e) { return (e->k == VK && !hasjumps(e) && e->u.info <= MAXARG_B && @@ -1283,15 +1283,16 @@ void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) { if (t->k == VUPVAL && !isKstr(fs, k)) /* upvalue indexed by non 'Kstr'? */ luaK_exp2anyreg(fs, t); /* put it in a register */ if (t->k == VUPVAL) { + lua_assert(isKstr(fs, k)); t->u.ind.t = t->u.info; /* upvalue index */ - t->u.ind.idx = k->u.info; /* literal string */ + t->u.ind.idx = k->u.info; /* literal short string */ t->k = VINDEXUP; } else { /* register index of the table */ t->u.ind.t = (t->k == VLOCAL) ? t->u.var.ridx: t->u.info; if (isKstr(fs, k)) { - t->u.ind.idx = k->u.info; /* literal string */ + t->u.ind.idx = k->u.info; /* literal short string */ t->k = VINDEXSTR; } else if (isCint(k)) { diff --git a/lopcodes.h b/lopcodes.h index 4c55145399..46911cac14 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -210,15 +210,15 @@ OP_LOADNIL,/* A B R[A], R[A+1], ..., R[A+B] := nil */ OP_GETUPVAL,/* A B R[A] := UpValue[B] */ OP_SETUPVAL,/* A B UpValue[B] := R[A] */ -OP_GETTABUP,/* A B C R[A] := UpValue[B][K[C]:string] */ +OP_GETTABUP,/* A B C R[A] := UpValue[B][K[C]:shortstring] */ OP_GETTABLE,/* A B C R[A] := R[B][R[C]] */ OP_GETI,/* A B C R[A] := R[B][C] */ -OP_GETFIELD,/* A B C R[A] := R[B][K[C]:string] */ +OP_GETFIELD,/* A B C R[A] := R[B][K[C]:shortstring] */ -OP_SETTABUP,/* A B C UpValue[A][K[B]:string] := RK(C) */ +OP_SETTABUP,/* A B C UpValue[A][K[B]:shortstring] := RK(C) */ OP_SETTABLE,/* A B C R[A][R[B]] := RK(C) */ OP_SETI,/* A B C R[A][B] := RK(C) */ -OP_SETFIELD,/* A B C R[A][K[B]:string] := RK(C) */ +OP_SETFIELD,/* A B C R[A][K[B]:shortstring] := RK(C) */ OP_NEWTABLE,/* A B C k R[A] := {} */ diff --git a/ltable.c b/ltable.c index 3c690c5f17..3fb575a1d6 100644 --- a/ltable.c +++ b/ltable.c @@ -662,7 +662,8 @@ static Node *getfreepos (Table *t) { ** put new key in its main position; otherwise (colliding node is in its main ** position), new key goes to an empty position. */ -void luaH_newkey (lua_State *L, Table *t, const TValue *key, TValue *value) { +static void luaH_newkey (lua_State *L, Table *t, const TValue *key, + TValue *value) { Node *mp; TValue aux; if (l_unlikely(ttisnil(key))) diff --git a/ltable.h b/ltable.h index 75dd9e26e0..8e68903423 100644 --- a/ltable.h +++ b/ltable.h @@ -41,8 +41,6 @@ LUAI_FUNC void luaH_setint (lua_State *L, Table *t, lua_Integer key, LUAI_FUNC const TValue *luaH_getshortstr (Table *t, TString *key); LUAI_FUNC const TValue *luaH_getstr (Table *t, TString *key); LUAI_FUNC const TValue *luaH_get (Table *t, const TValue *key); -LUAI_FUNC void luaH_newkey (lua_State *L, Table *t, const TValue *key, - TValue *value); LUAI_FUNC void luaH_set (lua_State *L, Table *t, const TValue *key, TValue *value); LUAI_FUNC void luaH_finishset (lua_State *L, Table *t, const TValue *key, diff --git a/lvm.c b/lvm.c index 8493a770c5..4c300a87ae 100644 --- a/lvm.c +++ b/lvm.c @@ -1253,7 +1253,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { const TValue *slot; TValue *upval = cl->upvals[GETARG_B(i)]->v.p; TValue *rc = KC(i); - TString *key = tsvalue(rc); /* key must be a string */ + TString *key = tsvalue(rc); /* key must be a short string */ if (luaV_fastget(L, upval, key, slot, luaH_getshortstr)) { setobj2s(L, ra, slot); } @@ -1296,7 +1296,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { const TValue *slot; TValue *rb = vRB(i); TValue *rc = KC(i); - TString *key = tsvalue(rc); /* key must be a string */ + TString *key = tsvalue(rc); /* key must be a short string */ if (luaV_fastget(L, rb, key, slot, luaH_getshortstr)) { setobj2s(L, ra, slot); } @@ -1309,7 +1309,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { TValue *upval = cl->upvals[GETARG_A(i)]->v.p; TValue *rb = KB(i); TValue *rc = RKC(i); - TString *key = tsvalue(rb); /* key must be a string */ + TString *key = tsvalue(rb); /* key must be a short string */ if (luaV_fastget(L, upval, key, slot, luaH_getshortstr)) { luaV_finishfastset(L, upval, slot, rc); } @@ -1352,7 +1352,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { const TValue *slot; TValue *rb = KB(i); TValue *rc = RKC(i); - TString *key = tsvalue(rb); /* key must be a string */ + TString *key = tsvalue(rb); /* key must be a short string */ if (luaV_fastget(L, s2v(ra), key, slot, luaH_getshortstr)) { luaV_finishfastset(L, s2v(ra), slot, rc); } From c197885cb00b85251c35cffdc4057efaee2d7a88 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 15 May 2023 10:20:13 -0300 Subject: [PATCH 0819/1145] Small improvements in tests --- testes/db.lua | 2 +- testes/main.lua | 43 +++++++++++++++++++++++-------------------- 2 files changed, 24 insertions(+), 21 deletions(-) diff --git a/testes/db.lua b/testes/db.lua index 02b96aca2e..67b5893404 100644 --- a/testes/db.lua +++ b/testes/db.lua @@ -928,7 +928,7 @@ do local cl = countlines(rest) -- at most 10 lines in first part, 11 in second, plus '...' assert(cl <= 10 + 11 + 1) - local brk = string.find(rest, "%.%.%.") + local brk = string.find(rest, "%.%.%.\t%(skip") if brk then -- does message have '...'? local rest1 = string.sub(rest, 1, brk) local rest2 = string.sub(rest, brk, #rest) diff --git a/testes/main.lua b/testes/main.lua index f59badcf88..3fa94e97b1 100644 --- a/testes/main.lua +++ b/testes/main.lua @@ -27,17 +27,19 @@ do end print("progname: "..progname) -local prepfile = function (s, p) - p = p or prog - io.output(p) - io.write(s) - assert(io.close()) + +local prepfile = function (s, mod, p) + mod = mod and "wb" or "w" -- mod true means binary files + p = p or prog -- file to write the program + local f = io.open(p, mod) + f:write(s) + assert(f:close()) end local function getoutput () - io.input(out) - local t = io.read("a") - io.input():close() + local f = io.open(out) + local t = f:read("a") + f:close() assert(os.remove(out)) return t end @@ -65,10 +67,11 @@ local function RUN (p, ...) assert(os.execute(s)) end + local function NoRun (msg, p, ...) p = string.gsub(p, "lua", '"'..progname..'"', 1) local s = string.format(p, ...) - s = string.format("%s 2> %s", s, out) -- will send error to 'out' + s = string.format("%s >%s 2>&1", s, out) -- send output and error to 'out' assert(not os.execute(s)) assert(string.find(getoutput(), msg, 1, true)) -- check error message end @@ -108,17 +111,17 @@ RUN('lua %s > %s', prog, out) checkout("3\n") -- bad BOMs -prepfile("\xEF") -NoRun("unexpected symbol", 'lua %s > %s', prog, out) +prepfile("\xEF", true) +NoRun("unexpected symbol", 'lua %s', prog) -prepfile("\xEF\xBB") -NoRun("unexpected symbol", 'lua %s > %s', prog, out) +prepfile("\xEF\xBB", true) +NoRun("unexpected symbol", 'lua %s', prog) -prepfile("\xEFprint(3)") -NoRun("unexpected symbol", 'lua %s > %s', prog, out) +prepfile("\xEFprint(3)", true) +NoRun("unexpected symbol", 'lua %s', prog) -prepfile("\xEF\xBBprint(3)") -NoRun("unexpected symbol", 'lua %s > %s', prog, out) +prepfile("\xEF\xBBprint(3)", true) +NoRun("unexpected symbol", 'lua %s', prog) -- test option '-' @@ -213,7 +216,7 @@ convert("a;b;;c") -- test -l over multiple libraries prepfile("print(1); a=2; return {x=15}") -prepfile(("print(a); print(_G['%s'].x)"):format(prog), otherprog) +prepfile(("print(a); print(_G['%s'].x)"):format(prog), false, otherprog) RUN('env LUA_PATH="?;;" lua -l %s -l%s -lstring -l io %s > %s', prog, otherprog, otherprog, out) checkout("1\n2\n15\n2\n15\n") @@ -237,7 +240,7 @@ RUN('lua "-e " -- %s a b c', prog) -- "-e " runs an empty command -- test 'arg' availability in libraries prepfile"assert(arg)" -prepfile("assert(arg)", otherprog) +prepfile("assert(arg)", false, otherprog) RUN('env LUA_PATH="?;;" lua -l%s - < %s', prog, otherprog) -- test messing up the 'arg' table @@ -413,7 +416,7 @@ prepfile[[#comment in 1st line without \n at the end]] RUN('lua %s', prog) -- first-line comment with binary file -prepfile("#comment\n" .. string.dump(load("print(3)"))) +prepfile("#comment\n" .. string.dump(load("print(3)")), true) RUN('lua %s > %s', prog, out) checkout('3\n') From 09f3c2372f5dbeaec9f50614a26c1b5761726a88 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 15 May 2023 13:46:38 -0300 Subject: [PATCH 0820/1145] Option '-l' discards version sufix from file name Like 'require', the command-line option '-l' discards an optional version suffix (everything after an hyphen) from a file name when creating the module name. --- loadlib.c | 9 --------- lua.c | 12 ++++++++++-- luaconf.h | 9 +++++++++ testes/main.lua | 7 +++++++ 4 files changed, 26 insertions(+), 11 deletions(-) diff --git a/loadlib.c b/loadlib.c index d792dffaa0..6d289fcebb 100644 --- a/loadlib.c +++ b/loadlib.c @@ -24,15 +24,6 @@ #include "lualib.h" -/* -** LUA_IGMARK is a mark to ignore all before it when building the -** luaopen_ function name. -*/ -#if !defined (LUA_IGMARK) -#define LUA_IGMARK "-" -#endif - - /* ** LUA_CSUBSEP is the character that replaces dots in submodule names ** when searching for a C loader. diff --git a/lua.c b/lua.c index 0ff8845453..3af5ce6a7f 100644 --- a/lua.c +++ b/lua.c @@ -210,12 +210,17 @@ static int dostring (lua_State *L, const char *s, const char *name) { /* ** Receives 'globname[=modname]' and runs 'globname = require(modname)'. +** If there is no explicit modname and globname contains a '-', cut +** the sufix after '-' (the "version") to make the global name. */ static int dolibrary (lua_State *L, char *globname) { int status; + char *suffix = NULL; char *modname = strchr(globname, '='); - if (modname == NULL) /* no explicit name? */ + if (modname == NULL) { /* no explicit name? */ modname = globname; /* module name is equal to global name */ + suffix = strchr(modname, *LUA_IGMARK); /* look for a suffix mark */ + } else { *modname = '\0'; /* global name ends here */ modname++; /* module name starts after the '=' */ @@ -223,8 +228,11 @@ static int dolibrary (lua_State *L, char *globname) { lua_getglobal(L, "require"); lua_pushstring(L, modname); status = docall(L, 1, 1); /* call 'require(modname)' */ - if (status == LUA_OK) + if (status == LUA_OK) { + if (suffix != NULL) /* is there a suffix mark? */ + *suffix = '\0'; /* remove sufix from global name */ lua_setglobal(L, globname); /* globname = require(modname) */ + } return report(L, status); } diff --git a/luaconf.h b/luaconf.h index 137103edec..acebe29c99 100644 --- a/luaconf.h +++ b/luaconf.h @@ -257,6 +257,15 @@ #endif + +/* +** LUA_IGMARK is a mark to ignore all after it when building the +** module name (e.g., used to build the luaopen_ function name). +** Typically, the sufix after the mark is the module version, +** as in "mod-v1.2.so". +*/ +#define LUA_IGMARK "-" + /* }================================================================== */ diff --git a/testes/main.lua b/testes/main.lua index 3fa94e97b1..11b14b4464 100644 --- a/testes/main.lua +++ b/testes/main.lua @@ -225,6 +225,13 @@ prepfile("print(str.upper'alo alo', m.max(10, 20))") RUN("lua -l 'str=string' '-lm=math' -e 'print(m.sin(0))' %s > %s", prog, out) checkout("0.0\nALO ALO\t20\n") + +-- test module names with version sufix ("libs/lib2-v2") +RUN("env LUA_CPATH='./libs/?.so' lua -l lib2-v2 -e 'print(lib2.id())' > %s", + out) +checkout("true\n") + + -- test 'arg' table local a = [[ assert(#arg == 3 and arg[1] == 'a' and From 351ccd733298e08c41937c1baf22a68e62bfeca9 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 15 May 2023 17:56:25 -0300 Subject: [PATCH 0821/1145] Towards a new implementation of arrays The array part of a table wastes too much space, due to padding. To avoid that, we need to store values in the array as something different from a TValue. Therefore, the API for table access should not assume that any value in a table lives in a *TValue. This commit is the first step to remove that assumption: functions luaH_get*, instead of returning a *TValue where the value lives, receive a *TValue where to put the value being accessed. (We still have to change the luaH_set* functions.) --- lapi.c | 32 ++++++++++++--------------- ltable.c | 30 +++++++++++++++++++++++++ ltable.h | 21 ++++++++++++++++++ lvm.c | 67 +++++++++++++++++++++++--------------------------------- lvm.h | 17 ++++++++++++-- 5 files changed, 108 insertions(+), 59 deletions(-) diff --git a/lapi.c b/lapi.c index 34e64af142..2e27bc92cf 100644 --- a/lapi.c +++ b/lapi.c @@ -637,16 +637,16 @@ LUA_API int lua_pushthread (lua_State *L) { l_sinline int auxgetstr (lua_State *L, const TValue *t, const char *k) { - const TValue *slot; + int aux; TString *str = luaS_new(L, k); - if (luaV_fastget(L, t, str, slot, luaH_getstr)) { - setobj2s(L, L->top.p, slot); + luaV_fastget1(t, str, s2v(L->top.p), luaH_getstr1, aux); + if (aux == HOK) { api_incr_top(L); } else { setsvalue2s(L, L->top.p, str); api_incr_top(L); - luaV_finishget(L, t, s2v(L->top.p - 1), L->top.p - 1, slot); + luaV_finishget1(L, t, s2v(L->top.p - 1), L->top.p - 1, aux); } lua_unlock(L); return ttype(s2v(L->top.p - 1)); @@ -672,15 +672,13 @@ LUA_API int lua_getglobal (lua_State *L, const char *name) { LUA_API int lua_gettable (lua_State *L, int idx) { - const TValue *slot; + int aux; TValue *t; lua_lock(L); t = index2value(L, idx); - if (luaV_fastget(L, t, s2v(L->top.p - 1), slot, luaH_get)) { - setobj2s(L, L->top.p - 1, slot); - } - else - luaV_finishget(L, t, s2v(L->top.p - 1), L->top.p - 1, slot); + luaV_fastget1(t, s2v(L->top.p - 1), s2v(L->top.p - 1), luaH_get1, aux); + if (aux != HOK) + luaV_finishget1(L, t, s2v(L->top.p - 1), L->top.p - 1, aux); lua_unlock(L); return ttype(s2v(L->top.p - 1)); } @@ -694,16 +692,14 @@ LUA_API int lua_getfield (lua_State *L, int idx, const char *k) { LUA_API int lua_geti (lua_State *L, int idx, lua_Integer n) { TValue *t; - const TValue *slot; + int aux; lua_lock(L); t = index2value(L, idx); - if (luaV_fastgeti(L, t, n, slot)) { - setobj2s(L, L->top.p, slot); - } - else { - TValue aux; - setivalue(&aux, n); - luaV_finishget(L, t, &aux, L->top.p, slot); + luaV_fastgeti1(t, n, s2v(L->top.p), aux); + if (aux != HOK) { + TValue key; + setivalue(&key, n); + luaV_finishget1(L, t, &key, L->top.p, aux); } api_incr_top(L); lua_unlock(L); diff --git a/ltable.c b/ltable.c index 3c690c5f17..8fd83fda2c 100644 --- a/ltable.c +++ b/ltable.c @@ -752,6 +752,21 @@ const TValue *luaH_getint (Table *t, lua_Integer key) { } +static int finishnodeget (const TValue *val, TValue *res) { + if (!ttisnil(val)) { + setobj(((lua_State*)NULL), res, val); + return HOK; /* success */ + } + else + return HNOTFOUND; /* could not get value */ +} + + +int luaH_getint1 (Table *t, lua_Integer key, TValue *res) { + return finishnodeget(luaH_getint(t, key), res); +} + + /* ** search function for short strings */ @@ -771,6 +786,11 @@ const TValue *luaH_getshortstr (Table *t, TString *key) { } +int luaH_getshortstr1 (Table *t, TString *key, TValue *res) { + return finishnodeget(luaH_getshortstr(t, key), res); +} + + const TValue *luaH_getstr (Table *t, TString *key) { if (key->tt == LUA_VSHRSTR) return luaH_getshortstr(t, key); @@ -782,6 +802,11 @@ const TValue *luaH_getstr (Table *t, TString *key) { } +int luaH_getstr1 (Table *t, TString *key, TValue *res) { + return finishnodeget(luaH_getstr(t, key), res); +} + + /* ** main search function */ @@ -802,6 +827,11 @@ const TValue *luaH_get (Table *t, const TValue *key) { } +int luaH_get1 (Table *t, const TValue *key, TValue *res) { + return finishnodeget(luaH_get(t, key), res); +} + + /* ** Finish a raw "set table" operation, where 'slot' is where the value ** should have been (the result of a previous "get table"). diff --git a/ltable.h b/ltable.h index 75dd9e26e0..c8a8c5e7cf 100644 --- a/ltable.h +++ b/ltable.h @@ -35,6 +35,27 @@ #define nodefromval(v) cast(Node *, (v)) +/* results from get/set */ +#define HOK 0 +#define HNOTFOUND 1 +#define HNOTATABLE 2 + + +/* fast access to components of array values */ +#define getArrTag(t,k) (&(t)->array[k - 1].tt_) +#define getArrVal(t,k) (&(t)->array[k - 1].value_) + +#define tagisempty(tag) (novariant(tag) == LUA_TNIL) + +#define arr2val(h,k,tag,res) \ + ((res)->tt_ = tag, (res)->value_ = *getArrVal(h,k)) + + +LUAI_FUNC int luaH_getshortstr1 (Table *t, TString *key, TValue *res); +LUAI_FUNC int luaH_getstr1 (Table *t, TString *key, TValue *res); +LUAI_FUNC int luaH_get1 (Table *t, const TValue *key, TValue *res); +LUAI_FUNC int luaH_getint1 (Table *t, lua_Integer key, TValue *res); + LUAI_FUNC const TValue *luaH_getint (Table *t, lua_Integer key); LUAI_FUNC void luaH_setint (lua_State *L, Table *t, lua_Integer key, TValue *value); diff --git a/lvm.c b/lvm.c index 8493a770c5..ee386847f7 100644 --- a/lvm.c +++ b/lvm.c @@ -284,12 +284,12 @@ static int floatforloop (StkId ra) { ** if 'slot' is NULL, 't' is not a table; otherwise, 'slot' points to ** t[k] entry (which must be empty). */ -void luaV_finishget (lua_State *L, const TValue *t, TValue *key, StkId val, - const TValue *slot) { +void luaV_finishget1 (lua_State *L, const TValue *t, TValue *key, StkId val, + int aux) { int loop; /* counter to avoid infinite loops */ const TValue *tm; /* metamethod */ for (loop = 0; loop < MAXTAGLOOP; loop++) { - if (slot == NULL) { /* 't' is not a table? */ + if (aux == HNOTATABLE) { /* 't' is not a table? */ lua_assert(!ttistable(t)); tm = luaT_gettmbyobj(L, t, TM_INDEX); if (l_unlikely(notm(tm))) @@ -297,7 +297,6 @@ void luaV_finishget (lua_State *L, const TValue *t, TValue *key, StkId val, /* else will try the metamethod */ } else { /* 't' is a table */ - lua_assert(isempty(slot)); tm = fasttm(L, hvalue(t)->metatable, TM_INDEX); /* table's metamethod */ if (tm == NULL) { /* no metamethod? */ setnilvalue(s2v(val)); /* result is nil */ @@ -310,10 +309,9 @@ void luaV_finishget (lua_State *L, const TValue *t, TValue *key, StkId val, return; } t = tm; /* else try to access 'tm[key]' */ - if (luaV_fastget(L, t, key, slot, luaH_get)) { /* fast track? */ - setobj2s(L, val, slot); /* done */ - return; - } + luaV_fastget1(t, key, s2v(val), luaH_get1, aux); + if (aux == HOK) + return; /* done */ /* else repeat (tail call 'luaV_finishget') */ } luaG_runerror(L, "'__index' chain too long; possible loop"); @@ -1250,58 +1248,51 @@ void luaV_execute (lua_State *L, CallInfo *ci) { } vmcase(OP_GETTABUP) { StkId ra = RA(i); - const TValue *slot; TValue *upval = cl->upvals[GETARG_B(i)]->v.p; TValue *rc = KC(i); TString *key = tsvalue(rc); /* key must be a string */ - if (luaV_fastget(L, upval, key, slot, luaH_getshortstr)) { - setobj2s(L, ra, slot); - } - else - Protect(luaV_finishget(L, upval, rc, ra, slot)); + int aux; + luaV_fastget1(upval, key, s2v(ra), luaH_getshortstr1, aux); + if (aux != HOK) + Protect(luaV_finishget1(L, upval, rc, ra, aux)); vmbreak; } vmcase(OP_GETTABLE) { StkId ra = RA(i); - const TValue *slot; TValue *rb = vRB(i); TValue *rc = vRC(i); - lua_Unsigned n; - if (ttisinteger(rc) /* fast track for integers? */ - ? (cast_void(n = ivalue(rc)), luaV_fastgeti(L, rb, n, slot)) - : luaV_fastget(L, rb, rc, slot, luaH_get)) { - setobj2s(L, ra, slot); + int aux; + if (ttisinteger(rc)) { /* fast track for integers? */ + luaV_fastgeti1(rb, ivalue(rc), s2v(ra), aux); } else - Protect(luaV_finishget(L, rb, rc, ra, slot)); + luaV_fastget1(rb, rc, s2v(ra), luaH_get1, aux); + if (aux != HOK) /* fast track for integers? */ + Protect(luaV_finishget1(L, rb, rc, ra, aux)); vmbreak; } vmcase(OP_GETI) { StkId ra = RA(i); - const TValue *slot; TValue *rb = vRB(i); int c = GETARG_C(i); - if (luaV_fastgeti(L, rb, c, slot)) { - setobj2s(L, ra, slot); - } - else { + int aux; + luaV_fastgeti1(rb, c, s2v(ra), aux); + if (aux != HOK) { TValue key; setivalue(&key, c); - Protect(luaV_finishget(L, rb, &key, ra, slot)); + Protect(luaV_finishget1(L, rb, &key, ra, aux)); } vmbreak; } vmcase(OP_GETFIELD) { StkId ra = RA(i); - const TValue *slot; TValue *rb = vRB(i); TValue *rc = KC(i); TString *key = tsvalue(rc); /* key must be a string */ - if (luaV_fastget(L, rb, key, slot, luaH_getshortstr)) { - setobj2s(L, ra, slot); - } - else - Protect(luaV_finishget(L, rb, rc, ra, slot)); + int aux; + luaV_fastget1(rb, key, s2v(ra), luaH_getshortstr1, aux); + if (aux != HOK) + Protect(luaV_finishget1(L, rb, rc, ra, aux)); vmbreak; } vmcase(OP_SETTABUP) { @@ -1381,16 +1372,14 @@ void luaV_execute (lua_State *L, CallInfo *ci) { } vmcase(OP_SELF) { StkId ra = RA(i); - const TValue *slot; + int aux; TValue *rb = vRB(i); TValue *rc = RKC(i); TString *key = tsvalue(rc); /* key must be a string */ setobj2s(L, ra + 1, rb); - if (luaV_fastget(L, rb, key, slot, luaH_getstr)) { - setobj2s(L, ra, slot); - } - else - Protect(luaV_finishget(L, rb, rc, ra, slot)); + luaV_fastget1(rb, key, s2v(ra), luaH_getstr1, aux); + if (aux != HOK) + Protect(luaV_finishget1(L, rb, rc, ra, aux)); vmbreak; } vmcase(OP_ADDI) { diff --git a/lvm.h b/lvm.h index dba1ad2770..704446c223 100644 --- a/lvm.h +++ b/lvm.h @@ -89,6 +89,10 @@ typedef enum { !isempty(slot))) /* result not empty? */ +#define luaV_fastget1(t,k,res,f, aux) \ + (aux = (!ttistable(t) ? HNOTATABLE : f(hvalue(t), k, res))) + + /* ** Special case of 'luaV_fastget' for integers, inlining the fast case ** of 'luaH_getint'. @@ -100,6 +104,15 @@ typedef enum { ? &hvalue(t)->array[k - 1] : luaH_getint(hvalue(t), k), \ !isempty(slot))) /* result not empty? */ +#define luaV_fastgeti1(t,k,val,aux) \ + if (!ttistable(t)) aux = HNOTATABLE; \ + else { Table *h = hvalue(t); lua_Unsigned u = l_castS2U(k); \ + if ((u - 1u < h->alimit)) { \ + int tag = *getArrTag(h,u); \ + if (tagisempty(tag)) aux = HNOTFOUND; \ + else { arr2val(h, u, tag, val); aux = HOK; }} \ + else { aux = luaH_getint1(h, u, val); }} + /* ** Finish a fast set operation (when fast get succeeds). In that case, @@ -125,8 +138,8 @@ LUAI_FUNC int luaV_tointeger (const TValue *obj, lua_Integer *p, F2Imod mode); LUAI_FUNC int luaV_tointegerns (const TValue *obj, lua_Integer *p, F2Imod mode); LUAI_FUNC int luaV_flttointeger (lua_Number n, lua_Integer *p, F2Imod mode); -LUAI_FUNC void luaV_finishget (lua_State *L, const TValue *t, TValue *key, - StkId val, const TValue *slot); +LUAI_FUNC void luaV_finishget1 (lua_State *L, const TValue *t, TValue *key, + StkId val, int aux); LUAI_FUNC void luaV_finishset (lua_State *L, const TValue *t, TValue *key, TValue *val, const TValue *slot); LUAI_FUNC void luaV_finishOp (lua_State *L); From f8d30826dda6ee8e99200de57a1997734b853db2 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 16 May 2023 14:55:49 -0300 Subject: [PATCH 0822/1145] New table API for 'set' functions --- lapi.c | 31 +++++++------- ltable.c | 120 +++++++++++++++++++++++++++++++++++++++++++++---------- ltable.h | 11 +++++ lvm.c | 64 ++++++++++++++--------------- lvm.h | 25 +++++++++--- 5 files changed, 180 insertions(+), 71 deletions(-) diff --git a/lapi.c b/lapi.c index 2e27bc92cf..97a9f272aa 100644 --- a/lapi.c +++ b/lapi.c @@ -823,17 +823,18 @@ LUA_API int lua_getiuservalue (lua_State *L, int idx, int n) { ** t[k] = value at the top of the stack (where 'k' is a string) */ static void auxsetstr (lua_State *L, const TValue *t, const char *k) { - const TValue *slot; + int aux; TString *str = luaS_new(L, k); api_checknelems(L, 1); - if (luaV_fastget(L, t, str, slot, luaH_getstr)) { - luaV_finishfastset(L, t, slot, s2v(L->top.p - 1)); + luaV_fastset1(t, str, s2v(L->top.p - 1), aux, luaH_setstr1); + if (aux == HOK) { + luaV_finishfastset1(L, t, s2v(L->top.p - 1)); L->top.p--; /* pop value */ } else { setsvalue2s(L, L->top.p, str); /* push 'str' (to make it a TValue) */ api_incr_top(L); - luaV_finishset(L, t, s2v(L->top.p - 1), s2v(L->top.p - 2), slot); + luaV_finishset1(L, t, s2v(L->top.p - 1), s2v(L->top.p - 2), aux); L->top.p -= 2; /* pop value and key */ } lua_unlock(L); /* lock done by caller */ @@ -850,15 +851,16 @@ LUA_API void lua_setglobal (lua_State *L, const char *name) { LUA_API void lua_settable (lua_State *L, int idx) { TValue *t; - const TValue *slot; + int aux; lua_lock(L); api_checknelems(L, 2); t = index2value(L, idx); - if (luaV_fastget(L, t, s2v(L->top.p - 2), slot, luaH_get)) { - luaV_finishfastset(L, t, slot, s2v(L->top.p - 1)); + luaV_fastset1(t, s2v(L->top.p - 2), s2v(L->top.p - 1), aux, luaH_set1); + if (aux == HOK) { + luaV_finishfastset1(L, t, s2v(L->top.p - 1)); } else - luaV_finishset(L, t, s2v(L->top.p - 2), s2v(L->top.p - 1), slot); + luaV_finishset1(L, t, s2v(L->top.p - 2), s2v(L->top.p - 1), aux); L->top.p -= 2; /* pop index and value */ lua_unlock(L); } @@ -872,17 +874,18 @@ LUA_API void lua_setfield (lua_State *L, int idx, const char *k) { LUA_API void lua_seti (lua_State *L, int idx, lua_Integer n) { TValue *t; - const TValue *slot; + int aux; lua_lock(L); api_checknelems(L, 1); t = index2value(L, idx); - if (luaV_fastgeti(L, t, n, slot)) { - luaV_finishfastset(L, t, slot, s2v(L->top.p - 1)); + luaV_fastseti1(t, n, s2v(L->top.p - 1), aux); + if (aux == HOK) { + luaV_finishfastset1(L, t, s2v(L->top.p - 1)); } else { - TValue aux; - setivalue(&aux, n); - luaV_finishset(L, t, &aux, s2v(L->top.p - 1), slot); + TValue temp; + setivalue(&temp, n); + luaV_finishset1(L, t, &temp, s2v(L->top.p - 1), aux); } L->top.p--; /* pop value */ lua_unlock(L); diff --git a/ltable.c b/ltable.c index 8fd83fda2c..902f05a785 100644 --- a/ltable.c +++ b/ltable.c @@ -719,15 +719,7 @@ void luaH_newkey (lua_State *L, Table *t, const TValue *key, TValue *value) { } -/* -** Search function for integers. If integer is inside 'alimit', get it -** directly from the array part. Otherwise, if 'alimit' is not equal to -** the real size of the array, key still can be in the array part. In -** this case, try to avoid a call to 'luaH_realasize' when key is just -** one more than the limit (so that it can be incremented without -** changing the real size of the array). -*/ -const TValue *luaH_getint (Table *t, lua_Integer key) { +static const TValue *getintfromarray (Table *t, lua_Integer key) { if (l_castS2U(key) - 1u < t->alimit) /* 'key' in [1, t->alimit]? */ return &t->array[key - 1]; else if (!limitequalsasize(t) && /* key still may be in the array part? */ @@ -736,19 +728,40 @@ const TValue *luaH_getint (Table *t, lua_Integer key) { t->alimit = cast_uint(key); /* probably '#t' is here now */ return &t->array[key - 1]; } - else { - Node *n = hashint(t, key); - for (;;) { /* check whether 'key' is somewhere in the chain */ - if (keyisinteger(n) && keyival(n) == key) - return gval(n); /* that's it */ - else { - int nx = gnext(n); - if (nx == 0) break; - n += nx; - } + else return NULL; /* key is not in the array part */ +} + + +static const TValue *getintfromhash (Table *t, lua_Integer key) { + Node *n = hashint(t, key); + lua_assert(l_castS2U(key) - 1u >= luaH_realasize(t)); + for (;;) { /* check whether 'key' is somewhere in the chain */ + if (keyisinteger(n) && keyival(n) == key) + return gval(n); /* that's it */ + else { + int nx = gnext(n); + if (nx == 0) break; + n += nx; } - return &absentkey; } + return &absentkey; +} + + +/* +** Search function for integers. If integer is inside 'alimit', get it +** directly from the array part. Otherwise, if 'alimit' is not equal to +** the real size of the array, key still can be in the array part. In +** this case, try to avoid a call to 'luaH_realasize' when key is just +** one more than the limit (so that it can be incremented without +** changing the real size of the array). +*/ +const TValue *luaH_getint (Table *t, lua_Integer key) { + const TValue *slot = getintfromarray(t, key); + if (slot != NULL) + return slot; + else + return getintfromhash(t, key); } @@ -832,6 +845,58 @@ int luaH_get1 (Table *t, const TValue *key, TValue *res) { } +static int finishnodeset (Table *t, const TValue *slot, TValue *val) { + if (!ttisnil(slot)) { + setobj(((lua_State*)NULL), cast(TValue*, slot), val); + return HOK; /* success */ + } + else if (isabstkey(slot)) + return HNOTFOUND; /* no slot with that key */ + else return (cast(Node*, slot) - t->node) + HFIRSTNODE; /* node encoded */ +} + + +int luaH_setint1 (Table *t, lua_Integer key, TValue *val) { + const TValue *slot = getintfromarray(t, key); + if (slot != NULL) { + if (!ttisnil(slot)) { + setobj(((lua_State*)NULL), cast(TValue*, slot), val); + return HOK; /* success */ + } + else + return ~cast_int(key); /* empty slot in the array part */ + } + else + return finishnodeset(t, getintfromhash(t, key), val); +} + + +int luaH_setshortstr1 (Table *t, TString *key, TValue *val) { + return finishnodeset(t, luaH_getshortstr(t, key), val); +} + + +int luaH_setstr1 (Table *t, TString *key, TValue *val) { + return finishnodeset(t, luaH_getstr(t, key), val); +} + + +int luaH_set1 (Table *t, const TValue *key, TValue *val) { + switch (ttypetag(key)) { + case LUA_VSHRSTR: return luaH_setshortstr1(t, tsvalue(key), val); + case LUA_VNUMINT: return luaH_setint1(t, ivalue(key), val); + case LUA_VNIL: return HNOTFOUND; + case LUA_VNUMFLT: { + lua_Integer k; + if (luaV_flttointeger(fltvalue(key), &k, F2Ieq)) /* integral index? */ + return luaH_setint1(t, k, val); /* use specialized version */ + /* else... */ + } /* FALLTHROUGH */ + default: + return finishnodeset(t, getgeneric(t, key, 0), val); + } +} + /* ** Finish a raw "set table" operation, where 'slot' is where the value ** should have been (the result of a previous "get table"). @@ -847,6 +912,21 @@ void luaH_finishset (lua_State *L, Table *t, const TValue *key, } +void luaH_finishset1 (lua_State *L, Table *t, const TValue *key, + TValue *value, int aux) { + if (aux == HNOTFOUND) { + luaH_newkey(L, t, key, value); + } + else if (aux > 0) { /* regular Node? */ + setobj2t(L, gval(gnode(t, aux - HFIRSTNODE)), value); + } + else { /* array entry */ + aux = ~aux; /* real index */ + val2arr(t, aux, getArrTag(t, aux), value); + } +} + + /* ** beware: when using this function you probably need to check a GC ** barrier and invalidate the TM cache. diff --git a/ltable.h b/ltable.h index c8a8c5e7cf..b9bb416a78 100644 --- a/ltable.h +++ b/ltable.h @@ -39,6 +39,7 @@ #define HOK 0 #define HNOTFOUND 1 #define HNOTATABLE 2 +#define HFIRSTNODE 3 /* fast access to components of array values */ @@ -50,12 +51,20 @@ #define arr2val(h,k,tag,res) \ ((res)->tt_ = tag, (res)->value_ = *getArrVal(h,k)) +#define val2arr(h,k,tag,val) \ + (*tag = (val)->tt_, *getArrVal(h,k) = (val)->value_) + LUAI_FUNC int luaH_getshortstr1 (Table *t, TString *key, TValue *res); LUAI_FUNC int luaH_getstr1 (Table *t, TString *key, TValue *res); LUAI_FUNC int luaH_get1 (Table *t, const TValue *key, TValue *res); LUAI_FUNC int luaH_getint1 (Table *t, lua_Integer key, TValue *res); +LUAI_FUNC int luaH_setint1 (Table *t, lua_Integer key, TValue *val); +LUAI_FUNC int luaH_setshortstr1 (Table *t, TString *key, TValue *val); +LUAI_FUNC int luaH_setstr1 (Table *t, TString *key, TValue *val); +LUAI_FUNC int luaH_set1 (Table *t, const TValue *key, TValue *val); + LUAI_FUNC const TValue *luaH_getint (Table *t, lua_Integer key); LUAI_FUNC void luaH_setint (lua_State *L, Table *t, lua_Integer key, TValue *value); @@ -68,6 +77,8 @@ LUAI_FUNC void luaH_set (lua_State *L, Table *t, const TValue *key, TValue *value); LUAI_FUNC void luaH_finishset (lua_State *L, Table *t, const TValue *key, const TValue *slot, TValue *value); +LUAI_FUNC void luaH_finishset1 (lua_State *L, Table *t, const TValue *key, + TValue *value, int aux); LUAI_FUNC Table *luaH_new (lua_State *L); LUAI_FUNC void luaH_resize (lua_State *L, Table *t, unsigned int nasize, unsigned int nhsize); diff --git a/lvm.c b/lvm.c index ee386847f7..f593402559 100644 --- a/lvm.c +++ b/lvm.c @@ -325,17 +325,16 @@ void luaV_finishget1 (lua_State *L, const TValue *t, TValue *key, StkId val, ** is no such entry. (The value at 'slot' must be empty, otherwise ** 'luaV_fastget' would have done the job.) */ -void luaV_finishset (lua_State *L, const TValue *t, TValue *key, - TValue *val, const TValue *slot) { +void luaV_finishset1 (lua_State *L, const TValue *t, TValue *key, + TValue *val, int aux) { int loop; /* counter to avoid infinite loops */ for (loop = 0; loop < MAXTAGLOOP; loop++) { const TValue *tm; /* '__newindex' metamethod */ - if (slot != NULL) { /* is 't' a table? */ + if (aux != HNOTATABLE) { /* is 't' a table? */ Table *h = hvalue(t); /* save 't' table */ - lua_assert(isempty(slot)); /* slot must be empty */ tm = fasttm(L, h->metatable, TM_NEWINDEX); /* get metamethod */ if (tm == NULL) { /* no metamethod? */ - luaH_finishset(L, h, key, slot, val); /* set new value */ + luaH_finishset1(L, h, key, val, aux); /* set new value */ invalidateTMcache(h); luaC_barrierback(L, obj2gco(h), val); return; @@ -353,10 +352,9 @@ void luaV_finishset (lua_State *L, const TValue *t, TValue *key, return; } t = tm; /* else repeat assignment over 'tm' */ - if (luaV_fastget(L, t, key, slot, luaH_get)) { - luaV_finishfastset(L, t, slot, val); + luaV_fastset1(t, key, val, aux, luaH_set1); + if (aux == HOK) return; /* done */ - } /* else 'return luaV_finishset(L, t, key, val, slot)' (loop) */ } luaG_runerror(L, "'__newindex' chain too long; possible loop"); @@ -1296,59 +1294,61 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_SETTABUP) { - const TValue *slot; + int aux; TValue *upval = cl->upvals[GETARG_A(i)]->v.p; TValue *rb = KB(i); TValue *rc = RKC(i); TString *key = tsvalue(rb); /* key must be a string */ - if (luaV_fastget(L, upval, key, slot, luaH_getshortstr)) { - luaV_finishfastset(L, upval, slot, rc); - } + luaV_fastset1(upval, key, rc, aux, luaH_setshortstr1); + if (aux == HOK) + luaV_finishfastset1(L, upval, rc); else - Protect(luaV_finishset(L, upval, rb, rc, slot)); + Protect(luaV_finishset1(L, upval, rb, rc, aux)); vmbreak; } vmcase(OP_SETTABLE) { StkId ra = RA(i); - const TValue *slot; + int aux; TValue *rb = vRB(i); /* key (table is in 'ra') */ TValue *rc = RKC(i); /* value */ - lua_Unsigned n; - if (ttisinteger(rb) /* fast track for integers? */ - ? (cast_void(n = ivalue(rb)), luaV_fastgeti(L, s2v(ra), n, slot)) - : luaV_fastget(L, s2v(ra), rb, slot, luaH_get)) { - luaV_finishfastset(L, s2v(ra), slot, rc); + if (ttisinteger(rb)) { /* fast track for integers? */ + luaV_fastseti1(s2v(ra), ivalue(rb), rc, aux); + } + else { + luaV_fastset1(s2v(ra), rb, rc, aux, luaH_set1); } + if (aux == HOK) + luaV_finishfastset1(L, s2v(ra), rc); else - Protect(luaV_finishset(L, s2v(ra), rb, rc, slot)); + Protect(luaV_finishset1(L, s2v(ra), rb, rc, aux)); vmbreak; } vmcase(OP_SETI) { StkId ra = RA(i); - const TValue *slot; - int c = GETARG_B(i); + int aux; + int b = GETARG_B(i); TValue *rc = RKC(i); - if (luaV_fastgeti(L, s2v(ra), c, slot)) { - luaV_finishfastset(L, s2v(ra), slot, rc); - } + luaV_fastseti1(s2v(ra), b, rc, aux); + if (aux == HOK) + luaV_finishfastset1(L, s2v(ra), rc); else { TValue key; - setivalue(&key, c); - Protect(luaV_finishset(L, s2v(ra), &key, rc, slot)); + setivalue(&key, b); + Protect(luaV_finishset1(L, s2v(ra), &key, rc, aux)); } vmbreak; } vmcase(OP_SETFIELD) { StkId ra = RA(i); - const TValue *slot; + int aux; TValue *rb = KB(i); TValue *rc = RKC(i); TString *key = tsvalue(rb); /* key must be a string */ - if (luaV_fastget(L, s2v(ra), key, slot, luaH_getshortstr)) { - luaV_finishfastset(L, s2v(ra), slot, rc); - } + luaV_fastset1(s2v(ra), key, rc, aux, luaH_setshortstr1); + if (aux == HOK) + luaV_finishfastset1(L, s2v(ra), rc); else - Protect(luaV_finishset(L, s2v(ra), rb, rc, slot)); + Protect(luaV_finishset1(L, s2v(ra), rb, rc, aux)); vmbreak; } vmcase(OP_NEWTABLE) { diff --git a/lvm.h b/lvm.h index 704446c223..750a22b261 100644 --- a/lvm.h +++ b/lvm.h @@ -104,14 +104,27 @@ typedef enum { ? &hvalue(t)->array[k - 1] : luaH_getint(hvalue(t), k), \ !isempty(slot))) /* result not empty? */ -#define luaV_fastgeti1(t,k,val,aux) \ +#define luaV_fastgeti1(t,k,res,aux) \ if (!ttistable(t)) aux = HNOTATABLE; \ else { Table *h = hvalue(t); lua_Unsigned u = l_castS2U(k); \ if ((u - 1u < h->alimit)) { \ int tag = *getArrTag(h,u); \ if (tagisempty(tag)) aux = HNOTFOUND; \ - else { arr2val(h, u, tag, val); aux = HOK; }} \ - else { aux = luaH_getint1(h, u, val); }} + else { arr2val(h, u, tag, res); aux = HOK; }} \ + else { aux = luaH_getint1(h, u, res); }} + + +#define luaV_fastset1(t,k,val,aux,f) \ + (aux = (!ttistable(t) ? HNOTATABLE : f(hvalue(t), k, val))) + +#define luaV_fastseti1(t,k,val,aux) \ + if (!ttistable(t)) aux = HNOTATABLE; \ + else { Table *h = hvalue(t); lua_Unsigned u = l_castS2U(k); \ + if ((u - 1u < h->alimit)) { \ + lu_byte *tag = getArrTag(h,u); \ + if (tagisempty(*tag)) aux = ~cast_int(u); \ + else { val2arr(h, u, tag, val); aux = HOK; }} \ + else { aux = luaH_setint1(h, u, val); }} /* @@ -122,6 +135,8 @@ typedef enum { { setobj2t(L, cast(TValue *,slot), v); \ luaC_barrierback(L, gcvalue(t), v); } +#define luaV_finishfastset1(L,t,v) luaC_barrierback(L, gcvalue(t), v) + /* ** Shift right is the same as shift left with a negative 'y' @@ -140,8 +155,8 @@ LUAI_FUNC int luaV_tointegerns (const TValue *obj, lua_Integer *p, LUAI_FUNC int luaV_flttointeger (lua_Number n, lua_Integer *p, F2Imod mode); LUAI_FUNC void luaV_finishget1 (lua_State *L, const TValue *t, TValue *key, StkId val, int aux); -LUAI_FUNC void luaV_finishset (lua_State *L, const TValue *t, TValue *key, - TValue *val, const TValue *slot); +LUAI_FUNC void luaV_finishset1 (lua_State *L, const TValue *t, TValue *key, + TValue *val, int aux); LUAI_FUNC void luaV_finishOp (lua_State *L); LUAI_FUNC void luaV_execute (lua_State *L, CallInfo *ci); LUAI_FUNC void luaV_concat (lua_State *L, int total); From 819bd51d87b799fdee029754c660dc9a5587ef57 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 16 May 2023 16:53:29 -0300 Subject: [PATCH 0823/1145] Some cleaning in the new table API --- lapi.c | 70 +++++++++++++++++----------------- lcode.c | 8 ++-- llex.c | 10 ++--- ltable.c | 102 +++++++++++++++++++++++++------------------------- ltable.h | 37 ++++++++++-------- ltm.c | 11 +++--- lvm.c | 112 ++++++++++++++++++++++++++----------------------------- lvm.h | 49 +++++++----------------- 8 files changed, 189 insertions(+), 210 deletions(-) diff --git a/lapi.c b/lapi.c index 97a9f272aa..4a3ba72438 100644 --- a/lapi.c +++ b/lapi.c @@ -637,16 +637,16 @@ LUA_API int lua_pushthread (lua_State *L) { l_sinline int auxgetstr (lua_State *L, const TValue *t, const char *k) { - int aux; + int hres; TString *str = luaS_new(L, k); - luaV_fastget1(t, str, s2v(L->top.p), luaH_getstr1, aux); - if (aux == HOK) { + luaV_fastget(t, str, s2v(L->top.p), luaH_getstr, hres); + if (hres == HOK) { api_incr_top(L); } else { setsvalue2s(L, L->top.p, str); api_incr_top(L); - luaV_finishget1(L, t, s2v(L->top.p - 1), L->top.p - 1, aux); + luaV_finishget(L, t, s2v(L->top.p - 1), L->top.p - 1, hres); } lua_unlock(L); return ttype(s2v(L->top.p - 1)); @@ -672,13 +672,13 @@ LUA_API int lua_getglobal (lua_State *L, const char *name) { LUA_API int lua_gettable (lua_State *L, int idx) { - int aux; + int hres; TValue *t; lua_lock(L); t = index2value(L, idx); - luaV_fastget1(t, s2v(L->top.p - 1), s2v(L->top.p - 1), luaH_get1, aux); - if (aux != HOK) - luaV_finishget1(L, t, s2v(L->top.p - 1), L->top.p - 1, aux); + luaV_fastget(t, s2v(L->top.p - 1), s2v(L->top.p - 1), luaH_get, hres); + if (hres != HOK) + luaV_finishget(L, t, s2v(L->top.p - 1), L->top.p - 1, hres); lua_unlock(L); return ttype(s2v(L->top.p - 1)); } @@ -692,14 +692,14 @@ LUA_API int lua_getfield (lua_State *L, int idx, const char *k) { LUA_API int lua_geti (lua_State *L, int idx, lua_Integer n) { TValue *t; - int aux; + int hres; lua_lock(L); t = index2value(L, idx); - luaV_fastgeti1(t, n, s2v(L->top.p), aux); - if (aux != HOK) { + luaV_fastgeti(t, n, s2v(L->top.p), hres); + if (hres != HOK) { TValue key; setivalue(&key, n); - luaV_finishget1(L, t, &key, L->top.p, aux); + luaV_finishget(L, t, &key, L->top.p, hres); } api_incr_top(L); lua_unlock(L); @@ -707,11 +707,9 @@ LUA_API int lua_geti (lua_State *L, int idx, lua_Integer n) { } -l_sinline int finishrawget (lua_State *L, const TValue *val) { - if (isempty(val)) /* avoid copying empty items to the stack */ +l_sinline int finishrawget (lua_State *L, int hres) { + if (hres != HOK) /* avoid copying empty items to the stack */ setnilvalue(s2v(L->top.p)); - else - setobj2s(L, L->top.p, val); api_incr_top(L); lua_unlock(L); return ttype(s2v(L->top.p - 1)); @@ -727,13 +725,13 @@ static Table *gettable (lua_State *L, int idx) { LUA_API int lua_rawget (lua_State *L, int idx) { Table *t; - const TValue *val; + int hres; lua_lock(L); api_checknelems(L, 1); t = gettable(L, idx); - val = luaH_get(t, s2v(L->top.p - 1)); + hres = luaH_get(t, s2v(L->top.p - 1), s2v(L->top.p - 1)); L->top.p--; /* remove key */ - return finishrawget(L, val); + return finishrawget(L, hres); } @@ -741,7 +739,7 @@ LUA_API int lua_rawgeti (lua_State *L, int idx, lua_Integer n) { Table *t; lua_lock(L); t = gettable(L, idx); - return finishrawget(L, luaH_getint(t, n)); + return finishrawget(L, luaH_getint(t, n, s2v(L->top.p))); } @@ -751,7 +749,7 @@ LUA_API int lua_rawgetp (lua_State *L, int idx, const void *p) { lua_lock(L); t = gettable(L, idx); setpvalue(&k, cast_voidp(p)); - return finishrawget(L, luaH_get(t, &k)); + return finishrawget(L, luaH_get(t, &k, s2v(L->top.p))); } @@ -823,18 +821,18 @@ LUA_API int lua_getiuservalue (lua_State *L, int idx, int n) { ** t[k] = value at the top of the stack (where 'k' is a string) */ static void auxsetstr (lua_State *L, const TValue *t, const char *k) { - int aux; + int hres; TString *str = luaS_new(L, k); api_checknelems(L, 1); - luaV_fastset1(t, str, s2v(L->top.p - 1), aux, luaH_setstr1); - if (aux == HOK) { - luaV_finishfastset1(L, t, s2v(L->top.p - 1)); + luaV_fastset(t, str, s2v(L->top.p - 1), hres, luaH_psetstr); + if (hres == HOK) { + luaV_finishfastset(L, t, s2v(L->top.p - 1)); L->top.p--; /* pop value */ } else { setsvalue2s(L, L->top.p, str); /* push 'str' (to make it a TValue) */ api_incr_top(L); - luaV_finishset1(L, t, s2v(L->top.p - 1), s2v(L->top.p - 2), aux); + luaV_finishset(L, t, s2v(L->top.p - 1), s2v(L->top.p - 2), hres); L->top.p -= 2; /* pop value and key */ } lua_unlock(L); /* lock done by caller */ @@ -851,16 +849,16 @@ LUA_API void lua_setglobal (lua_State *L, const char *name) { LUA_API void lua_settable (lua_State *L, int idx) { TValue *t; - int aux; + int hres; lua_lock(L); api_checknelems(L, 2); t = index2value(L, idx); - luaV_fastset1(t, s2v(L->top.p - 2), s2v(L->top.p - 1), aux, luaH_set1); - if (aux == HOK) { - luaV_finishfastset1(L, t, s2v(L->top.p - 1)); + luaV_fastset(t, s2v(L->top.p - 2), s2v(L->top.p - 1), hres, luaH_pset); + if (hres == HOK) { + luaV_finishfastset(L, t, s2v(L->top.p - 1)); } else - luaV_finishset1(L, t, s2v(L->top.p - 2), s2v(L->top.p - 1), aux); + luaV_finishset(L, t, s2v(L->top.p - 2), s2v(L->top.p - 1), hres); L->top.p -= 2; /* pop index and value */ lua_unlock(L); } @@ -874,18 +872,18 @@ LUA_API void lua_setfield (lua_State *L, int idx, const char *k) { LUA_API void lua_seti (lua_State *L, int idx, lua_Integer n) { TValue *t; - int aux; + int hres; lua_lock(L); api_checknelems(L, 1); t = index2value(L, idx); - luaV_fastseti1(t, n, s2v(L->top.p - 1), aux); - if (aux == HOK) { - luaV_finishfastset1(L, t, s2v(L->top.p - 1)); + luaV_fastseti(t, n, s2v(L->top.p - 1), hres); + if (hres == HOK) { + luaV_finishfastset(L, t, s2v(L->top.p - 1)); } else { TValue temp; setivalue(&temp, n); - luaV_finishset1(L, t, &temp, s2v(L->top.p - 1), aux); + luaV_finishset(L, t, &temp, s2v(L->top.p - 1), hres); } L->top.p--; /* pop value */ lua_unlock(L); diff --git a/lcode.c b/lcode.c index 1a371ca943..25623df7af 100644 --- a/lcode.c +++ b/lcode.c @@ -544,10 +544,10 @@ static int addk (FuncState *fs, TValue *key, TValue *v) { TValue val; lua_State *L = fs->ls->L; Proto *f = fs->f; - const TValue *idx = luaH_get(fs->ls->h, key); /* query scanner table */ + int aux = luaH_get(fs->ls->h, key, &val); /* query scanner table */ int k, oldsize; - if (ttisinteger(idx)) { /* is there an index there? */ - k = cast_int(ivalue(idx)); + if (aux == HOK && ttisinteger(&val)) { /* is there an index there? */ + k = cast_int(ivalue(&val)); /* correct value? (warning: must distinguish floats from integers!) */ if (k < fs->nk && ttypetag(&f->k[k]) == ttypetag(v) && luaV_rawequalobj(&f->k[k], v)) @@ -559,7 +559,7 @@ static int addk (FuncState *fs, TValue *key, TValue *v) { /* numerical value does not need GC barrier; table has no metatable, so it does not need to invalidate cache */ setivalue(&val, k); - luaH_finishset(L, fs->ls->h, key, idx, &val); + luaH_set(L, fs->ls->h, key, &val); luaM_growvector(L, f->k, k, f->sizek, TValue, MAXARG_Ax, "constants"); while (oldsize < f->sizek) setnilvalue(&f->k[oldsize++]); setobj(L, &f->k[k], v); diff --git a/llex.c b/llex.c index 5fc39a5cde..9f20d3c836 100644 --- a/llex.c +++ b/llex.c @@ -134,13 +134,13 @@ l_noret luaX_syntaxerror (LexState *ls, const char *msg) { TString *luaX_newstring (LexState *ls, const char *str, size_t l) { lua_State *L = ls->L; TString *ts = luaS_newlstr(L, str, l); /* create new string */ - const TValue *o = luaH_getstr(ls->h, ts); - if (!ttisnil(o)) /* string already present? */ - ts = keystrval(nodefromval(o)); /* get saved copy */ - else { /* not in use yet */ + TString *oldts = luaH_getstrkey(ls->h, ts); + if (oldts != NULL) /* string already present? */ + return oldts; /* use it */ + else { /* create a new entry */ TValue *stv = s2v(L->top.p++); /* reserve stack space for string */ setsvalue(L, stv, ts); /* temporarily anchor the string */ - luaH_finishset(L, ls->h, stv, o, stv); /* t[string] = string */ + luaH_set(L, ls->h, stv, stv); /* t[string] = string */ /* table is not a metatable, so it does not need to invalidate cache */ luaC_checkGC(L); L->top.p--; /* remove string from stack */ diff --git a/ltable.c b/ltable.c index 902f05a785..3f95ab0c60 100644 --- a/ltable.c +++ b/ltable.c @@ -756,7 +756,7 @@ static const TValue *getintfromhash (Table *t, lua_Integer key) { ** one more than the limit (so that it can be incremented without ** changing the real size of the array). */ -const TValue *luaH_getint (Table *t, lua_Integer key) { +static const TValue *Hgetint (Table *t, lua_Integer key) { const TValue *slot = getintfromarray(t, key); if (slot != NULL) return slot; @@ -775,15 +775,15 @@ static int finishnodeget (const TValue *val, TValue *res) { } -int luaH_getint1 (Table *t, lua_Integer key, TValue *res) { - return finishnodeget(luaH_getint(t, key), res); +int luaH_getint (Table *t, lua_Integer key, TValue *res) { + return finishnodeget(Hgetint(t, key), res); } /* ** search function for short strings */ -const TValue *luaH_getshortstr (Table *t, TString *key) { +const TValue *luaH_Hgetshortstr (Table *t, TString *key) { Node *n = hashstr(t, key); lua_assert(key->tt == LUA_VSHRSTR); for (;;) { /* check whether 'key' is somewhere in the chain */ @@ -799,14 +799,14 @@ const TValue *luaH_getshortstr (Table *t, TString *key) { } -int luaH_getshortstr1 (Table *t, TString *key, TValue *res) { - return finishnodeget(luaH_getshortstr(t, key), res); +int luaH_getshortstr (Table *t, TString *key, TValue *res) { + return finishnodeget(luaH_Hgetshortstr(t, key), res); } -const TValue *luaH_getstr (Table *t, TString *key) { +static const TValue *Hgetstr (Table *t, TString *key) { if (key->tt == LUA_VSHRSTR) - return luaH_getshortstr(t, key); + return luaH_Hgetshortstr(t, key); else { /* for long strings, use generic case */ TValue ko; setsvalue(cast(lua_State *, NULL), &ko, key); @@ -815,23 +815,32 @@ const TValue *luaH_getstr (Table *t, TString *key) { } -int luaH_getstr1 (Table *t, TString *key, TValue *res) { - return finishnodeget(luaH_getstr(t, key), res); +int luaH_getstr (Table *t, TString *key, TValue *res) { + return finishnodeget(Hgetstr(t, key), res); +} + + +TString *luaH_getstrkey (Table *t, TString *key) { + const TValue *o = Hgetstr(t, key); + if (!isabstkey(o)) /* string already present? */ + return keystrval(nodefromval(o)); /* get saved copy */ + else + return NULL; } /* ** main search function */ -const TValue *luaH_get (Table *t, const TValue *key) { +static const TValue *Hget (Table *t, const TValue *key) { switch (ttypetag(key)) { - case LUA_VSHRSTR: return luaH_getshortstr(t, tsvalue(key)); - case LUA_VNUMINT: return luaH_getint(t, ivalue(key)); + case LUA_VSHRSTR: return luaH_Hgetshortstr(t, tsvalue(key)); + case LUA_VNUMINT: return Hgetint(t, ivalue(key)); case LUA_VNIL: return &absentkey; case LUA_VNUMFLT: { lua_Integer k; if (luaV_flttointeger(fltvalue(key), &k, F2Ieq)) /* integral index? */ - return luaH_getint(t, k); /* use specialized version */ + return Hgetint(t, k); /* use specialized version */ /* else... */ } /* FALLTHROUGH */ default: @@ -840,8 +849,8 @@ const TValue *luaH_get (Table *t, const TValue *key) { } -int luaH_get1 (Table *t, const TValue *key, TValue *res) { - return finishnodeget(luaH_get(t, key), res); +int luaH_get (Table *t, const TValue *key, TValue *res) { + return finishnodeget(Hget(t, key), res); } @@ -856,7 +865,7 @@ static int finishnodeset (Table *t, const TValue *slot, TValue *val) { } -int luaH_setint1 (Table *t, lua_Integer key, TValue *val) { +int luaH_psetint (Table *t, lua_Integer key, TValue *val) { const TValue *slot = getintfromarray(t, key); if (slot != NULL) { if (!ttisnil(slot)) { @@ -871,25 +880,25 @@ int luaH_setint1 (Table *t, lua_Integer key, TValue *val) { } -int luaH_setshortstr1 (Table *t, TString *key, TValue *val) { - return finishnodeset(t, luaH_getshortstr(t, key), val); +int luaH_psetshortstr (Table *t, TString *key, TValue *val) { + return finishnodeset(t, luaH_Hgetshortstr(t, key), val); } -int luaH_setstr1 (Table *t, TString *key, TValue *val) { - return finishnodeset(t, luaH_getstr(t, key), val); +int luaH_psetstr (Table *t, TString *key, TValue *val) { + return finishnodeset(t, Hgetstr(t, key), val); } -int luaH_set1 (Table *t, const TValue *key, TValue *val) { +int luaH_pset (Table *t, const TValue *key, TValue *val) { switch (ttypetag(key)) { - case LUA_VSHRSTR: return luaH_setshortstr1(t, tsvalue(key), val); - case LUA_VNUMINT: return luaH_setint1(t, ivalue(key), val); + case LUA_VSHRSTR: return luaH_psetshortstr(t, tsvalue(key), val); + case LUA_VNUMINT: return luaH_psetint(t, ivalue(key), val); case LUA_VNIL: return HNOTFOUND; case LUA_VNUMFLT: { lua_Integer k; if (luaV_flttointeger(fltvalue(key), &k, F2Ieq)) /* integral index? */ - return luaH_setint1(t, k, val); /* use specialized version */ + return luaH_psetint(t, k, val); /* use specialized version */ /* else... */ } /* FALLTHROUGH */ default: @@ -903,26 +912,20 @@ int luaH_set1 (Table *t, const TValue *key, TValue *val) { ** Beware: when using this function you probably need to check a GC ** barrier and invalidate the TM cache. */ -void luaH_finishset (lua_State *L, Table *t, const TValue *key, - const TValue *slot, TValue *value) { - if (isabstkey(slot)) - luaH_newkey(L, t, key, value); - else - setobj2t(L, cast(TValue *, slot), value); -} -void luaH_finishset1 (lua_State *L, Table *t, const TValue *key, - TValue *value, int aux) { - if (aux == HNOTFOUND) { +void luaH_finishset (lua_State *L, Table *t, const TValue *key, + TValue *value, int hres) { + lua_assert(hres != HOK); + if (hres == HNOTFOUND) { luaH_newkey(L, t, key, value); } - else if (aux > 0) { /* regular Node? */ - setobj2t(L, gval(gnode(t, aux - HFIRSTNODE)), value); + else if (hres > 0) { /* regular Node? */ + setobj2t(L, gval(gnode(t, hres - HFIRSTNODE)), value); } else { /* array entry */ - aux = ~aux; /* real index */ - val2arr(t, aux, getArrTag(t, aux), value); + hres = ~hres; /* real index */ + val2arr(t, hres, getArrTag(t, hres), value); } } @@ -932,20 +935,19 @@ void luaH_finishset1 (lua_State *L, Table *t, const TValue *key, ** barrier and invalidate the TM cache. */ void luaH_set (lua_State *L, Table *t, const TValue *key, TValue *value) { - const TValue *slot = luaH_get(t, key); - luaH_finishset(L, t, key, slot, value); + int hres = luaH_pset(t, key, value); + if (hres != HOK) + luaH_finishset(L, t, key, value, hres); } void luaH_setint (lua_State *L, Table *t, lua_Integer key, TValue *value) { - const TValue *p = luaH_getint(t, key); - if (isabstkey(p)) { + int hres = luaH_psetint(t, key, value); + if (hres != HOK) { TValue k; setivalue(&k, key); - luaH_newkey(L, t, &k, value); + luaH_finishset(L, t, &k, value, hres); } - else - setobj2t(L, cast(TValue *, p), value); } @@ -971,16 +973,16 @@ static lua_Unsigned hash_search (Table *t, lua_Unsigned j) { j *= 2; else { j = LUA_MAXINTEGER; - if (isempty(luaH_getint(t, j))) /* t[j] not present? */ + if (isempty(Hgetint(t, j))) /* t[j] not present? */ break; /* 'j' now is an absent index */ else /* weird case */ return j; /* well, max integer is a boundary... */ } - } while (!isempty(luaH_getint(t, j))); /* repeat until an absent t[j] */ + } while (!isempty(Hgetint(t, j))); /* repeat until an absent t[j] */ /* i < j && t[i] present && t[j] absent */ while (j - i > 1u) { /* do a binary search between them */ lua_Unsigned m = (i + j) / 2; - if (isempty(luaH_getint(t, m))) j = m; + if (isempty(Hgetint(t, m))) j = m; else i = m; } return i; @@ -1071,7 +1073,7 @@ lua_Unsigned luaH_getn (Table *t) { /* (3) 'limit' is the last element and either is zero or present in table */ lua_assert(limit == luaH_realasize(t) && (limit == 0 || !isempty(&t->array[limit - 1]))); - if (isdummy(t) || isempty(luaH_getint(t, cast(lua_Integer, limit + 1)))) + if (isdummy(t) || isempty(Hgetint(t, cast(lua_Integer, limit + 1)))) return limit; /* 'limit + 1' is absent */ else /* 'limit + 1' is also present */ return hash_search(t, limit); diff --git a/ltable.h b/ltable.h index b9bb416a78..5ec7b447c0 100644 --- a/ltable.h +++ b/ltable.h @@ -35,12 +35,22 @@ #define nodefromval(v) cast(Node *, (v)) -/* results from get/set */ +/* results from get/pset */ #define HOK 0 #define HNOTFOUND 1 #define HNOTATABLE 2 #define HFIRSTNODE 3 +/* +** Besides these values, pset (pre-set) operations may also return an +** encoding of where the value should go (usually called 'hres'). That +** means that there is a slot with that key but with no value. (pset +** cannot set that value because there might be a metamethod.) If the +** slot is in the hash part, the encoding is (HFIRSTNODE + hash index); +** if the slot is in the array part, the encoding is (~array index). +*/ + + /* fast access to components of array values */ #define getArrTag(t,k) (&(t)->array[k - 1].tt_) @@ -55,29 +65,26 @@ (*tag = (val)->tt_, *getArrVal(h,k) = (val)->value_) -LUAI_FUNC int luaH_getshortstr1 (Table *t, TString *key, TValue *res); -LUAI_FUNC int luaH_getstr1 (Table *t, TString *key, TValue *res); -LUAI_FUNC int luaH_get1 (Table *t, const TValue *key, TValue *res); -LUAI_FUNC int luaH_getint1 (Table *t, lua_Integer key, TValue *res); +LUAI_FUNC int luaH_getshortstr (Table *t, TString *key, TValue *res); +LUAI_FUNC int luaH_getstr (Table *t, TString *key, TValue *res); +LUAI_FUNC int luaH_get (Table *t, const TValue *key, TValue *res); +LUAI_FUNC int luaH_getint (Table *t, lua_Integer key, TValue *res); + +LUAI_FUNC TString *luaH_getstrkey (Table *t, TString *key); -LUAI_FUNC int luaH_setint1 (Table *t, lua_Integer key, TValue *val); -LUAI_FUNC int luaH_setshortstr1 (Table *t, TString *key, TValue *val); -LUAI_FUNC int luaH_setstr1 (Table *t, TString *key, TValue *val); -LUAI_FUNC int luaH_set1 (Table *t, const TValue *key, TValue *val); +LUAI_FUNC int luaH_psetint (Table *t, lua_Integer key, TValue *val); +LUAI_FUNC int luaH_psetshortstr (Table *t, TString *key, TValue *val); +LUAI_FUNC int luaH_psetstr (Table *t, TString *key, TValue *val); +LUAI_FUNC int luaH_pset (Table *t, const TValue *key, TValue *val); -LUAI_FUNC const TValue *luaH_getint (Table *t, lua_Integer key); LUAI_FUNC void luaH_setint (lua_State *L, Table *t, lua_Integer key, TValue *value); -LUAI_FUNC const TValue *luaH_getshortstr (Table *t, TString *key); -LUAI_FUNC const TValue *luaH_getstr (Table *t, TString *key); -LUAI_FUNC const TValue *luaH_get (Table *t, const TValue *key); +LUAI_FUNC const TValue *luaH_Hgetshortstr (Table *t, TString *key); LUAI_FUNC void luaH_newkey (lua_State *L, Table *t, const TValue *key, TValue *value); LUAI_FUNC void luaH_set (lua_State *L, Table *t, const TValue *key, TValue *value); LUAI_FUNC void luaH_finishset (lua_State *L, Table *t, const TValue *key, - const TValue *slot, TValue *value); -LUAI_FUNC void luaH_finishset1 (lua_State *L, Table *t, const TValue *key, TValue *value, int aux); LUAI_FUNC Table *luaH_new (lua_State *L); LUAI_FUNC void luaH_resize (lua_State *L, Table *t, unsigned int nasize, diff --git a/ltm.c b/ltm.c index 07a060811d..fce6245cd0 100644 --- a/ltm.c +++ b/ltm.c @@ -58,7 +58,7 @@ void luaT_init (lua_State *L) { ** tag methods */ const TValue *luaT_gettm (Table *events, TMS event, TString *ename) { - const TValue *tm = luaH_getshortstr(events, ename); + const TValue *tm = luaH_Hgetshortstr(events, ename); lua_assert(event <= TM_EQ); if (notm(tm)) { /* no tag method? */ events->flags |= cast_byte(1u<mt[ttype(o)]; } - return (mt ? luaH_getshortstr(mt, G(L)->tmname[event]) : &G(L)->nilvalue); + return (mt ? luaH_Hgetshortstr(mt, G(L)->tmname[event]) : &G(L)->nilvalue); } @@ -92,9 +92,10 @@ const char *luaT_objtypename (lua_State *L, const TValue *o) { Table *mt; if ((ttistable(o) && (mt = hvalue(o)->metatable) != NULL) || (ttisfulluserdata(o) && (mt = uvalue(o)->metatable) != NULL)) { - const TValue *name = luaH_getshortstr(mt, luaS_new(L, "__name")); - if (ttisstring(name)) /* is '__name' a string? */ - return getstr(tsvalue(name)); /* use it as type name */ + TValue name; + int hres = luaH_getshortstr(mt, luaS_new(L, "__name"), &name); + if (hres == HOK && ttisstring(&name)) /* is '__name' a string? */ + return getstr(tsvalue(&name)); /* use it as type name */ } return ttypename(ttype(o)); /* else use standard type name */ } diff --git a/lvm.c b/lvm.c index f593402559..96413718a7 100644 --- a/lvm.c +++ b/lvm.c @@ -281,15 +281,13 @@ static int floatforloop (StkId ra) { /* ** Finish the table access 'val = t[key]'. -** if 'slot' is NULL, 't' is not a table; otherwise, 'slot' points to -** t[k] entry (which must be empty). */ -void luaV_finishget1 (lua_State *L, const TValue *t, TValue *key, StkId val, - int aux) { +void luaV_finishget (lua_State *L, const TValue *t, TValue *key, StkId val, + int hres) { int loop; /* counter to avoid infinite loops */ const TValue *tm; /* metamethod */ for (loop = 0; loop < MAXTAGLOOP; loop++) { - if (aux == HNOTATABLE) { /* 't' is not a table? */ + if (hres == HNOTATABLE) { /* 't' is not a table? */ lua_assert(!ttistable(t)); tm = luaT_gettmbyobj(L, t, TM_INDEX); if (l_unlikely(notm(tm))) @@ -309,8 +307,8 @@ void luaV_finishget1 (lua_State *L, const TValue *t, TValue *key, StkId val, return; } t = tm; /* else try to access 'tm[key]' */ - luaV_fastget1(t, key, s2v(val), luaH_get1, aux); - if (aux == HOK) + luaV_fastget(t, key, s2v(val), luaH_get, hres); + if (hres == HOK) return; /* done */ /* else repeat (tail call 'luaV_finishget') */ } @@ -320,21 +318,17 @@ void luaV_finishget1 (lua_State *L, const TValue *t, TValue *key, StkId val, /* ** Finish a table assignment 't[key] = val'. -** If 'slot' is NULL, 't' is not a table. Otherwise, 'slot' points -** to the entry 't[key]', or to a value with an absent key if there -** is no such entry. (The value at 'slot' must be empty, otherwise -** 'luaV_fastget' would have done the job.) */ -void luaV_finishset1 (lua_State *L, const TValue *t, TValue *key, - TValue *val, int aux) { +void luaV_finishset (lua_State *L, const TValue *t, TValue *key, + TValue *val, int hres) { int loop; /* counter to avoid infinite loops */ for (loop = 0; loop < MAXTAGLOOP; loop++) { const TValue *tm; /* '__newindex' metamethod */ - if (aux != HNOTATABLE) { /* is 't' a table? */ + if (hres != HNOTATABLE) { /* is 't' a table? */ Table *h = hvalue(t); /* save 't' table */ tm = fasttm(L, h->metatable, TM_NEWINDEX); /* get metamethod */ if (tm == NULL) { /* no metamethod? */ - luaH_finishset1(L, h, key, val, aux); /* set new value */ + luaH_finishset(L, h, key, val, hres); /* set new value */ invalidateTMcache(h); luaC_barrierback(L, obj2gco(h), val); return; @@ -352,8 +346,8 @@ void luaV_finishset1 (lua_State *L, const TValue *t, TValue *key, return; } t = tm; /* else repeat assignment over 'tm' */ - luaV_fastset1(t, key, val, aux, luaH_set1); - if (aux == HOK) + luaV_fastset(t, key, val, hres, luaH_pset); + if (hres == HOK) return; /* done */ /* else 'return luaV_finishset(L, t, key, val, slot)' (loop) */ } @@ -1249,36 +1243,36 @@ void luaV_execute (lua_State *L, CallInfo *ci) { TValue *upval = cl->upvals[GETARG_B(i)]->v.p; TValue *rc = KC(i); TString *key = tsvalue(rc); /* key must be a string */ - int aux; - luaV_fastget1(upval, key, s2v(ra), luaH_getshortstr1, aux); - if (aux != HOK) - Protect(luaV_finishget1(L, upval, rc, ra, aux)); + int hres; + luaV_fastget(upval, key, s2v(ra), luaH_getshortstr, hres); + if (hres != HOK) + Protect(luaV_finishget(L, upval, rc, ra, hres)); vmbreak; } vmcase(OP_GETTABLE) { StkId ra = RA(i); TValue *rb = vRB(i); TValue *rc = vRC(i); - int aux; + int hres; if (ttisinteger(rc)) { /* fast track for integers? */ - luaV_fastgeti1(rb, ivalue(rc), s2v(ra), aux); + luaV_fastgeti(rb, ivalue(rc), s2v(ra), hres); } else - luaV_fastget1(rb, rc, s2v(ra), luaH_get1, aux); - if (aux != HOK) /* fast track for integers? */ - Protect(luaV_finishget1(L, rb, rc, ra, aux)); + luaV_fastget(rb, rc, s2v(ra), luaH_get, hres); + if (hres != HOK) /* fast track for integers? */ + Protect(luaV_finishget(L, rb, rc, ra, hres)); vmbreak; } vmcase(OP_GETI) { StkId ra = RA(i); TValue *rb = vRB(i); int c = GETARG_C(i); - int aux; - luaV_fastgeti1(rb, c, s2v(ra), aux); - if (aux != HOK) { + int hres; + luaV_fastgeti(rb, c, s2v(ra), hres); + if (hres != HOK) { TValue key; setivalue(&key, c); - Protect(luaV_finishget1(L, rb, &key, ra, aux)); + Protect(luaV_finishget(L, rb, &key, ra, hres)); } vmbreak; } @@ -1287,68 +1281,68 @@ void luaV_execute (lua_State *L, CallInfo *ci) { TValue *rb = vRB(i); TValue *rc = KC(i); TString *key = tsvalue(rc); /* key must be a string */ - int aux; - luaV_fastget1(rb, key, s2v(ra), luaH_getshortstr1, aux); - if (aux != HOK) - Protect(luaV_finishget1(L, rb, rc, ra, aux)); + int hres; + luaV_fastget(rb, key, s2v(ra), luaH_getshortstr, hres); + if (hres != HOK) + Protect(luaV_finishget(L, rb, rc, ra, hres)); vmbreak; } vmcase(OP_SETTABUP) { - int aux; + int hres; TValue *upval = cl->upvals[GETARG_A(i)]->v.p; TValue *rb = KB(i); TValue *rc = RKC(i); TString *key = tsvalue(rb); /* key must be a string */ - luaV_fastset1(upval, key, rc, aux, luaH_setshortstr1); - if (aux == HOK) - luaV_finishfastset1(L, upval, rc); + luaV_fastset(upval, key, rc, hres, luaH_psetshortstr); + if (hres == HOK) + luaV_finishfastset(L, upval, rc); else - Protect(luaV_finishset1(L, upval, rb, rc, aux)); + Protect(luaV_finishset(L, upval, rb, rc, hres)); vmbreak; } vmcase(OP_SETTABLE) { StkId ra = RA(i); - int aux; + int hres; TValue *rb = vRB(i); /* key (table is in 'ra') */ TValue *rc = RKC(i); /* value */ if (ttisinteger(rb)) { /* fast track for integers? */ - luaV_fastseti1(s2v(ra), ivalue(rb), rc, aux); + luaV_fastseti(s2v(ra), ivalue(rb), rc, hres); } else { - luaV_fastset1(s2v(ra), rb, rc, aux, luaH_set1); + luaV_fastset(s2v(ra), rb, rc, hres, luaH_pset); } - if (aux == HOK) - luaV_finishfastset1(L, s2v(ra), rc); + if (hres == HOK) + luaV_finishfastset(L, s2v(ra), rc); else - Protect(luaV_finishset1(L, s2v(ra), rb, rc, aux)); + Protect(luaV_finishset(L, s2v(ra), rb, rc, hres)); vmbreak; } vmcase(OP_SETI) { StkId ra = RA(i); - int aux; + int hres; int b = GETARG_B(i); TValue *rc = RKC(i); - luaV_fastseti1(s2v(ra), b, rc, aux); - if (aux == HOK) - luaV_finishfastset1(L, s2v(ra), rc); + luaV_fastseti(s2v(ra), b, rc, hres); + if (hres == HOK) + luaV_finishfastset(L, s2v(ra), rc); else { TValue key; setivalue(&key, b); - Protect(luaV_finishset1(L, s2v(ra), &key, rc, aux)); + Protect(luaV_finishset(L, s2v(ra), &key, rc, hres)); } vmbreak; } vmcase(OP_SETFIELD) { StkId ra = RA(i); - int aux; + int hres; TValue *rb = KB(i); TValue *rc = RKC(i); TString *key = tsvalue(rb); /* key must be a string */ - luaV_fastset1(s2v(ra), key, rc, aux, luaH_setshortstr1); - if (aux == HOK) - luaV_finishfastset1(L, s2v(ra), rc); + luaV_fastset(s2v(ra), key, rc, hres, luaH_psetshortstr); + if (hres == HOK) + luaV_finishfastset(L, s2v(ra), rc); else - Protect(luaV_finishset1(L, s2v(ra), rb, rc, aux)); + Protect(luaV_finishset(L, s2v(ra), rb, rc, hres)); vmbreak; } vmcase(OP_NEWTABLE) { @@ -1372,14 +1366,14 @@ void luaV_execute (lua_State *L, CallInfo *ci) { } vmcase(OP_SELF) { StkId ra = RA(i); - int aux; + int hres; TValue *rb = vRB(i); TValue *rc = RKC(i); TString *key = tsvalue(rc); /* key must be a string */ setobj2s(L, ra + 1, rb); - luaV_fastget1(rb, key, s2v(ra), luaH_getstr1, aux); - if (aux != HOK) - Protect(luaV_finishget1(L, rb, rc, ra, aux)); + luaV_fastget(rb, key, s2v(ra), luaH_getstr, hres); + if (hres != HOK) + Protect(luaV_finishget(L, rb, rc, ra, hres)); vmbreak; } vmcase(OP_ADDI) { diff --git a/lvm.h b/lvm.h index 750a22b261..8808c94255 100644 --- a/lvm.h +++ b/lvm.h @@ -76,20 +76,9 @@ typedef enum { /* -** fast track for 'gettable': if 't' is a table and 't[k]' is present, -** return 1 with 'slot' pointing to 't[k]' (position of final result). -** Otherwise, return 0 (meaning it will have to check metamethod) -** with 'slot' pointing to an empty 't[k]' (if 't' is a table) or NULL -** (otherwise). 'f' is the raw get function to use. +** fast track for 'gettable' */ -#define luaV_fastget(L,t,k,slot,f) \ - (!ttistable(t) \ - ? (slot = NULL, 0) /* not a table; 'slot' is NULL and result is 0 */ \ - : (slot = f(hvalue(t), k), /* else, do raw access */ \ - !isempty(slot))) /* result not empty? */ - - -#define luaV_fastget1(t,k,res,f, aux) \ +#define luaV_fastget(t,k,res,f, aux) \ (aux = (!ttistable(t) ? HNOTATABLE : f(hvalue(t), k, res))) @@ -97,45 +86,33 @@ typedef enum { ** Special case of 'luaV_fastget' for integers, inlining the fast case ** of 'luaH_getint'. */ -#define luaV_fastgeti(L,t,k,slot) \ - (!ttistable(t) \ - ? (slot = NULL, 0) /* not a table; 'slot' is NULL and result is 0 */ \ - : (slot = (l_castS2U(k) - 1u < hvalue(t)->alimit) \ - ? &hvalue(t)->array[k - 1] : luaH_getint(hvalue(t), k), \ - !isempty(slot))) /* result not empty? */ - -#define luaV_fastgeti1(t,k,res,aux) \ +#define luaV_fastgeti(t,k,res,aux) \ if (!ttistable(t)) aux = HNOTATABLE; \ else { Table *h = hvalue(t); lua_Unsigned u = l_castS2U(k); \ if ((u - 1u < h->alimit)) { \ int tag = *getArrTag(h,u); \ if (tagisempty(tag)) aux = HNOTFOUND; \ else { arr2val(h, u, tag, res); aux = HOK; }} \ - else { aux = luaH_getint1(h, u, res); }} + else { aux = luaH_getint(h, u, res); }} -#define luaV_fastset1(t,k,val,aux,f) \ +#define luaV_fastset(t,k,val,aux,f) \ (aux = (!ttistable(t) ? HNOTATABLE : f(hvalue(t), k, val))) -#define luaV_fastseti1(t,k,val,aux) \ +#define luaV_fastseti(t,k,val,aux) \ if (!ttistable(t)) aux = HNOTATABLE; \ else { Table *h = hvalue(t); lua_Unsigned u = l_castS2U(k); \ if ((u - 1u < h->alimit)) { \ lu_byte *tag = getArrTag(h,u); \ if (tagisempty(*tag)) aux = ~cast_int(u); \ else { val2arr(h, u, tag, val); aux = HOK; }} \ - else { aux = luaH_setint1(h, u, val); }} + else { aux = luaH_psetint(h, u, val); }} /* -** Finish a fast set operation (when fast get succeeds). In that case, -** 'slot' points to the place to put the value. +** Finish a fast set operation (when fast set succeeds). */ -#define luaV_finishfastset(L,t,slot,v) \ - { setobj2t(L, cast(TValue *,slot), v); \ - luaC_barrierback(L, gcvalue(t), v); } - -#define luaV_finishfastset1(L,t,v) luaC_barrierback(L, gcvalue(t), v) +#define luaV_finishfastset(L,t,v) luaC_barrierback(L, gcvalue(t), v) /* @@ -153,10 +130,10 @@ LUAI_FUNC int luaV_tointeger (const TValue *obj, lua_Integer *p, F2Imod mode); LUAI_FUNC int luaV_tointegerns (const TValue *obj, lua_Integer *p, F2Imod mode); LUAI_FUNC int luaV_flttointeger (lua_Number n, lua_Integer *p, F2Imod mode); -LUAI_FUNC void luaV_finishget1 (lua_State *L, const TValue *t, TValue *key, - StkId val, int aux); -LUAI_FUNC void luaV_finishset1 (lua_State *L, const TValue *t, TValue *key, - TValue *val, int aux); +LUAI_FUNC void luaV_finishget (lua_State *L, const TValue *t, TValue *key, + StkId val, int aux); +LUAI_FUNC void luaV_finishset (lua_State *L, const TValue *t, TValue *key, + TValue *val, int aux); LUAI_FUNC void luaV_finishOp (lua_State *L); LUAI_FUNC void luaV_execute (lua_State *L, CallInfo *ci); LUAI_FUNC void luaV_concat (lua_State *L, int total); From 9be74ccc214eb6f4d9d0b9496fd973542c7377d9 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 22 May 2023 14:47:54 -0300 Subject: [PATCH 0824/1145] Several functions turned 'static' Several functions that were already being used only inside their own file have been declared as 'static'. --- lcode.c | 22 +++++++++++----------- lcode.h | 3 --- ldo.c | 6 +++--- ldo.h | 1 - lgc.c | 4 ++-- lstate.c | 4 ++-- lstate.h | 1 - 7 files changed, 18 insertions(+), 23 deletions(-) diff --git a/lcode.c b/lcode.c index eade2806ea..caac6ba322 100644 --- a/lcode.c +++ b/lcode.c @@ -415,7 +415,7 @@ int luaK_codeABx (FuncState *fs, OpCode o, int a, unsigned int bc) { /* ** Format and emit an 'iAsBx' instruction. */ -int luaK_codeAsBx (FuncState *fs, OpCode o, int a, int bc) { +static int codeAsBx (FuncState *fs, OpCode o, int a, int bc) { unsigned int b = bc + OFFSET_sBx; lua_assert(getOpMode(o) == iAsBx); lua_assert(a <= MAXARG_A && b <= MAXARG_Bx); @@ -671,7 +671,7 @@ static int fitsBx (lua_Integer i) { void luaK_int (FuncState *fs, int reg, lua_Integer i) { if (fitsBx(i)) - luaK_codeAsBx(fs, OP_LOADI, reg, cast_int(i)); + codeAsBx(fs, OP_LOADI, reg, cast_int(i)); else luaK_codek(fs, reg, luaK_intK(fs, i)); } @@ -680,7 +680,7 @@ void luaK_int (FuncState *fs, int reg, lua_Integer i) { static void luaK_float (FuncState *fs, int reg, lua_Number f) { lua_Integer fi; if (luaV_flttointeger(f, &fi, F2Ieq) && fitsBx(fi)) - luaK_codeAsBx(fs, OP_LOADF, reg, cast_int(fi)); + codeAsBx(fs, OP_LOADF, reg, cast_int(fi)); else luaK_codek(fs, reg, luaK_numberK(fs, f)); } @@ -1025,7 +1025,7 @@ static int luaK_exp2K (FuncState *fs, expdesc *e) { ** in the range of R/K indices). ** Returns 1 iff expression is K. */ -int luaK_exp2RK (FuncState *fs, expdesc *e) { +static int exp2RK (FuncState *fs, expdesc *e) { if (luaK_exp2K(fs, e)) return 1; else { /* not a constant in the right range: put it in a register */ @@ -1037,7 +1037,7 @@ int luaK_exp2RK (FuncState *fs, expdesc *e) { static void codeABRK (FuncState *fs, OpCode o, int a, int b, expdesc *ec) { - int k = luaK_exp2RK(fs, ec); + int k = exp2RK(fs, ec); luaK_codeABCk(fs, o, a, b, ec->u.info, k); } @@ -1225,7 +1225,7 @@ static int isKstr (FuncState *fs, expdesc *e) { /* ** Check whether expression 'e' is a literal integer. */ -int luaK_isKint (expdesc *e) { +static int isKint (expdesc *e) { return (e->k == VKINT && !hasjumps(e)); } @@ -1235,7 +1235,7 @@ int luaK_isKint (expdesc *e) { ** proper range to fit in register C */ static int isCint (expdesc *e) { - return luaK_isKint(e) && (l_castS2U(e->u.ival) <= l_castS2U(MAXARG_C)); + return isKint(e) && (l_castS2U(e->u.ival) <= l_castS2U(MAXARG_C)); } @@ -1244,7 +1244,7 @@ static int isCint (expdesc *e) { ** proper range to fit in register sC */ static int isSCint (expdesc *e) { - return luaK_isKint(e) && fitsC(e->u.ival); + return isKint(e) && fitsC(e->u.ival); } @@ -1460,7 +1460,7 @@ static void codebinK (FuncState *fs, BinOpr opr, */ static int finishbinexpneg (FuncState *fs, expdesc *e1, expdesc *e2, OpCode op, int line, TMS event) { - if (!luaK_isKint(e2)) + if (!isKint(e2)) return 0; /* not an integer constant */ else { lua_Integer i2 = e2->u.ival; @@ -1593,7 +1593,7 @@ static void codeeq (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) { op = OP_EQI; r2 = im; /* immediate operand */ } - else if (luaK_exp2RK(fs, e2)) { /* 2nd expression is constant? */ + else if (exp2RK(fs, e2)) { /* 2nd expression is constant? */ op = OP_EQK; r2 = e2->u.info; /* constant index */ } @@ -1659,7 +1659,7 @@ void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) { } case OPR_EQ: case OPR_NE: { if (!tonumeral(v, NULL)) - luaK_exp2RK(fs, v); + exp2RK(fs, v); /* else keep numeral, which may be an immediate operand */ break; } diff --git a/lcode.h b/lcode.h index 3265824452..0b971fc435 100644 --- a/lcode.h +++ b/lcode.h @@ -61,10 +61,8 @@ typedef enum UnOpr { OPR_MINUS, OPR_BNOT, OPR_NOT, OPR_LEN, OPR_NOUNOPR } UnOpr; LUAI_FUNC int luaK_code (FuncState *fs, Instruction i); LUAI_FUNC int luaK_codeABx (FuncState *fs, OpCode o, int A, unsigned int Bx); -LUAI_FUNC int luaK_codeAsBx (FuncState *fs, OpCode o, int A, int Bx); LUAI_FUNC int luaK_codeABCk (FuncState *fs, OpCode o, int A, int B, int C, int k); -LUAI_FUNC int luaK_isKint (expdesc *e); LUAI_FUNC int luaK_exp2const (FuncState *fs, const expdesc *e, TValue *v); LUAI_FUNC void luaK_fixline (FuncState *fs, int line); LUAI_FUNC void luaK_nil (FuncState *fs, int from, int n); @@ -76,7 +74,6 @@ LUAI_FUNC int luaK_exp2anyreg (FuncState *fs, expdesc *e); LUAI_FUNC void luaK_exp2anyregup (FuncState *fs, expdesc *e); LUAI_FUNC void luaK_exp2nextreg (FuncState *fs, expdesc *e); LUAI_FUNC void luaK_exp2val (FuncState *fs, expdesc *e); -LUAI_FUNC int luaK_exp2RK (FuncState *fs, expdesc *e); LUAI_FUNC void luaK_self (FuncState *fs, expdesc *e, expdesc *key); LUAI_FUNC void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k); LUAI_FUNC void luaK_goiftrue (FuncState *fs, expdesc *e); diff --git a/ldo.c b/ldo.c index 2a0017ca62..bd8d965fc0 100644 --- a/ldo.c +++ b/ldo.c @@ -409,7 +409,7 @@ static void rethook (lua_State *L, CallInfo *ci, int nres) { ** stack, below original 'func', so that 'luaD_precall' can call it. Raise ** an error if there is no '__call' metafield. */ -StkId luaD_tryfuncTM (lua_State *L, StkId func) { +static StkId tryfuncTM (lua_State *L, StkId func) { const TValue *tm; StkId p; checkstackGCp(L, 1, func); /* space for metamethod */ @@ -568,7 +568,7 @@ int luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, return -1; } default: { /* not a function */ - func = luaD_tryfuncTM(L, func); /* try to get '__call' metamethod */ + func = tryfuncTM(L, func); /* try to get '__call' metamethod */ /* return luaD_pretailcall(L, ci, func, narg1 + 1, delta); */ narg1++; goto retry; /* try again */ @@ -609,7 +609,7 @@ CallInfo *luaD_precall (lua_State *L, StkId func, int nresults) { return ci; } default: { /* not a function */ - func = luaD_tryfuncTM(L, func); /* try to get '__call' metamethod */ + func = tryfuncTM(L, func); /* try to get '__call' metamethod */ /* return luaD_precall(L, func, nresults); */ goto retry; /* try again with metamethod */ } diff --git a/ldo.h b/ldo.h index 1aa446ad09..56008ab30f 100644 --- a/ldo.h +++ b/ldo.h @@ -71,7 +71,6 @@ LUAI_FUNC int luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, LUAI_FUNC CallInfo *luaD_precall (lua_State *L, StkId func, int nResults); LUAI_FUNC void luaD_call (lua_State *L, StkId func, int nResults); LUAI_FUNC void luaD_callnoyield (lua_State *L, StkId func, int nResults); -LUAI_FUNC StkId luaD_tryfuncTM (lua_State *L, StkId func); LUAI_FUNC int luaD_closeprotected (lua_State *L, ptrdiff_t level, int status); LUAI_FUNC int luaD_pcall (lua_State *L, Pfunc func, void *u, ptrdiff_t oldtop, ptrdiff_t ef); diff --git a/lgc.c b/lgc.c index a3094ff571..dd824e77e3 100644 --- a/lgc.c +++ b/lgc.c @@ -1409,7 +1409,7 @@ static void stepgenfull (lua_State *L, global_State *g) { setminordebt(g); } else { /* another bad collection; stay in incremental mode */ - g->GCestimate = gettotalbytes(g); /* first estimate */; + g->GCestimate = gettotalbytes(g); /* first estimate */ entersweep(L); luaC_runtilstate(L, bitmask(GCSpause)); /* finish collection */ setpause(g); @@ -1604,7 +1604,7 @@ static lu_mem singlestep (lua_State *L) { case GCSenteratomic: { work = atomic(L); /* work is what was traversed by 'atomic' */ entersweep(L); - g->GCestimate = gettotalbytes(g); /* first estimate */; + g->GCestimate = gettotalbytes(g); /* first estimate */ break; } case GCSswpallgc: { /* sweep "regular" objects */ diff --git a/lstate.c b/lstate.c index 1e925e5ad4..06667dacf9 100644 --- a/lstate.c +++ b/lstate.c @@ -119,7 +119,7 @@ CallInfo *luaE_extendCI (lua_State *L) { /* ** free all CallInfo structures not in use by a thread */ -void luaE_freeCI (lua_State *L) { +static void freeCI (lua_State *L) { CallInfo *ci = L->ci; CallInfo *next = ci->next; ci->next = NULL; @@ -204,7 +204,7 @@ static void freestack (lua_State *L) { if (L->stack.p == NULL) return; /* stack not completely built yet */ L->ci = &L->base_ci; /* free the entire 'ci' list */ - luaE_freeCI(L); + freeCI(L); lua_assert(L->nci == 0); luaM_freearray(L, L->stack.p, stacksize(L) + EXTRA_STACK); /* free stack */ } diff --git a/lstate.h b/lstate.h index 8bf6600e34..40ff89aaa7 100644 --- a/lstate.h +++ b/lstate.h @@ -396,7 +396,6 @@ union GCUnion { LUAI_FUNC void luaE_setdebt (global_State *g, l_mem debt); LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1); LUAI_FUNC CallInfo *luaE_extendCI (lua_State *L); -LUAI_FUNC void luaE_freeCI (lua_State *L); LUAI_FUNC void luaE_shrinkCI (lua_State *L); LUAI_FUNC void luaE_checkcstack (lua_State *L); LUAI_FUNC void luaE_incCstack (lua_State *L); From f623b969325be736297bc1dff48e763c08778243 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 14 Jun 2023 14:38:07 -0300 Subject: [PATCH 0825/1145] Bug: read overflow in 'l_strcmp' Equality according to 'strcoll' does not imply that strings have the same length. --- lvm.c | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/lvm.c b/lvm.c index 4c300a87ae..2b437bdfdd 100644 --- a/lvm.c +++ b/lvm.c @@ -366,30 +366,32 @@ void luaV_finishset (lua_State *L, const TValue *t, TValue *key, /* -** Compare two strings 'ls' x 'rs', returning an integer less-equal- -** -greater than zero if 'ls' is less-equal-greater than 'rs'. +** Compare two strings 'ts1' x 'ts2', returning an integer less-equal- +** -greater than zero if 'ts1' is less-equal-greater than 'ts2'. ** The code is a little tricky because it allows '\0' in the strings -** and it uses 'strcoll' (to respect locales) for each segments -** of the strings. +** and it uses 'strcoll' (to respect locales) for each segment +** of the strings. Note that segments can compare equal but still +** have different lengths. */ -static int l_strcmp (const TString *ls, const TString *rs) { - const char *l = getstr(ls); - size_t ll = tsslen(ls); - const char *r = getstr(rs); - size_t lr = tsslen(rs); +static int l_strcmp (const TString *ts1, const TString *ts2) { + const char *s1 = getstr(ts1); + size_t rl1 = tsslen(ts1); /* real length */ + const char *s2 = getstr(ts2); + size_t rl2 = tsslen(ts2); for (;;) { /* for each segment */ - int temp = strcoll(l, r); + int temp = strcoll(s1, s2); if (temp != 0) /* not equal? */ return temp; /* done */ else { /* strings are equal up to a '\0' */ - size_t len = strlen(l); /* index of first '\0' in both strings */ - if (len == lr) /* 'rs' is finished? */ - return (len == ll) ? 0 : 1; /* check 'ls' */ - else if (len == ll) /* 'ls' is finished? */ - return -1; /* 'ls' is less than 'rs' ('rs' is not finished) */ - /* both strings longer than 'len'; go on comparing after the '\0' */ - len++; - l += len; ll -= len; r += len; lr -= len; + size_t zl1 = strlen(s1); /* index of first '\0' in 's1' */ + size_t zl2 = strlen(s2); /* index of first '\0' in 's2' */ + if (zl2 == rl2) /* 's2' is finished? */ + return (zl1 == rl1) ? 0 : 1; /* check 's1' */ + else if (zl1 == rl1) /* 's1' is finished? */ + return -1; /* 's1' is less than 's2' ('s2' is not finished) */ + /* both strings longer than 'zl'; go on comparing after the '\0' */ + zl1++; zl2++; + s1 += zl1; rl1 -= zl1; s2 += zl2; rl2 -= zl2; } } } From 05ec55f16b389a4377adab84efe374437da8dbd2 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 16 Jun 2023 11:52:14 -0300 Subject: [PATCH 0826/1145] Avoid inclusion loop in 'ltm.h' --- ltm.h | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/ltm.h b/ltm.h index c309e2ae10..73b833c605 100644 --- a/ltm.h +++ b/ltm.h @@ -9,7 +9,6 @@ #include "lobject.h" -#include "lstate.h" /* @@ -96,8 +95,8 @@ LUAI_FUNC int luaT_callorderiTM (lua_State *L, const TValue *p1, int v2, int inv, int isfloat, TMS event); LUAI_FUNC void luaT_adjustvarargs (lua_State *L, int nfixparams, - CallInfo *ci, const Proto *p); -LUAI_FUNC void luaT_getvarargs (lua_State *L, CallInfo *ci, + struct CallInfo *ci, const Proto *p); +LUAI_FUNC void luaT_getvarargs (lua_State *L, struct CallInfo *ci, StkId where, int wanted); From ea39042e13645f63713425c05cc9ee4cfdcf0a40 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 21 Jun 2023 15:04:24 -0300 Subject: [PATCH 0827/1145] Removed redundancy in definitions of version/release String rendering now derived from the numeric original definitions. --- lua.h | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/lua.h b/lua.h index fd16cf8050..040cc8e43e 100644 --- a/lua.h +++ b/lua.h @@ -1,7 +1,7 @@ /* ** $Id: lua.h $ ** Lua - A Scripting Language -** Lua.org, PUC-Rio, Brazil (http://www.lua.org) +** Lua.org, PUC-Rio, Brazil (www.lua.org) ** See Copyright Notice at the end of this file */ @@ -13,20 +13,19 @@ #include -#include "luaconf.h" +#define LUA_COPYRIGHT LUA_RELEASE " Copyright (C) 1994-2023 Lua.org, PUC-Rio" +#define LUA_AUTHORS "R. Ierusalimschy, L. H. de Figueiredo, W. Celes" -#define LUA_VERSION_MAJOR "5" -#define LUA_VERSION_MINOR "4" -#define LUA_VERSION_RELEASE "6" +#define LUA_VERSION_MAJOR_N 5 +#define LUA_VERSION_MINOR_N 4 +#define LUA_VERSION_RELEASE_N 6 -#define LUA_VERSION_NUM 504 -#define LUA_VERSION_RELEASE_NUM (LUA_VERSION_NUM * 100 + 6) +#define LUA_VERSION_NUM (LUA_VERSION_MAJOR_N * 100 + LUA_VERSION_MINOR_N) +#define LUA_VERSION_RELEASE_NUM (LUA_VERSION_NUM * 100 + LUA_VERSION_RELEASE_N) -#define LUA_VERSION "Lua " LUA_VERSION_MAJOR "." LUA_VERSION_MINOR -#define LUA_RELEASE LUA_VERSION "." LUA_VERSION_RELEASE -#define LUA_COPYRIGHT LUA_RELEASE " Copyright (C) 1994-2023 Lua.org, PUC-Rio" -#define LUA_AUTHORS "R. Ierusalimschy, L. H. de Figueiredo, W. Celes" + +#include "luaconf.h" /* mark for precompiled code ('Lua') */ @@ -496,6 +495,17 @@ struct lua_Debug { /* }====================================================================== */ +#define LUAI_TOSTRAUX(x) #x +#define LUAI_TOSTR(x) LUAI_TOSTRAUX(x) + +#define LUA_VERSION_MAJOR LUAI_TOSTR(LUA_VERSION_MAJOR_N) +#define LUA_VERSION_MINOR LUAI_TOSTR(LUA_VERSION_MINOR_N) +#define LUA_VERSION_RELEASE LUAI_TOSTR(LUA_VERSION_RELEASE_N) + +#define LUA_VERSION "Lua " LUA_VERSION_MAJOR "." LUA_VERSION_MINOR +#define LUA_RELEASE LUA_VERSION "." LUA_VERSION_RELEASE + + /****************************************************************************** * Copyright (C) 1994-2023 Lua.org, PUC-Rio. * From cbae01620278f9b568805db16a96d0631ced473d Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 3 Jul 2023 14:12:54 -0300 Subject: [PATCH 0828/1145] Details --- lundump.h | 3 +-- testes/calls.lua | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/lundump.h b/lundump.h index f3748a9980..bc71ced842 100644 --- a/lundump.h +++ b/lundump.h @@ -21,8 +21,7 @@ /* ** Encode major-minor version in one byte, one nibble for each */ -#define MYINT(s) (s[0]-'0') /* assume one-digit numerals */ -#define LUAC_VERSION (MYINT(LUA_VERSION_MAJOR)*16+MYINT(LUA_VERSION_MINOR)) +#define LUAC_VERSION (LUA_VERSION_MAJOR_N*16+LUA_VERSION_MINOR_N) #define LUAC_FORMAT 0 /* this is the official format */ diff --git a/testes/calls.lua b/testes/calls.lua index 2d562a24a8..664be1b4e7 100644 --- a/testes/calls.lua +++ b/testes/calls.lua @@ -342,7 +342,7 @@ do -- another bug (in 5.4.0) end -do -- another bug (since 5.2) +if not _port then -- another bug (since 5.2) -- corrupted binary dump: list of upvalue names is larger than number -- of upvalues, overflowing the array of upvalues. local code = From 6b51133a988587f34ee9581d799ea9913581afd3 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 13 Jul 2023 14:55:46 -0300 Subject: [PATCH 0829/1145] Thread stacks resized in the atomic phase Although stack resize can be a little expensive, it seems unusual to have too many threads needing resize during one GC cycle. On the other hand, the change allows full collections to skip the propagate phase, going straight from a pause to the atomic phase. --- lgc.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lgc.c b/lgc.c index dd824e77e3..f8f43393e0 100644 --- a/lgc.c +++ b/lgc.c @@ -638,7 +638,9 @@ static int traversethread (global_State *g, lua_State *th) { for (uv = th->openupval; uv != NULL; uv = uv->u.open.next) markobject(g, uv); /* open upvalues cannot be collected */ if (g->gcstate == GCSatomic) { /* final traversal? */ - for (; o < th->stack_last.p + EXTRA_STACK; o++) + if (!g->gcemergency) + luaD_shrinkstack(th); /* do not change stack in emergency cycle */ + for (o = th->top.p; o < th->stack_last.p + EXTRA_STACK; o++) setnilvalue(s2v(o)); /* clear dead stack slice */ /* 'remarkupvals' may have removed thread from 'twups' list */ if (!isintwups(th) && th->openupval != NULL) { @@ -646,8 +648,6 @@ static int traversethread (global_State *g, lua_State *th) { g->twups = th; } } - else if (!g->gcemergency) - luaD_shrinkstack(th); /* do not change stack in emergency cycle */ return 1 + stacksize(th); } @@ -1710,6 +1710,8 @@ static void fullinc (lua_State *L, global_State *g) { entersweep(L); /* sweep everything to turn them back to white */ /* finish any pending sweep phase to start a new cycle */ luaC_runtilstate(L, bitmask(GCSpause)); + luaC_runtilstate(L, bitmask(GCSpropagate)); /* start new cycle */ + g->gcstate = GCSenteratomic; /* go straight to atomic phase ??? */ luaC_runtilstate(L, bitmask(GCScallfin)); /* run up to finalizers */ /* estimate must be correct after a full GC cycle */ lua_assert(g->GCestimate == gettotalbytes(g)); From 1b3f507f620d996ffb69da7476a19251acfb89ca Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 25 Jul 2023 16:50:44 -0300 Subject: [PATCH 0830/1145] Bug: Call hook may be called twice when count hook yields Took the opportunity and moved the code that controls call hooks in 'luaV_execute' into a function. --- ldebug.c | 22 ++++++++++++++++++++++ ldebug.h | 1 + lstate.h | 2 +- lvm.c | 13 +++---------- 4 files changed, 27 insertions(+), 11 deletions(-) diff --git a/ldebug.c b/ldebug.c index 28b1caabf7..195d02f802 100644 --- a/ldebug.c +++ b/ldebug.c @@ -865,6 +865,28 @@ static int changedline (const Proto *p, int oldpc, int newpc) { } +/* +** Traces Lua calls. If code is running the first instruction of a function, +** and function is not vararg, and it is not coming from an yield, +** calls 'luaD_hookcall'. (Vararg functions will call 'luaD_hookcall' +** after adjusting its variable arguments; otherwise, they could call +** a line/count hook before the call hook. Functions coming from +** an yield already called 'luaD_hookcall' before yielding.) +*/ +int luaG_tracecall (lua_State *L) { + CallInfo *ci = L->ci; + Proto *p = ci_func(ci)->p; + ci->u.l.trap = 1; /* ensure hooks will be checked */ + if (ci->u.l.savedpc == p->code) { /* first instruction (not resuming)? */ + if (p->is_vararg) + return 0; /* hooks will start at VARARGPREP instruction */ + else if (!(ci->callstatus & CIST_HOOKYIELD)) /* not yieded? */ + luaD_hookcall(L, ci); /* check 'call' hook */ + } + return 1; /* keep 'trap' on */ +} + + /* ** Traces the execution of a Lua function. Called before the execution ** of each opcode, when debug is on. 'L->oldpc' stores the last diff --git a/ldebug.h b/ldebug.h index 2c3074c61b..2bfce3cb5e 100644 --- a/ldebug.h +++ b/ldebug.h @@ -58,6 +58,7 @@ LUAI_FUNC const char *luaG_addinfo (lua_State *L, const char *msg, TString *src, int line); LUAI_FUNC l_noret luaG_errormsg (lua_State *L); LUAI_FUNC int luaG_traceexec (lua_State *L, const Instruction *pc); +LUAI_FUNC int luaG_tracecall (lua_State *L); #endif diff --git a/lstate.h b/lstate.h index 40ff89aaa7..007704c826 100644 --- a/lstate.h +++ b/lstate.h @@ -181,7 +181,7 @@ struct CallInfo { union { struct { /* only for Lua functions */ const Instruction *savedpc; - volatile l_signalT trap; + volatile l_signalT trap; /* function is tracing lines/counts */ int nextraargs; /* # of extra arguments in vararg functions */ } l; struct { /* only for C functions */ diff --git a/lvm.c b/lvm.c index 2b437bdfdd..a98aaceb51 100644 --- a/lvm.c +++ b/lvm.c @@ -1157,18 +1157,11 @@ void luaV_execute (lua_State *L, CallInfo *ci) { startfunc: trap = L->hookmask; returning: /* trap already set */ - cl = clLvalue(s2v(ci->func.p)); + cl = ci_func(ci); k = cl->p->k; pc = ci->u.l.savedpc; - if (l_unlikely(trap)) { - if (pc == cl->p->code) { /* first instruction (not resuming)? */ - if (cl->p->is_vararg) - trap = 0; /* hooks will start after VARARGPREP instruction */ - else /* check 'call' hook */ - luaD_hookcall(L, ci); - } - ci->u.l.trap = 1; /* assume trap is on, for now */ - } + if (l_unlikely(trap)) + trap = luaG_tracecall(L); base = ci->func.p + 1; /* main loop of interpreter */ for (;;) { From f4211a5ea4e235ccfa8b8dfa46031c23e9e839e2 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 17 Aug 2023 10:42:56 -0300 Subject: [PATCH 0831/1145] More control over encoding of test files The few UTF-8 test files are commented as such, and there is only one non UTF-8 test file (to test non UTF-8 sources). --- testes/db.lua | 6 ++--- testes/files.lua | 8 +++---- testes/pm.lua | 56 +++++++++++++++++++++++++++++----------------- testes/sort.lua | 2 +- testes/strings.lua | 3 +++ testes/utf8.lua | 2 ++ 6 files changed, 49 insertions(+), 28 deletions(-) diff --git a/testes/db.lua b/testes/db.lua index 67b5893404..d3758c4151 100644 --- a/testes/db.lua +++ b/testes/db.lua @@ -345,7 +345,7 @@ function f(a,b) local _, y = debug.getlocal(1, 2) assert(x == a and y == b) assert(debug.setlocal(2, 3, "pera") == "AA".."AA") - assert(debug.setlocal(2, 4, "ma") == "B") + assert(debug.setlocal(2, 4, "manga") == "B") x = debug.getinfo(2) assert(x.func == g and x.what == "Lua" and x.name == 'g' and x.nups == 2 and string.find(x.source, "^@.*db%.lua$")) @@ -373,9 +373,9 @@ function g (...) local arg = {...} do local a,b,c; a=math.sin(40); end local feijao - local AAAA,B = "xuxu", "mamo" + local AAAA,B = "xuxu", "abacate" f(AAAA,B) - assert(AAAA == "pera" and B == "ma") + assert(AAAA == "pera" and B == "manga") do local B = 13 local x,y = debug.getlocal(1,5) diff --git a/testes/files.lua b/testes/files.lua index be00bf3fd1..1476006e64 100644 --- a/testes/files.lua +++ b/testes/files.lua @@ -92,8 +92,8 @@ assert(io.output():seek("end") == string.len("alo joao")) assert(io.output():seek("set") == 0) -assert(io.write('"lo"', "{a}\n", "second line\n", "third line \n")) -assert(io.write('fourth_line')) +assert(io.write('"alo"', "{a}\n", "second line\n", "third line \n")) +assert(io.write('Xfourth_line')) io.output(io.stdout) collectgarbage() -- file should be closed by GC assert(io.input() == io.stdin and rawequal(io.output(), io.stdout)) @@ -300,14 +300,14 @@ do -- test error returns end checkerr("invalid format", io.read, "x") assert(io.read(0) == "") -- not eof -assert(io.read(5, 'l') == '"lo"') +assert(io.read(5, 'l') == '"alo"') assert(io.read(0) == "") assert(io.read() == "second line") local x = io.input():seek() assert(io.read() == "third line ") assert(io.input():seek("set", x)) assert(io.read('L') == "third line \n") -assert(io.read(1) == "") +assert(io.read(1) == "X") assert(io.read(string.len"fourth_line") == "fourth_line") assert(io.input():seek("cur", -string.len"fourth_line")) assert(io.read() == "fourth_line") diff --git a/testes/pm.lua b/testes/pm.lua index 795596d412..44454dffa8 100644 --- a/testes/pm.lua +++ b/testes/pm.lua @@ -1,6 +1,9 @@ -- $Id: testes/pm.lua $ -- See Copyright Notice in file all.lua +-- UTF-8 file + + print('testing pattern matching') local function checkerror (msg, f, ...) @@ -50,6 +53,19 @@ assert(f('aLo_ALO', '%a*') == 'aLo') assert(f(" \n\r*&\n\r xuxu \n\n", "%g%g%g+") == "xuxu") + +-- Adapt a pattern to UTF-8 +local function PU (p) + -- break '?' into each individual byte of a character + p = string.gsub(p, "(" .. utf8.charpattern .. ")%?", function (c) + return string.gsub(c, ".", "%0?") + end) + -- change '.' to utf-8 character patterns + p = string.gsub(p, "%.", utf8.charpattern) + return p +end + + assert(f('aaab', 'a*') == 'aaa'); assert(f('aaa', '^.*$') == 'aaa'); assert(f('aaa', 'b*') == ''); @@ -73,16 +89,16 @@ assert(f('aaa', '^.-$') == 'aaa') assert(f('aabaaabaaabaaaba', 'b.*b') == 'baaabaaabaaab') assert(f('aabaaabaaabaaaba', 'b.-b') == 'baaab') assert(f('alo xo', '.o$') == 'xo') -assert(f(' \n isto assim', '%S%S*') == 'isto') -assert(f(' \n isto assim', '%S*$') == 'assim') -assert(f(' \n isto assim', '[a-z]*$') == 'assim') +assert(f(' \n isto é assim', '%S%S*') == 'isto') +assert(f(' \n isto é assim', '%S*$') == 'assim') +assert(f(' \n isto é assim', '[a-z]*$') == 'assim') assert(f('um caracter ? extra', '[^%sa-z]') == '?') assert(f('', 'a?') == '') -assert(f('', '?') == '') -assert(f('bl', '?b?l?') == 'bl') -assert(f(' bl', '?b?l?') == '') +assert(f('á', PU'á?') == 'á') +assert(f('ábl', PU'á?b?l?') == 'ábl') +assert(f(' ábl', PU'á?b?l?') == '') assert(f('aa', '^aa?a?a') == 'aa') -assert(f(']]]b', '[^]]') == '') +assert(f(']]]áb', '[^]]+') == 'áb') assert(f("0alo alo", "%x*") == "0a") assert(f("alo alo", "%C+") == "alo alo") print('+') @@ -136,28 +152,28 @@ assert(string.match("alo xyzK", "(%w+)K") == "xyz") assert(string.match("254 K", "(%d*)K") == "") assert(string.match("alo ", "(%w*)$") == "") assert(not string.match("alo ", "(%w+)$")) -assert(string.find("(lo)", "%(") == 1) -local a, b, c, d, e = string.match("lo alo", "^(((.).).* (%w*))$") -assert(a == 'lo alo' and b == 'l' and c == '' and d == 'alo' and e == nil) +assert(string.find("(álo)", "%(á") == 1) +local a, b, c, d, e = string.match("âlo alo", PU"^(((.).). (%w*))$") +assert(a == 'âlo alo' and b == 'âl' and c == 'â' and d == 'alo' and e == nil) a, b, c, d = string.match('0123456789', '(.+(.?)())') assert(a == '0123456789' and b == '' and c == 11 and d == nil) print('+') -assert(string.gsub('lo lo', '', 'x') == 'xlo xlo') -assert(string.gsub('alo lo ', ' +$', '') == 'alo lo') -- trim +assert(string.gsub('ülo ülo', 'ü', 'x') == 'xlo xlo') +assert(string.gsub('alo úlo ', ' +$', '') == 'alo úlo') -- trim assert(string.gsub(' alo alo ', '^%s*(.-)%s*$', '%1') == 'alo alo') -- double trim assert(string.gsub('alo alo \n 123\n ', '%s+', ' ') == 'alo alo 123 ') -local t = "ab d" -a, b = string.gsub(t, '(.)', '%1@') -assert('@'..a == string.gsub(t, '', '@') and b == 5) -a, b = string.gsub('abd', '(.)', '%0@', 2) -assert(a == 'a@b@d' and b == 2) +local t = "abç d" +a, b = string.gsub(t, PU'(.)', '%1@') +assert(a == "a@b@ç@ @d@" and b == 5) +a, b = string.gsub('abçd', PU'(.)', '%0@', 2) +assert(a == 'a@b@çd' and b == 2) assert(string.gsub('alo alo', '()[al]', '%1') == '12o 56o') assert(string.gsub("abc=xyz", "(%w*)(%p)(%w+)", "%3%2%1-%0") == "xyz=abc-abc=xyz") assert(string.gsub("abc", "%w", "%1%0") == "aabbcc") assert(string.gsub("abc", "%w+", "%0%1") == "abcabc") -assert(string.gsub('', '$', '\0') == '\0') +assert(string.gsub('áéí', '$', '\0óú') == 'áéí\0óú') assert(string.gsub('', '^', 'r') == 'r') assert(string.gsub('', '$', 'r') == 'r') print('+') @@ -188,8 +204,8 @@ do end function f(a,b) return string.gsub(a,'.',b) end -assert(string.gsub("trocar tudo em |teste|b| |beleza|al|", "|([^|]*)|([^|]*)|", f) == - "trocar tudo em bbbbb alalalalalal") +assert(string.gsub("trocar tudo em |teste|b| é |beleza|al|", "|([^|]*)|([^|]*)|", f) == + "trocar tudo em bbbbb é alalalalalal") local function dostring (s) return load(s, "")() or "" end assert(string.gsub("alo $a='x'$ novamente $return a$", diff --git a/testes/sort.lua b/testes/sort.lua index 52919b8cd2..40bb2d8a27 100644 --- a/testes/sort.lua +++ b/testes/sort.lua @@ -289,7 +289,7 @@ timesort(a, limit, function(x,y) return nil end, "equal") for i,v in pairs(a) do assert(v == false) end -AA = {"lo", "\0first :-)", "alo", "then this one", "45", "and a new"} +AA = {"\xE1lo", "\0first :-)", "alo", "then this one", "45", "and a new"} table.sort(AA) check(AA) diff --git a/testes/strings.lua b/testes/strings.lua index b033c6ab39..90983edd18 100644 --- a/testes/strings.lua +++ b/testes/strings.lua @@ -1,6 +1,9 @@ -- $Id: testes/strings.lua $ -- See Copyright Notice in file all.lua +-- ISO Latin encoding + + print('testing strings and string library') local maxi = math.maxinteger diff --git a/testes/utf8.lua b/testes/utf8.lua index c5a9dd3f02..efadbd5c39 100644 --- a/testes/utf8.lua +++ b/testes/utf8.lua @@ -1,6 +1,8 @@ -- $Id: testes/utf8.lua $ -- See Copyright Notice in file all.lua +-- UTF-8 file + print "testing UTF-8 library" local utf8 = require'utf8' From 9b4f39ab14fb2e55345c3d23537d129dac23b091 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 17 Aug 2023 15:59:28 -0300 Subject: [PATCH 0832/1145] More disciplined use of 'getstr' and 'tsslen' We may want to add other string variants in the future; this change documents better where the code may need to handle those variants. --- lapi.c | 4 ++-- ldebug.c | 6 +++--- lgc.c | 8 +++++--- lobject.c | 2 +- lobject.h | 18 ++++++++---------- lstate.c | 2 +- lstring.c | 11 ++++++----- lundump.c | 2 +- lvm.c | 17 ++++++++++------- 9 files changed, 37 insertions(+), 33 deletions(-) diff --git a/lapi.c b/lapi.c index 34e64af142..332e97d169 100644 --- a/lapi.c +++ b/lapi.c @@ -417,9 +417,9 @@ LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) { o = index2value(L, idx); /* previous call may reallocate the stack */ } if (len != NULL) - *len = vslen(o); + *len = tsslen(tsvalue(o)); lua_unlock(L); - return svalue(o); + return getstr(tsvalue(o)); } diff --git a/ldebug.c b/ldebug.c index 195d02f802..690ac38f6f 100644 --- a/ldebug.c +++ b/ldebug.c @@ -426,7 +426,7 @@ static const char *getobjname (const Proto *p, int lastpc, int reg, */ static void kname (const Proto *p, int c, const char **name) { TValue *kvalue = &p->k[c]; - *name = (ttisstring(kvalue)) ? svalue(kvalue) : "?"; + *name = (ttisstring(kvalue)) ? getstr(tsvalue(kvalue)) : "?"; } @@ -569,7 +569,7 @@ static const char *getobjname (const Proto *p, int lastpc, int reg, int b = (op == OP_LOADK) ? GETARG_Bx(i) : GETARG_Ax(p->code[pc + 1]); if (ttisstring(&p->k[b])) { - *name = svalue(&p->k[b]); + *name = getstr(tsvalue(&p->k[b])); return "constant"; } break; @@ -627,7 +627,7 @@ static const char *funcnamefromcode (lua_State *L, const Proto *p, default: return NULL; /* cannot find a reasonable name */ } - *name = getstr(G(L)->tmname[tm]) + 2; + *name = getshrstr(G(L)->tmname[tm]) + 2; return "metamethod"; } diff --git a/lgc.c b/lgc.c index f8f43393e0..253a2892c9 100644 --- a/lgc.c +++ b/lgc.c @@ -542,10 +542,12 @@ static void traversestrongtable (global_State *g, Table *h) { static lu_mem traversetable (global_State *g, Table *h) { const char *weakkey, *weakvalue; const TValue *mode = gfasttm(g, h->metatable, TM_MODE); + TString *smode; markobjectN(g, h->metatable); - if (mode && ttisstring(mode) && /* is there a weak mode? */ - (cast_void(weakkey = strchr(svalue(mode), 'k')), - cast_void(weakvalue = strchr(svalue(mode), 'v')), + if (mode && ttisshrstring(mode) && /* is there a weak mode? */ + (cast_void(smode = tsvalue(mode)), + cast_void(weakkey = strchr(getshrstr(smode), 'k')), + cast_void(weakvalue = strchr(getshrstr(smode), 'v')), (weakkey || weakvalue))) { /* is really weak? */ if (!weakkey) /* strong keys? */ traverseweakvalue(g, h); diff --git a/lobject.c b/lobject.c index f73ffc6d92..9cfa5227eb 100644 --- a/lobject.c +++ b/lobject.c @@ -542,7 +542,7 @@ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) { addstr2buff(&buff, fmt, strlen(fmt)); /* rest of 'fmt' */ clearbuff(&buff); /* empty buffer into the stack */ lua_assert(buff.pushed == 1); - return svalue(s2v(L->top.p - 1)); + return getstr(tsvalue(s2v(L->top.p - 1))); } diff --git a/lobject.h b/lobject.h index 556608e4aa..980e42f8c2 100644 --- a/lobject.h +++ b/lobject.h @@ -386,7 +386,7 @@ typedef struct GCObject { typedef struct TString { CommonHeader; lu_byte extra; /* reserved words for short strings; "has hash" for longs */ - lu_byte shrlen; /* length for short strings */ + lu_byte shrlen; /* length for short strings, 0xFF for long strings */ unsigned int hash; union { size_t lnglen; /* length for long strings */ @@ -398,19 +398,17 @@ typedef struct TString { /* -** Get the actual string (array of bytes) from a 'TString'. +** Get the actual string (array of bytes) from a 'TString'. (Generic +** version and specialized versions for long and short strings.) */ -#define getstr(ts) ((ts)->contents) +#define getstr(ts) ((ts)->contents) +#define getlngstr(ts) check_exp((ts)->shrlen == 0xFF, (ts)->contents) +#define getshrstr(ts) check_exp((ts)->shrlen != 0xFF, (ts)->contents) -/* get the actual string (array of bytes) from a Lua value */ -#define svalue(o) getstr(tsvalue(o)) - /* get string length from 'TString *s' */ -#define tsslen(s) ((s)->tt == LUA_VSHRSTR ? (s)->shrlen : (s)->u.lnglen) - -/* get string length from 'TValue *o' */ -#define vslen(o) tsslen(tsvalue(o)) +#define tsslen(s) \ + ((s)->shrlen != 0xFF ? (s)->shrlen : (s)->u.lnglen) /* }================================================================== */ diff --git a/lstate.c b/lstate.c index 06667dacf9..7fefacba2c 100644 --- a/lstate.c +++ b/lstate.c @@ -433,7 +433,7 @@ void luaE_warning (lua_State *L, const char *msg, int tocont) { void luaE_warnerror (lua_State *L, const char *where) { TValue *errobj = s2v(L->top.p - 1); /* error object */ const char *msg = (ttisstring(errobj)) - ? svalue(errobj) + ? getstr(tsvalue(errobj)) : "error object is not a string"; /* produce warning "error in %s (%s)" (where, msg) */ luaE_warning(L, "error in ", 1); diff --git a/lstring.c b/lstring.c index 13dcaf4259..1032ad8692 100644 --- a/lstring.c +++ b/lstring.c @@ -36,7 +36,7 @@ int luaS_eqlngstr (TString *a, TString *b) { lua_assert(a->tt == LUA_VLNGSTR && b->tt == LUA_VLNGSTR); return (a == b) || /* same instance or... */ ((len == b->u.lnglen) && /* equal length and ... */ - (memcmp(getstr(a), getstr(b), len) == 0)); /* equal contents */ + (memcmp(getlngstr(a), getlngstr(b), len) == 0)); /* equal contents */ } @@ -52,7 +52,7 @@ unsigned int luaS_hashlongstr (TString *ts) { lua_assert(ts->tt == LUA_VLNGSTR); if (ts->extra == 0) { /* no hash? */ size_t len = ts->u.lnglen; - ts->hash = luaS_hash(getstr(ts), len, ts->hash); + ts->hash = luaS_hash(getlngstr(ts), len, ts->hash); ts->extra = 1; /* now it has its hash */ } return ts->hash; @@ -157,6 +157,7 @@ static TString *createstrobj (lua_State *L, size_t l, int tag, unsigned int h) { TString *luaS_createlngstrobj (lua_State *L, size_t l) { TString *ts = createstrobj(L, l, LUA_VLNGSTR, G(L)->seed); ts->u.lnglen = l; + ts->shrlen = 0xFF; /* signals that it is a long string */ return ts; } @@ -193,7 +194,7 @@ static TString *internshrstr (lua_State *L, const char *str, size_t l) { TString **list = &tb->hash[lmod(h, tb->size)]; lua_assert(str != NULL); /* otherwise 'memcmp'/'memcpy' are undefined */ for (ts = *list; ts != NULL; ts = ts->u.hnext) { - if (l == ts->shrlen && (memcmp(str, getstr(ts), l * sizeof(char)) == 0)) { + if (l == ts->shrlen && (memcmp(str, getshrstr(ts), l * sizeof(char)) == 0)) { /* found! */ if (isdead(g, ts)) /* dead (but not collected yet)? */ changewhite(ts); /* resurrect it */ @@ -206,7 +207,7 @@ static TString *internshrstr (lua_State *L, const char *str, size_t l) { list = &tb->hash[lmod(h, tb->size)]; /* rehash with new size */ } ts = createstrobj(L, l, LUA_VSHRSTR, h); - memcpy(getstr(ts), str, l * sizeof(char)); + memcpy(getshrstr(ts), str, l * sizeof(char)); ts->shrlen = cast_byte(l); ts->u.hnext = *list; *list = ts; @@ -226,7 +227,7 @@ TString *luaS_newlstr (lua_State *L, const char *str, size_t l) { if (l_unlikely(l >= (MAX_SIZE - sizeof(TString))/sizeof(char))) luaM_toobig(L); ts = luaS_createlngstrobj(L, l); - memcpy(getstr(ts), str, l * sizeof(char)); + memcpy(getlngstr(ts), str, l * sizeof(char)); return ts; } } diff --git a/lundump.c b/lundump.c index 02aed64fb6..f1852c3581 100644 --- a/lundump.c +++ b/lundump.c @@ -122,7 +122,7 @@ static TString *loadStringN (LoadState *S, Proto *p) { ts = luaS_createlngstrobj(L, size); /* create string */ setsvalue2s(L, L->top.p, ts); /* anchor it ('loadVector' can GC) */ luaD_inctop(L); - loadVector(S, getstr(ts), size); /* load directly in final place */ + loadVector(S, getlngstr(ts), size); /* load directly in final place */ L->top.p--; /* pop string */ } luaC_objbarrier(L, p, ts); diff --git a/lvm.c b/lvm.c index a98aaceb51..4d71cfffd0 100644 --- a/lvm.c +++ b/lvm.c @@ -91,8 +91,10 @@ static int l_strton (const TValue *obj, TValue *result) { lua_assert(obj != result); if (!cvt2num(obj)) /* is object not a string? */ return 0; - else - return (luaO_str2num(svalue(obj), result) == vslen(obj) + 1); + else { + TString *st = tsvalue(obj); + return (luaO_str2num(getstr(st), result) == tsslen(st) + 1); + } } @@ -626,8 +628,9 @@ int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2) { static void copy2buff (StkId top, int n, char *buff) { size_t tl = 0; /* size already copied */ do { - size_t l = vslen(s2v(top - n)); /* length of string being copied */ - memcpy(buff + tl, svalue(s2v(top - n)), l * sizeof(char)); + TString *st = tsvalue(s2v(top - n)); + size_t l = tsslen(st); /* length of string being copied */ + memcpy(buff + tl, getstr(st), l * sizeof(char)); tl += l; } while (--n > 0); } @@ -653,11 +656,11 @@ void luaV_concat (lua_State *L, int total) { } else { /* at least two non-empty string values; get as many as possible */ - size_t tl = vslen(s2v(top - 1)); + size_t tl = tsslen(tsvalue(s2v(top - 1))); TString *ts; /* collect total length and number of strings */ for (n = 1; n < total && tostring(L, s2v(top - n - 1)); n++) { - size_t l = vslen(s2v(top - n - 1)); + size_t l = tsslen(tsvalue(s2v(top - n - 1))); if (l_unlikely(l >= (MAX_SIZE/sizeof(char)) - tl)) { L->top.p = top - total; /* pop strings to avoid wasting stack */ luaG_runerror(L, "string length overflow"); @@ -671,7 +674,7 @@ void luaV_concat (lua_State *L, int total) { } else { /* long string; copy strings directly to final result */ ts = luaS_createlngstrobj(L, tl); - copy2buff(top, n, getstr(ts)); + copy2buff(top, n, getlngstr(ts)); } setsvalue2s(L, top - n, ts); /* create result */ } From 5ab6a5756b3c50c99f1388885e9a48a7da8cbe2d Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 23 Aug 2023 13:49:27 -0300 Subject: [PATCH 0833/1145] Bug: Wrong line number for function calls --- lparser.c | 12 ++++++------ testes/errors.lua | 8 ++++---- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/lparser.c b/lparser.c index b745f236f0..2b888c7cff 100644 --- a/lparser.c +++ b/lparser.c @@ -1022,10 +1022,11 @@ static int explist (LexState *ls, expdesc *v) { } -static void funcargs (LexState *ls, expdesc *f, int line) { +static void funcargs (LexState *ls, expdesc *f) { FuncState *fs = ls->fs; expdesc args; int base, nparams; + int line = ls->linenumber; switch (ls->t.token) { case '(': { /* funcargs -> '(' [ explist ] ')' */ luaX_next(ls); @@ -1063,8 +1064,8 @@ static void funcargs (LexState *ls, expdesc *f, int line) { } init_exp(f, VCALL, luaK_codeABC(fs, OP_CALL, base, nparams+1, 2)); luaK_fixline(fs, line); - fs->freereg = base+1; /* call remove function and arguments and leaves - (unless changed) one result */ + fs->freereg = base+1; /* call removes function and arguments and leaves + one result (unless changed later) */ } @@ -1103,7 +1104,6 @@ static void suffixedexp (LexState *ls, expdesc *v) { /* suffixedexp -> primaryexp { '.' NAME | '[' exp ']' | ':' NAME funcargs | funcargs } */ FuncState *fs = ls->fs; - int line = ls->linenumber; primaryexp(ls, v); for (;;) { switch (ls->t.token) { @@ -1123,12 +1123,12 @@ static void suffixedexp (LexState *ls, expdesc *v) { luaX_next(ls); codename(ls, &key); luaK_self(fs, v, &key); - funcargs(ls, v, line); + funcargs(ls, v); break; } case '(': case TK_STRING: case '{': { /* funcargs */ luaK_exp2nextreg(fs, v); - funcargs(ls, v, line); + funcargs(ls, v); break; } default: return; diff --git a/testes/errors.lua b/testes/errors.lua index bf6f389d26..b777a3298a 100644 --- a/testes/errors.lua +++ b/testes/errors.lua @@ -392,19 +392,19 @@ lineerror("a\n=\n-\n\nprint\n;", 3) lineerror([[ a -( +( -- << 23) -]], 1) +]], 2) lineerror([[ local a = {x = 13} a . x -( +( -- << 23 ) -]], 2) +]], 5) lineerror([[ local a = {x = 13} From 9363a8b9901a5643c9da061ea8dda8a86cdc7ef1 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 23 Aug 2023 13:50:38 -0300 Subject: [PATCH 0834/1145] Documentation for "LUA_NOENV" Registry field "LUA_NOENV" (that signals to libraries that option -E is on) now part of the "official" API of Lua stand-alone. --- manual/manual.of | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/manual/manual.of b/manual/manual.of index f8d8ddd40e..ad120f5e48 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -9026,6 +9026,10 @@ Lua does not consult any environment variables. In particular, the values of @Lid{package.path} and @Lid{package.cpath} are set with the default paths defined in @id{luaconf.h}. +To signal to the libraries that this option is on, +the stand-alone interpreter sets the field +@idx{"LUA_NOENV"} in the registry to a true value. +Other libraries may consult this field for the same purpose. The options @T{-e}, @T{-l}, and @T{-W} are handled in the order they appear. From 07a9eab23ac073362f231ddc7215688cf221ff45 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 25 Aug 2023 15:55:14 -0300 Subject: [PATCH 0835/1145] Cannot use 'getshrstr' before setting 'shrlen' --- lstring.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lstring.c b/lstring.c index 1032ad8692..e921dd0f34 100644 --- a/lstring.c +++ b/lstring.c @@ -207,8 +207,8 @@ static TString *internshrstr (lua_State *L, const char *str, size_t l) { list = &tb->hash[lmod(h, tb->size)]; /* rehash with new size */ } ts = createstrobj(L, l, LUA_VSHRSTR, h); - memcpy(getshrstr(ts), str, l * sizeof(char)); ts->shrlen = cast_byte(l); + memcpy(getshrstr(ts), str, l * sizeof(char)); ts->u.hnext = *list; *list = ts; tb->nuse++; From 05545816057cfdc54bb58199388a2d8878ae5542 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 25 Aug 2023 17:40:20 -0300 Subject: [PATCH 0836/1145] Opcode in dumps is stored properly aligned --- ldump.c | 14 ++++++++++++++ lundump.c | 17 ++++++++++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/ldump.c b/ldump.c index 3e91190c7c..50d09f9a49 100644 --- a/ldump.c +++ b/ldump.c @@ -26,6 +26,7 @@ typedef struct { lua_State *L; lua_Writer writer; void *data; + lu_mem offset; /* current position relative to beginning of dump */ int strip; int status; Table *h; /* table to track saved strings */ @@ -47,6 +48,17 @@ static void dumpBlock (DumpState *D, const void *b, size_t size) { lua_unlock(D->L); D->status = (*D->writer)(D->L, b, size, D->data); lua_lock(D->L); + D->offset += size; + } +} + + +static void dumpAlign (DumpState *D, int align) { + int padding = align - (D->offset % align); + if (padding < align) { /* apd == align means no padding */ + static lua_Integer paddingContent = 0; + dumpBlock(D, &paddingContent, padding); + lua_assert(D->offset % align == 0); } } @@ -126,6 +138,7 @@ static void dumpString (DumpState *D, TString *s) { static void dumpCode (DumpState *D, const Proto *f) { dumpInt(D, f->sizecode); + dumpAlign(D, sizeof(f->code[0])); dumpVector(D, f->code, f->sizecode); } @@ -242,6 +255,7 @@ int luaU_dump(lua_State *L, const Proto *f, lua_Writer w, void *data, DumpState D; D.L = L; D.writer = w; + D.offset = 0; D.data = data; D.strip = strip; D.status = 0; diff --git a/lundump.c b/lundump.c index 0c93c99596..674bf21fb3 100644 --- a/lundump.c +++ b/lundump.c @@ -36,6 +36,7 @@ typedef struct { ZIO *Z; const char *name; Table *h; /* list for string reuse */ + lu_mem offset; /* current position relative to beginning of dump */ lua_Integer nstr; /* number of strings in the list */ } LoadState; @@ -55,6 +56,17 @@ static l_noret error (LoadState *S, const char *why) { static void loadBlock (LoadState *S, void *b, size_t size) { if (luaZ_read(S->Z, b, size) != 0) error(S, "truncated chunk"); + S->offset += size; +} + + +static void loadAlign (LoadState *S, int align) { + int padding = align - (S->offset % align); + if (padding < align) { /* apd == align means no padding */ + lua_Integer paddingContent; + loadBlock(S, &paddingContent, padding); + lua_assert(S->offset % align == 0); + } } @@ -65,6 +77,7 @@ static lu_byte loadByte (LoadState *S) { int b = zgetc(S->Z); if (b == EOZ) error(S, "truncated chunk"); + S->offset++; return cast_byte(b); } @@ -158,6 +171,7 @@ static void loadCode (LoadState *S, Proto *f) { int n = loadInt(S); f->code = luaM_newvectorchecked(S->L, n, Instruction); f->sizecode = n; + loadAlign(S, sizeof(f->code[0])); loadVector(S, f->code, n); } @@ -321,7 +335,7 @@ static void checkHeader (LoadState *S) { /* ** Load precompiled chunk. */ -LClosure *luaU_undump(lua_State *L, ZIO *Z, const char *name) { +LClosure *luaU_undump (lua_State *L, ZIO *Z, const char *name) { LoadState S; LClosure *cl; if (*name == '@' || *name == '=') @@ -332,6 +346,7 @@ LClosure *luaU_undump(lua_State *L, ZIO *Z, const char *name) { S.name = name; S.L = L; S.Z = Z; + S.offset = 1; /* fist byte was already read */ checkHeader(&S); cl = luaF_newLclosure(L, loadByte(&S)); setclLvalue2s(L, L->top.p, cl); From 96f77142374da8a4a7d4e5e8afd559fbaf0430e8 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 30 Aug 2023 10:44:28 -0300 Subject: [PATCH 0837/1145] Field 'Proto.is_vararg' uses only one bit So that the other bits can be used for other purposes. --- lcode.c | 4 ++-- ldebug.c | 8 ++++---- ldo.c | 2 +- ldump.c | 2 +- lfunc.c | 2 +- lobject.h | 9 ++++++++- lparser.c | 4 ++-- lundump.c | 2 +- 8 files changed, 20 insertions(+), 13 deletions(-) diff --git a/lcode.c b/lcode.c index caac6ba322..914d8cd097 100644 --- a/lcode.c +++ b/lcode.c @@ -1849,7 +1849,7 @@ void luaK_finish (FuncState *fs) { lua_assert(i == 0 || isOT(*(pc - 1)) == isIT(*pc)); switch (GET_OPCODE(*pc)) { case OP_RETURN0: case OP_RETURN1: { - if (!(fs->needclose || p->is_vararg)) + if (!(fs->needclose || (p->flag & PF_ISVARARG))) break; /* no extra work */ /* else use OP_RETURN to do the extra work */ SET_OPCODE(*pc, OP_RETURN); @@ -1857,7 +1857,7 @@ void luaK_finish (FuncState *fs) { case OP_RETURN: case OP_TAILCALL: { if (fs->needclose) SETARG_k(*pc, 1); /* signal that it needs to close */ - if (p->is_vararg) + if (p->flag & PF_ISVARARG) SETARG_C(*pc, p->numparams + 1); /* signal that it is vararg */ break; } diff --git a/ldebug.c b/ldebug.c index 690ac38f6f..1b789520b3 100644 --- a/ldebug.c +++ b/ldebug.c @@ -182,7 +182,7 @@ static const char *upvalname (const Proto *p, int uv) { static const char *findvararg (CallInfo *ci, int n, StkId *pos) { - if (clLvalue(s2v(ci->func.p))->p->is_vararg) { + if (clLvalue(s2v(ci->func.p))->p->flag & PF_ISVARARG) { int nextra = ci->u.l.nextraargs; if (n >= -nextra) { /* 'n' is negative */ *pos = ci->func.p - nextra - (n + 1); @@ -301,7 +301,7 @@ static void collectvalidlines (lua_State *L, Closure *f) { sethvalue2s(L, L->top.p, t); /* push it on stack */ api_incr_top(L); setbtvalue(&v); /* boolean 'true' to be the value of all indices */ - if (!p->is_vararg) /* regular function? */ + if (!(p->flag & PF_ISVARARG)) /* regular function? */ i = 0; /* consider all instructions */ else { /* vararg function */ lua_assert(GET_OPCODE(p->code[0]) == OP_VARARGPREP); @@ -344,7 +344,7 @@ static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar, ar->nparams = 0; } else { - ar->isvararg = f->l.p->is_vararg; + ar->isvararg = f->l.p->flag & PF_ISVARARG; ar->nparams = f->l.p->numparams; } break; @@ -878,7 +878,7 @@ int luaG_tracecall (lua_State *L) { Proto *p = ci_func(ci)->p; ci->u.l.trap = 1; /* ensure hooks will be checked */ if (ci->u.l.savedpc == p->code) { /* first instruction (not resuming)? */ - if (p->is_vararg) + if (p->flag & PF_ISVARARG) return 0; /* hooks will start at VARARGPREP instruction */ else if (!(ci->callstatus & CIST_HOOKYIELD)) /* not yieded? */ luaD_hookcall(L, ci); /* check 'call' hook */ diff --git a/ldo.c b/ldo.c index 3df6a4b846..a0e002299b 100644 --- a/ldo.c +++ b/ldo.c @@ -391,7 +391,7 @@ static void rethook (lua_State *L, CallInfo *ci, int nres) { int ftransfer; if (isLua(ci)) { Proto *p = ci_func(ci)->p; - if (p->is_vararg) + if (p->flag & PF_ISVARARG) delta = ci->u.l.nextraargs + p->numparams + 1; } ci->func.p += delta; /* if vararg, back to virtual 'func' */ diff --git a/ldump.c b/ldump.c index 50d09f9a49..bb15e45f0c 100644 --- a/ldump.c +++ b/ldump.c @@ -224,7 +224,7 @@ static void dumpFunction (DumpState *D, const Proto *f) { dumpInt(D, f->linedefined); dumpInt(D, f->lastlinedefined); dumpByte(D, f->numparams); - dumpByte(D, f->is_vararg); + dumpByte(D, f->flag); dumpByte(D, f->maxstacksize); dumpCode(D, f); dumpConstants(D, f); diff --git a/lfunc.c b/lfunc.c index 0945f241de..9866a2d52d 100644 --- a/lfunc.c +++ b/lfunc.c @@ -253,7 +253,7 @@ Proto *luaF_newproto (lua_State *L) { f->upvalues = NULL; f->sizeupvalues = 0; f->numparams = 0; - f->is_vararg = 0; + f->flag = 0; f->maxstacksize = 0; f->locvars = NULL; f->sizelocvars = 0; diff --git a/lobject.h b/lobject.h index 79dc6b1c8d..0e4924f523 100644 --- a/lobject.h +++ b/lobject.h @@ -544,13 +544,20 @@ typedef struct AbsLineInfo { int line; } AbsLineInfo; + +/* +** Flags in Prototypes +*/ +#define PF_ISVARARG 1 + + /* ** Function Prototypes */ typedef struct Proto { CommonHeader; lu_byte numparams; /* number of fixed (named) parameters */ - lu_byte is_vararg; + lu_byte flag; lu_byte maxstacksize; /* number of registers needed by this function */ int sizeupvalues; /* size of 'upvalues' */ int sizek; /* size of 'k' */ diff --git a/lparser.c b/lparser.c index 81c6df2277..a116451062 100644 --- a/lparser.c +++ b/lparser.c @@ -959,7 +959,7 @@ static void constructor (LexState *ls, expdesc *t) { static void setvararg (FuncState *fs, int nparams) { - fs->f->is_vararg = 1; + fs->f->flag |= PF_ISVARARG; luaK_codeABC(fs, OP_VARARGPREP, nparams, 0, 0); } @@ -1177,7 +1177,7 @@ static void simpleexp (LexState *ls, expdesc *v) { } case TK_DOTS: { /* vararg */ FuncState *fs = ls->fs; - check_condition(ls, fs->f->is_vararg, + check_condition(ls, fs->f->flag & PF_ISVARARG, "cannot use '...' outside a vararg function"); init_exp(v, VVARARG, luaK_codeABC(fs, OP_VARARG, 0, 0, 1)); break; diff --git a/lundump.c b/lundump.c index 674bf21fb3..07c42e622b 100644 --- a/lundump.c +++ b/lundump.c @@ -287,7 +287,7 @@ static void loadFunction (LoadState *S, Proto *f) { f->linedefined = loadInt(S); f->lastlinedefined = loadInt(S); f->numparams = loadByte(S); - f->is_vararg = loadByte(S); + f->flag = loadByte(S) & PF_ISVARARG; /* keep only the meaningful flags */ f->maxstacksize = loadByte(S); loadCode(S, f); loadConstants(S, f); From f33cda8d6eb1cac5b9042429e85f1096175c7ca5 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 30 Aug 2023 11:26:16 -0300 Subject: [PATCH 0838/1145] New macro 'getlstr' Accesses content and length of a 'TString'. --- ldebug.c | 10 ++++++---- ldump.c | 15 ++++++++------- lobject.h | 11 +++++++++-- lparser.c | 6 ++++-- lvm.c | 17 ++++++++++------- 5 files changed, 37 insertions(+), 22 deletions(-) diff --git a/ldebug.c b/ldebug.c index 1b789520b3..504459a677 100644 --- a/ldebug.c +++ b/ldebug.c @@ -264,8 +264,7 @@ static void funcinfo (lua_Debug *ar, Closure *cl) { else { const Proto *p = cl->l.p; if (p->source) { - ar->source = getstr(p->source); - ar->srclen = tsslen(p->source); + ar->source = getlstr(p->source, ar->srclen); } else { ar->source = "=?"; @@ -797,8 +796,11 @@ l_noret luaG_ordererror (lua_State *L, const TValue *p1, const TValue *p2) { const char *luaG_addinfo (lua_State *L, const char *msg, TString *src, int line) { char buff[LUA_IDSIZE]; - if (src) - luaO_chunkid(buff, getstr(src), tsslen(src)); + if (src) { + size_t idlen; + const char *id = getlstr(src, idlen); + luaO_chunkid(buff, id, idlen); + } else { /* no source available; use "?" instead */ buff[0] = '?'; buff[1] = '\0'; } diff --git a/ldump.c b/ldump.c index bb15e45f0c..01169c12bd 100644 --- a/ldump.c +++ b/ldump.c @@ -112,24 +112,25 @@ static void dumpInteger (DumpState *D, lua_Integer x) { ** size==size-2 and means that string, which will be saved with ** the next available index. */ -static void dumpString (DumpState *D, TString *s) { - if (s == NULL) +static void dumpString (DumpState *D, TString *ts) { + if (ts == NULL) dumpSize(D, 0); else { - const TValue *idx = luaH_getstr(D->h, s); + const TValue *idx = luaH_getstr(D->h, ts); if (ttisinteger(idx)) { /* string already saved? */ dumpSize(D, 1); /* reuse a saved string */ dumpInt(D, ivalue(idx)); /* index of saved string */ } else { /* must write and save the string */ TValue key, value; /* to save the string in the hash */ - size_t size = tsslen(s); + size_t size; + const char *s = getlstr(ts, size); dumpSize(D, size + 2); - dumpVector(D, getstr(s), size); + dumpVector(D, s, size); D->nstr++; /* one more saved string */ - setsvalue(D->L, &key, s); /* the string is the key */ + setsvalue(D->L, &key, ts); /* the string is the key */ setivalue(&value, D->nstr); /* its index is the value */ - luaH_finishset(D->L, D->h, &key, idx, &value); /* h[s] = nstr */ + luaH_finishset(D->L, D->h, &key, idx, &value); /* h[ts] = nstr */ /* integer value does not need barrier */ } } diff --git a/lobject.h b/lobject.h index 0e4924f523..9e7953e372 100644 --- a/lobject.h +++ b/lobject.h @@ -392,7 +392,7 @@ typedef struct TString { size_t lnglen; /* length for long strings */ struct TString *hnext; /* linked list for hash table */ } u; - char contents[1]; + char contents[1]; /* string body starts here */ } TString; @@ -401,15 +401,22 @@ typedef struct TString { ** Get the actual string (array of bytes) from a 'TString'. (Generic ** version and specialized versions for long and short strings.) */ -#define getstr(ts) ((ts)->contents) #define getlngstr(ts) check_exp((ts)->shrlen == 0xFF, (ts)->contents) #define getshrstr(ts) check_exp((ts)->shrlen != 0xFF, (ts)->contents) +#define getstr(ts) ((ts)->contents) /* get string length from 'TString *s' */ #define tsslen(s) \ ((s)->shrlen != 0xFF ? (s)->shrlen : (s)->u.lnglen) +/* +** Get string and length */ +#define getlstr(ts, len) \ + ((ts)->shrlen != 0xFF \ + ? (cast_void(len = (ts)->shrlen), (ts)->contents) \ + : (cast_void(len = (ts)->u.lnglen), (ts)->contents)) + /* }================================================================== */ diff --git a/lparser.c b/lparser.c index a116451062..2a84637a44 100644 --- a/lparser.c +++ b/lparser.c @@ -520,7 +520,8 @@ static void adjust_assign (LexState *ls, int nvars, int nexps, expdesc *e) { ** local variable. */ static l_noret jumpscopeerror (LexState *ls, Labeldesc *gt) { - const char *varname = getstr(getlocalvardesc(ls->fs, gt->nactvar)->vd.name); + TString *tsname = getlocalvardesc(ls->fs, gt->nactvar)->vd.name; + const char *varname = getstr(tsname); const char *msg = " at line %d jumps into the scope of local '%s'"; msg = luaO_pushfstring(ls->L, msg, getstr(gt->name), gt->line, varname); luaK_semerror(ls, msg); /* raise the error */ @@ -1708,7 +1709,8 @@ static void localfunc (LexState *ls) { static int getlocalattribute (LexState *ls) { /* ATTRIB -> ['<' Name '>'] */ if (testnext(ls, '<')) { - const char *attr = getstr(str_checkname(ls)); + TString *ts = str_checkname(ls); + const char *attr = getstr(ts); checknext(ls, '>'); if (strcmp(attr, "const") == 0) return RDKCONST; /* read-only variable */ diff --git a/lvm.c b/lvm.c index 256d689f64..0a6d62702d 100644 --- a/lvm.c +++ b/lvm.c @@ -93,7 +93,9 @@ static int l_strton (const TValue *obj, TValue *result) { return 0; else { TString *st = tsvalue(obj); - return (luaO_str2num(getstr(st), result) == tsslen(st) + 1); + size_t stlen; + const char *s = getlstr(st, stlen); + return (luaO_str2num(s, result) == stlen + 1); } } @@ -377,10 +379,10 @@ void luaV_finishset (lua_State *L, const TValue *t, TValue *key, ** have different lengths. */ static int l_strcmp (const TString *ts1, const TString *ts2) { - const char *s1 = getstr(ts1); - size_t rl1 = tsslen(ts1); /* real length */ - const char *s2 = getstr(ts2); - size_t rl2 = tsslen(ts2); + size_t rl1; /* real length */ + const char *s1 = getlstr(ts1, rl1); + size_t rl2; + const char *s2 = getlstr(ts2, rl2); for (;;) { /* for each segment */ int temp = strcoll(s1, s2); if (temp != 0) /* not equal? */ @@ -630,8 +632,9 @@ static void copy2buff (StkId top, int n, char *buff) { size_t tl = 0; /* size already copied */ do { TString *st = tsvalue(s2v(top - n)); - size_t l = tsslen(st); /* length of string being copied */ - memcpy(buff + tl, getstr(st), l * sizeof(char)); + size_t l; /* length of string being copied */ + const char *s = getlstr(st, l); + memcpy(buff + tl, s, l * sizeof(char)); tl += l; } while (--n > 0); } From 14e416355f83cf0a1b871eedec2c92b86dbe76d6 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 5 Sep 2023 15:30:45 -0300 Subject: [PATCH 0839/1145] Added suport for Fixed Buffers A fixed buffer keeps a binary chunk "forever", so that the program does not need to copy some of its parts when loading it. --- lbaselib.c | 15 ++++++++++++-- ldo.c | 13 ++++++++---- lfunc.c | 6 ++++-- lobject.h | 1 + lstring.c | 2 +- ltests.c | 7 +++++-- lundump.c | 42 +++++++++++++++++++++++++++++++-------- lundump.h | 3 ++- lzio.c | 36 ++++++++++++++++++++++++++-------- lzio.h | 1 + manual/manual.of | 17 +++++++++++++++- testes/api.lua | 51 +++++++++++++++++++++++++++++++++++++++++++----- 12 files changed, 160 insertions(+), 34 deletions(-) diff --git a/lbaselib.c b/lbaselib.c index 1d60c9dede..5a23c937c6 100644 --- a/lbaselib.c +++ b/lbaselib.c @@ -337,9 +337,20 @@ static int load_aux (lua_State *L, int status, int envidx) { } +static const char *getmode (lua_State *L, int idx) { + const char *mode = luaL_optstring(L, idx, "bt"); + int i = 0; + if (mode[i] == 'b') i++; + if (mode[i] == 't') i++; + if (mode[i] != '\0') + luaL_argerror(L, idx, "invalid mode"); + return mode; +} + + static int luaB_loadfile (lua_State *L) { const char *fname = luaL_optstring(L, 1, NULL); - const char *mode = luaL_optstring(L, 2, NULL); + const char *mode = getmode(L, 2); int env = (!lua_isnone(L, 3) ? 3 : 0); /* 'env' index or 0 if no 'env' */ int status = luaL_loadfilex(L, fname, mode); return load_aux(L, status, env); @@ -388,7 +399,7 @@ static int luaB_load (lua_State *L) { int status; size_t l; const char *s = lua_tolstring(L, 1, &l); - const char *mode = luaL_optstring(L, 3, "bt"); + const char *mode = getmode(L, 3); int env = (!lua_isnone(L, 4) ? 4 : 0); /* 'env' index or 0 if no 'env' */ if (s != NULL) { /* loading a string? */ const char *chunkname = luaL_optstring(L, 2, s); diff --git a/ldo.c b/ldo.c index a0e002299b..e9f9138480 100644 --- a/ldo.c +++ b/ldo.c @@ -977,7 +977,7 @@ struct SParser { /* data to 'f_parser' */ static void checkmode (lua_State *L, const char *mode, const char *x) { - if (mode && strchr(mode, x[0]) == NULL) { + if (strchr(mode, x[0]) == NULL) { luaO_pushfstring(L, "attempt to load a %s chunk (mode is '%s')", x, mode); luaD_throw(L, LUA_ERRSYNTAX); @@ -988,13 +988,18 @@ static void checkmode (lua_State *L, const char *mode, const char *x) { static void f_parser (lua_State *L, void *ud) { LClosure *cl; struct SParser *p = cast(struct SParser *, ud); + const char *mode = p->mode ? p->mode : "bt"; int c = zgetc(p->z); /* read first character */ if (c == LUA_SIGNATURE[0]) { - checkmode(L, p->mode, "binary"); - cl = luaU_undump(L, p->z, p->name); + int fixed = 0; + if (strchr(mode, 'B') != NULL) + fixed = 1; + else + checkmode(L, mode, "binary"); + cl = luaU_undump(L, p->z, p->name, fixed); } else { - checkmode(L, p->mode, "text"); + checkmode(L, mode, "text"); cl = luaY_parser(L, p->z, &p->buff, &p->dyd, p->name, c); } lua_assert(cl->nupvalues == cl->p->sizeupvalues); diff --git a/lfunc.c b/lfunc.c index 9866a2d52d..066409c08a 100644 --- a/lfunc.c +++ b/lfunc.c @@ -265,10 +265,12 @@ Proto *luaF_newproto (lua_State *L) { void luaF_freeproto (lua_State *L, Proto *f) { - luaM_freearray(L, f->code, f->sizecode); + if (!(f->flag & PF_FIXED)) { + luaM_freearray(L, f->code, f->sizecode); + luaM_freearray(L, f->lineinfo, f->sizelineinfo); + } luaM_freearray(L, f->p, f->sizep); luaM_freearray(L, f->k, f->sizek); - luaM_freearray(L, f->lineinfo, f->sizelineinfo); luaM_freearray(L, f->abslineinfo, f->sizeabslineinfo); luaM_freearray(L, f->locvars, f->sizelocvars); luaM_freearray(L, f->upvalues, f->sizeupvalues); diff --git a/lobject.h b/lobject.h index 9e7953e372..209a87fcbe 100644 --- a/lobject.h +++ b/lobject.h @@ -556,6 +556,7 @@ typedef struct AbsLineInfo { ** Flags in Prototypes */ #define PF_ISVARARG 1 +#define PF_FIXED 2 /* prototype has parts in fixed memory */ /* diff --git a/lstring.c b/lstring.c index 1032ad8692..e921dd0f34 100644 --- a/lstring.c +++ b/lstring.c @@ -207,8 +207,8 @@ static TString *internshrstr (lua_State *L, const char *str, size_t l) { list = &tb->hash[lmod(h, tb->size)]; /* rehash with new size */ } ts = createstrobj(L, l, LUA_VSHRSTR, h); - memcpy(getshrstr(ts), str, l * sizeof(char)); ts->shrlen = cast_byte(l); + memcpy(getshrstr(ts), str, l * sizeof(char)); ts->u.hnext = *list; *list = ts; tb->nuse++; diff --git a/ltests.c b/ltests.c index e218778a46..15d1a564b0 100644 --- a/ltests.c +++ b/ltests.c @@ -1513,8 +1513,11 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) { luaL_loadfile(L1, luaL_checkstring(L1, getnum)); } else if EQ("loadstring") { - const char *s = luaL_checkstring(L1, getnum); - luaL_loadstring(L1, s); + size_t slen; + const char *s = luaL_checklstring(L1, getnum, &slen); + const char *name = getstring; + const char *mode = getstring; + luaL_loadbufferx(L1, s, slen, name, mode); } else if EQ("newmetatable") { lua_pushboolean(L1, luaL_newmetatable(L1, getstring)); diff --git a/lundump.c b/lundump.c index 07c42e622b..100521a9b5 100644 --- a/lundump.c +++ b/lundump.c @@ -38,6 +38,7 @@ typedef struct { Table *h; /* list for string reuse */ lu_mem offset; /* current position relative to beginning of dump */ lua_Integer nstr; /* number of strings in the list */ + lu_byte fixed; /* dump is fixed in memory */ } LoadState; @@ -70,6 +71,16 @@ static void loadAlign (LoadState *S, int align) { } +#define getaddr(S,n,t) cast(t *, getaddr_(S,n,sizeof(t))) + +static const void *getaddr_ (LoadState *S, int n, int sz) { + const void *block = luaZ_getaddr(S->Z, n * sz); + if (block == NULL) + error(S, "truncated fixed buffer"); + return block; +} + + #define loadVar(S,x) loadVector(S,&x,1) @@ -169,10 +180,16 @@ static TString *loadString (LoadState *S, Proto *p) { static void loadCode (LoadState *S, Proto *f) { int n = loadInt(S); - f->code = luaM_newvectorchecked(S->L, n, Instruction); - f->sizecode = n; loadAlign(S, sizeof(f->code[0])); - loadVector(S, f->code, n); + if (S->fixed) { + f->code = getaddr(S, n, Instruction); + f->sizecode = n; + } + else { + f->code = luaM_newvectorchecked(S->L, n, Instruction); + f->sizecode = n; + loadVector(S, f->code, n); + } } @@ -254,9 +271,15 @@ static void loadUpvalues (LoadState *S, Proto *f) { static void loadDebug (LoadState *S, Proto *f) { int i, n; n = loadInt(S); - f->lineinfo = luaM_newvectorchecked(S->L, n, ls_byte); - f->sizelineinfo = n; - loadVector(S, f->lineinfo, n); + if (S->fixed) { + f->lineinfo = getaddr(S, n, ls_byte); + f->sizelineinfo = n; + } + else { + f->lineinfo = luaM_newvectorchecked(S->L, n, ls_byte); + f->sizelineinfo = n; + loadVector(S, f->lineinfo, n); + } n = loadInt(S); f->abslineinfo = luaM_newvectorchecked(S->L, n, AbsLineInfo); f->sizeabslineinfo = n; @@ -287,7 +310,9 @@ static void loadFunction (LoadState *S, Proto *f) { f->linedefined = loadInt(S); f->lastlinedefined = loadInt(S); f->numparams = loadByte(S); - f->flag = loadByte(S) & PF_ISVARARG; /* keep only the meaningful flags */ + f->flag = loadByte(S) & PF_ISVARARG; /* get only the meaningful flags */ + if (S->fixed) + f->flag |= PF_FIXED; /* signal that code is fixed */ f->maxstacksize = loadByte(S); loadCode(S, f); loadConstants(S, f); @@ -335,7 +360,7 @@ static void checkHeader (LoadState *S) { /* ** Load precompiled chunk. */ -LClosure *luaU_undump (lua_State *L, ZIO *Z, const char *name) { +LClosure *luaU_undump (lua_State *L, ZIO *Z, const char *name, int fixed) { LoadState S; LClosure *cl; if (*name == '@' || *name == '=') @@ -346,6 +371,7 @@ LClosure *luaU_undump (lua_State *L, ZIO *Z, const char *name) { S.name = name; S.L = L; S.Z = Z; + S.fixed = fixed; S.offset = 1; /* fist byte was already read */ checkHeader(&S); cl = luaF_newLclosure(L, loadByte(&S)); diff --git a/lundump.h b/lundump.h index bfabaa636e..05ac7f856f 100644 --- a/lundump.h +++ b/lundump.h @@ -26,7 +26,8 @@ #define LUAC_FORMAT 0 /* this is the official format */ /* load one chunk; from lundump.c */ -LUAI_FUNC LClosure* luaU_undump (lua_State* L, ZIO* Z, const char* name); +LUAI_FUNC LClosure* luaU_undump (lua_State* L, ZIO* Z, const char* name, + int fixed); /* dump one chunk; from ldump.c */ LUAI_FUNC int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, diff --git a/lzio.c b/lzio.c index cd0a02d5f9..78f7ac8354 100644 --- a/lzio.c +++ b/lzio.c @@ -45,17 +45,25 @@ void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, void *data) { /* --------------------------------------------------------------- read --- */ + +static int checkbuffer (ZIO *z) { + if (z->n == 0) { /* no bytes in buffer? */ + if (luaZ_fill(z) == EOZ) /* try to read more */ + return 0; /* no more input */ + else { + z->n++; /* luaZ_fill consumed first byte; put it back */ + z->p--; + } + } + return 1; /* now buffer has something */ +} + + size_t luaZ_read (ZIO *z, void *b, size_t n) { while (n) { size_t m; - if (z->n == 0) { /* no bytes in buffer? */ - if (luaZ_fill(z) == EOZ) /* try to read more */ - return n; /* no more input; return number of missing bytes */ - else { - z->n++; /* luaZ_fill consumed first byte; put it back */ - z->p--; - } - } + if (!checkbuffer(z)) + return n; /* no more input; return number of missing bytes */ m = (n <= z->n) ? n : z->n; /* min. between n and z->n */ memcpy(b, z->p, m); z->n -= m; @@ -66,3 +74,15 @@ size_t luaZ_read (ZIO *z, void *b, size_t n) { return 0; } + +const void *luaZ_getaddr (ZIO* z, size_t n) { + const void *res; + if (!checkbuffer(z)) + return NULL; /* no more input */ + if (z->n < n) /* not enough bytes? */ + return NULL; /* block not whole; cannot give an address */ + res = z->p; /* get block address */ + z->n -= n; /* consume these bytes */ + z->p += n; + return res; +} diff --git a/lzio.h b/lzio.h index 38f397fd28..55cc74adec 100644 --- a/lzio.h +++ b/lzio.h @@ -48,6 +48,7 @@ LUAI_FUNC void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, void *data); LUAI_FUNC size_t luaZ_read (ZIO* z, void *b, size_t n); /* read next n bytes */ +LUAI_FUNC const void *luaZ_getaddr (ZIO* z, size_t n); /* --------- Private Part ------------------ */ diff --git a/manual/manual.of b/manual/manual.of index c16039b4fd..3eab69fab8 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -2730,7 +2730,8 @@ For such errors, Lua does not call the @x{message handler}. @item{@defid{LUA_ERRERR}| error while running the @x{message handler}.} -@item{@defid{LUA_ERRSYNTAX}| syntax error during precompilation.} +@item{@defid{LUA_ERRSYNTAX}| syntax error during precompilation +or format error in a binary chunk.} @item{@defid{LUA_YIELD}| the thread (coroutine) yields.} @@ -3646,6 +3647,18 @@ and loads it accordingly (see program @idx{luac}). The string @id{mode} works as in function @Lid{load}, with the addition that a @id{NULL} value is equivalent to the string @St{bt}. +Moreover, it may have a @Char{B} instead of a @Char{b}, +meaning a @emphx{fixed buffer} with the binary dump. + +A fixed buffer means that the address returned by the reader function +should contain the chunk until everything created by the chunk has +been collected. +(In general, a fixed buffer would keep the chunk +as its contents until the end of the program, +for instance with the chunk in ROM.) +Moreover, for a fixed buffer, +the reader function should return the entire chunk in the first read. +(As an example, @Lid{luaL_loadbufferx} does that.) @id{lua_load} uses the stack internally, so the reader function must always leave the stack @@ -5688,6 +5701,8 @@ This function returns the same results as @Lid{lua_load}. @id{name} is the chunk name, used for debug information and error messages. The string @id{mode} works as in the function @Lid{lua_load}. +In particular, this function supports mode @Char{B} for +fixed buffers. } diff --git a/testes/api.lua b/testes/api.lua index dece98f55d..181c1d5340 100644 --- a/testes/api.lua +++ b/testes/api.lua @@ -407,7 +407,7 @@ do concat 3]]) == "hi alo mundo") -- "argerror" without frames - assert(T.checkpanic("loadstring 4") == + assert(T.checkpanic("loadstring 4 name bt") == "bad argument #4 (string expected, got no value)") @@ -420,7 +420,7 @@ do if not _soft then local msg = T.checkpanic[[ pushstring "function f() f() end" - loadstring -1; call 0 0 + loadstring -1 name t; call 0 0 getglobal f; call 0 0 ]] assert(string.find(msg, "stack overflow")) @@ -430,7 +430,7 @@ do assert(T.checkpanic([[ pushstring "return {__close = function () Y = 'ho'; end}" newtable - loadstring -2 + loadstring -2 name t call 0 1 setmetatable -2 toclose -1 @@ -458,6 +458,8 @@ if not _soft then print'+' end + + local lim = _soft and 500 or 12000 local prog = {"checkstack " .. (lim * 2 + 100) .. "msg", "newtable"} for i = 1,lim do @@ -481,10 +483,20 @@ for i = 1,lim do assert(t[i] == i*10); t[i] = undef end assert(next(t) == nil) prog, g, t = nil +do -- shrink stack + local m1, m2 = 0, collectgarbage"count" * 1024 + while m1 ~= m2 do -- repeat until stable + collectgarbage() + m1 = m2 + m2 = collectgarbage"count" * 1024 + end +end + + -- testing errors a = T.testC([[ - loadstring 2; pcall 0 1 0; + loadstring 2 name t; pcall 0 1 0; pushvalue 3; insert -2; pcall 1 1 0; pcall 0 0 0; return 1 @@ -498,7 +510,7 @@ local function check3(p, ...) assert(#arg == 3) assert(string.find(arg[3], p)) end -check3(":1:", T.testC("loadstring 2; return *", "x=")) +check3(":1:", T.testC("loadstring 2 name t; return *", "x=")) check3("%.", T.testC("loadfile 2; return *", ".")) check3("xxxx", T.testC("loadfile 2; return *", "xxxx")) @@ -509,6 +521,35 @@ local function checkerrnopro (code, msg) assert(not stt and string.find(err, msg)) end + +do + print("testing load of binaries in fixed buffers") + local source = {} + local N = 1000 + -- create a somewhat "large" source + for i = 1, N do source[i] = "X = X + 1; " end + source = table.concat(source) + -- give chunk an explicit name to avoid using source as name + source = load(source, "name1") + -- dump without debug information + source = string.dump(source, true) + -- each "X=X+1" generates 4 opcodes with 4 bytes each + assert(#source > N * 4 * 4) + collectgarbage(); collectgarbage() + local m1 = collectgarbage"count" * 1024 + -- load dump using fixed buffer + local code = T.testC([[ + loadstring 2 name B; + return 1 + ]], source) + collectgarbage() + local m2 = collectgarbage"count" * 1024 + -- load used fewer than 300 bytes + assert(m2 > m1 and m2 - m1 < 300) + X = 0; code(); assert(X == N); X = nil +end + + if not _soft then collectgarbage("stop") -- avoid __gc with full stack checkerrnopro("pushnum 3; call 0 0", "attempt to call") From edd8589f478e784bb8d1a8e9a3bb2bb3ca51738c Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 8 Sep 2023 11:34:39 -0300 Subject: [PATCH 0840/1145] Avoid casts from unsigned long to floating-point Old Microsoft compilers do not support those casts. --- lmathlib.c | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/lmathlib.c b/lmathlib.c index d0b1e1e5d6..f140d62330 100644 --- a/lmathlib.c +++ b/lmathlib.c @@ -249,6 +249,15 @@ static int math_type (lua_State *L) { ** =================================================================== */ +/* +** This code uses lots of shifts. ANSI C does not allow shifts greater +** than or equal to the width of the type being shifted, so some shifts +** are written in convoluted ways to match that restriction. For +** preprocessor tests, it assumes a width of 32 bits, so the maximum +** shift there is 31 bits. +*/ + + /* number of binary digits in the mantissa of a float */ #define FIGS l_floatatt(MANT_DIG) @@ -271,16 +280,19 @@ static int math_type (lua_State *L) { /* 'long' has at least 64 bits */ #define Rand64 unsigned long +#define SRand64 long #elif !defined(LUA_USE_C89) && defined(LLONG_MAX) /* there is a 'long long' type (which must have at least 64 bits) */ #define Rand64 unsigned long long +#define SRand64 long long #elif ((LUA_MAXUNSIGNED >> 31) >> 31) >= 3 /* 'lua_Unsigned' has at least 64 bits */ #define Rand64 lua_Unsigned +#define SRand64 lua_Integer #endif @@ -319,23 +331,30 @@ static Rand64 nextrand (Rand64 *state) { } -/* must take care to not shift stuff by more than 63 slots */ - - /* ** Convert bits from a random integer into a float in the ** interval [0,1), getting the higher FIG bits from the ** random unsigned integer and converting that to a float. +** Some old Microsoft compilers cannot cast an unsigned long +** to a floating-point number, so we use a signed long as an +** intermediary. When lua_Number is float or double, the shift ensures +** that 'sx' is non negative; in that case, a good compiler will remove +** the correction. */ /* must throw out the extra (64 - FIGS) bits */ #define shift64_FIG (64 - FIGS) -/* to scale to [0, 1), multiply by scaleFIG = 2^(-FIGS) */ +/* 2^(-FIGS) == 2^-1 / 2^(FIGS-1) */ #define scaleFIG (l_mathop(0.5) / ((Rand64)1 << (FIGS - 1))) static lua_Number I2d (Rand64 x) { - return (lua_Number)(trim64(x) >> shift64_FIG) * scaleFIG; + SRand64 sx = (SRand64)(trim64(x) >> shift64_FIG); + lua_Number res = (lua_Number)(sx) * scaleFIG; + if (sx < 0) + res += 1.0; /* correct the two's complement if negative */ + lua_assert(0 <= res && res < 1); + return res; } /* convert a 'Rand64' to a 'lua_Unsigned' */ @@ -471,8 +490,6 @@ static lua_Number I2d (Rand64 x) { #else /* 32 < FIGS <= 64 */ -/* must take care to not shift stuff by more than 31 slots */ - /* 2^(-FIGS) = 1.0 / 2^30 / 2^3 / 2^(FIGS-33) */ #define scaleFIG \ (l_mathop(1.0) / (UONE << 30) / l_mathop(8.0) / (UONE << (FIGS - 33))) From 6baee9ef9d5657ab582c8a4b9f885ec58ed502d0 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 8 Sep 2023 16:19:21 -0300 Subject: [PATCH 0841/1145] Removed test for "corrupted binary dump" Test is too non portable. (For instance, it does not work for different number types.) --- lundump.c | 2 +- testes/calls.lua | 14 -------------- 2 files changed, 1 insertion(+), 15 deletions(-) diff --git a/lundump.c b/lundump.c index f1852c3581..e8d92a8534 100644 --- a/lundump.c +++ b/lundump.c @@ -81,7 +81,7 @@ static size_t loadUnsigned (LoadState *S, size_t limit) { static size_t loadSize (LoadState *S) { - return loadUnsigned(S, ~(size_t)0); + return loadUnsigned(S, MAX_SIZET); } diff --git a/testes/calls.lua b/testes/calls.lua index 664be1b4e7..a19385843b 100644 --- a/testes/calls.lua +++ b/testes/calls.lua @@ -342,20 +342,6 @@ do -- another bug (in 5.4.0) end -if not _port then -- another bug (since 5.2) - -- corrupted binary dump: list of upvalue names is larger than number - -- of upvalues, overflowing the array of upvalues. - local code = - "\x1b\x4c\x75\x61\x54\x00\x19\x93\x0d\x0a\x1a\x0a\x04\x08\x08\x78\x56\z - \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x28\x77\x40\x00\x86\x40\z - \x74\x65\x6d\x70\x81\x81\x01\x00\x02\x82\x48\x00\x02\x00\xc7\x00\x01\z - \x00\x80\x80\x80\x82\x00\x00\x80\x81\x82\x78\x80\x82\x81\x86\x40\x74\z - \x65\x6d\x70" - - assert(load(code)) -- segfaults in previous versions -end - - x = string.dump(load("x = 1; return x")) a = assert(load(read1(x), nil, "b")) assert(a() == 1 and _G.x == 1) From 81e4fce5303fdb274bc5572fb168dd766fb8208e Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 26 Oct 2023 16:12:25 -0300 Subject: [PATCH 0842/1145] Simpler test in 'luaH_getint' The test whether key is inside the array part of a table uses a bit trick to avoid computing the real size of the array part. --- ltable.c | 36 +++++++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/ltable.c b/ltable.c index 3fb575a1d6..3353c04793 100644 --- a/ltable.c +++ b/ltable.c @@ -252,7 +252,7 @@ LUAI_FUNC unsigned int luaH_realasize (const Table *t) { return t->alimit; /* this is the size */ else { unsigned int size = t->alimit; - /* compute the smallest power of 2 not smaller than 'n' */ + /* compute the smallest power of 2 not smaller than 'size' */ size |= (size >> 1); size |= (size >> 2); size |= (size >> 4); @@ -722,22 +722,36 @@ static void luaH_newkey (lua_State *L, Table *t, const TValue *key, /* ** Search function for integers. If integer is inside 'alimit', get it -** directly from the array part. Otherwise, if 'alimit' is not equal to -** the real size of the array, key still can be in the array part. In -** this case, try to avoid a call to 'luaH_realasize' when key is just -** one more than the limit (so that it can be incremented without -** changing the real size of the array). +** directly from the array part. Otherwise, if 'alimit' is not +** the real size of the array, the key still can be in the array part. +** In this case, do the "Xmilia trick" to check whether 'key-1' is +** smaller than the real size. +** The trick works as follow: let 'p' be an integer such that +** '2^(p+1) >= alimit > 2^p', or '2^(p+1) > alimit-1 >= 2^p'. +** That is, 2^(p+1) is the real size of the array, and 'p' is the highest +** bit on in 'alimit-1'. What we have to check becomes 'key-1 < 2^(p+1)'. +** We compute '(key-1) & ~(alimit-1)', which we call 'res'; it will +** have the 'p' bit cleared. If the key is outside the array, that is, +** 'key-1 >= 2^(p+1)', then 'res' will have some bit on higher than 'p', +** therefore it will be larger or equal to 'alimit', and the check +** will fail. If 'key-1 < 2^(p+1)', then 'res' has no bit on higher than +** 'p', and as the bit 'p' itself was cleared, 'res' will be smaller +** than 2^p, therefore smaller than 'alimit', and the check succeeds. +** As special cases, when 'alimit' is 0 the condition is trivially false, +** and when 'alimit' is 1 the condition simplifies to 'key-1 < alimit'. +** If key is 0 or negative, 'res' will have its higher bit on, so that +** if cannot be smaller than alimit. */ const TValue *luaH_getint (Table *t, lua_Integer key) { - if (l_castS2U(key) - 1u < t->alimit) /* 'key' in [1, t->alimit]? */ + lua_Unsigned alimit = t->alimit; + if (l_castS2U(key) - 1u < alimit) /* 'key' in [1, t->alimit]? */ return &t->array[key - 1]; - else if (!limitequalsasize(t) && /* key still may be in the array part? */ - (l_castS2U(key) == t->alimit + 1 || - l_castS2U(key) - 1u < luaH_realasize(t))) { + else if (!isrealasize(t) && /* key still may be in the array part? */ + (((l_castS2U(key) - 1u) & ~(alimit - 1u)) < alimit)) { t->alimit = cast_uint(key); /* probably '#t' is here now */ return &t->array[key - 1]; } - else { + else { /* key is not in the array part; check the hash */ Node *n = hashint(t, key); for (;;) { /* check whether 'key' is somewhere in the chain */ if (keyisinteger(n) && keyival(n) == key) From b8b709b6d40c5c18d9b8ef33bb50afc55f048ab8 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 27 Oct 2023 16:32:49 -0300 Subject: [PATCH 0843/1145] Avoid direct accesses to the array part of a table --- ltable.c | 128 ++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 79 insertions(+), 49 deletions(-) diff --git a/ltable.c b/ltable.c index 3f95ab0c60..d436cf6ef1 100644 --- a/ltable.c +++ b/ltable.c @@ -719,16 +719,38 @@ void luaH_newkey (lua_State *L, Table *t, const TValue *key, TValue *value) { } -static const TValue *getintfromarray (Table *t, lua_Integer key) { - if (l_castS2U(key) - 1u < t->alimit) /* 'key' in [1, t->alimit]? */ - return &t->array[key - 1]; - else if (!limitequalsasize(t) && /* key still may be in the array part? */ - (l_castS2U(key) == t->alimit + 1 || - l_castS2U(key) - 1u < luaH_realasize(t))) { +/* +** Check whether key is in the array part. If 'alimit' is not the real +** size of the array, the key still can be in the array part. In this +** case, do the "Xmilia trick" to check whether 'key-1' is smaller than +** the real size. +** The trick works as follow: let 'p' be an integer such that +** '2^(p+1) >= alimit > 2^p', or '2^(p+1) > alimit-1 >= 2^p'. +** That is, 2^(p+1) is the real size of the array, and 'p' is the highest +** bit on in 'alimit-1'. What we have to check becomes 'key-1 < 2^(p+1)'. +** We compute '(key-1) & ~(alimit-1)', which we call 'res'; it will +** have the 'p' bit cleared. If the key is outside the array, that is, +** 'key-1 >= 2^(p+1)', then 'res' will have some 1-bit higher than 'p', +** therefore it will be larger or equal to 'alimit', and the check +** will fail. If 'key-1 < 2^(p+1)', then 'res' has no 1-bit higher than +** 'p', and as the bit 'p' itself was cleared, 'res' will be smaller +** than 2^p, therefore smaller than 'alimit', and the check succeeds. +** As special cases, when 'alimit' is 0 the condition is trivially false, +** and when 'alimit' is 1 the condition simplifies to 'key-1 < alimit'. +** If key is 0 or negative, 'res' will have its higher bit on, so that +** if cannot be smaller than alimit. +*/ +static int keyinarray (Table *t, lua_Integer key) { + lua_Unsigned alimit = t->alimit; + if (l_castS2U(key) - 1u < alimit) /* 'key' in [1, t->alimit]? */ + return 1; + else if (!isrealasize(t) && /* key still may be in the array part? */ + (((l_castS2U(key) - 1u) & ~(alimit - 1u)) < alimit)) { t->alimit = cast_uint(key); /* probably '#t' is here now */ - return &t->array[key - 1]; + return 1; } - else return NULL; /* key is not in the array part */ + else + return 0; } @@ -748,20 +770,16 @@ static const TValue *getintfromhash (Table *t, lua_Integer key) { } -/* -** Search function for integers. If integer is inside 'alimit', get it -** directly from the array part. Otherwise, if 'alimit' is not equal to -** the real size of the array, key still can be in the array part. In -** this case, try to avoid a call to 'luaH_realasize' when key is just -** one more than the limit (so that it can be incremented without -** changing the real size of the array). -*/ -static const TValue *Hgetint (Table *t, lua_Integer key) { - const TValue *slot = getintfromarray(t, key); - if (slot != NULL) - return slot; - else - return getintfromhash(t, key); +l_sinline int arraykeyisempty (Table *t, lua_Integer key) { + int tag = *getArrTag(t, key); + return tagisempty(tag); +} + + +static int hashkeyisempty (Table *t, lua_Integer key) { + const TValue *val = getintfromhash(t, key); + lua_assert(!keyinarray(t, key)); + return isempty(val); } @@ -776,7 +794,17 @@ static int finishnodeget (const TValue *val, TValue *res) { int luaH_getint (Table *t, lua_Integer key, TValue *res) { - return finishnodeget(Hgetint(t, key), res); + if (keyinarray(t, key)) { + int tag = *getArrTag(t, key); + if (!tagisempty(tag)) { + arr2val(t, key, tag, res); + return HOK; /* success */ + } + else + return ~cast_int(key); /* empty slot in the array part */ + } + else + return finishnodeget(getintfromhash(t, key), res); } @@ -832,25 +860,28 @@ TString *luaH_getstrkey (Table *t, TString *key) { /* ** main search function */ -static const TValue *Hget (Table *t, const TValue *key) { +int luaH_get (Table *t, const TValue *key, TValue *res) { + const TValue *slot; switch (ttypetag(key)) { - case LUA_VSHRSTR: return luaH_Hgetshortstr(t, tsvalue(key)); - case LUA_VNUMINT: return Hgetint(t, ivalue(key)); - case LUA_VNIL: return &absentkey; + case LUA_VSHRSTR: + slot = luaH_Hgetshortstr(t, tsvalue(key)); + break; + case LUA_VNUMINT: + return luaH_getint(t, ivalue(key), res); + case LUA_VNIL: + slot = &absentkey; + break; case LUA_VNUMFLT: { lua_Integer k; if (luaV_flttointeger(fltvalue(key), &k, F2Ieq)) /* integral index? */ - return Hgetint(t, k); /* use specialized version */ + return luaH_getint(t, k, res); /* use specialized version */ /* else... */ } /* FALLTHROUGH */ default: - return getgeneric(t, key, 0); + slot = getgeneric(t, key, 0); + break; } -} - - -int luaH_get (Table *t, const TValue *key, TValue *res) { - return finishnodeget(Hget(t, key), res); + return finishnodeget(slot, res); } @@ -866,10 +897,10 @@ static int finishnodeset (Table *t, const TValue *slot, TValue *val) { int luaH_psetint (Table *t, lua_Integer key, TValue *val) { - const TValue *slot = getintfromarray(t, key); - if (slot != NULL) { - if (!ttisnil(slot)) { - setobj(((lua_State*)NULL), cast(TValue*, slot), val); + if (keyinarray(t, key)) { + lu_byte *tag = getArrTag(t, key); + if (!tagisempty(*tag)) { + val2arr(t, key, tag, val); return HOK; /* success */ } else @@ -973,27 +1004,26 @@ static lua_Unsigned hash_search (Table *t, lua_Unsigned j) { j *= 2; else { j = LUA_MAXINTEGER; - if (isempty(Hgetint(t, j))) /* t[j] not present? */ + if (hashkeyisempty(t, j)) /* t[j] not present? */ break; /* 'j' now is an absent index */ else /* weird case */ return j; /* well, max integer is a boundary... */ } - } while (!isempty(Hgetint(t, j))); /* repeat until an absent t[j] */ + } while (!hashkeyisempty(t, j)); /* repeat until an absent t[j] */ /* i < j && t[i] present && t[j] absent */ while (j - i > 1u) { /* do a binary search between them */ lua_Unsigned m = (i + j) / 2; - if (isempty(Hgetint(t, m))) j = m; + if (hashkeyisempty(t, m)) j = m; else i = m; } return i; } -static unsigned int binsearch (const TValue *array, unsigned int i, - unsigned int j) { +static unsigned int binsearch (Table *array, unsigned int i, unsigned int j) { while (j - i > 1u) { /* binary search */ unsigned int m = (i + j) / 2; - if (isempty(&array[m - 1])) j = m; + if (arraykeyisempty(array, m)) j = m; else i = m; } return i; @@ -1034,9 +1064,9 @@ static unsigned int binsearch (const TValue *array, unsigned int i, */ lua_Unsigned luaH_getn (Table *t) { unsigned int limit = t->alimit; - if (limit > 0 && isempty(&t->array[limit - 1])) { /* (1)? */ + if (limit > 0 && arraykeyisempty(t, limit)) { /* (1)? */ /* there must be a boundary before 'limit' */ - if (limit >= 2 && !isempty(&t->array[limit - 2])) { + if (limit >= 2 && !arraykeyisempty(t, limit - 1)) { /* 'limit - 1' is a boundary; can it be a new limit? */ if (ispow2realasize(t) && !ispow2(limit - 1)) { t->alimit = limit - 1; @@ -1045,7 +1075,7 @@ lua_Unsigned luaH_getn (Table *t) { return limit - 1; } else { /* must search for a boundary in [0, limit] */ - unsigned int boundary = binsearch(t->array, 0, limit); + unsigned int boundary = binsearch(t, 0, limit); /* can this boundary represent the real size of the array? */ if (ispow2realasize(t) && boundary > luaH_realasize(t) / 2) { t->alimit = boundary; /* use it as the new limit */ @@ -1064,7 +1094,7 @@ lua_Unsigned luaH_getn (Table *t) { if (isempty(&t->array[limit - 1])) { /* empty? */ /* there must be a boundary in the array after old limit, and it must be a valid new limit */ - unsigned int boundary = binsearch(t->array, t->alimit, limit); + unsigned int boundary = binsearch(t, t->alimit, limit); t->alimit = boundary; return boundary; } @@ -1073,7 +1103,7 @@ lua_Unsigned luaH_getn (Table *t) { /* (3) 'limit' is the last element and either is zero or present in table */ lua_assert(limit == luaH_realasize(t) && (limit == 0 || !isempty(&t->array[limit - 1]))); - if (isdummy(t) || isempty(Hgetint(t, cast(lua_Integer, limit + 1)))) + if (isdummy(t) || hashkeyisempty(t, cast(lua_Integer, limit + 1))) return limit; /* 'limit + 1' is absent */ else /* 'limit + 1' is also present */ return hash_search(t, limit); From 43c8e5bded052801f54a7439d18933b83570eb82 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 30 Oct 2023 14:25:59 -0300 Subject: [PATCH 0844/1145] Full abstraction for representation of array values --- lapi.c | 31 +++++++-------- lgc.c | 25 ++++++++---- lobject.h | 5 ++- lstate.c | 7 +++- ltable.c | 116 ++++++++++++++++++++++++++++-------------------------- ltable.h | 21 ++++++++-- ltests.c | 10 +++-- lvm.c | 2 +- lvm.h | 4 +- 9 files changed, 128 insertions(+), 93 deletions(-) diff --git a/lapi.c b/lapi.c index 4a3ba72438..99c8473e13 100644 --- a/lapi.c +++ b/lapi.c @@ -653,21 +653,17 @@ l_sinline int auxgetstr (lua_State *L, const TValue *t, const char *k) { } -/* -** Get the global table in the registry. Since all predefined -** indices in the registry were inserted right when the registry -** was created and never removed, they must always be in the array -** part of the registry. -*/ -#define getGtable(L) \ - (&hvalue(&G(L)->l_registry)->array[LUA_RIDX_GLOBALS - 1]) +static void getGlobalTable (lua_State *L, TValue *gt) { + Table *registry = hvalue(&G(L)->l_registry); + luaH_getint(registry, LUA_RIDX_GLOBALS, gt); +} LUA_API int lua_getglobal (lua_State *L, const char *name) { - const TValue *G; + TValue gt; lua_lock(L); - G = getGtable(L); - return auxgetstr(L, G, name); + getGlobalTable(L, >); + return auxgetstr(L, >, name); } @@ -840,10 +836,10 @@ static void auxsetstr (lua_State *L, const TValue *t, const char *k) { LUA_API void lua_setglobal (lua_State *L, const char *name) { - const TValue *G; + TValue gt; lua_lock(L); /* unlock done in 'auxsetstr' */ - G = getGtable(L); - auxsetstr(L, G, name); + getGlobalTable(L, >); + auxsetstr(L, >, name); } @@ -1093,10 +1089,11 @@ LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data, LClosure *f = clLvalue(s2v(L->top.p - 1)); /* get new function */ if (f->nupvalues >= 1) { /* does it have an upvalue? */ /* get global table from registry */ - const TValue *gt = getGtable(L); + TValue gt; + getGlobalTable(L, >); /* set global table as 1st upvalue of 'f' (may be LUA_ENV) */ - setobj(L, f->upvals[0]->v.p, gt); - luaC_barrier(L, f->upvals[0], gt); + setobj(L, f->upvals[0]->v.p, >); + luaC_barrier(L, f->upvals[0], >); } } lua_unlock(L); diff --git a/lgc.c b/lgc.c index a3094ff571..813b08d593 100644 --- a/lgc.c +++ b/lgc.c @@ -91,6 +91,13 @@ #define gcvalueN(o) (iscollectable(o) ? gcvalue(o) : NULL) +/* +** Access to collectable objects in array part of tables +*/ +#define gcvalarr(t,i) \ + ((*getArrTag(t,i) & BIT_ISCOLLECTABLE) ? getArrVal(t,i)->gc : NULL) + + #define markvalue(g,o) { checkliveness(g->mainthread,o); \ if (valiswhite(o)) reallymarkobject(g,gcvalue(o)); } @@ -486,9 +493,10 @@ static int traverseephemeron (global_State *g, Table *h, int inv) { unsigned int nsize = sizenode(h); /* traverse array part */ for (i = 0; i < asize; i++) { - if (valiswhite(&h->array[i])) { + GCObject *o = gcvalarr(h, i + 1); + if (o != NULL && iswhite(o)) { marked = 1; - reallymarkobject(g, gcvalue(&h->array[i])); + reallymarkobject(g, o); } } /* traverse hash part; if 'inv', traverse descending @@ -524,8 +532,11 @@ static void traversestrongtable (global_State *g, Table *h) { Node *n, *limit = gnodelast(h); unsigned int i; unsigned int asize = luaH_realasize(h); - for (i = 0; i < asize; i++) /* traverse array part */ - markvalue(g, &h->array[i]); + for (i = 0; i < asize; i++) { /* traverse array part */ + GCObject *o = gcvalarr(h, i + 1); + if (o != NULL && iswhite(o)) + reallymarkobject(g, o); + } for (n = gnode(h, 0); n < limit; n++) { /* traverse hash part */ if (isempty(gval(n))) /* entry is empty? */ clearkey(n); /* clear its key */ @@ -746,9 +757,9 @@ static void clearbyvalues (global_State *g, GCObject *l, GCObject *f) { unsigned int i; unsigned int asize = luaH_realasize(h); for (i = 0; i < asize; i++) { - TValue *o = &h->array[i]; - if (iscleared(g, gcvalueN(o))) /* value was collected? */ - setempty(o); /* remove entry */ + GCObject *o = gcvalarr(h, i + 1); + if (iscleared(g, o)) /* value was collected? */ + *getArrTag(h, i + 1) = LUA_VEMPTY; /* remove entry */ } for (n = gnode(h, 0); n < limit; n++) { if (iscleared(g, gcvalueN(gval(n)))) /* unmarked value? */ diff --git a/lobject.h b/lobject.h index 556608e4aa..e91bb0f850 100644 --- a/lobject.h +++ b/lobject.h @@ -736,12 +736,15 @@ typedef union Node { #define setnorealasize(t) ((t)->flags |= BITRAS) +typedef struct ArrayCell ArrayCell; + + typedef struct Table { CommonHeader; lu_byte flags; /* 1<

l_registry, registry); luaH_resize(L, registry, LUA_RIDX_LAST, 0); /* registry[LUA_RIDX_MAINTHREAD] = L */ - setthvalue(L, ®istry->array[LUA_RIDX_MAINTHREAD - 1], L); + setthvalue(L, &aux, L); + luaH_setint(L, registry, LUA_RIDX_MAINTHREAD, &aux); /* registry[LUA_RIDX_GLOBALS] = new table (table of globals) */ - sethvalue(L, ®istry->array[LUA_RIDX_GLOBALS - 1], luaH_new(L)); + sethvalue(L, &aux, luaH_new(L)); + luaH_setint(L, registry, LUA_RIDX_GLOBALS, &aux); } diff --git a/ltable.c b/ltable.c index d436cf6ef1..ddda9a44d6 100644 --- a/ltable.c +++ b/ltable.c @@ -350,9 +350,10 @@ int luaH_next (lua_State *L, Table *t, StkId key) { unsigned int asize = luaH_realasize(t); unsigned int i = findindex(L, t, s2v(key), asize); /* find original key */ for (; i < asize; i++) { /* try first array part */ - if (!isempty(&t->array[i])) { /* a non-empty entry? */ + int tag = *getArrTag(t, i + 1); + if (!tagisempty(tag)) { /* a non-empty entry? */ setivalue(s2v(key), i + 1); - setobj2s(L, key + 1, &t->array[i]); + farr2val(t, i + 1, tag, s2v(key + 1)); return 1; } } @@ -374,6 +375,41 @@ static void freehash (lua_State *L, Table *t) { } +/* +** Check whether an integer key is in the array part. If 'alimit' is +** not the real size of the array, the key still can be in the array +** part. In this case, do the "Xmilia trick" to check whether 'key-1' +** is smaller than the real size. +** The trick works as follow: let 'p' be an integer such that +** '2^(p+1) >= alimit > 2^p', or '2^(p+1) > alimit-1 >= 2^p'. +** That is, 2^(p+1) is the real size of the array, and 'p' is the highest +** bit on in 'alimit-1'. What we have to check becomes 'key-1 < 2^(p+1)'. +** We compute '(key-1) & ~(alimit-1)', which we call 'res'; it will +** have the 'p' bit cleared. If the key is outside the array, that is, +** 'key-1 >= 2^(p+1)', then 'res' will have some 1-bit higher than 'p', +** therefore it will be larger or equal to 'alimit', and the check +** will fail. If 'key-1 < 2^(p+1)', then 'res' has no 1-bit higher than +** 'p', and as the bit 'p' itself was cleared, 'res' will be smaller +** than 2^p, therefore smaller than 'alimit', and the check succeeds. +** As special cases, when 'alimit' is 0 the condition is trivially false, +** and when 'alimit' is 1 the condition simplifies to 'key-1 < alimit'. +** If key is 0 or negative, 'res' will have its higher bit on, so that +** if cannot be smaller than alimit. +*/ +static int keyinarray (Table *t, lua_Integer key) { + lua_Unsigned alimit = t->alimit; + if (l_castS2U(key) - 1u < alimit) /* 'key' in [1, t->alimit]? */ + return 1; + else if (!isrealasize(t) && /* key still may be in the array part? */ + (((l_castS2U(key) - 1u) & ~(alimit - 1u)) < alimit)) { + t->alimit = cast_uint(key); /* probably '#t' is here now */ + return 1; + } + else + return 0; +} + + /* ** {============================================================= ** Rehash @@ -421,6 +457,12 @@ static int countint (lua_Integer key, unsigned int *nums) { } +l_sinline int arraykeyisempty (const Table *t, lua_Integer key) { + int tag = *getArrTag(t, key); + return tagisempty(tag); +} + + /* ** Count keys in array part of table 't': Fill 'nums[i]' with ** number of keys that will go into corresponding slice and return @@ -443,7 +485,7 @@ static unsigned int numusearray (const Table *t, unsigned int *nums) { } /* count elements in range (2^(lg - 1), 2^lg] */ for (; i <= lim; i++) { - if (!isempty(&t->array[i-1])) + if (!arraykeyisempty(t, i)) lc++; } nums[lg] += lc; @@ -555,7 +597,7 @@ void luaH_resize (lua_State *L, Table *t, unsigned int newasize, unsigned int i; Table newt; /* to keep the new hash part */ unsigned int oldasize = setlimittosize(t); - TValue *newarray; + ArrayCell *newarray; /* create new hash part with appropriate size into 'newt' */ setnodevector(L, &newt, nhsize); if (newasize < oldasize) { /* will array shrink? */ @@ -563,14 +605,18 @@ void luaH_resize (lua_State *L, Table *t, unsigned int newasize, exchangehashpart(t, &newt); /* and new hash */ /* re-insert into the new hash the elements from vanishing slice */ for (i = newasize; i < oldasize; i++) { - if (!isempty(&t->array[i])) - luaH_setint(L, t, i + 1, &t->array[i]); + int tag = *getArrTag(t, i + 1); + if (!tagisempty(tag)) { /* a non-empty entry? */ + TValue aux; + farr2val(t, i + 1, tag, &aux); + luaH_setint(L, t, i + 1, &aux); + } } t->alimit = oldasize; /* restore current size... */ exchangehashpart(t, &newt); /* and hash (in case of errors) */ } /* allocate new array */ - newarray = luaM_reallocvector(L, t->array, oldasize, newasize, TValue); + newarray = luaM_reallocvector(L, t->array, oldasize, newasize, ArrayCell); if (l_unlikely(newarray == NULL && newasize > 0)) { /* allocation failed? */ freehash(L, &newt); /* release new hash part */ luaM_error(L); /* raise error (with array unchanged) */ @@ -580,7 +626,7 @@ void luaH_resize (lua_State *L, Table *t, unsigned int newasize, t->array = newarray; /* set new array part */ t->alimit = newasize; for (i = oldasize; i < newasize; i++) /* clear new slice of the array */ - setempty(&t->array[i]); + *getArrTag(t, i + 1) = LUA_VEMPTY; /* re-insert elements from old hash part into new parts */ reinsert(L, &newt, t); /* 'newt' now has the old hash */ freehash(L, &newt); /* free old hash part */ @@ -719,41 +765,6 @@ void luaH_newkey (lua_State *L, Table *t, const TValue *key, TValue *value) { } -/* -** Check whether key is in the array part. If 'alimit' is not the real -** size of the array, the key still can be in the array part. In this -** case, do the "Xmilia trick" to check whether 'key-1' is smaller than -** the real size. -** The trick works as follow: let 'p' be an integer such that -** '2^(p+1) >= alimit > 2^p', or '2^(p+1) > alimit-1 >= 2^p'. -** That is, 2^(p+1) is the real size of the array, and 'p' is the highest -** bit on in 'alimit-1'. What we have to check becomes 'key-1 < 2^(p+1)'. -** We compute '(key-1) & ~(alimit-1)', which we call 'res'; it will -** have the 'p' bit cleared. If the key is outside the array, that is, -** 'key-1 >= 2^(p+1)', then 'res' will have some 1-bit higher than 'p', -** therefore it will be larger or equal to 'alimit', and the check -** will fail. If 'key-1 < 2^(p+1)', then 'res' has no 1-bit higher than -** 'p', and as the bit 'p' itself was cleared, 'res' will be smaller -** than 2^p, therefore smaller than 'alimit', and the check succeeds. -** As special cases, when 'alimit' is 0 the condition is trivially false, -** and when 'alimit' is 1 the condition simplifies to 'key-1 < alimit'. -** If key is 0 or negative, 'res' will have its higher bit on, so that -** if cannot be smaller than alimit. -*/ -static int keyinarray (Table *t, lua_Integer key) { - lua_Unsigned alimit = t->alimit; - if (l_castS2U(key) - 1u < alimit) /* 'key' in [1, t->alimit]? */ - return 1; - else if (!isrealasize(t) && /* key still may be in the array part? */ - (((l_castS2U(key) - 1u) & ~(alimit - 1u)) < alimit)) { - t->alimit = cast_uint(key); /* probably '#t' is here now */ - return 1; - } - else - return 0; -} - - static const TValue *getintfromhash (Table *t, lua_Integer key) { Node *n = hashint(t, key); lua_assert(l_castS2U(key) - 1u >= luaH_realasize(t)); @@ -770,15 +781,8 @@ static const TValue *getintfromhash (Table *t, lua_Integer key) { } -l_sinline int arraykeyisempty (Table *t, lua_Integer key) { - int tag = *getArrTag(t, key); - return tagisempty(tag); -} - - static int hashkeyisempty (Table *t, lua_Integer key) { const TValue *val = getintfromhash(t, key); - lua_assert(!keyinarray(t, key)); return isempty(val); } @@ -797,7 +801,7 @@ int luaH_getint (Table *t, lua_Integer key, TValue *res) { if (keyinarray(t, key)) { int tag = *getArrTag(t, key); if (!tagisempty(tag)) { - arr2val(t, key, tag, res); + farr2val(t, key, tag, res); return HOK; /* success */ } else @@ -900,7 +904,7 @@ int luaH_psetint (Table *t, lua_Integer key, TValue *val) { if (keyinarray(t, key)) { lu_byte *tag = getArrTag(t, key); if (!tagisempty(*tag)) { - val2arr(t, key, tag, val); + fval2arr(t, key, tag, val); return HOK; /* success */ } else @@ -956,7 +960,7 @@ void luaH_finishset (lua_State *L, Table *t, const TValue *key, } else { /* array entry */ hres = ~hres; /* real index */ - val2arr(t, hres, getArrTag(t, hres), value); + fval2arr(t, hres, getArrTag(t, hres), value); } } @@ -1087,11 +1091,11 @@ lua_Unsigned luaH_getn (Table *t) { /* 'limit' is zero or present in table */ if (!limitequalsasize(t)) { /* (2)? */ /* 'limit' > 0 and array has more elements after 'limit' */ - if (isempty(&t->array[limit])) /* 'limit + 1' is empty? */ + if (arraykeyisempty(t, limit + 1)) /* 'limit + 1' is empty? */ return limit; /* this is the boundary */ /* else, try last element in the array */ limit = luaH_realasize(t); - if (isempty(&t->array[limit - 1])) { /* empty? */ + if (arraykeyisempty(t, limit)) { /* empty? */ /* there must be a boundary in the array after old limit, and it must be a valid new limit */ unsigned int boundary = binsearch(t, t->alimit, limit); @@ -1102,7 +1106,7 @@ lua_Unsigned luaH_getn (Table *t) { } /* (3) 'limit' is the last element and either is zero or present in table */ lua_assert(limit == luaH_realasize(t) && - (limit == 0 || !isempty(&t->array[limit - 1]))); + (limit == 0 || !arraykeyisempty(t, limit))); if (isdummy(t) || hashkeyisempty(t, cast(lua_Integer, limit + 1))) return limit; /* 'limit + 1' is absent */ else /* 'limit + 1' is also present */ diff --git a/ltable.h b/ltable.h index 5ec7b447c0..371e721daf 100644 --- a/ltable.h +++ b/ltable.h @@ -51,20 +51,33 @@ */ +struct ArrayCell { + lu_byte tt; + Value value; +}; + /* fast access to components of array values */ -#define getArrTag(t,k) (&(t)->array[k - 1].tt_) -#define getArrVal(t,k) (&(t)->array[k - 1].value_) +#define getArrTag(t,k) (&(t)->array[k - 1].tt) +#define getArrVal(t,k) (&(t)->array[k - 1].value) #define tagisempty(tag) (novariant(tag) == LUA_TNIL) -#define arr2val(h,k,tag,res) \ + +#define farr2val(h,k,tag,res) \ ((res)->tt_ = tag, (res)->value_ = *getArrVal(h,k)) -#define val2arr(h,k,tag,val) \ +#define fval2arr(h,k,tag,val) \ (*tag = (val)->tt_, *getArrVal(h,k) = (val)->value_) +#define obj2arr(h,k,val) \ + (*getArrTag(h,k) = (val)->tt_, *getArrVal(h,k) = (val)->value_) + +#define arr2obj(h,k,val) \ + ((val)->tt_ = *getArrTag(h,k), (val)->value_ = *getArrVal(h,k)) + + LUAI_FUNC int luaH_getshortstr (Table *t, TString *key, TValue *res); LUAI_FUNC int luaH_getstr (Table *t, TString *key, TValue *res); LUAI_FUNC int luaH_get (Table *t, const TValue *key, TValue *res); diff --git a/ltests.c b/ltests.c index 7d184c0d8e..e2bf76bd71 100644 --- a/ltests.c +++ b/ltests.c @@ -362,8 +362,11 @@ static void checktable (global_State *g, Table *h) { Node *n, *limit = gnode(h, sizenode(h)); GCObject *hgc = obj2gco(h); checkobjrefN(g, hgc, h->metatable); - for (i = 0; i < asize; i++) - checkvalref(g, hgc, &h->array[i]); + for (i = 0; i < asize; i++) { + TValue aux; + arr2obj(h, i + 1, &aux); + checkvalref(g, hgc, &aux); + } for (n = gnode(h, 0); n < limit; n++) { if (!isempty(gval(n))) { TValue k; @@ -1005,7 +1008,8 @@ static int table_query (lua_State *L) { } else if ((unsigned int)i < asize) { lua_pushinteger(L, i); - pushobject(L, &t->array[i]); + arr2obj(t, i + 1, s2v(L->top.p)); + api_incr_top(L); lua_pushnil(L); } else if ((i -= asize) < sizenode(t)) { diff --git a/lvm.c b/lvm.c index 96413718a7..927272df39 100644 --- a/lvm.c +++ b/lvm.c @@ -1845,7 +1845,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { luaH_resizearray(L, h, last); /* preallocate it at once */ for (; n > 0; n--) { TValue *val = s2v(ra + n); - setobj2t(L, &h->array[last - 1], val); + obj2arr(h, last, val); last--; luaC_barrierback(L, obj2gco(h), val); } diff --git a/lvm.h b/lvm.h index 8808c94255..32455fba5f 100644 --- a/lvm.h +++ b/lvm.h @@ -92,7 +92,7 @@ typedef enum { if ((u - 1u < h->alimit)) { \ int tag = *getArrTag(h,u); \ if (tagisempty(tag)) aux = HNOTFOUND; \ - else { arr2val(h, u, tag, res); aux = HOK; }} \ + else { farr2val(h, u, tag, res); aux = HOK; }} \ else { aux = luaH_getint(h, u, res); }} @@ -105,7 +105,7 @@ typedef enum { if ((u - 1u < h->alimit)) { \ lu_byte *tag = getArrTag(h,u); \ if (tagisempty(*tag)) aux = ~cast_int(u); \ - else { val2arr(h, u, tag, val); aux = HOK; }} \ + else { fval2arr(h, u, tag, val); aux = HOK; }} \ else { aux = luaH_psetint(h, u, val); }} From 7923dbbf72da303ca1cca17efd24725668992f15 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 1 Nov 2023 12:00:54 -0300 Subject: [PATCH 0845/1145] Bug: Recursion in 'getobjname' can stack overflow 'getobjname' now broken in two, a basic version that handles locals, upvalues, and constants, and a full version, which uses the basic version to handle table accesses (globals and fields). --- ldebug.c | 153 +++++++++++++++++++++++++--------------------- testes/errors.lua | 3 + 2 files changed, 87 insertions(+), 69 deletions(-) diff --git a/ldebug.c b/ldebug.c index 690ac38f6f..b1f16ac9fb 100644 --- a/ldebug.c +++ b/ldebug.c @@ -417,40 +417,6 @@ LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) { ** ======================================================= */ -static const char *getobjname (const Proto *p, int lastpc, int reg, - const char **name); - - -/* -** Find a "name" for the constant 'c'. -*/ -static void kname (const Proto *p, int c, const char **name) { - TValue *kvalue = &p->k[c]; - *name = (ttisstring(kvalue)) ? getstr(tsvalue(kvalue)) : "?"; -} - - -/* -** Find a "name" for the register 'c'. -*/ -static void rname (const Proto *p, int pc, int c, const char **name) { - const char *what = getobjname(p, pc, c, name); /* search for 'c' */ - if (!(what && *what == 'c')) /* did not find a constant name? */ - *name = "?"; -} - - -/* -** Find a "name" for a 'C' value in an RK instruction. -*/ -static void rkname (const Proto *p, int pc, Instruction i, const char **name) { - int c = GETARG_C(i); /* key index */ - if (GETARG_k(i)) /* is 'c' a constant? */ - kname(p, c, name); - else /* 'c' is a register */ - rname(p, pc, c, name); -} - static int filterpc (int pc, int jmptarget) { if (pc < jmptarget) /* is code conditional (inside a jump)? */ @@ -509,28 +475,29 @@ static int findsetreg (const Proto *p, int lastpc, int reg) { /* -** Check whether table being indexed by instruction 'i' is the -** environment '_ENV' +** Find a "name" for the constant 'c'. */ -static const char *gxf (const Proto *p, int pc, Instruction i, int isup) { - int t = GETARG_B(i); /* table index */ - const char *name; /* name of indexed variable */ - if (isup) /* is an upvalue? */ - name = upvalname(p, t); - else - getobjname(p, pc, t, &name); - return (name && strcmp(name, LUA_ENV) == 0) ? "global" : "field"; +static const char *kname (const Proto *p, int index, const char **name) { + TValue *kvalue = &p->k[index]; + if (ttisstring(kvalue)) { + *name = getstr(tsvalue(kvalue)); + return "constant"; + } + else { + *name = "?"; + return NULL; + } } -static const char *getobjname (const Proto *p, int lastpc, int reg, - const char **name) { - int pc; - *name = luaF_getlocalname(p, reg + 1, lastpc); +static const char *basicgetobjname (const Proto *p, int *ppc, int reg, + const char **name) { + int pc = *ppc; + *name = luaF_getlocalname(p, reg + 1, pc); if (*name) /* is a local? */ return "local"; /* else try symbolic execution */ - pc = findsetreg(p, lastpc, reg); + *ppc = pc = findsetreg(p, pc, reg); if (pc != -1) { /* could find instruction? */ Instruction i = p->code[pc]; OpCode op = GET_OPCODE(i); @@ -538,18 +505,80 @@ static const char *getobjname (const Proto *p, int lastpc, int reg, case OP_MOVE: { int b = GETARG_B(i); /* move from 'b' to 'a' */ if (b < GETARG_A(i)) - return getobjname(p, pc, b, name); /* get name for 'b' */ + return basicgetobjname(p, ppc, b, name); /* get name for 'b' */ break; } + case OP_GETUPVAL: { + *name = upvalname(p, GETARG_B(i)); + return "upvalue"; + } + case OP_LOADK: return kname(p, GETARG_Bx(i), name); + case OP_LOADKX: return kname(p, GETARG_Ax(p->code[pc + 1]), name); + default: break; + } + } + return NULL; /* could not find reasonable name */ +} + + +/* +** Find a "name" for the register 'c'. +*/ +static void rname (const Proto *p, int pc, int c, const char **name) { + const char *what = basicgetobjname(p, &pc, c, name); /* search for 'c' */ + if (!(what && *what == 'c')) /* did not find a constant name? */ + *name = "?"; +} + + +/* +** Find a "name" for a 'C' value in an RK instruction. +*/ +static void rkname (const Proto *p, int pc, Instruction i, const char **name) { + int c = GETARG_C(i); /* key index */ + if (GETARG_k(i)) /* is 'c' a constant? */ + kname(p, c, name); + else /* 'c' is a register */ + rname(p, pc, c, name); +} + + +/* +** Check whether table being indexed by instruction 'i' is the +** environment '_ENV' +*/ +static const char *isEnv (const Proto *p, int pc, Instruction i, int isup) { + int t = GETARG_B(i); /* table index */ + const char *name; /* name of indexed variable */ + if (isup) /* is 't' an upvalue? */ + name = upvalname(p, t); + else /* 't' is a register */ + basicgetobjname(p, &pc, t, &name); + return (name && strcmp(name, LUA_ENV) == 0) ? "global" : "field"; +} + + +/* +** Extend 'basicgetobjname' to handle table accesses +*/ +static const char *getobjname (const Proto *p, int lastpc, int reg, + const char **name) { + const char *kind = basicgetobjname(p, &lastpc, reg, name); + if (kind != NULL) + return kind; + else if (lastpc != -1) { /* could find instruction? */ + Instruction i = p->code[lastpc]; + OpCode op = GET_OPCODE(i); + switch (op) { case OP_GETTABUP: { int k = GETARG_C(i); /* key index */ kname(p, k, name); - return gxf(p, pc, i, 1); + return isEnv(p, lastpc, i, 1); } case OP_GETTABLE: { int k = GETARG_C(i); /* key index */ - rname(p, pc, k, name); - return gxf(p, pc, i, 0); + rname(p, lastpc, k, name); + return isEnv(p, lastpc, i, 0); } case OP_GETI: { *name = "integer index"; @@ -558,24 +587,10 @@ static const char *getobjname (const Proto *p, int lastpc, int reg, case OP_GETFIELD: { int k = GETARG_C(i); /* key index */ kname(p, k, name); - return gxf(p, pc, i, 0); - } - case OP_GETUPVAL: { - *name = upvalname(p, GETARG_B(i)); - return "upvalue"; - } - case OP_LOADK: - case OP_LOADKX: { - int b = (op == OP_LOADK) ? GETARG_Bx(i) - : GETARG_Ax(p->code[pc + 1]); - if (ttisstring(&p->k[b])) { - *name = getstr(tsvalue(&p->k[b])); - return "constant"; - } - break; + return isEnv(p, lastpc, i, 0); } case OP_SELF: { - rkname(p, pc, i, name); + rkname(p, lastpc, i, name); return "method"; } default: break; /* go through to return NULL */ diff --git a/testes/errors.lua b/testes/errors.lua index b777a3298a..01cfe9060c 100644 --- a/testes/errors.lua +++ b/testes/errors.lua @@ -121,6 +121,9 @@ assert(not string.find(doit"aaa={13}; local bbbb=1; aaa[bbbb](3)", "'bbbb'")) checkmessage("aaa={13}; local bbbb=1; aaa[bbbb](3)", "number") checkmessage("aaa=(1)..{}", "a table value") +-- bug in 5.4.6 +checkmessage("a = {_ENV = {}}; print(a._ENV.x + 1)", "field 'x'") + _G.aaa, _G.bbbb = nil -- calls From 08a077d673b25cf1fbfe21794f240f4ff4999667 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 3 Nov 2023 15:26:13 -0300 Subject: [PATCH 0846/1145] Full implementation of new representation for arrays --- lgc.c | 8 +++---- lobject.h | 4 +++- ltable.c | 46 ++++++++++++++++++++++++++++++++-------- ltable.h | 63 +++++++++++++++++++++++++++++++++++++++++++------------ lvm.h | 4 ++-- 5 files changed, 96 insertions(+), 29 deletions(-) diff --git a/lgc.c b/lgc.c index 813b08d593..0f42328285 100644 --- a/lgc.c +++ b/lgc.c @@ -493,7 +493,7 @@ static int traverseephemeron (global_State *g, Table *h, int inv) { unsigned int nsize = sizenode(h); /* traverse array part */ for (i = 0; i < asize; i++) { - GCObject *o = gcvalarr(h, i + 1); + GCObject *o = gcvalarr(h, i); if (o != NULL && iswhite(o)) { marked = 1; reallymarkobject(g, o); @@ -533,7 +533,7 @@ static void traversestrongtable (global_State *g, Table *h) { unsigned int i; unsigned int asize = luaH_realasize(h); for (i = 0; i < asize; i++) { /* traverse array part */ - GCObject *o = gcvalarr(h, i + 1); + GCObject *o = gcvalarr(h, i); if (o != NULL && iswhite(o)) reallymarkobject(g, o); } @@ -757,9 +757,9 @@ static void clearbyvalues (global_State *g, GCObject *l, GCObject *f) { unsigned int i; unsigned int asize = luaH_realasize(h); for (i = 0; i < asize; i++) { - GCObject *o = gcvalarr(h, i + 1); + GCObject *o = gcvalarr(h, i); if (iscleared(g, o)) /* value was collected? */ - *getArrTag(h, i + 1) = LUA_VEMPTY; /* remove entry */ + *getArrTag(h, i) = LUA_VEMPTY; /* remove entry */ } for (n = gnode(h, 0); n < limit; n++) { if (iscleared(g, gcvalueN(gval(n)))) /* unmarked value? */ diff --git a/lobject.h b/lobject.h index e91bb0f850..25e268bef4 100644 --- a/lobject.h +++ b/lobject.h @@ -192,6 +192,8 @@ typedef union { /* macro to test for (any kind of) nil */ #define ttisnil(v) checktype((v), LUA_TNIL) +#define tagisempty(tag) (novariant(tag) == LUA_TNIL) + /* macro to test for a standard nil */ #define ttisstrictnil(o) checktag((o), LUA_VNIL) @@ -736,7 +738,7 @@ typedef union Node { #define setnorealasize(t) ((t)->flags |= BITRAS) -typedef struct ArrayCell ArrayCell; +typedef union ArrayCell ArrayCell; typedef struct Table { diff --git a/ltable.c b/ltable.c index ddda9a44d6..b7362a36ab 100644 --- a/ltable.c +++ b/ltable.c @@ -350,7 +350,7 @@ int luaH_next (lua_State *L, Table *t, StkId key) { unsigned int asize = luaH_realasize(t); unsigned int i = findindex(L, t, s2v(key), asize); /* find original key */ for (; i < asize; i++) { /* try first array part */ - int tag = *getArrTag(t, i + 1); + int tag = *getArrTag(t, i); if (!tagisempty(tag)) { /* a non-empty entry? */ setivalue(s2v(key), i + 1); farr2val(t, i + 1, tag, s2v(key + 1)); @@ -458,7 +458,7 @@ static int countint (lua_Integer key, unsigned int *nums) { l_sinline int arraykeyisempty (const Table *t, lua_Integer key) { - int tag = *getArrTag(t, key); + int tag = *getArrTag(t, key - 1); return tagisempty(tag); } @@ -512,6 +512,33 @@ static int numusehash (const Table *t, unsigned int *nums, unsigned int *pna) { } +/* +** Convert an "abstract size" (number of values in an array) to +** "concrete size" (number of cell elements in the array). Cells +** do not need to be full; we only must make sure it has the values +** needed and its 'tag' element. So, we compute the concrete tag index +** and the concrete value index of the last element, get their maximum +** and adds 1. +*/ +static unsigned int concretesize (unsigned int size) { + if (size == 0) return 0; + else { + unsigned int ts = TagIndex(size - 1); + unsigned int vs = ValueIndex(size - 1); + return ((ts >= vs) ? ts : vs) + 1; + } +} + + +static ArrayCell *resizearray (lua_State *L , Table *t, + unsigned int oldasize, + unsigned int newasize) { + oldasize = concretesize(oldasize); + newasize = concretesize(newasize); + return luaM_reallocvector(L, t->array, oldasize, newasize, ArrayCell); +} + + /* ** Creates an array for the hash part of a table with the given ** size, or reuses the dummy node if size is zero. @@ -605,7 +632,7 @@ void luaH_resize (lua_State *L, Table *t, unsigned int newasize, exchangehashpart(t, &newt); /* and new hash */ /* re-insert into the new hash the elements from vanishing slice */ for (i = newasize; i < oldasize; i++) { - int tag = *getArrTag(t, i + 1); + int tag = *getArrTag(t, i); if (!tagisempty(tag)) { /* a non-empty entry? */ TValue aux; farr2val(t, i + 1, tag, &aux); @@ -616,7 +643,7 @@ void luaH_resize (lua_State *L, Table *t, unsigned int newasize, exchangehashpart(t, &newt); /* and hash (in case of errors) */ } /* allocate new array */ - newarray = luaM_reallocvector(L, t->array, oldasize, newasize, ArrayCell); + newarray = resizearray(L, t, oldasize, newasize); if (l_unlikely(newarray == NULL && newasize > 0)) { /* allocation failed? */ freehash(L, &newt); /* release new hash part */ luaM_error(L); /* raise error (with array unchanged) */ @@ -626,7 +653,7 @@ void luaH_resize (lua_State *L, Table *t, unsigned int newasize, t->array = newarray; /* set new array part */ t->alimit = newasize; for (i = oldasize; i < newasize; i++) /* clear new slice of the array */ - *getArrTag(t, i + 1) = LUA_VEMPTY; + *getArrTag(t, i) = LUA_VEMPTY; /* re-insert elements from old hash part into new parts */ reinsert(L, &newt, t); /* 'newt' now has the old hash */ freehash(L, &newt); /* free old hash part */ @@ -682,8 +709,9 @@ Table *luaH_new (lua_State *L) { void luaH_free (lua_State *L, Table *t) { + unsigned ps = concretesize(luaH_realasize(t)); freehash(L, t); - luaM_freearray(L, t->array, luaH_realasize(t)); + luaM_freearray(L, t->array, ps); luaM_free(L, t); } @@ -799,7 +827,7 @@ static int finishnodeget (const TValue *val, TValue *res) { int luaH_getint (Table *t, lua_Integer key, TValue *res) { if (keyinarray(t, key)) { - int tag = *getArrTag(t, key); + int tag = *getArrTag(t, key - 1); if (!tagisempty(tag)) { farr2val(t, key, tag, res); return HOK; /* success */ @@ -902,7 +930,7 @@ static int finishnodeset (Table *t, const TValue *slot, TValue *val) { int luaH_psetint (Table *t, lua_Integer key, TValue *val) { if (keyinarray(t, key)) { - lu_byte *tag = getArrTag(t, key); + lu_byte *tag = getArrTag(t, key - 1); if (!tagisempty(*tag)) { fval2arr(t, key, tag, val); return HOK; /* success */ @@ -960,7 +988,7 @@ void luaH_finishset (lua_State *L, Table *t, const TValue *key, } else { /* array entry */ hres = ~hres; /* real index */ - fval2arr(t, hres, getArrTag(t, hres), value); + obj2arr(t, hres, value); } } diff --git a/ltable.h b/ltable.h index 371e721daf..b401aba1dd 100644 --- a/ltable.h +++ b/ltable.h @@ -51,31 +51,68 @@ */ -struct ArrayCell { - lu_byte tt; +/* +** The array part of a table is represented by an array of cells. +** Each cell is composed of (NM + 1) elements, and each element has the +** type 'ArrayCell'. In each cell, only one element has the variant +** 'tag', while the other NM elements have the variant 'value'. The +** array in the 'tag' element holds the tags of the other elements in +** that cell. +*/ +#define NM ((unsigned int)sizeof(Value)) + +union ArrayCell { + unsigned char tag[NM]; Value value; }; -/* fast access to components of array values */ -#define getArrTag(t,k) (&(t)->array[k - 1].tt) -#define getArrVal(t,k) (&(t)->array[k - 1].value) +/* +** 'NMTag' defines which cell element has the tags; that could be any +** value between 0 (tags come before all values) and NM (tags come after +** all values). +*/ +#define NMTag 0 -#define tagisempty(tag) (novariant(tag) == LUA_TNIL) +/* +** Computes the concrete index that holds the tag of abstract index 'i' +*/ +#define TagIndex(i) (((i)/NM * (NM + 1u)) + NMTag) -#define farr2val(h,k,tag,res) \ - ((res)->tt_ = tag, (res)->value_ = *getArrVal(h,k)) +/* +** Computes the concrete index that holds the value of abstract index 'i' +*/ +#define ValueIndex(i) ((i) + (((i) + (NM - NMTag))/NM)) -#define fval2arr(h,k,tag,val) \ - (*tag = (val)->tt_, *getArrVal(h,k) = (val)->value_) +/* Computes the address of the tag for the abstract index 'k' */ +#define getArrTag(t,k) (&(t)->array[TagIndex(k)].tag[(k)%NM]) -#define obj2arr(h,k,val) \ - (*getArrTag(h,k) = (val)->tt_, *getArrVal(h,k) = (val)->value_) +/* Computes the address of the value for the abstract index 'k' */ +#define getArrVal(t,k) (&(t)->array[ValueIndex(k)].value) + +/* +** Move TValues to/from arrays, using Lua indices +*/ #define arr2obj(h,k,val) \ - ((val)->tt_ = *getArrTag(h,k), (val)->value_ = *getArrVal(h,k)) + ((val)->tt_ = *getArrTag(h,(k)-1u), (val)->value_ = *getArrVal(h,(k)-1u)) + +#define obj2arr(h,k,val) \ + (*getArrTag(h,(k)-1u) = (val)->tt_, *getArrVal(h,(k)-1u) = (val)->value_) + + +/* +** Often, we need to check the tag of a value before moving it. These +** macros also move TValues to/from arrays, but receive the precomputed +** tag value or address as an extra argument. +*/ +#define farr2val(h,k,tag,res) \ + ((res)->tt_ = tag, (res)->value_ = *getArrVal(h,(k)-1u)) + +#define fval2arr(h,k,tag,val) \ + (*tag = (val)->tt_, *getArrVal(h,(k)-1u) = (val)->value_) LUAI_FUNC int luaH_getshortstr (Table *t, TString *key, TValue *res); diff --git a/lvm.h b/lvm.h index 32455fba5f..c74c81f843 100644 --- a/lvm.h +++ b/lvm.h @@ -90,7 +90,7 @@ typedef enum { if (!ttistable(t)) aux = HNOTATABLE; \ else { Table *h = hvalue(t); lua_Unsigned u = l_castS2U(k); \ if ((u - 1u < h->alimit)) { \ - int tag = *getArrTag(h,u); \ + int tag = *getArrTag(h,(u)-1u); \ if (tagisempty(tag)) aux = HNOTFOUND; \ else { farr2val(h, u, tag, res); aux = HOK; }} \ else { aux = luaH_getint(h, u, res); }} @@ -103,7 +103,7 @@ typedef enum { if (!ttistable(t)) aux = HNOTATABLE; \ else { Table *h = hvalue(t); lua_Unsigned u = l_castS2U(k); \ if ((u - 1u < h->alimit)) { \ - lu_byte *tag = getArrTag(h,u); \ + lu_byte *tag = getArrTag(h,(u)-1u); \ if (tagisempty(*tag)) aux = ~cast_int(u); \ else { fval2arr(h, u, tag, val); aux = HOK; }} \ else { aux = luaH_psetint(h, u, val); }} From 19afd916870a0621b59e8728d439b0fe10288b99 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 8 Nov 2023 10:02:06 -0300 Subject: [PATCH 0847/1145] Solving merge issue with use of tables in dump/undump The use of tables in dump/undump to reuse strings did not exist in the version that changed the representation of arrays, so it was not corrected for the new API for tables. --- ldump.c | 8 ++++---- lundump.c | 5 +++-- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/ldump.c b/ldump.c index 01169c12bd..c6f2c4e168 100644 --- a/ldump.c +++ b/ldump.c @@ -116,10 +116,10 @@ static void dumpString (DumpState *D, TString *ts) { if (ts == NULL) dumpSize(D, 0); else { - const TValue *idx = luaH_getstr(D->h, ts); - if (ttisinteger(idx)) { /* string already saved? */ + TValue idx; + if (luaH_getstr(D->h, ts, &idx) == HOK) { /* string already saved? */ dumpSize(D, 1); /* reuse a saved string */ - dumpInt(D, ivalue(idx)); /* index of saved string */ + dumpInt(D, ivalue(&idx)); /* index of saved string */ } else { /* must write and save the string */ TValue key, value; /* to save the string in the hash */ @@ -130,7 +130,7 @@ static void dumpString (DumpState *D, TString *ts) { D->nstr++; /* one more saved string */ setsvalue(D->L, &key, ts); /* the string is the key */ setivalue(&value, D->nstr); /* its index is the value */ - luaH_finishset(D->L, D->h, &key, idx, &value); /* h[ts] = nstr */ + luaH_set(D->L, D->h, &key, &value); /* h[ts] = nstr */ /* integer value does not need barrier */ } } diff --git a/lundump.c b/lundump.c index 45708f968e..5b4cd2ea20 100644 --- a/lundump.c +++ b/lundump.c @@ -143,8 +143,9 @@ static TString *loadStringN (LoadState *S, Proto *p) { return NULL; else if (size == 1) { /* previously saved string? */ int idx = loadInt(S); /* get its index */ - const TValue *stv = luaH_getint(S->h, idx); - return tsvalue(stv); + TValue stv; + luaH_getint(S->h, idx, &stv); + return tsvalue(&stv); } else if (size -= 2, size <= LUAI_MAXSHORTLEN) { /* short string? */ char buff[LUAI_MAXSHORTLEN]; From b8a9d14032b717f6e5c493a9ec20e3494c9f82a0 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 8 Nov 2023 10:41:24 -0300 Subject: [PATCH 0848/1145] Details Comments and parameter name in header file. --- ltable.h | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/ltable.h b/ltable.h index 210ae7acdf..d488a1f7c3 100644 --- a/ltable.h +++ b/ltable.h @@ -52,12 +52,18 @@ #define HFIRSTNODE 3 /* -** Besides these values, pset (pre-set) operations may also return an -** encoding of where the value should go (usually called 'hres'). That -** means that there is a slot with that key but with no value. (pset -** cannot set that value because there might be a metamethod.) If the -** slot is in the hash part, the encoding is (HFIRSTNODE + hash index); -** if the slot is in the array part, the encoding is (~array index). +** 'luaH_get*' operations set 'res' and return HOK, unless the value is +** absent. In that case, they set nothing and return HNOTFOUND. +** The 'luaH_pset*' (pre-set) operations set the given value and return +** HOK, unless the original value is absent. In that case, if the key +** is really absent, they return HNOTFOUND. Otherwise, if there is a +** slot with that key but with no value, 'luaH_pset*' return an encoding +** of where the key is (usually called 'hres'). (pset cannot set that +** value because there might be a metamethod.) If the slot is in the +** hash part, the encoding is (HFIRSTNODE + hash index); if the slot is +** in the array part, the encoding is (~array index), a negative value. +** The value HNOTATABLE is used by the fast macros to signal that the +** value being indexed is not a table. */ @@ -125,11 +131,14 @@ union ArrayCell { (*tag = (val)->tt_, *getArrVal(h,(k)-1u) = (val)->value_) +LUAI_FUNC int luaH_get (Table *t, const TValue *key, TValue *res); LUAI_FUNC int luaH_getshortstr (Table *t, TString *key, TValue *res); LUAI_FUNC int luaH_getstr (Table *t, TString *key, TValue *res); -LUAI_FUNC int luaH_get (Table *t, const TValue *key, TValue *res); LUAI_FUNC int luaH_getint (Table *t, lua_Integer key, TValue *res); +/* Special get for metamethods */ +LUAI_FUNC const TValue *luaH_Hgetshortstr (Table *t, TString *key); + LUAI_FUNC TString *luaH_getstrkey (Table *t, TString *key); LUAI_FUNC int luaH_psetint (Table *t, lua_Integer key, TValue *val); @@ -139,11 +148,11 @@ LUAI_FUNC int luaH_pset (Table *t, const TValue *key, TValue *val); LUAI_FUNC void luaH_setint (lua_State *L, Table *t, lua_Integer key, TValue *value); -LUAI_FUNC const TValue *luaH_Hgetshortstr (Table *t, TString *key); LUAI_FUNC void luaH_set (lua_State *L, Table *t, const TValue *key, TValue *value); + LUAI_FUNC void luaH_finishset (lua_State *L, Table *t, const TValue *key, - TValue *value, int aux); + TValue *value, int hres); LUAI_FUNC Table *luaH_new (lua_State *L); LUAI_FUNC void luaH_resize (lua_State *L, Table *t, unsigned int nasize, unsigned int nhsize); From 7f4906f565ab9f8b1125107a3abae3d759f3ecf2 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 8 Nov 2023 13:24:38 -0300 Subject: [PATCH 0849/1145] Towards external strings Long strings have a pointer to string contents. --- lgc.c | 4 ++-- lobject.h | 23 +++++++++++++---------- lstring.c | 19 +++++++++++-------- lstring.h | 12 ++++++++++-- 4 files changed, 36 insertions(+), 22 deletions(-) diff --git a/lgc.c b/lgc.c index e4f8e396ae..e3fcaa3e9d 100644 --- a/lgc.c +++ b/lgc.c @@ -808,12 +808,12 @@ static void freeobj (lua_State *L, GCObject *o) { case LUA_VSHRSTR: { TString *ts = gco2ts(o); luaS_remove(L, ts); /* remove it from hash table */ - luaM_freemem(L, ts, sizelstring(ts->shrlen)); + luaM_freemem(L, ts, sizestrshr(ts->shrlen)); break; } case LUA_VLNGSTR: { TString *ts = gco2ts(o); - luaM_freemem(L, ts, sizelstring(ts->u.lnglen)); + luaM_freemem(L, ts, sizestrlng(ts->u.lnglen)); break; } default: lua_assert(0); diff --git a/lobject.h b/lobject.h index c6c4364731..f76d26a689 100644 --- a/lobject.h +++ b/lobject.h @@ -388,35 +388,38 @@ typedef struct GCObject { typedef struct TString { CommonHeader; lu_byte extra; /* reserved words for short strings; "has hash" for longs */ - lu_byte shrlen; /* length for short strings, 0xFF for long strings */ + ls_byte shrlen; /* length for short strings, negative for long strings */ unsigned int hash; union { size_t lnglen; /* length for long strings */ struct TString *hnext; /* linked list for hash table */ } u; - char contents[1]; /* string body starts here */ + char *contents; /* pointer to content in long strings */ } TString; +#define strisshr(ts) ((ts)->shrlen >= 0) + /* ** Get the actual string (array of bytes) from a 'TString'. (Generic ** version and specialized versions for long and short strings.) */ -#define getlngstr(ts) check_exp((ts)->shrlen == 0xFF, (ts)->contents) -#define getshrstr(ts) check_exp((ts)->shrlen != 0xFF, (ts)->contents) -#define getstr(ts) ((ts)->contents) +#define rawgetshrstr(ts) (cast_charp(&(ts)->contents)) +#define getshrstr(ts) check_exp(strisshr(ts), rawgetshrstr(ts)) +#define getlngstr(ts) check_exp(!strisshr(ts), (ts)->contents) +#define getstr(ts) (strisshr(ts) ? rawgetshrstr(ts) : (ts)->contents) -/* get string length from 'TString *s' */ -#define tsslen(s) \ - ((s)->shrlen != 0xFF ? (s)->shrlen : (s)->u.lnglen) +/* get string length from 'TString *ts' */ +#define tsslen(ts) \ + (strisshr(ts) ? cast_uint((ts)->shrlen) : (ts)->u.lnglen) /* ** Get string and length */ #define getlstr(ts, len) \ - ((ts)->shrlen != 0xFF \ - ? (cast_void(len = (ts)->shrlen), (ts)->contents) \ + (strisshr(ts) \ + ? (cast_void(len = (ts)->shrlen), rawgetshrstr(ts)) \ : (cast_void(len = (ts)->u.lnglen), (ts)->contents)) /* }================================================================== */ diff --git a/lstring.c b/lstring.c index e921dd0f34..c4b3c7ba2b 100644 --- a/lstring.c +++ b/lstring.c @@ -140,24 +140,25 @@ void luaS_init (lua_State *L) { /* ** creates a new string object */ -static TString *createstrobj (lua_State *L, size_t l, int tag, unsigned int h) { +static TString *createstrobj (lua_State *L, size_t totalsize, int tag, + unsigned int h) { TString *ts; GCObject *o; - size_t totalsize; /* total size of TString object */ - totalsize = sizelstring(l); o = luaC_newobj(L, tag, totalsize); ts = gco2ts(o); ts->hash = h; ts->extra = 0; - getstr(ts)[l] = '\0'; /* ending 0 */ return ts; } TString *luaS_createlngstrobj (lua_State *L, size_t l) { - TString *ts = createstrobj(L, l, LUA_VLNGSTR, G(L)->seed); + size_t totalsize = sizestrlng(l); + TString *ts = createstrobj(L, totalsize, LUA_VLNGSTR, G(L)->seed); ts->u.lnglen = l; - ts->shrlen = 0xFF; /* signals that it is a long string */ + ts->shrlen = -1; /* signals that it is a long string */ + ts->contents = cast_charp(ts) + sizeof(TString); + ts->contents[l] = '\0'; /* ending 0 */ return ts; } @@ -194,7 +195,8 @@ static TString *internshrstr (lua_State *L, const char *str, size_t l) { TString **list = &tb->hash[lmod(h, tb->size)]; lua_assert(str != NULL); /* otherwise 'memcmp'/'memcpy' are undefined */ for (ts = *list; ts != NULL; ts = ts->u.hnext) { - if (l == ts->shrlen && (memcmp(str, getshrstr(ts), l * sizeof(char)) == 0)) { + if (l == cast_uint(ts->shrlen) && + (memcmp(str, getshrstr(ts), l * sizeof(char)) == 0)) { /* found! */ if (isdead(g, ts)) /* dead (but not collected yet)? */ changewhite(ts); /* resurrect it */ @@ -206,8 +208,9 @@ static TString *internshrstr (lua_State *L, const char *str, size_t l) { growstrtab(L, tb); list = &tb->hash[lmod(h, tb->size)]; /* rehash with new size */ } - ts = createstrobj(L, l, LUA_VSHRSTR, h); + ts = createstrobj(L, sizestrshr(l), LUA_VSHRSTR, h); ts->shrlen = cast_byte(l); + getshrstr(ts)[l] = '\0'; /* ending 0 */ memcpy(getshrstr(ts), str, l * sizeof(char)); ts->u.hnext = *list; *list = ts; diff --git a/lstring.h b/lstring.h index 450c2390d1..069e64b7fb 100644 --- a/lstring.h +++ b/lstring.h @@ -20,10 +20,18 @@ /* -** Size of a TString: Size of the header plus space for the string +** Size of a short TString: Size of the header plus space for the string ** itself (including final '\0'). */ -#define sizelstring(l) (offsetof(TString, contents) + ((l) + 1) * sizeof(char)) +#define sizestrshr(l) \ + (offsetof(TString, contents) + ((l) + 1) * sizeof(char)) + +/* +** Size of a long TString: Size of the header plus space for the string +** itself (including final '\0'). +*/ +#define sizestrlng(l) (sizeof(TString) + ((l) + 1) * sizeof(char)) + #define luaS_newliteral(L, s) (luaS_newlstr(L, "" s, \ (sizeof(s)/sizeof(char))-1)) From 024f9064f1b43758eb36aba52547edc0312bf4ba Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 9 Nov 2023 17:05:42 -0300 Subject: [PATCH 0850/1145] External strings Strings can use external buffers to store their contents. --- lapi.c | 14 +++++++++ lgc.c | 4 ++- lobject.h | 8 +++++ lstring.c | 78 ++++++++++++++++++++++++++++++++++++++++++++-- lstring.h | 10 ++---- ltests.c | 33 ++++++++++++++++++++ lua.h | 2 ++ manual/manual.of | 34 ++++++++++++++++++++ testes/strings.lua | 26 ++++++++++++++-- 9 files changed, 195 insertions(+), 14 deletions(-) diff --git a/lapi.c b/lapi.c index 34b335fd69..2aaa65059a 100644 --- a/lapi.c +++ b/lapi.c @@ -535,6 +535,20 @@ LUA_API const char *lua_pushlstring (lua_State *L, const char *s, size_t len) { } +LUA_API const char *lua_pushextlstring (lua_State *L, + const char *s, size_t len, lua_Alloc falloc, void *ud) { + TString *ts; + lua_lock(L); + api_check(L, s[len] == '\0', "string not ending with zero"); + ts = luaS_newextlstr (L, s, len, falloc, ud); + setsvalue2s(L, L->top.p, ts); + api_incr_top(L); + luaC_checkGC(L); + lua_unlock(L); + return getstr(ts); +} + + LUA_API const char *lua_pushstring (lua_State *L, const char *s) { lua_lock(L); if (s == NULL) diff --git a/lgc.c b/lgc.c index e3fcaa3e9d..3884aad038 100644 --- a/lgc.c +++ b/lgc.c @@ -813,7 +813,9 @@ static void freeobj (lua_State *L, GCObject *o) { } case LUA_VLNGSTR: { TString *ts = gco2ts(o); - luaM_freemem(L, ts, sizestrlng(ts->u.lnglen)); + if (ts->shrlen == LSTRMEM) /* must free external string? */ + (*ts->falloc)(ts->ud, ts->contents, ts->u.lnglen + 1, 0); + luaM_freemem(L, ts, luaS_sizelngstr(ts->u.lnglen, ts->shrlen)); break; } default: lua_assert(0); diff --git a/lobject.h b/lobject.h index f76d26a689..8688a84266 100644 --- a/lobject.h +++ b/lobject.h @@ -382,6 +382,12 @@ typedef struct GCObject { #define setsvalue2n setsvalue +/* Kinds of long strings (stored in 'shrlen') */ +#define LSTRREG -1 /* regular long string */ +#define LSTRFIX -2 /* fixed external long string */ +#define LSTRMEM -3 /* external long string with deallocation */ + + /* ** Header for a string value. */ @@ -395,6 +401,8 @@ typedef struct TString { struct TString *hnext; /* linked list for hash table */ } u; char *contents; /* pointer to content in long strings */ + lua_Alloc falloc; /* deallocation function for external strings */ + void *ud; /* user data for external strings */ } TString; diff --git a/lstring.c b/lstring.c index c4b3c7ba2b..8701b70557 100644 --- a/lstring.c +++ b/lstring.c @@ -136,6 +136,20 @@ void luaS_init (lua_State *L) { } +size_t luaS_sizelngstr (size_t len, int kind) { + switch (kind) { + case LSTRREG: /* regular long string */ + /* don't need 'falloc'/'ud', but need space for content */ + return offsetof(TString, falloc) + (len + 1) * sizeof(char); + case LSTRFIX: /* fixed external long string */ + /* don't need 'falloc'/'ud' */ + return offsetof(TString, falloc); + default: /* external long string with deallocation */ + lua_assert(kind == LSTRMEM); + return sizeof(TString); + } +} + /* ** creates a new string object @@ -153,11 +167,11 @@ static TString *createstrobj (lua_State *L, size_t totalsize, int tag, TString *luaS_createlngstrobj (lua_State *L, size_t l) { - size_t totalsize = sizestrlng(l); + size_t totalsize = luaS_sizelngstr(l, LSTRREG); TString *ts = createstrobj(L, totalsize, LUA_VLNGSTR, G(L)->seed); ts->u.lnglen = l; - ts->shrlen = -1; /* signals that it is a long string */ - ts->contents = cast_charp(ts) + sizeof(TString); + ts->shrlen = LSTRREG; /* signals that it is a regular long string */ + ts->contents = cast_charp(ts) + offsetof(TString, falloc); ts->contents[l] = '\0'; /* ending 0 */ return ts; } @@ -275,3 +289,61 @@ Udata *luaS_newudata (lua_State *L, size_t s, int nuvalue) { return u; } + +struct NewExt { + int kind; + const char *s; + size_t len; + TString *ts; /* output */ +}; + + +static void f_newext (lua_State *L, void *ud) { + struct NewExt *ne = cast(struct NewExt *, ud); + size_t size = luaS_sizelngstr(0, ne->kind); + ne->ts = createstrobj(L, size, LUA_VLNGSTR, G(L)->seed); +} + + +static void f_pintern (lua_State *L, void *ud) { + struct NewExt *ne = cast(struct NewExt *, ud); + ne->ts = internshrstr(L, ne->s, ne->len); +} + + +TString *luaS_newextlstr (lua_State *L, + const char *s, size_t len, lua_Alloc falloc, void *ud) { + struct NewExt ne; + if (len <= LUAI_MAXSHORTLEN) { /* short string? */ + ne.s = s; ne.len = len; + if (!falloc) + f_pintern(L, &ne); /* just internalize string */ + else { + int status = luaD_rawrunprotected(L, f_pintern, &ne); + (*falloc)(ud, cast_voidp(s), len + 1, 0); /* free external string */ + if (status != LUA_OK) /* memory error? */ + luaM_error(L); /* re-raise memory error */ + } + return ne.ts; + } + /* "normal" case: long strings */ + if (!falloc) { + ne.kind = LSTRFIX; + f_newext(L, &ne); /* just create header */ + } + else { + ne.kind = LSTRMEM; + if (luaD_rawrunprotected(L, f_newext, &ne) != LUA_OK) { /* mem. error? */ + (*falloc)(ud, cast_voidp(s), len + 1, 0); /* free external string */ + luaM_error(L); /* re-raise memory error */ + } + ne.ts->falloc = falloc; + ne.ts->ud = ud; + } + ne.ts->shrlen = ne.kind; + ne.ts->u.lnglen = len; + ne.ts->contents = cast_charp(s); + return ne.ts; +} + + diff --git a/lstring.h b/lstring.h index 069e64b7fb..e321bd4312 100644 --- a/lstring.h +++ b/lstring.h @@ -26,12 +26,6 @@ #define sizestrshr(l) \ (offsetof(TString, contents) + ((l) + 1) * sizeof(char)) -/* -** Size of a long TString: Size of the header plus space for the string -** itself (including final '\0'). -*/ -#define sizestrlng(l) (sizeof(TString) + ((l) + 1) * sizeof(char)) - #define luaS_newliteral(L, s) (luaS_newlstr(L, "" s, \ (sizeof(s)/sizeof(char))-1)) @@ -60,6 +54,8 @@ LUAI_FUNC Udata *luaS_newudata (lua_State *L, size_t s, int nuvalue); LUAI_FUNC TString *luaS_newlstr (lua_State *L, const char *str, size_t l); LUAI_FUNC TString *luaS_new (lua_State *L, const char *str); LUAI_FUNC TString *luaS_createlngstrobj (lua_State *L, size_t l); - +LUAI_FUNC TString *luaS_newextlstr (lua_State *L, + const char *s, size_t len, lua_Alloc falloc, void *ud); +LUAI_FUNC size_t luaS_sizelngstr (size_t len, int kind); #endif diff --git a/ltests.c b/ltests.c index 6f556dc99e..94bd4e33cb 100644 --- a/ltests.c +++ b/ltests.c @@ -1277,6 +1277,37 @@ static int checkpanic (lua_State *L) { } +static int externKstr (lua_State *L) { + size_t len; + const char *s = luaL_checklstring(L, 1, &len); + lua_pushextlstring(L, s, len, NULL, NULL); + return 1; +} + + +/* +** Create a buffer with the content of a given string and then +** create an external string using that buffer. Use the allocation +** function from Lua to create and free the buffer. +*/ +static int externstr (lua_State *L) { + size_t len; + const char *s = luaL_checklstring(L, 1, &len); + void *ud; + lua_Alloc allocf = lua_getallocf(L, &ud); /* get allocation function */ + /* create the buffer */ + char *buff = cast_charp((*allocf)(ud, NULL, 0, len + 1)); + if (buff == NULL) { /* memory error? */ + lua_pushliteral(L, "not enough memory"); + lua_error(L); /* raise a memory error */ + } + /* copy string content to buffer, including ending 0 */ + memcpy(buff, s, (len + 1) * sizeof(char)); + /* create external string */ + lua_pushextlstring(L, buff, len, allocf, ud); + return 1; +} + /* ** {==================================================================== @@ -1949,6 +1980,8 @@ static const struct luaL_Reg tests_funcs[] = { {"udataval", udataval}, {"unref", unref}, {"upvalue", upvalue}, + {"externKstr", externKstr}, + {"externstr", externstr}, {NULL, NULL} }; diff --git a/lua.h b/lua.h index 699b7ca7e3..ca8d06fe24 100644 --- a/lua.h +++ b/lua.h @@ -244,6 +244,8 @@ LUA_API void (lua_pushnil) (lua_State *L); LUA_API void (lua_pushnumber) (lua_State *L, lua_Number n); LUA_API void (lua_pushinteger) (lua_State *L, lua_Integer n); LUA_API const char *(lua_pushlstring) (lua_State *L, const char *s, size_t len); +LUA_API const char *(lua_pushextlstring) (lua_State *L, + const char *s, size_t len, lua_Alloc falloc, void *ud); LUA_API const char *(lua_pushstring) (lua_State *L, const char *s); LUA_API const char *(lua_pushvfstring) (lua_State *L, const char *fmt, va_list argp); diff --git a/manual/manual.of b/manual/manual.of index 3eab69fab8..9d6a7fd98c 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -3908,6 +3908,40 @@ This function is equivalent to @Lid{lua_pushcclosure} with no upvalues. } +@APIEntry{const char *(lua_pushextlstring) (lua_State *L, + const char *s, size_t len, lua_Alloc falloc, void *ud);| +@apii{0,1,m} + +Creates an @emphx{external string}, +that is, a string that uses memory not managed by Lua. +The pointer @id{s} points to the exernal buffer +holding the string content, +and @id{len} is the length of the string. +The string should have a zero at its end, +that is, the condition @T{s[len] == '\0'} should hold. + +If @id{falloc} is different from @id{NULL}, +that function will be called by Lua +when the external buffer is no longer needed. +The contents of the buffer should not change before this call. +The function will be called with the given @id{ud}, +the string @id{s} as the block, +the length plus one (to account for the ending zero) as the old size, +and 0 as the new size. + +Lua always @x{internalizes} strings with lengths up to 40 characters. +So, for strings in that range, +this function will immediately internalize the string +and call @id{falloc} to free the buffer. + +Even when using an external buffer, +Lua still has to allocate a header for the string. +In case of a memory-allocation error, +Lua will call @id{falloc} before raising the error. + +} + + @APIEntry{const char *lua_pushfstring (lua_State *L, const char *fmt, ...);| @apii{0,1,v} diff --git a/testes/strings.lua b/testes/strings.lua index 90983edd18..c124b3697d 100644 --- a/testes/strings.lua +++ b/testes/strings.lua @@ -157,6 +157,12 @@ else -- compatible coercion assert(tostring(-1203 + 0.0) == "-1203") end + +local function topointer (s) + return string.format("%p", s) +end + + do -- tests for '%p' format -- not much to test, as C does not specify what '%p' does. -- ("The value of the pointer is converted to a sequence of printing @@ -180,18 +186,18 @@ do -- tests for '%p' format do local t1 = {}; local t2 = {} - assert(string.format("%p", t1) ~= string.format("%p", t2)) + assert(topointer(t1) ~= topointer(t2)) end do -- short strings are internalized local s1 = string.rep("a", 10) local s2 = string.rep("aa", 5) - assert(string.format("%p", s1) == string.format("%p", s2)) + assert(topointer(s1) == topointer(s2)) end do -- long strings aren't internalized local s1 = string.rep("a", 300); local s2 = string.rep("a", 300) - assert(string.format("%p", s1) ~= string.format("%p", s2)) + assert(topointer(s1) ~= topointer(s2)) end end @@ -521,6 +527,20 @@ else testpfs("P", str, {}) end +if T == nil then + (Message or print)('\n >>> testC not active: skipping external strings tests <<<\n') +else + print("testing external strings") + local x = T.externKstr("hello") -- external fixed short string + assert(x == "hello") + local x = T.externstr("hello") -- external allocated short string + assert(x == "hello") + x = string.rep("a", 100) -- long string + local y = T.externKstr(x) -- external fixed long string + assert(y == x) + local z = T.externstr(x) -- external allocated long string + assert(z == y) +end print('OK') From 3b57e37e4821ddce4756428956b7e9f4969efa4c Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 10 Nov 2023 12:35:48 -0300 Subject: [PATCH 0851/1145] Fixed buffers save long strings as external. --- ldump.c | 2 +- lundump.c | 23 +++++++++++++++-------- manual/manual.of | 6 ++++-- testes/api.lua | 14 +++++++++----- 4 files changed, 29 insertions(+), 16 deletions(-) diff --git a/ldump.c b/ldump.c index c6f2c4e168..090d6aab5d 100644 --- a/ldump.c +++ b/ldump.c @@ -126,7 +126,7 @@ static void dumpString (DumpState *D, TString *ts) { size_t size; const char *s = getlstr(ts, size); dumpSize(D, size + 2); - dumpVector(D, s, size); + dumpVector(D, s, size + 1); /* include ending '\0' */ D->nstr++; /* one more saved string */ setsvalue(D->L, &key, ts); /* the string is the key */ setivalue(&value, D->nstr); /* its index is the value */ diff --git a/lundump.c b/lundump.c index 5b4cd2ea20..09752d9910 100644 --- a/lundump.c +++ b/lundump.c @@ -147,17 +147,24 @@ static TString *loadStringN (LoadState *S, Proto *p) { luaH_getint(S->h, idx, &stv); return tsvalue(&stv); } - else if (size -= 2, size <= LUAI_MAXSHORTLEN) { /* short string? */ - char buff[LUAI_MAXSHORTLEN]; - loadVector(S, buff, size); /* load string into buffer */ + else if ((size -= 2) <= LUAI_MAXSHORTLEN) { /* short string? */ + char buff[LUAI_MAXSHORTLEN + 1]; /* extra space for '\0' */ + loadVector(S, buff, size + 1); /* load string into buffer */ ts = luaS_newlstr(L, buff, size); /* create string */ } else { /* long string */ - ts = luaS_createlngstrobj(L, size); /* create string */ - setsvalue2s(L, L->top.p, ts); /* anchor it ('loadVector' can GC) */ - luaD_inctop(L); - loadVector(S, getlngstr(ts), size); /* load directly in final place */ - L->top.p--; /* pop string */ + if (S->fixed) { /* for a fixed buffer, use a fixed string */ + const char *s = getaddr(S, size + 1, char); /* get content address */ + ts = luaS_newextlstr(L, s, size, NULL, NULL); + } + else { /* create internal copy */ + ts = luaS_createlngstrobj(L, size); /* create string */ + setsvalue2s(L, L->top.p, ts); /* anchor it ('loadVector' can GC) */ + luaD_inctop(L); + loadVector(S, getlngstr(ts), size); /* load directly in final place */ + loadByte(S); /* skip ending '\0' */ + L->top.p--; /* pop string */ + } } luaC_objbarrier(L, p, ts); S->nstr++; /* add string to list of saved strings */ diff --git a/manual/manual.of b/manual/manual.of index 9d6a7fd98c..9ca28aee47 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -3651,8 +3651,10 @@ Moreover, it may have a @Char{B} instead of a @Char{b}, meaning a @emphx{fixed buffer} with the binary dump. A fixed buffer means that the address returned by the reader function -should contain the chunk until everything created by the chunk has -been collected. +will contain the chunk until everything created by the chunk has +been collected; +therefore, Lua can avoid copying to internal structures +some parts of the chunk. (In general, a fixed buffer would keep the chunk as its contents until the end of the program, for instance with the chunk in ROM.) diff --git a/testes/api.lua b/testes/api.lua index 181c1d5340..7d64cb22d0 100644 --- a/testes/api.lua +++ b/testes/api.lua @@ -528,13 +528,15 @@ do local N = 1000 -- create a somewhat "large" source for i = 1, N do source[i] = "X = X + 1; " end + -- add a long string to the source + source[#source + 1] = string.format("Y = '%s'", string.rep("a", N)); source = table.concat(source) -- give chunk an explicit name to avoid using source as name source = load(source, "name1") -- dump without debug information source = string.dump(source, true) - -- each "X=X+1" generates 4 opcodes with 4 bytes each - assert(#source > N * 4 * 4) + -- each "X=X+1" generates 4 opcodes with 4 bytes each, plus the string + assert(#source > N * 4 * 4 + N) collectgarbage(); collectgarbage() local m1 = collectgarbage"count" * 1024 -- load dump using fixed buffer @@ -544,9 +546,11 @@ do ]], source) collectgarbage() local m2 = collectgarbage"count" * 1024 - -- load used fewer than 300 bytes - assert(m2 > m1 and m2 - m1 < 300) - X = 0; code(); assert(X == N); X = nil + -- load used fewer than 350 bytes. Code alone has more than 3*N bytes, + -- and string literal has N bytes. Both were not loaded. + assert(m2 > m1 and m2 - m1 < 350) + X = 0; code(); assert(X == N and Y == string.rep("a", N)) + X = nil; Y = nil end From eabf425c76e0089eb88e102e2a44d8c8a37bc213 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 13 Nov 2023 13:11:09 -0300 Subject: [PATCH 0852/1145] Correct anchoring and GC barriers in 'loadString' Call to 'luaH_setint' could call the GC with the string unanchored. Moreover, previously saved strings were being assigned to the prototype without a barrier. --- ldump.c | 7 ++---- lundump.c | 72 +++++++++++++++++++++++++++---------------------------- 2 files changed, 37 insertions(+), 42 deletions(-) diff --git a/ldump.c b/ldump.c index 090d6aab5d..9c315cdd41 100644 --- a/ldump.c +++ b/ldump.c @@ -144,7 +144,7 @@ static void dumpCode (DumpState *D, const Proto *f) { } -static void dumpFunction(DumpState *D, const Proto *f); +static void dumpFunction (DumpState *D, const Proto *f); static void dumpConstants (DumpState *D, const Proto *f) { int i; @@ -218,10 +218,6 @@ static void dumpDebug (DumpState *D, const Proto *f) { static void dumpFunction (DumpState *D, const Proto *f) { - if (D->strip) - dumpString(D, NULL); /* no debug info */ - else - dumpString(D, f->source); dumpInt(D, f->linedefined); dumpInt(D, f->lastlinedefined); dumpByte(D, f->numparams); @@ -231,6 +227,7 @@ static void dumpFunction (DumpState *D, const Proto *f) { dumpConstants(D, f); dumpUpvalues(D, f); dumpProtos(D, f); + dumpString(D, D->strip ? NULL : f->source); dumpDebug(D, f); } diff --git a/lundump.c b/lundump.c index 09752d9910..3f18343acf 100644 --- a/lundump.c +++ b/lundump.c @@ -132,57 +132,49 @@ static lua_Integer loadInteger (LoadState *S) { /* -** Load a nullable string into prototype 'p'. +** Load a nullable string into slot 'sl' from prototype 'p'. The +** assignment to the slot and the barrier must be performed before any +** possible GC activity, to anchor the string. (Both 'loadVector' and +** 'luaH_setint' can call the GC.) */ -static TString *loadStringN (LoadState *S, Proto *p) { +static void loadString (LoadState *S, Proto *p, TString **sl) { lua_State *L = S->L; TString *ts; TValue sv; size_t size = loadSize(S); - if (size == 0) /* no string? */ - return NULL; + if (size == 0) { /* no string? */ + *sl = NULL; + return; + } else if (size == 1) { /* previously saved string? */ int idx = loadInt(S); /* get its index */ TValue stv; luaH_getint(S->h, idx, &stv); - return tsvalue(&stv); + *sl = ts = tsvalue(&stv); + luaC_objbarrier(L, p, ts); + return; } else if ((size -= 2) <= LUAI_MAXSHORTLEN) { /* short string? */ char buff[LUAI_MAXSHORTLEN + 1]; /* extra space for '\0' */ loadVector(S, buff, size + 1); /* load string into buffer */ - ts = luaS_newlstr(L, buff, size); /* create string */ + *sl = ts = luaS_newlstr(L, buff, size); /* create string */ + luaC_objbarrier(L, p, ts); } - else { /* long string */ - if (S->fixed) { /* for a fixed buffer, use a fixed string */ - const char *s = getaddr(S, size + 1, char); /* get content address */ - ts = luaS_newextlstr(L, s, size, NULL, NULL); - } - else { /* create internal copy */ - ts = luaS_createlngstrobj(L, size); /* create string */ - setsvalue2s(L, L->top.p, ts); /* anchor it ('loadVector' can GC) */ - luaD_inctop(L); - loadVector(S, getlngstr(ts), size); /* load directly in final place */ - loadByte(S); /* skip ending '\0' */ - L->top.p--; /* pop string */ - } + else if (S->fixed) { /* for a fixed buffer, use a fixed string */ + const char *s = getaddr(S, size + 1, char); /* get content address */ + *sl = ts = luaS_newextlstr(L, s, size, NULL, NULL); + luaC_objbarrier(L, p, ts); + } + else { /* create internal copy */ + *sl = ts = luaS_createlngstrobj(L, size); /* create string */ + luaC_objbarrier(L, p, ts); + loadVector(S, getlngstr(ts), size); /* load directly in final place */ + loadByte(S); /* skip ending '\0' */ } - luaC_objbarrier(L, p, ts); S->nstr++; /* add string to list of saved strings */ setsvalue(L, &sv, ts); luaH_setint(L, S->h, S->nstr, &sv); luaC_objbarrierback(L, obj2gco(S->h), ts); - return ts; -} - - -/* -** Load a non-nullable string into prototype 'p'. -*/ -static TString *loadString (LoadState *S, Proto *p) { - TString *st = loadStringN(S, p); - if (st == NULL) - error(S, "bad format for constant string"); - return st; } @@ -231,9 +223,15 @@ static void loadConstants (LoadState *S, Proto *f) { setivalue(o, loadInteger(S)); break; case LUA_VSHRSTR: - case LUA_VLNGSTR: - setsvalue2n(S->L, o, loadString(S, f)); + case LUA_VLNGSTR: { + lua_assert(f->source == NULL); + loadString(S, f, &f->source); /* use 'source' to anchor string */ + if (f->source == NULL) + error(S, "bad format for constant string"); + setsvalue2n(S->L, o, f->source); /* save it in the right place */ + f->source = NULL; break; + } default: lua_assert(0); } } @@ -301,7 +299,7 @@ static void loadDebug (LoadState *S, Proto *f) { for (i = 0; i < n; i++) f->locvars[i].varname = NULL; for (i = 0; i < n; i++) { - f->locvars[i].varname = loadStringN(S, f); + loadString(S, f, &f->locvars[i].varname); f->locvars[i].startpc = loadInt(S); f->locvars[i].endpc = loadInt(S); } @@ -309,12 +307,11 @@ static void loadDebug (LoadState *S, Proto *f) { if (n != 0) /* does it have debug information? */ n = f->sizeupvalues; /* must be this many */ for (i = 0; i < n; i++) - f->upvalues[i].name = loadStringN(S, f); + loadString(S, f, &f->upvalues[i].name); } static void loadFunction (LoadState *S, Proto *f) { - f->source = loadStringN(S, f); f->linedefined = loadInt(S); f->lastlinedefined = loadInt(S); f->numparams = loadByte(S); @@ -326,6 +323,7 @@ static void loadFunction (LoadState *S, Proto *f) { loadConstants(S, f); loadUpvalues(S, f); loadProtos(S, f); + loadString(S, f, &f->source); loadDebug(S, f); } From 6d042a178fba32d10ec23c98fb2fd284397ccddc Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 13 Nov 2023 13:12:33 -0300 Subject: [PATCH 0853/1145] Auxiliary buffer uses external strings The buffer system from the auxiliary library reuses its buffer as external memory when closing long strings. --- lauxlib.c | 54 +++++++++++++++++++++++++++++++++-------------- testes/gc.lua | 3 --- testes/locals.lua | 10 ++------- 3 files changed, 40 insertions(+), 27 deletions(-) diff --git a/lauxlib.c b/lauxlib.c index 555a5976ef..73190975db 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -470,18 +470,27 @@ typedef struct UBox { } UBox; +/* Resize the buffer used by a box. Optimize for the common case of +** resizing to the old size. (For instance, __gc will resize the box +** to 0 even after it was closed. 'pushresult' may also resize it to a +** final size that is equal to the one set when the buffer was created.) +*/ static void *resizebox (lua_State *L, int idx, size_t newsize) { - void *ud; - lua_Alloc allocf = lua_getallocf(L, &ud); UBox *box = (UBox *)lua_touserdata(L, idx); - void *temp = allocf(ud, box->box, box->bsize, newsize); - if (l_unlikely(temp == NULL && newsize > 0)) { /* allocation error? */ - lua_pushliteral(L, "not enough memory"); - lua_error(L); /* raise a memory error */ + if (box->bsize == newsize) /* not changing size? */ + return box->box; /* keep the buffer */ + else { + void *ud; + lua_Alloc allocf = lua_getallocf(L, &ud); + void *temp = allocf(ud, box->box, box->bsize, newsize); + if (l_unlikely(temp == NULL && newsize > 0)) { /* allocation error? */ + lua_pushliteral(L, "not enough memory"); + lua_error(L); /* raise a memory error */ + } + box->box = temp; + box->bsize = newsize; + return temp; } - box->box = temp; - box->bsize = newsize; - return temp; } @@ -526,15 +535,15 @@ static void newbox (lua_State *L) { /* ** Compute new size for buffer 'B', enough to accommodate extra 'sz' -** bytes. (The test for "not big enough" also gets the case when the -** computation of 'newsize' overflows.) +** bytes plus one for a terminating zero. (The test for "not big enough" +** also gets the case when the computation of 'newsize' overflows.) */ static size_t newbuffsize (luaL_Buffer *B, size_t sz) { size_t newsize = (B->size / 2) * 3; /* buffer size * 1.5 */ - if (l_unlikely(MAX_SIZET - sz < B->n)) /* overflow in (B->n + sz)? */ + if (l_unlikely(MAX_SIZET - sz - 1 < B->n)) /* overflow in (B->n + sz + 1)? */ return luaL_error(B->L, "buffer too large"); - if (newsize < B->n + sz) /* not big enough? */ - newsize = B->n + sz; + if (newsize < B->n + sz + 1) /* not big enough? */ + newsize = B->n + sz + 1; return newsize; } @@ -594,9 +603,22 @@ LUALIB_API void luaL_addstring (luaL_Buffer *B, const char *s) { LUALIB_API void luaL_pushresult (luaL_Buffer *B) { lua_State *L = B->L; checkbufferlevel(B, -1); - lua_pushlstring(L, B->b, B->n); - if (buffonstack(B)) + if (!buffonstack(B)) /* using static buffer? */ + lua_pushlstring(L, B->b, B->n); /* save result as regular string */ + else { /* reuse buffer already allocated */ + UBox *box = (UBox *)lua_touserdata(L, -1); + void *ud; + lua_Alloc allocf = lua_getallocf(L, &ud); /* function to free buffer */ + size_t len = B->n; /* final string length */ + char *s; + resizebox(L, -1, len + 1); /* adjust box size to content size */ + s = (char*)box->box; /* final buffer address */ + s[len] = '\0'; /* add ending zero */ + /* clear box, as 'lua_pushextlstring' will take control over buffer */ + box->bsize = 0; box->box = NULL; + lua_pushextlstring(L, s, len, allocf, ud); lua_closeslot(L, -2); /* close the box */ + } lua_remove(L, -2); /* remove box or placeholder from the stack */ } diff --git a/testes/gc.lua b/testes/gc.lua index 03093e34ff..3c928d7c15 100644 --- a/testes/gc.lua +++ b/testes/gc.lua @@ -460,10 +460,7 @@ do -- tests for string keys in weak tables a[string.rep("a", 2^22)] = 25 -- long string key -> number value a[string.rep("b", 2^22)] = {} -- long string key -> colectable value a[{}] = 14 -- colectable key - assert(collectgarbage("count") > m + 2^13) -- 2^13 == 2 * 2^22 in KB collectgarbage() - assert(collectgarbage("count") >= m + 2^12 and - collectgarbage("count") < m + 2^13) -- one key was collected local k, v = next(a) -- string key with number value preserved assert(k == string.rep("a", 2^22) and v == 25) assert(next(a, k) == nil) -- everything else cleared diff --git a/testes/locals.lua b/testes/locals.lua index 2c48546d5d..090d846bef 100644 --- a/testes/locals.lua +++ b/testes/locals.lua @@ -728,14 +728,8 @@ if rawget(_G, "T") then -- first buffer was released by 'toclose' assert(T.totalmem() - m <= extra) - -- error in creation of final string - T.totalmem(m + 2 * lim + extra) - assert(not pcall(table.concat, a)) - -- second buffer was released by 'toclose' - assert(T.totalmem() - m <= extra) - - -- userdata, buffer, buffer, final string - T.totalmem(m + 4*lim + extra) + -- userdata, buffer, final string + T.totalmem(m + 2*lim + extra) assert(#table.concat(a) == 2*lim) T.totalmem(0) -- remove memory limit From 1028f296a8e6477cb556c75fe1397cd4e2762abe Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 13 Nov 2023 13:41:59 -0300 Subject: [PATCH 0854/1145] Default paths stored as external strings --- loadlib.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/loadlib.c b/loadlib.c index 6d289fcebb..4f8024c697 100644 --- a/loadlib.c +++ b/loadlib.c @@ -283,7 +283,8 @@ static int noenv (lua_State *L) { /* -** Set a path +** Set a path. (If using the default path, assume it is a string +** literal in C and create it as an external string.) */ static void setpath (lua_State *L, const char *fieldname, const char *envname, @@ -294,7 +295,7 @@ static void setpath (lua_State *L, const char *fieldname, if (path == NULL) /* no versioned environment variable? */ path = getenv(envname); /* try unversioned name */ if (path == NULL || noenv(L)) /* no environment variable? */ - lua_pushstring(L, dft); /* use default */ + lua_pushextlstring(L, dft, strlen(dft), NULL, NULL); /* use default */ else if ((dftmark = strstr(path, LUA_PATH_SEP LUA_PATH_SEP)) == NULL) lua_pushstring(L, path); /* nothing to change */ else { /* path contains a ";;": insert default path in its place */ From 25cd3d377ec13176a6701d9d21a278ba8f2bc3d6 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 15 Nov 2023 10:28:32 -0300 Subject: [PATCH 0855/1145] Buffer in 'luai_makeseed' measured in bytes In the (rare) cases when sizeof(void*) or sizeof(time_t) are not multiples of sizeof(int), we still can use all their bytes in the seed. --- lauxlib.c | 25 +++++++++++++------------ ltests.c | 7 +++++++ 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/lauxlib.c b/lauxlib.c index 73190975db..927e36f06c 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -1124,29 +1124,30 @@ static void warnfon (void *ud, const char *message, int tocont) { #include -/* -** Size of 'e' measured in number of 'unsigned int's. (In the weird -** case that the division truncates, we just lose some part of the -** value, no big deal.) -*/ -#define sof(e) (sizeof(e) / sizeof(unsigned int)) +/* Size for the buffer, in bytes */ +#define BUFSEEDB (sizeof(void*) + sizeof(time_t)) + +/* Size for the buffer in int's, rounded up */ +#define BUFSEED ((BUFSEEDB + sizeof(int) - 1) / sizeof(int)) -#define addbuff(b,v) \ - (memcpy(b, &(v), sof(v) * sizeof(unsigned int)), b += sof(v)) +#define addbuff(b,v) (memcpy(b, &(v), sizeof(v)), b += sizeof(v)) static unsigned int luai_makeseed (void) { - unsigned int buff[sof(void*) + sof(time_t)]; + unsigned int buff[BUFSEED]; unsigned int res; - unsigned int *b = buff; + unsigned int i; time_t t = time(NULL); void *h = buff; + char *b = (char*)buff; addbuff(b, h); /* local variable's address */ addbuff(b, t); /* time */ + /* fill (rare but possible) remain of the buffer with zeros */ + memset(b, 0, BUFSEED * sizeof(int) - BUFSEEDB); res = buff[0]; - for (b = buff + 1; b < buff + sof(buff); b++) - res ^= (res >> 3) + (res << 7) + *b; + for (i = 0; i < BUFSEED; i++) + res ^= (res >> 3) + (res << 7) + buff[i]; return res; } diff --git a/ltests.c b/ltests.c index 94bd4e33cb..bd4147f24e 100644 --- a/ltests.c +++ b/ltests.c @@ -1160,6 +1160,12 @@ static int num2int (lua_State *L) { } +static int makeseed (lua_State *L) { + lua_pushinteger(L, luaL_makeseed(L)); + return 1; +} + + static int newstate (lua_State *L) { void *ud; lua_Alloc f = lua_getallocf(L, &ud); @@ -1962,6 +1968,7 @@ static const struct luaL_Reg tests_funcs[] = { {"newstate", newstate}, {"newuserdata", newuserdata}, {"num2int", num2int}, + {"makeseed", makeseed}, {"pushuserdata", pushuserdata}, {"querystr", string_query}, {"querytab", table_query}, From 52b899d60d8c61b8affe0206014173912de94940 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 24 Nov 2023 14:41:07 -0300 Subject: [PATCH 0856/1145] Simpler coding for new representation for arrays With the tags comming first in a cell, we can define the whole cell as a C type and let C do part of the address computations. --- lobject.h | 2 +- ltable.c | 46 +++++++++++++++++++++++++++------------------- ltable.h | 38 ++++++++------------------------------ 3 files changed, 36 insertions(+), 50 deletions(-) diff --git a/lobject.h b/lobject.h index 8688a84266..6da502156f 100644 --- a/lobject.h +++ b/lobject.h @@ -762,7 +762,7 @@ typedef union Node { #define setnorealasize(t) ((t)->flags |= BITRAS) -typedef union ArrayCell ArrayCell; +typedef struct ArrayCell ArrayCell; typedef struct Table { diff --git a/ltable.c b/ltable.c index c9f66b3c22..d3e90696a4 100644 --- a/ltable.c +++ b/ltable.c @@ -541,29 +541,28 @@ static int numusehash (const Table *t, unsigned int *nums, unsigned int *pna) { /* -** Convert an "abstract size" (number of values in an array) to -** "concrete size" (number of cell elements in the array). Cells -** do not need to be full; we only must make sure it has the values -** needed and its 'tag' element. So, we compute the concrete tag index -** and the concrete value index of the last element, get their maximum -** and adds 1. +** Convert an "abstract size" (number of slots in an array) to +** "concrete size" (number of bytes in the array). +** If the abstract size is not a multiple of NM, the last cell is +** incomplete, so we don't need to allocate memory for the whole cell. +** 'extra' computes how many values are not needed in that last cell. +** It will be zero when 'size' is a multiple of NM, and from there it +** increases as 'size' decreases, up to (NM - 1). */ -static unsigned int concretesize (unsigned int size) { - if (size == 0) return 0; - else { - unsigned int ts = TagIndex(size - 1); - unsigned int vs = ValueIndex(size - 1); - return ((ts >= vs) ? ts : vs) + 1; - } +static size_t concretesize (unsigned int size) { + unsigned int numcells = (size + NM - 1) / NM; /* (size / NM) rounded up */ + unsigned int extra = NM - 1 - ((size + NM - 1) % NM); + return numcells * sizeof(ArrayCell) - extra * sizeof(Value); } static ArrayCell *resizearray (lua_State *L , Table *t, unsigned int oldasize, unsigned int newasize) { - oldasize = concretesize(oldasize); - newasize = concretesize(newasize); - return luaM_reallocvector(L, t->array, oldasize, newasize, ArrayCell); + size_t oldasizeb = concretesize(oldasize); + size_t newasizeb = concretesize(newasize); + void *a = luaM_reallocvector(L, t->array, oldasizeb, newasizeb, lu_byte); + return cast(ArrayCell*, a); } @@ -747,10 +746,19 @@ Table *luaH_new (lua_State *L) { } +/* +** Frees a table. The assert ensures the correctness of 'concretesize', +** checking its result against the address of the last element in the +** array part of the table, computed abstractly. +*/ void luaH_free (lua_State *L, Table *t) { - unsigned ps = concretesize(luaH_realasize(t)); + unsigned int realsize = luaH_realasize(t); + size_t sizeb = concretesize(realsize); + lua_assert((sizeb == 0 && realsize == 0) || + cast_charp(t->array) + sizeb - sizeof(Value) == + cast_charp(getArrVal(t, realsize - 1))); freehash(L, t); - luaM_freearray(L, t->array, ps); + luaM_freemem(L, t->array, sizeb); luaM_free(L, t); } @@ -944,7 +952,7 @@ TString *luaH_getstrkey (Table *t, TString *key) { ** main search function */ int luaH_get (Table *t, const TValue *key, TValue *res) { - const TValue *slot; + const TValue *slot; switch (ttypetag(key)) { case LUA_VSHRSTR: slot = luaH_Hgetshortstr(t, tsvalue(key)); diff --git a/ltable.h b/ltable.h index d488a1f7c3..5581efb132 100644 --- a/ltable.h +++ b/ltable.h @@ -69,44 +69,22 @@ /* ** The array part of a table is represented by an array of cells. -** Each cell is composed of (NM + 1) elements, and each element has the -** type 'ArrayCell'. In each cell, only one element has the variant -** 'tag', while the other NM elements have the variant 'value'. The -** array in the 'tag' element holds the tags of the other elements in -** that cell. +** Each cell is composed of NM tags followed by NM values, so that +** no space is wasted in padding. */ -#define NM ((unsigned int)sizeof(Value)) +#define NM cast_uint(sizeof(Value)) -union ArrayCell { - unsigned char tag[NM]; - Value value; +struct ArrayCell { + lu_byte tag[NM]; + Value value[NM]; }; -/* -** 'NMTag' defines which cell element has the tags; that could be any -** value between 0 (tags come before all values) and NM (tags come after -** all values). -*/ -#define NMTag 0 - - -/* -** Computes the concrete index that holds the tag of abstract index 'i' -*/ -#define TagIndex(i) (((i)/NM * (NM + 1u)) + NMTag) - -/* -** Computes the concrete index that holds the value of abstract index 'i' -*/ -#define ValueIndex(i) ((i) + (((i) + (NM - NMTag))/NM)) - - /* Computes the address of the tag for the abstract index 'k' */ -#define getArrTag(t,k) (&(t)->array[TagIndex(k)].tag[(k)%NM]) +#define getArrTag(t,k) (&(t)->array[(k)/NM].tag[(k)%NM]) /* Computes the address of the value for the abstract index 'k' */ -#define getArrVal(t,k) (&(t)->array[ValueIndex(k)].value) +#define getArrVal(t,k) (&(t)->array[(k)/NM].value[(k)%NM]) /* From 011850a8f86f514d1ba2ebf7a9411c8036b917f4 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 24 Nov 2023 14:44:38 -0300 Subject: [PATCH 0857/1145] Details in the manual --- manual/manual.of | 34 +++++++++++++--------------------- 1 file changed, 13 insertions(+), 21 deletions(-) diff --git a/manual/manual.of b/manual/manual.of index 9ca28aee47..263ced728d 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -2563,8 +2563,8 @@ See also @Lid{luaL_checklstring}, @Lid{luaL_checkstring}, and @Lid{luaL_tolstring} in the auxiliary library.) In general, -Lua's garbage collection can free or move internal memory -and then invalidate pointers to internal strings. +Lua's garbage collection can free or move memory +and then invalidate pointers to strings handled by a Lua state. To allow a safe use of these pointers, the API guarantees that any pointer to a string in a stack index is valid while the string value at that index is not removed from the stack. @@ -3158,8 +3158,6 @@ The index must be the last index previously marked to be closed A @idx{__close} metamethod cannot yield when called through this function. -(This function was introduced in @N{release 5.4.3}.) - } @APIEntry{int lua_closethread (lua_State *L, lua_State *from);| @@ -3179,8 +3177,6 @@ The parameter @id{from} represents the coroutine that is resetting @id{L}. If there is no such coroutine, this parameter can be @id{NULL}. -(This function was introduced in @N{release 5.4.6}.) - } @APIEntry{int lua_compare (lua_State *L, int index1, int index2, int op);| @@ -4227,14 +4223,6 @@ and then pops the top element. } -@APIEntry{int lua_resetthread (lua_State *L);| -@apii{0,?,-} - -This function is deprecated; -it is equivalent to @Lid{lua_closethread} with -@id{from} being @id{NULL}. - -} @APIEntry{int lua_resume (lua_State *L, lua_State *from, int nargs, int *nresults);| @@ -4516,8 +4504,6 @@ indicates whether the operation succeeded. @apii{0,0,m} Converts the Lua value at the given index to a @N{C string}. -If @id{len} is not @id{NULL}, -it sets @T{*len} with the string length. The Lua value must be a string or a number; otherwise, the function returns @id{NULL}. If the value is a number, @@ -4526,12 +4512,16 @@ then @id{lua_tolstring} also (This change confuses @Lid{lua_next} when @id{lua_tolstring} is applied to keys during a table traversal.) -@id{lua_tolstring} returns a pointer -to a string inside the Lua state @see{constchar}. -This string always has a zero (@Char{\0}) -after its last character (as @N{in C}), +If @id{len} is not @id{NULL}, +the function sets @T{*len} with the string length. +The returned @N{C string} always has a zero (@Char{\0}) +after its last character, but can contain other zeros in its body. +The pointer returned by @id{lua_tolstring} +may be invalidated by the garbage collector if the +corresponding Lua value is removed from the stack @see{constchar}. + } @APIEntry{lua_Number lua_tonumber (lua_State *L, int index);| @@ -9063,7 +9053,6 @@ The options are: @item{@T{--}| stop handling options;} @item{@T{-}| execute @id{stdin} as a file and stop handling options.} } -(The form @T{-l @rep{g=mod}} was introduced in @N{release 5.4.4}.) After handling its options, @id{lua} runs the given @emph{script}. When called without arguments, @@ -9251,6 +9240,9 @@ declare a local variable with the same name in the loop body. @itemize{ @item{ +The function @id{lua_resetthread} is deprecated; +it is equivalent to @Lid{lua_closethread} with +@id{from} being @id{NULL}. } } From 842a83f09caa2ebd4bc03e0076420148ac07c808 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 24 Nov 2023 16:08:55 -0300 Subject: [PATCH 0858/1145] Panic functions should not raise errors The standard panic function was using 'lua_tostring', which may raise a memory-allocation error if error value is a number. --- lauxlib.c | 9 +++++++-- ltests.c | 5 +++-- manual/manual.of | 4 ++++ 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/lauxlib.c b/lauxlib.c index 4ca6c65488..1c9082e67e 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -1025,9 +1025,14 @@ static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) { } +/* +** Standard panic funcion just prints an error message. The test +** with 'lua_type' avoids possible memory errors in 'lua_tostring'. +*/ static int panic (lua_State *L) { - const char *msg = lua_tostring(L, -1); - if (msg == NULL) msg = "error object is not a string"; + const char *msg = (lua_type(L, -1) == LUA_TSTRING) + ? lua_tostring(L, -1) + : "error object is not a string"; lua_writestringerror("PANIC: unprotected error in call to Lua API (%s)\n", msg); return 0; /* return to Lua to abort */ diff --git a/ltests.c b/ltests.c index 7d184c0d8e..c2943a4f8e 100644 --- a/ltests.c +++ b/ltests.c @@ -73,8 +73,9 @@ static void badexit (const char *fmt, const char *s1, const char *s2) { static int tpanic (lua_State *L) { - const char *msg = lua_tostring(L, -1); - if (msg == NULL) msg = "error object is not a string"; + const char *msg = (lua_type(L, -1) == LUA_TSTRING) + ? lua_tostring(L, -1) + : "error object is not a string"; return (badexit("PANIC: unprotected error in call to Lua API (%s)\n", msg, NULL), 0); /* do not return to Lua */ diff --git a/manual/manual.of b/manual/manual.of index ad120f5e48..cef3e22a63 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -4486,6 +4486,10 @@ This string always has a zero (@Char{\0}) after its last character (as @N{in C}), but can contain other zeros in its body. +This function can raise memory errors only +when converting a number to a string +(as then it has to create a new string). + } @APIEntry{lua_Number lua_tonumber (lua_State *L, int index);| From 63d68bd657b7386c9c58b4439a100ea0ccbd633e Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 29 Nov 2023 16:22:09 -0300 Subject: [PATCH 0859/1145] Comments detailing the ages for generational GC Plus other comments and small details. --- lauxlib.c | 5 ++--- lgc.c | 11 +++++++---- lgc.h | 44 +++++++++++++++++++++++++++++++++++++++++--- lstate.c | 5 +++-- ltests.c | 10 ++++++---- 5 files changed, 59 insertions(+), 16 deletions(-) diff --git a/lauxlib.c b/lauxlib.c index 927e36f06c..ab3c7c93a7 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -1139,12 +1139,11 @@ static unsigned int luai_makeseed (void) { unsigned int res; unsigned int i; time_t t = time(NULL); - void *h = buff; char *b = (char*)buff; - addbuff(b, h); /* local variable's address */ + addbuff(b, b); /* local variable's address */ addbuff(b, t); /* time */ /* fill (rare but possible) remain of the buffer with zeros */ - memset(b, 0, BUFSEED * sizeof(int) - BUFSEEDB); + memset(b, 0, sizeof(buff) - BUFSEEDB); res = buff[0]; for (i = 0; i < BUFSEED; i++) res ^= (res >> 3) + (res << 7) + buff[i]; diff --git a/lgc.c b/lgc.c index 3884aad038..daee570732 100644 --- a/lgc.c +++ b/lgc.c @@ -1121,6 +1121,7 @@ static GCObject **sweepgen (lua_State *L, global_State *g, GCObject **p, curr->marked = cast_byte(marked | G_SURVIVAL | white); } else { /* all other objects will be old, and so keep their color */ + lua_assert(getage(curr) != G_OLD1); /* advanced in 'markold' */ setage(curr, nextage[getage(curr)]); if (getage(curr) == G_OLD1 && *pfirstold1 == NULL) *pfirstold1 = curr; /* first OLD1 object in the list */ @@ -1145,13 +1146,15 @@ static void whitelist (global_State *g, GCObject *p) { /* -** Correct a list of gray objects. Return pointer to where rest of the -** list should be linked. +** Correct a list of gray objects. Return a pointer to the last element +** left on the list, so that we can link another list to the end of +** this one. ** Because this correction is done after sweeping, young objects might ** be turned white and still be in the list. They are only removed. ** 'TOUCHED1' objects are advanced to 'TOUCHED2' and remain on the list; -** Non-white threads also remain on the list; 'TOUCHED2' objects become -** regular old; they and anything else are removed from the list. +** Non-white threads also remain on the list. 'TOUCHED2' objects and +** anything else become regular old, are marked black, and are removed +** from the list. */ static GCObject **correctgraylist (GCObject **p) { GCObject *curr; diff --git a/lgc.h b/lgc.h index 959465ec97..f06427c294 100644 --- a/lgc.h +++ b/lgc.h @@ -123,6 +123,43 @@ #define changeage(o,f,t) \ check_exp(getage(o) == (f), (o)->marked ^= ((f)^(t))) +/* +** In generational mode, objects are created 'new'. After surviving one +** cycle, they become 'survival'. Both 'new' and 'survival' can point +** to any other object, as they are traversed at the end of the cycle. +** We call them both 'young' objects. +** If a survival object survives another cycle, it becomes 'old1'. +** 'old1' objects can still point to survival objects (but not to +** new objects), so they still must be traversed. After another cycle +** (that, being old, 'old1' objects will "survive" no matter what) +** finally the 'old1' object becomes really 'old', and then they +** are no more traversed. +** +** To keep its invariants, the generational mode uses the same barriers +** also used by the incremental mode. If a young object is caught in a +** foward barrier, it cannot become old immediately, because it can +** still point to other young objects. Instead, it becomes 'old0', +** which in the next cycle becomes 'old1'. So, 'old0' objects is +** old but can point to new and survival objects; 'old1' is old +** but cannot point to new objects; and 'old' cannot point to any +** young object. +** +** If any old object ('old0', 'old1', 'old') is caught in a back +** barrier, it becomes 'touched1' and goes into a gray list, to be +** visited at the end of the cycle. There it evolves to 'touched2', +** which can point to survivals but not to new objects. In yet another +** cycle then it becomes 'old' again. +** +** The generational mode must also control the colors of objects, +** because of the barriers. While the mutator is running, young objects +** are kept white. 'old', 'old1', and 'touched2' objects are kept black, +** as they cannot point to new objects; exceptions are threads and open +** upvalues, which age to 'old1' and 'old' but are kept gray. 'old0' +** objects may be gray or black, as in the incremental mode. 'touched1' +** objects are kept gray, as they must be visited again at the end of +** the cycle. +*/ + /* Default Values for GC parameters */ @@ -161,9 +198,10 @@ ** (value * original parameter / 100). ** ** For most parameters, which are typically larger than 100%, 2^n is -** 16 (2^4), allowing maximum values up to 1599. For the minor -** multiplier, which is typically smaller, 2^n is 64 (2^6) to allow more -** precision. +** 16 (2^4), allowing maximum values up to ~1500%, with a granularity +** of ~6%. For the minor multiplier, which is typically smaller, +** 2^n is 64 (2^6) to allow more precision. In that case, the maximum +** value is ~400%, with a granularity of ~1.5%. */ #define gcparamshift(p) \ (offsetof(global_State, p) == offsetof(global_State, genminormul) ? 6 : 4) diff --git a/lstate.c b/lstate.c index c01e53edb5..1216db35fe 100644 --- a/lstate.c +++ b/lstate.c @@ -52,8 +52,9 @@ typedef struct LG { /* -** set GCdebt to a new value keeping the value (totalobjs + GCdebt) -** invariant (and avoiding underflows in 'totalobjs') +** set GCdebt to a new value keeping the real number of allocated +** objects (totalobjs - GCdebt) invariant and avoiding overflows in +** 'totalobjs'. */ void luaE_setdebt (global_State *g, l_obj debt) { l_obj tb = gettotalobjs(g); diff --git a/ltests.c b/ltests.c index bd4147f24e..09c2e030f8 100644 --- a/ltests.c +++ b/ltests.c @@ -302,8 +302,8 @@ static int testobjref1 (global_State *g, GCObject *f, GCObject *t) { else { /* generational mode */ if ((getage(f) == G_OLD && isblack(f)) && !isold(t)) return 0; - if (((getage(f) == G_OLD1 || getage(f) == G_TOUCHED2) && isblack(f)) && - getage(t) == G_NEW) + if ((getage(f) == G_OLD1 || getage(f) == G_TOUCHED2) && + getage(t) == G_NEW) return 0; return 1; } @@ -510,7 +510,8 @@ static void checkrefs (global_State *g, GCObject *o) { ** * objects must be old enough for their lists ('listage'). ** * old objects cannot be white. ** * old objects must be black, except for 'touched1', 'old0', -** threads, and open upvalues. +** threads, and open upvalues. +** * 'touched1' objects must be gray. */ static void checkobject (global_State *g, GCObject *o, int maybedead, int listage) { @@ -520,14 +521,15 @@ static void checkobject (global_State *g, GCObject *o, int maybedead, assert(g->gcstate != GCSpause || iswhite(o)); if (g->gckind == KGC_GEN) { /* generational mode? */ assert(getage(o) >= listage); - assert(!iswhite(o) || !isold(o)); if (isold(o)) { + assert(!iswhite(o)); assert(isblack(o) || getage(o) == G_TOUCHED1 || getage(o) == G_OLD0 || o->tt == LUA_VTHREAD || (o->tt == LUA_VUPVAL && upisopen(gco2upv(o)))); } + assert(getage(o) != G_TOUCHED1 || isgray(o)); } checkrefs(g, o); } From 35a2fed2d1e0b95a1bfab364707e469863517085 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 30 Nov 2023 15:51:02 -0300 Subject: [PATCH 0860/1145] Removed deprecated options in 'lua_gc' Options 'setpause' and 'setstepmul' were deprecated in Lua 5.4. --- lapi.c | 18 +++--------------- lbaselib.c | 14 ++------------ lua.h | 8 +++----- testes/gc.lua | 9 ++------- 4 files changed, 10 insertions(+), 39 deletions(-) diff --git a/lapi.c b/lapi.c index 2aaa65059a..936951444c 100644 --- a/lapi.c +++ b/lapi.c @@ -1163,7 +1163,7 @@ LUA_API int lua_gc (lua_State *L, int what, ...) { va_list argp; int res = 0; global_State *g = G(L); - if (g->gcstp & GCSTPGC) /* internal stop? */ + if (g->gcstp & (GCSTPGC | GCSTPCLS)) /* internal stop? */ return -1; /* all options are invalid when stopped */ lua_lock(L); va_start(argp, what); @@ -1174,7 +1174,7 @@ LUA_API int lua_gc (lua_State *L, int what, ...) { } case LUA_GCRESTART: { luaE_setdebt(g, 0); - g->gcstp = 0; /* (bit GCSTPGC must be zero here) */ + g->gcstp = 0; /* (other bits must be zero here) */ break; } case LUA_GCCOLLECT: { @@ -1194,7 +1194,7 @@ LUA_API int lua_gc (lua_State *L, int what, ...) { int todo = va_arg(argp, int); /* work to be done */ int didsomething = 0; lu_byte oldstp = g->gcstp; - g->gcstp = 0; /* allow GC to run (bit GCSTPGC must be zero here) */ + g->gcstp = 0; /* allow GC to run (other bits must be zero here) */ if (todo == 0) todo = 1 << g->gcstepsize; /* standard step size */ while (todo >= g->GCdebt) { /* enough to run a step? */ @@ -1213,18 +1213,6 @@ LUA_API int lua_gc (lua_State *L, int what, ...) { res = 1; /* signal it */ break; } - case LUA_GCSETPAUSE: { - unsigned int data = va_arg(argp, unsigned int); - res = applygcparam(g, gcpause, 100); - setgcparam(g, gcpause, data); - break; - } - case LUA_GCSETSTEPMUL: { - unsigned int data = va_arg(argp, unsigned int); - res = applygcparam(g, gcstepmul, 100); - setgcparam(g, gcstepmul, data); - break; - } case LUA_GCISRUNNING: { res = gcrunning(g); break; diff --git a/lbaselib.c b/lbaselib.c index 5a23c937c6..54a6c02deb 100644 --- a/lbaselib.c +++ b/lbaselib.c @@ -198,11 +198,9 @@ static int pushmode (lua_State *L, int oldmode) { static int luaB_collectgarbage (lua_State *L) { static const char *const opts[] = {"stop", "restart", "collect", - "count", "step", "setpause", "setstepmul", - "isrunning", "generational", "incremental", NULL}; + "count", "step", "isrunning", "generational", "incremental", NULL}; static const int optsnum[] = {LUA_GCSTOP, LUA_GCRESTART, LUA_GCCOLLECT, - LUA_GCCOUNT, LUA_GCSTEP, LUA_GCSETPAUSE, LUA_GCSETSTEPMUL, - LUA_GCISRUNNING, LUA_GCGEN, LUA_GCINC}; + LUA_GCCOUNT, LUA_GCSTEP, LUA_GCISRUNNING, LUA_GCGEN, LUA_GCINC}; int o = optsnum[luaL_checkoption(L, 1, "collect", opts)]; switch (o) { case LUA_GCCOUNT: { @@ -219,14 +217,6 @@ static int luaB_collectgarbage (lua_State *L) { lua_pushboolean(L, res); return 1; } - case LUA_GCSETPAUSE: - case LUA_GCSETSTEPMUL: { - int p = (int)luaL_optinteger(L, 2, 0); - int previous = lua_gc(L, o, p); - checkvalres(previous); - lua_pushinteger(L, previous); - return 1; - } case LUA_GCISRUNNING: { int res = lua_gc(L, o); checkvalres(res); diff --git a/lua.h b/lua.h index ca8d06fe24..32768561b2 100644 --- a/lua.h +++ b/lua.h @@ -334,11 +334,9 @@ LUA_API void (lua_warning) (lua_State *L, const char *msg, int tocont); #define LUA_GCCOUNT 3 #define LUA_GCCOUNTB 4 #define LUA_GCSTEP 5 -#define LUA_GCSETPAUSE 6 -#define LUA_GCSETSTEPMUL 7 -#define LUA_GCISRUNNING 9 -#define LUA_GCGEN 10 -#define LUA_GCINC 11 +#define LUA_GCISRUNNING 6 +#define LUA_GCGEN 7 +#define LUA_GCINC 8 LUA_API int (lua_gc) (lua_State *L, int what, ...); diff --git a/testes/gc.lua b/testes/gc.lua index 3c928d7c15..4cf7d556c7 100644 --- a/testes/gc.lua +++ b/testes/gc.lua @@ -27,23 +27,18 @@ end -- test weird parameters to 'collectgarbage' do - -- save original parameters - local a = collectgarbage("setpause", 200) - local b = collectgarbage("setstepmul", 200) local t = {0, 2, 10, 90, 500, 5000, 30000, 0x7ffffffe} for i = 1, #t do local p = t[i] for j = 1, #t do local m = t[j] - collectgarbage("setpause", p) - collectgarbage("setstepmul", m) + collectgarbage("incremental", p, m) collectgarbage("step", 0) collectgarbage("step", 10000) end end -- restore original parameters - collectgarbage("setpause", a) - collectgarbage("setstepmul", b) + collectgarbage("incremental", 200, 300) collectgarbage() end From b719ff9399cbc4b19b2b8325417fc5fa0ef3d0e3 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 1 Dec 2023 16:38:28 -0300 Subject: [PATCH 0861/1145] Removed parameter in 'collectgarbage("step")' A call to 'collectgarbage("step")' always performs one GC basic step. --- lapi.c | 18 ++---------------- testes/gc.lua | 42 +----------------------------------------- 2 files changed, 3 insertions(+), 57 deletions(-) diff --git a/lapi.c b/lapi.c index 936951444c..f807bd4b81 100644 --- a/lapi.c +++ b/lapi.c @@ -1191,25 +1191,11 @@ LUA_API int lua_gc (lua_State *L, int what, ...) { break; } case LUA_GCSTEP: { - int todo = va_arg(argp, int); /* work to be done */ - int didsomething = 0; lu_byte oldstp = g->gcstp; g->gcstp = 0; /* allow GC to run (other bits must be zero here) */ - if (todo == 0) - todo = 1 << g->gcstepsize; /* standard step size */ - while (todo >= g->GCdebt) { /* enough to run a step? */ - todo -= g->GCdebt; /* decrement 'todo' */ - luaC_step(L); /* run one basic step */ - didsomething = 1; - if (g->gckind == KGC_GEN) /* minor collections? */ - todo = 0; /* doesn't make sense to repeat in this case */ - else if (g->gcstate == GCSpause) - break; /* don't run more than one cycle */ - } - /* remove remaining 'todo' from total debt */ - luaE_setdebt(g, g->GCdebt - todo); + luaC_step(L); /* run one basic step */ g->gcstp = oldstp; /* restore previous state */ - if (didsomething && g->gcstate == GCSpause) /* end of cycle? */ + if (g->gcstate == GCSpause) /* end of cycle? */ res = 1; /* signal it */ break; } diff --git a/testes/gc.lua b/testes/gc.lua index 4cf7d556c7..d7e0c4ffe6 100644 --- a/testes/gc.lua +++ b/testes/gc.lua @@ -33,8 +33,7 @@ do for j = 1, #t do local m = t[j] collectgarbage("incremental", p, m) - collectgarbage("step", 0) - collectgarbage("step", 10000) + collectgarbage("step") end end -- restore original parameters @@ -169,45 +168,6 @@ do end --- --- test the "size" of basic GC steps (whatever they mean...) --- -do -print("steps") - - print("steps (2)") - - local function dosteps (siz) - collectgarbage() - local a = {} - for i=1,100 do a[i] = {{}}; local b = {} end - local x = gcinfo() - local i = 0 - repeat -- do steps until it completes a collection cycle - i = i+1 - until collectgarbage("step", siz) - assert(gcinfo() < x) - return i -- number of steps - end - - collectgarbage"stop" - - if not _port then - assert(dosteps(10) < dosteps(2)) - end - - -- collector should do a full collection with so many steps - assert(dosteps(20000) == 1) - assert(collectgarbage("step", 20000) == true) - assert(collectgarbage("step", 20000) == true) - - assert(not collectgarbage("isrunning")) - collectgarbage"restart" - assert(collectgarbage("isrunning")) - -end - - if not _port then -- test the pace of the collector collectgarbage(); collectgarbage() From 74b401353892318cd7ded6ca149258feb21d1724 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 1 Dec 2023 16:42:01 -0300 Subject: [PATCH 0862/1145] Removed macro 'changeage' It is simpler to use always 'setage'. The saving from 'changeage' is too irrelevant. --- lgc.c | 24 ++++++++++++------------ lgc.h | 2 -- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/lgc.c b/lgc.c index daee570732..6a77b09b3a 100644 --- a/lgc.c +++ b/lgc.c @@ -431,7 +431,7 @@ static void genlink (global_State *g, GCObject *o) { linkobjgclist(o, g->grayagain); /* link it back in 'grayagain' */ } /* everything else do not need to be linked back */ else if (getage(o) == G_TOUCHED2) - changeage(o, G_TOUCHED2, G_OLD); /* advance age */ + setage(o, G_OLD); /* advance age */ } @@ -826,9 +826,9 @@ static void freeobj (lua_State *L, GCObject *o) { /* ** sweep at most 'countin' elements from a list of GCObjects erasing dead ** objects, where a dead object is one marked with the old (non current) -** white; change all non-dead objects back to white, preparing for next -** collection cycle. Return where to continue the traversal or NULL if -** list is finished. +** white; change all non-dead objects back to white (and new), preparing +** for next collection cycle. Return where to continue the traversal or +** NULL if list is finished. */ static GCObject **sweeplist (lua_State *L, GCObject **p, int countin) { global_State *g = G(L); @@ -842,8 +842,8 @@ static GCObject **sweeplist (lua_State *L, GCObject **p, int countin) { *p = curr->next; /* remove 'curr' from list */ freeobj(L, curr); /* erase 'curr' */ } - else { /* change mark to 'white' */ - curr->marked = cast_byte((marked & ~maskgcbits) | white); + else { /* change mark to 'white' and age to 'new' */ + curr->marked = cast_byte((marked & ~maskgcbits) | white | G_NEW); p = &curr->next; /* go to next element */ } } @@ -1042,8 +1042,8 @@ void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt) { /* -** Set the "time" to wait before starting a new GC cycle; cycle will -** start when number of objects in use hits the threshold of +** Set the "time" to wait before starting a new incremental cycle; +** cycle will start when number of objects in use hits the threshold of ** approximately (marked * pause / 100). */ static void setpause (global_State *g) { @@ -1165,7 +1165,7 @@ static GCObject **correctgraylist (GCObject **p) { else if (getage(curr) == G_TOUCHED1) { /* touched in this cycle? */ lua_assert(isgray(curr)); nw2black(curr); /* make it black, for next barrier */ - changeage(curr, G_TOUCHED1, G_TOUCHED2); + setage(curr, G_TOUCHED2); goto remain; /* keep it in the list and go to next element */ } else if (curr->tt == LUA_VTHREAD) { @@ -1175,7 +1175,7 @@ static GCObject **correctgraylist (GCObject **p) { else { /* everything else is removed */ lua_assert(isold(curr)); /* young objects should be white here */ if (getage(curr) == G_TOUCHED2) /* advance from TOUCHED2... */ - changeage(curr, G_TOUCHED2, G_OLD); /* ... to OLD */ + setage(curr, G_OLD); /* ... to OLD */ nw2black(curr); /* make object black (to be removed) */ goto remove; } @@ -1210,7 +1210,7 @@ static void markold (global_State *g, GCObject *from, GCObject *to) { for (p = from; p != to; p = p->next) { if (getage(p) == G_OLD1) { lua_assert(!iswhite(p)); - changeage(p, G_OLD1, G_OLD); /* now they are old */ + setage(p, G_OLD); /* now they are old */ if (isblack(p)) reallymarkobject(g, p); } @@ -1399,7 +1399,7 @@ static void genmajorstep (lua_State *L, global_State *g) { static void genstep (lua_State *L, global_State *g) { l_obj majorbase = g->GClastmajor; /* count after last major collection */ l_obj majorinc = applygcparam(g, genmajormul, majorbase); - if (gettotalobjs(g) > majorbase + majorinc && 0) { + if (gettotalobjs(g) > majorbase + majorinc) { /* do a major collection */ enterinc(g); g->gckind = KGC_GENMAJOR; diff --git a/lgc.h b/lgc.h index f06427c294..3cc0c80d6a 100644 --- a/lgc.h +++ b/lgc.h @@ -120,8 +120,6 @@ #define setage(o,a) ((o)->marked = cast_byte(((o)->marked & (~AGEBITS)) | a)) #define isold(o) (getage(o) > G_SURVIVAL) -#define changeage(o,f,t) \ - check_exp(getage(o) == (f), (o)->marked ^= ((f)^(t))) /* ** In generational mode, objects are created 'new'. After surviving one From 789e7acdea3ada96bd00b7aac6d82e805bfee85c Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 6 Dec 2023 10:49:56 -0300 Subject: [PATCH 0863/1145] Major collections done incrementally Major collections do not need to "stop the world". Still pending: criteria for shifts minor-major, shifts generational-incremental. --- lapi.c | 2 +- lgc.c | 141 ++++++++++++++++++++++++++++--------------------------- lstate.h | 6 +-- ltests.c | 6 +-- 4 files changed, 80 insertions(+), 75 deletions(-) diff --git a/lapi.c b/lapi.c index f807bd4b81..71b679aa60 100644 --- a/lapi.c +++ b/lapi.c @@ -1211,7 +1211,7 @@ LUA_API int lua_gc (lua_State *L, int what, ...) { setgcparam(g, genminormul, minormul); if (majormul != 0) setgcparam(g, genmajormul, majormul); - luaC_changemode(L, KGC_GEN); + luaC_changemode(L, KGC_GENMINOR); break; } case LUA_GCINC: { diff --git a/lgc.c b/lgc.c index 6a77b09b3a..50e6e0e9d2 100644 --- a/lgc.c +++ b/lgc.c @@ -206,7 +206,7 @@ void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v) { } else { /* sweep phase */ lua_assert(issweepphase(g)); - if (g->gckind != KGC_GEN) /* incremental mode? */ + if (g->gckind != KGC_GENMINOR) /* incremental mode? */ makewhite(g, o); /* mark 'o' as white to avoid other barriers */ } } @@ -219,7 +219,8 @@ void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v) { void luaC_barrierback_ (lua_State *L, GCObject *o) { global_State *g = G(L); lua_assert(isblack(o) && !isdead(g, o)); - lua_assert((g->gckind == KGC_GEN) == (isold(o) && getage(o) != G_TOUCHED1)); + lua_assert((g->gckind != KGC_GENMINOR) + || (isold(o) && getage(o) != G_TOUCHED1)); if (getage(o) == G_TOUCHED2) /* already in gray list? */ set2gray(o); /* make it gray to become touched1 */ else /* link it in 'grayagain' and paint it gray */ @@ -1116,13 +1117,14 @@ static GCObject **sweepgen (lua_State *L, global_State *g, GCObject **p, freeobj(L, curr); /* erase 'curr' */ } else { /* correct mark and age */ - if (getage(curr) == G_NEW) { /* new objects go back to white */ + int age = getage(curr); + if (age == G_NEW) { /* new objects go back to white */ int marked = curr->marked & ~maskgcbits; /* erase GC bits */ curr->marked = cast_byte(marked | G_SURVIVAL | white); } else { /* all other objects will be old, and so keep their color */ - lua_assert(getage(curr) != G_OLD1); /* advanced in 'markold' */ - setage(curr, nextage[getage(curr)]); + lua_assert(age != G_OLD1); /* advanced in 'markold' */ + setage(curr, nextage[age]); if (getage(curr) == G_OLD1 && *pfirstold1 == NULL) *pfirstold1 = curr; /* first OLD1 object in the list */ } @@ -1202,8 +1204,9 @@ static void correctgraylists (global_State *g) { /* ** Mark black 'OLD1' objects when starting a new young collection. -** Gray objects are already in some gray list, and so will be visited -** in the atomic step. +** Gray objects are already in some gray list, and so will be visited in +** the atomic step. The counter 'GCmajorminor' keeps how many objects to +** become old before a major collection. */ static void markold (global_State *g, GCObject *from, GCObject *to) { GCObject *p; @@ -1211,6 +1214,7 @@ static void markold (global_State *g, GCObject *from, GCObject *to) { if (getage(p) == G_OLD1) { lua_assert(!iswhite(p)); setage(p, G_OLD); /* now they are old */ + g->GCmajorminor--; /* one more old object */ if (isblack(p)) reallymarkobject(g, p); } @@ -1230,23 +1234,46 @@ static void finishgencycle (lua_State *L, global_State *g) { } +/* +** shifts from the end of an atomic step in a minor collection to +** major collections. +*/ +static void atomic2major (lua_State *L, global_State *g) { + l_obj stepsize = cast(l_obj, 1) << g->gcstepsize; + g->GCmajorminor = gettotalobjs(g); + g->gckind = KGC_GENMAJOR; + g->reallyold = g->old1 = g->survival = NULL; + g->finobjrold = g->finobjold1 = g->finobjsur = NULL; + entersweep(L); /* continue from atomic as an incremental cycle */ + luaE_setdebt(g, stepsize); +} + + /* ** Does a young collection. First, mark 'OLD1' objects. Then does the -** atomic step. Then, sweep all lists and advance pointers. Finally, -** finish the collection. +** atomic step. Then, check whether to continue in minor mode. If so, +** sweep all lists and advance pointers. Finally, finish the collection. */ static void youngcollection (lua_State *L, global_State *g) { GCObject **psurvival; /* to point to first non-dead survival object */ GCObject *dummy; /* dummy out parameter to 'sweepgen' */ lua_assert(g->gcstate == GCSpropagate); + g->marked = 0; if (g->firstold1) { /* are there regular OLD1 objects? */ markold(g, g->firstold1, g->reallyold); /* mark them */ g->firstold1 = NULL; /* no more OLD1 objects (for now) */ } markold(g, g->finobj, g->finobjrold); markold(g, g->tobefnz, NULL); + atomic(L); + /* decide whether to shift to major mode */ + if (g->GCmajorminor <= 0) { /* ?? */ + atomic2major(L, g); /* go to major mode */ + return; /* nothing else to be done here */ + } + /* sweep nursery and get a pointer to its last live element */ g->gcstate = GCSswpallgc; psurvival = sweepgen(L, g, &g->allgc, g->survival, &g->firstold1); @@ -1291,8 +1318,8 @@ static void atomic2gen (lua_State *L, global_State *g) { sweep2old(L, &g->tobefnz); - g->gckind = KGC_GEN; - g->GClastmajor = gettotalobjs(g); /* base for memory control */ + g->gckind = KGC_GENMINOR; + g->GCmajorminor = applygcparam(g, genmajormul, g->marked); finishgencycle(L, g); } @@ -1328,9 +1355,9 @@ static void entergen (lua_State *L, global_State *g) { */ static void enterinc (global_State *g) { whitelist(g, g->allgc); - g->reallyold = g->old1 = g->survival = NULL; whitelist(g, g->finobj); whitelist(g, g->tobefnz); + g->reallyold = g->old1 = g->survival = NULL; g->finobjrold = g->finobjold1 = g->finobjsur = NULL; g->gcstate = GCSpause; g->gckind = KGC_INC; @@ -1350,7 +1377,7 @@ void luaC_changemode (lua_State *L, int newmode) { enterinc(g); /* entering incremental mode */ } else { - lua_assert(newmode == KGC_GEN); + lua_assert(newmode == KGC_GENMINOR); entergen(L, g); } } @@ -1367,50 +1394,19 @@ static void fullgen (lua_State *L, global_State *g) { /* -** Does a major collector up to the atomic phase and then either -** returns to minor collections or stays doing major ones. If the -** number of objects collected this time (numobjs - marked) is more than -** half the number of objects created since the last major collection -** (numobjs - lastmajor), it goes back to minor collections. +** After an atomic incremental step from a major collection, +** check whether collector could return to minor collections. */ -static void genmajorstep (lua_State *L, global_State *g) { - l_obj lastmajor = g->GClastmajor; /* count from last collection */ - l_obj numobjs = gettotalobjs(g); /* current count */ - luaC_runtilstate(L, bitmask(GCSpropagate)); /* start new cycle */ - atomic(L); /* mark everybody */ - if ((numobjs - g->marked) > ((numobjs - lastmajor) >> 1)) { - atomic2gen(L, g); /* return to generational mode */ - setminordebt(g); - } - else { /* bad collection; stay in major mode */ - entersweep(L); - luaC_runtilstate(L, bitmask(GCSpause)); /* finish collection */ - setpause(g); - g->GClastmajor = gettotalobjs(g); - } -} - - -/* -** Does a generational "step". If the total number of objects grew -** more than 'majormul'% since the last major collection, does a -** major collection. Otherwise, does a minor collection. -*/ -static void genstep (lua_State *L, global_State *g) { - l_obj majorbase = g->GClastmajor; /* count after last major collection */ - l_obj majorinc = applygcparam(g, genmajormul, majorbase); - if (gettotalobjs(g) > majorbase + majorinc) { - /* do a major collection */ - enterinc(g); - g->gckind = KGC_GENMAJOR; - genmajorstep(L, g); - } - else { /* regular case; do a minor collection */ - g->marked = 0; - youngcollection(L, g); - setminordebt(g); - lua_assert(g->GClastmajor == majorbase); +static int checkmajorminor (lua_State *L, global_State *g) { + if (g->gckind == KGC_GENMAJOR) { + l_obj numobjs = gettotalobjs(g); /* current count */ + if (g->marked < numobjs - (numobjs >> 2)) { /* ?? */ + atomic2gen(L, g); /* return to generational mode */ + setminordebt(g); + return 0; /* exit incremental collection */ + } } + return 1; /* stay doing incremental collections */ } /* }====================================================== */ @@ -1548,7 +1544,8 @@ static l_obj singlestep (lua_State *L) { } case GCSenteratomic: { work = atomic(L); - entersweep(L); + if (checkmajorminor(L, g)) + entersweep(L); break; } case GCSswpallgc: { /* sweep "regular" objects */ @@ -1597,6 +1594,7 @@ static l_obj singlestep (lua_State *L) { */ void luaC_runtilstate (lua_State *L, int statesmask) { global_State *g = G(L); + lua_assert(g->gckind == KGC_INC); while (!testbit(statesmask, g->gcstate)) singlestep(L); } @@ -1615,13 +1613,14 @@ static void incstep (lua_State *L, global_State *g) { l_obj work2do = applygcparam(g, gcstepmul, stepsize); do { /* repeat until pause or enough "credit" (negative debt) */ l_obj work = singlestep(L); /* perform one single step */ + if (g->gckind == KGC_GENMINOR) /* returned to minor collections? */ + return; /* nothing else to be done here */ work2do -= work; } while (work2do > 0 && g->gcstate != GCSpause); if (g->gcstate == GCSpause) setpause(g); /* pause until next cycle */ - else { + else luaE_setdebt(g, stepsize); - } } /* @@ -1635,17 +1634,18 @@ void luaC_step (lua_State *L) { if (!gcrunning(g)) /* not running? */ luaE_setdebt(g, 2000); else { +//printf("> step: %d %d %ld %ld -> ", g->gckind, g->gcstate, gettotalobjs(g), g->GCdebt); + switch (g->gckind) { - case KGC_INC: + case KGC_INC: case KGC_GENMAJOR: incstep(L, g); break; - case KGC_GEN: - genstep(L, g); - break; - case KGC_GENMAJOR: - genmajorstep(L, g); + case KGC_GENMINOR: + youngcollection(L, g); + setminordebt(g); break; } +//printf("%d %d %ld %ld\n", g->gckind, g->gcstate, gettotalobjs(g), g->GCdebt); } } @@ -1681,10 +1681,15 @@ void luaC_fullgc (lua_State *L, int isemergency) { global_State *g = G(L); lua_assert(!g->gcemergency); g->gcemergency = isemergency; /* set flag */ - if (g->gckind == KGC_GEN) - fullgen(L, g); - else - fullinc(L, g); + switch (g->gckind) { + case KGC_GENMINOR: fullgen(L, g); break; + case KGC_INC: fullinc(L, g); break; + case KGC_GENMAJOR: + g->gckind = KGC_INC; + fullinc(L, g); + g->gckind = KGC_GENMAJOR; + break; + } g->gcemergency = 0; } diff --git a/lstate.h b/lstate.h index f42db35d7c..1868981bb9 100644 --- a/lstate.h +++ b/lstate.h @@ -149,8 +149,8 @@ struct lua_longjmp; /* defined in ldo.c */ /* kinds of Garbage Collection */ #define KGC_INC 0 /* incremental gc */ -#define KGC_GEN 1 /* generational gc */ -#define KGC_GENMAJOR 2 /* generational in "major" mode */ +#define KGC_GENMINOR 1 /* generational gc in minor (regular) mode */ +#define KGC_GENMAJOR 2 /* generational in major mode */ typedef struct stringtable { @@ -259,7 +259,7 @@ typedef struct global_State { l_obj totalobjs; /* total number of objects allocated + GCdebt */ l_obj GCdebt; /* objects counted but not yet allocated */ l_obj marked; /* number of objects marked in a GC cycle */ - l_obj GClastmajor; /* objects at last major collection */ + l_obj GCmajorminor; /* auxiliar counter to control major-minor shifts */ stringtable strt; /* hash table for strings */ TValue l_registry; TValue nilvalue; /* a nil value */ diff --git a/ltests.c b/ltests.c index 09c2e030f8..9bc2f0da23 100644 --- a/ltests.c +++ b/ltests.c @@ -297,7 +297,7 @@ static int testobjref1 (global_State *g, GCObject *f, GCObject *t) { if (isdead(g,t)) return 0; if (issweepphase(g)) return 1; /* no invariants */ - else if (g->gckind != KGC_GEN) + else if (g->gckind != KGC_GENMINOR) return !(isblack(f) && iswhite(t)); /* basic incremental invariant */ else { /* generational mode */ if ((getage(f) == G_OLD && isblack(f)) && !isold(t)) @@ -519,7 +519,7 @@ static void checkobject (global_State *g, GCObject *o, int maybedead, assert(maybedead); else { assert(g->gcstate != GCSpause || iswhite(o)); - if (g->gckind == KGC_GEN) { /* generational mode? */ + if (g->gckind == KGC_GENMINOR) { /* generational mode? */ assert(getage(o) >= listage); if (isold(o)) { assert(!iswhite(o)); @@ -953,7 +953,7 @@ static int gc_state (lua_State *L) { } else { global_State *g = G(L); - if (G(L)->gckind == KGC_GEN) + if (G(L)->gckind != KGC_INC) luaL_error(L, "cannot change states in generational mode"); lua_lock(L); if (option < g->gcstate) { /* must cross 'pause'? */ From 925fe8a0f2a667c96c015ee74d1a702af9ea5507 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 7 Dec 2023 15:45:11 -0300 Subject: [PATCH 0864/1145] First criteria for shifts minor<->major --- lapi.c | 25 +++++---- lbaselib.c | 13 ++--- lgc.c | 81 +++++++++++++++++++--------- lgc.h | 48 +++++++++-------- lstate.c | 3 +- lstate.h | 9 ++-- lua.c | 2 +- manual/manual.of | 134 +++++++++++++++++++++++++++-------------------- 8 files changed, 187 insertions(+), 128 deletions(-) diff --git a/lapi.c b/lapi.c index 71b679aa60..374b3872aa 100644 --- a/lapi.c +++ b/lapi.c @@ -1204,26 +1204,29 @@ LUA_API int lua_gc (lua_State *L, int what, ...) { break; } case LUA_GCGEN: { - unsigned int minormul = va_arg(argp, unsigned int); - unsigned int majormul = va_arg(argp, unsigned int); + int minormul = va_arg(argp, int); + int minormajor = va_arg(argp, int); + int majorminor = va_arg(argp, int); res = (g->gckind == KGC_INC) ? LUA_GCINC : LUA_GCGEN; - if (minormul != 0) + if (minormul >= 0) setgcparam(g, genminormul, minormul); - if (majormul != 0) - setgcparam(g, genmajormul, majormul); + if (minormajor >= 0) + setgcparam(g, minormajor, minormajor); + if (majorminor >= 0) + setgcparam(g, majorminor, majorminor); luaC_changemode(L, KGC_GENMINOR); break; } case LUA_GCINC: { - unsigned int pause = va_arg(argp, unsigned int); - unsigned int stepmul = va_arg(argp, unsigned int); - unsigned int stepsize = va_arg(argp, unsigned int); + int pause = va_arg(argp, int); + int stepmul = va_arg(argp, int); + int stepsize = va_arg(argp, int); res = (g->gckind == KGC_INC) ? LUA_GCINC : LUA_GCGEN; - if (pause != 0) + if (pause >= 0) setgcparam(g, gcpause, pause); - if (stepmul != 0) + if (stepmul >= 0) setgcparam(g, gcstepmul, stepmul); - if (stepsize != 0) + if (stepsize >= 0) g->gcstepsize = (stepsize <= log2maxs(l_obj)) ? stepsize : log2maxs(l_obj); luaC_changemode(L, KGC_INC); diff --git a/lbaselib.c b/lbaselib.c index 54a6c02deb..9ad84dcfd6 100644 --- a/lbaselib.c +++ b/lbaselib.c @@ -224,14 +224,15 @@ static int luaB_collectgarbage (lua_State *L) { return 1; } case LUA_GCGEN: { - int minormul = (int)luaL_optinteger(L, 2, 0); - int majormul = (int)luaL_optinteger(L, 3, 0); - return pushmode(L, lua_gc(L, o, minormul, majormul)); + int minormul = (int)luaL_optinteger(L, 2, -1); + int majorminor = (int)luaL_optinteger(L, 3, -1); + int minormajor = (int)luaL_optinteger(L, 4, -1); + return pushmode(L, lua_gc(L, o, minormul, majorminor, minormajor)); } case LUA_GCINC: { - int pause = (int)luaL_optinteger(L, 2, 0); - int stepmul = (int)luaL_optinteger(L, 3, 0); - int stepsize = (int)luaL_optinteger(L, 4, 0); + int pause = (int)luaL_optinteger(L, 2, -1); + int stepmul = (int)luaL_optinteger(L, 3, -1); + int stepsize = (int)luaL_optinteger(L, 4, -1); return pushmode(L, lua_gc(L, o, pause, stepmul, stepsize)); } default: { diff --git a/lgc.c b/lgc.c index 50e6e0e9d2..a4f6376546 100644 --- a/lgc.c +++ b/lgc.c @@ -1205,20 +1205,21 @@ static void correctgraylists (global_State *g) { /* ** Mark black 'OLD1' objects when starting a new young collection. ** Gray objects are already in some gray list, and so will be visited in -** the atomic step. The counter 'GCmajorminor' keeps how many objects to -** become old before a major collection. +** the atomic step. Returns the number of objects that became old. */ -static void markold (global_State *g, GCObject *from, GCObject *to) { +static l_obj markold (global_State *g, GCObject *from, GCObject *to) { GCObject *p; + l_obj count = 0; for (p = from; p != to; p = p->next) { if (getage(p) == G_OLD1) { lua_assert(!iswhite(p)); setage(p, G_OLD); /* now they are old */ - g->GCmajorminor--; /* one more old object */ + count++; /* one more old object */ if (isblack(p)) reallymarkobject(g, p); } } + return count; } @@ -1240,7 +1241,7 @@ static void finishgencycle (lua_State *L, global_State *g) { */ static void atomic2major (lua_State *L, global_State *g) { l_obj stepsize = cast(l_obj, 1) << g->gcstepsize; - g->GCmajorminor = gettotalobjs(g); + g->GCmajorminor = g->marked; /* number of live objects */ g->gckind = KGC_GENMAJOR; g->reallyold = g->old1 = g->survival = NULL; g->finobjrold = g->finobjold1 = g->finobjsur = NULL; @@ -1249,30 +1250,54 @@ static void atomic2major (lua_State *L, global_State *g) { } +/* +** Decide whether to shift to major mode. It tests two conditions: +** 1) Whether the number of added old objects in this collection is more +** than half the number of new objects. ("step" is the number of objects +** created between minor collections. Except for forward barriers, it +** is the maximum number of objects that can become old in each minor +** collection.) +** 2) Whether the accumulated number of added old objects is larger +** than 'minormajor'% of the number of lived objects after the last +** major collection. (That percentage is computed in 'limit'.) +*/ +static int checkminormajor (lua_State *L, global_State *g, l_obj addedold1) { + l_obj step = applygcparam(g, genminormul, g->GCmajorminor); + l_obj limit = applygcparam(g, minormajor, g->GCmajorminor); +//printf("-> major? %ld %ld %ld %ld (%ld)\n", g->marked, limit, step, addedold1, gettotalobjs(g)); + if (addedold1 >= (step >> 1) || g->marked >= limit) { + atomic2major(L, g); /* go to major mode */ + return 1; + } + return 0; /* stay in minor mode */ +} + /* ** Does a young collection. First, mark 'OLD1' objects. Then does the ** atomic step. Then, check whether to continue in minor mode. If so, ** sweep all lists and advance pointers. Finally, finish the collection. */ static void youngcollection (lua_State *L, global_State *g) { + l_obj addedold1 = 0; + l_obj marked = g->marked; /* preserve 'g->marked' */ GCObject **psurvival; /* to point to first non-dead survival object */ GCObject *dummy; /* dummy out parameter to 'sweepgen' */ lua_assert(g->gcstate == GCSpropagate); - g->marked = 0; if (g->firstold1) { /* are there regular OLD1 objects? */ - markold(g, g->firstold1, g->reallyold); /* mark them */ + addedold1 += markold(g, g->firstold1, g->reallyold); /* mark them */ g->firstold1 = NULL; /* no more OLD1 objects (for now) */ } - markold(g, g->finobj, g->finobjrold); - markold(g, g->tobefnz, NULL); + addedold1 += markold(g, g->finobj, g->finobjrold); + addedold1 += markold(g, g->tobefnz, NULL); + + atomic(L); /* will lose 'g->marked' */ - atomic(L); + /* keep total number of added old1 objects */ + g->marked = marked + addedold1; /* decide whether to shift to major mode */ - if (g->GCmajorminor <= 0) { /* ?? */ - atomic2major(L, g); /* go to major mode */ + if (checkminormajor(L, g, addedold1)) return; /* nothing else to be done here */ - } /* sweep nursery and get a pointer to its last live element */ g->gcstate = GCSswpallgc; @@ -1319,7 +1344,8 @@ static void atomic2gen (lua_State *L, global_State *g) { sweep2old(L, &g->tobefnz); g->gckind = KGC_GENMINOR; - g->GCmajorminor = applygcparam(g, genmajormul, g->marked); + g->GCmajorminor = g->marked; /* "base" for number of objects */ + g->marked = 0; /* to count the number of added old1 objects */ finishgencycle(L, g); } @@ -1329,7 +1355,7 @@ static void atomic2gen (lua_State *L, global_State *g) { ** total number of objects grows 'genminormul'%. */ static void setminordebt (global_State *g) { - luaE_setdebt(g, applygcparam(g, genminormul, gettotalobjs(g))); + luaE_setdebt(g, applygcparam(g, genminormul, g->GCmajorminor)); } @@ -1369,13 +1395,11 @@ static void enterinc (global_State *g) { */ void luaC_changemode (lua_State *L, int newmode) { global_State *g = G(L); + if (g->gckind == KGC_GENMAJOR) /* doing major collections? */ + g->gckind = KGC_INC; /* already incremental but in name */ if (newmode != g->gckind) { /* does it need to change? */ - if (newmode == KGC_INC) { /* entering incremental mode? */ - if (g->gckind == KGC_GENMAJOR) - g->gckind = KGC_INC; /* already incremental but in name */ - else - enterinc(g); /* entering incremental mode */ - } + if (newmode == KGC_INC) /* entering incremental mode? */ + enterinc(g); /* entering incremental mode */ else { lua_assert(newmode == KGC_GENMINOR); entergen(L, g); @@ -1396,16 +1420,24 @@ static void fullgen (lua_State *L, global_State *g) { /* ** After an atomic incremental step from a major collection, ** check whether collector could return to minor collections. +** It checks whether the number of objects 'tobecollected' +** is greater than 'majorminor'% of the number of objects added +** since the last collection ('addedobjs'). */ static int checkmajorminor (lua_State *L, global_State *g) { if (g->gckind == KGC_GENMAJOR) { - l_obj numobjs = gettotalobjs(g); /* current count */ - if (g->marked < numobjs - (numobjs >> 2)) { /* ?? */ + l_obj numobjs = gettotalobjs(g); + l_obj addedobjs = numobjs - g->GCmajorminor; + l_obj limit = applygcparam(g, majorminor, addedobjs); + l_obj tobecollected = numobjs - g->marked; +//printf("-> minor? %ld %ld %ld\n", tobecollected, limit, numobjs); + if (tobecollected > limit) { atomic2gen(L, g); /* return to generational mode */ setminordebt(g); return 0; /* exit incremental collection */ } } + g->GCmajorminor = g->marked; /* prepare for next collection */ return 1; /* stay doing incremental collections */ } @@ -1634,8 +1666,6 @@ void luaC_step (lua_State *L) { if (!gcrunning(g)) /* not running? */ luaE_setdebt(g, 2000); else { -//printf("> step: %d %d %ld %ld -> ", g->gckind, g->gcstate, gettotalobjs(g), g->GCdebt); - switch (g->gckind) { case KGC_INC: case KGC_GENMAJOR: incstep(L, g); @@ -1645,7 +1675,6 @@ void luaC_step (lua_State *L) { setminordebt(g); break; } -//printf("%d %d %ld %ld\n", g->gckind, g->gcstate, gettotalobjs(g), g->GCdebt); } } diff --git a/lgc.h b/lgc.h index 3cc0c80d6a..4cbc6e61ff 100644 --- a/lgc.h +++ b/lgc.h @@ -161,10 +161,24 @@ /* Default Values for GC parameters */ -/* generational */ +/* +** Minor collections will shift to major ones after LUAI_MINORMAJOR% +** objects become old. +*/ +#define LUAI_MINORMAJOR 100 + +/* +** Major collections will shift to minor ones after a collection +** collects at least LUAI_MAJORMINOR% of the new objects. +*/ +#define LUAI_MAJORMINOR 80 + +/* +** A young (minor) collection will run after creating LUAI_GENMINORMUL% +** new objects. +*/ +#define LUAI_GENMINORMUL 20 -#define LUAI_GENMAJORMUL 100 /* major multiplier */ -#define LUAI_GENMINORMUL 20 /* minor multiplier */ /* incremental */ @@ -187,27 +201,17 @@ /* ** Macros to set and apply GC parameters. GC parameters are given in -** percentage points, but are stored as lu_byte. To reduce their -** values and avoid repeated divisions by 100, these macros store -** the original parameter multiplied by 2^n and divided by 100. -** To apply them, the value is divided by 2^n (a shift) and then -** multiplied by the stored parameter, yielding -** value / 2^n * (original parameter * 2^n / 100), or approximately -** (value * original parameter / 100). -** -** For most parameters, which are typically larger than 100%, 2^n is -** 16 (2^4), allowing maximum values up to ~1500%, with a granularity -** of ~6%. For the minor multiplier, which is typically smaller, -** 2^n is 64 (2^6) to allow more precision. In that case, the maximum -** value is ~400%, with a granularity of ~1.5%. +** percentage points, but are stored as lu_byte. To avoid repeated +** divisions by 100, these macros store the original parameter +** multiplied by 128 and divided by 100. To apply them, if it first +** divides the value by 128 it may lose precision; if it first +** multiplies by the parameter, it may overflow. So, it first divides +** by 32, then multiply by the parameter, and then divides the result by +** 4. */ -#define gcparamshift(p) \ - (offsetof(global_State, p) == offsetof(global_State, genminormul) ? 6 : 4) - -#define setgcparam(g,p,v) \ - (g->p = (cast_uint(v) << gcparamshift(p)) / 100u) -#define applygcparam(g,p,v) (((v) >> gcparamshift(p)) * g->p) +#define setgcparam(g,p,v) (g->gcp##p = (cast_uint(v) << 7) / 100u) +#define applygcparam(g,p,v) ((((v) >> 5) * g->gcp##p) >> 2) /* diff --git a/lstate.c b/lstate.c index 1216db35fe..aab90e34b6 100644 --- a/lstate.c +++ b/lstate.c @@ -368,8 +368,9 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud, unsigned int seed) { setgcparam(g, gcpause, LUAI_GCPAUSE); setgcparam(g, gcstepmul, LUAI_GCMUL); g->gcstepsize = LUAI_GCSTEPSIZE; - setgcparam(g, genmajormul, LUAI_GENMAJORMUL); setgcparam(g, genminormul, LUAI_GENMINORMUL); + setgcparam(g, minormajor, LUAI_MINORMAJOR); + setgcparam(g, majorminor, LUAI_MAJORMINOR); for (i=0; i < LUA_NUMTAGS; i++) g->mt[i] = NULL; if (luaD_rawrunprotected(L, f_luaopen, NULL) != LUA_OK) { /* memory allocation error: free partial state */ diff --git a/lstate.h b/lstate.h index 1868981bb9..49acfb7e31 100644 --- a/lstate.h +++ b/lstate.h @@ -264,16 +264,17 @@ typedef struct global_State { TValue l_registry; TValue nilvalue; /* a nil value */ unsigned int seed; /* randomized seed for hashes */ + unsigned short gcpgenminormul; /* control minor generational collections */ + unsigned short gcpmajorminor; /* control shift major->minor */ + unsigned short gcpminormajor; /* control shift minor->major */ + unsigned short gcpgcpause; /* size of pause between successive GCs */ + unsigned short gcpgcstepmul; /* GC "speed" */ lu_byte currentwhite; lu_byte gcstate; /* state of garbage collector */ lu_byte gckind; /* kind of GC running */ lu_byte gcstopem; /* stops emergency collections */ - lu_byte genminormul; /* control for minor generational collections */ - lu_byte genmajormul; /* control for major generational collections */ lu_byte gcstp; /* control whether GC is running */ lu_byte gcemergency; /* true if this is an emergency collection */ - lu_byte gcpause; /* size of pause between successive GCs */ - lu_byte gcstepmul; /* GC "speed" */ lu_byte gcstepsize; /* (log2 of) GC granularity */ GCObject *allgc; /* list of all collectable objects */ GCObject **sweepgc; /* current position of sweep in list */ diff --git a/lua.c b/lua.c index 1e884b07cb..d26dd8ac90 100644 --- a/lua.c +++ b/lua.c @@ -646,7 +646,7 @@ static int pmain (lua_State *L) { luai_openlibs(L); /* open standard libraries */ createargtable(L, argv, argc, script); /* create table 'arg' */ lua_gc(L, LUA_GCRESTART); /* start GC... */ - lua_gc(L, LUA_GCGEN, 0, 0); /* ...in generational mode */ + lua_gc(L, LUA_GCGEN, -1, -1, -1); /* ...in generational mode */ if (!(args & has_E)) { /* no option '-E'? */ if (handle_luainit(L) != LUA_OK) /* run LUA_INIT */ return 0; /* error running LUA_INIT */ diff --git a/manual/manual.of b/manual/manual.of index 263ced728d..8607e57de0 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -621,7 +621,8 @@ that is inaccessible from Lua. another live object refer to the object.) Because Lua has no knowledge about @N{C code}, it never collects objects accessible through the registry @see{registry}, -which includes the global environment @see{globalenv}. +which includes the global environment @see{globalenv} and +the main thread. The garbage collector (GC) in Lua can work in two modes: @@ -638,8 +639,8 @@ therefore, optimal settings are also non-portable. You can change the GC mode and parameters by calling @Lid{lua_gc} @N{in C} or @Lid{collectgarbage} in Lua. -You can also use these functions to control -the collector directly (e.g., to stop and restart it). +You can also use these functions to control the collector directly, +for instance to stop or restart it. } @@ -656,39 +657,36 @@ and the @def{garbage-collector step size}. The garbage-collector pause controls how long the collector waits before starting a new cycle. -The collector starts a new cycle when the use of memory -hits @M{n%} of the use after the previous collection. +The collector starts a new cycle when the number of objects +hits @M{n%} of the total after the previous collection. Larger values make the collector less aggressive. Values equal to or less than 100 mean the collector will not wait to start a new cycle. -A value of 200 means that the collector waits for the total memory in use -to double before starting a new cycle. +A value of 200 means that the collector waits for +the total number of objects to double before starting a new cycle. The default value is 200; the maximum value is 1000. The garbage-collector step multiplier controls the speed of the collector relative to -memory allocation, +object creation, that is, -how many elements it marks or sweeps for each -kilobyte of memory allocated. -Larger values make the collector more aggressive but also increase -the size of each incremental step. -You should not use values less than 100, -because they make the collector too slow and -can result in the collector never finishing a cycle. -The default value is 100; the maximum value is 1000. +how many objects it marks or sweeps for each object created. +Larger values make the collector more aggressive. +Beware that values too small can +make the collector too slow to ever finish a cycle. +The default value is 300; the maximum value is 1000. The garbage-collector step size controls the size of each incremental step, -specifically how many bytes the interpreter allocates +specifically how many objects the interpreter creates before performing a step. This parameter is logarithmic: -A value of @M{n} means the interpreter will allocate @M{2@sp{n}} -bytes between steps and perform equivalent work during the step. +A value of @M{n} means the interpreter will create @M{2@sp{n}} +objects between steps and perform equivalent work during the step. A large value (e.g., 60) makes the collector a stop-the-world (non-incremental) collector. -The default value is 13, -which means steps of approximately @N{8 Kbytes}. +The default value is 8, +which means steps of approximately @N{256 objects}. } @@ -697,31 +695,44 @@ which means steps of approximately @N{8 Kbytes}. In generational mode, the collector does frequent @emph{minor} collections, which traverses only objects recently created. -If after a minor collection the use of memory is still above a limit, -the collector does a stop-the-world @emph{major} collection, +If after a minor collection the number of objects is above a limit, +the collector shifts to a @emph{major} collection, which traverses all objects. -The generational mode uses two parameters: -the @def{minor multiplier} and the @def{the major multiplier}. +The collector will then stay doing major collections until +it detects that the program is generating enough garbage to justify +going back to minor collections. + +The generational mode uses three parameters: +the @def{minor multiplier}, the @def{minor-major multiplier}, +and the @def{major-minor multiplier}. The minor multiplier controls the frequency of minor collections. For a minor multiplier @M{x}, -a new minor collection will be done when memory -grows @M{x%} larger than the memory in use after the previous major -collection. +a new minor collection will be done when the number of objects +grows @M{x%} larger than the number in use just after the last collection. For instance, for a multiplier of 20, -the collector will do a minor collection when the use of memory -gets 20% larger than the use after the previous major collection. -The default value is 20; the maximum value is 200. - -The major multiplier controls the frequency of major collections. -For a major multiplier @M{x}, -a new major collection will be done when memory -grows @M{x%} larger than the memory in use after the previous major -collection. +the collector will do a minor collection when the number of objects +gets 20% larger than the total after the last major collection. +The default value is 20. + +The minor-major multiplier controls the shift to major collections. +For a multiplier @M{x}, +the collector will shift to a major collection +when the number of old objects grows @M{x%} larger +than the total after the previous major collection. For instance, for a multiplier of 100, -the collector will do a major collection when the use of memory -gets larger than twice the use after the previous collection. -The default value is 100; the maximum value is 1000. +the collector will do a major collection when the number of old objects +gets larger than twice the total after the previous major collection. +The default value is 100. + +The major-minor multiplier controls the shift back to minor collections. +For a multiplier @M{x}, +the collector will shift back to minor collections +after a major collection collects at least @M{x%} of the allocated objects. +In particular, for a multiplier of 0, +the collector will immediately shift back to minor collections +after doing one cycle of major collections. +The default value is 20. } @@ -3311,9 +3322,8 @@ Returns the remainder of dividing the current amount of bytes of memory in use by Lua by 1024. } -@item{@id{LUA_GCSTEP} @T{(int stepsize)}| -Performs an incremental step of garbage collection, -corresponding to the allocation of @id{stepsize} Kbytes. +@item{@id{LUA_GCSTEP}| +Performs a step of garbage collection. } @item{@id{LUA_GCISRUNNING}| @@ -3321,13 +3331,13 @@ Returns a boolean that tells whether the collector is running (i.e., not stopped). } -@item{@id{LUA_GCINC} (int pause, int stepmul, stepsize)| +@item{@id{LUA_GCINC} (int pause, int stepmul, int stepsize)| Changes the collector to incremental mode with the given parameters @see{incmode}. Returns the previous mode (@id{LUA_GCGEN} or @id{LUA_GCINC}). } -@item{@id{LUA_GCGEN} (int minormul, int majormul)| +@item{@id{LUA_GCGEN} (int minormul, int minormajor, int majorminor)| Changes the collector to generational mode with the given parameters @see{genmode}. Returns the previous mode (@id{LUA_GCGEN} or @id{LUA_GCINC}). @@ -6312,13 +6322,14 @@ gives the exact number of bytes in use by Lua. @item{@St{step}| Performs a garbage-collection step. -The step @Q{size} is controlled by @id{arg}. -With a zero value, -the collector will perform one basic (indivisible) step. -For non-zero values, -the collector will perform as if that amount of memory -(in Kbytes) had been allocated by Lua. -Returns @true if the step finished a collection cycle. +In incremental mode, +that step corresponds to the current step size; +the function returns @true if the step finished a collection cycle. +In generational mode, +the step performs a full minor collection or +a major collection, +if the collector has scheduled one; +the function returns @true if the step performed a major collection. } @item{@St{isrunning}| @@ -6332,15 +6343,15 @@ This option can be followed by three numbers: the garbage-collector pause, the step multiplier, and the step size @see{incmode}. -A zero means to not change that value. +A -1 or absent value means to not change that value. } @item{@St{generational}| Change the collector mode to generational. -This option can be followed by two numbers: -the garbage-collector minor multiplier -and the major multiplier @see{genmode}. -A zero means to not change that value. +This option can be followed by three numbers: +the garbage-collector minor multiplier, +the minor-major multiplier, and the major-minor multiplier @see{genmode}. +A -1 or absent value means to not change that value. } } @@ -9229,6 +9240,9 @@ declare a local variable with the same name in the loop body. @itemize{ @item{ +There were several changes in the parameters +for the options @St{incremental} and @St{generational} +of the function @Lid{collectgarbage}. } } @@ -9245,6 +9259,12 @@ it is equivalent to @Lid{lua_closethread} with @id{from} being @id{NULL}. } +@item{ +There were several changes in the parameters +for the options @Lid{LUA_GCINC} and @Lid{LUA_GCGEN} +of the function @Lid{lua_gc}. +} + } } From ad73b332240ef5b9bab1517517f63a1425dc7545 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 7 Dec 2023 15:45:41 -0300 Subject: [PATCH 0865/1145] Check minor->major made at the end of a minor cycle It does not make sense to wait for another cycle to decide when much of the information about creation of old objects is already available. --- lgc.c | 95 ++++++++++++++++++++++------------------------------------- 1 file changed, 35 insertions(+), 60 deletions(-) diff --git a/lgc.c b/lgc.c index a4f6376546..ecd142cb2e 100644 --- a/lgc.c +++ b/lgc.c @@ -1098,7 +1098,8 @@ static void sweep2old (lua_State *L, GCObject **p) { ** will also remove objects turned white here from any gray list. */ static GCObject **sweepgen (lua_State *L, global_State *g, GCObject **p, - GCObject *limit, GCObject **pfirstold1) { + GCObject *limit, GCObject **pfirstold1, + l_obj *paddedold) { static const lu_byte nextage[] = { G_SURVIVAL, /* from G_NEW */ G_OLD1, /* from G_SURVIVAL */ @@ -1108,6 +1109,7 @@ static GCObject **sweepgen (lua_State *L, global_State *g, GCObject **p, G_TOUCHED1, /* from G_TOUCHED1 (do not change) */ G_TOUCHED2 /* from G_TOUCHED2 (do not change) */ }; + l_obj addedold = 0; int white = luaC_white(g); GCObject *curr; while ((curr = *p) != limit) { @@ -1125,28 +1127,20 @@ static GCObject **sweepgen (lua_State *L, global_State *g, GCObject **p, else { /* all other objects will be old, and so keep their color */ lua_assert(age != G_OLD1); /* advanced in 'markold' */ setage(curr, nextage[age]); - if (getage(curr) == G_OLD1 && *pfirstold1 == NULL) - *pfirstold1 = curr; /* first OLD1 object in the list */ + if (getage(curr) == G_OLD1) { + addedold++; /* one more object becoming old */ + if (*pfirstold1 == NULL) + *pfirstold1 = curr; /* first OLD1 object in the list */ + } } p = &curr->next; /* go to next element */ } } + *paddedold += addedold; return p; } -/* -** Traverse a list making all its elements white and clearing their -** age. In incremental mode, all objects are 'new' all the time, -** except for fixed strings (which are always old). -*/ -static void whitelist (global_State *g, GCObject *p) { - int white = luaC_white(g); - for (; p != NULL; p = p->next) - p->marked = cast_byte((p->marked & ~maskgcbits) | white); -} - - /* ** Correct a list of gray objects. Return a pointer to the last element ** left on the list, so that we can link another list to the end of @@ -1205,21 +1199,18 @@ static void correctgraylists (global_State *g) { /* ** Mark black 'OLD1' objects when starting a new young collection. ** Gray objects are already in some gray list, and so will be visited in -** the atomic step. Returns the number of objects that became old. +** the atomic step. */ -static l_obj markold (global_State *g, GCObject *from, GCObject *to) { +static void markold (global_State *g, GCObject *from, GCObject *to) { GCObject *p; - l_obj count = 0; for (p = from; p != to; p = p->next) { if (getage(p) == G_OLD1) { lua_assert(!iswhite(p)); setage(p, G_OLD); /* now they are old */ - count++; /* one more old object */ if (isblack(p)) reallymarkobject(g, p); } } - return count; } @@ -1236,16 +1227,17 @@ static void finishgencycle (lua_State *L, global_State *g) { /* -** shifts from the end of an atomic step in a minor collection to -** major collections. +** Shifts from a minor collection to major collections. It starts in +** the "sweep all" state to clear all objects, which are mostly black +** in generational mode. */ -static void atomic2major (lua_State *L, global_State *g) { +static void minor2inc (lua_State *L, global_State *g, int kind) { l_obj stepsize = cast(l_obj, 1) << g->gcstepsize; g->GCmajorminor = g->marked; /* number of live objects */ - g->gckind = KGC_GENMAJOR; + g->gckind = kind; g->reallyold = g->old1 = g->survival = NULL; g->finobjrold = g->finobjold1 = g->finobjsur = NULL; - entersweep(L); /* continue from atomic as an incremental cycle */ + entersweep(L); /* continue as an incremental cycle */ luaE_setdebt(g, stepsize); } @@ -1266,7 +1258,7 @@ static int checkminormajor (lua_State *L, global_State *g, l_obj addedold1) { l_obj limit = applygcparam(g, minormajor, g->GCmajorminor); //printf("-> major? %ld %ld %ld %ld (%ld)\n", g->marked, limit, step, addedold1, gettotalobjs(g)); if (addedold1 >= (step >> 1) || g->marked >= limit) { - atomic2major(L, g); /* go to major mode */ + minor2inc(L, g, KGC_GENMAJOR); /* go to major mode */ return 1; } return 0; /* stay in minor mode */ @@ -1284,41 +1276,40 @@ static void youngcollection (lua_State *L, global_State *g) { GCObject *dummy; /* dummy out parameter to 'sweepgen' */ lua_assert(g->gcstate == GCSpropagate); if (g->firstold1) { /* are there regular OLD1 objects? */ - addedold1 += markold(g, g->firstold1, g->reallyold); /* mark them */ + markold(g, g->firstold1, g->reallyold); /* mark them */ g->firstold1 = NULL; /* no more OLD1 objects (for now) */ } - addedold1 += markold(g, g->finobj, g->finobjrold); - addedold1 += markold(g, g->tobefnz, NULL); + markold(g, g->finobj, g->finobjrold); + markold(g, g->tobefnz, NULL); atomic(L); /* will lose 'g->marked' */ - /* keep total number of added old1 objects */ - g->marked = marked + addedold1; - - /* decide whether to shift to major mode */ - if (checkminormajor(L, g, addedold1)) - return; /* nothing else to be done here */ - /* sweep nursery and get a pointer to its last live element */ g->gcstate = GCSswpallgc; - psurvival = sweepgen(L, g, &g->allgc, g->survival, &g->firstold1); + psurvival = sweepgen(L, g, &g->allgc, g->survival, &g->firstold1, &addedold1); /* sweep 'survival' */ - sweepgen(L, g, psurvival, g->old1, &g->firstold1); + sweepgen(L, g, psurvival, g->old1, &g->firstold1, &addedold1); g->reallyold = g->old1; g->old1 = *psurvival; /* 'survival' survivals are old now */ g->survival = g->allgc; /* all news are survivals */ /* repeat for 'finobj' lists */ dummy = NULL; /* no 'firstold1' optimization for 'finobj' lists */ - psurvival = sweepgen(L, g, &g->finobj, g->finobjsur, &dummy); + psurvival = sweepgen(L, g, &g->finobj, g->finobjsur, &dummy, &addedold1); /* sweep 'survival' */ - sweepgen(L, g, psurvival, g->finobjold1, &dummy); + sweepgen(L, g, psurvival, g->finobjold1, &dummy, &addedold1); g->finobjrold = g->finobjold1; g->finobjold1 = *psurvival; /* 'survival' survivals are old now */ g->finobjsur = g->finobj; /* all news are survivals */ - sweepgen(L, g, &g->tobefnz, NULL, &dummy); - finishgencycle(L, g); + sweepgen(L, g, &g->tobefnz, NULL, &dummy, &addedold1); + + /* keep total number of added old1 objects */ + g->marked = marked + addedold1; + + /* decide whether to shift to major mode */ + if (!checkminormajor(L, g, addedold1)) + finishgencycle(L, g); /* still in minor mode; finish it */ } @@ -1374,22 +1365,6 @@ static void entergen (lua_State *L, global_State *g) { } -/* -** Enter incremental mode. Turn all objects white, make all -** intermediate lists point to NULL (to avoid invalid pointers), -** and go to the pause state. -*/ -static void enterinc (global_State *g) { - whitelist(g, g->allgc); - whitelist(g, g->finobj); - whitelist(g, g->tobefnz); - g->reallyold = g->old1 = g->survival = NULL; - g->finobjrold = g->finobjold1 = g->finobjsur = NULL; - g->gcstate = GCSpause; - g->gckind = KGC_INC; -} - - /* ** Change collector mode to 'newmode'. */ @@ -1399,7 +1374,7 @@ void luaC_changemode (lua_State *L, int newmode) { g->gckind = KGC_INC; /* already incremental but in name */ if (newmode != g->gckind) { /* does it need to change? */ if (newmode == KGC_INC) /* entering incremental mode? */ - enterinc(g); /* entering incremental mode */ + minor2inc(L, g, KGC_INC); /* entering incremental mode */ else { lua_assert(newmode == KGC_GENMINOR); entergen(L, g); @@ -1412,7 +1387,7 @@ void luaC_changemode (lua_State *L, int newmode) { ** Does a full collection in generational mode. */ static void fullgen (lua_State *L, global_State *g) { - enterinc(g); + minor2inc(L, g, KGC_INC); entergen(L, g); } From 4eda1acafa1a69224b2d4f786cf1ec8f7a4d9ac5 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 14 Dec 2023 11:41:57 -0300 Subject: [PATCH 0866/1145] Cleaner protocol between 'lua_dump' and writer function 'lua_dump' signals to the writer function the end of a dump, so that is has more freedom when using the stack. --- lapi.c | 32 +++++++------------------------- ldump.c | 30 +++++++++++++++++++++++------- lstrlib.c | 19 ++++++++++++------- lundump.c | 8 ++++---- lundump.h | 2 +- manual/manual.of | 21 +++++++++++++++++++-- 6 files changed, 66 insertions(+), 46 deletions(-) diff --git a/lapi.c b/lapi.c index 374b3872aa..fd9ec4e4c0 100644 --- a/lapi.c +++ b/lapi.c @@ -1116,36 +1116,18 @@ LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data, /* -** Dump a function, calling 'writer' to write its parts. Because the -** writer can use the stack in unkown ways, this function should not -** push things on the stack, but it must anchor an auxiliary table -** used by 'luaU_dump'. To do so, it creates the table, anchors the -** function that is on the stack in the table, and substitutes the -** table for the function in the stack. +** Dump a Lua function, calling 'writer' to write its parts. Ensure +** the stack returns with its original size. */ - LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data, int strip) { int status; - StkId fstk; /* pointer to function */ - TValue *o; + ptrdiff_t otop = savestack(L, L->top.p); /* original top */ + TValue *f = s2v(L->top.p - 1); /* function to be dumped */ lua_lock(L); api_checknelems(L, 1); - fstk = L->top.p - 1; - o = s2v(fstk); - if (!isLfunction(o)) - status = 1; - else { - LClosure *f = clLvalue(o); - ptrdiff_t fidx = savestack(L, fstk); /* function index */ - Table *h = luaH_new(L); /* auxiliary table used by 'luaU_dump' */ - sethvalue2s(L, L->top.p, h); /* anchor it (luaH_set may call GC) */ - L->top.p++; /* (assume extra slot) */ - luaH_set(L, h, o, o); /* anchor function into table */ - setobjs2s(L, fstk, L->top.p - 1); /* move table over function */ - L->top.p--; /* stack back to initial size */ - status = luaU_dump(L, f->p, writer, data, strip, h); - setclLvalue2s(L, restorestack(L, fidx), f); /* put function back */ - } + api_check(L, isLfunction(f), "Lua function expected"); + status = luaU_dump(L, clLvalue(f)->p, writer, data, strip); + L->top.p = restorestack(L, otop); /* restore top */ lua_unlock(L); return status; } diff --git a/ldump.c b/ldump.c index 9c315cdd41..6cd5671f2c 100644 --- a/ldump.c +++ b/ldump.c @@ -43,8 +43,13 @@ typedef struct { #define dumpLiteral(D, s) dumpBlock(D,s,sizeof(s) - sizeof(char)) +/* +** Dump the block of memory pointed by 'b' with given 'size'. +** 'b' should not be NULL, except for the last call signaling the end +** of the dump. +*/ static void dumpBlock (DumpState *D, const void *b, size_t size) { - if (D->status == 0 && size > 0) { + if (D->status == 0) { /* do not write anything after an error */ lua_unlock(D->L); D->status = (*D->writer)(D->L, b, size, D->data); lua_lock(D->L); @@ -53,13 +58,18 @@ static void dumpBlock (DumpState *D, const void *b, size_t size) { } +/* +** Dump enough zeros to ensure that current position is a multiple of +** 'align'. +*/ static void dumpAlign (DumpState *D, int align) { int padding = align - (D->offset % align); - if (padding < align) { /* apd == align means no padding */ + if (padding < align) { /* padding == align means no padding */ static lua_Integer paddingContent = 0; + lua_assert(cast_uint(align) <= sizeof(lua_Integer)); dumpBlock(D, &paddingContent, padding); - lua_assert(D->offset % align == 0); } + lua_assert(D->offset % align == 0); } @@ -91,6 +101,7 @@ static void dumpSize (DumpState *D, size_t x) { static void dumpInt (DumpState *D, int x) { + lua_assert(x >= 0); dumpSize(D, x); } @@ -140,6 +151,7 @@ static void dumpString (DumpState *D, TString *ts) { static void dumpCode (DumpState *D, const Proto *f) { dumpInt(D, f->sizecode); dumpAlign(D, sizeof(f->code[0])); + lua_assert(f->code != NULL); dumpVector(D, f->code, f->sizecode); } @@ -196,7 +208,8 @@ static void dumpDebug (DumpState *D, const Proto *f) { int i, n; n = (D->strip) ? 0 : f->sizelineinfo; dumpInt(D, n); - dumpVector(D, f->lineinfo, n); + if (f->lineinfo != NULL) + dumpVector(D, f->lineinfo, n); n = (D->strip) ? 0 : f->sizeabslineinfo; dumpInt(D, n); for (i = 0; i < n; i++) { @@ -248,20 +261,23 @@ static void dumpHeader (DumpState *D) { /* ** dump Lua function as precompiled chunk */ -int luaU_dump(lua_State *L, const Proto *f, lua_Writer w, void *data, - int strip, Table *h) { +int luaU_dump (lua_State *L, const Proto *f, lua_Writer w, void *data, + int strip) { DumpState D; + D.h = luaH_new(L); /* aux. table to keep strings already dumped */ + sethvalue2s(L, L->top.p, D.h); /* anchor it */ + L->top.p++; D.L = L; D.writer = w; D.offset = 0; D.data = data; D.strip = strip; D.status = 0; - D.h = h; D.nstr = 0; dumpHeader(&D); dumpByte(&D, f->sizeupvalues); dumpFunction(&D, f); + dumpBlock(&D, NULL, 0); /* signal end of dump */ return D.status; } diff --git a/lstrlib.c b/lstrlib.c index e29c09b978..a90c4fd161 100644 --- a/lstrlib.c +++ b/lstrlib.c @@ -225,7 +225,12 @@ static int writer (lua_State *L, const void *b, size_t size, void *ud) { state->init = 1; luaL_buffinit(L, &state->B); } - luaL_addlstring(&state->B, (const char *)b, size); + if (b == NULL) { /* finishing dump? */ + luaL_pushresult(&state->B); /* push result */ + lua_replace(L, 1); /* move it to reserved slot */ + } + else + luaL_addlstring(&state->B, (const char *)b, size); return 0; } @@ -233,13 +238,13 @@ static int writer (lua_State *L, const void *b, size_t size, void *ud) { static int str_dump (lua_State *L) { struct str_Writer state; int strip = lua_toboolean(L, 2); - luaL_checktype(L, 1, LUA_TFUNCTION); - lua_settop(L, 1); /* ensure function is on the top of the stack */ + luaL_argcheck(L, lua_type(L, 1) == LUA_TFUNCTION && !lua_iscfunction(L, 1), + 1, "Lua function expected"); + /* ensure function is on the top of the stack and vacate slot 1 */ + lua_pushvalue(L, 1); state.init = 0; - if (l_unlikely(lua_dump(L, writer, &state, strip) != 0)) - return luaL_error(L, "unable to dump given function"); - luaL_pushresult(&state.B); - lua_assert(lua_isfunction(L, 1)); /* lua_dump kept that value */ + lua_dump(L, writer, &state, strip); + lua_settop(L, 1); /* leave final result on top */ return 1; } diff --git a/lundump.c b/lundump.c index 3f18343acf..f850dc4ad7 100644 --- a/lundump.c +++ b/lundump.c @@ -152,7 +152,7 @@ static void loadString (LoadState *S, Proto *p, TString **sl) { luaH_getint(S->h, idx, &stv); *sl = ts = tsvalue(&stv); luaC_objbarrier(L, p, ts); - return; + return; /* do not save it again */ } else if ((size -= 2) <= LUAI_MAXSHORTLEN) { /* short string? */ char buff[LUAI_MAXSHORTLEN + 1]; /* extra space for '\0' */ @@ -168,10 +168,10 @@ static void loadString (LoadState *S, Proto *p, TString **sl) { else { /* create internal copy */ *sl = ts = luaS_createlngstrobj(L, size); /* create string */ luaC_objbarrier(L, p, ts); - loadVector(S, getlngstr(ts), size); /* load directly in final place */ - loadByte(S); /* skip ending '\0' */ + loadVector(S, getlngstr(ts), size + 1); /* load directly in final place */ } - S->nstr++; /* add string to list of saved strings */ + /* add string to list of saved strings */ + S->nstr++; setsvalue(L, &sv, ts); luaH_setint(L, S->h, S->nstr, &sv); luaC_objbarrierback(L, obj2gco(S->h), ts); diff --git a/lundump.h b/lundump.h index 05ac7f856f..b10307e495 100644 --- a/lundump.h +++ b/lundump.h @@ -31,6 +31,6 @@ LUAI_FUNC LClosure* luaU_undump (lua_State* L, ZIO* Z, const char* name, /* dump one chunk; from ldump.c */ LUAI_FUNC int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, - void* data, int strip, Table *h); + void* data, int strip); #endif diff --git a/manual/manual.of b/manual/manual.of index 8607e57de0..ef1bdfd2b4 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -3266,6 +3266,13 @@ As it produces parts of the chunk, with the given @id{data} to write them. +The function @Lid{lua_dump} fully preserves the Lua stack +through the calls to the writer function, +except that it may push some values for internal use +before the first call, +and it restores the stack size to its original size +after the last call. + If @id{strip} is true, the binary representation may not include all debug information about the function, @@ -3275,8 +3282,6 @@ The value returned is the error code returned by the last call to the writer; @N{0 means} no errors. -This function does not pop the Lua function from the stack. - } @APIEntry{int lua_error (lua_State *L);| @@ -4688,6 +4693,10 @@ passing along the buffer to be written (@id{p}), its size (@id{sz}), and the @id{ud} parameter supplied to @Lid{lua_dump}. +After @Lid{lua_dump} writes its last piece, +it will signal that by calling the writer function one more time, +with a @id{NULL} buffer (and size 0). + The writer returns an error code: @N{0 means} no errors; any other value means an error and stops @Lid{lua_dump} from @@ -9259,6 +9268,14 @@ it is equivalent to @Lid{lua_closethread} with @id{from} being @id{NULL}. } +@item{ +The function @Lid{lua_dump} changed the way it keeps the stack +through the calls to the writer function. +(That was not specified in previous versions.) +Also, it calls the writer function one extra time, +to signal the end of the dump. +} + @item{ There were several changes in the parameters for the options @Lid{LUA_GCINC} and @Lid{LUA_GCGEN} From 666e95a66d1a2ceb98bdf320980b3f655264a9c9 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 20 Dec 2023 11:06:27 -0300 Subject: [PATCH 0867/1145] Option 0 for step multiplier makes GC non-incremental --- lgc.c | 67 +++++++++++++++++++++++++++++++----------------- lgc.h | 12 +++++---- llimits.h | 8 ++++-- ltests.c | 8 +++--- manual/manual.of | 28 ++++++++++---------- testes/gengc.lua | 11 ++++++-- 6 files changed, 84 insertions(+), 50 deletions(-) diff --git a/lgc.c b/lgc.c index ecd142cb2e..114b32d330 100644 --- a/lgc.c +++ b/lgc.c @@ -93,6 +93,7 @@ */ #define markobjectN(g,t) { if (t) markobject(g,t); } + static void reallymarkobject (global_State *g, GCObject *o); static l_obj atomic (lua_State *L); static void entersweep (lua_State *L); @@ -831,10 +832,10 @@ static void freeobj (lua_State *L, GCObject *o) { ** for next collection cycle. Return where to continue the traversal or ** NULL if list is finished. */ -static GCObject **sweeplist (lua_State *L, GCObject **p, int countin) { +static GCObject **sweeplist (lua_State *L, GCObject **p, l_obj countin) { global_State *g = G(L); int ow = otherwhite(g); - int i; + l_obj i; int white = luaC_white(g); /* current white */ for (i = 0; *p != NULL && i < countin; i++) { GCObject *curr = *p; @@ -1357,8 +1358,8 @@ static void setminordebt (global_State *g) { ** collection. */ static void entergen (lua_State *L, global_State *g) { - luaC_runtilstate(L, bitmask(GCSpause)); /* prepare to start a new cycle */ - luaC_runtilstate(L, bitmask(GCSpropagate)); /* start new cycle */ + luaC_runtilstate(L, GCSpause, 1); /* prepare to start a new cycle */ + luaC_runtilstate(L, GCSpropagate, 1); /* start new cycle */ atomic(L); /* propagates all and then do the atomic stuff */ atomic2gen(L, g); setminordebt(g); /* set debt assuming next cycle will be minor */ @@ -1515,10 +1516,14 @@ static l_obj atomic (lua_State *L) { } +/* +** Do a sweep step. The normal case (not fast) sweeps at most GCSWEEPMAX +** elements. The fast case sweeps the whole list. +*/ static void sweepstep (lua_State *L, global_State *g, - int nextstate, GCObject **nextlist) { + int nextstate, GCObject **nextlist, int fast) { if (g->sweepgc) - g->sweepgc = sweeplist(L, g->sweepgc, GCSWEEPMAX); + g->sweepgc = sweeplist(L, g->sweepgc, fast ? MAX_LOBJ : GCSWEEPMAX); else { /* enter next state */ g->gcstate = nextstate; g->sweepgc = nextlist; @@ -1526,7 +1531,19 @@ static void sweepstep (lua_State *L, global_State *g, } -static l_obj singlestep (lua_State *L) { +/* +** Performs one incremental "step" in an incremental garbage collection. +** For indivisible work, a step goes to the next state. When marking +** (propagating), a step traverses one object. When sweeping, a step +** sweeps GCSWEEPMAX objects, to avoid a big overhead for sweeping +** objects one by one. (Sweeping is inexpensive, no matter the +** object.) When 'fast' is true, 'singlestep' tries to finish a state +** "as fast as possible". In particular, it skips the propagation +** phase and leaves all objects to be traversed by the atomic phase: +** That avoids traversing twice some objects, such as theads and +** weak tables. +*/ +static l_obj singlestep (lua_State *L, int fast) { global_State *g = G(L); l_obj work; lua_assert(!g->gcstopem); /* collector is not reentrant */ @@ -1539,7 +1556,7 @@ static l_obj singlestep (lua_State *L) { break; } case GCSpropagate: { - if (g->gray == NULL) { /* no more gray objects? */ + if (fast || g->gray == NULL) { g->gcstate = GCSenteratomic; /* finish propagate phase */ work = 0; } @@ -1556,17 +1573,17 @@ static l_obj singlestep (lua_State *L) { break; } case GCSswpallgc: { /* sweep "regular" objects */ - sweepstep(L, g, GCSswpfinobj, &g->finobj); + sweepstep(L, g, GCSswpfinobj, &g->finobj, fast); work = GCSWEEPMAX; break; } case GCSswpfinobj: { /* sweep objects with finalizers */ - sweepstep(L, g, GCSswptobefnz, &g->tobefnz); + sweepstep(L, g, GCSswptobefnz, &g->tobefnz, fast); work = GCSWEEPMAX; break; } case GCSswptobefnz: { /* sweep objects to be finalized */ - sweepstep(L, g, GCSswpend, NULL); + sweepstep(L, g, GCSswpend, NULL, fast); work = GCSWEEPMAX; break; } @@ -1596,14 +1613,15 @@ static l_obj singlestep (lua_State *L) { /* -** advances the garbage collector until it reaches a state allowed -** by 'statemask' +** Advances the garbage collector until it reaches the given state. +** (The option 'fast' is only for testing; in normal code, 'fast' +** here is always true.) */ -void luaC_runtilstate (lua_State *L, int statesmask) { +void luaC_runtilstate (lua_State *L, int state, int fast) { global_State *g = G(L); lua_assert(g->gckind == KGC_INC); - while (!testbit(statesmask, g->gcstate)) - singlestep(L); + while (state != g->gcstate) + singlestep(L, fast); } @@ -1618,8 +1636,13 @@ void luaC_runtilstate (lua_State *L, int statesmask) { static void incstep (lua_State *L, global_State *g) { l_obj stepsize = cast(l_obj, 1) << g->gcstepsize; l_obj work2do = applygcparam(g, gcstepmul, stepsize); - do { /* repeat until pause or enough "credit" (negative debt) */ - l_obj work = singlestep(L); /* perform one single step */ + int fast = 0; + if (work2do == 0) { /* special case: do a full collection */ + work2do = MAX_LOBJ; /* do unlimited work */ + fast = 1; + } + do { /* repeat until pause or enough work */ + l_obj work = singlestep(L, fast); /* perform one single step */ if (g->gckind == KGC_GENMINOR) /* returned to minor collections? */ return; /* nothing else to be done here */ work2do -= work; @@ -1665,13 +1688,11 @@ static void fullinc (lua_State *L, global_State *g) { if (keepinvariant(g)) /* black objects? */ entersweep(L); /* sweep everything to turn them back to white */ /* finish any pending sweep phase to start a new cycle */ - luaC_runtilstate(L, bitmask(GCSpause)); - luaC_runtilstate(L, bitmask(GCSpropagate)); /* start new cycle */ - g->gcstate = GCSenteratomic; /* go straight to atomic phase ??? */ - luaC_runtilstate(L, bitmask(GCScallfin)); /* run up to finalizers */ + luaC_runtilstate(L, GCSpause, 1); + luaC_runtilstate(L, GCScallfin, 1); /* run up to finalizers */ /* 'marked' must be correct after a full GC cycle */ lua_assert(g->marked == gettotalobjs(g)); - luaC_runtilstate(L, bitmask(GCSpause)); /* finish collection */ + luaC_runtilstate(L, GCSpause, 1); /* finish collection */ setpause(g); } diff --git a/lgc.h b/lgc.h index 4cbc6e61ff..e8dee8a072 100644 --- a/lgc.h +++ b/lgc.h @@ -182,12 +182,14 @@ /* incremental */ -/* wait memory to double before starting new cycle */ -#define LUAI_GCPAUSE 200 +/* Number of objects must be LUAI_GCPAUSE% before starting new cycle */ +#define LUAI_GCPAUSE 300 -#define LUAI_GCMUL 300 /* step multiplier */ +/* Step multiplier. (Roughly, the collector handles LUAI_GCMUL% objects + for each new allocated object.) */ +#define LUAI_GCMUL 200 -/* how many objects to allocate before next GC step (log2) */ +/* How many objects to allocate before next GC step (log2) */ #define LUAI_GCSTEPSIZE 8 /* 256 objects */ @@ -244,7 +246,7 @@ LUAI_FUNC void luaC_fix (lua_State *L, GCObject *o); LUAI_FUNC void luaC_freeallobjects (lua_State *L); LUAI_FUNC void luaC_step (lua_State *L); -LUAI_FUNC void luaC_runtilstate (lua_State *L, int statesmask); +LUAI_FUNC void luaC_runtilstate (lua_State *L, int state, int fast); LUAI_FUNC void luaC_fullgc (lua_State *L, int isemergency); LUAI_FUNC GCObject *luaC_newobj (lua_State *L, int tt, size_t sz); LUAI_FUNC GCObject *luaC_newobjdt (lua_State *L, int tt, size_t sz, diff --git a/llimits.h b/llimits.h index 47dee9a687..3bcd1b7f36 100644 --- a/llimits.h +++ b/llimits.h @@ -33,7 +33,8 @@ typedef unsigned long lu_mem; typedef long l_obj; #endif /* } */ -#define MAX_LOBJ cast(l_obj, ~cast(lu_mem, 0) >> 1) +#define MAX_LOBJ \ + cast(l_obj, (cast(lu_mem, 1) << (sizeof(l_obj) * CHAR_BIT - 1)) - 1) /* chars used as small naturals (so that 'char' is reserved for characters) */ @@ -44,7 +45,10 @@ typedef signed char ls_byte; /* maximum value for size_t */ #define MAX_SIZET ((size_t)(~(size_t)0)) -/* maximum size visible for Lua (must be representable in a lua_Integer) */ +/* +** Maximum size for strings and userdata visible for Lua (should be +** representable in a lua_Integer) +*/ #define MAX_SIZE (sizeof(size_t) < sizeof(lua_Integer) ? MAX_SIZET \ : (size_t)(LUA_MAXINTEGER)) diff --git a/ltests.c b/ltests.c index 9bc2f0da23..51e4ff9b41 100644 --- a/ltests.c +++ b/ltests.c @@ -941,10 +941,10 @@ static int gc_printobj (lua_State *L) { static int gc_state (lua_State *L) { static const char *statenames[] = { - "propagate", "atomic", "enteratomic", "sweepallgc", "sweepfinobj", + "propagate", "atomic", "sweepallgc", "sweepfinobj", "sweeptobefnz", "sweepend", "callfin", "pause", ""}; static const int states[] = { - GCSpropagate, GCSenteratomic, GCSatomic, GCSswpallgc, GCSswpfinobj, + GCSpropagate, GCSenteratomic, GCSswpallgc, GCSswpfinobj, GCSswptobefnz, GCSswpend, GCScallfin, GCSpause, -1}; int option = states[luaL_checkoption(L, 1, "", statenames)]; if (option == -1) { @@ -957,9 +957,9 @@ static int gc_state (lua_State *L) { luaL_error(L, "cannot change states in generational mode"); lua_lock(L); if (option < g->gcstate) { /* must cross 'pause'? */ - luaC_runtilstate(L, bitmask(GCSpause)); /* run until pause */ + luaC_runtilstate(L, GCSpause, 1); /* run until pause */ } - luaC_runtilstate(L, bitmask(option)); + luaC_runtilstate(L, option, 0); /* do not skip propagation state */ lua_assert(G(L)->gcstate == option); lua_unlock(L); return 0; diff --git a/manual/manual.of b/manual/manual.of index ef1bdfd2b4..e6a3cd9e79 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -664,7 +664,7 @@ Values equal to or less than 100 mean the collector will not wait to start a new cycle. A value of 200 means that the collector waits for the total number of objects to double before starting a new cycle. -The default value is 200; the maximum value is 1000. +The default value is 300; the maximum value is 1000. The garbage-collector step multiplier controls the speed of the collector relative to @@ -674,7 +674,9 @@ how many objects it marks or sweeps for each object created. Larger values make the collector more aggressive. Beware that values too small can make the collector too slow to ever finish a cycle. -The default value is 300; the maximum value is 1000. +The default value is 200; the maximum value is 1000. +As a special case, a zero value means unlimited work, +effectively producing a non-incremental, stop-the-world collector. The garbage-collector step size controls the size of each incremental step, @@ -682,9 +684,7 @@ specifically how many objects the interpreter creates before performing a step. This parameter is logarithmic: A value of @M{n} means the interpreter will create @M{2@sp{n}} -objects between steps and perform equivalent work during the step. -A large value (e.g., 60) makes the collector a stop-the-world -(non-incremental) collector. +objects between steps. The default value is 8, which means steps of approximately @N{256 objects}. @@ -3306,43 +3306,43 @@ For options that need extra arguments, they are listed after the option. @description{ -@item{@id{LUA_GCCOLLECT}| +@item{@defid{LUA_GCCOLLECT}| Performs a full garbage-collection cycle. } -@item{@id{LUA_GCSTOP}| +@item{@defid{LUA_GCSTOP}| Stops the garbage collector. } -@item{@id{LUA_GCRESTART}| +@item{@defid{LUA_GCRESTART}| Restarts the garbage collector. } -@item{@id{LUA_GCCOUNT}| +@item{@defid{LUA_GCCOUNT}| Returns the current amount of memory (in Kbytes) in use by Lua. } -@item{@id{LUA_GCCOUNTB}| +@item{@defid{LUA_GCCOUNTB}| Returns the remainder of dividing the current amount of bytes of memory in use by Lua by 1024. } -@item{@id{LUA_GCSTEP}| +@item{@defid{LUA_GCSTEP}| Performs a step of garbage collection. } -@item{@id{LUA_GCISRUNNING}| +@item{@defid{LUA_GCISRUNNING}| Returns a boolean that tells whether the collector is running (i.e., not stopped). } -@item{@id{LUA_GCINC} (int pause, int stepmul, int stepsize)| +@item{@defid{LUA_GCINC} (int pause, int stepmul, int stepsize)| Changes the collector to incremental mode with the given parameters @see{incmode}. Returns the previous mode (@id{LUA_GCGEN} or @id{LUA_GCINC}). } -@item{@id{LUA_GCGEN} (int minormul, int minormajor, int majorminor)| +@item{@defid{LUA_GCGEN} (int minormul, int minormajor, int majorminor)| Changes the collector to generational mode with the given parameters @see{genmode}. Returns the previous mode (@id{LUA_GCGEN} or @id{LUA_GCINC}). diff --git a/testes/gengc.lua b/testes/gengc.lua index 3d4f67f8bc..d708d7fcc3 100644 --- a/testes/gengc.lua +++ b/testes/gengc.lua @@ -162,9 +162,16 @@ end assert(collectgarbage'isrunning') +do print"testing stop-the-world collection" + collectgarbage("incremental", nil, 0) --- just to make sure -assert(collectgarbage'isrunning') + -- each step does a complete cycle + assert(collectgarbage("step")) + assert(collectgarbage("step")) + + -- back to default value + collectgarbage("incremental", nil, 200) +end collectgarbage(oldmode) From ad0ea7813b39e76b377983138ca995189e22054f Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 20 Dec 2023 16:25:20 -0300 Subject: [PATCH 0868/1145] GC parameters encoded as floating-point bytes This encoding brings more precision and a larger range for these parameters. --- lapi.c | 19 ++++++------------- lgc.c | 18 +++++++++--------- lgc.h | 20 ++++---------------- lobject.c | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++++- lobject.h | 3 +++ lstate.c | 12 ++++++------ lstate.h | 12 ++++++------ ltests.c | 33 +++++++++++++++++++++++++++------ 8 files changed, 113 insertions(+), 57 deletions(-) diff --git a/lapi.c b/lapi.c index fd9ec4e4c0..3ea3d0aa6b 100644 --- a/lapi.c +++ b/lapi.c @@ -1190,12 +1190,9 @@ LUA_API int lua_gc (lua_State *L, int what, ...) { int minormajor = va_arg(argp, int); int majorminor = va_arg(argp, int); res = (g->gckind == KGC_INC) ? LUA_GCINC : LUA_GCGEN; - if (minormul >= 0) - setgcparam(g, genminormul, minormul); - if (minormajor >= 0) - setgcparam(g, minormajor, minormajor); - if (majorminor >= 0) - setgcparam(g, majorminor, majorminor); + setgcparam(g, gcpgenminormul, minormul); + setgcparam(g, gcpminormajor, minormajor); + setgcparam(g, gcpmajorminor, majorminor); luaC_changemode(L, KGC_GENMINOR); break; } @@ -1204,13 +1201,9 @@ LUA_API int lua_gc (lua_State *L, int what, ...) { int stepmul = va_arg(argp, int); int stepsize = va_arg(argp, int); res = (g->gckind == KGC_INC) ? LUA_GCINC : LUA_GCGEN; - if (pause >= 0) - setgcparam(g, gcpause, pause); - if (stepmul >= 0) - setgcparam(g, gcstepmul, stepmul); - if (stepsize >= 0) - g->gcstepsize = (stepsize <= log2maxs(l_obj)) ? stepsize - : log2maxs(l_obj); + setgcparam(g, gcppause, pause); + setgcparam(g, gcpstepmul, stepmul); + setgcparam(g, gcpstepsize, stepsize); luaC_changemode(L, KGC_INC); break; } diff --git a/lgc.c b/lgc.c index 114b32d330..149dddf61f 100644 --- a/lgc.c +++ b/lgc.c @@ -1049,7 +1049,7 @@ void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt) { ** approximately (marked * pause / 100). */ static void setpause (global_State *g) { - l_obj threshold = applygcparam(g, gcpause, g->marked); + l_obj threshold = luaO_applyparam(g->gcppause, g->marked); l_obj debt = threshold - gettotalobjs(g); if (debt < 0) debt = 0; luaE_setdebt(g, debt); @@ -1233,13 +1233,13 @@ static void finishgencycle (lua_State *L, global_State *g) { ** in generational mode. */ static void minor2inc (lua_State *L, global_State *g, int kind) { - l_obj stepsize = cast(l_obj, 1) << g->gcstepsize; g->GCmajorminor = g->marked; /* number of live objects */ g->gckind = kind; g->reallyold = g->old1 = g->survival = NULL; g->finobjrold = g->finobjold1 = g->finobjsur = NULL; entersweep(L); /* continue as an incremental cycle */ - luaE_setdebt(g, stepsize); + /* set a debt equal to the step size */ + luaE_setdebt(g, luaO_applyparam(g->gcpstepsize, 100)); } @@ -1255,8 +1255,8 @@ static void minor2inc (lua_State *L, global_State *g, int kind) { ** major collection. (That percentage is computed in 'limit'.) */ static int checkminormajor (lua_State *L, global_State *g, l_obj addedold1) { - l_obj step = applygcparam(g, genminormul, g->GCmajorminor); - l_obj limit = applygcparam(g, minormajor, g->GCmajorminor); + l_obj step = luaO_applyparam(g->gcpgenminormul, g->GCmajorminor); + l_obj limit = luaO_applyparam(g->gcpminormajor, g->GCmajorminor); //printf("-> major? %ld %ld %ld %ld (%ld)\n", g->marked, limit, step, addedold1, gettotalobjs(g)); if (addedold1 >= (step >> 1) || g->marked >= limit) { minor2inc(L, g, KGC_GENMAJOR); /* go to major mode */ @@ -1347,7 +1347,7 @@ static void atomic2gen (lua_State *L, global_State *g) { ** total number of objects grows 'genminormul'%. */ static void setminordebt (global_State *g) { - luaE_setdebt(g, applygcparam(g, genminormul, g->GCmajorminor)); + luaE_setdebt(g, luaO_applyparam(g->gcpgenminormul, g->GCmajorminor)); } @@ -1404,7 +1404,7 @@ static int checkmajorminor (lua_State *L, global_State *g) { if (g->gckind == KGC_GENMAJOR) { l_obj numobjs = gettotalobjs(g); l_obj addedobjs = numobjs - g->GCmajorminor; - l_obj limit = applygcparam(g, majorminor, addedobjs); + l_obj limit = luaO_applyparam(g->gcpmajorminor, addedobjs); l_obj tobecollected = numobjs - g->marked; //printf("-> minor? %ld %ld %ld\n", tobecollected, limit, numobjs); if (tobecollected > limit) { @@ -1634,8 +1634,8 @@ void luaC_runtilstate (lua_State *L, int state, int fast) { ** controls when next step will be performed. */ static void incstep (lua_State *L, global_State *g) { - l_obj stepsize = cast(l_obj, 1) << g->gcstepsize; - l_obj work2do = applygcparam(g, gcstepmul, stepsize); + l_obj stepsize = luaO_applyparam(g->gcpstepsize, 100); + l_obj work2do = luaO_applyparam(g->gcpstepmul, stepsize); int fast = 0; if (work2do == 0) { /* special case: do a full collection */ work2do = MAX_LOBJ; /* do unlimited work */ diff --git a/lgc.h b/lgc.h index e8dee8a072..6a03f78729 100644 --- a/lgc.h +++ b/lgc.h @@ -189,10 +189,12 @@ for each new allocated object.) */ #define LUAI_GCMUL 200 -/* How many objects to allocate before next GC step (log2) */ -#define LUAI_GCSTEPSIZE 8 /* 256 objects */ +/* How many objects to allocate before next GC step */ +#define LUAI_GCSTEPSIZE 250 +#define setgcparam(g,p,v) if ((v) >= 0) {g->p = luaO_codeparam(v);} + /* ** Control when GC is running: */ @@ -201,20 +203,6 @@ #define GCSTPCLS 4 /* bit true when closing Lua state */ #define gcrunning(g) ((g)->gcstp == 0) -/* -** Macros to set and apply GC parameters. GC parameters are given in -** percentage points, but are stored as lu_byte. To avoid repeated -** divisions by 100, these macros store the original parameter -** multiplied by 128 and divided by 100. To apply them, if it first -** divides the value by 128 it may lose precision; if it first -** multiplies by the parameter, it may overflow. So, it first divides -** by 32, then multiply by the parameter, and then divides the result by -** 4. -*/ - -#define setgcparam(g,p,v) (g->gcp##p = (cast_uint(v) << 7) / 100u) -#define applygcparam(g,p,v) ((((v) >> 5) * g->gcp##p) >> 2) - /* ** Does one step of collection when debt becomes zero. 'pre'/'pos' diff --git a/lobject.c b/lobject.c index 9cfa5227eb..4091b9d79b 100644 --- a/lobject.c +++ b/lobject.c @@ -33,7 +33,7 @@ ** Computes ceil(log2(x)) */ int luaO_ceillog2 (unsigned int x) { - static const lu_byte log_2[256] = { /* log_2[i] = ceil(log2(i - 1)) */ + static const lu_byte log_2[256] = { /* log_2[i - 1] = ceil(log2(i)) */ 0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5, 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, @@ -49,6 +49,57 @@ int luaO_ceillog2 (unsigned int x) { return l + log_2[x]; } +/* +** Encodes 'p'% as a floating-point byte, represented as (eeeeexxx). +** The exponent is represented using excess-7. Mimicking IEEE 754, the +** representation normalizes the number when possible, assuming an extra +** 1 before the mantissa (xxx) and adding one to the exponent (eeeeexxx) +** to signal that. So, the real value is (1xxx) * 2^(eeeee - 8) if +** eeeee != 0, and (xxx) * 2^-7 otherwise. +*/ +unsigned int luaO_codeparam (unsigned int p) { + if (p >= (cast(lu_mem, 0xF) << 0xF) / 128 * 100) /* overflow? */ + return 0xFF; /* return maximum value */ + else { + p = (p * 128u) / 100; + if (p <= 0xF) + return p; + else { + int log = luaO_ceillog2(p + 1) - 5; + return ((p >> log) - 0x10) | ((log + 1) << 4); + } + } +} + + +/* +** Computes 'p' times 'x', where 'p' is a floating-point byte. +*/ +l_obj luaO_applyparam (unsigned int p, l_obj x) { + unsigned int m = p & 0xF; /* mantissa */ + int e = (p >> 4); /* exponent */ + if (e > 0) { /* normalized? */ + e--; + m += 0x10; /* maximum 'm' is 0x1F */ + } + e -= 7; /* correct excess-7 */ + if (e < 0) { + e = -e; + if (x < MAX_LOBJ / 0x1F) /* multiplication cannot overflow? */ + return (x * m) >> e; /* multiplying first gives more precision */ + else if ((x >> e) < MAX_LOBJ / 0x1F) /* cannot overflow after shift? */ + return (x >> e) * m; + else /* real overflow */ + return MAX_LOBJ; + } + else { + if (x < (MAX_LOBJ / 0x1F) >> e) /* no overflow? */ + return (x * m) << e; /* order doesn't matter here */ + else /* real overflow */ + return MAX_LOBJ; + } +} + static lua_Integer intarith (lua_State *L, int op, lua_Integer v1, lua_Integer v2) { diff --git a/lobject.h b/lobject.h index 6da502156f..a7d85762de 100644 --- a/lobject.h +++ b/lobject.h @@ -826,6 +826,9 @@ typedef struct Table { LUAI_FUNC int luaO_utf8esc (char *buff, unsigned long x); LUAI_FUNC int luaO_ceillog2 (unsigned int x); +LUAI_FUNC unsigned int luaO_codeparam (unsigned int p); +LUAI_FUNC l_obj luaO_applyparam (unsigned int p, l_obj x); + LUAI_FUNC int luaO_rawarith (lua_State *L, int op, const TValue *p1, const TValue *p2, TValue *res); LUAI_FUNC void luaO_arith (lua_State *L, int op, const TValue *p1, diff --git a/lstate.c b/lstate.c index aab90e34b6..1950584553 100644 --- a/lstate.c +++ b/lstate.c @@ -365,12 +365,12 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud, unsigned int seed) { g->marked = 0; g->GCdebt = 0; setivalue(&g->nilvalue, 0); /* to signal that state is not yet built */ - setgcparam(g, gcpause, LUAI_GCPAUSE); - setgcparam(g, gcstepmul, LUAI_GCMUL); - g->gcstepsize = LUAI_GCSTEPSIZE; - setgcparam(g, genminormul, LUAI_GENMINORMUL); - setgcparam(g, minormajor, LUAI_MINORMAJOR); - setgcparam(g, majorminor, LUAI_MAJORMINOR); + setgcparam(g, gcppause, LUAI_GCPAUSE); + setgcparam(g, gcpstepmul, LUAI_GCMUL); + setgcparam(g, gcpstepsize, LUAI_GCSTEPSIZE); + setgcparam(g, gcpgenminormul, LUAI_GENMINORMUL); + setgcparam(g, gcpminormajor, LUAI_MINORMAJOR); + setgcparam(g, gcpmajorminor, LUAI_MAJORMINOR); for (i=0; i < LUA_NUMTAGS; i++) g->mt[i] = NULL; if (luaD_rawrunprotected(L, f_luaopen, NULL) != LUA_OK) { /* memory allocation error: free partial state */ diff --git a/lstate.h b/lstate.h index 49acfb7e31..5e2020e759 100644 --- a/lstate.h +++ b/lstate.h @@ -264,18 +264,18 @@ typedef struct global_State { TValue l_registry; TValue nilvalue; /* a nil value */ unsigned int seed; /* randomized seed for hashes */ - unsigned short gcpgenminormul; /* control minor generational collections */ - unsigned short gcpmajorminor; /* control shift major->minor */ - unsigned short gcpminormajor; /* control shift minor->major */ - unsigned short gcpgcpause; /* size of pause between successive GCs */ - unsigned short gcpgcstepmul; /* GC "speed" */ + lu_byte gcpgenminormul; /* control minor generational collections */ + lu_byte gcpmajorminor; /* control shift major->minor */ + lu_byte gcpminormajor; /* control shift minor->major */ + lu_byte gcppause; /* size of pause between successive GCs */ + lu_byte gcpstepmul; /* GC "speed" */ + lu_byte gcpstepsize; /* GC granularity */ lu_byte currentwhite; lu_byte gcstate; /* state of garbage collector */ lu_byte gckind; /* kind of GC running */ lu_byte gcstopem; /* stops emergency collections */ lu_byte gcstp; /* control whether GC is running */ lu_byte gcemergency; /* true if this is an emergency collection */ - lu_byte gcstepsize; /* (log2 of) GC granularity */ GCObject *allgc; /* list of all collectable objects */ GCObject **sweepgc; /* current position of sweep in list */ GCObject *finobj; /* list of collectable objects with finalizers */ diff --git a/ltests.c b/ltests.c index 51e4ff9b41..93af528eeb 100644 --- a/ltests.c +++ b/ltests.c @@ -1033,16 +1033,35 @@ static int table_query (lua_State *L) { } -static int query_inc (lua_State *L) { +static int query_GCparams (lua_State *L) { global_State *g = G(L); lua_pushinteger(L, gettotalobjs(g)); lua_pushinteger(L, g->GCdebt); - lua_pushinteger(L, applygcparam(g, gcpause, 100)); - lua_pushinteger(L, applygcparam(g, gcstepmul, 100)); - lua_pushinteger(L, cast(l_obj, 1) << g->gcstepsize); - return 5; + lua_pushinteger(L, luaO_applyparam(g->gcpgenminormul, 100)); + lua_pushinteger(L, luaO_applyparam(g->gcpmajorminor, 100)); + lua_pushinteger(L, luaO_applyparam(g->gcpminormajor, 100)); + lua_pushinteger(L, luaO_applyparam(g->gcppause, 100)); + lua_pushinteger(L, luaO_applyparam(g->gcpstepmul, 100)); + lua_pushinteger(L, luaO_applyparam(g->gcpstepsize, 100)); + return 8; +} + + +static int test_codeparam (lua_State *L) { + lua_Integer p = luaL_checkinteger(L, 1); + lua_pushinteger(L, luaO_codeparam(p)); + return 1; } + +static int test_applyparam (lua_State *L) { + lua_Integer p = luaL_checkinteger(L, 1); + lua_Integer x = luaL_checkinteger(L, 2); + lua_pushinteger(L, luaO_applyparam(p, x)); + return 1; +} + + static int string_query (lua_State *L) { stringtable *tb = &G(L)->strt; int s = cast_int(luaL_optinteger(L, 1, 0)) - 1; @@ -1974,7 +1993,9 @@ static const struct luaL_Reg tests_funcs[] = { {"pushuserdata", pushuserdata}, {"querystr", string_query}, {"querytab", table_query}, - {"queryinc", query_inc}, + {"queryGCparams", query_GCparams}, + {"codeparam", test_codeparam}, + {"applyparam", test_applyparam}, {"ref", tref}, {"resume", coresume}, {"s2d", s2d}, From 5853c37a83ec66ccb45094f9aeac23dfdbcde671 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 21 Dec 2023 13:37:51 -0300 Subject: [PATCH 0869/1145] Bug: Buffer overflow in string concatenation Even if the string fits in size_t, the whole size of the TString object can overflow when we add the header. --- lstring.c | 2 +- lvm.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lstring.c b/lstring.c index e921dd0f34..97757355c0 100644 --- a/lstring.c +++ b/lstring.c @@ -224,7 +224,7 @@ TString *luaS_newlstr (lua_State *L, const char *str, size_t l) { return internshrstr(L, str, l); else { TString *ts; - if (l_unlikely(l >= (MAX_SIZE - sizeof(TString))/sizeof(char))) + if (l_unlikely(l * sizeof(char) >= (MAX_SIZE - sizeof(TString)))) luaM_toobig(L); ts = luaS_createlngstrobj(L, l); memcpy(getlngstr(ts), str, l * sizeof(char)); diff --git a/lvm.c b/lvm.c index 4d71cfffd0..918ae64ca4 100644 --- a/lvm.c +++ b/lvm.c @@ -661,7 +661,7 @@ void luaV_concat (lua_State *L, int total) { /* collect total length and number of strings */ for (n = 1; n < total && tostring(L, s2v(top - n - 1)); n++) { size_t l = tsslen(tsvalue(s2v(top - n - 1))); - if (l_unlikely(l >= (MAX_SIZE/sizeof(char)) - tl)) { + if (l_unlikely(l >= MAX_SIZE - sizeof(TString) - tl)) { L->top.p = top - total; /* pop strings to avoid wasting stack */ luaG_runerror(L, "string length overflow"); } From e2cc179454c6aa6cde5f98954bd3783e0d5d53a3 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 22 Dec 2023 14:48:07 -0300 Subject: [PATCH 0870/1145] New option "setparms" for 'collectgarbage' The generational mode also uses the parameters for the incremental mode in its major collections, so it should be easy to change those parameters without having to change the GC mode. --- lapi.c | 24 +++++++++---- lbaselib.c | 34 +++++++++++++++--- lgc.c | 16 ++++----- lgc.h | 3 +- lstate.c | 12 +++---- lstate.h | 7 +--- ltests.c | 12 +++---- ltests.h | 2 ++ lua.c | 2 +- lua.h | 21 ++++++++++- manual/manual.of | 92 ++++++++++++++++++++++++++++++++---------------- testes/gc.lua | 11 +++--- testes/gengc.lua | 5 +-- 13 files changed, 163 insertions(+), 78 deletions(-) diff --git a/lapi.c b/lapi.c index 3ea3d0aa6b..aa2ee73572 100644 --- a/lapi.c +++ b/lapi.c @@ -1186,27 +1186,37 @@ LUA_API int lua_gc (lua_State *L, int what, ...) { break; } case LUA_GCGEN: { +#if defined(LUA_COMPAT_GCPARAMS) int minormul = va_arg(argp, int); int minormajor = va_arg(argp, int); - int majorminor = va_arg(argp, int); + if (minormul > 0) setgcparam(g, MINORMUL, minormul); + if (minormajor > 0) setgcparam(g, MINORMAJOR, minormajor); +#endif res = (g->gckind == KGC_INC) ? LUA_GCINC : LUA_GCGEN; - setgcparam(g, gcpgenminormul, minormul); - setgcparam(g, gcpminormajor, minormajor); - setgcparam(g, gcpmajorminor, majorminor); luaC_changemode(L, KGC_GENMINOR); break; } case LUA_GCINC: { +#if defined(LUA_COMPAT_GCPARAMS) int pause = va_arg(argp, int); int stepmul = va_arg(argp, int); int stepsize = va_arg(argp, int); + if (pause > 0) setgcparam(g, PAUSE, pause); + if (stepmul > 0) setgcparam(g, STEPMUL, stepmul); + if (stepsize > 0) setgcparam(g, STEPSIZE, 1u << stepsize); +#endif res = (g->gckind == KGC_INC) ? LUA_GCINC : LUA_GCGEN; - setgcparam(g, gcppause, pause); - setgcparam(g, gcpstepmul, stepmul); - setgcparam(g, gcpstepsize, stepsize); luaC_changemode(L, KGC_INC); break; } + case LUA_GCSETPARAM: { + int param = va_arg(argp, int); + int value = va_arg(argp, int); + api_check(L, 0 <= param && param < LUA_GCPN, "invalid parameter"); + res = luaO_applyparam(g->gcparams[param], 100); + g->gcparams[param] = luaO_codeparam(value); + break; + } default: res = -1; /* invalid option */ } va_end(argp); diff --git a/lbaselib.c b/lbaselib.c index 9ad84dcfd6..03df57f8ca 100644 --- a/lbaselib.c +++ b/lbaselib.c @@ -198,9 +198,11 @@ static int pushmode (lua_State *L, int oldmode) { static int luaB_collectgarbage (lua_State *L) { static const char *const opts[] = {"stop", "restart", "collect", - "count", "step", "isrunning", "generational", "incremental", NULL}; - static const int optsnum[] = {LUA_GCSTOP, LUA_GCRESTART, LUA_GCCOLLECT, - LUA_GCCOUNT, LUA_GCSTEP, LUA_GCISRUNNING, LUA_GCGEN, LUA_GCINC}; + "count", "step", "isrunning", "generational", "incremental", + "setparam", NULL}; + static const char optsnum[] = {LUA_GCSTOP, LUA_GCRESTART, LUA_GCCOLLECT, + LUA_GCCOUNT, LUA_GCSTEP, LUA_GCISRUNNING, LUA_GCGEN, LUA_GCINC, + LUA_GCSETPARAM}; int o = optsnum[luaL_checkoption(L, 1, "collect", opts)]; switch (o) { case LUA_GCCOUNT: { @@ -224,17 +226,39 @@ static int luaB_collectgarbage (lua_State *L) { return 1; } case LUA_GCGEN: { +#if defined(LUA_COMPAT_GCPARAMS) int minormul = (int)luaL_optinteger(L, 2, -1); int majorminor = (int)luaL_optinteger(L, 3, -1); - int minormajor = (int)luaL_optinteger(L, 4, -1); - return pushmode(L, lua_gc(L, o, minormul, majorminor, minormajor)); +#else + int minormul = 0; + int majorminor = 0; +#endif + return pushmode(L, lua_gc(L, o, minormul, majorminor)); } case LUA_GCINC: { +#if defined(LUA_COMPAT_GCPARAMS) int pause = (int)luaL_optinteger(L, 2, -1); int stepmul = (int)luaL_optinteger(L, 3, -1); int stepsize = (int)luaL_optinteger(L, 4, -1); +#else + int pause = 0; + int stepmul = 0; + int stepsize = 0; +#endif return pushmode(L, lua_gc(L, o, pause, stepmul, stepsize)); } + case LUA_GCSETPARAM: { + static const char *const params[] = { + "minormul", "majorminor", "minormajor", + "pause", "stepmul", "stepsize", NULL}; + static const char pnum[] = { + LUA_GCPMINORMUL, LUA_GCPMAJORMINOR, LUA_GCPMINORMAJOR, + LUA_GCPPAUSE, LUA_GCPSTEPMUL, LUA_GCPSTEPSIZE}; + int p = pnum[luaL_checkoption(L, 2, NULL, params)]; + lua_Integer value = luaL_checkinteger(L, 3); + lua_pushinteger(L, lua_gc(L, o, p, value)); + return 1; + } default: { int res = lua_gc(L, o); checkvalres(res); diff --git a/lgc.c b/lgc.c index 149dddf61f..bc4ddb0b2f 100644 --- a/lgc.c +++ b/lgc.c @@ -1049,7 +1049,7 @@ void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt) { ** approximately (marked * pause / 100). */ static void setpause (global_State *g) { - l_obj threshold = luaO_applyparam(g->gcppause, g->marked); + l_obj threshold = applygcparam(g, PAUSE, g->marked); l_obj debt = threshold - gettotalobjs(g); if (debt < 0) debt = 0; luaE_setdebt(g, debt); @@ -1239,7 +1239,7 @@ static void minor2inc (lua_State *L, global_State *g, int kind) { g->finobjrold = g->finobjold1 = g->finobjsur = NULL; entersweep(L); /* continue as an incremental cycle */ /* set a debt equal to the step size */ - luaE_setdebt(g, luaO_applyparam(g->gcpstepsize, 100)); + luaE_setdebt(g, applygcparam(g, STEPSIZE, 100)); } @@ -1255,8 +1255,8 @@ static void minor2inc (lua_State *L, global_State *g, int kind) { ** major collection. (That percentage is computed in 'limit'.) */ static int checkminormajor (lua_State *L, global_State *g, l_obj addedold1) { - l_obj step = luaO_applyparam(g->gcpgenminormul, g->GCmajorminor); - l_obj limit = luaO_applyparam(g->gcpminormajor, g->GCmajorminor); + l_obj step = applygcparam(g, MINORMUL, g->GCmajorminor); + l_obj limit = applygcparam(g, MINORMAJOR, g->GCmajorminor); //printf("-> major? %ld %ld %ld %ld (%ld)\n", g->marked, limit, step, addedold1, gettotalobjs(g)); if (addedold1 >= (step >> 1) || g->marked >= limit) { minor2inc(L, g, KGC_GENMAJOR); /* go to major mode */ @@ -1347,7 +1347,7 @@ static void atomic2gen (lua_State *L, global_State *g) { ** total number of objects grows 'genminormul'%. */ static void setminordebt (global_State *g) { - luaE_setdebt(g, luaO_applyparam(g->gcpgenminormul, g->GCmajorminor)); + luaE_setdebt(g, applygcparam(g, MINORMUL, g->GCmajorminor)); } @@ -1404,7 +1404,7 @@ static int checkmajorminor (lua_State *L, global_State *g) { if (g->gckind == KGC_GENMAJOR) { l_obj numobjs = gettotalobjs(g); l_obj addedobjs = numobjs - g->GCmajorminor; - l_obj limit = luaO_applyparam(g->gcpmajorminor, addedobjs); + l_obj limit = applygcparam(g, MAJORMINOR, addedobjs); l_obj tobecollected = numobjs - g->marked; //printf("-> minor? %ld %ld %ld\n", tobecollected, limit, numobjs); if (tobecollected > limit) { @@ -1634,8 +1634,8 @@ void luaC_runtilstate (lua_State *L, int state, int fast) { ** controls when next step will be performed. */ static void incstep (lua_State *L, global_State *g) { - l_obj stepsize = luaO_applyparam(g->gcpstepsize, 100); - l_obj work2do = luaO_applyparam(g->gcpstepmul, stepsize); + l_obj stepsize = applygcparam(g, STEPSIZE, 100); + l_obj work2do = applygcparam(g, STEPMUL, stepsize); int fast = 0; if (work2do == 0) { /* special case: do a full collection */ work2do = MAX_LOBJ; /* do unlimited work */ diff --git a/lgc.h b/lgc.h index 6a03f78729..9aff11f563 100644 --- a/lgc.h +++ b/lgc.h @@ -193,7 +193,8 @@ #define LUAI_GCSTEPSIZE 250 -#define setgcparam(g,p,v) if ((v) >= 0) {g->p = luaO_codeparam(v);} +#define setgcparam(g,p,v) (g->gcparams[LUA_GCP##p] = luaO_codeparam(v)) +#define applygcparam(g,p,x) luaO_applyparam(g->gcparams[LUA_GCP##p], x) /* ** Control when GC is running: diff --git a/lstate.c b/lstate.c index 1950584553..de02c91a3d 100644 --- a/lstate.c +++ b/lstate.c @@ -365,12 +365,12 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud, unsigned int seed) { g->marked = 0; g->GCdebt = 0; setivalue(&g->nilvalue, 0); /* to signal that state is not yet built */ - setgcparam(g, gcppause, LUAI_GCPAUSE); - setgcparam(g, gcpstepmul, LUAI_GCMUL); - setgcparam(g, gcpstepsize, LUAI_GCSTEPSIZE); - setgcparam(g, gcpgenminormul, LUAI_GENMINORMUL); - setgcparam(g, gcpminormajor, LUAI_MINORMAJOR); - setgcparam(g, gcpmajorminor, LUAI_MAJORMINOR); + setgcparam(g, PAUSE, LUAI_GCPAUSE); + setgcparam(g, STEPMUL, LUAI_GCMUL); + setgcparam(g, STEPSIZE, LUAI_GCSTEPSIZE); + setgcparam(g, MINORMUL, LUAI_GENMINORMUL); + setgcparam(g, MINORMAJOR, LUAI_MINORMAJOR); + setgcparam(g, MAJORMINOR, LUAI_MAJORMINOR); for (i=0; i < LUA_NUMTAGS; i++) g->mt[i] = NULL; if (luaD_rawrunprotected(L, f_luaopen, NULL) != LUA_OK) { /* memory allocation error: free partial state */ diff --git a/lstate.h b/lstate.h index 5e2020e759..7f5674531c 100644 --- a/lstate.h +++ b/lstate.h @@ -264,12 +264,7 @@ typedef struct global_State { TValue l_registry; TValue nilvalue; /* a nil value */ unsigned int seed; /* randomized seed for hashes */ - lu_byte gcpgenminormul; /* control minor generational collections */ - lu_byte gcpmajorminor; /* control shift major->minor */ - lu_byte gcpminormajor; /* control shift minor->major */ - lu_byte gcppause; /* size of pause between successive GCs */ - lu_byte gcpstepmul; /* GC "speed" */ - lu_byte gcpstepsize; /* GC granularity */ + lu_byte gcparams[LUA_GCPN]; lu_byte currentwhite; lu_byte gcstate; /* state of garbage collector */ lu_byte gckind; /* kind of GC running */ diff --git a/ltests.c b/ltests.c index 93af528eeb..cf9a8eaf17 100644 --- a/ltests.c +++ b/ltests.c @@ -1037,12 +1037,12 @@ static int query_GCparams (lua_State *L) { global_State *g = G(L); lua_pushinteger(L, gettotalobjs(g)); lua_pushinteger(L, g->GCdebt); - lua_pushinteger(L, luaO_applyparam(g->gcpgenminormul, 100)); - lua_pushinteger(L, luaO_applyparam(g->gcpmajorminor, 100)); - lua_pushinteger(L, luaO_applyparam(g->gcpminormajor, 100)); - lua_pushinteger(L, luaO_applyparam(g->gcppause, 100)); - lua_pushinteger(L, luaO_applyparam(g->gcpstepmul, 100)); - lua_pushinteger(L, luaO_applyparam(g->gcpstepsize, 100)); + lua_pushinteger(L, applygcparam(g, MINORMUL, 100)); + lua_pushinteger(L, applygcparam(g, MAJORMINOR, 100)); + lua_pushinteger(L, applygcparam(g, MINORMAJOR, 100)); + lua_pushinteger(L, applygcparam(g, PAUSE, 100)); + lua_pushinteger(L, applygcparam(g, STEPMUL, 100)); + lua_pushinteger(L, applygcparam(g, STEPSIZE, 100)); return 8; } diff --git a/ltests.h b/ltests.h index da773d6eed..70afa7a320 100644 --- a/ltests.h +++ b/ltests.h @@ -15,6 +15,8 @@ #define LUA_COMPAT_MATHLIB #define LUA_COMPAT_LT_LE +#define LUA_COMPAT_GCPARAMS + #define LUA_DEBUG diff --git a/lua.c b/lua.c index d26dd8ac90..1e884b07cb 100644 --- a/lua.c +++ b/lua.c @@ -646,7 +646,7 @@ static int pmain (lua_State *L) { luai_openlibs(L); /* open standard libraries */ createargtable(L, argv, argc, script); /* create table 'arg' */ lua_gc(L, LUA_GCRESTART); /* start GC... */ - lua_gc(L, LUA_GCGEN, -1, -1, -1); /* ...in generational mode */ + lua_gc(L, LUA_GCGEN, 0, 0); /* ...in generational mode */ if (!(args & has_E)) { /* no option '-E'? */ if (handle_luainit(L) != LUA_OK) /* run LUA_INIT */ return 0; /* error running LUA_INIT */ diff --git a/lua.h b/lua.h index 32768561b2..5e2e08d926 100644 --- a/lua.h +++ b/lua.h @@ -325,7 +325,7 @@ LUA_API void (lua_warning) (lua_State *L, const char *msg, int tocont); /* -** garbage-collection function and options +** garbage-collection options */ #define LUA_GCSTOP 0 @@ -337,6 +337,25 @@ LUA_API void (lua_warning) (lua_State *L, const char *msg, int tocont); #define LUA_GCISRUNNING 6 #define LUA_GCGEN 7 #define LUA_GCINC 8 +#define LUA_GCSETPARAM 9 + + +/* +** garbage-collection parameters +*/ +/* parameters for generational mode */ +#define LUA_GCPMINORMUL 0 /* control minor collections */ +#define LUA_GCPMAJORMINOR 1 /* control shift major->minor */ +#define LUA_GCPMINORMAJOR 2 /* control shift minor->major */ + +/* parameters for incremental mode */ +#define LUA_GCPPAUSE 3 /* size of pause between successive GCs */ +#define LUA_GCPSTEPMUL 4 /* GC "speed" */ +#define LUA_GCPSTEPSIZE 5 /* GC granularity */ + +/* number of parameters */ +#define LUA_GCPN 6 + LUA_API int (lua_gc) (lua_State *L, int what, ...); diff --git a/manual/manual.of b/manual/manual.of index e6a3cd9e79..92d408e5f2 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -681,12 +681,10 @@ effectively producing a non-incremental, stop-the-world collector. The garbage-collector step size controls the size of each incremental step, specifically how many objects the interpreter creates -before performing a step. -This parameter is logarithmic: -A value of @M{n} means the interpreter will create @M{2@sp{n}} -objects between steps. -The default value is 8, -which means steps of approximately @N{256 objects}. +before performing a step: +A value of @M{n} means the interpreter will create +approximately @M{n} objects between steps. +The default value is 250. } @@ -728,11 +726,13 @@ The default value is 100. The major-minor multiplier controls the shift back to minor collections. For a multiplier @M{x}, the collector will shift back to minor collections -after a major collection collects at least @M{x%} of the allocated objects. +after a major collection collects at least @M{x%} +of the objects allocated during the last cycle. + In particular, for a multiplier of 0, the collector will immediately shift back to minor collections after doing one cycle of major collections. -The default value is 20. +The default value is 80. } @@ -3336,19 +3336,32 @@ Returns a boolean that tells whether the collector is running (i.e., not stopped). } -@item{@defid{LUA_GCINC} (int pause, int stepmul, int stepsize)| -Changes the collector to incremental mode -with the given parameters @see{incmode}. +@item{@defid{LUA_GCINC}| +Changes the collector to incremental mode. Returns the previous mode (@id{LUA_GCGEN} or @id{LUA_GCINC}). } -@item{@defid{LUA_GCGEN} (int minormul, int minormajor, int majorminor)| -Changes the collector to generational mode -with the given parameters @see{genmode}. +@item{@defid{LUA_GCGEN}| +Changes the collector to generational mode. Returns the previous mode (@id{LUA_GCGEN} or @id{LUA_GCINC}). } +@item{@defid{LUA_GCSETPARAM} (int param, int value)| +Changes the values of a parameter of the collector and returns +the previous value of that parameter. +The argument @id{param} must have one of the following values: +@description{ +@item{@defid{LUA_GCPMINORMUL}| The minor multiplier. } +@item{@defid{LUA_GCPMAJORMINOR}| The major-minor multiplier. } +@item{@defid{LUA_GCPMINORMAJOR}| The minor-major multiplier. } +@item{@defid{LUA_GCPPAUSE}| The garbage-collector pause. } +@item{@defid{LUA_GCPSTEPMUL}| The step multiplier. } +@item{@defid{LUA_GCPSTEPSIZE}| The step size. } +} +} + } + For more details about these options, see @Lid{collectgarbage}. @@ -6347,20 +6360,35 @@ Returns a boolean that tells whether the collector is running } @item{@St{incremental}| -Change the collector mode to incremental. -This option can be followed by three numbers: -the garbage-collector pause, -the step multiplier, -and the step size @see{incmode}. -A -1 or absent value means to not change that value. +Changes the collector mode to incremental and returns the previous mode. } @item{@St{generational}| -Change the collector mode to generational. -This option can be followed by three numbers: -the garbage-collector minor multiplier, -the minor-major multiplier, and the major-minor multiplier @see{genmode}. -A -1 or absent value means to not change that value. +Changes the collector mode to generational and returns the previous mode. +} + +@item{@St{setparam}| +Changes the values of a parameter of the collector and returns +the previous value of that parameter. +This option must be followed by two extra arguments: +The name of the parameter being changed (a string) +and the new value for that parameter (an integer). +The argument @id{param} must have one of the following values: +@description{ +@item{@St{minormul}| The minor multiplier. } +@item{@St{majorminor}| The major-minor multiplier. } +@item{@St{minormajor}| The minor-major multiplier. } +@item{@St{pause}| The garbage-collector pause. } +@item{@St{stepmul}| The step multiplier. } +@item{@St{stepsize}| The step size. } +} +To be able to divide by 100 +(as most parameters are given as percentages) +without using floating-point arithmetic, +Lua stores these parameters encoded. +This encoding approximates the real value; +so, the value returned as the previous value may not be +equal to the last value set. } } @@ -9249,9 +9277,10 @@ declare a local variable with the same name in the loop body. @itemize{ @item{ -There were several changes in the parameters -for the options @St{incremental} and @St{generational} -of the function @Lid{collectgarbage}. +Parameters for the garbage collection are not set +with the options @St{incremental} and @St{generational}; +instead, there is a new option @St{setparam} to that end. +Moreover, there were some changes in the parameters themselves. } } @@ -9277,9 +9306,10 @@ to signal the end of the dump. } @item{ -There were several changes in the parameters -for the options @Lid{LUA_GCINC} and @Lid{LUA_GCGEN} -of the function @Lid{lua_gc}. +Parameters for the garbage collection are not set +with the options @Lid{LUA_GCINC} and @Lid{LUA_GCGEN}; +instead, there is a new option @Lid{LUA_GCSETPARAM} to that end. +Moreover, there were some changes in the parameters themselves. } } diff --git a/testes/gc.lua b/testes/gc.lua index d7e0c4ffe6..61b5da9c4e 100644 --- a/testes/gc.lua +++ b/testes/gc.lua @@ -27,17 +27,20 @@ end -- test weird parameters to 'collectgarbage' do + collectgarbage("incremental") + local opause = collectgarbage("setparam", "pause", 100) + local ostepmul = collectgarbage("setparam", "stepmul", 100) local t = {0, 2, 10, 90, 500, 5000, 30000, 0x7ffffffe} for i = 1, #t do - local p = t[i] + collectgarbage("setparam", "pause", t[i]) for j = 1, #t do - local m = t[j] - collectgarbage("incremental", p, m) + collectgarbage("setparam", "stepmul", t[j]) collectgarbage("step") end end -- restore original parameters - collectgarbage("incremental", 200, 300) + collectgarbage("setparam", "pause", opause) + collectgarbage("setparam", "stepmul", ostepmul) collectgarbage() end diff --git a/testes/gengc.lua b/testes/gengc.lua index d708d7fcc3..cae072855a 100644 --- a/testes/gengc.lua +++ b/testes/gengc.lua @@ -163,14 +163,15 @@ assert(collectgarbage'isrunning') do print"testing stop-the-world collection" - collectgarbage("incremental", nil, 0) + local step = collectgarbage("setparam", "stepsize", 0); + collectgarbage("incremental") -- each step does a complete cycle assert(collectgarbage("step")) assert(collectgarbage("step")) -- back to default value - collectgarbage("incremental", nil, 200) + collectgarbage("setparam", "stepsize", step); end collectgarbage(oldmode) From e81f586001d767c8de9b760ae2e2c3b5da1542c6 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 22 Dec 2023 14:57:43 -0300 Subject: [PATCH 0871/1145] Removed compatibility option LUA_COMPAT_GCPARAMS The meaning of different GC parameters changed, so there is point in supporting old values for them. The new code simply ignores the parameters when changing the GC mode, so the incompatibility is small. --- lapi.c | 14 -------------- lbaselib.c | 23 +++-------------------- ltests.h | 2 -- lua.c | 2 +- testes/gc.lua | 2 +- testes/gengc.lua | 30 +++++++++++++++--------------- 6 files changed, 20 insertions(+), 53 deletions(-) diff --git a/lapi.c b/lapi.c index aa2ee73572..dcdc1cd329 100644 --- a/lapi.c +++ b/lapi.c @@ -1186,25 +1186,11 @@ LUA_API int lua_gc (lua_State *L, int what, ...) { break; } case LUA_GCGEN: { -#if defined(LUA_COMPAT_GCPARAMS) - int minormul = va_arg(argp, int); - int minormajor = va_arg(argp, int); - if (minormul > 0) setgcparam(g, MINORMUL, minormul); - if (minormajor > 0) setgcparam(g, MINORMAJOR, minormajor); -#endif res = (g->gckind == KGC_INC) ? LUA_GCINC : LUA_GCGEN; luaC_changemode(L, KGC_GENMINOR); break; } case LUA_GCINC: { -#if defined(LUA_COMPAT_GCPARAMS) - int pause = va_arg(argp, int); - int stepmul = va_arg(argp, int); - int stepsize = va_arg(argp, int); - if (pause > 0) setgcparam(g, PAUSE, pause); - if (stepmul > 0) setgcparam(g, STEPMUL, stepmul); - if (stepsize > 0) setgcparam(g, STEPSIZE, 1u << stepsize); -#endif res = (g->gckind == KGC_INC) ? LUA_GCINC : LUA_GCGEN; luaC_changemode(L, KGC_INC); break; diff --git a/lbaselib.c b/lbaselib.c index 03df57f8ca..a9d39e9f1f 100644 --- a/lbaselib.c +++ b/lbaselib.c @@ -213,8 +213,7 @@ static int luaB_collectgarbage (lua_State *L) { return 1; } case LUA_GCSTEP: { - int step = (int)luaL_optinteger(L, 2, 0); - int res = lua_gc(L, o, step); + int res = lua_gc(L, o); checkvalres(res); lua_pushboolean(L, res); return 1; @@ -226,26 +225,10 @@ static int luaB_collectgarbage (lua_State *L) { return 1; } case LUA_GCGEN: { -#if defined(LUA_COMPAT_GCPARAMS) - int minormul = (int)luaL_optinteger(L, 2, -1); - int majorminor = (int)luaL_optinteger(L, 3, -1); -#else - int minormul = 0; - int majorminor = 0; -#endif - return pushmode(L, lua_gc(L, o, minormul, majorminor)); + return pushmode(L, lua_gc(L, o)); } case LUA_GCINC: { -#if defined(LUA_COMPAT_GCPARAMS) - int pause = (int)luaL_optinteger(L, 2, -1); - int stepmul = (int)luaL_optinteger(L, 3, -1); - int stepsize = (int)luaL_optinteger(L, 4, -1); -#else - int pause = 0; - int stepmul = 0; - int stepsize = 0; -#endif - return pushmode(L, lua_gc(L, o, pause, stepmul, stepsize)); + return pushmode(L, lua_gc(L, o)); } case LUA_GCSETPARAM: { static const char *const params[] = { diff --git a/ltests.h b/ltests.h index 70afa7a320..da773d6eed 100644 --- a/ltests.h +++ b/ltests.h @@ -15,8 +15,6 @@ #define LUA_COMPAT_MATHLIB #define LUA_COMPAT_LT_LE -#define LUA_COMPAT_GCPARAMS - #define LUA_DEBUG diff --git a/lua.c b/lua.c index 1e884b07cb..e574ec9bb7 100644 --- a/lua.c +++ b/lua.c @@ -646,7 +646,7 @@ static int pmain (lua_State *L) { luai_openlibs(L); /* open standard libraries */ createargtable(L, argv, argc, script); /* create table 'arg' */ lua_gc(L, LUA_GCRESTART); /* start GC... */ - lua_gc(L, LUA_GCGEN, 0, 0); /* ...in generational mode */ + lua_gc(L, LUA_GCGEN); /* ...in generational mode */ if (!(args & has_E)) { /* no option '-E'? */ if (handle_luainit(L) != LUA_OK) /* run LUA_INIT */ return 0; /* error running LUA_INIT */ diff --git a/testes/gc.lua b/testes/gc.lua index 61b5da9c4e..8bacffa006 100644 --- a/testes/gc.lua +++ b/testes/gc.lua @@ -504,7 +504,7 @@ end do collectgarbage() collectgarbage"stop" - collectgarbage("step", 0) -- steps should not unblock the collector + collectgarbage("step") -- steps should not unblock the collector local x = gcinfo() repeat for i=1,1000 do _ENV.a = {} end -- no collection during the loop diff --git a/testes/gengc.lua b/testes/gengc.lua index cae072855a..51872cc11e 100644 --- a/testes/gengc.lua +++ b/testes/gengc.lua @@ -24,12 +24,12 @@ do assert(not T or (T.gcage(U) == "touched1" and T.gcage(U[1]) == "new")) -- both U and the table survive one more collection - collectgarbage("step", 0) + collectgarbage("step") assert(not T or (T.gcage(U) == "touched2" and T.gcage(U[1]) == "survival")) -- both U and the table survive yet another collection -- now everything is old - collectgarbage("step", 0) + collectgarbage("step") assert(not T or (T.gcage(U) == "old" and T.gcage(U[1]) == "old1")) -- data was not corrupted @@ -46,10 +46,10 @@ do assert(not T or T.gcage(old) == "old") setmetatable(old, {}) -- new table becomes OLD0 (barrier) assert(not T or T.gcage(getmetatable(old)) == "old0") - collectgarbage("step", 0) -- new table becomes OLD1 and firstold1 + collectgarbage("step") -- new table becomes OLD1 and firstold1 assert(not T or T.gcage(getmetatable(old)) == "old1") setmetatable(getmetatable(old), {__gc = foo}) -- get it out of allgc list - collectgarbage("step", 0) -- should not seg. fault + collectgarbage("step") -- should not seg. fault end @@ -65,18 +65,18 @@ do -- bug in 5.4.0 A[1] = obj -- anchor object assert(not T or T.gcage(obj) == "old1") obj = nil -- remove it from the stack - collectgarbage("step", 0) -- do a young collection + collectgarbage("step") -- do a young collection print(getmetatable(A[1]).x) -- metatable was collected end collectgarbage() -- make A old local obj = {} -- create a new object - collectgarbage("step", 0) -- make it a survival + collectgarbage("step") -- make it a survival assert(not T or T.gcage(obj) == "survival") setmetatable(obj, {__gc = gcf, x = "+"}) -- create its metatable assert(not T or T.gcage(getmetatable(obj)) == "new") obj = nil -- clear object - collectgarbage("step", 0) -- will call obj's finalizer + collectgarbage("step") -- will call obj's finalizer end @@ -94,13 +94,13 @@ do -- another bug in 5.4.0 end ) local _, f = coroutine.resume(co) -- create closure over 'x' in coroutine - collectgarbage("step", 0) -- make upvalue a survival + collectgarbage("step") -- make upvalue a survival old[1] = {"hello"} -- 'old' go to grayagain as 'touched1' coroutine.resume(co, {123}) -- its value will be new co = nil - collectgarbage("step", 0) -- hit the barrier + collectgarbage("step") -- hit the barrier assert(f() == 123 and old[1][1] == "hello") - collectgarbage("step", 0) -- run the collector once more + collectgarbage("step") -- run the collector once more -- make sure old[1] was not collected assert(f() == 123 and old[1][1] == "hello") end @@ -112,12 +112,12 @@ do -- bug introduced in commit 9cf3299fa assert(not T or T.gcage(t) == "old") t[1] = {10} assert(not T or (T.gcage(t) == "touched1" and T.gccolor(t) == "gray")) - collectgarbage("step", 0) -- minor collection + collectgarbage("step") -- minor collection assert(not T or (T.gcage(t) == "touched2" and T.gccolor(t) == "black")) - collectgarbage("step", 0) -- minor collection + collectgarbage("step") -- minor collection assert(not T or T.gcage(t) == "old") -- t should be black, but it was gray t[1] = {10} -- no barrier here, so t was still old - collectgarbage("step", 0) -- minor collection + collectgarbage("step") -- minor collection -- t, being old, is ignored by the collection, so it is not cleared assert(t[1] == nil) -- fails with the bug end @@ -144,13 +144,13 @@ do T.gcage(debug.getuservalue(U)) == "new") -- both U and the table survive one more collection - collectgarbage("step", 0) + collectgarbage("step") assert(T.gcage(U) == "touched2" and T.gcage(debug.getuservalue(U)) == "survival") -- both U and the table survive yet another collection -- now everything is old - collectgarbage("step", 0) + collectgarbage("step") assert(T.gcage(U) == "old" and T.gcage(debug.getuservalue(U)) == "old1") From 12b6f610b0f1b4157c04f0db264f1f1d0634709b Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 27 Dec 2023 12:09:11 -0300 Subject: [PATCH 0872/1145] Several tweaks in the garbage collector - back with step size in collectgarbage("step") - adjustments in defaults for some GC parameters - adjustments in 'luaO_codeparam' --- lapi.c | 18 ++++++++----- lbaselib.c | 10 +++---- lgc.h | 4 +-- lobject.c | 18 ++++++------- lobject.h | 4 +-- manual/manual.of | 69 +++++++++++++++++++++++++++--------------------- testes/files.lua | 2 ++ testes/gc.lua | 29 +++++++++++++++++++- 8 files changed, 98 insertions(+), 56 deletions(-) diff --git a/lapi.c b/lapi.c index dcdc1cd329..9ff1b8514d 100644 --- a/lapi.c +++ b/lapi.c @@ -416,10 +416,11 @@ LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) { luaC_checkGC(L); o = index2value(L, idx); /* previous call may reallocate the stack */ } - if (len != NULL) - *len = tsslen(tsvalue(o)); lua_unlock(L); - return getstr(tsvalue(o)); + if (len != NULL) + return getlstr(tsvalue(o), *len); + else + return getstr(tsvalue(o)); } @@ -1174,11 +1175,16 @@ LUA_API int lua_gc (lua_State *L, int what, ...) { } case LUA_GCSTEP: { lu_byte oldstp = g->gcstp; + l_obj n = va_arg(argp, int); + int work = 0; /* true if GC did some work */ g->gcstp = 0; /* allow GC to run (other bits must be zero here) */ - luaC_step(L); /* run one basic step */ - g->gcstp = oldstp; /* restore previous state */ - if (g->gcstate == GCSpause) /* end of cycle? */ + if (n <= 0) + n = g->GCdebt; /* force to run one basic step */ + luaE_setdebt(g, g->GCdebt - n); + luaC_condGC(L, (void)0, work = 1); + if (work && g->gcstate == GCSpause) /* end of cycle? */ res = 1; /* signal it */ + g->gcstp = oldstp; /* restore previous state */ break; } case LUA_GCISRUNNING: { diff --git a/lbaselib.c b/lbaselib.c index a9d39e9f1f..25dcaf520f 100644 --- a/lbaselib.c +++ b/lbaselib.c @@ -213,7 +213,8 @@ static int luaB_collectgarbage (lua_State *L) { return 1; } case LUA_GCSTEP: { - int res = lua_gc(L, o); + lua_Integer n = luaL_optinteger(L, 2, 0); + int res = lua_gc(L, o, (int)n); checkvalres(res); lua_pushboolean(L, res); return 1; @@ -239,7 +240,7 @@ static int luaB_collectgarbage (lua_State *L) { LUA_GCPPAUSE, LUA_GCPSTEPMUL, LUA_GCPSTEPSIZE}; int p = pnum[luaL_checkoption(L, 2, NULL, params)]; lua_Integer value = luaL_checkinteger(L, 3); - lua_pushinteger(L, lua_gc(L, o, p, value)); + lua_pushinteger(L, lua_gc(L, o, p, (int)value)); return 1; } default: { @@ -337,10 +338,7 @@ static int load_aux (lua_State *L, int status, int envidx) { static const char *getmode (lua_State *L, int idx) { const char *mode = luaL_optstring(L, idx, "bt"); - int i = 0; - if (mode[i] == 'b') i++; - if (mode[i] == 't') i++; - if (mode[i] != '\0') + if (strchr(mode, 'B') != NULL) /* Lua code cannot use fixed buffers */ luaL_argerror(L, idx, "invalid mode"); return mode; } diff --git a/lgc.h b/lgc.h index 9aff11f563..b4c4f23482 100644 --- a/lgc.h +++ b/lgc.h @@ -171,13 +171,13 @@ ** Major collections will shift to minor ones after a collection ** collects at least LUAI_MAJORMINOR% of the new objects. */ -#define LUAI_MAJORMINOR 80 +#define LUAI_MAJORMINOR 50 /* ** A young (minor) collection will run after creating LUAI_GENMINORMUL% ** new objects. */ -#define LUAI_GENMINORMUL 20 +#define LUAI_GENMINORMUL 25 /* incremental */ diff --git a/lobject.c b/lobject.c index 4091b9d79b..5a9b435ee0 100644 --- a/lobject.c +++ b/lobject.c @@ -50,22 +50,22 @@ int luaO_ceillog2 (unsigned int x) { } /* -** Encodes 'p'% as a floating-point byte, represented as (eeeeexxx). +** Encodes 'p'% as a floating-point byte, represented as (eeeexxxx). ** The exponent is represented using excess-7. Mimicking IEEE 754, the ** representation normalizes the number when possible, assuming an extra -** 1 before the mantissa (xxx) and adding one to the exponent (eeeeexxx) -** to signal that. So, the real value is (1xxx) * 2^(eeeee - 8) if -** eeeee != 0, and (xxx) * 2^-7 otherwise. +** 1 before the mantissa (xxxx) and adding one to the exponent (eeee) +** to signal that. So, the real value is (1xxxx) * 2^(eeee - 7 - 1) if +** eeee != 0, and (xxxx) * 2^-7 otherwise (subnormal numbers). */ unsigned int luaO_codeparam (unsigned int p) { - if (p >= (cast(lu_mem, 0xF) << 0xF) / 128 * 100) /* overflow? */ + if (p >= (cast(lu_mem, 0x1F) << (0xF - 7 - 1)) * 100u) /* overflow? */ return 0xFF; /* return maximum value */ else { - p = (p * 128u) / 100; - if (p <= 0xF) - return p; + p = (cast(l_uint32, p) * 128 + 99) / 100; /* round up the division */ + if (p < 0x10) /* subnormal number? */ + return p; /* exponent bits are already zero; nothing else to do */ else { - int log = luaO_ceillog2(p + 1) - 5; + int log = luaO_ceillog2(p + 1) - 5; /* preserve 5 bits */ return ((p >> log) - 0x10) | ((log + 1) << 4); } } diff --git a/lobject.h b/lobject.h index a7d85762de..81dfd4751c 100644 --- a/lobject.h +++ b/lobject.h @@ -427,8 +427,8 @@ typedef struct TString { ** Get string and length */ #define getlstr(ts, len) \ (strisshr(ts) \ - ? (cast_void(len = (ts)->shrlen), rawgetshrstr(ts)) \ - : (cast_void(len = (ts)->u.lnglen), (ts)->contents)) + ? (cast_void((len) = (ts)->shrlen), rawgetshrstr(ts)) \ + : (cast_void((len) = (ts)->u.lnglen), (ts)->contents)) /* }================================================================== */ diff --git a/manual/manual.of b/manual/manual.of index 92d408e5f2..6fd0df6de4 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -666,18 +666,6 @@ A value of 200 means that the collector waits for the total number of objects to double before starting a new cycle. The default value is 300; the maximum value is 1000. -The garbage-collector step multiplier -controls the speed of the collector relative to -object creation, -that is, -how many objects it marks or sweeps for each object created. -Larger values make the collector more aggressive. -Beware that values too small can -make the collector too slow to ever finish a cycle. -The default value is 200; the maximum value is 1000. -As a special case, a zero value means unlimited work, -effectively producing a non-incremental, stop-the-world collector. - The garbage-collector step size controls the size of each incremental step, specifically how many objects the interpreter creates @@ -686,6 +674,17 @@ A value of @M{n} means the interpreter will create approximately @M{n} objects between steps. The default value is 250. +The garbage-collector step multiplier +controls the size of each GC step. +A value of @M{n} means the interpreter will mark or sweep, +in each step, @M{n%} objects for each created object. +Larger values make the collector more aggressive. +Beware that values too small can +make the collector too slow to ever finish a cycle. +The default value is 200; the maximum value is 1000. +As a special case, a zero value means unlimited work, +effectively producing a non-incremental, stop-the-world collector. + } @sect3{genmode| @title{Generational Garbage Collection} @@ -707,11 +706,12 @@ and the @def{major-minor multiplier}. The minor multiplier controls the frequency of minor collections. For a minor multiplier @M{x}, a new minor collection will be done when the number of objects -grows @M{x%} larger than the number in use just after the last collection. +grows @M{x%} larger than the number in use just +after the last major collection. For instance, for a multiplier of 20, the collector will do a minor collection when the number of objects gets 20% larger than the total after the last major collection. -The default value is 20. +The default value is 25. The minor-major multiplier controls the shift to major collections. For a multiplier @M{x}, @@ -728,11 +728,10 @@ For a multiplier @M{x}, the collector will shift back to minor collections after a major collection collects at least @M{x%} of the objects allocated during the last cycle. - In particular, for a multiplier of 0, the collector will immediately shift back to minor collections after doing one cycle of major collections. -The default value is 80. +The default value is 50. } @@ -3327,7 +3326,7 @@ Returns the remainder of dividing the current amount of bytes of memory in use by Lua by 1024. } -@item{@defid{LUA_GCSTEP}| +@item{@defid{LUA_GCSTEP} (int n)| Performs a step of garbage collection. } @@ -3686,9 +3685,12 @@ Moreover, for a fixed buffer, the reader function should return the entire chunk in the first read. (As an example, @Lid{luaL_loadbufferx} does that.) -@id{lua_load} uses the stack internally, -so the reader function must always leave the stack -unmodified when returning. +The function @Lid{lua_load} fully preserves the Lua stack +through the calls to the reader function, +except that it may push some values for internal use +before the first call, +and it restores the stack size to its original size plus one +(for the pushed result) after the last call. @id{lua_load} can return @Lid{LUA_OK}, @Lid{LUA_ERRSYNTAX}, or @Lid{LUA_ERRMEM}. @@ -6344,13 +6346,24 @@ gives the exact number of bytes in use by Lua. @item{@St{step}| Performs a garbage-collection step. +This option may be followed by an extra argument, +an integer with the step size. +The default for this argument is zero. + +If the size is a positive @id{n}, +the collector acts as if @id{n} new objects have been created. +If the size is zero, +the collector performs a basic step. In incremental mode, -that step corresponds to the current step size; -the function returns @true if the step finished a collection cycle. +a basic step corresponds to the current step size. In generational mode, -the step performs a full minor collection or +a basic step performs a full minor collection or a major collection, -if the collector has scheduled one; +if the collector has scheduled one. + +In incremental mode, +the function returns @true if the step finished a collection cycle. +In generational mode, the function returns @true if the step performed a major collection. } @@ -6382,13 +6395,9 @@ The argument @id{param} must have one of the following values: @item{@St{stepmul}| The step multiplier. } @item{@St{stepsize}| The step size. } } -To be able to divide by 100 -(as most parameters are given as percentages) -without using floating-point arithmetic, -Lua stores these parameters encoded. -This encoding approximates the real value; +Lua rounds these values before storing them; so, the value returned as the previous value may not be -equal to the last value set. +exactly the last value set. } } diff --git a/testes/files.lua b/testes/files.lua index 2582406f86..4f925f5089 100644 --- a/testes/files.lua +++ b/testes/files.lua @@ -74,6 +74,8 @@ io.input(io.stdin); io.output(io.stdout); os.remove(file) assert(not loadfile(file)) +-- Lua code cannot use chunks with fixed buffers +checkerr("invalid mode", load, "", "", "B") checkerr("", dofile, file) assert(not io.open(file)) io.output(file) diff --git a/testes/gc.lua b/testes/gc.lua index 8bacffa006..c26de40619 100644 --- a/testes/gc.lua +++ b/testes/gc.lua @@ -35,7 +35,7 @@ do collectgarbage("setparam", "pause", t[i]) for j = 1, #t do collectgarbage("setparam", "stepmul", t[j]) - collectgarbage("step") + collectgarbage("step", t[j]) end end -- restore original parameters @@ -45,6 +45,33 @@ do end +-- +-- test the "size" of basic GC steps (whatever they mean...) +-- +do print("steps") + + local function dosteps (siz) + collectgarbage() + local a = {} + for i=1,100 do a[i] = {{}}; local b = {} end + local x = gcinfo() + local i = 0 + repeat -- do steps until it completes a collection cycle + i = i+1 + until collectgarbage("step", siz) + assert(gcinfo() < x) + return i -- number of steps + end + + collectgarbage"stop" + + if not _port then + assert(dosteps(10) < dosteps(2)) + end + +end + + _G["while"] = 234 From e7af9cdf0b9fca080e8bb3463e16d60933e786f9 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 27 Dec 2023 17:42:00 -0300 Subject: [PATCH 0873/1145] Fixed buffers reuse absolute line information --- ldump.c | 7 ++++--- lfunc.c | 2 +- lundump.c | 26 +++++++++++++++++--------- testes/api.lua | 14 ++++++++++++++ 4 files changed, 36 insertions(+), 13 deletions(-) diff --git a/ldump.c b/ldump.c index 6cd5671f2c..b31e7bc797 100644 --- a/ldump.c +++ b/ldump.c @@ -212,9 +212,10 @@ static void dumpDebug (DumpState *D, const Proto *f) { dumpVector(D, f->lineinfo, n); n = (D->strip) ? 0 : f->sizeabslineinfo; dumpInt(D, n); - for (i = 0; i < n; i++) { - dumpInt(D, f->abslineinfo[i].pc); - dumpInt(D, f->abslineinfo[i].line); + if (n > 0) { + /* 'abslineinfo' is an array of structures of int's */ + dumpAlign(D, sizeof(int)); + dumpVector(D, f->abslineinfo, n); } n = (D->strip) ? 0 : f->sizelocvars; dumpInt(D, n); diff --git a/lfunc.c b/lfunc.c index 066409c08a..d63d05fcda 100644 --- a/lfunc.c +++ b/lfunc.c @@ -268,10 +268,10 @@ void luaF_freeproto (lua_State *L, Proto *f) { if (!(f->flag & PF_FIXED)) { luaM_freearray(L, f->code, f->sizecode); luaM_freearray(L, f->lineinfo, f->sizelineinfo); + luaM_freearray(L, f->abslineinfo, f->sizeabslineinfo); } luaM_freearray(L, f->p, f->sizep); luaM_freearray(L, f->k, f->sizek); - luaM_freearray(L, f->abslineinfo, f->sizeabslineinfo); luaM_freearray(L, f->locvars, f->sizelocvars); luaM_freearray(L, f->upvalues, f->sizeupvalues); luaM_free(L, f); diff --git a/lundump.c b/lundump.c index f850dc4ad7..b33258b03d 100644 --- a/lundump.c +++ b/lundump.c @@ -36,7 +36,7 @@ typedef struct { ZIO *Z; const char *name; Table *h; /* list for string reuse */ - lu_mem offset; /* current position relative to beginning of dump */ + size_t offset; /* current position relative to beginning of dump */ lua_Integer nstr; /* number of strings in the list */ lu_byte fixed; /* dump is fixed in memory */ } LoadState; @@ -73,8 +73,10 @@ static void loadAlign (LoadState *S, int align) { #define getaddr(S,n,t) cast(t *, getaddr_(S,n,sizeof(t))) -static const void *getaddr_ (LoadState *S, int n, int sz) { - const void *block = luaZ_getaddr(S->Z, n * sz); +static const void *getaddr_ (LoadState *S, int n, size_t sz) { + size_t size = n * sz; + const void *block = luaZ_getaddr(S->Z, size); + S->offset += size; if (block == NULL) error(S, "truncated fixed buffer"); return block; @@ -143,7 +145,7 @@ static void loadString (LoadState *S, Proto *p, TString **sl) { TValue sv; size_t size = loadSize(S); if (size == 0) { /* no string? */ - *sl = NULL; + lua_assert(*sl == NULL); /* must be prefilled */ return; } else if (size == 1) { /* previously saved string? */ @@ -287,11 +289,17 @@ static void loadDebug (LoadState *S, Proto *f) { loadVector(S, f->lineinfo, n); } n = loadInt(S); - f->abslineinfo = luaM_newvectorchecked(S->L, n, AbsLineInfo); - f->sizeabslineinfo = n; - for (i = 0; i < n; i++) { - f->abslineinfo[i].pc = loadInt(S); - f->abslineinfo[i].line = loadInt(S); + if (n > 0) { + loadAlign(S, sizeof(int)); + if (S->fixed) { + f->abslineinfo = getaddr(S, n, AbsLineInfo); + f->sizeabslineinfo = n; + } + else { + f->abslineinfo = luaM_newvectorchecked(S->L, n, AbsLineInfo); + f->sizeabslineinfo = n; + loadVector(S, f->abslineinfo, n); + } } n = loadInt(S); f->locvars = luaM_newvectorchecked(S->L, n, LocVar); diff --git a/testes/api.lua b/testes/api.lua index 7d64cb22d0..85dadb6908 100644 --- a/testes/api.lua +++ b/testes/api.lua @@ -551,6 +551,20 @@ do assert(m2 > m1 and m2 - m1 < 350) X = 0; code(); assert(X == N and Y == string.rep("a", N)) X = nil; Y = nil + + -- testing debug info in fixed buffers + source = {"X = 0"} + for i = 2, 300 do source[i] = "X = X + 1" end + source[#source + 1] = "X = X + {}" -- error in last line + source = table.concat(source, "\n") + source = load(source, "name1") + source = string.dump(source) + -- load dump using fixed buffer + local code = T.testC([[ + loadstring 2 name B; + return 1 + ]], source) + checkerr(":301:", code) -- correct line information end From 7827c40c49d841daca2a40463b8a60f9a113f77e Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 10 Jan 2024 14:45:58 -0300 Subject: [PATCH 0874/1145] A few more tweaks in the garbage collector --- config.lua | 4 ++++ lapi.c | 14 ++++++++++++++ lgc.c | 23 ++++++++++++----------- lgc.h | 2 +- manual/manual.of | 4 ++-- 5 files changed, 33 insertions(+), 14 deletions(-) create mode 100644 config.lua diff --git a/config.lua b/config.lua new file mode 100644 index 0000000000..14afdc8ac7 --- /dev/null +++ b/config.lua @@ -0,0 +1,4 @@ +collectgarbage("setparam", "minormul", 25) +-- collectgarbage("generational") + + diff --git a/lapi.c b/lapi.c index 9ff1b8514d..d18445e0de 100644 --- a/lapi.c +++ b/lapi.c @@ -53,6 +53,16 @@ const char lua_ident[] = #define isupvalue(i) ((i) < LUA_REGISTRYINDEX) +/* Advance the garbage collector when creating large objects */ +static void advancegc (lua_State *L, size_t delta) { + delta >>= 5; /* one object for each 32 bytes (empirical) */ + if (delta > 0) { + global_State *g = G(L); + luaE_setdebt(g, g->GCdebt - delta); + } +} + + /* ** Convert an acceptable index to a pointer to its respective value. ** Non-valid indices return the special nil value 'G(L)->nilvalue'. @@ -530,6 +540,7 @@ LUA_API const char *lua_pushlstring (lua_State *L, const char *s, size_t len) { ts = (len == 0) ? luaS_new(L, "") : luaS_newlstr(L, s, len); setsvalue2s(L, L->top.p, ts); api_incr_top(L); + advancegc(L, len); luaC_checkGC(L); lua_unlock(L); return getstr(ts); @@ -544,6 +555,8 @@ LUA_API const char *lua_pushextlstring (lua_State *L, ts = luaS_newextlstr (L, s, len, falloc, ud); setsvalue2s(L, L->top.p, ts); api_incr_top(L); + if (falloc != NULL) /* non-static string? */ + advancegc(L, len); /* count its memory */ luaC_checkGC(L); lua_unlock(L); return getstr(ts); @@ -1336,6 +1349,7 @@ LUA_API void *lua_newuserdatauv (lua_State *L, size_t size, int nuvalue) { u = luaS_newudata(L, size, nuvalue); setuvalue(L, s2v(L->top.p), u); api_incr_top(L); + advancegc(L, size); luaC_checkGC(L); lua_unlock(L); return getudatamem(u); diff --git a/lgc.c b/lgc.c index bc4ddb0b2f..4cdea02a3f 100644 --- a/lgc.c +++ b/lgc.c @@ -1052,6 +1052,7 @@ static void setpause (global_State *g) { l_obj threshold = applygcparam(g, PAUSE, g->marked); l_obj debt = threshold - gettotalobjs(g); if (debt < 0) debt = 0; +//printf("pause: %ld %ld\n", debt, g->marked); luaE_setdebt(g, debt); } @@ -1246,7 +1247,7 @@ static void minor2inc (lua_State *L, global_State *g, int kind) { /* ** Decide whether to shift to major mode. It tests two conditions: ** 1) Whether the number of added old objects in this collection is more -** than half the number of new objects. ("step" is the number of objects +** than half the number of new objects. ('step' is the number of objects ** created between minor collections. Except for forward barriers, it ** is the maximum number of objects that can become old in each minor ** collection.) @@ -1254,15 +1255,11 @@ static void minor2inc (lua_State *L, global_State *g, int kind) { ** than 'minormajor'% of the number of lived objects after the last ** major collection. (That percentage is computed in 'limit'.) */ -static int checkminormajor (lua_State *L, global_State *g, l_obj addedold1) { +static int checkminormajor (global_State *g, l_obj addedold1) { l_obj step = applygcparam(g, MINORMUL, g->GCmajorminor); l_obj limit = applygcparam(g, MINORMAJOR, g->GCmajorminor); -//printf("-> major? %ld %ld %ld %ld (%ld)\n", g->marked, limit, step, addedold1, gettotalobjs(g)); - if (addedold1 >= (step >> 1) || g->marked >= limit) { - minor2inc(L, g, KGC_GENMAJOR); /* go to major mode */ - return 1; - } - return 0; /* stay in minor mode */ +//printf("-> (%ld) major? marked: %ld limit: %ld step: %ld addedold1: %ld)\n", gettotalobjs(g), g->marked, limit, step, addedold1); + return (addedold1 >= (step >> 1) || g->marked >= limit); } /* @@ -1309,7 +1306,11 @@ static void youngcollection (lua_State *L, global_State *g) { g->marked = marked + addedold1; /* decide whether to shift to major mode */ - if (!checkminormajor(L, g, addedold1)) + if (checkminormajor(g, addedold1)) { + minor2inc(L, g, KGC_GENMAJOR); /* go to major mode */ + g->marked = 0; /* avoid pause in first major cycle */ + } + else finishgencycle(L, g); /* still in minor mode; finish it */ } @@ -1401,12 +1402,12 @@ static void fullgen (lua_State *L, global_State *g) { ** since the last collection ('addedobjs'). */ static int checkmajorminor (lua_State *L, global_State *g) { - if (g->gckind == KGC_GENMAJOR) { + if (g->gckind == KGC_GENMAJOR) { /* generational mode? */ l_obj numobjs = gettotalobjs(g); l_obj addedobjs = numobjs - g->GCmajorminor; l_obj limit = applygcparam(g, MAJORMINOR, addedobjs); l_obj tobecollected = numobjs - g->marked; -//printf("-> minor? %ld %ld %ld\n", tobecollected, limit, numobjs); +//printf("(%ld) -> minor? tobecollected: %ld limit: %ld\n", numobjs, tobecollected, limit); if (tobecollected > limit) { atomic2gen(L, g); /* return to generational mode */ setminordebt(g); diff --git a/lgc.h b/lgc.h index b4c4f23482..5e474114dc 100644 --- a/lgc.h +++ b/lgc.h @@ -183,7 +183,7 @@ /* incremental */ /* Number of objects must be LUAI_GCPAUSE% before starting new cycle */ -#define LUAI_GCPAUSE 300 +#define LUAI_GCPAUSE 200 /* Step multiplier. (Roughly, the collector handles LUAI_GCMUL% objects for each new allocated object.) */ diff --git a/manual/manual.of b/manual/manual.of index 6fd0df6de4..ae38d7c6ac 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -664,7 +664,7 @@ Values equal to or less than 100 mean the collector will not wait to start a new cycle. A value of 200 means that the collector waits for the total number of objects to double before starting a new cycle. -The default value is 300; the maximum value is 1000. +The default value is 200. The garbage-collector step size controls the size of each incremental step, @@ -681,7 +681,7 @@ in each step, @M{n%} objects for each created object. Larger values make the collector more aggressive. Beware that values too small can make the collector too slow to ever finish a cycle. -The default value is 200; the maximum value is 1000. +The default value is 200. As a special case, a zero value means unlimited work, effectively producing a non-incremental, stop-the-world collector. From e288c5a91883793d14ed9e9d93464f6ee0b08915 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 11 Jan 2024 13:44:16 -0300 Subject: [PATCH 0875/1145] Bug: Yielding in a hook stops in the wrong instruction Yielding in a hook must decrease the program counter, because it already counted an instruction that, in the end, was not executed. However, that decrement should be done only when about to restart the thread. Otherwise, inspecting the thread with the debug library shows it one instruction behind of where it really is. --- ldebug.c | 5 ++--- ldo.c | 4 ++++ testes/coroutine.lua | 8 +++++--- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/ldebug.c b/ldebug.c index b1f16ac9fb..d6f132ea2d 100644 --- a/ldebug.c +++ b/ldebug.c @@ -925,12 +925,12 @@ int luaG_traceexec (lua_State *L, const Instruction *pc) { } pc++; /* reference is always next instruction */ ci->u.l.savedpc = pc; /* save 'pc' */ - counthook = (--L->hookcount == 0 && (mask & LUA_MASKCOUNT)); + counthook = (mask & LUA_MASKCOUNT) && (--L->hookcount == 0); if (counthook) resethookcount(L); /* reset count */ else if (!(mask & LUA_MASKLINE)) return 1; /* no line hook and count != 0; nothing to be done now */ - if (ci->callstatus & CIST_HOOKYIELD) { /* called hook last time? */ + if (ci->callstatus & CIST_HOOKYIELD) { /* hook yielded last time? */ ci->callstatus &= ~CIST_HOOKYIELD; /* erase mark */ return 1; /* do not call hook again (VM yielded, so it did not move) */ } @@ -952,7 +952,6 @@ int luaG_traceexec (lua_State *L, const Instruction *pc) { if (L->status == LUA_YIELD) { /* did hook yield? */ if (counthook) L->hookcount = 1; /* undo decrement to zero */ - ci->u.l.savedpc--; /* undo increment (resume will increment it again) */ ci->callstatus |= CIST_HOOKYIELD; /* mark that it yielded */ luaD_throw(L, LUA_YIELD); } diff --git a/ldo.c b/ldo.c index bd8d965fc0..ea0529507e 100644 --- a/ldo.c +++ b/ldo.c @@ -792,6 +792,10 @@ static void resume (lua_State *L, void *ud) { lua_assert(L->status == LUA_YIELD); L->status = LUA_OK; /* mark that it is running (again) */ if (isLua(ci)) { /* yielded inside a hook? */ + /* undo increment made by 'luaG_traceexec': instruction was not + executed yet */ + lua_assert(ci->callstatus & CIST_HOOKYIELD); + ci->u.l.savedpc--; L->top.p = firstArg; /* discard arguments */ luaV_execute(L, ci); /* just continue running Lua code */ } diff --git a/testes/coroutine.lua b/testes/coroutine.lua index de7e46fbd3..e566c86e96 100644 --- a/testes/coroutine.lua +++ b/testes/coroutine.lua @@ -610,18 +610,20 @@ else -- (bug in 5.2/5.3) c = coroutine.create(function (a, ...) T.sethook("yield 0", "l") -- will yield on next two lines - assert(a == 10) + local b = a return ... end) assert(coroutine.resume(c, 1, 2, 3)) -- start coroutine local n,v = debug.getlocal(c, 0, 1) -- check its local - assert(n == "a" and v == 1) + assert(n == "a" and v == 1 and debug.getlocal(c, 0, 2) ~= "b") assert(debug.setlocal(c, 0, 1, 10)) -- test 'setlocal' local t = debug.getinfo(c, 0) -- test 'getinfo' - assert(t.currentline == t.linedefined + 1) + assert(t.currentline == t.linedefined + 2) assert(not debug.getinfo(c, 1)) -- no other level assert(coroutine.resume(c)) -- run next line + local n,v = debug.getlocal(c, 0, 2) -- check next local + assert(n == "b" and v == 10) v = {coroutine.resume(c)} -- finish coroutine assert(v[1] == true and v[2] == 2 and v[3] == 3 and v[4] == undef) assert(not coroutine.resume(c)) From d862da6d04111ce7e5b225040fbe7e526761f478 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 12 Jan 2024 15:50:51 -0300 Subject: [PATCH 0876/1145] Optimizations for 'lua_rawgeti' and 'lua_rawseti' 'lua_rawgeti' now uses "fast track"; 'lua_rawseti' still calls 'luaH_setint', but the latter was recoded to avoid extra overhead when writing to the array part after 'alimit'. --- lapi.c | 19 +++++++++++-------- ltable.c | 55 +++++++++++++++++++++++++++++++++++++------------------ ltable.h | 19 +++++++++++++++++++ lvm.h | 36 +++++++++++++----------------------- 4 files changed, 80 insertions(+), 49 deletions(-) diff --git a/lapi.c b/lapi.c index d18445e0de..74f1d66b1f 100644 --- a/lapi.c +++ b/lapi.c @@ -102,7 +102,7 @@ static TValue *index2value (lua_State *L, int idx) { /* ** Convert a valid actual index (not a pseudo-index) to its address. */ -l_sinline StkId index2stack (lua_State *L, int idx) { +static StkId index2stack (lua_State *L, int idx) { CallInfo *ci = L->ci; if (idx > 0) { StkId o = ci->func.p + idx; @@ -234,7 +234,7 @@ LUA_API void lua_closeslot (lua_State *L, int idx) { ** Note that we move(copy) only the value inside the stack. ** (We do not move additional fields that may exist.) */ -l_sinline void reverse (lua_State *L, StkId from, StkId to) { +static void reverse (lua_State *L, StkId from, StkId to) { for (; from < to; from++, to--) { TValue temp; setobj(L, &temp, s2v(from)); @@ -664,7 +664,7 @@ LUA_API int lua_pushthread (lua_State *L) { */ -l_sinline int auxgetstr (lua_State *L, const TValue *t, const char *k) { +static int auxgetstr (lua_State *L, const TValue *t, const char *k) { int hres; TString *str = luaS_new(L, k); luaV_fastget(t, str, s2v(L->top.p), luaH_getstr, hres); @@ -683,7 +683,9 @@ l_sinline int auxgetstr (lua_State *L, const TValue *t, const char *k) { static void getGlobalTable (lua_State *L, TValue *gt) { Table *registry = hvalue(&G(L)->l_registry); - luaH_getint(registry, LUA_RIDX_GLOBALS, gt); + int hres = luaH_getint(registry, LUA_RIDX_GLOBALS, gt); + (void)hres; /* avoid warnings (not used) when checks are off */ + api_check(L, hres == HOK, "global table must exist"); } @@ -740,7 +742,7 @@ l_sinline int finishrawget (lua_State *L, int hres) { } -static Table *gettable (lua_State *L, int idx) { +l_sinline Table *gettable (lua_State *L, int idx) { TValue *t = index2value(L, idx); api_check(L, ttistable(t), "table expected"); return hvalue(t); @@ -761,9 +763,11 @@ LUA_API int lua_rawget (lua_State *L, int idx) { LUA_API int lua_rawgeti (lua_State *L, int idx, lua_Integer n) { Table *t; + int hres; lua_lock(L); t = gettable(L, idx); - return finishrawget(L, luaH_getint(t, n, s2v(L->top.p))); + luaH_fastgeti(t, n, s2v(L->top.p), hres); + return finishrawget(L, hres); } @@ -901,9 +905,8 @@ LUA_API void lua_seti (lua_State *L, int idx, lua_Integer n) { api_checknelems(L, 1); t = index2value(L, idx); luaV_fastseti(t, n, s2v(L->top.p - 1), hres); - if (hres == HOK) { + if (hres == HOK) luaV_finishfastset(L, t, s2v(L->top.p - 1)); - } else { TValue temp; setivalue(&temp, n); diff --git a/ltable.c b/ltable.c index d3e90696a4..21a54f81d3 100644 --- a/ltable.c +++ b/ltable.c @@ -408,21 +408,22 @@ static void freehash (lua_State *L, Table *t) { ** not the real size of the array, the key still can be in the array ** part. In this case, do the "Xmilia trick" to check whether 'key-1' ** is smaller than the real size. -** The trick works as follow: let 'p' be an integer such that -** '2^(p+1) >= alimit > 2^p', or '2^(p+1) > alimit-1 >= 2^p'. -** That is, 2^(p+1) is the real size of the array, and 'p' is the highest -** bit on in 'alimit-1'. What we have to check becomes 'key-1 < 2^(p+1)'. -** We compute '(key-1) & ~(alimit-1)', which we call 'res'; it will -** have the 'p' bit cleared. If the key is outside the array, that is, -** 'key-1 >= 2^(p+1)', then 'res' will have some 1-bit higher than 'p', -** therefore it will be larger or equal to 'alimit', and the check +** The trick works as follow: let 'p' be the integer such that +** '2^(p+1) >= alimit > 2^p', or '2^(p+1) > alimit-1 >= 2^p'. That is, +** 'p' is the highest 1-bit in 'alimit-1', and 2^(p+1) is the real size +** of the array. What we have to check becomes 'key-1 < 2^(p+1)'. We +** compute '(key-1) & ~(alimit-1)', which we call 'res'; it will have +** the 'p' bit cleared. (It may also clear other bits smaller than 'p', +** but no bit higher than 'p'.) If the key is outside the array, that +** is, 'key-1 >= 2^(p+1)', then 'res' will have some 1-bit higher than +** 'p', therefore it will be larger or equal to 'alimit', and the check ** will fail. If 'key-1 < 2^(p+1)', then 'res' has no 1-bit higher than ** 'p', and as the bit 'p' itself was cleared, 'res' will be smaller ** than 2^p, therefore smaller than 'alimit', and the check succeeds. ** As special cases, when 'alimit' is 0 the condition is trivially false, ** and when 'alimit' is 1 the condition simplifies to 'key-1 < alimit'. ** If key is 0 or negative, 'res' will have its higher bit on, so that -** if cannot be smaller than alimit. +** it cannot be smaller than 'alimit'. */ static int keyinarray (Table *t, lua_Integer key) { lua_Unsigned alimit = t->alimit; @@ -788,11 +789,11 @@ static Node *getfreepos (Table *t) { /* -** inserts a new key into a hash table; first, check whether key's main +** Inserts a new key into a hash table; first, check whether key's main ** position is free. If not, check whether colliding node is in its main -** position or not: if it is not, move colliding node to an empty place and -** put new key in its main position; otherwise (colliding node is in its main -** position), new key goes to an empty position. +** position or not: if it is not, move colliding node to an empty place +** and put new key in its main position; otherwise (colliding node is in +** its main position), new key goes to an empty position. */ static void luaH_newkey (lua_State *L, Table *t, const TValue *key, TValue *value) { @@ -987,6 +988,16 @@ static int finishnodeset (Table *t, const TValue *slot, TValue *val) { } +static int rawfinishnodeset (const TValue *slot, TValue *val) { + if (isabstkey(slot)) + return 0; /* no slot with that key */ + else { + setobj(((lua_State*)NULL), cast(TValue*, slot), val); + return 1; /* success */ + } +} + + int luaH_psetint (Table *t, lua_Integer key, TValue *val) { if (keyinarray(t, key)) { lu_byte *tag = getArrTag(t, key - 1); @@ -1063,12 +1074,20 @@ void luaH_set (lua_State *L, Table *t, const TValue *key, TValue *value) { } +/* +** Ditto for a GC barrier. (No need to invalidate the TM cache, as +** integers cannot be keys to metamethods.) +*/ void luaH_setint (lua_State *L, Table *t, lua_Integer key, TValue *value) { - int hres = luaH_psetint(t, key, value); - if (hres != HOK) { - TValue k; - setivalue(&k, key); - luaH_finishset(L, t, &k, value, hres); + if (keyinarray(t, key)) + obj2arr(t, key, value); + else { + int ok = rawfinishnodeset(getintfromhash(t, key), value); + if (!ok) { + TValue k; + setivalue(&k, key); + luaH_newkey(L, t, &k, value); + } } } diff --git a/ltable.h b/ltable.h index 5581efb132..8b0340b56f 100644 --- a/ltable.h +++ b/ltable.h @@ -45,6 +45,25 @@ #define nodefromval(v) cast(Node *, (v)) + +#define luaH_fastgeti(t,k,res,hres) \ + { Table *h = t; lua_Unsigned u = l_castS2U(k); \ + if ((u - 1u < h->alimit)) { \ + int tag = *getArrTag(h,(u)-1u); \ + if (tagisempty(tag)) hres = HNOTFOUND; \ + else { farr2val(h, u, tag, res); hres = HOK; }} \ + else { hres = luaH_getint(h, u, res); }} + + +#define luaH_fastseti(t,k,val,hres) \ + { Table *h = t; lua_Unsigned u = l_castS2U(k); \ + if ((u - 1u < h->alimit)) { \ + lu_byte *tag = getArrTag(h,(u)-1u); \ + if (tagisempty(*tag)) hres = ~cast_int(u); \ + else { fval2arr(h, u, tag, val); hres = HOK; }} \ + else { hres = luaH_psetint(h, u, val); }} + + /* results from get/pset */ #define HOK 0 #define HNOTFOUND 1 diff --git a/lvm.h b/lvm.h index c74c81f843..54ee5dd71e 100644 --- a/lvm.h +++ b/lvm.h @@ -78,35 +78,25 @@ typedef enum { /* ** fast track for 'gettable' */ -#define luaV_fastget(t,k,res,f, aux) \ - (aux = (!ttistable(t) ? HNOTATABLE : f(hvalue(t), k, res))) +#define luaV_fastget(t,k,res,f, hres) \ + (hres = (!ttistable(t) ? HNOTATABLE : f(hvalue(t), k, res))) /* ** Special case of 'luaV_fastget' for integers, inlining the fast case ** of 'luaH_getint'. */ -#define luaV_fastgeti(t,k,res,aux) \ - if (!ttistable(t)) aux = HNOTATABLE; \ - else { Table *h = hvalue(t); lua_Unsigned u = l_castS2U(k); \ - if ((u - 1u < h->alimit)) { \ - int tag = *getArrTag(h,(u)-1u); \ - if (tagisempty(tag)) aux = HNOTFOUND; \ - else { farr2val(h, u, tag, res); aux = HOK; }} \ - else { aux = luaH_getint(h, u, res); }} - - -#define luaV_fastset(t,k,val,aux,f) \ - (aux = (!ttistable(t) ? HNOTATABLE : f(hvalue(t), k, val))) - -#define luaV_fastseti(t,k,val,aux) \ - if (!ttistable(t)) aux = HNOTATABLE; \ - else { Table *h = hvalue(t); lua_Unsigned u = l_castS2U(k); \ - if ((u - 1u < h->alimit)) { \ - lu_byte *tag = getArrTag(h,(u)-1u); \ - if (tagisempty(*tag)) aux = ~cast_int(u); \ - else { fval2arr(h, u, tag, val); aux = HOK; }} \ - else { aux = luaH_psetint(h, u, val); }} +#define luaV_fastgeti(t,k,res,hres) \ + if (!ttistable(t)) hres = HNOTATABLE; \ + else { luaH_fastgeti(hvalue(t), k, res, hres); } + + +#define luaV_fastset(t,k,val,hres,f) \ + (hres = (!ttistable(t) ? HNOTATABLE : f(hvalue(t), k, val))) + +#define luaV_fastseti(t,k,val,hres) \ + if (!ttistable(t)) hres = HNOTATABLE; \ + else { luaH_fastseti(hvalue(t), k, val, hres); } /* From 8eb0abc9db4d47db5192bed18565e3d1aa53566d Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Sat, 13 Jan 2024 18:10:50 -0300 Subject: [PATCH 0877/1145] Removed uses of LUA_NUMTAGS That constant was already deprecated (see commit 6aabf4b15e7). --- lgc.c | 2 +- lstate.c | 2 +- ltests.c | 4 ++-- lua.h | 2 -- 4 files changed, 4 insertions(+), 6 deletions(-) diff --git a/lgc.c b/lgc.c index 4cdea02a3f..f813038f34 100644 --- a/lgc.c +++ b/lgc.c @@ -330,7 +330,7 @@ static void reallymarkobject (global_State *g, GCObject *o) { */ static void markmt (global_State *g) { int i; - for (i=0; i < LUA_NUMTAGS; i++) + for (i=0; i < LUA_NUMTYPES; i++) markobjectN(g, g->mt[i]); } diff --git a/lstate.c b/lstate.c index de02c91a3d..78146bdbfa 100644 --- a/lstate.c +++ b/lstate.c @@ -371,7 +371,7 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud, unsigned int seed) { setgcparam(g, MINORMUL, LUAI_GENMINORMUL); setgcparam(g, MINORMAJOR, LUAI_MINORMAJOR); setgcparam(g, MAJORMINOR, LUAI_MAJORMINOR); - for (i=0; i < LUA_NUMTAGS; i++) g->mt[i] = NULL; + for (i=0; i < LUA_NUMTYPES; i++) g->mt[i] = NULL; if (luaD_rawrunprotected(L, f_luaopen, NULL) != LUA_OK) { /* memory allocation error: free partial state */ close_state(L); diff --git a/ltests.c b/ltests.c index cf9a8eaf17..6eebc73200 100644 --- a/ltests.c +++ b/ltests.c @@ -216,7 +216,7 @@ void *debug_realloc (void *ud, void *b, size_t oldsize, size_t size) { mc->memlimit = limit ? strtoul(limit, NULL, 10) : ULONG_MAX; } if (block == NULL) { - type = (oldsize < LUA_NUMTAGS) ? oldsize : 0; + type = (oldsize < LUA_NUMTYPES) ? oldsize : 0; oldsize = 0; } else { @@ -856,7 +856,7 @@ static int mem_query (lua_State *L) { else { const char *t = luaL_checkstring(L, 1); int i; - for (i = LUA_NUMTAGS - 1; i >= 0; i--) { + for (i = LUA_NUMTYPES - 1; i >= 0; i--) { if (strcmp(t, ttypename(i)) == 0) { lua_pushinteger(L, l_memcontrol.objcount[i]); return 1; diff --git a/lua.h b/lua.h index 5e2e08d926..26b45e3edf 100644 --- a/lua.h +++ b/lua.h @@ -442,8 +442,6 @@ LUA_API void (lua_closeslot) (lua_State *L, int idx); #define lua_getuservalue(L,idx) lua_getiuservalue(L,idx,1) #define lua_setuservalue(L,idx) lua_setiuservalue(L,idx,1) -#define LUA_NUMTAGS LUA_NUMTYPES - #define lua_resetthread(L) lua_closethread(L,NULL) /* }============================================================== */ From 17e0c29d9b435392016b707309ed51409b0aea12 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 15 Jan 2024 11:31:49 -0300 Subject: [PATCH 0878/1145] Clear interface between references and predefines The reference system has a defined way to add initial values to the table where it operates. --- lauxlib.c | 28 ++++++++++++---------------- lstate.c | 3 +++ ltests.c | 28 +++++++++++++++++++++++++--- lua.h | 5 +++-- manual/manual.of | 26 ++++++++++++++++++-------- testes/api.lua | 37 +++++++++++++++++++++++++++---------- testes/coroutine.lua | 8 ++++---- 7 files changed, 92 insertions(+), 43 deletions(-) diff --git a/lauxlib.c b/lauxlib.c index ab3c7c93a7..8d23bc7d6d 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -672,13 +672,10 @@ LUALIB_API char *luaL_buffinitsize (lua_State *L, luaL_Buffer *B, size_t sz) { ** ======================================================= */ -/* index of free-list header (after the predefined values) */ -#define freelist (LUA_RIDX_LAST + 1) - /* -** The previously freed references form a linked list: -** t[freelist] is the index of a first free index, or zero if list is -** empty; t[t[freelist]] is the index of the second element; etc. +** The previously freed references form a linked list: t[1] is the index +** of a first free index, t[t[1]] is the index of the second element, +** etc. A zero signals the end of the list. */ LUALIB_API int luaL_ref (lua_State *L, int t) { int ref; @@ -687,19 +684,18 @@ LUALIB_API int luaL_ref (lua_State *L, int t) { return LUA_REFNIL; /* 'nil' has a unique fixed reference */ } t = lua_absindex(L, t); - if (lua_rawgeti(L, t, freelist) == LUA_TNIL) { /* first access? */ + if (lua_rawgeti(L, t, 1) == LUA_TNUMBER) /* already initialized? */ + ref = (int)lua_tointeger(L, -1); /* ref = t[1] */ + else { /* first access */ + lua_assert(!lua_toboolean(L, -1)); /* must be nil or false */ ref = 0; /* list is empty */ lua_pushinteger(L, 0); /* initialize as an empty list */ - lua_rawseti(L, t, freelist); /* ref = t[freelist] = 0 */ - } - else { /* already initialized */ - lua_assert(lua_isinteger(L, -1)); - ref = (int)lua_tointeger(L, -1); /* ref = t[freelist] */ + lua_rawseti(L, t, 1); /* ref = t[1] = 0 */ } lua_pop(L, 1); /* remove element from stack */ if (ref != 0) { /* any free element? */ lua_rawgeti(L, t, ref); /* remove it from list */ - lua_rawseti(L, t, freelist); /* (t[freelist] = t[ref]) */ + lua_rawseti(L, t, 1); /* (t[1] = t[ref]) */ } else /* no free elements */ ref = (int)lua_rawlen(L, t) + 1; /* get a new reference */ @@ -711,11 +707,11 @@ LUALIB_API int luaL_ref (lua_State *L, int t) { LUALIB_API void luaL_unref (lua_State *L, int t, int ref) { if (ref >= 0) { t = lua_absindex(L, t); - lua_rawgeti(L, t, freelist); + lua_rawgeti(L, t, 1); lua_assert(lua_isinteger(L, -1)); - lua_rawseti(L, t, ref); /* t[ref] = t[freelist] */ + lua_rawseti(L, t, ref); /* t[ref] = t[1] */ lua_pushinteger(L, ref); - lua_rawseti(L, t, freelist); /* t[freelist] = ref */ + lua_rawseti(L, t, 1); /* t[1] = ref */ } } diff --git a/lstate.c b/lstate.c index 78146bdbfa..9a61cd6d5d 100644 --- a/lstate.c +++ b/lstate.c @@ -189,6 +189,9 @@ static void init_registry (lua_State *L, global_State *g) { Table *registry = luaH_new(L); sethvalue(L, &g->l_registry, registry); luaH_resize(L, registry, LUA_RIDX_LAST, 0); + /* registry[1] = false */ + setbfvalue(&aux); + luaH_setint(L, registry, 1, &aux); /* registry[LUA_RIDX_MAINTHREAD] = L */ setthvalue(L, &aux, L); luaH_setint(L, registry, LUA_RIDX_MAINTHREAD, &aux); diff --git a/ltests.c b/ltests.c index 6eebc73200..6de62e529c 100644 --- a/ltests.c +++ b/ltests.c @@ -1084,27 +1084,39 @@ static int string_query (lua_State *L) { } +static int getreftable (lua_State *L) { + if (lua_istable(L, 2)) /* is there a table as second argument? */ + return 2; /* use it as the table */ + else + return LUA_REGISTRYINDEX; /* default is to use the register */ +} + + static int tref (lua_State *L) { + int t = getreftable(L); int level = lua_gettop(L); luaL_checkany(L, 1); lua_pushvalue(L, 1); - lua_pushinteger(L, luaL_ref(L, LUA_REGISTRYINDEX)); + lua_pushinteger(L, luaL_ref(L, t)); cast_void(level); /* to avoid warnings */ lua_assert(lua_gettop(L) == level+1); /* +1 for result */ return 1; } + static int getref (lua_State *L) { + int t = getreftable(L); int level = lua_gettop(L); - lua_rawgeti(L, LUA_REGISTRYINDEX, luaL_checkinteger(L, 1)); + lua_rawgeti(L, t, luaL_checkinteger(L, 1)); cast_void(level); /* to avoid warnings */ lua_assert(lua_gettop(L) == level+1); return 1; } static int unref (lua_State *L) { + int t = getreftable(L); int level = lua_gettop(L); - luaL_unref(L, LUA_REGISTRYINDEX, cast_int(luaL_checkinteger(L, 1))); + luaL_unref(L, t, cast_int(luaL_checkinteger(L, 1))); cast_void(level); /* to avoid warnings */ lua_assert(lua_gettop(L) == level); return 0; @@ -1373,6 +1385,16 @@ static int getnum_aux (lua_State *L, lua_State *L1, const char **pc) { (*pc)++; return res; } + else if (**pc == '!') { + (*pc)++; + if (**pc == 'G') + res = LUA_RIDX_GLOBALS; + else if (**pc == 'M') + res = LUA_RIDX_MAINTHREAD; + else lua_assert(0); + (*pc)++; + return res; + } else if (**pc == '-') { sig = -1; (*pc)++; diff --git a/lua.h b/lua.h index 26b45e3edf..b7508b4e68 100644 --- a/lua.h +++ b/lua.h @@ -80,9 +80,10 @@ typedef struct lua_State lua_State; /* predefined values in the registry */ -#define LUA_RIDX_MAINTHREAD 1 +/* index 1 is reserved for the reference mechanism */ #define LUA_RIDX_GLOBALS 2 -#define LUA_RIDX_LAST LUA_RIDX_GLOBALS +#define LUA_RIDX_MAINTHREAD 3 +#define LUA_RIDX_LAST 3 /* type of numbers in Lua */ diff --git a/manual/manual.of b/manual/manual.of index ae38d7c6ac..64bb5473c5 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -2645,8 +2645,8 @@ string keys starting with an underscore followed by uppercase letters are reserved for Lua. The integer keys in the registry are used -by the reference mechanism @seeC{luaL_ref} -and by some predefined values. +by the reference mechanism @seeC{luaL_ref}, +with some predefined values. Therefore, integer keys in the registry must not be used for other purposes. @@ -6018,11 +6018,21 @@ Creates and returns a @def{reference}, in the table at index @id{t}, for the object on the top of the stack (and pops the object). -A reference is a unique integer key. -As long as you do not manually add integer keys into the table @id{t}, -@Lid{luaL_ref} ensures the uniqueness of the key it returns. +The reference system uses the integer keys of the table. +A reference is a unique integer key; +@Lid{luaL_ref} ensures the uniqueness of the keys it returns. +The entry 1 is reserved for internal use. +Before the first use of @Lid{luaL_ref}, +the integer keys of the table +should form a proper sequence (no holes), +and the value at entry 1 should be false: +@nil if the sequence is empty, +@false otherwise. +You should not manually set integer keys in the table +after the first use of @Lid{luaL_ref}. + You can retrieve an object referred by the reference @id{r} -by calling @T{lua_rawgeti(L, t, r)}. +by calling @T{lua_rawgeti(L, t, r)} or @T{lua_geti(L, t, r)}. The function @Lid{luaL_unref} frees a reference. If the object on the top of the stack is @nil, @@ -6188,8 +6198,8 @@ Returns the name of the type of the value at the given index. Releases the reference @id{ref} from the table at index @id{t} @seeC{luaL_ref}. The entry is removed from the table, -so that the referred object can be collected. -The reference @id{ref} is also freed to be used again. +so that the referred object can be collected and +the reference @id{ref} can be used again by @Lid{luaL_ref}. If @id{ref} is @Lid{LUA_NOREF} or @Lid{LUA_REFNIL}, @Lid{luaL_unref} does nothing. diff --git a/testes/api.lua b/testes/api.lua index 85dadb6908..ca4b3fb47f 100644 --- a/testes/api.lua +++ b/testes/api.lua @@ -467,7 +467,7 @@ for i = 1,lim do prog[#prog + 1] = "pushnum " .. i * 10 end -prog[#prog + 1] = "rawgeti R 2" -- get global table in registry +prog[#prog + 1] = "rawgeti R !G" -- get global table in registry prog[#prog + 1] = "insert " .. -(2*lim + 2) for i = 1,lim do @@ -930,28 +930,30 @@ checkerr("FILE%* expected, got userdata", io.input, x) assert(debug.getmetatable(x) == nil and debug.getmetatable(y) == nil) -local d = T.ref(a); -local e = T.ref(b); -local f = T.ref(c); -t = {T.getref(d), T.getref(e), T.getref(f)} +-- Test references in an arbitrary table +local reftable = {} +local d = T.ref(a, reftable); +local e = T.ref(b, reftable); +local f = T.ref(c, reftable); +t = {T.getref(d, reftable), T.getref(e, reftable), T.getref(f, reftable)} assert(t[1] == a and t[2] == b and t[3] == c) t=nil; a=nil; c=nil; -T.unref(e); T.unref(f) +T.unref(e, reftable); T.unref(f, reftable) collectgarbage() -- check that unref objects have been collected assert(#cl == 1 and cl[1] == nc) -x = T.getref(d) +x = T.getref(d, reftable) assert(type(x) == 'userdata' and debug.getmetatable(x) == tt) x =nil tt.b = b -- create cycle tt=nil -- frees tt for GC A = nil b = nil -T.unref(d); +T.unref(d, reftable); local n5 = T.newuserdata(0) debug.setmetatable(n5, {__gc=F}) n5 = T.udataval(n5) @@ -960,6 +962,21 @@ assert(#cl == 4) -- check order of collection assert(cl[2] == n5 and cl[3] == nb and cl[4] == na) +-- reuse a reference in 'reftable' +T.unref(T.ref(23, reftable), reftable) + +do -- check reftable + local count = 0 + local i = 1 + while reftable[i] ~= 0 do + i = reftable[i] -- traverse linked list of free references + count = count + 1 + end + -- maximum number of simultaneously locked objects was 3 + assert(count == 3 and #reftable == 3 + 1) -- +1 for reserved [1] +end + + collectgarbage"restart" @@ -1363,8 +1380,8 @@ end) -- testing threads --- get main thread from registry (at index LUA_RIDX_MAINTHREAD == 1) -local mt = T.testC("rawgeti R 1; return 1") +-- get main thread from registry +local mt = T.testC("rawgeti R !M; return 1") assert(type(mt) == "thread" and coroutine.running() == mt) diff --git a/testes/coroutine.lua b/testes/coroutine.lua index 990da8c480..6c15db03c1 100644 --- a/testes/coroutine.lua +++ b/testes/coroutine.lua @@ -681,7 +681,7 @@ else c == "ERRRUN" and d == 4) a, b, c, d = T.testC([[ - rawgeti R 1 # get main thread + rawgeti R !M # get main thread pushnum 10; pushnum 20; resume -3 2; @@ -699,7 +699,7 @@ else assert(T.testC(state, "newthread; isyieldable -1; remove 1; return 1")) -- main thread is not yieldable - assert(not T.testC(state, "rawgeti R 1; isyieldable -1; remove 1; return 1")) + assert(not T.testC(state, "rawgeti R !M; isyieldable -1; remove 1; return 1")) T.testC(state, "settop 0") @@ -711,7 +711,7 @@ else return 'ok']])) local t = table.pack(T.testC(state, [[ - rawgeti R 1 # get main thread + rawgeti R !M # get main thread pushstring 'XX' getglobal X # get function for body pushstring AA # arg @@ -720,7 +720,7 @@ else setglobal T # top setglobal B # second yielded value setglobal A # fist yielded value - rawgeti R 1 # get main thread + rawgeti R !M # get main thread pushnum 5 # arg (noise) resume 1 1 # after coroutine ends, previous stack is back pushstatus From 4a8e48086433ad12f2991c07f3064278714fd0f1 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 16 Jan 2024 17:02:55 -0300 Subject: [PATCH 0879/1145] New mechanism to query GC parameters --- lapi.c | 5 +++-- lbaselib.c | 8 ++++---- lua.h | 2 +- manual/manual.of | 27 +++++++++++++++------------ testes/gc.lua | 14 ++++++++------ testes/gengc.lua | 6 ++++-- 6 files changed, 35 insertions(+), 27 deletions(-) diff --git a/lapi.c b/lapi.c index 74f1d66b1f..b8e588017d 100644 --- a/lapi.c +++ b/lapi.c @@ -1217,12 +1217,13 @@ LUA_API int lua_gc (lua_State *L, int what, ...) { luaC_changemode(L, KGC_INC); break; } - case LUA_GCSETPARAM: { + case LUA_GCPARAM: { int param = va_arg(argp, int); int value = va_arg(argp, int); api_check(L, 0 <= param && param < LUA_GCPN, "invalid parameter"); res = luaO_applyparam(g->gcparams[param], 100); - g->gcparams[param] = luaO_codeparam(value); + if (value >= 0) + g->gcparams[param] = luaO_codeparam(value); break; } default: res = -1; /* invalid option */ diff --git a/lbaselib.c b/lbaselib.c index 25dcaf520f..4238f96a64 100644 --- a/lbaselib.c +++ b/lbaselib.c @@ -199,10 +199,10 @@ static int pushmode (lua_State *L, int oldmode) { static int luaB_collectgarbage (lua_State *L) { static const char *const opts[] = {"stop", "restart", "collect", "count", "step", "isrunning", "generational", "incremental", - "setparam", NULL}; + "param", NULL}; static const char optsnum[] = {LUA_GCSTOP, LUA_GCRESTART, LUA_GCCOLLECT, LUA_GCCOUNT, LUA_GCSTEP, LUA_GCISRUNNING, LUA_GCGEN, LUA_GCINC, - LUA_GCSETPARAM}; + LUA_GCPARAM}; int o = optsnum[luaL_checkoption(L, 1, "collect", opts)]; switch (o) { case LUA_GCCOUNT: { @@ -231,7 +231,7 @@ static int luaB_collectgarbage (lua_State *L) { case LUA_GCINC: { return pushmode(L, lua_gc(L, o)); } - case LUA_GCSETPARAM: { + case LUA_GCPARAM: { static const char *const params[] = { "minormul", "majorminor", "minormajor", "pause", "stepmul", "stepsize", NULL}; @@ -239,7 +239,7 @@ static int luaB_collectgarbage (lua_State *L) { LUA_GCPMINORMUL, LUA_GCPMAJORMINOR, LUA_GCPMINORMAJOR, LUA_GCPPAUSE, LUA_GCPSTEPMUL, LUA_GCPSTEPSIZE}; int p = pnum[luaL_checkoption(L, 2, NULL, params)]; - lua_Integer value = luaL_checkinteger(L, 3); + lua_Integer value = luaL_optinteger(L, 3, -1); lua_pushinteger(L, lua_gc(L, o, p, (int)value)); return 1; } diff --git a/lua.h b/lua.h index b7508b4e68..58f3164638 100644 --- a/lua.h +++ b/lua.h @@ -338,7 +338,7 @@ LUA_API void (lua_warning) (lua_State *L, const char *msg, int tocont); #define LUA_GCISRUNNING 6 #define LUA_GCGEN 7 #define LUA_GCINC 8 -#define LUA_GCSETPARAM 9 +#define LUA_GCPARAM 9 /* diff --git a/manual/manual.of b/manual/manual.of index 64bb5473c5..48f396d947 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -3345,9 +3345,9 @@ Changes the collector to generational mode. Returns the previous mode (@id{LUA_GCGEN} or @id{LUA_GCINC}). } -@item{@defid{LUA_GCSETPARAM} (int param, int value)| -Changes the values of a parameter of the collector and returns -the previous value of that parameter. +@item{@defid{LUA_GCPARAM} (int param, int val)| +Changes and/or returns the value of a parameter of the collector. +If @id{val} is negative, the call only returns the current value. The argument @id{param} must have one of the following values: @description{ @item{@defid{LUA_GCPMINORMUL}| The minor multiplier. } @@ -6390,13 +6390,12 @@ Changes the collector mode to incremental and returns the previous mode. Changes the collector mode to generational and returns the previous mode. } -@item{@St{setparam}| -Changes the values of a parameter of the collector and returns -the previous value of that parameter. -This option must be followed by two extra arguments: -The name of the parameter being changed (a string) -and the new value for that parameter (an integer). -The argument @id{param} must have one of the following values: +@item{@St{param}| +Changes and/or retrieves the values of a parameter of the collector. +This option must be followed by one or two extra arguments: +The name of the parameter being changed or retrieved (a string) +and an optional new value for that parameter (an integer). +The first argument must have one of the following values: @description{ @item{@St{minormul}| The minor multiplier. } @item{@St{majorminor}| The major-minor multiplier. } @@ -6405,6 +6404,10 @@ The argument @id{param} must have one of the following values: @item{@St{stepmul}| The step multiplier. } @item{@St{stepsize}| The step size. } } +The call always returns the previous value of the parameter. +If the call does not give a new value, +the value is left unchanged. + Lua rounds these values before storing them; so, the value returned as the previous value may not be exactly the last value set. @@ -9298,7 +9301,7 @@ declare a local variable with the same name in the loop body. @item{ Parameters for the garbage collection are not set with the options @St{incremental} and @St{generational}; -instead, there is a new option @St{setparam} to that end. +instead, there is a new option @St{param} to that end. Moreover, there were some changes in the parameters themselves. } @@ -9327,7 +9330,7 @@ to signal the end of the dump. @item{ Parameters for the garbage collection are not set with the options @Lid{LUA_GCINC} and @Lid{LUA_GCGEN}; -instead, there is a new option @Lid{LUA_GCSETPARAM} to that end. +instead, there is a new option @Lid{LUA_GCPARAM} to that end. Moreover, there were some changes in the parameters themselves. } diff --git a/testes/gc.lua b/testes/gc.lua index c26de40619..5b39bac11a 100644 --- a/testes/gc.lua +++ b/testes/gc.lua @@ -28,19 +28,21 @@ end -- test weird parameters to 'collectgarbage' do collectgarbage("incremental") - local opause = collectgarbage("setparam", "pause", 100) - local ostepmul = collectgarbage("setparam", "stepmul", 100) + local opause = collectgarbage("param", "pause", 100) + local ostepmul = collectgarbage("param", "stepmul", 100) + assert(collectgarbage("param", "pause") == 100) + assert(collectgarbage("param", "stepmul") == 100) local t = {0, 2, 10, 90, 500, 5000, 30000, 0x7ffffffe} for i = 1, #t do - collectgarbage("setparam", "pause", t[i]) + collectgarbage("param", "pause", t[i]) for j = 1, #t do - collectgarbage("setparam", "stepmul", t[j]) + collectgarbage("param", "stepmul", t[j]) collectgarbage("step", t[j]) end end -- restore original parameters - collectgarbage("setparam", "pause", opause) - collectgarbage("setparam", "stepmul", ostepmul) + collectgarbage("param", "pause", opause) + collectgarbage("param", "stepmul", ostepmul) collectgarbage() end diff --git a/testes/gengc.lua b/testes/gengc.lua index 51872cc11e..c4f6ca1b71 100644 --- a/testes/gengc.lua +++ b/testes/gengc.lua @@ -163,15 +163,17 @@ assert(collectgarbage'isrunning') do print"testing stop-the-world collection" - local step = collectgarbage("setparam", "stepsize", 0); + local step = collectgarbage("param", "stepsize", 0); collectgarbage("incremental") + assert(collectgarbage("param", "stepsize") == 0) -- each step does a complete cycle assert(collectgarbage("step")) assert(collectgarbage("step")) -- back to default value - collectgarbage("setparam", "stepsize", step); + collectgarbage("param", "stepsize", step); + assert(collectgarbage("param", "stepsize") == step) end collectgarbage(oldmode) From 3e9dbe143d3338f5f13a5e421ea593adff482da0 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 18 Jan 2024 15:16:26 -0300 Subject: [PATCH 0880/1145] New function 'table.create' Creates a table preallocating memory. (It just exports to Lua the API function 'lua_createtable'.) --- ltablib.c | 9 +++++++++ manual/manual.of | 17 +++++++++++++++-- testes/sort.lua | 21 +++++++++++++++++++++ 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/ltablib.c b/ltablib.c index 44d55ef511..c8838963d5 100644 --- a/ltablib.c +++ b/ltablib.c @@ -58,6 +58,14 @@ static void checktab (lua_State *L, int arg, int what) { } +static int tcreate (lua_State *L) { + int sizeseq = (int)luaL_checkinteger(L, 1); + int sizerest = (int)luaL_optinteger(L, 2, 0); + lua_createtable(L, sizeseq, sizerest); + return 1; +} + + static int tinsert (lua_State *L) { lua_Integer pos; /* where to insert new element */ lua_Integer e = aux_getn(L, 1, TAB_RW); @@ -390,6 +398,7 @@ static int sort (lua_State *L) { static const luaL_Reg tab_funcs[] = { {"concat", tconcat}, + {"create", tcreate}, {"insert", tinsert}, {"pack", tpack}, {"unpack", tunpack}, diff --git a/manual/manual.of b/manual/manual.of index 48f396d947..42269ff49d 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -3234,11 +3234,11 @@ Values at other positions are not affected. } -@APIEntry{void lua_createtable (lua_State *L, int narr, int nrec);| +@APIEntry{void lua_createtable (lua_State *L, int nseq, int nrec);| @apii{0,1,m} Creates a new empty table and pushes it onto the stack. -Parameter @id{narr} is a hint for how many elements the table +Parameter @id{nseq} is a hint for how many elements the table will have as a sequence; parameter @id{nrec} is a hint for how many other elements the table will have. @@ -7969,6 +7969,19 @@ If @id{i} is greater than @id{j}, returns the empty string. } +@LibEntry{table.create (nseq [, nrec])| + +Creates a new empty table, preallocating memory. +This preallocation may help performance and save memory +when you know in advance how many elements the table will have. + +Parameter @id{nseq} is a hint for how many elements the table +will have as a sequence. +Optional parameter @id{nrec} is a hint for how many other elements +the table will have; its default is zero. + +} + @LibEntry{table.insert (list, [pos,] value)| Inserts element @id{value} at position @id{pos} in @id{list}, diff --git a/testes/sort.lua b/testes/sort.lua index 40bb2d8a27..4501465283 100644 --- a/testes/sort.lua +++ b/testes/sort.lua @@ -3,6 +3,27 @@ print "testing (parts of) table library" +do print "testing 'table.create'" + collectgarbage() + local m = collectgarbage("count") * 1024 + local t = table.create(10000) + local memdiff = collectgarbage("count") * 1024 - m + assert(memdiff > 10000 * 4) + for i = 1, 20 do + assert(#t == i - 1) + t[i] = 0 + end + assert(not T or T.querytab(t) == 10000) + t = nil + collectgarbage() + m = collectgarbage("count") * 1024 + t = table.create(0, 1024) + memdiff = collectgarbage("count") * 1024 - m + assert(memdiff > 1024 * 12) + assert(not T or select(2, T.querytab(t)) == 1024) +end + + print "testing unpack" local unpack = table.unpack From b34a97a4af5c9e973915c07dba918d95009e0acd Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 25 Jan 2024 13:44:49 -0300 Subject: [PATCH 0881/1145] Small optimization in 'luaH_psetint' It is quite common to write to empty but existing cells in the array part of a table, so 'luaH_psetint' checks for the common case that the table doesn't have a newindex metamethod to complete the write. --- ltable.c | 2 +- ltm.h | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/ltable.c b/ltable.c index 21a54f81d3..353e567b04 100644 --- a/ltable.c +++ b/ltable.c @@ -1001,7 +1001,7 @@ static int rawfinishnodeset (const TValue *slot, TValue *val) { int luaH_psetint (Table *t, lua_Integer key, TValue *val) { if (keyinarray(t, key)) { lu_byte *tag = getArrTag(t, key - 1); - if (!tagisempty(*tag)) { + if (!tagisempty(*tag) || checknoTM(t->metatable, TM_NEWINDEX)) { fval2arr(t, key, tag, val); return HOK; /* success */ } diff --git a/ltm.h b/ltm.h index f387265585..3c49713aae 100644 --- a/ltm.h +++ b/ltm.h @@ -60,11 +60,12 @@ typedef enum { */ #define notm(tm) ttisnil(tm) +#define checknoTM(mt,e) ((mt) == NULL || (mt)->flags & (1u<<(e))) -#define gfasttm(g,et,e) ((et) == NULL ? NULL : \ - ((et)->flags & (1u<<(e))) ? NULL : luaT_gettm(et, e, (g)->tmname[e])) +#define gfasttm(g,mt,e) \ + (checknoTM(mt, e) ? NULL : luaT_gettm(mt, e, (g)->tmname[e])) -#define fasttm(l,et,e) gfasttm(G(l), et, e) +#define fasttm(l,mt,e) gfasttm(G(l), mt, e) #define ttypename(x) luaT_typenames_[(x) + 1] From c31d6774ac7db4cfbc548ce507ae65ab6036f873 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 29 Jan 2024 14:29:24 -0300 Subject: [PATCH 0882/1145] Details --- config.lua | 4 ---- lapi.c | 3 +-- lapi.h | 5 ++--- lobject.c | 26 ++++++++++++++++---------- ltm.c | 7 +++---- manual/manual.of | 2 +- testes/sort.lua | 2 ++ 7 files changed, 25 insertions(+), 24 deletions(-) delete mode 100644 config.lua diff --git a/config.lua b/config.lua deleted file mode 100644 index 14afdc8ac7..0000000000 --- a/config.lua +++ /dev/null @@ -1,4 +0,0 @@ -collectgarbage("setparam", "minormul", 25) --- collectgarbage("generational") - - diff --git a/lapi.c b/lapi.c index b8e588017d..bb76b15a25 100644 --- a/lapi.c +++ b/lapi.c @@ -1262,9 +1262,8 @@ LUA_API int lua_next (lua_State *L, int idx) { api_checknelems(L, 1); t = gettable(L, idx); more = luaH_next(L, t, L->top.p - 1); - if (more) { + if (more) api_incr_top(L); - } else /* no more elements */ L->top.p -= 1; /* remove key */ lua_unlock(L); diff --git a/lapi.h b/lapi.h index a742427cdc..4384564803 100644 --- a/lapi.h +++ b/lapi.h @@ -13,9 +13,8 @@ /* Increments 'L->top.p', checking for stack overflows */ -#define api_incr_top(L) {L->top.p++; \ - api_check(L, L->top.p <= L->ci->top.p, \ - "stack overflow");} +#define api_incr_top(L) \ + (L->top.p++, api_check(L, L->top.p <= L->ci->top.p, "stack overflow")) /* diff --git a/lobject.c b/lobject.c index 5a9b435ee0..45a2731110 100644 --- a/lobject.c +++ b/lobject.c @@ -73,17 +73,29 @@ unsigned int luaO_codeparam (unsigned int p) { /* -** Computes 'p' times 'x', where 'p' is a floating-point byte. +** Computes 'p' times 'x', where 'p' is a floating-point byte. Roughly, +** we have to multiply 'x' by the mantissa and then shift accordingly to +** the exponent. If the exponent is positive, both the multiplication +** and the shift increase 'x', so we have to care only about overflows. +** For negative exponents, however, multiplying before the shift keeps +** more significant bits, as long as the multiplication does not +** overflow, so we check which order is best. */ l_obj luaO_applyparam (unsigned int p, l_obj x) { unsigned int m = p & 0xF; /* mantissa */ int e = (p >> 4); /* exponent */ if (e > 0) { /* normalized? */ - e--; - m += 0x10; /* maximum 'm' is 0x1F */ + e--; /* correct exponent */ + m += 0x10; /* correct mantissa; maximum value is 0x1F */ } e -= 7; /* correct excess-7 */ - if (e < 0) { + if (e >= 0) { + if (x < (MAX_LOBJ / 0x1F) >> e) /* no overflow? */ + return (x * m) << e; /* order doesn't matter here */ + else /* real overflow */ + return MAX_LOBJ; + } + else { /* negative exponent */ e = -e; if (x < MAX_LOBJ / 0x1F) /* multiplication cannot overflow? */ return (x * m) >> e; /* multiplying first gives more precision */ @@ -92,12 +104,6 @@ l_obj luaO_applyparam (unsigned int p, l_obj x) { else /* real overflow */ return MAX_LOBJ; } - else { - if (x < (MAX_LOBJ / 0x1F) >> e) /* no overflow? */ - return (x * m) << e; /* order doesn't matter here */ - else /* real overflow */ - return MAX_LOBJ; - } } diff --git a/ltm.c b/ltm.c index c943bc7b14..c28f9122ee 100644 --- a/ltm.c +++ b/ltm.c @@ -92,10 +92,9 @@ const char *luaT_objtypename (lua_State *L, const TValue *o) { Table *mt; if ((ttistable(o) && (mt = hvalue(o)->metatable) != NULL) || (ttisfulluserdata(o) && (mt = uvalue(o)->metatable) != NULL)) { - TValue name; - int hres = luaH_getshortstr(mt, luaS_new(L, "__name"), &name); - if (hres == HOK && ttisstring(&name)) /* is '__name' a string? */ - return getstr(tsvalue(&name)); /* use it as type name */ + const TValue *name = luaH_Hgetshortstr(mt, luaS_new(L, "__name")); + if (ttisstring(name)) /* is '__name' a string? */ + return getstr(tsvalue(name)); /* use it as type name */ } return ttypename(ttype(o)); /* else use standard type name */ } diff --git a/manual/manual.of b/manual/manual.of index e3cbddb33f..aaaf15b787 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -6473,7 +6473,7 @@ Otherwise, returns the metatable of the given object. @LibEntry{ipairs (t)| -Returns three values (an iterator function, the table @id{t}, and 0) +Returns three values (an iterator function, the value @id{t}, and 0) so that the construction @verbatim{ for i,v in ipairs(t) do @rep{body} end diff --git a/testes/sort.lua b/testes/sort.lua index 4501465283..7e566a5a2d 100644 --- a/testes/sort.lua +++ b/testes/sort.lua @@ -13,6 +13,8 @@ do print "testing 'table.create'" assert(#t == i - 1) t[i] = 0 end + for i = 1, 20 do t[#t + 1] = i * 10 end + assert(#t == 40 and t[39] == 190) assert(not T or T.querytab(t) == 10000) t = nil collectgarbage() From 0c9bec0d38ed3d2c45d7be4e764a0bcffef98be1 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 7 Feb 2024 13:39:54 -0300 Subject: [PATCH 0883/1145] Better handling of size limit when resizing a table Avoid silent conversions from int to unsigned int when calling 'luaH_resize'; avoid silent conversions from lua_Integer to int in 'table.create'; MAXASIZE corrected for the new implementation of arrays; 'luaH_resize' checks explicitly whether new size respects MAXASIZE. (Even constructors were bypassing that check.) --- lapi.c | 2 +- ltable.c | 17 +++++++++++++---- ltablib.c | 6 ++++-- lua.h | 2 +- manual/manual.of | 2 +- testes/sort.lua | 29 +++++++++++++++++------------ 6 files changed, 37 insertions(+), 21 deletions(-) diff --git a/lapi.c b/lapi.c index bb76b15a25..69b890cda4 100644 --- a/lapi.c +++ b/lapi.c @@ -781,7 +781,7 @@ LUA_API int lua_rawgetp (lua_State *L, int idx, const void *p) { } -LUA_API void lua_createtable (lua_State *L, int narray, int nrec) { +LUA_API void lua_createtable (lua_State *L, unsigned narray, unsigned nrec) { Table *t; lua_lock(L); t = luaH_new(L); diff --git a/ltable.c b/ltable.c index 353e567b04..b86e228142 100644 --- a/ltable.c +++ b/ltable.c @@ -61,18 +61,25 @@ typedef union { /* -** MAXABITS is the largest integer such that MAXASIZE fits in an +** MAXABITS is the largest integer such that 2^MAXABITS fits in an ** unsigned int. */ #define MAXABITS cast_int(sizeof(int) * CHAR_BIT - 1) +/* +** MAXASIZEB is the maximum number of elements in the array part such +** that the size of the array fits in 'size_t'. +*/ +#define MAXASIZEB ((MAX_SIZET/sizeof(ArrayCell)) * NM) + + /* ** MAXASIZE is the maximum size of the array part. It is the minimum -** between 2^MAXABITS and the maximum size that, measured in bytes, -** fits in a 'size_t'. +** between 2^MAXABITS and MAXASIZEB. */ -#define MAXASIZE luaM_limitN(1u << MAXABITS, TValue) +#define MAXASIZE \ + (((1u << MAXABITS) < MAXASIZEB) ? (1u << MAXABITS) : cast_uint(MAXASIZEB)) /* ** MAXHBITS is the largest integer such that 2^MAXHBITS fits in a @@ -663,6 +670,8 @@ void luaH_resize (lua_State *L, Table *t, unsigned int newasize, Table newt; /* to keep the new hash part */ unsigned int oldasize = setlimittosize(t); ArrayCell *newarray; + if (newasize > MAXASIZE) + luaG_runerror(L, "table overflow"); /* create new hash part with appropriate size into 'newt' */ newt.flags = 0; setnodevector(L, &newt, nhsize); diff --git a/ltablib.c b/ltablib.c index c8838963d5..2ba31a4fd5 100644 --- a/ltablib.c +++ b/ltablib.c @@ -59,8 +59,10 @@ static void checktab (lua_State *L, int arg, int what) { static int tcreate (lua_State *L) { - int sizeseq = (int)luaL_checkinteger(L, 1); - int sizerest = (int)luaL_optinteger(L, 2, 0); + lua_Unsigned sizeseq = (lua_Unsigned)luaL_checkinteger(L, 1); + lua_Unsigned sizerest = (lua_Unsigned)luaL_optinteger(L, 2, 0); + luaL_argcheck(L, sizeseq <= UINT_MAX, 1, "out of range"); + luaL_argcheck(L, sizerest <= UINT_MAX, 2, "out of range"); lua_createtable(L, sizeseq, sizerest); return 1; } diff --git a/lua.h b/lua.h index 58f3164638..b8e0c571cb 100644 --- a/lua.h +++ b/lua.h @@ -268,7 +268,7 @@ LUA_API int (lua_rawget) (lua_State *L, int idx); LUA_API int (lua_rawgeti) (lua_State *L, int idx, lua_Integer n); LUA_API int (lua_rawgetp) (lua_State *L, int idx, const void *p); -LUA_API void (lua_createtable) (lua_State *L, int narr, int nrec); +LUA_API void (lua_createtable) (lua_State *L, unsigned narr, unsigned nrec); LUA_API void *(lua_newuserdatauv) (lua_State *L, size_t sz, int nuvalue); LUA_API int (lua_getmetatable) (lua_State *L, int objindex); LUA_API int (lua_getiuservalue) (lua_State *L, int idx, int n); diff --git a/manual/manual.of b/manual/manual.of index aaaf15b787..cdd54f6618 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -3234,7 +3234,7 @@ Values at other positions are not affected. } -@APIEntry{void lua_createtable (lua_State *L, int nseq, int nrec);| +@APIEntry{void lua_createtable (lua_State *L, unsigned nseq, unsigned nrec);| @apii{0,1,m} Creates a new empty table and pushes it onto the stack. diff --git a/testes/sort.lua b/testes/sort.lua index 7e566a5a2d..442b3129e4 100644 --- a/testes/sort.lua +++ b/testes/sort.lua @@ -3,19 +3,30 @@ print "testing (parts of) table library" +local maxI = math.maxinteger +local minI = math.mininteger + + +local function checkerror (msg, f, ...) + local s, err = pcall(f, ...) + assert(not s and string.find(err, msg)) +end + + do print "testing 'table.create'" + local N = 10000 collectgarbage() local m = collectgarbage("count") * 1024 - local t = table.create(10000) + local t = table.create(N) local memdiff = collectgarbage("count") * 1024 - m - assert(memdiff > 10000 * 4) + assert(memdiff > N * 4) for i = 1, 20 do assert(#t == i - 1) t[i] = 0 end for i = 1, 20 do t[#t + 1] = i * 10 end assert(#t == 40 and t[39] == 190) - assert(not T or T.querytab(t) == 10000) + assert(not T or T.querytab(t) == N) t = nil collectgarbage() m = collectgarbage("count") * 1024 @@ -23,6 +34,9 @@ do print "testing 'table.create'" memdiff = collectgarbage("count") * 1024 - m assert(memdiff > 1024 * 12) assert(not T or select(2, T.querytab(t)) == 1024) + + checkerror("table overflow", table.create, (1<<31) + 1) + checkerror("table overflow", table.create, 0, (1<<31) + 1) end @@ -30,15 +44,6 @@ print "testing unpack" local unpack = table.unpack -local maxI = math.maxinteger -local minI = math.mininteger - - -local function checkerror (msg, f, ...) - local s, err = pcall(f, ...) - assert(not s and string.find(err, msg)) -end - checkerror("wrong number of arguments", table.insert, {}, 2, 3, 4) From 6063c47031afa2d62e6038fcf8f3c805785c7df3 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 7 Feb 2024 13:56:39 -0300 Subject: [PATCH 0884/1145] Field 'lastfree' changed (back) to 'Node *' Due to allignment, it is already using the space of a pointer, and a pointer generates slightly simpler code. --- ltable.c | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/ltable.c b/ltable.c index b86e228142..dc4621aae1 100644 --- a/ltable.c +++ b/ltable.c @@ -40,24 +40,26 @@ /* -** Only tables with hash parts larget than LIMFORLAST has a 'lastfree' -** field that optimizes finding a free slot. Smaller tables do a +** Only tables with hash parts larger than 2^LIMFORLAST has a 'lastfree' +** field that optimizes finding a free slot. That field is stored just +** before the array of nodes, in the same block. Smaller tables do a ** complete search when looking for a free slot. */ -#define LLIMFORLAST 2 /* log2 of LIMTFORLAST */ -#define LIMFORLAST twoto(LLIMFORLAST) +#define LIMFORLAST 2 /* log2 of real limit */ /* -** Union to store an int field ensuring that what follows it in -** memory is properly aligned to store a TValue. +** The union 'Limbox' stores 'lastfree' and ensures that what follows it +** is properly aligned to store a Node. */ +typedef struct { Node *dummy; Node follows_pNode; } Limbox_aux; + typedef union { - int lastfree; - char padding[offsetof(struct { int i; TValue v; }, v)]; + Node *lastfree; + char padding[offsetof(Limbox_aux, follows_pNode)]; } Limbox; -#define haslastfree(t) ((t)->lsizenode > LLIMFORLAST) -#define getlastfree(t) (&((cast(Limbox *, (t)->node) - 1)->lastfree)) +#define haslastfree(t) ((t)->lsizenode > LIMFORLAST) +#define getlastfree(t) ((cast(Limbox *, (t)->node) - 1)->lastfree) /* @@ -593,13 +595,13 @@ static void setnodevector (lua_State *L, Table *t, unsigned int size) { if (lsize > MAXHBITS || (1u << lsize) > MAXHSIZE) luaG_runerror(L, "table overflow"); size = twoto(lsize); - if (lsize <= LLIMFORLAST) /* no 'lastfree' field? */ + if (lsize <= LIMFORLAST) /* no 'lastfree' field? */ t->node = luaM_newvector(L, size, Node); else { size_t bsize = size * sizeof(Node) + sizeof(Limbox); char *node = luaM_newblock(L, bsize); t->node = cast(Node *, node + sizeof(Limbox)); - *getlastfree(t) = size; /* all positions are free */ + getlastfree(t) = gnode(t, size); /* all positions are free */ } t->lsizenode = cast_byte(lsize); setnodummy(t); @@ -776,8 +778,8 @@ void luaH_free (lua_State *L, Table *t) { static Node *getfreepos (Table *t) { if (haslastfree(t)) { /* does it have 'lastfree' information? */ /* look for a spot before 'lastfree', updating 'lastfree' */ - while (*getlastfree(t) > 0) { - Node *free = gnode(t, --(*getlastfree(t))); + while (getlastfree(t) > t->node) { + Node *free = --getlastfree(t); if (keyisnil(free)) return free; } From 7360f8d0fd91344deb583ff76b8250a1883dcd4c Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 7 Feb 2024 14:20:43 -0300 Subject: [PATCH 0885/1145] Removed deprecated function 'setcstacklimit' --- ldblib.c | 9 --------- lstate.c | 6 ------ lua.h | 1 - 3 files changed, 16 deletions(-) diff --git a/ldblib.c b/ldblib.c index 6dcbaa9824..2c94138472 100644 --- a/ldblib.c +++ b/ldblib.c @@ -446,14 +446,6 @@ static int db_traceback (lua_State *L) { } -static int db_setcstacklimit (lua_State *L) { - int limit = (int)luaL_checkinteger(L, 1); - int res = lua_setcstacklimit(L, limit); - lua_pushinteger(L, res); - return 1; -} - - static const luaL_Reg dblib[] = { {"debug", db_debug}, {"getuservalue", db_getuservalue}, @@ -471,7 +463,6 @@ static const luaL_Reg dblib[] = { {"setmetatable", db_setmetatable}, {"setupvalue", db_setupvalue}, {"traceback", db_traceback}, - {"setcstacklimit", db_setcstacklimit}, {NULL, NULL} }; diff --git a/lstate.c b/lstate.c index 9a61cd6d5d..2ae76d8c8a 100644 --- a/lstate.c +++ b/lstate.c @@ -66,12 +66,6 @@ void luaE_setdebt (global_State *g, l_obj debt) { } -LUA_API int lua_setcstacklimit (lua_State *L, unsigned int limit) { - UNUSED(L); UNUSED(limit); - return LUAI_MAXCCALLS; /* warning?? */ -} - - CallInfo *luaE_extendCI (lua_State *L) { CallInfo *ci; lua_assert(L->ci->next == NULL); diff --git a/lua.h b/lua.h index b8e0c571cb..face93fa52 100644 --- a/lua.h +++ b/lua.h @@ -489,7 +489,6 @@ LUA_API lua_Hook (lua_gethook) (lua_State *L); LUA_API int (lua_gethookmask) (lua_State *L); LUA_API int (lua_gethookcount) (lua_State *L); -LUA_API int (lua_setcstacklimit) (lua_State *L, unsigned int limit); struct lua_Debug { int event; From c8121ce34b39c6fd31899f4da91e26063c8af54f Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 12 Feb 2024 15:16:11 -0300 Subject: [PATCH 0886/1145] Revising code for Varint encoding in dumps - Usign lua_Unsigned to count strings. - Varint uses a type large enough both for size_t and lua_Unsigned. - Most-Significant Bit 0 means last byte, to conform to common usage. - (unrelated) Change in macro 'getaddr' so that multiplication is by constants. --- ldump.c | 35 ++++++++++++++++++----------------- lundump.c | 24 ++++++++++++------------ lundump.h | 15 +++++++++++++++ 3 files changed, 45 insertions(+), 29 deletions(-) diff --git a/ldump.c b/ldump.c index b31e7bc797..34cfb5761b 100644 --- a/ldump.c +++ b/ldump.c @@ -30,7 +30,7 @@ typedef struct { int strip; int status; Table *h; /* table to track saved strings */ - lua_Integer nstr; /* counter to number saved strings */ + lua_Unsigned nstr; /* counter to number saved strings */ } DumpState; @@ -83,26 +83,27 @@ static void dumpByte (DumpState *D, int y) { /* -** 'dumpSize' buffer size: each byte can store up to 7 bits. (The "+6" -** rounds up the division.) +** size for 'dumpVarint' buffer: each byte can store up to 7 bits. +** (The "+6" rounds up the division.) */ -#define DIBS ((sizeof(size_t) * CHAR_BIT + 6) / 7) +#define DIBS ((sizeof(varint_t) * CHAR_BIT + 6) / 7) -static void dumpSize (DumpState *D, size_t x) { +/* +** Dumps an unsigned integer using the MSB Varint encoding +*/ +static void dumpVarint (DumpState *D, varint_t x) { lu_byte buff[DIBS]; - int n = 0; - do { - buff[DIBS - (++n)] = x & 0x7f; /* fill buffer in reverse order */ - x >>= 7; - } while (x != 0); - buff[DIBS - 1] |= 0x80; /* mark last byte */ + int n = 1; + buff[DIBS - 1] = x & 0x7f; /* fill least-significant byte */ + while ((x >>= 7) != 0) /* fill other bytes in reverse order */ + buff[DIBS - (++n)] = (x & 0x7f) | 0x80; dumpVector(D, buff + DIBS - n, n); } static void dumpInt (DumpState *D, int x) { lua_assert(x >= 0); - dumpSize(D, x); + dumpVarint(D, x); } @@ -125,22 +126,22 @@ static void dumpInteger (DumpState *D, lua_Integer x) { */ static void dumpString (DumpState *D, TString *ts) { if (ts == NULL) - dumpSize(D, 0); + dumpVarint(D, 0); else { TValue idx; if (luaH_getstr(D->h, ts, &idx) == HOK) { /* string already saved? */ - dumpSize(D, 1); /* reuse a saved string */ - dumpInt(D, ivalue(&idx)); /* index of saved string */ + dumpVarint(D, 1); /* reuse a saved string */ + dumpVarint(D, l_castS2U(ivalue(&idx))); /* index of saved string */ } else { /* must write and save the string */ TValue key, value; /* to save the string in the hash */ size_t size; const char *s = getlstr(ts, size); - dumpSize(D, size + 2); + dumpVarint(D, size + 2); dumpVector(D, s, size + 1); /* include ending '\0' */ D->nstr++; /* one more saved string */ setsvalue(D->L, &key, ts); /* the string is the key */ - setivalue(&value, D->nstr); /* its index is the value */ + setivalue(&value, l_castU2S(D->nstr)); /* its index is the value */ luaH_set(D->L, D->h, &key, &value); /* h[ts] = nstr */ /* integer value does not need barrier */ } diff --git a/lundump.c b/lundump.c index b33258b03d..d485f266cf 100644 --- a/lundump.c +++ b/lundump.c @@ -37,7 +37,7 @@ typedef struct { const char *name; Table *h; /* list for string reuse */ size_t offset; /* current position relative to beginning of dump */ - lua_Integer nstr; /* number of strings in the list */ + lua_Unsigned nstr; /* number of strings in the list */ lu_byte fixed; /* dump is fixed in memory */ } LoadState; @@ -71,10 +71,9 @@ static void loadAlign (LoadState *S, int align) { } -#define getaddr(S,n,t) cast(t *, getaddr_(S,n,sizeof(t))) +#define getaddr(S,n,t) cast(t *, getaddr_(S,(n) * sizeof(t))) -static const void *getaddr_ (LoadState *S, int n, size_t sz) { - size_t size = n * sz; +static const void *getaddr_ (LoadState *S, size_t size) { const void *block = luaZ_getaddr(S->Z, size); S->offset += size; if (block == NULL) @@ -95,8 +94,8 @@ static lu_byte loadByte (LoadState *S) { } -static size_t loadUnsigned (LoadState *S, size_t limit) { - size_t x = 0; +static varint_t loadVarint (LoadState *S, varint_t limit) { + varint_t x = 0; int b; limit >>= 7; do { @@ -104,18 +103,18 @@ static size_t loadUnsigned (LoadState *S, size_t limit) { if (x >= limit) error(S, "integer overflow"); x = (x << 7) | (b & 0x7f); - } while ((b & 0x80) == 0); + } while ((b & 0x80) != 0); return x; } static size_t loadSize (LoadState *S) { - return loadUnsigned(S, MAX_SIZET); + return cast_sizet(loadVarint(S, MAX_SIZET)); } static int loadInt (LoadState *S) { - return cast_int(loadUnsigned(S, INT_MAX)); + return cast_int(loadVarint(S, INT_MAX)); } @@ -149,9 +148,10 @@ static void loadString (LoadState *S, Proto *p, TString **sl) { return; } else if (size == 1) { /* previously saved string? */ - int idx = loadInt(S); /* get its index */ + /* get its index */ + lua_Unsigned idx = cast(lua_Unsigned, loadVarint(S, LUA_MAXUNSIGNED)); TValue stv; - luaH_getint(S->h, idx, &stv); + luaH_getint(S->h, l_castU2S(idx), &stv); /* get its value */ *sl = ts = tsvalue(&stv); luaC_objbarrier(L, p, ts); return; /* do not save it again */ @@ -175,7 +175,7 @@ static void loadString (LoadState *S, Proto *p, TString **sl) { /* add string to list of saved strings */ S->nstr++; setsvalue(L, &sv, ts); - luaH_setint(L, S->h, S->nstr, &sv); + luaH_setint(L, S->h, l_castU2S(S->nstr), &sv); luaC_objbarrierback(L, obj2gco(S->h), ts); } diff --git a/lundump.h b/lundump.h index b10307e495..ff66d2e780 100644 --- a/lundump.h +++ b/lundump.h @@ -7,6 +7,8 @@ #ifndef lundump_h #define lundump_h +#include + #include "llimits.h" #include "lobject.h" #include "lzio.h" @@ -25,6 +27,19 @@ #define LUAC_FORMAT 0 /* this is the official format */ + +/* +** Type to handle MSB Varint encoding: Try to get the largest unsigned +** integer available. (It was enough to be the largest between size_t and +** lua_Integer, but the C89 preprocessor knows nothing about size_t.) +*/ +#if !defined(LUA_USE_C89) && defined(LLONG_MAX) +typedef unsigned long long varint_t; +#else +typedef unsigned long varint_t; +#endif + + /* load one chunk; from lundump.c */ LUAI_FUNC LClosure* luaU_undump (lua_State* L, ZIO* Z, const char* name, int fixed); From 165389b27bc54e7c5214276db177e3ef75226f18 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 15 Feb 2024 11:17:39 -0300 Subject: [PATCH 0887/1145] New interface to function 'luaL_openselectedlibs' Instead of preloading all non-loaded libraries, there is another mask to select which libraries to preload. --- linit.c | 22 ++++++------ ltests.c | 5 +-- lua.c | 2 +- lualib.h | 8 ++--- manual/2html | 2 +- manual/manual.of | 85 ++++++++++++++++++++++++++++---------------- testes/api.lua | 10 +++--- testes/coroutine.lua | 2 +- 8 files changed, 80 insertions(+), 56 deletions(-) diff --git a/linit.c b/linit.c index 675fb65fbb..140f6d7590 100644 --- a/linit.c +++ b/linit.c @@ -21,12 +21,12 @@ /* -** Standard Libraries +** Standard Libraries. (Must be listed in the same ORDER of their +** respective constants LUA_K.) */ static const luaL_Reg stdlibs[] = { {LUA_GNAME, luaopen_base}, {LUA_LOADLIBNAME, luaopen_package}, - {LUA_COLIBNAME, luaopen_coroutine}, {LUA_DBLIBNAME, luaopen_debug}, {LUA_IOLIBNAME, luaopen_io}, @@ -35,30 +35,28 @@ static const luaL_Reg stdlibs[] = { {LUA_STRLIBNAME, luaopen_string}, {LUA_TABLIBNAME, luaopen_table}, {LUA_UTF8LIBNAME, luaopen_utf8}, - {NULL, NULL} }; /* -** require selected standard libraries and add the others to the -** preload table. +** require and preload selected standard libraries */ -LUALIB_API void luaL_openselectedlibs (lua_State *L, int what) { - int mask = 1; +LUALIB_API void luaL_openselectedlibs (lua_State *L, int load, int preload) { + int mask; const luaL_Reg *lib; luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE); - for (lib = stdlibs; lib->func; (lib++, mask <<= 1)) { - if (what & mask) { /* selected? */ + for (lib = stdlibs, mask = 1; lib->name != NULL; lib++, mask <<= 1) { + if (load & mask) { /* selected? */ luaL_requiref(L, lib->name, lib->func, 1); /* require library */ lua_pop(L, 1); /* remove result from the stack */ } - else { /* add library to PRELOAD table */ + else if (preload & mask) { /* selected? */ lua_pushcfunction(L, lib->func); - lua_setfield(L, -2, lib->name); + lua_setfield(L, -2, lib->name); /* add library to PRELOAD table */ } } lua_assert((mask >> 1) == LUA_UTF8LIBK); - lua_pop(L, 1); // remove PRELOAD table + lua_pop(L, 1); /* remove PRELOAD table */ } diff --git a/ltests.c b/ltests.c index 6081aba622..59df7cadc8 100644 --- a/ltests.c +++ b/ltests.c @@ -1223,8 +1223,9 @@ static lua_State *getstate (lua_State *L) { static int loadlib (lua_State *L) { lua_State *L1 = getstate(L); - int what = luaL_checkinteger(L, 2); - luaL_openselectedlibs(L1, what); + int load = luaL_checkinteger(L, 2); + int preload = luaL_checkinteger(L, 3); + luaL_openselectedlibs(L1, load, preload); luaL_requiref(L1, "T", luaB_opentests, 0); lua_assert(lua_type(L1, -1) == LUA_TTABLE); /* 'requiref' should not reload module already loaded... */ diff --git a/lua.c b/lua.c index e574ec9bb7..6a9bb94894 100644 --- a/lua.c +++ b/lua.c @@ -618,7 +618,7 @@ static void doREPL (lua_State *L) { /* }================================================================== */ #if !defined(luai_openlibs) -#define luai_openlibs(L) luaL_openlibs(L) +#define luai_openlibs(L) luaL_openselectedlibs(L, ~0, 0) #endif diff --git a/lualib.h b/lualib.h index e124cf1b85..068f60ab3b 100644 --- a/lualib.h +++ b/lualib.h @@ -14,11 +14,11 @@ /* version suffix for environment variable names */ #define LUA_VERSUFFIX "_" LUA_VERSION_MAJOR "_" LUA_VERSION_MINOR -#define LUA_GK 1 +#define LUA_GLIBK 1 LUAMOD_API int (luaopen_base) (lua_State *L); #define LUA_LOADLIBNAME "package" -#define LUA_LOADLIBK (LUA_GK << 1) +#define LUA_LOADLIBK (LUA_GLIBK << 1) LUAMOD_API int (luaopen_package) (lua_State *L); @@ -56,10 +56,10 @@ LUAMOD_API int (luaopen_utf8) (lua_State *L); /* open selected libraries */ -LUALIB_API void (luaL_openselectedlibs) (lua_State *L, int what); +LUALIB_API void (luaL_openselectedlibs) (lua_State *L, int load, int preload); /* open all libraries */ -#define luaL_openlibs(L) luaL_openselectedlibs(L, ~0) +#define luaL_openlibs(L) luaL_openselectedlibs(L, ~0, 0) #endif diff --git a/manual/2html b/manual/2html index 43fd89133b..bada6ee0e4 100755 --- a/manual/2html +++ b/manual/2html @@ -358,7 +358,7 @@ item = function (s) local t, p = string.match(s, "^([^\n|]+)|()") if t then s = string.sub(s, p) - s = Tag.b(t..": ") .. s + s = Tag.b(t) ..": " .. s end return Tag.li(fixpara(s)) end, diff --git a/manual/manual.of b/manual/manual.of index cdd54f6618..3181549db0 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -664,7 +664,6 @@ Values equal to or less than 100 mean the collector will not wait to start a new cycle. A value of 200 means that the collector waits for the total number of objects to double before starting a new cycle. -The default value is 200. The garbage-collector step size controls the size of each incremental step, @@ -672,7 +671,6 @@ specifically how many objects the interpreter creates before performing a step: A value of @M{n} means the interpreter will create approximately @M{n} objects between steps. -The default value is 250. The garbage-collector step multiplier controls the size of each GC step. @@ -681,7 +679,6 @@ in each step, @M{n%} objects for each created object. Larger values make the collector more aggressive. Beware that values too small can make the collector too slow to ever finish a cycle. -The default value is 200. As a special case, a zero value means unlimited work, effectively producing a non-incremental, stop-the-world collector. @@ -711,7 +708,6 @@ after the last major collection. For instance, for a multiplier of 20, the collector will do a minor collection when the number of objects gets 20% larger than the total after the last major collection. -The default value is 25. The minor-major multiplier controls the shift to major collections. For a multiplier @M{x}, @@ -721,7 +717,6 @@ than the total after the previous major collection. For instance, for a multiplier of 100, the collector will do a major collection when the number of old objects gets larger than twice the total after the previous major collection. -The default value is 100. The major-minor multiplier controls the shift back to minor collections. For a multiplier @M{x}, @@ -731,7 +726,6 @@ of the objects allocated during the last cycle. In particular, for a multiplier of 0, the collector will immediately shift back to minor collections after doing one cycle of major collections. -The default value is 50. } @@ -5885,13 +5879,6 @@ or @id{NULL} if there is a @x{memory allocation error}. } -@APIEntry{void luaL_openlibs (lua_State *L);| -@apii{0,0,e} - -Opens all standard Lua libraries into the given state. - -} - @APIEntry{ T luaL_opt (L, func, arg, dflt);| @apii{0,0,-} @@ -6073,7 +6060,7 @@ and sets the call result to @T{package.loaded[modname]}, as if that function has been called through @Lid{require}. If @id{glb} is true, -also stores the module into the global @id{modname}. +also stores the module into the global variable @id{modname}. Leaves a copy of the module on the stack. @@ -6290,23 +6277,61 @@ Except for the basic and the package libraries, each library provides all its functions as fields of a global table or as methods of its objects. -To have access to these libraries, -the @N{C host} program should call the @Lid{luaL_openlibs} function, -which opens all standard libraries. +} + + +@sect2{lualib-h| @title{Loading the Libraries in C code} + +A @N{C host} program must explicitly load +the standard libraries into a state, +if it wants its scripts to use them. +For that, +the host program can call the function @Lid{luaL_openlibs}. Alternatively, -the host program can open them individually by using -@Lid{luaL_requiref} to call -@defid{luaopen_base} (for the basic library), -@defid{luaopen_package} (for the package library), -@defid{luaopen_coroutine} (for the coroutine library), -@defid{luaopen_string} (for the string library), -@defid{luaopen_utf8} (for the UTF-8 library), -@defid{luaopen_table} (for the table library), -@defid{luaopen_math} (for the mathematical library), -@defid{luaopen_io} (for the I/O library), -@defid{luaopen_os} (for the operating system library), -and @defid{luaopen_debug} (for the debug library). -These functions are declared in @defid{lualib.h}. +the host can select which libraries to open, +by using @Lid{luaL_openselectedlibs}. +Both functions are defined in the header file @id{lualib.h}. +@index{lualib.h} + +The stand-alone interpreter @id{lua} @see{lua-sa} +already opens all standard libraries. + +@APIEntry{void luaL_openlibs (lua_State *L);| +@apii{0,0,e} + +Opens all standard Lua libraries into the given state. + +} + +@APIEntry{void luaL_openselectedlibs (lua_State *L, int load, int preload);| +@apii{0,0,e} + +Opens (loads) and preloads selected libraries into the state @id{L}. +(To @emph{preload} means to add +the library loader into the table @Lid{package.preload}, +so that the library can be required later by the program. +Keep in mind that @Lid{require} itself is provided +by the @emph{package} library. +If a program does not load that library, +it will be unable to require anything.) + +The integer @id{load} selects which libraries to load; +the integer @id{preload} selects which to preload, among those not loaded. +Both are masks formed by a bitwise OR of the following constants: +@description{ +@item{@defid{LUA_GLIBK} | the basic library.} +@item{@defid{LUA_LOADLIBK} | the package library.} +@item{@defid{LUA_COLIBK} | the coroutine library.} +@item{@defid{LUA_STRLIBK} | the string library.} +@item{@defid{LUA_UTF8LIBK} | the UTF-8 library.} +@item{@defid{LUA_TABLIBK} | the table library.} +@item{@defid{LUA_MATHLIBK} | the mathematical library.} +@item{@defid{LUA_IOLIBK} | the I/O library.} +@item{@defid{LUA_OSLIBK} | the operating system library.} +@item{@defid{LUA_DBLIBK} | the debug library.} +} + +} } diff --git a/testes/api.lua b/testes/api.lua index ca4b3fb47f..eec9c0abd9 100644 --- a/testes/api.lua +++ b/testes/api.lua @@ -546,9 +546,9 @@ do ]], source) collectgarbage() local m2 = collectgarbage"count" * 1024 - -- load used fewer than 350 bytes. Code alone has more than 3*N bytes, + -- load used fewer than 400 bytes. Code alone has more than 3*N bytes, -- and string literal has N bytes. Both were not loaded. - assert(m2 > m1 and m2 - m1 < 350) + assert(m2 > m1 and m2 - m1 < 400) X = 0; code(); assert(X == N and Y == string.rep("a", N)) X = nil; Y = nil @@ -1122,7 +1122,7 @@ assert(a == nil and c == 2) -- 2 == run-time error a, b, c = T.doremote(L1, "return a+") assert(a == nil and c == 3 and type(b) == "string") -- 3 == syntax error -T.loadlib(L1, 2) -- load only 'package' +T.loadlib(L1, 2, ~2) -- load only 'package', preload all others a, b, c = T.doremote(L1, [[ string = require'string' local initialG = _G -- not loaded yet @@ -1141,7 +1141,7 @@ T.closestate(L1); L1 = T.newstate() -T.loadlib(L1, 0) +T.loadlib(L1, 0, 0) T.doremote(L1, "a = {}") T.testC(L1, [[getglobal "a"; pushstring "x"; pushint 1; settable -3]]) @@ -1524,7 +1524,7 @@ end do -- garbage collection with no extra memory local L = T.newstate() - T.loadlib(L, 1 | 2) -- load _G and 'package' + T.loadlib(L, 1 | 2, 0) -- load _G and 'package' local res = (T.doremote(L, [[ _ENV = _G assert(string == nil) diff --git a/testes/coroutine.lua b/testes/coroutine.lua index 664ef5fabc..c1252ab89f 100644 --- a/testes/coroutine.lua +++ b/testes/coroutine.lua @@ -705,7 +705,7 @@ else T.testC(state, "settop 0") - T.loadlib(state, 1 | 2) -- load _G and 'package' + T.loadlib(state, 1 | 2, 4) -- load _G and 'package', preload 'coroutine' assert(T.doremote(state, [[ coroutine = require'coroutine'; From 7237eb3f1c480d6bc7fe2832ddd36f2137fb69d9 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 15 Feb 2024 11:18:34 -0300 Subject: [PATCH 0888/1145] Fixed warnings from different compilers --- lapi.c | 2 +- lauxlib.c | 9 ++++++--- lmathlib.c | 2 +- ltable.c | 3 ++- 4 files changed, 10 insertions(+), 6 deletions(-) diff --git a/lapi.c b/lapi.c index 69b890cda4..b2b82cd7ea 100644 --- a/lapi.c +++ b/lapi.c @@ -1221,7 +1221,7 @@ LUA_API int lua_gc (lua_State *L, int what, ...) { int param = va_arg(argp, int); int value = va_arg(argp, int); api_check(L, 0 <= param && param < LUA_GCPN, "invalid parameter"); - res = luaO_applyparam(g->gcparams[param], 100); + res = cast_int(luaO_applyparam(g->gcparams[param], 100)); if (value >= 0) g->gcparams[param] = luaO_codeparam(value); break; diff --git a/lauxlib.c b/lauxlib.c index f48060ed91..634c85cd2d 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -1131,8 +1131,11 @@ static void warnfon (void *ud, const char *message, int tocont) { /* Size for the buffer in int's, rounded up */ #define BUFSEED ((BUFSEEDB + sizeof(int) - 1) / sizeof(int)) - -#define addbuff(b,v) (memcpy(b, &(v), sizeof(v)), b += sizeof(v)) +/* +** Copy the contents of variable 'v' into the buffer pointed by 'b'. +** (The '&b[0]' disguises 'b' to fix an absurd warning from clang.) +*/ +#define addbuff(b,v) (memcpy(&b[0], &(v), sizeof(v)), b += sizeof(v)) static unsigned int luai_makeseed (void) { @@ -1146,7 +1149,7 @@ static unsigned int luai_makeseed (void) { /* fill (rare but possible) remain of the buffer with zeros */ memset(b, 0, sizeof(buff) - BUFSEEDB); res = buff[0]; - for (i = 0; i < BUFSEED; i++) + for (i = 1; i < BUFSEED; i++) res ^= (res >> 3) + (res << 7) + buff[i]; return res; } diff --git a/lmathlib.c b/lmathlib.c index c0a75f0656..c1041f37b5 100644 --- a/lmathlib.c +++ b/lmathlib.c @@ -352,7 +352,7 @@ static lua_Number I2d (Rand64 x) { SRand64 sx = (SRand64)(trim64(x) >> shift64_FIG); lua_Number res = (lua_Number)(sx) * scaleFIG; if (sx < 0) - res += 1.0; /* correct the two's complement if negative */ + res += l_mathop(1.0); /* correct the two's complement if negative */ lua_assert(0 <= res && res < 1); return res; } diff --git a/ltable.c b/ltable.c index dc4621aae1..cb7eb6488b 100644 --- a/ltable.c +++ b/ltable.c @@ -995,7 +995,8 @@ static int finishnodeset (Table *t, const TValue *slot, TValue *val) { } else if (isabstkey(slot)) return HNOTFOUND; /* no slot with that key */ - else return (cast(Node*, slot) - t->node) + HFIRSTNODE; /* node encoded */ + else /* return node encoded */ + return cast_int((cast(Node*, slot) - t->node)) + HFIRSTNODE; } From 65b07dd53d7938a60112fc4473f5cad3473e3534 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 11 Mar 2024 14:05:06 -0300 Subject: [PATCH 0889/1145] API asserts for illegal pops of to-be-closed variables --- lapi.c | 52 ++++++++++++++++++++++++++------------------------ lapi.h | 14 ++++++++++++-- ldebug.c | 1 + ldo.c | 5 +++-- testes/api.lua | 3 ++- 5 files changed, 45 insertions(+), 30 deletions(-) diff --git a/lapi.c b/lapi.c index b2b82cd7ea..7df637985e 100644 --- a/lapi.c +++ b/lapi.c @@ -139,7 +139,7 @@ LUA_API void lua_xmove (lua_State *from, lua_State *to, int n) { int i; if (from == to) return; lua_lock(to); - api_checknelems(from, n); + api_checkpop(from, n); api_check(from, G(from) == G(to), "moving among independent states"); api_check(from, to->ci->top.p - to->top.p >= n, "stack overflow"); from->top.p -= n; @@ -205,7 +205,6 @@ LUA_API void lua_settop (lua_State *L, int idx) { api_check(L, -(idx+1) <= (L->top.p - (func + 1)), "invalid new top"); diff = idx + 1; /* will "subtract" index (as it is negative) */ } - api_check(L, L->tbclist.p < L->top.p, "previous pop of an unclosed slot"); newtop = L->top.p + diff; if (diff < 0 && L->tbclist.p >= newtop) { lua_assert(hastocloseCfunc(ci->nresults)); @@ -253,6 +252,7 @@ LUA_API void lua_rotate (lua_State *L, int idx, int n) { lua_lock(L); t = L->top.p - 1; /* end of stack segment being rotated */ p = index2stack(L, idx); /* start of segment */ + api_check(L, L->tbclist.p < p, "moving a to-be-closed slot"); api_check(L, (n >= 0 ? n : -n) <= (t - p + 1), "invalid 'n'"); m = (n >= 0 ? t - n : p - n - 1); /* end of prefix */ reverse(L, p, m); /* reverse the prefix with length 'n' */ @@ -345,9 +345,9 @@ LUA_API int lua_rawequal (lua_State *L, int index1, int index2) { LUA_API void lua_arith (lua_State *L, int op) { lua_lock(L); if (op != LUA_OPUNM && op != LUA_OPBNOT) - api_checknelems(L, 2); /* all other operations expect two operands */ + api_checkpop(L, 2); /* all other operations expect two operands */ else { /* for unary operations, add fake 2nd operand */ - api_checknelems(L, 1); + api_checkpop(L, 1); setobjs2s(L, L->top.p, L->top.p - 1); api_incr_top(L); } @@ -611,17 +611,18 @@ LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) { api_incr_top(L); } else { + int i; CClosure *cl; - api_checknelems(L, n); + api_checkpop(L, n); api_check(L, n <= MAXUPVAL, "upvalue index too large"); cl = luaF_newCclosure(L, n); cl->f = fn; - L->top.p -= n; - while (n--) { - setobj2n(L, &cl->upvalue[n], s2v(L->top.p + n)); + for (i = 0; i < n; i++) { + setobj2n(L, &cl->upvalue[i], s2v(L->top.p - n + i)); /* does not need barrier because closure is white */ lua_assert(iswhite(cl)); } + L->top.p -= n; setclCvalue(L, s2v(L->top.p), cl); api_incr_top(L); luaC_checkGC(L); @@ -701,6 +702,7 @@ LUA_API int lua_gettable (lua_State *L, int idx) { int hres; TValue *t; lua_lock(L); + api_checkpop(L, 1); t = index2value(L, idx); luaV_fastget(t, s2v(L->top.p - 1), s2v(L->top.p - 1), luaH_get, hres); if (hres != HOK) @@ -751,13 +753,13 @@ l_sinline Table *gettable (lua_State *L, int idx) { LUA_API int lua_rawget (lua_State *L, int idx) { Table *t; - int hres; lua_lock(L); - api_checknelems(L, 1); + api_checkpop(L, 1); t = gettable(L, idx); - hres = luaH_get(t, s2v(L->top.p - 1), s2v(L->top.p - 1)); - L->top.p--; /* remove key */ - return finishrawget(L, hres); + if (luaH_get(t, s2v(L->top.p - 1), s2v(L->top.p - 1)) != HOK) + setnilvalue(s2v(L->top.p - 1)); + lua_unlock(L); + return ttype(s2v(L->top.p - 1)); } @@ -851,7 +853,7 @@ LUA_API int lua_getiuservalue (lua_State *L, int idx, int n) { static void auxsetstr (lua_State *L, const TValue *t, const char *k) { int hres; TString *str = luaS_new(L, k); - api_checknelems(L, 1); + api_checkpop(L, 1); luaV_fastset(t, str, s2v(L->top.p - 1), hres, luaH_psetstr); if (hres == HOK) { luaV_finishfastset(L, t, s2v(L->top.p - 1)); @@ -879,7 +881,7 @@ LUA_API void lua_settable (lua_State *L, int idx) { TValue *t; int hres; lua_lock(L); - api_checknelems(L, 2); + api_checkpop(L, 2); t = index2value(L, idx); luaV_fastset(t, s2v(L->top.p - 2), s2v(L->top.p - 1), hres, luaH_pset); if (hres == HOK) { @@ -902,7 +904,7 @@ LUA_API void lua_seti (lua_State *L, int idx, lua_Integer n) { TValue *t; int hres; lua_lock(L); - api_checknelems(L, 1); + api_checkpop(L, 1); t = index2value(L, idx); luaV_fastseti(t, n, s2v(L->top.p - 1), hres); if (hres == HOK) @@ -920,7 +922,7 @@ LUA_API void lua_seti (lua_State *L, int idx, lua_Integer n) { static void aux_rawset (lua_State *L, int idx, TValue *key, int n) { Table *t; lua_lock(L); - api_checknelems(L, n); + api_checkpop(L, n); t = gettable(L, idx); luaH_set(L, t, key, s2v(L->top.p - 1)); invalidateTMcache(t); @@ -945,7 +947,7 @@ LUA_API void lua_rawsetp (lua_State *L, int idx, const void *p) { LUA_API void lua_rawseti (lua_State *L, int idx, lua_Integer n) { Table *t; lua_lock(L); - api_checknelems(L, 1); + api_checkpop(L, 1); t = gettable(L, idx); luaH_setint(L, t, n, s2v(L->top.p - 1)); luaC_barrierback(L, obj2gco(t), s2v(L->top.p - 1)); @@ -958,7 +960,7 @@ LUA_API int lua_setmetatable (lua_State *L, int objindex) { TValue *obj; Table *mt; lua_lock(L); - api_checknelems(L, 1); + api_checkpop(L, 1); obj = index2value(L, objindex); if (ttisnil(s2v(L->top.p - 1))) mt = NULL; @@ -998,7 +1000,7 @@ LUA_API int lua_setiuservalue (lua_State *L, int idx, int n) { TValue *o; int res; lua_lock(L); - api_checknelems(L, 1); + api_checkpop(L, 1); o = index2value(L, idx); api_check(L, ttisfulluserdata(o), "full userdata expected"); if (!(cast_uint(n) - 1u < cast_uint(uvalue(o)->nuvalue))) @@ -1031,7 +1033,7 @@ LUA_API void lua_callk (lua_State *L, int nargs, int nresults, lua_lock(L); api_check(L, k == NULL || !isLua(L->ci), "cannot use continuations inside hooks"); - api_checknelems(L, nargs+1); + api_checkpop(L, nargs + 1); api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread"); checkresults(L, nargs, nresults); func = L->top.p - (nargs+1); @@ -1072,7 +1074,7 @@ LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc, lua_lock(L); api_check(L, k == NULL || !isLua(L->ci), "cannot use continuations inside hooks"); - api_checknelems(L, nargs+1); + api_checkpop(L, nargs + 1); api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread"); checkresults(L, nargs, nresults); if (errfunc == 0) @@ -1141,7 +1143,7 @@ LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data, int strip) { ptrdiff_t otop = savestack(L, L->top.p); /* original top */ TValue *f = s2v(L->top.p - 1); /* function to be dumped */ lua_lock(L); - api_checknelems(L, 1); + api_checkpop(L, 1); api_check(L, isLfunction(f), "Lua function expected"); status = luaU_dump(L, clLvalue(f)->p, writer, data, strip); L->top.p = restorestack(L, otop); /* restore top */ @@ -1244,7 +1246,7 @@ LUA_API int lua_error (lua_State *L) { TValue *errobj; lua_lock(L); errobj = s2v(L->top.p - 1); - api_checknelems(L, 1); + api_checkpop(L, 1); /* error object is the memory error message? */ if (ttisshrstring(errobj) && eqshrstr(tsvalue(errobj), G(L)->memerrmsg)) luaM_error(L); /* raise a memory error */ @@ -1259,7 +1261,7 @@ LUA_API int lua_next (lua_State *L, int idx) { Table *t; int more; lua_lock(L); - api_checknelems(L, 1); + api_checkpop(L, 1); t = gettable(L, idx); more = luaH_next(L, t, L->top.p - 1); if (more) diff --git a/lapi.h b/lapi.h index 4384564803..757bf3d2e6 100644 --- a/lapi.h +++ b/lapi.h @@ -29,8 +29,18 @@ /* Ensure the stack has at least 'n' elements */ #define api_checknelems(L,n) \ - api_check(L, (n) < (L->top.p - L->ci->func.p), \ - "not enough elements in the stack") + api_check(L, (n) < (L->top.p - L->ci->func.p), \ + "not enough elements in the stack") + + +/* Ensure the stack has at least 'n' elements to be popped. (Some +** functions only update a slot after checking it for popping, but that +** is only an optimization for a pop followed by a push.) +*/ +#define api_checkpop(L,n) \ + api_check(L, (n) < L->top.p - L->ci->func.p && \ + L->tbclist.p < L->top.p - (n), \ + "not enough free elements in the stack") /* diff --git a/ldebug.c b/ldebug.c index aa3277cb9f..daa979afed 100644 --- a/ldebug.c +++ b/ldebug.c @@ -245,6 +245,7 @@ LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) { lua_lock(L); name = luaG_findlocal(L, ar->i_ci, n, &pos); if (name) { + api_checkpop(L, 1); setobjs2s(L, pos, L->top.p - 1); L->top.p--; /* pop value */ } diff --git a/ldo.c b/ldo.c index 05b14ec8a4..699a9d2a75 100644 --- a/ldo.c +++ b/ldo.c @@ -767,6 +767,7 @@ static CallInfo *findpcall (lua_State *L) { ** coroutine error handler and should not kill the coroutine.) */ static int resume_error (lua_State *L, const char *msg, int narg) { + api_checkpop(L, narg); L->top.p -= narg; /* remove args from the stack */ setsvalue2s(L, L->top.p, luaS_new(L, msg)); /* push error message */ api_incr_top(L); @@ -849,7 +850,7 @@ LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs, return resume_error(L, "C stack overflow", nargs); L->nCcalls++; luai_userstateresume(L, nargs); - api_checknelems(L, (L->status == LUA_OK) ? nargs + 1 : nargs); + api_checkpop(L, (L->status == LUA_OK) ? nargs + 1 : nargs); status = luaD_rawrunprotected(L, resume, &nargs); /* continue running after recoverable errors */ status = precover(L, status); @@ -878,7 +879,7 @@ LUA_API int lua_yieldk (lua_State *L, int nresults, lua_KContext ctx, luai_userstateyield(L, nresults); lua_lock(L); ci = L->ci; - api_checknelems(L, nresults); + api_checkpop(L, nresults); if (l_unlikely(!yieldable(L))) { if (L != G(L)->mainthread) luaG_runerror(L, "attempt to yield across a C-call boundary"); diff --git a/testes/api.lua b/testes/api.lua index eec9c0abd9..dc4852405a 100644 --- a/testes/api.lua +++ b/testes/api.lua @@ -1193,7 +1193,8 @@ do local a, b = pcall(T.makeCfunc[[ call 0 1 # create resource toclose -1 # mark it to be closed - error # resource is the error object + pushvalue -1 # replicate it as error object + error # resource right after error object ]], newresource) assert(a == false and b[1] == 11) assert(#openresource == 0) -- was closed From cc2b66c85687b095e68304c010b59851ca4093e1 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 13 Mar 2024 09:16:51 -0300 Subject: [PATCH 0890/1145] Removed type 'varint_t' size_t should be big enough to count the number of strings in a dump. (And, by definition, it is big enough to count the length of each string.) --- ldump.c | 22 +++++++++++++--------- lundump.c | 21 ++++++++++----------- lundump.h | 12 ------------ 3 files changed, 23 insertions(+), 32 deletions(-) diff --git a/ldump.c b/ldump.c index 34cfb5761b..0d20fb0af4 100644 --- a/ldump.c +++ b/ldump.c @@ -30,7 +30,7 @@ typedef struct { int strip; int status; Table *h; /* table to track saved strings */ - lua_Unsigned nstr; /* counter to number saved strings */ + lua_Integer nstr; /* counter for counting saved strings */ } DumpState; @@ -86,12 +86,12 @@ static void dumpByte (DumpState *D, int y) { ** size for 'dumpVarint' buffer: each byte can store up to 7 bits. ** (The "+6" rounds up the division.) */ -#define DIBS ((sizeof(varint_t) * CHAR_BIT + 6) / 7) +#define DIBS ((sizeof(size_t) * CHAR_BIT + 6) / 7) /* ** Dumps an unsigned integer using the MSB Varint encoding */ -static void dumpVarint (DumpState *D, varint_t x) { +static void dumpVarint (DumpState *D, size_t x) { lu_byte buff[DIBS]; int n = 1; buff[DIBS - 1] = x & 0x7f; /* fill least-significant byte */ @@ -101,9 +101,13 @@ static void dumpVarint (DumpState *D, varint_t x) { } +static void dumpSize (DumpState *D, size_t sz) { + dumpVarint(D, sz); +} + static void dumpInt (DumpState *D, int x) { lua_assert(x >= 0); - dumpVarint(D, x); + dumpVarint(D, cast(size_t, x)); } @@ -126,22 +130,22 @@ static void dumpInteger (DumpState *D, lua_Integer x) { */ static void dumpString (DumpState *D, TString *ts) { if (ts == NULL) - dumpVarint(D, 0); + dumpSize(D, 0); else { TValue idx; if (luaH_getstr(D->h, ts, &idx) == HOK) { /* string already saved? */ - dumpVarint(D, 1); /* reuse a saved string */ - dumpVarint(D, l_castS2U(ivalue(&idx))); /* index of saved string */ + dumpSize(D, 1); /* reuse a saved string */ + dumpSize(D, cast_sizet(ivalue(&idx))); /* index of saved string */ } else { /* must write and save the string */ TValue key, value; /* to save the string in the hash */ size_t size; const char *s = getlstr(ts, size); - dumpVarint(D, size + 2); + dumpSize(D, size + 2); dumpVector(D, s, size + 1); /* include ending '\0' */ D->nstr++; /* one more saved string */ setsvalue(D->L, &key, ts); /* the string is the key */ - setivalue(&value, l_castU2S(D->nstr)); /* its index is the value */ + setivalue(&value, D->nstr); /* its index is the value */ luaH_set(D->L, D->h, &key, &value); /* h[ts] = nstr */ /* integer value does not need barrier */ } diff --git a/lundump.c b/lundump.c index d485f266cf..51d5dc6645 100644 --- a/lundump.c +++ b/lundump.c @@ -36,8 +36,8 @@ typedef struct { ZIO *Z; const char *name; Table *h; /* list for string reuse */ - size_t offset; /* current position relative to beginning of dump */ - lua_Unsigned nstr; /* number of strings in the list */ + lu_mem offset; /* current position relative to beginning of dump */ + lua_Integer nstr; /* number of strings in the list */ lu_byte fixed; /* dump is fixed in memory */ } LoadState; @@ -94,13 +94,13 @@ static lu_byte loadByte (LoadState *S) { } -static varint_t loadVarint (LoadState *S, varint_t limit) { - varint_t x = 0; +static size_t loadVarint (LoadState *S, size_t limit) { + size_t x = 0; int b; limit >>= 7; do { b = loadByte(S); - if (x >= limit) + if (x > limit) error(S, "integer overflow"); x = (x << 7) | (b & 0x7f); } while ((b & 0x80) != 0); @@ -109,12 +109,12 @@ static varint_t loadVarint (LoadState *S, varint_t limit) { static size_t loadSize (LoadState *S) { - return cast_sizet(loadVarint(S, MAX_SIZET)); + return loadVarint(S, MAX_SIZET); } static int loadInt (LoadState *S) { - return cast_int(loadVarint(S, INT_MAX)); + return cast_int(loadVarint(S, cast_sizet(INT_MAX))); } @@ -148,10 +148,9 @@ static void loadString (LoadState *S, Proto *p, TString **sl) { return; } else if (size == 1) { /* previously saved string? */ - /* get its index */ - lua_Unsigned idx = cast(lua_Unsigned, loadVarint(S, LUA_MAXUNSIGNED)); + lua_Integer idx = cast(lua_Integer, loadSize(S)); /* get its index */ TValue stv; - luaH_getint(S->h, l_castU2S(idx), &stv); /* get its value */ + luaH_getint(S->h, idx, &stv); /* get its value */ *sl = ts = tsvalue(&stv); luaC_objbarrier(L, p, ts); return; /* do not save it again */ @@ -175,7 +174,7 @@ static void loadString (LoadState *S, Proto *p, TString **sl) { /* add string to list of saved strings */ S->nstr++; setsvalue(L, &sv, ts); - luaH_setint(L, S->h, l_castU2S(S->nstr), &sv); + luaH_setint(L, S->h, S->nstr, &sv); luaC_objbarrierback(L, obj2gco(S->h), ts); } diff --git a/lundump.h b/lundump.h index ff66d2e780..1d6e50ea84 100644 --- a/lundump.h +++ b/lundump.h @@ -28,18 +28,6 @@ #define LUAC_FORMAT 0 /* this is the official format */ -/* -** Type to handle MSB Varint encoding: Try to get the largest unsigned -** integer available. (It was enough to be the largest between size_t and -** lua_Integer, but the C89 preprocessor knows nothing about size_t.) -*/ -#if !defined(LUA_USE_C89) && defined(LLONG_MAX) -typedef unsigned long long varint_t; -#else -typedef unsigned long varint_t; -#endif - - /* load one chunk; from lundump.c */ LUAI_FUNC LClosure* luaU_undump (lua_State* L, ZIO* Z, const char* name, int fixed); From 52aa2b5d24c560fb4d7a642971571ff9cbeabfcd Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 13 Mar 2024 09:20:34 -0300 Subject: [PATCH 0891/1145] Details - 'unsigned int' -> 'unsigned' - Some explicit casts to avoid warnings - Test avoids printing the value of 'fail' (which may not be nil) --- lauxlib.h | 2 +- ltablib.c | 2 +- lua.h | 3 +-- testes/main.lua | 4 ++-- 4 files changed, 5 insertions(+), 6 deletions(-) diff --git a/lauxlib.h b/lauxlib.h index 0ee9a57237..3c37068682 100644 --- a/lauxlib.h +++ b/lauxlib.h @@ -100,7 +100,7 @@ LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s); LUALIB_API lua_State *(luaL_newstate) (void); -LUALIB_API unsigned int luaL_makeseed (lua_State *L); +LUALIB_API unsigned luaL_makeseed (lua_State *L); LUALIB_API lua_Integer (luaL_len) (lua_State *L, int idx); diff --git a/ltablib.c b/ltablib.c index 2ba31a4fd5..4c3f690015 100644 --- a/ltablib.c +++ b/ltablib.c @@ -63,7 +63,7 @@ static int tcreate (lua_State *L) { lua_Unsigned sizerest = (lua_Unsigned)luaL_optinteger(L, 2, 0); luaL_argcheck(L, sizeseq <= UINT_MAX, 1, "out of range"); luaL_argcheck(L, sizerest <= UINT_MAX, 2, "out of range"); - lua_createtable(L, sizeseq, sizerest); + lua_createtable(L, (unsigned)sizeseq, (unsigned)sizerest); return 1; } diff --git a/lua.h b/lua.h index face93fa52..b6934e6869 100644 --- a/lua.h +++ b/lua.h @@ -160,8 +160,7 @@ extern const char lua_ident[]; /* ** state manipulation */ -LUA_API lua_State *(lua_newstate) (lua_Alloc f, void *ud, - unsigned int seed); +LUA_API lua_State *(lua_newstate) (lua_Alloc f, void *ud, unsigned seed); LUA_API void (lua_close) (lua_State *L); LUA_API lua_State *(lua_newthread) (lua_State *L); LUA_API int (lua_closethread) (lua_State *L, lua_State *from); diff --git a/testes/main.lua b/testes/main.lua index dde72a744e..5c7d0a107c 100644 --- a/testes/main.lua +++ b/testes/main.lua @@ -312,7 +312,7 @@ setmetatable({}, {__gc = function () -- this finalizer should not be called, as object will be -- created after 'lua_close' has been called setmetatable({}, {__gc = function () print(3) end}) - print(collectgarbage()) -- cannot call collector here + print(collectgarbage() or false) -- cannot call collector here os.exit(0, true) end}) ]] @@ -322,7 +322,7 @@ creating 1 creating 2 2 creating 3 -nil +false 1 ]] From 3823fc6c814d20f2b2a0a1e3be8782084440040f Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 15 Mar 2024 11:01:34 -0300 Subject: [PATCH 0892/1145] Added "bulk operations" to arrays A few operations on arrays can be performed "in bulk", treating all tags of a cell as a simple (or a few) word(s). --- lgc.c | 59 ++++++++++++++++++++++++++++++++++++++++---------------- ltable.c | 57 ++++++++++++++++++++++++++++++++++++++++-------------- ltable.h | 18 ++++++++++++++--- 3 files changed, 99 insertions(+), 35 deletions(-) diff --git a/lgc.c b/lgc.c index f813038f34..f76e851eac 100644 --- a/lgc.c +++ b/lgc.c @@ -465,6 +465,46 @@ static void traverseweakvalue (global_State *g, Table *h) { } +#define BK2(x) cast(lua_Unsigned, ((x) << 8) | BIT_ISCOLLECTABLE) +/* +** Check whether some value in the cell starting at index 'i' +** is collectable +*/ +static int checkBulkCollectable (Table *h, unsigned i) { + const lua_Unsigned bitscoll = BK2(BK2(BK2(BK2(BK2(BK2(BK2(BK2(~0u)))))))); + int j; + i /= NM; + for (j = 0; j < BKSZ; j++) { + if (h->array[i].u.bulk[j] & bitscoll) + return 1; + } + return 0; +} + + +/* +** Traverse the array part of a table. The traversal is made by cells, +** only traversing a cell if it has some collectable tag among its tags. +*/ +static int traversearray (global_State *g, Table *h) { + unsigned asize = luaH_realasize(h); + int marked = 0; /* true if some object is marked in this traversal */ + unsigned i; + for (i = 0; i < asize; i += NM) { /* traverse array in cells */ + if (checkBulkCollectable(h, i)) { /* something to mark in this cell? */ + unsigned j; + for (j = 0; j < NM && i + j < asize; j++) { + GCObject *o = gcvalarr(h, i + j); + if (o != NULL && iswhite(o)) { + marked = 1; + reallymarkobject(g, o); + } + } + } + } + return marked; +} + /* ** Traverse an ephemeron table and link it to proper list. Returns true ** iff any object was marked during this traversal (which implies that @@ -478,20 +518,11 @@ static void traverseweakvalue (global_State *g, Table *h) { ** by 'genlink'. */ static int traverseephemeron (global_State *g, Table *h, int inv) { - int marked = 0; /* true if an object is marked in this traversal */ int hasclears = 0; /* true if table has white keys */ int hasww = 0; /* true if table has entry "white-key -> white-value" */ unsigned int i; - unsigned int asize = luaH_realasize(h); unsigned int nsize = sizenode(h); - /* traverse array part */ - for (i = 0; i < asize; i++) { - GCObject *o = gcvalarr(h, i); - if (o != NULL && iswhite(o)) { - marked = 1; - reallymarkobject(g, o); - } - } + int marked = traversearray(g, h); /* traverse array part */ /* traverse hash part; if 'inv', traverse descending (see 'convergeephemerons') */ for (i = 0; i < nsize; i++) { @@ -523,13 +554,7 @@ static int traverseephemeron (global_State *g, Table *h, int inv) { static void traversestrongtable (global_State *g, Table *h) { Node *n, *limit = gnodelast(h); - unsigned int i; - unsigned int asize = luaH_realasize(h); - for (i = 0; i < asize; i++) { /* traverse array part */ - GCObject *o = gcvalarr(h, i); - if (o != NULL && iswhite(o)) - reallymarkobject(g, o); - } + traversearray(g, h); for (n = gnode(h, 0); n < limit; n++) { /* traverse hash part */ if (isempty(gval(n))) /* entry is empty? */ clearkey(n); /* clear its key */ diff --git a/ltable.c b/ltable.c index cb7eb6488b..6eb5f3e3e1 100644 --- a/ltable.c +++ b/ltable.c @@ -653,6 +653,44 @@ static void exchangehashpart (Table *t1, Table *t2) { } +/* +** Re-insert into the new hash part of a table the elements from the +** vanishing slice of the array part. +*/ +static void reinsertOldSlice (lua_State *L, Table *t, unsigned oldasize, + unsigned newasize) { + unsigned i; + t->alimit = newasize; /* pretend array has new size... */ + for (i = newasize; i < oldasize; i++) { /* traverse vanishing slice */ + int tag = *getArrTag(t, i); + if (!tagisempty(tag)) { /* a non-empty entry? */ + TValue aux; + farr2val(t, i + 1, tag, &aux); + luaH_setint(L, t, i + 1, &aux); /* re-insert it into the table */ + } + } + t->alimit = oldasize; /* restore current size... */ +} + + +#define BK1(x) cast(lua_Unsigned, ((x) << 8) | LUA_VEMPTY) + +/* +** Clear new slice of the array, in bulk. +*/ +static void clearNewSlice (Table *t, unsigned oldasize, unsigned newasize) { + int i, j; + int firstcell = (oldasize + NM - 1) / NM; + int lastcell = cast_int((newasize + NM - 1) / NM) - 1; + for (i = firstcell; i <= lastcell; i++) { + /* empty tag repeated for all tags in a word */ + const lua_Unsigned empty = BK1(BK1(BK1(BK1(BK1(BK1(BK1(BK1(0)))))))); + for (j = 0; j < BKSZ; j++) + t->array[i].u.bulk[j] = empty; + } +} + + /* ** Resize table 't' for the new given sizes. Both allocations (for ** the hash part and for the array part) can fail, which creates some @@ -668,7 +706,6 @@ static void exchangehashpart (Table *t1, Table *t2) { */ void luaH_resize (lua_State *L, Table *t, unsigned int newasize, unsigned int nhsize) { - unsigned int i; Table newt; /* to keep the new hash part */ unsigned int oldasize = setlimittosize(t); ArrayCell *newarray; @@ -678,19 +715,10 @@ void luaH_resize (lua_State *L, Table *t, unsigned int newasize, newt.flags = 0; setnodevector(L, &newt, nhsize); if (newasize < oldasize) { /* will array shrink? */ - t->alimit = newasize; /* pretend array has new size... */ - exchangehashpart(t, &newt); /* and new hash */ /* re-insert into the new hash the elements from vanishing slice */ - for (i = newasize; i < oldasize; i++) { - int tag = *getArrTag(t, i); - if (!tagisempty(tag)) { /* a non-empty entry? */ - TValue aux; - farr2val(t, i + 1, tag, &aux); - luaH_setint(L, t, i + 1, &aux); - } - } - t->alimit = oldasize; /* restore current size... */ - exchangehashpart(t, &newt); /* and hash (in case of errors) */ + exchangehashpart(t, &newt); /* pretend table has new hash */ + reinsertOldSlice(L, t, oldasize, newasize); + exchangehashpart(t, &newt); /* restore old hash (in case of errors) */ } /* allocate new array */ newarray = resizearray(L, t, oldasize, newasize); @@ -702,8 +730,7 @@ void luaH_resize (lua_State *L, Table *t, unsigned int newasize, exchangehashpart(t, &newt); /* 't' has the new hash ('newt' has the old) */ t->array = newarray; /* set new array part */ t->alimit = newasize; - for (i = oldasize; i < newasize; i++) /* clear new slice of the array */ - *getArrTag(t, i) = LUA_VEMPTY; + clearNewSlice(t, oldasize, newasize); /* re-insert elements from old hash part into new parts */ reinsert(L, &newt, t); /* 'newt' now has the old hash */ freehash(L, &newt); /* free old hash part */ diff --git a/ltable.h b/ltable.h index 8b0340b56f..8688264c7d 100644 --- a/ltable.h +++ b/ltable.h @@ -87,20 +87,32 @@ /* -** The array part of a table is represented by an array of cells. +** The array part of a table is represented by an array of *cells*. ** Each cell is composed of NM tags followed by NM values, so that ** no space is wasted in padding. */ #define NM cast_uint(sizeof(Value)) + +/* +** A few operations on arrays can be performed "in bulk", treating all +** tags of a cell as a simple (or a few) word(s). The next constant is +** the number of words to cover the tags of a cell. (In conventional +** architectures that will be 1 or 2.) +*/ +#define BKSZ cast_int((NM - 1) / sizeof(lua_Unsigned) + 1) + struct ArrayCell { - lu_byte tag[NM]; + union { + lua_Unsigned bulk[BKSZ]; /* for "bulk" operations */ + lu_byte tag[NM]; + } u; Value value[NM]; }; /* Computes the address of the tag for the abstract index 'k' */ -#define getArrTag(t,k) (&(t)->array[(k)/NM].tag[(k)%NM]) +#define getArrTag(t,k) (&(t)->array[(k)/NM].u.tag[(k)%NM]) /* Computes the address of the value for the abstract index 'k' */ #define getArrVal(t,k) (&(t)->array[(k)/NM].value[(k)%NM]) From ba710603811c68fe3a69b3bb98e9038d37489a79 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 15 Mar 2024 11:23:35 -0300 Subject: [PATCH 0893/1145] Removed "bulk operations" Negligible performance gains don't justify extra complexity. --- lgc.c | 36 +++++++----------------------------- ltable.c | 17 ++++------------- ltable.h | 18 +++--------------- 3 files changed, 14 insertions(+), 57 deletions(-) diff --git a/lgc.c b/lgc.c index f76e851eac..d1f5590e57 100644 --- a/lgc.c +++ b/lgc.c @@ -465,46 +465,24 @@ static void traverseweakvalue (global_State *g, Table *h) { } -#define BK2(x) cast(lua_Unsigned, ((x) << 8) | BIT_ISCOLLECTABLE) -/* -** Check whether some value in the cell starting at index 'i' -** is collectable -*/ -static int checkBulkCollectable (Table *h, unsigned i) { - const lua_Unsigned bitscoll = BK2(BK2(BK2(BK2(BK2(BK2(BK2(BK2(~0u)))))))); - int j; - i /= NM; - for (j = 0; j < BKSZ; j++) { - if (h->array[i].u.bulk[j] & bitscoll) - return 1; - } - return 0; -} - - /* -** Traverse the array part of a table. The traversal is made by cells, -** only traversing a cell if it has some collectable tag among its tags. +** Traverse the array part of a table. */ static int traversearray (global_State *g, Table *h) { unsigned asize = luaH_realasize(h); int marked = 0; /* true if some object is marked in this traversal */ unsigned i; - for (i = 0; i < asize; i += NM) { /* traverse array in cells */ - if (checkBulkCollectable(h, i)) { /* something to mark in this cell? */ - unsigned j; - for (j = 0; j < NM && i + j < asize; j++) { - GCObject *o = gcvalarr(h, i + j); - if (o != NULL && iswhite(o)) { - marked = 1; - reallymarkobject(g, o); - } - } + for (i = 0; i < asize; i++) { + GCObject *o = gcvalarr(h, i); + if (o != NULL && iswhite(o)) { + marked = 1; + reallymarkobject(g, o); } } return marked; } + /* ** Traverse an ephemeron table and link it to proper list. Returns true ** iff any object was marked during this traversal (which implies that diff --git a/ltable.c b/ltable.c index 6eb5f3e3e1..c5f487164c 100644 --- a/ltable.c +++ b/ltable.c @@ -665,7 +665,7 @@ static void reinsertOldSlice (lua_State *L, Table *t, unsigned oldasize, int tag = *getArrTag(t, i); if (!tagisempty(tag)) { /* a non-empty entry? */ TValue aux; - farr2val(t, i + 1, tag, &aux); + farr2val(t, i + 1, tag, &aux); /* copy entry into 'aux' */ luaH_setint(L, t, i + 1, &aux); /* re-insert it into the table */ } } @@ -673,21 +673,12 @@ static void reinsertOldSlice (lua_State *L, Table *t, unsigned oldasize, } -#define BK1(x) cast(lua_Unsigned, ((x) << 8) | LUA_VEMPTY) - /* -** Clear new slice of the array, in bulk. +** Clear new slice of the array. */ static void clearNewSlice (Table *t, unsigned oldasize, unsigned newasize) { - int i, j; - int firstcell = (oldasize + NM - 1) / NM; - int lastcell = cast_int((newasize + NM - 1) / NM) - 1; - for (i = firstcell; i <= lastcell; i++) { - /* empty tag repeated for all tags in a word */ - const lua_Unsigned empty = BK1(BK1(BK1(BK1(BK1(BK1(BK1(BK1(0)))))))); - for (j = 0; j < BKSZ; j++) - t->array[i].u.bulk[j] = empty; - } + for (; oldasize < newasize; oldasize++) + *getArrTag(t, oldasize) = LUA_VEMPTY; } diff --git a/ltable.h b/ltable.h index 8688264c7d..8b0340b56f 100644 --- a/ltable.h +++ b/ltable.h @@ -87,32 +87,20 @@ /* -** The array part of a table is represented by an array of *cells*. +** The array part of a table is represented by an array of cells. ** Each cell is composed of NM tags followed by NM values, so that ** no space is wasted in padding. */ #define NM cast_uint(sizeof(Value)) - -/* -** A few operations on arrays can be performed "in bulk", treating all -** tags of a cell as a simple (or a few) word(s). The next constant is -** the number of words to cover the tags of a cell. (In conventional -** architectures that will be 1 or 2.) -*/ -#define BKSZ cast_int((NM - 1) / sizeof(lua_Unsigned) + 1) - struct ArrayCell { - union { - lua_Unsigned bulk[BKSZ]; /* for "bulk" operations */ - lu_byte tag[NM]; - } u; + lu_byte tag[NM]; Value value[NM]; }; /* Computes the address of the tag for the abstract index 'k' */ -#define getArrTag(t,k) (&(t)->array[(k)/NM].u.tag[(k)%NM]) +#define getArrTag(t,k) (&(t)->array[(k)/NM].tag[(k)%NM]) /* Computes the address of the value for the abstract index 'k' */ #define getArrVal(t,k) (&(t)->array[(k)/NM].value[(k)%NM]) From ce6f5502c99ce9a367e25b678e375db6f8164d73 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 18 Mar 2024 15:56:32 -0300 Subject: [PATCH 0894/1145] 'luaH_get' functions return 'TValue' Instead of receiving a parameter telling them where to put the result of the query, these functions return the TValue directly. (That is, they return a structure.) --- lapi.c | 61 +++++++++++++++++++++++++++---------------------------- lcode.c | 5 ++--- ldump.c | 4 ++-- lobject.h | 19 +++++++++++++++++ ltable.c | 53 ++++++++++++++++------------------------------- ltable.h | 29 ++++++++++++-------------- lundump.c | 3 +-- lvm.c | 54 ++++++++++++++++++++++++------------------------ lvm.h | 18 +++++++++------- 9 files changed, 124 insertions(+), 122 deletions(-) diff --git a/lapi.c b/lapi.c index 7df637985e..a6ef56639e 100644 --- a/lapi.c +++ b/lapi.c @@ -666,47 +666,45 @@ LUA_API int lua_pushthread (lua_State *L) { static int auxgetstr (lua_State *L, const TValue *t, const char *k) { - int hres; + TValue aux; TString *str = luaS_new(L, k); - luaV_fastget(t, str, s2v(L->top.p), luaH_getstr, hres); - if (hres == HOK) { + luaV_fastget(t, str, s2v(L->top.p), luaH_getstr, aux); + if (!isemptyV(aux)) { api_incr_top(L); } else { setsvalue2s(L, L->top.p, str); api_incr_top(L); - luaV_finishget(L, t, s2v(L->top.p - 1), L->top.p - 1, hres); + luaV_finishget(L, t, s2v(L->top.p - 1), L->top.p - 1, aux); } lua_unlock(L); return ttype(s2v(L->top.p - 1)); } -static void getGlobalTable (lua_State *L, TValue *gt) { +static TValue getGlobalTable (lua_State *L) { Table *registry = hvalue(&G(L)->l_registry); - int hres = luaH_getint(registry, LUA_RIDX_GLOBALS, gt); - (void)hres; /* avoid warnings (not used) when checks are off */ - api_check(L, hres == HOK, "global table must exist"); + return luaH_getint(registry, LUA_RIDX_GLOBALS); } LUA_API int lua_getglobal (lua_State *L, const char *name) { TValue gt; lua_lock(L); - getGlobalTable(L, >); + gt = getGlobalTable(L); return auxgetstr(L, >, name); } LUA_API int lua_gettable (lua_State *L, int idx) { - int hres; + TValue aux; TValue *t; lua_lock(L); api_checkpop(L, 1); t = index2value(L, idx); - luaV_fastget(t, s2v(L->top.p - 1), s2v(L->top.p - 1), luaH_get, hres); - if (hres != HOK) - luaV_finishget(L, t, s2v(L->top.p - 1), L->top.p - 1, hres); + luaV_fastget(t, s2v(L->top.p - 1), s2v(L->top.p - 1), luaH_get, aux); + if (isemptyV(aux)) + luaV_finishget(L, t, s2v(L->top.p - 1), L->top.p - 1, aux); lua_unlock(L); return ttype(s2v(L->top.p - 1)); } @@ -720,14 +718,14 @@ LUA_API int lua_getfield (lua_State *L, int idx, const char *k) { LUA_API int lua_geti (lua_State *L, int idx, lua_Integer n) { TValue *t; - int hres; + TValue aux; lua_lock(L); t = index2value(L, idx); - luaV_fastgeti(t, n, s2v(L->top.p), hres); - if (hres != HOK) { + luaV_fastgeti(t, n, s2v(L->top.p), aux); + if (isemptyV(aux)) { TValue key; setivalue(&key, n); - luaV_finishget(L, t, &key, L->top.p, hres); + luaV_finishget(L, t, &key, L->top.p, aux); } api_incr_top(L); lua_unlock(L); @@ -735,12 +733,14 @@ LUA_API int lua_geti (lua_State *L, int idx, lua_Integer n) { } -l_sinline int finishrawget (lua_State *L, int hres) { - if (hres != HOK) /* avoid copying empty items to the stack */ +l_sinline int finishrawget (lua_State *L, TValue res) { + if (isemptyV(res)) /* avoid copying empty items to the stack */ setnilvalue(s2v(L->top.p)); + else + setobjV(L, s2v(L->top.p), res); api_incr_top(L); lua_unlock(L); - return ttype(s2v(L->top.p - 1)); + return ttypeV(res); } @@ -753,23 +753,23 @@ l_sinline Table *gettable (lua_State *L, int idx) { LUA_API int lua_rawget (lua_State *L, int idx) { Table *t; + TValue res; lua_lock(L); api_checkpop(L, 1); t = gettable(L, idx); - if (luaH_get(t, s2v(L->top.p - 1), s2v(L->top.p - 1)) != HOK) - setnilvalue(s2v(L->top.p - 1)); - lua_unlock(L); - return ttype(s2v(L->top.p - 1)); + res = luaH_get(t, s2v(L->top.p - 1)); + L->top.p--; /* pop key */ + return finishrawget(L, res); } LUA_API int lua_rawgeti (lua_State *L, int idx, lua_Integer n) { Table *t; - int hres; + TValue aux; lua_lock(L); t = gettable(L, idx); - luaH_fastgeti(t, n, s2v(L->top.p), hres); - return finishrawget(L, hres); + luaH_fastgeti(t, n, s2v(L->top.p), aux); + return finishrawget(L, aux); } @@ -779,7 +779,7 @@ LUA_API int lua_rawgetp (lua_State *L, int idx, const void *p) { lua_lock(L); t = gettable(L, idx); setpvalue(&k, cast_voidp(p)); - return finishrawget(L, luaH_get(t, &k, s2v(L->top.p))); + return finishrawget(L, luaH_get(t, &k)); } @@ -872,7 +872,7 @@ static void auxsetstr (lua_State *L, const TValue *t, const char *k) { LUA_API void lua_setglobal (lua_State *L, const char *name) { TValue gt; lua_lock(L); /* unlock done in 'auxsetstr' */ - getGlobalTable(L, >); + gt = getGlobalTable(L); auxsetstr(L, >, name); } @@ -1122,8 +1122,7 @@ LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data, LClosure *f = clLvalue(s2v(L->top.p - 1)); /* get new function */ if (f->nupvalues >= 1) { /* does it have an upvalue? */ /* get global table from registry */ - TValue gt; - getGlobalTable(L, >); + TValue gt = getGlobalTable(L); /* set global table as 1st upvalue of 'f' (may be LUA_ENV) */ setobj(L, f->upvals[0]->v.p, >); luaC_barrier(L, f->upvals[0], >); diff --git a/lcode.c b/lcode.c index 0d888822f0..18bf94135b 100644 --- a/lcode.c +++ b/lcode.c @@ -541,12 +541,11 @@ static void freeexps (FuncState *fs, expdesc *e1, expdesc *e2) { ** a function can make some indices wrong. */ static int addk (FuncState *fs, TValue *key, TValue *v) { - TValue val; lua_State *L = fs->ls->L; Proto *f = fs->f; - int aux = luaH_get(fs->ls->h, key, &val); /* query scanner table */ + TValue val = luaH_get(fs->ls->h, key); /* query scanner table */ int k, oldsize; - if (aux == HOK && ttisinteger(&val)) { /* is there an index there? */ + if (ttisintegerV(val)) { /* is there an index there? */ k = cast_int(ivalue(&val)); /* correct value? (warning: must distinguish floats from integers!) */ if (k < fs->nk && ttypetag(&f->k[k]) == ttypetag(v) && diff --git a/ldump.c b/ldump.c index 0d20fb0af4..34b63a8a09 100644 --- a/ldump.c +++ b/ldump.c @@ -132,8 +132,8 @@ static void dumpString (DumpState *D, TString *ts) { if (ts == NULL) dumpSize(D, 0); else { - TValue idx; - if (luaH_getstr(D->h, ts, &idx) == HOK) { /* string already saved? */ + TValue idx = luaH_getstr(D->h, ts); + if (!isemptyV(idx)) { /* string already saved? */ dumpSize(D, 1); /* reuse a saved string */ dumpSize(D, cast_sizet(ivalue(&idx))); /* index of saved string */ } diff --git a/lobject.h b/lobject.h index 81dfd4751c..69fa626087 100644 --- a/lobject.h +++ b/lobject.h @@ -75,6 +75,7 @@ typedef struct TValue { /* raw type tag of a TValue */ #define rawtt(o) ((o)->tt_) +#define rawttV(o) ((o).tt_) /* tag with no variants (bits 0-3) */ #define novariant(t) ((t) & 0x0F) @@ -82,14 +83,18 @@ typedef struct TValue { /* type tag of a TValue (bits 0-3 for tags + variant bits 4-5) */ #define withvariant(t) ((t) & 0x3F) #define ttypetag(o) withvariant(rawtt(o)) +#define ttypetagV(o) withvariant(rawttV(o)) /* type of a TValue */ #define ttype(o) (novariant(rawtt(o))) +#define ttypeV(o) (novariant(rawttV(o))) /* Macros to test type */ #define checktag(o,t) (rawtt(o) == (t)) +#define checktagV(o,t) (rawttV(o) == (t)) #define checktype(o,t) (ttype(o) == (t)) +#define checktypeV(o,t) (ttypeV(o) == (t)) /* Macros for internal tests */ @@ -112,6 +117,7 @@ typedef struct TValue { /* set a value's tag */ #define settt_(o,t) ((o)->tt_=(t)) +#define setttV_(o,t) ((o).tt_=(t)) /* main macro to copy values (from 'obj2' to 'obj1') */ @@ -120,6 +126,11 @@ typedef struct TValue { io1->value_ = io2->value_; settt_(io1, io2->tt_); \ checkliveness(L,io1); lua_assert(!isnonstrictnil(io1)); } +#define setobjV(L,obj1,obj2) \ + { TValue *io1=(obj1); const TValue io2=(obj2); \ + io1->value_ = io2.value_; settt_(io1, io2.tt_); \ + checkliveness(L,io1); lua_assert(!isnonstrictnil(io1)); } + /* ** Different types of assignments, according to source and destination. ** (They are mostly equal now, but may be different in the future.) @@ -188,9 +199,15 @@ typedef union { /* Value returned for a key not found in a table (absent key) */ #define LUA_VABSTKEY makevariant(LUA_TNIL, 2) +/* Special "value" to signal that a fast get is accessing a non-table */ +#define LUA_VNOTABLE makevariant(LUA_TNIL, 3) + +#define setnotableV(obj) setttV_(obj, LUA_VNOTABLE) + /* macro to test for (any kind of) nil */ #define ttisnil(v) checktype((v), LUA_TNIL) +#define ttisnilV(v) checktypeV((v), LUA_TNIL) #define tagisempty(tag) (novariant(tag) == LUA_TNIL) @@ -217,6 +234,7 @@ typedef union { ** be accepted as empty.) */ #define isempty(v) ttisnil(v) +#define isemptyV(v) checktypeV((v), LUA_TNIL) /* macro defining a value corresponding to an absent key */ @@ -328,6 +346,7 @@ typedef struct GCObject { #define ttisnumber(o) checktype((o), LUA_TNUMBER) #define ttisfloat(o) checktag((o), LUA_VNUMFLT) #define ttisinteger(o) checktag((o), LUA_VNUMINT) +#define ttisintegerV(o) checktagV((o), LUA_VNUMINT) #define nvalue(o) check_exp(ttisnumber(o), \ (ttisinteger(o) ? cast_num(ivalue(o)) : fltvalue(o))) diff --git a/ltable.c b/ltable.c index c5f487164c..f675f39bbe 100644 --- a/ltable.c +++ b/ltable.c @@ -904,28 +904,14 @@ static int hashkeyisempty (Table *t, lua_Integer key) { } -static int finishnodeget (const TValue *val, TValue *res) { - if (!ttisnil(val)) { - setobj(((lua_State*)NULL), res, val); - return HOK; /* success */ - } - else - return HNOTFOUND; /* could not get value */ -} - - -int luaH_getint (Table *t, lua_Integer key, TValue *res) { +TValue luaH_getint (Table *t, lua_Integer key) { if (keyinarray(t, key)) { - int tag = *getArrTag(t, key - 1); - if (!tagisempty(tag)) { - farr2val(t, key, tag, res); - return HOK; /* success */ - } - else - return ~cast_int(key); /* empty slot in the array part */ + TValue res; + arr2objV(t, key, res); + return res; } - else - return finishnodeget(getintfromhash(t, key), res); + else + return *getintfromhash(t, key); } @@ -948,8 +934,8 @@ const TValue *luaH_Hgetshortstr (Table *t, TString *key) { } -int luaH_getshortstr (Table *t, TString *key, TValue *res) { - return finishnodeget(luaH_Hgetshortstr(t, key), res); +TValue luaH_getshortstr (Table *t, TString *key) { + return *luaH_Hgetshortstr(t, key); } @@ -964,8 +950,8 @@ static const TValue *Hgetstr (Table *t, TString *key) { } -int luaH_getstr (Table *t, TString *key, TValue *res) { - return finishnodeget(Hgetstr(t, key), res); +TValue luaH_getstr (Table *t, TString *key) { + return *Hgetstr(t, key); } @@ -981,34 +967,31 @@ TString *luaH_getstrkey (Table *t, TString *key) { /* ** main search function */ -int luaH_get (Table *t, const TValue *key, TValue *res) { - const TValue *slot; +TValue luaH_get (Table *t, const TValue *key) { switch (ttypetag(key)) { case LUA_VSHRSTR: - slot = luaH_Hgetshortstr(t, tsvalue(key)); + return *luaH_Hgetshortstr(t, tsvalue(key)); break; case LUA_VNUMINT: - return luaH_getint(t, ivalue(key), res); + return luaH_getint(t, ivalue(key)); case LUA_VNIL: - slot = &absentkey; + return absentkey; break; case LUA_VNUMFLT: { lua_Integer k; if (luaV_flttointeger(fltvalue(key), &k, F2Ieq)) /* integral index? */ - return luaH_getint(t, k, res); /* use specialized version */ + return luaH_getint(t, k); /* use specialized version */ /* else... */ } /* FALLTHROUGH */ default: - slot = getgeneric(t, key, 0); - break; + return *getgeneric(t, key, 0); } - return finishnodeget(slot, res); } static int finishnodeset (Table *t, const TValue *slot, TValue *val) { if (!ttisnil(slot)) { - setobj(((lua_State*)NULL), cast(TValue*, slot), val); + setobj(cast(lua_State*, NULL), cast(TValue*, slot), val); return HOK; /* success */ } else if (isabstkey(slot)) @@ -1022,7 +1005,7 @@ static int rawfinishnodeset (const TValue *slot, TValue *val) { if (isabstkey(slot)) return 0; /* no slot with that key */ else { - setobj(((lua_State*)NULL), cast(TValue*, slot), val); + setobj(cast(lua_State*, NULL), cast(TValue*, slot), val); return 1; /* success */ } } diff --git a/ltable.h b/ltable.h index 8b0340b56f..10dae5c719 100644 --- a/ltable.h +++ b/ltable.h @@ -46,13 +46,11 @@ -#define luaH_fastgeti(t,k,res,hres) \ +#define luaH_fastgeti(t,k,res,aux) \ { Table *h = t; lua_Unsigned u = l_castS2U(k); \ - if ((u - 1u < h->alimit)) { \ - int tag = *getArrTag(h,(u)-1u); \ - if (tagisempty(tag)) hres = HNOTFOUND; \ - else { farr2val(h, u, tag, res); hres = HOK; }} \ - else { hres = luaH_getint(h, u, res); }} + if ((u - 1u < h->alimit)) arr2objV(h,u,aux); \ + else aux = luaH_getint(h, u); \ + if (!isemptyV(aux)) setobjV(cast(lua_State*, NULL), res, aux); } #define luaH_fastseti(t,k,val,hres) \ @@ -64,15 +62,13 @@ else { hres = luaH_psetint(h, u, val); }} -/* results from get/pset */ +/* results from pset */ #define HOK 0 #define HNOTFOUND 1 #define HNOTATABLE 2 #define HFIRSTNODE 3 /* -** 'luaH_get*' operations set 'res' and return HOK, unless the value is -** absent. In that case, they set nothing and return HNOTFOUND. ** The 'luaH_pset*' (pre-set) operations set the given value and return ** HOK, unless the original value is absent. In that case, if the key ** is really absent, they return HNOTFOUND. Otherwise, if there is a @@ -109,8 +105,10 @@ struct ArrayCell { /* ** Move TValues to/from arrays, using Lua indices */ -#define arr2obj(h,k,val) \ - ((val)->tt_ = *getArrTag(h,(k)-1u), (val)->value_ = *getArrVal(h,(k)-1u)) +#define arr2objV(h,k,val) \ + ((val).tt_ = *getArrTag(h,(k)-1u), (val).value_ = *getArrVal(h,(k)-1u)) + +#define arr2obj(h,k,val) arr2objV(h,k,*(val)) #define obj2arr(h,k,val) \ (*getArrTag(h,(k)-1u) = (val)->tt_, *getArrVal(h,(k)-1u) = (val)->value_) @@ -128,12 +126,11 @@ struct ArrayCell { (*tag = (val)->tt_, *getArrVal(h,(k)-1u) = (val)->value_) -LUAI_FUNC int luaH_get (Table *t, const TValue *key, TValue *res); -LUAI_FUNC int luaH_getshortstr (Table *t, TString *key, TValue *res); -LUAI_FUNC int luaH_getstr (Table *t, TString *key, TValue *res); -LUAI_FUNC int luaH_getint (Table *t, lua_Integer key, TValue *res); +LUAI_FUNC TValue luaH_get (Table *t, const TValue *key); +LUAI_FUNC TValue luaH_getshortstr (Table *t, TString *key); +LUAI_FUNC TValue luaH_getstr (Table *t, TString *key); +LUAI_FUNC TValue luaH_getint (Table *t, lua_Integer key); -/* Special get for metamethods */ LUAI_FUNC const TValue *luaH_Hgetshortstr (Table *t, TString *key); LUAI_FUNC TString *luaH_getstrkey (Table *t, TString *key); diff --git a/lundump.c b/lundump.c index 51d5dc6645..593a4951ff 100644 --- a/lundump.c +++ b/lundump.c @@ -149,8 +149,7 @@ static void loadString (LoadState *S, Proto *p, TString **sl) { } else if (size == 1) { /* previously saved string? */ lua_Integer idx = cast(lua_Integer, loadSize(S)); /* get its index */ - TValue stv; - luaH_getint(S->h, idx, &stv); /* get its value */ + TValue stv = luaH_getint(S->h, idx); /* get its value */ *sl = ts = tsvalue(&stv); luaC_objbarrier(L, p, ts); return; /* do not save it again */ diff --git a/lvm.c b/lvm.c index 78e39b71b2..a251f423de 100644 --- a/lvm.c +++ b/lvm.c @@ -287,12 +287,13 @@ static int floatforloop (StkId ra) { /* ** Finish the table access 'val = t[key]'. */ -void luaV_finishget (lua_State *L, const TValue *t, TValue *key, StkId val, - int hres) { +void luaV_finishget_ (lua_State *L, const TValue *t, TValue *key, StkId val, + int tag) { int loop; /* counter to avoid infinite loops */ const TValue *tm; /* metamethod */ + TValue aux; for (loop = 0; loop < MAXTAGLOOP; loop++) { - if (hres == HNOTATABLE) { /* 't' is not a table? */ + if (tag == LUA_VNOTABLE) { /* 't' is not a table? */ lua_assert(!ttistable(t)); tm = luaT_gettmbyobj(L, t, TM_INDEX); if (l_unlikely(notm(tm))) @@ -312,10 +313,11 @@ void luaV_finishget (lua_State *L, const TValue *t, TValue *key, StkId val, return; } t = tm; /* else try to access 'tm[key]' */ - luaV_fastget(t, key, s2v(val), luaH_get, hres); - if (hres == HOK) + luaV_fastget(t, key, s2v(val), luaH_get, aux); + if (!isemptyV(aux)) return; /* done */ /* else repeat (tail call 'luaV_finishget') */ + tag = ttypetagV(aux); } luaG_runerror(L, "'__index' chain too long; possible loop"); } @@ -1245,36 +1247,36 @@ void luaV_execute (lua_State *L, CallInfo *ci) { TValue *upval = cl->upvals[GETARG_B(i)]->v.p; TValue *rc = KC(i); TString *key = tsvalue(rc); /* key must be a short string */ - int hres; - luaV_fastget(upval, key, s2v(ra), luaH_getshortstr, hres); - if (hres != HOK) - Protect(luaV_finishget(L, upval, rc, ra, hres)); + TValue aux; + luaV_fastget(upval, key, s2v(ra), luaH_getshortstr, aux); + if (isemptyV(aux)) + Protect(luaV_finishget(L, upval, rc, ra, aux)); vmbreak; } vmcase(OP_GETTABLE) { StkId ra = RA(i); TValue *rb = vRB(i); TValue *rc = vRC(i); - int hres; + TValue aux; if (ttisinteger(rc)) { /* fast track for integers? */ - luaV_fastgeti(rb, ivalue(rc), s2v(ra), hres); + luaV_fastgeti(rb, ivalue(rc), s2v(ra), aux); } else - luaV_fastget(rb, rc, s2v(ra), luaH_get, hres); - if (hres != HOK) /* fast track for integers? */ - Protect(luaV_finishget(L, rb, rc, ra, hres)); + luaV_fastget(rb, rc, s2v(ra), luaH_get, aux); + if (isemptyV(aux)) /* fast track for integers? */ + Protect(luaV_finishget(L, rb, rc, ra, aux)); vmbreak; } vmcase(OP_GETI) { StkId ra = RA(i); TValue *rb = vRB(i); int c = GETARG_C(i); - int hres; - luaV_fastgeti(rb, c, s2v(ra), hres); - if (hres != HOK) { + TValue aux; + luaV_fastgeti(rb, c, s2v(ra), aux); + if (isemptyV(aux)) { TValue key; setivalue(&key, c); - Protect(luaV_finishget(L, rb, &key, ra, hres)); + Protect(luaV_finishget(L, rb, &key, ra, aux)); } vmbreak; } @@ -1283,10 +1285,10 @@ void luaV_execute (lua_State *L, CallInfo *ci) { TValue *rb = vRB(i); TValue *rc = KC(i); TString *key = tsvalue(rc); /* key must be a short string */ - int hres; - luaV_fastget(rb, key, s2v(ra), luaH_getshortstr, hres); - if (hres != HOK) - Protect(luaV_finishget(L, rb, rc, ra, hres)); + TValue aux; + luaV_fastget(rb, key, s2v(ra), luaH_getshortstr, aux); + if (isemptyV(aux)) + Protect(luaV_finishget(L, rb, rc, ra, aux)); vmbreak; } vmcase(OP_SETTABUP) { @@ -1368,14 +1370,14 @@ void luaV_execute (lua_State *L, CallInfo *ci) { } vmcase(OP_SELF) { StkId ra = RA(i); - int hres; + TValue aux; TValue *rb = vRB(i); TValue *rc = RKC(i); TString *key = tsvalue(rc); /* key must be a string */ setobj2s(L, ra + 1, rb); - luaV_fastget(rb, key, s2v(ra), luaH_getstr, hres); - if (hres != HOK) - Protect(luaV_finishget(L, rb, rc, ra, hres)); + luaV_fastget(rb, key, s2v(ra), luaH_getstr, aux); + if (isemptyV(aux)) + Protect(luaV_finishget(L, rb, rc, ra, aux)); vmbreak; } vmcase(OP_ADDI) { diff --git a/lvm.h b/lvm.h index 54ee5dd71e..3b11e789e6 100644 --- a/lvm.h +++ b/lvm.h @@ -78,17 +78,19 @@ typedef enum { /* ** fast track for 'gettable' */ -#define luaV_fastget(t,k,res,f, hres) \ - (hres = (!ttistable(t) ? HNOTATABLE : f(hvalue(t), k, res))) +#define luaV_fastget(t,k,res,f, aux) \ + {if (!ttistable(t)) setnotableV(aux); \ + else { aux = f(hvalue(t), k); \ + if (!isemptyV(aux)) { setobjV(cast(lua_State*, NULL), res, aux); } } } /* ** Special case of 'luaV_fastget' for integers, inlining the fast case ** of 'luaH_getint'. */ -#define luaV_fastgeti(t,k,res,hres) \ - if (!ttistable(t)) hres = HNOTATABLE; \ - else { luaH_fastgeti(hvalue(t), k, res, hres); } +#define luaV_fastgeti(t,k,res,aux) \ + { if (!ttistable(t)) setnotableV(aux); \ + else { luaH_fastgeti(hvalue(t), k, res, aux); } } #define luaV_fastset(t,k,val,hres,f) \ @@ -120,8 +122,10 @@ LUAI_FUNC int luaV_tointeger (const TValue *obj, lua_Integer *p, F2Imod mode); LUAI_FUNC int luaV_tointegerns (const TValue *obj, lua_Integer *p, F2Imod mode); LUAI_FUNC int luaV_flttointeger (lua_Number n, lua_Integer *p, F2Imod mode); -LUAI_FUNC void luaV_finishget (lua_State *L, const TValue *t, TValue *key, - StkId val, int aux); +#define luaV_finishget(L,t,key,val,aux) \ + luaV_finishget_(L,t,key,val,ttypetagV(aux)) +LUAI_FUNC void luaV_finishget_ (lua_State *L, const TValue *t, TValue *key, + StkId val, int tag); LUAI_FUNC void luaV_finishset (lua_State *L, const TValue *t, TValue *key, TValue *val, int aux); LUAI_FUNC void luaV_finishOp (lua_State *L); From 0593256707ceddb1bc9cd4b25b822a7fbcfedd66 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 21 Mar 2024 11:23:21 -0300 Subject: [PATCH 0895/1145] 'luaH_get' functions return tag of the result Undoing previous commit. Returning TValue increases code size without any visible gains. Returning the tag is a little simpler than returning a special code (HOK/HNOTFOUND) and the tag is useful by itself in some cases. --- lapi.c | 69 ++++++++++++++++++++++++++++--------------------------- lcode.c | 5 ++-- ldump.c | 5 ++-- lobject.h | 26 +++++++-------------- ltable.c | 48 +++++++++++++++++++++++--------------- ltable.h | 29 ++++++++++++----------- ltests.c | 6 +++-- lundump.c | 3 ++- lvm.c | 61 ++++++++++++++++++++++++------------------------ lvm.h | 18 ++++++--------- 10 files changed, 138 insertions(+), 132 deletions(-) diff --git a/lapi.c b/lapi.c index a6ef56639e..2b14c15ee0 100644 --- a/lapi.c +++ b/lapi.c @@ -353,7 +353,7 @@ LUA_API void lua_arith (lua_State *L, int op) { } /* first operand at top - 2, second at top - 1; result go to top - 2 */ luaO_arith(L, op, s2v(L->top.p - 2), s2v(L->top.p - 1), L->top.p - 2); - L->top.p--; /* remove second operand */ + L->top.p--; /* pop second operand */ lua_unlock(L); } @@ -666,47 +666,49 @@ LUA_API int lua_pushthread (lua_State *L) { static int auxgetstr (lua_State *L, const TValue *t, const char *k) { - TValue aux; + int tag; TString *str = luaS_new(L, k); - luaV_fastget(t, str, s2v(L->top.p), luaH_getstr, aux); - if (!isemptyV(aux)) { + luaV_fastget(t, str, s2v(L->top.p), luaH_getstr, tag); + if (!tagisempty(tag)) { api_incr_top(L); } else { setsvalue2s(L, L->top.p, str); api_incr_top(L); - luaV_finishget(L, t, s2v(L->top.p - 1), L->top.p - 1, aux); + tag = luaV_finishget(L, t, s2v(L->top.p - 1), L->top.p - 1, tag); } lua_unlock(L); - return ttype(s2v(L->top.p - 1)); + return novariant(tag); } -static TValue getGlobalTable (lua_State *L) { +static void getGlobalTable (lua_State *L, TValue *gt) { Table *registry = hvalue(&G(L)->l_registry); - return luaH_getint(registry, LUA_RIDX_GLOBALS); + int tag = luaH_getint(registry, LUA_RIDX_GLOBALS, gt); + (void)tag; /* avoid not-used warnings when checks are off */ + api_check(L, novariant(tag) == LUA_TTABLE, "global table must exist"); } LUA_API int lua_getglobal (lua_State *L, const char *name) { TValue gt; lua_lock(L); - gt = getGlobalTable(L); + getGlobalTable(L, >); return auxgetstr(L, >, name); } LUA_API int lua_gettable (lua_State *L, int idx) { - TValue aux; + int tag; TValue *t; lua_lock(L); api_checkpop(L, 1); t = index2value(L, idx); - luaV_fastget(t, s2v(L->top.p - 1), s2v(L->top.p - 1), luaH_get, aux); - if (isemptyV(aux)) - luaV_finishget(L, t, s2v(L->top.p - 1), L->top.p - 1, aux); + luaV_fastget(t, s2v(L->top.p - 1), s2v(L->top.p - 1), luaH_get, tag); + if (tagisempty(tag)) + tag = luaV_finishget(L, t, s2v(L->top.p - 1), L->top.p - 1, tag); lua_unlock(L); - return ttype(s2v(L->top.p - 1)); + return novariant(tag); } @@ -718,29 +720,27 @@ LUA_API int lua_getfield (lua_State *L, int idx, const char *k) { LUA_API int lua_geti (lua_State *L, int idx, lua_Integer n) { TValue *t; - TValue aux; + int tag; lua_lock(L); t = index2value(L, idx); - luaV_fastgeti(t, n, s2v(L->top.p), aux); - if (isemptyV(aux)) { + luaV_fastgeti(t, n, s2v(L->top.p), tag); + if (tagisempty(tag)) { TValue key; setivalue(&key, n); - luaV_finishget(L, t, &key, L->top.p, aux); + tag = luaV_finishget(L, t, &key, L->top.p, tag); } api_incr_top(L); lua_unlock(L); - return ttype(s2v(L->top.p - 1)); + return novariant(tag); } -l_sinline int finishrawget (lua_State *L, TValue res) { - if (isemptyV(res)) /* avoid copying empty items to the stack */ +static int finishrawget (lua_State *L, int tag) { + if (tagisempty(tag)) /* avoid copying empty items to the stack */ setnilvalue(s2v(L->top.p)); - else - setobjV(L, s2v(L->top.p), res); api_incr_top(L); lua_unlock(L); - return ttypeV(res); + return novariant(tag); } @@ -753,23 +753,23 @@ l_sinline Table *gettable (lua_State *L, int idx) { LUA_API int lua_rawget (lua_State *L, int idx) { Table *t; - TValue res; + int tag; lua_lock(L); api_checkpop(L, 1); t = gettable(L, idx); - res = luaH_get(t, s2v(L->top.p - 1)); + tag = luaH_get(t, s2v(L->top.p - 1), s2v(L->top.p - 1)); L->top.p--; /* pop key */ - return finishrawget(L, res); + return finishrawget(L, tag); } LUA_API int lua_rawgeti (lua_State *L, int idx, lua_Integer n) { Table *t; - TValue aux; + int tag; lua_lock(L); t = gettable(L, idx); - luaH_fastgeti(t, n, s2v(L->top.p), aux); - return finishrawget(L, aux); + luaH_fastgeti(t, n, s2v(L->top.p), tag); + return finishrawget(L, tag); } @@ -779,7 +779,7 @@ LUA_API int lua_rawgetp (lua_State *L, int idx, const void *p) { lua_lock(L); t = gettable(L, idx); setpvalue(&k, cast_voidp(p)); - return finishrawget(L, luaH_get(t, &k)); + return finishrawget(L, luaH_get(t, &k, s2v(L->top.p))); } @@ -872,7 +872,7 @@ static void auxsetstr (lua_State *L, const TValue *t, const char *k) { LUA_API void lua_setglobal (lua_State *L, const char *name) { TValue gt; lua_lock(L); /* unlock done in 'auxsetstr' */ - gt = getGlobalTable(L); + getGlobalTable(L, >); auxsetstr(L, >, name); } @@ -1122,7 +1122,8 @@ LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data, LClosure *f = clLvalue(s2v(L->top.p - 1)); /* get new function */ if (f->nupvalues >= 1) { /* does it have an upvalue? */ /* get global table from registry */ - TValue gt = getGlobalTable(L); + TValue gt; + getGlobalTable(L, >); /* set global table as 1st upvalue of 'f' (may be LUA_ENV) */ setobj(L, f->upvals[0]->v.p, >); luaC_barrier(L, f->upvals[0], >); @@ -1266,7 +1267,7 @@ LUA_API int lua_next (lua_State *L, int idx) { if (more) api_incr_top(L); else /* no more elements */ - L->top.p -= 1; /* remove key */ + L->top.p--; /* pop key */ lua_unlock(L); return more; } diff --git a/lcode.c b/lcode.c index 18bf94135b..2c57fdafbc 100644 --- a/lcode.c +++ b/lcode.c @@ -541,11 +541,12 @@ static void freeexps (FuncState *fs, expdesc *e1, expdesc *e2) { ** a function can make some indices wrong. */ static int addk (FuncState *fs, TValue *key, TValue *v) { + TValue val; lua_State *L = fs->ls->L; Proto *f = fs->f; - TValue val = luaH_get(fs->ls->h, key); /* query scanner table */ + int tag = luaH_get(fs->ls->h, key, &val); /* query scanner table */ int k, oldsize; - if (ttisintegerV(val)) { /* is there an index there? */ + if (tag == LUA_VNUMINT) { /* is there an index there? */ k = cast_int(ivalue(&val)); /* correct value? (warning: must distinguish floats from integers!) */ if (k < fs->nk && ttypetag(&f->k[k]) == ttypetag(v) && diff --git a/ldump.c b/ldump.c index 34b63a8a09..ca708a412a 100644 --- a/ldump.c +++ b/ldump.c @@ -132,8 +132,9 @@ static void dumpString (DumpState *D, TString *ts) { if (ts == NULL) dumpSize(D, 0); else { - TValue idx = luaH_getstr(D->h, ts); - if (!isemptyV(idx)) { /* string already saved? */ + TValue idx; + int tag = luaH_getstr(D->h, ts, &idx); + if (!tagisempty(tag)) { /* string already saved? */ dumpSize(D, 1); /* reuse a saved string */ dumpSize(D, cast_sizet(ivalue(&idx))); /* index of saved string */ } diff --git a/lobject.h b/lobject.h index 69fa626087..b42539cf28 100644 --- a/lobject.h +++ b/lobject.h @@ -75,7 +75,6 @@ typedef struct TValue { /* raw type tag of a TValue */ #define rawtt(o) ((o)->tt_) -#define rawttV(o) ((o).tt_) /* tag with no variants (bits 0-3) */ #define novariant(t) ((t) & 0x0F) @@ -83,18 +82,14 @@ typedef struct TValue { /* type tag of a TValue (bits 0-3 for tags + variant bits 4-5) */ #define withvariant(t) ((t) & 0x3F) #define ttypetag(o) withvariant(rawtt(o)) -#define ttypetagV(o) withvariant(rawttV(o)) /* type of a TValue */ #define ttype(o) (novariant(rawtt(o))) -#define ttypeV(o) (novariant(rawttV(o))) /* Macros to test type */ #define checktag(o,t) (rawtt(o) == (t)) -#define checktagV(o,t) (rawttV(o) == (t)) #define checktype(o,t) (ttype(o) == (t)) -#define checktypeV(o,t) (ttypeV(o) == (t)) /* Macros for internal tests */ @@ -117,7 +112,6 @@ typedef struct TValue { /* set a value's tag */ #define settt_(o,t) ((o)->tt_=(t)) -#define setttV_(o,t) ((o).tt_=(t)) /* main macro to copy values (from 'obj2' to 'obj1') */ @@ -126,11 +120,6 @@ typedef struct TValue { io1->value_ = io2->value_; settt_(io1, io2->tt_); \ checkliveness(L,io1); lua_assert(!isnonstrictnil(io1)); } -#define setobjV(L,obj1,obj2) \ - { TValue *io1=(obj1); const TValue io2=(obj2); \ - io1->value_ = io2.value_; settt_(io1, io2.tt_); \ - checkliveness(L,io1); lua_assert(!isnonstrictnil(io1)); } - /* ** Different types of assignments, according to source and destination. ** (They are mostly equal now, but may be different in the future.) @@ -199,16 +188,19 @@ typedef union { /* Value returned for a key not found in a table (absent key) */ #define LUA_VABSTKEY makevariant(LUA_TNIL, 2) -/* Special "value" to signal that a fast get is accessing a non-table */ -#define LUA_VNOTABLE makevariant(LUA_TNIL, 3) - -#define setnotableV(obj) setttV_(obj, LUA_VNOTABLE) +/* Special variant to signal that a fast get is accessing a non-table */ +#define LUA_VNOTABLE makevariant(LUA_TNIL, 3) /* macro to test for (any kind of) nil */ #define ttisnil(v) checktype((v), LUA_TNIL) -#define ttisnilV(v) checktypeV((v), LUA_TNIL) +/* +** Macro to test the result of a table access. Formally, it should +** distinguish between LUA_VEMPTY/LUA_VABSTKEY/LUA_VNOTABLE and +** other tags. As currently nil is equivalent to LUA_VEMPTY, it is +** simpler to just test whether the value is nil. +*/ #define tagisempty(tag) (novariant(tag) == LUA_TNIL) @@ -234,7 +226,6 @@ typedef union { ** be accepted as empty.) */ #define isempty(v) ttisnil(v) -#define isemptyV(v) checktypeV((v), LUA_TNIL) /* macro defining a value corresponding to an absent key */ @@ -346,7 +337,6 @@ typedef struct GCObject { #define ttisnumber(o) checktype((o), LUA_TNUMBER) #define ttisfloat(o) checktag((o), LUA_VNUMFLT) #define ttisinteger(o) checktag((o), LUA_VNUMINT) -#define ttisintegerV(o) checktagV((o), LUA_VNUMINT) #define nvalue(o) check_exp(ttisnumber(o), \ (ttisinteger(o) ? cast_num(ivalue(o)) : fltvalue(o))) diff --git a/ltable.c b/ltable.c index f675f39bbe..f62f36bc26 100644 --- a/ltable.c +++ b/ltable.c @@ -904,14 +904,23 @@ static int hashkeyisempty (Table *t, lua_Integer key) { } -TValue luaH_getint (Table *t, lua_Integer key) { +static int finishnodeget (const TValue *val, TValue *res) { + if (!ttisnil(val)) { + setobj(((lua_State*)NULL), res, val); + } + return ttypetag(val); +} + + +int luaH_getint (Table *t, lua_Integer key, TValue *res) { if (keyinarray(t, key)) { - TValue res; - arr2objV(t, key, res); - return res; + int tag = *getArrTag(t, key - 1); + if (!tagisempty(tag)) + farr2val(t, key, tag, res); + return tag; } - else - return *getintfromhash(t, key); + else + return finishnodeget(getintfromhash(t, key), res); } @@ -934,8 +943,8 @@ const TValue *luaH_Hgetshortstr (Table *t, TString *key) { } -TValue luaH_getshortstr (Table *t, TString *key) { - return *luaH_Hgetshortstr(t, key); +int luaH_getshortstr (Table *t, TString *key, TValue *res) { + return finishnodeget(luaH_Hgetshortstr(t, key), res); } @@ -950,8 +959,8 @@ static const TValue *Hgetstr (Table *t, TString *key) { } -TValue luaH_getstr (Table *t, TString *key) { - return *Hgetstr(t, key); +int luaH_getstr (Table *t, TString *key, TValue *res) { + return finishnodeget(Hgetstr(t, key), res); } @@ -967,31 +976,34 @@ TString *luaH_getstrkey (Table *t, TString *key) { /* ** main search function */ -TValue luaH_get (Table *t, const TValue *key) { +int luaH_get (Table *t, const TValue *key, TValue *res) { + const TValue *slot; switch (ttypetag(key)) { case LUA_VSHRSTR: - return *luaH_Hgetshortstr(t, tsvalue(key)); + slot = luaH_Hgetshortstr(t, tsvalue(key)); break; case LUA_VNUMINT: - return luaH_getint(t, ivalue(key)); + return luaH_getint(t, ivalue(key), res); case LUA_VNIL: - return absentkey; + slot = &absentkey; break; case LUA_VNUMFLT: { lua_Integer k; if (luaV_flttointeger(fltvalue(key), &k, F2Ieq)) /* integral index? */ - return luaH_getint(t, k); /* use specialized version */ + return luaH_getint(t, k, res); /* use specialized version */ /* else... */ } /* FALLTHROUGH */ default: - return *getgeneric(t, key, 0); + slot = getgeneric(t, key, 0); + break; } + return finishnodeget(slot, res); } static int finishnodeset (Table *t, const TValue *slot, TValue *val) { if (!ttisnil(slot)) { - setobj(cast(lua_State*, NULL), cast(TValue*, slot), val); + setobj(((lua_State*)NULL), cast(TValue*, slot), val); return HOK; /* success */ } else if (isabstkey(slot)) @@ -1005,7 +1017,7 @@ static int rawfinishnodeset (const TValue *slot, TValue *val) { if (isabstkey(slot)) return 0; /* no slot with that key */ else { - setobj(cast(lua_State*, NULL), cast(TValue*, slot), val); + setobj(((lua_State*)NULL), cast(TValue*, slot), val); return 1; /* success */ } } diff --git a/ltable.h b/ltable.h index 10dae5c719..1f2ea3ee5c 100644 --- a/ltable.h +++ b/ltable.h @@ -46,11 +46,12 @@ -#define luaH_fastgeti(t,k,res,aux) \ +#define luaH_fastgeti(t,k,res,tag) \ { Table *h = t; lua_Unsigned u = l_castS2U(k); \ - if ((u - 1u < h->alimit)) arr2objV(h,u,aux); \ - else aux = luaH_getint(h, u); \ - if (!isemptyV(aux)) setobjV(cast(lua_State*, NULL), res, aux); } + if ((u - 1u < h->alimit)) { \ + tag = *getArrTag(h,(u)-1u); \ + if (!tagisempty(tag)) { farr2val(h, u, tag, res); }} \ + else { tag = luaH_getint(h, u, res); }} #define luaH_fastseti(t,k,val,hres) \ @@ -69,6 +70,8 @@ #define HFIRSTNODE 3 /* +** 'luaH_get*' operations set 'res', unless the value is absent, and +** return the tag of the result, ** The 'luaH_pset*' (pre-set) operations set the given value and return ** HOK, unless the original value is absent. In that case, if the key ** is really absent, they return HNOTFOUND. Otherwise, if there is a @@ -85,7 +88,8 @@ /* ** The array part of a table is represented by an array of cells. ** Each cell is composed of NM tags followed by NM values, so that -** no space is wasted in padding. +** no space is wasted in padding. The last cell may be incomplete, +** that is, it may have fewer than NM values. */ #define NM cast_uint(sizeof(Value)) @@ -105,10 +109,8 @@ struct ArrayCell { /* ** Move TValues to/from arrays, using Lua indices */ -#define arr2objV(h,k,val) \ - ((val).tt_ = *getArrTag(h,(k)-1u), (val).value_ = *getArrVal(h,(k)-1u)) - -#define arr2obj(h,k,val) arr2objV(h,k,*(val)) +#define arr2obj(h,k,val) \ + ((val)->tt_ = *getArrTag(h,(k)-1u), (val)->value_ = *getArrVal(h,(k)-1u)) #define obj2arr(h,k,val) \ (*getArrTag(h,(k)-1u) = (val)->tt_, *getArrVal(h,(k)-1u) = (val)->value_) @@ -126,11 +128,12 @@ struct ArrayCell { (*tag = (val)->tt_, *getArrVal(h,(k)-1u) = (val)->value_) -LUAI_FUNC TValue luaH_get (Table *t, const TValue *key); -LUAI_FUNC TValue luaH_getshortstr (Table *t, TString *key); -LUAI_FUNC TValue luaH_getstr (Table *t, TString *key); -LUAI_FUNC TValue luaH_getint (Table *t, lua_Integer key); +LUAI_FUNC int luaH_get (Table *t, const TValue *key, TValue *res); +LUAI_FUNC int luaH_getshortstr (Table *t, TString *key, TValue *res); +LUAI_FUNC int luaH_getstr (Table *t, TString *key, TValue *res); +LUAI_FUNC int luaH_getint (Table *t, lua_Integer key, TValue *res); +/* Special get for metamethods */ LUAI_FUNC const TValue *luaH_Hgetshortstr (Table *t, TString *key); LUAI_FUNC TString *luaH_getstrkey (Table *t, TString *key); diff --git a/ltests.c b/ltests.c index 59df7cadc8..1a34870eda 100644 --- a/ltests.c +++ b/ltests.c @@ -1538,7 +1538,8 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) { } else if EQ("getfield") { int t = getindex; - lua_getfield(L1, t, getstring); + int tp = lua_getfield(L1, t, getstring); + lua_assert(tp == lua_type(L1, -1)); } else if EQ("getglobal") { lua_getglobal(L1, getstring); @@ -1548,7 +1549,8 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) { lua_pushnil(L1); } else if EQ("gettable") { - lua_gettable(L1, getindex); + int tp = lua_gettable(L1, getindex); + lua_assert(tp == lua_type(L1, -1)); } else if EQ("gettop") { lua_pushinteger(L1, lua_gettop(L1)); diff --git a/lundump.c b/lundump.c index 593a4951ff..51d5dc6645 100644 --- a/lundump.c +++ b/lundump.c @@ -149,7 +149,8 @@ static void loadString (LoadState *S, Proto *p, TString **sl) { } else if (size == 1) { /* previously saved string? */ lua_Integer idx = cast(lua_Integer, loadSize(S)); /* get its index */ - TValue stv = luaH_getint(S->h, idx); /* get its value */ + TValue stv; + luaH_getint(S->h, idx, &stv); /* get its value */ *sl = ts = tsvalue(&stv); luaC_objbarrier(L, p, ts); return; /* do not save it again */ diff --git a/lvm.c b/lvm.c index a251f423de..cfa9961bff 100644 --- a/lvm.c +++ b/lvm.c @@ -285,13 +285,12 @@ static int floatforloop (StkId ra) { /* -** Finish the table access 'val = t[key]'. +** Finish the table access 'val = t[key]' and return the tag of the result. */ -void luaV_finishget_ (lua_State *L, const TValue *t, TValue *key, StkId val, - int tag) { +int luaV_finishget (lua_State *L, const TValue *t, TValue *key, StkId val, + int tag) { int loop; /* counter to avoid infinite loops */ const TValue *tm; /* metamethod */ - TValue aux; for (loop = 0; loop < MAXTAGLOOP; loop++) { if (tag == LUA_VNOTABLE) { /* 't' is not a table? */ lua_assert(!ttistable(t)); @@ -304,22 +303,22 @@ void luaV_finishget_ (lua_State *L, const TValue *t, TValue *key, StkId val, tm = fasttm(L, hvalue(t)->metatable, TM_INDEX); /* table's metamethod */ if (tm == NULL) { /* no metamethod? */ setnilvalue(s2v(val)); /* result is nil */ - return; + return LUA_VNIL; } /* else will try the metamethod */ } if (ttisfunction(tm)) { /* is metamethod a function? */ luaT_callTMres(L, tm, t, key, val); /* call it */ - return; + return ttypetag(s2v(val)); } t = tm; /* else try to access 'tm[key]' */ - luaV_fastget(t, key, s2v(val), luaH_get, aux); - if (!isemptyV(aux)) - return; /* done */ + luaV_fastget(t, key, s2v(val), luaH_get, tag); + if (!tagisempty(tag)) + return tag; /* done */ /* else repeat (tail call 'luaV_finishget') */ - tag = ttypetagV(aux); } luaG_runerror(L, "'__index' chain too long; possible loop"); + return 0; /* to avoid warnings */ } @@ -1247,36 +1246,36 @@ void luaV_execute (lua_State *L, CallInfo *ci) { TValue *upval = cl->upvals[GETARG_B(i)]->v.p; TValue *rc = KC(i); TString *key = tsvalue(rc); /* key must be a short string */ - TValue aux; - luaV_fastget(upval, key, s2v(ra), luaH_getshortstr, aux); - if (isemptyV(aux)) - Protect(luaV_finishget(L, upval, rc, ra, aux)); + int tag; + luaV_fastget(upval, key, s2v(ra), luaH_getshortstr, tag); + if (tagisempty(tag)) + Protect(luaV_finishget(L, upval, rc, ra, tag)); vmbreak; } vmcase(OP_GETTABLE) { StkId ra = RA(i); TValue *rb = vRB(i); TValue *rc = vRC(i); - TValue aux; + int tag; if (ttisinteger(rc)) { /* fast track for integers? */ - luaV_fastgeti(rb, ivalue(rc), s2v(ra), aux); + luaV_fastgeti(rb, ivalue(rc), s2v(ra), tag); } else - luaV_fastget(rb, rc, s2v(ra), luaH_get, aux); - if (isemptyV(aux)) /* fast track for integers? */ - Protect(luaV_finishget(L, rb, rc, ra, aux)); + luaV_fastget(rb, rc, s2v(ra), luaH_get, tag); + if (tagisempty(tag)) + Protect(luaV_finishget(L, rb, rc, ra, tag)); vmbreak; } vmcase(OP_GETI) { StkId ra = RA(i); TValue *rb = vRB(i); int c = GETARG_C(i); - TValue aux; - luaV_fastgeti(rb, c, s2v(ra), aux); - if (isemptyV(aux)) { + int tag; + luaV_fastgeti(rb, c, s2v(ra), tag); + if (tagisempty(tag)) { TValue key; setivalue(&key, c); - Protect(luaV_finishget(L, rb, &key, ra, aux)); + Protect(luaV_finishget(L, rb, &key, ra, tag)); } vmbreak; } @@ -1285,10 +1284,10 @@ void luaV_execute (lua_State *L, CallInfo *ci) { TValue *rb = vRB(i); TValue *rc = KC(i); TString *key = tsvalue(rc); /* key must be a short string */ - TValue aux; - luaV_fastget(rb, key, s2v(ra), luaH_getshortstr, aux); - if (isemptyV(aux)) - Protect(luaV_finishget(L, rb, rc, ra, aux)); + int tag; + luaV_fastget(rb, key, s2v(ra), luaH_getshortstr, tag); + if (tagisempty(tag)) + Protect(luaV_finishget(L, rb, rc, ra, tag)); vmbreak; } vmcase(OP_SETTABUP) { @@ -1370,14 +1369,14 @@ void luaV_execute (lua_State *L, CallInfo *ci) { } vmcase(OP_SELF) { StkId ra = RA(i); - TValue aux; + int tag; TValue *rb = vRB(i); TValue *rc = RKC(i); TString *key = tsvalue(rc); /* key must be a string */ setobj2s(L, ra + 1, rb); - luaV_fastget(rb, key, s2v(ra), luaH_getstr, aux); - if (isemptyV(aux)) - Protect(luaV_finishget(L, rb, rc, ra, aux)); + luaV_fastget(rb, key, s2v(ra), luaH_getstr, tag); + if (tagisempty(tag)) + Protect(luaV_finishget(L, rb, rc, ra, tag)); vmbreak; } vmcase(OP_ADDI) { diff --git a/lvm.h b/lvm.h index 3b11e789e6..a11db83c2a 100644 --- a/lvm.h +++ b/lvm.h @@ -78,19 +78,17 @@ typedef enum { /* ** fast track for 'gettable' */ -#define luaV_fastget(t,k,res,f, aux) \ - {if (!ttistable(t)) setnotableV(aux); \ - else { aux = f(hvalue(t), k); \ - if (!isemptyV(aux)) { setobjV(cast(lua_State*, NULL), res, aux); } } } +#define luaV_fastget(t,k,res,f, tag) \ + (tag = (!ttistable(t) ? LUA_VNOTABLE : f(hvalue(t), k, res))) /* ** Special case of 'luaV_fastget' for integers, inlining the fast case ** of 'luaH_getint'. */ -#define luaV_fastgeti(t,k,res,aux) \ - { if (!ttistable(t)) setnotableV(aux); \ - else { luaH_fastgeti(hvalue(t), k, res, aux); } } +#define luaV_fastgeti(t,k,res,tag) \ + if (!ttistable(t)) tag = LUA_VNOTABLE; \ + else { luaH_fastgeti(hvalue(t), k, res, tag); } #define luaV_fastset(t,k,val,hres,f) \ @@ -122,10 +120,8 @@ LUAI_FUNC int luaV_tointeger (const TValue *obj, lua_Integer *p, F2Imod mode); LUAI_FUNC int luaV_tointegerns (const TValue *obj, lua_Integer *p, F2Imod mode); LUAI_FUNC int luaV_flttointeger (lua_Number n, lua_Integer *p, F2Imod mode); -#define luaV_finishget(L,t,key,val,aux) \ - luaV_finishget_(L,t,key,val,ttypetagV(aux)) -LUAI_FUNC void luaV_finishget_ (lua_State *L, const TValue *t, TValue *key, - StkId val, int tag); +LUAI_FUNC int luaV_finishget (lua_State *L, const TValue *t, TValue *key, + StkId val, int tag); LUAI_FUNC void luaV_finishset (lua_State *L, const TValue *t, TValue *key, TValue *val, int aux); LUAI_FUNC void luaV_finishOp (lua_State *L); From 9fa63a62682c1353eeabd4575152941fa6f3e70f Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 22 Mar 2024 14:06:11 -0300 Subject: [PATCH 0896/1145] Some 'unsigned int' changed to 'unsigned' 'unsigned int' is too long sometimes. (We already write 'long' instead of 'long int'...) --- lcode.h | 2 +- llimits.h | 2 +- lstate.c | 2 +- lstring.c | 6 +++--- lstring.h | 4 ++-- ltable.c | 20 ++++++++++---------- ltable.h | 8 ++++---- ltablib.c | 3 +-- ltests.c | 2 +- 9 files changed, 24 insertions(+), 25 deletions(-) diff --git a/lcode.h b/lcode.h index 0b971fc435..5b8eb29e25 100644 --- a/lcode.h +++ b/lcode.h @@ -60,7 +60,7 @@ typedef enum UnOpr { OPR_MINUS, OPR_BNOT, OPR_NOT, OPR_LEN, OPR_NOUNOPR } UnOpr; #define luaK_jumpto(fs,t) luaK_patchlist(fs, luaK_jump(fs), t) LUAI_FUNC int luaK_code (FuncState *fs, Instruction i); -LUAI_FUNC int luaK_codeABx (FuncState *fs, OpCode o, int A, unsigned int Bx); +LUAI_FUNC int luaK_codeABx (FuncState *fs, OpCode o, int A, unsigned Bx); LUAI_FUNC int luaK_codeABCk (FuncState *fs, OpCode o, int A, int B, int C, int k); LUAI_FUNC int luaK_exp2const (FuncState *fs, const expdesc *e, TValue *v); diff --git a/llimits.h b/llimits.h index 3bcd1b7f36..2adbd32e7d 100644 --- a/llimits.h +++ b/llimits.h @@ -91,7 +91,7 @@ typedef signed char ls_byte; #define L_P2I size_t #endif -#define point2uint(p) ((unsigned int)((L_P2I)(p) & UINT_MAX)) +#define point2uint(p) cast_uint((L_P2I)(p) & UINT_MAX) diff --git a/lstate.c b/lstate.c index 2ae76d8c8a..c3422589a0 100644 --- a/lstate.c +++ b/lstate.c @@ -320,7 +320,7 @@ LUA_API int lua_closethread (lua_State *L, lua_State *from) { } -LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud, unsigned int seed) { +LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud, unsigned seed) { int i; lua_State *L; global_State *g; diff --git a/lstring.c b/lstring.c index 9570e79889..a374c9652c 100644 --- a/lstring.c +++ b/lstring.c @@ -40,7 +40,7 @@ int luaS_eqlngstr (TString *a, TString *b) { } -unsigned int luaS_hash (const char *str, size_t l, unsigned int seed) { +unsigned luaS_hash (const char *str, size_t l, unsigned seed) { unsigned int h = seed ^ cast_uint(l); for (; l > 0; l--) h ^= ((h<<5) + (h>>2) + cast_byte(str[l - 1])); @@ -48,7 +48,7 @@ unsigned int luaS_hash (const char *str, size_t l, unsigned int seed) { } -unsigned int luaS_hashlongstr (TString *ts) { +unsigned luaS_hashlongstr (TString *ts) { lua_assert(ts->tt == LUA_VLNGSTR); if (ts->extra == 0) { /* no hash? */ size_t len = ts->u.lnglen; @@ -155,7 +155,7 @@ size_t luaS_sizelngstr (size_t len, int kind) { ** creates a new string object */ static TString *createstrobj (lua_State *L, size_t totalsize, int tag, - unsigned int h) { + unsigned h) { TString *ts; GCObject *o; o = luaC_newobj(L, tag, totalsize); diff --git a/lstring.h b/lstring.h index e321bd4312..b7226d8310 100644 --- a/lstring.h +++ b/lstring.h @@ -43,8 +43,8 @@ #define eqshrstr(a,b) check_exp((a)->tt == LUA_VSHRSTR, (a) == (b)) -LUAI_FUNC unsigned int luaS_hash (const char *str, size_t l, unsigned int seed); -LUAI_FUNC unsigned int luaS_hashlongstr (TString *ts); +LUAI_FUNC unsigned luaS_hash (const char *str, size_t l, unsigned seed); +LUAI_FUNC unsigned luaS_hashlongstr (TString *ts); LUAI_FUNC int luaS_eqlngstr (TString *a, TString *b); LUAI_FUNC void luaS_resize (lua_State *L, int newsize); LUAI_FUNC void luaS_clearcache (global_State *g); diff --git a/ltable.c b/ltable.c index f62f36bc26..ef19a5c5d5 100644 --- a/ltable.c +++ b/ltable.c @@ -358,8 +358,8 @@ static unsigned int arrayindex (lua_Integer k) { ** elements in the array part, then elements in the hash part. The ** beginning of a traversal is signaled by 0. */ -static unsigned int findindex (lua_State *L, Table *t, TValue *key, - unsigned int asize) { +static unsigned findindex (lua_State *L, Table *t, TValue *key, + unsigned asize) { unsigned int i; if (ttisnil(key)) return 0; /* first iteration */ i = ttisinteger(key) ? arrayindex(ivalue(key)) : 0; @@ -462,7 +462,7 @@ static int keyinarray (Table *t, lua_Integer key) { ** will go to the array part; return the optimal size. (The condition ** 'twotoi > 0' in the for loop stops the loop if 'twotoi' overflows.) */ -static unsigned int computesizes (unsigned int nums[], unsigned int *pna) { +static unsigned computesizes (unsigned nums[], unsigned *pna) { int i; unsigned int twotoi; /* 2^i (candidate for optimal size) */ unsigned int a = 0; /* number of elements smaller than 2^i */ @@ -506,7 +506,7 @@ l_sinline int arraykeyisempty (const Table *t, lua_Integer key) { ** number of keys that will go into corresponding slice and return ** total number of non-nil keys. */ -static unsigned int numusearray (const Table *t, unsigned int *nums) { +static unsigned numusearray (const Table *t, unsigned *nums) { int lg; unsigned int ttlg; /* 2^lg */ unsigned int ause = 0; /* summation of 'nums' */ @@ -533,7 +533,7 @@ static unsigned int numusearray (const Table *t, unsigned int *nums) { } -static int numusehash (const Table *t, unsigned int *nums, unsigned int *pna) { +static int numusehash (const Table *t, unsigned *nums, unsigned *pna) { int totaluse = 0; /* total number of elements */ int ause = 0; /* elements added to 'nums' (can go to array part) */ int i = sizenode(t); @@ -567,8 +567,8 @@ static size_t concretesize (unsigned int size) { static ArrayCell *resizearray (lua_State *L , Table *t, - unsigned int oldasize, - unsigned int newasize) { + unsigned oldasize, + unsigned newasize) { size_t oldasizeb = concretesize(oldasize); size_t newasizeb = concretesize(newasize); void *a = luaM_reallocvector(L, t->array, oldasizeb, newasizeb, lu_byte); @@ -583,7 +583,7 @@ static ArrayCell *resizearray (lua_State *L , Table *t, ** comparison ensures that the shift in the second one does not ** overflow. */ -static void setnodevector (lua_State *L, Table *t, unsigned int size) { +static void setnodevector (lua_State *L, Table *t, unsigned size) { if (size == 0) { /* no elements to hash part? */ t->node = cast(Node *, dummynode); /* use common 'dummynode' */ t->lsizenode = 0; @@ -695,8 +695,8 @@ static void clearNewSlice (Table *t, unsigned oldasize, unsigned newasize) { ** nils and reinserts the elements of the old hash back into the new ** parts of the table. */ -void luaH_resize (lua_State *L, Table *t, unsigned int newasize, - unsigned int nhsize) { +void luaH_resize (lua_State *L, Table *t, unsigned newasize, + unsigned nhsize) { Table newt; /* to keep the new hash part */ unsigned int oldasize = setlimittosize(t); ArrayCell *newarray; diff --git a/ltable.h b/ltable.h index 1f2ea3ee5c..4734bd5072 100644 --- a/ltable.h +++ b/ltable.h @@ -151,13 +151,13 @@ LUAI_FUNC void luaH_set (lua_State *L, Table *t, const TValue *key, LUAI_FUNC void luaH_finishset (lua_State *L, Table *t, const TValue *key, TValue *value, int hres); LUAI_FUNC Table *luaH_new (lua_State *L); -LUAI_FUNC void luaH_resize (lua_State *L, Table *t, unsigned int nasize, - unsigned int nhsize); -LUAI_FUNC void luaH_resizearray (lua_State *L, Table *t, unsigned int nasize); +LUAI_FUNC void luaH_resize (lua_State *L, Table *t, unsigned nasize, + unsigned nhsize); +LUAI_FUNC void luaH_resizearray (lua_State *L, Table *t, unsigned nasize); LUAI_FUNC void luaH_free (lua_State *L, Table *t); LUAI_FUNC int luaH_next (lua_State *L, Table *t, StkId key); LUAI_FUNC lua_Unsigned luaH_getn (Table *t); -LUAI_FUNC unsigned int luaH_realasize (const Table *t); +LUAI_FUNC unsigned luaH_realasize (const Table *t); #if defined(LUA_DEBUG) diff --git a/ltablib.c b/ltablib.c index 4c3f690015..a402daeaab 100644 --- a/ltablib.c +++ b/ltablib.c @@ -329,8 +329,7 @@ static IdxT choosePivot (IdxT lo, IdxT up, unsigned int rnd) { /* ** Quicksort algorithm (recursive function) */ -static void auxsort (lua_State *L, IdxT lo, IdxT up, - unsigned int rnd) { +static void auxsort (lua_State *L, IdxT lo, IdxT up, unsigned rnd) { while (lo < up) { /* loop for tail recursion */ IdxT p; /* Pivot index */ IdxT n; /* to be used later */ diff --git a/ltests.c b/ltests.c index 1a34870eda..4780673e70 100644 --- a/ltests.c +++ b/ltests.c @@ -1008,7 +1008,7 @@ static int table_query (lua_State *L) { lua_pushinteger(L, t->alimit); return 3; } - else if ((unsigned int)i < asize) { + else if (cast_uint(i) < asize) { lua_pushinteger(L, i); arr2obj(t, i + 1, s2v(L->top.p)); api_incr_top(L); From 86a8e74824b3ec7918e3dbeaff222bb1ea1ec22f Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 28 Mar 2024 17:11:33 -0300 Subject: [PATCH 0897/1145] Details --- lgc.c | 3 --- manual/manual.of | 14 ++++++++++---- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/lgc.c b/lgc.c index d1f5590e57..700390d25a 100644 --- a/lgc.c +++ b/lgc.c @@ -1055,7 +1055,6 @@ static void setpause (global_State *g) { l_obj threshold = applygcparam(g, PAUSE, g->marked); l_obj debt = threshold - gettotalobjs(g); if (debt < 0) debt = 0; -//printf("pause: %ld %ld\n", debt, g->marked); luaE_setdebt(g, debt); } @@ -1261,7 +1260,6 @@ static void minor2inc (lua_State *L, global_State *g, int kind) { static int checkminormajor (global_State *g, l_obj addedold1) { l_obj step = applygcparam(g, MINORMUL, g->GCmajorminor); l_obj limit = applygcparam(g, MINORMAJOR, g->GCmajorminor); -//printf("-> (%ld) major? marked: %ld limit: %ld step: %ld addedold1: %ld)\n", gettotalobjs(g), g->marked, limit, step, addedold1); return (addedold1 >= (step >> 1) || g->marked >= limit); } @@ -1410,7 +1408,6 @@ static int checkmajorminor (lua_State *L, global_State *g) { l_obj addedobjs = numobjs - g->GCmajorminor; l_obj limit = applygcparam(g, MAJORMINOR, addedobjs); l_obj tobecollected = numobjs - g->marked; -//printf("(%ld) -> minor? tobecollected: %ld limit: %ld\n", numobjs, tobecollected, limit); if (tobecollected > limit) { atomic2gen(L, g); /* return to generational mode */ setminordebt(g); diff --git a/manual/manual.of b/manual/manual.of index 3181549db0..7df32fcf37 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -3672,12 +3672,13 @@ will contain the chunk until everything created by the chunk has been collected; therefore, Lua can avoid copying to internal structures some parts of the chunk. -(In general, a fixed buffer would keep the chunk -as its contents until the end of the program, +(In general, a fixed buffer would keep its contents +until the end of the program, for instance with the chunk in ROM.) Moreover, for a fixed buffer, the reader function should return the entire chunk in the first read. -(As an example, @Lid{luaL_loadbufferx} does that.) +(As an example, @Lid{luaL_loadbufferx} does that, +which means that you can use it to load fixed buffers.) The function @Lid{lua_load} fully preserves the Lua stack through the calls to the reader function, @@ -3936,7 +3937,7 @@ This function is equivalent to @Lid{lua_pushcclosure} with no upvalues. Creates an @emphx{external string}, that is, a string that uses memory not managed by Lua. -The pointer @id{s} points to the exernal buffer +The pointer @id{s} points to the external buffer holding the string content, and @id{len} is the length of the string. The string should have a zero at its end, @@ -9361,6 +9362,11 @@ it is equivalent to @Lid{lua_closethread} with @id{from} being @id{NULL}. } +@item{ +The function @id{lua_setcstacklimit} is deprecated. +Calls to it can simply be removed. +} + @item{ The function @Lid{lua_dump} changed the way it keeps the stack through the calls to the writer function. From 88a50ffa715483e7187c0d7d6caaf708ebacf756 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 29 Mar 2024 15:10:50 -0300 Subject: [PATCH 0898/1145] Fixed dangling 'StkId' in 'luaV_finishget' Bug introduced in 05932567. --- lobject.h | 2 ++ ltm.c | 43 ++++++++++++++++++++++++------------------- ltm.h | 4 ++-- lvm.c | 10 +++++----- testes/events.lua | 9 +++++++++ 5 files changed, 42 insertions(+), 26 deletions(-) diff --git a/lobject.h b/lobject.h index b42539cf28..169512f832 100644 --- a/lobject.h +++ b/lobject.h @@ -256,6 +256,8 @@ typedef union { #define l_isfalse(o) (ttisfalse(o) || ttisnil(o)) +#define tagisfalse(t) ((t) == LUA_VFALSE || novariant(t) == LUA_TNIL) + #define setbfvalue(obj) settt_(obj, LUA_VFALSE) diff --git a/ltm.c b/ltm.c index c28f9122ee..236f3bb483 100644 --- a/ltm.c +++ b/ltm.c @@ -116,8 +116,8 @@ void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1, } -void luaT_callTMres (lua_State *L, const TValue *f, const TValue *p1, - const TValue *p2, StkId res) { +int luaT_callTMres (lua_State *L, const TValue *f, const TValue *p1, + const TValue *p2, StkId res) { ptrdiff_t result = savestack(L, res); StkId func = L->top.p; setobj2s(L, func, f); /* push function (assume EXTRA_STACK) */ @@ -131,6 +131,7 @@ void luaT_callTMres (lua_State *L, const TValue *f, const TValue *p1, luaD_callnoyield(L, func, 1); res = restorestack(L, result); setobjs2s(L, res, --L->top.p); /* move result to its place */ + return ttypetag(s2v(res)); /* return tag of the result */ } @@ -139,15 +140,16 @@ static int callbinTM (lua_State *L, const TValue *p1, const TValue *p2, const TValue *tm = luaT_gettmbyobj(L, p1, event); /* try first operand */ if (notm(tm)) tm = luaT_gettmbyobj(L, p2, event); /* try second operand */ - if (notm(tm)) return 0; - luaT_callTMres(L, tm, p1, p2, res); - return 1; + if (notm(tm)) + return -1; /* tag method not found */ + else /* call tag method and return the tag of the result */ + return luaT_callTMres(L, tm, p1, p2, res); } void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2, StkId res, TMS event) { - if (l_unlikely(!callbinTM(L, p1, p2, res, event))) { + if (l_unlikely(callbinTM(L, p1, p2, res, event) < 0)) { switch (event) { case TM_BAND: case TM_BOR: case TM_BXOR: case TM_SHL: case TM_SHR: case TM_BNOT: { @@ -164,11 +166,14 @@ void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2, } +/* +** The use of 'p1' after 'callbinTM' is safe because, when a tag +** method is not found, 'callbinTM' cannot change the stack. +*/ void luaT_tryconcatTM (lua_State *L) { - StkId top = L->top.p; - if (l_unlikely(!callbinTM(L, s2v(top - 2), s2v(top - 1), top - 2, - TM_CONCAT))) - luaG_concaterror(L, s2v(top - 2), s2v(top - 1)); + StkId p1 = L->top.p - 2; /* first argument */ + if (l_unlikely(callbinTM(L, s2v(p1), s2v(p1 + 1), p1, TM_CONCAT) < 0)) + luaG_concaterror(L, s2v(p1), s2v(p1 + 1)); } @@ -200,17 +205,17 @@ void luaT_trybiniTM (lua_State *L, const TValue *p1, lua_Integer i2, */ int luaT_callorderTM (lua_State *L, const TValue *p1, const TValue *p2, TMS event) { - if (callbinTM(L, p1, p2, L->top.p, event)) /* try original event */ - return !l_isfalse(s2v(L->top.p)); + int tag = callbinTM(L, p1, p2, L->top.p, event); /* try original event */ + if (tag >= 0) /* found tag method? */ + return !tagisfalse(tag); #if defined(LUA_COMPAT_LT_LE) else if (event == TM_LE) { - /* try '!(p2 < p1)' for '(p1 <= p2)' */ - L->ci->callstatus |= CIST_LEQ; /* mark it is doing 'lt' for 'le' */ - if (callbinTM(L, p2, p1, L->top.p, TM_LT)) { - L->ci->callstatus ^= CIST_LEQ; /* clear mark */ - return l_isfalse(s2v(L->top.p)); - } - /* else error will remove this 'ci'; no need to clear mark */ + /* try '!(p2 < p1)' for '(p1 <= p2)' */ + L->ci->callstatus |= CIST_LEQ; /* mark it is doing 'lt' for 'le' */ + tag = callbinTM(L, p2, p1, L->top.p, TM_LT); + L->ci->callstatus ^= CIST_LEQ; /* clear mark */ + if (tag >= 0) /* found tag method? */ + return tagisfalse(tag); } #endif luaG_ordererror(L, p1, p2); /* no metamethod found */ diff --git a/ltm.h b/ltm.h index 3c49713aae..df05b741f7 100644 --- a/ltm.h +++ b/ltm.h @@ -81,8 +81,8 @@ LUAI_FUNC void luaT_init (lua_State *L); LUAI_FUNC void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1, const TValue *p2, const TValue *p3); -LUAI_FUNC void luaT_callTMres (lua_State *L, const TValue *f, - const TValue *p1, const TValue *p2, StkId p3); +LUAI_FUNC int luaT_callTMres (lua_State *L, const TValue *f, + const TValue *p1, const TValue *p2, StkId p3); LUAI_FUNC void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2, StkId res, TMS event); LUAI_FUNC void luaT_tryconcatTM (lua_State *L); diff --git a/lvm.c b/lvm.c index cfa9961bff..37023afb42 100644 --- a/lvm.c +++ b/lvm.c @@ -308,8 +308,8 @@ int luaV_finishget (lua_State *L, const TValue *t, TValue *key, StkId val, /* else will try the metamethod */ } if (ttisfunction(tm)) { /* is metamethod a function? */ - luaT_callTMres(L, tm, t, key, val); /* call it */ - return ttypetag(s2v(val)); + tag = luaT_callTMres(L, tm, t, key, val); /* call it */ + return tag; /* return tag of the result */ } t = tm; /* else try to access 'tm[key]' */ luaV_fastget(t, key, s2v(val), luaH_get, tag); @@ -606,8 +606,8 @@ int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2) { if (tm == NULL) /* no TM? */ return 0; /* objects are different */ else { - luaT_callTMres(L, tm, t1, t2, L->top.p); /* call TM */ - return !l_isfalse(s2v(L->top.p)); + int tag = luaT_callTMres(L, tm, t1, t2, L->top.p); /* call TM */ + return !tagisfalse(tag); } } @@ -914,7 +914,7 @@ void luaV_finishOp (lua_State *L) { /* ** Auxiliary function for arithmetic operations over floats and others -** with two register operands. +** with two operands. */ #define op_arithf_aux(L,v1,v2,fop) { \ lua_Number n1; lua_Number n2; \ diff --git a/testes/events.lua b/testes/events.lua index 8d8563b952..5360ac301c 100644 --- a/testes/events.lua +++ b/testes/events.lua @@ -248,6 +248,15 @@ end test(Op(1), Op(2), Op(3)) +do -- test nil as false + local x = setmetatable({12}, {__eq= function (a,b) + return a[1] == b[1] or nil + end}) + assert(not (x == {20})) + assert(x == {12}) +end + + -- test `partial order' local function rawSet(x) From 3507c3380f5251a49c63f87c81c027b2664795c7 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 3 Apr 2024 16:01:23 -0300 Subject: [PATCH 0899/1145] Small simplification in 'findloader' Instead of allways adding a prefix for the next message, and then removing it if there is no message, add the prefix after each message. --- loadlib.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/loadlib.c b/loadlib.c index 4f8024c697..7b4bb16a16 100644 --- a/loadlib.c +++ b/loadlib.c @@ -621,12 +621,12 @@ static void findloader (lua_State *L, const char *name) { != LUA_TTABLE)) luaL_error(L, "'package.searchers' must be a table"); luaL_buffinit(L, &msg); + luaL_addstring(&msg, "\n\t"); /* error-message prefix for first message */ /* iterate over available searchers to find a loader */ for (i = 1; ; i++) { - luaL_addstring(&msg, "\n\t"); /* error-message prefix */ if (l_unlikely(lua_rawgeti(L, 3, i) == LUA_TNIL)) { /* no more searchers? */ lua_pop(L, 1); /* remove nil */ - luaL_buffsub(&msg, 2); /* remove prefix */ + luaL_buffsub(&msg, 2); /* remove last prefix */ luaL_pushresult(&msg); /* create error message */ luaL_error(L, "module '%s' not found:%s", name, lua_tostring(L, -1)); } @@ -637,11 +637,10 @@ static void findloader (lua_State *L, const char *name) { else if (lua_isstring(L, -2)) { /* searcher returned error message? */ lua_pop(L, 1); /* remove extra return */ luaL_addvalue(&msg); /* concatenate error message */ + luaL_addstring(&msg, "\n\t"); /* prefix for next message */ } - else { /* no error message */ + else /* no error message */ lua_pop(L, 2); /* remove both returns */ - luaL_buffsub(&msg, 2); /* remove prefix */ - } } } From 5edacafcfa36a1fa86a7b5316bacf8c6a2c47227 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 5 Apr 2024 15:35:11 -0300 Subject: [PATCH 0900/1145] Yet another representation for arrays This "linear" representation (see ltable.h for details) has worse locality than cells, but the simpler access code seems to compensate that. --- lobject.h | 5 +---- ltable.c | 67 ++++++++++++++++++++++++++++++++++++------------------- ltable.h | 37 ++++++++++++++++-------------- 3 files changed, 65 insertions(+), 44 deletions(-) diff --git a/lobject.h b/lobject.h index 169512f832..a70731f783 100644 --- a/lobject.h +++ b/lobject.h @@ -773,15 +773,12 @@ typedef union Node { #define setnorealasize(t) ((t)->flags |= BITRAS) -typedef struct ArrayCell ArrayCell; - - typedef struct Table { CommonHeader; lu_byte flags; /* 1<

#include +#include #include "lua.h" @@ -73,7 +74,7 @@ typedef union { ** MAXASIZEB is the maximum number of elements in the array part such ** that the size of the array fits in 'size_t'. */ -#define MAXASIZEB ((MAX_SIZET/sizeof(ArrayCell)) * NM) +#define MAXASIZEB (MAX_SIZET/(sizeof(Value) + 1)) /* @@ -553,26 +554,52 @@ static int numusehash (const Table *t, unsigned *nums, unsigned *pna) { /* ** Convert an "abstract size" (number of slots in an array) to ** "concrete size" (number of bytes in the array). -** If the abstract size is not a multiple of NM, the last cell is -** incomplete, so we don't need to allocate memory for the whole cell. -** 'extra' computes how many values are not needed in that last cell. -** It will be zero when 'size' is a multiple of NM, and from there it -** increases as 'size' decreases, up to (NM - 1). */ static size_t concretesize (unsigned int size) { - unsigned int numcells = (size + NM - 1) / NM; /* (size / NM) rounded up */ - unsigned int extra = NM - 1 - ((size + NM - 1) % NM); - return numcells * sizeof(ArrayCell) - extra * sizeof(Value); + return size * sizeof(Value) + size; /* space for the two arrays */ } -static ArrayCell *resizearray (lua_State *L , Table *t, +/* +** Resize the array part of a table. If new size is equal to the old, +** do nothing. Else, if new size is zero, free the old array. (It must +** be present, as the sizes are different.) Otherwise, allocate a new +** array, move the common elements to new proper position, and then +** frees old array. +** When array grows, we could reallocate it, but we still would need +** to move the elements to their new position, so the copy implicit +** in realloc is a waste. When array shrinks, it always erases some +** elements that should still be in the array, so we must reallocate in +** two steps anyway. It is simpler to always reallocate in two steps. +*/ +static Value *resizearray (lua_State *L , Table *t, unsigned oldasize, unsigned newasize) { - size_t oldasizeb = concretesize(oldasize); - size_t newasizeb = concretesize(newasize); - void *a = luaM_reallocvector(L, t->array, oldasizeb, newasizeb, lu_byte); - return cast(ArrayCell*, a); + if (oldasize == newasize) + return t->array; /* nothing to be done */ + else if (newasize == 0) { /* erasing array? */ + Value *op = t->array - oldasize; /* original array's real address */ + luaM_freemem(L, op, concretesize(oldasize)); /* free it */ + return NULL; + } + else { + size_t newasizeb = concretesize(newasize); + Value *np = cast(Value *, + luaM_reallocvector(L, NULL, 0, newasizeb, lu_byte)); + if (np == NULL) /* allocation error? */ + return NULL; + if (oldasize > 0) { + Value *op = t->array - oldasize; /* real original array */ + unsigned tomove = (oldasize < newasize) ? oldasize : newasize; + lua_assert(tomove > 0); + /* move common elements to new position */ + memcpy(np + newasize - tomove, + op + oldasize - tomove, + concretesize(tomove)); + luaM_freemem(L, op, concretesize(oldasize)); + } + return np + newasize; /* shift pointer to the end of value segment */ + } } @@ -699,7 +726,7 @@ void luaH_resize (lua_State *L, Table *t, unsigned newasize, unsigned nhsize) { Table newt; /* to keep the new hash part */ unsigned int oldasize = setlimittosize(t); - ArrayCell *newarray; + Value *newarray; if (newasize > MAXASIZE) luaG_runerror(L, "table overflow"); /* create new hash part with appropriate size into 'newt' */ @@ -777,18 +804,12 @@ Table *luaH_new (lua_State *L) { /* -** Frees a table. The assert ensures the correctness of 'concretesize', -** checking its result against the address of the last element in the -** array part of the table, computed abstractly. +** Frees a table. */ void luaH_free (lua_State *L, Table *t) { unsigned int realsize = luaH_realasize(t); - size_t sizeb = concretesize(realsize); - lua_assert((sizeb == 0 && realsize == 0) || - cast_charp(t->array) + sizeb - sizeof(Value) == - cast_charp(getArrVal(t, realsize - 1))); freehash(L, t); - luaM_freemem(L, t->array, sizeb); + resizearray(L, t, realsize, 0); luaM_free(L, t); } diff --git a/ltable.h b/ltable.h index 4734bd5072..6db197badd 100644 --- a/ltable.h +++ b/ltable.h @@ -71,7 +71,7 @@ /* ** 'luaH_get*' operations set 'res', unless the value is absent, and -** return the tag of the result, +** return the tag of the result. ** The 'luaH_pset*' (pre-set) operations set the given value and return ** HOK, unless the original value is absent. In that case, if the key ** is really absent, they return HNOTFOUND. Otherwise, if there is a @@ -86,24 +86,27 @@ /* -** The array part of a table is represented by an array of cells. -** Each cell is composed of NM tags followed by NM values, so that -** no space is wasted in padding. The last cell may be incomplete, -** that is, it may have fewer than NM values. +** The array part of a table is represented by an inverted array of +** values followed by an array of tags, to avoid wasting space with +** padding. The 'array' pointer points to the junction of the two +** arrays, so that values are indexed with negative indices and tags +** with non-negative indices. + + Values Tags + -------------------------------------------------------- + ... | Value 1 | Value 0 |0|1|... + -------------------------------------------------------- + ^ t->array + +** All accesses to 't->array' should be through the macros 'getArrTag' +** and 'getArrVal'. */ -#define NM cast_uint(sizeof(Value)) - -struct ArrayCell { - lu_byte tag[NM]; - Value value[NM]; -}; - /* Computes the address of the tag for the abstract index 'k' */ -#define getArrTag(t,k) (&(t)->array[(k)/NM].tag[(k)%NM]) +#define getArrTag(t,k) (cast(lu_byte*, (t)->array) + (k)) /* Computes the address of the value for the abstract index 'k' */ -#define getArrVal(t,k) (&(t)->array[(k)/NM].value[(k)%NM]) +#define getArrVal(t,k) ((t)->array - 1 - (k)) /* @@ -117,9 +120,9 @@ struct ArrayCell { /* -** Often, we need to check the tag of a value before moving it. These -** macros also move TValues to/from arrays, but receive the precomputed -** tag value or address as an extra argument. +** Often, we need to check the tag of a value before moving it. The +** following macros also move TValues to/from arrays, but receive the +** precomputed tag value or address as an extra argument. */ #define farr2val(h,k,tag,res) \ ((res)->tt_ = tag, (res)->value_ = *getArrVal(h,(k)-1u)) From 0897c0a4289ef3a8d45761266124613f364bef60 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 8 Apr 2024 14:28:26 -0300 Subject: [PATCH 0901/1145] 'getmode' renamed to 'getMode' The name 'getmode' conficts with a function from BSD, defined in . Although 'lbaselib.c' cannot include that header, 'onelua.c' can. --- lbaselib.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lbaselib.c b/lbaselib.c index 4238f96a64..b2da6a7719 100644 --- a/lbaselib.c +++ b/lbaselib.c @@ -336,7 +336,7 @@ static int load_aux (lua_State *L, int status, int envidx) { } -static const char *getmode (lua_State *L, int idx) { +static const char *getMode (lua_State *L, int idx) { const char *mode = luaL_optstring(L, idx, "bt"); if (strchr(mode, 'B') != NULL) /* Lua code cannot use fixed buffers */ luaL_argerror(L, idx, "invalid mode"); @@ -346,7 +346,7 @@ static const char *getmode (lua_State *L, int idx) { static int luaB_loadfile (lua_State *L) { const char *fname = luaL_optstring(L, 1, NULL); - const char *mode = getmode(L, 2); + const char *mode = getMode(L, 2); int env = (!lua_isnone(L, 3) ? 3 : 0); /* 'env' index or 0 if no 'env' */ int status = luaL_loadfilex(L, fname, mode); return load_aux(L, status, env); @@ -395,7 +395,7 @@ static int luaB_load (lua_State *L) { int status; size_t l; const char *s = lua_tolstring(L, 1, &l); - const char *mode = getmode(L, 3); + const char *mode = getMode(L, 3); int env = (!lua_isnone(L, 4) ? 4 : 0); /* 'env' index or 0 if no 'env' */ if (s != NULL) { /* loading a string? */ const char *chunkname = luaL_optstring(L, 2, s); From 9d985db7bb09c92b5b3fa660fffe5907d01e6a02 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 2 May 2024 12:03:30 -0300 Subject: [PATCH 0902/1145] New year (2024) --- lua.h | 4 ++-- manual/2html | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lua.h b/lua.h index b6934e6869..2f9d0abb40 100644 --- a/lua.h +++ b/lua.h @@ -13,7 +13,7 @@ #include -#define LUA_COPYRIGHT LUA_RELEASE " Copyright (C) 1994-2023 Lua.org, PUC-Rio" +#define LUA_COPYRIGHT LUA_RELEASE " Copyright (C) 1994-2024 Lua.org, PUC-Rio" #define LUA_AUTHORS "R. Ierusalimschy, L. H. de Figueiredo, W. Celes" @@ -525,7 +525,7 @@ struct lua_Debug { /****************************************************************************** -* Copyright (C) 1994-2023 Lua.org, PUC-Rio. +* Copyright (C) 1994-2024 Lua.org, PUC-Rio. * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the diff --git a/manual/2html b/manual/2html index bada6ee0e4..59bb4578a4 100755 --- a/manual/2html +++ b/manual/2html @@ -30,7 +30,7 @@ by Roberto Ierusalimschy, Luiz Henrique de Figueiredo, Waldemar Celes

Copyright -© 2023 Lua.org, PUC-Rio. All rights reserved. +© 2024 Lua.org, PUC-Rio. All rights reserved.


From 262dc5729a28b2bad0b6413d4eab2290d14395cf Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 8 May 2024 17:50:10 -0300 Subject: [PATCH 0903/1145] Details Corrections in comments and manual. Added note in the manual about local variables in the REPL. --- lauxlib.c | 2 +- lgc.c | 2 +- lgc.h | 2 +- lstate.h | 2 +- lua.c | 4 ++-- luaconf.h | 2 +- lvm.c | 8 ++++---- manual/manual.of | 32 +++++++++++++++++++++++--------- testes/pm.lua | 3 ++- 9 files changed, 36 insertions(+), 21 deletions(-) diff --git a/lauxlib.c b/lauxlib.c index 634c85cd2d..1f786e1547 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -1044,7 +1044,7 @@ static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) { /* -** Standard panic funcion just prints an error message. The test +** Standard panic function just prints an error message. The test ** with 'lua_type' avoids possible memory errors in 'lua_tostring'. */ static int panic (lua_State *L) { diff --git a/lgc.c b/lgc.c index 700390d25a..0ad3a16f7b 100644 --- a/lgc.c +++ b/lgc.c @@ -1541,7 +1541,7 @@ static void sweepstep (lua_State *L, global_State *g, ** object.) When 'fast' is true, 'singlestep' tries to finish a state ** "as fast as possible". In particular, it skips the propagation ** phase and leaves all objects to be traversed by the atomic phase: -** That avoids traversing twice some objects, such as theads and +** That avoids traversing twice some objects, such as threads and ** weak tables. */ static l_obj singlestep (lua_State *L, int fast) { diff --git a/lgc.h b/lgc.h index 5e474114dc..72d318ca80 100644 --- a/lgc.h +++ b/lgc.h @@ -135,7 +135,7 @@ ** ** To keep its invariants, the generational mode uses the same barriers ** also used by the incremental mode. If a young object is caught in a -** foward barrier, it cannot become old immediately, because it can +** forward barrier, it cannot become old immediately, because it can ** still point to other young objects. Instead, it becomes 'old0', ** which in the next cycle becomes 'old1'. So, 'old0' objects is ** old but can point to new and survival objects; 'old1' is old diff --git a/lstate.h b/lstate.h index 7f5674531c..2ff0d02bcf 100644 --- a/lstate.h +++ b/lstate.h @@ -259,7 +259,7 @@ typedef struct global_State { l_obj totalobjs; /* total number of objects allocated + GCdebt */ l_obj GCdebt; /* objects counted but not yet allocated */ l_obj marked; /* number of objects marked in a GC cycle */ - l_obj GCmajorminor; /* auxiliar counter to control major-minor shifts */ + l_obj GCmajorminor; /* auxiliary counter to control major-minor shifts */ stringtable strt; /* hash table for strings */ TValue l_registry; TValue nilvalue; /* a nil value */ diff --git a/lua.c b/lua.c index 6a9bb94894..9d347d7544 100644 --- a/lua.c +++ b/lua.c @@ -211,7 +211,7 @@ static int dostring (lua_State *L, const char *s, const char *name) { /* ** Receives 'globname[=modname]' and runs 'globname = require(modname)'. ** If there is no explicit modname and globname contains a '-', cut -** the sufix after '-' (the "version") to make the global name. +** the suffix after '-' (the "version") to make the global name. */ static int dolibrary (lua_State *L, char *globname) { int status; @@ -230,7 +230,7 @@ static int dolibrary (lua_State *L, char *globname) { status = docall(L, 1, 1); /* call 'require(modname)' */ if (status == LUA_OK) { if (suffix != NULL) /* is there a suffix mark? */ - *suffix = '\0'; /* remove sufix from global name */ + *suffix = '\0'; /* remove suffix from global name */ lua_setglobal(L, globname); /* globname = require(modname) */ } return report(L, status); diff --git a/luaconf.h b/luaconf.h index acebe29c99..33bb580d17 100644 --- a/luaconf.h +++ b/luaconf.h @@ -261,7 +261,7 @@ /* ** LUA_IGMARK is a mark to ignore all after it when building the ** module name (e.g., used to build the luaopen_ function name). -** Typically, the sufix after the mark is the module version, +** Typically, the suffix after the mark is the module version, ** as in "mod-v1.2.so". */ #define LUA_IGMARK "-" diff --git a/lvm.c b/lvm.c index 37023afb42..88f8fe2730 100644 --- a/lvm.c +++ b/lvm.c @@ -92,10 +92,10 @@ static int l_strton (const TValue *obj, TValue *result) { if (!cvt2num(obj)) /* is object not a string? */ return 0; else { - TString *st = tsvalue(obj); - size_t stlen; - const char *s = getlstr(st, stlen); - return (luaO_str2num(s, result) == stlen + 1); + TString *st = tsvalue(obj); + size_t stlen; + const char *s = getlstr(st, stlen); + return (luaO_str2num(s, result) == stlen + 1); } } diff --git a/manual/manual.of b/manual/manual.of index 7df32fcf37..5aea2623e3 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -289,7 +289,7 @@ Whenever there is an error, an @def{error object} is propagated with information about the error. Lua itself only generates errors whose error object is a string, -but programs may generate errors with +but programs can generate errors with any value as the error object. It is up to the Lua program or its host to handle such error objects. For historical reasons, @@ -298,7 +298,7 @@ even though it does not have to be a string. When you use @Lid{xpcall} (or @Lid{lua_pcall}, in C) -you may give a @def{message handler} +you can give a @def{message handler} to be called in case of errors. This function is called with the original error object and returns a new error object. @@ -343,7 +343,7 @@ which is then called a @def{metamethod}. In the previous example, the key is the string @St{__add} and the metamethod is the function that performs the addition. Unless stated otherwise, -a metamethod may in fact be any @x{callable value}, +a metamethod can in fact be any @x{callable value}, which is either a function or a value with a @idx{__call} metamethod. You can query the metatable of any value @@ -1421,7 +1421,7 @@ labels in Lua are considered statements too: A label is visible in the entire block where it is defined, except inside nested functions. -A goto may jump to any visible label as long as it does not +A goto can jump to any visible label as long as it does not enter into the scope of a local variable. A label should not be declared where a label with the same name is visible, @@ -4549,7 +4549,7 @@ corresponding Lua value is removed from the stack @see{constchar}. This function can raise memory errors only when converting a number to a string -(as then it may have to create a new string). +(as then it may create a new string). } @@ -6113,8 +6113,8 @@ The metatable is created by the I/O library This userdata must start with the structure @id{luaL_Stream}; it can contain other data after this initial structure. -The field @id{f} points to the corresponding C stream -(or it can be @id{NULL} to indicate an incompletely created handle). +The field @id{f} points to the corresponding C stream, +or it is @id{NULL} to indicate an incompletely created handle. The field @id{closef} points to a Lua function that will be called to close the stream when the handle is closed or collected; @@ -9239,11 +9239,25 @@ Lua repeatedly prompts and waits for a line. After reading a line, Lua first try to interpret the line as an expression. If it succeeds, it prints its value. -Otherwise, it interprets the line as a statement. -If you write an incomplete statement, +Otherwise, it interprets the line as a chunk. +If you write an incomplete chunk, the interpreter waits for its completion by issuing a different prompt. +Note that, as each complete line is read as a new chunk, +local variables do not outlive lines: +@verbatim{ +> x = 20 +> local x = 10; print(x) --> 10 +> print(x) --> 20 -- global 'x' +> do -- incomplete line +>> local x = 10; print(x) -- '>>' prompts for line completion +>> print(x) +>> end -- line completed; Lua will run it as a single chunk + --> 10 + --> 10 +} + If the global variable @defid{_PROMPT} contains a string, then its value is used as the prompt. Similarly, if the global variable @defid{_PROMPT2} contains a string, diff --git a/testes/pm.lua b/testes/pm.lua index 44454dffa8..f5889fcd07 100644 --- a/testes/pm.lua +++ b/testes/pm.lua @@ -56,7 +56,8 @@ assert(f(" \n\r*&\n\r xuxu \n\n", "%g%g%g+") == "xuxu") -- Adapt a pattern to UTF-8 local function PU (p) - -- break '?' into each individual byte of a character + -- distribute '?' into each individual byte of a character. + -- (For instance, "á?" becomes "\195?\161?".) p = string.gsub(p, "(" .. utf8.charpattern .. ")%?", function (c) return string.gsub(c, ".", "%0?") end) From cbdf4969ec425f1df1ade358425c0bf0bf811d83 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 23 May 2024 09:55:26 -0300 Subject: [PATCH 0904/1145] Manual: errors in lua_toclose are not memory errors --- lauxlib.c | 2 +- liolib.c | 2 +- manual/manual.of | 5 ++++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/lauxlib.c b/lauxlib.c index 1f786e1547..fec834d317 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -951,7 +951,7 @@ LUALIB_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len) { LUALIB_API void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) { luaL_checkstack(L, nup, "too many upvalues"); for (; l->name != NULL; l++) { /* fill the table with given functions */ - if (l->func == NULL) /* place holder? */ + if (l->func == NULL) /* placeholder? */ lua_pushboolean(L, 0); else { int i; diff --git a/liolib.c b/liolib.c index b08397da45..6879a6033a 100644 --- a/liolib.c +++ b/liolib.c @@ -773,7 +773,7 @@ static const luaL_Reg meth[] = { ** metamethods for file handles */ static const luaL_Reg metameth[] = { - {"__index", NULL}, /* place holder */ + {"__index", NULL}, /* placeholder */ {"__gc", f_gc}, {"__close", f_gc}, {"__tostring", f_tostring}, diff --git a/manual/manual.of b/manual/manual.of index 5aea2623e3..f830b01cd1 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -4475,7 +4475,7 @@ otherwise, returns @id{NULL}. } @APIEntry{void lua_toclose (lua_State *L, int index);| -@apii{0,0,m} +@apii{0,0,v} Marks the given index in the stack as a to-be-closed slot @see{to-be-closed}. @@ -4492,6 +4492,9 @@ A slot marked as to-be-closed should not be removed from the stack by any other function in the API except @Lid{lua_settop} or @Lid{lua_pop}, unless previously deactivated by @Lid{lua_closeslot}. +This function raises an error if the value at the given slot +neither has a @idx{__close} metamethod nor is a false value. + This function should not be called for an index that is equal to or below an active to-be-closed slot. From 814213b65fa4ab2b1a7216d06f68a6f3df89efcd Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 27 May 2024 11:29:39 -0300 Subject: [PATCH 0905/1145] utf8.offset returns also final position of character 'utf8.offset' returns two values: the initial and the final position of the given character. --- lutf8lib.c | 20 ++++++++++++++------ manual/manual.of | 22 ++++++++++++++-------- testes/utf8.lua | 44 +++++++++++++++++++++++++++----------------- 3 files changed, 55 insertions(+), 31 deletions(-) diff --git a/lutf8lib.c b/lutf8lib.c index 3a5b9bc38a..7b7479373d 100644 --- a/lutf8lib.c +++ b/lutf8lib.c @@ -181,8 +181,8 @@ static int utfchar (lua_State *L) { /* -** offset(s, n, [i]) -> index where n-th character counting from -** position 'i' starts; 0 means character at 'i'. +** offset(s, n, [i]) -> indices where n-th character counting from +** position 'i' starts and ends; 0 means character at 'i'. */ static int byteoffset (lua_State *L) { size_t len; @@ -217,11 +217,19 @@ static int byteoffset (lua_State *L) { } } } - if (n == 0) /* did it find given character? */ - lua_pushinteger(L, posi + 1); - else /* no such character */ + if (n != 0) { /* did not find given character? */ luaL_pushfail(L); - return 1; + return 1; + } + lua_pushinteger(L, posi + 1); /* initial position */ + if ((s[posi] & 0x80) != 0) { /* multi-byte character? */ + do { + posi++; + } while (iscontp(s + posi + 1)); /* skip to final byte */ + } + /* else one-byte character: final position is the initial one */ + lua_pushinteger(L, posi + 1); /* 'posi' now is the final position */ + return 2; } diff --git a/manual/manual.of b/manual/manual.of index f830b01cd1..359bd166b9 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -7958,21 +7958,27 @@ returns @fail plus the position of the first invalid byte. @LibEntry{utf8.offset (s, n [, i])| -Returns the position (in bytes) where the encoding of the -@id{n}-th character of @id{s} -(counting from position @id{i}) starts. +Returns the the position of the @id{n}-th character of @id{s} +(counting from byte position @id{i}) as two integers: +The index (in bytes) where its encoding starts and the +index (in bytes) where it ends. + +If the specified character is right after the end of @id{s}, +the function behaves as if there was a @Char{\0} there. +If the specified character is neither in the subject +nor right after its end, +the function returns @fail. + A negative @id{n} gets characters before position @id{i}. The default for @id{i} is 1 when @id{n} is non-negative and @T{#s + 1} otherwise, so that @T{utf8.offset(s, -n)} gets the offset of the @id{n}-th character from the end of the string. -If the specified character is neither in the subject -nor right after its end, -the function returns @fail. As a special case, -when @id{n} is 0 the function returns the start of the encoding -of the character that contains the @id{i}-th byte of @id{s}. +when @id{n} is 0 the function returns the start and end +of the encoding of the character that contains the +@id{i}-th byte of @id{s}. This function assumes that @id{s} is a valid UTF-8 string. diff --git a/testes/utf8.lua b/testes/utf8.lua index efadbd5c39..dc0f2f09d5 100644 --- a/testes/utf8.lua +++ b/testes/utf8.lua @@ -52,25 +52,35 @@ local function check (s, t, nonstrict) for i = 1, #t do assert(t[i] == t1[i]) end -- 't' is equal to 't1' for i = 1, l do -- for all codepoints - local pi = utf8.offset(s, i) -- position of i-th char + local pi, pie = utf8.offset(s, i) -- position of i-th char local pi1 = utf8.offset(s, 2, pi) -- position of next char + assert(pi1 == pie + 1) assert(string.find(string.sub(s, pi, pi1 - 1), justone)) assert(utf8.offset(s, -1, pi1) == pi) assert(utf8.offset(s, i - l - 1) == pi) assert(pi1 - pi == #utf8.char(utf8.codepoint(s, pi, pi, nonstrict))) for j = pi, pi1 - 1 do - assert(utf8.offset(s, 0, j) == pi) + local off1, off2 = utf8.offset(s, 0, j) + assert(off1 == pi and off2 == pi1 - 1) end for j = pi + 1, pi1 - 1 do assert(not utf8.len(s, j)) end - assert(utf8.len(s, pi, pi, nonstrict) == 1) - assert(utf8.len(s, pi, pi1 - 1, nonstrict) == 1) - assert(utf8.len(s, pi, -1, nonstrict) == l - i + 1) - assert(utf8.len(s, pi1, -1, nonstrict) == l - i) - assert(utf8.len(s, 1, pi, nonstrict) == i) + assert(utf8.len(s, pi, pi, nonstrict) == 1) + assert(utf8.len(s, pi, pi1 - 1, nonstrict) == 1) + assert(utf8.len(s, pi, -1, nonstrict) == l - i + 1) + assert(utf8.len(s, pi1, -1, nonstrict) == l - i) + assert(utf8.len(s, 1, pi, nonstrict) == i) end + local expected = 1 -- expected position of "current" character + for i = 1, l + 1 do + local p, e = utf8.offset(s, i) + assert(p == expected) + expected = e + 1 + end + assert(expected - 1 == #s + 1) + local i = 0 for p, c in utf8.codes(s, nonstrict) do i = i + 1 @@ -94,20 +104,20 @@ end do -- error indication in utf8.len - local function check (s, p) + local function checklen (s, p) local a, b = utf8.len(s) assert(not a and b == p) end - check("abc\xE3def", 4) - check("\xF4\x9F\xBF", 1) - check("\xF4\x9F\xBF\xBF", 1) + checklen("abc\xE3def", 4) + checklen("\xF4\x9F\xBF", 1) + checklen("\xF4\x9F\xBF\xBF", 1) -- spurious continuation bytes - check("汉字\x80", #("汉字") + 1) - check("\x80hello", 1) - check("hel\x80lo", 4) - check("汉字\xBF", #("汉字") + 1) - check("\xBFhello", 1) - check("hel\xBFlo", 4) + checklen("汉字\x80", #("汉字") + 1) + checklen("\x80hello", 1) + checklen("hel\x80lo", 4) + checklen("汉字\xBF", #("汉字") + 1) + checklen("\xBFhello", 1) + checklen("hel\xBFlo", 4) end -- errors in utf8.codes From b291008cc2a63eb19918d4cce7e58118f4154b03 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 4 Jun 2024 16:51:31 -0300 Subject: [PATCH 0906/1145] Manual for 'string.format' lists what it accepts Instead of listing what it does not accept, which is always relative. --- manual/manual.of | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/manual/manual.of b/manual/manual.of index 359bd166b9..337731fe2e 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -7308,9 +7308,12 @@ Returns a formatted version of its variable number of arguments following the description given in its first argument, which must be a string. The format string follows the same rules as the @ANSI{sprintf}. -The only differences are that the conversion specifiers and modifiers -@id{F}, @id{n}, @T{*}, @id{h}, @id{L}, and @id{l} are not supported -and that there is an extra specifier, @id{q}. +The accepted conversion specifiers are +@id{A}, @id{a}, @id{c}, @id{d}, @id{E}, @id{e}, @id{f}, @id{G}, @id{g}, +@id{i}, @id{o}, @id{p}, @id{s}, @id{u}, @id{X}, and @id{x}, +plus a non-C specifier @id{q}. +The accepted flags are @Char{-}, @Char{+}, @Char{#}, +@Char{0}, and @Char{ } (space). Both width and precision, when present, are limited to two digits. From bdc85357aa41a9610498232c2cffe7aa191e5cf6 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 4 Jun 2024 17:27:13 -0300 Subject: [PATCH 0907/1145] Bug: Active-lines for stripped vararg functions Lua seg. faults when asked to create the 'activelines' table for a vararg function with no debug information. --- ldebug.c | 36 +++++++++++++++++++----------------- manual/manual.of | 12 ++++++------ testes/db.lua | 9 +++++++++ 3 files changed, 34 insertions(+), 23 deletions(-) diff --git a/ldebug.c b/ldebug.c index daa979afed..e199decf6e 100644 --- a/ldebug.c +++ b/ldebug.c @@ -31,7 +31,7 @@ -#define noLuaClosure(f) ((f) == NULL || (f)->c.tt == LUA_VCCL) +#define LuaClosure(f) ((f) != NULL && (f)->c.tt == LUA_VLCL) static const char *funcnamefromcall (lua_State *L, CallInfo *ci, @@ -255,7 +255,7 @@ LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) { static void funcinfo (lua_Debug *ar, Closure *cl) { - if (noLuaClosure(cl)) { + if (!LuaClosure(cl)) { ar->source = "=[C]"; ar->srclen = LL("=[C]"); ar->linedefined = -1; @@ -288,29 +288,31 @@ static int nextline (const Proto *p, int currentline, int pc) { static void collectvalidlines (lua_State *L, Closure *f) { - if (noLuaClosure(f)) { + if (!LuaClosure(f)) { setnilvalue(s2v(L->top.p)); api_incr_top(L); } else { - int i; - TValue v; const Proto *p = f->l.p; int currentline = p->linedefined; Table *t = luaH_new(L); /* new table to store active lines */ sethvalue2s(L, L->top.p, t); /* push it on stack */ api_incr_top(L); - setbtvalue(&v); /* boolean 'true' to be the value of all indices */ - if (!(p->flag & PF_ISVARARG)) /* regular function? */ - i = 0; /* consider all instructions */ - else { /* vararg function */ - lua_assert(GET_OPCODE(p->code[0]) == OP_VARARGPREP); - currentline = nextline(p, currentline, 0); - i = 1; /* skip first instruction (OP_VARARGPREP) */ - } - for (; i < p->sizelineinfo; i++) { /* for each instruction */ - currentline = nextline(p, currentline, i); /* get its line */ - luaH_setint(L, t, currentline, &v); /* table[line] = true */ + if (p->lineinfo != NULL) { /* proto with debug information? */ + int i; + TValue v; + setbtvalue(&v); /* boolean 'true' to be the value of all indices */ + if (!(p->flag & PF_ISVARARG)) /* regular function? */ + i = 0; /* consider all instructions */ + else { /* vararg function */ + lua_assert(GET_OPCODE(p->code[0]) == OP_VARARGPREP); + currentline = nextline(p, currentline, 0); + i = 1; /* skip first instruction (OP_VARARGPREP) */ + } + for (; i < p->sizelineinfo; i++) { /* for each instruction */ + currentline = nextline(p, currentline, i); /* get its line */ + luaH_setint(L, t, currentline, &v); /* table[line] = true */ + } } } } @@ -339,7 +341,7 @@ static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar, } case 'u': { ar->nups = (f == NULL) ? 0 : f->c.nupvalues; - if (noLuaClosure(f)) { + if (!LuaClosure(f)) { ar->isvararg = 1; ar->nparams = 0; } diff --git a/manual/manual.of b/manual/manual.of index 337731fe2e..4fbdbf31bf 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -8942,13 +8942,13 @@ The returned table can contain all the fields returned by @Lid{lua_getinfo}, with the string @id{what} describing which fields to fill in. The default for @id{what} is to get all information available, except the table of valid lines. -If present, -the option @Char{f} +The option @Char{f} adds a field named @id{func} with the function itself. -If present, -the option @Char{L} -adds a field named @id{activelines} with the table of -valid lines. +The option @Char{L} adds a field named @id{activelines} +with the table of valid lines, +provided the function is a Lua function. +If the function has no debug information, +the table is empty. For instance, the expression @T{debug.getinfo(1,"n").name} returns a name for the current function, diff --git a/testes/db.lua b/testes/db.lua index d3758c4151..49ff8e3e89 100644 --- a/testes/db.lua +++ b/testes/db.lua @@ -49,6 +49,15 @@ do end +-- bug in 5.4.4-5.4.6: activelines in vararg functions +-- without debug information +do + local func = load(string.dump(load("print(10)"), true)) + local actl = debug.getinfo(func, "L").activelines + assert(#actl == 0) -- no line info +end + + -- test file and string names truncation local a = "function f () end" local function dostring (s, x) return load(s, x)() end From 94b503d95ef00f1e38b58b024ef45bf8973a8746 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 10 Jun 2024 12:09:35 -0300 Subject: [PATCH 0908/1145] Encoding of table indices (hres) must use C indices As the encoding of array indices is (~index), 0 is encoded as -1 and INT_MAX is encoded as INT_MIN. --- ltable.c | 12 ++++++------ ltable.h | 36 +++++++++++++++++++++--------------- ltests.c | 4 ++-- lvm.c | 2 +- 4 files changed, 30 insertions(+), 24 deletions(-) diff --git a/ltable.c b/ltable.c index e969adef82..40a4683f76 100644 --- a/ltable.c +++ b/ltable.c @@ -384,7 +384,7 @@ int luaH_next (lua_State *L, Table *t, StkId key) { int tag = *getArrTag(t, i); if (!tagisempty(tag)) { /* a non-empty entry? */ setivalue(s2v(key), i + 1); - farr2val(t, i + 1, tag, s2v(key + 1)); + farr2val(t, i, tag, s2v(key + 1)); return 1; } } @@ -692,7 +692,7 @@ static void reinsertOldSlice (lua_State *L, Table *t, unsigned oldasize, int tag = *getArrTag(t, i); if (!tagisempty(tag)) { /* a non-empty entry? */ TValue aux; - farr2val(t, i + 1, tag, &aux); /* copy entry into 'aux' */ + farr2val(t, i, tag, &aux); /* copy entry into 'aux' */ luaH_setint(L, t, i + 1, &aux); /* re-insert it into the table */ } } @@ -937,7 +937,7 @@ int luaH_getint (Table *t, lua_Integer key, TValue *res) { if (keyinarray(t, key)) { int tag = *getArrTag(t, key - 1); if (!tagisempty(tag)) - farr2val(t, key, tag, res); + farr2val(t, key - 1, tag, res); return tag; } else @@ -1048,11 +1048,11 @@ int luaH_psetint (Table *t, lua_Integer key, TValue *val) { if (keyinarray(t, key)) { lu_byte *tag = getArrTag(t, key - 1); if (!tagisempty(*tag) || checknoTM(t->metatable, TM_NEWINDEX)) { - fval2arr(t, key, tag, val); + fval2arr(t, key - 1, tag, val); return HOK; /* success */ } else - return ~cast_int(key); /* empty slot in the array part */ + return ~cast_int(key - 1); /* empty slot in the array part */ } else return finishnodeset(t, getintfromhash(t, key), val); @@ -1126,7 +1126,7 @@ void luaH_set (lua_State *L, Table *t, const TValue *key, TValue *value) { */ void luaH_setint (lua_State *L, Table *t, lua_Integer key, TValue *value) { if (keyinarray(t, key)) - obj2arr(t, key, value); + obj2arr(t, key - 1, value); else { int ok = rawfinishnodeset(getintfromhash(t, key), value); if (!ok) { diff --git a/ltable.h b/ltable.h index 6db197badd..2e7f86fd67 100644 --- a/ltable.h +++ b/ltable.h @@ -47,20 +47,20 @@ #define luaH_fastgeti(t,k,res,tag) \ - { Table *h = t; lua_Unsigned u = l_castS2U(k); \ - if ((u - 1u < h->alimit)) { \ - tag = *getArrTag(h,(u)-1u); \ + { Table *h = t; lua_Unsigned u = l_castS2U(k) - 1u; \ + if ((u < h->alimit)) { \ + tag = *getArrTag(h, u); \ if (!tagisempty(tag)) { farr2val(h, u, tag, res); }} \ - else { tag = luaH_getint(h, u, res); }} + else { tag = luaH_getint(h, (k), res); }} #define luaH_fastseti(t,k,val,hres) \ - { Table *h = t; lua_Unsigned u = l_castS2U(k); \ - if ((u - 1u < h->alimit)) { \ - lu_byte *tag = getArrTag(h,(u)-1u); \ + { Table *h = t; lua_Unsigned u = l_castS2U(k) - 1u; \ + if ((u < h->alimit)) { \ + lu_byte *tag = getArrTag(h, u); \ if (tagisempty(*tag)) hres = ~cast_int(u); \ else { fval2arr(h, u, tag, val); hres = HOK; }} \ - else { hres = luaH_psetint(h, u, val); }} + else { hres = luaH_psetint(h, k, val); }} /* results from pset */ @@ -82,6 +82,12 @@ ** in the array part, the encoding is (~array index), a negative value. ** The value HNOTATABLE is used by the fast macros to signal that the ** value being indexed is not a table. +** (The size for the array part is limited by the maximum power of two +** that fits in an unsigned integer; that is INT_MAX+1. So, the C-index +** ranges from 0, which encodes to -1, to INT_MAX, which encodes to +** INT_MIN. The size of the hash part is limited by the maximum power of +** two that fits in a signed integer; that is (INT_MAX+1)/2. So, it is +** safe to add HFIRSTNODE to any index there.) */ @@ -102,21 +108,21 @@ ** and 'getArrVal'. */ -/* Computes the address of the tag for the abstract index 'k' */ +/* Computes the address of the tag for the abstract C-index 'k' */ #define getArrTag(t,k) (cast(lu_byte*, (t)->array) + (k)) -/* Computes the address of the value for the abstract index 'k' */ +/* Computes the address of the value for the abstract C-index 'k' */ #define getArrVal(t,k) ((t)->array - 1 - (k)) /* -** Move TValues to/from arrays, using Lua indices +** Move TValues to/from arrays, using C indices */ #define arr2obj(h,k,val) \ - ((val)->tt_ = *getArrTag(h,(k)-1u), (val)->value_ = *getArrVal(h,(k)-1u)) + ((val)->tt_ = *getArrTag(h,(k)), (val)->value_ = *getArrVal(h,(k))) #define obj2arr(h,k,val) \ - (*getArrTag(h,(k)-1u) = (val)->tt_, *getArrVal(h,(k)-1u) = (val)->value_) + (*getArrTag(h,(k)) = (val)->tt_, *getArrVal(h,(k)) = (val)->value_) /* @@ -125,10 +131,10 @@ ** precomputed tag value or address as an extra argument. */ #define farr2val(h,k,tag,res) \ - ((res)->tt_ = tag, (res)->value_ = *getArrVal(h,(k)-1u)) + ((res)->tt_ = tag, (res)->value_ = *getArrVal(h,(k))) #define fval2arr(h,k,tag,val) \ - (*tag = (val)->tt_, *getArrVal(h,(k)-1u) = (val)->value_) + (*tag = (val)->tt_, *getArrVal(h,(k)) = (val)->value_) LUAI_FUNC int luaH_get (Table *t, const TValue *key, TValue *res); diff --git a/ltests.c b/ltests.c index 4780673e70..57df10e1b3 100644 --- a/ltests.c +++ b/ltests.c @@ -365,7 +365,7 @@ static void checktable (global_State *g, Table *h) { checkobjrefN(g, hgc, h->metatable); for (i = 0; i < asize; i++) { TValue aux; - arr2obj(h, i + 1, &aux); + arr2obj(h, i, &aux); checkvalref(g, hgc, &aux); } for (n = gnode(h, 0); n < limit; n++) { @@ -1010,7 +1010,7 @@ static int table_query (lua_State *L) { } else if (cast_uint(i) < asize) { lua_pushinteger(L, i); - arr2obj(t, i + 1, s2v(L->top.p)); + arr2obj(t, i, s2v(L->top.p)); api_incr_top(L); lua_pushnil(L); } diff --git a/lvm.c b/lvm.c index 88f8fe2730..7ee5f6bcf8 100644 --- a/lvm.c +++ b/lvm.c @@ -1857,7 +1857,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { luaH_resizearray(L, h, last); /* preallocate it at once */ for (; n > 0; n--) { TValue *val = s2v(ra + n); - obj2arr(h, last, val); + obj2arr(h, last - 1, val); last--; luaC_barrierback(L, obj2gco(h), val); } From bb7bb5944c9b3c868c6ab9cbe7d11b611251066b Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 12 Jun 2024 15:50:31 -0300 Subject: [PATCH 0909/1145] More disciplined use of 'errno' Set errno to zero before calling any function where we may use its errno, and check errno for zero before using it (as functions may not set it even in error). The code assumes that no function will put garbage on errno (although ISO C allows that): If any function during an operation set errno, and the operation result in an error, assume that errno has something to say. --- lauxlib.c | 16 ++++++++++++---- liolib.c | 25 +++++++++++++++++++------ loslib.c | 2 ++ 3 files changed, 33 insertions(+), 10 deletions(-) diff --git a/lauxlib.c b/lauxlib.c index fec834d317..d742fd2797 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -249,11 +249,13 @@ LUALIB_API int luaL_fileresult (lua_State *L, int stat, const char *fname) { return 1; } else { + const char *msg; luaL_pushfail(L); + msg = (en != 0) ? strerror(en) : "(no extra info)"; if (fname) - lua_pushfstring(L, "%s: %s", fname, strerror(en)); + lua_pushfstring(L, "%s: %s", fname, msg); else - lua_pushstring(L, strerror(en)); + lua_pushstring(L, msg); lua_pushinteger(L, en); return 3; } @@ -750,9 +752,12 @@ static const char *getF (lua_State *L, void *ud, size_t *size) { static int errfile (lua_State *L, const char *what, int fnameindex) { - const char *serr = strerror(errno); + int err = errno; const char *filename = lua_tostring(L, fnameindex) + 1; - lua_pushfstring(L, "cannot %s %s: %s", what, filename, serr); + if (err != 0) + lua_pushfstring(L, "cannot %s %s: %s", what, filename, strerror(err)); + else + lua_pushfstring(L, "cannot %s %s", what, filename); lua_remove(L, fnameindex); return LUA_ERRFILE; } @@ -805,6 +810,7 @@ LUALIB_API int luaL_loadfilex (lua_State *L, const char *filename, } else { lua_pushfstring(L, "@%s", filename); + errno = 0; lf.f = fopen(filename, "r"); if (lf.f == NULL) return errfile(L, "open", fnameindex); } @@ -814,6 +820,7 @@ LUALIB_API int luaL_loadfilex (lua_State *L, const char *filename, if (c == LUA_SIGNATURE[0]) { /* binary file? */ lf.n = 0; /* remove possible newline */ if (filename) { /* "real" file? */ + errno = 0; lf.f = freopen(filename, "rb", lf.f); /* reopen in binary mode */ if (lf.f == NULL) return errfile(L, "reopen", fnameindex); skipcomment(lf.f, &c); /* re-read initial portion */ @@ -823,6 +830,7 @@ LUALIB_API int luaL_loadfilex (lua_State *L, const char *filename, lf.buff[lf.n++] = c; /* 'c' is the first character of the stream */ status = lua_load(L, getF, &lf, lua_tostring(L, -1), mode); readstatus = ferror(lf.f); + errno = 0; /* no useful error number until here */ if (filename) fclose(lf.f); /* close file (even in case of errors) */ if (readstatus) { lua_settop(L, fnameindex); /* ignore results from 'lua_load' */ diff --git a/liolib.c b/liolib.c index 6879a6033a..c5075f3e78 100644 --- a/liolib.c +++ b/liolib.c @@ -245,8 +245,8 @@ static int f_gc (lua_State *L) { */ static int io_fclose (lua_State *L) { LStream *p = tolstream(L); - int res = fclose(p->f); - return luaL_fileresult(L, (res == 0), NULL); + errno = 0; + return luaL_fileresult(L, (fclose(p->f) == 0), NULL); } @@ -272,6 +272,7 @@ static int io_open (lua_State *L) { LStream *p = newfile(L); const char *md = mode; /* to traverse/check mode */ luaL_argcheck(L, l_checkmode(md), 2, "invalid mode"); + errno = 0; p->f = fopen(filename, mode); return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1; } @@ -292,6 +293,7 @@ static int io_popen (lua_State *L) { const char *mode = luaL_optstring(L, 2, "r"); LStream *p = newprefile(L); luaL_argcheck(L, l_checkmodep(mode), 2, "invalid mode"); + errno = 0; p->f = l_popen(L, filename, mode); p->closef = &io_pclose; return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1; @@ -300,6 +302,7 @@ static int io_popen (lua_State *L) { static int io_tmpfile (lua_State *L) { LStream *p = newfile(L); + errno = 0; p->f = tmpfile(); return (p->f == NULL) ? luaL_fileresult(L, 0, NULL) : 1; } @@ -567,6 +570,7 @@ static int g_read (lua_State *L, FILE *f, int first) { int nargs = lua_gettop(L) - 1; int n, success; clearerr(f); + errno = 0; if (nargs == 0) { /* no arguments? */ success = read_line(L, f, 1); n = first + 1; /* to return 1 result */ @@ -660,6 +664,7 @@ static int io_readline (lua_State *L) { static int g_write (lua_State *L, FILE *f, int arg) { int nargs = lua_gettop(L) - arg; int status = 1; + errno = 0; for (; nargs--; arg++) { if (lua_type(L, arg) == LUA_TNUMBER) { /* optimization: could be done exactly as for strings */ @@ -678,7 +683,8 @@ static int g_write (lua_State *L, FILE *f, int arg) { } if (l_likely(status)) return 1; /* file handle already on stack top */ - else return luaL_fileresult(L, status, NULL); + else + return luaL_fileresult(L, status, NULL); } @@ -703,6 +709,7 @@ static int f_seek (lua_State *L) { l_seeknum offset = (l_seeknum)p3; luaL_argcheck(L, (lua_Integer)offset == p3, 3, "not an integer in proper range"); + errno = 0; op = l_fseek(f, offset, mode[op]); if (l_unlikely(op)) return luaL_fileresult(L, 0, NULL); /* error */ @@ -719,19 +726,25 @@ static int f_setvbuf (lua_State *L) { FILE *f = tofile(L); int op = luaL_checkoption(L, 2, NULL, modenames); lua_Integer sz = luaL_optinteger(L, 3, LUAL_BUFFERSIZE); - int res = setvbuf(f, NULL, mode[op], (size_t)sz); + int res; + errno = 0; + res = setvbuf(f, NULL, mode[op], (size_t)sz); return luaL_fileresult(L, res == 0, NULL); } static int io_flush (lua_State *L) { - return luaL_fileresult(L, fflush(getiofile(L, IO_OUTPUT)) == 0, NULL); + FILE *f = getiofile(L, IO_OUTPUT); + errno = 0; + return luaL_fileresult(L, fflush(f) == 0, NULL); } static int f_flush (lua_State *L) { - return luaL_fileresult(L, fflush(tofile(L)) == 0, NULL); + FILE *f = tofile(L); + errno = 0; + return luaL_fileresult(L, fflush(f) == 0, NULL); } diff --git a/loslib.c b/loslib.c index ad5a927688..ba80d72c45 100644 --- a/loslib.c +++ b/loslib.c @@ -155,6 +155,7 @@ static int os_execute (lua_State *L) { static int os_remove (lua_State *L) { const char *filename = luaL_checkstring(L, 1); + errno = 0; return luaL_fileresult(L, remove(filename) == 0, filename); } @@ -162,6 +163,7 @@ static int os_remove (lua_State *L) { static int os_rename (lua_State *L) { const char *fromname = luaL_checkstring(L, 1); const char *toname = luaL_checkstring(L, 2); + errno = 0; return luaL_fileresult(L, rename(fromname, toname) == 0, NULL); } From d51022bf9e496ae4a7276b600d2755becc7d4323 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 12 Jun 2024 15:56:13 -0300 Subject: [PATCH 0910/1145] Bug: overlapping assignments ISO C forbids assignment of a union field to another field of the same union. --- lcode.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lcode.c b/lcode.c index 2c57fdafbc..b2c0b64f5c 100644 --- a/lcode.c +++ b/lcode.c @@ -776,7 +776,8 @@ void luaK_dischargevars (FuncState *fs, expdesc *e) { break; } case VLOCAL: { /* already in a register */ - e->u.info = e->u.var.ridx; + int temp = e->u.var.ridx; + e->u.info = temp; /* (can't do a direct assignment; values overlap) */ e->k = VNONRELOC; /* becomes a non-relocatable value */ break; } @@ -1283,8 +1284,9 @@ void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) { if (t->k == VUPVAL && !isKstr(fs, k)) /* upvalue indexed by non 'Kstr'? */ luaK_exp2anyreg(fs, t); /* put it in a register */ if (t->k == VUPVAL) { + int temp = t->u.info; /* upvalue index */ lua_assert(isKstr(fs, k)); - t->u.ind.t = t->u.info; /* upvalue index */ + t->u.ind.t = temp; /* (can't do a direct assignment; values overlap) */ t->u.ind.idx = k->u.info; /* literal short string */ t->k = VINDEXUP; } From b529aefc531276775f8827052d5594749232cf07 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 12 Jun 2024 16:02:01 -0300 Subject: [PATCH 0911/1145] Bug: luaL_traceback may need more than 5 stack slots --- lauxlib.c | 1 + ltests.c | 5 +++++ testes/errors.lua | 15 ++++++++++++++- 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/lauxlib.c b/lauxlib.c index d742fd2797..5f8e8f42ee 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -80,6 +80,7 @@ static int pushglobalfuncname (lua_State *L, lua_Debug *ar) { int top = lua_gettop(L); lua_getinfo(L, "f", ar); /* push function */ lua_getfield(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE); + luaL_checkstack(L, 6, "not enough stack"); /* slots for 'findfield' */ if (findfield(L, top + 1, 2)) { const char *name = lua_tostring(L, -1); if (strncmp(name, LUA_GNAME ".", 3) == 0) { /* name start with '_G.'? */ diff --git a/ltests.c b/ltests.c index 57df10e1b3..1f69fe034b 100644 --- a/ltests.c +++ b/ltests.c @@ -1733,6 +1733,11 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) { int nres; status = lua_resume(lua_tothread(L1, i), L, getnum, &nres); } + else if EQ("traceback") { + const char *msg = getstring; + int level = getnum; + luaL_traceback(L1, L1, msg, level); + } else if EQ("return") { int n = getnum; if (L1 != L) { diff --git a/testes/errors.lua b/testes/errors.lua index 01cfe9060c..80d91a9213 100644 --- a/testes/errors.lua +++ b/testes/errors.lua @@ -91,7 +91,7 @@ end if not T then (Message or print) - ('\n >>> testC not active: skipping memory message test <<<\n') + ('\n >>> testC not active: skipping tests for messages in C <<<\n') else print "testing memory error message" local a = {} @@ -104,6 +104,19 @@ else end) T.totalmem(0) assert(not st and msg == "not enough" .. " memory") + + -- stack space for luaL_traceback (bug in 5.4.6) + local res = T.testC[[ + # push 16 elements on the stack + pushnum 1; pushnum 1; pushnum 1; pushnum 1; pushnum 1; + pushnum 1; pushnum 1; pushnum 1; pushnum 1; pushnum 1; + pushnum 1; pushnum 1; pushnum 1; pushnum 1; pushnum 1; + pushnum 1; + # traceback should work with 4 remaining slots + traceback xuxu 1; + return 1 + ]] + assert(string.find(res, "xuxu.-main chunk")) end From aaf35336533c17cbeb9ac8137d13cd7908a13327 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 12 Jun 2024 16:04:25 -0300 Subject: [PATCH 0912/1145] Tricky _PROMPT may trigger undefined behavior in lua.c --- lua.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lua.c b/lua.c index 9d347d7544..d109acbf91 100644 --- a/lua.c +++ b/lua.c @@ -115,12 +115,13 @@ static void l_message (const char *pname, const char *msg) { /* ** Check whether 'status' is not OK and, if so, prints the error -** message on the top of the stack. It assumes that the error object -** is a string, as it was either generated by Lua or by 'msghandler'. +** message on the top of the stack. */ static int report (lua_State *L, int status) { if (status != LUA_OK) { const char *msg = lua_tostring(L, -1); + if (msg == NULL) + msg = "(error message not a string)"; l_message(progname, msg); lua_pop(L, 1); /* remove message */ } From 97ef8e7bd40340d47a9789beb06f0128d7438d0a Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 18 Jun 2024 17:14:23 -0300 Subject: [PATCH 0913/1145] GC test was not restarting collector after pause --- testes/gc.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/testes/gc.lua b/testes/gc.lua index 5b39bac11a..3f8143b194 100644 --- a/testes/gc.lua +++ b/testes/gc.lua @@ -65,10 +65,11 @@ do print("steps") return i -- number of steps end - collectgarbage"stop" if not _port then + collectgarbage"stop" assert(dosteps(10) < dosteps(2)) + collectgarbage"restart" end end From 55ac40f859ad8e28fe71a8801d49f4a4140e8aa3 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 20 Jun 2024 13:43:33 -0300 Subject: [PATCH 0914/1145] Cleaning of llimits.h Several definitions that don't need to be "global" (that is, that concerns only specific parts of the code) moved out of llimits.h, to more appropriate places. --- lapi.h | 20 ++++++++ lcode.c | 6 +-- ldo.c | 13 +++++ ldo.h | 21 +++++++- ldump.c | 1 + lgc.h | 8 +++ llex.c | 7 ++- llimits.h | 144 ++--------------------------------------------------- lobject.h | 3 ++ lopcodes.h | 6 +-- lparser.c | 2 +- lstate.c | 21 ++++++++ lstate.h | 11 ++++ lstring.c | 16 ++++-- lstring.h | 11 ++++ luaconf.h | 5 +- lvm.c | 9 ++++ lzio.c | 1 + makefile | 19 +++---- 19 files changed, 159 insertions(+), 165 deletions(-) diff --git a/lapi.h b/lapi.h index 757bf3d2e6..21be4a2412 100644 --- a/lapi.h +++ b/lapi.h @@ -12,11 +12,31 @@ #include "lstate.h" +#if defined(LUA_USE_APICHECK) +#include +#define api_check(l,e,msg) assert(e) +#else /* for testing */ +#define api_check(l,e,msg) ((void)(l), lua_assert((e) && msg)) +#endif + + + /* Increments 'L->top.p', checking for stack overflows */ #define api_incr_top(L) \ (L->top.p++, api_check(L, L->top.p <= L->ci->top.p, "stack overflow")) +/* +** macros that are executed whenever program enters the Lua core +** ('lua_lock') and leaves the core ('lua_unlock') +*/ +#if !defined(lua_lock) +#define lua_lock(L) ((void) 0) +#define lua_unlock(L) ((void) 0) +#endif + + + /* ** If a call returns too many multiple returns, the callee may not have ** stack space to accommodate all results. In this case, this macro diff --git a/lcode.c b/lcode.c index b2c0b64f5c..79c15132bc 100644 --- a/lcode.c +++ b/lcode.c @@ -331,14 +331,14 @@ static void savelineinfo (FuncState *fs, Proto *f, int line) { int pc = fs->pc - 1; /* last instruction coded */ if (abs(linedif) >= LIMLINEDIFF || fs->iwthabs++ >= MAXIWTHABS) { luaM_growvector(fs->ls->L, f->abslineinfo, fs->nabslineinfo, - f->sizeabslineinfo, AbsLineInfo, MAX_INT, "lines"); + f->sizeabslineinfo, AbsLineInfo, INT_MAX, "lines"); f->abslineinfo[fs->nabslineinfo].pc = pc; f->abslineinfo[fs->nabslineinfo++].line = line; linedif = ABSLINEINFO; /* signal that there is absolute information */ fs->iwthabs = 1; /* restart counter */ } luaM_growvector(fs->ls->L, f->lineinfo, pc, f->sizelineinfo, ls_byte, - MAX_INT, "opcodes"); + INT_MAX, "opcodes"); f->lineinfo[pc] = linedif; fs->previousline = line; /* last line saved */ } @@ -383,7 +383,7 @@ int luaK_code (FuncState *fs, Instruction i) { Proto *f = fs->f; /* put new instruction in code array */ luaM_growvector(fs->ls->L, f->code, fs->pc, f->sizecode, Instruction, - MAX_INT, "opcodes"); + INT_MAX, "opcodes"); f->code[fs->pc++] = i; savelineinfo(fs, f, fs->ls->lastline); return fs->pc - 1; /* index of new instruction */ diff --git a/ldo.c b/ldo.c index 699a9d2a75..cd6dded6d8 100644 --- a/ldo.c +++ b/ldo.c @@ -38,6 +38,19 @@ #define errorstatus(s) ((s) > LUA_YIELD) +/* +** these macros allow user-specific actions when a thread is +** resumed/yielded. +*/ +#if !defined(luai_userstateresume) +#define luai_userstateresume(L,n) ((void)L) +#endif + +#if !defined(luai_userstateyield) +#define luai_userstateyield(L,n) ((void)L) +#endif + + /* ** {====================================================== ** Error-recovery functions diff --git a/ldo.h b/ldo.h index 4bc75030d0..b52a353fda 100644 --- a/ldo.h +++ b/ldo.h @@ -23,10 +23,19 @@ ** 'condmovestack' is used in heavy tests to force a stack reallocation ** at every check. */ + +#if !defined(HARDSTACKTESTS) +#define condmovestack(L,pre,pos) ((void)0) +#else +/* realloc stack keeping its size */ +#define condmovestack(L,pre,pos) \ + { int sz_ = stacksize(L); pre; luaD_reallocstack((L), sz_, 0); pos; } +#endif + #define luaD_checkstackaux(L,n,pre,pos) \ if (l_unlikely(L->stack_last.p - L->top.p <= (n))) \ { pre; luaD_growstack(L, n, 1); pos; } \ - else { condmovestack(L,pre,pos); } + else { condmovestack(L,pre,pos); } /* In general, 'pre'/'pos' are empty (nothing to save) */ #define luaD_checkstack(L,n) luaD_checkstackaux(L,n,(void)0,(void)0) @@ -44,6 +53,16 @@ p = restorestack(L, t__)) /* 'pos' part: restore 'p' */ +/* +** Maximum depth for nested C calls, syntactical nested non-terminals, +** and other features implemented through recursion in C. (Value must +** fit in a 16-bit unsigned integer. It must also be compatible with +** the size of the C stack.) +*/ +#if !defined(LUAI_MAXCCALLS) +#define LUAI_MAXCCALLS 200 +#endif + /* type of protected functions, to be ran by 'runprotected' */ typedef void (*Pfunc) (lua_State *L, void *ud); diff --git a/ldump.c b/ldump.c index ca708a412a..a1e098567e 100644 --- a/ldump.c +++ b/ldump.c @@ -15,6 +15,7 @@ #include "lua.h" +#include "lapi.h" #include "lgc.h" #include "lobject.h" #include "lstate.h" diff --git a/lgc.h b/lgc.h index 72d318ca80..5b71ddb92c 100644 --- a/lgc.h +++ b/lgc.h @@ -211,6 +211,14 @@ ** 'condchangemem' is used only for heavy tests (forcing a full ** GC cycle on every opportunity) */ + +#if !defined(HARDMEMTESTS) +#define condchangemem(L,pre,pos) ((void)0) +#else +#define condchangemem(L,pre,pos) \ + { if (gcrunning(G(L))) { pre; luaC_fullgc(L, 0); pos; } } +#endif + #define luaC_condGC(L,pre,pos) \ { if (G(L)->GCdebt <= 0) { pre; luaC_step(L); pos;}; \ condchangemem(L,pre,pos); } diff --git a/llex.c b/llex.c index 9f20d3c836..3446f4e07a 100644 --- a/llex.c +++ b/llex.c @@ -32,6 +32,11 @@ #define next(ls) (ls->current = zgetc(ls->z)) +/* minimum size for string buffer */ +#if !defined(LUA_MINBUFFER) +#define LUA_MINBUFFER 32 +#endif + #define currIsNewline(ls) (ls->current == '\n' || ls->current == '\r') @@ -159,7 +164,7 @@ static void inclinenumber (LexState *ls) { next(ls); /* skip '\n' or '\r' */ if (currIsNewline(ls) && ls->current != old) next(ls); /* skip '\n\r' or '\r\n' */ - if (++ls->linenumber >= MAX_INT) + if (++ls->linenumber >= INT_MAX) lexerror(ls, "chunk has too many lines", 0); } diff --git a/llimits.h b/llimits.h index 2adbd32e7d..2954d2ef1c 100644 --- a/llimits.h +++ b/llimits.h @@ -46,15 +46,11 @@ typedef signed char ls_byte; #define MAX_SIZET ((size_t)(~(size_t)0)) /* -** Maximum size for strings and userdata visible for Lua (should be -** representable in a lua_Integer) +** Maximum size for strings and userdata visible for Lua; should be +** representable as a lua_Integer and as a size_t. */ #define MAX_SIZE (sizeof(size_t) < sizeof(lua_Integer) ? MAX_SIZET \ - : (size_t)(LUA_MAXINTEGER)) - - -#define MAX_INT INT_MAX /* maximum value of an int */ - + : cast_sizet(LUA_MAXINTEGER)) /* ** floor of the log2 of the maximum signed value for integral type 't'. @@ -119,15 +115,6 @@ typedef LUAI_UACINT l_uacInt; #define lua_longassert(c) ((void)0) #endif -/* -** assertion for checking API calls -*/ -#if !defined(luai_apicheck) -#define luai_apicheck(l,e) ((void)l, lua_assert(e)) -#endif - -#define api_check(l,e,msg) luai_apicheck(l,(e) && msg) - /* macro to avoid warnings about unused variables */ #if !defined(UNUSED) @@ -196,8 +183,7 @@ typedef LUAI_UACINT l_uacInt; /* -** type for virtual-machine instructions; -** must be an unsigned with (at least) 4 bytes (see details in lopcodes.h) +** An unsigned with (at least) 4 bytes */ #if LUAI_IS32INT typedef unsigned int l_uint32; @@ -205,107 +191,6 @@ typedef unsigned int l_uint32; typedef unsigned long l_uint32; #endif -typedef l_uint32 Instruction; - - - -/* -** Maximum length for short strings, that is, strings that are -** internalized. (Cannot be smaller than reserved words or tags for -** metamethods, as these strings must be internalized; -** #("function") = 8, #("__newindex") = 10.) -*/ -#if !defined(LUAI_MAXSHORTLEN) -#define LUAI_MAXSHORTLEN 40 -#endif - - -/* -** Initial size for the string table (must be power of 2). -** The Lua core alone registers ~50 strings (reserved words + -** metaevent keys + a few others). Libraries would typically add -** a few dozens more. -*/ -#if !defined(MINSTRTABSIZE) -#define MINSTRTABSIZE 128 -#endif - - -/* -** Size of cache for strings in the API. 'N' is the number of -** sets (better be a prime) and "M" is the size of each set (M == 1 -** makes a direct cache.) -*/ -#if !defined(STRCACHE_N) -#define STRCACHE_N 53 -#define STRCACHE_M 2 -#endif - - -/* minimum size for string buffer */ -#if !defined(LUA_MINBUFFER) -#define LUA_MINBUFFER 32 -#endif - - -/* -** Maximum depth for nested C calls, syntactical nested non-terminals, -** and other features implemented through recursion in C. (Value must -** fit in a 16-bit unsigned integer. It must also be compatible with -** the size of the C stack.) -*/ -#if !defined(LUAI_MAXCCALLS) -#define LUAI_MAXCCALLS 200 -#endif - - -/* -** macros that are executed whenever program enters the Lua core -** ('lua_lock') and leaves the core ('lua_unlock') -*/ -#if !defined(lua_lock) -#define lua_lock(L) ((void) 0) -#define lua_unlock(L) ((void) 0) -#endif - -/* -** macro executed during Lua functions at points where the -** function can yield. -*/ -#if !defined(luai_threadyield) -#define luai_threadyield(L) {lua_unlock(L); lua_lock(L);} -#endif - - -/* -** these macros allow user-specific actions when a thread is -** created/deleted/resumed/yielded. -*/ -#if !defined(luai_userstateopen) -#define luai_userstateopen(L) ((void)L) -#endif - -#if !defined(luai_userstateclose) -#define luai_userstateclose(L) ((void)L) -#endif - -#if !defined(luai_userstatethread) -#define luai_userstatethread(L,L1) ((void)L) -#endif - -#if !defined(luai_userstatefree) -#define luai_userstatefree(L,L1) ((void)L) -#endif - -#if !defined(luai_userstateresume) -#define luai_userstateresume(L,n) ((void)L) -#endif - -#if !defined(luai_userstateyield) -#define luai_userstateyield(L,n) ((void)L) -#endif - - /* ** The luai_num* macros define the primitive operations over numbers. @@ -359,25 +244,4 @@ typedef l_uint32 Instruction; #endif - - - -/* -** macro to control inclusion of some hard tests on stack reallocation -*/ -#if !defined(HARDSTACKTESTS) -#define condmovestack(L,pre,pos) ((void)0) -#else -/* realloc stack keeping its size */ -#define condmovestack(L,pre,pos) \ - { int sz_ = stacksize(L); pre; luaD_reallocstack((L), sz_, 0); pos; } -#endif - -#if !defined(HARDMEMTESTS) -#define condchangemem(L,pre,pos) ((void)0) -#else -#define condchangemem(L,pre,pos) \ - { if (gcrunning(G(L))) { pre; luaC_fullgc(L, 0); pos; } } -#endif - #endif diff --git a/lobject.h b/lobject.h index a70731f783..641e782c60 100644 --- a/lobject.h +++ b/lobject.h @@ -538,6 +538,9 @@ typedef struct Udata0 { #define LUA_VPROTO makevariant(LUA_TPROTO, 0) +typedef l_uint32 Instruction; + + /* ** Description of an upvalue for function prototypes */ diff --git a/lopcodes.h b/lopcodes.h index 46911cac14..6d88804225 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -71,7 +71,7 @@ enum OpMode {iABC, iABx, iAsBx, iAx, isJ}; /* basic instruction formats */ #if L_INTHASBITS(SIZE_Bx) #define MAXARG_Bx ((1<>1) /* 'sBx' is signed */ @@ -80,13 +80,13 @@ enum OpMode {iABC, iABx, iAsBx, iAx, isJ}; /* basic instruction formats */ #if L_INTHASBITS(SIZE_Ax) #define MAXARG_Ax ((1<> 1) diff --git a/lparser.c b/lparser.c index 2a84637a44..cdc8cf420a 100644 --- a/lparser.c +++ b/lparser.c @@ -859,7 +859,7 @@ static void recfield (LexState *ls, ConsControl *cc) { int reg = ls->fs->freereg; expdesc tab, key, val; if (ls->t.token == TK_NAME) { - checklimit(fs, cc->nh, MAX_INT, "items in a constructor"); + checklimit(fs, cc->nh, INT_MAX, "items in a constructor"); codename(ls, &key); } else /* ls->t.token == '[' */ diff --git a/lstate.c b/lstate.c index c3422589a0..8df86bf5b1 100644 --- a/lstate.c +++ b/lstate.c @@ -51,6 +51,27 @@ typedef struct LG { #define fromstate(L) (cast(LX *, cast(lu_byte *, (L)) - offsetof(LX, l))) +/* +** these macros allow user-specific actions when a thread is +** created/deleted +*/ +#if !defined(luai_userstateopen) +#define luai_userstateopen(L) ((void)L) +#endif + +#if !defined(luai_userstateclose) +#define luai_userstateclose(L) ((void)L) +#endif + +#if !defined(luai_userstatethread) +#define luai_userstatethread(L,L1) ((void)L) +#endif + +#if !defined(luai_userstatefree) +#define luai_userstatefree(L,L1) ((void)L) +#endif + + /* ** set GCdebt to a new value keeping the real number of allocated ** objects (totalobjs - GCdebt) invariant and avoiding overflows in diff --git a/lstate.h b/lstate.h index 2ff0d02bcf..6094016d65 100644 --- a/lstate.h +++ b/lstate.h @@ -142,6 +142,17 @@ struct lua_longjmp; /* defined in ldo.c */ #define EXTRA_STACK 5 +/* +** Size of cache for strings in the API. 'N' is the number of +** sets (better be a prime) and "M" is the size of each set. +** (M == 1 makes a direct cache.) +*/ +#if !defined(STRCACHE_N) +#define STRCACHE_N 53 +#define STRCACHE_M 2 +#endif + + #define BASIC_STACK_SIZE (2*LUA_MINSTACK) #define stacksize(th) cast_int((th)->stack_last.p - (th)->stack.p) diff --git a/lstring.c b/lstring.c index a374c9652c..86ee24114e 100644 --- a/lstring.c +++ b/lstring.c @@ -25,7 +25,17 @@ /* ** Maximum size for string table. */ -#define MAXSTRTB cast_int(luaM_limitN(MAX_INT, TString*)) +#define MAXSTRTB cast_int(luaM_limitN(INT_MAX, TString*)) + +/* +** Initial size for the string table (must be power of 2). +** The Lua core alone registers ~50 strings (reserved words + +** metaevent keys + a few others). Libraries would typically add +** a few dozens more. +*/ +#if !defined(MINSTRTABSIZE) +#define MINSTRTABSIZE 128 +#endif /* @@ -188,9 +198,9 @@ void luaS_remove (lua_State *L, TString *ts) { static void growstrtab (lua_State *L, stringtable *tb) { - if (l_unlikely(tb->nuse == MAX_INT)) { /* too many strings? */ + if (l_unlikely(tb->nuse == INT_MAX)) { /* too many strings? */ luaC_fullgc(L, 1); /* try to free some... */ - if (tb->nuse == MAX_INT) /* still too many? */ + if (tb->nuse == INT_MAX) /* still too many? */ luaM_error(L); /* cannot even create a message... */ } if (tb->size <= MAXSTRTB / 2) /* can grow string table? */ diff --git a/lstring.h b/lstring.h index b7226d8310..c88357aaba 100644 --- a/lstring.h +++ b/lstring.h @@ -19,6 +19,17 @@ #define MEMERRMSG "not enough memory" +/* +** Maximum length for short strings, that is, strings that are +** internalized. (Cannot be smaller than reserved words or tags for +** metamethods, as these strings must be internalized; +** #("function") = 8, #("__newindex") = 10.) +*/ +#if !defined(LUAI_MAXSHORTLEN) +#define LUAI_MAXSHORTLEN 40 +#endif + + /* ** Size of a short TString: Size of the header plus space for the string ** itself (including final '\0'). diff --git a/luaconf.h b/luaconf.h index 33bb580d17..fe98d9a924 100644 --- a/luaconf.h +++ b/luaconf.h @@ -722,10 +722,7 @@ @@ LUA_USE_APICHECK turns on several consistency checks on the C API. ** Define it as a help when debugging C code. */ -#if defined(LUA_USE_APICHECK) -#include -#define luai_apicheck(l,e) assert(e) -#endif +/* #define LUA_USE_APICHECK */ /* }================================================================== */ diff --git a/lvm.c b/lvm.c index 7ee5f6bcf8..940a15e60d 100644 --- a/lvm.c +++ b/lvm.c @@ -18,6 +18,7 @@ #include "lua.h" +#include "lapi.h" #include "ldebug.h" #include "ldo.h" #include "lfunc.h" @@ -1122,6 +1123,14 @@ void luaV_finishOp (lua_State *L) { */ #define halfProtect(exp) (savestate(L,ci), (exp)) +/* +** macro executed during Lua functions at points where the +** function can yield. +*/ +#if !defined(luai_threadyield) +#define luai_threadyield(L) {lua_unlock(L); lua_lock(L);} +#endif + /* 'c' is the limit of live values in the stack */ #define checkGC(L,c) \ { luaC_condGC(L, (savepc(L), L->top.p = (c)), \ diff --git a/lzio.c b/lzio.c index 78f7ac8354..301df4b94e 100644 --- a/lzio.c +++ b/lzio.c @@ -14,6 +14,7 @@ #include "lua.h" +#include "lapi.h" #include "llimits.h" #include "lmem.h" #include "lstate.h" diff --git a/makefile b/makefile index 38e21f1f57..a56c9f62c6 100644 --- a/makefile +++ b/makefile @@ -158,12 +158,13 @@ ldebug.o: ldebug.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \ ldo.o: ldo.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \ lobject.h ltm.h lzio.h lmem.h ldebug.h ldo.h lfunc.h lgc.h lopcodes.h \ lparser.h lstring.h ltable.h lundump.h lvm.h -ldump.o: ldump.c lprefix.h lua.h luaconf.h lobject.h llimits.h lstate.h \ - ltm.h lzio.h lmem.h lundump.h +ldump.o: ldump.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \ + lobject.h ltm.h lzio.h lmem.h lgc.h ltable.h lundump.h lfunc.o: lfunc.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \ llimits.h ltm.h lzio.h lmem.h ldo.h lfunc.h lgc.h lgc.o: lgc.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \ - llimits.h ltm.h lzio.h lmem.h ldo.h lfunc.h lgc.h lstring.h ltable.h + llimits.h ltm.h lzio.h lmem.h ldo.h lfunc.h lgc.h llex.h lstring.h \ + ltable.h linit.o: linit.c lprefix.h lua.h luaconf.h lualib.h lauxlib.h liolib.o: liolib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h llex.o: llex.c lprefix.h lua.h luaconf.h lctype.h llimits.h ldebug.h \ @@ -199,12 +200,12 @@ ltm.o: ltm.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \ lua.o: lua.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h lundump.o: lundump.c lprefix.h lua.h luaconf.h ldebug.h lstate.h \ lobject.h llimits.h ltm.h lzio.h lmem.h ldo.h lfunc.h lstring.h lgc.h \ - lundump.h + ltable.h lundump.h lutf8lib.o: lutf8lib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h -lvm.o: lvm.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \ - llimits.h ltm.h lzio.h lmem.h ldo.h lfunc.h lgc.h lopcodes.h lstring.h \ - ltable.h lvm.h ljumptab.h -lzio.o: lzio.c lprefix.h lua.h luaconf.h llimits.h lmem.h lstate.h \ - lobject.h ltm.h lzio.h +lvm.o: lvm.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \ + lobject.h ltm.h lzio.h lmem.h ldebug.h ldo.h lfunc.h lgc.h lopcodes.h \ + lstring.h ltable.h lvm.h ljumptab.h +lzio.o: lzio.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \ + lobject.h ltm.h lzio.h lmem.h # (end of Makefile) From a08d82eb132bfd9db5b91e0d5ebcb81d7b26dcd0 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 20 Jun 2024 14:46:06 -0300 Subject: [PATCH 0915/1145] llimits.h being used by all Lua code The definitions in llimits.h are useful not only for the core. That header only defines types and '#define's, so libs and core still do not share any real code/data. --- lauxlib.c | 1 + lauxlib.h | 15 --------------- lbaselib.c | 1 + lcorolib.c | 1 + ldblib.c | 1 + linit.c | 1 + liolib.c | 3 +-- lmathlib.c | 21 +++++++-------------- loadlib.c | 1 + loslib.c | 1 + lstrlib.c | 49 +++++++++++++++++++++++-------------------------- ltablib.c | 1 + lua.c | 1 + lutf8lib.c | 24 ++++++++---------------- 14 files changed, 48 insertions(+), 73 deletions(-) diff --git a/lauxlib.c b/lauxlib.c index 5f8e8f42ee..fe99aca1da 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -25,6 +25,7 @@ #include "lua.h" #include "lauxlib.h" +#include "llimits.h" #if !defined(MAX_SIZET) diff --git a/lauxlib.h b/lauxlib.h index 3c37068682..6c5ecbb78f 100644 --- a/lauxlib.h +++ b/lauxlib.h @@ -168,21 +168,6 @@ LUALIB_API void (luaL_requiref) (lua_State *L, const char *modname, #define luaL_pushfail(L) lua_pushnil(L) -/* -** Internal assertions for in-house debugging -*/ -#if !defined(lua_assert) - -#if defined LUAI_ASSERT - #include - #define lua_assert(c) assert(c) -#else - #define lua_assert(c) ((void)0) -#endif - -#endif - - /* ** {====================================================== diff --git a/lbaselib.c b/lbaselib.c index b2da6a7719..8b03434021 100644 --- a/lbaselib.c +++ b/lbaselib.c @@ -19,6 +19,7 @@ #include "lauxlib.h" #include "lualib.h" +#include "llimits.h" static int luaB_print (lua_State *L) { diff --git a/lcorolib.c b/lcorolib.c index c64adf08a8..3d95f8735a 100644 --- a/lcorolib.c +++ b/lcorolib.c @@ -16,6 +16,7 @@ #include "lauxlib.h" #include "lualib.h" +#include "llimits.h" static lua_State *getco (lua_State *L) { diff --git a/ldblib.c b/ldblib.c index 2c94138472..a0a06dd7f6 100644 --- a/ldblib.c +++ b/ldblib.c @@ -18,6 +18,7 @@ #include "lauxlib.h" #include "lualib.h" +#include "llimits.h" /* diff --git a/linit.c b/linit.c index 140f6d7590..00d06f7ecb 100644 --- a/linit.c +++ b/linit.c @@ -18,6 +18,7 @@ #include "lualib.h" #include "lauxlib.h" +#include "llimits.h" /* diff --git a/liolib.c b/liolib.c index c5075f3e78..4349f860bb 100644 --- a/liolib.c +++ b/liolib.c @@ -21,8 +21,7 @@ #include "lauxlib.h" #include "lualib.h" - - +#include "llimits.h" /* diff --git a/lmathlib.c b/lmathlib.c index c1041f37b5..2bdcb63758 100644 --- a/lmathlib.c +++ b/lmathlib.c @@ -20,6 +20,7 @@ #include "lauxlib.h" #include "lualib.h" +#include "llimits.h" #undef PI @@ -366,25 +367,17 @@ static lua_Number I2d (Rand64 x) { #else /* no 'Rand64' }{ */ -/* get an integer with at least 32 bits */ -#if LUAI_IS32INT -typedef unsigned int lu_int32; -#else -typedef unsigned long lu_int32; -#endif - - /* ** Use two 32-bit integers to represent a 64-bit quantity. */ typedef struct Rand64 { - lu_int32 h; /* higher half */ - lu_int32 l; /* lower half */ + l_uint32 h; /* higher half */ + l_uint32 l; /* lower half */ } Rand64; /* -** If 'lu_int32' has more than 32 bits, the extra bits do not interfere +** If 'l_uint32' has more than 32 bits, the extra bits do not interfere ** with the 32 initial bits, except in a right shift and comparisons. ** Moreover, the final result has to discard the extra bits. */ @@ -398,7 +391,7 @@ typedef struct Rand64 { */ /* build a new Rand64 value */ -static Rand64 packI (lu_int32 h, lu_int32 l) { +static Rand64 packI (l_uint32 h, l_uint32 l) { Rand64 result; result.h = h; result.l = l; @@ -471,7 +464,7 @@ static Rand64 nextrand (Rand64 *state) { */ /* an unsigned 1 with proper type */ -#define UONE ((lu_int32)1) +#define UONE ((l_uint32)1) #if FIGS <= 32 @@ -522,7 +515,7 @@ static lua_Unsigned I2UInt (Rand64 x) { /* convert a 'lua_Unsigned' to a 'Rand64' */ static Rand64 Int2I (lua_Unsigned n) { - return packI((lu_int32)((n >> 31) >> 1), (lu_int32)n); + return packI((l_uint32)((n >> 31) >> 1), (l_uint32)n); } #endif /* } */ diff --git a/loadlib.c b/loadlib.c index 7b4bb16a16..45db3b7216 100644 --- a/loadlib.c +++ b/loadlib.c @@ -22,6 +22,7 @@ #include "lauxlib.h" #include "lualib.h" +#include "llimits.h" /* diff --git a/loslib.c b/loslib.c index ba80d72c45..8280331b77 100644 --- a/loslib.c +++ b/loslib.c @@ -20,6 +20,7 @@ #include "lauxlib.h" #include "lualib.h" +#include "llimits.h" /* diff --git a/lstrlib.c b/lstrlib.c index a90c4fd161..97d974f8c8 100644 --- a/lstrlib.c +++ b/lstrlib.c @@ -24,6 +24,7 @@ #include "lauxlib.h" #include "lualib.h" +#include "llimits.h" /* @@ -36,10 +37,6 @@ #endif -/* macro to 'unsign' a character */ -#define uchar(c) ((unsigned char)(c)) - - /* ** Some sizes are better limited to fit in 'int', but must also fit in ** 'size_t'. (We assume that 'lua_Integer' cannot be smaller than 'int'.) @@ -128,7 +125,7 @@ static int str_lower (lua_State *L) { const char *s = luaL_checklstring(L, 1, &l); char *p = luaL_buffinitsize(L, &b, l); for (i=0; i= ms->src_end) return 0; else { - int c = uchar(*s); + int c = cast_uchar(*s); switch (*p) { case '.': return 1; /* matches any char */ - case L_ESC: return match_class(c, uchar(*(p+1))); + case L_ESC: return match_class(c, cast_uchar(*(p+1))); case '[': return matchbracketclass(c, p, ep-1); - default: return (uchar(*p) == c); + default: return (cast_uchar(*p) == c); } } } @@ -612,8 +609,8 @@ static const char *match (MatchState *ms, const char *s, const char *p) { luaL_error(ms->L, "missing '[' after '%%f' in pattern"); ep = classend(ms, p); /* points to what is next */ previous = (s == ms->src_init) ? '\0' : *(s - 1); - if (!matchbracketclass(uchar(previous), p, ep - 1) && - matchbracketclass(uchar(*s), p, ep - 1)) { + if (!matchbracketclass(cast_uchar(previous), p, ep - 1) && + matchbracketclass(cast_uchar(*s), p, ep - 1)) { p = ep; goto init; /* return match(ms, s, ep); */ } s = NULL; /* match failed */ @@ -622,7 +619,7 @@ static const char *match (MatchState *ms, const char *s, const char *p) { case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': { /* capture results (%0-%9)? */ - s = match_capture(ms, s, uchar(*(p + 1))); + s = match_capture(ms, s, cast_uchar(*(p + 1))); if (s != NULL) { p += 2; goto init; /* return match(ms, s, p + 2) */ } @@ -887,7 +884,7 @@ static void add_s (MatchState *ms, luaL_Buffer *b, const char *s, luaL_addchar(b, *p); else if (*p == '0') /* '%0' */ luaL_addlstring(b, s, e - s); - else if (isdigit(uchar(*p))) { /* '%n' */ + else if (isdigit(cast_uchar(*p))) { /* '%n' */ const char *cap; ptrdiff_t resl = get_onecapture(ms, *p - '1', s, e, &cap); if (resl == CAP_POSITION) @@ -1065,7 +1062,7 @@ static int lua_number2strx (lua_State *L, char *buff, int sz, if (fmt[SIZELENMOD] == 'A') { int i; for (i = 0; i < n; i++) - buff[i] = toupper(uchar(buff[i])); + buff[i] = toupper(cast_uchar(buff[i])); } else if (l_unlikely(fmt[SIZELENMOD] != 'a')) return luaL_error(L, "modifiers for format '%%a'/'%%A' not implemented"); @@ -1132,12 +1129,12 @@ static void addquoted (luaL_Buffer *b, const char *s, size_t len) { luaL_addchar(b, '\\'); luaL_addchar(b, *s); } - else if (iscntrl(uchar(*s))) { + else if (iscntrl(cast_uchar(*s))) { char buff[10]; - if (!isdigit(uchar(*(s+1)))) - l_sprintf(buff, sizeof(buff), "\\%d", (int)uchar(*s)); + if (!isdigit(cast_uchar(*(s+1)))) + l_sprintf(buff, sizeof(buff), "\\%d", (int)cast_uchar(*s)); else - l_sprintf(buff, sizeof(buff), "\\%03d", (int)uchar(*s)); + l_sprintf(buff, sizeof(buff), "\\%03d", (int)cast_uchar(*s)); luaL_addstring(b, buff); } else @@ -1214,9 +1211,9 @@ static void addliteral (lua_State *L, luaL_Buffer *b, int arg) { static const char *get2digits (const char *s) { - if (isdigit(uchar(*s))) { + if (isdigit(cast_uchar(*s))) { s++; - if (isdigit(uchar(*s))) s++; /* (2 digits at most) */ + if (isdigit(cast_uchar(*s))) s++; /* (2 digits at most) */ } return s; } @@ -1239,7 +1236,7 @@ static void checkformat (lua_State *L, const char *form, const char *flags, spec = get2digits(spec); /* skip precision */ } } - if (!isalpha(uchar(*spec))) /* did not go to the end? */ + if (!isalpha(cast_uchar(*spec))) /* did not go to the end? */ luaL_error(L, "invalid conversion specification: '%s'", form); } diff --git a/ltablib.c b/ltablib.c index a402daeaab..b59485911e 100644 --- a/ltablib.c +++ b/ltablib.c @@ -18,6 +18,7 @@ #include "lauxlib.h" #include "lualib.h" +#include "llimits.h" /* diff --git a/lua.c b/lua.c index d109acbf91..88fb8793fa 100644 --- a/lua.c +++ b/lua.c @@ -19,6 +19,7 @@ #include "lauxlib.h" #include "lualib.h" +#include "llimits.h" #if !defined(LUA_PROGNAME) diff --git a/lutf8lib.c b/lutf8lib.c index 7b7479373d..243196c8ca 100644 --- a/lutf8lib.c +++ b/lutf8lib.c @@ -19,6 +19,7 @@ #include "lauxlib.h" #include "lualib.h" +#include "llimits.h" #define MAXUNICODE 0x10FFFFu @@ -28,15 +29,6 @@ #define MSGInvalid "invalid UTF-8 code" -/* -** Integer type for decoded UTF-8 values; MAXUTF needs 31 bits. -*/ -#if (UINT_MAX >> 30) >= 1 -typedef unsigned int utfint; -#else -typedef unsigned long utfint; -#endif - #define iscont(c) (((c) & 0xC0) == 0x80) #define iscontp(p) iscont(*(p)) @@ -58,11 +50,11 @@ static lua_Integer u_posrelat (lua_Integer pos, size_t len) { ** entry forces an error for non-ascii bytes with no continuation ** bytes (count == 0). */ -static const char *utf8_decode (const char *s, utfint *val, int strict) { - static const utfint limits[] = - {~(utfint)0, 0x80, 0x800, 0x10000u, 0x200000u, 0x4000000u}; +static const char *utf8_decode (const char *s, l_uint32 *val, int strict) { + static const l_uint32 limits[] = + {~(l_uint32)0, 0x80, 0x800, 0x10000u, 0x200000u, 0x4000000u}; unsigned int c = (unsigned char)s[0]; - utfint res = 0; /* final result */ + l_uint32 res = 0; /* final result */ if (c < 0x80) /* ascii? */ res = c; else { @@ -73,7 +65,7 @@ static const char *utf8_decode (const char *s, utfint *val, int strict) { return NULL; /* invalid byte sequence */ res = (res << 6) | (cc & 0x3F); /* add lower 6 bits from cont. byte */ } - res |= ((utfint)(c & 0x7F) << (count * 5)); /* add first byte */ + res |= ((l_uint32)(c & 0x7F) << (count * 5)); /* add first byte */ if (count > 5 || res > MAXUTF || res < limits[count]) return NULL; /* invalid byte sequence */ s += count; /* skip continuation bytes read */ @@ -141,7 +133,7 @@ static int codepoint (lua_State *L) { n = 0; /* count the number of returns */ se = s + pose; /* string end */ for (s += posi - 1; s < se;) { - utfint code; + l_uint32 code; s = utf8_decode(s, &code, !lax); if (s == NULL) return luaL_error(L, MSGInvalid); @@ -243,7 +235,7 @@ static int iter_aux (lua_State *L, int strict) { if (n >= len) /* (also handles original 'n' being negative) */ return 0; /* no more codepoints */ else { - utfint code; + l_uint32 code; const char *next = utf8_decode(s + n, &code, strict); if (next == NULL || iscontp(next)) return luaL_error(L, MSGInvalid); From e24ce8c2b322226bbc211e57f301c265a2622c4b Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 21 Jun 2024 12:29:08 -0300 Subject: [PATCH 0916/1145] lua_writestring & co. moved to llimits.h They don't need to be visible by clients of Lua. --- lauxlib.c | 6 ------ lauxlib.h | 24 ------------------------ llimits.h | 25 +++++++++++++++++++++++++ lstrlib.c | 2 -- 4 files changed, 25 insertions(+), 32 deletions(-) diff --git a/lauxlib.c b/lauxlib.c index fe99aca1da..99a6309202 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -28,12 +28,6 @@ #include "llimits.h" -#if !defined(MAX_SIZET) -/* maximum value for size_t */ -#define MAX_SIZET ((size_t)(~(size_t)0)) -#endif - - /* ** {====================================================== ** Traceback diff --git a/lauxlib.h b/lauxlib.h index 6c5ecbb78f..4be008b90d 100644 --- a/lauxlib.h +++ b/lauxlib.h @@ -236,30 +236,6 @@ typedef struct luaL_Stream { /* }====================================================== */ -/* -** {================================================================== -** "Abstraction Layer" for basic report of messages and errors -** =================================================================== -*/ - -/* print a string */ -#if !defined(lua_writestring) -#define lua_writestring(s,l) fwrite((s), sizeof(char), (l), stdout) -#endif - -/* print a newline and flush the output */ -#if !defined(lua_writeline) -#define lua_writeline() (lua_writestring("\n", 1), fflush(stdout)) -#endif - -/* print an error message */ -#if !defined(lua_writestringerror) -#define lua_writestringerror(s,p) \ - (fprintf(stderr, (s), (p)), fflush(stderr)) -#endif - -/* }================================================================== */ - /* ** {============================================================ diff --git a/llimits.h b/llimits.h index 2954d2ef1c..57e7bed7b0 100644 --- a/llimits.h +++ b/llimits.h @@ -244,4 +244,29 @@ typedef unsigned long l_uint32; #endif +/* +** {================================================================== +** "Abstraction Layer" for basic report of messages and errors +** =================================================================== +*/ + +/* print a string */ +#if !defined(lua_writestring) +#define lua_writestring(s,l) fwrite((s), sizeof(char), (l), stdout) #endif + +/* print a newline and flush the output */ +#if !defined(lua_writeline) +#define lua_writeline() (lua_writestring("\n", 1), fflush(stdout)) +#endif + +/* print an error message */ +#if !defined(lua_writestringerror) +#define lua_writestringerror(s,p) \ + (fprintf(stderr, (s), (p)), fflush(stderr)) +#endif + +/* }================================================================== */ + +#endif + diff --git a/lstrlib.c b/lstrlib.c index 97d974f8c8..ab33bffe54 100644 --- a/lstrlib.c +++ b/lstrlib.c @@ -41,8 +41,6 @@ ** Some sizes are better limited to fit in 'int', but must also fit in ** 'size_t'. (We assume that 'lua_Integer' cannot be smaller than 'int'.) */ -#define MAX_SIZET ((size_t)(~(size_t)0)) - #define MAXSIZE \ (sizeof(size_t) < sizeof(int) ? MAX_SIZET : (size_t)(INT_MAX)) From ec65ab878e04822f1cbcc3198f19076d57900e9f Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 21 Jun 2024 14:55:12 -0300 Subject: [PATCH 0917/1145] Removed 'int' size limit for pack/unpack --- lstrlib.c | 67 +++++++++++++++++++++++++++--------------------- testes/tpack.lua | 19 +++++++------- 2 files changed, 48 insertions(+), 38 deletions(-) diff --git a/lstrlib.c b/lstrlib.c index ab33bffe54..eb38b67d98 100644 --- a/lstrlib.c +++ b/lstrlib.c @@ -1447,14 +1447,14 @@ typedef enum KOption { */ static int digit (int c) { return '0' <= c && c <= '9'; } -static int getnum (const char **fmt, int df) { +static size_t getnum (const char **fmt, size_t df) { if (!digit(**fmt)) /* no number? */ return df; /* return default value */ else { - int a = 0; + size_t a = 0; do { a = a*10 + (*((*fmt)++) - '0'); - } while (digit(**fmt) && a <= ((int)MAXSIZE - 9)/10); + } while (digit(**fmt) && a <= (MAX_SIZE - 9)/10); return a; } } @@ -1462,14 +1462,14 @@ static int getnum (const char **fmt, int df) { /* ** Read an integer numeral and raises an error if it is larger -** than the maximum size for integers. +** than the maximum size of integers. */ static int getnumlimit (Header *h, const char **fmt, int df) { - int sz = getnum(fmt, df); - if (l_unlikely(sz > MAXINTSIZE || sz <= 0)) + size_t sz = getnum(fmt, df); + if (l_unlikely((sz - 1u) >= MAXINTSIZE)) return luaL_error(h->L, "integral size (%d) out of limits [1,%d]", sz, MAXINTSIZE); - return sz; + return cast_int(sz); } @@ -1486,7 +1486,7 @@ static void initheader (lua_State *L, Header *h) { /* ** Read and classify next option. 'size' is filled with option's size. */ -static KOption getoption (Header *h, const char **fmt, int *size) { +static KOption getoption (Header *h, const char **fmt, size_t *size) { /* dummy structure to get native alignment requirements */ struct cD { char c; union { LUAI_MAXALIGN; } u; }; int opt = *((*fmt)++); @@ -1508,8 +1508,8 @@ static KOption getoption (Header *h, const char **fmt, int *size) { case 'I': *size = getnumlimit(h, fmt, sizeof(int)); return Kuint; case 's': *size = getnumlimit(h, fmt, sizeof(size_t)); return Kstring; case 'c': - *size = getnum(fmt, -1); - if (l_unlikely(*size == -1)) + *size = getnum(fmt, cast_sizet(-1)); + if (l_unlikely(*size == cast_sizet(-1))) luaL_error(h->L, "missing size for format option 'c'"); return Kchar; case 'z': return Kzstr; @@ -1540,9 +1540,9 @@ static KOption getoption (Header *h, const char **fmt, int *size) { ** despite its size. */ static KOption getdetails (Header *h, size_t totalsize, - const char **fmt, int *psize, int *ntoalign) { + const char **fmt, size_t *psize, int *ntoalign) { KOption opt = getoption(h, fmt, psize); - int align = *psize; /* usually, alignment follows size */ + size_t align = *psize; /* usually, alignment follows size */ if (opt == Kpaddalign) { /* 'X' gets alignment from following option */ if (**fmt == '\0' || getoption(h, fmt, &align) == Kchar || align == 0) luaL_argerror(h->L, 1, "invalid next option for option 'X'"); @@ -1550,9 +1550,9 @@ static KOption getdetails (Header *h, size_t totalsize, if (align <= 1 || opt == Kchar) /* need no alignment? */ *ntoalign = 0; else { - if (align > h->maxalign) /* enforce maximum alignment */ + if (align > cast_sizet(h->maxalign)) /* enforce maximum alignment */ align = h->maxalign; - if (l_unlikely((align & (align - 1)) != 0)) /* not a power of 2? */ + if (l_unlikely(!ispow2(align))) /* not a power of 2? */ luaL_argerror(h->L, 1, "format asks for alignment not power of 2"); *ntoalign = (align - (int)(totalsize & (align - 1))) & (align - 1); } @@ -1609,8 +1609,11 @@ static int str_pack (lua_State *L) { lua_pushnil(L); /* mark to separate arguments from string buffer */ luaL_buffinit(L, &b); while (*fmt != '\0') { - int size, ntoalign; + int ntoalign; + size_t size; KOption opt = getdetails(&h, totalsize, &fmt, &size, &ntoalign); + luaL_argcheck(L, size + ntoalign <= MAX_SIZE - totalsize, arg, + "result too long"); totalsize += ntoalign + size; while (ntoalign-- > 0) luaL_addchar(&b, LUAL_PACKPADBYTE); /* fill alignment */ @@ -1660,18 +1663,21 @@ static int str_pack (lua_State *L) { case Kchar: { /* fixed-size string */ size_t len; const char *s = luaL_checklstring(L, arg, &len); - luaL_argcheck(L, len <= (size_t)size, arg, - "string longer than given size"); + luaL_argcheck(L, len <= size, arg, "string longer than given size"); luaL_addlstring(&b, s, len); /* add string */ - while (len++ < (size_t)size) /* pad extra space */ - luaL_addchar(&b, LUAL_PACKPADBYTE); + if (len < size) { /* does it need padding? */ + size_t psize = size - len; /* pad size */ + char *buff = luaL_prepbuffsize(&b, psize); + memset(buff, LUAL_PACKPADBYTE, psize); + luaL_addsize(&b, psize); + } break; } case Kstring: { /* strings with length count */ size_t len; const char *s = luaL_checklstring(L, arg, &len); - luaL_argcheck(L, size >= (int)sizeof(size_t) || - len < ((size_t)1 << (size * NB)), + luaL_argcheck(L, size >= sizeof(lua_Unsigned) || + len < ((lua_Unsigned)1 << (size * NB)), arg, "string length does not fit in given size"); packint(&b, (lua_Unsigned)len, h.islittle, size, 0); /* pack length */ luaL_addlstring(&b, s, len); @@ -1701,19 +1707,20 @@ static int str_pack (lua_State *L) { static int str_packsize (lua_State *L) { Header h; const char *fmt = luaL_checkstring(L, 1); /* format string */ - size_t totalsize = 0; /* accumulate total size of result */ + lua_Integer totalsize = 0; /* accumulate total size of result */ initheader(L, &h); while (*fmt != '\0') { - int size, ntoalign; + int ntoalign; + size_t size; KOption opt = getdetails(&h, totalsize, &fmt, &size, &ntoalign); luaL_argcheck(L, opt != Kstring && opt != Kzstr, 1, "variable-length format"); size += ntoalign; /* total space used by option */ - luaL_argcheck(L, totalsize <= MAXSIZE - size, 1, - "format result too large"); + luaL_argcheck(L, totalsize <= LUA_MAXINTEGER - cast(lua_Integer, size), + 1, "format result too large"); totalsize += size; } - lua_pushinteger(L, (lua_Integer)totalsize); + lua_pushinteger(L, totalsize); return 1; } @@ -1762,9 +1769,10 @@ static int str_unpack (lua_State *L) { luaL_argcheck(L, pos <= ld, 3, "initial position out of string"); initheader(L, &h); while (*fmt != '\0') { - int size, ntoalign; + int ntoalign; + size_t size; KOption opt = getdetails(&h, pos, &fmt, &size, &ntoalign); - luaL_argcheck(L, (size_t)ntoalign + size <= ld - pos, 2, + luaL_argcheck(L, ntoalign + size <= ld - pos, 2, "data string too short"); pos += ntoalign; /* skip alignment */ /* stack space for item + next position */ @@ -1801,7 +1809,8 @@ static int str_unpack (lua_State *L) { break; } case Kstring: { - size_t len = (size_t)unpackint(L, data + pos, h.islittle, size, 0); + lua_Unsigned len = (lua_Unsigned)unpackint(L, data + pos, + h.islittle, size, 0); luaL_argcheck(L, len <= ld - pos - size, 2, "data string too short"); lua_pushlstring(L, data + pos + size, len); pos += len; /* skip string */ diff --git a/testes/tpack.lua b/testes/tpack.lua index bfa63fc40c..4b32efb59b 100644 --- a/testes/tpack.lua +++ b/testes/tpack.lua @@ -135,15 +135,15 @@ checkerror("variable%-length format", packsize, "z") -- overflow in option size (error will be in digit after limit) checkerror("invalid format", packsize, "c1" .. string.rep("0", 40)) -if packsize("i") == 4 then - -- result would be 2^31 (2^3 repetitions of 2^28 strings) - local s = string.rep("c268435456", 2^3) - checkerror("too large", packsize, s) - -- one less is OK - s = string.rep("c268435456", 2^3 - 1) .. "c268435455" - assert(packsize(s) == 0x7fffffff) +do + local maxsize = (packsize("j") <= packsize("T")) and + math.maxinteger or (1 << (packsize("T") * 8)) + assert (packsize(string.format("c%d", maxsize - 9)) == maxsize - 9) + checkerror("too large", packsize, string.format("c%dc10", maxsize - 9)) + checkerror("too long", pack, string.format("xxxxxxxxxx c%d", maxsize - 9)) end + -- overflow in packing for i = 1, sizeLI - 1 do local umax = (1 << (i * 8)) - 1 @@ -229,8 +229,9 @@ do assert(pack("c3", "123") == "123") assert(pack("c0", "") == "") assert(pack("c8", "123456") == "123456\0\0") - assert(pack("c88", "") == string.rep("\0", 88)) - assert(pack("c188", "ab") == "ab" .. string.rep("\0", 188 - 2)) + assert(pack("c88 c1", "", "X") == string.rep("\0", 88) .. "X") + assert(pack("c188 c2", "ab", "X\1") == + "ab" .. string.rep("\0", 188 - 2) .. "X\1") local a, b, c = unpack("!4 z c3", "abcdefghi\0xyz") assert(a == "abcdefghi" and b == "xyz" and c == 14) checkerror("longer than", pack, "c3", "1234") From ef28e5f789f7e7be1a3961d13cb35bbfd2542997 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 21 Jun 2024 16:26:49 -0300 Subject: [PATCH 0918/1145] Removed 'int' size limit for string.rep --- lstrlib.c | 14 ++------------ testes/strings.lua | 7 +++---- 2 files changed, 5 insertions(+), 16 deletions(-) diff --git a/lstrlib.c b/lstrlib.c index eb38b67d98..8d6573a6cd 100644 --- a/lstrlib.c +++ b/lstrlib.c @@ -37,16 +37,6 @@ #endif -/* -** Some sizes are better limited to fit in 'int', but must also fit in -** 'size_t'. (We assume that 'lua_Integer' cannot be smaller than 'int'.) -*/ -#define MAXSIZE \ - (sizeof(size_t) < sizeof(int) ? MAX_SIZET : (size_t)(INT_MAX)) - - - - static int str_len (lua_State *L) { size_t l; luaL_checklstring(L, 1, &l); @@ -149,10 +139,10 @@ static int str_rep (lua_State *L) { const char *sep = luaL_optlstring(L, 3, "", &lsep); if (n <= 0) lua_pushliteral(L, ""); - else if (l_unlikely(l + lsep < l || l + lsep > MAXSIZE / n)) + else if (l_unlikely(l + lsep < l || l + lsep > MAX_SIZE / n)) return luaL_error(L, "resulting string too large"); else { - size_t totallen = (size_t)n * l + (size_t)(n - 1) * lsep; + size_t totallen = ((size_t)n * (l + lsep)) - lsep; luaL_Buffer b; char *p = luaL_buffinitsize(L, &b, totallen); while (n-- > 1) { /* first n-1 copies (followed by separator) */ diff --git a/testes/strings.lua b/testes/strings.lua index c124b3697d..a0204309c8 100644 --- a/testes/strings.lua +++ b/testes/strings.lua @@ -109,10 +109,9 @@ assert(string.rep('teste', 0) == '') assert(string.rep('ts\00t', 2) == 'ts\0tts\000t') assert(string.rep('', 10) == '') -if string.packsize("i") == 4 then - -- result length would be 2^31 (int overflow) - checkerror("too large", string.rep, 'aa', (1 << 30)) - checkerror("too large", string.rep, 'a', (1 << 30), ',') +do + checkerror("too large", string.rep, 'aa', math.maxinteger); + checkerror("too large", string.rep, 'a', math.maxinteger/2, ',') end -- repetitions with separator From 0f7025dcae08e35a31866234d8d757ab54392190 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 21 Jun 2024 16:36:24 -0300 Subject: [PATCH 0919/1145] Details in the manual --- manual/manual.of | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/manual/manual.of b/manual/manual.of index 4fbdbf31bf..774981c43e 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -3713,8 +3713,8 @@ Lua will do all memory allocation for this state through this function @seeF{lua_Alloc}. The second argument, @id{ud}, is an opaque pointer that Lua passes to the allocator in every call. -The third argument, @id{seed}, is a seed for the hashing of -strings when they are used as table keys. +The third argument, @id{seed}, +is a seed for the hashing of strings. } @@ -7310,7 +7310,7 @@ which must be a string. The format string follows the same rules as the @ANSI{sprintf}. The accepted conversion specifiers are @id{A}, @id{a}, @id{c}, @id{d}, @id{E}, @id{e}, @id{f}, @id{G}, @id{g}, -@id{i}, @id{o}, @id{p}, @id{s}, @id{u}, @id{X}, and @id{x}, +@id{i}, @id{o}, @id{p}, @id{s}, @id{u}, @id{X}, @id{x}, and @Char{%}, plus a non-C specifier @id{q}. The accepted flags are @Char{-}, @Char{+}, @Char{#}, @Char{0}, and @Char{ } (space). @@ -8819,7 +8819,7 @@ because of its reliance on @CId{setlocale}. @LibEntry{os.time ([table])| -Returns the current time when called without arguments, +Returns the current local time when called without arguments, or a time representing the local date and time specified by the given table. This table must have fields @id{year}, @id{month}, and @id{day}, and may have fields @@ -9382,6 +9382,11 @@ Moreover, there were some changes in the parameters themselves. @itemize{ +@item{ +@Lid{lua_newstate} has a third parameter, +a seed for the hashing of strings. +} + @item{ The function @id{lua_resetthread} is deprecated; it is equivalent to @Lid{lua_closethread} with From c1dc08e8e8e22af9902a6341b4a9a9a7811954cc Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 24 Jun 2024 12:03:59 -0300 Subject: [PATCH 0920/1145] Length of external strings must fit in Lua integer (As the length of any string in Lua.) --- lapi.c | 1 + lauxlib.c | 8 +++++--- lundump.c | 2 +- manual/manual.of | 2 ++ 4 files changed, 9 insertions(+), 4 deletions(-) diff --git a/lapi.c b/lapi.c index 2b14c15ee0..f00bd53f62 100644 --- a/lapi.c +++ b/lapi.c @@ -551,6 +551,7 @@ LUA_API const char *lua_pushextlstring (lua_State *L, const char *s, size_t len, lua_Alloc falloc, void *ud) { TString *ts; lua_lock(L); + api_check(L, len <= MAX_SIZE, "string too large"); api_check(L, s[len] == '\0', "string not ending with zero"); ts = luaS_newextlstr (L, s, len, falloc, ud); setsvalue2s(L, L->top.p, ts); diff --git a/lauxlib.c b/lauxlib.c index 99a6309202..5aeec55f2c 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -538,10 +538,12 @@ static void newbox (lua_State *L) { */ static size_t newbuffsize (luaL_Buffer *B, size_t sz) { size_t newsize = (B->size / 2) * 3; /* buffer size * 1.5 */ - if (l_unlikely(MAX_SIZET - sz - 1 < B->n)) /* overflow in (B->n + sz + 1)? */ - return luaL_error(B->L, "buffer too large"); - if (newsize < B->n + sz + 1) /* not big enough? */ + if (l_unlikely(sz > MAX_SIZE - B->n - 1)) + return luaL_error(B->L, "resulting string too large"); + if (newsize < B->n + sz + 1 || newsize > MAX_SIZE) { + /* newsize was not big enough or too big */ newsize = B->n + sz + 1; + } return newsize; } diff --git a/lundump.c b/lundump.c index 51d5dc6645..b5dbaec98a 100644 --- a/lundump.c +++ b/lundump.c @@ -109,7 +109,7 @@ static size_t loadVarint (LoadState *S, size_t limit) { static size_t loadSize (LoadState *S) { - return loadVarint(S, MAX_SIZET); + return loadVarint(S, MAX_SIZE); } diff --git a/manual/manual.of b/manual/manual.of index 774981c43e..56619afe3c 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -3942,6 +3942,8 @@ holding the string content, and @id{len} is the length of the string. The string should have a zero at its end, that is, the condition @T{s[len] == '\0'} should hold. +As with any string in Lua, +the length must fit in a Lua integer. If @id{falloc} is different from @id{NULL}, that function will be called by Lua From fb7e5b76c9d41108c399cf4d16470018b717007b Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 26 Jun 2024 14:46:44 -0300 Subject: [PATCH 0921/1145] Clearer code for controlling maximum registers Plus, added a test to check that limit. --- lcode.c | 6 +----- lopcodes.h | 17 ++++++++++++----- testes/code.lua | 15 +++++++++++++++ 3 files changed, 28 insertions(+), 10 deletions(-) diff --git a/lcode.c b/lcode.c index 79c15132bc..bc0a3341b5 100644 --- a/lcode.c +++ b/lcode.c @@ -31,10 +31,6 @@ #include "lvm.h" -/* Maximum number of registers in a Lua function (must fit in 8 bits) */ -#define MAXREGS 255 - - #define hasjumps(e) ((e)->t != (e)->f) @@ -466,7 +462,7 @@ static int luaK_codek (FuncState *fs, int reg, int k) { void luaK_checkstack (FuncState *fs, int n) { int newstack = fs->freereg + n; if (newstack > fs->f->maxstacksize) { - if (newstack >= MAXREGS) + if (newstack > MAX_FSTACK) luaX_syntaxerror(fs->ls, "function or expression needs too many registers"); fs->f->maxstacksize = cast_byte(newstack); diff --git a/lopcodes.h b/lopcodes.h index 6d88804225..235c51f65c 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -23,9 +23,9 @@ iAsBx sBx (signed)(17) | A(8) | Op(7) | iAx Ax(25) | Op(7) | isJ sJ (signed)(25) | Op(7) | - A signed argument is represented in excess K: the represented value is - the written unsigned value minus K, where K is half the maximum for the - corresponding unsigned argument. + A signed argument is represented in excess K: The represented value is + the written unsigned value minus K, where K is half (rounded down) the + maximum value for the corresponding unsigned argument. ===========================================================================*/ @@ -177,9 +177,16 @@ enum OpMode {iABC, iABx, iAsBx, iAx, isJ}; /* basic instruction formats */ /* -** invalid register that fits in 8 bits +** Maximum size for the stack of a Lua function. It must fit in 8 bits. +** The highest valid register is one less than this value. */ -#define NO_REG MAXARG_A +#define MAX_FSTACK MAXARG_A + +/* +** Invalid register (one more than last valid register). +*/ +#define NO_REG MAX_FSTACK + /* diff --git a/testes/code.lua b/testes/code.lua index bd4b10d028..329619f11b 100644 --- a/testes/code.lua +++ b/testes/code.lua @@ -445,5 +445,20 @@ do -- string constants assert(T.listk(f2)[1] == nil) end + +do -- check number of available registers + -- 1 register for local + 1 for function + 252 arguments + local source = "local a; return a(" .. string.rep("a, ", 252) .. "a)" + local prog = T.listcode(assert(load(source))) + -- maximum valid register is 254 + for i = 1, 254 do + assert(string.find(prog[2 + i], "MOVE%s*" .. i)) + end + -- one more argument would need register #255 (but that is reserved) + source = "local a; return a(" .. string.rep("a, ", 253) .. "a)" + local _, msg = load(source) + assert(string.find(msg, "too many registers")) +end + print 'OK' From 9904c253da9690728710082cfb94654709ab89e7 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 27 Jun 2024 11:24:27 -0300 Subject: [PATCH 0922/1145] Flexible limit for use of registers by constructors Instead of a fixed limit of 50 registers (which, in a bad worst case, can limit the nesting of constructors to 5 levels), the compiler computes an individual limit for each constructor based on how many registers are available when it runs. This limit then controls the frequency of SETLIST instructions. --- lcode.c | 2 +- lopcodes.h | 3 --- lparser.c | 21 +++++++++++++++++++-- ltests.c | 1 - testes/code.lua | 11 +++++++++++ 5 files changed, 31 insertions(+), 7 deletions(-) diff --git a/lcode.c b/lcode.c index bc0a3341b5..a74c2a16c0 100644 --- a/lcode.c +++ b/lcode.c @@ -1804,7 +1804,7 @@ void luaK_settablesize (FuncState *fs, int pc, int ra, int asize, int hsize) { ** table (or LUA_MULTRET to add up to stack top). */ void luaK_setlist (FuncState *fs, int base, int nelems, int tostore) { - lua_assert(tostore != 0 && tostore <= LFIELDS_PER_FLUSH); + lua_assert(tostore != 0); if (tostore == LUA_MULTRET) tostore = 0; if (nelems <= MAXARG_C) diff --git a/lopcodes.h b/lopcodes.h index 235c51f65c..1d31e7c541 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -406,7 +406,4 @@ LUAI_DDEC(const lu_byte luaP_opmodes[NUM_OPCODES];) (((mm) << 7) | ((ot) << 6) | ((it) << 5) | ((t) << 4) | ((a) << 3) | (m)) -/* number of list items to accumulate before a SETLIST instruction */ -#define LFIELDS_PER_FLUSH 50 - #endif diff --git a/lparser.c b/lparser.c index cdc8cf420a..f3779864c9 100644 --- a/lparser.c +++ b/lparser.c @@ -843,13 +843,13 @@ static void yindex (LexState *ls, expdesc *v) { ** ======================================================================= */ - typedef struct ConsControl { expdesc v; /* last list item read */ expdesc *t; /* table descriptor */ int nh; /* total number of 'record' elements */ int na; /* number of array elements already stored */ int tostore; /* number of array elements pending to be stored */ + int maxtostore; /* maximum number of pending elements */ } ConsControl; @@ -878,7 +878,7 @@ static void closelistfield (FuncState *fs, ConsControl *cc) { if (cc->v.k == VVOID) return; /* there is no list item */ luaK_exp2nextreg(fs, &cc->v); cc->v.k = VVOID; - if (cc->tostore == LFIELDS_PER_FLUSH) { + if (cc->tostore >= cc->maxtostore) { luaK_setlist(fs, cc->t->u.info, cc->na, cc->tostore); /* flush */ cc->na += cc->tostore; cc->tostore = 0; /* no more items pending */ @@ -931,6 +931,22 @@ static void field (LexState *ls, ConsControl *cc) { } +/* +** Compute a limit for how many registers a constructor can use before +** emitting a 'SETLIST' instruction, based on how many registers are +** available. +*/ +static int maxtostore (FuncState *fs) { + int numfreeregs = MAX_FSTACK - fs->freereg; + if (numfreeregs >= 160) /* "lots" of registers? */ + return numfreeregs / 5u; /* use up to 1/5 of them */ + else if (numfreeregs >= 80) /* still "enough" registers? */ + return 10; /* one 'SETLIST' instruction for each 10 values */ + else /* save registers for potential more nesting */ + return 1; +} + + static void constructor (LexState *ls, expdesc *t) { /* constructor -> '{' [ field { sep field } [sep] ] '}' sep -> ',' | ';' */ @@ -945,6 +961,7 @@ static void constructor (LexState *ls, expdesc *t) { luaK_reserveregs(fs, 1); init_exp(&cc.v, VVOID, 0); /* no value (yet) */ checknext(ls, '{'); + cc.maxtostore = maxtostore(fs); do { lua_assert(cc.v.k == VVOID || cc.tostore > 0); if (ls->t.token == '}') break; diff --git a/ltests.c b/ltests.c index 1f69fe034b..2b8db37537 100644 --- a/ltests.c +++ b/ltests.c @@ -835,7 +835,6 @@ static int get_limits (lua_State *L) { setnameval(L, "MAXARG_Ax", MAXARG_Ax); setnameval(L, "MAXARG_Bx", MAXARG_Bx); setnameval(L, "OFFSET_sBx", OFFSET_sBx); - setnameval(L, "LFPF", LFIELDS_PER_FLUSH); setnameval(L, "NUM_OPCODES", NUM_OPCODES); return 1; } diff --git a/testes/code.lua b/testes/code.lua index 329619f11b..08b3e23faa 100644 --- a/testes/code.lua +++ b/testes/code.lua @@ -460,5 +460,16 @@ do -- check number of available registers assert(string.find(msg, "too many registers")) end + +do -- basic check for SETLIST + -- create a list constructor with 50 elements + local source = "local a; return {" .. string.rep("a, ", 50) .. "}" + local func = assert(load(source)) + local code = table.concat(T.listcode(func), "\n") + local _, count = string.gsub(code, "SETLIST", "") + -- code uses only 1 SETLIST for the constructor + assert(count == 1) +end + print 'OK' From 6ac7219da31df0238dc33c2d4457f69bfe0c1e79 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 27 Jun 2024 15:01:57 -0300 Subject: [PATCH 0923/1145] 'isIT'/'isOT' turned from macros to functions --- lcode.c | 4 +++- ldebug.c | 2 +- lopcodes.c | 28 ++++++++++++++++++++++++++++ lopcodes.h | 12 +++--------- ltable.c | 2 +- lvm.c | 4 ++-- 6 files changed, 38 insertions(+), 14 deletions(-) diff --git a/lcode.c b/lcode.c index a74c2a16c0..e120f0dbe9 100644 --- a/lcode.c +++ b/lcode.c @@ -1844,7 +1844,9 @@ void luaK_finish (FuncState *fs) { Proto *p = fs->f; for (i = 0; i < fs->pc; i++) { Instruction *pc = &p->code[i]; - lua_assert(i == 0 || isOT(*(pc - 1)) == isIT(*pc)); + /* avoid "not used" warnings when assert is off (for 'onelua.c') */ + (void)luaP_isOT; (void)luaP_isIT; + lua_assert(i == 0 || luaP_isOT(*(pc - 1)) == luaP_isIT(*pc)); switch (GET_OPCODE(*pc)) { case OP_RETURN0: case OP_RETURN1: { if (!(fs->needclose || (p->flag & PF_ISVARARG))) diff --git a/ldebug.c b/ldebug.c index e199decf6e..202d6417dd 100644 --- a/ldebug.c +++ b/ldebug.c @@ -939,7 +939,7 @@ int luaG_traceexec (lua_State *L, const Instruction *pc) { ci->callstatus &= ~CIST_HOOKYIELD; /* erase mark */ return 1; /* do not call hook again (VM yielded, so it did not move) */ } - if (!isIT(*(ci->u.l.savedpc - 1))) /* top not being used? */ + if (!luaP_isIT(*(ci->u.l.savedpc - 1))) /* top not being used? */ L->top.p = ci->top.p; /* correct top */ if (counthook) luaD_hook(L, LUA_HOOKCOUNT, -1, 0, 0); /* call count hook */ diff --git a/lopcodes.c b/lopcodes.c index c67aa227c5..2f9d55c5dd 100644 --- a/lopcodes.c +++ b/lopcodes.c @@ -13,6 +13,10 @@ #include "lopcodes.h" +#define opmode(mm,ot,it,t,a,m) \ + (((mm) << 7) | ((ot) << 6) | ((it) << 5) | ((t) << 4) | ((a) << 3) | (m)) + + /* ORDER OP */ LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = { @@ -102,3 +106,27 @@ LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = { ,opmode(0, 0, 0, 0, 0, iAx) /* OP_EXTRAARG */ }; + + +/* +** Check whether instruction sets top for next instruction, that is, +** it results in multiple values. +*/ +int luaP_isOT (Instruction i) { + OpCode op = GET_OPCODE(i); + switch (op) { + case OP_TAILCALL: return 1; + default: + return testOTMode(op) && GETARG_C(i) == 0; + } +} + + +/* +** Check whether instruction uses top from previous instruction, that is, +** it accepts multiple results. +*/ +int luaP_isIT (Instruction i) { + return testITMode(GET_OPCODE(i)) && GETARG_B(i) == 0; +} + diff --git a/lopcodes.h b/lopcodes.h index 1d31e7c541..63918be1ee 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -8,6 +8,7 @@ #define lopcodes_h #include "llimits.h" +#include "lobject.h" /*=========================================================================== @@ -394,16 +395,9 @@ LUAI_DDEC(const lu_byte luaP_opmodes[NUM_OPCODES];) #define testOTMode(m) (luaP_opmodes[m] & (1 << 6)) #define testMMMode(m) (luaP_opmodes[m] & (1 << 7)) -/* "out top" (set top for next instruction) */ -#define isOT(i) \ - ((testOTMode(GET_OPCODE(i)) && GETARG_C(i) == 0) || \ - GET_OPCODE(i) == OP_TAILCALL) -/* "in top" (uses top from previous instruction) */ -#define isIT(i) (testITMode(GET_OPCODE(i)) && GETARG_B(i) == 0) - -#define opmode(mm,ot,it,t,a,m) \ - (((mm) << 7) | ((ot) << 6) | ((it) << 5) | ((t) << 4) | ((a) << 3) | (m)) +LUAI_FUNC int luaP_isOT (Instruction i); +LUAI_FUNC int luaP_isIT (Instruction i); #endif diff --git a/ltable.c b/ltable.c index 40a4683f76..1be291c700 100644 --- a/ltable.c +++ b/ltable.c @@ -278,7 +278,7 @@ static int equalkey (const TValue *k1, const Node *n2, int deadok) { /* ** Returns the real size of the 'array' array */ -LUAI_FUNC unsigned int luaH_realasize (const Table *t) { +unsigned int luaH_realasize (const Table *t) { if (limitequalsasize(t)) return t->alimit; /* this is the size */ else { diff --git a/lvm.c b/lvm.c index 940a15e60d..4bc8ee818b 100644 --- a/lvm.c +++ b/lvm.c @@ -1180,8 +1180,8 @@ void luaV_execute (lua_State *L, CallInfo *ci) { #endif lua_assert(base == ci->func.p + 1); lua_assert(base <= L->top.p && L->top.p <= L->stack_last.p); - /* invalidate top for instructions not expecting it */ - lua_assert(isIT(i) || (cast_void(L->top.p = base), 1)); + /* for tests, invalidate top for instructions not expecting it */ + lua_assert(luaP_isIT(i) || (cast_void(L->top.p = base), 1)); vmdispatch (GET_OPCODE(i)) { vmcase(OP_MOVE) { StkId ra = RA(i); From c403e456b66ddacf7f8f974323e9cffdfe6365d4 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 28 Jun 2024 11:18:14 -0300 Subject: [PATCH 0924/1145] New instruction format for SETLIST/NEWTABLE New instruction format 'ivABC' (a variant of iABC where parameter vC has 10 bits) allows constructors of up to 1024 elements to be coded without EXTRAARG. --- lcode.c | 59 +++++++++++++++++++++++++++++++----------------------- lcode.h | 6 ++++-- lopcodes.c | 12 ++++++++--- lopcodes.h | 35 ++++++++++++++++++++++++++------ lparser.c | 2 +- ltests.c | 5 +++++ lvm.c | 25 ++++++++++++++--------- 7 files changed, 97 insertions(+), 47 deletions(-) diff --git a/lcode.c b/lcode.c index e120f0dbe9..c1fce37f38 100644 --- a/lcode.c +++ b/lcode.c @@ -390,32 +390,40 @@ int luaK_code (FuncState *fs, Instruction i) { ** Format and emit an 'iABC' instruction. (Assertions check consistency ** of parameters versus opcode.) */ -int luaK_codeABCk (FuncState *fs, OpCode o, int a, int b, int c, int k) { +int luaK_codeABCk (FuncState *fs, OpCode o, int A, int B, int C, int k) { lua_assert(getOpMode(o) == iABC); - lua_assert(a <= MAXARG_A && b <= MAXARG_B && - c <= MAXARG_C && (k & ~1) == 0); - return luaK_code(fs, CREATE_ABCk(o, a, b, c, k)); + lua_assert(A <= MAXARG_A && B <= MAXARG_B && + C <= MAXARG_C && (k & ~1) == 0); + return luaK_code(fs, CREATE_ABCk(o, A, B, C, k)); +} + + +int luaK_codevABCk (FuncState *fs, OpCode o, int A, int B, int C, int k) { + lua_assert(getOpMode(o) == ivABC); + lua_assert(A <= MAXARG_A && B <= MAXARG_vB && + C <= MAXARG_vC && (k & ~1) == 0); + return luaK_code(fs, CREATE_vABCk(o, A, B, C, k)); } /* ** Format and emit an 'iABx' instruction. */ -int luaK_codeABx (FuncState *fs, OpCode o, int a, unsigned int bc) { +int luaK_codeABx (FuncState *fs, OpCode o, int A, unsigned int Bc) { lua_assert(getOpMode(o) == iABx); - lua_assert(a <= MAXARG_A && bc <= MAXARG_Bx); - return luaK_code(fs, CREATE_ABx(o, a, bc)); + lua_assert(A <= MAXARG_A && Bc <= MAXARG_Bx); + return luaK_code(fs, CREATE_ABx(o, A, Bc)); } /* ** Format and emit an 'iAsBx' instruction. */ -static int codeAsBx (FuncState *fs, OpCode o, int a, int bc) { - unsigned int b = bc + OFFSET_sBx; +static int codeAsBx (FuncState *fs, OpCode o, int A, int Bc) { + unsigned int b = cast_uint(Bc) + OFFSET_sBx; lua_assert(getOpMode(o) == iAsBx); - lua_assert(a <= MAXARG_A && b <= MAXARG_Bx); - return luaK_code(fs, CREATE_ABx(o, a, b)); + lua_assert(A <= MAXARG_A && b <= MAXARG_Bx); + return luaK_code(fs, CREATE_ABx(o, A, b)); } @@ -423,7 +431,7 @@ static int codeAsBx (FuncState *fs, OpCode o, int a, int bc) { ** Format and emit an 'isJ' instruction. */ static int codesJ (FuncState *fs, OpCode o, int sj, int k) { - unsigned int j = sj + OFFSET_sJ; + unsigned int j = cast_uint(sj) + OFFSET_sJ; lua_assert(getOpMode(o) == isJ); lua_assert(j <= MAXARG_sJ && (k & ~1) == 0); return luaK_code(fs, CREATE_sJ(o, j, k)); @@ -433,9 +441,9 @@ static int codesJ (FuncState *fs, OpCode o, int sj, int k) { /* ** Emit an "extra argument" instruction (format 'iAx') */ -static int codeextraarg (FuncState *fs, int a) { - lua_assert(a <= MAXARG_Ax); - return luaK_code(fs, CREATE_Ax(OP_EXTRAARG, a)); +static int codeextraarg (FuncState *fs, int A) { + lua_assert(A <= MAXARG_Ax); + return luaK_code(fs, CREATE_Ax(OP_EXTRAARG, A)); } @@ -1032,10 +1040,10 @@ static int exp2RK (FuncState *fs, expdesc *e) { } -static void codeABRK (FuncState *fs, OpCode o, int a, int b, +static void codeABRK (FuncState *fs, OpCode o, int A, int B, expdesc *ec) { int k = exp2RK(fs, ec); - luaK_codeABCk(fs, o, a, b, ec->u.info, k); + luaK_codeABCk(fs, o, A, B, ec->u.info, k); } @@ -1788,10 +1796,10 @@ void luaK_fixline (FuncState *fs, int line) { void luaK_settablesize (FuncState *fs, int pc, int ra, int asize, int hsize) { Instruction *inst = &fs->f->code[pc]; int rb = (hsize != 0) ? luaO_ceillog2(hsize) + 1 : 0; /* hash size */ - int extra = asize / (MAXARG_C + 1); /* higher bits of array size */ - int rc = asize % (MAXARG_C + 1); /* lower bits of array size */ + int extra = asize / (MAXARG_vC + 1); /* higher bits of array size */ + int rc = asize % (MAXARG_vC + 1); /* lower bits of array size */ int k = (extra > 0); /* true iff needs extra argument */ - *inst = CREATE_ABCk(OP_NEWTABLE, ra, rb, rc, k); + *inst = CREATE_vABCk(OP_NEWTABLE, ra, rb, rc, k); *(inst + 1) = CREATE_Ax(OP_EXTRAARG, extra); } @@ -1807,12 +1815,12 @@ void luaK_setlist (FuncState *fs, int base, int nelems, int tostore) { lua_assert(tostore != 0); if (tostore == LUA_MULTRET) tostore = 0; - if (nelems <= MAXARG_C) - luaK_codeABC(fs, OP_SETLIST, base, tostore, nelems); + if (nelems <= MAXARG_vC) + luaK_codevABCk(fs, OP_SETLIST, base, tostore, nelems, 0); else { - int extra = nelems / (MAXARG_C + 1); - nelems %= (MAXARG_C + 1); - luaK_codeABCk(fs, OP_SETLIST, base, tostore, nelems, 1); + int extra = nelems / (MAXARG_vC + 1); + nelems %= (MAXARG_vC + 1); + luaK_codevABCk(fs, OP_SETLIST, base, tostore, nelems, 1); codeextraarg(fs, extra); } fs->freereg = base + 1; /* free registers with list values */ @@ -1839,6 +1847,7 @@ static int finaltarget (Instruction *code, int i) { ** Do a final pass over the code of a function, doing small peephole ** optimizations and adjustments. */ +#include "lopnames.h" void luaK_finish (FuncState *fs) { int i; Proto *p = fs->f; diff --git a/lcode.h b/lcode.h index 5b8eb29e25..c1f16da0a4 100644 --- a/lcode.h +++ b/lcode.h @@ -61,8 +61,10 @@ typedef enum UnOpr { OPR_MINUS, OPR_BNOT, OPR_NOT, OPR_LEN, OPR_NOUNOPR } UnOpr; LUAI_FUNC int luaK_code (FuncState *fs, Instruction i); LUAI_FUNC int luaK_codeABx (FuncState *fs, OpCode o, int A, unsigned Bx); -LUAI_FUNC int luaK_codeABCk (FuncState *fs, OpCode o, int A, - int B, int C, int k); +LUAI_FUNC int luaK_codeABCk (FuncState *fs, OpCode o, int A, int B, int C, + int k); +LUAI_FUNC int luaK_codevABCk (FuncState *fs, OpCode o, int A, int B, int C, + int k); LUAI_FUNC int luaK_exp2const (FuncState *fs, const expdesc *e, TValue *v); LUAI_FUNC void luaK_fixline (FuncState *fs, int line); LUAI_FUNC void luaK_nil (FuncState *fs, int from, int n); diff --git a/lopcodes.c b/lopcodes.c index 2f9d55c5dd..5533b51785 100644 --- a/lopcodes.c +++ b/lopcodes.c @@ -40,7 +40,7 @@ LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = { ,opmode(0, 0, 0, 0, 0, iABC) /* OP_SETTABLE */ ,opmode(0, 0, 0, 0, 0, iABC) /* OP_SETI */ ,opmode(0, 0, 0, 0, 0, iABC) /* OP_SETFIELD */ - ,opmode(0, 0, 0, 0, 1, iABC) /* OP_NEWTABLE */ + ,opmode(0, 0, 0, 0, 1, ivABC) /* OP_NEWTABLE */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_SELF */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_ADDI */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_ADDK */ @@ -99,7 +99,7 @@ LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = { ,opmode(0, 0, 0, 0, 0, iABx) /* OP_TFORPREP */ ,opmode(0, 0, 0, 0, 0, iABC) /* OP_TFORCALL */ ,opmode(0, 0, 0, 0, 1, iABx) /* OP_TFORLOOP */ - ,opmode(0, 0, 1, 0, 0, iABC) /* OP_SETLIST */ + ,opmode(0, 0, 1, 0, 0, ivABC) /* OP_SETLIST */ ,opmode(0, 0, 0, 0, 1, iABx) /* OP_CLOSURE */ ,opmode(0, 1, 0, 0, 1, iABC) /* OP_VARARG */ ,opmode(0, 0, 1, 0, 1, iABC) /* OP_VARARGPREP */ @@ -127,6 +127,12 @@ int luaP_isOT (Instruction i) { ** it accepts multiple results. */ int luaP_isIT (Instruction i) { - return testITMode(GET_OPCODE(i)) && GETARG_B(i) == 0; + OpCode op = GET_OPCODE(i); + switch (op) { + case OP_SETLIST: + return testITMode(GET_OPCODE(i)) && GETARG_vB(i) == 0; + default: + return testITMode(GET_OPCODE(i)) && GETARG_B(i) == 0; + } } diff --git a/lopcodes.h b/lopcodes.h index 63918be1ee..736946e388 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -19,25 +19,30 @@ 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 iABC C(8) | B(8) |k| A(8) | Op(7) | +ivABC vC(10) | vB(6) |k| A(8) | Op(7) | iABx Bx(17) | A(8) | Op(7) | iAsBx sBx (signed)(17) | A(8) | Op(7) | iAx Ax(25) | Op(7) | isJ sJ (signed)(25) | Op(7) | + ('v' stands for "variant", 's' for "signed", 'x' for "extended".) A signed argument is represented in excess K: The represented value is the written unsigned value minus K, where K is half (rounded down) the maximum value for the corresponding unsigned argument. ===========================================================================*/ -enum OpMode {iABC, iABx, iAsBx, iAx, isJ}; /* basic instruction formats */ +/* basic instruction formats */ +enum OpMode {iABC, ivABC, iABx, iAsBx, iAx, isJ}; /* ** size and position of opcode arguments. */ #define SIZE_C 8 +#define SIZE_vC 10 #define SIZE_B 8 +#define SIZE_vB 6 #define SIZE_Bx (SIZE_C + SIZE_B + 1) #define SIZE_A 8 #define SIZE_Ax (SIZE_Bx + SIZE_A) @@ -50,7 +55,9 @@ enum OpMode {iABC, iABx, iAsBx, iAx, isJ}; /* basic instruction formats */ #define POS_A (POS_OP + SIZE_OP) #define POS_k (POS_A + SIZE_A) #define POS_B (POS_k + 1) +#define POS_vB (POS_k + 1) #define POS_C (POS_B + SIZE_B) +#define POS_vC (POS_vB + SIZE_vB) #define POS_Bx POS_k @@ -95,7 +102,9 @@ enum OpMode {iABC, iABx, iAsBx, iAx, isJ}; /* basic instruction formats */ #define MAXARG_A ((1<> 1) #define int2sC(i) ((i) + OFFSET_sC) @@ -126,16 +135,24 @@ enum OpMode {iABC, iABx, iAsBx, iAx, isJ}; /* basic instruction formats */ #define GETARG_A(i) getarg(i, POS_A, SIZE_A) #define SETARG_A(i,v) setarg(i, v, POS_A, SIZE_A) -#define GETARG_B(i) check_exp(checkopm(i, iABC), getarg(i, POS_B, SIZE_B)) +#define GETARG_B(i) \ + check_exp(checkopm(i, iABC), getarg(i, POS_B, SIZE_B)) +#define GETARG_vB(i) \ + check_exp(checkopm(i, ivABC), getarg(i, POS_vB, SIZE_vB)) #define GETARG_sB(i) sC2int(GETARG_B(i)) #define SETARG_B(i,v) setarg(i, v, POS_B, SIZE_B) +#define SETARG_vB(i,v) setarg(i, v, POS_vB, SIZE_vB) -#define GETARG_C(i) check_exp(checkopm(i, iABC), getarg(i, POS_C, SIZE_C)) +#define GETARG_C(i) \ + check_exp(checkopm(i, iABC), getarg(i, POS_C, SIZE_C)) +#define GETARG_vC(i) \ + check_exp(checkopm(i, ivABC), getarg(i, POS_vC, SIZE_vC)) #define GETARG_sC(i) sC2int(GETARG_C(i)) #define SETARG_C(i,v) setarg(i, v, POS_C, SIZE_C) +#define SETARG_vC(i,v) setarg(i, v, POS_vC, SIZE_vC) -#define TESTARG_k(i) check_exp(checkopm(i, iABC), (cast_int(((i) & (1u << POS_k))))) -#define GETARG_k(i) check_exp(checkopm(i, iABC), getarg(i, POS_k, 1)) +#define TESTARG_k(i) (cast_int(((i) & (1u << POS_k)))) +#define GETARG_k(i) getarg(i, POS_k, 1) #define SETARG_k(i,v) setarg(i, v, POS_k, 1) #define GETARG_Bx(i) check_exp(checkopm(i, iABx), getarg(i, POS_Bx, SIZE_Bx)) @@ -160,6 +177,12 @@ enum OpMode {iABC, iABx, iAsBx, iAx, isJ}; /* basic instruction formats */ | (cast(Instruction, c)< ',' | ';' */ FuncState *fs = ls->fs; int line = ls->linenumber; - int pc = luaK_codeABC(fs, OP_NEWTABLE, 0, 0, 0); + int pc = luaK_codevABCk(fs, OP_NEWTABLE, 0, 0, 0, 0); ConsControl cc; luaK_code(fs, 0); /* space for extra arg. */ cc.na = cc.nh = cc.tostore = 0; diff --git a/ltests.c b/ltests.c index 2b8db37537..cd72894772 100644 --- a/ltests.c +++ b/ltests.c @@ -697,6 +697,11 @@ static char *buildop (Proto *p, int pc, char *buff) { GETARG_A(i), GETARG_B(i), GETARG_C(i), GETARG_k(i) ? " (k)" : ""); break; + case ivABC: + sprintf(buff, "%-12s%4d %4d %4d%s", name, + GETARG_A(i), GETARG_vB(i), GETARG_vC(i), + GETARG_k(i) ? " (k)" : ""); + break; case iABx: sprintf(buff, "%-12s%4d %4d", name, GETARG_A(i), GETARG_Bx(i)); break; diff --git a/lvm.c b/lvm.c index 4bc8ee818b..d8fe55e5da 100644 --- a/lvm.c +++ b/lvm.c @@ -1359,14 +1359,15 @@ void luaV_execute (lua_State *L, CallInfo *ci) { } vmcase(OP_NEWTABLE) { StkId ra = RA(i); - int b = GETARG_B(i); /* log2(hash size) + 1 */ - int c = GETARG_C(i); /* array size */ + int b = GETARG_vB(i); /* log2(hash size) + 1 */ + int c = GETARG_vC(i); /* array size */ Table *t; if (b > 0) - b = 1 << (b - 1); /* size is 2^(b - 1) */ - lua_assert((!TESTARG_k(i)) == (GETARG_Ax(*pc) == 0)); - if (TESTARG_k(i)) /* non-zero extra argument? */ - c += GETARG_Ax(*pc) * (MAXARG_C + 1); /* add it to size */ + b = 1 << (b - 1); /* hash size is 2^(b - 1) */ + if (TESTARG_k(i)) { /* non-zero extra argument? */ + lua_assert(GETARG_Ax(*pc) != 0); + c += GETARG_Ax(*pc) * (MAXARG_vC + 1); /* add it to array size */ + } pc++; /* skip extra argument */ L->top.p = ra + 1; /* correct top in case of emergency GC */ t = luaH_new(L); /* memory allocation */ @@ -1850,8 +1851,8 @@ void luaV_execute (lua_State *L, CallInfo *ci) { }} vmcase(OP_SETLIST) { StkId ra = RA(i); - int n = GETARG_B(i); - unsigned int last = GETARG_C(i); + int n = GETARG_vB(i); + unsigned int last = GETARG_vC(i); Table *h = hvalue(s2v(ra)); if (n == 0) n = cast_int(L->top.p - ra) - 1; /* get up to the top */ @@ -1859,11 +1860,15 @@ void luaV_execute (lua_State *L, CallInfo *ci) { L->top.p = ci->top.p; /* correct top in case of emergency GC */ last += n; if (TESTARG_k(i)) { - last += GETARG_Ax(*pc) * (MAXARG_C + 1); + last += GETARG_Ax(*pc) * (MAXARG_vC + 1); pc++; } - if (last > luaH_realasize(h)) /* needs more space? */ + /* when 'n' is known, table should have proper size */ + if (last > luaH_realasize(h)) { /* needs more space? */ + /* fixed-size sets should have space preallocated */ + lua_assert(GETARG_vB(i) == 0); luaH_resizearray(L, h, last); /* preallocate it at once */ + } for (; n > 0; n--) { TValue *val = s2v(ra + n); obj2arr(h, last - 1, val); From d71fbc3175d3f1f9dff89edc3f04cd20447fe091 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 1 Jul 2024 15:58:07 -0300 Subject: [PATCH 0925/1145] Updated dependencies in the make file Mainly to include 'llimits.h' in the non-kernel files --- makefile | 38 +++++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/makefile b/makefile index a56c9f62c6..74801eef3e 100644 --- a/makefile +++ b/makefile @@ -144,14 +144,16 @@ $(ALL_O): makefile ltests.h lapi.o: lapi.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \ lobject.h ltm.h lzio.h lmem.h ldebug.h ldo.h lfunc.h lgc.h lstring.h \ ltable.h lundump.h lvm.h -lauxlib.o: lauxlib.c lprefix.h lua.h luaconf.h lauxlib.h -lbaselib.o: lbaselib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h +lauxlib.o: lauxlib.c lprefix.h lua.h luaconf.h lauxlib.h llimits.h +lbaselib.o: lbaselib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h \ + llimits.h lcode.o: lcode.c lprefix.h lua.h luaconf.h lcode.h llex.h lobject.h \ llimits.h lzio.h lmem.h lopcodes.h lparser.h ldebug.h lstate.h ltm.h \ - ldo.h lgc.h lstring.h ltable.h lvm.h -lcorolib.o: lcorolib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h + ldo.h lgc.h lstring.h ltable.h lvm.h lopnames.h +lcorolib.o: lcorolib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h \ + llimits.h lctype.o: lctype.c lprefix.h lctype.h lua.h luaconf.h llimits.h -ldblib.o: ldblib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h +ldblib.o: ldblib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h llimits.h ldebug.o: ldebug.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \ lobject.h ltm.h lzio.h lmem.h lcode.h llex.h lopcodes.h lparser.h \ ldebug.h ldo.h lfunc.h lstring.h lgc.h ltable.h lvm.h @@ -165,20 +167,23 @@ lfunc.o: lfunc.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \ lgc.o: lgc.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \ llimits.h ltm.h lzio.h lmem.h ldo.h lfunc.h lgc.h llex.h lstring.h \ ltable.h -linit.o: linit.c lprefix.h lua.h luaconf.h lualib.h lauxlib.h -liolib.o: liolib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h +linit.o: linit.c lprefix.h lua.h luaconf.h lualib.h lauxlib.h llimits.h +liolib.o: liolib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h llimits.h llex.o: llex.c lprefix.h lua.h luaconf.h lctype.h llimits.h ldebug.h \ lstate.h lobject.h ltm.h lzio.h lmem.h ldo.h lgc.h llex.h lparser.h \ lstring.h ltable.h -lmathlib.o: lmathlib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h +lmathlib.o: lmathlib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h \ + llimits.h lmem.o: lmem.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \ llimits.h ltm.h lzio.h lmem.h ldo.h lgc.h -loadlib.o: loadlib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h +loadlib.o: loadlib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h \ + llimits.h lobject.o: lobject.c lprefix.h lua.h luaconf.h lctype.h llimits.h \ ldebug.h lstate.h lobject.h ltm.h lzio.h lmem.h ldo.h lstring.h lgc.h \ lvm.h -lopcodes.o: lopcodes.c lprefix.h lopcodes.h llimits.h lua.h luaconf.h -loslib.o: loslib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h +lopcodes.o: lopcodes.c lprefix.h lopcodes.h llimits.h lua.h luaconf.h \ + lobject.h +loslib.o: loslib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h llimits.h lparser.o: lparser.c lprefix.h lua.h luaconf.h lcode.h llex.h lobject.h \ llimits.h lzio.h lmem.h lopcodes.h lparser.h ldebug.h lstate.h ltm.h \ ldo.h lfunc.h lstring.h lgc.h ltable.h @@ -187,21 +192,24 @@ lstate.o: lstate.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \ lstring.h ltable.h lstring.o: lstring.c lprefix.h lua.h luaconf.h ldebug.h lstate.h \ lobject.h llimits.h ltm.h lzio.h lmem.h ldo.h lstring.h lgc.h -lstrlib.o: lstrlib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h +lstrlib.o: lstrlib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h \ + llimits.h ltable.o: ltable.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \ llimits.h ltm.h lzio.h lmem.h ldo.h lgc.h lstring.h ltable.h lvm.h -ltablib.o: ltablib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h +ltablib.o: ltablib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h \ + llimits.h ltests.o: ltests.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \ lobject.h ltm.h lzio.h lmem.h lauxlib.h lcode.h llex.h lopcodes.h \ lparser.h lctype.h ldebug.h ldo.h lfunc.h lopnames.h lstring.h lgc.h \ ltable.h lualib.h ltm.o: ltm.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \ llimits.h ltm.h lzio.h lmem.h ldo.h lgc.h lstring.h ltable.h lvm.h -lua.o: lua.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h +lua.o: lua.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h llimits.h lundump.o: lundump.c lprefix.h lua.h luaconf.h ldebug.h lstate.h \ lobject.h llimits.h ltm.h lzio.h lmem.h ldo.h lfunc.h lstring.h lgc.h \ ltable.h lundump.h -lutf8lib.o: lutf8lib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h +lutf8lib.o: lutf8lib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h \ + llimits.h lvm.o: lvm.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \ lobject.h ltm.h lzio.h lmem.h ldebug.h ldo.h lfunc.h lgc.h lopcodes.h \ lstring.h ltable.h lvm.h ljumptab.h From 781219dbe16fc327f5b828e1ff6fa45ec3265cba Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 2 Jul 2024 11:09:46 -0300 Subject: [PATCH 0926/1145] Small changes in casts from void* to functions Macro moved to llimits.h, and casts from void* to lua_CFunction first go through 'voidf' (a pointer to a function from void to void), a kind of void* for functions. --- llimits.h | 20 ++++++++++++++++++++ loadlib.c | 28 ++++++---------------------- 2 files changed, 26 insertions(+), 22 deletions(-) diff --git a/llimits.h b/llimits.h index 57e7bed7b0..0f3a8ecda4 100644 --- a/llimits.h +++ b/llimits.h @@ -152,6 +152,26 @@ typedef LUAI_UACINT l_uacInt; #endif +/* +** Special type equivalent to '(void*)' for functions (to suppress some +** warnings when converting function pointers) +*/ +typedef void (*voidf)(void); + + +/* +** Macro to convert pointer-to-void* to pointer-to-function. This cast +** is undefined according to ISO C, but POSIX assumes that it works. +** (The '__extension__' in gnu compilers is only to avoid warnings.) +*/ +#if defined(__GNUC__) +#define cast_func(p) (__extension__ (voidf)(p)) +#else +#define cast_func(p) ((voidf)(p)) +#endif + + + /* ** non-return type */ diff --git a/loadlib.c b/loadlib.c index 45db3b7216..84f56ea667 100644 --- a/loadlib.c +++ b/loadlib.c @@ -59,11 +59,8 @@ static const char *const CLIBS = "_CLIBS"; #define setprogdir(L) ((void)0) -/* -** Special type equivalent to '(void*)' for functions in gcc -** (to suppress warnings when converting function pointers) -*/ -typedef void (*voidf)(void); +/* cast void* to a Lua function */ +#define cast_Lfunc(p) cast(lua_CFunction, cast_func(p)) /* @@ -96,26 +93,13 @@ static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym); #if defined(LUA_USE_DLOPEN) /* { */ /* ** {======================================================================== -** This is an implementation of loadlib based on the dlfcn interface. -** The dlfcn interface is available in Linux, SunOS, Solaris, IRIX, FreeBSD, -** NetBSD, AIX 4.2, HPUX 11, and probably most other Unix flavors, at least -** as an emulation layer on top of native functions. +** This is an implementation of loadlib based on the dlfcn interface, +** which is available in all POSIX systems. ** ========================================================================= */ #include -/* -** Macro to convert pointer-to-void* to pointer-to-function. This cast -** is undefined according to ISO C, but POSIX assumes that it works. -** (The '__extension__' in gnu compilers is only to avoid warnings.) -*/ -#if defined(__GNUC__) -#define cast_func(p) (__extension__ (lua_CFunction)(p)) -#else -#define cast_func(p) ((lua_CFunction)(p)) -#endif - static void lsys_unloadlib (void *lib) { dlclose(lib); @@ -131,7 +115,7 @@ static void *lsys_load (lua_State *L, const char *path, int seeglb) { static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) { - lua_CFunction f = cast_func(dlsym(lib, sym)); + lua_CFunction f = cast_Lfunc(dlsym(lib, sym)); if (l_unlikely(f == NULL)) lua_pushstring(L, dlerror()); return f; @@ -207,7 +191,7 @@ static void *lsys_load (lua_State *L, const char *path, int seeglb) { static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) { - lua_CFunction f = (lua_CFunction)(voidf)GetProcAddress((HMODULE)lib, sym); + lua_CFunction f = cast_Lfunc(GetProcAddress((HMODULE)lib, sym)); if (f == NULL) pusherror(L); return f; } From 366c85564874d560b3608349f752e9e490f9002d Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 4 Jul 2024 17:11:58 -0300 Subject: [PATCH 0927/1145] lua.c loads 'readline' dynamically (See comments in luaconf.h.) This change allows easier compilation, as Lua compiles and works even if the package 'readline' is absent from the system. Moreover, non-interactive uses don't load the library, making the stand-alone slightly faster for small loads. --- lua.c | 88 +++++++++++++++++++++++++++++++++++++++---------- luaconf.h | 11 +++++++ makefile | 4 +-- testes/main.lua | 16 ++++----- 4 files changed, 90 insertions(+), 29 deletions(-) diff --git a/lua.c b/lua.c index 88fb8793fa..51979a8ba9 100644 --- a/lua.c +++ b/lua.c @@ -437,27 +437,80 @@ static int handle_luainit (lua_State *L) { ** lua_saveline defines how to "save" a read line in a "history". ** lua_freeline defines how to free a line read by lua_readline. */ -#if !defined(lua_readline) /* { */ -#if defined(LUA_USE_READLINE) /* { */ +#if defined(LUA_USE_READLINE) #include #include #define lua_initreadline(L) ((void)L, rl_readline_name="lua") -#define lua_readline(L,b,p) ((void)L, ((b)=readline(p)) != NULL) -#define lua_saveline(L,line) ((void)L, add_history(line)) -#define lua_freeline(L,b) ((void)L, free(b)) +#define lua_readline(b,p) ((void)b, readline(p)) +#define lua_saveline(line) add_history(line) +#define lua_freeline(b) free(b) + +#endif + + +#if !defined(lua_readline) /* { */ + +/* pointer to dynamically loaded 'readline' function (if any) */ +typedef char *(*l_readline_t) (const char *prompt); +static l_readline_t l_readline = NULL; + +static char *lua_readline (char *buff, const char *prompt) { + if (l_readline != NULL) /* is there a dynamic 'readline'? */ + return (*l_readline)(prompt); /* use it */ + else { /* emulate 'readline' over 'buff' */ + fputs(prompt, stdout); + fflush(stdout); /* show prompt */ + return fgets(buff, LUA_MAXINPUT, stdin); /* read line */ + } +} + + +/* pointer to dynamically loaded 'add_history' function (if any) */ +typedef void (*l_addhist_t) (const char *string); +static l_addhist_t l_addhist = NULL; + +static void lua_saveline (const char *line) { + if (l_addhist != NULL) /* is there a dynamic 'add_history'? */ + (*l_addhist)(line); /* use it */ + /* else nothing to be done */ +} -#else /* }{ */ + +static void lua_freeline (char *line) { + if (l_readline != NULL) /* is there a dynamic 'readline'? */ + free(line); /* free line created by it */ + /* else 'lua_readline' used an automatic buffer; nothing to free */ +} + + +#if !defined(LUA_USE_DLOPEN) || !defined(LUA_READLINELIB) #define lua_initreadline(L) ((void)L) -#define lua_readline(L,b,p) \ - ((void)L, fputs(p, stdout), fflush(stdout), /* show prompt */ \ - fgets(b, LUA_MAXINPUT, stdin) != NULL) /* get line */ -#define lua_saveline(L,line) { (void)L; (void)line; } -#define lua_freeline(L,b) { (void)L; (void)b; } -#endif /* } */ +#else /* { */ + +#include + + +static void lua_initreadline (lua_State *L) { + void *lib = dlopen(LUA_READLINELIB, RTLD_NOW | RTLD_LOCAL); + if (lib == NULL) + lua_warning(L, "library '" LUA_READLINELIB "'not found", 0); + else { + const char **name = cast(const char**, dlsym(lib, "rl_readline_name")); + if (name != NULL) + *name = "Lua"; + l_readline = cast(l_readline_t, cast_func(dlsym(lib, "readline"))); + if (l_readline == NULL) + lua_warning(L, "unable to load 'readline'", 0); + else + l_addhist = cast(l_addhist_t, cast_func(dlsym(lib, "add_history"))); + } +} + +#endif /* } */ #endif /* } */ @@ -505,11 +558,10 @@ static int incomplete (lua_State *L, int status) { */ static int pushline (lua_State *L, int firstline) { char buffer[LUA_MAXINPUT]; - char *b = buffer; size_t l; const char *prmt = get_prompt(L, firstline); - int readstatus = lua_readline(L, b, prmt); - if (readstatus == 0) + char *b = lua_readline(buffer, prmt); + if (b == NULL) return 0; /* no input (prompt will be popped by caller) */ lua_pop(L, 1); /* remove prompt */ l = strlen(b); @@ -519,7 +571,7 @@ static int pushline (lua_State *L, int firstline) { lua_pushfstring(L, "return %s", b + 1); /* change '=' to 'return' */ else lua_pushlstring(L, b, l); - lua_freeline(L, b); + lua_freeline(b); return 1; } @@ -535,7 +587,7 @@ static int addreturn (lua_State *L) { if (status == LUA_OK) { lua_remove(L, -2); /* remove modified line */ if (line[0] != '\0') /* non empty? */ - lua_saveline(L, line); /* keep history */ + lua_saveline(line); /* keep history */ } else lua_pop(L, 2); /* pop result from 'luaL_loadbuffer' and modified line */ @@ -552,7 +604,7 @@ static int multiline (lua_State *L) { const char *line = lua_tolstring(L, 1, &len); /* get what it has */ int status = luaL_loadbuffer(L, line, len, "=stdin"); /* try it */ if (!incomplete(L, status) || !pushline(L, 0)) { - lua_saveline(L, line); /* keep history */ + lua_saveline(line); /* keep history */ return status; /* cannot or should not try to add continuation line */ } lua_pushliteral(L, "\n"); /* add newline... */ diff --git a/luaconf.h b/luaconf.h index fe98d9a924..65715c8b75 100644 --- a/luaconf.h +++ b/luaconf.h @@ -58,15 +58,26 @@ #endif +/* +** When Posix DLL ('LUA_USE_DLOPEN') is enabled, the Lua stand-alone +** application will try to dynamically link a 'readline' facility +** for its REPL. In that case, LUA_READLINELIB is the name of the +** library it will look for those facilities. If lua.c cannot open +** the specified library, it will generate a warning and then run +** without 'readline'. If that macro is not defined, lua.c will not +** use 'readline'. +*/ #if defined(LUA_USE_LINUX) #define LUA_USE_POSIX #define LUA_USE_DLOPEN /* needs an extra library: -ldl */ +#define LUA_READLINELIB "libreadline.so" #endif #if defined(LUA_USE_MACOSX) #define LUA_USE_POSIX #define LUA_USE_DLOPEN /* MacOS does not need -ldl */ +#define LUA_READLINELIB "libedit.dylib" #endif diff --git a/makefile b/makefile index 74801eef3e..58b12f8e14 100644 --- a/makefile +++ b/makefile @@ -70,9 +70,9 @@ LOCAL = $(TESTS) $(CWARNS) # enable Linux goodies -MYCFLAGS= $(LOCAL) -std=c99 -DLUA_USE_LINUX -DLUA_USE_READLINE +MYCFLAGS= $(LOCAL) -std=c99 -DLUA_USE_LINUX MYLDFLAGS= $(LOCAL) -Wl,-E -MYLIBS= -ldl -lreadline +MYLIBS= -ldl CC= gcc diff --git a/testes/main.lua b/testes/main.lua index 5c7d0a107c..9a86fb5aff 100644 --- a/testes/main.lua +++ b/testes/main.lua @@ -368,20 +368,18 @@ assert(string.find(t, prompt .. ".*" .. prompt .. ".*" .. prompt)) -- non-string prompt -prompt = - "local C = 0;\z - _PROMPT=setmetatable({},{__tostring = function () \z - C = C + 1; return C end})" +prompt = [[ + local C = 'X'; + _PROMPT=setmetatable({},{__tostring = function () + C = C .. 'X'; return C end}) +]] prepfile[[ -- a = 2 ]] RUN([[lua -e "%s" -i < %s > %s]], prompt, prog, out) local t = getoutput() -assert(string.find(t, [[ -1 -- -2a = 2 -3 -]], 1, true)) +-- skip version line and then check the presence of the three prompts +assert(string.find(t, "^.-\nXX[^\nX]*\n?XXX[^\nX]*\n?XXXX\n?$")) -- test for error objects From 193bf7919ea97a2d1a98734e1a215ee6d3fc021b Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 5 Jul 2024 14:57:11 -0300 Subject: [PATCH 0928/1145] 'printstack' (from ltests.c) made public That function is useful for debugging the API. --- ltests.c | 4 ++-- ltests.h | 6 +++++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/ltests.c b/ltests.c index cd72894772..ad40801e24 100644 --- a/ltests.c +++ b/ltests.c @@ -822,7 +822,7 @@ static int listlocals (lua_State *L) { -static void printstack (lua_State *L) { +void lua_printstack (lua_State *L) { int i; int n = lua_gettop(L); printf("stack: >>\n"); @@ -1652,7 +1652,7 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) { printf("%s\n", luaL_tolstring(L1, n, NULL)); lua_pop(L1, 1); } - else printstack(L1); + else lua_printstack(L1); } else if EQ("print") { const char *msg = getstring; diff --git a/ltests.h b/ltests.h index da773d6eed..078c9fc344 100644 --- a/ltests.h +++ b/ltests.h @@ -64,7 +64,6 @@ LUA_API Memcontrol l_memcontrol; extern void *l_Trick; - /* ** Function to traverse and check all memory used by Lua */ @@ -76,6 +75,11 @@ LUAI_FUNC int lua_checkmemory (lua_State *L); struct GCObject; LUAI_FUNC void lua_printobj (lua_State *L, struct GCObject *o); +/* +** Function to print the stack +*/ +LUAI_FUNC void lua_printstack (lua_State *L); + /* test for lock/unlock */ From 93fd6892f85ecd8a4e82d2339016a9f71a42d0e8 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 5 Jul 2024 15:13:46 -0300 Subject: [PATCH 0929/1145] Fixed bug in 'multiline' 'incomplete' was popping error message that should be used in case there is no more lines to complete the input, that is, 'pushline' returns NULL, due to end of file. --- lua.c | 28 ++++++++++++++-------------- testes/main.lua | 5 +++++ 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/lua.c b/lua.c index 51979a8ba9..bff6a8f55a 100644 --- a/lua.c +++ b/lua.c @@ -544,10 +544,8 @@ static int incomplete (lua_State *L, int status) { if (status == LUA_ERRSYNTAX) { size_t lmsg; const char *msg = lua_tolstring(L, -1, &lmsg); - if (lmsg >= marklen && strcmp(msg + lmsg - marklen, EOFMARK) == 0) { - lua_pop(L, 1); + if (lmsg >= marklen && strcmp(msg + lmsg - marklen, EOFMARK) == 0) return 1; - } } return 0; /* else... */ } @@ -561,9 +559,9 @@ static int pushline (lua_State *L, int firstline) { size_t l; const char *prmt = get_prompt(L, firstline); char *b = lua_readline(buffer, prmt); - if (b == NULL) - return 0; /* no input (prompt will be popped by caller) */ lua_pop(L, 1); /* remove prompt */ + if (b == NULL) + return 0; /* no input */ l = strlen(b); if (l > 0 && b[l-1] == '\n') /* line ends with newline? */ b[--l] = '\0'; /* remove it */ @@ -584,11 +582,8 @@ static int addreturn (lua_State *L) { const char *line = lua_tostring(L, -1); /* original line */ const char *retline = lua_pushfstring(L, "return %s;", line); int status = luaL_loadbuffer(L, retline, strlen(retline), "=stdin"); - if (status == LUA_OK) { + if (status == LUA_OK) lua_remove(L, -2); /* remove modified line */ - if (line[0] != '\0') /* non empty? */ - lua_saveline(line); /* keep history */ - } else lua_pop(L, 2); /* pop result from 'luaL_loadbuffer' and modified line */ return status; @@ -596,17 +591,18 @@ static int addreturn (lua_State *L) { /* -** Read multiple lines until a complete Lua statement +** Read multiple lines until a complete Lua statement or an error not +** for an incomplete statement. Start with first line already read in +** the stack. */ static int multiline (lua_State *L) { for (;;) { /* repeat until gets a complete statement */ size_t len; const char *line = lua_tolstring(L, 1, &len); /* get what it has */ int status = luaL_loadbuffer(L, line, len, "=stdin"); /* try it */ - if (!incomplete(L, status) || !pushline(L, 0)) { - lua_saveline(line); /* keep history */ - return status; /* cannot or should not try to add continuation line */ - } + if (!incomplete(L, status) || !pushline(L, 0)) + return status; /* should not or cannot try to add continuation line */ + lua_remove(L, -2); /* remove error message (from incomplete line) */ lua_pushliteral(L, "\n"); /* add newline... */ lua_insert(L, -2); /* ...between the two lines */ lua_concat(L, 3); /* join them */ @@ -621,12 +617,16 @@ static int multiline (lua_State *L) { ** in the top of the stack. */ static int loadline (lua_State *L) { + const char *line; int status; lua_settop(L, 0); if (!pushline(L, 1)) return -1; /* no input */ if ((status = addreturn(L)) != LUA_OK) /* 'return ...' did not work? */ status = multiline(L); /* try as command, maybe with continuation lines */ + line = lua_tostring(L, 1); + if (line[0] != '\0') /* non empty? */ + lua_saveline(line); /* keep history */ lua_remove(L, 1); /* remove line from the stack */ lua_assert(lua_gettop(L) == 1); return status; diff --git a/testes/main.lua b/testes/main.lua index 9a86fb5aff..17fbcb61b0 100644 --- a/testes/main.lua +++ b/testes/main.lua @@ -349,6 +349,11 @@ prepfile("a = [[b\nc\nd\ne]]\n=a") RUN([[lua -e"_PROMPT='' _PROMPT2=''" -i < %s > %s]], prog, out) checkprogout("b\nc\nd\ne\n\n") +-- input interrupted in continuation line +prepfile("a.\n") +RUN([[lua -i < %s > /dev/null 2> %s]], prog, out) +checkprogout("near \n") + local prompt = "alo" prepfile[[ -- a = 2 From 6b45ccf4ed24dcfe437cf0159d6185119a2e8f95 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 5 Jul 2024 15:19:11 -0300 Subject: [PATCH 0930/1145] Removed compatibility with "= exp" in the REPL --- lua.c | 5 +---- testes/main.lua | 2 +- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/lua.c b/lua.c index bff6a8f55a..3d807c98c3 100644 --- a/lua.c +++ b/lua.c @@ -565,10 +565,7 @@ static int pushline (lua_State *L, int firstline) { l = strlen(b); if (l > 0 && b[l-1] == '\n') /* line ends with newline? */ b[--l] = '\0'; /* remove it */ - if (firstline && b[0] == '=') /* for compatibility with 5.2, ... */ - lua_pushfstring(L, "return %s", b + 1); /* change '=' to 'return' */ - else - lua_pushlstring(L, b, l); + lua_pushlstring(L, b, l); lua_freeline(b); return 1; } diff --git a/testes/main.lua b/testes/main.lua index 17fbcb61b0..7b0f4ee081 100644 --- a/testes/main.lua +++ b/testes/main.lua @@ -345,7 +345,7 @@ a]] RUN([[lua -e"_PROMPT='' _PROMPT2=''" -i < %s > %s]], prog, out) checkprogout("6\n10\n10\n\n") -prepfile("a = [[b\nc\nd\ne]]\n=a") +prepfile("a = [[b\nc\nd\ne]]\na") RUN([[lua -e"_PROMPT='' _PROMPT2=''" -i < %s > %s]], prog, out) checkprogout("b\nc\nd\ne\n\n") From cd4de92762434e6ed0e6c207d56d365300396dd8 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 16 Jul 2024 11:33:30 -0300 Subject: [PATCH 0931/1145] Maximum stack size may not fit in unsigned short Therefore, fields ftransfer/ntransfer in lua_Debug must have type 'int'. (Maximum stack size must fit in an 'int'.) Also, this commit adds check that maximum stack size respects size_t for size in bytes. --- ldo.c | 43 +++++++++++++++++++++++++++++-------------- lstate.h | 4 ++-- lua.h | 4 ++-- luaconf.h | 8 ++++---- manual/manual.of | 15 +++++++++------ 5 files changed, 46 insertions(+), 28 deletions(-) diff --git a/ldo.c b/ldo.c index cd6dded6d8..34101ba30d 100644 --- a/ldo.c +++ b/ldo.c @@ -171,6 +171,24 @@ int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) { ** =================================================================== */ +/* some stack space for error handling */ +#define STACKERRSPACE 200 + + +/* maximum stack size that respects size_t */ +#define MAXSTACK_BYSIZET ((MAX_SIZET / sizeof(StackValue)) - STACKERRSPACE) + +/* +** Minimum between LUAI_MAXSTACK and MAXSTACK_BYSIZET +** (Maximum size for the stack must respect size_t.) +*/ +#define MAXSTACK cast_int(LUAI_MAXSTACK < MAXSTACK_BYSIZET \ + ? LUAI_MAXSTACK : MAXSTACK_BYSIZET) + + +/* stack size with extra space for error handling */ +#define ERRORSTACKSIZE (MAXSTACK + STACKERRSPACE) + /* ** Change all pointers to the stack into offsets. @@ -208,9 +226,6 @@ static void correctstack (lua_State *L) { } -/* some space for error handling */ -#define ERRORSTACKSIZE (LUAI_MAXSTACK + 200) - /* ** Reallocate the stack to a new size, correcting all pointers into it. ** In ISO C, any pointer use after the pointer has been deallocated is @@ -227,7 +242,7 @@ int luaD_reallocstack (lua_State *L, int newsize, int raiseerror) { int i; StkId newstack; int oldgcstop = G(L)->gcstopem; - lua_assert(newsize <= LUAI_MAXSTACK || newsize == ERRORSTACKSIZE); + lua_assert(newsize <= MAXSTACK || newsize == ERRORSTACKSIZE); relstack(L); /* change pointers to offsets */ G(L)->gcstopem = 1; /* stop emergency collection */ newstack = luaM_reallocvector(L, L->stack.p, oldsize + EXTRA_STACK, @@ -254,7 +269,7 @@ int luaD_reallocstack (lua_State *L, int newsize, int raiseerror) { */ int luaD_growstack (lua_State *L, int n, int raiseerror) { int size = stacksize(L); - if (l_unlikely(size > LUAI_MAXSTACK)) { + if (l_unlikely(size > MAXSTACK)) { /* if stack is larger than maximum, thread is already using the extra space reserved for errors, that is, thread is handling a stack error; cannot grow further than that. */ @@ -263,14 +278,14 @@ int luaD_growstack (lua_State *L, int n, int raiseerror) { luaD_throw(L, LUA_ERRERR); /* error inside message handler */ return 0; /* if not 'raiseerror', just signal it */ } - else if (n < LUAI_MAXSTACK) { /* avoids arithmetic overflows */ + else if (n < MAXSTACK) { /* avoids arithmetic overflows */ int newsize = 2 * size; /* tentative new size */ int needed = cast_int(L->top.p - L->stack.p) + n; - if (newsize > LUAI_MAXSTACK) /* cannot cross the limit */ - newsize = LUAI_MAXSTACK; + if (newsize > MAXSTACK) /* cannot cross the limit */ + newsize = MAXSTACK; if (newsize < needed) /* but must respect what was asked for */ newsize = needed; - if (l_likely(newsize <= LUAI_MAXSTACK)) + if (l_likely(newsize <= MAXSTACK)) return luaD_reallocstack(L, newsize, raiseerror); } /* else stack overflow */ @@ -306,17 +321,17 @@ static int stackinuse (lua_State *L) { ** to twice the current use. (So, the final stack size is at most 2/3 the ** previous size, and half of its entries are empty.) ** As a particular case, if stack was handling a stack overflow and now -** it is not, 'max' (limited by LUAI_MAXSTACK) will be smaller than +** it is not, 'max' (limited by MAXSTACK) will be smaller than ** stacksize (equal to ERRORSTACKSIZE in this case), and so the stack ** will be reduced to a "regular" size. */ void luaD_shrinkstack (lua_State *L) { int inuse = stackinuse(L); - int max = (inuse > LUAI_MAXSTACK / 3) ? LUAI_MAXSTACK : inuse * 3; + int max = (inuse > MAXSTACK / 3) ? MAXSTACK : inuse * 3; /* if thread is currently not handling a stack overflow and its size is larger than maximum "reasonable" size, shrink it */ - if (inuse <= LUAI_MAXSTACK && stacksize(L) > max) { - int nsize = (inuse > LUAI_MAXSTACK / 2) ? LUAI_MAXSTACK : inuse * 2; + if (inuse <= MAXSTACK && stacksize(L) > max) { + int nsize = (inuse > MAXSTACK / 2) ? MAXSTACK : inuse * 2; luaD_reallocstack(L, nsize, 0); /* ok if that fails */ } else /* don't change stack */ @@ -408,7 +423,7 @@ static void rethook (lua_State *L, CallInfo *ci, int nres) { delta = ci->u.l.nextraargs + p->numparams + 1; } ci->func.p += delta; /* if vararg, back to virtual 'func' */ - ftransfer = cast(unsigned short, firstres - ci->func.p); + ftransfer = cast_int(firstres - ci->func.p); luaD_hook(L, LUA_HOOKRET, -1, ftransfer, nres); /* call it */ ci->func.p -= delta; } diff --git a/lstate.h b/lstate.h index 6094016d65..e5056abe00 100644 --- a/lstate.h +++ b/lstate.h @@ -207,8 +207,8 @@ struct CallInfo { int nyield; /* number of values yielded */ int nres; /* number of values returned */ struct { /* info about transferred values (for call/return hooks) */ - unsigned short ftransfer; /* offset of first value transferred */ - unsigned short ntransfer; /* number of values transferred */ + int ftransfer; /* offset of first value transferred */ + int ntransfer; /* number of values transferred */ } transferinfo; } u2; short nresults; /* expected number of results from this function */ diff --git a/lua.h b/lua.h index 2f9d0abb40..dcf4926415 100644 --- a/lua.h +++ b/lua.h @@ -503,8 +503,8 @@ struct lua_Debug { unsigned char nparams;/* (u) number of parameters */ char isvararg; /* (u) */ char istailcall; /* (t) */ - unsigned short ftransfer; /* (r) index of first value transferred */ - unsigned short ntransfer; /* (r) number of transferred values */ + int ftransfer; /* (r) index of first value transferred */ + int ntransfer; /* (r) number of transferred values */ char short_src[LUA_IDSIZE]; /* (S) */ /* private part */ struct CallInfo *i_ci; /* active function */ diff --git a/luaconf.h b/luaconf.h index 65715c8b75..80349acc39 100644 --- a/luaconf.h +++ b/luaconf.h @@ -750,13 +750,13 @@ @@ LUAI_MAXSTACK limits the size of the Lua stack. ** CHANGE it if you need a different limit. This limit is arbitrary; ** its only purpose is to stop Lua from consuming unlimited stack -** space (and to reserve some numbers for pseudo-indices). -** (It must fit into max(size_t)/32 and max(int)/2.) +** space and to reserve some numbers for pseudo-indices. +** (It must fit into max(int)/2.) */ -#if LUAI_IS32INT +#if 1000000 < (INT_MAX / 2) #define LUAI_MAXSTACK 1000000 #else -#define LUAI_MAXSTACK 15000 +#define LUAI_MAXSTACK (INT_MAX / 2u) #endif diff --git a/manual/manual.of b/manual/manual.of index 56619afe3c..1069f6444e 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -4262,8 +4262,9 @@ you push the main function plus any arguments onto the empty stack of the thread. then you call @Lid{lua_resume}, with @id{nargs} being the number of arguments. -This call returns when the coroutine suspends or finishes its execution. -When it returns, +The function returns when the coroutine suspends, +finishes its execution, or raises an unprotected error. +When it returns without errors, @id{*nresults} is updated and the top of the stack contains the @id{*nresults} values passed to @Lid{lua_yield} @@ -4274,9 +4275,11 @@ or returned by the body function. without errors, or an error code in case of errors @see{statuscodes}. In case of errors, -the error object is on the top of the stack. +the error object is pushed on the top of the stack. +(In that case, @id{nresults} is not updated, +as its value would have to be 1 for the sole error object.) -To resume a coroutine, +To resume a suspended coroutine, you remove the @id{*nresults} yielded values from its stack, push the values to be passed as results from @id{yield}, and then call @Lid{lua_resume}. @@ -4822,8 +4825,8 @@ typedef struct lua_Debug { unsigned char nparams; /* (u) number of parameters */ char isvararg; /* (u) */ char istailcall; /* (t) */ - unsigned short ftransfer; /* (r) index of first value transferred */ - unsigned short ntransfer; /* (r) number of transferred values */ + int ftransfer; /* (r) index of first value transferred */ + int ntransfer; /* (r) number of transferred values */ char short_src[LUA_IDSIZE]; /* (S) */ /* private part */ @rep{other fields} From a546138d158d79d44b2c5b42630be00d306f4e7c Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 18 Jul 2024 14:44:40 -0300 Subject: [PATCH 0932/1145] Explicit limit for number of results in a call The parameter 'nresults' in 'lua_call' and similar functions has a limit of 250. It already had an undocumented (and unchecked) limit of SHRT_MAX, but it is seldom larger than 2. --- lapi.c | 9 +++++++-- lcode.c | 2 ++ manual/manual.of | 10 ++++++++-- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/lapi.c b/lapi.c index f00bd53f62..dbd291d753 100644 --- a/lapi.c +++ b/lapi.c @@ -1022,10 +1022,15 @@ LUA_API int lua_setiuservalue (lua_State *L, int idx, int n) { */ +#define MAXRESULTS 250 + + #define checkresults(L,na,nr) \ - api_check(L, (nr) == LUA_MULTRET \ + (api_check(L, (nr) == LUA_MULTRET \ || (L->ci->top.p - L->top.p >= (nr) - (na)), \ - "results from function overflow current stack size") + "results from function overflow current stack size"), \ + api_check(L, LUA_MULTRET <= (nr) && (nr) <= MAXRESULTS, \ + "invalid number of results")) LUA_API void lua_callk (lua_State *L, int nargs, int nresults, diff --git a/lcode.c b/lcode.c index c1fce37f38..0799306ea0 100644 --- a/lcode.c +++ b/lcode.c @@ -724,6 +724,8 @@ static void const2exp (TValue *v, expdesc *e) { */ void luaK_setreturns (FuncState *fs, expdesc *e, int nresults) { Instruction *pc = &getinstruction(fs, e); + if (nresults + 1 > MAXARG_C) + luaX_syntaxerror(fs->ls, "too many multiple results"); if (e->k == VCALL) /* expression is an open function call? */ SETARG_C(*pc, nresults + 1); else { diff --git a/manual/manual.of b/manual/manual.of index 1069f6444e..c7f6904ad0 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -3028,14 +3028,20 @@ When the function returns, all arguments and the function value are popped and the call results are pushed onto the stack. The number of results is adjusted to @id{nresults}, -unless @id{nresults} is @defid{LUA_MULTRET}. -In this case, all results from the function are pushed; +unless @id{nresults} is @defid{LUA_MULTRET}, +which makes all results from the function to be pushed. +In the first case, an explicit number of results, +the caller must ensure that the stack has space for the +returned values. +In the second case, all results, Lua takes care that the returned values fit into the stack space, but it does not ensure any extra space in the stack. The function results are pushed onto the stack in direct order (the first result is pushed first), so that after the call the last result is on the top of the stack. +The maximum value for @id{nresults} is 250. + Any error while calling and running the function is propagated upwards (with a @id{longjmp}). From f407b3c4a1bc9667867ec51e835c20d97aab55a2 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 19 Jul 2024 17:34:22 -0300 Subject: [PATCH 0933/1145] Using CIST_CLSRET instead of trick with 'nresults' The callstatus flag CIST_CLSRET is used in all tests for the presence of variables to be closed in C functions. --- lapi.c | 8 +++----- lapi.h | 16 ---------------- ldo.c | 20 +++++++++++--------- testes/api.lua | 17 +++++++++++++++++ 4 files changed, 31 insertions(+), 30 deletions(-) diff --git a/lapi.c b/lapi.c index dbd291d753..70e2a44a8a 100644 --- a/lapi.c +++ b/lapi.c @@ -207,7 +207,7 @@ LUA_API void lua_settop (lua_State *L, int idx) { } newtop = L->top.p + diff; if (diff < 0 && L->tbclist.p >= newtop) { - lua_assert(hastocloseCfunc(ci->nresults)); + lua_assert(ci->callstatus & CIST_CLSRET); newtop = luaF_close(L, newtop, CLOSEKTOP, 0); } L->top.p = newtop; /* correct top only after closing any upvalue */ @@ -219,7 +219,7 @@ LUA_API void lua_closeslot (lua_State *L, int idx) { StkId level; lua_lock(L); level = index2stack(L, idx); - api_check(L, hastocloseCfunc(L->ci->nresults) && L->tbclist.p == level, + api_check(L, (L->ci->callstatus & CIST_CLSRET) && L->tbclist.p == level, "no variable to close at given level"); level = luaF_close(L, level, CLOSEKTOP, 0); setnilvalue(s2v(level)); @@ -1287,9 +1287,7 @@ LUA_API void lua_toclose (lua_State *L, int idx) { nresults = L->ci->nresults; api_check(L, L->tbclist.p < o, "given index below or equal a marked one"); luaF_newtbcupval(L, o); /* create new to-be-closed upvalue */ - if (!hastocloseCfunc(nresults)) /* function not marked yet? */ - L->ci->nresults = codeNresults(nresults); /* mark it */ - lua_assert(hastocloseCfunc(L->ci->nresults)); + L->ci->callstatus |= CIST_CLSRET; /* mark that function has TBC slots */ lua_unlock(L); } diff --git a/lapi.h b/lapi.h index 21be4a2412..9b54534428 100644 --- a/lapi.h +++ b/lapi.h @@ -62,20 +62,4 @@ L->tbclist.p < L->top.p - (n), \ "not enough free elements in the stack") - -/* -** To reduce the overhead of returning from C functions, the presence of -** to-be-closed variables in these functions is coded in the CallInfo's -** field 'nresults', in a way that functions with no to-be-closed variables -** with zero, one, or "all" wanted results have no overhead. Functions -** with other number of wanted results, as well as functions with -** variables to be closed, have an extra check. -*/ - -#define hastocloseCfunc(n) ((n) < LUA_MULTRET) - -/* Map [-1, inf) (range of 'nresults') into (-inf, -2] */ -#define codeNresults(n) (-(n) - 3) -#define decodeNresults(n) (-(n) - 3) - #endif diff --git a/ldo.c b/ldo.c index 34101ba30d..6eaa31a0c5 100644 --- a/ldo.c +++ b/ldo.c @@ -462,22 +462,23 @@ l_sinline void moveresults (lua_State *L, StkId res, int nres, int wanted) { StkId firstresult; int i; switch (wanted) { /* handle typical cases separately */ - case 0: /* no values needed */ + case 0 + 1: /* no values needed */ L->top.p = res; return; - case 1: /* one value needed */ + case 1 + 1: /* one value needed */ if (nres == 0) /* no results? */ setnilvalue(s2v(res)); /* adjust with nil */ else /* at least one result */ setobjs2s(L, res, L->top.p - nres); /* move it to proper place */ L->top.p = res + 1; return; - case LUA_MULTRET: + case LUA_MULTRET + 1: wanted = nres; /* we want all results */ break; default: /* two/more results and/or to-be-closed variables */ - if (hastocloseCfunc(wanted)) { /* to-be-closed variables? */ - L->ci->callstatus |= CIST_CLSRET; /* in case of yields */ + if (!(wanted & CIST_CLSRET)) + wanted--; + else { /* to-be-closed variables? */ L->ci->u2.nres = nres; res = luaF_close(L, res, CLOSEKTOP, 1); L->ci->callstatus &= ~CIST_CLSRET; @@ -486,7 +487,7 @@ l_sinline void moveresults (lua_State *L, StkId res, int nres, int wanted) { rethook(L, L->ci, nres); res = restorestack(L, savedres); /* hook can move stack */ } - wanted = decodeNresults(wanted); + wanted = (wanted & ~CIST_CLSRET) - 1; if (wanted == LUA_MULTRET) wanted = nres; /* we want all results */ } @@ -511,8 +512,10 @@ l_sinline void moveresults (lua_State *L, StkId res, int nres, int wanted) { ** that. */ void luaD_poscall (lua_State *L, CallInfo *ci, int nres) { - int wanted = ci->nresults; - if (l_unlikely(L->hookmask && !hastocloseCfunc(wanted))) + int wanted = ci->nresults + 1; + if (ci->callstatus & CIST_CLSRET) + wanted |= CIST_CLSRET; /* don't check hook in this case */ + else if (l_unlikely(L->hookmask)) rethook(L, ci, nres); /* move results to proper place */ moveresults(L, ci->func.p, nres, wanted); @@ -736,7 +739,6 @@ static int finishpcallk (lua_State *L, CallInfo *ci) { static void finishCcall (lua_State *L, CallInfo *ci) { int n; /* actual number of results from C function */ if (ci->callstatus & CIST_CLSRET) { /* was returning? */ - lua_assert(hastocloseCfunc(ci->nresults)); n = ci->u2.nres; /* just redo 'luaD_poscall' */ /* don't need to reset CIST_CLSRET, as it will be set again anyway */ } diff --git a/testes/api.lua b/testes/api.lua index dc4852405a..ae2f82dd33 100644 --- a/testes/api.lua +++ b/testes/api.lua @@ -165,6 +165,23 @@ do -- test returning more results than fit in the caller stack end +do -- testing multipe returns + local function foo (n) + if n > 0 then return n, foo(n - 1) end + end + + local t = {T.testC("call 1 10; return 10", foo, 20)} + assert(t[1] == 20 and t[10] == 11 and t[11] == nil) + + local t = table.pack(T.testC("call 1 10; return 10", foo, 2)) + assert(t[1] == 2 and t[2] == 1 and t[3] == nil and t.n == 10) + + local t = {T.testC([[ + checkstack 300 "error"; call 1 250; return 250]], foo, 250)} + assert(t[1] == 250 and t[250] == 1 and t[251] == nil) +end + + -- testing globals _G.AA = 14; _G.BB = "a31" local a = {T.testC[[ From 15231d4fb2f6984b25e0353ff46eda1a180b686d Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Sun, 21 Jul 2024 14:56:59 -0300 Subject: [PATCH 0934/1145] 'nresults' moved into 'callstatus' That gives us more free bits in 'callstatus', for future use. --- lapi.c | 4 +--- ldebug.c | 4 ++-- ldo.c | 72 ++++++++++++++++++++++++++++++-------------------------- lstate.c | 1 - lstate.h | 54 +++++++++++++++++++++++++++--------------- lvm.c | 6 ++--- 6 files changed, 79 insertions(+), 62 deletions(-) diff --git a/lapi.c b/lapi.c index 70e2a44a8a..a9ab1d087f 100644 --- a/lapi.c +++ b/lapi.c @@ -1103,7 +1103,7 @@ LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc, ci->u2.funcidx = cast_int(savestack(L, c.func)); ci->u.c.old_errfunc = L->errfunc; L->errfunc = func; - setoah(ci->callstatus, L->allowhook); /* save value of 'allowhook' */ + setoah(ci, L->allowhook); /* save value of 'allowhook' */ ci->callstatus |= CIST_YPCALL; /* function can do error recovery */ luaD_call(L, c.func, nresults); /* do the call */ ci->callstatus &= ~CIST_YPCALL; @@ -1280,11 +1280,9 @@ LUA_API int lua_next (lua_State *L, int idx) { LUA_API void lua_toclose (lua_State *L, int idx) { - int nresults; StkId o; lua_lock(L); o = index2stack(L, idx); - nresults = L->ci->nresults; api_check(L, L->tbclist.p < o, "given index below or equal a marked one"); luaF_newtbcupval(L, o); /* create new to-be-closed upvalue */ L->ci->callstatus |= CIST_CLSRET; /* mark that function has TBC slots */ diff --git a/ldebug.c b/ldebug.c index 202d6417dd..1c8b57c853 100644 --- a/ldebug.c +++ b/ldebug.c @@ -346,13 +346,13 @@ static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar, ar->nparams = 0; } else { - ar->isvararg = f->l.p->flag & PF_ISVARARG; + ar->isvararg = (f->l.p->flag & PF_ISVARARG) ? 1 : 0; ar->nparams = f->l.p->numparams; } break; } case 't': { - ar->istailcall = (ci) ? ci->callstatus & CIST_TAIL : 0; + ar->istailcall = (ci != NULL && (ci->callstatus & CIST_TAIL)); break; } case 'n': { diff --git a/ldo.c b/ldo.c index 6eaa31a0c5..933a55bf9e 100644 --- a/ldo.c +++ b/ldo.c @@ -452,16 +452,31 @@ static StkId tryfuncTM (lua_State *L, StkId func) { } +/* Generic case for 'moveresult */ +l_sinline void genmoveresults (lua_State *L, StkId res, int nres, + int wanted) { + StkId firstresult = L->top.p - nres; /* index of first result */ + int i; + if (nres > wanted) /* extra results? */ + nres = wanted; /* don't need them */ + for (i = 0; i < nres; i++) /* move all results to correct place */ + setobjs2s(L, res + i, firstresult + i); + for (; i < wanted; i++) /* complete wanted number of results */ + setnilvalue(s2v(res + i)); + L->top.p = res + wanted; /* top points after the last result */ +} + + /* -** Given 'nres' results at 'firstResult', move 'wanted' of them to 'res'. -** Handle most typical cases (zero results for commands, one result for -** expressions, multiple results for tail calls/single parameters) -** separated. +** Given 'nres' results at 'firstResult', move 'fwanted-1' of them +** to 'res'. Handle most typical cases (zero results for commands, +** one result for expressions, multiple results for tail calls/single +** parameters) separated. The flag CIST_CLSRET in 'fwanted', if set, +** forces the swicth to go to the default case. */ -l_sinline void moveresults (lua_State *L, StkId res, int nres, int wanted) { - StkId firstresult; - int i; - switch (wanted) { /* handle typical cases separately */ +l_sinline void moveresults (lua_State *L, StkId res, int nres, + l_uint32 fwanted) { + switch (fwanted) { /* handle typical cases separately */ case 0 + 1: /* no values needed */ L->top.p = res; return; @@ -473,12 +488,11 @@ l_sinline void moveresults (lua_State *L, StkId res, int nres, int wanted) { L->top.p = res + 1; return; case LUA_MULTRET + 1: - wanted = nres; /* we want all results */ + genmoveresults(L, res, nres, nres); /* we want all results */ break; - default: /* two/more results and/or to-be-closed variables */ - if (!(wanted & CIST_CLSRET)) - wanted--; - else { /* to-be-closed variables? */ + default: { /* two/more results and/or to-be-closed variables */ + int wanted = get_nresults(fwanted); + if (fwanted & CIST_CLSRET) { /* to-be-closed variables? */ L->ci->u2.nres = nres; res = luaF_close(L, res, CLOSEKTOP, 1); L->ci->callstatus &= ~CIST_CLSRET; @@ -487,21 +501,13 @@ l_sinline void moveresults (lua_State *L, StkId res, int nres, int wanted) { rethook(L, L->ci, nres); res = restorestack(L, savedres); /* hook can move stack */ } - wanted = (wanted & ~CIST_CLSRET) - 1; if (wanted == LUA_MULTRET) wanted = nres; /* we want all results */ } + genmoveresults(L, res, nres, wanted); break; + } } - /* generic case */ - firstresult = L->top.p - nres; /* index of first result */ - if (nres > wanted) /* extra results? */ - nres = wanted; /* don't need them */ - for (i = 0; i < nres; i++) /* move all results to correct place */ - setobjs2s(L, res + i, firstresult + i); - for (; i < wanted; i++) /* complete wanted number of results */ - setnilvalue(s2v(res + i)); - L->top.p = res + wanted; /* top points after the last result */ } @@ -512,13 +518,11 @@ l_sinline void moveresults (lua_State *L, StkId res, int nres, int wanted) { ** that. */ void luaD_poscall (lua_State *L, CallInfo *ci, int nres) { - int wanted = ci->nresults + 1; - if (ci->callstatus & CIST_CLSRET) - wanted |= CIST_CLSRET; /* don't check hook in this case */ - else if (l_unlikely(L->hookmask)) + l_uint32 fwanted = ci->callstatus & (CIST_CLSRET | CIST_NRESULTS); + if (l_unlikely(L->hookmask) && !(fwanted & CIST_CLSRET)) rethook(L, ci, nres); /* move results to proper place */ - moveresults(L, ci->func.p, nres, wanted); + moveresults(L, ci->func.p, nres, fwanted); /* function cannot be in any of these cases when returning */ lua_assert(!(ci->callstatus & (CIST_HOOKED | CIST_YPCALL | CIST_FIN | CIST_TRAN | CIST_CLSRET))); @@ -530,12 +534,12 @@ void luaD_poscall (lua_State *L, CallInfo *ci, int nres) { #define next_ci(L) (L->ci->next ? L->ci->next : luaE_extendCI(L)) -l_sinline CallInfo *prepCallInfo (lua_State *L, StkId func, int nret, - int mask, StkId top) { +l_sinline CallInfo *prepCallInfo (lua_State *L, StkId func, int nresults, + l_uint32 mask, StkId top) { CallInfo *ci = L->ci = next_ci(L); /* new frame */ ci->func.p = func; - ci->nresults = nret; - ci->callstatus = mask; + lua_assert(((nresults + 1) & ~CIST_NRESULTS) == 0); + ci->callstatus = mask | cast(l_uint32, nresults + 1); ci->top.p = top; return ci; } @@ -664,7 +668,7 @@ l_sinline void ccall (lua_State *L, StkId func, int nResults, l_uint32 inc) { luaE_checkcstack(L); } if ((ci = luaD_precall(L, func, nResults)) != NULL) { /* Lua function? */ - ci->callstatus = CIST_FRESH; /* mark that it is a "fresh" execute */ + ci->callstatus |= CIST_FRESH; /* mark that it is a "fresh" execute */ luaV_execute(L, ci); /* call it */ } L->nCcalls -= inc; @@ -709,7 +713,7 @@ static int finishpcallk (lua_State *L, CallInfo *ci) { status = LUA_YIELD; /* was interrupted by an yield */ else { /* error */ StkId func = restorestack(L, ci->u2.funcidx); - L->allowhook = getoah(ci->callstatus); /* restore 'allowhook' */ + L->allowhook = getoah(ci); /* restore 'allowhook' */ func = luaF_close(L, func, status, 1); /* can yield or raise an error */ luaD_seterrorobj(L, status, func); luaD_shrinkstack(L); /* restore stack size in case of overflow */ diff --git a/lstate.c b/lstate.c index 8df86bf5b1..4511bc005d 100644 --- a/lstate.c +++ b/lstate.c @@ -177,7 +177,6 @@ static void stack_init (lua_State *L1, lua_State *L) { ci->callstatus = CIST_C; ci->func.p = L1->top.p; ci->u.c.k = NULL; - ci->nresults = 0; setnilvalue(s2v(L1->top.p)); /* 'function' entry for this 'ci' */ L1->top.p++; ci->top.p = L1->top.p + LUA_MINSTACK; diff --git a/lstate.h b/lstate.h index e5056abe00..ff86d8253e 100644 --- a/lstate.h +++ b/lstate.h @@ -211,31 +211,45 @@ struct CallInfo { int ntransfer; /* number of values transferred */ } transferinfo; } u2; - short nresults; /* expected number of results from this function */ - unsigned short callstatus; + l_uint32 callstatus; }; /* ** Bits in CallInfo status */ -#define CIST_OAH (1<<0) /* original value of 'allowhook' */ -#define CIST_C (1<<1) /* call is running a C function */ -#define CIST_FRESH (1<<2) /* call is on a fresh "luaV_execute" frame */ -#define CIST_HOOKED (1<<3) /* call is running a debug hook */ -#define CIST_YPCALL (1<<4) /* doing a yieldable protected call */ -#define CIST_TAIL (1<<5) /* call was tail called */ -#define CIST_HOOKYIELD (1<<6) /* last hook called yielded */ -#define CIST_FIN (1<<7) /* function "called" a finalizer */ -#define CIST_TRAN (1<<8) /* 'ci' has transfer information */ -#define CIST_CLSRET (1<<9) /* function is closing tbc variables */ -/* Bits 10-12 are used for CIST_RECST (see below) */ -#define CIST_RECST 10 +/* bits 0-7 are the expected number of results from this function + 1 */ +#define CIST_NRESULTS 0xff +/* original value of 'allowhook' */ +#define CIST_OAH (cast(l_uint32, 1) << 8) +/* call is running a C function */ +#define CIST_C (cast(l_uint32, 1) << 9) +/* call is on a fresh "luaV_execute" frame */ +#define CIST_FRESH (cast(l_uint32, 1) << 10) +/* call is running a debug hook */ +#define CIST_HOOKED (cast(l_uint32, 1) << 11) +/* doing a yieldable protected call */ +#define CIST_YPCALL (cast(l_uint32, 1) << 12) +/* call was tail called */ +#define CIST_TAIL (cast(l_uint32, 1) << 13) +/* last hook called yielded */ +#define CIST_HOOKYIELD (cast(l_uint32, 1) << 14) +/* function "called" a finalizer */ +#define CIST_FIN (cast(l_uint32, 1) << 15) +/* 'ci' has transfer information */ +#define CIST_TRAN (cast(l_uint32, 1) << 16) + /* function is closing tbc variables */ +#define CIST_CLSRET (cast(l_uint32, 1) << 17) +/* Bits 18-20 are used for CIST_RECST (see below) */ +#define CIST_RECST 18 /* the offset, not the mask */ #if defined(LUA_COMPAT_LT_LE) -#define CIST_LEQ (1<<13) /* using __lt for __le */ +/* using __lt for __le */ +#define CIST_LEQ (cast(l_uint32, 1) << 21) #endif +#define get_nresults(cs) (cast_int((cs) & CIST_NRESULTS) - 1) + /* ** Field CIST_RECST stores the "recover status", used to keep the error ** status while closing to-be-closed variables in coroutines, so that @@ -246,7 +260,7 @@ struct CallInfo { #define setcistrecst(ci,st) \ check_exp(((st) & 7) == (st), /* status must fit in three bits */ \ ((ci)->callstatus = ((ci)->callstatus & ~(7 << CIST_RECST)) \ - | ((st) << CIST_RECST))) + | (cast(l_uint32, st) << CIST_RECST))) /* active function is a Lua function */ @@ -255,9 +269,11 @@ struct CallInfo { /* call is running Lua code (not a hook) */ #define isLuacode(ci) (!((ci)->callstatus & (CIST_C | CIST_HOOKED))) -/* assume that CIST_OAH has offset 0 and that 'v' is strictly 0/1 */ -#define setoah(st,v) ((st) = ((st) & ~CIST_OAH) | (v)) -#define getoah(st) ((st) & CIST_OAH) + +#define setoah(ci,v) \ + ((ci)->callstatus = ((v) ? (ci)->callstatus | CIST_OAH \ + : (ci)->callstatus & ~CIST_OAH)) +#define getoah(ci) (((ci)->callstatus & CIST_OAH) ? 1 : 0) /* diff --git a/lvm.c b/lvm.c index d8fe55e5da..5771c31a82 100644 --- a/lvm.c +++ b/lvm.c @@ -1742,10 +1742,10 @@ void luaV_execute (lua_State *L, CallInfo *ci) { trap = 1; } else { /* do the 'poscall' here */ - int nres; + int nres = get_nresults(ci->callstatus); L->ci = ci->previous; /* back to caller */ L->top.p = base - 1; - for (nres = ci->nresults; l_unlikely(nres > 0); nres--) + for (; l_unlikely(nres > 0); nres--) setnilvalue(s2v(L->top.p++)); /* all results are nil */ } goto ret; @@ -1759,7 +1759,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { trap = 1; } else { /* do the 'poscall' here */ - int nres = ci->nresults; + int nres = get_nresults(ci->callstatus); L->ci = ci->previous; /* back to caller */ if (nres == 0) L->top.p = base - 1; /* asked for no results */ From 0acd55898d0aaae8dbc14c8a1bc1e3bdffc8701b Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Sat, 27 Jul 2024 13:32:59 -0300 Subject: [PATCH 0935/1145] Added gcc option '-Wconversion' No warnings for standard numerical types. Still pending alternative numerical types. --- lapi.c | 26 +++++----- lauxlib.c | 8 +-- lbaselib.c | 15 +++--- lcode.c | 34 ++++++------ lcode.h | 2 +- ldebug.c | 4 +- ldo.c | 10 ++-- ldump.c | 18 +++---- lfunc.c | 14 ++--- lfunc.h | 8 +-- lgc.c | 14 ++--- lgc.h | 4 +- liolib.c | 8 +-- llex.c | 2 +- llimits.h | 11 ++++ lmathlib.c | 12 ++--- lmem.c | 6 +-- lmem.h | 2 +- loadlib.c | 8 +-- lobject.c | 68 ++++++++++++------------ lobject.h | 20 +++---- lopcodes.h | 7 ++- loslib.c | 2 +- lparser.c | 34 ++++++------ lparser.h | 2 +- lstate.c | 5 +- lstate.h | 2 +- lstring.c | 8 +-- lstring.h | 3 +- lstrlib.c | 132 +++++++++++++++++++++++++---------------------- ltable.c | 67 ++++++++++++------------ ltable.h | 10 ++-- ltablib.c | 4 +- ltests.c | 62 +++++++++++----------- ltm.c | 4 +- ltm.h | 6 +-- lua.c | 2 +- lundump.c | 62 ++++++++++++---------- lutf8lib.c | 4 +- lvm.c | 37 ++++++------- lvm.h | 4 +- lzio.h | 2 +- makefile | 2 +- manual/manual.of | 2 +- 44 files changed, 398 insertions(+), 359 deletions(-) diff --git a/lapi.c b/lapi.c index a9ab1d087f..1f4e9f9646 100644 --- a/lapi.c +++ b/lapi.c @@ -58,7 +58,7 @@ static void advancegc (lua_State *L, size_t delta) { delta >>= 5; /* one object for each 32 bytes (empirical) */ if (delta > 0) { global_State *g = G(L); - luaE_setdebt(g, g->GCdebt - delta); + luaE_setdebt(g, g->GCdebt - cast(l_obj, delta)); } } @@ -437,9 +437,9 @@ LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) { LUA_API lua_Unsigned lua_rawlen (lua_State *L, int idx) { const TValue *o = index2value(L, idx); switch (ttypetag(o)) { - case LUA_VSHRSTR: return tsvalue(o)->shrlen; - case LUA_VLNGSTR: return tsvalue(o)->u.lnglen; - case LUA_VUSERDATA: return uvalue(o)->len; + case LUA_VSHRSTR: return cast(lua_Unsigned, tsvalue(o)->shrlen); + case LUA_VLNGSTR: return cast(lua_Unsigned, tsvalue(o)->u.lnglen); + case LUA_VUSERDATA: return cast(lua_Unsigned, uvalue(o)->len); case LUA_VTABLE: return luaH_getn(hvalue(o)); default: return 0; } @@ -667,7 +667,7 @@ LUA_API int lua_pushthread (lua_State *L) { static int auxgetstr (lua_State *L, const TValue *t, const char *k) { - int tag; + lu_byte tag; TString *str = luaS_new(L, k); luaV_fastget(t, str, s2v(L->top.p), luaH_getstr, tag); if (!tagisempty(tag)) { @@ -685,7 +685,7 @@ static int auxgetstr (lua_State *L, const TValue *t, const char *k) { static void getGlobalTable (lua_State *L, TValue *gt) { Table *registry = hvalue(&G(L)->l_registry); - int tag = luaH_getint(registry, LUA_RIDX_GLOBALS, gt); + lu_byte tag = luaH_getint(registry, LUA_RIDX_GLOBALS, gt); (void)tag; /* avoid not-used warnings when checks are off */ api_check(L, novariant(tag) == LUA_TTABLE, "global table must exist"); } @@ -700,7 +700,7 @@ LUA_API int lua_getglobal (lua_State *L, const char *name) { LUA_API int lua_gettable (lua_State *L, int idx) { - int tag; + lu_byte tag; TValue *t; lua_lock(L); api_checkpop(L, 1); @@ -721,7 +721,7 @@ LUA_API int lua_getfield (lua_State *L, int idx, const char *k) { LUA_API int lua_geti (lua_State *L, int idx, lua_Integer n) { TValue *t; - int tag; + lu_byte tag; lua_lock(L); t = index2value(L, idx); luaV_fastgeti(t, n, s2v(L->top.p), tag); @@ -736,7 +736,7 @@ LUA_API int lua_geti (lua_State *L, int idx, lua_Integer n) { } -static int finishrawget (lua_State *L, int tag) { +static int finishrawget (lua_State *L, lu_byte tag) { if (tagisempty(tag)) /* avoid copying empty items to the stack */ setnilvalue(s2v(L->top.p)); api_incr_top(L); @@ -754,7 +754,7 @@ l_sinline Table *gettable (lua_State *L, int idx) { LUA_API int lua_rawget (lua_State *L, int idx) { Table *t; - int tag; + lu_byte tag; lua_lock(L); api_checkpop(L, 1); t = gettable(L, idx); @@ -766,7 +766,7 @@ LUA_API int lua_rawget (lua_State *L, int idx) { LUA_API int lua_rawgeti (lua_State *L, int idx, lua_Integer n) { Table *t; - int tag; + lu_byte tag; lua_lock(L); t = gettable(L, idx); luaH_fastgeti(t, n, s2v(L->top.p), tag); @@ -1231,7 +1231,7 @@ LUA_API int lua_gc (lua_State *L, int what, ...) { api_check(L, 0 <= param && param < LUA_GCPN, "invalid parameter"); res = cast_int(luaO_applyparam(g->gcparams[param], 100)); if (value >= 0) - g->gcparams[param] = luaO_codeparam(value); + g->gcparams[param] = luaO_codeparam(cast_uint(value)); break; } default: res = -1; /* invalid option */ @@ -1353,7 +1353,7 @@ LUA_API void *lua_newuserdatauv (lua_State *L, size_t size, int nuvalue) { Udata *u; lua_lock(L); api_check(L, 0 <= nuvalue && nuvalue < USHRT_MAX, "invalid value"); - u = luaS_newudata(L, size, nuvalue); + u = luaS_newudata(L, size, cast(unsigned short, nuvalue)); setuvalue(L, s2v(L->top.p), u); api_incr_top(L); advancegc(L, size); diff --git a/lauxlib.c b/lauxlib.c index 5aeec55f2c..b70b7ae63a 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -539,7 +539,7 @@ static void newbox (lua_State *L) { static size_t newbuffsize (luaL_Buffer *B, size_t sz) { size_t newsize = (B->size / 2) * 3; /* buffer size * 1.5 */ if (l_unlikely(sz > MAX_SIZE - B->n - 1)) - return luaL_error(B->L, "resulting string too large"); + return cast_sizet(luaL_error(B->L, "resulting string too large")); if (newsize < B->n + sz + 1 || newsize > MAX_SIZE) { /* newsize was not big enough or too big */ newsize = B->n + sz + 1; @@ -725,7 +725,7 @@ LUALIB_API void luaL_unref (lua_State *L, int t, int ref) { */ typedef struct LoadF { - int n; /* number of pre-read characters */ + unsigned n; /* number of pre-read characters */ FILE *f; /* file being read */ char buff[BUFSIZ]; /* area for reading file */ } LoadF; @@ -825,7 +825,7 @@ LUALIB_API int luaL_loadfilex (lua_State *L, const char *filename, } } if (c != EOF) - lf.buff[lf.n++] = c; /* 'c' is the first character of the stream */ + lf.buff[lf.n++] = cast_char(c); /* 'c' is the first character */ status = lua_load(L, getF, &lf, lua_tostring(L, -1), mode); readstatus = ferror(lf.f); errno = 0; /* no useful error number until here */ @@ -1020,7 +1020,7 @@ LUALIB_API void luaL_addgsub (luaL_Buffer *b, const char *s, const char *wild; size_t l = strlen(p); while ((wild = strstr(s, p)) != NULL) { - luaL_addlstring(b, s, wild - s); /* push prefix */ + luaL_addlstring(b, s, ct_diff2sz(wild - s)); /* push prefix */ luaL_addstring(b, r); /* push replacement in place of pattern */ s = wild + l; /* continue after 'p' */ } diff --git a/lbaselib.c b/lbaselib.c index 8b03434021..a7b6c3ed7f 100644 --- a/lbaselib.c +++ b/lbaselib.c @@ -58,21 +58,22 @@ static int luaB_warn (lua_State *L) { #define SPACECHARS " \f\n\r\t\v" -static const char *b_str2int (const char *s, int base, lua_Integer *pn) { +static const char *b_str2int (const char *s, unsigned base, lua_Integer *pn) { lua_Unsigned n = 0; int neg = 0; s += strspn(s, SPACECHARS); /* skip initial spaces */ if (*s == '-') { s++; neg = 1; } /* handle sign */ else if (*s == '+') s++; - if (!isalnum((unsigned char)*s)) /* no digit? */ + if (!isalnum(cast_uchar(*s))) /* no digit? */ return NULL; do { - int digit = (isdigit((unsigned char)*s)) ? *s - '0' - : (toupper((unsigned char)*s) - 'A') + 10; + unsigned digit = cast_uint(isdigit(cast_uchar(*s)) + ? *s - '0' + : (toupper(cast_uchar(*s)) - 'A') + 10); if (digit >= base) return NULL; /* invalid numeral */ n = n * base + digit; s++; - } while (isalnum((unsigned char)*s)); + } while (isalnum(cast_uchar(*s))); s += strspn(s, SPACECHARS); /* skip trailing spaces */ *pn = (lua_Integer)((neg) ? (0u - n) : n); return s; @@ -102,7 +103,7 @@ static int luaB_tonumber (lua_State *L) { luaL_checktype(L, 1, LUA_TSTRING); /* no numbers as strings */ s = lua_tolstring(L, 1, &l); luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range"); - if (b_str2int(s, (int)base, &n) == s + l) { + if (b_str2int(s, cast_uint(base), &n) == s + l) { lua_pushinteger(L, n); return 1; } /* else not a number */ @@ -159,7 +160,7 @@ static int luaB_rawlen (lua_State *L) { int t = lua_type(L, 1); luaL_argexpected(L, t == LUA_TTABLE || t == LUA_TSTRING, 1, "table or string"); - lua_pushinteger(L, lua_rawlen(L, 1)); + lua_pushinteger(L, l_castU2S(lua_rawlen(L, 1))); return 1; } diff --git a/lcode.c b/lcode.c index 0799306ea0..47e5424e93 100644 --- a/lcode.c +++ b/lcode.c @@ -335,7 +335,7 @@ static void savelineinfo (FuncState *fs, Proto *f, int line) { } luaM_growvector(fs->ls->L, f->lineinfo, pc, f->sizelineinfo, ls_byte, INT_MAX, "opcodes"); - f->lineinfo[pc] = linedif; + f->lineinfo[pc] = cast(ls_byte, linedif); fs->previousline = line; /* last line saved */ } @@ -409,7 +409,7 @@ int luaK_codevABCk (FuncState *fs, OpCode o, int A, int B, int C, int k) { /* ** Format and emit an 'iABx' instruction. */ -int luaK_codeABx (FuncState *fs, OpCode o, int A, unsigned int Bc) { +int luaK_codeABx (FuncState *fs, OpCode o, int A, int Bc) { lua_assert(getOpMode(o) == iABx); lua_assert(A <= MAXARG_A && Bc <= MAXARG_Bx); return luaK_code(fs, CREATE_ABx(o, A, Bc)); @@ -420,7 +420,7 @@ int luaK_codeABx (FuncState *fs, OpCode o, int A, unsigned int Bc) { ** Format and emit an 'iAsBx' instruction. */ static int codeAsBx (FuncState *fs, OpCode o, int A, int Bc) { - unsigned int b = cast_uint(Bc) + OFFSET_sBx; + int b = Bc + OFFSET_sBx; lua_assert(getOpMode(o) == iAsBx); lua_assert(A <= MAXARG_A && b <= MAXARG_Bx); return luaK_code(fs, CREATE_ABx(o, A, b)); @@ -431,7 +431,7 @@ static int codeAsBx (FuncState *fs, OpCode o, int A, int Bc) { ** Format and emit an 'isJ' instruction. */ static int codesJ (FuncState *fs, OpCode o, int sj, int k) { - unsigned int j = cast_uint(sj) + OFFSET_sJ; + int j = sj + OFFSET_sJ; lua_assert(getOpMode(o) == isJ); lua_assert(j <= MAXARG_sJ && (k & ~1) == 0); return luaK_code(fs, CREATE_sJ(o, j, k)); @@ -483,7 +483,7 @@ void luaK_checkstack (FuncState *fs, int n) { */ void luaK_reserveregs (FuncState *fs, int n) { luaK_checkstack(fs, n); - fs->freereg += n; + fs->freereg = cast_byte(fs->freereg + n); } @@ -1290,25 +1290,25 @@ void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) { if (t->k == VUPVAL && !isKstr(fs, k)) /* upvalue indexed by non 'Kstr'? */ luaK_exp2anyreg(fs, t); /* put it in a register */ if (t->k == VUPVAL) { - int temp = t->u.info; /* upvalue index */ + lu_byte temp = cast_byte(t->u.info); /* upvalue index */ lua_assert(isKstr(fs, k)); t->u.ind.t = temp; /* (can't do a direct assignment; values overlap) */ - t->u.ind.idx = k->u.info; /* literal short string */ + t->u.ind.idx = cast(short, k->u.info); /* literal short string */ t->k = VINDEXUP; } else { /* register index of the table */ - t->u.ind.t = (t->k == VLOCAL) ? t->u.var.ridx: t->u.info; + t->u.ind.t = cast_byte((t->k == VLOCAL) ? t->u.var.ridx: t->u.info); if (isKstr(fs, k)) { - t->u.ind.idx = k->u.info; /* literal short string */ + t->u.ind.idx = cast(short, k->u.info); /* literal short string */ t->k = VINDEXSTR; } - else if (isCint(k)) { - t->u.ind.idx = cast_int(k->u.ival); /* int. constant in proper range */ + else if (isCint(k)) { /* int. constant in proper range? */ + t->u.ind.idx = cast(short, k->u.ival); t->k = VINDEXI; } else { - t->u.ind.idx = luaK_exp2anyreg(fs, k); /* register */ + t->u.ind.idx = cast(short, luaK_exp2anyreg(fs, k)); /* register */ t->k = VINDEXED; } } @@ -1623,7 +1623,7 @@ void luaK_prefix (FuncState *fs, UnOpr opr, expdesc *e, int line) { luaK_dischargevars(fs, e); switch (opr) { case OPR_MINUS: case OPR_BNOT: /* use 'ef' as fake 2nd operand */ - if (constfolding(fs, opr + LUA_OPUNM, e, &ef)) + if (constfolding(fs, cast_int(opr + LUA_OPUNM), e, &ef)) break; /* else */ /* FALLTHROUGH */ case OPR_LEN: @@ -1711,7 +1711,7 @@ static void codeconcat (FuncState *fs, expdesc *e1, expdesc *e2, int line) { void luaK_posfix (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2, int line) { luaK_dischargevars(fs, e2); - if (foldbinop(opr) && constfolding(fs, opr + LUA_OPADD, e1, e2)) + if (foldbinop(opr) && constfolding(fs, cast_int(opr + LUA_OPADD), e1, e2)) return; /* done by folding */ switch (opr) { case OPR_AND: { @@ -1797,11 +1797,11 @@ void luaK_fixline (FuncState *fs, int line) { void luaK_settablesize (FuncState *fs, int pc, int ra, int asize, int hsize) { Instruction *inst = &fs->f->code[pc]; - int rb = (hsize != 0) ? luaO_ceillog2(hsize) + 1 : 0; /* hash size */ int extra = asize / (MAXARG_vC + 1); /* higher bits of array size */ int rc = asize % (MAXARG_vC + 1); /* lower bits of array size */ int k = (extra > 0); /* true iff needs extra argument */ - *inst = CREATE_vABCk(OP_NEWTABLE, ra, rb, rc, k); + hsize = (hsize != 0) ? luaO_ceillog2(cast_uint(hsize)) + 1 : 0; + *inst = CREATE_vABCk(OP_NEWTABLE, ra, hsize, rc, k); *(inst + 1) = CREATE_Ax(OP_EXTRAARG, extra); } @@ -1825,7 +1825,7 @@ void luaK_setlist (FuncState *fs, int base, int nelems, int tostore) { luaK_codevABCk(fs, OP_SETLIST, base, tostore, nelems, 1); codeextraarg(fs, extra); } - fs->freereg = base + 1; /* free registers with list values */ + fs->freereg = cast_byte(base + 1); /* free registers with list values */ } diff --git a/lcode.h b/lcode.h index c1f16da0a4..414ebe3999 100644 --- a/lcode.h +++ b/lcode.h @@ -60,7 +60,7 @@ typedef enum UnOpr { OPR_MINUS, OPR_BNOT, OPR_NOT, OPR_LEN, OPR_NOUNOPR } UnOpr; #define luaK_jumpto(fs,t) luaK_patchlist(fs, luaK_jump(fs), t) LUAI_FUNC int luaK_code (FuncState *fs, Instruction i); -LUAI_FUNC int luaK_codeABx (FuncState *fs, OpCode o, int A, unsigned Bx); +LUAI_FUNC int luaK_codeABx (FuncState *fs, OpCode o, int A, int Bx); LUAI_FUNC int luaK_codeABCk (FuncState *fs, OpCode o, int A, int B, int C, int k); LUAI_FUNC int luaK_codevABCk (FuncState *fs, OpCode o, int A, int B, int C, diff --git a/ldebug.c b/ldebug.c index 1c8b57c853..a3a536bba3 100644 --- a/ldebug.c +++ b/ldebug.c @@ -63,7 +63,7 @@ static int getbaseline (const Proto *f, int pc, int *basepc) { return f->linedefined; } else { - int i = cast_uint(pc) / MAXIWTHABS - 1; /* get an estimate */ + int i = pc / MAXIWTHABS - 1; /* get an estimate */ /* estimate must be a lower bound of the correct base */ lua_assert(i < 0 || (i < f->sizeabslineinfo && f->abslineinfo[i].pc <= pc)); @@ -921,7 +921,7 @@ int luaG_tracecall (lua_State *L) { */ int luaG_traceexec (lua_State *L, const Instruction *pc) { CallInfo *ci = L->ci; - lu_byte mask = L->hookmask; + lu_byte mask = cast_byte(L->hookmask); const Proto *p = ci_func(ci)->p; int counthook; if (!(mask & (LUA_MASKLINE | LUA_MASKCOUNT))) { /* no hooks? */ diff --git a/ldo.c b/ldo.c index 933a55bf9e..1d1b7a7150 100644 --- a/ldo.c +++ b/ldo.c @@ -241,7 +241,7 @@ int luaD_reallocstack (lua_State *L, int newsize, int raiseerror) { int oldsize = stacksize(L); int i; StkId newstack; - int oldgcstop = G(L)->gcstopem; + lu_byte oldgcstop = G(L)->gcstopem; lua_assert(newsize <= MAXSTACK || newsize == ERRORSTACKSIZE); relstack(L); /* change pointers to offsets */ G(L)->gcstopem = 1; /* stop emergency collection */ @@ -357,7 +357,7 @@ void luaD_hook (lua_State *L, int event, int line, int ftransfer, int ntransfer) { lua_Hook hook = L->hook; if (hook && L->allowhook) { /* make sure there is a hook */ - int mask = CIST_HOOKED; + unsigned mask = CIST_HOOKED; CallInfo *ci = L->ci; ptrdiff_t top = savestack(L, L->top.p); /* preserve original 'top' */ ptrdiff_t ci_top = savestack(L, ci->top.p); /* idem for 'ci->top' */ @@ -1058,9 +1058,9 @@ int luaD_protectedparser (lua_State *L, ZIO *z, const char *name, luaZ_initbuffer(L, &p.buff); status = luaD_pcall(L, f_parser, &p, savestack(L, L->top.p), L->errfunc); luaZ_freebuffer(L, &p.buff); - luaM_freearray(L, p.dyd.actvar.arr, p.dyd.actvar.size); - luaM_freearray(L, p.dyd.gt.arr, p.dyd.gt.size); - luaM_freearray(L, p.dyd.label.arr, p.dyd.label.size); + luaM_freearray(L, p.dyd.actvar.arr, cast_sizet(p.dyd.actvar.size)); + luaM_freearray(L, p.dyd.gt.arr, cast_sizet(p.dyd.gt.size)); + luaM_freearray(L, p.dyd.label.arr, cast_sizet(p.dyd.label.size)); decnny(L); return status; } diff --git a/ldump.c b/ldump.c index a1e098567e..71d9a5b1c9 100644 --- a/ldump.c +++ b/ldump.c @@ -27,7 +27,7 @@ typedef struct { lua_State *L; lua_Writer writer; void *data; - lu_mem offset; /* current position relative to beginning of dump */ + size_t offset; /* current position relative to beginning of dump */ int strip; int status; Table *h; /* table to track saved strings */ @@ -63,11 +63,11 @@ static void dumpBlock (DumpState *D, const void *b, size_t size) { ** Dump enough zeros to ensure that current position is a multiple of ** 'align'. */ -static void dumpAlign (DumpState *D, int align) { - int padding = align - (D->offset % align); +static void dumpAlign (DumpState *D, unsigned align) { + unsigned padding = align - cast_uint(D->offset % align); if (padding < align) { /* padding == align means no padding */ static lua_Integer paddingContent = 0; - lua_assert(cast_uint(align) <= sizeof(lua_Integer)); + lua_assert(align <= sizeof(lua_Integer)); dumpBlock(D, &paddingContent, padding); } lua_assert(D->offset % align == 0); @@ -94,10 +94,10 @@ static void dumpByte (DumpState *D, int y) { */ static void dumpVarint (DumpState *D, size_t x) { lu_byte buff[DIBS]; - int n = 1; + unsigned n = 1; buff[DIBS - 1] = x & 0x7f; /* fill least-significant byte */ while ((x >>= 7) != 0) /* fill other bytes in reverse order */ - buff[DIBS - (++n)] = (x & 0x7f) | 0x80; + buff[DIBS - (++n)] = cast_byte((x & 0x7f) | 0x80); dumpVector(D, buff + DIBS - n, n); } @@ -159,7 +159,7 @@ static void dumpCode (DumpState *D, const Proto *f) { dumpInt(D, f->sizecode); dumpAlign(D, sizeof(f->code[0])); lua_assert(f->code != NULL); - dumpVector(D, f->code, f->sizecode); + dumpVector(D, f->code, cast_uint(f->sizecode)); } @@ -216,13 +216,13 @@ static void dumpDebug (DumpState *D, const Proto *f) { n = (D->strip) ? 0 : f->sizelineinfo; dumpInt(D, n); if (f->lineinfo != NULL) - dumpVector(D, f->lineinfo, n); + dumpVector(D, f->lineinfo, cast_uint(n)); n = (D->strip) ? 0 : f->sizeabslineinfo; dumpInt(D, n); if (n > 0) { /* 'abslineinfo' is an array of structures of int's */ dumpAlign(D, sizeof(int)); - dumpVector(D, f->abslineinfo, n); + dumpVector(D, f->abslineinfo, cast_uint(n)); } n = (D->strip) ? 0 : f->sizelocvars; dumpInt(D, n); diff --git a/lfunc.c b/lfunc.c index d63d05fcda..d650c00079 100644 --- a/lfunc.c +++ b/lfunc.c @@ -266,14 +266,14 @@ Proto *luaF_newproto (lua_State *L) { void luaF_freeproto (lua_State *L, Proto *f) { if (!(f->flag & PF_FIXED)) { - luaM_freearray(L, f->code, f->sizecode); - luaM_freearray(L, f->lineinfo, f->sizelineinfo); - luaM_freearray(L, f->abslineinfo, f->sizeabslineinfo); + luaM_freearray(L, f->code, cast_sizet(f->sizecode)); + luaM_freearray(L, f->lineinfo, cast_sizet(f->sizelineinfo)); + luaM_freearray(L, f->abslineinfo, cast_sizet(f->sizeabslineinfo)); } - luaM_freearray(L, f->p, f->sizep); - luaM_freearray(L, f->k, f->sizek); - luaM_freearray(L, f->locvars, f->sizelocvars); - luaM_freearray(L, f->upvalues, f->sizeupvalues); + luaM_freearray(L, f->p, cast_sizet(f->sizep)); + luaM_freearray(L, f->k, cast_sizet(f->sizek)); + luaM_freearray(L, f->locvars, cast_sizet(f->sizelocvars)); + luaM_freearray(L, f->upvalues, cast_sizet(f->sizeupvalues)); luaM_free(L, f); } diff --git a/lfunc.h b/lfunc.h index 3be265efb5..162b55ecdb 100644 --- a/lfunc.h +++ b/lfunc.h @@ -11,11 +11,11 @@ #include "lobject.h" -#define sizeCclosure(n) (cast_int(offsetof(CClosure, upvalue)) + \ - cast_int(sizeof(TValue)) * (n)) +#define sizeCclosure(n) \ + (offsetof(CClosure, upvalue) + sizeof(TValue) * cast_uint(n)) -#define sizeLclosure(n) (cast_int(offsetof(LClosure, upvals)) + \ - cast_int(sizeof(TValue *)) * (n)) +#define sizeLclosure(n) \ + (offsetof(LClosure, upvals) + sizeof(TValue *) * cast_uint(n)) /* test whether thread is in 'twups' list */ diff --git a/lgc.c b/lgc.c index 0ad3a16f7b..93d2249b1b 100644 --- a/lgc.c +++ b/lgc.c @@ -246,7 +246,7 @@ void luaC_fix (lua_State *L, GCObject *o) { ** create a new collectable object (with given type, size, and offset) ** and link it to 'allgc' list. */ -GCObject *luaC_newobjdt (lua_State *L, int tt, size_t sz, size_t offset) { +GCObject *luaC_newobjdt (lua_State *L, lu_byte tt, size_t sz, size_t offset) { global_State *g = G(L); char *p = cast_charp(luaM_newobject(L, novariant(tt), sz)); GCObject *o = cast(GCObject *, p + offset); @@ -262,7 +262,7 @@ GCObject *luaC_newobjdt (lua_State *L, int tt, size_t sz, size_t offset) { /* ** create a new collectable object with no offset. */ -GCObject *luaC_newobj (lua_State *L, int tt, size_t sz) { +GCObject *luaC_newobj (lua_State *L, lu_byte tt, size_t sz) { return luaC_newobjdt(L, tt, sz, 0); } @@ -813,7 +813,7 @@ static void freeobj (lua_State *L, GCObject *o) { case LUA_VSHRSTR: { TString *ts = gco2ts(o); luaS_remove(L, ts); /* remove it from hash table */ - luaM_freemem(L, ts, sizestrshr(ts->shrlen)); + luaM_freemem(L, ts, sizestrshr(cast_uint(ts->shrlen))); break; } case LUA_VLNGSTR: { @@ -922,7 +922,7 @@ static void GCTM (lua_State *L) { if (!notm(tm)) { /* is there a finalizer? */ int status; lu_byte oldah = L->allowhook; - int oldgcstp = g->gcstp; + lu_byte oldgcstp = g->gcstp; g->gcstp |= GCSTPGC; /* avoid GC steps */ L->allowhook = 0; /* stop debug hooks during GC metamethod */ setobj2s(L, L->top.p++, tm); /* push finalizer... */ @@ -1235,7 +1235,7 @@ static void finishgencycle (lua_State *L, global_State *g) { ** the "sweep all" state to clear all objects, which are mostly black ** in generational mode. */ -static void minor2inc (lua_State *L, global_State *g, int kind) { +static void minor2inc (lua_State *L, global_State *g, lu_byte kind) { g->GCmajorminor = g->marked; /* number of live objects */ g->gckind = kind; g->reallyold = g->old1 = g->survival = NULL; @@ -1522,7 +1522,7 @@ static l_obj atomic (lua_State *L) { ** elements. The fast case sweeps the whole list. */ static void sweepstep (lua_State *L, global_State *g, - int nextstate, GCObject **nextlist, int fast) { + lu_byte nextstate, GCObject **nextlist, int fast) { if (g->sweepgc) g->sweepgc = sweeplist(L, g->sweepgc, fast ? MAX_LOBJ : GCSWEEPMAX); else { /* enter next state */ @@ -1706,7 +1706,7 @@ static void fullinc (lua_State *L, global_State *g) { void luaC_fullgc (lua_State *L, int isemergency) { global_State *g = G(L); lua_assert(!g->gcemergency); - g->gcemergency = isemergency; /* set flag */ + g->gcemergency = cast_byte(isemergency); /* set flag */ switch (g->gckind) { case KGC_GENMINOR: fullgen(L, g); break; case KGC_INC: fullinc(L, g); break; diff --git a/lgc.h b/lgc.h index 5b71ddb92c..a30755d05a 100644 --- a/lgc.h +++ b/lgc.h @@ -245,8 +245,8 @@ LUAI_FUNC void luaC_freeallobjects (lua_State *L); LUAI_FUNC void luaC_step (lua_State *L); LUAI_FUNC void luaC_runtilstate (lua_State *L, int state, int fast); LUAI_FUNC void luaC_fullgc (lua_State *L, int isemergency); -LUAI_FUNC GCObject *luaC_newobj (lua_State *L, int tt, size_t sz); -LUAI_FUNC GCObject *luaC_newobjdt (lua_State *L, int tt, size_t sz, +LUAI_FUNC GCObject *luaC_newobj (lua_State *L, lu_byte tt, size_t sz); +LUAI_FUNC GCObject *luaC_newobjdt (lua_State *L, lu_byte tt, size_t sz, size_t offset); LUAI_FUNC void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v); LUAI_FUNC void luaC_barrierback_ (lua_State *L, GCObject *o); diff --git a/liolib.c b/liolib.c index 4349f860bb..17522bb207 100644 --- a/liolib.c +++ b/liolib.c @@ -443,7 +443,7 @@ static int nextc (RN *rn) { return 0; /* fail */ } else { - rn->buff[rn->n++] = rn->c; /* save current char */ + rn->buff[rn->n++] = cast_char(rn->c); /* save current char */ rn->c = l_getc(rn->f); /* read next one */ return 1; } @@ -524,15 +524,15 @@ static int read_line (lua_State *L, FILE *f, int chop) { luaL_buffinit(L, &b); do { /* may need to read several chunks to get whole line */ char *buff = luaL_prepbuffer(&b); /* preallocate buffer space */ - int i = 0; + unsigned i = 0; l_lockfile(f); /* no memory errors can happen inside the lock */ while (i < LUAL_BUFFERSIZE && (c = l_getc(f)) != EOF && c != '\n') - buff[i++] = c; /* read up to end of line or buffer limit */ + buff[i++] = cast_char(c); /* read up to end of line or buffer limit */ l_unlockfile(f); luaL_addsize(&b, i); } while (c != EOF && c != '\n'); /* repeat until end of line */ if (!chop && c == '\n') /* want a newline and have one? */ - luaL_addchar(&b, c); /* add ending newline to result */ + luaL_addchar(&b, '\n'); /* add ending newline to result */ luaL_pushresult(&b); /* close buffer */ /* return ok if read something (either a newline or something else) */ return (c == '\n' || lua_rawlen(L, -1) > 0); diff --git a/llex.c b/llex.c index 3446f4e07a..b2e77c9c87 100644 --- a/llex.c +++ b/llex.c @@ -350,7 +350,7 @@ static unsigned long readutf8esc (LexState *ls) { int i = 4; /* chars to be removed: '\', 'u', '{', and first digit */ save_and_next(ls); /* skip 'u' */ esccheck(ls, ls->current == '{', "missing '{'"); - r = gethexa(ls); /* must have at least one digit */ + r = cast_ulong(gethexa(ls)); /* must have at least one digit */ while (cast_void(save_and_next(ls)), lisxdigit(ls->current)) { i++; esccheck(ls, r <= (0x7FFFFFFFu >> 4), "UTF-8 value too large"); diff --git a/llimits.h b/llimits.h index 0f3a8ecda4..e7da009b16 100644 --- a/llimits.h +++ b/llimits.h @@ -130,6 +130,7 @@ typedef LUAI_UACINT l_uacInt; #define cast_num(i) cast(lua_Number, (i)) #define cast_int(i) cast(int, (i)) #define cast_uint(i) cast(unsigned int, (i)) +#define cast_ulong(i) cast(unsigned long, (i)) #define cast_byte(i) cast(lu_byte, (i)) #define cast_uchar(i) cast(unsigned char, (i)) #define cast_char(i) cast(char, (i)) @@ -151,6 +152,16 @@ typedef LUAI_UACINT l_uacInt; #define l_castU2S(i) ((lua_Integer)(i)) #endif +/* +** cast a size_t to lua_Integer: These casts are always valid for +** sizes of Lua objects (see MAX_SIZE) +*/ +#define cast_st2S(sz) ((lua_Integer)(sz)) + +/* Cast a ptrdiff_t to size_t, when it is known that the minuend +** comes from the subtraend (the base) +*/ +#define ct_diff2sz(df) ((size_t)(df)) /* ** Special type equivalent to '(void*)' for functions (to suppress some diff --git a/lmathlib.c b/lmathlib.c index 2bdcb63758..f8b24d1d01 100644 --- a/lmathlib.c +++ b/lmathlib.c @@ -578,7 +578,7 @@ static int math_random (lua_State *L) { low = 1; up = luaL_checkinteger(L, 1); if (up == 0) { /* single 0 as argument? */ - lua_pushinteger(L, I2UInt(rv)); /* full random integer */ + lua_pushinteger(L, l_castU2S(I2UInt(rv))); /* full random integer */ return 1; } break; @@ -594,7 +594,7 @@ static int math_random (lua_State *L) { luaL_argcheck(L, low <= up, 1, "interval is empty"); /* project random integer into the interval [0, up - low] */ p = project(I2UInt(rv), (lua_Unsigned)up - (lua_Unsigned)low, state); - lua_pushinteger(L, p + (lua_Unsigned)low); + lua_pushinteger(L, l_castU2S(p) + low); return 1; } @@ -608,8 +608,8 @@ static void setseed (lua_State *L, Rand64 *state, state[3] = Int2I(0); for (i = 0; i < 16; i++) nextrand(state); /* discard initial values to "spread" seed */ - lua_pushinteger(L, n1); - lua_pushinteger(L, n2); + lua_pushinteger(L, l_castU2S(n1)); + lua_pushinteger(L, l_castU2S(n2)); } @@ -621,8 +621,8 @@ static int math_randomseed (lua_State *L) { n2 = I2UInt(nextrand(state->s)); /* in case seed is not that random... */ } else { - n1 = luaL_checkinteger(L, 1); - n2 = luaL_optinteger(L, 2, 0); + n1 = l_castS2U(luaL_checkinteger(L, 1)); + n2 = l_castS2U(luaL_optinteger(L, 2, 0)); } setseed(L, state->s, n1, n2); return 2; /* return seeds */ diff --git a/lmem.c b/lmem.c index dfd8a49b46..d02c9fdc98 100644 --- a/lmem.c +++ b/lmem.c @@ -95,7 +95,7 @@ static void *firsttry (global_State *g, void *block, size_t os, size_t ns) { void *luaM_growaux_ (lua_State *L, void *block, int nelems, int *psize, - int size_elems, int limit, const char *what) { + unsigned size_elems, int limit, const char *what) { void *newblock; int size = *psize; if (nelems + 1 <= size) /* does one extra element still fit? */ @@ -203,9 +203,9 @@ void *luaM_malloc_ (lua_State *L, size_t size, int tag) { return NULL; /* that's all */ else { global_State *g = G(L); - void *newblock = firsttry(g, NULL, tag, size); + void *newblock = firsttry(g, NULL, cast_sizet(tag), size); if (l_unlikely(newblock == NULL)) { - newblock = tryagain(L, NULL, tag, size); + newblock = tryagain(L, NULL, cast_sizet(tag), size); if (newblock == NULL) luaM_error(L); } diff --git a/lmem.h b/lmem.h index c5dada9cb3..aa30610518 100644 --- a/lmem.h +++ b/lmem.h @@ -85,7 +85,7 @@ LUAI_FUNC void *luaM_saferealloc_ (lua_State *L, void *block, size_t oldsize, size_t size); LUAI_FUNC void luaM_free_ (lua_State *L, void *block, size_t osize); LUAI_FUNC void *luaM_growaux_ (lua_State *L, void *block, int nelems, - int *size, int size_elem, int limit, + int *size, unsigned size_elem, int limit, const char *what); LUAI_FUNC void *luaM_shrinkvector_ (lua_State *L, void *block, int *nelem, int final_n, int size_elem); diff --git a/loadlib.c b/loadlib.c index 84f56ea667..e5ed135286 100644 --- a/loadlib.c +++ b/loadlib.c @@ -288,13 +288,13 @@ static void setpath (lua_State *L, const char *fieldname, luaL_Buffer b; luaL_buffinit(L, &b); if (path < dftmark) { /* is there a prefix before ';;'? */ - luaL_addlstring(&b, path, dftmark - path); /* add it */ + luaL_addlstring(&b, path, ct_diff2sz(dftmark - path)); /* add it */ luaL_addchar(&b, *LUA_PATH_SEP); } luaL_addstring(&b, dft); /* add default */ if (dftmark < path + len - 2) { /* is there a suffix after ';;'? */ luaL_addchar(&b, *LUA_PATH_SEP); - luaL_addlstring(&b, dftmark + 2, (path + len - 2) - dftmark); + luaL_addlstring(&b, dftmark + 2, ct_diff2sz((path + len - 2) - dftmark)); } luaL_pushresult(&b); } @@ -543,7 +543,7 @@ static int loadfunc (lua_State *L, const char *filename, const char *modname) { mark = strchr(modname, *LUA_IGMARK); if (mark) { int stat; - openfunc = lua_pushlstring(L, modname, mark - modname); + openfunc = lua_pushlstring(L, modname, ct_diff2sz(mark - modname)); openfunc = lua_pushfstring(L, LUA_POF"%s", openfunc); stat = lookforfunc(L, filename, openfunc); if (stat != ERRFUNC) return stat; @@ -568,7 +568,7 @@ static int searcher_Croot (lua_State *L) { const char *p = strchr(name, '.'); int stat; if (p == NULL) return 0; /* is root */ - lua_pushlstring(L, name, p - name); + lua_pushlstring(L, name, ct_diff2sz(p - name)); filename = findfile(L, lua_tostring(L, -1), "cpath", LUA_CSUBSEP); if (filename == NULL) return 1; /* root not found */ if ((stat = loadfunc(L, filename, name)) != 0) { diff --git a/lobject.c b/lobject.c index 45a2731110..1c4ea1aff1 100644 --- a/lobject.c +++ b/lobject.c @@ -32,7 +32,7 @@ /* ** Computes ceil(log2(x)) */ -int luaO_ceillog2 (unsigned int x) { +lu_byte luaO_ceillog2 (unsigned int x) { static const lu_byte log_2[256] = { /* log_2[i - 1] = ceil(log2(i)) */ 0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5, 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, @@ -46,7 +46,7 @@ int luaO_ceillog2 (unsigned int x) { int l = 0; x--; while (x >= 256) { l += 8; x >>= 8; } - return l + log_2[x]; + return cast_byte(l + log_2[x]); } /* @@ -57,16 +57,19 @@ int luaO_ceillog2 (unsigned int x) { ** to signal that. So, the real value is (1xxxx) * 2^(eeee - 7 - 1) if ** eeee != 0, and (xxxx) * 2^-7 otherwise (subnormal numbers). */ -unsigned int luaO_codeparam (unsigned int p) { +lu_byte luaO_codeparam (unsigned int p) { if (p >= (cast(lu_mem, 0x1F) << (0xF - 7 - 1)) * 100u) /* overflow? */ return 0xFF; /* return maximum value */ else { p = (cast(l_uint32, p) * 128 + 99) / 100; /* round up the division */ - if (p < 0x10) /* subnormal number? */ - return p; /* exponent bits are already zero; nothing else to do */ - else { - int log = luaO_ceillog2(p + 1) - 5; /* preserve 5 bits */ - return ((p >> log) - 0x10) | ((log + 1) << 4); + if (p < 0x10) { /* subnormal number? */ + /* exponent bits are already zero; nothing else to do */ + return cast_byte(p); + } + else { /* p >= 0x10 implies ceil(log2(p + 1)) >= 5 */ + /* preserve 5 bits in 'p' */ + unsigned log = luaO_ceillog2(p + 1) - 5u; + return cast_byte(((p >> log) - 0x10) | ((log + 1) << 4)); } } } @@ -81,7 +84,7 @@ unsigned int luaO_codeparam (unsigned int p) { ** more significant bits, as long as the multiplication does not ** overflow, so we check which order is best. */ -l_obj luaO_applyparam (unsigned int p, l_obj x) { +l_obj luaO_applyparam (lu_byte p, l_obj x) { unsigned int m = p & 0xF; /* mantissa */ int e = (p >> 4); /* exponent */ if (e > 0) { /* normalized? */ @@ -189,9 +192,9 @@ void luaO_arith (lua_State *L, int op, const TValue *p1, const TValue *p2, } -int luaO_hexavalue (int c) { - if (lisdigit(c)) return c - '0'; - else return (ltolower(c) - 'a') + 10; +lu_byte luaO_hexavalue (int c) { + if (lisdigit(c)) return cast_byte(c - '0'); + else return cast_byte((ltolower(c) - 'a') + 10); } @@ -349,7 +352,7 @@ static const char *l_str2int (const char *s, lua_Integer *result) { int d = *s - '0'; if (a >= MAXBY10 && (a > MAXBY10 || d > MAXLASTD + neg)) /* overflow? */ return NULL; /* do not accept it (as integer) */ - a = a * 10 + d; + a = a * 10 + cast_uint(d); empty = 0; } } @@ -373,7 +376,7 @@ size_t luaO_str2num (const char *s, TValue *o) { } else return 0; /* conversion failed */ - return (e - s) + 1; /* success; return string size */ + return ct_diff2sz(e - s) + 1; /* success; return string size */ } @@ -409,7 +412,7 @@ int luaO_utf8esc (char *buff, unsigned long x) { /* ** Convert a number object to a string, adding it to a buffer */ -static int tostringbuff (TValue *obj, char *buff) { +static unsigned tostringbuff (TValue *obj, char *buff) { int len; lua_assert(ttisnumber(obj)); if (ttisinteger(obj)) @@ -421,7 +424,7 @@ static int tostringbuff (TValue *obj, char *buff) { buff[len++] = '0'; /* adds '.0' to result */ } } - return len; + return cast_uint(len); } @@ -430,7 +433,7 @@ static int tostringbuff (TValue *obj, char *buff) { */ void luaO_tostring (lua_State *L, TValue *obj) { char buff[MAXNUMBER2STR]; - int len = tostringbuff(obj, buff); + unsigned len = tostringbuff(obj, buff); setsvalue(L, obj, luaS_newlstr(L, buff, len)); } @@ -448,13 +451,13 @@ void luaO_tostring (lua_State *L, TValue *obj) { ** (LUA_IDSIZE + MAXNUMBER2STR) + a minimal space for basic messages, ** so that 'luaG_addinfo' can work directly on the buffer. */ -#define BUFVFS (LUA_IDSIZE + MAXNUMBER2STR + 95) +#define BUFVFS cast_uint(LUA_IDSIZE + MAXNUMBER2STR + 95) /* buffer used by 'luaO_pushvfstring' */ typedef struct BuffFS { lua_State *L; int pushed; /* true if there is a part of the result on the stack */ - int blen; /* length of partial string in 'space' */ + unsigned blen; /* length of partial string in 'space' */ char space[BUFVFS]; /* holds last part of the result */ } BuffFS; @@ -492,7 +495,7 @@ static void clearbuff (BuffFS *buff) { ** Get a space of size 'sz' in the buffer. If buffer has not enough ** space, empty it. 'sz' must fit in an empty buffer. */ -static char *getbuff (BuffFS *buff, int sz) { +static char *getbuff (BuffFS *buff, unsigned sz) { lua_assert(buff->blen <= BUFVFS); lua_assert(sz <= BUFVFS); if (sz > BUFVFS - buff->blen) /* not enough space? */ clearbuff(buff); @@ -509,9 +512,9 @@ static char *getbuff (BuffFS *buff, int sz) { */ static void addstr2buff (BuffFS *buff, const char *str, size_t slen) { if (slen <= BUFVFS) { /* does string fit into buffer? */ - char *bf = getbuff(buff, cast_int(slen)); + char *bf = getbuff(buff, cast_uint(slen)); memcpy(bf, str, slen); /* add string to buffer */ - addsize(buff, cast_int(slen)); + addsize(buff, cast_uint(slen)); } else { /* string larger than buffer */ clearbuff(buff); /* string comes after buffer's content */ @@ -525,7 +528,7 @@ static void addstr2buff (BuffFS *buff, const char *str, size_t slen) { */ static void addnum2buff (BuffFS *buff, TValue *num) { char *numbuff = getbuff(buff, MAXNUMBER2STR); - int len = tostringbuff(num, numbuff); /* format number into 'numbuff' */ + unsigned len = tostringbuff(num, numbuff); /* format number into 'numbuff' */ addsize(buff, len); } @@ -537,10 +540,10 @@ static void addnum2buff (BuffFS *buff, TValue *num) { const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) { BuffFS buff; /* holds last part of the result */ const char *e; /* points to next '%' */ - buff.pushed = buff.blen = 0; + buff.pushed = 0; buff.blen = 0; buff.L = L; while ((e = strchr(fmt, '%')) != NULL) { - addstr2buff(&buff, fmt, e - fmt); /* add 'fmt' up to '%' */ + addstr2buff(&buff, fmt, ct_diff2sz(e - fmt)); /* add 'fmt' up to '%' */ switch (*(e + 1)) { /* conversion specifier */ case 's': { /* zero-terminated string */ const char *s = va_arg(argp, char *); @@ -549,7 +552,7 @@ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) { break; } case 'c': { /* an 'int' as a character */ - char c = cast_uchar(va_arg(argp, int)); + char c = cast_char(va_arg(argp, int)); addstr2buff(&buff, &c, sizeof(char)); break; } @@ -572,17 +575,17 @@ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) { break; } case 'p': { /* a pointer */ - const int sz = 3 * sizeof(void*) + 8; /* enough space for '%p' */ + const unsigned sz = 3 * sizeof(void*) + 8; /* enough space for '%p' */ char *bf = getbuff(&buff, sz); void *p = va_arg(argp, void *); int len = lua_pointer2str(bf, sz, p); - addsize(&buff, len); + addsize(&buff, cast_uint(len)); break; } - case 'U': { /* a 'long' as a UTF-8 sequence */ + case 'U': { /* an 'unsigned long' as a UTF-8 sequence */ char bf[UTF8BUFFSZ]; - int len = luaO_utf8esc(bf, va_arg(argp, long)); - addstr2buff(&buff, bf + UTF8BUFFSZ - len, len); + int len = luaO_utf8esc(bf, va_arg(argp, unsigned long)); + addstr2buff(&buff, bf + UTF8BUFFSZ - len, cast_uint(len)); break; } case '%': { @@ -648,7 +651,8 @@ void luaO_chunkid (char *out, const char *source, size_t srclen) { addstr(out, source, srclen); /* keep it */ } else { - if (nl != NULL) srclen = nl - source; /* stop at first newline */ + if (nl != NULL) + srclen = ct_diff2sz(nl - source); /* stop at first newline */ if (srclen > bufflen) srclen = bufflen; addstr(out, source, srclen); addstr(out, RETS, LL(RETS)); diff --git a/lobject.h b/lobject.h index 641e782c60..fb66dff7c9 100644 --- a/lobject.h +++ b/lobject.h @@ -432,13 +432,13 @@ typedef struct TString { /* get string length from 'TString *ts' */ #define tsslen(ts) \ - (strisshr(ts) ? cast_uint((ts)->shrlen) : (ts)->u.lnglen) + (strisshr(ts) ? cast_sizet((ts)->shrlen) : (ts)->u.lnglen) /* ** Get string and length */ #define getlstr(ts, len) \ (strisshr(ts) \ - ? (cast_void((len) = (ts)->shrlen), rawgetshrstr(ts)) \ + ? (cast_void((len) = cast_sizet((ts)->shrlen)), rawgetshrstr(ts)) \ : (cast_void((len) = (ts)->u.lnglen), (ts)->contents)) /* }================================================================== */ @@ -517,8 +517,8 @@ typedef struct Udata0 { /* compute the offset of the memory area of a userdata */ #define udatamemoffset(nuv) \ - ((nuv) == 0 ? offsetof(Udata0, bindata) \ - : offsetof(Udata, uv) + (sizeof(UValue) * (nuv))) + ((nuv) == 0 ? offsetof(Udata0, bindata) \ + : offsetof(Udata, uv) + (sizeof(UValue) * (nuv))) /* get the address of the memory block inside 'Udata' */ #define getudatamem(u) (cast_charp(u) + udatamemoffset((u)->nuvalue)) @@ -825,10 +825,10 @@ typedef struct Table { ** 'module' operation for hashing (size is always a power of 2) */ #define lmod(s,size) \ - (check_exp((size&(size-1))==0, (cast_int((s) & ((size)-1))))) + (check_exp((size&(size-1))==0, (cast_uint(s) & cast_uint((size)-1)))) -#define twoto(x) (1<<(x)) +#define twoto(x) (1u<<(x)) #define sizenode(t) (twoto((t)->lsizenode)) @@ -836,16 +836,16 @@ typedef struct Table { #define UTF8BUFFSZ 8 LUAI_FUNC int luaO_utf8esc (char *buff, unsigned long x); -LUAI_FUNC int luaO_ceillog2 (unsigned int x); -LUAI_FUNC unsigned int luaO_codeparam (unsigned int p); -LUAI_FUNC l_obj luaO_applyparam (unsigned int p, l_obj x); +LUAI_FUNC lu_byte luaO_ceillog2 (unsigned int x); +LUAI_FUNC lu_byte luaO_codeparam (unsigned int p); +LUAI_FUNC l_obj luaO_applyparam (lu_byte p, l_obj x); LUAI_FUNC int luaO_rawarith (lua_State *L, int op, const TValue *p1, const TValue *p2, TValue *res); LUAI_FUNC void luaO_arith (lua_State *L, int op, const TValue *p1, const TValue *p2, StkId res); LUAI_FUNC size_t luaO_str2num (const char *s, TValue *o); -LUAI_FUNC int luaO_hexavalue (int c); +LUAI_FUNC lu_byte luaO_hexavalue (int c); LUAI_FUNC void luaO_tostring (lua_State *L, TValue *obj); LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp); diff --git a/lopcodes.h b/lopcodes.h index 736946e388..31f6fac01b 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -72,8 +72,11 @@ enum OpMode {iABC, ivABC, iABx, iAsBx, iAx, isJ}; ** so they must fit in ints. */ -/* Check whether type 'int' has at least 'b' bits ('b' < 32) */ -#define L_INTHASBITS(b) ((UINT_MAX >> ((b) - 1)) >= 1) +/* +** Check whether type 'int' has at least 'b' + 1 bits. +** 'b' < 32; +1 for the sign bit. +*/ +#define L_INTHASBITS(b) ((UINT_MAX >> (b)) >= 1) #if L_INTHASBITS(SIZE_Bx) diff --git a/loslib.c b/loslib.c index 8280331b77..4623ad5ecf 100644 --- a/loslib.c +++ b/loslib.c @@ -275,7 +275,7 @@ static int getfield (lua_State *L, const char *key, int d, int delta) { static const char *checkoption (lua_State *L, const char *conv, ptrdiff_t convlen, char *buff) { const char *option = LUA_STRFTIMEOPTIONS; - int oplen = 1; /* length of options being checked */ + unsigned oplen = 1; /* length of options being checked */ for (; *option != '\0' && oplen <= convlen; option += oplen) { if (*option == '|') /* next block? */ oplen++; /* will check options with next length (+1) */ diff --git a/lparser.c b/lparser.c index 0ed9631ad3..452ab19eac 100644 --- a/lparser.c +++ b/lparser.c @@ -172,7 +172,8 @@ static void codename (LexState *ls, expdesc *e) { ** Register a new local variable in the active 'Proto' (for debug ** information). */ -static int registerlocalvar (LexState *ls, FuncState *fs, TString *varname) { +static short registerlocalvar (LexState *ls, FuncState *fs, + TString *varname) { Proto *f = fs->f; int oldsize = f->sizelocvars; luaM_growvector(ls->L, f->locvars, fs->ndebugvars, f->sizelocvars, @@ -190,7 +191,7 @@ static int registerlocalvar (LexState *ls, FuncState *fs, TString *varname) { ** Create a new local variable with the given 'name' and given 'kind'. ** Return its index in the function. */ -static int new_localvarkind (LexState *ls, TString *name, int kind) { +static int new_localvarkind (LexState *ls, TString *name, lu_byte kind) { lua_State *L = ls->L; FuncState *fs = ls->fs; Dyndata *dyd = ls->dyd; @@ -234,11 +235,11 @@ static Vardesc *getlocalvardesc (FuncState *fs, int vidx) { ** register. For that, search for the highest variable below that level ** that is in a register and uses its register index ('ridx') plus one. */ -static int reglevel (FuncState *fs, int nvar) { +static lu_byte reglevel (FuncState *fs, int nvar) { while (nvar-- > 0) { Vardesc *vd = getlocalvardesc(fs, nvar); /* get previous variable */ if (vd->vd.kind != RDKCTC) /* is in a register? */ - return vd->vd.ridx + 1; + return cast_byte(vd->vd.ridx + 1); } return 0; /* no variables in registers */ } @@ -248,7 +249,7 @@ static int reglevel (FuncState *fs, int nvar) { ** Return the number of variables in the register stack for the given ** function. */ -int luaY_nvarstack (FuncState *fs) { +lu_byte luaY_nvarstack (FuncState *fs) { return reglevel(fs, fs->nactvar); } @@ -274,7 +275,7 @@ static LocVar *localdebuginfo (FuncState *fs, int vidx) { static void init_var (FuncState *fs, expdesc *e, int vidx) { e->f = e->t = NO_JUMP; e->k = VLOCAL; - e->u.var.vidx = vidx; + e->u.var.vidx = cast(unsigned short, vidx); e->u.var.ridx = getlocalvardesc(fs, vidx)->vd.ridx; } @@ -323,7 +324,7 @@ static void adjustlocalvars (LexState *ls, int nvars) { for (i = 0; i < nvars; i++) { int vidx = fs->nactvar++; Vardesc *var = getlocalvardesc(fs, vidx); - var->vd.ridx = reglevel++; + var->vd.ridx = cast_byte(reglevel++); var->vd.pidx = registerlocalvar(ls, fs, var->vd.name); } } @@ -505,7 +506,7 @@ static void adjust_assign (LexState *ls, int nvars, int nexps, expdesc *e) { if (needed > 0) luaK_reserveregs(fs, needed); /* registers for extra values */ else /* adding 'needed' is actually a subtraction */ - fs->freereg += needed; /* remove extra values */ + fs->freereg = cast_byte(fs->freereg + needed); /* remove extra values */ } @@ -682,7 +683,7 @@ static void leaveblock (FuncState *fs) { BlockCnt *bl = fs->bl; LexState *ls = fs->ls; int hasclose = 0; - int stklevel = reglevel(fs, bl->nactvar); /* level outside the block */ + lu_byte stklevel = reglevel(fs, bl->nactvar); /* level outside the block */ removevars(fs, bl->nactvar); /* remove block locals */ lua_assert(bl->nactvar == fs->nactvar); /* back to level on entry */ if (bl->isloop) /* has to fix pending breaks? */ @@ -856,7 +857,7 @@ typedef struct ConsControl { static void recfield (LexState *ls, ConsControl *cc) { /* recfield -> (NAME | '['exp']') = exp */ FuncState *fs = ls->fs; - int reg = ls->fs->freereg; + lu_byte reg = ls->fs->freereg; expdesc tab, key, val; if (ls->t.token == TK_NAME) { checklimit(fs, cc->nh, INT_MAX, "items in a constructor"); @@ -939,7 +940,7 @@ static void field (LexState *ls, ConsControl *cc) { static int maxtostore (FuncState *fs) { int numfreeregs = MAX_FSTACK - fs->freereg; if (numfreeregs >= 160) /* "lots" of registers? */ - return numfreeregs / 5u; /* use up to 1/5 of them */ + return numfreeregs / 5; /* use up to 1/5 of them */ else if (numfreeregs >= 80) /* still "enough" registers? */ return 10; /* one 'SETLIST' instruction for each 10 values */ else /* save registers for potential more nesting */ @@ -1090,8 +1091,9 @@ static void funcargs (LexState *ls, expdesc *f) { } init_exp(f, VCALL, luaK_codeABC(fs, OP_CALL, base, nparams+1, 2)); luaK_fixline(fs, line); - fs->freereg = base+1; /* call removes function and arguments and leaves - one result (unless changed later) */ + /* call removes function and arguments and leaves one result (unless + changed later) */ + fs->freereg = cast_byte(base + 1); } @@ -1356,7 +1358,7 @@ struct LHS_assign { */ 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 */ + lu_byte extra = fs->freereg; /* eventual position to save local variable */ int conflict = 0; for (; lh; lh = lh->prev) { /* check all previous assignments */ if (vkisindexed(lh->v.k)) { /* assignment to table field? */ @@ -1723,7 +1725,7 @@ static void localfunc (LexState *ls) { } -static int getlocalattribute (LexState *ls) { +static lu_byte getlocalattribute (LexState *ls) { /* ATTRIB -> ['<' Name '>'] */ if (testnext(ls, '<')) { TString *ts = str_checkname(ls); @@ -1760,7 +1762,7 @@ static void localstat (LexState *ls) { expdesc e; do { TString *vname = str_checkname(ls); - int kind = getlocalattribute(ls); + lu_byte kind = getlocalattribute(ls); vidx = new_localvarkind(ls, vname, kind); if (kind == RDKTOCLOSE) { /* to-be-closed? */ if (toclose != -1) /* one already present? */ diff --git a/lparser.h b/lparser.h index 5e4500f181..535dc9da88 100644 --- a/lparser.h +++ b/lparser.h @@ -163,7 +163,7 @@ typedef struct FuncState { } FuncState; -LUAI_FUNC int luaY_nvarstack (FuncState *fs); +LUAI_FUNC lu_byte luaY_nvarstack (FuncState *fs); LUAI_FUNC LClosure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, Dyndata *dyd, const char *name, int firstchar); diff --git a/lstate.c b/lstate.c index 4511bc005d..eb71ed8c9e 100644 --- a/lstate.c +++ b/lstate.c @@ -190,7 +190,8 @@ static void freestack (lua_State *L) { L->ci = &L->base_ci; /* free the entire 'ci' list */ freeCI(L); lua_assert(L->nci == 0); - luaM_freearray(L, L->stack.p, stacksize(L) + EXTRA_STACK); /* free stack */ + /* free stack */ + luaM_freearray(L, L->stack.p, cast_sizet(stacksize(L) + EXTRA_STACK)); } @@ -266,7 +267,7 @@ static void close_state (lua_State *L) { luaC_freeallobjects(L); /* collect all objects */ luai_userstateclose(L); } - luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size); + luaM_freearray(L, G(L)->strt.hash, cast_sizet(G(L)->strt.size)); freestack(L); lua_assert(g->totalbytes == sizeof(LG)); lua_assert(gettotalobjs(g) == 1); diff --git a/lstate.h b/lstate.h index ff86d8253e..aa9687aed4 100644 --- a/lstate.h +++ b/lstate.h @@ -259,7 +259,7 @@ struct CallInfo { #define getcistrecst(ci) (((ci)->callstatus >> CIST_RECST) & 7) #define setcistrecst(ci,st) \ check_exp(((st) & 7) == (st), /* status must fit in three bits */ \ - ((ci)->callstatus = ((ci)->callstatus & ~(7 << CIST_RECST)) \ + ((ci)->callstatus = ((ci)->callstatus & ~(7u << CIST_RECST)) \ | (cast(l_uint32, st) << CIST_RECST))) diff --git a/lstring.c b/lstring.c index 86ee24114e..0c89a51b07 100644 --- a/lstring.c +++ b/lstring.c @@ -164,7 +164,7 @@ size_t luaS_sizelngstr (size_t len, int kind) { /* ** creates a new string object */ -static TString *createstrobj (lua_State *L, size_t totalsize, int tag, +static TString *createstrobj (lua_State *L, size_t totalsize, lu_byte tag, unsigned h) { TString *ts; GCObject *o; @@ -233,7 +233,7 @@ static TString *internshrstr (lua_State *L, const char *str, size_t l) { list = &tb->hash[lmod(h, tb->size)]; /* rehash with new size */ } ts = createstrobj(L, sizestrshr(l), LUA_VSHRSTR, h); - ts->shrlen = cast_byte(l); + ts->shrlen = cast(ls_byte, l); getshrstr(ts)[l] = '\0'; /* ending 0 */ memcpy(getshrstr(ts), str, l * sizeof(char)); ts->u.hnext = *list; @@ -283,7 +283,7 @@ TString *luaS_new (lua_State *L, const char *str) { } -Udata *luaS_newudata (lua_State *L, size_t s, int nuvalue) { +Udata *luaS_newudata (lua_State *L, size_t s, unsigned short nuvalue) { Udata *u; int i; GCObject *o; @@ -301,7 +301,7 @@ Udata *luaS_newudata (lua_State *L, size_t s, int nuvalue) { struct NewExt { - int kind; + ls_byte kind; const char *s; size_t len; TString *ts; /* output */ diff --git a/lstring.h b/lstring.h index c88357aaba..26f4b8e1f3 100644 --- a/lstring.h +++ b/lstring.h @@ -61,7 +61,8 @@ LUAI_FUNC void luaS_resize (lua_State *L, int newsize); LUAI_FUNC void luaS_clearcache (global_State *g); LUAI_FUNC void luaS_init (lua_State *L); LUAI_FUNC void luaS_remove (lua_State *L, TString *ts); -LUAI_FUNC Udata *luaS_newudata (lua_State *L, size_t s, int nuvalue); +LUAI_FUNC Udata *luaS_newudata (lua_State *L, size_t s, + unsigned short nuvalue); LUAI_FUNC TString *luaS_newlstr (lua_State *L, const char *str, size_t l); LUAI_FUNC TString *luaS_new (lua_State *L, const char *str); LUAI_FUNC TString *luaS_createlngstrobj (lua_State *L, size_t l); diff --git a/lstrlib.c b/lstrlib.c index 8d6573a6cd..e9421c279b 100644 --- a/lstrlib.c +++ b/lstrlib.c @@ -113,7 +113,7 @@ static int str_lower (lua_State *L) { const char *s = luaL_checklstring(L, 1, &l); char *p = luaL_buffinitsize(L, &b, l); for (i=0; i MAX_SIZE / n)) + else if (l_unlikely(l + lsep < l || l + lsep > MAX_SIZE / cast_sizet(n))) return luaL_error(L, "resulting string too large"); else { size_t totallen = ((size_t)n * (l + lsep)) - lsep; @@ -172,7 +172,7 @@ static int str_byte (lua_State *L) { n = (int)(pose - posi) + 1; luaL_checkstack(L, n, "string slice too long"); for (i=0; icapture[l].len; + len = cast_sizet(ms->capture[l].len); if ((size_t)(ms->src_end-s) >= len && memcmp(ms->capture[l].init, s, len) == 0) return s+len; @@ -674,7 +674,7 @@ static const char *lmemfind (const char *s1, size_t l1, if (memcmp(init, s2+1, l2) == 0) return init-1; else { /* correct 'l1' and 's1' to try again */ - l1 -= init-s1; + l1 -= ct_diff2sz(init - s1); s1 = init; } } @@ -690,13 +690,13 @@ static const char *lmemfind (const char *s1, size_t l1, ** its length and put its address in '*cap'. If it is an integer ** (a position), push it on the stack and return CAP_POSITION. */ -static size_t get_onecapture (MatchState *ms, int i, const char *s, +static ptrdiff_t get_onecapture (MatchState *ms, int i, const char *s, const char *e, const char **cap) { if (i >= ms->level) { if (l_unlikely(i != 0)) luaL_error(ms->L, "invalid capture index %%%d", i + 1); *cap = s; - return e - s; + return (e - s); } else { ptrdiff_t capl = ms->capture[i].len; @@ -718,7 +718,7 @@ static void push_onecapture (MatchState *ms, int i, const char *s, const char *cap; ptrdiff_t l = get_onecapture(ms, i, s, e, &cap); if (l != CAP_POSITION) - lua_pushlstring(ms->L, cap, l); + lua_pushlstring(ms->L, cap, cast_sizet(l)); /* else position was already pushed */ } @@ -776,7 +776,7 @@ static int str_find_aux (lua_State *L, int find) { const char *s2 = lmemfind(s + init, ls - init, p, lp); if (s2) { lua_pushinteger(L, (s2 - s) + 1); - lua_pushinteger(L, (s2 - s) + lp); + lua_pushinteger(L, cast_st2S(ct_diff2sz(s2 - s) + lp)); return 2; } } @@ -866,23 +866,23 @@ static void add_s (MatchState *ms, luaL_Buffer *b, const char *s, const char *news = lua_tolstring(L, 3, &l); const char *p; while ((p = (char *)memchr(news, L_ESC, l)) != NULL) { - luaL_addlstring(b, news, p - news); + luaL_addlstring(b, news, ct_diff2sz(p - news)); p++; /* skip ESC */ if (*p == L_ESC) /* '%%' */ luaL_addchar(b, *p); else if (*p == '0') /* '%0' */ - luaL_addlstring(b, s, e - s); + luaL_addlstring(b, s, ct_diff2sz(e - s)); else if (isdigit(cast_uchar(*p))) { /* '%n' */ const char *cap; ptrdiff_t resl = get_onecapture(ms, *p - '1', s, e, &cap); if (resl == CAP_POSITION) luaL_addvalue(b); /* add position to accumulated result */ else - luaL_addlstring(b, cap, resl); + luaL_addlstring(b, cap, cast_sizet(resl)); } else luaL_error(L, "invalid use of '%c' in replacement string", L_ESC); - l -= p + 1 - news; + l -= ct_diff2sz(p + 1 - news); news = p + 1; } luaL_addlstring(b, news, l); @@ -917,7 +917,7 @@ static int add_value (MatchState *ms, luaL_Buffer *b, const char *s, } if (!lua_toboolean(L, -1)) { /* nil or false? */ lua_pop(L, 1); /* remove value */ - luaL_addlstring(b, s, e - s); /* keep original text */ + luaL_addlstring(b, s, ct_diff2sz(e - s)); /* keep original text */ return 0; /* no changes */ } else if (l_unlikely(!lua_isstring(L, -1))) @@ -936,7 +936,8 @@ static int str_gsub (lua_State *L) { const char *p = luaL_checklstring(L, 2, &lp); /* pattern */ const char *lastmatch = NULL; /* end of last match */ int tr = lua_type(L, 3); /* replacement type */ - lua_Integer max_s = luaL_optinteger(L, 4, srcl + 1); /* max replacements */ + /* max replacements */ + lua_Integer max_s = luaL_optinteger(L, 4, cast_st2S(srcl) + 1); int anchor = (*p == '^'); lua_Integer n = 0; /* replacement count */ int changed = 0; /* change flag */ @@ -966,7 +967,7 @@ static int str_gsub (lua_State *L) { if (!changed) /* no changes? */ lua_pushvalue(L, 1); /* return original string */ else { /* something changed */ - luaL_addlstring(&b, src, ms.src_end-src); + luaL_addlstring(&b, src, ct_diff2sz(ms.src_end - src)); luaL_pushresult(&b); /* create and return new string */ } lua_pushinteger(L, n); /* number of substitutions */ @@ -1004,15 +1005,15 @@ static int str_gsub (lua_State *L) { /* ** Add integer part of 'x' to buffer and return new 'x' */ -static lua_Number adddigit (char *buff, int n, lua_Number x) { +static lua_Number adddigit (char *buff, unsigned n, lua_Number x) { lua_Number dd = l_mathop(floor)(x); /* get integer part from 'x' */ int d = (int)dd; - buff[n] = (d < 10 ? d + '0' : d - 10 + 'a'); /* add to buffer */ + buff[n] = cast_char(d < 10 ? d + '0' : d - 10 + 'a'); /* add to buffer */ return x - dd; /* return what is left */ } -static int num2straux (char *buff, int sz, lua_Number x) { +static int num2straux (char *buff, unsigned sz, lua_Number x) { /* if 'inf' or 'NaN', format it like '%g' */ if (x != x || x == (lua_Number)HUGE_VAL || x == -(lua_Number)HUGE_VAL) return l_sprintf(buff, sz, LUA_NUMBER_FMT, (LUAI_UACNUMBER)x); @@ -1023,7 +1024,7 @@ static int num2straux (char *buff, int sz, lua_Number x) { else { int e; lua_Number m = l_mathop(frexp)(x, &e); /* 'x' fraction and exponent */ - int n = 0; /* character count */ + unsigned n = 0; /* character count */ if (m < 0) { /* is number negative? */ buff[n++] = '-'; /* add sign */ m = -m; /* make it positive */ @@ -1037,20 +1038,20 @@ static int num2straux (char *buff, int sz, lua_Number x) { m = adddigit(buff, n++, m * 16); } while (m > 0); } - n += l_sprintf(buff + n, sz - n, "p%+d", e); /* add exponent */ + n += cast_uint(l_sprintf(buff + n, sz - n, "p%+d", e)); /* add exponent */ lua_assert(n < sz); - return n; + return cast_int(n); } } -static int lua_number2strx (lua_State *L, char *buff, int sz, +static int lua_number2strx (lua_State *L, char *buff, unsigned sz, const char *fmt, lua_Number x) { int n = num2straux(buff, sz, x); if (fmt[SIZELENMOD] == 'A') { int i; for (i = 0; i < n; i++) - buff[i] = toupper(cast_uchar(buff[i])); + buff[i] = cast_char(toupper(cast_uchar(buff[i]))); } else if (l_unlikely(fmt[SIZELENMOD] != 'a')) return luaL_error(L, "modifiers for format '%%a'/'%%A' not implemented"); @@ -1151,9 +1152,9 @@ static int quotefloat (lua_State *L, char *buff, lua_Number n) { int nb = lua_number2strx(L, buff, MAX_ITEM, "%" LUA_NUMBER_FRMLEN "a", n); /* ensures that 'buff' string uses a dot as the radix character */ - if (memchr(buff, '.', nb) == NULL) { /* no dot? */ + if (memchr(buff, '.', cast_uint(nb)) == NULL) { /* no dot? */ char point = lua_getlocaledecpoint(); /* try locale point */ - char *ppoint = (char *)memchr(buff, point, nb); + char *ppoint = (char *)memchr(buff, point, cast_uint(nb)); if (ppoint) *ppoint = '.'; /* change it to a dot */ } return nb; @@ -1183,7 +1184,7 @@ static void addliteral (lua_State *L, luaL_Buffer *b, int arg) { : LUA_INTEGER_FMT; /* else use default format */ nb = l_sprintf(buff, MAX_ITEM, format, (LUAI_UACINT)n); } - luaL_addsize(b, nb); + luaL_addsize(b, cast_uint(nb)); break; } case LUA_TNIL: case LUA_TBOOLEAN: { @@ -1277,7 +1278,7 @@ static int str_format (lua_State *L) { luaL_addchar(&b, *strfrmt++); /* %% */ else { /* format item */ char form[MAX_FORMAT]; /* to store the format ('%...') */ - int maxitem = MAX_ITEM; /* maximum length for the result */ + unsigned maxitem = MAX_ITEM; /* maximum length for the result */ char *buff = luaL_prepbuffsize(&b, maxitem); /* to put result */ int nb = 0; /* number of bytes in result */ if (++arg > top) @@ -1360,8 +1361,8 @@ static int str_format (lua_State *L) { return luaL_error(L, "invalid conversion '%s' to 'format'", form); } } - lua_assert(nb < maxitem); - luaL_addsize(&b, nb); + lua_assert(cast_uint(nb) < maxitem); + luaL_addsize(&b, cast_uint(nb)); } } luaL_pushresult(&b); @@ -1409,7 +1410,7 @@ static const union { typedef struct Header { lua_State *L; int islittle; - int maxalign; + unsigned maxalign; } Header; @@ -1443,7 +1444,7 @@ static size_t getnum (const char **fmt, size_t df) { else { size_t a = 0; do { - a = a*10 + (*((*fmt)++) - '0'); + a = a*10 + cast_uint(*((*fmt)++) - '0'); } while (digit(**fmt) && a <= (MAX_SIZE - 9)/10); return a; } @@ -1454,12 +1455,12 @@ static size_t getnum (const char **fmt, size_t df) { ** Read an integer numeral and raises an error if it is larger ** than the maximum size of integers. */ -static int getnumlimit (Header *h, const char **fmt, int df) { +static unsigned getnumlimit (Header *h, const char **fmt, size_t df) { size_t sz = getnum(fmt, df); if (l_unlikely((sz - 1u) >= MAXINTSIZE)) - return luaL_error(h->L, "integral size (%d) out of limits [1,%d]", - sz, MAXINTSIZE); - return cast_int(sz); + return cast_uint(luaL_error(h->L, + "integral size (%d) out of limits [1,%d]", sz, MAXINTSIZE)); + return cast_uint(sz); } @@ -1510,7 +1511,7 @@ static KOption getoption (Header *h, const char **fmt, size_t *size) { case '>': h->islittle = 0; break; case '=': h->islittle = nativeendian.little; break; case '!': { - const int maxalign = offsetof(struct cD, u); + const size_t maxalign = offsetof(struct cD, u); h->maxalign = getnumlimit(h, fmt, maxalign); break; } @@ -1529,8 +1530,8 @@ static KOption getoption (Header *h, const char **fmt, size_t *size) { ** the maximum alignment ('maxalign'). Kchar option needs no alignment ** despite its size. */ -static KOption getdetails (Header *h, size_t totalsize, - const char **fmt, size_t *psize, int *ntoalign) { +static KOption getdetails (Header *h, size_t totalsize, const char **fmt, + size_t *psize, unsigned *ntoalign) { KOption opt = getoption(h, fmt, psize); size_t align = *psize; /* usually, alignment follows size */ if (opt == Kpaddalign) { /* 'X' gets alignment from following option */ @@ -1540,11 +1541,15 @@ static KOption getdetails (Header *h, size_t totalsize, if (align <= 1 || opt == Kchar) /* need no alignment? */ *ntoalign = 0; else { - if (align > cast_sizet(h->maxalign)) /* enforce maximum alignment */ + if (align > h->maxalign) /* enforce maximum alignment */ align = h->maxalign; if (l_unlikely(!ispow2(align))) /* not a power of 2? */ luaL_argerror(h->L, 1, "format asks for alignment not power of 2"); - *ntoalign = (align - (int)(totalsize & (align - 1))) & (align - 1); + else { + /* 'szmoda' = totalsize % align */ + unsigned szmoda = cast_uint(totalsize & (align - 1)); + *ntoalign = cast_uint((align - szmoda) & (align - 1)); + } } return opt; } @@ -1557,9 +1562,9 @@ static KOption getdetails (Header *h, size_t totalsize, ** bytes if necessary (by default they would be zeros). */ static void packint (luaL_Buffer *b, lua_Unsigned n, - int islittle, int size, int neg) { + int islittle, unsigned size, int neg) { char *buff = luaL_prepbuffsize(b, size); - int i; + unsigned i; buff[islittle ? 0 : size - 1] = (char)(n & MC); /* first byte */ for (i = 1; i < size; i++) { n >>= NB; @@ -1578,7 +1583,7 @@ static void packint (luaL_Buffer *b, lua_Unsigned n, ** given 'islittle' is different from native endianness. */ static void copywithendian (char *dest, const char *src, - int size, int islittle) { + unsigned size, int islittle) { if (islittle == nativeendian.little) memcpy(dest, src, size); else { @@ -1599,7 +1604,7 @@ static int str_pack (lua_State *L) { lua_pushnil(L); /* mark to separate arguments from string buffer */ luaL_buffinit(L, &b); while (*fmt != '\0') { - int ntoalign; + unsigned ntoalign; size_t size; KOption opt = getdetails(&h, totalsize, &fmt, &size, &ntoalign); luaL_argcheck(L, size + ntoalign <= MAX_SIZE - totalsize, arg, @@ -1615,7 +1620,7 @@ static int str_pack (lua_State *L) { lua_Integer lim = (lua_Integer)1 << ((size * NB) - 1); luaL_argcheck(L, -lim <= n && n < lim, arg, "integer overflow"); } - packint(&b, (lua_Unsigned)n, h.islittle, size, (n < 0)); + packint(&b, (lua_Unsigned)n, h.islittle, cast_uint(size), (n < 0)); break; } case Kuint: { /* unsigned integers */ @@ -1623,7 +1628,7 @@ static int str_pack (lua_State *L) { if (size < SZINT) /* need overflow check? */ luaL_argcheck(L, (lua_Unsigned)n < ((lua_Unsigned)1 << (size * NB)), arg, "unsigned overflow"); - packint(&b, (lua_Unsigned)n, h.islittle, size, 0); + packint(&b, (lua_Unsigned)n, h.islittle, cast_uint(size), 0); break; } case Kfloat: { /* C float */ @@ -1669,7 +1674,8 @@ static int str_pack (lua_State *L) { luaL_argcheck(L, size >= sizeof(lua_Unsigned) || len < ((lua_Unsigned)1 << (size * NB)), arg, "string length does not fit in given size"); - packint(&b, (lua_Unsigned)len, h.islittle, size, 0); /* pack length */ + /* pack length */ + packint(&b, (lua_Unsigned)len, h.islittle, cast_uint(size), 0); luaL_addlstring(&b, s, len); totalsize += len; break; @@ -1697,20 +1703,20 @@ static int str_pack (lua_State *L) { static int str_packsize (lua_State *L) { Header h; const char *fmt = luaL_checkstring(L, 1); /* format string */ - lua_Integer totalsize = 0; /* accumulate total size of result */ + size_t totalsize = 0; /* accumulate total size of result */ initheader(L, &h); while (*fmt != '\0') { - int ntoalign; + unsigned ntoalign; size_t size; KOption opt = getdetails(&h, totalsize, &fmt, &size, &ntoalign); luaL_argcheck(L, opt != Kstring && opt != Kzstr, 1, "variable-length format"); size += ntoalign; /* total space used by option */ - luaL_argcheck(L, totalsize <= LUA_MAXINTEGER - cast(lua_Integer, size), + luaL_argcheck(L, totalsize <= LUA_MAXINTEGER - size, 1, "format result too large"); totalsize += size; } - lua_pushinteger(L, totalsize); + lua_pushinteger(L, cast_st2S(totalsize)); return 1; } @@ -1759,7 +1765,7 @@ static int str_unpack (lua_State *L) { luaL_argcheck(L, pos <= ld, 3, "initial position out of string"); initheader(L, &h); while (*fmt != '\0') { - int ntoalign; + unsigned ntoalign; size_t size; KOption opt = getdetails(&h, pos, &fmt, &size, &ntoalign); luaL_argcheck(L, ntoalign + size <= ld - pos, 2, @@ -1771,8 +1777,8 @@ static int str_unpack (lua_State *L) { switch (opt) { case Kint: case Kuint: { - lua_Integer res = unpackint(L, data + pos, h.islittle, size, - (opt == Kint)); + lua_Integer res = unpackint(L, data + pos, h.islittle, + cast_int(size), (opt == Kint)); lua_pushinteger(L, res); break; } @@ -1800,7 +1806,7 @@ static int str_unpack (lua_State *L) { } case Kstring: { lua_Unsigned len = (lua_Unsigned)unpackint(L, data + pos, - h.islittle, size, 0); + h.islittle, cast_int(size), 0); luaL_argcheck(L, len <= ld - pos - size, 2, "data string too short"); lua_pushlstring(L, data + pos + size, len); pos += len; /* skip string */ @@ -1820,7 +1826,7 @@ static int str_unpack (lua_State *L) { } pos += size; } - lua_pushinteger(L, pos + 1); /* next position */ + lua_pushinteger(L, cast_st2S(pos) + 1); /* next position */ return n + 1; } diff --git a/ltable.c b/ltable.c index 1be291c700..c2d957e90d 100644 --- a/ltable.c +++ b/ltable.c @@ -109,7 +109,7 @@ typedef union { ** for other types, it is better to avoid modulo by power of 2, as ** they can have many 2 factors. */ -#define hashmod(t,n) (gnode(t, ((n) % ((sizenode(t)-1)|1)))) +#define hashmod(t,n) (gnode(t, ((n) % ((sizenode(t)-1u)|1u)))) #define hashstr(t,str) hashpow2(t, (str)->hash) @@ -139,7 +139,7 @@ static const TValue absentkey = {ABSTKEYCONSTANT}; static Node *hashint (const Table *t, lua_Integer i) { lua_Unsigned ui = l_castS2U(i); if (ui <= cast_uint(INT_MAX)) - return hashmod(t, cast_int(ui)); + return gnode(t, cast_int(ui) % cast_int((sizenode(t)-1) | 1)); else return hashmod(t, ui); } @@ -159,7 +159,7 @@ static Node *hashint (const Table *t, lua_Integer i) { ** INT_MIN. */ #if !defined(l_hashfloat) -static int l_hashfloat (lua_Number n) { +static unsigned l_hashfloat (lua_Number n) { int i; lua_Integer ni; n = l_mathop(frexp)(n, &i) * -cast_num(INT_MIN); @@ -169,7 +169,7 @@ static int l_hashfloat (lua_Number n) { } else { /* normal case */ unsigned int u = cast_uint(i) + cast_uint(ni); - return cast_int(u <= cast_uint(INT_MAX) ? u : ~u); + return (u <= cast_uint(INT_MAX) ? u : ~u); } } #endif @@ -370,7 +370,7 @@ static unsigned findindex (lua_State *L, Table *t, TValue *key, const TValue *n = getgeneric(t, key, 1); if (l_unlikely(isabstkey(n))) luaG_runerror(L, "invalid key to 'next'"); /* key not found */ - i = cast_int(nodefromval(n) - gnode(t, 0)); /* key index in hash table */ + i = cast_uint(nodefromval(n) - gnode(t, 0)); /* key index in hash table */ /* hash elements are numbered after array ones */ return (i + 1) + asize; } @@ -381,14 +381,14 @@ int luaH_next (lua_State *L, Table *t, StkId key) { unsigned int asize = luaH_realasize(t); unsigned int i = findindex(L, t, s2v(key), asize); /* find original key */ for (; i < asize; i++) { /* try first array part */ - int tag = *getArrTag(t, i); + lu_byte tag = *getArrTag(t, i); if (!tagisempty(tag)) { /* a non-empty entry? */ - setivalue(s2v(key), i + 1); + setivalue(s2v(key), cast_int(i) + 1); farr2val(t, i, tag, s2v(key + 1)); return 1; } } - for (i -= asize; cast_int(i) < sizenode(t); i++) { /* hash part */ + for (i -= asize; i < sizenode(t); i++) { /* hash part */ if (!isempty(gval(gnode(t, i)))) { /* a non-empty entry? */ Node *n = gnode(t, i); getnodekey(L, s2v(key), n); @@ -485,7 +485,7 @@ static unsigned computesizes (unsigned nums[], unsigned *pna) { } -static int countint (lua_Integer key, unsigned int *nums) { +static unsigned countint (lua_Integer key, unsigned int *nums) { unsigned int k = arrayindex(key); if (k != 0) { /* is 'key' an appropriate array index? */ nums[luaO_ceillog2(k)]++; /* count as such */ @@ -496,7 +496,7 @@ static int countint (lua_Integer key, unsigned int *nums) { } -l_sinline int arraykeyisempty (const Table *t, lua_Integer key) { +l_sinline int arraykeyisempty (const Table *t, lua_Unsigned key) { int tag = *getArrTag(t, key - 1); return tagisempty(tag); } @@ -534,10 +534,10 @@ static unsigned numusearray (const Table *t, unsigned *nums) { } -static int numusehash (const Table *t, unsigned *nums, unsigned *pna) { - int totaluse = 0; /* total number of elements */ - int ause = 0; /* elements added to 'nums' (can go to array part) */ - int i = sizenode(t); +static unsigned numusehash (const Table *t, unsigned *nums, unsigned *pna) { + unsigned totaluse = 0; /* total number of elements */ + unsigned ause = 0; /* elements added to 'nums' (can go to array part) */ + unsigned i = sizenode(t); while (i--) { Node *n = &t->node[i]; if (!isempty(gval(n))) { @@ -646,8 +646,8 @@ static void setnodevector (lua_State *L, Table *t, unsigned size) { ** (Re)insert all elements from the hash part of 'ot' into table 't'. */ static void reinsert (lua_State *L, Table *ot, Table *t) { - int j; - int size = sizenode(ot); + unsigned j; + unsigned size = sizenode(ot); for (j = 0; j < size; j++) { Node *old = gnode(ot, j); if (!isempty(gval(old))) { @@ -673,10 +673,10 @@ static void exchangehashpart (Table *t1, Table *t2) { int bitdummy1 = t1->flags & BITDUMMY; t1->lsizenode = t2->lsizenode; t1->node = t2->node; - t1->flags = (t1->flags & NOTBITDUMMY) | (t2->flags & BITDUMMY); + t1->flags = cast_byte((t1->flags & NOTBITDUMMY) | (t2->flags & BITDUMMY)); t2->lsizenode = lsizenode; t2->node = node; - t2->flags = (t2->flags & NOTBITDUMMY) | bitdummy1; + t2->flags = cast_byte((t2->flags & NOTBITDUMMY) | bitdummy1); } @@ -689,11 +689,12 @@ static void reinsertOldSlice (lua_State *L, Table *t, unsigned oldasize, unsigned i; t->alimit = newasize; /* pretend array has new size... */ for (i = newasize; i < oldasize; i++) { /* traverse vanishing slice */ - int tag = *getArrTag(t, i); + lu_byte tag = *getArrTag(t, i); if (!tagisempty(tag)) { /* a non-empty entry? */ TValue aux; farr2val(t, i, tag, &aux); /* copy entry into 'aux' */ - luaH_setint(L, t, i + 1, &aux); /* re-insert it into the table */ + /* re-insert it into the table */ + luaH_setint(L, t, cast_int(i) + 1, &aux); } } t->alimit = oldasize; /* restore current size... */ @@ -756,7 +757,7 @@ void luaH_resize (lua_State *L, Table *t, unsigned newasize, void luaH_resizearray (lua_State *L, Table *t, unsigned int nasize) { - int nsize = allocsizenode(t); + unsigned nsize = allocsizenode(t); luaH_resize(L, t, nasize, nsize); } @@ -768,7 +769,7 @@ static void rehash (lua_State *L, Table *t, const TValue *ek) { unsigned int na; /* number of keys in the array part */ unsigned int nums[MAXABITS + 1]; int i; - int totaluse; + unsigned totaluse; for (i = 0; i <= MAXABITS; i++) nums[i] = 0; /* reset counts */ setlimittosize(t); na = numusearray(t, nums); /* count keys in array part */ @@ -795,7 +796,7 @@ Table *luaH_new (lua_State *L) { GCObject *o = luaC_newobj(L, LUA_VTABLE, sizeof(Table)); Table *t = gco2t(o); t->metatable = NULL; - t->flags = cast_byte(maskflags); /* table has no metamethod fields */ + t->flags = maskflags; /* table has no metamethod fields */ t->array = NULL; t->alimit = 0; setnodevector(L, t, 0); @@ -825,7 +826,7 @@ static Node *getfreepos (Table *t) { } else { /* no 'lastfree' information */ if (!isdummy(t)) { - int i = sizenode(t); + unsigned i = sizenode(t); while (i--) { /* do a linear search */ Node *free = gnode(t, i); if (keyisnil(free)) @@ -919,13 +920,13 @@ static const TValue *getintfromhash (Table *t, lua_Integer key) { } -static int hashkeyisempty (Table *t, lua_Integer key) { - const TValue *val = getintfromhash(t, key); +static int hashkeyisempty (Table *t, lua_Unsigned key) { + const TValue *val = getintfromhash(t, l_castU2S(key)); return isempty(val); } -static int finishnodeget (const TValue *val, TValue *res) { +static lu_byte finishnodeget (const TValue *val, TValue *res) { if (!ttisnil(val)) { setobj(((lua_State*)NULL), res, val); } @@ -933,9 +934,9 @@ static int finishnodeget (const TValue *val, TValue *res) { } -int luaH_getint (Table *t, lua_Integer key, TValue *res) { +lu_byte luaH_getint (Table *t, lua_Integer key, TValue *res) { if (keyinarray(t, key)) { - int tag = *getArrTag(t, key - 1); + lu_byte tag = *getArrTag(t, key - 1); if (!tagisempty(tag)) farr2val(t, key - 1, tag, res); return tag; @@ -964,7 +965,7 @@ const TValue *luaH_Hgetshortstr (Table *t, TString *key) { } -int luaH_getshortstr (Table *t, TString *key, TValue *res) { +lu_byte luaH_getshortstr (Table *t, TString *key, TValue *res) { return finishnodeget(luaH_Hgetshortstr(t, key), res); } @@ -980,7 +981,7 @@ static const TValue *Hgetstr (Table *t, TString *key) { } -int luaH_getstr (Table *t, TString *key, TValue *res) { +lu_byte luaH_getstr (Table *t, TString *key, TValue *res) { return finishnodeget(Hgetstr(t, key), res); } @@ -997,7 +998,7 @@ TString *luaH_getstrkey (Table *t, TString *key) { /* ** main search function */ -int luaH_get (Table *t, const TValue *key, TValue *res) { +lu_byte luaH_get (Table *t, const TValue *key, TValue *res) { const TValue *slot; switch (ttypetag(key)) { case LUA_VSHRSTR: @@ -1259,7 +1260,7 @@ lua_Unsigned luaH_getn (Table *t) { /* (3) 'limit' is the last element and either is zero or present in table */ lua_assert(limit == luaH_realasize(t) && (limit == 0 || !arraykeyisempty(t, limit))); - if (isdummy(t) || hashkeyisempty(t, cast(lua_Integer, limit + 1))) + if (isdummy(t) || hashkeyisempty(t, limit + 1)) return limit; /* 'limit + 1' is absent */ else /* 'limit + 1' is also present */ return hash_search(t, limit); diff --git a/ltable.h b/ltable.h index 2e7f86fd67..c6a87807af 100644 --- a/ltable.h +++ b/ltable.h @@ -20,7 +20,7 @@ ** may have any of these metamethods. (First access that fails after the ** clearing will set the bit again.) */ -#define invalidateTMcache(t) ((t)->flags &= ~maskflags) +#define invalidateTMcache(t) ((t)->flags &= cast_byte(~maskflags)) /* @@ -137,10 +137,10 @@ (*tag = (val)->tt_, *getArrVal(h,(k)) = (val)->value_) -LUAI_FUNC int luaH_get (Table *t, const TValue *key, TValue *res); -LUAI_FUNC int luaH_getshortstr (Table *t, TString *key, TValue *res); -LUAI_FUNC int luaH_getstr (Table *t, TString *key, TValue *res); -LUAI_FUNC int luaH_getint (Table *t, lua_Integer key, TValue *res); +LUAI_FUNC lu_byte luaH_get (Table *t, const TValue *key, TValue *res); +LUAI_FUNC lu_byte luaH_getshortstr (Table *t, TString *key, TValue *res); +LUAI_FUNC lu_byte luaH_getstr (Table *t, TString *key, TValue *res); +LUAI_FUNC lu_byte luaH_getint (Table *t, lua_Integer key, TValue *res); /* Special get for metamethods */ LUAI_FUNC const TValue *luaH_Hgetshortstr (Table *t, TString *key); diff --git a/ltablib.c b/ltablib.c index b59485911e..538d585dd9 100644 --- a/ltablib.c +++ b/ltablib.c @@ -192,7 +192,7 @@ static int tconcat (lua_State *L) { static int tpack (lua_State *L) { int i; int n = lua_gettop(L); /* number of elements to pack */ - lua_createtable(L, n, 1); /* create result table */ + lua_createtable(L, cast_uint(n), 1); /* create result table */ lua_insert(L, 1); /* put it at index 1 */ for (i = n; i >= 1; i--) /* assign elements */ lua_seti(L, 1, i); @@ -207,7 +207,7 @@ static int tunpack (lua_State *L) { lua_Integer i = luaL_optinteger(L, 2, 1); lua_Integer e = luaL_opt(L, luaL_checkinteger, 3, luaL_len(L, 1)); if (i > e) return 0; /* empty range */ - n = (lua_Unsigned)e - i; /* number of elements minus 1 (avoid overflows) */ + n = l_castS2U(e) - l_castS2U(i); /* number of elements minus 1 */ if (l_unlikely(n >= (unsigned int)INT_MAX || !lua_checkstack(L, (int)(++n)))) return luaL_error(L, "too many results to unpack"); diff --git a/ltests.c b/ltests.c index ad40801e24..8a6b4065bc 100644 --- a/ltests.c +++ b/ltests.c @@ -217,7 +217,7 @@ void *debug_realloc (void *ud, void *b, size_t oldsize, size_t size) { mc->memlimit = limit ? strtoul(limit, NULL, 10) : ULONG_MAX; } if (block == NULL) { - type = (oldsize < LUA_NUMTYPES) ? oldsize : 0; + type = (oldsize < LUA_NUMTYPES) ? cast_int(oldsize) : 0; oldsize = 0; } else { @@ -567,7 +567,7 @@ static l_obj checkgraylist (global_State *g, GCObject *o) { ** Check objects in gray lists. */ static l_obj checkgrays (global_State *g) { - int total = 0; /* count number of elements in all lists */ + l_obj total = 0; /* count number of elements in all lists */ if (!keepinvariant(g)) return total; total += checkgraylist(g, g->gray); total += checkgraylist(g, g->grayagain); @@ -778,7 +778,7 @@ static int listk (lua_State *L) { luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1), 1, "Lua function expected"); p = getproto(obj_at(L, 1)); - lua_createtable(L, p->sizek, 0); + lua_createtable(L, cast_uint(p->sizek), 0); for (i=0; isizek; i++) { pushobject(L, p->k+i); lua_rawseti(L, -2, i+1); @@ -794,7 +794,7 @@ static int listabslineinfo (lua_State *L) { 1, "Lua function expected"); p = getproto(obj_at(L, 1)); luaL_argcheck(L, p->abslineinfo != NULL, 1, "function has no debug info"); - lua_createtable(L, 2 * p->sizeabslineinfo, 0); + lua_createtable(L, 2u * cast_uint(p->sizeabslineinfo), 0); for (i=0; i < p->sizeabslineinfo; i++) { lua_pushinteger(L, p->abslineinfo[i].pc); lua_rawseti(L, -2, 2 * i + 1); @@ -847,9 +847,9 @@ static int get_limits (lua_State *L) { static int mem_query (lua_State *L) { if (lua_isnone(L, 1)) { - lua_pushinteger(L, l_memcontrol.total); - lua_pushinteger(L, l_memcontrol.numblocks); - lua_pushinteger(L, l_memcontrol.maxmem); + lua_pushinteger(L, cast(lua_Integer, l_memcontrol.total)); + lua_pushinteger(L, cast(lua_Integer, l_memcontrol.numblocks)); + lua_pushinteger(L, cast(lua_Integer, l_memcontrol.maxmem)); return 3; } else if (lua_isnumber(L, 1)) { @@ -863,7 +863,7 @@ static int mem_query (lua_State *L) { int i; for (i = LUA_NUMTYPES - 1; i >= 0; i--) { if (strcmp(t, ttypename(i)) == 0) { - lua_pushinteger(L, l_memcontrol.objcount[i]); + lua_pushinteger(L, cast(lua_Integer, l_memcontrol.objcount[i])); return 1; } } @@ -874,9 +874,9 @@ static int mem_query (lua_State *L) { static int alloc_count (lua_State *L) { if (lua_isnone(L, 1)) - l_memcontrol.countlimit = ~0L; + l_memcontrol.countlimit = cast(unsigned long, ~0L); else - l_memcontrol.countlimit = luaL_checkinteger(L, 1); + l_memcontrol.countlimit = cast(unsigned long, luaL_checkinteger(L, 1)); return 0; } @@ -975,26 +975,26 @@ static int gc_state (lua_State *L) { static int hash_query (lua_State *L) { if (lua_isnone(L, 2)) { luaL_argcheck(L, lua_type(L, 1) == LUA_TSTRING, 1, "string expected"); - lua_pushinteger(L, tsvalue(obj_at(L, 1))->hash); + lua_pushinteger(L, cast_int(tsvalue(obj_at(L, 1))->hash)); } else { TValue *o = obj_at(L, 1); Table *t; luaL_checktype(L, 2, LUA_TTABLE); t = hvalue(obj_at(L, 2)); - lua_pushinteger(L, luaH_mainposition(t, o) - t->node); + lua_pushinteger(L, cast(lua_Integer, luaH_mainposition(t, o) - t->node)); } return 1; } static int stacklevel (lua_State *L) { - unsigned long a = 0; - lua_pushinteger(L, (L->top.p - L->stack.p)); + int a = 0; + lua_pushinteger(L, cast(lua_Integer, L->top.p - L->stack.p)); lua_pushinteger(L, stacksize(L)); - lua_pushinteger(L, L->nCcalls); + lua_pushinteger(L, cast(lua_Integer, L->nCcalls)); lua_pushinteger(L, L->nci); - lua_pushinteger(L, (unsigned long)&a); + lua_pushinteger(L, (lua_Integer)(size_t)&a); return 5; } @@ -1007,9 +1007,9 @@ static int table_query (lua_State *L) { t = hvalue(obj_at(L, 1)); asize = luaH_realasize(t); if (i == -1) { - lua_pushinteger(L, asize); - lua_pushinteger(L, allocsizenode(t)); - lua_pushinteger(L, t->alimit); + lua_pushinteger(L, cast(lua_Integer, asize)); + lua_pushinteger(L, cast(lua_Integer, allocsizenode(t))); + lua_pushinteger(L, cast(lua_Integer, t->alimit)); return 3; } else if (cast_uint(i) < asize) { @@ -1018,7 +1018,7 @@ static int table_query (lua_State *L) { api_incr_top(L); lua_pushnil(L); } - else if ((i -= asize) < sizenode(t)) { + else if (cast_uint(i -= cast_int(asize)) < sizenode(t)) { TValue k; getnodekey(L, &k, gnode(t, i)); if (!isempty(gval(gnode(t, i))) || @@ -1054,7 +1054,7 @@ static int query_GCparams (lua_State *L) { static int test_codeparam (lua_State *L) { lua_Integer p = luaL_checkinteger(L, 1); - lua_pushinteger(L, luaO_codeparam(p)); + lua_pushinteger(L, luaO_codeparam(cast_uint(p))); return 1; } @@ -1062,7 +1062,7 @@ static int test_codeparam (lua_State *L) { static int test_applyparam (lua_State *L) { lua_Integer p = luaL_checkinteger(L, 1); lua_Integer x = luaL_checkinteger(L, 2); - lua_pushinteger(L, luaO_applyparam(p, x)); + lua_pushinteger(L, luaO_applyparam(cast_byte(p), x)); return 1; } @@ -1147,7 +1147,7 @@ static int upvalue (lua_State *L) { static int newuserdata (lua_State *L) { size_t size = cast_sizet(luaL_optinteger(L, 1, 0)); - int nuv = luaL_optinteger(L, 2, 0); + int nuv = cast_int(luaL_optinteger(L, 2, 0)); char *p = cast_charp(lua_newuserdatauv(L, size, nuv)); while (size--) *p++ = '\0'; return 1; @@ -1227,8 +1227,8 @@ static lua_State *getstate (lua_State *L) { static int loadlib (lua_State *L) { lua_State *L1 = getstate(L); - int load = luaL_checkinteger(L, 2); - int preload = luaL_checkinteger(L, 3); + int load = cast_int(luaL_checkinteger(L, 2)); + int preload = cast_int(luaL_checkinteger(L, 3)); luaL_openselectedlibs(L1, load, preload); luaL_requiref(L1, "T", luaB_opentests, 0); lua_assert(lua_type(L1, -1) == LUA_TTABLE); @@ -1490,13 +1490,13 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) { } else if EQ("append") { int t = getindex; - int i = lua_rawlen(L1, t); + int i = cast_int(lua_rawlen(L1, t)); lua_rawseti(L1, t, i + 1); } else if EQ("arith") { int op; skip(&pc); - op = strchr(ops, *pc++) - ops; + op = cast_int(strchr(ops, *pc++) - ops); lua_arith(L1, op); } else if EQ("call") { @@ -1538,7 +1538,7 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) { } else if EQ("func2num") { lua_CFunction func = lua_tocfunction(L1, getindex); - lua_pushnumber(L1, cast_sizet(func)); + lua_pushinteger(L1, cast(lua_Integer, func)); } else if EQ("getfield") { int t = getindex; @@ -1624,13 +1624,13 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) { lua_pushinteger(L1, lua_resetthread(L1)); /* deprecated */ } else if EQ("newuserdata") { - lua_newuserdata(L1, getnum); + lua_newuserdata(L1, cast_sizet(getnum)); } else if EQ("next") { lua_next(L1, -2); } else if EQ("objsize") { - lua_pushinteger(L1, lua_rawlen(L1, getindex)); + lua_pushinteger(L1, l_castU2S(lua_rawlen(L1, getindex))); } else if EQ("pcall") { int narg = getnum; @@ -1903,7 +1903,7 @@ static int Cfunck (lua_State *L, int status, lua_KContext ctx) { lua_setglobal(L, "status"); lua_pushinteger(L, ctx); lua_setglobal(L, "ctx"); - return runC(L, L, lua_tostring(L, ctx)); + return runC(L, L, lua_tostring(L, cast_int(ctx))); } diff --git a/ltm.c b/ltm.c index 236f3bb483..8eca2d6e1f 100644 --- a/ltm.c +++ b/ltm.c @@ -116,8 +116,8 @@ void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1, } -int luaT_callTMres (lua_State *L, const TValue *f, const TValue *p1, - const TValue *p2, StkId res) { +lu_byte luaT_callTMres (lua_State *L, const TValue *f, const TValue *p1, + const TValue *p2, StkId res) { ptrdiff_t result = savestack(L, res); StkId func = L->top.p; setobj2s(L, func, f); /* push function (assume EXTRA_STACK) */ diff --git a/ltm.h b/ltm.h index df05b741f7..ba2e47606e 100644 --- a/ltm.h +++ b/ltm.h @@ -51,7 +51,7 @@ typedef enum { ** corresponding metamethod field. (Bit 6 of the flag indicates that ** the table is using the dummy node; bit 7 is used for 'isrealasize'.) */ -#define maskflags (~(~0u << (TM_EQ + 1))) +#define maskflags cast_byte(~(~0u << (TM_EQ + 1))) /* @@ -81,8 +81,8 @@ LUAI_FUNC void luaT_init (lua_State *L); LUAI_FUNC void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1, const TValue *p2, const TValue *p3); -LUAI_FUNC int luaT_callTMres (lua_State *L, const TValue *f, - const TValue *p1, const TValue *p2, StkId p3); +LUAI_FUNC lu_byte luaT_callTMres (lua_State *L, const TValue *f, + const TValue *p1, const TValue *p2, StkId p3); LUAI_FUNC void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2, StkId res, TMS event); LUAI_FUNC void luaT_tryconcatTM (lua_State *L); diff --git a/lua.c b/lua.c index 3d807c98c3..9693ad6800 100644 --- a/lua.c +++ b/lua.c @@ -185,7 +185,7 @@ static void print_version (void) { static void createargtable (lua_State *L, char **argv, int argc, int script) { int i, narg; narg = argc - (script + 1); /* number of positive indices */ - lua_createtable(L, narg, script + 1); + lua_createtable(L, cast_uint(narg), cast_uint(script + 1)); for (i = 0; i < argc; i++) { lua_pushstring(L, argv[i]); lua_rawseti(L, -2, i - script); diff --git a/lundump.c b/lundump.c index b5dbaec98a..4d6e8bd2fd 100644 --- a/lundump.c +++ b/lundump.c @@ -36,7 +36,7 @@ typedef struct { ZIO *Z; const char *name; Table *h; /* list for string reuse */ - lu_mem offset; /* current position relative to beginning of dump */ + size_t offset; /* current position relative to beginning of dump */ lua_Integer nstr; /* number of strings in the list */ lu_byte fixed; /* dump is fixed in memory */ } LoadState; @@ -61,8 +61,8 @@ static void loadBlock (LoadState *S, void *b, size_t size) { } -static void loadAlign (LoadState *S, int align) { - int padding = align - (S->offset % align); +static void loadAlign (LoadState *S, unsigned align) { + unsigned padding = align - cast_uint(S->offset % align); if (padding < align) { /* apd == align means no padding */ lua_Integer paddingContent; loadBlock(S, &paddingContent, padding); @@ -113,11 +113,19 @@ static size_t loadSize (LoadState *S) { } +/* +** Read an non-negative int */ +static unsigned loadUint (LoadState *S) { + return cast_uint(loadVarint(S, cast_sizet(INT_MAX))); +} + + static int loadInt (LoadState *S) { return cast_int(loadVarint(S, cast_sizet(INT_MAX))); } + static lua_Number loadNumber (LoadState *S) { lua_Number x; loadVar(S, x); @@ -180,15 +188,15 @@ static void loadString (LoadState *S, Proto *p, TString **sl) { static void loadCode (LoadState *S, Proto *f) { - int n = loadInt(S); + unsigned n = loadUint(S); loadAlign(S, sizeof(f->code[0])); if (S->fixed) { f->code = getaddr(S, n, Instruction); - f->sizecode = n; + f->sizecode = cast_int(n); } else { f->code = luaM_newvectorchecked(S->L, n, Instruction); - f->sizecode = n; + f->sizecode = cast_int(n); loadVector(S, f->code, n); } } @@ -198,10 +206,10 @@ static void loadFunction(LoadState *S, Proto *f); static void loadConstants (LoadState *S, Proto *f) { - int i; - int n = loadInt(S); + unsigned i; + unsigned n = loadUint(S); f->k = luaM_newvectorchecked(S->L, n, TValue); - f->sizek = n; + f->sizek = cast_int(n); for (i = 0; i < n; i++) setnilvalue(&f->k[i]); for (i = 0; i < n; i++) { @@ -240,10 +248,10 @@ static void loadConstants (LoadState *S, Proto *f) { static void loadProtos (LoadState *S, Proto *f) { - int i; - int n = loadInt(S); + unsigned i; + unsigned n = loadUint(S); f->p = luaM_newvectorchecked(S->L, n, Proto *); - f->sizep = n; + f->sizep = cast_int(n); for (i = 0; i < n; i++) f->p[i] = NULL; for (i = 0; i < n; i++) { @@ -261,10 +269,10 @@ static void loadProtos (LoadState *S, Proto *f) { ** in that case all prototypes must be consistent for the GC. */ static void loadUpvalues (LoadState *S, Proto *f) { - int i, n; - n = loadInt(S); + unsigned i; + unsigned n = loadUint(S); f->upvalues = luaM_newvectorchecked(S->L, n, Upvaldesc); - f->sizeupvalues = n; + f->sizeupvalues = cast_int(n); for (i = 0; i < n; i++) /* make array valid for GC */ f->upvalues[i].name = NULL; for (i = 0; i < n; i++) { /* following calls can raise errors */ @@ -276,33 +284,33 @@ static void loadUpvalues (LoadState *S, Proto *f) { static void loadDebug (LoadState *S, Proto *f) { - int i, n; - n = loadInt(S); + unsigned i; + unsigned n = loadUint(S); if (S->fixed) { f->lineinfo = getaddr(S, n, ls_byte); - f->sizelineinfo = n; + f->sizelineinfo = cast_int(n); } else { f->lineinfo = luaM_newvectorchecked(S->L, n, ls_byte); - f->sizelineinfo = n; + f->sizelineinfo = cast_int(n); loadVector(S, f->lineinfo, n); } - n = loadInt(S); + n = loadUint(S); if (n > 0) { loadAlign(S, sizeof(int)); if (S->fixed) { f->abslineinfo = getaddr(S, n, AbsLineInfo); - f->sizeabslineinfo = n; + f->sizeabslineinfo = cast_int(n); } else { f->abslineinfo = luaM_newvectorchecked(S->L, n, AbsLineInfo); - f->sizeabslineinfo = n; + f->sizeabslineinfo = cast_int(n); loadVector(S, f->abslineinfo, n); } } - n = loadInt(S); + n = loadUint(S); f->locvars = luaM_newvectorchecked(S->L, n, LocVar); - f->sizelocvars = n; + f->sizelocvars = cast_int(n); for (i = 0; i < n; i++) f->locvars[i].varname = NULL; for (i = 0; i < n; i++) { @@ -310,9 +318,9 @@ static void loadDebug (LoadState *S, Proto *f) { f->locvars[i].startpc = loadInt(S); f->locvars[i].endpc = loadInt(S); } - n = loadInt(S); + n = loadUint(S); if (n != 0) /* does it have debug information? */ - n = f->sizeupvalues; /* must be this many */ + n = cast_uint(f->sizeupvalues); /* must be this many */ for (i = 0; i < n; i++) loadString(S, f, &f->upvalues[i].name); } @@ -384,7 +392,7 @@ LClosure *luaU_undump (lua_State *L, ZIO *Z, const char *name, int fixed) { S.name = name; S.L = L; S.Z = Z; - S.fixed = fixed; + S.fixed = cast_byte(fixed); S.offset = 1; /* fist byte was already read */ checkHeader(&S); cl = luaF_newLclosure(L, loadByte(&S)); diff --git a/lutf8lib.c b/lutf8lib.c index 243196c8ca..6dfdd1f47d 100644 --- a/lutf8lib.c +++ b/lutf8lib.c @@ -180,7 +180,7 @@ static int byteoffset (lua_State *L) { size_t len; const char *s = luaL_checklstring(L, 1, &len); lua_Integer n = luaL_checkinteger(L, 2); - lua_Integer posi = (n >= 0) ? 1 : len + 1; + lua_Integer posi = (n >= 0) ? 1 : cast_st2S(len) + 1; posi = u_posrelat(luaL_optinteger(L, 3, posi), len); luaL_argcheck(L, 1 <= posi && --posi <= (lua_Integer)len, 3, "position out of bounds"); @@ -239,7 +239,7 @@ static int iter_aux (lua_State *L, int strict) { const char *next = utf8_decode(s + n, &code, strict); if (next == NULL || iscontp(next)) return luaL_error(L, MSGInvalid); - lua_pushinteger(L, n + 1); + lua_pushinteger(L, l_castU2S(n + 1)); lua_pushinteger(L, code); return 2; } diff --git a/lvm.c b/lvm.c index 5771c31a82..33da560955 100644 --- a/lvm.c +++ b/lvm.c @@ -288,8 +288,8 @@ static int floatforloop (StkId ra) { /* ** Finish the table access 'val = t[key]' and return the tag of the result. */ -int luaV_finishget (lua_State *L, const TValue *t, TValue *key, StkId val, - int tag) { +lu_byte luaV_finishget (lua_State *L, const TValue *t, TValue *key, + StkId val, lu_byte tag) { int loop; /* counter to avoid infinite loops */ const TValue *tm; /* metamethod */ for (loop = 0; loop < MAXTAGLOOP; loop++) { @@ -690,7 +690,7 @@ void luaV_objlen (lua_State *L, StkId ra, const TValue *rb) { Table *h = hvalue(rb); tm = fasttm(L, h->metatable, TM_LEN); if (tm) break; /* metamethod? break switch to call it */ - setivalue(s2v(ra), luaH_getn(h)); /* else primitive len */ + setivalue(s2v(ra), l_castU2S(luaH_getn(h))); /* else primitive len */ return; } case LUA_VSHRSTR: { @@ -698,7 +698,7 @@ void luaV_objlen (lua_State *L, StkId ra, const TValue *rb) { return; } case LUA_VLNGSTR: { - setivalue(s2v(ra), tsvalue(rb)->u.lnglen); + setivalue(s2v(ra), cast_st2S(tsvalue(rb)->u.lnglen)); return; } default: { /* try metamethod */ @@ -1255,7 +1255,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { TValue *upval = cl->upvals[GETARG_B(i)]->v.p; TValue *rc = KC(i); TString *key = tsvalue(rc); /* key must be a short string */ - int tag; + lu_byte tag; luaV_fastget(upval, key, s2v(ra), luaH_getshortstr, tag); if (tagisempty(tag)) Protect(luaV_finishget(L, upval, rc, ra, tag)); @@ -1265,7 +1265,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { StkId ra = RA(i); TValue *rb = vRB(i); TValue *rc = vRC(i); - int tag; + lu_byte tag; if (ttisinteger(rc)) { /* fast track for integers? */ luaV_fastgeti(rb, ivalue(rc), s2v(ra), tag); } @@ -1279,7 +1279,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { StkId ra = RA(i); TValue *rb = vRB(i); int c = GETARG_C(i); - int tag; + lu_byte tag; luaV_fastgeti(rb, c, s2v(ra), tag); if (tagisempty(tag)) { TValue key; @@ -1293,7 +1293,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { TValue *rb = vRB(i); TValue *rc = KC(i); TString *key = tsvalue(rc); /* key must be a short string */ - int tag; + lu_byte tag; luaV_fastget(rb, key, s2v(ra), luaH_getshortstr, tag); if (tagisempty(tag)) Protect(luaV_finishget(L, rb, rc, ra, tag)); @@ -1359,14 +1359,15 @@ void luaV_execute (lua_State *L, CallInfo *ci) { } vmcase(OP_NEWTABLE) { StkId ra = RA(i); - int b = GETARG_vB(i); /* log2(hash size) + 1 */ - int c = GETARG_vC(i); /* array size */ + unsigned b = cast_uint(GETARG_vB(i)); /* log2(hash size) + 1 */ + unsigned c = cast_uint(GETARG_vC(i)); /* array size */ Table *t; if (b > 0) - b = 1 << (b - 1); /* hash size is 2^(b - 1) */ + b = 1u << (b - 1); /* hash size is 2^(b - 1) */ if (TESTARG_k(i)) { /* non-zero extra argument? */ lua_assert(GETARG_Ax(*pc) != 0); - c += GETARG_Ax(*pc) * (MAXARG_vC + 1); /* add it to array size */ + /* add it to array size */ + c += cast_uint(GETARG_Ax(*pc)) * (MAXARG_vC + 1); } pc++; /* skip extra argument */ L->top.p = ra + 1; /* correct top in case of emergency GC */ @@ -1379,7 +1380,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { } vmcase(OP_SELF) { StkId ra = RA(i); - int tag; + lu_byte tag; TValue *rb = vRB(i); TValue *rc = RKC(i); TString *key = tsvalue(rc); /* key must be a string */ @@ -1786,7 +1787,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { if (count > 0) { /* still more iterations? */ lua_Integer step = ivalue(s2v(ra + 1)); lua_Integer idx = ivalue(s2v(ra + 2)); /* control variable */ - chgivalue(s2v(ra), count - 1); /* update counter */ + chgivalue(s2v(ra), l_castU2S(count - 1)); /* update counter */ idx = intop(+, idx, step); /* add step to index */ chgivalue(s2v(ra + 2), idx); /* update control variable */ pc -= GETARG_Bx(i); /* jump back */ @@ -1851,16 +1852,16 @@ void luaV_execute (lua_State *L, CallInfo *ci) { }} vmcase(OP_SETLIST) { StkId ra = RA(i); - int n = GETARG_vB(i); - unsigned int last = GETARG_vC(i); + unsigned n = cast_uint(GETARG_vB(i)); + unsigned int last = cast_uint(GETARG_vC(i)); Table *h = hvalue(s2v(ra)); if (n == 0) - n = cast_int(L->top.p - ra) - 1; /* get up to the top */ + n = cast_uint(L->top.p - ra) - 1; /* get up to the top */ else L->top.p = ci->top.p; /* correct top in case of emergency GC */ last += n; if (TESTARG_k(i)) { - last += GETARG_Ax(*pc) * (MAXARG_vC + 1); + last += cast_uint(GETARG_Ax(*pc)) * (MAXARG_vC + 1); pc++; } /* when 'n' is known, table should have proper size */ diff --git a/lvm.h b/lvm.h index a11db83c2a..c88985599a 100644 --- a/lvm.h +++ b/lvm.h @@ -120,8 +120,8 @@ LUAI_FUNC int luaV_tointeger (const TValue *obj, lua_Integer *p, F2Imod mode); LUAI_FUNC int luaV_tointegerns (const TValue *obj, lua_Integer *p, F2Imod mode); LUAI_FUNC int luaV_flttointeger (lua_Number n, lua_Integer *p, F2Imod mode); -LUAI_FUNC int luaV_finishget (lua_State *L, const TValue *t, TValue *key, - StkId val, int tag); +LUAI_FUNC lu_byte luaV_finishget (lua_State *L, const TValue *t, TValue *key, + StkId val, lu_byte tag); LUAI_FUNC void luaV_finishset (lua_State *L, const TValue *t, TValue *key, TValue *val, int aux); LUAI_FUNC void luaV_finishOp (lua_State *L); diff --git a/lzio.h b/lzio.h index 55cc74adec..49047c98cb 100644 --- a/lzio.h +++ b/lzio.h @@ -32,7 +32,7 @@ typedef struct Mbuffer { #define luaZ_sizebuffer(buff) ((buff)->buffsize) #define luaZ_bufflen(buff) ((buff)->n) -#define luaZ_buffremove(buff,i) ((buff)->n -= (i)) +#define luaZ_buffremove(buff,i) ((buff)->n -= cast_sizet(i)) #define luaZ_resetbuffer(buff) ((buff)->n = 0) diff --git a/makefile b/makefile index 58b12f8e14..b37fdb28fe 100644 --- a/makefile +++ b/makefile @@ -14,11 +14,11 @@ CWARNSCPP= \ -Wdisabled-optimization \ -Wdouble-promotion \ -Wmissing-declarations \ + -Wconversion \ # the next warnings might be useful sometimes, # but usually they generate too much noise # -Werror \ # -pedantic # warns if we use jump tables \ - # -Wconversion \ # -Wsign-conversion \ # -Wstrict-overflow=2 \ # -Wformat=2 \ diff --git a/manual/manual.of b/manual/manual.of index c7f6904ad0..93e3a114e6 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -3995,7 +3995,7 @@ The conversion specifiers can only be @Char{%p} (inserts a pointer), @Char{%d} (inserts an @T{int}), @Char{%c} (inserts an @T{int} as a one-byte character), and -@Char{%U} (inserts a @T{long int} as a @x{UTF-8} byte sequence). +@Char{%U} (inserts an @T{unsigned long} as a @x{UTF-8} byte sequence). This function may raise errors due to memory overflow or an invalid conversion specifier. From f2206b2abe848f65956fa48da338c2bfac599e4a Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Sat, 27 Jul 2024 15:13:21 -0300 Subject: [PATCH 0936/1145] '-Wconversion' extended to all options of Lua numbers --- llimits.h | 4 +++- lmem.c | 6 +++--- lmem.h | 2 +- lstrlib.c | 9 +++++---- ltablib.c | 32 ++++++++++++++++++++------------ ltests.c | 28 ++++++++++++++-------------- lutf8lib.c | 6 +++--- testes/strings.lua | 2 +- 8 files changed, 50 insertions(+), 39 deletions(-) diff --git a/llimits.h b/llimits.h index e7da009b16..d7ae065b17 100644 --- a/llimits.h +++ b/llimits.h @@ -163,13 +163,15 @@ typedef LUAI_UACINT l_uacInt; */ #define ct_diff2sz(df) ((size_t)(df)) +/* ptrdiff_t to lua_Integer */ +#define ct_diff2S(df) cast_st2S(ct_diff2sz(df)) + /* ** Special type equivalent to '(void*)' for functions (to suppress some ** warnings when converting function pointers) */ typedef void (*voidf)(void); - /* ** Macro to convert pointer-to-void* to pointer-to-function. This cast ** is undefined according to ISO C, but POSIX assumes that it works. diff --git a/lmem.c b/lmem.c index d02c9fdc98..52a4f99927 100644 --- a/lmem.c +++ b/lmem.c @@ -126,10 +126,10 @@ void *luaM_growaux_ (lua_State *L, void *block, int nelems, int *psize, ** error. */ void *luaM_shrinkvector_ (lua_State *L, void *block, int *size, - int final_n, int size_elem) { + int final_n, unsigned size_elem) { void *newblock; - size_t oldsize = cast_sizet((*size) * size_elem); - size_t newsize = cast_sizet(final_n * size_elem); + size_t oldsize = cast_sizet(*size) * size_elem; + size_t newsize = cast_sizet(final_n) * size_elem; lua_assert(newsize <= oldsize); newblock = luaM_saferealloc_(L, block, oldsize, newsize); *size = final_n; diff --git a/lmem.h b/lmem.h index aa30610518..204ce3bcae 100644 --- a/lmem.h +++ b/lmem.h @@ -88,7 +88,7 @@ LUAI_FUNC void *luaM_growaux_ (lua_State *L, void *block, int nelems, int *size, unsigned size_elem, int limit, const char *what); LUAI_FUNC void *luaM_shrinkvector_ (lua_State *L, void *block, int *nelem, - int final_n, int size_elem); + int final_n, unsigned size_elem); LUAI_FUNC void *luaM_malloc_ (lua_State *L, size_t size, int tag); #endif diff --git a/lstrlib.c b/lstrlib.c index e9421c279b..321d6a0b0a 100644 --- a/lstrlib.c +++ b/lstrlib.c @@ -704,7 +704,8 @@ static ptrdiff_t get_onecapture (MatchState *ms, int i, const char *s, if (l_unlikely(capl == CAP_UNFINISHED)) luaL_error(ms->L, "unfinished capture"); else if (capl == CAP_POSITION) - lua_pushinteger(ms->L, (ms->capture[i].init - ms->src_init) + 1); + lua_pushinteger(ms->L, + ct_diff2S(ms->capture[i].init - ms->src_init) + 1); return capl; } } @@ -775,7 +776,7 @@ static int str_find_aux (lua_State *L, int find) { /* do a plain search */ const char *s2 = lmemfind(s + init, ls - init, p, lp); if (s2) { - lua_pushinteger(L, (s2 - s) + 1); + lua_pushinteger(L, ct_diff2S(s2 - s) + 1); lua_pushinteger(L, cast_st2S(ct_diff2sz(s2 - s) + lp)); return 2; } @@ -793,8 +794,8 @@ static int str_find_aux (lua_State *L, int find) { reprepstate(&ms); if ((res=match(&ms, s1, p)) != NULL) { if (find) { - lua_pushinteger(L, (s1 - s) + 1); /* start */ - lua_pushinteger(L, res - s); /* end */ + lua_pushinteger(L, ct_diff2S(s1 - s) + 1); /* start */ + lua_pushinteger(L, ct_diff2S(res - s)); /* end */ return push_captures(&ms, NULL, 0) + 2; } else diff --git a/ltablib.c b/ltablib.c index 538d585dd9..4db3768a52 100644 --- a/ltablib.c +++ b/ltablib.c @@ -231,10 +231,18 @@ static int tunpack (lua_State *L) { */ -/* type for array indices */ +/* +** Type for array indices. These indices are always limited by INT_MAX, +** so it is safe to cast them to lua_Integer even for Lua 32 bits. +*/ typedef unsigned int IdxT; +/* Versions of lua_seti/lua_geti specialized for IdxT */ +#define geti(L,idt,idx) lua_geti(L, idt, l_castU2S(idx)) +#define seti(L,idt,idx) lua_seti(L, idt, l_castU2S(idx)) + + /* ** Produce a "random" 'unsigned int' to randomize pivot choice. This ** macro is used only when 'sort' detects a big imbalance in the result @@ -251,8 +259,8 @@ typedef unsigned int IdxT; static void set2 (lua_State *L, IdxT i, IdxT j) { - lua_seti(L, 1, i); - lua_seti(L, 1, j); + seti(L, 1, i); + seti(L, 1, j); } @@ -289,14 +297,14 @@ static IdxT partition (lua_State *L, IdxT lo, IdxT up) { /* loop invariant: a[lo .. i] <= P <= a[j .. up] */ for (;;) { /* next loop: repeat ++i while a[i] < P */ - while ((void)lua_geti(L, 1, ++i), sort_comp(L, -1, -2)) { + while ((void)geti(L, 1, ++i), sort_comp(L, -1, -2)) { if (l_unlikely(i == up - 1)) /* a[i] < P but a[up - 1] == P ?? */ luaL_error(L, "invalid order function for sorting"); lua_pop(L, 1); /* remove a[i] */ } /* after the loop, a[i] >= P and a[lo .. i - 1] < P */ /* next loop: repeat --j while P < a[j] */ - while ((void)lua_geti(L, 1, --j), sort_comp(L, -3, -1)) { + while ((void)geti(L, 1, --j), sort_comp(L, -3, -1)) { if (l_unlikely(j < i)) /* j < i but a[j] > P ?? */ luaL_error(L, "invalid order function for sorting"); lua_pop(L, 1); /* remove a[j] */ @@ -335,8 +343,8 @@ static void auxsort (lua_State *L, IdxT lo, IdxT up, unsigned rnd) { IdxT p; /* Pivot index */ IdxT n; /* to be used later */ /* sort elements 'lo', 'p', and 'up' */ - lua_geti(L, 1, lo); - lua_geti(L, 1, up); + geti(L, 1, lo); + geti(L, 1, up); if (sort_comp(L, -1, -2)) /* a[up] < a[lo]? */ set2(L, lo, up); /* swap a[lo] - a[up] */ else @@ -347,13 +355,13 @@ static void auxsort (lua_State *L, IdxT lo, IdxT up, unsigned rnd) { p = (lo + up)/2; /* middle element is a good pivot */ else /* for larger intervals, it is worth a random pivot */ p = choosePivot(lo, up, rnd); - lua_geti(L, 1, p); - lua_geti(L, 1, lo); + geti(L, 1, p); + geti(L, 1, lo); if (sort_comp(L, -2, -1)) /* a[p] < a[lo]? */ set2(L, p, lo); /* swap a[p] - a[lo] */ else { lua_pop(L, 1); /* remove a[lo] */ - lua_geti(L, 1, up); + geti(L, 1, up); if (sort_comp(L, -1, -2)) /* a[up] < a[p]? */ set2(L, p, up); /* swap a[up] - a[p] */ else @@ -361,9 +369,9 @@ static void auxsort (lua_State *L, IdxT lo, IdxT up, unsigned rnd) { } if (up - lo == 2) /* only 3 elements? */ return; /* already sorted */ - lua_geti(L, 1, p); /* get middle element (Pivot) */ + geti(L, 1, p); /* get middle element (Pivot) */ lua_pushvalue(L, -1); /* push Pivot */ - lua_geti(L, 1, up - 1); /* push a[up - 1] */ + geti(L, 1, up - 1); /* push a[up - 1] */ set2(L, p, up - 1); /* swap Pivot (a[p]) with a[up - 1] */ p = partition(L, lo, up); /* a[lo .. p - 1] <= a[p] == P <= a[p + 1 .. up] */ diff --git a/ltests.c b/ltests.c index 8a6b4065bc..7d134e2da3 100644 --- a/ltests.c +++ b/ltests.c @@ -1040,14 +1040,14 @@ static int table_query (lua_State *L) { static int query_GCparams (lua_State *L) { global_State *g = G(L); - lua_pushinteger(L, gettotalobjs(g)); - lua_pushinteger(L, g->GCdebt); - lua_pushinteger(L, applygcparam(g, MINORMUL, 100)); - lua_pushinteger(L, applygcparam(g, MAJORMINOR, 100)); - lua_pushinteger(L, applygcparam(g, MINORMAJOR, 100)); - lua_pushinteger(L, applygcparam(g, PAUSE, 100)); - lua_pushinteger(L, applygcparam(g, STEPMUL, 100)); - lua_pushinteger(L, applygcparam(g, STEPSIZE, 100)); + lua_pushinteger(L, cast(lua_Integer, gettotalobjs(g))); + lua_pushinteger(L, cast(lua_Integer, g->GCdebt)); + lua_pushinteger(L, cast(lua_Integer, applygcparam(g, MINORMUL, 100))); + lua_pushinteger(L, cast(lua_Integer, applygcparam(g, MAJORMINOR, 100))); + lua_pushinteger(L, cast(lua_Integer, applygcparam(g, MINORMAJOR, 100))); + lua_pushinteger(L, cast(lua_Integer, applygcparam(g, PAUSE, 100))); + lua_pushinteger(L, cast(lua_Integer, applygcparam(g, STEPMUL, 100))); + lua_pushinteger(L, cast(lua_Integer, applygcparam(g, STEPSIZE, 100))); return 8; } @@ -1062,7 +1062,7 @@ static int test_codeparam (lua_State *L) { static int test_applyparam (lua_State *L) { lua_Integer p = luaL_checkinteger(L, 1); lua_Integer x = luaL_checkinteger(L, 2); - lua_pushinteger(L, luaO_applyparam(cast_byte(p), x)); + lua_pushinteger(L, cast(lua_Integer, luaO_applyparam(cast_byte(p), x))); return 1; } @@ -1162,7 +1162,7 @@ static int pushuserdata (lua_State *L) { static int udataval (lua_State *L) { - lua_pushinteger(L, cast(long, lua_touserdata(L, 1))); + lua_pushinteger(L, cast(lua_Integer, cast(size_t, lua_touserdata(L, 1)))); return 1; } @@ -1199,7 +1199,7 @@ static int num2int (lua_State *L) { static int makeseed (lua_State *L) { - lua_pushinteger(L, luaL_makeseed(L)); + lua_pushinteger(L, cast(lua_Integer, luaL_makeseed(L))); return 1; } @@ -1486,7 +1486,7 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) { const char *inst = getstring; if EQ("") return 0; else if EQ("absindex") { - lua_pushnumber(L1, lua_absindex(L1, getindex)); + lua_pushinteger(L1, lua_absindex(L1, getindex)); } else if EQ("append") { int t = getindex; @@ -1538,7 +1538,7 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) { } else if EQ("func2num") { lua_CFunction func = lua_tocfunction(L1, getindex); - lua_pushinteger(L1, cast(lua_Integer, func)); + lua_pushinteger(L1, cast(lua_Integer, cast(size_t, func))); } else if EQ("getfield") { int t = getindex; @@ -1901,7 +1901,7 @@ static int Cfunc (lua_State *L) { static int Cfunck (lua_State *L, int status, lua_KContext ctx) { lua_pushstring(L, statcodes[status]); lua_setglobal(L, "status"); - lua_pushinteger(L, ctx); + lua_pushinteger(L, cast(lua_Integer, ctx)); lua_setglobal(L, "ctx"); return runC(L, L, lua_tostring(L, cast_int(ctx))); } diff --git a/lutf8lib.c b/lutf8lib.c index 6dfdd1f47d..04bbfa567b 100644 --- a/lutf8lib.c +++ b/lutf8lib.c @@ -103,7 +103,7 @@ static int utflen (lua_State *L) { lua_pushinteger(L, posi + 1); /* ... and current position */ return 2; } - posi = s1 - s; + posi = ct_diff2S(s1 - s); n++; } lua_pushinteger(L, n); @@ -137,7 +137,7 @@ static int codepoint (lua_State *L) { s = utf8_decode(s, &code, !lax); if (s == NULL) return luaL_error(L, MSGInvalid); - lua_pushinteger(L, code); + lua_pushinteger(L, l_castU2S(code)); n++; } return n; @@ -240,7 +240,7 @@ static int iter_aux (lua_State *L, int strict) { if (next == NULL || iscontp(next)) return luaL_error(L, MSGInvalid); lua_pushinteger(L, l_castU2S(n + 1)); - lua_pushinteger(L, code); + lua_pushinteger(L, l_castU2S(code)); return 2; } } diff --git a/testes/strings.lua b/testes/strings.lua index a0204309c8..9bb52b35dd 100644 --- a/testes/strings.lua +++ b/testes/strings.lua @@ -111,7 +111,7 @@ assert(string.rep('', 10) == '') do checkerror("too large", string.rep, 'aa', math.maxinteger); - checkerror("too large", string.rep, 'a', math.maxinteger/2, ',') + checkerror("too large", string.rep, 'a', math.maxinteger, ',') end -- repetitions with separator From 4c6afbcb01d1cae72d829af5301df5f592fa2079 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 30 Jul 2024 10:16:19 -0300 Subject: [PATCH 0937/1145] Struct 'transferinfo' moved to "lua_State" That reduces the size of "CallInfo". Moreover, bit CIST_HOOKED from call status is not needed. When in a hook, 'transferinfo' is always valid, being zero when the hook is not call/return. --- ldebug.c | 6 +++--- ldo.c | 14 +++++--------- lstate.h | 20 ++++++++------------ 3 files changed, 16 insertions(+), 24 deletions(-) diff --git a/ldebug.c b/ldebug.c index a3a536bba3..9e341f1164 100644 --- a/ldebug.c +++ b/ldebug.c @@ -364,11 +364,11 @@ static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar, break; } case 'r': { - if (ci == NULL || !(ci->callstatus & CIST_TRAN)) + if (ci == NULL || !(ci->callstatus & CIST_HOOKED)) ar->ftransfer = ar->ntransfer = 0; else { - ar->ftransfer = ci->u2.transferinfo.ftransfer; - ar->ntransfer = ci->u2.transferinfo.ntransfer; + ar->ftransfer = L->transferinfo.ftransfer; + ar->ntransfer = L->transferinfo.ntransfer; } break; } diff --git a/ldo.c b/ldo.c index 1d1b7a7150..d63c82675b 100644 --- a/ldo.c +++ b/ldo.c @@ -357,7 +357,6 @@ void luaD_hook (lua_State *L, int event, int line, int ftransfer, int ntransfer) { lua_Hook hook = L->hook; if (hook && L->allowhook) { /* make sure there is a hook */ - unsigned mask = CIST_HOOKED; CallInfo *ci = L->ci; ptrdiff_t top = savestack(L, L->top.p); /* preserve original 'top' */ ptrdiff_t ci_top = savestack(L, ci->top.p); /* idem for 'ci->top' */ @@ -365,18 +364,15 @@ void luaD_hook (lua_State *L, int event, int line, ar.event = event; ar.currentline = line; ar.i_ci = ci; - if (ntransfer != 0) { - mask |= CIST_TRAN; /* 'ci' has transfer information */ - ci->u2.transferinfo.ftransfer = ftransfer; - ci->u2.transferinfo.ntransfer = ntransfer; - } + L->transferinfo.ftransfer = ftransfer; + L->transferinfo.ntransfer = ntransfer; if (isLua(ci) && L->top.p < ci->top.p) L->top.p = ci->top.p; /* protect entire activation register */ luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */ if (ci->top.p < L->top.p + LUA_MINSTACK) ci->top.p = L->top.p + LUA_MINSTACK; L->allowhook = 0; /* cannot call hooks inside a hook */ - ci->callstatus |= mask; + ci->callstatus |= CIST_HOOKED; lua_unlock(L); (*hook)(L, &ar); lua_lock(L); @@ -384,7 +380,7 @@ void luaD_hook (lua_State *L, int event, int line, L->allowhook = 1; ci->top.p = restorestack(L, ci_top); L->top.p = restorestack(L, top); - ci->callstatus &= ~mask; + ci->callstatus &= ~CIST_HOOKED; } } @@ -525,7 +521,7 @@ void luaD_poscall (lua_State *L, CallInfo *ci, int nres) { moveresults(L, ci->func.p, nres, fwanted); /* function cannot be in any of these cases when returning */ lua_assert(!(ci->callstatus & - (CIST_HOOKED | CIST_YPCALL | CIST_FIN | CIST_TRAN | CIST_CLSRET))); + (CIST_HOOKED | CIST_YPCALL | CIST_FIN | CIST_CLSRET))); L->ci = ci->previous; /* back to caller (after closing variables) */ } diff --git a/lstate.h b/lstate.h index aa9687aed4..e12ca154f4 100644 --- a/lstate.h +++ b/lstate.h @@ -183,8 +183,6 @@ typedef struct stringtable { ** yield (from the yield until the next resume); ** - field 'nres' is used only while closing tbc variables when ** returning from a function; -** - field 'transferinfo' is used only during call/returnhooks, -** before the function starts or after it ends. */ struct CallInfo { StkIdRel func; /* function index in the stack */ @@ -206,10 +204,6 @@ struct CallInfo { int funcidx; /* called-function index */ int nyield; /* number of values yielded */ int nres; /* number of values returned */ - struct { /* info about transferred values (for call/return hooks) */ - int ftransfer; /* offset of first value transferred */ - int ntransfer; /* number of values transferred */ - } transferinfo; } u2; l_uint32 callstatus; }; @@ -236,15 +230,13 @@ struct CallInfo { #define CIST_HOOKYIELD (cast(l_uint32, 1) << 14) /* function "called" a finalizer */ #define CIST_FIN (cast(l_uint32, 1) << 15) -/* 'ci' has transfer information */ -#define CIST_TRAN (cast(l_uint32, 1) << 16) /* function is closing tbc variables */ -#define CIST_CLSRET (cast(l_uint32, 1) << 17) -/* Bits 18-20 are used for CIST_RECST (see below) */ -#define CIST_RECST 18 /* the offset, not the mask */ +#define CIST_CLSRET (cast(l_uint32, 1) << 16) +/* Bits 17-19 are used for CIST_RECST (see below) */ +#define CIST_RECST 17 /* the offset, not the mask */ #if defined(LUA_COMPAT_LT_LE) /* using __lt for __le */ -#define CIST_LEQ (cast(l_uint32, 1) << 21) +#define CIST_LEQ (cast(l_uint32, 1) << 20) #endif @@ -354,6 +346,10 @@ struct lua_State { int basehookcount; int hookcount; volatile l_signalT hookmask; + struct { /* info about transferred values (for call/return hooks) */ + int ftransfer; /* offset of first value transferred */ + int ntransfer; /* number of values transferred */ + } transferinfo; }; From 1bf4b80f1ace8384eb9dd6f7f8b67256b3944a7a Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 2 Aug 2024 15:09:30 -0300 Subject: [PATCH 0938/1145] Floats formatted with "correct" precision Conversion float->string ensures that, for any float f, tonumber(tostring(f)) == f, but still avoiding noise like 1.1 converting to "1.1000000000000001". --- lobject.c | 52 ++++++++++++++++++------ luaconf.h | 16 +++++--- testes/math.lua | 106 ++++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 153 insertions(+), 21 deletions(-) diff --git a/lobject.c b/lobject.c index 1c4ea1aff1..f71595478f 100644 --- a/lobject.c +++ b/lobject.c @@ -10,6 +10,7 @@ #include "lprefix.h" +#include #include #include #include @@ -401,29 +402,54 @@ int luaO_utf8esc (char *buff, unsigned long x) { /* ** Maximum length of the conversion of a number to a string. Must be ** enough to accommodate both LUA_INTEGER_FMT and LUA_NUMBER_FMT. -** (For a long long int, this is 19 digits plus a sign and a final '\0', -** adding to 21. For a long double, it can go to a sign, 33 digits, -** the dot, an exponent letter, an exponent sign, 5 exponent digits, -** and a final '\0', adding to 43.) +** For a long long int, this is 19 digits plus a sign and a final '\0', +** adding to 21. For a long double, it can go to a sign, the dot, an +** exponent letter, an exponent sign, 4 exponent digits, the final +** '\0', plus the significant digits, which are approximately the *_DIG +** attribute. */ -#define MAXNUMBER2STR 44 +#define MAXNUMBER2STR (20 + l_floatatt(DIG)) /* -** Convert a number object to a string, adding it to a buffer +** Convert a float to a string, adding it to a buffer. First try with +** a not too large number of digits, to avoid noise (for instance, +** 1.1 going to "1.1000000000000001"). If that lose precision, so +** that reading the result back gives a different number, then do the +** conversion again with extra precision. Moreover, if the numeral looks +** like an integer (without a decimal point or an exponent), add ".0" to +** its end. +*/ +static int tostringbuffFloat (lua_Number n, char *buff) { + /* first conversion */ + int len = l_sprintf(buff, MAXNUMBER2STR, LUA_NUMBER_FMT, + (LUAI_UACNUMBER)n); + lua_Number check = lua_str2number(buff, NULL); /* read it back */ + if (check != n) { /* not enough precision? */ + /* convert again with more precision */ + len = l_sprintf(buff, MAXNUMBER2STR, LUA_NUMBER_FMT_N, + (LUAI_UACNUMBER)n); + } + /* looks like an integer? */ + if (buff[strspn(buff, "-0123456789")] == '\0') { + buff[len++] = lua_getlocaledecpoint(); + buff[len++] = '0'; /* adds '.0' to result */ + } + return len; +} + + +/* +** Convert a number object to a string, adding it to a buffer. */ static unsigned tostringbuff (TValue *obj, char *buff) { int len; lua_assert(ttisnumber(obj)); if (ttisinteger(obj)) len = lua_integer2str(buff, MAXNUMBER2STR, ivalue(obj)); - else { - len = lua_number2str(buff, MAXNUMBER2STR, fltvalue(obj)); - if (buff[strspn(buff, "-0123456789")] == '\0') { /* looks like an int? */ - buff[len++] = lua_getlocaledecpoint(); - buff[len++] = '0'; /* adds '.0' to result */ - } - } + else + len = tostringbuffFloat(fltvalue(obj), buff); + lua_assert(len < MAXNUMBER2STR); return cast_uint(len); } diff --git a/luaconf.h b/luaconf.h index 80349acc39..afc1b8b526 100644 --- a/luaconf.h +++ b/luaconf.h @@ -416,8 +416,13 @@ @@ l_floatatt(x) corrects float attribute 'x' to the proper float type ** by prefixing it with one of FLT/DBL/LDBL. @@ LUA_NUMBER_FRMLEN is the length modifier for writing floats. -@@ LUA_NUMBER_FMT is the format for writing floats. -@@ lua_number2str converts a float to a string. +@@ LUA_NUMBER_FMT is the format for writing floats with the maximum +** number of digits that respects tostring(tonumber(numeral)) == numeral. +** (That would be floor(log10(2^n)), where n is the number of bits in +** the float mantissa.) +@@ LUA_NUMBER_FMT_N is the format for writing floats with the minimum +** number of digits that ensures tonumber(tostring(number)) == number. +** (That would be LUA_NUMBER_FMT+2.) @@ l_mathop allows the addition of an 'l' or 'f' to all math operations. @@ l_floor takes the floor of a float. @@ lua_str2number converts a decimal numeral to a number. @@ -428,8 +433,6 @@ #define l_floor(x) (l_mathop(floor)(x)) -#define lua_number2str(s,sz,n) \ - l_sprintf((s), sz, LUA_NUMBER_FMT, (LUAI_UACNUMBER)(n)) /* @@ lua_numbertointeger converts a float number with an integral value @@ -458,6 +461,7 @@ #define LUA_NUMBER_FRMLEN "" #define LUA_NUMBER_FMT "%.7g" +#define LUA_NUMBER_FMT_N "%.9g" #define l_mathop(op) op##f @@ -474,6 +478,7 @@ #define LUA_NUMBER_FRMLEN "L" #define LUA_NUMBER_FMT "%.19Lg" +#define LUA_NUMBER_FMT_N "%.21Lg" #define l_mathop(op) op##l @@ -488,7 +493,8 @@ #define LUAI_UACNUMBER double #define LUA_NUMBER_FRMLEN "" -#define LUA_NUMBER_FMT "%.14g" +#define LUA_NUMBER_FMT "%.15g" +#define LUA_NUMBER_FMT_N "%.17g" #define l_mathop(op) op diff --git a/testes/math.lua b/testes/math.lua index 0191f7ddad..3937b9ce56 100644 --- a/testes/math.lua +++ b/testes/math.lua @@ -22,6 +22,18 @@ do end end + +-- maximum exponent for a floating-point number +local maxexp = 0 +do + local p = 2.0 + while p < math.huge do + maxexp = maxexp + 1 + p = p + p + end +end + + local function isNaN (x) return (x ~= x) end @@ -34,8 +46,8 @@ do local x = 2.0^floatbits assert(x > x - 1.0 and x == x + 1.0) - print(string.format("%d-bit integers, %d-bit (mantissa) floats", - intbits, floatbits)) + local msg = " %d-bit integers, %d-bit*2^%d floats" + print(string.format(msg, intbits, floatbits, maxexp)) end assert(math.type(0) == "integer" and math.type(0.0) == "float" @@ -803,7 +815,11 @@ do end -print("testing 'math.random'") +-- +-- [[================================================================== + print("testing 'math.random'") +-- -=================================================================== +-- local random, max, min = math.random, math.max, math.min @@ -1019,6 +1035,90 @@ assert(not pcall(random, minint + 1, minint)) assert(not pcall(random, maxint, maxint - 1)) assert(not pcall(random, maxint, minint)) +-- ]]================================================================== + + +-- +-- [[================================================================== + print("testing precision of 'tostring'") +-- -=================================================================== +-- + +-- number of decimal digits supported by float precision +local decdig = math.floor(floatbits * math.log(2, 10)) +print(string.format(" %d-digit float numbers with full precision", + decdig)) +-- number of decimal digits supported by integer precision +local Idecdig = math.floor(math.log(maxint, 10)) +print(string.format(" %d-digit integer numbers with full precision", + Idecdig)) + +do + -- Any number should print so that reading it back gives itself: + -- tonumber(tostring(x)) == x + + -- Mersenne fractions + local p = 1.0 + for i = 1, maxexp do + p = p + p + local x = 1 / (p - 1) + assert(x == tonumber(tostring(x))) + end + + -- some random numbers in [0,1) + for i = 1, 100 do + local x = math.random() + assert(x == tonumber(tostring(x))) + end + + -- different numbers shold print differently. + -- check pairs of floats with minimum detectable difference + local p = floatbits - 1 + for i = 1, maxexp - 1 do + for _, i in ipairs{-i, i} do + local x = 2^i + local diff = 2^(i - p) -- least significant bit for 'x' + local y = x + diff + local fy = tostring(y) + assert(x ~= y and tostring(x) ~= fy) + assert(tonumber(fy) == y) + end + end + + + -- "reasonable" numerals should be printed like themselves + + -- create random float numerals with 5 digits, with a decimal point + -- inserted in all places. (With more than 5, things like "0.00001" + -- reformats like "1e-5".) + for i = 1, 1000 do + -- random numeral with 5 digits + local x = string.format("%.5d", math.random(0, 99999)) + for i = 2, #x do + -- insert decimal point at position 'i' + local y = string.sub(x, 1, i - 1) .. "." .. string.sub(x, i, -1) + y = string.gsub(y, "^0*(%d.-%d)0*$", "%1") -- trim extra zeros + assert(y == tostring(tonumber(y))) + end + end + + -- all-random floats + local Fsz = string.packsize("n") -- size of floats in bytes + + for i = 1, 400 do + local s = string.pack("j", math.random(0)) -- a random string of bits + while #s < Fsz do -- make 's' long enough + s = s .. string.pack("j", math.random(0)) + end + local n = string.unpack("n", s) -- read 's' as a float + s = tostring(n) + if string.find(s, "^%-?%d") then -- avoid NaN, inf, -inf + assert(tonumber(s) == n) + end + end + +end +-- ]]================================================================== print('OK') From 8b752ddf14c1987411906d07a8c68f72f168b9b7 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Sat, 17 Aug 2024 12:37:04 -0300 Subject: [PATCH 0939/1145] Bug: wrong code gen. for indices with comparisons In function 'luaK_exp2val', used to generate code for indices: Macro 'hasjumps' does not consider the case when the whole expression is a "jump" (a test). In all other of its uses, the surrounding code ensures that the expression cannot be VJMP. --- lcode.c | 3 ++- testes/closure.lua | 8 ++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/lcode.c b/lcode.c index 47e5424e93..e7750fffcc 100644 --- a/lcode.c +++ b/lcode.c @@ -31,6 +31,7 @@ #include "lvm.h" +/* (note that expressions VJMP also have jumps.) */ #define hasjumps(e) ((e)->t != (e)->f) @@ -991,7 +992,7 @@ void luaK_exp2anyregup (FuncState *fs, expdesc *e) { ** or it is a constant. */ void luaK_exp2val (FuncState *fs, expdesc *e) { - if (hasjumps(e)) + if (e->k == VJMP || hasjumps(e)) luaK_exp2anyreg(fs, e); else luaK_dischargevars(fs, e); diff --git a/testes/closure.lua b/testes/closure.lua index de1b54ec61..07149ef36a 100644 --- a/testes/closure.lua +++ b/testes/closure.lua @@ -3,6 +3,14 @@ print "testing closures" +do -- bug in 5.4.7 + _ENV[true] = 10 + local function aux () return _ENV[1 < 2] end + assert(aux() == 10) + _ENV[true] = nil +end + + local A,B = 0,{g=10} local function f(x) local a = {} From 3e88b72b8e71c0946d089a04876e7bdc61d187a9 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 19 Aug 2024 18:39:25 -0300 Subject: [PATCH 0940/1145] A return can have at most 254 values --- lcode.c | 2 ++ testes/calls.lua | 11 +++++++++++ 2 files changed, 13 insertions(+) diff --git a/lcode.c b/lcode.c index e7750fffcc..8786a7219e 100644 --- a/lcode.c +++ b/lcode.c @@ -208,6 +208,8 @@ void luaK_ret (FuncState *fs, int first, int nret) { case 1: op = OP_RETURN1; break; default: op = OP_RETURN; break; } + if (nret + 1 > MAXARG_B) + luaX_syntaxerror(fs->ls, "too many returns"); luaK_codeABC(fs, op, first, nret + 1, 0); } diff --git a/testes/calls.lua b/testes/calls.lua index 9a5eed0bbd..409a275d5e 100644 --- a/testes/calls.lua +++ b/testes/calls.lua @@ -518,5 +518,16 @@ do -- check reuse of strings in dumps end end + +do -- test limit of multiple returns (254 values) + local code = "return 10" .. string.rep(",10", 253) + local res = {assert(load(code))()} + assert(#res == 254 and res[254] == 10) + + code = code .. ",10" + local status, msg = load(code) + assert(not status and string.find(msg, "too many returns")) +end + print('OK') return deep From 75620b45ae9d500a3251a0e698de98ab588d2a29 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 20 Aug 2024 15:15:23 -0300 Subject: [PATCH 0941/1145] 'lcode.c' can use 'checklimit', too --- lcode.c | 10 +++------- lparser.c | 10 +++++----- lparser.h | 2 ++ 3 files changed, 10 insertions(+), 12 deletions(-) diff --git a/lcode.c b/lcode.c index 8786a7219e..c25226abf6 100644 --- a/lcode.c +++ b/lcode.c @@ -208,8 +208,7 @@ void luaK_ret (FuncState *fs, int first, int nret) { case 1: op = OP_RETURN1; break; default: op = OP_RETURN; break; } - if (nret + 1 > MAXARG_B) - luaX_syntaxerror(fs->ls, "too many returns"); + luaY_checklimit(fs, nret + 1, MAXARG_B, "returns"); luaK_codeABC(fs, op, first, nret + 1, 0); } @@ -473,9 +472,7 @@ static int luaK_codek (FuncState *fs, int reg, int k) { void luaK_checkstack (FuncState *fs, int n) { int newstack = fs->freereg + n; if (newstack > fs->f->maxstacksize) { - if (newstack > MAX_FSTACK) - luaX_syntaxerror(fs->ls, - "function or expression needs too many registers"); + luaY_checklimit(fs, newstack, MAX_FSTACK, "registers"); fs->f->maxstacksize = cast_byte(newstack); } } @@ -727,8 +724,7 @@ static void const2exp (TValue *v, expdesc *e) { */ void luaK_setreturns (FuncState *fs, expdesc *e, int nresults) { Instruction *pc = &getinstruction(fs, e); - if (nresults + 1 > MAXARG_C) - luaX_syntaxerror(fs->ls, "too many multiple results"); + luaY_checklimit(fs, nresults + 1, MAXARG_C, "multiple results"); if (e->k == VCALL) /* expression is an open function call? */ SETARG_C(*pc, nresults + 1); else { diff --git a/lparser.c b/lparser.c index 452ab19eac..b193b67251 100644 --- a/lparser.c +++ b/lparser.c @@ -84,8 +84,8 @@ static l_noret errorlimit (FuncState *fs, int limit, const char *what) { } -static void checklimit (FuncState *fs, int v, int l, const char *what) { - if (v > l) errorlimit(fs, l, what); +void luaY_checklimit (FuncState *fs, int v, int l, const char *what) { + if (l_unlikely(v > l)) errorlimit(fs, l, what); } @@ -196,7 +196,7 @@ static int new_localvarkind (LexState *ls, TString *name, lu_byte kind) { FuncState *fs = ls->fs; Dyndata *dyd = ls->dyd; Vardesc *var; - checklimit(fs, dyd->actvar.n + 1 - fs->firstlocal, + luaY_checklimit(fs, dyd->actvar.n + 1 - fs->firstlocal, MAXVARS, "local variables"); luaM_growvector(L, dyd->actvar.arr, dyd->actvar.n + 1, dyd->actvar.size, Vardesc, USHRT_MAX, "local variables"); @@ -361,7 +361,7 @@ static int searchupvalue (FuncState *fs, TString *name) { static Upvaldesc *allocupvalue (FuncState *fs) { Proto *f = fs->f; int oldsize = f->sizeupvalues; - checklimit(fs, fs->nups + 1, MAXUPVAL, "upvalues"); + luaY_checklimit(fs, fs->nups + 1, MAXUPVAL, "upvalues"); luaM_growvector(fs->ls->L, f->upvalues, fs->nups, f->sizeupvalues, Upvaldesc, MAXUPVAL, "upvalues"); while (oldsize < f->sizeupvalues) @@ -860,7 +860,7 @@ static void recfield (LexState *ls, ConsControl *cc) { lu_byte reg = ls->fs->freereg; expdesc tab, key, val; if (ls->t.token == TK_NAME) { - checklimit(fs, cc->nh, INT_MAX, "items in a constructor"); + luaY_checklimit(fs, cc->nh, INT_MAX / 2, "items in a constructor"); codename(ls, &key); } else /* ls->t.token == '[' */ diff --git a/lparser.h b/lparser.h index 535dc9da88..8a87776d67 100644 --- a/lparser.h +++ b/lparser.h @@ -164,6 +164,8 @@ typedef struct FuncState { LUAI_FUNC lu_byte luaY_nvarstack (FuncState *fs); +LUAI_FUNC void luaY_checklimit (FuncState *fs, int v, int l, + const char *what); LUAI_FUNC LClosure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, Dyndata *dyd, const char *name, int firstchar); From fd0e1f530d06340f99334b07d74e5133ce073787 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 22 Aug 2024 11:11:00 -0300 Subject: [PATCH 0942/1145] Added option for direct correction of stack pointers The use of a pointer (not access, only for computations) after its deallocation is forbiden in ISO C, but seems to work fine in all platforms we are aware of. So, using that to correct stack pointers after a stack reallocation seems safe and is much simpler than the current implementation (first change all pointers to offsets and then changing the offsets back to pointers). Anyway, for now that option is disabled. --- ldo.c | 54 ++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 44 insertions(+), 10 deletions(-) diff --git a/ldo.c b/ldo.c index d63c82675b..e75a79ab2f 100644 --- a/ldo.c +++ b/ldo.c @@ -190,6 +190,16 @@ int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) { #define ERRORSTACKSIZE (MAXSTACK + STACKERRSPACE) +/* +** In ISO C, any pointer use after the pointer has been deallocated is +** undefined behavior. So, before a stack reallocation, all pointers are +** changed to offsets, and after the reallocation they are changed back +** to pointers. As during the reallocation the pointers are invalid, the +** reallocation cannot run emergency collections. +** +*/ + +#if 1 /* ** Change all pointers to the stack into offsets. */ @@ -210,9 +220,10 @@ static void relstack (lua_State *L) { /* ** Change back all offsets into pointers. */ -static void correctstack (lua_State *L) { +static void correctstack (lua_State *L, StkId oldstack) { CallInfo *ci; UpVal *up; + UNUSED(oldstack); L->top.p = restorestack(L, L->top.offset); L->tbclist.p = restorestack(L, L->tbclist.offset); for (up = L->openupval; up != NULL; up = up->u.open.next) @@ -225,15 +236,37 @@ static void correctstack (lua_State *L) { } } +#else +/* +** Alternatively, we can use the old address after the dealocation. +** That is not strict ISO C, but seems to work fine everywhere. +*/ + +static void relstack (lua_State *L) { UNUSED(L); } + +static void correctstack (lua_State *L, StkId oldstack) { + CallInfo *ci; + UpVal *up; + StkId newstack = L->stack.p; + if (oldstack == newstack) + return; + L->top.p = L->top.p - oldstack + newstack; + L->tbclist.p = L->tbclist.p - oldstack + newstack; + for (up = L->openupval; up != NULL; up = up->u.open.next) + up->v.p = s2v(uplevel(up) - oldstack + newstack); + for (ci = L->ci; ci != NULL; ci = ci->previous) { + ci->top.p = ci->top.p - oldstack + newstack; + ci->func.p = ci->func.p - oldstack + newstack; + if (isLua(ci)) + ci->u.l.trap = 1; /* signal to update 'trap' in 'luaV_execute' */ + } +} + +#endif + /* ** Reallocate the stack to a new size, correcting all pointers into it. -** In ISO C, any pointer use after the pointer has been deallocated is -** undefined behavior. So, before the reallocation, all pointers are -** changed to offsets, and after the reallocation they are changed back -** to pointers. As during the reallocation the pointers are invalid, the -** reallocation cannot run emergency collections. -** ** In case of allocation error, raise an error or return false according ** to 'raiseerror'. */ @@ -241,21 +274,22 @@ int luaD_reallocstack (lua_State *L, int newsize, int raiseerror) { int oldsize = stacksize(L); int i; StkId newstack; + StkId oldstack = L->stack.p; lu_byte oldgcstop = G(L)->gcstopem; lua_assert(newsize <= MAXSTACK || newsize == ERRORSTACKSIZE); relstack(L); /* change pointers to offsets */ G(L)->gcstopem = 1; /* stop emergency collection */ - newstack = luaM_reallocvector(L, L->stack.p, oldsize + EXTRA_STACK, + newstack = luaM_reallocvector(L, oldstack, oldsize + EXTRA_STACK, newsize + EXTRA_STACK, StackValue); G(L)->gcstopem = oldgcstop; /* restore emergency collection */ if (l_unlikely(newstack == NULL)) { /* reallocation failed? */ - correctstack(L); /* change offsets back to pointers */ + correctstack(L, oldstack); /* change offsets back to pointers */ if (raiseerror) luaM_error(L); else return 0; /* do not raise an error */ } L->stack.p = newstack; - correctstack(L); /* change offsets back to pointers */ + correctstack(L, oldstack); /* change offsets back to pointers */ L->stack_last.p = L->stack.p + newsize; for (i = oldsize + EXTRA_STACK; i < newsize + EXTRA_STACK; i++) setnilvalue(s2v(newstack + i)); /* erase new segment */ From 007b8c7a01eaa97d796561a19c7e9af1ec474495 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 6 Sep 2024 14:35:04 -0300 Subject: [PATCH 0943/1145] Details Identation + comments --- lcode.c | 4 ++-- lopcodes.c | 4 ++-- lutf8lib.c | 32 ++++++++++++++++---------------- 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/lcode.c b/lcode.c index c25226abf6..4267079495 100644 --- a/lcode.c +++ b/lcode.c @@ -1837,8 +1837,8 @@ static int finaltarget (Instruction *code, int i) { Instruction pc = code[i]; if (GET_OPCODE(pc) != OP_JMP) break; - else - i += GETARG_sJ(pc) + 1; + else + i += GETARG_sJ(pc) + 1; } return i; } diff --git a/lopcodes.c b/lopcodes.c index 5533b51785..092c390206 100644 --- a/lopcodes.c +++ b/lopcodes.c @@ -68,8 +68,8 @@ LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = { ,opmode(0, 0, 0, 0, 1, iABC) /* OP_SHL */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_SHR */ ,opmode(1, 0, 0, 0, 0, iABC) /* OP_MMBIN */ - ,opmode(1, 0, 0, 0, 0, iABC) /* OP_MMBINI*/ - ,opmode(1, 0, 0, 0, 0, iABC) /* OP_MMBINK*/ + ,opmode(1, 0, 0, 0, 0, iABC) /* OP_MMBINI */ + ,opmode(1, 0, 0, 0, 0, iABC) /* OP_MMBINK */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_UNM */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_BNOT */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_NOT */ diff --git a/lutf8lib.c b/lutf8lib.c index 04bbfa567b..4c9784e093 100644 --- a/lutf8lib.c +++ b/lutf8lib.c @@ -192,22 +192,22 @@ static int byteoffset (lua_State *L) { if (iscontp(s + posi)) return luaL_error(L, "initial position is a continuation byte"); if (n < 0) { - while (n < 0 && posi > 0) { /* move back */ - do { /* find beginning of previous character */ - posi--; - } while (posi > 0 && iscontp(s + posi)); - n++; - } - } - else { - n--; /* do not move for 1st character */ - while (n > 0 && posi < (lua_Integer)len) { - do { /* find beginning of next character */ - posi++; - } while (iscontp(s + posi)); /* (cannot pass final '\0') */ - n--; - } - } + while (n < 0 && posi > 0) { /* move back */ + do { /* find beginning of previous character */ + posi--; + } while (posi > 0 && iscontp(s + posi)); + n++; + } + } + else { + n--; /* do not move for 1st character */ + while (n > 0 && posi < (lua_Integer)len) { + do { /* find beginning of next character */ + posi++; + } while (iscontp(s + posi)); /* (cannot pass final '\0') */ + n--; + } + } } if (n != 0) { /* did not find given character? */ luaL_pushfail(L); From a04e0ffdb9be42a77b5657f46cac8d7faa5a0f43 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 6 Sep 2024 14:38:39 -0300 Subject: [PATCH 0944/1145] Rename of fields in global state that control GC All fields in the global state that control the pace of the garbage collector prefixed with 'GC'. --- lapi.c | 4 ++-- lgc.c | 28 ++++++++++++++-------------- lmem.c | 8 ++++---- lstate.c | 16 ++++++++-------- lstate.h | 8 ++++---- 5 files changed, 32 insertions(+), 32 deletions(-) diff --git a/lapi.c b/lapi.c index 1f4e9f9646..98d2366505 100644 --- a/lapi.c +++ b/lapi.c @@ -1190,11 +1190,11 @@ LUA_API int lua_gc (lua_State *L, int what, ...) { } case LUA_GCCOUNT: { /* GC values are expressed in Kbytes: #bytes/2^10 */ - res = cast_int(g->totalbytes >> 10); + res = cast_int(g->GCtotalbytes >> 10); break; } case LUA_GCCOUNTB: { - res = cast_int(g->totalbytes & 0x3ff); + res = cast_int(g->GCtotalbytes & 0x3ff); break; } case LUA_GCSTEP: { diff --git a/lgc.c b/lgc.c index 93d2249b1b..9203463528 100644 --- a/lgc.c +++ b/lgc.c @@ -290,7 +290,7 @@ GCObject *luaC_newobj (lua_State *L, lu_byte tt, size_t sz) { ** (only closures can), and a userdata's metatable must be a table. */ static void reallymarkobject (global_State *g, GCObject *o) { - g->marked++; + g->GCmarked++; switch (o->tt) { case LUA_VSHRSTR: case LUA_VLNGSTR: { @@ -401,7 +401,7 @@ static void cleargraylists (global_State *g) { */ static void restartcollection (global_State *g) { cleargraylists(g); - g->marked = NFIXED; + g->GCmarked = NFIXED; markobject(g, g->mainthread); markvalue(g, &g->l_registry); markmt(g); @@ -781,7 +781,7 @@ static void freeupval (lua_State *L, UpVal *uv) { static void freeobj (lua_State *L, GCObject *o) { - G(L)->totalobjs--; + G(L)->GCtotalobjs--; switch (o->tt) { case LUA_VPROTO: luaF_freeproto(L, gco2p(o)); @@ -1052,7 +1052,7 @@ void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt) { ** approximately (marked * pause / 100). */ static void setpause (global_State *g) { - l_obj threshold = applygcparam(g, PAUSE, g->marked); + l_obj threshold = applygcparam(g, PAUSE, g->GCmarked); l_obj debt = threshold - gettotalobjs(g); if (debt < 0) debt = 0; luaE_setdebt(g, debt); @@ -1236,7 +1236,7 @@ static void finishgencycle (lua_State *L, global_State *g) { ** in generational mode. */ static void minor2inc (lua_State *L, global_State *g, lu_byte kind) { - g->GCmajorminor = g->marked; /* number of live objects */ + g->GCmajorminor = g->GCmarked; /* number of live objects */ g->gckind = kind; g->reallyold = g->old1 = g->survival = NULL; g->finobjrold = g->finobjold1 = g->finobjsur = NULL; @@ -1260,7 +1260,7 @@ static void minor2inc (lua_State *L, global_State *g, lu_byte kind) { static int checkminormajor (global_State *g, l_obj addedold1) { l_obj step = applygcparam(g, MINORMUL, g->GCmajorminor); l_obj limit = applygcparam(g, MINORMAJOR, g->GCmajorminor); - return (addedold1 >= (step >> 1) || g->marked >= limit); + return (addedold1 >= (step >> 1) || g->GCmarked >= limit); } /* @@ -1270,7 +1270,7 @@ static int checkminormajor (global_State *g, l_obj addedold1) { */ static void youngcollection (lua_State *L, global_State *g) { l_obj addedold1 = 0; - l_obj marked = g->marked; /* preserve 'g->marked' */ + l_obj marked = g->GCmarked; /* preserve 'g->GCmarked' */ GCObject **psurvival; /* to point to first non-dead survival object */ GCObject *dummy; /* dummy out parameter to 'sweepgen' */ lua_assert(g->gcstate == GCSpropagate); @@ -1304,12 +1304,12 @@ static void youngcollection (lua_State *L, global_State *g) { sweepgen(L, g, &g->tobefnz, NULL, &dummy, &addedold1); /* keep total number of added old1 objects */ - g->marked = marked + addedold1; + g->GCmarked = marked + addedold1; /* decide whether to shift to major mode */ if (checkminormajor(g, addedold1)) { minor2inc(L, g, KGC_GENMAJOR); /* go to major mode */ - g->marked = 0; /* avoid pause in first major cycle */ + g->GCmarked = 0; /* avoid pause in first major cycle */ } else finishgencycle(L, g); /* still in minor mode; finish it */ @@ -1338,8 +1338,8 @@ static void atomic2gen (lua_State *L, global_State *g) { sweep2old(L, &g->tobefnz); g->gckind = KGC_GENMINOR; - g->GCmajorminor = g->marked; /* "base" for number of objects */ - g->marked = 0; /* to count the number of added old1 objects */ + g->GCmajorminor = g->GCmarked; /* "base" for number of objects */ + g->GCmarked = 0; /* to count the number of added old1 objects */ finishgencycle(L, g); } @@ -1407,14 +1407,14 @@ static int checkmajorminor (lua_State *L, global_State *g) { l_obj numobjs = gettotalobjs(g); l_obj addedobjs = numobjs - g->GCmajorminor; l_obj limit = applygcparam(g, MAJORMINOR, addedobjs); - l_obj tobecollected = numobjs - g->marked; + l_obj tobecollected = numobjs - g->GCmarked; if (tobecollected > limit) { atomic2gen(L, g); /* return to generational mode */ setminordebt(g); return 0; /* exit incremental collection */ } } - g->GCmajorminor = g->marked; /* prepare for next collection */ + g->GCmajorminor = g->GCmarked; /* prepare for next collection */ return 1; /* stay doing incremental collections */ } @@ -1692,7 +1692,7 @@ static void fullinc (lua_State *L, global_State *g) { luaC_runtilstate(L, GCSpause, 1); luaC_runtilstate(L, GCScallfin, 1); /* run up to finalizers */ /* 'marked' must be correct after a full GC cycle */ - lua_assert(g->marked == gettotalobjs(g)); + lua_assert(g->GCmarked == gettotalobjs(g)); luaC_runtilstate(L, GCSpause, 1); /* finish collection */ setpause(g); } diff --git a/lmem.c b/lmem.c index 52a4f99927..f18ea17211 100644 --- a/lmem.c +++ b/lmem.c @@ -151,7 +151,7 @@ void luaM_free_ (lua_State *L, void *block, size_t osize) { global_State *g = G(L); lua_assert((osize == 0) == (block == NULL)); callfrealloc(g, block, osize, 0); - g->totalbytes -= osize; + g->GCtotalbytes -= osize; } @@ -181,10 +181,10 @@ void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) { if (l_unlikely(newblock == NULL && nsize > 0)) { newblock = tryagain(L, block, osize, nsize); if (newblock == NULL) /* still no memory? */ - return NULL; /* do not update 'totalbytes' */ + return NULL; /* do not update 'GCtotalbytes' */ } lua_assert((nsize == 0) == (newblock == NULL)); - g->totalbytes += nsize - osize; + g->GCtotalbytes += nsize - osize; return newblock; } @@ -209,7 +209,7 @@ void *luaM_malloc_ (lua_State *L, size_t size, int tag) { if (newblock == NULL) luaM_error(L); } - g->totalbytes += size; + g->GCtotalbytes += size; return newblock; } } diff --git a/lstate.c b/lstate.c index eb71ed8c9e..f4c9081dfd 100644 --- a/lstate.c +++ b/lstate.c @@ -74,15 +74,15 @@ typedef struct LG { /* ** set GCdebt to a new value keeping the real number of allocated -** objects (totalobjs - GCdebt) invariant and avoiding overflows in -** 'totalobjs'. +** objects (GCtotalobjs - GCdebt) invariant and avoiding overflows in +** 'GCtotalobjs'. */ void luaE_setdebt (global_State *g, l_obj debt) { l_obj tb = gettotalobjs(g); lua_assert(tb > 0); if (debt > MAX_LOBJ - tb) - debt = MAX_LOBJ - tb; /* will make 'totalobjs == MAX_LMEM' */ - g->totalobjs = tb + debt; + debt = MAX_LOBJ - tb; /* will make GCtotalobjs == MAX_LOBJ */ + g->GCtotalobjs = tb + debt; g->GCdebt = debt; } @@ -269,7 +269,7 @@ static void close_state (lua_State *L) { } luaM_freearray(L, G(L)->strt.hash, cast_sizet(G(L)->strt.size)); freestack(L); - lua_assert(g->totalbytes == sizeof(LG)); + lua_assert(g->GCtotalbytes == sizeof(LG)); lua_assert(gettotalobjs(g) == 1); (*g->frealloc)(g->ud, fromstate(L), sizeof(LG), 0); /* free main block */ } @@ -378,9 +378,9 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud, unsigned seed) { g->gray = g->grayagain = NULL; g->weak = g->ephemeron = g->allweak = NULL; g->twups = NULL; - g->totalbytes = sizeof(LG); - g->totalobjs = 1; - g->marked = 0; + g->GCtotalbytes = sizeof(LG); + g->GCtotalobjs = 1; + g->GCmarked = 0; g->GCdebt = 0; setivalue(&g->nilvalue, 0); /* to signal that state is not yet built */ setgcparam(g, PAUSE, LUAI_GCPAUSE); diff --git a/lstate.h b/lstate.h index e12ca154f4..6aa02889a0 100644 --- a/lstate.h +++ b/lstate.h @@ -274,10 +274,10 @@ struct CallInfo { typedef struct global_State { lua_Alloc frealloc; /* function to reallocate memory */ void *ud; /* auxiliary data to 'frealloc' */ - lu_mem totalbytes; /* number of bytes currently allocated */ - l_obj totalobjs; /* total number of objects allocated + GCdebt */ + lu_mem GCtotalbytes; /* number of bytes currently allocated */ + l_obj GCtotalobjs; /* total number of objects allocated + GCdebt */ l_obj GCdebt; /* objects counted but not yet allocated */ - l_obj marked; /* number of objects marked in a GC cycle */ + l_obj GCmarked; /* number of objects marked in a GC cycle */ l_obj GCmajorminor; /* auxiliary counter to control major-minor shifts */ stringtable strt; /* hash table for strings */ TValue l_registry; @@ -412,7 +412,7 @@ union GCUnion { /* actual number of total objects allocated */ -#define gettotalobjs(g) ((g)->totalobjs - (g)->GCdebt) +#define gettotalobjs(g) ((g)->GCtotalobjs - (g)->GCdebt) LUAI_FUNC void luaE_setdebt (global_State *g, l_obj debt); From 4901853c1163d0bba81ef1579835cb2b6560e245 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 10 Sep 2024 17:05:39 -0300 Subject: [PATCH 0945/1145] Parameter for lua_gc/LUA_GCSTEP changed to 'size_t' 'size_t' is the common type for measuring memory. 'int' can be too small for steps. --- lapi.c | 2 +- lbaselib.c | 2 +- manual/manual.of | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lapi.c b/lapi.c index 98d2366505..4f4e3021f9 100644 --- a/lapi.c +++ b/lapi.c @@ -1199,7 +1199,7 @@ LUA_API int lua_gc (lua_State *L, int what, ...) { } case LUA_GCSTEP: { lu_byte oldstp = g->gcstp; - l_obj n = va_arg(argp, int); + l_obj n = cast(l_obj, va_arg(argp, size_t)); int work = 0; /* true if GC did some work */ g->gcstp = 0; /* allow GC to run (other bits must be zero here) */ if (n <= 0) diff --git a/lbaselib.c b/lbaselib.c index a7b6c3ed7f..b296c4b761 100644 --- a/lbaselib.c +++ b/lbaselib.c @@ -216,7 +216,7 @@ static int luaB_collectgarbage (lua_State *L) { } case LUA_GCSTEP: { lua_Integer n = luaL_optinteger(L, 2, 0); - int res = lua_gc(L, o, (int)n); + int res = lua_gc(L, o, cast_sizet(n)); checkvalres(res); lua_pushboolean(L, res); return 1; diff --git a/manual/manual.of b/manual/manual.of index 93e3a114e6..f0a2ed9452 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -3326,7 +3326,7 @@ Returns the remainder of dividing the current amount of bytes of memory in use by Lua by 1024. } -@item{@defid{LUA_GCSTEP} (int n)| +@item{@defid{LUA_GCSTEP} (size_t n)| Performs a step of garbage collection. } From b443145ff3415fcaee903a7d95fa7212df5a77db Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 12 Sep 2024 11:08:11 -0300 Subject: [PATCH 0946/1145] Details Fixed comments in sort partition. --- ltablib.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ltablib.c b/ltablib.c index 4db3768a52..baa7111e2c 100644 --- a/ltablib.c +++ b/ltablib.c @@ -298,14 +298,14 @@ static IdxT partition (lua_State *L, IdxT lo, IdxT up) { for (;;) { /* next loop: repeat ++i while a[i] < P */ while ((void)geti(L, 1, ++i), sort_comp(L, -1, -2)) { - if (l_unlikely(i == up - 1)) /* a[i] < P but a[up - 1] == P ?? */ + if (l_unlikely(i == up - 1)) /* a[up - 1] < P == a[up - 1] */ luaL_error(L, "invalid order function for sorting"); lua_pop(L, 1); /* remove a[i] */ } - /* after the loop, a[i] >= P and a[lo .. i - 1] < P */ + /* after the loop, a[i] >= P and a[lo .. i - 1] < P (a) */ /* next loop: repeat --j while P < a[j] */ while ((void)geti(L, 1, --j), sort_comp(L, -3, -1)) { - if (l_unlikely(j < i)) /* j < i but a[j] > P ?? */ + if (l_unlikely(j < i)) /* j <= i - 1 and a[j] > P, contradicts (a) */ luaL_error(L, "invalid order function for sorting"); lua_pop(L, 1); /* remove a[j] */ } From ddfa1fbccfe4c1ec69f7396a4f5842abe70927ba Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 19 Sep 2024 19:02:14 -0300 Subject: [PATCH 0947/1145] GC back to controling pace counting bytes Memory is the resource we want to save. Still to be reviewed again. --- lapi.c | 19 +--- lauxlib.c | 1 + lgc.c | 307 ++++++++++++++++++++++++++++++++---------------------- lgc.h | 34 +++--- llimits.h | 19 ++-- lmem.c | 8 +- lobject.c | 12 +-- lobject.h | 2 +- lstate.c | 14 ++- lstate.h | 15 ++- ltests.c | 18 ++-- 11 files changed, 247 insertions(+), 202 deletions(-) diff --git a/lapi.c b/lapi.c index 4f4e3021f9..c493609a7d 100644 --- a/lapi.c +++ b/lapi.c @@ -53,16 +53,6 @@ const char lua_ident[] = #define isupvalue(i) ((i) < LUA_REGISTRYINDEX) -/* Advance the garbage collector when creating large objects */ -static void advancegc (lua_State *L, size_t delta) { - delta >>= 5; /* one object for each 32 bytes (empirical) */ - if (delta > 0) { - global_State *g = G(L); - luaE_setdebt(g, g->GCdebt - cast(l_obj, delta)); - } -} - - /* ** Convert an acceptable index to a pointer to its respective value. ** Non-valid indices return the special nil value 'G(L)->nilvalue'. @@ -540,7 +530,6 @@ LUA_API const char *lua_pushlstring (lua_State *L, const char *s, size_t len) { ts = (len == 0) ? luaS_new(L, "") : luaS_newlstr(L, s, len); setsvalue2s(L, L->top.p, ts); api_incr_top(L); - advancegc(L, len); luaC_checkGC(L); lua_unlock(L); return getstr(ts); @@ -557,7 +546,6 @@ LUA_API const char *lua_pushextlstring (lua_State *L, setsvalue2s(L, L->top.p, ts); api_incr_top(L); if (falloc != NULL) /* non-static string? */ - advancegc(L, len); /* count its memory */ luaC_checkGC(L); lua_unlock(L); return getstr(ts); @@ -1190,16 +1178,16 @@ LUA_API int lua_gc (lua_State *L, int what, ...) { } case LUA_GCCOUNT: { /* GC values are expressed in Kbytes: #bytes/2^10 */ - res = cast_int(g->GCtotalbytes >> 10); + res = cast_int(gettotalbytes(g) >> 10); break; } case LUA_GCCOUNTB: { - res = cast_int(g->GCtotalbytes & 0x3ff); + res = cast_int(gettotalbytes(g) & 0x3ff); break; } case LUA_GCSTEP: { lu_byte oldstp = g->gcstp; - l_obj n = cast(l_obj, va_arg(argp, size_t)); + l_mem n = cast(l_mem, va_arg(argp, size_t)); int work = 0; /* true if GC did some work */ g->gcstp = 0; /* allow GC to run (other bits must be zero here) */ if (n <= 0) @@ -1356,7 +1344,6 @@ LUA_API void *lua_newuserdatauv (lua_State *L, size_t size, int nuvalue) { u = luaS_newudata(L, size, cast(unsigned short, nuvalue)); setuvalue(L, s2v(L->top.p), u); api_incr_top(L); - advancegc(L, size); luaC_checkGC(L); lua_unlock(L); return getudatamem(u); diff --git a/lauxlib.c b/lauxlib.c index b70b7ae63a..defd4d578e 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -618,6 +618,7 @@ LUALIB_API void luaL_pushresult (luaL_Buffer *B) { box->bsize = 0; box->box = NULL; lua_pushextlstring(L, s, len, allocf, ud); lua_closeslot(L, -2); /* close the box */ + lua_gc(L, LUA_GCSTEP, len); } lua_remove(L, -2); /* remove box or placeholder from the stack */ } diff --git a/lgc.c b/lgc.c index 9203463528..a38d11b2d5 100644 --- a/lgc.c +++ b/lgc.c @@ -18,7 +18,6 @@ #include "ldo.h" #include "lfunc.h" #include "lgc.h" -#include "llex.h" #include "lmem.h" #include "lobject.h" #include "lstate.h" @@ -27,13 +26,6 @@ #include "ltm.h" -/* -** Number of fixed (luaC_fix) objects in a Lua state: metafield names, -** plus reserved words, plus "_ENV", plus the memory-error message. -*/ -#define NFIXED (TM_N + NUM_RESERVED + 2) - - /* ** Maximum number of elements to sweep in each single step. ** (Large enough to dissipate fixed overheads but small enough @@ -42,6 +34,12 @@ #define GCSWEEPMAX 20 +/* +** Cost (in work units) of running one finalizer. +*/ +#define CWUFIN 10 + + /* mask with all color bits */ #define maskcolors (bitmask(BLACKBIT) | WHITEBITS) @@ -95,7 +93,7 @@ static void reallymarkobject (global_State *g, GCObject *o); -static l_obj atomic (lua_State *L); +static void atomic (lua_State *L); static void entersweep (lua_State *L); @@ -112,6 +110,66 @@ static void entersweep (lua_State *L); #define gnodelast(h) gnode(h, cast_sizet(sizenode(h))) +static size_t objsize (GCObject *o) { + switch (o->tt) { + case LUA_VTABLE: { + /* Fow now, table size does not consider 'haslastfree' */ + Table *t = gco2t(o); + size_t sz = sizeof(Table) + + luaH_realasize(t) * (sizeof(Value) + 1); + if (!isdummy(t)) + sz += sizenode(t) * sizeof(Node); + return sz; + } + case LUA_VLCL: { + LClosure *cl = gco2lcl(o); + return sizeLclosure(cl->nupvalues); + } + case LUA_VCCL: { + CClosure *cl = gco2ccl(o); + return sizeCclosure(cl->nupvalues); + break; + } + case LUA_VUSERDATA: { + Udata *u = gco2u(o); + return sizeudata(u->nuvalue, u->len); + } + case LUA_VPROTO: { + Proto *p = gco2p(o); + size_t sz = sizeof(Proto) + + cast_uint(p->sizep) * sizeof(Proto*) + + cast_uint(p->sizek) * sizeof(TValue) + + cast_uint(p->sizelocvars) * sizeof(LocVar) + + cast_uint(p->sizeupvalues) * sizeof(Upvaldesc); + if (!(p->flag & PF_FIXED)) { + sz += cast_uint(p->sizecode) * sizeof(Instruction) + + cast_uint(p->sizelineinfo) * sizeof(lu_byte) + + cast_uint(p->sizeabslineinfo) * sizeof(AbsLineInfo); + } + return sz; + } + case LUA_VTHREAD: { + lua_State *L1 = gco2th(o); + size_t sz = sizeof(lua_State) + LUA_EXTRASPACE + + cast_uint(L1->nci) * sizeof(CallInfo); + if (L1->stack.p != NULL) + sz += cast_uint(stacksize(L1) + EXTRA_STACK) * sizeof(StackValue); + return sz; + } + case LUA_VSHRSTR: { + TString *ts = gco2ts(o); + return sizestrshr(cast_uint(ts->shrlen)); + } + case LUA_VLNGSTR: { + TString *ts = gco2ts(o); + return luaS_sizelngstr(ts->u.lnglen, ts->shrlen); + } + case LUA_VUPVAL: return sizeof(UpVal); + default: lua_assert(0); return 0; + } +} + + static GCObject **getgclist (GCObject *o) { switch (o->tt) { case LUA_VTABLE: return &gco2t(o)->gclist; @@ -250,7 +308,6 @@ GCObject *luaC_newobjdt (lua_State *L, lu_byte tt, size_t sz, size_t offset) { global_State *g = G(L); char *p = cast_charp(luaM_newobject(L, novariant(tt), sz)); GCObject *o = cast(GCObject *, p + offset); - g->GCdebt--; o->marked = luaC_white(g); o->tt = tt; o->next = g->allgc; @@ -290,7 +347,7 @@ GCObject *luaC_newobj (lua_State *L, lu_byte tt, size_t sz) { ** (only closures can), and a userdata's metatable must be a table. */ static void reallymarkobject (global_State *g, GCObject *o) { - g->GCmarked++; + g->GCmarked += cast(l_mem, objsize(o)); switch (o->tt) { case LUA_VSHRSTR: case LUA_VLNGSTR: { @@ -338,14 +395,10 @@ static void markmt (global_State *g) { /* ** mark all objects in list of being-finalized */ -static l_obj markbeingfnz (global_State *g) { +static void markbeingfnz (global_State *g) { GCObject *o; - l_obj count = 0; - for (o = g->tobefnz; o != NULL; o = o->next) { - count++; + for (o = g->tobefnz; o != NULL; o = o->next) markobject(g, o); - } - return count; } @@ -360,8 +413,7 @@ static l_obj markbeingfnz (global_State *g) { ** upvalues, as they have nothing to be checked. (If the thread gets an ** upvalue later, it will be linked in the list again.) */ -static l_obj remarkupvals (global_State *g) { - l_obj work = 0; +static void remarkupvals (global_State *g) { lua_State *thread; lua_State **p = &g->twups; while ((thread = *p) != NULL) { @@ -380,9 +432,7 @@ static l_obj remarkupvals (global_State *g) { } } } - work++; } - return work; } @@ -401,7 +451,7 @@ static void cleargraylists (global_State *g) { */ static void restartcollection (global_State *g) { cleargraylists(g); - g->GCmarked = NFIXED; + g->GCmarked = 0; markobject(g, g->mainthread); markvalue(g, &g->l_registry); markmt(g); @@ -546,7 +596,7 @@ static void traversestrongtable (global_State *g, Table *h) { } -static void traversetable (global_State *g, Table *h) { +static l_mem traversetable (global_State *g, Table *h) { const char *weakkey, *weakvalue; const TValue *mode = gfasttm(g, h->metatable, TM_MODE); TString *smode; @@ -565,15 +615,17 @@ static void traversetable (global_State *g, Table *h) { } else /* not weak */ traversestrongtable(g, h); + return 1 + sizenode(h) + h->alimit; } -static void traverseudata (global_State *g, Udata *u) { +static l_mem traverseudata (global_State *g, Udata *u) { int i; markobjectN(g, u->metatable); /* mark its metatable */ for (i = 0; i < u->nuvalue; i++) markvalue(g, &u->uv[i].uv); genlink(g, obj2gco(u)); + return 1 + u->nuvalue; } @@ -582,7 +634,7 @@ static void traverseudata (global_State *g, Udata *u) { ** arrays can be larger than needed; the extra slots are filled with ** NULL, so the use of 'markobjectN') */ -static void traverseproto (global_State *g, Proto *f) { +static l_mem traverseproto (global_State *g, Proto *f) { int i; markobjectN(g, f->source); for (i = 0; i < f->sizek; i++) /* mark literals */ @@ -593,26 +645,29 @@ static void traverseproto (global_State *g, Proto *f) { markobjectN(g, f->p[i]); for (i = 0; i < f->sizelocvars; i++) /* mark local-variable names */ markobjectN(g, f->locvars[i].varname); + return 1 + f->sizek + f->sizeupvalues + f->sizep + f->sizelocvars; } -static void traverseCclosure (global_State *g, CClosure *cl) { +static l_mem traverseCclosure (global_State *g, CClosure *cl) { int i; for (i = 0; i < cl->nupvalues; i++) /* mark its upvalues */ markvalue(g, &cl->upvalue[i]); + return 1 + cl->nupvalues; } /* ** Traverse a Lua closure, marking its prototype and its upvalues. ** (Both can be NULL while closure is being created.) */ -static void traverseLclosure (global_State *g, LClosure *cl) { +static l_mem traverseLclosure (global_State *g, LClosure *cl) { int i; markobjectN(g, cl->p); /* mark its prototype */ for (i = 0; i < cl->nupvalues; i++) { /* visit its upvalues */ UpVal *uv = cl->upvals[i]; markobjectN(g, uv); /* mark upvalue */ } + return 1 + cl->nupvalues; } @@ -628,13 +683,13 @@ static void traverseLclosure (global_State *g, LClosure *cl) { ** (which can only happen in generational mode) or if the traverse is in ** the propagate phase (which can only happen in incremental mode). */ -static void traversethread (global_State *g, lua_State *th) { +static l_mem traversethread (global_State *g, lua_State *th) { UpVal *uv; StkId o = th->stack.p; if (isold(th) || g->gcstate == GCSpropagate) linkgclist(th, g->grayagain); /* insert into 'grayagain' list */ if (o == NULL) - return; /* stack not completely built yet */ + return 0; /* stack not completely built yet */ lua_assert(g->gcstate == GCSatomic || th->openupval == NULL || isintwups(th)); for (; o < th->top.p; o++) /* mark live elements in the stack */ @@ -652,35 +707,33 @@ static void traversethread (global_State *g, lua_State *th) { g->twups = th; } } + return 1 + (th->top.p - th->stack.p); } /* -** traverse one gray object, turning it to black. +** traverse one gray object, turning it to black. Return an estimate +** of the number of slots traversed. */ -static void propagatemark (global_State *g) { +static l_mem propagatemark (global_State *g) { GCObject *o = g->gray; nw2black(o); g->gray = *getgclist(o); /* remove from 'gray' list */ switch (o->tt) { - case LUA_VTABLE: traversetable(g, gco2t(o)); break; - case LUA_VUSERDATA: traverseudata(g, gco2u(o)); break; - case LUA_VLCL: traverseLclosure(g, gco2lcl(o)); break; - case LUA_VCCL: traverseCclosure(g, gco2ccl(o)); break; - case LUA_VPROTO: traverseproto(g, gco2p(o)); break; - case LUA_VTHREAD: traversethread(g, gco2th(o)); break; - default: lua_assert(0); + case LUA_VTABLE: return traversetable(g, gco2t(o)); + case LUA_VUSERDATA: return traverseudata(g, gco2u(o)); + case LUA_VLCL: return traverseLclosure(g, gco2lcl(o)); + case LUA_VCCL: return traverseCclosure(g, gco2ccl(o)); + case LUA_VPROTO: return traverseproto(g, gco2p(o)); + case LUA_VTHREAD: return traversethread(g, gco2th(o)); + default: lua_assert(0); return 0; } } -static l_obj propagateall (global_State *g) { - l_obj work = 0; - while (g->gray) { +static void propagateall (global_State *g) { + while (g->gray) propagatemark(g); - work++; - } - return work; } @@ -690,9 +743,8 @@ static l_obj propagateall (global_State *g) { ** inverts the direction of the traversals, trying to speed up ** convergence on chains in the same table. */ -static l_obj convergeephemerons (global_State *g) { +static void convergeephemerons (global_State *g) { int changed; - l_obj work = 0; int dir = 0; do { GCObject *w; @@ -707,11 +759,9 @@ static l_obj convergeephemerons (global_State *g) { propagateall(g); /* propagate changes */ changed = 1; /* will have to revisit all ephemeron tables */ } - work++; } dir = !dir; /* invert direction next time */ } while (changed); /* repeat until no more changes */ - return work; } /* }====================================================== */ @@ -727,8 +777,7 @@ static l_obj convergeephemerons (global_State *g) { /* ** clear entries with unmarked keys from all weaktables in list 'l' */ -static l_obj clearbykeys (global_State *g, GCObject *l) { - l_obj work = 0; +static void clearbykeys (global_State *g, GCObject *l) { for (; l; l = gco2t(l)->gclist) { Table *h = gco2t(l); Node *limit = gnodelast(h); @@ -739,9 +788,7 @@ static l_obj clearbykeys (global_State *g, GCObject *l) { if (isempty(gval(n))) /* is entry empty? */ clearkey(n); /* clear its key */ } - work++; } - return work; } @@ -749,8 +796,7 @@ static l_obj clearbykeys (global_State *g, GCObject *l) { ** clear entries with unmarked values from all weaktables in list 'l' up ** to element 'f' */ -static l_obj clearbyvalues (global_State *g, GCObject *l, GCObject *f) { - l_obj work = 0; +static void clearbyvalues (global_State *g, GCObject *l, GCObject *f) { for (; l != f; l = gco2t(l)->gclist) { Table *h = gco2t(l); Node *n, *limit = gnodelast(h); @@ -767,9 +813,7 @@ static l_obj clearbyvalues (global_State *g, GCObject *l, GCObject *f) { if (isempty(gval(n))) /* is entry empty? */ clearkey(n); /* clear its key */ } - work++; } - return work; } @@ -781,7 +825,6 @@ static void freeupval (lua_State *L, UpVal *uv) { static void freeobj (lua_State *L, GCObject *o) { - G(L)->GCtotalobjs--; switch (o->tt) { case LUA_VPROTO: luaF_freeproto(L, gco2p(o)); @@ -835,12 +878,11 @@ static void freeobj (lua_State *L, GCObject *o) { ** for next collection cycle. Return where to continue the traversal or ** NULL if list is finished. */ -static GCObject **sweeplist (lua_State *L, GCObject **p, l_obj countin) { +static GCObject **sweeplist (lua_State *L, GCObject **p, l_mem countin) { global_State *g = G(L); int ow = otherwhite(g); - l_obj i; int white = luaC_white(g); /* current white */ - for (i = 0; *p != NULL && i < countin; i++) { + while (*p != NULL && countin-- > 0) { GCObject *curr = *p; int marked = curr->marked; if (isdeadm(ow, marked)) { /* is 'curr' dead? */ @@ -1052,8 +1094,8 @@ void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt) { ** approximately (marked * pause / 100). */ static void setpause (global_State *g) { - l_obj threshold = applygcparam(g, PAUSE, g->GCmarked); - l_obj debt = threshold - gettotalobjs(g); + l_mem threshold = applygcparam(g, PAUSE, g->GCmarked); + l_mem debt = threshold - gettotalbytes(g); if (debt < 0) debt = 0; luaE_setdebt(g, debt); } @@ -1103,7 +1145,7 @@ static void sweep2old (lua_State *L, GCObject **p) { */ static GCObject **sweepgen (lua_State *L, global_State *g, GCObject **p, GCObject *limit, GCObject **pfirstold1, - l_obj *paddedold) { + l_mem *paddedold) { static const lu_byte nextage[] = { G_SURVIVAL, /* from G_NEW */ G_OLD1, /* from G_SURVIVAL */ @@ -1113,7 +1155,7 @@ static GCObject **sweepgen (lua_State *L, global_State *g, GCObject **p, G_TOUCHED1, /* from G_TOUCHED1 (do not change) */ G_TOUCHED2 /* from G_TOUCHED2 (do not change) */ }; - l_obj addedold = 0; + l_mem addedold = 0; int white = luaC_white(g); GCObject *curr; while ((curr = *p) != limit) { @@ -1132,7 +1174,7 @@ static GCObject **sweepgen (lua_State *L, global_State *g, GCObject **p, lua_assert(age != G_OLD1); /* advanced in 'markold' */ setage(curr, nextage[age]); if (getage(curr) == G_OLD1) { - addedold++; /* one more object becoming old */ + addedold += cast(l_mem, objsize(curr)); /* bytes becoming old */ if (*pfirstold1 == NULL) *pfirstold1 = curr; /* first OLD1 object in the list */ } @@ -1257,9 +1299,9 @@ static void minor2inc (lua_State *L, global_State *g, lu_byte kind) { ** than 'minormajor'% of the number of lived objects after the last ** major collection. (That percentage is computed in 'limit'.) */ -static int checkminormajor (global_State *g, l_obj addedold1) { - l_obj step = applygcparam(g, MINORMUL, g->GCmajorminor); - l_obj limit = applygcparam(g, MINORMAJOR, g->GCmajorminor); +static int checkminormajor (global_State *g, l_mem addedold1) { + l_mem step = applygcparam(g, MINORMUL, g->GCmajorminor); + l_mem limit = applygcparam(g, MINORMAJOR, g->GCmajorminor); return (addedold1 >= (step >> 1) || g->GCmarked >= limit); } @@ -1269,8 +1311,8 @@ static int checkminormajor (global_State *g, l_obj addedold1) { ** sweep all lists and advance pointers. Finally, finish the collection. */ static void youngcollection (lua_State *L, global_State *g) { - l_obj addedold1 = 0; - l_obj marked = g->GCmarked; /* preserve 'g->GCmarked' */ + l_mem addedold1 = 0; + l_mem marked = g->GCmarked; /* preserve 'g->GCmarked' */ GCObject **psurvival; /* to point to first non-dead survival object */ GCObject *dummy; /* dummy out parameter to 'sweepgen' */ lua_assert(g->gcstate == GCSpropagate); @@ -1346,7 +1388,9 @@ static void atomic2gen (lua_State *L, global_State *g) { /* ** Set debt for the next minor collection, which will happen when -** total number of objects grows 'genminormul'%. +** total number of bytes grows 'genminormul'% in relation to +** the base, GCmajorminor, which is the number of bytes being used +** after the last major collection. */ static void setminordebt (global_State *g) { luaE_setdebt(g, applygcparam(g, MINORMUL, g->GCmajorminor)); @@ -1404,18 +1448,18 @@ static void fullgen (lua_State *L, global_State *g) { */ static int checkmajorminor (lua_State *L, global_State *g) { if (g->gckind == KGC_GENMAJOR) { /* generational mode? */ - l_obj numobjs = gettotalobjs(g); - l_obj addedobjs = numobjs - g->GCmajorminor; - l_obj limit = applygcparam(g, MAJORMINOR, addedobjs); - l_obj tobecollected = numobjs - g->GCmarked; + l_mem numbytes = gettotalbytes(g); + l_mem addedobjs = numbytes - g->GCmajorminor; + l_mem limit = applygcparam(g, MAJORMINOR, addedobjs); + l_mem tobecollected = numbytes - g->GCmarked; if (tobecollected > limit) { atomic2gen(L, g); /* return to generational mode */ setminordebt(g); - return 0; /* exit incremental collection */ + return 1; /* exit incremental collection */ } } g->GCmajorminor = g->GCmarked; /* prepare for next collection */ - return 1; /* stay doing incremental collections */ + return 0; /* stay doing incremental collections */ } /* }====================================================== */ @@ -1474,8 +1518,7 @@ void luaC_freeallobjects (lua_State *L) { } -static l_obj atomic (lua_State *L) { - l_obj work = 0; +static void atomic (lua_State *L) { global_State *g = G(L); GCObject *origweak, *origall; GCObject *grayagain = g->grayagain; /* save original list */ @@ -1487,33 +1530,32 @@ static l_obj atomic (lua_State *L) { /* registry and global metatables may be changed by API */ markvalue(g, &g->l_registry); markmt(g); /* mark global metatables */ - work += propagateall(g); /* empties 'gray' list */ + propagateall(g); /* empties 'gray' list */ /* remark occasional upvalues of (maybe) dead threads */ - work += remarkupvals(g); - work += propagateall(g); /* propagate changes */ + remarkupvals(g); + propagateall(g); /* propagate changes */ g->gray = grayagain; - work += propagateall(g); /* traverse 'grayagain' list */ - work += convergeephemerons(g); + propagateall(g); /* traverse 'grayagain' list */ + convergeephemerons(g); /* at this point, all strongly accessible objects are marked. */ /* Clear values from weak tables, before checking finalizers */ - work += clearbyvalues(g, g->weak, NULL); - work += clearbyvalues(g, g->allweak, NULL); + clearbyvalues(g, g->weak, NULL); + clearbyvalues(g, g->allweak, NULL); origweak = g->weak; origall = g->allweak; separatetobefnz(g, 0); /* separate objects to be finalized */ - work += markbeingfnz(g); /* mark objects that will be finalized */ - work += propagateall(g); /* remark, to propagate 'resurrection' */ - work += convergeephemerons(g); + markbeingfnz(g); /* mark objects that will be finalized */ + propagateall(g); /* remark, to propagate 'resurrection' */ + convergeephemerons(g); /* at this point, all resurrected objects are marked. */ /* remove dead objects from weak tables */ - work += clearbykeys(g, g->ephemeron); /* clear keys from all ephemeron */ - work += clearbykeys(g, g->allweak); /* clear keys from all 'allweak' */ + clearbykeys(g, g->ephemeron); /* clear keys from all ephemeron */ + clearbykeys(g, g->allweak); /* clear keys from all 'allweak' */ /* clear values from resurrected weak tables */ - work += clearbyvalues(g, g->weak, origweak); - work += clearbyvalues(g, g->allweak, origall); + clearbyvalues(g, g->weak, origweak); + clearbyvalues(g, g->allweak, origall); luaS_clearcache(g); g->currentwhite = cast_byte(otherwhite(g)); /* flip current white */ lua_assert(g->gray == NULL); - return work; } @@ -1524,7 +1566,7 @@ static l_obj atomic (lua_State *L) { static void sweepstep (lua_State *L, global_State *g, lu_byte nextstate, GCObject **nextlist, int fast) { if (g->sweepgc) - g->sweepgc = sweeplist(L, g->sweepgc, fast ? MAX_LOBJ : GCSWEEPMAX); + g->sweepgc = sweeplist(L, g->sweepgc, fast ? MAX_LMEM : GCSWEEPMAX); else { /* enter next state */ g->gcstate = nextstate; g->sweepgc = nextlist; @@ -1544,72 +1586,80 @@ static void sweepstep (lua_State *L, global_State *g, ** That avoids traversing twice some objects, such as threads and ** weak tables. */ -static l_obj singlestep (lua_State *L, int fast) { + +#define step2pause -3 /* finished collection; entered pause state */ +#define atomicstep -2 /* atomic step */ +#define step2minor -1 /* moved to minor collections */ + + +static l_mem singlestep (lua_State *L, int fast) { global_State *g = G(L); - l_obj work; + l_mem stepresult; lua_assert(!g->gcstopem); /* collector is not reentrant */ g->gcstopem = 1; /* no emergency collections while collecting */ switch (g->gcstate) { case GCSpause: { restartcollection(g); g->gcstate = GCSpropagate; - work = 1; + stepresult = 1; break; } case GCSpropagate: { if (fast || g->gray == NULL) { g->gcstate = GCSenteratomic; /* finish propagate phase */ - work = 0; - } - else { - propagatemark(g); /* traverse one gray object */ - work = 1; + stepresult = 1; } + else + stepresult = propagatemark(g); /* traverse one gray object */ break; } case GCSenteratomic: { - work = atomic(L); + atomic(L); if (checkmajorminor(L, g)) + stepresult = step2minor; + else { entersweep(L); + stepresult = atomicstep; + } break; } case GCSswpallgc: { /* sweep "regular" objects */ sweepstep(L, g, GCSswpfinobj, &g->finobj, fast); - work = GCSWEEPMAX; + stepresult = GCSWEEPMAX; break; } case GCSswpfinobj: { /* sweep objects with finalizers */ sweepstep(L, g, GCSswptobefnz, &g->tobefnz, fast); - work = GCSWEEPMAX; + stepresult = GCSWEEPMAX; break; } case GCSswptobefnz: { /* sweep objects to be finalized */ sweepstep(L, g, GCSswpend, NULL, fast); - work = GCSWEEPMAX; + stepresult = GCSWEEPMAX; break; } case GCSswpend: { /* finish sweeps */ checkSizes(L, g); g->gcstate = GCScallfin; - work = 0; + stepresult = GCSWEEPMAX; break; } case GCScallfin: { /* call finalizers */ if (g->tobefnz && !g->gcemergency) { g->gcstopem = 0; /* ok collections during finalizers */ GCTM(L); /* call one finalizer */ - work = 1; + stepresult = CWUFIN; } else { /* emergency mode or no more finalizers */ g->gcstate = GCSpause; /* finish collection */ - work = 0; + stepresult = step2pause; } break; } default: lua_assert(0); return 0; } g->gcstopem = 0; - return work; + return stepresult; } @@ -1635,25 +1685,26 @@ void luaC_runtilstate (lua_State *L, int state, int fast) { ** controls when next step will be performed. */ static void incstep (lua_State *L, global_State *g) { - l_obj stepsize = applygcparam(g, STEPSIZE, 100); - l_obj work2do = applygcparam(g, STEPMUL, stepsize); - int fast = 0; - if (work2do == 0) { /* special case: do a full collection */ - work2do = MAX_LOBJ; /* do unlimited work */ - fast = 1; - } - do { /* repeat until pause or enough work */ - l_obj work = singlestep(L, fast); /* perform one single step */ - if (g->gckind == KGC_GENMINOR) /* returned to minor collections? */ + l_mem stepsize = applygcparam(g, STEPSIZE, 100); + l_mem work2do = applygcparam(g, STEPMUL, stepsize); + l_mem stres; + int fast = (work2do == 0); /* special case: do a full collection */ + do { /* repeat until enough work */ + stres = singlestep(L, fast); /* perform one single step */ + if (stres == step2minor) /* returned to minor collections? */ return; /* nothing else to be done here */ - work2do -= work; - } while (work2do > 0 && g->gcstate != GCSpause); + else if (stres == step2pause || (stres == atomicstep && !fast)) + break; /* end of cycle or atomic */ + else + work2do -= stres; + } while (fast || work2do > 0); if (g->gcstate == GCSpause) setpause(g); /* pause until next cycle */ else luaE_setdebt(g, stepsize); } + /* ** Performs a basic GC step if collector is running. (If collector is ** not running, set a reasonable debt to avoid it being called at @@ -1663,17 +1714,23 @@ void luaC_step (lua_State *L) { global_State *g = G(L); lua_assert(!g->gcemergency); if (!gcrunning(g)) /* not running? */ - luaE_setdebt(g, 2000); + luaE_setdebt(g, 20000); else { +// printf("mem: %ld kind: %s ", gettotalbytes(g), +// g->gckind == KGC_INC ? "inc" : g->gckind == KGC_GENMAJOR ? "genmajor" : +// "genminor"); switch (g->gckind) { case KGC_INC: case KGC_GENMAJOR: +// printf("(%d -> ", g->gcstate); incstep(L, g); +// printf("%d) ", g->gcstate); break; case KGC_GENMINOR: youngcollection(L, g); setminordebt(g); break; } +// printf("-> mem: %ld debt: %ld\n", gettotalbytes(g), g->GCdebt); } } @@ -1692,7 +1749,7 @@ static void fullinc (lua_State *L, global_State *g) { luaC_runtilstate(L, GCSpause, 1); luaC_runtilstate(L, GCScallfin, 1); /* run up to finalizers */ /* 'marked' must be correct after a full GC cycle */ - lua_assert(g->GCmarked == gettotalobjs(g)); + /* lua_assert(g->GCmarked == gettotalobjs(g)); ??? */ luaC_runtilstate(L, GCSpause, 1); /* finish collection */ setpause(g); } diff --git a/lgc.h b/lgc.h index a30755d05a..0b16ac7f6d 100644 --- a/lgc.h +++ b/lgc.h @@ -23,8 +23,9 @@ ** never point to a white one. Moreover, any gray object must be in a ** "gray list" (gray, grayagain, weak, allweak, ephemeron) so that it ** can be visited again before finishing the collection cycle. (Open -** upvalues are an exception to this rule.) These lists have no meaning -** when the invariant is not being enforced (e.g., sweep phase). +** upvalues are an exception to this rule, as they are attached to +** a corresponding thread.) These lists have no meaning when the +** invariant is not being enforced (e.g., sweep phase). */ @@ -48,10 +49,10 @@ /* ** macro to tell when main invariant (white objects cannot point to black -** ones) must be kept. During a collection, the sweep -** phase may break the invariant, as objects turned white may point to -** still-black objects. The invariant is restored when sweep ends and -** all objects are white again. +** ones) must be kept. During a collection, the sweep phase may break +** the invariant, as objects turned white may point to still-black +** objects. The invariant is restored when sweep ends and all objects +** are white again. */ #define keepinvariant(g) ((g)->gcstate <= GCSatomic) @@ -163,34 +164,37 @@ /* ** Minor collections will shift to major ones after LUAI_MINORMAJOR% -** objects become old. +** bytes become old. */ #define LUAI_MINORMAJOR 100 /* ** Major collections will shift to minor ones after a collection -** collects at least LUAI_MAJORMINOR% of the new objects. +** collects at least LUAI_MAJORMINOR% of the new bytes. */ #define LUAI_MAJORMINOR 50 /* ** A young (minor) collection will run after creating LUAI_GENMINORMUL% -** new objects. +** new bytes. */ #define LUAI_GENMINORMUL 25 /* incremental */ -/* Number of objects must be LUAI_GCPAUSE% before starting new cycle */ +/* Number of bytes must be LUAI_GCPAUSE% before starting new cycle */ #define LUAI_GCPAUSE 200 -/* Step multiplier. (Roughly, the collector handles LUAI_GCMUL% objects - for each new allocated object.) */ -#define LUAI_GCMUL 200 +/* +** Step multiplier: The collector handles LUAI_GCMUL% work units for +** each new allocated byte. (Each "work unit" corresponds roughly to +** sweeping or marking one object.) +*/ +#define LUAI_GCMUL 20 /* ??? */ -/* How many objects to allocate before next GC step */ -#define LUAI_GCSTEPSIZE 250 +/* How many bytes to allocate before next GC step */ +#define LUAI_GCSTEPSIZE (250 * sizeof(void*)) #define setgcparam(g,p,v) (g->gcparams[LUA_GCP##p] = luaO_codeparam(v)) diff --git a/llimits.h b/llimits.h index d7ae065b17..f189048ca7 100644 --- a/llimits.h +++ b/llimits.h @@ -16,25 +16,24 @@ /* -** 'lu_mem' is an unsigned integer big enough to count the total memory -** used by Lua (in bytes). 'l_obj' is a signed integer big enough to -** count the total number of objects used by Lua. (It is signed due -** to the use of debt in several computations.) Usually, 'size_t' and -** 'ptrdiff_t' should work, but we use 'long' for 16-bit machines. +** 'l_mem' is a signed integer big enough to count the total memory +** used by Lua. (It is signed due to the use of debt in several +** computations.) Usually, 'ptrdiff_t' should work, but we use 'long' +** for 16-bit machines. */ #if defined(LUAI_MEM) /* { external definitions? */ +typedef LUAI_MEM l_mem; typedef LUAI_UMEM lu_mem; -typedef LUAI_MEM l_obj; #elif LUAI_IS32INT /* }{ */ +typedef ptrdiff_t l_mem; typedef size_t lu_mem; -typedef ptrdiff_t l_obj; #else /* 16-bit ints */ /* }{ */ +typedef long l_mem; typedef unsigned long lu_mem; -typedef long l_obj; #endif /* } */ -#define MAX_LOBJ \ - cast(l_obj, (cast(lu_mem, 1) << (sizeof(l_obj) * CHAR_BIT - 1)) - 1) +#define MAX_LMEM \ + cast(l_mem, (cast(lu_mem, 1) << (sizeof(l_mem) * 8 - 1)) - 1) /* chars used as small naturals (so that 'char' is reserved for characters) */ diff --git a/lmem.c b/lmem.c index f18ea17211..de8503d91b 100644 --- a/lmem.c +++ b/lmem.c @@ -151,7 +151,7 @@ void luaM_free_ (lua_State *L, void *block, size_t osize) { global_State *g = G(L); lua_assert((osize == 0) == (block == NULL)); callfrealloc(g, block, osize, 0); - g->GCtotalbytes -= osize; + g->GCdebt += cast(l_mem, osize); } @@ -181,10 +181,10 @@ void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) { if (l_unlikely(newblock == NULL && nsize > 0)) { newblock = tryagain(L, block, osize, nsize); if (newblock == NULL) /* still no memory? */ - return NULL; /* do not update 'GCtotalbytes' */ + return NULL; /* do not update 'GCdebt' */ } lua_assert((nsize == 0) == (newblock == NULL)); - g->GCtotalbytes += nsize - osize; + g->GCdebt -= cast(l_mem, nsize) - cast(l_mem, osize); return newblock; } @@ -209,7 +209,7 @@ void *luaM_malloc_ (lua_State *L, size_t size, int tag) { if (newblock == NULL) luaM_error(L); } - g->GCtotalbytes += size; + g->GCdebt -= cast(l_mem, size); return newblock; } } diff --git a/lobject.c b/lobject.c index f71595478f..1ca03e762d 100644 --- a/lobject.c +++ b/lobject.c @@ -85,7 +85,7 @@ lu_byte luaO_codeparam (unsigned int p) { ** more significant bits, as long as the multiplication does not ** overflow, so we check which order is best. */ -l_obj luaO_applyparam (lu_byte p, l_obj x) { +l_mem luaO_applyparam (lu_byte p, l_mem x) { unsigned int m = p & 0xF; /* mantissa */ int e = (p >> 4); /* exponent */ if (e > 0) { /* normalized? */ @@ -94,19 +94,19 @@ l_obj luaO_applyparam (lu_byte p, l_obj x) { } e -= 7; /* correct excess-7 */ if (e >= 0) { - if (x < (MAX_LOBJ / 0x1F) >> e) /* no overflow? */ + if (x < (MAX_LMEM / 0x1F) >> e) /* no overflow? */ return (x * m) << e; /* order doesn't matter here */ else /* real overflow */ - return MAX_LOBJ; + return MAX_LMEM; } else { /* negative exponent */ e = -e; - if (x < MAX_LOBJ / 0x1F) /* multiplication cannot overflow? */ + if (x < MAX_LMEM / 0x1F) /* multiplication cannot overflow? */ return (x * m) >> e; /* multiplying first gives more precision */ - else if ((x >> e) < MAX_LOBJ / 0x1F) /* cannot overflow after shift? */ + else if ((x >> e) < MAX_LMEM / 0x1F) /* cannot overflow after shift? */ return (x >> e) * m; else /* real overflow */ - return MAX_LOBJ; + return MAX_LMEM; } } diff --git a/lobject.h b/lobject.h index fb66dff7c9..2411410b4d 100644 --- a/lobject.h +++ b/lobject.h @@ -838,7 +838,7 @@ typedef struct Table { LUAI_FUNC int luaO_utf8esc (char *buff, unsigned long x); LUAI_FUNC lu_byte luaO_ceillog2 (unsigned int x); LUAI_FUNC lu_byte luaO_codeparam (unsigned int p); -LUAI_FUNC l_obj luaO_applyparam (lu_byte p, l_obj x); +LUAI_FUNC l_mem luaO_applyparam (lu_byte p, l_mem x); LUAI_FUNC int luaO_rawarith (lua_State *L, int op, const TValue *p1, const TValue *p2, TValue *res); diff --git a/lstate.c b/lstate.c index f4c9081dfd..8e7c8b8605 100644 --- a/lstate.c +++ b/lstate.c @@ -77,12 +77,12 @@ typedef struct LG { ** objects (GCtotalobjs - GCdebt) invariant and avoiding overflows in ** 'GCtotalobjs'. */ -void luaE_setdebt (global_State *g, l_obj debt) { - l_obj tb = gettotalobjs(g); +void luaE_setdebt (global_State *g, l_mem debt) { + l_mem tb = gettotalbytes(g); lua_assert(tb > 0); - if (debt > MAX_LOBJ - tb) - debt = MAX_LOBJ - tb; /* will make GCtotalobjs == MAX_LOBJ */ - g->GCtotalobjs = tb + debt; + if (debt > MAX_LMEM - tb) + debt = MAX_LMEM - tb; /* will make GCtotalbytes == MAX_LMEM */ + g->GCtotalbytes = tb + debt; g->GCdebt = debt; } @@ -269,8 +269,7 @@ static void close_state (lua_State *L) { } luaM_freearray(L, G(L)->strt.hash, cast_sizet(G(L)->strt.size)); freestack(L); - lua_assert(g->GCtotalbytes == sizeof(LG)); - lua_assert(gettotalobjs(g) == 1); + lua_assert(gettotalbytes(g) == sizeof(LG)); (*g->frealloc)(g->ud, fromstate(L), sizeof(LG), 0); /* free main block */ } @@ -379,7 +378,6 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud, unsigned seed) { g->weak = g->ephemeron = g->allweak = NULL; g->twups = NULL; g->GCtotalbytes = sizeof(LG); - g->GCtotalobjs = 1; g->GCmarked = 0; g->GCdebt = 0; setivalue(&g->nilvalue, 0); /* to signal that state is not yet built */ diff --git a/lstate.h b/lstate.h index 6aa02889a0..2a03576dd4 100644 --- a/lstate.h +++ b/lstate.h @@ -274,11 +274,10 @@ struct CallInfo { typedef struct global_State { lua_Alloc frealloc; /* function to reallocate memory */ void *ud; /* auxiliary data to 'frealloc' */ - lu_mem GCtotalbytes; /* number of bytes currently allocated */ - l_obj GCtotalobjs; /* total number of objects allocated + GCdebt */ - l_obj GCdebt; /* objects counted but not yet allocated */ - l_obj GCmarked; /* number of objects marked in a GC cycle */ - l_obj GCmajorminor; /* auxiliary counter to control major-minor shifts */ + l_mem GCtotalbytes; /* number of bytes currently allocated + debt */ + l_mem GCdebt; /* bytes counted but not yet allocated */ + l_mem GCmarked; /* number of objects marked in a GC cycle */ + l_mem GCmajorminor; /* auxiliary counter to control major-minor shifts */ stringtable strt; /* hash table for strings */ TValue l_registry; TValue nilvalue; /* a nil value */ @@ -411,11 +410,11 @@ union GCUnion { #define obj2gco(v) check_exp((v)->tt >= LUA_TSTRING, &(cast_u(v)->gc)) -/* actual number of total objects allocated */ -#define gettotalobjs(g) ((g)->GCtotalobjs - (g)->GCdebt) +/* actual number of total memory allocated */ +#define gettotalbytes(g) ((g)->GCtotalbytes - (g)->GCdebt) -LUAI_FUNC void luaE_setdebt (global_State *g, l_obj debt); +LUAI_FUNC void luaE_setdebt (global_State *g, l_mem debt); LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1); LUAI_FUNC CallInfo *luaE_extendCI (lua_State *L); LUAI_FUNC void luaE_shrinkCI (lua_State *L); diff --git a/ltests.c b/ltests.c index 7d134e2da3..91bce2a12e 100644 --- a/ltests.c +++ b/ltests.c @@ -537,7 +537,7 @@ static void checkobject (global_State *g, GCObject *o, int maybedead, } -static l_obj checkgraylist (global_State *g, GCObject *o) { +static l_mem checkgraylist (global_State *g, GCObject *o) { int total = 0; /* count number of elements in the list */ cast_void(g); /* better to keep it if we need to print an object */ while (o) { @@ -566,8 +566,8 @@ static l_obj checkgraylist (global_State *g, GCObject *o) { /* ** Check objects in gray lists. */ -static l_obj checkgrays (global_State *g) { - l_obj total = 0; /* count number of elements in all lists */ +static l_mem checkgrays (global_State *g) { + l_mem total = 0; /* count number of elements in all lists */ if (!keepinvariant(g)) return total; total += checkgraylist(g, g->gray); total += checkgraylist(g, g->grayagain); @@ -583,7 +583,7 @@ static l_obj checkgrays (global_State *g) { ** 'count' and check its TESTBIT. (It must have been previously set by ** 'checkgraylist'.) */ -static void incifingray (global_State *g, GCObject *o, l_obj *count) { +static void incifingray (global_State *g, GCObject *o, l_mem *count) { if (!keepinvariant(g)) return; /* gray lists not being kept in these phases */ if (o->tt == LUA_VUPVAL) { @@ -600,10 +600,10 @@ static void incifingray (global_State *g, GCObject *o, l_obj *count) { } -static l_obj checklist (global_State *g, int maybedead, int tof, +static l_mem checklist (global_State *g, int maybedead, int tof, GCObject *newl, GCObject *survival, GCObject *old, GCObject *reallyold) { GCObject *o; - l_obj total = 0; /* number of object that should be in gray lists */ + l_mem total = 0; /* number of object that should be in gray lists */ for (o = newl; o != survival; o = o->next) { checkobject(g, o, maybedead, G_NEW); incifingray(g, o, &total); @@ -632,8 +632,8 @@ int lua_checkmemory (lua_State *L) { global_State *g = G(L); GCObject *o; int maybedead; - l_obj totalin; /* total of objects that are in gray lists */ - l_obj totalshould; /* total of objects that should be in gray lists */ + l_mem totalin; /* total of objects that are in gray lists */ + l_mem totalshould; /* total of objects that should be in gray lists */ if (keepinvariant(g)) { assert(!iswhite(g->mainthread)); assert(!iswhite(gcvalue(&g->l_registry))); @@ -1040,7 +1040,7 @@ static int table_query (lua_State *L) { static int query_GCparams (lua_State *L) { global_State *g = G(L); - lua_pushinteger(L, cast(lua_Integer, gettotalobjs(g))); + lua_pushinteger(L, cast(lua_Integer, gettotalbytes(g))); lua_pushinteger(L, cast(lua_Integer, g->GCdebt)); lua_pushinteger(L, cast(lua_Integer, applygcparam(g, MINORMUL, 100))); lua_pushinteger(L, cast(lua_Integer, applygcparam(g, MAJORMINOR, 100))); From 9b72355f993567facefec570fd95c8909de33393 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 19 Sep 2024 19:06:16 -0300 Subject: [PATCH 0948/1145] USHRT_MAX changed to SHRT_MAX USHRT_MAX does not fit in an 'int' in 16-bit systems. --- lapi.c | 2 +- lparser.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lapi.c b/lapi.c index c493609a7d..40db1214cd 100644 --- a/lapi.c +++ b/lapi.c @@ -1340,7 +1340,7 @@ void lua_warning (lua_State *L, const char *msg, int tocont) { LUA_API void *lua_newuserdatauv (lua_State *L, size_t size, int nuvalue) { Udata *u; lua_lock(L); - api_check(L, 0 <= nuvalue && nuvalue < USHRT_MAX, "invalid value"); + api_check(L, 0 <= nuvalue && nuvalue < SHRT_MAX, "invalid value"); u = luaS_newudata(L, size, cast(unsigned short, nuvalue)); setuvalue(L, s2v(L->top.p), u); api_incr_top(L); diff --git a/lparser.c b/lparser.c index b193b67251..3db7df4cc5 100644 --- a/lparser.c +++ b/lparser.c @@ -199,7 +199,7 @@ static int new_localvarkind (LexState *ls, TString *name, lu_byte kind) { luaY_checklimit(fs, dyd->actvar.n + 1 - fs->firstlocal, MAXVARS, "local variables"); luaM_growvector(L, dyd->actvar.arr, dyd->actvar.n + 1, - dyd->actvar.size, Vardesc, USHRT_MAX, "local variables"); + dyd->actvar.size, Vardesc, SHRT_MAX, "local variables"); var = &dyd->actvar.arr[dyd->actvar.n++]; var->vd.kind = kind; /* default */ var->vd.name = name; From 8fac494509523dba479640a3e9b60c6f929b0788 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 19 Sep 2024 19:09:35 -0300 Subject: [PATCH 0949/1145] Avoid Microsoft warning > warning C4334: '<<': result of 32-bit shift implicitly converted to > 64 bits (was 64-bit shift intended?) --- ltable.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ltable.c b/ltable.c index c2d957e90d..80a1bf84c2 100644 --- a/ltable.c +++ b/ltable.c @@ -402,7 +402,8 @@ int luaH_next (lua_State *L, Table *t, StkId key) { static void freehash (lua_State *L, Table *t) { if (!isdummy(t)) { - size_t bsize = sizenode(t) * sizeof(Node); /* 'node' size in bytes */ + /* 'node' size in bytes */ + size_t bsize = cast_sizet(sizenode(t)) * sizeof(Node); char *arr = cast_charp(t->node); if (haslastfree(t)) { bsize += sizeof(Limbox); From 45a8f1b59310f9db74d9fc17264be2c2b2a06217 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 20 Sep 2024 09:43:46 -0300 Subject: [PATCH 0950/1145] Removed 'if' left from commit ddfa1fbccfe --- lapi.c | 1 - 1 file changed, 1 deletion(-) diff --git a/lapi.c b/lapi.c index 40db1214cd..7980ead00a 100644 --- a/lapi.c +++ b/lapi.c @@ -545,7 +545,6 @@ LUA_API const char *lua_pushextlstring (lua_State *L, ts = luaS_newextlstr (L, s, len, falloc, ud); setsvalue2s(L, L->top.p, ts); api_incr_top(L); - if (falloc != NULL) /* non-static string? */ luaC_checkGC(L); lua_unlock(L); return getstr(ts); From 00e34375ec7996422a617b0e99024d42ba61f634 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 20 Sep 2024 10:06:06 -0300 Subject: [PATCH 0951/1145] In 'luaO_pushvfstring', all options use 'addstr2buff' --- lobject.c | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/lobject.c b/lobject.c index 1ca03e762d..4dd94f4577 100644 --- a/lobject.c +++ b/lobject.c @@ -529,9 +529,6 @@ static char *getbuff (BuffFS *buff, unsigned sz) { } -#define addsize(b,sz) ((b)->blen += (sz)) - - /* ** Add 'str' to the buffer. If string is larger than the buffer space, ** push the string directly to the stack. @@ -540,7 +537,7 @@ static void addstr2buff (BuffFS *buff, const char *str, size_t slen) { if (slen <= BUFVFS) { /* does string fit into buffer? */ char *bf = getbuff(buff, cast_uint(slen)); memcpy(bf, str, slen); /* add string to buffer */ - addsize(buff, cast_uint(slen)); + buff->blen += cast_uint(slen); } else { /* string larger than buffer */ clearbuff(buff); /* string comes after buffer's content */ @@ -553,9 +550,9 @@ static void addstr2buff (BuffFS *buff, const char *str, size_t slen) { ** Add a numeral to the buffer. */ static void addnum2buff (BuffFS *buff, TValue *num) { - char *numbuff = getbuff(buff, MAXNUMBER2STR); + char numbuff[MAXNUMBER2STR]; unsigned len = tostringbuff(num, numbuff); /* format number into 'numbuff' */ - addsize(buff, len); + addstr2buff(buff, numbuff, len); } @@ -601,11 +598,10 @@ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) { break; } case 'p': { /* a pointer */ - const unsigned sz = 3 * sizeof(void*) + 8; /* enough space for '%p' */ - char *bf = getbuff(&buff, sz); + char bf[MAXNUMBER2STR]; /* enough space for '%p' */ void *p = va_arg(argp, void *); - int len = lua_pointer2str(bf, sz, p); - addsize(&buff, cast_uint(len)); + int len = lua_pointer2str(bf, MAXNUMBER2STR, p); + addstr2buff(&buff, bf, cast_uint(len)); break; } case 'U': { /* an 'unsigned long' as a UTF-8 sequence */ @@ -619,8 +615,8 @@ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) { break; } default: { - luaG_runerror(L, "invalid option '%%%c' to 'lua_pushfstring'", - *(e + 1)); + addstr2buff(&buff, e, 2); /* keep unknown format in the result */ + break; } } fmt = e + 2; /* skip '%' and the specifier */ From 70d6975018c1f2b8ce34058a4d54a28a3fafca66 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 20 Sep 2024 12:21:11 -0300 Subject: [PATCH 0952/1145] Towards no errors in 'luaO_pushvfstring' Any call to 'va_start' must have a corresponding call to 'va_end'; so, functions called between them (luaO_pushvfstring in particular) cannot raise errors. --- lobject.c | 121 +++++++++++++++++++++++++++++++----------------------- 1 file changed, 69 insertions(+), 52 deletions(-) diff --git a/lobject.c b/lobject.c index 4dd94f4577..0116e01b63 100644 --- a/lobject.c +++ b/lobject.c @@ -475,74 +475,94 @@ void luaO_tostring (lua_State *L, TValue *obj) { /* ** Size for buffer space used by 'luaO_pushvfstring'. It should be ** (LUA_IDSIZE + MAXNUMBER2STR) + a minimal space for basic messages, -** so that 'luaG_addinfo' can work directly on the buffer. +** so that 'luaG_addinfo' can work directly on the static buffer. */ #define BUFVFS cast_uint(LUA_IDSIZE + MAXNUMBER2STR + 95) -/* buffer used by 'luaO_pushvfstring' */ +/* +** Buffer used by 'luaO_pushvfstring'. 'err' signals any error while +** building result (memory error [1] or buffer overflow [2]). +*/ typedef struct BuffFS { lua_State *L; - int pushed; /* true if there is a part of the result on the stack */ - unsigned blen; /* length of partial string in 'space' */ - char space[BUFVFS]; /* holds last part of the result */ + char *b; + size_t buffsize; + size_t blen; /* length of string in 'buff' */ + int err; + char space[BUFVFS]; /* initial buffer */ } BuffFS; -/* -** Push given string to the stack, as part of the result, and -** join it to previous partial result if there is one. -** It may call 'luaV_concat' while using one slot from EXTRA_STACK. -** This call cannot invoke metamethods, as both operands must be -** strings. It can, however, raise an error if the result is too -** long. In that case, 'luaV_concat' frees the extra slot before -** raising the error. -*/ -static void pushstr (BuffFS *buff, const char *str, size_t lstr) { - lua_State *L = buff->L; - setsvalue2s(L, L->top.p, luaS_newlstr(L, str, lstr)); - L->top.p++; /* may use one slot from EXTRA_STACK */ - if (!buff->pushed) /* no previous string on the stack? */ - buff->pushed = 1; /* now there is one */ - else /* join previous string with new one */ - luaV_concat(L, 2); +static void initbuff (lua_State *L, BuffFS *buff) { + buff->L = L; + buff->b = buff->space; + buff->buffsize = sizeof(buff->space); + buff->blen = 0; + buff->err = 0; } /* -** empty the buffer space into the stack +** Push final result from 'luaO_pushvfstring'. This function may raise +** errors explicitly or through memory errors, so it must run protected. */ -static void clearbuff (BuffFS *buff) { - pushstr(buff, buff->space, buff->blen); /* push buffer contents */ - buff->blen = 0; /* space now is empty */ +static void pushbuff (lua_State *L, void *ud) { + BuffFS *buff = cast(BuffFS*, ud); + switch (buff->err) { + case 1: + luaD_throw(L, LUA_ERRMEM); + break; + case 2: + luaG_runerror(L, "buffer overflow"); + break; + default: { /* no errors */ + TString *ts = luaS_newlstr(L, buff->b, buff->blen); + setsvalue2s(L, L->top.p, ts); + L->top.p++; + } + } } -/* -** Get a space of size 'sz' in the buffer. If buffer has not enough -** space, empty it. 'sz' must fit in an empty buffer. -*/ -static char *getbuff (BuffFS *buff, unsigned sz) { - lua_assert(buff->blen <= BUFVFS); lua_assert(sz <= BUFVFS); - if (sz > BUFVFS - buff->blen) /* not enough space? */ - clearbuff(buff); - return buff->space + buff->blen; +static const char *clearbuff (BuffFS *buff) { + lua_State *L = buff->L; + const char *res; + pushbuff(L, buff); + res = getstr(tsvalue(s2v(L->top.p - 1))); + if (buff->b != buff->space) /* using dynamic buffer? */ + luaM_freearray(L, buff->b, buff->buffsize); /* free it */ + return res; } -/* -** Add 'str' to the buffer. If string is larger than the buffer space, -** push the string directly to the stack. -*/ static void addstr2buff (BuffFS *buff, const char *str, size_t slen) { - if (slen <= BUFVFS) { /* does string fit into buffer? */ - char *bf = getbuff(buff, cast_uint(slen)); - memcpy(bf, str, slen); /* add string to buffer */ - buff->blen += cast_uint(slen); - } - else { /* string larger than buffer */ - clearbuff(buff); /* string comes after buffer's content */ - pushstr(buff, str, slen); /* push string */ + if (buff->err) /* do nothing else after an error */ + return; + if (slen > buff->buffsize - buff->blen) { + /* new string doesn't fit into current buffer */ + if (slen > ((MAX_SIZE/2) - buff->blen)) { /* overflow? */ + buff->err = 2; + return; + } + else { + size_t newsize = buff->buffsize + slen; /* limited to MAX_SIZE/2 */ + char *newb = + (buff->b == buff->space) /* still using static space? */ + ? luaM_reallocvector(buff->L, NULL, 0, newsize, char) + : luaM_reallocvector(buff->L, buff->b, buff->buffsize, newsize, + char); + if (newb == NULL) { /* allocation error? */ + buff->err = 1; + return; + } + if (buff->b == buff->space) + memcpy(newb, buff->b, buff->blen); /* copy previous content */ + buff->b = newb; + buff->buffsize = newsize; + } } + memcpy(buff->b + buff->blen, str, slen); /* copy new content */ + buff->blen += slen; } @@ -563,8 +583,7 @@ static void addnum2buff (BuffFS *buff, TValue *num) { const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) { BuffFS buff; /* holds last part of the result */ const char *e; /* points to next '%' */ - buff.pushed = 0; buff.blen = 0; - buff.L = L; + initbuff(L, &buff); while ((e = strchr(fmt, '%')) != NULL) { addstr2buff(&buff, fmt, ct_diff2sz(e - fmt)); /* add 'fmt' up to '%' */ switch (*(e + 1)) { /* conversion specifier */ @@ -622,9 +641,7 @@ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) { fmt = e + 2; /* skip '%' and the specifier */ } addstr2buff(&buff, fmt, strlen(fmt)); /* rest of 'fmt' */ - clearbuff(&buff); /* empty buffer into the stack */ - lua_assert(buff.pushed == 1); - return getstr(tsvalue(s2v(L->top.p - 1))); + return clearbuff(&buff); /* empty buffer into a new string */ } From 20d42ccaaed9a84783d548d76633a5a38f0091f1 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 20 Sep 2024 15:56:39 -0300 Subject: [PATCH 0953/1145] No errors in 'luaO_pushvfstring' Any call to 'va_start' must have a corresponding call to 'va_end'; so, functions called between them (luaO_pushvfstring in particular) cannot raise errors. --- lapi.c | 2 ++ lauxlib.c | 2 +- ldebug.c | 3 ++- lobject.c | 37 ++++++++++++++++++++++++------------- manual/manual.of | 25 ++++++++++++++++--------- 5 files changed, 45 insertions(+), 24 deletions(-) diff --git a/lapi.c b/lapi.c index 7980ead00a..fffd7d262a 100644 --- a/lapi.c +++ b/lapi.c @@ -587,6 +587,8 @@ LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) { ret = luaO_pushvfstring(L, fmt, argp); va_end(argp); luaC_checkGC(L); + if (ret == NULL) /* error? */ + luaD_throw(L, LUA_ERRMEM); lua_unlock(L); return ret; } diff --git a/lauxlib.c b/lauxlib.c index defd4d578e..a36655f2bb 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -225,7 +225,7 @@ LUALIB_API void luaL_where (lua_State *L, int level) { /* ** Again, the use of 'lua_pushvfstring' ensures this function does ** not need reserved stack space when called. (At worst, it generates -** an error with "stack overflow" instead of the given message.) +** a memory error instead of the given message.) */ LUALIB_API int luaL_error (lua_State *L, const char *fmt, ...) { va_list argp; diff --git a/ldebug.c b/ldebug.c index 9e341f1164..d1b47c565d 100644 --- a/ldebug.c +++ b/ldebug.c @@ -847,7 +847,8 @@ l_noret luaG_runerror (lua_State *L, const char *fmt, ...) { va_start(argp, fmt); msg = luaO_pushvfstring(L, fmt, argp); /* format message */ va_end(argp); - if (isLua(ci)) { /* if Lua function, add source:line information */ + if (msg != NULL && isLua(ci)) { /* Lua function? (and no error) */ + /* add source:line information */ luaG_addinfo(L, msg, ci_func(ci)->p->source, getcurrentline(ci)); setobjs2s(L, L->top.p - 2, L->top.p - 1); /* remove 'msg' */ L->top.p--; diff --git a/lobject.c b/lobject.c index 0116e01b63..ba10189de0 100644 --- a/lobject.c +++ b/lobject.c @@ -480,7 +480,7 @@ void luaO_tostring (lua_State *L, TValue *obj) { #define BUFVFS cast_uint(LUA_IDSIZE + MAXNUMBER2STR + 95) /* -** Buffer used by 'luaO_pushvfstring'. 'err' signals any error while +** Buffer used by 'luaO_pushvfstring'. 'err' signals an error while ** building result (memory error [1] or buffer overflow [2]). */ typedef struct BuffFS { @@ -512,9 +512,14 @@ static void pushbuff (lua_State *L, void *ud) { case 1: luaD_throw(L, LUA_ERRMEM); break; - case 2: - luaG_runerror(L, "buffer overflow"); - break; + case 2: /* length overflow: Add "..." at the end of result */ + if (buff->buffsize - buff->blen < 3) + strcpy(buff->b + buff->blen - 3, "..."); /* 'blen' must be > 3 */ + else { /* there is enough space left for the "..." */ + strcpy(buff->b + buff->blen, "..."); + buff->blen += 3; + } + /* FALLTHROUGH */ default: { /* no errors */ TString *ts = luaS_newlstr(L, buff->b, buff->blen); setsvalue2s(L, L->top.p, ts); @@ -527,8 +532,10 @@ static void pushbuff (lua_State *L, void *ud) { static const char *clearbuff (BuffFS *buff) { lua_State *L = buff->L; const char *res; - pushbuff(L, buff); - res = getstr(tsvalue(s2v(L->top.p - 1))); + if (luaD_rawrunprotected(L, pushbuff, buff) != LUA_OK) /* errors? */ + res = NULL; /* error message is on the top of the stack */ + else + res = getstr(tsvalue(s2v(L->top.p - 1))); if (buff->b != buff->space) /* using dynamic buffer? */ luaM_freearray(L, buff->b, buff->buffsize); /* free it */ return res; @@ -536,12 +543,14 @@ static const char *clearbuff (BuffFS *buff) { static void addstr2buff (BuffFS *buff, const char *str, size_t slen) { + size_t left = buff->buffsize - buff->blen; /* space left in the buffer */ if (buff->err) /* do nothing else after an error */ return; - if (slen > buff->buffsize - buff->blen) { - /* new string doesn't fit into current buffer */ + if (slen > left) { /* new string doesn't fit into current buffer? */ if (slen > ((MAX_SIZE/2) - buff->blen)) { /* overflow? */ - buff->err = 2; + memcpy(buff->b + buff->blen, str, left); /* copy what it can */ + buff->blen = buff->buffsize; + buff->err = 2; /* doesn't add anything else */ return; } else { @@ -552,13 +561,13 @@ static void addstr2buff (BuffFS *buff, const char *str, size_t slen) { : luaM_reallocvector(buff->L, buff->b, buff->buffsize, newsize, char); if (newb == NULL) { /* allocation error? */ - buff->err = 1; + buff->err = 1; /* signal a memory error */ return; } - if (buff->b == buff->space) + if (buff->b == buff->space) /* new buffer (not reallocated)? */ memcpy(newb, buff->b, buff->blen); /* copy previous content */ - buff->b = newb; - buff->buffsize = newsize; + buff->b = newb; /* set new (larger) buffer... */ + buff->buffsize = newsize; /* ...and its new size */ } } memcpy(buff->b + buff->blen, str, slen); /* copy new content */ @@ -651,6 +660,8 @@ const char *luaO_pushfstring (lua_State *L, const char *fmt, ...) { va_start(argp, fmt); msg = luaO_pushvfstring(L, fmt, argp); va_end(argp); + if (msg == NULL) /* error? */ + luaD_throw(L, LUA_ERRMEM); return msg; } diff --git a/manual/manual.of b/manual/manual.of index f0a2ed9452..1ac537f7c9 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -3974,7 +3974,7 @@ Lua will call @id{falloc} before raising the error. @APIEntry{const char *lua_pushfstring (lua_State *L, const char *fmt, ...);| -@apii{0,1,v} +@apii{0,1,m} Pushes onto the stack a formatted string and returns a pointer to this string @see{constchar}. @@ -3997,9 +3997,6 @@ The conversion specifiers can only be @Char{%c} (inserts an @T{int} as a one-byte character), and @Char{%U} (inserts an @T{unsigned long} as a @x{UTF-8} byte sequence). -This function may raise errors due to memory overflow -or an invalid conversion specifier. - } @APIEntry{void lua_pushglobaltable (lua_State *L);| @@ -4104,10 +4101,14 @@ onto the stack. const char *lua_pushvfstring (lua_State *L, const char *fmt, va_list argp);| -@apii{0,1,v} +@apii{0,1,-} -Equivalent to @Lid{lua_pushfstring}, except that it receives a @id{va_list} -instead of a variable number of arguments. +Equivalent to @Lid{lua_pushfstring}, +except that it receives a @id{va_list} +instead of a variable number of arguments, +and it does not raise errors. +Instead, in case of errors it pushes the error message +and returns @id{NULL}. } @@ -5636,6 +5637,7 @@ It is defined as the following macro: } It @N{returns 0} (@Lid{LUA_OK}) if there are no errors, or 1 in case of errors. +(Except for out-of-memory errors, which are raised.) } @@ -5800,7 +5802,7 @@ The first line in the file is ignored if it starts with a @T{#}. The string @id{mode} works as in the function @Lid{lua_load}. -This function returns the same results as @Lid{lua_load} +This function returns the same results as @Lid{lua_load}, or @Lid{LUA_ERRFILE} for file-related errors. As @Lid{lua_load}, this function only loads the chunk; @@ -9260,7 +9262,7 @@ the script is compiled as a variadic function. In interactive mode, Lua repeatedly prompts and waits for a line. After reading a line, -Lua first try to interpret the line as an expression. +Lua first tries to interpret the line as an expression. If it succeeds, it prints its value. Otherwise, it interprets the line as a chunk. If you write an incomplete chunk, @@ -9424,6 +9426,11 @@ instead, there is a new option @Lid{LUA_GCPARAM} to that end. Moreover, there were some changes in the parameters themselves. } +@item{ +The function @Lid{lua_pushvfstring} now reports errors, +instead of raising them. +} + } } From e4f418f07c7349f5ff844fbdc9a3b37b488113a5 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 27 Sep 2024 10:00:35 -0300 Subject: [PATCH 0954/1145] Local declaration in the REPL generates a warning --- lua.c | 18 ++++++++++++++++-- testes/main.lua | 9 +++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/lua.c b/lua.c index 9693ad6800..ea6141bb33 100644 --- a/lua.c +++ b/lua.c @@ -587,15 +587,28 @@ static int addreturn (lua_State *L) { } +static void checklocal (const char *line) { + static const size_t szloc = sizeof("local") - 1; + static const char space[] = " \t"; + line += strspn(line, space); /* skip spaces */ + if (strncmp(line, "local", szloc) == 0 && /* "local"? */ + strchr(space, *(line + szloc)) != NULL) { /* followed by a space? */ + lua_writestringerror("%s\n", + "warning: locals do not survive across lines in interactive mode"); + } +} + + /* ** Read multiple lines until a complete Lua statement or an error not ** for an incomplete statement. Start with first line already read in ** the stack. */ static int multiline (lua_State *L) { + size_t len; + const char *line = lua_tolstring(L, 1, &len); /* get first line */ + checklocal(line); for (;;) { /* repeat until gets a complete statement */ - size_t len; - const char *line = lua_tolstring(L, 1, &len); /* get what it has */ int status = luaL_loadbuffer(L, line, len, "=stdin"); /* try it */ if (!incomplete(L, status) || !pushline(L, 0)) return status; /* should not or cannot try to add continuation line */ @@ -603,6 +616,7 @@ static int multiline (lua_State *L) { lua_pushliteral(L, "\n"); /* add newline... */ lua_insert(L, -2); /* ...between the two lines */ lua_concat(L, 3); /* join them */ + line = lua_tolstring(L, 1, &len); /* get what is has */ } } diff --git a/testes/main.lua b/testes/main.lua index 7b0f4ee081..1aa7b21771 100644 --- a/testes/main.lua +++ b/testes/main.lua @@ -263,6 +263,15 @@ assert(string.find(getoutput(), "error calling 'print'")) RUN('echo "io.stderr:write(1000)\ncont" | lua -e "require\'debug\'.debug()" 2> %s', out) checkout("lua_debug> 1000lua_debug> ") +do -- test warning for locals + RUN('echo " local x" | lua -i > %s 2>&1', out) + assert(string.find(getoutput(), "warning: ")) + + RUN('echo "local1 = 10\nlocal1 + 3" | lua -i > %s 2>&1', out) + local t = getoutput() + assert(not string.find(t, "warning")) + assert(string.find(t, "13")) +end print("testing warnings") From 3d54b42d59bcc1b31a369f3497ac22745d63cae6 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 30 Sep 2024 14:01:42 -0300 Subject: [PATCH 0955/1145] 'objsize' broke in smaller pieces --- lfunc.c | 15 +++++++++++++ lfunc.h | 1 + lgc.c | 58 +++++++++++++++++------------------------------- lgc.h | 15 +++++++++---- lstate.c | 8 +++++++ lstate.h | 1 + ltable.c | 12 ++++++++++ ltable.h | 1 + manual/manual.of | 43 +++++++++++++++++------------------ 9 files changed, 91 insertions(+), 63 deletions(-) diff --git a/lfunc.c b/lfunc.c index d650c00079..2b0412818d 100644 --- a/lfunc.c +++ b/lfunc.c @@ -264,6 +264,21 @@ Proto *luaF_newproto (lua_State *L) { } +size_t luaF_protosize (Proto *p) { + size_t sz = sizeof(Proto) + + cast_uint(p->sizep) * sizeof(Proto*) + + cast_uint(p->sizek) * sizeof(TValue) + + cast_uint(p->sizelocvars) * sizeof(LocVar) + + cast_uint(p->sizeupvalues) * sizeof(Upvaldesc); + if (!(p->flag & PF_FIXED)) { + sz += cast_uint(p->sizecode) * sizeof(Instruction) + + cast_uint(p->sizelineinfo) * sizeof(lu_byte) + + cast_uint(p->sizeabslineinfo) * sizeof(AbsLineInfo); + } + return sz; +} + + void luaF_freeproto (lua_State *L, Proto *f) { if (!(f->flag & PF_FIXED)) { luaM_freearray(L, f->code, cast_sizet(f->sizecode)); diff --git a/lfunc.h b/lfunc.h index 162b55ecdb..b96510747f 100644 --- a/lfunc.h +++ b/lfunc.h @@ -56,6 +56,7 @@ LUAI_FUNC void luaF_newtbcupval (lua_State *L, StkId level); LUAI_FUNC void luaF_closeupval (lua_State *L, StkId level); LUAI_FUNC StkId luaF_close (lua_State *L, StkId level, int status, int yy); LUAI_FUNC void luaF_unlinkupval (UpVal *uv); +LUAI_FUNC size_t luaF_protosize (Proto *p); LUAI_FUNC void luaF_freeproto (lua_State *L, Proto *f); LUAI_FUNC const char *luaF_getlocalname (const Proto *func, int local_number, int pc); diff --git a/lgc.c b/lgc.c index a38d11b2d5..e154402b17 100644 --- a/lgc.c +++ b/lgc.c @@ -113,13 +113,7 @@ static void entersweep (lua_State *L); static size_t objsize (GCObject *o) { switch (o->tt) { case LUA_VTABLE: { - /* Fow now, table size does not consider 'haslastfree' */ - Table *t = gco2t(o); - size_t sz = sizeof(Table) - + luaH_realasize(t) * (sizeof(Value) + 1); - if (!isdummy(t)) - sz += sizenode(t) * sizeof(Node); - return sz; + return luaH_size(gco2t(o)); } case LUA_VLCL: { LClosure *cl = gco2lcl(o); @@ -135,26 +129,10 @@ static size_t objsize (GCObject *o) { return sizeudata(u->nuvalue, u->len); } case LUA_VPROTO: { - Proto *p = gco2p(o); - size_t sz = sizeof(Proto) - + cast_uint(p->sizep) * sizeof(Proto*) - + cast_uint(p->sizek) * sizeof(TValue) - + cast_uint(p->sizelocvars) * sizeof(LocVar) - + cast_uint(p->sizeupvalues) * sizeof(Upvaldesc); - if (!(p->flag & PF_FIXED)) { - sz += cast_uint(p->sizecode) * sizeof(Instruction) - + cast_uint(p->sizelineinfo) * sizeof(lu_byte) - + cast_uint(p->sizeabslineinfo) * sizeof(AbsLineInfo); - } - return sz; + return luaF_protosize(gco2p(o)); } case LUA_VTHREAD: { - lua_State *L1 = gco2th(o); - size_t sz = sizeof(lua_State) + LUA_EXTRASPACE - + cast_uint(L1->nci) * sizeof(CallInfo); - if (L1->stack.p != NULL) - sz += cast_uint(stacksize(L1) + EXTRA_STACK) * sizeof(StackValue); - return sz; + return luaE_statesize(gco2th(o)); } case LUA_VSHRSTR: { TString *ts = gco2ts(o); @@ -164,7 +142,9 @@ static size_t objsize (GCObject *o) { TString *ts = gco2ts(o); return luaS_sizelngstr(ts->u.lnglen, ts->shrlen); } - case LUA_VUPVAL: return sizeof(UpVal); + case LUA_VUPVAL: { + return sizeof(UpVal); + } default: lua_assert(0); return 0; } } @@ -615,7 +595,7 @@ static l_mem traversetable (global_State *g, Table *h) { } else /* not weak */ traversestrongtable(g, h); - return 1 + sizenode(h) + h->alimit; + return 1 + 2*sizenode(h) + h->alimit; } @@ -1291,10 +1271,11 @@ static void minor2inc (lua_State *L, global_State *g, lu_byte kind) { /* ** Decide whether to shift to major mode. It tests two conditions: ** 1) Whether the number of added old objects in this collection is more -** than half the number of new objects. ('step' is the number of objects -** created between minor collections. Except for forward barriers, it -** is the maximum number of objects that can become old in each minor -** collection.) +** than half the number of new objects. ('step' is equal to the debt set +** to trigger the next minor collection; that is equal to the number +** of objects created since the previous minor collection. Except for +** forward barriers, it is the maximum number of objects that can become +** old in each minor collection.) ** 2) Whether the accumulated number of added old objects is larger ** than 'minormajor'% of the number of lived objects after the last ** major collection. (That percentage is computed in 'limit'.) @@ -1678,7 +1659,7 @@ void luaC_runtilstate (lua_State *L, int state, int fast) { /* -** Performs a basic incremental step. The debt and step size are +** Performs a basic incremental step. The step size is ** converted from bytes to "units of work"; then the function loops ** running single steps until adding that many units of work or ** finishing a cycle (pause state). Finally, it sets the debt that @@ -1689,7 +1670,9 @@ static void incstep (lua_State *L, global_State *g) { l_mem work2do = applygcparam(g, STEPMUL, stepsize); l_mem stres; int fast = (work2do == 0); /* special case: do a full collection */ +//printf("\n** %ld %ld %d\n", work2do, stepsize, g->gcstate); do { /* repeat until enough work */ +//printf("%d-", g->gcstate); stres = singlestep(L, fast); /* perform one single step */ if (stres == step2minor) /* returned to minor collections? */ return; /* nothing else to be done here */ @@ -1716,21 +1699,20 @@ void luaC_step (lua_State *L) { if (!gcrunning(g)) /* not running? */ luaE_setdebt(g, 20000); else { -// printf("mem: %ld kind: %s ", gettotalbytes(g), -// g->gckind == KGC_INC ? "inc" : g->gckind == KGC_GENMAJOR ? "genmajor" : -// "genminor"); +//printf("mem: %ld kind: %s ", gettotalbytes(g), +// g->gckind == KGC_INC ? "inc" : g->gckind == KGC_GENMAJOR ? "genmajor" : +// "genminor"); switch (g->gckind) { case KGC_INC: case KGC_GENMAJOR: -// printf("(%d -> ", g->gcstate); incstep(L, g); -// printf("%d) ", g->gcstate); +//printf("%d) ", g->gcstate); break; case KGC_GENMINOR: youngcollection(L, g); setminordebt(g); break; } -// printf("-> mem: %ld debt: %ld\n", gettotalbytes(g), g->GCdebt); +//printf("-> mem: %ld debt: %ld\n", gettotalbytes(g), g->GCdebt); } } diff --git a/lgc.h b/lgc.h index 0b16ac7f6d..a3bc746a0b 100644 --- a/lgc.h +++ b/lgc.h @@ -160,7 +160,11 @@ */ -/* Default Values for GC parameters */ +/* +** {====================================================== +** Default Values for GC parameters +** ======================================================= +*/ /* ** Minor collections will shift to major ones after LUAI_MINORMAJOR% @@ -189,17 +193,20 @@ /* ** Step multiplier: The collector handles LUAI_GCMUL% work units for ** each new allocated byte. (Each "work unit" corresponds roughly to -** sweeping or marking one object.) +** sweeping one object or traversing one slot.) */ -#define LUAI_GCMUL 20 /* ??? */ +#define LUAI_GCMUL 40 /* How many bytes to allocate before next GC step */ -#define LUAI_GCSTEPSIZE (250 * sizeof(void*)) +#define LUAI_GCSTEPSIZE (200 * sizeof(Table)) #define setgcparam(g,p,v) (g->gcparams[LUA_GCP##p] = luaO_codeparam(v)) #define applygcparam(g,p,x) luaO_applyparam(g->gcparams[LUA_GCP##p], x) +/* }====================================================== */ + + /* ** Control when GC is running: */ diff --git a/lstate.c b/lstate.c index 8e7c8b8605..d6b9c90f30 100644 --- a/lstate.c +++ b/lstate.c @@ -257,6 +257,14 @@ static void preinit_thread (lua_State *L, global_State *g) { } +size_t luaE_statesize (lua_State *L) { + size_t sz = sizeof(LG) + cast_uint(L->nci) * sizeof(CallInfo); + if (L->stack.p != NULL) + sz += cast_uint(stacksize(L) + EXTRA_STACK) * sizeof(StackValue); + return sz; +} + + static void close_state (lua_State *L) { global_State *g = G(L); if (!completestate(g)) /* closing a partially built state? */ diff --git a/lstate.h b/lstate.h index 2a03576dd4..e2108668c5 100644 --- a/lstate.h +++ b/lstate.h @@ -416,6 +416,7 @@ union GCUnion { LUAI_FUNC void luaE_setdebt (global_State *g, l_mem debt); LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1); +LUAI_FUNC size_t luaE_statesize (lua_State *L); LUAI_FUNC CallInfo *luaE_extendCI (lua_State *L); LUAI_FUNC void luaE_shrinkCI (lua_State *L); LUAI_FUNC void luaE_checkcstack (lua_State *L); diff --git a/ltable.c b/ltable.c index 80a1bf84c2..bf44e82e26 100644 --- a/ltable.c +++ b/ltable.c @@ -805,6 +805,18 @@ Table *luaH_new (lua_State *L) { } +size_t luaH_size (Table *t) { + size_t sz = sizeof(Table) + + luaH_realasize(t) * (sizeof(Value) + 1); + if (!isdummy(t)) { + sz += sizenode(t) * sizeof(Node); + if (haslastfree(t)) + sz += sizeof(Limbox); + } + return sz; +} + + /* ** Frees a table. */ diff --git a/ltable.h b/ltable.h index c6a87807af..c352da3840 100644 --- a/ltable.h +++ b/ltable.h @@ -163,6 +163,7 @@ LUAI_FUNC Table *luaH_new (lua_State *L); LUAI_FUNC void luaH_resize (lua_State *L, Table *t, unsigned nasize, unsigned nhsize); LUAI_FUNC void luaH_resizearray (lua_State *L, Table *t, unsigned nasize); +LUAI_FUNC size_t luaH_size (Table *t); LUAI_FUNC void luaH_free (lua_State *L, Table *t); LUAI_FUNC int luaH_next (lua_State *L, Table *t, StkId key); LUAI_FUNC lua_Unsigned luaH_getn (Table *t); diff --git a/manual/manual.of b/manual/manual.of index 1ac537f7c9..c93fbfcb1b 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -608,8 +608,8 @@ An object is considered @def{dead} as soon as the collector can be sure the object will not be accessed again in the normal execution of the program. (@Q{Normal execution} here excludes finalizers, -which can resurrect dead objects @see{finalizers}, -and excludes also operations using the debug library.) +which resurrect dead objects @see{finalizers}, +and it excludes also some operations using the debug library.) Note that the time when the collector can be sure that an object is dead may not coincide with the programmer's expectations. The only guarantees are that Lua will not collect an object @@ -657,25 +657,27 @@ and the @def{garbage-collector step size}. The garbage-collector pause controls how long the collector waits before starting a new cycle. -The collector starts a new cycle when the number of objects +The collector starts a new cycle when the number of bytes hits @M{n%} of the total after the previous collection. Larger values make the collector less aggressive. Values equal to or less than 100 mean the collector will not wait to start a new cycle. A value of 200 means that the collector waits for -the total number of objects to double before starting a new cycle. +the total number of bytes to double before starting a new cycle. The garbage-collector step size controls the size of each incremental step, -specifically how many objects the interpreter creates +specifically how many bytes the interpreter allocates before performing a step: -A value of @M{n} means the interpreter will create -approximately @M{n} objects between steps. +A value of @M{n} means the interpreter will allocate +approximately @M{n} bytes between steps. The garbage-collector step multiplier -controls the size of each GC step. -A value of @M{n} means the interpreter will mark or sweep, -in each step, @M{n%} objects for each created object. +controls how much work each incremental step does. +A value of @M{n} means the interpreter will execute +@M{n%} @emphx{units of work} for each byte allocated. +A unit of work corresponds roughly to traversing one slot +or sweeping one object. Larger values make the collector more aggressive. Beware that values too small can make the collector too slow to ever finish a cycle. @@ -689,7 +691,7 @@ effectively producing a non-incremental, stop-the-world collector. In generational mode, the collector does frequent @emph{minor} collections, which traverses only objects recently created. -If after a minor collection the number of objects is above a limit, +If after a minor collection the number of bytes is above a limit, the collector shifts to a @emph{major} collection, which traverses all objects. The collector will then stay doing major collections until @@ -702,30 +704,30 @@ and the @def{major-minor multiplier}. The minor multiplier controls the frequency of minor collections. For a minor multiplier @M{x}, -a new minor collection will be done when the number of objects +a new minor collection will be done when the number of bytes grows @M{x%} larger than the number in use just after the last major collection. For instance, for a multiplier of 20, -the collector will do a minor collection when the number of objects +the collector will do a minor collection when the number of bytes gets 20% larger than the total after the last major collection. The minor-major multiplier controls the shift to major collections. For a multiplier @M{x}, the collector will shift to a major collection -when the number of old objects grows @M{x%} larger +when the number of bytes from old objects grows @M{x%} larger than the total after the previous major collection. For instance, for a multiplier of 100, -the collector will do a major collection when the number of old objects +the collector will do a major collection when the number of old bytes gets larger than twice the total after the previous major collection. The major-minor multiplier controls the shift back to minor collections. For a multiplier @M{x}, the collector will shift back to minor collections after a major collection collects at least @M{x%} -of the objects allocated during the last cycle. +of the bytes allocated during the last cycle. In particular, for a multiplier of 0, the collector will immediately shift back to minor collections -after doing one cycle of major collections. +after doing one major collection. } @@ -6404,23 +6406,22 @@ gives the exact number of bytes in use by Lua. Performs a garbage-collection step. This option may be followed by an extra argument, an integer with the step size. -The default for this argument is zero. If the size is a positive @id{n}, -the collector acts as if @id{n} new objects have been created. +the collector acts as if @id{n} new bytes have been allocated. If the size is zero, the collector performs a basic step. In incremental mode, a basic step corresponds to the current step size. In generational mode, a basic step performs a full minor collection or -a major collection, +an incremental step, if the collector has scheduled one. In incremental mode, the function returns @true if the step finished a collection cycle. In generational mode, -the function returns @true if the step performed a major collection. +the function returns @true if the step finished a major collection. } @item{@St{isrunning}| From d0815046d003f8f24efcdb03d35dd125ddd3b5f9 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 18 Oct 2024 17:10:20 -0300 Subject: [PATCH 0956/1145] Some adjustments in transition minor->major Plus extra comments and other details. --- lgc.c | 78 ++++++++++++++++++++++++++---------------------- manual/manual.of | 5 +++- 2 files changed, 46 insertions(+), 37 deletions(-) diff --git a/lgc.c b/lgc.c index e154402b17..58d0bf7d1f 100644 --- a/lgc.c +++ b/lgc.c @@ -424,10 +424,8 @@ static void cleargraylists (global_State *g) { /* ** mark root set and reset all gray lists, to start a new collection. -** 'marked' is initialized with the number of fixed objects in the state, -** to count the total number of live objects during a cycle. (That is -** the metafield names, plus the reserved words, plus "_ENV" plus the -** memory-error message.) +** 'GCmarked' is initialized to count the total number of live bytes +** during a cycle. */ static void restartcollection (global_State *g) { cleargraylists(g); @@ -1067,10 +1065,25 @@ void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt) { ** ======================================================= */ +/* +** Fields 'GCmarked' and 'GCmajorminor' are used to control the pace and +** the mode of the collector. They play several roles, depending on the +** mode of the collector: +** * KGC_INC: +** GCmarked: number of marked bytes during a cycle. +** GCmajorminor: not used. +** * KGC_GENMINOR +** GCmarked: number of bytes that became old since last major collection. +** GCmajorminor: number of bytes marked in last major collection. +** * KGC_GENMAJOR +** GCmarked: number of bytes that became old sinse last major collection. +** GCmajorminor: number of bytes marked in last major collection. +*/ + /* ** Set the "time" to wait before starting a new incremental cycle; -** cycle will start when number of objects in use hits the threshold of +** cycle will start when number of bytes in use hits the threshold of ** approximately (marked * pause / 100). */ static void setpause (global_State *g) { @@ -1258,7 +1271,7 @@ static void finishgencycle (lua_State *L, global_State *g) { ** in generational mode. */ static void minor2inc (lua_State *L, global_State *g, lu_byte kind) { - g->GCmajorminor = g->GCmarked; /* number of live objects */ + g->GCmajorminor = g->GCmarked; /* number of live bytes */ g->gckind = kind; g->reallyold = g->old1 = g->survival = NULL; g->finobjrold = g->finobjold1 = g->finobjsur = NULL; @@ -1269,21 +1282,16 @@ static void minor2inc (lua_State *L, global_State *g, lu_byte kind) { /* -** Decide whether to shift to major mode. It tests two conditions: -** 1) Whether the number of added old objects in this collection is more -** than half the number of new objects. ('step' is equal to the debt set -** to trigger the next minor collection; that is equal to the number -** of objects created since the previous minor collection. Except for -** forward barriers, it is the maximum number of objects that can become -** old in each minor collection.) -** 2) Whether the accumulated number of added old objects is larger -** than 'minormajor'% of the number of lived objects after the last -** major collection. (That percentage is computed in 'limit'.) +** Decide whether to shift to major mode. It shifts if the accumulated +** number of added old bytes (counted in 'GCmarked') is larger than +** 'minormajor'% of the number of lived bytes after the last major +** collection. (This number is kept in 'GCmajorminor'.) */ -static int checkminormajor (global_State *g, l_mem addedold1) { - l_mem step = applygcparam(g, MINORMUL, g->GCmajorminor); +static int checkminormajor (global_State *g) { l_mem limit = applygcparam(g, MINORMAJOR, g->GCmajorminor); - return (addedold1 >= (step >> 1) || g->GCmarked >= limit); + if (limit == 0) + return 0; /* special case: 'minormajor' 0 stops major collections */ + return (g->GCmarked >= limit); } /* @@ -1326,13 +1334,13 @@ static void youngcollection (lua_State *L, global_State *g) { sweepgen(L, g, &g->tobefnz, NULL, &dummy, &addedold1); - /* keep total number of added old1 objects */ + /* keep total number of added old1 bytes */ g->GCmarked = marked + addedold1; /* decide whether to shift to major mode */ - if (checkminormajor(g, addedold1)) { + if (checkminormajor(g)) { minor2inc(L, g, KGC_GENMAJOR); /* go to major mode */ - g->GCmarked = 0; /* avoid pause in first major cycle */ + g->GCmarked = 0; /* avoid pause in first major cycle (see 'setpause') */ } else finishgencycle(L, g); /* still in minor mode; finish it */ @@ -1361,8 +1369,8 @@ static void atomic2gen (lua_State *L, global_State *g) { sweep2old(L, &g->tobefnz); g->gckind = KGC_GENMINOR; - g->GCmajorminor = g->GCmarked; /* "base" for number of objects */ - g->GCmarked = 0; /* to count the number of added old1 objects */ + g->GCmajorminor = g->GCmarked; /* "base" for number of bytes */ + g->GCmarked = 0; /* to count the number of added old1 bytes */ finishgencycle(L, g); } @@ -1423,15 +1431,15 @@ static void fullgen (lua_State *L, global_State *g) { /* ** After an atomic incremental step from a major collection, ** check whether collector could return to minor collections. -** It checks whether the number of objects 'tobecollected' -** is greater than 'majorminor'% of the number of objects added -** since the last collection ('addedobjs'). +** It checks whether the number of bytes 'tobecollected' +** is greater than 'majorminor'% of the number of bytes added +** since the last collection ('addedbytes'). */ static int checkmajorminor (lua_State *L, global_State *g) { if (g->gckind == KGC_GENMAJOR) { /* generational mode? */ l_mem numbytes = gettotalbytes(g); - l_mem addedobjs = numbytes - g->GCmajorminor; - l_mem limit = applygcparam(g, MAJORMINOR, addedobjs); + l_mem addedbytes = numbytes - g->GCmajorminor; + l_mem limit = applygcparam(g, MAJORMINOR, addedbytes); l_mem tobecollected = numbytes - g->GCmarked; if (tobecollected > limit) { atomic2gen(L, g); /* return to generational mode */ @@ -1670,9 +1678,7 @@ static void incstep (lua_State *L, global_State *g) { l_mem work2do = applygcparam(g, STEPMUL, stepsize); l_mem stres; int fast = (work2do == 0); /* special case: do a full collection */ -//printf("\n** %ld %ld %d\n", work2do, stepsize, g->gcstate); do { /* repeat until enough work */ -//printf("%d-", g->gcstate); stres = singlestep(L, fast); /* perform one single step */ if (stres == step2minor) /* returned to minor collections? */ return; /* nothing else to be done here */ @@ -1688,6 +1694,10 @@ static void incstep (lua_State *L, global_State *g) { } +#if !defined(luai_tracegc) +#define luai_tracegc(L) ((void)0) +#endif + /* ** Performs a basic GC step if collector is running. (If collector is ** not running, set a reasonable debt to avoid it being called at @@ -1699,20 +1709,16 @@ void luaC_step (lua_State *L) { if (!gcrunning(g)) /* not running? */ luaE_setdebt(g, 20000); else { -//printf("mem: %ld kind: %s ", gettotalbytes(g), -// g->gckind == KGC_INC ? "inc" : g->gckind == KGC_GENMAJOR ? "genmajor" : -// "genminor"); + luai_tracegc(L); /* for internal debugging */ switch (g->gckind) { case KGC_INC: case KGC_GENMAJOR: incstep(L, g); -//printf("%d) ", g->gcstate); break; case KGC_GENMINOR: youngcollection(L, g); setminordebt(g); break; } -//printf("-> mem: %ld debt: %ld\n", gettotalbytes(g), g->GCdebt); } } diff --git a/manual/manual.of b/manual/manual.of index c93fbfcb1b..6947b2a0a1 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -719,6 +719,8 @@ than the total after the previous major collection. For instance, for a multiplier of 100, the collector will do a major collection when the number of old bytes gets larger than twice the total after the previous major collection. +As a special case, +a value of 0 stops the collector from doing major collections. The major-minor multiplier controls the shift back to minor collections. For a multiplier @M{x}, @@ -6441,7 +6443,8 @@ Changes the collector mode to generational and returns the previous mode. Changes and/or retrieves the values of a parameter of the collector. This option must be followed by one or two extra arguments: The name of the parameter being changed or retrieved (a string) -and an optional new value for that parameter (an integer). +and an optional new value for that parameter, +an integer in the range @M{[0,100000]}. The first argument must have one of the following values: @description{ @item{@St{minormul}| The minor multiplier. } From 258355734d3aceb34eb6288ece37d9bbd7f2bc6d Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 21 Oct 2024 15:18:20 -0300 Subject: [PATCH 0957/1145] Better support in 'ltests' for tracing the GC --- lgc.c | 17 +++++++------ ltests.c | 68 ++++++++++++++++++++++++++++++++++++-------------- ltests.h | 4 +++ testes/api.lua | 2 +- testes/gc.lua | 6 ++--- 5 files changed, 67 insertions(+), 30 deletions(-) diff --git a/lgc.c b/lgc.c index 58d0bf7d1f..a43cc6a814 100644 --- a/lgc.c +++ b/lgc.c @@ -1695,21 +1695,23 @@ static void incstep (lua_State *L, global_State *g) { #if !defined(luai_tracegc) -#define luai_tracegc(L) ((void)0) +#define luai_tracegc(L,f) ((void)0) #endif /* -** Performs a basic GC step if collector is running. (If collector is -** not running, set a reasonable debt to avoid it being called at -** every single check.) +** Performs a basic GC step if collector is running. (If collector was +** stopped by the user, set a reasonable debt to avoid it being called +** at every single check.) */ void luaC_step (lua_State *L) { global_State *g = G(L); lua_assert(!g->gcemergency); - if (!gcrunning(g)) /* not running? */ - luaE_setdebt(g, 20000); + if (!gcrunning(g)) { /* not running? */ + if (g->gcstp & GCSTPUSR) /* stopped by the user? */ + luaE_setdebt(g, 20000); + } else { - luai_tracegc(L); /* for internal debugging */ + luai_tracegc(L, 1); /* for internal debugging */ switch (g->gckind) { case KGC_INC: case KGC_GENMAJOR: incstep(L, g); @@ -1719,6 +1721,7 @@ void luaC_step (lua_State *L) { setminordebt(g); break; } + luai_tracegc(L, 0); /* for internal debugging */ } } diff --git a/ltests.c b/ltests.c index 91bce2a12e..3534d8d501 100644 --- a/ltests.c +++ b/ltests.c @@ -944,34 +944,63 @@ static int gc_printobj (lua_State *L) { } +static const char *statenames[] = { + "propagate", "enteratomic", "atomic", "sweepallgc", "sweepfinobj", + "sweeptobefnz", "sweepend", "callfin", "pause", ""}; + static int gc_state (lua_State *L) { - static const char *statenames[] = { - "propagate", "atomic", "sweepallgc", "sweepfinobj", - "sweeptobefnz", "sweepend", "callfin", "pause", ""}; static const int states[] = { - GCSpropagate, GCSenteratomic, GCSswpallgc, GCSswpfinobj, + GCSpropagate, GCSenteratomic, GCSatomic, GCSswpallgc, GCSswpfinobj, GCSswptobefnz, GCSswpend, GCScallfin, GCSpause, -1}; int option = states[luaL_checkoption(L, 1, "", statenames)]; + global_State *g = G(L); if (option == -1) { - lua_pushstring(L, statenames[G(L)->gcstate]); + lua_pushstring(L, statenames[g->gcstate]); return 1; } else { - global_State *g = G(L); - if (G(L)->gckind != KGC_INC) + if (g->gckind != KGC_INC) luaL_error(L, "cannot change states in generational mode"); lua_lock(L); if (option < g->gcstate) { /* must cross 'pause'? */ luaC_runtilstate(L, GCSpause, 1); /* run until pause */ } luaC_runtilstate(L, option, 0); /* do not skip propagation state */ - lua_assert(G(L)->gcstate == option); + lua_assert(g->gcstate == option); lua_unlock(L); return 0; } } +static int tracinggc = 0; +void luai_tracegctest (lua_State *L, int first) { + if (!tracinggc) return; + else { + global_State *g = G(L); + lua_unlock(L); + g->gcstp = GCSTPGC; + lua_checkstack(L, 10); + lua_getfield(L, LUA_REGISTRYINDEX, "tracegc"); + lua_pushboolean(L, first); + lua_call(L, 1, 0); + g->gcstp = 0; + lua_lock(L); + } +} + + +static int tracegc (lua_State *L) { + if (lua_isnil(L, 1)) + tracinggc = 0; + else { + tracinggc = 1; + lua_setfield(L, LUA_REGISTRYINDEX, "tracegc"); + } + return 0; +} + + static int hash_query (lua_State *L) { if (lua_isnone(L, 2)) { luaL_argcheck(L, lua_type(L, 1) == LUA_TSTRING, 1, "string expected"); @@ -1038,17 +1067,17 @@ static int table_query (lua_State *L) { } -static int query_GCparams (lua_State *L) { +static int gc_query (lua_State *L) { global_State *g = G(L); - lua_pushinteger(L, cast(lua_Integer, gettotalbytes(g))); - lua_pushinteger(L, cast(lua_Integer, g->GCdebt)); - lua_pushinteger(L, cast(lua_Integer, applygcparam(g, MINORMUL, 100))); - lua_pushinteger(L, cast(lua_Integer, applygcparam(g, MAJORMINOR, 100))); - lua_pushinteger(L, cast(lua_Integer, applygcparam(g, MINORMAJOR, 100))); - lua_pushinteger(L, cast(lua_Integer, applygcparam(g, PAUSE, 100))); - lua_pushinteger(L, cast(lua_Integer, applygcparam(g, STEPMUL, 100))); - lua_pushinteger(L, cast(lua_Integer, applygcparam(g, STEPSIZE, 100))); - return 8; + lua_pushstring(L, g->gckind == KGC_INC ? "inc" + : g->gckind == KGC_GENMAJOR ? "genmajor" + : "genminor"); + lua_pushstring(L, statenames[g->gcstate]); + lua_pushinteger(L, cast_st2S(gettotalbytes(g))); + lua_pushinteger(L, cast_st2S(g->GCdebt)); + lua_pushinteger(L, cast_st2S(g->GCmarked)); + lua_pushinteger(L, cast_st2S(g->GCmajorminor)); + return 6; } @@ -2009,6 +2038,7 @@ static const struct luaL_Reg tests_funcs[] = { {"gccolor", gc_color}, {"gcage", gc_age}, {"gcstate", gc_state}, + {"tracegc", tracegc}, {"pobj", gc_printobj}, {"getref", getref}, {"hash", hash_query}, @@ -2026,9 +2056,9 @@ static const struct luaL_Reg tests_funcs[] = { {"num2int", num2int}, {"makeseed", makeseed}, {"pushuserdata", pushuserdata}, + {"gcquery", gc_query}, {"querystr", string_query}, {"querytab", table_query}, - {"queryGCparams", query_GCparams}, {"codeparam", test_codeparam}, {"applyparam", test_applyparam}, {"ref", tref}, diff --git a/ltests.h b/ltests.h index 078c9fc344..906fae335d 100644 --- a/ltests.h +++ b/ltests.h @@ -58,6 +58,10 @@ typedef struct Memcontrol { LUA_API Memcontrol l_memcontrol; +#define luai_tracegc(L,f) luai_tracegctest(L, f) +LUAI_FUNC void luai_tracegctest (lua_State *L, int first); + + /* ** generic variable for debug tricks */ diff --git a/testes/api.lua b/testes/api.lua index ae2f82dd33..b7e34f7fc5 100644 --- a/testes/api.lua +++ b/testes/api.lua @@ -799,7 +799,7 @@ assert(debug.getuservalue(b) == 134) -- test barrier for uservalues do local oldmode = collectgarbage("incremental") - T.gcstate("atomic") + T.gcstate("enteratomic") assert(T.gccolor(b) == "black") debug.setuservalue(b, {x = 100}) T.gcstate("pause") -- complete collection diff --git a/testes/gc.lua b/testes/gc.lua index 3f8143b194..09bfe09ab3 100644 --- a/testes/gc.lua +++ b/testes/gc.lua @@ -560,8 +560,8 @@ if T then -- tests for weird cases collecting upvalues -- create coroutine in a weak table, so it will never be marked t.co = coroutine.wrap(foo) local f = t.co() -- create function to access local 'a' - T.gcstate("atomic") -- ensure all objects are traversed - assert(T.gcstate() == "atomic") + T.gcstate("enteratomic") -- ensure all objects are traversed + assert(T.gcstate() == "enteratomic") assert(t.co() == 100) -- resume coroutine, creating new table for 'a' assert(T.gccolor(t.co) == "white") -- thread was not traversed T.gcstate("pause") -- collect thread, but should mark 'a' before that @@ -574,7 +574,7 @@ if T then -- tests for weird cases collecting upvalues collectgarbage() collectgarbage"stop" local a = {} -- avoid 'u' as first element in 'allgc' - T.gcstate"atomic" + T.gcstate"enteratomic" T.gcstate"sweepallgc" local x = {} assert(T.gccolor(u) == "black") -- userdata is "old" (black) From 9b01da97e3a8f2df9acbd3b86af50e4040e9d914 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 21 Oct 2024 15:30:35 -0300 Subject: [PATCH 0958/1145] Small bug in 'luaE_luaE_statesize' Plus, function was renamed to 'luaE_threadsize'. --- lgc.c | 2 +- lstate.c | 4 ++-- lstate.h | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lgc.c b/lgc.c index a43cc6a814..7922bc0843 100644 --- a/lgc.c +++ b/lgc.c @@ -132,7 +132,7 @@ static size_t objsize (GCObject *o) { return luaF_protosize(gco2p(o)); } case LUA_VTHREAD: { - return luaE_statesize(gco2th(o)); + return luaE_threadsize(gco2th(o)); } case LUA_VSHRSTR: { TString *ts = gco2ts(o); diff --git a/lstate.c b/lstate.c index d6b9c90f30..a8db9477c3 100644 --- a/lstate.c +++ b/lstate.c @@ -257,8 +257,8 @@ static void preinit_thread (lua_State *L, global_State *g) { } -size_t luaE_statesize (lua_State *L) { - size_t sz = sizeof(LG) + cast_uint(L->nci) * sizeof(CallInfo); +size_t luaE_threadsize (lua_State *L) { + size_t sz = sizeof(LX) + cast_uint(L->nci) * sizeof(CallInfo); if (L->stack.p != NULL) sz += cast_uint(stacksize(L) + EXTRA_STACK) * sizeof(StackValue); return sz; diff --git a/lstate.h b/lstate.h index e2108668c5..a1b463c6f0 100644 --- a/lstate.h +++ b/lstate.h @@ -416,7 +416,7 @@ union GCUnion { LUAI_FUNC void luaE_setdebt (global_State *g, l_mem debt); LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1); -LUAI_FUNC size_t luaE_statesize (lua_State *L); +LUAI_FUNC size_t luaE_threadsize (lua_State *L); LUAI_FUNC CallInfo *luaE_extendCI (lua_State *L); LUAI_FUNC void luaE_shrinkCI (lua_State *L); LUAI_FUNC void luaE_checkcstack (lua_State *L); From 5ffcd458f001fce02e5f20a6130e145c6a3caf53 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 23 Oct 2024 17:15:06 -0300 Subject: [PATCH 0959/1145] Some changes in default GC parameters --- lgc.c | 4 +--- lgc.h | 10 +++++----- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/lgc.c b/lgc.c index 7922bc0843..e8b9ad5e36 100644 --- a/lgc.c +++ b/lgc.c @@ -1675,7 +1675,7 @@ void luaC_runtilstate (lua_State *L, int state, int fast) { */ static void incstep (lua_State *L, global_State *g) { l_mem stepsize = applygcparam(g, STEPSIZE, 100); - l_mem work2do = applygcparam(g, STEPMUL, stepsize); + l_mem work2do = applygcparam(g, STEPMUL, stepsize / cast_int(sizeof(void*))); l_mem stres; int fast = (work2do == 0); /* special case: do a full collection */ do { /* repeat until enough work */ @@ -1739,8 +1739,6 @@ static void fullinc (lua_State *L, global_State *g) { /* finish any pending sweep phase to start a new cycle */ luaC_runtilstate(L, GCSpause, 1); luaC_runtilstate(L, GCScallfin, 1); /* run up to finalizers */ - /* 'marked' must be correct after a full GC cycle */ - /* lua_assert(g->GCmarked == gettotalobjs(g)); ??? */ luaC_runtilstate(L, GCSpause, 1); /* finish collection */ setpause(g); } diff --git a/lgc.h b/lgc.h index a3bc746a0b..339cc17622 100644 --- a/lgc.h +++ b/lgc.h @@ -170,7 +170,7 @@ ** Minor collections will shift to major ones after LUAI_MINORMAJOR% ** bytes become old. */ -#define LUAI_MINORMAJOR 100 +#define LUAI_MINORMAJOR 70 /* ** Major collections will shift to minor ones after a collection @@ -182,20 +182,20 @@ ** A young (minor) collection will run after creating LUAI_GENMINORMUL% ** new bytes. */ -#define LUAI_GENMINORMUL 25 +#define LUAI_GENMINORMUL 20 /* incremental */ /* Number of bytes must be LUAI_GCPAUSE% before starting new cycle */ -#define LUAI_GCPAUSE 200 +#define LUAI_GCPAUSE 250 /* ** Step multiplier: The collector handles LUAI_GCMUL% work units for -** each new allocated byte. (Each "work unit" corresponds roughly to +** each new allocated word. (Each "work unit" corresponds roughly to ** sweeping one object or traversing one slot.) */ -#define LUAI_GCMUL 40 +#define LUAI_GCMUL 200 /* How many bytes to allocate before next GC step */ #define LUAI_GCSTEPSIZE (200 * sizeof(Table)) From e3ce88c9e850b7e79751083014699c5eae1bff31 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 23 Oct 2024 17:16:17 -0300 Subject: [PATCH 0960/1145] New function 'lua_numbertostrbuff' It converts a Lua number to a string in a buffer, without creating a new Lua string. --- lapi.c | 12 ++++++++++++ lauxlib.c | 7 +++---- liolib.c | 22 +++++++++------------- lobject.c | 44 +++++++++++++++++++++++--------------------- lobject.h | 1 + lua.h | 4 +++- manual/manual.of | 17 ++++++++++++++++- 7 files changed, 67 insertions(+), 40 deletions(-) diff --git a/lapi.c b/lapi.c index fffd7d262a..631cf44e8b 100644 --- a/lapi.c +++ b/lapi.c @@ -368,6 +368,18 @@ LUA_API int lua_compare (lua_State *L, int index1, int index2, int op) { } +LUA_API unsigned (lua_numbertostrbuff) (lua_State *L, int idx, char *buff) { + const TValue *o = index2value(L, idx); + if (ttisnumber(o)) { + unsigned len = luaO_tostringbuff(o, buff); + buff[len++] = '\0'; /* add final zero */ + return len; + } + else + return 0; +} + + LUA_API size_t lua_stringtonumber (lua_State *L, const char *s) { size_t sz = luaO_str2num(s, s2v(L->top.p)); if (sz != 0) diff --git a/lauxlib.c b/lauxlib.c index a36655f2bb..e4b125878d 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -920,10 +920,9 @@ LUALIB_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len) { else { switch (lua_type(L, idx)) { case LUA_TNUMBER: { - if (lua_isinteger(L, idx)) - lua_pushfstring(L, "%I", (LUAI_UACINT)lua_tointeger(L, idx)); - else - lua_pushfstring(L, "%f", (LUAI_UACNUMBER)lua_tonumber(L, idx)); + char buff[LUA_N2SBUFFSZ]; + lua_numbertostrbuff(L, idx, buff); + lua_pushstring(L, buff); break; } case LUA_TSTRING: diff --git a/liolib.c b/liolib.c index 17522bb207..98ff3d0dfb 100644 --- a/liolib.c +++ b/liolib.c @@ -665,20 +665,16 @@ static int g_write (lua_State *L, FILE *f, int arg) { int status = 1; errno = 0; for (; nargs--; arg++) { - if (lua_type(L, arg) == LUA_TNUMBER) { - /* optimization: could be done exactly as for strings */ - int len = lua_isinteger(L, arg) - ? fprintf(f, LUA_INTEGER_FMT, - (LUAI_UACINT)lua_tointeger(L, arg)) - : fprintf(f, LUA_NUMBER_FMT, - (LUAI_UACNUMBER)lua_tonumber(L, arg)); - status = status && (len > 0); - } - else { - size_t l; - const char *s = luaL_checklstring(L, arg, &l); - status = status && (fwrite(s, sizeof(char), l, f) == l); + char buff[LUA_N2SBUFFSZ]; + const char *s; + size_t len = lua_numbertostrbuff(L, arg, buff); /* try as a number */ + if (len > 0) { /* did conversion work (value was a number)? */ + s = buff; + len--; } + else /* must be a string */ + s = luaL_checklstring(L, arg, &len); + status = status && (fwrite(s, sizeof(char), len, f) == len); } if (l_likely(status)) return 1; /* file handle already on stack top */ diff --git a/lobject.c b/lobject.c index ba10189de0..97dacaf514 100644 --- a/lobject.c +++ b/lobject.c @@ -400,15 +400,17 @@ int luaO_utf8esc (char *buff, unsigned long x) { /* -** Maximum length of the conversion of a number to a string. Must be -** enough to accommodate both LUA_INTEGER_FMT and LUA_NUMBER_FMT. -** For a long long int, this is 19 digits plus a sign and a final '\0', -** adding to 21. For a long double, it can go to a sign, the dot, an -** exponent letter, an exponent sign, 4 exponent digits, the final -** '\0', plus the significant digits, which are approximately the *_DIG -** attribute. +** The size of the buffer for the conversion of a number to a string +** 'LUA_N2SBUFFSZ' must be enough to accommodate both LUA_INTEGER_FMT +** and LUA_NUMBER_FMT. For a long long int, this is 19 digits plus a +** sign and a final '\0', adding to 21. For a long double, it can go to +** a sign, the dot, an exponent letter, an exponent sign, 4 exponent +** digits, the final '\0', plus the significant digits, which are +** approximately the *_DIG attribute. */ -#define MAXNUMBER2STR (20 + l_floatatt(DIG)) +#if LUA_N2SBUFFSZ < (20 + l_floatatt(DIG)) +#error "invalid value for LUA_N2SBUFFSZ" +#endif /* @@ -422,12 +424,12 @@ int luaO_utf8esc (char *buff, unsigned long x) { */ static int tostringbuffFloat (lua_Number n, char *buff) { /* first conversion */ - int len = l_sprintf(buff, MAXNUMBER2STR, LUA_NUMBER_FMT, + int len = l_sprintf(buff, LUA_N2SBUFFSZ, LUA_NUMBER_FMT, (LUAI_UACNUMBER)n); lua_Number check = lua_str2number(buff, NULL); /* read it back */ if (check != n) { /* not enough precision? */ /* convert again with more precision */ - len = l_sprintf(buff, MAXNUMBER2STR, LUA_NUMBER_FMT_N, + len = l_sprintf(buff, LUA_N2SBUFFSZ, LUA_NUMBER_FMT_N, (LUAI_UACNUMBER)n); } /* looks like an integer? */ @@ -442,14 +444,14 @@ static int tostringbuffFloat (lua_Number n, char *buff) { /* ** Convert a number object to a string, adding it to a buffer. */ -static unsigned tostringbuff (TValue *obj, char *buff) { +unsigned luaO_tostringbuff (const TValue *obj, char *buff) { int len; lua_assert(ttisnumber(obj)); if (ttisinteger(obj)) - len = lua_integer2str(buff, MAXNUMBER2STR, ivalue(obj)); + len = lua_integer2str(buff, LUA_N2SBUFFSZ, ivalue(obj)); else len = tostringbuffFloat(fltvalue(obj), buff); - lua_assert(len < MAXNUMBER2STR); + lua_assert(len < LUA_N2SBUFFSZ); return cast_uint(len); } @@ -458,8 +460,8 @@ static unsigned tostringbuff (TValue *obj, char *buff) { ** Convert a number object to a Lua string, replacing the value at 'obj' */ void luaO_tostring (lua_State *L, TValue *obj) { - char buff[MAXNUMBER2STR]; - unsigned len = tostringbuff(obj, buff); + char buff[LUA_N2SBUFFSZ]; + unsigned len = luaO_tostringbuff(obj, buff); setsvalue(L, obj, luaS_newlstr(L, buff, len)); } @@ -474,10 +476,10 @@ void luaO_tostring (lua_State *L, TValue *obj) { /* ** Size for buffer space used by 'luaO_pushvfstring'. It should be -** (LUA_IDSIZE + MAXNUMBER2STR) + a minimal space for basic messages, +** (LUA_IDSIZE + LUA_N2SBUFFSZ) + a minimal space for basic messages, ** so that 'luaG_addinfo' can work directly on the static buffer. */ -#define BUFVFS cast_uint(LUA_IDSIZE + MAXNUMBER2STR + 95) +#define BUFVFS cast_uint(LUA_IDSIZE + LUA_N2SBUFFSZ + 95) /* ** Buffer used by 'luaO_pushvfstring'. 'err' signals an error while @@ -579,8 +581,8 @@ static void addstr2buff (BuffFS *buff, const char *str, size_t slen) { ** Add a numeral to the buffer. */ static void addnum2buff (BuffFS *buff, TValue *num) { - char numbuff[MAXNUMBER2STR]; - unsigned len = tostringbuff(num, numbuff); /* format number into 'numbuff' */ + char numbuff[LUA_N2SBUFFSZ]; + unsigned len = luaO_tostringbuff(num, numbuff); addstr2buff(buff, numbuff, len); } @@ -626,9 +628,9 @@ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) { break; } case 'p': { /* a pointer */ - char bf[MAXNUMBER2STR]; /* enough space for '%p' */ + char bf[LUA_N2SBUFFSZ]; /* enough space for '%p' */ void *p = va_arg(argp, void *); - int len = lua_pointer2str(bf, MAXNUMBER2STR, p); + int len = lua_pointer2str(bf, LUA_N2SBUFFSZ, p); addstr2buff(&buff, bf, cast_uint(len)); break; } diff --git a/lobject.h b/lobject.h index 2411410b4d..b1407b7791 100644 --- a/lobject.h +++ b/lobject.h @@ -845,6 +845,7 @@ LUAI_FUNC int luaO_rawarith (lua_State *L, int op, const TValue *p1, LUAI_FUNC void luaO_arith (lua_State *L, int op, const TValue *p1, const TValue *p2, StkId res); LUAI_FUNC size_t luaO_str2num (const char *s, TValue *o); +LUAI_FUNC unsigned luaO_tostringbuff (const TValue *obj, char *buff); LUAI_FUNC lu_byte luaO_hexavalue (int c); LUAI_FUNC void luaO_tostring (lua_State *L, TValue *obj); LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt, diff --git a/lua.h b/lua.h index dcf4926415..5fbc9d34ea 100644 --- a/lua.h +++ b/lua.h @@ -371,7 +371,9 @@ LUA_API int (lua_next) (lua_State *L, int idx); LUA_API void (lua_concat) (lua_State *L, int n); LUA_API void (lua_len) (lua_State *L, int idx); -LUA_API size_t (lua_stringtonumber) (lua_State *L, const char *s); +#define LUA_N2SBUFFSZ 64 +LUA_API unsigned (lua_numbertostrbuff) (lua_State *L, int idx, char *buff); +LUA_API size_t (lua_stringtonumber) (lua_State *L, const char *s); LUA_API lua_Alloc (lua_getallocf) (lua_State *L, void **ud); LUA_API void (lua_setallocf) (lua_State *L, lua_Alloc f, void *ud); diff --git a/manual/manual.of b/manual/manual.of index 6947b2a0a1..f0b17b4c1d 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -675,7 +675,7 @@ approximately @M{n} bytes between steps. The garbage-collector step multiplier controls how much work each incremental step does. A value of @M{n} means the interpreter will execute -@M{n%} @emphx{units of work} for each byte allocated. +@M{n%} @emphx{units of work} for each word allocated. A unit of work corresponds roughly to traversing one slot or sweeping one object. Larger values make the collector more aggressive. @@ -3829,11 +3829,26 @@ This macro may evaluate its arguments more than once. } +@APIEntry{unsigned (lua_numbertostrbuff) (lua_State *L, int idx, + char *buff);| +@apii{0,0,-} + +Converts the number at acceptable index @id{idx} to a string +and puts the result in @id{buff}. +The buffer must have a size of at least @Lid{LUA_N2SBUFFSZ} bytes. +The conversion follows a non-specified format @see{coercion}. +The function returns the number of bytes written to the buffer +(including the final zero), +or zero if the value at @id{idx} is not a number. + +} + @APIEntry{int lua_pcall (lua_State *L, int nargs, int nresults, int msgh);| @apii{nargs + 1,nresults|1,-} Calls a function (or a callable object) in protected mode. + Both @id{nargs} and @id{nresults} have the same meaning as in @Lid{lua_call}. If there are no errors during the call, From 25a2dac2bcab84d1f1ce8c49b673b4a032a29a4a Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 24 Oct 2024 15:33:25 -0300 Subject: [PATCH 0961/1145] Always use unsigned int for indexing table-arrays --- ltable.c | 8 ++++---- ltests.c | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/ltable.c b/ltable.c index bf44e82e26..a36a993f38 100644 --- a/ltable.c +++ b/ltable.c @@ -951,7 +951,7 @@ lu_byte luaH_getint (Table *t, lua_Integer key, TValue *res) { if (keyinarray(t, key)) { lu_byte tag = *getArrTag(t, key - 1); if (!tagisempty(tag)) - farr2val(t, key - 1, tag, res); + farr2val(t, cast_uint(key) - 1, tag, res); return tag; } else @@ -1062,7 +1062,7 @@ int luaH_psetint (Table *t, lua_Integer key, TValue *val) { if (keyinarray(t, key)) { lu_byte *tag = getArrTag(t, key - 1); if (!tagisempty(*tag) || checknoTM(t->metatable, TM_NEWINDEX)) { - fval2arr(t, key - 1, tag, val); + fval2arr(t, cast_uint(key) - 1, tag, val); return HOK; /* success */ } else @@ -1118,7 +1118,7 @@ void luaH_finishset (lua_State *L, Table *t, const TValue *key, } else { /* array entry */ hres = ~hres; /* real index */ - obj2arr(t, hres, value); + obj2arr(t, cast_uint(hres), value); } } @@ -1140,7 +1140,7 @@ void luaH_set (lua_State *L, Table *t, const TValue *key, TValue *value) { */ void luaH_setint (lua_State *L, Table *t, lua_Integer key, TValue *value) { if (keyinarray(t, key)) - obj2arr(t, key - 1, value); + obj2arr(t, cast_uint(key) - 1, value); else { int ok = rawfinishnodeset(getintfromhash(t, key), value); if (!ok) { diff --git a/ltests.c b/ltests.c index 3534d8d501..2dafbee5f7 100644 --- a/ltests.c +++ b/ltests.c @@ -1043,7 +1043,7 @@ static int table_query (lua_State *L) { } else if (cast_uint(i) < asize) { lua_pushinteger(L, i); - arr2obj(t, i, s2v(L->top.p)); + arr2obj(t, cast_uint(i), s2v(L->top.p)); api_incr_top(L); lua_pushnil(L); } From 853311e5b1c1d9fe9d006e3a4f322e9916764933 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 28 Oct 2024 10:54:36 -0300 Subject: [PATCH 0962/1145] Table rehash can resize only the hash part If there are no integer keys outside the array part, there is no reason to resize it, saving the time to count its elements. Moreover, assignments to non-integer keys will not collapse a table created with 'table.create'. --- ltable.c | 33 +++++++++++++++++++-------------- testes/nextvar.lua | 25 ++++++++++++++++++++++--- 2 files changed, 41 insertions(+), 17 deletions(-) diff --git a/ltable.c b/ltable.c index a36a993f38..86ef109243 100644 --- a/ltable.c +++ b/ltable.c @@ -512,7 +512,7 @@ static unsigned numusearray (const Table *t, unsigned *nums) { int lg; unsigned int ttlg; /* 2^lg */ unsigned int ause = 0; /* summation of 'nums' */ - unsigned int i = 1; /* count to traverse all array keys */ + unsigned int i = 1; /* index to traverse all array keys */ unsigned int asize = limitasasize(t); /* real array size */ /* traverse each slice */ for (lg = 0, ttlg = 1; lg <= MAXABITS; lg++, ttlg *= 2) { @@ -766,22 +766,27 @@ void luaH_resizearray (lua_State *L, Table *t, unsigned int nasize) { ** nums[i] = number of keys 'k' where 2^(i - 1) < k <= 2^i */ static void rehash (lua_State *L, Table *t, const TValue *ek) { - unsigned int asize; /* optimal size for array part */ - unsigned int na; /* number of keys in the array part */ - unsigned int nums[MAXABITS + 1]; - int i; - unsigned totaluse; + unsigned asize; /* optimal size for array part */ + unsigned na = 0; /* number of keys candidate for the array part */ + unsigned nums[MAXABITS + 1]; + unsigned i; + unsigned totaluse; /* total number of keys */ for (i = 0; i <= MAXABITS; i++) nums[i] = 0; /* reset counts */ setlimittosize(t); - na = numusearray(t, nums); /* count keys in array part */ - totaluse = na; /* all those keys are integer keys */ - totaluse += numusehash(t, nums, &na); /* count keys in hash part */ - /* count extra key */ + totaluse = 1; /* count extra key */ if (ttisinteger(ek)) - na += countint(ivalue(ek), nums); - totaluse++; - /* compute new size for array part */ - asize = computesizes(nums, &na); + na += countint(ivalue(ek), nums); /* extra key may go to array */ + totaluse += numusehash(t, nums, &na); /* count keys in hash part */ + if (na == 0) { + /* no new keys to enter array part; keep it with the same size */ + asize = luaH_realasize(t); + } + else { /* compute best size for array part */ + unsigned n = numusearray(t, nums); /* count keys in array part */ + totaluse += n; /* all keys in array part are keys */ + na += n; /* all keys in array part are candidates for new array part */ + asize = computesizes(nums, &na); /* compute new size for array part */ + } /* resize the table to new computed sizes */ luaH_resize(L, t, asize, totaluse - na); } diff --git a/testes/nextvar.lua b/testes/nextvar.lua index 5d8796f78e..cee77f76ff 100644 --- a/testes/nextvar.lua +++ b/testes/nextvar.lua @@ -39,13 +39,32 @@ do -- rehash moving elements from array to hash for i = 5, 95 do a[i] = nil end check(a, 128, 0) - a.x = 1 -- force a re-hash - check(a, 4, 8) + a[129] = 1 -- force a re-hash + check(a, 4, 8) -- keys larger than 4 go to the hash part for i = 1, 4 do assert(a[i] == i) end for i = 5, 95 do assert(a[i] == nil) end for i = 96, 100 do assert(a[i] == i) end - assert(a.x == 1) + assert(a[129] == 1) +end + + +do -- growing hash part keeping array part + local a = table.create(1000) + check(a, 1000, 0) + a.x = 10 + check(a, 1000, 1) -- array part keeps its elements +end + + +do -- "growing" length of a prebuilt table + local N = 100 + local a = table.create(N) + for i = 1, N do + a[#a + 1] = true + assert(#a == i) + end + check(a, N, 0) end From 0de81911525bc62bc2a8fc52a368102afed7022b Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 28 Oct 2024 14:15:21 -0300 Subject: [PATCH 0963/1145] New structure to count keys in a table for rehashing --- ltable.c | 115 +++++++++++++++++++++++++++++++------------------------ 1 file changed, 65 insertions(+), 50 deletions(-) diff --git a/ltable.c b/ltable.c index 86ef109243..3451445c62 100644 --- a/ltable.c +++ b/ltable.c @@ -456,15 +456,29 @@ static int keyinarray (Table *t, lua_Integer key) { ** ============================================================== */ + /* -** Compute the optimal size for the array part of table 't'. 'nums' is a -** "count array" where 'nums[i]' is the number of integers in the table -** between 2^(i - 1) + 1 and 2^i. 'pna' enters with the total number of -** integer keys in the table and leaves with the number of keys that -** will go to the array part; return the optimal size. (The condition -** 'twotoi > 0' in the for loop stops the loop if 'twotoi' overflows.) +** Structure to count the keys in a table. +** 'total' is the total number of keys in the table. +** 'na' is the number of *array indices* in the table (see 'arrayindex'). +** 'nums' is a "count array" where 'nums[i]' is the number of integer +** keys between 2^(i - 1) + 1 and 2^i. Note that 'na' is the summation +** of 'nums'. */ -static unsigned computesizes (unsigned nums[], unsigned *pna) { +typedef struct { + unsigned total; + unsigned na; + unsigned nums[MAXABITS + 1]; +} Counters; + +/* +** Compute the optimal size for the array part of table 't'. +** 'ct->na' enters with the total number of array indices in the table +** and leaves with the number of keys that will go to the array part; +** return the optimal size. (The condition 'twotoi > 0' in the for loop +** stops the loop if 'twotoi' overflows.) +*/ +static unsigned computesizes (Counters *ct) { int i; unsigned int twotoi; /* 2^i (candidate for optimal size) */ unsigned int a = 0; /* number of elements smaller than 2^i */ @@ -472,28 +486,26 @@ static unsigned computesizes (unsigned nums[], unsigned *pna) { unsigned int optimal = 0; /* optimal size for array part */ /* loop while keys can fill more than half of total size */ for (i = 0, twotoi = 1; - twotoi > 0 && *pna > twotoi / 2; + twotoi > 0 && ct->na > twotoi / 2; i++, twotoi *= 2) { - a += nums[i]; + a += ct->nums[i]; if (a > twotoi/2) { /* more than half elements present? */ optimal = twotoi; /* optimal size (till now) */ na = a; /* all elements up to 'optimal' will go to array part */ } } lua_assert((optimal == 0 || optimal / 2 < na) && na <= optimal); - *pna = na; + ct->na = na; return optimal; } -static unsigned countint (lua_Integer key, unsigned int *nums) { +static void countint (lua_Integer key, Counters *ct) { unsigned int k = arrayindex(key); - if (k != 0) { /* is 'key' an appropriate array index? */ - nums[luaO_ceillog2(k)]++; /* count as such */ - return 1; + if (k != 0) { /* is 'key' an array index? */ + ct->nums[luaO_ceillog2(k)]++; /* count as such */ + ct->na++; } - else - return 0; } @@ -504,11 +516,9 @@ l_sinline int arraykeyisempty (const Table *t, lua_Unsigned key) { /* -** Count keys in array part of table 't': Fill 'nums[i]' with -** number of keys that will go into corresponding slice and return -** total number of non-nil keys. +** Count keys in array part of table 't'. */ -static unsigned numusearray (const Table *t, unsigned *nums) { +static void numusearray (const Table *t, Counters *ct) { int lg; unsigned int ttlg; /* 2^lg */ unsigned int ause = 0; /* summation of 'nums' */ @@ -528,27 +538,29 @@ static unsigned numusearray (const Table *t, unsigned *nums) { if (!arraykeyisempty(t, i)) lc++; } - nums[lg] += lc; + ct->nums[lg] += lc; ause += lc; } - return ause; + ct->total += ause; + ct->na += ause; } -static unsigned numusehash (const Table *t, unsigned *nums, unsigned *pna) { - unsigned totaluse = 0; /* total number of elements */ - unsigned ause = 0; /* elements added to 'nums' (can go to array part) */ +/* +** Count keys in hash part of table 't'. +*/ +static void numusehash (const Table *t, Counters *ct) { unsigned i = sizenode(t); + unsigned total = 0; while (i--) { Node *n = &t->node[i]; if (!isempty(gval(n))) { + total++; if (keyisinteger(n)) - ause += countint(keyival(n), nums); - totaluse++; + countint(keyival(n), ct); } } - *pna += ause; - return totaluse; + ct->total += total; } @@ -566,12 +578,11 @@ static size_t concretesize (unsigned int size) { ** do nothing. Else, if new size is zero, free the old array. (It must ** be present, as the sizes are different.) Otherwise, allocate a new ** array, move the common elements to new proper position, and then -** frees old array. -** When array grows, we could reallocate it, but we still would need -** to move the elements to their new position, so the copy implicit -** in realloc is a waste. When array shrinks, it always erases some -** elements that should still be in the array, so we must reallocate in -** two steps anyway. It is simpler to always reallocate in two steps. +** frees the old array. +** We could reallocate the array, but we still would need to move the +** elements to their new position, so the copy implicit in realloc is a +** waste. Moreover, most allocators will move the array anyway when the +** new size is double the old one (the most common case). */ static Value *resizearray (lua_State *L , Table *t, unsigned oldasize, @@ -590,10 +601,10 @@ static Value *resizearray (lua_State *L , Table *t, if (np == NULL) /* allocation error? */ return NULL; if (oldasize > 0) { + /* move common elements to new position */ Value *op = t->array - oldasize; /* real original array */ unsigned tomove = (oldasize < newasize) ? oldasize : newasize; lua_assert(tomove > 0); - /* move common elements to new position */ memcpy(np + newasize - tomove, op + oldasize - tomove, concretesize(tomove)); @@ -723,6 +734,9 @@ static void clearNewSlice (Table *t, unsigned oldasize, unsigned newasize) { ** into the table, initializes the new part of the array (if any) with ** nils and reinserts the elements of the old hash back into the new ** parts of the table. +** Note that if the new size for the arry part ('newasize') is equal to +** the old one ('oldasize'), this function will do nothing with that +** part. */ void luaH_resize (lua_State *L, Table *t, unsigned newasize, unsigned nhsize) { @@ -762,33 +776,34 @@ void luaH_resizearray (lua_State *L, Table *t, unsigned int nasize) { luaH_resize(L, t, nasize, nsize); } + /* -** nums[i] = number of keys 'k' where 2^(i - 1) < k <= 2^i +** Rehash a table. First, count its keys. If there are array indices +** outside the array part, compute the new best size for that part. +** Then, resize the table. */ static void rehash (lua_State *L, Table *t, const TValue *ek) { unsigned asize; /* optimal size for array part */ - unsigned na = 0; /* number of keys candidate for the array part */ - unsigned nums[MAXABITS + 1]; + Counters ct; unsigned i; - unsigned totaluse; /* total number of keys */ - for (i = 0; i <= MAXABITS; i++) nums[i] = 0; /* reset counts */ setlimittosize(t); - totaluse = 1; /* count extra key */ + /* reset counts */ + for (i = 0; i <= MAXABITS; i++) ct.nums[i] = 0; + ct.na = 0; + ct.total = 1; /* count extra key */ if (ttisinteger(ek)) - na += countint(ivalue(ek), nums); /* extra key may go to array */ - totaluse += numusehash(t, nums, &na); /* count keys in hash part */ - if (na == 0) { + countint(ivalue(ek), &ct); /* extra key may go to array */ + numusehash(t, &ct); /* count keys in hash part */ + if (ct.na == 0) { /* no new keys to enter array part; keep it with the same size */ asize = luaH_realasize(t); } else { /* compute best size for array part */ - unsigned n = numusearray(t, nums); /* count keys in array part */ - totaluse += n; /* all keys in array part are keys */ - na += n; /* all keys in array part are candidates for new array part */ - asize = computesizes(nums, &na); /* compute new size for array part */ + numusearray(t, &ct); /* count keys in array part */ + asize = computesizes(&ct); /* compute new size for array part */ } /* resize the table to new computed sizes */ - luaH_resize(L, t, asize, totaluse - na); + luaH_resize(L, t, asize, ct.total - ct.na); } From 2491b87c10db530eac2f3d81cd39f95875d16cd5 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 13 Nov 2024 13:37:24 -0300 Subject: [PATCH 0964/1145] New rule for size of array part Array part needs 1/3 of its elements filled, instead of 1/2. Array entries use ~1/3 the memory of hash entries, so this new rule still ensures that array parts do not use more memory than keeping the values in the hash, while allowing more uses of the array part, which is more efficient than the hash. --- ltable.c | 27 ++++++++++++----- ltests.c | 11 ++++--- testes/nextvar.lua | 72 ++++++++++++++++++++++++++++++++++------------ 3 files changed, 81 insertions(+), 29 deletions(-) diff --git a/ltable.c b/ltable.c index 3451445c62..923f3eaa8e 100644 --- a/ltable.c +++ b/ltable.c @@ -471,12 +471,23 @@ typedef struct { unsigned nums[MAXABITS + 1]; } Counters; + +/* +** Check whether it is worth to use 'na' array entries instead of 'nh' +** hash nodes. (A hash node uses ~3 times more memory than an array +** entry: Two values plus 'next' versus one value.) Evaluate with size_t +** to avoid overflows. +*/ +#define arrayXhash(na,nh) (cast_sizet(na) <= cast_sizet(nh) * 3) + /* ** Compute the optimal size for the array part of table 't'. +** This size maximizes the number of elements going to the array part +** while satisfying the condition 'arrayXhash' with the use of memory if +** all those elements went to the hash part. ** 'ct->na' enters with the total number of array indices in the table ** and leaves with the number of keys that will go to the array part; -** return the optimal size. (The condition 'twotoi > 0' in the for loop -** stops the loop if 'twotoi' overflows.) +** return the optimal size for the array part. */ static unsigned computesizes (Counters *ct) { int i; @@ -484,17 +495,19 @@ static unsigned computesizes (Counters *ct) { unsigned int a = 0; /* number of elements smaller than 2^i */ unsigned int na = 0; /* number of elements to go to array part */ unsigned int optimal = 0; /* optimal size for array part */ - /* loop while keys can fill more than half of total size */ + /* traverse slices while 'twotoi' does not overflow and total of array + indices still can satisfy 'arrayXhash' against the array size */ for (i = 0, twotoi = 1; - twotoi > 0 && ct->na > twotoi / 2; + twotoi > 0 && arrayXhash(twotoi, ct->na); i++, twotoi *= 2) { - a += ct->nums[i]; - if (a > twotoi/2) { /* more than half elements present? */ + unsigned nums = ct->nums[i]; + a += nums; + if (nums > 0 && /* grows array only if it gets more elements... */ + arrayXhash(twotoi, a)) { /* ...while using "less memory" */ optimal = twotoi; /* optimal size (till now) */ na = a; /* all elements up to 'optimal' will go to array part */ } } - lua_assert((optimal == 0 || optimal / 2 < na) && na <= optimal); ct->na = na; return optimal; } diff --git a/ltests.c b/ltests.c index 2dafbee5f7..8191f14a5b 100644 --- a/ltests.c +++ b/ltests.c @@ -1043,7 +1043,10 @@ static int table_query (lua_State *L) { } else if (cast_uint(i) < asize) { lua_pushinteger(L, i); - arr2obj(t, cast_uint(i), s2v(L->top.p)); + if (!tagisempty(*getArrTag(t, i))) + arr2obj(t, cast_uint(i), s2v(L->top.p)); + else + setnilvalue(s2v(L->top.p)); api_incr_top(L); lua_pushnil(L); } @@ -1057,11 +1060,11 @@ static int table_query (lua_State *L) { } else lua_pushliteral(L, ""); - pushobject(L, gval(gnode(t, i))); - if (gnext(&t->node[i]) != 0) - lua_pushinteger(L, gnext(&t->node[i])); + if (!isempty(gval(gnode(t, i)))) + pushobject(L, gval(gnode(t, i))); else lua_pushnil(L); + lua_pushinteger(L, gnext(&t->node[i])); } return 3; } diff --git a/testes/nextvar.lua b/testes/nextvar.lua index cee77f76ff..85c9ff6bb6 100644 --- a/testes/nextvar.lua +++ b/testes/nextvar.lua @@ -9,6 +9,22 @@ local function checkerror (msg, f, ...) end + +---------------------------------------------------------------- +local function printTable (t) + local a, h = T.querytab(t) + print("array:") + for i = 1, a do + print("", T.querytab(t, i - 1)) + end + print("hash:") + for i = 1, h do + print("", T.querytab(t, a + i - 1)) + end +end +---------------------------------------------------------------- + + local function check (t, na, nh) if not T then return end local a, h = T.querytab(t) @@ -106,9 +122,10 @@ else --[ -- testing table sizes -local function mp2 (n) -- minimum power of 2 >= n +-- minimum power of 2 (or zero) >= n +local function mp2 (n) local mp = 2^math.ceil(math.log(n, 2)) - assert(n == 0 or (mp/2 < n and n <= mp)) + assert((mp == 0 or mp/2 < n) and n <= mp) return mp end @@ -123,7 +140,7 @@ end -- testing constructor sizes local sizes = {0, 1, 2, 3, 4, 5, 7, 8, 9, 15, 16, 17, - 30, 31, 32, 33, 34, 254, 255, 256, 500, 1000} + 30, 31, 32, 33, 34, 254, 255, 256, 257, 500, 1001} for _, sa in ipairs(sizes) do -- 'sa' is size of the array part local arr = {"return {"} @@ -167,8 +184,9 @@ end -- testing tables dynamically built local lim = 130 -local a = {}; a[2] = 1; check(a, 0, 1) -a = {}; a[0] = 1; check(a, 0, 1); a[2] = 1; check(a, 0, 2) +local a = {}; a[2] = 1; check(a, 2, 0) +a = {}; a[0] = 1; check(a, 0, 1); +a[2] = 1; check(a, 2, 1) a = {}; a[0] = 1; a[1] = 1; check(a, 1, 1) a = {} for i = 1,lim do @@ -184,28 +202,46 @@ for i = 1,lim do check(a, 0, mp2(i)) end -a = {} -for i=1,16 do a[i] = i end -check(a, 16, 0) do + local a = {} + for i=1,16 do a[i] = i end + check(a, 16, 0) for i=1,11 do a[i] = undef end - for i=30,50 do a[i] = true; a[i] = undef end -- force a rehash (?) - check(a, 0, 8) -- 5 elements in the table + check(a, 16, 0) + a[30] = true -- force a rehash + a[30] = undef + check(a, 0, 8) -- 5 elements in the hash part: [12]-[16] a[10] = 1 - for i=30,50 do a[i] = true; a[i] = undef end -- force a rehash (?) - check(a, 0, 8) -- only 6 elements in the table + for i=30,50 do a[i] = true; a[i] = undef end -- force a rehash + check(a, 16, 1) for i=1,14 do a[i] = true; a[i] = undef end - for i=18,50 do a[i] = true; a[i] = undef end -- force a rehash (?) - check(a, 0, 4) -- only 2 elements ([15] and [16]) + check(a, 16, 1) -- no rehash... + a[31] = true; a[32] = true -- force a rehash + check(a, 0, 4) -- [15], [16], [31], [32] end -- reverse filling -for i=1,lim do +do + local N = 2^10 local a = {} - for i=i,1,-1 do a[i] = i end -- fill in reverse - check(a, mp2(i), 0) + for i = N, 1, -1 do a[i] = i end -- fill in reverse + check(a, mp2(N), 0) end + +do -- "almost sparse" arrays + -- create table with holes in 1/3 of its entries; all its + -- elements are always in the array part + local a = {} + for i = 1, 257 do + if i % 3 ~= 1 then + a[i] = true + check(a, mp2(i), 0) + end + end +end + + -- size tests for vararg lim = 35 local function foo (n, ...) @@ -840,7 +876,7 @@ do co() -- start coroutine co(1) -- continue after yield assert(res[1] == 30 and res[2] == 20 and res[3] == 10 and #res == 3) - + end print"OK" From 9a91fe1640ddbe5b55e7454541059372b971f400 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 14 Nov 2024 11:48:25 -0300 Subject: [PATCH 0965/1145] Add extra size when resizing tables with deleted keys MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Without this extra space, sequences of insertions/deletions (and some other uses) can have unpexpected low performances. See the added tests for an example, and *Mathematical Models to Analyze Lua Hybrid Tables and Why They Need a Fix* (Martínez, Nicaud, Rotondo; arXiv:2208.13602v2) for detais. --- ltable.c | 26 +++++++++++++++---- testes/nextvar.lua | 62 +++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 82 insertions(+), 6 deletions(-) diff --git a/ltable.c b/ltable.c index 923f3eaa8e..0192d039a9 100644 --- a/ltable.c +++ b/ltable.c @@ -461,6 +461,7 @@ static int keyinarray (Table *t, lua_Integer key) { ** Structure to count the keys in a table. ** 'total' is the total number of keys in the table. ** 'na' is the number of *array indices* in the table (see 'arrayindex'). +** 'deleted' is true if there are deleted nodes in the hash part. ** 'nums' is a "count array" where 'nums[i]' is the number of integer ** keys between 2^(i - 1) + 1 and 2^i. Note that 'na' is the summation ** of 'nums'. @@ -468,6 +469,7 @@ static int keyinarray (Table *t, lua_Integer key) { typedef struct { unsigned total; unsigned na; + int deleted; unsigned nums[MAXABITS + 1]; } Counters; @@ -560,14 +562,21 @@ static void numusearray (const Table *t, Counters *ct) { /* -** Count keys in hash part of table 't'. +** Count keys in hash part of table 't'. As this only happens during +** a rehash, all nodes have been used. A node can have a nil value only +** if it was deleted after being created. */ static void numusehash (const Table *t, Counters *ct) { unsigned i = sizenode(t); unsigned total = 0; while (i--) { Node *n = &t->node[i]; - if (!isempty(gval(n))) { + if (isempty(gval(n))) { + /* entry was deleted; key cannot be nil */ + lua_assert(isdummy(t) || !keyisnil(n)); + ct->deleted = 1; + } + else { total++; if (keyisinteger(n)) countint(keyival(n), ct); @@ -799,10 +808,12 @@ static void rehash (lua_State *L, Table *t, const TValue *ek) { unsigned asize; /* optimal size for array part */ Counters ct; unsigned i; + unsigned nsize; /* size for the hash part */ setlimittosize(t); /* reset counts */ for (i = 0; i <= MAXABITS; i++) ct.nums[i] = 0; ct.na = 0; + ct.deleted = 0; ct.total = 1; /* count extra key */ if (ttisinteger(ek)) countint(ivalue(ek), &ct); /* extra key may go to array */ @@ -815,12 +826,17 @@ static void rehash (lua_State *L, Table *t, const TValue *ek) { numusearray(t, &ct); /* count keys in array part */ asize = computesizes(&ct); /* compute new size for array part */ } + /* all keys not in the array part go to the hash part */ + nsize = ct.total - ct.na; + if (ct.deleted) { /* table has deleted entries? */ + /* insertion-deletion-insertion: give hash some extra size to + avoid constant resizings */ + nsize += nsize >> 2; + } /* resize the table to new computed sizes */ - luaH_resize(L, t, asize, ct.total - ct.na); + luaH_resize(L, t, asize, nsize); } - - /* ** }============================================================= */ diff --git a/testes/nextvar.lua b/testes/nextvar.lua index 85c9ff6bb6..00e509f87d 100644 --- a/testes/nextvar.lua +++ b/testes/nextvar.lua @@ -23,6 +23,12 @@ local function printTable (t) end end ---------------------------------------------------------------- +local function countentries (t) + local e = 0 + for _ in pairs(t) do e = e + 1 end + return e +end +---------------------------------------------------------------- local function check (t, na, nh) @@ -115,6 +121,24 @@ do -- overflow (must wrap-around) assert(k == nil) end + +do + -- alternate insertions and deletions in an almost full hash. + -- In versions pre-5.5, that causes constant rehashings and + -- takes a long time to complete. + local a = {} + for i = 1, 2^11 - 1 do + a[i .. ""] = true + end + + for i = 1, 1e5 do + local key = i .. "." + a[key] = true + a[key] = nil + end + assert(countentries(a) == 2^11 - 1) +end + if not T then (Message or print) ('\n >>> testC not active: skipping tests for table sizes <<<\n') @@ -202,6 +226,23 @@ for i = 1,lim do check(a, 0, mp2(i)) end + +-- insert and delete elements until a rehash occurr. Caller must ensure +-- that a rehash will change the shape of the table. Must repeat because +-- the insertion may collide with the deleted element, and then there is +-- no rehash. +local function forcerehash (t) + local na, nh = T.querytab(t) + local i = 10000 + repeat + i = i + 1 + t[i] = true + t[i] = undef + local nna, nnh = T.querytab(t) + until nna ~= na or nnh ~= nh +end + + do local a = {} for i=1,16 do a[i] = i end @@ -212,7 +253,7 @@ do a[30] = undef check(a, 0, 8) -- 5 elements in the hash part: [12]-[16] a[10] = 1 - for i=30,50 do a[i] = true; a[i] = undef end -- force a rehash + forcerehash(a) check(a, 16, 1) for i=1,14 do a[i] = true; a[i] = undef end check(a, 16, 1) -- no rehash... @@ -242,6 +283,25 @@ do -- "almost sparse" arrays end +do + -- alternate insertions and deletions should give some extra + -- space for the hash part. Otherwise, a mix of insertions/deletions + -- could cause too many rehashes. (See the other test for "alternate + -- insertions and deletions" in this file.) + local a = {} + for i = 1, 256 do + a[i .. ""] = true + end + check(a, 0, 256) -- hash part is full + a["256"] = nil -- delete a key + forcerehash(a) + -- table has only 255 elements, but it got some extra space; + -- otherwise, almost each delete-insert would rehash the table again. + assert(countentries(a) == 255) + check(a, 0, 512) +end + + -- size tests for vararg lim = 35 local function foo (n, ...) From 8a4419b119ea9d03bb20b208587b0bbd6f473cdc Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 15 Nov 2024 10:48:52 -0300 Subject: [PATCH 0966/1145] Dummy node has a non-nil key That allows 'getfreepos' to treat it like a regular hash part that has a deleted entry. --- ltable.c | 53 ++++++++++++++++++++++++++++------------------------- 1 file changed, 28 insertions(+), 25 deletions(-) diff --git a/ltable.c b/ltable.c index 0192d039a9..981e9df423 100644 --- a/ltable.c +++ b/ltable.c @@ -121,9 +121,15 @@ typedef union { #define dummynode (&dummynode_) +/* +** Common hash part for tables with empty hash parts. That allows all +** tables to have a hash part, avoding an extra check ("is there a hash +** part?") when indexing. Its sole node has an empty value and a key +** (DEADKEY, NULL) that is different from any valid TValue. +*/ static const Node dummynode_ = { {{NULL}, LUA_VEMPTY, /* value's value and type */ - LUA_VNIL, 0, {NULL}} /* key type, next, and key value */ + LUA_TDEADKEY, 0, {NULL}} /* key type, next, and key value */ }; @@ -400,16 +406,20 @@ int luaH_next (lua_State *L, Table *t, StkId key) { } +/* Extra space in Node array if it has a lastfree entry */ +#define extraLastfree(t) (haslastfree(t) ? sizeof(Limbox) : 0) + +/* 'node' size in bytes */ +static size_t sizehash (Table *t) { + return cast_sizet(sizenode(t)) * sizeof(Node) + extraLastfree(t); +} + + static void freehash (lua_State *L, Table *t) { if (!isdummy(t)) { - /* 'node' size in bytes */ - size_t bsize = cast_sizet(sizenode(t)) * sizeof(Node); - char *arr = cast_charp(t->node); - if (haslastfree(t)) { - bsize += sizeof(Limbox); - arr -= sizeof(Limbox); - } - luaM_freearray(L, arr, bsize); + /* get pointer to the beginning of Node array */ + char *arr = cast_charp(t->node) - extraLastfree(t); + luaM_freearray(L, arr, sizehash(t)); } } @@ -572,8 +582,7 @@ static void numusehash (const Table *t, Counters *ct) { while (i--) { Node *n = &t->node[i]; if (isempty(gval(n))) { - /* entry was deleted; key cannot be nil */ - lua_assert(isdummy(t) || !keyisnil(n)); + lua_assert(!keyisnil(n)); /* entry was deleted; key cannot be nil */ ct->deleted = 1; } else { @@ -855,13 +864,9 @@ Table *luaH_new (lua_State *L) { size_t luaH_size (Table *t) { - size_t sz = sizeof(Table) - + luaH_realasize(t) * (sizeof(Value) + 1); - if (!isdummy(t)) { - sz += sizenode(t) * sizeof(Node); - if (haslastfree(t)) - sz += sizeof(Limbox); - } + size_t sz = sizeof(Table) + luaH_realasize(t) * (sizeof(Value) + 1); + if (!isdummy(t)) + sz += sizehash(t); return sz; } @@ -887,13 +892,11 @@ static Node *getfreepos (Table *t) { } } else { /* no 'lastfree' information */ - if (!isdummy(t)) { - unsigned i = sizenode(t); - while (i--) { /* do a linear search */ - Node *free = gnode(t, i); - if (keyisnil(free)) - return free; - } + unsigned i = sizenode(t); + while (i--) { /* do a linear search */ + Node *free = gnode(t, i); + if (keyisnil(free)) + return free; } } return NULL; /* could not find a free place */ From ee6a4cd1eca4fa736d108e1e0ac3cb9858f7a5ef Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 15 Nov 2024 11:43:32 -0300 Subject: [PATCH 0967/1145] Ease slightly making Lua with C89 --- luaconf.h | 5 +++++ makefile | 4 +++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/luaconf.h b/luaconf.h index afc1b8b526..bd39465052 100644 --- a/luaconf.h +++ b/luaconf.h @@ -87,6 +87,11 @@ #endif +#if defined(LUA_USE_C89) && defined(LUA_USE_POSIX) +#error "Posix is not compatible with C89" +#endif + + /* @@ LUAI_IS32INT is true iff 'int' has (at least) 32 bits. */ diff --git a/makefile b/makefile index b37fdb28fe..58de5ddb59 100644 --- a/makefile +++ b/makefile @@ -69,7 +69,9 @@ CWARNS= $(CWARNSCPP) $(CWARNSC) $(CWARNGCC) LOCAL = $(TESTS) $(CWARNS) -# enable Linux goodies +# To enable Linux goodies, -DLUA_USE_LINUX +# For C89, "-std=c89 -DLUA_USE_C89" +# Note that Linux/Posix options are not compatible with C89 MYCFLAGS= $(LOCAL) -std=c99 -DLUA_USE_LINUX MYLDFLAGS= $(LOCAL) -Wl,-E MYLIBS= -ldl From d4247befa18a7911c56e7110154ad73574cd6648 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 15 Nov 2024 11:57:18 -0300 Subject: [PATCH 0968/1145] New macro 'assert_code' It allows code that is only used by assertions but that are not assertions (e.g., declaration of a variable used in a later assertion). --- llimits.h | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/llimits.h b/llimits.h index f189048ca7..6cf35e0cf7 100644 --- a/llimits.h +++ b/llimits.h @@ -102,18 +102,19 @@ typedef LUAI_UACINT l_uacInt; #undef NDEBUG #include #define lua_assert(c) assert(c) +#define assert_code(c) c #endif #if defined(lua_assert) -#define check_exp(c,e) (lua_assert(c), (e)) -/* to avoid problems with conditions too long */ -#define lua_longassert(c) ((c) ? (void)0 : lua_assert(0)) #else #define lua_assert(c) ((void)0) -#define check_exp(c,e) (e) -#define lua_longassert(c) ((void)0) +#define assert_code(c) ((void)0) #endif +#define check_exp(c,e) (lua_assert(c), (e)) +/* to avoid problems with conditions too long */ +#define lua_longassert(c) assert_code((c) ? (void)0 : lua_assert(0)) + /* macro to avoid warnings about unused variables */ #if !defined(UNUSED) From a4762b6ffe74f5878882ef238d37bfa92d90e418 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 15 Nov 2024 12:04:53 -0300 Subject: [PATCH 0969/1145] 'objsize' returns 'l_mem' Sums of size_t may not fit in a size_t. --- lfunc.c | 10 +++++----- lfunc.h | 2 +- lgc.c | 39 ++++++++++++++++++++++++++------------- lstate.c | 5 +++-- lstate.h | 2 +- ltable.c | 5 +++-- ltable.h | 2 +- 7 files changed, 40 insertions(+), 25 deletions(-) diff --git a/lfunc.c b/lfunc.c index 2b0412818d..0ea05e009a 100644 --- a/lfunc.c +++ b/lfunc.c @@ -264,16 +264,16 @@ Proto *luaF_newproto (lua_State *L) { } -size_t luaF_protosize (Proto *p) { - size_t sz = sizeof(Proto) +lu_mem luaF_protosize (Proto *p) { + lu_mem sz = cast(lu_mem, sizeof(Proto)) + cast_uint(p->sizep) * sizeof(Proto*) + cast_uint(p->sizek) * sizeof(TValue) + cast_uint(p->sizelocvars) * sizeof(LocVar) + cast_uint(p->sizeupvalues) * sizeof(Upvaldesc); if (!(p->flag & PF_FIXED)) { - sz += cast_uint(p->sizecode) * sizeof(Instruction) - + cast_uint(p->sizelineinfo) * sizeof(lu_byte) - + cast_uint(p->sizeabslineinfo) * sizeof(AbsLineInfo); + sz += cast_uint(p->sizecode) * sizeof(Instruction); + sz += cast_uint(p->sizelineinfo) * sizeof(lu_byte); + sz += cast_uint(p->sizeabslineinfo) * sizeof(AbsLineInfo); } return sz; } diff --git a/lfunc.h b/lfunc.h index b96510747f..b981edebb8 100644 --- a/lfunc.h +++ b/lfunc.h @@ -56,7 +56,7 @@ LUAI_FUNC void luaF_newtbcupval (lua_State *L, StkId level); LUAI_FUNC void luaF_closeupval (lua_State *L, StkId level); LUAI_FUNC StkId luaF_close (lua_State *L, StkId level, int status, int yy); LUAI_FUNC void luaF_unlinkupval (UpVal *uv); -LUAI_FUNC size_t luaF_protosize (Proto *p); +LUAI_FUNC lu_mem luaF_protosize (Proto *p); LUAI_FUNC void luaF_freeproto (lua_State *L, Proto *f); LUAI_FUNC const char *luaF_getlocalname (const Proto *func, int local_number, int pc); diff --git a/lgc.c b/lgc.c index e8b9ad5e36..e4130bd560 100644 --- a/lgc.c +++ b/lgc.c @@ -110,43 +110,54 @@ static void entersweep (lua_State *L); #define gnodelast(h) gnode(h, cast_sizet(sizenode(h))) -static size_t objsize (GCObject *o) { +static l_mem objsize (GCObject *o) { + lu_mem res; switch (o->tt) { case LUA_VTABLE: { - return luaH_size(gco2t(o)); + res = luaH_size(gco2t(o)); + break; } case LUA_VLCL: { LClosure *cl = gco2lcl(o); - return sizeLclosure(cl->nupvalues); + res = sizeLclosure(cl->nupvalues); + break; } case LUA_VCCL: { CClosure *cl = gco2ccl(o); - return sizeCclosure(cl->nupvalues); + res = sizeCclosure(cl->nupvalues); + break; break; } case LUA_VUSERDATA: { Udata *u = gco2u(o); - return sizeudata(u->nuvalue, u->len); + res = sizeudata(u->nuvalue, u->len); + break; } case LUA_VPROTO: { - return luaF_protosize(gco2p(o)); + res = luaF_protosize(gco2p(o)); + break; } case LUA_VTHREAD: { - return luaE_threadsize(gco2th(o)); + res = luaE_threadsize(gco2th(o)); + break; } case LUA_VSHRSTR: { TString *ts = gco2ts(o); - return sizestrshr(cast_uint(ts->shrlen)); + res = sizestrshr(cast_uint(ts->shrlen)); + break; } case LUA_VLNGSTR: { TString *ts = gco2ts(o); - return luaS_sizelngstr(ts->u.lnglen, ts->shrlen); + res = luaS_sizelngstr(ts->u.lnglen, ts->shrlen); + break; } case LUA_VUPVAL: { - return sizeof(UpVal); + res = sizeof(UpVal); + break; } - default: lua_assert(0); return 0; + default: res = 0; lua_assert(0); } + return cast(l_mem, res); } @@ -327,7 +338,7 @@ GCObject *luaC_newobj (lua_State *L, lu_byte tt, size_t sz) { ** (only closures can), and a userdata's metatable must be a table. */ static void reallymarkobject (global_State *g, GCObject *o) { - g->GCmarked += cast(l_mem, objsize(o)); + g->GCmarked += objsize(o); switch (o->tt) { case LUA_VSHRSTR: case LUA_VLNGSTR: { @@ -803,6 +814,7 @@ static void freeupval (lua_State *L, UpVal *uv) { static void freeobj (lua_State *L, GCObject *o) { + assert_code(l_mem newmem = gettotalbytes(G(L)) - objsize(o)); switch (o->tt) { case LUA_VPROTO: luaF_freeproto(L, gco2p(o)); @@ -846,6 +858,7 @@ static void freeobj (lua_State *L, GCObject *o) { } default: lua_assert(0); } + lua_assert(gettotalbytes(G(L)) == newmem); } @@ -1167,7 +1180,7 @@ static GCObject **sweepgen (lua_State *L, global_State *g, GCObject **p, lua_assert(age != G_OLD1); /* advanced in 'markold' */ setage(curr, nextage[age]); if (getage(curr) == G_OLD1) { - addedold += cast(l_mem, objsize(curr)); /* bytes becoming old */ + addedold += objsize(curr); /* bytes becoming old */ if (*pfirstold1 == NULL) *pfirstold1 = curr; /* first OLD1 object in the list */ } diff --git a/lstate.c b/lstate.c index a8db9477c3..0e1cb01ebb 100644 --- a/lstate.c +++ b/lstate.c @@ -257,8 +257,9 @@ static void preinit_thread (lua_State *L, global_State *g) { } -size_t luaE_threadsize (lua_State *L) { - size_t sz = sizeof(LX) + cast_uint(L->nci) * sizeof(CallInfo); +lu_mem luaE_threadsize (lua_State *L) { + lu_mem sz = cast(lu_mem, sizeof(LX)) + + cast_uint(L->nci) * sizeof(CallInfo); if (L->stack.p != NULL) sz += cast_uint(stacksize(L) + EXTRA_STACK) * sizeof(StackValue); return sz; diff --git a/lstate.h b/lstate.h index a1b463c6f0..d1bc0542ab 100644 --- a/lstate.h +++ b/lstate.h @@ -416,7 +416,7 @@ union GCUnion { LUAI_FUNC void luaE_setdebt (global_State *g, l_mem debt); LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1); -LUAI_FUNC size_t luaE_threadsize (lua_State *L); +LUAI_FUNC lu_mem luaE_threadsize (lua_State *L); LUAI_FUNC CallInfo *luaE_extendCI (lua_State *L); LUAI_FUNC void luaE_shrinkCI (lua_State *L); LUAI_FUNC void luaE_checkcstack (lua_State *L); diff --git a/ltable.c b/ltable.c index 981e9df423..21fafa5e76 100644 --- a/ltable.c +++ b/ltable.c @@ -863,8 +863,9 @@ Table *luaH_new (lua_State *L) { } -size_t luaH_size (Table *t) { - size_t sz = sizeof(Table) + luaH_realasize(t) * (sizeof(Value) + 1); +lu_mem luaH_size (Table *t) { + lu_mem sz = cast(lu_mem, sizeof(Table)) + + luaH_realasize(t) * (sizeof(Value) + 1); if (!isdummy(t)) sz += sizehash(t); return sz; diff --git a/ltable.h b/ltable.h index c352da3840..17e4fb4a10 100644 --- a/ltable.h +++ b/ltable.h @@ -163,7 +163,7 @@ LUAI_FUNC Table *luaH_new (lua_State *L); LUAI_FUNC void luaH_resize (lua_State *L, Table *t, unsigned nasize, unsigned nhsize); LUAI_FUNC void luaH_resizearray (lua_State *L, Table *t, unsigned nasize); -LUAI_FUNC size_t luaH_size (Table *t); +LUAI_FUNC lu_mem luaH_size (Table *t); LUAI_FUNC void luaH_free (lua_State *L, Table *t); LUAI_FUNC int luaH_next (lua_State *L, Table *t, StkId key); LUAI_FUNC lua_Unsigned luaH_getn (Table *t); From f12ce4029dfbce7b89ec136e6b7ba5f6bca039da Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 15 Nov 2024 15:25:11 -0300 Subject: [PATCH 0970/1145] More integration of 'nresults' into 'callstatus' --- lapi.c | 3 --- ldo.c | 25 +++++++++++++------------ lstate.h | 33 ++++++++++++++++++++------------- 3 files changed, 33 insertions(+), 28 deletions(-) diff --git a/lapi.c b/lapi.c index 631cf44e8b..01abfc15ad 100644 --- a/lapi.c +++ b/lapi.c @@ -1023,9 +1023,6 @@ LUA_API int lua_setiuservalue (lua_State *L, int idx, int n) { */ -#define MAXRESULTS 250 - - #define checkresults(L,na,nr) \ (api_check(L, (nr) == LUA_MULTRET \ || (L->ci->top.p - L->top.p >= (nr) - (na)), \ diff --git a/ldo.c b/ldo.c index e75a79ab2f..72a1e306eb 100644 --- a/ldo.c +++ b/ldo.c @@ -564,12 +564,12 @@ void luaD_poscall (lua_State *L, CallInfo *ci, int nres) { #define next_ci(L) (L->ci->next ? L->ci->next : luaE_extendCI(L)) -l_sinline CallInfo *prepCallInfo (lua_State *L, StkId func, int nresults, - l_uint32 mask, StkId top) { +l_sinline CallInfo *prepCallInfo (lua_State *L, StkId func, unsigned status, + StkId top) { CallInfo *ci = L->ci = next_ci(L); /* new frame */ ci->func.p = func; - lua_assert(((nresults + 1) & ~CIST_NRESULTS) == 0); - ci->callstatus = mask | cast(l_uint32, nresults + 1); + lua_assert((status & ~(CIST_NRESULTS | CIST_C)) == 0); + ci->callstatus = status; ci->top.p = top; return ci; } @@ -578,12 +578,12 @@ l_sinline CallInfo *prepCallInfo (lua_State *L, StkId func, int nresults, /* ** precall for C functions */ -l_sinline int precallC (lua_State *L, StkId func, int nresults, +l_sinline int precallC (lua_State *L, StkId func, unsigned status, lua_CFunction f) { int n; /* number of returns */ CallInfo *ci; checkstackp(L, LUA_MINSTACK, func); /* ensure minimum stack size */ - L->ci = ci = prepCallInfo(L, func, nresults, CIST_C, + L->ci = ci = prepCallInfo(L, func, status | CIST_C, L->top.p + LUA_MINSTACK); lua_assert(ci->top.p <= L->stack_last.p); if (l_unlikely(L->hookmask & LUA_MASKCALL)) { @@ -610,9 +610,9 @@ int luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, retry: switch (ttypetag(s2v(func))) { case LUA_VCCL: /* C closure */ - return precallC(L, func, LUA_MULTRET, clCvalue(s2v(func))->f); + return precallC(L, func, LUA_MULTRET + 1, clCvalue(s2v(func))->f); case LUA_VLCF: /* light C function */ - return precallC(L, func, LUA_MULTRET, fvalue(s2v(func))); + return precallC(L, func, LUA_MULTRET + 1, fvalue(s2v(func))); case LUA_VLCL: { /* Lua function */ Proto *p = clLvalue(s2v(func))->p; int fsize = p->maxstacksize; /* frame size */ @@ -651,13 +651,15 @@ int luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, ** original function position. */ CallInfo *luaD_precall (lua_State *L, StkId func, int nresults) { + unsigned status = cast_uint(nresults + 1); + lua_assert(status <= MAXRESULTS + 1); retry: switch (ttypetag(s2v(func))) { case LUA_VCCL: /* C closure */ - precallC(L, func, nresults, clCvalue(s2v(func))->f); + precallC(L, func, status, clCvalue(s2v(func))->f); return NULL; case LUA_VLCF: /* light C function */ - precallC(L, func, nresults, fvalue(s2v(func))); + precallC(L, func, status, fvalue(s2v(func))); return NULL; case LUA_VLCL: { /* Lua function */ CallInfo *ci; @@ -666,7 +668,7 @@ CallInfo *luaD_precall (lua_State *L, StkId func, int nresults) { int nfixparams = p->numparams; int fsize = p->maxstacksize; /* frame size */ checkstackp(L, fsize, func); - L->ci = ci = prepCallInfo(L, func, nresults, 0, func + 1 + fsize); + L->ci = ci = prepCallInfo(L, func, status, func + 1 + fsize); ci->u.l.savedpc = p->code; /* starting point */ for (; narg < nfixparams; narg++) setnilvalue(s2v(L->top.p++)); /* complete missing arguments */ @@ -675,7 +677,6 @@ CallInfo *luaD_precall (lua_State *L, StkId func, int nresults) { } default: { /* not a function */ func = tryfuncTM(L, func); /* try to get '__call' metamethod */ - /* return luaD_precall(L, func, nresults); */ goto retry; /* try again with metamethod */ } } diff --git a/lstate.h b/lstate.h index d1bc0542ab..ab5672130f 100644 --- a/lstate.h +++ b/lstate.h @@ -209,34 +209,41 @@ struct CallInfo { }; +/* +** Maximum expected number of results from a function +** (must fit in CIST_NRESULTS). +*/ +#define MAXRESULTS 250 + + /* ** Bits in CallInfo status */ /* bits 0-7 are the expected number of results from this function + 1 */ -#define CIST_NRESULTS 0xff +#define CIST_NRESULTS 0xffu +/* Bits 8-10 are used for CIST_RECST (see below) */ +#define CIST_RECST 8 /* the offset, not the mask */ /* original value of 'allowhook' */ -#define CIST_OAH (cast(l_uint32, 1) << 8) +#define CIST_OAH (cast(l_uint32, 1) << 11) /* call is running a C function */ -#define CIST_C (cast(l_uint32, 1) << 9) +#define CIST_C (CIST_OAH << 1) /* call is on a fresh "luaV_execute" frame */ -#define CIST_FRESH (cast(l_uint32, 1) << 10) +#define CIST_FRESH (CIST_C << 1) /* call is running a debug hook */ -#define CIST_HOOKED (cast(l_uint32, 1) << 11) +#define CIST_HOOKED (CIST_FRESH << 1) /* doing a yieldable protected call */ -#define CIST_YPCALL (cast(l_uint32, 1) << 12) +#define CIST_YPCALL (CIST_HOOKED << 1) /* call was tail called */ -#define CIST_TAIL (cast(l_uint32, 1) << 13) +#define CIST_TAIL (CIST_YPCALL << 1) /* last hook called yielded */ -#define CIST_HOOKYIELD (cast(l_uint32, 1) << 14) +#define CIST_HOOKYIELD (CIST_TAIL << 1) /* function "called" a finalizer */ -#define CIST_FIN (cast(l_uint32, 1) << 15) +#define CIST_FIN (CIST_HOOKYIELD << 1) /* function is closing tbc variables */ -#define CIST_CLSRET (cast(l_uint32, 1) << 16) -/* Bits 17-19 are used for CIST_RECST (see below) */ -#define CIST_RECST 17 /* the offset, not the mask */ +#define CIST_CLSRET (CIST_FIN << 1) #if defined(LUA_COMPAT_LT_LE) /* using __lt for __le */ -#define CIST_LEQ (cast(l_uint32, 1) << 20) +#define CIST_LEQ (CIST_CLSRET << 1) #endif From b117bdb3448778d9e7f9a0302791e8ac3bb97ddd Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Sat, 16 Nov 2024 12:00:28 -0300 Subject: [PATCH 0971/1145] Counter for length of chains of __call metamethods This counter will allow (in a later commit) error messages to correct argument numbers in functions called through __call metamethods. --- ldo.c | 41 +++++++++++++++++++++++++++-------------- lstate.h | 28 +++++++++++++++++----------- manual/manual.of | 4 ++++ testes/calls.lua | 23 ++++++++++++++++++++--- 4 files changed, 68 insertions(+), 28 deletions(-) diff --git a/ldo.c b/ldo.c index 72a1e306eb..cb7e5aef4e 100644 --- a/ldo.c +++ b/ldo.c @@ -464,21 +464,26 @@ static void rethook (lua_State *L, CallInfo *ci, int nres) { /* ** Check whether 'func' has a '__call' metafield. If so, put it in the -** stack, below original 'func', so that 'luaD_precall' can call it. Raise -** an error if there is no '__call' metafield. +** stack, below original 'func', so that 'luaD_precall' can call it. +** Raise an error if there is no '__call' metafield. +** Bits CIST_CCMT in status count how many _call metamethods were +** invoked and how many corresponding extra arguments were pushed. +** (This count will be saved in the 'callstatus' of the call). +** Raise an error if this counter overflows. */ -static StkId tryfuncTM (lua_State *L, StkId func) { +static unsigned tryfuncTM (lua_State *L, StkId func, unsigned status) { const TValue *tm; StkId p; - checkstackp(L, 1, func); /* space for metamethod */ - tm = luaT_gettmbyobj(L, s2v(func), TM_CALL); /* (after previous GC) */ - if (l_unlikely(ttisnil(tm))) - luaG_callerror(L, s2v(func)); /* nothing to call */ + tm = luaT_gettmbyobj(L, s2v(func), TM_CALL); + if (l_unlikely(ttisnil(tm))) /* no metamethod? */ + luaG_callerror(L, s2v(func)); for (p = L->top.p; p > func; p--) /* open space for metamethod */ setobjs2s(L, p, p-1); L->top.p++; /* stack space pre-allocated by the caller */ setobj2s(L, func, tm); /* metamethod is the new function to be called */ - return func; + if ((status & MAX_CCMT) == MAX_CCMT) /* is counter full? */ + luaG_runerror(L, "'__call' chain too long"); + return status + (1u << CIST_CCMT); /* increment counter */ } @@ -564,11 +569,17 @@ void luaD_poscall (lua_State *L, CallInfo *ci, int nres) { #define next_ci(L) (L->ci->next ? L->ci->next : luaE_extendCI(L)) +/* +** Allocate and initialize CallInfo structure. At this point, the +** only valid fields in the call status are number of results, +** CIST_C (if it's a C function), and number of extra arguments. +** (All these bit-fields fit in 16-bit values.) +*/ l_sinline CallInfo *prepCallInfo (lua_State *L, StkId func, unsigned status, StkId top) { CallInfo *ci = L->ci = next_ci(L); /* new frame */ ci->func.p = func; - lua_assert((status & ~(CIST_NRESULTS | CIST_C)) == 0); + lua_assert((status & ~(CIST_NRESULTS | CIST_C | MAX_CCMT)) == 0); ci->callstatus = status; ci->top.p = top; return ci; @@ -607,12 +618,13 @@ l_sinline int precallC (lua_State *L, StkId func, unsigned status, */ int luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, int narg1, int delta) { + unsigned status = LUA_MULTRET + 1; retry: switch (ttypetag(s2v(func))) { case LUA_VCCL: /* C closure */ - return precallC(L, func, LUA_MULTRET + 1, clCvalue(s2v(func))->f); + return precallC(L, func, status, clCvalue(s2v(func))->f); case LUA_VLCF: /* light C function */ - return precallC(L, func, LUA_MULTRET + 1, fvalue(s2v(func))); + return precallC(L, func, status, fvalue(s2v(func))); case LUA_VLCL: { /* Lua function */ Proto *p = clLvalue(s2v(func))->p; int fsize = p->maxstacksize; /* frame size */ @@ -633,8 +645,8 @@ int luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, return -1; } default: { /* not a function */ - func = tryfuncTM(L, func); /* try to get '__call' metamethod */ - /* return luaD_pretailcall(L, ci, func, narg1 + 1, delta); */ + checkstackp(L, 1, func); /* space for metamethod */ + status = tryfuncTM(L, func, status); /* try '__call' metamethod */ narg1++; goto retry; /* try again */ } @@ -676,7 +688,8 @@ CallInfo *luaD_precall (lua_State *L, StkId func, int nresults) { return ci; } default: { /* not a function */ - func = tryfuncTM(L, func); /* try to get '__call' metamethod */ + checkstackp(L, 1, func); /* space for metamethod */ + status = tryfuncTM(L, func, status); /* try '__call' metamethod */ goto retry; /* try again with metamethod */ } } diff --git a/lstate.h b/lstate.h index ab5672130f..1c81b6edc9 100644 --- a/lstate.h +++ b/lstate.h @@ -221,16 +221,24 @@ struct CallInfo { */ /* bits 0-7 are the expected number of results from this function + 1 */ #define CIST_NRESULTS 0xffu -/* Bits 8-10 are used for CIST_RECST (see below) */ -#define CIST_RECST 8 /* the offset, not the mask */ -/* original value of 'allowhook' */ -#define CIST_OAH (cast(l_uint32, 1) << 11) -/* call is running a C function */ -#define CIST_C (CIST_OAH << 1) + +/* bits 8-11 count call metamethods (and their extra arguments) */ +#define CIST_CCMT 8 /* the offset, not the mask */ +#define MAX_CCMT (0xfu << CIST_CCMT) + +/* Bits 12-14 are used for CIST_RECST (see below) */ +#define CIST_RECST 12 /* the offset, not the mask */ + +/* call is running a C function (still in first 16 bits) */ +#define CIST_C (1u << (CIST_RECST + 3)) /* call is on a fresh "luaV_execute" frame */ -#define CIST_FRESH (CIST_C << 1) +#define CIST_FRESH cast(l_uint32, CIST_C << 1) +/* function is closing tbc variables */ +#define CIST_CLSRET (CIST_FRESH << 1) +/* original value of 'allowhook' */ +#define CIST_OAH (CIST_CLSRET << 1) /* call is running a debug hook */ -#define CIST_HOOKED (CIST_FRESH << 1) +#define CIST_HOOKED (CIST_OAH << 1) /* doing a yieldable protected call */ #define CIST_YPCALL (CIST_HOOKED << 1) /* call was tail called */ @@ -239,11 +247,9 @@ struct CallInfo { #define CIST_HOOKYIELD (CIST_TAIL << 1) /* function "called" a finalizer */ #define CIST_FIN (CIST_HOOKYIELD << 1) - /* function is closing tbc variables */ -#define CIST_CLSRET (CIST_FIN << 1) #if defined(LUA_COMPAT_LT_LE) /* using __lt for __le */ -#define CIST_LEQ (CIST_CLSRET << 1) +#define CIST_LEQ (CIST_FIN << 1) #endif diff --git a/manual/manual.of b/manual/manual.of index f0b17b4c1d..ce42ff5168 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -9392,6 +9392,10 @@ If you need to change it, declare a local variable with the same name in the loop body. } +@item{ +A chain of @id{__call} metamethods can have at most 15 objects. +} + } } diff --git a/testes/calls.lua b/testes/calls.lua index 409a275d5e..12312d60fb 100644 --- a/testes/calls.lua +++ b/testes/calls.lua @@ -178,7 +178,7 @@ do -- tail calls x chain of __call end -- build a chain of __call metamethods ending in function 'foo' - for i = 1, 100 do + for i = 1, 15 do foo = setmetatable({}, {__call = foo}) end @@ -190,8 +190,8 @@ end print('+') -do -- testing chains of '__call' - local N = 20 +do print"testing chains of '__call'" + local N = 15 local u = table.pack for i = 1, N do u = setmetatable({i}, {__call = u}) @@ -207,6 +207,23 @@ do -- testing chains of '__call' end +do -- testing chains too long + local a = {} + for i = 1, 16 do -- one too many + a = setmetatable({}, {__call = a}) + end + local status, msg = pcall(a) + assert(not status and string.find(msg, "too long")) + + setmetatable(a, {__call = a}) -- infinite chain + local status, msg = pcall(a) + assert(not status and string.find(msg, "too long")) + + -- again, with a tail call + local status, msg = pcall(function () return a() end) + assert(not status and string.find(msg, "too long")) +end + a = nil (function (x) a=x end)(23) assert(a == 23 and (function (x) return x*2 end)(20) == 40) From 50c7c915ee2fa239043d5456237f5145d064089b Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 19 Nov 2024 14:09:18 -0300 Subject: [PATCH 0972/1145] Debug information about extra arguments from __call 'debug.getinfo' can return number of extra arguments added to a call by a chain of __call metavalues. That information is being used to improve error messages about errors in these extra arguments. --- lauxlib.c | 24 ++++++++++++++++-------- ldblib.c | 4 +++- ldebug.c | 10 +++++++++- ltests.c | 4 ++++ lua.h | 1 + manual/manual.of | 13 +++++++++++-- testes/calls.lua | 11 +++++++++++ testes/db.lua | 3 +++ testes/errors.lua | 25 +++++++++++++++++++++++++ 9 files changed, 83 insertions(+), 12 deletions(-) diff --git a/lauxlib.c b/lauxlib.c index e4b125878d..d37d2f8c3f 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -170,19 +170,27 @@ LUALIB_API void luaL_traceback (lua_State *L, lua_State *L1, LUALIB_API int luaL_argerror (lua_State *L, int arg, const char *extramsg) { lua_Debug ar; + const char *argword; if (!lua_getstack(L, 0, &ar)) /* no stack frame? */ return luaL_error(L, "bad argument #%d (%s)", arg, extramsg); - lua_getinfo(L, "n", &ar); - if (strcmp(ar.namewhat, "method") == 0) { - arg--; /* do not count 'self' */ - if (arg == 0) /* error is in the self argument itself? */ - return luaL_error(L, "calling '%s' on bad self (%s)", - ar.name, extramsg); + lua_getinfo(L, "nt", &ar); + if (arg <= ar.extraargs) /* error in an extra argument? */ + argword = "extra argument"; + else { + arg -= ar.extraargs; /* do not count extra arguments */ + if (strcmp(ar.namewhat, "method") == 0) { /* colon syntax? */ + arg--; /* do not count (extra) self argument */ + if (arg == 0) /* error in self argument? */ + return luaL_error(L, "calling '%s' on bad self (%s)", + ar.name, extramsg); + /* else go through; error in a regular argument */ + } + argword = "argument"; } if (ar.name == NULL) ar.name = (pushglobalfuncname(L, &ar)) ? lua_tostring(L, -1) : "?"; - return luaL_error(L, "bad argument #%d to '%s' (%s)", - arg, ar.name, extramsg); + return luaL_error(L, "bad %s #%d to '%s' (%s)", + argword, arg, ar.name, extramsg); } diff --git a/ldblib.c b/ldblib.c index a0a06dd7f6..c7b74812e8 100644 --- a/ldblib.c +++ b/ldblib.c @@ -191,8 +191,10 @@ static int db_getinfo (lua_State *L) { settabsi(L, "ftransfer", ar.ftransfer); settabsi(L, "ntransfer", ar.ntransfer); } - if (strchr(options, 't')) + if (strchr(options, 't')) { settabsb(L, "istailcall", ar.istailcall); + settabsi(L, "extraargs", ar.extraargs); + } if (strchr(options, 'L')) treatstackoption(L, L1, "activelines"); if (strchr(options, 'f')) diff --git a/ldebug.c b/ldebug.c index d1b47c565d..ee3ac17fb5 100644 --- a/ldebug.c +++ b/ldebug.c @@ -352,7 +352,15 @@ static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar, break; } case 't': { - ar->istailcall = (ci != NULL && (ci->callstatus & CIST_TAIL)); + if (ci != NULL) { + ar->istailcall = !!(ci->callstatus & CIST_TAIL); + ar->extraargs = + cast_uchar((ci->callstatus & MAX_CCMT) >> CIST_CCMT); + } + else { + ar->istailcall = 0; + ar->extraargs = 0; + } break; } case 'n': { diff --git a/ltests.c b/ltests.c index 8191f14a5b..3edf805e1a 100644 --- a/ltests.c +++ b/ltests.c @@ -1900,6 +1900,10 @@ static struct X { int x; } x; else if EQ("closeslot") { lua_closeslot(L1, getnum); } + else if EQ("argerror") { + int arg = getnum; + luaL_argerror(L1, arg, getstring); + } else luaL_error(L, "unknown instruction %s", buff); } return 0; diff --git a/lua.h b/lua.h index 5fbc9d34ea..aefa3b8c3d 100644 --- a/lua.h +++ b/lua.h @@ -504,6 +504,7 @@ struct lua_Debug { unsigned char nups; /* (u) number of upvalues */ unsigned char nparams;/* (u) number of parameters */ char isvararg; /* (u) */ + unsigned char extraargs; /* (t) number of extra arguments */ char istailcall; /* (t) */ int ftransfer; /* (r) index of first value transferred */ int ntransfer; /* (r) number of transferred values */ diff --git a/manual/manual.of b/manual/manual.of index ce42ff5168..a441cea13a 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -4850,6 +4850,7 @@ typedef struct lua_Debug { unsigned char nups; /* (u) number of upvalues */ unsigned char nparams; /* (u) number of parameters */ char isvararg; /* (u) */ + unsigned char extraargs; /* (t) number of extra arguments */ char istailcall; /* (t) */ int ftransfer; /* (r) index of first value transferred */ int ntransfer; /* (r) number of transferred values */ @@ -4938,6 +4939,14 @@ true if this function invocation was called by a tail call. In this case, the caller of this level is not in the stack. } +@item{@id{extraargs}| +The number of extra arguments added by the call +to functions called through @idx{__call} metamethods. +(Each @idx{__call} metavalue adds a single extra argument, +the object being called, +but there may be a chain of @idx{__call} metavalues.) +} + @item{@id{nups}| the number of upvalues of the function. } @@ -5045,7 +5054,7 @@ fills in the fields @id{source}, @id{short_src}, @id{linedefined}, @id{lastlinedefined}, and @id{what}; } -@item{@Char{t}| fills in the field @id{istailcall}; +@item{@Char{t}| fills in the fields @id{istailcall} and @id{extraargs}; } @item{@Char{u}| fills in the fields @@ -7993,7 +8002,7 @@ returns @fail plus the position of the first invalid byte. @LibEntry{utf8.offset (s, n [, i])| -Returns the the position of the @id{n}-th character of @id{s} +Returns the position of the @id{n}-th character of @id{s} (counting from byte position @id{i}) as two integers: The index (in bytes) where its encoding starts and the index (in bytes) where it ends. diff --git a/testes/calls.lua b/testes/calls.lua index 12312d60fb..310282157d 100644 --- a/testes/calls.lua +++ b/testes/calls.lua @@ -204,6 +204,17 @@ do print"testing chains of '__call'" assert(Res[i][1] == i) end assert(Res[N + 1] == "a" and Res[N + 2] == "b" and Res[N + 3] == "c") + + local function u (...) + local n = debug.getinfo(1, 't').extraargs + assert(select("#", ...) == n) + return n + end + + for i = 0, N do + assert(u() == i) + u = setmetatable({}, {__call = u}) + end end diff --git a/testes/db.lua b/testes/db.lua index 49ff8e3e89..fc0db9eae0 100644 --- a/testes/db.lua +++ b/testes/db.lua @@ -624,6 +624,9 @@ local function f (x) end end +assert(debug.getinfo(print, 't').istailcall == false) +assert(debug.getinfo(print, 't').extraargs == 0) + function g(x) return f(x) end function g1(x) g(x) end diff --git a/testes/errors.lua b/testes/errors.lua index 80d91a9213..0925fe582a 100644 --- a/testes/errors.lua +++ b/testes/errors.lua @@ -117,6 +117,31 @@ else return 1 ]] assert(string.find(res, "xuxu.-main chunk")) + + do -- tests for error messages about extra arguments from __call + local function createobj (n) + -- function that raises an error on its n-th argument + local code = string.format("argerror %d 'msg'", n) + local func = T.makeCfunc(code) + -- create a chain of 2 __call objects + local M = setmetatable({}, {__call = func}) + M = setmetatable({}, {__call = M}) + -- put it as a method for a new object + return {foo = M} + end + + _G.a = createobj(1) -- error in first (extra) argument + checkmessage("a:foo()", "bad extra argument #1") + + _G.a = createobj(2) -- error in second (extra) argument + checkmessage("a:foo()", "bad extra argument #2") + + _G.a = createobj(3) -- error in self (after two extra arguments) + checkmessage("a:foo()", "bad self") + + _G.a = createobj(4) -- error in first regular argument (after self) + checkmessage("a:foo()", "bad argument #1") + end end From 682efe2678589eebc7c982e0ed66ad4990711e5a Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 25 Nov 2024 15:47:08 -0300 Subject: [PATCH 0973/1145] Change to macro 'LUAI_TRY' The call to 'f' is done by the macro, to give it more flexibility. --- ldo.c | 14 ++++++-------- testes/files.lua | 1 + 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/ldo.c b/ldo.c index cb7e5aef4e..9459391f9f 100644 --- a/ldo.c +++ b/ldo.c @@ -69,22 +69,22 @@ /* C++ exceptions */ #define LUAI_THROW(L,c) throw(c) -#define LUAI_TRY(L,c,a) \ - try { a } catch(...) { if ((c)->status == 0) (c)->status = -1; } -#define luai_jmpbuf int /* dummy variable */ +#define LUAI_TRY(L,c,f,ud) \ + try { (f)(L, ud); } catch(...) { if ((c)->status == 0) (c)->status = -1; } +#define luai_jmpbuf int /* dummy field */ #elif defined(LUA_USE_POSIX) /* }{ */ /* in POSIX, try _longjmp/_setjmp (more efficient) */ #define LUAI_THROW(L,c) _longjmp((c)->b, 1) -#define LUAI_TRY(L,c,a) if (_setjmp((c)->b) == 0) { a } +#define LUAI_TRY(L,c,f,ud) if (_setjmp((c)->b) == 0) ((f)(L, ud)) #define luai_jmpbuf jmp_buf #else /* }{ */ /* ISO C handling with long jumps */ #define LUAI_THROW(L,c) longjmp((c)->b, 1) -#define LUAI_TRY(L,c,a) if (setjmp((c)->b) == 0) { a } +#define LUAI_TRY(L,c,f,ud) if (setjmp((c)->b) == 0) ((f)(L, ud)) #define luai_jmpbuf jmp_buf #endif /* } */ @@ -154,9 +154,7 @@ int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) { lj.status = LUA_OK; lj.previous = L->errorJmp; /* chain new error handler */ L->errorJmp = &lj; - LUAI_TRY(L, &lj, - (*f)(L, ud); - ); + LUAI_TRY(L, &lj, f, ud); /* call 'f' catching errors */ L->errorJmp = lj.previous; /* restore old error handler */ L->nCcalls = oldnCcalls; return lj.status; diff --git a/testes/files.lua b/testes/files.lua index 4f925f5089..9bdf04d09a 100644 --- a/testes/files.lua +++ b/testes/files.lua @@ -766,6 +766,7 @@ if not _port then assert((v[3] == nil and z > 0) or v[3] == z) end end + print("(done)") end From 9329eeac3b7035c223d7a8b63dbde1f6db371bf5 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 27 Nov 2024 18:37:29 -0300 Subject: [PATCH 0974/1145] Avoid an extra call to 'concretesize' in 'resizearray' --- ltable.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/ltable.c b/ltable.c index 21fafa5e76..71a6f30d59 100644 --- a/ltable.c +++ b/ltable.c @@ -632,14 +632,14 @@ static Value *resizearray (lua_State *L , Table *t, if (np == NULL) /* allocation error? */ return NULL; if (oldasize > 0) { + size_t oldasizeb = concretesize(oldasize); /* move common elements to new position */ Value *op = t->array - oldasize; /* real original array */ unsigned tomove = (oldasize < newasize) ? oldasize : newasize; - lua_assert(tomove > 0); - memcpy(np + newasize - tomove, - op + oldasize - tomove, - concretesize(tomove)); - luaM_freemem(L, op, concretesize(oldasize)); + size_t tomoveb = (oldasize < newasize) ? oldasizeb : newasizeb; + lua_assert(tomoveb > 0); + memcpy(np + newasize - tomove, op + oldasize - tomove, tomoveb); + luaM_freemem(L, op, oldasizeb); } return np + newasize; /* shift pointer to the end of value segment */ } From 002beeebe79065e03dd9f531bee367e8459e3f64 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 29 Nov 2024 17:26:20 -0300 Subject: [PATCH 0975/1145] New way to keep hints for table length Instead of using 'alimit' for keeping the size of the array and at the same time being a hint for '#t', a table now keeps these two values separate. The Table structure has a field 'asize' with the size of the array, while the length hint is kept in the array itself. That way, tables with no array part waste no space with that field. Moreover, the space for the hint may have zero cost for small arrays, if the array of tags plus the hint still fits in a single word. --- lgc.c | 8 +- lobject.h | 16 +-- ltable.c | 298 +++++++++++++++------------------------------ ltable.h | 36 +++--- ltests.c | 6 +- lvm.c | 2 +- testes/nextvar.lua | 29 +++-- 7 files changed, 146 insertions(+), 249 deletions(-) diff --git a/lgc.c b/lgc.c index e4130bd560..3cdfd0064c 100644 --- a/lgc.c +++ b/lgc.c @@ -486,7 +486,7 @@ static void traverseweakvalue (global_State *g, Table *h) { Node *n, *limit = gnodelast(h); /* if there is array part, assume it may have white values (it is not worth traversing it now just to check) */ - int hasclears = (h->alimit > 0); + int hasclears = (h->asize > 0); for (n = gnode(h, 0); n < limit; n++) { /* traverse hash part */ if (isempty(gval(n))) /* entry is empty? */ clearkey(n); /* clear its key */ @@ -508,7 +508,7 @@ static void traverseweakvalue (global_State *g, Table *h) { ** Traverse the array part of a table. */ static int traversearray (global_State *g, Table *h) { - unsigned asize = luaH_realasize(h); + unsigned asize = h->asize; int marked = 0; /* true if some object is marked in this traversal */ unsigned i; for (i = 0; i < asize; i++) { @@ -604,7 +604,7 @@ static l_mem traversetable (global_State *g, Table *h) { } else /* not weak */ traversestrongtable(g, h); - return 1 + 2*sizenode(h) + h->alimit; + return 1 + 2*sizenode(h) + h->asize; } @@ -790,7 +790,7 @@ static void clearbyvalues (global_State *g, GCObject *l, GCObject *f) { Table *h = gco2t(l); Node *n, *limit = gnodelast(h); unsigned int i; - unsigned int asize = luaH_realasize(h); + unsigned int asize = h->asize; for (i = 0; i < asize; i++) { GCObject *o = gcvalarr(h, i); if (iscleared(g, o)) /* value was collected? */ diff --git a/lobject.h b/lobject.h index b1407b7791..4a0835a8e7 100644 --- a/lobject.h +++ b/lobject.h @@ -763,24 +763,12 @@ typedef union Node { checkliveness(L,io_); } -/* -** About 'alimit': if 'isrealasize(t)' is true, then 'alimit' is the -** real size of 'array'. Otherwise, the real size of 'array' is the -** smallest power of two not smaller than 'alimit' (or zero iff 'alimit' -** is zero); 'alimit' is then used as a hint for #t. -*/ - -#define BITRAS (1 << 7) -#define isrealasize(t) (!((t)->flags & BITRAS)) -#define setrealasize(t) ((t)->flags &= cast_byte(~BITRAS)) -#define setnorealasize(t) ((t)->flags |= BITRAS) - typedef struct Table { CommonHeader; lu_byte flags; /* 1<

lsizenode > LIMFORLAST) +#define haslastfree(t) ((t)->lsizenode >= LIMFORLAST) #define getlastfree(t) ((cast(Limbox *, (t)->node) - 1)->lastfree) @@ -273,61 +273,6 @@ static int equalkey (const TValue *k1, const Node *n2, int deadok) { } -/* -** True if value of 'alimit' is equal to the real size of the array -** part of table 't'. (Otherwise, the array part must be larger than -** 'alimit'.) -*/ -#define limitequalsasize(t) (isrealasize(t) || ispow2((t)->alimit)) - - -/* -** Returns the real size of the 'array' array -*/ -unsigned int luaH_realasize (const Table *t) { - if (limitequalsasize(t)) - return t->alimit; /* this is the size */ - else { - unsigned int size = t->alimit; - /* compute the smallest power of 2 not smaller than 'size' */ - size |= (size >> 1); - size |= (size >> 2); - size |= (size >> 4); - size |= (size >> 8); -#if (UINT_MAX >> 14) > 3 /* unsigned int has more than 16 bits */ - size |= (size >> 16); -#if (UINT_MAX >> 30) > 3 - size |= (size >> 32); /* unsigned int has more than 32 bits */ -#endif -#endif - size++; - lua_assert(ispow2(size) && size/2 < t->alimit && t->alimit < size); - return size; - } -} - - -/* -** Check whether real size of the array is a power of 2. -** (If it is not, 'alimit' cannot be changed to any other value -** without changing the real size.) -*/ -static int ispow2realasize (const Table *t) { - return (!isrealasize(t) || ispow2(t->alimit)); -} - - -static unsigned int setlimittosize (Table *t) { - t->alimit = luaH_realasize(t); - setrealasize(t); - return t->alimit; -} - - -#define limitasasize(t) check_exp(isrealasize(t), t->alimit) - - - /* ** "Generic" get version. (Not that generic: not valid for integers, ** which may be in array part, nor for floats with integral values.) @@ -384,7 +329,7 @@ static unsigned findindex (lua_State *L, Table *t, TValue *key, int luaH_next (lua_State *L, Table *t, StkId key) { - unsigned int asize = luaH_realasize(t); + unsigned int asize = t->asize; unsigned int i = findindex(L, t, s2v(key), asize); /* find original key */ for (; i < asize; i++) { /* try first array part */ lu_byte tag = *getArrTag(t, i); @@ -425,38 +370,10 @@ static void freehash (lua_State *L, Table *t) { /* -** Check whether an integer key is in the array part. If 'alimit' is -** not the real size of the array, the key still can be in the array -** part. In this case, do the "Xmilia trick" to check whether 'key-1' -** is smaller than the real size. -** The trick works as follow: let 'p' be the integer such that -** '2^(p+1) >= alimit > 2^p', or '2^(p+1) > alimit-1 >= 2^p'. That is, -** 'p' is the highest 1-bit in 'alimit-1', and 2^(p+1) is the real size -** of the array. What we have to check becomes 'key-1 < 2^(p+1)'. We -** compute '(key-1) & ~(alimit-1)', which we call 'res'; it will have -** the 'p' bit cleared. (It may also clear other bits smaller than 'p', -** but no bit higher than 'p'.) If the key is outside the array, that -** is, 'key-1 >= 2^(p+1)', then 'res' will have some 1-bit higher than -** 'p', therefore it will be larger or equal to 'alimit', and the check -** will fail. If 'key-1 < 2^(p+1)', then 'res' has no 1-bit higher than -** 'p', and as the bit 'p' itself was cleared, 'res' will be smaller -** than 2^p, therefore smaller than 'alimit', and the check succeeds. -** As special cases, when 'alimit' is 0 the condition is trivially false, -** and when 'alimit' is 1 the condition simplifies to 'key-1 < alimit'. -** If key is 0 or negative, 'res' will have its higher bit on, so that -** it cannot be smaller than 'alimit'. +** Check whether an integer key is in the array part of a table. */ -static int keyinarray (Table *t, lua_Integer key) { - lua_Unsigned alimit = t->alimit; - if (l_castS2U(key) - 1u < alimit) /* 'key' in [1, t->alimit]? */ - return 1; - else if (!isrealasize(t) && /* key still may be in the array part? */ - (((l_castS2U(key) - 1u) & ~(alimit - 1u)) < alimit)) { - t->alimit = cast_uint(key); /* probably '#t' is here now */ - return 1; - } - else - return 0; +l_sinline int keyinarray (Table *t, lua_Integer key) { + return (l_castS2U(key) - 1u < t->asize); /* 'key' in [1, t->asize]? */ } @@ -466,7 +383,6 @@ static int keyinarray (Table *t, lua_Integer key) { ** ============================================================== */ - /* ** Structure to count the keys in a table. ** 'total' is the total number of keys in the table. @@ -534,7 +450,7 @@ static void countint (lua_Integer key, Counters *ct) { } -l_sinline int arraykeyisempty (const Table *t, lua_Unsigned key) { +l_sinline int arraykeyisempty (const Table *t, unsigned key) { int tag = *getArrTag(t, key - 1); return tagisempty(tag); } @@ -548,7 +464,7 @@ static void numusearray (const Table *t, Counters *ct) { unsigned int ttlg; /* 2^lg */ unsigned int ause = 0; /* summation of 'nums' */ unsigned int i = 1; /* index to traverse all array keys */ - unsigned int asize = limitasasize(t); /* real array size */ + unsigned int asize = t->asize; /* traverse each slice */ for (lg = 0, ttlg = 1; lg <= MAXABITS; lg++, ttlg *= 2) { unsigned int lc = 0; /* counter */ @@ -600,7 +516,10 @@ static void numusehash (const Table *t, Counters *ct) { ** "concrete size" (number of bytes in the array). */ static size_t concretesize (unsigned int size) { - return size * sizeof(Value) + size; /* space for the two arrays */ + if (size == 0) + return 0; + else /* space for the two arrays plus an unsigned in between */ + return size * (sizeof(Value) + 1) + sizeof(unsigned); } @@ -631,17 +550,18 @@ static Value *resizearray (lua_State *L , Table *t, luaM_reallocvector(L, NULL, 0, newasizeb, lu_byte)); if (np == NULL) /* allocation error? */ return NULL; + np += newasize; /* shift pointer to the end of value segment */ if (oldasize > 0) { - size_t oldasizeb = concretesize(oldasize); /* move common elements to new position */ - Value *op = t->array - oldasize; /* real original array */ + size_t oldasizeb = concretesize(oldasize); + Value *op = t->array; /* original array */ unsigned tomove = (oldasize < newasize) ? oldasize : newasize; size_t tomoveb = (oldasize < newasize) ? oldasizeb : newasizeb; lua_assert(tomoveb > 0); - memcpy(np + newasize - tomove, op + oldasize - tomove, tomoveb); - luaM_freemem(L, op, oldasizeb); + memcpy(np - tomove, op - tomove, tomoveb); + luaM_freemem(L, op - oldasize, oldasizeb); /* free old block */ } - return np + newasize; /* shift pointer to the end of value segment */ + return np; } } @@ -665,7 +585,7 @@ static void setnodevector (lua_State *L, Table *t, unsigned size) { if (lsize > MAXHBITS || (1u << lsize) > MAXHSIZE) luaG_runerror(L, "table overflow"); size = twoto(lsize); - if (lsize <= LIMFORLAST) /* no 'lastfree' field? */ + if (lsize < LIMFORLAST) /* no 'lastfree' field? */ t->node = luaM_newvector(L, size, Node); else { size_t bsize = size * sizeof(Node) + sizeof(Limbox); @@ -730,7 +650,7 @@ static void exchangehashpart (Table *t1, Table *t2) { static void reinsertOldSlice (lua_State *L, Table *t, unsigned oldasize, unsigned newasize) { unsigned i; - t->alimit = newasize; /* pretend array has new size... */ + t->asize = newasize; /* pretend array has new size... */ for (i = newasize; i < oldasize; i++) { /* traverse vanishing slice */ lu_byte tag = *getArrTag(t, i); if (!tagisempty(tag)) { /* a non-empty entry? */ @@ -740,7 +660,7 @@ static void reinsertOldSlice (lua_State *L, Table *t, unsigned oldasize, luaH_setint(L, t, cast_int(i) + 1, &aux); } } - t->alimit = oldasize; /* restore current size... */ + t->asize = oldasize; /* restore current size... */ } @@ -772,7 +692,7 @@ static void clearNewSlice (Table *t, unsigned oldasize, unsigned newasize) { void luaH_resize (lua_State *L, Table *t, unsigned newasize, unsigned nhsize) { Table newt; /* to keep the new hash part */ - unsigned int oldasize = setlimittosize(t); + unsigned oldasize = t->asize; Value *newarray; if (newasize > MAXASIZE) luaG_runerror(L, "table overflow"); @@ -794,7 +714,9 @@ void luaH_resize (lua_State *L, Table *t, unsigned newasize, /* allocation ok; initialize new part of the array */ exchangehashpart(t, &newt); /* 't' has the new hash ('newt' has the old) */ t->array = newarray; /* set new array part */ - t->alimit = newasize; + t->asize = newasize; + if (newarray != NULL) + *lenhint(t) = newasize / 2u; /* set an initial hint */ clearNewSlice(t, oldasize, newasize); /* re-insert elements from old hash part into new parts */ reinsert(L, &newt, t); /* 'newt' now has the old hash */ @@ -818,7 +740,6 @@ static void rehash (lua_State *L, Table *t, const TValue *ek) { Counters ct; unsigned i; unsigned nsize; /* size for the hash part */ - setlimittosize(t); /* reset counts */ for (i = 0; i <= MAXABITS; i++) ct.nums[i] = 0; ct.na = 0; @@ -829,7 +750,7 @@ static void rehash (lua_State *L, Table *t, const TValue *ek) { numusehash(t, &ct); /* count keys in hash part */ if (ct.na == 0) { /* no new keys to enter array part; keep it with the same size */ - asize = luaH_realasize(t); + asize = t->asize; } else { /* compute best size for array part */ numusearray(t, &ct); /* count keys in array part */ @@ -857,15 +778,14 @@ Table *luaH_new (lua_State *L) { t->metatable = NULL; t->flags = maskflags; /* table has no metamethod fields */ t->array = NULL; - t->alimit = 0; + t->asize = 0; setnodevector(L, t, 0); return t; } lu_mem luaH_size (Table *t) { - lu_mem sz = cast(lu_mem, sizeof(Table)) - + luaH_realasize(t) * (sizeof(Value) + 1); + lu_mem sz = cast(lu_mem, sizeof(Table)) + concretesize(t->asize); if (!isdummy(t)) sz += sizehash(t); return sz; @@ -876,9 +796,8 @@ lu_mem luaH_size (Table *t) { ** Frees a table. */ void luaH_free (lua_State *L, Table *t) { - unsigned int realsize = luaH_realasize(t); freehash(L, t); - resizearray(L, t, realsize, 0); + resizearray(L, t, t->asize, 0); luaM_free(L, t); } @@ -972,7 +891,7 @@ static void luaH_newkey (lua_State *L, Table *t, const TValue *key, static const TValue *getintfromhash (Table *t, lua_Integer key) { Node *n = hashint(t, key); - lua_assert(l_castS2U(key) - 1u >= luaH_realasize(t)); + lua_assert(!keyinarray(t, key)); for (;;) { /* check whether 'key' is somewhere in the chain */ if (keyisinteger(n) && keyival(n) == key) return gval(n); /* that's it */ @@ -1112,17 +1031,15 @@ static int rawfinishnodeset (const TValue *slot, TValue *val) { int luaH_psetint (Table *t, lua_Integer key, TValue *val) { - if (keyinarray(t, key)) { - lu_byte *tag = getArrTag(t, key - 1); - if (!tagisempty(*tag) || checknoTM(t->metatable, TM_NEWINDEX)) { - fval2arr(t, cast_uint(key) - 1, tag, val); - return HOK; /* success */ - } - else - return ~cast_int(key - 1); /* empty slot in the array part */ - } - else - return finishnodeset(t, getintfromhash(t, key), val); + lua_assert(!keyinarray(t, key)); + return finishnodeset(t, getintfromhash(t, key), val); +} + + +static int psetint (Table *t, lua_Integer key, TValue *val) { + int hres; + luaH_fastseti(t, key, val, hres); + return hres; } @@ -1139,12 +1056,12 @@ int luaH_psetstr (Table *t, TString *key, TValue *val) { int luaH_pset (Table *t, const TValue *key, TValue *val) { switch (ttypetag(key)) { case LUA_VSHRSTR: return luaH_psetshortstr(t, tsvalue(key), val); - case LUA_VNUMINT: return luaH_psetint(t, ivalue(key), val); + case LUA_VNUMINT: return psetint(t, ivalue(key), val); case LUA_VNIL: return HNOTFOUND; case LUA_VNUMFLT: { lua_Integer k; if (luaV_flttointeger(fltvalue(key), &k, F2Ieq)) /* integral index? */ - return luaH_psetint(t, k, val); /* use specialized version */ + return psetint(t, k, val); /* use specialized version */ /* else... */ } /* FALLTHROUGH */ default: @@ -1244,6 +1161,7 @@ static lua_Unsigned hash_search (Table *t, lua_Unsigned j) { static unsigned int binsearch (Table *array, unsigned int i, unsigned int j) { + lua_assert(i <= j); while (j - i > 1u) { /* binary search */ unsigned int m = (i + j) / 2; if (arraykeyisempty(array, m)) j = m; @@ -1253,90 +1171,74 @@ static unsigned int binsearch (Table *array, unsigned int i, unsigned int j) { } +/* return a border, saving it as a hint for next call */ +static lua_Unsigned newhint (Table *t, unsigned hint) { + lua_assert(hint <= t->asize); + *lenhint(t) = hint; + return hint; +} + + /* -** Try to find a boundary in table 't'. (A 'boundary' is an integer index -** such that t[i] is present and t[i+1] is absent, or 0 if t[1] is absent -** and 'maxinteger' if t[maxinteger] is present.) -** (In the next explanation, we use Lua indices, that is, with base 1. -** The code itself uses base 0 when indexing the array part of the table.) -** The code starts with 'limit = t->alimit', a position in the array -** part that may be a boundary. -** -** (1) If 't[limit]' is empty, there must be a boundary before it. -** As a common case (e.g., after 't[#t]=nil'), check whether 'limit-1' -** is present. If so, it is a boundary. Otherwise, do a binary search -** between 0 and limit to find a boundary. In both cases, try to -** use this boundary as the new 'alimit', as a hint for the next call. -** -** (2) If 't[limit]' is not empty and the array has more elements -** after 'limit', try to find a boundary there. Again, try first -** the special case (which should be quite frequent) where 'limit+1' -** is empty, so that 'limit' is a boundary. Otherwise, check the -** last element of the array part. If it is empty, there must be a -** boundary between the old limit (present) and the last element -** (absent), which is found with a binary search. (This boundary always -** can be a new limit.) -** -** (3) The last case is when there are no elements in the array part -** (limit == 0) or its last element (the new limit) is present. -** In this case, must check the hash part. If there is no hash part -** or 'limit+1' is absent, 'limit' is a boundary. Otherwise, call -** 'hash_search' to find a boundary in the hash part of the table. -** (In those cases, the boundary is not inside the array part, and -** therefore cannot be used as a new limit.) +** Try to find a border in table 't'. (A 'border' is an integer index +** such that t[i] is present and t[i+1] is absent, or 0 if t[1] is absent, +** or 'maxinteger' if t[maxinteger] is present.) +** If there is an array part, try to find a border there. First try +** to find it in the vicinity of the previous result (hint), to handle +** cases like 't[#t + 1] = val' or 't[#t] = nil', that move the border +** by one entry. Otherwise, do a binary search to find the border. +** If there is no array part, or its last element is non empty, the +** border may be in the hash part. */ lua_Unsigned luaH_getn (Table *t) { - unsigned int limit = t->alimit; - if (limit > 0 && arraykeyisempty(t, limit)) { /* (1)? */ - /* there must be a boundary before 'limit' */ - if (limit >= 2 && !arraykeyisempty(t, limit - 1)) { - /* 'limit - 1' is a boundary; can it be a new limit? */ - if (ispow2realasize(t) && !ispow2(limit - 1)) { - t->alimit = limit - 1; - setnorealasize(t); /* now 'alimit' is not the real size */ + unsigned asize = t->asize; + if (asize > 0) { /* is there an array part? */ + const unsigned maxvicinity = 4; + unsigned limit = *lenhint(t); /* start with the hint */ + if (limit == 0) + limit = 1; /* make limit a valid index in the array */ + if (arraykeyisempty(t, limit)) { /* t[limit] empty? */ + /* there must be a border before 'limit' */ + unsigned i; + /* look for a border in the vicinity of the hint */ + for (i = 0; i < maxvicinity && limit > 1; i++) { + limit--; + if (!arraykeyisempty(t, limit)) + return newhint(t, limit); /* 'limit' is a border */ } - return limit - 1; + /* t[limit] still empty; search for a border in [0, limit) */ + return newhint(t, binsearch(t, 0, limit)); } - else { /* must search for a boundary in [0, limit] */ - unsigned int boundary = binsearch(t, 0, limit); - /* can this boundary represent the real size of the array? */ - if (ispow2realasize(t) && boundary > luaH_realasize(t) / 2) { - t->alimit = boundary; /* use it as the new limit */ - setnorealasize(t); + else { /* 'limit' is present in table; look for a border after it */ + unsigned i; + /* look for a border in the vicinity of the hint */ + for (i = 0; i < maxvicinity && limit < asize; i++) { + limit++; + if (arraykeyisempty(t, limit)) + return newhint(t, limit - 1); /* 'limit - 1' is a border */ + } + if (arraykeyisempty(t, asize)) { /* last element empty? */ + /* t[limit] not empty; search for a border in [limit, asize) */ + return newhint(t, binsearch(t, limit, asize)); } - return boundary; - } - } - /* 'limit' is zero or present in table */ - if (!limitequalsasize(t)) { /* (2)? */ - /* 'limit' > 0 and array has more elements after 'limit' */ - if (arraykeyisempty(t, limit + 1)) /* 'limit + 1' is empty? */ - return limit; /* this is the boundary */ - /* else, try last element in the array */ - limit = luaH_realasize(t); - if (arraykeyisempty(t, limit)) { /* empty? */ - /* there must be a boundary in the array after old limit, - and it must be a valid new limit */ - unsigned int boundary = binsearch(t, t->alimit, limit); - t->alimit = boundary; - return boundary; } - /* else, new limit is present in the table; check the hash part */ + /* last element non empty; set a hint to speed up findind that again */ + /* (keys in the hash part cannot be hints) */ + *lenhint(t) = asize; } - /* (3) 'limit' is the last element and either is zero or present in table */ - lua_assert(limit == luaH_realasize(t) && - (limit == 0 || !arraykeyisempty(t, limit))); - if (isdummy(t) || hashkeyisempty(t, limit + 1)) - return limit; /* 'limit + 1' is absent */ - else /* 'limit + 1' is also present */ - return hash_search(t, limit); + /* no array part or t[asize] is not empty; check the hash part */ + lua_assert(asize == 0 || !arraykeyisempty(t, asize)); + if (isdummy(t) || hashkeyisempty(t, asize + 1)) + return asize; /* 'asize + 1' is empty */ + else /* 'asize + 1' is also non empty */ + return hash_search(t, asize); } #if defined(LUA_DEBUG) -/* export these functions for the test library */ +/* export this function for the test library */ Node *luaH_mainposition (const Table *t, const TValue *key) { return mainpositionTV(t, key); diff --git a/ltable.h b/ltable.h index 17e4fb4a10..9c4eb937bc 100644 --- a/ltable.h +++ b/ltable.h @@ -48,7 +48,7 @@ #define luaH_fastgeti(t,k,res,tag) \ { Table *h = t; lua_Unsigned u = l_castS2U(k) - 1u; \ - if ((u < h->alimit)) { \ + if ((u < h->asize)) { \ tag = *getArrTag(h, u); \ if (!tagisempty(tag)) { farr2val(h, u, tag, res); }} \ else { tag = luaH_getint(h, (k), res); }} @@ -56,10 +56,11 @@ #define luaH_fastseti(t,k,val,hres) \ { Table *h = t; lua_Unsigned u = l_castS2U(k) - 1u; \ - if ((u < h->alimit)) { \ + if ((u < h->asize)) { \ lu_byte *tag = getArrTag(h, u); \ - if (tagisempty(*tag)) hres = ~cast_int(u); \ - else { fval2arr(h, u, tag, val); hres = HOK; }} \ + if (h->metatable == NULL || !tagisempty(*tag)) \ + { fval2arr(h, u, tag, val); hres = HOK; } \ + else hres = ~cast_int(u); } \ else { hres = luaH_psetint(h, k, val); }} @@ -94,27 +95,35 @@ /* ** The array part of a table is represented by an inverted array of ** values followed by an array of tags, to avoid wasting space with -** padding. The 'array' pointer points to the junction of the two -** arrays, so that values are indexed with negative indices and tags -** with non-negative indices. +** padding. In between them there is an unsigned int, explained later. +** The 'array' pointer points between the two arrays, so that values are +** indexed with negative indices and tags with non-negative indices. - Values Tags - -------------------------------------------------------- - ... | Value 1 | Value 0 |0|1|... - -------------------------------------------------------- - ^ t->array + Values Tags + -------------------------------------------------------- + ... | Value 1 | Value 0 |unsigned|0|1|... + -------------------------------------------------------- + ^ t->array ** All accesses to 't->array' should be through the macros 'getArrTag' ** and 'getArrVal'. */ /* Computes the address of the tag for the abstract C-index 'k' */ -#define getArrTag(t,k) (cast(lu_byte*, (t)->array) + (k)) +#define getArrTag(t,k) (cast(lu_byte*, (t)->array) + sizeof(unsigned) + (k)) /* Computes the address of the value for the abstract C-index 'k' */ #define getArrVal(t,k) ((t)->array - 1 - (k)) +/* +** The unsigned between the two arrays is used as a hint for #t; +** see luaH_getn. It is stored there to avoid wasting space in +** the structure Table for tables with no array part. +*/ +#define lenhint(t) cast(unsigned*, (t)->array) + + /* ** Move TValues to/from arrays, using C indices */ @@ -167,7 +176,6 @@ LUAI_FUNC lu_mem luaH_size (Table *t); LUAI_FUNC void luaH_free (lua_State *L, Table *t); LUAI_FUNC int luaH_next (lua_State *L, Table *t, StkId key); LUAI_FUNC lua_Unsigned luaH_getn (Table *t); -LUAI_FUNC unsigned luaH_realasize (const Table *t); #if defined(LUA_DEBUG) diff --git a/ltests.c b/ltests.c index 3edf805e1a..44ed7bcc5d 100644 --- a/ltests.c +++ b/ltests.c @@ -359,7 +359,7 @@ static void checkvalref (global_State *g, GCObject *f, const TValue *t) { static void checktable (global_State *g, Table *h) { unsigned int i; - unsigned int asize = luaH_realasize(h); + unsigned int asize = h->asize; Node *n, *limit = gnode(h, sizenode(h)); GCObject *hgc = obj2gco(h); checkobjrefN(g, hgc, h->metatable); @@ -1034,11 +1034,11 @@ static int table_query (lua_State *L) { unsigned int asize; luaL_checktype(L, 1, LUA_TTABLE); t = hvalue(obj_at(L, 1)); - asize = luaH_realasize(t); + asize = t->asize; if (i == -1) { lua_pushinteger(L, cast(lua_Integer, asize)); lua_pushinteger(L, cast(lua_Integer, allocsizenode(t))); - lua_pushinteger(L, cast(lua_Integer, t->alimit)); + lua_pushinteger(L, cast(lua_Integer, asize > 0 ? *lenhint(t) : 0)); return 3; } else if (cast_uint(i) < asize) { diff --git a/lvm.c b/lvm.c index 33da560955..1c564a713d 100644 --- a/lvm.c +++ b/lvm.c @@ -1865,7 +1865,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { pc++; } /* when 'n' is known, table should have proper size */ - if (last > luaH_realasize(h)) { /* needs more space? */ + if (last > h->asize) { /* needs more space? */ /* fixed-size sets should have space preallocated */ lua_assert(GETARG_vB(i) == 0); luaH_resizearray(L, h, last); /* preallocate it at once */ diff --git a/testes/nextvar.lua b/testes/nextvar.lua index 00e509f87d..d1da3ceeb3 100644 --- a/testes/nextvar.lua +++ b/testes/nextvar.lua @@ -316,21 +316,6 @@ end local a = {} for i=1,lim do a[i] = true; foo(i, table.unpack(a)) end - --- Table length with limit smaller than maximum value at array -local a = {} -for i = 1,64 do a[i] = true end -- make its array size 64 -for i = 1,64 do a[i] = nil end -- erase all elements -assert(T.querytab(a) == 64) -- array part has 64 elements -a[32] = true; a[48] = true; -- binary search will find these ones -a[51] = true -- binary search will miss this one -assert(#a == 48) -- this will set the limit -assert(select(3, T.querytab(a)) == 48) -- this is the limit now -a[50] = true -- this will set a new limit -assert(select(3, T.querytab(a)) == 50) -- this is the limit now --- but the size is larger (and still inside the array part) -assert(#a == 51) - end --] @@ -344,6 +329,20 @@ assert(#{1, 2, 3, nil, nil} == 3) print'+' +do + local s1, s2 = math.randomseed() + print(string.format( + "testing length for some random tables (seeds 0X%x:%x)", s1, s2)) + local N = 130 + for i = 1, 1e3 do -- create that many random tables + local a = table.create(math.random(N)) -- initiate with random size + for j = 1, math.random(N) do -- add random number of random entries + a[math.random(N)] = true + end + assert(#a == 0 or a[#a] and not a[#a + 1]) + end +end + local nofind = {} a,b,c = 1,2,3 From 62afbc6bfce222ebe745863131f952f6fce5eafb Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 29 Nov 2024 17:39:20 -0300 Subject: [PATCH 0976/1145] Details Added two warnings to the makefile. --- makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/makefile b/makefile index 58de5ddb59..e3f8cf698c 100644 --- a/makefile +++ b/makefile @@ -15,12 +15,12 @@ CWARNSCPP= \ -Wdouble-promotion \ -Wmissing-declarations \ -Wconversion \ + -Wuninitialized \ + -Wstrict-overflow=2 \ # the next warnings might be useful sometimes, # but usually they generate too much noise # -Werror \ # -pedantic # warns if we use jump tables \ - # -Wsign-conversion \ - # -Wstrict-overflow=2 \ # -Wformat=2 \ # -Wcast-qual \ From 04e495403ba66e88abfb5cc4cf1887f094eea57f Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 2 Dec 2024 11:19:03 -0300 Subject: [PATCH 0977/1145] New function 'lua_printvalue' for internal debugging --- ltests.c | 40 ++++++++++++++++++++++++++++++++++++---- ltests.h | 7 +++++++ 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/ltests.c b/ltests.c index 44ed7bcc5d..6c77703c4f 100644 --- a/ltests.c +++ b/ltests.c @@ -325,6 +325,37 @@ void lua_printobj (lua_State *L, struct GCObject *o) { printobj(G(L), o); } + +void lua_printvalue (TValue *v) { + switch (ttype(v)) { + case LUA_TNUMBER: { + char buff[LUA_N2SBUFFSZ]; + unsigned len = luaO_tostringbuff(v, buff); + buff[len] = '\0'; + printf("%s", buff); + break; + } + case LUA_TSTRING: { + printf("'%s'", getstr(tsvalue(v))); + break; + } + case LUA_TBOOLEAN: { + printf("%s", (!l_isfalse(v) ? "true" : "false")); + break; + } + case LUA_TNIL: { + printf("nil"); + break; + } + default: { + void *p = iscollectable(v) ? gcvalue(v) : NULL; + printf("%s: %p", ttypename(ttype(v)), p); + break; + } + } +} + + static int testobjref (global_State *g, GCObject *f, GCObject *t) { int r1 = testobjref1(g, f, t); if (!r1) { @@ -827,8 +858,9 @@ void lua_printstack (lua_State *L) { int n = lua_gettop(L); printf("stack: >>\n"); for (i = 1; i <= n; i++) { - printf("%3d: %s\n", i, luaL_tolstring(L, i, NULL)); - lua_pop(L, 1); + printf("%3d: ", i); + lua_printvalue(s2v(L->ci->func.p + i)); + printf("\n"); } printf("<<\n"); } @@ -1681,8 +1713,8 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) { else if EQ("printstack") { int n = getnum; if (n != 0) { - printf("%s\n", luaL_tolstring(L1, n, NULL)); - lua_pop(L1, 1); + lua_printvalue(s2v(L->ci->func.p + n)); + printf("\n"); } else lua_printstack(L1); } diff --git a/ltests.h b/ltests.h index 906fae335d..543b0d553a 100644 --- a/ltests.h +++ b/ltests.h @@ -79,6 +79,13 @@ LUAI_FUNC int lua_checkmemory (lua_State *L); struct GCObject; LUAI_FUNC void lua_printobj (lua_State *L, struct GCObject *o); + +/* +** Function to print a value +*/ +struct TValue; +LUAI_FUNC void lua_printvalue (struct TValue *v); + /* ** Function to print the stack */ From 975d4e0592f980aef09c432302496d834249b6a7 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 3 Dec 2024 10:53:46 -0300 Subject: [PATCH 0978/1145] Fix in the definition of 'sizeLclosure' The array at the end of a Lua closure has pointers to upvalues, not to tagged values. This bug cannot cause any issue: The ISO C standard requires that all pointers to structures have the same representation, so sizeof(TValue*) must be equal to sizeof(UpVal*). --- lfunc.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lfunc.h b/lfunc.h index b981edebb8..342389e48a 100644 --- a/lfunc.h +++ b/lfunc.h @@ -15,7 +15,7 @@ (offsetof(CClosure, upvalue) + sizeof(TValue) * cast_uint(n)) #define sizeLclosure(n) \ - (offsetof(LClosure, upvals) + sizeof(TValue *) * cast_uint(n)) + (offsetof(LClosure, upvals) + sizeof(UpVal *) * cast_uint(n)) /* test whether thread is in 'twups' list */ From bb93f04d87cfc093757dc712905e1fe3fbe65f58 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 5 Dec 2024 14:27:58 -0300 Subject: [PATCH 0979/1145] Refactoring of 'luaH_newkey' Function broke in two and some checks moved to the caller. (We may want to call it without the checks.) --- lobject.h | 5 +-- ltable.c | 130 ++++++++++++++++++++++++++++++++---------------------- 2 files changed, 79 insertions(+), 56 deletions(-) diff --git a/lobject.h b/lobject.h index 4a0835a8e7..8c06a224cc 100644 --- a/lobject.h +++ b/lobject.h @@ -750,10 +750,9 @@ typedef union Node { /* copy a value into a key */ -#define setnodekey(L,node,obj) \ +#define setnodekey(node,obj) \ { Node *n_=(node); const TValue *io_=(obj); \ - n_->u.key_val = io_->value_; n_->u.key_tt = io_->tt_; \ - checkliveness(L,io_); } + n_->u.key_val = io_->value_; n_->u.key_tt = io_->tt_; } /* copy a value from a key */ diff --git a/ltable.c b/ltable.c index 6ea382a1fb..735ae8738b 100644 --- a/ltable.c +++ b/ltable.c @@ -294,14 +294,34 @@ static const TValue *getgeneric (Table *t, const TValue *key, int deadok) { /* -** returns the index for 'k' if 'k' is an appropriate key to live in -** the array part of a table, 0 otherwise. +** Return the index 'k' (converted to an unsigned) if it is inside +** the range [1, limit]. */ -static unsigned int arrayindex (lua_Integer k) { - if (l_castS2U(k) - 1u < MAXASIZE) /* 'k' in [1, MAXASIZE]? */ - return cast_uint(k); /* 'key' is an appropriate array index */ - else - return 0; +static unsigned checkrange (lua_Integer k, unsigned limit) { + return (l_castS2U(k) - 1u < limit) ? cast_uint(k) : 0; +} + + +/* +** Return the index 'k' if 'k' is an appropriate key to live in the +** array part of a table, 0 otherwise. +*/ +#define arrayindex(k) checkrange(k, MAXASIZE) + + +/* +** Check whether an integer key is in the array part of a table and +** return its index there, or zero. +*/ +#define ikeyinarray(t,k) checkrange(k, t->asize) + + +/* +** Check whether a key is in the array part of a table and return its +** index there, or zero. +*/ +static unsigned keyinarray (Table *t, const TValue *key) { + return (ttisinteger(key)) ? ikeyinarray(t, ivalue(key)) : 0; } @@ -314,8 +334,8 @@ static unsigned findindex (lua_State *L, Table *t, TValue *key, unsigned asize) { unsigned int i; if (ttisnil(key)) return 0; /* first iteration */ - i = ttisinteger(key) ? arrayindex(ivalue(key)) : 0; - if (i - 1u < asize) /* is 'key' inside array part? */ + i = keyinarray(t, key); + if (i != 0) /* is 'key' inside array part? */ return i; /* yes; that's the index */ else { const TValue *n = getgeneric(t, key, 1); @@ -369,14 +389,6 @@ static void freehash (lua_State *L, Table *t) { } -/* -** Check whether an integer key is in the array part of a table. -*/ -l_sinline int keyinarray (Table *t, lua_Integer key) { - return (l_castS2U(key) - 1u < t->asize); /* 'key' in [1, t->asize]? */ -} - - /* ** {============================================================= ** Rehash @@ -829,36 +841,18 @@ static Node *getfreepos (Table *t) { ** position is free. If not, check whether colliding node is in its main ** position or not: if it is not, move colliding node to an empty place ** and put new key in its main position; otherwise (colliding node is in -** its main position), new key goes to an empty position. +** its main position), new key goes to an empty position. Return 0 if +** could not insert key (could not find a free space). */ -static void luaH_newkey (lua_State *L, Table *t, const TValue *key, - TValue *value) { - Node *mp; - TValue aux; - if (l_unlikely(ttisnil(key))) - luaG_runerror(L, "table index is nil"); - else if (ttisfloat(key)) { - lua_Number f = fltvalue(key); - lua_Integer k; - if (luaV_flttointeger(f, &k, F2Ieq)) { /* does key fit in an integer? */ - setivalue(&aux, k); - key = &aux; /* insert it as an integer */ - } - else if (l_unlikely(luai_numisnan(f))) - luaG_runerror(L, "table index is NaN"); - } - if (ttisnil(value)) - return; /* do not insert nil values */ - mp = mainpositionTV(t, key); +static int insertkey (Table *t, const TValue *key, TValue *value) { + Node *mp = mainpositionTV(t, key); + /* table cannot already contain the key */ + lua_assert(isabstkey(getgeneric(t, key, 0))); if (!isempty(gval(mp)) || isdummy(t)) { /* main position is taken? */ Node *othern; Node *f = getfreepos(t); /* get a free place */ - if (f == NULL) { /* cannot find a free place? */ - rehash(L, t, key); /* grow table */ - /* whatever called 'newkey' takes care of TM cache */ - luaH_set(L, t, key, value); /* insert key into grown table */ - return; - } + if (f == NULL) /* cannot find a free place? */ + return 0; lua_assert(!isdummy(t)); othern = mainpositionfromnode(t, mp); if (othern != mp) { /* is colliding node out of its main position? */ @@ -882,16 +876,31 @@ static void luaH_newkey (lua_State *L, Table *t, const TValue *key, mp = f; } } - setnodekey(L, mp, key); - luaC_barrierback(L, obj2gco(t), key); + setnodekey(mp, key); lua_assert(isempty(gval(mp))); - setobj2t(L, gval(mp), value); + setobj2t(cast(lua_State *, 0), gval(mp), value); + return 1; +} + + +static void luaH_newkey (lua_State *L, Table *t, const TValue *key, + TValue *value) { + if (!ttisnil(value)) { /* do not insert nil values */ + int done = insertkey(t, key, value); + if (done) + luaC_barrierback(L, obj2gco(t), key); + else { /* could not find a free place? */ + rehash(L, t, key); /* grow table */ + /* whatever called 'newkey' takes care of TM cache */ + luaH_set(L, t, key, value); /* insert key into grown table */ + } + } } static const TValue *getintfromhash (Table *t, lua_Integer key) { Node *n = hashint(t, key); - lua_assert(!keyinarray(t, key)); + lua_assert(!ikeyinarray(t, key)); for (;;) { /* check whether 'key' is somewhere in the chain */ if (keyisinteger(n) && keyival(n) == key) return gval(n); /* that's it */ @@ -920,10 +929,11 @@ static lu_byte finishnodeget (const TValue *val, TValue *res) { lu_byte luaH_getint (Table *t, lua_Integer key, TValue *res) { - if (keyinarray(t, key)) { - lu_byte tag = *getArrTag(t, key - 1); + unsigned k = ikeyinarray(t, key); + if (k > 0) { + lu_byte tag = *getArrTag(t, k - 1); if (!tagisempty(tag)) - farr2val(t, cast_uint(key) - 1, tag, res); + farr2val(t, k - 1, tag, res); return tag; } else @@ -1031,7 +1041,7 @@ static int rawfinishnodeset (const TValue *slot, TValue *val) { int luaH_psetint (Table *t, lua_Integer key, TValue *val) { - lua_assert(!keyinarray(t, key)); + lua_assert(!ikeyinarray(t, key)); return finishnodeset(t, getintfromhash(t, key), val); } @@ -1081,6 +1091,19 @@ void luaH_finishset (lua_State *L, Table *t, const TValue *key, TValue *value, int hres) { lua_assert(hres != HOK); if (hres == HNOTFOUND) { + TValue aux; + if (l_unlikely(ttisnil(key))) + luaG_runerror(L, "table index is nil"); + else if (ttisfloat(key)) { + lua_Number f = fltvalue(key); + lua_Integer k; + if (luaV_flttointeger(f, &k, F2Ieq)) { + setivalue(&aux, k); /* key is equal to an integer */ + key = &aux; /* insert it as an integer */ + } + else if (l_unlikely(luai_numisnan(f))) + luaG_runerror(L, "table index is NaN"); + } luaH_newkey(L, t, key, value); } else if (hres > 0) { /* regular Node? */ @@ -1109,8 +1132,9 @@ void luaH_set (lua_State *L, Table *t, const TValue *key, TValue *value) { ** integers cannot be keys to metamethods.) */ void luaH_setint (lua_State *L, Table *t, lua_Integer key, TValue *value) { - if (keyinarray(t, key)) - obj2arr(t, cast_uint(key) - 1, value); + unsigned ik = ikeyinarray(t, key); + if (ik > 0) + obj2arr(t, ik - 1, value); else { int ok = rawfinishnodeset(getintfromhash(t, key), value); if (!ok) { From b4b616bdf2beb161b89930cc71a06936e8531b2c Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 5 Dec 2024 17:34:08 -0300 Subject: [PATCH 0980/1145] Rehash reinserts elements with "lighter" functions When reinserting elements into a table during a rehash, the code does not need to invoke all the complexity of a full 'luaH_set': - The table has space for all keys. - The key cannot exist in the new hash. - The keys are valid (not NaN nor nil). - The keys are normalized (1.0 -> 1). - The values cannot be nil. - No barrier needed (the table already pointed to the key and value). --- ltable.c | 50 +++++++++++++++++++++++++++++++++----------------- 1 file changed, 33 insertions(+), 17 deletions(-) diff --git a/ltable.c b/ltable.c index 735ae8738b..052e005e52 100644 --- a/ltable.c +++ b/ltable.c @@ -395,6 +395,10 @@ static void freehash (lua_State *L, Table *t) { ** ============================================================== */ +static int insertkey (Table *t, const TValue *key, TValue *value); +static void newcheckedkey (Table *t, const TValue *key, TValue *value); + + /* ** Structure to count the keys in a table. ** 'total' is the total number of keys in the table. @@ -620,7 +624,7 @@ static void setnodevector (lua_State *L, Table *t, unsigned size) { /* ** (Re)insert all elements from the hash part of 'ot' into table 't'. */ -static void reinsert (lua_State *L, Table *ot, Table *t) { +static void reinserthash (lua_State *L, Table *ot, Table *t) { unsigned j; unsigned size = sizenode(ot); for (j = 0; j < size; j++) { @@ -630,7 +634,7 @@ static void reinsert (lua_State *L, Table *ot, Table *t) { already present in the table */ TValue k; getnodekey(L, &k, old); - luaH_set(L, t, &k, gval(old)); + newcheckedkey(t, &k, gval(old)); } } } @@ -659,20 +663,18 @@ static void exchangehashpart (Table *t1, Table *t2) { ** Re-insert into the new hash part of a table the elements from the ** vanishing slice of the array part. */ -static void reinsertOldSlice (lua_State *L, Table *t, unsigned oldasize, - unsigned newasize) { +static void reinsertOldSlice (Table *t, unsigned oldasize, + unsigned newasize) { unsigned i; - t->asize = newasize; /* pretend array has new size... */ for (i = newasize; i < oldasize; i++) { /* traverse vanishing slice */ lu_byte tag = *getArrTag(t, i); if (!tagisempty(tag)) { /* a non-empty entry? */ - TValue aux; - farr2val(t, i, tag, &aux); /* copy entry into 'aux' */ - /* re-insert it into the table */ - luaH_setint(L, t, cast_int(i) + 1, &aux); + TValue key, aux; + setivalue(&key, l_castU2S(i) + 1); /* make the key */ + farr2val(t, i, tag, &aux); /* copy value into 'aux' */ + insertkey(t, &key, &aux); /* insert entry into the hash part */ } } - t->asize = oldasize; /* restore current size... */ } @@ -714,7 +716,7 @@ void luaH_resize (lua_State *L, Table *t, unsigned newasize, if (newasize < oldasize) { /* will array shrink? */ /* re-insert into the new hash the elements from vanishing slice */ exchangehashpart(t, &newt); /* pretend table has new hash */ - reinsertOldSlice(L, t, oldasize, newasize); + reinsertOldSlice(t, oldasize, newasize); exchangehashpart(t, &newt); /* restore old hash (in case of errors) */ } /* allocate new array */ @@ -731,7 +733,7 @@ void luaH_resize (lua_State *L, Table *t, unsigned newasize, *lenhint(t) = newasize / 2u; /* set an initial hint */ clearNewSlice(t, oldasize, newasize); /* re-insert elements from old hash part into new parts */ - reinsert(L, &newt, t); /* 'newt' now has the old hash */ + reinserthash(L, &newt, t); /* 'newt' now has the old hash */ freehash(L, &newt); /* free old hash part */ } @@ -883,17 +885,31 @@ static int insertkey (Table *t, const TValue *key, TValue *value) { } +/* +** Insert a key in a table where there is space for that key, the +** key is valid, and the value is not nil. +*/ +static void newcheckedkey (Table *t, const TValue *key, TValue *value) { + unsigned i = keyinarray(t, key); + if (i > 0) /* is key in the array part? */ + obj2arr(t, i - 1, value); /* set value in the array */ + else { + int done = insertkey(t, key, value); /* insert key in the hash part */ + lua_assert(done); /* it cannot fail */ + cast(void, done); /* to avoid warnings */ + } +} + + static void luaH_newkey (lua_State *L, Table *t, const TValue *key, TValue *value) { if (!ttisnil(value)) { /* do not insert nil values */ int done = insertkey(t, key, value); - if (done) - luaC_barrierback(L, obj2gco(t), key); - else { /* could not find a free place? */ + if (!done) { /* could not find a free place? */ rehash(L, t, key); /* grow table */ - /* whatever called 'newkey' takes care of TM cache */ - luaH_set(L, t, key, value); /* insert key into grown table */ + newcheckedkey(t, key, value); /* insert key in grown table */ } + luaC_barrierback(L, obj2gco(t), key); } } From 25a491fe349fc52b69ece2ecbcb0b0189decb36f Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 11 Dec 2024 13:56:03 -0300 Subject: [PATCH 0981/1145] OP_SELF restricted to constant short strings Optimize this opcode for the common case. For long names or method calls after too many constants, operation can be coded as a move followed by 'gettable'. --- lcode.c | 43 +++++++++++++++++++++++++++---------------- ldebug.c | 15 ++------------- lopcodes.h | 2 +- lvm.c | 6 +++--- testes/errors.lua | 3 ++- 5 files changed, 35 insertions(+), 34 deletions(-) diff --git a/lcode.c b/lcode.c index 4267079495..6c124ff64b 100644 --- a/lcode.c +++ b/lcode.c @@ -1085,22 +1085,6 @@ void luaK_storevar (FuncState *fs, expdesc *var, expdesc *ex) { } -/* -** Emit SELF instruction (convert expression 'e' into 'e:key(e,'). -*/ -void luaK_self (FuncState *fs, expdesc *e, expdesc *key) { - int ereg; - luaK_exp2anyreg(fs, e); - ereg = e->u.info; /* register where 'e' was placed */ - freeexp(fs, e); - e->u.info = fs->freereg; /* base register for op_self */ - e->k = VNONRELOC; /* self expression has a fixed register */ - luaK_reserveregs(fs, 2); /* function and 'self' produced by op_self */ - codeABRK(fs, OP_SELF, e->u.info, ereg, key); - freeexp(fs, key); -} - - /* ** Negate condition 'e' (where 'e' is a comparison). */ @@ -1275,6 +1259,33 @@ static int isSCnumber (expdesc *e, int *pi, int *isfloat) { } +/* +** Emit SELF instruction or equivalent: the code will convert +** expression 'e' into 'e.key(e,'. +*/ +void luaK_self (FuncState *fs, expdesc *e, expdesc *key) { + int ereg, base; + luaK_exp2anyreg(fs, e); + ereg = e->u.info; /* register where 'e' (the receiver) was placed */ + freeexp(fs, e); + base = e->u.info = fs->freereg; /* base register for op_self */ + e->k = VNONRELOC; /* self expression has a fixed register */ + luaK_reserveregs(fs, 2); /* method and 'self' produced by op_self */ + lua_assert(key->k == VKSTR); + /* is method name a short string in a valid K index? */ + if (strisshr(key->u.strval) && luaK_exp2K(fs, key)) { + /* can use 'self' opcode */ + luaK_codeABCk(fs, OP_SELF, base, ereg, key->u.info, 0); + } + else { /* cannot use 'self' opcode; use move+gettable */ + luaK_exp2anyreg(fs, key); /* put method name in a register */ + luaK_codeABC(fs, OP_MOVE, base + 1, ereg, 0); /* copy self to base+1 */ + luaK_codeABC(fs, OP_GETTABLE, base, ereg, key->u.info); /* get method */ + } + freeexp(fs, key); +} + + /* ** Create expression 't[k]'. 't' must have its final result already in a ** register or upvalue. Upvalues can only be indexed by literal strings. diff --git a/ldebug.c b/ldebug.c index ee3ac17fb5..09ec197c42 100644 --- a/ldebug.c +++ b/ldebug.c @@ -541,18 +541,6 @@ static void rname (const Proto *p, int pc, int c, const char **name) { } -/* -** Find a "name" for a 'C' value in an RK instruction. -*/ -static void rkname (const Proto *p, int pc, Instruction i, const char **name) { - int c = GETARG_C(i); /* key index */ - if (GETARG_k(i)) /* is 'c' a constant? */ - kname(p, c, name); - else /* 'c' is a register */ - rname(p, pc, c, name); -} - - /* ** Check whether table being indexed by instruction 'i' is the ** environment '_ENV' @@ -600,7 +588,8 @@ static const char *getobjname (const Proto *p, int lastpc, int reg, return isEnv(p, lastpc, i, 0); } case OP_SELF: { - rkname(p, lastpc, i, name); + int k = GETARG_C(i); /* key index */ + kname(p, k, name); return "method"; } default: break; /* go through to return NULL */ diff --git a/lopcodes.h b/lopcodes.h index 31f6fac01b..7511eb2237 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -256,7 +256,7 @@ OP_SETFIELD,/* A B C R[A][K[B]:shortstring] := RK(C) */ OP_NEWTABLE,/* A B C k R[A] := {} */ -OP_SELF,/* A B C R[A+1] := R[B]; R[A] := R[B][RK(C):string] */ +OP_SELF,/* A B C R[A+1] := R[B]; R[A] := R[B][K[C]:shortstring] */ OP_ADDI,/* A B sC R[A] := R[B] + sC */ diff --git a/lvm.c b/lvm.c index 1c564a713d..b6b18a69aa 100644 --- a/lvm.c +++ b/lvm.c @@ -1382,10 +1382,10 @@ void luaV_execute (lua_State *L, CallInfo *ci) { StkId ra = RA(i); lu_byte tag; TValue *rb = vRB(i); - TValue *rc = RKC(i); - TString *key = tsvalue(rc); /* key must be a string */ + TValue *rc = KC(i); + TString *key = tsvalue(rc); /* key must be a short string */ setobj2s(L, ra + 1, rb); - luaV_fastget(rb, key, s2v(ra), luaH_getstr, tag); + luaV_fastget(rb, key, s2v(ra), luaH_getshortstr, tag); if (tagisempty(tag)) Protect(luaV_finishget(L, rb, rc, ra, tag)); vmbreak; diff --git a/testes/errors.lua b/testes/errors.lua index 0925fe582a..027e1b03af 100644 --- a/testes/errors.lua +++ b/testes/errors.lua @@ -321,7 +321,8 @@ t = nil checkmessage(s.."; aaa = bbb + 1", "global 'bbb'") checkmessage("local _ENV=_ENV;"..s.."; aaa = bbb + 1", "global 'bbb'") checkmessage(s.."; local t = {}; aaa = t.bbb + 1", "field 'bbb'") -checkmessage(s.."; local t = {}; t:bbb()", "method 'bbb'") +-- cannot use 'self' opcode +checkmessage(s.."; local t = {}; t:bbb()", "field 'bbb'") checkmessage([[aaa=9 repeat until 3==3 From 412e9a4d952d47631feddfa4ec25a520ec75b103 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 11 Dec 2024 15:32:43 -0300 Subject: [PATCH 0982/1145] 'luaH_fastseti' uses 'checknoTM' The extra check in checknoTM (versus only checking whether there is a metatable) is cheap, and it is not that uncommon for a table to have a metatable without a __newindex metafield. --- ltable.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ltable.h b/ltable.h index 9c4eb937bc..e4aa98f047 100644 --- a/ltable.h +++ b/ltable.h @@ -58,7 +58,7 @@ { Table *h = t; lua_Unsigned u = l_castS2U(k) - 1u; \ if ((u < h->asize)) { \ lu_byte *tag = getArrTag(h, u); \ - if (h->metatable == NULL || !tagisempty(*tag)) \ + if (checknoTM(h->metatable, TM_NEWINDEX) || !tagisempty(*tag)) \ { fval2arr(h, u, tag, val); hres = HOK; } \ else hres = ~cast_int(u); } \ else { hres = luaH_psetint(h, k, val); }} From 7538f3886dfa091d661c56e48ebb1578ced8e467 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 16 Dec 2024 14:13:49 -0300 Subject: [PATCH 0983/1145] 'addk' broken in two functions --- lcode.c | 47 ++++++++++++++++++++++++++++------------------- 1 file changed, 28 insertions(+), 19 deletions(-) diff --git a/lcode.c b/lcode.c index 6c124ff64b..e6a98bb649 100644 --- a/lcode.c +++ b/lcode.c @@ -537,6 +537,22 @@ static void freeexps (FuncState *fs, expdesc *e1, expdesc *e2) { /* ** Add constant 'v' to prototype's list of constants (field 'k'). +*/ +static int addk (FuncState *fs, Proto *f, TValue *v) { + lua_State *L = fs->ls->L; + int oldsize = f->sizek; + int k = fs->nk; + luaM_growvector(L, f->k, k, f->sizek, TValue, MAXARG_Ax, "constants"); + while (oldsize < f->sizek) + setnilvalue(&f->k[oldsize++]); + setobj(L, &f->k[k], v); + fs->nk++; + luaC_barrier(L, f, v); + return k; +} + + +/* ** Use scanner's table to cache position of constants in constant list ** and try to reuse constants. Because some values should not be used ** as keys (nil cannot be a key, integer keys can collapse with float @@ -544,12 +560,11 @@ static void freeexps (FuncState *fs, expdesc *e1, expdesc *e2) { ** Note that all functions share the same table, so entering or exiting ** a function can make some indices wrong. */ -static int addk (FuncState *fs, TValue *key, TValue *v) { +static int k2proto (FuncState *fs, TValue *key, TValue *v) { TValue val; - lua_State *L = fs->ls->L; Proto *f = fs->f; int tag = luaH_get(fs->ls->h, key, &val); /* query scanner table */ - int k, oldsize; + int k; if (tag == LUA_VNUMINT) { /* is there an index there? */ k = cast_int(ivalue(&val)); /* correct value? (warning: must distinguish floats from integers!) */ @@ -558,17 +573,11 @@ static int addk (FuncState *fs, TValue *key, TValue *v) { return k; /* reuse index */ } /* constant not found; create a new entry */ - oldsize = f->sizek; - k = fs->nk; - /* numerical value does not need GC barrier; + k = addk(fs, f, v); + /* cache for reuse; numerical value does not need GC barrier; table has no metatable, so it does not need to invalidate cache */ setivalue(&val, k); - luaH_set(L, fs->ls->h, key, &val); - luaM_growvector(L, f->k, k, f->sizek, TValue, MAXARG_Ax, "constants"); - while (oldsize < f->sizek) setnilvalue(&f->k[oldsize++]); - setobj(L, &f->k[k], v); - fs->nk++; - luaC_barrier(L, f, v); + luaH_set(fs->ls->L, fs->ls->h, key, &val); return k; } @@ -579,7 +588,7 @@ static int addk (FuncState *fs, TValue *key, TValue *v) { static int stringK (FuncState *fs, TString *s) { TValue o; setsvalue(fs->ls->L, &o, s); - return addk(fs, &o, &o); /* use string itself as key */ + return k2proto(fs, &o, &o); /* use string itself as key */ } @@ -589,7 +598,7 @@ static int stringK (FuncState *fs, TString *s) { static int luaK_intK (FuncState *fs, lua_Integer n) { TValue o; setivalue(&o, n); - return addk(fs, &o, &o); /* use integer itself as key */ + return k2proto(fs, &o, &o); /* use integer itself as key */ } /* @@ -608,7 +617,7 @@ static int luaK_numberK (FuncState *fs, lua_Number r) { lua_Integer ik; setfltvalue(&o, r); if (!luaV_flttointeger(r, &ik, F2Ieq)) /* not an integral value? */ - return addk(fs, &o, &o); /* use number itself as key */ + return k2proto(fs, &o, &o); /* use number itself as key */ else { /* must build an alternative key */ const int nbm = l_floatatt(MANT_DIG); const lua_Number q = l_mathop(ldexp)(l_mathop(1.0), -nbm + 1); @@ -618,7 +627,7 @@ static int luaK_numberK (FuncState *fs, lua_Number r) { /* result is not an integral value, unless value is too large */ lua_assert(!luaV_flttointeger(k, &ik, F2Ieq) || l_mathop(fabs)(r) >= l_mathop(1e6)); - return addk(fs, &kv, &o); + return k2proto(fs, &kv, &o); } } @@ -629,7 +638,7 @@ static int luaK_numberK (FuncState *fs, lua_Number r) { static int boolF (FuncState *fs) { TValue o; setbfvalue(&o); - return addk(fs, &o, &o); /* use boolean itself as key */ + return k2proto(fs, &o, &o); /* use boolean itself as key */ } @@ -639,7 +648,7 @@ static int boolF (FuncState *fs) { static int boolT (FuncState *fs) { TValue o; setbtvalue(&o); - return addk(fs, &o, &o); /* use boolean itself as key */ + return k2proto(fs, &o, &o); /* use boolean itself as key */ } @@ -651,7 +660,7 @@ static int nilK (FuncState *fs) { setnilvalue(&v); /* cannot use nil as key; instead use table itself to represent nil */ sethvalue(fs->ls->L, &k, fs->ls->h); - return addk(fs, &k, &v); + return k2proto(fs, &k, &v); } From 1c40ff9faafed620aa0458b397bcbfbe19e0f663 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 17 Dec 2024 11:23:22 -0300 Subject: [PATCH 0984/1145] Scanner and parser use different tables for constants Moreover, each function being parsed has its own table. The code is cleaner when each table is used for one specific purpose: The scanner uses its table to anchor and unify strings, mapping strings to themselves; the parser uses it to reuse constants in the code, mapping constants to their indices in the constant table. A different table for each task avoids false collisions. --- lcode.c | 12 ++++++------ llex.c | 15 ++++++--------- lparser.c | 7 ++++++- lparser.h | 1 + ltable.c | 11 +---------- ltable.h | 2 -- 6 files changed, 20 insertions(+), 28 deletions(-) diff --git a/lcode.c b/lcode.c index e6a98bb649..8f08302eb5 100644 --- a/lcode.c +++ b/lcode.c @@ -563,13 +563,13 @@ static int addk (FuncState *fs, Proto *f, TValue *v) { static int k2proto (FuncState *fs, TValue *key, TValue *v) { TValue val; Proto *f = fs->f; - int tag = luaH_get(fs->ls->h, key, &val); /* query scanner table */ + int tag = luaH_get(fs->kcache, key, &val); /* query scanner table */ int k; - if (tag == LUA_VNUMINT) { /* is there an index there? */ + if (!tagisempty(tag)) { /* is there an index there? */ k = cast_int(ivalue(&val)); + lua_assert(k < fs->nk); /* correct value? (warning: must distinguish floats from integers!) */ - if (k < fs->nk && ttypetag(&f->k[k]) == ttypetag(v) && - luaV_rawequalobj(&f->k[k], v)) + if (ttypetag(&f->k[k]) == ttypetag(v) && luaV_rawequalobj(&f->k[k], v)) return k; /* reuse index */ } /* constant not found; create a new entry */ @@ -577,7 +577,7 @@ static int k2proto (FuncState *fs, TValue *key, TValue *v) { /* cache for reuse; numerical value does not need GC barrier; table has no metatable, so it does not need to invalidate cache */ setivalue(&val, k); - luaH_set(fs->ls->L, fs->ls->h, key, &val); + luaH_set(fs->ls->L, fs->kcache, key, &val); return k; } @@ -659,7 +659,7 @@ static int nilK (FuncState *fs) { TValue k, v; setnilvalue(&v); /* cannot use nil as key; instead use table itself to represent nil */ - sethvalue(fs->ls->L, &k, fs->ls->h); + sethvalue(fs->ls->L, &k, fs->kcache); return k2proto(fs, &k, &v); } diff --git a/llex.c b/llex.c index b2e77c9c87..d913db1754 100644 --- a/llex.c +++ b/llex.c @@ -130,18 +130,15 @@ l_noret luaX_syntaxerror (LexState *ls, const char *msg) { ** Creates a new string and anchors it in scanner's table so that it ** will not be collected until the end of the compilation; by that time ** it should be anchored somewhere. It also internalizes long strings, -** ensuring there is only one copy of each unique string. The table -** here is used as a set: the string enters as the key, while its value -** is irrelevant. We use the string itself as the value only because it -** is a TValue readily available. Later, the code generation can change -** this value. +** ensuring there is only one copy of each unique string. */ TString *luaX_newstring (LexState *ls, const char *str, size_t l) { lua_State *L = ls->L; TString *ts = luaS_newlstr(L, str, l); /* create new string */ - TString *oldts = luaH_getstrkey(ls->h, ts); - if (oldts != NULL) /* string already present? */ - return oldts; /* use it */ + TValue oldts; + int tag = luaH_getstr(ls->h, ts, &oldts); + if (!tagisempty(tag)) /* string already present? */ + return tsvalue(&oldts); /* use stored value */ else { /* create a new entry */ TValue *stv = s2v(L->top.p++); /* reserve stack space for string */ setsvalue(L, stv, ts); /* temporarily anchor the string */ @@ -149,8 +146,8 @@ TString *luaX_newstring (LexState *ls, const char *str, size_t l) { /* table is not a metatable, so it does not need to invalidate cache */ luaC_checkGC(L); L->top.p--; /* remove string from stack */ + return ts; } - return ts; } diff --git a/lparser.c b/lparser.c index 3db7df4cc5..642e43b7ea 100644 --- a/lparser.c +++ b/lparser.c @@ -737,6 +737,7 @@ static void codeclosure (LexState *ls, expdesc *v) { static void open_func (LexState *ls, FuncState *fs, BlockCnt *bl) { + lua_State *L = ls->L; Proto *f = fs->f; fs->prev = ls->fs; /* linked list of funcstates */ fs->ls = ls; @@ -757,8 +758,11 @@ static void open_func (LexState *ls, FuncState *fs, BlockCnt *bl) { fs->firstlabel = ls->dyd->label.n; fs->bl = NULL; f->source = ls->source; - luaC_objbarrier(ls->L, f, f->source); + luaC_objbarrier(L, f, f->source); f->maxstacksize = 2; /* registers 0/1 are always valid */ + fs->kcache = luaH_new(L); /* create table for function */ + sethvalue2s(L, L->top.p, fs->kcache); /* anchor it */ + luaD_inctop(L); enterblock(fs, bl, 0); } @@ -780,6 +784,7 @@ static void close_func (LexState *ls) { luaM_shrinkvector(L, f->locvars, f->sizelocvars, fs->ndebugvars, LocVar); luaM_shrinkvector(L, f->upvalues, f->sizeupvalues, fs->nups, Upvaldesc); ls->fs = fs->prev; + L->top.p--; /* pop kcache table */ luaC_checkGC(L); } diff --git a/lparser.h b/lparser.h index 8a87776d67..589befdb76 100644 --- a/lparser.h +++ b/lparser.h @@ -146,6 +146,7 @@ typedef struct FuncState { struct FuncState *prev; /* enclosing function */ struct LexState *ls; /* lexical state */ struct BlockCnt *bl; /* chain of current blocks */ + Table *kcache; /* cache for reusing constants */ int pc; /* next position to code (equivalent to 'ncode') */ int lasttarget; /* 'label' of last 'jump label' */ int previousline; /* last line that was saved in 'lineinfo' */ diff --git a/ltable.c b/ltable.c index 052e005e52..eb5abf9f3d 100644 --- a/ltable.c +++ b/ltable.c @@ -962,7 +962,7 @@ lu_byte luaH_getint (Table *t, lua_Integer key, TValue *res) { */ const TValue *luaH_Hgetshortstr (Table *t, TString *key) { Node *n = hashstr(t, key); - lua_assert(key->tt == LUA_VSHRSTR); + lua_assert(strisshr(key)); for (;;) { /* check whether 'key' is somewhere in the chain */ if (keyisshrstr(n) && eqshrstr(keystrval(n), key)) return gval(n); /* that's it */ @@ -997,15 +997,6 @@ lu_byte luaH_getstr (Table *t, TString *key, TValue *res) { } -TString *luaH_getstrkey (Table *t, TString *key) { - const TValue *o = Hgetstr(t, key); - if (!isabstkey(o)) /* string already present? */ - return keystrval(nodefromval(o)); /* get saved copy */ - else - return NULL; -} - - /* ** main search function */ diff --git a/ltable.h b/ltable.h index e4aa98f047..ca21e69202 100644 --- a/ltable.h +++ b/ltable.h @@ -154,8 +154,6 @@ LUAI_FUNC lu_byte luaH_getint (Table *t, lua_Integer key, TValue *res); /* Special get for metamethods */ LUAI_FUNC const TValue *luaH_Hgetshortstr (Table *t, TString *key); -LUAI_FUNC TString *luaH_getstrkey (Table *t, TString *key); - LUAI_FUNC int luaH_psetint (Table *t, lua_Integer key, TValue *val); LUAI_FUNC int luaH_psetshortstr (Table *t, TString *key, TValue *val); LUAI_FUNC int luaH_psetstr (Table *t, TString *key, TValue *val); From f81d0bbd4f940399eb4b68845802bc3fe1cad73a Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 17 Dec 2024 13:36:12 -0300 Subject: [PATCH 0985/1145] Detail in 'luaD_inctop' Protect stack top before possible stack reallocation. (In the current implementation, a stack reallocation cannot call an emergency collection, so there is no bug, but it is safer not to depend on that.) --- ldo.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ldo.c b/ldo.c index 9459391f9f..f825d95950 100644 --- a/ldo.c +++ b/ldo.c @@ -373,8 +373,8 @@ void luaD_shrinkstack (lua_State *L) { void luaD_inctop (lua_State *L) { - luaD_checkstack(L, 1); L->top.p++; + luaD_checkstack(L, 1); } /* }================================================================== */ From 2a307f898be2716638e7ac30d119f7cd9bbbe096 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Sat, 28 Dec 2024 15:03:48 -0300 Subject: [PATCH 0986/1145] When parser reuses constants, only floats can collide Ensure that float constants never use integer keys, so that only floats can collide in 'k2proto'. --- lcode.c | 54 ++++++++++++++++++++++++++++-------------------------- 1 file changed, 28 insertions(+), 26 deletions(-) diff --git a/lcode.c b/lcode.c index 8f08302eb5..641f0d09cc 100644 --- a/lcode.c +++ b/lcode.c @@ -557,8 +557,6 @@ static int addk (FuncState *fs, Proto *f, TValue *v) { ** and try to reuse constants. Because some values should not be used ** as keys (nil cannot be a key, integer keys can collapse with float ** keys), the caller must provide a useful 'key' for indexing the cache. -** Note that all functions share the same table, so entering or exiting -** a function can make some indices wrong. */ static int k2proto (FuncState *fs, TValue *key, TValue *v) { TValue val; @@ -567,15 +565,14 @@ static int k2proto (FuncState *fs, TValue *key, TValue *v) { int k; if (!tagisempty(tag)) { /* is there an index there? */ k = cast_int(ivalue(&val)); - lua_assert(k < fs->nk); - /* correct value? (warning: must distinguish floats from integers!) */ - if (ttypetag(&f->k[k]) == ttypetag(v) && luaV_rawequalobj(&f->k[k], v)) - return k; /* reuse index */ + /* collisions can happen only for float keys */ + lua_assert(ttisfloat(key) || luaV_rawequalobj(&f->k[k], v)); + return k; /* reuse index */ } /* constant not found; create a new entry */ k = addk(fs, f, v); - /* cache for reuse; numerical value does not need GC barrier; - table has no metatable, so it does not need to invalidate cache */ + /* cache it for reuse; numerical value does not need GC barrier; + table is not a metatable, so it does not need to invalidate cache */ setivalue(&val, k); luaH_set(fs->ls->L, fs->kcache, key, &val); return k; @@ -607,27 +604,32 @@ static int luaK_intK (FuncState *fs, lua_Integer n) { ** with actual integers. To that, we add to the number its smaller ** power-of-two fraction that is still significant in its scale. ** For doubles, that would be 1/2^52. -** (This method is not bulletproof: there may be another float -** with that value, and for floats larger than 2^53 the result is -** still an integer. At worst, this only wastes an entry with -** a duplicate.) +** This method is not bulletproof: different numbers may generate the +** same key (e.g., very large numbers will overflow to 'inf') and for +** floats larger than 2^53 the result is still an integer. At worst, +** this only wastes an entry with a duplicate. */ static int luaK_numberK (FuncState *fs, lua_Number r) { - TValue o; - lua_Integer ik; - setfltvalue(&o, r); - if (!luaV_flttointeger(r, &ik, F2Ieq)) /* not an integral value? */ - return k2proto(fs, &o, &o); /* use number itself as key */ - else { /* must build an alternative key */ + TValue o, kv; + setfltvalue(&o, r); /* value as a TValue */ + if (r == 0) { /* handle zero as a special case */ + setpvalue(&kv, fs); /* use FuncState as index */ + return k2proto(fs, &kv, &o); /* cannot collide */ + } + else { const int nbm = l_floatatt(MANT_DIG); const lua_Number q = l_mathop(ldexp)(l_mathop(1.0), -nbm + 1); - const lua_Number k = (ik == 0) ? q : r + r*q; /* new key */ - TValue kv; - setfltvalue(&kv, k); - /* result is not an integral value, unless value is too large */ - lua_assert(!luaV_flttointeger(k, &ik, F2Ieq) || - l_mathop(fabs)(r) >= l_mathop(1e6)); - return k2proto(fs, &kv, &o); + const lua_Number k = r * (1 + q); /* key */ + lua_Integer ik; + setfltvalue(&kv, k); /* key as a TValue */ + if (!luaV_flttointeger(k, &ik, F2Ieq)) { /* not an integral value? */ + int n = k2proto(fs, &kv, &o); /* use key */ + if (luaV_rawequalobj(&fs->f->k[n], &o)) /* correct value? */ + return n; + } + /* else, either key is still an integer or there was a collision; + anyway, do not try to reuse constant; instead, create a new one */ + return addk(fs, fs->f, &o); } } @@ -658,7 +660,7 @@ static int boolT (FuncState *fs) { static int nilK (FuncState *fs) { TValue k, v; setnilvalue(&v); - /* cannot use nil as key; instead use table itself to represent nil */ + /* cannot use nil as key; instead use table itself */ sethvalue(fs->ls->L, &k, fs->kcache); return k2proto(fs, &k, &v); } From abf8b1cd4a798fada026b4046e9dbc08791963f2 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Sat, 28 Dec 2024 15:05:01 -0300 Subject: [PATCH 0987/1145] Small optimization in 'luaH_psetshortstr' Do not optimize only for table updates (key already present). Creation of new short keys in new tables can be quite common in programs that create lots of small tables, for instance with constructors like {x=e1,y=e2}. --- ltable.c | 81 +++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 63 insertions(+), 18 deletions(-) diff --git a/ltable.c b/ltable.c index eb5abf9f3d..f67853675e 100644 --- a/ltable.c +++ b/ltable.c @@ -981,14 +981,19 @@ lu_byte luaH_getshortstr (Table *t, TString *key, TValue *res) { } +static const TValue *Hgetlongstr (Table *t, TString *key) { + TValue ko; + lua_assert(!strisshr(key)); + setsvalue(cast(lua_State *, NULL), &ko, key); + return getgeneric(t, &ko, 0); /* for long strings, use generic case */ +} + + static const TValue *Hgetstr (Table *t, TString *key) { - if (key->tt == LUA_VSHRSTR) + if (strisshr(key)) return luaH_Hgetshortstr(t, key); - else { /* for long strings, use generic case */ - TValue ko; - setsvalue(cast(lua_State *, NULL), &ko, key); - return getgeneric(t, &ko, 0); - } + else + return Hgetlongstr(t, key); } @@ -1025,15 +1030,25 @@ lu_byte luaH_get (Table *t, const TValue *key, TValue *res) { } +/* +** When a 'pset' cannot be completed, this function returns an encoding +** of its result, to be used by 'luaH_finishset'. +*/ +static int retpsetcode (Table *t, const TValue *slot) { + if (isabstkey(slot)) + return HNOTFOUND; /* no slot with that key */ + else /* return node encoded */ + return cast_int((cast(Node*, slot) - t->node)) + HFIRSTNODE; +} + + static int finishnodeset (Table *t, const TValue *slot, TValue *val) { if (!ttisnil(slot)) { setobj(((lua_State*)NULL), cast(TValue*, slot), val); return HOK; /* success */ } - else if (isabstkey(slot)) - return HNOTFOUND; /* no slot with that key */ - else /* return node encoded */ - return cast_int((cast(Node*, slot) - t->node)) + HFIRSTNODE; + else + return retpsetcode(t, slot); } @@ -1060,13 +1075,45 @@ static int psetint (Table *t, lua_Integer key, TValue *val) { } +/* +** This function could be just this: +** return finishnodeset(t, luaH_Hgetshortstr(t, key), val); +** However, it optimizes the common case created by constructors (e.g., +** {x=1, y=2}), which creates a key in a table that has no metatable, +** it is not old/black, and it already has space for the key. +*/ + int luaH_psetshortstr (Table *t, TString *key, TValue *val) { - return finishnodeset(t, luaH_Hgetshortstr(t, key), val); + const TValue *slot = luaH_Hgetshortstr(t, key); + if (!ttisnil(slot)) { /* key already has a value? (all too common) */ + setobj(((lua_State*)NULL), cast(TValue*, slot), val); /* update it */ + return HOK; /* done */ + } + else if (checknoTM(t->metatable, TM_NEWINDEX)) { /* no metamethod? */ + if (ttisnil(val)) /* new value is nil? */ + return HOK; /* done (value is already nil/absent) */ + if (isabstkey(slot) && /* key is absent? */ + !(isblack(t) && iswhite(key))) { /* and don't need barrier? */ + TValue tk; /* key as a TValue */ + setsvalue(cast(lua_State *, NULL), &tk, key); + if (insertkey(t, &tk, val)) { /* insert key, if there is space */ + invalidateTMcache(t); + return HOK; + } + } + } + /* Else, either table has new-index metamethod, or it needs barrier, + or it needs to rehash for the new key. In any of these cases, the + operation cannot be completed here. Return a code for the caller. */ + return retpsetcode(t, slot); } int luaH_psetstr (Table *t, TString *key, TValue *val) { - return finishnodeset(t, Hgetstr(t, key), val); + if (strisshr(key)) + return luaH_psetshortstr(t, key, val); + else + return finishnodeset(t, Hgetlongstr(t, key), val); } @@ -1087,13 +1134,11 @@ int luaH_pset (Table *t, const TValue *key, TValue *val) { } /* -** Finish a raw "set table" operation, where 'slot' is where the value -** should have been (the result of a previous "get table"). -** Beware: when using this function you probably need to check a GC -** barrier and invalidate the TM cache. +** Finish a raw "set table" operation, where 'hres' encodes where the +** value should have been (the result of a previous 'pset' operation). +** Beware: when using this function the caller probably need to check a +** GC barrier and invalidate the TM cache. */ - - void luaH_finishset (lua_State *L, Table *t, const TValue *key, TValue *value, int hres) { lua_assert(hres != HOK); From 5894ca7b95d7fb05f1e93ee77e849a8d816d1c6d Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 30 Dec 2024 16:53:51 -0300 Subject: [PATCH 0988/1145] Scanner doesn't need to anchor reserved words --- llex.c | 30 +++++++++++++++++++----------- lstring.h | 2 +- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/llex.c b/llex.c index d913db1754..3518f0dab6 100644 --- a/llex.c +++ b/llex.c @@ -127,21 +127,20 @@ l_noret luaX_syntaxerror (LexState *ls, const char *msg) { /* -** Creates a new string and anchors it in scanner's table so that it -** will not be collected until the end of the compilation; by that time -** it should be anchored somewhere. It also internalizes long strings, -** ensuring there is only one copy of each unique string. +** Anchors a string in scanner's table so that it will not be collected +** until the end of the compilation; by that time it should be anchored +** somewhere. It also internalizes long strings, ensuring there is only +** one copy of each unique string. */ -TString *luaX_newstring (LexState *ls, const char *str, size_t l) { +static TString *anchorstr (LexState *ls, TString *ts) { lua_State *L = ls->L; - TString *ts = luaS_newlstr(L, str, l); /* create new string */ TValue oldts; int tag = luaH_getstr(ls->h, ts, &oldts); if (!tagisempty(tag)) /* string already present? */ return tsvalue(&oldts); /* use stored value */ else { /* create a new entry */ TValue *stv = s2v(L->top.p++); /* reserve stack space for string */ - setsvalue(L, stv, ts); /* temporarily anchor the string */ + setsvalue(L, stv, ts); /* push (anchor) the string on the stack */ luaH_set(L, ls->h, stv, stv); /* t[string] = string */ /* table is not a metatable, so it does not need to invalidate cache */ luaC_checkGC(L); @@ -151,6 +150,14 @@ TString *luaX_newstring (LexState *ls, const char *str, size_t l) { } +/* +** Creates a new string and anchors it in scanner's table. +*/ +TString *luaX_newstring (LexState *ls, const char *str, size_t l) { + return anchorstr(ls, luaS_newlstr(ls->L, str, l)); +} + + /* ** increment line number and skips newline sequence (any of ** \n, \r, \n\r, or \r\n) @@ -544,12 +551,13 @@ static int llex (LexState *ls, SemInfo *seminfo) { do { save_and_next(ls); } while (lislalnum(ls->current)); - ts = luaX_newstring(ls, luaZ_buffer(ls->buff), - luaZ_bufflen(ls->buff)); - seminfo->ts = ts; - if (isreserved(ts)) /* reserved word? */ + /* find or create string */ + ts = luaS_newlstr(ls->L, luaZ_buffer(ls->buff), + luaZ_bufflen(ls->buff)); + if (isreserved(ts)) /* reserved word? */ return ts->extra - 1 + FIRST_RESERVED; else { + seminfo->ts = anchorstr(ls, ts); return TK_NAME; } } diff --git a/lstring.h b/lstring.h index 26f4b8e1f3..1751e0434e 100644 --- a/lstring.h +++ b/lstring.h @@ -45,7 +45,7 @@ /* ** test whether a string is a reserved word */ -#define isreserved(s) ((s)->tt == LUA_VSHRSTR && (s)->extra > 0) +#define isreserved(s) (strisshr(s) && (s)->extra > 0) /* From 1ec251e091302515e54aa81d965840a5de4be0a1 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 6 Jan 2025 12:41:39 -0300 Subject: [PATCH 0989/1145] Detail (debugging aid) When compiling with option HARDMEMTESTS, every creation of a new key in a table forces an emergency GC. --- lgc.h | 8 ++++---- ltable.c | 2 ++ 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/lgc.h b/lgc.h index 339cc17622..ee0541793b 100644 --- a/lgc.h +++ b/lgc.h @@ -224,15 +224,15 @@ */ #if !defined(HARDMEMTESTS) -#define condchangemem(L,pre,pos) ((void)0) +#define condchangemem(L,pre,pos,emg) ((void)0) #else -#define condchangemem(L,pre,pos) \ - { if (gcrunning(G(L))) { pre; luaC_fullgc(L, 0); pos; } } +#define condchangemem(L,pre,pos,emg) \ + { if (gcrunning(G(L))) { pre; luaC_fullgc(L, emg); pos; } } #endif #define luaC_condGC(L,pre,pos) \ { if (G(L)->GCdebt <= 0) { pre; luaC_step(L); pos;}; \ - condchangemem(L,pre,pos); } + condchangemem(L,pre,pos,0); } /* more often than not, 'pre'/'pos' are empty */ #define luaC_checkGC(L) luaC_condGC(L,(void)0,(void)0) diff --git a/ltable.c b/ltable.c index f67853675e..b6b1fa1a96 100644 --- a/ltable.c +++ b/ltable.c @@ -910,6 +910,8 @@ static void luaH_newkey (lua_State *L, Table *t, const TValue *key, newcheckedkey(t, key, value); /* insert key in grown table */ } luaC_barrierback(L, obj2gco(t), key); + /* for debugging only: any new key may force an emergency collection */ + condchangemem(L, (void)0, (void)0, 1); } } From 8a3a49250ce4a7e46ec9e90810a61d9f97aece3d Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 6 Jan 2025 14:44:06 -0300 Subject: [PATCH 0990/1145] Detail Small improvement in line-tracing for internal debugging. --- lvm.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lvm.c b/lvm.c index b6b18a69aa..73d7ee4341 100644 --- a/lvm.c +++ b/lvm.c @@ -1175,8 +1175,12 @@ void luaV_execute (lua_State *L, CallInfo *ci) { Instruction i; /* instruction being executed */ vmfetch(); #if 0 - /* low-level line tracing for debugging Lua */ - printf("line: %d\n", luaG_getfuncline(cl->p, pcRel(pc, cl->p))); + { /* low-level line tracing for debugging Lua */ + #include "lopnames.h" + int pcrel = pcRel(pc, cl->p); + printf("line: %d; %s (%d)\n", luaG_getfuncline(cl->p, pcrel), + opnames[GET_OPCODE(i)], pcrel); + } #endif lua_assert(base == ci->func.p + 1); lua_assert(base <= L->top.p && L->top.p <= L->stack_last.p); From 7ca3c40b50b385ead6b8bc4c54de97b61d11a12a Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 10 Jan 2025 13:54:51 -0300 Subject: [PATCH 0991/1145] Another way to compile goto's The compilation of a goto or a label just create an entry and generate boilerplate code for the gotos. As we don't know yet whether it needs a CLOSE, we code a jump followed by a CLOSE, which is then dead code. When a block ends (and then we know for sure whether there are variables that need to be closed), we check the goto's against the labels of that block. When closing a goto against a label, if it needs a CLOSE, the compiler swaps the order of the jump and the CLOSE, making the CLOSE active. --- lparser.c | 187 +++++++++++++++++++++--------------------------- lparser.h | 2 +- lvm.c | 1 + testes/code.lua | 13 +++- testes/db.lua | 2 +- testes/goto.lua | 35 ++++++--- 6 files changed, 119 insertions(+), 121 deletions(-) diff --git a/lparser.c b/lparser.c index 642e43b7ea..6022b38eb0 100644 --- a/lparser.c +++ b/lparser.c @@ -530,18 +530,31 @@ static l_noret jumpscopeerror (LexState *ls, Labeldesc *gt) { /* -** Solves the goto at index 'g' to given 'label' and removes it +** Closes the goto at index 'g' to given 'label' and removes it ** from the list of pending gotos. ** If it jumps into the scope of some variable, raises an error. +** The goto needs a CLOSE if it jumps out of a block with upvalues, +** or out of the scope of some variable and the block has upvalues +** (signaled by parameter 'bup'). */ -static void solvegoto (LexState *ls, int g, Labeldesc *label) { +static void closegoto (LexState *ls, int g, Labeldesc *label, int bup) { int i; + FuncState *fs = ls->fs; Labellist *gl = &ls->dyd->gt; /* list of gotos */ Labeldesc *gt = &gl->arr[g]; /* goto to be resolved */ lua_assert(eqstr(gt->name, label->name)); if (l_unlikely(gt->nactvar < label->nactvar)) /* enter some scope? */ jumpscopeerror(ls, gt); - luaK_patchlist(ls->fs, gt->pc, label->pc); + if (gt->close || + (label->nactvar < gt->nactvar && bup)) { /* needs close? */ + lu_byte stklevel = reglevel(fs, label->nactvar); + /* move jump to CLOSE position */ + fs->f->code[gt->pc + 1] = fs->f->code[gt->pc]; + /* put CLOSE instruction at original position */ + fs->f->code[gt->pc] = CREATE_ABCk(OP_CLOSE, stklevel, 0, 0, 0); + gt->pc++; /* must point to jump instruction */ + } + luaK_patchlist(ls->fs, gt->pc, label->pc); /* goto jumps to label */ for (i = g; i < gl->n - 1; i++) /* remove goto from pending list */ gl->arr[i] = gl->arr[i + 1]; gl->n--; @@ -549,14 +562,14 @@ static void solvegoto (LexState *ls, int g, Labeldesc *label) { /* -** Search for an active label with the given name. +** Search for an active label with the given name, starting at +** index 'ilb' (so that it can searh for all labels in current block +** or all labels in current function). */ -static Labeldesc *findlabel (LexState *ls, TString *name) { - int i; +static Labeldesc *findlabel (LexState *ls, TString *name, int ilb) { Dyndata *dyd = ls->dyd; - /* check labels in current function for a match */ - for (i = ls->fs->firstlabel; i < dyd->label.n; i++) { - Labeldesc *lb = &dyd->label.arr[i]; + for (; ilb < dyd->label.n; ilb++) { + Labeldesc *lb = &dyd->label.arr[ilb]; if (eqstr(lb->name, name)) /* correct label? */ return lb; } @@ -582,29 +595,19 @@ static int newlabelentry (LexState *ls, Labellist *l, TString *name, } -static int newgotoentry (LexState *ls, TString *name, int line, int pc) { - return newlabelentry(ls, &ls->dyd->gt, name, line, pc); -} - - /* -** Solves forward jumps. Check whether new label 'lb' matches any -** pending gotos in current block and solves them. Return true -** if any of the gotos need to close upvalues. +** Create an entry for the goto and the code for it. As it is not known +** at this point whether the goto may need a CLOSE, the code has a jump +** followed by an CLOSE. (As the CLOSE comes after the jump, it is a +** dead instruction; it works as a placeholder.) When the goto is closed +** against a label, if it needs a CLOSE, the two instructions swap +** positions, so that the CLOSE comes before the jump. */ -static int solvegotos (LexState *ls, Labeldesc *lb) { - Labellist *gl = &ls->dyd->gt; - int i = ls->fs->bl->firstgoto; - int needsclose = 0; - while (i < gl->n) { - if (eqstr(gl->arr[i].name, lb->name)) { - needsclose |= gl->arr[i].close; - solvegoto(ls, i, lb); /* will remove 'i' from the list */ - } - else - i++; - } - return needsclose; +static int newgotoentry (LexState *ls, TString *name, int line) { + FuncState *fs = ls->fs; + int pc = luaK_jump(fs); /* create jump */ + luaK_codeABC(fs, OP_CLOSE, 0, 1, 0); /* spaceholder, marked as dead */ + return newlabelentry(ls, &ls->dyd->gt, name, line, pc); } @@ -615,8 +618,7 @@ static int solvegotos (LexState *ls, Labeldesc *lb) { ** a close instruction if necessary. ** Returns true iff it added a close instruction. */ -static int createlabel (LexState *ls, TString *name, int line, - int last) { +static void createlabel (LexState *ls, TString *name, int line, int last) { FuncState *fs = ls->fs; Labellist *ll = &ls->dyd->label; int l = newlabelentry(ls, ll, name, line, luaK_getlabel(fs)); @@ -624,28 +626,37 @@ static int createlabel (LexState *ls, TString *name, int line, /* assume that locals are already out of scope */ ll->arr[l].nactvar = fs->bl->nactvar; } - if (solvegotos(ls, &ll->arr[l])) { /* need close? */ - luaK_codeABC(fs, OP_CLOSE, luaY_nvarstack(fs), 0, 0); - return 1; - } - return 0; } /* -** Adjust pending gotos to outer level of a block. +** Traverse the pending goto's of the finishing block checking whether +** each match some label of that block. Those that do not match are +** "exported" to the outer block, to be solved there. In particular, +** its 'nactvar' is updated with the level of the inner block, +** as the variables of the inner block are now out of scope. */ -static void movegotosout (FuncState *fs, BlockCnt *bl) { - int i; - Labellist *gl = &fs->ls->dyd->gt; - /* correct pending gotos to current block */ - for (i = bl->firstgoto; i < gl->n; i++) { /* for each pending goto */ - Labeldesc *gt = &gl->arr[i]; - /* leaving a variable scope? */ - if (reglevel(fs, gt->nactvar) > reglevel(fs, bl->nactvar)) - gt->close |= bl->upval; /* jump may need a close */ - gt->nactvar = bl->nactvar; /* update goto level */ +static void solvegotos (FuncState *fs, BlockCnt *bl) { + LexState *ls = fs->ls; + Labellist *gl = &ls->dyd->gt; + int outlevel = reglevel(fs, bl->nactvar); /* level outside the block */ + int igt = bl->firstgoto; /* first goto in the finishing block */ + while (igt < gl->n) { /* for each pending goto */ + Labeldesc *gt = &gl->arr[igt]; + /* search for a matching label in the current block */ + Labeldesc *lb = findlabel(ls, gt->name, bl->firstlabel); + if (lb != NULL) /* found a match? */ + closegoto(ls, igt, lb, bl->upval); /* close and remove goto */ + else { /* adjust 'goto' for outer block */ + /* block has variables to be closed and goto escapes the scope of + some variable? */ + if (bl->upval && reglevel(fs, gt->nactvar) > outlevel) + gt->close = 1; /* jump may need a close */ + gt->nactvar = bl->nactvar; /* correct level for outer block */ + igt++; /* go to next goto */ + } } + ls->dyd->label.n = bl->firstlabel; /* remove local labels */ } @@ -682,23 +693,20 @@ static l_noret undefgoto (LexState *ls, Labeldesc *gt) { static void leaveblock (FuncState *fs) { BlockCnt *bl = fs->bl; LexState *ls = fs->ls; - int hasclose = 0; - lu_byte stklevel = reglevel(fs, bl->nactvar); /* level outside the block */ + lu_byte stklevel = reglevel(fs, bl->nactvar); /* level outside block */ + if (bl->previous && bl->upval) /* need a 'close'? */ + luaK_codeABC(fs, OP_CLOSE, stklevel, 0, 0); + fs->freereg = stklevel; /* free registers */ removevars(fs, bl->nactvar); /* remove block locals */ lua_assert(bl->nactvar == fs->nactvar); /* back to level on entry */ if (bl->isloop) /* has to fix pending breaks? */ - hasclose = createlabel(ls, luaS_newliteral(ls->L, "break"), 0, 0); - if (!hasclose && bl->previous && bl->upval) /* still need a 'close'? */ - luaK_codeABC(fs, OP_CLOSE, stklevel, 0, 0); - fs->freereg = stklevel; /* free registers */ - ls->dyd->label.n = bl->firstlabel; /* remove local labels */ - fs->bl = bl->previous; /* current block now is previous one */ - if (bl->previous) /* was it a nested block? */ - movegotosout(fs, bl); /* update pending gotos to enclosing block */ - else { + createlabel(ls, luaS_newliteral(ls->L, "break"), 0, 0); + solvegotos(fs, bl); + if (bl->previous == NULL) { /* was it the last block? */ if (bl->firstgoto < ls->dyd->gt.n) /* still pending gotos? */ undefgoto(ls, &ls->dyd->gt.arr[bl->firstgoto]); /* error */ } + fs->bl = bl->previous; /* current block now is previous one */ } @@ -1446,40 +1454,27 @@ static int cond (LexState *ls) { } -static void gotostat (LexState *ls) { - FuncState *fs = ls->fs; - int line = ls->linenumber; +static void gotostat (LexState *ls, int line) { TString *name = str_checkname(ls); /* label's name */ - Labeldesc *lb = findlabel(ls, name); - if (lb == NULL) /* no label? */ - /* forward jump; will be resolved when the label is declared */ - newgotoentry(ls, name, line, luaK_jump(fs)); - else { /* found a label */ - /* backward jump; will be resolved here */ - int lblevel = reglevel(fs, lb->nactvar); /* label level */ - if (luaY_nvarstack(fs) > lblevel) /* leaving the scope of a variable? */ - luaK_codeABC(fs, OP_CLOSE, lblevel, 0, 0); - /* create jump and link it to the label */ - luaK_patchlist(fs, luaK_jump(fs), lb->pc); - } + newgotoentry(ls, name, line); } /* ** Break statement. Semantically equivalent to "goto break". */ -static void breakstat (LexState *ls) { - int line = ls->linenumber; +static void breakstat (LexState *ls, int line) { luaX_next(ls); /* skip break */ - newgotoentry(ls, luaS_newliteral(ls->L, "break"), line, luaK_jump(ls->fs)); + newgotoentry(ls, luaS_newliteral(ls->L, "break"), line); } /* -** Check whether there is already a label with the given 'name'. +** Check whether there is already a label with the given 'name' at +** current function. */ static void checkrepeated (LexState *ls, TString *name) { - Labeldesc *lb = findlabel(ls, name); + Labeldesc *lb = findlabel(ls, name, ls->fs->firstlabel); if (l_unlikely(lb != NULL)) { /* already defined? */ const char *msg = "label '%s' already defined on line %d"; msg = luaO_pushfstring(ls->L, msg, getstr(name), lb->line); @@ -1669,38 +1664,16 @@ static void forstat (LexState *ls, int line) { static void test_then_block (LexState *ls, int *escapelist) { /* test_then_block -> [IF | ELSEIF] cond THEN block */ - BlockCnt bl; FuncState *fs = ls->fs; - expdesc v; - int jf; /* instruction to skip 'then' code (if condition is false) */ + int condtrue; luaX_next(ls); /* skip IF or ELSEIF */ - expr(ls, &v); /* read condition */ + condtrue = cond(ls); /* read condition */ checknext(ls, TK_THEN); - if (ls->t.token == TK_BREAK) { /* 'if x then break' ? */ - int line = ls->linenumber; - luaK_goiffalse(ls->fs, &v); /* will jump if condition is true */ - luaX_next(ls); /* skip 'break' */ - enterblock(fs, &bl, 0); /* must enter block before 'goto' */ - newgotoentry(ls, luaS_newliteral(ls->L, "break"), line, v.t); - while (testnext(ls, ';')) {} /* skip semicolons */ - if (block_follow(ls, 0)) { /* jump is the entire block? */ - leaveblock(fs); - return; /* and that is it */ - } - else /* must skip over 'then' part if condition is false */ - jf = luaK_jump(fs); - } - else { /* regular case (not a break) */ - luaK_goiftrue(ls->fs, &v); /* skip over block if condition is false */ - enterblock(fs, &bl, 0); - jf = v.f; - } - statlist(ls); /* 'then' part */ - leaveblock(fs); + block(ls); /* 'then' part */ if (ls->t.token == TK_ELSE || ls->t.token == TK_ELSEIF) /* followed by 'else'/'elseif'? */ luaK_concat(fs, escapelist, luaK_jump(fs)); /* must jump over it */ - luaK_patchtohere(fs, jf); + luaK_patchtohere(fs, condtrue); } @@ -1928,12 +1901,12 @@ static void statement (LexState *ls) { break; } case TK_BREAK: { /* stat -> breakstat */ - breakstat(ls); + breakstat(ls, line); break; } case TK_GOTO: { /* stat -> 'goto' NAME */ luaX_next(ls); /* skip 'goto' */ - gotostat(ls); + gotostat(ls, line); break; } default: { /* stat -> func | assignment */ diff --git a/lparser.h b/lparser.h index 589befdb76..a8004fa0ce 100644 --- a/lparser.h +++ b/lparser.h @@ -112,7 +112,7 @@ typedef struct Labeldesc { int pc; /* position in code */ int line; /* line where it appeared */ lu_byte nactvar; /* number of active variables in that position */ - lu_byte close; /* goto that escapes upvalues */ + lu_byte close; /* true for goto that escapes upvalues */ } Labeldesc; diff --git a/lvm.c b/lvm.c index 73d7ee4341..074ee718ec 100644 --- a/lvm.c +++ b/lvm.c @@ -1590,6 +1590,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { } vmcase(OP_CLOSE) { StkId ra = RA(i); + lua_assert(!GETARG_B(i)); /* 'close must be alive */ Protect(luaF_close(L, ra, LUA_OK, 1)); vmbreak; } diff --git a/testes/code.lua b/testes/code.lua index 08b3e23faa..50ce7392f3 100644 --- a/testes/code.lua +++ b/testes/code.lua @@ -412,13 +412,22 @@ checkequal(function (l) local a; return 0 <= a and a <= l end, function (l) local a; return not (not(a >= 0) or not(a <= l)) end) --- if-break optimizations check(function (a, b) while a do if b then break else a = a + 1 end end end, -'TEST', 'JMP', 'TEST', 'JMP', 'ADDI', 'MMBINI', 'JMP', 'RETURN0') +'TEST', 'JMP', 'TEST', 'JMP', 'JMP', 'CLOSE', 'JMP', 'ADDI', 'MMBINI', 'JMP', 'RETURN0') + +check(function () + do + goto exit -- don't need to close + local x = nil + goto exit -- must close + end + ::exit:: + end, 'JMP', 'CLOSE', 'LOADNIL', 'TBC', + 'CLOSE', 'JMP', 'CLOSE', 'RETURN') checkequal(function () return 6 or true or nil end, function () return k6 or kTrue or kNil end) diff --git a/testes/db.lua b/testes/db.lua index fc0db9eae0..75730d27b0 100644 --- a/testes/db.lua +++ b/testes/db.lua @@ -128,7 +128,7 @@ then else a=2 end -]], {2,3,4,7}) +]], {2,4,7}) test([[ diff --git a/testes/goto.lua b/testes/goto.lua index 4ac6d7d089..103cccef52 100644 --- a/testes/goto.lua +++ b/testes/goto.lua @@ -250,21 +250,36 @@ assert(testG(3) == "3") assert(testG(4) == 5) assert(testG(5) == 10) -do - -- if x back goto out of scope of upvalue - local X +do -- test goto's around to-be-closed variable + + -- set 'var' and return an object that will reset 'var' when + -- it goes out of scope + local function newobj (var) + _ENV[var] = true + return setmetatable({}, {__close = function () + _ENV[var] = nil + end}) + end + goto L1 - ::L2:: goto L3 + ::L4:: assert(not X); goto L5 -- varX dead here - ::L1:: do - local a = setmetatable({}, {__close = function () X = true end}) - assert(X == nil) - if a then goto L2 end -- jumping back out of scope of 'a' - end + ::L1:: + local varX = newobj("X") + assert(X); goto L2 -- varX alive here - ::L3:: assert(X == true) -- checks that 'a' was correctly closed + ::L3:: + assert(X); goto L4 -- varX alive here + + ::L2:: assert(X); goto L3 -- varX alive here + + ::L5:: -- return end + + + +foo() -------------------------------------------------------------------------------- From 915c29f8bd0d4b0435a4b51a6c7913f5e170d09e Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 10 Jan 2025 15:11:54 -0300 Subject: [PATCH 0992/1145] Improvements in the manual Plus details --- lapi.c | 3 +-- ldo.c | 2 +- lstate.h | 2 +- manual/manual.of | 34 ++++++++++++++++++++-------------- 4 files changed, 23 insertions(+), 18 deletions(-) diff --git a/lapi.c b/lapi.c index 01abfc15ad..4411cb2962 100644 --- a/lapi.c +++ b/lapi.c @@ -671,9 +671,8 @@ static int auxgetstr (lua_State *L, const TValue *t, const char *k) { lu_byte tag; TString *str = luaS_new(L, k); luaV_fastget(t, str, s2v(L->top.p), luaH_getstr, tag); - if (!tagisempty(tag)) { + if (!tagisempty(tag)) api_incr_top(L); - } else { setsvalue2s(L, L->top.p, str); api_incr_top(L); diff --git a/ldo.c b/ldo.c index f825d95950..009bf47ad2 100644 --- a/ldo.c +++ b/ldo.c @@ -367,7 +367,7 @@ void luaD_shrinkstack (lua_State *L) { luaD_reallocstack(L, nsize, 0); /* ok if that fails */ } else /* don't change stack */ - condmovestack(L,{},{}); /* (change only for debugging) */ + condmovestack(L,(void)0,(void)0); /* (change only for debugging) */ luaE_shrinkCI(L); /* shrink CI list */ } diff --git a/lstate.h b/lstate.h index 1c81b6edc9..e95c72880e 100644 --- a/lstate.h +++ b/lstate.h @@ -186,7 +186,7 @@ typedef struct stringtable { */ struct CallInfo { StkIdRel func; /* function index in the stack */ - StkIdRel top; /* top for this function */ + StkIdRel top; /* top for this function */ struct CallInfo *previous, *next; /* dynamic call link */ union { struct { /* only for Lua functions */ diff --git a/manual/manual.of b/manual/manual.of index a441cea13a..bb95148ad6 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -1428,7 +1428,7 @@ except inside nested functions. A goto can jump to any visible label as long as it does not enter into the scope of a local variable. A label should not be declared -where a label with the same name is visible, +where a previous label with the same name is visible, even if this other label has been declared in an enclosing block. The @Rw{break} statement terminates the execution of a @@ -3835,7 +3835,7 @@ This macro may evaluate its arguments more than once. Converts the number at acceptable index @id{idx} to a string and puts the result in @id{buff}. -The buffer must have a size of at least @Lid{LUA_N2SBUFFSZ} bytes. +The buffer must have a size of at least @defid{LUA_N2SBUFFSZ} bytes. The conversion follows a non-specified format @see{coercion}. The function returns the number of bytes written to the buffer (including the final zero), @@ -3997,25 +3997,22 @@ Lua will call @id{falloc} before raising the error. Pushes onto the stack a formatted string and returns a pointer to this string @see{constchar}. -It is similar to the @ANSI{sprintf}, -but has two important differences. -First, -you do not have to allocate space for the result; -the result is a Lua string and Lua takes care of memory allocation -(and deallocation, through garbage collection). -Second, -the conversion specifiers are quite restricted. -There are no flags, widths, or precisions. -The conversion specifiers can only be +The result is a copy of @id{fmt} with +each @emph{conversion specifier} replaced by its respective +extra argument. +A conversion specifier can be @Char{%%} (inserts the character @Char{%}), @Char{%s} (inserts a zero-terminated string, with no size restrictions), @Char{%f} (inserts a @Lid{lua_Number}), @Char{%I} (inserts a @Lid{lua_Integer}), -@Char{%p} (inserts a pointer), +@Char{%p} (inserts a void pointer), @Char{%d} (inserts an @T{int}), @Char{%c} (inserts an @T{int} as a one-byte character), and @Char{%U} (inserts an @T{unsigned long} as a @x{UTF-8} byte sequence). +Every occurrence of @Char{%} in the string @id{fmt} +must form a valid conversion specifier. + } @APIEntry{void lua_pushglobaltable (lua_State *L);| @@ -4413,7 +4410,7 @@ for the @Q{newindex} event @see{metatable}. @APIEntry{void lua_settop (lua_State *L, int index);| @apii{?,?,e} -Accepts any index, @N{or 0}, +Receives any acceptable stack index, @N{or 0}, and sets the stack top to this index. If the new top is greater than the old one, then the new elements are filled with @nil. @@ -9427,6 +9424,15 @@ Moreover, there were some changes in the parameters themselves. @itemize{ +@item{ +In @Lid{lua_call} and related functions, +the maximum value for the number of required results +(@id{nresults}) is 250. +If you really need a larger value, +use @Lid{LUA_MULTRET} and then adjust the stack size. +Previously, this limit was unspecified. +} + @item{ @Lid{lua_newstate} has a third parameter, a seed for the hashing of strings. From 429761d7d29226dd0c220de9fdc7c28ea54d95c0 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 10 Jan 2025 15:14:01 -0300 Subject: [PATCH 0993/1145] New optimization option for testing Using gcc's option '-Og' (instead of '-O0') for testing/debugging. --- makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/makefile b/makefile index e3f8cf698c..64dee501ec 100644 --- a/makefile +++ b/makefile @@ -63,7 +63,7 @@ CWARNS= $(CWARNSCPP) $(CWARNSC) $(CWARNGCC) # ASAN_OPTIONS="detect_invalid_pointer_pairs=2". # -fsanitize=undefined # -fsanitize=pointer-subtract -fsanitize=address -fsanitize=pointer-compare -# TESTS= -DLUA_USER_H='"ltests.h"' -O0 -g +# TESTS= -DLUA_USER_H='"ltests.h"' -Og -g LOCAL = $(TESTS) $(CWARNS) From 4b107a87760ee5f85185a904c9088ca476b94be5 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 10 Jan 2025 15:27:17 -0300 Subject: [PATCH 0994/1145] Details in lparser.c Added comments so that all braces pair correctly. (The parser has several instances of unmatched braces as characters ('{' or '}'), which hinders matching regular braces in the code.) --- lparser.c | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/lparser.c b/lparser.c index 6022b38eb0..036f133ba7 100644 --- a/lparser.c +++ b/lparser.c @@ -797,10 +797,11 @@ static void close_func (LexState *ls) { } - -/*============================================================*/ -/* GRAMMAR RULES */ -/*============================================================*/ +/* +** {====================================================================== +** GRAMMAR RULES +** ======================================================================= +*/ /* @@ -974,15 +975,15 @@ static void constructor (LexState *ls, expdesc *t) { init_exp(t, VNONRELOC, fs->freereg); /* table will be at stack top */ luaK_reserveregs(fs, 1); init_exp(&cc.v, VVOID, 0); /* no value (yet) */ - checknext(ls, '{'); + checknext(ls, '{' /*}*/); cc.maxtostore = maxtostore(fs); do { lua_assert(cc.v.k == VVOID || cc.tostore > 0); - if (ls->t.token == '}') break; + if (ls->t.token == /*{*/ '}') break; closelistfield(fs, &cc); field(ls, &cc); } while (testnext(ls, ',') || testnext(ls, ';')); - check_match(ls, '}', '{', line); + check_match(ls, /*{*/ '}', '{' /*}*/, line); lastlistfield(fs, &cc); luaK_settablesize(fs, pc, t->u.info, cc.na, cc.nh); } @@ -1080,7 +1081,7 @@ static void funcargs (LexState *ls, expdesc *f) { check_match(ls, ')', '(', line); break; } - case '{': { /* funcargs -> constructor */ + case '{' /*}*/: { /* funcargs -> constructor */ constructor(ls, &args); break; } @@ -1167,7 +1168,7 @@ static void suffixedexp (LexState *ls, expdesc *v) { funcargs(ls, v); break; } - case '(': case TK_STRING: case '{': { /* funcargs */ + case '(': case TK_STRING: case '{' /*}*/: { /* funcargs */ luaK_exp2nextreg(fs, v); funcargs(ls, v); break; @@ -1215,7 +1216,7 @@ static void simpleexp (LexState *ls, expdesc *v) { init_exp(v, VVARARG, luaK_codeABC(fs, OP_VARARG, 0, 0, 1)); break; } - case '{': { /* constructor */ + case '{' /*}*/: { /* constructor */ constructor(ls, v); return; } @@ -1922,6 +1923,8 @@ static void statement (LexState *ls) { /* }====================================================================== */ +/* }====================================================================== */ + /* ** compiles the main function, which is a regular vararg function with an From 10e931da82268a9d190c17a9bdb9b1a4b48c2947 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 13 Jan 2025 11:23:07 -0300 Subject: [PATCH 0995/1145] Error "break outside loop" made a syntax error Syntax errors are easier to handle than semantic errors. --- lparser.c | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/lparser.c b/lparser.c index 036f133ba7..83e341ed94 100644 --- a/lparser.c +++ b/lparser.c @@ -52,7 +52,7 @@ typedef struct BlockCnt { int firstgoto; /* index of first pending goto in this block */ lu_byte nactvar; /* # active locals outside the block */ lu_byte upval; /* true if some variable in the block is an upvalue */ - lu_byte isloop; /* true if 'block' is a loop */ + lu_byte isloop; /* 1 if 'block' is a loop; 2 if it has pending breaks */ lu_byte insidetbc; /* true if inside the scope of a to-be-closed var. */ } BlockCnt; @@ -677,15 +677,10 @@ static void enterblock (FuncState *fs, BlockCnt *bl, lu_byte isloop) { ** generates an error for an undefined 'goto'. */ static l_noret undefgoto (LexState *ls, Labeldesc *gt) { - const char *msg; - if (eqstr(gt->name, luaS_newliteral(ls->L, "break"))) { - msg = "break outside loop at line %d"; - msg = luaO_pushfstring(ls->L, msg, gt->line); - } - else { - msg = "no visible label '%s' for at line %d"; - msg = luaO_pushfstring(ls->L, msg, getstr(gt->name), gt->line); - } + const char *msg = "no visible label '%s' for at line %d"; + msg = luaO_pushfstring(ls->L, msg, getstr(gt->name), gt->line); + /* breaks are checked when created, cannot be undefined */ + lua_assert(!eqstr(gt->name, luaS_newliteral(ls->L, "break"))); luaK_semerror(ls, msg); } @@ -699,7 +694,7 @@ static void leaveblock (FuncState *fs) { fs->freereg = stklevel; /* free registers */ removevars(fs, bl->nactvar); /* remove block locals */ lua_assert(bl->nactvar == fs->nactvar); /* back to level on entry */ - if (bl->isloop) /* has to fix pending breaks? */ + if (bl->isloop == 2) /* has to fix pending breaks? */ createlabel(ls, luaS_newliteral(ls->L, "break"), 0, 0); solvegotos(fs, bl); if (bl->previous == NULL) { /* was it the last block? */ @@ -1465,6 +1460,14 @@ static void gotostat (LexState *ls, int line) { ** Break statement. Semantically equivalent to "goto break". */ static void breakstat (LexState *ls, int line) { + BlockCnt *bl; /* to look for an enclosing loop */ + for (bl = ls->fs->bl; bl != NULL; bl = bl->previous) { + if (bl->isloop) /* found one? */ + goto ok; + } + luaX_syntaxerror(ls, "break outside loop"); + ok: + bl->isloop = 2; /* signal that block has pending breaks */ luaX_next(ls); /* skip break */ newgotoentry(ls, luaS_newliteral(ls->L, "break"), line); } From 3cdd49c94a8feed94853ba3a6adaa556fb34fd8d Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 14 Jan 2025 16:24:46 -0300 Subject: [PATCH 0996/1145] Fixed conversion warnings from clang Plus some other details. (Option '-Wuninitialized' was removed from the makefile because it is already enabled by -Wall.) --- lcode.c | 2 +- llex.c | 7 ++++++- lmem.h | 4 ++-- lobject.c | 1 + lparser.c | 2 +- ltable.c | 4 ++-- makefile | 1 - manual/manual.of | 7 +++---- testes/libs/makefile | 2 +- 9 files changed, 17 insertions(+), 13 deletions(-) diff --git a/lcode.c b/lcode.c index 641f0d09cc..8c04d8ab16 100644 --- a/lcode.c +++ b/lcode.c @@ -1439,7 +1439,7 @@ static void finishbinexpval (FuncState *fs, expdesc *e1, expdesc *e2, e1->u.info = pc; e1->k = VRELOC; /* all those operations are relocatable */ luaK_fixline(fs, line); - luaK_codeABCk(fs, mmop, v1, v2, event, flip); /* to call metamethod */ + luaK_codeABCk(fs, mmop, v1, v2, cast_int(event), flip); /* metamethod */ luaK_fixline(fs, line); } diff --git a/llex.c b/llex.c index 3518f0dab6..1c4227ca4c 100644 --- a/llex.c +++ b/llex.c @@ -349,9 +349,14 @@ static int readhexaesc (LexState *ls) { } +/* +** When reading a UTF-8 escape sequence, save everything to the buffer +** for error reporting in case of errors; 'i' counts the number of +** saved characters, so that they can be removed if case of success. +*/ static unsigned long readutf8esc (LexState *ls) { unsigned long r; - int i = 4; /* chars to be removed: '\', 'u', '{', and first digit */ + int i = 4; /* number of chars to be removed: start with #"\u{X" */ save_and_next(ls); /* skip 'u' */ esccheck(ls, ls->current == '{', "missing '{'"); r = cast_ulong(gethexa(ls)); /* must have at least one digit */ diff --git a/lmem.h b/lmem.h index 204ce3bcae..083585920d 100644 --- a/lmem.h +++ b/lmem.h @@ -39,11 +39,11 @@ ** Computes the minimum between 'n' and 'MAX_SIZET/sizeof(t)', so that ** the result is not larger than 'n' and cannot overflow a 'size_t' ** when multiplied by the size of type 't'. (Assumes that 'n' is an -** 'int' or 'unsigned int' and that 'int' is not larger than 'size_t'.) +** 'int' and that 'int' is not larger than 'size_t'.) */ #define luaM_limitN(n,t) \ ((cast_sizet(n) <= MAX_SIZET/sizeof(t)) ? (n) : \ - cast_uint((MAX_SIZET/sizeof(t)))) + cast_int((MAX_SIZET/sizeof(t)))) /* diff --git a/lobject.c b/lobject.c index 97dacaf514..c0fd182f13 100644 --- a/lobject.c +++ b/lobject.c @@ -194,6 +194,7 @@ void luaO_arith (lua_State *L, int op, const TValue *p1, const TValue *p2, lu_byte luaO_hexavalue (int c) { + lua_assert(lisxdigit(c)); if (lisdigit(c)) return cast_byte(c - '0'); else return cast_byte((ltolower(c) - 'a') + 10); } diff --git a/lparser.c b/lparser.c index 83e341ed94..380e45f58c 100644 --- a/lparser.c +++ b/lparser.c @@ -405,7 +405,7 @@ static int searchvar (FuncState *fs, TString *n, expdesc *var) { init_exp(var, VCONST, fs->firstlocal + i); else /* real variable */ init_var(fs, var, i); - return var->k; + return cast_int(var->k); } } return -1; /* not found */ diff --git a/ltable.c b/ltable.c index b6b1fa1a96..122b7f1756 100644 --- a/ltable.c +++ b/ltable.c @@ -96,7 +96,7 @@ typedef union { ** between 2^MAXHBITS and the maximum size such that, measured in bytes, ** it fits in a 'size_t'. */ -#define MAXHSIZE luaM_limitN(1u << MAXHBITS, Node) +#define MAXHSIZE luaM_limitN(1 << MAXHBITS, Node) /* @@ -598,7 +598,7 @@ static void setnodevector (lua_State *L, Table *t, unsigned size) { else { int i; int lsize = luaO_ceillog2(size); - if (lsize > MAXHBITS || (1u << lsize) > MAXHSIZE) + if (lsize > MAXHBITS || (1 << lsize) > MAXHSIZE) luaG_runerror(L, "table overflow"); size = twoto(lsize); if (lsize < LIMFORLAST) /* no 'lastfree' field? */ diff --git a/makefile b/makefile index 64dee501ec..8506e93c20 100644 --- a/makefile +++ b/makefile @@ -15,7 +15,6 @@ CWARNSCPP= \ -Wdouble-promotion \ -Wmissing-declarations \ -Wconversion \ - -Wuninitialized \ -Wstrict-overflow=2 \ # the next warnings might be useful sometimes, # but usually they generate too much noise diff --git a/manual/manual.of b/manual/manual.of index bb95148ad6..77e37de383 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -3848,7 +3848,6 @@ or zero if the value at @id{idx} is not a number. Calls a function (or a callable object) in protected mode. - Both @id{nargs} and @id{nresults} have the same meaning as in @Lid{lua_call}. If there are no errors during the call, @@ -3998,9 +3997,9 @@ Lua will call @id{falloc} before raising the error. Pushes onto the stack a formatted string and returns a pointer to this string @see{constchar}. The result is a copy of @id{fmt} with -each @emph{conversion specifier} replaced by its respective -extra argument. -A conversion specifier can be +each @emph{conversion specifier} replaced by a string representation +of its respective extra argument. +A conversion specifier (and its corresponding extra argument) can be @Char{%%} (inserts the character @Char{%}), @Char{%s} (inserts a zero-terminated string, with no size restrictions), @Char{%f} (inserts a @Lid{lua_Number}), diff --git a/testes/libs/makefile b/testes/libs/makefile index 9c0c4e3f7f..4e7f965e99 100644 --- a/testes/libs/makefile +++ b/testes/libs/makefile @@ -5,7 +5,7 @@ LUA_DIR = ../../ CC = gcc # compilation should generate Dynamic-Link Libraries -CFLAGS = -Wall -std=gnu99 -O2 -I$(LUA_DIR) -fPIC -shared +CFLAGS = -Wall -std=c99 -O2 -I$(LUA_DIR) -fPIC -shared # libraries used by the tests all: lib1.so lib11.so lib2.so lib21.so lib2-v2.so From 2d8d5c74b5ef3d333314feede0165df7c3d13811 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 16 Jan 2025 11:51:16 -0300 Subject: [PATCH 0997/1145] Details New year (2024->2025), typos in comments --- ldebug.c | 2 +- ldo.c | 4 ++-- lgc.c | 4 ++-- llex.h | 2 +- llimits.h | 2 +- lmathlib.c | 2 +- lparser.h | 2 +- ltable.c | 8 ++++---- lua.c | 2 +- lua.h | 4 ++-- lundump.c | 2 +- lvm.c | 4 ++-- lvm.h | 2 +- manual/2html | 2 +- testes/all.lua | 7 +++---- 15 files changed, 24 insertions(+), 25 deletions(-) diff --git a/ldebug.c b/ldebug.c index 09ec197c42..af3b758334 100644 --- a/ldebug.c +++ b/ldebug.c @@ -898,7 +898,7 @@ int luaG_tracecall (lua_State *L) { if (ci->u.l.savedpc == p->code) { /* first instruction (not resuming)? */ if (p->flag & PF_ISVARARG) return 0; /* hooks will start at VARARGPREP instruction */ - else if (!(ci->callstatus & CIST_HOOKYIELD)) /* not yieded? */ + else if (!(ci->callstatus & CIST_HOOKYIELD)) /* not yielded? */ luaD_hookcall(L, ci); /* check 'call' hook */ } return 1; /* keep 'trap' on */ diff --git a/ldo.c b/ldo.c index 009bf47ad2..fb9df5d392 100644 --- a/ldo.c +++ b/ldo.c @@ -236,7 +236,7 @@ static void correctstack (lua_State *L, StkId oldstack) { #else /* -** Alternatively, we can use the old address after the dealocation. +** Alternatively, we can use the old address after the deallocation. ** That is not strict ISO C, but seems to work fine everywhere. */ @@ -485,7 +485,7 @@ static unsigned tryfuncTM (lua_State *L, StkId func, unsigned status) { } -/* Generic case for 'moveresult */ +/* Generic case for 'moveresult' */ l_sinline void genmoveresults (lua_State *L, StkId res, int nres, int wanted) { StkId firstresult = L->top.p - nres; /* index of first result */ diff --git a/lgc.c b/lgc.c index 3cdfd0064c..1e9f75698c 100644 --- a/lgc.c +++ b/lgc.c @@ -242,7 +242,7 @@ static int iscleared (global_State *g, const GCObject *o) { ** incremental sweep phase, it clears the black object to white (sweep ** it) to avoid other barrier calls for this same object. (That cannot ** be done is generational mode, as its sweep does not distinguish -** whites from deads.) +** white from dead.) */ void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v) { global_State *g = G(L); @@ -1089,7 +1089,7 @@ void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt) { ** GCmarked: number of bytes that became old since last major collection. ** GCmajorminor: number of bytes marked in last major collection. ** * KGC_GENMAJOR -** GCmarked: number of bytes that became old sinse last major collection. +** GCmarked: number of bytes that became old since last major collection. ** GCmajorminor: number of bytes marked in last major collection. */ diff --git a/llex.h b/llex.h index 389d2f8635..c3500ef6a8 100644 --- a/llex.h +++ b/llex.h @@ -59,7 +59,7 @@ typedef struct Token { } Token; -/* state of the lexer plus state of the parser when shared by all +/* state of the scanner plus state of the parser when shared by all functions */ typedef struct LexState { int current; /* current character (charint) */ diff --git a/llimits.h b/llimits.h index 6cf35e0cf7..d98171ae6b 100644 --- a/llimits.h +++ b/llimits.h @@ -159,7 +159,7 @@ typedef LUAI_UACINT l_uacInt; #define cast_st2S(sz) ((lua_Integer)(sz)) /* Cast a ptrdiff_t to size_t, when it is known that the minuend -** comes from the subtraend (the base) +** comes from the subtrahend (the base) */ #define ct_diff2sz(df) ((size_t)(df)) diff --git a/lmathlib.c b/lmathlib.c index f8b24d1d01..c7418e69ec 100644 --- a/lmathlib.c +++ b/lmathlib.c @@ -106,7 +106,7 @@ static int math_floor (lua_State *L) { static int math_ceil (lua_State *L) { if (lua_isinteger(L, 1)) - lua_settop(L, 1); /* integer is its own ceil */ + lua_settop(L, 1); /* integer is its own ceiling */ else { lua_Number d = l_mathop(ceil)(luaL_checknumber(L, 1)); pushnumint(L, d); diff --git a/lparser.h b/lparser.h index a8004fa0ce..a3063569c7 100644 --- a/lparser.h +++ b/lparser.h @@ -32,7 +32,7 @@ typedef enum { VKFLT, /* floating constant; nval = numerical float value */ VKINT, /* integer constant; ival = numerical integer value */ VKSTR, /* string constant; strval = TString address; - (string is fixed by the lexer) */ + (string is fixed by the scanner) */ VNONRELOC, /* expression has its value in a fixed register; info = result register */ VLOCAL, /* local variable; var.ridx = register index; diff --git a/ltable.c b/ltable.c index 122b7f1756..8df9a4fbfe 100644 --- a/ltable.c +++ b/ltable.c @@ -123,7 +123,7 @@ typedef union { /* ** Common hash part for tables with empty hash parts. That allows all -** tables to have a hash part, avoding an extra check ("is there a hash +** tables to have a hash part, avoiding an extra check ("is there a hash ** part?") when indexing. Its sole node has an empty value and a key ** (DEADKEY, NULL) that is different from any valid TValue. */ @@ -699,7 +699,7 @@ static void clearNewSlice (Table *t, unsigned oldasize, unsigned newasize) { ** into the table, initializes the new part of the array (if any) with ** nils and reinserts the elements of the old hash back into the new ** parts of the table. -** Note that if the new size for the arry part ('newasize') is equal to +** Note that if the new size for the array part ('newasize') is equal to ** the old one ('oldasize'), this function will do nothing with that ** part. */ @@ -774,7 +774,7 @@ static void rehash (lua_State *L, Table *t, const TValue *ek) { nsize = ct.total - ct.na; if (ct.deleted) { /* table has deleted entries? */ /* insertion-deletion-insertion: give hash some extra size to - avoid constant resizings */ + avoid repeated resizings */ nsize += nsize >> 2; } /* resize the table to new computed sizes */ @@ -1300,7 +1300,7 @@ lua_Unsigned luaH_getn (Table *t) { return newhint(t, binsearch(t, limit, asize)); } } - /* last element non empty; set a hint to speed up findind that again */ + /* last element non empty; set a hint to speed up finding that again */ /* (keys in the hash part cannot be hints) */ *lenhint(t) = asize; } diff --git a/lua.c b/lua.c index ea6141bb33..64d391609a 100644 --- a/lua.c +++ b/lua.c @@ -497,7 +497,7 @@ static void lua_freeline (char *line) { static void lua_initreadline (lua_State *L) { void *lib = dlopen(LUA_READLINELIB, RTLD_NOW | RTLD_LOCAL); if (lib == NULL) - lua_warning(L, "library '" LUA_READLINELIB "'not found", 0); + lua_warning(L, "library '" LUA_READLINELIB "' not found", 0); else { const char **name = cast(const char**, dlsym(lib, "rl_readline_name")); if (name != NULL) diff --git a/lua.h b/lua.h index aefa3b8c3d..76068fdcc7 100644 --- a/lua.h +++ b/lua.h @@ -13,7 +13,7 @@ #include -#define LUA_COPYRIGHT LUA_RELEASE " Copyright (C) 1994-2024 Lua.org, PUC-Rio" +#define LUA_COPYRIGHT LUA_RELEASE " Copyright (C) 1994-2025 Lua.org, PUC-Rio" #define LUA_AUTHORS "R. Ierusalimschy, L. H. de Figueiredo, W. Celes" @@ -528,7 +528,7 @@ struct lua_Debug { /****************************************************************************** -* Copyright (C) 1994-2024 Lua.org, PUC-Rio. +* Copyright (C) 1994-2025 Lua.org, PUC-Rio. * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the diff --git a/lundump.c b/lundump.c index 4d6e8bd2fd..fd5a2ca6eb 100644 --- a/lundump.c +++ b/lundump.c @@ -63,7 +63,7 @@ static void loadBlock (LoadState *S, void *b, size_t size) { static void loadAlign (LoadState *S, unsigned align) { unsigned padding = align - cast_uint(S->offset % align); - if (padding < align) { /* apd == align means no padding */ + if (padding < align) { /* (padding == align) means no padding */ lua_Integer paddingContent; loadBlock(S, &paddingContent, padding); lua_assert(S->offset % align == 0); diff --git a/lvm.c b/lvm.c index 074ee718ec..f0e73f9bb6 100644 --- a/lvm.c +++ b/lvm.c @@ -127,8 +127,8 @@ int luaV_flttointeger (lua_Number n, lua_Integer *p, F2Imod mode) { lua_Number f = l_floor(n); if (n != f) { /* not an integral value? */ if (mode == F2Ieq) return 0; /* fails if mode demands integral value */ - else if (mode == F2Iceil) /* needs ceil? */ - f += 1; /* convert floor to ceil (remember: n != f) */ + else if (mode == F2Iceil) /* needs ceiling? */ + f += 1; /* convert floor to ceiling (remember: n != f) */ } return lua_numbertointeger(f, p); } diff --git a/lvm.h b/lvm.h index c88985599a..be7b9cb0ea 100644 --- a/lvm.h +++ b/lvm.h @@ -43,7 +43,7 @@ typedef enum { F2Ieq, /* no rounding; accepts only integral values */ F2Ifloor, /* takes the floor of the number */ - F2Iceil /* takes the ceil of the number */ + F2Iceil /* takes the ceiling of the number */ } F2Imod; diff --git a/manual/2html b/manual/2html index 59bb4578a4..ac5ea04351 100755 --- a/manual/2html +++ b/manual/2html @@ -30,7 +30,7 @@ by Roberto Ierusalimschy, Luiz Henrique de Figueiredo, Waldemar Celes

Copyright -© 2024 Lua.org, PUC-Rio. All rights reserved. +© 2025 Lua.org, PUC-Rio. All rights reserved.


diff --git a/testes/all.lua b/testes/all.lua index 3c1ff5c71a..4ffa9efee1 100644 --- a/testes/all.lua +++ b/testes/all.lua @@ -28,10 +28,9 @@ _nomsg = rawget(_G, "_nomsg") or false local usertests = rawget(_G, "_U") if usertests then - -- tests for sissies ;) Avoid problems - _soft = true - _port = true - _nomsg = true + _soft = true -- avoid tests that take too long + _port = true -- avoid non-portable tests + _nomsg = true -- avoid messages about tests not performed end -- tests should require debug when needed From 664bda02ba4bd167728a2acbe658cc4a9dd9b0b5 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 16 Jan 2025 16:07:39 -0300 Subject: [PATCH 0998/1145] fixing 'lua_status' in panic. 'luaD_throw' may call 'luaE_resetthread', which returns an error code but clears 'L->status'; so, 'luaD_throw' should set that status again. --- ldo.c | 1 + ltests.c | 10 ++++++++-- testes/api.lua | 19 +++++++++++++++++++ 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/ldo.c b/ldo.c index fb9df5d392..994ad6f0fb 100644 --- a/ldo.c +++ b/ldo.c @@ -133,6 +133,7 @@ l_noret luaD_throw (lua_State *L, int errcode) { else { /* thread has no error handler */ global_State *g = G(L); errcode = luaE_resetthread(L, errcode); /* close all upvalues */ + L->status = cast_byte(errcode); if (g->mainthread->errorJmp) { /* main thread has a handler? */ setobjs2s(L, g->mainthread->top.p++, L->top.p - 1); /* copy error obj. */ luaD_throw(g->mainthread, errcode); /* re-throw in main thread */ diff --git a/ltests.c b/ltests.c index 6c77703c4f..83d08ac880 100644 --- a/ltests.c +++ b/ltests.c @@ -1367,7 +1367,7 @@ static int checkpanic (lua_State *L) { b.L = L; L1 = lua_newstate(f, ud, 0); /* create new state */ if (L1 == NULL) { /* error? */ - lua_pushnil(L); + lua_pushstring(L, MEMERRMSG); return 1; } lua_atpanic(L1, panicback); /* set its panic function */ @@ -1507,7 +1507,7 @@ static int getindex_aux (lua_State *L, lua_State *L1, const char **pc) { static const char *const statcodes[] = {"OK", "YIELD", "ERRRUN", - "ERRSYNTAX", MEMERRMSG, "ERRGCMM", "ERRERR"}; + "ERRSYNTAX", MEMERRMSG, "ERRERR"}; /* ** Avoid these stat codes from being collected, to avoid possible @@ -1806,6 +1806,12 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) { int level = getnum; luaL_traceback(L1, L1, msg, level); } + else if EQ("threadstatus") { + lua_pushstring(L1, statcodes[lua_status(L1)]); + } + else if EQ("alloccount") { + l_memcontrol.countlimit = cast_uint(getnum); + } else if EQ("return") { int n = getnum; if (L1 != L) { diff --git a/testes/api.lua b/testes/api.lua index b7e34f7fc5..21f703fd17 100644 --- a/testes/api.lua +++ b/testes/api.lua @@ -416,6 +416,10 @@ do -- trivial error assert(T.checkpanic("pushstring hi; error") == "hi") + -- thread status inside panic (bug in 5.4.4) + assert(T.checkpanic("pushstring hi; error", "threadstatus; return 2") == + "ERRRUN") + -- using the stack inside panic assert(T.checkpanic("pushstring hi; error;", [[checkstack 5 XX @@ -433,6 +437,21 @@ do assert(T.checkpanic("newuserdata 20000") == MEMERRMSG) T.totalmem(0) -- restore high limit + -- memory error + thread status + local x = T.checkpanic( + [[ alloccount 0 # force a memory error in next line + newtable + ]], + [[ + alloccount -1 # allow free allocations again + pushstring XX + threadstatus + concat 2 # to make sure message came from here + return 1 + ]]) + T.alloccount() + assert(x == "XX" .. "not enough memory") + -- stack error if not _soft then local msg = T.checkpanic[[ From 724012d3d07f43f03451bb05d2bd9f55dff1d116 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 16 Jan 2025 16:11:49 -0300 Subject: [PATCH 0999/1145] Small change in macro 'isvalid' The "faster way" to check whether a value is not 'nilvalue' is not faster. (Both forms entail one memory access.) --- lapi.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lapi.c b/lapi.c index 4411cb2962..cf73324b6b 100644 --- a/lapi.c +++ b/lapi.c @@ -40,10 +40,8 @@ const char lua_ident[] = /* ** Test for a valid index (one that is not the 'nilvalue'). -** '!ttisnil(o)' implies 'o != &G(L)->nilvalue', so it is not needed. -** However, it covers the most common cases in a faster way. */ -#define isvalid(L, o) (!ttisnil(o) || o != &G(L)->nilvalue) +#define isvalid(L, o) ((o) != &G(L)->nilvalue) /* test for pseudo index */ From 7d7ae8781e64e2b3b212d5c7b7c1b98b694df5ef Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 21 Jan 2025 13:33:59 -0300 Subject: [PATCH 1000/1145] Parameters for 'lua_createtable' back to int Tables don't accept sizes larger than int. --- lapi.c | 4 ++-- ltablib.c | 8 ++++---- ltests.c | 6 +++--- lua.c | 2 +- lua.h | 2 +- manual/manual.of | 6 +++--- testes/sort.lua | 6 ++++-- 7 files changed, 18 insertions(+), 16 deletions(-) diff --git a/lapi.c b/lapi.c index cf73324b6b..47d328ca93 100644 --- a/lapi.c +++ b/lapi.c @@ -782,14 +782,14 @@ LUA_API int lua_rawgetp (lua_State *L, int idx, const void *p) { } -LUA_API void lua_createtable (lua_State *L, unsigned narray, unsigned nrec) { +LUA_API void lua_createtable (lua_State *L, int narray, int nrec) { Table *t; lua_lock(L); t = luaH_new(L); sethvalue2s(L, L->top.p, t); api_incr_top(L); if (narray > 0 || nrec > 0) - luaH_resize(L, t, narray, nrec); + luaH_resize(L, t, cast_uint(narray), cast_uint(nrec)); luaC_checkGC(L); lua_unlock(L); } diff --git a/ltablib.c b/ltablib.c index baa7111e2c..46ecb5e024 100644 --- a/ltablib.c +++ b/ltablib.c @@ -62,9 +62,9 @@ static void checktab (lua_State *L, int arg, int what) { static int tcreate (lua_State *L) { lua_Unsigned sizeseq = (lua_Unsigned)luaL_checkinteger(L, 1); lua_Unsigned sizerest = (lua_Unsigned)luaL_optinteger(L, 2, 0); - luaL_argcheck(L, sizeseq <= UINT_MAX, 1, "out of range"); - luaL_argcheck(L, sizerest <= UINT_MAX, 2, "out of range"); - lua_createtable(L, (unsigned)sizeseq, (unsigned)sizerest); + luaL_argcheck(L, sizeseq <= cast_uint(INT_MAX), 1, "out of range"); + luaL_argcheck(L, sizerest <= cast_uint(INT_MAX), 2, "out of range"); + lua_createtable(L, cast_int(sizeseq), cast_int(sizerest)); return 1; } @@ -192,7 +192,7 @@ static int tconcat (lua_State *L) { static int tpack (lua_State *L) { int i; int n = lua_gettop(L); /* number of elements to pack */ - lua_createtable(L, cast_uint(n), 1); /* create result table */ + lua_createtable(L, n, 1); /* create result table */ lua_insert(L, 1); /* put it at index 1 */ for (i = n; i >= 1; i--) /* assign elements */ lua_seti(L, 1, i); diff --git a/ltests.c b/ltests.c index 83d08ac880..eaf3b251f4 100644 --- a/ltests.c +++ b/ltests.c @@ -809,7 +809,7 @@ static int listk (lua_State *L) { luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1), 1, "Lua function expected"); p = getproto(obj_at(L, 1)); - lua_createtable(L, cast_uint(p->sizek), 0); + lua_createtable(L, p->sizek, 0); for (i=0; isizek; i++) { pushobject(L, p->k+i); lua_rawseti(L, -2, i+1); @@ -825,7 +825,7 @@ static int listabslineinfo (lua_State *L) { 1, "Lua function expected"); p = getproto(obj_at(L, 1)); luaL_argcheck(L, p->abslineinfo != NULL, 1, "function has no debug info"); - lua_createtable(L, 2u * cast_uint(p->sizeabslineinfo), 0); + lua_createtable(L, 2 * p->sizeabslineinfo, 0); for (i=0; i < p->sizeabslineinfo; i++) { lua_pushinteger(L, p->abslineinfo[i].pc); lua_rawseti(L, -2, 2 * i + 1); @@ -867,7 +867,7 @@ void lua_printstack (lua_State *L) { static int get_limits (lua_State *L) { - lua_createtable(L, 0, 6); + lua_createtable(L, 0, 5); setnameval(L, "IS32INT", LUAI_IS32INT); setnameval(L, "MAXARG_Ax", MAXARG_Ax); setnameval(L, "MAXARG_Bx", MAXARG_Bx); diff --git a/lua.c b/lua.c index 64d391609a..b611cbcace 100644 --- a/lua.c +++ b/lua.c @@ -185,7 +185,7 @@ static void print_version (void) { static void createargtable (lua_State *L, char **argv, int argc, int script) { int i, narg; narg = argc - (script + 1); /* number of positive indices */ - lua_createtable(L, cast_uint(narg), cast_uint(script + 1)); + lua_createtable(L, narg, script + 1); for (i = 0; i < argc; i++) { lua_pushstring(L, argv[i]); lua_rawseti(L, -2, i - script); diff --git a/lua.h b/lua.h index 76068fdcc7..e82fc255c8 100644 --- a/lua.h +++ b/lua.h @@ -267,7 +267,7 @@ LUA_API int (lua_rawget) (lua_State *L, int idx); LUA_API int (lua_rawgeti) (lua_State *L, int idx, lua_Integer n); LUA_API int (lua_rawgetp) (lua_State *L, int idx, const void *p); -LUA_API void (lua_createtable) (lua_State *L, unsigned narr, unsigned nrec); +LUA_API void (lua_createtable) (lua_State *L, int narr, int nrec); LUA_API void *(lua_newuserdatauv) (lua_State *L, size_t sz, int nuvalue); LUA_API int (lua_getmetatable) (lua_State *L, int objindex); LUA_API int (lua_getiuservalue) (lua_State *L, int idx, int n); diff --git a/manual/manual.of b/manual/manual.of index 77e37de383..150315d41d 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -3238,7 +3238,7 @@ Values at other positions are not affected. } -@APIEntry{void lua_createtable (lua_State *L, unsigned nseq, unsigned nrec);| +@APIEntry{void lua_createtable (lua_State *L, int nseq, int nrec);| @apii{0,1,m} Creates a new empty table and pushes it onto the stack. @@ -3249,7 +3249,7 @@ the table will have. Lua may use these hints to preallocate memory for the new table. This preallocation may help performance when you know in advance how many elements the table will have. -Otherwise you can use the function @Lid{lua_newtable}. +Otherwise you should use the function @Lid{lua_newtable}. } @@ -3351,7 +3351,7 @@ Returns the previous mode (@id{LUA_GCGEN} or @id{LUA_GCINC}). @item{@defid{LUA_GCPARAM} (int param, int val)| Changes and/or returns the value of a parameter of the collector. -If @id{val} is negative, the call only returns the current value. +If @id{val} is -1, the call only returns the current value. The argument @id{param} must have one of the following values: @description{ @item{@defid{LUA_GCPMINORMUL}| The minor multiplier. } diff --git a/testes/sort.lua b/testes/sort.lua index 442b3129e4..965e153482 100644 --- a/testes/sort.lua +++ b/testes/sort.lua @@ -35,8 +35,10 @@ do print "testing 'table.create'" assert(memdiff > 1024 * 12) assert(not T or select(2, T.querytab(t)) == 1024) - checkerror("table overflow", table.create, (1<<31) + 1) - checkerror("table overflow", table.create, 0, (1<<31) + 1) + local maxint1 = 1 << (string.packsize("i") * 8 - 1) + checkerror("out of range", table.create, maxint1) + checkerror("out of range", table.create, 0, maxint1) + checkerror("table overflow", table.create, 0, maxint1 - 1) end From c4e7cdb541d89142056927ebdfd8f97017d38f45 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 27 Jan 2025 16:09:55 -0300 Subject: [PATCH 1001/1145] Renaming two new functions 'lua_numbertostrbuff' -> 'lua_numbertocstring' 'lua_pushextlstring' -> 'lua_pushexternalstring' --- lapi.c | 4 ++-- lauxlib.c | 6 +++--- liolib.c | 2 +- loadlib.c | 2 +- ltests.c | 4 ++-- lua.h | 4 ++-- manual/manual.of | 4 ++-- 7 files changed, 13 insertions(+), 13 deletions(-) diff --git a/lapi.c b/lapi.c index 47d328ca93..c0fd1a1b19 100644 --- a/lapi.c +++ b/lapi.c @@ -366,7 +366,7 @@ LUA_API int lua_compare (lua_State *L, int index1, int index2, int op) { } -LUA_API unsigned (lua_numbertostrbuff) (lua_State *L, int idx, char *buff) { +LUA_API unsigned (lua_numbertocstring) (lua_State *L, int idx, char *buff) { const TValue *o = index2value(L, idx); if (ttisnumber(o)) { unsigned len = luaO_tostringbuff(o, buff); @@ -546,7 +546,7 @@ LUA_API const char *lua_pushlstring (lua_State *L, const char *s, size_t len) { } -LUA_API const char *lua_pushextlstring (lua_State *L, +LUA_API const char *lua_pushexternalstring (lua_State *L, const char *s, size_t len, lua_Alloc falloc, void *ud) { TString *ts; lua_lock(L); diff --git a/lauxlib.c b/lauxlib.c index d37d2f8c3f..5bca18166d 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -622,9 +622,9 @@ LUALIB_API void luaL_pushresult (luaL_Buffer *B) { resizebox(L, -1, len + 1); /* adjust box size to content size */ s = (char*)box->box; /* final buffer address */ s[len] = '\0'; /* add ending zero */ - /* clear box, as 'lua_pushextlstring' will take control over buffer */ + /* clear box, as Lua will take control of the buffer */ box->bsize = 0; box->box = NULL; - lua_pushextlstring(L, s, len, allocf, ud); + lua_pushexternalstring(L, s, len, allocf, ud); lua_closeslot(L, -2); /* close the box */ lua_gc(L, LUA_GCSTEP, len); } @@ -929,7 +929,7 @@ LUALIB_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len) { switch (lua_type(L, idx)) { case LUA_TNUMBER: { char buff[LUA_N2SBUFFSZ]; - lua_numbertostrbuff(L, idx, buff); + lua_numbertocstring(L, idx, buff); lua_pushstring(L, buff); break; } diff --git a/liolib.c b/liolib.c index 98ff3d0dfb..a0988db06a 100644 --- a/liolib.c +++ b/liolib.c @@ -667,7 +667,7 @@ static int g_write (lua_State *L, FILE *f, int arg) { for (; nargs--; arg++) { char buff[LUA_N2SBUFFSZ]; const char *s; - size_t len = lua_numbertostrbuff(L, arg, buff); /* try as a number */ + size_t len = lua_numbertocstring(L, arg, buff); /* try as a number */ if (len > 0) { /* did conversion work (value was a number)? */ s = buff; len--; diff --git a/loadlib.c b/loadlib.c index e5ed135286..5f0c170296 100644 --- a/loadlib.c +++ b/loadlib.c @@ -280,7 +280,7 @@ static void setpath (lua_State *L, const char *fieldname, if (path == NULL) /* no versioned environment variable? */ path = getenv(envname); /* try unversioned name */ if (path == NULL || noenv(L)) /* no environment variable? */ - lua_pushextlstring(L, dft, strlen(dft), NULL, NULL); /* use default */ + lua_pushexternalstring(L, dft, strlen(dft), NULL, NULL); /* use default */ else if ((dftmark = strstr(path, LUA_PATH_SEP LUA_PATH_SEP)) == NULL) lua_pushstring(L, path); /* nothing to change */ else { /* path contains a ";;": insert default path in its place */ diff --git a/ltests.c b/ltests.c index eaf3b251f4..e3037be32b 100644 --- a/ltests.c +++ b/ltests.c @@ -1389,7 +1389,7 @@ static int checkpanic (lua_State *L) { static int externKstr (lua_State *L) { size_t len; const char *s = luaL_checklstring(L, 1, &len); - lua_pushextlstring(L, s, len, NULL, NULL); + lua_pushexternalstring(L, s, len, NULL, NULL); return 1; } @@ -1413,7 +1413,7 @@ static int externstr (lua_State *L) { /* copy string content to buffer, including ending 0 */ memcpy(buff, s, (len + 1) * sizeof(char)); /* create external string */ - lua_pushextlstring(L, buff, len, allocf, ud); + lua_pushexternalstring(L, buff, len, allocf, ud); return 1; } diff --git a/lua.h b/lua.h index e82fc255c8..95e0db321a 100644 --- a/lua.h +++ b/lua.h @@ -244,7 +244,7 @@ LUA_API void (lua_pushnil) (lua_State *L); LUA_API void (lua_pushnumber) (lua_State *L, lua_Number n); LUA_API void (lua_pushinteger) (lua_State *L, lua_Integer n); LUA_API const char *(lua_pushlstring) (lua_State *L, const char *s, size_t len); -LUA_API const char *(lua_pushextlstring) (lua_State *L, +LUA_API const char *(lua_pushexternalstring) (lua_State *L, const char *s, size_t len, lua_Alloc falloc, void *ud); LUA_API const char *(lua_pushstring) (lua_State *L, const char *s); LUA_API const char *(lua_pushvfstring) (lua_State *L, const char *fmt, @@ -372,7 +372,7 @@ LUA_API void (lua_concat) (lua_State *L, int n); LUA_API void (lua_len) (lua_State *L, int idx); #define LUA_N2SBUFFSZ 64 -LUA_API unsigned (lua_numbertostrbuff) (lua_State *L, int idx, char *buff); +LUA_API unsigned (lua_numbertocstring) (lua_State *L, int idx, char *buff); LUA_API size_t (lua_stringtonumber) (lua_State *L, const char *s); LUA_API lua_Alloc (lua_getallocf) (lua_State *L, void **ud); diff --git a/manual/manual.of b/manual/manual.of index 150315d41d..274799e3f1 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -3829,7 +3829,7 @@ This macro may evaluate its arguments more than once. } -@APIEntry{unsigned (lua_numbertostrbuff) (lua_State *L, int idx, +@APIEntry{unsigned (lua_numbertocstring) (lua_State *L, int idx, char *buff);| @apii{0,0,-} @@ -3955,7 +3955,7 @@ This function is equivalent to @Lid{lua_pushcclosure} with no upvalues. } -@APIEntry{const char *(lua_pushextlstring) (lua_State *L, +@APIEntry{const char *(lua_pushexternalstring) (lua_State *L, const char *s, size_t len, lua_Alloc falloc, void *ud);| @apii{0,1,m} From 39a14ea7d7b14172595c61619e8f35c2614b2606 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 28 Jan 2025 11:45:45 -0300 Subject: [PATCH 1002/1145] CallInfo bit CIST_CLSRET broken in two Since commit f407b3c4a, it was being used for two distinct (and incompatible) meanings: A: Function has TBC variables (now bit CIST_TBC) B: Interpreter is closing TBC variables (original bit CIST_CLSRET) B implies A, but A does not imply B. --- lapi.c | 6 +++--- ldo.c | 12 +++++++----- lstate.h | 4 +++- testes/coroutine.lua | 37 +++++++++++++++++++++++++++++++------ 4 files changed, 44 insertions(+), 15 deletions(-) diff --git a/lapi.c b/lapi.c index c0fd1a1b19..7b30617f7e 100644 --- a/lapi.c +++ b/lapi.c @@ -195,7 +195,7 @@ LUA_API void lua_settop (lua_State *L, int idx) { } newtop = L->top.p + diff; if (diff < 0 && L->tbclist.p >= newtop) { - lua_assert(ci->callstatus & CIST_CLSRET); + lua_assert(ci->callstatus & CIST_TBC); newtop = luaF_close(L, newtop, CLOSEKTOP, 0); } L->top.p = newtop; /* correct top only after closing any upvalue */ @@ -207,7 +207,7 @@ LUA_API void lua_closeslot (lua_State *L, int idx) { StkId level; lua_lock(L); level = index2stack(L, idx); - api_check(L, (L->ci->callstatus & CIST_CLSRET) && L->tbclist.p == level, + api_check(L, (L->ci->callstatus & CIST_TBC) && (L->tbclist.p == level), "no variable to close at given level"); level = luaF_close(L, level, CLOSEKTOP, 0); setnilvalue(s2v(level)); @@ -1280,7 +1280,7 @@ LUA_API void lua_toclose (lua_State *L, int idx) { o = index2stack(L, idx); api_check(L, L->tbclist.p < o, "given index below or equal a marked one"); luaF_newtbcupval(L, o); /* create new to-be-closed upvalue */ - L->ci->callstatus |= CIST_CLSRET; /* mark that function has TBC slots */ + L->ci->callstatus |= CIST_TBC; /* mark that function has TBC slots */ lua_unlock(L); } diff --git a/ldo.c b/ldo.c index 994ad6f0fb..31c00a2169 100644 --- a/ldo.c +++ b/ldo.c @@ -505,7 +505,7 @@ l_sinline void genmoveresults (lua_State *L, StkId res, int nres, ** Given 'nres' results at 'firstResult', move 'fwanted-1' of them ** to 'res'. Handle most typical cases (zero results for commands, ** one result for expressions, multiple results for tail calls/single -** parameters) separated. The flag CIST_CLSRET in 'fwanted', if set, +** parameters) separated. The flag CIST_TBC in 'fwanted', if set, ** forces the swicth to go to the default case. */ l_sinline void moveresults (lua_State *L, StkId res, int nres, @@ -526,8 +526,9 @@ l_sinline void moveresults (lua_State *L, StkId res, int nres, break; default: { /* two/more results and/or to-be-closed variables */ int wanted = get_nresults(fwanted); - if (fwanted & CIST_CLSRET) { /* to-be-closed variables? */ + if (fwanted & CIST_TBC) { /* to-be-closed variables? */ L->ci->u2.nres = nres; + L->ci->callstatus |= CIST_CLSRET; /* in case of yields */ res = luaF_close(L, res, CLOSEKTOP, 1); L->ci->callstatus &= ~CIST_CLSRET; if (L->hookmask) { /* if needed, call hook after '__close's */ @@ -552,8 +553,8 @@ l_sinline void moveresults (lua_State *L, StkId res, int nres, ** that. */ void luaD_poscall (lua_State *L, CallInfo *ci, int nres) { - l_uint32 fwanted = ci->callstatus & (CIST_CLSRET | CIST_NRESULTS); - if (l_unlikely(L->hookmask) && !(fwanted & CIST_CLSRET)) + l_uint32 fwanted = ci->callstatus & (CIST_TBC | CIST_NRESULTS); + if (l_unlikely(L->hookmask) && !(fwanted & CIST_TBC)) rethook(L, ci, nres); /* move results to proper place */ moveresults(L, ci->func.p, nres, fwanted); @@ -785,7 +786,8 @@ static int finishpcallk (lua_State *L, CallInfo *ci) { */ static void finishCcall (lua_State *L, CallInfo *ci) { int n; /* actual number of results from C function */ - if (ci->callstatus & CIST_CLSRET) { /* was returning? */ + if (ci->callstatus & CIST_CLSRET) { /* was closing TBC variable? */ + lua_assert(ci->callstatus & CIST_TBC); n = ci->u2.nres; /* just redo 'luaD_poscall' */ /* don't need to reset CIST_CLSRET, as it will be set again anyway */ } diff --git a/lstate.h b/lstate.h index e95c72880e..635f41d2ec 100644 --- a/lstate.h +++ b/lstate.h @@ -235,8 +235,10 @@ struct CallInfo { #define CIST_FRESH cast(l_uint32, CIST_C << 1) /* function is closing tbc variables */ #define CIST_CLSRET (CIST_FRESH << 1) +/* function has tbc variables to close */ +#define CIST_TBC (CIST_CLSRET << 1) /* original value of 'allowhook' */ -#define CIST_OAH (CIST_CLSRET << 1) +#define CIST_OAH (CIST_TBC << 1) /* call is running a debug hook */ #define CIST_HOOKED (CIST_OAH << 1) /* doing a yieldable protected call */ diff --git a/testes/coroutine.lua b/testes/coroutine.lua index c1252ab89f..78b9bdca19 100644 --- a/testes/coroutine.lua +++ b/testes/coroutine.lua @@ -515,7 +515,7 @@ else print "testing yields inside hooks" local turn - + local function fact (t, x) assert(turn == t) if x == 0 then return 1 @@ -642,7 +642,7 @@ else print "testing coroutine API" - + -- reusing a thread assert(T.testC([[ newthread # create thread @@ -920,7 +920,7 @@ do -- a few more tests for comparison operators until res ~= 10 return res end - + local function test () local a1 = setmetatable({x=1}, mt1) local a2 = setmetatable({x=2}, mt2) @@ -932,7 +932,7 @@ do -- a few more tests for comparison operators assert(2 >= a2) return true end - + run(test) end @@ -1037,6 +1037,31 @@ f = T.makeCfunc([[ return * ]], 23, "huu") + +do -- testing bug introduced in commit f407b3c4a + local X = false -- flag "to be closed" + local coro = coroutine.wrap(T.testC) + -- runs it until 'pcallk' (that yields) + -- 4th argument (at index 4): object to be closed + local res1, res2 = coro( + [[ + toclose 3 # this could break the next 'pcallk' + pushvalue 2 # push function 'yield' to call it + pushint 22; pushint 33 # arguments to yield + # calls 'yield' (2 args; 2 results; continuation function at index 4) + pcallk 2 2 4 + invalid command (should not arrive here) + ]], -- 1st argument (at index 1): code; + coroutine.yield, -- (at index 2): function to be called + func2close(function () X = true end), -- (index 3): TBC slot + "pushint 43; return 3" -- (index 4): code for continuation function + ) + + assert(res1 == 22 and res2 == 33 and not X) + local res1, res2, res3 = coro(34, "hi") -- runs continuation function + assert(res1 == 34 and res2 == "hi" and res3 == 43 and X) +end + x = coroutine.wrap(f) assert(x() == 102) eqtab({x()}, {23, "huu"}) @@ -1094,11 +1119,11 @@ co = coroutine.wrap(function (...) return cannot be here! ]], [[ # 1st continuation - yieldk 0 3 + yieldk 0 3 cannot be here! ]], [[ # 2nd continuation - yieldk 0 4 + yieldk 0 4 cannot be here! ]], [[ # 3th continuation From f7439112a5469078ac4f444106242cf1c1d3fe8a Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 29 Jan 2025 14:47:06 -0300 Subject: [PATCH 1003/1145] Details (in test library) - Added support for negative stack indices in the "C interpreter" - Improved format when printing values --- ltests.c | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/ltests.c b/ltests.c index e3037be32b..6b5dc27643 100644 --- a/ltests.c +++ b/ltests.c @@ -343,13 +343,19 @@ void lua_printvalue (TValue *v) { printf("%s", (!l_isfalse(v) ? "true" : "false")); break; } + case LUA_TLIGHTUSERDATA: { + printf("light udata: %p", pvalue(v)); + break; + } case LUA_TNIL: { printf("nil"); break; } default: { - void *p = iscollectable(v) ? gcvalue(v) : NULL; - printf("%s: %p", ttypename(ttype(v)), p); + if (ttislcf(v)) + printf("light C function: %p", fvalue(v)); + else /* must be collectable */ + printf("%s: %p", ttypename(ttype(v)), gcvalue(v)); break; } } @@ -1499,9 +1505,14 @@ static int getindex_aux (lua_State *L, lua_State *L1, const char **pc) { skip(pc); switch (*(*pc)++) { case 'R': return LUA_REGISTRYINDEX; - case 'G': return luaL_error(L, "deprecated index 'G'"); case 'U': return lua_upvalueindex(getnum_aux(L, L1, pc)); - default: (*pc)--; return getnum_aux(L, L1, pc); + default: { + int n; + (*pc)--; /* to read again */ + n = getnum_aux(L, L1, pc); + if (n == 0) return 0; + else return lua_absindex(L1, n); + } } } @@ -1550,7 +1561,7 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) { const char *inst = getstring; if EQ("") return 0; else if EQ("absindex") { - lua_pushinteger(L1, lua_absindex(L1, getindex)); + lua_pushinteger(L1, getindex); } else if EQ("append") { int t = getindex; From d1e677c52be3b107a7a29fdc482158f6d9251e79 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 30 Jan 2025 11:41:39 -0300 Subject: [PATCH 1004/1145] New type 'TStatus' for thread status/error codes --- lapi.c | 10 +++++----- ldo.c | 45 +++++++++++++++++++++++---------------------- ldo.h | 14 ++++++++------ lfunc.c | 5 +++-- lfunc.h | 4 ++-- lgc.c | 2 +- llimits.h | 6 ++++++ lstate.c | 6 +++--- lstate.h | 8 ++++---- lstring.c | 2 +- 10 files changed, 56 insertions(+), 46 deletions(-) diff --git a/lapi.c b/lapi.c index 7b30617f7e..b3062072ab 100644 --- a/lapi.c +++ b/lapi.c @@ -1070,7 +1070,7 @@ static void f_call (lua_State *L, void *ud) { LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc, lua_KContext ctx, lua_KFunction k) { struct CallS c; - int status; + TStatus status; ptrdiff_t func; lua_lock(L); api_check(L, k == NULL || !isLua(L->ci), @@ -1107,14 +1107,14 @@ LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc, } adjustresults(L, nresults); lua_unlock(L); - return status; + return APIstatus(status); } LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data, const char *chunkname, const char *mode) { ZIO z; - int status; + TStatus status; lua_lock(L); if (!chunkname) chunkname = "?"; luaZ_init(L, &z, reader, data); @@ -1131,7 +1131,7 @@ LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data, } } lua_unlock(L); - return status; + return APIstatus(status); } @@ -1154,7 +1154,7 @@ LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data, int strip) { LUA_API int lua_status (lua_State *L) { - return L->status; + return APIstatus(L->status); } diff --git a/ldo.c b/ldo.c index 31c00a2169..3f9c8b7dfb 100644 --- a/ldo.c +++ b/ldo.c @@ -97,11 +97,11 @@ struct lua_longjmp { struct lua_longjmp *previous; luai_jmpbuf b; - volatile int status; /* error code */ + volatile TStatus status; /* error code */ }; -void luaD_seterrorobj (lua_State *L, int errcode, StkId oldtop) { +void luaD_seterrorobj (lua_State *L, TStatus errcode, StkId oldtop) { switch (errcode) { case LUA_ERRMEM: { /* memory error? */ setsvalue2s(L, oldtop, G(L)->memerrmsg); /* reuse preregistered msg. */ @@ -125,7 +125,7 @@ void luaD_seterrorobj (lua_State *L, int errcode, StkId oldtop) { } -l_noret luaD_throw (lua_State *L, int errcode) { +l_noret luaD_throw (lua_State *L, TStatus errcode) { if (L->errorJmp) { /* thread has an error handler? */ L->errorJmp->status = errcode; /* set status */ LUAI_THROW(L, L->errorJmp); /* jump to it */ @@ -133,7 +133,7 @@ l_noret luaD_throw (lua_State *L, int errcode) { else { /* thread has no error handler */ global_State *g = G(L); errcode = luaE_resetthread(L, errcode); /* close all upvalues */ - L->status = cast_byte(errcode); + L->status = errcode; if (g->mainthread->errorJmp) { /* main thread has a handler? */ setobjs2s(L, g->mainthread->top.p++, L->top.p - 1); /* copy error obj. */ luaD_throw(g->mainthread, errcode); /* re-throw in main thread */ @@ -149,7 +149,7 @@ l_noret luaD_throw (lua_State *L, int errcode) { } -int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) { +TStatus luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) { l_uint32 oldnCcalls = L->nCcalls; struct lua_longjmp lj; lj.status = LUA_OK; @@ -751,8 +751,8 @@ void luaD_callnoyield (lua_State *L, StkId func, int nResults) { ** particular, field CIST_RECST preserves the error status across these ** multiple runs, changing only if there is a new error. */ -static int finishpcallk (lua_State *L, CallInfo *ci) { - int status = getcistrecst(ci); /* get original status */ +static TStatus finishpcallk (lua_State *L, CallInfo *ci) { + TStatus status = getcistrecst(ci); /* get original status */ if (l_likely(status == LUA_OK)) /* no error? */ status = LUA_YIELD; /* was interrupted by an yield */ else { /* error */ @@ -792,14 +792,15 @@ static void finishCcall (lua_State *L, CallInfo *ci) { /* don't need to reset CIST_CLSRET, as it will be set again anyway */ } else { - int status = LUA_YIELD; /* default if there were no errors */ + TStatus status = LUA_YIELD; /* default if there were no errors */ + lua_KFunction kf = ci->u.c.k; /* continuation function */ /* must have a continuation and must be able to call it */ - lua_assert(ci->u.c.k != NULL && yieldable(L)); + lua_assert(kf != NULL && yieldable(L)); if (ci->callstatus & CIST_YPCALL) /* was inside a 'lua_pcallk'? */ status = finishpcallk(L, ci); /* finish it */ adjustresults(L, LUA_MULTRET); /* finish 'lua_callk' */ lua_unlock(L); - n = (*ci->u.c.k)(L, status, ci->u.c.ctx); /* call continuation */ + n = (*kf)(L, APIstatus(status), ci->u.c.ctx); /* call continuation */ lua_lock(L); api_checknelems(L, n); } @@ -901,7 +902,7 @@ static void resume (lua_State *L, void *ud) { ** (status == LUA_YIELD), or an unprotected error ('findpcall' doesn't ** find a recover point). */ -static int precover (lua_State *L, int status) { +static TStatus precover (lua_State *L, TStatus status) { CallInfo *ci; while (errorstatus(status) && (ci = findpcall(L)) != NULL) { L->ci = ci; /* go down to recovery functions */ @@ -914,7 +915,7 @@ static int precover (lua_State *L, int status) { LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs, int *nresults) { - int status; + TStatus status; lua_lock(L); if (L->status == LUA_OK) { /* may be starting a coroutine */ if (L->ci != &L->base_ci) /* not in base level? */ @@ -936,14 +937,14 @@ LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs, if (l_likely(!errorstatus(status))) lua_assert(status == L->status); /* normal end or yield */ else { /* unrecoverable error */ - L->status = cast_byte(status); /* mark thread as 'dead' */ + L->status = status; /* mark thread as 'dead' */ luaD_seterrorobj(L, status, L->top.p); /* push error message */ L->ci->top.p = L->top.p; } *nresults = (status == LUA_YIELD) ? L->ci->u2.nyield : cast_int(L->top.p - (L->ci->func.p + 1)); lua_unlock(L); - return status; + return APIstatus(status); } @@ -988,7 +989,7 @@ LUA_API int lua_yieldk (lua_State *L, int nresults, lua_KContext ctx, */ struct CloseP { StkId level; - int status; + TStatus status; }; @@ -1005,7 +1006,7 @@ static void closepaux (lua_State *L, void *ud) { ** Calls 'luaF_close' in protected mode. Return the original status ** or, in case of errors, the new status. */ -int luaD_closeprotected (lua_State *L, ptrdiff_t level, int status) { +TStatus luaD_closeprotected (lua_State *L, ptrdiff_t level, TStatus status) { CallInfo *old_ci = L->ci; lu_byte old_allowhooks = L->allowhook; for (;;) { /* keep closing upvalues until no more errors */ @@ -1027,9 +1028,9 @@ int luaD_closeprotected (lua_State *L, ptrdiff_t level, int status) { ** thread information ('allowhook', etc.) and in particular ** its stack level in case of errors. */ -int luaD_pcall (lua_State *L, Pfunc func, void *u, - ptrdiff_t old_top, ptrdiff_t ef) { - int status; +TStatus luaD_pcall (lua_State *L, Pfunc func, void *u, ptrdiff_t old_top, + ptrdiff_t ef) { + TStatus status; CallInfo *old_ci = L->ci; lu_byte old_allowhooks = L->allowhook; ptrdiff_t old_errfunc = L->errfunc; @@ -1091,10 +1092,10 @@ static void f_parser (lua_State *L, void *ud) { } -int luaD_protectedparser (lua_State *L, ZIO *z, const char *name, - const char *mode) { +TStatus luaD_protectedparser (lua_State *L, ZIO *z, const char *name, + const char *mode) { struct SParser p; - int status; + TStatus status; incnny(L); /* cannot yield during parsing */ p.z = z; p.name = name; p.mode = mode; p.dyd.actvar.arr = NULL; p.dyd.actvar.size = 0; diff --git a/ldo.h b/ldo.h index b52a353fda..ea1655e1fa 100644 --- a/ldo.h +++ b/ldo.h @@ -67,8 +67,9 @@ /* type of protected functions, to be ran by 'runprotected' */ typedef void (*Pfunc) (lua_State *L, void *ud); -LUAI_FUNC void luaD_seterrorobj (lua_State *L, int errcode, StkId oldtop); -LUAI_FUNC int luaD_protectedparser (lua_State *L, ZIO *z, const char *name, +LUAI_FUNC void luaD_seterrorobj (lua_State *L, TStatus errcode, StkId oldtop); +LUAI_FUNC TStatus luaD_protectedparser (lua_State *L, ZIO *z, + const char *name, const char *mode); LUAI_FUNC void luaD_hook (lua_State *L, int event, int line, int fTransfer, int nTransfer); @@ -78,8 +79,9 @@ LUAI_FUNC int luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, LUAI_FUNC CallInfo *luaD_precall (lua_State *L, StkId func, int nResults); LUAI_FUNC void luaD_call (lua_State *L, StkId func, int nResults); LUAI_FUNC void luaD_callnoyield (lua_State *L, StkId func, int nResults); -LUAI_FUNC int luaD_closeprotected (lua_State *L, ptrdiff_t level, int status); -LUAI_FUNC int luaD_pcall (lua_State *L, Pfunc func, void *u, +LUAI_FUNC TStatus luaD_closeprotected (lua_State *L, ptrdiff_t level, + TStatus status); +LUAI_FUNC TStatus luaD_pcall (lua_State *L, Pfunc func, void *u, ptrdiff_t oldtop, ptrdiff_t ef); LUAI_FUNC void luaD_poscall (lua_State *L, CallInfo *ci, int nres); LUAI_FUNC int luaD_reallocstack (lua_State *L, int newsize, int raiseerror); @@ -87,8 +89,8 @@ LUAI_FUNC int luaD_growstack (lua_State *L, int n, int raiseerror); LUAI_FUNC void luaD_shrinkstack (lua_State *L); LUAI_FUNC void luaD_inctop (lua_State *L); -LUAI_FUNC l_noret luaD_throw (lua_State *L, int errcode); -LUAI_FUNC int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud); +LUAI_FUNC l_noret luaD_throw (lua_State *L, TStatus errcode); +LUAI_FUNC TStatus luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud); #endif diff --git a/lfunc.c b/lfunc.c index 0ea05e009a..d6853ff82f 100644 --- a/lfunc.c +++ b/lfunc.c @@ -140,7 +140,8 @@ static void checkclosemth (lua_State *L, StkId level) { ** the 'level' of the upvalue being closed, as everything after that ** won't be used again. */ -static void prepcallclosemth (lua_State *L, StkId level, int status, int yy) { +static void prepcallclosemth (lua_State *L, StkId level, TStatus status, + int yy) { TValue *uv = s2v(level); /* value being closed */ TValue *errobj; if (status == CLOSEKTOP) @@ -224,7 +225,7 @@ static void poptbclist (lua_State *L) { ** Close all upvalues and to-be-closed variables up to the given stack ** level. Return restored 'level'. */ -StkId luaF_close (lua_State *L, StkId level, int status, int yy) { +StkId luaF_close (lua_State *L, StkId level, TStatus status, int yy) { ptrdiff_t levelrel = savestack(L, level); luaF_closeupval(L, level); /* first, close the upvalues */ while (L->tbclist.p >= level) { /* traverse tbc's down to that level */ diff --git a/lfunc.h b/lfunc.h index 342389e48a..d6aad3a6df 100644 --- a/lfunc.h +++ b/lfunc.h @@ -44,7 +44,7 @@ /* special status to close upvalues preserving the top of the stack */ -#define CLOSEKTOP (-1) +#define CLOSEKTOP (LUA_ERRERR + 1) LUAI_FUNC Proto *luaF_newproto (lua_State *L); @@ -54,7 +54,7 @@ LUAI_FUNC void luaF_initupvals (lua_State *L, LClosure *cl); LUAI_FUNC UpVal *luaF_findupval (lua_State *L, StkId level); LUAI_FUNC void luaF_newtbcupval (lua_State *L, StkId level); LUAI_FUNC void luaF_closeupval (lua_State *L, StkId level); -LUAI_FUNC StkId luaF_close (lua_State *L, StkId level, int status, int yy); +LUAI_FUNC StkId luaF_close (lua_State *L, StkId level, TStatus status, int yy); LUAI_FUNC void luaF_unlinkupval (UpVal *uv); LUAI_FUNC lu_mem luaF_protosize (Proto *p); LUAI_FUNC void luaF_freeproto (lua_State *L, Proto *f); diff --git a/lgc.c b/lgc.c index 1e9f75698c..8a82b6d990 100644 --- a/lgc.c +++ b/lgc.c @@ -953,7 +953,7 @@ static void GCTM (lua_State *L) { setgcovalue(L, &v, udata2finalize(g)); tm = luaT_gettmbyobj(L, &v, TM_GC); if (!notm(tm)) { /* is there a finalizer? */ - int status; + TStatus status; lu_byte oldah = L->allowhook; lu_byte oldgcstp = g->gcstp; g->gcstp |= GCSTPGC; /* avoid GC steps */ diff --git a/llimits.h b/llimits.h index d98171ae6b..d206e9e1cd 100644 --- a/llimits.h +++ b/llimits.h @@ -41,6 +41,12 @@ typedef unsigned char lu_byte; typedef signed char ls_byte; +/* Type for thread status/error codes */ +typedef lu_byte TStatus; + +/* The C API still uses 'int' for status/error codes */ +#define APIstatus(st) cast_int(st) + /* maximum value for size_t */ #define MAX_SIZET ((size_t)(~(size_t)0)) diff --git a/lstate.c b/lstate.c index 0e1cb01ebb..18ab4900ba 100644 --- a/lstate.c +++ b/lstate.c @@ -320,7 +320,7 @@ void luaE_freethread (lua_State *L, lua_State *L1) { } -int luaE_resetthread (lua_State *L, int status) { +TStatus luaE_resetthread (lua_State *L, TStatus status) { CallInfo *ci = L->ci = &L->base_ci; /* unwind CallInfo list */ setnilvalue(s2v(L->stack.p)); /* 'function' entry for basic 'ci' */ ci->func.p = L->stack.p; @@ -340,12 +340,12 @@ int luaE_resetthread (lua_State *L, int status) { LUA_API int lua_closethread (lua_State *L, lua_State *from) { - int status; + TStatus status; lua_lock(L); L->nCcalls = (from) ? getCcalls(from) : 0; status = luaE_resetthread(L, L->status); lua_unlock(L); - return status; + return APIstatus(status); } diff --git a/lstate.h b/lstate.h index 635f41d2ec..b47a4e9b31 100644 --- a/lstate.h +++ b/lstate.h @@ -339,8 +339,8 @@ typedef struct global_State { */ struct lua_State { CommonHeader; - lu_byte status; lu_byte allowhook; + TStatus status; unsigned short nci; /* number of items in 'ci' list */ StkIdRel top; /* first free slot in the stack */ global_State *l_G; @@ -352,10 +352,10 @@ struct lua_State { GCObject *gclist; struct lua_State *twups; /* list of threads with open upvalues */ struct lua_longjmp *errorJmp; /* current error recover point */ - CallInfo base_ci; /* CallInfo for first level (C calling Lua) */ + CallInfo base_ci; /* CallInfo for first level (C host) */ volatile lua_Hook hook; ptrdiff_t errfunc; /* current error handling function (stack index) */ - l_uint32 nCcalls; /* number of nested (non-yieldable | C) calls */ + l_uint32 nCcalls; /* number of nested non-yieldable or C calls */ int oldpc; /* last pc traced */ int basehookcount; int hookcount; @@ -438,7 +438,7 @@ LUAI_FUNC void luaE_checkcstack (lua_State *L); LUAI_FUNC void luaE_incCstack (lua_State *L); LUAI_FUNC void luaE_warning (lua_State *L, const char *msg, int tocont); LUAI_FUNC void luaE_warnerror (lua_State *L, const char *where); -LUAI_FUNC int luaE_resetthread (lua_State *L, int status); +LUAI_FUNC TStatus luaE_resetthread (lua_State *L, TStatus status); #endif diff --git a/lstring.c b/lstring.c index 0c89a51b07..b5c8f89f02 100644 --- a/lstring.c +++ b/lstring.c @@ -329,7 +329,7 @@ TString *luaS_newextlstr (lua_State *L, if (!falloc) f_pintern(L, &ne); /* just internalize string */ else { - int status = luaD_rawrunprotected(L, f_pintern, &ne); + TStatus status = luaD_rawrunprotected(L, f_pintern, &ne); (*falloc)(ud, cast_voidp(s), len + 1, 0); /* free external string */ if (status != LUA_OK) /* memory error? */ luaM_error(L); /* re-raise memory error */ From fa1382b5cd504bdfc5fc3f5c447ed09a4c9804fd Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 31 Jan 2025 13:51:38 -0300 Subject: [PATCH 1005/1145] Main thread is a regular field of global_State They were already allocated as a single block, so there is no need for the global_State to point to its main thread. --- lapi.c | 2 +- ldo.c | 9 ++++--- lgc.c | 8 +++--- lstate.c | 39 +++++++--------------------- lstate.h | 78 ++++++++++++++++++++++++++++++++------------------------ ltests.c | 4 +-- 6 files changed, 65 insertions(+), 75 deletions(-) diff --git a/lapi.c b/lapi.c index b3062072ab..a5e94507ac 100644 --- a/lapi.c +++ b/lapi.c @@ -655,7 +655,7 @@ LUA_API int lua_pushthread (lua_State *L) { setthvalue(L, s2v(L->top.p), L); api_incr_top(L); lua_unlock(L); - return (G(L)->mainthread == L); + return (mainthread(G(L)) == L); } diff --git a/ldo.c b/ldo.c index 3f9c8b7dfb..65252e07e9 100644 --- a/ldo.c +++ b/ldo.c @@ -132,11 +132,12 @@ l_noret luaD_throw (lua_State *L, TStatus errcode) { } else { /* thread has no error handler */ global_State *g = G(L); + lua_State *mainth = mainthread(g); errcode = luaE_resetthread(L, errcode); /* close all upvalues */ L->status = errcode; - if (g->mainthread->errorJmp) { /* main thread has a handler? */ - setobjs2s(L, g->mainthread->top.p++, L->top.p - 1); /* copy error obj. */ - luaD_throw(g->mainthread, errcode); /* re-throw in main thread */ + if (mainth->errorJmp) { /* main thread has a handler? */ + setobjs2s(L, mainth->top.p++, L->top.p - 1); /* copy error obj. */ + luaD_throw(mainth, errcode); /* re-throw in main thread */ } else { /* no handler at all; abort */ if (g->panic) { /* panic function? */ @@ -961,7 +962,7 @@ LUA_API int lua_yieldk (lua_State *L, int nresults, lua_KContext ctx, ci = L->ci; api_checkpop(L, nresults); if (l_unlikely(!yieldable(L))) { - if (L != G(L)->mainthread) + if (L != mainthread(G(L))) luaG_runerror(L, "attempt to yield across a C-call boundary"); else luaG_runerror(L, "attempt to yield from outside a coroutine"); diff --git a/lgc.c b/lgc.c index 8a82b6d990..e853305211 100644 --- a/lgc.c +++ b/lgc.c @@ -78,7 +78,7 @@ ((*getArrTag(t,i) & BIT_ISCOLLECTABLE) ? getArrVal(t,i)->gc : NULL) -#define markvalue(g,o) { checkliveness(g->mainthread,o); \ +#define markvalue(g,o) { checkliveness(mainthread(g),o); \ if (valiswhite(o)) reallymarkobject(g,gcvalue(o)); } #define markkey(g, n) { if keyiswhite(n) reallymarkobject(g,gckey(n)); } @@ -441,7 +441,7 @@ static void cleargraylists (global_State *g) { static void restartcollection (global_State *g) { cleargraylists(g); g->GCmarked = 0; - markobject(g, g->mainthread); + markobject(g, mainthread(g)); markvalue(g, &g->l_registry); markmt(g); markbeingfnz(g); /* mark any finalizing object left from previous cycle */ @@ -1513,7 +1513,7 @@ void luaC_freeallobjects (lua_State *L) { separatetobefnz(g, 1); /* separate all objects with finalizers */ lua_assert(g->finobj == NULL); callallpendingfinalizers(L); - deletelist(L, g->allgc, obj2gco(g->mainthread)); + deletelist(L, g->allgc, obj2gco(mainthread(g))); lua_assert(g->finobj == NULL); /* no new finalizers */ deletelist(L, g->fixedgc, NULL); /* collect fixed objects */ lua_assert(g->strt.nuse == 0); @@ -1526,7 +1526,7 @@ static void atomic (lua_State *L) { GCObject *grayagain = g->grayagain; /* save original list */ g->grayagain = NULL; lua_assert(g->ephemeron == NULL && g->weak == NULL); - lua_assert(!iswhite(g->mainthread)); + lua_assert(!iswhite(mainthread(g))); g->gcstate = GCSatomic; markobject(g, L); /* mark running thread */ /* registry and global metatables may be changed by API */ diff --git a/lstate.c b/lstate.c index 18ab4900ba..69ddef40ef 100644 --- a/lstate.c +++ b/lstate.c @@ -29,25 +29,6 @@ -/* -** thread state + extra space -*/ -typedef struct LX { - lu_byte extra_[LUA_EXTRASPACE]; - lua_State l; -} LX; - - -/* -** Main thread combines a thread state and the global state -*/ -typedef struct LG { - LX l; - global_State g; -} LG; - - - #define fromstate(L) (cast(LX *, cast(lu_byte *, (L)) - offsetof(LX, l))) @@ -278,8 +259,8 @@ static void close_state (lua_State *L) { } luaM_freearray(L, G(L)->strt.hash, cast_sizet(G(L)->strt.size)); freestack(L); - lua_assert(gettotalbytes(g) == sizeof(LG)); - (*g->frealloc)(g->ud, fromstate(L), sizeof(LG), 0); /* free main block */ + lua_assert(gettotalbytes(g) == sizeof(global_State)); + (*g->frealloc)(g->ud, g, sizeof(global_State), 0); /* free main block */ } @@ -301,7 +282,7 @@ LUA_API lua_State *lua_newthread (lua_State *L) { L1->hook = L->hook; resethookcount(L1); /* initialize L1 extra space */ - memcpy(lua_getextraspace(L1), lua_getextraspace(g->mainthread), + memcpy(lua_getextraspace(L1), lua_getextraspace(mainthread(g)), LUA_EXTRASPACE); luai_userstatethread(L, L1); stack_init(L1, L); /* init stack */ @@ -352,11 +333,10 @@ LUA_API int lua_closethread (lua_State *L, lua_State *from) { LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud, unsigned seed) { int i; lua_State *L; - global_State *g; - LG *l = cast(LG *, (*f)(ud, NULL, LUA_TTHREAD, sizeof(LG))); - if (l == NULL) return NULL; - L = &l->l.l; - g = &l->g; + global_State *g = cast(global_State*, + (*f)(ud, NULL, LUA_TTHREAD, sizeof(global_State))); + if (g == NULL) return NULL; + L = &g->mainth.l; L->tt = LUA_VTHREAD; g->currentwhite = bitmask(WHITE0BIT); L->marked = luaC_white(g); @@ -368,7 +348,6 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud, unsigned seed) { g->ud = ud; g->warnf = NULL; g->ud_warn = NULL; - g->mainthread = L; g->seed = seed; g->gcstp = GCSTPGC; /* no GC while building state */ g->strt.size = g->strt.nuse = 0; @@ -386,7 +365,7 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud, unsigned seed) { g->gray = g->grayagain = NULL; g->weak = g->ephemeron = g->allweak = NULL; g->twups = NULL; - g->GCtotalbytes = sizeof(LG); + g->GCtotalbytes = sizeof(global_State); g->GCmarked = 0; g->GCdebt = 0; setivalue(&g->nilvalue, 0); /* to signal that state is not yet built */ @@ -408,7 +387,7 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud, unsigned seed) { LUA_API void lua_close (lua_State *L) { lua_lock(L); - L = G(L)->mainthread; /* only the main thread can be closed */ + L = mainthread(G(L)); /* only the main thread can be closed */ close_state(L); } diff --git a/lstate.h b/lstate.h index b47a4e9b31..050fc35f52 100644 --- a/lstate.h +++ b/lstate.h @@ -283,6 +283,48 @@ struct CallInfo { #define getoah(ci) (((ci)->callstatus & CIST_OAH) ? 1 : 0) +/* +** 'per thread' state +*/ +struct lua_State { + CommonHeader; + lu_byte allowhook; + TStatus status; + unsigned short nci; /* number of items in 'ci' list */ + StkIdRel top; /* first free slot in the stack */ + struct global_State *l_G; + CallInfo *ci; /* call info for current function */ + StkIdRel stack_last; /* end of stack (last element + 1) */ + StkIdRel stack; /* stack base */ + UpVal *openupval; /* list of open upvalues in this stack */ + StkIdRel tbclist; /* list of to-be-closed variables */ + GCObject *gclist; + struct lua_State *twups; /* list of threads with open upvalues */ + struct lua_longjmp *errorJmp; /* current error recover point */ + CallInfo base_ci; /* CallInfo for first level (C host) */ + volatile lua_Hook hook; + ptrdiff_t errfunc; /* current error handling function (stack index) */ + l_uint32 nCcalls; /* number of nested non-yieldable or C calls */ + int oldpc; /* last pc traced */ + int basehookcount; + int hookcount; + volatile l_signalT hookmask; + struct { /* info about transferred values (for call/return hooks) */ + int ftransfer; /* offset of first value transferred */ + int ntransfer; /* number of values transferred */ + } transferinfo; +}; + + +/* +** thread state + extra space +*/ +typedef struct LX { + lu_byte extra_[LUA_EXTRASPACE]; + lua_State l; +} LX; + + /* ** 'global state', shared by all threads of this state */ @@ -324,50 +366,18 @@ typedef struct global_State { GCObject *finobjrold; /* list of really old objects with finalizers */ struct lua_State *twups; /* list of threads with open upvalues */ lua_CFunction panic; /* to be called in unprotected errors */ - struct lua_State *mainthread; TString *memerrmsg; /* message for memory-allocation errors */ TString *tmname[TM_N]; /* array with tag-method names */ struct Table *mt[LUA_NUMTYPES]; /* metatables for basic types */ TString *strcache[STRCACHE_N][STRCACHE_M]; /* cache for strings in API */ lua_WarnFunction warnf; /* warning function */ void *ud_warn; /* auxiliary data to 'warnf' */ + LX mainth; /* main thread of this state */ } global_State; -/* -** 'per thread' state -*/ -struct lua_State { - CommonHeader; - lu_byte allowhook; - TStatus status; - unsigned short nci; /* number of items in 'ci' list */ - StkIdRel top; /* first free slot in the stack */ - global_State *l_G; - CallInfo *ci; /* call info for current function */ - StkIdRel stack_last; /* end of stack (last element + 1) */ - StkIdRel stack; /* stack base */ - UpVal *openupval; /* list of open upvalues in this stack */ - StkIdRel tbclist; /* list of to-be-closed variables */ - GCObject *gclist; - struct lua_State *twups; /* list of threads with open upvalues */ - struct lua_longjmp *errorJmp; /* current error recover point */ - CallInfo base_ci; /* CallInfo for first level (C host) */ - volatile lua_Hook hook; - ptrdiff_t errfunc; /* current error handling function (stack index) */ - l_uint32 nCcalls; /* number of nested non-yieldable or C calls */ - int oldpc; /* last pc traced */ - int basehookcount; - int hookcount; - volatile l_signalT hookmask; - struct { /* info about transferred values (for call/return hooks) */ - int ftransfer; /* offset of first value transferred */ - int ntransfer; /* number of values transferred */ - } transferinfo; -}; - - #define G(L) (L->l_G) +#define mainthread(G) (&(G)->mainth.l) /* ** 'g->nilvalue' being a nil value flags that the state was completely diff --git a/ltests.c b/ltests.c index 6b5dc27643..f4855fea95 100644 --- a/ltests.c +++ b/ltests.c @@ -408,7 +408,7 @@ static void checktable (global_State *g, Table *h) { for (n = gnode(h, 0); n < limit; n++) { if (!isempty(gval(n))) { TValue k; - getnodekey(g->mainthread, &k, n); + getnodekey(mainthread(g), &k, n); assert(!keyisnil(n)); checkvalref(g, hgc, &k); checkvalref(g, hgc, gval(n)); @@ -672,7 +672,7 @@ int lua_checkmemory (lua_State *L) { l_mem totalin; /* total of objects that are in gray lists */ l_mem totalshould; /* total of objects that should be in gray lists */ if (keepinvariant(g)) { - assert(!iswhite(g->mainthread)); + assert(!iswhite(mainthread(g))); assert(!iswhite(gcvalue(&g->l_registry))); } assert(!isdead(g, gcvalue(&g->l_registry))); From cd38fe8cf3b0f54dcc1d4a21a7a9cb585c46a43e Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 18 Feb 2025 17:02:32 -0300 Subject: [PATCH 1006/1145] Added macro LUAI_STRICT_ADDRESS By default, the code assumes it is safe to use a dealocated pointer as long as the code does not access it. --- ldo.c | 28 ++++++++++++++++++---------- ltests.h | 4 ++++ 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/ldo.c b/ldo.c index 65252e07e9..4705b26c17 100644 --- a/ldo.c +++ b/ldo.c @@ -192,14 +192,19 @@ TStatus luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) { /* ** In ISO C, any pointer use after the pointer has been deallocated is -** undefined behavior. So, before a stack reallocation, all pointers are -** changed to offsets, and after the reallocation they are changed back -** to pointers. As during the reallocation the pointers are invalid, the -** reallocation cannot run emergency collections. -** +** undefined behavior. So, before a stack reallocation, all pointers +** should be changed to offsets, and after the reallocation they should +** be changed back to pointers. As during the reallocation the pointers +** are invalid, the reallocation cannot run emergency collections. +** Alternatively, we can use the old address after the deallocation. +** That is not strict ISO C, but seems to work fine everywhere. +** The following macro chooses how strict is the code. */ +#if !defined(LUAI_STRICT_ADDRESS) +#define LUAI_STRICT_ADDRESS 0 +#endif -#if 1 +#if LUAI_STRICT_ADDRESS /* ** Change all pointers to the stack into offsets. */ @@ -238,12 +243,16 @@ static void correctstack (lua_State *L, StkId oldstack) { #else /* -** Alternatively, we can use the old address after the deallocation. -** That is not strict ISO C, but seems to work fine everywhere. +** Assume that it is fine to use an address after its deallocation, +** as long as we do not dereference it. */ -static void relstack (lua_State *L) { UNUSED(L); } +static void relstack (lua_State *L) { UNUSED(L); } /* do nothing */ + +/* +** Correct pointers into 'oldstack' to point into 'L->stack'. +*/ static void correctstack (lua_State *L, StkId oldstack) { CallInfo *ci; UpVal *up; @@ -261,7 +270,6 @@ static void correctstack (lua_State *L, StkId oldstack) { ci->u.l.trap = 1; /* signal to update 'trap' in 'luaV_execute' */ } } - #endif diff --git a/ltests.h b/ltests.h index 543b0d553a..df72307a04 100644 --- a/ltests.h +++ b/ltests.h @@ -44,6 +44,10 @@ #define LUA_RAND32 +/* test stack reallocation with strict address use */ +#define LUAI_STRICT_ADDRESS 1 + + /* memory-allocator control variables */ typedef struct Memcontrol { int failnext; From e5f4927a0b97015d4c22bc22fbf80fb2c11ca7cc Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 20 Feb 2025 10:09:04 -0300 Subject: [PATCH 1007/1145] Array sizes in undump changed from unsigned to int Array sizes are always int and are dumped as int, so there is no reason to read them back as unsigned. --- lmem.h | 3 ++- lundump.c | 57 ++++++++++++++++++++++++------------------------------- 2 files changed, 27 insertions(+), 33 deletions(-) diff --git a/lmem.h b/lmem.h index 083585920d..dc714fb2e4 100644 --- a/lmem.h +++ b/lmem.h @@ -57,7 +57,8 @@ #define luaM_freearray(L, b, n) luaM_free_(L, (b), (n)*sizeof(*(b))) #define luaM_new(L,t) cast(t*, luaM_malloc_(L, sizeof(t), 0)) -#define luaM_newvector(L,n,t) cast(t*, luaM_malloc_(L, (n)*sizeof(t), 0)) +#define luaM_newvector(L,n,t) \ + cast(t*, luaM_malloc_(L, cast_sizet(n)*sizeof(t), 0)) #define luaM_newvectorchecked(L,n,t) \ (luaM_checksize(L,n,sizeof(t)), luaM_newvector(L,n,t)) diff --git a/lundump.c b/lundump.c index fd5a2ca6eb..d074a0734e 100644 --- a/lundump.c +++ b/lundump.c @@ -52,7 +52,7 @@ static l_noret error (LoadState *S, const char *why) { ** All high-level loads go through loadVector; you can change it to ** adapt to the endianness of the input */ -#define loadVector(S,b,n) loadBlock(S,b,(n)*sizeof((b)[0])) +#define loadVector(S,b,n) loadBlock(S,b,cast_sizet(n)*sizeof((b)[0])) static void loadBlock (LoadState *S, void *b, size_t size) { if (luaZ_read(S->Z, b, size) != 0) @@ -71,7 +71,7 @@ static void loadAlign (LoadState *S, unsigned align) { } -#define getaddr(S,n,t) cast(t *, getaddr_(S,(n) * sizeof(t))) +#define getaddr(S,n,t) cast(t *, getaddr_(S,cast_sizet(n) * sizeof(t))) static const void *getaddr_ (LoadState *S, size_t size) { const void *block = luaZ_getaddr(S->Z, size); @@ -113,13 +113,6 @@ static size_t loadSize (LoadState *S) { } -/* -** Read an non-negative int */ -static unsigned loadUint (LoadState *S) { - return cast_uint(loadVarint(S, cast_sizet(INT_MAX))); -} - - static int loadInt (LoadState *S) { return cast_int(loadVarint(S, cast_sizet(INT_MAX))); } @@ -188,15 +181,15 @@ static void loadString (LoadState *S, Proto *p, TString **sl) { static void loadCode (LoadState *S, Proto *f) { - unsigned n = loadUint(S); + int n = loadInt(S); loadAlign(S, sizeof(f->code[0])); if (S->fixed) { f->code = getaddr(S, n, Instruction); - f->sizecode = cast_int(n); + f->sizecode = n; } else { f->code = luaM_newvectorchecked(S->L, n, Instruction); - f->sizecode = cast_int(n); + f->sizecode = n; loadVector(S, f->code, n); } } @@ -206,10 +199,10 @@ static void loadFunction(LoadState *S, Proto *f); static void loadConstants (LoadState *S, Proto *f) { - unsigned i; - unsigned n = loadUint(S); + int i; + int n = loadInt(S); f->k = luaM_newvectorchecked(S->L, n, TValue); - f->sizek = cast_int(n); + f->sizek = n; for (i = 0; i < n; i++) setnilvalue(&f->k[i]); for (i = 0; i < n; i++) { @@ -248,10 +241,10 @@ static void loadConstants (LoadState *S, Proto *f) { static void loadProtos (LoadState *S, Proto *f) { - unsigned i; - unsigned n = loadUint(S); + int i; + int n = loadInt(S); f->p = luaM_newvectorchecked(S->L, n, Proto *); - f->sizep = cast_int(n); + f->sizep = n; for (i = 0; i < n; i++) f->p[i] = NULL; for (i = 0; i < n; i++) { @@ -269,10 +262,10 @@ static void loadProtos (LoadState *S, Proto *f) { ** in that case all prototypes must be consistent for the GC. */ static void loadUpvalues (LoadState *S, Proto *f) { - unsigned i; - unsigned n = loadUint(S); + int i; + int n = loadInt(S); f->upvalues = luaM_newvectorchecked(S->L, n, Upvaldesc); - f->sizeupvalues = cast_int(n); + f->sizeupvalues = n; for (i = 0; i < n; i++) /* make array valid for GC */ f->upvalues[i].name = NULL; for (i = 0; i < n; i++) { /* following calls can raise errors */ @@ -284,33 +277,33 @@ static void loadUpvalues (LoadState *S, Proto *f) { static void loadDebug (LoadState *S, Proto *f) { - unsigned i; - unsigned n = loadUint(S); + int i; + int n = loadInt(S); if (S->fixed) { f->lineinfo = getaddr(S, n, ls_byte); - f->sizelineinfo = cast_int(n); + f->sizelineinfo = n; } else { f->lineinfo = luaM_newvectorchecked(S->L, n, ls_byte); - f->sizelineinfo = cast_int(n); + f->sizelineinfo = n; loadVector(S, f->lineinfo, n); } - n = loadUint(S); + n = loadInt(S); if (n > 0) { loadAlign(S, sizeof(int)); if (S->fixed) { f->abslineinfo = getaddr(S, n, AbsLineInfo); - f->sizeabslineinfo = cast_int(n); + f->sizeabslineinfo = n; } else { f->abslineinfo = luaM_newvectorchecked(S->L, n, AbsLineInfo); - f->sizeabslineinfo = cast_int(n); + f->sizeabslineinfo = n; loadVector(S, f->abslineinfo, n); } } - n = loadUint(S); + n = loadInt(S); f->locvars = luaM_newvectorchecked(S->L, n, LocVar); - f->sizelocvars = cast_int(n); + f->sizelocvars = n; for (i = 0; i < n; i++) f->locvars[i].varname = NULL; for (i = 0; i < n; i++) { @@ -318,9 +311,9 @@ static void loadDebug (LoadState *S, Proto *f) { f->locvars[i].startpc = loadInt(S); f->locvars[i].endpc = loadInt(S); } - n = loadUint(S); + n = loadInt(S); if (n != 0) /* does it have debug information? */ - n = cast_uint(f->sizeupvalues); /* must be this many */ + n = f->sizeupvalues; /* must be this many */ for (i = 0; i < n; i++) loadString(S, f, &f->upvalues[i].name); } From ceac82f78be8baeddfa8536472d8b08df2eb7d49 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 26 Feb 2025 11:29:54 -0300 Subject: [PATCH 1008/1145] Details Comments, small changes in the manual, an extra test for errors in error handling, small changes in tests. --- lobject.c | 8 ++++---- manual/manual.of | 2 +- testes/errors.lua | 25 +++++++++++++++++++++++-- testes/main.lua | 5 ++++- testes/sort.lua | 2 +- 5 files changed, 33 insertions(+), 9 deletions(-) diff --git a/lobject.c b/lobject.c index c0fd182f13..68566a2bad 100644 --- a/lobject.c +++ b/lobject.c @@ -247,7 +247,7 @@ static lua_Number lua_strx2number (const char *s, char **endptr) { nosigdig++; else if (++sigdig <= MAXSIGDIG) /* can read it without overflow? */ r = (r * l_mathop(16.0)) + luaO_hexavalue(*s); - else e++; /* too many digits; ignore, but still count for exponent */ + else e++; /* too many digits; ignore, but still count for exponent */ if (hasdot) e--; /* decimal digit? correct exponent */ } else break; /* neither a dot nor a digit */ @@ -512,18 +512,18 @@ static void initbuff (lua_State *L, BuffFS *buff) { static void pushbuff (lua_State *L, void *ud) { BuffFS *buff = cast(BuffFS*, ud); switch (buff->err) { - case 1: + case 1: /* memory error */ luaD_throw(L, LUA_ERRMEM); break; case 2: /* length overflow: Add "..." at the end of result */ if (buff->buffsize - buff->blen < 3) - strcpy(buff->b + buff->blen - 3, "..."); /* 'blen' must be > 3 */ + strcpy(buff->b + buff->blen - 3, "..."); /* 'blen' must be > 3 */ else { /* there is enough space left for the "..." */ strcpy(buff->b + buff->blen, "..."); buff->blen += 3; } /* FALLTHROUGH */ - default: { /* no errors */ + default: { /* no errors, but it can raise one creating the new string */ TString *ts = luaS_newlstr(L, buff->b, buff->blen); setsvalue2s(L, L->top.p, ts); L->top.p++; diff --git a/manual/manual.of b/manual/manual.of index 274799e3f1..a55c7b49cb 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -6347,7 +6347,7 @@ Opens all standard Lua libraries into the given state. @APIEntry{void luaL_openselectedlibs (lua_State *L, int load, int preload);| @apii{0,0,e} -Opens (loads) and preloads selected libraries into the state @id{L}. +Opens (loads) and preloads selected standard libraries into the state @id{L}. (To @emph{preload} means to add the library loader into the table @Lid{package.preload}, so that the library can be required later by the program. diff --git a/testes/errors.lua b/testes/errors.lua index 027e1b03af..adc111fd3f 100644 --- a/testes/errors.lua +++ b/testes/errors.lua @@ -45,7 +45,7 @@ end -- test error message with no extra info assert(doit("error('hi', 0)") == 'hi') --- test error message with no info +-- test nil error message assert(doit("error()") == nil) @@ -555,7 +555,7 @@ if not _soft then -- error in error handling local res, msg = xpcall(error, error) - assert(not res and type(msg) == 'string') + assert(not res and msg == 'error in error handling') print('+') local function f (x) @@ -586,6 +586,27 @@ if not _soft then end +do -- errors in error handle that not necessarily go forever + local function err (n) -- function to be used as message handler + -- generate an error unless n is zero, so that there is a limited + -- loop of errors + if type(n) ~= "number" then -- some other error? + return n -- report it + elseif n == 0 then + return "END" -- that will be the final message + else error(n - 1) -- does the loop + end + end + + local res, msg = xpcall(error, err, 170) + assert(not res and msg == "END") + + -- too many levels + local res, msg = xpcall(error, err, 300) + assert(not res and msg == "C stack overflow") +end + + do -- non string messages local t = {} diff --git a/testes/main.lua b/testes/main.lua index 1aa7b21771..e0e9cbe8a4 100644 --- a/testes/main.lua +++ b/testes/main.lua @@ -310,8 +310,11 @@ checkprogout("ZYX)\nXYZ)\n") -- bug since 5.2: finalizer called when closing a state could -- subvert finalization order prepfile[[ --- should be called last +-- ensure tables will be collected only at the end of the program +collectgarbage"stop" + print("creating 1") +-- this finalizer should be called last setmetatable({}, {__gc = function () print(1) end}) print("creating 2") diff --git a/testes/sort.lua b/testes/sort.lua index 965e153482..290b199ee5 100644 --- a/testes/sort.lua +++ b/testes/sort.lua @@ -199,7 +199,7 @@ do __index = function (_,k) pos1 = k end, __newindex = function (_,k) pos2 = k; error() end, }) local st, msg = pcall(table.move, a, f, e, t) - assert(not st and not msg and pos1 == x and pos2 == y) + assert(not st and pos1 == x and pos2 == y) end checkmove(1, maxI, 0, 1, 0) checkmove(0, maxI - 1, 1, maxI - 1, maxI) From f9e35627ed26dff4114a1d01ff113d8b4cc91ab5 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 26 Feb 2025 11:31:10 -0300 Subject: [PATCH 1009/1145] 'lua_State.nci' must be an integer Lua can easily overflow an unsigned short counting nested calls. (The limit to this value is the maximum stack size, LUAI_MAXSTACK, which is currently 1e6.) --- lstate.h | 2 +- ltests.h | 7 +++++-- testes/coroutine.lua | 12 ++++++++++++ 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/lstate.h b/lstate.h index 050fc35f52..f841c2321e 100644 --- a/lstate.h +++ b/lstate.h @@ -290,7 +290,6 @@ struct lua_State { CommonHeader; lu_byte allowhook; TStatus status; - unsigned short nci; /* number of items in 'ci' list */ StkIdRel top; /* first free slot in the stack */ struct global_State *l_G; CallInfo *ci; /* call info for current function */ @@ -306,6 +305,7 @@ struct lua_State { ptrdiff_t errfunc; /* current error handling function (stack index) */ l_uint32 nCcalls; /* number of nested non-yieldable or C calls */ int oldpc; /* last pc traced */ + int nci; /* number of items in 'ci' list */ int basehookcount; int hookcount; volatile l_signalT hookmask; diff --git a/ltests.h b/ltests.h index df72307a04..cc372b8f4b 100644 --- a/ltests.h +++ b/ltests.h @@ -152,9 +152,12 @@ LUA_API void *debug_realloc (void *ud, void *block, */ -/* make stack-overflow tests run faster */ +/* +** Reduce maximum stack size to make stack-overflow tests run faster. +** (But value is still large enough to overflow smaller integers.) +*/ #undef LUAI_MAXSTACK -#define LUAI_MAXSTACK 50000 +#define LUAI_MAXSTACK 68000 /* test mode uses more stack space */ diff --git a/testes/coroutine.lua b/testes/coroutine.lua index 78b9bdca19..abc08039d2 100644 --- a/testes/coroutine.lua +++ b/testes/coroutine.lua @@ -127,6 +127,18 @@ assert(#a == 22 and a[#a] == 79) x, a = nil +do -- "bug" in 5.4.2 + local function foo () foo () end -- just create a stack overflow + local co = coroutine.create(foo) + -- running this coroutine would overflow the unsigned short 'nci', the + -- counter of CallInfo structures available to the thread. + -- (The issue only manifests in an 'assert'.) + local st, msg = coroutine.resume(co) + assert(string.find(msg, "stack overflow")) + assert(coroutine.status(co) == "dead") +end + + print("to-be-closed variables in coroutines") local function func2close (f) From 127a8e80fe0d74efd26994b3877cdc77b712ea56 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 28 Feb 2025 10:10:27 -0300 Subject: [PATCH 1010/1145] '__close' gets no error object if there is no error Instead of receiving nil as a second argument, __close metamethods are called with just one argument when there are no errors. --- ldo.c | 4 ---- lfunc.c | 32 ++++++++++++++++++++------------ manual/manual.of | 7 ++++--- testes/locals.lua | 44 ++++++++++++++++++++++++++++++++++++-------- 4 files changed, 60 insertions(+), 27 deletions(-) diff --git a/ldo.c b/ldo.c index 4705b26c17..3ddc5a4c04 100644 --- a/ldo.c +++ b/ldo.c @@ -111,10 +111,6 @@ void luaD_seterrorobj (lua_State *L, TStatus errcode, StkId oldtop) { setsvalue2s(L, oldtop, luaS_newliteral(L, "error in error handling")); break; } - case LUA_OK: { /* special case only for closing upvalues */ - setnilvalue(s2v(oldtop)); /* no error message */ - break; - } default: { lua_assert(errorstatus(errcode)); /* real error */ setobjs2s(L, oldtop, L->top.p - 1); /* error message on current top */ diff --git a/lfunc.c b/lfunc.c index d6853ff82f..c62a5d2395 100644 --- a/lfunc.c +++ b/lfunc.c @@ -100,21 +100,23 @@ UpVal *luaF_findupval (lua_State *L, StkId level) { /* -** Call closing method for object 'obj' with error message 'err'. The +** Call closing method for object 'obj' with error object 'err'. The ** boolean 'yy' controls whether the call is yieldable. ** (This function assumes EXTRA_STACK.) */ static void callclosemethod (lua_State *L, TValue *obj, TValue *err, int yy) { StkId top = L->top.p; + StkId func = top; const TValue *tm = luaT_gettmbyobj(L, obj, TM_CLOSE); - setobj2s(L, top, tm); /* will call metamethod... */ - setobj2s(L, top + 1, obj); /* with 'self' as the 1st argument */ - setobj2s(L, top + 2, err); /* and error msg. as 2nd argument */ - L->top.p = top + 3; /* add function and arguments */ + setobj2s(L, top++, tm); /* will call metamethod... */ + setobj2s(L, top++, obj); /* with 'self' as the 1st argument */ + if (err != NULL) /* if there was an error... */ + setobj2s(L, top++, err); /* then error object will be 2nd argument */ + L->top.p = top; /* add function and arguments */ if (yy) - luaD_call(L, top, 0); + luaD_call(L, func, 0); else - luaD_callnoyield(L, top, 0); + luaD_callnoyield(L, func, 0); } @@ -144,11 +146,17 @@ static void prepcallclosemth (lua_State *L, StkId level, TStatus status, int yy) { TValue *uv = s2v(level); /* value being closed */ TValue *errobj; - if (status == CLOSEKTOP) - errobj = &G(L)->nilvalue; /* error object is nil */ - else { /* 'luaD_seterrorobj' will set top to level + 2 */ - errobj = s2v(level + 1); /* error object goes after 'uv' */ - luaD_seterrorobj(L, status, level + 1); /* set error object */ + switch (status) { + case LUA_OK: + L->top.p = level + 1; /* call will be at this level */ + /* FALLTHROUGH */ + case CLOSEKTOP: /* don't need to change top */ + errobj = NULL; /* no error object */ + break; + default: /* 'luaD_seterrorobj' will set top to level + 2 */ + errobj = s2v(level + 1); /* error object goes after 'uv' */ + luaD_seterrorobj(L, status, level + 1); /* set error object */ + break; } callclosemethod(L, uv, errobj, yy); } diff --git a/manual/manual.of b/manual/manual.of index a55c7b49cb..ff4e79feb9 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -1612,10 +1612,11 @@ or exiting by an error. Here, to @emph{close} a value means to call its @idx{__close} metamethod. When calling the metamethod, -the value itself is passed as the first argument -and the error object that caused the exit (if any) +the value itself is passed as the first argument. +If there was an error, +the error object that caused the exit is passed as a second argument; -if there was no error, the second argument is @nil. +otherwise, there is no second argument. The value assigned to a to-be-closed variable must have a @idx{__close} metamethod diff --git a/testes/locals.lua b/testes/locals.lua index 090d846bef..910deb8a52 100644 --- a/testes/locals.lua +++ b/testes/locals.lua @@ -280,6 +280,32 @@ do end +do -- testing presence of second argument + local function foo (howtoclose, obj, n) + local ca -- copy of 'a' visible inside its close metamethod + do + local a = func2close(function (...) + local t = table.pack(...) + assert(select("#", ...) == n) + assert(t.n == n and t[1] == ca and (t.n < 2 or t[2] == obj)) + ca = 15 -- final value to be returned if howtoclose=="scope" + end) + ca = a + if howtoclose == "ret" then return obj -- 'a' closed by return + elseif howtoclose == "err" then error(obj) -- 'a' closed by error + end + end -- 'a' closed by end of scope + return ca -- ca now should be 15 + end + -- with no errors, closing methods receive no extra argument + assert(foo("scope", nil, 1) == 15) -- close by end of scope + assert(foo("ret", 32, 1) == 32) -- close by return + -- with errors, they do + local st, msg = pcall(foo, "err", 23, 2) -- close by error + assert(not st and msg == 23) +end + + -- testing to-be-closed x compile-time constants -- (there were some bugs here in Lua 5.4-rc3, due to a confusion -- between compile levels and stack levels of variables) @@ -865,8 +891,10 @@ do if extra then extrares = co() -- runs until first (extra) yield end - local res = table.pack(co()) -- runs until yield inside '__close' - assert(res.n == 2 and res[2] == nil) + local res = table.pack(co()) -- runs until "regular" yield + -- regular yield will yield all values passed to the close function; + -- without errors, that is only the object being closed. + assert(res.n == 1 and type(res[1]) == "table") local res2 = table.pack(co()) -- runs until end of function assert(res2.n == t.n) for i = 1, #t do @@ -879,10 +907,10 @@ do end local function foo () - local x = func2close(coroutine.yield) + local x = func2close(coroutine.yield) -- "regular" yield local extra = func2close(function (self) assert(self == extrares) - coroutine.yield(100) + coroutine.yield(100) -- first (extra) yield end) extrares = extra return table.unpack{10, x, 30} @@ -891,21 +919,21 @@ do assert(extrares == 100) local function foo () - local x = func2close(coroutine.yield) + local x = func2close(coroutine.yield) -- "regular" yield return end check(foo, false) local function foo () - local x = func2close(coroutine.yield) + local x = func2close(coroutine.yield) -- "regular" yield local y, z = 20, 30 return x end check(foo, false, "x") local function foo () - local x = func2close(coroutine.yield) - local extra = func2close(coroutine.yield) + local x = func2close(coroutine.yield) -- "regular" yield + local extra = func2close(coroutine.yield) -- extra yield return table.unpack({}, 1, 100) -- 100 nils end check(foo, true, table.unpack({}, 1, 100)) From ee99452158de5e2fa804bd10de7669848f3b3952 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 28 Feb 2025 14:53:58 -0300 Subject: [PATCH 1011/1145] Error object cannot be nil Lua will change a nil as error object to a string message, so that it never reports an error with nil as the error object. --- ldebug.c | 4 +++- ldo.c | 10 +++++++--- manual/manual.of | 11 ++++++++++- testes/errors.lua | 6 +++--- 4 files changed, 23 insertions(+), 8 deletions(-) diff --git a/ldebug.c b/ldebug.c index af3b758334..18bdc5959c 100644 --- a/ldebug.c +++ b/ldebug.c @@ -844,7 +844,9 @@ l_noret luaG_runerror (lua_State *L, const char *fmt, ...) { va_start(argp, fmt); msg = luaO_pushvfstring(L, fmt, argp); /* format message */ va_end(argp); - if (msg != NULL && isLua(ci)) { /* Lua function? (and no error) */ + if (msg == NULL) /* no memory to format message? */ + luaD_throw(L, LUA_ERRMEM); + else if (isLua(ci)) { /* Lua function? */ /* add source:line information */ luaG_addinfo(L, msg, ci_func(ci)->p->source, getcurrentline(ci)); setobjs2s(L, L->top.p - 2, L->top.p - 1); /* remove 'msg' */ diff --git a/ldo.c b/ldo.c index 3ddc5a4c04..84f7bbb2c4 100644 --- a/ldo.c +++ b/ldo.c @@ -112,12 +112,16 @@ void luaD_seterrorobj (lua_State *L, TStatus errcode, StkId oldtop) { break; } default: { - lua_assert(errorstatus(errcode)); /* real error */ - setobjs2s(L, oldtop, L->top.p - 1); /* error message on current top */ + lua_assert(errorstatus(errcode)); /* must be a real error */ + if (!ttisnil(s2v(L->top.p - 1))) { /* error object is not nil? */ + setobjs2s(L, oldtop, L->top.p - 1); /* move it to 'oldtop' */ + } + else /* change it to a proper message */ + setsvalue2s(L, oldtop, luaS_newliteral(L, "")); break; } } - L->top.p = oldtop + 1; + L->top.p = oldtop + 1; /* top goes back to old top plus error object */ } diff --git a/manual/manual.of b/manual/manual.of index ff4e79feb9..b34e1e9cb7 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -290,7 +290,9 @@ an @def{error object} is propagated with information about the error. Lua itself only generates errors whose error object is a string, but programs can generate errors with -any value as the error object. +any value as the error object, +except @nil. +(Lua will change a @nil as error object to a string message.) It is up to the Lua program or its host to handle such error objects. For historical reasons, an error object is often called an @def{error message}, @@ -8082,6 +8084,8 @@ multiple assignment: The default for @id{a2} is @id{a1}. The destination range can overlap with the source range. The number of elements to be moved must fit in a Lua integer. +If @id{f} is larger than @id{e}, +nothing is moved. Returns the destination table @id{a2}. @@ -9402,6 +9406,11 @@ declare a local variable with the same name in the loop body. A chain of @id{__call} metamethods can have at most 15 objects. } +@item{ +In an error, a @nil as the error object is replaced by a +string message. +} + } } diff --git a/testes/errors.lua b/testes/errors.lua index adc111fd3f..5fdb772263 100644 --- a/testes/errors.lua +++ b/testes/errors.lua @@ -46,7 +46,7 @@ end assert(doit("error('hi', 0)") == 'hi') -- test nil error message -assert(doit("error()") == nil) +assert(doit("error()") == "") -- test common errors/errors that crashed in the past @@ -614,7 +614,7 @@ do assert(not res and msg == t) res, msg = pcall(function () error(nil) end) - assert(not res and msg == nil) + assert(not res and msg == "") local function f() error{msg='x'} end res, msg = xpcall(f, function (r) return {msg=r.msg..'y'} end) @@ -634,7 +634,7 @@ do assert(not res and msg == t) res, msg = pcall(assert, nil, nil) - assert(not res and msg == nil) + assert(not res and type(msg) == "string") -- 'assert' without arguments res, msg = pcall(assert) From cb88c1cd5d22fe7c56f4f374ded7c16f7cf14bf3 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 28 Feb 2025 15:48:45 -0300 Subject: [PATCH 1012/1145] Detail Added macro LUA_FAILISFALSE to make easier to change the fail value from nil to false. --- lauxlib.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lauxlib.h b/lauxlib.h index 4be008b90d..d8522098a7 100644 --- a/lauxlib.h +++ b/lauxlib.h @@ -165,7 +165,11 @@ LUALIB_API void (luaL_requiref) (lua_State *L, const char *modname, /* push the value used to represent failure/error */ +#if defined(LUA_FAILISFALSE) +#define luaL_pushfail(L) lua_pushboolean(L, 0) +#else #define luaL_pushfail(L) lua_pushnil(L) +#endif From b5b1995f2925b2f9be4a48304ac97a38f8608648 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 10 Mar 2025 15:21:32 -0300 Subject: [PATCH 1013/1145] Checks for type 'int' added to binary header The structure 'AbsLineInfo' is hard-dumped into binary chunks, and it comprises two 'int' fields. --- ldump.c | 13 ++++++++----- lundump.c | 38 ++++++++++++++++++++++++++------------ lundump.h | 5 +++-- testes/calls.lua | 47 ++++++++++++++++++++++++++++++----------------- 4 files changed, 67 insertions(+), 36 deletions(-) diff --git a/ldump.c b/ldump.c index 71d9a5b1c9..54f96674e1 100644 --- a/ldump.c +++ b/ldump.c @@ -253,16 +253,19 @@ static void dumpFunction (DumpState *D, const Proto *f) { } +#define dumpNumInfo(D, tvar, value) \ + { tvar i = value; dumpByte(D, sizeof(tvar)); dumpVar(D, i); } + + static void dumpHeader (DumpState *D) { dumpLiteral(D, LUA_SIGNATURE); dumpByte(D, LUAC_VERSION); dumpByte(D, LUAC_FORMAT); dumpLiteral(D, LUAC_DATA); - dumpByte(D, sizeof(Instruction)); - dumpByte(D, sizeof(lua_Integer)); - dumpByte(D, sizeof(lua_Number)); - dumpInteger(D, LUAC_INT); - dumpNumber(D, LUAC_NUM); + dumpNumInfo(D, int, LUAC_INT); + dumpNumInfo(D, Instruction, LUAC_INST); + dumpNumInfo(D, lua_Integer, LUAC_INT); + dumpNumInfo(D, lua_Number, LUAC_NUM); } diff --git a/lundump.c b/lundump.c index d074a0734e..d53bfc9a99 100644 --- a/lundump.c +++ b/lundump.c @@ -345,13 +345,29 @@ static void checkliteral (LoadState *S, const char *s, const char *msg) { } -static void fchecksize (LoadState *S, size_t size, const char *tname) { - if (loadByte(S) != size) - error(S, luaO_pushfstring(S->L, "%s size mismatch", tname)); +static l_noret numerror (LoadState *S, const char *what, const char *tname) { + const char *msg = luaO_pushfstring(S->L, "%s %s mismatch", tname, what); + error(S, msg); } -#define checksize(S,t) fchecksize(S,sizeof(t),#t) +static void checknumsize (LoadState *S, int size, const char *tname) { + if (size != loadByte(S)) + numerror(S, "size", tname); +} + + +static void checknumformat (LoadState *S, int eq, const char *tname) { + if (!eq) + numerror(S, "format", tname); +} + + +#define checknum(S,tvar,value,tname) \ + { tvar i; checknumsize(S, sizeof(i), tname); \ + loadVar(S, i); \ + checknumformat(S, i == value, tname); } + static void checkHeader (LoadState *S) { /* skip 1st char (already read and checked) */ @@ -361,13 +377,10 @@ static void checkHeader (LoadState *S) { if (loadByte(S) != LUAC_FORMAT) error(S, "format mismatch"); checkliteral(S, LUAC_DATA, "corrupted chunk"); - checksize(S, Instruction); - checksize(S, lua_Integer); - checksize(S, lua_Number); - if (loadInteger(S) != LUAC_INT) - error(S, "integer format mismatch"); - if (loadNumber(S) != LUAC_NUM) - error(S, "float format mismatch"); + checknum(S, int, LUAC_INT, "int"); + checknum(S, Instruction, LUAC_INST, "instruction"); + checknum(S, lua_Integer, LUAC_INT, "Lua integer"); + checknum(S, lua_Number, LUAC_NUM, "Lua number"); } @@ -398,7 +411,8 @@ LClosure *luaU_undump (lua_State *L, ZIO *Z, const char *name, int fixed) { cl->p = luaF_newproto(L); luaC_objbarrier(L, cl, cl->p); loadFunction(&S, cl->p); - lua_assert(cl->nupvalues == cl->p->sizeupvalues); + if (cl->nupvalues != cl->p->sizeupvalues) + error(&S, "corrupted chunk"); luai_verifycode(L, cl->p); L->top.p--; /* pop table */ return cl; diff --git a/lundump.h b/lundump.h index 1d6e50ea84..c4e06f9ebd 100644 --- a/lundump.h +++ b/lundump.h @@ -17,8 +17,9 @@ /* data to catch conversion errors */ #define LUAC_DATA "\x19\x93\r\n\x1a\n" -#define LUAC_INT 0x5678 -#define LUAC_NUM cast_num(370.5) +#define LUAC_INT -0x5678 +#define LUAC_INST 0x12345678 +#define LUAC_NUM cast_num(-370.5) /* ** Encode major-minor version in one byte, one nibble for each diff --git a/testes/calls.lua b/testes/calls.lua index 310282157d..8b35595768 100644 --- a/testes/calls.lua +++ b/testes/calls.lua @@ -480,15 +480,22 @@ assert((function (a) return a end)() == nil) print("testing binary chunks") do - local header = string.pack("c4BBc6BBB", - "\27Lua", -- signature - 0x55, -- version 5.5 (0x55) - 0, -- format - "\x19\x93\r\n\x1a\n", -- data - 4, -- size of instruction - string.packsize("j"), -- sizeof(lua integer) - string.packsize("n") -- sizeof(lua number) - ) + local headformat = "c4BBc6BiBI4BjBn" + local header = { -- header components + "\27Lua", -- signature + 0x55, -- version 5.5 (0x55) + 0, -- format + "\x19\x93\r\n\x1a\n", -- a binary string + string.packsize("i"), -- size of an int + -0x5678, -- an int + 4, -- size of an instruction + 0x12345678, -- an instruction (4 bytes) + string.packsize("j"), -- size of a Lua integer + -0x5678, -- a Lua integer + string.packsize("n"), -- size of a Lua float + -370.5, -- a Lua float + } + local c = string.dump(function () local a = 1; local b = 3; local f = function () return a + b + _ENV.c; end -- upvalues @@ -500,17 +507,23 @@ do assert(assert(load(c))() == 10) -- check header - assert(string.sub(c, 1, #header) == header) - -- check LUAC_INT and LUAC_NUM - local ci, cn = string.unpack("jn", c, #header + 1) - assert(ci == 0x5678 and cn == 370.5) - - -- corrupted header + local t = {string.unpack(headformat, c)} for i = 1, #header do + assert(t[i] == header[i]) + end + + -- Testing corrupted header. + -- A single wrong byte in the head invalidates the chunk, + -- except for the Lua float check. (If numbers are long double, + -- the representation may need padding, and changing that padding + -- will not invalidate the chunk.) + local headlen = string.packsize(headformat) + headlen = headlen - string.packsize("n") -- remove float check + for i = 1, headlen do local s = string.sub(c, 1, i - 1) .. - string.char(string.byte(string.sub(c, i, i)) + 1) .. + string.char((string.byte(string.sub(c, i, i)) + 1) & 0xFF) .. string.sub(c, i + 1, -1) - assert(#s == #c) + assert(#s == #c and s ~= c) assert(not load(s)) end From 808976bb59d91a031d9832b5482a9fb5a41faee3 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 12 Mar 2025 12:35:36 -0300 Subject: [PATCH 1014/1145] Small correction in 'traverseweakvalue' After a weak table is traversed in the atomic phase, if it does not have white values ('hasclears') it does not need to be retraversed again. (Comments were correct, but code did not agree with them.) --- lgc.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lgc.c b/lgc.c index e853305211..cada07d9a1 100644 --- a/lgc.c +++ b/lgc.c @@ -497,10 +497,10 @@ static void traverseweakvalue (global_State *g, Table *h) { hasclears = 1; /* table will have to be cleared */ } } - if (g->gcstate == GCSatomic && hasclears) - linkgclist(h, g->weak); /* has to be cleared later */ - else + if (g->gcstate == GCSpropagate) linkgclist(h, g->grayagain); /* must retraverse it in atomic phase */ + else if (hasclears) + linkgclist(h, g->weak); /* has to be cleared later */ } From 4398e488e678decd06a5ca48a27751d509361405 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 12 Mar 2025 13:52:35 -0300 Subject: [PATCH 1015/1145] New test file 'memerr.lua' Tests for memory-allocation errors moved from 'api.lua' to this new file, as 'api.lua' was already too big. (Besides, these tests have nothing to do with the API.) --- testes/all.lua | 1 + testes/api.lua | 243 ------------------------------------------ testes/memerr.lua | 266 ++++++++++++++++++++++++++++++++++++++++++++++ testes/packtests | 1 + 4 files changed, 268 insertions(+), 243 deletions(-) create mode 100644 testes/memerr.lua diff --git a/testes/all.lua b/testes/all.lua index 4ffa9efee1..c3fdac957c 100644 --- a/testes/all.lua +++ b/testes/all.lua @@ -182,6 +182,7 @@ dofile('nextvar.lua') dofile('pm.lua') dofile('utf8.lua') dofile('api.lua') +dofile('memerr.lua') assert(dofile('events.lua') == 12) dofile('vararg.lua') dofile('closure.lua') diff --git a/testes/api.lua b/testes/api.lua index 21f703fd17..ee2de98bdb 100644 --- a/testes/api.lua +++ b/testes/api.lua @@ -11,9 +11,6 @@ local debug = require "debug" local pack = table.pack --- standard error message for memory errors -local MEMERRMSG = "not enough memory" - local function tcheck (t1, t2) assert(t1.n == (t2.n or #t2) + 1) for i = 2, t1.n do assert(t1[i] == t2[i - 1]) end @@ -432,11 +429,6 @@ do "bad argument #4 (string expected, got no value)") - -- memory error - T.totalmem(T.totalmem()+10000) -- set low memory limit (+10k) - assert(T.checkpanic("newuserdata 20000") == MEMERRMSG) - T.totalmem(0) -- restore high limit - -- memory error + thread status local x = T.checkpanic( [[ alloccount 0 # force a memory error in next line @@ -1306,241 +1298,6 @@ do end ---[[ -** {================================================================== -** Testing memory limits -** =================================================================== ---]] - -print("memory-allocation errors") - -checkerr("block too big", T.newuserdata, math.maxinteger) -collectgarbage() -local f = load"local a={}; for i=1,100000 do a[i]=i end" -T.alloccount(10) -checkerr(MEMERRMSG, f) -T.alloccount() -- remove limit - - --- test memory errors; increase limit for maximum memory by steps, --- o that we get memory errors in all allocations of a given --- task, until there is enough memory to complete the task without --- errors. -local function testbytes (s, f) - collectgarbage() - local M = T.totalmem() - local oldM = M - local a,b = nil - while true do - collectgarbage(); collectgarbage() - T.totalmem(M) - a, b = T.testC("pcall 0 1 0; pushstatus; return 2", f) - T.totalmem(0) -- remove limit - if a and b == "OK" then break end -- stop when no more errors - if b ~= "OK" and b ~= MEMERRMSG then -- not a memory error? - error(a, 0) -- propagate it - end - M = M + 7 -- increase memory limit - end - print(string.format("minimum memory for %s: %d bytes", s, M - oldM)) - return a -end - --- test memory errors; increase limit for number of allocations one --- by one, so that we get memory errors in all allocations of a given --- task, until there is enough allocations to complete the task without --- errors. - -local function testalloc (s, f) - collectgarbage() - local M = 0 - local a,b = nil - while true do - collectgarbage(); collectgarbage() - T.alloccount(M) - a, b = T.testC("pcall 0 1 0; pushstatus; return 2", f) - T.alloccount() -- remove limit - if a and b == "OK" then break end -- stop when no more errors - if b ~= "OK" and b ~= MEMERRMSG then -- not a memory error? - error(a, 0) -- propagate it - end - M = M + 1 -- increase allocation limit - end - print(string.format("minimum allocations for %s: %d allocations", s, M)) - return a -end - - -local function testamem (s, f) - testalloc(s, f) - return testbytes(s, f) -end - - --- doing nothing -b = testamem("doing nothing", function () return 10 end) -assert(b == 10) - --- testing memory errors when creating a new state - -testamem("state creation", function () - local st = T.newstate() - if st then T.closestate(st) end -- close new state - return st -end) - -testamem("empty-table creation", function () - return {} -end) - -testamem("string creation", function () - return "XXX" .. "YYY" -end) - -testamem("coroutine creation", function() - return coroutine.create(print) -end) - - --- testing to-be-closed variables -testamem("to-be-closed variables", function() - local flag - do - local x = - setmetatable({}, {__close = function () flag = true end}) - flag = false - local x = {} - end - return flag -end) - - --- testing threads - --- get main thread from registry -local mt = T.testC("rawgeti R !M; return 1") -assert(type(mt) == "thread" and coroutine.running() == mt) - - - -local function expand (n,s) - if n==0 then return "" end - local e = string.rep("=", n) - return string.format("T.doonnewstack([%s[ %s;\n collectgarbage(); %s]%s])\n", - e, s, expand(n-1,s), e) -end - -G=0; collectgarbage(); a =collectgarbage("count") -load(expand(20,"G=G+1"))() -assert(G==20); collectgarbage(); -- assert(gcinfo() <= a+1) -G = nil - -testamem("running code on new thread", function () - return T.doonnewstack("local x=1") == 0 -- try to create thread -end) - - --- testing memory x compiler - -testamem("loadstring", function () - return load("x=1") -- try to do load a string -end) - - -local testprog = [[ -local function foo () return end -local t = {"x"} -AA = "aaa" -for i = 1, #t do AA = AA .. t[i] end -return true -]] - --- testing memory x dofile -_G.AA = nil -local t =os.tmpname() -local f = assert(io.open(t, "w")) -f:write(testprog) -f:close() -testamem("dofile", function () - local a = loadfile(t) - return a and a() -end) -assert(os.remove(t)) -assert(_G.AA == "aaax") - - --- other generic tests - -testamem("gsub", function () - local a, b = string.gsub("alo alo", "(a)", function (x) return x..'b' end) - return (a == 'ablo ablo') -end) - -testamem("dump/undump", function () - local a = load(testprog) - local b = a and string.dump(a) - a = b and load(b) - return a and a() -end) - -_G.AA = nil - -local t = os.tmpname() -testamem("file creation", function () - local f = assert(io.open(t, 'w')) - assert (not io.open"nomenaoexistente") - io.close(f); - return not loadfile'nomenaoexistente' -end) -assert(os.remove(t)) - -testamem("table creation", function () - local a, lim = {}, 10 - for i=1,lim do a[i] = i; a[i..'a'] = {} end - return (type(a[lim..'a']) == 'table' and a[lim] == lim) -end) - -testamem("constructors", function () - local a = {10, 20, 30, 40, 50; a=1, b=2, c=3, d=4, e=5} - return (type(a) == 'table' and a.e == 5) -end) - -local a = 1 -local close = nil -testamem("closure creation", function () - function close (b) - return function (x) return b + x end - end - return (close(2)(4) == 6) -end) - -testamem("using coroutines", function () - local a = coroutine.wrap(function () - coroutine.yield(string.rep("a", 10)) - return {} - end) - assert(string.len(a()) == 10) - return a() -end) - -do -- auxiliary buffer - local lim = 100 - local a = {}; for i = 1, lim do a[i] = "01234567890123456789" end - testamem("auxiliary buffer", function () - return (#table.concat(a, ",") == 20*lim + lim - 1) - end) -end - -testamem("growing stack", function () - local function foo (n) - if n == 0 then return 1 else return 1 + foo(n - 1) end - end - return foo(100) -end) - --- }================================================================== - - do -- testing failing in 'lua_checkstack' local res = T.testC([[rawcheckstack 500000; return 1]]) assert(res == false) diff --git a/testes/memerr.lua b/testes/memerr.lua new file mode 100644 index 0000000000..cb236eb976 --- /dev/null +++ b/testes/memerr.lua @@ -0,0 +1,266 @@ +-- $Id: testes/memerr.lua $ +-- See Copyright Notice in file all.lua + + +local function checkerr (msg, f, ...) + local stat, err = pcall(f, ...) + assert(not stat and string.find(err, msg)) +end + +if T==nil then + (Message or print) + ('\n >>> testC not active: skipping memory error tests <<<\n') + return +end + +print("testing memory-allocation errors") + +local debug = require "debug" + +local pack = table.pack + +-- standard error message for memory errors +local MEMERRMSG = "not enough memory" + + +-- memory error in panic function +T.totalmem(T.totalmem()+10000) -- set low memory limit (+10k) +assert(T.checkpanic("newuserdata 20000") == MEMERRMSG) +T.totalmem(0) -- restore high limit + + + +-- {================================================================== +-- Testing memory limits +-- =================================================================== + +checkerr("block too big", T.newuserdata, math.maxinteger) +collectgarbage() +local f = load"local a={}; for i=1,100000 do a[i]=i end" +T.alloccount(10) +checkerr(MEMERRMSG, f) +T.alloccount() -- remove limit + + +-- test memory errors; increase limit for maximum memory by steps, +-- o that we get memory errors in all allocations of a given +-- task, until there is enough memory to complete the task without +-- errors. +local function testbytes (s, f) + collectgarbage() + local M = T.totalmem() + local oldM = M + local a,b = nil + while true do + collectgarbage(); collectgarbage() + T.totalmem(M) + a, b = T.testC("pcall 0 1 0; pushstatus; return 2", f) + T.totalmem(0) -- remove limit + if a and b == "OK" then break end -- stop when no more errors + if b ~= "OK" and b ~= MEMERRMSG then -- not a memory error? + error(a, 0) -- propagate it + end + M = M + 7 -- increase memory limit + end + print(string.format("minimum memory for %s: %d bytes", s, M - oldM)) + return a +end + +-- test memory errors; increase limit for number of allocations one +-- by one, so that we get memory errors in all allocations of a given +-- task, until there is enough allocations to complete the task without +-- errors. + +local function testalloc (s, f) + collectgarbage() + local M = 0 + local a,b = nil + while true do + collectgarbage(); collectgarbage() + T.alloccount(M) + a, b = T.testC("pcall 0 1 0; pushstatus; return 2", f) + T.alloccount() -- remove limit + if a and b == "OK" then break end -- stop when no more errors + if b ~= "OK" and b ~= MEMERRMSG then -- not a memory error? + error(a, 0) -- propagate it + end + M = M + 1 -- increase allocation limit + end + print(string.format("minimum allocations for %s: %d allocations", s, M)) + return a +end + + +local function testamem (s, f) + testalloc(s, f) + return testbytes(s, f) +end + + +-- doing nothing +b = testamem("doing nothing", function () return 10 end) +assert(b == 10) + +-- testing memory errors when creating a new state + +testamem("state creation", function () + local st = T.newstate() + if st then T.closestate(st) end -- close new state + return st +end) + +testamem("empty-table creation", function () + return {} +end) + +testamem("string creation", function () + return "XXX" .. "YYY" +end) + +testamem("coroutine creation", function() + return coroutine.create(print) +end) + + +-- testing to-be-closed variables +testamem("to-be-closed variables", function() + local flag + do + local x = + setmetatable({}, {__close = function () flag = true end}) + flag = false + local x = {} + end + return flag +end) + + +-- testing threads + +-- get main thread from registry +local mt = T.testC("rawgeti R !M; return 1") +assert(type(mt) == "thread" and coroutine.running() == mt) + + + +local function expand (n,s) + if n==0 then return "" end + local e = string.rep("=", n) + return string.format("T.doonnewstack([%s[ %s;\n collectgarbage(); %s]%s])\n", + e, s, expand(n-1,s), e) +end + +G=0; collectgarbage(); a =collectgarbage("count") +load(expand(20,"G=G+1"))() +assert(G==20); collectgarbage(); -- assert(gcinfo() <= a+1) +G = nil + +testamem("running code on new thread", function () + return T.doonnewstack("local x=1") == 0 -- try to create thread +end) + + +-- testing memory x compiler + +testamem("loadstring", function () + return load("x=1") -- try to do load a string +end) + + +local testprog = [[ +local function foo () return end +local t = {"x"} +AA = "aaa" +for i = 1, #t do AA = AA .. t[i] end +return true +]] + +-- testing memory x dofile +_G.AA = nil +local t =os.tmpname() +local f = assert(io.open(t, "w")) +f:write(testprog) +f:close() +testamem("dofile", function () + local a = loadfile(t) + return a and a() +end) +assert(os.remove(t)) +assert(_G.AA == "aaax") + + +-- other generic tests + +testamem("gsub", function () + local a, b = string.gsub("alo alo", "(a)", function (x) return x..'b' end) + return (a == 'ablo ablo') +end) + +testamem("dump/undump", function () + local a = load(testprog) + local b = a and string.dump(a) + a = b and load(b) + return a and a() +end) + +_G.AA = nil + +local t = os.tmpname() +testamem("file creation", function () + local f = assert(io.open(t, 'w')) + assert (not io.open"nomenaoexistente") + io.close(f); + return not loadfile'nomenaoexistente' +end) +assert(os.remove(t)) + +testamem("table creation", function () + local a, lim = {}, 10 + for i=1,lim do a[i] = i; a[i..'a'] = {} end + return (type(a[lim..'a']) == 'table' and a[lim] == lim) +end) + +testamem("constructors", function () + local a = {10, 20, 30, 40, 50; a=1, b=2, c=3, d=4, e=5} + return (type(a) == 'table' and a.e == 5) +end) + +local a = 1 +local close = nil +testamem("closure creation", function () + function close (b) + return function (x) return b + x end + end + return (close(2)(4) == 6) +end) + +testamem("using coroutines", function () + local a = coroutine.wrap(function () + coroutine.yield(string.rep("a", 10)) + return {} + end) + assert(string.len(a()) == 10) + return a() +end) + +do -- auxiliary buffer + local lim = 100 + local a = {}; for i = 1, lim do a[i] = "01234567890123456789" end + testamem("auxiliary buffer", function () + return (#table.concat(a, ",") == 20*lim + lim - 1) + end) +end + +testamem("growing stack", function () + local function foo (n) + if n == 0 then return 1 else return 1 + foo(n - 1) end + end + return foo(100) +end) + +-- }================================================================== + + +print "Ok" + + diff --git a/testes/packtests b/testes/packtests index 0dbb92fe5d..855c054a0c 100755 --- a/testes/packtests +++ b/testes/packtests @@ -28,6 +28,7 @@ $NAME/literals.lua \ $NAME/locals.lua \ $NAME/main.lua \ $NAME/math.lua \ +$NAME/memerr.lua \ $NAME/nextvar.lua \ $NAME/pm.lua \ $NAME/sort.lua \ From ab66652b3270b95222dea134b5e47bb3afc434cc Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 12 Mar 2025 14:00:58 -0300 Subject: [PATCH 1016/1145] Removed copyright notice from 'testes/all.lua' All test files refer to the main copyright notice in 'lua.h'. --- testes/all.lua | 29 +---------------------------- testes/api.lua | 2 +- testes/attrib.lua | 2 +- testes/big.lua | 2 +- testes/bitwise.lua | 2 +- testes/calls.lua | 2 +- testes/closure.lua | 2 +- testes/code.lua | 2 +- testes/constructs.lua | 2 +- testes/coroutine.lua | 2 +- testes/cstack.lua | 2 +- testes/db.lua | 2 +- testes/errors.lua | 2 +- testes/events.lua | 2 +- testes/files.lua | 2 +- testes/gc.lua | 2 +- testes/gengc.lua | 2 +- testes/goto.lua | 2 +- testes/heavy.lua | 4 ++-- testes/literals.lua | 2 +- testes/locals.lua | 2 +- testes/main.lua | 2 +- testes/math.lua | 2 +- testes/memerr.lua | 2 +- testes/nextvar.lua | 2 +- testes/pm.lua | 2 +- testes/sort.lua | 2 +- testes/strings.lua | 2 +- testes/tpack.lua | 2 +- testes/utf8.lua | 2 +- testes/vararg.lua | 2 +- testes/verybig.lua | 2 +- 32 files changed, 33 insertions(+), 60 deletions(-) diff --git a/testes/all.lua b/testes/all.lua index c3fdac957c..5c7ebfa5bf 100644 --- a/testes/all.lua +++ b/testes/all.lua @@ -1,6 +1,6 @@ #!../lua -- $Id: testes/all.lua $ --- See Copyright Notice at the end of this file +-- See Copyright Notice in file lua.h local version = "Lua 5.5" @@ -283,30 +283,3 @@ end print("final OK !!!") - - ---[[ -***************************************************************************** -* Copyright (C) 1994-2016 Lua.org, PUC-Rio. -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be -* included in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -***************************************************************************** -]] - diff --git a/testes/api.lua b/testes/api.lua index ee2de98bdb..49e3f9b987 100644 --- a/testes/api.lua +++ b/testes/api.lua @@ -1,5 +1,5 @@ -- $Id: testes/api.lua $ --- See Copyright Notice in file all.lua +-- See Copyright Notice in file lua.h if T==nil then (Message or print)('\n >>> testC not active: skipping API tests <<<\n') diff --git a/testes/attrib.lua b/testes/attrib.lua index 9054e0b64d..d8b6e0f3f2 100644 --- a/testes/attrib.lua +++ b/testes/attrib.lua @@ -1,5 +1,5 @@ -- $Id: testes/attrib.lua $ --- See Copyright Notice in file all.lua +-- See Copyright Notice in file lua.h print "testing require" diff --git a/testes/big.lua b/testes/big.lua index 46fd846674..119caa6c32 100644 --- a/testes/big.lua +++ b/testes/big.lua @@ -1,5 +1,5 @@ -- $Id: testes/big.lua $ --- See Copyright Notice in file all.lua +-- See Copyright Notice in file lua.h if _soft then return 'a' diff --git a/testes/bitwise.lua b/testes/bitwise.lua index dd0a1a9a39..10afff432f 100644 --- a/testes/bitwise.lua +++ b/testes/bitwise.lua @@ -1,5 +1,5 @@ -- $Id: testes/bitwise.lua $ --- See Copyright Notice in file all.lua +-- See Copyright Notice in file lua.h print("testing bitwise operations") diff --git a/testes/calls.lua b/testes/calls.lua index 8b35595768..942fad72e0 100644 --- a/testes/calls.lua +++ b/testes/calls.lua @@ -1,5 +1,5 @@ -- $Id: testes/calls.lua $ --- See Copyright Notice in file all.lua +-- See Copyright Notice in file lua.h print("testing functions and calls") diff --git a/testes/closure.lua b/testes/closure.lua index 07149ef36a..d3b9f6216a 100644 --- a/testes/closure.lua +++ b/testes/closure.lua @@ -1,5 +1,5 @@ -- $Id: testes/closure.lua $ --- See Copyright Notice in file all.lua +-- See Copyright Notice in file lua.h print "testing closures" diff --git a/testes/code.lua b/testes/code.lua index 50ce7392f3..111717cefe 100644 --- a/testes/code.lua +++ b/testes/code.lua @@ -1,5 +1,5 @@ -- $Id: testes/code.lua $ --- See Copyright Notice in file all.lua +-- See Copyright Notice in file lua.h if T==nil then (Message or print)('\n >>> testC not active: skipping opcode tests <<<\n') diff --git a/testes/constructs.lua b/testes/constructs.lua index 6ac6816671..3f6d506f0a 100644 --- a/testes/constructs.lua +++ b/testes/constructs.lua @@ -1,5 +1,5 @@ -- $Id: testes/constructs.lua $ --- See Copyright Notice in file all.lua +-- See Copyright Notice in file lua.h ;;print "testing syntax";; diff --git a/testes/coroutine.lua b/testes/coroutine.lua index abc08039d2..680fc6058d 100644 --- a/testes/coroutine.lua +++ b/testes/coroutine.lua @@ -1,5 +1,5 @@ -- $Id: testes/coroutine.lua $ --- See Copyright Notice in file all.lua +-- See Copyright Notice in file lua.h print "testing coroutines" diff --git a/testes/cstack.lua b/testes/cstack.lua index 97afe9fd03..0a68a30ac2 100644 --- a/testes/cstack.lua +++ b/testes/cstack.lua @@ -1,5 +1,5 @@ -- $Id: testes/cstack.lua $ --- See Copyright Notice in file all.lua +-- See Copyright Notice in file lua.h local tracegc = require"tracegc" diff --git a/testes/db.lua b/testes/db.lua index 75730d27b0..3c821ab7d5 100644 --- a/testes/db.lua +++ b/testes/db.lua @@ -1,5 +1,5 @@ -- $Id: testes/db.lua $ --- See Copyright Notice in file all.lua +-- See Copyright Notice in file lua.h -- testing debug library diff --git a/testes/errors.lua b/testes/errors.lua index 5fdb772263..8ef267579a 100644 --- a/testes/errors.lua +++ b/testes/errors.lua @@ -1,5 +1,5 @@ -- $Id: testes/errors.lua $ --- See Copyright Notice in file all.lua +-- See Copyright Notice in file lua.h print("testing errors") diff --git a/testes/events.lua b/testes/events.lua index 5360ac301c..2500fbd554 100644 --- a/testes/events.lua +++ b/testes/events.lua @@ -1,5 +1,5 @@ -- $Id: testes/events.lua $ --- See Copyright Notice in file all.lua +-- See Copyright Notice in file lua.h print('testing metatables') diff --git a/testes/files.lua b/testes/files.lua index 9bdf04d09a..05fae49b44 100644 --- a/testes/files.lua +++ b/testes/files.lua @@ -1,5 +1,5 @@ -- $Id: testes/files.lua $ --- See Copyright Notice in file all.lua +-- See Copyright Notice in file lua.h local debug = require "debug" diff --git a/testes/gc.lua b/testes/gc.lua index 09bfe09ab3..96eadad816 100644 --- a/testes/gc.lua +++ b/testes/gc.lua @@ -1,5 +1,5 @@ -- $Id: testes/gc.lua $ --- See Copyright Notice in file all.lua +-- See Copyright Notice in file lua.h print('testing incremental garbage collection') diff --git a/testes/gengc.lua b/testes/gengc.lua index c4f6ca1b71..ea99bdc43a 100644 --- a/testes/gengc.lua +++ b/testes/gengc.lua @@ -1,5 +1,5 @@ -- $Id: testes/gengc.lua $ --- See Copyright Notice in file all.lua +-- See Copyright Notice in file lua.h print('testing generational garbage collection') diff --git a/testes/goto.lua b/testes/goto.lua index 103cccef52..eca6851689 100644 --- a/testes/goto.lua +++ b/testes/goto.lua @@ -1,5 +1,5 @@ -- $Id: testes/goto.lua $ --- See Copyright Notice in file all.lua +-- See Copyright Notice in file lua.h collectgarbage() diff --git a/testes/heavy.lua b/testes/heavy.lua index 4731c7472f..3b4e4ce352 100644 --- a/testes/heavy.lua +++ b/testes/heavy.lua @@ -1,5 +1,5 @@ --- $Id: heavy.lua,v 1.7 2017/12/29 15:42:15 roberto Exp $ --- See Copyright Notice in file all.lua +-- $Id: testes/heavy.lua,v $ +-- See Copyright Notice in file lua.h local function teststring () print("creating a string too long") diff --git a/testes/literals.lua b/testes/literals.lua index 30ab9ab115..28995718b7 100644 --- a/testes/literals.lua +++ b/testes/literals.lua @@ -1,5 +1,5 @@ -- $Id: testes/literals.lua $ --- See Copyright Notice in file all.lua +-- See Copyright Notice in file lua.h print('testing scanner') diff --git a/testes/locals.lua b/testes/locals.lua index 910deb8a52..ccea0a1422 100644 --- a/testes/locals.lua +++ b/testes/locals.lua @@ -1,5 +1,5 @@ -- $Id: testes/locals.lua $ --- See Copyright Notice in file all.lua +-- See Copyright Notice in file lua.h print('testing local variables and environments') diff --git a/testes/main.lua b/testes/main.lua index e0e9cbe8a4..bf3c898eed 100644 --- a/testes/main.lua +++ b/testes/main.lua @@ -1,6 +1,6 @@ # testing special comment on first line -- $Id: testes/main.lua $ --- See Copyright Notice in file all.lua +-- See Copyright Notice in file lua.h -- most (all?) tests here assume a reasonable "Unix-like" shell if _port then return end diff --git a/testes/math.lua b/testes/math.lua index 3937b9ce56..bad8bc5e71 100644 --- a/testes/math.lua +++ b/testes/math.lua @@ -1,5 +1,5 @@ -- $Id: testes/math.lua $ --- See Copyright Notice in file all.lua +-- See Copyright Notice in file lua.h print("testing numbers and math lib") diff --git a/testes/memerr.lua b/testes/memerr.lua index cb236eb976..77cb47cb1e 100644 --- a/testes/memerr.lua +++ b/testes/memerr.lua @@ -1,5 +1,5 @@ -- $Id: testes/memerr.lua $ --- See Copyright Notice in file all.lua +-- See Copyright Notice in file lua.h local function checkerr (msg, f, ...) diff --git a/testes/nextvar.lua b/testes/nextvar.lua index d1da3ceeb3..031ad3fd99 100644 --- a/testes/nextvar.lua +++ b/testes/nextvar.lua @@ -1,5 +1,5 @@ -- $Id: testes/nextvar.lua $ --- See Copyright Notice in file all.lua +-- See Copyright Notice in file lua.h print('testing tables, next, and for') diff --git a/testes/pm.lua b/testes/pm.lua index f5889fcd07..2a0cfb0bb5 100644 --- a/testes/pm.lua +++ b/testes/pm.lua @@ -1,5 +1,5 @@ -- $Id: testes/pm.lua $ --- See Copyright Notice in file all.lua +-- See Copyright Notice in file lua.h -- UTF-8 file diff --git a/testes/sort.lua b/testes/sort.lua index 290b199ee5..b012766057 100644 --- a/testes/sort.lua +++ b/testes/sort.lua @@ -1,5 +1,5 @@ -- $Id: testes/sort.lua $ --- See Copyright Notice in file all.lua +-- See Copyright Notice in file lua.h print "testing (parts of) table library" diff --git a/testes/strings.lua b/testes/strings.lua index 9bb52b35dd..ce28e4c560 100644 --- a/testes/strings.lua +++ b/testes/strings.lua @@ -1,5 +1,5 @@ -- $Id: testes/strings.lua $ --- See Copyright Notice in file all.lua +-- See Copyright Notice in file lua.h -- ISO Latin encoding diff --git a/testes/tpack.lua b/testes/tpack.lua index 4b32efb59b..70386178c4 100644 --- a/testes/tpack.lua +++ b/testes/tpack.lua @@ -1,5 +1,5 @@ -- $Id: testes/tpack.lua $ --- See Copyright Notice in file all.lua +-- See Copyright Notice in file lua.h local pack = string.pack local packsize = string.packsize diff --git a/testes/utf8.lua b/testes/utf8.lua index dc0f2f09d5..0704782c12 100644 --- a/testes/utf8.lua +++ b/testes/utf8.lua @@ -1,5 +1,5 @@ -- $Id: testes/utf8.lua $ --- See Copyright Notice in file all.lua +-- See Copyright Notice in file lua.h -- UTF-8 file diff --git a/testes/vararg.lua b/testes/vararg.lua index 1b02510244..10553de2af 100644 --- a/testes/vararg.lua +++ b/testes/vararg.lua @@ -1,5 +1,5 @@ -- $Id: testes/vararg.lua $ --- See Copyright Notice in file all.lua +-- See Copyright Notice in file lua.h print('testing vararg') diff --git a/testes/verybig.lua b/testes/verybig.lua index 250ea79501..8163802c1d 100644 --- a/testes/verybig.lua +++ b/testes/verybig.lua @@ -1,5 +1,5 @@ -- $Id: testes/verybig.lua $ --- See Copyright Notice in file all.lua +-- See Copyright Notice in file lua.h print "testing RK" From d9e0f64a5de699a620771af299ea22f522c72f19 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 12 Mar 2025 15:45:39 -0300 Subject: [PATCH 1017/1145] Small changes in the manual --- manual/manual.of | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/manual/manual.of b/manual/manual.of index b34e1e9cb7..b698672a08 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -2222,10 +2222,10 @@ f(3, 4, 5) a=3, b=4 f(r(), 10) a=1, b=10 f(r()) a=1, b=2 -g(3) a=3, b=nil, ... --> (nothing) -g(3, 4) a=3, b=4, ... --> (nothing) -g(3, 4, 5, 8) a=3, b=4, ... --> 5 8 -g(5, r()) a=5, b=1, ... --> 2 3 +g(3) a=3, b=nil, ... -> (nothing) +g(3, 4) a=3, b=4, ... -> (nothing) +g(3, 4, 5, 8) a=3, b=4, ... -> 5 8 +g(5, r()) a=5, b=1, ... -> 2 3 } Results are returned using the @Rw{return} statement @see{control}. @@ -7477,25 +7477,25 @@ then there is no replacement Here are some examples: @verbatim{ x = string.gsub("hello world", "(%w+)", "%1 %1") ---> x="hello hello world world" +-- x="hello hello world world" x = string.gsub("hello world", "%w+", "%0 %0", 1) ---> x="hello hello world" +-- x="hello hello world" x = string.gsub("hello world from Lua", "(%w+)%s*(%w+)", "%2 %1") ---> x="world hello Lua from" +-- x="world hello Lua from" x = string.gsub("home = $HOME, user = $USER", "%$(%w+)", os.getenv) ---> x="home = /home/roberto, user = roberto" +-- x="home = /home/roberto, user = roberto" x = string.gsub("4+5 = $return 4+5$", "%$(.-)%$", function (s) return load(s)() end) ---> x="4+5 = 9" +-- x="4+5 = 9" local t = {name="lua", version="5.4"} x = string.gsub("$name-$version.tar.gz", "%$(%w+)", t) ---> x="lua-5.4.tar.gz" +-- x="lua-5.4.tar.gz" } } @@ -9299,15 +9299,21 @@ the interpreter waits for its completion by issuing a different prompt. Note that, as each complete line is read as a new chunk, -local variables do not outlive lines: +local variables do not outlive lines. +To steer clear of confusion, +the interpreter gives a warning if a line starts with the +reserved word @Rw{local}: @verbatim{ -> x = 20 -> local x = 10; print(x) --> 10 -> print(x) --> 20 -- global 'x' -> do -- incomplete line +> x = 20 -- global 'x' +> local x = 10; print(x) + --> warning: locals do not survive across lines in interactive mode + --> 10 +> print(x) -- back to global 'x' + --> 20 +> do -- incomplete chunk >> local x = 10; print(x) -- '>>' prompts for line completion >> print(x) ->> end -- line completed; Lua will run it as a single chunk +>> end -- chunk completed --> 10 --> 10 } From c931d86e98da320c71da70c16d44aa28e9755520 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 12 Mar 2025 15:51:16 -0300 Subject: [PATCH 1018/1145] 'luaD_seterrorobj' should not raise errors This function can be called unprotected, so it should not raise any kind of errors. (It could raise a memory-allocation error when creating a message). --- ldebug.c | 4 ++++ ldo.c | 36 +++++++++++++++++------------------- ldo.h | 1 + lstate.c | 2 +- testes/errors.lua | 4 ++-- 5 files changed, 25 insertions(+), 22 deletions(-) diff --git a/ldebug.c b/ldebug.c index 18bdc5959c..a32029815b 100644 --- a/ldebug.c +++ b/ldebug.c @@ -832,6 +832,10 @@ l_noret luaG_errormsg (lua_State *L) { L->top.p++; /* assume EXTRA_STACK */ luaD_callnoyield(L, L->top.p - 2, 1); /* call it */ } + if (ttisnil(s2v(L->top.p - 1))) { /* error object is nil? */ + /* change it to a proper message */ + setsvalue2s(L, L->top.p - 1, luaS_newliteral(L, "")); + } luaD_throw(L, LUA_ERRRUN); } diff --git a/ldo.c b/ldo.c index 84f7bbb2c4..b0d37bf7f3 100644 --- a/ldo.c +++ b/ldo.c @@ -102,24 +102,13 @@ struct lua_longjmp { void luaD_seterrorobj (lua_State *L, TStatus errcode, StkId oldtop) { - switch (errcode) { - case LUA_ERRMEM: { /* memory error? */ - setsvalue2s(L, oldtop, G(L)->memerrmsg); /* reuse preregistered msg. */ - break; - } - case LUA_ERRERR: { - setsvalue2s(L, oldtop, luaS_newliteral(L, "error in error handling")); - break; - } - default: { - lua_assert(errorstatus(errcode)); /* must be a real error */ - if (!ttisnil(s2v(L->top.p - 1))) { /* error object is not nil? */ - setobjs2s(L, oldtop, L->top.p - 1); /* move it to 'oldtop' */ - } - else /* change it to a proper message */ - setsvalue2s(L, oldtop, luaS_newliteral(L, "")); - break; - } + if (errcode == LUA_ERRMEM) { /* memory error? */ + setsvalue2s(L, oldtop, G(L)->memerrmsg); /* reuse preregistered msg. */ + } + else { + lua_assert(errorstatus(errcode)); /* must be a real error */ + lua_assert(!ttisnil(s2v(L->top.p - 1))); /* with a non-nil object */ + setobjs2s(L, oldtop, L->top.p - 1); /* move it to 'oldtop' */ } L->top.p = oldtop + 1; /* top goes back to old top plus error object */ } @@ -190,6 +179,15 @@ TStatus luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) { #define ERRORSTACKSIZE (MAXSTACK + STACKERRSPACE) +/* raise an error while running the message handler */ +l_noret luaD_errerr (lua_State *L) { + TString *msg = luaS_newliteral(L, "error in error handling"); + setsvalue2s(L, L->top.p, msg); + L->top.p++; /* assume EXTRA_STACK */ + luaD_throw(L, LUA_ERRERR); +} + + /* ** In ISO C, any pointer use after the pointer has been deallocated is ** undefined behavior. So, before a stack reallocation, all pointers @@ -317,7 +315,7 @@ int luaD_growstack (lua_State *L, int n, int raiseerror) { a stack error; cannot grow further than that. */ lua_assert(stacksize(L) == ERRORSTACKSIZE); if (raiseerror) - luaD_throw(L, LUA_ERRERR); /* error inside message handler */ + luaD_errerr(L); /* error inside message handler */ return 0; /* if not 'raiseerror', just signal it */ } else if (n < MAXSTACK) { /* avoids arithmetic overflows */ diff --git a/ldo.h b/ldo.h index ea1655e1fa..465f4fb8d8 100644 --- a/ldo.h +++ b/ldo.h @@ -67,6 +67,7 @@ /* type of protected functions, to be ran by 'runprotected' */ typedef void (*Pfunc) (lua_State *L, void *ud); +LUAI_FUNC l_noret luaD_errerr (lua_State *L); LUAI_FUNC void luaD_seterrorobj (lua_State *L, TStatus errcode, StkId oldtop); LUAI_FUNC TStatus luaD_protectedparser (lua_State *L, ZIO *z, const char *name, diff --git a/lstate.c b/lstate.c index 69ddef40ef..ed5ccaaa32 100644 --- a/lstate.c +++ b/lstate.c @@ -132,7 +132,7 @@ void luaE_checkcstack (lua_State *L) { if (getCcalls(L) == LUAI_MAXCCALLS) luaG_runerror(L, "C stack overflow"); else if (getCcalls(L) >= (LUAI_MAXCCALLS / 10 * 11)) - luaD_throw(L, LUA_ERRERR); /* error while handling stack error */ + luaD_errerr(L); /* error while handling stack error */ } diff --git a/testes/errors.lua b/testes/errors.lua index 8ef267579a..d83e6023b1 100644 --- a/testes/errors.lua +++ b/testes/errors.lua @@ -46,7 +46,7 @@ end assert(doit("error('hi', 0)") == 'hi') -- test nil error message -assert(doit("error()") == "") +assert(doit("error()") == "") -- test common errors/errors that crashed in the past @@ -614,7 +614,7 @@ do assert(not res and msg == t) res, msg = pcall(function () error(nil) end) - assert(not res and msg == "") + assert(not res and msg == "") local function f() error{msg='x'} end res, msg = xpcall(f, function (r) return {msg=r.msg..'y'} end) From 22974326ca0d4f893849ce722cc1d65b3e228f42 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 13 Mar 2025 15:30:52 -0300 Subject: [PATCH 1019/1145] Use after free in 'luaV_finishset' If a metatable is a weak table, its __newindex field could be collected by an emergency collection while being used in 'luaV_finishset'. (This bug has similarities with bug 5.3.2-1, fixed in commit a272fa66.) --- lapi.c | 5 +++++ lvm.c | 8 ++++++++ testes/events.lua | 13 ++++++++++++- 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/lapi.c b/lapi.c index a5e94507ac..eab12cac0f 100644 --- a/lapi.c +++ b/lapi.c @@ -681,6 +681,11 @@ static int auxgetstr (lua_State *L, const TValue *t, const char *k) { } +/* +** The following function assumes that the registry cannot be a weak +** table, so that en mergency collection while using the global table +** cannot collect it. +*/ static void getGlobalTable (lua_State *L, TValue *gt) { Table *registry = hvalue(&G(L)->l_registry); lu_byte tag = luaH_getint(registry, LUA_RIDX_GLOBALS, gt); diff --git a/lvm.c b/lvm.c index f0e73f9bb6..af048d8130 100644 --- a/lvm.c +++ b/lvm.c @@ -325,6 +325,11 @@ lu_byte luaV_finishget (lua_State *L, const TValue *t, TValue *key, /* ** Finish a table assignment 't[key] = val'. +** About anchoring the table before the call to 'luaH_finishset': +** This call may trigger an emergency collection. When loop>0, +** the table being acessed is a field in some metatable. If this +** metatable is weak and the table is not anchored, this collection +** could collect that table while it is being updated. */ void luaV_finishset (lua_State *L, const TValue *t, TValue *key, TValue *val, int hres) { @@ -335,7 +340,10 @@ void luaV_finishset (lua_State *L, const TValue *t, TValue *key, Table *h = hvalue(t); /* save 't' table */ tm = fasttm(L, h->metatable, TM_NEWINDEX); /* get metamethod */ if (tm == NULL) { /* no metamethod? */ + sethvalue2s(L, L->top.p, h); /* anchor 't' */ + L->top.p++; /* assume EXTRA_STACK */ luaH_finishset(L, h, key, val, hres); /* set new value */ + L->top.p--; invalidateTMcache(h); luaC_barrierback(L, obj2gco(h), val); return; diff --git a/testes/events.lua b/testes/events.lua index 2500fbd554..7e434b1f6f 100644 --- a/testes/events.lua +++ b/testes/events.lua @@ -379,6 +379,17 @@ x = 0 .."a".."b"..c..d.."e".."f".."g" assert(x.val == "0abcdefg") +do + -- bug since 5.4.1 (test needs T) + local mt = setmetatable({__newindex={}}, {__mode='v'}) + local t = setmetatable({}, mt) + + if T then T.allocfailnext() end + + -- seg. fault + for i=1, 10 do t[i] = 1 end +end + -- concat metamethod x numbers (bug in 5.1.1) c = {} local x @@ -481,7 +492,7 @@ assert(not pcall(function (a,b) return a[b] end, a, 10)) assert(not pcall(function (a,b,c) a[b] = c end, a, 10, true)) -- bug in 5.1 -T, K, V = nil +local T, K, V = nil grandparent = {} grandparent.__newindex = function(t,k,v) T=t; K=k; V=v end From c2dc6e8e947ed0c7b18d452592f722f56ee1f96a Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 14 Mar 2025 12:37:19 -0300 Subject: [PATCH 1020/1145] Missing GC barrier in 'luaV_finishset' --- lapi.c | 3 +-- lvm.c | 4 +++- testes/gc.lua | 15 +++++++++++++++ 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/lapi.c b/lapi.c index eab12cac0f..f59430a7f3 100644 --- a/lapi.c +++ b/lapi.c @@ -888,9 +888,8 @@ LUA_API void lua_settable (lua_State *L, int idx) { api_checkpop(L, 2); t = index2value(L, idx); luaV_fastset(t, s2v(L->top.p - 2), s2v(L->top.p - 1), hres, luaH_pset); - if (hres == HOK) { + if (hres == HOK) luaV_finishfastset(L, t, s2v(L->top.p - 1)); - } else luaV_finishset(L, t, s2v(L->top.p - 2), s2v(L->top.p - 1), hres); L->top.p -= 2; /* pop index and value */ diff --git a/lvm.c b/lvm.c index af048d8130..f14397d46e 100644 --- a/lvm.c +++ b/lvm.c @@ -362,8 +362,10 @@ void luaV_finishset (lua_State *L, const TValue *t, TValue *key, } t = tm; /* else repeat assignment over 'tm' */ luaV_fastset(t, key, val, hres, luaH_pset); - if (hres == HOK) + if (hres == HOK) { + luaV_finishfastset(L, t, val); return; /* done */ + } /* else 'return luaV_finishset(L, t, key, val, slot)' (loop) */ } luaG_runerror(L, "'__newindex' chain too long; possible loop"); diff --git a/testes/gc.lua b/testes/gc.lua index 96eadad816..0693837c35 100644 --- a/testes/gc.lua +++ b/testes/gc.lua @@ -600,6 +600,21 @@ if T then end +if T then + collectgarbage("stop") + T.gcstate("pause") + local sup = {x = 0} + local a = setmetatable({}, {__newindex = sup}) + T.gcstate("enteratomic") + assert(T.gccolor(sup) == "black") + a.x = {} -- should not break the invariant + assert(not (T.gccolor(sup) == "black" and T.gccolor(sup.x) == "white")) + T.gcstate("pause") -- complete the GC cycle + sup.x.y = 10 + collectgarbage("restart") +end + + if T then print("emergency collections") collectgarbage() From 94d38560c3095190fa2c868cbf7bcf39ca444568 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 14 Mar 2025 15:16:09 -0300 Subject: [PATCH 1021/1145] Wrong error message when using "_ENV" fields The string "_ENV" is erroneously identified as a variable _ENV, so that results from a field is classified as a global. --- ldebug.c | 17 ++++++++++++----- ltests.c | 2 +- testes/errors.lua | 3 +++ 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/ldebug.c b/ldebug.c index a32029815b..258a439478 100644 --- a/ldebug.c +++ b/ldebug.c @@ -33,6 +33,8 @@ #define LuaClosure(f) ((f) != NULL && (f)->c.tt == LUA_VLCL) +static const char strlocal[] = "local"; +static const char strupval[] = "upvalue"; static const char *funcnamefromcall (lua_State *L, CallInfo *ci, const char **name); @@ -505,7 +507,7 @@ static const char *basicgetobjname (const Proto *p, int *ppc, int reg, int pc = *ppc; *name = luaF_getlocalname(p, reg + 1, pc); if (*name) /* is a local? */ - return "local"; + return strlocal; /* else try symbolic execution */ *ppc = pc = findsetreg(p, pc, reg); if (pc != -1) { /* could find instruction? */ @@ -520,7 +522,7 @@ static const char *basicgetobjname (const Proto *p, int *ppc, int reg, } case OP_GETUPVAL: { *name = upvalname(p, GETARG_B(i)); - return "upvalue"; + return strupval; } case OP_LOADK: return kname(p, GETARG_Bx(i), name); case OP_LOADKX: return kname(p, GETARG_Ax(p->code[pc + 1]), name); @@ -550,8 +552,13 @@ static const char *isEnv (const Proto *p, int pc, Instruction i, int isup) { const char *name; /* name of indexed variable */ if (isup) /* is 't' an upvalue? */ name = upvalname(p, t); - else /* 't' is a register */ - basicgetobjname(p, &pc, t, &name); + else { /* 't' is a register */ + const char *what = basicgetobjname(p, &pc, t, &name); + /* 'name' must be the name of a local variable (at the current + level or an upvalue) */ + if (what != strlocal && what != strupval) + name = NULL; /* cannot be the variable _ENV */ + } return (name && strcmp(name, LUA_ENV) == 0) ? "global" : "field"; } @@ -698,7 +705,7 @@ static const char *getupvalname (CallInfo *ci, const TValue *o, for (i = 0; i < c->nupvalues; i++) { if (c->upvals[i]->v.p == o) { *name = upvalname(c->p, i); - return "upvalue"; + return strupval; } } return NULL; diff --git a/ltests.c b/ltests.c index f4855fea95..d3509862d9 100644 --- a/ltests.c +++ b/ltests.c @@ -982,7 +982,7 @@ static int gc_printobj (lua_State *L) { } -static const char *statenames[] = { +static const char *const statenames[] = { "propagate", "enteratomic", "atomic", "sweepallgc", "sweepfinobj", "sweeptobefnz", "sweepend", "callfin", "pause", ""}; diff --git a/testes/errors.lua b/testes/errors.lua index d83e6023b1..c1c40fecdf 100644 --- a/testes/errors.lua +++ b/testes/errors.lua @@ -162,6 +162,9 @@ checkmessage("aaa=(1)..{}", "a table value") -- bug in 5.4.6 checkmessage("a = {_ENV = {}}; print(a._ENV.x + 1)", "field 'x'") +-- a similar bug, since 5.4.0 +checkmessage("print(('_ENV').x + 1)", "field 'x'") + _G.aaa, _G.bbbb = nil -- calls From 205f9aa67b43b3d9b5059769cfc1ed0265341586 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 17 Mar 2025 14:30:43 -0300 Subject: [PATCH 1022/1145] New function 'printallstack' in test library --- ltests.c | 23 +++++++++++++++++++++++ ltests.h | 1 + 2 files changed, 24 insertions(+) diff --git a/ltests.c b/ltests.c index d3509862d9..5b4a600037 100644 --- a/ltests.c +++ b/ltests.c @@ -872,6 +872,28 @@ void lua_printstack (lua_State *L) { } +int lua_printallstack (lua_State *L) { + StkId p; + int i = 1; + CallInfo *ci = &L->base_ci; + printf("stack: >>\n"); + for (p = L->stack.p; p < L->top.p; p++) { + if (ci != NULL && p == ci->func.p) { + printf(" ---\n"); + if (ci == L->ci) + ci = NULL; /* printed last frame */ + else + ci = ci->next; + } + printf("%3d: ", i++); + lua_printvalue(s2v(p)); + printf("\n"); + } + printf("<<\n"); + return 0; +} + + static int get_limits (lua_State *L) { lua_createtable(L, 0, 5); setnameval(L, "IS32INT", LUAI_IS32INT); @@ -2102,6 +2124,7 @@ static const struct luaL_Reg tests_funcs[] = { {"limits", get_limits}, {"listcode", listcode}, {"printcode", printcode}, + {"printallstack", lua_printallstack}, {"listk", listk}, {"listabslineinfo", listabslineinfo}, {"listlocals", listlocals}, diff --git a/ltests.h b/ltests.h index cc372b8f4b..af5641ba8b 100644 --- a/ltests.h +++ b/ltests.h @@ -94,6 +94,7 @@ LUAI_FUNC void lua_printvalue (struct TValue *v); ** Function to print the stack */ LUAI_FUNC void lua_printstack (lua_State *L); +LUAI_FUNC int lua_printallstack (lua_State *L); /* test for lock/unlock */ From 921832be8d7f687d2cd891654c8680c6e9d6584c Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 17 Mar 2025 14:32:08 -0300 Subject: [PATCH 1023/1145] New function 'resetCI' New function 'resetCI' resets the CallInfo list of a thread, ensuring a proper state when creating a new thread, closing a thread, or closing a state, so that we can run code after that. (When closing a thread, we need to run its __close metamethods; when closing a state, we need to run its __close metamethods and its finalizers.) --- lstate.c | 39 ++++++++++++++++++++------------------- testes/coroutine.lua | 19 +++++++++++++++++++ 2 files changed, 39 insertions(+), 19 deletions(-) diff --git a/lstate.c b/lstate.c index ed5ccaaa32..20ed838f42 100644 --- a/lstate.c +++ b/lstate.c @@ -143,25 +143,29 @@ LUAI_FUNC void luaE_incCstack (lua_State *L) { } +static void resetCI (lua_State *L) { + CallInfo *ci = L->ci = &L->base_ci; + ci->func.p = L->stack.p; + setnilvalue(s2v(ci->func.p)); /* 'function' entry for basic 'ci' */ + ci->top.p = ci->func.p + 1 + LUA_MINSTACK; /* +1 for 'function' entry */ + ci->u.c.k = NULL; + ci->callstatus = CIST_C; + L->status = LUA_OK; + L->errfunc = 0; /* stack unwind can "throw away" the error function */ +} + + static void stack_init (lua_State *L1, lua_State *L) { - int i; CallInfo *ci; + int i; /* initialize stack array */ L1->stack.p = luaM_newvector(L, BASIC_STACK_SIZE + EXTRA_STACK, StackValue); L1->tbclist.p = L1->stack.p; for (i = 0; i < BASIC_STACK_SIZE + EXTRA_STACK; i++) setnilvalue(s2v(L1->stack.p + i)); /* erase new stack */ - L1->top.p = L1->stack.p; L1->stack_last.p = L1->stack.p + BASIC_STACK_SIZE; /* initialize first ci */ - ci = &L1->base_ci; - ci->next = ci->previous = NULL; - ci->callstatus = CIST_C; - ci->func.p = L1->top.p; - ci->u.c.k = NULL; - setnilvalue(s2v(L1->top.p)); /* 'function' entry for this 'ci' */ - L1->top.p++; - ci->top.p = L1->top.p + LUA_MINSTACK; - L1->ci = ci; + resetCI(L1); + L1->top.p = L1->stack.p + 1; /* +1 for 'function' entry */ } @@ -235,6 +239,7 @@ static void preinit_thread (lua_State *L, global_State *g) { L->status = LUA_OK; L->errfunc = 0; L->oldpc = 0; + L->base_ci.previous = L->base_ci.next = NULL; } @@ -252,8 +257,9 @@ static void close_state (lua_State *L) { if (!completestate(g)) /* closing a partially built state? */ luaC_freeallobjects(L); /* just collect its objects */ else { /* closing a fully built state */ - L->ci = &L->base_ci; /* unwind CallInfo list */ + resetCI(L); luaD_closeprotected(L, 1, LUA_OK); /* close all upvalues */ + L->top.p = L->stack.p + 1; /* empty the stack to run finalizers */ luaC_freeallobjects(L); /* collect all objects */ luai_userstateclose(L); } @@ -302,20 +308,15 @@ void luaE_freethread (lua_State *L, lua_State *L1) { TStatus luaE_resetthread (lua_State *L, TStatus status) { - CallInfo *ci = L->ci = &L->base_ci; /* unwind CallInfo list */ - setnilvalue(s2v(L->stack.p)); /* 'function' entry for basic 'ci' */ - ci->func.p = L->stack.p; - ci->callstatus = CIST_C; + resetCI(L); if (status == LUA_YIELD) status = LUA_OK; - L->status = LUA_OK; /* so it can run __close metamethods */ status = luaD_closeprotected(L, 1, status); if (status != LUA_OK) /* errors? */ luaD_seterrorobj(L, status, L->stack.p + 1); else L->top.p = L->stack.p + 1; - ci->top.p = L->top.p + LUA_MINSTACK; - luaD_reallocstack(L, cast_int(ci->top.p - L->stack.p), 0); + luaD_reallocstack(L, cast_int(L->ci->top.p - L->stack.p), 0); return status; } diff --git a/testes/coroutine.lua b/testes/coroutine.lua index 680fc6058d..17f6cebaae 100644 --- a/testes/coroutine.lua +++ b/testes/coroutine.lua @@ -505,6 +505,25 @@ assert(not pcall(a, a)) a = nil +do + -- bug in 5.4: thread can use message handler higher in the stack + -- than the variable being closed + local c = coroutine.create(function() + local clo = setmetatable({}, {__close=function() + local x = 134 -- will overwrite message handler + error(x) + end}) + -- yields coroutine but leaves a new message handler for it, + -- that would be used when closing the coroutine (except that it + -- will be overwritten) + xpcall(coroutine.yield, function() return "XXX" end) + end) + + assert(coroutine.resume(c)) -- start coroutine + local st, msg = coroutine.close(c) + assert(not st and msg == 134) +end + -- access to locals of erroneous coroutines local x = coroutine.create (function () local a = 10 From fdbb4c23414cef141602a45ae8464f0553085e02 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 17 Mar 2025 16:05:47 -0300 Subject: [PATCH 1024/1145] Detail in the manual --- manual/manual.of | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/manual/manual.of b/manual/manual.of index b698672a08..c1a9c138d7 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -6230,15 +6230,17 @@ Returns the name of the type of the value at the given index. @APIEntry{void luaL_unref (lua_State *L, int t, int ref);| @apii{0,0,-} -Releases the reference @id{ref} from the table at index @id{t} -@seeC{luaL_ref}. -The entry is removed from the table, +Releases a reference @see{luaL_ref}. +The integer @id{ref} must be either +@Lid{LUA_NOREF}, @Lid{LUA_REFNIL}, +or a reference previously returned by @Lid{luaL_ref} +and not already released. +If @id{ref} is either @Lid{LUA_NOREF} or @Lid{LUA_REFNIL} +this function does nothing. +Otherwise, the entry is removed from the table, so that the referred object can be collected and the reference @id{ref} can be used again by @Lid{luaL_ref}. -If @id{ref} is @Lid{LUA_NOREF} or @Lid{LUA_REFNIL}, -@Lid{luaL_unref} does nothing. - } @APIEntry{void luaL_where (lua_State *L, int lvl);| From cad5a4fdbb0f0843ec67596d1e472187decf1c88 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 24 Mar 2025 15:23:55 -0300 Subject: [PATCH 1025/1145] Details Small changes in test library: - execute mode added to 'all.lua'; - more information about subtypes (tags) when printing a stack. --- ltests.c | 53 ++++++++++++++++++++++++++------------------------ testes/all.lua | 0 2 files changed, 28 insertions(+), 25 deletions(-) mode change 100644 => 100755 testes/all.lua diff --git a/ltests.c b/ltests.c index 5b4a600037..6a638d051b 100644 --- a/ltests.c +++ b/ltests.c @@ -327,37 +327,40 @@ void lua_printobj (lua_State *L, struct GCObject *o) { void lua_printvalue (TValue *v) { - switch (ttype(v)) { - case LUA_TNUMBER: { + switch (ttypetag(v)) { + case LUA_VNUMINT: case LUA_VNUMFLT: { char buff[LUA_N2SBUFFSZ]; unsigned len = luaO_tostringbuff(v, buff); buff[len] = '\0'; printf("%s", buff); break; } - case LUA_TSTRING: { - printf("'%s'", getstr(tsvalue(v))); - break; - } - case LUA_TBOOLEAN: { - printf("%s", (!l_isfalse(v) ? "true" : "false")); - break; - } - case LUA_TLIGHTUSERDATA: { - printf("light udata: %p", pvalue(v)); - break; - } - case LUA_TNIL: { - printf("nil"); - break; - } - default: { - if (ttislcf(v)) - printf("light C function: %p", fvalue(v)); - else /* must be collectable */ - printf("%s: %p", ttypename(ttype(v)), gcvalue(v)); - break; - } + case LUA_VSHRSTR: + printf("'%s'", getstr(tsvalue(v))); break; + case LUA_VLNGSTR: + printf("'%.30s...'", getstr(tsvalue(v))); break; + case LUA_VFALSE: + printf("%s", "false"); break; + case LUA_VTRUE: + printf("%s", "true"); break; + case LUA_VLIGHTUSERDATA: + printf("light udata: %p", pvalue(v)); break; + case LUA_VUSERDATA: + printf("full udata: %p", uvalue(v)); break; + case LUA_VNIL: + printf("nil"); break; + case LUA_VLCF: + printf("light C function: %p", fvalue(v)); break; + case LUA_VCCL: + printf("C closure: %p", clCvalue(v)); break; + case LUA_VLCL: + printf("Lua function: %p", clLvalue(v)); break; + case LUA_VTHREAD: + printf("thread: %p", thvalue(v)); break; + case LUA_VTABLE: + printf("table: %p", hvalue(v)); break; + default: + lua_assert(0); } } diff --git a/testes/all.lua b/testes/all.lua old mode 100644 new mode 100755 From b0f3df16a495745cf16657a48dde6845ec85c732 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 25 Mar 2025 11:43:03 -0300 Subject: [PATCH 1026/1145] Addition in math.random can overflow To avoid complains from some tools, the addition when computing math.random(n,m), which is computed as n + random(0, m - n), should use unsigned integers. --- lmathlib.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lmathlib.c b/lmathlib.c index c7418e69ec..a098177235 100644 --- a/lmathlib.c +++ b/lmathlib.c @@ -593,8 +593,8 @@ static int math_random (lua_State *L) { /* random integer in the interval [low, up] */ luaL_argcheck(L, low <= up, 1, "interval is empty"); /* project random integer into the interval [0, up - low] */ - p = project(I2UInt(rv), (lua_Unsigned)up - (lua_Unsigned)low, state); - lua_pushinteger(L, l_castU2S(p) + low); + p = project(I2UInt(rv), l_castS2U(up) - l_castS2U(low), state); + lua_pushinteger(L, l_castU2S(p + l_castS2U(low))); return 1; } From ef5d171cc89b19ac1fea905b99d819b5f97cba00 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 27 Mar 2025 12:38:29 -0300 Subject: [PATCH 1027/1145] New macro 'l_numbits' --- ldump.c | 2 +- lfunc.c | 9 ++------- llimits.h | 6 ++++-- ltable.c | 2 +- ltests.h | 3 --- lvm.c | 2 +- 6 files changed, 9 insertions(+), 15 deletions(-) diff --git a/ldump.c b/ldump.c index 54f96674e1..d8fca317f0 100644 --- a/ldump.c +++ b/ldump.c @@ -87,7 +87,7 @@ static void dumpByte (DumpState *D, int y) { ** size for 'dumpVarint' buffer: each byte can store up to 7 bits. ** (The "+6" rounds up the division.) */ -#define DIBS ((sizeof(size_t) * CHAR_BIT + 6) / 7) +#define DIBS ((l_numbits(size_t) + 6) / 7) /* ** Dumps an unsigned integer using the MSB Varint encoding diff --git a/lfunc.c b/lfunc.c index c62a5d2395..da7c623974 100644 --- a/lfunc.c +++ b/lfunc.c @@ -162,13 +162,8 @@ static void prepcallclosemth (lua_State *L, StkId level, TStatus status, } -/* -** Maximum value for deltas in 'tbclist', dependent on the type -** of delta. (This macro assumes that an 'L' is in scope where it -** is used.) -*/ -#define MAXDELTA \ - ((256ul << ((sizeof(L->stack.p->tbclist.delta) - 1) * 8)) - 1) +/* Maximum value for deltas in 'tbclist' */ +#define MAXDELTA USHRT_MAX /* diff --git a/llimits.h b/llimits.h index d206e9e1cd..710dc1b440 100644 --- a/llimits.h +++ b/llimits.h @@ -15,6 +15,8 @@ #include "lua.h" +#define l_numbits(t) cast_int(sizeof(t) * CHAR_BIT) + /* ** 'l_mem' is a signed integer big enough to count the total memory ** used by Lua. (It is signed due to the use of debt in several @@ -33,7 +35,7 @@ typedef unsigned long lu_mem; #endif /* } */ #define MAX_LMEM \ - cast(l_mem, (cast(lu_mem, 1) << (sizeof(l_mem) * 8 - 1)) - 1) + cast(l_mem, (cast(lu_mem, 1) << (l_numbits(l_mem) - 1)) - 1) /* chars used as small naturals (so that 'char' is reserved for characters) */ @@ -61,7 +63,7 @@ typedef lu_byte TStatus; ** floor of the log2 of the maximum signed value for integral type 't'. ** (That is, maximum 'n' such that '2^n' fits in the given signed type.) */ -#define log2maxs(t) cast_int(sizeof(t) * 8 - 2) +#define log2maxs(t) (l_numbits(t) - 2) /* diff --git a/ltable.c b/ltable.c index 8df9a4fbfe..0b3ec1762c 100644 --- a/ltable.c +++ b/ltable.c @@ -67,7 +67,7 @@ typedef union { ** MAXABITS is the largest integer such that 2^MAXABITS fits in an ** unsigned int. */ -#define MAXABITS cast_int(sizeof(int) * CHAR_BIT - 1) +#define MAXABITS (l_numbits(int) - 1) /* diff --git a/ltests.h b/ltests.h index af5641ba8b..3420516763 100644 --- a/ltests.h +++ b/ltests.h @@ -142,9 +142,6 @@ LUA_API void *debug_realloc (void *ud, void *block, #define STRCACHE_N 23 #define STRCACHE_M 5 -#undef LUAI_USER_ALIGNMENT_T -#define LUAI_USER_ALIGNMENT_T union { char b[sizeof(void*) * 8]; } - /* ** This one is not compatible with tests for opcode optimizations, diff --git a/lvm.c b/lvm.c index f14397d46e..e2c36ef577 100644 --- a/lvm.c +++ b/lvm.c @@ -774,7 +774,7 @@ lua_Number luaV_modf (lua_State *L, lua_Number m, lua_Number n) { /* number of bits in an integer */ -#define NBITS cast_int(sizeof(lua_Integer) * CHAR_BIT) +#define NBITS l_numbits(lua_Integer) /* From 37a1b72706b6e55e60b8d73bcbe269921976825e Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 27 Mar 2025 15:22:40 -0300 Subject: [PATCH 1028/1145] Small optimization in 'project' from math.random When computing the Mersenne number, instead of spreading 1's a fixed number of times (with shifts of 1, 2, 4, 8, 16, and 32), spread only until the number becomes a Mersenne number. --- lmathlib.c | 30 +++++++++--------------------- 1 file changed, 9 insertions(+), 21 deletions(-) diff --git a/lmathlib.c b/lmathlib.c index a098177235..bd34c88860 100644 --- a/lmathlib.c +++ b/lmathlib.c @@ -533,7 +533,7 @@ typedef struct { ** Project the random integer 'ran' into the interval [0, n]. ** Because 'ran' has 2^B possible values, the projection can only be ** uniform when the size of the interval is a power of 2 (exact -** division). Otherwise, to get a uniform projection into [0, n], we +** division). So, to get a uniform projection into [0, n], we ** first compute 'lim', the smallest Mersenne number not smaller than ** 'n'. We then project 'ran' into the interval [0, lim]. If the result ** is inside [0, n], we are done. Otherwise, we try with another 'ran', @@ -541,26 +541,14 @@ typedef struct { */ static lua_Unsigned project (lua_Unsigned ran, lua_Unsigned n, RanState *state) { - if ((n & (n + 1)) == 0) /* is 'n + 1' a power of 2? */ - return ran & n; /* no bias */ - else { - lua_Unsigned lim = n; - /* compute the smallest (2^b - 1) not smaller than 'n' */ - lim |= (lim >> 1); - lim |= (lim >> 2); - lim |= (lim >> 4); - lim |= (lim >> 8); - lim |= (lim >> 16); -#if (LUA_MAXUNSIGNED >> 31) >= 3 - lim |= (lim >> 32); /* integer type has more than 32 bits */ -#endif - lua_assert((lim & (lim + 1)) == 0 /* 'lim + 1' is a power of 2, */ - && lim >= n /* not smaller than 'n', */ - && (lim >> 1) < n); /* and it is the smallest one */ - while ((ran &= lim) > n) /* project 'ran' into [0..lim] */ - ran = I2UInt(nextrand(state->s)); /* not inside [0..n]? try again */ - return ran; - } + lua_Unsigned lim = n; /* to compute the Mersenne number */ + int sh; /* how much to spread bits to the right in 'lim' */ + /* spread '1' bits in 'lim' until it becomes a Mersenne number */ + for (sh = 1; (lim & (lim + 1)) != 0; sh *= 2) + lim |= (lim >> sh); /* spread '1's to the right */ + while ((ran &= lim) > n) /* project 'ran' into [0..lim] and test */ + ran = I2UInt(nextrand(state->s)); /* not inside [0..n]? try again */ + return ran; } From f4123b2fc2a662c08e3d7edc721241c251a22c4b Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 31 Mar 2025 13:44:41 -0300 Subject: [PATCH 1029/1145] Growth factor of 1.5 for stack and lexical buffer --- lauxlib.c | 14 +++++++------- ldo.c | 2 +- llex.c | 6 +++--- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/lauxlib.c b/lauxlib.c index 5bca18166d..7c9ad53b2b 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -541,17 +541,17 @@ static void newbox (lua_State *L) { /* ** Compute new size for buffer 'B', enough to accommodate extra 'sz' -** bytes plus one for a terminating zero. (The test for "not big enough" -** also gets the case when the computation of 'newsize' overflows.) +** bytes plus one for a terminating zero. */ static size_t newbuffsize (luaL_Buffer *B, size_t sz) { - size_t newsize = (B->size / 2) * 3; /* buffer size * 1.5 */ - if (l_unlikely(sz > MAX_SIZE - B->n - 1)) + size_t newsize = B->size; + if (l_unlikely(sz >= MAX_SIZE - B->n)) return cast_sizet(luaL_error(B->L, "resulting string too large")); - if (newsize < B->n + sz + 1 || newsize > MAX_SIZE) { - /* newsize was not big enough or too big */ + /* else B->n + sz + 1 <= MAX_SIZE */ + if (newsize <= MAX_SIZE/3 * 2) /* no overflow? */ + newsize += (newsize >> 1); /* new size *= 1.5 */ + if (newsize < B->n + sz + 1) /* not big enough? */ newsize = B->n + sz + 1; - } return newsize; } diff --git a/ldo.c b/ldo.c index b0d37bf7f3..6824a21fa6 100644 --- a/ldo.c +++ b/ldo.c @@ -319,7 +319,7 @@ int luaD_growstack (lua_State *L, int n, int raiseerror) { return 0; /* if not 'raiseerror', just signal it */ } else if (n < MAXSTACK) { /* avoids arithmetic overflows */ - int newsize = 2 * size; /* tentative new size */ + int newsize = size + (size >> 1); /* tentative new size (size * 1.5) */ int needed = cast_int(L->top.p - L->stack.p) + n; if (newsize > MAXSTACK) /* cannot cross the limit */ newsize = MAXSTACK; diff --git a/llex.c b/llex.c index 1c4227ca4c..4b5a1f7509 100644 --- a/llex.c +++ b/llex.c @@ -62,10 +62,10 @@ static l_noret lexerror (LexState *ls, const char *msg, int token); static void save (LexState *ls, int c) { Mbuffer *b = ls->buff; if (luaZ_bufflen(b) + 1 > luaZ_sizebuffer(b)) { - size_t newsize; - if (luaZ_sizebuffer(b) >= MAX_SIZE/2) + size_t newsize = luaZ_sizebuffer(b); /* get old size */; + if (newsize >= (MAX_SIZE/3 * 2)) /* larger than MAX_SIZE/1.5 ? */ lexerror(ls, "lexical element too long", 0); - newsize = luaZ_sizebuffer(b) * 2; + newsize += (newsize >> 1); /* new size is 1.5 times the old one */ luaZ_resizebuffer(ls->L, b, newsize); } b->buffer[luaZ_bufflen(b)++] = cast_char(c); From 93e347b51923a3f0b993aac37c74e1489c02f3b5 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 1 Apr 2025 10:41:25 -0300 Subject: [PATCH 1030/1145] Corrections of stack addresses back to strict mode It can be a little slower, but only for quite large stacks and moreover stack reallocation is not a common operation. With no strong contrary reason, it is better to follow the standard. --- ldo.c | 2 +- ltests.h | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/ldo.c b/ldo.c index 6824a21fa6..3e5c7504f4 100644 --- a/ldo.c +++ b/ldo.c @@ -199,7 +199,7 @@ l_noret luaD_errerr (lua_State *L) { ** The following macro chooses how strict is the code. */ #if !defined(LUAI_STRICT_ADDRESS) -#define LUAI_STRICT_ADDRESS 0 +#define LUAI_STRICT_ADDRESS 1 #endif #if LUAI_STRICT_ADDRESS diff --git a/ltests.h b/ltests.h index 3420516763..7f0ce4041d 100644 --- a/ltests.h +++ b/ltests.h @@ -44,8 +44,8 @@ #define LUA_RAND32 -/* test stack reallocation with strict address use */ -#define LUAI_STRICT_ADDRESS 1 +/* test stack reallocation without strict address use */ +#define LUAI_STRICT_ADDRESS 0 /* memory-allocator control variables */ From 3f4f28010aa5065456f1edf97de1ab268cc49944 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 3 Apr 2025 11:32:49 -0300 Subject: [PATCH 1031/1145] io.write returns number of written bytes on error --- liolib.c | 18 +++++++++++------- ltests.c | 20 ++++++++++++++++++++ manual/manual.of | 3 +++ testes/files.lua | 31 +++++++++++++++++++++++++++++++ 4 files changed, 65 insertions(+), 7 deletions(-) diff --git a/liolib.c b/liolib.c index a0988db06a..3225ed5f2c 100644 --- a/liolib.c +++ b/liolib.c @@ -662,11 +662,12 @@ static int io_readline (lua_State *L) { static int g_write (lua_State *L, FILE *f, int arg) { int nargs = lua_gettop(L) - arg; - int status = 1; + size_t totalbytes = 0; /* total number of bytes written */ errno = 0; - for (; nargs--; arg++) { + for (; nargs--; arg++) { /* for each argument */ char buff[LUA_N2SBUFFSZ]; const char *s; + size_t numbytes; /* bytes written in one call to 'fwrite' */ size_t len = lua_numbertocstring(L, arg, buff); /* try as a number */ if (len > 0) { /* did conversion work (value was a number)? */ s = buff; @@ -674,12 +675,15 @@ static int g_write (lua_State *L, FILE *f, int arg) { } else /* must be a string */ s = luaL_checklstring(L, arg, &len); - status = status && (fwrite(s, sizeof(char), len, f) == len); + numbytes = fwrite(s, sizeof(char), len, f); + totalbytes += numbytes; + if (numbytes < len) { /* write error? */ + int n = luaL_fileresult(L, 0, NULL); + lua_pushinteger(L, cast_st2S(totalbytes)); + return n + 1; /* return fail, error msg., error code, and counter */ + } } - if (l_likely(status)) - return 1; /* file handle already on stack top */ - else - return luaL_fileresult(L, status, NULL); + return 1; /* no errors; file handle already on stack top */ } diff --git a/ltests.c b/ltests.c index 6a638d051b..1517aa88f6 100644 --- a/ltests.c +++ b/ltests.c @@ -2106,6 +2106,25 @@ static int coresume (lua_State *L) { } } +#if !defined(LUA_USE_POSIX) + +#define nonblock NULL + +#else + +#include +#include + +static int nonblock (lua_State *L) { + FILE *f = cast(luaL_Stream*, luaL_checkudata(L, 1, LUA_FILEHANDLE))->f; + int fd = fileno(f); + int flags = fcntl(fd, F_GETFL, 0); + flags |= O_NONBLOCK; + fcntl(fd, F_SETFL, flags); + return 0; +} +#endif + /* }====================================================== */ @@ -2159,6 +2178,7 @@ static const struct luaL_Reg tests_funcs[] = { {"upvalue", upvalue}, {"externKstr", externKstr}, {"externstr", externstr}, + {"nonblock", nonblock}, {NULL, NULL} }; diff --git a/manual/manual.of b/manual/manual.of index c1a9c138d7..7cd0d4dbde 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -8699,6 +8699,9 @@ Writes the value of each of its arguments to @id{file}. The arguments must be strings or numbers. In case of success, this function returns @id{file}. +Otherwise, it returns four values: +@fail, the error message, the error code, +and the number of bytes it was able to write. } diff --git a/testes/files.lua b/testes/files.lua index 05fae49b44..2c802047df 100644 --- a/testes/files.lua +++ b/testes/files.lua @@ -696,6 +696,37 @@ do end +if T and T.nonblock then + print("testing failed write") + + -- unable to write anything to /dev/full + local f = io.open("/dev/full", "w") + assert(f:setvbuf("no")) + local _, _, err, count = f:write("abcd") + assert(err > 0 and count == 0) + assert(f:close()) + + -- receiver will read a "few" bytes (enough to empty a large buffer) + local receiver = [[ + lua -e 'assert(io.stdin:setvbuf("no")); assert(#io.read(1e4) == 1e4)' ]] + + local f = io.popen(receiver, "w") + assert(f:setvbuf("no")) + T.nonblock(f) + + -- able to write a few bytes + assert(f:write(string.rep("a", 1e2))) + + -- Unable to write more bytes than the pipe buffer supports. + -- (In Linux, the pipe buffer size is 64K (2^16). Posix requires at + -- least 512 bytes.) + local _, _, err, count = f:write("abcd", string.rep("a", 2^17)) + assert(err > 0 and count >= 512 and count < 2^17) + + assert(f:close()) +end + + if not _soft then print("testing large files (> BUFSIZ)") io.output(file) From 620f49a7aae8a5c982b21f0accbf2ff9019a55f6 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 3 Apr 2025 12:56:52 -0300 Subject: [PATCH 1032/1145] Tiny refactoring in io.flush --- liolib.c | 13 +++++++------ testes/files.lua | 19 ++++++++++++++++++- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/liolib.c b/liolib.c index 3225ed5f2c..c8f165cb05 100644 --- a/liolib.c +++ b/liolib.c @@ -732,18 +732,19 @@ static int f_setvbuf (lua_State *L) { } - -static int io_flush (lua_State *L) { - FILE *f = getiofile(L, IO_OUTPUT); +static int aux_flush (lua_State *L, FILE *f) { errno = 0; return luaL_fileresult(L, fflush(f) == 0, NULL); } static int f_flush (lua_State *L) { - FILE *f = tofile(L); - errno = 0; - return luaL_fileresult(L, fflush(f) == 0, NULL); + return aux_flush(L, tofile(L)); +} + + +static int io_flush (lua_State *L) { + return aux_flush(L, getiofile(L, IO_OUTPUT)); } diff --git a/testes/files.lua b/testes/files.lua index 2c802047df..53edf3145f 100644 --- a/testes/files.lua +++ b/testes/files.lua @@ -347,7 +347,7 @@ collectgarbage() assert(io.write(' ' .. t .. ' ')) assert(io.write(';', 'end of file\n')) -f:flush(); io.flush() +assert(f:flush()); assert(io.flush()) f:close() print('+') @@ -461,6 +461,23 @@ do -- testing closing file in line iteration end +do print("testing flush") + local f = io.output("/dev/null") + assert(f:write("abcd")) -- write to buffer + assert(f:flush()) -- write to device + assert(f:write("abcd")) -- write to buffer + assert(io.flush()) -- write to device + assert(f:close()) + + local f = io.output("/dev/full") + assert(f:write("abcd")) -- write to buffer + assert(not f:flush()) -- cannot write to device + assert(f:write("abcd")) -- write to buffer + assert(not io.flush()) -- cannot write to device + assert(f:close()) +end + + -- test for multipe arguments in 'lines' io.output(file); io.write"0123456789\n":close() for a,b in io.lines(file, 1, 1) do From 3dd8ea54daa77345a8f193e871f6792722d8e131 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 3 Apr 2025 15:31:22 -0300 Subject: [PATCH 1033/1145] Order change in 'pushfuncname' 'pushglobalfuncname' can be quite slow (as it traverses all globals and all loaded modules), so try first to get a name from the code. --- lauxlib.c | 10 +++++----- testes/db.lua | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/lauxlib.c b/lauxlib.c index 7c9ad53b2b..7f33f0addb 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -94,14 +94,14 @@ static int pushglobalfuncname (lua_State *L, lua_Debug *ar) { static void pushfuncname (lua_State *L, lua_Debug *ar) { - if (pushglobalfuncname(L, ar)) { /* try first a global name */ - lua_pushfstring(L, "function '%s'", lua_tostring(L, -1)); - lua_remove(L, -2); /* remove name */ - } - else if (*ar->namewhat != '\0') /* is there a name from code? */ + if (*ar->namewhat != '\0') /* is there a name from code? */ lua_pushfstring(L, "%s '%s'", ar->namewhat, ar->name); /* use it */ else if (*ar->what == 'm') /* main? */ lua_pushliteral(L, "main chunk"); + else if (pushglobalfuncname(L, ar)) { /* try a global name */ + lua_pushfstring(L, "function '%s'", lua_tostring(L, -1)); + lua_remove(L, -2); /* remove name */ + } else if (*ar->what != 'C') /* for Lua functions, use */ lua_pushfstring(L, "function <%s:%d>", ar->short_src, ar->linedefined); else /* nothing left... */ diff --git a/testes/db.lua b/testes/db.lua index 3c821ab7d5..8e13373c2d 100644 --- a/testes/db.lua +++ b/testes/db.lua @@ -701,7 +701,7 @@ assert(debug.traceback(print, 4) == print) assert(string.find(debug.traceback("hi", 4), "^hi\n")) assert(string.find(debug.traceback("hi"), "^hi\n")) assert(not string.find(debug.traceback("hi"), "'debug.traceback'")) -assert(string.find(debug.traceback("hi", 0), "'debug.traceback'")) +assert(string.find(debug.traceback("hi", 0), "'traceback'")) assert(string.find(debug.traceback(), "^stack traceback:\n")) do -- C-function names in traceback @@ -829,7 +829,7 @@ end co = coroutine.create(function (x) f(x) end) a, b = coroutine.resume(co, 3) -t = {"'coroutine.yield'", "'f'", "in function <"} +t = {"'yield'", "'f'", "in function <"} while coroutine.status(co) == "suspended" do checktraceback(co, t) a, b = coroutine.resume(co) From 3dbb1a4b894c0744a331d4319d8d1704dc4ad943 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 15 Apr 2025 17:00:30 -0300 Subject: [PATCH 1034/1145] In gen. GC, some gray objects stay in gray lists In generational collection, objects marked as touched1 stay in gray lists between collections. This commit fixes a bug introduced in commit 808976bb59. --- lgc.c | 7 ++++++- lstrlib.c | 4 +++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/lgc.c b/lgc.c index cada07d9a1..c0d68377b9 100644 --- a/lgc.c +++ b/lgc.c @@ -465,6 +465,8 @@ static void restartcollection (global_State *g) { ** TOUCHED1 objects need to be in the list. TOUCHED2 doesn't need to go ** back to a gray list, but then it must become OLD. (That is what ** 'correctgraylist' does when it finds a TOUCHED2 object.) +** This function is a no-op in incremental mode, as objects cannot be +** marked as touched in that mode. */ static void genlink (global_State *g, GCObject *o) { lua_assert(isblack(o)); @@ -480,7 +482,8 @@ static void genlink (global_State *g, GCObject *o) { ** Traverse a table with weak values and link it to proper list. During ** propagate phase, keep it in 'grayagain' list, to be revisited in the ** atomic phase. In the atomic phase, if table has any white value, -** put it in 'weak' list, to be cleared. +** put it in 'weak' list, to be cleared; otherwise, call 'genlink' +** to check table age in generational mode. */ static void traverseweakvalue (global_State *g, Table *h) { Node *n, *limit = gnodelast(h); @@ -501,6 +504,8 @@ static void traverseweakvalue (global_State *g, Table *h) { linkgclist(h, g->grayagain); /* must retraverse it in atomic phase */ else if (hasclears) linkgclist(h, g->weak); /* has to be cleared later */ + else + genlink(g, obj2gco(h)); } diff --git a/lstrlib.c b/lstrlib.c index 321d6a0b0a..306cd0bfeb 100644 --- a/lstrlib.c +++ b/lstrlib.c @@ -1544,8 +1544,10 @@ static KOption getdetails (Header *h, size_t totalsize, const char **fmt, else { if (align > h->maxalign) /* enforce maximum alignment */ align = h->maxalign; - if (l_unlikely(!ispow2(align))) /* not a power of 2? */ + if (l_unlikely(!ispow2(align))) { /* not a power of 2? */ + *ntoalign = 0; /* to avoid warnings */ luaL_argerror(h->L, 1, "format asks for alignment not power of 2"); + } else { /* 'szmoda' = totalsize % align */ unsigned szmoda = cast_uint(totalsize & (align - 1)); From 50fd8d03c33bbe52ac5b34c4eb748197b349cedd Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 17 Apr 2025 14:58:55 -0300 Subject: [PATCH 1035/1145] Function 'luaK_semerror' made vararg All calls to 'luaK_semerror' were using 'luaO_pushfstring' to create the error messages. --- lcode.c | 9 ++++++++- lcode.h | 2 +- lparser.c | 30 ++++++++++++------------------ 3 files changed, 21 insertions(+), 20 deletions(-) diff --git a/lcode.c b/lcode.c index 8c04d8ab16..d22a081aed 100644 --- a/lcode.c +++ b/lcode.c @@ -40,7 +40,14 @@ static int codesJ (FuncState *fs, OpCode o, int sj, int k); /* semantic error */ -l_noret luaK_semerror (LexState *ls, const char *msg) { +l_noret luaK_semerror (LexState *ls, const char *fmt, ...) { + const char *msg; + va_list argp; + va_start(argp, fmt); + msg = luaO_pushvfstring(ls->L, fmt, argp); + va_end(argp); + if (msg == NULL) /* error? */ + luaD_throw(ls->L, LUA_ERRMEM); ls->t.token = 0; /* remove "near " from final message */ luaX_syntaxerror(ls, msg); } diff --git a/lcode.h b/lcode.h index 414ebe3999..94fc2417dd 100644 --- a/lcode.h +++ b/lcode.h @@ -97,7 +97,7 @@ LUAI_FUNC void luaK_settablesize (FuncState *fs, int pc, int ra, int asize, int hsize); LUAI_FUNC void luaK_setlist (FuncState *fs, int base, int nelems, int tostore); LUAI_FUNC void luaK_finish (FuncState *fs); -LUAI_FUNC l_noret luaK_semerror (LexState *ls, const char *msg); +LUAI_FUNC l_noret luaK_semerror (LexState *ls, const char *fmt, ...); #endif diff --git a/lparser.c b/lparser.c index 380e45f58c..e71d721211 100644 --- a/lparser.c +++ b/lparser.c @@ -306,11 +306,9 @@ static void check_readonly (LexState *ls, expdesc *e) { default: return; /* other cases cannot be read-only */ } - if (varname) { - const char *msg = luaO_pushfstring(ls->L, - "attempt to assign to const variable '%s'", getstr(varname)); - luaK_semerror(ls, msg); /* error */ - } + if (varname) + luaK_semerror(ls, "attempt to assign to const variable '%s'", + getstr(varname)); } @@ -523,9 +521,9 @@ static void adjust_assign (LexState *ls, int nvars, int nexps, expdesc *e) { static l_noret jumpscopeerror (LexState *ls, Labeldesc *gt) { TString *tsname = getlocalvardesc(ls->fs, gt->nactvar)->vd.name; const char *varname = getstr(tsname); - const char *msg = " at line %d jumps into the scope of local '%s'"; - msg = luaO_pushfstring(ls->L, msg, getstr(gt->name), gt->line, varname); - luaK_semerror(ls, msg); /* raise the error */ + luaK_semerror(ls, + " at line %d jumps into the scope of local '%s'", + getstr(gt->name), gt->line, varname); /* raise the error */ } @@ -677,11 +675,10 @@ static void enterblock (FuncState *fs, BlockCnt *bl, lu_byte isloop) { ** generates an error for an undefined 'goto'. */ static l_noret undefgoto (LexState *ls, Labeldesc *gt) { - const char *msg = "no visible label '%s' for at line %d"; - msg = luaO_pushfstring(ls->L, msg, getstr(gt->name), gt->line); /* breaks are checked when created, cannot be undefined */ lua_assert(!eqstr(gt->name, luaS_newliteral(ls->L, "break"))); - luaK_semerror(ls, msg); + luaK_semerror(ls, "no visible label '%s' for at line %d", + getstr(gt->name), gt->line); } @@ -1479,11 +1476,9 @@ static void breakstat (LexState *ls, int line) { */ static void checkrepeated (LexState *ls, TString *name) { Labeldesc *lb = findlabel(ls, name, ls->fs->firstlabel); - if (l_unlikely(lb != NULL)) { /* already defined? */ - const char *msg = "label '%s' already defined on line %d"; - msg = luaO_pushfstring(ls->L, msg, getstr(name), lb->line); - luaK_semerror(ls, msg); /* error */ - } + if (l_unlikely(lb != NULL)) /* already defined? */ + luaK_semerror(ls, "label '%s' already defined on line %d", + getstr(name), lb->line); /* error */ } @@ -1718,8 +1713,7 @@ static lu_byte getlocalattribute (LexState *ls) { else if (strcmp(attr, "close") == 0) return RDKTOCLOSE; /* to-be-closed variable */ else - luaK_semerror(ls, - luaO_pushfstring(ls->L, "unknown attribute '%s'", attr)); + luaK_semerror(ls, "unknown attribute '%s'", attr); } return VDKREG; /* regular variable */ } From 9b014d4bcd4ebadb523f1c1a1d38148d8526e5ed Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 23 Apr 2025 11:36:09 -0300 Subject: [PATCH 1036/1145] Details (typos in comments) --- ldo.c | 2 +- lgc.c | 1 - lparser.c | 2 +- lvm.c | 2 +- testes/api.lua | 6 +++--- testes/constructs.lua | 2 +- testes/db.lua | 12 ++++++------ testes/errors.lua | 2 +- testes/files.lua | 2 +- testes/gc.lua | 10 +++++----- testes/locals.lua | 2 +- testes/main.lua | 4 ++-- testes/math.lua | 2 +- testes/nextvar.lua | 4 ++-- testes/pm.lua | 4 ++-- testes/utf8.lua | 2 +- 16 files changed, 29 insertions(+), 30 deletions(-) diff --git a/ldo.c b/ldo.c index 3e5c7504f4..820b5a9ad0 100644 --- a/ldo.c +++ b/ldo.c @@ -513,7 +513,7 @@ l_sinline void genmoveresults (lua_State *L, StkId res, int nres, ** to 'res'. Handle most typical cases (zero results for commands, ** one result for expressions, multiple results for tail calls/single ** parameters) separated. The flag CIST_TBC in 'fwanted', if set, -** forces the swicth to go to the default case. +** forces the switch to go to the default case. */ l_sinline void moveresults (lua_State *L, StkId res, int nres, l_uint32 fwanted) { diff --git a/lgc.c b/lgc.c index c0d68377b9..f0045dd6f2 100644 --- a/lgc.c +++ b/lgc.c @@ -126,7 +126,6 @@ static l_mem objsize (GCObject *o) { CClosure *cl = gco2ccl(o); res = sizeCclosure(cl->nupvalues); break; - break; } case LUA_VUSERDATA: { Udata *u = gco2u(o); diff --git a/lparser.c b/lparser.c index e71d721211..e7e05f480e 100644 --- a/lparser.c +++ b/lparser.c @@ -561,7 +561,7 @@ static void closegoto (LexState *ls, int g, Labeldesc *label, int bup) { /* ** Search for an active label with the given name, starting at -** index 'ilb' (so that it can searh for all labels in current block +** index 'ilb' (so that it can search for all labels in current block ** or all labels in current function). */ static Labeldesc *findlabel (LexState *ls, TString *name, int ilb) { diff --git a/lvm.c b/lvm.c index e2c36ef577..97dfe5ee62 100644 --- a/lvm.c +++ b/lvm.c @@ -327,7 +327,7 @@ lu_byte luaV_finishget (lua_State *L, const TValue *t, TValue *key, ** Finish a table assignment 't[key] = val'. ** About anchoring the table before the call to 'luaH_finishset': ** This call may trigger an emergency collection. When loop>0, -** the table being acessed is a field in some metatable. If this +** the table being accessed is a field in some metatable. If this ** metatable is weak and the table is not anchored, this collection ** could collect that table while it is being updated. */ diff --git a/testes/api.lua b/testes/api.lua index 49e3f9b987..b3791654d4 100644 --- a/testes/api.lua +++ b/testes/api.lua @@ -114,7 +114,7 @@ end -- testing warnings T.testC([[ - warningC "#This shold be a" + warningC "#This should be a" warningC " single " warning "warning" warningC "#This should be " @@ -162,7 +162,7 @@ do -- test returning more results than fit in the caller stack end -do -- testing multipe returns +do -- testing multiple returns local function foo (n) if n > 0 then return n, foo(n - 1) end end @@ -902,7 +902,7 @@ F = function (x) assert(T.udataval(A) == B) debug.getmetatable(A) -- just access it end - A = x -- ressurect userdata + A = x -- resurrect userdata B = udval return 1,2,3 end diff --git a/testes/constructs.lua b/testes/constructs.lua index 3f6d506f0a..94f670c7a5 100644 --- a/testes/constructs.lua +++ b/testes/constructs.lua @@ -60,7 +60,7 @@ assert((x>y) and x or y == 2); assert(1234567890 == tonumber('1234567890') and 1234567890+1 == 1234567891) -do -- testing operators with diffent kinds of constants +do -- testing operators with different kinds of constants -- operands to consider: -- * fit in register -- * constant doesn't fit in register diff --git a/testes/db.lua b/testes/db.lua index 8e13373c2d..e4982c207a 100644 --- a/testes/db.lua +++ b/testes/db.lua @@ -431,7 +431,7 @@ do assert(a == nil and not b) end --- testing iteraction between multiple values x hooks +-- testing interaction between multiple values x hooks do local function f(...) return 3, ... end local count = 0 @@ -587,7 +587,7 @@ t = getupvalues(foo2) assert(t.a == 1 and t.b == 2 and t.c == 3) assert(debug.setupvalue(foo1, 1, "xuxu") == "b") assert(({debug.getupvalue(foo2, 3)})[2] == "xuxu") --- upvalues of C functions are allways "called" "" (the empty string) +-- upvalues of C functions are always named "" (the empty string) assert(debug.getupvalue(string.gmatch("x", "x"), 1) == "") @@ -839,7 +839,7 @@ t[1] = "'error'" checktraceback(co, t) --- test acessing line numbers of a coroutine from a resume inside +-- test accessing line numbers of a coroutine from a resume inside -- a C function (this is a known bug in Lua 5.0) local function g(x) @@ -966,9 +966,9 @@ local debug = require'debug' local a = 12 -- a local variable local n, v = debug.getlocal(1, 1) -assert(n == "(temporary)" and v == debug) -- unkown name but known value +assert(n == "(temporary)" and v == debug) -- unknown name but known value n, v = debug.getlocal(1, 2) -assert(n == "(temporary)" and v == 12) -- unkown name but known value +assert(n == "(temporary)" and v == 12) -- unknown name but known value -- a function with an upvalue local f = function () local x; return a end @@ -1018,7 +1018,7 @@ do -- bug in 5.4.0: line hooks in stripped code line = l end, "l") assert(s() == 2); debug.sethook(nil) - assert(line == nil) -- hook called withoug debug info for 1st instruction + assert(line == nil) -- hook called without debug info for 1st instruction end do -- tests for 'source' in binary dumps diff --git a/testes/errors.lua b/testes/errors.lua index c1c40fecdf..c80051fc7a 100644 --- a/testes/errors.lua +++ b/testes/errors.lua @@ -507,7 +507,7 @@ end if not _soft then - -- several tests that exaust the Lua stack + -- several tests that exhaust the Lua stack collectgarbage() print"testing stack overflow" local C = 0 diff --git a/testes/files.lua b/testes/files.lua index 53edf3145f..a0ae661c40 100644 --- a/testes/files.lua +++ b/testes/files.lua @@ -478,7 +478,7 @@ do print("testing flush") end --- test for multipe arguments in 'lines' +-- test for multiple arguments in 'lines' io.output(file); io.write"0123456789\n":close() for a,b in io.lines(file, 1, 1) do if a == "\n" then assert(not b) diff --git a/testes/gc.lua b/testes/gc.lua index 0693837c35..ca8aa1bc51 100644 --- a/testes/gc.lua +++ b/testes/gc.lua @@ -446,8 +446,8 @@ do -- tests for string keys in weak tables local m = collectgarbage("count") -- current memory local a = setmetatable({}, {__mode = "kv"}) a[string.rep("a", 2^22)] = 25 -- long string key -> number value - a[string.rep("b", 2^22)] = {} -- long string key -> colectable value - a[{}] = 14 -- colectable key + a[string.rep("b", 2^22)] = {} -- long string key -> collectable value + a[{}] = 14 -- collectable key collectgarbage() local k, v = next(a) -- string key with number value preserved assert(k == string.rep("a", 2^22) and v == 25) @@ -459,7 +459,7 @@ do -- tests for string keys in weak tables assert(next(a) == nil) -- make sure will not try to compare with dead key assert(a[string.rep("b", 100)] == undef) - assert(collectgarbage("count") <= m + 1) -- eveything collected + assert(collectgarbage("count") <= m + 1) -- everything collected end @@ -524,7 +524,7 @@ do local co = coroutine.create(f) assert(coroutine.resume(co, co)) end - -- Now, thread and closure are not reacheable any more. + -- Now, thread and closure are not reachable any more. collectgarbage() assert(collected) collectgarbage("restart") @@ -644,7 +644,7 @@ do assert(getmetatable(o) == tt) -- create new objects during GC local a = 'xuxu'..(10+3)..'joao', {} - ___Glob = o -- ressurrect object! + ___Glob = o -- resurrect object! setmetatable({}, tt) -- creates a new one with same metatable print(">>> closing state " .. "<<<\n") end diff --git a/testes/locals.lua b/testes/locals.lua index ccea0a1422..eeeb4338fe 100644 --- a/testes/locals.lua +++ b/testes/locals.lua @@ -1162,7 +1162,7 @@ do local function open (x) numopen = numopen + 1 return - function () -- iteraction function + function () -- iteration function x = x - 1 if x > 0 then return x end end, diff --git a/testes/main.lua b/testes/main.lua index bf3c898eed..eb63d58859 100644 --- a/testes/main.lua +++ b/testes/main.lua @@ -226,7 +226,7 @@ RUN("lua -l 'str=string' '-lm=math' -e 'print(m.sin(0))' %s > %s", prog, out) checkout("0.0\nALO ALO\t20\n") --- test module names with version sufix ("libs/lib2-v2") +-- test module names with version suffix ("libs/lib2-v2") RUN("env LUA_CPATH='./libs/?.so' lua -l lib2-v2 -e 'print(lib2.id())' > %s", out) checkout("true\n") @@ -347,7 +347,7 @@ checkout("a\n") RUN([[lua "-eprint(1)" -ea=3 -e "print(a)" > %s]], out) checkout("1\n3\n") --- test iteractive mode +-- test interactive mode prepfile[[ (6*2-6) -- === a = diff --git a/testes/math.lua b/testes/math.lua index bad8bc5e71..88a57ce75a 100644 --- a/testes/math.lua +++ b/testes/math.lua @@ -1071,7 +1071,7 @@ do assert(x == tonumber(tostring(x))) end - -- different numbers shold print differently. + -- different numbers should print differently. -- check pairs of floats with minimum detectable difference local p = floatbits - 1 for i = 1, maxexp - 1 do diff --git a/testes/nextvar.lua b/testes/nextvar.lua index 031ad3fd99..679cb1e4ac 100644 --- a/testes/nextvar.lua +++ b/testes/nextvar.lua @@ -227,7 +227,7 @@ for i = 1,lim do end --- insert and delete elements until a rehash occurr. Caller must ensure +-- insert and delete elements until a rehash occur. Caller must ensure -- that a rehash will change the shape of the table. Must repeat because -- the insertion may collide with the deleted element, and then there is -- no rehash. @@ -349,7 +349,7 @@ a,b,c = 1,2,3 a,b,c = nil --- next uses always the same iteraction function +-- next uses always the same iteration function assert(next{} == next{}) local function find (name) diff --git a/testes/pm.lua b/testes/pm.lua index 2a0cfb0bb5..ab19eb5db8 100644 --- a/testes/pm.lua +++ b/testes/pm.lua @@ -23,9 +23,9 @@ a,b = string.find('alo', '') assert(a == 1 and b == 0) a,b = string.find('a\0o a\0o a\0o', 'a', 1) -- first position assert(a == 1 and b == 1) -a,b = string.find('a\0o a\0o a\0o', 'a\0o', 2) -- starts in the midle +a,b = string.find('a\0o a\0o a\0o', 'a\0o', 2) -- starts in the middle assert(a == 5 and b == 7) -a,b = string.find('a\0o a\0o a\0o', 'a\0o', 9) -- starts in the midle +a,b = string.find('a\0o a\0o a\0o', 'a\0o', 9) -- starts in the middle assert(a == 9 and b == 11) a,b = string.find('a\0a\0a\0a\0\0ab', '\0ab', 2); -- finds at the end assert(a == 9 and b == 11); diff --git a/testes/utf8.lua b/testes/utf8.lua index 0704782c12..d0c0184d34 100644 --- a/testes/utf8.lua +++ b/testes/utf8.lua @@ -134,7 +134,7 @@ do errorcodes("\xbfinvalid") errorcodes("αλφ\xBFα") - -- calling interation function with invalid arguments + -- calling iteration function with invalid arguments local f = utf8.codes("") assert(f("", 2) == nil) assert(f("", -1) == nil) From e05590591410a5e007a1e3f1691f6c1cf9d8fe45 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 23 Apr 2025 11:55:04 -0300 Subject: [PATCH 1037/1145] New macro 'pushvfstring' Helps to ensure that 'luaO_pushvfstring' is being called correctly, with an error check after closing the vararg list with 'va_end'. --- lapi.c | 6 +----- lcode.c | 6 +----- ldebug.c | 8 ++------ lobject.h | 9 +++++++++ 4 files changed, 13 insertions(+), 16 deletions(-) diff --git a/lapi.c b/lapi.c index f59430a7f3..769eba13b6 100644 --- a/lapi.c +++ b/lapi.c @@ -593,12 +593,8 @@ LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) { const char *ret; va_list argp; lua_lock(L); - va_start(argp, fmt); - ret = luaO_pushvfstring(L, fmt, argp); - va_end(argp); + pushvfstring(L, argp, fmt, ret); luaC_checkGC(L); - if (ret == NULL) /* error? */ - luaD_throw(L, LUA_ERRMEM); lua_unlock(L); return ret; } diff --git a/lcode.c b/lcode.c index d22a081aed..e8b9bb5dbd 100644 --- a/lcode.c +++ b/lcode.c @@ -43,11 +43,7 @@ static int codesJ (FuncState *fs, OpCode o, int sj, int k); l_noret luaK_semerror (LexState *ls, const char *fmt, ...) { const char *msg; va_list argp; - va_start(argp, fmt); - msg = luaO_pushvfstring(ls->L, fmt, argp); - va_end(argp); - if (msg == NULL) /* error? */ - luaD_throw(ls->L, LUA_ERRMEM); + pushvfstring(ls->L, argp, fmt, msg); ls->t.token = 0; /* remove "near " from final message */ luaX_syntaxerror(ls, msg); } diff --git a/ldebug.c b/ldebug.c index 258a439478..f4bb0a08a9 100644 --- a/ldebug.c +++ b/ldebug.c @@ -852,12 +852,8 @@ l_noret luaG_runerror (lua_State *L, const char *fmt, ...) { const char *msg; va_list argp; luaC_checkGC(L); /* error message uses memory */ - va_start(argp, fmt); - msg = luaO_pushvfstring(L, fmt, argp); /* format message */ - va_end(argp); - if (msg == NULL) /* no memory to format message? */ - luaD_throw(L, LUA_ERRMEM); - else if (isLua(ci)) { /* Lua function? */ + pushvfstring(L, argp, fmt, msg); + if (isLua(ci)) { /* Lua function? */ /* add source:line information */ luaG_addinfo(L, msg, ci_func(ci)->p->source, getcurrentline(ci)); setobjs2s(L, L->top.p - 2, L->top.p - 1); /* remove 'msg' */ diff --git a/lobject.h b/lobject.h index 8c06a224cc..b5ca36680d 100644 --- a/lobject.h +++ b/lobject.h @@ -822,6 +822,15 @@ typedef struct Table { /* size of buffer for 'luaO_utf8esc' function */ #define UTF8BUFFSZ 8 + +/* macro to call 'luaO_pushvfstring' correctly */ +#define pushvfstring(L, argp, fmt, msg) \ + { va_start(argp, fmt); \ + msg = luaO_pushvfstring(L, fmt, argp); \ + va_end(argp); \ + if (msg == NULL) luaD_throw(L, LUA_ERRMEM); /* only after 'va_end' */ } + + LUAI_FUNC int luaO_utf8esc (char *buff, unsigned long x); LUAI_FUNC lu_byte luaO_ceillog2 (unsigned int x); LUAI_FUNC lu_byte luaO_codeparam (unsigned int p); From be8120906304a8658fab998587b969e0e42f5650 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 5 May 2025 16:24:59 -0300 Subject: [PATCH 1038/1145] First implementation of global declarations --- llex.c | 7 +- llex.h | 4 +- lparser.c | 111 +++++++++++++++++++--------- lparser.h | 15 +++- ltests.h | 1 + luaconf.h | 6 ++ manual/manual.of | 184 ++++++++++++++++++++++++++++------------------- testes/db.lua | 1 + testes/goto.lua | 44 +++++++++++- testes/math.lua | 16 ++++- 10 files changed, 272 insertions(+), 117 deletions(-) diff --git a/llex.c b/llex.c index 4b5a1f7509..9d93224f28 100644 --- a/llex.c +++ b/llex.c @@ -40,11 +40,16 @@ #define currIsNewline(ls) (ls->current == '\n' || ls->current == '\r') +#if defined(LUA_COMPAT_GLOBAL) +#define GLOBALLEX ".g" /* not recognizable by the scanner */ +#else +#define GLOBALLEX "global" +#endif /* ORDER RESERVED */ static const char *const luaX_tokens [] = { "and", "break", "do", "else", "elseif", - "end", "false", "for", "function", "goto", "if", + "end", "false", "for", "function", GLOBALLEX, "goto", "if", "in", "local", "nil", "not", "or", "repeat", "return", "then", "true", "until", "while", "//", "..", "...", "==", ">=", "<=", "~=", diff --git a/llex.h b/llex.h index c3500ef6a8..078e4d31e5 100644 --- a/llex.h +++ b/llex.h @@ -33,8 +33,8 @@ enum RESERVED { /* terminal symbols denoted by reserved words */ TK_AND = FIRST_RESERVED, TK_BREAK, TK_DO, TK_ELSE, TK_ELSEIF, TK_END, TK_FALSE, TK_FOR, TK_FUNCTION, - TK_GOTO, TK_IF, TK_IN, TK_LOCAL, TK_NIL, TK_NOT, TK_OR, TK_REPEAT, - TK_RETURN, TK_THEN, TK_TRUE, TK_UNTIL, TK_WHILE, + TK_GLOBAL, TK_GOTO, TK_IF, TK_IN, TK_LOCAL, TK_NIL, TK_NOT, TK_OR, + TK_REPEAT, TK_RETURN, TK_THEN, TK_TRUE, TK_UNTIL, TK_WHILE, /* other terminal symbols */ TK_IDIV, TK_CONCAT, TK_DOTS, TK_EQ, TK_GE, TK_LE, TK_NE, TK_SHL, TK_SHR, diff --git a/lparser.c b/lparser.c index e7e05f480e..1c5fdca6f3 100644 --- a/lparser.c +++ b/lparser.c @@ -30,8 +30,8 @@ -/* maximum number of local variables per function (must be smaller - than 250, due to the bytecode format) */ +/* maximum number of variable declarationss per function (must be + smaller than 250, due to the bytecode format) */ #define MAXVARS 200 @@ -54,6 +54,7 @@ typedef struct BlockCnt { lu_byte upval; /* true if some variable in the block is an upvalue */ lu_byte isloop; /* 1 if 'block' is a loop; 2 if it has pending breaks */ lu_byte insidetbc; /* true if inside the scope of a to-be-closed var. */ + lu_byte globdec; /* true if inside the scope of any global declaration */ } BlockCnt; @@ -188,10 +189,10 @@ static short registerlocalvar (LexState *ls, FuncState *fs, /* -** Create a new local variable with the given 'name' and given 'kind'. +** Create a new variable with the given 'name' and given 'kind'. ** Return its index in the function. */ -static int new_localvarkind (LexState *ls, TString *name, lu_byte kind) { +static int new_varkind (LexState *ls, TString *name, lu_byte kind) { lua_State *L = ls->L; FuncState *fs = ls->fs; Dyndata *dyd = ls->dyd; @@ -211,7 +212,7 @@ static int new_localvarkind (LexState *ls, TString *name, lu_byte kind) { ** Create a new local variable with the given 'name' and regular kind. */ static int new_localvar (LexState *ls, TString *name) { - return new_localvarkind(ls, name, VDKREG); + return new_varkind(ls, name, VDKREG); } #define new_localvarliteral(ls,v) \ @@ -238,7 +239,7 @@ static Vardesc *getlocalvardesc (FuncState *fs, int vidx) { static lu_byte reglevel (FuncState *fs, int nvar) { while (nvar-- > 0) { Vardesc *vd = getlocalvardesc(fs, nvar); /* get previous variable */ - if (vd->vd.kind != RDKCTC) /* is in a register? */ + if (varinreg(vd)) /* is in a register? */ return cast_byte(vd->vd.ridx + 1); } return 0; /* no variables in registers */ @@ -259,7 +260,7 @@ lu_byte luaY_nvarstack (FuncState *fs) { */ static LocVar *localdebuginfo (FuncState *fs, int vidx) { Vardesc *vd = getlocalvardesc(fs, vidx); - if (vd->vd.kind == RDKCTC) + if (!varinreg(vd)) return NULL; /* no debug info. for constants */ else { int idx = vd->vd.pidx; @@ -401,7 +402,9 @@ static int searchvar (FuncState *fs, TString *n, expdesc *var) { if (eqstr(n, vd->vd.name)) { /* found? */ if (vd->vd.kind == RDKCTC) /* compile-time constant? */ init_exp(var, VCONST, fs->firstlocal + i); - else /* real variable */ + else if (vd->vd.kind == GDKREG || vd->vd.kind == GDKCONST) + init_exp(var, VGLOBAL, i); + else /* local variable */ init_var(fs, var, i); return cast_int(var->k); } @@ -440,25 +443,24 @@ static void marktobeclosed (FuncState *fs) { ** 'var' as 'void' as a flag. */ static void singlevaraux (FuncState *fs, TString *n, expdesc *var, int base) { - if (fs == NULL) /* no more levels? */ - init_exp(var, VVOID, 0); /* default is global */ - else { - int v = searchvar(fs, n, var); /* look up locals at current level */ - if (v >= 0) { /* found? */ - if (v == VLOCAL && !base) - markupval(fs, var->u.var.vidx); /* local will be used as an upval */ - } - else { /* not found as local at current level; try upvalues */ - int idx = searchupvalue(fs, n); /* try existing upvalues */ - if (idx < 0) { /* not found? */ + int v = searchvar(fs, n, var); /* look up locals at current level */ + if (v >= 0) { /* found? */ + if (v == VLOCAL && !base) + markupval(fs, var->u.var.vidx); /* local will be used as an upval */ + } + else { /* not found as local at current level; try upvalues */ + int idx = searchupvalue(fs, n); /* try existing upvalues */ + if (idx < 0) { /* not found? */ + if (fs->prev != NULL) /* more levels? */ singlevaraux(fs->prev, n, var, 0); /* try upper levels */ - if (var->k == VLOCAL || var->k == VUPVAL) /* local or upvalue? */ - idx = newupvalue(fs, n, var); /* will be a new upvalue */ - else /* it is a global or a constant */ - return; /* don't need to do anything at this level */ - } - init_exp(var, VUPVAL, idx); /* new or old upvalue */ + else /* no more levels */ + init_exp(var, VGLOBAL, -1); /* global by default */ + if (var->k == VLOCAL || var->k == VUPVAL) /* local or upvalue? */ + idx = newupvalue(fs, n, var); /* will be a new upvalue */ + else /* it is a global or a constant */ + return; /* don't need to do anything at this level */ } + init_exp(var, VUPVAL, idx); /* new or old upvalue */ } } @@ -471,10 +473,15 @@ static void singlevar (LexState *ls, expdesc *var) { TString *varname = str_checkname(ls); FuncState *fs = ls->fs; singlevaraux(fs, varname, var, 1); - if (var->k == VVOID) { /* global name? */ + if (var->k == VGLOBAL) { /* global name? */ expdesc key; + /* global by default in the scope of a global declaration? */ + if (var->u.info == -1 && fs->bl->globdec) + luaK_semerror(ls, "variable '%s' not declared", getstr(varname)); singlevaraux(fs, ls->envn, var, 1); /* get environment variable */ - lua_assert(var->k != VVOID); /* this one must exist */ + if (var->k == VGLOBAL) + luaK_semerror(ls, "_ENV is global when accessing variable '%s'", + getstr(varname)); luaK_exp2anyregup(fs, var); /* but could be a constant */ codestring(&key, varname); /* key is variable name */ luaK_indexed(fs, var, &key); /* env[varname] */ @@ -664,8 +671,13 @@ static void enterblock (FuncState *fs, BlockCnt *bl, lu_byte isloop) { bl->firstlabel = fs->ls->dyd->label.n; bl->firstgoto = fs->ls->dyd->gt.n; bl->upval = 0; + /* inherit 'insidetbc' from enclosing block */ bl->insidetbc = (fs->bl != NULL && fs->bl->insidetbc); - bl->previous = fs->bl; + /* inherit 'globdec' from enclosing block or enclosing function */ + bl->globdec = fs->bl != NULL ? fs->bl->globdec + : fs->prev != NULL ? fs->prev->bl->globdec + : 0; /* chunk's first block */ + bl->previous = fs->bl; /* link block in function's block list */ fs->bl = bl; lua_assert(fs->freereg == luaY_nvarstack(fs)); } @@ -1600,7 +1612,7 @@ static void fornum (LexState *ls, TString *varname, int line) { int base = fs->freereg; new_localvarliteral(ls, "(for state)"); new_localvarliteral(ls, "(for state)"); - new_localvarkind(ls, varname, RDKCONST); /* control variable */ + new_varkind(ls, varname, RDKCONST); /* control variable */ checknext(ls, '='); exp1(ls); /* initial value */ checknext(ls, ','); @@ -1627,7 +1639,7 @@ static void forlist (LexState *ls, TString *indexname) { new_localvarliteral(ls, "(for state)"); /* iterator function */ new_localvarliteral(ls, "(for state)"); /* state */ new_localvarliteral(ls, "(for state)"); /* closing var. (after swap) */ - new_localvarkind(ls, indexname, RDKCONST); /* control variable */ + new_varkind(ls, indexname, RDKCONST); /* control variable */ /* other declared variables */ while (testnext(ls, ',')) { new_localvar(ls, str_checkname(ls)); @@ -1702,7 +1714,7 @@ static void localfunc (LexState *ls) { } -static lu_byte getlocalattribute (LexState *ls) { +static lu_byte getvarattribute (LexState *ls) { /* ATTRIB -> ['<' Name '>'] */ if (testnext(ls, '<')) { TString *ts = str_checkname(ls); @@ -1738,8 +1750,8 @@ static void localstat (LexState *ls) { expdesc e; do { TString *vname = str_checkname(ls); - lu_byte kind = getlocalattribute(ls); - vidx = new_localvarkind(ls, vname, kind); + lu_byte kind = getvarattribute(ls); + vidx = new_varkind(ls, vname, kind); if (kind == RDKTOCLOSE) { /* to-be-closed? */ if (toclose != -1) /* one already present? */ luaK_semerror(ls, "multiple to-be-closed variables in local list"); @@ -1769,6 +1781,24 @@ static void localstat (LexState *ls) { } +static void globalstat (LexState *ls) { + FuncState *fs = ls->fs; + luaX_next(ls); /* skip 'global' */ + do { + TString *vname = str_checkname(ls); + lu_byte kind = getvarattribute(ls); + if (kind == RDKTOCLOSE) + luaK_semerror(ls, "global variable ('%s') cannot be to-be-closed", + getstr(vname)); + /* adjust kind for global variable */ + kind = (kind == VDKREG) ? GDKREG : GDKCONST; + new_varkind(ls, vname, kind); + fs->nactvar++; /* activate declaration */ + } while (testnext(ls, ',')); + fs->bl->globdec = 1; /* code is in the scope of a global declaration */ +} + + static int funcname (LexState *ls, expdesc *v) { /* funcname -> NAME {fieldsel} [':' NAME] */ int ismethod = 0; @@ -1888,6 +1918,10 @@ static void statement (LexState *ls) { localstat(ls); break; } + case TK_GLOBAL: { /* stat -> globalstat */ + globalstat(ls); + break; + } case TK_DBCOLON: { /* stat -> label */ luaX_next(ls); /* skip double colon */ labelstat(ls, str_checkname(ls), line); @@ -1907,6 +1941,17 @@ static void statement (LexState *ls) { gotostat(ls, line); break; } + case TK_NAME: { + /* compatibility code to parse global keyword when "global" + is not reserved */ + if (eqstr(ls->t.seminfo.ts, luaS_newliteral(ls->L, "global"))) { + int lk = luaX_lookahead(ls); + if (lk == TK_NAME) { /* 'global name'? */ + globalstat(ls); + break; + } + } /* else... */ + } /* FALLTHROUGH */ default: { /* stat -> func | assignment */ exprstat(ls); break; diff --git a/lparser.h b/lparser.h index a3063569c7..3cd0ba77a4 100644 --- a/lparser.h +++ b/lparser.h @@ -37,6 +37,9 @@ typedef enum { info = result register */ VLOCAL, /* local variable; var.ridx = register index; var.vidx = relative index in 'actvar.arr' */ + VGLOBAL, /* global variable; + info = relative index in 'actvar.arr' (or -1 for + implicit declaration) */ VUPVAL, /* upvalue variable; info = index of upvalue in 'upvalues' */ VCONST, /* compile-time variable; info = absolute index in 'actvar.arr' */ @@ -87,10 +90,16 @@ typedef struct expdesc { /* kinds of variables */ -#define VDKREG 0 /* regular */ -#define RDKCONST 1 /* constant */ +#define VDKREG 0 /* regular local */ +#define RDKCONST 1 /* local constant */ #define RDKTOCLOSE 2 /* to-be-closed */ -#define RDKCTC 3 /* compile-time constant */ +#define RDKCTC 3 /* local compile-time constant */ +#define GDKREG 4 /* regular global */ +#define GDKCONST 5 /* global constant */ + +/* variables that live in registers */ +#define varinreg(v) ((v)->vd.kind <= RDKTOCLOSE) + /* description of an active local variable */ typedef union Vardesc { diff --git a/ltests.h b/ltests.h index 7f0ce4041d..43f08162cd 100644 --- a/ltests.h +++ b/ltests.h @@ -14,6 +14,7 @@ /* test Lua with compatibility code */ #define LUA_COMPAT_MATHLIB #define LUA_COMPAT_LT_LE +#undef LUA_COMPAT_GLOBAL #define LUA_DEBUG diff --git a/luaconf.h b/luaconf.h index bd39465052..51e77547be 100644 --- a/luaconf.h +++ b/luaconf.h @@ -355,6 +355,12 @@ ** =================================================================== */ +/* +@@ LUA_COMPAT_GLOBAL avoids 'global' being a reserved word +*/ +#define LUA_COMPAT_GLOBAL + + /* @@ LUA_COMPAT_5_3 controls other macros for compatibility with Lua 5.3. ** You can define it to get all options, or change specific options diff --git a/manual/manual.of b/manual/manual.of index 7cd0d4dbde..ace5d37513 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -213,11 +213,88 @@ of a given value @seeF{type}. } -@sect2{globalenv| @title{Environments and the Global Environment} +@sect2{globalenv| @title{Scopes, Variables, and Environments} +@index{visibility} + +A variable name refers to a global or a local variable according +to the declaration that is in context at that point of the code. +(For the purposes of this discussion, +a function's formal parameter is equivalent to a local variable.) + +All chunks start with an implicit declaration @T{global *}, +which declares all free names as global variables; +this implicit declaration becomes void inside the scope of any other +@Rw{global} declaration, regardless of the names being declared. +@verbatim{ +X = 1 -- Ok, global by default +do + global Y -- voids implicit initial declaration + X = 1 -- ERROR, X not declared + Y = 1 -- Ok, Y declared as global +end +X = 2 -- Ok, global by default again +} +So, outside any global declaration, +Lua works as @x{global-by-default}. +Inside any global declaration, +Lua works without a default: +All variables must be declared. + +Lua is a lexically scoped language. +The scope of a variable declaration begins at the first statement after +the declaration and lasts until the last non-void statement +of the innermost block that includes the declaration. +(@emph{Void statements} are labels and empty statements.) + +A declaration shadows any declaration for the same name that +is in context at the point of the declaration. Inside this +shadow, any outer declaration for that name is void. +See the next example: +@verbatim{ +global print, x +x = 10 -- global variable +do -- new block + local x = x -- new 'x', with value 10 + print(x) --> 10 + x = x+1 + do -- another block + local x = x+1 -- another 'x' + print(x) --> 12 + end + print(x) --> 11 +end +print(x) --> 10 (the global one) +} + +Notice that, in a declaration like @T{local x = x}, +the new @id{x} being declared is not in scope yet, +and so the @id{x} in the left-hand side refers to the outside variable. + +Because of the @x{lexical scoping} rules, +local variables can be freely accessed by functions +defined inside their scope. +A local variable used by an inner function is called an @def{upvalue} +(or @emphx{external local variable}, or simply @emphx{external variable}) +inside the inner function. + +Notice that each execution of a @Rw{local} statement +defines new local variables. +Consider the following example: +@verbatim{ +a = {} +local x = 20 +for i = 1, 10 do + local y = 0 + a[i] = function () y = y + 1; return x + y end +end +} +The loop creates ten closures +(that is, ten instances of the anonymous function). +Each of these closures uses a different @id{y} variable, +while all of them share the same @id{x}. As we will discuss further in @refsec{variables} and @refsec{assignment}, -any reference to a free name -(that is, a name not bound to any declaration) @id{var} +any reference to a global variable @id{var} is syntactically translated to @T{_ENV.var}. Moreover, every chunk is compiled in the scope of an external local variable named @id{_ENV} @see{chunks}, @@ -225,12 +302,14 @@ so @id{_ENV} itself is never a free name in a chunk. Despite the existence of this external @id{_ENV} variable and the translation of free names, -@id{_ENV} is a completely regular name. +@id{_ENV} is a regular name. In particular, you can define new variables and parameters with that name. -Each reference to a free name uses the @id{_ENV} that is -visible at that point in the program, -following the usual visibility rules of Lua @see{visibility}. +(However, you should not define @id{_ENV} as a global variable, +otherwise @T{_ENV.var} would translate to +@T{_ENV._ENV.var} and so on, in an infinite loop.) +Each reference to a global variable name uses the @id{_ENV} that is +visible at that point in the program. Any table used as the value of @id{_ENV} is called an @def{environment}. @@ -244,8 +323,8 @@ When Lua loads a chunk, the default value for its @id{_ENV} variable is the global environment @seeF{load}. Therefore, by default, -free names in Lua code refer to entries in the global environment -and, therefore, they are also called @def{global variables}. +global variables in Lua code refer to entries in the global environment +and, therefore, they act as conventional global variables. Moreover, all standard libraries are loaded in the global environment and some functions there operate on that environment. You can use @Lid{load} (or @Lid{loadfile}) @@ -1198,17 +1277,15 @@ global variables, local variables, and table fields. A single name can denote a global variable or a local variable (or a function's formal parameter, -which is a particular kind of local variable): +which is a particular kind of local variable) @see{globalenv}: @Produc{ @producname{var}@producbody{@bnfNter{Name}} } @bnfNter{Name} denotes identifiers @see{lexical}. -Any variable name is assumed to be global unless explicitly declared -as a local @see{localvar}. -@x{Local variables} are @emph{lexically scoped}: +Because variables are @emph{lexically scoped}, local variables can be freely accessed by functions -defined inside their scope @see{visibility}. +defined inside their scope @see{globalenv}. Before the first assignment to a variable, its value is @nil. @@ -1227,8 +1304,6 @@ The syntax @id{var.Name} is just syntactic sugar for An access to a global variable @id{x} is equivalent to @id{_ENV.x}. -Due to the way that chunks are compiled, -the variable @id{_ENV} itself is never global @see{globalenv}. } @@ -1571,17 +1646,18 @@ Function calls are explained in @See{functioncall}. } -@sect3{localvar| @title{Local Declarations} -@x{Local variables} can be declared anywhere inside a block. -The declaration can include an initialization: +@sect3{localvar| @title{Variable Declarations} +Local and global variables can be declared anywhere inside a block. +The declaration for locals can include an initialization: @Produc{ @producname{stat}@producbody{@Rw{local} attnamelist @bnfopt{@bnfter{=} explist}} +@producname{stat}@producbody{@Rw{global} attnamelist} @producname{attnamelist}@producbody{ @bnfNter{Name} attrib @bnfrep{@bnfter{,} @bnfNter{Name} attrib}} } If present, an initial assignment has the same semantics of a multiple assignment @see{assignment}. -Otherwise, all variables are initialized with @nil. +Otherwise, all local variables are initialized with @nil. Each variable name may be postfixed by an attribute (a name between angle brackets): @@ -1595,11 +1671,22 @@ that is, a variable that cannot be assigned to after its initialization; and @id{close}, which declares a to-be-closed variable @see{to-be-closed}. A list of variables can contain at most one to-be-closed variable. +Only local variables can have the @id{close} attribute. + +Note that, for global variables, +the @emph{read-only} atribute is only a syntactical restriction: +@verbatim{ +global X +X = 1 -- ERROR +_ENV.X = 1 -- Ok +foo() -- 'foo' can freely change the global X +} A chunk is also a block @see{chunks}, -and so local variables can be declared in a chunk outside any explicit block. +and so variables can be declared in a chunk outside any explicit block. -The visibility rules for local variables are explained in @See{visibility}. +The visibility rules for variable declarations +are explained in @See{globalenv}. } @@ -2356,58 +2443,6 @@ return x,y,f() -- returns x, y, and all results from f(). } -@sect2{visibility| @title{Visibility Rules} - -@index{visibility} -Lua is a lexically scoped language. -The scope of a local variable begins at the first statement after -its declaration and lasts until the last non-void statement -of the innermost block that includes the declaration. -(@emph{Void statements} are labels and empty statements.) -Consider the following example: -@verbatim{ -x = 10 -- global variable -do -- new block - local x = x -- new 'x', with value 10 - print(x) --> 10 - x = x+1 - do -- another block - local x = x+1 -- another 'x' - print(x) --> 12 - end - print(x) --> 11 -end -print(x) --> 10 (the global one) -} - -Notice that, in a declaration like @T{local x = x}, -the new @id{x} being declared is not in scope yet, -and so the second @id{x} refers to the outside variable. - -Because of the @x{lexical scoping} rules, -local variables can be freely accessed by functions -defined inside their scope. -A local variable used by an inner function is called an @def{upvalue} -(or @emphx{external local variable}, or simply @emphx{external variable}) -inside the inner function. - -Notice that each execution of a @Rw{local} statement -defines new local variables. -Consider the following example: -@verbatim{ -a = {} -local x = 20 -for i = 1, 10 do - local y = 0 - a[i] = function () y = y + 1; return x + y end -end -} -The loop creates ten closures -(that is, ten instances of the anonymous function). -Each of these closures uses a different @id{y} variable, -while all of them share the same @id{x}. - -} } @@ -9535,6 +9570,7 @@ and @bnfNter{LiteralString}, see @See{lexical}.) @OrNL @Rw{function} funcname funcbody @OrNL @Rw{local} @Rw{function} @bnfNter{Name} funcbody @OrNL @Rw{local} attnamelist @bnfopt{@bnfter{=} explist} +@OrNL @Rw{global} attnamelist } @producname{attnamelist}@producbody{ diff --git a/testes/db.lua b/testes/db.lua index e4982c207a..ae204c4176 100644 --- a/testes/db.lua +++ b/testes/db.lua @@ -349,6 +349,7 @@ end, "crl") function f(a,b) + global collectgarbage, assert, g, string collectgarbage() local _, x = debug.getlocal(1, 1) local _, y = debug.getlocal(1, 2) diff --git a/testes/goto.lua b/testes/goto.lua index eca6851689..fdfddb8570 100644 --- a/testes/goto.lua +++ b/testes/goto.lua @@ -1,6 +1,8 @@ -- $Id: testes/goto.lua $ -- See Copyright Notice in file lua.h +print("testing goto and global declarations") + collectgarbage() local function errmsg (code, m) @@ -280,7 +282,47 @@ end foo() --------------------------------------------------------------------------------- +-------------------------------------------------------------------------- +do + global print, load, T; global assert + global string + + local function checkerr (code, err) + local st, msg = load(code) + assert(not st and string.find(msg, err)) + end + + -- globals must be declared after a global declaration + checkerr("global none; X = 1", "variable 'X'") + + -- global variables cannot be to-be-closed + checkerr("global X", "cannot be") + + do + local X = 10 + do global X; X = 20 end + assert(X == 10) -- local X + end + assert(_ENV.X == 20) -- global X + + -- '_ENV' cannot be global + checkerr("global _ENV, a; a = 10", "variable 'a'") + + -- global declarations inside functions + checkerr([[ + global none + local function foo () XXX = 1 end --< ERROR]], "variable 'XXX'") + + if not T then -- when not in "test mode", "global" isn't reserved + assert(load("global = 1; return global")() == 1) + print " ('global' is not a reserved word)" + else + -- "global" reserved, cannot be used as a variable + assert(not load("global = 1; return global")) + end + +end print'OK' + diff --git a/testes/math.lua b/testes/math.lua index 88a57ce75a..242579b177 100644 --- a/testes/math.lua +++ b/testes/math.lua @@ -3,6 +3,14 @@ print("testing numbers and math lib") +local math = require "math" +local string = require "string" + +global none + +global print, assert, pcall, type, pairs, load +global tonumber, tostring, select + local minint = math.mininteger local maxint = math.maxinteger @@ -184,7 +192,7 @@ do for i = -3, 3 do -- variables avoid constant folding for j = -3, 3 do -- domain errors (0^(-n)) are not portable - if not _port or i ~= 0 or j > 0 then + if not _ENV._port or i ~= 0 or j > 0 then assert(eq(i^j, 1 / i^(-j))) end end @@ -430,7 +438,7 @@ for i = 2,36 do assert(tonumber('\t10000000000\t', i) == i10) end -if not _soft then +if not _ENV._soft then -- tests with very long numerals assert(tonumber("0x"..string.rep("f", 13)..".0") == 2.0^(4*13) - 1) assert(tonumber("0x"..string.rep("f", 150)..".0") == 2.0^(4*150) - 1) @@ -632,7 +640,7 @@ assert(maxint % -2 == -1) -- non-portable tests because Windows C library cannot compute -- fmod(1, huge) correctly -if not _port then +if not _ENV._port then local function anan (x) assert(isNaN(x)) end -- assert Not a Number anan(0.0 % 0) anan(1.3 % 0) @@ -779,6 +787,7 @@ assert(a == '10' and b == '20') do print("testing -0 and NaN") + global rawset, undef local mz = -0.0 local z = 0.0 assert(mz == z) @@ -1074,6 +1083,7 @@ do -- different numbers should print differently. -- check pairs of floats with minimum detectable difference local p = floatbits - 1 + global ipairs for i = 1, maxexp - 1 do for _, i in ipairs{-i, i} do local x = 2^i From 4365a45d681b4e71e3c39148489bb8eae538ccf7 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 6 May 2025 15:54:05 -0300 Subject: [PATCH 1039/1145] Checks for read-only globals --- lcode.c | 3 ++- lparser.c | 24 ++++++++++++++++++------ lparser.h | 5 +++-- testes/locals.lua | 10 ++++++++++ 4 files changed, 33 insertions(+), 9 deletions(-) diff --git a/lcode.c b/lcode.c index e8b9bb5dbd..8f658500dc 100644 --- a/lcode.c +++ b/lcode.c @@ -1315,8 +1315,8 @@ void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) { luaK_exp2anyreg(fs, t); /* put it in a register */ if (t->k == VUPVAL) { lu_byte temp = cast_byte(t->u.info); /* upvalue index */ - lua_assert(isKstr(fs, k)); t->u.ind.t = temp; /* (can't do a direct assignment; values overlap) */ + lua_assert(isKstr(fs, k)); t->u.ind.idx = cast(short, k->u.info); /* literal short string */ t->k = VINDEXUP; } @@ -1336,6 +1336,7 @@ void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) { t->k = VINDEXED; } } + t->u.ind.vidx = -1; /* by default, not a declared global */ } diff --git a/lparser.c b/lparser.c index 1c5fdca6f3..61ce090840 100644 --- a/lparser.c +++ b/lparser.c @@ -200,7 +200,7 @@ static int new_varkind (LexState *ls, TString *name, lu_byte kind) { luaY_checklimit(fs, dyd->actvar.n + 1 - fs->firstlocal, MAXVARS, "local variables"); luaM_growvector(L, dyd->actvar.arr, dyd->actvar.n + 1, - dyd->actvar.size, Vardesc, SHRT_MAX, "local variables"); + dyd->actvar.size, Vardesc, SHRT_MAX, "variable declarationss"); var = &dyd->actvar.arr[dyd->actvar.n++]; var->vd.kind = kind; /* default */ var->vd.name = name; @@ -276,7 +276,7 @@ static LocVar *localdebuginfo (FuncState *fs, int vidx) { static void init_var (FuncState *fs, expdesc *e, int vidx) { e->f = e->t = NO_JUMP; e->k = VLOCAL; - e->u.var.vidx = cast(unsigned short, vidx); + e->u.var.vidx = cast(short, vidx); e->u.var.ridx = getlocalvardesc(fs, vidx)->vd.ridx; } @@ -304,8 +304,16 @@ static void check_readonly (LexState *ls, expdesc *e) { varname = up->name; break; } + case VINDEXUP: case VINDEXSTR: case VINDEXED: { + int vidx = e->u.ind.vidx; + /* is it a read-only declared global? */ + if (vidx != -1 && ls->dyd->actvar.arr[vidx].vd.kind == GDKCONST) + varname = ls->dyd->actvar.arr[vidx].vd.name; + break; + } default: - return; /* other cases cannot be read-only */ + lua_assert(e->k == VINDEXI); /* this one doesn't need any check */ + return; /* integer index cannot be read-only */ } if (varname) luaK_semerror(ls, "attempt to assign to const variable '%s'", @@ -391,7 +399,7 @@ static int newupvalue (FuncState *fs, TString *name, expdesc *v) { /* -** Look for an active local variable with the name 'n' in the +** Look for an active variable with the name 'n' in the ** function 'fs'. If found, initialize 'var' with it and return ** its expression kind; otherwise return -1. */ @@ -403,7 +411,7 @@ static int searchvar (FuncState *fs, TString *n, expdesc *var) { if (vd->vd.kind == RDKCTC) /* compile-time constant? */ init_exp(var, VCONST, fs->firstlocal + i); else if (vd->vd.kind == GDKREG || vd->vd.kind == GDKCONST) - init_exp(var, VGLOBAL, i); + init_exp(var, VGLOBAL, fs->firstlocal + i); else /* local variable */ init_var(fs, var, i); return cast_int(var->k); @@ -475,8 +483,11 @@ static void singlevar (LexState *ls, expdesc *var) { singlevaraux(fs, varname, var, 1); if (var->k == VGLOBAL) { /* global name? */ expdesc key; + int info = var->u.info; + lua_assert(info == -1 || + eqstr(ls->dyd->actvar.arr[info].vd.name, varname)); /* global by default in the scope of a global declaration? */ - if (var->u.info == -1 && fs->bl->globdec) + if (info == -1 && fs->bl->globdec) luaK_semerror(ls, "variable '%s' not declared", getstr(varname)); singlevaraux(fs, ls->envn, var, 1); /* get environment variable */ if (var->k == VGLOBAL) @@ -485,6 +496,7 @@ static void singlevar (LexState *ls, expdesc *var) { luaK_exp2anyregup(fs, var); /* but could be a constant */ codestring(&key, varname); /* key is variable name */ luaK_indexed(fs, var, &key); /* env[varname] */ + var->u.ind.vidx = cast(short, info); /* mark it as a declared global */ } } diff --git a/lparser.h b/lparser.h index 3cd0ba77a4..274fb1c469 100644 --- a/lparser.h +++ b/lparser.h @@ -77,11 +77,12 @@ typedef struct expdesc { int info; /* for generic use */ struct { /* for indexed variables */ short idx; /* index (R or "long" K) */ + short vidx; /* index in 'actvar.arr' or -1 if not a declared global */ lu_byte t; /* table (register or upvalue) */ } ind; struct { /* for local variables */ lu_byte ridx; /* register holding the variable */ - unsigned short vidx; /* compiler index (in 'actvar.arr') */ + short vidx; /* index in 'actvar.arr' */ } var; } u; int t; /* patch list of 'exit when true' */ @@ -101,7 +102,7 @@ typedef struct expdesc { #define varinreg(v) ((v)->vd.kind <= RDKTOCLOSE) -/* description of an active local variable */ +/* description of an active variable */ typedef union Vardesc { struct { TValuefields; /* constant value (if it is a compile-time constant) */ diff --git a/testes/locals.lua b/testes/locals.lua index eeeb4338fe..421595bb9b 100644 --- a/testes/locals.lua +++ b/testes/locals.lua @@ -178,6 +178,8 @@ A = nil do -- constants + global assert, load, string, X + X = 1 -- not a constant local a, b, c = 10, 20, 30 b = a + c + b -- 'b' is not constant assert(a == 10 and b == 60 and c == 30) @@ -191,6 +193,9 @@ do -- constants checkro("z", "local x , y, z = 10, 20, 30; y = 10; z = 11") checkro("foo", "local foo = 10; function foo() end") checkro("foo", "local foo = {}; function foo() end") + checkro("foo", "global foo ; function foo() end") + checkro("XX", "global XX ; XX = 10") + checkro("XX", "local _ENV; global XX ; XX = 10") checkro("z", [[ local a, z , b = 10; @@ -201,6 +206,11 @@ do -- constants local a, var1 = 10; function foo() a = 20; z = function () var1 = 12; end end ]]) + + checkro("var1", [[ + global a, var1 , z; + local function foo() a = 20; z = function () var1 = 12; end end + ]]) end From 3f0ea90aa8b8493485637f6e8d2a070a1ac0d5cb Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 8 May 2025 11:08:03 -0300 Subject: [PATCH 1040/1145] New syntax 'global function' --- lparser.c | 49 +++++++++++++++++++++++++++++++++++++---------- manual/manual.of | 15 +++++++++++++-- testes/errors.lua | 8 ++++++++ testes/goto.lua | 23 +++++++++++++++++++++- 4 files changed, 82 insertions(+), 13 deletions(-) diff --git a/lparser.c b/lparser.c index 61ce090840..a11f1dd34d 100644 --- a/lparser.c +++ b/lparser.c @@ -477,8 +477,7 @@ static void singlevaraux (FuncState *fs, TString *n, expdesc *var, int base) { ** Find a variable with the given name 'n', handling global variables ** too. */ -static void singlevar (LexState *ls, expdesc *var) { - TString *varname = str_checkname(ls); +static void buildvar (LexState *ls, TString *varname, expdesc *var) { FuncState *fs = ls->fs; singlevaraux(fs, varname, var, 1); if (var->k == VGLOBAL) { /* global name? */ @@ -501,6 +500,11 @@ static void singlevar (LexState *ls, expdesc *var) { } +static void singlevar (LexState *ls, expdesc *var) { + buildvar(ls, str_checkname(ls), var); +} + + /* ** Adjust the number of results from an expression list 'e' with 'nexps' ** expressions to 'nvars' values. @@ -1727,7 +1731,7 @@ static void localfunc (LexState *ls) { static lu_byte getvarattribute (LexState *ls) { - /* ATTRIB -> ['<' Name '>'] */ + /* attrib -> ['<' NAME '>'] */ if (testnext(ls, '<')) { TString *ts = str_checkname(ls); const char *attr = getstr(ts); @@ -1752,7 +1756,7 @@ static void checktoclose (FuncState *fs, int level) { static void localstat (LexState *ls) { - /* stat -> LOCAL NAME ATTRIB { ',' NAME ATTRIB } ['=' explist] */ + /* stat -> LOCAL NAME attrib { ',' NAME attrib } ['=' explist] */ FuncState *fs = ls->fs; int toclose = -1; /* index of to-be-closed variable (if any) */ Vardesc *var; /* last variable */ @@ -1794,8 +1798,8 @@ static void localstat (LexState *ls) { static void globalstat (LexState *ls) { + /* globalstat -> (GLOBAL) NAME attrib {',' NAME attrib} */ FuncState *fs = ls->fs; - luaX_next(ls); /* skip 'global' */ do { TString *vname = str_checkname(ls); lu_byte kind = getvarattribute(ls); @@ -1807,7 +1811,31 @@ static void globalstat (LexState *ls) { new_varkind(ls, vname, kind); fs->nactvar++; /* activate declaration */ } while (testnext(ls, ',')); - fs->bl->globdec = 1; /* code is in the scope of a global declaration */ +} + + +static void globalfunc (LexState *ls, int line) { + /* globalfunc -> (GLOBAL FUNCTION) NAME body */ + expdesc var, b; + FuncState *fs = ls->fs; + TString *fname = str_checkname(ls); + new_varkind(ls, fname, GDKREG); /* declare global variable */ + fs->nactvar++; /* enter its scope */ + buildvar(ls, fname, &var); + body(ls, &b, 0, ls->linenumber); /* compile and return closure in 'b' */ + luaK_storevar(fs, &var, &b); + luaK_fixline(fs, line); /* definition "happens" in the first line */ +} + + +static void globalstatfunc (LexState *ls, int line) { + /* stat -> GLOBAL globalfunc | GLOBAL globalstat */ + luaX_next(ls); /* skip 'global' */ + ls->fs->bl->globdec = 1; /* in the scope of a global declaration */ + if (testnext(ls, TK_FUNCTION)) + globalfunc(ls, line); + else + globalstat(ls); } @@ -1930,8 +1958,8 @@ static void statement (LexState *ls) { localstat(ls); break; } - case TK_GLOBAL: { /* stat -> globalstat */ - globalstat(ls); + case TK_GLOBAL: { /* stat -> globalstatfunc */ + globalstatfunc(ls, line); break; } case TK_DBCOLON: { /* stat -> label */ @@ -1958,8 +1986,9 @@ static void statement (LexState *ls) { is not reserved */ if (eqstr(ls->t.seminfo.ts, luaS_newliteral(ls->L, "global"))) { int lk = luaX_lookahead(ls); - if (lk == TK_NAME) { /* 'global name'? */ - globalstat(ls); + if (lk == TK_NAME || lk == TK_FUNCTION) { + /* 'global ' or 'global function' */ + globalstatfunc(ls, line); break; } } /* else... */ diff --git a/manual/manual.of b/manual/manual.of index ace5d37513..cc71aaada7 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -2229,6 +2229,7 @@ The following syntactic sugar simplifies function definitions: @Produc{ @producname{stat}@producbody{@Rw{function} funcname funcbody} @producname{stat}@producbody{@Rw{local} @Rw{function} @bnfNter{Name} funcbody} +@producname{stat}@producbody{@Rw{global} @Rw{function} @bnfNter{Name} funcbody} @producname{funcname}@producbody{@bnfNter{Name} @bnfrep{@bnfter{.} @bnfNter{Name}} @bnfopt{@bnfter{:} @bnfNter{Name}}} } The statement @@ -2247,6 +2248,7 @@ translates to @verbatim{ t.a.b.c.f = function () @rep{body} end } + The statement @verbatim{ local function f () @rep{body} end @@ -2260,7 +2262,15 @@ not to local f = function () @rep{body} end } (This only makes a difference when the body of the function -contains references to @id{f}.) +contains recursive references to @id{f}.) +Similarly, the statement +@verbatim{ +global function f () @rep{body} end +} +translates to +@verbatim{ +global f; f = function () @rep{body} end +} A function definition is an executable expression, whose value has type @emph{function}. @@ -2323,7 +2333,7 @@ then the function returns with no results. @index{multiple return} There is a system-dependent limit on the number of values that a function may return. -This limit is guaranteed to be greater than 1000. +This limit is guaranteed to be at least 1000. The @emphx{colon} syntax is used to emulate @def{methods}, @@ -9569,6 +9579,7 @@ and @bnfNter{LiteralString}, see @See{lexical}.) @OrNL @Rw{for} namelist @Rw{in} explist @Rw{do} block @Rw{end} @OrNL @Rw{function} funcname funcbody @OrNL @Rw{local} @Rw{function} @bnfNter{Name} funcbody +@OrNL @Rw{global} @Rw{function} @bnfNter{Name} funcbody @OrNL @Rw{local} attnamelist @bnfopt{@bnfter{=} explist} @OrNL @Rw{global} attnamelist } diff --git a/testes/errors.lua b/testes/errors.lua index c80051fc7a..6c76a99a16 100644 --- a/testes/errors.lua +++ b/testes/errors.lua @@ -489,6 +489,14 @@ if not b then end end]], 5) +lineerror([[ +_ENV = 1 +global function foo () + local a = 10 + return a +end +]], 2) + -- bug in 5.4.0 lineerror([[ diff --git a/testes/goto.lua b/testes/goto.lua index fdfddb8570..b41399ffce 100644 --- a/testes/goto.lua +++ b/testes/goto.lua @@ -293,8 +293,9 @@ do assert(not st and string.find(msg, err)) end - -- globals must be declared after a global declaration + -- globals must be declared, after a global declaration checkerr("global none; X = 1", "variable 'X'") + checkerr("global none; function XX() end", "variable 'XX'") -- global variables cannot be to-be-closed checkerr("global X", "cannot be") @@ -321,6 +322,26 @@ do -- "global" reserved, cannot be used as a variable assert(not load("global = 1; return global")) end + + local foo = 20 + do + global function foo (x) + if x == 0 then return 1 else return 2 * foo(x - 1) end + end + assert(foo == _ENV.foo and foo(4) == 16) + end + assert(_ENV.foo(4) == 16) + assert(foo == 20) -- local one is in context here + + do + global foo; + function foo (x) return end -- Ok after declaration + end + + checkerr([[ + global foo ; + function foo (x) return end -- ERROR: foo is read-only + ]], "assign to const variable 'foo'") end From d827e96f33056bcc0daca0c04b3273604f9d5986 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 8 May 2025 12:49:39 -0300 Subject: [PATCH 1041/1145] Using 'l_uint32' for unicode codepoints in scanner 'l_uint32' is enough for unicode codepoints (versus unsigned long), and the utf-8 library already uses that type. --- llex.c | 6 +++--- llimits.h | 1 - lobject.c | 5 +++-- lobject.h | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/llex.c b/llex.c index 9d93224f28..edeb48feee 100644 --- a/llex.c +++ b/llex.c @@ -359,12 +359,12 @@ static int readhexaesc (LexState *ls) { ** for error reporting in case of errors; 'i' counts the number of ** saved characters, so that they can be removed if case of success. */ -static unsigned long readutf8esc (LexState *ls) { - unsigned long r; +static l_uint32 readutf8esc (LexState *ls) { + l_uint32 r; int i = 4; /* number of chars to be removed: start with #"\u{X" */ save_and_next(ls); /* skip 'u' */ esccheck(ls, ls->current == '{', "missing '{'"); - r = cast_ulong(gethexa(ls)); /* must have at least one digit */ + r = cast_uint(gethexa(ls)); /* must have at least one digit */ while (cast_void(save_and_next(ls)), lisxdigit(ls->current)) { i++; esccheck(ls, r <= (0x7FFFFFFFu >> 4), "UTF-8 value too large"); diff --git a/llimits.h b/llimits.h index 710dc1b440..b1fc384bfa 100644 --- a/llimits.h +++ b/llimits.h @@ -138,7 +138,6 @@ typedef LUAI_UACINT l_uacInt; #define cast_num(i) cast(lua_Number, (i)) #define cast_int(i) cast(int, (i)) #define cast_uint(i) cast(unsigned int, (i)) -#define cast_ulong(i) cast(unsigned long, (i)) #define cast_byte(i) cast(lu_byte, (i)) #define cast_uchar(i) cast(unsigned char, (i)) #define cast_char(i) cast(char, (i)) diff --git a/lobject.c b/lobject.c index 68566a2bad..57fc6a91a4 100644 --- a/lobject.c +++ b/lobject.c @@ -382,7 +382,7 @@ size_t luaO_str2num (const char *s, TValue *o) { } -int luaO_utf8esc (char *buff, unsigned long x) { +int luaO_utf8esc (char *buff, l_uint32 x) { int n = 1; /* number of bytes put in buffer (backwards) */ lua_assert(x <= 0x7FFFFFFFu); if (x < 0x80) /* ascii? */ @@ -637,7 +637,8 @@ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) { } case 'U': { /* an 'unsigned long' as a UTF-8 sequence */ char bf[UTF8BUFFSZ]; - int len = luaO_utf8esc(bf, va_arg(argp, unsigned long)); + unsigned long arg = va_arg(argp, unsigned long); + int len = luaO_utf8esc(bf, cast(l_uint32, arg)); addstr2buff(&buff, bf + UTF8BUFFSZ - len, cast_uint(len)); break; } diff --git a/lobject.h b/lobject.h index b5ca36680d..bc2f69ab4a 100644 --- a/lobject.h +++ b/lobject.h @@ -831,7 +831,7 @@ typedef struct Table { if (msg == NULL) luaD_throw(L, LUA_ERRMEM); /* only after 'va_end' */ } -LUAI_FUNC int luaO_utf8esc (char *buff, unsigned long x); +LUAI_FUNC int luaO_utf8esc (char *buff, l_uint32 x); LUAI_FUNC lu_byte luaO_ceillog2 (unsigned int x); LUAI_FUNC lu_byte luaO_codeparam (unsigned int p); LUAI_FUNC l_mem luaO_applyparam (lu_byte p, l_mem x); From 7ade1557627cf3f09c23c892ee227b7386f28414 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 8 May 2025 15:18:57 -0300 Subject: [PATCH 1042/1145] Janitorial work on casts --- lcode.c | 8 ++++---- ldump.c | 2 +- llimits.h | 3 +++ lobject.c | 2 +- lopcodes.h | 40 ++++++++++++++++++++-------------------- lparser.c | 4 ++-- ltests.c | 30 +++++++++++++++--------------- lundump.c | 2 +- 8 files changed, 47 insertions(+), 44 deletions(-) diff --git a/lcode.c b/lcode.c index 8f658500dc..119d91ab6e 100644 --- a/lcode.c +++ b/lcode.c @@ -1317,22 +1317,22 @@ void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) { lu_byte temp = cast_byte(t->u.info); /* upvalue index */ t->u.ind.t = temp; /* (can't do a direct assignment; values overlap) */ lua_assert(isKstr(fs, k)); - t->u.ind.idx = cast(short, k->u.info); /* literal short string */ + t->u.ind.idx = cast_short(k->u.info); /* literal short string */ t->k = VINDEXUP; } else { /* register index of the table */ t->u.ind.t = cast_byte((t->k == VLOCAL) ? t->u.var.ridx: t->u.info); if (isKstr(fs, k)) { - t->u.ind.idx = cast(short, k->u.info); /* literal short string */ + t->u.ind.idx = cast_short(k->u.info); /* literal short string */ t->k = VINDEXSTR; } else if (isCint(k)) { /* int. constant in proper range? */ - t->u.ind.idx = cast(short, k->u.ival); + t->u.ind.idx = cast_short(k->u.ival); t->k = VINDEXI; } else { - t->u.ind.idx = cast(short, luaK_exp2anyreg(fs, k)); /* register */ + t->u.ind.idx = cast_short(luaK_exp2anyreg(fs, k)); /* register */ t->k = VINDEXED; } } diff --git a/ldump.c b/ldump.c index d8fca317f0..79bb1dc9a7 100644 --- a/ldump.c +++ b/ldump.c @@ -108,7 +108,7 @@ static void dumpSize (DumpState *D, size_t sz) { static void dumpInt (DumpState *D, int x) { lua_assert(x >= 0); - dumpVarint(D, cast(size_t, x)); + dumpVarint(D, cast_sizet(x)); } diff --git a/llimits.h b/llimits.h index b1fc384bfa..223b5e6c34 100644 --- a/llimits.h +++ b/llimits.h @@ -137,12 +137,15 @@ typedef LUAI_UACINT l_uacInt; #define cast_voidp(i) cast(void *, (i)) #define cast_num(i) cast(lua_Number, (i)) #define cast_int(i) cast(int, (i)) +#define cast_short(i) cast(short, (i)) #define cast_uint(i) cast(unsigned int, (i)) #define cast_byte(i) cast(lu_byte, (i)) #define cast_uchar(i) cast(unsigned char, (i)) #define cast_char(i) cast(char, (i)) #define cast_charp(i) cast(char *, (i)) #define cast_sizet(i) cast(size_t, (i)) +#define cast_Integer(i) cast(lua_Integer, (i)) +#define cast_Inst(i) cast(Instruction, (i)) /* cast a signed lua_Integer to lua_Unsigned */ diff --git a/lobject.c b/lobject.c index 57fc6a91a4..1c32ecf7a9 100644 --- a/lobject.c +++ b/lobject.c @@ -618,7 +618,7 @@ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) { } case 'I': { /* a 'lua_Integer' */ TValue num; - setivalue(&num, cast(lua_Integer, va_arg(argp, l_uacInt))); + setivalue(&num, cast_Integer(va_arg(argp, l_uacInt))); addnum2buff(&buff, &num); break; } diff --git a/lopcodes.h b/lopcodes.h index 7511eb2237..e3ac9d0969 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -126,14 +126,14 @@ enum OpMode {iABC, ivABC, iABx, iAsBx, iAx, isJ}; #define GET_OPCODE(i) (cast(OpCode, ((i)>>POS_OP) & MASK1(SIZE_OP,0))) #define SET_OPCODE(i,o) ((i) = (((i)&MASK0(SIZE_OP,POS_OP)) | \ - ((cast(Instruction, o)<>(pos)) & MASK1(size,0))) #define setarg(i,v,pos,size) ((i) = (((i)&MASK0(size,pos)) | \ - ((cast(Instruction, v)<f = e->t = NO_JUMP; e->k = VLOCAL; - e->u.var.vidx = cast(short, vidx); + e->u.var.vidx = cast_short(vidx); e->u.var.ridx = getlocalvardesc(fs, vidx)->vd.ridx; } @@ -495,7 +495,7 @@ static void buildvar (LexState *ls, TString *varname, expdesc *var) { luaK_exp2anyregup(fs, var); /* but could be a constant */ codestring(&key, varname); /* key is variable name */ luaK_indexed(fs, var, &key); /* env[varname] */ - var->u.ind.vidx = cast(short, info); /* mark it as a declared global */ + var->u.ind.vidx = cast_short(info); /* mark it as a declared global */ } } diff --git a/ltests.c b/ltests.c index 1517aa88f6..e7bc66dd28 100644 --- a/ltests.c +++ b/ltests.c @@ -910,9 +910,9 @@ static int get_limits (lua_State *L) { static int mem_query (lua_State *L) { if (lua_isnone(L, 1)) { - lua_pushinteger(L, cast(lua_Integer, l_memcontrol.total)); - lua_pushinteger(L, cast(lua_Integer, l_memcontrol.numblocks)); - lua_pushinteger(L, cast(lua_Integer, l_memcontrol.maxmem)); + lua_pushinteger(L, cast_Integer(l_memcontrol.total)); + lua_pushinteger(L, cast_Integer(l_memcontrol.numblocks)); + lua_pushinteger(L, cast_Integer(l_memcontrol.maxmem)); return 3; } else if (lua_isnumber(L, 1)) { @@ -926,7 +926,7 @@ static int mem_query (lua_State *L) { int i; for (i = LUA_NUMTYPES - 1; i >= 0; i--) { if (strcmp(t, ttypename(i)) == 0) { - lua_pushinteger(L, cast(lua_Integer, l_memcontrol.objcount[i])); + lua_pushinteger(L, cast_Integer(l_memcontrol.objcount[i])); return 1; } } @@ -1074,7 +1074,7 @@ static int hash_query (lua_State *L) { Table *t; luaL_checktype(L, 2, LUA_TTABLE); t = hvalue(obj_at(L, 2)); - lua_pushinteger(L, cast(lua_Integer, luaH_mainposition(t, o) - t->node)); + lua_pushinteger(L, cast_Integer(luaH_mainposition(t, o) - t->node)); } return 1; } @@ -1082,9 +1082,9 @@ static int hash_query (lua_State *L) { static int stacklevel (lua_State *L) { int a = 0; - lua_pushinteger(L, cast(lua_Integer, L->top.p - L->stack.p)); + lua_pushinteger(L, cast_Integer(L->top.p - L->stack.p)); lua_pushinteger(L, stacksize(L)); - lua_pushinteger(L, cast(lua_Integer, L->nCcalls)); + lua_pushinteger(L, cast_Integer(L->nCcalls)); lua_pushinteger(L, L->nci); lua_pushinteger(L, (lua_Integer)(size_t)&a); return 5; @@ -1099,9 +1099,9 @@ static int table_query (lua_State *L) { t = hvalue(obj_at(L, 1)); asize = t->asize; if (i == -1) { - lua_pushinteger(L, cast(lua_Integer, asize)); - lua_pushinteger(L, cast(lua_Integer, allocsizenode(t))); - lua_pushinteger(L, cast(lua_Integer, asize > 0 ? *lenhint(t) : 0)); + lua_pushinteger(L, cast_Integer(asize)); + lua_pushinteger(L, cast_Integer(allocsizenode(t))); + lua_pushinteger(L, cast_Integer(asize > 0 ? *lenhint(t) : 0)); return 3; } else if (cast_uint(i) < asize) { @@ -1157,7 +1157,7 @@ static int test_codeparam (lua_State *L) { static int test_applyparam (lua_State *L) { lua_Integer p = luaL_checkinteger(L, 1); lua_Integer x = luaL_checkinteger(L, 2); - lua_pushinteger(L, cast(lua_Integer, luaO_applyparam(cast_byte(p), x))); + lua_pushinteger(L, cast_Integer(luaO_applyparam(cast_byte(p), x))); return 1; } @@ -1257,7 +1257,7 @@ static int pushuserdata (lua_State *L) { static int udataval (lua_State *L) { - lua_pushinteger(L, cast(lua_Integer, cast(size_t, lua_touserdata(L, 1)))); + lua_pushinteger(L, cast_st2S(cast_sizet(lua_touserdata(L, 1)))); return 1; } @@ -1294,7 +1294,7 @@ static int num2int (lua_State *L) { static int makeseed (lua_State *L) { - lua_pushinteger(L, cast(lua_Integer, luaL_makeseed(L))); + lua_pushinteger(L, cast_Integer(luaL_makeseed(L))); return 1; } @@ -1638,7 +1638,7 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) { } else if EQ("func2num") { lua_CFunction func = lua_tocfunction(L1, getindex); - lua_pushinteger(L1, cast(lua_Integer, cast(size_t, func))); + lua_pushinteger(L1, cast_st2S(cast_sizet(func))); } else if EQ("getfield") { int t = getindex; @@ -2011,7 +2011,7 @@ static int Cfunc (lua_State *L) { static int Cfunck (lua_State *L, int status, lua_KContext ctx) { lua_pushstring(L, statcodes[status]); lua_setglobal(L, "status"); - lua_pushinteger(L, cast(lua_Integer, ctx)); + lua_pushinteger(L, cast_Integer(ctx)); lua_setglobal(L, "ctx"); return runC(L, L, lua_tostring(L, cast_int(ctx))); } diff --git a/lundump.c b/lundump.c index d53bfc9a99..fccded7d79 100644 --- a/lundump.c +++ b/lundump.c @@ -149,7 +149,7 @@ static void loadString (LoadState *S, Proto *p, TString **sl) { return; } else if (size == 1) { /* previously saved string? */ - lua_Integer idx = cast(lua_Integer, loadSize(S)); /* get its index */ + lua_Integer idx = cast_st2S(loadSize(S)); /* get its index */ TValue stv; luaH_getint(S->h, idx, &stv); /* get its value */ *sl = ts = tsvalue(&stv); From 5b1ab8efdcb7b48cab8148a407266c467d57114c Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Sun, 11 May 2025 11:51:58 -0300 Subject: [PATCH 1043/1145] 'expdesc' doesn't depend on 'actvar' for var. info. In preparation for 'global *', the structure 'expdesc' does not point to 'actvar.arr' for information about global variables. --- lcode.c | 9 ++++++--- lparser.c | 15 +++++++-------- lparser.h | 16 ++++++++++------ 3 files changed, 23 insertions(+), 17 deletions(-) diff --git a/lcode.c b/lcode.c index 119d91ab6e..7ca895f147 100644 --- a/lcode.c +++ b/lcode.c @@ -753,10 +753,11 @@ void luaK_setreturns (FuncState *fs, expdesc *e, int nresults) { /* ** Convert a VKSTR to a VK */ -static void str2K (FuncState *fs, expdesc *e) { +static int str2K (FuncState *fs, expdesc *e) { lua_assert(e->k == VKSTR); e->u.info = stringK(fs, e->u.strval); e->k = VK; + return e->u.info; } @@ -1307,8 +1308,9 @@ void luaK_self (FuncState *fs, expdesc *e, expdesc *key) { ** values in registers. */ void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) { + int keystr = -1; if (k->k == VKSTR) - str2K(fs, k); + keystr = str2K(fs, k); lua_assert(!hasjumps(t) && (t->k == VLOCAL || t->k == VNONRELOC || t->k == VUPVAL)); if (t->k == VUPVAL && !isKstr(fs, k)) /* upvalue indexed by non 'Kstr'? */ @@ -1336,7 +1338,8 @@ void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) { t->k = VINDEXED; } } - t->u.ind.vidx = -1; /* by default, not a declared global */ + t->u.ind.keystr = keystr; /* string index in 'k' */ + t->u.ind.ro = 0; /* by default, not read-only */ } diff --git a/lparser.c b/lparser.c index 6658bb2061..3c2f57ab5e 100644 --- a/lparser.c +++ b/lparser.c @@ -304,11 +304,9 @@ static void check_readonly (LexState *ls, expdesc *e) { varname = up->name; break; } - case VINDEXUP: case VINDEXSTR: case VINDEXED: { - int vidx = e->u.ind.vidx; - /* is it a read-only declared global? */ - if (vidx != -1 && ls->dyd->actvar.arr[vidx].vd.kind == GDKCONST) - varname = ls->dyd->actvar.arr[vidx].vd.name; + case VINDEXUP: case VINDEXSTR: case VINDEXED: { /* global variable */ + if (e->u.ind.ro) /* read-only? */ + varname = tsvalue(&fs->f->k[e->u.ind.keystr]); break; } default: @@ -483,8 +481,6 @@ static void buildvar (LexState *ls, TString *varname, expdesc *var) { if (var->k == VGLOBAL) { /* global name? */ expdesc key; int info = var->u.info; - lua_assert(info == -1 || - eqstr(ls->dyd->actvar.arr[info].vd.name, varname)); /* global by default in the scope of a global declaration? */ if (info == -1 && fs->bl->globdec) luaK_semerror(ls, "variable '%s' not declared", getstr(varname)); @@ -495,7 +491,10 @@ static void buildvar (LexState *ls, TString *varname, expdesc *var) { luaK_exp2anyregup(fs, var); /* but could be a constant */ codestring(&key, varname); /* key is variable name */ luaK_indexed(fs, var, &key); /* env[varname] */ - var->u.ind.vidx = cast_short(info); /* mark it as a declared global */ + if (info != -1 && ls->dyd->actvar.arr[info].vd.kind == GDKCONST) + var->u.ind.ro = 1; /* mark variable as read-only */ + else /* anyway must be a global */ + lua_assert(info == -1 || ls->dyd->actvar.arr[info].vd.kind == GDKREG); } } diff --git a/lparser.h b/lparser.h index 274fb1c469..524df6ea74 100644 --- a/lparser.h +++ b/lparser.h @@ -45,16 +45,19 @@ typedef enum { info = absolute index in 'actvar.arr' */ VINDEXED, /* indexed variable; ind.t = table register; - ind.idx = key's R index */ + ind.idx = key's R index; + ind.ro = true if it represents a read-only global; + ind.keystr = if key is a string, index in 'k' of that string; + -1 if key is not a string */ VINDEXUP, /* indexed upvalue; - ind.t = table upvalue; - ind.idx = key's K index */ + ind.idx = key's K index; + ind.* as in VINDEXED */ VINDEXI, /* indexed variable with constant integer; ind.t = table register; ind.idx = key's value */ VINDEXSTR, /* indexed variable with literal string; - ind.t = table register; - ind.idx = key's K index */ + ind.idx = key's K index; + ind.* as in VINDEXED */ VJMP, /* expression is a test/comparison; info = pc of corresponding jump instruction */ VRELOC, /* expression can put result in any register; @@ -77,8 +80,9 @@ typedef struct expdesc { int info; /* for generic use */ struct { /* for indexed variables */ short idx; /* index (R or "long" K) */ - short vidx; /* index in 'actvar.arr' or -1 if not a declared global */ lu_byte t; /* table (register or upvalue) */ + lu_byte ro; /* true if variable is read-only */ + int keystr; /* index in 'k' of string key, or -1 if not a string */ } ind; struct { /* for local variables */ lu_byte ridx; /* register holding the variable */ From 7dc6aae29057c9dc4588f780c7abd72a62ff4c8e Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 12 May 2025 11:42:45 -0300 Subject: [PATCH 1044/1145] Correct line in error message for constant function --- lparser.c | 2 +- testes/goto.lua | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/lparser.c b/lparser.c index 3c2f57ab5e..93991cb051 100644 --- a/lparser.c +++ b/lparser.c @@ -1858,8 +1858,8 @@ static void funcstat (LexState *ls, int line) { expdesc v, b; luaX_next(ls); /* skip FUNCTION */ ismethod = funcname(ls, &v); - body(ls, &b, ismethod, line); check_readonly(ls, &v); + body(ls, &b, ismethod, line); luaK_storevar(ls->fs, &v, &b); luaK_fixline(ls->fs, line); /* definition "happens" in the first line */ } diff --git a/testes/goto.lua b/testes/goto.lua index b41399ffce..59713dd78a 100644 --- a/testes/goto.lua +++ b/testes/goto.lua @@ -342,6 +342,13 @@ do global foo ; function foo (x) return end -- ERROR: foo is read-only ]], "assign to const variable 'foo'") + + checkerr([[ + global foo ; + function foo (x) -- ERROR: foo is read-only + return + end + ]], "%:2%:") -- correct line in error message end From 3b9dd52be02fd43c598f4adb6fa7844e6a573923 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 13 May 2025 11:43:10 -0300 Subject: [PATCH 1045/1145] Collective declaration for globals ('global *') --- lparser.c | 55 ++++++++++++++++++++------------ manual/manual.of | 76 ++++++++++++++++++++++++++++++++++----------- testes/all.lua | 8 +++-- testes/calls.lua | 15 +++++---- testes/closure.lua | 2 ++ testes/code.lua | 6 ++-- testes/files.lua | 6 ++-- testes/goto.lua | 24 ++++++++++---- testes/literals.lua | 2 ++ testes/locals.lua | 12 ++++--- testes/nextvar.lua | 7 ++--- testes/pm.lua | 2 ++ testes/strings.lua | 1 + testes/utf8.lua | 2 ++ 14 files changed, 155 insertions(+), 63 deletions(-) diff --git a/lparser.c b/lparser.c index 93991cb051..242bb0010b 100644 --- a/lparser.c +++ b/lparser.c @@ -405,7 +405,12 @@ static int searchvar (FuncState *fs, TString *n, expdesc *var) { int i; for (i = cast_int(fs->nactvar) - 1; i >= 0; i--) { Vardesc *vd = getlocalvardesc(fs, i); - if (eqstr(n, vd->vd.name)) { /* found? */ + if (vd->vd.name == NULL) { /* 'global *'? */ + if (var->u.info == -1) { /* no previous collective declaration? */ + var->u.info = fs->firstlocal + i; /* will use this one as default */ + } + } + else if (eqstr(n, vd->vd.name)) { /* found? */ if (vd->vd.kind == RDKCTC) /* compile-time constant? */ init_exp(var, VCONST, fs->firstlocal + i); else if (vd->vd.kind == GDKREG || vd->vd.kind == GDKCONST) @@ -449,18 +454,16 @@ static void marktobeclosed (FuncState *fs) { ** 'var' as 'void' as a flag. */ static void singlevaraux (FuncState *fs, TString *n, expdesc *var, int base) { - int v = searchvar(fs, n, var); /* look up locals at current level */ + int v = searchvar(fs, n, var); /* look up variables at current level */ if (v >= 0) { /* found? */ if (v == VLOCAL && !base) markupval(fs, var->u.var.vidx); /* local will be used as an upval */ } - else { /* not found as local at current level; try upvalues */ + else { /* not found at current level; try upvalues */ int idx = searchupvalue(fs, n); /* try existing upvalues */ if (idx < 0) { /* not found? */ if (fs->prev != NULL) /* more levels? */ singlevaraux(fs->prev, n, var, 0); /* try upper levels */ - else /* no more levels */ - init_exp(var, VGLOBAL, -1); /* global by default */ if (var->k == VLOCAL || var->k == VUPVAL) /* local or upvalue? */ idx = newupvalue(fs, n, var); /* will be a new upvalue */ else /* it is a global or a constant */ @@ -477,6 +480,7 @@ static void singlevaraux (FuncState *fs, TString *n, expdesc *var, int base) { */ static void buildvar (LexState *ls, TString *varname, expdesc *var) { FuncState *fs = ls->fs; + init_exp(var, VGLOBAL, -1); /* global by default */ singlevaraux(fs, varname, var, 1); if (var->k == VGLOBAL) { /* global name? */ expdesc key; @@ -1796,20 +1800,33 @@ static void localstat (LexState *ls) { } +static lu_byte getglobalattribute (LexState *ls) { + lu_byte kind = getvarattribute(ls); + if (kind == RDKTOCLOSE) + luaK_semerror(ls, "global variables cannot be to-be-closed"); + /* adjust kind for global variable */ + return (kind == VDKREG) ? GDKREG : GDKCONST; +} + + static void globalstat (LexState *ls) { - /* globalstat -> (GLOBAL) NAME attrib {',' NAME attrib} */ + /* globalstat -> (GLOBAL) '*' attrib + globalstat -> (GLOBAL) NAME attrib {',' NAME attrib} */ FuncState *fs = ls->fs; - do { - TString *vname = str_checkname(ls); - lu_byte kind = getvarattribute(ls); - if (kind == RDKTOCLOSE) - luaK_semerror(ls, "global variable ('%s') cannot be to-be-closed", - getstr(vname)); - /* adjust kind for global variable */ - kind = (kind == VDKREG) ? GDKREG : GDKCONST; - new_varkind(ls, vname, kind); + if (testnext(ls, '*')) { + lu_byte kind = getglobalattribute(ls); + /* use NULL as name to represent '*' entries */ + new_varkind(ls, NULL, kind); fs->nactvar++; /* activate declaration */ - } while (testnext(ls, ',')); + } + else { + do { + TString *vname = str_checkname(ls); + lu_byte kind = getglobalattribute(ls); + new_varkind(ls, vname, kind); + fs->nactvar++; /* activate declaration */ + } while (testnext(ls, ',')); + } } @@ -1983,10 +2000,10 @@ static void statement (LexState *ls) { case TK_NAME: { /* compatibility code to parse global keyword when "global" is not reserved */ - if (eqstr(ls->t.seminfo.ts, luaS_newliteral(ls->L, "global"))) { + if (strcmp(getstr(ls->t.seminfo.ts), "global") == 0) { int lk = luaX_lookahead(ls); - if (lk == TK_NAME || lk == TK_FUNCTION) { - /* 'global ' or 'global function' */ + if (lk == TK_NAME || lk == '*' || lk == TK_FUNCTION) { + /* 'global ' or 'global *' or 'global function' */ globalstatfunc(ls, line); break; } diff --git a/manual/manual.of b/manual/manual.of index cc71aaada7..effb95da15 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -223,14 +223,15 @@ a function's formal parameter is equivalent to a local variable.) All chunks start with an implicit declaration @T{global *}, which declares all free names as global variables; -this implicit declaration becomes void inside the scope of any other -@Rw{global} declaration, regardless of the names being declared. +this preambular declaration becomes void inside the scope of any other +@Rw{global} declaration, +as the following example illustrates: @verbatim{ X = 1 -- Ok, global by default do global Y -- voids implicit initial declaration - X = 1 -- ERROR, X not declared Y = 1 -- Ok, Y declared as global + X = 1 -- ERROR, X not declared end X = 2 -- Ok, global by default again } @@ -1110,9 +1111,9 @@ and cannot be used as names: @index{reserved words} @verbatim{ and break do else elseif end -false for function goto if in -local nil not or repeat return -then true until while +false for function global goto if +in local nil not or repeat +return then true until while } Lua is a case-sensitive language: @@ -1653,7 +1654,8 @@ The declaration for locals can include an initialization: @producname{stat}@producbody{@Rw{local} attnamelist @bnfopt{@bnfter{=} explist}} @producname{stat}@producbody{@Rw{global} attnamelist} @producname{attnamelist}@producbody{ - @bnfNter{Name} attrib @bnfrep{@bnfter{,} @bnfNter{Name} attrib}} + @bnfNter{Name} @bnfopt{attrib} + @bnfrep{@bnfter{,} @bnfNter{Name} @bnfopt{attrib}}} } If present, an initial assignment has the same semantics of a multiple assignment @see{assignment}. @@ -1662,24 +1664,55 @@ Otherwise, all local variables are initialized with @nil. Each variable name may be postfixed by an attribute (a name between angle brackets): @Produc{ -@producname{attrib}@producbody{@bnfopt{@bnfter{<} @bnfNter{Name} @bnfter{>}}} +@producname{attrib}@producbody{@bnfter{<} @bnfNter{Name} @bnfter{>}} } There are two possible attributes: @id{const}, which declares a @emph{constant} or @emph{read-only} variable, @index{constant variable} -that is, a variable that cannot be assigned to -after its initialization; +that is, a variable that cannot be used as the left-hand side of an +assignment, and @id{close}, which declares a to-be-closed variable @see{to-be-closed}. A list of variables can contain at most one to-be-closed variable. Only local variables can have the @id{close} attribute. +Lua offers also a collective declaration for global variables: +@Produc{ +@producname{stat}@producbody{@Rw{global} @bnfter{*} @bnfopt{attrib}} +} +This special form implicitly declares +as globals all names not explicitly declared previously. +In particular, +@T{global * } implicitly declares +as read-only globals all names not explicitly declared previously; +see the following example: +@verbatim{ +global X +global * +print(math.pi) -- Ok, 'print' and 'math' are read-only +X = 1 -- Ok, declared as read-write +Y = 1 -- Error, Y is read-only +} + +As noted in @See{globalenv}, +all chunks start with an implicit declaration @T{global *}, +but this preambular declaration becomes void inside +the scope of any other @Rw{global} declaration. +Therefore, a program that does not use global declarations +or start with @T{global *} +has free read-write access to any global; +a program that starts with @T{global * } +has free read-only access to any global; +and a program that starts with any other global declaration +(e.g., @T{global none}) can only refer to declared variables. + Note that, for global variables, -the @emph{read-only} atribute is only a syntactical restriction: +the effect of any declaration is only syntactical: @verbatim{ -global X -X = 1 -- ERROR -_ENV.X = 1 -- Ok -foo() -- 'foo' can freely change the global X +global X , _G +X = 1 -- ERROR +_ENV.X = 1 -- Ok +_G.print(X) -- Ok +foo() -- 'foo' can freely change any global } A chunk is also a block @see{chunks}, @@ -9453,7 +9486,12 @@ change between versions. @itemize{ @item{ -The control variable in @Rw{for} loops are read only. +The word @Rw{global} is a reserved word. +Do not use it as a regular name. +} + +@item{ +The control variable in @Rw{for} loops is read only. If you need to change it, declare a local variable with the same name in the loop body. } @@ -9582,12 +9620,14 @@ and @bnfNter{LiteralString}, see @See{lexical}.) @OrNL @Rw{global} @Rw{function} @bnfNter{Name} funcbody @OrNL @Rw{local} attnamelist @bnfopt{@bnfter{=} explist} @OrNL @Rw{global} attnamelist +@OrNL @Rw{global} @bnfter{*} @bnfopt{attrib} } @producname{attnamelist}@producbody{ - @bnfNter{Name} attrib @bnfrep{@bnfter{,} @bnfNter{Name} attrib}} + @bnfNter{Name} @bnfopt{attrib} + @bnfrep{@bnfter{,} @bnfNter{Name} @bnfopt{attrib}}} -@producname{attrib}@producbody{@bnfopt{@bnfter{<} @bnfNter{Name} @bnfter{>}}} +@producname{attrib}@producbody{@bnfter{<} @bnfNter{Name} @bnfter{>}} @producname{retstat}@producbody{@Rw{return} @bnfopt{explist} @bnfopt{@bnfter{;}}} diff --git a/testes/all.lua b/testes/all.lua index 5c7ebfa5bf..499c100d76 100755 --- a/testes/all.lua +++ b/testes/all.lua @@ -2,6 +2,10 @@ -- $Id: testes/all.lua $ -- See Copyright Notice in file lua.h +global * + +global _soft, _port, _nomsg +global T local version = "Lua 5.5" if _VERSION ~= version then @@ -34,7 +38,7 @@ if usertests then end -- tests should require debug when needed -debug = nil +global debug; debug = nil if usertests then @@ -71,7 +75,7 @@ do -- ( -- track messages for tests not performed local msgs = {} -function Message (m) +global function Message (m) if not _nomsg then print(m) msgs[#msgs+1] = string.sub(m, 3, -3) diff --git a/testes/calls.lua b/testes/calls.lua index 942fad72e0..0ea1c4ab0d 100644 --- a/testes/calls.lua +++ b/testes/calls.lua @@ -1,6 +1,8 @@ -- $Id: testes/calls.lua $ -- See Copyright Notice in file lua.h +global * + print("testing functions and calls") local debug = require "debug" @@ -22,7 +24,7 @@ assert(not pcall(type)) -- testing local-function recursion -fact = false +global fact; fact = false do local res = 1 local function fact (n) @@ -63,7 +65,7 @@ a.b.c:f2('k', 12); assert(a.b.c.k == 12) print('+') -t = nil -- 'declare' t +global t; t = nil -- 'declare' t function f(a,b,c) local d = 'a'; t={a,b,c,d} end f( -- this line change must be valid @@ -75,7 +77,7 @@ assert(t[1] == 1 and t[2] == 2 and t[3] == 3 and t[4] == 'a') t = nil -- delete 't' -function fat(x) +global function fat(x) if x <= 1 then return 1 else return x*load("return fat(" .. x-1 .. ")", "")() end @@ -107,7 +109,7 @@ end _G.deep = nil -- "declaration" (used by 'all.lua') -function deep (n) +global function deep (n) if n>0 then deep(n-1) end end deep(10) @@ -352,7 +354,7 @@ assert(not load(function () return true end)) -- small bug local t = {nil, "return ", "3"} -f, msg = load(function () return table.remove(t, 1) end) +local f, msg = load(function () return table.remove(t, 1) end) assert(f() == nil) -- should read the empty chunk -- another small bug (in 5.2.1) @@ -388,7 +390,8 @@ assert(load("return _ENV", nil, nil, 123)() == 123) -- load when _ENV is not first upvalue -local x; XX = 123 +global XX; local x +XX = 123 local function h () local y=x -- use 'x', so that it becomes 1st upvalue return XX -- global name diff --git a/testes/closure.lua b/testes/closure.lua index d3b9f6216a..c55d15838f 100644 --- a/testes/closure.lua +++ b/testes/closure.lua @@ -1,6 +1,8 @@ -- $Id: testes/closure.lua $ -- See Copyright Notice in file lua.h +global * + print "testing closures" do -- bug in 5.4.7 diff --git a/testes/code.lua b/testes/code.lua index 111717cefe..b6ceb34cb3 100644 --- a/testes/code.lua +++ b/testes/code.lua @@ -1,6 +1,8 @@ -- $Id: testes/code.lua $ -- See Copyright Notice in file lua.h +global * + if T==nil then (Message or print)('\n >>> testC not active: skipping opcode tests <<<\n') return @@ -405,8 +407,8 @@ do -- tests for table access in upvalues end -- de morgan -checkequal(function () local a; if not (a or b) then b=a end end, - function () local a; if (not a and not b) then b=a end end) +checkequal(function () local a, b; if not (a or b) then b=a end end, + function () local a, b; if (not a and not b) then b=a end end) checkequal(function (l) local a; return 0 <= a and a <= l end, function (l) local a; return not (not(a >= 0) or not(a <= l)) end) diff --git a/testes/files.lua b/testes/files.lua index a0ae661c40..c2b355fb8d 100644 --- a/testes/files.lua +++ b/testes/files.lua @@ -1,6 +1,8 @@ -- $Id: testes/files.lua $ -- See Copyright Notice in file lua.h +global * + local debug = require "debug" local maxint = math.maxinteger @@ -838,13 +840,13 @@ assert(os.date("!\0\0") == "\0\0") local x = string.rep("a", 10000) assert(os.date(x) == x) local t = os.time() -D = os.date("*t", t) +global D; D = os.date("*t", t) assert(os.date(string.rep("%d", 1000), t) == string.rep(os.date("%d", t), 1000)) assert(os.date(string.rep("%", 200)) == string.rep("%", 100)) local function checkDateTable (t) - _G.D = os.date("*t", t) + D = os.date("*t", t) assert(os.time(D) == t) load(os.date([[assert(D.year==%Y and D.month==%m and D.day==%d and D.hour==%H and D.min==%M and D.sec==%S and diff --git a/testes/goto.lua b/testes/goto.lua index 59713dd78a..3f1f6e6949 100644 --- a/testes/goto.lua +++ b/testes/goto.lua @@ -1,6 +1,10 @@ -- $Id: testes/goto.lua $ -- See Copyright Notice in file lua.h +global require +global print, load, assert, string, setmetatable +global collectgarbage, error + print("testing goto and global declarations") collectgarbage() @@ -254,6 +258,8 @@ assert(testG(5) == 10) do -- test goto's around to-be-closed variable + global * + -- set 'var' and return an object that will reset 'var' when -- it goes out of scope local function newobj (var) @@ -265,16 +271,16 @@ do -- test goto's around to-be-closed variable goto L1 - ::L4:: assert(not X); goto L5 -- varX dead here + ::L4:: assert(not varX); goto L5 -- varX dead here ::L1:: local varX = newobj("X") - assert(X); goto L2 -- varX alive here + assert(varX); goto L2 -- varX alive here ::L3:: - assert(X); goto L4 -- varX alive here + assert(varX); goto L4 -- varX alive here - ::L2:: assert(X); goto L3 -- varX alive here + ::L2:: assert(varX); goto L3 -- varX alive here ::L5:: -- return end @@ -285,8 +291,7 @@ foo() -------------------------------------------------------------------------- do - global print, load, T; global assert - global string + global T local function checkerr (code, err) local st, msg = load(code) @@ -299,6 +304,7 @@ do -- global variables cannot be to-be-closed checkerr("global X", "cannot be") + checkerr("global * ", "cannot be") do local X = 10 @@ -349,6 +355,12 @@ do return end ]], "%:2%:") -- correct line in error message + + checkerr([[ + global * ; + print(X) -- Ok to use + Y = 1 -- ERROR + ]], "assign to const variable 'Y'") end diff --git a/testes/literals.lua b/testes/literals.lua index 28995718b7..fecdd6d3b9 100644 --- a/testes/literals.lua +++ b/testes/literals.lua @@ -3,6 +3,8 @@ print('testing scanner') +global * + local debug = require "debug" diff --git a/testes/locals.lua b/testes/locals.lua index 421595bb9b..99ff9edc5e 100644 --- a/testes/locals.lua +++ b/testes/locals.lua @@ -1,6 +1,8 @@ -- $Id: testes/locals.lua $ -- See Copyright Notice in file lua.h +global * + print('testing local variables and environments') local debug = require"debug" @@ -39,9 +41,11 @@ f = nil local f local x = 1 -a = nil -load('local a = {}')() -assert(a == nil) +do + global a; a = nil + load('local a = {}')() + assert(a == nil) +end function f (a) local _1, _2, _3, _4, _5 @@ -154,7 +158,7 @@ local _ENV = (function (...) return ... end)(_G, dummy) -- { do local _ENV = {assert=assert}; assert(true) end local mt = {_G = _G} local foo,x -A = false -- "declare" A +global A; A = false -- "declare" A do local _ENV = mt function foo (x) A = x diff --git a/testes/nextvar.lua b/testes/nextvar.lua index 679cb1e4ac..e5a9717841 100644 --- a/testes/nextvar.lua +++ b/testes/nextvar.lua @@ -1,6 +1,8 @@ -- $Id: testes/nextvar.lua $ -- See Copyright Notice in file lua.h +global * + print('testing tables, next, and for') local function checkerror (msg, f, ...) @@ -345,9 +347,6 @@ end local nofind = {} -a,b,c = 1,2,3 -a,b,c = nil - -- next uses always the same iteration function assert(next{} == next{}) @@ -396,7 +395,7 @@ for i=0,10000 do end end -n = {n=0} +local n = {n=0} for i,v in pairs(a) do n.n = n.n+1 assert(i and v and a[i] == v) diff --git a/testes/pm.lua b/testes/pm.lua index ab19eb5db8..1700ca2c23 100644 --- a/testes/pm.lua +++ b/testes/pm.lua @@ -6,6 +6,8 @@ print('testing pattern matching') +global * + local function checkerror (msg, f, ...) local s, err = pcall(f, ...) assert(not s and string.find(err, msg)) diff --git a/testes/strings.lua b/testes/strings.lua index ce28e4c560..455398c3f5 100644 --- a/testes/strings.lua +++ b/testes/strings.lua @@ -3,6 +3,7 @@ -- ISO Latin encoding +global * print('testing strings and string library') diff --git a/testes/utf8.lua b/testes/utf8.lua index d0c0184d34..ec9b706ff7 100644 --- a/testes/utf8.lua +++ b/testes/utf8.lua @@ -3,6 +3,8 @@ -- UTF-8 file +global * + print "testing UTF-8 library" local utf8 = require'utf8' From fded0b4a844990b1a6d0cda1aba25df33eb5f46f Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 13 May 2025 11:50:43 -0300 Subject: [PATCH 1046/1145] Remove compat code in parser when not needed --- llex.c | 2 +- lparser.c | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/llex.c b/llex.c index edeb48feee..59d927d468 100644 --- a/llex.c +++ b/llex.c @@ -41,7 +41,7 @@ #define currIsNewline(ls) (ls->current == '\n' || ls->current == '\r') #if defined(LUA_COMPAT_GLOBAL) -#define GLOBALLEX ".g" /* not recognizable by the scanner */ +#define GLOBALLEX ".g" /* anything not recognizable as a name */ #else #define GLOBALLEX "global" #endif diff --git a/lparser.c b/lparser.c index 242bb0010b..27c8a92758 100644 --- a/lparser.c +++ b/lparser.c @@ -1997,6 +1997,7 @@ static void statement (LexState *ls) { gotostat(ls, line); break; } +#if defined(LUA_COMPAT_GLOBAL) case TK_NAME: { /* compatibility code to parse global keyword when "global" is not reserved */ @@ -2008,7 +2009,9 @@ static void statement (LexState *ls) { break; } } /* else... */ - } /* FALLTHROUGH */ + } +#endif + /* FALLTHROUGH */ default: { /* stat -> func | assignment */ exprstat(ls); break; From 3fb7a77731e6140674a6b13b73979256bfb95ce3 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 15 May 2025 12:43:37 -0300 Subject: [PATCH 1047/1145] Internalized string "break" kept by the parser The parser uses "break" as fake label to compile "break" as "goto break". To avoid producing this string at each use, it keeps it available in its state. --- llex.c | 3 +++ llex.h | 1 + lparser.c | 6 +++--- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/llex.c b/llex.c index 59d927d468..54e7f343d4 100644 --- a/llex.c +++ b/llex.c @@ -190,6 +190,9 @@ void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, TString *source, ls->lastline = 1; ls->source = source; ls->envn = luaS_newliteral(L, LUA_ENV); /* get env name */ + ls->brkn = luaS_newliteral(L, "break"); /* get "break" name */ + /* "break" cannot be collected, as it is a reserved word" */ + lua_assert(isreserved(ls->brkn)); luaZ_resizebuffer(ls->L, ls->buff, LUA_MINBUFFER); /* initialize buffer */ } diff --git a/llex.h b/llex.h index 078e4d31e5..0dba9d6094 100644 --- a/llex.h +++ b/llex.h @@ -75,6 +75,7 @@ typedef struct LexState { struct Dyndata *dyd; /* dynamic structures used by the parser */ TString *source; /* current source name */ TString *envn; /* environment variable name */ + TString *brkn; /* "break" name (used as a label) */ } LexState; diff --git a/lparser.c b/lparser.c index 27c8a92758..29022bfdb6 100644 --- a/lparser.c +++ b/lparser.c @@ -707,7 +707,7 @@ static void enterblock (FuncState *fs, BlockCnt *bl, lu_byte isloop) { */ static l_noret undefgoto (LexState *ls, Labeldesc *gt) { /* breaks are checked when created, cannot be undefined */ - lua_assert(!eqstr(gt->name, luaS_newliteral(ls->L, "break"))); + lua_assert(!eqstr(gt->name, ls->brkn)); luaK_semerror(ls, "no visible label '%s' for at line %d", getstr(gt->name), gt->line); } @@ -723,7 +723,7 @@ static void leaveblock (FuncState *fs) { removevars(fs, bl->nactvar); /* remove block locals */ lua_assert(bl->nactvar == fs->nactvar); /* back to level on entry */ if (bl->isloop == 2) /* has to fix pending breaks? */ - createlabel(ls, luaS_newliteral(ls->L, "break"), 0, 0); + createlabel(ls, ls->brkn, 0, 0); solvegotos(fs, bl); if (bl->previous == NULL) { /* was it the last block? */ if (bl->firstgoto < ls->dyd->gt.n) /* still pending gotos? */ @@ -1497,7 +1497,7 @@ static void breakstat (LexState *ls, int line) { ok: bl->isloop = 2; /* signal that block has pending breaks */ luaX_next(ls); /* skip break */ - newgotoentry(ls, luaS_newliteral(ls->L, "break"), line); + newgotoentry(ls, ls->brkn, line); } From ded2ad2d86f44424c6b6e12bf1b75836cfa9e502 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 16 May 2025 14:51:07 -0300 Subject: [PATCH 1048/1145] Slightly faster way to check for "global" --- llex.c | 20 ++++++++++---------- llex.h | 1 + lparser.c | 4 ++-- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/llex.c b/llex.c index 54e7f343d4..f8bb3ea4b4 100644 --- a/llex.c +++ b/llex.c @@ -40,16 +40,11 @@ #define currIsNewline(ls) (ls->current == '\n' || ls->current == '\r') -#if defined(LUA_COMPAT_GLOBAL) -#define GLOBALLEX ".g" /* anything not recognizable as a name */ -#else -#define GLOBALLEX "global" -#endif /* ORDER RESERVED */ static const char *const luaX_tokens [] = { "and", "break", "do", "else", "elseif", - "end", "false", "for", "function", GLOBALLEX, "goto", "if", + "end", "false", "for", "function", "global", "goto", "if", "in", "local", "nil", "not", "or", "repeat", "return", "then", "true", "until", "while", "//", "..", "...", "==", ">=", "<=", "~=", @@ -189,10 +184,15 @@ void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, TString *source, ls->linenumber = 1; ls->lastline = 1; ls->source = source; - ls->envn = luaS_newliteral(L, LUA_ENV); /* get env name */ - ls->brkn = luaS_newliteral(L, "break"); /* get "break" name */ - /* "break" cannot be collected, as it is a reserved word" */ - lua_assert(isreserved(ls->brkn)); + /* all three strings here ("_ENV", "break", "global") were fixed, + so they cannot be collected */ + ls->envn = luaS_newliteral(L, LUA_ENV); /* get env string */ + ls->brkn = luaS_newliteral(L, "break"); /* get "break" string */ +#if defined(LUA_COMPAT_GLOBAL) + /* compatibility mode: "global" is not a reserved word */ + ls->glbn = luaS_newliteral(L, "global"); /* get "global" string */ + ls->glbn->extra = 0; /* mark it as not reserved */ +#endif luaZ_resizebuffer(ls->L, ls->buff, LUA_MINBUFFER); /* initialize buffer */ } diff --git a/llex.h b/llex.h index 0dba9d6094..37016e8a3f 100644 --- a/llex.h +++ b/llex.h @@ -76,6 +76,7 @@ typedef struct LexState { TString *source; /* current source name */ TString *envn; /* environment variable name */ TString *brkn; /* "break" name (used as a label) */ + TString *glbn; /* "global" name (when not a reserved word) */ } LexState; diff --git a/lparser.c b/lparser.c index 29022bfdb6..384ef690c9 100644 --- a/lparser.c +++ b/lparser.c @@ -2001,10 +2001,10 @@ static void statement (LexState *ls) { case TK_NAME: { /* compatibility code to parse global keyword when "global" is not reserved */ - if (strcmp(getstr(ls->t.seminfo.ts), "global") == 0) { + if (ls->t.seminfo.ts == ls->glbn) { /* current = "global"? */ int lk = luaX_lookahead(ls); if (lk == TK_NAME || lk == '*' || lk == TK_FUNCTION) { - /* 'global ' or 'global *' or 'global function' */ + /* 'global name' or 'global *' or 'global function' */ globalstatfunc(ls, line); break; } From f2c1531e6cacb10926158d8def5fa5841a0f357e Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 16 May 2025 15:20:32 -0300 Subject: [PATCH 1049/1145] Detail Reports errors with "?:?:" (instead of "?:-1:") when there is no debug information. --- ldebug.c | 11 +++++------ testes/errors.lua | 4 ++-- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/ldebug.c b/ldebug.c index f4bb0a08a9..9110f437bf 100644 --- a/ldebug.c +++ b/ldebug.c @@ -817,16 +817,15 @@ l_noret luaG_ordererror (lua_State *L, const TValue *p1, const TValue *p2) { /* add src:line information to 'msg' */ const char *luaG_addinfo (lua_State *L, const char *msg, TString *src, int line) { - char buff[LUA_IDSIZE]; - if (src) { + if (src == NULL) /* no debug information? */ + return luaO_pushfstring(L, "?:?: %s", msg); + else { + char buff[LUA_IDSIZE]; size_t idlen; const char *id = getlstr(src, idlen); luaO_chunkid(buff, id, idlen); + return luaO_pushfstring(L, "%s:%d: %s", buff, line, msg); } - else { /* no source available; use "?" instead */ - buff[0] = '?'; buff[1] = '\0'; - } - return luaO_pushfstring(L, "%s:%d: %s", buff, line, msg); } diff --git a/testes/errors.lua b/testes/errors.lua index 6c76a99a16..a072891366 100644 --- a/testes/errors.lua +++ b/testes/errors.lua @@ -303,14 +303,14 @@ do local f = function (a) return a + 1 end f = assert(load(string.dump(f, true))) assert(f(3) == 4) - checkerr("^%?:%-1:", f, {}) + checkerr("^%?:%?:", f, {}) -- code with a move to a local var ('OP_MOV A B' with A Date: Sun, 18 May 2025 11:43:43 -0300 Subject: [PATCH 1050/1145] Variable attributes can prefix name list In this format, the attribute applies to all names in the list; e.g. "global print, require, math". --- lparser.c | 53 ++++++++++++++++++++++++++------------------- manual/manual.of | 30 ++++++++++++++----------- testes/all.lua | 2 +- testes/calls.lua | 2 +- testes/closure.lua | 2 +- testes/code.lua | 2 +- testes/files.lua | 2 +- testes/goto.lua | 12 +++++----- testes/literals.lua | 2 +- testes/locals.lua | 22 ++++++++++++++----- testes/math.lua | 7 +++--- testes/nextvar.lua | 2 +- testes/pm.lua | 2 +- testes/strings.lua | 2 +- testes/utf8.lua | 2 +- 15 files changed, 84 insertions(+), 60 deletions(-) diff --git a/lparser.c b/lparser.c index 384ef690c9..bad3592ade 100644 --- a/lparser.c +++ b/lparser.c @@ -1733,7 +1733,7 @@ static void localfunc (LexState *ls) { } -static lu_byte getvarattribute (LexState *ls) { +static lu_byte getvarattribute (LexState *ls, lu_byte df) { /* attrib -> ['<' NAME '>'] */ if (testnext(ls, '<')) { TString *ts = str_checkname(ls); @@ -1746,7 +1746,7 @@ static lu_byte getvarattribute (LexState *ls) { else luaK_semerror(ls, "unknown attribute '%s'", attr); } - return VDKREG; /* regular variable */ + return df; /* return default value */ } @@ -1767,10 +1767,12 @@ static void localstat (LexState *ls) { int nvars = 0; int nexps; expdesc e; - do { - TString *vname = str_checkname(ls); - lu_byte kind = getvarattribute(ls); - vidx = new_varkind(ls, vname, kind); + /* get prefixed attribute (if any); default is regular local variable */ + lu_byte defkind = getvarattribute(ls, VDKREG); + do { /* for each variable */ + TString *vname = str_checkname(ls); /* get its name */ + lu_byte kind = getvarattribute(ls, defkind); /* postfixed attribute */ + vidx = new_varkind(ls, vname, kind); /* predeclare it */ if (kind == RDKTOCLOSE) { /* to-be-closed? */ if (toclose != -1) /* one already present? */ luaK_semerror(ls, "multiple to-be-closed variables in local list"); @@ -1778,13 +1780,13 @@ static void localstat (LexState *ls) { } nvars++; } while (testnext(ls, ',')); - if (testnext(ls, '=')) + if (testnext(ls, '=')) /* initialization? */ nexps = explist(ls, &e); else { e.k = VVOID; nexps = 0; } - var = getlocalvardesc(fs, vidx); /* get last variable */ + var = getlocalvardesc(fs, vidx); /* retrieve last variable */ if (nvars == nexps && /* no adjustments? */ var->vd.kind == RDKCONST && /* last variable is const? */ luaK_exp2const(fs, &e, &var->k)) { /* compile-time constant? */ @@ -1800,29 +1802,35 @@ static void localstat (LexState *ls) { } -static lu_byte getglobalattribute (LexState *ls) { - lu_byte kind = getvarattribute(ls); - if (kind == RDKTOCLOSE) - luaK_semerror(ls, "global variables cannot be to-be-closed"); - /* adjust kind for global variable */ - return (kind == VDKREG) ? GDKREG : GDKCONST; +static lu_byte getglobalattribute (LexState *ls, lu_byte df) { + lu_byte kind = getvarattribute(ls, df); + switch (kind) { + case RDKTOCLOSE: + luaK_semerror(ls, "global variables cannot be to-be-closed"); + break; /* to avoid warnings */ + case RDKCONST: + return GDKCONST; /* adjust kind for global variable */ + default: + return kind; + } } static void globalstat (LexState *ls) { - /* globalstat -> (GLOBAL) '*' attrib - globalstat -> (GLOBAL) NAME attrib {',' NAME attrib} */ + /* globalstat -> (GLOBAL) attrib '*' + globalstat -> (GLOBAL) attrib NAME attrib {',' NAME attrib} */ FuncState *fs = ls->fs; + /* get prefixed attribute (if any); default is regular global variable */ + lu_byte defkind = getglobalattribute(ls, GDKREG); if (testnext(ls, '*')) { - lu_byte kind = getglobalattribute(ls); /* use NULL as name to represent '*' entries */ - new_varkind(ls, NULL, kind); + new_varkind(ls, NULL, defkind); fs->nactvar++; /* activate declaration */ } else { - do { + do { /* list of names */ TString *vname = str_checkname(ls); - lu_byte kind = getglobalattribute(ls); + lu_byte kind = getglobalattribute(ls, defkind); new_varkind(ls, vname, kind); fs->nactvar++; /* activate declaration */ } while (testnext(ls, ',')); @@ -2003,8 +2011,9 @@ static void statement (LexState *ls) { is not reserved */ if (ls->t.seminfo.ts == ls->glbn) { /* current = "global"? */ int lk = luaX_lookahead(ls); - if (lk == TK_NAME || lk == '*' || lk == TK_FUNCTION) { - /* 'global name' or 'global *' or 'global function' */ + if (lk == '<' || lk == TK_NAME || lk == '*' || lk == TK_FUNCTION) { + /* 'global ' or 'global name' or 'global *' or + 'global function' */ globalstatfunc(ls, line); break; } diff --git a/manual/manual.of b/manual/manual.of index effb95da15..eb97e853d1 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -1651,43 +1651,47 @@ Function calls are explained in @See{functioncall}. Local and global variables can be declared anywhere inside a block. The declaration for locals can include an initialization: @Produc{ -@producname{stat}@producbody{@Rw{local} attnamelist @bnfopt{@bnfter{=} explist}} +@producname{stat}@producbody{@Rw{local} + attnamelist @bnfopt{@bnfter{=} explist}} @producname{stat}@producbody{@Rw{global} attnamelist} -@producname{attnamelist}@producbody{ - @bnfNter{Name} @bnfopt{attrib} - @bnfrep{@bnfter{,} @bnfNter{Name} @bnfopt{attrib}}} } If present, an initial assignment has the same semantics of a multiple assignment @see{assignment}. Otherwise, all local variables are initialized with @nil. -Each variable name may be postfixed by an attribute -(a name between angle brackets): +The list of names may be prefixed by an attribute +(a name between angle brackets) +and each variable name may be postfixed by an attribute: @Produc{ +@producname{attnamelist}@producbody{ + @bnfopt{attrib} @bnfNter{Name} @bnfopt{attrib} + @bnfrep{@bnfter{,} @bnfNter{Name} @bnfopt{attrib}}} @producname{attrib}@producbody{@bnfter{<} @bnfNter{Name} @bnfter{>}} } +A prefixed attribute applies to all names in the list; +a postfixed attribute applies to its particular name. There are two possible attributes: @id{const}, which declares a @emph{constant} or @emph{read-only} variable, @index{constant variable} that is, a variable that cannot be used as the left-hand side of an assignment, and @id{close}, which declares a to-be-closed variable @see{to-be-closed}. -A list of variables can contain at most one to-be-closed variable. Only local variables can have the @id{close} attribute. +A list of variables can contain at most one to-be-closed variable. Lua offers also a collective declaration for global variables: @Produc{ -@producname{stat}@producbody{@Rw{global} @bnfter{*} @bnfopt{attrib}} +@producname{stat}@producbody{@Rw{global} @bnfopt{attrib} @bnfter{*}} } This special form implicitly declares as globals all names not explicitly declared previously. In particular, -@T{global * } implicitly declares +@T{global *} implicitly declares as read-only globals all names not explicitly declared previously; see the following example: @verbatim{ global X -global * +global * print(math.pi) -- Ok, 'print' and 'math' are read-only X = 1 -- Ok, declared as read-write Y = 1 -- Error, Y is read-only @@ -1700,7 +1704,7 @@ the scope of any other @Rw{global} declaration. Therefore, a program that does not use global declarations or start with @T{global *} has free read-write access to any global; -a program that starts with @T{global * } +a program that starts with @T{global *} has free read-only access to any global; and a program that starts with any other global declaration (e.g., @T{global none}) can only refer to declared variables. @@ -9620,11 +9624,11 @@ and @bnfNter{LiteralString}, see @See{lexical}.) @OrNL @Rw{global} @Rw{function} @bnfNter{Name} funcbody @OrNL @Rw{local} attnamelist @bnfopt{@bnfter{=} explist} @OrNL @Rw{global} attnamelist -@OrNL @Rw{global} @bnfter{*} @bnfopt{attrib} +@OrNL @Rw{global} @bnfopt{attrib} @bnfter{*} } @producname{attnamelist}@producbody{ - @bnfNter{Name} @bnfopt{attrib} + @bnfopt{attrib} @bnfNter{Name} @bnfopt{attrib} @bnfrep{@bnfter{,} @bnfNter{Name} @bnfopt{attrib}}} @producname{attrib}@producbody{@bnfter{<} @bnfNter{Name} @bnfter{>}} diff --git a/testes/all.lua b/testes/all.lua index 499c100d76..d3e2f12368 100755 --- a/testes/all.lua +++ b/testes/all.lua @@ -2,7 +2,7 @@ -- $Id: testes/all.lua $ -- See Copyright Notice in file lua.h -global * +global * global _soft, _port, _nomsg global T diff --git a/testes/calls.lua b/testes/calls.lua index 0ea1c4ab0d..214417014a 100644 --- a/testes/calls.lua +++ b/testes/calls.lua @@ -1,7 +1,7 @@ -- $Id: testes/calls.lua $ -- See Copyright Notice in file lua.h -global * +global * print("testing functions and calls") diff --git a/testes/closure.lua b/testes/closure.lua index c55d15838f..0c2e96c0f1 100644 --- a/testes/closure.lua +++ b/testes/closure.lua @@ -1,7 +1,7 @@ -- $Id: testes/closure.lua $ -- See Copyright Notice in file lua.h -global * +global * print "testing closures" diff --git a/testes/code.lua b/testes/code.lua index b6ceb34cb3..633f48969b 100644 --- a/testes/code.lua +++ b/testes/code.lua @@ -1,7 +1,7 @@ -- $Id: testes/code.lua $ -- See Copyright Notice in file lua.h -global * +global * if T==nil then (Message or print)('\n >>> testC not active: skipping opcode tests <<<\n') diff --git a/testes/files.lua b/testes/files.lua index c2b355fb8d..d4e327b71b 100644 --- a/testes/files.lua +++ b/testes/files.lua @@ -1,7 +1,7 @@ -- $Id: testes/files.lua $ -- See Copyright Notice in file lua.h -global * +global * local debug = require "debug" diff --git a/testes/goto.lua b/testes/goto.lua index 3f1f6e6949..44486e2029 100644 --- a/testes/goto.lua +++ b/testes/goto.lua @@ -1,9 +1,9 @@ -- $Id: testes/goto.lua $ -- See Copyright Notice in file lua.h -global require -global print, load, assert, string, setmetatable -global collectgarbage, error +global require +global print, load, assert, string, setmetatable +global collectgarbage, error print("testing goto and global declarations") @@ -304,7 +304,7 @@ do -- global variables cannot be to-be-closed checkerr("global X", "cannot be") - checkerr("global * ", "cannot be") + checkerr("global *", "cannot be") do local X = 10 @@ -345,7 +345,7 @@ do end checkerr([[ - global foo ; + global foo; function foo (x) return end -- ERROR: foo is read-only ]], "assign to const variable 'foo'") @@ -357,7 +357,7 @@ do ]], "%:2%:") -- correct line in error message checkerr([[ - global * ; + global *; print(X) -- Ok to use Y = 1 -- ERROR ]], "assign to const variable 'Y'") diff --git a/testes/literals.lua b/testes/literals.lua index fecdd6d3b9..336ef585c5 100644 --- a/testes/literals.lua +++ b/testes/literals.lua @@ -3,7 +3,7 @@ print('testing scanner') -global * +global * local debug = require "debug" diff --git a/testes/locals.lua b/testes/locals.lua index 99ff9edc5e..02f41980a8 100644 --- a/testes/locals.lua +++ b/testes/locals.lua @@ -1,7 +1,7 @@ -- $Id: testes/locals.lua $ -- See Copyright Notice in file lua.h -global * +global * print('testing local variables and environments') @@ -181,23 +181,25 @@ assert(x==20) A = nil -do -- constants +do print("testing local constants") global assert, load, string, X X = 1 -- not a constant local a, b, c = 10, 20, 30 b = a + c + b -- 'b' is not constant assert(a == 10 and b == 60 and c == 30) + local function checkro (name, code) local st, msg = load(code) local gab = string.format("attempt to assign to const variable '%s'", name) assert(not st and string.find(msg, gab)) end + checkro("y", "local x, y , z = 10, 20, 30; x = 11; y = 12") checkro("x", "local x , y, z = 10, 20, 30; x = 11") checkro("z", "local x , y, z = 10, 20, 30; y = 10; z = 11") - checkro("foo", "local foo = 10; function foo() end") - checkro("foo", "local foo = {}; function foo() end") - checkro("foo", "global foo ; function foo() end") + checkro("foo", "local foo = 10; function foo() end") + checkro("foo", "local foo = {}; function foo() end") + checkro("foo", "global foo ; function foo() end") checkro("XX", "global XX ; XX = 10") checkro("XX", "local _ENV; global XX ; XX = 10") @@ -218,8 +220,18 @@ do -- constants end + print"testing to-be-closed variables" + +do + local st, msg = load("local a, b") + assert(not st and string.find(msg, "multiple")) + + local st, msg = load("local a, b") + assert(not st and string.find(msg, "multiple")) +end + local function stack(n) n = ((n == 0) or stack(n - 1)) end local function func2close (f, x, y) diff --git a/testes/math.lua b/testes/math.lua index 242579b177..0d228d0988 100644 --- a/testes/math.lua +++ b/testes/math.lua @@ -8,11 +8,10 @@ local string = require "string" global none -global print, assert, pcall, type, pairs, load -global tonumber, tostring, select +global print, assert, pcall, type, pairs, load +global tonumber, tostring, select -local minint = math.mininteger -local maxint = math.maxinteger +local minint, maxint = math.mininteger, math.maxinteger local intbits = math.floor(math.log(maxint, 2) + 0.5) + 1 assert((1 << intbits) == 0) diff --git a/testes/nextvar.lua b/testes/nextvar.lua index e5a9717841..03810a8e41 100644 --- a/testes/nextvar.lua +++ b/testes/nextvar.lua @@ -1,7 +1,7 @@ -- $Id: testes/nextvar.lua $ -- See Copyright Notice in file lua.h -global * +global * print('testing tables, next, and for') diff --git a/testes/pm.lua b/testes/pm.lua index 1700ca2c23..720d2a3562 100644 --- a/testes/pm.lua +++ b/testes/pm.lua @@ -6,7 +6,7 @@ print('testing pattern matching') -global * +global * local function checkerror (msg, f, ...) local s, err = pcall(f, ...) diff --git a/testes/strings.lua b/testes/strings.lua index 455398c3f5..46912d4392 100644 --- a/testes/strings.lua +++ b/testes/strings.lua @@ -3,7 +3,7 @@ -- ISO Latin encoding -global * +global * print('testing strings and string library') diff --git a/testes/utf8.lua b/testes/utf8.lua index ec9b706ff7..143c6d3467 100644 --- a/testes/utf8.lua +++ b/testes/utf8.lua @@ -3,7 +3,7 @@ -- UTF-8 file -global * +global * print "testing UTF-8 library" From 6d53701c7a0dc4736d824fd891ee6f22265d0d68 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Sun, 18 May 2025 12:03:54 -0300 Subject: [PATCH 1051/1145] Proper error message when jumping into 'global *' A goto cannot jump into the scope of any variable declaration, including 'global *'. To report the error, it needs a "name" for the scope it is entering. --- lparser.c | 6 +++--- manual/manual.of | 2 +- testes/goto.lua | 13 ++++++++----- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/lparser.c b/lparser.c index bad3592ade..c134b7a89c 100644 --- a/lparser.c +++ b/lparser.c @@ -542,13 +542,13 @@ static void adjust_assign (LexState *ls, int nvars, int nexps, expdesc *e) { /* ** Generates an error that a goto jumps into the scope of some -** local variable. +** variable declaration. */ static l_noret jumpscopeerror (LexState *ls, Labeldesc *gt) { TString *tsname = getlocalvardesc(ls->fs, gt->nactvar)->vd.name; - const char *varname = getstr(tsname); + const char *varname = (tsname != NULL) ? getstr(tsname) : "*"; luaK_semerror(ls, - " at line %d jumps into the scope of local '%s'", + " at line %d jumps into the scope of '%s'", getstr(gt->name), gt->line, varname); /* raise the error */ } diff --git a/manual/manual.of b/manual/manual.of index eb97e853d1..a6361fa25b 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -1504,7 +1504,7 @@ labels in Lua are considered statements too: A label is visible in the entire block where it is defined, except inside nested functions. A goto can jump to any visible label as long as it does not -enter into the scope of a local variable. +enter into the scope of a variable declaration. A label should not be declared where a previous label with the same name is visible, even if this other label has been declared in an enclosing block. diff --git a/testes/goto.lua b/testes/goto.lua index 44486e2029..d773006102 100644 --- a/testes/goto.lua +++ b/testes/goto.lua @@ -23,15 +23,18 @@ errmsg([[ ::l1:: ::l1:: ]], "label 'l1'") errmsg([[ ::l1:: do ::l1:: end]], "label 'l1'") --- undefined label -errmsg([[ goto l1; local aa ::l1:: ::l2:: print(3) ]], "local 'aa'") --- jumping over variable definition +-- jumping over variable declaration +errmsg([[ goto l1; local aa ::l1:: ::l2:: print(3) ]], "scope of 'aa'") + +errmsg([[ goto l2; global *; ::l1:: ::l2:: print(3) ]], "scope of '*'") + errmsg([[ do local bb, cc; goto l1; end local aa ::l1:: print(3) -]], "local 'aa'") +]], "scope of 'aa'") + -- jumping into a block errmsg([[ do ::l1:: end goto l1 ]], "label 'l1'") @@ -44,7 +47,7 @@ errmsg([[ local xuxu = 10 ::cont:: until xuxu < x -]], "local 'xuxu'") +]], "scope of 'xuxu'") -- simple gotos local x From be05c444818989463dc307eed283503d391f93eb Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 20 May 2025 17:36:05 -0300 Subject: [PATCH 1052/1145] New way to control preambular declaration Validity of the preambular global declaration in controled together with all declarations, when checking variable names. --- lparser.c | 33 ++++++++++++++++++++------------- lparser.h | 3 +++ testes/db.lua | 6 +++++- testes/goto.lua | 18 +++++++++++++++++- 4 files changed, 45 insertions(+), 15 deletions(-) diff --git a/lparser.c b/lparser.c index c134b7a89c..e868e887da 100644 --- a/lparser.c +++ b/lparser.c @@ -54,7 +54,6 @@ typedef struct BlockCnt { lu_byte upval; /* true if some variable in the block is an upvalue */ lu_byte isloop; /* 1 if 'block' is a loop; 2 if it has pending breaks */ lu_byte insidetbc; /* true if inside the scope of a to-be-closed var. */ - lu_byte globdec; /* true if inside the scope of any global declaration */ } BlockCnt; @@ -399,22 +398,35 @@ static int newupvalue (FuncState *fs, TString *name, expdesc *v) { /* ** Look for an active variable with the name 'n' in the ** function 'fs'. If found, initialize 'var' with it and return -** its expression kind; otherwise return -1. +** its expression kind; otherwise return -1. While searching, +** var->u.info==-1 means that the preambular global declaration is +** active (the default while there is no other global declaration); +** var->u.info==-2 means there is no active collective declaration +** (some previous global declaration but no collective declaration); +** and var->u.info>=0 points to the inner-most (the first one found) +** collective declaration, if there is one. */ static int searchvar (FuncState *fs, TString *n, expdesc *var) { int i; for (i = cast_int(fs->nactvar) - 1; i >= 0; i--) { Vardesc *vd = getlocalvardesc(fs, i); - if (vd->vd.name == NULL) { /* 'global *'? */ - if (var->u.info == -1) { /* no previous collective declaration? */ - var->u.info = fs->firstlocal + i; /* will use this one as default */ + if (varglobal(vd)) { /* global declaration? */ + if (vd->vd.name == NULL) { /* collective declaration? */ + if (var->u.info < 0) /* no previous collective declaration? */ + var->u.info = fs->firstlocal + i; /* this is the first one */ + } + else { /* global name */ + if (eqstr(n, vd->vd.name)) { /* found? */ + init_exp(var, VGLOBAL, fs->firstlocal + i); + return VGLOBAL; + } + else if (var->u.info == -1) /* active preambular declaration? */ + var->u.info = -2; /* invalidate preambular declaration */ } } else if (eqstr(n, vd->vd.name)) { /* found? */ if (vd->vd.kind == RDKCTC) /* compile-time constant? */ init_exp(var, VCONST, fs->firstlocal + i); - else if (vd->vd.kind == GDKREG || vd->vd.kind == GDKCONST) - init_exp(var, VGLOBAL, fs->firstlocal + i); else /* local variable */ init_var(fs, var, i); return cast_int(var->k); @@ -486,7 +498,7 @@ static void buildvar (LexState *ls, TString *varname, expdesc *var) { expdesc key; int info = var->u.info; /* global by default in the scope of a global declaration? */ - if (info == -1 && fs->bl->globdec) + if (info == -2) luaK_semerror(ls, "variable '%s' not declared", getstr(varname)); singlevaraux(fs, ls->envn, var, 1); /* get environment variable */ if (var->k == VGLOBAL) @@ -692,10 +704,6 @@ static void enterblock (FuncState *fs, BlockCnt *bl, lu_byte isloop) { bl->upval = 0; /* inherit 'insidetbc' from enclosing block */ bl->insidetbc = (fs->bl != NULL && fs->bl->insidetbc); - /* inherit 'globdec' from enclosing block or enclosing function */ - bl->globdec = fs->bl != NULL ? fs->bl->globdec - : fs->prev != NULL ? fs->prev->bl->globdec - : 0; /* chunk's first block */ bl->previous = fs->bl; /* link block in function's block list */ fs->bl = bl; lua_assert(fs->freereg == luaY_nvarstack(fs)); @@ -1855,7 +1863,6 @@ static void globalfunc (LexState *ls, int line) { static void globalstatfunc (LexState *ls, int line) { /* stat -> GLOBAL globalfunc | GLOBAL globalstat */ luaX_next(ls); /* skip 'global' */ - ls->fs->bl->globdec = 1; /* in the scope of a global declaration */ if (testnext(ls, TK_FUNCTION)) globalfunc(ls, line); else diff --git a/lparser.h b/lparser.h index 524df6ea74..b08008ce62 100644 --- a/lparser.h +++ b/lparser.h @@ -105,6 +105,9 @@ typedef struct expdesc { /* variables that live in registers */ #define varinreg(v) ((v)->vd.kind <= RDKTOCLOSE) +/* test for global variables */ +#define varglobal(v) ((v)->vd.kind >= GDKREG) + /* description of an active variable */ typedef union Vardesc { diff --git a/testes/db.lua b/testes/db.lua index ae204c4176..0f174f17f7 100644 --- a/testes/db.lua +++ b/testes/db.lua @@ -349,9 +349,11 @@ end, "crl") function f(a,b) - global collectgarbage, assert, g, string + -- declare some globals to check that they don't interfere with 'getlocal' + global collectgarbage collectgarbage() local _, x = debug.getlocal(1, 1) + global assert, g, string local _, y = debug.getlocal(1, 2) assert(x == a and y == b) assert(debug.setlocal(2, 3, "pera") == "AA".."AA") @@ -387,7 +389,9 @@ function g (...) f(AAAA,B) assert(AAAA == "pera" and B == "manga") do + global * local B = 13 + global assert local x,y = debug.getlocal(1,5) assert(x == 'B' and y == 13) end diff --git a/testes/goto.lua b/testes/goto.lua index d773006102..7e40fc4faf 100644 --- a/testes/goto.lua +++ b/testes/goto.lua @@ -364,7 +364,23 @@ do print(X) -- Ok to use Y = 1 -- ERROR ]], "assign to const variable 'Y'") - + + checkerr([[ + global *; + Y = X -- Ok to use + global *; + Y = 1 -- ERROR + ]], "assign to const variable 'Y'") + + global * + Y = 10 + assert(_ENV.Y == 10) + global * + local x = Y + global * + Y = x + Y + assert(_ENV.Y == 20) + end print'OK' From c15543b9afa31ab5dc564511ae11acd808405e8f Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 20 May 2025 17:50:56 -0300 Subject: [PATCH 1053/1145] Bug: check for constructor overflow in [exp] fields The check for constructor overflow was considering only fields with explicit names, ignoring fields with syntax '[exp]=exp'. --- lopcodes.h | 6 +++--- lparser.c | 5 ++--- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/lopcodes.h b/lopcodes.h index e3ac9d0969..9787003846 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -254,7 +254,7 @@ OP_SETTABLE,/* A B C R[A][R[B]] := RK(C) */ OP_SETI,/* A B C R[A][B] := RK(C) */ OP_SETFIELD,/* A B C R[A][K[B]:shortstring] := RK(C) */ -OP_NEWTABLE,/* A B C k R[A] := {} */ +OP_NEWTABLE,/* A vB vC k R[A] := {} */ OP_SELF,/* A B C R[A+1] := R[B]; R[A] := R[B][K[C]:shortstring] */ @@ -378,9 +378,9 @@ OP_EXTRAARG/* Ax extra (larger) argument for previous opcode */ real C = EXTRAARG _ C (the bits of EXTRAARG concatenated with the bits of C). - (*) In OP_NEWTABLE, B is log2 of the hash size (which is always a + (*) In OP_NEWTABLE, vB is log2 of the hash size (which is always a power of 2) plus 1, or zero for size zero. If not k, the array size - is C. Otherwise, the array size is EXTRAARG _ C. + is vC. Otherwise, the array size is EXTRAARG _ vC. (*) For comparisons, k specifies what condition the test should accept (true or false). diff --git a/lparser.c b/lparser.c index e868e887da..992d45bdf3 100644 --- a/lparser.c +++ b/lparser.c @@ -904,12 +904,11 @@ static void recfield (LexState *ls, ConsControl *cc) { FuncState *fs = ls->fs; lu_byte reg = ls->fs->freereg; expdesc tab, key, val; - if (ls->t.token == TK_NAME) { - luaY_checklimit(fs, cc->nh, INT_MAX / 2, "items in a constructor"); + if (ls->t.token == TK_NAME) codename(ls, &key); - } else /* ls->t.token == '[' */ yindex(ls, &key); + luaY_checklimit(fs, cc->nh, INT_MAX / 2, "items in a constructor"); cc->nh++; checknext(ls, '='); tab = *cc->t; From 519c57d597625f010d1bbb3f91bac5d193111060 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 4 Jun 2025 09:54:31 -0300 Subject: [PATCH 1054/1145] Removed uneeded check in parser In a constructor, each field generates at least one opcode, and the number of opcodes is limited by INT_MAX. Therefore, the counters for number of fields cannot exceed this limit. (The current limit for items in the hash part of a table has a limit smaller than INT_MAX. However, as long as there are no overflows, the logic for table resizing will handle that limit.) --- lparser.c | 1 - 1 file changed, 1 deletion(-) diff --git a/lparser.c b/lparser.c index 992d45bdf3..9abaa374ba 100644 --- a/lparser.c +++ b/lparser.c @@ -908,7 +908,6 @@ static void recfield (LexState *ls, ConsControl *cc) { codename(ls, &key); else /* ls->t.token == '[' */ yindex(ls, &key); - luaY_checklimit(fs, cc->nh, INT_MAX / 2, "items in a constructor"); cc->nh++; checknext(ls, '='); tab = *cc->t; From d05fe48bfdd89956c0ebd115dca0fb115aa28dd6 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 4 Jun 2025 12:55:43 -0300 Subject: [PATCH 1055/1145] Loading a binary chunk should not break assertions Although the execution of a bad binary chunk can crash the interpreter, simply loading it should be safe. --- lundump.c | 2 +- manual/manual.of | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/lundump.c b/lundump.c index fccded7d79..b69ec336c3 100644 --- a/lundump.c +++ b/lundump.c @@ -234,7 +234,7 @@ static void loadConstants (LoadState *S, Proto *f) { f->source = NULL; break; } - default: lua_assert(0); + default: error(S, "invalid constant"); } } } diff --git a/manual/manual.of b/manual/manual.of index a6361fa25b..0d473eed4f 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -1402,6 +1402,9 @@ Chunks can also be precompiled into binary form; see the program @idx{luac} and the function @Lid{string.dump} for details. Programs in source and compiled forms are interchangeable; Lua automatically detects the file type and acts accordingly @seeF{load}. +Be aware that, unlike source code, +the execution of maliciously crafted +bytecode can crash the interpreter. } From fd897027f19288ce2cb0249cb8c1818e2f3f1c4c Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 12 Jun 2025 11:15:09 -0300 Subject: [PATCH 1056/1145] A coroutine can close itself A call to close itself will close all its to-be-closed variables and return to the resume that (re)started the coroutine. --- lcorolib.c | 13 ++++++++-- ldo.c | 10 +++++++ ldo.h | 1 + lstate.c | 2 ++ manual/manual.of | 36 ++++++++++++++++++------- testes/coroutine.lua | 62 +++++++++++++++++++++++++++++++++++++------- 6 files changed, 103 insertions(+), 21 deletions(-) diff --git a/lcorolib.c b/lcorolib.c index 3d95f8735a..5b9736f100 100644 --- a/lcorolib.c +++ b/lcorolib.c @@ -154,8 +154,13 @@ static int luaB_costatus (lua_State *L) { } +static lua_State *getoptco (lua_State *L) { + return (lua_isnone(L, 1) ? L : getco(L)); +} + + static int luaB_yieldable (lua_State *L) { - lua_State *co = lua_isnone(L, 1) ? L : getco(L); + lua_State *co = getoptco(L); lua_pushboolean(L, lua_isyieldable(co)); return 1; } @@ -169,7 +174,7 @@ static int luaB_corunning (lua_State *L) { static int luaB_close (lua_State *L) { - lua_State *co = getco(L); + lua_State *co = getoptco(L); int status = auxstatus(L, co); switch (status) { case COS_DEAD: case COS_YIELD: { @@ -184,6 +189,10 @@ static int luaB_close (lua_State *L) { return 2; } } + case COS_RUN: /* running coroutine? */ + lua_closethread(co, L); /* close itself */ + lua_assert(0); /* previous call does not return */ + return 0; default: /* normal or running coroutine */ return luaL_error(L, "cannot close a %s coroutine", statname[status]); } diff --git a/ldo.c b/ldo.c index 820b5a9ad0..776519dc9d 100644 --- a/ldo.c +++ b/ldo.c @@ -139,6 +139,16 @@ l_noret luaD_throw (lua_State *L, TStatus errcode) { } +l_noret luaD_throwbaselevel (lua_State *L, TStatus errcode) { + if (L->errorJmp) { + /* unroll error entries up to the first level */ + while (L->errorJmp->previous != NULL) + L->errorJmp = L->errorJmp->previous; + } + luaD_throw(L, errcode); +} + + TStatus luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) { l_uint32 oldnCcalls = L->nCcalls; struct lua_longjmp lj; diff --git a/ldo.h b/ldo.h index 465f4fb8d8..2d4ca8be46 100644 --- a/ldo.h +++ b/ldo.h @@ -91,6 +91,7 @@ LUAI_FUNC void luaD_shrinkstack (lua_State *L); LUAI_FUNC void luaD_inctop (lua_State *L); LUAI_FUNC l_noret luaD_throw (lua_State *L, TStatus errcode); +LUAI_FUNC l_noret luaD_throwbaselevel (lua_State *L, TStatus errcode); LUAI_FUNC TStatus luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud); #endif diff --git a/lstate.c b/lstate.c index 20ed838f42..70a11aaec6 100644 --- a/lstate.c +++ b/lstate.c @@ -326,6 +326,8 @@ LUA_API int lua_closethread (lua_State *L, lua_State *from) { lua_lock(L); L->nCcalls = (from) ? getCcalls(from) : 0; status = luaE_resetthread(L, L->status); + if (L == from) /* closing itself? */ + luaD_throwbaselevel(L, status); lua_unlock(L); return APIstatus(status); } diff --git a/manual/manual.of b/manual/manual.of index 0d473eed4f..7c504d976e 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -3267,17 +3267,25 @@ when called through this function. Resets a thread, cleaning its call stack and closing all pending to-be-closed variables. -Returns a status code: +The parameter @id{from} represents the coroutine that is resetting @id{L}. +If there is no such coroutine, +this parameter can be @id{NULL}. + +Unless @id{L} is equal to @id{from}, +the call returns a status code: @Lid{LUA_OK} for no errors in the thread (either the original error that stopped the thread or errors in closing methods), or an error status otherwise. In case of error, -leaves the error object on the top of the stack. +the error object is put on the top of the stack. -The parameter @id{from} represents the coroutine that is resetting @id{L}. -If there is no such coroutine, -this parameter can be @id{NULL}. +If @id{L} is equal to @id{from}, +it corresponds to a thread closing itself. +In that case, +the call does not return; +instead, the resume or the protected call +that (re)started the thread returns. } @@ -6939,18 +6947,26 @@ which come inside the table @defid{coroutine}. See @See{coroutine} for a general description of coroutines. -@LibEntry{coroutine.close (co)| +@LibEntry{coroutine.close ([co])| Closes coroutine @id{co}, that is, closes all its pending to-be-closed variables and puts the coroutine in a dead state. -The given coroutine must be dead or suspended. -In case of error +The default for @id{co} is the running coroutine. + +The given coroutine must be dead, suspended, +or be the running coroutine. +For the running coroutine, +this function does not return. +Instead, the resume that (re)started the coroutine returns. + +For other coroutines, +in case of error (either the original error that stopped the coroutine or errors in closing methods), -returns @false plus the error object; -otherwise returns @true. +this function returns @false plus the error object; +otherwise ir returns @true. } diff --git a/testes/coroutine.lua b/testes/coroutine.lua index 17f6cebaae..02536ee536 100644 --- a/testes/coroutine.lua +++ b/testes/coroutine.lua @@ -156,11 +156,6 @@ do st, msg = coroutine.close(co) assert(st and msg == nil) - - -- cannot close the running coroutine - local st, msg = pcall(coroutine.close, coroutine.running()) - assert(not st and string.find(msg, "running")) - local main = coroutine.running() -- cannot close a "normal" coroutine @@ -169,20 +164,19 @@ do assert(not st and string.find(msg, "normal")) end))() - -- cannot close a coroutine while closing it - do + do -- close a coroutine while closing it local co co = coroutine.create( function() local x = func2close(function() - coroutine.close(co) -- try to close it again + coroutine.close(co) -- close it again end) coroutine.yield(20) end) local st, msg = coroutine.resume(co) assert(st and msg == 20) st, msg = coroutine.close(co) - assert(not st and string.find(msg, "running coroutine")) + assert(st and msg == nil) end -- to-be-closed variables in coroutines @@ -289,6 +283,56 @@ do end +do print("coroutines closing itself") + global coroutine, string, os + global assert, error, pcall + + local X = nil + + local function new () + return coroutine.create(function (what) + + local var = func2close(function (t, err) + if what == "yield" then + coroutine.yield() + elseif what == "error" then + error(200) + else + X = "Ok" + return X + end + end) + + -- do an unprotected call so that coroutine becomes non-yieldable + string.gsub("a", "a", function () + assert(not coroutine.isyieldable()) + -- do protected calls while non-yieldable, to add recovery + -- entries (setjmp) to the stack + assert(pcall(pcall, function () + -- 'close' works even while non-yieldable + coroutine.close() -- close itself + os.exit(false) -- not reacheable + end)) + end) + end) + end + + local co = new() + local st, msg = coroutine.resume(co, "ret") + assert(st and msg == nil) + assert(X == "Ok") + + local co = new() + local st, msg = coroutine.resume(co, "error") + assert(not st and msg == 200) + + local co = new() + local st, msg = coroutine.resume(co, "yield") + assert(not st and string.find(msg, "attempt to yield")) + +end + + -- yielding across C boundaries local co = coroutine.wrap(function() From e657a48ea5698bbd9982d878eb65e6615ec94f7e Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 13 Jun 2025 14:08:38 -0300 Subject: [PATCH 1057/1145] The main thread cannot be closed No thread started with pcall (instead of resume) can be closed, because coroutine.close would not respect the expected number of results from the protected call. --- lcorolib.c | 3 +++ manual/manual.of | 4 ++-- testes/coroutine.lua | 5 +++++ 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/lcorolib.c b/lcorolib.c index 5b9736f100..23dd844156 100644 --- a/lcorolib.c +++ b/lcorolib.c @@ -190,6 +190,9 @@ static int luaB_close (lua_State *L) { } } case COS_RUN: /* running coroutine? */ + lua_geti(L, LUA_REGISTRYINDEX, LUA_RIDX_MAINTHREAD); /* get main */ + if (lua_tothread(L, -1) == co) + return luaL_error(L, "cannot close main thread"); lua_closethread(co, L); /* close itself */ lua_assert(0); /* previous call does not return */ return 0; diff --git a/manual/manual.of b/manual/manual.of index 7c504d976e..baa33d88a6 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -3284,8 +3284,8 @@ If @id{L} is equal to @id{from}, it corresponds to a thread closing itself. In that case, the call does not return; -instead, the resume or the protected call -that (re)started the thread returns. +instead, the resume that (re)started the thread returns. +The thread must be running inside a resume. } diff --git a/testes/coroutine.lua b/testes/coroutine.lua index 02536ee536..4881d96478 100644 --- a/testes/coroutine.lua +++ b/testes/coroutine.lua @@ -158,6 +158,11 @@ do local main = coroutine.running() + -- cannot close 'main' + local st, msg = pcall(coroutine.close, main); + assert(not st and string.find(msg, "main")) + + -- cannot close a "normal" coroutine ;(coroutine.wrap(function () local st, msg = pcall(coroutine.close, main) From 0cecf1ab6d76e6a7d200fb01bdd999b61835fe21 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 13 Jun 2025 14:14:50 -0300 Subject: [PATCH 1058/1145] Dump uses varints also for integer constants Unlike sizes, these constants can be negative, so it encodes those integers into unsigned integers in a way that keeps small numbers small. --- ldump.c | 27 ++++++++++++++++++--------- lundump.c | 21 ++++++++++++--------- testes/code.lua | 18 ++++++++++++++++++ 3 files changed, 48 insertions(+), 18 deletions(-) diff --git a/ldump.c b/ldump.c index 79bb1dc9a7..a75b20d247 100644 --- a/ldump.c +++ b/ldump.c @@ -31,7 +31,7 @@ typedef struct { int strip; int status; Table *h; /* table to track saved strings */ - lua_Integer nstr; /* counter for counting saved strings */ + lua_Unsigned nstr; /* counter for counting saved strings */ } DumpState; @@ -87,12 +87,12 @@ static void dumpByte (DumpState *D, int y) { ** size for 'dumpVarint' buffer: each byte can store up to 7 bits. ** (The "+6" rounds up the division.) */ -#define DIBS ((l_numbits(size_t) + 6) / 7) +#define DIBS ((l_numbits(lua_Unsigned) + 6) / 7) /* ** Dumps an unsigned integer using the MSB Varint encoding */ -static void dumpVarint (DumpState *D, size_t x) { +static void dumpVarint (DumpState *D, lua_Unsigned x) { lu_byte buff[DIBS]; unsigned n = 1; buff[DIBS - 1] = x & 0x7f; /* fill least-significant byte */ @@ -103,12 +103,13 @@ static void dumpVarint (DumpState *D, size_t x) { static void dumpSize (DumpState *D, size_t sz) { - dumpVarint(D, sz); + dumpVarint(D, cast(lua_Unsigned, sz)); } + static void dumpInt (DumpState *D, int x) { lua_assert(x >= 0); - dumpVarint(D, cast_sizet(x)); + dumpVarint(D, cast_uint(x)); } @@ -117,8 +118,16 @@ static void dumpNumber (DumpState *D, lua_Number x) { } +/* +** Signed integers are coded to keep small values small. (Coding -1 as +** 0xfff...fff would use too many bytes to save a quite common value.) +** A non-negative x is coded as 2x; a negative x is coded as -2x - 1. +** (0 => 0; -1 => 1; 1 => 2; -2 => 3; 2 => 4; ...) +*/ static void dumpInteger (DumpState *D, lua_Integer x) { - dumpVar(D, x); + lua_Unsigned cx = (x >= 0) ? 2u * l_castS2U(x) + : (2u * ~l_castS2U(x)) + 1; + dumpVarint(D, cx); } @@ -136,8 +145,8 @@ static void dumpString (DumpState *D, TString *ts) { TValue idx; int tag = luaH_getstr(D->h, ts, &idx); if (!tagisempty(tag)) { /* string already saved? */ - dumpSize(D, 1); /* reuse a saved string */ - dumpSize(D, cast_sizet(ivalue(&idx))); /* index of saved string */ + dumpVarint(D, 1); /* reuse a saved string */ + dumpVarint(D, l_castS2U(ivalue(&idx))); /* index of saved string */ } else { /* must write and save the string */ TValue key, value; /* to save the string in the hash */ @@ -147,7 +156,7 @@ static void dumpString (DumpState *D, TString *ts) { dumpVector(D, s, size + 1); /* include ending '\0' */ D->nstr++; /* one more saved string */ setsvalue(D->L, &key, ts); /* the string is the key */ - setivalue(&value, D->nstr); /* its index is the value */ + setivalue(&value, l_castU2S(D->nstr)); /* its index is the value */ luaH_set(D->L, D->h, &key, &value); /* h[ts] = nstr */ /* integer value does not need barrier */ } diff --git a/lundump.c b/lundump.c index b69ec336c3..10528987c0 100644 --- a/lundump.c +++ b/lundump.c @@ -37,7 +37,7 @@ typedef struct { const char *name; Table *h; /* list for string reuse */ size_t offset; /* current position relative to beginning of dump */ - lua_Integer nstr; /* number of strings in the list */ + lua_Unsigned nstr; /* number of strings in the list */ lu_byte fixed; /* dump is fixed in memory */ } LoadState; @@ -94,8 +94,8 @@ static lu_byte loadByte (LoadState *S) { } -static size_t loadVarint (LoadState *S, size_t limit) { - size_t x = 0; +static lua_Unsigned loadVarint (LoadState *S, lua_Unsigned limit) { + lua_Unsigned x = 0; int b; limit >>= 7; do { @@ -127,9 +127,12 @@ static lua_Number loadNumber (LoadState *S) { static lua_Integer loadInteger (LoadState *S) { - lua_Integer x; - loadVar(S, x); - return x; + lua_Unsigned cx = loadVarint(S, LUA_MAXUNSIGNED); + /* decode unsigned to signed */ + if ((cx & 1) != 0) + return l_castU2S(~(cx >> 1)); + else + return l_castU2S(cx >> 1); } @@ -149,9 +152,9 @@ static void loadString (LoadState *S, Proto *p, TString **sl) { return; } else if (size == 1) { /* previously saved string? */ - lua_Integer idx = cast_st2S(loadSize(S)); /* get its index */ + lua_Unsigned idx = loadVarint(S, LUA_MAXUNSIGNED); /* get its index */ TValue stv; - luaH_getint(S->h, idx, &stv); /* get its value */ + luaH_getint(S->h, l_castU2S(idx), &stv); /* get its value */ *sl = ts = tsvalue(&stv); luaC_objbarrier(L, p, ts); return; /* do not save it again */ @@ -175,7 +178,7 @@ static void loadString (LoadState *S, Proto *p, TString **sl) { /* add string to list of saved strings */ S->nstr++; setsvalue(L, &sv, ts); - luaH_setint(L, S->h, S->nstr, &sv); + luaH_setint(L, S->h, l_castU2S(S->nstr), &sv); luaC_objbarrierback(L, obj2gco(S->h), ts); } diff --git a/testes/code.lua b/testes/code.lua index 633f48969b..380ff70c1b 100644 --- a/testes/code.lua +++ b/testes/code.lua @@ -482,5 +482,23 @@ do -- basic check for SETLIST assert(count == 1) end + +do print("testing code for integer limits") + local function checkints (n) + local source = string.format( + "local a = {[true] = 0X%x}; return a[true]", n) + local f = assert(load(source)) + checkKlist(f, {n}) + assert(f() == n) + f = load(string.dump(f)) + assert(f() == n) + end + + checkints(math.maxinteger) + checkints(math.mininteger) + checkints(-1) + +end + print 'OK' From 8cd7ae7da06f54b97f95d6994d6bf47086e4e7eb Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 16 Jun 2025 15:50:12 -0300 Subject: [PATCH 1059/1145] Simpler code for 'traversetable' Check the mode in a separate function (getmode), instead of using comma expressions inside the 'if' condition. --- lgc.c | 39 ++++++++++++++++++++++++++------------- testes/gc.lua | 5 +++++ 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/lgc.c b/lgc.c index f0045dd6f2..e4cbcf0c0a 100644 --- a/lgc.c +++ b/lgc.c @@ -589,25 +589,38 @@ static void traversestrongtable (global_State *g, Table *h) { } -static l_mem traversetable (global_State *g, Table *h) { - const char *weakkey, *weakvalue; +/* +** (result & 1) iff weak values; (result & 2) iff weak keys. +*/ +static int getmode (global_State *g, Table *h) { const TValue *mode = gfasttm(g, h->metatable, TM_MODE); - TString *smode; + if (mode == NULL || !ttisshrstring(mode)) + return 0; /* ignore non-(short)string modes */ + else { + const char *smode = getshrstr(tsvalue(mode)); + const char *weakkey = strchr(smode, 'k'); + const char *weakvalue = strchr(smode, 'v'); + return ((weakkey != NULL) << 1) | (weakvalue != NULL); + } +} + + +static l_mem traversetable (global_State *g, Table *h) { markobjectN(g, h->metatable); - if (mode && ttisshrstring(mode) && /* is there a weak mode? */ - (cast_void(smode = tsvalue(mode)), - cast_void(weakkey = strchr(getshrstr(smode), 'k')), - cast_void(weakvalue = strchr(getshrstr(smode), 'v')), - (weakkey || weakvalue))) { /* is really weak? */ - if (!weakkey) /* strong keys? */ + switch (getmode(g, h)) { + case 0: /* not weak */ + traversestrongtable(g, h); + break; + case 1: /* weak values */ traverseweakvalue(g, h); - else if (!weakvalue) /* strong values? */ + break; + case 2: /* weak keys */ traverseephemeron(g, h, 0); - else /* all weak */ + break; + case 3: /* all weak */ linkgclist(h, g->allweak); /* nothing to traverse now */ + break; } - else /* not weak */ - traversestrongtable(g, h); return 1 + 2*sizenode(h) + h->asize; } diff --git a/testes/gc.lua b/testes/gc.lua index ca8aa1bc51..5d2b3085aa 100644 --- a/testes/gc.lua +++ b/testes/gc.lua @@ -288,6 +288,11 @@ x,y,z=nil collectgarbage() assert(next(a) == string.rep('$', 11)) +do -- invalid mode + local a = setmetatable({}, {__mode = 34}) + collectgarbage() +end + -- 'bug' in 5.1 a = {} From 9386e49a3173b68e8b5a7ba882c4c2faf557b61e Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 16 Jun 2025 16:29:32 -0300 Subject: [PATCH 1060/1145] New metatable in an all-weak table can fool the GC All-weak tables are not being revisited after being visited during propagation; if it gets a new metatable after that, the new metatable may not be marked. --- lgc.c | 7 +++++-- testes/gc.lua | 10 ++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/lgc.c b/lgc.c index e4cbcf0c0a..bbaa5ff7e1 100644 --- a/lgc.c +++ b/lgc.c @@ -617,8 +617,11 @@ static l_mem traversetable (global_State *g, Table *h) { case 2: /* weak keys */ traverseephemeron(g, h, 0); break; - case 3: /* all weak */ - linkgclist(h, g->allweak); /* nothing to traverse now */ + case 3: /* all weak; nothing to traverse */ + if (g->gcstate == GCSpropagate) + linkgclist(h, g->grayagain); /* must visit again its metatable */ + else + linkgclist(h, g->allweak); /* must clear collected entries */ break; } return 1 + 2*sizenode(h) + h->asize; diff --git a/testes/gc.lua b/testes/gc.lua index 5d2b3085aa..62713dac64 100644 --- a/testes/gc.lua +++ b/testes/gc.lua @@ -294,6 +294,16 @@ do -- invalid mode end +if T then -- bug since 5.3: all-weak tables are not being revisited + T.gcstate("propagate") + local t = setmetatable({}, {__mode = "kv"}) + T.gcstate("enteratomic") -- 't' was visited + setmetatable(t, {__mode = "kv"}) + T.gcstate("pause") -- its new metatable is not being visited + assert(getmetatable(t).__mode == "kv") +end + + -- 'bug' in 5.1 a = {} local t = {x = 10} From f71156744851701b5d5fabdda5061b31e53f8f14 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 17 Jun 2025 11:40:49 -0300 Subject: [PATCH 1061/1145] Check string indices when loading binary chunk Lua is not religious about that, but it tries to avoid crashes when loading binary chunks. --- lundump.c | 12 ++++++------ manual/manual.of | 12 +++++------- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/lundump.c b/lundump.c index 10528987c0..ade4038426 100644 --- a/lundump.c +++ b/lundump.c @@ -154,8 +154,9 @@ static void loadString (LoadState *S, Proto *p, TString **sl) { else if (size == 1) { /* previously saved string? */ lua_Unsigned idx = loadVarint(S, LUA_MAXUNSIGNED); /* get its index */ TValue stv; - luaH_getint(S->h, l_castU2S(idx), &stv); /* get its value */ - *sl = ts = tsvalue(&stv); + if (novariant(luaH_getint(S->h, l_castU2S(idx), &stv)) != LUA_TSTRING) + error(S, "invalid string index"); + *sl = ts = tsvalue(&stv); /* get its value */ luaC_objbarrier(L, p, ts); return; /* do not save it again */ } @@ -394,11 +395,10 @@ LClosure *luaU_undump (lua_State *L, ZIO *Z, const char *name, int fixed) { LoadState S; LClosure *cl; if (*name == '@' || *name == '=') - S.name = name + 1; + name = name + 1; else if (*name == LUA_SIGNATURE[0]) - S.name = "binary string"; - else - S.name = name; + name = "binary string"; + S.name = name; S.L = L; S.Z = Z; S.fixed = cast_byte(fixed); diff --git a/manual/manual.of b/manual/manual.of index baa33d88a6..5bab781b4a 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -1403,8 +1403,7 @@ see the program @idx{luac} and the function @Lid{string.dump} for details. Programs in source and compiled forms are interchangeable; Lua automatically detects the file type and acts accordingly @seeF{load}. Be aware that, unlike source code, -the execution of maliciously crafted -bytecode can crash the interpreter. +maliciously crafted binary chunks can crash the interpreter. } @@ -6694,11 +6693,10 @@ It may be the string @St{b} (only @x{binary chunk}s), or @St{bt} (both binary and text). The default is @St{bt}. -It is safe to load malformed binary chunks; -@id{load} signals an appropriate error. -However, -Lua does not check the consistency of the code inside binary chunks; -running maliciously crafted bytecode can crash the interpreter. +Lua does not check the consistency of binary chunks. +Maliciously crafted binary chunks can crash +the interpreter. +You can use the @id{mode} parameter to prevent loading binary chunks. } From 07b009c3712c062957593d0a4fa82e0fe9023024 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 18 Jun 2025 16:45:55 -0300 Subject: [PATCH 1062/1145] No need to limit variable declarations to 250 Only local variables, which use registers, need this low limit. --- lparser.c | 5 ++--- lparser.h | 4 ++-- testes/errors.lua | 2 +- testes/goto.lua | 38 +++++++++++++++++++++++++++++++++----- 4 files changed, 38 insertions(+), 11 deletions(-) diff --git a/lparser.c b/lparser.c index 9abaa374ba..201dbe8b6a 100644 --- a/lparser.c +++ b/lparser.c @@ -50,7 +50,7 @@ typedef struct BlockCnt { struct BlockCnt *previous; /* chain */ int firstlabel; /* index of first label in this block */ int firstgoto; /* index of first pending goto in this block */ - lu_byte nactvar; /* # active locals outside the block */ + short nactvar; /* number of active declarations at block entry */ lu_byte upval; /* true if some variable in the block is an upvalue */ lu_byte isloop; /* 1 if 'block' is a loop; 2 if it has pending breaks */ lu_byte insidetbc; /* true if inside the scope of a to-be-closed var. */ @@ -196,8 +196,6 @@ static int new_varkind (LexState *ls, TString *name, lu_byte kind) { FuncState *fs = ls->fs; Dyndata *dyd = ls->dyd; Vardesc *var; - luaY_checklimit(fs, dyd->actvar.n + 1 - fs->firstlocal, - MAXVARS, "local variables"); luaM_growvector(L, dyd->actvar.arr, dyd->actvar.n + 1, dyd->actvar.size, Vardesc, SHRT_MAX, "variable declarationss"); var = &dyd->actvar.arr[dyd->actvar.n++]; @@ -330,6 +328,7 @@ static void adjustlocalvars (LexState *ls, int nvars) { Vardesc *var = getlocalvardesc(fs, vidx); var->vd.ridx = cast_byte(reglevel++); var->vd.pidx = registerlocalvar(ls, fs, var->vd.name); + luaY_checklimit(fs, reglevel, MAXVARS, "local variables"); } } diff --git a/lparser.h b/lparser.h index b08008ce62..fdbb9b8a0b 100644 --- a/lparser.h +++ b/lparser.h @@ -128,7 +128,7 @@ typedef struct Labeldesc { TString *name; /* label identifier */ int pc; /* position in code */ int line; /* line where it appeared */ - lu_byte nactvar; /* number of active variables in that position */ + short nactvar; /* number of active variables in that position */ lu_byte close; /* true for goto that escapes upvalues */ } Labeldesc; @@ -173,7 +173,7 @@ typedef struct FuncState { int firstlocal; /* index of first local var (in Dyndata array) */ int firstlabel; /* index of first label (in 'dyd->label->arr') */ short ndebugvars; /* number of elements in 'f->locvars' */ - lu_byte nactvar; /* number of active local variables */ + short nactvar; /* number of active variable declarations */ lu_byte nups; /* number of upvalues */ lu_byte freereg; /* first free register */ lu_byte iwthabs; /* instructions issued since last absolute line info */ diff --git a/testes/errors.lua b/testes/errors.lua index a072891366..4230a35249 100644 --- a/testes/errors.lua +++ b/testes/errors.lua @@ -742,7 +742,7 @@ assert(c > 255 and string.find(b, "too many upvalues") and -- local variables s = "\nfunction foo ()\n local " -for j = 1,300 do +for j = 1,200 do s = s.."a"..j..", " end s = s.."b\n" diff --git a/testes/goto.lua b/testes/goto.lua index 7e40fc4faf..3519e75d65 100644 --- a/testes/goto.lua +++ b/testes/goto.lua @@ -293,14 +293,14 @@ end foo() -------------------------------------------------------------------------- +local function checkerr (code, err) + local st, msg = load(code) + assert(not st and string.find(msg, err)) +end + do global T - local function checkerr (code, err) - local st, msg = load(code) - assert(not st and string.find(msg, err)) - end - -- globals must be declared, after a global declaration checkerr("global none; X = 1", "variable 'X'") checkerr("global none; function XX() end", "variable 'XX'") @@ -383,5 +383,33 @@ do end + +do -- Ok to declare hundreds of globals + global table + local code = {} + for i = 1, 1000 do + code[#code + 1] = ";global x" .. i + end + code[#code + 1] = "; return x990" + code = table.concat(code) + _ENV.x990 = 11 + assert(load(code)() == 11) + _ENV.x990 = nil +end + +do -- mixing lots of global/local declarations + global table + local code = {} + for i = 1, 200 do + code[#code + 1] = ";global x" .. i + code[#code + 1] = ";local y" .. i .. "=" .. (2*i) + end + code[#code + 1] = "; return x200 + y200" + code = table.concat(code) + _ENV.x200 = 11 + assert(assert(load(code))() == 2*200 + 11) + _ENV.x200 = nil +end + print'OK' From 30531c291b25243e05a9734033a6a023c18f13ac Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 23 Jun 2025 14:00:21 -0300 Subject: [PATCH 1063/1145] Refactoring in the use of 'readline' by 'lua.c' More common code for 'readline' loaded statically or dynamically (or not loaded). --- lua.c | 71 ++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 38 insertions(+), 33 deletions(-) diff --git a/lua.c b/lua.c index b611cbcace..dfb98e3884 100644 --- a/lua.c +++ b/lua.c @@ -432,32 +432,30 @@ static int handle_luainit (lua_State *L) { /* -** lua_readline defines how to show a prompt and then read a line from -** the standard input. -** lua_saveline defines how to "save" a read line in a "history". -** lua_freeline defines how to free a line read by lua_readline. +** * lua_initreadline initializes the readline system. +** * lua_readline defines how to show a prompt and then read a line from +** the standard input. +** * lua_saveline defines how to "save" a read line in a "history". +** * lua_freeline defines how to free a line read by lua_readline. +** +** If lua_readline is defined, all of them should be defined. */ -#if defined(LUA_USE_READLINE) - -#include -#include -#define lua_initreadline(L) ((void)L, rl_readline_name="lua") -#define lua_readline(b,p) ((void)b, readline(p)) -#define lua_saveline(line) add_history(line) -#define lua_freeline(b) free(b) +#if !defined(lua_readline) /* { */ -#endif +/* Code to use the readline library, either statically or dynamically linked */ +/* pointer to 'readline' function (if any) */ +typedef char *(*l_readlineT) (const char *prompt); +static l_readlineT l_readline = NULL; -#if !defined(lua_readline) /* { */ +/* pointer to 'add_history' function (if any) */ +typedef void (*l_addhistT) (const char *string); +static l_addhistT l_addhist = NULL; -/* pointer to dynamically loaded 'readline' function (if any) */ -typedef char *(*l_readline_t) (const char *prompt); -static l_readline_t l_readline = NULL; static char *lua_readline (char *buff, const char *prompt) { - if (l_readline != NULL) /* is there a dynamic 'readline'? */ + if (l_readline != NULL) /* is there a 'readline'? */ return (*l_readline)(prompt); /* use it */ else { /* emulate 'readline' over 'buff' */ fputs(prompt, stdout); @@ -467,33 +465,38 @@ static char *lua_readline (char *buff, const char *prompt) { } -/* pointer to dynamically loaded 'add_history' function (if any) */ -typedef void (*l_addhist_t) (const char *string); -static l_addhist_t l_addhist = NULL; - static void lua_saveline (const char *line) { - if (l_addhist != NULL) /* is there a dynamic 'add_history'? */ + if (l_addhist != NULL) /* is there an 'add_history'? */ (*l_addhist)(line); /* use it */ /* else nothing to be done */ } static void lua_freeline (char *line) { - if (l_readline != NULL) /* is there a dynamic 'readline'? */ + if (l_readline != NULL) /* is there a 'readline'? */ free(line); /* free line created by it */ /* else 'lua_readline' used an automatic buffer; nothing to free */ } -#if !defined(LUA_USE_DLOPEN) || !defined(LUA_READLINELIB) +#if defined(LUA_USE_READLINE) /* { */ -#define lua_initreadline(L) ((void)L) +/* assume Lua will be linked with '-lreadline' */ +#include +#include + +static void lua_initreadline(lua_State *L) { + UNUSED(L); + rl_readline_name = "lua"; + l_readline = readline; + l_addhist = add_history; +} -#else /* { */ +#elif defined(LUA_USE_DLOPEN) && defined(LUA_READLINELIB) /* }{ */ +/* try to load 'readline' dynamically */ #include - static void lua_initreadline (lua_State *L) { void *lib = dlopen(LUA_READLINELIB, RTLD_NOW | RTLD_LOCAL); if (lib == NULL) @@ -502,14 +505,16 @@ static void lua_initreadline (lua_State *L) { const char **name = cast(const char**, dlsym(lib, "rl_readline_name")); if (name != NULL) *name = "Lua"; - l_readline = cast(l_readline_t, cast_func(dlsym(lib, "readline"))); - if (l_readline == NULL) - lua_warning(L, "unable to load 'readline'", 0); - else - l_addhist = cast(l_addhist_t, cast_func(dlsym(lib, "add_history"))); + l_readline = cast(l_readlineT, cast_func(dlsym(lib, "readline"))); + l_addhist = cast(l_addhistT, cast_func(dlsym(lib, "add_history"))); } } +#else /* }{ */ + +/* no readline; leave function pointers as NULL */ +#define lua_initreadline(L) cast(void, L) + #endif /* } */ #endif /* } */ From 270a58c0629dea1a376f05433e8df383756173a8 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 23 Jun 2025 14:36:32 -0300 Subject: [PATCH 1064/1145] Application name for 'readline' is "lua", not "Lua" --- lua.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua.c b/lua.c index dfb98e3884..3fe9b7da36 100644 --- a/lua.c +++ b/lua.c @@ -504,7 +504,7 @@ static void lua_initreadline (lua_State *L) { else { const char **name = cast(const char**, dlsym(lib, "rl_readline_name")); if (name != NULL) - *name = "Lua"; + *name = "lua"; l_readline = cast(l_readlineT, cast_func(dlsym(lib, "readline"))); l_addhist = cast(l_addhistT, cast_func(dlsym(lib, "add_history"))); } From f6c627af20e48ae96bd17f4392ca74ce0ae90f36 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 26 Jun 2025 11:45:42 -0300 Subject: [PATCH 1065/1145] Cast added to 'add_history' MacOS defines 'add_history' with a "wrong" type (it returns 'int' instead of 'void'). --- lua.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lua.c b/lua.c index 3fe9b7da36..a90bf29b50 100644 --- a/lua.c +++ b/lua.c @@ -488,8 +488,8 @@ static void lua_freeline (char *line) { static void lua_initreadline(lua_State *L) { UNUSED(L); rl_readline_name = "lua"; - l_readline = readline; - l_addhist = add_history; + l_readline = cast(l_readlineT, readline); + l_addhist = cast(l_addhistT, add_history); } #elif defined(LUA_USE_DLOPEN) && defined(LUA_READLINELIB) /* }{ */ From 1da89da62fac7515937fb0f583b97dd50fdd0cbe Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 27 Jun 2025 14:46:41 -0300 Subject: [PATCH 1066/1145] Manual updated to version 5.5 --- manual/2html | 6 +++--- manual/manual.of | 12 ++++++------ 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/manual/2html b/manual/2html index ac5ea04351..b7afd2a6e4 100755 --- a/manual/2html +++ b/manual/2html @@ -8,11 +8,11 @@ --------------------------------------------------------------- header = [[ - + -Lua 5.4 Reference Manual +Lua 5.5 Reference Manual @@ -23,7 +23,7 @@ header = [[

[Lua logo] -Lua 5.4 Reference Manual +Lua 5.5 Reference Manual

by Roberto Ierusalimschy, Luiz Henrique de Figueiredo, Waldemar Celes diff --git a/manual/manual.of b/manual/manual.of index 5bab781b4a..bcc8173b4f 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -6908,7 +6908,7 @@ and @St{userdata}. A global variable (not a function) that holds a string containing the running Lua version. -The current value of this variable is @St{Lua 5.4}. +The current value of this variable is @St{Lua 5.5}. } @@ -7154,7 +7154,7 @@ to search for a @N{C loader}. Lua initializes the @N{C path} @Lid{package.cpath} in the same way it initializes the Lua path @Lid{package.path}, -using the environment variable @defid{LUA_CPATH_5_4}, +using the environment variable @defid{LUA_CPATH_5_5}, or the environment variable @defid{LUA_CPATH}, or a default path defined in @id{luaconf.h}. @@ -7223,7 +7223,7 @@ A string with the path used by @Lid{require} to search for a Lua loader. At start-up, Lua initializes this variable with -the value of the environment variable @defid{LUA_PATH_5_4} or +the value of the environment variable @defid{LUA_PATH_5_5} or the environment variable @defid{LUA_PATH} or with a default path defined in @id{luaconf.h}, if those environment variables are not defined. @@ -7594,9 +7594,9 @@ x = string.gsub("4+5 = $return 4+5$", "%$(.-)%$", function (s) end) -- x="4+5 = 9" -local t = {name="lua", version="5.4"} +local t = {name="lua", version="5.5"} x = string.gsub("$name-$version.tar.gz", "%$(%w+)", t) --- x="lua-5.4.tar.gz" +-- x="lua-5.5.tar.gz" } } @@ -9332,7 +9332,7 @@ when the standard input (@id{stdin}) is a terminal, and as @T{lua -} otherwise. When called without the option @T{-E}, -the interpreter checks for an environment variable @defid{LUA_INIT_5_4} +the interpreter checks for an environment variable @defid{LUA_INIT_5_5} (or @defid{LUA_INIT} if the versioned name is not defined) before running any argument. If the variable content has the format @T{@At@rep{filename}}, From cfce6f4b20afe85ede2182b3df3ab2bfcdb0e692 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 27 Jun 2025 14:47:11 -0300 Subject: [PATCH 1067/1145] Warning in loslib.c (signed-unsigned comparison) --- loslib.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/loslib.c b/loslib.c index 4623ad5ecf..3f605028fe 100644 --- a/loslib.c +++ b/loslib.c @@ -273,7 +273,7 @@ static int getfield (lua_State *L, const char *key, int d, int delta) { static const char *checkoption (lua_State *L, const char *conv, - ptrdiff_t convlen, char *buff) { + size_t convlen, char *buff) { const char *option = LUA_STRFTIMEOPTIONS; unsigned oplen = 1; /* length of options being checked */ for (; *option != '\0' && oplen <= convlen; option += oplen) { @@ -333,7 +333,8 @@ static int os_date (lua_State *L) { size_t reslen; char *buff = luaL_prepbuffsize(&b, SIZETIMEFMT); s++; /* skip '%' */ - s = checkoption(L, s, se - s, cc + 1); /* copy specifier to 'cc' */ + /* copy specifier to 'cc' */ + s = checkoption(L, s, ct_diff2sz(se - s), cc + 1); reslen = strftime(buff, SIZETIMEFMT, cc, stm); luaL_addsize(&b, reslen); } From 59a1adf194efe43741c2bb2005d93d8320a19d14 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 1 Jul 2025 10:57:02 -0300 Subject: [PATCH 1068/1145] LUAI_MAXSTACK defined privately LUAI_MAXSTACK is limited to INT_MAX/2, so can use INT_MAX/2 to define pseudo-indices (LUA_REGISTRYINDEX) in 'lua.h'. A change in the maximum stack size does not need to change the Lua-C ABI. --- ldo.c | 14 ++++++++++++++ ltests.h | 1 - lua.h | 6 +++--- luaconf.h | 14 -------------- 4 files changed, 17 insertions(+), 18 deletions(-) diff --git a/ldo.c b/ldo.c index 776519dc9d..f232b588a9 100644 --- a/ldo.c +++ b/ldo.c @@ -174,6 +174,20 @@ TStatus luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) { #define STACKERRSPACE 200 +/* +** LUAI_MAXSTACK limits the size of the Lua stack. +** It must fit into INT_MAX/2. +*/ + +#if !defined(LUAI_MAXSTACK) +#if 1000000 < (INT_MAX / 2) +#define LUAI_MAXSTACK 1000000 +#else +#define LUAI_MAXSTACK (INT_MAX / 2u) +#endif +#endif + + /* maximum stack size that respects size_t */ #define MAXSTACK_BYSIZET ((MAX_SIZET / sizeof(StackValue)) - STACKERRSPACE) diff --git a/ltests.h b/ltests.h index 43f08162cd..d34e9d421d 100644 --- a/ltests.h +++ b/ltests.h @@ -155,7 +155,6 @@ LUA_API void *debug_realloc (void *ud, void *block, ** Reduce maximum stack size to make stack-overflow tests run faster. ** (But value is still large enough to overflow smaller integers.) */ -#undef LUAI_MAXSTACK #define LUAI_MAXSTACK 68000 diff --git a/lua.h b/lua.h index 95e0db321a..131a8fcb9f 100644 --- a/lua.h +++ b/lua.h @@ -37,10 +37,10 @@ /* ** Pseudo-indices -** (-LUAI_MAXSTACK is the minimum valid index; we keep some free empty -** space after that to help overflow detection) +** (The stack size is limited to INT_MAX/2; we keep some free empty +** space after that to help overflow detection.) */ -#define LUA_REGISTRYINDEX (-LUAI_MAXSTACK - 1000) +#define LUA_REGISTRYINDEX (-(INT_MAX/2 + 1000)) #define lua_upvalueindex(i) (LUA_REGISTRYINDEX - (i)) diff --git a/luaconf.h b/luaconf.h index 51e77547be..bc5fbe9f6f 100644 --- a/luaconf.h +++ b/luaconf.h @@ -763,20 +763,6 @@ ** ===================================================================== */ -/* -@@ LUAI_MAXSTACK limits the size of the Lua stack. -** CHANGE it if you need a different limit. This limit is arbitrary; -** its only purpose is to stop Lua from consuming unlimited stack -** space and to reserve some numbers for pseudo-indices. -** (It must fit into max(int)/2.) -*/ -#if 1000000 < (INT_MAX / 2) -#define LUAI_MAXSTACK 1000000 -#else -#define LUAI_MAXSTACK (INT_MAX / 2u) -#endif - - /* @@ LUA_EXTRASPACE defines the size of a raw memory area associated with ** a Lua state with very fast access. From 03bf7fdd4f3a588cd7ff0a8c51ed68c596d3d575 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 1 Jul 2025 16:07:03 -0300 Subject: [PATCH 1069/1145] Added missing casts from lua_Unsigned to size_t size_t can be smaller than lua_Usigned. --- lstrlib.c | 4 ++-- lundump.c | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lstrlib.c b/lstrlib.c index 306cd0bfeb..8056c6ff57 100644 --- a/lstrlib.c +++ b/lstrlib.c @@ -1811,8 +1811,8 @@ static int str_unpack (lua_State *L) { lua_Unsigned len = (lua_Unsigned)unpackint(L, data + pos, h.islittle, cast_int(size), 0); luaL_argcheck(L, len <= ld - pos - size, 2, "data string too short"); - lua_pushlstring(L, data + pos + size, len); - pos += len; /* skip string */ + lua_pushlstring(L, data + pos + size, cast_sizet(len)); + pos += cast_sizet(len); /* skip string */ break; } case Kzstr: { diff --git a/lundump.c b/lundump.c index ade4038426..76f0ddc11b 100644 --- a/lundump.c +++ b/lundump.c @@ -109,7 +109,7 @@ static lua_Unsigned loadVarint (LoadState *S, lua_Unsigned limit) { static size_t loadSize (LoadState *S) { - return loadVarint(S, MAX_SIZE); + return cast_sizet(loadVarint(S, MAX_SIZE)); } From 03d672a95cfd287855b373587f3975165eab9e02 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 7 Jul 2025 15:02:09 -0300 Subject: [PATCH 1070/1145] Details (comments) --- lapi.c | 2 +- lctype.c | 2 +- lobject.c | 2 +- luaconf.h | 6 +++--- lutf8lib.c | 4 ++-- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/lapi.c b/lapi.c index 769eba13b6..71405a2584 100644 --- a/lapi.c +++ b/lapi.c @@ -679,7 +679,7 @@ static int auxgetstr (lua_State *L, const TValue *t, const char *k) { /* ** The following function assumes that the registry cannot be a weak -** table, so that en mergency collection while using the global table +** table; so, an emergency collection while using the global table ** cannot collect it. */ static void getGlobalTable (lua_State *L, TValue *gt) { diff --git a/lctype.c b/lctype.c index 9542280942..b1a43e44b0 100644 --- a/lctype.c +++ b/lctype.c @@ -18,7 +18,7 @@ #if defined (LUA_UCID) /* accept UniCode IDentifiers? */ -/* consider all non-ascii codepoints to be alphabetic */ +/* consider all non-ASCII codepoints to be alphabetic */ #define NONA 0x01 #else #define NONA 0x00 /* default */ diff --git a/lobject.c b/lobject.c index 1c32ecf7a9..5c270b274b 100644 --- a/lobject.c +++ b/lobject.c @@ -385,7 +385,7 @@ size_t luaO_str2num (const char *s, TValue *o) { int luaO_utf8esc (char *buff, l_uint32 x) { int n = 1; /* number of bytes put in buffer (backwards) */ lua_assert(x <= 0x7FFFFFFFu); - if (x < 0x80) /* ascii? */ + if (x < 0x80) /* ASCII? */ buff[UTF8BUFFSZ - 1] = cast_char(x); else { /* need continuation bytes */ unsigned int mfb = 0x3f; /* maximum that fits in first byte */ diff --git a/luaconf.h b/luaconf.h index bc5fbe9f6f..0adc9c13f1 100644 --- a/luaconf.h +++ b/luaconf.h @@ -59,7 +59,7 @@ /* -** When Posix DLL ('LUA_USE_DLOPEN') is enabled, the Lua stand-alone +** When POSIX DLL ('LUA_USE_DLOPEN') is enabled, the Lua stand-alone ** application will try to dynamically link a 'readline' facility ** for its REPL. In that case, LUA_READLINELIB is the name of the ** library it will look for those facilities. If lua.c cannot open @@ -76,7 +76,7 @@ #if defined(LUA_USE_MACOSX) #define LUA_USE_POSIX -#define LUA_USE_DLOPEN /* MacOS does not need -ldl */ +#define LUA_USE_DLOPEN /* macOS does not need -ldl */ #define LUA_READLINELIB "libedit.dylib" #endif @@ -88,7 +88,7 @@ #if defined(LUA_USE_C89) && defined(LUA_USE_POSIX) -#error "Posix is not compatible with C89" +#error "POSIX is not compatible with C89" #endif diff --git a/lutf8lib.c b/lutf8lib.c index 4c9784e093..be01613514 100644 --- a/lutf8lib.c +++ b/lutf8lib.c @@ -47,7 +47,7 @@ static lua_Integer u_posrelat (lua_Integer pos, size_t len) { ** Decode one UTF-8 sequence, returning NULL if byte sequence is ** invalid. The array 'limits' stores the minimum value for each ** sequence length, to check for overlong representations. Its first -** entry forces an error for non-ascii bytes with no continuation +** entry forces an error for non-ASCII bytes with no continuation ** bytes (count == 0). */ static const char *utf8_decode (const char *s, l_uint32 *val, int strict) { @@ -55,7 +55,7 @@ static const char *utf8_decode (const char *s, l_uint32 *val, int strict) { {~(l_uint32)0, 0x80, 0x800, 0x10000u, 0x200000u, 0x4000000u}; unsigned int c = (unsigned char)s[0]; l_uint32 res = 0; /* final result */ - if (c < 0x80) /* ascii? */ + if (c < 0x80) /* ASCII? */ res = c; else { int count = 0; /* to count number of continuation bytes */ From 848568790826b7e201f84682185b5b605c473016 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 7 Jul 2025 15:03:45 -0300 Subject: [PATCH 1071/1145] Correction in definition of CIST_FRESH The cast must be made before the shift. If int has 16 bits, the shift would zero the value and the cast would cast 0 to 0. --- lstate.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lstate.h b/lstate.h index f841c2321e..80df3b0a95 100644 --- a/lstate.h +++ b/lstate.h @@ -85,7 +85,7 @@ typedef struct CallInfo CallInfo; ** they must be visited again at the end of the cycle), but they are ** marked black because assignments to them must activate barriers (to ** move them back to TOUCHED1). -** - Open upvales are kept gray to avoid barriers, but they stay out +** - Open upvalues are kept gray to avoid barriers, but they stay out ** of gray lists. (They don't even have a 'gclist' field.) */ @@ -232,7 +232,7 @@ struct CallInfo { /* call is running a C function (still in first 16 bits) */ #define CIST_C (1u << (CIST_RECST + 3)) /* call is on a fresh "luaV_execute" frame */ -#define CIST_FRESH cast(l_uint32, CIST_C << 1) +#define CIST_FRESH (cast(l_uint32, CIST_C) << 1) /* function is closing tbc variables */ #define CIST_CLSRET (CIST_FRESH << 1) /* function has tbc variables to close */ From 942c10a5e33811a08a290ec15031c950a6d17c99 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 8 Jul 2025 13:33:57 -0300 Subject: [PATCH 1072/1145] Optional initialization for global declarations --- lparser.c | 81 +++++++++++++++++++++++++++++++------------ manual/manual.of | 18 +++++----- testes/bwcoercion.lua | 2 +- testes/calls.lua | 4 +-- testes/files.lua | 4 +-- testes/goto.lua | 23 +++++++++++- testes/tracegc.lua | 2 +- 7 files changed, 96 insertions(+), 38 deletions(-) diff --git a/lparser.c b/lparser.c index 201dbe8b6a..dde0b6d5e0 100644 --- a/lparser.c +++ b/lparser.c @@ -30,7 +30,7 @@ -/* maximum number of variable declarationss per function (must be +/* maximum number of variable declarations per function (must be smaller than 250, due to the bytecode format) */ #define MAXVARS 200 @@ -197,7 +197,7 @@ static int new_varkind (LexState *ls, TString *name, lu_byte kind) { Dyndata *dyd = ls->dyd; Vardesc *var; luaM_growvector(L, dyd->actvar.arr, dyd->actvar.n + 1, - dyd->actvar.size, Vardesc, SHRT_MAX, "variable declarationss"); + dyd->actvar.size, Vardesc, SHRT_MAX, "variable declarations"); var = &dyd->actvar.arr[dyd->actvar.n++]; var->vd.kind = kind; /* default */ var->vd.name = name; @@ -485,6 +485,20 @@ static void singlevaraux (FuncState *fs, TString *n, expdesc *var, int base) { } +static void buildglobal (LexState *ls, TString *varname, expdesc *var) { + FuncState *fs = ls->fs; + expdesc key; + init_exp(var, VGLOBAL, -1); /* global by default */ + singlevaraux(fs, ls->envn, var, 1); /* get environment variable */ + if (var->k == VGLOBAL) + luaK_semerror(ls, "_ENV is global when accessing variable '%s'", + getstr(varname)); + luaK_exp2anyregup(fs, var); /* _ENV could be a constant */ + codestring(&key, varname); /* key is variable name */ + luaK_indexed(fs, var, &key); /* 'var' represents _ENV[varname] */ +} + + /* ** Find a variable with the given name 'n', handling global variables ** too. @@ -494,18 +508,11 @@ static void buildvar (LexState *ls, TString *varname, expdesc *var) { init_exp(var, VGLOBAL, -1); /* global by default */ singlevaraux(fs, varname, var, 1); if (var->k == VGLOBAL) { /* global name? */ - expdesc key; int info = var->u.info; /* global by default in the scope of a global declaration? */ if (info == -2) luaK_semerror(ls, "variable '%s' not declared", getstr(varname)); - singlevaraux(fs, ls->envn, var, 1); /* get environment variable */ - if (var->k == VGLOBAL) - luaK_semerror(ls, "_ENV is global when accessing variable '%s'", - getstr(varname)); - luaK_exp2anyregup(fs, var); /* but could be a constant */ - codestring(&key, varname); /* key is variable name */ - luaK_indexed(fs, var, &key); /* env[varname] */ + buildglobal(ls, varname, var); if (info != -1 && ls->dyd->actvar.arr[info].vd.kind == GDKCONST) var->u.ind.ro = 1; /* mark variable as read-only */ else /* anyway must be a global */ @@ -665,7 +672,7 @@ static void createlabel (LexState *ls, TString *name, int line, int last) { /* -** Traverse the pending goto's of the finishing block checking whether +** Traverse the pending gotos of the finishing block checking whether ** each match some label of that block. Those that do not match are ** "exported" to the outer block, to be solved there. In particular, ** its 'nactvar' is updated with the level of the inner block, @@ -1435,6 +1442,15 @@ static void check_conflict (LexState *ls, struct LHS_assign *lh, expdesc *v) { } } + +/* Create code to store the "top" register in 'var' */ +static void storevartop (FuncState *fs, expdesc *var) { + expdesc e; + init_exp(&e, VNONRELOC, fs->freereg - 1); + luaK_storevar(fs, var, &e); /* will also free the top register */ +} + + /* ** Parse and compile a multiple assignment. The first "variable" ** (a 'suffixedexp') was already read by the caller. @@ -1468,8 +1484,7 @@ static void restassign (LexState *ls, struct LHS_assign *lh, int nvars) { return; /* avoid default */ } } - init_exp(&e, VNONRELOC, ls->fs->freereg-1); /* default assignment */ - luaK_storevar(ls->fs, &lh->v, &e); + storevartop(ls->fs, &lh->v); /* default assignment */ } @@ -1821,25 +1836,45 @@ static lu_byte getglobalattribute (LexState *ls, lu_byte df) { } +static void globalnames (LexState *ls, lu_byte defkind) { + FuncState *fs = ls->fs; + int nvars = 0; + int lastidx; /* index of last registered variable */ + do { /* for each name */ + TString *vname = str_checkname(ls); + lu_byte kind = getglobalattribute(ls, defkind); + lastidx = new_varkind(ls, vname, kind); + nvars++; + } while (testnext(ls, ',')); + if (testnext(ls, '=')) { /* initialization? */ + expdesc e; + int i; + int nexps = explist(ls, &e); /* read list of expressions */ + adjust_assign(ls, nvars, nexps, &e); + for (i = 0; i < nvars; i++) { /* for each variable */ + expdesc var; + TString *varname = getlocalvardesc(fs, lastidx - i)->vd.name; + buildglobal(ls, varname, &var); /* create global variable in 'var' */ + storevartop(fs, &var); + } + } + fs->nactvar = cast_short(fs->nactvar + nvars); /* activate declaration */ +} + + static void globalstat (LexState *ls) { /* globalstat -> (GLOBAL) attrib '*' globalstat -> (GLOBAL) attrib NAME attrib {',' NAME attrib} */ FuncState *fs = ls->fs; /* get prefixed attribute (if any); default is regular global variable */ lu_byte defkind = getglobalattribute(ls, GDKREG); - if (testnext(ls, '*')) { + if (!testnext(ls, '*')) + globalnames(ls, defkind); + else { /* use NULL as name to represent '*' entries */ new_varkind(ls, NULL, defkind); fs->nactvar++; /* activate declaration */ } - else { - do { /* list of names */ - TString *vname = str_checkname(ls); - lu_byte kind = getglobalattribute(ls, defkind); - new_varkind(ls, vname, kind); - fs->nactvar++; /* activate declaration */ - } while (testnext(ls, ',')); - } } @@ -1850,7 +1885,7 @@ static void globalfunc (LexState *ls, int line) { TString *fname = str_checkname(ls); new_varkind(ls, fname, GDKREG); /* declare global variable */ fs->nactvar++; /* enter its scope */ - buildvar(ls, fname, &var); + buildglobal(ls, fname, &var); body(ls, &b, 0, ls->linenumber); /* compile and return closure in 'b' */ luaK_storevar(fs, &var, &b); luaK_fixline(fs, line); /* definition "happens" in the first line */ diff --git a/manual/manual.of b/manual/manual.of index bcc8173b4f..8f90f942ee 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -229,7 +229,7 @@ as the following example illustrates: @verbatim{ X = 1 -- Ok, global by default do - global Y -- voids implicit initial declaration + global Y -- voids the implicit initial declaration Y = 1 -- Ok, Y declared as global X = 1 -- ERROR, X not declared end @@ -269,7 +269,7 @@ print(x) --> 10 (the global one) Notice that, in a declaration like @T{local x = x}, the new @id{x} being declared is not in scope yet, -and so the @id{x} in the left-hand side refers to the outside variable. +and so the @id{x} in the right-hand side refers to the outside variable. Because of the @x{lexical scoping} rules, local variables can be freely accessed by functions @@ -1651,11 +1651,12 @@ Function calls are explained in @See{functioncall}. @sect3{localvar| @title{Variable Declarations} Local and global variables can be declared anywhere inside a block. -The declaration for locals can include an initialization: +The declaration can include an initialization: @Produc{ @producname{stat}@producbody{@Rw{local} attnamelist @bnfopt{@bnfter{=} explist}} -@producname{stat}@producbody{@Rw{global} attnamelist} +@producname{stat}@producbody{@Rw{global} + attnamelist @bnfopt{@bnfter{=} explist}} } If present, an initial assignment has the same semantics of a multiple assignment @see{assignment}. @@ -1712,7 +1713,8 @@ and a program that starts with any other global declaration (e.g., @T{global none}) can only refer to declared variables. Note that, for global variables, -the effect of any declaration is only syntactical: +the effect of any declaration is only syntactical +(except for the optional assignment): @verbatim{ global X , _G X = 1 -- ERROR @@ -3924,8 +3926,8 @@ This macro may evaluate its arguments more than once. } -@APIEntry{unsigned (lua_numbertocstring) (lua_State *L, int idx, - char *buff);| +@APIEntry{unsigned lua_numbertocstring (lua_State *L, int idx, + char *buff);| @apii{0,0,-} Converts the number at acceptable index @id{idx} to a string @@ -4050,7 +4052,7 @@ This function is equivalent to @Lid{lua_pushcclosure} with no upvalues. } -@APIEntry{const char *(lua_pushexternalstring) (lua_State *L, +@APIEntry{const char *lua_pushexternalstring (lua_State *L, const char *s, size_t len, lua_Alloc falloc, void *ud);| @apii{0,1,m} diff --git a/testes/bwcoercion.lua b/testes/bwcoercion.lua index cd735ab0b6..0544944d84 100644 --- a/testes/bwcoercion.lua +++ b/testes/bwcoercion.lua @@ -4,7 +4,7 @@ local strsub = string.sub local print = print -_ENV = nil +global none -- Try to convert a value to an integer, without assuming any coercion. local function toint (x) diff --git a/testes/calls.lua b/testes/calls.lua index 214417014a..0dacb85ab7 100644 --- a/testes/calls.lua +++ b/testes/calls.lua @@ -24,7 +24,7 @@ assert(not pcall(type)) -- testing local-function recursion -global fact; fact = false +global fact = false do local res = 1 local function fact (n) @@ -65,7 +65,7 @@ a.b.c:f2('k', 12); assert(a.b.c.k == 12) print('+') -global t; t = nil -- 'declare' t +global t = nil -- 'declare' t function f(a,b,c) local d = 'a'; t={a,b,c,d} end f( -- this line change must be valid diff --git a/testes/files.lua b/testes/files.lua index d4e327b71b..7146ac7ca2 100644 --- a/testes/files.lua +++ b/testes/files.lua @@ -715,7 +715,7 @@ do end -if T and T.nonblock then +if T and T.nonblock and not _port then print("testing failed write") -- unable to write anything to /dev/full @@ -840,7 +840,7 @@ assert(os.date("!\0\0") == "\0\0") local x = string.rep("a", 10000) assert(os.date(x) == x) local t = os.time() -global D; D = os.date("*t", t) +global D = os.date("*t", t) assert(os.date(string.rep("%d", 1000), t) == string.rep(os.date("%d", t), 1000)) assert(os.date(string.rep("%", 200)) == string.rep("%", 100)) diff --git a/testes/goto.lua b/testes/goto.lua index 3519e75d65..3310314d8a 100644 --- a/testes/goto.lua +++ b/testes/goto.lua @@ -380,7 +380,7 @@ do global * Y = x + Y assert(_ENV.Y == 20) - + Y = nil end @@ -411,5 +411,26 @@ do -- mixing lots of global/local declarations _ENV.x200 = nil end +do print "testing initialization in global declarations" + global a, b, c = 10, 20, 30 + assert(_ENV.a == 10 and b == 20 and c == 30) + + global a, b, c = 10 + assert(_ENV.a == 10 and b == nil and c == nil) + + global table + global a, b, c, d = table.unpack{1, 2, 3, 6, 5} + assert(_ENV.a == 1 and b == 2 and c == 3 and d == 6) + + local a, b = 100, 200 + do + global a, b = a, b + end + assert(_ENV.a == 100 and _ENV.b == 200) + + + _ENV.a, _ENV.b, _ENV.c, _ENV.d = nil -- erase these globals +end + print'OK' diff --git a/testes/tracegc.lua b/testes/tracegc.lua index 9c5c1b3f51..a8c929dffd 100644 --- a/testes/tracegc.lua +++ b/testes/tracegc.lua @@ -6,7 +6,7 @@ local M = {} local setmetatable, stderr, collectgarbage = setmetatable, io.stderr, collectgarbage -_ENV = nil +global none local active = false From f65d1f9e02d891733d4ff1cf8d4bc91291e0098e Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 8 Jul 2025 15:40:59 -0300 Subject: [PATCH 1073/1145] lua option '--' may not be followed by script --- lua.c | 3 ++- testes/main.lua | 9 +++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/lua.c b/lua.c index a90bf29b50..66fb74b7b5 100644 --- a/lua.c +++ b/lua.c @@ -303,7 +303,8 @@ static int collectargs (char **argv, int *first) { case '-': /* '--' */ if (argv[i][2] != '\0') /* extra characters after '--'? */ return has_error; /* invalid option */ - *first = i + 1; + /* if there is a script name, it comes after '--' */ + *first = (argv[i + 1] != NULL) ? i + 1 : 0; return args; case '\0': /* '-' */ return args; /* script "name" is '-' */ diff --git a/testes/main.lua b/testes/main.lua index eb63d58859..dc48dc485f 100644 --- a/testes/main.lua +++ b/testes/main.lua @@ -90,7 +90,7 @@ prepfile[[ 1, a ) ]] -RUN('lua - < %s > %s', prog, out) +RUN('lua - -- < %s > %s', prog, out) checkout("1\tnil\n") RUN('echo "print(10)\nprint(2)\n" | lua > %s', out) @@ -133,7 +133,7 @@ checkout("-h\n") prepfile("print(package.path)") -- test LUA_PATH -RUN('env LUA_INIT= LUA_PATH=x lua %s > %s', prog, out) +RUN('env LUA_INIT= LUA_PATH=x lua -- %s > %s', prog, out) checkout("x\n") -- test LUA_PATH_version @@ -358,7 +358,7 @@ RUN([[lua -e"_PROMPT='' _PROMPT2=''" -i < %s > %s]], prog, out) checkprogout("6\n10\n10\n\n") prepfile("a = [[b\nc\nd\ne]]\na") -RUN([[lua -e"_PROMPT='' _PROMPT2=''" -i < %s > %s]], prog, out) +RUN([[lua -e"_PROMPT='' _PROMPT2=''" -i -- < %s > %s]], prog, out) checkprogout("b\nc\nd\ne\n\n") -- input interrupted in continuation line @@ -488,12 +488,13 @@ assert(not os.remove(out)) -- invalid options NoRun("unrecognized option '-h'", "lua -h") NoRun("unrecognized option '---'", "lua ---") -NoRun("unrecognized option '-Ex'", "lua -Ex") +NoRun("unrecognized option '-Ex'", "lua -Ex --") NoRun("unrecognized option '-vv'", "lua -vv") NoRun("unrecognized option '-iv'", "lua -iv") NoRun("'-e' needs argument", "lua -e") NoRun("syntax error", "lua -e a") NoRun("'-l' needs argument", "lua -l") +NoRun("-i", "lua -- -i") -- handles -i as a script name if T then -- test library? From 85a3c1699c9587a9e952b4ab75b1c6c310ebebea Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 9 Jul 2025 14:40:36 -0300 Subject: [PATCH 1074/1145] New method to unload DLLs External strings created by DLLs may need the DLL code to be deallocated. This implies that a DLL can only be unloaded after all its strings were deallocated, which happen only after the run of all finalizers. To ensure that order, we create a 'library string' to represent each DLL and keep it locked. When this string is deallocated (after the deallocation of any string created by the DLL) it closes its corresponding DLL. --- loadlib.c | 75 ++++++++++++++++++++++++--------------------- testes/attrib.lua | 8 ++++- testes/libs/lib22.c | 51 ++++++++++++++++++++++++++++++ 3 files changed, 98 insertions(+), 36 deletions(-) diff --git a/loadlib.c b/loadlib.c index 5f0c170296..2cd95ca308 100644 --- a/loadlib.c +++ b/loadlib.c @@ -306,6 +306,16 @@ static void setpath (lua_State *L, const char *fieldname, /* }================================================================== */ +/* +** External strings created by DLLs may need the DLL code to be +** deallocated. This implies that a DLL can only be unloaded after all +** its strings were deallocated. To ensure that, we create a 'library +** string' to represent each DLL, and when this string is deallocated +** it closes its corresponding DLL. +** (The string itself is irrelevant; its userdata is the DLL pointer.) +*/ + + /* ** return registry.CLIBS[path] */ @@ -320,34 +330,41 @@ static void *checkclib (lua_State *L, const char *path) { /* -** registry.CLIBS[path] = plib -- for queries -** registry.CLIBS[#CLIBS + 1] = plib -- also keep a list of all libraries +** Deallocate function for library strings. +** Unload the DLL associated with the string being deallocated. */ -static void addtoclib (lua_State *L, const char *path, void *plib) { - lua_getfield(L, LUA_REGISTRYINDEX, CLIBS); - lua_pushlightuserdata(L, plib); - lua_pushvalue(L, -1); - lua_setfield(L, -3, path); /* CLIBS[path] = plib */ - lua_rawseti(L, -2, luaL_len(L, -2) + 1); /* CLIBS[#CLIBS + 1] = plib */ - lua_pop(L, 1); /* pop CLIBS table */ +static void *freelib (void *ud, void *ptr, size_t osize, size_t nsize) { + /* string itself is irrelevant and static */ + (void)ptr; (void)osize; (void)nsize; + lsys_unloadlib(ud); /* unload library represented by the string */ + return NULL; } /* -** __gc tag method for CLIBS table: calls 'lsys_unloadlib' for all lib -** handles in list CLIBS +** Create a library string that, when deallocated, will unload 'plib' */ -static int gctm (lua_State *L) { - lua_Integer n = luaL_len(L, 1); - for (; n >= 1; n--) { /* for each handle, in reverse order */ - lua_rawgeti(L, 1, n); /* get handle CLIBS[n] */ - lsys_unloadlib(lua_touserdata(L, -1)); - lua_pop(L, 1); /* pop handle */ - } - return 0; +static void createlibstr (lua_State *L, void *plib) { + static const char dummy[] = /* common long body for all library strings */ + "01234567890123456789012345678901234567890123456789"; + lua_pushexternalstring(L, dummy, sizeof(dummy) - 1, freelib, plib); } +/* +** registry.CLIBS[path] = plib -- for queries. +** Also create a reference to strlib, so that the library string will +** only be collected when registry.CLIBS is collected. +*/ +static void addtoclib (lua_State *L, const char *path, void *plib) { + lua_getfield(L, LUA_REGISTRYINDEX, CLIBS); + lua_pushlightuserdata(L, plib); + lua_setfield(L, -2, path); /* CLIBS[path] = plib */ + createlibstr(L, plib); + luaL_ref(L, -2); /* keep library string in CLIBS */ + lua_pop(L, 1); /* pop CLIBS table */ +} + /* error codes for 'lookforfunc' */ #define ERRLIB 1 @@ -361,8 +378,8 @@ static int gctm (lua_State *L) { ** Then, if 'sym' is '*', return true (as library has been loaded). ** Otherwise, look for symbol 'sym' in the library and push a ** C function with that symbol. -** Return 0 and 'true' or a function in the stack; in case of -** errors, return an error code and an error message in the stack. +** Return 0 with 'true' or a function in the stack; in case of +** errors, return an error code with an error message in the stack. */ static int lookforfunc (lua_State *L, const char *path, const char *sym) { void *reg = checkclib(L, path); /* check loaded C libraries */ @@ -704,21 +721,9 @@ static void createsearcherstable (lua_State *L) { } -/* -** create table CLIBS to keep track of loaded C libraries, -** setting a finalizer to close all libraries when closing state. -*/ -static void createclibstable (lua_State *L) { - luaL_getsubtable(L, LUA_REGISTRYINDEX, CLIBS); /* create CLIBS table */ - lua_createtable(L, 0, 1); /* create metatable for CLIBS */ - lua_pushcfunction(L, gctm); - lua_setfield(L, -2, "__gc"); /* set finalizer for CLIBS table */ - lua_setmetatable(L, -2); -} - - LUAMOD_API int luaopen_package (lua_State *L) { - createclibstable(L); + luaL_getsubtable(L, LUA_REGISTRYINDEX, CLIBS); /* create CLIBS table */ + lua_pop(L, 1); /* will not use it now */ luaL_newlib(L, pk_funcs); /* create 'package' table */ createsearcherstable(L); /* set paths */ diff --git a/testes/attrib.lua b/testes/attrib.lua index d8b6e0f3f2..8a3462ea9d 100644 --- a/testes/attrib.lua +++ b/testes/attrib.lua @@ -300,6 +300,12 @@ else assert(_ENV.x == "lib2-v2" and _ENV.y == DC"lib2-v2") assert(lib2.id("x") == true) -- a different "id" implementation + for _, len in ipairs{0, 10, 39, 40, 41, 1000} do + local str = string.rep("a", len) + local str1 = lib2.newstr(str) + assert(str == str1) + end + -- test C submodules local fs, ext = require"lib1.sub" assert(_ENV.x == "lib1.sub" and _ENV.y == DC"lib1") @@ -447,7 +453,7 @@ do end --- test of large float/integer indices +-- test of large float/integer indices -- compute maximum integer where all bits fit in a float local maxint = math.maxinteger diff --git a/testes/libs/lib22.c b/testes/libs/lib22.c index 8e6565022e..b377cce520 100644 --- a/testes/libs/lib22.c +++ b/testes/libs/lib22.c @@ -1,3 +1,7 @@ +/* implementation for lib2-v2 */ + +#include + #include "lua.h" #include "lauxlib.h" @@ -8,8 +12,54 @@ static int id (lua_State *L) { } +struct STR { + void *ud; + lua_Alloc allocf; +}; + + +static void *t_freestr (void *ud, void *ptr, size_t osize, size_t nsize) { + struct STR *blk = (struct STR*)ptr - 1; + blk->allocf(blk->ud, blk, sizeof(struct STR) + osize, 0); + return NULL; +} + + +static int newstr (lua_State *L) { + size_t len; + const char *str = luaL_checklstring(L, 1, &len); + void *ud; + lua_Alloc allocf = lua_getallocf(L, &ud); + struct STR *blk = (struct STR*)allocf(ud, NULL, 0, + len + 1 + sizeof(struct STR)); + if (blk == NULL) { /* allocation error? */ + lua_pushliteral(L, "not enough memory"); + lua_error(L); /* raise a memory error */ + } + blk->ud = ud; blk->allocf = allocf; + memcpy(blk + 1, str, len + 1); + lua_pushexternalstring(L, (char *)(blk + 1), len, t_freestr, L); + return 1; +} + + +/* +** Create an external string and keep it in the registry, so that it +** will test that the library code is still available (to deallocate +** this string) when closing the state. +*/ +static void initstr (lua_State *L) { + lua_pushcfunction(L, newstr); + lua_pushstring(L, + "012345678901234567890123456789012345678901234567890123456789"); + lua_call(L, 1, 1); /* call newstr("0123...") */ + luaL_ref(L, LUA_REGISTRYINDEX); /* keep string in the registry */ +} + + static const struct luaL_Reg funcs[] = { {"id", id}, + {"newstr", newstr}, {NULL, NULL} }; @@ -18,6 +68,7 @@ LUAMOD_API int luaopen_lib2 (lua_State *L) { lua_settop(L, 2); lua_setglobal(L, "y"); /* y gets 2nd parameter */ lua_setglobal(L, "x"); /* x gets 1st parameter */ + initstr(L); luaL_newlib(L, funcs); return 1; } From c612685d4b9ecdf0525b4d4410efa9f70d4b4518 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 9 Jul 2025 14:43:31 -0300 Subject: [PATCH 1075/1145] lua.c doesn't use function pointers with LUA_READLINE Bugs in macOS prevent assigning 'add_history' to 'l_addhist' without a warning. --- lua.c | 47 +++++++++++++++++++++++++---------------------- 1 file changed, 25 insertions(+), 22 deletions(-) diff --git a/lua.c b/lua.c index 66fb74b7b5..b2967a447d 100644 --- a/lua.c +++ b/lua.c @@ -438,13 +438,24 @@ static int handle_luainit (lua_State *L) { ** the standard input. ** * lua_saveline defines how to "save" a read line in a "history". ** * lua_freeline defines how to free a line read by lua_readline. -** -** If lua_readline is defined, all of them should be defined. */ #if !defined(lua_readline) /* { */ +/* Otherwise, all previously listed functions should be defined. */ -/* Code to use the readline library, either statically or dynamically linked */ +#if defined(LUA_USE_READLINE) /* { */ +/* Lua will be linked with '-lreadline' */ + +#include +#include + +#define lua_initreadline(L) ((void)L, rl_readline_name="lua") +#define lua_readline(buff,prompt) ((void)buff, readline(prompt)) +#define lua_saveline(line) add_history(line) +#define lua_freeline(line) free(line) + +#else /* }{ */ +/* use dynamically loaded readline (or nothing) */ /* pointer to 'readline' function (if any) */ typedef char *(*l_readlineT) (const char *prompt); @@ -480,22 +491,9 @@ static void lua_freeline (char *line) { } -#if defined(LUA_USE_READLINE) /* { */ - -/* assume Lua will be linked with '-lreadline' */ -#include -#include - -static void lua_initreadline(lua_State *L) { - UNUSED(L); - rl_readline_name = "lua"; - l_readline = cast(l_readlineT, readline); - l_addhist = cast(l_addhistT, add_history); -} - -#elif defined(LUA_USE_DLOPEN) && defined(LUA_READLINELIB) /* }{ */ - +#if defined(LUA_USE_DLOPEN) && defined(LUA_READLINELIB) /* { */ /* try to load 'readline' dynamically */ + #include static void lua_initreadline (lua_State *L) { @@ -508,15 +506,20 @@ static void lua_initreadline (lua_State *L) { *name = "lua"; l_readline = cast(l_readlineT, cast_func(dlsym(lib, "readline"))); l_addhist = cast(l_addhistT, cast_func(dlsym(lib, "add_history"))); + if (l_readline == NULL) + lua_warning(L, "unable to load 'readline'", 0); } } -#else /* }{ */ +#else /* }{ */ +/* no dlopen or LUA_READLINELIB undefined */ -/* no readline; leave function pointers as NULL */ -#define lua_initreadline(L) cast(void, L) +/* Leave pointers with NULL */ +#define lua_initreadline(L) ((void)L) -#endif /* } */ +#endif /* } */ + +#endif /* } */ #endif /* } */ From 60b6599e8322dd93e3b33c9496ff035a1c45552f Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 15 Jul 2025 14:40:27 -0300 Subject: [PATCH 1076/1145] Short strings can be external, too That complicates a little object equality (and therefore table access for long strings), but the old behavior was somewhat weird. (Short strings, a concept otherwise absent from the manual, could not be external.) --- loadlib.c | 4 +- lobject.h | 1 + lstring.c | 46 +++++++++----------- lstring.h | 3 +- ltable.c | 82 +++++++++++++++++++++-------------- ltests.c | 6 ++- lvm.c | 106 ++++++++++++++++++++++++++++------------------ manual/manual.of | 12 ++---- testes/attrib.lua | 28 +++++++++--- 9 files changed, 168 insertions(+), 120 deletions(-) diff --git a/loadlib.c b/loadlib.c index 2cd95ca308..8d2e68e261 100644 --- a/loadlib.c +++ b/loadlib.c @@ -345,8 +345,8 @@ static void *freelib (void *ud, void *ptr, size_t osize, size_t nsize) { ** Create a library string that, when deallocated, will unload 'plib' */ static void createlibstr (lua_State *L, void *plib) { - static const char dummy[] = /* common long body for all library strings */ - "01234567890123456789012345678901234567890123456789"; + /* common content for all library strings */ + static const char dummy[] = "01234567890"; lua_pushexternalstring(L, dummy, sizeof(dummy) - 1, freelib, plib); } diff --git a/lobject.h b/lobject.h index bc2f69ab4a..cc3dd370d0 100644 --- a/lobject.h +++ b/lobject.h @@ -418,6 +418,7 @@ typedef struct TString { #define strisshr(ts) ((ts)->shrlen >= 0) +#define isextstr(ts) (ttislngstring(ts) && tsvalue(ts)->shrlen != LSTRREG) /* diff --git a/lstring.c b/lstring.c index b5c8f89f02..17c6fd8f51 100644 --- a/lstring.c +++ b/lstring.c @@ -39,14 +39,14 @@ /* -** equality for long strings +** generic equality for strings */ -int luaS_eqlngstr (TString *a, TString *b) { - size_t len = a->u.lnglen; - lua_assert(a->tt == LUA_VLNGSTR && b->tt == LUA_VLNGSTR); - return (a == b) || /* same instance or... */ - ((len == b->u.lnglen) && /* equal length and ... */ - (memcmp(getlngstr(a), getlngstr(b), len) == 0)); /* equal contents */ +int luaS_eqstr (TString *a, TString *b) { + size_t len1, len2; + const char *s1 = getlstr(a, len1); + const char *s2 = getlstr(b, len2); + return ((len1 == len2) && /* equal length and ... */ + (memcmp(s1, s2, len1) == 0)); /* equal contents */ } @@ -315,28 +315,9 @@ static void f_newext (lua_State *L, void *ud) { } -static void f_pintern (lua_State *L, void *ud) { - struct NewExt *ne = cast(struct NewExt *, ud); - ne->ts = internshrstr(L, ne->s, ne->len); -} - - TString *luaS_newextlstr (lua_State *L, const char *s, size_t len, lua_Alloc falloc, void *ud) { struct NewExt ne; - if (len <= LUAI_MAXSHORTLEN) { /* short string? */ - ne.s = s; ne.len = len; - if (!falloc) - f_pintern(L, &ne); /* just internalize string */ - else { - TStatus status = luaD_rawrunprotected(L, f_pintern, &ne); - (*falloc)(ud, cast_voidp(s), len + 1, 0); /* free external string */ - if (status != LUA_OK) /* memory error? */ - luaM_error(L); /* re-raise memory error */ - } - return ne.ts; - } - /* "normal" case: long strings */ if (!falloc) { ne.kind = LSTRFIX; f_newext(L, &ne); /* just create header */ @@ -357,3 +338,16 @@ TString *luaS_newextlstr (lua_State *L, } +/* +** Normalize an external string: If it is short, internalize it. +*/ +TString *luaS_normstr (lua_State *L, TString *ts) { + size_t len = ts->u.lnglen; + if (len > LUAI_MAXSHORTLEN) + return ts; /* long string; keep the original */ + else { + const char *str = getlngstr(ts); + return internshrstr(L, str, len); + } +} + diff --git a/lstring.h b/lstring.h index 1751e0434e..2eac222b08 100644 --- a/lstring.h +++ b/lstring.h @@ -56,7 +56,7 @@ LUAI_FUNC unsigned luaS_hash (const char *str, size_t l, unsigned seed); LUAI_FUNC unsigned luaS_hashlongstr (TString *ts); -LUAI_FUNC int luaS_eqlngstr (TString *a, TString *b); +LUAI_FUNC int luaS_eqstr (TString *a, TString *b); LUAI_FUNC void luaS_resize (lua_State *L, int newsize); LUAI_FUNC void luaS_clearcache (global_State *g); LUAI_FUNC void luaS_init (lua_State *L); @@ -69,5 +69,6 @@ LUAI_FUNC TString *luaS_createlngstrobj (lua_State *L, size_t l); LUAI_FUNC TString *luaS_newextlstr (lua_State *L, const char *s, size_t len, lua_Alloc falloc, void *ud); LUAI_FUNC size_t luaS_sizelngstr (size_t len, int kind); +LUAI_FUNC TString *luaS_normstr (lua_State *L, TString *ts); #endif diff --git a/ltable.c b/ltable.c index 0b3ec1762c..1bea7affe8 100644 --- a/ltable.c +++ b/ltable.c @@ -234,41 +234,51 @@ l_sinline Node *mainpositionfromnode (const Table *t, Node *nd) { ** Check whether key 'k1' is equal to the key in node 'n2'. This ** equality is raw, so there are no metamethods. Floats with integer ** values have been normalized, so integers cannot be equal to -** floats. It is assumed that 'eqshrstr' is simply pointer equality, so -** that short strings are handled in the default case. -** A true 'deadok' means to accept dead keys as equal to their original -** values. All dead keys are compared in the default case, by pointer -** identity. (Only collectable objects can produce dead keys.) Note that -** dead long strings are also compared by identity. -** Once a key is dead, its corresponding value may be collected, and -** then another value can be created with the same address. If this -** other value is given to 'next', 'equalkey' will signal a false -** positive. In a regular traversal, this situation should never happen, -** as all keys given to 'next' came from the table itself, and therefore -** could not have been collected. Outside a regular traversal, we -** have garbage in, garbage out. What is relevant is that this false -** positive does not break anything. (In particular, 'next' will return -** some other valid item on the table or nil.) +** floats. It is assumed that 'eqshrstr' is simply pointer equality, +** so that short strings are handled in the default case. The flag +** 'deadok' means to accept dead keys as equal to their original values. +** (Only collectable objects can produce dead keys.) Note that dead +** long strings are also compared by identity. Once a key is dead, +** its corresponding value may be collected, and then another value +** can be created with the same address. If this other value is given +** to 'next', 'equalkey' will signal a false positive. In a regular +** traversal, this situation should never happen, as all keys given to +** 'next' came from the table itself, and therefore could not have been +** collected. Outside a regular traversal, we have garbage in, garbage +** out. What is relevant is that this false positive does not break +** anything. (In particular, 'next' will return some other valid item +** on the table or nil.) */ static int equalkey (const TValue *k1, const Node *n2, int deadok) { - if ((rawtt(k1) != keytt(n2)) && /* not the same variants? */ - !(deadok && keyisdead(n2) && iscollectable(k1))) - return 0; /* cannot be same key */ - switch (keytt(n2)) { - case LUA_VNIL: case LUA_VFALSE: case LUA_VTRUE: - return 1; - case LUA_VNUMINT: - return (ivalue(k1) == keyival(n2)); - case LUA_VNUMFLT: - return luai_numeq(fltvalue(k1), fltvalueraw(keyval(n2))); - case LUA_VLIGHTUSERDATA: - return pvalue(k1) == pvalueraw(keyval(n2)); - case LUA_VLCF: - return fvalue(k1) == fvalueraw(keyval(n2)); - case ctb(LUA_VLNGSTR): - return luaS_eqlngstr(tsvalue(k1), keystrval(n2)); - default: + if (rawtt(k1) != keytt(n2)) { /* not the same variants? */ + if (keyisshrstr(n2) && ttislngstring(k1)) { + /* an external string can be equal to a short-string key */ + return luaS_eqstr(tsvalue(k1), keystrval(n2)); + } + else if (deadok && keyisdead(n2) && iscollectable(k1)) { + /* a collectable value can be equal to a dead key */ return gcvalue(k1) == gcvalueraw(keyval(n2)); + } + else + return 0; /* otherwise, different variants cannot be equal */ + } + else { /* equal variants */ + switch (keytt(n2)) { + case LUA_VNIL: case LUA_VFALSE: case LUA_VTRUE: + return 1; + case LUA_VNUMINT: + return (ivalue(k1) == keyival(n2)); + case LUA_VNUMFLT: + return luai_numeq(fltvalue(k1), fltvalueraw(keyval(n2))); + case LUA_VLIGHTUSERDATA: + return pvalue(k1) == pvalueraw(keyval(n2)); + case LUA_VLCF: + return fvalue(k1) == fvalueraw(keyval(n2)); + case ctb(LUA_VLNGSTR): + return luaS_eqstr(tsvalue(k1), keystrval(n2)); + default: + return gcvalue(k1) == gcvalueraw(keyval(n2)); + } } } @@ -1158,6 +1168,14 @@ void luaH_finishset (lua_State *L, Table *t, const TValue *key, else if (l_unlikely(luai_numisnan(f))) luaG_runerror(L, "table index is NaN"); } + else if (isextstr(key)) { /* external string? */ + /* If string is short, must internalize it to be used as table key */ + TString *ts = luaS_normstr(L, tsvalue(key)); + setsvalue2s(L, L->top.p++, ts); /* anchor 'ts' (EXTRA_STACK) */ + luaH_newkey(L, t, s2v(L->top.p - 1), value); + L->top.p--; + return; + } luaH_newkey(L, t, key, value); } else if (hres > 0) { /* regular Node? */ diff --git a/ltests.c b/ltests.c index e7bc66dd28..d92cd6c56d 100644 --- a/ltests.c +++ b/ltests.c @@ -1066,8 +1066,12 @@ static int tracegc (lua_State *L) { static int hash_query (lua_State *L) { if (lua_isnone(L, 2)) { + TString *ts; luaL_argcheck(L, lua_type(L, 1) == LUA_TSTRING, 1, "string expected"); - lua_pushinteger(L, cast_int(tsvalue(obj_at(L, 1))->hash)); + ts = tsvalue(obj_at(L, 1)); + if (ts->tt == LUA_VLNGSTR) + luaS_hashlongstr(ts); /* make sure long string has a hash */ + lua_pushinteger(L, cast_int(ts->hash)); } else { TValue *o = obj_at(L, 1); diff --git a/lvm.c b/lvm.c index 97dfe5ee62..7456688826 100644 --- a/lvm.c +++ b/lvm.c @@ -573,52 +573,74 @@ int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r) { */ int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2) { const TValue *tm; - if (ttypetag(t1) != ttypetag(t2)) { /* not the same variant? */ - if (ttype(t1) != ttype(t2) || ttype(t1) != LUA_TNUMBER) - return 0; /* only numbers can be equal with different variants */ - else { /* two numbers with different variants */ - /* One of them is an integer. If the other does not have an - integer value, they cannot be equal; otherwise, compare their - integer values. */ - lua_Integer i1, i2; - return (luaV_tointegerns(t1, &i1, F2Ieq) && - luaV_tointegerns(t2, &i2, F2Ieq) && - i1 == i2); + if (ttype(t1) != ttype(t2)) /* not the same type? */ + return 0; + else if (ttypetag(t1) != ttypetag(t2)) { + switch (ttypetag(t1)) { + case LUA_VNUMINT: { /* integer == float? */ + /* integer and float can only be equal if float has an integer + value equal to the integer */ + lua_Integer i2; + return (luaV_flttointeger(fltvalue(t2), &i2, F2Ieq) && + ivalue(t1) == i2); + } + case LUA_VNUMFLT: { /* float == integer? */ + lua_Integer i1; /* see comment in previous case */ + return (luaV_flttointeger(fltvalue(t1), &i1, F2Ieq) && + i1 == ivalue(t2)); + } + case LUA_VSHRSTR: case LUA_VLNGSTR: { + /* compare two strings with different variants: they can be + equal when one string is a short string and the other is + an external string */ + return luaS_eqstr(tsvalue(t1), tsvalue(t2)); + } + default: + /* only numbers (integer/float) and strings (long/short) can have + equal values with different variants */ + return 0; } } - /* values have same type and same variant */ - switch (ttypetag(t1)) { - case LUA_VNIL: case LUA_VFALSE: case LUA_VTRUE: return 1; - case LUA_VNUMINT: return (ivalue(t1) == ivalue(t2)); - case LUA_VNUMFLT: return luai_numeq(fltvalue(t1), fltvalue(t2)); - case LUA_VLIGHTUSERDATA: return pvalue(t1) == pvalue(t2); - case LUA_VLCF: return fvalue(t1) == fvalue(t2); - case LUA_VSHRSTR: return eqshrstr(tsvalue(t1), tsvalue(t2)); - case LUA_VLNGSTR: return luaS_eqlngstr(tsvalue(t1), tsvalue(t2)); - case LUA_VUSERDATA: { - if (uvalue(t1) == uvalue(t2)) return 1; - else if (L == NULL) return 0; - tm = fasttm(L, uvalue(t1)->metatable, TM_EQ); - if (tm == NULL) - tm = fasttm(L, uvalue(t2)->metatable, TM_EQ); - break; /* will try TM */ + else { /* equal variants */ + switch (ttypetag(t1)) { + case LUA_VNIL: case LUA_VFALSE: case LUA_VTRUE: + return 1; + case LUA_VNUMINT: + return (ivalue(t1) == ivalue(t2)); + case LUA_VNUMFLT: + return (fltvalue(t1) == fltvalue(t2)); + case LUA_VLIGHTUSERDATA: return pvalue(t1) == pvalue(t2); + case LUA_VSHRSTR: + return eqshrstr(tsvalue(t1), tsvalue(t2)); + case LUA_VLNGSTR: + return luaS_eqstr(tsvalue(t1), tsvalue(t2)); + case LUA_VUSERDATA: { + if (uvalue(t1) == uvalue(t2)) return 1; + else if (L == NULL) return 0; + tm = fasttm(L, uvalue(t1)->metatable, TM_EQ); + if (tm == NULL) + tm = fasttm(L, uvalue(t2)->metatable, TM_EQ); + break; /* will try TM */ + } + case LUA_VTABLE: { + if (hvalue(t1) == hvalue(t2)) return 1; + else if (L == NULL) return 0; + tm = fasttm(L, hvalue(t1)->metatable, TM_EQ); + if (tm == NULL) + tm = fasttm(L, hvalue(t2)->metatable, TM_EQ); + break; /* will try TM */ + } + case LUA_VLCF: + return (fvalue(t1) == fvalue(t2)); + default: /* functions and threads */ + return (gcvalue(t1) == gcvalue(t2)); } - case LUA_VTABLE: { - if (hvalue(t1) == hvalue(t2)) return 1; - else if (L == NULL) return 0; - tm = fasttm(L, hvalue(t1)->metatable, TM_EQ); - if (tm == NULL) - tm = fasttm(L, hvalue(t2)->metatable, TM_EQ); - break; /* will try TM */ + if (tm == NULL) /* no TM? */ + return 0; /* objects are different */ + else { + int tag = luaT_callTMres(L, tm, t1, t2, L->top.p); /* call TM */ + return !tagisfalse(tag); } - default: - return gcvalue(t1) == gcvalue(t2); - } - if (tm == NULL) /* no TM? */ - return 0; /* objects are different */ - else { - int tag = luaT_callTMres(L, tm, t1, t2, L->top.p); /* call TM */ - return !tagisfalse(tag); } } diff --git a/manual/manual.of b/manual/manual.of index 8f90f942ee..b276577430 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -2419,8 +2419,8 @@ for instance @T{foo(e1, e2, e3)} @see{functioncall}.} @item{A multiple assignment, for instance @T{a , b, c = e1, e2, e3} @see{assignment}.} -@item{A local declaration, -for instance @T{local a , b, c = e1, e2, e3} @see{localvar}.} +@item{A local or global declaration, +which is a special case of multiple assignment.} @item{The initial values in a generic @rw{for} loop, for instance @T{for k in e1, e2, e3 do ... end} @see{for}.} @@ -2431,8 +2431,7 @@ the list of values from the list of expressions must be @emph{adjusted} to a specific length: the number of parameters in a call to a non-variadic function @see{func-def}, -the number of variables in a multiple assignment or -a local declaration, +the number of variables in a multiple assignment or a declaration, and exactly four values for a generic @rw{for} loop. The @def{adjustment} follows these rules: If there are more values than needed, @@ -4075,11 +4074,6 @@ the string @id{s} as the block, the length plus one (to account for the ending zero) as the old size, and 0 as the new size. -Lua always @x{internalizes} strings with lengths up to 40 characters. -So, for strings in that range, -this function will immediately internalize the string -and call @id{falloc} to free the buffer. - Even when using an external buffer, Lua still has to allocate a header for the string. In case of a memory-allocation error, diff --git a/testes/attrib.lua b/testes/attrib.lua index 8a3462ea9d..f415608699 100644 --- a/testes/attrib.lua +++ b/testes/attrib.lua @@ -300,12 +300,6 @@ else assert(_ENV.x == "lib2-v2" and _ENV.y == DC"lib2-v2") assert(lib2.id("x") == true) -- a different "id" implementation - for _, len in ipairs{0, 10, 39, 40, 41, 1000} do - local str = string.rep("a", len) - local str1 = lib2.newstr(str) - assert(str == str1) - end - -- test C submodules local fs, ext = require"lib1.sub" assert(_ENV.x == "lib1.sub" and _ENV.y == DC"lib1") @@ -314,11 +308,11 @@ else _ENV.x, _ENV.y = nil end + _ENV = _G -- testing preload - do local p = package package = {} @@ -337,6 +331,26 @@ do assert(type(package.path) == "string") end + +do print("testing external strings") + package.cpath = DC"?" + local lib2 = require"lib2-v2" + local t = {} + for _, len in ipairs{0, 10, 39, 40, 41, 1000} do + local str = string.rep("a", len) + local str1 = lib2.newstr(str) + assert(str == str1) + assert(not T or T.hash(str) == T.hash(str1)) + t[str1] = 20; assert(t[str] == 20 and t[str1] == 20) + t[str] = 10; assert(t[str1] == 10) + local tt = {[str1] = str1} + assert(next(tt) == str1 and next(tt, str1) == nil) + assert(tt[str] == str) + local str2 = lib2.newstr(str1) + assert(str == str2 and t[str2] == 10 and tt[str2] == str) + end +end + print('+') end --] From ccb8b307f11c7497e61f617b12f3a7f0a697256c Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 18 Jul 2025 16:10:28 -0300 Subject: [PATCH 1077/1145] Correction in utf8.offset Wrong utf-8 character may have no continuation bytes. --- lutf8lib.c | 7 ++++--- testes/utf8.lua | 9 +++++++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/lutf8lib.c b/lutf8lib.c index be01613514..df49c901d6 100644 --- a/lutf8lib.c +++ b/lutf8lib.c @@ -215,9 +215,10 @@ static int byteoffset (lua_State *L) { } lua_pushinteger(L, posi + 1); /* initial position */ if ((s[posi] & 0x80) != 0) { /* multi-byte character? */ - do { - posi++; - } while (iscontp(s + posi + 1)); /* skip to final byte */ + if (iscont(s[posi])) + return luaL_error(L, "initial position is a continuation byte"); + while (iscontp(s + posi + 1)) + posi++; /* skip to last continuation byte */ } /* else one-byte character: final position is the initial one */ lua_pushinteger(L, posi + 1); /* 'posi' now is the final position */ diff --git a/testes/utf8.lua b/testes/utf8.lua index 143c6d3467..028995a478 100644 --- a/testes/utf8.lua +++ b/testes/utf8.lua @@ -152,11 +152,20 @@ checkerror("position out of bounds", utf8.offset, "", 1, -1) checkerror("continuation byte", utf8.offset, "𦧺", 1, 2) checkerror("continuation byte", utf8.offset, "𦧺", 1, 2) checkerror("continuation byte", utf8.offset, "\x80", 1) +checkerror("continuation byte", utf8.offset, "\x9c", -1) -- error in indices for len checkerror("out of bounds", utf8.len, "abc", 0, 2) checkerror("out of bounds", utf8.len, "abc", 1, 4) +do -- missing continuation bytes + -- get what is available + local p, e = utf8.offset("\xE0", 1) + assert(p == 1 and e == 1) + local p, e = utf8.offset("\xE0\x9e", -1) + assert(p == 1 and e == 2) +end + local s = "hello World" local t = {string.byte(s, 1, -1)} From 303f4155593721dfd57dadc6e56122e465ce9efb Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 18 Jul 2025 16:18:30 -0300 Subject: [PATCH 1078/1145] Randomness added to table length computation A bad actor could fill only a few entries in a table (power of twos in decreasing order, see tests) and produce a small table with a huge length. If your program builds a table with external data and iterates over its length, this behavior could be an issue. --- lapi.c | 2 +- lobject.c | 3 ++- ltable.c | 50 ++++++++++++++++++++++++++++------------------ ltable.h | 2 +- lvm.c | 2 +- testes/nextvar.lua | 12 +++++++++++ 6 files changed, 48 insertions(+), 23 deletions(-) diff --git a/lapi.c b/lapi.c index 71405a2584..0c88751a2e 100644 --- a/lapi.c +++ b/lapi.c @@ -440,7 +440,7 @@ LUA_API lua_Unsigned lua_rawlen (lua_State *L, int idx) { case LUA_VSHRSTR: return cast(lua_Unsigned, tsvalue(o)->shrlen); case LUA_VLNGSTR: return cast(lua_Unsigned, tsvalue(o)->u.lnglen); case LUA_VUSERDATA: return cast(lua_Unsigned, uvalue(o)->len); - case LUA_VTABLE: return luaH_getn(hvalue(o)); + case LUA_VTABLE: return luaH_getn(L, hvalue(o)); default: return 0; } } diff --git a/lobject.c b/lobject.c index 5c270b274b..b558cfe0d2 100644 --- a/lobject.c +++ b/lobject.c @@ -31,7 +31,8 @@ /* -** Computes ceil(log2(x)) +** Computes ceil(log2(x)), which is the smallest integer n such that +** x <= (1 << n). */ lu_byte luaO_ceillog2 (unsigned int x) { static const lu_byte log_2[256] = { /* log_2[i - 1] = ceil(log2(i)) */ diff --git a/ltable.c b/ltable.c index 1bea7affe8..4017d8c7d0 100644 --- a/ltable.c +++ b/ltable.c @@ -1220,24 +1220,36 @@ void luaH_setint (lua_State *L, Table *t, lua_Integer key, TValue *value) { /* ** Try to find a boundary in the hash part of table 't'. From the -** caller, we know that 'j' is zero or present and that 'j + 1' is -** present. We want to find a larger key that is absent from the -** table, so that we can do a binary search between the two keys to -** find a boundary. We keep doubling 'j' until we get an absent index. -** If the doubling would overflow, we try LUA_MAXINTEGER. If it is -** absent, we are ready for the binary search. ('j', being max integer, -** is larger or equal to 'i', but it cannot be equal because it is -** absent while 'i' is present; so 'j > i'.) Otherwise, 'j' is a -** boundary. ('j + 1' cannot be a present integer key because it is -** not a valid integer in Lua.) +** caller, we know that 'asize + 1' is present. We want to find a larger +** key that is absent from the table, so that we can do a binary search +** between the two keys to find a boundary. We keep doubling 'j' until +** we get an absent index. If the doubling would overflow, we try +** LUA_MAXINTEGER. If it is absent, we are ready for the binary search. +** ('j', being max integer, is larger or equal to 'i', but it cannot be +** equal because it is absent while 'i' is present.) Otherwise, 'j' is a +** boundary. ('j + 1' cannot be a present integer key because it is not +** a valid integer in Lua.) +** About 'rnd': If we used a fixed algorithm, a bad actor could fill +** a table with only the keys that would be probed, in such a way that +** a small table could result in a huge length. To avoid that, we use +** the state's seed as a source of randomness. For the first probe, +** we "randomly double" 'i' by adding to it a random number roughly its +** width. */ -static lua_Unsigned hash_search (Table *t, lua_Unsigned j) { - lua_Unsigned i; - if (j == 0) j++; /* the caller ensures 'j + 1' is present */ - do { +static lua_Unsigned hash_search (lua_State *L, Table *t, unsigned asize) { + lua_Unsigned i = asize + 1; /* caller ensures t[i] is present */ + unsigned rnd = G(L)->seed; + int n = (asize > 0) ? luaO_ceillog2(asize) : 0; /* width of 'asize' */ + unsigned mask = (1u << n) - 1; /* 11...111 with the width of 'asize' */ + unsigned incr = (rnd & mask) + 1; /* first increment (at least 1) */ + lua_Unsigned j = (incr <= l_castS2U(LUA_MAXINTEGER) - i) ? i + incr : i + 1; + rnd >>= n; /* used 'n' bits from 'rnd' */ + while (!hashkeyisempty(t, j)) { /* repeat until an absent t[j] */ i = j; /* 'i' is a present index */ - if (j <= l_castS2U(LUA_MAXINTEGER) / 2) - j *= 2; + if (j <= l_castS2U(LUA_MAXINTEGER)/2 - 1) { + j = j*2 + (rnd & 1); /* try again with 2j or 2j+1 */ + rnd >>= 1; + } else { j = LUA_MAXINTEGER; if (hashkeyisempty(t, j)) /* t[j] not present? */ @@ -1245,7 +1257,7 @@ static lua_Unsigned hash_search (Table *t, lua_Unsigned j) { else /* weird case */ return j; /* well, max integer is a boundary... */ } - } while (!hashkeyisempty(t, j)); /* repeat until an absent t[j] */ + } /* i < j && t[i] present && t[j] absent */ while (j - i > 1u) { /* do a binary search between them */ lua_Unsigned m = (i + j) / 2; @@ -1286,7 +1298,7 @@ static lua_Unsigned newhint (Table *t, unsigned hint) { ** If there is no array part, or its last element is non empty, the ** border may be in the hash part. */ -lua_Unsigned luaH_getn (Table *t) { +lua_Unsigned luaH_getn (lua_State *L, Table *t) { unsigned asize = t->asize; if (asize > 0) { /* is there an array part? */ const unsigned maxvicinity = 4; @@ -1327,7 +1339,7 @@ lua_Unsigned luaH_getn (Table *t) { if (isdummy(t) || hashkeyisempty(t, asize + 1)) return asize; /* 'asize + 1' is empty */ else /* 'asize + 1' is also non empty */ - return hash_search(t, asize); + return hash_search(L, t, asize); } diff --git a/ltable.h b/ltable.h index ca21e69202..f3b7bc7e7e 100644 --- a/ltable.h +++ b/ltable.h @@ -173,7 +173,7 @@ LUAI_FUNC void luaH_resizearray (lua_State *L, Table *t, unsigned nasize); LUAI_FUNC lu_mem luaH_size (Table *t); LUAI_FUNC void luaH_free (lua_State *L, Table *t); LUAI_FUNC int luaH_next (lua_State *L, Table *t, StkId key); -LUAI_FUNC lua_Unsigned luaH_getn (Table *t); +LUAI_FUNC lua_Unsigned luaH_getn (lua_State *L, Table *t); #if defined(LUA_DEBUG) diff --git a/lvm.c b/lvm.c index 7456688826..cfdcf97a55 100644 --- a/lvm.c +++ b/lvm.c @@ -722,7 +722,7 @@ void luaV_objlen (lua_State *L, StkId ra, const TValue *rb) { Table *h = hvalue(rb); tm = fasttm(L, h->metatable, TM_LEN); if (tm) break; /* metamethod? break switch to call it */ - setivalue(s2v(ra), l_castU2S(luaH_getn(h))); /* else primitive len */ + setivalue(s2v(ra), l_castU2S(luaH_getn(L, h))); /* else primitive len */ return; } case LUA_VSHRSTR: { diff --git a/testes/nextvar.lua b/testes/nextvar.lua index 03810a8e41..7e5bed5685 100644 --- a/testes/nextvar.lua +++ b/testes/nextvar.lua @@ -345,6 +345,18 @@ do end end + +do print("testing attack on table length") + local t = {} + local lim = math.floor(math.log(math.maxinteger, 2)) - 1 + for i = lim, 0, -1 do + t[2^i] = true + end + assert(t[1 << lim]) + -- next loop should not take forever + for i = 1, #t do end +end + local nofind = {} From e3716ee161bb5416b5eb846eff6039d61954cfbd Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 23 Jul 2025 18:12:53 -0300 Subject: [PATCH 1079/1145] Fix in string.rep The cast of n (number of repetitions) to size_t may truncate its value, causing a buffer overflow later. Better to check the buffer size using lua_Integer, as all string lengths must fit in a lua_Integer and n already is a lua_Integer. If everything fits in MAX_SIZE, then we can safely convert n to size_t and compute the buffer size as a size_t. As a corner case, n can be larger than size_t if the strings being repeated have length zero, but in this case it will be multiplied by zero, so an overflow in the cast is irrelevant. --- lstrlib.c | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/lstrlib.c b/lstrlib.c index 8056c6ff57..d9735903d4 100644 --- a/lstrlib.c +++ b/lstrlib.c @@ -132,27 +132,31 @@ static int str_upper (lua_State *L) { } +/* +** MAX_SIZE is limited both by size_t and lua_Integer. +** When x <= MAX_SIZE, x can be safely cast to size_t or lua_Integer. +*/ static int str_rep (lua_State *L) { - size_t l, lsep; - const char *s = luaL_checklstring(L, 1, &l); + size_t len, lsep; + const char *s = luaL_checklstring(L, 1, &len); lua_Integer n = luaL_checkinteger(L, 2); const char *sep = luaL_optlstring(L, 3, "", &lsep); if (n <= 0) lua_pushliteral(L, ""); - else if (l_unlikely(l + lsep < l || l + lsep > MAX_SIZE / cast_sizet(n))) + else if (l_unlikely(len > MAX_SIZE - lsep || + cast_st2S(len + lsep) > cast_st2S(MAX_SIZE) / n)) return luaL_error(L, "resulting string too large"); else { - size_t totallen = ((size_t)n * (l + lsep)) - lsep; + size_t totallen = (cast_sizet(n) * (len + lsep)) - lsep; luaL_Buffer b; char *p = luaL_buffinitsize(L, &b, totallen); while (n-- > 1) { /* first n-1 copies (followed by separator) */ - memcpy(p, s, l * sizeof(char)); p += l; + memcpy(p, s, len * sizeof(char)); p += len; if (lsep > 0) { /* empty 'memcpy' is not that cheap */ - memcpy(p, sep, lsep * sizeof(char)); - p += lsep; + memcpy(p, sep, lsep * sizeof(char)); p += lsep; } } - memcpy(p, s, l * sizeof(char)); /* last copy (not followed by separator) */ + memcpy(p, s, len * sizeof(char)); /* last copy without separator */ luaL_pushresultsize(&b, totallen); } return 1; From c33bb08ffe04f24e09571b59eed3c9b59b622d91 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 29 Jul 2025 11:50:20 -0300 Subject: [PATCH 1080/1145] Added some casts for 32-bit machines When both 'int' and 'l_obj' have 32 bits, an unsigned int needs a cast to be assigned to 'l_obj'. (As long as 'l_obj' can count the total memory used by the system, these casts should be safe.) --- lgc.c | 2 +- llimits.h | 4 ++-- lobject.c | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lgc.c b/lgc.c index bbaa5ff7e1..a775b6e510 100644 --- a/lgc.c +++ b/lgc.c @@ -624,7 +624,7 @@ static l_mem traversetable (global_State *g, Table *h) { linkgclist(h, g->allweak); /* must clear collected entries */ break; } - return 1 + 2*sizenode(h) + h->asize; + return cast(l_mem, 1 + 2*sizenode(h) + h->asize); } diff --git a/llimits.h b/llimits.h index 223b5e6c34..c4719a1507 100644 --- a/llimits.h +++ b/llimits.h @@ -20,8 +20,8 @@ /* ** 'l_mem' is a signed integer big enough to count the total memory ** used by Lua. (It is signed due to the use of debt in several -** computations.) Usually, 'ptrdiff_t' should work, but we use 'long' -** for 16-bit machines. +** computations.) 'lu_mem' is a corresponding unsigned type. Usually, +** 'ptrdiff_t' should work, but we use 'long' for 16-bit machines. */ #if defined(LUAI_MEM) /* { external definitions? */ typedef LUAI_MEM l_mem; diff --git a/lobject.c b/lobject.c index b558cfe0d2..763b484609 100644 --- a/lobject.c +++ b/lobject.c @@ -87,7 +87,7 @@ lu_byte luaO_codeparam (unsigned int p) { ** overflow, so we check which order is best. */ l_mem luaO_applyparam (lu_byte p, l_mem x) { - unsigned int m = p & 0xF; /* mantissa */ + int m = p & 0xF; /* mantissa */ int e = (p >> 4); /* exponent */ if (e > 0) { /* normalized? */ e--; /* correct exponent */ From 8fddca81e7d4137512e92f398ca638d61b935dbd Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 29 Jul 2025 14:35:04 -0300 Subject: [PATCH 1081/1145] 'onelua' can use the test library Just add -DLUA_USER_H='"ltests.h"' when compiling it. --- lauxlib.c | 6 +++++- ltests.c | 32 ++++++++++++++++++++++---------- ltests.h | 4 ++-- onelua.c | 5 +++++ 4 files changed, 34 insertions(+), 13 deletions(-) diff --git a/lauxlib.c b/lauxlib.c index 7f33f0addb..adb3851e82 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -1177,7 +1177,11 @@ LUALIB_API unsigned int luaL_makeseed (lua_State *L) { } -LUALIB_API lua_State *luaL_newstate (void) { +/* +** Use the name with parentheses so that headers can redefine it +** as a macro. +*/ +LUALIB_API lua_State *(luaL_newstate) (void) { lua_State *L = lua_newstate(l_alloc, NULL, luai_makeseed()); if (l_likely(L)) { lua_atpanic(L, &panic); diff --git a/ltests.c b/ltests.c index d92cd6c56d..c4905f9487 100644 --- a/ltests.c +++ b/ltests.c @@ -164,13 +164,13 @@ static void warnf (void *ud, const char *msg, int tocont) { #define MARK 0x55 /* 01010101 (a nice pattern) */ -typedef union Header { +typedef union memHeader { LUAI_MAXALIGN; struct { size_t size; int type; } d; -} Header; +} memHeader; #if !defined(EXTERNMEMCHECK) @@ -193,14 +193,14 @@ Memcontrol l_memcontrol = {0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL}}; -static void freeblock (Memcontrol *mc, Header *block) { +static void freeblock (Memcontrol *mc, memHeader *block) { if (block) { size_t size = block->d.size; int i; for (i = 0; i < MARKSIZE; i++) /* check marks after block */ lua_assert(*(cast_charp(block + 1) + size + i) == MARK); mc->objcount[block->d.type]--; - fillmem(block, sizeof(Header) + size + MARKSIZE); /* erase block */ + fillmem(block, sizeof(memHeader) + size + MARKSIZE); /* erase block */ free(block); /* actually free block */ mc->numblocks--; /* update counts */ mc->total -= size; @@ -210,7 +210,7 @@ static void freeblock (Memcontrol *mc, Header *block) { void *debug_realloc (void *ud, void *b, size_t oldsize, size_t size) { Memcontrol *mc = cast(Memcontrol *, ud); - Header *block = cast(Header *, b); + memHeader *block = cast(memHeader *, b); int type; if (mc->memlimit == 0) { /* first time? */ char *limit = getenv("MEMLIMIT"); /* initialize memory limit */ @@ -241,12 +241,12 @@ void *debug_realloc (void *ud, void *b, size_t oldsize, size_t size) { if (size > oldsize && mc->total+size-oldsize > mc->memlimit) return NULL; /* fake a memory allocation error */ else { - Header *newblock; + memHeader *newblock; int i; size_t commonsize = (oldsize < size) ? oldsize : size; - size_t realsize = sizeof(Header) + size + MARKSIZE; + size_t realsize = sizeof(memHeader) + size + MARKSIZE; if (realsize < size) return NULL; /* arithmetic overflow! */ - newblock = cast(Header *, malloc(realsize)); /* alloc a new block */ + newblock = cast(memHeader *, malloc(realsize)); /* alloc a new block */ if (newblock == NULL) return NULL; /* really out of memory? */ if (block) { @@ -480,7 +480,7 @@ static int lua_checkpc (CallInfo *ci) { } -static void checkstack (global_State *g, lua_State *L1) { +static void check_stack (global_State *g, lua_State *L1) { StkId o; CallInfo *ci; UpVal *uv; @@ -517,7 +517,7 @@ static void checkrefs (global_State *g, GCObject *o) { break; } case LUA_VTHREAD: { - checkstack(g, gco2th(o)); + check_stack(g, gco2th(o)); break; } case LUA_VLCL: { @@ -908,6 +908,17 @@ static int get_limits (lua_State *L) { } +static int get_sizes (lua_State *L) { + lua_newtable(L); + setnameval(L, "Lua state", sizeof(lua_State)); + setnameval(L, "global state", sizeof(global_State)); + setnameval(L, "TValue", sizeof(TValue)); + setnameval(L, "Node", sizeof(Node)); + setnameval(L, "stack Value", sizeof(StackValue)); + return 1; +} + + static int mem_query (lua_State *L) { if (lua_isnone(L, 1)) { lua_pushinteger(L, cast_Integer(l_memcontrol.total)); @@ -2171,6 +2182,7 @@ static const struct luaL_Reg tests_funcs[] = { {"s2d", s2d}, {"sethook", sethook}, {"stacklevel", stacklevel}, + {"sizes", get_sizes}, {"testC", testC}, {"makeCfunc", makeCfunc}, {"totalmem", mem_query}, diff --git a/ltests.h b/ltests.h index d34e9d421d..c825bdcfc1 100644 --- a/ltests.h +++ b/ltests.h @@ -122,14 +122,14 @@ LUA_API int luaB_opentests (lua_State *L); LUA_API void *debug_realloc (void *ud, void *block, size_t osize, size_t nsize); -#if defined(lua_c) + #define luaL_newstate() \ lua_newstate(debug_realloc, &l_memcontrol, luaL_makeseed(NULL)) #define luai_openlibs(L) \ { luaL_openlibs(L); \ luaL_requiref(L, "T", luaB_opentests, 1); \ lua_pop(L, 1); } -#endif + diff --git a/onelua.c b/onelua.c index 2a43496124..cc639494cc 100644 --- a/onelua.c +++ b/onelua.c @@ -110,6 +110,11 @@ #include "linit.c" #endif +/* test library -- used only for internal development */ +#if defined(LUA_DEBUG) +#include "ltests.c" +#endif + /* lua */ #ifdef MAKE_LUA #include "lua.c" From 5b179eaf6a78af5f000d76147af94669d04487b2 Mon Sep 17 00:00:00 2001 From: Roberto I Date: Sat, 9 Aug 2025 15:08:53 -0300 Subject: [PATCH 1082/1145] Details --- lcode.c | 29 +++++++++++++++-------------- lparser.c | 2 +- ltable.c | 2 +- manual/manual.of | 32 ++++++++++++++++++++------------ testes/gengc.lua | 14 ++++++++++++++ 5 files changed, 51 insertions(+), 28 deletions(-) diff --git a/lcode.c b/lcode.c index 7ca895f147..cafe265e73 100644 --- a/lcode.c +++ b/lcode.c @@ -565,20 +565,20 @@ static int k2proto (FuncState *fs, TValue *key, TValue *v) { TValue val; Proto *f = fs->f; int tag = luaH_get(fs->kcache, key, &val); /* query scanner table */ - int k; if (!tagisempty(tag)) { /* is there an index there? */ - k = cast_int(ivalue(&val)); + int k = cast_int(ivalue(&val)); /* collisions can happen only for float keys */ lua_assert(ttisfloat(key) || luaV_rawequalobj(&f->k[k], v)); return k; /* reuse index */ } - /* constant not found; create a new entry */ - k = addk(fs, f, v); - /* cache it for reuse; numerical value does not need GC barrier; - table is not a metatable, so it does not need to invalidate cache */ - setivalue(&val, k); - luaH_set(fs->ls->L, fs->kcache, key, &val); - return k; + else { /* constant not found; create a new entry */ + int k = addk(fs, f, v); + /* cache it for reuse; numerical value does not need GC barrier; + table is not a metatable, so it does not need to invalidate cache */ + setivalue(&val, k); + luaH_set(fs->ls->L, fs->kcache, key, &val); + return k; + } } @@ -604,13 +604,14 @@ static int luaK_intK (FuncState *fs, lua_Integer n) { /* ** Add a float to list of constants and return its index. Floats ** with integral values need a different key, to avoid collision -** with actual integers. To that, we add to the number its smaller +** with actual integers. To that end, we add to the number its smaller ** power-of-two fraction that is still significant in its scale. -** For doubles, that would be 1/2^52. +** (For doubles, the fraction would be 2^-52). ** This method is not bulletproof: different numbers may generate the ** same key (e.g., very large numbers will overflow to 'inf') and for -** floats larger than 2^53 the result is still an integer. At worst, -** this only wastes an entry with a duplicate. +** floats larger than 2^53 the result is still an integer. For those +** cases, just generate a new entry. At worst, this only wastes an entry +** with a duplicate. */ static int luaK_numberK (FuncState *fs, lua_Number r) { TValue o, kv; @@ -625,7 +626,7 @@ static int luaK_numberK (FuncState *fs, lua_Number r) { const lua_Number k = r * (1 + q); /* key */ lua_Integer ik; setfltvalue(&kv, k); /* key as a TValue */ - if (!luaV_flttointeger(k, &ik, F2Ieq)) { /* not an integral value? */ + if (!luaV_flttointeger(k, &ik, F2Ieq)) { /* not an integer value? */ int n = k2proto(fs, &kv, &o); /* use key */ if (luaV_rawequalobj(&fs->f->k[n], &o)) /* correct value? */ return n; diff --git a/lparser.c b/lparser.c index dde0b6d5e0..73dad6d77d 100644 --- a/lparser.c +++ b/lparser.c @@ -1827,7 +1827,7 @@ static lu_byte getglobalattribute (LexState *ls, lu_byte df) { switch (kind) { case RDKTOCLOSE: luaK_semerror(ls, "global variables cannot be to-be-closed"); - break; /* to avoid warnings */ + return kind; /* to avoid warnings */ case RDKCONST: return GDKCONST; /* adjust kind for global variable */ default: diff --git a/ltable.c b/ltable.c index 4017d8c7d0..b7f88f6ffe 100644 --- a/ltable.c +++ b/ltable.c @@ -156,7 +156,7 @@ static Node *hashint (const Table *t, lua_Integer i) { ** The main computation should be just ** n = frexp(n, &i); return (n * INT_MAX) + i ** but there are some numerical subtleties. -** In a two-complement representation, INT_MAX does not has an exact +** In a two-complement representation, INT_MAX may not have an exact ** representation as a float, but INT_MIN does; because the absolute ** value of 'frexp' is smaller than 1 (unless 'n' is inf/NaN), the ** absolute value of the product 'frexp * -INT_MIN' is smaller or equal diff --git a/manual/manual.of b/manual/manual.of index b276577430..61dd42f248 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -127,7 +127,8 @@ strings can contain any 8-bit value, including @x{embedded zeros} (@Char{\0}). Lua is also encoding-agnostic; it makes no assumptions about the contents of a string. -The length of any string in Lua must fit in a Lua integer. +The length of any string in Lua must fit in a Lua integer, +and the string plus a small header must fit in @id{size_t}. Lua can call (and manipulate) functions written in Lua and functions written in C @see{functioncall}. @@ -1555,7 +1556,8 @@ It has the following syntax: exp @bnfter{,} exp @bnfopt{@bnfter{,} exp} @Rw{do} block @Rw{end}} } The given identifier (@bnfNter{Name}) defines the control variable, -which is a new read-only variable local to the loop body (@emph{block}). +which is a new read-only (@id{const}) variable local to the loop body +(@emph{block}). The loop starts by evaluating once the three control expressions. Their values are called respectively @@ -1610,7 +1612,7 @@ works as follows. The names @rep{var_i} declare loop variables local to the loop body. The first of these variables is the @emph{control variable}, -which is a read-only variable. +which is a read-only (@id{const}) variable. The loop starts by evaluating @rep{explist} to produce four values: @@ -4083,7 +4085,7 @@ Lua will call @id{falloc} before raising the error. @APIEntry{const char *lua_pushfstring (lua_State *L, const char *fmt, ...);| -@apii{0,1,m} +@apii{0,1,v} Pushes onto the stack a formatted string and returns a pointer to this string @see{constchar}. @@ -4103,6 +4105,9 @@ A conversion specifier (and its corresponding extra argument) can be Every occurrence of @Char{%} in the string @id{fmt} must form a valid conversion specifier. +Besides memory allocation errors, +this function may raise an error if the resulting string is too large. + } @APIEntry{void lua_pushglobaltable (lua_State *L);| @@ -4135,7 +4140,7 @@ light userdata with the same @N{C address}. } @APIEntry{const char *lua_pushliteral (lua_State *L, const char *s);| -@apii{0,1,m} +@apii{0,1,v} This macro is equivalent to @Lid{lua_pushstring}, but should be used only when @id{s} is a literal string. @@ -4144,7 +4149,7 @@ but should be used only when @id{s} is a literal string. } @APIEntry{const char *lua_pushlstring (lua_State *L, const char *s, size_t len);| -@apii{0,1,m} +@apii{0,1,v} Pushes the string pointed to by @id{s} with size @id{len} onto the stack. @@ -4156,6 +4161,9 @@ including @x{embedded zeros}. Returns a pointer to the internal copy of the string @see{constchar}. +Besides memory allocation errors, +this function may raise an error if the string is too large. + } @APIEntry{void lua_pushnil (lua_State *L);| @@ -5015,8 +5023,8 @@ then @id{name} is set to @id{NULL}. @item{@id{namewhat}| explains the @T{name} field. The value of @T{namewhat} can be -@T{"global"}, @T{"local"}, @T{"method"}, -@T{"field"}, @T{"upvalue"}, or @T{""} (the empty string), +@T{"global"}, @T{"local"}, @T{"upvalue"}, +@T{"field"}, @T{""} (the empty string), plus some other options, according to how the function was called. (Lua uses the empty string when no other option seems to apply.) } @@ -6571,7 +6579,7 @@ The call always returns the previous value of the parameter. If the call does not give a new value, the value is left unchanged. -Lua rounds these values before storing them; +Lua stores these values in a compressed format, so, the value returned as the previous value may not be exactly the last value set. } @@ -6585,10 +6593,10 @@ This function should not be called by a finalizer. } @LibEntry{dofile ([filename])| -Opens the named file and executes its content as a Lua chunk. +Opens the named file and executes its content as a Lua chunk, +returning all values returned by the chunk. When called without arguments, @id{dofile} executes the content of the standard input (@id{stdin}). -Returns all values returned by the chunk. In case of errors, @id{dofile} propagates the error to its caller. (That is, @id{dofile} does not run in protected mode.) @@ -6960,7 +6968,7 @@ in case of error (either the original error that stopped the coroutine or errors in closing methods), this function returns @false plus the error object; -otherwise ir returns @true. +otherwise it returns @true. } diff --git a/testes/gengc.lua b/testes/gengc.lua index ea99bdc43a..6509e39d8a 100644 --- a/testes/gengc.lua +++ b/testes/gengc.lua @@ -176,6 +176,20 @@ do print"testing stop-the-world collection" assert(collectgarbage("param", "stepsize") == step) end + +if T then -- test GC parameter codification + for _, percentage in ipairs{5, 10, 12, 20, 50, 100, 200, 500} do + local param = T.codeparam(percentage) -- codify percentage + for _, value in ipairs{1, 2, 10, 100, 257, 1023, 6500, 100000} do + local exact = value*percentage // 100 + local aprox = T.applyparam(param, value) -- apply percentage + -- difference is at most 10% (+1 compensates difference due to + -- rounding to integers) + assert(math.abs(aprox - exact) <= exact/10 + 1) + end + end +end + collectgarbage(oldmode) print('OK') From 53dc5a3bbadac166a8b40904790f91b351e55dd9 Mon Sep 17 00:00:00 2001 From: Roberto I Date: Sat, 9 Aug 2025 15:15:20 -0300 Subject: [PATCH 1083/1145] Functions 'frexp'-'ldexp' back to the math library They are basic for anything that handles the representation of floating numbers. --- lmathlib.c | 32 ++++++++++++++++---------------- manual/manual.of | 19 ++++++++++++++++++- testes/math.lua | 12 ++++++++++++ 3 files changed, 46 insertions(+), 17 deletions(-) diff --git a/lmathlib.c b/lmathlib.c index bd34c88860..b030f97ead 100644 --- a/lmathlib.c +++ b/lmathlib.c @@ -203,6 +203,20 @@ static int math_rad (lua_State *L) { return 1; } +static int math_frexp (lua_State *L) { + int e; + lua_pushnumber(L, l_mathop(frexp)(luaL_checknumber(L, 1), &e)); + lua_pushinteger(L, e); + return 2; +} + +static int math_ldexp (lua_State *L) { + lua_Number x = luaL_checknumber(L, 1); + int ep = (int)luaL_checkinteger(L, 2); + lua_pushnumber(L, l_mathop(ldexp)(x, ep)); + return 1; +} + static int math_min (lua_State *L) { int n = lua_gettop(L); /* number of arguments */ @@ -666,20 +680,6 @@ static int math_pow (lua_State *L) { return 1; } -static int math_frexp (lua_State *L) { - int e; - lua_pushnumber(L, l_mathop(frexp)(luaL_checknumber(L, 1), &e)); - lua_pushinteger(L, e); - return 2; -} - -static int math_ldexp (lua_State *L) { - lua_Number x = luaL_checknumber(L, 1); - int ep = (int)luaL_checkinteger(L, 2); - lua_pushnumber(L, l_mathop(ldexp)(x, ep)); - return 1; -} - static int math_log10 (lua_State *L) { lua_pushnumber(L, l_mathop(log10)(luaL_checknumber(L, 1))); return 1; @@ -702,7 +702,9 @@ static const luaL_Reg mathlib[] = { {"tointeger", math_toint}, {"floor", math_floor}, {"fmod", math_fmod}, + {"frexp", math_frexp}, {"ult", math_ult}, + {"ldexp", math_ldexp}, {"log", math_log}, {"max", math_max}, {"min", math_min}, @@ -718,8 +720,6 @@ static const luaL_Reg mathlib[] = { {"sinh", math_sinh}, {"tanh", math_tanh}, {"pow", math_pow}, - {"frexp", math_frexp}, - {"ldexp", math_ldexp}, {"log10", math_log10}, #endif /* placeholders */ diff --git a/manual/manual.of b/manual/manual.of index 61dd42f248..89e9b8f440 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -8341,6 +8341,17 @@ that rounds the quotient towards zero. (integer/float) } +@LibEntry{math.frexp (x)| + +Returns two numbers @id{m} and @id{e} such that @M{x = m2@sp{e}}, +where @id{e} is an integer. +When @id{x} is zero, NaN, +inf, or -inf, +@id{m} is equal to @id{x}; +otherwise, the absolute value of @id{m} +is in the range @C{(} @M{[0.5, 1)} @C{]}. + +} + @LibEntry{math.huge| The float value @idx{HUGE_VAL}, @@ -8348,6 +8359,12 @@ a value greater than any other numeric value. } +@LibEntry{math.ldexp(m, e)| + +Returns @M{m2@sp{e}}, where @id{e} is an integer. + +} + @LibEntry{math.log (x [, base])| Returns the logarithm of @id{x} in the given base. @@ -8403,7 +8420,7 @@ Converts the angle @id{x} from degrees to radians. When called without arguments, returns a pseudo-random float with uniform distribution -in the range @C{(} @M{[0,1)}. @C{]} +in the range @C{(} @M{[0, 1)}. @C{]} When called with two integers @id{m} and @id{n}, @id{math.random} returns a pseudo-random integer with uniform distribution in the range @M{[m, n]}. diff --git a/testes/math.lua b/testes/math.lua index 0d228d0988..54d19c4075 100644 --- a/testes/math.lua +++ b/testes/math.lua @@ -685,6 +685,18 @@ assert(eq(math.exp(0), 1)) assert(eq(math.sin(10), math.sin(10%(2*math.pi)))) +do print("testing ldexp/frexp") + global ipairs + for _, x in ipairs{0, 10, 32, -math.pi, 1e10, 1e-10, math.huge, -math.huge} do + local m, p = math.frexp(x) + assert(math.ldexp(m, p) == x) + local am = math.abs(m) + assert(m == x or (0.5 <= am and am < 1)) + end + +end + + assert(tonumber(' 1.3e-2 ') == 1.3e-2) assert(tonumber(' -1.00000000000001 ') == -1.00000000000001) From dd095677e38a104d0fd073f31530e08c9f5286fc Mon Sep 17 00:00:00 2001 From: Roberto I Date: Wed, 20 Aug 2025 13:59:08 -0300 Subject: [PATCH 1084/1145] Small cleaning services --- lfunc.c | 3 +-- lmathlib.c | 19 ++++++++++++++++--- lvm.c | 54 +++++++++++++++++++++++++++++------------------------- 3 files changed, 46 insertions(+), 30 deletions(-) diff --git a/lfunc.c b/lfunc.c index da7c623974..b6fd9ceb55 100644 --- a/lfunc.c +++ b/lfunc.c @@ -196,8 +196,7 @@ void luaF_unlinkupval (UpVal *uv) { */ void luaF_closeupval (lua_State *L, StkId level) { UpVal *uv; - StkId upl; /* stack index pointed by 'uv' */ - while ((uv = L->openupval) != NULL && (upl = uplevel(uv)) >= level) { + while ((uv = L->openupval) != NULL && uplevel(uv) >= level) { TValue *slot = &uv->u.value; /* new position for value */ lua_assert(uplevel(uv) < L->top.p); luaF_unlinkupval(uv); /* remove upvalue from 'openupval' list */ diff --git a/lmathlib.c b/lmathlib.c index b030f97ead..2f0f3d1bee 100644 --- a/lmathlib.c +++ b/lmathlib.c @@ -38,31 +38,37 @@ static int math_abs (lua_State *L) { return 1; } + static int math_sin (lua_State *L) { lua_pushnumber(L, l_mathop(sin)(luaL_checknumber(L, 1))); return 1; } + static int math_cos (lua_State *L) { lua_pushnumber(L, l_mathop(cos)(luaL_checknumber(L, 1))); return 1; } + static int math_tan (lua_State *L) { lua_pushnumber(L, l_mathop(tan)(luaL_checknumber(L, 1))); return 1; } + static int math_asin (lua_State *L) { lua_pushnumber(L, l_mathop(asin)(luaL_checknumber(L, 1))); return 1; } + static int math_acos (lua_State *L) { lua_pushnumber(L, l_mathop(acos)(luaL_checknumber(L, 1))); return 1; } + static int math_atan (lua_State *L) { lua_Number y = luaL_checknumber(L, 1); lua_Number x = luaL_optnumber(L, 2, 1); @@ -167,6 +173,7 @@ static int math_ult (lua_State *L) { return 1; } + static int math_log (lua_State *L) { lua_Number x = luaL_checknumber(L, 1); lua_Number res; @@ -188,28 +195,34 @@ static int math_log (lua_State *L) { return 1; } + static int math_exp (lua_State *L) { lua_pushnumber(L, l_mathop(exp)(luaL_checknumber(L, 1))); return 1; } + static int math_deg (lua_State *L) { lua_pushnumber(L, luaL_checknumber(L, 1) * (l_mathop(180.0) / PI)); return 1; } + static int math_rad (lua_State *L) { lua_pushnumber(L, luaL_checknumber(L, 1) * (PI / l_mathop(180.0))); return 1; } + static int math_frexp (lua_State *L) { - int e; - lua_pushnumber(L, l_mathop(frexp)(luaL_checknumber(L, 1), &e)); - lua_pushinteger(L, e); + lua_Number x = luaL_checknumber(L, 1); + int ep; + lua_pushnumber(L, l_mathop(frexp)(x, &ep)); + lua_pushinteger(L, ep); return 2; } + static int math_ldexp (lua_State *L) { lua_Number x = luaL_checknumber(L, 1); int ep = (int)luaL_checkinteger(L, 2); diff --git a/lvm.c b/lvm.c index cfdcf97a55..bde850ea16 100644 --- a/lvm.c +++ b/lvm.c @@ -910,6 +910,10 @@ void luaV_finishOp (lua_State *L) { /* ** {================================================================== ** Macros for arithmetic/bitwise/comparison opcodes in 'luaV_execute' +** +** All these macros are to be used exclusively inside the main +** iterpreter loop (function luaV_execute) and may access directly +** the local variables of that function (L, i, pc, ci, etc.). ** =================================================================== */ @@ -931,17 +935,17 @@ void luaV_finishOp (lua_State *L) { ** operation, 'fop' is the float operation. */ #define op_arithI(L,iop,fop) { \ - StkId ra = RA(i); \ + TValue *ra = vRA(i); \ TValue *v1 = vRB(i); \ int imm = GETARG_sC(i); \ if (ttisinteger(v1)) { \ lua_Integer iv1 = ivalue(v1); \ - pc++; setivalue(s2v(ra), iop(L, iv1, imm)); \ + pc++; setivalue(ra, iop(L, iv1, imm)); \ } \ else if (ttisfloat(v1)) { \ lua_Number nb = fltvalue(v1); \ lua_Number fimm = cast_num(imm); \ - pc++; setfltvalue(s2v(ra), fop(L, nb, fimm)); \ + pc++; setfltvalue(ra, fop(L, nb, fimm)); \ }} @@ -952,6 +956,7 @@ void luaV_finishOp (lua_State *L) { #define op_arithf_aux(L,v1,v2,fop) { \ lua_Number n1; lua_Number n2; \ if (tonumberns(v1, n1) && tonumberns(v2, n2)) { \ + StkId ra = RA(i); \ pc++; setfltvalue(s2v(ra), fop(L, n1, n2)); \ }} @@ -960,7 +965,6 @@ void luaV_finishOp (lua_State *L) { ** Arithmetic operations over floats and others with register operands. */ #define op_arithf(L,fop) { \ - StkId ra = RA(i); \ TValue *v1 = vRB(i); \ TValue *v2 = vRC(i); \ op_arithf_aux(L, v1, v2, fop); } @@ -970,7 +974,6 @@ void luaV_finishOp (lua_State *L) { ** Arithmetic operations with K operands for floats. */ #define op_arithfK(L,fop) { \ - StkId ra = RA(i); \ TValue *v1 = vRB(i); \ TValue *v2 = KC(i); lua_assert(ttisnumber(v2)); \ op_arithf_aux(L, v1, v2, fop); } @@ -980,8 +983,8 @@ void luaV_finishOp (lua_State *L) { ** Arithmetic operations over integers and floats. */ #define op_arith_aux(L,v1,v2,iop,fop) { \ - StkId ra = RA(i); \ if (ttisinteger(v1) && ttisinteger(v2)) { \ + StkId ra = RA(i); \ lua_Integer i1 = ivalue(v1); lua_Integer i2 = ivalue(v2); \ pc++; setivalue(s2v(ra), iop(L, i1, i2)); \ } \ @@ -1010,12 +1013,12 @@ void luaV_finishOp (lua_State *L) { ** Bitwise operations with constant operand. */ #define op_bitwiseK(L,op) { \ - StkId ra = RA(i); \ TValue *v1 = vRB(i); \ TValue *v2 = KC(i); \ lua_Integer i1; \ lua_Integer i2 = ivalue(v2); \ if (tointegerns(v1, &i1)) { \ + StkId ra = RA(i); \ pc++; setivalue(s2v(ra), op(i1, i2)); \ }} @@ -1024,11 +1027,11 @@ void luaV_finishOp (lua_State *L) { ** Bitwise operations with register operands. */ #define op_bitwise(L,op) { \ - StkId ra = RA(i); \ TValue *v1 = vRB(i); \ TValue *v2 = vRC(i); \ lua_Integer i1; lua_Integer i2; \ if (tointegerns(v1, &i1) && tointegerns(v2, &i2)) { \ + StkId ra = RA(i); \ pc++; setivalue(s2v(ra), op(i1, i2)); \ }} @@ -1039,18 +1042,18 @@ void luaV_finishOp (lua_State *L) { ** integers. */ #define op_order(L,opi,opn,other) { \ - StkId ra = RA(i); \ + TValue *ra = vRA(i); \ int cond; \ TValue *rb = vRB(i); \ - if (ttisinteger(s2v(ra)) && ttisinteger(rb)) { \ - lua_Integer ia = ivalue(s2v(ra)); \ + if (ttisinteger(ra) && ttisinteger(rb)) { \ + lua_Integer ia = ivalue(ra); \ lua_Integer ib = ivalue(rb); \ cond = opi(ia, ib); \ } \ - else if (ttisnumber(s2v(ra)) && ttisnumber(rb)) \ - cond = opn(s2v(ra), rb); \ + else if (ttisnumber(ra) && ttisnumber(rb)) \ + cond = opn(ra, rb); \ else \ - Protect(cond = other(L, s2v(ra), rb)); \ + Protect(cond = other(L, ra, rb)); \ docondjump(); } @@ -1059,19 +1062,19 @@ void luaV_finishOp (lua_State *L) { ** always small enough to have an exact representation as a float.) */ #define op_orderI(L,opi,opf,inv,tm) { \ - StkId ra = RA(i); \ + TValue *ra = vRA(i); \ int cond; \ int im = GETARG_sB(i); \ - if (ttisinteger(s2v(ra))) \ - cond = opi(ivalue(s2v(ra)), im); \ - else if (ttisfloat(s2v(ra))) { \ - lua_Number fa = fltvalue(s2v(ra)); \ + if (ttisinteger(ra)) \ + cond = opi(ivalue(ra), im); \ + else if (ttisfloat(ra)) { \ + lua_Number fa = fltvalue(ra); \ lua_Number fim = cast_num(im); \ cond = opf(fa, fim); \ } \ else { \ int isf = GETARG_C(i); \ - Protect(cond = luaT_callorderiTM(L, s2v(ra), im, inv, isf, tm)); \ + Protect(cond = luaT_callorderiTM(L, ra, im, inv, isf, tm)); \ } \ docondjump(); } @@ -1090,6 +1093,7 @@ void luaV_finishOp (lua_State *L) { #define RA(i) (base+GETARG_A(i)) +#define vRA(i) s2v(RA(i)) #define RB(i) (base+GETARG_B(i)) #define vRB(i) s2v(RB(i)) #define KB(i) (k+GETARG_B(i)) @@ -1130,14 +1134,14 @@ void luaV_finishOp (lua_State *L) { /* ** Correct global 'pc'. */ -#define savepc(L) (ci->u.l.savedpc = pc) +#define savepc(ci) (ci->u.l.savedpc = pc) /* ** Whenever code can raise errors, the global 'pc' and the global ** 'top' must be correct to report occasional errors. */ -#define savestate(L,ci) (savepc(L), L->top.p = ci->top.p) +#define savestate(L,ci) (savepc(ci), L->top.p = ci->top.p) /* @@ -1147,7 +1151,7 @@ void luaV_finishOp (lua_State *L) { #define Protect(exp) (savestate(L,ci), (exp), updatetrap(ci)) /* special version that does not change the top */ -#define ProtectNT(exp) (savepc(L), (exp), updatetrap(ci)) +#define ProtectNT(exp) (savepc(ci), (exp), updatetrap(ci)) /* ** Protect code that can only raise errors. (That is, it cannot change @@ -1165,7 +1169,7 @@ void luaV_finishOp (lua_State *L) { /* 'c' is the limit of live values in the stack */ #define checkGC(L,c) \ - { luaC_condGC(L, (savepc(L), L->top.p = (c)), \ + { luaC_condGC(L, (savepc(ci), L->top.p = (c)), \ updatetrap(ci)); \ luai_threadyield(L); } @@ -1714,7 +1718,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { if (b != 0) /* fixed number of arguments? */ L->top.p = ra + b; /* top signals number of arguments */ /* else previous instruction set top */ - savepc(L); /* in case of errors */ + savepc(ci); /* in case of errors */ if ((newci = luaD_precall(L, ra, nresults)) == NULL) updatetrap(ci); /* C call; nothing else to be done */ else { /* Lua call: run function in this same C frame */ From c688b00f73205273c46d7cf5656ff5a27e90ce36 Mon Sep 17 00:00:00 2001 From: Roberto I Date: Wed, 20 Aug 2025 14:18:12 -0300 Subject: [PATCH 1085/1145] Detail in 'obj2gco' Its check should use the type of the object, not its tag. (Change only relevant in test mode.) --- lstate.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lstate.h b/lstate.h index 80df3b0a95..a4d5570c78 100644 --- a/lstate.h +++ b/lstate.h @@ -430,9 +430,9 @@ union GCUnion { /* ** macro to convert a Lua object into a GCObject -** (The access to 'tt' tries to ensure that 'v' is actually a Lua object.) */ -#define obj2gco(v) check_exp((v)->tt >= LUA_TSTRING, &(cast_u(v)->gc)) +#define obj2gco(v) \ + check_exp(novariant((v)->tt) >= LUA_TSTRING, &(cast_u(v)->gc)) /* actual number of total memory allocated */ From 88aa4049ad3e638571bfffcf5fd8b6a8e07c6aaf Mon Sep 17 00:00:00 2001 From: Roberto I Date: Wed, 20 Aug 2025 14:31:07 -0300 Subject: [PATCH 1086/1145] Keep the order left-right in shifts Opcodes OP_SHLI-OP_SHRI and the cases for opcodes OP_SHL-OP_SHR were out of order. --- ljumptab.h | 2 +- lopcodes.c | 2 +- lopcodes.h | 2 +- lopnames.h | 2 +- lvm.c | 16 ++++++++-------- 5 files changed, 12 insertions(+), 12 deletions(-) diff --git a/ljumptab.h b/ljumptab.h index 8306f250cc..a24828bb5a 100644 --- a/ljumptab.h +++ b/ljumptab.h @@ -57,8 +57,8 @@ static const void *const disptab[NUM_OPCODES] = { &&L_OP_BANDK, &&L_OP_BORK, &&L_OP_BXORK, -&&L_OP_SHRI, &&L_OP_SHLI, +&&L_OP_SHRI, &&L_OP_ADD, &&L_OP_SUB, &&L_OP_MUL, diff --git a/lopcodes.c b/lopcodes.c index 092c390206..79ffbe2590 100644 --- a/lopcodes.c +++ b/lopcodes.c @@ -53,8 +53,8 @@ LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = { ,opmode(0, 0, 0, 0, 1, iABC) /* OP_BANDK */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_BORK */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_BXORK */ - ,opmode(0, 0, 0, 0, 1, iABC) /* OP_SHRI */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_SHLI */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_SHRI */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_ADD */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_SUB */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_MUL */ diff --git a/lopcodes.h b/lopcodes.h index 9787003846..9ad21021ea 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -272,8 +272,8 @@ OP_BANDK,/* A B C R[A] := R[B] & K[C]:integer */ OP_BORK,/* A B C R[A] := R[B] | K[C]:integer */ OP_BXORK,/* A B C R[A] := R[B] ~ K[C]:integer */ -OP_SHRI,/* A B sC R[A] := R[B] >> sC */ OP_SHLI,/* A B sC R[A] := sC << R[B] */ +OP_SHRI,/* A B sC R[A] := R[B] >> sC */ OP_ADD,/* A B C R[A] := R[B] + R[C] */ OP_SUB,/* A B C R[A] := R[B] - R[C] */ diff --git a/lopnames.h b/lopnames.h index 965cec9bf2..39df332f5e 100644 --- a/lopnames.h +++ b/lopnames.h @@ -45,8 +45,8 @@ static const char *const opnames[] = { "BANDK", "BORK", "BXORK", - "SHRI", "SHLI", + "SHRI", "ADD", "SUB", "MUL", diff --git a/lvm.c b/lvm.c index bde850ea16..8ad4344a39 100644 --- a/lvm.c +++ b/lvm.c @@ -1476,23 +1476,23 @@ void luaV_execute (lua_State *L, CallInfo *ci) { op_bitwiseK(L, l_bxor); vmbreak; } - vmcase(OP_SHRI) { + vmcase(OP_SHLI) { StkId ra = RA(i); TValue *rb = vRB(i); int ic = GETARG_sC(i); lua_Integer ib; if (tointegerns(rb, &ib)) { - pc++; setivalue(s2v(ra), luaV_shiftl(ib, -ic)); + pc++; setivalue(s2v(ra), luaV_shiftl(ic, ib)); } vmbreak; } - vmcase(OP_SHLI) { + vmcase(OP_SHRI) { StkId ra = RA(i); TValue *rb = vRB(i); int ic = GETARG_sC(i); lua_Integer ib; if (tointegerns(rb, &ib)) { - pc++; setivalue(s2v(ra), luaV_shiftl(ic, ib)); + pc++; setivalue(s2v(ra), luaV_shiftl(ib, -ic)); } vmbreak; } @@ -1538,14 +1538,14 @@ void luaV_execute (lua_State *L, CallInfo *ci) { op_bitwise(L, l_bxor); vmbreak; } - vmcase(OP_SHR) { - op_bitwise(L, luaV_shiftr); - vmbreak; - } vmcase(OP_SHL) { op_bitwise(L, luaV_shiftl); vmbreak; } + vmcase(OP_SHR) { + op_bitwise(L, luaV_shiftr); + vmbreak; + } vmcase(OP_MMBIN) { StkId ra = RA(i); Instruction pi = *(pc - 2); /* original arith. expression */ From 907d172c1114a2d61e85e1ca7aba50ef1fc4ffe3 Mon Sep 17 00:00:00 2001 From: Roberto I Date: Wed, 20 Aug 2025 15:00:53 -0300 Subject: [PATCH 1087/1145] Added lock/unlock to API function 'lua_rawlen' The call to 'luaH_getn' can change the "field" 'lenhint' of a table. --- lapi.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lapi.c b/lapi.c index 0c88751a2e..55e371dcc9 100644 --- a/lapi.c +++ b/lapi.c @@ -440,7 +440,13 @@ LUA_API lua_Unsigned lua_rawlen (lua_State *L, int idx) { case LUA_VSHRSTR: return cast(lua_Unsigned, tsvalue(o)->shrlen); case LUA_VLNGSTR: return cast(lua_Unsigned, tsvalue(o)->u.lnglen); case LUA_VUSERDATA: return cast(lua_Unsigned, uvalue(o)->len); - case LUA_VTABLE: return luaH_getn(L, hvalue(o)); + case LUA_VTABLE: { + lua_Unsigned res; + lua_lock(L); + res = luaH_getn(L, hvalue(o)); + lua_unlock(L); + return res; + } default: return 0; } } From c345877e4c2588324d9a1e5655e8f48200ba2e5e Mon Sep 17 00:00:00 2001 From: Roberto I Date: Wed, 20 Aug 2025 15:29:46 -0300 Subject: [PATCH 1088/1145] Better documentation for LUA_ERRERR Not all errors in a message handler generate a LUA_ERRERR. --- ldo.c | 4 ++-- manual/manual.of | 13 +++++++++++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/ldo.c b/ldo.c index f232b588a9..dff9488e96 100644 --- a/ldo.c +++ b/ldo.c @@ -203,7 +203,7 @@ TStatus luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) { #define ERRORSTACKSIZE (MAXSTACK + STACKERRSPACE) -/* raise an error while running the message handler */ +/* raise a stack error while running the message handler */ l_noret luaD_errerr (lua_State *L) { TString *msg = luaS_newliteral(L, "error in error handling"); setsvalue2s(L, L->top.p, msg); @@ -339,7 +339,7 @@ int luaD_growstack (lua_State *L, int n, int raiseerror) { a stack error; cannot grow further than that. */ lua_assert(stacksize(L) == ERRORSTACKSIZE); if (raiseerror) - luaD_errerr(L); /* error inside message handler */ + luaD_errerr(L); /* stack error inside message handler */ return 0; /* if not 'raiseerror', just signal it */ } else if (n < MAXSTACK) { /* avoids arithmetic overflows */ diff --git a/manual/manual.of b/manual/manual.of index 89e9b8f440..3c7041182a 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -270,7 +270,7 @@ print(x) --> 10 (the global one) Notice that, in a declaration like @T{local x = x}, the new @id{x} being declared is not in scope yet, -and so the @id{x} in the right-hand side refers to the outside variable. +and so the @id{x} on the right-hand side refers to the outside variable. Because of the @x{lexical scoping} rules, local variables can be freely accessed by functions @@ -2826,7 +2826,16 @@ status codes to indicate different kinds of errors or other conditions: For such errors, Lua does not call the @x{message handler}. } -@item{@defid{LUA_ERRERR}| error while running the @x{message handler}.} +@item{@defid{LUA_ERRERR}| +stack overflow while running the @x{message handler} +due to another stack overflow. +More often than not, +this error is the result of some other error while running +a message handler. +An error in a message handler will call the handler again, +which will generate the error again, and so on, +until this loop exhausts the stack and cause this error. +} @item{@defid{LUA_ERRSYNTAX}| syntax error during precompilation or format error in a binary chunk.} From 06c5d3825f03eafc90b56d43f70f70048b785bc8 Mon Sep 17 00:00:00 2001 From: Roberto I Date: Wed, 20 Aug 2025 16:10:54 -0300 Subject: [PATCH 1089/1145] Removed code for compatibility with version 5.3 --- lstate.h | 4 ---- ltests.h | 1 - ltm.c | 16 ---------------- lua.h | 7 ------- luaconf.h | 27 +-------------------------- lvm.c | 6 ------ testes/api.lua | 3 ++- 7 files changed, 3 insertions(+), 61 deletions(-) diff --git a/lstate.h b/lstate.h index a4d5570c78..20dc4d24f0 100644 --- a/lstate.h +++ b/lstate.h @@ -249,10 +249,6 @@ struct CallInfo { #define CIST_HOOKYIELD (CIST_TAIL << 1) /* function "called" a finalizer */ #define CIST_FIN (CIST_HOOKYIELD << 1) -#if defined(LUA_COMPAT_LT_LE) -/* using __lt for __le */ -#define CIST_LEQ (CIST_FIN << 1) -#endif #define get_nresults(cs) (cast_int((cs) & CIST_NRESULTS) - 1) diff --git a/ltests.h b/ltests.h index c825bdcfc1..305f561976 100644 --- a/ltests.h +++ b/ltests.h @@ -13,7 +13,6 @@ /* test Lua with compatibility code */ #define LUA_COMPAT_MATHLIB -#define LUA_COMPAT_LT_LE #undef LUA_COMPAT_GLOBAL diff --git a/ltm.c b/ltm.c index 8eca2d6e1f..d1a61a6250 100644 --- a/ltm.c +++ b/ltm.c @@ -196,28 +196,12 @@ void luaT_trybiniTM (lua_State *L, const TValue *p1, lua_Integer i2, /* ** Calls an order tag method. -** For lessequal, LUA_COMPAT_LT_LE keeps compatibility with old -** behavior: if there is no '__le', try '__lt', based on l <= r iff -** !(r < l) (assuming a total order). If the metamethod yields during -** this substitution, the continuation has to know about it (to negate -** the result of rtop.p, event); /* try original event */ if (tag >= 0) /* found tag method? */ return !tagisfalse(tag); -#if defined(LUA_COMPAT_LT_LE) - else if (event == TM_LE) { - /* try '!(p2 < p1)' for '(p1 <= p2)' */ - L->ci->callstatus |= CIST_LEQ; /* mark it is doing 'lt' for 'le' */ - tag = callbinTM(L, p2, p1, L->top.p, TM_LT); - L->ci->callstatus ^= CIST_LEQ; /* clear mark */ - if (tag >= 0) /* found tag method? */ - return tagisfalse(tag); - } -#endif luaG_ordererror(L, p1, p2); /* no metamethod found */ return 0; /* to avoid warnings */ } diff --git a/lua.h b/lua.h index 131a8fcb9f..ab473dc3e4 100644 --- a/lua.h +++ b/lua.h @@ -432,13 +432,6 @@ LUA_API void (lua_closeslot) (lua_State *L, int idx); ** compatibility macros ** =============================================================== */ -#if defined(LUA_COMPAT_APIINTCASTS) - -#define lua_pushunsigned(L,n) lua_pushinteger(L, (lua_Integer)(n)) -#define lua_tounsignedx(L,i,is) ((lua_Unsigned)lua_tointegerx(L,i,is)) -#define lua_tounsigned(L,i) lua_tounsignedx(L,(i),NULL) - -#endif #define lua_newuserdata(L,s) lua_newuserdatauv(L,s,1) #define lua_getuservalue(L,idx) lua_getiuservalue(L,idx,1) diff --git a/luaconf.h b/luaconf.h index 0adc9c13f1..56d2916519 100644 --- a/luaconf.h +++ b/luaconf.h @@ -361,36 +361,13 @@ #define LUA_COMPAT_GLOBAL -/* -@@ LUA_COMPAT_5_3 controls other macros for compatibility with Lua 5.3. -** You can define it to get all options, or change specific options -** to fit your specific needs. -*/ -#if defined(LUA_COMPAT_5_3) /* { */ - /* @@ LUA_COMPAT_MATHLIB controls the presence of several deprecated ** functions in the mathematical library. ** (These functions were already officially removed in 5.3; ** nevertheless they are still available here.) */ -#define LUA_COMPAT_MATHLIB - -/* -@@ LUA_COMPAT_APIINTCASTS controls the presence of macros for -** manipulating other integer types (lua_pushunsigned, lua_tounsigned, -** luaL_checkint, luaL_checklong, etc.) -** (These macros were also officially removed in 5.3, but they are still -** available here.) -*/ -#define LUA_COMPAT_APIINTCASTS - - -/* -@@ LUA_COMPAT_LT_LE controls the emulation of the '__le' metamethod -** using '__lt'. -*/ -#define LUA_COMPAT_LT_LE +/* #define LUA_COMPAT_MATHLIB */ /* @@ -407,8 +384,6 @@ #define lua_equal(L,idx1,idx2) lua_compare(L,(idx1),(idx2),LUA_OPEQ) #define lua_lessthan(L,idx1,idx2) lua_compare(L,(idx1),(idx2),LUA_OPLT) -#endif /* } */ - /* }================================================================== */ diff --git a/lvm.c b/lvm.c index 8ad4344a39..a9de5cbccc 100644 --- a/lvm.c +++ b/lvm.c @@ -861,12 +861,6 @@ void luaV_finishOp (lua_State *L) { case OP_EQ: { /* note that 'OP_EQI'/'OP_EQK' cannot yield */ int res = !l_isfalse(s2v(L->top.p - 1)); L->top.p--; -#if defined(LUA_COMPAT_LT_LE) - if (ci->callstatus & CIST_LEQ) { /* "<=" using "<" instead? */ - ci->callstatus ^= CIST_LEQ; /* clear mark */ - res = !res; /* negate result */ - } -#endif lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_JMP); if (res != GETARG_k(inst)) /* condition failed? */ ci->u.l.savedpc++; /* skip jump instruction */ diff --git a/testes/api.lua b/testes/api.lua index b3791654d4..9855f5411d 100644 --- a/testes/api.lua +++ b/testes/api.lua @@ -246,7 +246,8 @@ assert(not T.testC("compare LT 1 4, return 1")) assert(not T.testC("compare LE 9 1, return 1")) assert(not T.testC("compare EQ 9 9, return 1")) -local b = {__lt = function (a,b) return a[1] < b[1] end} +local b = {__lt = function (a,b) return a[1] < b[1] end, + __le = function (a,b) return a[1] <= b[1] end} local a1,a3,a4 = setmetatable({1}, b), setmetatable({3}, b), setmetatable({4}, b) From 13d2326a23a6cde050c17c5b856f962e93e06f3d Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 21 Aug 2025 10:51:17 -0300 Subject: [PATCH 1090/1145] Some definitions moved from luaconf.h to llimits.h These definitions were in luaconf.h only because the standard libraries need them. Now that llimits.h is included by the libraries, it offers a more private place for these definitions. --- llimits.h | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ luaconf.h | 51 +-------------------------------------------------- 2 files changed, 50 insertions(+), 50 deletions(-) diff --git a/llimits.h b/llimits.h index c4719a1507..d115496f7a 100644 --- a/llimits.h +++ b/llimits.h @@ -287,6 +287,55 @@ typedef unsigned long l_uint32; #endif + +/* +** lua_numbertointeger converts a float number with an integral value +** to an integer, or returns 0 if the float is not within the range of +** a lua_Integer. (The range comparisons are tricky because of +** rounding. The tests here assume a two-complement representation, +** where MININTEGER always has an exact representation as a float; +** MAXINTEGER may not have one, and therefore its conversion to float +** may have an ill-defined value.) +*/ +#define lua_numbertointeger(n,p) \ + ((n) >= (LUA_NUMBER)(LUA_MININTEGER) && \ + (n) < -(LUA_NUMBER)(LUA_MININTEGER) && \ + (*(p) = (LUA_INTEGER)(n), 1)) + + + +/* +** LUAI_FUNC is a mark for all extern functions that are not to be +** exported to outside modules. +** LUAI_DDEF and LUAI_DDEC are marks for all extern (const) variables, +** none of which to be exported to outside modules (LUAI_DDEF for +** definitions and LUAI_DDEC for declarations). +** Elf/gcc (versions 3.2 and later) mark them as "hidden" to optimize +** access when Lua is compiled as a shared library. Not all elf targets +** support this attribute. Unfortunately, gcc does not offer a way to +** check whether the target offers that support, and those without +** support give a warning about it. To avoid these warnings, change to +** the default definition. +*/ +#if !defined(LUAI_FUNC) + +#if defined(__GNUC__) && ((__GNUC__*100 + __GNUC_MINOR__) >= 302) && \ + defined(__ELF__) /* { */ +#define LUAI_FUNC __attribute__((visibility("internal"))) extern +#else /* }{ */ +#define LUAI_FUNC extern +#endif /* } */ + +#define LUAI_DDEC(dec) LUAI_FUNC dec +#define LUAI_DDEF /* empty */ + +#endif + + +/* Give these macros simpler names for internal use */ +#define l_likely(x) luai_likely(x) +#define l_unlikely(x) luai_unlikely(x) + /* ** {================================================================== ** "Abstraction Layer" for basic report of messages and errors diff --git a/luaconf.h b/luaconf.h index 56d2916519..b42da518fc 100644 --- a/luaconf.h +++ b/luaconf.h @@ -321,31 +321,6 @@ #define LUALIB_API LUA_API #define LUAMOD_API LUA_API - -/* -@@ LUAI_FUNC is a mark for all extern functions that are not to be -** exported to outside modules. -@@ LUAI_DDEF and LUAI_DDEC are marks for all extern (const) variables, -** none of which to be exported to outside modules (LUAI_DDEF for -** definitions and LUAI_DDEC for declarations). -** CHANGE them if you need to mark them in some special way. Elf/gcc -** (versions 3.2 and later) mark them as "hidden" to optimize access -** when Lua is compiled as a shared library. Not all elf targets support -** this attribute. Unfortunately, gcc does not offer a way to check -** whether the target offers that support, and those without support -** give a warning about it. To avoid these warnings, change to the -** default definition. -*/ -#if defined(__GNUC__) && ((__GNUC__*100 + __GNUC_MINOR__) >= 302) && \ - defined(__ELF__) /* { */ -#define LUAI_FUNC __attribute__((visibility("internal"))) extern -#else /* }{ */ -#define LUAI_FUNC extern -#endif /* } */ - -#define LUAI_DDEC(dec) LUAI_FUNC dec -#define LUAI_DDEF /* empty */ - /* }================================================================== */ @@ -415,26 +390,11 @@ */ -/* The following definitions are good for most cases here */ +/* The following definition is good for most cases here */ #define l_floor(x) (l_mathop(floor)(x)) -/* -@@ lua_numbertointeger converts a float number with an integral value -** to an integer, or returns 0 if float is not within the range of -** a lua_Integer. (The range comparisons are tricky because of -** rounding. The tests here assume a two-complement representation, -** where MININTEGER always has an exact representation as a float; -** MAXINTEGER may not have one, and therefore its conversion to float -** may have an ill-defined value.) -*/ -#define lua_numbertointeger(n,p) \ - ((n) >= (LUA_NUMBER)(LUA_MININTEGER) && \ - (n) < -(LUA_NUMBER)(LUA_MININTEGER) && \ - (*(p) = (LUA_INTEGER)(n), 1)) - - /* now the variable definitions */ #if LUA_FLOAT_TYPE == LUA_FLOAT_FLOAT /* { single float */ @@ -694,13 +654,6 @@ #endif -#if defined(LUA_CORE) || defined(LUA_LIB) -/* shorter names for Lua's own use */ -#define l_likely(x) luai_likely(x) -#define l_unlikely(x) luai_unlikely(x) -#endif - - /* }================================================================== */ @@ -782,7 +735,5 @@ - - #endif From 711890624fae4508d1cb5b4d358817bf4ebcfcb2 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 26 Aug 2025 12:30:05 -0300 Subject: [PATCH 1091/1145] update in 'onelua.c' --- onelua.c | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/onelua.c b/onelua.c index cc639494cc..e717121391 100644 --- a/onelua.c +++ b/onelua.c @@ -5,10 +5,14 @@ ** ** $ gcc -O2 -std=c99 -o lua onelua.c -lm ** -** or +** or (for C89) ** ** $ gcc -O2 -std=c89 -DLUA_USE_C89 -o lua onelua.c -lm ** +** or (for Linux) +** +** gcc -O2 -o lua -DLUA_USE_LINUX -Wl,-E onelua.c -lm -ldl +** */ /* default is to build the full interpreter */ @@ -30,7 +34,15 @@ #define LUA_USE_LINUX #define LUA_USE_MACOSX #define LUA_USE_POSIX -#define LUA_ANSI +#endif + + +/* +** Other specific features +*/ +#if 0 +#define LUA_32BITS +#define LUA_USE_C89 #endif @@ -54,12 +66,10 @@ #include #include - /* setup for luaconf.h */ #define LUA_CORE #define LUA_LIB -#define ltable_c -#define lvm_c + #include "luaconf.h" /* do not export internal symbols */ From 9a3940380a2a1540dc500593a6de0c1c5e6feb69 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 26 Aug 2025 12:30:34 -0300 Subject: [PATCH 1092/1145] New compile option LUA_USE_OFF_T Allows non-Posix systems to use off_t and related functions for file offsets. --- liolib.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/liolib.c b/liolib.c index c8f165cb05..57615e6f32 100644 --- a/liolib.c +++ b/liolib.c @@ -114,7 +114,7 @@ static int l_checkmode (const char *mode) { #if !defined(l_fseek) /* { */ -#if defined(LUA_USE_POSIX) /* { */ +#if defined(LUA_USE_POSIX) || defined(LUA_USE_OFF_T) /* { */ #include From 03a3473687ef0df86fc6d31de7d46158a0436666 Mon Sep 17 00:00:00 2001 From: Roberto I Date: Wed, 27 Aug 2025 10:28:31 -0300 Subject: [PATCH 1093/1145] 'ltests.h' should not use LUAI_FUNC LUAI_FUNC is now defined in llimits.h. --- ltests.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/ltests.h b/ltests.h index 305f561976..26ffed83de 100644 --- a/ltests.h +++ b/ltests.h @@ -63,7 +63,7 @@ LUA_API Memcontrol l_memcontrol; #define luai_tracegc(L,f) luai_tracegctest(L, f) -LUAI_FUNC void luai_tracegctest (lua_State *L, int first); +extern void luai_tracegctest (lua_State *L, int first); /* @@ -75,26 +75,26 @@ extern void *l_Trick; /* ** Function to traverse and check all memory used by Lua */ -LUAI_FUNC int lua_checkmemory (lua_State *L); +extern int lua_checkmemory (lua_State *L); /* ** Function to print an object GC-friendly */ struct GCObject; -LUAI_FUNC void lua_printobj (lua_State *L, struct GCObject *o); +extern void lua_printobj (lua_State *L, struct GCObject *o); /* ** Function to print a value */ struct TValue; -LUAI_FUNC void lua_printvalue (struct TValue *v); +extern void lua_printvalue (struct TValue *v); /* ** Function to print the stack */ -LUAI_FUNC void lua_printstack (lua_State *L); -LUAI_FUNC int lua_printallstack (lua_State *L); +extern void lua_printstack (lua_State *L); +extern int lua_printallstack (lua_State *L); /* test for lock/unlock */ From f87416f1a3e47aa69ed8d27e7406ec6b7848da9a Mon Sep 17 00:00:00 2001 From: Roberto I Date: Wed, 27 Aug 2025 10:30:54 -0300 Subject: [PATCH 1094/1145] Added limit to number of elements in a constructor The reasoning in commit 519c57d5 is wrong: A sequence of nils generates several fields with just one OP_LOADNIL. --- lparser.c | 21 ++++++++++++++++++--- lvm.c | 2 +- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/lparser.c b/lparser.c index 73dad6d77d..5abcd407cb 100644 --- a/lparser.c +++ b/lparser.c @@ -905,6 +905,19 @@ typedef struct ConsControl { } ConsControl; +/* +** Maximum number of elements in a constructor, to control the following: +** * counter overflows; +** * overflows in 'extra' for OP_NEWTABLE and OP_SETLIST; +** * overflows when adding multiple returns in OP_SETLIST. +*/ +#define MAX_CNST (INT_MAX/2) +#if MAX_CNST/(MAXARG_vC + 1) > MAXARG_Ax +#undef MAX_CNST +#define MAX_CNST (MAXARG_Ax * (MAXARG_vC + 1)) +#endif + + static void recfield (LexState *ls, ConsControl *cc) { /* recfield -> (NAME | '['exp']') = exp */ FuncState *fs = ls->fs; @@ -925,7 +938,7 @@ static void recfield (LexState *ls, ConsControl *cc) { static void closelistfield (FuncState *fs, ConsControl *cc) { - if (cc->v.k == VVOID) return; /* there is no list item */ + lua_assert(cc->tostore > 0); luaK_exp2nextreg(fs, &cc->v); cc->v.k = VVOID; if (cc->tostore >= cc->maxtostore) { @@ -1013,10 +1026,12 @@ static void constructor (LexState *ls, expdesc *t) { checknext(ls, '{' /*}*/); cc.maxtostore = maxtostore(fs); do { - lua_assert(cc.v.k == VVOID || cc.tostore > 0); if (ls->t.token == /*{*/ '}') break; - closelistfield(fs, &cc); + if (cc.v.k != VVOID) /* is there a previous list item? */ + closelistfield(fs, &cc); /* close it */ field(ls, &cc); + luaY_checklimit(fs, cc.tostore + cc.na + cc.nh, MAX_CNST, + "items in a constructor"); } while (testnext(ls, ',') || testnext(ls, ';')); check_match(ls, /*{*/ '}', '{' /*}*/, line); lastlistfield(fs, &cc); diff --git a/lvm.c b/lvm.c index a9de5cbccc..d0a1c05d0b 100644 --- a/lvm.c +++ b/lvm.c @@ -1888,7 +1888,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmcase(OP_SETLIST) { StkId ra = RA(i); unsigned n = cast_uint(GETARG_vB(i)); - unsigned int last = cast_uint(GETARG_vC(i)); + unsigned last = cast_uint(GETARG_vC(i)); Table *h = hvalue(s2v(ra)); if (n == 0) n = cast_uint(L->top.p - ra) - 1; /* get up to the top */ From 0b73ed8f083c99b5ff88e0822532db7ad8785881 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Sat, 30 Aug 2025 16:16:02 -0300 Subject: [PATCH 1095/1145] Allows LUA_32BITS to be defined externally An external definition for LUA_32BITS can change the API, but libraries check number-format compatibility when loading. So, any incompatible modules will report a clear error. --- luaconf.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/luaconf.h b/luaconf.h index b42da518fc..1ac64328e6 100644 --- a/luaconf.h +++ b/luaconf.h @@ -138,7 +138,7 @@ /* @@ LUA_32BITS enables Lua with 32-bit integers and 32-bit floats. */ -#define LUA_32BITS 0 +/* #define LUA_32BITS */ /* @@ -153,7 +153,7 @@ #endif -#if LUA_32BITS /* { */ +#if defined(LUA_32BITS) /* { */ /* ** 32-bit integers and 'float' */ From ffbcadfb4197213d55222bca3ecc52606cd980f4 Mon Sep 17 00:00:00 2001 From: Roberto I Date: Fri, 5 Sep 2025 15:29:15 -0300 Subject: [PATCH 1096/1145] In C++, 'throw' must go to the correct handler. In C, we may have several "setjmp" nested, and the "longjmp" will go to the one given by the corresponding "jmp_buf". In C++, a "throw" will always go to the inner "catch". So, the "catch" must check whether it is the recipient of the "throw" and, if not, rethrow the exception to the outer level. --- ldo.c | 42 +++++++++++++++++++++++++----------------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/ldo.c b/ldo.c index dff9488e96..44937068f8 100644 --- a/ldo.c +++ b/ldo.c @@ -57,10 +57,18 @@ ** ======================================================= */ +/* chained list of long jump buffers */ +typedef struct lua_longjmp { + struct lua_longjmp *previous; + jmp_buf b; + volatile TStatus status; /* error code */ +} lua_longjmp; + + /* ** LUAI_THROW/LUAI_TRY define how Lua does exception handling. By ** default, Lua handles errors with exceptions when compiling as -** C++ code, with _longjmp/_setjmp when asked to use them, and with +** C++ code, with _longjmp/_setjmp when available (POSIX), and with ** longjmp/setjmp otherwise. */ #if !defined(LUAI_THROW) /* { */ @@ -69,38 +77,38 @@ /* C++ exceptions */ #define LUAI_THROW(L,c) throw(c) -#define LUAI_TRY(L,c,f,ud) \ - try { (f)(L, ud); } catch(...) { if ((c)->status == 0) (c)->status = -1; } -#define luai_jmpbuf int /* dummy field */ + +static void LUAI_TRY (lua_State *L, lua_longjmp *c, Pfunc f, void *ud) { + try { + f(L, ud); /* call function protected */ + } + catch (lua_longjmp *c1) { /* Lua error */ + if (c1 != c) /* not the correct level? */ + throw; /* rethrow to upper level */ + } + catch (...) { /* non-Lua exception */ + c->status = -1; /* create some error code */ + } +} + #elif defined(LUA_USE_POSIX) /* }{ */ -/* in POSIX, try _longjmp/_setjmp (more efficient) */ +/* in POSIX, use _longjmp/_setjmp (more efficient) */ #define LUAI_THROW(L,c) _longjmp((c)->b, 1) #define LUAI_TRY(L,c,f,ud) if (_setjmp((c)->b) == 0) ((f)(L, ud)) -#define luai_jmpbuf jmp_buf #else /* }{ */ /* ISO C handling with long jumps */ #define LUAI_THROW(L,c) longjmp((c)->b, 1) #define LUAI_TRY(L,c,f,ud) if (setjmp((c)->b) == 0) ((f)(L, ud)) -#define luai_jmpbuf jmp_buf #endif /* } */ #endif /* } */ - -/* chain list of long jump buffers */ -struct lua_longjmp { - struct lua_longjmp *previous; - luai_jmpbuf b; - volatile TStatus status; /* error code */ -}; - - void luaD_seterrorobj (lua_State *L, TStatus errcode, StkId oldtop) { if (errcode == LUA_ERRMEM) { /* memory error? */ setsvalue2s(L, oldtop, G(L)->memerrmsg); /* reuse preregistered msg. */ @@ -151,7 +159,7 @@ l_noret luaD_throwbaselevel (lua_State *L, TStatus errcode) { TStatus luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) { l_uint32 oldnCcalls = L->nCcalls; - struct lua_longjmp lj; + lua_longjmp lj; lj.status = LUA_OK; lj.previous = L->errorJmp; /* chain new error handler */ L->errorJmp = &lj; From 9ea06e61f20ae34974226074fc6123dbb54a07c2 Mon Sep 17 00:00:00 2001 From: Roberto I Date: Fri, 5 Sep 2025 15:36:47 -0300 Subject: [PATCH 1097/1145] Details - LUAMOD_API defined as 'extern "C"' in C++. - "ANSI C" is in fact "ISO C" (comments) - Removed option -std from makefile in testes/libs. (Easier to change to C++ for tests). --- lapi.c | 2 +- lmathlib.c | 2 +- loslib.c | 2 +- luaconf.h | 6 ++++++ testes/libs/lib11.c | 2 +- testes/libs/makefile | 2 +- 6 files changed, 11 insertions(+), 5 deletions(-) diff --git a/lapi.c b/lapi.c index 55e371dcc9..27fa524797 100644 --- a/lapi.c +++ b/lapi.c @@ -484,7 +484,7 @@ LUA_API lua_State *lua_tothread (lua_State *L, int idx) { /* ** Returns a pointer to the internal representation of an object. -** Note that ANSI C does not allow the conversion of a pointer to +** Note that ISO C does not allow the conversion of a pointer to ** function to a 'void*', so the conversion here goes through ** a 'size_t'. (As the returned pointer is only informative, this ** conversion should not be a problem.) diff --git a/lmathlib.c b/lmathlib.c index 2f0f3d1bee..a6b13f969c 100644 --- a/lmathlib.c +++ b/lmathlib.c @@ -278,7 +278,7 @@ static int math_type (lua_State *L) { */ /* -** This code uses lots of shifts. ANSI C does not allow shifts greater +** This code uses lots of shifts. ISO C does not allow shifts greater ** than or equal to the width of the type being shifted, so some shifts ** are written in convoluted ways to match that restriction. For ** preprocessor tests, it assumes a width of 32 bits, so the maximum diff --git a/loslib.c b/loslib.c index 3f605028fe..b7a2b0d15f 100644 --- a/loslib.c +++ b/loslib.c @@ -34,7 +34,7 @@ #if defined(LUA_USE_WINDOWS) #define LUA_STRFTIMEOPTIONS "aAbBcdHIjmMpSUwWxXyYzZ%" \ "||" "#c#x#d#H#I#j#m#M#S#U#w#W#y#Y" /* two-char options */ -#elif defined(LUA_USE_C89) /* ANSI C 89 (only 1-char options) */ +#elif defined(LUA_USE_C89) /* C89 (only 1-char options) */ #define LUA_STRFTIMEOPTIONS "aAbBcdHIjmMpSUwWxXyYZ%" #else /* C99 specification */ #define LUA_STRFTIMEOPTIONS "aAbBcCdDeFgGhHIjmMnprRStTuUVwWxXyYzZ%" \ diff --git a/luaconf.h b/luaconf.h index 1ac64328e6..96a77802b9 100644 --- a/luaconf.h +++ b/luaconf.h @@ -319,7 +319,13 @@ ** More often than not the libs go together with the core. */ #define LUALIB_API LUA_API + +#if defined(__cplusplus) +/* Lua uses the "C name" when calling open functions */ +#define LUAMOD_API extern "C" +#else #define LUAMOD_API LUA_API +#endif /* }================================================================== */ diff --git a/testes/libs/lib11.c b/testes/libs/lib11.c index 377d0c484f..6a85f4d621 100644 --- a/testes/libs/lib11.c +++ b/testes/libs/lib11.c @@ -1,7 +1,7 @@ #include "lua.h" /* function from lib1.c */ -int lib1_export (lua_State *L); +LUAMOD_API int lib1_export (lua_State *L); LUAMOD_API int luaopen_lib11 (lua_State *L) { return lib1_export(L); diff --git a/testes/libs/makefile b/testes/libs/makefile index 4e7f965e99..cf4c688152 100644 --- a/testes/libs/makefile +++ b/testes/libs/makefile @@ -5,7 +5,7 @@ LUA_DIR = ../../ CC = gcc # compilation should generate Dynamic-Link Libraries -CFLAGS = -Wall -std=c99 -O2 -I$(LUA_DIR) -fPIC -shared +CFLAGS = -Wall -O2 -I$(LUA_DIR) -fPIC -shared # libraries used by the tests all: lib1.so lib11.so lib2.so lib21.so lib2-v2.so From 140b672e2ee2ac842661ece4b48e1a64f0cd11ea Mon Sep 17 00:00:00 2001 From: Roberto I Date: Tue, 16 Sep 2025 13:26:24 -0300 Subject: [PATCH 1098/1145] Vararg table Not yet optimized nor documented. --- lobject.h | 6 ++++-- lopcodes.h | 2 +- lparser.c | 29 +++++++++++++++++++---------- lparser.h | 9 +++++---- ltm.c | 45 +++++++++++++++++++++++++++++++++++++++------ ltm.h | 4 ++-- lundump.c | 3 ++- lvm.c | 2 +- testes/vararg.lua | 11 +++++++---- 9 files changed, 80 insertions(+), 31 deletions(-) diff --git a/lobject.h b/lobject.h index cc3dd370d0..a805dcbffb 100644 --- a/lobject.h +++ b/lobject.h @@ -583,8 +583,10 @@ typedef struct AbsLineInfo { /* ** Flags in Prototypes */ -#define PF_ISVARARG 1 -#define PF_FIXED 2 /* prototype has parts in fixed memory */ +#define PF_ISVARARG 1 /* function is vararg */ +#define PF_VATAB 2 /* function is vararg with table */ +#define PF_VAPTAB 4 /* function is vararg with pseudo-table */ +#define PF_FIXED 8 /* prototype has parts in fixed memory */ /* diff --git a/lopcodes.h b/lopcodes.h index 9ad21021ea..c3f7f64d69 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -338,7 +338,7 @@ OP_CLOSURE,/* A Bx R[A] := closure(KPROTO[Bx]) */ OP_VARARG,/* A C R[A], R[A+1], ..., R[A+C-2] = vararg */ -OP_VARARGPREP,/*A (adjust vararg parameters) */ +OP_VARARGPREP,/* (adjust vararg parameters) */ OP_EXTRAARG/* Ax extra (larger) argument for previous opcode */ } OpCode; diff --git a/lparser.c b/lparser.c index 5abcd407cb..f7e787936f 100644 --- a/lparser.c +++ b/lparser.c @@ -1041,9 +1041,10 @@ static void constructor (LexState *ls, expdesc *t) { /* }====================================================================== */ -static void setvararg (FuncState *fs, int nparams) { - fs->f->flag |= PF_ISVARARG; - luaK_codeABC(fs, OP_VARARGPREP, nparams, 0, 0); +static void setvararg (FuncState *fs, int kind) { + lua_assert(kind & PF_ISVARARG); + fs->f->flag |= cast_byte(kind); + luaK_codeABC(fs, OP_VARARGPREP, 0, 0, 0); } @@ -1052,7 +1053,7 @@ static void parlist (LexState *ls) { FuncState *fs = ls->fs; Proto *f = fs->f; int nparams = 0; - int isvararg = 0; + int varargk = 0; if (ls->t.token != ')') { /* is 'parlist' not empty? */ do { switch (ls->t.token) { @@ -1062,19 +1063,27 @@ static void parlist (LexState *ls) { break; } case TK_DOTS: { + varargk |= PF_ISVARARG; luaX_next(ls); - isvararg = 1; + if (testnext(ls, '=')) { + new_varkind(ls, str_checkname(ls), RDKVATAB); + varargk |= PF_VATAB; + } break; } default: luaX_syntaxerror(ls, " or '...' expected"); } - } while (!isvararg && testnext(ls, ',')); + } while (!varargk && testnext(ls, ',')); } adjustlocalvars(ls, nparams); f->numparams = cast_byte(fs->nactvar); - if (isvararg) - setvararg(fs, f->numparams); /* declared vararg */ - luaK_reserveregs(fs, fs->nactvar); /* reserve registers for parameters */ + if (varargk != 0) { + setvararg(fs, varargk); /* declared vararg */ + if (varargk & PF_VATAB) + adjustlocalvars(ls, 1); /* vararg table */ + } + /* reserve registers for parameters (and vararg variable, if present) */ + luaK_reserveregs(fs, fs->nactvar); } @@ -2099,7 +2108,7 @@ static void mainfunc (LexState *ls, FuncState *fs) { BlockCnt bl; Upvaldesc *env; open_func(ls, fs, &bl); - setvararg(fs, 0); /* main function is always declared vararg */ + setvararg(fs, PF_ISVARARG); /* main function is always vararg */ env = allocupvalue(fs); /* ...set environment upvalue */ env->instack = 1; env->idx = 0; diff --git a/lparser.h b/lparser.h index fdbb9b8a0b..e479905efd 100644 --- a/lparser.h +++ b/lparser.h @@ -97,10 +97,11 @@ typedef struct expdesc { /* kinds of variables */ #define VDKREG 0 /* regular local */ #define RDKCONST 1 /* local constant */ -#define RDKTOCLOSE 2 /* to-be-closed */ -#define RDKCTC 3 /* local compile-time constant */ -#define GDKREG 4 /* regular global */ -#define GDKCONST 5 /* global constant */ +#define RDKVATAB 2 /* vararg table */ +#define RDKTOCLOSE 3 /* to-be-closed */ +#define RDKCTC 4 /* local compile-time constant */ +#define GDKREG 5 /* regular global */ +#define GDKCONST 6 /* global constant */ /* variables that live in registers */ #define varinreg(v) ((v)->vd.kind <= RDKTOCLOSE) diff --git a/ltm.c b/ltm.c index d1a61a6250..619be59e93 100644 --- a/ltm.c +++ b/ltm.c @@ -224,11 +224,38 @@ int luaT_callorderiTM (lua_State *L, const TValue *p1, int v2, } -void luaT_adjustvarargs (lua_State *L, int nfixparams, CallInfo *ci, - const Proto *p) { +/* +** Create a vararg table at the top of the stack, with 'n' elements +** starting at 'f'. +*/ +static void createvarargtab (lua_State *L, StkId f, int n) { int i; - int actual = cast_int(L->top.p - ci->func.p) - 1; /* number of arguments */ - int nextra = actual - nfixparams; /* number of extra arguments */ + TValue key, value; + Table *t = luaH_new(L); + sethvalue(L, s2v(L->top.p), t); + L->top.p++; + luaH_resize(L, t, cast_uint(n), 1); + setsvalue(L, &key, luaS_new(L, "n")); /* key is "n" */ + setivalue(&value, n); /* value is n */ + /* No need to anchor the key: Due to the resize, the next operation + cannot trigger a garbage collection */ + luaH_set(L, t, &key, &value); /* t.n = n */ + for (i = 0; i < n; i++) + luaH_setint(L, t, i + 1, s2v(f + i)); +} + + +/* +** initial stack: func arg1 ... argn extra1 ... +** ^ ci->func ^ L->top +** final stack: func nil ... nil extra1 ... func arg1 ... argn +** ^ ci->func ^ L->top +*/ +void luaT_adjustvarargs (lua_State *L, CallInfo *ci, const Proto *p) { + int i; + int totalargs = cast_int(L->top.p - ci->func.p) - 1; + int nfixparams = p->numparams; + int nextra = totalargs - nfixparams; /* number of extra arguments */ ci->u.l.nextraargs = nextra; luaD_checkstack(L, p->maxstacksize + 1); /* copy function to the top of the stack */ @@ -238,8 +265,14 @@ void luaT_adjustvarargs (lua_State *L, int nfixparams, CallInfo *ci, setobjs2s(L, L->top.p++, ci->func.p + i); setnilvalue(s2v(ci->func.p + i)); /* erase original parameter (for GC) */ } - ci->func.p += actual + 1; - ci->top.p += actual + 1; + if (p->flag & (PF_VAPTAB | PF_VATAB)) { /* is there a vararg table? */ + if (p->flag & PF_VAPTAB) /* is vararg table fake? */ + setnilvalue(s2v(L->top.p)); /* initialize it */ + else + createvarargtab(L, ci->func.p + nfixparams + 1, nextra); + } + ci->func.p += totalargs + 1; + ci->top.p += totalargs + 1; lua_assert(L->top.p <= ci->top.p && ci->top.p <= L->stack_last.p); } diff --git a/ltm.h b/ltm.h index ba2e47606e..ed479bb4cd 100644 --- a/ltm.h +++ b/ltm.h @@ -95,8 +95,8 @@ LUAI_FUNC int luaT_callorderTM (lua_State *L, const TValue *p1, LUAI_FUNC int luaT_callorderiTM (lua_State *L, const TValue *p1, int v2, int inv, int isfloat, TMS event); -LUAI_FUNC void luaT_adjustvarargs (lua_State *L, int nfixparams, - struct CallInfo *ci, const Proto *p); +LUAI_FUNC void luaT_adjustvarargs (lua_State *L, struct CallInfo *ci, + const Proto *p); LUAI_FUNC void luaT_getvarargs (lua_State *L, struct CallInfo *ci, StkId where, int wanted); diff --git a/lundump.c b/lundump.c index 76f0ddc11b..74839af8bd 100644 --- a/lundump.c +++ b/lundump.c @@ -327,7 +327,8 @@ static void loadFunction (LoadState *S, Proto *f) { f->linedefined = loadInt(S); f->lastlinedefined = loadInt(S); f->numparams = loadByte(S); - f->flag = loadByte(S) & PF_ISVARARG; /* get only the meaningful flags */ + /* get only the meaningful flags */ + f->flag = cast_byte(loadByte(S) & ~PF_FIXED); if (S->fixed) f->flag |= PF_FIXED; /* signal that code is fixed */ f->maxstacksize = loadByte(S); diff --git a/lvm.c b/lvm.c index d0a1c05d0b..d88a80d19f 100644 --- a/lvm.c +++ b/lvm.c @@ -1927,7 +1927,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { vmbreak; } vmcase(OP_VARARGPREP) { - ProtectNT(luaT_adjustvarargs(L, GETARG_A(i), ci, cl->p)); + ProtectNT(luaT_adjustvarargs(L, ci, cl->p)); if (l_unlikely(trap)) { /* previous "Protect" updated trap */ luaD_hookcall(L, ci); L->oldpc = 1; /* next opcode will be seen as a "new" line */ diff --git a/testes/vararg.lua b/testes/vararg.lua index 10553de2af..4320684e1c 100644 --- a/testes/vararg.lua +++ b/testes/vararg.lua @@ -3,9 +3,12 @@ print('testing vararg') -local function f (a, ...) +local function f (a, ...=t) local x = {n = select('#', ...), ...} - for i = 1, x.n do assert(a[i] == x[i]) end + assert(x.n == t.n) + for i = 1, x.n do + assert(a[i] == x[i] and x[i] == t[i]) + end return x.n end @@ -17,7 +20,7 @@ local function c12 (...) return res, 2 end -local function vararg (...) return {n = select('#', ...), ...} end +local function vararg (...=t) return t end local call = function (f, args) return f(table.unpack(args, 1, args.n)) end @@ -99,7 +102,7 @@ assert(a==nil and b==nil and c==nil and d==nil and e==nil) -- varargs for main chunks -local f = load[[ return {...} ]] +local f = assert(load[[ return {...} ]]) local x = f(2,3) assert(x[1] == 2 and x[2] == 3 and x[3] == undef) From 8fb1af0e33cd8688f57cd0e3ab86420a8cfe99bd Mon Sep 17 00:00:00 2001 From: Roberto I Date: Wed, 17 Sep 2025 16:07:48 -0300 Subject: [PATCH 1099/1145] Varag parameter is a new kind of variable To allow some optimizations on its use. --- lcode.c | 12 ++++++++++++ lcode.h | 1 + lobject.h | 4 ++-- lparser.c | 26 +++++++++++++++++--------- lparser.h | 4 +++- ltm.c | 8 ++++---- testes/vararg.lua | 26 ++++++++++++++++++++++++++ 7 files changed, 65 insertions(+), 16 deletions(-) diff --git a/lcode.c b/lcode.c index cafe265e73..f74223eb66 100644 --- a/lcode.c +++ b/lcode.c @@ -785,6 +785,15 @@ void luaK_setoneret (FuncState *fs, expdesc *e) { } } +/* +** Change a vararg parameter into a regular local variable +*/ +void luaK_vapar2local (FuncState *fs, expdesc *var) { + fs->f->flag |= PF_VATAB; /* function will need a vararg table */ + /* now a vararg parameter is equivalent to a regular local variable */ + var->k = VLOCAL; +} + /* ** Ensure that expression 'e' is not a variable (nor a ). @@ -796,6 +805,9 @@ void luaK_dischargevars (FuncState *fs, expdesc *e) { const2exp(const2val(fs, e), e); break; } + case VVARGVAR: { + luaK_vapar2local(fs, e); /* turn it into a local variable */ + } /* FALLTHROUGH */ case VLOCAL: { /* already in a register */ int temp = e->u.var.ridx; e->u.info = temp; /* (can't do a direct assignment; values overlap) */ diff --git a/lcode.h b/lcode.h index 94fc2417dd..8c27bc92fd 100644 --- a/lcode.h +++ b/lcode.h @@ -71,6 +71,7 @@ LUAI_FUNC void luaK_nil (FuncState *fs, int from, int n); LUAI_FUNC void luaK_reserveregs (FuncState *fs, int n); LUAI_FUNC void luaK_checkstack (FuncState *fs, int n); LUAI_FUNC void luaK_int (FuncState *fs, int reg, lua_Integer n); +LUAI_FUNC void luaK_vapar2local (FuncState *fs, expdesc *var); LUAI_FUNC void luaK_dischargevars (FuncState *fs, expdesc *e); LUAI_FUNC int luaK_exp2anyreg (FuncState *fs, expdesc *e); LUAI_FUNC void luaK_exp2anyregup (FuncState *fs, expdesc *e); diff --git a/lobject.h b/lobject.h index a805dcbffb..841ab5b9c3 100644 --- a/lobject.h +++ b/lobject.h @@ -584,8 +584,8 @@ typedef struct AbsLineInfo { ** Flags in Prototypes */ #define PF_ISVARARG 1 /* function is vararg */ -#define PF_VATAB 2 /* function is vararg with table */ -#define PF_VAPTAB 4 /* function is vararg with pseudo-table */ +#define PF_VAVAR 2 /* function has vararg parameter */ +#define PF_VATAB 4 /* function has vararg table */ #define PF_FIXED 8 /* prototype has parts in fixed memory */ diff --git a/lparser.c b/lparser.c index f7e787936f..8b909f3d44 100644 --- a/lparser.c +++ b/lparser.c @@ -289,7 +289,7 @@ static void check_readonly (LexState *ls, expdesc *e) { varname = ls->dyd->actvar.arr[e->u.info].vd.name; break; } - case VLOCAL: { + case VLOCAL: case VVARGVAR: { Vardesc *vardesc = getlocalvardesc(fs, e->u.var.vidx); if (vardesc->vd.kind != VDKREG) /* not a regular variable? */ varname = vardesc->vd.name; @@ -426,8 +426,11 @@ static int searchvar (FuncState *fs, TString *n, expdesc *var) { else if (eqstr(n, vd->vd.name)) { /* found? */ if (vd->vd.kind == RDKCTC) /* compile-time constant? */ init_exp(var, VCONST, fs->firstlocal + i); - else /* local variable */ + else { /* local variable */ init_var(fs, var, i); + if (vd->vd.kind == RDKVAVAR) /* vararg parameter? */ + var->k = VVARGVAR; + } return cast_int(var->k); } } @@ -467,8 +470,13 @@ static void marktobeclosed (FuncState *fs) { static void singlevaraux (FuncState *fs, TString *n, expdesc *var, int base) { int v = searchvar(fs, n, var); /* look up variables at current level */ if (v >= 0) { /* found? */ - if (v == VLOCAL && !base) - markupval(fs, var->u.var.vidx); /* local will be used as an upval */ + if (!base) { + if (var->k == VVARGVAR) /* vararg parameter? */ + luaK_vapar2local(fs, var); /* change it to a regular local */ + if (var->k == VLOCAL) + markupval(fs, var->u.var.vidx); /* will be used as an upvalue */ + } + /* else nothing else to be done */ } else { /* not found at current level; try upvalues */ int idx = searchupvalue(fs, n); /* try existing upvalues */ @@ -1066,8 +1074,8 @@ static void parlist (LexState *ls) { varargk |= PF_ISVARARG; luaX_next(ls); if (testnext(ls, '=')) { - new_varkind(ls, str_checkname(ls), RDKVATAB); - varargk |= PF_VATAB; + new_varkind(ls, str_checkname(ls), RDKVAVAR); + varargk |= PF_VAVAR; } break; } @@ -1079,10 +1087,10 @@ static void parlist (LexState *ls) { f->numparams = cast_byte(fs->nactvar); if (varargk != 0) { setvararg(fs, varargk); /* declared vararg */ - if (varargk & PF_VATAB) - adjustlocalvars(ls, 1); /* vararg table */ + if (varargk & PF_VAVAR) + adjustlocalvars(ls, 1); /* vararg parameter */ } - /* reserve registers for parameters (and vararg variable, if present) */ + /* reserve registers for parameters (plus vararg parameter, if present) */ luaK_reserveregs(fs, fs->nactvar); } diff --git a/lparser.h b/lparser.h index e479905efd..327170e3b1 100644 --- a/lparser.h +++ b/lparser.h @@ -37,6 +37,8 @@ typedef enum { info = result register */ VLOCAL, /* local variable; var.ridx = register index; var.vidx = relative index in 'actvar.arr' */ + VVARGVAR, /* vararg parameter; var.ridx = register index; + var.vidx = relative index in 'actvar.arr' */ VGLOBAL, /* global variable; info = relative index in 'actvar.arr' (or -1 for implicit declaration) */ @@ -97,7 +99,7 @@ typedef struct expdesc { /* kinds of variables */ #define VDKREG 0 /* regular local */ #define RDKCONST 1 /* local constant */ -#define RDKVATAB 2 /* vararg table */ +#define RDKVAVAR 2 /* vararg parameter */ #define RDKTOCLOSE 3 /* to-be-closed */ #define RDKCTC 4 /* local compile-time constant */ #define GDKREG 5 /* regular global */ diff --git a/ltm.c b/ltm.c index 619be59e93..cc812e6234 100644 --- a/ltm.c +++ b/ltm.c @@ -265,11 +265,11 @@ void luaT_adjustvarargs (lua_State *L, CallInfo *ci, const Proto *p) { setobjs2s(L, L->top.p++, ci->func.p + i); setnilvalue(s2v(ci->func.p + i)); /* erase original parameter (for GC) */ } - if (p->flag & (PF_VAPTAB | PF_VATAB)) { /* is there a vararg table? */ - if (p->flag & PF_VAPTAB) /* is vararg table fake? */ - setnilvalue(s2v(L->top.p)); /* initialize it */ - else + if (p->flag & PF_VAVAR) { /* is there a vararg parameter? */ + if (p->flag & PF_VATAB) /* does it need a vararg table? */ createvarargtab(L, ci->func.p + nfixparams + 1, nextra); + else /* no table; set parameter to nil */ + setnilvalue(s2v(L->top.p)); } ci->func.p += totalargs + 1; ci->top.p += totalargs + 1; diff --git a/testes/vararg.lua b/testes/vararg.lua index 4320684e1c..92f720cb6d 100644 --- a/testes/vararg.lua +++ b/testes/vararg.lua @@ -150,5 +150,31 @@ do local a, b = g() assert(a == nil and b == 2) end + + +do -- vararg parameter used in nested functions + local function foo (... = tab1) + return function (... = tab2) + return {tab1, tab2} + end + end + local f = foo(10, 20, 30) + local t = f("a", "b") + assert(t[1].n == 3 and t[1][1] == 10) + assert(t[2].n == 2 and t[2][1] == "a") +end + +do -- vararg parameter is read-only + local st, msg = load("return function (... = t) t = 10 end") + assert(string.find(msg, "const variable 't'")) + + local st, msg = load[[ + local function foo (... = extra) + return function (...) extra = nil end + end + ]] + assert(string.find(msg, "const variable 'extra'")) +end + print('OK') From 0cc3c9447cca9abae9738ee77c24d88801c3916c Mon Sep 17 00:00:00 2001 From: Roberto I Date: Thu, 18 Sep 2025 11:03:55 -0300 Subject: [PATCH 1100/1145] Small tweaks in makefile --- makefile | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/makefile b/makefile index 8506e93c20..2a8ca48969 100644 --- a/makefile +++ b/makefile @@ -15,9 +15,9 @@ CWARNSCPP= \ -Wdouble-promotion \ -Wmissing-declarations \ -Wconversion \ - -Wstrict-overflow=2 \ # the next warnings might be useful sometimes, # but usually they generate too much noise + # -Wstrict-overflow=2 \ # -Werror \ # -pedantic # warns if we use jump tables \ # -Wformat=2 \ @@ -166,8 +166,7 @@ ldump.o: ldump.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \ lfunc.o: lfunc.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \ llimits.h ltm.h lzio.h lmem.h ldo.h lfunc.h lgc.h lgc.o: lgc.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \ - llimits.h ltm.h lzio.h lmem.h ldo.h lfunc.h lgc.h llex.h lstring.h \ - ltable.h + llimits.h ltm.h lzio.h lmem.h ldo.h lfunc.h lgc.h lstring.h ltable.h linit.o: linit.c lprefix.h lua.h luaconf.h lualib.h lauxlib.h llimits.h liolib.o: liolib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h llimits.h llex.o: llex.c lprefix.h lua.h luaconf.h lctype.h llimits.h ldebug.h \ From 25c54fe60e22d05cdfaa48c64372d354efa59547 Mon Sep 17 00:00:00 2001 From: Roberto I Date: Wed, 24 Sep 2025 18:33:08 -0300 Subject: [PATCH 1101/1145] Optimization for vararg tables A vararg table can be virtual. If the vararg table is used only as a base in indexing expressions, the code does not need to create an actual table for it. Instead, it compiles the indexing expressions into direct accesses to the internal vararg data. --- lcode.c | 57 ++++++++++++++++++----------- ljumptab.h | 3 +- lopcodes.c | 1 + lopcodes.h | 2 ++ lopnames.h | 1 + lparser.c | 10 ++++-- lparser.h | 2 ++ ltm.c | 22 ++++++++++++ ltm.h | 1 + lvm.c | 6 ++++ manual/manual.of | 91 +++++++++++++++++++++++++++++++---------------- testes/locals.lua | 6 ++-- testes/vararg.lua | 47 ++++++++++++++++++++---- 13 files changed, 186 insertions(+), 63 deletions(-) diff --git a/lcode.c b/lcode.c index f74223eb66..f7c2334ca7 100644 --- a/lcode.c +++ b/lcode.c @@ -842,6 +842,12 @@ void luaK_dischargevars (FuncState *fs, expdesc *e) { e->k = VRELOC; break; } + case VVARGIND: { + freeregs(fs, e->u.ind.t, e->u.ind.idx); + e->u.info = luaK_codeABC(fs, OP_GETVARG, 0, e->u.ind.t, e->u.ind.idx); + e->k = VRELOC; + break; + } case VVARARG: case VCALL: { luaK_setoneret(fs, e); break; @@ -1004,11 +1010,11 @@ int luaK_exp2anyreg (FuncState *fs, expdesc *e) { /* -** Ensures final expression result is either in a register -** or in an upvalue. +** Ensures final expression result is either in a register, +** in an upvalue, or it is the vararg parameter. */ void luaK_exp2anyregup (FuncState *fs, expdesc *e) { - if (e->k != VUPVAL || hasjumps(e)) + if ((e->k != VUPVAL && e->k != VVARGVAR) || hasjumps(e)) luaK_exp2anyreg(fs, e); } @@ -1314,6 +1320,13 @@ void luaK_self (FuncState *fs, expdesc *e, expdesc *key) { } +/* auxiliary function to define indexing expressions */ +static void fillidxk (expdesc *t, int idx, expkind k) { + t->u.ind.idx = cast_byte(idx); + t->k = k; +} + + /* ** Create expression 't[k]'. 't' must have its final result already in a ** register or upvalue. Upvalues can only be indexed by literal strings. @@ -1325,31 +1338,30 @@ void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) { if (k->k == VKSTR) keystr = str2K(fs, k); lua_assert(!hasjumps(t) && - (t->k == VLOCAL || t->k == VNONRELOC || t->k == VUPVAL)); + (t->k == VLOCAL || t->k == VVARGVAR || + t->k == VNONRELOC || t->k == VUPVAL)); if (t->k == VUPVAL && !isKstr(fs, k)) /* upvalue indexed by non 'Kstr'? */ luaK_exp2anyreg(fs, t); /* put it in a register */ if (t->k == VUPVAL) { lu_byte temp = cast_byte(t->u.info); /* upvalue index */ t->u.ind.t = temp; /* (can't do a direct assignment; values overlap) */ lua_assert(isKstr(fs, k)); - t->u.ind.idx = cast_short(k->u.info); /* literal short string */ - t->k = VINDEXUP; + fillidxk(t, k->u.info, VINDEXUP); /* literal short string */ + } + else if (t->k == VVARGVAR) { /* indexing the vararg parameter? */ + lua_assert(t->u.ind.t == fs->f->numparams); + t->u.ind.t = cast_byte(t->u.var.ridx); + fillidxk(t, luaK_exp2anyreg(fs, k), VVARGIND); /* register */ } else { /* register index of the table */ t->u.ind.t = cast_byte((t->k == VLOCAL) ? t->u.var.ridx: t->u.info); - if (isKstr(fs, k)) { - t->u.ind.idx = cast_short(k->u.info); /* literal short string */ - t->k = VINDEXSTR; - } - else if (isCint(k)) { /* int. constant in proper range? */ - t->u.ind.idx = cast_short(k->u.ival); - t->k = VINDEXI; - } - else { - t->u.ind.idx = cast_short(luaK_exp2anyreg(fs, k)); /* register */ - t->k = VINDEXED; - } + if (isKstr(fs, k)) + fillidxk(t, k->u.info, VINDEXSTR); /* literal short string */ + else if (isCint(k)) /* int. constant in proper range? */ + fillidxk(t, cast_int(k->u.ival), VINDEXI); + else + fillidxk(t, luaK_exp2anyreg(fs, k), VINDEXED); /* register */ } t->u.ind.keystr = keystr; /* string index in 'k' */ t->u.ind.ro = 0; /* by default, not read-only */ @@ -1913,9 +1925,14 @@ void luaK_finish (FuncState *fs) { SETARG_C(*pc, p->numparams + 1); /* signal that it is vararg */ break; } - case OP_JMP: { + case OP_GETVARG: { + if (p->flag & PF_VATAB) /* function has a vararg table? */ + SET_OPCODE(*pc, OP_GETTABLE); /* must get vararg there */ + break; + } + case OP_JMP: { /* to optimize jumps to jumps */ int target = finaltarget(p->code, i); - fixjump(fs, i, target); + fixjump(fs, i, target); /* jump directly to final target */ break; } default: break; diff --git a/ljumptab.h b/ljumptab.h index a24828bb5a..f896b6585b 100644 --- a/ljumptab.h +++ b/ljumptab.h @@ -21,7 +21,7 @@ static const void *const disptab[NUM_OPCODES] = { #if 0 ** you can update the following list with this command: ** -** sed -n '/^OP_/\!d; s/OP_/\&\&L_OP_/ ; s/,.*/,/ ; s/\/.*// ; p' lopcodes.h +** sed -n '/^OP_/!d; s/OP_/\&\&L_OP_/ ; s/,.*/,/ ; s/\/.*// ; p' lopcodes.h ** #endif @@ -106,6 +106,7 @@ static const void *const disptab[NUM_OPCODES] = { &&L_OP_SETLIST, &&L_OP_CLOSURE, &&L_OP_VARARG, +&&L_OP_GETVARG, &&L_OP_VARARGPREP, &&L_OP_EXTRAARG diff --git a/lopcodes.c b/lopcodes.c index 79ffbe2590..47458e40ca 100644 --- a/lopcodes.c +++ b/lopcodes.c @@ -102,6 +102,7 @@ LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = { ,opmode(0, 0, 1, 0, 0, ivABC) /* OP_SETLIST */ ,opmode(0, 0, 0, 0, 1, iABx) /* OP_CLOSURE */ ,opmode(0, 1, 0, 0, 1, iABC) /* OP_VARARG */ + ,opmode(0, 0, 0, 0, 1, iABC) /* OP_GETVARG */ ,opmode(0, 0, 1, 0, 1, iABC) /* OP_VARARGPREP */ ,opmode(0, 0, 0, 0, 0, iAx) /* OP_EXTRAARG */ }; diff --git a/lopcodes.h b/lopcodes.h index c3f7f64d69..82bba721ff 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -338,6 +338,8 @@ OP_CLOSURE,/* A Bx R[A] := closure(KPROTO[Bx]) */ OP_VARARG,/* A C R[A], R[A+1], ..., R[A+C-2] = vararg */ +OP_GETVARG, /* A B C R[A] := R[B][R[C]], R[B] is vararg parameter */ + OP_VARARGPREP,/* (adjust vararg parameters) */ OP_EXTRAARG/* Ax extra (larger) argument for previous opcode */ diff --git a/lopnames.h b/lopnames.h index 39df332f5e..aa7bea77c9 100644 --- a/lopnames.h +++ b/lopnames.h @@ -94,6 +94,7 @@ static const char *const opnames[] = { "SETLIST", "CLOSURE", "VARARG", + "GETVARG", "VARARGPREP", "EXTRAARG", NULL diff --git a/lparser.c b/lparser.c index 8b909f3d44..408b8e216d 100644 --- a/lparser.c +++ b/lparser.c @@ -279,7 +279,9 @@ static void init_var (FuncState *fs, expdesc *e, int vidx) { /* -** Raises an error if variable described by 'e' is read only +** Raises an error if variable described by 'e' is read only; moreover, +** if 'e' is t[exp] where t is the vararg parameter, change it to index +** a real table. (Virtual vararg tables cannot be changed.) */ static void check_readonly (LexState *ls, expdesc *e) { FuncState *fs = ls->fs; @@ -301,6 +303,10 @@ static void check_readonly (LexState *ls, expdesc *e) { varname = up->name; break; } + case VVARGIND: { + fs->f->flag |= PF_VATAB; /* function will need a vararg table */ + e->k = VINDEXED; + } /* FALLTHROUGH */ case VINDEXUP: case VINDEXSTR: case VINDEXED: { /* global variable */ if (e->u.ind.ro) /* read-only? */ varname = tsvalue(&fs->f->k[e->u.ind.keystr]); @@ -1073,7 +1079,7 @@ static void parlist (LexState *ls) { case TK_DOTS: { varargk |= PF_ISVARARG; luaX_next(ls); - if (testnext(ls, '=')) { + if (testnext(ls, '|')) { new_varkind(ls, str_checkname(ls), RDKVAVAR); varargk |= PF_VAVAR; } diff --git a/lparser.h b/lparser.h index 327170e3b1..a30df04f77 100644 --- a/lparser.h +++ b/lparser.h @@ -51,6 +51,8 @@ typedef enum { ind.ro = true if it represents a read-only global; ind.keystr = if key is a string, index in 'k' of that string; -1 if key is not a string */ + VVARGIND, /* indexed vararg parameter; + ind.* as in VINDEXED */ VINDEXUP, /* indexed upvalue; ind.idx = key's K index; ind.* as in VINDEXED */ diff --git a/ltm.c b/ltm.c index cc812e6234..92a03e71be 100644 --- a/ltm.c +++ b/ltm.c @@ -277,6 +277,28 @@ void luaT_adjustvarargs (lua_State *L, CallInfo *ci, const Proto *p) { } +void luaT_getvararg (CallInfo *ci, StkId ra, TValue *rc) { + int nextra = ci->u.l.nextraargs; + lua_Integer n; + if (tointegerns(rc, &n)) { /* integral value? */ + if (l_castS2U(n) - 1 < cast_uint(nextra)) { + StkId slot = ci->func.p - nextra + cast_int(n) - 1; + setobjs2s(((lua_State*)NULL), ra, slot); + return; + } + } + else if (ttisshrstring(rc)) { /* short-string value? */ + size_t len; + const char *s = getlstr(tsvalue(rc), len); + if (len == 1 && s[0] == 'n') { /* key is "n"? */ + setivalue(s2v(ra), nextra); + return; + } + } + setnilvalue(s2v(ra)); /* else produce nil */ +} + + void luaT_getvarargs (lua_State *L, CallInfo *ci, StkId where, int wanted) { int i; int nextra = ci->u.l.nextraargs; diff --git a/ltm.h b/ltm.h index ed479bb4cd..86f457ebce 100644 --- a/ltm.h +++ b/ltm.h @@ -97,6 +97,7 @@ LUAI_FUNC int luaT_callorderiTM (lua_State *L, const TValue *p1, int v2, LUAI_FUNC void luaT_adjustvarargs (lua_State *L, struct CallInfo *ci, const Proto *p); +LUAI_FUNC void luaT_getvararg (CallInfo *ci, StkId ra, TValue *rc); LUAI_FUNC void luaT_getvarargs (lua_State *L, struct CallInfo *ci, StkId where, int wanted); diff --git a/lvm.c b/lvm.c index d88a80d19f..3ce7e87f8f 100644 --- a/lvm.c +++ b/lvm.c @@ -1926,6 +1926,12 @@ void luaV_execute (lua_State *L, CallInfo *ci) { Protect(luaT_getvarargs(L, ci, ra, n)); vmbreak; } + vmcase(OP_GETVARG) { + StkId ra = RA(i); + TValue *rc = vRC(i); + luaT_getvararg(ci, ra, rc); + vmbreak; + } vmcase(OP_VARARGPREP) { ProtectNT(luaT_adjustvarargs(L, ci, cl->p)); if (l_unlikely(trap)) { /* previous "Protect" updated trap */ diff --git a/manual/manual.of b/manual/manual.of index 3c7041182a..beea41f96a 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -2262,7 +2262,7 @@ return x or f(x) -- results adjusted to 1 @sect3{func-def| @title{Function Definitions} -The syntax for function definition is +The syntax for a function definition is @Produc{ @producname{functiondef}@producbody{@Rw{function} funcbody} @producname{funcbody}@producbody{@bnfter{(} @bnfopt{parlist} @bnfter{)} block @Rw{end}} @@ -2315,6 +2315,18 @@ translates to global f; f = function () @rep{body} end } +The @emphx{colon} syntax +is used to emulate @def{methods}, +adding an implicit extra parameter @idx{self} to the function. +Thus, the statement +@verbatim{ +function t.a.b.c:f (@rep{params}) @rep{body} end +} +is syntactic sugar for +@verbatim{ +t.a.b.c.f = function (self, @rep{params}) @rep{body} end +} + A function definition is an executable expression, whose value has type @emph{function}. When Lua precompiles a chunk, @@ -2325,11 +2337,25 @@ the function is @emph{instantiated} (or @emph{closed}). This function instance, or @emphx{closure}, is the final value of the expression. +Results are returned using the @Rw{return} statement @see{control}. +If control reaches the end of a function +without encountering a @Rw{return} statement, +then the function returns with no results. + +@index{multiple return} +There is a system-dependent limit on the number of values +that a function may return. +This limit is guaranteed to be at least 1000. + +@sect4{@title{Parameters} + Parameters act as local variables that are initialized with the argument values: @Produc{ -@producname{parlist}@producbody{namelist @bnfopt{@bnfter{,} @bnfter{...}} @Or - @bnfter{...}} +@producname{parlist}@producbody{namelist @bnfopt{@bnfter{,} varargparam} @Or + varargparam} +@producname{varargparam}@producbody{@bnfter{...} + @bnfopt{@bnfter{|} @bnfNter{Name}}} } When a Lua function is called, it adjusts its list of @x{arguments} to @@ -2339,11 +2365,12 @@ which is indicated by three dots (@Char{...}) at the end of its parameter list. A variadic function does not adjust its argument list; instead, it collects all extra arguments and supplies them -to the function through a @def{vararg expression}, -which is also written as three dots. -The value of this expression is a list of all actual extra arguments, -similar to a function with multiple results @see{multires}. +to the function through a @def{vararg expression} and, +if present, a @def{vararg table}. +A vararg expression is also written as three dots, +and its value is a list of all actual extra arguments, +similar to a function with multiple results @see{multires}. As an example, consider the following definitions: @verbatim{ @@ -2368,26 +2395,27 @@ g(3, 4, 5, 8) a=3, b=4, ... -> 5 8 g(5, r()) a=5, b=1, ... -> 2 3 } -Results are returned using the @Rw{return} statement @see{control}. -If control reaches the end of a function -without encountering a @Rw{return} statement, -then the function returns with no results. - -@index{multiple return} -There is a system-dependent limit on the number of values -that a function may return. -This limit is guaranteed to be at least 1000. - -The @emphx{colon} syntax -is used to emulate @def{methods}, -adding an implicit extra parameter @idx{self} to the function. -Thus, the statement +The presence of a varag table in a variadic function is indicated +by the @T{|name} syntax after the three dots. +When present, +a vararg table behaves like a read-only local variable +with the given name that is initialized with a table. +In that table, +the values at indices 1, 2, etc. are the extra arguments, +and the value at index @St{n} is the number of extra arguments. +In other words, the code behaves as if the function started with +the following statement, +assuming the standard behavior of @Lid{table.pack}: @verbatim{ -function t.a.b.c:f (@rep{params}) @rep{body} end +local name = table.pack(...) } -is syntactic sugar for -@verbatim{ -t.a.b.c.f = function (self, @rep{params}) @rep{body} end + +As an optimization, +if the vararg table is used only as a base in indexing expressions +(the @T{t} in @T{t[exp]} or @T{t.id}) and it is not an upvalue, +the code does not create an actual table and instead translates +the indexing expressions into accesses to the internal vararg data. + } } @@ -2422,7 +2450,7 @@ for instance @T{foo(e1, e2, e3)} @see{functioncall}.} for instance @T{a , b, c = e1, e2, e3} @see{assignment}.} @item{A local or global declaration, -which is a special case of multiple assignment.} +which is similar to a multiple assignment.} @item{The initial values in a generic @rw{for} loop, for instance @T{for k in e1, e2, e3 do ... end} @see{for}.} @@ -3016,7 +3044,7 @@ typedef void * (*lua_Alloc) (void *ud, size_t osize, size_t nsize);| -The type of the @x{memory-allocation function} used by Lua states. +The type of the @x{memory-allocator function} used by Lua states. The allocator function must provide a functionality similar to @id{realloc}, but not exactly the same. @@ -3482,7 +3510,7 @@ This function should not be called by a finalizer. @APIEntry{lua_Alloc lua_getallocf (lua_State *L, void **ud);| @apii{0,0,-} -Returns the @x{memory-allocation function} of a given state. +Returns the @x{memory-allocator function} of a given state. If @id{ud} is not @id{NULL}, Lua stores in @T{*ud} the opaque pointer given when the memory-allocator function was set. @@ -9732,8 +9760,11 @@ and @bnfNter{LiteralString}, see @See{lexical}.) @producname{funcbody}@producbody{@bnfter{(} @bnfopt{parlist} @bnfter{)} block @Rw{end}} -@producname{parlist}@producbody{namelist @bnfopt{@bnfter{,} @bnfter{...}} - @Or @bnfter{...}} +@producname{parlist}@producbody{namelist @bnfopt{@bnfter{,} varargparam} @Or + varargparam} + +@producname{varargparam}@producbody{@bnfter{...} + @bnfopt{@bnfter{|} @bnfNter{Name}}} @producname{tableconstructor}@producbody{@bnfter{@Open} @bnfopt{fieldlist} @bnfter{@Close}} diff --git a/testes/locals.lua b/testes/locals.lua index 02f41980a8..5222802f44 100644 --- a/testes/locals.lua +++ b/testes/locals.lua @@ -310,8 +310,7 @@ do -- testing presence of second argument local function foo (howtoclose, obj, n) local ca -- copy of 'a' visible inside its close metamethod do - local a = func2close(function (...) - local t = table.pack(...) + local a = func2close(function (... | t) assert(select("#", ...) == n) assert(t.n == n and t[1] == ca and (t.n < 2 or t[2] == obj)) ca = 15 -- final value to be returned if howtoclose=="scope" @@ -911,8 +910,7 @@ do local extrares -- result from extra yield (if any) - local function check (body, extra, ...) - local t = table.pack(...) -- expected returns + local function check (body, extra, ...|t) local co = coroutine.wrap(body) if extra then extrares = co() -- runs until first (extra) yield diff --git a/testes/vararg.lua b/testes/vararg.lua index 92f720cb6d..5711f78b84 100644 --- a/testes/vararg.lua +++ b/testes/vararg.lua @@ -3,7 +3,7 @@ print('testing vararg') -local function f (a, ...=t) +local function f (a, ...|t) local x = {n = select('#', ...), ...} assert(x.n == t.n) for i = 1, x.n do @@ -20,7 +20,7 @@ local function c12 (...) return res, 2 end -local function vararg (...=t) return t end +local function vararg (... | t) return t end local call = function (f, args) return f(table.unpack(args, 1, args.n)) end @@ -153,8 +153,8 @@ end do -- vararg parameter used in nested functions - local function foo (... = tab1) - return function (... = tab2) + local function foo (... | tab1) + return function (... | tab2) return {tab1, tab2} end end @@ -165,16 +165,51 @@ do -- vararg parameter used in nested functions end do -- vararg parameter is read-only - local st, msg = load("return function (... = t) t = 10 end") + local st, msg = load("return function (... | t) t = 10 end") assert(string.find(msg, "const variable 't'")) local st, msg = load[[ - local function foo (... = extra) + local function foo (... | extra) return function (...) extra = nil end end ]] assert(string.find(msg, "const variable 'extra'")) end + +do -- _ENV as vararg parameter + local st, msg = load[[ + local function aux (... | _ENV) + global a + a = 10 + end ]] + assert(string.find(msg, "const variable 'a'")) +end + + +do -- access to vararg parameter + local function notab (keys, t, ... | v) + for _, k in pairs(keys) do + assert(t[k] == v[k]) + end + assert(t.n == v.n) + end + + local t = table.pack(10, 20, 30) + local keys = {-1, 0, 1, t.n, t.n + 1, 1.0, 1.1, "n", print, "k", "1"} + notab(keys, t, 10, 20, 30) -- ensure stack space + local m = collectgarbage"count" + notab(keys, t, 10, 20, 30) + -- 'notab' does not create any table/object + assert(m == collectgarbage"count") + + -- writing to the vararg table + local function foo (... | t) + t[1] = t[1] + 10 + return t[1] + end + assert(foo(10, 30) == 20) +end + print('OK') From 3347c9d32d4d91b6139bff475c78cf0c4796e2a7 Mon Sep 17 00:00:00 2001 From: Roberto I Date: Fri, 10 Oct 2025 13:22:19 -0300 Subject: [PATCH 1102/1145] Initialization of too many locals break assertion The check for limit of local variables is made after generating code to initialize them. If there are too many local variables not initialized, the coding of instruction OP_LOADNIL could overflow an argument. --- lparser.c | 1 + testes/errors.lua | 19 ++++++++++++------- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/lparser.c b/lparser.c index 408b8e216d..dc646fea99 100644 --- a/lparser.c +++ b/lparser.c @@ -547,6 +547,7 @@ static void singlevar (LexState *ls, expdesc *var) { static void adjust_assign (LexState *ls, int nvars, int nexps, expdesc *e) { FuncState *fs = ls->fs; int needed = nvars - nexps; /* extra values needed */ + luaK_checkstack(fs, needed); if (hasmultret(e->k)) { /* last expression has multiple returns? */ int extra = needed + 1; /* discount last expression itself */ if (extra < 0) diff --git a/testes/errors.lua b/testes/errors.lua index 4230a35249..00a43fc69b 100644 --- a/testes/errors.lua +++ b/testes/errors.lua @@ -689,21 +689,26 @@ end -- testing syntax limits local function testrep (init, rep, close, repc, finalresult) - local s = init .. string.rep(rep, 100) .. close .. string.rep(repc, 100) - local res, msg = load(s) - assert(res) -- 100 levels is OK + local function gencode (n) + return init .. string.rep(rep, n) .. close .. string.rep(repc, n) + end + local res, msg = load(gencode(100)) -- 100 levels is OK + assert(res) if (finalresult) then assert(res() == finalresult) end - s = init .. string.rep(rep, 500) - local res, msg = load(s) -- 500 levels not ok + local res, msg = load(gencode(500)) -- 500 levels not ok assert(not res and (string.find(msg, "too many") or string.find(msg, "overflow"))) end +testrep("local a", ",a", ";", "") -- local variables +testrep("local a", ",a", "= 1", ",1") -- local variables initialized +testrep("local a", ",a", "= f()", "") -- local variables initialized testrep("local a; a", ",a", "= 1", ",1") -- multiple assignment -testrep("local a; a=", "{", "0", "}") -testrep("return ", "(", "2", ")", 2) +testrep("local a; a=", "{", "0", "}") -- constructors +testrep("return ", "(", "2", ")", 2) -- parentheses +-- nested calls (a(a(a(a(...))))) testrep("local function a (x) return x end; return ", "a(", "2.2", ")", 2.2) testrep("", "do ", "", " end") testrep("", "while a do ", "", " end") From 7a92f3f99a26d9e51be40b744ed4fab0b50ecaa5 Mon Sep 17 00:00:00 2001 From: Roberto I Date: Fri, 10 Oct 2025 15:28:41 -0300 Subject: [PATCH 1103/1145] Change in dumping of NULL strings When dumping a string, adding 2 to its size may overflow a size_t for external strings, which may not have a header. (Adding 1 is Ok, because all strings end with a '\0' not included in their size.) The new method for saving NULL strings code them as a repeated string, using the reserved index 0. --- ldump.c | 22 +++++++++++++--------- lundump.c | 12 ++++++------ 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/ldump.c b/ldump.c index a75b20d247..5795788922 100644 --- a/ldump.c +++ b/ldump.c @@ -132,27 +132,31 @@ static void dumpInteger (DumpState *D, lua_Integer x) { /* -** Dump a String. First dump its "size": size==0 means NULL; -** size==1 is followed by an index and means "reuse saved string with -** that index"; size>=2 is followed by the string contents with real -** size==size-2 and means that string, which will be saved with -** the next available index. +** Dump a String. First dump its "size": +** size==0 is followed by an index and means "reuse saved string with +** that index"; index==0 means NULL. +** size>=1 is followed by the string contents with real size==size-1 and +** means that string, which will be saved with the next available index. +** The real size does not include the ending '\0' (which is not dumped), +** so adding 1 to it cannot overflow a size_t. */ static void dumpString (DumpState *D, TString *ts) { - if (ts == NULL) - dumpSize(D, 0); + if (ts == NULL) { + dumpVarint(D, 0); /* will "reuse" NULL */ + dumpVarint(D, 0); /* special index for NULL */ + } else { TValue idx; int tag = luaH_getstr(D->h, ts, &idx); if (!tagisempty(tag)) { /* string already saved? */ - dumpVarint(D, 1); /* reuse a saved string */ + dumpVarint(D, 0); /* reuse a saved string */ dumpVarint(D, l_castS2U(ivalue(&idx))); /* index of saved string */ } else { /* must write and save the string */ TValue key, value; /* to save the string in the hash */ size_t size; const char *s = getlstr(ts, size); - dumpSize(D, size + 2); + dumpSize(D, size + 1); dumpVector(D, s, size + 1); /* include ending '\0' */ D->nstr++; /* one more saved string */ setsvalue(D->L, &key, ts); /* the string is the key */ diff --git a/lundump.c b/lundump.c index 74839af8bd..3b61cc8cbb 100644 --- a/lundump.c +++ b/lundump.c @@ -147,20 +147,20 @@ static void loadString (LoadState *S, Proto *p, TString **sl) { TString *ts; TValue sv; size_t size = loadSize(S); - if (size == 0) { /* no string? */ - lua_assert(*sl == NULL); /* must be prefilled */ - return; - } - else if (size == 1) { /* previously saved string? */ + if (size == 0) { /* previously saved string? */ lua_Unsigned idx = loadVarint(S, LUA_MAXUNSIGNED); /* get its index */ TValue stv; + if (idx == 0) { /* no string? */ + lua_assert(*sl == NULL); /* must be prefilled */ + return; + } if (novariant(luaH_getint(S->h, l_castU2S(idx), &stv)) != LUA_TSTRING) error(S, "invalid string index"); *sl = ts = tsvalue(&stv); /* get its value */ luaC_objbarrier(L, p, ts); return; /* do not save it again */ } - else if ((size -= 2) <= LUAI_MAXSHORTLEN) { /* short string? */ + else if ((size -= 1) <= LUAI_MAXSHORTLEN) { /* short string? */ char buff[LUAI_MAXSHORTLEN + 1]; /* extra space for '\0' */ loadVector(S, buff, size + 1); /* load string into buffer */ *sl = ts = luaS_newlstr(L, buff, size); /* create string */ From 30a7b93439f72570cd3315c201b140df3c07e106 Mon Sep 17 00:00:00 2001 From: Roberto I Date: Sun, 12 Oct 2025 15:13:28 -0300 Subject: [PATCH 1104/1145] Two new memory tests For external strings and for vararg tables. --- testes/memerr.lua | 42 +++++++++++++++++++++++++++++++++--------- 1 file changed, 33 insertions(+), 9 deletions(-) diff --git a/testes/memerr.lua b/testes/memerr.lua index 77cb47cb1e..69d2ef8550 100644 --- a/testes/memerr.lua +++ b/testes/memerr.lua @@ -42,8 +42,12 @@ checkerr(MEMERRMSG, f) T.alloccount() -- remove limit +-- preallocate stack space +local function deep (n) if n > 0 then deep(n - 1) end end + + -- test memory errors; increase limit for maximum memory by steps, --- o that we get memory errors in all allocations of a given +-- so that we get memory errors in all allocations of a given -- task, until there is enough memory to complete the task without -- errors. local function testbytes (s, f) @@ -53,6 +57,7 @@ local function testbytes (s, f) local a,b = nil while true do collectgarbage(); collectgarbage() + deep(4) T.totalmem(M) a, b = T.testC("pcall 0 1 0; pushstatus; return 2", f) T.totalmem(0) -- remove limit @@ -77,6 +82,7 @@ local function testalloc (s, f) local a,b = nil while true do collectgarbage(); collectgarbage() + deep(4) T.alloccount(M) a, b = T.testC("pcall 0 1 0; pushstatus; return 2", f) T.alloccount() -- remove limit @@ -87,21 +93,19 @@ local function testalloc (s, f) M = M + 1 -- increase allocation limit end print(string.format("minimum allocations for %s: %d allocations", s, M)) - return a + return M end local function testamem (s, f) - testalloc(s, f) - return testbytes(s, f) + local aloc = testalloc(s, f) + local res = testbytes(s, f) + return {aloc = aloc, res = res} end --- doing nothing -b = testamem("doing nothing", function () return 10 end) -assert(b == 10) - --- testing memory errors when creating a new state +local b = testamem("function call", function () return 10 end) +assert(b.res == 10 and b.aloc == 0) testamem("state creation", function () local st = T.newstate() @@ -121,6 +125,18 @@ testamem("coroutine creation", function() return coroutine.create(print) end) +do -- vararg tables + local function pack (... | t) return t end + local b = testamem("vararg table", function () + return pack(10, 20, 30, 40, "hello") + end) + assert(b.aloc == 3) -- new table uses three memory blocks + -- table optimized away + local function sel (n, ...|arg) return arg[n] + arg.n end + local b = testamem("optimized vararg table", + function () return sel(2.0, 20, 30) end) + assert(b.res == 32 and b.aloc == 0) -- no memory needed for this case +end -- testing to-be-closed variables testamem("to-be-closed variables", function() @@ -160,6 +176,14 @@ testamem("running code on new thread", function () end) +do -- external strings + local str = string.rep("a", 100) + testamem("creating external strings", function () + return T.externstr(str) + end) +end + + -- testing memory x compiler testamem("loadstring", function () From 9c66903cc55388006a833f0f3911ea81fa86edea Mon Sep 17 00:00:00 2001 From: Roberto I Date: Tue, 14 Oct 2025 13:50:24 -0300 Subject: [PATCH 1105/1145] Details - Functions luaK_goiffalse, luaS_hash made private. - Removed unused macro log2maxs. --- lcode.c | 2 +- lcode.h | 1 - llimits.h | 7 ------- lstring.c | 2 +- lstring.h | 1 - 5 files changed, 2 insertions(+), 11 deletions(-) diff --git a/lcode.c b/lcode.c index f7c2334ca7..429d4f8020 100644 --- a/lcode.c +++ b/lcode.c @@ -1181,7 +1181,7 @@ void luaK_goiftrue (FuncState *fs, expdesc *e) { /* ** Emit code to go through if 'e' is false, jump otherwise. */ -void luaK_goiffalse (FuncState *fs, expdesc *e) { +static void luaK_goiffalse (FuncState *fs, expdesc *e) { int pc; /* pc of new jump */ luaK_dischargevars(fs, e); switch (e->k) { diff --git a/lcode.h b/lcode.h index 8c27bc92fd..f6397a3cde 100644 --- a/lcode.h +++ b/lcode.h @@ -80,7 +80,6 @@ LUAI_FUNC void luaK_exp2val (FuncState *fs, expdesc *e); LUAI_FUNC void luaK_self (FuncState *fs, expdesc *e, expdesc *key); LUAI_FUNC void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k); LUAI_FUNC void luaK_goiftrue (FuncState *fs, expdesc *e); -LUAI_FUNC void luaK_goiffalse (FuncState *fs, expdesc *e); LUAI_FUNC void luaK_storevar (FuncState *fs, expdesc *var, expdesc *e); LUAI_FUNC void luaK_setreturns (FuncState *fs, expdesc *e, int nresults); LUAI_FUNC void luaK_setoneret (FuncState *fs, expdesc *e); diff --git a/llimits.h b/llimits.h index d115496f7a..2163254378 100644 --- a/llimits.h +++ b/llimits.h @@ -59,13 +59,6 @@ typedef lu_byte TStatus; #define MAX_SIZE (sizeof(size_t) < sizeof(lua_Integer) ? MAX_SIZET \ : cast_sizet(LUA_MAXINTEGER)) -/* -** floor of the log2 of the maximum signed value for integral type 't'. -** (That is, maximum 'n' such that '2^n' fits in the given signed type.) -*/ -#define log2maxs(t) (l_numbits(t) - 2) - - /* ** test whether an unsigned value is a power of 2 (or zero) */ diff --git a/lstring.c b/lstring.c index 17c6fd8f51..75635142e9 100644 --- a/lstring.c +++ b/lstring.c @@ -50,7 +50,7 @@ int luaS_eqstr (TString *a, TString *b) { } -unsigned luaS_hash (const char *str, size_t l, unsigned seed) { +static unsigned luaS_hash (const char *str, size_t l, unsigned seed) { unsigned int h = seed ^ cast_uint(l); for (; l > 0; l--) h ^= ((h<<5) + (h>>2) + cast_byte(str[l - 1])); diff --git a/lstring.h b/lstring.h index 2eac222b08..1643c3d82b 100644 --- a/lstring.h +++ b/lstring.h @@ -54,7 +54,6 @@ #define eqshrstr(a,b) check_exp((a)->tt == LUA_VSHRSTR, (a) == (b)) -LUAI_FUNC unsigned luaS_hash (const char *str, size_t l, unsigned seed); LUAI_FUNC unsigned luaS_hashlongstr (TString *ts); LUAI_FUNC int luaS_eqstr (TString *a, TString *b); LUAI_FUNC void luaS_resize (lua_State *L, int newsize); From b352217b8498a5ed8f6c954b3da365fcbb89751f Mon Sep 17 00:00:00 2001 From: Roberto I Date: Fri, 17 Oct 2025 13:53:35 -0300 Subject: [PATCH 1106/1145] Standard allocator function added to the API That makes easier to redefine luaL_newstate. --- lauxlib.c | 12 ++++++------ lauxlib.h | 3 +++ manual/manual.of | 28 +++++++++++++++++++--------- 3 files changed, 28 insertions(+), 15 deletions(-) diff --git a/lauxlib.c b/lauxlib.c index adb3851e82..1bb41bb1da 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -742,7 +742,7 @@ typedef struct LoadF { static const char *getF (lua_State *L, void *ud, size_t *size) { LoadF *lf = (LoadF *)ud; - (void)L; /* not used */ + UNUSED(L); if (lf->n > 0) { /* are there pre-read characters to be read? */ *size = lf->n; /* return them (chars already in buffer) */ lf->n = 0; /* no more pre-read characters */ @@ -856,7 +856,7 @@ typedef struct LoadS { static const char *getS (lua_State *L, void *ud, size_t *size) { LoadS *ls = (LoadS *)ud; - (void)L; /* not used */ + UNUSED(L); if (ls->size == 0) return NULL; *size = ls->size; ls->size = 0; @@ -1046,8 +1046,8 @@ LUALIB_API const char *luaL_gsub (lua_State *L, const char *s, } -static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) { - (void)ud; (void)osize; /* not used */ +void *luaL_alloc (void *ud, void *ptr, size_t osize, size_t nsize) { + UNUSED(ud); UNUSED(osize); if (nsize == 0) { free(ptr); return NULL; @@ -1172,7 +1172,7 @@ static unsigned int luai_makeseed (void) { LUALIB_API unsigned int luaL_makeseed (lua_State *L) { - (void)L; /* unused */ + UNUSED(L); return luai_makeseed(); } @@ -1182,7 +1182,7 @@ LUALIB_API unsigned int luaL_makeseed (lua_State *L) { ** as a macro. */ LUALIB_API lua_State *(luaL_newstate) (void) { - lua_State *L = lua_newstate(l_alloc, NULL, luai_makeseed()); + lua_State *L = lua_newstate(luaL_alloc, NULL, luaL_makeseed(NULL)); if (l_likely(L)) { lua_atpanic(L, &panic); lua_setwarnf(L, warnfoff, L); /* default is warnings off */ diff --git a/lauxlib.h b/lauxlib.h index d8522098a7..7f1d3ca195 100644 --- a/lauxlib.h +++ b/lauxlib.h @@ -81,6 +81,9 @@ LUALIB_API int (luaL_checkoption) (lua_State *L, int arg, const char *def, LUALIB_API int (luaL_fileresult) (lua_State *L, int stat, const char *fname); LUALIB_API int (luaL_execresult) (lua_State *L, int stat); +LUALIB_API void *luaL_alloc (void *ud, void *ptr, size_t osize, + size_t nsize); + /* predefined references */ #define LUA_NOREF (-2) diff --git a/manual/manual.of b/manual/manual.of index beea41f96a..ad273d625d 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -3079,11 +3079,12 @@ the allocator must behave like @id{realloc}. In particular, the allocator returns @id{NULL} if and only if it cannot fulfill the request. -Here is a simple implementation for the @x{allocator function}. -It is used in the auxiliary library by @Lid{luaL_newstate}. +Here is a simple implementation for the @x{allocator function}, +corresponding to the function @Lid{luaL_alloc} from the +auxiliary library. @verbatim{ -static void *l_alloc (void *ud, void *ptr, size_t osize, - size_t nsize) { +void *luaL_alloc (void *ud, void *ptr, size_t osize, + size_t nsize) { (void)ud; (void)osize; /* not used */ if (nsize == 0) { free(ptr); @@ -5988,9 +5989,8 @@ it does not run it. @apii{0,0,-} Returns a value with a weak attempt for randomness. -(It produces that value based on the current date and time -and the address of an internal variable, -in case the machine has Address Space Layout Randomization.) +The parameter @id{L} can be @id{NULL} +if there is no Lua state available. } @@ -6046,8 +6046,9 @@ with @id{tname} in the registry. @apii{0,0,-} Creates a new Lua state. -It calls @Lid{lua_newstate} with an -allocator based on the @N{ISO C} allocation functions +It calls @Lid{lua_newstate} with @Lid{luaL_alloc} as +the allocator function and the result of @T{luaL_makeseed(NULL)} +as the seed, and then sets a warning function and a panic function @see{C-error} that print messages to the standard error output. @@ -6271,6 +6272,15 @@ in the registry @seeC{luaL_newmetatable}. } +@APIEntry{ +void *luaL_alloc (void *ud, void *ptr, size_t osize, size_t nsize);| + +A standard allocator function for Lua @seeF{lua_Alloc}, +built on top of the C functions @id{realloc} and @id{free}. + +} + + @APIEntry{ typedef struct luaL_Stream { FILE *f; From 26755cad99cd2a362a3f149114a2e7f05928db0a Mon Sep 17 00:00:00 2001 From: Roberto I Date: Sat, 18 Oct 2025 10:30:12 -0300 Subject: [PATCH 1107/1145] Added "attribute internal" to __MACH__ platforms Also, makefile does not add compiling options (LOCAL) to linker flags (MYLDFLAGS). --- llimits.h | 18 +++++++++--------- makefile | 2 +- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/llimits.h b/llimits.h index 2163254378..fc5cb276f6 100644 --- a/llimits.h +++ b/llimits.h @@ -303,21 +303,21 @@ typedef unsigned long l_uint32; ** LUAI_DDEF and LUAI_DDEC are marks for all extern (const) variables, ** none of which to be exported to outside modules (LUAI_DDEF for ** definitions and LUAI_DDEC for declarations). -** Elf/gcc (versions 3.2 and later) mark them as "hidden" to optimize -** access when Lua is compiled as a shared library. Not all elf targets -** support this attribute. Unfortunately, gcc does not offer a way to -** check whether the target offers that support, and those without -** support give a warning about it. To avoid these warnings, change to -** the default definition. +** Elf and MACH/gcc (versions 3.2 and later) mark them as "hidden" to +** optimize access when Lua is compiled as a shared library. Not all elf +** targets support this attribute. Unfortunately, gcc does not offer +** a way to check whether the target offers that support, and those +** without support give a warning about it. To avoid these warnings, +** change to the default definition. */ #if !defined(LUAI_FUNC) #if defined(__GNUC__) && ((__GNUC__*100 + __GNUC_MINOR__) >= 302) && \ - defined(__ELF__) /* { */ + (defined(__ELF__) || defined(__MACH__)) #define LUAI_FUNC __attribute__((visibility("internal"))) extern -#else /* }{ */ +#else #define LUAI_FUNC extern -#endif /* } */ +#endif #define LUAI_DDEC(dec) LUAI_FUNC dec #define LUAI_DDEF /* empty */ diff --git a/makefile b/makefile index 2a8ca48969..8674519f5f 100644 --- a/makefile +++ b/makefile @@ -72,7 +72,7 @@ LOCAL = $(TESTS) $(CWARNS) # For C89, "-std=c89 -DLUA_USE_C89" # Note that Linux/Posix options are not compatible with C89 MYCFLAGS= $(LOCAL) -std=c99 -DLUA_USE_LINUX -MYLDFLAGS= $(LOCAL) -Wl,-E +MYLDFLAGS= -Wl,-E MYLIBS= -ldl From fca974486d12aa29bb6d731fdb5b25055157ece8 Mon Sep 17 00:00:00 2001 From: Roberto I Date: Sat, 18 Oct 2025 10:34:42 -0300 Subject: [PATCH 1108/1145] Small change in 'trymt' Operation name can be diferent from metamethod name. --- lstrlib.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/lstrlib.c b/lstrlib.c index d9735903d4..23df839ea0 100644 --- a/lstrlib.c +++ b/lstrlib.c @@ -269,11 +269,18 @@ static int tonum (lua_State *L, int arg) { } -static void trymt (lua_State *L, const char *mtname) { +/* +** To be here, either the first operand was a string or the first +** operand didn't have a corresponding metamethod. (Otherwise, that +** other metamethod would have been called.) So, if this metamethod +** doesn't work, the only other option would be for the second +** operand to have a different metamethod. +*/ +static void trymt (lua_State *L, const char *mtkey, const char *opname) { lua_settop(L, 2); /* back to the original arguments */ if (l_unlikely(lua_type(L, 2) == LUA_TSTRING || - !luaL_getmetafield(L, 2, mtname))) - luaL_error(L, "attempt to %s a '%s' with a '%s'", mtname + 2, + !luaL_getmetafield(L, 2, mtkey))) + luaL_error(L, "attempt to %s a '%s' with a '%s'", opname, luaL_typename(L, -2), luaL_typename(L, -1)); lua_insert(L, -3); /* put metamethod before arguments */ lua_call(L, 2, 1); /* call metamethod */ @@ -284,7 +291,7 @@ static int arith (lua_State *L, int op, const char *mtname) { if (tonum(L, 1) && tonum(L, 2)) lua_arith(L, op); /* result will be on the top */ else - trymt(L, mtname); + trymt(L, mtname, mtname + 2); return 1; } From d4eff00234dc55dac4cb86b6187f5607c1254f9b Mon Sep 17 00:00:00 2001 From: Roberto I Date: Wed, 29 Oct 2025 13:14:48 -0300 Subject: [PATCH 1109/1145] Fixed initialization of global variables When calling 'luaK_storevar', the 'expdesc' for the variable must be created before the one for the expression, to satisfy the assumptions for register allocation. So, in a statement like 'global a = exp', where 'a' is actually '_ENV.a', this variable must be handled before the initializing expression 'exp'. --- lcode.c | 2 +- lparser.c | 41 +++++++++++++++++++++++++++++------------ testes/goto.lua | 22 ++++++++++++++++++++++ 3 files changed, 52 insertions(+), 13 deletions(-) diff --git a/lcode.c b/lcode.c index 429d4f8020..7c63abb2eb 100644 --- a/lcode.c +++ b/lcode.c @@ -1242,7 +1242,7 @@ static void codenot (FuncState *fs, expdesc *e) { ** Check whether expression 'e' is a short literal string */ static int isKstr (FuncState *fs, expdesc *e) { - return (e->k == VK && !hasjumps(e) && e->u.info <= MAXARG_B && + return (e->k == VK && !hasjumps(e) && e->u.info <= MAXINDEXRK && ttisshrstring(&fs->f->k[e->u.info])); } diff --git a/lparser.c b/lparser.c index dc646fea99..e3538c16f5 100644 --- a/lparser.c +++ b/lparser.c @@ -1875,6 +1875,33 @@ static lu_byte getglobalattribute (LexState *ls, lu_byte df) { } +/* +** Recursively traverse list of globals to be initalized. When +** going, generate table description for the global. In the end, +** after all indices have been generated, read list of initializing +** expressions. When returning, generate the assignment of the value on +** the stack to the corresponding table description. 'n' is the variable +** being handled, range [0, nvars - 1]. +*/ +static void initglobal (LexState *ls, int nvars, int firstidx, int n) { + if (n == nvars) { /* traversed all variables? */ + expdesc e; + int nexps = explist(ls, &e); /* read list of expressions */ + adjust_assign(ls, nvars, nexps, &e); + } + else { /* handle variable 'n' */ + FuncState *fs = ls->fs; + expdesc var; + TString *varname = getlocalvardesc(fs, firstidx + n)->vd.name; + buildglobal(ls, varname, &var); /* create global variable in 'var' */ + enterlevel(ls); /* control recursion depth */ + initglobal(ls, nvars, firstidx, n + 1); + leavelevel(ls); + storevartop(fs, &var); + } +} + + static void globalnames (LexState *ls, lu_byte defkind) { FuncState *fs = ls->fs; int nvars = 0; @@ -1885,18 +1912,8 @@ static void globalnames (LexState *ls, lu_byte defkind) { lastidx = new_varkind(ls, vname, kind); nvars++; } while (testnext(ls, ',')); - if (testnext(ls, '=')) { /* initialization? */ - expdesc e; - int i; - int nexps = explist(ls, &e); /* read list of expressions */ - adjust_assign(ls, nvars, nexps, &e); - for (i = 0; i < nvars; i++) { /* for each variable */ - expdesc var; - TString *varname = getlocalvardesc(fs, lastidx - i)->vd.name; - buildglobal(ls, varname, &var); /* create global variable in 'var' */ - storevartop(fs, &var); - } - } + if (testnext(ls, '=')) /* initialization? */ + initglobal(ls, nvars, lastidx - nvars + 1, 0); fs->nactvar = cast_short(fs->nactvar + nvars); /* activate declaration */ } diff --git a/testes/goto.lua b/testes/goto.lua index 3310314d8a..a692a623b4 100644 --- a/testes/goto.lua +++ b/testes/goto.lua @@ -432,5 +432,27 @@ do print "testing initialization in global declarations" _ENV.a, _ENV.b, _ENV.c, _ENV.d = nil -- erase these globals end +do + global table, string + -- global initialization when names don't fit in K + + -- to fill constant table + local code = {} + for i = 1, 300 do code[i] = "'" .. i .. "'" end + code = table.concat(code, ",") + code = string.format([[ + return function (_ENV) + local dummy = {%s} -- fill initial positions in constant table, + -- so that initialization must use registers for global names + global a, b, c = 10, 20, 30 + end]], code) + + local fun = assert(load(code))() + + local env = {} + fun(env) + assert(env.a == 10 and env.b == 20 and env.c == 30) +end + print'OK' From 0149b781d438091ce086449101a916e9b4456b4e Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 30 Oct 2025 10:39:55 -0300 Subject: [PATCH 1110/1145] Case VVARGIND added to luaK_storevar In a global initialization, the variable does not pass through 'check_readonly', and therefore a VVARGIND is not normalized to a VINDEXED. --- lcode.c | 4 ++++ testes/vararg.lua | 12 ++++++++++++ 2 files changed, 16 insertions(+) diff --git a/lcode.c b/lcode.c index 7c63abb2eb..f09edb5f88 100644 --- a/lcode.c +++ b/lcode.c @@ -1109,6 +1109,10 @@ void luaK_storevar (FuncState *fs, expdesc *var, expdesc *ex) { codeABRK(fs, OP_SETFIELD, var->u.ind.t, var->u.ind.idx, ex); break; } + case VVARGIND: { + fs->f->flag |= PF_VATAB; /* function will need a vararg table */ + /* now, assignment is to a regular table */ + } /* FALLTHROUGH */ case VINDEXED: { codeABRK(fs, OP_SETTABLE, var->u.ind.t, var->u.ind.idx, ex); break; diff --git a/testes/vararg.lua b/testes/vararg.lua index 5711f78b84..840c3eee49 100644 --- a/testes/vararg.lua +++ b/testes/vararg.lua @@ -184,6 +184,18 @@ do -- _ENV as vararg parameter a = 10 end ]] assert(string.find(msg, "const variable 'a'")) + + local function aux (... | _ENV) + global a; a = 10 + return a + end + assert(aux() == 10) + + local function aux (... | _ENV) + global a = 10 + return a + end + assert(aux() == 10) end From d342328e5b24c9b3c6c5b33bfcf9f8534210b8e6 Mon Sep 17 00:00:00 2001 From: Roberto I Date: Thu, 30 Oct 2025 11:07:01 -0300 Subject: [PATCH 1111/1145] Vertical bar removed from syntax of vararg table The syntax 'function foo (a, b, ...arg)' is already used by JavaScript for this same semantics, so it seems natural to use the same notation in Lua. --- lparser.c | 4 ++-- manual/manual.of | 8 +++----- testes/locals.lua | 4 ++-- testes/memerr.lua | 4 ++-- testes/vararg.lua | 22 +++++++++++----------- 5 files changed, 20 insertions(+), 22 deletions(-) diff --git a/lparser.c b/lparser.c index e3538c16f5..40a30ff685 100644 --- a/lparser.c +++ b/lparser.c @@ -1079,8 +1079,8 @@ static void parlist (LexState *ls) { } case TK_DOTS: { varargk |= PF_ISVARARG; - luaX_next(ls); - if (testnext(ls, '|')) { + luaX_next(ls); /* skip '...' */ + if (ls->t.token == TK_NAME) { new_varkind(ls, str_checkname(ls), RDKVAVAR); varargk |= PF_VAVAR; } diff --git a/manual/manual.of b/manual/manual.of index ad273d625d..0127df0276 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -2354,8 +2354,7 @@ initialized with the argument values: @Produc{ @producname{parlist}@producbody{namelist @bnfopt{@bnfter{,} varargparam} @Or varargparam} -@producname{varargparam}@producbody{@bnfter{...} - @bnfopt{@bnfter{|} @bnfNter{Name}}} +@producname{varargparam}@producbody{@bnfter{...} @bnfopt{@bnfNter{Name}}} } When a Lua function is called, it adjusts its list of @x{arguments} to @@ -2396,7 +2395,7 @@ g(5, r()) a=5, b=1, ... -> 2 3 } The presence of a varag table in a variadic function is indicated -by the @T{|name} syntax after the three dots. +by a name after the three dots. When present, a vararg table behaves like a read-only local variable with the given name that is initialized with a table. @@ -9773,8 +9772,7 @@ and @bnfNter{LiteralString}, see @See{lexical}.) @producname{parlist}@producbody{namelist @bnfopt{@bnfter{,} varargparam} @Or varargparam} -@producname{varargparam}@producbody{@bnfter{...} - @bnfopt{@bnfter{|} @bnfNter{Name}}} +@producname{varargparam}@producbody{@bnfter{...} @bnfopt{@bnfNter{Name}}} @producname{tableconstructor}@producbody{@bnfter{@Open} @bnfopt{fieldlist} @bnfter{@Close}} diff --git a/testes/locals.lua b/testes/locals.lua index 5222802f44..6cd1054764 100644 --- a/testes/locals.lua +++ b/testes/locals.lua @@ -310,7 +310,7 @@ do -- testing presence of second argument local function foo (howtoclose, obj, n) local ca -- copy of 'a' visible inside its close metamethod do - local a = func2close(function (... | t) + local a = func2close(function (...t) assert(select("#", ...) == n) assert(t.n == n and t[1] == ca and (t.n < 2 or t[2] == obj)) ca = 15 -- final value to be returned if howtoclose=="scope" @@ -910,7 +910,7 @@ do local extrares -- result from extra yield (if any) - local function check (body, extra, ...|t) + local function check (body, extra, ...t) local co = coroutine.wrap(body) if extra then extrares = co() -- runs until first (extra) yield diff --git a/testes/memerr.lua b/testes/memerr.lua index 69d2ef8550..2cc8f48173 100644 --- a/testes/memerr.lua +++ b/testes/memerr.lua @@ -126,13 +126,13 @@ testamem("coroutine creation", function() end) do -- vararg tables - local function pack (... | t) return t end + local function pack (...t) return t end local b = testamem("vararg table", function () return pack(10, 20, 30, 40, "hello") end) assert(b.aloc == 3) -- new table uses three memory blocks -- table optimized away - local function sel (n, ...|arg) return arg[n] + arg.n end + local function sel (n, ...arg) return arg[n] + arg.n end local b = testamem("optimized vararg table", function () return sel(2.0, 20, 30) end) assert(b.res == 32 and b.aloc == 0) -- no memory needed for this case diff --git a/testes/vararg.lua b/testes/vararg.lua index 840c3eee49..a01598ff3b 100644 --- a/testes/vararg.lua +++ b/testes/vararg.lua @@ -3,7 +3,7 @@ print('testing vararg') -local function f (a, ...|t) +local function f (a, ...t) local x = {n = select('#', ...), ...} assert(x.n == t.n) for i = 1, x.n do @@ -20,7 +20,7 @@ local function c12 (...) return res, 2 end -local function vararg (... | t) return t end +local function vararg (... t) return t end local call = function (f, args) return f(table.unpack(args, 1, args.n)) end @@ -153,8 +153,8 @@ end do -- vararg parameter used in nested functions - local function foo (... | tab1) - return function (... | tab2) + local function foo (...tab1) + return function (...tab2) return {tab1, tab2} end end @@ -165,11 +165,11 @@ do -- vararg parameter used in nested functions end do -- vararg parameter is read-only - local st, msg = load("return function (... | t) t = 10 end") + local st, msg = load("return function (... t) t = 10 end") assert(string.find(msg, "const variable 't'")) local st, msg = load[[ - local function foo (... | extra) + local function foo (...extra) return function (...) extra = nil end end ]] @@ -179,19 +179,19 @@ end do -- _ENV as vararg parameter local st, msg = load[[ - local function aux (... | _ENV) + local function aux (... _ENV) global a a = 10 end ]] assert(string.find(msg, "const variable 'a'")) - local function aux (... | _ENV) + local function aux (..._ENV) global a; a = 10 return a end assert(aux() == 10) - local function aux (... | _ENV) + local function aux (... _ENV) global a = 10 return a end @@ -200,7 +200,7 @@ end do -- access to vararg parameter - local function notab (keys, t, ... | v) + local function notab (keys, t, ...v) for _, k in pairs(keys) do assert(t[k] == v[k]) end @@ -216,7 +216,7 @@ do -- access to vararg parameter assert(m == collectgarbage"count") -- writing to the vararg table - local function foo (... | t) + local function foo (...t) t[1] = t[1] + 10 return t[1] end From f791bb69061c15f73395c5a95958ac18af5ef764 Mon Sep 17 00:00:00 2001 From: Roberto I Date: Fri, 31 Oct 2025 14:48:55 -0300 Subject: [PATCH 1112/1145] Details - New macro l_strcoll to ease changing 'strcoll' to something else. - MAXINDEXRK==1 in 'ltests.h' is enough to run test 'code.lua'. - Removed unused '#include' in 'lutf8lib.c'. --- ltests.h | 7 +------ lutf8lib.c | 1 - lvm.c | 10 +++++++++- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/ltests.h b/ltests.h index 26ffed83de..93096da810 100644 --- a/ltests.h +++ b/ltests.h @@ -142,12 +142,7 @@ LUA_API void *debug_realloc (void *ud, void *block, #define STRCACHE_N 23 #define STRCACHE_M 5 - -/* -** This one is not compatible with tests for opcode optimizations, -** as it blocks some optimizations -#define MAXINDEXRK 0 -*/ +#define MAXINDEXRK 1 /* diff --git a/lutf8lib.c b/lutf8lib.c index df49c901d6..b7f3fe1e16 100644 --- a/lutf8lib.c +++ b/lutf8lib.c @@ -10,7 +10,6 @@ #include "lprefix.h" -#include #include #include #include diff --git a/lvm.c b/lvm.c index 3ce7e87f8f..efb0db289e 100644 --- a/lvm.c +++ b/lvm.c @@ -372,6 +372,14 @@ void luaV_finishset (lua_State *L, const TValue *t, TValue *key, } +/* +** Function to be used for 0-terminated string order comparison +*/ +#if !defined(l_strcoll) +#define l_strcoll strcoll +#endif + + /* ** Compare two strings 'ts1' x 'ts2', returning an integer less-equal- ** -greater than zero if 'ts1' is less-equal-greater than 'ts2'. @@ -386,7 +394,7 @@ static int l_strcmp (const TString *ts1, const TString *ts2) { size_t rl2; const char *s2 = getlstr(ts2, rl2); for (;;) { /* for each segment */ - int temp = strcoll(s1, s2); + int temp = l_strcoll(s1, s2); if (temp != 0) /* not equal? */ return temp; /* done */ else { /* strings are equal up to a '\0' */ From e44f3a2ffc7ced5e75cca7657aaa60ef27da89aa Mon Sep 17 00:00:00 2001 From: Roberto I Date: Sat, 8 Nov 2025 11:43:42 -0300 Subject: [PATCH 1113/1145] Global initialization checks name conflict Initialization "global a = 10" raises an error if global 'a' is already defined, that is, it has a non-nil value. --- lcode.c | 16 ++++++++++++++++ lcode.h | 2 ++ ldebug.c | 8 ++++++++ ldebug.h | 1 + ljumptab.h | 1 + lopcodes.c | 1 + lopcodes.h | 2 ++ lopnames.h | 1 + lparser.c | 19 ++++++++++++++++--- lvm.c | 6 ++++++ manual/manual.of | 14 +++++++++++--- testes/goto.lua | 21 ++++++++++++++++++++- testes/memerr.lua | 4 ++-- 13 files changed, 87 insertions(+), 9 deletions(-) diff --git a/lcode.c b/lcode.c index f09edb5f88..d82f8263e1 100644 --- a/lcode.c +++ b/lcode.c @@ -705,6 +705,22 @@ static void luaK_float (FuncState *fs, int reg, lua_Number f) { } +/* +** Get the value of 'var' in a register and generate an opcode to check +** whether that register is nil. 'k' is the index of the variable name +** in the list of constants. If its value cannot be encoded in Bx, a 0 +** will use '?' for the name. +*/ +void luaK_codecheckglobal (FuncState *fs, expdesc *var, int k, int line) { + luaK_exp2anyreg(fs, var); + luaK_fixline(fs, line); + k = (k >= MAXARG_Bx) ? 0 : k + 1; + luaK_codeABx(fs, OP_ERRNNIL, var->u.info, k); + luaK_fixline(fs, line); + freeexp(fs, var); +} + + /* ** Convert a constant in 'v' into an expression description 'e' */ diff --git a/lcode.h b/lcode.h index f6397a3cde..09e5c802b0 100644 --- a/lcode.h +++ b/lcode.h @@ -68,6 +68,8 @@ LUAI_FUNC int luaK_codevABCk (FuncState *fs, OpCode o, int A, int B, int C, LUAI_FUNC int luaK_exp2const (FuncState *fs, const expdesc *e, TValue *v); LUAI_FUNC void luaK_fixline (FuncState *fs, int line); LUAI_FUNC void luaK_nil (FuncState *fs, int from, int n); +LUAI_FUNC void luaK_codecheckglobal (FuncState *fs, expdesc *var, int k, + int line); LUAI_FUNC void luaK_reserveregs (FuncState *fs, int n); LUAI_FUNC void luaK_checkstack (FuncState *fs, int n); LUAI_FUNC void luaK_int (FuncState *fs, int reg, lua_Integer n); diff --git a/ldebug.c b/ldebug.c index 9110f437bf..abead91ce6 100644 --- a/ldebug.c +++ b/ldebug.c @@ -814,6 +814,14 @@ l_noret luaG_ordererror (lua_State *L, const TValue *p1, const TValue *p2) { } +l_noret luaG_errnnil (lua_State *L, LClosure *cl, int k) { + const char *globalname = "?"; /* default name if k == 0 */ + if (k > 0) + kname(cl->p, k - 1, &globalname); + luaG_runerror(L, "global '%s' already defined", globalname); +} + + /* add src:line information to 'msg' */ const char *luaG_addinfo (lua_State *L, const char *msg, TString *src, int line) { diff --git a/ldebug.h b/ldebug.h index 2bfce3cb5e..20d07818b4 100644 --- a/ldebug.h +++ b/ldebug.h @@ -53,6 +53,7 @@ LUAI_FUNC l_noret luaG_tointerror (lua_State *L, const TValue *p1, const TValue *p2); LUAI_FUNC l_noret luaG_ordererror (lua_State *L, const TValue *p1, const TValue *p2); +LUAI_FUNC l_noret luaG_errnnil (lua_State *L, LClosure *cl, int k); LUAI_FUNC l_noret luaG_runerror (lua_State *L, const char *fmt, ...); LUAI_FUNC const char *luaG_addinfo (lua_State *L, const char *msg, TString *src, int line); diff --git a/ljumptab.h b/ljumptab.h index f896b6585b..52fa6d746e 100644 --- a/ljumptab.h +++ b/ljumptab.h @@ -107,6 +107,7 @@ static const void *const disptab[NUM_OPCODES] = { &&L_OP_CLOSURE, &&L_OP_VARARG, &&L_OP_GETVARG, +&&L_OP_ERRNNIL, &&L_OP_VARARGPREP, &&L_OP_EXTRAARG diff --git a/lopcodes.c b/lopcodes.c index 47458e40ca..7e182315bc 100644 --- a/lopcodes.c +++ b/lopcodes.c @@ -103,6 +103,7 @@ LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = { ,opmode(0, 0, 0, 0, 1, iABx) /* OP_CLOSURE */ ,opmode(0, 1, 0, 0, 1, iABC) /* OP_VARARG */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_GETVARG */ + ,opmode(0, 0, 0, 0, 0, iABx) /* OP_ERRNNIL */ ,opmode(0, 0, 1, 0, 1, iABC) /* OP_VARARGPREP */ ,opmode(0, 0, 0, 0, 0, iAx) /* OP_EXTRAARG */ }; diff --git a/lopcodes.h b/lopcodes.h index 82bba721ff..f7bded2cc1 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -340,6 +340,8 @@ OP_VARARG,/* A C R[A], R[A+1], ..., R[A+C-2] = vararg */ OP_GETVARG, /* A B C R[A] := R[B][R[C]], R[B] is vararg parameter */ +OP_ERRNNIL,/* A Bx raise error if R[A] ~= nil (K[Bx] is global name)*/ + OP_VARARGPREP,/* (adjust vararg parameters) */ OP_EXTRAARG/* Ax extra (larger) argument for previous opcode */ diff --git a/lopnames.h b/lopnames.h index aa7bea77c9..0554a2e9a1 100644 --- a/lopnames.h +++ b/lopnames.h @@ -95,6 +95,7 @@ static const char *const opnames[] = { "CLOSURE", "VARARG", "GETVARG", + "ERRNNIL", "VARARGPREP", "EXTRAARG", NULL diff --git a/lparser.c b/lparser.c index 40a30ff685..77141e79f9 100644 --- a/lparser.c +++ b/lparser.c @@ -1875,6 +1875,16 @@ static lu_byte getglobalattribute (LexState *ls, lu_byte df) { } +static void checkglobal (LexState *ls, TString *varname, int line) { + FuncState *fs = ls->fs; + expdesc var; + int k; + buildglobal(ls, varname, &var); /* create global variable in 'var' */ + k = var.u.ind.keystr; /* index of global name in 'k' */ + luaK_codecheckglobal(fs, &var, k, line); +} + + /* ** Recursively traverse list of globals to be initalized. When ** going, generate table description for the global. In the end, @@ -1883,7 +1893,8 @@ static lu_byte getglobalattribute (LexState *ls, lu_byte df) { ** the stack to the corresponding table description. 'n' is the variable ** being handled, range [0, nvars - 1]. */ -static void initglobal (LexState *ls, int nvars, int firstidx, int n) { +static void initglobal (LexState *ls, int nvars, int firstidx, int n, + int line) { if (n == nvars) { /* traversed all variables? */ expdesc e; int nexps = explist(ls, &e); /* read list of expressions */ @@ -1895,8 +1906,9 @@ static void initglobal (LexState *ls, int nvars, int firstidx, int n) { TString *varname = getlocalvardesc(fs, firstidx + n)->vd.name; buildglobal(ls, varname, &var); /* create global variable in 'var' */ enterlevel(ls); /* control recursion depth */ - initglobal(ls, nvars, firstidx, n + 1); + initglobal(ls, nvars, firstidx, n + 1, line); leavelevel(ls); + checkglobal(ls, varname, line); storevartop(fs, &var); } } @@ -1913,7 +1925,7 @@ static void globalnames (LexState *ls, lu_byte defkind) { nvars++; } while (testnext(ls, ',')); if (testnext(ls, '=')) /* initialization? */ - initglobal(ls, nvars, lastidx - nvars + 1, 0); + initglobal(ls, nvars, lastidx - nvars + 1, 0, ls->linenumber); fs->nactvar = cast_short(fs->nactvar + nvars); /* activate declaration */ } @@ -1943,6 +1955,7 @@ static void globalfunc (LexState *ls, int line) { fs->nactvar++; /* enter its scope */ buildglobal(ls, fname, &var); body(ls, &b, 0, ls->linenumber); /* compile and return closure in 'b' */ + checkglobal(ls, fname, line); luaK_storevar(fs, &var, &b); luaK_fixline(fs, line); /* definition "happens" in the first line */ } diff --git a/lvm.c b/lvm.c index efb0db289e..2a9fb67a7e 100644 --- a/lvm.c +++ b/lvm.c @@ -1940,6 +1940,12 @@ void luaV_execute (lua_State *L, CallInfo *ci) { luaT_getvararg(ci, ra, rc); vmbreak; } + vmcase(OP_ERRNNIL) { + TValue *ra = vRA(i); + if (!ttisnil(ra)) + halfProtect(luaG_errnnil(L, cl, GETARG_Bx(i))); + vmbreak; + } vmcase(OP_VARARGPREP) { ProtectNT(luaT_adjustvarargs(L, ci, cl->p)); if (l_unlikely(trap)) { /* previous "Protect" updated trap */ diff --git a/manual/manual.of b/manual/manual.of index 0127df0276..eaf0ce7890 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -1660,9 +1660,15 @@ The declaration can include an initialization: @producname{stat}@producbody{@Rw{global} attnamelist @bnfopt{@bnfter{=} explist}} } -If present, an initial assignment has the same semantics +If there is no initialization, +local variables are initialized with @nil; +global variables are left unchanged. +Otherwise, the initialization gets the same adjustment of a multiple assignment @see{assignment}. -Otherwise, all local variables are initialized with @nil. +Moreover, for global variables, +the initialization will raise a runtime error +if the variable is already defined, +that is, it has a non-nil value. The list of names may be prefixed by an attribute (a name between angle brackets) @@ -2312,8 +2318,10 @@ global function f () @rep{body} end } translates to @verbatim{ -global f; f = function () @rep{body} end +global f; global f = function () @rep{body} end } +The second @Rw{global} makes the assignment an initialization, +which will raise an error if that global is already defined. The @emphx{colon} syntax is used to emulate @def{methods}, diff --git a/testes/goto.lua b/testes/goto.lua index a692a623b4..906208b553 100644 --- a/testes/goto.lua +++ b/testes/goto.lua @@ -293,6 +293,7 @@ end foo() -------------------------------------------------------------------------- +-- check for compilation errors local function checkerr (code, err) local st, msg = load(code) assert(not st and string.find(msg, err)) @@ -414,22 +415,26 @@ end do print "testing initialization in global declarations" global a, b, c = 10, 20, 30 assert(_ENV.a == 10 and b == 20 and c == 30) + _ENV.a = nil; _ENV.b = nil; _ENV.c = nil; global a, b, c = 10 assert(_ENV.a == 10 and b == nil and c == nil) + _ENV.a = nil; _ENV.b = nil; _ENV.c = nil; global table global a, b, c, d = table.unpack{1, 2, 3, 6, 5} assert(_ENV.a == 1 and b == 2 and c == 3 and d == 6) + a = nil; b = nil; c = nil; d = nil local a, b = 100, 200 do global a, b = a, b end assert(_ENV.a == 100 and _ENV.b == 200) + _ENV.a = nil; _ENV.b = nil - _ENV.a, _ENV.b, _ENV.c, _ENV.d = nil -- erase these globals + assert(_ENV.a == nil and _ENV.b == nil and _ENV.c == nil and _ENV.d == nil) end do @@ -454,5 +459,19 @@ do assert(env.a == 10 and env.b == 20 and env.c == 30) end + +do -- testing global redefinitions + -- cannot use 'checkerr' as errors are not compile time + global pcall + local f = assert(load("global print = 10")) + local st, msg = pcall(f) + assert(string.find(msg, "global 'print' already defined")) + + local f = assert(load("local _ENV = {AA = false}; global AA = 10")) + local st, msg = pcall(f) + assert(string.find(msg, "global 'AA' already defined")) + +end + print'OK' diff --git a/testes/memerr.lua b/testes/memerr.lua index 2cc8f48173..9c940ca79a 100644 --- a/testes/memerr.lua +++ b/testes/memerr.lua @@ -166,9 +166,9 @@ local function expand (n,s) e, s, expand(n-1,s), e) end -G=0; collectgarbage(); a =collectgarbage("count") +G=0; collectgarbage() load(expand(20,"G=G+1"))() -assert(G==20); collectgarbage(); -- assert(gcinfo() <= a+1) +assert(G==20); collectgarbage() G = nil testamem("running code on new thread", function () From 81f4def54f440e045b1401f11ef78b65b56b7abe Mon Sep 17 00:00:00 2001 From: Roberto I Date: Tue, 11 Nov 2025 14:36:16 -0300 Subject: [PATCH 1114/1145] Correction in line info for semantic errors Semantic errors should refer the last used token, not the next one. --- lcode.c | 1 + testes/errors.lua | 64 +++++++++++++++++++++++++++++++---------------- 2 files changed, 43 insertions(+), 22 deletions(-) diff --git a/lcode.c b/lcode.c index d82f8263e1..95ef900cfd 100644 --- a/lcode.c +++ b/lcode.c @@ -45,6 +45,7 @@ l_noret luaK_semerror (LexState *ls, const char *fmt, ...) { va_list argp; pushvfstring(ls->L, argp, fmt, msg); ls->t.token = 0; /* remove "near " from final message */ + ls->linenumber = ls->lastline; /* back to line of last used token */ luaX_syntaxerror(ls, msg); } diff --git a/testes/errors.lua b/testes/errors.lua index 00a43fc69b..c9d850994b 100644 --- a/testes/errors.lua +++ b/testes/errors.lua @@ -418,28 +418,28 @@ end -- testing line error -local function lineerror (s, l) +local function lineerror (s, l, w) local err,msg = pcall(load(s)) local line = tonumber(string.match(msg, ":(%d+):")) - assert(line == l or (not line and not l)) + assert((line == l or (not line and not l)) and string.find(msg, w)) end -lineerror("local a\n for i=1,'a' do \n print(i) \n end", 2) -lineerror("\n local a \n for k,v in 3 \n do \n print(k) \n end", 3) -lineerror("\n\n for k,v in \n 3 \n do \n print(k) \n end", 4) -lineerror("function a.x.y ()\na=a+1\nend", 1) +lineerror("local a\n for i=1,'a' do \n print(i) \n end", 2, "limit") +lineerror("\n local a \n for k,v in 3 \n do \n print(k) \n end", 3, "to call") +lineerror("\n\n for k,v in \n 3 \n do \n print(k) \n end", 4, "to call") +lineerror("function a.x.y ()\na=a+1\nend", 1, "index") -lineerror("a = \na\n+\n{}", 3) -lineerror("a = \n3\n+\n(\n4\n/\nprint)", 6) -lineerror("a = \nprint\n+\n(\n4\n/\n7)", 3) +lineerror("a = \na\n+\n{}", 3, "arithmetic") +lineerror("a = \n3\n+\n(\n4\n/\nprint)", 6, "arithmetic") +lineerror("a = \nprint\n+\n(\n4\n/\n7)", 3, "arithmetic") -lineerror("a\n=\n-\n\nprint\n;", 3) +lineerror("a\n=\n-\n\nprint\n;", 3, "arithmetic") lineerror([[ a ( -- << 23) -]], 2) +]], 2, "call") lineerror([[ local a = {x = 13} @@ -449,7 +449,7 @@ x ( -- << 23 ) -]], 5) +]], 5, "call") lineerror([[ local a = {x = 13} @@ -459,17 +459,17 @@ x ( 23 + a ) -]], 6) +]], 6, "arithmetic") local p = [[ function g() f() end function f(x) error('a', XX) end g() ]] -XX=3;lineerror((p), 3) -XX=0;lineerror((p), false) -XX=1;lineerror((p), 2) -XX=2;lineerror((p), 1) +XX=3;lineerror((p), 3, "a") +XX=0;lineerror((p), false, "a") +XX=1;lineerror((p), 2, "a") +XX=2;lineerror((p), 1, "a") _G.XX, _G.g, _G.f = nil @@ -477,7 +477,7 @@ lineerror([[ local b = false if not b then error 'test' -end]], 3) +end]], 3, "test") lineerror([[ local b = false @@ -487,7 +487,7 @@ if not b then error 'test' end end -end]], 5) +end]], 5, "test") lineerror([[ _ENV = 1 @@ -495,7 +495,7 @@ global function foo () local a = 10 return a end -]], 2) +]], 2, "index") -- bug in 5.4.0 @@ -503,17 +503,37 @@ lineerror([[ local a = 0 local b = 1 local c = b % a -]], 3) +]], 3, "perform") do -- Force a negative estimate for base line. Error in instruction 2 -- (after VARARGPREP, GETGLOBAL), with first absolute line information -- (forced by too many lines) in instruction 0. local s = string.format("%s return __A.x", string.rep("\n", 300)) - lineerror(s, 301) + lineerror(s, 301, "index") end +local function stxlineerror (s, l, w) + local err,msg = load(s) + local line = tonumber(string.match(msg, ":(%d+):")) + assert((line == l or (not line and not l)) and string.find(msg, w, 1, true)) +end + +stxlineerror([[ +::L1:: +::L1:: +]], 2, "already defined") + +stxlineerror([[ +global none +local x = b +]], 2, "not declared") + +stxlineerror([[ +local a, b +]], 1, "multiple") + if not _soft then -- several tests that exhaust the Lua stack collectgarbage() From 5b7d9987642f72d44223a8e5e79e013bb2b3d579 Mon Sep 17 00:00:00 2001 From: Roberto I Date: Tue, 11 Nov 2025 14:40:30 -0300 Subject: [PATCH 1115/1145] External strings are as good as internal ones A '__mode' metafield and an "n" key both can be external strings. --- lgc.c | 6 +++--- ltm.c | 2 +- lvm.c | 9 +++++++-- testes/strings.lua | 17 +++++++++++++++++ 4 files changed, 28 insertions(+), 6 deletions(-) diff --git a/lgc.c b/lgc.c index a775b6e510..60f042c7a8 100644 --- a/lgc.c +++ b/lgc.c @@ -594,10 +594,10 @@ static void traversestrongtable (global_State *g, Table *h) { */ static int getmode (global_State *g, Table *h) { const TValue *mode = gfasttm(g, h->metatable, TM_MODE); - if (mode == NULL || !ttisshrstring(mode)) - return 0; /* ignore non-(short)string modes */ + if (mode == NULL || !ttisstring(mode)) + return 0; /* ignore non-string modes */ else { - const char *smode = getshrstr(tsvalue(mode)); + const char *smode = getstr(tsvalue(mode)); const char *weakkey = strchr(smode, 'k'); const char *weakvalue = strchr(smode, 'v'); return ((weakkey != NULL) << 1) | (weakvalue != NULL); diff --git a/ltm.c b/ltm.c index 92a03e71be..8d64235e81 100644 --- a/ltm.c +++ b/ltm.c @@ -287,7 +287,7 @@ void luaT_getvararg (CallInfo *ci, StkId ra, TValue *rc) { return; } } - else if (ttisshrstring(rc)) { /* short-string value? */ + else if (ttisstring(rc)) { /* string value? */ size_t len; const char *s = getlstr(tsvalue(rc), len); if (len == 1 && s[0] == 'n') { /* key is "n"? */ diff --git a/lvm.c b/lvm.c index 2a9fb67a7e..2c868c2128 100644 --- a/lvm.c +++ b/lvm.c @@ -657,6 +657,11 @@ int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2) { #define tostring(L,o) \ (ttisstring(o) || (cvt2str(o) && (luaO_tostring(L, o), 1))) +/* +** Check whether object is a short empty string to optimize concatenation. +** (External strings can be empty too; they will be concatenated like +** non-empty ones.) +*/ #define isemptystr(o) (ttisshrstring(o) && tsvalue(o)->shrlen == 0) /* copy strings in stack from top - n up to top - 1 to buffer */ @@ -691,8 +696,8 @@ void luaV_concat (lua_State *L, int total) { setobjs2s(L, top - 2, top - 1); /* result is second op. */ } else { - /* at least two non-empty string values; get as many as possible */ - size_t tl = tsslen(tsvalue(s2v(top - 1))); + /* at least two string values; get as many as possible */ + size_t tl = tsslen(tsvalue(s2v(top - 1))); /* total length */ TString *ts; /* collect total length and number of strings */ for (n = 1; n < total && tostring(L, s2v(top - n - 1)); n++) { diff --git a/testes/strings.lua b/testes/strings.lua index 46912d4392..84ff115483 100644 --- a/testes/strings.lua +++ b/testes/strings.lua @@ -540,6 +540,23 @@ else assert(y == x) local z = T.externstr(x) -- external allocated long string assert(z == y) + + local e = T.externstr("") -- empty external string + assert(e .. "x" == "x" and "x" .. e == "x") + assert(e .. e == "" and #e == 0) + + -- external string as the "n" key in vararg table + local n = T.externstr("n") + local n0 = T.externstr("n\0") + local function aux (...t) assert(t[n0] == nil); return t[n] end + assert(aux(10, 20, 30) == 3) + + -- external string as mode in weak table + local t = setmetatable({}, {__mode = T.externstr("kv")}) + t[{}] = {} + assert(next(t)) + collectgarbage() + assert(next(t) == nil) end print('OK') From 4cf498210e6a60637a7abb06d32460ec21efdbdc Mon Sep 17 00:00:00 2001 From: Roberto I Date: Tue, 11 Nov 2025 15:11:06 -0300 Subject: [PATCH 1116/1145] '__pairs' can also return a to-be-closed object --- lbaselib.c | 13 +++++++------ manual/manual.of | 4 ++-- testes/nextvar.lua | 7 ++++++- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/lbaselib.c b/lbaselib.c index b296c4b761..891bb90f48 100644 --- a/lbaselib.c +++ b/lbaselib.c @@ -279,21 +279,22 @@ static int luaB_next (lua_State *L) { static int pairscont (lua_State *L, int status, lua_KContext k) { (void)L; (void)status; (void)k; /* unused */ - return 3; + return 4; /* __pairs did all the work, just return its results */ } static int luaB_pairs (lua_State *L) { luaL_checkany(L, 1); if (luaL_getmetafield(L, 1, "__pairs") == LUA_TNIL) { /* no metamethod? */ - lua_pushcfunction(L, luaB_next); /* will return generator, */ - lua_pushvalue(L, 1); /* state, */ - lua_pushnil(L); /* and initial value */ + lua_pushcfunction(L, luaB_next); /* will return generator and */ + lua_pushvalue(L, 1); /* state */ + lua_pushnil(L); /* initial value */ + lua_pushnil(L); /* to-be-closed object */ } else { lua_pushvalue(L, 1); /* argument 'self' to metamethod */ - lua_callk(L, 1, 3, 0, pairscont); /* get 3 values from metamethod */ + lua_callk(L, 1, 4, 0, pairscont); /* get 4 values from metamethod */ } - return 3; + return 4; } diff --git a/manual/manual.of b/manual/manual.of index eaf0ce7890..9b6976ca05 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -6799,11 +6799,11 @@ In particular, you may set existing fields to nil. @LibEntry{pairs (t)| If @id{t} has a metamethod @idx{__pairs}, -calls it with @id{t} as argument and returns the first three +calls it with @id{t} as argument and returns the first four results from the call. Otherwise, -returns three values: the @Lid{next} function, the table @id{t}, and @nil, +returns the @Lid{next} function, the table @id{t}, plus two @nil values, so that the construction @verbatim{ for k,v in pairs(t) do @rep{body} end diff --git a/testes/nextvar.lua b/testes/nextvar.lua index 7e5bed5685..098e7891c9 100644 --- a/testes/nextvar.lua +++ b/testes/nextvar.lua @@ -905,13 +905,18 @@ local function foo1 (e,i) if i <= e.n then return i,a[i] end end -setmetatable(a, {__pairs = function (x) return foo, x, 0 end}) +local closed = false +setmetatable(a, {__pairs = function (x) + local tbc = setmetatable({}, {__close = function () closed = true end}) + return foo, x, 0, tbc + end}) local i = 0 for k,v in pairs(a) do i = i + 1 assert(k == i and v == k+1) end +assert(closed) -- 'tbc' has been closed a.n = 5 a[3] = 30 From d94f7ba3040eb06895d7305014e88157d3bfd1a1 Mon Sep 17 00:00:00 2001 From: Roberto I Date: Mon, 24 Nov 2025 11:39:46 -0300 Subject: [PATCH 1117/1145] Details Comments, capitalization in the manual, globals in test 'heady.lua' --- lopcodes.h | 5 ++++- manual/manual.of | 16 +++++++++------- testes/heavy.lua | 18 ++++++++++-------- 3 files changed, 23 insertions(+), 16 deletions(-) diff --git a/lopcodes.h b/lopcodes.h index f7bded2cc1..f5c95151ba 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -340,7 +340,7 @@ OP_VARARG,/* A C R[A], R[A+1], ..., R[A+C-2] = vararg */ OP_GETVARG, /* A B C R[A] := R[B][R[C]], R[B] is vararg parameter */ -OP_ERRNNIL,/* A Bx raise error if R[A] ~= nil (K[Bx] is global name)*/ +OP_ERRNNIL,/* A Bx raise error if R[A] ~= nil (K[Bx - 1] is global name)*/ OP_VARARGPREP,/* (adjust vararg parameters) */ @@ -386,6 +386,9 @@ OP_EXTRAARG/* Ax extra (larger) argument for previous opcode */ power of 2) plus 1, or zero for size zero. If not k, the array size is vC. Otherwise, the array size is EXTRAARG _ vC. + (*) In OP_ERRNNIL, (Bx == 0) means index of global name doesn't + fit in Bx. (So, that name is not available for the instruction.) + (*) For comparisons, k specifies what condition the test should accept (true or false). diff --git a/manual/manual.of b/manual/manual.of index 9b6976ca05..96203d7fff 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -2402,7 +2402,7 @@ g(3, 4, 5, 8) a=3, b=4, ... -> 5 8 g(5, r()) a=5, b=1, ... -> 2 3 } -The presence of a varag table in a variadic function is indicated +The presence of a vararg table in a variadic function is indicated by a name after the three dots. When present, a vararg table behaves like a read-only local variable @@ -2418,8 +2418,9 @@ local name = table.pack(...) } As an optimization, -if the vararg table is used only as a base in indexing expressions -(the @T{t} in @T{t[exp]} or @T{t.id}) and it is not an upvalue, +if the vararg table is used only as the base table +in the syntactic constructions @T{t[exp]} or @T{t.id}) +and it is not an upvalue, the code does not create an actual table and instead translates the indexing expressions into accesses to the internal vararg data. @@ -2427,8 +2428,7 @@ the indexing expressions into accesses to the internal vararg data. } -@sect3{multires| @title{Lists of expressions, multiple results, -and adjustment} +@sect3{multires| @title{Lists of Expressions, Multiple Results, and Adjustment} Both function calls and vararg expressions can result in multiple values. These expressions are called @def{multires expressions}. @@ -2686,7 +2686,7 @@ which behaves like a nil value. } -@sect3{constchar|@title{Pointers to strings} +@sect3{constchar|@title{Pointers to Strings} Several functions in the API return pointers (@T{const char*}) to Lua strings in the stack. @@ -4126,6 +4126,8 @@ Lua still has to allocate a header for the string. In case of a memory-allocation error, Lua will call @id{falloc} before raising the error. +The function returns a pointer to the string (that is, @id{s}). + } @@ -8413,7 +8415,7 @@ a value greater than any other numeric value. } -@LibEntry{math.ldexp(m, e)| +@LibEntry{math.ldexp (m, e)| Returns @M{m2@sp{e}}, where @id{e} is an integer. diff --git a/testes/heavy.lua b/testes/heavy.lua index 3b4e4ce352..e7219a91ae 100644 --- a/testes/heavy.lua +++ b/testes/heavy.lua @@ -1,6 +1,8 @@ -- $Id: testes/heavy.lua,v $ -- See Copyright Notice in file lua.h +global * + local function teststring () print("creating a string too long") do @@ -47,9 +49,9 @@ local function loadrep (x, what) end -function controlstruct () +local function controlstruct () print("control structure too long") - local lim = ((1 << 24) - 2) // 3 + local lim = ((1 << 24) - 2) // 4 local s = string.rep("a = a + 1\n", lim) s = "while true do " .. s .. "end" assert(load(s)) @@ -63,7 +65,7 @@ function controlstruct () end -function manylines () +local function manylines () print("loading chunk with too many lines") local st, msg = loadrep("\n", "lines") assert(not st and string.find(msg, "too many lines")) @@ -71,7 +73,7 @@ function manylines () end -function hugeid () +local function hugeid () print("loading chunk with huge identifier") local st, msg = loadrep("a", "chars") assert(not st and @@ -80,7 +82,7 @@ function hugeid () print('+') end -function toomanyinst () +local function toomanyinst () print("loading chunk with too many instructions") local st, msg = loadrep("a = 10; ", "instructions") print('+') @@ -107,7 +109,7 @@ local function loadrepfunc (prefix, f) end -function toomanyconst () +local function toomanyconst () print("loading function with too many constants") loadrepfunc("function foo () return {0,", function (n) @@ -126,7 +128,7 @@ function toomanyconst () end -function toomanystr () +local function toomanystr () local a = {} local st, msg = pcall(function () for i = 1, math.huge do @@ -144,7 +146,7 @@ function toomanystr () end -function toomanyidx () +local function toomanyidx () local a = {} local st, msg = pcall(function () for i = 1, math.huge do From f33cc4ddec886ea499d7d41dd60cac5ddc5687db Mon Sep 17 00:00:00 2001 From: Roberto I Date: Wed, 26 Nov 2025 11:18:29 -0300 Subject: [PATCH 1118/1145] New conceptual model for vararg Conceptually, all functions get their vararg arguments in a vararg table. The storing of vararg arguments in the stack is always treated as an optimization. --- lcode.c | 5 ++++ lobject.h | 5 ++-- lopcodes.h | 2 +- lparser.c | 24 +++++++--------- ltm.c | 64 ++++++++++++++++++++++++++++++++--------- ltm.h | 4 +-- lvm.c | 5 ++-- manual/manual.of | 68 +++++++++++++++++++++++++------------------- testes/coroutine.lua | 4 ++- testes/db.lua | 9 +++--- testes/vararg.lua | 33 +++++++++++++++++++++ 11 files changed, 154 insertions(+), 69 deletions(-) diff --git a/lcode.c b/lcode.c index 95ef900cfd..afed05d15d 100644 --- a/lcode.c +++ b/lcode.c @@ -1951,6 +1951,11 @@ void luaK_finish (FuncState *fs) { SET_OPCODE(*pc, OP_GETTABLE); /* must get vararg there */ break; } + case OP_VARARG: { + if (p->flag & PF_VATAB) /* function has a vararg table? */ + SETARG_k(*pc, 1); /* must get vararg there */ + break; + } case OP_JMP: { /* to optimize jumps to jumps */ int target = finaltarget(p->code, i); fixjump(fs, i, target); /* jump directly to final target */ diff --git a/lobject.h b/lobject.h index 841ab5b9c3..070f12a42f 100644 --- a/lobject.h +++ b/lobject.h @@ -584,9 +584,8 @@ typedef struct AbsLineInfo { ** Flags in Prototypes */ #define PF_ISVARARG 1 /* function is vararg */ -#define PF_VAVAR 2 /* function has vararg parameter */ -#define PF_VATAB 4 /* function has vararg table */ -#define PF_FIXED 8 /* prototype has parts in fixed memory */ +#define PF_VATAB 2 /* function has vararg table */ +#define PF_FIXED 4 /* prototype has parts in fixed memory */ /* diff --git a/lopcodes.h b/lopcodes.h index f5c95151ba..fac87da2ce 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -336,7 +336,7 @@ OP_SETLIST,/* A vB vC k R[A][vC+i] := R[A+i], 1 <= i <= vB */ OP_CLOSURE,/* A Bx R[A] := closure(KPROTO[Bx]) */ -OP_VARARG,/* A C R[A], R[A+1], ..., R[A+C-2] = vararg */ +OP_VARARG,/* A C R[A], ..., R[A+C-2] = vararg, R[B] is vararg param. */ OP_GETVARG, /* A B C R[A] := R[B][R[C]], R[B] is vararg parameter */ diff --git a/lparser.c b/lparser.c index 77141e79f9..a07044b8d9 100644 --- a/lparser.c +++ b/lparser.c @@ -1056,9 +1056,8 @@ static void constructor (LexState *ls, expdesc *t) { /* }====================================================================== */ -static void setvararg (FuncState *fs, int kind) { - lua_assert(kind & PF_ISVARARG); - fs->f->flag |= cast_byte(kind); +static void setvararg (FuncState *fs) { + fs->f->flag |= PF_ISVARARG; luaK_codeABC(fs, OP_VARARGPREP, 0, 0, 0); } @@ -1078,12 +1077,12 @@ static void parlist (LexState *ls) { break; } case TK_DOTS: { - varargk |= PF_ISVARARG; + varargk = 1; luaX_next(ls); /* skip '...' */ - if (ls->t.token == TK_NAME) { + if (ls->t.token == TK_NAME) new_varkind(ls, str_checkname(ls), RDKVAVAR); - varargk |= PF_VAVAR; - } + else + new_localvarliteral(ls, "(vararg table)"); break; } default: luaX_syntaxerror(ls, " or '...' expected"); @@ -1092,10 +1091,9 @@ static void parlist (LexState *ls) { } adjustlocalvars(ls, nparams); f->numparams = cast_byte(fs->nactvar); - if (varargk != 0) { - setvararg(fs, varargk); /* declared vararg */ - if (varargk & PF_VAVAR) - adjustlocalvars(ls, 1); /* vararg parameter */ + if (varargk) { + setvararg(fs); /* declared vararg */ + adjustlocalvars(ls, 1); /* vararg parameter */ } /* reserve registers for parameters (plus vararg parameter, if present) */ luaK_reserveregs(fs, fs->nactvar); @@ -1287,7 +1285,7 @@ static void simpleexp (LexState *ls, expdesc *v) { FuncState *fs = ls->fs; check_condition(ls, fs->f->flag & PF_ISVARARG, "cannot use '...' outside a vararg function"); - init_exp(v, VVARARG, luaK_codeABC(fs, OP_VARARG, 0, 0, 1)); + init_exp(v, VVARARG, luaK_codeABC(fs, OP_VARARG, 0, fs->f->numparams, 1)); break; } case '{' /*}*/: { /* constructor */ @@ -2153,7 +2151,7 @@ static void mainfunc (LexState *ls, FuncState *fs) { BlockCnt bl; Upvaldesc *env; open_func(ls, fs, &bl); - setvararg(fs, PF_ISVARARG); /* main function is always vararg */ + setvararg(fs); /* main function is always vararg */ env = allocupvalue(fs); /* ...set environment upvalue */ env->instack = 1; env->idx = 0; diff --git a/ltm.c b/ltm.c index 8d64235e81..39ac59d423 100644 --- a/ltm.c +++ b/ltm.c @@ -242,6 +242,7 @@ static void createvarargtab (lua_State *L, StkId f, int n) { luaH_set(L, t, &key, &value); /* t.n = n */ for (i = 0; i < n; i++) luaH_setint(L, t, i + 1, s2v(f + i)); + luaC_checkGC(L); } @@ -265,11 +266,11 @@ void luaT_adjustvarargs (lua_State *L, CallInfo *ci, const Proto *p) { setobjs2s(L, L->top.p++, ci->func.p + i); setnilvalue(s2v(ci->func.p + i)); /* erase original parameter (for GC) */ } - if (p->flag & PF_VAVAR) { /* is there a vararg parameter? */ - if (p->flag & PF_VATAB) /* does it need a vararg table? */ - createvarargtab(L, ci->func.p + nfixparams + 1, nextra); - else /* no table; set parameter to nil */ - setnilvalue(s2v(L->top.p)); + if (p->flag & PF_VATAB) /* does it need a vararg table? */ + createvarargtab(L, ci->func.p + nfixparams + 1, nextra); + else { /* no table; set parameter to nil */ + setnilvalue(s2v(L->top.p)); + L->top.p++; } ci->func.p += totalargs + 1; ci->top.p += totalargs + 1; @@ -299,16 +300,53 @@ void luaT_getvararg (CallInfo *ci, StkId ra, TValue *rc) { } -void luaT_getvarargs (lua_State *L, CallInfo *ci, StkId where, int wanted) { - int i; - int nextra = ci->u.l.nextraargs; +/* +** Get the number of extra arguments in a vararg function. If vararg +** table has been optimized away, that number is in the call info. +** Otherwise, get the field 'n' from the vararg table and check that it +** has a proper value (non-negative integer not larger than the stack +** limit). +*/ +static int getnumargs (lua_State *L, CallInfo *ci, Table *h) { + if (h == NULL) /* no vararg table? */ + return ci->u.l.nextraargs; + else { + TValue res; + if (luaH_getshortstr(h, luaS_new(L, "n"), &res) != LUA_VNUMINT || + l_castS2U(ivalue(&res)) > cast_uint(INT_MAX/2)) + luaG_runerror(L, "vararg table has no proper 'n'"); + return cast_int(ivalue(&res)); + } +} + + +/* +** Get 'wanted' vararg arguments and put them in 'where'. 'vatab' is +** the register of the vararg table or -1 if there is no vararg table. +*/ +void luaT_getvarargs (lua_State *L, CallInfo *ci, StkId where, int wanted, + int vatab) { + Table *h = (vatab < 0) ? NULL : hvalue(s2v(ci->func.p + vatab + 1)); + int nargs = getnumargs(L, ci, h); /* number of available vararg args. */ + int i, touse; /* 'touse' is minimum between 'wanted' and 'nargs' */ if (wanted < 0) { - wanted = nextra; /* get all extra arguments available */ - checkstackp(L, nextra, where); /* ensure stack space */ - L->top.p = where + nextra; /* next instruction will need top */ + touse = wanted = nargs; /* get all extra arguments available */ + checkstackp(L, nargs, where); /* ensure stack space */ + L->top.p = where + nargs; /* next instruction will need top */ + } + else + touse = (nargs > wanted) ? wanted : nargs; + if (h == NULL) { /* no vararg table? */ + for (i = 0; i < touse; i++) /* get vararg values from the stack */ + setobjs2s(L, where + i, ci->func.p - nargs + i); + } + else { /* get vararg values from vararg table */ + for (i = 0; i < touse; i++) { + lu_byte tag = luaH_getint(h, i + 1, s2v(where + i)); + if (tagisempty(tag)) + setnilvalue(s2v(where + i)); + } } - for (i = 0; i < wanted && i < nextra; i++) - setobjs2s(L, where + i, ci->func.p - nextra + i); for (; i < wanted; i++) /* complete required results with nil */ setnilvalue(s2v(where + i)); } diff --git a/ltm.h b/ltm.h index 86f457ebce..07fc8c1c98 100644 --- a/ltm.h +++ b/ltm.h @@ -98,8 +98,8 @@ LUAI_FUNC int luaT_callorderiTM (lua_State *L, const TValue *p1, int v2, LUAI_FUNC void luaT_adjustvarargs (lua_State *L, struct CallInfo *ci, const Proto *p); LUAI_FUNC void luaT_getvararg (CallInfo *ci, StkId ra, TValue *rc); -LUAI_FUNC void luaT_getvarargs (lua_State *L, struct CallInfo *ci, - StkId where, int wanted); +LUAI_FUNC void luaT_getvarargs (lua_State *L, struct CallInfo *ci, StkId where, + int wanted, int vatab); #endif diff --git a/lvm.c b/lvm.c index 2c868c2128..c70e2b8a8a 100644 --- a/lvm.c +++ b/lvm.c @@ -1935,8 +1935,9 @@ void luaV_execute (lua_State *L, CallInfo *ci) { } vmcase(OP_VARARG) { StkId ra = RA(i); - int n = GETARG_C(i) - 1; /* required results */ - Protect(luaT_getvarargs(L, ci, ra, n)); + int n = GETARG_C(i) - 1; /* required results (-1 means all) */ + int vatab = GETARG_k(i) ? GETARG_B(i) : -1; + Protect(luaT_getvarargs(L, ci, ra, n, vatab)); vmbreak; } vmcase(OP_GETVARG) { diff --git a/manual/manual.of b/manual/manual.of index 96203d7fff..9b8e144d76 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -2221,7 +2221,7 @@ The form } can be used to emulate methods. A call @T{v:name(@rep{args})} -is syntactic sugar for @T{v.name(v,@rep{args})}, +is syntactic sugar for @T{v.name(v, @rep{args})}, except that @id{v} is evaluated only once. Arguments have the following syntax: @@ -2372,12 +2372,10 @@ which is indicated by three dots (@Char{...}) at the end of its parameter list. A variadic function does not adjust its argument list; instead, it collects all extra arguments and supplies them -to the function through a @def{vararg expression} and, -if present, a @def{vararg table}. - -A vararg expression is also written as three dots, -and its value is a list of all actual extra arguments, -similar to a function with multiple results @see{multires}. +to the function through a @def{vararg table}. +In that table, +the values at indices 1, 2, etc. are the extra arguments, +and the value at index @St{n} is the number of extra arguments. As an example, consider the following definitions: @verbatim{ @@ -2386,7 +2384,7 @@ function g(a, b, ...) end function r() return 1,2,3 end } Then, we have the following mapping from arguments to parameters and -to the vararg expression: +to the vararg table: @verbatim{ CALL PARAMETERS @@ -2396,33 +2394,39 @@ f(3, 4, 5) a=3, b=4 f(r(), 10) a=1, b=10 f(r()) a=1, b=2 -g(3) a=3, b=nil, ... -> (nothing) -g(3, 4) a=3, b=4, ... -> (nothing) -g(3, 4, 5, 8) a=3, b=4, ... -> 5 8 -g(5, r()) a=5, b=1, ... -> 2 3 +g(3) a=3, b=nil, va. table -> {n = 0} +g(3, 4) a=3, b=4, va. table -> {n = 0} +g(3, 4, 5, 8) a=3, b=4, va. table -> {5, 8, n = 2} +g(5, r()) a=5, b=1, va. table -> {2, 3, n = 2} } -The presence of a vararg table in a variadic function is indicated -by a name after the three dots. +A vararg table in a variadic function can have an optional name, +given after the three dots. When present, -a vararg table behaves like a read-only local variable -with the given name that is initialized with a table. -In that table, -the values at indices 1, 2, etc. are the extra arguments, -and the value at index @St{n} is the number of extra arguments. -In other words, the code behaves as if the function started with -the following statement, -assuming the standard behavior of @Lid{table.pack}: -@verbatim{ -local name = table.pack(...) -} +that name denotes a read-only local variable that +refers to the vararg table. +If the vararg table does not have a name, +it can only be accessed through a vararg expression. + +A vararg expression is also written as three dots, +and its value is a list of the values in the vararg table, +from 1 to the integer value at index @St{n}. +(Therefore, if the code does not modify the vararg table, +this list corresponds to the extra arguments in the function call.) +This list behaves like the results from a +function with multiple results @see{multires}. As an optimization, -if the vararg table is used only as the base table -in the syntactic constructions @T{t[exp]} or @T{t.id}) -and it is not an upvalue, +if the vararg table satisfies some conditions, the code does not create an actual table and instead translates -the indexing expressions into accesses to the internal vararg data. +the indexing expressions and the vararg expressions +into accesses to the internal vararg data. +The conditions are as follows: +If the vararg table has a name, +that name is not an upvalue in a nested function +and it is used only as the base table +in the syntactic constructions @T{t[exp]} or @T{t.id}). +Note that an anonymous vararg table always satisfy these conditions. } @@ -3103,7 +3107,7 @@ void *luaL_alloc (void *ud, void *ptr, size_t osize, } Note that @N{ISO C} ensures that @T{free(NULL)} has no effect and that -@T{realloc(NULL,size)} is equivalent to @T{malloc(size)}. +@T{realloc(NULL, size)} is equivalent to @T{malloc(size)}. } @@ -9197,6 +9201,10 @@ Compile-time constants may not appear in this listing, if they were optimized away by the compiler. Negative indices refer to vararg arguments; @num{-1} is the first vararg argument. +These negative indices are only available when the vararg table +has been optimized away; +otherwise, the vararg arguments are available in the vararg table. + The function returns @fail if there is no variable with the given index, and raises an error when called with a level out of range. diff --git a/testes/coroutine.lua b/testes/coroutine.lua index 4881d96478..ba394e0c46 100644 --- a/testes/coroutine.lua +++ b/testes/coroutine.lua @@ -702,7 +702,9 @@ else assert(t.currentline == t.linedefined + 2) assert(not debug.getinfo(c, 1)) -- no other level assert(coroutine.resume(c)) -- run next line - local n,v = debug.getlocal(c, 0, 2) -- check next local + local n,v = debug.getlocal(c, 0, 2) -- check vararg table + assert(n == "(vararg table)" and v == nil) + local n,v = debug.getlocal(c, 0, 3) -- check next local assert(n == "b" and v == 10) v = {coroutine.resume(c)} -- finish coroutine assert(v[1] == true and v[2] == 2 and v[3] == 3 and v[4] == undef) diff --git a/testes/db.lua b/testes/db.lua index 0f174f17f7..4220b68ba7 100644 --- a/testes/db.lua +++ b/testes/db.lua @@ -356,8 +356,8 @@ function f(a,b) global assert, g, string local _, y = debug.getlocal(1, 2) assert(x == a and y == b) - assert(debug.setlocal(2, 3, "pera") == "AA".."AA") - assert(debug.setlocal(2, 4, "manga") == "B") + assert(debug.setlocal(2, 4, "pera") == "AA".."AA") + assert(debug.setlocal(2, 5, "manga") == "B") x = debug.getinfo(2) assert(x.func == g and x.what == "Lua" and x.name == 'g' and x.nups == 2 and string.find(x.source, "^@.*db%.lua$")) @@ -392,7 +392,7 @@ function g (...) global * local B = 13 global assert - local x,y = debug.getlocal(1,5) + local x,y = debug.getlocal(1,6) assert(x == 'B' and y == 13) end end @@ -458,7 +458,8 @@ local function collectlocals (level) local tab = {} for i = 1, math.huge do local n, v = debug.getlocal(level + 1, i) - if not (n and string.find(n, "^[a-zA-Z0-9_]+$")) then + if not (n and string.find(n, "^[a-zA-Z0-9_]+$") or + n == "(vararg table)") then break -- consider only real variables end tab[n] = v diff --git a/testes/vararg.lua b/testes/vararg.lua index a01598ff3b..043fa7d47a 100644 --- a/testes/vararg.lua +++ b/testes/vararg.lua @@ -101,6 +101,38 @@ a,b,c,d,e = f(4) assert(a==nil and b==nil and c==nil and d==nil and e==nil) +do -- vararg expressions using unpack + local function aux (a, v, ...t) + for k, val in pairs(v) do t[k] = val end + return ... + end + + local t = table.pack(aux(10, {11, [5] = 24}, 1, 2, 3, nil, 4)) + assert(t.n == 5 and t[1] == 11 and t[2] == 2 and t[3] == 3 + and t[4] == nil and t[5] == 24) + + local t = table.pack(aux(nil, {1, [20] = "a", [30] = "b", n = 30})) + assert(t.n == 30 and t[1] == 1 and t[20] == "a" and t[30] == "b") + -- table has only those four elements + assert(next(t, next(t, next(t, next(t, next(t, nil))))) == nil) + + local a, b, c, d = aux(nil, {}, 10, 20, 30) + assert(a == 10 and b == 20 and c == 30 and d == nil) + + local function aux (a, b, n, ...t) t.n = n; return b, ... end + local t = table.pack(aux(10, 1, 10000)) + assert(t.n == 10001 and t[1] == 1 and #t == 1) + + local function checkerr (emsg, f, ...) + local st, msg = pcall(f, ...) + assert(not st and string.find(msg, emsg)) + end + checkerr("no proper 'n'", aux, 1, 1, -1) + checkerr("no proper 'n'", aux, 1, 1, math.maxinteger) + checkerr("no proper 'n'", aux, 1, 1, math.mininteger) + checkerr("no proper 'n'", aux, 1, 1, 1.0) +end + -- varargs for main chunks local f = assert(load[[ return {...} ]]) local x = f(2,3) @@ -205,6 +237,7 @@ do -- access to vararg parameter assert(t[k] == v[k]) end assert(t.n == v.n) + return ... end local t = table.pack(10, 20, 30) From a07f6a824197d7dc01c321599d3bc71936a2590e Mon Sep 17 00:00:00 2001 From: Roberto I Date: Fri, 28 Nov 2025 15:12:51 -0300 Subject: [PATCH 1119/1145] Functions with vararg tables don't need hidden args. Vararg functions with vararg tables don't use the arguments hidden in the stack; therfore, it doesn't need to build/keep them. --- lcode.c | 12 +++++++----- ldebug.c | 8 ++++---- ldo.c | 2 +- lobject.h | 10 +++++++++- lopcodes.h | 31 +++++++++++++++++-------------- lparser.c | 6 +++--- ltm.c | 39 +++++++++++++++++++++++++-------------- manual/manual.of | 2 +- testes/db.lua | 3 +++ 9 files changed, 70 insertions(+), 43 deletions(-) diff --git a/lcode.c b/lcode.c index afed05d15d..b0a8142121 100644 --- a/lcode.c +++ b/lcode.c @@ -806,7 +806,7 @@ void luaK_setoneret (FuncState *fs, expdesc *e) { ** Change a vararg parameter into a regular local variable */ void luaK_vapar2local (FuncState *fs, expdesc *var) { - fs->f->flag |= PF_VATAB; /* function will need a vararg table */ + needvatab(fs->f); /* function will need a vararg table */ /* now a vararg parameter is equivalent to a regular local variable */ var->k = VLOCAL; } @@ -1127,7 +1127,7 @@ void luaK_storevar (FuncState *fs, expdesc *var, expdesc *ex) { break; } case VVARGIND: { - fs->f->flag |= PF_VATAB; /* function will need a vararg table */ + needvatab(fs->f); /* function will need a vararg table */ /* now, assignment is to a regular table */ } /* FALLTHROUGH */ case VINDEXED: { @@ -1927,6 +1927,8 @@ static int finaltarget (Instruction *code, int i) { void luaK_finish (FuncState *fs) { int i; Proto *p = fs->f; + if (p->flag & PF_VATAB) /* will it use a vararg table? */ + p->flag &= cast_byte(~PF_VAHID); /* then it will not use hidden args. */ for (i = 0; i < fs->pc; i++) { Instruction *pc = &p->code[i]; /* avoid "not used" warnings when assert is off (for 'onelua.c') */ @@ -1934,7 +1936,7 @@ void luaK_finish (FuncState *fs) { lua_assert(i == 0 || luaP_isOT(*(pc - 1)) == luaP_isIT(*pc)); switch (GET_OPCODE(*pc)) { case OP_RETURN0: case OP_RETURN1: { - if (!(fs->needclose || (p->flag & PF_ISVARARG))) + if (!(fs->needclose || (p->flag & PF_VAHID))) break; /* no extra work */ /* else use OP_RETURN to do the extra work */ SET_OPCODE(*pc, OP_RETURN); @@ -1942,8 +1944,8 @@ void luaK_finish (FuncState *fs) { case OP_RETURN: case OP_TAILCALL: { if (fs->needclose) SETARG_k(*pc, 1); /* signal that it needs to close */ - if (p->flag & PF_ISVARARG) - SETARG_C(*pc, p->numparams + 1); /* signal that it is vararg */ + if (p->flag & PF_VAHID) /* does it use hidden arguments? */ + SETARG_C(*pc, p->numparams + 1); /* signal that */ break; } case OP_GETVARG: { diff --git a/ldebug.c b/ldebug.c index abead91ce6..8df5f5f28b 100644 --- a/ldebug.c +++ b/ldebug.c @@ -184,7 +184,7 @@ static const char *upvalname (const Proto *p, int uv) { static const char *findvararg (CallInfo *ci, int n, StkId *pos) { - if (clLvalue(s2v(ci->func.p))->p->flag & PF_ISVARARG) { + if (clLvalue(s2v(ci->func.p))->p->flag & PF_VAHID) { int nextra = ci->u.l.nextraargs; if (n >= -nextra) { /* 'n' is negative */ *pos = ci->func.p - nextra - (n + 1); @@ -304,7 +304,7 @@ static void collectvalidlines (lua_State *L, Closure *f) { int i; TValue v; setbtvalue(&v); /* boolean 'true' to be the value of all indices */ - if (!(p->flag & PF_ISVARARG)) /* regular function? */ + if (!(isvararg(p))) /* regular function? */ i = 0; /* consider all instructions */ else { /* vararg function */ 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, ar->nparams = 0; } else { - ar->isvararg = (f->l.p->flag & PF_ISVARARG) ? 1 : 0; + ar->isvararg = (isvararg(f->l.p)) ? 1 : 0; ar->nparams = f->l.p->numparams; } break; @@ -912,7 +912,7 @@ int luaG_tracecall (lua_State *L) { Proto *p = ci_func(ci)->p; ci->u.l.trap = 1; /* ensure hooks will be checked */ if (ci->u.l.savedpc == p->code) { /* first instruction (not resuming)? */ - if (p->flag & PF_ISVARARG) + if (isvararg(p)) return 0; /* hooks will start at VARARGPREP instruction */ else if (!(ci->callstatus & CIST_HOOKYIELD)) /* not yielded? */ luaD_hookcall(L, ci); /* check 'call' hook */ diff --git a/ldo.c b/ldo.c index 44937068f8..75ce14889a 100644 --- a/ldo.c +++ b/ldo.c @@ -487,7 +487,7 @@ static void rethook (lua_State *L, CallInfo *ci, int nres) { int ftransfer; if (isLua(ci)) { Proto *p = ci_func(ci)->p; - if (p->flag & PF_ISVARARG) + if (p->flag & PF_VAHID) delta = ci->u.l.nextraargs + p->numparams + 1; } ci->func.p += delta; /* if vararg, back to virtual 'func' */ diff --git a/lobject.h b/lobject.h index 070f12a42f..156c942f01 100644 --- a/lobject.h +++ b/lobject.h @@ -583,10 +583,18 @@ typedef struct AbsLineInfo { /* ** Flags in Prototypes */ -#define PF_ISVARARG 1 /* function is vararg */ +#define PF_VAHID 1 /* function has hidden vararg arguments */ #define PF_VATAB 2 /* function has vararg table */ #define PF_FIXED 4 /* prototype has parts in fixed memory */ +/* a vararg function either has hidden args. or a vararg table */ +#define isvararg(p) ((p)->flag & (PF_VAHID | PF_VATAB)) + +/* +** mark that a function needs a vararg table. (The flag PF_VAHID will +** be cleared later.) +*/ +#define needvatab(p) ((p)->flag |= PF_VATAB) /* ** Function Prototypes diff --git a/lopcodes.h b/lopcodes.h index fac87da2ce..b6bd182ea2 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -224,8 +224,8 @@ enum OpMode {iABC, ivABC, iABx, iAsBx, iAx, isJ}; /* -** Grep "ORDER OP" if you change these enums. Opcodes marked with a (*) -** has extra descriptions in the notes after the enumeration. +** Grep "ORDER OP" if you change this enum. +** See "Notes" below for more information about some instructions. */ typedef enum { @@ -238,7 +238,7 @@ OP_LOADF,/* A sBx R[A] := (lua_Number)sBx */ OP_LOADK,/* A Bx R[A] := K[Bx] */ OP_LOADKX,/* A R[A] := K[extra arg] */ OP_LOADFALSE,/* A R[A] := false */ -OP_LFALSESKIP,/*A R[A] := false; pc++ (*) */ +OP_LFALSESKIP,/*A R[A] := false; pc++ */ OP_LOADTRUE,/* A R[A] := true */ OP_LOADNIL,/* A B R[A], R[A+1], ..., R[A+B] := nil */ OP_GETUPVAL,/* A B R[A] := UpValue[B] */ @@ -289,7 +289,7 @@ OP_BXOR,/* A B C R[A] := R[B] ~ R[C] */ OP_SHL,/* A B C R[A] := R[B] << R[C] */ OP_SHR,/* A B C R[A] := R[B] >> R[C] */ -OP_MMBIN,/* A B C call C metamethod over R[A] and R[B] (*) */ +OP_MMBIN,/* A B C call C metamethod over R[A] and R[B] */ OP_MMBINI,/* A sB C k call C metamethod over R[A] and sB */ OP_MMBINK,/* A B C k call C metamethod over R[A] and K[B] */ @@ -315,12 +315,12 @@ OP_GTI,/* A sB k if ((R[A] > sB) ~= k) then pc++ */ OP_GEI,/* A sB k if ((R[A] >= sB) ~= k) then pc++ */ OP_TEST,/* A k if (not R[A] == k) then pc++ */ -OP_TESTSET,/* A B k if (not R[B] == k) then pc++ else R[A] := R[B] (*) */ +OP_TESTSET,/* A B k if (not R[B] == k) then pc++ else R[A] := R[B] */ OP_CALL,/* A B C R[A], ... ,R[A+C-2] := R[A](R[A+1], ... ,R[A+B-1]) */ OP_TAILCALL,/* A B C k return R[A](R[A+1], ... ,R[A+B-1]) */ -OP_RETURN,/* A B C k return R[A], ... ,R[A+B-2] (see note) */ +OP_RETURN,/* A B C k return R[A], ... ,R[A+B-2] */ OP_RETURN0,/* return */ OP_RETURN1,/* A return R[A] */ @@ -336,13 +336,13 @@ OP_SETLIST,/* A vB vC k R[A][vC+i] := R[A+i], 1 <= i <= vB */ OP_CLOSURE,/* A Bx R[A] := closure(KPROTO[Bx]) */ -OP_VARARG,/* A C R[A], ..., R[A+C-2] = vararg, R[B] is vararg param. */ +OP_VARARG,/* A B C k R[A], ..., R[A+C-2] = varargs */ OP_GETVARG, /* A B C R[A] := R[B][R[C]], R[B] is vararg parameter */ OP_ERRNNIL,/* A Bx raise error if R[A] ~= nil (K[Bx - 1] is global name)*/ -OP_VARARGPREP,/* (adjust vararg parameters) */ +OP_VARARGPREP,/* (adjust varargs) */ OP_EXTRAARG/* Ax extra (larger) argument for previous opcode */ } OpCode; @@ -371,7 +371,8 @@ OP_EXTRAARG/* Ax extra (larger) argument for previous opcode */ OP_RETURN*, OP_SETLIST) may use 'top'. (*) In OP_VARARG, if (C == 0) then use actual number of varargs and - set top (like in OP_CALL with C == 0). + set top (like in OP_CALL with C == 0). 'k' means function has a + vararg table, which is in R[B]. (*) In OP_RETURN, if (B == 0) then return up to 'top'. @@ -387,20 +388,22 @@ OP_EXTRAARG/* Ax extra (larger) argument for previous opcode */ is vC. Otherwise, the array size is EXTRAARG _ vC. (*) In OP_ERRNNIL, (Bx == 0) means index of global name doesn't - fit in Bx. (So, that name is not available for the instruction.) + fit in Bx. (So, that name is not available for the error message.) (*) For comparisons, k specifies what condition the test should accept (true or false). (*) In OP_MMBINI/OP_MMBINK, k means the arguments were flipped - (the constant is the first operand). + (the constant is the first operand). - (*) All 'skips' (pc++) assume that next instruction is a jump. + (*) All comparison and test instructions assume that the instruction + being skipped (pc++) is a jump. (*) In instructions OP_RETURN/OP_TAILCALL, 'k' specifies that the function builds upvalues, which may need to be closed. C > 0 means - the function is vararg, so that its 'func' must be corrected before - returning; in this case, (C - 1) is its number of fixed parameters. + the function has hidden vararg arguments, so that its 'func' must be + corrected before returning; in this case, (C - 1) is its number of + fixed parameters. (*) In comparisons with an immediate operand, C signals whether the original operand was a float. (It must be corrected in case of diff --git a/lparser.c b/lparser.c index a07044b8d9..e015dfc578 100644 --- a/lparser.c +++ b/lparser.c @@ -304,7 +304,7 @@ static void check_readonly (LexState *ls, expdesc *e) { break; } case VVARGIND: { - fs->f->flag |= PF_VATAB; /* function will need a vararg table */ + needvatab(fs->f); /* function will need a vararg table */ e->k = VINDEXED; } /* FALLTHROUGH */ case VINDEXUP: case VINDEXSTR: case VINDEXED: { /* global variable */ @@ -1057,7 +1057,7 @@ static void constructor (LexState *ls, expdesc *t) { static void setvararg (FuncState *fs) { - fs->f->flag |= PF_ISVARARG; + fs->f->flag |= PF_VAHID; /* by default, use hidden vararg arguments */ luaK_codeABC(fs, OP_VARARGPREP, 0, 0, 0); } @@ -1283,7 +1283,7 @@ static void simpleexp (LexState *ls, expdesc *v) { } case TK_DOTS: { /* vararg */ FuncState *fs = ls->fs; - check_condition(ls, fs->f->flag & PF_ISVARARG, + check_condition(ls, isvararg(fs->f), "cannot use '...' outside a vararg function"); init_exp(v, VVARARG, luaK_codeABC(fs, OP_VARARG, 0, fs->f->numparams, 1)); break; diff --git a/ltm.c b/ltm.c index 39ac59d423..f2a373f86c 100644 --- a/ltm.c +++ b/ltm.c @@ -250,31 +250,42 @@ static void createvarargtab (lua_State *L, StkId f, int n) { ** initial stack: func arg1 ... argn extra1 ... ** ^ ci->func ^ L->top ** final stack: func nil ... nil extra1 ... func arg1 ... argn -** ^ ci->func ^ L->top +** ^ ci->func */ -void luaT_adjustvarargs (lua_State *L, CallInfo *ci, const Proto *p) { +static void buildhiddenargs (lua_State *L, CallInfo *ci, const Proto *p, + int totalargs, int nfixparams, int nextra) { int i; - int totalargs = cast_int(L->top.p - ci->func.p) - 1; - int nfixparams = p->numparams; - int nextra = totalargs - nfixparams; /* number of extra arguments */ ci->u.l.nextraargs = nextra; luaD_checkstack(L, p->maxstacksize + 1); - /* copy function to the top of the stack */ + /* copy function to the top of the stack, after extra arguments */ setobjs2s(L, L->top.p++, ci->func.p); - /* move fixed parameters to the top of the stack */ + /* move fixed parameters to after the copied function */ for (i = 1; i <= nfixparams; i++) { setobjs2s(L, L->top.p++, ci->func.p + i); setnilvalue(s2v(ci->func.p + i)); /* erase original parameter (for GC) */ } - if (p->flag & PF_VATAB) /* does it need a vararg table? */ + ci->func.p += totalargs + 1; /* 'func' now lives after hidden arguments */ + ci->top.p += totalargs + 1; +} + + +void luaT_adjustvarargs (lua_State *L, CallInfo *ci, const Proto *p) { + int totalargs = cast_int(L->top.p - ci->func.p) - 1; + int nfixparams = p->numparams; + int nextra = totalargs - nfixparams; /* number of extra arguments */ + if (p->flag & PF_VATAB) { /* does it need a vararg table? */ + lua_assert(!(p->flag & PF_VAHID)); createvarargtab(L, ci->func.p + nfixparams + 1, nextra); - else { /* no table; set parameter to nil */ - setnilvalue(s2v(L->top.p)); - L->top.p++; + /* move table to proper place (last parameter) */ + setobjs2s(L, ci->func.p + nfixparams + 1, L->top.p - 1); + } + else { /* no table */ + lua_assert(p->flag & PF_VAHID); + buildhiddenargs(L, ci, p, totalargs, nfixparams, nextra); + /* set vararg parameter to nil */ + setnilvalue(s2v(ci->func.p + nfixparams + 1)); + lua_assert(L->top.p <= ci->top.p && ci->top.p <= L->stack_last.p); } - ci->func.p += totalargs + 1; - ci->top.p += totalargs + 1; - lua_assert(L->top.p <= ci->top.p && ci->top.p <= L->stack_last.p); } diff --git a/manual/manual.of b/manual/manual.of index 9b8e144d76..54f67b3e2f 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -2425,7 +2425,7 @@ The conditions are as follows: If the vararg table has a name, that name is not an upvalue in a nested function and it is used only as the base table -in the syntactic constructions @T{t[exp]} or @T{t.id}). +in the syntactic constructions @T{t[exp]} or @T{t.id}. Note that an anonymous vararg table always satisfy these conditions. } diff --git a/testes/db.lua b/testes/db.lua index 4220b68ba7..e15a5be6bd 100644 --- a/testes/db.lua +++ b/testes/db.lua @@ -726,6 +726,9 @@ assert(t.isvararg == false and t.nparams == 3 and t.nups == 0) t = debug.getinfo(function (a,b,...) return t[a] end, "u") assert(t.isvararg == true and t.nparams == 2 and t.nups == 1) +t = debug.getinfo(function (a,b,...t) t.n = 2; return t[a] end, "u") +assert(t.isvararg == true and t.nparams == 2 and t.nups == 0) + t = debug.getinfo(1) -- main assert(t.isvararg == true and t.nparams == 0 and t.nups == 1 and debug.getupvalue(t.func, 1) == "_ENV") From 985ef32248f17ae4ca2d4e83e5e39e15393bb2f6 Mon Sep 17 00:00:00 2001 From: Roberto I Date: Mon, 1 Dec 2025 10:25:44 -0300 Subject: [PATCH 1120/1145] In luaB_close, running coroutines do not go to default This should had been corrected in commit fd897027f1. --- lcorolib.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lcorolib.c b/lcorolib.c index 23dd844156..eb30bf4da5 100644 --- a/lcorolib.c +++ b/lcorolib.c @@ -189,15 +189,17 @@ static int luaB_close (lua_State *L) { return 2; } } - case COS_RUN: /* running coroutine? */ + case COS_NORM: + return luaL_error(L, "cannot close a %s coroutine", statname[status]); + case COS_RUN: lua_geti(L, LUA_REGISTRYINDEX, LUA_RIDX_MAINTHREAD); /* get main */ if (lua_tothread(L, -1) == co) return luaL_error(L, "cannot close main thread"); lua_closethread(co, L); /* close itself */ - lua_assert(0); /* previous call does not return */ + /* previous call does not return *//* FALLTHROUGH */ + default: + lua_assert(0); return 0; - default: /* normal or running coroutine */ - return luaL_error(L, "cannot close a %s coroutine", statname[status]); } } From 8164d09338d06ecd89bd654e4ff5379f040eba71 Mon Sep 17 00:00:00 2001 From: Roberto I Date: Mon, 8 Dec 2025 11:08:12 -0300 Subject: [PATCH 1121/1145] Wrong assert in 'luaK_indexed' --- lcode.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lcode.c b/lcode.c index b0a8142121..4caa8046f6 100644 --- a/lcode.c +++ b/lcode.c @@ -1370,9 +1370,11 @@ void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) { fillidxk(t, k->u.info, VINDEXUP); /* literal short string */ } else if (t->k == VVARGVAR) { /* indexing the vararg parameter? */ - lua_assert(t->u.ind.t == fs->f->numparams); - t->u.ind.t = cast_byte(t->u.var.ridx); - fillidxk(t, luaK_exp2anyreg(fs, k), VVARGIND); /* register */ + int kreg = luaK_exp2anyreg(fs, k); /* put key in some register */ + lu_byte vreg = cast_byte(t->u.var.ridx); /* register with vararg param. */ + lua_assert(vreg == fs->f->numparams); + t->u.ind.t = vreg; /* (avoid a direct assignment; values may overlap) */ + fillidxk(t, kreg, VVARGIND); /* 't' represents 'vararg[k]' */ } else { /* register index of the table */ From 104b0fc7008b1f6b7d818985fbbad05cd37ee654 Mon Sep 17 00:00:00 2001 From: Roberto I Date: Mon, 8 Dec 2025 13:09:47 -0300 Subject: [PATCH 1122/1145] Details - Avoid fixing name "_ENV" in the code - Small improvements in the manual --- lparser.c | 4 ++-- manual/manual.of | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/lparser.c b/lparser.c index e015dfc578..b3855d4cb6 100644 --- a/lparser.c +++ b/lparser.c @@ -505,8 +505,8 @@ static void buildglobal (LexState *ls, TString *varname, expdesc *var) { init_exp(var, VGLOBAL, -1); /* global by default */ singlevaraux(fs, ls->envn, var, 1); /* get environment variable */ if (var->k == VGLOBAL) - luaK_semerror(ls, "_ENV is global when accessing variable '%s'", - getstr(varname)); + luaK_semerror(ls, "%s is global when accessing variable '%s'", + LUA_ENV, getstr(varname)); luaK_exp2anyregup(fs, var); /* _ENV could be a constant */ codestring(&key, varname); /* key is variable name */ luaK_indexed(fs, var, &key); /* 'var' represents _ENV[varname] */ diff --git a/manual/manual.of b/manual/manual.of index 54f67b3e2f..317adcaa4d 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -107,7 +107,7 @@ for small machines and embedded systems. Unless stated otherwise, any overflow when manipulating integer values @def{wrap around}, -according to the usual rules of two-complement arithmetic. +according to the usual rules of two's complement arithmetic. (In other words, the actual result is the unique representable integer that is equal modulo @M{2@sp{n}} to the mathematical result, @@ -2458,7 +2458,7 @@ for instance @T{{e1, e2, e3}} @see{tableconstructor}.} for instance @T{foo(e1, e2, e3)} @see{functioncall}.} @item{A multiple assignment, -for instance @T{a , b, c = e1, e2, e3} @see{assignment}.} +for instance @T{a, b, c = e1, e2, e3} @see{assignment}.} @item{A local or global declaration, which is similar to a multiple assignment.} @@ -3640,9 +3640,9 @@ because a pseudo-index is not an actual stack position. The type of integers in Lua. By default this type is @id{long long}, -(usually a 64-bit two-complement integer), +(usually a 64-bit two's complement integer), but that can be changed to @id{long} or @id{int} -(usually a 32-bit two-complement integer). +(usually a 32-bit two's complement integer). (See @id{LUA_INT_TYPE} in @id{luaconf.h}.) Lua also defines the constants @@ -5439,7 +5439,7 @@ the auxiliary library provides higher-level functions for some common tasks. All functions and types from the auxiliary library -are defined in header file @id{lauxlib.h} and +are defined in the header file @id{lauxlib.h} and have a prefix @id{luaL_}. All functions in the auxiliary library are built on @@ -6492,7 +6492,7 @@ the host program can call the function @Lid{luaL_openlibs}. Alternatively, the host can select which libraries to open, by using @Lid{luaL_openselectedlibs}. -Both functions are defined in the header file @id{lualib.h}. +Both functions are declared in the header file @id{lualib.h}. @index{lualib.h} The stand-alone interpreter @id{lua} @see{lua-sa} From 82d721a8554df9b14ff520b4dd55ce5303ab560e Mon Sep 17 00:00:00 2001 From: Roberto I Date: Wed, 10 Dec 2025 10:35:05 -0300 Subject: [PATCH 1123/1145] Format adjust in the manual Lists in inline code don't get a space after commas. (That keeps the code more compact and avoids line breaks in the middle of the code.) --- manual/manual.of | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/manual/manual.of b/manual/manual.of index 317adcaa4d..5fa4e097e1 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -2091,12 +2091,12 @@ Note that keys that are not positive integers do not interfere with borders. A table with exactly one border is called a @def{sequence}. -For instance, the table @T{{10, 20, 30, 40, 50}} is a sequence, +For instance, the table @T{{10,20,30,40,50}} is a sequence, as it has only one border (5). -The table @T{{10, 20, 30, nil, 50}} has two borders (3 and 5), +The table @T{{10,20,30,nil,50}} has two borders (3 and 5), and therefore it is not a sequence. (The @nil at index 4 is called a @emphx{hole}.) -The table @T{{nil, 20, 30, nil, nil, 60, nil}} +The table @T{{nil,20,30,nil,nil,60,nil}} has three borders (0, 3, and 6), so it is not a sequence, too. The table @T{{}} is a sequence with border 0. @@ -2449,22 +2449,22 @@ These are the places where Lua expects a list of expressions: @description{ @item{A @rw{return} statement, -for instance @T{return e1, e2, e3} @see{control}.} +for instance @T{return e1,e2,e3} @see{control}.} @item{A table constructor, -for instance @T{{e1, e2, e3}} @see{tableconstructor}.} +for instance @T{{e1,e2,e3}} @see{tableconstructor}.} @item{The arguments of a function call, -for instance @T{foo(e1, e2, e3)} @see{functioncall}.} +for instance @T{foo(e1,e2,e3)} @see{functioncall}.} @item{A multiple assignment, -for instance @T{a, b, c = e1, e2, e3} @see{assignment}.} +for instance @T{a,b,c = e1,e2,e3} @see{assignment}.} @item{A local or global declaration, which is similar to a multiple assignment.} @item{The initial values in a generic @rw{for} loop, -for instance @T{for k in e1, e2, e3 do ... end} @see{for}.} +for instance @T{for k in e1,e2,e3 do ... end} @see{for}.} } In the last four cases, @@ -2501,7 +2501,7 @@ we recommend assigning the vararg expression to a single variable and using that variable in its place. -Here are some examples of uses of mutlres expressions. +Here are some examples of uses of multires expressions. In all cases, when the construction needs @Q{the n-th result} and there is no such result, it uses a @nil. @@ -3107,7 +3107,7 @@ void *luaL_alloc (void *ud, void *ptr, size_t osize, } Note that @N{ISO C} ensures that @T{free(NULL)} has no effect and that -@T{realloc(NULL, size)} is equivalent to @T{malloc(size)}. +@T{realloc(NULL,size)} is equivalent to @T{malloc(size)}. } @@ -3879,7 +3879,7 @@ is a seed for the hashing of strings. @apii{0,1,m} Creates a new empty table and pushes it onto the stack. -It is equivalent to @T{lua_createtable(L, 0, 0)}. +It is equivalent to @T{lua_createtable(L,0,0)}. } @@ -5583,7 +5583,7 @@ Its pattern of use is as follows: @item{First declare a variable @id{b} of type @Lid{luaL_Buffer}.} -@item{Then initialize it with a call @T{luaL_buffinit(L, &b)}.} +@item{Then initialize it with a call @T{luaL_buffinit(L,&b)}.} @item{ Then add string pieces to the buffer calling any of @@ -5604,12 +5604,12 @@ you can use the buffer like this: @item{First declare a variable @id{b} of type @Lid{luaL_Buffer}.} @item{Then initialize it and preallocate a space of -size @id{sz} with a call @T{luaL_buffinitsize(L, &b, sz)}.} +size @id{sz} with a call @T{luaL_buffinitsize(L,&b,sz)}.} @item{Then produce the string into that space.} @item{ -Finish by calling @T{luaL_pushresultsize(&b, sz)}, +Finish by calling @T{luaL_pushresultsize(&b,sz)}, where @id{sz} is the total size of the resulting string copied into that space (which may be less than or equal to the preallocated size). @@ -6214,7 +6214,7 @@ You should not manually set integer keys in the table after the first use of @Lid{luaL_ref}. You can retrieve an object referred by the reference @id{r} -by calling @T{lua_rawgeti(L, t, r)} or @T{lua_geti(L, t, r)}. +by calling @T{lua_rawgeti(L,t,r)} or @T{lua_geti(L,t,r)}. The function @Lid{luaL_unref} frees a reference. If the object on the top of the stack is @nil, @@ -7744,7 +7744,7 @@ If @id{j} is absent, then it is assumed to be equal to @num{-1} In particular, the call @T{string.sub(s,1,j)} returns a prefix of @id{s} with length @id{j}, -and @T{string.sub(s, -i)} (for a positive @id{i}) +and @T{string.sub(s,-i)} (for a positive @id{i}) returns a suffix of @id{s} with length @id{i}. @@ -8180,7 +8180,7 @@ the function returns @fail. A negative @id{n} gets characters before position @id{i}. The default for @id{i} is 1 when @id{n} is non-negative and @T{#s + 1} otherwise, -so that @T{utf8.offset(s, -n)} gets the offset of the +so that @T{utf8.offset(s,-n)} gets the offset of the @id{n}-th character from the end of the string. As a special case, @@ -8233,7 +8233,7 @@ the table will have; its default is zero. Inserts element @id{value} at position @id{pos} in @id{list}, shifting up the elements -@T{list[pos], list[pos+1], @Cdots, list[#list]}. +@T{list[pos],list[pos+1],@Cdots,list[#list]}. The default value for @id{pos} is @T{#list+1}, so that a call @T{table.insert(t,x)} inserts @id{x} at the end of the list @id{t}. @@ -8271,7 +8271,7 @@ Removes from @id{list} the element at position @id{pos}, returning the value of the removed element. When @id{pos} is an integer between 1 and @T{#list}, it shifts down the elements -@T{list[pos+1], list[pos+2], @Cdots, list[#list]} +@T{list[pos+1],list[pos+2],@Cdots,list[#list]} and erases element @T{list[#list]}; The index @id{pos} can also be 0 when @T{#list} is 0, or @T{#list + 1}. From 3d03ae5bd6314f27c8635e06ec363150c2c19062 Mon Sep 17 00:00:00 2001 From: Roberto I Date: Sat, 13 Dec 2025 11:00:30 -0300 Subject: [PATCH 1124/1145] 'luaL_newstate' starts state with warnings on It is easier to forget to turn them on then to turn them off. --- lauxlib.c | 2 +- lua.c | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/lauxlib.c b/lauxlib.c index 1bb41bb1da..7cf90cb78a 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -1185,7 +1185,7 @@ LUALIB_API lua_State *(luaL_newstate) (void) { lua_State *L = lua_newstate(luaL_alloc, NULL, luaL_makeseed(NULL)); if (l_likely(L)) { lua_atpanic(L, &panic); - lua_setwarnf(L, warnfoff, L); /* default is warnings off */ + lua_setwarnf(L, warnfon, L); } return L; } diff --git a/lua.c b/lua.c index b2967a447d..5054583de9 100644 --- a/lua.c +++ b/lua.c @@ -349,6 +349,7 @@ static int collectargs (char **argv, int *first) { */ static int runargs (lua_State *L, char **argv, int n) { int i; + lua_warning(L, "@off", 0); /* by default, Lua stand-alone has warnings off */ for (i = 1; i < n; i++) { int option = argv[i][1]; lua_assert(argv[i][0] == '-'); /* already checked */ @@ -725,7 +726,7 @@ static int pmain (lua_State *L) { if (handle_luainit(L) != LUA_OK) /* run LUA_INIT */ return 0; /* error running LUA_INIT */ } - if (!runargs(L, argv, optlim)) /* execute arguments -e and -l */ + if (!runargs(L, argv, optlim)) /* execute arguments -e, -l, and -W */ return 0; /* something failed */ if (script > 0) { /* execute main script (if there is one) */ if (handle_script(L, argv + script) != LUA_OK) From a5522f06d2679b8f18534fd6a9968f7eb539dc31 Mon Sep 17 00:00:00 2001 From: Roberto I Date: Sat, 13 Dec 2025 16:16:59 -0300 Subject: [PATCH 1125/1145] GC checks stack space before running finalizer If the stack does not have some minimum available space, the GC defers calling a finalizer until the next cycle. That avoids errors while running a finalizer that the programmer cannot control. --- ldo.c | 11 +++++++++++ ldo.h | 1 + lgc.c | 7 ++++--- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/ldo.c b/ldo.c index 75ce14889a..6d0184ecd5 100644 --- a/ldo.c +++ b/ldo.c @@ -220,6 +220,17 @@ l_noret luaD_errerr (lua_State *L) { } +/* +** Check whether stack has enough space to run a simple function (such +** as a finalizer): At least BASIC_STACK_SIZE in the Lua stack and +** 2 slots in the C stack. +*/ +int luaD_checkminstack (lua_State *L) { + return ((stacksize(L) < MAXSTACK - BASIC_STACK_SIZE) && + (getCcalls(L) < LUAI_MAXCCALLS - 2)); +} + + /* ** In ISO C, any pointer use after the pointer has been deallocated is ** undefined behavior. So, before a stack reallocation, all pointers diff --git a/ldo.h b/ldo.h index 2d4ca8be46..b64729541c 100644 --- a/ldo.h +++ b/ldo.h @@ -89,6 +89,7 @@ LUAI_FUNC int luaD_reallocstack (lua_State *L, int newsize, int raiseerror); LUAI_FUNC int luaD_growstack (lua_State *L, int n, int raiseerror); LUAI_FUNC void luaD_shrinkstack (lua_State *L); LUAI_FUNC void luaD_inctop (lua_State *L); +LUAI_FUNC int luaD_checkminstack (lua_State *L); LUAI_FUNC l_noret luaD_throw (lua_State *L, TStatus errcode); LUAI_FUNC l_noret luaD_throwbaselevel (lua_State *L, TStatus errcode); diff --git a/lgc.c b/lgc.c index 60f042c7a8..c64d74b8e8 100644 --- a/lgc.c +++ b/lgc.c @@ -1293,7 +1293,7 @@ static void finishgencycle (lua_State *L, global_State *g) { correctgraylists(g); checkSizes(L, g); g->gcstate = GCSpropagate; /* skip restart */ - if (!g->gcemergency) + if (!g->gcemergency && luaD_checkminstack(L)) callallpendingfinalizers(L); } @@ -1667,12 +1667,13 @@ static l_mem singlestep (lua_State *L, int fast) { break; } case GCScallfin: { /* call finalizers */ - if (g->tobefnz && !g->gcemergency) { + if (g->tobefnz && !g->gcemergency && luaD_checkminstack(L)) { g->gcstopem = 0; /* ok collections during finalizers */ GCTM(L); /* call one finalizer */ stepresult = CWUFIN; } - else { /* emergency mode or no more finalizers */ + else { /* no more finalizers or emergency mode or no enough stack + to run finalizers */ g->gcstate = GCSpause; /* finish collection */ stepresult = step2pause; } From 578ae5745cecee56d48795cd4ae1eaf13618715c Mon Sep 17 00:00:00 2001 From: Roberto I Date: Tue, 23 Dec 2025 14:44:06 -0300 Subject: [PATCH 1126/1145] Details typo in comment + formatting + logical 'and' was written as a bitwise operation (makes code more fragile) --- lapi.c | 2 +- lgc.c | 2 +- lstrlib.c | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lapi.c b/lapi.c index 27fa524797..42c8fcdd70 100644 --- a/lapi.c +++ b/lapi.c @@ -366,7 +366,7 @@ LUA_API int lua_compare (lua_State *L, int index1, int index2, int op) { } -LUA_API unsigned (lua_numbertocstring) (lua_State *L, int idx, char *buff) { +LUA_API unsigned lua_numbertocstring (lua_State *L, int idx, char *buff) { const TValue *o = index2value(L, idx); if (ttisnumber(o)) { unsigned len = luaO_tostringbuff(o, buff); diff --git a/lgc.c b/lgc.c index c64d74b8e8..f1d9a7ce8a 100644 --- a/lgc.c +++ b/lgc.c @@ -1672,7 +1672,7 @@ static l_mem singlestep (lua_State *L, int fast) { GCTM(L); /* call one finalizer */ stepresult = CWUFIN; } - else { /* no more finalizers or emergency mode or no enough stack + else { /* no more finalizers or emergency mode or not enough stack to run finalizers */ g->gcstate = GCSpause; /* finish collection */ stepresult = step2pause; diff --git a/lstrlib.c b/lstrlib.c index 23df839ea0..e26eb1a8de 100644 --- a/lstrlib.c +++ b/lstrlib.c @@ -968,7 +968,7 @@ static int str_gsub (lua_State *L) { reprepstate(&ms); /* (re)prepare state for new match */ if ((e = match(&ms, src, p)) != NULL && e != lastmatch) { /* match? */ n++; - changed = add_value(&ms, &b, src, e, tr) | changed; + changed = add_value(&ms, &b, src, e, tr) || changed; src = lastmatch = e; } else if (src < ms.src_end) /* otherwise, skip one character */ From 632a71b24d8661228a726deb5e1698e9638f96d8 Mon Sep 17 00:00:00 2001 From: Roberto I Date: Sat, 27 Dec 2025 16:22:13 -0300 Subject: [PATCH 1127/1145] BUG: Arithmetic overflow in 'collectgarbage"step"' The computation of a new debt could overflow when we give a too large step to 'collectgarbage"step"' and the current debt was already negative. This is only an issue if your platform cares for it or if you compile Lua with an option like '-fsanitize=undefined'. --- lapi.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/lapi.c b/lapi.c index 42c8fcdd70..9b6ca1ecfb 100644 --- a/lapi.c +++ b/lapi.c @@ -1201,11 +1201,16 @@ LUA_API int lua_gc (lua_State *L, int what, ...) { case LUA_GCSTEP: { lu_byte oldstp = g->gcstp; l_mem n = cast(l_mem, va_arg(argp, size_t)); + l_mem newdebt; int work = 0; /* true if GC did some work */ g->gcstp = 0; /* allow GC to run (other bits must be zero here) */ if (n <= 0) - n = g->GCdebt; /* force to run one basic step */ - luaE_setdebt(g, g->GCdebt - n); + newdebt = 0; /* force to run one basic step */ + else if (g->GCdebt >= n - MAX_LMEM) /* no overflow? */ + newdebt = g->GCdebt - n; + else /* overflow */ + newdebt = -MAX_LMEM; /* set debt to miminum value */ + luaE_setdebt(g, newdebt); luaC_condGC(L, (void)0, work = 1); if (work && g->gcstate == GCSpause) /* end of cycle? */ res = 1; /* signal it */ From c4e2c91973fed04e7da940c00c92f10f9eb0f9ec Mon Sep 17 00:00:00 2001 From: Roberto I Date: Tue, 30 Dec 2025 10:50:49 -0300 Subject: [PATCH 1128/1145] Details Some comments still talked about bit 'isrealasize', which has been removed. --- ltable.c | 7 +++---- ltm.h | 2 +- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/ltable.c b/ltable.c index b7f88f6ffe..2f61be84b2 100644 --- a/ltable.c +++ b/ltable.c @@ -651,10 +651,9 @@ static void reinserthash (lua_State *L, Table *ot, Table *t) { /* -** Exchange the hash part of 't1' and 't2'. (In 'flags', only the -** dummy bit must be exchanged: The 'isrealasize' is not related -** to the hash part, and the metamethod bits do not change during -** a resize, so the "real" table can keep their values.) +** Exchange the hash part of 't1' and 't2'. (In 'flags', only the dummy +** bit must be exchanged: The metamethod bits do not change during a +** resize, so the "real" table can keep their values.) */ static void exchangehashpart (Table *t1, Table *t2) { lu_byte lsizenode = t1->lsizenode; diff --git a/ltm.h b/ltm.h index 07fc8c1c98..afc7ad00e2 100644 --- a/ltm.h +++ b/ltm.h @@ -49,7 +49,7 @@ typedef enum { ** Mask with 1 in all fast-access methods. A 1 in any of these bits ** in the flag of a (meta)table means the metatable does not have the ** corresponding metamethod field. (Bit 6 of the flag indicates that -** the table is using the dummy node; bit 7 is used for 'isrealasize'.) +** the table is using the dummy node.) */ #define maskflags cast_byte(~(~0u << (TM_EQ + 1))) From 962f444a755882ecfc24ca7e96ffe193d64ed12d Mon Sep 17 00:00:00 2001 From: Roberto I Date: Sun, 4 Jan 2026 16:27:54 -0300 Subject: [PATCH 1129/1145] Details In an assignment like 'a = &b', is looks suspicious if 'a' has a scope larger than 'b'. --- ltable.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/ltable.c b/ltable.c index 2f61be84b2..2f2b5c1f5c 100644 --- a/ltable.c +++ b/ltable.c @@ -1155,14 +1155,15 @@ void luaH_finishset (lua_State *L, Table *t, const TValue *key, lua_assert(hres != HOK); if (hres == HNOTFOUND) { TValue aux; + const TValue *actk = key; /* actual key to insert */ if (l_unlikely(ttisnil(key))) luaG_runerror(L, "table index is nil"); else if (ttisfloat(key)) { lua_Number f = fltvalue(key); lua_Integer k; - if (luaV_flttointeger(f, &k, F2Ieq)) { - setivalue(&aux, k); /* key is equal to an integer */ - key = &aux; /* insert it as an integer */ + if (luaV_flttointeger(f, &k, F2Ieq)) { /* is key equal to an integer? */ + setivalue(&aux, k); + actk = &aux; /* use the integer as the key */ } else if (l_unlikely(luai_numisnan(f))) luaG_runerror(L, "table index is NaN"); @@ -1175,7 +1176,7 @@ void luaH_finishset (lua_State *L, Table *t, const TValue *key, L->top.p--; return; } - luaH_newkey(L, t, key, value); + luaH_newkey(L, t, actk, value); } else if (hres > 0) { /* regular Node? */ setobj2t(L, gval(gnode(t, hres - HFIRSTNODE)), value); From 45c7ae5b1b05069543fe1710454c651350bc1c42 Mon Sep 17 00:00:00 2001 From: Roberto I Date: Sun, 4 Jan 2026 16:31:17 -0300 Subject: [PATCH 1130/1145] BUG: Possible overflow in 'string.packsize' 'string.packsize' can overflow result in 32-bit machines using 64-bit integers, as LUA_MAXINTEGER may not fit into size_t. --- lstrlib.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lstrlib.c b/lstrlib.c index e26eb1a8de..06ea10d9f6 100644 --- a/lstrlib.c +++ b/lstrlib.c @@ -1726,7 +1726,7 @@ static int str_packsize (lua_State *L) { luaL_argcheck(L, opt != Kstring && opt != Kzstr, 1, "variable-length format"); size += ntoalign; /* total space used by option */ - luaL_argcheck(L, totalsize <= LUA_MAXINTEGER - size, + luaL_argcheck(L, totalsize <= MAX_SIZE - size, 1, "format result too large"); totalsize += size; } From 5cfc725a8b61a6f96c7324f60ac26739315095ba Mon Sep 17 00:00:00 2001 From: Roberto I Date: Sun, 4 Jan 2026 16:39:22 -0300 Subject: [PATCH 1131/1145] Special case for 'string.rep' over an empty string --- lauxlib.h | 6 +++--- lstrlib.c | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lauxlib.h b/lauxlib.h index 7f1d3ca195..2d015362ff 100644 --- a/lauxlib.h +++ b/lauxlib.h @@ -81,8 +81,8 @@ LUALIB_API int (luaL_checkoption) (lua_State *L, int arg, const char *def, LUALIB_API int (luaL_fileresult) (lua_State *L, int stat, const char *fname); LUALIB_API int (luaL_execresult) (lua_State *L, int stat); -LUALIB_API void *luaL_alloc (void *ud, void *ptr, size_t osize, - size_t nsize); +LUALIB_API void *(luaL_alloc) (void *ud, void *ptr, size_t osize, + size_t nsize); /* predefined references */ @@ -103,7 +103,7 @@ LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s); LUALIB_API lua_State *(luaL_newstate) (void); -LUALIB_API unsigned luaL_makeseed (lua_State *L); +LUALIB_API unsigned (luaL_makeseed) (lua_State *L); LUALIB_API lua_Integer (luaL_len) (lua_State *L, int idx); diff --git a/lstrlib.c b/lstrlib.c index 06ea10d9f6..874cec8086 100644 --- a/lstrlib.c +++ b/lstrlib.c @@ -141,8 +141,8 @@ static int str_rep (lua_State *L) { const char *s = luaL_checklstring(L, 1, &len); lua_Integer n = luaL_checkinteger(L, 2); const char *sep = luaL_optlstring(L, 3, "", &lsep); - if (n <= 0) - lua_pushliteral(L, ""); + if (n <= 0 || (len | lsep) == 0) + lua_pushliteral(L, ""); /* no repetitions or both strings empty */ else if (l_unlikely(len > MAX_SIZE - lsep || cast_st2S(len + lsep) > cast_st2S(MAX_SIZE) / n)) return luaL_error(L, "resulting string too large"); From 2a7cf4f319fc276f4554a8f6364e6b1ba4eb2ded Mon Sep 17 00:00:00 2001 From: Roberto I Date: Sun, 11 Jan 2026 15:36:03 -0300 Subject: [PATCH 1132/1145] More effort in avoiding errors in finalizers Before calling a finalizer, Lua not only checks stack limits, but actually ensures that a minimum number of slots are already allocated for the call. (If it cannot ensure that, it postpones the finalizer.) That avoids finalizers not running due to memory errors that the programmer cannot control. --- ldo.c | 20 ++++++++++++++------ lgc.c | 2 +- lstate.c | 17 +++++++++++------ lstate.h | 2 +- ltests.c | 23 +++++++++++++++++++++++ testes/gc.lua | 42 ++++++++++++++++++++++++++++++++++++++++++ testes/memerr.lua | 19 +++++++++++++++++++ testes/tracegc.lua | 9 +++++++-- 8 files changed, 118 insertions(+), 16 deletions(-) diff --git a/ldo.c b/ldo.c index 6d0184ecd5..12e0364b98 100644 --- a/ldo.c +++ b/ldo.c @@ -221,13 +221,21 @@ l_noret luaD_errerr (lua_State *L) { /* -** Check whether stack has enough space to run a simple function (such -** as a finalizer): At least BASIC_STACK_SIZE in the Lua stack and -** 2 slots in the C stack. +** Check whether stacks have enough space to run a simple function (such +** as a finalizer): At least BASIC_STACK_SIZE in the Lua stack, two +** available CallInfos, and two "slots" in the C stack. */ int luaD_checkminstack (lua_State *L) { - return ((stacksize(L) < MAXSTACK - BASIC_STACK_SIZE) && - (getCcalls(L) < LUAI_MAXCCALLS - 2)); + if (getCcalls(L) >= LUAI_MAXCCALLS - 2) + return 0; /* not enough C-stack slots */ + if (L->ci->next == NULL && luaE_extendCI(L, 0) == NULL) + return 0; /* unable to allocate first ci */ + if (L->ci->next->next == NULL && luaE_extendCI(L, 0) == NULL) + return 0; /* unable to allocate second ci */ + if (L->stack_last.p - L->top.p >= BASIC_STACK_SIZE) + return 1; /* enough (BASIC_STACK_SIZE) free slots in the Lua stack */ + else /* try to grow stack to a size with enough free slots */ + return luaD_growstack(L, BASIC_STACK_SIZE, 0); } @@ -616,7 +624,7 @@ void luaD_poscall (lua_State *L, CallInfo *ci, int nres) { -#define next_ci(L) (L->ci->next ? L->ci->next : luaE_extendCI(L)) +#define next_ci(L) (L->ci->next ? L->ci->next : luaE_extendCI(L, 1)) /* diff --git a/lgc.c b/lgc.c index f1d9a7ce8a..0f89451c64 100644 --- a/lgc.c +++ b/lgc.c @@ -1293,7 +1293,7 @@ static void finishgencycle (lua_State *L, global_State *g) { correctgraylists(g); checkSizes(L, g); g->gcstate = GCSpropagate; /* skip restart */ - if (!g->gcemergency && luaD_checkminstack(L)) + if (g->tobefnz != NULL && !g->gcemergency && luaD_checkminstack(L)) callallpendingfinalizers(L); } diff --git a/lstate.c b/lstate.c index 70a11aaec6..7d34199198 100644 --- a/lstate.c +++ b/lstate.c @@ -68,14 +68,19 @@ void luaE_setdebt (global_State *g, l_mem debt) { } -CallInfo *luaE_extendCI (lua_State *L) { +CallInfo *luaE_extendCI (lua_State *L, int err) { CallInfo *ci; - lua_assert(L->ci->next == NULL); - ci = luaM_new(L, CallInfo); - lua_assert(L->ci->next == NULL); - L->ci->next = ci; + ci = luaM_reallocvector(L, NULL, 0, 1, CallInfo); + if (l_unlikely(ci == NULL)) { /* allocation failed? */ + if (err) + luaM_error(L); /* raise the error */ + return NULL; /* else only report it */ + } + ci->next = L->ci->next; ci->previous = L->ci; - ci->next = NULL; + L->ci->next = ci; + if (ci->next) + ci->next->previous = ci; ci->u.l.trap = 0; L->nci++; return ci; diff --git a/lstate.h b/lstate.h index 20dc4d24f0..013872835d 100644 --- a/lstate.h +++ b/lstate.h @@ -438,7 +438,7 @@ union GCUnion { LUAI_FUNC void luaE_setdebt (global_State *g, l_mem debt); LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1); LUAI_FUNC lu_mem luaE_threadsize (lua_State *L); -LUAI_FUNC CallInfo *luaE_extendCI (lua_State *L); +LUAI_FUNC CallInfo *luaE_extendCI (lua_State *L, int err); LUAI_FUNC void luaE_shrinkCI (lua_State *L); LUAI_FUNC void luaE_checkcstack (lua_State *L); LUAI_FUNC void luaE_incCstack (lua_State *L); diff --git a/ltests.c b/ltests.c index c4905f9487..ce2b20ca5e 100644 --- a/ltests.c +++ b/ltests.c @@ -1106,6 +1106,27 @@ static int stacklevel (lua_State *L) { } +static int resetCI (lua_State *L) { + CallInfo *ci = L->ci; + while (ci->next != NULL) { + CallInfo *tofree = ci->next; + ci->next = ci->next->next; + luaM_free(L, tofree); + L->nci--; + } + return 0; +} + + +static int reallocstack (lua_State *L) { + int n = cast_int(luaL_checkinteger(L, 1)); + lua_lock(L); + luaD_reallocstack(L, cast_int(L->top.p - L->stack.p) + n, 1); + lua_unlock(L); + return 0; +} + + static int table_query (lua_State *L) { const Table *t; int i = cast_int(luaL_optinteger(L, 2, -1)); @@ -2182,6 +2203,8 @@ static const struct luaL_Reg tests_funcs[] = { {"s2d", s2d}, {"sethook", sethook}, {"stacklevel", stacklevel}, + {"resetCI", resetCI}, + {"reallocstack", reallocstack}, {"sizes", get_sizes}, {"testC", testC}, {"makeCfunc", makeCfunc}, diff --git a/testes/gc.lua b/testes/gc.lua index 62713dac64..e50d9029b0 100644 --- a/testes/gc.lua +++ b/testes/gc.lua @@ -707,4 +707,46 @@ end collectgarbage(oldmode) + +if T then + print("testing stack issues when calling finalizers") + + local X + local obj + + local function initobj () + X = false + obj = setmetatable({}, {__gc = function () X = true end}) + end + + local function loop (n) + if n > 0 then loop(n - 1) end + end + + -- should not try to call finalizer without a CallInfo available + initobj() + loop(20) -- ensure stack space + T.resetCI() -- remove extra CallInfos + T.alloccount(0) -- cannot allocate more CallInfos + obj = nil + collectgarbage() -- will not call finalizer + T.alloccount() + assert(X == false) + collectgarbage() -- now will call finalizer (it was still pending) + assert(X == true) + + -- should not try to call finalizer without stack space available + initobj() + loop(5) -- ensure enough CallInfos + T.reallocstack(0) -- remove extra stack slots + T.alloccount(0) -- cannot reallocate stack + obj = nil + collectgarbage() -- will not call finalizer + T.alloccount() + assert(X == false) + collectgarbage() -- now will call finalizer (it was still pending) + assert(X == true) +end + + print('OK') diff --git a/testes/memerr.lua b/testes/memerr.lua index 9c940ca79a..a55514a9e8 100644 --- a/testes/memerr.lua +++ b/testes/memerr.lua @@ -282,6 +282,25 @@ testamem("growing stack", function () return foo(100) end) + +collectgarbage() +collectgarbage() +global io, T, setmetatable, collectgarbage, print + +local Count = 0 +testamem("finalizers", function () + local X = false + local obj = setmetatable({}, {__gc = function () X = true end}) + obj = nil + T.resetCI() -- remove extra CallInfos + T.reallocstack(18) -- remove extra stack slots + Count = Count + 1 + io.stderr:write(Count, "\n") + T.trick(io) + collectgarbage() + return X +end) + -- }================================================================== diff --git a/testes/tracegc.lua b/testes/tracegc.lua index a8c929dffd..c1154f90f2 100644 --- a/testes/tracegc.lua +++ b/testes/tracegc.lua @@ -1,10 +1,15 @@ -- track collections + local M = {} -- import list -local setmetatable, stderr, collectgarbage = - setmetatable, io.stderr, collectgarbage +local stderr, collectgarbage = io.stderr, collectgarbage + +-- the debug version of setmetatable does not create any object (such as +-- a '__metatable' string), and so it is more appropriate to be used in +-- a finalizer +local setmetatable = require"debug".setmetatable global none From f5d1e8639bf5df24c761602354218df21f796a30 Mon Sep 17 00:00:00 2001 From: Roberto I Date: Fri, 16 Jan 2026 16:38:44 -0300 Subject: [PATCH 1133/1145] New compile option LUA_COMPAT_LOOPVAR When on, this option makes for-loop control variables not read only. --- lparser.c | 13 +++++++++++-- luaconf.h | 7 +++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/lparser.c b/lparser.c index b3855d4cb6..b27463af3e 100644 --- a/lparser.c +++ b/lparser.c @@ -1682,13 +1682,22 @@ static void forbody (LexState *ls, int base, int line, int nvars, int isgen) { } +/* +** Control whether for-loop control variables are read-only +*/ +#if defined(LUA_COMPAT_LOOPVAR) +#define LOOPVARKIND VDKREG +#else /* by default, these variables are read only */ +#define LOOPVARKIND RDKCONST +#endif + static void fornum (LexState *ls, TString *varname, int line) { /* fornum -> NAME = exp,exp[,exp] forbody */ FuncState *fs = ls->fs; int base = fs->freereg; new_localvarliteral(ls, "(for state)"); new_localvarliteral(ls, "(for state)"); - new_varkind(ls, varname, RDKCONST); /* control variable */ + new_varkind(ls, varname, LOOPVARKIND); /* control variable */ checknext(ls, '='); exp1(ls); /* initial value */ checknext(ls, ','); @@ -1715,7 +1724,7 @@ static void forlist (LexState *ls, TString *indexname) { new_localvarliteral(ls, "(for state)"); /* iterator function */ new_localvarliteral(ls, "(for state)"); /* state */ new_localvarliteral(ls, "(for state)"); /* closing var. (after swap) */ - new_varkind(ls, indexname, RDKCONST); /* control variable */ + new_varkind(ls, indexname, LOOPVARKIND); /* control variable */ /* other declared variables */ while (testnext(ls, ',')) { new_localvar(ls, str_checkname(ls)); diff --git a/luaconf.h b/luaconf.h index 96a77802b9..f076c98472 100644 --- a/luaconf.h +++ b/luaconf.h @@ -342,6 +342,13 @@ #define LUA_COMPAT_GLOBAL +/* +@@ LUA_COMPAT_LOOPVAR makes for-loop control variables not read-only, +** as they were in previous versions. +*/ +/* #define LUA_COMPAT_LOOPVAR */ + + /* @@ LUA_COMPAT_MATHLIB controls the presence of several deprecated ** functions in the mathematical library. From e992c6a95939c8e1fe357bfce481e0d0c762c3c6 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 20 Jan 2026 13:06:16 -0300 Subject: [PATCH 1134/1145] Some compilation options configurable from makefile Compilation options LUA_COMPAT_GLOBAL, LUA_COMPAT_LOOPVAR, and LUA_READLINELIB do not affect the API, so they can be changed through the make file. --- llex.c | 2 +- lparser.c | 4 ++-- ltests.h | 1 + luaconf.h | 12 ++++++++++-- manual/manual.of | 6 ++++++ 5 files changed, 20 insertions(+), 5 deletions(-) diff --git a/llex.c b/llex.c index f8bb3ea4b4..7cd9fcaf52 100644 --- a/llex.c +++ b/llex.c @@ -188,7 +188,7 @@ void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, TString *source, so they cannot be collected */ ls->envn = luaS_newliteral(L, LUA_ENV); /* get env string */ ls->brkn = luaS_newliteral(L, "break"); /* get "break" string */ -#if defined(LUA_COMPAT_GLOBAL) +#if LUA_COMPAT_GLOBAL /* compatibility mode: "global" is not a reserved word */ ls->glbn = luaS_newliteral(L, "global"); /* get "global" string */ ls->glbn->extra = 0; /* mark it as not reserved */ diff --git a/lparser.c b/lparser.c index b27463af3e..6b87773ea3 100644 --- a/lparser.c +++ b/lparser.c @@ -1685,7 +1685,7 @@ static void forbody (LexState *ls, int base, int line, int nvars, int isgen) { /* ** Control whether for-loop control variables are read-only */ -#if defined(LUA_COMPAT_LOOPVAR) +#if LUA_COMPAT_LOOPVAR #define LOOPVARKIND VDKREG #else /* by default, these variables are read only */ #define LOOPVARKIND RDKCONST @@ -2120,7 +2120,7 @@ static void statement (LexState *ls) { gotostat(ls, line); break; } -#if defined(LUA_COMPAT_GLOBAL) +#if LUA_COMPAT_GLOBAL case TK_NAME: { /* compatibility code to parse global keyword when "global" is not reserved */ diff --git a/ltests.h b/ltests.h index 93096da810..f5f14cd61c 100644 --- a/ltests.h +++ b/ltests.h @@ -14,6 +14,7 @@ /* test Lua with compatibility code */ #define LUA_COMPAT_MATHLIB #undef LUA_COMPAT_GLOBAL +#define LUA_COMPAT_GLOBAL 0 #define LUA_DEBUG diff --git a/luaconf.h b/luaconf.h index f076c98472..1b72e3384a 100644 --- a/luaconf.h +++ b/luaconf.h @@ -70,15 +70,19 @@ #if defined(LUA_USE_LINUX) #define LUA_USE_POSIX #define LUA_USE_DLOPEN /* needs an extra library: -ldl */ +#if !defined(LUA_READLINELIB) #define LUA_READLINELIB "libreadline.so" #endif +#endif #if defined(LUA_USE_MACOSX) #define LUA_USE_POSIX #define LUA_USE_DLOPEN /* macOS does not need -ldl */ +#if !defined(LUA_READLINELIB) #define LUA_READLINELIB "libedit.dylib" #endif +#endif #if defined(LUA_USE_IOS) @@ -339,14 +343,18 @@ /* @@ LUA_COMPAT_GLOBAL avoids 'global' being a reserved word */ -#define LUA_COMPAT_GLOBAL +#if !defined(LUA_COMPAT_GLOBAL) +#define LUA_COMPAT_GLOBAL 1 +#endif /* @@ LUA_COMPAT_LOOPVAR makes for-loop control variables not read-only, ** as they were in previous versions. */ -/* #define LUA_COMPAT_LOOPVAR */ +#if !defined(LUA_COMPAT_LOOPVAR) +#define LUA_COMPAT_LOOPVAR 0 +#endif /* diff --git a/manual/manual.of b/manual/manual.of index 5fa4e097e1..09075346fd 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -9594,12 +9594,18 @@ change between versions. @item{ The word @Rw{global} is a reserved word. Do not use it as a regular name. + +The compilation option @id{LUA_COMPAT_GLOBAL} (see @id{luaconf.h}) +makes @id{global} a regular word. } @item{ The control variable in @Rw{for} loops is read only. If you need to change it, declare a local variable with the same name in the loop body. + +The compilation option @id{LUA_COMPAT_LOOPVAR} (see @id{luaconf.h}) +makes these variables regular (writable). } @item{ From 3360710bd3ea8da06fa5062f9d10c2719083097c Mon Sep 17 00:00:00 2001 From: Roberto I Date: Thu, 22 Jan 2026 13:47:10 -0300 Subject: [PATCH 1135/1145] Another way to handle option -E A pointer to function 'l_getenv' can point to the regular 'getenv' or to a dumb function (that always returns NULL) to ignore environment variables. --- lua.c | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/lua.c b/lua.c index 5054583de9..37fd1cb87e 100644 --- a/lua.c +++ b/lua.c @@ -374,12 +374,21 @@ static int runargs (lua_State *L, char **argv, int n) { } +static char *(*l_getenv)(const char *name); + +/* Function to ignore environment variables, used by option -E */ +static char *no_getenv (const char *name) { + UNUSED(name); + return NULL; +} + + static int handle_luainit (lua_State *L) { const char *name = "=" LUA_INITVARVERSION; - const char *init = getenv(name + 1); + const char *init = l_getenv(name + 1); if (init == NULL) { name = "=" LUA_INIT_VAR; - init = getenv(name + 1); /* try alternative name */ + init = l_getenv(name + 1); /* try alternative name */ } if (init == NULL) return LUA_OK; else if (init[0] == '@') @@ -715,17 +724,18 @@ static int pmain (lua_State *L) { if (args & has_v) /* option '-v'? */ print_version(); if (args & has_E) { /* option '-E'? */ + l_getenv = &no_getenv; /* program will ignore environment variables */ lua_pushboolean(L, 1); /* signal for libraries to ignore env. vars. */ lua_setfield(L, LUA_REGISTRYINDEX, "LUA_NOENV"); } + else + l_getenv = &getenv; luai_openlibs(L); /* open standard libraries */ createargtable(L, argv, argc, script); /* create table 'arg' */ lua_gc(L, LUA_GCRESTART); /* start GC... */ lua_gc(L, LUA_GCGEN); /* ...in generational mode */ - if (!(args & has_E)) { /* no option '-E'? */ - if (handle_luainit(L) != LUA_OK) /* run LUA_INIT */ - return 0; /* error running LUA_INIT */ - } + if (handle_luainit(L) != LUA_OK) /* run LUA_INIT */ + return 0; /* error running LUA_INIT */ if (!runargs(L, argv, optlim)) /* execute arguments -e, -l, and -W */ return 0; /* something failed */ if (script > 0) { /* execute main script (if there is one) */ From cfcaa9493b783527e5b5dfb71afb51602b3bccac Mon Sep 17 00:00:00 2001 From: Roberto I Date: Fri, 23 Jan 2026 16:25:18 -0300 Subject: [PATCH 1136/1145] Explanation about char* parameters in the C API --- manual/manual.of | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/manual/manual.of b/manual/manual.of index 09075346fd..893592da60 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -2692,7 +2692,19 @@ which behaves like a nil value. @sect3{constchar|@title{Pointers to Strings} -Several functions in the API return pointers (@T{const char*}) +Several functions in the API accept pointers (@T{const char*}) +to C strings. +Some of there parameters have an associated length (@T{size_t}). +Unless stated otherwise, +when there is an associated length, +the string can contain embedded zeros; +moreover, the pointer can be @id{NULL} if the length is zero. +When there is no associated length, +the pointer must point to a zero-terminated string. +In any case, the string contents should remain unchanged +until the function returns. + +Several functions in the API also return pointers (@T{const char*}) to Lua strings in the stack. (See @Lid{lua_pushfstring}, @Lid{lua_pushlstring}, @Lid{lua_pushstring}, and @Lid{lua_tolstring}. From efbc29754544dd820bfdc81edf17d7dcfad31d05 Mon Sep 17 00:00:00 2001 From: Roberto I Date: Thu, 29 Jan 2026 14:24:25 -0300 Subject: [PATCH 1137/1145] New year and (eventual) new release --- lua.h | 6 +++--- manual/2html | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lua.h b/lua.h index ab473dc3e4..6deaed49c2 100644 --- a/lua.h +++ b/lua.h @@ -13,13 +13,13 @@ #include -#define LUA_COPYRIGHT LUA_RELEASE " Copyright (C) 1994-2025 Lua.org, PUC-Rio" +#define LUA_COPYRIGHT LUA_RELEASE " Copyright (C) 1994-2026 Lua.org, PUC-Rio" #define LUA_AUTHORS "R. Ierusalimschy, L. H. de Figueiredo, W. Celes" #define LUA_VERSION_MAJOR_N 5 #define LUA_VERSION_MINOR_N 5 -#define LUA_VERSION_RELEASE_N 0 +#define LUA_VERSION_RELEASE_N 1 #define LUA_VERSION_NUM (LUA_VERSION_MAJOR_N * 100 + LUA_VERSION_MINOR_N) #define LUA_VERSION_RELEASE_NUM (LUA_VERSION_NUM * 100 + LUA_VERSION_RELEASE_N) @@ -521,7 +521,7 @@ struct lua_Debug { /****************************************************************************** -* Copyright (C) 1994-2025 Lua.org, PUC-Rio. +* Copyright (C) 1994-2026 Lua.org, PUC-Rio. * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the diff --git a/manual/2html b/manual/2html index b7afd2a6e4..d3b88b349b 100755 --- a/manual/2html +++ b/manual/2html @@ -30,7 +30,7 @@ by Roberto Ierusalimschy, Luiz Henrique de Figueiredo, Waldemar Celes

Copyright -© 2025 Lua.org, PUC-Rio. All rights reserved. +© 2026 Lua.org, PUC-Rio. All rights reserved.


From c6b484823806e08e1756b1a6066a3ace6f080fae Mon Sep 17 00:00:00 2001 From: Roberto I Date: Fri, 30 Jan 2026 16:47:33 -0300 Subject: [PATCH 1138/1145] Environment variable for readline library name The name of the readline library can be changed from its default value through environment variable LUA_READLINELIB. --- lua.c | 24 ++++++++++++++++++------ testes/main.lua | 23 ++++++++++++++++++++++- 2 files changed, 40 insertions(+), 7 deletions(-) diff --git a/lua.c b/lua.c index 37fd1cb87e..3f4fc9f780 100644 --- a/lua.c +++ b/lua.c @@ -30,6 +30,12 @@ #define LUA_INIT_VAR "LUA_INIT" #endif +/* Name of the environment variable with the name of the readline library */ +#if !defined(LUA_RLLIB_VAR) +#define LUA_RLLIB_VAR "LUA_READLINELIB" +#endif + + #define LUA_INITVARVERSION LUA_INIT_VAR LUA_VERSUFFIX @@ -507,18 +513,24 @@ static void lua_freeline (char *line) { #include static void lua_initreadline (lua_State *L) { - void *lib = dlopen(LUA_READLINELIB, RTLD_NOW | RTLD_LOCAL); - if (lib == NULL) - lua_warning(L, "library '" LUA_READLINELIB "' not found", 0); - else { + const char *rllib = l_getenv(LUA_RLLIB_VAR); /* name of readline library */ + void *lib; /* library handle */ + if (rllib == NULL) /* no environment variable? */ + rllib = LUA_READLINELIB; /* use default name */ + lib = dlopen(rllib, RTLD_NOW | RTLD_LOCAL); + if (lib != NULL) { const char **name = cast(const char**, dlsym(lib, "rl_readline_name")); if (name != NULL) *name = "lua"; l_readline = cast(l_readlineT, cast_func(dlsym(lib, "readline"))); l_addhist = cast(l_addhistT, cast_func(dlsym(lib, "add_history"))); - if (l_readline == NULL) - lua_warning(L, "unable to load 'readline'", 0); + if (l_readline != NULL) /* could load readline function? */ + return; /* everything ok */ + /* else emit a warning */ } + lua_warning(L, "unable to load readline library '", 1); + lua_warning(L, rllib, 1); + lua_warning(L, "'", 0); } #else /* }{ */ diff --git a/testes/main.lua b/testes/main.lua index dc48dc485f..98d3695189 100644 --- a/testes/main.lua +++ b/testes/main.lua @@ -78,6 +78,9 @@ end RUN('lua -v') +RUN('lua -v > %s', out) +local release = string.match(getoutput(), "Lua (%d+%.%d+%.%d+)") + print(string.format("(temporary program file used in these tests: %s)", prog)) -- running stdin as a file @@ -167,7 +170,9 @@ checkout("10\n11\n") -- test errors in LUA_INIT NoRun('LUA_INIT:1: msg', 'env LUA_INIT="error(\'msg\')" lua') --- test option '-E' + +print("testing option '-E'") + local defaultpath, defaultCpath do @@ -192,6 +197,22 @@ assert(not string.find(defaultpath, "xxx") and string.find(defaultCpath, "lua")) +-- (LUA_READLINELIB was introduced in 5.5.1) +if release >= "5.5.1" then + print"testing readline library name" + -- should generate a warning when trying to load inexistent library "xuxu" + local env = [[LUA_READLINELIB=xuxu LUA_INIT="warn('@allow')"]] + local code = 'echo " " | env %s lua %s -W -i >%s 2>&1' + RUN(code, env, "", out) -- run code with no extra options + assert(string.find(getoutput(), + "warning: unable to load readline library 'xuxu'")) + + RUN(code, env, "-E", out) -- run again with option -E + -- no warning when LUA_READLINELIB is to be ignored + assert(not string.find(getoutput(), "warning")) +end + + -- test replacement of ';;' to default path local function convert (p) prepfile("print(package.path)") From b60e2bcd7ca4c349bd6ee7a8e929f55e04f7ca87 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 9 Feb 2026 13:44:27 -0300 Subject: [PATCH 1139/1145] Avoid an assignment of values that overlap The original code was like this, where t->u.ind.t and t->u.info overlap: t->u.ind.t = cast_byte((t->k == VLOCAL) ? t->u.var.ridx: t->u.info); --- lcode.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/lcode.c b/lcode.c index 4caa8046f6..33cbd6874f 100644 --- a/lcode.c +++ b/lcode.c @@ -827,7 +827,7 @@ void luaK_dischargevars (FuncState *fs, expdesc *e) { } /* FALLTHROUGH */ case VLOCAL: { /* already in a register */ int temp = e->u.var.ridx; - e->u.info = temp; /* (can't do a direct assignment; values overlap) */ + e->u.info = temp; /* (avoid a direct assignment; values overlap) */ e->k = VNONRELOC; /* becomes a non-relocatable value */ break; } @@ -1365,7 +1365,7 @@ void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) { luaK_exp2anyreg(fs, t); /* put it in a register */ if (t->k == VUPVAL) { lu_byte temp = cast_byte(t->u.info); /* upvalue index */ - t->u.ind.t = temp; /* (can't do a direct assignment; values overlap) */ + t->u.ind.t = temp; /* (avoid a direct assignment; values overlap) */ lua_assert(isKstr(fs, k)); fillidxk(t, k->u.info, VINDEXUP); /* literal short string */ } @@ -1373,12 +1373,13 @@ void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) { int kreg = luaK_exp2anyreg(fs, k); /* put key in some register */ lu_byte vreg = cast_byte(t->u.var.ridx); /* register with vararg param. */ lua_assert(vreg == fs->f->numparams); - t->u.ind.t = vreg; /* (avoid a direct assignment; values may overlap) */ + t->u.ind.t = vreg; /* (avoid a direct assignment; values may overlap?) */ fillidxk(t, kreg, VVARGIND); /* 't' represents 'vararg[k]' */ } else { /* register index of the table */ - t->u.ind.t = cast_byte((t->k == VLOCAL) ? t->u.var.ridx: t->u.info); + lu_byte temp = cast_byte((t->k == VLOCAL) ? t->u.var.ridx: t->u.info); + t->u.ind.t = temp; /* (avoid a direct assignment; values may overlap?) */ if (isKstr(fs, k)) fillidxk(t, k->u.info, VINDEXSTR); /* literal short string */ else if (isCint(k)) /* int. constant in proper range? */ From 7c40c5edb2364745bf0add6feb02c7c90dfbae3e Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 10 Feb 2026 16:41:02 -0300 Subject: [PATCH 1140/1145] Details Spaces + added initialization to the documentation of global declarations. --- luaconf.h | 18 +++++++++--------- manual/manual.of | 2 +- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/luaconf.h b/luaconf.h index 1b72e3384a..7f2206d06d 100644 --- a/luaconf.h +++ b/luaconf.h @@ -228,17 +228,17 @@ #if !defined(LUA_PATH_DEFAULT) #define LUA_PATH_DEFAULT \ - LUA_LDIR"?.lua;" LUA_LDIR"?\\init.lua;" \ - LUA_CDIR"?.lua;" LUA_CDIR"?\\init.lua;" \ - LUA_SHRDIR"?.lua;" LUA_SHRDIR"?\\init.lua;" \ + LUA_LDIR "?.lua;" LUA_LDIR "?\\init.lua;" \ + LUA_CDIR "?.lua;" LUA_CDIR "?\\init.lua;" \ + LUA_SHRDIR "?.lua;" LUA_SHRDIR "?\\init.lua;" \ ".\\?.lua;" ".\\?\\init.lua" #endif #if !defined(LUA_CPATH_DEFAULT) #define LUA_CPATH_DEFAULT \ - LUA_CDIR"?.dll;" \ - LUA_CDIR"..\\lib\\lua\\" LUA_VDIR "\\?.dll;" \ - LUA_CDIR"loadall.dll;" ".\\?.dll" + LUA_CDIR "?.dll;" \ + LUA_CDIR "..\\lib\\lua\\" LUA_VDIR "\\?.dll;" \ + LUA_CDIR "loadall.dll;" ".\\?.dll" #endif #else /* }{ */ @@ -249,14 +249,14 @@ #if !defined(LUA_PATH_DEFAULT) #define LUA_PATH_DEFAULT \ - LUA_LDIR"?.lua;" LUA_LDIR"?/init.lua;" \ - LUA_CDIR"?.lua;" LUA_CDIR"?/init.lua;" \ + LUA_LDIR "?.lua;" LUA_LDIR "?/init.lua;" \ + LUA_CDIR "?.lua;" LUA_CDIR "?/init.lua;" \ "./?.lua;" "./?/init.lua" #endif #if !defined(LUA_CPATH_DEFAULT) #define LUA_CPATH_DEFAULT \ - LUA_CDIR"?.so;" LUA_CDIR"loadall.so;" "./?.so" + LUA_CDIR "?.so;" LUA_CDIR "loadall.so;" "./?.so" #endif #endif /* } */ diff --git a/manual/manual.of b/manual/manual.of index 893592da60..18c187f439 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -9743,7 +9743,7 @@ and @bnfNter{LiteralString}, see @See{lexical}.) @OrNL @Rw{local} @Rw{function} @bnfNter{Name} funcbody @OrNL @Rw{global} @Rw{function} @bnfNter{Name} funcbody @OrNL @Rw{local} attnamelist @bnfopt{@bnfter{=} explist} -@OrNL @Rw{global} attnamelist +@OrNL @Rw{global} attnamelist @bnfopt{@bnfter{=} explist} @OrNL @Rw{global} @bnfopt{attrib} @bnfter{*} } From 10eb89d1141dc528806b32401e408e36fb2f3bf5 Mon Sep 17 00:00:00 2001 From: Roberto I Date: Wed, 18 Feb 2026 13:24:04 -0300 Subject: [PATCH 1141/1145] BUG: shift overflow in utf-8 decode An initial byte \xFF will ask for 7 continuation bytes, and then the shift by (count * 5) will try to shift 35 bits. --- lutf8lib.c | 5 ++++- makefile | 2 +- testes/utf8.lua | 14 +++++++++++++- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/lutf8lib.c b/lutf8lib.c index b7f3fe1e16..73f0e49bf6 100644 --- a/lutf8lib.c +++ b/lutf8lib.c @@ -56,6 +56,8 @@ static const char *utf8_decode (const char *s, l_uint32 *val, int strict) { l_uint32 res = 0; /* final result */ if (c < 0x80) /* ASCII? */ res = c; + else if (c >= 0xfe) /* c >= 1111 1110b ? */ + return NULL; /* would need six or more continuation bytes */ else { int count = 0; /* to count number of continuation bytes */ for (; c & 0x40; c <<= 1) { /* while it needs continuation bytes... */ @@ -64,8 +66,9 @@ static const char *utf8_decode (const char *s, l_uint32 *val, int strict) { return NULL; /* invalid byte sequence */ res = (res << 6) | (cc & 0x3F); /* add lower 6 bits from cont. byte */ } + lua_assert(count <= 5); res |= ((l_uint32)(c & 0x7F) << (count * 5)); /* add first byte */ - if (count > 5 || res > MAXUTF || res < limits[count]) + if (res > MAXUTF || res < limits[count]) return NULL; /* invalid byte sequence */ s += count; /* skip continuation bytes read */ } diff --git a/makefile b/makefile index 8674519f5f..fa165bca6a 100644 --- a/makefile +++ b/makefile @@ -60,7 +60,7 @@ CWARNS= $(CWARNSCPP) $(CWARNSC) $(CWARNGCC) # create problems; some are only available in newer gcc versions. To # use some of them, we also have to define an environment variable # ASAN_OPTIONS="detect_invalid_pointer_pairs=2". -# -fsanitize=undefined +# -fsanitize=undefined (you may need to add "-lubsan" to libs) # -fsanitize=pointer-subtract -fsanitize=address -fsanitize=pointer-compare # TESTS= -DLUA_USER_H='"ltests.h"' -Og -g diff --git a/testes/utf8.lua b/testes/utf8.lua index 028995a478..8a0213d651 100644 --- a/testes/utf8.lua +++ b/testes/utf8.lua @@ -238,10 +238,18 @@ s = "\0 \x7F\z s = string.gsub(s, " ", "") check(s, {0,0x7F, 0x80,0x7FF, 0x800,0xFFFF, 0x10000,0x10FFFF}) + +-- again, without strictness +s = "\xF0\x90\x80\x80 \xF7\xBF\xBF\xBF\z + \xF8\x88\x80\x80\x80 \xFB\xBF\xBF\xBF\xBF\z + \xFC\x84\x80\x80\x80\x80 \xFD\xBF\xBF\xBF\xBF\xBF" +s = string.gsub(s, " ", "") +check(s, {0x10000,0x1FFFFF, 0x200000,0x3FFFFFF, 0x4000000,0x7FFFFFFF}, true) + do -- original UTF-8 values local s = "\u{4000000}\u{7FFFFFFF}" - assert(#s == 12) + assert(s == "\xFC\x84\x80\x80\x80\x80\xFD\xBF\xBF\xBF\xBF\xBF") check(s, {0x4000000, 0x7FFFFFFF}, true) s = "\u{200000}\u{3FFFFFF}" @@ -257,6 +265,10 @@ local x = "日本語a-4\0éó" check(x, {26085, 26412, 35486, 97, 45, 52, 0, 233, 243}) +-- more than 5 continuation bytes +assert(not utf8.len("\xff\x8f\x8f\x8f\x8f\x8f\x8f\x8f")) + + -- Supplementary Characters check("𣲷𠜎𠱓𡁻𠵼ab𠺢", {0x23CB7, 0x2070E, 0x20C53, 0x2107B, 0x20D7C, 0x61, 0x62, 0x20EA2,}) From 9e501d9855e560b08c50fb0cf6e147af93bb497e Mon Sep 17 00:00:00 2001 From: Roberto I Date: Mon, 9 Mar 2026 16:23:03 -0300 Subject: [PATCH 1142/1145] Slightly better documentation for LUAI_MAXALIGN --- luaconf.h | 11 +++++++++-- manual/manual.of | 8 ++++++-- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/luaconf.h b/luaconf.h index 7f2206d06d..5ac9d9884e 100644 --- a/luaconf.h +++ b/luaconf.h @@ -736,10 +736,17 @@ /* -@@ LUAI_MAXALIGN defines fields that, when used in a union, ensure -** maximum alignment for the other items in that union. +@@ LUAI_MAXALIGN defines fields that ensure proper alignment for +** memory areas offered by Lua (e.g., userdata memory). +** Add fields to it if you need alignment for non-ISO objects. */ +#if defined(LLONG_MAX) +/* use ISO C99 stuff */ +#define LUAI_MAXALIGN long double u; void *s; long long l +#else +/* use only C89 stuff */ #define LUAI_MAXALIGN lua_Number n; double u; void *s; lua_Integer i; long l +#endif /* }================================================================== */ diff --git a/manual/manual.of b/manual/manual.of index 18c187f439..5eb2fb1f4f 100644 --- a/manual/manual.of +++ b/manual/manual.of @@ -3915,8 +3915,12 @@ like any Lua object. This function creates and pushes on the stack a new full userdata, with @id{nuvalue} associated Lua values, called @id{user values}, plus an associated block of raw memory with @id{size} bytes. -(The user values can be set and read with the functions -@Lid{lua_setiuservalue} and @Lid{lua_getiuservalue}.) + +The user values can be set and read with the functions +@Lid{lua_setiuservalue} and @Lid{lua_getiuservalue}. +The block of memory is suitably aligned for any @N{ISO C} object. +(See macro @id{LUAI_MAXALIGN} in file @id{luaconf.h} for other +alignment requirements.) The function returns the address of the block of memory. Lua ensures that this address is valid as long as From 36d5d2b2847906aa3b66e020d5d894a14ba2bf90 Mon Sep 17 00:00:00 2001 From: Roberto I Date: Mon, 9 Mar 2026 16:24:06 -0300 Subject: [PATCH 1143/1145] Details --- lopcodes.c | 2 +- lutf8lib.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lopcodes.c b/lopcodes.c index 7e182315bc..c4828bfc02 100644 --- a/lopcodes.c +++ b/lopcodes.c @@ -104,7 +104,7 @@ LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = { ,opmode(0, 1, 0, 0, 1, iABC) /* OP_VARARG */ ,opmode(0, 0, 0, 0, 1, iABC) /* OP_GETVARG */ ,opmode(0, 0, 0, 0, 0, iABx) /* OP_ERRNNIL */ - ,opmode(0, 0, 1, 0, 1, iABC) /* OP_VARARGPREP */ + ,opmode(0, 0, 1, 0, 0, iABC) /* OP_VARARGPREP */ ,opmode(0, 0, 0, 0, 0, iAx) /* OP_EXTRAARG */ }; diff --git a/lutf8lib.c b/lutf8lib.c index 73f0e49bf6..0cd7f9c363 100644 --- a/lutf8lib.c +++ b/lutf8lib.c @@ -149,7 +149,7 @@ static int codepoint (lua_State *L) { static void pushutfchar (lua_State *L, int arg) { lua_Unsigned code = (lua_Unsigned)luaL_checkinteger(L, arg); luaL_argcheck(L, code <= MAXUTF, arg, "value out of range"); - lua_pushfstring(L, "%U", (long)code); + lua_pushfstring(L, "%U", cast(unsigned long, code)); } From 377cbea61b2688b21c7d243fc0f42498851df794 Mon Sep 17 00:00:00 2001 From: Roberto I Date: Mon, 9 Mar 2026 16:24:49 -0300 Subject: [PATCH 1144/1145] 'table.tunpack' using 'aux_getn' like the others 'table.tunpack' was not checking its first argument, which could result in error messages generated inside the API, without location information. --- ltablib.c | 11 +++++++---- testes/sort.lua | 13 +++++++++++++ 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/ltablib.c b/ltablib.c index 46ecb5e024..15c3c09f04 100644 --- a/ltablib.c +++ b/ltablib.c @@ -42,15 +42,17 @@ static int checkfield (lua_State *L, const char *key, int n) { /* ** Check that 'arg' either is a table or can behave like one (that is, -** has a metatable with the required metamethods) +** has a metatable with the required metamethods). */ static void checktab (lua_State *L, int arg, int what) { - if (lua_type(L, arg) != LUA_TTABLE) { /* is it not a table? */ + int tp = lua_type(L, arg); + if (tp != LUA_TTABLE) { /* is it not a table? */ int n = 1; /* number of elements to pop */ if (lua_getmetatable(L, arg) && /* must have metatable */ (!(what & TAB_R) || checkfield(L, "__index", ++n)) && (!(what & TAB_W) || checkfield(L, "__newindex", ++n)) && - (!(what & TAB_L) || checkfield(L, "__len", ++n))) { + (!(what & TAB_L) || /* strings don't need '__len' to have a length */ + tp == LUA_TSTRING || checkfield(L, "__len", ++n))) { lua_pop(L, n); /* pop metatable and tested metamethods */ } else @@ -204,8 +206,9 @@ static int tpack (lua_State *L) { static int tunpack (lua_State *L) { lua_Unsigned n; + lua_Integer len = aux_getn(L, 1, TAB_R); lua_Integer i = luaL_optinteger(L, 2, 1); - lua_Integer e = luaL_opt(L, luaL_checkinteger, 3, luaL_len(L, 1)); + lua_Integer e = luaL_opt(L, luaL_checkinteger, 3, len); if (i > e) return 0; /* empty range */ n = l_castS2U(e) - l_castS2U(i); /* number of elements minus 1 */ if (l_unlikely(n >= (unsigned int)INT_MAX || diff --git a/testes/sort.lua b/testes/sort.lua index b012766057..92aaca3cef 100644 --- a/testes/sort.lua +++ b/testes/sort.lua @@ -72,6 +72,19 @@ assert(a==1 and x==nil) a,x = unpack({1,2}, 1, 1) assert(a==1 and x==nil) + +do -- unpack with non-tables + local debug = require"debug" + local oldmt = debug.getmetatable(0) + local str = "hello" + debug.setmetatable(0, + { __len = function () return #str end, + __index = function (_, i) return string.sub(str, i, i) end}) + assert(table.concat({table.unpack(0)}) == str) + debug.setmetatable(0, oldmt) -- restore original metatable for numbers +end + + do local maxi = (1 << 31) - 1 -- maximum value for an int (usually) local mini = -(1 << 31) -- minimum value for an int (usually) From 51269bd783c9371252947b26cc865239dbb0153d Mon Sep 17 00:00:00 2001 From: Roberto I Date: Sun, 15 Mar 2026 15:14:14 -0300 Subject: [PATCH 1145/1145] Adjustment in useless parameter L in macros luai_num* --- llimits.h | 12 ++++++------ lvm.c | 6 +++--- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/llimits.h b/llimits.h index fc5cb276f6..3f0372552a 100644 --- a/llimits.h +++ b/llimits.h @@ -234,12 +234,12 @@ typedef unsigned long l_uint32; /* floor division (defined as 'floor(a/b)') */ #if !defined(luai_numidiv) -#define luai_numidiv(L,a,b) ((void)L, l_floor(luai_numdiv(L,a,b))) +#define luai_numidiv(L,a,b) l_floor(luai_numdiv(L,a,b)) #endif /* float division */ #if !defined(luai_numdiv) -#define luai_numdiv(L,a,b) ((a)/(b)) +#define luai_numdiv(L,a,b) ((void)L, (a)/(b)) #endif /* @@ -267,10 +267,10 @@ typedef unsigned long l_uint32; /* the others are quite standard operations */ #if !defined(luai_numadd) -#define luai_numadd(L,a,b) ((a)+(b)) -#define luai_numsub(L,a,b) ((a)-(b)) -#define luai_nummul(L,a,b) ((a)*(b)) -#define luai_numunm(L,a) (-(a)) +#define luai_numadd(L,a,b) ((void)L, (a)+(b)) +#define luai_numsub(L,a,b) ((void)L, (a)-(b)) +#define luai_nummul(L,a,b) ((void)L, (a)*(b)) +#define luai_numunm(L,a) ((void)L, -(a)) #define luai_numeq(a,b) ((a)==(b)) #define luai_numlt(a,b) ((a)<(b)) #define luai_numle(a,b) ((a)<=(b)) diff --git a/lvm.c b/lvm.c index c70e2b8a8a..96ae16390f 100644 --- a/lvm.c +++ b/lvm.c @@ -268,9 +268,9 @@ static int forprep (lua_State *L, StkId ra) { /* ** Execute a step of a float numerical for loop, returning ** true iff the loop must continue. (The integer case is -** written online with opcode OP_FORLOOP, for performance.) +** written inline with opcode OP_FORLOOP, for performance.) */ -static int floatforloop (StkId ra) { +static int floatforloop (lua_State *L, StkId ra) { lua_Number step = fltvalue(s2v(ra + 1)); lua_Number limit = fltvalue(s2v(ra)); lua_Number idx = fltvalue(s2v(ra + 2)); /* control variable */ @@ -1841,7 +1841,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) { pc -= GETARG_Bx(i); /* jump back */ } } - else if (floatforloop(ra)) /* float loop */ + else if (floatforloop(L, ra)) /* float loop */ pc -= GETARG_Bx(i); /* jump back */ updatetrap(ci); /* allows a signal to break the loop */ vmbreak;