Fixed OP_INCOBJECT to properly handle dicts

minor refactoring in OP_INCINDEX
This commit is contained in:
CPunch 2021-01-01 22:52:54 -06:00
parent c96b155412
commit bccabdebd7

View File

@ -912,7 +912,8 @@ int cosmoV_execute(CState *state) {
StkPtr key = cosmoV_getTop(state, 0); // grabs key StkPtr key = cosmoV_getTop(state, 0); // grabs key
if (IS_OBJ(*temp)) { if (IS_OBJ(*temp)) {
if (cosmoV_readObj(*temp)->type == COBJ_DICT) { switch (cosmoV_readObj(*temp)->type) {
case COBJ_DICT: {
CObjDict *dict = (CObjDict*)cosmoV_readObj(*temp); CObjDict *dict = (CObjDict*)cosmoV_readObj(*temp);
CValue *val = cosmoT_insert(state, &dict->tbl, *key); CValue *val = cosmoT_insert(state, &dict->tbl, *key);
@ -926,7 +927,9 @@ int cosmoV_execute(CState *state) {
cosmoV_error(state, "Expected number, got %s!", cosmoV_typeStr(*val)); cosmoV_error(state, "Expected number, got %s!", cosmoV_typeStr(*val));
return -1; return -1;
} }
} else if (cosmoV_readObj(*temp)->type == COBJ_OBJECT) { // check for __newindex! break;
}
case COBJ_OBJECT: {
CObjObject *object = (CObjObject*)cosmoV_readObj(*temp); CObjObject *object = (CObjObject*)cosmoV_readObj(*temp);
CValue val; CValue val;
@ -942,7 +945,9 @@ int cosmoV_execute(CState *state) {
return -1; return -1;
} }
} }
} else { break;
}
default:
cosmoV_error(state, "Couldn't set index with type %s!", cosmoV_typeStr(*temp)); cosmoV_error(state, "Couldn't set index with type %s!", cosmoV_typeStr(*temp));
return -1; return -1;
} }
@ -960,11 +965,9 @@ int cosmoV_execute(CState *state) {
CValue ident = constants[indx]; // grabs identifier CValue ident = constants[indx]; // grabs identifier
// sanity check // sanity check
if (!IS_OBJ(*temp) || cosmoV_readObj(*temp)->type != COBJ_OBJECT) { if (IS_OBJ(*temp)) {
cosmoV_error(state, "Couldn't set a field on non-object type %s!", cosmoV_typeStr(*temp)); switch (cosmoV_readObj(*temp)->type) {
return -1; case COBJ_OBJECT: {
}
CObjObject *object = (CObjObject*)cosmoV_readObj(*temp); CObjObject *object = (CObjObject*)cosmoV_readObj(*temp);
CValue val; CValue val;
@ -981,6 +984,32 @@ int cosmoV_execute(CState *state) {
cosmoV_error(state, "Expected number, got %s!", cosmoV_typeStr(val)); cosmoV_error(state, "Expected number, got %s!", cosmoV_typeStr(val));
return -1; return -1;
} }
break;
}
case COBJ_DICT: {
CObjDict *dict = (CObjDict*)cosmoV_readObj(*temp);
CValue *val = cosmoT_insert(state, &dict->tbl, ident);
// pops dict from stack
cosmoV_pop(state);
if (IS_NUMBER(*val)) {
cosmoV_pushValue(state, *val); // pushes old value onto the stack :)
*val = cosmoV_newNumber(cosmoV_readNumber(*val) + inc);
} else {
cosmoV_error(state, "Expected number, got %s!", cosmoV_typeStr(*val));
return -1;
}
break;
}
default:
cosmoV_error(state, "Couldn't set a field on type %s!", cosmoV_typeStr(*temp));
return -1;
}
} else {
cosmoV_error(state, "Couldn't set a field on type %s!", cosmoV_typeStr(*temp));
return -1;
}
break; break;
} }