8 Executable Code and Execution Contexts
8.1 Lexical Environments
A Lexical Environment is a specification
An
The outer environment reference is used to model the logical nesting of Lexical Environment values. The outer reference of a (inner) Lexical Environment is a reference to the Lexical Environment that logically surrounds the inner Lexical Environment. An outer Lexical Environment may, of course, have its own outer Lexical Environment. A Lexical Environment may serve as the outer environment for multiple inner Lexical Environments. For example, if a
A global environment is a Lexical Environment which does not have an outer environment. The
A module environment is a Lexical Environment that contains the bindings for the top level declarations of a
A function environment is a Lexical Environment that corresponds to the invocation of an ECMAScript this binding. A super method invocations.
Lexical Environments and
8.1.1 Environment Records
There are two primary kinds of Environment Record values used in this specification: declarative Environment Records and object Environment Records. Declarative Environment Records are used to define the effect of ECMAScript language syntactic elements such as
For specification purposes Environment Record values are values of the
| Method | Purpose |
|---|---|
| HasBinding(N) |
Determine if an Environment Record has a binding for the String value N. Return |
| CreateMutableBinding(N, D) |
Create a new but uninitialized mutable binding in an Environment Record. The String value N is the text of the bound name. If the Boolean argument D is |
| CreateImmutableBinding(N, S) |
Create a new but uninitialized immutable binding in an Environment Record. The String value N is the text of the bound name. If S is |
| InitializeBinding(N, V) |
|
| SetMutableBinding(N, V, S) |
|
| GetBindingValue(N, S) |
Returns the value of an already existing binding from an Environment Record. The String value N is the text of the bound name. S is used to identify references originating in |
| DeleteBinding(N) |
Delete a binding from an Environment Record. The String value N is the text of the bound name. If a binding for N exists, remove the binding and return |
| HasThisBinding() |
Determine if an Environment Record establishes a this binding. Return |
| HasSuperBinding() |
Determine if an Environment Record establishes a super method binding. Return |
| WithBaseObject() |
If this Environment Record is associated with a with statement, return the with object. Otherwise, return |
8.1.1.1 Declarative Environment Records
Each declarative
The behaviour of the concrete specification methods for declarative Environment Records is defined by the following algorithms.
8.1.1.1.1 HasBinding ( N )
The concrete
- Let envRec be the declarative
Environment Record for which the method was invoked. - If envRec has a binding for the name that is the value of N, return
true . - Return
false .
8.1.1.1.2 CreateMutableBinding ( N, D )
The concrete
- Let envRec be the declarative
Environment Record for which the method was invoked. 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 ).
8.1.1.1.3 CreateImmutableBinding ( N, S )
The concrete
- Let envRec be the declarative
Environment Record for which the method was invoked. 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 ).
8.1.1.1.4 InitializeBinding ( N, V )
The concrete
- Let envRec be the declarative
Environment Record for which the method was invoked. 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 ).
8.1.1.1.5 SetMutableBinding ( N, V, S )
The concrete
- Let envRec be the declarative
Environment Record for which the method was invoked. - 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 2 is:
function f() { eval("var x; x = (delete x, 0);"); }
8.1.1.1.6 GetBindingValue ( N, S )
The concrete
- Let envRec be the declarative
Environment Record for which the method was invoked. 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.
8.1.1.1.7 DeleteBinding ( N )
The concrete
- Let envRec be the declarative
Environment Record for which the method was invoked. 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 .
8.1.1.1.8 HasThisBinding ( )
Regular declarative Environment Records do not provide a this binding.
- Return
false .
8.1.1.1.9 HasSuperBinding ( )
Regular declarative Environment Records do not provide a super binding.
- Return
false .
8.1.1.1.10 WithBaseObject ( )
Declarative Environment Records always return
- Return
undefined .
8.1.1.2 Object Environment Records
Each object
Object Environment Records created for with statements (
The behaviour of the concrete specification methods for object Environment Records is defined by the following algorithms.
8.1.1.2.1 HasBinding ( N )
The concrete
- Let envRec be the object
Environment Record for which the method was invoked. - 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 .
8.1.1.2.2 CreateMutableBinding ( N, D )
The concrete
- Let envRec be the object
Environment Record for which the method was invoked. - 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
8.1.1.2.3 CreateImmutableBinding ( N, S )
The concrete
8.1.1.2.4 InitializeBinding ( N, V )
The concrete
- Let envRec be the object
Environment Record for which the method was invoked. Assert : envRec must have an uninitialized binding for N.Record that the binding for N in envRec has been initialized.- 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, implementations do not need to explicitly track the initialization state of individual object
8.1.1.2.5 SetMutableBinding ( N, V, S )
The concrete
- Let envRec be the object
Environment Record for which the method was invoked. - Let bindings be the binding object for envRec.
- Return ?
Set (bindings, N, V, S).
8.1.1.2.6 GetBindingValue ( N, S )
The concrete
- Let envRec be the object
Environment Record for which the method was invoked. - 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).
8.1.1.2.7 DeleteBinding ( N )
The concrete
- Let envRec be the object
Environment Record for which the method was invoked. - Let bindings be the binding object for envRec.
- Return ? bindings.[[Delete]](N).
8.1.1.2.8 HasThisBinding ( )
Regular object Environment Records do not provide a this binding.
- Return
false .
8.1.1.2.9 HasSuperBinding ( )
Regular object Environment Records do not provide a super binding.
- Return
false .
8.1.1.2.10 WithBaseObject ( )
Object Environment Records return
- Let envRec be the object
Environment Record for which the method was invoked. - If the withEnvironment flag of envRec is
true , return the binding object for envRec. - Otherwise, return
undefined .
8.1.1.3 Function Environment Records
A function Environment Record is a declarative 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 |
| [[HomeObject]] |
Object | |
If the associated function has super property accesses and is not an |
| [[NewTarget]] |
Object | |
If this |
Function Environment Records support all of the declarative
| 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:
8.1.1.3.1 BindThisValue ( V )
- Let envRec be the
function Environment Record for which the method was invoked. Assert : envRec.[[ThisBindingStatus]] is notlexical .- If envRec.[[ThisBindingStatus]] is
initialized , throw aReferenceError exception. Set envRec.[[ThisValue]] to V.Set envRec.[[ThisBindingStatus]] toinitialized .- Return V.
8.1.1.3.2 HasThisBinding ( )
- Let envRec be the
function Environment Record for which the method was invoked. - If envRec.[[ThisBindingStatus]] is
lexical , returnfalse ; otherwise, returntrue .
8.1.1.3.3 HasSuperBinding ( )
- Let envRec be the
function Environment Record for which the method was invoked. - If envRec.[[ThisBindingStatus]] is
lexical , returnfalse . - If envRec.[[HomeObject]] has the value
undefined , returnfalse ; otherwise, returntrue .
8.1.1.3.4 GetThisBinding ( )
- Let envRec be the
function Environment Record for which the method was invoked. Assert : envRec.[[ThisBindingStatus]] is notlexical .- If envRec.[[ThisBindingStatus]] is
uninitialized , throw aReferenceError exception. - Return envRec.[[ThisValue]].
8.1.1.3.5 GetSuperBase ( )
- Let envRec be the
function Environment Record for which the method was invoked. - Let home be envRec.[[HomeObject]].
- If home has the value
undefined , returnundefined . Assert :Type (home) is Object.- Return ? home.[[GetPrototypeOf]]().
8.1.1.4 Global Environment Records
A global
A global
Properties may be created directly on a
Global Environment Records have the additional fields listed in
| Field Name | Value | Meaning |
|---|---|---|
| [[ObjectRecord]] |
Object |
Binding object is the |
| [[GlobalThisValue]] | Object |
The value returned by this in global scope. Hosts may provide any ECMAScript Object value.
|
| [[DeclarativeRecord]] |
Declarative |
Contains bindings for all declarations in global code for the associated |
| [[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 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 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.
8.1.1.4.1 HasBinding ( N )
The concrete
- Let envRec be the global
Environment Record for which the method was invoked. - Let DclRec be envRec.[[DeclarativeRecord]].
- If DclRec.HasBinding(N) is
true , returntrue . - Let ObjRec be envRec.[[ObjectRecord]].
- Return ? ObjRec.HasBinding(N).
8.1.1.4.2 CreateMutableBinding ( N, D )
The concrete
- Let envRec be the global
Environment Record for which the method was invoked. - Let DclRec be envRec.[[DeclarativeRecord]].
- If DclRec.HasBinding(N) is
true , throw aTypeError exception. - Return DclRec.CreateMutableBinding(N, D).
8.1.1.4.3 CreateImmutableBinding ( N, S )
The concrete
- Let envRec be the global
Environment Record for which the method was invoked. - Let DclRec be envRec.[[DeclarativeRecord]].
- If DclRec.HasBinding(N) is
true , throw aTypeError exception. - Return DclRec.CreateImmutableBinding(N, S).
8.1.1.4.4 InitializeBinding ( N, V )
The concrete
- Let envRec be the global
Environment Record for which the method was invoked. - 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 the objectEnvironment Record .- Let ObjRec be envRec.[[ObjectRecord]].
- Return ? ObjRec.InitializeBinding(N, V).
8.1.1.4.5 SetMutableBinding ( N, V, S )
The concrete
- Let envRec be the global
Environment Record for which the method was invoked. - 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).
8.1.1.4.6 GetBindingValue ( N, S )
The concrete
- Let envRec be the global
Environment Record for which the method was invoked. - 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).
8.1.1.4.7 DeleteBinding ( N )
The concrete
- Let envRec be the global
Environment Record for which the method was invoked. - 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 .
8.1.1.4.8 HasThisBinding ( )
- Return
true .
8.1.1.4.9 HasSuperBinding ( )
- Return
false .
8.1.1.4.10 WithBaseObject ( )
Global Environment Records always return
- Return
undefined .
8.1.1.4.11 GetThisBinding ( )
- Let envRec be the global
Environment Record for which the method was invoked. - Return envRec.[[GlobalThisValue]].
8.1.1.4.12 HasVarDeclaration ( N )
The concrete
- Let envRec be the global
Environment Record for which the method was invoked. - Let varDeclaredNames be envRec.[[VarNames]].
- If varDeclaredNames contains N, return
true . - Return
false .
8.1.1.4.13 HasLexicalDeclaration ( N )
The concrete
- Let envRec be the global
Environment Record for which the method was invoked. - Let DclRec be envRec.[[DeclarativeRecord]].
- Return DclRec.HasBinding(N).
8.1.1.4.14 HasRestrictedGlobalProperty ( N )
The concrete
- Let envRec be the global
Environment Record for which the method was invoked. - 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
8.1.1.4.15 CanDeclareGlobalVar ( N )
The concrete
- Let envRec be the global
Environment Record for which the method was invoked. - 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).
8.1.1.4.16 CanDeclareGlobalFunction ( N )
The concrete
- Let envRec be the global
Environment Record for which the method was invoked. - 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 .
8.1.1.4.17 CreateGlobalVarBinding ( N, D )
The concrete
- Let envRec be the global
Environment Record for which the method was invoked. - 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 ).
8.1.1.4.18 CreateGlobalFunctionBinding ( N, V, D )
The concrete
- Let envRec be the global
Environment Record for which the method was invoked. - 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). Record that the binding for N in ObjRec has been initialized.- 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
8.1.1.5 Module Environment Records
A module
Module Environment Records support all of the declarative
| Method | Purpose |
|---|---|
| CreateImportBinding(N, M, N2) |
Create an immutable indirect binding in a module |
| 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:
8.1.1.5.1 GetBindingValue ( N, S )
The concrete
Assert : S istrue .- Let envRec be the module
Environment Record for which the method was invoked. 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. - Let targetER be targetEnv's
EnvironmentRecord . - Return ? targetER.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
8.1.1.5.2 DeleteBinding ( N )
The concrete
Module Environment Records are only used within strict code and an
8.1.1.5.3 HasThisBinding ( )
Module Environment Records provide a this binding.
- Return
true .
8.1.1.5.4 GetThisBinding ( )
- Return
undefined .
8.1.1.5.5 CreateImportBinding ( N, M, N2 )
The concrete
- Let envRec be the module
Environment Record for which the method was invoked. 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 ).
8.1.2 Lexical Environment Operations
The following
8.1.2.1 GetIdentifierReference ( lex, name, strict )
The abstract operation GetIdentifierReference is called with a
- If lex is the value
null , then - Let envRec be lex's
EnvironmentRecord . - Let exists be ? envRec.HasBinding(name).
- If exists is
true , then - Else,
- Let outer be the value of lex's outer environment reference.
- Return ? GetIdentifierReference(outer, name, strict).
8.1.2.2 NewDeclarativeEnvironment ( E )
When the abstract operation NewDeclarativeEnvironment is called with a
- Let env be a new
Lexical Environment . - Let envRec be a new declarative
Environment Record containing no bindings. Set env'sEnvironmentRecord to envRec.Set the outer lexical environment reference of env to E.- Return env.
8.1.2.3 NewObjectEnvironment ( O, E )
When the abstract operation NewObjectEnvironment is called with an Object O and a
- Let env be a new
Lexical Environment . - Let envRec be a new object
Environment Record containing O as the binding object. Set env'sEnvironmentRecord to envRec.Set the outer lexical environment reference of env to E.- Return env.
8.1.2.4 NewFunctionEnvironment ( F, newTarget )
When the abstract operation NewFunctionEnvironment is called with arguments F and newTarget the following steps are performed:
Assert : F is an ECMAScript function.Assert :Type (newTarget) is Undefined or Object.- Let env be a new
Lexical Environment . - Let envRec be a new
function Environment Record containing no bindings. Set envRec.[[FunctionObject]] to F.- If F.[[ThisMode]] is
lexical , set envRec.[[ThisBindingStatus]] tolexical . - Else, set envRec.[[ThisBindingStatus]] to
uninitialized . - Let home be F.[[HomeObject]].
Set envRec.[[HomeObject]] to home.Set envRec.[[NewTarget]] to newTarget.Set env'sEnvironmentRecord to envRec.Set the outer lexical environment reference of env to F.[[Environment]].- Return env.
8.1.2.5 NewGlobalEnvironment ( G, thisValue )
When the abstract operation NewGlobalEnvironment is called with arguments G and thisValue, the following steps are performed:
- Let env be a new
Lexical Environment . - 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 globalRec be a new global
Environment Record . Set globalRec.[[ObjectRecord]] to objRec.Set globalRec.[[GlobalThisValue]] to thisValue.Set globalRec.[[DeclarativeRecord]] to dclRec.Set globalRec.[[VarNames]] to a new emptyList .Set env'sEnvironmentRecord to globalRec.Set the outer lexical environment reference of env tonull .- Return env.
8.1.2.6 NewModuleEnvironment ( E )
When the abstract operation NewModuleEnvironment is called with a
- Let env be a new
Lexical Environment . - Let envRec be a new module
Environment Record containing no bindings. Set env'sEnvironmentRecord to envRec.Set the outer lexical environment reference of env to E.- Return env.
8.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 |
| [[TemplateMap]] |
A |
Template objects are canonicalized separately for each Once a |
| [[HostDefined]] |
Any, default value is |
Field reserved for use by host environments that need to associate additional information with a |
8.2.1 CreateRealm ( )
The abstract operation CreateRealm with no arguments performs the following steps:
- 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.
8.2.2 CreateIntrinsics ( realmRec )
The abstract operation CreateIntrinsics with argument realmRec performs the following steps:
- 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 clauses 18-26. All object property values are newly created object values. All values that are built-in function objects are created by performingCreateBuiltinFunction (<steps>, <slots>, realmRec, <prototype>) where <steps> is the definition of that function provided by this specification, <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.
8.2.3 SetRealmGlobalObject ( realmRec, globalObj, thisValue )
The abstract operation SetRealmGlobalObject with arguments realmRec, globalObj, and thisValue performs the following steps:
- If globalObj is
undefined , then- Let intrinsics be realmRec.[[Intrinsics]].
Set globalObj toOrdinaryObjectCreate (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.
8.2.4 SetDefaultGlobalBindings ( realmRec )
The abstract operation SetDefaultGlobalBindings with argument realmRec performs the following steps:
- Let global be realmRec.[[GlobalObject]].
- For each property of the Global Object specified in clause
18 , 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 in18.2 ,18.3 , or18.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.
8.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 Lexical Environments.
Execution contexts representing the evaluation of generator objects have the additional state components listed in
| Component | Purpose |
|---|---|
| Generator | The GeneratorObject 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.
8.3.1 GetActiveScriptOrModule ( )
The GetActiveScriptOrModule abstract operation 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.
8.3.2 ResolveBinding ( name [ , env ] )
The ResolveBinding abstract operation is used to determine the binding of name passed as a String value. The optional argument env can be used to explicitly provide the
- If env is not present or if env is
undefined , thenSet env to therunning execution context 's LexicalEnvironment.
Assert : env is aLexical Environment .- 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
8.3.3 GetThisEnvironment ( )
The abstract operation GetThisEnvironment finds the this. GetThisEnvironment performs the following steps:
- Let lex be the
running execution context 's LexicalEnvironment. - Repeat,
- Let envRec be lex's
EnvironmentRecord . - Let exists be envRec.HasThisBinding().
- If exists is
true , return envRec. - Let outer be the value of lex's outer environment reference.
Assert : outer is notnull .Set lex to outer.
- Let envRec be lex's
The loop in step 2 will always terminate because the list of environments always ends with the this binding.
8.3.4 ResolveThisBinding ( )
The abstract operation ResolveThisBinding determines the binding of the this using the LexicalEnvironment of the
- Let envRec be
GetThisEnvironment (). - Return ? envRec.GetThisBinding().
8.3.5 GetNewTarget ( )
The abstract operation GetNewTarget determines the NewTarget value using the LexicalEnvironment of the
- Let envRec be
GetThisEnvironment (). Assert : envRec has a [[NewTarget]] field.- Return envRec.[[NewTarget]].
8.3.6 GetGlobalObject ( )
The abstract operation GetGlobalObject returns the
- Let currentRealm be
the current Realm Record . - Return currentRealm.[[GlobalObject]].
8.4 Jobs and Host Operations to Enqueue Jobs
A Job is an
Jobs are scheduled for execution by ECMAScript host environments. This specification describes the host hook
- At some future point in time, when there is no
running execution context and theexecution context stack is empty, the implementation must:- Push an
execution context onto theexecution context stack . - Perform any implementation-defined preparation steps.
- Call the
abstract closure . - Perform any implementation-defined cleanup steps.
- Pop the previously-pushed
execution context from theexecution context stack .
- Push an
- 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.
Host environments are not required to treat Jobs uniformly with respect to scheduling. For example, web browsers and Node.js treat Promise-handling Jobs as a higher priority than other work; future features may add Jobs that are not treated at such a high priority.
8.4.1 HostEnqueuePromiseJob ( job, realm )
HostEnqueuePromiseJob is a host-defined abstract operation that schedules the
The realm parameter is passed through to hosts with no normative requirements; it is either
The implementation of HostEnqueuePromiseJob must conform to the requirements in
8.5 InitializeHostDefinedRealm ( )
The abstract operation InitializeHostDefinedRealm performs the following steps:
- 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 an
exotic object to serve as realm'sglobal object , let global be such an object created in an implementation-defined manner. Otherwise, let global beundefined , indicating that anordinary object should be created as theglobal object . - If the host requires that the
thisbinding in realm's global scope return an object other than theglobal object , let thisValue be such an object created in an implementation-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 implementation-defined
global object properties on globalObj. - Return
NormalCompletion (empty ).
8.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 |
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
8.6.1 AgentSignifier ( )
The abstract operation AgentSignifier takes no arguments. It performs the following steps:
- Let AR be the
Agent Record of thesurrounding agent . - Return AR.[[Signifier]].
8.6.2 AgentCanSuspend ( )
The abstract operation AgentCanSuspend takes no arguments. It performs the following steps:
- Let AR be the
Agent Record of thesurrounding agent . - Return AR.[[CanBlock]].
In some environments it may not be reasonable for a given
8.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.
8.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