Javascript 如何转换为字节码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8819846/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 04:40:42  来源:igfitidea点击:

How is Javascript translated to bytecode?

javascriptcompiler-construction

提问by u635504

I can't find information on the web how W3C languages compile to machine code. I know that the gap between the web and the processor must be somehow the browser, but how does it work and what are the steps till Javascript is executed in the processor?

我在网络上找不到 W3C 语言如何编译为机器代码的信息。我知道网络和处理器之间的差距一定是浏览器,但它是如何工作的以及在处理器中执行 Javascript 之前的步骤是什么?

Links to scientific documents would be also greatly appreciated.

对科学文件的链接也将不胜感激。

回答by T.J. Crowder

It's up to the implementation; the specificationis the full description of the language and how it's supposed to work, implementations are free to satisfy that implementation in any way they like. Some implementations seem (from the outside) to run it purely as an interpreter in the old sense; others may or may not compile to bytecode; V8 (the JavaScript engine in Chrome, Chromium, Brave, Node.js, and others) used to compile to machine code (twice, for hotspots in the app), but now starts out parsing to bytecode and running it in an interpreter and only compiling hotspots as necessary (details). (There's also a V8 mode where it onlyinterprets, which they're experimenting with for environments where compiling at runtime isn't an option, such as iOS where non-Apple apps aren't allowed to allocate executable memory.)

这取决于实施;规范是对语言及其应该如何工作的完整描述,实现可以自由地以他们喜欢的任何方式满足该实现。一些实现(从外部看)似乎纯粹作为旧意义上的解释器运行它;其他人可能会也可能不会编译为字节码;V8(Chrome、Chromium、Brave、Node.js 等中的 JavaScript 引擎)曾经编译为机器代码(两次,用于应用程序中的热点),但现在开始解析为字节码并在解释器中运行它根据需要编译热点(详细信息)。(还有一个 V8 模式,它只解释,他们正在尝试在运行时编译不是一种选择的环境中进行实验,例如不允许非 Apple 应用程序分配可执行内存的 iOS。)

The V8 team (V8 being the JavaScript engine in Chromium and Chrome) periodically publish descriptions of how they get the fantastic speed out of V8 that they do. You may find some of that on the V8 blog.

V8 团队(V8 是 Chromium 和 Chrome 中的 JavaScript 引擎)定期发布他们如何从 V8 中获得惊人速度的描述。您可以在V8 博客上找到其中的一些内容。

Naturally, you can also kick around the code of any of the open-source implementations. V8 and SpiderMonkey (Mozilla's engine) are the two major open-source ones I know.

当然,您也可以随意使用任何开源实现的代码。V8 和 SpiderMonkey(Mozilla 的引擎)是我所知道的两个主要的开源引擎。

回答by tofarr

This may help : http://www.ecma-international.org/publications/standards/Ecma-262.htm

这可能有帮助:http: //www.ecma-international.org/publications/standards/Ecma-262.htm

There is no spec for how to translate into bytecode (That is up to the browser developers) but there are specs about how the language should behave

没有关于如何翻译成字节码的规范(这取决于浏览器开发人员),但是有关于语言应该如何表现的规范

回答by ApprenticeHacker

Javascript (as it's name suggests) is a dynamic scripting language. Meaning that it's code is analysed and executed at runtime by the web-browser's Javascript engine.

Javascript(顾名思义)是一种动态脚本语言。这意味着它的代码在运行时由网络浏览器的 Javascript 引擎分析和执行。

It is up to the Web-browser, how it wants to deal with Javascript. Some may generate an intermediate language, or bytecode. Some may directly analyse and execute it.

这取决于 Web 浏览器,它想如何处理 Javascript。有些可能会生成中间语言或字节码。有些可能会直接分析并执行它。

Here are the steps to the simplest way to execute Javascript (parsing and executing at runtime):

以下是执行 Javascript 的最简单方法(在运行时解析和执行)的步骤:

  • Parsing and Preprocessing (recursive descentor otherwise)
  • 解析和预处理(递归下降或其他)
  • Analysis
  • 分析
  • Execution


    Chrome's Javascript Enginecompiles Javascript to native platform-specific machine code (for optimum performance) It also has a Garbage CollectionMechanism.

  • 执行


    Chrome 的 Javascript 引擎将 Javascript 编译为特定于本机平台的机器代码(以获得最佳性能)它还具有垃圾收集机制。

  • 回答by Tommy

    In addition to the useful, specific answers already given, the phrase 'adaptive optimisation' is probably worth looking up if performance is your main interest. JavaScript and its interpreters are just the latest instance of systems that need to convert something else to machine code at runtime, so there's plenty of wider reading. The bytecode forms of Pascal, Smalltalk, Java, etc can easily enough be viewed as an intermediate form in the process of running a defined language on arbitrary hardware — Apple's SquirrelFish explicitlycreates a bytecode and uses a JIT compiler on that, for example.

    除了已经给出的有用的具体答案之外,如果性能是您的主要兴趣,那么“自适应优化”一词可能值得一看。JavaScript 及其解释器只是需要在运行时将其他内容转换为机器代码的系统的最新实例,因此有很多更广泛的阅读。Pascal、Smalltalk、Java 等的字节码形式可以很容易地被视为在任意硬件上运行定义的语言过程中的中间形式——例如,Apple 的 SquirrelFish显式地创建了一个字节码并在其上使用 JIT 编译器。