14 ECMAScript 言語: 文と宣言

構文

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 文の意味論

14.1.1 Runtime Semantics: Evaluation

HoistableDeclaration : GeneratorDeclaration AsyncFunctionDeclaration AsyncGeneratorDeclaration
  1. empty を返す。
HoistableDeclaration : FunctionDeclaration
  1. Evaluation of FunctionDeclaration を返す。
BreakableStatement : IterationStatement SwitchStatement
  1. newLabelSet を新しい空の List とする。
  2. newLabelSet を引数として this BreakableStatementLabelledEvaluation を ? 返す。

14.2 Block

構文

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 }

14.2.2 Runtime Semantics: Evaluation

Block : { }
  1. empty を返す。
Block : { StatementList }
  1. oldEnv を、running execution context の LexicalEnvironment とする。
  2. blockEnvNewDeclarativeEnvironment(oldEnv) とす る。
  3. BlockDeclarationInstantiation( StatementList, blockEnv) を実行する。
  4. running execution context の LexicalEnvironment を blockEnv に設定す る。
  5. blockValueCompletion(Evaluation of StatementList) とする。
  6. running execution context の LexicalEnvironment を oldEnv に設定す る。
  7. blockValue を返す。
Note 1

どのように control が Block を離れても、 LexicalEnvironment は常に以前の状態に復元される。

StatementList : StatementList StatementListItem
  1. sl を ? Evaluation of StatementList とする。
  2. sCompletion(Evaluation of StatementListItem) とする。
  3. UpdateEmpty(s, sl) を返す。
Note 2

StatementList の値は、StatementList 内 の最後の値生成 item の値である。たとえば、次の 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 は block の本体に対応する Parse Node である。env は束縛が作成される Environment Record である。

Note

Block または CaseBlock が評価されるとき、新 しい Declarative Environment Record が作成され、 block 内で宣言された各 block scoped variable、 constant、function、または class に対する束縛がそ の Environment Record 内に instantiate され る。

これは呼び出されると次の手順を実行する:

  1. declarationscodeLexicallyScopedDeclarations とする。
  2. privateEnv を、running execution context の PrivateEnvironment とする。
  3. declarations の各要素 decl について、 次を行う
    1. declBoundNames の各要素 dn に ついて、次を行う
      1. IsConstantDeclaration of decltrue なら、
        1. env.CreateImmutableBinding(dn, true) を実行する。
      2. Else,
        1. host が web browser であるか、または ブロックレベル Function Declaration の Web レガシー互換性意味論 をサポートするなら、
          1. env.HasBinding(dn) が false なら、
            1. env.CreateMutableBinding(dn, false) を実行する。
        2. Else,
          1. env.CreateMutableBinding(dn, false) を実行する。
    2. declFunctionDeclarationGeneratorDeclarationAsyncFunctionDeclaration、または AsyncGeneratorDeclaration のいずれか なら、
      1. fndeclBoundNames の唯一 の要素とする。
      2. fo を、env および privateEnv を 引数とする declInstantiateFunctionObject とする。
      3. host が web browser であるか、または ブロックレベル Function Declaration の Web レガシー互換性意味論 をサポートするなら、
        1. env における fn の束縛が未初期化 の束縛なら、
          1. env.InitializeBinding(fn, fo) を実行する。
        2. Else,
          1. Assert: declFunctionDeclaration である。
          2. env.SetMutableBinding(fn, fo, false) を実行する。
      4. Else,
        1. env.InitializeBinding(fn, fo) を実行する。
  4. unused を返す。

14.3 宣言と Variable Statement

14.3.1 Let および Const 宣言

Note

let および const 宣言は、running execution context の LexicalEnvironment に scoped な variable を定義する。variable は、それ を含む Environment Record が instantiate され たときに作成されるが、その variable の LexicalBinding が評価されるまで、いかなる方法でも アクセスされてはならない。Initializer を伴う LexicalBinding によって定義される variable は、 その variable が作成されたときではなく、 LexicalBinding が評価されたときに、 InitializerAssignmentExpression の値を 代入される。let 宣言内の LexicalBindingInitializer を持たない場合、その variable は LexicalBinding が評価されたときに undefined を代入される。

構文

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 Static Semantics: Early Errors

LexicalDeclaration : LetOrConst BindingList ; LexicalBinding : BindingIdentifier Initializeropt

14.3.1.2 Runtime Semantics: Evaluation

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

静的意味論規則により、この形の LexicalBindingconst 宣言内には決して現れな いことが保証される。

LexicalBinding : BindingIdentifier Initializer
  1. bindingIdBindingIdentifierStringValue とする。
  2. lhs を ! ResolveBinding(bindingId) とする。
  3. IsAnonymousFunctionDefinition( Initializer) が true なら、
    1. value を、bindingId を引数とする InitializerNamedEvaluation の ? とする。
  4. Else,
    1. rhs を ? Evaluation of Initializer とする。
    2. value を ? GetValue(rhs) とする。
  5. InitializeReferencedBinding(lhs, value) を実行する。
  6. empty を返す。
LexicalBinding : BindingPattern Initializer
  1. rhs を ? Evaluation of Initializer とする。
  2. value を ? GetValue(rhs) とする。
  3. env を、running execution context の LexicalEnvironment とする。
  4. value および env を引数とする BindingPatternBindingInitialization を ? 返す。

14.3.2 Variable Statement

Note

var statement は、running execution context の VariableEnvironment に scoped な variable を宣言する。var variable は、それを含む Environment Record が instantiate されたときに 作成され、作成時に undefined に初期化される。任意 の VariableEnvironment の scope 内では、共通の BindingIdentifier が複数の VariableDeclaration に現れ得るが、それらの宣言は 集合的にただ 1 つの variable だけを定義する。 Initializer を伴う VariableDeclaration によっ て定義される variable は、その variable が作成され たときではなく、VariableDeclaration が実行された ときに、InitializerAssignmentExpression の値を代入される。

構文

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 Runtime Semantics: Evaluation

VariableStatement : var VariableDeclarationList ;
  1. Evaluation of VariableDeclarationList を実行する。
  2. empty を返す。
