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 /lvm.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 'lvm.c')
-rw-r--r-- | lvm.c | 33 |
1 files changed, 21 insertions, 12 deletions
@@ -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 | } |