14 ECMAScript 言語: 文 (Statement) と 宣言 (Declaration)

構文 (Syntax)

Statement[Yield, Await, Return] : BlockStatement[?Yield, ?Await, ?Return] VariableStatement[?Yield, ?Await] EmptyStatement ExpressionStatement[?Yield, ?Await] IfStatement[?Yield, ?Await, ?Return] BreakableStatement[?Yield, ?Await, ?Return] ContinueStatement[?Yield, ?Await] BreakStatement[?Yield, ?Await] [+Return] ReturnStatement[?Yield, ?Await] WithStatement[?Yield, ?Await, ?Return] LabelledStatement[?Yield, ?Await, ?Return] ThrowStatement[?Yield, ?Await] TryStatement[?Yield, ?Await, ?Return] DebuggerStatement Declaration[Yield, Await] : HoistableDeclaration[?Yield, ?Await, ~Default] ClassDeclaration[?Yield, ?Await, ~Default] LexicalDeclaration[+In, ?Yield, ?Await] HoistableDeclaration[Yield, Await, Default] : FunctionDeclaration[?Yield, ?Await, ?Default] GeneratorDeclaration[?Yield, ?Await, ?Default] AsyncFunctionDeclaration[?Yield, ?Await, ?Default] AsyncGeneratorDeclaration[?Yield, ?Await, ?Default] BreakableStatement[Yield, Await, Return] : IterationStatement[?Yield, ?Await, ?Return] SwitchStatement[?Yield, ?Await, ?Return]

14.1 Statement の意味論 (Semantics)

14.1.1 実行時意味論: 評価 (Runtime Semantics: Evaluation)

HoistableDeclaration : GeneratorDeclaration AsyncFunctionDeclaration AsyncGeneratorDeclaration
  1. empty を返す。
HoistableDeclaration : FunctionDeclaration
  1. FunctionDeclaration の Evaluation の結果を ? 付きで返す。
BreakableStatement : IterationStatement SwitchStatement
  1. newLabelSet を新しい空の List とする。
  2. この BreakableStatement の LabelledEvaluation (引数 newLabelSet) の結果を ? 付きで返す。

14.2 Block

構文 (Syntax)

BlockStatement[Yield, Await, Return] : Block[?Yield, ?Await, ?Return] Block[Yield, Await, Return] : { StatementList[?Yield, ?Await, ?Return]opt } StatementList[Yield, Await, Return] : StatementListItem[?Yield, ?Await, ?Return] StatementList[?Yield, ?Await, ?Return] StatementListItem[?Yield, ?Await, ?Return] StatementListItem[Yield, Await, Return] : Statement[?Yield, ?Await, ?Return] Declaration[?Yield, ?Await]

14.2.1 静的意味論: 早期エラー (Static Semantics: Early Errors)

Block : { StatementList }
  • StatementList の LexicallyDeclaredNames に重複する要素が含まれるなら構文エラーである。ただしホストが Web ブラウザであるか又は ブロックレベル Function 宣言 (Web レガシー互換意味論) をサポートし、以下の両条件を満たす場合は除く:

    • IsStrict(this production) が false である。
    • 重複している要素はいずれも FunctionDeclarations によってのみ束縛される。
  • StatementList の LexicallyDeclaredNames の任意の要素が StatementList の VarDeclaredNames にも含まれているなら構文エラーである。

14.2.2 実行時意味論: 評価

Block : { }
  1. empty を返す。
Block : { StatementList }
  1. oldEnv を現在実行中コンテキストの LexicalEnvironment とする。
  2. blockEnvNewDeclarativeEnvironment(oldEnv) とする。
  3. BlockDeclarationInstantiation(StatementList, blockEnv) を実行する。
  4. 実行中コンテキストの LexicalEnvironment を blockEnv に設定する。
  5. blockValueStatementList の Evaluation の Completion とする。
  6. 実行中コンテキストの LexicalEnvironment を oldEnv に戻す。
  7. blockValue を返す。
Note 1

どのように Block を抜けても LexicalEnvironment は常に元に復元される。

StatementList : StatementList StatementListItem
  1. slStatementList の Evaluation を ? 付きで行った結果とする。
  2. sStatementListItem の Evaluation の Completion とする。
  3. UpdateEmpty(s, sl) を ? 付きで返す。
Note 2

StatementList の値はその中で最後に値を生成する項目の値である。例えば以下の eval 呼び出しはいずれも 1 を返す:

eval("1;;;;;")
eval("1;{}")
eval("1;var a;")

14.2.3 BlockDeclarationInstantiation ( code, env )

The abstract operation BlockDeclarationInstantiation takes arguments code (a Parse Node) and env (a Declarative Environment Record) and returns unused. code はブロック本体に対応する Parse Node。env は束縛を生成する Environment Record

Note

Block または CaseBlock が評価される際、新しい Declarative Environment Record が生成され、そのブロック内で宣言された block スコープの変数・定数・関数・クラスの束縛がそこにインスタンス化される。

呼び出されたとき以下の手順を行う:

  1. declarationscode の LexicallyScopedDeclarations とする。
  2. privateEnv を実行中コンテキストの PrivateEnvironment とする。
  3. declarations の各要素 d について:
    1. d の BoundNames の各要素 dn について:
      1. IsConstantDeclaration of dtrue なら:
        1. env.CreateImmutableBinding(dn, true) を実行。
      2. それ以外:
        1. Normative Optional
          ホストが Web ブラウザまたは ブロックレベル Function 宣言 (Web レガシー互換意味論) をサポートするなら:
          1. env.HasBinding(dn) が false なら:
            1. env.CreateMutableBinding(dn, false) を実行。
        2. それ以外:
          1. env.CreateMutableBinding(dn, false) を実行。
    2. dFunctionDeclaration, GeneratorDeclaration, AsyncFunctionDeclaration, AsyncGeneratorDeclaration のいずれかであれば:
      1. fnd の BoundNames の唯一の要素とする。
      2. fo を 引数 env, privateEnvd の InstantiateFunctionObject とする。
      3. Normative Optional
        ホストが Web ブラウザまたは ブロックレベル Function 宣言 (Web レガシー互換意味論) をサポートするなら:
        1. env 内の fn の束縛が未初期化なら:
          1. env.InitializeBinding(fn, fo) を実行。
        2. それ以外:
          1. Assert: dFunctionDeclaration である。
          2. env.SetMutableBinding(fn, fo, false) を実行。
      4. それ以外:
        1. env.InitializeBinding(fn, fo) を実行。
  4. unused を返す。

14.3 Declarations と Variable 文

14.3.1 Let および Const 宣言

Note

letconst 宣言は、現在実行中コンテキストの LexicalEnvironment にスコープを持つ変数を定義する。変数はその包含 Environment Record がインスタンス化された時に生成されるが、その LexicalBinding が評価されるまでいかなる方法でもアクセスできない。Initializer を伴う LexicalBinding により定義された変数は、その InitializerAssignmentExpression が評価されたときに値が割り当てられ、生成時ではない。let 宣言内で Initializer を持たない LexicalBinding によって定義された変数は、その LexicalBinding が評価されたとき undefined が割り当てられる。

構文 (Syntax)

LexicalDeclaration[In, Yield, Await] : LetOrConst BindingList[?In, ?Yield, ?Await] ; LetOrConst : let const BindingList[In, Yield, Await] : LexicalBinding[?In, ?Yield, ?Await] BindingList[?In, ?Yield, ?Await] , LexicalBinding[?In, ?Yield, ?Await] LexicalBinding[In, Yield, Await] : BindingIdentifier[?Yield, ?Await] Initializer[?In, ?Yield, ?Await]opt BindingPattern[?Yield, ?Await] Initializer[?In, ?Yield, ?Await]

14.3.1.1 静的意味論: 早期エラー

LexicalDeclaration : LetOrConst BindingList ;
  • BindingList の BoundNames に "let" が含まれるなら構文エラー。
  • BindingList の BoundNames に重複があるなら構文エラー。
LexicalBinding : BindingIdentifier Initializeropt

14.3.1.2 実行時意味論: 評価

LexicalDeclaration : LetOrConst BindingList ;
  1. BindingList の Evaluation を ? 付きで実行する。
  2. empty を返す。
BindingList : BindingList , LexicalBinding
  1. BindingList の Evaluation を ? 付きで実行。
  2. LexicalBinding の Evaluation を ? 付きで返す。
LexicalBinding : BindingIdentifier
  1. lhs を ! ResolveBinding(StringValue of BindingIdentifier) とする。
  2. InitializeReferencedBinding(lhs, undefined) を実行。
  3. empty を返す。
Note

静的意味論により、この形の LexicalBindingconst 宣言で現れないことが保証されている。

