src/lj_tab.c - luajit-2.0-src

Functions defined

Macros defined

Source code

  1. /*
  2. ** Table handling.
  3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h
  4. **
  5. ** Major portions taken verbatim or adapted from the Lua interpreter.
  6. ** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h
  7. */

  8. #define lj_tab_c
  9. #define LUA_CORE

  10. #include "lj_obj.h"
  11. #include "lj_gc.h"
  12. #include "lj_err.h"
  13. #include "lj_tab.h"

  14. /* -- Object hashing ------------------------------------------------------ */

  15. /* Hash values are masked with the table hash mask and used as an index. */
  16. static LJ_AINLINE Node *hashmask(const GCtab *t, uint32_t hash)
  17. {
  18.   Node *n = noderef(t->node);
  19.   return &n[hash & t->hmask];
  20. }

  21. /* String hashes are precomputed when they are interned. */
  22. #define hashstr(t, s)                hashmask(t, (s)->hash)

  23. #define hashlohi(t, lo, hi)        hashmask((t), hashrot((lo), (hi)))
  24. #define hashnum(t, o)                hashlohi((t), (o)->u32.lo, ((o)->u32.hi << 1))
  25. #define hashptr(t, p)                hashlohi((t), u32ptr(p), u32ptr(p) + HASH_BIAS)
  26. #if LJ_GC64
  27. #define hashgcref(t, r) \
  28.   hashlohi((t), (uint32_t)gcrefu(r), (uint32_t)(gcrefu(r) >> 32))
  29. #else
  30. #define hashgcref(t, r)                hashlohi((t), gcrefu(r), gcrefu(r) + HASH_BIAS)
  31. #endif

  32. /* Hash an arbitrary key and return its anchor position in the hash table. */
  33. static Node *hashkey(const GCtab *t, cTValue *key)
  34. {
  35.   lua_assert(!tvisint(key));
  36.   if (tvisstr(key))
  37.     return hashstr(t, strV(key));
  38.   else if (tvisnum(key))
  39.     return hashnum(t, key);
  40.   else if (tvisbool(key))
  41.     return hashmask(t, boolV(key));
  42.   else
  43.     return hashgcref(t, key->gcr);
  44.   /* Only hash 32 bits of lightuserdata on a 64 bit CPU. Good enough? */
  45. }

  46. /* -- Table creation and destruction -------------------------------------- */

  47. /* Create new hash part for table. */
  48. static LJ_AINLINE void newhpart(lua_State *L, GCtab *t, uint32_t hbits)
  49. {
  50.   uint32_t hsize;
  51.   Node *node;
  52.   lua_assert(hbits != 0);
  53.   if (hbits > LJ_MAX_HBITS)
  54.     lj_err_msg(L, LJ_ERR_TABOV);
  55.   hsize = 1u << hbits;
  56.   node = lj_mem_newvec(L, hsize, Node);
  57.   setmref(t->node, node);
  58.   setfreetop(t, node, &node[hsize]);
  59.   t->hmask = hsize-1;
  60. }

  61. /*
  62. ** Q: Why all of these copies of t->hmask, t->node etc. to local variables?
  63. ** A: Because alias analysis for C is _really_ tough.
  64. **    Even state-of-the-art C compilers won't produce good code without this.
  65. */

  66. /* Clear hash part of table. */
  67. static LJ_AINLINE void clearhpart(GCtab *t)
  68. {
  69.   uint32_t i, hmask = t->hmask;
  70.   Node *node = noderef(t->node);
  71.   lua_assert(t->hmask != 0);
  72.   for (i = 0; i <= hmask; i++) {
  73.     Node *n = &node[i];
  74.     setmref(n->next, NULL);
  75.     setnilV(&n->key);
  76.     setnilV(&n->val);
  77.   }
  78. }

  79. /* Clear array part of table. */
  80. static LJ_AINLINE void clearapart(GCtab *t)
  81. {
  82.   uint32_t i, asize = t->asize;
  83.   TValue *array = tvref(t->array);
  84.   for (i = 0; i < asize; i++)
  85.     setnilV(&array[i]);
  86. }

  87. /* Create a new table. Note: the slots are not initialized (yet). */
  88. static GCtab *newtab(lua_State *L, uint32_t asize, uint32_t hbits)
  89. {
  90.   GCtab *t;
  91.   /* First try to colocate the array part. */
  92.   if (LJ_MAX_COLOSIZE != 0 && asize > 0 && asize <= LJ_MAX_COLOSIZE) {
  93.     Node *nilnode;
  94.     lua_assert((sizeof(GCtab) & 7) == 0);
  95.     t = (GCtab *)lj_mem_newgco(L, sizetabcolo(asize));
  96.     t->gct = ~LJ_TTAB;
  97.     t->nomm = (uint8_t)~0;
  98.     t->colo = (int8_t)asize;
  99.     setmref(t->array, (TValue *)((char *)t + sizeof(GCtab)));
  100.     setgcrefnull(t->metatable);
  101.     t->asize = asize;
  102.     t->hmask = 0;
  103.     nilnode = &G(L)->nilnode;
  104.     setmref(t->node, nilnode);
  105. #if LJ_GC64
  106.     setmref(t->freetop, nilnode);
  107. #endif
  108.   } else/* Otherwise separately allocate the array part. */
  109.     Node *nilnode;
  110.     t = lj_mem_newobj(L, GCtab);
  111.     t->gct = ~LJ_TTAB;
  112.     t->nomm = (uint8_t)~0;
  113.     t->colo = 0;
  114.     setmref(t->array, NULL);
  115.     setgcrefnull(t->metatable);
  116.     t->asize = 0/* In case the array allocation fails. */
  117.     t->hmask = 0;
  118.     nilnode = &G(L)->nilnode;
  119.     setmref(t->node, nilnode);
  120. #if LJ_GC64
  121.     setmref(t->freetop, nilnode);
  122. #endif
  123.     if (asize > 0) {
  124.       if (asize > LJ_MAX_ASIZE)
  125.         lj_err_msg(L, LJ_ERR_TABOV);
  126.       setmref(t->array, lj_mem_newvec(L, asize, TValue));
  127.       t->asize = asize;
  128.     }
  129.   }
  130.   if (hbits)
  131.     newhpart(L, t, hbits);
  132.   return t;
  133. }

  134. /* Create a new table.
  135. **
  136. ** IMPORTANT NOTE: The API differs from lua_createtable()!
  137. **
  138. ** The array size is non-inclusive. E.g. asize=128 creates array slots
  139. ** for 0..127, but not for 128. If you need slots 1..128, pass asize=129
  140. ** (slot 0 is wasted in this case).
  141. **
  142. ** The hash size is given in hash bits. hbits=0 means no hash part.
  143. ** hbits=1 creates 2 hash slots, hbits=2 creates 4 hash slots and so on.
  144. */
  145. GCtab *lj_tab_new(lua_State *L, uint32_t asize, uint32_t hbits)
  146. {
  147.   GCtab *t = newtab(L, asize, hbits);
  148.   clearapart(t);
  149.   if (t->hmask > 0) clearhpart(t);
  150.   return t;
  151. }

  152. /* The API of this function conforms to lua_createtable(). */
  153. GCtab *lj_tab_new_ah(lua_State *L, int32_t a, int32_t h)
  154. {
  155.   return lj_tab_new(L, (uint32_t)(a > 0 ? a+1 : 0), hsize2hbits(h));
  156. }

  157. #if LJ_HASJIT
  158. GCtab * LJ_FASTCALL lj_tab_new1(lua_State *L, uint32_t ahsize)
  159. {
  160.   GCtab *t = newtab(L, ahsize & 0xffffff, ahsize >> 24);
  161.   clearapart(t);
  162.   if (t->hmask > 0) clearhpart(t);
  163.   return t;
  164. }
  165. #endif

  166. /* Duplicate a table. */
  167. GCtab * LJ_FASTCALL lj_tab_dup(lua_State *L, const GCtab *kt)
  168. {
  169.   GCtab *t;
  170.   uint32_t asize, hmask;
  171.   t = newtab(L, kt->asize, kt->hmask > 0 ? lj_fls(kt->hmask)+1 : 0);
  172.   lua_assert(kt->asize == t->asize && kt->hmask == t->hmask);
  173.   t->nomm = 0/* Keys with metamethod names may be present. */
  174.   asize = kt->asize;
  175.   if (asize > 0) {
  176.     TValue *array = tvref(t->array);
  177.     TValue *karray = tvref(kt->array);
  178.     if (asize < 64) {  /* An inlined loop beats memcpy for < 512 bytes. */
  179.       uint32_t i;
  180.       for (i = 0; i < asize; i++)
  181.         copyTV(L, &array[i], &karray[i]);
  182.     } else {
  183.       memcpy(array, karray, asize*sizeof(TValue));
  184.     }
  185.   }
  186.   hmask = kt->hmask;
  187.   if (hmask > 0) {
  188.     uint32_t i;
  189.     Node *node = noderef(t->node);
  190.     Node *knode = noderef(kt->node);
  191.     ptrdiff_t d = (char *)node - (char *)knode;
  192.     setfreetop(t, node, (Node *)((char *)getfreetop(kt, knode) + d));
  193.     for (i = 0; i <= hmask; i++) {
  194.       Node *kn = &knode[i];
  195.       Node *n = &node[i];
  196.       Node *next = nextnode(kn);
  197.       /* Don't use copyTV here, since it asserts on a copy of a dead key. */
  198.       n->val = kn->val; n->key = kn->key;
  199.       setmref(n->next, next == NULL? next : (Node *)((char *)next + d));
  200.     }
  201.   }
  202.   return t;
  203. }

  204. /* Clear a table. */
  205. void LJ_FASTCALL lj_tab_clear(GCtab *t)
  206. {
  207.   clearapart(t);
  208.   if (t->hmask > 0) {
  209.     Node *node = noderef(t->node);
  210.     setfreetop(t, node, &node[t->hmask+1]);
  211.     clearhpart(t);
  212.   }
  213. }

  214. /* Free a table. */
  215. void LJ_FASTCALL lj_tab_free(global_State *g, GCtab *t)
  216. {
  217.   if (t->hmask > 0)
  218.     lj_mem_freevec(g, noderef(t->node), t->hmask+1, Node);
  219.   if (t->asize > 0 && LJ_MAX_COLOSIZE != 0 && t->colo <= 0)
  220.     lj_mem_freevec(g, tvref(t->array), t->asize, TValue);
  221.   if (LJ_MAX_COLOSIZE != 0 && t->colo)
  222.     lj_mem_free(g, t, sizetabcolo((uint32_t)t->colo & 0x7f));
  223.   else
  224.     lj_mem_freet(g, t);
  225. }

  226. /* -- Table resizing ------------------------------------------------------ */

  227. /* Resize a table to fit the new array/hash part sizes. */
  228. static void resizetab(lua_State *L, GCtab *t, uint32_t asize, uint32_t hbits)
  229. {
  230.   Node *oldnode = noderef(t->node);
  231.   uint32_t oldasize = t->asize;
  232.   uint32_t oldhmask = t->hmask;
  233.   if (asize > oldasize) {  /* Array part grows? */
  234.     TValue *array;
  235.     uint32_t i;
  236.     if (asize > LJ_MAX_ASIZE)
  237.       lj_err_msg(L, LJ_ERR_TABOV);
  238.     if (LJ_MAX_COLOSIZE != 0 && t->colo > 0) {
  239.       /* A colocated array must be separated and copied. */
  240.       TValue *oarray = tvref(t->array);
  241.       array = lj_mem_newvec(L, asize, TValue);
  242.       t->colo = (int8_t)(t->colo | 0x80);  /* Mark as separated (colo < 0). */
  243.       for (i = 0; i < oldasize; i++)
  244.         copyTV(L, &array[i], &oarray[i]);
  245.     } else {
  246.       array = (TValue *)lj_mem_realloc(L, tvref(t->array),
  247.                           oldasize*sizeof(TValue), asize*sizeof(TValue));
  248.     }
  249.     setmref(t->array, array);
  250.     t->asize = asize;
  251.     for (i = oldasize; i < asize; i++)  /* Clear newly allocated slots. */
  252.       setnilV(&array[i]);
  253.   }
  254.   /* Create new (empty) hash part. */
  255.   if (hbits) {
  256.     newhpart(L, t, hbits);
  257.     clearhpart(t);
  258.   } else {
  259.     global_State *g = G(L);
  260.     setmref(t->node, &g->nilnode);
  261. #if LJ_GC64
  262.     setmref(t->freetop, &g->nilnode);
  263. #endif
  264.     t->hmask = 0;
  265.   }
  266.   if (asize < oldasize) {  /* Array part shrinks? */
  267.     TValue *array = tvref(t->array);
  268.     uint32_t i;
  269.     t->asize = asize;  /* Note: This 'shrinks' even colocated arrays. */
  270.     for (i = asize; i < oldasize; i++)  /* Reinsert old array values. */
  271.       if (!tvisnil(&array[i]))
  272.         copyTV(L, lj_tab_setinth(L, t, (int32_t)i), &array[i]);
  273.     /* Physically shrink only separated arrays. */
  274.     if (LJ_MAX_COLOSIZE != 0 && t->colo <= 0)
  275.       setmref(t->array, lj_mem_realloc(L, array,
  276.               oldasize*sizeof(TValue), asize*sizeof(TValue)));
  277.   }
  278.   if (oldhmask > 0) {  /* Reinsert pairs from old hash part. */
  279.     global_State *g;
  280.     uint32_t i;
  281.     for (i = 0; i <= oldhmask; i++) {
  282.       Node *n = &oldnode[i];
  283.       if (!tvisnil(&n->val))
  284.         copyTV(L, lj_tab_set(L, t, &n->key), &n->val);
  285.     }
  286.     g = G(L);
  287.     lj_mem_freevec(g, oldnode, oldhmask+1, Node);
  288.   }
  289. }

  290. static uint32_t countint(cTValue *key, uint32_t *bins)
  291. {
  292.   lua_assert(!tvisint(key));
  293.   if (tvisnum(key)) {
  294.     lua_Number nk = numV(key);
  295.     int32_t k = lj_num2int(nk);
  296.     if ((uint32_t)k < LJ_MAX_ASIZE && nk == (lua_Number)k) {
  297.       bins[(k > 2 ? lj_fls((uint32_t)(k-1)) : 0)]++;
  298.       return 1;
  299.     }
  300.   }
  301.   return 0;
  302. }

  303. static uint32_t countarray(const GCtab *t, uint32_t *bins)
  304. {
  305.   uint32_t na, b, i;
  306.   if (t->asize == 0) return 0;
  307.   for (na = i = b = 0; b < LJ_MAX_ABITS; b++) {
  308.     uint32_t n, top = 2u << b;
  309.     TValue *array;
  310.     if (top >= t->asize) {
  311.       top = t->asize-1;
  312.       if (i > top)
  313.         break;
  314.     }
  315.     array = tvref(t->array);
  316.     for (n = 0; i <= top; i++)
  317.       if (!tvisnil(&array[i]))
  318.         n++;
  319.     bins[b] += n;
  320.     na += n;
  321.   }
  322.   return na;
  323. }

  324. static uint32_t counthash(const GCtab *t, uint32_t *bins, uint32_t *narray)
  325. {
  326.   uint32_t total, na, i, hmask = t->hmask;
  327.   Node *node = noderef(t->node);
  328.   for (total = na = 0, i = 0; i <= hmask; i++) {
  329.     Node *n = &node[i];
  330.     if (!tvisnil(&n->val)) {
  331.       na += countint(&n->key, bins);
  332.       total++;
  333.     }
  334.   }
  335.   *narray += na;
  336.   return total;
  337. }

  338. static uint32_t bestasize(uint32_t bins[], uint32_t *narray)
  339. {
  340.   uint32_t b, sum, na = 0, sz = 0, nn = *narray;
  341.   for (b = 0, sum = 0; 2*nn > (1u<<b) && sum != nn; b++)
  342.     if (bins[b] > 0 && 2*(sum += bins[b]) > (1u<<b)) {
  343.       sz = (2u<<b)+1;
  344.       na = sum;
  345.     }
  346.   *narray = sz;
  347.   return na;
  348. }

  349. static void rehashtab(lua_State *L, GCtab *t, cTValue *ek)
  350. {
  351.   uint32_t bins[LJ_MAX_ABITS];
  352.   uint32_t total, asize, na, i;
  353.   for (i = 0; i < LJ_MAX_ABITS; i++) bins[i] = 0;
  354.   asize = countarray(t, bins);
  355.   total = 1 + asize;
  356.   total += counthash(t, bins, &asize);
  357.   asize += countint(ek, bins);
  358.   na = bestasize(bins, &asize);
  359.   total -= na;
  360.   resizetab(L, t, asize, hsize2hbits(total));
  361. }

  362. #if LJ_HASFFI
  363. void lj_tab_rehash(lua_State *L, GCtab *t)
  364. {
  365.   rehashtab(L, t, niltv(L));
  366. }
  367. #endif

  368. void lj_tab_reasize(lua_State *L, GCtab *t, uint32_t nasize)
  369. {
  370.   resizetab(L, t, nasize+1, t->hmask > 0 ? lj_fls(t->hmask)+1 : 0);
  371. }

  372. /* -- Table getters ------------------------------------------------------- */

  373. cTValue * LJ_FASTCALL lj_tab_getinth(GCtab *t, int32_t key)
  374. {
  375.   TValue k;
  376.   Node *n;
  377.   k.n = (lua_Number)key;
  378.   n = hashnum(t, &k);
  379.   do {
  380.     if (tvisnum(&n->key) && n->key.n == k.n)
  381.       return &n->val;
  382.   } while ((n = nextnode(n)));
  383.   return NULL;
  384. }

  385. cTValue *lj_tab_getstr(GCtab *t, GCstr *key)
  386. {
  387.   Node *n = hashstr(t, key);
  388.   do {
  389.     if (tvisstr(&n->key) && strV(&n->key) == key)
  390.       return &n->val;
  391.   } while ((n = nextnode(n)));
  392.   return NULL;
  393. }

  394. cTValue *lj_tab_get(lua_State *L, GCtab *t, cTValue *key)
  395. {
  396.   if (tvisstr(key)) {
  397.     cTValue *tv = lj_tab_getstr(t, strV(key));
  398.     if (tv)
  399.       return tv;
  400.   } else if (tvisint(key)) {
  401.     cTValue *tv = lj_tab_getint(t, intV(key));
  402.     if (tv)
  403.       return tv;
  404.   } else if (tvisnum(key)) {
  405.     lua_Number nk = numV(key);
  406.     int32_t k = lj_num2int(nk);
  407.     if (nk == (lua_Number)k) {
  408.       cTValue *tv = lj_tab_getint(t, k);
  409.       if (tv)
  410.         return tv;
  411.     } else {
  412.       goto genlookup;  /* Else use the generic lookup. */
  413.     }
  414.   } else if (!tvisnil(key)) {
  415.     Node *n;
  416.   genlookup:
  417.     n = hashkey(t, key);
  418.     do {
  419.       if (lj_obj_equal(&n->key, key))
  420.         return &n->val;
  421.     } while ((n = nextnode(n)));
  422.   }
  423.   return niltv(L);
  424. }

  425. /* -- Table setters ------------------------------------------------------- */

  426. /* Insert new key. Use Brent's variation to optimize the chain length. */
  427. TValue *lj_tab_newkey(lua_State *L, GCtab *t, cTValue *key)
  428. {
  429.   Node *n = hashkey(t, key);
  430.   if (!tvisnil(&n->val) || t->hmask == 0) {
  431.     Node *nodebase = noderef(t->node);
  432.     Node *collide, *freenode = getfreetop(t, nodebase);
  433.     lua_assert(freenode >= nodebase && freenode <= nodebase+t->hmask+1);
  434.     do {
  435.       if (freenode == nodebase) {  /* No free node found? */
  436.         rehashtab(L, t, key);  /* Rehash table. */
  437.         return lj_tab_set(L, t, key);  /* Retry key insertion. */
  438.       }
  439.     } while (!tvisnil(&(--freenode)->key));
  440.     setfreetop(t, nodebase, freenode);
  441.     lua_assert(freenode != &G(L)->nilnode);
  442.     collide = hashkey(t, &n->key);
  443.     if (collide != n) {  /* Colliding node not the main node? */
  444.       while (noderef(collide->next) != n)  /* Find predecessor. */
  445.         collide = nextnode(collide);
  446.       setmref(collide->next, freenode);  /* Relink chain. */
  447.       /* Copy colliding node into free node and free main node. */
  448.       freenode->val = n->val;
  449.       freenode->key = n->key;
  450.       freenode->next = n->next;
  451.       setmref(n->next, NULL);
  452.       setnilV(&n->val);
  453.       /* Rechain pseudo-resurrected string keys with colliding hashes. */
  454.       while (nextnode(freenode)) {
  455.         Node *nn = nextnode(freenode);
  456.         if (tvisstr(&nn->key) && !tvisnil(&nn->val) &&
  457.             hashstr(t, strV(&nn->key)) == n) {
  458.           freenode->next = nn->next;
  459.           nn->next = n->next;
  460.           setmref(n->next, nn);
  461.         } else {
  462.           freenode = nn;
  463.         }
  464.       }
  465.     } else/* Otherwise use free node. */
  466.       setmrefr(freenode->next, n->next);  /* Insert into chain. */
  467.       setmref(n->next, freenode);
  468.       n = freenode;
  469.     }
  470.   }
  471.   n->key.u64 = key->u64;
  472.   if (LJ_UNLIKELY(tvismzero(&n->key)))
  473.     n->key.u64 = 0;
  474.   lj_gc_anybarriert(L, t);
  475.   lua_assert(tvisnil(&n->val));
  476.   return &n->val;
  477. }

  478. TValue *lj_tab_setinth(lua_State *L, GCtab *t, int32_t key)
  479. {
  480.   TValue k;
  481.   Node *n;
  482.   k.n = (lua_Number)key;
  483.   n = hashnum(t, &k);
  484.   do {
  485.     if (tvisnum(&n->key) && n->key.n == k.n)
  486.       return &n->val;
  487.   } while ((n = nextnode(n)));
  488.   return lj_tab_newkey(L, t, &k);
  489. }

  490. TValue *lj_tab_setstr(lua_State *L, GCtab *t, GCstr *key)
  491. {
  492.   TValue k;
  493.   Node *n = hashstr(t, key);
  494.   do {
  495.     if (tvisstr(&n->key) && strV(&n->key) == key)
  496.       return &n->val;
  497.   } while ((n = nextnode(n)));
  498.   setstrV(L, &k, key);
  499.   return lj_tab_newkey(L, t, &k);
  500. }

  501. TValue *lj_tab_set(lua_State *L, GCtab *t, cTValue *key)
  502. {
  503.   Node *n;
  504.   t->nomm = 0/* Invalidate negative metamethod cache. */
  505.   if (tvisstr(key)) {
  506.     return lj_tab_setstr(L, t, strV(key));
  507.   } else if (tvisint(key)) {
  508.     return lj_tab_setint(L, t, intV(key));
  509.   } else if (tvisnum(key)) {
  510.     lua_Number nk = numV(key);
  511.     int32_t k = lj_num2int(nk);
  512.     if (nk == (lua_Number)k)
  513.       return lj_tab_setint(L, t, k);
  514.     if (tvisnan(key))
  515.       lj_err_msg(L, LJ_ERR_NANIDX);
  516.     /* Else use the generic lookup. */
  517.   } else if (tvisnil(key)) {
  518.     lj_err_msg(L, LJ_ERR_NILIDX);
  519.   }
  520.   n = hashkey(t, key);
  521.   do {
  522.     if (lj_obj_equal(&n->key, key))
  523.       return &n->val;
  524.   } while ((n = nextnode(n)));
  525.   return lj_tab_newkey(L, t, key);
  526. }

  527. /* -- Table traversal ----------------------------------------------------- */

  528. /* Get the traversal index of a key. */
  529. static uint32_t keyindex(lua_State *L, GCtab *t, cTValue *key)
  530. {
  531.   TValue tmp;
  532.   if (tvisint(key)) {
  533.     int32_t k = intV(key);
  534.     if ((uint32_t)k < t->asize)
  535.       return (uint32_t)k;  /* Array key indexes: [0..t->asize-1] */
  536.     setnumV(&tmp, (lua_Number)k);
  537.     key = &tmp;
  538.   } else if (tvisnum(key)) {
  539.     lua_Number nk = numV(key);
  540.     int32_t k = lj_num2int(nk);
  541.     if ((uint32_t)k < t->asize && nk == (lua_Number)k)
  542.       return (uint32_t)k;  /* Array key indexes: [0..t->asize-1] */
  543.   }
  544.   if (!tvisnil(key)) {
  545.     Node *n = hashkey(t, key);
  546.     do {
  547.       if (lj_obj_equal(&n->key, key))
  548.         return t->asize + (uint32_t)(n - noderef(t->node));
  549.         /* Hash key indexes: [t->asize..t->asize+t->nmask] */
  550.     } while ((n = nextnode(n)));
  551.     if (key->u32.hi == 0xfffe7fff/* ITERN was despecialized while running. */
  552.       return key->u32.lo - 1;
  553.     lj_err_msg(L, LJ_ERR_NEXTIDX);
  554.     return 0/* unreachable */
  555.   }
  556.   return ~0u/* A nil key starts the traversal. */
  557. }

  558. /* Advance to the next step in a table traversal. */
  559. int lj_tab_next(lua_State *L, GCtab *t, TValue *key)
  560. {
  561.   uint32_t i = keyindex(L, t, key);  /* Find predecessor key index. */
  562.   for (i++; i < t->asize; i++)  /* First traverse the array keys. */
  563.     if (!tvisnil(arrayslot(t, i))) {
  564.       setintV(key, i);
  565.       copyTV(L, key+1, arrayslot(t, i));
  566.       return 1;
  567.     }
  568.   for (i -= t->asize; i <= t->hmask; i++) {  /* Then traverse the hash keys. */
  569.     Node *n = &noderef(t->node)[i];
  570.     if (!tvisnil(&n->val)) {
  571.       copyTV(L, key, &n->key);
  572.       copyTV(L, key+1, &n->val);
  573.       return 1;
  574.     }
  575.   }
  576.   return 0/* End of traversal. */
  577. }

  578. /* -- Table length calculation -------------------------------------------- */

  579. static MSize unbound_search(GCtab *t, MSize j)
  580. {
  581.   cTValue *tv;
  582.   MSize i = j;  /* i is zero or a present index */
  583.   j++;
  584.   /* find `i' and `j' such that i is present and j is not */
  585.   while ((tv = lj_tab_getint(t, (int32_t)j)) && !tvisnil(tv)) {
  586.     i = j;
  587.     j *= 2;
  588.     if (j > (MSize)(INT_MAX-2)) {  /* overflow? */
  589.       /* table was built with bad purposes: resort to linear search */
  590.       i = 1;
  591.       while ((tv = lj_tab_getint(t, (int32_t)i)) && !tvisnil(tv)) i++;
  592.       return i - 1;
  593.     }
  594.   }
  595.   /* now do a binary search between them */
  596.   while (j - i > 1) {
  597.     MSize m = (i+j)/2;
  598.     cTValue *tvb = lj_tab_getint(t, (int32_t)m);
  599.     if (tvb && !tvisnil(tvb)) i = m; else j = m;
  600.   }
  601.   return i;
  602. }

  603. /*
  604. ** Try to find a boundary in table `t'. A `boundary' is an integer index
  605. ** such that t[i] is non-nil and t[i+1] is nil (and 0 if t[1] is nil).
  606. */
  607. MSize LJ_FASTCALL lj_tab_len(GCtab *t)
  608. {
  609.   MSize j = (MSize)t->asize;
  610.   if (j > 1 && tvisnil(arrayslot(t, j-1))) {
  611.     MSize i = 1;
  612.     while (j - i > 1) {
  613.       MSize m = (i+j)/2;
  614.       if (tvisnil(arrayslot(t, m-1))) j = m; else i = m;
  615.     }
  616.     return i-1;
  617.   }
  618.   if (j) j--;
  619.   if (t->hmask <= 0)
  620.     return j;
  621.   return unbound_search(t, j);
  622. }