Environment Record 是一种规范类型,用于基于 ECMAScript 代码的词法嵌套结构,定义 Identifier 与特定变量和函数之间的关联。通常,一个 Environment Record 与 ECMAScript 代码的某个特定句法结构相关联,例如 FunctionDeclaration、BlockStatement 或 TryStatement 的 Catch 子句。每当这类代码被求值时,都会创建一个新的 Environment Record,用于记录由该代码创建的标识符绑定。
每个 Environment Record 都有一个 [[OuterEnv]] 字段,它要么是 null,要么是对外层 Environment Record 的引用。这用于建模 Environment Record 值的逻辑嵌套。某个(内层)Environment Record 的外层引用,是对在逻辑上包围该内层 Environment Record 的 Environment Record 的引用。当然,一个外层 Environment Record 可以有自己的外层 Environment Record。一个 Environment Record 可以作为多个内层 Environment Records 的外层环境。例如,如果一个 FunctionDeclaration包含两个嵌套的 FunctionDeclaration,则每个嵌套函数的 Environment Record 都会将包围函数当前求值的 Environment Record 作为其外层 Environment Record。
Environment Records 纯粹是规范机制,并不需要对应于 ECMAScript 实现中的任何特定工件。ECMAScript 程序不可能直接访问或操纵这类值。
The abstract method CreateMutableBinding takes arguments name (a String) and deletable (a Boolean) and returns either a normal completion containing unused or a throw completion.
它在 Environment Record 中创建一个新的、但未初始化的可变绑定。name 是被绑定名称的文本。如果 deletable 是 true,则该绑定随后可以被删除。
The abstract method CreateImmutableBinding takes arguments name (a String) and strict (a Boolean) and returns either a normal completion containing unused or a throw completion.
它在 Environment Record 中创建一个新的、但未初始化的不可变绑定。name 是被绑定名称的文本。如果 strict 是 true,则在它已被初始化之后尝试设置它时,总是会抛出异常,而不管引用该绑定的操作的严格模式设置如何。
The abstract method InitializeBinding takes arguments name (a String) and value (an ECMAScript language value) and returns either a normal completion containing unused or a throw completion.
The abstract method SetMutableBinding takes arguments name (a String), value (an ECMAScript language value), and strict (a Boolean) and returns either a normal completion containing unused or a throw completion.
The abstract method GetBindingValue takes arguments name (a String) and strict (a Boolean) and returns either a normal completion containing an ECMAScript language value or a throw completion.
The abstract method GetThisBinding takes no arguments and returns either a normal completion containing an ECMAScript language value or a throw completion.
每个 Declarative Environment Record 都与一个 ECMAScript 程序作用域相关联,该作用域包含变量、常量、let、class、module、import 和/或 function 声明。Declarative Environment Record 绑定其作用域内包含的声明所定义的一组标识符。
每个声明式环境记录也都有一个 [[DisposableResourceStack]] 字段,其中包含一个 DisposableResource 记录的列表。此列表中的元素由 using 声明和 await using 声明跟踪,并且必须在构造该环境记录的求值步骤完成时被处置。
9.1.1.1.1 HasBinding ( name )
The HasBinding concrete method of a Declarative Environment RecordenvRecord takes argument name (a String) and returns a normal completion containing a Boolean. 它确定实参标识符是否为该记录所绑定的标识符之一。 It performs the following steps when called:
The CreateMutableBinding concrete method of a Declarative Environment RecordenvRecord takes arguments name (a String) and deletable (a Boolean) and returns a normal completion containing unused. 它为名称 name 创建一个新的、未初始化的可变绑定。此 Environment Record 中必须尚不存在 name 的绑定。如果 deletable 是 true,则新绑定会被标记为可被删除。 It performs the following steps when called:
断言:envRecord 尚不具有 name 的绑定。
在 envRecord 中为 name 创建一个可变绑定,并记录它未被初始化。如果 deletable 是 true,则记录新创建的绑定可以由后续的 DeleteBinding 调用删除。
返回 unused。
9.1.1.1.3 CreateImmutableBinding ( name, strict )
The CreateImmutableBinding concrete method of a Declarative Environment RecordenvRecord takes arguments name (a String) and strict (a Boolean) and returns a normal completion containing unused. 它为名称 name 创建一个新的、未初始化的不可变绑定。此 Environment Record 中必须尚不存在 name 的绑定。如果 strict 是 true,则新绑定会被标记为严格绑定。 It performs the following steps when called:
断言:envRecord 尚不具有 name 的绑定。
在 envRecord 中为 name 创建一个不可变绑定,并记录它未被初始化。如果 strict 是 true,则记录新创建的绑定是严格绑定。
返回 unused。
9.1.1.1.4 InitializeBinding ( name, value )
The InitializeBinding concrete method of a Declarative Environment RecordenvRecord takes arguments name (a String) and value (an ECMAScript language value) and returns a normal completion containing unused. 它用于将名称为 name 的标识符的当前绑定的绑定值设置为值 value。name 的未初始化绑定必须已经存在。 It performs the following steps when called:
The SetMutableBinding concrete method of a Declarative Environment RecordenvRecord takes arguments name (a String), value (an ECMAScript language value), and strict (a Boolean) and returns either a normal completion containing unused or a throw completion. 它尝试将名称为 name 的标识符的当前绑定的绑定值更改为值 value。name 的绑定通常已经存在,但在少数情况下可能不存在。如果该绑定是不可变绑定,且 strict 是 true,则会抛出 TypeError。 It performs the following steps when called:
functionf() { eval("var x; x = (delete x, 0);"); }
9.1.1.1.6 GetBindingValue ( name, strict )
The GetBindingValue concrete method of a Declarative Environment RecordenvRecord takes arguments name (a String) and strict (a Boolean) and returns either a normal completion containing an ECMAScript language value or a throw completion. 它返回其名称为 name 的已绑定标识符的值。如果该绑定存在但未初始化,则会抛出 ReferenceError,而不管 strict 的值如何。 It performs the following steps when called:
断言:envRecord 具有 name 的绑定。
如果 envRecord 中 name 的绑定是未初始化绑定,则抛出 ReferenceError 异常。
返回 envRecord 中当前绑定到 name 的值。
9.1.1.1.7 DeleteBinding ( name )
The DeleteBinding concrete method of a Declarative Environment RecordenvRecord takes argument name (a String) and returns a normal completion containing a Boolean. 它只能删除那些已被显式指定为可被删除的绑定。 It performs the following steps when called:
The HasBinding concrete method of an Object Environment RecordenvRecord takes argument name (a String) and returns either a normal completion containing a Boolean or a throw completion. 它确定其关联的绑定对象是否具有名称为 name 的属性。 It performs the following steps when called:
The CreateMutableBinding concrete method of an Object Environment RecordenvRecord takes arguments name (a String) and deletable (a Boolean) and returns either a normal completion containing unused or a throw completion. 它在 Environment Record 的关联绑定对象中创建一个名称为 name 的属性,并将其初始化为值 undefined。如果 deletable 是 true,则新属性的 [[Configurable]] 特性被设置为 true;否则被设置为 false。 It performs the following steps when called:
The InitializeBinding concrete method of an Object Environment RecordenvRecord takes arguments name (a String) and value (an ECMAScript language value) and returns either a normal completion containing unused or a throw completion. 它用于将名称为 name 的标识符的当前绑定的绑定值设置为值 value。 It performs the following steps when called:
The SetMutableBinding concrete method of an Object Environment RecordenvRecord takes arguments name (a String), value (an ECMAScript language value), and strict (a Boolean) and returns either a normal completion containing unused or a throw completion. 它尝试将该 Environment Record 的关联绑定对象中名称为 name 的属性的值设置为值 value。名为 name 的属性通常已经存在,但如果它不存在或当前不可写,则错误处理由 strict 决定。 It performs the following steps when called:
The GetBindingValue concrete method of an Object Environment RecordenvRecord takes arguments name (a String) and strict (a Boolean) and returns either a normal completion containing an ECMAScript language value or a throw completion. 它返回其关联绑定对象中名称为 name 的属性的值。该属性应当已经存在,但如果它不存在,结果取决于 strict。 It performs the following steps when called:
The DeleteBinding concrete method of an Object Environment RecordenvRecord takes argument name (a String) and returns either a normal completion containing a Boolean or a throw completion. 它只能删除对应于环境对象属性且其 [[Configurable]] 特性值为 true 的绑定。 It performs the following steps when called:
令 bindingObj 为 envRecord.[[BindingObject]]。
返回 ? bindingObj.[[Delete]](name)。
9.1.1.2.8 HasThisBinding ( )
The HasThisBinding concrete method of an Object Environment RecordenvRecord takes no arguments and returns false. It performs the following steps when called:
The WithBaseObject concrete method of an Object Environment RecordenvRecord takes no arguments and returns an Object or undefined. It performs the following steps when called:
The abstract operation BindThisValue takes arguments envRecord (a Function Environment Record) and value (an ECMAScript language value) and returns either a normal completion containing unused or a throw completion. 它设置 envRecord.[[ThisValue]],并记录其已被初始化。 It performs the following steps when called:
The GetThisBinding concrete method of a Function Environment RecordenvRecord takes no arguments and returns either a normal completion containing an ECMAScript language value or a throw completion. It performs the following steps when called:
The abstract operation GetSuperBase takes argument envRecord (a Function Environment Record) and returns an Object, null, or undefined. 它返回在 envRecord 中绑定的 super 属性访问的基对象。值 undefined 表示这类访问会产生运行时错误。 It performs the following steps when called:
令 home 为 envRecord.[[FunctionObject]].[[HomeObject]]。
Global Environment Record 用于表示由在共同 realm 中处理的所有 ECMAScript Script 元素共享的最外层作用域。Global Environment Record 提供内置全局(条款 19)、全局对象属性,以及在 Script 内出现的所有顶层声明(8.2.11、8.2.13)的绑定。
The HasBinding concrete method of a Global Environment RecordenvRecord takes argument name (a String) and returns either a normal completion containing a Boolean or a throw completion. 它确定实参标识符是否为该记录所绑定的标识符之一。 It performs the following steps when called:
The CreateMutableBinding concrete method of a Global Environment RecordenvRecord takes arguments name (a String) and deletable (a Boolean) and returns either a normal completion containing unused or a throw completion. 它为名称 name 创建一个新的、未初始化的可变绑定。该绑定创建于关联的 DeclarativeRecord 中。DeclarativeRecord 中必须尚不存在 name 的绑定。如果 deletable 是 true,则新绑定会被标记为可被删除。 It performs the following steps when called:
The CreateImmutableBinding concrete method of a Global Environment RecordenvRecord takes arguments name (a String) and strict (a Boolean) and returns either a normal completion containing unused or a throw completion. 它为名称 name 创建一个新的、未初始化的不可变绑定。此 Environment Record 中必须尚不存在 name 的绑定。如果 strict 是 true,则新绑定会被标记为严格绑定。 It performs the following steps when called:
The InitializeBinding concrete method of a Global Environment RecordenvRecord takes arguments name (a String) and value (an ECMAScript language value) and returns either a normal completion containing unused or a throw completion. 它用于将名称为 name 的标识符的当前绑定的绑定值设置为值 value。name 的未初始化绑定必须已经存在。 It performs the following steps when called:
The SetMutableBinding concrete method of a Global Environment RecordenvRecord takes arguments name (a String), value (an ECMAScript language value), and strict (a Boolean) and returns either a normal completion containing unused or a throw completion. 它尝试将名称为 name 的标识符的当前绑定的绑定值更改为值 value。如果该绑定是不可变绑定且 strict 是 true,则会抛出 TypeError。名为 name 的绑定通常已经存在,但如果它不存在或当前不可写,则错误处理由 strict 决定。 It performs the following steps when called:
The GetBindingValue concrete method of a Global Environment RecordenvRecord takes arguments name (a String) and strict (a Boolean) and returns either a normal completion containing an ECMAScript language value or a throw completion. 它返回其名称为 name 的已绑定标识符的值。如果该绑定是未初始化绑定,则会抛出 ReferenceError 异常。名为 name 的绑定通常已经存在,但如果它不存在或当前不可写,则错误处理由 strict 决定。 It performs the following steps when called:
The DeleteBinding concrete method of a Global Environment RecordenvRecord takes argument name (a String) and returns either a normal completion containing a Boolean or a throw completion. 它只能删除那些已被显式指定为可被删除的绑定。 It performs the following steps when called:
The HasThisBinding concrete method of a Global Environment RecordenvRecord takes no arguments and returns true. It performs the following steps when called:
The GetThisBinding concrete method of a Global Environment RecordenvRecord takes no arguments and returns a normal completion containing an Object. It performs the following steps when called:
The WithBaseObject concrete method of a Global Environment RecordenvRecord takes no arguments and returns undefined. It performs the following steps when called:
返回 undefined。
9.1.1.4.12 HasLexicalDeclaration ( envRecord, name )
The abstract operation HasLexicalDeclaration takes arguments envRecord (a Global Environment Record) and name (a String) and returns a Boolean. 它确定实参标识符是否在 envRecord 中具有通过词法声明(例如 LexicalDeclaration 或 ClassDeclaration)创建的绑定。 It performs the following steps when called:
9.1.1.4.13 HasRestrictedGlobalProperty ( envRecord, name )
The abstract operation HasRestrictedGlobalProperty takes arguments envRecord (a Global Environment Record) and name (a String) and returns either a normal completion containing a Boolean or a throw completion. 它确定实参标识符是否为全局对象的某个属性的名称,而该属性不得被全局词法绑定遮蔽。 It performs the following steps when called:
全局对象上可能存在直接创建而不是使用 var 或 function 声明声明的属性。不得创建与全局对象的不可配置属性具有相同名称的全局词法绑定。全局属性 "undefined" 是这类属性的一个例子。
9.1.1.4.14 CanDeclareGlobalVar ( envRecord, name )
The abstract operation CanDeclareGlobalVar takes arguments envRecord (a Global Environment Record) and name (a String) and returns either a normal completion containing a Boolean or a throw completion. 它确定如果使用相同实参 name 调用对应的 CreateGlobalVarBinding 调用是否会成功。允许冗余的 var 声明和针对预先存在的全局对象属性的 var 声明。 It performs the following steps when called:
9.1.1.4.15 CanDeclareGlobalFunction ( envRecord, name )
The abstract operation CanDeclareGlobalFunction takes arguments envRecord (a Global Environment Record) and name (a String) and returns either a normal completion containing a Boolean or a throw completion. 它确定如果使用相同实参 name 调用对应的 CreateGlobalFunctionBinding 调用是否会成功。 It performs the following steps when called:
The abstract operation CreateGlobalVarBinding takes arguments envRecord (a Global Environment Record), name (a String), and deletable (a Boolean) and returns either a normal completion containing unused or a throw completion. 它在关联的 Object Environment Record 中创建并初始化一个可变绑定。如果绑定已经存在,则重用它,并假定它已被初始化。 It performs the following steps when called:
The abstract operation CreateGlobalFunctionBinding takes arguments envRecord (a Global Environment Record), name (a String), value (an ECMAScript function object), and deletable (a Boolean) and returns either a normal completion containing unused or a throw completion. 它在关联的 Object Environment Record 中创建并初始化一个可变绑定。如果绑定已经存在,则替换它。 It performs the following steps when called:
The GetBindingValue concrete method of a Module Environment RecordenvRecord takes arguments name (a String) and strict (a Boolean) and returns either a normal completion containing an ECMAScript language value or a throw completion. 它返回其名称为 name 的已绑定标识符的值。不过,如果该绑定是间接绑定,则返回目标绑定的值。如果该绑定存在但未初始化,则会抛出 ReferenceError。 It performs the following steps when called:
The HasThisBinding concrete method of a Module Environment RecordenvRecord takes no arguments and returns true. It performs the following steps when called:
The GetThisBinding concrete method of a Module Environment RecordenvRecord takes no arguments and returns a normal completion containing undefined. It performs the following steps when called:
The abstract operation CreateImportBinding takes arguments envRecord (a Module Environment Record), name (a String), targetModule (a Module Record), and targetName (a String) and returns unused. 它为名称 name 创建一个新的、已初始化的不可变间接绑定。envRecord 中必须尚不存在 name 的绑定。targetName 是存在于 targetModule 的 Module Environment Record 中的绑定名称。对新绑定值的访问将间接访问目标绑定的绑定值。 It performs the following steps when called:
The abstract operation GetIdentifierReference takes arguments envRecord (an Environment Record or null), name (a String), and strict (a Boolean) and returns either a normal completion containing a Reference Record or a throw completion. It performs the following steps when called:
The abstract operation NewDeclarativeEnvironment takes argument outerEnv (an Environment Record or null) and returns a Declarative Environment Record. It performs the following steps when called:
The abstract operation NewObjectEnvironment takes arguments obj (an Object), isWithEnv (a Boolean), and outerEnv (an Environment Record or null) and returns an Object Environment Record. It performs the following steps when called:
The abstract operation NewFunctionEnvironment takes arguments func (an ECMAScript function object) and newTarget (an Object or undefined) and returns a Function Environment Record. It performs the following steps when called:
The abstract operation NewGlobalEnvironment takes arguments obj (an Object) and thisValue (an Object) and returns a 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 either a normal completion containing unused or a throw completion. It performs the following steps when called:
The abstract operation SetDefaultGlobalBindings takes argument realmRecord (a Realm Record) and returns either a normal completion containing unused or a throw completion. It performs the following steps when called:
The abstract operation GetActiveScriptOrModule takes no arguments and returns a Script Record, a Module Record, or null. 它用于基于运行中执行上下文来确定运行中的脚本或模块。 It performs the following steps when called:
The abstract operation ResolveBinding takes argument name (a String) and optional argument envRecord (an Environment Record or undefined) and returns either a normal completion containing a Reference Record or a throw completion. 它用于确定 name 的绑定。envRecord 可用于显式提供要搜索该绑定的 Environment Record。 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 RunSuspendedContext takes arguments context (an execution context) and completionRecord (a Completion Record) and returns either a normal completion containing either an ECMAScript language value or unused, or an abrupt completion. 它恢复 context(将 completionRecord 作为恢复值发送给它),并等待结果。 It performs the following steps when called:
令 callerContext 为运行中执行上下文。
暂停 callerContext。
将 context 压入执行上下文栈;context 现在是运行中执行上下文。
使用 completionRecord 作为导致其暂停的操作的结果,恢复 context 的暂停求值。令 result 为恢复的计算返回的 Completion Record。
The abstract operation RunCallerContext takes argument value (一个 ECMAScript 语言值或 empty) and returns 一个完成记录. 它恢复调用方上下文(将 value 作为恢复值发送给它),并等待结果(如果有)。 It performs the following steps when called:
The host-defined abstract operation HostCallJobCallback takes arguments jobCallback (a JobCallback Record), thisValue (an ECMAScript language value), and argList (a List of ECMAScript language values) and returns either a normal completion containing an ECMAScript language value or a throw completion.
The host-defined abstract operation HostEnqueuePromiseJob takes arguments job (a JobAbstract Closure) and realm (a Realm Record or null) and returns unused. 它调度 job 在将来某个时间执行。与此算法一起使用的 Abstract Closures 旨在与 Promises 的处理有关,或者以其他方式按与 Promise 处理操作相同的优先级调度。
在某些环境中,允许给定 agent 暂停可能并不合理。例如,在 Web 浏览器环境中,禁止挂起 document 的主事件处理线程可能是合理的,同时仍允许 workers 的事件处理线程暂停。
9.6.3 IncrementModuleAsyncEvaluationCount ( )
The abstract operation IncrementModuleAsyncEvaluationCount takes no arguments and returns a non-negative integer. It performs the following steps when called:
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 CleanupFinalizationRegistry takes argument finalizationRegistry (a FinalizationRegistry) and returns either a normal completion containing unused or a throw completion. It performs the following steps when called:
The abstract operation CanBeHeldWeakly takes argument arg (an ECMAScript language value) and returns a Boolean. 当且仅当 arg 适合用作弱引用时,它返回 true。只有适合用作弱引用的值,才可以作为 WeakMap 的键、WeakSet 的元素、WeakRef 的目标,或 FinalizationRegistry 的目标之一。 It performs the following steps when called:
如果 arg 是 Object,则返回 true。
如果 arg 是 Symbol 且 KeyForSymbol(arg) 是 undefined,则返回 true。
返回 false。
Note
没有语言身份的语言值可以在没有先前引用的情况下显现,因此不适合用作弱引用。由 Symbol.for 产生的 Symbol 值不同于其他 Symbol 值,它没有语言身份,因此不适合用作弱引用。众所周知符号可能永远不会被回收,但仍被视为适合用作弱引用,因为它们数量有限,因此可由各种实现方法管理。然而,在 live WeakMap 中与众所周知符号关联的任何值都不太可能被回收,并可能在实现中“泄漏”内存资源。