LexicalBinding : BindingIdentifier Initializer
  1. bindingIdBindingIdentifier の StringValue とする。
  2. lhs を ! ResolveBinding(bindingId) とする。
  3. IsAnonymousFunctionDefinition(Initializer) が true なら:
    1. valueInitializer の NamedEvaluation (引数 bindingId) を ? 付きで行った結果とする。
  4. それ以外:
    1. rhsInitializer の Evaluation を ? 付きで行った結果とする。
    2. value を ? GetValue(rhs) とする。
  5. InitializeReferencedBinding(lhs, value) を実行。
  6. empty を返す。
LexicalBinding : BindingPattern Initializer
  1. rhsInitializer の Evaluation を ? 付きで行った結果とする。
  2. value を ? GetValue(rhs) とする。
  3. env を実行中コンテキストの LexicalEnvironment とする。
  4. BindingPattern の BindingInitialization (引数 value, env) を ? 付きで返す。

14.3.2 Variable 文 (var 文)

Note

var 文は現在実行中コンテキストの VariableEnvironment にスコープを持つ変数を宣言する。Var 変数は包含 Environment Record のインスタンス化時に生成され、生成時に undefined へ初期化される。同一 VariableEnvironment のスコープ内で同じ BindingIdentifier が複数の VariableDeclaration に現れても、それらは単一の変数のみを定義する。Initializer を持つ VariableDeclaration により定義された変数は、生成時ではなくその VariableDeclaration が実行される際に InitializerAssignmentExpression の値が割り当てられる。

構文 (Syntax)

VariableStatement[Yield, Await] : var VariableDeclarationList[+In, ?Yield, ?Await] ; VariableDeclarationList[In, Yield, Await] : VariableDeclaration[?In, ?Yield, ?Await] VariableDeclarationList[?In, ?Yield, ?Await] , VariableDeclaration[?In, ?Yield, ?Await] VariableDeclaration[In, Yield, Await] : BindingIdentifier[?Yield, ?Await] Initializer[?In, ?Yield, ?Await]opt BindingPattern[?Yield, ?Await] Initializer[?In, ?Yield, ?Await]

14.3.2.1 実行時意味論: 評価

VariableStatement : var VariableDeclarationList ;
  1. VariableDeclarationList の Evaluation を ? 付きで実行。
  2. empty を返す。
VariableDeclarationList : VariableDeclarationList , VariableDeclaration
  1. VariableDeclarationList の Evaluation を ? 付きで実行。
  2. VariableDeclaration の Evaluation を ? 付きで返す。
VariableDeclaration : BindingIdentifier
  1. empty を返す。
VariableDeclaration : BindingIdentifier Initializer
  1. bindingIdBindingIdentifier の StringValue とする。
  2. lhs を ? ResolveBinding(bindingId) とする。
  3. IsAnonymousFunctionDefinition(Initializer) が true なら:
    1. valueInitializer の NamedEvaluation (引数 bindingId) を ? 付きで行った結果とする。
  4. それ以外:
    1. rhsInitializer の Evaluation を ? 付きで行った結果とする。
    2. value を ? GetValue(rhs) とする。
  5. PutValue(lhs, value) を実行。
  6. empty を返す。
Note

VariableDeclaration が with 文内にネストしており、その BindingIdentifier がその with 文の Object Environment Record のバインディングオブジェクトのプロパティ名と同一である場合、5 のステップは Identifier の VariableEnvironment の束縛ではなくそのプロパティに value を代入する。

VariableDeclaration : BindingPattern Initializer
  1. rhsInitializer の Evaluation を ? 付きで行った結果とする。
  2. rVal を ? GetValue(rhs) とする。
  3. BindingPattern の BindingInitialization (引数 rVal, undefined) を ? 付きで返す。

14.3.3 分割代入用束縛パターン (Destructuring Binding Patterns)

構文 (Syntax)

BindingPattern[Yield, Await] : ObjectBindingPattern[?Yield, ?Await] ArrayBindingPattern[?Yield, ?Await] ObjectBindingPattern[Yield, Await] : { } { BindingRestProperty[?Yield, ?Await] } { BindingPropertyList[?Yield, ?Await] } { BindingPropertyList[?Yield, ?Await] , BindingRestProperty[?Yield, ?Await]opt } ArrayBindingPattern[Yield, Await] : [ Elisionopt BindingRestElement[?Yield, ?Await]opt ] [ BindingElementList[?Yield, ?Await] ] [ BindingElementList[?Yield, ?Await] , Elisionopt BindingRestElement[?Yield, ?Await]opt ] BindingRestProperty[Yield, Await] : ... BindingIdentifier[?Yield, ?Await] BindingPropertyList[Yield, Await] : BindingProperty[?Yield, ?Await] BindingPropertyList[?Yield, ?Await] , BindingProperty[?Yield, ?Await] BindingElementList[Yield, Await] : BindingElisionElement[?Yield, ?Await] BindingElementList[?Yield, ?Await] , BindingElisionElement[?Yield, ?Await] BindingElisionElement[Yield, Await] : Elisionopt BindingElement[?Yield, ?Await] BindingProperty[Yield, Await] : SingleNameBinding[?Yield, ?Await] PropertyName[?Yield, ?Await] : BindingElement[?Yield, ?Await] BindingElement[Yield, Await] : SingleNameBinding[?Yield, ?Await] BindingPattern[?Yield, ?Await] Initializer[+In, ?Yield, ?Await]opt SingleNameBinding[Yield, Await] : BindingIdentifier[?Yield, ?Await] Initializer[+In, ?Yield, ?Await]opt BindingRestElement[Yield, Await] : ... BindingIdentifier[?Yield, ?Await] ... BindingPattern[?Yield, ?Await]

14.3.3.1 実行時意味論: PropertyBindingInitialization : プロパティキーの List を含む normal completion または abrupt completion

The syntax-directed operation UNKNOWN takes UNPARSEABLE ARGUMENTS. 束縛される全プロパティ名のリストを収集する。 It is defined piecewise over the following productions:

BindingPropertyList : BindingPropertyList , BindingProperty
  1. boundNamesBindingPropertyList の PropertyBindingInitialization(value, environment) を ? 付きで行った結果とする。
  2. nextNamesBindingProperty の PropertyBindingInitialization(value, environment) を ? 付きで行った結果とする。
  3. boundNamesnextNames のリスト連結を返す。
BindingProperty : SingleNameBinding
  1. nameSingleNameBinding の BoundNames の唯一の要素とする。
  2. SingleNameBinding の KeyedBindingInitialization(value, environment, name) を ? 付きで実行。
  3. « name » を返す。
BindingProperty : PropertyName : BindingElement
  1. PPropertyName の Evaluation を ? 付きで行った結果とする。
  2. BindingElement の KeyedBindingInitialization(value, environment, P) を ? 付きで実行。
  3. « P » を返す。

14.3.3.2 実行時意味論: RestBindingInitialization : unused を含む normal completion または abrupt completion

The syntax-directed operation UNKNOWN takes UNPARSEABLE ARGUMENTS. It is defined piecewise over the following productions:

BindingRestProperty : ... BindingIdentifier
  1. lhs を ? ResolveBinding(StringValue of BindingIdentifier, environment) とする。
  2. restObjOrdinaryObjectCreate(%Object.prototype%) とする。
  3. CopyDataProperties(restObj, value, excludedNames) を実行。
  4. environmentundefined なら ? PutValue(lhs, restObj) を返す。
  5. InitializeReferencedBinding(lhs, restObj) を返す。

14.3.3.3 実行時意味論: KeyedBindingInitialization : unused を含む normal completion または abrupt completion

The syntax-directed operation UNKNOWN takes UNPARSEABLE ARGUMENTS.

Note

environmentundefined を渡すのは PutValue を用いて初期化値を代入すべきであることを示す。その例は非 strict 関数の仮引数リストなど。そこでは複数同名パラメータの可能性に対応するため事前初期化が行われる。

It is defined piecewise over the following productions:

BindingElement : BindingPattern Initializeropt
  1. v を ? GetV(value, propertyName) とする。
  2. Initializer が存在し vundefined なら:
    1. defaultValueInitializer の Evaluation を ? 付きで行った結果とする。
    2. v を ? GetValue(defaultValue) に設定。
  3. BindingPattern の BindingInitialization(v, environment) を ? 付きで返す。
SingleNameBinding : BindingIdentifier Initializeropt
  1. bindingIdBindingIdentifier の StringValue とする。
  2. lhs を ? ResolveBinding(bindingId, environment) とする。
  3. v を ? GetV(value, propertyName) とする。
  4. Initializer が存在し vundefined なら:
    1. IsAnonymousFunctionDefinition(Initializer) が true なら:
      1. vInitializer の NamedEvaluation (引数 bindingId) を ? 付きで行った結果とする。
    2. それ以外:
      1. defaultValueInitializer の Evaluation を ? 付きで行った結果とする。
      2. v を ? GetValue(defaultValue) に設定。
  5. environmentundefined なら ? PutValue(lhs, v) を返す。
  6. InitializeReferencedBinding(lhs, v) を返す。

