9.1 Environment Records
Environment Record is a specification type used to define the association of
Every Environment Record has an [[OuterEnv]] field, which is either
Environment Records are purely specification mechanisms and need not correspond to any specific artefact of an ECMAScript implementation. It is impossible for an ECMAScript program to directly access or manipulate such values.
9.1.1 The Environment Record Type Hierarchy
-
Environment Record (abstract)-
A
Declarative Environment Record is used to define the effect of ECMAScript language syntactic elements such asFunctionDeclaration s,VariableDeclaration s, andCatch clauses that directly associate identifier bindings withECMAScript language values .-
A
Function Environment Record corresponds to the invocation of an ECMAScriptfunction object , and contains bindings for the top-level declarations within that function. It may establish a newthis
binding. It also captures the state necessary to supportsuper
method invocations. -
A
Module Environment Record contains the bindings for the top-level declarations of aModule . It also contains the bindings that are explicitly imported by theModule . Its [[OuterEnv]] is aGlobal Environment Record .
-
-
An
Object Environment Record is used to define the effect of ECMAScript elements such asWithStatement that associate identifier bindings with the properties of some object. -
A
Global Environment Record is used forScript global declarations. It does not have an outer environment; its [[OuterEnv]] isnull . It may be prepopulated with identifier bindings and it includes an associatedglobal object whose properties provide some of the global environment's identifier bindings. As ECMAScript code is executed, additional properties may be added to theglobal object and the initial properties may be modified.
-
The
Method | Purpose |
---|---|
HasBinding(N) |
Determine if an |
CreateMutableBinding(N, D) |
Create a new but uninitialized mutable binding in an |
CreateImmutableBinding(N, S) |
Create a new but uninitialized immutable binding in an |
InitializeBinding(N, V) |
Set the value of an already existing but uninitialized binding in an |
SetMutableBinding(N, V, S) |
Set the value of an already existing mutable binding in an |
GetBindingValue(N, S) |
Returns the value of an already existing binding from an |
DeleteBinding(N) |
Delete a binding from an |
HasThisBinding() |
Determine if an this binding. Return |
HasSuperBinding() |
Determine if an super method binding. Return |
WithBaseObject() |
If this with statement, return the with object. Otherwise, return |
9.1.1.1 Declarative Environment Records
Each Declarative Environment Record is associated with an ECMAScript program scope containing variable, constant, let, class, module, import, and/or function declarations. A Declarative Environment Record binds the set of identifiers defined by the declarations contained within its scope.
The behaviour of the concrete specification methods for Declarative Environment Records is defined by the following algorithms.
9.1.1.1.1 HasBinding ( N )
The HasBinding concrete method of a
- If envRec has a binding for N, return
true . - Return
false .
9.1.1.1.2 CreateMutableBinding ( N, D )
The CreateMutableBinding concrete method of a
Assert : envRec does not already have a binding for N.- Create a mutable binding in envRec for N and record that it is uninitialized. If D is
true , record that the newly created binding may be deleted by a subsequent DeleteBinding call. - Return
unused .
9.1.1.1.3 CreateImmutableBinding ( N, S )
The CreateImmutableBinding concrete method of a
Assert : envRec does not already have a binding for N.- Create an immutable binding in envRec for N and record that it is uninitialized. If S is
true , record that the newly created binding is a strict binding. - Return
unused .
9.1.1.1.4 InitializeBinding ( N, V )
The InitializeBinding concrete method of a
Assert : envRec must have an uninitialized binding for N.- Set the bound value for N in envRec to V.
Record that the binding for N in envRec has been initialized.- Return
unused .
9.1.1.1.5 SetMutableBinding ( N, V, S )
The SetMutableBinding concrete method of a
- If envRec does not have a binding for N, then
- If S is
true , throw aReferenceError exception. - Perform ! envRec.CreateMutableBinding(N,
true ). - Perform ! envRec.InitializeBinding(N, V).
- Return
unused .
- 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, then
- Throw a
ReferenceError exception.
- Throw a
- Else if the binding for N in envRec is a mutable binding, then
- 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
unused .
An example of ECMAScript code that results in a missing binding at step
function f() { eval("var x; x = (delete x, 0);"); }
9.1.1.1.6 GetBindingValue ( N, S )
The GetBindingValue concrete method of a
Assert : envRec has a binding for N.- If the binding for N in envRec is an uninitialized binding, throw a
ReferenceError exception. - Return the value currently bound to N in envRec.
9.1.1.1.7 DeleteBinding ( N )
The DeleteBinding concrete method of a
Assert : envRec has a binding for N.- If the binding for N in envRec cannot be deleted, return
false . - Remove the binding for N from envRec.
- Return
true .
9.1.1.1.8 HasThisBinding ( )
The HasThisBinding concrete method of a
- Return
false .
A regular this
binding.
9.1.1.1.9 HasSuperBinding ( )
The HasSuperBinding concrete method of a
- Return
false .
A regular super
binding.
9.1.1.1.10 WithBaseObject ( )
The WithBaseObject concrete method of a
- Return
undefined .
9.1.1.2 Object Environment Records
Each Object Environment Record is associated with an object called its binding object. An Object Environment Record binds the set of string identifier names that directly correspond to the property names of its binding object.
Object Environment Records created for with
statements (
Object Environment Records have the additional state fields listed in
Field Name | Value | Meaning |
---|---|---|
[[BindingObject]] | an Object |
The binding object of this |
[[IsWithEnvironment]] | a Boolean |
Indicates whether this with statement.
|
The behaviour of the concrete specification methods for Object Environment Records is defined by the following algorithms.
9.1.1.2.1 HasBinding ( N )
The HasBinding concrete method of an
- Let bindingObject be envRec.[[BindingObject]].
- Let foundBinding be ?
HasProperty (bindingObject, N). - If foundBinding is
false , returnfalse . - If envRec.[[IsWithEnvironment]] is
false , returntrue . - Let unscopables be ?
Get (bindingObject,@@unscopables ). - If unscopables
is an Object , then - Return
true .
9.1.1.2.2 CreateMutableBinding ( N, D )
The CreateMutableBinding concrete method of an
- Let bindingObject be envRec.[[BindingObject]].
- Perform ?
DefinePropertyOrThrow (bindingObject, N, PropertyDescriptor { [[Value]]:undefined , [[Writable]]:true , [[Enumerable]]:true , [[Configurable]]: D }). - Return
unused .
Normally envRec will not have a binding for N but if it does, the semantics of
9.1.1.2.3 CreateImmutableBinding ( N, S )
The CreateImmutableBinding concrete method of an
9.1.1.2.4 InitializeBinding ( N, V )
The InitializeBinding concrete method of an
- Perform ? envRec.SetMutableBinding(N, V,
false ). - Return
unused .
In this specification, all uses of CreateMutableBinding for
9.1.1.2.5 SetMutableBinding ( N, V, S )
The SetMutableBinding concrete method of an
- Let bindingObject be envRec.[[BindingObject]].
- Let stillExists be ?
HasProperty (bindingObject, N). - If stillExists is
false and S istrue , throw aReferenceError exception. - Perform ?
Set (bindingObject, N, V, S). - Return
unused .
9.1.1.2.6 GetBindingValue ( N, S )
The GetBindingValue concrete method of an
- Let bindingObject be envRec.[[BindingObject]].
- Let value be ?
HasProperty (bindingObject, N). - If value is
false , then- If S is
false , returnundefined ; otherwise throw aReferenceError exception.
- If S is
- Return ?
Get (bindingObject, N).
9.1.1.2.7 DeleteBinding ( N )
The DeleteBinding concrete method of an
- Let bindingObject be envRec.[[BindingObject]].
- Return ? bindingObject.[[Delete]](N).
9.1.1.2.8 HasThisBinding ( )
The HasThisBinding concrete method of an
- Return
false .
this
binding.
9.1.1.2.9 HasSuperBinding ( )
The HasSuperBinding concrete method of an
- Return
false .
super
binding.
9.1.1.2.10 WithBaseObject ( )
The WithBaseObject concrete method of an
- If envRec.[[IsWithEnvironment]] is
true , return envRec.[[BindingObject]]. - Otherwise, return
undefined .
9.1.1.3 Function Environment Records
A Function Environment Record is a this
binding. If a function is not an super
, its Function Environment Record also contains the state that is used to perform super
method invocations from within the function.
Function Environment Records have the additional state fields listed in
Field Name | Value | Meaning |
---|---|---|
[[ThisValue]] |
an |
This is the |
[[ThisBindingStatus]] |
|
If the value is |
[[FunctionObject]] |
an ECMAScript |
The |
[[NewTarget]] |
an Object or |
If this |
Function Environment Records support all of the
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 |
The behaviour of the additional concrete specification methods for Function Environment Records is defined by the following algorithms:
9.1.1.3.1 BindThisValue ( V )
The BindThisValue concrete method of a
Assert : envRec.[[ThisBindingStatus]] is notlexical .- If envRec.[[ThisBindingStatus]] is
initialized , throw aReferenceError exception. - Set envRec.[[ThisValue]] to V.
- Set envRec.[[ThisBindingStatus]] to
initialized . - Return V.
9.1.1.3.2 HasThisBinding ( )
The HasThisBinding concrete method of a
- If envRec.[[ThisBindingStatus]] is
lexical , returnfalse ; otherwise, returntrue .
9.1.1.3.3 HasSuperBinding ( )
The HasSuperBinding concrete method of a
- If envRec.[[ThisBindingStatus]] is
lexical , returnfalse . - If envRec.[[FunctionObject]].[[HomeObject]] is
undefined , returnfalse ; otherwise, returntrue .
9.1.1.3.4 GetThisBinding ( )
The GetThisBinding concrete method of a
Assert : envRec.[[ThisBindingStatus]] is notlexical .- If envRec.[[ThisBindingStatus]] is
uninitialized , throw aReferenceError exception. - Return envRec.[[ThisValue]].
9.1.1.3.5 GetSuperBase ( )
The GetSuperBase concrete method of a
- Let home be envRec.[[FunctionObject]].[[HomeObject]].
- If home is
undefined , returnundefined . Assert : homeis an Object .- Return ? home.[[GetPrototypeOf]]().
9.1.1.4 Global Environment Records
A Global Environment Record is used to represent the outer most scope that is shared by all of the ECMAScript
A Global Environment Record is logically a single record but it is specified as a composite encapsulating an
Properties may be created directly on a
Global Environment Records have the additional fields listed in
Field Name | Value | Meaning |
---|---|---|
[[ObjectRecord]] |
an |
Binding object is the |
[[GlobalThisValue]] | an Object |
The value returned by this in global scope. |
[[DeclarativeRecord]] |
a |
|
[[VarNames]] |
a |
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 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 function . The String value N is the bound name. V is the initialization value. If the Boolean argument D is |
The behaviour of the concrete specification methods for Global Environment Records is defined by the following algorithms.
9.1.1.4.1 HasBinding ( N )
The HasBinding concrete method of a
- Let DclRec be envRec.[[DeclarativeRecord]].
- If ! DclRec.HasBinding(N) is
true , returntrue . - Let ObjRec be envRec.[[ObjectRecord]].
- Return ? ObjRec.HasBinding(N).
9.1.1.4.2 CreateMutableBinding ( N, D )
The CreateMutableBinding concrete method of a
- Let DclRec be envRec.[[DeclarativeRecord]].
- If ! DclRec.HasBinding(N) is
true , throw aTypeError exception. - Return ! DclRec.CreateMutableBinding(N, D).
9.1.1.4.3 CreateImmutableBinding ( N, S )
The CreateImmutableBinding concrete method of a
- Let DclRec be envRec.[[DeclarativeRecord]].
- If ! DclRec.HasBinding(N) is
true , throw aTypeError exception. - Return ! DclRec.CreateImmutableBinding(N, S).
9.1.1.4.4 InitializeBinding ( N, V )
The InitializeBinding concrete method of a
- Let DclRec be envRec.[[DeclarativeRecord]].
- If ! DclRec.HasBinding(N) is
true , then- Return ! DclRec.InitializeBinding(N, V).
Assert : If the binding exists, it must be in theObject Environment Record .- Let ObjRec be envRec.[[ObjectRecord]].
- Return ? ObjRec.InitializeBinding(N, V).
9.1.1.4.5 SetMutableBinding ( N, V, S )
The SetMutableBinding concrete method of a
- Let DclRec be envRec.[[DeclarativeRecord]].
- If ! DclRec.HasBinding(N) is
true , then- Return ? DclRec.SetMutableBinding(N, V, S).
- Let ObjRec be envRec.[[ObjectRecord]].
- Return ? ObjRec.SetMutableBinding(N, V, S).
9.1.1.4.6 GetBindingValue ( N, S )
The GetBindingValue concrete method of a
- Let DclRec be envRec.[[DeclarativeRecord]].
- If ! DclRec.HasBinding(N) is
true , then- Return ? DclRec.GetBindingValue(N, S).
- Let ObjRec be envRec.[[ObjectRecord]].
- Return ? ObjRec.GetBindingValue(N, S).
9.1.1.4.7 DeleteBinding ( N )
The DeleteBinding concrete method of a
- Let DclRec be envRec.[[DeclarativeRecord]].
- If ! DclRec.HasBinding(N) is
true , then- Return ! DclRec.DeleteBinding(N).
- Let ObjRec be envRec.[[ObjectRecord]].
- Let globalObject be ObjRec.[[BindingObject]].
- Let existingProp be ?
HasOwnProperty (globalObject, N). - If existingProp is
true , then- Let status be ? ObjRec.DeleteBinding(N).
- If status is
true and envRec.[[VarNames]] contains N, then- Remove N from envRec.[[VarNames]].
- Return status.
- Return
true .
9.1.1.4.8 HasThisBinding ( )
The HasThisBinding concrete method of a
- Return
true .
this
binding.
9.1.1.4.9 HasSuperBinding ( )
The HasSuperBinding concrete method of a
- Return
false .
super
binding.
9.1.1.4.10 WithBaseObject ( )
The WithBaseObject concrete method of a
- Return
undefined .
9.1.1.4.11 GetThisBinding ( )
The GetThisBinding concrete method of a
- Return envRec.[[GlobalThisValue]].
9.1.1.4.12 HasVarDeclaration ( N )
The HasVarDeclaration concrete method of a
- Let varDeclaredNames be envRec.[[VarNames]].
- If varDeclaredNames contains N, return
true . - Return
false .
9.1.1.4.13 HasLexicalDeclaration ( N )
The HasLexicalDeclaration concrete method of a
- Let DclRec be envRec.[[DeclarativeRecord]].
- Return ! DclRec.HasBinding(N).
9.1.1.4.14 HasRestrictedGlobalProperty ( N )
The HasRestrictedGlobalProperty concrete method of a
- Let ObjRec be envRec.[[ObjectRecord]].
- Let globalObject be ObjRec.[[BindingObject]].
- Let existingProp be ? globalObject.[[GetOwnProperty]](N).
- If existingProp is
undefined , returnfalse . - If existingProp.[[Configurable]] is
true , returnfalse . - Return
true .
Properties may exist upon a
9.1.1.4.15 CanDeclareGlobalVar ( N )
The CanDeclareGlobalVar concrete method of a
- Let ObjRec be envRec.[[ObjectRecord]].
- Let globalObject be ObjRec.[[BindingObject]].
- Let hasProperty be ?
HasOwnProperty (globalObject, N). - If hasProperty is
true , returntrue . - Return ?
IsExtensible (globalObject).
9.1.1.4.16 CanDeclareGlobalFunction ( N )
The CanDeclareGlobalFunction concrete method of a
- Let ObjRec be envRec.[[ObjectRecord]].
- Let globalObject be ObjRec.[[BindingObject]].
- Let existingProp be ? globalObject.[[GetOwnProperty]](N).
- If existingProp is
undefined , return ?IsExtensible (globalObject). - If existingProp.[[Configurable]] is
true , returntrue . - If
IsDataDescriptor (existingProp) istrue and existingProp has attribute values { [[Writable]]:true , [[Enumerable]]:true }, returntrue . - Return
false .
9.1.1.4.17 CreateGlobalVarBinding ( N, D )
The CreateGlobalVarBinding concrete method of a
- Let ObjRec be envRec.[[ObjectRecord]].
- Let globalObject be ObjRec.[[BindingObject]].
- 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 ).
- If envRec.[[VarNames]] does not contain N, then
- Append N to envRec.[[VarNames]].
- Return
unused .
9.1.1.4.18 CreateGlobalFunctionBinding ( N, V, D )
The CreateGlobalFunctionBinding concrete method of a
- Let ObjRec be envRec.[[ObjectRecord]].
- Let globalObject be ObjRec.[[BindingObject]].
- Let existingProp be ? globalObject.[[GetOwnProperty]](N).
- If existingProp is
undefined or existingProp.[[Configurable]] istrue , then- Let desc be the PropertyDescriptor { [[Value]]: V, [[Writable]]:
true , [[Enumerable]]:true , [[Configurable]]: D }.
- Let desc be the PropertyDescriptor { [[Value]]: V, [[Writable]]:
- Else,
- Let desc be the PropertyDescriptor { [[Value]]: V }.
- Perform ?
DefinePropertyOrThrow (globalObject, N, desc). - Perform ?
Set (globalObject, N, V,false ). - If envRec.[[VarNames]] does not contain N, then
- Append N to envRec.[[VarNames]].
- Return
unused .
Global function declarations are always represented as own properties of the
9.1.1.5 Module Environment Records
A Module Environment Record is a
Module Environment Records support all of the
Method | Purpose |
---|---|
CreateImportBinding(N, M, N2) |
Create an immutable indirect binding in a |
GetThisBinding() |
Return the value of this this binding.
|
The behaviour of the additional concrete specification methods for Module Environment Records are defined by the following algorithms:
9.1.1.5.1 GetBindingValue ( N, S )
The GetBindingValue concrete method of a
Assert : S istrue .Assert : envRec has a binding for N.- If the binding for N is an indirect binding, then
- Let M and N2 be the indirection values provided when this binding for N was created.
- Let targetEnv be M.[[Environment]].
- If targetEnv is
empty , throw aReferenceError exception. - Return ? targetEnv.GetBindingValue(N2,
true ).
- If the binding for N in envRec is an uninitialized binding, throw a
ReferenceError exception. - Return the value currently bound to N in envRec.
S will always be
9.1.1.5.2 DeleteBinding ( N )
The DeleteBinding concrete method of a
9.1.1.5.3 HasThisBinding ( )
The HasThisBinding concrete method of a
- Return
true .
this
binding.
9.1.1.5.4 GetThisBinding ( )
The GetThisBinding concrete method of a
- Return
undefined .
9.1.1.5.5 CreateImportBinding ( N, M, N2 )
The CreateImportBinding concrete method of a
9.1.2 Environment Record Operations
The following
9.1.2.1 GetIdentifierReference ( env, name, strict )
The abstract operation GetIdentifierReference takes arguments env (an
- If env is
null , then- Return the
Reference Record { [[Base]]:unresolvable , [[ReferencedName]]: name, [[Strict]]: strict, [[ThisValue]]:empty }.
- Return the
- Let exists be ? env.HasBinding(name).
- If exists is
true , then- Return the
Reference Record { [[Base]]: env, [[ReferencedName]]: name, [[Strict]]: strict, [[ThisValue]]:empty }.
- Return the
- Else,
- Let outer be env.[[OuterEnv]].
- Return ?
GetIdentifierReference (outer, name, strict).
9.1.2.2 NewDeclarativeEnvironment ( E )
The abstract operation NewDeclarativeEnvironment takes argument E (an
- Let env be a new
Declarative Environment Record containing no bindings. - Set env.[[OuterEnv]] to E.
- Return env.
9.1.2.3 NewObjectEnvironment ( O, W, E )
The abstract operation NewObjectEnvironment takes arguments O (an Object), W (a Boolean), and E (an
- Let env be a new
Object Environment Record . - Set env.[[BindingObject]] to O.
- Set env.[[IsWithEnvironment]] to W.
- Set env.[[OuterEnv]] to E.
- Return env.
9.1.2.4 NewFunctionEnvironment ( F, newTarget )
The abstract operation NewFunctionEnvironment takes arguments F (an ECMAScript
- Let env be a new
Function Environment Record containing no bindings. - Set env.[[FunctionObject]] to F.
- If F.[[ThisMode]] is
lexical , set env.[[ThisBindingStatus]] tolexical . - Else, set env.[[ThisBindingStatus]] to
uninitialized . - Set env.[[NewTarget]] to newTarget.
- Set env.[[OuterEnv]] to F.[[Environment]].
- Return env.
9.1.2.5 NewGlobalEnvironment ( G, thisValue )
The abstract operation NewGlobalEnvironment takes arguments G (an Object) and thisValue (an Object) and returns a
- Let objRec be
NewObjectEnvironment (G,false ,null ). - Let dclRec be
NewDeclarativeEnvironment (null ). - Let env be a new
Global Environment Record . - Set env.[[ObjectRecord]] to objRec.
- Set env.[[GlobalThisValue]] to thisValue.
- Set env.[[DeclarativeRecord]] to dclRec.
- Set env.[[VarNames]] to a new empty
List . - Set env.[[OuterEnv]] to
null . - Return env.
9.1.2.6 NewModuleEnvironment ( E )
The abstract operation NewModuleEnvironment takes argument E (an
- Let env be a new
Module Environment Record containing no bindings. - Set env.[[OuterEnv]] to E.
- Return env.