feat: add WrenErrorType enum and route runtime errors through config errorFn callback

Replace direct fprintf(stderr) calls in wrenDebugPrintStackTrace with invocations of the host-provided errorFn callback, passing WREN_ERROR_RUNTIME for the error message and WREN_ERROR_STACK_TRACE for each stack frame. Introduce the WrenErrorType enum in wren.h with WREN_ERROR_COMPILE, WREN_ERROR_RUNTIME, and WREN_ERROR_STACK_TRACE values, update the WrenErrorFn typedef to accept a type parameter, and modify reportError in cli/vm.c to switch on the error type for formatted output. Adjust wrenDebugPrintStackTrace signature to take WrenVM* instead of ObjFiber* to access the config, and update all call sites accordingly.
This commit is contained in:
Bob Nystrom
2016-03-17 14:17:03 +00:00
parent 63e462bb84
commit 7e72462d90
7 changed files with 56 additions and 12 deletions
+24 -1
View File
@@ -58,8 +58,31 @@ typedef WrenForeignMethodFn (*WrenBindForeignMethodFn)(WrenVM* vm,
// Displays a string of text to the user.
typedef void (*WrenWriteFn)(WrenVM* vm, const char* text);
typedef enum
{
// A syntax or resolution error detected at compile time.
WREN_ERROR_COMPILE,
// The error message for a runtime error.
WREN_ERROR_RUNTIME,
// One entry of a runtime error's stack trace.
WREN_ERROR_STACK_TRACE
} WrenErrorType;
// Reports an error to the user.
typedef void (*WrenErrorFn)(const char* module, int line, const char* message);
//
// An error detected during compile time is reported by calling this once with
// `WREN_ERROR_COMPILE`, the name of the module and line where the error occurs,
// and the compiler's error message.
//
// A runtime error is reported by calling this once with `WREN_ERROR_RUNTIME`,
// no module or line, and the runtime error's message. After that, a series of
// `WREN_ERROR_STACK_TRACE` calls are made for each line in the stack trace.
// Each of those has the module and line where the method or function is
// defined and [message] is the name of the method or function.
typedef void (*WrenErrorFn)(
WrenErrorType type, const char* module, int line, const char* message);
typedef struct
{