VariableDeclarationList : VariableDeclarationList , VariableDeclaration
  1. Evaluation of VariableDeclarationList を実行する。
  2. Evaluation of VariableDeclaration を返す。
VariableDeclaration : BindingIdentifier
  1. empty を返す。
VariableDeclaration : BindingIdentifier Initializer
  1. bindingIdBindingIdentifierStringValue とする。
  2. lhs を ? ResolveBinding(bindingId) とする。
  3. IsAnonymousFunctionDefinition(Initializer) が true なら、
    1. value を、bindingId を引数とする InitializerNamedEvaluation の ? とする。
  4. Else,
    1. rhs を ? Evaluation of Initializer とする。
    2. value を ? GetValue(rhs) とする。
  5. PutValue(lhs, value) を実行する。
  6. empty を返す。
Note

VariableDeclaration が with statement の内部に入れ子になっており、かつその VariableDeclaration 内の BindingIdentifier が with statement の Object Environment Record の binding object の property 名と同じである場合、step 5 は、Identifier の VariableEnvironment 束縛に代入する代わりに、その property に value を代入する。

VariableDeclaration : BindingPattern Initializer
  1. rhs を ? Evaluation of Initializer とする。
  2. rVal を ? GetValue(rhs) とする。
  3. rVal および undefined を引数とする BindingPatternBindingInitialization を ? 返す。

14.3.3 Destructuring Binding Pattern

構文

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 Runtime Semantics: PropertyBindingInitialization

The syntax-directed operation PropertyBindingInitialization takes arguments value (an ECMAScript language value) and environment (an Environment Record or undefined) and returns either a normal completion containing a List of property keys or an abrupt completion. これは、束縛されたすべての property 名の list を収集する。 It is defined piecewise over the following productions:

BindingPropertyList : BindingPropertyList , BindingProperty
  1. boundNames を、value および environment を引数とする BindingPropertyListPropertyBindingInitialization の ? とす る。
  2. nextNames を、value および environment を引数とする BindingPropertyPropertyBindingInitialization の ? とす る。
  3. boundNamesnextNameslist-concatenation を返す。
BindingProperty : SingleNameBinding
  1. nameSingleNameBindingBoundNames の唯一の要素とする。
  2. value, environment, および name を 引数として SingleNameBindingKeyedBindingInitialization を ? 実行す る。
  3. « name » を返す。
BindingProperty : PropertyName : BindingElement
  1. propertyKey を ? Evaluation of PropertyName とする。
  2. value, environment, および propertyKey を引数として BindingElementKeyedBindingInitialization を ? 実行す る。
  3. « propertyKey » を返す。

14.3.3.2 Runtime Semantics: RestBindingInitialization

The syntax-directed operation RestBindingInitialization takes arguments value (an ECMAScript language value), environment (an Environment Record or undefined), and excludedNames (a List of property keys) and returns either a normal completion containing unused or an abrupt completion. 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 Runtime Semantics: KeyedBindingInitialization

The syntax-directed operation KeyedBindingInitialization takes arguments value (an ECMAScript language value), environment (an Environment Record or undefined), and propertyName (a property key) and returns either a normal completion containing unused or an abrupt completion.

Note

undefinedenvironment として渡されると き、それは初期化値の代入に PutValue 操作を使用すべき ことを示す。これは non-strict function の仮引数 list の場合である。その場合、同名の複数 parameter の可能性に対処するため、仮引数束縛はあらかじめ初期化され る。

It is defined piecewise over the following productions:

BindingElement : BindingPattern Initializeropt
  1. v を ? GetV(value, propertyName) とする。
  2. Initializer が存在し、かつ vundefined なら、
    1. defaultValue を ? Evaluation of Initializer とする。
    2. v を ? GetValue(defaultValue) に設 定する。
  3. v および environment を引数とする BindingPatternBindingInitialization を ? 返す。
SingleNameBinding : BindingIdentifier Initializeropt
  1. bindingIdBindingIdentifierStringValue とする。
  2. lhs を ? ResolveBinding(bindingId, environment) とする。
  3. v を ? GetV(value, propertyName) とする。
  4. Initializer が存在し、かつ vundefined なら、
    1. IsAnonymousFunctionDefinition( Initializer) が true なら、
      1. v を、bindingId を引数とする InitializerNamedEvaluation の ? に設定する。
    2. Else,
      1. defaultValue を ? Evaluation of Initializer とする。
      2. v を ? GetValue(defaultValue) に設定する。
  5. environmentundefined なら、 ? PutValue(lhs, v) を返す。
  6. InitializeReferencedBinding(lhs, v) を返す。

14.4 Empty Statement

構文

EmptyStatement : ;

14.4.1 Runtime Semantics: Evaluation

EmptyStatement : ;
  1. empty を返す。

14.5 Expression Statement

構文

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

ExpressionStatement は U+007B (LEFT CURLY BRACKET) で始まってはならない。なぜなら、それにより Block と曖昧になる可能性があるからである。 ExpressionStatementfunction または class keyword で始まってはならない。なぜなら、 それにより FunctionDeclarationGeneratorDeclaration、または ClassDeclaration と曖昧になるからである。 ExpressionStatementasync function で始 まってはならない。なぜなら、それにより AsyncFunctionDeclaration または AsyncGeneratorDeclaration と曖昧になるからであ る。ExpressionStatement は 2 token 列 let [ で始まってはならない。なぜなら、それにより最 初の LexicalBindingArrayBindingPattern である let LexicalDeclaration と曖昧になるか らである。

14.5.1 Runtime Semantics: Evaluation

ExpressionStatement : Expression ;
  1. exprRef を ? Evaluation of Expression とする。
  2. GetValue(exprRef) を返す。

14.6 if Statement

構文

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] という lookahead-restriction は、古典的な “dangling else” 問題を通常の方法で解決する。すなわち、関連付け られる if の選択が他の点では曖昧である場合、else は候補となる if のうち最も近い(最も内側の)ものに 関連付けられる。

14.6.1 Static Semantics: Early Errors

IfStatement : if ( Expression ) Statement else Statement IfStatement : if ( Expression ) Statement Note

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

