Fix bug #362 calling Foreign methods

I assume that invoking callForeign that the apiStack should be restored
to it's original value, rather than reset to NULL.
This commit is contained in:
Daniel Larimer 2016-10-26 13:45:35 -04:00
parent 57f34fab95
commit a3aaad83ea

View File

@ -366,6 +366,7 @@ static void bindMethod(WrenVM* vm, int methodType, int symbol,
static void callForeign(WrenVM* vm, ObjFiber* fiber, static void callForeign(WrenVM* vm, ObjFiber* fiber,
WrenForeignMethodFn foreign, int numArgs) WrenForeignMethodFn foreign, int numArgs)
{ {
Value* oldApiStack = vm->apiStack; /// save the current state so we can restore it when done
vm->apiStack = fiber->stackTop - numArgs; vm->apiStack = fiber->stackTop - numArgs;
foreign(vm); foreign(vm);
@ -373,7 +374,7 @@ static void callForeign(WrenVM* vm, ObjFiber* fiber,
// Discard the stack slots for the arguments and temporaries but leave one // Discard the stack slots for the arguments and temporaries but leave one
// for the result. // for the result.
fiber->stackTop = vm->apiStack + 1; fiber->stackTop = vm->apiStack + 1;
vm->apiStack = NULL; vm->apiStack = oldApiStack; /// restore apiStack to value prior to calling foreign method
} }
// Handles the current fiber having aborted because of an error. Switches to // Handles the current fiber having aborted because of an error. Switches to
@ -655,11 +656,12 @@ static void createForeign(WrenVM* vm, ObjFiber* fiber, Value* stack)
ASSERT(method->type == METHOD_FOREIGN, "Allocator should be foreign."); ASSERT(method->type == METHOD_FOREIGN, "Allocator should be foreign.");
// Pass the constructor arguments to the allocator as well. // Pass the constructor arguments to the allocator as well.
Value* oldApiStack = vm->apiStack;
vm->apiStack = stack; vm->apiStack = stack;
method->fn.foreign(vm); method->fn.foreign(vm);
vm->apiStack = NULL; vm->apiStack = oldApiStack;
// TODO: Check that allocateForeign was called. // TODO: Check that allocateForeign was called.
} }