When control is transferred to an ECMA Script, an execution context is instantiated and activated. Active execution contexts logically form a stack. The top execution context on this logical stack is the running execution context. 6.4.1 Function ObjectsThere are two types of Function objects: Program and internal functions. Program functions are defined in source text by a FunctionDeclaration or created dynamically either by using a FunctionExpression or by using the built-in Function object as a constructor. Internal functions are built-in objects of the language, such as parseInt and Math.exp . A script engine implementation may also provide implementation dependent internal functions that do not contain executable code defined by the ECMA Script grammar. 6.4.2 Types of Executable CodeThere are three types of ECMA Script executable code: Global code, eval() code, and function code.
6.4.3 Instantiation of Variable on Method InvocationEvery execution context is associated with a variable object. Variables and functions declared in the source text are added as properties of the variable object. For function code, parameters are added as properties of the variable object. The type of code determines which object is used as the variable object and what attributes are used for the properties. On entering an execution context, the properties are bound to the variable object as follows : For each formal parameter identifier in the FormalParameterList of the invoked function, the script engine creates a variable object property whose name is that identifier and whose attributes are determined by the type of code. The values of the parameters are supplied by the caller as arguments to [[Call]] . 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 FunctionDeclaration in the code, in source text order, the script engine creates a property of the variable object whose name is the Identifier in the FunctionDeclaration , whose value is the result returned by creating a Function object, 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. Semantically, this step follows the creation of FormalParameterList properties. For each VariableDeclaration in the code, the script engine creates a property of the variable object whose name is the Identifier in the 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 follows the creation of the FormalParameterList and FunctionDeclaration 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. 6.4.3.1 Scope ChainEvery execution context is associated with a scope chain. A scope chain is a list of objects that are searched when evaluating an Identifier . When control enters an execution context, a scope chain is created and populated with an initial set of objects, depending on the type of code. During execution within an execution context, the scope chain of the execution context is affected only by WithStatements and CatchClauses . 6.4.4 Global ObjectThere is a unique global object, which is created before control enters any execution context. Initially, built-in objects such as Math , String , Date , etc., have the attributes DontEnum . Additional host defined properties may exist, such as properties valued to the global object itself; for example, in the HTML DOM, the window property of the global object is the global object itself. As control enters execution contexts additional properties may be added to the global object and the initial properties may be changed. The global object is intended to be constructed by the script engine prior to entering any execution context, and therefore the global object does not have a [[Construct]] property. It is therefore not possible to use the global object as a constructor with the new operator. The global object does not have a [[Call]] property; it is not possible to invoke the global object as a function. Because global objects are intended to be constructed by the script engine prior to execution of any script, the values of the [[Prototype]] and [[Class]] properties of the global object are implementation-dependent. 6.4.5 Activation ObjectWhen control enters an execution context for function code, the ActivationObject is created and associated with the execution context. The activation object is initialized with a DontDelete property and is subsequently used as the variable's object for the purposes of variable instantiation. It is purely a specification mechanism as it is impossible for an ECMA Script program to access the ActivationObject . Nevertheless, a program can access members of the ActivationObject without accessing the object itself. 6.4.6 ThisThere is an immutable 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. 6.4.7 Arguments ObjectWhen control enters an execution context for function code, an ArgumentsObject is created and passed along to that function's code. This ArgumentsObject is initialized by the script engine as follows:
6.4.8 Entering an Execution ContextEvery function and constructor call enters a new execution context, even if a function is calling itself recursively. Every return exits an execution context. A thrown exception, if not caught, may also exit one or more execution contexts. When control enters an execution context, the scope chain is created and initialized, variable instantiation is performed, and the this value is determined. The initial instantiation of the scope chain, variable instantiation, and the determination of the this value depend on the type of code being entered. 6.4.9 Global CodeFor global code, the scope chain is created and initialized to contain the global object and no others. The value this in global code refers to the global object. Variable instantiation is performed using the global object as the variable object. 6.4.10 eval() CodeWhen 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, this process is performed just as for global code. The scope chain is initialized 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 with statements and catch clauses. The this value is the same as the this value of the calling context. Variable instantiation is performed using the calling context's variable object and using empty property attributes. 6.4.11 Function CodeWhen control enters an execution context for function code, the scope chain of this function is initialized to contain the activation object followed by the objects in the scope chain stored in the [[Scope]] property of the Function object. The caller provides the this value. If this is not an object (including the case where it is null), then the this value is interpreted as referring to the global object. |