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