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)
This commit is contained in:
29
ltable.c
29
ltable.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: ltable.c,v 2.60 2011/06/16 14:14:31 roberto Exp roberto $
|
||||
** $Id: ltable.c,v 2.61 2011/08/09 20:58:29 roberto Exp roberto $
|
||||
** Lua tables (hash)
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -322,8 +322,11 @@ void luaH_resize (lua_State *L, Table *t, int nasize, int nhsize) {
|
||||
/* re-insert elements from hash part */
|
||||
for (i = twoto(oldhsize) - 1; i >= 0; i--) {
|
||||
Node *old = nold+i;
|
||||
if (!ttisnil(gval(old)))
|
||||
if (!ttisnil(gval(old))) {
|
||||
/* doesn't need barrier/invalidate cache, as entry was
|
||||
already present in the table */
|
||||
setobjt2t(L, luaH_set(L, t, gkey(old)), gval(old));
|
||||
}
|
||||
}
|
||||
if (!isdummy(nold))
|
||||
luaM_freearray(L, nold, twoto(oldhsize)); /* free old array */
|
||||
@@ -398,14 +401,18 @@ static Node *getfreepos (Table *t) {
|
||||
** put new key in its main position; otherwise (colliding node is in its main
|
||||
** position), new key goes to an empty position.
|
||||
*/
|
||||
static TValue *newkey (lua_State *L, Table *t, const TValue *key) {
|
||||
TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) {
|
||||
Node *mp = mainposition(t, key);
|
||||
if (ttisnil(key)) luaG_runerror(L, "table index is nil");
|
||||
else if (ttisnumber(key) && luai_numisnan(L, nvalue(key)))
|
||||
luaG_runerror(L, "table index is NaN");
|
||||
if (!ttisnil(gval(mp)) || isdummy(mp)) { /* main position is taken? */
|
||||
Node *othern;
|
||||
Node *n = getfreepos(t); /* get a free place */
|
||||
if (n == NULL) { /* cannot find a free place? */
|
||||
rehash(L, t, key); /* grow table */
|
||||
return luaH_set(L, t, key); /* re-insert key into grown table */
|
||||
/* whatever called 'newkey' take care of TM cache and GC barrier */
|
||||
return luaH_set(L, t, key); /* insert key into grown table */
|
||||
}
|
||||
lua_assert(!isdummy(n));
|
||||
othern = mainposition(t, gkey(mp));
|
||||
@@ -493,17 +500,15 @@ const TValue *luaH_get (Table *t, const TValue *key) {
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** beware: when using this function you probably need to check a GC
|
||||
** barrier and invalidate the TM cache.
|
||||
*/
|
||||
TValue *luaH_set (lua_State *L, Table *t, const TValue *key) {
|
||||
const TValue *p = luaH_get(t, key);
|
||||
t->flags = 0;
|
||||
if (p != luaO_nilobject)
|
||||
return cast(TValue *, p);
|
||||
else {
|
||||
if (ttisnil(key)) luaG_runerror(L, "table index is nil");
|
||||
else if (ttisnumber(key) && luai_numisnan(L, nvalue(key)))
|
||||
luaG_runerror(L, "table index is NaN");
|
||||
return newkey(L, t, key);
|
||||
}
|
||||
else return luaH_newkey(L, t, key);
|
||||
}
|
||||
|
||||
|
||||
@@ -515,7 +520,7 @@ void luaH_setint (lua_State *L, Table *t, int key, TValue *value) {
|
||||
else {
|
||||
TValue k;
|
||||
setnvalue(&k, cast_num(key));
|
||||
cell = newkey(L, t, &k);
|
||||
cell = luaH_newkey(L, t, &k);
|
||||
}
|
||||
setobj2t(L, cell, value);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user