14.6.2 Runtime Semantics: Evaluation

IfStatement : if ( Expression ) Statement else Statement
  1. exprRef を ? Evaluation of Expression とする。
  2. exprValueToBoolean(? GetValue(exprRef)) とする。
  3. exprValuetrue なら、
    1. stmtCompletion を、最初の StatementEvaluationCompletion とする。
  4. Else,
    1. stmtCompletion を、2 番目の StatementEvaluationCompletion とする。
  5. UpdateEmpty(stmtCompletion, undefined) を返す。
IfStatement : if ( Expression ) Statement
  1. exprRef を ? Evaluation of Expression とする。
  2. exprValueToBoolean(? GetValue(exprRef)) とする。
  3. exprValuefalse なら、 undefined を返す。
  4. stmtCompletion を、StatementEvaluationCompletion とする。
  5. UpdateEmpty(stmtCompletion, undefined) を返す。

14.7 Iteration Statement

構文

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

14.7.1 意味論

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 部分の 内部では、新しい iteration を開始するために ContinueStatement を使用してよい。

14.7.1.2 Runtime Semantics: LoopEvaluation

The syntax-directed operation LoopEvaluation takes argument labelSet (a List of Strings) and returns either a normal completion containing an ECMAScript language value or an abrupt completion. It is defined piecewise over the following productions:

IterationStatement : DoWhileStatement
  1. labelSet を引数とする DoWhileStatementDoWhileLoopEvaluation を ? 返す。
IterationStatement : WhileStatement
  1. labelSet を引数とする WhileStatementWhileLoopEvaluation を ? 返す。
IterationStatement : ForStatement
  1. labelSet を引数とする ForStatementForLoopEvaluation を ? 返す。
IterationStatement : ForInOfStatement
  1. labelSet を引数とする ForInOfStatementForInOfLoopEvaluation を ? 返す。

14.7.2 do-while Statement

構文

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

14.7.2.1 Static Semantics: Early Errors

DoWhileStatement : do Statement while ( Expression ) ; Note

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

14.7.2.2 Runtime Semantics: DoWhileLoopEvaluation

The syntax-directed operation DoWhileLoopEvaluation takes argument labelSet (a List of Strings) and returns either a normal completion containing an ECMAScript language value or an abrupt completion. It is defined piecewise over the following productions:

DoWhileStatement : do Statement while ( Expression ) ;
  1. iterationResultundefined とす る。
  2. 繰り返す
    1. stmtResultCompletion(Evaluation of Statement) とする。
    2. LoopContinues(stmtResult, labelSet) が false なら、 ? UpdateEmpty(stmtResult, iterationResult) を返す。
    3. stmtResult.[[Value]]empty で ないなら、iterationResultstmtResult.[[Value]] に設定する。
    4. exprRef を ? Evaluation of Expression とする。
    5. exprValue を ? GetValue(exprRef) とする。
    6. ToBoolean(exprValue) が false なら、iterationResult を返す。

14.7.3 while Statement

構文

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

14.7.3.1 Static Semantics: Early Errors

WhileStatement : while ( Expression ) Statement Note

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

14.7.3.2 Runtime Semantics: WhileLoopEvaluation

The syntax-directed operation WhileLoopEvaluation takes argument labelSet (a List of Strings) and returns either a normal completion containing an ECMAScript language value or an abrupt completion. It is defined piecewise over the following productions:

WhileStatement : while ( Expression ) Statement
  1. iterationResultundefined とす る。
  2. 繰り返す
    1. exprRef を ? Evaluation of Expression とする。
    2. exprValue を ? GetValue(exprRef) とする。
    3. ToBoolean(exprValue) が false なら、iterationResult を返す。
    4. stmtResultCompletion(Evaluation of Statement) とする。
    5. LoopContinues(stmtResult, labelSet) が false なら、 ? UpdateEmpty(stmtResult, iterationResult) を返す。
    6. stmtResult.[[Value]]empty で ないなら、iterationResultstmtResult.[[Value]] に設定する。

14.7.4 for Statement

構文

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 Static Semantics: Early Errors

ForStatement : for ( Expressionopt ; Expressionopt ; Expressionopt ) Statement for ( var VariableDeclarationList ; Expressionopt ; Expressionopt ) Statement for ( LexicalDeclaration Expressionopt ; Expressionopt ) Statement Note

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

ForStatement : for ( LexicalDeclaration Expressionopt ; Expressionopt ) Statement

14.7.4.2 Runtime Semantics: ForLoopEvaluation

The syntax-directed operation ForLoopEvaluation takes argument labelSet (a List of Strings) and returns either a normal completion containing an ECMAScript language value or an abrupt completion. It is defined piecewise over the following productions:

ForStatement : for ( Expressionopt ; Expressionopt ; Expressionopt ) Statement
  1. 最初の Expression が存在するなら、
    1. exprRef を、最初の ExpressionEvaluation の ? とする。
    2. GetValue(exprRef) を実行する。
  2. 2 番目の Expression が存在するなら、 test を 2 番目の Expression とする; そうでなければ testempty とす る。
  3. 3 番目の Expression が存在するなら、 increment を 3 番目の Expression と する; そうでなければ incrementempty とする。
  4. test, increment, Statement, « », および labelSet を引数とする ForBodyEvaluation を ? 返す。
ForStatement : for ( var VariableDeclarationList ; Expressionopt ; Expressionopt ) Statement
  1. Evaluation of VariableDeclarationList を 実行する。
  2. 最初の Expression が存在するなら、 test を最初の Expression とする; そ うでなければ testempty とす る。
  3. 2 番目の Expression が存在するなら、 increment を 2 番目の Expression と する; そうでなければ incrementempty とする。
  4. test, increment, Statement, « », および labelSet を引数とする ForBodyEvaluation を ? 返す。
