feat: add userData field and pass WrenVM* to error callback

Extend WrenConfiguration with a void* userData field for attaching arbitrary state to a VM instance. Add wrenGetUserData and wrenSetUserData accessor functions. Modify the WrenErrorFn typedef and all call sites (compiler, debug, VM) to pass the WrenVM pointer as the final argument, enabling error handlers to access VM context.
This commit is contained in:
Bob Nystrom
2017-03-22 14:12:26 +00:00
5 changed files with 27 additions and 6 deletions
+1 -1
View File
@@ -404,7 +404,7 @@ static void printError(Parser* parser, int line, const char* label,
ASSERT(length < ERROR_MESSAGE_SIZE, "Error should not exceed buffer.");
parser->vm->config.errorFn(WREN_ERROR_COMPILE,
parser->module->name->value, line, message);
parser->module->name->value, line, message, parser->vm);
}
// Outputs a compile or syntax error. This also marks the compilation as having
+3 -3
View File
@@ -10,13 +10,13 @@ void wrenDebugPrintStackTrace(WrenVM* vm)
ObjFiber* fiber = vm->fiber;
if (IS_STRING(fiber->error))
{
vm->config.errorFn(WREN_ERROR_RUNTIME, NULL, -1, AS_CSTRING(fiber->error));
vm->config.errorFn(WREN_ERROR_RUNTIME, NULL, -1, AS_CSTRING(fiber->error), vm);
}
else
{
// TODO: Print something a little useful here. Maybe the name of the error's
// class?
vm->config.errorFn(WREN_ERROR_RUNTIME, NULL, -1, "[error object]");
vm->config.errorFn(WREN_ERROR_RUNTIME, NULL, -1, "[error object]", vm);
}
for (int i = fiber->numFrames - 1; i >= 0; i--)
@@ -35,7 +35,7 @@ void wrenDebugPrintStackTrace(WrenVM* vm)
// -1 because IP has advanced past the instruction that it just executed.
int line = fn->debug->sourceLines.data[frame->ip - fn->code.data - 1];
vm->config.errorFn(WREN_ERROR_STACK_TRACE, fn->module->name->value, line,
fn->debug->name);
fn->debug->name, vm);
}
}
+11
View File
@@ -46,6 +46,7 @@ void wrenInitConfiguration(WrenConfiguration* config)
config->initialHeapSize = 1024 * 1024 * 10;
config->minHeapSize = 1024 * 1024;
config->heapGrowthPercent = 50;
config->userData = NULL;
}
WrenVM* wrenNewVM(WrenConfiguration* config)
@@ -1695,3 +1696,13 @@ void wrenAbortFiber(WrenVM* vm, int slot)
validateApiSlot(vm, slot);
vm->fiber->error = vm->apiStack[slot];
}
void* wrenGetUserData(WrenVM* vm)
{
return vm->config.userData;
}
void wrenSetUserData(WrenVM* vm, void* userData)
{
vm->config.userData = userData;
}