diff options
Diffstat (limited to 'apps/plugins/lua/ltable.c')
-rw-r--r-- | apps/plugins/lua/ltable.c | 194 |
1 files changed, 100 insertions, 94 deletions
diff --git a/apps/plugins/lua/ltable.c b/apps/plugins/lua/ltable.c index ec84f4fabc..e0fe98ce3e 100644 --- a/apps/plugins/lua/ltable.c +++ b/apps/plugins/lua/ltable.c | |||
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: ltable.c,v 2.32.1.2 2007/12/28 15:32:23 roberto Exp $ | 2 | ** $Id: ltable.c,v 2.72.1.1 2013/04/12 18:48:47 roberto Exp $ |
3 | ** Lua tables (hash) | 3 | ** Lua tables (hash) |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -18,7 +18,6 @@ | |||
18 | ** Hence even when the load factor reaches 100%, performance remains good. | 18 | ** Hence even when the load factor reaches 100%, performance remains good. |
19 | */ | 19 | */ |
20 | 20 | ||
21 | #include <math.h> | ||
22 | #include <string.h> | 21 | #include <string.h> |
23 | 22 | ||
24 | #define ltable_c | 23 | #define ltable_c |
@@ -32,14 +31,16 @@ | |||
32 | #include "lmem.h" | 31 | #include "lmem.h" |
33 | #include "lobject.h" | 32 | #include "lobject.h" |
34 | #include "lstate.h" | 33 | #include "lstate.h" |
34 | #include "lstring.h" | ||
35 | #include "ltable.h" | 35 | #include "ltable.h" |
36 | #include "lvm.h" | ||
36 | 37 | ||
37 | 38 | ||
38 | /* | 39 | /* |
39 | ** max size of array part is 2^MAXBITS | 40 | ** max size of array part is 2^MAXBITS |
40 | */ | 41 | */ |
41 | #if LUAI_BITSINT > 26 | 42 | #if LUAI_BITSINT >= 32 |
42 | #define MAXBITS 26 | 43 | #define MAXBITS 30 |
43 | #else | 44 | #else |
44 | #define MAXBITS (LUAI_BITSINT-2) | 45 | #define MAXBITS (LUAI_BITSINT-2) |
45 | #endif | 46 | #endif |
@@ -47,10 +48,10 @@ | |||
47 | #define MAXASIZE (1 << MAXBITS) | 48 | #define MAXASIZE (1 << MAXBITS) |
48 | 49 | ||
49 | 50 | ||
50 | #define hashpow2(t,n) (gnode(t, lmod((n), sizenode(t)))) | 51 | #define hashpow2(t,n) (gnode(t, lmod((n), sizenode(t)))) |
51 | 52 | ||
52 | #define hashstr(t,str) hashpow2(t, (str)->tsv.hash) | 53 | #define hashstr(t,str) hashpow2(t, (str)->tsv.hash) |
53 | #define hashboolean(t,p) hashpow2(t, p) | 54 | #define hashboolean(t,p) hashpow2(t, p) |
54 | 55 | ||
55 | 56 | ||
56 | /* | 57 | /* |
@@ -72,9 +73,11 @@ | |||
72 | 73 | ||
73 | #define dummynode (&dummynode_) | 74 | #define dummynode (&dummynode_) |
74 | 75 | ||
76 | #define isdummy(n) ((n) == dummynode) | ||
77 | |||
75 | static const Node dummynode_ = { | 78 | static const Node dummynode_ = { |
76 | {{NULL}, LUA_TNIL}, /* value */ | 79 | {NILCONSTANT}, /* value */ |
77 | {{{NULL}, LUA_TNIL, NULL}} /* key */ | 80 | {{NILCONSTANT, NULL}} /* key */ |
78 | }; | 81 | }; |
79 | 82 | ||
80 | 83 | ||
@@ -101,12 +104,22 @@ static Node *mainposition (const Table *t, const TValue *key) { | |||
101 | switch (ttype(key)) { | 104 | switch (ttype(key)) { |
102 | case LUA_TNUMBER: | 105 | case LUA_TNUMBER: |
103 | return hashnum(t, nvalue(key)); | 106 | return hashnum(t, nvalue(key)); |
104 | case LUA_TSTRING: | 107 | case LUA_TLNGSTR: { |
108 | TString *s = rawtsvalue(key); | ||
109 | if (s->tsv.extra == 0) { /* no hash? */ | ||
110 | s->tsv.hash = luaS_hash(getstr(s), s->tsv.len, s->tsv.hash); | ||
111 | s->tsv.extra = 1; /* now it has its hash */ | ||
112 | } | ||
113 | return hashstr(t, rawtsvalue(key)); | ||
114 | } | ||
115 | case LUA_TSHRSTR: | ||
105 | return hashstr(t, rawtsvalue(key)); | 116 | return hashstr(t, rawtsvalue(key)); |
106 | case LUA_TBOOLEAN: | 117 | case LUA_TBOOLEAN: |
107 | return hashboolean(t, bvalue(key)); | 118 | return hashboolean(t, bvalue(key)); |
108 | case LUA_TLIGHTUSERDATA: | 119 | case LUA_TLIGHTUSERDATA: |
109 | return hashpointer(t, pvalue(key)); | 120 | return hashpointer(t, pvalue(key)); |
121 | case LUA_TLCF: | ||
122 | return hashpointer(t, fvalue(key)); | ||
110 | default: | 123 | default: |
111 | return hashpointer(t, gcvalue(key)); | 124 | return hashpointer(t, gcvalue(key)); |
112 | } | 125 | } |
@@ -132,7 +145,7 @@ static int arrayindex (const TValue *key) { | |||
132 | /* | 145 | /* |
133 | ** returns the index of a `key' for table traversals. First goes all | 146 | ** returns the index of a `key' for table traversals. First goes all |
134 | ** elements in the array part, then elements in the hash part. The | 147 | ** elements in the array part, then elements in the hash part. The |
135 | ** beginning of a traversal is signalled by -1. | 148 | ** beginning of a traversal is signaled by -1. |
136 | */ | 149 | */ |
137 | static int findindex (lua_State *L, Table *t, StkId key) { | 150 | static int findindex (lua_State *L, Table *t, StkId key) { |
138 | int i; | 151 | int i; |
@@ -142,19 +155,19 @@ static int findindex (lua_State *L, Table *t, StkId key) { | |||
142 | return i-1; /* yes; that's the index (corrected to C) */ | 155 | return i-1; /* yes; that's the index (corrected to C) */ |
143 | else { | 156 | else { |
144 | Node *n = mainposition(t, key); | 157 | Node *n = mainposition(t, key); |
145 | do { /* check whether `key' is somewhere in the chain */ | 158 | for (;;) { /* check whether `key' is somewhere in the chain */ |
146 | /* key may be dead already, but it is ok to use it in `next' */ | 159 | /* key may be dead already, but it is ok to use it in `next' */ |
147 | if (luaO_rawequalObj(key2tval(n), key) || | 160 | if (luaV_rawequalobj(gkey(n), key) || |
148 | (ttype(gkey(n)) == LUA_TDEADKEY && iscollectable(key) && | 161 | (ttisdeadkey(gkey(n)) && iscollectable(key) && |
149 | gcvalue(gkey(n)) == gcvalue(key))) { | 162 | deadvalue(gkey(n)) == gcvalue(key))) { |
150 | i = cast_int(n - gnode(t, 0)); /* key index in hash table */ | 163 | i = cast_int(n - gnode(t, 0)); /* key index in hash table */ |
151 | /* hash elements are numbered after array ones */ | 164 | /* hash elements are numbered after array ones */ |
152 | return i + t->sizearray; | 165 | return i + t->sizearray; |
153 | } | 166 | } |
154 | else n = gnext(n); | 167 | else n = gnext(n); |
155 | } while (n); | 168 | if (n == NULL) |
156 | luaG_runerror(L, "invalid key to " LUA_QL("next")); /* key not found */ | 169 | luaG_runerror(L, "invalid key to " LUA_QL("next")); /* key not found */ |
157 | return 0; /* to avoid warnings */ | 170 | } |
158 | } | 171 | } |
159 | } | 172 | } |
160 | 173 | ||
@@ -170,7 +183,7 @@ int luaH_next (lua_State *L, Table *t, StkId key) { | |||
170 | } | 183 | } |
171 | for (i -= t->sizearray; i < sizenode(t); i++) { /* then hash part */ | 184 | for (i -= t->sizearray; i < sizenode(t); i++) { /* then hash part */ |
172 | if (!ttisnil(gval(gnode(t, i)))) { /* a non-nil value? */ | 185 | if (!ttisnil(gval(gnode(t, i)))) { /* a non-nil value? */ |
173 | setobj2s(L, key, key2tval(gnode(t, i))); | 186 | setobj2s(L, key, gkey(gnode(t, i))); |
174 | setobj2s(L, key+1, gval(gnode(t, i))); | 187 | setobj2s(L, key+1, gval(gnode(t, i))); |
175 | return 1; | 188 | return 1; |
176 | } | 189 | } |
@@ -211,7 +224,7 @@ static int computesizes (int nums[], int *narray) { | |||
211 | static int countint (const TValue *key, int *nums) { | 224 | static int countint (const TValue *key, int *nums) { |
212 | int k = arrayindex(key); | 225 | int k = arrayindex(key); |
213 | if (0 < k && k <= MAXASIZE) { /* is `key' an appropriate array index? */ | 226 | if (0 < k && k <= MAXASIZE) { /* is `key' an appropriate array index? */ |
214 | nums[ceillog2(k)]++; /* count as such */ | 227 | nums[luaO_ceillog2(k)]++; /* count as such */ |
215 | return 1; | 228 | return 1; |
216 | } | 229 | } |
217 | else | 230 | else |
@@ -251,7 +264,7 @@ static int numusehash (const Table *t, int *nums, int *pnasize) { | |||
251 | while (i--) { | 264 | while (i--) { |
252 | Node *n = &t->node[i]; | 265 | Node *n = &t->node[i]; |
253 | if (!ttisnil(gval(n))) { | 266 | if (!ttisnil(gval(n))) { |
254 | ause += countint(key2tval(n), nums); | 267 | ause += countint(gkey(n), nums); |
255 | totaluse++; | 268 | totaluse++; |
256 | } | 269 | } |
257 | } | 270 | } |
@@ -277,7 +290,7 @@ static void setnodevector (lua_State *L, Table *t, int size) { | |||
277 | } | 290 | } |
278 | else { | 291 | else { |
279 | int i; | 292 | int i; |
280 | lsize = ceillog2(size); | 293 | lsize = luaO_ceillog2(size); |
281 | if (lsize > MAXBITS) | 294 | if (lsize > MAXBITS) |
282 | luaG_runerror(L, "table overflow"); | 295 | luaG_runerror(L, "table overflow"); |
283 | size = twoto(lsize); | 296 | size = twoto(lsize); |
@@ -294,7 +307,7 @@ static void setnodevector (lua_State *L, Table *t, int size) { | |||
294 | } | 307 | } |
295 | 308 | ||
296 | 309 | ||
297 | static void resize (lua_State *L, Table *t, int nasize, int nhsize) { | 310 | void luaH_resize (lua_State *L, Table *t, int nasize, int nhsize) { |
298 | int i; | 311 | int i; |
299 | int oldasize = t->sizearray; | 312 | int oldasize = t->sizearray; |
300 | int oldhsize = t->lsizenode; | 313 | int oldhsize = t->lsizenode; |
@@ -302,13 +315,13 @@ static void resize (lua_State *L, Table *t, int nasize, int nhsize) { | |||
302 | if (nasize > oldasize) /* array part must grow? */ | 315 | if (nasize > oldasize) /* array part must grow? */ |
303 | setarrayvector(L, t, nasize); | 316 | setarrayvector(L, t, nasize); |
304 | /* create new hash part with appropriate size */ | 317 | /* create new hash part with appropriate size */ |
305 | setnodevector(L, t, nhsize); | 318 | setnodevector(L, t, nhsize); |
306 | if (nasize < oldasize) { /* array part must shrink? */ | 319 | if (nasize < oldasize) { /* array part must shrink? */ |
307 | t->sizearray = nasize; | 320 | t->sizearray = nasize; |
308 | /* re-insert elements from vanishing slice */ | 321 | /* re-insert elements from vanishing slice */ |
309 | for (i=nasize; i<oldasize; i++) { | 322 | for (i=nasize; i<oldasize; i++) { |
310 | if (!ttisnil(&t->array[i])) | 323 | if (!ttisnil(&t->array[i])) |
311 | setobjt2t(L, luaH_setnum(L, t, i+1), &t->array[i]); | 324 | luaH_setint(L, t, i + 1, &t->array[i]); |
312 | } | 325 | } |
313 | /* shrink array */ | 326 | /* shrink array */ |
314 | luaM_reallocvector(L, t->array, oldasize, nasize, TValue); | 327 | luaM_reallocvector(L, t->array, oldasize, nasize, TValue); |
@@ -316,23 +329,26 @@ static void resize (lua_State *L, Table *t, int nasize, int nhsize) { | |||
316 | /* re-insert elements from hash part */ | 329 | /* re-insert elements from hash part */ |
317 | for (i = twoto(oldhsize) - 1; i >= 0; i--) { | 330 | for (i = twoto(oldhsize) - 1; i >= 0; i--) { |
318 | Node *old = nold+i; | 331 | Node *old = nold+i; |
319 | if (!ttisnil(gval(old))) | 332 | if (!ttisnil(gval(old))) { |
320 | setobjt2t(L, luaH_set(L, t, key2tval(old)), gval(old)); | 333 | /* doesn't need barrier/invalidate cache, as entry was |
334 | already present in the table */ | ||
335 | setobjt2t(L, luaH_set(L, t, gkey(old)), gval(old)); | ||
336 | } | ||
321 | } | 337 | } |
322 | if (nold != dummynode) | 338 | if (!isdummy(nold)) |
323 | luaM_freearray(L, nold, twoto(oldhsize), Node); /* free old array */ | 339 | luaM_freearray(L, nold, cast(size_t, twoto(oldhsize))); /* free old array */ |
324 | } | 340 | } |
325 | 341 | ||
326 | 342 | ||
327 | void luaH_resizearray (lua_State *L, Table *t, int nasize) { | 343 | void luaH_resizearray (lua_State *L, Table *t, int nasize) { |
328 | int nsize = (t->node == dummynode) ? 0 : sizenode(t); | 344 | int nsize = isdummy(t->node) ? 0 : sizenode(t); |
329 | resize(L, t, nasize, nsize); | 345 | luaH_resize(L, t, nasize, nsize); |
330 | } | 346 | } |
331 | 347 | ||
332 | 348 | ||
333 | static void rehash (lua_State *L, Table *t, const TValue *ek) { | 349 | static void rehash (lua_State *L, Table *t, const TValue *ek) { |
334 | int nasize, na; | 350 | int nasize, na; |
335 | int nums[MAXBITS+1]; /* nums[i] = number of keys between 2^(i-1) and 2^i */ | 351 | int nums[MAXBITS+1]; /* nums[i] = number of keys with 2^(i-1) < k <= 2^i */ |
336 | int i; | 352 | int i; |
337 | int totaluse; | 353 | int totaluse; |
338 | for (i=0; i<=MAXBITS; i++) nums[i] = 0; /* reset counts */ | 354 | for (i=0; i<=MAXBITS; i++) nums[i] = 0; /* reset counts */ |
@@ -345,7 +361,7 @@ static void rehash (lua_State *L, Table *t, const TValue *ek) { | |||
345 | /* compute new size for array part */ | 361 | /* compute new size for array part */ |
346 | na = computesizes(nums, &nasize); | 362 | na = computesizes(nums, &nasize); |
347 | /* resize the table to new computed sizes */ | 363 | /* resize the table to new computed sizes */ |
348 | resize(L, t, nasize, totaluse - na); | 364 | luaH_resize(L, t, nasize, totaluse - na); |
349 | } | 365 | } |
350 | 366 | ||
351 | 367 | ||
@@ -355,32 +371,28 @@ static void rehash (lua_State *L, Table *t, const TValue *ek) { | |||
355 | */ | 371 | */ |
356 | 372 | ||
357 | 373 | ||
358 | Table *luaH_new (lua_State *L, int narray, int nhash) { | 374 | Table *luaH_new (lua_State *L) { |
359 | Table *t = luaM_new(L, Table); | 375 | Table *t = &luaC_newobj(L, LUA_TTABLE, sizeof(Table), NULL, 0)->h; |
360 | luaC_link(L, obj2gco(t), LUA_TTABLE); | ||
361 | t->metatable = NULL; | 376 | t->metatable = NULL; |
362 | t->flags = cast_byte(~0); | 377 | t->flags = cast_byte(~0); |
363 | /* temporary values (kept only if some malloc fails) */ | ||
364 | t->array = NULL; | 378 | t->array = NULL; |
365 | t->sizearray = 0; | 379 | t->sizearray = 0; |
366 | t->lsizenode = 0; | 380 | setnodevector(L, t, 0); |
367 | t->node = cast(Node *, dummynode); | ||
368 | setarrayvector(L, t, narray); | ||
369 | setnodevector(L, t, nhash); | ||
370 | return t; | 381 | return t; |
371 | } | 382 | } |
372 | 383 | ||
373 | 384 | ||
374 | void luaH_free (lua_State *L, Table *t) { | 385 | void luaH_free (lua_State *L, Table *t) { |
375 | if (t->node != dummynode) | 386 | if (!isdummy(t->node)) |
376 | luaM_freearray(L, t->node, sizenode(t), Node); | 387 | luaM_freearray(L, t->node, cast(size_t, sizenode(t))); |
377 | luaM_freearray(L, t->array, t->sizearray, TValue); | 388 | luaM_freearray(L, t->array, t->sizearray); |
378 | luaM_free(L, t); | 389 | luaM_free(L, t); |
379 | } | 390 | } |
380 | 391 | ||
381 | 392 | ||
382 | static Node *getfreepos (Table *t) { | 393 | static Node *getfreepos (Table *t) { |
383 | while (t->lastfree-- > t->node) { | 394 | while (t->lastfree > t->node) { |
395 | t->lastfree--; | ||
384 | if (ttisnil(gkey(t->lastfree))) | 396 | if (ttisnil(gkey(t->lastfree))) |
385 | return t->lastfree; | 397 | return t->lastfree; |
386 | } | 398 | } |
@@ -390,23 +402,28 @@ static Node *getfreepos (Table *t) { | |||
390 | 402 | ||
391 | 403 | ||
392 | /* | 404 | /* |
393 | ** inserts a new key into a hash table; first, check whether key's main | 405 | ** inserts a new key into a hash table; first, check whether key's main |
394 | ** position is free. If not, check whether colliding node is in its main | 406 | ** position is free. If not, check whether colliding node is in its main |
395 | ** position or not: if it is not, move colliding node to an empty place and | 407 | ** position or not: if it is not, move colliding node to an empty place and |
396 | ** put new key in its main position; otherwise (colliding node is in its main | 408 | ** put new key in its main position; otherwise (colliding node is in its main |
397 | ** position), new key goes to an empty position. | 409 | ** position), new key goes to an empty position. |
398 | */ | 410 | */ |
399 | static TValue *newkey (lua_State *L, Table *t, const TValue *key) { | 411 | TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) { |
400 | Node *mp = mainposition(t, key); | 412 | Node *mp; |
401 | if (!ttisnil(gval(mp)) || mp == dummynode) { | 413 | if (ttisnil(key)) luaG_runerror(L, "table index is nil"); |
414 | else if (ttisnumber(key) && luai_numisnan(L, nvalue(key))) | ||
415 | luaG_runerror(L, "table index is NaN"); | ||
416 | mp = mainposition(t, key); | ||
417 | if (!ttisnil(gval(mp)) || isdummy(mp)) { /* main position is taken? */ | ||
402 | Node *othern; | 418 | Node *othern; |
403 | Node *n = getfreepos(t); /* get a free place */ | 419 | Node *n = getfreepos(t); /* get a free place */ |
404 | if (n == NULL) { /* cannot find a free place? */ | 420 | if (n == NULL) { /* cannot find a free place? */ |
405 | rehash(L, t, key); /* grow table */ | 421 | rehash(L, t, key); /* grow table */ |
406 | return luaH_set(L, t, key); /* re-insert key into grown table */ | 422 | /* whatever called 'newkey' take care of TM cache and GC barrier */ |
423 | return luaH_set(L, t, key); /* insert key into grown table */ | ||
407 | } | 424 | } |
408 | lua_assert(n != dummynode); | 425 | lua_assert(!isdummy(n)); |
409 | othern = mainposition(t, key2tval(mp)); | 426 | othern = mainposition(t, gkey(mp)); |
410 | if (othern != mp) { /* is colliding node out of its main position? */ | 427 | if (othern != mp) { /* is colliding node out of its main position? */ |
411 | /* yes; move colliding node into free position */ | 428 | /* yes; move colliding node into free position */ |
412 | while (gnext(othern) != mp) othern = gnext(othern); /* find previous */ | 429 | while (gnext(othern) != mp) othern = gnext(othern); /* find previous */ |
@@ -422,8 +439,8 @@ static TValue *newkey (lua_State *L, Table *t, const TValue *key) { | |||
422 | mp = n; | 439 | mp = n; |
423 | } | 440 | } |
424 | } | 441 | } |
425 | gkey(mp)->value = key->value; gkey(mp)->tt = key->tt; | 442 | setobj2t(L, gkey(mp), key); |
426 | luaC_barriert(L, t, key); | 443 | luaC_barrierback(L, obj2gco(t), key); |
427 | lua_assert(ttisnil(gval(mp))); | 444 | lua_assert(ttisnil(gval(mp))); |
428 | return gval(mp); | 445 | return gval(mp); |
429 | } | 446 | } |
@@ -432,7 +449,7 @@ static TValue *newkey (lua_State *L, Table *t, const TValue *key) { | |||
432 | /* | 449 | /* |
433 | ** search function for integers | 450 | ** search function for integers |
434 | */ | 451 | */ |
435 | const TValue *luaH_getnum (Table *t, int key) { | 452 | const TValue *luaH_getint (Table *t, int key) { |
436 | /* (1 <= key && key <= t->sizearray) */ | 453 | /* (1 <= key && key <= t->sizearray) */ |
437 | if (cast(unsigned int, key-1) < cast(unsigned int, t->sizearray)) | 454 | if (cast(unsigned int, key-1) < cast(unsigned int, t->sizearray)) |
438 | return &t->array[key-1]; | 455 | return &t->array[key-1]; |
@@ -450,12 +467,13 @@ const TValue *luaH_getnum (Table *t, int key) { | |||
450 | 467 | ||
451 | 468 | ||
452 | /* | 469 | /* |
453 | ** search function for strings | 470 | ** search function for short strings |
454 | */ | 471 | */ |
455 | const TValue *luaH_getstr (Table *t, TString *key) { | 472 | const TValue *luaH_getstr (Table *t, TString *key) { |
456 | Node *n = hashstr(t, key); | 473 | Node *n = hashstr(t, key); |
474 | lua_assert(key->tsv.tt == LUA_TSHRSTR); | ||
457 | do { /* check whether `key' is somewhere in the chain */ | 475 | do { /* check whether `key' is somewhere in the chain */ |
458 | if (ttisstring(gkey(n)) && rawtsvalue(gkey(n)) == key) | 476 | if (ttisshrstring(gkey(n)) && eqshrstr(rawtsvalue(gkey(n)), key)) |
459 | return gval(n); /* that's it */ | 477 | return gval(n); /* that's it */ |
460 | else n = gnext(n); | 478 | else n = gnext(n); |
461 | } while (n); | 479 | } while (n); |
@@ -468,20 +486,20 @@ const TValue *luaH_getstr (Table *t, TString *key) { | |||
468 | */ | 486 | */ |
469 | const TValue *luaH_get (Table *t, const TValue *key) { | 487 | const TValue *luaH_get (Table *t, const TValue *key) { |
470 | switch (ttype(key)) { | 488 | switch (ttype(key)) { |
489 | case LUA_TSHRSTR: return luaH_getstr(t, rawtsvalue(key)); | ||
471 | case LUA_TNIL: return luaO_nilobject; | 490 | case LUA_TNIL: return luaO_nilobject; |
472 | case LUA_TSTRING: return luaH_getstr(t, rawtsvalue(key)); | ||
473 | case LUA_TNUMBER: { | 491 | case LUA_TNUMBER: { |
474 | int k; | 492 | int k; |
475 | lua_Number n = nvalue(key); | 493 | lua_Number n = nvalue(key); |
476 | lua_number2int(k, n); | 494 | lua_number2int(k, n); |
477 | if (luai_numeq(cast_num(k), nvalue(key))) /* index is int? */ | 495 | if (luai_numeq(cast_num(k), n)) /* index is int? */ |
478 | return luaH_getnum(t, k); /* use specialized version */ | 496 | return luaH_getint(t, k); /* use specialized version */ |
479 | /* else go through */ | 497 | /* else go through */ |
480 | } | 498 | } |
481 | default: { | 499 | default: { |
482 | Node *n = mainposition(t, key); | 500 | Node *n = mainposition(t, key); |
483 | do { /* check whether `key' is somewhere in the chain */ | 501 | do { /* check whether `key' is somewhere in the chain */ |
484 | if (luaO_rawequalObj(key2tval(n), key)) | 502 | if (luaV_rawequalobj(gkey(n), key)) |
485 | return gval(n); /* that's it */ | 503 | return gval(n); /* that's it */ |
486 | else n = gnext(n); | 504 | else n = gnext(n); |
487 | } while (n); | 505 | } while (n); |
@@ -491,41 +509,29 @@ const TValue *luaH_get (Table *t, const TValue *key) { | |||
491 | } | 509 | } |
492 | 510 | ||
493 | 511 | ||
512 | /* | ||
513 | ** beware: when using this function you probably need to check a GC | ||
514 | ** barrier and invalidate the TM cache. | ||
515 | */ | ||
494 | TValue *luaH_set (lua_State *L, Table *t, const TValue *key) { | 516 | TValue *luaH_set (lua_State *L, Table *t, const TValue *key) { |
495 | const TValue *p = luaH_get(t, key); | 517 | const TValue *p = luaH_get(t, key); |
496 | t->flags = 0; | ||
497 | if (p != luaO_nilobject) | 518 | if (p != luaO_nilobject) |
498 | return cast(TValue *, p); | 519 | return cast(TValue *, p); |
499 | else { | 520 | else return luaH_newkey(L, t, key); |
500 | if (ttisnil(key)) luaG_runerror(L, "table index is nil"); | ||
501 | else if (ttisnumber(key) && luai_numisnan(nvalue(key))) | ||
502 | luaG_runerror(L, "table index is NaN"); | ||
503 | return newkey(L, t, key); | ||
504 | } | ||
505 | } | 521 | } |
506 | 522 | ||
507 | 523 | ||
508 | TValue *luaH_setnum (lua_State *L, Table *t, int key) { | 524 | void luaH_setint (lua_State *L, Table *t, int key, TValue *value) { |
509 | const TValue *p = luaH_getnum(t, key); | 525 | const TValue *p = luaH_getint(t, key); |
526 | TValue *cell; | ||
510 | if (p != luaO_nilobject) | 527 | if (p != luaO_nilobject) |
511 | return cast(TValue *, p); | 528 | cell = cast(TValue *, p); |
512 | else { | 529 | else { |
513 | TValue k; | 530 | TValue k; |
514 | setnvalue(&k, cast_num(key)); | 531 | setnvalue(&k, cast_num(key)); |
515 | return newkey(L, t, &k); | 532 | cell = luaH_newkey(L, t, &k); |
516 | } | ||
517 | } | ||
518 | |||
519 | |||
520 | TValue *luaH_setstr (lua_State *L, Table *t, TString *key) { | ||
521 | const TValue *p = luaH_getstr(t, key); | ||
522 | if (p != luaO_nilobject) | ||
523 | return cast(TValue *, p); | ||
524 | else { | ||
525 | TValue k; | ||
526 | setsvalue(L, &k, key); | ||
527 | return newkey(L, t, &k); | ||
528 | } | 533 | } |
534 | setobj2t(L, cell, value); | ||
529 | } | 535 | } |
530 | 536 | ||
531 | 537 | ||
@@ -533,20 +539,20 @@ static int unbound_search (Table *t, unsigned int j) { | |||
533 | unsigned int i = j; /* i is zero or a present index */ | 539 | unsigned int i = j; /* i is zero or a present index */ |
534 | j++; | 540 | j++; |
535 | /* find `i' and `j' such that i is present and j is not */ | 541 | /* find `i' and `j' such that i is present and j is not */ |
536 | while (!ttisnil(luaH_getnum(t, j))) { | 542 | while (!ttisnil(luaH_getint(t, j))) { |
537 | i = j; | 543 | i = j; |
538 | j *= 2; | 544 | j *= 2; |
539 | if (j > cast(unsigned int, MAX_INT)) { /* overflow? */ | 545 | if (j > cast(unsigned int, MAX_INT)) { /* overflow? */ |
540 | /* table was built with bad purposes: resort to linear search */ | 546 | /* table was built with bad purposes: resort to linear search */ |
541 | i = 1; | 547 | i = 1; |
542 | while (!ttisnil(luaH_getnum(t, i))) i++; | 548 | while (!ttisnil(luaH_getint(t, i))) i++; |
543 | return i - 1; | 549 | return i - 1; |
544 | } | 550 | } |
545 | } | 551 | } |
546 | /* now do a binary search between them */ | 552 | /* now do a binary search between them */ |
547 | while (j - i > 1) { | 553 | while (j - i > 1) { |
548 | unsigned int m = (i+j)/2; | 554 | unsigned int m = (i+j)/2; |
549 | if (ttisnil(luaH_getnum(t, m))) j = m; | 555 | if (ttisnil(luaH_getint(t, m))) j = m; |
550 | else i = m; | 556 | else i = m; |
551 | } | 557 | } |
552 | return i; | 558 | return i; |
@@ -570,7 +576,7 @@ int luaH_getn (Table *t) { | |||
570 | return i; | 576 | return i; |
571 | } | 577 | } |
572 | /* else must find a boundary in hash part */ | 578 | /* else must find a boundary in hash part */ |
573 | else if (t->node == dummynode) /* hash part is empty? */ | 579 | else if (isdummy(t->node)) /* hash part is empty? */ |
574 | return j; /* that is easy... */ | 580 | return j; /* that is easy... */ |
575 | else return unbound_search(t, j); | 581 | else return unbound_search(t, j); |
576 | } | 582 | } |
@@ -583,6 +589,6 @@ Node *luaH_mainposition (const Table *t, const TValue *key) { | |||
583 | return mainposition(t, key); | 589 | return mainposition(t, key); |
584 | } | 590 | } |
585 | 591 | ||
586 | int luaH_isdummy (Node *n) { return n == dummynode; } | 592 | int luaH_isdummy (Node *n) { return isdummy(n); } |
587 | 593 | ||
588 | #endif | 594 | #endif |