Use errorFn for runtime errors.

This commit is contained in:
Bob Nystrom
2016-03-17 07:17:03 -07:00
parent 66b7acd862
commit 984fa1ddd1
7 changed files with 56 additions and 12 deletions
+3 -1
View File
@@ -1,5 +1,6 @@
#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include "wren_common.h"
@@ -400,7 +401,8 @@ static void printError(Parser* parser, int line, const char* label,
length += vsprintf(message + length, format, args);
ASSERT(length < ERROR_MESSAGE_SIZE, "Error should not exceed buffer.");
parser->vm->config.errorFn(parser->module->name->value, line, message);
parser->vm->config.errorFn(WREN_ERROR_COMPILE,
parser->module->name->value, line, message);
}
// Outputs a compile or syntax error. This also marks the compilation as having
+9 -5
View File
@@ -2,17 +2,21 @@
#include "wren_debug.h"
void wrenDebugPrintStackTrace(ObjFiber* fiber)
void wrenDebugPrintStackTrace(WrenVM* vm)
{
// Bail if the host doesn't enable printing errors.
if (vm->config.errorFn == NULL) return;
ObjFiber* fiber = vm->fiber;
if (IS_STRING(fiber->error))
{
fprintf(stderr, "%s\n", AS_CSTRING(fiber->error));
vm->config.errorFn(WREN_ERROR_RUNTIME, NULL, -1, AS_CSTRING(fiber->error));
}
else
{
// TODO: Print something a little useful here. Maybe the name of the error's
// class?
fprintf(stderr, "[error object]\n");
vm->config.errorFn(WREN_ERROR_RUNTIME, NULL, -1, "[error object]");
}
for (int i = fiber->numFrames - 1; i >= 0; i--)
@@ -30,8 +34,8 @@ void wrenDebugPrintStackTrace(ObjFiber* fiber)
// -1 because IP has advanced past the instruction that it just executed.
int line = fn->debug->sourceLines.data[frame->ip - fn->code.data - 1];
fprintf(stderr, "[%s line %d] in %s\n",
fn->module->name->value, line, fn->debug->name);
vm->config.errorFn(WREN_ERROR_STACK_TRACE, fn->module->name->value, line,
fn->debug->name);
}
}
+2 -2
View File
@@ -4,10 +4,10 @@
#include "wren_value.h"
#include "wren_vm.h"
// Prints the current stack trace for [fiber] to stderr.
// Prints the stack trace for the current fiber.
//
// Used when a fiber throws a runtime error which is not caught.
void wrenDebugPrintStackTrace(ObjFiber* fiber);
void wrenDebugPrintStackTrace(WrenVM* vm);
// The "dump" functions are used for debugging Wren itself. Normal code paths
// will not call them unless one of the various DEBUG_ flags is enabled.
+1
View File
@@ -1,5 +1,6 @@
#include <math.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include "wren.h"
+1 -1
View File
@@ -388,7 +388,7 @@ static void runtimeError(WrenVM* vm)
}
// If we got here, nothing caught the error, so show the stack trace.
wrenDebugPrintStackTrace(vm->fiber);
wrenDebugPrintStackTrace(vm);
vm->fiber = NULL;
}