1
0
mirror of https://github.com/lua/lua.git synced 2025-02-04 06:13:04 +08:00

Scanner doesn't need to anchor reserved words

This commit is contained in:
Roberto Ierusalimschy 2024-12-30 16:53:51 -03:00
parent abf8b1cd4a
commit 5894ca7b95
2 changed files with 20 additions and 12 deletions

30
llex.c
View File

@ -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;
}
}

View File

@ -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)
/*