Skip to content

10 Execution Contexts

When control is transferred to ECMAScript executable code, control is entering an execution context . Active execution contexts logically form a stack. The top execution context on this logical stack is the running execution context.

10.1 Definitions

10.1.1 Function Objects

There are four types of function objects:

  • -Declared functions are defined in source text by a FunctionDeclaration .
  • - Anonymous functions are created dynamically by using the built-in Function object as a constructor, which is referred to as an instantiating Function .
  • - Implementation-supplied functions are created at the request of the host with source text supplied by the host. The mechanism for their creation is implementation-dependent. Implementation-supplied functions may have any subset of the following attributes {ImplicitThis, ImplicitParents }. Note that these are attributes of function objects, not of properties. The use of these attributes is described in section 10.2.4.
  • - Internal functions are built-in objects of the language, such as parseInt and Math.exp . An implementation may also provide implementation-dependent internal functions that are not described in this specification. These functions do not contain executable code defined by the ECMAScript grammar, so are excluded from this discussion of execution contexts.

10.1.2 Types of Executable Code

There are five types of executable ECMAScript source text:

  • - Global code is source text that is outside all function declarations. More precisely, the global code of a particular ECMAScript Program consists of all SourceElements in the Program production, which come from the Statement definition.
  • - Eval code is the source text supplied to the built-in eval function. More precisely, if the parameter to the built-in eval function is a string, it is treated as an ECMAScript Program . The eval code for a particular invocation of eval is the global code portion of the string parameter.
  • - Function code is source text that is inside a function declaration. More precisely, the function code of a particular ECMAScript FunctionDeclaration consists of the Block in the definition of FunctionDeclaration .
  • - Anonymous code is the source text supplied when instantiating Function . More precisely, the last parameter provided in an instantiation of Function is converted to a string and treated as the StatementList of the Block of a FunctionDeclaration . If more than one parameter is provided in an instantiation of Function , all parameters except the last one are converted to strings and concatenated together, separated by commas. The resulting string is interpreted as the FormalParameterList of a FunctionDeclaration for the StatementList defined by the last parameter.
  • - Implementation-supplied code is the source text supplied by the host when creating an implementation-supplied function. The source text is treated as the StatementList of the Block of a FunctionDeclaration . Depending on the implementation, the host may also supply a FormalParameterList .

10.1.3 Variable instantiation

Every execution context has associated with it a variable object. Variables declared in the source text are added as properties of the variable object. For global and eval code, functions defined in the source text are added as properties of the variable object. Function declarations in other types of code are not allowed by the grammar. For function, anonymous, and implementation-supplied code, parameters are added as properties of the variable object.

Which object is used as the variable object and what attributes are used for the properties depends on the type of code, but the remainder of the behaviour is generic:

  • - For each FunctionDeclaration in the code, in source text order, instantiate a declared function from the FunctionDeclaration and create a property of the variable object whose name is the Identifier in the FunctionDeclaration , whose value is the declared function and whose attributes are determined by the type of code. If the variable object already has a property with this name, replace its value and attributes.
  • - For each formal parameter, as defined in the FormalParameterList , create a property of the variable object whose name is the Identifier and whose attributes are determined by the type of code. The values of the parameters are supplied by the caller. If the caller supplies fewer parameter values than there are formal parameters, the extra formal parameters have value undefined . If two or more formal parameters share the same name, hence the same property, the corresponding property is given the value that was supplied for the last parameter with this name. If the value of this last parameter was not supplied by the caller, the value of the corresponding property is undefined .
  • - For each VariableDeclaration in the code, create a property of the variable object whose name is the Identifier in VariableDeclaration , whose value is undefined and whose attributes are determined by the type of code. If there is already a property of the variable object with the name of a declared variable, the value of the property and its attributes are not changed. Semantically, this step must follow the creation of the FunctionDeclaration and FormalParameterList properties. In particular, if a declared variable has the same name as a declared function or formal parameter, the variable declaration does not disturb the existing property.

10.1.4 Scope Chain and Identifier Resolution

Every execution context has associated with it a scope chain . This is logically a list of objects that are searched when binding an Identifier . When control enters an execution context, the scope chain is created and is populated with an initial set of objects, depending on the type of code. When control leaves the execution context, the scope chain is destroyed.

During execution, the scope chain of the execution context is affected only by WithStatement . When execution enters a with block, the object specified in the with statement is added to the front of the scope chain. When execution leaves a with block, whether normally or via a break or continue statement, the object is removed from the scope chain. The object being removed will always be the first object in the scope chain.

