From 1df786307c1983b8ce693e3916081a8bcd4e95ae Mon Sep 17 00:00:00 2001 From: Li Jin Date: Wed, 3 Mar 2021 21:31:01 +0800 Subject: add new metatable syntax for issue #41, fix reusing local variable issue, update built-in Lua. --- spec/inputs/metatable.yue | 43 +++++ src/lua/lapi.c | 15 +- src/lua/lapi.h | 2 + src/lua/lauxlib.c | 44 +++-- src/lua/lauxlib.h | 8 +- src/lua/lbaselib.c | 15 +- src/lua/lcode.c | 16 +- src/lua/lcorolib.c | 10 +- src/lua/ldblib.c | 7 +- src/lua/ldebug.c | 105 +++++++----- src/lua/ldebug.h | 10 ++ src/lua/ldo.c | 299 ++++++++++++++++++-------------- src/lua/ldo.h | 2 +- src/lua/lfunc.c | 69 ++++---- src/lua/lfunc.h | 9 +- src/lua/lgc.c | 38 +++-- src/lua/liolib.c | 17 +- src/lua/llimits.h | 16 -- src/lua/lmathlib.c | 5 +- src/lua/lmem.c | 33 ++-- src/lua/loadlib.c | 17 +- src/lua/lobject.h | 10 +- src/lua/lopcodes.h | 14 +- src/lua/loslib.c | 8 +- src/lua/lparser.c | 8 +- src/lua/lstate.c | 24 +-- src/lua/lstate.h | 46 ++++- src/lua/lstring.c | 8 +- src/lua/lstrlib.c | 41 ++--- src/lua/ltable.c | 8 +- src/lua/ltablib.c | 11 +- src/lua/ltm.c | 5 +- src/lua/luaconf.h | 103 ++++++----- src/lua/lvm.c | 60 ++++--- src/lua/lvm.h | 6 +- src/yuescript/ast.hpp | 4 +- src/yuescript/yue_ast.h | 35 +++- src/yuescript/yue_compiler.cpp | 379 +++++++++++++++++++++++++++++++++-------- src/yuescript/yue_parser.cpp | 16 +- src/yuescript/yue_parser.h | 4 + 40 files changed, 1017 insertions(+), 553 deletions(-) create mode 100644 spec/inputs/metatable.yue diff --git a/spec/inputs/metatable.yue b/spec/inputs/metatable.yue new file mode 100644 index 0000000..6f7b418 --- /dev/null +++ b/spec/inputs/metatable.yue @@ -0,0 +1,43 @@ +a = close: true, close#: => print "out of scope" +b = add#: (left, right)-> right - left +c = key1: true, :add#, key2: true + +close _ = close#: -> print "out of scope" + +d, e = a.close, a.close# + +f = a\close# 1 +a.add# = (x, y)-> x + y + +do + {:new, :close#, close#: closeA} = a + print new, close, closeA + +do + local * + x, \ + {:new, :var, :close#, close#: closeA}, \ + :num, :add#, :sub# \ + = 123, a.b.c, func! + +x.abc, a.b.# = 123, {} +func!.# = mt, extra +a, b.c.#, d, e = 1, mt, "abc" + +is_same = a.#.__index == a.index# + +-- +a.# = __index: tb +a.#.__index = tb +a.index# = tb +-- + +mt = a.# + +tb\func #list +tb\func#list +tb\func# list + +import "module" as :index#, newindex#:setFunc + +nil diff --git a/src/lua/lapi.c b/src/lua/lapi.c index 3583e9c..a9cf2fd 100644 --- a/src/lua/lapi.c +++ b/src/lua/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. */ @@ -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; } } } @@ -191,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); } @@ -202,10 +202,10 @@ 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 */ setnilvalue(s2v(level)); lua_unlock(L); } @@ -1264,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/src/lua/lapi.h b/src/lua/lapi.h index 41216b2..9e99cc4 100644 --- a/src/lua/lapi.h +++ b/src/lua/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/src/lua/lauxlib.c b/src/lua/lauxlib.c index e8fc486..94835ef 100644 --- a/src/lua/lauxlib.c +++ b/src/lua/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 */ } @@ -515,13 +515,22 @@ 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. */ 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; @@ -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 */ } @@ -861,7 +873,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 +1086,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/src/lua/lauxlib.h b/src/lua/lauxlib.h index 6571491..9058e26 100644 --- a/src/lua/lauxlib.h +++ b/src/lua/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/src/lua/lbaselib.c b/src/lua/lbaselib.c index 747fd45..83ad306 100644 --- a/src/lua/lbaselib.c +++ b/src/lua/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); @@ -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; } @@ -299,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 */ @@ -355,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); @@ -393,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); @@ -401,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 */ @@ -437,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/src/lua/lcode.c b/src/lua/lcode.c index d8d353f..80d975c 100644 --- a/src/lua/lcode.c +++ b/src/lua/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 @@ -337,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"); @@ -1307,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/src/lua/lcorolib.c b/src/lua/lcorolib.c index ed7c58b..fedbebe 100644 --- a/src/lua/lcorolib.c +++ b/src/lua/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/src/lua/ldblib.c b/src/lua/ldblib.c index 15593bf..6dcbaa9 100644 --- a/src/lua/ldblib.c +++ b/src/lua/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"); } @@ -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 */ @@ -212,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); @@ -237,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/src/lua/ldebug.c b/src/lua/ldebug.c index 819550d..8e3657a 100644 --- a/src/lua/ldebug.c +++ b/src/lua/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); @@ -48,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) { @@ -59,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; } @@ -305,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 */ } } @@ -645,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 */ } @@ -733,7 +730,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)); } @@ -791,16 +788,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 +819,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 +847,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/src/lua/ldebug.h b/src/lua/ldebug.h index 55b3ae0..974960e 100644 --- a/src/lua/ldebug.h +++ b/src/lua/ldebug.h @@ -26,6 +26,16 @@ */ #define ABSLINEINFO (-0x80) + +/* +** MAXimum number of successive Instructions WiTHout ABSolute line +** information. (A power of two allows fast divisions.) +*/ +#if !defined(MAXIWTHABS) +#define MAXIWTHABS 128 +#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/src/lua/ldo.c b/src/lua/ldo.c index aa159cf..7135079 100644 --- a/src/lua/ldo.c +++ b/src/lua/ldo.c @@ -160,9 +160,8 @@ 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) up->v = s2v((uplevel(up) - oldstack) + newstack); for (ci = L->ci; ci != NULL; ci = ci->previous) { @@ -178,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 (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 */ } - 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; @@ -203,7 +218,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. */ @@ -219,7 +234,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 */ @@ -294,8 +309,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; @@ -305,8 +320,10 @@ 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 (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; @@ -328,38 +345,40 @@ 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' */ -} - - -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)) { + 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; 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->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 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? */ + StkId firstres = L->top - nres; /* index of first result */ + int delta = 0; /* correction for vararg functions */ int ftransfer; + if (isLua(ci)) { + Proto *p = ci_func(ci)->p; + 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); luaD_hook(L, LUA_HOOKRET, -1, ftransfer, nres); /* call it */ ci->func -= delta; } if (isLua(ci = ci->previous)) - L->oldpc = pcRel(ci->u.l.savedpc, ci_func(ci)->p); /* update 'oldpc' */ - return restorestack(L, oldtop); + L->oldpc = pcRel(ci->u.l.savedpc, ci_func(ci)->p); /* set 'oldpc' */ } @@ -371,7 +390,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 (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); @@ -396,27 +415,34 @@ 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; 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 */ - res = restorestack(L, savedres); - wanted = codeNresults(wanted); /* correct value */ + 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; + wanted = nres; /* we want all results */ } break; } + /* generic case */ 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) /* 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)); @@ -425,15 +451,21 @@ 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_unlikely(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); + /* 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) */ } @@ -492,7 +524,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); } @@ -538,7 +570,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 */ @@ -565,27 +597,74 @@ 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; - int n; - /* 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); - lua_unlock(L); - n = (*ci->u.c.k)(L, status, ci->u.c.ctx); /* call continuation function */ - lua_lock(L); - api_checknelems(L, n); +static int finishpcallk (lua_State *L, CallInfo *ci) { + int status = getcistrecst(ci); /* get original status */ + 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); + 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 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; /* 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' */ } @@ -600,7 +679,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 +702,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 @@ -688,8 +733,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); @@ -705,19 +752,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,8 +787,8 @@ 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); - if (likely(!errorstatus(status))) + status = precover(L, 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' */ @@ -765,22 +814,22 @@ 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 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 */ @@ -818,7 +867,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; @@ -841,7 +890,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/src/lua/ldo.h b/src/lua/ldo.h index c7721d6..6bf0ed8 100644 --- a/src/lua/ldo.h +++ b/src/lua/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/src/lua/lfunc.c b/src/lua/lfunc.c index 13e44d4..b4c04bd 100644 --- a/src/lua/lfunc.c +++ b/src/lua/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,33 +155,21 @@ 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. +** 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 */ - 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 */ - } + 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; } @@ -194,11 +182,9 @@ 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. +** 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' */ while ((uv = L->openupval) != NULL && (upl = uplevel(uv)) >= level) { @@ -211,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/src/lua/lfunc.h b/src/lua/lfunc.h index 2e6df53..dc1cebc 100644 --- a/src/lua/lfunc.h +++ b/src/lua/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/src/lua/lgc.c b/src/lua/lgc.c index bab9beb..b360eed 100644 --- a/src/lua/lgc.c +++ b/src/lua/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 */ } @@ -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/src/lua/liolib.c b/src/lua/liolib.c index 7951672..b08397d 100644 --- a/src/lua/liolib.c +++ b/src/lua/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/src/lua/llimits.h b/src/lua/llimits.h index d039483..025f1c8 100644 --- a/src/lua/llimits.h +++ b/src/lua/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/src/lua/lmathlib.c b/src/lua/lmathlib.c index 86def47..5f5983a 100644 --- a/src/lua/lmathlib.c +++ b/src/lua/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/src/lua/lmem.c b/src/lua/lmem.c index 43739bf..9029d58 100644 --- a/src/lua/lmem.c +++ b/src/lua/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 (ttisnil(&g->nilvalue) && ns > os) + if (completestate(g) && ns > 0) /* frees never fail */ return NULL; /* fail */ else /* normal allocation */ return (*g->frealloc)(g->ud, block, os, ns); @@ -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 */ } @@ -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 (ttisnil(&g->nilvalue)) { /* 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,17 +158,14 @@ 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; global_State *g = G(L); lua_assert((osize == 0) == (block == NULL)); 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); + 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' */ } @@ -179,7 +178,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 +190,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/src/lua/loadlib.c b/src/lua/loadlib.c index c0ec9a1..6f9fa37 100644 --- a/src/lua/loadlib.c +++ b/src/lua/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/src/lua/lobject.h b/src/lua/lobject.h index 470b17d..1a7a737 100644 --- a/src/lua/lobject.h +++ b/src/lua/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/src/lua/lopcodes.h b/src/lua/lopcodes.h index 120cdd9..d6a47e5 100644 --- a/src/lua/lopcodes.h +++ b/src/lua/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/src/lua/loslib.c b/src/lua/loslib.c index e65e188..3e20d62 100644 --- a/src/lua/loslib.c +++ b/src/lua/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/src/lua/lparser.c b/src/lua/lparser.c index 249ba9a..284ef1f 100644 --- a/src/lua/lparser.c +++ b/src/lua/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/src/lua/lstate.c b/src/lua/lstate.c index 92ccbf9..c5e3b43 100644 --- a/src/lua/lstate.c +++ b/src/lua/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); } @@ -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; @@ -226,8 +227,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 +237,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); } @@ -253,6 +252,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; @@ -268,10 +268,13 @@ static void preinit_thread (lua_State *L, global_State *g) { 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 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)); @@ -296,7 +299,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; @@ -313,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); @@ -328,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 @@ -363,7 +365,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; @@ -378,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/src/lua/lstate.h b/src/lua/lstate.h index 38a6c9b..c1283bb 100644 --- a/src/lua/lstate.h +++ b/src/lua/lstate.h @@ -156,6 +156,18 @@ 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 '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. */ typedef struct CallInfo { StkId func; /* function index in the stack */ @@ -176,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 */ @@ -191,17 +204,34 @@ 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_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) /* call is running 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 #if defined(LUA_COMPAT_LT_LE) -#define CIST_LEQ (1<<9) /* using __lt for __le */ +#define CIST_LEQ (1<<13) /* 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)) @@ -230,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 */ @@ -281,6 +312,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 */ @@ -297,6 +329,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) diff --git a/src/lua/lstring.c b/src/lua/lstring.c index 138871c..13dcaf4 100644 --- a/src/lua/lstring.c +++ b/src/lua/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/src/lua/lstrlib.c b/src/lua/lstrlib.c index c7242ea..47e5b27 100644 --- a/src/lua/lstrlib.c +++ b/src/lua/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/src/lua/ltable.c b/src/lua/ltable.c index e98bab7..b520cdf 100644 --- a/src/lua/ltable.c +++ b/src/lua/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/src/lua/ltablib.c b/src/lua/ltablib.c index d344a47..d80eb80 100644 --- a/src/lua/ltablib.c +++ b/src/lua/ltablib.c @@ -145,8 +145,8 @@ 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'", + 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/src/lua/ltm.c b/src/lua/ltm.c index 4770f96..b657b78 100644 --- a/src/lua/ltm.c +++ b/src/lua/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/src/lua/luaconf.h b/src/lua/luaconf.h index d9cf18c..ae73e2f 100644 --- a/src/lua/luaconf.h +++ b/src/lua/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. @@ -659,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/src/lua/lvm.c b/src/lua/lvm.c index d6c05bb..c9729bc 100644 --- a/src/lua/lvm.c +++ b/src/lua/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 */ @@ -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 */ @@ -651,7 +656,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 +700,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 +716,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 +736,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 */ @@ -921,7 +926,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 +955,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); } @@ -1049,7 +1054,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 +1113,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 +1141,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 */ @@ -1150,6 +1156,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 */ @@ -1633,10 +1641,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? */ @@ -1678,23 +1684,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 +1714,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 +1818,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/src/lua/lvm.h b/src/lua/lvm.h index 2d4ac16..1bc16f3 100644 --- a/src/lua/lvm.h +++ b/src/lua/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)) diff --git a/src/yuescript/ast.hpp b/src/yuescript/ast.hpp index c88fcf9..eb5cd69 100644 --- a/src/yuescript/ast.hpp +++ b/src/yuescript/ast.hpp @@ -100,7 +100,9 @@ public: inline ast_ptr new_ptr() const { auto item = new T; item->m_begin.m_line = m_begin.m_line; - item->m_end.m_line = m_begin.m_line; + item->m_begin.m_col = m_begin.m_col; + item->m_end.m_line = m_end.m_line; + item->m_end.m_col = m_end.m_col; return ast_ptr(item); } private: diff --git a/src/yuescript/yue_ast.h b/src/yuescript/yue_ast.h index 2d4814d..535a239 100644 --- a/src/yuescript/yue_ast.h +++ b/src/yuescript/yue_ast.h @@ -161,10 +161,12 @@ AST_END(import_all_macro) class variable_pair_t; class normal_pair_t; +class meta_variable_pair_t; +class meta_normal_pair_t; AST_NODE(ImportTabLit) ast_ptr sep; - ast_sel_list items; + ast_sel_list items; AST_MEMBER(ImportTabLit, &sep, &items) AST_END(ImportTabLit) @@ -423,9 +425,20 @@ AST_NODE(normal_pair) AST_MEMBER(normal_pair, &key, &value) AST_END(normal_pair) +AST_NODE(meta_variable_pair) + ast_ptr name; + AST_MEMBER(meta_variable_pair, &name) +AST_END(meta_variable_pair) + +AST_NODE(meta_normal_pair) + ast_sel key; + ast_sel value; + AST_MEMBER(meta_normal_pair, &key, &value) +AST_END(meta_normal_pair) + AST_NODE(simple_table) ast_ptr sep; - ast_sel_list pairs; + ast_sel_list pairs; AST_MEMBER(simple_table, &sep, &pairs) AST_END(simple_table) @@ -484,13 +497,21 @@ AST_NODE(String) AST_MEMBER(String, &str) AST_END(String) -AST_NODE(DotChainItem) +AST_LEAF(Metatable) +AST_END(Metatable) + +AST_NODE(Metamethod) ast_ptr name; + AST_MEMBER(Metamethod, &name) +AST_END(Metamethod) + +AST_NODE(DotChainItem) + ast_sel name; AST_MEMBER(DotChainItem, &name) AST_END(DotChainItem) AST_NODE(ColonChainItem) - ast_sel name; + ast_sel name; bool switchToDot = false; AST_MEMBER(ColonChainItem, &name) AST_END(ColonChainItem) @@ -542,19 +563,19 @@ AST_END(default_value) AST_NODE(TableLit) ast_ptr sep; - ast_sel_list values; + ast_sel_list values; AST_MEMBER(TableLit, &sep, &values) AST_END(TableLit) AST_NODE(TableBlockIndent) ast_ptr sep; - ast_sel_list values; + ast_sel_list values; AST_MEMBER(TableBlockIndent, &sep, &values) AST_END(TableBlockIndent) AST_NODE(TableBlock) ast_ptr sep; - ast_sel_list values; + ast_sel_list values; AST_MEMBER(TableBlock, &sep, &values) AST_END(TableBlock) diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp index 0e65ae9..043c705 100644 --- a/src/yuescript/yue_compiler.cpp +++ b/src/yuescript/yue_compiler.cpp @@ -48,9 +48,9 @@ using namespace parserlib; #define YUEE(msg,node) throw std::logic_error( \ _info.errorMessage( \ std::string("[File] ") + __FILE__ \ - + ", [Func] " + __FUNCTION__ \ - + ", [Line] " + std::to_string(__LINE__) \ - + ", [Error] " + msg, node)) + + ",\n[Func] " + __FUNCTION__ \ + + ",\n[Line] " + std::to_string(__LINE__) \ + + ",\n[Error] " + msg, node)) typedef std::list str_list; @@ -58,7 +58,7 @@ inline std::string s(std::string_view sv) { return std::string(sv); } -const std::string_view version = "0.6.10"sv; +const std::string_view version = "0.7.0"sv; const std::string_view extension = "yue"sv; class YueCompilerImpl { @@ -618,13 +618,11 @@ private: template ast_ptr toAst(std::string_view codes, ast_node* parent) { auto res = _parser.parse(s(codes)); - int line = parent->m_begin.m_line; - int col = parent->m_begin.m_line; res.node->traverse([&](ast_node* node) { - node->m_begin.m_line = line; - node->m_end.m_line = line; - node->m_begin.m_col = col; - node->m_end.m_col = col; + node->m_begin.m_line = parent->m_begin.m_line; + node->m_begin.m_col = parent->m_begin.m_col; + node->m_end.m_line = parent->m_end.m_line; + node->m_end.m_col = parent->m_end.m_col; return traversal::Continue; }); _codeCache.push_back(std::move(res.codes)); @@ -641,7 +639,8 @@ private: EndWithEOP, HasEOP, HasKeyword, - Macro + Macro, + Metatable }; ChainType specialChainValue(ChainValue_t* chainValue) const { @@ -654,6 +653,11 @@ private: if (ast_is(chainValue->items.back())) { return ChainType::EndWithEOP; } + if (auto dot = ast_cast(chainValue->items.back())) { + if (dot->name.is()) { + return ChainType::Metatable; + } + } ChainType type = ChainType::Common; for (auto item : chainValue->items.objects()) { if (auto colonChain = ast_cast(item)) { @@ -798,6 +802,18 @@ private: return false; } + std::string globalVar(std::string_view var, ast_node* x) { + std::string str(var); + if (_config.lintGlobalVariable) { + if (!isDefined(str)) { + if (_globals.find(str) == _globals.end()) { + _globals[str] = {x->m_begin.m_line, x->m_begin.m_col}; + } + } + } + return str; + } + void transformStatement(Statement_t* statement, str_list& out) { auto x = statement; if (statement->appendix) { @@ -1107,6 +1123,53 @@ private: BLOCK_START auto assign = ast_cast(assignment->action); BREAK_IF(!assign); + auto x = assignment; + const auto& exprs = assignment->expList->exprs.objects(); + const auto& values = assign->values.objects(); + auto vit = values.begin(); + for (auto it = exprs.begin(); it != exprs.end(); ++it) { + BLOCK_START + auto value = singleValueFrom(*it); + BREAK_IF(!value); + auto chainValue = value->item.as(); + BREAK_IF(!chainValue); + if (specialChainValue(chainValue) == ChainType::Metatable) { + str_list args; + chainValue->items.pop_back(); + transformExp(static_cast(*it), args, ExpUsage::Closure); + if (vit != values.end()) transformAssignItem(*vit, args); + else args.push_back(s("nil"sv)); + _buf << indent() << globalVar("setmetatable"sv, x) << '(' << join(args, ", "sv) << ')' << nll(x); + str_list temp; + temp.push_back(clearBuf()); + auto newExpList = x->new_ptr(); + auto newAssign = x->new_ptr(); + auto newAssignment = x->new_ptr(); + newAssignment->expList.set(newExpList); + newAssignment->action.set(newAssign); + for (auto exp : exprs) { + if (exp != *it) newExpList->exprs.push_back(exp); + } + for (auto value : values) { + if (value != *vit) newAssign->values.push_back(value); + } + if (newExpList->exprs.empty() && newAssign->values.empty()) { + out.push_back(temp.back()); + return; + } + if (newExpList->exprs.size() < newAssign->values.size()) { + auto exp = toAst("_"sv, x); + while (newExpList->exprs.size() < newAssign->values.size()) { + newExpList->exprs.push_back(exp); + } + } + transformAssignment(newAssignment, temp); + out.push_back(join(temp)); + return; + } + BLOCK_END + if (vit != values.end()) ++vit; + } BREAK_IF(assign->values.objects().size() != 1); auto value = assign->values.objects().back(); if (ast_is(value)) { @@ -1221,15 +1284,19 @@ private: return; case ChainType::Common: case ChainType::EndWithEOP: + case ChainType::Metatable: break; } } BLOCK_END - auto info = extractDestructureInfo(assignment); + auto info = extractDestructureInfo(assignment, false); if (info.first.empty()) { transformAssignmentCommon(assignment, out); } else { str_list temp; + if (info.second) { + transformAssignmentCommon(info.second, temp); + } for (const auto& destruct : info.first) { if (destruct.items.size() == 1) { auto& pair = destruct.items.front(); @@ -1240,7 +1307,7 @@ private: _buf << pair.name << " = "sv << destruct.value << pair.structure << nll(assignment); addToScope(pair.name); temp.push_back(clearBuf()); - } else if (_parser.match(destruct.value)) { + } else if (_parser.match(destruct.value) && isDefined(destruct.value)) { str_list defs, names, values; for (const auto& item : destruct.items) { if (item.isVariable && addToScope(item.name)) { @@ -1281,9 +1348,6 @@ private: temp.push_back(clearBuf()); } } - if (info.second) { - transformAssignmentCommon(info.second, temp); - } out.push_back(join(temp)); } } @@ -1426,13 +1490,13 @@ private: } std::pair, ast_ptr> - extractDestructureInfo(ExpListAssign_t* assignment, bool varDefOnly = false) { + extractDestructureInfo(ExpListAssign_t* assignment, bool varDefOnly) { auto x = assignment; std::list destructs; - if (!assignment->action.is()) return { destructs, nullptr }; + if (!assignment->action.is()) return {destructs, nullptr}; auto exprs = assignment->expList->exprs.objects(); auto values = assignment->action.to()->values.objects(); - size_t size = std::max(exprs.size(),values.size()); + size_t size = std::max(exprs.size(), values.size()); ast_ptr var; if (exprs.size() < size) { var = toAst("_"sv, x); @@ -1445,6 +1509,8 @@ private: } using iter = node_container::iterator; std::vector> destructPairs; + ast_list valueItems; + if (!varDefOnly) pushScope(); str_list temp; for (auto i = exprs.begin(), j = values.begin(); i != exprs.end(); ++i, ++j) { auto expr = *i; @@ -1459,26 +1525,86 @@ private: } } destructPairs.push_back({i,j}); - auto& destruct = destructs.emplace_back(); - if (!varDefOnly) { - pushScope(); - transformAssignItem(*j, temp); - destruct.value = temp.back(); - temp.pop_back(); - popScope(); + auto subDestruct = destructNode->new_ptr(); + auto subMetaDestruct = destructNode->new_ptr(); + const node_container* dlist = nullptr; + switch (destructNode->getId()) { + case id(): + dlist = &static_cast(destructNode)->values.objects(); + break; + case id(): + dlist = &static_cast(destructNode)->pairs.objects(); + break; + default: YUEE("AST node mismatch", destructNode); break; + } + for (auto item : *dlist) { + switch (item->getId()) { + case id(): { + auto mp = static_cast(item); + auto name = _parser.toString(mp->name); + _buf << "__"sv << name << ':' << name; + auto newPair = toAst(clearBuf(), item); + subMetaDestruct->values.push_back(newPair); + break; + } + case id(): { + auto mp = static_cast(item); + auto key = _parser.toString(mp->key); + _buf << "__"sv << key; + auto newKey = toAst(clearBuf(), item); + auto newPair = item->new_ptr(); + newPair->key.set(newKey); + newPair->value.set(mp->value); + subMetaDestruct->values.push_back(newPair); + break; + } + default: + subDestruct->values.push_back(item); + break; + } } - auto pairs = destructFromExp(expr); - destruct.items = std::move(pairs); - if (*j == nullNode) { - for (auto& item : destruct.items) { - item.structure.clear(); + valueItems.push_back(*j); + if (!varDefOnly && !subDestruct->values.empty() && !subMetaDestruct->values.empty()) { + auto objVar = getUnusedName("_obj_"sv); + addToScope(objVar); + valueItems.pop_back(); + valueItems.push_back(toAst(objVar, *j)); + exprs.push_back(valueItems.back()); + values.push_back(*j); + } + TableLit_t* tabs[] = {subDestruct.get(), subMetaDestruct.get()}; + for (auto tab : tabs) { + if (!tab->values.empty()) { + auto& destruct = destructs.emplace_back(); + if (!varDefOnly) { + transformAssignItem(valueItems.back(), temp); + destruct.value = temp.back(); + temp.pop_back(); + } + auto simpleValue = tab->new_ptr(); + simpleValue->value.set(tab); + auto value = tab->new_ptr(); + value->item.set(simpleValue); + auto pairs = destructFromExp(newExp(value, expr)); + destruct.items = std::move(pairs); + if (!varDefOnly) { + if (*j == nullNode) { + for (auto& item : destruct.items) { + item.structure.clear(); + } + } else if (tab == subMetaDestruct.get()) { + destruct.value.insert(0, globalVar("getmetatable"sv, tab) + '('); + destruct.value.append(")"sv); + } else if (destruct.items.size() == 1 && !singleValueFrom(*j)) { + destruct.value.insert(0, "("sv); + destruct.value.append(")"sv); + } + } } - } else if (destruct.items.size() == 1 && !singleValueFrom(*j)) { - destruct.value.insert(0, "("sv); - destruct.value.append(")"sv); } } } + if (!varDefOnly) popScope(); for (const auto& p : destructPairs) { exprs.erase(p.first); values.erase(p.second); @@ -1511,6 +1637,8 @@ private: auto leftValue = singleValueFrom(leftExp); if (!leftValue) throw std::logic_error(_info.errorMessage("left hand expression is not assignable"sv, leftExp)); if (auto chain = leftValue->item.as()) { + if (specialChainValue(chain) == ChainType::Metatable) {throw std::logic_error(_info.errorMessage("can not apply update to a metatable"sv, leftExp)); + } auto tmpChain = x->new_ptr(); for (auto item : chain->items.objects()) { bool itemAdded = false; @@ -1911,7 +2039,7 @@ private: switch (item->getId()) { case id(): { transformVariable(static_cast(item), out); - if (_config.lintGlobalVariable && !isDefined(out.back())) { + if (_config.lintGlobalVariable && !isSolidDefined(out.back())) { if (_globals.find(out.back()) == _globals.end()) { _globals[out.back()] = {item->m_begin.m_line, item->m_begin.m_col}; } @@ -1920,14 +2048,7 @@ private: } case id(): { transformSelfName(static_cast(item), out, invoke); - if (_config.lintGlobalVariable) { - std::string self("self"sv); - if (!isDefined(self)) { - if (_globals.find(self) == _globals.end()) { - _globals[self] = {item->m_begin.m_line, item->m_begin.m_col}; - } - } - } + globalVar("self"sv, item); break; } case id(): @@ -3030,6 +3151,55 @@ private: return false; } + bool transformChainWithMetatable(const node_container& chainList, str_list& out, ExpUsage usage, ExpList_t* assignList) { + auto opIt = std::find_if(chainList.begin(), chainList.end(), [](ast_node* node) { + if (auto colonChain = ast_cast(node)) { + if (ast_is(colonChain->name)) { + return true; + } + } else if (auto dotChain = ast_cast(node)) { + if (ast_is(dotChain->name)) { + return true; + } + } + return false; + }); + if (opIt == chainList.end()) return false; + auto x = chainList.front(); + auto chain = x->new_ptr(); + for (auto it = chainList.begin(); it != opIt; ++it) { + chain->items.push_back(*it); + } + auto value = x->new_ptr(); + value->item.set(chain); + auto exp = newExp(value, x); + + chain = toAst("getmetatable()"sv, x); + ast_to(chain->items.back())->args.push_back(exp); + switch ((*opIt)->getId()) { + case id(): { + auto colon = static_cast(*opIt); + auto meta = colon->name.to(); + auto newColon = toAst(s("\\__"sv) + _parser.toString(meta->name), x); + chain->items.push_back(newColon); + break; + } + case id(): { + auto dot = static_cast(*opIt); + if (dot->name.is()) break; + auto meta = dot->name.to(); + auto newDot = toAst(s(".__"sv) + _parser.toString(meta->name), x); + chain->items.push_back(newDot); + break; + } + } + for (auto it = ++opIt; it != chainList.end(); ++it) { + chain->items.push_back(*it); + } + transformChainValue(chain, out, usage, assignList); + return true; + } + void transformChainList(const node_container& chainList, str_list& out, ExpUsage usage, ExpList_t* assignList = nullptr) { auto x = chainList.front(); str_list temp; @@ -3553,6 +3723,9 @@ private: #endif // YUE_NO_MACRO } const auto& chainList = chainValue->items.objects(); + if (transformChainWithMetatable(chainList, out, usage, assignList)) { + return; + } if (transformChainEndWithEOP(chainList, out, usage, assignList)) { return; } @@ -3801,7 +3974,8 @@ private: switch (loopTarget->getId()) { case id(): { auto star_exp = static_cast(loopTarget); - std::string listVar; + auto listVar = singleVariableFrom(star_exp->value); + if (!isSolidDefined(listVar)) listVar.clear(); auto indexVar = getUnusedName("_index_"sv); varAfter.push_back(indexVar); auto value = singleValueFrom(star_exp->value); @@ -3814,6 +3988,12 @@ private: auto slice = ast_cast(chainList.back()); BREAK_IF(!slice); endWithSlice = true; + if (listVar.empty() && chainList.size() == 2) { + if (auto var = chainList.front()->getByPath()) { + listVar = _parser.toString(var); + if (!isSolidDefined(listVar)) listVar.clear(); + } + } chainList.pop_back(); auto chain = x->new_ptr(); for (auto item : chainList) { @@ -3837,10 +4017,12 @@ private: stepValue = temp.back(); temp.pop_back(); } - listVar = getUnusedName("_list_"sv); - varBefore.push_back(listVar); - transformChainValue(chain, temp, ExpUsage::Closure); - _buf << indent() << "local "sv << listVar << " = "sv << temp.back() << nll(nameList); + if (listVar.empty()) { + listVar = getUnusedName("_list_"sv); + varBefore.push_back(listVar); + transformChainValue(chain, temp, ExpUsage::Closure); + _buf << indent() << "local "sv << listVar << " = "sv << temp.back() << nll(nameList); + } std::string maxVar; if (!stopValue.empty()) { maxVar = getUnusedName("_max_"sv); @@ -4248,15 +4430,7 @@ private: } case id(): { transformExp(static_cast(content), temp, ExpUsage::Closure); - std::string tostr("tostring"sv); - temp.back() = tostr + '(' + temp.back() + s(")"sv); - if (_config.lintGlobalVariable) { - if (!isDefined(tostr)) { - if (_globals.find(tostr) == _globals.end()) { - _globals[tostr] = {content->m_begin.m_line, content->m_begin.m_col}; - } - } - } + temp.back() = globalVar("tostring"sv, content) + '(' + temp.back() + s(")"sv); break; } default: YUEE("AST node mismatch", content); break; @@ -4460,7 +4634,7 @@ private: if (extend) { _buf << indent() << "setmetatable("sv << baseVar << ", "sv << parentVar << ".__base)"sv << nll(classDecl); } - _buf << indent() << classVar << " = setmetatable({"sv << nll(classDecl); + _buf << indent() << classVar << " = " << globalVar("setmetatable"sv, classDecl) << "({"sv << nll(classDecl); if (!builtins.empty()) { _buf << join(builtins) << ","sv << nll(classDecl); } else { @@ -4673,8 +4847,14 @@ private: checkAssignable(with->valueList); auto vars = getAssignVars(with); if (vars.front().empty()) { - withVar = getUnusedName("_with_"sv); - { + if (with->assigns->values.objects().size() == 1) { + auto var = singleVariableFrom(with->assigns->values.objects().front()); + if (!var.empty() && isSolidDefined(var)) { + withVar = var; + } + } + if (withVar.empty()) { + withVar = getUnusedName("_with_"sv); auto assignment = x->new_ptr(); assignment->expList.set(toAst(withVar, x)); auto assign = x->new_ptr(); @@ -4714,18 +4894,21 @@ private: transformAssignment(assignment, temp); } } else { - withVar = getUnusedName("_with_"sv); - auto assignment = x->new_ptr(); - assignment->expList.set(toAst(withVar, x)); - auto assign = x->new_ptr(); - assign->values.dup(with->valueList->exprs); - assignment->action.set(assign); - if (!returnValue) { - scoped = true; - temp.push_back(indent() + s("do"sv) + nll(with)); - pushScope(); + withVar = singleVariableFrom(with->valueList); + if (withVar.empty() || !isSolidDefined(withVar)) { + withVar = getUnusedName("_with_"sv); + auto assignment = x->new_ptr(); + assignment->expList.set(toAst(withVar, x)); + auto assign = x->new_ptr(); + assign->values.dup(with->valueList->exprs); + assignment->action.set(assign); + if (!returnValue) { + scoped = true; + temp.push_back(indent() + s("do"sv) + nll(with)); + pushScope(); + } + transformAssignment(assignment, temp); } - transformAssignment(assignment, temp); } if (!with->eop && !scoped && !returnValue) { pushScope(); @@ -4953,20 +5136,64 @@ private: } str_list temp; incIndentOffset(); + auto metatable = table->new_ptr(); for (auto pair : pairs) { + bool isMetamethod = false; switch (pair->getId()) { case id(): transformExp(static_cast(pair), temp, ExpUsage::Closure); break; case id(): transform_variable_pair(static_cast(pair), temp); break; case id(): transform_normal_pair(static_cast(pair), temp); break; case id(): transformTableBlockIndent(static_cast(pair), temp); break; case id(): transformTableBlock(static_cast(pair), temp); break; + case id(): { + isMetamethod = true; + auto mp = static_cast(pair); + auto name = _parser.toString(mp->name); + _buf << "__"sv << name << ':' << name; + auto newPair = toAst(clearBuf(), pair); + metatable->pairs.push_back(newPair); + break; + } + case id(): { + isMetamethod = true; + auto mp = static_cast(pair); + auto key = _parser.toString(mp->key); + _buf << "__"sv << key; + auto newKey = toAst(clearBuf(), pair); + auto newPair = pair->new_ptr(); + newPair->key.set(newKey); + newPair->value.set(mp->value); + metatable->pairs.push_back(newPair); + break; + } default: YUEE("AST node mismatch", pair); break; } - temp.back() = indent() + temp.back() + (pair == pairs.back() ? Empty : s(","sv)) + nll(pair); + if (!isMetamethod) { + temp.back() = indent() + temp.back() + (pair == pairs.back() ? Empty : s(","sv)) + nll(pair); + } + } + if (metatable->pairs.empty()) { + out.push_back(s("{"sv) + nll(table) + join(temp)); + decIndentOffset(); + out.back() += (indent() + s("}"sv)); + } else { + auto tabStr = globalVar("setmetatable"sv, table); + tabStr += '('; + if (temp.empty()) { + decIndentOffset(); + tabStr += "{ }"sv; + } else { + tabStr += (s("{"sv) + nll(table) + join(temp)); + decIndentOffset(); + tabStr += (indent() + s("}"sv)); + } + tabStr += ", "sv; + str_list tmp; + transform_simple_table(metatable, tmp); + tabStr += tmp.back(); + tabStr += s(")"sv); + out.push_back(tabStr); } - out.push_back(s("{"sv) + nll(table) + join(temp)); - decIndentOffset(); - out.back() += (indent() + s("}"sv)); } void transform_simple_table(simple_table_t* table, str_list& out) { @@ -5236,6 +5463,8 @@ private: break; case id(): case id(): + case id(): + case id(): case id(): newTab->items.push_back(item); break; diff --git a/src/yuescript/yue_parser.cpp b/src/yuescript/yue_parser.cpp index 7cf2ebe..c72c005 100644 --- a/src/yuescript/yue_parser.cpp +++ b/src/yuescript/yue_parser.cpp @@ -412,6 +412,9 @@ YueParser::YueParser() { FnArgs = (symx('(') >> *SpaceBreak >> -FnArgsExpList >> *SpaceBreak >> sym(')')) | (sym('!') >> not_(expr('='))); + Metatable = expr('#'); + Metamethod = Name >> expr('#'); + existential_op = expr('?'); chain_call = (Callable | String) >> -existential_op >> ChainItems; chain_item = and_(set(".\\")) >> ChainItems; @@ -427,8 +430,8 @@ YueParser::YueParser() { Index = symx('[') >> Exp >> sym(']'); ChainItem = Invoke >> -existential_op | DotChainItem >> -existential_op | Slice | Index >> -existential_op; - DotChainItem = symx('.') >> Name; - ColonChainItem = symx('\\') >> (LuaKeyword | Name); + DotChainItem = symx('.') >> (Name >> not_('#') | Metatable | Metamethod); + ColonChainItem = symx('\\') >> ((LuaKeyword | Name) >> not_('#') | Metamethod); invoke_chain = Invoke >> -existential_op >> -ChainItems; ColonChain = ColonChainItem >> -existential_op >> -invoke_chain; @@ -508,7 +511,7 @@ YueParser::YueParser() { }) >> ExpList >> -Assign) | Macro) >> not_(Space >> statement_appendix); - variable_pair = sym(':') >> Variable; + variable_pair = sym(':') >> Variable >> not_('#'); normal_pair = ( KeyName | @@ -520,7 +523,12 @@ YueParser::YueParser() { symx(':') >> (Exp | TableBlock | +(SpaceBreak) >> Exp); - KeyValue = variable_pair | normal_pair; + meta_variable_pair = sym(':') >> Variable >> expr('#'); + + meta_normal_pair = Space >> Name >> expr("#:") >> + (Exp | TableBlock | +(SpaceBreak) >> Exp); + + KeyValue = variable_pair | normal_pair | meta_variable_pair | meta_normal_pair; KeyValueList = KeyValue >> *(sym(',') >> KeyValue); KeyValueLine = CheckIndent >> (KeyValueList >> -sym(',') | TableBlockIndent | Space >> expr('*') >> (Exp | TableBlock)); diff --git a/src/yuescript/yue_parser.h b/src/yuescript/yue_parser.h index f9a0d54..729304a 100644 --- a/src/yuescript/yue_parser.h +++ b/src/yuescript/yue_parser.h @@ -265,6 +265,8 @@ private: AST_RULE(Parens) AST_RULE(DotChainItem) AST_RULE(ColonChainItem) + AST_RULE(Metatable) + AST_RULE(Metamethod) AST_RULE(default_value) AST_RULE(Slice) AST_RULE(Invoke) @@ -282,6 +284,8 @@ private: AST_RULE(Export) AST_RULE(variable_pair) AST_RULE(normal_pair) + AST_RULE(meta_variable_pair) + AST_RULE(meta_normal_pair) AST_RULE(FnArgDef) AST_RULE(FnArgDefList) AST_RULE(outer_var_shadow) -- cgit v1.2.3-55-g6feb