feat: add WrenErrorFn callback to WrenConfiguration for custom error reporting

- Add `WrenErrorFn` typedef in wren.h accepting module, line, and message parameters
- Include `errorFn` slot in `WrenConfiguration` struct with documentation comment
- Initialize `errorFn` to NULL in `wrenInitConfiguration()` in wren_vm.c
- Replace direct `fprintf(stderr, ...)` in wren_compiler.c with call to `parser->vm->config.errorFn()`
- Add guard in wren_compiler.c to skip error reporting when `errorFn` is NULL
- Implement `reportError()` in cli/vm.c that writes formatted errors to stderr
- Wire `reportError` into config during `initVM()` in cli/vm.c
This commit is contained in:
Bob Nystrom
2016-03-16 15:04:03 +00:00
parent f16e5c775f
commit d9ec3627cf
4 changed files with 21 additions and 2 deletions
+6
View File
@@ -156,6 +156,11 @@ static void write(WrenVM* vm, const char* text)
fflush(stdout);
}
static void reportError(const char* module, int line, const char* message)
{
fprintf(stderr, "[%s line %d] %s\n", module, line, message);
}
static void initVM()
{
WrenConfiguration config;
@@ -165,6 +170,7 @@ static void initVM()
config.bindForeignClassFn = bindForeignClass;
config.loadModuleFn = readModule;
config.writeFn = write;
config.errorFn = reportError;
// Since we're running in a standalone process, be generous with memory.
config.initialHeapSize = 1024 * 1024 * 100;