8 Executable Code and Execution Contexts
8.1 Lexical Environments
A Lexical Environment is a specification type used to define the association of
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 global environment's outer environment reference is this binding. As ECMAScript code is executed, additional properties may be added to the global object and the initial properties may be modified.
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 function object. A function environment may establish a new this binding. A function environment also captures the state necessary to support 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 Record specification type and can be thought of as existing in a simple object-oriented hierarchy where Environment Record is an abstract class with three concrete subclasses, declarative Environment Record, object Environment Record, and global Environment Record. Function Environment Records and module Environment Records are subclasses of declarative Environment Record. The abstract class includes the abstract specification methods defined in
| 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 optional 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) |
Set the value of an already existing but uninitialized binding in an Environment Record. The String value N is the text of the bound name. V is the value for the binding and is a value of any |
| SetMutableBinding(N,V, S) |
Set the value of an already existing mutable binding in an Environment Record. The String value N is the text of the bound name. V is the value for the binding and may be a value of any |
| 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, let S be
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 this must be an attempt to change the value of an immutable binding so 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) ReturnIfAbrupt (foundBinding).- If foundBinding is
false , returnfalse . - If the withEnvironment flag of envRec is
false , returntrue . - Let unscopables be
Get (bindings, @@unscopables). ReturnIfAbrupt (unscopables).- If
Type (unscopables) is Object, then- Let blocked be
ToBoolean (Get (unscopables, N)). ReturnIfAbrupt (blocked).- If blocked is
true , returnfalse .
- Let blocked be
- 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.
- If D is
true then let configValue betrue otherwise let configValue befalse . - Return
DefinePropertyOrThrow (bindings, N, PropertyDescriptor{[[Value]]:undefined , [[Writable]]:true , [[Enumerable]]:true , [[Configurable]]: configValue}).
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). ReturnIfAbrupt (value).- 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 this binding. If a function is not an super, its function 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]] |
"lexical" | "initialized" | "uninitialized"
|
If the value is "lexical", this is an |
| [[FunctionObject]] | Object |
The function Object whose invocation caused this |
| [[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) | Set the [[thisValue]] and record that it has been initialized. |
| 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 not
"lexical". - If envRec.[[thisBindingStatus]] is
"initialized", throw aReferenceError exception. - Set envRec.[[thisValue]] to V.
- Set envRec.[[thisBindingStatus]] to
"initialized". - 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 not
"lexical". - 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 the value of 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 object. Hence, the object
Global Environment Records have the additional fields listed in
| Field Name | Value | Meaning |
|---|---|---|
| [[ObjectRecord]] |
Object |
Binding object is the global object. It contains global built-in bindings as well as |
| [[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 global object property that may not be shadowed by a global lexically binding. |
| 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 optional 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 object
Environment 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). ReturnIfAbrupt (existingProp).- If existingProp is
true , then- Let status be ObjRec.DeleteBinding(N).
ReturnIfAbrupt (status).- 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 ()
Global Environment Records always provide a this binding whose value is the associated global object.
- 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. - Let ObjRec be envRec.[[ObjectRecord]].
- Let bindings be the binding object for ObjRec.
- Return bindings.
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 the value of 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).
ReturnIfAbrupt (existingProp).- If existingProp is
undefined , returnfalse . - If existingProp.[[Configurable]] is
true , returnfalse . - Return
true .
Properties may exist upon a global object that were directly created rather than being declared using a var or function declaration. A global lexical binding may not be created that has the same name as a non-configurable property of the global object. The global property undefined is an example of such a property.
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). ReturnIfAbrupt (hasProperty).- 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).
ReturnIfAbrupt (existingProp).- If existingProp is
undefined , returnIsExtensible (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). ReturnIfAbrupt (hasProperty).- Let extensible be
IsExtensible (globalObject). ReturnIfAbrupt (extensible).- If hasProperty is
false and extensible istrue , then- Let status be ObjRec.CreateMutableBinding(N, D).
ReturnIfAbrupt (status).- Let status be ObjRec.InitializeBinding(N,
undefined ). ReturnIfAbrupt (status).
- Let varDeclaredNames be envRec.[[VarNames]].
- If varDeclaredNames does not contain the value of 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).
ReturnIfAbrupt (existingProp).- 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 }.
- Let status be
DefinePropertyOrThrow (globalObject, N, desc). ReturnIfAbrupt (status).- Let status be
Set (globalObject, N, V,false ). - Record that the binding for N in ObjRec has been initialized.
ReturnIfAbrupt (status).- Let varDeclaredNames be envRec.[[VarNames]].
- If varDeclaredNames does not contain the value of N, then
- Append N to varDeclaredNames.
- Return
NormalCompletion (empty ).
Global function declarations are always represented as own properties of the global object. If possible, an existing own property is reconfigured to have a standard set of attribute values. Steps 10-12 are equivalent to what calling the InitializeBinding concrete method would do and if globalObject is a Proxy will produce the same sequence of Proxy trap calls.
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
- 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, S).
- If the binding for N in envRec is an uninitialized binding, throw a
ReferenceError exception. - Return the value currently bound to N in envRec.
Because a
8.1.1.5.2 DeleteBinding (N)
The concrete
- Let envRec be the module
Environment Record for which the method was invoked. - If envRec does not have a binding for the name that is the value of N, return
true . - Return
false .
The bindings of a module
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 a
Module 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 abstract operations are used in this specification to operate upon lexical environments:
8.1.2.1 GetIdentifierReference (lex, name, strict)
The abstract operation GetIdentifierReference is called with a
- If lex is the value
null , then- Return a value of type
Reference whose base value isundefined , whose referenced name is name, and whose strict reference flag is strict.
- Return a value of type
- Let envRec be lex's
EnvironmentRecord . - Let exists be envRec.HasBinding(name).
ReturnIfAbrupt (exists).- If exists is
true , then- Return a value of type
Reference whose base value is envRec, whose referenced name is name, and whose strict reference flag is strict.
- Return a value of type
- 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's
EnvironmentRecord to be 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's
EnvironmentRecord 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's [[ThisMode]] internal slot is
lexical , set envRec.[[thisBindingStatus]] to"lexical". - Else, Set envRec.[[thisBindingStatus]] to
"uninitialized". - Let home be the value of F's [[HomeObject]] internal slot.
- Set envRec.[[HomeObject]] to home.
- Set envRec.[[NewTarget]] to newTarget.
- Set env's
EnvironmentRecord to be envRec. - Set the outer lexical environment reference of env to the value of F's [[Environment]] internal slot.
- Return env.
8.1.2.5 NewGlobalEnvironment ( G )
When the abstract operation NewGlobalEnvironment is called with an ECMAScript Object G as its argument, 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.[[DeclarativeRecord]] to dclRec.
- Set globalRec.[[VarNames]] to a new empty
List . - Set env's
EnvironmentRecord to globalRec. - Set the outer lexical environment reference of env to
null - 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's
EnvironmentRecord to be envRec. - Set the outer lexical environment reference of env to E.
- Return env.
8.2 Code Realms
Before it is evaluated, all ECMAScript code must be associated with a Realm. Conceptually, a realm consists of a set of intrinsic objects, an ECMAScript global environment, all of the ECMAScript code that is loaded within the scope of that global environment, and other associated state and resources.
A Realm is specified as a Record with the fields specified in
| Field Name | Value | Meaning |
|---|---|---|
| [[intrinsics]] | Record whose field names are intrinsic keys and whose values are objects | These are the intrinsic values used by code associated with this Realm |
| [[globalThis]] | Object | The global object for this Realm |
| [[globalEnv]] |
|
The global environment for this Realm |
| [[templateMap]] |
A |
Template objects are canonicalized separately for each Realm using its [[templateMap]]. Each [[strings]] value is a |
An implementation may define other, implementation specific fields.
8.2.1 CreateRealm ( )
The abstract operation CreateRealm with no arguments performs the following steps:
- Let realmRec be a new Record.
- Perform
CreateIntrinsics (realmRec). - Set realmRec.[[globalThis]] to
undefined . - Set realmRec.[[globalEnv]] to
undefined . - Set realmRec.[[templateMap]] to a new empty
List . - Return realmRec.
8.2.2 CreateIntrinsics ( realmRec )
When the abstract operation CreateIntrinsics with argument realmRec performs the following steps:
- Let intrinsics be a new Record.
- Set realmRec.[[intrinsics]] to intrinsics.
- Let objProto be
ObjectCreate (null ). - Set intrinsics.[[
%ObjectPrototype% ]] to objProto. - Let throwerSteps be the algorithm steps specified in
9.2.7.1 for the%ThrowTypeError% function. - Let thrower be
CreateBuiltinFunction (realmRec, throwerSteps,null ). - Set intrinsics.[[
%ThrowTypeError% ]] to thrower. - Let noSteps be an empty sequence of algorithm steps.
- Let funcProto be
CreateBuiltinFunction (realmRec, noSteps, objProto). - Set intrinsics.[[
%FunctionPrototype% ]] to funcProto. - Call thrower.[[SetPrototypeOf]](funcProto).
- Perform
AddRestrictedFunctionProperties (funcProto, realmRec). - Set fields of intrinsics with the values listed in
Table 7 that have not already been handled above. 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 (realmRec, <steps>, <prototype>, <slots>) where <steps> is the definition of that function provided by this specification, <prototype> is the specified value of the function's [[Prototype]] internal slot and <slots> is a list of the names, if any, of the functions specified internal slots. The creation of the intrinsics and their properties must be ordered to avoid any dependencies upon objects that have not yet been created. - Return intrinsics.
8.2.3 SetRealmGlobalObject ( realmRec, globalObj )
The abstract operation SetRealmGlobalObject with arguments realmRec and globalObj performs the following steps:
- If globalObj is
undefined , then- Let intrinsics be realmRec.[[intrinsics]].
- Let globalObj be
ObjectCreate (intrinsics.[[%ObjectPrototype% ]]).
- Assert:
Type (globalObj) is Object. - Set realmRec.[[globalThis]] to globalObj.
- Let newGlobalEnv be
NewGlobalEnvironment (globalObj). - 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.[[globalThis]].
- 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 in
18.2 ,18.3 , or18.4 the value of the [[Value]] attribute is the corresponding intrinsic object from realmRec. - Let status be
DefinePropertyOrThrow (global, name, desc). ReturnIfAbrupt (status).
- 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 that is actually executing code. This is known as the running execution context. A stack is used to track execution contexts. The running execution context is always the top element of this stack. A new execution context is created whenever control is transferred from the executable code associated with the currently running execution context to executable code that is not associated with that execution context. The newly created execution context is pushed onto the stack and becomes the running execution context.
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 function object, then the value of this component is that function object. If the context is evaluating the code of a |
|
|
The |
Evaluation of code by the running execution context may be suspended at various points defined within this specification. Once the running execution context has been suspended a different execution context may become the running execution context and commence evaluating its code. At some later time a suspended execution context may again become the running execution context and continue evaluating its code at the point where it had previously been suspended. Transition of the running execution context status among execution contexts usually occurs in stack-like last-in/first-out manner. However, some ECMAScript features require non-LIFO transitions of the running execution context.
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. When an execution context is created its LexicalEnvironment and VariableEnvironment components initially have the same value.
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 running execution context (the top of the execution context stack) is directly manipulated by algorithms within this specification. Hence when the terms “LexicalEnvironment”, and “VariableEnvironment” are used without qualification they are in reference to those components of the running execution context.
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 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 was not passed or if env is
undefined , then- Let env be the running
execution context 's LexicalEnvironment.
- Let env be the running
- Assert: env is a
Lexical 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.2 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.
- Let lex be outer.
- Let envRec be lex's
The loop in step 2 will always terminate because the list of environments always ends with the global environment which has a this binding.
8.3.3 ResolveThisBinding ( )
The abstract operation ResolveThisBinding determines the binding of the keyword this using the LexicalEnvironment of the running
- Let envRec be
GetThisEnvironment ( ). - Return envRec.GetThisBinding().
8.3.4 GetNewTarget ( )
The abstract operation GetNewTarget determines the NewTarget value using the LexicalEnvironment of the running
- Let envRec be
GetThisEnvironment ( ). - Assert: envRec has a [[NewTarget]] field.
- Return envRec.[[NewTarget]].
8.3.5 GetGlobalObject ( )
The abstract operation GetGlobalObject returns the global object used by the currently running
- Let ctx be the running
execution context . - Let currentRealm be ctx's
Realm . - Return currentRealm.[[globalThis]].
8.4 Jobs and Job Queues
A Job is an abstract operation that initiates an ECMAScript computation when no other ECMAScript computation is currently in progress. A Job abstract operation may be defined to accept an arbitrary set of job parameters.
Execution of a Job can be initiated only when there is no running
| Field Name | Value | Meaning |
|---|---|---|
| [[Job]] | The name of a Job abstract operation |
This is the abstract operation that is performed when execution of this PendingJob is initiated. Jobs are abstract operations that use |
| [[Arguments]] |
A |
The |
| [[Realm]] |
A |
The |
| [[HostDefined]] |
Any, default value is |
Field reserved for use by host environments that need to associate additional information with a pending Job. |
A Job Queue is a FIFO queue of PendingJob records. Each Job Queue has a name and the full set of available Job Queues are defined by an ECMAScript implementation. Every ECMAScript implementation has at least the Job Queues defined in
| Name | Purpose |
|---|---|
| ScriptJobs |
Jobs that validate and evaluate ECMAScript |
| PromiseJobs |
Jobs that are responses to the settlement of a Promise (see |
A request for the future execution of a Job is made by enqueueing, on a Job Queue, a PendingJob record that includes a Job abstract operation name and any necessary argument values. When there is no running
The PendingJob records from a single Job Queue are always initiated in FIFO order. This specification does not define the order in which multiple Job Queues are serviced. An ECMAScript implementation may interweave the FIFO evaluation of the PendingJob records of a Job Queue with the evaluation of the PendingJob records of one or more other Job Queues. An implementation must define what occurs when there are no running
Typically an ECMAScript implementation will have its Job Queues pre-initialized with at least one PendingJob and one of those Jobs will be the first to be executed. An implementation might choose to free all resources and terminate if the current Job completes and all Job Queues are empty. Alternatively, it might choose to wait for a some implementation specific agent or mechanism to enqueue new PendingJob requests.
The following abstract operations are used to create and manage Jobs and Job Queues:
8.4.1 EnqueueJob (queueName, job, arguments)
The EnqueueJob abstract operation requires three arguments: queueName, job, and arguments. It performs the following steps:
- Assert:
Type (queueName) is String and its value is the name of a Job Queue recognized by this implementation. - Assert: job is the name of a Job.
- Assert: arguments is a
List that has the same number of elements as the number of parameters required by job. - Let callerContext be the running
execution context . - Let callerRealm be callerContext's
Realm . - Let pending be PendingJob{ [[Job]]: job, [[Arguments]]: arguments, [[Realm]]: callerRealm, [[HostDefined]]:
undefined }. - Perform any implementation or host environment defined processing of pending. This may include modifying the [[HostDefined]] field or any other field of pending.
- Add pending at the back of the Job Queue named by queueName.
- Return
NormalCompletion (empty ).
8.4.2 NextJob result
An algorithm step such as:
- NextJob result.
is used in Job abstract operations in place of:
- Return result.
Job abstract operations must not contain a Return step or a
- If result is an
abrupt completion , perform implementation defined unhandled exception processing. - Suspend the running
execution context and remove it from theexecution context stack. - Assert: The
execution context stack is now empty. - Let nextQueue be a non-empty Job Queue chosen in an implementation defined manner. If all Job Queues are empty, the result is implementation defined.
- Let nextPending be the PendingJob record at the front of nextQueue. Remove that record from nextQueue.
- Let newContext be a new
execution context . - Set newContext's
Realm to nextPending.[[Realm]]. - Push newContext onto the
execution context stack; newContext is now the runningexecution context . - Perform any implementation or host environment defined job initialization using nextPending.
- Perform the abstract operation named by nextPending.[[Job]] using the elements of nextPending.[[Arguments]] as its arguments.
8.5 ECMAScript Initialization()
An ECMAScript implementation performs the following steps prior to the execution of any Jobs or the evaluation of any ECMAScript code:
- Let realm be
CreateRealm (). - Let newContext be a new
execution context . - Set the Function of newContext to
null . - Set the
Realm of newContext to realm. - Push newContext onto the
execution context stack; newContext is now the runningexecution context . - Let status be
InitializeHostDefinedRealm (realm). - If status is an
abrupt completion , then- Assert: The first realm could not be created.
- Terminate ECMAScript execution.
- In an implementation dependent manner, obtain the ECMAScript source texts (see clause
10 ) for zero or more ECMAScript scripts and/or ECMAScript modules. For each such sourceText do,- If sourceText is the source code of a script, then
- Perform
EnqueueJob ("ScriptJobs",ScriptEvaluationJob , « sourceText »).
- Perform
- Else sourceText is the source code of a module,
- Perform
EnqueueJob ("ScriptJobs",TopLevelModuleEvaluationJob , « sourceText »).
- Perform
- If sourceText is the source code of a script, then
NextJob NormalCompletion (undefined ).
8.5.1 InitializeHostDefinedRealm ( realm )
The abstract operation InitializeHostDefinedRealm with parameter realm performs the following steps:
- If this implementation requires use of an exotic object to serve as realm's global object, let global be such an object created in an implementation defined manner. Otherwise, let global be
undefined indicating that an ordinary object should be created as the global object. - Perform
SetRealmGlobalObject (realm, global). - Let globalObj be
SetDefaultGlobalBindings (realm). ReturnIfAbrupt (globalObj).- Create any implementation defined global object properties on globalObj.
- Return
NormalCompletion (undefined ).