28

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.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
最白目
  • 3,505
  • 6
  • 59
  • 114
  • 1
    http://code.google.com/p/v8/ This is where google hosts their v8 javascript engine. Why do you want to know this btw? – TJHeuvel Jan 11 '12 at 13:14
  • thanks! I need to know because I want to compare mobile apps that run in a webview to the ones who run on the native operating system for my bachelor thesis. This may be helpful for performance comparison – 最白目 Jan 11 '12 at 13:17
  • 7
    JavaScript is not a W3C language. It (or rather ECMAScript) is an ECMA-International standard. – Quentin Jan 11 '12 at 13:19
  • 1
    Every engine does it differently. Not all are part of browsers. There is no single specification saying how JavaScript should translate into bytecode, but [each engine](http://en.wikipedia.org/wiki/List_of_ECMAScript_engines) has its own way. – kojiro Jan 11 '12 at 13:19
  • Not seeing the reason for the downvote. – T.J. Crowder Jan 11 '12 at 13:22
  • These were all helpful, I aldready learned a lot from the answers and will dig into the links. Thanks @ all. – 最白目 Jan 11 '12 at 13:27
  • +1, one of the few questions here, an idiot like me knew how to answer (since I've been writing useless parsers and lexers all my life) – ApprenticeHacker Jan 11 '12 at 13:27
  • @T.J.Crowder it isnt really a question about programming, more about language. – TJHeuvel Jan 11 '12 at 13:33
  • 2
    @TJHeuvel: Wow, that's an incredibly fine distinction, I sure wouldn't down- or close-vote on that. For me, a question about programming languages is, ipso facto, a question about programming or plenty close enough. (In any case, that would be more a reason for a close vote as opposed to a downvote, but don't let's quibble. :-) ) – T.J. Crowder Jan 11 '12 at 13:37
  • 2
    I just want to add that I first asked the question here http://cstheory.stackexchange.com/questions, they sent me back here :) – 最白目 Jan 11 '12 at 13:39

5 Answers5

32

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 periodically publish descriptions of how they get the fantastic speed out of V8 that they do. You can 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 of.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
5

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

jiyinyiyong
  • 4,586
  • 7
  • 45
  • 88
5

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

tofarr
  • 7,682
  • 5
  • 22
  • 30
  • BTW Javascript is a dialect of ECMAScript in case you didn't know – tofarr Jan 11 '12 at 13:16
  • 8
    Actually, JavaScript is an implementation of the ECMAScript standard: JavaScript contains extensions which are not defined by the standard. – Rob W Jan 11 '12 at 13:18
3

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.

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.

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

  • Parsing and Preprocessing (recursive descent or otherwise)
  • Analysis
  • Execution

    Chrome's Javascript Engine compiles Javascript to native platform-specific machine code (for optimum performance) It also has a Garbage Collection Mechanism.

  • ApprenticeHacker
    • 21,351
    • 27
    • 103
    • 153
    3

    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 explicitly creates a bytecode and uses a JIT compiler on that, for example.

    Tommy
    • 99,986
    • 12
    • 185
    • 204