feat: add wrenAbortFiber() API to abort current fiber with runtime error object

Add a new public API function `wrenAbortFiber()` that allows foreign methods to abort the currently executing fiber with a custom runtime error object. The function validates the API slot, then sets the fiber's error field to the value in the specified slot. The interpreter loop is updated to check for a non-null fiber error after calling a foreign method, triggering a runtime error if set. New test files (error.c, error.h, error.wren) demonstrate usage via a static Error.runtimeError foreign method, verifying that the error is propagated through Fiber.try() and that the fiber's isDone and error properties are correctly set. Xcode project updated to include the new error.c source file.
This commit is contained in:
Bob Nystrom
2016-06-10 02:14:21 +00:00
parent 78fa150869
commit 1de17224e8
8 changed files with 55 additions and 1 deletions
+18
View File
@@ -0,0 +1,18 @@
#include <stdio.h>
#include <string.h>
#include "error.h"
static void runtimeError(WrenVM* vm)
{
wrenEnsureSlots(vm, 1);
wrenSetSlotString(vm, 0, "Error!");
wrenAbortFiber(vm, 0);
}
WrenForeignMethodFn errorBindMethod(const char* signature)
{
if (strcmp(signature, "static Error.runtimeError") == 0) return runtimeError;
return NULL;
}
+3
View File
@@ -0,0 +1,3 @@
#include "wren.h"
WrenForeignMethodFn errorBindMethod(const char* signature);
+12
View File
@@ -0,0 +1,12 @@
class Error {
foreign static runtimeError
}
var fiber = Fiber.new {
Error.runtimeError
}
var error = fiber.try()
System.print(error) // expect: Error!
System.print(fiber.isDone) // expect: true
System.print(fiber.error) // expect: Error!
+4
View File
@@ -6,6 +6,7 @@
#include "benchmark.h"
#include "call.h"
#include "error.h"
#include "get_variable.h"
#include "foreign_class.h"
#include "handle.h"
@@ -35,6 +36,9 @@ static WrenForeignMethodFn bindForeignMethod(
method = benchmarkBindMethod(fullName);
if (method != NULL) return method;
method = errorBindMethod(fullName);
if (method != NULL) return method;
method = getVariableBindMethod(fullName);
if (method != NULL) return method;