** 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 t
| 132 | ** this value. |
| 133 | */ |
| 134 | TString *luaX_newstring (LexState *ls, const char *str, size_t l) { |
| 135 | lua_State *L = ls->L; |
| 136 | TString *ts = luaS_newlstr(L, str, l); /* create new string */ |
| 137 | const TValue *o = luaH_getstr(ls->h, ts); |
| 138 | if (!ttisnil(o)) /* string already present? */ |
| 139 | ts = keystrval(nodefromval(o)); /* get saved copy */ |
| 140 | else { /* not in use yet */ |
| 141 | TValue *stv = s2v(L->top.p++); /* reserve stack space for string */ |
| 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 */ |
| 145 | luaC_checkGC(L); |
| 146 | L->top.p--; /* remove string from stack */ |
| 147 | } |
| 148 | return ts; |
| 149 | } |
| 150 | |
| 151 | |
| 152 | /* |
no test coverage detected