ForStatement : for ( LexicalDeclaration Expressionopt ; Expressionopt ) Statement
  1. oldEnv を、running execution context の LexicalEnvironment とす る。
  2. loopEnvNewDeclarativeEnvironment(oldEnv) と する。
  3. isConstLexicalDeclarationIsConstantDeclaration とする。
  4. boundNamesLexicalDeclarationBoundNames とする。
  5. boundNames の各要素 dn について、 次を行う
    1. isConsttrue なら、
      1. loopEnv.CreateImmutableBinding( dn, true) を実行する。
    2. Else,
      1. loopEnv.CreateMutableBinding(dn, false) を実行する。
  6. running execution context の LexicalEnvironment を loopEnv に設定 する。
  7. forDclCompletion(Evaluation of LexicalDeclaration) とする。
  8. forDclabrupt completion な ら、
    1. running execution context の LexicalEnvironment を oldEnv に設 定する。
    2. forDcl を返す。
  9. isConstfalse なら、 perIterationLetsboundNames と する; そうでなければ perIterationLets を新しい空の List とする。
  10. 最初の Expression が存在するなら、 test を最初の Expression とする; そ うでなければ testempty とす る。
  11. 2 番目の Expression が存在するなら、 increment を 2 番目の Expression と する; そうでなければ incrementempty とする。
  12. bodyResult を、test, increment, Statement, perIterationLets, およ び labelSet を引数とする ForBodyEvaluationCompletion とす る。
  13. running execution context の 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 either a normal completion containing an ECMAScript language value or an abrupt completion. It performs the following steps when called:

  1. iterationResultundefined とす る。
  2. CreatePerIterationEnvironment( perIterationBindings) を実行する。
  3. 繰り返す
    1. testempty でないなら、
      1. testRef を ? Evaluation of test とする。
      2. testValue を ? GetValue(testRef) とする。
      3. ToBoolean(testValue) が false なら、iterationResult を返す。
    2. resultCompletion(Evaluation of stmt) とする。
    3. LoopContinues(result, labelSet) が false なら、 ? UpdateEmpty(result, iterationResult) を返す。
    4. result.[[Value]]empty でない なら、iterationResultresult.[[Value]] に設定する。
    5. CreatePerIterationEnvironment( perIterationBindings) を実行する。
    6. incrementempty でないなら、
      1. incRef を ? Evaluation of increment とする。
      2. GetValue(incRef) を実行する。

14.7.4.4 CreatePerIterationEnvironment ( perIterationBindings )

The abstract operation CreatePerIterationEnvironment takes argument perIterationBindings (a List of Strings) and returns either a normal completion containing unused or a throw completion. It performs the following steps when called:

  1. perIterationBindings が何らかの要素を持つ なら、
    1. lastIterationEnv を、running execution context の 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. running execution context の LexicalEnvironment を thisIterationEnv に設定する。
  2. unused を返す。

14.7.5 for-in, for-of, および for-await-of Statement

構文

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

この節は、Annex B.3.5 によって拡張される。

14.7.5.1 Static Semantics: Early Errors

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 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

14.7.5.2 Static Semantics: IsDestructuring

The syntax-directed operation IsDestructuring takes no arguments and returns a Boolean. 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. ForBindingIsDestructuring を返す。
ForBinding : BindingIdentifier
  1. false を返す。
ForBinding : BindingPattern
  1. true を返す。
Note

この節は、Annex B.3.5 によって拡張される。

14.7.5.3 Runtime Semantics: ForDeclarationBindingInitialization

The syntax-directed operation ForDeclarationBindingInitialization takes arguments value (an ECMAScript language value) and environment (an Environment Record) and returns either a normal completion containing unused or an abrupt completion. It is defined piecewise over the following productions:

ForDeclaration : LetOrConst ForBinding
  1. value および environment を引数とす る ForBindingBindingInitialization を ? 返す。

14.7.5.4 Runtime Semantics: ForDeclarationBindingInstantiation

The syntax-directed operation ForDeclarationBindingInstantiation takes argument environment (a Declarative Environment Record) and returns unused. It is defined piecewise over the following productions:

ForDeclaration : LetOrConst ForBinding
  1. ForBindingBoundNames の各要素 name について、次を行う
    1. LetOrConstIsConstantDeclarationtrue なら、
      1. environment.CreateImmutableBinding( name, true) を実行する。
    2. Else,
      1. environment.CreateMutableBinding( name, false) を実行する。
  2. unused を返す。

14.7.5.5 Runtime Semantics: ForInOfLoopEvaluation

The syntax-directed operation ForInOfLoopEvaluation takes argument labelSet (a List of Strings) and returns either a normal completion containing an ECMAScript language value or an abrupt completion. 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

