6.4 Execution


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 Objects

There 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 Code

There are three types of ECMA Script executable code: Global code, eval() code, and function code.

  • Global code : This is source text that is treated as an ECMA Script program. The global code of a particular program does not include any source text that is parsed as part of a FunctionBody .

  • eval() code : This 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 ECMA Script program. The eval() code for a particular invocation of eval() is the global code portion of the string parameter.

  • Function code : This is source text that is parsed as part of a FunctionBody . The function code of a particular FunctionBody does not include any source text that is parsed as part of a nested FunctionBody. Function code also denotes the source text supplied when using the built-in Function object as a constructor. More precisely, the last parameter provided to the Function constructor is converted to a string and treated as the FunctionBody. If more than one parameter is provided to the Function constructor, all parameters except the last one are converted to strings and concatenated together, separated by commas. The resulting string is interpreted as the FormalParameterList for the FunctionBody defined by the last parameter. The function code for a particular instantiation of a Function object does not include any source text that is parsed as part of a nested FunctionBody.

6.4.3 Instantiation of Variable on Method Invocation

Every 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 Chain

Every 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 Object

There 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 Object

When 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 This

There 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 Object

When 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:

  • The value of the internal [[Prototype]] property of the arguments object is the original Object prototype object.

  • A callee property is created and initialized to the Function object being executed. This allows anonymous functions to be recursive.

  • A length property is created and initialized to the number of actual parameter values supplied by the caller.

  • A property is created with name ToString(arg) where arg is a nonnegative integer less than the value of the length property. The first parameter value corresponds to arg=0 , the second to arg=1 , and so on. The initial value of ToString(arg) is the value of the corresponding actual parameter supplied by the caller. 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.

6.4.8 Entering an Execution Context

Every 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 Code

For 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() 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, 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 Code

When 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.



ITV Handbook. Technologies and Standards
ITV Handbook: Technologies and Standards
ISBN: 0131003127
EAN: 2147483647
Year: 2003
Pages: 170

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net