aboutsummaryrefslogtreecommitdiff
path: root/llex.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2024-12-17 11:23:22 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2024-12-17 11:23:22 -0300
commit1c40ff9faafed620aa0458b397bcbfbe19e0f663 (patch)
treeedfb96022feb99244a7a6c4c9d28525e141b783e /llex.c
parent7538f3886dfa091d661c56e48ebb1578ced8e467 (diff)
downloadlua-1c40ff9faafed620aa0458b397bcbfbe19e0f663.tar.gz
lua-1c40ff9faafed620aa0458b397bcbfbe19e0f663.tar.bz2
lua-1c40ff9faafed620aa0458b397bcbfbe19e0f663.zip
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.
Diffstat (limited to 'llex.c')
-rw-r--r--llex.c15
1 files changed, 6 insertions, 9 deletions
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) {
130** Creates a new string and anchors it in scanner's table so that it 130** Creates a new string and anchors it in scanner's table so that it
131** will not be collected until the end of the compilation; by that time 131** will not be collected until the end of the compilation; by that time
132** it should be anchored somewhere. It also internalizes long strings, 132** it should be anchored somewhere. It also internalizes long strings,
133** ensuring there is only one copy of each unique string. The table 133** ensuring there is only one copy of each unique string.
134** here is used as a set: the string enters as the key, while its value
135** is irrelevant. We use the string itself as the value only because it
136** is a TValue readily available. Later, the code generation can change
137** this value.
138*/ 134*/
139TString *luaX_newstring (LexState *ls, const char *str, size_t l) { 135TString *luaX_newstring (LexState *ls, const char *str, size_t l) {
140 lua_State *L = ls->L; 136 lua_State *L = ls->L;
141 TString *ts = luaS_newlstr(L, str, l); /* create new string */ 137 TString *ts = luaS_newlstr(L, str, l); /* create new string */
142 TString *oldts = luaH_getstrkey(ls->h, ts); 138 TValue oldts;
143 if (oldts != NULL) /* string already present? */ 139 int tag = luaH_getstr(ls->h, ts, &oldts);
144 return oldts; /* use it */ 140 if (!tagisempty(tag)) /* string already present? */
141 return tsvalue(&oldts); /* use stored value */
145 else { /* create a new entry */ 142 else { /* create a new entry */
146 TValue *stv = s2v(L->top.p++); /* reserve stack space for string */ 143 TValue *stv = s2v(L->top.p++); /* reserve stack space for string */
147 setsvalue(L, stv, ts); /* temporarily anchor the string */ 144 setsvalue(L, stv, ts); /* temporarily anchor the string */
@@ -149,8 +146,8 @@ TString *luaX_newstring (LexState *ls, const char *str, size_t l) {
149 /* table is not a metatable, so it does not need to invalidate cache */ 146 /* table is not a metatable, so it does not need to invalidate cache */
150 luaC_checkGC(L); 147 luaC_checkGC(L);
151 L->top.p--; /* remove string from stack */ 148 L->top.p--; /* remove string from stack */
149 return ts;
152 } 150 }
153 return ts;
154} 151}
155 152
156 153