Skip to content

Commit

Permalink
implement PrepareForTailCall
Browse files Browse the repository at this point in the history
  • Loading branch information
devsnek committed Mar 11, 2020
1 parent 574448c commit 8c1f6f2
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
12 changes: 7 additions & 5 deletions src/abstract-ops/function-operations.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -400,9 +400,11 @@ export function CreateBuiltinFunction(steps, internalSlotsList, realm, prototype

// 14.9.3 #sec-preparefortailcall
export function PrepareForTailCall() {
// const leafContext = surroundingAgent.runningExecutionContext;
// Suspend(leafContext);
// surroundingAgent.executionContextStack.pop();
// Assert: leafContext has no further use. It will never
// be activated as the running execution context.
// 1. Let leafContext be the running execution context.
const leafContext = surroundingAgent.runningExecutionContext;
// 2. Suspend leafContext.
// 3. Pop leafContext from the execution context stack. The execution context now on the top of the stack becomes the running execution context.
surroundingAgent.executionContextStack.pop(leafContext);
// 4. Assert: leafContext has no further use. It will never be activated as the running execution context.
leafContext.poppedForTailCall = true;
}
9 changes: 5 additions & 4 deletions src/engine.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ export class Agent {

this.executionContextStack = [];
const stackPop = this.executionContextStack.pop;
this.executionContextStack.pop = function pop(...args) {
const popped = stackPop.call(this);
if (args.length === 1) {
Assert(args[0] === popped);
this.executionContextStack.pop = function pop(ctx) {
if (!ctx.poppedForTailCall) {
const popped = stackPop.call(this);
Assert(popped === ctx);
}
};

Expand Down Expand Up @@ -159,6 +159,7 @@ export class ExecutionContext {
// NON-SPEC
this.callSite = new CallSite(this);
this.promiseCapability = undefined;
this.poppedForTailCall = false;
}

copy() {
Expand Down

0 comments on commit 8c1f6f2

Please sign in to comment.