From 5894ca7b95d7fb05f1e93ee77e849a8d816d1c6d Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 30 Dec 2024 16:53:51 -0300 Subject: [PATCH] Scanner doesn't need to anchor reserved words --- llex.c | 30 +++++++++++++++++++----------- lstring.h | 2 +- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/llex.c b/llex.c index d913db17..3518f0da 100644 --- a/llex.c +++ b/llex.c @@ -127,21 +127,20 @@ l_noret luaX_syntaxerror (LexState *ls, const char *msg) { /* -** Creates a new string and anchors it in scanner's table so that it -** will not be collected until the end of the compilation; by that time -** it should be anchored somewhere. It also internalizes long strings, -** ensuring there is only one copy of each unique string. +** Anchors a string in scanner's table so that it will not be collected +** until the end of the compilation; by that time it should be anchored +** somewhere. It also internalizes long strings, ensuring there is only +** one copy of each unique string. */ -TString *luaX_newstring (LexState *ls, const char *str, size_t l) { +static TString *anchorstr (LexState *ls, TString *ts) { lua_State *L = ls->L; - TString *ts = luaS_newlstr(L, str, l); /* create new string */ TValue oldts; int tag = luaH_getstr(ls->h, ts, &oldts); if (!tagisempty(tag)) /* string already present? */ return tsvalue(&oldts); /* use stored value */ else { /* create a new entry */ TValue *stv = s2v(L->top.p++); /* reserve stack space for string */ - setsvalue(L, stv, ts); /* temporarily anchor the string */ + setsvalue(L, stv, ts); /* push (anchor) the string on the stack */ luaH_set(L, ls->h, stv, stv); /* t[string] = string */ /* table is not a metatable, so it does not need to invalidate cache */ luaC_checkGC(L); @@ -151,6 +150,14 @@ TString *luaX_newstring (LexState *ls, const char *str, size_t l) { } +/* +** Creates a new string and anchors it in scanner's table. +*/ +TString *luaX_newstring (LexState *ls, const char *str, size_t l) { + return anchorstr(ls, luaS_newlstr(ls->L, str, l)); +} + + /* ** increment line number and skips newline sequence (any of ** \n, \r, \n\r, or \r\n) @@ -544,12 +551,13 @@ static int llex (LexState *ls, SemInfo *seminfo) { do { save_and_next(ls); } while (lislalnum(ls->current)); - ts = luaX_newstring(ls, luaZ_buffer(ls->buff), - luaZ_bufflen(ls->buff)); - seminfo->ts = ts; - if (isreserved(ts)) /* reserved word? */ + /* find or create string */ + ts = luaS_newlstr(ls->L, luaZ_buffer(ls->buff), + luaZ_bufflen(ls->buff)); + if (isreserved(ts)) /* reserved word? */ return ts->extra - 1 + FIRST_RESERVED; else { + seminfo->ts = anchorstr(ls, ts); return TK_NAME; } } diff --git a/lstring.h b/lstring.h index 26f4b8e1..1751e043 100644 --- a/lstring.h +++ b/lstring.h @@ -45,7 +45,7 @@ /* ** test whether a string is a reserved word */ -#define isreserved(s) ((s)->tt == LUA_VSHRSTR && (s)->extra > 0) +#define isreserved(s) (strisshr(s) && (s)->extra > 0) /*