Sie sind auf Seite 1von 6

1/7/2018 Understanding How the Chrome V8 Engine Translates JavaScript into Machine Code

DroidHead Follow
Android Developer | Machine Learning Enthusiast | Hackathon Lover | Traveler | Foodie
Dec 20, 2017 · 4 min read

Understanding How the Chrome V8 Engine


Translates JavaScript into Machine Code
Before diving deep into the core of Chrome’s V8, rst let’s get our
fundamentals down. All of our systems consists 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.
Di erent microprocessors speak di erent 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 consist of instructions that are performed at a particular piece of


memory in your system at low level. You must feel lucky for not having

https://medium.freecodecamp.org/understanding-the-core-of-nodejs-the-powerful-chrome-v8-engine-79e7eb8af964 1/6
1/7/2018 Understanding How the Chrome V8 Engine Translates JavaScript into Machine Code

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 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 di erent JavaScript engines including Rhino, JavaScriptCore,


and SpiderMonkey. These engines follow the ECMAScript Standards.
ECMAScript de nes the standard for the scripting language. JavaScript
is based on ECMAScript standards. These standards de ne how the
language should work and what features it should have. You can learn
more about ECMAScript here.

https://medium.freecodecamp.org/understanding-the-core-of-nodejs-the-powerful-chrome-v8-engine-79e7eb8af964 2/6
1/7/2018 Understanding How the Chrome V8 Engine Translates JavaScript into Machine Code

Source : Google

The Chrome V8 engine :

• The V8 engine is written in C++ and used in Chrome and Nodejs.

• It implements ECMAScript as speci ed 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.

https://medium.freecodecamp.org/understanding-the-core-of-nodejs-the-powerful-chrome-v8-engine-79e7eb8af964 3/6
1/7/2018 Understanding How the Chrome V8 Engine Translates JavaScript into Machine Code

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 speci es 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 les 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 di erent 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.

https://medium.freecodecamp.org/understanding-the-core-of-nodejs-the-powerful-chrome-v8-engine-79e7eb8af964 4/6
1/7/2018 Understanding How the Chrome V8 Engine Translates JavaScript into Machine Code

1 // The callback that is invoked by v8 whenever the JavaScri


2 // function is called. Prints its arguments on stdout sepa
3 // spaces and ending with a newline.
4 void Print(const v8::FunctionCallbackInfo<v8::Value>& args)
5 bool first = true;
6 for (int i = 0; i < args.Length(); i++) {
7 v8::HandleScope handle_scope(args.GetIsolate());
8 if (first) {
9 first = false;
10 } else {
11 printf(" ");
12 }
13 v8::String::Utf8Value str(args.GetIsolate(), args[i]);

Similarly we can add our own implementation of di erent new


functions in C++ inside V8 allowing it to be understood by Node.js.

That is certianly 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.

https://medium.freecodecamp.org/understanding-the-core-of-nodejs-the-powerful-chrome-v8-engine-79e7eb8af964 5/6
1/7/2018 Understanding How the Chrome V8 Engine Translates JavaScript into Machine Code

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


Github and Facebook.

https://medium.freecodecamp.org/understanding-the-core-of-nodejs-the-powerful-chrome-v8-engine-79e7eb8af964 6/6

Das könnte Ihnen auch gefallen