Compare commits

..

2 Commits

Author SHA1 Message Date
a337e26229 added <obj>:keys() method
this method returns a table of keys or 'fields' available in the object
2023-12-25 19:08:04 -06:00
b7bb0773b1 removed stale WRITE_GLOBAL_OP macro 2023-12-25 19:08:04 -06:00
2 changed files with 30 additions and 6 deletions

View File

@ -182,6 +182,32 @@ int cosmoB_ogetProto(CState *state, int nargs, CValue *args)
return 1; // 1 result return 1; // 1 result
} }
int cosmoB_ogetKeys(CState *state, int nargs, CValue *args)
{
if (nargs != 1)
cosmoV_error(state, "Expected 1 argument, got %d!", nargs);
if (!IS_OBJECT(args[0])) {
cosmoV_typeError(state, "object.__keys", "<object>", "%s", cosmoV_typeStr(args[0]));
}
// push keys
CObjObject *obj = cosmoV_readObject(args[0]);
int cap = cosmoT_getCapacity(&obj->tbl);
int indx = 0;
for (int i = 0; i < cap; i++) {
CTableEntry *entry = &obj->tbl.table[i];
if (IS_NIL(entry->key))
continue;
cosmoV_pushNumber(state, indx++);
cosmoV_pushValue(state, entry->key);
}
cosmoV_makeTable(state, indx);
return 1; // 1 result
}
int cosmoB_oisChild(CState *state, int nargs, CValue *args) int cosmoB_oisChild(CState *state, int nargs, CValue *args)
{ {
if (nargs != 2) { if (nargs != 2) {
@ -203,9 +229,9 @@ int cosmoB_oisChild(CState *state, int nargs, CValue *args)
COSMO_API void cosmoB_loadObjLib(CState *state) COSMO_API void cosmoB_loadObjLib(CState *state)
{ {
const char *identifiers[] = {"ischild"}; const char *identifiers[] = {"ischild", "keys"};
CosmoCFunction objLib[] = {cosmoB_oisChild}; CosmoCFunction objLib[] = {cosmoB_oisChild, cosmoB_ogetKeys};
// make object library object // make object library object
cosmoV_pushString(state, "object"); cosmoV_pushString(state, "object");
@ -213,11 +239,11 @@ COSMO_API void cosmoB_loadObjLib(CState *state)
// make __getter object for debug proto // make __getter object for debug proto
cosmoV_pushString(state, "__getter"); cosmoV_pushString(state, "__getter");
// key & value pair // key & value pairs
cosmoV_pushString(state, "__proto"); // key cosmoV_pushString(state, "__proto"); // key
cosmoV_pushCFunction(state, cosmoB_ogetProto); // value cosmoV_pushCFunction(state, cosmoB_ogetProto); // value
cosmoV_makeTable(state, 1); cosmoV_makeTable(state, 2);
// make __setter table // make __setter table
cosmoV_pushString(state, "__setter"); cosmoV_pushString(state, "__setter");

View File

@ -592,8 +592,6 @@ static void group(CParseState *pstate, bool canAssign, Precedence prec)
consume(pstate, TOKEN_RIGHT_PAREN, "Expected ')'"); consume(pstate, TOKEN_RIGHT_PAREN, "Expected ')'");
} }
#define WRITE_GLOBAL_OP(pstate, op, arg)
static void _etterAB(CParseState *pstate, uint8_t a, int b, bool isGlobal) static void _etterAB(CParseState *pstate, uint8_t a, int b, bool isGlobal)
{ {
writeu8(pstate, a); writeu8(pstate, a);