Added CObjError, cosmoV_throw(), pcall(), and cosmoV_printError()

Errors are now handled very differently, parser errors and VM errors are now treated the same.
When cosmoV_error is called, cosmoV_throw is also called, which formats the error object and sets the panic state.
state->error now points to the latest CObjError when state->panic is true. To get a nice formatted Objection message, use
cosmoV_printError() and pass the state->error. pcall() was added to the standard base library. When called, the first argument
passed is called with the subsequent arguments given. If the call completed successfully, `true`,`nil` is returned. However
when an error occurs during the call, `false`,`<error>` is returned. Simply print the `<error>` to retrieve the error string.
This commit is contained in:
2021-01-05 22:27:59 -06:00
parent 417a1f15f1
commit eb2f50e456
13 changed files with 195 additions and 65 deletions

View File

@@ -120,6 +120,16 @@ void blackenObject(CState *state, CObj *obj) {
markObject(state, (CObj*)method->obj);
break;
}
case COBJ_ERROR: {
CObjError *err = (CObjError*)obj;
markValue(state, err->err);
// mark callframes
for (int i = 0; i < err->frameCount; i++)
markObject(state, (CObj*)err->frames[i].closure);
break;
}
case COBJ_CLOSURE: {
CObjClosure *closure = (CObjClosure*)obj;
markObject(state, (CObj*)closure->function);
@@ -232,8 +242,10 @@ void markRoots(CState *state) {
// mark the user defined roots
markUserRoots(state);
// mark our proto object
// mark other misc. internally reserved objects
markObject(state, (CObj*)state->protoObj);
markObject(state, (CObj*)state->error);
traceGrays(state);
}