Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is Javascript translated to bytecode?

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?

Links to scientific documents would be also greatly appreciated.

like image 594
最白目 Avatar asked Jan 11 '12 13:01

最白目


People also ask

What is bytecode in JavaScript?

The bytecode is a format that simplifies the execution of the JavaScript code by an interpreter, and then by the Just-In-Time compilers (JITs). The bytecode is much larger than the source code, so Firefox only generates the bytecode of executed functions.

Is JavaScript converted to assembly?

Yes, the JavaScript engines used in "modern" (since 2008) browsers have just-in-time compilers that compile JavaScript to machine code.

Is JavaScript compiled or interpreted?

What is JavaScript? JavaScript (often shortened to JS) is a lightweight, interpreted, object-oriented language with first-class functions, and is best known as the scripting language for Web pages, but it's used in many non-browser environments as well.

What compiler does JavaScript use?

Heart of the engine As we discussed earlier, JavaScript is interpreted by an interpreter named Ignition as well as compiled by a JIT optimizing compiler named TurboFan.


3 Answers

It's up to the implementation; the specification is 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 only interprets, 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.)

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.

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.

like image 166
T.J. Crowder Avatar answered Oct 17 '22 08:10

T.J. Crowder


This may help : 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

like image 44
tofarr Avatar answered Oct 17 '22 08:10

tofarr


For Firefox there's some specifications on its bytecodes:

https://developer.mozilla.org/en-US/docs/Mozilla/Projects/SpiderMonkey/Bytecodes https://developer.mozilla.org/en-US/docs/Mozilla/Projects/SpiderMonkey/Internals/Bytecode

For V8 it's compiled to native code directly:

http://jayconrod.com/posts/51/a-tour-of-v8-full-compiler

like image 43
jiyinyiyong Avatar answered Oct 17 '22 08:10

jiyinyiyong