feat: add configurable write callback for System.print output delegation
Add a `WrenWriteFn` callback to `WrenConfiguration` that allows host applications to control how text from `System.print()` and related functions is displayed. When set, the VM invokes this callback instead of directly calling `printf`. If the callback is NULL, printed text is silently discarded. The CLI's `initVM()` now registers a simple `write` function that prints to stdout, while the core `system_writeString` primitive checks for the callback before outputting. This change also refactors `wrenInitConfiguration` to set `defaultReallocate` as the default reallocation function and consolidates configuration fields into a single `WrenConfiguration` struct stored directly in `WrenVM`, removing separate member fields for reallocate, bindForeignMethod, bindForeignClass, loadModule, minNextGC, and heapScalePercent.
This commit is contained in:
@@ -145,6 +145,11 @@ static WrenForeignClassMethods bindForeignClass(
|
||||
return methods;
|
||||
}
|
||||
|
||||
static void write(WrenVM* vm, const char* text)
|
||||
{
|
||||
printf("%s", text);
|
||||
}
|
||||
|
||||
static void initVM()
|
||||
{
|
||||
WrenConfiguration config;
|
||||
@@ -153,6 +158,7 @@ static void initVM()
|
||||
config.bindForeignMethodFn = bindForeignMethod;
|
||||
config.bindForeignClassFn = bindForeignClass;
|
||||
config.loadModuleFn = readModule;
|
||||
config.writeFn = write;
|
||||
|
||||
// Since we're running in a standalone process, be generous with memory.
|
||||
config.initialHeapSize = 1024 * 1024 * 100;
|
||||
|
||||
Reference in New Issue
Block a user