執行上下文(執行環境)-Chapter1

第一次翻譯,但願各位多多包涵,有錯誤處還望指出,歡迎提出建議。javascript

Chapter 1.Execution Contexts

  1. Introduction (介紹)java

  2. Definitions (定義)數組

  3. Types of excutable code (可執行代碼的類型)app

    1. Global code(全局代碼)ide

    2. Funcion code(函數代碼)函數

    3. Eval code(eval代碼)ui

  4. Conclusion(結論)this

  5. Additional literature (文獻參考)lua

Introduction

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

譯:在這篇筆記中,咱們將討論執行環境和相關的可執行代碼類型。

Definitions

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

譯:當控制流即將執行代碼時,老是先進入執行上下文

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

譯:執行上下文(縮寫爲EC)是ECMA-262使用的抽象概念,一般用來表示可執行代碼的類型和區別。

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.

譯:官方標準沒有定義EC的確切結構和技術實現,按照規範來實現依然ECMAScript引擎的問題

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.

譯:從邏輯上來看,許多激活的執行上下文會造成一個堆棧結構。堆棧結構的底部是全局執行上下文,頂部是當前執行上下文。不一樣的執行上下文切換時堆棧會發生改變

Types of executable code

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 the function is called recursively or as the constructor), and also at built-in eval function work.

譯:控制流每次進入函數(即便該函數是遞歸調用或做爲構造器)時,入棧就會發生,一樣內嵌在該函數中的eval函數也會引起入棧行爲。

Global code

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.

譯:這種類型的代碼是以程序級別處理的:好比說額外的js文件或者局部的內連代碼(在<script>標籤中的)。全局代碼不包括任何函數體內的代碼

At initialization(program start), ECStack looks like:

ECStack = [
   globalContext  
];

Function code

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.

譯:一旦進入函數代碼,ECStack就會加入新元素。該函數代碼並不包括其內部函數的代碼。

For example, let's take the function which calls itself recursively once:

(function foo(flag) {    
    if (flag) {
        return;
    }
    foo(true);
})(false);

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 from a function exits the current execution context and ECStack poped accordingly - consecutively and upside-down - quite natural implementation of a stack. After the work of this code is finished, ECStack again contains only globalContext- until the program end.

譯:函數的每次返回都表明了當前執行上下文的結束,ECStack會有序的推出該函數。函數體中代碼執行完後,ECStack只剩全局上下文直到程序結束

A thrown but not caught exception may also exit one or more execution contexts:

(function foo() {
   (function bar() {
       throw 'Exit from bar and foo contexts';
   })();
})();

Eval code

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.

譯:eval代碼更有意思。在該狀況下,有個調用上下文的新概念,其本質是上下文,只不過是當eval函數被調用時的上下文

The actions made by eval, such as variable or function definition, influence exactly the calling context:

//influence global context
eval('var x = 10');

(function foo() {
   // and here, variable "y" is
   // created in the local context
   // of "foo" function
   eval('var y = 20');
})();

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

Note, in the strict-mode of ES5,eval already does not influence the calling context, but instead evaluates the code in the local sandbox.

For the example above we hav the following ECStack modifications:

ECStack = [
   globalContext
];

// eval("var x = 10");
ECStack.push({
   context: evalContext,
   callingContext: globalContext
});

//eval extied context
ECStack.pop();

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

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

//return from eval
ECStack.pop();

//return from foo
ECStack.pop();

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

In old SpiderMonkey implementations(Firefox), up to version 1.7, it was possible to pass a calling context as a second argument for eval function. Thus, if the context still exists, it is possible to influence private 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

However, due to security reasons in modern engines it was fixed and is not significant anymore.

Conclusion

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

譯:這片理論最低要求是掌握執行環境的細節分析,好比說變量對象或做用域,有關細節描述能夠在相關章節中查閱到。

Additional literature

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

譯:該內容截選自ECMA-262-3說明第十章執行上下文。

相關文章
相關標籤/搜索