From ded2ad2d86f44424c6b6e12bf1b75836cfa9e502 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 16 May 2025 14:51:07 -0300 Subject: [PATCH] Slightly faster way to check for "global" --- llex.c | 20 ++++++++++---------- llex.h | 1 + lparser.c | 4 ++-- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/llex.c b/llex.c index 54e7f343..f8bb3ea4 100644 --- a/llex.c +++ b/llex.c @@ -40,16 +40,11 @@ #define currIsNewline(ls) (ls->current == '\n' || ls->current == '\r') -#if defined(LUA_COMPAT_GLOBAL) -#define GLOBALLEX ".g" /* anything not recognizable as a name */ -#else -#define GLOBALLEX "global" -#endif /* ORDER RESERVED */ static const char *const luaX_tokens [] = { "and", "break", "do", "else", "elseif", - "end", "false", "for", "function", GLOBALLEX, "goto", "if", + "end", "false", "for", "function", "global", "goto", "if", "in", "local", "nil", "not", "or", "repeat", "return", "then", "true", "until", "while", "//", "..", "...", "==", ">=", "<=", "~=", @@ -189,10 +184,15 @@ void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, TString *source, ls->linenumber = 1; ls->lastline = 1; ls->source = source; - ls->envn = luaS_newliteral(L, LUA_ENV); /* get env name */ - ls->brkn = luaS_newliteral(L, "break"); /* get "break" name */ - /* "break" cannot be collected, as it is a reserved word" */ - lua_assert(isreserved(ls->brkn)); + /* all three strings here ("_ENV", "break", "global") were fixed, + so they cannot be collected */ + ls->envn = luaS_newliteral(L, LUA_ENV); /* get env string */ + ls->brkn = luaS_newliteral(L, "break"); /* get "break" string */ +#if defined(LUA_COMPAT_GLOBAL) + /* compatibility mode: "global" is not a reserved word */ + ls->glbn = luaS_newliteral(L, "global"); /* get "global" string */ + ls->glbn->extra = 0; /* mark it as not reserved */ +#endif luaZ_resizebuffer(ls->L, ls->buff, LUA_MINBUFFER); /* initialize buffer */ } diff --git a/llex.h b/llex.h index 0dba9d60..37016e8a 100644 --- a/llex.h +++ b/llex.h @@ -76,6 +76,7 @@ typedef struct LexState { TString *source; /* current source name */ TString *envn; /* environment variable name */ TString *brkn; /* "break" name (used as a label) */ + TString *glbn; /* "global" name (when not a reserved word) */ } LexState; diff --git a/lparser.c b/lparser.c index 29022bfd..384ef690 100644 --- a/lparser.c +++ b/lparser.c @@ -2001,10 +2001,10 @@ static void statement (LexState *ls) { case TK_NAME: { /* compatibility code to parse global keyword when "global" is not reserved */ - if (strcmp(getstr(ls->t.seminfo.ts), "global") == 0) { + if (ls->t.seminfo.ts == ls->glbn) { /* current = "global"? */ int lk = luaX_lookahead(ls); if (lk == TK_NAME || lk == '*' || lk == TK_FUNCTION) { - /* 'global ' or 'global *' or 'global function' */ + /* 'global name' or 'global *' or 'global function' */ globalstatfunc(ls, line); break; }