From 1c40ff9faafed620aa0458b397bcbfbe19e0f663 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 17 Dec 2024 11:23:22 -0300 Subject: Scanner and parser use different tables for constants Moreover, each function being parsed has its own table. The code is cleaner when each table is used for one specific purpose: The scanner uses its table to anchor and unify strings, mapping strings to themselves; the parser uses it to reuse constants in the code, mapping constants to their indices in the constant table. A different table for each task avoids false collisions. --- llex.c | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) (limited to 'llex.c') diff --git a/llex.c b/llex.c index b2e77c9c..d913db17 100644 --- a/llex.c +++ b/llex.c @@ -130,18 +130,15 @@ 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. The table -** here is used as a set: the string enters as the key, while its value -** is irrelevant. We use the string itself as the value only because it -** is a TValue readily available. Later, the code generation can change -** this value. +** ensuring there is only one copy of each unique string. */ TString *luaX_newstring (LexState *ls, const char *str, size_t l) { lua_State *L = ls->L; TString *ts = luaS_newlstr(L, str, l); /* create new string */ - TString *oldts = luaH_getstrkey(ls->h, ts); - if (oldts != NULL) /* string already present? */ - return oldts; /* use it */ + 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 */ @@ -149,8 +146,8 @@ TString *luaX_newstring (LexState *ls, const char *str, size_t l) { /* table is not a metatable, so it does not need to invalidate cache */ luaC_checkGC(L); L->top.p--; /* remove string from stack */ + return ts; } - return ts; } -- cgit v1.2.3-55-g6feb