14.4 Empty 文 (Empty Statement)

構文 (Syntax)

EmptyStatement : ;

14.4.1 実行時意味論: 評価

EmptyStatement : ;
  1. empty を返す。

14.5 Expression 文 (Expression Statement)

構文 (Syntax)

ExpressionStatement[Yield, Await] : [lookahead ∉ { {, function, async [no LineTerminator here] function, class, let [ }] Expression[+In, ?Yield, ?Await] ; Note

ExpressionStatement は U+007B LEFT CURLY BRACKET で始まれない。これは Block との曖昧さを避けるためである。function または class で始めることもできない。これは FunctionDeclaration, GeneratorDeclaration, ClassDeclaration との曖昧さを避けるためである。async function で始めることは AsyncFunctionDeclaration または AsyncGeneratorDeclaration との曖昧さを避けるため禁止される。let [ の 2 トークン列で始めることは、最初の LexicalBindingArrayBindingPattern である let LexicalDeclaration との曖昧さを避けるため禁止される。

14.5.1 実行時意味論: 評価

ExpressionStatement : Expression ;
  1. exprRefExpression の Evaluation を ? 付きで行った結果とする。
  2. GetValue(exprRef) を返す。

14.6 if

構文 (Syntax)

IfStatement[Yield, Await, Return] : if ( Expression[+In, ?Yield, ?Await] ) Statement[?Yield, ?Await, ?Return] else Statement[?Yield, ?Await, ?Return] if ( Expression[+In, ?Yield, ?Await] ) Statement[?Yield, ?Await, ?Return] [lookahead ≠ else] Note
[lookahead ≠ else] 制約は古典的な「dangling else」問題を通常の方法で解決する。すなわち曖昧な場合、else は最も内側の if に関連付けられる。

14.6.1 静的意味論: 早期エラー

IfStatement : if ( Expression ) Statement else Statement
  • 最初の Statement について IsLabelledFunction が true なら構文エラー。
  • 二番目の Statement について IsLabelledFunction が true なら構文エラー。
IfStatement : if ( Expression ) Statement
  • Statement について IsLabelledFunction が true なら構文エラー。
Note

B.3.1 で規定される拡張が実装されている場合にのみこの規則を適用する必要がある。

14.6.2 実行時意味論: 評価

IfStatement : if ( Expression ) Statement else Statement
  1. exprRefExpression の Evaluation を ? 付きで行った結果とする。
  2. exprValueToBoolean(? GetValue(exprRef)) とする。
  3. exprValuetrue なら:
    1. stmtCompletion を 最初の Statement の Evaluation の Completion とする。
  4. それ以外:
    1. stmtCompletion を 二番目の Statement の Evaluation の Completion とする。
  5. UpdateEmpty(stmtCompletion, undefined) を返す。
IfStatement : if ( Expression ) Statement
  1. exprRefExpression の Evaluation を ? 付きで行った結果とする。
  2. exprValueToBoolean(? GetValue(exprRef)) とする。
  3. exprValuefalse なら:
    1. undefined を返す。
  4. それ以外:
    1. stmtCompletionStatement の Evaluation の Completion とする。
    2. UpdateEmpty(stmtCompletion, undefined) を返す。

14.7 反復文 (Iteration Statements)

構文 (Syntax)

IterationStatement[Yield, Await, Return] : DoWhileStatement[?Yield, ?Await, ?Return] WhileStatement[?Yield, ?Await, ?Return] ForStatement[?Yield, ?Await, ?Return] ForInOfStatement[?Yield, ?Await, ?Return]

14.7.1 意味論 (Semantics)

14.7.1.1 LoopContinues ( completion, labelSet )

The abstract operation LoopContinues takes arguments completion (a Completion Record) and labelSet (a List of Strings) and returns a Boolean. It performs the following steps when called:

  1. completionnormal completion なら true を返す。
  2. completioncontinue completion でないなら false を返す。
  3. completion.[[Target]]empty なら true を返す。
  4. labelSetcompletion.[[Target]] を含むなら true を返す。
  5. false を返す。
Note

IterationStatementStatement 部内では ContinueStatement により新しい反復を開始できる。

14.7.1.2 実行時意味論: LoopEvaluation : ECMAScript 言語値を含む normal completion または abrupt completion

The syntax-directed operation UNKNOWN takes UNPARSEABLE ARGUMENTS. It is defined piecewise over the following productions:

IterationStatement : DoWhileStatement
  1. DoWhileStatement の DoWhileLoopEvaluation(labelSet) を ? 付きで返す。
IterationStatement : WhileStatement
  1. WhileStatement の WhileLoopEvaluation(labelSet) を ? 付きで返す。
IterationStatement : ForStatement
  1. ForStatement の ForLoopEvaluation(labelSet) を ? 付きで返す。
IterationStatement : ForInOfStatement
  1. ForInOfStatement の ForIn/OfLoopEvaluation(labelSet) を ? 付きで返す。

14.7.2 do-while

構文 (Syntax)

DoWhileStatement[Yield, Await, Return] : do Statement[?Yield, ?Await, ?Return] while ( Expression[+In, ?Yield, ?Await] ) ;

14.7.2.1 静的意味論: 早期エラー

DoWhileStatement : do Statement while ( Expression ) ;
  • Statement について IsLabelledFunction が true なら構文エラー。
Note

B.3.1 で規定される拡張が実装されている場合にのみこの規則を適用する。

14.7.2.2 実行時意味論: DoWhileLoopEvaluation : ECMAScript 言語値を含む normal completion または abrupt completion

The syntax-directed operation UNKNOWN takes UNPARSEABLE ARGUMENTS. It is defined piecewise over the following productions:

DoWhileStatement : do Statement while ( Expression ) ;
  1. Vundefined とする。
  2. 繰り返し:
    1. stmtResultStatement の Evaluation の Completion とする。
    2. LoopContinues(stmtResult, labelSet) が false なら ? UpdateEmpty(stmtResult, V) を返す。
    3. stmtResult.[[Value]]empty でなければ VstmtResult.[[Value]] に設定。
    4. exprRefExpression の Evaluation を ? 付きで行った結果とする。
    5. exprValue を ? GetValue(exprRef) とする。
    6. ToBoolean(exprValue) が false なら V を返す。

14.7.3 while

構文 (Syntax)

WhileStatement[Yield, Await, Return] : while ( Expression[+In, ?Yield, ?Await] ) Statement[?Yield, ?Await, ?Return]

14.7.3.1 静的意味論: 早期エラー

WhileStatement : while ( Expression ) Statement
  • Statement について IsLabelledFunction が true なら構文エラー。
Note

B.3.1 の拡張が実装されている場合のみ適用。

14.7.3.2 実行時意味論: WhileLoopEvaluation : ECMAScript 言語値を含む normal completion または abrupt completion

The syntax-directed operation UNKNOWN takes UNPARSEABLE ARGUMENTS. It is defined piecewise over the following productions:

WhileStatement : while ( Expression ) Statement
  1. Vundefined とする。
  2. 繰り返し:
    1. exprRefExpression の Evaluation を ? 付きで行った結果とする。
    2. exprValue を ? GetValue(exprRef) とする。
    3. ToBoolean(exprValue) が false なら V を返す。
    4. stmtResultStatement の Evaluation の Completion とする。
    5. LoopContinues(stmtResult, labelSet) が false なら ? UpdateEmpty(stmtResult, V) を返す。
    6. stmtResult.[[Value]]empty でなければ VstmtResult.[[Value]] に設定。

14.7.4 for

構文 (Syntax)

ForStatement[Yield, Await, Return] : for ( [lookahead ≠ let [] Expression[~In, ?Yield, ?Await]opt ; Expression[+In, ?Yield, ?Await]opt ; Expression[+In, ?Yield, ?Await]opt ) Statement[?Yield, ?Await, ?Return] for ( var VariableDeclarationList[~In, ?Yield, ?Await] ; Expression[+In, ?Yield, ?Await]opt ; Expression[+In, ?Yield, ?Await]opt ) Statement[?Yield, ?Await, ?Return] for ( LexicalDeclaration[~In, ?Yield, ?Await] Expression[+In, ?Yield, ?Await]opt ; Expression[+In, ?Yield, ?Await]opt ) Statement[?Yield, ?Await, ?Return]

14.7.4.1 静的意味論: 早期エラー

ForStatement : for ( Expressionopt ; Expressionopt ; Expressionopt ) Statement for ( var VariableDeclarationList ; Expressionopt ; Expressionopt ) Statement for ( LexicalDeclaration Expressionopt ; Expressionopt ) Statement
  • Statement について IsLabelledFunction が true なら構文エラー。
Note

B.3.1 の拡張が実装されている場合のみ適用。

ForStatement : for ( LexicalDeclaration Expressionopt ; Expressionopt ) Statement

14.7.4.2 実行時意味論: ForLoopEvaluation : ECMAScript 言語値を含む normal completion または abrupt completion

The syntax-directed operation UNKNOWN takes UNPARSEABLE ARGUMENTS. It is defined piecewise over the following productions:

ForStatement : for ( Expressionopt ; Expressionopt ; Expressionopt ) Statement
  1. 最初の Expression が存在するなら:
    1. exprRef をその Evaluation を ? 付きで行った結果とする。
    2. GetValue(exprRef) を実行。
  2. 二番目の Expression が存在するなら test をそれに、存在しなければ empty とする。
  3. 三番目の Expression が存在するなら increment をそれに、存在しなければ empty とする。
  4. ForBodyEvaluation(test, increment, Statement, « », labelSet) を ? 付きで返す。
ForStatement : for ( var VariableDeclarationList ; Expressionopt ; Expressionopt ) Statement
  1. VariableDeclarationList の Evaluation を ? 付きで実行。
  2. 最初の Expression が存在するなら test をそれに、存在しなければ empty
  3. 二番目の Expression が存在するなら increment をそれに、存在しなければ empty
  4. ForBodyEvaluation(test, increment, Statement, « », labelSet) を ? 付きで返す。
ForStatement : for ( LexicalDeclaration Expressionopt ; Expressionopt ) Statement
  1. oldEnv を現在実行中コンテキストの LexicalEnvironment とする。
  2. loopEnvNewDeclarativeEnvironment(oldEnv) とする。
  3. isConstLexicalDeclaration の IsConstantDeclaration とする。
  4. boundNamesLexicalDeclaration の BoundNames とする。
  5. boundNames の各 dn について:
    1. isConsttrue なら:
      1. loopEnv.CreateImmutableBinding(dn, true) を実行。
    2. それ以外:
      1. loopEnv.CreateMutableBinding(dn, false) を実行。
  6. 実行中コンテキストの LexicalEnvironment を loopEnv に設定。
  7. forDclLexicalDeclaration の Evaluation の Completion とする。
  8. forDclabrupt completion なら:
    1. 実行中コンテキストの LexicalEnvironment を oldEnv に戻す。
    2. forDcl を返す。
  9. isConstfalse なら perIterationLetsboundNames、そうでなければ新しい空 List とする。
  10. 最初の Expression が存在するなら test をそれに、存在しなければ empty
  11. 二番目の Expression が存在するなら increment をそれに、存在しなければ empty
  12. bodyResultForBodyEvaluation(test, increment, Statement, perIterationLets, labelSet) の Completion とする。
  13. 実行中コンテキストの LexicalEnvironment を oldEnv に戻す。
  14. bodyResult を返す。

14.7.4.3 ForBodyEvaluation ( test, increment, stmt, perIterationBindings, labelSet )

The abstract operation ForBodyEvaluation takes arguments test (an Expression Parse Node or empty), increment (an Expression Parse Node or empty), stmt (a Statement Parse Node), perIterationBindings (a List of Strings), and labelSet (a List of Strings) and returns ECMAScript 言語値を含む normal completion または abrupt completion. It performs the following steps when called:

  1. Vundefined とする。
  2. CreatePerIterationEnvironment(perIterationBindings) を実行。
  3. 繰り返し:
    1. testempty でないなら:
      1. testReftest の Evaluation を ? 付きで行った結果とする。
      2. testValue を ? GetValue(testRef) とする。
      3. ToBoolean(testValue) が false なら V を返す。
    2. resultstmt の Evaluation の Completion とする。
    3. LoopContinues(result, labelSet) が false なら ? UpdateEmpty(result, V) を返す。
    4. result.[[Value]]empty でなければ Vresult.[[Value]] に設定。
    5. CreatePerIterationEnvironment(perIterationBindings) を実行。
    6. incrementempty でないなら:
      1. incRefincrement の Evaluation を ? 付きで行った結果とする。
      2. GetValue(incRef) を実行。

14.7.4.4 CreatePerIterationEnvironment ( perIterationBindings )

The abstract operation CreatePerIterationEnvironment takes argument perIterationBindings (a List of Strings) and returns unused を含む normal completion または throw completion. It performs the following steps when called:

  1. perIterationBindings が要素を持つなら:
    1. lastIterationEnv を現在実行中コンテキストの LexicalEnvironment とする。
    2. outerlastIterationEnv.[[OuterEnv]] とする。
    3. Assert: outernull ではない。
    4. thisIterationEnvNewDeclarativeEnvironment(outer) とする。
    5. perIterationBindings の各 bn について:
      1. thisIterationEnv.CreateMutableBinding(bn, false) を実行。
      2. lastValue を ? lastIterationEnv.GetBindingValue(bn, true) とする。
      3. thisIterationEnv.InitializeBinding(bn, lastValue) を実行。
    6. 実行中コンテキストの LexicalEnvironment を thisIterationEnv に設定。
  2. unused を返す。

14.7.5 for-in, for-of, for-await-of

構文 (Syntax)

ForInOfStatement[Yield, Await, Return] : for ( [lookahead ≠ let [] LeftHandSideExpression[?Yield, ?Await] in Expression[+In, ?Yield, ?Await] ) Statement[?Yield, ?Await, ?Return] for ( var ForBinding[?Yield, ?Await] in Expression[+In, ?Yield, ?Await] ) Statement[?Yield, ?Await, ?Return] for ( ForDeclaration[?Yield, ?Await] in Expression[+In, ?Yield, ?Await] ) Statement[?Yield, ?Await, ?Return] for ( [lookahead ∉ { let, async of }] LeftHandSideExpression[?Yield, ?Await] of AssignmentExpression[+In, ?Yield, ?Await] ) Statement[?Yield, ?Await, ?Return] for ( var ForBinding[?Yield, ?Await] of AssignmentExpression[+In, ?Yield, ?Await] ) Statement[?Yield, ?Await, ?Return] for ( ForDeclaration[?Yield, ?Await] of AssignmentExpression[+In, ?Yield, ?Await] ) Statement[?Yield, ?Await, ?Return] [+Await] for await ( [lookahead ≠ let] LeftHandSideExpression[?Yield, ?Await] of AssignmentExpression[+In, ?Yield, ?Await] ) Statement[?Yield, ?Await, ?Return] [+Await] for await ( var ForBinding[?Yield, ?Await] of AssignmentExpression[+In, ?Yield, ?Await] ) Statement[?Yield, ?Await, ?Return] [+Await] for await ( ForDeclaration[?Yield, ?Await] of AssignmentExpression[+In, ?Yield, ?Await] ) Statement[?Yield, ?Await, ?Return] ForDeclaration[Yield, Await] : LetOrConst ForBinding[?Yield, ?Await] ForBinding[Yield, Await] : BindingIdentifier[?Yield, ?Await] BindingPattern[?Yield, ?Await] Note

この節は 付録 B.3.5 により拡張される。

14.7.5.1 静的意味論: 早期エラー

ForInOfStatement : for ( LeftHandSideExpression in Expression ) Statement for ( var ForBinding in Expression ) Statement for ( ForDeclaration in Expression ) Statement for ( LeftHandSideExpression of AssignmentExpression ) Statement for ( var ForBinding of AssignmentExpression ) Statement for ( ForDeclaration of AssignmentExpression ) Statement for await ( LeftHandSideExpression of AssignmentExpression ) Statement for await ( var ForBinding of AssignmentExpression ) Statement for await ( ForDeclaration of AssignmentExpression ) Statement
  • Statement について IsLabelledFunction が true なら構文エラー。
Note

B.3.1 の拡張が実装されている場合のみ適用。

ForInOfStatement : for ( LeftHandSideExpression in Expression ) Statement for ( LeftHandSideExpression of AssignmentExpression ) Statement for await ( LeftHandSideExpression of AssignmentExpression ) Statement ForInOfStatement : for ( ForDeclaration in Expression ) Statement for ( ForDeclaration of AssignmentExpression ) Statement for await ( ForDeclaration of AssignmentExpression ) Statement
  • ForDeclaration の BoundNames に "let" が含まれるなら構文エラー。
  • ForDeclaration の BoundNames の任意要素が Statement の VarDeclaredNames にも現れるなら構文エラー。
  • ForDeclaration の BoundNames に重複があるなら構文エラー。

14.7.5.2 静的意味論: IsDestructuring : Boolean

The syntax-directed operation UNKNOWN takes UNPARSEABLE ARGUMENTS. It is defined piecewise over the following productions:

MemberExpression : PrimaryExpression
  1. PrimaryExpressionObjectLiteral または ArrayLiteral なら true を返す。
  2. false を返す。
MemberExpression : MemberExpression [ Expression ] MemberExpression . IdentifierName MemberExpression TemplateLiteral SuperProperty MetaProperty new MemberExpression Arguments MemberExpression . PrivateIdentifier NewExpression : new NewExpression LeftHandSideExpression : CallExpression OptionalExpression
  1. false を返す。
ForDeclaration : LetOrConst ForBinding
  1. ForBinding の IsDestructuring を返す。
ForBinding : BindingIdentifier
  1. false を返す。
ForBinding : BindingPattern
  1. true を返す。
Note

この節は 付録 B.3.5 により拡張される。

14.7.5.3 実行時意味論: ForDeclarationBindingInitialization : unused を含む normal completion または abrupt completion

The syntax-directed operation UNKNOWN takes UNPARSEABLE ARGUMENTS.

Note

environmentundefined を渡すのは PutValue による代入を用いるべきことを示す。これは var 文や一部非 strict 関数(10.2.11 参照)の仮引数リストの場合で、これらでは初期化子評価前にレキシカル束縛がホイストされ事前初期化される。

It is defined piecewise over the following productions:

ForDeclaration : LetOrConst ForBinding
  1. ForBinding の BindingInitialization(value, environment) を ? 付きで返す。

14.7.5.4 実行時意味論: ForDeclarationBindingInstantiation : unused

The syntax-directed operation UNKNOWN takes UNPARSEABLE ARGUMENTS. It is defined piecewise over the following productions:

ForDeclaration : LetOrConst ForBinding
  1. ForBinding の BoundNames の各 name について:
    1. LetOrConst の IsConstantDeclaration が true なら:
      1. environment.CreateImmutableBinding(name, true) を実行。
    2. それ以外:
      1. environment.CreateMutableBinding(name, false) を実行。
  2. unused を返す。

14.7.5.5 実行時意味論: ForInOfLoopEvaluation : ECMAScript 言語値を含む normal completion または abrupt completion

The syntax-directed operation UNKNOWN takes UNPARSEABLE ARGUMENTS. It is defined piecewise over the following productions:

ForInOfStatement : for ( LeftHandSideExpression in Expression ) Statement
  1. keyResult を ? ForIn/OfHeadEvaluation(« », Expression, enumerate) とする。
  2. ForIn/OfBodyEvaluation(LeftHandSideExpression, Statement, keyResult, enumerate, assignment, labelSet) を返す。
ForInOfStatement : for ( var ForBinding in Expression ) Statement
  1. keyResult を ? ForIn/OfHeadEvaluation(« », Expression, enumerate) とする。
  2. ForIn/OfBodyEvaluation(ForBinding, Statement, keyResult, enumerate, var-binding, labelSet) を返す。
ForInOfStatement : for ( ForDeclaration in Expression ) Statement
  1. keyResult を ? ForIn/OfHeadEvaluation(BoundNames of ForDeclaration, Expression, enumerate) とする。
  2. ForIn/OfBodyEvaluation(ForDeclaration, Statement, keyResult, enumerate, lexical-binding, labelSet) を返す。
ForInOfStatement : for ( LeftHandSideExpression of AssignmentExpression ) Statement
  1. keyResult を ? ForIn/OfHeadEvaluation(« », AssignmentExpression, iterate) とする。
  2. ForIn/OfBodyEvaluation(LeftHandSideExpression, Statement, keyResult, iterate, assignment, labelSet) を返す。
ForInOfStatement : for ( var ForBinding of AssignmentExpression ) Statement
  1. keyResult を ? ForIn/OfHeadEvaluation(« », AssignmentExpression, iterate) とする。
  2. ForIn/OfBodyEvaluation(ForBinding, Statement, keyResult, iterate, var-binding, labelSet) を返す。
ForInOfStatement : for ( ForDeclaration of AssignmentExpression ) Statement
  1. keyResult を ? ForIn/OfHeadEvaluation(BoundNames of ForDeclaration, AssignmentExpression, iterate) とする。
  2. ForIn/OfBodyEvaluation(ForDeclaration, Statement, keyResult, iterate, lexical-binding, labelSet) を返す。
ForInOfStatement : for await ( LeftHandSideExpression of AssignmentExpression ) Statement
  1. keyResult を ? ForIn/OfHeadEvaluation(« », AssignmentExpression, async-iterate) とする。
  2. ForIn/OfBodyEvaluation(LeftHandSideExpression, Statement, keyResult, iterate, assignment, labelSet, async) を返す。
ForInOfStatement : for await ( var ForBinding of AssignmentExpression ) Statement
  1. keyResult を ? ForIn/OfHeadEvaluation(« », AssignmentExpression, async-iterate) とする。
  2. ForIn/OfBodyEvaluation(ForBinding, Statement, keyResult, iterate, var-binding, labelSet, async) を返す。
ForInOfStatement : for await ( ForDeclaration of AssignmentExpression ) Statement
  1. keyResult を ? ForIn/OfHeadEvaluation(BoundNames of ForDeclaration, AssignmentExpression, async-iterate) とする。
  2. ForIn/OfBodyEvaluation(ForDeclaration, Statement, keyResult, iterate, lexical-binding, labelSet, async) を返す。
Note

この節は 付録 B.3.5 により拡張される。

14.7.5.6 ForIn/OfHeadEvaluation ( uninitializedBoundNames, expr, iterationKind )

The abstract operation ForIn/OfHeadEvaluation takes arguments uninitializedBoundNames (a List of Strings), expr (an Expression Parse Node or an AssignmentExpression Parse Node), and iterationKind (enumerate, iterate, or async-iterate) and returns Iterator Record を含む normal completion または abrupt completion. It performs the following steps when called:

  1. oldEnv を現在実行中コンテキストの LexicalEnvironment とする。
  2. uninitializedBoundNames が空でないなら:
    1. Assert: 重複はない。
    2. newEnvNewDeclarativeEnvironment(oldEnv) とする。
    3. uninitializedBoundNames の各 String name について:
      1. newEnv.CreateMutableBinding(name, false) を実行。
    4. 実行中コンテキストの LexicalEnvironment を newEnv に設定。
  3. exprRefexpr の Evaluation の Completion とする。
  4. 実行中コンテキストの LexicalEnvironment を oldEnv に戻す。
  5. exprValue を ? GetValue(? exprRef) とする。
  6. iterationKindenumerate なら:
    1. exprValueundefined または null なら:
      1. Completion Record { [[Type]]: break, [[Value]]: empty, [[Target]]: empty } を返す。
    2. obj を ! ToObject(exprValue) とする。
    3. iteratorEnumerateObjectProperties(obj) とする。
    4. nextMethod を ! GetV(iterator, "next") とする。
    5. Iterator Record { [[Iterator]]: iterator, [[NextMethod]]: nextMethod, [[Done]]: false } を返す。
  7. それ以外:
    1. Assert: iterationKinditerate 又は async-iterate
    2. iterationKindasync-iterate なら iteratorKindasync とする。そうでなければ sync
    3. GetIterator(exprValue, iteratorKind) を返す。

14.7.5.7 ForIn/OfBodyEvaluation ( lhs, stmt, iteratorRecord, iterationKind, lhsKind, labelSet [ , iteratorKind ] )

The abstract operation ForIn/OfBodyEvaluation takes arguments lhs (a Parse Node), stmt (a Statement Parse Node), iteratorRecord (an Iterator Record), iterationKind (enumerate or iterate), lhsKind (assignment, var-binding, or lexical-binding), and labelSet (a List of Strings) and optional argument iteratorKind (sync or async) and returns ECMAScript 言語値を含む normal completion または abrupt completion. It performs the following steps when called:

  1. iteratorKind が与えられていなければ sync とする。
  2. oldEnv を現在実行中コンテキストの LexicalEnvironment とする。
  3. Vundefined とする。
  4. destructuring を IsDestructuring of lhs とする。
  5. destructuringtrue かつ lhsKindassignment なら:
    1. Assert: lhsLeftHandSideExpression
    2. assignmentPatternlhs が覆う AssignmentPattern とする。
  6. 繰り返し:
    1. nextResult を ? Call(iteratorRecord.[[NextMethod]], iteratorRecord.[[Iterator]]) とする。
    2. iteratorKindasync なら nextResult を ? Await(nextResult) とする。
    3. nextResult が Object でなければ TypeError を throw。
    4. done を ? IteratorComplete(nextResult) とする。
    5. donetrue なら V を返す。
    6. nextValue を ? IteratorValue(nextResult) とする。
    7. lhsKindassignment または var-binding の場合:
      1. destructuringtrue なら:
        1. lhsKindassignment なら:
          1. status を DestructuringAssignmentEvaluation of assignmentPattern (nextValue 引数) の Completion
        2. それ以外:
          1. Assert: lhsKindvar-binding
          2. Assert: lhsForBinding
          3. statusForBinding の BindingInitialization(nextValue, undefined) の Completion
      2. それ以外:
        1. lhsReflhs の Evaluation の Completion(繰り返し行われ得る)とする。
        2. lhsKindassignment かつ lhs の AssignmentTargetType が web-compat なら ReferenceError を throw。
        3. lhsRefabrupt completion なら:
          1. statuslhsRef とする。
        4. それ以外:
          1. statusCompletion(PutValue(lhsRef.[[Value]], nextValue)) とする。
    8. それ以外:
      1. Assert: lhsKindlexical-binding
      2. Assert: lhsForDeclaration
      3. iterationEnvNewDeclarativeEnvironment(oldEnv) とする。
      4. ForDeclarationBindingInstantiation of lhs (iterationEnv) を実行。
      5. 実行中コンテキストの LexicalEnvironment を iterationEnv に設定。
      6. destructuringtrue なら:
        1. status を ForDeclarationBindingInitialization of lhs (nextValue, iterationEnv) の Completion
      7. それ以外:
        1. Assert: lhs は単一名を束縛する。
        2. lhsNamelhs の BoundNames の唯一の要素とする。
        3. lhsRef を ! ResolveBinding(lhsName) とする。
        4. statusCompletion(InitializeReferencedBinding(lhsRef, nextValue)) とする。
    9. statusabrupt completion なら:
      1. 実行中コンテキストの LexicalEnvironment を oldEnv に戻す。
      2. iteratorKindasync なら ? AsyncIteratorClose(iteratorRecord, status) を返す。
      3. iterationKindenumerate なら:
        1. status を返す。
      4. それ以外:
        1. Assert: iterationKinditerate
        2. IteratorClose(iteratorRecord, status) を返す。
    10. resultStatement の Evaluation の Completion とする。
    11. 実行中コンテキストの LexicalEnvironment を oldEnv に戻す。
    12. LoopContinues(result, labelSet) が false なら:
      1. iterationKindenumerate なら:
        1. UpdateEmpty(result, V) を返す。
      2. それ以外:
        1. Assert: iterationKinditerate
        2. statusCompletion(UpdateEmpty(result, V)) とする。
        3. iteratorKindasync なら ? AsyncIteratorClose(iteratorRecord, status) を返す。
        4. IteratorClose(iteratorRecord, status) を返す。
    13. result.[[Value]]empty でなければ Vresult.[[Value]] に設定。

14.7.5.8 実行時意味論: 評価

BindingIdentifier : Identifier yield await
  1. bindingIdBindingIdentifier の StringValue とする。
  2. ResolveBinding(bindingId) を返す。

14.7.5.9 EnumerateObjectProperties ( O )

The abstract operation EnumerateObjectProperties takes argument O (an Object) and returns 反復子 (iterator object). It performs the following steps when called:

  1. O の列挙可能なプロパティの String キーを反復する next メソッドを持つ反復子オブジェクトを返す。この反復子は ECMAScript コードから直接アクセスできない。列挙の機構と順序は下記規則に従う限り規定されない。

反復子の throw および return メソッドは null で呼ばれない。next メソッドはプロパティキーを返すべきか判定する。返されるキーは Symbol を含まない。列挙中にターゲットオブジェクトのプロパティが削除されることがある。削除されたプロパティは無視される。列挙中に新規プロパティが追加されても処理される保証はない。いかなる列挙でも同じプロパティ名は高々一度だけ返される。

ターゲットオブジェクトの列挙はそのプロトタイプ、さらにそのプロトタイプ…と再帰的に行う。ただし既に next により処理済みの名前と同名のプロトタイプ上のプロパティは処理しない。処理済み判定に [[Enumerable]] 属性値は考慮しない。プロトタイプオブジェクトの列挙可能プロパティ名取得には EnumerateObjectProperties をそのプロトタイプに対し呼び出す。ターゲットオブジェクトの own プロパティキー[[OwnPropertyKeys]] 内部メソッド呼出しで取得し、属性は [[GetOwnProperty]] 内部メソッド呼出しで取得する。

加えて O およびそのプロトタイプ連鎖上のいずれも Proxy, TypedArray, module namespace, 実装提供の exotic object でないなら、以下のいずれかが起こるまで反復子は CreateForInIterator(O) によるものと同様に振る舞わねばならない:

  • O またはプロトタイプ連鎖上のオブジェクトの [[Prototype]] 値が変化
  • O または連鎖上オブジェクトからプロパティが削除
  • O のプロトタイプ連鎖上のオブジェクトにプロパティが追加
  • O または連鎖上オブジェクトのプロパティの [[Enumerable]] が変化
Note 1

実装は 14.7.5.10.2.1 のアルゴリズムを直接実装する必要はない。上記制約が破られない限り挙動が一致する任意実装を選べる。

以下はこれら規則に従う ECMAScript generator function の参考定義である:

function* EnumerateObjectProperties(obj) {
  const visited = new Set();
  for (const key of Reflect.ownKeys(obj)) {
    if (typeof key === "symbol") continue;
    const desc = Reflect.getOwnPropertyDescriptor(obj, key);
    if (desc) {
      visited.add(key);
      if (desc.enumerable) yield key;
    }
  }
  const proto = Reflect.getPrototypeOf(obj);
  if (proto === null) return;
  for (const protoKey of EnumerateObjectProperties(proto)) {
    if (!visited.has(protoKey)) yield protoKey;
  }
}
Note 2
CreateForInIterator との一致が不要な exotic object のリストは、歴史的に挙動差異があり他については一致していたため選択された。

14.7.5.10 For-In 反復子オブジェクト

For-In Iterator は特定オブジェクト上の特定の列挙を表すオブジェクトである。ECMAScript コードから直接アクセスされず、EnumerateObjectProperties の挙動説明のためのみ存在する。

14.7.5.10.1 CreateForInIterator ( object )

The abstract operation CreateForInIterator takes argument object (an Object) and returns For-In Iterator. object の own および継承された列挙可能な文字列プロパティを特定順序で反復する For-In Iterator オブジェクトを生成する。 It performs the following steps when called:

  1. iteratorOrdinaryObjectCreate(%ForInIteratorPrototype%, « [[Object]], [[ObjectWasVisited]], [[VisitedKeys]], [[RemainingKeys]] ») とする。
  2. iterator.[[Object]]object に設定。
  3. iterator.[[ObjectWasVisited]]false に設定。
  4. iterator.[[VisitedKeys]] を空 List に設定。
  5. iterator.[[RemainingKeys]] を空 List に設定。
  6. iterator を返す。

14.7.5.10.2 %ForInIteratorPrototype% オブジェクト

%ForInIteratorPrototype% オブジェクト:

  • For-In Iterator オブジェクトが継承するプロパティを持つ。
  • 通常のオブジェクトである。
  • [[Prototype]] 内部スロットの値は %Iterator.prototype%
  • ECMAScript コードから直接アクセスされない。
  • 以下のプロパティを持つ。

14.7.5.10.2.1 %ForInIteratorPrototype%.next ( )

  1. Othis 値とする。
  2. Assert: O は Object。
  3. Assert: OFor-In Iterator インスタンスの内部スロットを全て持つ (14.7.5.10.3)。
  4. objectO.[[Object]] とする。
  5. 繰り返し:
    1. O.[[ObjectWasVisited]]false なら:
      1. keys を ? object.[[OwnPropertyKeys]]() とする。
      2. keys の各 key について:
        1. key が String なら:
          1. O.[[RemainingKeys]]key を追加。
      3. O.[[ObjectWasVisited]]true に設定。
    2. O.[[RemainingKeys]] が空でない間の繰り返し:
      1. rO.[[RemainingKeys]] の先頭要素とする。
      2. 先頭要素を削除。
      3. O.[[VisitedKeys]]r を含まないなら:
        1. desc を ? object.[[GetOwnProperty]](r) とする。
        2. descundefined でなければ:
          1. O.[[VisitedKeys]]r を追加。
          2. desc.[[Enumerable]]true なら CreateIteratorResultObject(r, false) を返す。
    3. object を ? object.[[GetPrototypeOf]]() とする。
    4. O.[[Object]]object に設定。
    5. O.[[ObjectWasVisited]]false に設定。
    6. objectnull なら CreateIteratorResultObject(undefined, true) を返す。

14.7.5.10.3 For-In Iterator インスタンスのプロパティ

For-In Iterator インスタンスは %ForInIteratorPrototype% からプロパティを継承する通常オブジェクトであり、Table 36 に列挙する内部スロットで初期化される。

Table 36: For-In Iterator インスタンスの内部スロット
Internal Slot Type 説明 (Description)
[[Object]] an Object プロパティを列挙中のオブジェクト値。
[[ObjectWasVisited]] a Boolean 既に [[OwnPropertyKeys]] を呼び出したなら true、そうでなければ false
[[VisitedKeys]] a List of Strings これまでにこの反復子が出力した値。
[[RemainingKeys]] a List of Strings 現在のオブジェクトで未出力の値(プロトタイプ列挙前。プロトタイプが null でなければ)。

14.8 continue

構文 (Syntax)

ContinueStatement[Yield, Await] : continue ; continue [no LineTerminator here] LabelIdentifier[?Yield, ?Await] ;

14.8.1 静的意味論: 早期エラー

ContinueStatement : continue ; continue LabelIdentifier ;
  • この ContinueStatement が(関数や static 初期化ブロック境界を越えずに)直接または間接的に IterationStatement 内にネストしていないなら構文エラー。

14.8.2 実行時意味論: 評価

ContinueStatement : continue ;
  1. Completion Record { [[Type]]: continue, [[Value]]: empty, [[Target]]: empty } を返す。
ContinueStatement : continue LabelIdentifier ;
  1. labelLabelIdentifier の StringValue とする。
  2. Completion Record { [[Type]]: continue, [[Value]]: empty, [[Target]]: label } を返す。

14.9 break

構文 (Syntax)

BreakStatement[Yield, Await] : break ; break [no LineTerminator here] LabelIdentifier[?Yield, ?Await] ;

14.9.1 静的意味論: 早期エラー

BreakStatement : break ;

14.9.2 実行時意味論: 評価

BreakStatement : break ;
  1. Completion Record { [[Type]]: break, [[Value]]: empty, [[Target]]: empty } を返す。
BreakStatement : break LabelIdentifier ;
  1. labelLabelIdentifier の StringValue とする。
  2. Completion Record { [[Type]]: break, [[Value]]: empty, [[Target]]: label } を返す。

14.10 return

構文 (Syntax)

ReturnStatement[Yield, Await] : return ; return [no LineTerminator here] Expression[+In, ?Yield, ?Await] ; Note

return 文は関数の実行を停止し、通常は呼出し元に値を返す。Expression が省略された場合戻り値は undefined。存在する場合その値を返す。try ブロック内など文脈によっては必ずしも呼出し元に値を返さない(finally で上書きされる等)。

14.10.1 実行時意味論: 評価

ReturnStatement : return ;
  1. ReturnCompletion(undefined) を返す。
ReturnStatement : return Expression ;
  1. exprRefExpression の Evaluation を ? 付きで行った結果とする。
  2. exprValue を ? GetValue(exprRef) とする。
  3. GetGeneratorKind() が async なら exprValue を ? Await(exprValue) に設定。
  4. ReturnCompletion(exprValue) を返す。
Legacy

14.11 with

Note 1

レガシー with 文の使用は新しい ECMAScript コードでは推奨されない。分割代入 など strict / 非 strict 両方で許容される代替を検討せよ。

構文 (Syntax)

WithStatement[Yield, Await, Return] : with ( Expression[+In, ?Yield, ?Await] ) Statement[?Yield, ?Await, ?Return] Note 2

with 文は計算されたオブジェクトの Object Environment Record を実行中コンテキストのレキシカル環境に追加し、この拡張環境で Statement を実行し、その後元の環境を復元する。

14.11.1 静的意味論: 早期エラー

WithStatement : with ( Expression ) Statement
  • IsStrict(this production) が true なら構文エラー。
  • Statement について IsLabelledFunction が true なら構文エラー。
Note

2 番目の規則は B.3.1 の拡張が実装される場合のみ適用。

14.11.2 実行時意味論: 評価

WithStatement : with ( Expression ) Statement
  1. valExpression の Evaluation を ? 付きで行った結果とする。
  2. obj を ? ToObject(? GetValue(val)) とする。
  3. oldEnv を実行中コンテキストの LexicalEnvironment とする。
  4. newEnvNewObjectEnvironment(obj, true, oldEnv) とする。
  5. 実行中コンテキストの LexicalEnvironment を newEnv に設定。
  6. CStatement の Evaluation の Completion とする。
  7. 実行中コンテキストの LexicalEnvironment を oldEnv に戻す。
  8. UpdateEmpty(C, undefined) を返す。
Note

埋め込まれた Statement をどのように抜けても(通常終了・abrupt completion・例外)LexicalEnvironment は常に元に戻る。

14.12 switch

構文 (Syntax)

SwitchStatement[Yield, Await, Return] : switch ( Expression[+In, ?Yield, ?Await] ) CaseBlock[?Yield, ?Await, ?Return] CaseBlock[Yield, Await, Return] : { CaseClauses[?Yield, ?Await, ?Return]opt } { CaseClauses[?Yield, ?Await, ?Return]opt DefaultClause[?Yield, ?Await, ?Return] CaseClauses[?Yield, ?Await, ?Return]opt } CaseClauses[Yield, Await, Return] : CaseClause[?Yield, ?Await, ?Return] CaseClauses[?Yield, ?Await, ?Return] CaseClause[?Yield, ?Await, ?Return] CaseClause[Yield, Await, Return] : case Expression[+In, ?Yield, ?Await] : StatementList[?Yield, ?Await, ?Return]opt DefaultClause[Yield, Await, Return] : default : StatementList[?Yield, ?Await, ?Return]opt

14.12.1 静的意味論: 早期エラー

SwitchStatement : switch ( Expression ) CaseBlock

14.12.2 実行時意味論: CaseBlockEvaluation : ECMAScript 言語値を含む normal completion または abrupt completion

The syntax-directed operation UNKNOWN takes UNPARSEABLE ARGUMENTS. It is defined piecewise over the following productions:

CaseBlock : { }
  1. undefined を返す。
CaseBlock : { CaseClauses }
  1. Vundefined とする。
  2. ACaseClauses 内の CaseClause 項目(ソース順)の List とする。
  3. foundfalse とする。
  4. CaseClause C について:
    1. foundfalse なら:
      1. found を ? CaseClauseIsSelected(C, input) に設定。
    2. foundtrue なら:
      1. RC の Evaluation の Completion とする。
      2. R.[[Value]]empty でなければ VR.[[Value]] に設定。
      3. Rabrupt completion なら ? UpdateEmpty(R, V) を返す。
  5. V を返す。
CaseBlock : { CaseClausesopt DefaultClause CaseClausesopt }
  1. Vundefined とする。
  2. 最初の CaseClauses が存在するなら:
    1. A をその CaseClause 項目(ソース順)List とする。
  3. それ以外:
    1. A を空 List とする。
  4. foundfalse とする。
  5. A の各 CaseClause C について:
    1. foundfalse なら:
      1. found を ? CaseClauseIsSelected(C, input) に設定。
    2. foundtrue なら:
      1. RC の Evaluation の Completion とする。
      2. R.[[Value]]empty でなければ VR.[[Value]] に設定。
      3. Rabrupt completion なら ? UpdateEmpty(R, V) を返す。
  6. foundInBfalse とする。
  7. 二番目の CaseClauses が存在するなら:
    1. B をその CaseClause 項目(ソース順)List とする。
  8. それ以外:
    1. B を空 List とする。
  9. foundfalse なら:
    1. B の各 CaseClause C について:
      1. foundInBfalse なら:
        1. foundInB を ? CaseClauseIsSelected(C, input) に設定。
      2. foundInBtrue なら:
        1. RCaseClause C の Evaluation の Completion とする。
        2. R.[[Value]]empty でなければ VR.[[Value]] に設定。
        3. Rabrupt completion なら ? UpdateEmpty(R, V) を返す。
  10. foundInBtrue なら V を返す。
  11. defaultRDefaultClause の Evaluation の Completion とする。
  12. defaultR.[[Value]]empty でなければ VdefaultR.[[Value]] に設定。
  13. defaultRabrupt completion なら ? UpdateEmpty(defaultR, V) を返す。
  14. 注: ここから二番目の CaseClauses を再度全走査する。
  15. B の各 CaseClause C について:
    1. RCaseClause C の Evaluation の Completion とする。
    2. R.[[Value]]empty でなければ VR.[[Value]] に設定。
    3. Rabrupt completion なら ? UpdateEmpty(R, V) を返す。
  16. V を返す。

14.12.3 CaseClauseIsSelected ( C, input )

The abstract operation CaseClauseIsSelected takes arguments C (a CaseClause Parse Node) and input (an ECMAScript language value) and returns Boolean を含む normal completion または abrupt completion. Cinput にマッチするか判定する。 It performs the following steps when called:

  1. Assert: C CaseClause : case Expression : StatementListopt のインスタンス。
  2. exprRefCExpression の Evaluation を ? 付きで行った結果とする。
  3. clauseSelector を ? GetValue(exprRef) とする。
  4. IsStrictlyEqual(input, clauseSelector) を返す。
Note

この操作は CStatementList を実行しない。CaseBlock アルゴリズムは戻り値によりどの StatementList から実行開始するかを決定する。

14.12.4 実行時意味論: 評価

SwitchStatement : switch ( Expression ) CaseBlock
  1. exprRefExpression の Evaluation を ? 付きで行った結果とする。
  2. switchValue を ? GetValue(exprRef) とする。
  3. oldEnv を現在実行中コンテキストの LexicalEnvironment とする。
  4. blockEnvNewDeclarativeEnvironment(oldEnv) とする。
  5. BlockDeclarationInstantiation(CaseBlock, blockEnv) を実行。
  6. 実行中コンテキストの LexicalEnvironment を blockEnv に設定。
  7. R を CaseBlockEvaluation of CaseBlock (引数 switchValue) の Completion とする。
  8. 実行中コンテキストの LexicalEnvironment を oldEnv に戻す。
  9. R を返す。
Note

SwitchStatement をどのように抜けても LexicalEnvironment は常に元に戻る。

CaseClause : case Expression :
  1. empty を返す。
CaseClause : case Expression : StatementList
  1. StatementList の Evaluation を ? 付きで返す。
DefaultClause : default :
  1. empty を返す。
DefaultClause : default : StatementList
  1. StatementList の Evaluation を ? 付きで返す。

14.13 ラベル付き文 (Labelled Statements)

構文 (Syntax)

LabelledStatement[Yield, Await, Return] : LabelIdentifier[?Yield, ?Await] : LabelledItem[?Yield, ?Await, ?Return] LabelledItem[Yield, Await, Return] : Statement[?Yield, ?Await, ?Return] FunctionDeclaration[?Yield, ?Await, ~Default] Note

Statement はラベルで前置できる。ラベル付き文はラベル付き break / continue と組み合わせてのみ利用され、ECMAScript には goto はない。StatementLabelledStatement の一部となり再帰的にネスト可能。その導入されたラベル集合を「current label set」と呼ぶ。

14.13.1 静的意味論: 早期エラー

LabelledItem : FunctionDeclaration
  • この生成規則にマッチするソーステキストが存在する場合、そのソーステキストが非 strict コードでありホストラベル付き Function 宣言 をサポートする場合を除き構文エラー。

14.13.2 静的意味論: IsLabelledFunction ( stmt: a Statement Parse Node, ): Boolean

The abstract operation UNKNOWN takes UNPARSEABLE ARGUMENTS. It performs the following steps when called:

  1. stmtLabelledStatement でなければ false を返す。
  2. itemstmtLabelledItem とする。
  3. item LabelledItem : FunctionDeclaration なら true を返す。
  4. subStmtitemStatement とする。
  5. IsLabelledFunction(subStmt) を返す。

14.13.3 実行時意味論: 評価

LabelledStatement : LabelIdentifier : LabelledItem
  1. この LabelledStatement の LabelledEvaluation (引数 « ») を ? 付きで返す。

14.13.4 実行時意味論: LabelledEvaluation : ECMAScript 言語値または empty を含む normal completion あるいは abrupt completion

The syntax-directed operation UNKNOWN takes UNPARSEABLE ARGUMENTS. It is defined piecewise over the following productions:

BreakableStatement : IterationStatement
  1. stmtResult を LoopEvaluation of IterationStatement (labelSet) の Completion とする。
  2. stmtResultbreak completion なら:
    1. stmtResult.[[Target]]empty なら:
      1. stmtResult.[[Value]]empty なら stmtResultNormalCompletion(undefined) に設定。
      2. それ以外は NormalCompletion(stmtResult.[[Value]]) に設定。
  3. stmtResult を返す。
BreakableStatement : SwitchStatement
  1. stmtResultSwitchStatement の Evaluation の Completion とする。
  2. stmtResultbreak completion なら:
    1. stmtResult.[[Target]]empty なら:
      1. stmtResult.[[Value]]empty なら stmtResultNormalCompletion(undefined) に、そうでなければ NormalCompletion(stmtResult.[[Value]]) に設定。
  3. stmtResult を返す。
Note 1

BreakableStatement は無ラベル BreakStatement により脱出可能な文である。

LabelledStatement : LabelIdentifier : LabelledItem
  1. labelLabelIdentifier の StringValue とする。
  2. newLabelSetlabelSet と « label » のリスト連結とする。
  3. stmtResultLabelledItem の LabelledEvaluation(newLabelSet) の Completion とする。
  4. stmtResultbreak completion かつ stmtResult.[[Target]]label なら:
    1. stmtResultNormalCompletion(stmtResult.[[Value]]) に設定。
  5. stmtResult を返す。
LabelledItem : FunctionDeclaration
  1. FunctionDeclaration の Evaluation を ? 付きで返す。
Statement : BlockStatement VariableStatement EmptyStatement ExpressionStatement IfStatement ContinueStatement BreakStatement ReturnStatement WithStatement ThrowStatement TryStatement DebuggerStatement
  1. Statement の Evaluation を ? 付きで返す。
Note 2

Statement のうち LabelledEvaluation に特別な意味論を持つのは BreakableStatementLabelledStatement のみ。

14.14 throw

構文 (Syntax)

ThrowStatement[Yield, Await] : throw [no LineTerminator here] Expression[+In, ?Yield, ?Await] ;

14.14.1 実行時意味論: 評価

ThrowStatement : throw Expression ;
  1. exprRefExpression の Evaluation を ? 付きで行った結果とする。
  2. exprValue を ? GetValue(exprRef) とする。
  3. ThrowCompletion(exprValue) を返す。

14.15 try

構文 (Syntax)

TryStatement[Yield, Await, Return] : try Block[?Yield, ?Await, ?Return] Catch[?Yield, ?Await, ?Return] try Block[?Yield, ?Await, ?Return] Finally[?Yield, ?Await, ?Return] try Block[?Yield, ?Await, ?Return] Catch[?Yield, ?Await, ?Return] Finally[?Yield, ?Await, ?Return] Catch[Yield, Await, Return] : catch ( CatchParameter[?Yield, ?Await] ) Block[?Yield, ?Await, ?Return] catch Block[?Yield, ?Await, ?Return] Finally[Yield, Await, Return] : finally Block[?Yield, ?Await, ?Return] CatchParameter[Yield, Await] : BindingIdentifier[?Yield, ?Await] BindingPattern[?Yield, ?Await] Note

try 文は例外(実行時エラーや throw)が発生し得るコードブロックを囲む。catch 節は例外処理コードを提供し、捕捉時その CatchParameter に例外が束縛される。

14.15.1 静的意味論: 早期エラー

Catch : catch ( CatchParameter ) Block

14.15.2 実行時意味論: CatchClauseEvaluation : ECMAScript 言語値または empty を含む normal completion あるいは abrupt completion

The syntax-directed operation UNKNOWN takes UNPARSEABLE ARGUMENTS. It is defined piecewise over the following productions:

Catch : catch ( CatchParameter ) Block
  1. oldEnv を実行中コンテキストの LexicalEnvironment とする。
  2. catchEnvNewDeclarativeEnvironment(oldEnv) とする。
  3. CatchParameter の BoundNames の各 argName について:
    1. catchEnv.CreateMutableBinding(argName, false) を実行。
  4. 実行中コンテキストの LexicalEnvironment を catchEnv に設定。
  5. statusCatchParameter の BindingInitialization(thrownValue, catchEnv) の Completion とする。
  6. statusabrupt completion なら:
    1. LexicalEnvironment を oldEnv に戻す。
    2. status を返す。
  7. BBlock の Evaluation の Completion とする。
  8. LexicalEnvironment を oldEnv に戻す。
  9. B を返す。
Catch : catch Block
  1. Block の Evaluation を ? 付きで返す。
Note

Block をどのように抜けても LexicalEnvironment は常に元に復元される。

14.15.3 実行時意味論: 評価

TryStatement : try Block Catch
  1. BBlock の Evaluation の Completion とする。
  2. Bthrow completion なら C を CatchClauseEvaluation of Catch (B.[[Value]] 引数) の Completion、そうでなければ CB とする。
  3. UpdateEmpty(C, undefined) を返す。
TryStatement : try Block Finally
  1. BBlock の Evaluation の Completion とする。
  2. FFinally の Evaluation の Completion とする。
  3. Fnormal completion なら FB に設定。
  4. UpdateEmpty(F, undefined) を返す。
TryStatement : try Block Catch Finally
  1. BBlock の Evaluation の Completion とする。
  2. Bthrow completion なら C を CatchClauseEvaluation of Catch (B.[[Value]] 引数) の Completion、そうでなければ CB とする。
  3. FFinally の Evaluation の Completion とする。
  4. Fnormal completion なら FC に設定。
  5. UpdateEmpty(F, undefined) を返す。

14.16 debugger

構文 (Syntax)

DebuggerStatement : debugger ;

14.16.1 実行時意味論: 評価

Note

DebuggerStatement の評価は、デバッガ下で実行時にブレークポイントを発生させることを実装に許す。デバッガ非存在または非活性なら可視効果はない。

DebuggerStatement : debugger ;
  1. 実装定義のデバッグ機能が利用可能かつ有効なら:
    1. 実装定義のデバッグ動作を実行。
    2. 新しい実装定義 Completion Record を返す。
  2. それ以外:
    1. empty を返す。