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
+4 -2
View File
@@ -394,14 +394,16 @@ static void printError(Parser* parser, int line, const char* label,
parser->hasError = true;
if (!parser->printErrors) return;
// Only report errors if there is a WrenErrorFn to handle them.
if (parser->vm->config.errorFn == NULL) return;
// Format the label and message.
char message[ERROR_MESSAGE_SIZE];
int length = sprintf(message, "%s: ", label);
length += vsprintf(message + length, format, args);
ASSERT(length < ERROR_MESSAGE_SIZE, "Error should not exceed buffer.");
fprintf(stderr, "[%s line %d] %s\n", parser->module->name->value, line,
message);
parser->vm->config.errorFn(parser->module->name->value, line, message);
}
// Outputs a compile or syntax error. This also marks the compilation as having
+1
View File
@@ -42,6 +42,7 @@ void wrenInitConfiguration(WrenConfiguration* config)
config->bindForeignMethodFn = NULL;
config->bindForeignClassFn = NULL;
config->writeFn = NULL;
config->errorFn = NULL;
config->initialHeapSize = 1024 * 1024 * 10;
config->minHeapSize = 1024 * 1024;
config->heapGrowthPercent = 50;