14 ECMAScript语言:函数和类

注:各种ECMAScript语言元素导致创建ECMAScript函数对象(9.2)。对此类函数的运算始于其[[Call]]内部方法(9.2.1)的执行。

14.1 函数定义

语法

  1. FunctionDeclaration[Yield, Await, Default] :
  2. function BindingIdentifier[?Yield, ?Await] ( FormalParameters[~Yield, ~Await] ) {
  3. FunctionBody[~Yield, ~Await] }
  4. [+Default] function ( FormalParameters[~Yield, ~Await] ) { FunctionBody[~Yield, ~Await] }
  5. FunctionExpression :
  6. function BindingIdentifier[~Yield, ~Await] opt ( FormalParameters[~Yield, ~Await] ) {
  7. FunctionBody[~Yield, ~Await] }
  8. UniqueFormalParameters[Yield, Await] :
  9. FormalParameters[?Yield, ?Await]
  10. FormalParameters[Yield, Await] :
  11. [empty]
  12. FunctionRestParameter[?Yield, ?Await]
  13. FormalParameterList[?Yield, ?Await]
  14. FormalParameterList[?Yield, ?Await] ,
  15. FormalParameterList[?Yield, ?Await] , FunctionRestParameter[?Yield, ?Await]
  16. FormalParameterList[Yield, Await] :
  17. FormalParameter[?Yield, ?Await]
  18. FormalParameterList[?Yield, ?Await] , FormalParameter[?Yield, ?Await]
  19. FunctionRestParameter[Yield, Await] :
  20. BindingRestElement[?Yield, ?Await]
  21. FormalParameter[Yield, Await] :
  22. BindingElement[?Yield, ?Await]
  23. FunctionBody[Yield, Await] :
  24. FunctionStatementList[?Yield, ?Await]
  25. FunctionStatementList[Yield, Await] :
  26. StatementList[?Yield, ?Await, +返回] opt

14.1.1 指令序言和严格使用指令

指令序言是最长的ExpressionStatements序列,出现在FunctionBody,ScriptBody或ModuleBody的初始StatementListItems或ModuleItems上,并且序列中的每个ExpressionStatement完全由StringLiteral标记后跟一个分号组成。分号可以显式显示,也可以通过自动分号插入来插入。指令序言可能是空序列。

使用严格指令是指令序言中的ExpressionStatement,其StringLiteral是 “use strict” 或 ‘use strict’的确切代码单元序列。使用严格指令可能不包含EscapeSequence或LineContinuation。

指令序言中可能包含多个严格使用指令。但是,若发生这种情况,则实现可能会发出警告。

注:指令序言的ExpressionStatement语句通常在包含产生式的求值过程中进行计算。实现可以为ExpressionStatement定义实现特定的含义,这些含义不是“使用严格”指令,而是在指令序言中出现。若存在适当的通知机制,则若实现在指令序言中遇到不是Use Strict指令且没有实现定义的含义的ExpressionStatement,则该实现应发出警告。

14.1.2 静态语义:Early Errors

  1. FunctionDeclaration : function BindingIdentifier ( FormalParameters ) { FunctionBody }
  2. FunctionDeclaration : function ( FormalParameters ) { FunctionBody }
  3. FunctionExpression : function BindingIdentifier ( FormalParameters ) { FunctionBody }
  • 若与此产生式匹配的源代码是严格模式代码,则应用 UniqueFormalParameters: FormalParameters 的早期错误规则。
  • 若与此产生式匹配的源代码是严格模式代码,那么若BindingIdentifier是存在的,并且BindingIdentifier的StringValue是”eval”或”arguments”,那么这就是一个语法错误。
  • 若FunctionBody的ContainsUseStrict为真,FormalParameters的IsSimpleParameterList为假,则为语法错误。
  • 若FormalParameters的BoundNames的任何元素也出现在FunctionBody的LexicallyDeclaredNames中,这是一个语法错误。
  • 若包含SuperProperty的FormalParameters为真,则为语法错误。
  • 若包含SuperProperty的FunctionBody为真,则为语法错误。
  • 若包含SuperCall的FormalParameters为真,则为语法错误。
  • 若包含SuperCall的FunctionBody为真,则为语法错误。

注 1:FunctionBody的LexicallyDeclaredNames不包括使用var或函数声明绑定的标识符

  1. UniqueFormalParameters : FormalParameters

若FormalParameters的BoundNames包含任何重复的元素,则是语法错误。

  1. FormalParameters : FormalParameterList

若FormalParameterList的IsSimpleParameterList为false,并且FormalParameterList的BoundNames包含任何重复元素,则是语法错误。

注 2:仅对于具有简单参数列表且未在严格模式代码中定义的函数,允许FormalParameterList中多次出现同一BindingIdentifier。

  1. FunctionBody : FunctionStatementList
  • 若FunctionStatementList的LexicallyDeclaredNames包含任何重复的条目,则是语法错误。
  • 若FunctionStatementList的LexicallyDeclaredNames的任何元素也出现在FunctionStatementList的VarDeclaredNames中,则是语法错误。
  • 若FunctionStatementList的ContainsDuplicateLabels和参数«»为true,则为语法错误。
  • 若FunctionStatementList的ContainsUndefinedBreakTarget带有参数«»,则为语法错误。
  • 若FunctionStatementList的ContainsUndefinedContinueTarget带有参数«»,并且«»为true,则为语法错误。

14.1.3 静态语义:BoundNames

  1. FunctionDeclaration : function BindingIdentifier ( FormalParameters ) { FunctionBody }
  1. 返回 the BindingIdentifier 的 BoundNames
  1. FunctionDeclaration : function ( FormalParameters ) { FunctionBody }
  1. 返回 « “*default*“ ».

“*default*“在本规范中用作使用导出声明定义的可提升匿名函数的合成名称。

  1. FormalParameters : [empty]
  1. 返回一个新的空列表
  1. FormalParameters : FormalParameterList , FunctionRestParameter
  1. 令 names 为 FormalParameterList 的 BoundNames
  2. 在 names 后追加 FunctionRestParameter 的 BoundNames。
  3. 返回 names.
  1. FormalParameterList : FormalParameterList , FormalParameter
  1. 令 names 为 FormalParameterList 的 BoundNames
  2. 在 names 后追加 FormalParameter 的 BoundNames
  3. 返回 names.

14.1.4 静态语义:包含

带有参数 symbol.

  1. FunctionDeclaration : function BindingIdentifier ( FormalParameters ) { FunctionBody }
  2. FunctionDeclaration : function ( FormalParameters ) { FunctionBody }
  3. FunctionExpression : function BindingIdentifier ( FormalParameters ) { FunctionBody }
  1. 返回 false.

注:依赖于子结构的静态语义规则通常不会考虑函数定义。

14.1.5 静态语义:ContainsExpression

  1. FormalParameters : [empty]
  1. 返回 false.
  1. FormalParameters : FormalParameterList , FunctionRestParameter
  1. 若 FormalParameterList 的 ContainsExpression 是 true, 返回 true.
  2. 返回 FunctionRestParameter 的 ContainsExpression
  1. FormalParameterList : FormalParameterList , FormalParameter
  1. 若 FormalParameterList 的 ContainsExpression 是 true, 返回 true.
  2. 返回 FormalParameter 的 ContainsExpression

14.1.6 静态语义:ContainsUseStrict

  1. FunctionBody : FunctionStatementList
  1. 若 FunctionStatementList的指令序言包含一个使用严格的指令, 返回 true; 否则, 返回 false.

14.1.7 静态语义:ExpectedArgumentCount

  1. FormalParameters : [empty]
  1. 返回 0.
  1. FormalParameters : FormalParameterList , FunctionRestParameter
  1. 返回 FormalParameterList 的 ExpectedArgumentCount

注:FormalParameterList的ExpetedArgumentCount是REST参数左边的FormalParameters数,或者是带有初始化器的第一个FormalParameter数。在带有初始化器的第一个参数之后,允许使用没有初始化器的FormalParameters,但是这些参数被认为是可选的,undefined为它们的默认值。

  1. FormalParameterList : FormalParameterList , FormalParameter
  1. 令 count 为 FormalParameterList 的 ExpectedArgumentCount
  2. 若FormAlParameterList的HasInitializer设定为true,或者FormAlParameter的HasInitializer设定为true, 返回 count.
  3. 返回 count + 1.

14.1.8 静态语义:HasInitializer

  1. FormalParameterList : FormalParameterList , FormalParameter
  1. 若 FormalParameterList 的 HasInitializer 是 true, 返回 true.
  2. 返回 FormalParameter 的 HasInitializer

14.1.9 静态语义:HasName

  1. FunctionExpression : function ( FormalParameters ) { FunctionBody }
  1. 返回 false.
  1. FunctionExpression : function BindingIdentifier ( FormalParameters ) { FunctionBody }
  1. 返回 true.

14.1.10 静态语义:IsAnonymousFunctionDefinition ( expr )

抽象操作IsAnonyousFunctionDefinition确定其参数是否是不绑定名称的函数定义。参数expr是AssignmentExpression或Initializer的结果。采取了以下步骤:

  1. 若 expr 的 IsFunctionDefinition 是 false, 返回 false.
  2. 令 hasName 为 expr 的 HasName 的结果
  3. 若 hasName 是 true, 返回 false.
  4. 返回 true.

14.1.11 静态语义:IsConstantDeclaration

  1. FunctionDeclaration : function BindingIdentifier ( FormalParameters ) { FunctionBody }
  2. FunctionDeclaration : function ( FormalParameters ) { FunctionBody }
  1. 返回 false.

14.1.12 静态语义:IsFunctionDefinition

  1. FunctionExpression : function BindingIdentifier ( FormalParameters ) { FunctionBody }
  1. 返回 true.

14.1.13 静态语义:IsSimpleParameterList

  1. FormalParameters : [empty]
  1. 返回 true.
  1. FormalParameters : FormalParameterList , FunctionRestParameter
  1. 返回 false
  1. FormalParameterList : FormalParameterList , FormalParameter
  1. 若 FormalParameterList 的 IsSimpleParameterList 是 false, 返回 false.
  2. 返回 FormalParameter 的 IsSimpleParameterList
  1. FormalParameter : BindingElement
  1. 返回 BindingElement 的 IsSimpleParameterList

14.1.14 静态语义:LexicallyDeclaredNames

  1. FunctionStatementList : [empty]
  1. 返回一个新的空列表
  1. FunctionStatementList : StatementList
  1. 返回 TopLevelLexicallyDeclaredNames of StatementList

14.1.15 静态语义:LexicallyScopedDeclarations

  1. FunctionStatementList : [empty]
  1. 返回一个新的空列表.
  1. FunctionStatementList : StatementList
  1. 返回 the StatementList 的 TopLevelLexicallyScopedDeclarations

14.1.16 静态语义:VarDeclaredNames

  1. FunctionStatementList : [empty]
  1. 返回一个新的空列表.
  1. FunctionStatementList : StatementList
  1. 返回 StatementList 的 TopLevelVarDeclaredNames

14.1.17 静态语义:VarScopedDeclarations

  1. FunctionStatementList : [empty]
  1. 返回一个新的空列表.
  1. FunctionStatementList : StatementList
  1. 返回 the StatementList 的 TopLevelVarScopedDeclarations

14.1.18 运行时语义:EvaluateBody

带有参数 functionObject 和 argumentsList列表.

  1. FunctionBody : FunctionStatementList
  1. 执行 ? FunctionDeclarationInstantiation(functionObject, argumentsList).
  2. 返回运算 FunctionStatementList 的结果

14.1.19 运行时语义:IteratorBindingInitialization

带有参数 iteratorRecord 和 environment

注 1:当为环境传递undefined时,表明应该使用PutValue操作来分配初始化值。非严格函数的形式参数列表就是这种情况。在那种情况下,形式参数绑定是预先初始化的,以便处理多个具有相同名称的参数的可能性。

  1. FormalParameters : [empty]
  1. 返回 NormalCompletion(empty).
  1. FormalParameters : FormalParameterList , FunctionRestParameter
  1. 执行 ? 使用iteratorRecord和环境作为参数的FormalParameterList的IteratorBindingInitialization
  2. 返回使用iteratorRecord和环境作为参数对FunctionRestParameter执行IteratorBindingInitialization的结果。
  1. FormalParameterList : FormalParameterList , FormalParameter
  1. 执行 ? 使用iteratorRecord和环境作为参数的FormalParameterList的IteratorBindingInitialization。
  2. 返回使用iteratorRecord和环境作为参数对FormalParameter执行IteratorBindingInitialization的结果。
  1. FormalParameter : BindingElement
  1. 若 BindingElement 的 ContainsExpression 是 false, 返回使用iteratorRecord和环境作为参数对BindingElement执行IteratorBindingInitialization的结果。
  2. 令 currentContext 为运行时上下文.
  3. 令 originalEnv 为 currentContext 的 VariableEnvironment
  4. 断言: currentContext的VariableEnvironment和LexicalEnvironment相同。
  5. 断言: environment和originalEnv相同。
  6. 令 paramVarEnv 为 NewDeclarativeEnvironment(originalEnv).
  7. 设置currentContext的VariableEnvironment为paramVarEnv
  8. 设置currentContext的LexicalEnvironment为paramVarEnv
  9. 令 result 为使用iteratorRecord和environment作为参数对BindingElement执行IteratorBindingInitialization的结果。
  10. 设置currentContext的VariableEnvironment为originalEnv
  11. 设置currentContext的LexicalEnvironment为originalEnv
  12. 返回 result.

