diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2011-08-17 17:26:47 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2011-08-17 17:26:47 -0300 |
| commit | 89b59eee7386379e87200a86215fc6de91e49036 (patch) | |
| tree | 7c82d2f7f98680787e3b235d94defef00e87133a /ltable.c | |
| parent | 166dd0261a0fc012808a6448a1c46030c8db4806 (diff) | |
| download | lua-89b59eee7386379e87200a86215fc6de91e49036.tar.gz lua-89b59eee7386379e87200a86215fc6de91e49036.tar.bz2 lua-89b59eee7386379e87200a86215fc6de91e49036.zip | |
bug: __newindex metamethod may not work if metatable is its own
metatable + luaV_settable does not create entry when there is a
metamethod (and therefore entry is useless)
Diffstat (limited to 'ltable.c')
| -rw-r--r-- | ltable.c | 29 |
1 files changed, 17 insertions, 12 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: ltable.c,v 2.60 2011/06/16 14:14:31 roberto Exp roberto $ | 2 | ** $Id: ltable.c,v 2.61 2011/08/09 20:58:29 roberto Exp roberto $ |
| 3 | ** Lua tables (hash) | 3 | ** Lua tables (hash) |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -322,8 +322,11 @@ void luaH_resize (lua_State *L, Table *t, int nasize, int nhsize) { | |||
| 322 | /* re-insert elements from hash part */ | 322 | /* re-insert elements from hash part */ |
| 323 | for (i = twoto(oldhsize) - 1; i >= 0; i--) { | 323 | for (i = twoto(oldhsize) - 1; i >= 0; i--) { |
| 324 | Node *old = nold+i; | 324 | Node *old = nold+i; |
| 325 | if (!ttisnil(gval(old))) | 325 | if (!ttisnil(gval(old))) { |
| 326 | /* doesn't need barrier/invalidate cache, as entry was | ||
| 327 | already present in the table */ | ||
| 326 | setobjt2t(L, luaH_set(L, t, gkey(old)), gval(old)); | 328 | setobjt2t(L, luaH_set(L, t, gkey(old)), gval(old)); |
| 329 | } | ||
| 327 | } | 330 | } |
| 328 | if (!isdummy(nold)) | 331 | if (!isdummy(nold)) |
| 329 | luaM_freearray(L, nold, twoto(oldhsize)); /* free old array */ | 332 | luaM_freearray(L, nold, twoto(oldhsize)); /* free old array */ |
| @@ -398,14 +401,18 @@ static Node *getfreepos (Table *t) { | |||
| 398 | ** put new key in its main position; otherwise (colliding node is in its main | 401 | ** put new key in its main position; otherwise (colliding node is in its main |
| 399 | ** position), new key goes to an empty position. | 402 | ** position), new key goes to an empty position. |
| 400 | */ | 403 | */ |
| 401 | static TValue *newkey (lua_State *L, Table *t, const TValue *key) { | 404 | TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) { |
| 402 | Node *mp = mainposition(t, key); | 405 | Node *mp = mainposition(t, key); |
| 406 | if (ttisnil(key)) luaG_runerror(L, "table index is nil"); | ||
| 407 | else if (ttisnumber(key) && luai_numisnan(L, nvalue(key))) | ||
| 408 | luaG_runerror(L, "table index is NaN"); | ||
| 403 | if (!ttisnil(gval(mp)) || isdummy(mp)) { /* main position is taken? */ | 409 | if (!ttisnil(gval(mp)) || isdummy(mp)) { /* main position is taken? */ |
| 404 | Node *othern; | 410 | Node *othern; |
| 405 | Node *n = getfreepos(t); /* get a free place */ | 411 | Node *n = getfreepos(t); /* get a free place */ |
| 406 | if (n == NULL) { /* cannot find a free place? */ | 412 | if (n == NULL) { /* cannot find a free place? */ |
| 407 | rehash(L, t, key); /* grow table */ | 413 | rehash(L, t, key); /* grow table */ |
| 408 | return luaH_set(L, t, key); /* re-insert key into grown table */ | 414 | /* whatever called 'newkey' take care of TM cache and GC barrier */ |
| 415 | return luaH_set(L, t, key); /* insert key into grown table */ | ||
| 409 | } | 416 | } |
| 410 | lua_assert(!isdummy(n)); | 417 | lua_assert(!isdummy(n)); |
| 411 | othern = mainposition(t, gkey(mp)); | 418 | othern = mainposition(t, gkey(mp)); |
| @@ -493,17 +500,15 @@ const TValue *luaH_get (Table *t, const TValue *key) { | |||
| 493 | } | 500 | } |
| 494 | 501 | ||
| 495 | 502 | ||
| 503 | /* | ||
| 504 | ** beware: when using this function you probably need to check a GC | ||
| 505 | ** barrier and invalidate the TM cache. | ||
| 506 | */ | ||
| 496 | TValue *luaH_set (lua_State *L, Table *t, const TValue *key) { | 507 | TValue *luaH_set (lua_State *L, Table *t, const TValue *key) { |
| 497 | const TValue *p = luaH_get(t, key); | 508 | const TValue *p = luaH_get(t, key); |
| 498 | t->flags = 0; | ||
| 499 | if (p != luaO_nilobject) | 509 | if (p != luaO_nilobject) |
| 500 | return cast(TValue *, p); | 510 | return cast(TValue *, p); |
| 501 | else { | 511 | else return luaH_newkey(L, t, key); |
| 502 | if (ttisnil(key)) luaG_runerror(L, "table index is nil"); | ||
| 503 | else if (ttisnumber(key) && luai_numisnan(L, nvalue(key))) | ||
| 504 | luaG_runerror(L, "table index is NaN"); | ||
| 505 | return newkey(L, t, key); | ||
| 506 | } | ||
| 507 | } | 512 | } |
| 508 | 513 | ||
| 509 | 514 | ||
| @@ -515,7 +520,7 @@ void luaH_setint (lua_State *L, Table *t, int key, TValue *value) { | |||
| 515 | else { | 520 | else { |
| 516 | TValue k; | 521 | TValue k; |
| 517 | setnvalue(&k, cast_num(key)); | 522 | setnvalue(&k, cast_num(key)); |
| 518 | cell = newkey(L, t, &k); | 523 | cell = luaH_newkey(L, t, &k); |
| 519 | } | 524 | } |
| 520 | setobj2t(L, cell, value); | 525 | setobj2t(L, cell, value); |
| 521 | } | 526 | } |
