Skip to content

Commit

Permalink
fix: add error handling for sythentic call frame
Browse files Browse the repository at this point in the history
  • Loading branch information
liz3 committed Nov 14, 2024
1 parent b07a823 commit da8129f
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions src/vm/vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,21 +59,25 @@ void runtimeError(DictuVM *vm, const char *format, ...) {
for (int i = vm->frameCount - 1; i >= 0; i--) {
CallFrame *frame = &vm->frames[i];

ObjFunction *function = frame->closure->function;
if(frame->closure == NULL) {
// synthetic frame created by callFunction
log_error("Function <native> {reset}");
} else {
ObjFunction *function = frame->closure->function;

// -1 because the IP is sitting on the next instruction to be
// executed.
size_t instruction = frame->ip - function->chunk.code - 1;
// -1 because the IP is sitting on the next instruction to be
// executed.
size_t instruction = frame->ip - function->chunk.code - 1;

if (function->name == NULL) {
log_error("File '%s', {bold}line %d{reset}", function->module->name->chars, function->chunk.lines[instruction]);
i = -1;
} else {
log_error("Function '%s' in '%s', {bold}line %d{reset}", function->name->chars, function->module->name->chars, function->chunk.lines[instruction]);
}
if (function->name == NULL) {
log_error("File '%s', {bold}line %d{reset}", function->module->name->chars, function->chunk.lines[instruction]);
i = -1;
} else {
log_error("Function '%s' in '%s', {bold}line %d{reset}", function->name->chars, function->module->name->chars, function->chunk.lines[instruction]);
}

}
log_pad("");

va_list args;
va_start(args, format);
vfprintf(stderr, format, args);
Expand Down Expand Up @@ -2490,6 +2494,7 @@ Value callFunction(DictuVM* vm, Value function, int argCount, Value* args) {
CallFrame *frame = &vm->frames[vm->frameCount++];
uint8_t code[4] = {OP_CALL, argCount, 0, OP_RETURN};
frame->ip = code;
frame->closure = NULL;
push(vm, function);
for(int i = 0; i < argCount; i++) {
push(vm, args[i]);
Expand Down

0 comments on commit da8129f

Please sign in to comment.