注 2 仅当BindingElement包含直接运算时,才使用在步骤6中创建的新环境记录。

  1. FunctionRestParameter : BindingRestElement
  1. 若 BindingRestElement 的 ContainsExpression 是 false, 返回使用iteratorRecord和environment作为参数对BindingRestElement执行IteratorBindingInitialization的结果。
  2. 令 currentContext 为 运行时上下文.
  3. 令 originalEnv 为 currentContext 的 VariableEnvironment
  4. 断言: currentContext的VariableEnvironment和LexicalEnvironment相同。
  5. 断言: environment和originalEnv相同。
  6. 令 paramVarEnv 为 NewDeclarativeEnvironment(originalEnv).
  7. 设置currentContext的VariableEnvironment为paramVarEnv
  8. 设置currentContext的LexicalEnvironment为paramVarEnv
  9. 令 result 为使用iteratorRecord和environment作为参数对BindingRestElement执行IteratorBindingInitialization的结果。
  10. 设置currentContext的VariableEnvironment为originalEnv
  11. 设置currentContext的LexicalEnvironment为originalEnv
  12. 返回 result.

注 3:仅当BindingRestElement包含直接运算时,才使用在步骤6中创建的新环境记录。

14.1.20 运行时语义:InstantiateFunctionObject

带有参数 scope.

  1. FunctionDeclaration : function BindingIdentifier ( FormalParameters ) { FunctionBody }
  1. 若 FunctionDeclaration 的函数代码是严格模式代码, 令 strict 为 true. 否则,令 strict 为 false.
  2. 令 name 为 BindingIdentifier 的 StringValue
  3. 令 F 为 FunctionCreate(Normal, FormalParameters, FunctionBody, scope, strict).
  4. 执行 MakeConstructor(F).
  5. 执行 SetFunctionName(F, name).
  6. 设置 F.[[SourceText]] 为由FunctionDeclaration匹配的源文本。
  7. 返回 F.
  1. FunctionDeclaration : function ( FormalParameters ) { FunctionBody }
  1. 令 F 为 FunctionCreate(Normal, FormalParameters, FunctionBody, scope, true).
  2. 执行 MakeConstructor(F).
  3. 执行 SetFunctionName(F, “default”).
  4. 设置 F.[[SourceText]]由FunctionDeclaration匹配的源文本。
  5. 返回 F

注:匿名FunctionDeclaration只能作为 export default 声明的一部分出现,因此其函数代码始终是严格模式代码。

14.1.21 运行时语义:NamedEvaluation

带有参数 name.

  1. FunctionExpression : function ( FormalParameters ) { FunctionBody }
  1. 令 closure 为运算 this 的结果FunctionExpression.
  2. 执行 SetFunctionName(closure, name).
  3. 返回 closure.

14.1.22 运行时语义:Evaluation

  1. FunctionDeclaration : function BindingIdentifier ( FormalParameters ) { FunctionBody }
  1. 返回 NormalCompletion(empty).

注 1:B.3.3中提供了另一种语义

  1. FunctionDeclaration : function ( FormalParameters ) { FunctionBody }
  1. 返回 NormalCompletion(empty).
  1. FunctionExpression : function ( FormalParameters ) { FunctionBody }
  1. 若 FunctionExpression 的函数代码是严格模式代码, 令 strict 为 true. 否则,令 strict 为 false.
  2. 令 scope 为运行时上下文的 LexicalEnvironment。
  3. 令 closure 为 FunctionCreate(Normal, FormalParameters, FunctionBody, scope, strict).
  4. 执行 MakeConstructor(closure).
  5. 设置 closure.[[SourceText]] 为由 FunctionExpression 匹配的源文本。
  6. 返回 closure.
  1. FunctionExpression : function BindingIdentifier ( FormalParameters ) { FunctionBody }
  1. 若 FunctionExpression 的函数代码是严格模式代码, 令 strict 为 true. 否则,令 strict 为 false.
  2. 令 scope 为运行时上下文的 LexicalEnvironment.
  3. 令 funcEnv 为 NewDeclarativeEnvironment(scope).
  4. 令 envRec 为 funcEnv’ 的环境记录项.
  5. 令 name 为 BindingIdentifier 的 StringValue
  6. 执行 envRec.CreateImmutableBinding(name, false).
  7. 令 closure 为 FunctionCreate(Normal, FormalParameters, FunctionBody, funcEnv, strict).
  8. 执行 MakeConstructor(closure).
  9. 执行 SetFunctionName(closure, name).
  10. 设置 closure.[[SourceText]] 为由FunctionExpression匹配的源文本。
  11. 执行 envRec.InitializeBinding(name, closure).
  12. 返回 closure

注 2:可以从FunctionExpression的FunctionBody内部引用FunctionExpression中的BindingIdentifier,以允许该函数以递归方式调用其自身。但是,与FunctionDeclaration中的功能不同,FunctionExpression中的BindingIdentifier不能从其引用,也不影响包围FunctionExpression的作用域。

注 3:将为使用FunctionDeclaration或FunctionExpression定义的每个函数自动创建一个原型属性,以允许将该函数用作构造函数。

  1. FunctionStatementList : [empty]
  1. 返回 NormalCompletion(undefined).

14.2 箭头函数定义

