@@ -172,11 +172,17 @@ static Node *mainpositionTV (const Table *t, const TValue *key) {
172172** be equal to floats. It is assumed that 'eqshrstr' is simply
173173** pointer equality, so that short strings are handled in the
174174** default case.
175- */
176- static int equalkey (const TValue * k1 , const Node * n2 ) {
177- if (rawtt (k1 ) != keytt (n2 )) /* not the same variants? */
175+ ** A true 'deadok' means to accept dead keys as equal to their original
176+ ** values, which can only happen if the original key was collectable.
177+ ** All dead values are compared in the default case, by pointer
178+ ** identity. (Note that dead long strings are also compared by
179+ ** identity).
180+ */
181+ static int equalkey (const TValue * k1 , const Node * n2 , int deadok ) {
182+ if ((rawtt (k1 ) != keytt (n2 )) && /* not the same variants? */
183+ !(deadok && keyisdead (n2 ) && iscollectable (k1 )))
178184 return 0 ; /* cannot be same key */
179- switch (ttypetag ( k1 )) {
185+ switch (keytt ( n2 )) {
180186 case LUA_VNIL : case LUA_VFALSE : case LUA_VTRUE :
181187 return 1 ;
182188 case LUA_VNUMINT :
@@ -187,7 +193,7 @@ static int equalkey (const TValue *k1, const Node *n2) {
187193 return pvalue (k1 ) == pvalueraw (keyval (n2 ));
188194 case LUA_VLCF :
189195 return fvalue (k1 ) == fvalueraw (keyval (n2 ));
190- case LUA_VLNGSTR :
196+ case ctb ( LUA_VLNGSTR ) :
191197 return luaS_eqlngstr (tsvalue (k1 ), keystrval (n2 ));
192198 default :
193199 return gcvalue (k1 ) == gcvalueraw (keyval (n2 ));
@@ -251,11 +257,12 @@ static unsigned int setlimittosize (Table *t) {
251257/*
252258** "Generic" get version. (Not that generic: not valid for integers,
253259** which may be in array part, nor for floats with integral values.)
260+ ** See explanation about 'deadok' in function 'equalkey'.
254261*/
255- static const TValue * getgeneric (Table * t , const TValue * key ) {
262+ static const TValue * getgeneric (Table * t , const TValue * key , int deadok ) {
256263 Node * n = mainpositionTV (t , key );
257264 for (;;) { /* check whether 'key' is somewhere in the chain */
258- if (equalkey (key , n ))
265+ if (equalkey (key , n , deadok ))
259266 return gval (n ); /* that's it */
260267 else {
261268 int nx = gnext (n );
@@ -292,7 +299,7 @@ static unsigned int findindex (lua_State *L, Table *t, TValue *key,
292299 if (i - 1u < asize ) /* is 'key' inside array part? */
293300 return i ; /* yes; that's the index */
294301 else {
295- const TValue * n = getgeneric (t , key );
302+ const TValue * n = getgeneric (t , key , 1 );
296303 if (unlikely (isabstkey (n )))
297304 luaG_runerror (L , "invalid key to 'next'" ); /* key not found */
298305 i = cast_int (nodefromval (n ) - gnode (t , 0 )); /* key index in hash table */
@@ -730,7 +737,7 @@ const TValue *luaH_getstr (Table *t, TString *key) {
730737 else { /* for long strings, use generic case */
731738 TValue ko ;
732739 setsvalue (cast (lua_State * , NULL ), & ko , key );
733- return getgeneric (t , & ko );
740+ return getgeneric (t , & ko , 0 );
734741 }
735742}
736743
@@ -750,7 +757,7 @@ const TValue *luaH_get (Table *t, const TValue *key) {
750757 /* else... */
751758 } /* FALLTHROUGH */
752759 default :
753- return getgeneric (t , key );
760+ return getgeneric (t , key , 0 );
754761 }
755762}
756763
0 commit comments