9 実行可能コードと実行コンテキスト (Executable Code and Execution Contexts)
9.1 環境レコード (Environment Records)
Environment Record は、ECMAScript コードの字句上の入れ子(レキシカル)構造に基づいて Identifier と特定の変数・関数との関連付けを定義するために用いられる仕様上の型である。通常、Environment Record は FunctionDeclaration、BlockStatement、または TryStatement の Catch 節といった ECMAScript コードの特定の構文構造に関連付けられる。そのようなコードが評価されるたびに、そのコードが生成する識別子束縛を記録する新しい Environment Record が作成される。
すべての Environment Record は [[OuterEnv]] フィールドを持ち、これは null か、外側の Environment Record への参照である。これは Environment Record 値の論理的な入れ子をモデル化するために使われる。ある(内側の)Environment Record の外側参照は、その内側 Environment Record を論理的に囲む Environment Record への参照である。外側の Environment Record もまた自身の外側 Environment Record を持ちうる。1 つの Environment Record は複数の内側 Environment Record の外側環境として機能することがある。例えば、ある FunctionDeclaration が 2 つのネストした FunctionDeclaration を含む場合、それぞれのネストした関数の Environment Record の外側 Environment Record は、その囲む関数の現在の評価の Environment Record になる。
Environment Record は純粋に仕様上のメカニズムであり、ECMAScript 実装の特定の成果物に対応する必要はない。ECMAScript プログラムがそれらの値に直接アクセスまたは操作することは不可能である。
9.1.1 Environment Record 型階層 (The Environment Record Type Hierarchy)
各 Declarative Environment Record は、変数、定数、let、class、module、import、および / または function 宣言を含む ECMAScript プログラムスコープに関連付けられる。Declarative Environment Record は、そのスコープ内の宣言によって定義される識別子集合を束縛する。
9.1.1.1.1 HasBinding ( N )
The HasBinding concrete method of Declarative Environment RecordenvRec takes argument N (String) and returns Boolean を含む正常完了. 引数の識別子がそのレコードで束縛される識別子の一つかどうかを判定する。 It performs the following steps when called:
envRec が N の束縛を持つなら true を返す。
false を返す。
9.1.1.1.2 CreateMutableBinding ( N, D )
The CreateMutableBinding concrete method of Declarative Environment RecordenvRec takes arguments N (String) and D (Boolean) and returns unused を含む正常完了. 未初期化の新しい可変束縛 N を作成する。envRec に N の既存束縛があってはならない。D が true の場合、その新しい束縛は削除対象としてマークされる。 It performs the following steps when called:
アサート: envRec はまだ N の束縛を持たない。
envRec に N の未初期化可変束縛を作成する。D が true なら、後続の DeleteBinding 呼び出しで削除可能であることを記録する。
unused を返す。
9.1.1.1.3 CreateImmutableBinding ( N, S )
The CreateImmutableBinding concrete method of Declarative Environment RecordenvRec takes arguments N (String) and S (Boolean) and returns unused を含む正常完了. 未初期化の新しい不変束縛 N を作成する。既存束縛は存在してはならない。S が true の場合、新しい束縛を strict 束縛としてマークする。 It performs the following steps when called:
アサート: envRec はまだ N の束縛を持たない。
envRec に N の未初期化不変束縛を作成する。S が true なら strict 束縛であることを記録する。
unused を返す。
9.1.1.1.4 InitializeBinding ( N, V )
The InitializeBinding concrete method of Declarative Environment RecordenvRec takes arguments N (String) and V (ECMAScript 言語値) and returns unused を含む正常完了. 名前 N を持つ識別子の現在の未初期化束縛に値 V を設定するために使用される。未初期化束縛が既に存在しなければならない。 It performs the following steps when called:
アサート: envRec は N の未初期化束縛を持つ。
envRec における N の束縛値を V に設定する。
Record その束縛が初期化されたことを記録する。
unused を返す。
9.1.1.1.5 SetMutableBinding ( N, V, S )
The SetMutableBinding concrete method of Declarative Environment RecordenvRec takes arguments N (String), V (ECMAScript 言語値), and S (Boolean) and returns unused を含む正常完了または throw 完了. 名前 N の可変束縛値を V に変更しようとする。通常束縛は既存だが、まれに存在しない場合がある。束縛が不変束縛で S が true のとき TypeError を投げる。 It performs the following steps when called:
functionf() { eval("var x; x = (delete x, 0);"); }
9.1.1.1.6 GetBindingValue ( N, S )
The GetBindingValue concrete method of Declarative Environment RecordenvRec takes arguments N (String) and S (Boolean) and returns ECMAScript 言語値を含む正常完了または throw 完了. 名前 N の束縛値を返す。束縛が存在し未初期化である場合は、S の値に関わらず ReferenceError を投げる。 It performs the following steps when called:
アサート: envRec は N の束縛を持つ。
envRec における N の束縛が未初期化なら ReferenceError 例外を投げる。
現在 envRec で N に束縛されている値を返す。
9.1.1.1.7 DeleteBinding ( N )
The DeleteBinding concrete method of Declarative Environment RecordenvRec takes argument N (String) and returns Boolean を含む正常完了. 削除対象として明示的に指定された束縛のみ削除できる。 It performs the following steps when called:
アサート: envRec は N の束縛を持つ。
envRec における N の束縛が削除不可なら false を返す。
envRec から N の束縛を除去する。
true を返す。
9.1.1.1.8 HasThisBinding ( )
The HasThisBinding concrete method of Declarative Environment RecordenvRec takes no arguments and returns false. It performs the following steps when called:
The HasSuperBinding concrete method of Declarative Environment RecordenvRec takes no arguments and returns false. It performs the following steps when called:
The WithBaseObject concrete method of Declarative Environment RecordenvRec takes no arguments and returns undefined. It performs the following steps when called:
undefined を返す。
9.1.1.2 オブジェクト環境レコード (Object Environment Records)
各 Object Environment Record は binding object と呼ばれるオブジェクトに関連付けられる。Object Environment Record は、その binding object のプロパティ名と直接対応する文字列識別子名の集合を束縛する。IdentifierName 形式でない文字列でないプロパティキーは束縛された識別子集合に含まれない。自身のプロパティと継承されたプロパティは [[Enumerable]] 属性の設定に関わらず集合に含まれる。プロパティは動的に追加・削除されうるため、Object Environment Record によって束縛される識別子集合は、プロパティを追加または削除する任意の操作の副作用として変化し得る。そのような副作用によって作成された束縛は、対応するプロパティの Writable 属性が false であっても可変束縛とみなされる。Object Environment Record には不変束縛は存在しない。
with 文(14.11)のために作成される Object Environment Record は、関数呼び出しで使用する暗黙の this 値として binding object を提供できる。この能力は Boolean フィールド [[IsWithEnvironment]] によって制御される。
Object Environment Record は Table 15 に列挙される追加状態フィールドを持つ。
The HasBinding concrete method of Object Environment RecordenvRec takes argument N (String) and returns Boolean を含む正常完了または throw 完了. 関連付けられた binding object が名前 N のプロパティを持つか判定する。 It performs the following steps when called:
The CreateMutableBinding concrete method of Object Environment RecordenvRec takes arguments N (String) and D (Boolean) and returns unused を含む正常完了または throw 完了. Environment Record の binding object に名前 N のプロパティを作成し、その値を undefined に初期化する。D が true なら新プロパティの [[Configurable]] を true にし、そうでなければ false にする。 It performs the following steps when called:
bindingObject を envRec.[[BindingObject]] とする。
? DefinePropertyOrThrow(bindingObject, N, PropertyDescriptor { [[Value]]: undefined, [[Writable]]: true, [[Enumerable]]: true, [[Configurable]]: D }) を実行。
unused を返す。
Note
通常 envRec は N の束縛を持たないが、持つ場合には DefinePropertyOrThrow のセマンティクスにより既存束縛が置換・シャドウされるか、または急な完了が返ることがある。
The InitializeBinding concrete method of Object Environment RecordenvRec takes arguments N (String) and V (ECMAScript 言語値) and returns unused を含む正常完了または throw 完了. 名前 N の現在の束縛値を V に設定する。 It performs the following steps when called:
? envRec.SetMutableBinding(N, V, false) を実行。
unused を返す。
Note
本仕様では Object Environment Record に対する CreateMutableBinding の全使用は直後に同じ名前で InitializeBinding を呼び出す。したがって初期化状態は明示的に追跡しない。
9.1.1.2.5 SetMutableBinding ( N, V, S )
The SetMutableBinding concrete method of Object Environment RecordenvRec takes arguments N (String), V (ECMAScript 言語値), and S (Boolean) and returns unused を含む正常完了または throw 完了. 関連する binding object のプロパティ N の値を V に設定しようとする。通常 N のプロパティは既に存在するが、存在しないか書き込み不可の場合、エラー処理は S に依存する。 It performs the following steps when called:
The GetBindingValue concrete method of Object Environment RecordenvRec takes arguments N (String) and S (Boolean) and returns ECMAScript 言語値を含む正常完了または throw 完了. 関連 binding object のプロパティ N の値を返す。プロパティは存在するはずだが存在しない場合、結果は S に依存する。 It performs the following steps when called:
The DeleteBinding concrete method of Object Environment RecordenvRec takes argument N (String) and returns Boolean を含む正常完了または throw 完了. [[Configurable]] 属性が true の環境オブジェクトプロパティに対応する束縛のみ削除できる。 It performs the following steps when called:
bindingObject を envRec.[[BindingObject]] とする。
? bindingObject.[[Delete]](N) を返す。
9.1.1.2.8 HasThisBinding ( )
The HasThisBinding concrete method of Object Environment RecordenvRec takes no arguments and returns false. It performs the following steps when called:
The HasSuperBinding concrete method of Object Environment RecordenvRec takes no arguments and returns false. It performs the following steps when called:
The WithBaseObject concrete method of Object Environment RecordenvRec takes no arguments and returns Object または undefined. It performs the following steps when called:
Function Environment Record は、関数のトップレベルスコープを表し、かつその関数が ArrowFunction でない場合 this 束縛を提供する Declarative Environment Record である。関数が ArrowFunction でなく super を参照する場合、その Function Environment Record は関数内部からの super メソッド呼び出しを実行するために使用される状態も含む。
Function Environment Record は Table 16 に列挙される追加状態フィールドを持つ。
The abstract operation BindThisValue takes arguments envRec (Function Environment Record) and V (ECMAScript 言語値) and returns unused を含む正常完了または throw 完了. envRec.[[ThisValue]] を設定し初期化済みであることを記録する。 It performs the following steps when called:
The HasThisBinding concrete method of Function Environment RecordenvRec takes no arguments and returns Boolean. It performs the following steps when called:
The HasSuperBinding concrete method of Function Environment RecordenvRec takes no arguments and returns Boolean. It performs the following steps when called:
The GetThisBinding concrete method of Function Environment RecordenvRec takes no arguments and returns ECMAScript 言語値を含む正常完了または throw 完了. It performs the following steps when called:
The abstract operation GetSuperBase takes argument envRec (Function Environment Record) and returns Object, null, または undefined. envRec に束縛された super プロパティアクセスの基底オブジェクトを返す。値 undefined はそうしたアクセスが実行時エラーになることを示す。 It performs the following steps when called:
home を envRec.[[FunctionObject]].[[HomeObject]] とする。
home が undefined なら undefined を返す。
アサート: home は ordinary object。
! home.[[GetPrototypeOf]]() を返す。
9.1.1.4 グローバル環境レコード (Global Environment Records)
Global Environment Record は、共通の realm で処理されるすべての ECMAScript Script 要素に共有される最も外側のスコープを表す。Global Environment Record は組み込みグローバル(19)、グローバルオブジェクトのプロパティ、および Script 内に現れるすべてのトップレベル宣言(8.2.9, 8.2.11)に束縛を提供する。
The HasBinding concrete method of Global Environment RecordenvRec takes argument N (String) and returns Boolean を含む正常完了または throw 完了. 引数の識別子がこのレコードで束縛される一つか判定する。 It performs the following steps when called:
DclRec を envRec.[[DeclarativeRecord]] とする。
! DclRec.HasBinding(N) が true なら true を返す。
ObjRec を envRec.[[ObjectRecord]] とする。
? ObjRec.HasBinding(N) を返す。
9.1.1.4.2 CreateMutableBinding ( N, D )
The CreateMutableBinding concrete method of Global Environment RecordenvRec takes arguments N (String) and D (Boolean) and returns unused を含む正常完了または throw 完了. 未初期化の新しい可変束縛 N を作成する。束縛は関連 DeclarativeRecord に作成される。N の束縛は既に存在してはならない。D が true の場合、削除対象としてマークされる。 It performs the following steps when called:
The CreateImmutableBinding concrete method of Global Environment RecordenvRec takes arguments N (String) and S (Boolean) and returns unused を含む正常完了または throw 完了. 未初期化の新しい不変束縛 N を作成する。N の既存束縛は存在してはならない。S が true なら strict 束縛としてマーク。 It performs the following steps when called:
The InitializeBinding concrete method of Global Environment RecordenvRec takes arguments N (String) and V (ECMAScript 言語値) and returns unused を含む正常完了または throw 完了. 名前 N の未初期化束縛値を V に設定する。未初期化束縛が既に存在する必要がある。 It performs the following steps when called:
The SetMutableBinding concrete method of Global Environment RecordenvRec takes arguments N (String), V (ECMAScript 言語値), and S (Boolean) and returns unused を含む正常完了または throw 完了. 名前 N の現在の束縛値を V に変更しようとする。不変束縛で S が true なら TypeError。プロパティ N が存在しないか書き込み不可の場合の処理は S に依存。 It performs the following steps when called:
DclRec を envRec.[[DeclarativeRecord]] とする。
! DclRec.HasBinding(N) が true なら
? DclRec.SetMutableBinding(N, V, S) を返す。
ObjRec を envRec.[[ObjectRecord]] とする。
? ObjRec.SetMutableBinding(N, V, S) を返す。
9.1.1.4.6 GetBindingValue ( N, S )
The GetBindingValue concrete method of Global Environment RecordenvRec takes arguments N (String) and S (Boolean) and returns ECMAScript 言語値を含む正常完了または throw 完了. 名前 N の束縛値を返す。束縛が未初期化なら ReferenceError。プロパティ N の存在や書き込み可否に関連するエラー処理は S に依存。 It performs the following steps when called:
DclRec を envRec.[[DeclarativeRecord]] とする。
! DclRec.HasBinding(N) が true なら
? DclRec.GetBindingValue(N, S) を返す。
ObjRec を envRec.[[ObjectRecord]] とする。
? ObjRec.GetBindingValue(N, S) を返す。
9.1.1.4.7 DeleteBinding ( N )
The DeleteBinding concrete method of Global Environment RecordenvRec takes argument N (String) and returns Boolean を含む正常完了または throw 完了. 削除対象として明示的に指定された束縛のみ削除できる。 It performs the following steps when called:
The HasThisBinding concrete method of Global Environment RecordenvRec takes no arguments and returns true. It performs the following steps when called:
The HasSuperBinding concrete method of Global Environment RecordenvRec takes no arguments and returns false. It performs the following steps when called:
The WithBaseObject concrete method of Global Environment RecordenvRec takes no arguments and returns undefined. It performs the following steps when called:
undefined を返す。
9.1.1.4.11 GetThisBinding ( )
The GetThisBinding concrete method of Global Environment RecordenvRec takes no arguments and returns Object を含む正常完了. It performs the following steps when called:
envRec.[[GlobalThisValue]] を返す。
9.1.1.4.12 HasLexicalDeclaration ( envRec, N )
The abstract operation HasLexicalDeclaration takes arguments envRec (Global Environment Record) and N (String) and returns Boolean. 引数の識別子が、LexicalDeclaration や ClassDeclaration のようなレキシカル宣言で envRec に作成された束縛を持つか判定する。 It performs the following steps when called:
DclRec を envRec.[[DeclarativeRecord]] とする。
! DclRec.HasBinding(N) を返す。
9.1.1.4.13 HasRestrictedGlobalProperty ( envRec, N )
The abstract operation HasRestrictedGlobalProperty takes arguments envRec (Global Environment Record) and N (String) and returns Boolean を含む正常完了または throw 完了. 引数の識別子が、グローバルレキシカル束縛によってシャドウされてはならないグローバルオブジェクトのプロパティ名か判定する。 It performs the following steps when called:
グローバルオブジェクト上には var や function 宣言ではなく直接作成されたプロパティが存在し得る。非 configurable なグローバルオブジェクトのプロパティと同名のグローバルレキシカル束縛は作成できない。"undefined" はその例である。
9.1.1.4.14 CanDeclareGlobalVar ( envRec, N )
The abstract operation CanDeclareGlobalVar takes arguments envRec (Global Environment Record) and N (String) and returns Boolean を含む正常完了または throw 完了. 同じ引数 N で CreateGlobalVarBinding 呼び出しが成功するか判定する。冗長な var 宣言や既存グローバルオブジェクトプロパティに対する var 宣言は許可される。 It performs the following steps when called:
The abstract operation CanDeclareGlobalFunction takes arguments envRec (Global Environment Record) and N (String) and returns Boolean を含む正常完了または throw 完了. 同じ引数 N で CreateGlobalFunctionBinding 呼び出しが成功するか判定する。 It performs the following steps when called:
9.1.1.4.16 CreateGlobalVarBinding ( envRec, N, D )
The abstract operation CreateGlobalVarBinding takes arguments envRec (Global Environment Record), N (String), and D (Boolean) and returns unused を含む正常完了または throw 完了. 関連する Object Environment Record に可変束縛を作成・初期化する。既存束縛があれば再利用され初期化済みとみなされる。 It performs the following steps when called:
9.1.1.4.17 CreateGlobalFunctionBinding ( envRec, N, V, D )
The abstract operation CreateGlobalFunctionBinding takes arguments envRec (Global Environment Record), N (String), V (ECMAScript 言語値), and D (Boolean) and returns unused を含む正常完了または throw 完了. 関連する Object Environment Record に可変束縛を作成・初期化する。既存束縛があれば置き換える。 It performs the following steps when called:
The GetBindingValue concrete method of Module Environment RecordenvRec takes arguments N (String) and S (Boolean) and returns ECMAScript 言語値を含む正常完了または throw 完了. 名前 N の束縛値を返す。ただし束縛が間接束縛なら対象束縛値を返す。束縛が未初期化なら ReferenceError を投げる。 It performs the following steps when called:
The HasThisBinding concrete method of Module Environment RecordenvRec takes no arguments and returns true. It performs the following steps when called:
The GetThisBinding concrete method of Module Environment RecordenvRec takes no arguments and returns undefined を含む正常完了. It performs the following steps when called:
undefined を返す。
9.1.1.5.5 CreateImportBinding ( envRec, N, M, N2 )
The abstract operation CreateImportBinding takes arguments envRec (Module Environment Record), N (String), M (Module Record), and N2 (String) and returns unused. 名前 N の新しい初期化済み不変間接束縛を作成する。envRec に N の束縛は存在してはならない。N2 は M の Module Environment Record に存在する束縛名。新束縛の値へのアクセスは対象束縛値を間接的にアクセスする。 It performs the following steps when called:
The abstract operation GetIdentifierReference takes arguments env (Environment Record または null), name (String), and strict (Boolean) and returns Reference Record を含む正常完了または throw 完了. It performs the following steps when called:
The abstract operation NewObjectEnvironment takes arguments O (Object), W (Boolean), and E (Environment Record または null) and returns Object Environment Record. It performs the following steps when called:
The abstract operation NewFunctionEnvironment takes arguments F (ECMAScript 関数オブジェクト) and newTarget (Object または undefined) and returns Function Environment Record. It performs the following steps when called:
The abstract operation NewGlobalEnvironment takes arguments G (Object) and thisValue (Object) and returns Global Environment Record. It performs the following steps when called:
The abstract operation ResolvePrivateIdentifier takes arguments privateEnv (a PrivateEnvironment Record) and identifier (a String) and returns a Private Name. It performs the following steps when called:
The abstract operation InitializeHostDefinedRealm takes no arguments and returns unused を含む正常完了または throw 完了. It performs the following steps when called:
The abstract operation SetDefaultGlobalBindings takes argument realmRec (a Realm Record) and returns unused を含む正常完了または throw 完了. It performs the following steps when called:
The abstract operation GetActiveScriptOrModule takes no arguments and returns a Script Record, a Module Record, or null. 実行中実行コンテキストに基づき、実行中の script または module を決定するために用いられる。 It performs the following steps when called:
ResolveBinding の結果は常に [[ReferencedName]] フィールドが name である Reference Record である。
9.4.3 GetThisEnvironment ( )
The abstract operation GetThisEnvironment takes no arguments and returns an Environment Record. 現在 this の束縛を提供している Environment Record を見つける。 It performs the following steps when called:
The abstract operation ResolveThisBinding takes no arguments and returns either a normal completion containing an ECMAScript language value or a throw completion. 実行中実行コンテキストの LexicalEnvironment を用いてキーワード this の束縛を決定する。 It performs the following steps when called:
The abstract operation GetNewTarget takes no arguments and returns an Object or undefined. 実行中実行コンテキストの LexicalEnvironment を用いて NewTarget 値を決定する。 It performs the following steps when called:
The abstract operation GetGlobalObject takes no arguments and returns an Object. 現在実行中の実行コンテキストが使用するグローバルオブジェクトを返す。 It performs the following steps when called:
The host-defined abstract operation HostEnqueuePromiseJob takes arguments job (a JobAbstract Closure) and realm (a Realm Record or null) and returns unused. 将来のある時点に job をスケジュールする。このアルゴリズムと共に用いられる Abstract Closure は、Promise の処理に関連するか、さもなくば Promise 処理操作と同等の優先度でスケジュールされることを意図している。
実装は、非 live オブジェクトやシンボルの極大集合に対して WeakRef を必ずしも空にする義務はない。
実装が WeakRef を空にするために非 live 集合 S を選ぶ場合、本定義は S 内のすべての値に対する WeakRef を同時に空にすることを要求する。言い換えると、値 v を指す WeakRef を空にしながら、v の値を観測しうる他の WeakRef を空にしないままにするのは非適合である。
The host-defined abstract operation HostEnqueueFinalizationRegistryCleanupJob takes argument finalizationRegistry (a FinalizationRegistry) and returns unused.
The abstract operation ClearKeptObjects takes no arguments and returns unused. ECMAScript 実装は同期的な ECMAScript 実行列が完了した際に ClearKeptObjects を呼び出すことが期待される。 It performs the following steps when called:
The abstract operation CanBeHeldWeakly takes argument v (an ECMAScript language value) and returns a Boolean. v が弱参照として利用に適している場合に限り true を返す。弱参照として利用に適した値のみが WeakMap のキー、WeakSet の要素、WeakRef の target、あるいは FinalizationRegistry の targets の 1 つになれる。 It performs the following steps when called:
v が Object であるなら true を返す。
v が Symbol でかつ KeyForSymbol(v) が undefined であるなら true を返す。
false を返す。
Note
言語同一性 (language identity) を持たない言語値は、事前の参照なしに生成し得るため弱参照としては不適切である。Symbol.for により生成された Symbol 値は他の Symbol と異なり言語同一性を持たず、弱参照として不適切である。Well-known symbols は収集されない可能性が高いが、数が限定され多様な実装手法で管理可能であるため、弱参照として利用に適したものとして扱われる。しかし、live な WeakMap 内で well-known symbol に関連付けられた任意の値は収集されにくく、実装においてメモリ資源を「リーク」させる可能性がある。