语法

  1. ArrowFunction[In, Yield, Await] :
  2. ArrowParameters[?Yield, ?Await] [no LineTerminator here] => ConciseBody[?In]
  3. ArrowParameters[Yield, Await] :
  4. BindingIdentifier[?Yield, ?Await]
  5. CoverParenthesizedExpressionAndArrowParameterList[?Yield, ?Await]
  6. ConciseBody[In] :
  7. [lookahead {] AssignmentExpression[?In, ~Yield, ~Await]
  8. { FunctionBody[~Yield, ~Await] }

补充语法

当产生式

  1. ArrowParameters : CoverParenthesizedExpressionAndArrowParameterList

公认以下语法用于完善对

  1. CoverParenthesizedExpressionAndArrowParameterList:
  1. ArrowFormalParameters[Yield]:
  2. (StrictFormalParameters[?Yield])

14.2.1 静态语义:Early Errors

  1. ArrowFunction : ArrowParameters => ConciseBody
  • 若 ArrowParameters 包含 YieldExpression 是 true,则为语法错误
  • 若 ArrowParameters 包含AwaitExpression 是 true,则为语法错误
  • 若 ConciseBody 的 ContainsUseStrict 是 true 并且 ArrowParameters 的 IsSimpleParameterList 是 false,则为语法错误
  • 若ConciseBody的LexicallyDeclaredNames中也出现ArrowParameters的BoundNames的任何元素,则是语法错误。
  1. ArrowParameters : CoverParenthesizedExpressionAndArrowParameterList
  • 若CoverParenthesizedExpressionAndArrowParameterList没有覆盖ArrowFormalParameters,则是语法错误。
  • ArrowFormalParameters及其派生生产式的所有早期错误规则也适用于CoverParenthesizedExpressionAndArrowParameterList的CoveredFormalsList。

14.2.2 静态语义:BoundNames

  1. ArrowParameters : CoverParenthesizedExpressionAndArrowParameterList
  1. 令 formals 为 CoverParenthesizedExpressionAndArrowParameterList 的 CoveredFormalsList
  2. 返回 the formals 的 BoundNames

14.2.3 静态语义:包含

带有参数 symbol

  1. ArrowFunction : ArrowParameters => ConciseBody
  1. 若 symbol 不是 NewTarget, SuperProperty, SuperCall, super 或 this其中之一, 返回 false.
  2. 若 ArrowParameters 包含 symbol 是 true, 返回 true.
  3. 返回 ConciseBody 包含 symbol.

注:通常,包含不会在大多数函数形式中出现。但是,Contains用于检测ArrowFunction中的new.target,this和super用法。

  1. ArrowParameters : CoverParenthesizedExpressionAndArrowParameterList
  1. 令 formals 为 CoverParenthesizedExpressionAndArrowParameterList 的 CoveredFormalsList
  2. 返回 formals 包含 symbol.

14.2.4 静态语义:ContainsExpression

  1. ArrowParameters : BindingIdentifier
  1. 返回 false.

14.2.5 静态语义:ContainsUseStrict

  1. ConciseBody : AssignmentExpression
  1. 返回 false.

14.2.6 静态语义:ExpectedArgumentCount

  1. ArrowParameters : BindingIdentifier
  1. 返回 1.

14.2.7 静态语义:HasName

  1. ArrowFunction : ArrowParameters => ConciseBody
  1. 返回 false.

14.2.8 静态语义:IsSimpleParameterList

  1. ArrowParameters : BindingIdentifier
  1. 返回 true.
  1. ArrowParameters : CoverParenthesizedExpressionAndArrowParameterList
  1. 令 formals 为 CoverParenthesizedExpressionAndArrowParameterList 的 CoveredFormalsList
  2. 返回 formals 的 IsSimpleParameterList

14.2.9 静态语义:CoveredFormalsList

  1. ArrowParameters : BindingIdentifier
  1. 返回 this ArrowParameters.

    1. CoverParenthesizedExpressionAndArrowParameterList :
    2. ( Expression )
    3. ( )
    4. ( ... BindingIdentifier )
    5. ( ... BindingPattern )
    6. ( Expression , ... BindingIdentifier )
    7. ( Expression , ... BindingPattern )
    1. 返回CoverParenthesizedExpressionAndArrowParameterList涵盖的ArrowFormalParameters

14.2.10 静态语义:LexicallyDeclaredNames

  1. ConciseBody : AssignmentExpression
  1. 返回一个新的空列表.

14.2.11 静态语义:LexicallyScopedDeclarations

  1. ConciseBody : AssignmentExpression
  1. 返回一个新的空列表.

14.2.12 静态语义:VarDeclaredNames

  1. ConciseBody : AssignmentExpression
  1. 返回一个新的空列表.

14.2.13 静态语义:VarScopedDeclarations

  1. ConciseBody : AssignmentExpression
  1. 返回一个新的空列表.

14.2.14 运行时语义:IteratorBindingInitialization

带有参数 iteratorRecord 和 environment.

注:当为环境传递undefined时,表明应该使用PutValue操作来分配初始化值。非严格函数的形式参数列表就是这种情况。在那种情况下,形式参数绑定是预先初始化的,以便处理多个具有相同名称的参数的可能性。

  1. ArrowParameters : BindingIdentifier
  1. 断言: iteratorRecord.[[Done]] 是 false.
  2. 令 next 为 IteratorStep(iteratorRecord).
  3. 若 next 是 abrupt completion, 设置 iteratorRecord.[[Done]] 为 true.
  4. ReturnIfAbrupt(next).
  5. 若 next 是 false, 设置 iteratorRecord.[[Done]] 为 true.
  6. 否则,
  7. 令 v 为 IteratorValue(next).
  8. 若 v 是 abrupt completion, 设置 iteratorRecord.[[Done]] 为 true.
  9. ReturnIfAbrupt(v).
  10. 若 iteratorRecord.[[Done]] 是 true, 令 v 为 undefined.
  11. 返回使用v和environment作为参数对BindingIdentifier执行BindingInitialization的结果。

14.2.15 运行时语义:EvaluateBody

带有参数 functionObject 和 argumentsList列表.

  1. ConciseBody : AssignmentExpression
  1. 执行 ? FunctionDeclarationInstantiation(functionObject, argumentsList).
  2. 令 exprRef 为运算 AssignmentExpression 的结果
  3. 令 exprValue 为 ? GetValue(exprRef).
  4. 返回 Completion { [[Type]]: return, [[Value]]: exprValue, [[Target]]: empty }.

14.2.16 运行时语义:NamedEvaluation

带有参数 name.

  1. ArrowFunction : ArrowParameters => ConciseBody
  1. 令 closure 为运算此ArrowFunction的结果.
  2. 执行 SetFunctionName(closure, name).
  3. 返回 closure.

14.2.17 运行时语义:Evaluation

  1. ArrowFunction : ArrowParameters => ConciseBody
  1. 若此ArrowFunction的函数代码是严格模式代码, 令 strict 为 true. 否则,令 strict 为 false.
  2. 令 scope 为运行时上下文的LexicalEnvironment.
  3. 令 parameters 为 ArrowParameters 的 CoveredFormalsList
  4. 令 closure 为 FunctionCreate(Arrow, parameters, ConciseBody, scope, strict).
  5. 设置 closure.[[SourceText]] 为ArrowFunction匹配的源文本.
  6. 返回 closure.

注:ArrowFunction没有为arguments,super,this或new.target定义本地绑定。 ArrowFunction中对arguments,super,this或new.target的任何引用都必须解析为词法包围环境中的绑定。通常,这将是立即封闭函数的函数环境。即使ArrowFunction可能包含对super的引用,也无法通过执行MakeMethod将第4步中创建的函数对象制成方法。引用super的ArrowFunction始终包含在nonArrowFunction中,并且可以通过ArrowFunction的函数对象捕获的范围来访问实现super的必要状态。

14.3 方法定义

语法

  1. MethodDefinition[Yield, Await] :
  2. PropertyName[?Yield, ?Await] ( UniqueFormalParameters[~Yield, ~Await] ) {
  3. FunctionBody[~Yield, ~Await] }
  4. GeneratorMethod[?Yield, ?Await]
  5. AsyncMethod[?Yield, ?Await]
  6. AsyncGeneratorMethod[?Yield, ?Await]
  7. get PropertyName[?Yield, ?Await] ( ) { FunctionBody[~Yield, ~Await] }
  8. set PropertyName[?Yield, ?Await] ( PropertySetParameterList ) {
  9. FunctionBody[~Yield, ~Await] }
  10. PropertySetParameterList :
  11. FormalParameter[~Yield, ~Await]

14.3.1 静态语义:Early Errors

  1. MethodDefinition : PropertyName ( UniqueFormalParameters ) { FunctionBody }
  • 若FunctionBody的ContainsUseStrict为true并且UniqueFormalParameters的IsSimpleParameterList为false,则为语法错误
  • 若FunctionBody的LexicallyDeclaredNames中还出现UniqueFormalParameters的BoundNames的任何元素,则是语法错误。
  1. MethodDefinition : set PropertyName ( PropertySetParameterList ) { FunctionBody }
  • 若PropertySetParameterList的BoundNames包含任何重复的元素,则为语法错误
  • 若FunctionBody的ContainsUseStrict为true,PropertySetParameterList的IsSimpleParameterList为false,则为语法错误。
  • 若FunctionBody的LexicallyDeclaredNames中还出现了PropertySetParameterList的BoundNames的任何元素,则是语法错误。

14.3.2 静态语义:ComputedProperty包含

带有参数 symbol.

  1. MethodDefinition :
  2. PropertyName ( UniqueFormalParameters ) { FunctionBody }
  3. get PropertyName ( ) { FunctionBody }
  4. set PropertyName ( PropertySetParameterList ) { FunctionBody }
  1. 返回具有symbol参数的PropertyName的ComputedPropertyContains结果。

14.3.3 静态语义:ExpectedArgumentCount

  1. PropertySetParameterList : FormalParameter
  1. 若 FormalParameter 的 HasInitializer 是 true, 返回 0.
  2. 返回 1.

14.3.4 静态语义:HasDirectSuper

  1. MethodDefinition : PropertyName ( UniqueFormalParameters ) { FunctionBody }
  1. 若 UniqueFormalParameters 包含 SuperCall 是 true, 返回 true.
  2. 返回 FunctionBody 包含SuperCall.
  1. MethodDefinition : get PropertyName ( ) { FunctionBody }
  1. 返回 FunctionBody 包含SuperCall
  1. MethodDefinition : 设置 PropertyName ( PropertySetParameterList ) { FunctionBody }
  1. 若 PropertySetParameterList 包含SuperCall 是 true, 返回 true.
  2. 返回 FunctionBody 包含SuperCall.

14.3.5 静态语义:PropName

  1. MethodDefinition :
  2. PropertyName ( UniqueFormalParameters ) { FunctionBody }
  3. get PropertyName ( ) { FunctionBody }
  4. 设置 PropertyName ( PropertySetParameterList ) { FunctionBody }
  1. 返回 PropertyName 的 PropName

14.3.6 静态语义:SpecialMethod

  1. MethodDefinition : PropertyName ( UniqueFormalParameters ) { FunctionBody }
  1. 返回 false.

    1. MethodDefinition :
    2. GeneratorMethod
    3. AsyncMethod
    4. AsyncGeneratorMethod
    5. get PropertyName ( ) { FunctionBody }
    6. 设置 PropertyName ( PropertySetParameterList ) { FunctionBody }
    1. 返回 true.

14.3.7 运行时语义:DefineMethod

带有参数 object 和 optional parameter functionPrototype.

  1. MethodDefinition : PropertyName ( UniqueFormalParameters ) { FunctionBody }
  1. 令 propKey 为运算 PropertyName 的结果
  2. ReturnIfAbrupt(propKey).
  3. 若此MethodDefinition的函数代码是严格模式代码, 令 strict 为 true. 否则,令 strict 为 false.
  4. 令 scope 为 运行时上下文的 LexicalEnvironment.
  5. 若 functionPrototype 作为参数存在, 那么
    1. 令 kind 为 Normal.
    2. 令 prototype 为 functionPrototype.
  6. 否则,
    1. 令 kind 为 Method.
      1. 令 prototype 为内部对象 %FunctionPrototype%.
  7. 令 closure 为 FunctionCreate(kind, UniqueFormalParameters, FunctionBody, scope, strict, prototype).
  8. 执行 MakeMethod(closure, object).
  9. 设置 closure.[[SourceText]] 为MethodDefinition匹配的源文本.
  10. 返回 the Record { [[Key]]: propKey, [[Closure]]: closure }.

14.3.8 运行时语义:PropertyDefinitionEvaluation

带有参数 object 和 enumerable.

  1. MethodDefinition : PropertyName ( UniqueFormalParameters ) { FunctionBody }
  1. 令 methodDef 为带参数对象的MethodDefinition的DefineMethod。
  2. ReturnIfAbrupt(methodDef).
  3. 执行 SetFunctionName(methodDef.[[Closure]], methodDef.[[Key]]).
  4. 令 desc 为 the PropertyDescriptor { [[Value]]: methodDef.[[Closure]], [[Writable]]: true, [[Enumerable]]: enumerable, [[Configurable]]: true }.
  5. 返回 ? DefinePropertyOrThrow(object, methodDef.[[Key]], desc)
  1. MethodDefinition : get PropertyName ( ) { FunctionBody }
  1. 令 propKey 为运算 PropertyName 的结果
  2. ReturnIfAbrupt(propKey).
  3. 若此MethodDefinition的函数代码是严格模式代码, 令 strict 为 true. 否则,令 strict 为 false.
  4. 令 scope 为 运行时上下文的 LexicalEnvironment.
  5. 令 formalParameterList 为产生式的一个实例 FormalParameters : [empty] .
  6. 令 closure 为 FunctionCreate(Method, formalParameterList, FunctionBody, scope, strict).
  7. 执行 MakeMethod(closure, object).
  8. 执行 SetFunctionName(closure, propKey, “get”).
  9. 设置 closure.[[SourceText]] 为MethodDefinition匹配的源文本.
  10. 令 desc 为 the PropertyDescriptor { [[Get]]: closure, [[Enumerable]]: enumerable, [[Configurable]]: true }.
  11. 返回 ? DefinePropertyOrThrow(object, propKey, desc).
  1. MethodDefinition : set PropertyName ( PropertySetParameterList ) { FunctionBody }
  1. 令 propKey 为运算 PropertyName 的结果
  2. ReturnIfAbrupt(propKey).
  3. 若此MethodDefinition的函数代码是严格模式代码, 令 strict 为 true. 否则,令 strict 为 false.
  4. 令 scope 为 运行时上下文的 LexicalEnvironment.
  5. 令 closure 为 FunctionCreate(Method, PropertySetParameterList, FunctionBody, scope, strict).
  6. 执行 MakeMethod(closure, object).
  7. 执行 SetFunctionName(closure, propKey, “set”).
  8. 设置 closure.[[SourceText]] 为MethodDefinition匹配的源文本.
  9. 令 desc 为 the PropertyDescriptor { [[Set]]: closure, [[Enumerable]]: enumerable, [[Configurable]]: true }.
  10. 返回 ? DefinePropertyOrThrow(object, propKey, desc).

14.4 Generator 函数定义

语法

  1. GeneratorMethod[Yield, Await] :
  2. * PropertyName[?Yield, ?Await] ( UniqueFormalParameters[+Yield, ~Await] ) { GeneratorBody }
  3. GeneratorDeclaration[Yield, Await, Default] :
  4. function * BindingIdentifier[?Yield, ?Await] ( FormalParameters[+Yield, ~Await] ) { GeneratorBody }
  5. [+Default] function * ( FormalParameters[+Yield, ~Await] ) { GeneratorBody } GeneratorExpression :
  6. function * BindingIdentifier[+Yield, ~Await] opt ( FormalParameters[+Yield, ~Await] ) { GeneratorBody }
  7. GeneratorBody :
  8. FunctionBody[+Yield, ~Await]
  9. YieldExpression[In, Await] :
  10. yield
  11. yield [no LineTerminator here] AssignmentExpression[?In, +Yield, ?Await]
  12. yield [no LineTerminator here] * AssignmentExpression[?In, +Yield, ?Await]

注 1 紧随产生之后的句法上下文需要使用 InputElementRegExpOrTemplateTail 词法目标。

注 2 YieldExpression不能在生成器函数的FormalParameters中使用,因为在生成的生成器对象处于可恢复状态之前,将计算FormalParameters组成部分的任何表达式。

注 3 与生成器对象有关的抽象操作在25.4.3中定义。

14.4.1 静态语义:Early Errors

  1. GeneratorMethod : * PropertyName ( UniqueFormalParameters ) { GeneratorBody }
  • 若 GeneratorMethod 的 HasDirectSuper 是 true,则为语法错误
  • 若 UniqueFormalParameters 包含YieldExpression 是 true,则为语法错误
  • 若GeneratorBody的ContainsUseStrict为true并且UniqueFormalParameters的IsSimpleParameterList为false,则为语法错误
  • 若GeneratorBody的LexicallyDeclaredNames中还出现UniqueFormalParameters BoundNames的任何元素,则是语法错误。
  1. GeneratorDeclaration : function * BindingIdentifier ( FormalParameters ) { GeneratorBody }
  2. GeneratorDeclaration : function * ( FormalParameters ) { GeneratorBody }
  3. GeneratorExpression : function * BindingIdentifier ( FormalParameters ) { GeneratorBody }
  • 若与此产生式匹配的源代码是严格模式代码,则将应用UniqueFormalParameters:FormalParameters的早期错误规则。
  • 若与此产生式匹配的源代码是严格模式代码,则若存在BindingIdentifier并且BindingIdentifier的StringValue是 “eval” 或 “arguments”,则它是语法错误。
  • 若GeneratorBody的ContainsUseStrict为true,FormalParameters的IsSimpleParameterList为false,则为语法错误。
  • 若FormalParameters的BoundNames的任何元素也会出现在GeneratorBody的LexicallyDeclaredNames中,则为语法错误
  • 若 FormalParameters 包含YieldExpression 是 true,则为语法错误
  • 若 FormalParameters 包含SuperProperty 是 true,则为语法错误
  • 若 GeneratorBody 包含SuperProperty 是 true,则为语法错误
  • 若 FormalParameters 包含SuperCall 是 true,则为语法错误
  • 若 GeneratorBody 包含SuperCall 是 true,则为语法错误

14.4.2 静态语义:BoundNames

  1. GeneratorDeclaration : function * BindingIdentifier ( FormalParameters ) { GeneratorBody }
  1. 返回 BindingIdentifier 的 BoundNames
  1. GeneratorDeclaration : function * ( FormalParameters ) { GeneratorBody }
  1. 返回 « “*default*“ ».

注 “*default*“在本规范中用作使用导出声明定义的可提升匿名函数的合成名称。

14.4.3 静态语义:ComputedProperty包含

带有参数 symbol.

  1. GeneratorMethod : * PropertyName ( UniqueFormalParameters ) { GeneratorBody }
  1. 返回带有参数symbol的PropertyName的ComputedPropertyContains结果

14.4.4 静态语义:包含

带有参数 symbol.

  1. GeneratorDeclaration : function * BindingIdentifier ( FormalParameters ) { GeneratorBody }
  2. GeneratorDeclaration : function * ( FormalParameters ) { GeneratorBody }
  3. GeneratorExpression : function * BindingIdentifier ( FormalParameters ) { GeneratorBody }
  1. 返回 false.

注:依赖于子结构的静态语义规则通常不会考虑函数定义。

14.4.5 静态语义:HasDirectSuper

  1. GeneratorMethod : * PropertyName ( UniqueFormalParameters ) { GeneratorBody }
  1. 若 UniqueFormalParameters 包含 SuperCall 是 true, 返回 true.
  2. 返回 GeneratorBody 包含 SuperCall.

14.4.6 静态语义:HasName

  1. GeneratorExpression : function * ( FormalParameters ) { GeneratorBody }
  1. 返回 false.
  1. GeneratorExpression : function * BindingIdentifier ( FormalParameters ) { GeneratorBody }
  1. 返回 true

14.4.7 静态语义:IsConstantDeclaration

  1. GeneratorDeclaration : function * BindingIdentifier ( FormalParameters ) { GeneratorBody }
  2. GeneratorDeclaration : function * ( FormalParameters ) { GeneratorBody }
  1. 返回 false

14.4.8 静态语义:IsFunctionDefinition

  1. GeneratorExpression : function * BindingIdentifier ( FormalParameters ) { GeneratorBody }
  1. 返回 true.

14.4.9 静态语义:PropName

  1. GeneratorMethod : * PropertyName ( UniqueFormalParameters ) { GeneratorBody }
  1. 返回 PropertyName 的 PropName

14.4.10 运行时语义:EvaluateBody

带有参数 functionObject 和 argumentsList列表.

  1. GeneratorBody : FunctionBody
  1. 执行 ? FunctionDeclarationInstantiation(functionObject, argumentsList).
  2. 令 G 为 ? OrdinaryCreateFromConstructor(functionObject, “%GeneratorPrototype%”, « [[GeneratorState]], [[GeneratorContext]] »)
  3. 执行 GeneratorStart(G, FunctionBody).
  4. 返回 Completion { [[Type]]: return, [[Value]]: G, [[Target]]: empty }.

14.4.11 运行时语义:InstantiateFunctionObject

带有参数 scope.

  1. GeneratorDeclaration : function * BindingIdentifier ( FormalParameters ) { GeneratorBody }
  1. 若 GeneratorDeclaration 的函数代码是严格模式代码,则令strict为true,否则令strict为false
  2. 令 name 为 BindingIdentifier 的 StringValue
  3. 令 F 为 GeneratorFunctionCreate(Normal, FormalParameters, GeneratorBody, scope, strict).
  4. 令 prototype 为 ObjectCreate(%GeneratorPrototype%).
  5. 执行 DefinePropertyOrThrow(F, “prototype”, PropertyDescriptor { [[Value]]: prototype, [[Writable]]: true, [[Enumerable]]: false, [[Configurable]]: false }).
  6. 执行 SetFunctionName(F, name).
  7. 设置 F.[[SourceText]] 为GeneratorDeclaration匹配的源文本.
  8. 返回 F
  1. GeneratorDeclaration : function * ( FormalParameters ) { GeneratorBody }
  1. 令 F 为 GeneratorFunctionCreate(Normal, FormalParameters, GeneratorBody, scope, true).
  2. 令 prototype 为 ObjectCreate(%GeneratorPrototype%).
  3. 执行 DefinePropertyOrThrow(F, “prototype”, PropertyDescriptor { [[Value]]: prototype, [[Writable]]: true, [[Enumerable]]: false, [[Configurable]]: false }).
  4. 执行 SetFunctionName(F, “default”).
  5. 设置 F.[[SourceText]] 为GeneratorDeclaration匹配的源文本.
  6. 返回 F.

注:匿名GeneratorDeclaration仅可作为export default声明的一部分出现,因此其函数代码始终为严格模式代码。

14.4.12 运行时语义:PropertyDefinitionEvaluation

带有参数 object 和 enumerable.

  1. GeneratorMethod : * PropertyName ( UniqueFormalParameters ) { GeneratorBody }
  1. 令 propKey 为运算 PropertyName 的结果
  2. ReturnIfAbrupt(propKey).
  3. 若此GeneratorMethod的函数代码是严格模式代码, 令 strict 为 true. 否则,令 strict 为 false.
  4. 令 scope 为 运行时上下文的 LexicalEnvironment.
  5. 令 closure 为 GeneratorFunctionCreate(Method, UniqueFormalParameters, GeneratorBody, scope, strict).
  6. 执行 MakeMethod(closure, object).
  7. 令 prototype 为 ObjectCreate(%GeneratorPrototype%).
  8. 执行 DefinePropertyOrThrow(closure, “prototype”, PropertyDescriptor { [[Value]]: prototype, [[Writable]]: true, [[Enumerable]]: false, [[Configurable]]: false }).
  9. 执行 SetFunctionName(closure, propKey).
  10. 设置 closure.[[SourceText]] 为GeneratorMethod匹配的源文本.
  11. 令 desc 为 the PropertyDescriptor { [[Value]]: closure, [[Writable]]: true, [[Enumerable]]: enumerable, [[Configurable]]: true }.
  12. 返回 ? DefinePropertyOrThrow(object, propKey, desc).

14.4.13 运行时语义:NamedEvaluation

带有参数 name.

  1. GeneratorExpression : function * ( FormalParameters ) { GeneratorBody }
  1. 令 closure 为运算此GeneratorExpression的结果。
  2. 执行 SetFunctionName(closure, name).
  3. 返回 closure.

14.4.14 运行时语义:Evaluation

  1. GeneratorExpression : function * ( FormalParameters ) { GeneratorBody }
  1. 若此GeneratorExpression的函数代码是严格模式代码, 令 strict 为 true. 否则,令 strict 为 false.
  2. 令 scope 为运行时上下文的LexicalEnvironment.
  3. 令 closure 为 GeneratorFunctionCreate(Normal, FormalParameters, GeneratorBody, scope, strict).
  4. 令 prototype 为 ObjectCreate(%GeneratorPrototype%).
  5. 执行 DefinePropertyOrThrow(closure, “prototype”, PropertyDescriptor { [[Value]]: prototype, [[Writable]]: true, [[Enumerable]]: false, [[Configurable]]: false }).
  6. 设置 closure.[[SourceText]] 为GeneratorExpression匹配的源文本.
  7. 返回 closure
  1. GeneratorExpression : function * BindingIdentifier ( FormalParameters ) { GeneratorBody }
  1. 若此GeneratorExpression的函数代码是严格模式代码, 令 strict 为 true. 否则,令 strict 为 false.
  2. 令 scope 为 运行时上下文的 LexicalEnvironment.
  3. 令 funcEnv 为 NewDeclarativeEnvironment(scope).
  4. 令 envRec 为 funcEnv 的 EnvironmentRecord.
  5. 令 name 为 BindingIdentifier 的 StringValue
  6. 执行 envRec.CreateImmutableBinding(name, false).
  7. 令 closure 为 GeneratorFunctionCreate(Normal, FormalParameters, GeneratorBody, funcEnv, strict).
  8. 令 prototype 为 ObjectCreate(%GeneratorPrototype%).
  9. 执行 DefinePropertyOrThrow(closure, “prototype”, PropertyDescriptor { [[Value]]: prototype, [[Writable]]: true, [[Enumerable]]: false, [[Configurable]]: false }).
  10. 执行 SetFunctionName(closure, name).
  11. 执行 envRec.InitializeBinding(name, closure).
  12. 设置 closure.[[SourceText]] 为GeneratorExpression匹配的源文本.
  13. 返回 closure.

注:可以从GeneratorExpression的FunctionBody内部引用GeneratorExpression中的BindingIdentifier,以允许生成器代码递归调用自身。但是,与GeneratorDeclaration中的不同,GeneratorExpression中的BindingIdentifier不能从GeneratorExpression中引用,也不影响封装GeneratorExpression的范围。

  1. YieldExpression : yield
  1. 令 generatorKind 为 ! GetGeneratorKind().
  2. 若 generatorKind 是 async,那么 返回 ? AsyncGeneratorYield(undefined).
  3. 否则, 返回 ? GeneratorYield(CreateIterResultObject(undefined, false)).
  1. YieldExpression : yield AssignmentExpression
  1. 令 generatorKind 为 ! GetGeneratorKind().
  2. 令 exprRef 为运算 AssignmentExpression 的结果
  3. 令 value 为 ? GetValue(exprRef).
  4. 若 generatorKind 是 async,那么 返回 ? AsyncGeneratorYield(value).
  5. 否则, 返回 ? GeneratorYield(CreateIterResultObject(value, false)).
  1. YieldExpression : yield * AssignmentExpression
  1. 令 generatorKind 为 ! GetGeneratorKind().
  2. 令 exprRef 为运算 AssignmentExpression 的结果
  3. 令 value 为 ? GetValue(exprRef).
  4. 令 iteratorRecord 为 ? GetIterator(value, generatorKind).
  5. 令 iterator 为 iteratorRecord.[[Iterator]].
  6. 令 received 为 NormalCompletion(undefined).
  7. 重复,

    1. 若 received.[[Type]] 是 normal,那么

      1. 令 innerResult 为 ? Call(iteratorRecord.[[NextMethod]], iteratorRecord.[[Iterator]], « received.[[Value]] »).

      2. 若 generatorKind 是 async,那么 设置 innerResult 为 ? Await(innerResult).

      3. 若 Type(innerResult) 不是 Object, 抛出 TypeError 异常.
      4. 令 done 为 ? IteratorComplete(innerResult).
      5. 若 done 是 true,那么
        1. 返回 ? IteratorValue(innerResult).
        2. 若 generatorKind 是 async,那么 设置 received 为 AsyncGeneratorYield(? IteratorValue(innerResult)).
        3. 否则, 设置 received 为 GeneratorYield(innerResult).
    2. 否则 若 received.[[Type]] 是 throw,那么
      1. 令 throw 为 ? GetMethod(iterator, “throw”).
      2. 若 throw 不是 undefined,那么
        1. 令 innerResult 为 ? Call(throw, iterator, « received.[[Value]] »).
        2. 若 generatorKind 是 async,那么 设置 innerResult 为 ? Await(innerResult).
        3. 注: 内部迭代器throw方法的异常会传播。内部throw方法的正常完成与内部next类似。
        4. 若 Type(innerResult) 不是 Object, 抛出 TypeError 异常.
        5. 令 done 为 ? IteratorComplete(innerResult).
        6. 若 done 是 true,那么
          1. 返回 ? IteratorValue(innerResult).
        7. 若 generatorKind 是 async,那么 设置 received 为 AsyncGeneratorYield(? IteratorValue(innerResult)).
        8. 否则, 设置 received 为 GeneratorYield(innerResult).
    3. 否则,
      1. 注: 若iterator没有throw方法,则此throw将终止yield *循环。但是首先我们需要给迭代器一个清理的机会。
      2. 令 closeCompletion 为 Completion { [[Type]]: normal, [[Value]]: empty, [[Target]]: empty }.
      3. 若 generatorKind 是 async, 执行 ? AsyncIteratorClose(iteratorRecord, closeCompletion).
      4. 否则, 执行 ? IteratorClose(iteratorRecord, closeCompletion).
      5. 注: 下一步将引发TypeError,以表明发生了yield *协议违规:iterator没有throw方法。
      6. 抛出 TypeError 异常.
  8. 否则,
    1. 断言: received.[[Type]] 是 return.
    2. 令 返回 为 ? GetMethod(iterator, “return”).
    3. 若 返回 是 undefined,那么
      1. 若 generatorKind 是 async,那么 设置 received.[[Value]] 为 ? Await(received.[[Value]]).
      2. 返回 Completion(received).
    4. 令 innerReturnResult 为 ? Call(return, iterator, « received.[[Value]] »).
    5. 若 generatorKind 是 async,那么 设置 innerReturnResult 为 ? Await(innerReturnResult).
    6. 若 Type(innerReturnResult) 不是 Object, 抛出 TypeError 异常.
    7. 令 done 为 ? IteratorComplete(innerReturnResult).
    8. 若 done 是 true,那么
      1. 令 value 为 ? IteratorValue(innerReturnResult).
      2. 返回 Completion { [[Type]]: return, [[Value]]: value, [[Target]]: empty }.
    9. 若 generatorKind 是 async,那么 设置 received 为 AsyncGeneratorYield(? IteratorValue(innerReturnResult)).
    10. 否则, 设置 received 为 GeneratorYield(innerReturnResult).

14.5 异步 Generator 函数定义

语法

  1. AsyncGeneratorMethod[Yield, Await] :
  2. async [no LineTerminator here] * PropertyName[?Yield, ?Await] (UniqueFormalParameters[+Yield, +Await] ) { AsyncGeneratorBody }
  3. AsyncGeneratorDeclaration[Yield, Await, Default] :
  4. async [no LineTerminator here] function * BindingIdentifier[?Yield, ?Await] ( FormalParameters[+Yield, +Await] ) { AsyncGeneratorBody }
  5. [+Default] async [no LineTerminator here] function * ( FormalParameters[+Yield, +Await] ) { AsyncGeneratorBody }
  6. AsyncGeneratorExpression :
  7. async [no LineTerminator here] function * BindingIdentifier[+Yield, +Await] opt ( FormalParameters[+Yield, +Await] ) { AsyncGeneratorBody }
  8. AsyncGeneratorBody :
  9. FunctionBody[+Yield, +Await]

注1:YieldExpression和AwaitExpression不能在异步生成器函数的FormalParameters中使用,因为作为FormalParameters一部分的任何表达式都在生成的异步生成器对象处于可恢复状态之前进行求值

注 2:与异步生成器对象有关的抽象操作在25.5.3中定义。

14.5.1 静态语义:Early Errors

  1. AsyncGeneratorMethod : async * PropertyName ( UniqueFormalParameters ) { AsyncGeneratorBody }
  • 若 AsyncGeneratorMethod 的 HasDirectSuper 是 true,则为语法错误
  • 若 UniqueFormalParameters 包含 YieldExpression 是 true,则为语法错误
  • 若 UniqueFormalParameters 包含 AwaitExpression 是 true,则为语法错误
  • 若 AsyncGeneratorBody 的 ContainsUseStrict 是 true 并且 UniqueFormalParameters 的 IsSimpleParameterList 是 false,则为语法错误
  • 若UniqueFormalParameters的BoundNames的任何元素也都出现在AsyncGeneratorBody的LexicallyDeclaredNames中,则为语法错误
  1. AsyncGeneratorDeclaration : async function * BindingIdentifier ( FormalParameters ) { AsyncGeneratorBody }
  2. AsyncGeneratorDeclaration : async function * ( FormalParameters ) { AsyncGeneratorBody }
  3. AsyncGeneratorExpression : async function * BindingIdentifier ( FormalParameters ) { AsyncGeneratorBody }
  • 若与此产生式匹配的源代码是严格模式代码,则将应用UniqueFormalParameters:FormalParameters的早期错误规则。
  • 若与此产生式匹配的源代码是严格模式代码,并且若BindingIdentifier是IdentifierName eval或IdentifierName arguments,则它是语法错误。
  • 若AsyncGeneratorBody的ContainsUseStrict为true并且FormalParameters的IsSimpleParameterList为false,则为语法错误。
  • 若 FormalParameters 的 BoundNames 的任何元素也出现在 AsyncGeneratorBody 的 LexicallyDeclaredNames 中,则是语法错误。
  • 若 FormalParameters 包含YieldExpression 是 true,则为语法错误
  • 若 FormalParameters 包含AwaitExpression 是 true,则为语法错误
  • 若 FormalParameters 包含SuperProperty 是 true,则为语法错误
  • 若 AsyncGeneratorBody 包含SuperProperty 是 true,则为语法错误
  • 若 FormalParameters 包含SuperCall 是 true,则为语法错误
  • 若 AsyncGeneratorBody 包含SuperCall 是 true,则为语法错误

14.5.2 静态语义:BoundNames

  1. AsyncGeneratorDeclaration : async function * BindingIdentifier ( FormalParameters ) {
  2. AsyncGeneratorBody }
  1. 返回 the BindingIdentifier 的 BoundNames
  1. AsyncGeneratorDeclaration : async function * ( FormalParameters ) { AsyncGeneratorBody }
  1. 返回 « “*default*“ »

注 “*default*“在本规范中用作使用导出声明定义的可提升匿名函数的合成名称。

14.5.3 静态语义:ComputedProperty包含

带有参数 symbol.

  1. AsyncGeneratorMethod : async * PropertyName ( UniqueFormalParameters ) { AsyncGeneratorBody }
  1. 返回带有参数symbol的PropertyName的ComputedPropertyContains结果。

14.5.4 静态语义:包含

带有参数 symbol.

  1. AsyncGeneratorDeclaration : async function * BindingIdentifier ( FormalParameters ) {
  2. AsyncGeneratorBody }
  3. AsyncGeneratorDeclaration : async function * ( FormalParameters ) { AsyncGeneratorBody }
  4. AsyncGeneratorExpression : async function * BindingIdentifier ( FormalParameters ) {
  5. AsyncGeneratorBody }
  1. 返回 false.

注:依赖于子结构的静态语义规则通常不会考虑函数定义。

14.5.5 静态语义:HasDirectSuper

  1. AsyncGeneratorMethod : async * PropertyName ( UniqueFormalParameters ) { AsyncGeneratorBody }
  1. 若 UniqueFormalParameters 包含SuperCall 是 true, 返回 true.
  2. 返回 AsyncGeneratorBody 包含SuperCall.

14.5.6 静态语义:HasName

  1. AsyncGeneratorExpression : async function * ( FormalParameters ) { AsyncGeneratorBody }
  1. 返回 false.
  1. AsyncGeneratorExpression : async function * BindingIdentifier ( FormalParameters ) {
  2. AsyncGeneratorBody }
  1. 返回 true.

14.5.7 静态语义:IsConstantDeclaration

  1. AsyncGeneratorDeclaration : async function * BindingIdentifier ( FormalParameters ) {
  2. AsyncGeneratorBody }
  3. AsyncGeneratorDeclaration : async function * ( FormalParameters ) { AsyncGeneratorBody }
  1. 返回 false.

14.5.8 静态语义:IsFunctionDefinition

  1. AsyncGeneratorExpression : async function * BindingIdentifier ( FormalParameters ) {
  2. AsyncGeneratorBody }
  1. 返回 true.

14.5.9 静态语义:PropName

  1. AsyncGeneratorMethod : async * PropertyName ( UniqueFormalParameters ) { AsyncGeneratorBody }
  1. 返回 PropertyName 的 PropName

14.5.10 运行时语义:EvaluateBody

带有参数 functionObject 和 argumentsList列表.

  1. AsyncGeneratorBody : FunctionBody
  1. 执行 ? FunctionDeclarationInstantiation(functionObject, argumentsList).
  2. 令 generator 为 ? OrdinaryCreateFromConstructor(functionObject, “%AsyncGeneratorPrototype%”, « [[AsyncGeneratorState]], [[AsyncGeneratorContext]], [[AsyncGeneratorQueue]] »).
  3. 执行 ! AsyncGeneratorStart(generator, FunctionBody).
  4. 返回 Completion { [[Type]]: return, [[Value]]: generator, [[Target]]: empty }.

14.5.11 运行时语义:InstantiateFunctionObject

带有参数 scope.

  1. AsyncGeneratorDeclaration : async function * BindingIdentifier ( FormalParameters ) {
  2. AsyncGeneratorBody }
  1. 若AsyncGeneratorDeclaration的函数代码是严格模式代码, 令 strict 为 true. 否则,令 strict 为 false.
  2. 令 name 为 BindingIdentifier 的 StringValue
  3. 令 F 为 ! AsyncGeneratorFunctionCreate(Normal, FormalParameters, AsyncGeneratorBody, scope, strict).
  4. 令 prototype 为 ! ObjectCreate(%AsyncGeneratorPrototype%).
  5. 执行 ! DefinePropertyOrThrow(F, “prototype”, PropertyDescriptor { [[Value]]: prototype, [[Writable]]: true, [[Enumerable]]: false, [[Configurable]]: false }).
  6. 执行 ! SetFunctionName(F, name).
  7. 设置 F.[[SourceText]] 为AsyncGeneratorDeclaration匹配的源文本.
  8. 返回 F.
  1. AsyncGeneratorDeclaration : async function * ( FormalParameters ) { AsyncGeneratorBody }
  1. 若AsyncGeneratorDeclaration的函数代码是严格模式代码, 令 strict 为 true. 否则,令 strict 为 false.
  2. 令 F 为 AsyncGeneratorFunctionCreate(Normal, FormalParameters, AsyncGeneratorBody, scope, strict).
  3. 令 prototype 为 ObjectCreate(%AsyncGeneratorPrototype%).
  4. 执行 DefinePropertyOrThrow(F, “prototype”, PropertyDescriptor { [[Value]]: prototype, [[Writable]]: true, [[Enumerable]]: false, [[Configurable]]: false }).
  5. 执行 SetFunctionName(F, “default”).
  6. 设置 F.[[SourceText]] 为AsyncGeneratorDeclaration匹配的源文本.
  7. 返回 F

注:匿名AsyncGeneratorDeclaration只能作为 export default 声明的一部分出现。

14.5.12 运行时语义:PropertyDefinitionEvaluation

带有参数 object 和 enumerable.

  1. AsyncGeneratorMethod : async * PropertyName ( UniqueFormalParameters ) { AsyncGeneratorBody }
  1. 令 propKey 为运算 PropertyName 的结果
  2. ReturnIfAbrupt(propKey).
  3. 若此AsyncGeneratorMethod的函数代码是严格模式代码, 令 strict 为 true. 否则,令 strict 为 false.
  4. 令 scope 为 运行时上下文的 LexicalEnvironment.
  5. 令 closure 为 ! AsyncGeneratorFunctionCreate(Method, UniqueFormalParameters, AsyncGeneratorBody, scope, strict).
  6. 执行 ! MakeMethod(closure, object).
  7. 令 prototype 为 ! ObjectCreate(%AsyncGeneratorPrototype%).
  8. 执行 ! DefinePropertyOrThrow(closure, “prototype”, PropertyDescriptor { [[Value]]: prototype, [[Writable]]: true, [[Enumerable]]: false, [[Configurable]]: false }).
  9. 执行 ! SetFunctionName(closure, propKey).
  10. 设置 closure.[[SourceText]] 为AsyncGeneratorMethod匹配的源文本.
  11. 令 desc 为 PropertyDescriptor { [[Value]]: closure, [[Writable]]: true, [[Enumerable]]: enumerable, [[Configurable]]: true }.
  12. 返回 ? DefinePropertyOrThrow(object, propKey, desc).

14.5.13 运行时语义:NamedEvaluation

带有参数 name.

  1. AsyncGeneratorExpression : async function * ( FormalParameters ) { AsyncGeneratorBody }
  1. 令 closure 为运算此AsyncGeneratorExpression的结果。
  2. 执行 SetFunctionName(closure, name).
  3. 返回 closure.

14.5.14 运行时语义:Evaluation

  1. AsyncGeneratorExpression : async function * ( FormalParameters ) { AsyncGeneratorBody }
  1. 若此AsyncGeneratorExpression的函数代码是严格模式代码, 令 strict 为 true. 否则,令 strict 为 false.
  2. 令 scope 为运行时上下文的LexicalEnvironment.
  3. 令 closure 为 ! AsyncGeneratorFunctionCreate(Normal, FormalParameters, AsyncGeneratorBody, scope, strict).
  4. 令 prototype 为 ! ObjectCreate(%AsyncGeneratorPrototype%).
  5. 执行 ! DefinePropertyOrThrow(closure, “prototype”, PropertyDescriptor { [[Value]]: prototype, [[Writable]]: true, [[Enumerable]]: false, [[Configurable]]: false }).
  6. 设置 closure.[[SourceText]] 为AsyncGeneratorExpression匹配的源文本.
  7. 返回 closure.
  1. AsyncGeneratorExpression : async function * BindingIdentifier ( FormalParameters ) {
  2. AsyncGeneratorBody }
  1. 若此AsyncGeneratorExpression的函数代码是严格模式代码, 令 strict 为 true. 否则,令 strict 为 false.
  2. 令 scope 为 运行时上下文的 LexicalEnvironment.
  3. 令 funcEnv 为 ! NewDeclarativeEnvironment(scope).
  4. 令 envRec 为 funcEnv 的 EnvironmentRecord.
  5. 令 name 为 BindingIdentifier 的 StringValue
  6. 执行 ! envRec.CreateImmutableBinding(name).
  7. 令 closure 为 ! AsyncGeneratorFunctionCreate(Normal, FormalParameters, AsyncGeneratorBody, funcEnv, strict).
  8. 令 prototype 为 ! ObjectCreate(%AsyncGeneratorPrototype%).
  9. 执行 ! DefinePropertyOrThrow(closure, “prototype”, PropertyDescriptor { [[Value]]: prototype, [[Writable]]: true, [[Enumerable]]: false, [[Configurable]]: false }).
  10. 执行 ! SetFunctionName(closure, name).
  11. 执行 ! envRec.InitializeBinding(name, closure).
  12. 设置 closure.[[SourceText]] 为AsyncGeneratorExpression匹配的源文本.
  13. 返回 closure.

注:可以从AsyncGeneratorExpression的AsyncGeneratorBody内部引用AsyncGeneratorExpression中的BindingIdentifier,以允许生成器代码递归调用自身。但是,与AsyncGeneratorDeclaration中的方法不同,AsyncGeneratorExpression中的BindingIdentifier不能从引用,也不会影响包围AsyncGeneratorExpression的作用域。

14.6 Class 定义

语法

  1. ClassDeclaration[Yield, Await, Default] :
  2. class BindingIdentifier[?Yield, ?Await] ClassTail[?Yield, ?Await]
  3. [+Default] class ClassTail[?Yield, ?Await]
  4. ClassExpression[Yield, Await] :
  5. class BindingIdentifier[?Yield, ?Await] opt ClassTail[?Yield, ?Await]
  6. ClassTail[Yield, Await] :
  7. ClassHeritage[?Yield, ?Await] opt { ClassBody[?Yield, ?Await] opt }
  8. ClassHeritage[Yield, Await] :
  9. extends LeftHandSideExpression[?Yield, ?Await]
  10. ClassBody[Yield, Await] :
  11. ClassElementList[?Yield, ?Await]
  12. ClassElementList[Yield, Await] :
  13. ClassElement[?Yield, ?Await]
  14. ClassElementList[?Yield, ?Await] ClassElement[?Yield, ?Await]
  15. ClassElement[Yield, Await] :
  16. MethodDefinition[?Yield, ?Await]
  17. static MethodDefinition[?Yield, ?Await]
  18. ;

注:class定义始终是严格模式代码。

14.6.1 静态语义:Early Errors

  1. ClassTail : ClassHeritage { ClassBody }
  • 若 ClassHeritage 不存在,并且下面的算法运算为true,则为语法错误:
    1. 令 constructor 为 ClassBody 的 ConstructorMethod
    2. 若 constructor 是 empty, 返回 false.
    3. 返回 constructor 的 HasDirectSuper
  1. ClassBody : ClassElementList
  • 若ClassElementList的PrototypePropertyNameList包含多个“constructor”,则为语法错误。
  1. ClassElement : MethodDefinition
  • 若Method Definition的proName不是“constructor”,且Method Definition的HasDirectSuper为真,则为语法错误。
  • 若MethodDefinition的proName是“constructor”,而SpecialMethod Definition是正确的,则是语法错误。
  1. ClassElement : static MethodDefinition
  • 若 MethodDefinition 的 HasDirectSuper 是 true,则为语法错误
  • 若 MethodDefinition 的 PropName 是 “prototype”,则为语法错误

14.6.2 静态语义:BoundNames

  1. ClassDeclaration : class BindingIdentifier ClassTail
  1. 返回 BindingIdentifier 的 BoundNames
  1. ClassDeclaration : class ClassTail
  1. 返回 « “*default*“ ».

14.6.3 静态语义:ConstructorMethod

  1. ClassElementList : ClassElement
  1. 若 ClassElement 是 ClassElement : ; , 返回 empty.
  2. 若 ClassElement 的 IsStatic 是 true, 返回 empty.
  3. 若 ClassElement 的 PropName 不是”constructor”, 返回 empty.
  4. 返回 ClassElement.
  1. ClassElementList : ClassElementList ClassElement
  1. 令 head 为 ClassElementList 的 ConstructorMethod
  2. 若 head 不是 empty, 返回 head.
  3. 若 ClassElement 是 ClassElement : ; , 返回 empty.
  4. 若 ClassElement 的 IsStatic 是 true, 返回 empty.
  5. 若 ClassElement 的 PropName 不是”constructor”, 返回 empty.
  6. 返回 ClassElement

注:早期错误规则确保只有一个名为“constructor”的方法定义,并且它不是访问器属性或生成器定义。

14.6.4 静态语义:包含

带有参数 symbol.

  1. ClassTail : ClassHeritage { ClassBody }
  1. 若 symbol 是 ClassBody, 返回 true.
  2. 若 symbol 是 ClassHeritage,那么
    1. 若 ClassHeritage 存在, 返回 true; 否则 返回 false.
  3. 令 inHeritage 为 ClassHeritage 包含symbol.
  4. 若 inHeritage 是 true, 返回 true.
  5. 返回对于具有参数symbol的ClassBody,计算属性的结果。

注:依赖于子结构的静态语义规则通常不会查看类主体,除了PropertyNames。

14.6.5 静态语义:ComputedProperty包含

带有参数 symbol.

  1. ClassElementList : ClassElementList ClassElement
  1. 令 inList 为带参数symbol的ClassElementList的ComputedPropertyContains结果.
  2. 若 inList 是 true, 返回 true.
  3. 返回带参数symbol的ClassElement的ComputedPropertyContains结果.
  1. ClassElement : MethodDefinition
  1. 返回带参数symbol的MethodDefinition的ComputedPropertyContains结果
  1. ClassElement : static MethodDefinition
  1. 返回带参数symbol的MethodDefinition的ComputedPropertyContains结果.
  1. ClassElement : ;
  1. 返回 false.

14.6.6 静态语义:HasName

  1. ClassExpression : class ClassTail
  1. 返回 false.
  1. ClassExpression : class BindingIdentifier ClassTail
  1. 返回 true.

14.6.7 静态语义:IsConstantDeclaration

  1. ClassDeclaration : class BindingIdentifier ClassTail
  2. ClassDeclaration : class ClassTail
  1. 返回 false.

14.6.8 静态语义:IsFunctionDefinition

  1. ClassExpression : class BindingIdentifier ClassTail
  1. 返回 true.

14.6.9 静态语义:IsStatic

  1. ClassElement : MethodDefinition
  1. 返回 false.
  1. ClassElement : static MethodDefinition
  1. 返回 true.
  1. ClassElement : ;
  1. 返回 false.

14.6.10 静态语义:NonConstructorMethodDefinitions

  1. ClassElementList : ClassElement
  1. 若 ClassElement 是 ClassElement : ; , 返回一个新的空列表.
  2. 若 ClassElement 的 IsStatic 是 false,并且 ClassElement 的 PropName 是 “constructor”, 返回一个新的空列表.
  3. 返回 a List containing ClassElement.
  1. ClassElementList : ClassElementList ClassElement
  1. 令 list 为 ClassElementList 的 NonConstructorMethodDefinitions
  2. 若 ClassElement 是 ClassElement : ; , 返回 list.
  3. 若 ClassElement 的 IsStatic 是 false,并且 ClassElement 的 PropName 是 “constructor”, 返回 list.
  4. 将ClassElement追加到列表的末尾。
  5. 返回 list.

14.6.11 静态语义:PrototypePropertyNameList

  1. ClassElementList : ClassElement
  1. 若 ClassElement 的 PropName 是 empty, 返回一个新的空列表.
  2. 若 ClassElement 的 IsStatic 是 true, 返回一个新的空列表.
  3. 返回一个包含ClassElement的PropName的列表。
  1. ClassElementList : ClassElementList ClassElement
  1. 令 list 为 ClassElementList 的 PrototypePropertyNameList
  2. 若 ClassElement 的 PropName 是 empty, 返回 list.
  3. 若 ClassElement 的 IsStatic 是 true, 返回 list.
  4. 将ClassElement的PropName追加到列表的末尾
  5. 返回 list.

14.6.12 静态语义:PropName

  1. ClassElement : ;
  1. 返回 empty.

14.6.13 运行时语义:ClassDefinitionEvaluation

带有参数 classBinding 和 className

  1. ClassTail : ClassHeritage { ClassBody }
  1. 令 lex 为运行时上下文的LexicalEnvironment.

  2. 令 classScope 为 NewDeclarativeEnvironment(lex).

  3. 令 classScopeEnvRec 为 classScope 的 EnvironmentRecord.

  4. 若 classBinding 不是 undefined,那么

  5. 执行 classScopeEnvRec.CreateImmutableBinding(classBinding, true).

  6. 若 ClassHeritageopt 不存在,那么

    1. 令 protoParent 为内部对象 %ObjectPrototype%.
    2. 令 constructorParent 为内部对象 %FunctionPrototype%.
  7. 否则,

    1. 设置 运行时上下文的 LexicalEnvironment 为 classScope.
    2. 令 superclassRef 为运算 ClassHeritage 的结果
    3. 设置 运行时上下文的 LexicalEnvironment 为 lex.
    4. 令 superclass 为 ? GetValue(superclassRef).
    5. 若 superclass 是 null,那么
      1. 令 protoParent 为 null.
      2. 令 constructorParent 为内部对象 %FunctionPrototype%.
    6. 否则 若 IsConstructor(superclass) 是 false, 抛出 TypeError 异常.
    7. 否则,
      1. 令 protoParent 为 ? Get(superclass, “prototype”).
      2. 若 Type(protoParent) 不是 Object 或 Null, 抛出 TypeError 异常.
      3. 令 constructorParent 为 superclass.
  8. 令 proto 为 ObjectCreate(protoParent).

  9. 若 ClassBodyopt 不存在, 令 constructor 为 empty.

  10. 否则, 令 constructor 为 ClassBody 的 ConstructorMethod

  11. 若 constructor 是 empty,那么

    1. 若 ClassHeritageopt 存在,那么

      1. 设置 constructor 为解析源文本的结果

        1. constructor(... args){ super (...args);}
        2. using the syntactic grammar with the goal symbol MethodDefinition[~Yield, ~Await] .
  12. 否则,

    1. 设置 constructor 为解析源文本的结果

      1. constructor(... args){ super (...args);}
      2. using the syntactic grammar with the goal symbol MethodDefinition[~Yield, ~Await] .
  13. 设置 运行时上下文的 LexicalEnvironment 为 classScope.

  14. 令 constructorInfo 为使用参数proto和ConstructorParent作为可选functionPrototype参数为构造函数执行DefineMethod的结果。

  15. 断言: builderInfo不是 abrupt completion.

  16. 令 F 为 constructorInfo.[[Closure]].

  17. 若 ClassHeritageopt 存在, 设置 F.[[ConstructorKind]] 为 “derived”.

  18. 执行 MakeConstructor(F, false, proto).

  19. 执行 MakeClassConstructor(F).

  20. 若 className 不是 undefined,那么

    1. 执行 SetFunctionName(F, className).
  21. 执行 CreateMethodProperty(proto, “constructor”, F).

  22. 若 ClassBodyopt 不存在, 令 methods 为一个新的空列表.

  23. 否则, 令 methods 为 ClassBody 的 NonConstructorMethodDefinitions

  24. 对于methods的每一个按顺序的 ClassElement m, 执行

    1. 若 m 的 IsStatic 是 false,那么
      1. 令 status 为使用参数proto和false对m执行PropertyDefinitionEvaluation的结果。
    2. 否则,
      1. 令 status 为使用参数F和false对m执行PropertyDefinitionEvaluation的结果。
    3. 若 status 是 abrupt completion,那么
      1. 设置 运行时上下文的 LexicalEnvironment 为 lex.
      2. 返回 Completion(status).
  25. 设置 运行时上下文的 LexicalEnvironment 为 lex.

  26. 若 classBinding 不是 undefined,那么

    1. 执行 classScopeEnvRec.InitializeBinding(classBinding, F).
  27. 返回 F.

14.6.14 运行时语义:BindingClassDeclarationEvaluation

  1. ClassDeclaration : class BindingIdentifier ClassTail
  1. 令 className 为 BindingIdentifier 的 StringValue
  2. 令 value 为带参数className和className的ClassTail的ClassDefinitionEvaluation结果
  3. ReturnIfAbrupt(value).
  4. 设置 value.[[SourceText]] 为ClassDeclaration匹配的源文本.
  5. 令 env 为 运行时上下文的 LexicalEnvironment.
  6. 执行 ? InitializeBoundName(className, value, env).
  7. 返回 value.
  1. ClassDeclaration : class ClassTail
  1. 令 value 为参数undefined和“default”的ClassTail的ClassDefinitionEvaluation结果。
  2. ReturnIfAbrupt(value).
  3. 设置 value.[[SourceText]] 为ClassDeclaration匹配的源文本.
  4. 返回 value.

注:ClassDeclaration:ClassTail类仅作为ExportDeclaration的一部分出现,并且建立其绑定是 作为该产生式运算动作的一部分进行处理。见15.2.3.11。

14.6.15 运行时语义:NamedEvaluation

带有参数 name.

  1. ClassExpression : class ClassTail
  1. 返回参数undefined和名称的ClassTail的ClassDefinitionEvaluation结果

14.6.16 运行时语义:Evaluation

  1. ClassDeclaration : class BindingIdentifier ClassTail
  1. 执行 ? 此 ClassDeclaration 的 BindingClassDeclarationEvaluation.
  2. 返回 NormalCompletion(empty).

注:ClassDeclaration:ClassTail类仅作为ExportDeclaration的一部分出现,并且从不直接运算

  1. ClassExpression : class BindingIdentifier ClassTail
  1. 若 BindingIdentifieropt 不存在, 令 className 为 undefined.
  2. 否则, 令 className 为 BindingIdentifier 的 StringValue
  3. 令 value 为参数为className和className的ClassTail的ClassDefinitionEvaluation结果。
  4. ReturnIfAbrupt(value).

  5. 设置 value.[[SourceText]] 为ClassExpression匹配的源文本.

  6. 返回 value.

14.7 Async 函数定义

语法

  1. AsyncFunctionDeclaration[Yield, Await, Default] :
  2. async [no LineTerminator here] function BindingIdentifier[?Yield, ?Await] (
  3. FormalParameters[~Yield, +Await] ) { AsyncFunctionBody }
  4. [+Default] async [no LineTerminator here] function ( FormalParameters[~Yield, +Await] ) { AsyncFunctionBody }
  5. AsyncFunctionExpression :
  6. async [no LineTerminator here] function ( FormalParameters[~Yield, +Await] ) { AsyncFunctionBody }
  7. async [no LineTerminator here] function BindingIdentifier[~Yield, +Await] ( FormalParameters[~Yield, +Await] ) { AsyncFunctionBody }
  8. AsyncMethod[Yield, Await] :
  9. async [no LineTerminator here] PropertyName[?Yield, ?Await] (
  10. UniqueFormalParameters[~Yield, +Await] ) { AsyncFunctionBody }
  11. AsyncFunctionBody :
  12. FunctionBody[~Yield, +Await]
  13. AwaitExpression[Yield] :
  14. await UnaryExpression[?Yield, +Await]

注 1:若存在[Await]参数,则将await解析为AwaitExpression。 [Await]参数出现在以下上下文中:

在AsyncFunctionBody中。 在AsyncFunctionDeclaration,AsyncFunctionExpression,AsyncGeneratorDeclaration或AsyncGeneratorExpression的FormalParameters中。 AwaitExpression在此位置是通过静态语义的语法错误。

当Module是语法目标符号并且[Await]参数不存在时,await将被解析为关键字,并且将是语法错误。当Script是语法目标符号时,若[Await]参数不存在,则等待可能被解析为标识符。这包括以下上下文:

AsyncFunctionBody或AsyncFunctionDeclaration,AsyncFunctionExpression,AsyncGeneratorDeclaration或AsyncGeneratorExpression的FormalParameters之外的任何位置。在FunctionExpression,GeneratorExpression或AsyncGeneratorExpression的BindingIdentifier中

注 2:与YieldExpression不同,省略AwaitExpression的操作数是语法错误。您必须等待。

14.7.1 静态语义:Early Errors

  1. AsyncMethod : async PropertyName ( UniqueFormalParameters ) { AsyncFunctionBody }
  • 若AsyncFunctionBody的ContainsUseStrict为true,并且UniqueFormalParameters的IsSimpleParameterList为false,则为语法错误。
  • 若 AsyncMethod 的 HasDirectSuper 是 true,则为语法错误
  • 若 UniqueFormalParameters 包含AwaitExpression 是 true,则为语法错误
  • 若UniqueFormalParameters的BoundNames的任何元素也出现在AsyncFunctionBody的LexicallyDeclaredNames中,则是语法错误。
  1. AsyncFunctionDeclaration : async function BindingIdentifier ( FormalParameters ) { AsyncFunctionBody }
  2. AsyncFunctionDeclaration : async function ( FormalParameters ) { AsyncFunctionBody }
  3. AsyncFunctionExpression : async function ( FormalParameters ) { AsyncFunctionBody }
  4. AsyncFunctionExpression : async function BindingIdentifier ( FormalParameters ) { AsyncFunctionBody }
  • 若AsyncFunctionBody的ContainsUseStrict为true,并且FormalParameters的IsSimpleParameterList为false,则为语法错误。
  • 若 FormalParameters 包含 AwaitExpression 是 true,则为语法错误
  • 若与此产品匹配的源代码是严格代码,则将应用UniqueFormalParameters:FormalParameters的Early Error规则。
  • 若与此生产匹配的源代码是严格代码,则若存在BindingIdentifier并且BindingIdentifier的StringValue为“ eval”或“ arguments”,则它是语法错误。
  • 若FormalParameters的BoundNames的任何元素也出现在AsyncFunctionBody的LexicallyDeclaredNames中,则是语法错误。
  • 若 FormalParameters 包含SuperProperty 是 true,则为语法错误
  • 若 AsyncFunctionBody 包含SuperProperty 是 true,则为语法错误
  • 若 FormalParameters 包含SuperCall 是 true,则为语法错误
  • 若 AsyncFunctionBody 包含SuperCall 是 true,则为语法错误

14.7.2 静态语义:BoundNames

  1. AsyncFunctionDeclaration : async function BindingIdentifier ( FormalParameters ) { AsyncFunctionBody }
  1. 返回 BindingIdentifier 的 BoundNames
  1. AsyncFunctionDeclaration : async function ( FormalParameters ) { AsyncFunctionBody }
  1. 返回 « “*default*“ ».

注 “*default*“在本规范中用作使用导出声明定义的可提升匿名函数的合成名称。.

14.7.3 静态语义:ComputedProperty包含

带有参数 symbol.

  1. AsyncMethod : async PropertyName ( UniqueFormalParameters ) { AsyncFunctionBody }
  1. 返回带参数symbol的PropertyName的ComputedPropertyContains结果.

14.7.4 静态语义:包含

带有参数 symbol.

  1. AsyncFunctionDeclaration : async function BindingIdentifier ( FormalParameters ) { AsyncFunctionBody }
  2. AsyncFunctionDeclaration : async function ( FormalParameters ) { AsyncFunctionBody }
  3. AsyncFunctionExpression : async function ( FormalParameters ) { AsyncFunctionBody }
  4. AsyncFunctionExpression : async function BindingIdentifier ( FormalParameters ) { AsyncFunctionBody }
  1. 返回 false.

14.7.5 静态语义:HasDirectSuper

  1. AsyncMethod : async PropertyName ( UniqueFormalParameters ) { AsyncFunctionBody }
  1. 若 UniqueFormalParameters 包含SuperCall 是 true, 返回 true.
  2. 返回 AsyncFunctionBody 包含SuperCall.

14.7.6 静态语义:HasName

  1. AsyncFunctionExpression : async function ( FormalParameters ) { AsyncFunctionBody }
  1. 返回 false
  1. AsyncFunctionExpression : async function BindingIdentifier ( FormalParameters ) { AsyncFunctionBody }
  1. 返回 true.

14.7.7 静态语义:IsConstantDeclaration

  1. AsyncFunctionDeclaration : async function BindingIdentifier ( FormalParameters ) { AsyncFunctionBody }
  2. AsyncFunctionDeclaration : async function ( FormalParameters ) { AsyncFunctionBody }
  1. 返回 false.

14.7.8 静态语义:IsFunctionDefinition

  1. AsyncFunctionExpression : async function ( FormalParameters ) { AsyncFunctionBody }
  2. AsyncFunctionExpression : async function BindingIdentifier ( FormalParameters ) { AsyncFunctionBody }
  1. 返回 true.

14.7.9 静态语义:PropName

  1. AsyncMethod : async PropertyName ( UniqueFormalParameters ) { AsyncFunctionBody }
  1. 返回 PropertyName 的 PropName

14.7.10 运行时语义:InstantiateFunctionObject

带有参数 scope.

  1. AsyncFunctionDeclaration : async function BindingIdentifier ( FormalParameters ) { AsyncFunctionBody }
  1. 如果AsyncFunctionDeclaration的函数代码是严格模式代码, 令 strict 为 true. 否则,令 strict 为 false.
  2. 令 name 为 BindingIdentifier 的 StringValue
  3. 令 F 为 ! AsyncFunctionCreate(Normal, FormalParameters, AsyncFunctionBody, scope, strict).
  4. 执行 ! SetFunctionName(F, name).
  5. 设置 F.[[SourceText]] 为AsyncFunctionDeclaration匹配的源文本.
  6. 返回 F
  1. AsyncFunctionDeclaration : async function ( FormalParameters ) { AsyncFunctionBody }
  1. 若如果AsyncFunctionDeclaration的函数代码是严格模式代码, 令 strict 为 true. 否则,令 strict 为 false.
  2. 令 F 为 ! AsyncFunctionCreate(Normal, FormalParameters, AsyncFunctionBody, scope, strict).
  3. 执行 ! SetFunctionName(F, “default”).
  4. 设置 F.[[SourceText]] 为AsyncFunctionDeclaration匹配的源文本.
  5. 返回 F.

14.7.11 运行时语义:EvaluateBody

带有参数 functionObject 和 argumentsList列表.

  1. AsyncFunctionBody : FunctionBody
  1. 令 promiseCapability 为 ! NewPromiseCapability(%Promise%).
  2. 令 declResult 为 FunctionDeclarationInstantiation(functionObject, argumentsList).
  3. 若 declResult 不是 abrupt completion,那么
  4. 执行 ! AsyncFunctionStart(promiseCapability, FunctionBody).
  5. 否则 declResult 是 abrupt completion,
    1. 执行 ! Call(promiseCapability.[[Reject]], undefined, « declResult.[[Value]] »).
  6. 返回 Completion { [[Type]]: 返回, [[Value]]: promiseCapability.[[Promise]], [[Target]]: empty }.

14.7.12 运行时语义:PropertyDefinitionEvaluation

带有参数 object 和 enumerable.

  1. AsyncMethod : async PropertyName ( UniqueFormalParameters ) { AsyncFunctionBody }
  1. 令 propKey 为运算 PropertyName 的结果
  2. ReturnIfAbrupt(propKey).
  3. 若此AsyncMethod的函数代码是严格模式代码, 令 strict 为 true. 否则,令 strict 为 false.
  4. 令 scope 为运行时上下文的LexicalEnvironment.
  5. 令 closure 为 ! AsyncFunctionCreate(Method, UniqueFormalParameters, AsyncFunctionBody, scope, strict).
  6. 执行 ! MakeMethod(closure, object).
  7. 执行 ! SetFunctionName(closure, propKey).
  8. 设置 closure.[[SourceText]] 为AsyncMethod匹配的源文本.
  9. 令 desc 为 PropertyDescriptor { [[Value]]: closure, [[Writable]]: true, [[Enumerable]]: enumerable, [[Configurable]]: true }.
  10. 返回 ? DefinePropertyOrThrow(object, propKey, desc).

14.7.13 运行时语义:NamedEvaluation

带有参数 name.

  1. AsyncFunctionExpression : async function ( FormalParameters ) { AsyncFunctionBody }
  1. 令 closure 为运算此AsyncFunctionExpression的结果
  2. 执行 SetFunctionName(closure, name).
  3. 返回 closure

14.7.14 运行时语义:Evaluation

  1. AsyncFunctionDeclaration : async function BindingIdentifier ( FormalParameters ) { AsyncFunctionBody }
  1. 返回 NormalCompletion(empty)
  1. AsyncFunctionDeclaration : async function ( FormalParameters ) { AsyncFunctionBody }
  1. 返回 NormalCompletion(empty).
  1. AsyncFunctionExpression : async function ( FormalParameters ) { AsyncFunctionBody }
  1. 若AsyncFunctionExpression的函数代码是严格模式代码, 令 strict 为 true. 否则,令 strict 为 false.
  2. 令 scope 为运行时上下文的LexicalEnvironment.
  3. 令 closure 为 ! AsyncFunctionCreate(Normal, FormalParameters, AsyncFunctionBody, scope, strict).
  4. 设置 closure.[[SourceText]] 为AsyncFunctionExpression匹配的源文本.
  5. 返回 closure.
  1. AsyncFunctionExpression : async function BindingIdentifier ( FormalParameters ) { AsyncFunctionBody }
  1. 若AsyncFunctionExpression的函数代码是严格模式代码, 令 strict 为 true. 否则,令 strict 为 false.
  2. 令 scope 为运行时上下文的LexicalEnvironment.
  3. 令 funcEnv 为 ! NewDeclarativeEnvironment(scope).
  4. 令 envRec 为 funcEnv 的 EnvironmentRecord.
  5. 令 name 为 BindingIdentifier 的 StringValue
  6. 执行 ! envRec.CreateImmutableBinding(name).
  7. 令 closure 为 ! AsyncFunctionCreate(Normal, FormalParameters, AsyncFunctionBody, funcEnv, strict).
  8. 执行 ! SetFunctionName(closure, name).
  9. 执行 ! envRec.InitializeBinding(name, closure).
  10. 设置 closure.[[SourceText]] 为AsyncFunctionExpression匹配的源文本.
  11. 返回 closure.
  1. AwaitExpression : await UnaryExpression
  1. 令 exprRef 为运算 UnaryExpression 的结果
  2. 令 value 为 ? GetValue(exprRef).
  3. 返回 ? Await(value)

14.8 Async 箭头函数

语法

  1. AsyncArrowFunction[In, Yield, Await] :
  2. async [no LineTerminator here] AsyncArrowBindingIdentifier[?Yield] [no LineTerminator here] =>
  3. AsyncConciseBody[?In]
  4. CoverCallExpressionAndAsyncArrowHead[?Yield, ?Await] [no LineTerminator here] =>
  5. AsyncConciseBody[?In]
  6. AsyncConciseBody[In] :
  7. [lookahead {] AssignmentExpression[?In, ~Yield, +Await]
  8. { AsyncFunctionBody }
  9. AsyncArrowBindingIdentifier[Yield] :
  10. BindingIdentifier[?Yield, +Await]
  11. CoverCallExpressionAndAsyncArrowHead[Yield, Await] :
  12. MemberExpression[?Yield, ?Await] Arguments[?Yield, ?Await]

补充语法

处理产生式的实例时 AsyncArrowFunction : CoverCallExpressionAndAsyncArrowHead => AsyncConciseBody,使用以下语法完善CoverCallExpressionAndAsyncArrowHead的解释:

  1. AsyncArrowHead :
  2. async [no LineTerminator here] ArrowFormalParameters[~Yield, +Await]

14.8.1 静态语义:Early Errors

  1. AsyncArrowFunction : async AsyncArrowBindingIdentifier => AsyncConciseBody
  • 若AsyncArrowBindingIdentifier的BoundNames中的任何元素也出现在AsyncConciseBody的LexicallyDeclaredNames中。,则为语法错误
  1. AsyncArrowFunction : CoverCallExpressionAndAsyncArrowHead => AsyncConciseBody
  • 若 CoverCallExpressionAndAsyncArrowHead 包含 YieldExpression 是 true,则为语法错误
  • 若 CoverCallExpressionAndAsyncArrowHead 包含 AwaitExpression 是 true,则为语法错误
  • 若 CoverCallExpressionAndAsyncArrowHead 没有覆盖 AsyncArrowHead。,则为语法错误
  • 如果CoverCallExpressionAndAsyncArrowHead的BoundNames中的任何元素也出现在AsyncConciseBody的LexicallyDeclaredNames中,则是语法错误。
  • 如果AsyncConciseBody的ContainsUseStrict为true,并且CoverCallExpressionAndAsyncArrowHead的IsSimpleParameterList为false,则为语法错误。
  • AsyncArrowHead及其衍生产生式的所有早期错误规则均适用于CoverCallExpressionAndAsyncArrowHead的CoveredAsyncArrowHead。

14.8.2 静态语义:CoveredAsyncArrowHead

  1. CoverCallExpressionAndAsyncArrowHead : MemberExpression Arguments
  1. 返回被CoverCallExpressionAndAsyncArrowHead覆盖的AsyncArrowHead。

14.8.3 静态语义:BoundNames

  1. CoverCallExpressionAndAsyncArrowHead : MemberExpression Arguments
  1. 令 head 为 CoverCallExpressionAndAsyncArrowHead 的 CoveredAsyncArrowHead
  2. 返回 head 的 BoundNames

14.8.4 静态语义:包含

带有参数 symbol.

  1. AsyncArrowFunction : async AsyncArrowBindingIdentifier => AsyncConciseBody
  1. 若 symbol 不是 NewTarget, SuperProperty, SuperCall, super, 或 this 其中之一, 返回 false.
  2. 返回 AsyncConciseBody 包含symbol.
  1. AsyncArrowFunction : CoverCallExpressionAndAsyncArrowHead => AsyncConciseBody
  1. 若 symbol 不是 NewTarget, SuperProperty, SuperCall, super, 或 this 其中之一, 返回 false.
  2. 令 head 为 CoverCallExpressionAndAsyncArrowHead 的 CoveredAsyncArrowHead
  3. 若 head 包含symbol 是 true, 返回 true.
  4. 返回 AsyncConciseBody 包含symbol.

注:通常,Contains并不在大多数函数表单中。但是,Contains用于检测AsyncArrowFunction中的new.target、this和super用法。

14.8.5 静态语义:ContainsExpression

  1. AsyncArrowBindingIdentifier : BindingIdentifier
  1. 返回 false.

14.8.6 静态语义:ExpectedArgumentCount

  1. AsyncArrowBindingIdentifier : BindingIdentifier
  1. 返回 1.

14.8.7 静态语义:HasName

  1. AsyncArrowFunction : async AsyncArrowBindingIdentifier => AsyncConciseBody
  2. AsyncArrowFunction : CoverCallExpressionAndAsyncArrowHead => AsyncConciseBody
  1. 返回 false.

14.8.8 静态语义:IsSimpleParameterList

  1. AsyncArrowBindingIdentifier : BindingIdentifier
  1. 返回 true.
  1. CoverCallExpressionAndAsyncArrowHead : MemberExpression Arguments
  1. 令 head 为 CoverCallExpressionAndAsyncArrowHead 的 CoveredAsyncArrowHead
  2. 返回 head 的 IsSimpleParameterList

14.8.9 静态语义:LexicallyDeclaredNames

  1. AsyncConciseBody : AssignmentExpression
  1. 返回一个新的空列表.

14.8.10 静态语义:LexicallyScopedDeclarations

  1. AsyncConciseBody : AssignmentExpression
  1. 返回一个新的空列表.

14.8.11 静态语义:VarDeclaredNames

  1. AsyncConciseBody : AssignmentExpression
  1. 返回一个新的空列表.

14.8.12 静态语义:VarScopedDeclarations

  1. AsyncConciseBody : AssignmentExpression
  1. 返回一个新的空列表.

14.8.13 运行时语义:IteratorBindingInitialization

带有参数 iteratorRecord 和 environment.

  1. AsyncArrowBindingIdentifier : BindingIdentifier
  1. 断言: iteratorRecord.[[Done]] 是 false.
  2. 令 next 为 IteratorStep(iteratorRecord).
  3. 若 next 是 abrupt completion, 设置 iteratorRecord.[[Done]] 为 true.
  4. ReturnIfAbrupt(next).
  5. 若 next 是 false, 设置 iteratorRecord.[[Done]] 为 true.
  6. 否则,
    1. 令 v 为 IteratorValue(next).
    2. 若 v 是 abrupt completion, 设置 iteratorRecord.[[Done]] 为 true.
    3. ReturnIfAbrupt(v).
  7. 若 iteratorRecord.[[Done]] 是 true, 令 v 为 undefined.
  8. 返回使用v和environment作为参数对BindingIdentifier执行BindingInitialization的结果。

14.8.14 运行时语义:EvaluateBody

带有参数 functionObject 和 argumentsList列表.

  1. AsyncConciseBody : AssignmentExpression
  1. 令 promiseCapability 为 ! NewPromiseCapability(%Promise%).
  2. 令 declResult 为 FunctionDeclarationInstantiation(functionObject, argumentsList).
  3. 若 declResult 不是 abrupt completion,那么
  4. 执行 ! AsyncFunctionStart(promiseCapability, AssignmentExpression).
  5. 否则 declResult 是 abrupt completion,
    1. 执行 ! Call(promiseCapability.[[Reject]], undefined, « declResult.[[Value]] »).
  6. 返回 Completion { [[Type]]: return, [[Value]]: promiseCapability.[[Promise]], [[Target]]: empty }.
  1. AsyncConciseBody : { AsyncFunctionBody }
  1. 返回AsyncFunctionBody的EvaluateBody将functionObject和argumentsList作为参数传递的结果。

14.8.15 运行时语义:NamedEvaluation

带有参数 name.

  1. AsyncArrowFunction : async AsyncArrowBindingIdentifier => AsyncConciseBody
  2. AsyncArrowFunction : CoverCallExpressionAndAsyncArrowHead => AsyncConciseBody
  1. 令 closure 为运算此AsyncArrowFunction的结果.
  2. 执行 SetFunctionName(closure, name).
  3. 返回 closure.

14.8.16 运行时语义:Evaluation

  1. AsyncArrowFunction : async AsyncArrowBindingIdentifier => AsyncConciseBody
  1. 若此AsyncArrowFunction的函数代码是严格模式代码, 令 strict 为 true. 否则,令 strict 为 false.
  2. 令 scope 为运行时上下文的LexicalEnvironment.
  3. 令 parameters 为 AsyncArrowBindingIdentifier.
  4. 令 closure 为 ! AsyncFunctionCreate(Arrow, parameters, AsyncConciseBody, scope, strict).
  5. 返回 closure.
  1. AsyncArrowFunction : CoverCallExpressionAndAsyncArrowHead => AsyncConciseBody
  1. 若此AsyncArrowFunction的函数代码是严格模式代码, 令 strict 为 true. 否则,令 strict 为 false.
  2. 令 scope 为运行时上下文的LexicalEnvironment.
  3. 令 head 为 CoverCallExpressionAndAsyncArrowHead 的 CoveredAsyncArrowHead
  4. 令 parameters 为 head 的 ArrowFormalParameters
  5. 令 closure 为 ! AsyncFunctionCreate(Arrow, parameters, AsyncConciseBody, scope, strict).
  6. 返回 closure.

14.9 尾调用

14.9.1 静态语义:IsInTailPosition ( call )

带有参数call的抽象操作IsInTailPosition执行以下步骤:

  1. 断言: call 是一个解析节点.
  2. 若源代码匹配调用是非严格代码, 返回 false.
  3. 若 call 不包含在 FunctionBody, ConciseBody, 或 AsyncConciseBody 中, 返回 false.
  4. 令 body 成为最包含call的FunctionBody、ConciseBody或AsyncConciseBody。
  5. 若 body 是 GeneratorBody 的 FunctionBody , 返回 false.
  6. 若 body 是 AsyncFunctionBody 的 FunctionBody, 返回 false.
  7. 若 body 是 AsyncGeneratorBody 的 FunctionBody, 返回 false.
  8. 若 body 是 AsyncConciseBody, 返回 false.
  9. 返回带参数call 的 body 的 HasCallInTailPosition 的结果

注:尾部位置调用仅在严格模式代码中定义,因为有一个通用的非标准语言扩展(参见9.2.9),它支持观察调用者上下文链。

14.9.2 静态语义:HasCallInTailPosition

带有参数 call.

注:call是一个表示特定范围的源文本的解析节点。当以下算法将call与另一个解析节点进行比较时,将测试它们是否表示相同的源文本。

14.9.2.1 Statement Rules

  1. ConciseBody : AssignmentExpression
  1. 返回带有参数 call 的 AssignmentExpression 的 HasCallInTailPosition。
  1. StatementList : StatementList StatementListItem
  1. 令 has 为带有参数 call 的 StatementList 的 HasCallInTailPosition。
  2. 若 has 是 true, 返回 true.
  3. 返回带有参数 call 的 StatementListItem 的 HasCallInTailPosition。
  1. FunctionStatementList : [empty]
  2. StatementListItem : Declaration
  3. Statement :
  4. VariableStatement
  5. EmptyStatement
  6. ExpressionStatement
  7. ContinueStatement
  8. BreakStatement
  9. ThrowStatement
  10. DebuggerStatement
  11. Block : { }
  12. ReturnStatement : return ;
  13. LabelledItem : FunctionDeclaration
  14. IterationStatement :
  15. for ( LeftHandSideExpression of AssignmentExpression ) Statement
  16. for ( var ForBinding of AssignmentExpression ) Statement
  17. for ( ForDeclaration of AssignmentExpression ) Statement
  18. CaseBlock : { }
  1. 返回 false.
  1. IfStatement : if ( Expression ) Statement else Statement
  1. 令 has 为带有参数call的第一个语句的HasCallInTailPosition。
  2. 若 has 是 true, 返回 true.
  3. 返回带有参数call的第二个语句的HasCallInTailPosition。
  1. IfStatement : if ( Expression ) Statement
  2. IterationStatement :
  3. do Statement while ( Expression ) ;
  4. while ( Expression ) Statement
  5. for ( Expressionopt ; Expressionopt ; Expressionopt ) Statement
  6. for ( var VariableDeclarationList ; Expressionopt ; Expressionopt ) Statement
  7. for ( LexicalDeclaration Expressionopt ; Expressionopt ) Statement
  8. for ( LeftHandSideExpression in Expression ) Statement
  9. for ( var ForBinding in Expression ) Statement
  10. for ( ForDeclaration in Expression ) Statement
  1. WithStatement : with ( Expression ) Statement
  1. 返回具有参数call的Statement的HasCallInTailPosition

    1. LabelledStatement :
    2. LabelIdentifier : LabelledItem
    1. 返回具有参数call的LabelledItem的HasCallInTailPosition
  1. ReturnStatement : 返回 Expression ;
  1. 返回具有参数call的Expression的HasCallInTailPosition.
  1. SwitchStatement : switch ( Expression ) CaseBlock
  1. 返回具有参数call的CaseBlock的HasCallInTailPosition
  1. CaseBlock : { CaseClauses DefaultClause CaseClauses }
  1. 令 has 为 false.
  2. 若 the first CaseClauses 存在, 令 has 为具有参数call的第一个CaseClauses的HasCallInTailPosition。
  3. 若 has 是 true, 返回 true.
  4. 令 has 为带参数call的DefaultClause的HasCallInTailPosition。
  5. 若 has 是 true, 返回 true.
  6. 如果存在第二个CaseClauses,则令has成为带有参数call的第二个CaseClauses的HasCallInTailPosition。
  7. 返回 has.
  1. CaseClauses : CaseClauses CaseClause
  1. 令 has 为具有参数call的CaseClauses的HasCallInTailPosition.
  2. 若 has 是 true, 返回 true.
  3. 返回具有参数call的CaseClause的HasCallInTailPosition.
  1. CaseClause : case Expression : StatementList
  2. DefaultClause : default : StatementList
  1. 若 StatementList 存在, 返回具有参数call的StatementList的HasCallInTailPosition.
  2. 返回 false.
  1. TryStatement : try Block Catch
  1. 返回具有参数call的Catch的HasCallInTailPosition.
  1. TryStatement : try Block Finally
  2. TryStatement : try Block Catch Finally
  1. 返回具有参数call的Finally的HasCallInTailPosition
  1. Catch : catch ( CatchParameter ) Block
  1. 返回具有参数call的Block的HasCallInTailPosition.

14.9.2.2 Expression Rules

注:潜在的尾部位置调用(紧随其后的是调用结果的返回GetValue)也是可能的尾部位置调用。函数调用不能返回参考值,因此,此类GetValue操作将始终返回与实际函数调用结果相同的值。

  1. AssignmentExpression :
  2. YieldExpression
  3. ArrowFunction
  4. AsyncArrowFunction
  5. LeftHandSideExpression = AssignmentExpression
  6. LeftHandSideExpression AssignmentOperator AssignmentExpression
  7. BitwiseANDExpression : BitwiseANDExpression & EqualityExpression
  8. BitwiseXORExpression : BitwiseXORExpression ^ BitwiseANDExpression
  9. BitwiseORExpression : BitwiseORExpression | BitwiseXORExpression
  10. EqualityExpression :
  11. EqualityExpression == RelationalExpression
  12. EqualityExpression != RelationalExpression
  13. EqualityExpression === RelationalExpression
  14. EqualityExpression !== RelationalExpression
  15. RelationalExpression :
  16. RelationalExpression < ShiftExpression
  17. RelationalExpression > ShiftExpression
  18. RelationalExpression <= ShiftExpression
  19. RelationalExpression >= ShiftExpression
  20. RelationalExpression instanceof ShiftExpression
  21. RelationalExpression in ShiftExpression
  22. ShiftExpression :
  23. ShiftExpression << AdditiveExpression
  24. ShiftExpression >> AdditiveExpression
  25. ShiftExpression >>> AdditiveExpression
  26. AdditiveExpression :
  27. AdditiveExpression + MultiplicativeExpression
  28. AdditiveExpression - MultiplicativeExpression
  29. MultiplicativeExpression :
  30. MultiplicativeExpression MultiplicativeOperator ExponentiationExpression
  31. ExponentiationExpression :
  32. UpdateExpression ** ExponentiationExpression
  33. UpdateExpression :
  34. LeftHandSideExpression ++
  35. LeftHandSideExpression --
  36. ++ UnaryExpression
  37. -- UnaryExpression
  38. UnaryExpression :
  39. delete UnaryExpression
  40. void UnaryExpression
  41. typeof UnaryExpression
  42. + UnaryExpression
  43. - UnaryExpression
  44. ~ UnaryExpression
  45. ! UnaryExpression
  46. AwaitExpression
  47. CallExpression :
  48. SuperCall
  49. CallExpression [ Expression ]
  50. CallExpression . IdentifierName
  51. NewExpression : new NewExpression
  52. MemberExpression :
  53. MemberExpression [ Expression ]
  54. MemberExpression . IdentifierName
  55. SuperProperty
  56. MetaProperty
  57. new MemberExpression Arguments
  58. PrimaryExpression :
  59. this
  60. IdentifierReference
  61. Literal
  62. ArrayLiteral
  63. ObjectLiteral
  64. FunctionExpression
  65. ClassExpression
  66. GeneratorExpression
  67. AsyncFunctionExpression
  68. AsyncGeneratorExpression
  69. RegularExpressionLiteral
  70. TemplateLiteral
  1. 返回 false.

    1. Expression :
    2. AssignmentExpression
    3. Expression , AssignmentExpression
    1. 返回具有参数call的AssignmentExpression的HasCallInTailPosition.
  1. ConditionalExpression : LogicalORExpression ? AssignmentExpression : AssignmentExpression
  1. 令 has 为带参数call的第一个AssignmentExpression的HasCallInTailPosition。
  2. 若 has 是 true, 返回 true.
  3. 返回具有参数call的第二个AssignmentExpression的HasCallInTailPosition
  1. LogicalANDExpression : LogicalANDExpression && BitwiseORExpression
  1. 返回具有参数call的BitwiseORExpression的HasCallInTailPosition.
  1. LogicalORExpression : LogicalORExpression || LogicalANDExpression
  1. 返回具有参数call的LogicalANDExpression的HasCallInTailPosition.

    1. CallExpression :
    2. CoverCallExpressionAndAsyncArrowHead
    3. CallExpression Arguments
    4. CallExpression TemplateLiteral
    1. 若此 CallExpression 是 call, 返回 true.
    2. 返回 false.
    1. MemberExpression :
    2. MemberExpression TemplateLiteral
    1. 若此 MemberExpression 是 call, 返回 true.
    2. 返回 false.
  1. PrimaryExpression : CoverParenthesizedExpressionAndArrowParameterList
  1. 令 expr 为 CoverParenthesizedExpressionAndArrowParameterList 的 CoveredParenthesizedExpression

  2. 返回具有参数call的expr的HasCallInTailPosition.

    1. ParenthesizedExpression :
    2. ( Expression )
    1. 返回具有参数call的Expression的HasCallInTailPosition

14.9.3 运行时语义:PrepareForTailCall ( )

抽象操作PrepareForTailCall执行以下步骤:

  1. 令 leafContext 为 运行时上下文.
  2. 挂起 leafContext.
  3. 从执行上下文堆栈中弹出leafContext。现在堆栈顶部的执行上下文成为正在运行的执行上下文。
  4. 断言: leafContext没有进一步的用途。它永远不会作为正在运行的执行参数被激活

尾位置调用必须在调用目标函数之前释放与当前执行的函数执行上下文相关的任何临时内部资源,或者重用这些资源以支持目标函数。

注:例如,尾部位置调用应该只将实现的激活记录堆栈的大小增加到目标函数的激活记录的大小超过调用函数的激活记录的大小。如果目标函数的激活记录较小,则堆栈的总大小应该减小。