Added '__count' metamethod to objects

This commit is contained in:
2021-01-22 15:22:30 -06:00
parent 6b407534c0
commit 7c92749e0d
6 changed files with 51 additions and 8 deletions

View File

@@ -979,17 +979,18 @@ int cosmoV_execute(CState *state) {
}
continue;
}
case OP_COUNT: { // pop 1 value off the stack & if it's a table return the amount of active entries it has
case OP_COUNT: {
StkPtr temp = cosmoV_getTop(state, 0);
if (!IS_TABLE(*temp)) {
cosmoV_error(state, "Expected table, got %s!", cosmoV_typeStr(*temp));
if (!IS_OBJ(*temp)) {
cosmoV_error(state, "Expected non-primitive, got %s!", cosmoV_typeStr(*temp));
return -1;
}
CObjTable *tbl = (CObjTable*)cosmoV_readObj(*temp);
int count = cosmoO_count(state, cosmoV_readObj(*temp));
cosmoV_pop(state);
cosmoV_pushNumber(state, cosmoT_count(&tbl->tbl)); // pushes the count onto the stack
cosmoV_pushNumber(state, count); // pushes the count onto the stack
continue;
}
case OP_CONCAT: {