この節は、Annex 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 either a normal completion containing an Iterator Record or an abrupt completion. It performs the following steps when called:

  1. oldEnv を、running execution context の LexicalEnvironment とす る。
  2. uninitializedBoundNames が空でないな ら、
    1. Assert: uninitializedBoundNames は重複する項目を持たない。
    2. newEnvNewDeclarativeEnvironment(oldEnv) とする。
    3. uninitializedBoundNames の各 String name について、次を行う
      1. newEnv.CreateMutableBinding(name, false) を実行する。
    4. running execution context の LexicalEnvironment を newEnv に設 定する。
  3. exprRefCompletion(Evaluation of expr) とする。
  4. running execution context の 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. Assert: iterationKinditerate または async-iterate のい ずれかである。
  8. iterationKindasync-iterate なら、iteratorKindasync とす る。
  9. Else, iteratorKindsync とす る。
  10. 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 either a normal completion containing an ECMAScript language value or an abrupt completion. It performs the following steps when called:

  1. iteratorKind が存在しないなら、 iteratorKindsync に設定する。
  2. oldEnv を、running execution context の LexicalEnvironment とす る。
  3. iterationResultundefined とす る。
  4. destructuringlhsIsDestructuring とする。
  5. destructuringtrue であり、 かつ lhsKindassignment なら、
    1. Assert: lhsLeftHandSideExpression である。
    2. assignmentPattern を、lhs によっ て cover される AssignmentPattern とする。
  6. 繰り返す
    1. nextResult を ? Call(iteratorRecord.[[NextMethod]], iteratorRecord.[[Iterator]]) とす る。
    2. iteratorKindasync なら、 nextResult を ? Await(nextResult) に設定する。
    3. nextResult が Object でなければ、 TypeError 例外を送出する。
    4. done を ? IteratorComplete( nextResult) とする。
    5. donetrue なら、 iterationResult を返す。
    6. nextValue を ? IteratorValue( nextResult) とする。
    7. lhsKindassignment または var-binding のいずれかなら、
      1. destructuringtrue なら、
        1. lhsKindassignment なら、
          1. status を、nextValue を引数 とする assignmentPatternDestructuringAssignmentEvaluationCompletion とする。
        2. Else,
          1. Assert: lhsKindvar-binding である。
          2. Assert: lhsForBinding である。
          3. status を、nextValue およ び undefined を引数とする lhsBindingInitializationCompletion とする。
      2. Else,
        1. lhsRefCompletion(Evaluation of lhs) とする。(それは繰り返し評 価され得る。)
        2. lhsKindassignment であ り、かつ lhsAssignmentTargetTypeweb-compat なら、 ReferenceError 例外を送出す る。
        3. lhsRefabrupt completion なら、
          1. statuslhsRef とする。
        4. Else,
          1. statusCompletion( PutValue(lhsRef.[[Value]], nextValue)) とする。
    8. Else,
      1. Assert: lhsKindlexical-binding である。
      2. Assert: lhsForDeclaration である。
      3. iterationEnvNewDeclarativeEnvironment(oldEnv) とする。
      4. iterationEnv を引数として lhsForDeclarationBindingInstantiation を実行する。
      5. running execution context の LexicalEnvironment を iterationEnv に設定する。
      6. destructuringtrue なら、
        1. status を、nextValue および iterationEnv を引数とする lhsForDeclarationBindingInitializationCompletion とする。
      7. Else,
        1. Assert: lhs は単一の名前を束縛す る。
        2. lhsNamelhsBoundNames の唯一の要素とする。
        3. lhsRef を ! ResolveBinding( lhsName) とする。
        4. statusCompletion( InitializeReferencedBinding(lhsRef, nextValue)) とする。
    9. statusabrupt completion な ら、
      1. running execution context の LexicalEnvironment を oldEnv に 設定する。
      2. iterationKindenumerate なら、? status を返す。
      3. Assert: iterationKinditerate である。
      4. iteratorKindasync なら、 ? AsyncIteratorClose(iteratorRecord, status) を返す。
      5. IteratorClose(iteratorRecord, status) を返す。
    10. resultCompletion(Evaluation of stmt) とする。
    11. running execution context の LexicalEnvironment を oldEnv に設 定する。
    12. LoopContinues(result, labelSet) が false なら、
      1. statusCompletion( UpdateEmpty(result, iterationResult)) に設定する。
      2. iterationKindenumerate なら、? status を返す。
      3. Assert: iterationKinditerate である。
      4. iteratorKindasync なら、 ? AsyncIteratorClose(iteratorRecord, status) を返す。
      5. IteratorClose(iteratorRecord, status) を返す。
    13. result.[[Value]]empty でない なら、iterationResultresult.[[Value]] に設定する。

14.7.5.8 Runtime Semantics: Evaluation

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

14.7.5.9 EnumerateObjectProperties ( obj )

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

  1. obj の enumerable property のすべての String 値 key を反復する next method を持つ iterator object を返 す。この iterator object は ECMAScript code から直接アクセス可能ではない。 property を列挙する仕組みおよび順序は規定さ れないが、以下で規定される規則には適合しなければな らない。

iteratorthrow および return method は null であり、決して呼び出されない。 iteratornext method は、property keyiterator 値として返すべきかどうかを決定するため に object property を処理する。返される property key には Symbol である key は含まれない。target object の property は列挙中に削除され得る。 iteratornext method によって処理される前に 削除された property は無視される。列挙中に target object に新しい property が追加された場合、新たに 追加された property が進行中の列挙で処理されることは 保証されない。任意の列挙において、property 名は iteratornext method によって高々 1 回し か返されない。

target object の property の列挙には、その prototype の property、その prototype の prototype の property、というように再帰的に含ま れる; ただし、prototype の property は、それがす でに iteratornext method によって処理済み の property と同じ名前を持つ場合には処理されない。 prototype object の property がすでに処理済み かどうかを判定する際には、[[Enumerable]] 属性値は考慮 されない。prototype object の enumerable property 名は、その prototype object を引数とし て渡して EnumerateObjectProperties を起動するこ とにより取得されなければならない。 EnumerateObjectProperties は、target object の own property key を、その [[OwnPropertyKeys]] 内部メソッドを呼び出すことにより取得しなければならない。 target object の property 属性は、その [[GetOwnProperty]] 内部メソッドを呼び出すことによ り取得しなければならない。

さらに、obj も、その prototype chain 上 のいずれの object も、Proxy 特殊オブジェクトTypedArray、module namespace 特殊オブジェク ト、または implementation により提供される特殊オブ ジェクトでない場合、iterator は以下のいずれかが起こ るまで、CreateForInIterator(obj) によって与えら れる iterator と同様に振る舞わなければならない:

  • obj またはその prototype chain 上の いずれかの object の [[Prototype]] internal slot の値が変化する、
  • obj またはその prototype chain 上の いずれかの object から property が削除される、
  • obj の prototype chain 上の object に property が追加される、または
  • obj またはその prototype chain 上の いずれかの object の property の [[Enumerable]] 属性の値が変化する。
Note 1

ECMAScript 実装は、 14.7.5.10.2.1 にある algorithm を直接実装することを要求されない。 前段落の制約のいずれかが破られない限り、その algorithm から挙動が逸脱しない任意の実装を選んでよ い。

以下は、これらの規則に適合する 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
implementation が CreateForInIterator に一 致することを要求されない特殊オブジェクトの list は、実 装が歴史的にそれらの事例について挙動が異なっており、その 他すべてでは一致していたために選ばれた。

14.7.5.10 For-In Iterator Object

For-In Iterator とは、ある特定の object に対する特定の iteration を表す object である。For-In Iterator object は ECMAScript code から直接アクセス可能ではなく、 EnumerateObjectProperties の挙動を説明するためだ けに存在する。

14.7.5.10.1 CreateForInIterator ( object )

