diff --git a/src/vm/wren_vm.c b/src/vm/wren_vm.c index 1d4551bd..10ba8a04 100644 --- a/src/vm/wren_vm.c +++ b/src/vm/wren_vm.c @@ -1446,8 +1446,12 @@ WrenInterpretResult wrenInterpret(WrenVM* vm, const char* module, wrenPushRoot(vm, (Obj*)closure); ObjFiber* fiber = wrenNewFiber(vm, closure); wrenPopRoot(vm); // closure. - - return runInterpreter(vm, fiber); + + WrenInterpretResult result = runInterpreter(vm, fiber); + + vm->fiber = NULL; + vm->apiStack = NULL; + return result; } ObjClosure* wrenCompileSource(WrenVM* vm, const char* module, const char* source, diff --git a/test/api/new_vm.c b/test/api/new_vm.c index aa409688..4c50fd4a 100644 --- a/test/api/new_vm.c +++ b/test/api/new_vm.c @@ -13,9 +13,43 @@ static void nullConfig(WrenVM* vm) wrenFreeVM(otherVM); } +static void multipleInterpretCalls(WrenVM* vm) +{ + WrenVM* otherVM = wrenNewVM(NULL); + WrenInterpretResult result; + + bool correct = true; + + // Handles should be valid across calls into Wren code. + WrenHandle* absMethod = wrenMakeCallHandle(otherVM, "abs"); + + for (int i = 0; i < 5; i++) { + // Calling `wrenEnsureSlots()` before `wrenInterpret()` should not introduce + // problems later. + wrenEnsureSlots(otherVM, 2); + + result = wrenInterpret(otherVM, "main", "1 + 2"); + correct = correct && (result == WREN_RESULT_SUCCESS); + + wrenEnsureSlots(otherVM, 2); + wrenSetSlotDouble(otherVM, 0, -i); + result = wrenCall(otherVM, absMethod); + correct = correct && (result == WREN_RESULT_SUCCESS); + + double absValue = wrenGetSlotDouble(otherVM, 0); + correct = correct && (absValue == (double)i); + } + + wrenSetSlotBool(vm, 0, correct); + + wrenReleaseHandle(otherVM, absMethod); + wrenFreeVM(otherVM); +} + WrenForeignMethodFn newVMBindMethod(const char* signature) { if (strcmp(signature, "static VM.nullConfig()") == 0) return nullConfig; + if (strcmp(signature, "static VM.multipleInterpretCalls()") == 0) return multipleInterpretCalls; return NULL; } diff --git a/test/api/new_vm.wren b/test/api/new_vm.wren index 451c81e2..9ee090dd 100644 --- a/test/api/new_vm.wren +++ b/test/api/new_vm.wren @@ -1,6 +1,8 @@ class VM { foreign static nullConfig() + foreign static multipleInterpretCalls() } // TODO: Other configuration settings. System.print(VM.nullConfig()) // expect: true +System.print(VM.multipleInterpretCalls()) // expect: true