CObjMethod refactor

This commit is contained in:
2020-11-12 23:04:09 -06:00
parent 40ae495aaf
commit 08b6dcbf4c
12 changed files with 135 additions and 65 deletions

View File

@@ -4,7 +4,7 @@
#include "cobj.h"
#include "cmem.h"
void cosmoB_loadlibrary(CState *state) {
void cosmoB_loadLibrary(CState *state) {
cosmoM_freezeGC(state);
cosmoV_register(state, "print", cosmoV_newObj(cosmoO_newCFunction(state, cosmoB_print)));
cosmoM_unfreezeGC(state);
@@ -18,4 +18,28 @@ CValue cosmoB_print(CState *state, int nargs, CValue *args) {
printf("\n");
return cosmoV_newNil(); // print doesn't return any args
}
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));
}