diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2024-12-30 16:53:51 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2024-12-30 16:53:51 -0300 |
commit | 5894ca7b95d7fb05f1e93ee77e849a8d816d1c6d (patch) | |
tree | f73f22e8661399b0aa4a115671c08c240f745e0b /llex.c | |
parent | abf8b1cd4a798fada026b4046e9dbc08791963f2 (diff) | |
download | lua-5894ca7b95d7fb05f1e93ee77e849a8d816d1c6d.tar.gz lua-5894ca7b95d7fb05f1e93ee77e849a8d816d1c6d.tar.bz2 lua-5894ca7b95d7fb05f1e93ee77e849a8d816d1c6d.zip |
Scanner doesn't need to anchor reserved words
Diffstat (limited to 'llex.c')
-rw-r--r-- | llex.c | 30 |
1 files changed, 19 insertions, 11 deletions
@@ -127,21 +127,20 @@ l_noret luaX_syntaxerror (LexState *ls, const char *msg) { | |||
127 | 127 | ||
128 | 128 | ||
129 | /* | 129 | /* |
130 | ** Creates a new string and anchors it in scanner's table so that it | 130 | ** Anchors a string in scanner's table so that it will not be collected |
131 | ** will not be collected until the end of the compilation; by that time | 131 | ** until the end of the compilation; by that time it should be anchored |
132 | ** it should be anchored somewhere. It also internalizes long strings, | 132 | ** somewhere. It also internalizes long strings, ensuring there is only |
133 | ** ensuring there is only one copy of each unique string. | 133 | ** one copy of each unique string. |
134 | */ | 134 | */ |
135 | TString *luaX_newstring (LexState *ls, const char *str, size_t l) { | 135 | static TString *anchorstr (LexState *ls, TString *ts) { |
136 | lua_State *L = ls->L; | 136 | lua_State *L = ls->L; |
137 | TString *ts = luaS_newlstr(L, str, l); /* create new string */ | ||
138 | TValue oldts; | 137 | TValue oldts; |
139 | int tag = luaH_getstr(ls->h, ts, &oldts); | 138 | int tag = luaH_getstr(ls->h, ts, &oldts); |
140 | if (!tagisempty(tag)) /* string already present? */ | 139 | if (!tagisempty(tag)) /* string already present? */ |
141 | return tsvalue(&oldts); /* use stored value */ | 140 | return tsvalue(&oldts); /* use stored value */ |
142 | else { /* create a new entry */ | 141 | else { /* create a new entry */ |
143 | TValue *stv = s2v(L->top.p++); /* reserve stack space for string */ | 142 | TValue *stv = s2v(L->top.p++); /* reserve stack space for string */ |
144 | setsvalue(L, stv, ts); /* temporarily anchor the string */ | 143 | setsvalue(L, stv, ts); /* push (anchor) the string on the stack */ |
145 | luaH_set(L, ls->h, stv, stv); /* t[string] = string */ | 144 | luaH_set(L, ls->h, stv, stv); /* t[string] = string */ |
146 | /* table is not a metatable, so it does not need to invalidate cache */ | 145 | /* table is not a metatable, so it does not need to invalidate cache */ |
147 | luaC_checkGC(L); | 146 | luaC_checkGC(L); |
@@ -152,6 +151,14 @@ TString *luaX_newstring (LexState *ls, const char *str, size_t l) { | |||
152 | 151 | ||
153 | 152 | ||
154 | /* | 153 | /* |
154 | ** Creates a new string and anchors it in scanner's table. | ||
155 | */ | ||
156 | TString *luaX_newstring (LexState *ls, const char *str, size_t l) { | ||
157 | return anchorstr(ls, luaS_newlstr(ls->L, str, l)); | ||
158 | } | ||
159 | |||
160 | |||
161 | /* | ||
155 | ** increment line number and skips newline sequence (any of | 162 | ** increment line number and skips newline sequence (any of |
156 | ** \n, \r, \n\r, or \r\n) | 163 | ** \n, \r, \n\r, or \r\n) |
157 | */ | 164 | */ |
@@ -544,12 +551,13 @@ static int llex (LexState *ls, SemInfo *seminfo) { | |||
544 | do { | 551 | do { |
545 | save_and_next(ls); | 552 | save_and_next(ls); |
546 | } while (lislalnum(ls->current)); | 553 | } while (lislalnum(ls->current)); |
547 | ts = luaX_newstring(ls, luaZ_buffer(ls->buff), | 554 | /* find or create string */ |
548 | luaZ_bufflen(ls->buff)); | 555 | ts = luaS_newlstr(ls->L, luaZ_buffer(ls->buff), |
549 | seminfo->ts = ts; | 556 | luaZ_bufflen(ls->buff)); |
550 | if (isreserved(ts)) /* reserved word? */ | 557 | if (isreserved(ts)) /* reserved word? */ |
551 | return ts->extra - 1 + FIRST_RESERVED; | 558 | return ts->extra - 1 + FIRST_RESERVED; |
552 | else { | 559 | else { |
560 | seminfo->ts = anchorstr(ls, ts); | ||
553 | return TK_NAME; | 561 | return TK_NAME; |
554 | } | 562 | } |
555 | } | 563 | } |