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 | |
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)
-rw-r--r-- | lapi.c | 3 | ||||
-rw-r--r-- | ltable.c | 29 | ||||
-rw-r--r-- | ltable.h | 5 | ||||
-rw-r--r-- | lvm.c | 33 |
4 files changed, 44 insertions, 26 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lapi.c,v 2.149 2011/06/13 14:13:06 roberto Exp roberto $ | 2 | ** $Id: lapi.c,v 2.150 2011/08/09 20:58:29 roberto Exp roberto $ |
3 | ** Lua API | 3 | ** Lua API |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -732,6 +732,7 @@ LUA_API void lua_rawset (lua_State *L, int idx) { | |||
732 | t = index2addr(L, idx); | 732 | t = index2addr(L, idx); |
733 | api_check(L, ttistable(t), "table expected"); | 733 | api_check(L, ttistable(t), "table expected"); |
734 | setobj2t(L, luaH_set(L, hvalue(t), L->top-2), L->top-1); | 734 | setobj2t(L, luaH_set(L, hvalue(t), L->top-2), L->top-1); |
735 | invalidateTMcache(hvalue(t)); | ||
735 | luaC_barrierback(L, gcvalue(t), L->top-1); | 736 | luaC_barrierback(L, gcvalue(t), L->top-1); |
736 | L->top -= 2; | 737 | L->top -= 2; |
737 | lua_unlock(L); | 738 | lua_unlock(L); |
@@ -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 | } |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: ltable.h,v 2.14 2010/06/25 12:18:10 roberto Exp roberto $ | 2 | ** $Id: ltable.h,v 2.15 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 | */ |
@@ -15,11 +15,14 @@ | |||
15 | #define gval(n) (&(n)->i_val) | 15 | #define gval(n) (&(n)->i_val) |
16 | #define gnext(n) ((n)->i_key.nk.next) | 16 | #define gnext(n) ((n)->i_key.nk.next) |
17 | 17 | ||
18 | #define invalidateTMcache(t) ((t)->flags = 0) | ||
19 | |||
18 | 20 | ||
19 | LUAI_FUNC const TValue *luaH_getint (Table *t, int key); | 21 | LUAI_FUNC const TValue *luaH_getint (Table *t, int key); |
20 | LUAI_FUNC void luaH_setint (lua_State *L, Table *t, int key, TValue *value); | 22 | LUAI_FUNC void luaH_setint (lua_State *L, Table *t, int key, TValue *value); |
21 | LUAI_FUNC const TValue *luaH_getstr (Table *t, TString *key); | 23 | LUAI_FUNC const TValue *luaH_getstr (Table *t, TString *key); |
22 | LUAI_FUNC const TValue *luaH_get (Table *t, const TValue *key); | 24 | LUAI_FUNC const TValue *luaH_get (Table *t, const TValue *key); |
25 | LUAI_FUNC TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key); | ||
23 | LUAI_FUNC TValue *luaH_set (lua_State *L, Table *t, const TValue *key); | 26 | LUAI_FUNC TValue *luaH_set (lua_State *L, Table *t, const TValue *key); |
24 | LUAI_FUNC Table *luaH_new (lua_State *L); | 27 | LUAI_FUNC Table *luaH_new (lua_State *L); |
25 | LUAI_FUNC void luaH_resize (lua_State *L, Table *t, int nasize, int nhsize); | 28 | LUAI_FUNC void luaH_resize (lua_State *L, Table *t, int nasize, int nhsize); |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lvm.c,v 2.141 2011/06/09 18:24:22 roberto Exp roberto $ | 2 | ** $Id: lvm.c,v 2.142 2011/08/09 20:58:29 roberto Exp roberto $ |
3 | ** Lua virtual machine | 3 | ** Lua virtual machine |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -127,29 +127,38 @@ void luaV_gettable (lua_State *L, const TValue *t, TValue *key, StkId val) { | |||
127 | 127 | ||
128 | void luaV_settable (lua_State *L, const TValue *t, TValue *key, StkId val) { | 128 | void luaV_settable (lua_State *L, const TValue *t, TValue *key, StkId val) { |
129 | int loop; | 129 | int loop; |
130 | TValue temp; | ||
131 | for (loop = 0; loop < MAXTAGLOOP; loop++) { | 130 | for (loop = 0; loop < MAXTAGLOOP; loop++) { |
132 | const TValue *tm; | 131 | const TValue *tm; |
133 | if (ttistable(t)) { /* `t' is a table? */ | 132 | if (ttistable(t)) { /* `t' is a table? */ |
134 | Table *h = hvalue(t); | 133 | Table *h = hvalue(t); |
135 | TValue *oldval = luaH_set(L, h, key); /* do a primitive set */ | 134 | TValue *oldval = cast(TValue *, luaH_get(h, key)); |
136 | if (!ttisnil(oldval) || /* result is not nil? */ | 135 | /* if previous value is not nil, there must be a previous entry |
137 | (tm = fasttm(L, h->metatable, TM_NEWINDEX)) == NULL) { /* or no TM? */ | 136 | in the table; moreover, a metamethod has no relevance */ |
138 | setobj2t(L, oldval, val); | 137 | if (!ttisnil(oldval) || |
138 | /* previous value is nil; must check the metamethod */ | ||
139 | ((tm = fasttm(L, h->metatable, TM_NEWINDEX)) == NULL && | ||
140 | /* no metamethod; is there a previous entry in the table? */ | ||
141 | (oldval != luaO_nilobject || | ||
142 | /* no previous entry; must create one. (The next test is | ||
143 | always true; we only need the assignment.) */ | ||
144 | (oldval = luaH_newkey(L, h, key), 1)))) { | ||
145 | /* no metamethod and (now) there is an entry with given key */ | ||
146 | setobj2t(L, oldval, val); /* assign new value to that entry */ | ||
147 | invalidateTMcache(h); | ||
139 | luaC_barrierback(L, obj2gco(h), val); | 148 | luaC_barrierback(L, obj2gco(h), val); |
140 | return; | 149 | return; |
141 | } | 150 | } |
142 | /* else will try the tag method */ | 151 | /* else will try the metamethod */ |
143 | } | 152 | } |
144 | else if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_NEWINDEX))) | 153 | else /* not a table; check metamethod */ |
145 | luaG_typeerror(L, t, "index"); | 154 | if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_NEWINDEX))) |
155 | luaG_typeerror(L, t, "index"); | ||
156 | /* there is a metamethod */ | ||
146 | if (ttisfunction(tm)) { | 157 | if (ttisfunction(tm)) { |
147 | callTM(L, tm, t, key, val, 0); | 158 | callTM(L, tm, t, key, val, 0); |
148 | return; | 159 | return; |
149 | } | 160 | } |
150 | /* else repeat with 'tm' */ | 161 | t = tm; /* else repeat with 'tm' */ |
151 | setobj(L, &temp, tm); /* avoid pointing inside table (may rehash) */ | ||
152 | t = &temp; | ||
153 | } | 162 | } |
154 | luaG_runerror(L, "loop in settable"); | 163 | luaG_runerror(L, "loop in settable"); |
155 | } | 164 | } |