ECMA-262-3 in detail. Chapter 1. Execution Contexts.

Read this article in: Russian, Chinese, Arabic.

In this note we will mention execution contexts of ECMAScript and types of executable code related with them.

Every time when control is transferred to ECMAScript executable code, control is entered an execution context.

Execution context (abbreviated form — EC) is the abstract concept used by ECMA-262 specification for typification and differentiation of an executable code.

The standard does not define accurate structure and kind of EC from the technical implementation viewpoint; it is a question of the ECMAScript-engines implementing the standard.

Logically, set of active execution contexts forms a stack. The bottom of this stack is always a global context, the top — a current (active) execution context. The stack is modified (pushed/popped) during the entering and exiting various kinds of EC.

With abstract concept of an execution context, the concept of type of an executable code is related. Speaking about code type, it is possible in the certain moments to mean an execution context.

For examples, we define the stack of execution contexts as an array:

ECStack = [];

The stack is pushed every time on entering a function (even if function is called recursively or as the constructor), and also at built-in eval function work.

This type of code is processed at level “Program”: i.e. the loaded external .js-file or the local inline-code (inside the <script></script> tags). The global code does not include any parts of a code which are in bodies of functions.

At initialization (program start), ECStack looks like:

ECStack = [
  globalContext
];

On entering the function code (all kinds of functions), ECStack is pushed with new elements. It is necessary to notice that the code of concrete function does not include codes of the inner functions. For example, let’s take the function which calls itself recursively once:

(function foo(bar) {
  if (bar) {
    return;
  }
  foo(true);
})();

Then, ECStack is modified as follows:

// first activation of foo
ECStack = [
  <foo> functionContext
  globalContext
];

// recursive activation of foo
ECStack = [
  <foo> functionContext – recursively
  <foo> functionContext
  globalContext
];

Every return exits the current execution context and ECStack popped accordingly — consecutively and upside-down — quite natural implementation of a stack. A thrown but not caught exception also may exit one or more execution contexts. After the work of this code is finished, ECStack again contains only globalContext — until the program end.

Things are more interesting with eval code. In this case, there is a concept of a calling context, i.e. a context from which eval function is called. The actions made by eval (e.g. variable or function declaration) influence the calling context:

eval('var x = 10');

(function foo() {
  eval('var y = 20');
})();

alert(x); // 10
alert(y); // "y" is not defined

ECStack modifications:

ECStack = [
  globalContext
];

// eval('var x = 10');
ECStack.push(
  evalContext,
  callingContext: globalContext
);

// eval exited context
ECStack.pop();

// foo funciton call
ECStack.push(<foo> functionContext);

// eval('var y = 20');
ECStack.push(
  evalContext,
  callingContext: <foo> functionContext
);

// return from eval
ECStack.pop();

// return from foo
ECStack.pop();

I.e. quite casual and logical Call-stack.

In SpiderMonkey implementation (built into Firefox, Thunderbird), up to version 1.7, it is possible to pass a calling context as a second argument for eval function. Thus, if this context exists, it is possible to influence “private” (as some like to name them) variables:

function foo() {
  var x = 1;
  return function () { alert(x); };
};

var bar = foo();

bar(); // 1

eval('x = 2', bar); // pass context, influence internal var "x"

bar(); // 2

This theoretical minimum is required for the further analysis of details related with execution contexts (such as variable object, scope chain, etc.) which description can be found in appropriate chapters.

Corresponding section of ECMA-262-3 specification — 10. Execution Contexts.


Translated by: Dmitry A. Soshnikov.
Published on: 2010-03-11

Originally written by: Dmitry A. Soshnikov [ru, read »]
Originally published on: 2009-06-26


Tags: , ,

 
 
 

7 Comments:

  1. Gravatar of John Merge John Merge
    15. March 2010 at 21:29

    Great article, as usual :)


  2. Gravatar of Robert Polovsky Robert Polovsky
    17. March 2010 at 22:30

    Thanks for these excellent articles! Your blog is very professional and useful for every JavaScript programmer.


  3. Gravatar of Dmitry A. Soshnikov Dmitry A. Soshnikov
    8. April 2010 at 16:15

    @John Merge, @Robert Polovsky, thanks colleagues.


  4. Gravatar of justin justin
    15. April 2010 at 20:41

    It’s an most excellent articles I have read.
    so I have tranclated it into Chinese,let more Chinese programmers see it.I would like to tranclate all articles of the set of “ECMA-262-3 in detail”.
    you can visit http://www.cnblogs.com/justinw/archive/2010/04/16/1713086.html
    I wish you like it.

    thanks for the share again


  5. Gravatar of Dmitry A. Soshnikov Dmitry A. Soshnikov
    15. April 2010 at 23:01

    @justin

    Thanks, justin. As Google translate shows me (unfortunately I can’t read Chinese myself :) ), your translation is good. I am glad to see Chinese colleagues interested in deep JavaScript; that’s great.

    I added a link to your translation.

    Dmitry.


  6. Gravatar of Demetria Engleson Demetria Engleson
    27. April 2010 at 13:30

    You are a very smart person!


  7. Gravatar of Hans Höglund Hans Höglund
    2. September 2010 at 03:57

    Thanks for these series. An excellent overview of the specification.


Leave a Reply

Code: For code you can use tags [js], [text], [ruby] and other.

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>