| Index: src/x64/macro-assembler-x64.cc
 | 
| diff --git a/src/x64/macro-assembler-x64.cc b/src/x64/macro-assembler-x64.cc
 | 
| index fa8cf18fe1365e4fcfeec7b87351020f1912a844..2fb0f74b0b5bbd833089c4b659425473012a05ba 100644
 | 
| --- a/src/x64/macro-assembler-x64.cc
 | 
| +++ b/src/x64/macro-assembler-x64.cc
 | 
| @@ -691,13 +691,16 @@ void MacroAssembler::PrepareCallApiFunction(int arg_stack_space) {
 | 
|  }
 | 
|  
 | 
|  
 | 
| -void MacroAssembler::CallApiFunctionAndReturn(Address function_address,
 | 
| -                                              Address thunk_address,
 | 
| -                                              Register thunk_last_arg,
 | 
| -                                              int stack_space,
 | 
| -                                              int return_value_offset) {
 | 
| +void MacroAssembler::CallApiFunctionAndReturn(
 | 
| +    Address function_address,
 | 
| +    Address thunk_address,
 | 
| +    Register thunk_last_arg,
 | 
| +    int stack_space,
 | 
| +    Operand return_value_operand,
 | 
| +    Operand* context_restore_operand) {
 | 
|    Label prologue;
 | 
|    Label promote_scheduled_exception;
 | 
| +  Label exception_handled;
 | 
|    Label delete_allocated_handles;
 | 
|    Label leave_exit_frame;
 | 
|    Label write_back;
 | 
| @@ -768,7 +771,7 @@ void MacroAssembler::CallApiFunctionAndReturn(Address function_address,
 | 
|    }
 | 
|  
 | 
|    // Load the value from ReturnValue
 | 
| -  movq(rax, Operand(rbp, return_value_offset * kPointerSize));
 | 
| +  movq(rax, return_value_operand);
 | 
|    bind(&prologue);
 | 
|  
 | 
|    // No more valid handles (the result handle was the last one). Restore
 | 
| @@ -783,6 +786,7 @@ void MacroAssembler::CallApiFunctionAndReturn(Address function_address,
 | 
|    movq(rsi, scheduled_exception_address);
 | 
|    Cmp(Operand(rsi, 0), factory->the_hole_value());
 | 
|    j(not_equal, &promote_scheduled_exception);
 | 
| +  bind(&exception_handled);
 | 
|  
 | 
|  #if ENABLE_EXTRA_CHECKS
 | 
|    // Check if the function returned a valid JavaScript value.
 | 
| @@ -819,11 +823,19 @@ void MacroAssembler::CallApiFunctionAndReturn(Address function_address,
 | 
|    bind(&ok);
 | 
|  #endif
 | 
|  
 | 
| -  LeaveApiExitFrame();
 | 
| +  bool restore_context = context_restore_operand != NULL;
 | 
| +  if (restore_context) {
 | 
| +    movq(rsi, *context_restore_operand);
 | 
| +  }
 | 
| +  LeaveApiExitFrame(!restore_context);
 | 
|    ret(stack_space * kPointerSize);
 | 
|  
 | 
|    bind(&promote_scheduled_exception);
 | 
| -  TailCallRuntime(Runtime::kPromoteScheduledException, 0, 1);
 | 
| +  {
 | 
| +    FrameScope frame(this, StackFrame::INTERNAL);
 | 
| +    CallRuntime(Runtime::kPromoteScheduledException, 0);
 | 
| +  }
 | 
| +  jmp(&exception_handled);
 | 
|  
 | 
|    // HandleScope limit has changed. Delete allocated extensions.
 | 
|    bind(&delete_allocated_handles);
 | 
| @@ -3695,23 +3707,25 @@ void MacroAssembler::LeaveExitFrame(bool save_doubles) {
 | 
|  
 | 
|    PushReturnAddressFrom(rcx);
 | 
|  
 | 
| -  LeaveExitFrameEpilogue();
 | 
| +  LeaveExitFrameEpilogue(true);
 | 
|  }
 | 
|  
 | 
|  
 | 
| -void MacroAssembler::LeaveApiExitFrame() {
 | 
| +void MacroAssembler::LeaveApiExitFrame(bool restore_context) {
 | 
|    movq(rsp, rbp);
 | 
|    pop(rbp);
 | 
|  
 | 
| -  LeaveExitFrameEpilogue();
 | 
| +  LeaveExitFrameEpilogue(restore_context);
 | 
|  }
 | 
|  
 | 
|  
 | 
| -void MacroAssembler::LeaveExitFrameEpilogue() {
 | 
| +void MacroAssembler::LeaveExitFrameEpilogue(bool restore_context) {
 | 
|    // Restore current context from top and clear it in debug mode.
 | 
|    ExternalReference context_address(Isolate::kContextAddress, isolate());
 | 
|    Operand context_operand = ExternalOperand(context_address);
 | 
| -  movq(rsi, context_operand);
 | 
| +  if (restore_context) {
 | 
| +    movq(rsi, context_operand);
 | 
| +  }
 | 
|  #ifdef DEBUG
 | 
|    movq(context_operand, Immediate(0));
 | 
|  #endif
 | 
| 
 |