The abstract operation CreateForInIterator takes argument object (an Object) and returns a For-In Iterator. これは、object の own および継承された enumerable string property を特定の順序で反復 する For-In Iterator object を作成するために用いら れる。 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% Object

%ForInIteratorPrototype% object は:

  • すべての For-In Iterator object に よって継承される property を持つ。
  • 通常 object である。
  • [[Prototype]] internal slot を持ち、 その値は %Iterator.prototype% である。
  • ECMAScript code から直接アクセス可能では ない。
  • 次の property を持つ:

14.7.5.10.2.1 %ForInIteratorPrototype%.next ( )

  1. iterthis 値とする。
  2. Assert: iter は Object である。
  3. Assert: iter は、For-In Iterator instance のすべての internal slot を持つ (14.7.5.10.3)。
  4. objectiter.[[Object]] とする。
  5. 繰り返す
    1. iter.[[ObjectWasVisited]]false なら、
      1. keys を ? object.[[OwnPropertyKeys]]() とする。
      2. keys の各要素 key について、次を 行う
        1. key が String なら、
          1. keyiter.[[RemainingKeys]] に append する。
      3. iter.[[ObjectWasVisited]]true に設定する。
    2. iter.[[RemainingKeys]] が空でな い間、繰り返す
      1. keyiter.[[RemainingKeys]] の最初 の要素とする。
      2. 最初の要素を iter.[[RemainingKeys]] から除 去する。
      3. iter.[[VisitedKeys]]key を 含まないなら、
        1. desc を ? object.[[GetOwnProperty]](key) とする。
        2. descundefined でなけれ ば、
          1. keyiter.[[VisitedKeys]] に append する。
          2. desc.[[Enumerable]]true なら、 CreateIteratorResultObject( key, false) を返す。
    3. object を ? object.[[GetPrototypeOf]]() に設定する。
    4. iter.[[Object]]object に設定 する。
    5. iter.[[ObjectWasVisited]]false に設定する。
    6. objectnull なら、 CreateIteratorResultObject( undefined, true) を返す。

14.7.5.10.3 For-In Iterator Instance の Property

For-In Iterator instance は、%ForInIteratorPrototype% intrinsic object から property を継承する通 常 object である。For-In Iterator instance は、 初期状態で Table 33 に列挙された internal slot を持つように作成され る。

Table 33: Internal Slots of For-In Iterator Instances
Internal Slot 説明
[[Object]] an Object その property が反復されている Object 値。
[[ObjectWasVisited]] a Boolean iterator[[Object]] に対して [[OwnPropertyKeys]] を呼び出した場合 は true、それ以外は false
[[VisitedKeys]] a List of Strings これまでにこの iterator によって emit された値。
[[RemainingKeys]] a List of Strings prototype の property を反復する前 に(その prototype が null でなけ れば)、現在の object に対してまだ emit されていない値。

14.8 continue Statement

構文

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

14.8.1 Static Semantics: Early Errors

ContinueStatement : continue ; continue LabelIdentifier ;
  • この ContinueStatement が、 IterationStatement の内部に、直接または間 接に(ただし function または static initialization block の境界はまたがず に)入れ子になっていない場合、それは Syntax Error である。

14.8.2 Runtime Semantics: Evaluation

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

14.9 break Statement

構文

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

14.9.1 Static Semantics: Early Errors

BreakStatement : break ;
  • この BreakStatement が、 IterationStatement または SwitchStatement の内部に、直接または間接 に(ただし function または static initialization block の境界はまたがず に)入れ子になっていない場合、それは Syntax Error である。

14.9.2 Runtime Semantics: Evaluation

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

14.10 return Statement

構文

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

return statement は function の実行を 停止させ、ほとんどの場合、呼出し元へ値を返す。 Expression が省略された場合、返値は undefined である。そうでなければ、返値は Expression の値であ る。return statement は、周囲の文脈によっては実際 には呼出し元へ値を返さないことがある。たとえば try block では、return statement の Completion Record は、finally block の評価中に別の Completion Record に置き換えられ得る。

14.10.1 Runtime Semantics: Evaluation

ReturnStatement : return ;
  1. ReturnCompletion(undefined) を返す。
ReturnStatement : return Expression ;
  1. exprRef を ? Evaluation of Expression とする。
  2. exprValue を ? GetValue(exprRef) とする。
  3. GetGeneratorKind() が async なら、 exprValue を ? Await(exprValue) に設 定する。
  4. ReturnCompletion(exprValue) を返す。

14.11 with Statement

Note 1

Legacywith statement の使用は、新し い ECMAScript code では推奨されない。strict mode codenon-strict code の両方で許可され る代替手段、たとえば destructuring assignment を検討せよ。

構文

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

with statement は、計算された object に 対する Object Environment Record を、running execution context の lexical environment に 追加する。その後、この拡張された lexical environment を用いて statement を実行する。最後に、元の lexical environment を復元する。

14.11.1 Static Semantics: Early Errors

WithStatement : with ( Expression ) Statement Note

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

14.11.2 Runtime Semantics: Evaluation

WithStatement : with ( Expression ) Statement
  1. val を ? Evaluation of Expression とする。
  2. obj を ? ToObject(? GetValue(val)) とする。
  3. oldEnv を、running execution context の LexicalEnvironment とす る。
  4. newEnvNewObjectEnvironment(obj, true, oldEnv) とする。
  5. running execution context の LexicalEnvironment を newEnv に設定 する。
  6. stmtCompletionCompletion(Evaluation of Statement) とする。
  7. running execution context の LexicalEnvironment を oldEnv に設定 する。
  8. UpdateEmpty(stmtCompletion, undefined) を返す。
Note

embedded された Statement を control がどのように離れても、通常であれ、ある種の abrupt completion または exception によるものであれ、 LexicalEnvironment は常に以前の状態に復元される。

14.12 switch Statement

構文

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 Static Semantics: Early Errors

SwitchStatement : switch ( Expression ) CaseBlock

14.12.2 Runtime Semantics: CaseBlockEvaluation

The syntax-directed operation CaseBlockEvaluation takes argument input (an ECMAScript language value) and returns either a normal completion containing an ECMAScript language value or an abrupt completion. It is defined piecewise over the following productions:

