interplane.Get Omega
Read in your language

lua

[Ported from github.com/lua/lua] Cast in 'rl_readline_name'

Brought by drakestapleton

branch master Branches Tags History

lua / ltable.c

1355 lines · 43200 bytes Raw Blame History
1/*
2** $Id: ltable.c $
3** Lua tables (hash)
4** See Copyright Notice in lua.h
5*/
6
7#define ltable_c
8#define LUA_CORE
9
10#include "lprefix.h"
11
12
13/*
14** Implementation of tables (aka arrays, objects, or hash tables).
15** Tables keep its elements in two parts: an array part and a hash part.
16** Non-negative integer keys are all candidates to be kept in the array
17** part. The actual size of the array is the largest 'n' such that
18** more than half the slots between 1 and n are in use.
19** Hash uses a mix of chained scatter table with Brent's variation.
20** A main invariant of these tables is that, if an element is not
21** in its main position (i.e. the 'original' position that its hash gives
22** to it), then the colliding element is in its own main position.
23** Hence even when the load factor reaches 100%, performance remains good.
24*/
25
26#include <math.h>
27#include <limits.h>
28#include <string.h>
29
30#include "lua.h"
31
32#include "ldebug.h"
33#include "ldo.h"
34#include "lgc.h"
35#include "lmem.h"
36#include "lobject.h"
37#include "lstate.h"
38#include "lstring.h"
39#include "ltable.h"
40#include "lvm.h"
41
42
43/*
44** Only hash parts with at least 2^LIMFORLAST have a 'lastfree' field
45** that optimizes finding a free slot. That field is stored just before
46** the array of nodes, in the same block. Smaller tables do a complete
47** search when looking for a free slot.
48*/
49#define LIMFORLAST 3 /* log2 of real limit (8) */
50
51/*
52** The union 'Limbox' stores 'lastfree' and ensures that what follows it
53** is properly aligned to store a Node.
54*/
55typedef struct { Node *dummy; Node follows_pNode; } Limbox_aux;
56
57typedef union {
58 Node *lastfree;
59 char padding[offsetof(Limbox_aux, follows_pNode)];
60} Limbox;
61
62#define haslastfree(t) ((t)->lsizenode >= LIMFORLAST)
63#define getlastfree(t) ((cast(Limbox *, (t)->node) - 1)->lastfree)
64
65
66/*
67** MAXABITS is the largest integer such that 2^MAXABITS fits in an
68** unsigned int.
69*/
70#define MAXABITS (l_numbits(int) - 1)
71
72
73/*
74** MAXASIZEB is the maximum number of elements in the array part such
75** that the size of the array fits in 'size_t'.
76*/
77#define MAXASIZEB (MAX_SIZET/(sizeof(Value) + 1))
78
79
80/*
81** MAXASIZE is the maximum size of the array part. It is the minimum
82** between 2^MAXABITS and MAXASIZEB.
83*/
84#define MAXASIZE \
85 (((1u << MAXABITS) < MAXASIZEB) ? (1u << MAXABITS) : cast_uint(MAXASIZEB))
86
87/*
88** MAXHBITS is the largest integer such that 2^MAXHBITS fits in a
89** signed int.
90*/
91#define MAXHBITS (MAXABITS - 1)
92
93
94/*
95** MAXHSIZE is the maximum size of the hash part. It is the minimum
96** between 2^MAXHBITS and the maximum size such that, measured in bytes,
97** it fits in a 'size_t'.
98*/
99#define MAXHSIZE luaM_limitN(1 << MAXHBITS, Node)
100
101
102/*
103** When the original hash value is good, hashing by a power of 2
104** avoids the cost of '%'.
105*/
106#define hashpow2(t,n) (gnode(t, lmod((n), sizenode(t))))
107
108/*
109** for other types, it is better to avoid modulo by power of 2, as
110** they can have many 2 factors.
111*/
112#define hashmod(t,n) (gnode(t, ((n) % ((sizenode(t)-1u)|1u))))
113
114
115#define hashstr(t,str) hashpow2(t, (str)->hash)
116#define hashboolean(t,p) hashpow2(t, p)
117
118
119#define hashpointer(t,p) hashmod(t, point2uint(p))
120
121
122#define dummynode (&dummynode_)
123
124/*
125** Common hash part for tables with empty hash parts. That allows all
126** tables to have a hash part, avoiding an extra check ("is there a hash
127** part?") when indexing. Its sole node has an empty value and a key
128** (DEADKEY, NULL) that is different from any valid TValue.
129*/
130static const Node dummynode_ = {
131 {{NULL}, LUA_VEMPTY, /* value's value and type */
132 LUA_TDEADKEY, 0, {NULL}} /* key type, next, and key value */
133};
134
135
136static const TValue absentkey = {ABSTKEYCONSTANT};
137
138
139/*
140** Hash for integers. To allow a good hash, use the remainder operator
141** ('%'). If integer fits as a non-negative int, compute an int
142** remainder, which is faster. Otherwise, use an unsigned-integer
143** remainder, which uses all bits and ensures a non-negative result.
144*/
145static Node *hashint (const Table *t, lua_Integer i) {
146 lua_Unsigned ui = l_castS2U(i);
147 if (ui <= cast_uint(INT_MAX))
148 return gnode(t, cast_int(ui) % cast_int((sizenode(t)-1) | 1));
149 else
150 return hashmod(t, ui);
151}
152
153
154/*
155** Hash for floating-point numbers.
156** The main computation should be just
157** n = frexp(n, &i); return (n * INT_MAX) + i
158** but there are some numerical subtleties.
159** In a two-complement representation, INT_MAX may not have an exact
160** representation as a float, but INT_MIN does; because the absolute
161** value of 'frexp' is smaller than 1 (unless 'n' is inf/NaN), the
162** absolute value of the product 'frexp * -INT_MIN' is smaller or equal
163** to INT_MAX. Next, the use of 'unsigned int' avoids overflows when
164** adding 'i'; the use of '~u' (instead of '-u') avoids problems with
165** INT_MIN.
166*/
167#if !defined(l_hashfloat)
168static unsigned l_hashfloat (lua_Number n) {
169 int i;
170 lua_Integer ni;
171 n = l_mathop(frexp)(n, &i) * -cast_num(INT_MIN);
172 if (!lua_numbertointeger(n, &ni)) { /* is 'n' inf/-inf/NaN? */
173 lua_assert(luai_numisnan(n) || l_mathop(fabs)(n) == cast_num(HUGE_VAL));
174 return 0;
175 }
176 else { /* normal case */
177 unsigned int u = cast_uint(i) + cast_uint(ni);
178 return (u <= cast_uint(INT_MAX) ? u : ~u);
179 }
180}
181#endif
182
183
184/*
185** returns the 'main' position of an element in a table (that is,
186** the index of its hash value).
187*/
188static Node *mainpositionTV (const Table *t, const TValue *key) {
189 switch (ttypetag(key)) {
190 case LUA_VNUMINT: {
191 lua_Integer i = ivalue(key);
192 return hashint(t, i);
193 }
194 case LUA_VNUMFLT: {
195 lua_Number n = fltvalue(key);
196 return hashmod(t, l_hashfloat(n));
197 }
198 case LUA_VSHRSTR: {
199 TString *ts = tsvalue(key);
200 return hashstr(t, ts);
201 }
202 case LUA_VLNGSTR: {
203 TString *ts = tsvalue(key);
204 return hashpow2(t, luaS_hashlongstr(ts));
205 }
206 case LUA_VFALSE:
207 return hashboolean(t, 0);
208 case LUA_VTRUE:
209 return hashboolean(t, 1);
210 case LUA_VLIGHTUSERDATA: {
211 void *p = pvalue(key);
212 return hashpointer(t, p);
213 }
214 case LUA_VLCF: {
215 lua_CFunction f = fvalue(key);
216 return hashpointer(t, f);
217 }
218 default: {
219 GCObject *o = gcvalue(key);
220 return hashpointer(t, o);
221 }
222 }
223}
224
225
226l_sinline Node *mainpositionfromnode (const Table *t, Node *nd) {
227 TValue key;
228 getnodekey(cast(lua_State *, NULL), &key, nd);
229 return mainpositionTV(t, &key);
230}
231
232
233/*
234** Check whether key 'k1' is equal to the key in node 'n2'. This
235** equality is raw, so there are no metamethods. Floats with integer
236** values have been normalized, so integers cannot be equal to
237** floats. It is assumed that 'eqshrstr' is simply pointer equality,
238** so that short strings are handled in the default case. The flag
239** 'deadok' means to accept dead keys as equal to their original values.
240** (Only collectable objects can produce dead keys.) Note that dead
241** long strings are also compared by identity. Once a key is dead,
242** its corresponding value may be collected, and then another value
243** can be created with the same address. If this other value is given
244** to 'next', 'equalkey' will signal a false positive. In a regular
245** traversal, this situation should never happen, as all keys given to
246** 'next' came from the table itself, and therefore could not have been
247** collected. Outside a regular traversal, we have garbage in, garbage
248** out. What is relevant is that this false positive does not break
249** anything. (In particular, 'next' will return some other valid item
250** on the table or nil.)
251*/
252static int equalkey (const TValue *k1, const Node *n2, int deadok) {
253 if (rawtt(k1) != keytt(n2)) { /* not the same variants? */
254 if (keyisshrstr(n2) && ttislngstring(k1)) {
255 /* an external string can be equal to a short-string key */
256 return luaS_eqstr(tsvalue(k1), keystrval(n2));
257 }
258 else if (deadok && keyisdead(n2) && iscollectable(k1)) {
259 /* a collectable value can be equal to a dead key */
260 return gcvalue(k1) == gcvalueraw(keyval(n2));
261 }
262 else
263 return 0; /* otherwise, different variants cannot be equal */
264 }
265 else { /* equal variants */
266 switch (keytt(n2)) {
267 case LUA_VNIL: case LUA_VFALSE: case LUA_VTRUE:
268 return 1;
269 case LUA_VNUMINT:
270 return (ivalue(k1) == keyival(n2));
271 case LUA_VNUMFLT:
272 return luai_numeq(fltvalue(k1), fltvalueraw(keyval(n2)));
273 case LUA_VLIGHTUSERDATA:
274 return pvalue(k1) == pvalueraw(keyval(n2));
275 case LUA_VLCF:
276 return fvalue(k1) == fvalueraw(keyval(n2));
277 case ctb(LUA_VLNGSTR):
278 return luaS_eqstr(tsvalue(k1), keystrval(n2));
279 default:
280 return gcvalue(k1) == gcvalueraw(keyval(n2));
281 }
282 }
283}
284
285
286/*
287** "Generic" get version. (Not that generic: not valid for integers,
288** which may be in array part, nor for floats with integral values.)
289** See explanation about 'deadok' in function 'equalkey'.
290*/
291static const TValue *getgeneric (Table *t, const TValue *key, int deadok) {
292 Node *n = mainpositionTV(t, key);
293 for (;;) { /* check whether 'key' is somewhere in the chain */
294 if (equalkey(key, n, deadok))
295 return gval(n); /* that's it */
296 else {
297 int nx = gnext(n);
298 if (nx == 0)
299 return &absentkey; /* not found */
300 n += nx;
301 }
302 }
303}
304
305
306/*
307** Return the index 'k' (converted to an unsigned) if it is inside
308** the range [1, limit].
309*/
310static unsigned checkrange (lua_Integer k, unsigned limit) {
311 return (l_castS2U(k) - 1u < limit) ? cast_uint(k) : 0;
312}
313
314
315/*
316** Return the index 'k' if 'k' is an appropriate key to live in the
317** array part of a table, 0 otherwise.
318*/
319#define arrayindex(k) checkrange(k, MAXASIZE)
320
321
322/*
323** Check whether an integer key is in the array part of a table and
324** return its index there, or zero.
325*/
326#define ikeyinarray(t,k) checkrange(k, t->asize)
327
328
329/*
330** Check whether a key is in the array part of a table and return its
331** index there, or zero.
332*/
333static unsigned keyinarray (Table *t, const TValue *key) {
334 return (ttisinteger(key)) ? ikeyinarray(t, ivalue(key)) : 0;
335}
336
337
338/*
339** returns the index of a 'key' for table traversals. First goes all
340** elements in the array part, then elements in the hash part. The
341** beginning of a traversal is signaled by 0.
342*/
343static unsigned findindex (lua_State *L, Table *t, TValue *key,
344 unsigned asize) {
345 unsigned int i;
346 if (ttisnil(key)) return 0; /* first iteration */
347 i = keyinarray(t, key);
348 if (i != 0) /* is 'key' inside array part? */
349 return i; /* yes; that's the index */
350 else {
351 const TValue *n = getgeneric(t, key, 1);
352 if (l_unlikely(isabstkey(n)))
353 luaG_runerror(L, "invalid key to 'next'"); /* key not found */
354 i = cast_uint(nodefromval(n) - gnode(t, 0)); /* key index in hash table */
355 /* hash elements are numbered after array ones */
356 return (i + 1) + asize;
357 }
358}
359
360
361int luaH_next (lua_State *L, Table *t, StkId key) {
362 unsigned int asize = t->asize;
363 unsigned int i = findindex(L, t, s2v(key), asize); /* find original key */
364 for (; i < asize; i++) { /* try first array part */
365 lu_byte tag = *getArrTag(t, i);
366 if (!tagisempty(tag)) { /* a non-empty entry? */
367 setivalue(s2v(key), cast_int(i) + 1);
368 farr2val(t, i, tag, s2v(key + 1));
369 return 1;
370 }
371 }
372 for (i -= asize; i < sizenode(t); i++) { /* hash part */
373 if (!isempty(gval(gnode(t, i)))) { /* a non-empty entry? */
374 Node *n = gnode(t, i);
375 getnodekey(L, s2v(key), n);
376 setobj2s(L, key + 1, gval(n));
377 return 1;
378 }
379 }
380 return 0; /* no more elements */
381}
382
383
384/* Extra space in Node array if it has a lastfree entry */
385#define extraLastfree(t) (haslastfree(t) ? sizeof(Limbox) : 0)
386
387/* 'node' size in bytes */
388static size_t sizehash (Table *t) {
389 return cast_sizet(sizenode(t)) * sizeof(Node) + extraLastfree(t);
390}
391
392
393static void freehash (lua_State *L, Table *t) {
394 if (!isdummy(t)) {
395 /* get pointer to the beginning of Node array */
396 char *arr = cast_charp(t->node) - extraLastfree(t);
397 luaM_freearray(L, arr, sizehash(t));
398 }
399}
400
401
402/*
403** {=============================================================
404** Rehash
405** ==============================================================
406*/
407
408static int insertkey (Table *t, const TValue *key, TValue *value);
409static void newcheckedkey (Table *t, const TValue *key, TValue *value);
410
411
412/*
413** Structure to count the keys in a table.
414** 'total' is the total number of keys in the table.
415** 'na' is the number of *array indices* in the table (see 'arrayindex').
416** 'deleted' is true if there are deleted nodes in the hash part.
417** 'nums' is a "count array" where 'nums[i]' is the number of integer
418** keys between 2^(i - 1) + 1 and 2^i. Note that 'na' is the summation
419** of 'nums'.
420*/
421typedef struct {
422 unsigned total;
423 unsigned na;
424 int deleted;
425 unsigned nums[MAXABITS + 1];
426} Counters;
427
428
429/*
430** Check whether it is worth to use 'na' array entries instead of 'nh'
431** hash nodes. (A hash node uses ~3 times more memory than an array
432** entry: Two values plus 'next' versus one value.) Evaluate with size_t
433** to avoid overflows.
434*/
435#define arrayXhash(na,nh) (cast_sizet(na) <= cast_sizet(nh) * 3)
436
437/*
438** Compute the optimal size for the array part of table 't'.
439** This size maximizes the number of elements going to the array part
440** while satisfying the condition 'arrayXhash' with the use of memory if
441** all those elements went to the hash part.
442** 'ct->na' enters with the total number of array indices in the table
443** and leaves with the number of keys that will go to the array part;
444** return the optimal size for the array part.
445*/
446static unsigned computesizes (Counters *ct) {
447 int i;
448 unsigned int twotoi; /* 2^i (candidate for optimal size) */
449 unsigned int a = 0; /* number of elements smaller than 2^i */
450 unsigned int na = 0; /* number of elements to go to array part */
451 unsigned int optimal = 0; /* optimal size for array part */
452 /* traverse slices while 'twotoi' does not overflow and total of array
453 indices still can satisfy 'arrayXhash' against the array size */
454 for (i = 0, twotoi = 1;
455 twotoi > 0 && arrayXhash(twotoi, ct->na);
456 i++, twotoi *= 2) {
457 unsigned nums = ct->nums[i];
458 a += nums;
459 if (nums > 0 && /* grows array only if it gets more elements... */
460 arrayXhash(twotoi, a)) { /* ...while using "less memory" */
461 optimal = twotoi; /* optimal size (till now) */
462 na = a; /* all elements up to 'optimal' will go to array part */
463 }
464 }
465 ct->na = na;
466 return optimal;
467}
468
469
470static void countint (lua_Integer key, Counters *ct) {
471 unsigned int k = arrayindex(key);
472 if (k != 0) { /* is 'key' an array index? */
473 ct->nums[luaO_ceillog2(k)]++; /* count as such */
474 ct->na++;
475 }
476}
477
478
479l_sinline int arraykeyisempty (const Table *t, unsigned key) {
480 int tag = *getArrTag(t, key - 1);
481 return tagisempty(tag);
482}
483
484
485/*
486** Count keys in array part of table 't'.
487*/
488static void numusearray (const Table *t, Counters *ct) {
489 int lg;
490 unsigned int ttlg; /* 2^lg */
491 unsigned int ause = 0; /* summation of 'nums' */
492 unsigned int i = 1; /* index to traverse all array keys */
493 unsigned int asize = t->asize;
494 /* traverse each slice */
495 for (lg = 0, ttlg = 1; lg <= MAXABITS; lg++, ttlg *= 2) {
496 unsigned int lc = 0; /* counter */
497 unsigned int lim = ttlg;
498 if (lim > asize) {
499 lim = asize; /* adjust upper limit */
500 if (i > lim)
501 break; /* no more elements to count */
502 }
503 /* count elements in range (2^(lg - 1), 2^lg] */
504 for (; i <= lim; i++) {
505 if (!arraykeyisempty(t, i))
506 lc++;
507 }
508 ct->nums[lg] += lc;
509 ause += lc;
510 }
511 ct->total += ause;
512 ct->na += ause;
513}
514
515
516/*
517** Count keys in hash part of table 't'. As this only happens during
518** a rehash, all nodes have been used. A node can have a nil value only
519** if it was deleted after being created.
520*/
521static void numusehash (const Table *t, Counters *ct) {
522 unsigned i = sizenode(t);
523 unsigned total = 0;
524 while (i--) {
525 Node *n = &t->node[i];
526 if (isempty(gval(n))) {
527 lua_assert(!keyisnil(n)); /* entry was deleted; key cannot be nil */
528 ct->deleted = 1;
529 }
530 else {
531 total++;
532 if (keyisinteger(n))
533 countint(keyival(n), ct);
534 }
535 }
536 ct->total += total;
537}
538
539
540/*
541** Convert an "abstract size" (number of slots in an array) to
542** "concrete size" (number of bytes in the array).
543*/
544static size_t concretesize (unsigned int size) {
545 if (size == 0)
546 return 0;
547 else /* space for the two arrays plus an unsigned in between */
548 return size * (sizeof(Value) + 1) + sizeof(unsigned);
549}
550
551
552/*
553** Resize the array part of a table. If new size is equal to the old,
554** do nothing. Else, if new size is zero, free the old array. (It must
555** be present, as the sizes are different.) Otherwise, allocate a new
556** array, move the common elements to new proper position, and then
557** frees the old array.
558** We could reallocate the array, but we still would need to move the
559** elements to their new position, so the copy implicit in realloc is a
560** waste. Moreover, most allocators will move the array anyway when the
561** new size is double the old one (the most common case).
562*/
563static Value *resizearray (lua_State *L , Table *t,
564 unsigned oldasize,
565 unsigned newasize) {
566 if (oldasize == newasize)
567 return t->array; /* nothing to be done */
568 else if (newasize == 0) { /* erasing array? */
569 Value *op = t->array - oldasize; /* original array's real address */
570 luaM_freemem(L, op, concretesize(oldasize)); /* free it */
571 return NULL;
572 }
573 else {
574 size_t newasizeb = concretesize(newasize);
575 Value *np = cast(Value *,
576 luaM_reallocvector(L, NULL, 0, newasizeb, lu_byte));
577 if (np == NULL) /* allocation error? */
578 return NULL;
579 np += newasize; /* shift pointer to the end of value segment */
580 if (oldasize > 0) {
581 /* move common elements to new position */
582 size_t oldasizeb = concretesize(oldasize);
583 Value *op = t->array; /* original array */
584 unsigned tomove = (oldasize < newasize) ? oldasize : newasize;
585 size_t tomoveb = (oldasize < newasize) ? oldasizeb : newasizeb;
586 lua_assert(tomoveb > 0);
587 memcpy(np - tomove, op - tomove, tomoveb);
588 luaM_freemem(L, op - oldasize, oldasizeb); /* free old block */
589 }
590 return np;
591 }
592}
593
594
595/*
596** Creates an array for the hash part of a table with the given
597** size, or reuses the dummy node if size is zero.
598** The computation for size overflow is in two steps: the first
599** comparison ensures that the shift in the second one does not
600** overflow.
601*/
602static void setnodevector (lua_State *L, Table *t, unsigned size) {
603 if (size == 0) { /* no elements to hash part? */
604 t->node = cast(Node *, dummynode); /* use common 'dummynode' */
605 t->lsizenode = 0;
606 setdummy(t); /* signal that it is using dummy node */
607 }
608 else {
609 int i;
610 int lsize = luaO_ceillog2(size);
611 if (lsize > MAXHBITS || (1 << lsize) > MAXHSIZE)
612 luaG_runerror(L, "table overflow");
613 size = twoto(lsize);
614 if (lsize < LIMFORLAST) /* no 'lastfree' field? */
615 t->node = luaM_newvector(L, size, Node);
616 else {
617 size_t bsize = size * sizeof(Node) + sizeof(Limbox);
618 char *node = luaM_newblock(L, bsize);
619 t->node = cast(Node *, node + sizeof(Limbox));
620 getlastfree(t) = gnode(t, size); /* all positions are free */
621 }
622 t->lsizenode = cast_byte(lsize);
623 setnodummy(t);
624 for (i = 0; i < cast_int(size); i++) {
625 Node *n = gnode(t, i);
626 gnext(n) = 0;
627 setnilkey(n);
628 setempty(gval(n));
629 }
630 }
631}
632
633
634/*
635** (Re)insert all elements from the hash part of 'ot' into table 't'.
636*/
637static void reinserthash (lua_State *L, Table *ot, Table *t) {
638 unsigned j;
639 unsigned size = sizenode(ot);
640 for (j = 0; j < size; j++) {
641 Node *old = gnode(ot, j);
642 if (!isempty(gval(old))) {
643 /* doesn't need barrier/invalidate cache, as entry was
644 already present in the table */
645 TValue k;
646 getnodekey(L, &k, old);
647 newcheckedkey(t, &k, gval(old));
648 }
649 }
650}
651
652
653/*
654** Exchange the hash part of 't1' and 't2'. (In 'flags', only the dummy
655** bit must be exchanged: The metamethod bits do not change during a
656** resize, so the "real" table can keep their values.)
657*/
658static void exchangehashpart (Table *t1, Table *t2) {
659 lu_byte lsizenode = t1->lsizenode;
660 Node *node = t1->node;
661 int bitdummy1 = t1->flags & BITDUMMY;
662 t1->lsizenode = t2->lsizenode;
663 t1->node = t2->node;
664 t1->flags = cast_byte((t1->flags & NOTBITDUMMY) | (t2->flags & BITDUMMY));
665 t2->lsizenode = lsizenode;
666 t2->node = node;
667 t2->flags = cast_byte((t2->flags & NOTBITDUMMY) | bitdummy1);
668}
669
670
671/*
672** Re-insert into the new hash part of a table the elements from the
673** vanishing slice of the array part.
674*/
675static void reinsertOldSlice (Table *t, unsigned oldasize,
676 unsigned newasize) {
677 unsigned i;
678 for (i = newasize; i < oldasize; i++) { /* traverse vanishing slice */
679 lu_byte tag = *getArrTag(t, i);
680 if (!tagisempty(tag)) { /* a non-empty entry? */
681 TValue key, aux;
682 setivalue(&key, l_castU2S(i) + 1); /* make the key */
683 farr2val(t, i, tag, &aux); /* copy value into 'aux' */
684 insertkey(t, &key, &aux); /* insert entry into the hash part */
685 }
686 }
687}
688
689
690/*
691** Clear new slice of the array.
692*/
693static void clearNewSlice (Table *t, unsigned oldasize, unsigned newasize) {
694 for (; oldasize < newasize; oldasize++)
695 *getArrTag(t, oldasize) = LUA_VEMPTY;
696}
697
698
699/*
700** Resize table 't' for the new given sizes. Both allocations (for
701** the hash part and for the array part) can fail, which creates some
702** subtleties. If the first allocation, for the hash part, fails, an
703** error is raised and that is it. Otherwise, it copies the elements from
704** the shrinking part of the array (if it is shrinking) into the new
705** hash. Then it reallocates the array part. If that fails, the table
706** is in its original state; the function frees the new hash part and then
707** raises the allocation error. Otherwise, it sets the new hash part
708** into the table, initializes the new part of the array (if any) with
709** nils and reinserts the elements of the old hash back into the new
710** parts of the table.
711** Note that if the new size for the array part ('newasize') is equal to
712** the old one ('oldasize'), this function will do nothing with that
713** part.
714*/
715void luaH_resize (lua_State *L, Table *t, unsigned newasize,
716 unsigned nhsize) {
717 Table newt; /* to keep the new hash part */
718 unsigned oldasize = t->asize;
719 Value *newarray;
720 if (newasize > MAXASIZE)
721 luaG_runerror(L, "table overflow");
722 /* create new hash part with appropriate size into 'newt' */
723 newt.flags = 0;
724 setnodevector(L, &newt, nhsize);
725 if (newasize < oldasize) { /* will array shrink? */
726 /* re-insert into the new hash the elements from vanishing slice */
727 exchangehashpart(t, &newt); /* pretend table has new hash */
728 reinsertOldSlice(t, oldasize, newasize);
729 exchangehashpart(t, &newt); /* restore old hash (in case of errors) */
730 }
731 /* allocate new array */
732 newarray = resizearray(L, t, oldasize, newasize);
733 if (l_unlikely(newarray == NULL && newasize > 0)) { /* allocation failed? */
734 freehash(L, &newt); /* release new hash part */
735 luaM_error(L); /* raise error (with array unchanged) */
736 }
737 /* allocation ok; initialize new part of the array */
738 exchangehashpart(t, &newt); /* 't' has the new hash ('newt' has the old) */
739 t->array = newarray; /* set new array part */
740 t->asize = newasize;
741 if (newarray != NULL)
742 *lenhint(t) = newasize / 2u; /* set an initial hint */
743 clearNewSlice(t, oldasize, newasize);
744 /* re-insert elements from old hash part into new parts */
745 reinserthash(L, &newt, t); /* 'newt' now has the old hash */
746 freehash(L, &newt); /* free old hash part */
747}
748
749
750void luaH_resizearray (lua_State *L, Table *t, unsigned int nasize) {
751 unsigned nsize = allocsizenode(t);
752 luaH_resize(L, t, nasize, nsize);
753}
754
755
756/*
757** Rehash a table. First, count its keys. If there are array indices
758** outside the array part, compute the new best size for that part.
759** Then, resize the table.
760*/
761static void rehash (lua_State *L, Table *t, const TValue *ek) {
762 unsigned asize; /* optimal size for array part */
763 Counters ct;
764 unsigned i;
765 unsigned nsize; /* size for the hash part */
766 /* reset counts */
767 for (i = 0; i <= MAXABITS; i++) ct.nums[i] = 0;
768 ct.na = 0;
769 ct.deleted = 0;
770 ct.total = 1; /* count extra key */
771 if (ttisinteger(ek))
772 countint(ivalue(ek), &ct); /* extra key may go to array */
773 numusehash(t, &ct); /* count keys in hash part */
774 if (ct.na == 0) {
775 /* no new keys to enter array part; keep it with the same size */
776 asize = t->asize;
777 }
778 else { /* compute best size for array part */
779 numusearray(t, &ct); /* count keys in array part */
780 asize = computesizes(&ct); /* compute new size for array part */
781 }
782 /* all keys not in the array part go to the hash part */
783 nsize = ct.total - ct.na;
784 if (ct.deleted) { /* table has deleted entries? */
785 /* insertion-deletion-insertion: give hash some extra size to
786 avoid repeated resizings */
787 nsize += nsize >> 2;
788 }
789 /* resize the table to new computed sizes */
790 luaH_resize(L, t, asize, nsize);
791}
792
793/*
794** }=============================================================
795*/
796
797
798Table *luaH_new (lua_State *L) {
799 GCObject *o = luaC_newobj(L, LUA_VTABLE, sizeof(Table));
800 Table *t = gco2t(o);
801 t->metatable = NULL;
802 t->flags = maskflags; /* table has no metamethod fields */
803 t->array = NULL;
804 t->asize = 0;
805 setnodevector(L, t, 0);
806 return t;
807}
808
809
810lu_mem luaH_size (Table *t) {
811 lu_mem sz = cast(lu_mem, sizeof(Table)) + concretesize(t->asize);
812 if (!isdummy(t))
813 sz += sizehash(t);
814 return sz;
815}
816
817
818/*
819** Frees a table.
820*/
821void luaH_free (lua_State *L, Table *t) {
822 freehash(L, t);
823 resizearray(L, t, t->asize, 0);
824 luaM_free(L, t);
825}
826
827
828static Node *getfreepos (Table *t) {
829 if (haslastfree(t)) { /* does it have 'lastfree' information? */
830 /* look for a spot before 'lastfree', updating 'lastfree' */
831 while (getlastfree(t) > t->node) {
832 Node *free = --getlastfree(t);
833 if (keyisnil(free))
834 return free;
835 }
836 }
837 else { /* no 'lastfree' information */
838 unsigned i = sizenode(t);
839 while (i--) { /* do a linear search */
840 Node *free = gnode(t, i);
841 if (keyisnil(free))
842 return free;
843 }
844 }
845 return NULL; /* could not find a free place */
846}
847
848
849
850/*
851** Inserts a new key into a hash table; first, check whether key's main
852** position is free. If not, check whether colliding node is in its main
853** position or not: if it is not, move colliding node to an empty place
854** and put new key in its main position; otherwise (colliding node is in
855** its main position), new key goes to an empty position. Return 0 if
856** could not insert key (could not find a free space).
857*/
858static int insertkey (Table *t, const TValue *key, TValue *value) {
859 Node *mp = mainpositionTV(t, key);
860 /* table cannot already contain the key */
861 lua_assert(isabstkey(getgeneric(t, key, 0)));
862 if (!isempty(gval(mp)) || isdummy(t)) { /* main position is taken? */
863 Node *othern;
864 Node *f = getfreepos(t); /* get a free place */
865 if (f == NULL) /* cannot find a free place? */
866 return 0;
867 lua_assert(!isdummy(t));
868 othern = mainpositionfromnode(t, mp);
869 if (othern != mp) { /* is colliding node out of its main position? */
870 /* yes; move colliding node into free position */
871 while (othern + gnext(othern) != mp) /* find previous */
872 othern += gnext(othern);
873 gnext(othern) = cast_int(f - othern); /* rechain to point to 'f' */
874 *f = *mp; /* copy colliding node into free pos. (mp->next also goes) */
875 if (gnext(mp) != 0) {
876 gnext(f) += cast_int(mp - f); /* correct 'next' */
877 gnext(mp) = 0; /* now 'mp' is free */
878 }
879 setempty(gval(mp));
880 }
881 else { /* colliding node is in its own main position */
882 /* new node will go into free position */
883 if (gnext(mp) != 0)
884 gnext(f) = cast_int((mp + gnext(mp)) - f); /* chain new position */
885 else lua_assert(gnext(f) == 0);
886 gnext(mp) = cast_int(f - mp);
887 mp = f;
888 }
889 }
890 setnodekey(mp, key);
891 lua_assert(isempty(gval(mp)));
892 setobj2t(cast(lua_State *, 0), gval(mp), value);
893 return 1;
894}
895
896
897/*
898** Insert a key in a table where there is space for that key, the
899** key is valid, and the value is not nil.
900*/
901static void newcheckedkey (Table *t, const TValue *key, TValue *value) {
902 unsigned i = keyinarray(t, key);
903 if (i > 0) /* is key in the array part? */
904 obj2arr(t, i - 1, value); /* set value in the array */
905 else {
906 int done = insertkey(t, key, value); /* insert key in the hash part */
907 lua_assert(done); /* it cannot fail */
908 cast(void, done); /* to avoid warnings */
909 }
910}
911
912
913static void luaH_newkey (lua_State *L, Table *t, const TValue *key,
914 TValue *value) {
915 if (!ttisnil(value)) { /* do not insert nil values */
916 int done = insertkey(t, key, value);
917 if (!done) { /* could not find a free place? */
918 rehash(L, t, key); /* grow table */
919 newcheckedkey(t, key, value); /* insert key in grown table */
920 }
921 luaC_barrierback(L, obj2gco(t), key);
922 /* for debugging only: any new key may force an emergency collection */
923 condchangemem(L, (void)0, (void)0, 1);
924 }
925}
926
927
928static const TValue *getintfromhash (Table *t, lua_Integer key) {
929 Node *n = hashint(t, key);
930 lua_assert(!ikeyinarray(t, key));
931 for (;;) { /* check whether 'key' is somewhere in the chain */
932 if (keyisinteger(n) && keyival(n) == key)
933 return gval(n); /* that's it */
934 else {
935 int nx = gnext(n);
936 if (nx == 0) break;
937 n += nx;
938 }
939 }
940 return &absentkey;
941}
942
943
944static int hashkeyisempty (Table *t, lua_Unsigned key) {
945 const TValue *val = getintfromhash(t, l_castU2S(key));
946 return isempty(val);
947}
948
949
950static lu_byte finishnodeget (const TValue *val, TValue *res) {
951 if (!ttisnil(val)) {
952 setobj(((lua_State*)NULL), res, val);
953 }
954 return ttypetag(val);
955}
956
957
958lu_byte luaH_getint (Table *t, lua_Integer key, TValue *res) {
959 unsigned k = ikeyinarray(t, key);
960 if (k > 0) {
961 lu_byte tag = *getArrTag(t, k - 1);
962 if (!tagisempty(tag))
963 farr2val(t, k - 1, tag, res);
964 return tag;
965 }
966 else
967 return finishnodeget(getintfromhash(t, key), res);
968}
969
970
971/*
972** search function for short strings
973*/
974const TValue *luaH_Hgetshortstr (Table *t, TString *key) {
975 Node *n = hashstr(t, key);
976 lua_assert(strisshr(key));
977 for (;;) { /* check whether 'key' is somewhere in the chain */
978 if (keyisshrstr(n) && eqshrstr(keystrval(n), key))
979 return gval(n); /* that's it */
980 else {
981 int nx = gnext(n);
982 if (nx == 0)
983 return &absentkey; /* not found */
984 n += nx;
985 }
986 }
987}
988
989
990lu_byte luaH_getshortstr (Table *t, TString *key, TValue *res) {
991 return finishnodeget(luaH_Hgetshortstr(t, key), res);
992}
993
994
995static const TValue *Hgetlongstr (Table *t, TString *key) {
996 TValue ko;
997 lua_assert(!strisshr(key));
998 setsvalue(cast(lua_State *, NULL), &ko, key);
999 return getgeneric(t, &ko, 0); /* for long strings, use generic case */
1000}
1001
1002
1003static const TValue *Hgetstr (Table *t, TString *key) {
1004 if (strisshr(key))
1005 return luaH_Hgetshortstr(t, key);
1006 else
1007 return Hgetlongstr(t, key);
1008}
1009
1010
1011lu_byte luaH_getstr (Table *t, TString *key, TValue *res) {
1012 return finishnodeget(Hgetstr(t, key), res);
1013}
1014
1015
1016/*
1017** main search function
1018*/
1019lu_byte luaH_get (Table *t, const TValue *key, TValue *res) {
1020 const TValue *slot;
1021 switch (ttypetag(key)) {
1022 case LUA_VSHRSTR:
1023 slot = luaH_Hgetshortstr(t, tsvalue(key));
1024 break;
1025 case LUA_VNUMINT:
1026 return luaH_getint(t, ivalue(key), res);
1027 case LUA_VNIL:
1028 slot = &absentkey;
1029 break;
1030 case LUA_VNUMFLT: {
1031 lua_Integer k;
1032 if (luaV_flttointeger(fltvalue(key), &k, F2Ieq)) /* integral index? */
1033 return luaH_getint(t, k, res); /* use specialized version */
1034 /* else... */
1035 } /* FALLTHROUGH */
1036 default:
1037 slot = getgeneric(t, key, 0);
1038 break;
1039 }
1040 return finishnodeget(slot, res);
1041}
1042
1043
1044/*
1045** When a 'pset' cannot be completed, this function returns an encoding
1046** of its result, to be used by 'luaH_finishset'.
1047*/
1048static int retpsetcode (Table *t, const TValue *slot) {
1049 if (isabstkey(slot))
1050 return HNOTFOUND; /* no slot with that key */
1051 else /* return node encoded */
1052 return cast_int((cast(Node*, slot) - t->node)) + HFIRSTNODE;
1053}
1054
1055
1056static int finishnodeset (Table *t, const TValue *slot, TValue *val) {
1057 if (!ttisnil(slot)) {
1058 setobj(((lua_State*)NULL), cast(TValue*, slot), val);
1059 return HOK; /* success */
1060 }
1061 else
1062 return retpsetcode(t, slot);
1063}
1064
1065
1066static int rawfinishnodeset (const TValue *slot, TValue *val) {
1067 if (isabstkey(slot))
1068 return 0; /* no slot with that key */
1069 else {
1070 setobj(((lua_State*)NULL), cast(TValue*, slot), val);
1071 return 1; /* success */
1072 }
1073}
1074
1075
1076int luaH_psetint (Table *t, lua_Integer key, TValue *val) {
1077 lua_assert(!ikeyinarray(t, key));
1078 return finishnodeset(t, getintfromhash(t, key), val);
1079}
1080
1081
1082static int psetint (Table *t, lua_Integer key, TValue *val) {
1083 int hres;
1084 luaH_fastseti(t, key, val, hres);
1085 return hres;
1086}
1087
1088
1089/*
1090** This function could be just this:
1091** return finishnodeset(t, luaH_Hgetshortstr(t, key), val);
1092** However, it optimizes the common case created by constructors (e.g.,
1093** {x=1, y=2}), which creates a key in a table that has no metatable,
1094** it is not old/black, and it already has space for the key.
1095*/
1096
1097int luaH_psetshortstr (Table *t, TString *key, TValue *val) {
1098 const TValue *slot = luaH_Hgetshortstr(t, key);
1099 if (!ttisnil(slot)) { /* key already has a value? (all too common) */
1100 setobj(((lua_State*)NULL), cast(TValue*, slot), val); /* update it */
1101 return HOK; /* done */
1102 }
1103 else if (checknoTM(t->metatable, TM_NEWINDEX)) { /* no metamethod? */
1104 if (ttisnil(val)) /* new value is nil? */
1105 return HOK; /* done (value is already nil/absent) */
1106 if (isabstkey(slot) && /* key is absent? */
1107 !(isblack(t) && iswhite(key))) { /* and don't need barrier? */
1108 TValue tk; /* key as a TValue */
1109 setsvalue(cast(lua_State *, NULL), &tk, key);
1110 if (insertkey(t, &tk, val)) { /* insert key, if there is space */
1111 invalidateTMcache(t);
1112 return HOK;
1113 }
1114 }
1115 }
1116 /* Else, either table has new-index metamethod, or it needs barrier,
1117 or it needs to rehash for the new key. In any of these cases, the
1118 operation cannot be completed here. Return a code for the caller. */
1119 return retpsetcode(t, slot);
1120}
1121
1122
1123int luaH_psetstr (Table *t, TString *key, TValue *val) {
1124 if (strisshr(key))
1125 return luaH_psetshortstr(t, key, val);
1126 else
1127 return finishnodeset(t, Hgetlongstr(t, key), val);
1128}
1129
1130
1131int luaH_pset (Table *t, const TValue *key, TValue *val) {
1132 switch (ttypetag(key)) {
1133 case LUA_VSHRSTR: return luaH_psetshortstr(t, tsvalue(key), val);
1134 case LUA_VNUMINT: return psetint(t, ivalue(key), val);
1135 case LUA_VNIL: return HNOTFOUND;
1136 case LUA_VNUMFLT: {
1137 lua_Integer k;
1138 if (luaV_flttointeger(fltvalue(key), &k, F2Ieq)) /* integral index? */
1139 return psetint(t, k, val); /* use specialized version */
1140 /* else... */
1141 } /* FALLTHROUGH */
1142 default:
1143 return finishnodeset(t, getgeneric(t, key, 0), val);
1144 }
1145}
1146
1147/*
1148** Finish a raw "set table" operation, where 'hres' encodes where the
1149** value should have been (the result of a previous 'pset' operation).
1150** Beware: when using this function the caller probably need to check a
1151** GC barrier and invalidate the TM cache.
1152*/
1153void luaH_finishset (lua_State *L, Table *t, const TValue *key,
1154 TValue *value, int hres) {
1155 lua_assert(hres != HOK);
1156 if (hres == HNOTFOUND) {
1157 TValue aux;
1158 const TValue *actk = key; /* actual key to insert */
1159 if (l_unlikely(ttisnil(key)))
1160 luaG_runerror(L, "table index is nil");
1161 else if (ttisfloat(key)) {
1162 lua_Number f = fltvalue(key);
1163 lua_Integer k;
1164 if (luaV_flttointeger(f, &k, F2Ieq)) { /* is key equal to an integer? */
1165 setivalue(&aux, k);
1166 actk = &aux; /* use the integer as the key */
1167 }
1168 else if (l_unlikely(luai_numisnan(f)))
1169 luaG_runerror(L, "table index is NaN");
1170 }
1171 else if (isextstr(key)) { /* external string? */
1172 /* If string is short, must internalize it to be used as table key */
1173 TString *ts = luaS_normstr(L, tsvalue(key));
1174 setsvalue2s(L, L->top.p++, ts); /* anchor 'ts' (EXTRA_STACK) */
1175 luaH_newkey(L, t, s2v(L->top.p - 1), value);
1176 L->top.p--;
1177 return;
1178 }
1179 luaH_newkey(L, t, actk, value);
1180 }
1181 else if (hres > 0) { /* regular Node? */
1182 setobj2t(L, gval(gnode(t, hres - HFIRSTNODE)), value);
1183 }
1184 else { /* array entry */
1185 hres = ~hres; /* real index */
1186 obj2arr(t, cast_uint(hres), value);
1187 }
1188}
1189
1190
1191/*
1192** beware: when using this function you probably need to check a GC
1193** barrier and invalidate the TM cache.
1194*/
1195void luaH_set (lua_State *L, Table *t, const TValue *key, TValue *value) {
1196 int hres = luaH_pset(t, key, value);
1197 if (hres != HOK)
1198 luaH_finishset(L, t, key, value, hres);
1199}
1200
1201
1202/*
1203** Ditto for a GC barrier. (No need to invalidate the TM cache, as
1204** integers cannot be keys to metamethods.)
1205*/
1206void luaH_setint (lua_State *L, Table *t, lua_Integer key, TValue *value) {
1207 unsigned ik = ikeyinarray(t, key);
1208 if (ik > 0)
1209 obj2arr(t, ik - 1, value);
1210 else {
1211 int ok = rawfinishnodeset(getintfromhash(t, key), value);
1212 if (!ok) {
1213 TValue k;
1214 setivalue(&k, key);
1215 luaH_newkey(L, t, &k, value);
1216 }
1217 }
1218}
1219
1220
1221/*
1222** Try to find a boundary in the hash part of table 't'. From the
1223** caller, we know that 'asize + 1' is present. We want to find a larger
1224** key that is absent from the table, so that we can do a binary search
1225** between the two keys to find a boundary. We keep doubling 'j' until
1226** we get an absent index. If the doubling would overflow, we try
1227** LUA_MAXINTEGER. If it is absent, we are ready for the binary search.
1228** ('j', being max integer, is larger or equal to 'i', but it cannot be
1229** equal because it is absent while 'i' is present.) Otherwise, 'j' is a
1230** boundary. ('j + 1' cannot be a present integer key because it is not
1231** a valid integer in Lua.)
1232** About 'rnd': If we used a fixed algorithm, a bad actor could fill
1233** a table with only the keys that would be probed, in such a way that
1234** a small table could result in a huge length. To avoid that, we use
1235** the state's seed as a source of randomness. For the first probe,
1236** we "randomly double" 'i' by adding to it a random number roughly its
1237** width.
1238*/
1239static lua_Unsigned hash_search (lua_State *L, Table *t, unsigned asize) {
1240 lua_Unsigned i = asize + 1; /* caller ensures t[i] is present */
1241 unsigned rnd = G(L)->seed;
1242 int n = (asize > 0) ? luaO_ceillog2(asize) : 0; /* width of 'asize' */
1243 unsigned mask = (1u << n) - 1; /* 11...111 with the width of 'asize' */
1244 unsigned incr = (rnd & mask) + 1; /* first increment (at least 1) */
1245 lua_Unsigned j = (incr <= l_castS2U(LUA_MAXINTEGER) - i) ? i + incr : i + 1;
1246 rnd >>= n; /* used 'n' bits from 'rnd' */
1247 while (!hashkeyisempty(t, j)) { /* repeat until an absent t[j] */
1248 i = j; /* 'i' is a present index */
1249 if (j <= l_castS2U(LUA_MAXINTEGER)/2 - 1) {
1250 j = j*2 + (rnd & 1); /* try again with 2j or 2j+1 */
1251 rnd >>= 1;
1252 }
1253 else {
1254 j = LUA_MAXINTEGER;
1255 if (hashkeyisempty(t, j)) /* t[j] not present? */
1256 break; /* 'j' now is an absent index */
1257 else /* weird case */
1258 return j; /* well, max integer is a boundary... */
1259 }
1260 }
1261 /* i < j && t[i] present && t[j] absent */
1262 while (j - i > 1u) { /* do a binary search between them */
1263 lua_Unsigned m = (i + j) / 2;
1264 if (hashkeyisempty(t, m)) j = m;
1265 else i = m;
1266 }
1267 return i;
1268}
1269
1270
1271static unsigned int binsearch (Table *array, unsigned int i, unsigned int j) {
1272 lua_assert(i <= j);
1273 while (j - i > 1u) { /* binary search */
1274 unsigned int m = (i + j) / 2;
1275 if (arraykeyisempty(array, m)) j = m;
1276 else i = m;
1277 }
1278 return i;
1279}
1280
1281
1282/* return a border, saving it as a hint for next call */
1283static lua_Unsigned newhint (Table *t, unsigned hint) {
1284 lua_assert(hint <= t->asize);
1285 *lenhint(t) = hint;
1286 return hint;
1287}
1288
1289
1290/*
1291** Try to find a border in table 't'. (A 'border' is an integer index
1292** such that t[i] is present and t[i+1] is absent, or 0 if t[1] is absent,
1293** or 'maxinteger' if t[maxinteger] is present.)
1294** If there is an array part, try to find a border there. First try
1295** to find it in the vicinity of the previous result (hint), to handle
1296** cases like 't[#t + 1] = val' or 't[#t] = nil', that move the border
1297** by one entry. Otherwise, do a binary search to find the border.
1298** If there is no array part, or its last element is non empty, the
1299** border may be in the hash part.
1300*/
1301lua_Unsigned luaH_getn (lua_State *L, Table *t) {
1302 unsigned asize = t->asize;
1303 if (asize > 0) { /* is there an array part? */
1304 const unsigned maxvicinity = 4;
1305 unsigned limit = *lenhint(t); /* start with the hint */
1306 if (limit == 0)
1307 limit = 1; /* make limit a valid index in the array */
1308 if (arraykeyisempty(t, limit)) { /* t[limit] empty? */
1309 /* there must be a border before 'limit' */
1310 unsigned i;
1311 /* look for a border in the vicinity of the hint */
1312 for (i = 0; i < maxvicinity && limit > 1; i++) {
1313 limit--;
1314 if (!arraykeyisempty(t, limit))
1315 return newhint(t, limit); /* 'limit' is a border */
1316 }
1317 /* t[limit] still empty; search for a border in [0, limit) */
1318 return newhint(t, binsearch(t, 0, limit));
1319 }
1320 else { /* 'limit' is present in table; look for a border after it */
1321 unsigned i;
1322 /* look for a border in the vicinity of the hint */
1323 for (i = 0; i < maxvicinity && limit < asize; i++) {
1324 limit++;
1325 if (arraykeyisempty(t, limit))
1326 return newhint(t, limit - 1); /* 'limit - 1' is a border */
1327 }
1328 if (arraykeyisempty(t, asize)) { /* last element empty? */
1329 /* t[limit] not empty; search for a border in [limit, asize) */
1330 return newhint(t, binsearch(t, limit, asize));
1331 }
1332 }
1333 /* last element non empty; set a hint to speed up finding that again */
1334 /* (keys in the hash part cannot be hints) */
1335 *lenhint(t) = asize;
1336 }
1337 /* no array part or t[asize] is not empty; check the hash part */
1338 lua_assert(asize == 0 || !arraykeyisempty(t, asize));
1339 if (isdummy(t) || hashkeyisempty(t, asize + 1))
1340 return asize; /* 'asize + 1' is empty */
1341 else /* 'asize + 1' is also non empty */
1342 return hash_search(L, t, asize);
1343}
1344
1345
1346
1347#if defined(LUA_DEBUG)
1348
1349/* export this function for the test library */
1350
1351Node *luaH_mainposition (const Table *t, const TValue *key) {
1352 return mainpositionTV(t, key);
1353}
1354
1355#endif