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-12-27 19:36:17 +00:00
|
|
|
// print
|
2020-12-20 03:15:12 +00:00
|
|
|
cosmoV_pushString(state, "print");
|
|
|
|
cosmoV_pushCFunction(state, cosmoB_print);
|
2020-12-27 19:36:17 +00:00
|
|
|
|
|
|
|
// assert (for unit testing)
|
|
|
|
cosmoV_pushString(state, "assert");
|
|
|
|
cosmoV_pushCFunction(state, cosmoB_assert);
|
|
|
|
|
|
|
|
cosmoV_register(state, 2);
|
2020-10-28 05:16:30 +00:00
|
|
|
}
|
|
|
|
|
2020-12-13 03:53:12 +00:00
|
|
|
int 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-12-13 03:53:12 +00:00
|
|
|
return 0; // print doesn't return any args
|
2020-11-13 05:04:09 +00:00
|
|
|
}
|
|
|
|
|
2020-12-27 19:36:17 +00:00
|
|
|
int cosmoB_assert(CState *state, int nargs, CValue *args) {
|
|
|
|
if (nargs != 1) {
|
|
|
|
cosmoV_error(state, "assert() expected 1 argument, got %d!", nargs);
|
|
|
|
return 0; // nothing pushed onto the stack to return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!IS_BOOLEAN(args[0])) {
|
|
|
|
cosmoV_error(state, "assert() expected <boolean>, got %s!", cosmoV_typeStr(args[0]));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!cosmoV_readBoolean(args[0])) { // expression passed was false, error!
|
|
|
|
cosmoV_error(state, "assert() failed!");
|
|
|
|
} // else do nothing :)
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-12-13 03:53:12 +00:00
|
|
|
int 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
|
|
|
|
2020-12-13 03:53:12 +00:00
|
|
|
return 0; // nothing
|
2020-11-13 05:04:09 +00:00
|
|
|
}
|
2020-11-13 23:39:47 +00:00
|
|
|
|
2020-12-13 03:53:12 +00:00
|
|
|
int cosmoB_dgetProto(CState *state, int nargs, CValue *args) {
|
2020-11-13 18:54:06 +00:00
|
|
|
if (nargs != 1) {
|
2020-12-27 19:36:17 +00:00
|
|
|
cosmoV_error(state, "Expected 1 argument, got %d!", nargs);
|
2020-11-13 18:54:06 +00:00
|
|
|
}
|
|
|
|
|
2020-12-13 03:53:12 +00:00
|
|
|
cosmoV_pushValue(state, cosmoV_newObj(cosmoV_readObject(args[0])->proto)); // just return the proto
|
|
|
|
|
|
|
|
return 1; // 1 result
|
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
|
|
|
}
|