CaseBlock : { }
  1. undefined を返す。
CaseBlock : { CaseClauses }
  1. resultValueundefined とする。
  2. caseClauses を、source text 順の CaseClauses 内の CaseClause item の List とする。
  3. foundfalse とする。
  4. caseClauses の各 CaseClause clause について、次を行う
    1. foundfalse なら、
      1. found を ? CaseClauseIsSelected(clause, input) に設定する。
    2. foundtrue なら、
      1. completionCompletion(Evaluation of clause) とする。
      2. completion.[[Value]]empty でないなら、resultValuecompletion.[[Value]] に設定する。
      3. completionabrupt completion なら、 ? UpdateEmpty(completion, resultValue) を返す。
  5. resultValue を返す。
CaseBlock : { CaseClausesopt DefaultClause CaseClausesopt }
  1. resultValueundefined とする。
  2. 最初の CaseClauses が存在するなら、
    1. caseClauses を、source text 順の最 初の CaseClauses 内の CaseClause item の List とする。
  3. Else,
    1. caseClauses を新しい空の List とする。
  4. foundfalse とする。
  5. caseClauses の各 CaseClause clause について、次を行う
    1. foundfalse なら、
      1. found を ? CaseClauseIsSelected(clause, input) に設定する。
    2. foundtrue なら、
      1. completionCompletion(Evaluation of clause) とする。
      2. completion.[[Value]]empty でないなら、resultValuecompletion.[[Value]] に設定する。
      3. completionabrupt completion なら、 ? UpdateEmpty(completion, resultValue) を返す。
  6. foundInBfalse とする。
  7. 2 番目の CaseClauses が存在するなら、
    1. secondCaseClauses を、source text 順の 2 番目の CaseClauses 内の CaseClause item の List とする。
  8. Else,
    1. secondCaseClauses を新しい空の List とする。
  9. foundfalse なら、
    1. secondCaseClauses の各 CaseClause clause について、次を行う
      1. foundInBfalse なら、
        1. foundInB を ? CaseClauseIsSelected(clause, input) に設定する。
      2. foundInBtrue なら、
        1. completionCompletion( Evaluation of CaseClause clause) とする。
        2. completion.[[Value]]empty でないなら、resultValuecompletion.[[Value]] に設定する。
        3. completionabrupt completion なら、 ? UpdateEmpty(completion, resultValue) を返す。
  10. foundInBtrue なら、 resultValue を返す。
  11. defaultRCompletion(Evaluation of DefaultClause) とする。
  12. defaultR.[[Value]]empty でない なら、resultValuedefaultR.[[Value]] に設定する。
  13. defaultRabrupt completion なら、 ? UpdateEmpty(defaultR, resultValue) を返す。
  14. NOTE: 以下は、2 番目の CaseClauses の もう 1 回の完全な iteration である。
  15. secondCaseClauses の各 CaseClause clause について、次を行う
    1. completionCompletion(Evaluation of CaseClause clause) とする。
    2. completion.[[Value]]empty で ないなら、resultValuecompletion.[[Value]] に設定する。
    3. completionabrupt completion なら、 ? UpdateEmpty(completion, resultValue) を返す。
  16. resultValue を返す。

14.12.3 CaseClauseIsSelected ( constructor, input )

The abstract operation CaseClauseIsSelected takes arguments constructor (a CaseClause Parse Node) and input (an ECMAScript language value) and returns either a normal completion containing a Boolean or an abrupt completion. これは、constructorinput に一致するかど うかを判定する。 It performs the following steps when called:

  1. Assert: constructor は production CaseClause : case Expression : StatementListopt の instance である。
  2. exprRefconstructorExpressionEvaluation の ? とす る。
  3. clauseSelector を ? GetValue(exprRef) とする。
  4. IsStrictlyEqual(input, clauseSelector) を返す。
Note

この操作は constructorStatementList (もしあれば)を実行しない。CaseBlock algorithm は、その返値を用いてどの StatementList の実行を開始するかを決定する。

14.12.4 Runtime Semantics: Evaluation

SwitchStatement : switch ( Expression ) CaseBlock
  1. exprRef を ? Evaluation of Expression とする。
  2. switchValue を ? GetValue(exprRef) とする。
  3. oldEnv を、running execution context の LexicalEnvironment とす る。
  4. blockEnvNewDeclarativeEnvironment(oldEnv) と する。
  5. BlockDeclarationInstantiation( CaseBlock, blockEnv) を実行する。
  6. running execution context の LexicalEnvironment を blockEnv に設定 する。
  7. blockResult を、switchValue を引数とす る CaseBlockCaseBlockEvaluationCompletion とする。
  8. running execution context の LexicalEnvironment を oldEnv に設定 する。
  9. blockResult を返す。
Note

どのように control が SwitchStatement を離れても、LexicalEnvironment は常に以前の状態 に復元される。

CaseClause : case Expression :
  1. empty を返す。
CaseClause : case Expression : StatementList
  1. Evaluation of StatementList を返す。
DefaultClause : default :
  1. empty を返す。
DefaultClause : default : StatementList
  1. Evaluation of StatementList を返す。

14.13 Labelled Statement

構文

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 には label を前置してよい。 label 付き statement は、label 付き break お よび continue statement と組み合わせてのみ使 用される。ECMAScript には goto statement はな い。StatementLabelledStatement の一部とな り得て、それ自体がさらに LabelledStatement の一部 となり得る。このように導入された label は、個々の statement の意味論を記述するとき、まとめて “current label set” と呼ばれる。

14.13.1 Static Semantics: Early Errors

LabelledItem : FunctionDeclaration
  • 任意の source text がこの production に 一致する場合、それは Syntax Error である 。ただし、その source text が non-strict code であ り、かつ host が web browser であるか、 または ラベル付き Function Declaration をサポートしている場合を除く

14.13.2 Static Semantics: IsLabelledFunction ( stmt )

The abstract operation IsLabelledFunction takes argument stmt (a Statement Parse Node) and returns a Boolean. 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 Runtime Semantics: Evaluation

