9 Executable Code and Execution Contexts
9.1 Environment Records
Environment Record is a specification
Every Environment Record has an [[OuterEnv]] field, which is either
Environment Records are purely specification mechanisms and need not correspond to any specific artefact of an ECMAScript implementation. It is impossible for an ECMAScript program to directly access or manipulate such values.
9.1.1 The Environment Record Type Hierarchy
Environment Records can be thought of as existing in a simple object-oriented hierarchy where
-
Environment Record (abstract)-
A
declarative Environment Record is used to define the effect of ECMAScript language syntactic elements such asFunctionDeclaration s,VariableDeclaration s, andCatch clauses that directly associate identifier bindings with ECMAScript language values.-
A
function Environment Record corresponds to the invocation of an ECMAScriptfunction object , and contains bindings for the top-level declarations within that function. It may establish a newthisbinding. It also captures the state necessary to supportsupermethod invocations. -
A
module Environment Record contains the bindings for the top-level declarations of aModule . It also contains the bindings that are explicitly imported by theModule . Its [[OuterEnv]] is aglobal Environment Record .
-
-
An
object Environment Record is used to define the effect of ECMAScript elements such asWithStatement that associate identifier bindings with the properties of some object. -
A
global Environment Record is used forScript global declarations. It does not have an outer environment; its [[OuterEnv]] isnull . It may be prepopulated with identifier bindings and it includes an associatedglobal object whose properties provide some of the global environment's identifier bindings. As ECMAScript code is executed, additional properties may be added to theglobal object and the initial properties may be modified.
-
The
| Method | Purpose |
|---|---|
| HasBinding(N) |
Determine if an |
| CreateMutableBinding(N, D) |
Create a new but uninitialized mutable binding in an |
| CreateImmutableBinding(N, S) |
Create a new but uninitialized immutable binding in an |
| InitializeBinding(N, V) |
|
| SetMutableBinding(N, V, S) |
|
| GetBindingValue(N, S) |
Returns the value of an already existing binding from an |
| DeleteBinding(N) |
Delete a binding from an |
| HasThisBinding() |
Determine if an this binding. Return |
| HasSuperBinding() |
Determine if an super method binding. Return |
| WithBaseObject() |
If this with statement, return the with object. Otherwise, return |
9.1.1.1 Declarative Environment Records
Each declarative Environment Record is associated with an ECMAScript program scope containing variable, constant, let, class, module, import, and/or function declarations. A declarative Environment Record binds the set of identifiers defined by the declarations contained within its scope.
The behaviour of the concrete specification methods for declarative Environment Records is defined by the following algorithms.
9.1.1.1.1 HasBinding ( N )
The HasBinding concrete method of a
- If envRec has a binding for the name that is the value of N, return
true . - Return
false .
9.1.1.1.2 CreateMutableBinding ( N, D )
The CreateMutableBinding concrete method of a
Assert : envRec does not already have a binding for N.- Create a mutable binding in envRec for N and record that it is uninitialized. If D is
true , record that the newly created binding may be deleted by a subsequent DeleteBinding call. - Return
NormalCompletion (empty ).
9.1.1.1.3 CreateImmutableBinding ( N, S )
The CreateImmutableBinding concrete method of a
Assert : envRec does not already have a binding for N.- Create an immutable binding in envRec for N and record that it is uninitialized. If S is
true , record that the newly created binding is a strict binding. - Return
NormalCompletion (empty ).
9.1.1.1.4 InitializeBinding ( N, V )
The InitializeBinding concrete method of a
Assert : envRec must have an uninitialized binding for N.Set the bound value for N in envRec to V.Record that the binding for N in envRec has been initialized.- Return
NormalCompletion (empty ).
9.1.1.1.5 SetMutableBinding ( N, V, S )
The SetMutableBinding concrete method of a
- If envRec does not have a binding for N, then
- If S is
true , throw aReferenceError exception. - Perform envRec.CreateMutableBinding(N,
true ). - Perform envRec.InitializeBinding(N, V).
- Return
NormalCompletion (empty ).
- If S is
- If the binding for N in envRec is a strict binding, set S to
true . - If the binding for N in envRec has not yet been initialized, throw a
ReferenceError exception. - Else if the binding for N in envRec is a mutable binding, change its bound value to V.
- Else,
Assert : This is an attempt to change the value of an immutable binding.- If S is
true , throw aTypeError exception.
- Return
NormalCompletion (empty ).
An example of ECMAScript code that results in a missing binding at step
function f() { eval("var x; x = (delete x, 0);"); }
9.1.1.1.6 GetBindingValue ( N, S )
The GetBindingValue concrete method of a
Assert : envRec has a binding for N.- If the binding for N in envRec is an uninitialized binding, throw a
ReferenceError exception. - Return the value currently bound to N in envRec.
9.1.1.1.7 DeleteBinding ( N )
The DeleteBinding concrete method of a
Assert : envRec has a binding for the name that is the value of N.- If the binding for N in envRec cannot be deleted, return
false . - Remove the binding for N from envRec.
- Return
true .
9.1.1.1.8 HasThisBinding ( )
The HasThisBinding concrete method of a
- Return
false .
A regular this binding.
9.1.1.1.9 HasSuperBinding ( )
The HasSuperBinding concrete method of a
- Return
false .
A regular super binding.
9.1.1.1.10 WithBaseObject ( )
The WithBaseObject concrete method of a
- Return
undefined .
9.1.1.2 Object Environment Records
Each object Environment Record is associated with an object called its binding object. An object Environment Record binds the set of string identifier names that directly correspond to the property names of its binding object. Property keys that are not strings in the form of an
Object Environment Records created for with statements (
The behaviour of the concrete specification methods for object Environment Records is defined by the following algorithms.
9.1.1.2.1 HasBinding ( N )
The HasBinding concrete method of an
- Let bindings be the binding object for envRec.
- Let foundBinding be ?
HasProperty (bindings, N). - If foundBinding is
false , returnfalse . - If the withEnvironment flag of envRec is
false , returntrue . - Let unscopables be ?
Get (bindings, @@unscopables). - If
Type (unscopables) is Object, then - Return
true .
9.1.1.2.2 CreateMutableBinding ( N, D )
The CreateMutableBinding concrete method of an
- Let bindings be the binding object for envRec.
- Return ?
DefinePropertyOrThrow (bindings, N, PropertyDescriptor { [[Value]]:undefined , [[Writable]]:true , [[Enumerable]]:true , [[Configurable]]: D }).
Normally envRec will not have a binding for N but if it does, the semantics of
9.1.1.2.3 CreateImmutableBinding ( N, S )
The CreateImmutableBinding concrete method of an
9.1.1.2.4 InitializeBinding ( N, V )
The InitializeBinding concrete method of an
- Return ? envRec.SetMutableBinding(N, V,
false ).
In this specification, all uses of CreateMutableBinding for object Environment Records are immediately followed by a call to InitializeBinding for the same name. Hence, this specification does not explicitly track the initialization state of bindings in object Environment Records.
9.1.1.2.5 SetMutableBinding ( N, V, S )
The SetMutableBinding concrete method of an
- Let bindings be the binding object for envRec.
- Let stillExists be ?
HasProperty (bindings, N). - If stillExists is
false and S istrue , throw aReferenceError exception. - Return ?
Set (bindings, N, V, S).
9.1.1.2.6 GetBindingValue ( N, S )
The GetBindingValue concrete method of an
- Let bindings be the binding object for envRec.
- Let value be ?
HasProperty (bindings, N). - If value is
false , then- If S is
false , return the valueundefined ; otherwise throw aReferenceError exception.
- If S is
- Return ?
Get (bindings, N).
9.1.1.2.7 DeleteBinding ( N )
The DeleteBinding concrete method of an
- Let bindings be the binding object for envRec.
- Return ? bindings.[[Delete]](N).
9.1.1.2.8 HasThisBinding ( )
The HasThisBinding concrete method of an
- Return
false .
Object Environment Records do not provide a this binding.
9.1.1.2.9 HasSuperBinding ( )
The HasSuperBinding concrete method of an
- Return
false .
Object Environment Records do not provide a super binding.
9.1.1.2.10 WithBaseObject ( )
The WithBaseObject concrete method of an
- If the withEnvironment flag of envRec is
true , return the binding object for envRec. - Otherwise, return
undefined .
9.1.1.3 Function Environment Records
A function Environment Record is a this binding. If a function is not an super, its function Environment Record also contains the state that is used to perform super method invocations from within the function.
Function Environment Records have the additional state fields listed in
| Field Name | Value | Meaning |
|---|---|---|
| [[ThisValue]] | Any |
This is the |
| [[ThisBindingStatus]] |
|
If the value is |
| [[FunctionObject]] | Object |
The |
| [[NewTarget]] |
Object | |
If this |
Function Environment Records support all of the
| Method | Purpose |
|---|---|
| BindThisValue(V) |
|
| GetThisBinding() |
Return the value of this this binding. Throws a this binding has not been initialized.
|
| GetSuperBase() |
Return the object that is the base for super property accesses bound in this super property accesses will produce runtime errors.
|
The behaviour of the additional concrete specification methods for function Environment Records is defined by the following algorithms:
9.1.1.3.1 BindThisValue ( V )
The BindThisValue concrete method of a
9.1.1.3.2 HasThisBinding ( )
The HasThisBinding concrete method of a
- If envRec.[[ThisBindingStatus]] is
lexical , returnfalse ; otherwise, returntrue .
9.1.1.3.3 HasSuperBinding ( )
The HasSuperBinding concrete method of a
- If envRec.[[ThisBindingStatus]] is
lexical , returnfalse . - If envRec.[[FunctionObject]].[[HomeObject]] has the value
undefined , returnfalse ; otherwise, returntrue .
9.1.1.3.4 GetThisBinding ( )
The GetThisBinding concrete method of a
Assert : envRec.[[ThisBindingStatus]] is notlexical .- If envRec.[[ThisBindingStatus]] is
uninitialized , throw aReferenceError exception. - Return envRec.[[ThisValue]].
9.1.1.3.5 GetSuperBase ( )
The GetSuperBase concrete method of a
9.1.1.4 Global Environment Records
A global Environment Record is used to represent the outer most scope that is shared by all of the ECMAScript
A global Environment Record is logically a single record but it is specified as a composite encapsulating an
Properties may be created directly on a
Global Environment Records have the additional fields listed in
| Field Name | Value | Meaning |
|---|---|---|
| [[ObjectRecord]] |
|
Binding object is the |
| [[GlobalThisValue]] | Object |
The value returned by this in global scope. Hosts may provide any ECMAScript Object value.
|
| [[DeclarativeRecord]] |
|
|
| [[VarNames]] |
|
The string names bound by |
| Method | Purpose |
|---|---|
| GetThisBinding() |
Return the value of this this binding.
|
| HasVarDeclaration (N) |
Determines if the argument identifier has a binding in this |
| HasLexicalDeclaration (N) |
Determines if the argument identifier has a binding in this |
| HasRestrictedGlobalProperty (N) |
Determines if the argument is the name of a |
| CanDeclareGlobalVar (N) | Determines if a corresponding CreateGlobalVarBinding call would succeed if called for the same argument N. |
| CanDeclareGlobalFunction (N) | Determines if a corresponding CreateGlobalFunctionBinding call would succeed if called for the same argument N. |
| CreateGlobalVarBinding(N, D) |
Used to create and initialize to var binding in the [[ObjectRecord]] component of a global Environment Record. The binding will be a mutable binding. The corresponding var. The String value N is the bound name. If D is |
| CreateGlobalFunctionBinding(N, V, D) |
Create and initialize a global function binding in the [[ObjectRecord]] component of a global Environment Record. The binding will be a mutable binding. The corresponding function. The String value N is the bound name. V is the initialization value. If the Boolean argument D is |
The behaviour of the concrete specification methods for global Environment Records is defined by the following algorithms.
9.1.1.4.1 HasBinding ( N )
The HasBinding concrete method of a
- Let DclRec be envRec.[[DeclarativeRecord]].
- If DclRec.HasBinding(N) is
true , returntrue . - Let ObjRec be envRec.[[ObjectRecord]].
- Return ? ObjRec.HasBinding(N).
9.1.1.4.2 CreateMutableBinding ( N, D )
The CreateMutableBinding concrete method of a
- Let DclRec be envRec.[[DeclarativeRecord]].
- If DclRec.HasBinding(N) is
true , throw aTypeError exception. - Return DclRec.CreateMutableBinding(N, D).
9.1.1.4.3 CreateImmutableBinding ( N, S )
The CreateImmutableBinding concrete method of a
- Let DclRec be envRec.[[DeclarativeRecord]].
- If DclRec.HasBinding(N) is
true , throw aTypeError exception. - Return DclRec.CreateImmutableBinding(N, S).
9.1.1.4.4 InitializeBinding ( N, V )
The InitializeBinding concrete method of a
- Let DclRec be envRec.[[DeclarativeRecord]].
- If DclRec.HasBinding(N) is
true , then- Return DclRec.InitializeBinding(N, V).
Assert : If the binding exists, it must be in theobject Environment Record .- Let ObjRec be envRec.[[ObjectRecord]].
- Return ? ObjRec.InitializeBinding(N, V).
9.1.1.4.5 SetMutableBinding ( N, V, S )
The SetMutableBinding concrete method of a
- Let DclRec be envRec.[[DeclarativeRecord]].
- If DclRec.HasBinding(N) is
true , then- Return DclRec.SetMutableBinding(N, V, S).
- Let ObjRec be envRec.[[ObjectRecord]].
- Return ? ObjRec.SetMutableBinding(N, V, S).
9.1.1.4.6 GetBindingValue ( N, S )
The GetBindingValue concrete method of a
- Let DclRec be envRec.[[DeclarativeRecord]].
- If DclRec.HasBinding(N) is
true , then- Return DclRec.GetBindingValue(N, S).
- Let ObjRec be envRec.[[ObjectRecord]].
- Return ? ObjRec.GetBindingValue(N, S).
9.1.1.4.7 DeleteBinding ( N )
The DeleteBinding concrete method of a
- Let DclRec be envRec.[[DeclarativeRecord]].
- If DclRec.HasBinding(N) is
true , then- Return DclRec.DeleteBinding(N).
- Let ObjRec be envRec.[[ObjectRecord]].
- Let globalObject be the binding object for ObjRec.
- Let existingProp be ?
HasOwnProperty (globalObject, N). - If existingProp is
true , then- Let status be ? ObjRec.DeleteBinding(N).
- If status is
true , then- Let varNames be envRec.[[VarNames]].
- If N is an element of varNames, remove that element from the varNames.
- Return status.
- Return
true .
9.1.1.4.8 HasThisBinding ( )
The HasThisBinding concrete method of a
- Return
true .
Global Environment Records always provide a this binding.
9.1.1.4.9 HasSuperBinding ( )
The HasSuperBinding concrete method of a
- Return
false .
Global Environment Records do not provide a super binding.
9.1.1.4.10 WithBaseObject ( )
The WithBaseObject concrete method of a
- Return
undefined .
9.1.1.4.11 GetThisBinding ( )
The GetThisBinding concrete method of a
- Return envRec.[[GlobalThisValue]].
9.1.1.4.12 HasVarDeclaration ( N )
The HasVarDeclaration concrete method of a
- Let varDeclaredNames be envRec.[[VarNames]].
- If varDeclaredNames contains N, return
true . - Return
false .
9.1.1.4.13 HasLexicalDeclaration ( N )
The HasLexicalDeclaration concrete method of a
- Let DclRec be envRec.[[DeclarativeRecord]].
- Return DclRec.HasBinding(N).
9.1.1.4.14 HasRestrictedGlobalProperty ( N )
The HasRestrictedGlobalProperty concrete method of a
- Let ObjRec be envRec.[[ObjectRecord]].
- Let globalObject be the binding object for ObjRec.
- Let existingProp be ? globalObject.[[GetOwnProperty]](N).
- If existingProp is
undefined , returnfalse . - If existingProp.[[Configurable]] is
true , returnfalse . - Return
true .
Properties may exist upon a
9.1.1.4.15 CanDeclareGlobalVar ( N )
The CanDeclareGlobalVar concrete method of a
- Let ObjRec be envRec.[[ObjectRecord]].
- Let globalObject be the binding object for ObjRec.
- Let hasProperty be ?
HasOwnProperty (globalObject, N). - If hasProperty is
true , returntrue . - Return ?
IsExtensible (globalObject).
9.1.1.4.16 CanDeclareGlobalFunction ( N )
The CanDeclareGlobalFunction concrete method of a
- Let ObjRec be envRec.[[ObjectRecord]].
- Let globalObject be the binding object for ObjRec.
- Let existingProp be ? globalObject.[[GetOwnProperty]](N).
- If existingProp is
undefined , return ?IsExtensible (globalObject). - If existingProp.[[Configurable]] is
true , returntrue . - If
IsDataDescriptor (existingProp) istrue and existingProp has attribute values { [[Writable]]:true , [[Enumerable]]:true }, returntrue . - Return
false .
9.1.1.4.17 CreateGlobalVarBinding ( N, D )
The CreateGlobalVarBinding concrete method of a
- Let ObjRec be envRec.[[ObjectRecord]].
- Let globalObject be the binding object for ObjRec.
- Let hasProperty be ?
HasOwnProperty (globalObject, N). - Let extensible be ?
IsExtensible (globalObject). - If hasProperty is
false and extensible istrue , then- Perform ? ObjRec.CreateMutableBinding(N, D).
- Perform ? ObjRec.InitializeBinding(N,
undefined ).
- Let varDeclaredNames be envRec.[[VarNames]].
- If varDeclaredNames does not contain N, then
- Append N to varDeclaredNames.
- Return
NormalCompletion (empty ).
9.1.1.4.18 CreateGlobalFunctionBinding ( N, V, D )
The CreateGlobalFunctionBinding concrete method of a
- Let ObjRec be envRec.[[ObjectRecord]].
- Let globalObject be the binding object for ObjRec.
- Let existingProp be ? globalObject.[[GetOwnProperty]](N).
- If existingProp is
undefined or existingProp.[[Configurable]] istrue , then- Let desc be the PropertyDescriptor { [[Value]]: V, [[Writable]]:
true , [[Enumerable]]:true , [[Configurable]]: D }.
- Let desc be the PropertyDescriptor { [[Value]]: V, [[Writable]]:
- Else,
- Let desc be the PropertyDescriptor { [[Value]]: V }.
- Perform ?
DefinePropertyOrThrow (globalObject, N, desc). - Perform ?
Set (globalObject, N, V,false ). - Let varDeclaredNames be envRec.[[VarNames]].
- If varDeclaredNames does not contain N, then
- Append N to varDeclaredNames.
- Return
NormalCompletion (empty ).
Global function declarations are always represented as own properties of the
9.1.1.5 Module Environment Records
A module Environment Record is a
Module Environment Records support all of the
| Method | Purpose |
|---|---|
| CreateImportBinding(N, M, N2) |
Create an immutable indirect binding in a module Environment Record. The String value N is the text of the bound name. M is a |
| GetThisBinding() |
Return the value of this this binding.
|
The behaviour of the additional concrete specification methods for module Environment Records are defined by the following algorithms:
9.1.1.5.1 GetBindingValue ( N, S )
The GetBindingValue concrete method of a
Assert : S istrue .Assert : envRec has a binding for N.- If the binding for N is an indirect binding, then
- Let M and N2 be the indirection values provided when this binding for N was created.
- Let targetEnv be M.[[Environment]].
- If targetEnv is
undefined , throw aReferenceError exception. - Return ? targetEnv.GetBindingValue(N2,
true ).
- If the binding for N in envRec is an uninitialized binding, throw a
ReferenceError exception. - Return the value currently bound to N in envRec.
S will always be
9.1.1.5.2 DeleteBinding ( N )
The DeleteBinding concrete method of a
Module Environment Records are only used within strict code and an
9.1.1.5.3 HasThisBinding ( )
The HasThisBinding concrete method of a
- Return
true .
Module Environment Records always provide a this binding.
9.1.1.5.4 GetThisBinding ( )
The GetThisBinding concrete method of a
- Return
undefined .
9.1.1.5.5 CreateImportBinding ( N, M, N2 )
The CreateImportBinding concrete method of a
Assert : envRec does not already have a binding for N.Assert : M is aModule Record .Assert : When M.[[Environment]] is instantiated it will have a direct binding for N2.- Create an immutable indirect binding in envRec for N that references M and N2 as its target binding and record that the binding is initialized.
- Return
NormalCompletion (empty ).
9.1.2 Environment Record Operations
The following
9.1.2.1 GetIdentifierReference ( env, name, strict )
The abstract operation GetIdentifierReference takes arguments env (an
- If env is the value
null , then- Return the
Reference Record { [[Base]]:unresolvable , [[ReferencedName]]: name, [[Strict]]: strict, [[ThisValue]]:empty }.
- Return the
- Let exists be ? env.HasBinding(name).
- If exists is
true , then- Return the
Reference Record { [[Base]]: env, [[ReferencedName]]: name, [[Strict]]: strict, [[ThisValue]]:empty }.
- Return the
- Else,
- Let outer be env.[[OuterEnv]].
- Return ? GetIdentifierReference(outer, name, strict).
9.1.2.2 NewDeclarativeEnvironment ( E )
The abstract operation NewDeclarativeEnvironment takes argument E (an
- Let env be a new
declarative Environment Record containing no bindings. Set env.[[OuterEnv]] to E.- Return env.
9.1.2.3 NewObjectEnvironment ( O, E )
The abstract operation NewObjectEnvironment takes arguments O (an Object) and E (an
- Let env be a new
object Environment Record containing O as the binding object. Set env.[[OuterEnv]] to E.- Return env.
9.1.2.4 NewFunctionEnvironment ( F, newTarget )
The abstract operation NewFunctionEnvironment takes arguments F and newTarget. It performs the following steps when called:
Assert : F is an ECMAScript function.Assert :Type (newTarget) is Undefined or Object.- Let env be a new
function Environment Record containing no bindings. Set env.[[FunctionObject]] to F.- If F.[[ThisMode]] is
lexical , set env.[[ThisBindingStatus]] tolexical . - Else, set env.[[ThisBindingStatus]] to
uninitialized . Set env.[[NewTarget]] to newTarget.Set env.[[OuterEnv]] to F.[[Environment]].- Return env.
9.1.2.5 NewGlobalEnvironment ( G, thisValue )
The abstract operation NewGlobalEnvironment takes arguments G and thisValue. It performs the following steps when called:
- Let objRec be a new
object Environment Record containing G as the binding object. - Let dclRec be a new
declarative Environment Record containing no bindings. - Let env be a new
global Environment Record . Set env.[[ObjectRecord]] to objRec.Set env.[[GlobalThisValue]] to thisValue.Set env.[[DeclarativeRecord]] to dclRec.Set env.[[VarNames]] to a new emptyList .Set env.[[OuterEnv]] tonull .- Return env.
9.1.2.6 NewModuleEnvironment ( E )
The abstract operation NewModuleEnvironment takes argument E (an
- Let env be a new
module Environment Record containing no bindings. Set env.[[OuterEnv]] to E.- Return env.
9.2 Realms
Before it is evaluated, all ECMAScript code must be associated with a realm. Conceptually, a
A
| Field Name | Value | Meaning |
|---|---|---|
| [[Intrinsics]] |
|
The intrinsic values used by code associated with this |
| [[GlobalObject]] | Object |
The |
| [[GlobalEnv]] |
|
The global environment for this |
| [[TemplateMap]] |
A |
Template objects are canonicalized separately for each Once a |
| [[HostDefined]] |
Any, default value is |
Field reserved for use by hosts that need to associate additional information with a |
9.2.1 CreateRealm ( )
The abstract operation CreateRealm takes no arguments. It performs the following steps when called:
- Let realmRec be a new
Realm Record . - Perform
CreateIntrinsics (realmRec). Set realmRec.[[GlobalObject]] toundefined .Set realmRec.[[GlobalEnv]] toundefined .Set realmRec.[[TemplateMap]] to a new emptyList .- Return realmRec.
9.2.2 CreateIntrinsics ( realmRec )
The abstract operation CreateIntrinsics takes argument realmRec. It performs the following steps when called:
- Let intrinsics be a new
Record . Set realmRec.[[Intrinsics]] to intrinsics.Set fields of intrinsics with the values listed inTable 8 . The field names are the names listed in column one of the table. The value of each field is a new object value fully and recursively populated with property values as defined by the specification of each object in clauses19 through28 . All object property values are newly created object values. All values that are built-in function objects are created by performingCreateBuiltinFunction (steps, length, name, slots, realmRec, prototype) where steps is the definition of that function provided by this specification, name is the initial value of the function'snameproperty, length is the initial value of the function'slengthproperty, slots is a list of the names, if any, of the function's specified internal slots, and prototype is the specified value of the function's [[Prototype]] internal slot. The creation of the intrinsics and their properties must be ordered to avoid any dependencies upon objects that have not yet been created.- Perform
AddRestrictedFunctionProperties (intrinsics.[[%Function.prototype% ]], realmRec). - Return intrinsics.
9.2.3 SetRealmGlobalObject ( realmRec, globalObj, thisValue )
The abstract operation SetRealmGlobalObject takes arguments realmRec, globalObj, and thisValue. It performs the following steps when called:
- If globalObj is
undefined , then- Let intrinsics be realmRec.[[Intrinsics]].
Set globalObj to !OrdinaryObjectCreate (intrinsics.[[%Object.prototype% ]]).
Assert :Type (globalObj) is Object.- If thisValue is
undefined , set thisValue to globalObj. Set realmRec.[[GlobalObject]] to globalObj.- Let newGlobalEnv be
NewGlobalEnvironment (globalObj, thisValue). Set realmRec.[[GlobalEnv]] to newGlobalEnv.- Return realmRec.
9.2.4 SetDefaultGlobalBindings ( realmRec )
The abstract operation SetDefaultGlobalBindings takes argument realmRec. It performs the following steps when called:
- Let global be realmRec.[[GlobalObject]].
- For each property of the Global Object specified in clause
19 , do- Let name be the String value of the
property name . - Let desc be the fully populated data
Property Descriptor for the property, containing the specified attributes for the property. For properties listed in19.2 ,19.3 , or19.4 the value of the [[Value]] attribute is the corresponding intrinsic object from realmRec. - Perform ?
DefinePropertyOrThrow (global, name, desc).
- Let name be the String value of the
- Return global.
9.3 Execution Contexts
An execution context is a specification device that is used to track the runtime evaluation of code by an ECMAScript implementation. At any point in time, there is at most one execution context per
The execution context stack is used to track execution contexts. The
An execution context contains whatever implementation specific state is necessary to track the execution progress of its associated code. Each execution context has at least the state components listed in
| Component | Purpose |
|---|---|
| code evaluation state | Any state needed to perform, suspend, and resume evaluation of the code associated with this execution context. |
| Function |
If this execution context is evaluating the code of a |
|
|
The |
| ScriptOrModule |
The |
Evaluation of code by the
The value of the
Execution contexts for ECMAScript code have the additional state components listed in
| Component | Purpose |
|---|---|
| LexicalEnvironment |
Identifies the |
| VariableEnvironment |
Identifies the |
The LexicalEnvironment and VariableEnvironment components of an execution context are always Environment Records.
Execution contexts representing the evaluation of generator objects have the additional state components listed in
| Component | Purpose |
|---|---|
| Generator | The generator object that this execution context is evaluating. |
In most situations only the
An execution context is purely a specification mechanism and need not correspond to any particular artefact of an ECMAScript implementation. It is impossible for ECMAScript code to directly access or observe an execution context.
9.3.1 GetActiveScriptOrModule ( )
The abstract operation GetActiveScriptOrModule takes no arguments. It is used to determine the running script or module, based on the
- If the
execution context stack is empty, returnnull . - Let ec be the topmost
execution context on theexecution context stack whose ScriptOrModule component is notnull . - If no such
execution context exists, returnnull . Otherwise, return ec's ScriptOrModule.
9.3.2 ResolveBinding ( name [ , env ] )
The abstract operation ResolveBinding takes argument name (a String) and optional argument env (an
- If env is not present or if env is
undefined , thenSet env to therunning execution context 's LexicalEnvironment.
Assert : env is anEnvironment Record .- If the code matching the syntactic production that is being evaluated is contained in
strict mode code , let strict betrue ; else let strict befalse . - Return ?
GetIdentifierReference (env, name, strict).
The result of ResolveBinding is always a
9.3.3 GetThisEnvironment ( )
The abstract operation GetThisEnvironment takes no arguments. It finds the this. It performs the following steps when called:
- Let env be the
running execution context 's LexicalEnvironment. - Repeat,
The loop in step this binding.
9.3.4 ResolveThisBinding ( )
The abstract operation ResolveThisBinding takes no arguments. It determines the binding of the this using the LexicalEnvironment of the
- Let envRec be
GetThisEnvironment (). - Return ? envRec.GetThisBinding().
9.3.5 GetNewTarget ( )
The abstract operation GetNewTarget takes no arguments. It determines the NewTarget value using the LexicalEnvironment of the
- Let envRec be
GetThisEnvironment (). Assert : envRec has a [[NewTarget]] field.- Return envRec.[[NewTarget]].
9.3.6 GetGlobalObject ( )
The abstract operation GetGlobalObject takes no arguments. It returns the
- Let currentRealm be
the current Realm Record . - Return currentRealm.[[GlobalObject]].
9.4 Jobs and Host Operations to Enqueue Jobs
A Job is an
Jobs are scheduled for execution by ECMAScript
- At some future point in time, when there is no
running execution context and theexecution context stack is empty, the implementation must:- Perform any
host-defined preparation steps. Invoke theJob Abstract Closure .- Perform any
host-defined cleanup steps, after which theexecution context stack must be empty.
- Perform any
- Only one
Job may be actively undergoing evaluation at any point in time. - Once evaluation of a
Job starts, it must run to completion before evaluation of any otherJob starts. - The
Abstract Closure must return a normal completion, implementing its own handling of errors.
At any particular time, scriptOrModule (a
GetActiveScriptOrModule () is scriptOrModule.- If scriptOrModule is a
Script Record orModule Record , let ec be the topmostexecution context on theexecution context stack whose ScriptOrModule component is scriptOrModule. TheRealm component of ec is scriptOrModule.[[Realm]].
At any particular time, an execution is prepared to evaluate ECMAScript code if all of the following conditions are true:
- The
execution context stack is not empty. - The
Realm component of the topmostexecution context on theexecution context stack is aRealm Record .
The specific choice of
Particular kinds of Jobs have additional conformance requirements.
9.4.1 JobCallback Records
A JobCallback Record is a
The WHATWG HTML specification (https://html.spec.whatwg.org/), for example, uses the
JobCallback Records have the fields listed in
| Field Name | Value | Meaning |
|---|---|---|
| [[Callback]] |
A |
The function to invoke when the |
| [[HostDefined]] |
Any, default value is |
Field reserved for use by hosts. |
9.4.2 HostMakeJobCallback ( callback )
The
The implementation of HostMakeJobCallback must conform to the following requirements:
- It must always complete normally (i.e., not return an
abrupt completion ). - It must always return a
JobCallback Record whose [[Callback]] field is callback.
The default implementation of HostMakeJobCallback performs the following steps when called:
Assert :IsCallable (callback) istrue .- Return the
JobCallback Record { [[Callback]]: callback, [[HostDefined]]:empty }.
ECMAScript hosts that are not web browsers must use the default implementation of HostMakeJobCallback.
This is called at the time that the callback is passed to the function that is responsible for its being eventually scheduled and run. For example, promise.then(thenAction) calls MakeJobCallback on thenAction at the time of invoking Promise.prototype.then, not at the time of scheduling the reaction
9.4.3 HostCallJobCallback ( jobCallback, V, argumentsList )
The
The implementation of HostCallJobCallback must conform to the following requirements:
- It must always perform and return the result of
Call (jobCallback.[[Callback]], V, argumentsList).
This requirement means that hosts cannot change the [[Call]] behaviour of function objects defined in this specification.
The default implementation of HostCallJobCallback performs the following steps when called:
Assert :IsCallable (jobCallback.[[Callback]]) istrue .- Return ?
Call (jobCallback.[[Callback]], V, argumentsList).
ECMAScript hosts that are not web browsers must use the default implementation of HostCallJobCallback.
9.4.4 HostEnqueuePromiseJob ( job, realm )
The
The implementation of HostEnqueuePromiseJob must conform to the requirements in
- If realm is not
null , each time job is invoked the implementation must performimplementation-defined steps such that execution isprepared to evaluate ECMAScript code at the time of job's invocation. - Let scriptOrModule be
GetActiveScriptOrModule () at the time HostEnqueuePromiseJob is invoked. If realm is notnull , each time job is invoked the implementation must performimplementation-defined steps such that scriptOrModule is theactive script or module at the time of job's invocation. - Jobs must run in the same order as the HostEnqueuePromiseJob invocations that scheduled them.
The realm for Jobs returned by
9.5 InitializeHostDefinedRealm ( )
The abstract operation InitializeHostDefinedRealm takes no arguments. It performs the following steps when called:
- Let realm be
CreateRealm (). - Let newContext be a new
execution context . Set the Function of newContext tonull .Set theRealm of newContext to realm.Set the ScriptOrModule of newContext tonull .- Push newContext onto the
execution context stack ; newContext is now therunning execution context . - If the
host requires use of anexotic object to serve as realm'sglobal object , let global be such an object created in ahost-defined manner. Otherwise, let global beundefined , indicating that anordinary object should be created as theglobal object . - If the
host requires that thethisbinding in realm's global scope return an object other than theglobal object , let thisValue be such an object created in ahost-defined manner. Otherwise, let thisValue beundefined , indicating that realm's globalthisbinding should be theglobal object . - Perform
SetRealmGlobalObject (realm, global, thisValue). - Let globalObj be ?
SetDefaultGlobalBindings (realm). - Create any
host-defined global object properties on globalObj. - Return
NormalCompletion (empty ).
9.6 Agents
An agent comprises a set of ECMAScript execution contexts, an
An
Some web browsers share a single
While an
| Field Name | Value | Meaning |
|---|---|---|
| [[LittleEndian]] | Boolean | The default value computed for the isLittleEndian parameter when it is needed by the algorithms |
| [[CanBlock]] | Boolean | Determines whether the |
| [[Signifier]] | Any globally-unique value | Uniquely identifies the |
| [[IsLockFree1]] | Boolean | |
| [[IsLockFree2]] | Boolean | |
| [[IsLockFree8]] | Boolean | |
| [[CandidateExecution]] | A |
See the |
| [[KeptAlive]] | Initially a new empty |
Once the values of [[Signifier]], [[IsLockFree1]], and [[IsLockFree2]] have been observed by any
The values of [[IsLockFree1]] and [[IsLockFree2]] are not necessarily determined by the hardware, but may also reflect implementation choices that can vary over time and between ECMAScript implementations.
There is no [[IsLockFree4]] property: 4-byte atomic operations are always lock-free.
In practice, if an atomic operation is implemented with any
That an atomic access of size n is lock-free does not imply anything about the (perceived) atomicity of non-atomic accesses of size n, specifically, non-atomic accesses may still be performed as a sequence of several separate memory accesses. See
An
9.6.1 AgentSignifier ( )
The abstract operation AgentSignifier takes no arguments. It performs the following steps when called:
- Let AR be the
Agent Record of thesurrounding agent . - Return AR.[[Signifier]].
9.6.2 AgentCanSuspend ( )
The abstract operation AgentCanSuspend takes no arguments. It performs the following steps when called:
- Let AR be the
Agent Record of thesurrounding agent . - Return AR.[[CanBlock]].
In some environments it may not be reasonable for a given
9.7 Agent Clusters
An agent cluster is a maximal set of agents that can communicate by operating on shared memory.
Programs within different agents may share memory by unspecified means. At a minimum, the backing memory for SharedArrayBuffer objects can be shared among the agents in the cluster.
There may be agents that can communicate by message passing that cannot share memory; they are never in the same agent cluster.
Every
All agents within a cluster must have the same value for the [[LittleEndian]] property in their respective
If different agents within an agent cluster have different values of [[LittleEndian]] it becomes hard to use shared memory for multi-byte data.
All agents within a cluster must have the same values for the [[IsLockFree1]] property in their respective
All agents within a cluster must have different values for the [[Signifier]] property in their respective
An embedding may deactivate (stop forward progress) or activate (resume forward progress) an
The purpose of the preceding restriction is to avoid a situation where an
The implication of the restriction is that it will not be possible to share memory between agents that don't belong to the same suspend/wake collective within the embedding.
An embedding may terminate an
Prior to any evaluation of any ECMAScript code by any
All agents in an agent cluster share the same
An agent cluster is a specification mechanism and need not correspond to any particular artefact of an ECMAScript implementation.
9.8 Forward Progress
For an
An
Implementations must ensure that:
- every unblocked
agent with a dedicatedexecuting thread eventually makes forward progress - in a set of agents that share an
executing thread , oneagent eventually makes forward progress - an
agent does not cause anotheragent to become blocked except via explicit APIs that provide blocking.
This, along with the liveness guarantee in the
9.9 Processing Model of WeakRef and FinalizationRegistry Objects
9.9.1 Objectives
This specification does not make any guarantees that any object will be garbage collected. Objects which are not
The semantics of
-
When
WeakRef.prototype.derefis called, the referent (ifundefined is not returned) is kept alive so that subsequent, synchronous accesses also return the object. This list is reset when synchronous work is done using theClearKeptObjects abstract operation. -
When an object which is registered with a
FinalizationRegistry becomes unreachable, a call of theFinalizationRegistry 's cleanup callback may eventually be made, after synchronous ECMAScript execution completes. TheFinalizationRegistry cleanup is performed with theCleanupFinalizationRegistry abstract operation.
Neither of these actions (
Some ECMAScript implementations include garbage collector implementations which run in the background, including when ECMAScript is idle. Letting the
9.9.2 Liveness
For some set of objects S, a hypothetical WeakRef-oblivious execution with respect to S is an execution whereby the abstract operation
At any point during evaluation, a set of objects S is considered live if either of the following conditions is met:
-
Any element in S is included in any
agent 's [[KeptAlive]]List . - There exists a valid future hypothetical WeakRef-oblivious execution with respect to S that observes the Object value of any object in S.
Presence of an object in a field, an internal slot, or a property does not imply that the object is live. For example if the object in question is never passed back to the program, then it cannot be observed.
This is the case for keys in a WeakMap, members of a WeakSet, as well as the [[WeakRefTarget]] and [[UnregisterToken]] fields of a
The above definition implies that, if a key in a WeakMap is not live, then its corresponding value is not necessarily live either.
9.9.3 Execution
At any time, if a set of objects S is not
- For each element obj of S, do
- For each
WeakRef ref such that ref.[[WeakRefTarget]] is obj, doSet ref.[[WeakRefTarget]] toempty .
- For each
FinalizationRegistry fg such that fg.[[Cells]] contains aRecord cell such that cell.[[WeakRefTarget]] is obj, doSet cell.[[WeakRefTarget]] toempty .- Optionally, perform !
HostEnqueueFinalizationRegistryCleanupJob (fg).
- For each WeakMap map such that map.[[WeakMapData]] contains a
Record r such that r.[[Key]] is obj, do - For each WeakSet set such that set.[[WeakSetData]] contains obj, do
- Replace the element of set.[[WeakSetData]] whose value is obj with an element whose value is
empty .
- Replace the element of set.[[WeakSetData]] whose value is obj with an element whose value is
- For each
Together with the definition of liveness, this clause prescribes legal optimizations that an implementation may apply regarding WeakRefs.
It is possible to access an object without observing its identity. Optimizations such as dead variable elimination and scalar replacement on properties of non-escaping objects whose identity is not observed are allowed. These optimizations are thus allowed to observably empty WeakRefs that point to such objects.
On the other hand, if an object's identity is observable, and that object is in the [[WeakRefTarget]] internal slot of a
Because calling
Implementations are not obligated to empty WeakRefs for maximal sets of non-
If an implementation chooses a non-
9.9.4 Host Hooks
9.9.4.1 HostEnqueueFinalizationRegistryCleanupJob ( finalizationRegistry )
The abstract operation HostEnqueueFinalizationRegistryCleanupJob takes argument finalizationRegistry (a
9.10 ClearKeptObjects ( )
The abstract operation ClearKeptObjects takes no arguments. ECMAScript implementations are expected to call ClearKeptObjects when a synchronous sequence of ECMAScript executions completes. It performs the following steps when called:
- Let agentRecord be the
surrounding agent 'sAgent Record . Set agentRecord.[[KeptAlive]] to a new emptyList .
9.11 AddToKeptObjects ( object )
The abstract operation AddToKeptObjects takes argument object (an Object). It performs the following steps when called:
- Let agentRecord be the
surrounding agent 'sAgent Record . - Append object to agentRecord.[[KeptAlive]].
9.12 CleanupFinalizationRegistry ( finalizationRegistry )
The abstract operation CleanupFinalizationRegistry takes argument finalizationRegistry (a
Assert : finalizationRegistry has [[Cells]] and [[CleanupCallback]] internal slots.- Let callback be finalizationRegistry.[[CleanupCallback]].
- While finalizationRegistry.[[Cells]] contains a
Record cell such that cell.[[WeakRefTarget]] isempty , an implementation may perform the following steps:- Choose any such cell.
- Remove cell from finalizationRegistry.[[Cells]].
- Perform ?
Call (callback,undefined , « cell.[[HeldValue]] »).
- Return
NormalCompletion (undefined ).