During execution, the syntactic production PrimaryExpression : Identifier is evaluated using the following algorithm:

  1. Get the next object in the scope chain. If there isn't one, go to step 5.
  2. Call the [[HasProperty]] method of Result(l), passing the Identifier as the property.
  3. If Result(2) is true , return a value of type Reference whose base object is Result(l) and whose property name is the Identifier .
  4. Go to step 1.
  5. Return a value of type Reference whose base object is null and whose property name is the Identifier .

The result of binding an identifier is always a value of type Reference with its member name component equal to the identifier string.

10.1.5 Global Object

There is a unique global object , which is created before control enters any execution context. Initially the global object has the following properties:

  • -Built-in objects such as Math, String, Date, parseInt, etc. These have attributes { DontEnum }.
  • - Additional host defined properties. This may include a property whose value is the global object itself, for example window in HTML.

As control enters execution contexts, and as ECMAScript code is executed, additional properties may be added to the global object and the initial properties may be changed.

10.1.6 Activation object

When control enters an execution context for declared function code, anonymous code or implementation-supplied code, an object called the activation object is created and associated with the execution context. The activation object is initialised with a property with name arguments and property attributes { DontDelete }. The initial value of this property is the arguments object described below.

The activation object is then used as the variable object for the purposes of variable instantiation.

When a value is to be returned from the call to a function, its activation object is no longer needed and may be permanently decommissioned.

The activation object is purely a specification mechanism. It is impossible for an ECMAScript program to access the activation object. It can access members of the activation object, but not the activation object itself. When the call operation is applied to a Reference value whose base object is an activation object, null is used as the this value of the call.

10.1.7 This

There is a this value associated with every active execution context. The this value depends on the caller and the type of code being executed and is determined when control enters the execution context. The this value associated with an execution context is immutable.

10.1.8 Arguments Object

When control enters an execution context for declared function code, anonymous code, or implementation-supplied code, an arguments object is created and initialised as follows:

  • - The value of the internal [[Prototype]] property of the arguments object is the original Object prototype object, the one that is the initial value of Object.prototype (section 15.2.3.1).
  • - A property is created with name callee and property attributes { DontEnum }. The initial value of this property is the function object being executed. This allows anonymous functions to be recursive.
  • - A property is created with name length and property attributes { DontEnum }. The initial value of this property is the number of actual parameter values supplied by the caller.
  • - For each non-negative integer, iarg , less than the value of the length property, a property is created with name ToString( iarg ) and property attributes { DontEnum }. The initial value of this property is the value of the corresponding actual parameter supplied by the caller. The first actual parameter value corresponds to iarg = 0, the second to iarg = 1 and so on. In the case when iarg is less than the number of formal parameters for the function object, this property shares its value with the corresponding property of the activation object. This means that changing this property changes the corresponding property of the activation object and vice versa. The value sharing mechanism depends on the implementation.

10.2 Entering An Execution Context

When control enters an execution context, the scope chain is created and initialised, variable instantiation is performed, and the this value is determined.

The initialisation of the scope chain, variable instantiation, and the determination of the this value depend on the type of code being entered.

10.2.1 Global Code

  • -The scope chain is created and initialised to contain the global object and no others.
  • - Variable instantiation is performed using the global object as the variable object and using empty property attributes.
  • -The this value is the global object.

10.2.2 Eval Code

When control enters an execution context for eval code, the previous active execution context, referred to as the calling context , is used to determine the scope chain, the variable object, and the this value.

If there is no calling context, then initialising the scope chain, variable instantiation, and determination of the this value are performed just as for global code.

  • - The scope chain is initialised to contain the same objects, in the same order, as the calling context's scope chain. This includes objects added to the calling context's scope chain by WithStatement.
  • - Variable instantiation is performed using the calling context's variable object and using empty property attributes.
  • -The this value is the same as the this value of the calling context.

10.2.3 Function and Anonymous Code

  • -The scope chain is initialised to contain the activation object followed by the global object.
  • - Variable instantiation is performed using the activation object as the variable object and using property attributes { DontDelete }.
  • - The caller provides the this value. If the this value provided by the caller is not an object (including the case where it is null ), then the this value is the global object.

10.2.4 Implementation-supplied Code

  • -The scope chain is initialised to contain the activation object as its first element.
  • -The this value is determined just as for function and anonymous code.
  • - If the implementation-supplied function has the ImplicitThis attribute (10.1.1), the this value is placed in the scope chain after the activation object.
  • - If the implementation-supplied function has the ImplicitParents attribute (10.1.1), a list of objects, determined solely by the this value, is inserted in the scope chain after the activation object (if the implementation-supplied function does not have the ImplicitThis attribute) or after the activation object and this object (if the implementation-supplied function has the ImplicitThis attribute). Note that this list is determined at run time by the this value. It is not determined by any form of lexical scoping.
  • -The global object is placed in the scope chain after all other objects.
  • - Variable instantiation is performed using the activation object as the variable object and using attributes { DontDelete }.