LabelledStatement : LabelIdentifier : LabelledItem
  1. « » を引数として this LabelledStatementLabelledEvaluation を ? 返す。

14.13.4 Runtime Semantics: LabelledEvaluation

The syntax-directed operation LabelledEvaluation takes argument labelSet (a List of Strings) and returns either a normal completion containing either an ECMAScript language value or empty, or an abrupt completion. It is defined piecewise over the following productions:

BreakableStatement : IterationStatement
  1. stmtResult を、labelSet を引数とする IterationStatementLoopEvaluationCompletion とする。
  2. stmtResultbreak completion な ら、
    1. stmtResult.[[Target]]empty な ら、
      1. stmtResult.[[Value]]empty なら、stmtResultNormalCompletion(undefined) に設 定する。
      2. Else, stmtResultNormalCompletion(stmtResult.[[Value]]) に設定する。
  3. stmtResult を返す。
BreakableStatement : SwitchStatement
  1. stmtResultCompletion(Evaluation of SwitchStatement) とする。
  2. stmtResultbreak completion な ら、
    1. stmtResult.[[Target]]empty な ら、
      1. stmtResult.[[Value]]empty なら、stmtResultNormalCompletion(undefined) に設 定する。
      2. Else, stmtResultNormalCompletion(stmtResult.[[Value]]) に設定する。
  3. stmtResult を返す。
Note 1

BreakableStatement とは、label のない BreakStatement によって脱出できるものである。

LabelledStatement : LabelIdentifier : LabelledItem
  1. labelLabelIdentifierStringValue とする。
  2. newLabelSetlabelSet と « label » の list-concatenation とする。
  3. stmtResult を、newLabelSet を引数とす る LabelledItemLabelledEvaluationCompletion とする。
  4. stmtResultbreak completion で あり、かつ stmtResult.[[Target]]label なら、
    1. stmtResultNormalCompletion(stmtResult.[[Value]]) に設定する。
  5. stmtResult を返す。
LabelledItem : FunctionDeclaration
  1. Evaluation of FunctionDeclaration を返 す。
Statement : BlockStatement VariableStatement EmptyStatement ExpressionStatement IfStatement ContinueStatement BreakStatement ReturnStatement WithStatement ThrowStatement TryStatement DebuggerStatement
  1. Evaluation of Statement を返す。
Note 2

Statement の生成規則のうち、 LabelledEvaluation に対して特別な意味論を持つのは BreakableStatementLabelledStatement だ けである。

14.14 throw Statement

構文

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

14.14.1 Runtime Semantics: Evaluation

ThrowStatement : throw Expression ;
  1. exprRef を ? Evaluation of Expression とする。
  2. exprValue を ? GetValue(exprRef) とする。
  3. exprValue を throw する。

14.15 try Statement

構文

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 statement は、runtime error や throw statement のような例外的条件が発生し得る code block を囲む。catch clause は例外処理 code を提供する。catch clause が exception を捕 捉するとき、その CatchParameter はその exception に束縛される。

14.15.1 Static Semantics: Early Errors

Catch : catch ( CatchParameter ) Block

14.15.2 Runtime Semantics: CatchClauseEvaluation

The syntax-directed operation CatchClauseEvaluation takes argument thrownValue (an ECMAScript language value) and returns either a normal completion containing either an ECMAScript language value or empty, or an abrupt completion. It is defined piecewise over the following productions:

Catch : catch ( CatchParameter ) Block
  1. oldEnv を、running execution context の LexicalEnvironment とす る。
  2. catchEnvNewDeclarativeEnvironment(oldEnv) と する。
  3. CatchParameterBoundNames の各要素 argName について、次を行う
    1. catchEnv.CreateMutableBinding( argName, false) を実行する。
  4. running execution context の LexicalEnvironment を catchEnv に設定 する。
  5. status を、thrownValue および catchEnv を引数とする CatchParameterBindingInitializationCompletion とする。
  6. statusabrupt completion な ら、
    1. running execution context の LexicalEnvironment を oldEnv に設定 する。
    2. status を返す。
  7. blockCompletionCompletion( Evaluation of Block) とする。
  8. running execution context の LexicalEnvironment を oldEnv に設定 する。
  9. blockCompletion を返す。
Catch : catch Block
  1. Evaluation of Block を返す。
Note

どのように control が Block を離れても、 LexicalEnvironment は常に以前の状態に復元される。

14.15.3 Runtime Semantics: Evaluation

TryStatement : try Block Catch
  1. blockResultCompletion(Evaluation of Block) とする。
  2. blockResultthrow completion な ら、catchResult を、blockResult.[[Value]] を引数とする CatchCatchClauseEvaluationCompletion と する。
  3. Else, catchResultblockResult とする。
  4. UpdateEmpty(catchResult, undefined) を返す。
TryStatement : try Block Finally
  1. blockResultCompletion(Evaluation of Block) とする。
  2. finallyResultCompletion(Evaluation of Finally) とする。
  3. finallyResultnormal completion なら、finallyResultblockResult に設定する。
  4. UpdateEmpty(finallyResult, undefined) を返す。
TryStatement : try Block Catch Finally
  1. blockResultCompletion(Evaluation of Block) とする。
  2. blockResultthrow completion な ら、catchResult を、blockResult.[[Value]] を引数とする CatchCatchClauseEvaluationCompletion と する。
  3. Else, catchResultblockResult とする。
  4. finallyResultCompletion(Evaluation of Finally) とする。
  5. finallyResultnormal completion なら、finallyResultcatchResult に設定する。
  6. UpdateEmpty(finallyResult, undefined) を返す。

14.16 debugger Statement

構文

DebuggerStatement : debugger ;

14.16.1 Runtime Semantics: Evaluation

Note

DebuggerStatement の評価は、debugger の 下で実行されているときに implementation が breakpoint を発生させることを可能にし得る。 debugger が存在しないか active でない場合、この statement は観測可能な効果を持たない。

DebuggerStatement : debugger ;
  1. implementation-defined な debugging facility が利用可能かつ有効なら、
    1. implementation-defined な debugging action を実行する。
    2. 新しい implementation-definedCompletion Record を返す。
  2. empty を返す。