diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2002-05-27 17:35:40 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2002-05-27 17:35:40 -0300 |
| commit | 46c471d7e97292d923721655683affd7e8b314de (patch) | |
| tree | c9ae53cc94b1cca209fd5af7f594d355d6c111a9 /ltable.c | |
| parent | 859ecf36b6f96a5a0961f09c00cd98088081384c (diff) | |
| download | lua-46c471d7e97292d923721655683affd7e8b314de.tar.gz lua-46c471d7e97292d923721655683affd7e8b314de.tar.bz2 lua-46c471d7e97292d923721655683affd7e8b314de.zip | |
new `__newindex' eventfield
Diffstat (limited to 'ltable.c')
| -rw-r--r-- | ltable.c | 43 |
1 files changed, 23 insertions, 20 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: ltable.c,v 1.107 2002/05/13 13:38:59 roberto Exp roberto $ | 2 | ** $Id: ltable.c,v 1.108 2002/05/15 18:57:44 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 | */ |
| @@ -56,7 +56,7 @@ | |||
| 56 | #define hashboolean(t,p) (node(t, lmod(p, sizenode(t)))) | 56 | #define hashboolean(t,p) (node(t, lmod(p, sizenode(t)))) |
| 57 | 57 | ||
| 58 | /* | 58 | /* |
| 59 | ** for pointers, avoid modulus by power of 2, as they tend to have many | 59 | ** avoid modulus by power of 2 for pointers, as they tend to have many |
| 60 | ** 2 factors. | 60 | ** 2 factors. |
| 61 | */ | 61 | */ |
| 62 | #define hashpointer(t,p) (node(t, (IntPoint(p) % ((sizenode(t)-1)|1)))) | 62 | #define hashpointer(t,p) (node(t, (IntPoint(p) % ((sizenode(t)-1)|1)))) |
| @@ -261,7 +261,7 @@ static void resize (lua_State *L, Table *t, int nasize, int nhsize) { | |||
| 261 | /* re-insert elements from vanishing slice */ | 261 | /* re-insert elements from vanishing slice */ |
| 262 | for (i=nasize; i<oldasize; i++) { | 262 | for (i=nasize; i<oldasize; i++) { |
| 263 | if (ttype(&t->array[i]) != LUA_TNIL) | 263 | if (ttype(&t->array[i]) != LUA_TNIL) |
| 264 | luaH_setnum(L, t, i+1, &t->array[i]); | 264 | setobj(luaH_setnum(L, t, i+1), &t->array[i]); |
| 265 | } | 265 | } |
| 266 | /* shrink array */ | 266 | /* shrink array */ |
| 267 | luaM_reallocvector(L, t->array, oldasize, nasize, TObject); | 267 | luaM_reallocvector(L, t->array, oldasize, nasize, TObject); |
| @@ -270,7 +270,7 @@ static void resize (lua_State *L, Table *t, int nasize, int nhsize) { | |||
| 270 | for (i = twoto(oldhsize) - 1; i >= 0; i--) { | 270 | for (i = twoto(oldhsize) - 1; i >= 0; i--) { |
| 271 | Node *old = nold+i; | 271 | Node *old = nold+i; |
| 272 | if (ttype(val(old)) != LUA_TNIL) | 272 | if (ttype(val(old)) != LUA_TNIL) |
| 273 | luaH_set(L, t, key(old), val(old)); | 273 | setobj(luaH_set(L, t, key(old)), val(old)); |
| 274 | } | 274 | } |
| 275 | if (oldhsize) | 275 | if (oldhsize) |
| 276 | luaM_freearray(L, nold, twoto(oldhsize), Node); /* free old array */ | 276 | luaM_freearray(L, nold, twoto(oldhsize), Node); /* free old array */ |
| @@ -344,8 +344,8 @@ void luaH_remove (Table *t, Node *e) { | |||
| 344 | ** put new key in its main position; otherwise (colliding node is in its main | 344 | ** put new key in its main position; otherwise (colliding node is in its main |
| 345 | ** position), new key goes to an empty position. | 345 | ** position), new key goes to an empty position. |
| 346 | */ | 346 | */ |
| 347 | static void newkey (lua_State *L, Table *t, const TObject *key, | 347 | static TObject *newkey (lua_State *L, Table *t, const TObject *key) { |
| 348 | const TObject *val) { | 348 | TObject *val; |
| 349 | Node *mp = luaH_mainposition(t, key); | 349 | Node *mp = luaH_mainposition(t, key); |
| 350 | if (ttype(val(mp)) != LUA_TNIL) { /* main position is not free? */ | 350 | if (ttype(val(mp)) != LUA_TNIL) { /* main position is not free? */ |
| 351 | Node *othern = luaH_mainposition(t, key(mp)); /* `mp' of colliding node */ | 351 | Node *othern = luaH_mainposition(t, key(mp)); /* `mp' of colliding node */ |
| @@ -367,14 +367,19 @@ static void newkey (lua_State *L, Table *t, const TObject *key, | |||
| 367 | } | 367 | } |
| 368 | setobj(key(mp), key); | 368 | setobj(key(mp), key); |
| 369 | lua_assert(ttype(val(mp)) == LUA_TNIL); | 369 | lua_assert(ttype(val(mp)) == LUA_TNIL); |
| 370 | settableval(val(mp), val); | ||
| 371 | for (;;) { /* correct `firstfree' */ | 370 | for (;;) { /* correct `firstfree' */ |
| 372 | if (ttype(key(t->firstfree)) == LUA_TNIL) | 371 | if (ttype(key(t->firstfree)) == LUA_TNIL) |
| 373 | return; /* OK; table still has a free place */ | 372 | return val(mp); /* OK; table still has a free place */ |
| 374 | else if (t->firstfree == t->node) break; /* cannot decrement from here */ | 373 | else if (t->firstfree == t->node) break; /* cannot decrement from here */ |
| 375 | else (t->firstfree)--; | 374 | else (t->firstfree)--; |
| 376 | } | 375 | } |
| 377 | rehash(L, t); /* no more free places; must create one */ | 376 | /* no more free places; must create one */ |
| 377 | setbvalue(val(mp), 0); /* avoid new key being removed */ | ||
| 378 | rehash(L, t); /* grow table */ | ||
| 379 | val = cast(TObject *, luaH_get(t, key)); /* get new position */ | ||
| 380 | lua_assert(ttype(val) == LUA_TBOOLEAN); | ||
| 381 | setnilvalue(val); | ||
| 382 | return val; | ||
| 378 | } | 383 | } |
| 379 | 384 | ||
| 380 | 385 | ||
| @@ -444,28 +449,26 @@ const TObject *luaH_get (Table *t, const TObject *key) { | |||
| 444 | } | 449 | } |
| 445 | 450 | ||
| 446 | 451 | ||
| 447 | void luaH_set (lua_State *L, Table *t, const TObject *key, const TObject *val) { | 452 | TObject *luaH_set (lua_State *L, Table *t, const TObject *key) { |
| 448 | const TObject *p = luaH_get(t, key); | 453 | const TObject *p = luaH_get(t, key); |
| 449 | if (p != &luaO_nilobject) { | 454 | t->flags = 0; |
| 450 | settableval(p, val); | 455 | if (p != &luaO_nilobject) |
| 451 | } | 456 | return cast(TObject *, p); |
| 452 | else { | 457 | else { |
| 453 | if (ttype(key) == LUA_TNIL) luaG_runerror(L, "table index is nil"); | 458 | if (ttype(key) == LUA_TNIL) luaG_runerror(L, "table index is nil"); |
| 454 | newkey(L, t, key, val); | 459 | return newkey(L, t, key); |
| 455 | } | 460 | } |
| 456 | t->flags = 0; | ||
| 457 | } | 461 | } |
| 458 | 462 | ||
| 459 | 463 | ||
| 460 | void luaH_setnum (lua_State *L, Table *t, int key, const TObject *val) { | 464 | TObject *luaH_setnum (lua_State *L, Table *t, int key) { |
| 461 | const TObject *p = luaH_getnum(t, key); | 465 | const TObject *p = luaH_getnum(t, key); |
| 462 | if (p != &luaO_nilobject) { | 466 | if (p != &luaO_nilobject) |
| 463 | settableval(p, val); | 467 | return cast(TObject *, p); |
| 464 | } | ||
| 465 | else { | 468 | else { |
| 466 | TObject k; | 469 | TObject k; |
| 467 | setnvalue(&k, key); | 470 | setnvalue(&k, key); |
| 468 | newkey(L, t, &k, val); | 471 | return newkey(L, t, &k); |
| 469 | } | 472 | } |
| 470 | } | 473 | } |
| 471 | 474 | ||
