summaryrefslogtreecommitdiff
path: root/src/lj_tab.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lj_tab.c')
-rw-r--r--src/lj_tab.c33
1 files changed, 24 insertions, 9 deletions
diff --git a/src/lj_tab.c b/src/lj_tab.c
index d64a0426..c8e4efe1 100644
--- a/src/lj_tab.c
+++ b/src/lj_tab.c
@@ -34,6 +34,7 @@ static LJ_AINLINE Node *hashmask(const GCtab *t, uint32_t hash)
34/* Hash an arbitrary key and return its anchor position in the hash table. */ 34/* Hash an arbitrary key and return its anchor position in the hash table. */
35static Node *hashkey(const GCtab *t, cTValue *key) 35static Node *hashkey(const GCtab *t, cTValue *key)
36{ 36{
37 lua_assert(!tvisint(key));
37 if (tvisstr(key)) 38 if (tvisstr(key))
38 return hashstr(t, strV(key)); 39 return hashstr(t, strV(key));
39 else if (tvisnum(key)) 40 else if (tvisnum(key))
@@ -100,7 +101,7 @@ static GCtab *newtab(lua_State *L, uint32_t asize, uint32_t hbits)
100 lua_assert((sizeof(GCtab) & 7) == 0); 101 lua_assert((sizeof(GCtab) & 7) == 0);
101 t = (GCtab *)lj_mem_newgco(L, sizetabcolo(asize)); 102 t = (GCtab *)lj_mem_newgco(L, sizetabcolo(asize));
102 t->gct = ~LJ_TTAB; 103 t->gct = ~LJ_TTAB;
103 t->nomm = cast_byte(~0); 104 t->nomm = (uint8_t)~0;
104 t->colo = (int8_t)asize; 105 t->colo = (int8_t)asize;
105 setmref(t->array, (TValue *)((char *)t + sizeof(GCtab))); 106 setmref(t->array, (TValue *)((char *)t + sizeof(GCtab)));
106 setgcrefnull(t->metatable); 107 setgcrefnull(t->metatable);
@@ -110,7 +111,7 @@ static GCtab *newtab(lua_State *L, uint32_t asize, uint32_t hbits)
110 } else { /* Otherwise separately allocate the array part. */ 111 } else { /* Otherwise separately allocate the array part. */
111 t = lj_mem_newobj(L, GCtab); 112 t = lj_mem_newobj(L, GCtab);
112 t->gct = ~LJ_TTAB; 113 t->gct = ~LJ_TTAB;
113 t->nomm = cast_byte(~0); 114 t->nomm = (uint8_t)~0;
114 t->colo = 0; 115 t->colo = 0;
115 setmref(t->array, NULL); 116 setmref(t->array, NULL);
116 setgcrefnull(t->metatable); 117 setgcrefnull(t->metatable);
@@ -275,10 +276,11 @@ static void resizetab(lua_State *L, GCtab *t, uint32_t asize, uint32_t hbits)
275 276
276static uint32_t countint(cTValue *key, uint32_t *bins) 277static uint32_t countint(cTValue *key, uint32_t *bins)
277{ 278{
279 lua_assert(!tvisint(key));
278 if (tvisnum(key)) { 280 if (tvisnum(key)) {
279 lua_Number nk = numV(key); 281 lua_Number nk = numV(key);
280 int32_t k = lj_num2int(nk); 282 int32_t k = lj_num2int(nk);
281 if ((uint32_t)k < LJ_MAX_ASIZE && nk == cast_num(k)) { 283 if ((uint32_t)k < LJ_MAX_ASIZE && nk == (lua_Number)k) {
282 bins[(k > 2 ? lj_fls((uint32_t)(k-1)) : 0)]++; 284 bins[(k > 2 ? lj_fls((uint32_t)(k-1)) : 0)]++;
283 return 1; 285 return 1;
284 } 286 }
@@ -360,7 +362,7 @@ cTValue * LJ_FASTCALL lj_tab_getinth(GCtab *t, int32_t key)
360{ 362{
361 TValue k; 363 TValue k;
362 Node *n; 364 Node *n;
363 k.n = cast_num(key); 365 k.n = (lua_Number)key;
364 n = hashnum(t, &k); 366 n = hashnum(t, &k);
365 do { 367 do {
366 if (tvisnum(&n->key) && n->key.n == k.n) 368 if (tvisnum(&n->key) && n->key.n == k.n)
@@ -385,10 +387,14 @@ cTValue *lj_tab_get(lua_State *L, GCtab *t, cTValue *key)
385 cTValue *tv = lj_tab_getstr(t, strV(key)); 387 cTValue *tv = lj_tab_getstr(t, strV(key));
386 if (tv) 388 if (tv)
387 return tv; 389 return tv;
390 } else if (tvisint(key)) {
391 cTValue *tv = lj_tab_getint(t, intV(key));
392 if (tv)
393 return tv;
388 } else if (tvisnum(key)) { 394 } else if (tvisnum(key)) {
389 lua_Number nk = numV(key); 395 lua_Number nk = numV(key);
390 int32_t k = lj_num2int(nk); 396 int32_t k = lj_num2int(nk);
391 if (nk == cast_num(k)) { 397 if (nk == (lua_Number)k) {
392 cTValue *tv = lj_tab_getint(t, k); 398 cTValue *tv = lj_tab_getint(t, k);
393 if (tv) 399 if (tv)
394 return tv; 400 return tv;
@@ -466,7 +472,7 @@ TValue *lj_tab_setinth(lua_State *L, GCtab *t, int32_t key)
466{ 472{
467 TValue k; 473 TValue k;
468 Node *n; 474 Node *n;
469 k.n = cast_num(key); 475 k.n = (lua_Number)key;
470 n = hashnum(t, &k); 476 n = hashnum(t, &k);
471 do { 477 do {
472 if (tvisnum(&n->key) && n->key.n == k.n) 478 if (tvisnum(&n->key) && n->key.n == k.n)
@@ -493,10 +499,12 @@ TValue *lj_tab_set(lua_State *L, GCtab *t, cTValue *key)
493 t->nomm = 0; /* Invalidate negative metamethod cache. */ 499 t->nomm = 0; /* Invalidate negative metamethod cache. */
494 if (tvisstr(key)) { 500 if (tvisstr(key)) {
495 return lj_tab_setstr(L, t, strV(key)); 501 return lj_tab_setstr(L, t, strV(key));
502 } else if (tvisint(key)) {
503 return lj_tab_setint(L, t, intV(key));
496 } else if (tvisnum(key)) { 504 } else if (tvisnum(key)) {
497 lua_Number nk = numV(key); 505 lua_Number nk = numV(key);
498 int32_t k = lj_num2int(nk); 506 int32_t k = lj_num2int(nk);
499 if (nk == cast_num(k)) 507 if (nk == (lua_Number)k)
500 return lj_tab_setint(L, t, k); 508 return lj_tab_setint(L, t, k);
501 if (tvisnan(key)) 509 if (tvisnan(key))
502 lj_err_msg(L, LJ_ERR_NANIDX); 510 lj_err_msg(L, LJ_ERR_NANIDX);
@@ -517,10 +525,17 @@ TValue *lj_tab_set(lua_State *L, GCtab *t, cTValue *key)
517/* Get the traversal index of a key. */ 525/* Get the traversal index of a key. */
518static uint32_t keyindex(lua_State *L, GCtab *t, cTValue *key) 526static uint32_t keyindex(lua_State *L, GCtab *t, cTValue *key)
519{ 527{
520 if (tvisnum(key)) { 528 TValue tmp;
529 if (tvisint(key)) {
530 int32_t k = intV(key);
531 if ((uint32_t)k < t->asize)
532 return (uint32_t)k; /* Array key indexes: [0..t->asize-1] */
533 setnumV(&tmp, (lua_Number)k);
534 key = &tmp;
535 } else if (tvisnum(key)) {
521 lua_Number nk = numV(key); 536 lua_Number nk = numV(key);
522 int32_t k = lj_num2int(nk); 537 int32_t k = lj_num2int(nk);
523 if ((uint32_t)k < t->asize && nk == cast_num(k)) 538 if ((uint32_t)k < t->asize && nk == (lua_Number)k)
524 return (uint32_t)k; /* Array key indexes: [0..t->asize-1] */ 539 return (uint32_t)k; /* Array key indexes: [0..t->asize-1] */
525 } 540 }
526 if (!tvisnil(key)) { 541 if (!tvisnil(key)) {