15.10 Tail Position Calls
15.10.1 Static Semantics: IsInTailPosition ( call )
The abstract operation IsInTailPosition takes argument call (a
- If the
source text matched by call isnon-strict code , returnfalse . - If call is not contained within a
FunctionBody , aConciseBody , or anAsyncConciseBody , returnfalse . - Let body be the
FunctionBody ,ConciseBody , orAsyncConciseBody that most closely contains call. - If body is the
FunctionBody of aGeneratorBody , returnfalse . - If body is the
FunctionBody of anAsyncFunctionBody , returnfalse . - If body is the
FunctionBody of anAsyncGeneratorBody , returnfalse . - If body is an
AsyncConciseBody , returnfalse . - Return the result of
HasCallInTailPosition of body with argument call.
Tail Position calls are only defined in
15.10.2 Static Semantics: HasCallInTailPosition
The
call is a
A potential tail position call that is immediately followed by return
It is defined piecewise over the following productions:
- Let has be
HasCallInTailPosition ofStatementList with argument call. - If has is
true , returntrue . - Return
HasCallInTailPosition ofStatementListItem with argument call.
- Return
false .
- Let has be
HasCallInTailPosition of the firstStatement with argument call. - If has is
true , returntrue . - Return
HasCallInTailPosition of the secondStatement with argument call.
- Return
HasCallInTailPosition ofStatement with argument call.
- Return
HasCallInTailPosition ofLabelledItem with argument call.
- Return
HasCallInTailPosition ofExpression with argument call.
- Return
HasCallInTailPosition ofCaseBlock with argument call.
- Let has be
false . - If the first
CaseClauses is present, set has toHasCallInTailPosition of the firstCaseClauses with argument call. - If has is
true , returntrue . - Set has to
HasCallInTailPosition ofDefaultClause with argument call. - If has is
true , returntrue . - If the second
CaseClauses is present, set has toHasCallInTailPosition of the secondCaseClauses with argument call. - Return has.
- Let has be
HasCallInTailPosition ofCaseClauses with argument call. - If has is
true , returntrue . - Return
HasCallInTailPosition ofCaseClause with argument call.
- If
StatementList is present, returnHasCallInTailPosition ofStatementList with argument call. - Return
false .
- Return
HasCallInTailPosition ofCatch with argument call.
- Return
HasCallInTailPosition ofFinally with argument call.
- Return
HasCallInTailPosition ofBlock with argument call.
- Return
false .
- Return
HasCallInTailPosition ofAssignmentExpression with argument call.
- Let has be
HasCallInTailPosition of the firstAssignmentExpression with argument call. - If has is
true , returntrue . - Return
HasCallInTailPosition of the secondAssignmentExpression with argument call.
- Return
HasCallInTailPosition ofBitwiseORExpression with argument call.
- Return
HasCallInTailPosition ofLogicalANDExpression with argument call.
- Return
HasCallInTailPosition ofBitwiseORExpression with argument call.
- If this
CallExpression is call, returntrue . - Return
false .
- Return
HasCallInTailPosition ofOptionalChain with argument call.
- Return
false .
- If this
OptionalChain is call, returntrue . - Return
false .
- If this
MemberExpression is call, returntrue . - Return
false .
- Let expr be the
ParenthesizedExpression that iscovered byCoverParenthesizedExpressionAndArrowParameterList . - Return
HasCallInTailPosition of expr with argument call.
- Return
HasCallInTailPosition ofExpression with argument call.
15.10.3 PrepareForTailCall ( )
The abstract operation PrepareForTailCall takes no arguments and returns
Assert : The currentexecution context will not subsequently be used for the evaluation of any ECMAScript code or built-in functions. The invocation of Call subsequent to the invocation of this abstract operation will create and push a newexecution context before performing any such evaluation.- Discard all resources associated with the current
execution context . - Return
unused .
A tail position call must either release any transient internal resources associated with the currently executing function
For example, a tail position call should only grow an implementation's activation record stack by the amount that the size of the target function's activation record exceeds the size of the calling function's activation record. If the target function's activation record is smaller, then the total size of the stack should decrease.