2020-10-28 05:16:30 +00:00
|
|
|
#include "cbaselib.h"
|
2020-11-12 23:17:41 +00:00
|
|
|
#include "cvm.h"
|
2020-10-28 05:16:30 +00:00
|
|
|
#include "cvalue.h"
|
|
|
|
#include "cobj.h"
|
2020-11-03 04:32:39 +00:00
|
|
|
#include "cmem.h"
|
2020-10-28 05:16:30 +00:00
|
|
|
|
2020-11-13 05:04:09 +00:00
|
|
|
void cosmoB_loadLibrary(CState *state) {
|
2020-11-03 04:32:39 +00:00
|
|
|
cosmoM_freezeGC(state);
|
2020-10-28 05:16:30 +00:00
|
|
|
cosmoV_register(state, "print", cosmoV_newObj(cosmoO_newCFunction(state, cosmoB_print)));
|
2020-12-13 00:16:31 +00:00
|
|
|
cosmoV_register(state, "foreach", cosmoV_newObj(cosmoO_newCFunction(state, cosmoB_foreach)));
|
2020-11-03 04:32:39 +00:00
|
|
|
cosmoM_unfreezeGC(state);
|
2020-10-28 05:16:30 +00:00
|
|
|
}
|
|
|
|
|
2020-11-10 01:44:12 +00:00
|
|
|
CValue cosmoB_print(CState *state, int nargs, CValue *args) {
|
2020-10-28 05:16:30 +00:00
|
|
|
for (int i = 0; i < nargs; i++) {
|
|
|
|
CObjString *str = cosmoV_toString(state, args[i]);
|
|
|
|
printf("%s", cosmoO_readCString(str));
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
|
2020-11-10 01:44:12 +00:00
|
|
|
return cosmoV_newNil(); // print doesn't return any args
|
2020-11-13 05:04:09 +00:00
|
|
|
}
|
|
|
|
|
2020-12-13 00:16:31 +00:00
|
|
|
CValue cosmoB_foreach(CState *state, int nargs, CValue *args) {
|
|
|
|
if (nargs != 2) {
|
|
|
|
cosmoV_error(state, "foreach() expected 2 parameters, got %d!", nargs);
|
|
|
|
return cosmoV_newNil();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!IS_DICT(args[0])) {
|
|
|
|
cosmoV_error(state, "foreach() expected first parameter to be <dictionary>, got %s!", cosmoV_typeStr(args[0]));
|
|
|
|
return cosmoV_newNil();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!IS_CALLABLE(args[1])) {
|
|
|
|
cosmoV_error(state, "foreach() expected second parameter to be callable, got %s!", cosmoV_typeStr(args[1]));
|
|
|
|
return cosmoV_newNil();
|
|
|
|
}
|
|
|
|
|
|
|
|
// loop through dictonary table, calling args[1] on active entries
|
|
|
|
CObjDict *dict = (CObjDict*)cosmoV_readObj(args[0]);
|
|
|
|
|
|
|
|
for (int i = 0; i < dict->tbl.capacity; i++) {
|
|
|
|
CTableEntry *entry = &dict->tbl.table[i];
|
|
|
|
|
|
|
|
if (!IS_NIL(entry->key)) {
|
|
|
|
// push key & value, then call args[1]
|
|
|
|
cosmoV_pushValue(state, args[1]);
|
|
|
|
cosmoV_pushValue(state, entry->key);
|
|
|
|
cosmoV_pushValue(state, entry->val);
|
|
|
|
cosmoV_call(state, 2);
|
|
|
|
cosmoV_pop(state); // throw away the return value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return cosmoV_newNil();
|
|
|
|
}
|
|
|
|
|
2020-11-15 18:22:11 +00:00
|
|
|
CValue cosmoB_dsetProto(CState *state, int nargs, CValue *args) {
|
2020-11-13 05:04:09 +00:00
|
|
|
if (nargs == 2) {
|
2020-11-15 18:22:11 +00:00
|
|
|
CObjObject *obj = cosmoV_readObject(args[0]); // object to set proto too
|
|
|
|
CObjObject *proto = cosmoV_readObject(args[1]);
|
2020-11-13 05:04:09 +00:00
|
|
|
|
2020-11-15 18:22:11 +00:00
|
|
|
obj->proto = proto; // boom done
|
2020-11-13 18:54:06 +00:00
|
|
|
} else {
|
|
|
|
cosmoV_error(state, "Expected 2 parameters, got %d!", nargs);
|
|
|
|
}
|
2020-11-13 05:04:09 +00:00
|
|
|
|
|
|
|
return cosmoV_newNil(); // nothing
|
|
|
|
}
|
2020-11-13 23:39:47 +00:00
|
|
|
|
2020-11-15 18:22:11 +00:00
|
|
|
CValue cosmoB_dgetProto(CState *state, int nargs, CValue *args) {
|
2020-11-13 18:54:06 +00:00
|
|
|
if (nargs != 1) {
|
|
|
|
cosmoV_error(state, "Expected 1 parameter, got %d!", nargs);
|
|
|
|
}
|
|
|
|
|
2020-11-15 18:22:11 +00:00
|
|
|
return cosmoV_newObj(cosmoV_readObject(args[0])->proto); // just return the proto
|
2020-11-13 05:04:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void cosmoB_loadDebug(CState *state) {
|
2020-12-06 20:11:33 +00:00
|
|
|
// make __getter object for debug proto
|
|
|
|
cosmoV_pushString(state, "__getter");
|
|
|
|
|
|
|
|
// key & value pair
|
|
|
|
cosmoV_pushString(state, "__proto"); // key
|
2020-12-05 23:55:09 +00:00
|
|
|
cosmoV_pushCFunction(state, cosmoB_dgetProto); // value
|
|
|
|
|
2020-12-06 20:11:33 +00:00
|
|
|
cosmoV_makeObject(state, 1);
|
|
|
|
|
|
|
|
// make __setter object
|
|
|
|
cosmoV_pushString(state, "__setter");
|
|
|
|
|
|
|
|
cosmoV_pushString(state, "__proto");
|
2020-11-15 18:22:11 +00:00
|
|
|
cosmoV_pushCFunction(state, cosmoB_dsetProto);
|
2020-11-13 05:04:09 +00:00
|
|
|
|
2020-12-06 20:11:33 +00:00
|
|
|
cosmoV_makeObject(state, 1);
|
|
|
|
|
|
|
|
// we call makeObject leting it know there are 2 sets of key & value pairs on the stack
|
2020-12-05 23:55:09 +00:00
|
|
|
cosmoV_makeObject(state, 2);
|
|
|
|
|
|
|
|
// set debug proto to the debug object
|
2020-11-15 18:22:11 +00:00
|
|
|
state->protoObj = cosmoV_readObject(*cosmoV_pop(state));
|
2020-10-28 05:16:30 +00:00
|
|
|
}
|