-
Notifications
You must be signed in to change notification settings - Fork 0
On V8
Saibaba Vinayakaya namaha! Saraswati devi
JavaScript, the language is a very high level language.
The normal workflow for JavaScript is to write code and see it being executed in the browser using SCRIPT tags or on node.
This is rather different than others, if you have ever worked with another programming language, say C#, Java, the C based languages.
The workflow is different for such languages.
In other languages, You will have met your friend, the compiler.
The compiler is a translator. Its responsibility is to translate code.
You invoke the compiler with your program as arguments and it converts the source language to a machine level language, thereby allowing the code to run on a specific platform.
Since every language is different, each has its own compiler.
Thus, we have various compilers, each translating one kind of source language.
You might ask: Well, then, how does JavaScript run? I have never "compiled" my JavaScript code.
Interestingly we have. Unknowingly. It happens without us knowing anything about it.
There are some important things we have to understand here.
Language Specification : ECMASCRIPT 262
Implementation : JavaScript
JavaScript Engines: V8, SpiderMonkey, Chakra
Environment : Web browser, node or any other program that embeds and exposes one of these engines.
JavaScript runs in Environments, such as the Web browser or node. These are programs with different responsibilities and design goals. JavaScript could very well run in any environment. If the environment embeds a JavaScript Engine inside it and allows you to target this engine.
The Web browsers and node do.
They embed a JavaScript Engine inside their code and interface it with the developer's code.
Whenever some developer code is encountered, it is passed to the JavaScript engine for translation and execution.
All this may sound very complex and very mystical and magical.
But it is pretty simple.
Scripting languages have the unique ability to be embeddable inside other programs.
Since the way they translate code and the way code executes is different.
All this can be understood, by performing simple experiments.
For this to be done, we need to inspect code and use some tools.
We will just use V8 in our experiments, since it is both used inside the web browser(chrome) and on node.
So we can understand both the environments. Or atleast have an idea on whats happening.
V8 is a JavaScript Engine, created by Google, primarily to be run inside their browser, Chrome.
It is important to note that V8 is one of the components inside Chrome.
Since it is a component, it is maintained as a separate codebase and opensourced.
Also, since chrome is available on various platforms, so is V8.
This is information that one can find at V8 at its best documented place:
https://developers.google.com/v8/intro
Take a minute to read all that.
Now, what is interesting is V8 can be run as a standalone, or it can be embedded inside a C++ program.
Our adventures relate to that.
How can we run V8 as a standalone program? And how can we embed it inside a C++ program.
Can we download some binaries and see this in action?
Unfortunately, not.
We have to build the V8 source code on our machine.
And that my friends is an interesting experience.
And rewarding as well.