aboutsummaryrefslogtreecommitdiff
path: root/llex.c
diff options
context:
space:
mode:
Diffstat (limited to 'llex.c')
-rw-r--r--llex.c31
1 files changed, 17 insertions, 14 deletions
diff --git a/llex.c b/llex.c
index 4b8dec99..e9915178 100644
--- a/llex.c
+++ b/llex.c
@@ -122,26 +122,29 @@ l_noret luaX_syntaxerror (LexState *ls, const char *msg) {
122 122
123 123
124/* 124/*
125** creates a new string and anchors it in scanner's table so that 125** Creates a new string and anchors it in scanner's table so that it
126** it will not be collected until the end of the compilation 126** will not be collected until the end of the compilation; by that time
127** (by that time it should be anchored somewhere) 127** it should be anchored somewhere. It also internalizes long strings,
128** ensuring there is only one copy of each unique string. The table
129** here is used as a set: the string enters as the key, while its value
130** is irrelevant. We use the string itself as the value only because it
131** is a TValue readly available. Later, the code generation can change
132** this value.
128*/ 133*/
129TString *luaX_newstring (LexState *ls, const char *str, size_t l) { 134TString *luaX_newstring (LexState *ls, const char *str, size_t l) {
130 lua_State *L = ls->L; 135 lua_State *L = ls->L;
131 TValue *o; /* entry for 'str' */
132 TString *ts = luaS_newlstr(L, str, l); /* create new string */ 136 TString *ts = luaS_newlstr(L, str, l); /* create new string */
133 setsvalue2s(L, L->top++, ts); /* temporarily anchor it in stack */ 137 const TValue *o = luaH_getstr(ls->h, ts);
134 o = luaH_set(L, ls->h, s2v(L->top - 1)); 138 if (!ttisnil(o)) /* string already present? */
135 if (isempty(o)) { /* not in use yet? */ 139 ts = keystrval(nodefromval(o)); /* get saved copy */
136 /* boolean value does not need GC barrier; 140 else { /* not in use yet */
137 table is not a metatable, so it does not need to invalidate cache */ 141 TValue *stv = s2v(L->top++); /* reserve stack space for string */
138 setbtvalue(o); /* t[string] = true */ 142 setsvalue(L, stv, ts); /* temporarily anchor the string */
143 luaH_finishset(L, ls->h, stv, o, stv); /* t[string] = string */
144 /* table is not a metatable, so it does not need to invalidate cache */
139 luaC_checkGC(L); 145 luaC_checkGC(L);
146 L->top--; /* remove string from stack */
140 } 147 }
141 else { /* string already present */
142 ts = keystrval(nodefromval(o)); /* re-use value previously stored */
143 }
144 L->top--; /* remove string from stack */
145 return ts; 148 return ts;
146} 149}
147 150