摘要:堆栈结构的底部是全局执行上下文,顶部是当前执行上下文。不同的执行上下文切换时堆栈会发生改变译论及代码类型时,在某些时候可能也意味着执行上下文。函数体中代码执行完后,只剩全局上下文直到程序结束译代码更有意思。
第一次翻译,希望各位多多包涵,有错误处还望指出,欢迎提出建议。
Chapter 1.Execution ContextsIntroduction (介绍)
Definitions (定义)
Types of excutable code (可执行代码的类型)
Global code(全局代码)
Funcion code(函数代码)
Eval code(eval代码)
Conclusion(结论)
Additional literature (文献参考)
IntroductionIn this note we will metion execution contexts of ECMAScript and types of executable code related with them.
译:在这篇笔记中,我们将讨论执行环境和相关的可执行代码类型。
DefinitionsEvery 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 codeWith 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 codeThis type of code is processed at level Program: i.e. the loaded external .js -file or the local inline-code (inside the tags). The global code does not include any parts of a code which are in bodies of functions.
译:这种类型的代码是以程序级别处理的:比如说额外的js文件或者局部的内连代码(在标签中的)。全局代码不包括任何函数体内的代码
At initialization(program start), ECStack looks like:
Function codeECStack = [ 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.
译:一旦进入函数代码,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 = [functionContext globalContext ]; //recursive activation of foo ECStack = [ functionContext - recursively 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:
Eval code(function foo() { (function bar() { throw "Exit from bar and foo contexts"; })(); })();
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(functionContext); //eval("var y = 20"); ECStack.push({ context:evalContext, callingContext: functionContext }); //return from eval ECStack.pop(); //return from foo ECStack.pop();
I.e. quite casual and logical call-stack.
译
ConclusionIn 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(); // 2However, due to security reasons in modern engines it was fixed and is not significant anymore.
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 literatureCorresponding section ECMA-262-3 specification - 10. Execution Contexts.
译:该内容截选自ECMA-262-3说明第十章执行上下文。
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/78650.html
摘要:简介年由公司发布的一门面向对象的程序设计语言虚拟机。两个主要组件编译器源程序转成字节码运行编译后的程序后缀运行时环境。 Lecture1 Java简介 1995年由Sun公司发布的一门面向对象的程序设计语言 JVM(Java Virtual Machine):Java虚拟机。是实现Java平台无关性的关键Java程序的执行流程:解释执行的过程由JVM来完成,即JVM把字节码文件解释...
摘要:执行环境在很多方面都有其独特之处全局变量和函数便是其中之一事实上的初始执行环境是由多种多样的全局变量所定义的这写全局变量在脚本环境创建之初就已经存在了我们说这些都是挂载在全局对象上的全局对象是一个神秘的对象它表示了脚本最外层上下文在浏览器中 JavaScript执行环境在很多方面都有其独特之处. 全局变量和函数便是其中之一. 事实上, js的初始执行环境是由多种多样的全局变量所定义的,...
摘要:由于本人更习惯使用所以后续案例都是基于与,同时这里是基于最新的编写的哦创建项目初次接触,我们先来看看如何创建一个项目,这里以为例,其他的工具小伙伴们自行搜索创建方式。创建完项目后,各位小伙伴请认真细心的对比下与传统的工程有何区别如目录结构。 SpringBoot 是为了简化 Spring 应用的创建、运行、调试、部署等一系列问题而诞生的产物,自动装配的特性让我们可以更好的关注业务本身...
摘要:大数据时代第三次信息化浪潮年前后,以云计算大数据物联网的首发为标志迎来第三次信息化浪潮。大数据的发展历程大数据的概念和影响大数据的特性特性指。处理大规模图结构数据。物联网应用大数据云计算物联网的关系三者相辅相成,既有联系又有区别。 ...
摘要:在传输层前者提供面向连接的服务后者提供面向无连接或无连接的传输服务。共同点均实现异构网络互联,不同厂家数据通信网络传输过程用户自然语言通信数据应用层封装分段数据单元协议控制信息网络传输解封装通信数据自然语言应用层用户 ...
阅读 3448·2021-09-22 15:50
阅读 3203·2019-08-30 15:54
阅读 2685·2019-08-30 14:12
阅读 3024·2019-08-30 11:22
阅读 2046·2019-08-29 11:16
阅读 3542·2019-08-26 13:43
阅读 1147·2019-08-23 18:33
阅读 887·2019-08-23 18:32