Fix slot array corrupted by wrenInterpret() (#730)

This commit is contained in:
kawa-yoiko
2020-06-14 12:42:06 +08:00
committed by GitHub
parent 7983082b71
commit 344d3432b3
3 changed files with 42 additions and 2 deletions

View File

@ -1447,7 +1447,11 @@ WrenInterpretResult wrenInterpret(WrenVM* vm, const char* module,
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,

View File

@ -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;
}

View File

@ -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