DUALNUM: Add integer type to core VM.
This commit is contained in:
33
src/lj_tab.c
33
src/lj_tab.c
@@ -34,6 +34,7 @@ static LJ_AINLINE Node *hashmask(const GCtab *t, uint32_t hash)
|
||||
/* Hash an arbitrary key and return its anchor position in the hash table. */
|
||||
static Node *hashkey(const GCtab *t, cTValue *key)
|
||||
{
|
||||
lua_assert(!tvisint(key));
|
||||
if (tvisstr(key))
|
||||
return hashstr(t, strV(key));
|
||||
else if (tvisnum(key))
|
||||
@@ -100,7 +101,7 @@ static GCtab *newtab(lua_State *L, uint32_t asize, uint32_t hbits)
|
||||
lua_assert((sizeof(GCtab) & 7) == 0);
|
||||
t = (GCtab *)lj_mem_newgco(L, sizetabcolo(asize));
|
||||
t->gct = ~LJ_TTAB;
|
||||
t->nomm = cast_byte(~0);
|
||||
t->nomm = (uint8_t)~0;
|
||||
t->colo = (int8_t)asize;
|
||||
setmref(t->array, (TValue *)((char *)t + sizeof(GCtab)));
|
||||
setgcrefnull(t->metatable);
|
||||
@@ -110,7 +111,7 @@ static GCtab *newtab(lua_State *L, uint32_t asize, uint32_t hbits)
|
||||
} else { /* Otherwise separately allocate the array part. */
|
||||
t = lj_mem_newobj(L, GCtab);
|
||||
t->gct = ~LJ_TTAB;
|
||||
t->nomm = cast_byte(~0);
|
||||
t->nomm = (uint8_t)~0;
|
||||
t->colo = 0;
|
||||
setmref(t->array, NULL);
|
||||
setgcrefnull(t->metatable);
|
||||
@@ -275,10 +276,11 @@ static void resizetab(lua_State *L, GCtab *t, uint32_t asize, uint32_t hbits)
|
||||
|
||||
static uint32_t countint(cTValue *key, uint32_t *bins)
|
||||
{
|
||||
lua_assert(!tvisint(key));
|
||||
if (tvisnum(key)) {
|
||||
lua_Number nk = numV(key);
|
||||
int32_t k = lj_num2int(nk);
|
||||
if ((uint32_t)k < LJ_MAX_ASIZE && nk == cast_num(k)) {
|
||||
if ((uint32_t)k < LJ_MAX_ASIZE && nk == (lua_Number)k) {
|
||||
bins[(k > 2 ? lj_fls((uint32_t)(k-1)) : 0)]++;
|
||||
return 1;
|
||||
}
|
||||
@@ -360,7 +362,7 @@ cTValue * LJ_FASTCALL lj_tab_getinth(GCtab *t, int32_t key)
|
||||
{
|
||||
TValue k;
|
||||
Node *n;
|
||||
k.n = cast_num(key);
|
||||
k.n = (lua_Number)key;
|
||||
n = hashnum(t, &k);
|
||||
do {
|
||||
if (tvisnum(&n->key) && n->key.n == k.n)
|
||||
@@ -385,10 +387,14 @@ cTValue *lj_tab_get(lua_State *L, GCtab *t, cTValue *key)
|
||||
cTValue *tv = lj_tab_getstr(t, strV(key));
|
||||
if (tv)
|
||||
return tv;
|
||||
} else if (tvisint(key)) {
|
||||
cTValue *tv = lj_tab_getint(t, intV(key));
|
||||
if (tv)
|
||||
return tv;
|
||||
} else if (tvisnum(key)) {
|
||||
lua_Number nk = numV(key);
|
||||
int32_t k = lj_num2int(nk);
|
||||
if (nk == cast_num(k)) {
|
||||
if (nk == (lua_Number)k) {
|
||||
cTValue *tv = lj_tab_getint(t, k);
|
||||
if (tv)
|
||||
return tv;
|
||||
@@ -466,7 +472,7 @@ TValue *lj_tab_setinth(lua_State *L, GCtab *t, int32_t key)
|
||||
{
|
||||
TValue k;
|
||||
Node *n;
|
||||
k.n = cast_num(key);
|
||||
k.n = (lua_Number)key;
|
||||
n = hashnum(t, &k);
|
||||
do {
|
||||
if (tvisnum(&n->key) && n->key.n == k.n)
|
||||
@@ -493,10 +499,12 @@ TValue *lj_tab_set(lua_State *L, GCtab *t, cTValue *key)
|
||||
t->nomm = 0; /* Invalidate negative metamethod cache. */
|
||||
if (tvisstr(key)) {
|
||||
return lj_tab_setstr(L, t, strV(key));
|
||||
} else if (tvisint(key)) {
|
||||
return lj_tab_setint(L, t, intV(key));
|
||||
} else if (tvisnum(key)) {
|
||||
lua_Number nk = numV(key);
|
||||
int32_t k = lj_num2int(nk);
|
||||
if (nk == cast_num(k))
|
||||
if (nk == (lua_Number)k)
|
||||
return lj_tab_setint(L, t, k);
|
||||
if (tvisnan(key))
|
||||
lj_err_msg(L, LJ_ERR_NANIDX);
|
||||
@@ -517,10 +525,17 @@ TValue *lj_tab_set(lua_State *L, GCtab *t, cTValue *key)
|
||||
/* Get the traversal index of a key. */
|
||||
static uint32_t keyindex(lua_State *L, GCtab *t, cTValue *key)
|
||||
{
|
||||
if (tvisnum(key)) {
|
||||
TValue tmp;
|
||||
if (tvisint(key)) {
|
||||
int32_t k = intV(key);
|
||||
if ((uint32_t)k < t->asize)
|
||||
return (uint32_t)k; /* Array key indexes: [0..t->asize-1] */
|
||||
setnumV(&tmp, (lua_Number)k);
|
||||
key = &tmp;
|
||||
} else if (tvisnum(key)) {
|
||||
lua_Number nk = numV(key);
|
||||
int32_t k = lj_num2int(nk);
|
||||
if ((uint32_t)k < t->asize && nk == cast_num(k))
|
||||
if ((uint32_t)k < t->asize && nk == (lua_Number)k)
|
||||
return (uint32_t)k; /* Array key indexes: [0..t->asize-1] */
|
||||
}
|
||||
if (!tvisnil(key)) {
|
||||
|
||||
Reference in New Issue
Block a user