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 function object. A 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) |
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). - 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.
- If D is
true , 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). - 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 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 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). - 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 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).
- If existingProp is
undefined , returnfalse . - If existingProp.[[Configurable]] is
true , returnfalse . - Return
true .
Properties may exist upon a 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). - 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 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).
- 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 the value of 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
- 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 - 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's
EnvironmentRecord 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'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 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, 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 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 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 |
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
Realm Record . - Perform
CreateIntrinsics (realmRec). - Set realmRec.[[GlobalObject]] 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 function's 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, 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]].
- Let globalObj be
ObjectCreate (intrinsics.[[%ObjectPrototype% ]]).
- Assert:
Type (globalObj) is Object. - If thisValue is
undefined , let thisValue be 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 in
18.2 ,18.3 , or18.4 the value of the [[Value]] attribute is the corresponding intrinsic object from realmRec. - Perform ?
DefinePropertyOrThrow (global, name, desc).
- 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.
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 function object, then the value of this component is that function object. If the 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. 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
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 Function component's [[ScriptOrModule]] component is notnull . - If such an
execution context exists, return ec's Function component's [[ScriptOrModule]] slot's value. - Otherwise, let ec be the
running execution context . - Assert: ec's ScriptOrModule component is not
null . - Return ec's ScriptOrModule component.
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 was not passed or if env is
undefined , then- Let env be the
running execution context 's LexicalEnvironment.
- Let env be the
- 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.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.
- 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 this binding.
8.3.4 ResolveThisBinding ( )
The abstract operation ResolveThisBinding determines the binding of the keyword 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 ctx be the
running execution context . - Let currentRealm be ctx's
Realm . - Return currentRealm.[[GlobalObject]].
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
| 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 |
| [[ScriptOrModule]] |
A |
The script or module for the initial |
| [[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
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
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 callerScriptOrModule be callerContext's ScriptOrModule.
- Let pending be PendingJob{ [[Job]]: job, [[Arguments]]: arguments, [[Realm]]: callerRealm, [[ScriptOrModule]]: callerScriptOrModule, [[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
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 , performHostReportErrors (« result.[[Value]] »). - 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 Function to
null . - Set newContext's
Realm to nextPending.[[Realm]]. - Set newContext's ScriptOrModule to nextPending.[[ScriptOrModule]].
- Push newContext onto the
execution context stack ; newContext is now therunning execution 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 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 to
null . - Set the
Realm of newContext to realm. - Set the ScriptOrModule of newContext to
null . - 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's
global object , let global be such an object created in an implementation defined manner. Otherwise, let global beundefined , indicating that an ordinary 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. - In an implementation dependent manner, obtain the ECMAScript source texts (see clause
10 ) and any associated host-defined values for zero or more ECMAScript scripts and/or ECMAScript modules. For each such sourceText and hostDefined,- If sourceText is the source code of a script, then
- Perform
EnqueueJob ("ScriptJobs",ScriptEvaluationJob , « sourceText, hostDefined »).
- Perform
- Else sourceText is the source code of a module,
- Perform
EnqueueJob ("ScriptJobs",TopLevelModuleEvaluationJob , « sourceText, hostDefined »).
- Perform
- If sourceText is the source code of a script, then
NextJob NormalCompletion (undefined ).