by Mayank Tripathi

Understanding How the Chrome V8 Engine Translates JavaScript into Machine Code

Before diving deep into the core of Chrome’s V8, first, let’s get our fundamentals down. All of our systems consist of microprocessors, the thing that is sitting inside your computer right now and allowing you to read this.

Microprocessors are tiny machines that work with electrical signals and ultimately do the job. We give microprocessors the instructions. The instructions are in the language that microprocessors can interpret. Different microprocessors speak different languages. Some of the most common are IA-32, x86–64, MIPS, and ARM. These languages directly interact with the hardware so the code written in them is called machine code. Code that we write on our computers is converted or compiled into machine code.

That’s what machine code looks like:

Source : Google

It consists of instructions that are performed at a particular piece of memory in your system at a low level. You must feel lucky for not having to write all this to run your program!

High-level languages are abstracted from machine language. In the level of abstraction below, you can see how far JavaScript is abstracted from the machine level. C/C++ are relatively much closer to the hardware and hence much faster than other high-level languages.

Now back to the V8 engine: V8 is a powerful open source Javascript engine provided by Google. So what actually is a Javascript Engine? It is a program that converts Javascript code into lower level or machine code that microprocessors can understand.

There are different JavaScript engines including Rhino, JavaScriptCore, and SpiderMonkey. These engines follow the ECMAScript Standards. ECMAScript defines the standard for the scripting language. JavaScript is based on ECMAScript standards. These standards define how the language should work and what features it should have. You can learn more about ECMAScript here.

Source: Google

The Chrome V8 engine :

  • The V8 engine is written in C++ and used in Chrome and Nodejs.
  • It implements ECMAScript as specified in ECMA-262.
  • The V8 engine can run standalone we can embed it with our own C++ program.

Let us understand the last point a little better. V8 can run standalone and at the same time we can add our own function implementation in C++ to add new features to JavaScript.

So for example: print('hello world')is not a valid statement in Node.js. It will give error if we compile it. But we can add our own implementation of the print function in C++ on top of the V8 which is open source at Github, thus making the print function work natively. This allows the JavaScript to understand more than what the ECMAScript standard specifies the JavaScript should understand.

This is a powerful feature since C++ has more features as a programming language as compared to JavaScript, as it is much closer to hardware like dealing with files and folders on the hard drive.

Allowing us to write code in C++ and making it available to JavaScript makes it so we can add more features to JavaScript.

Node.js in itself is a C++ implementation of a V8 engine allowing server-side programming and networking applications.

Let’s now look at some of the open source code inside the engine. To do this, you need to go to the v8/samples/shell.cc folder.

Here you can see the implementation of different functions such as Print and Read, which are natively not available in Node.js.

Below, you can see the implementation of the Print function. Whenever the print() function is invoked in Node.js, it will create a callback and the function will be executed.

Similarly, we can add our own implementation of different new functions in C++ inside V8 allowing it to be understood by Node.js.

That is certainly too much to grab for a simple statement and that’s the amount of work V8 engine does under the hood.

Now you must have a clear understanding of how Node.js works and what actually is the Chrome V8 engine.

Thanks for reading this article. Let’s follow up on Twitter, Linkedin, Github, and Facebook.