Cosmo/src/cbaselib.c

45 lines
1.3 KiB
C
Raw Normal View History

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"
#include "cmem.h"
2020-10-28 05:16:30 +00:00
2020-11-13 05:04:09 +00:00
void cosmoB_loadLibrary(CState *state) {
cosmoM_freezeGC(state);
2020-10-28 05:16:30 +00:00
cosmoV_register(state, "print", cosmoV_newObj(cosmoO_newCFunction(state, cosmoB_print)));
cosmoM_unfreezeGC(state);
2020-10-28 05:16:30 +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");
return cosmoV_newNil(); // print doesn't return any args
2020-11-13 05:04:09 +00:00
}
CValue cosmoB_dsetMeta(CState *state, int nargs, CValue *args) {
if (nargs == 2) {
CObjObject *obj = cosmoV_readObject(args[0]); // object to set meta too
CObjObject *meta = cosmoV_readObject(args[1]);
obj->meta = meta; // boom done
}
return cosmoV_newNil(); // nothing
}
CValue cosmoB_dgetMeta(CState *state, int nargs, CValue *args) {
return cosmoV_newObj(cosmoV_readObject(args[0])->meta); // just return the meta
}
void cosmoB_loadDebug(CState *state) {
cosmoV_pushString(state, "getMeta");
cosmoV_pushCFunction(state, cosmoB_dgetMeta);
cosmoV_pushString(state, "setMeta");
cosmoV_pushCFunction(state, cosmoB_dsetMeta);
cosmoV_pushObject(state, 2);
state->metaObj = cosmoV_readObject(*cosmoV_pop(state));
2020-10-28 05:16:30 +00:00
}