V8 Engine and Introduction to NodeJS
Sharing is Caring
This post is going to talk about V8 Engine and Introduction to NodeJS
Processor
Accepts instructions in a particular language (eg: IA-32, X86-64, ARM, MIPS…). We need to give instruction to a processor on a computer in the language it understands. The microprocessor inputs those instructions and executes them.
Machine Code (or Machine Language)
Programming languages understood by computer processors. Every program that runs on a computer is compiled (converted) into the machine language using the respective compiler. All code eventually converts to machine code so that the machine can carry out the job.
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.
Javascript Engine
It is a program that converts Javascript code into machine code that microprocessors can understand.
Eg: Rhino, Spidermonkey, V8…etc
Since there are so many js engines with their own little tweaks in js, ECMAScript was introduced to standardize Javascript. It’s the standard that JS is based on.
Formally, ECMAScript (or ES) is a scripting-language specification standardized by Ecma International in ECMA-262 and ISO/IEC 16262. It was created to standardize JavaScript.
The V8 Engine
V8 is a powerful open source Javascript engine provided by Google. It’s written in C++ and used in Chrome. It follows ECMA standards for Javascript.
V8 is just a C++ program and takes JS and converts it into processor understandable language. One important feature of V8 is that it can run standalone (as in Google Chrome Javascript console), or can be embedded into any C++ application. This makes it possible to write a C++ program where others can write Javascript in, and make this program execute that Javascript via V8.
Let’s understand this concept in a bit more detail. V8 can run standalone or can be embedded into a C++ program to add more features to Javascript.
For example: Greet() is not a valid JS code as per ECMA standards. There’s no corresponding C++ code in V8 engine for Greet() function.
What we can do is that write our own C++ program with Greet() definition and embed V8 into this program. So now when someone will call Greet() function in JS and pass it through our custom made engine (our C++ program with V8 embedded), it will invoke the Greet() function defined in our C++ program.
This is a very significant feature and allows the JavaScript to understand more than what the ECMAScript standard specifies the JavaScript should understand. It’s extremely powerful because C++ has far more features than Javascript as a programming language. Javascript was designed to be in the browser and not for lower level operations like dealing with files and folders that are sitting on the hard drive or connecting directly to the database. But all these can be done via C++ as it’s more closer to the machine. So one can write things in C++ that Javascript doesn’t have and make anything that can be done in C++ available to Javascript.
So what is NodeJS?
We’ve seen that V8 is designed so that we can embed other C++ programs and make features and things we can do in C++ available to JS. This is what Nodejs is. Nodejs is a C++ program with V8 embedded that has added a wealth of great features that make is suitable to be a server technology.
Nodejs came into existence when the original developers of JavaScript extended it from something you could only run in the browser to something you could run on your machine as a standalone application.
Let’s look at some of the open source code inside the Engine (V8 plus C++ program).v8/samples/shell.cc
Here we can see definitions of functions such as Print() which are natively not available in EcmaScript. Whenever the Print() function is invoked in nodejs, 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’s the basic working of V8 Engine and how it helps in execution of JS code on the server side (nodejs).
Published on Web Code Geeks with permission by Ashish Awasthi, partner at our WCG program. See the original article here: V8 Engine and Introduction to NodeJS Opinions expressed by Web Code Geeks contributors are their own. |