9 可执行代码与执行上下文 (Executable Code and Execution Contexts)
9.1 环境记录 (Environment Records)
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 的外层环境。例如,若一个 FunctionDeclaration 内含两个嵌套的 FunctionDeclaration,那么这两个嵌套函数的 Environment Record 的外层 Environment Record 都是外层函数当前求值对应的 Environment Record。
Environment Record 纯属规范机制,不需要对应到 ECMAScript 实现中的任何具体产物。ECMAScript 程序无法直接访问或操纵这些值。
9.1.1 Environment Record 类型层次 (The Environment Record Type Hierarchy)
每个 Declarative Environment Record 与一个 ECMAScript 程序作用域关联,该作用域包含变量、常量、let、class、module、import 以及(或)函数声明。Declarative Environment Record 绑定其作用域内声明定义的标识符集合。
9.1.1.1.1 HasBinding ( N )
The HasBinding concrete method of 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 takes arguments N (一个 String) and D (一个 Boolean) and returns 一个包含 unused 的正常完成. 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 takes arguments N (一个 String) and S (一个 Boolean) and returns 一个包含 unused 的正常完成. It performs the following steps when called:
断言:envRec 尚无 N 绑定。
在 envRec 中创建 N 的不可变绑定并记录其未初始化。若 S 为 true,记录其为严格绑定。
返回 unused。
9.1.1.1.4 InitializeBinding ( N, V )
The InitializeBinding concrete method of takes arguments N (一个 String) and V (一个 ECMAScript 语言值) and returns 一个包含 unused 的正常完成. It performs the following steps when called:
断言:envRec 必须有 N 的未初始化绑定。
将 envRec 中 N 的绑定值设为 V。
记录envRec 中 N 的绑定已被初始化。
返回 unused。
9.1.1.1.5 SetMutableBinding ( N, V, S )
The SetMutableBinding concrete method of takes arguments N (一个 String), V (一个 ECMAScript 语言值), and S (一个 Boolean) and returns 一个包含 unused 的正常完成或一个抛出完成. 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 takes arguments N (一个 String) and S (一个 Boolean) and returns 一个包含 ECMAScript 语言值的正常完成或一个抛出完成. It performs the following steps when called:
断言:envRec 有 N 的绑定。
若其为未初始化绑定,抛 ReferenceError。
返回当前绑定于 N 的值。
9.1.1.1.7 DeleteBinding ( N )
The DeleteBinding concrete method of takes argument N (一个 String) and returns 一个包含 Boolean 的正常完成. It performs the following steps when called:
断言:envRec 有 N 的绑定。
若该绑定不可删除,返回 false。
从 envRec 移除该绑定。
返回 true。
9.1.1.1.8 HasThisBinding ( )
The HasThisBinding concrete method of takes no arguments and returns false. It performs the following steps when called:
The CreateMutableBinding concrete method of takes arguments N (一个 String) and D (一个 Boolean) and returns 一个包含 unused 的正常完成或抛出完成. It performs the following steps when called:
令 bindingObject 为 envRec.[[BindingObject]]。
执行 ? DefinePropertyOrThrow(bindingObject, N, PropertyDescriptor { [[Value]]: undefined, [[Writable]]: true, [[Enumerable]]: true, [[Configurable]]: D })。
The InitializeBinding concrete method of takes arguments N (一个 String) and V (一个 ECMAScript 语言值) and returns 一个包含 unused 的正常完成或抛出完成. It performs the following steps when called:
The SetMutableBinding concrete method of takes arguments N (一个 String), V (一个 ECMAScript 语言值), and S (一个 Boolean) and returns 一个包含 unused 的正常完成或抛出完成. It performs the following steps when called:
The GetBindingValue concrete method of takes arguments N (一个 String) and S (一个 Boolean) and returns 一个包含 ECMAScript 语言值的正常完成或抛出完成. It performs the following steps when called:
The abstract operation BindThisValue takes arguments envRec (一个 Function Environment Record) and V (一个 ECMAScript 语言值) and returns 一个包含 unused 的正常完成或抛出完成. It performs the following steps when called:
The abstract operation GetSuperBase takes argument envRec (一个 Function Environment Record) and returns 一个 Object、null 或 undefined. It performs the following steps when called:
令 home 为 envRec.[[FunctionObject]].[[HomeObject]]。
The HasBinding concrete method of takes argument N (一个 String) and returns 一个包含 Boolean 的正常完成或抛出完成. 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 takes arguments N (一个 String) and D (一个 Boolean) and returns 一个包含 unused 的正常完成或抛出完成. It performs the following steps when called:
令 DclRec 为 envRec.[[DeclarativeRecord]]。
若 ! DclRec.HasBinding(N) 为 true,抛 TypeError。
返回 ! DclRec.CreateMutableBinding(N, D)。
9.1.1.4.3 CreateImmutableBinding ( N, S )
The CreateImmutableBinding concrete method of takes arguments N (一个 String) and S (一个 Boolean) and returns 一个包含 unused 的正常完成或抛出完成. It performs the following steps when called:
令 DclRec 为 envRec.[[DeclarativeRecord]]。
若 ! DclRec.HasBinding(N) 为 true,抛 TypeError。
返回 ! DclRec.CreateImmutableBinding(N, S)。
9.1.1.4.4 InitializeBinding ( N, V )
The InitializeBinding concrete method of takes arguments N (一个 String) and V (一个 ECMAScript 语言值) and returns 一个包含 unused 的正常完成或抛出完成. It performs the following steps when called:
The SetMutableBinding concrete method of takes arguments N (一个 String), V (一个 ECMAScript 语言值), and S (一个 Boolean) and returns 一个包含 unused 的正常完成或抛出完成. 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 takes arguments N (一个 String) and S (一个 Boolean) and returns 一个包含 ECMAScript 语言值的正常完成或抛出完成. 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 takes argument N (一个 String) and returns 一个包含 Boolean 的正常完成或抛出完成. It performs the following steps when called:
The WithBaseObject concrete method of 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 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. 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 的正常完成或抛出完成. It performs the following steps when called:
The abstract operation CanDeclareGlobalVar takes arguments envRec (一个 Global Environment Record) and N (一个 String) and returns 一个包含 Boolean 的正常完成或抛出完成. It performs the following steps when called:
The abstract operation CanDeclareGlobalFunction takes arguments envRec (一个 Global Environment Record) and N (一个 String) and returns 一个包含 Boolean 的正常完成或抛出完成. 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 的正常完成或抛出完成. 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 的正常完成或抛出完成. It performs the following steps when called:
The GetBindingValue concrete method of takes arguments N (一个 String) and S (一个 Boolean) and returns 一个包含 ECMAScript 语言值的正常完成或抛出完成. It performs the following steps when called:
The GetThisBinding concrete method of 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. 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 的正常完成或抛出完成. 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 either a normal completion containing unused or a throw completion. It performs the following steps when called:
The abstract operation SetDefaultGlobalBindings takes argument realmRec (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 env (an Environment Record or undefined) and returns either a normal completion containing a Reference Record or a throw completion. 用于确定 name 的绑定。env 可显式提供要搜索绑定的 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 host-defined abstract operation HostCallJobCallback takes arguments jobCallback (a JobCallback Record), V (an ECMAScript language value), and argumentsList (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 Job Abstract Closure) and realm (a Realm Record or null) and returns unused. 安排 job 于未来某个时间执行。与该算法一起使用的抽象闭包意在与 Promise 处理相关,或以与 Promise 处理操作同等优先级被调度。
对于一组对象和/或 symbol S,相对于 S 的一次 假设的 WeakRef-oblivious(弱引用无感) 执行是指:任何将其 referent 属于 S 的 WeakRef 的抽象操作WeakRefDeref 总是返回 undefined 的执行。
Note 1
WeakRef-obliviousness 与 liveness 一起捕获两个概念:其一,WeakRef 本身不会保持其 referent 存活;其二,存活性中的循环不意味着某值存活。具体地,若判定 v 的存活性依赖判定某 WeakRef referent r 的存活性,则 r 的存活性不能假定 v 的存活性,否则是循环论证。
Note 2
WeakRef-obliviousness 针对对象或 symbol 的集合而不是单个值定义,以处理循环。若对单个值定义,则一个循环中的 WeakRef referent 会被视为存活,即便其身份仅通过循环中其它 WeakRef referent 被观察。
Note 3
口语化地,若包含某个对象或 symbol 的每个集合都是存活的,则我们说该对象或 symbol 存活。
在求值任意时刻,若下列任一条件成立,则对象和/或 symbol 集合 S 被视为 live(存活):
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 v (an ECMAScript language value) and returns a Boolean. 当且仅当 v 适合作为弱引用使用时返回 true。只有适合作为弱引用使用的值才能成为 WeakMap 的键、WeakSet 的元素、WeakRef 的 target,或 FinalizationRegistry 的某个 target。 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 很可能永远不会被回收,但由于其数量有限,仍被视为适合用作弱引用,可通过多种实现手段管理。然而,在一个存活的 WeakMap 中与某个 well-known symbol 关联的任何值都不太可能被回收,可能在实现中“泄漏”内存资源。