fix: close upvalues before fiber completion and add closure tests

Move upvalue closing logic from the else branch to before the fiber completion check
to ensure upvalues are properly closed when a fiber finishes. Add test cases for
closure behavior across fiber yields and for Fiber.create argument type validation.
This commit is contained in:
Bob Nystrom
2014-02-14 15:16:56 +00:00
parent 00239d37b2
commit 04cb19c605
4 changed files with 34 additions and 13 deletions
+8 -10
View File
@@ -883,14 +883,20 @@ static bool runInterpreter(WrenVM* vm)
Value result = POP();
fiber->numFrames--;
// Close any upvalues still in scope.
Value* firstValue = &fiber->stack[frame->stackStart];
while (fiber->openUpvalues != NULL &&
fiber->openUpvalues->value >= firstValue)
{
closeUpvalue(fiber);
}
// If the fiber is complete, end it.
if (fiber->numFrames == 0)
{
// If this is the main fiber, we're done.
if (fiber->caller == NULL) return true;
// TODO: Do we need to close upvalues here?
// We have a calling fiber to resume.
fiber = fiber->caller;
@@ -899,14 +905,6 @@ static bool runInterpreter(WrenVM* vm)
}
else
{
// Close any upvalues still in scope.
Value* firstValue = &fiber->stack[frame->stackStart];
while (fiber->openUpvalues != NULL &&
fiber->openUpvalues->value >= firstValue)
{
closeUpvalue(fiber);
}
// Store the result of the block in the first slot, which is where the
// caller expects it.
fiber->stack[frame->stackStart] = result;