1
0
Fork 0
mirror of https://github.com/lua/lua.git synced 2025-07-24 04:32:35 +00:00

Slightly faster way to check for "global"

This commit is contained in:
Roberto Ierusalimschy 2025-05-16 14:51:07 -03:00
commit ded2ad2d86
3 changed files with 13 additions and 12 deletions

20
llex.c
View file

@ -40,16 +40,11 @@
#define currIsNewline(ls) (ls->current == '\n' || ls->current == '\r') #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 */ /* ORDER RESERVED */
static const char *const luaX_tokens [] = { static const char *const luaX_tokens [] = {
"and", "break", "do", "else", "elseif", "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", "in", "local", "nil", "not", "or", "repeat",
"return", "then", "true", "until", "while", "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->linenumber = 1;
ls->lastline = 1; ls->lastline = 1;
ls->source = source; ls->source = source;
ls->envn = luaS_newliteral(L, LUA_ENV); /* get env name */ /* all three strings here ("_ENV", "break", "global") were fixed,
ls->brkn = luaS_newliteral(L, "break"); /* get "break" name */ so they cannot be collected */
/* "break" cannot be collected, as it is a reserved word" */ ls->envn = luaS_newliteral(L, LUA_ENV); /* get env string */
lua_assert(isreserved(ls->brkn)); 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 */ luaZ_resizebuffer(ls->L, ls->buff, LUA_MINBUFFER); /* initialize buffer */
} }

1
llex.h
View file

@ -76,6 +76,7 @@ typedef struct LexState {
TString *source; /* current source name */ TString *source; /* current source name */
TString *envn; /* environment variable name */ TString *envn; /* environment variable name */
TString *brkn; /* "break" name (used as a label) */ TString *brkn; /* "break" name (used as a label) */
TString *glbn; /* "global" name (when not a reserved word) */
} LexState; } LexState;

View file

@ -2001,10 +2001,10 @@ static void statement (LexState *ls) {
case TK_NAME: { case TK_NAME: {
/* compatibility code to parse global keyword when "global" /* compatibility code to parse global keyword when "global"
is not reserved */ 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); int lk = luaX_lookahead(ls);
if (lk == TK_NAME || lk == '*' || lk == TK_FUNCTION) { if (lk == TK_NAME || lk == '*' || lk == TK_FUNCTION) {
/* 'global <name>' or 'global *' or 'global function' */ /* 'global name' or 'global *' or 'global function' */
globalstatfunc(ls, line); globalstatfunc(ls, line);
break; break;
} }