Understanding Chrome V8 — Chapter 2: Hello World

Written by huidou | Published 2022/08/14
Tech Story Tags: javascript | chrome-v8 | front-end-development | security | cpp | understanding-chrome-v8 | javascript-development | javascript-tutorial

TLDRHello-world.cc is the smallest example for embedding V8, it contains the most basic functions and is friendly to beginners. V8 is a virtual machine and the isolate is an execution host of JavaScript. The isolate is single-threaded mode, that is, only one thread can enter into the isolate at a time. The AST is an abstract representation of the JavaScript code, used to generate bytecode. In addition, V8 uses the compile cache to improve efficiency in the compilation.via the TL;DR App

Welcome to other chapters of Let’s Understand Chrome V8

1. Hello-world.cc

v8\samples\hello-world.cc is the smallest example for embedding V8, it contains the most basic functions and is very friendly to beginners. For example, Turbofan is not in the hello-world.cc, cause it can be confusing for beginners.

https://gist.github.com/Qing-8391/25cf76bd1e4158ad1766fe4fdf62f593#file-chap2-cpp

The above code is the most important part of hello-world.cc, include v8::V8::Initialize(), v8::Isolate::New,v8::Script::Compile, and script->Run. The handle and context are not mentioned because they are not important for beginners. Figure 1 shows the import data structures, you can debug it with VS2019.

We also can see the workflow and the corresponding methods in Figure 1.

V8 is very huge, if you study it through data structures and key methods, you can get an overview of V8 easily. Then, you can get twice the result with half the effort.

2. Isolate

Isolate is a V8 engine instance that has a self-manage stack and heap. Specifically, V8 is a virtual machine and the isolate is an execution host of JavaScript. The isolate is in single-threaded mode, that is, only one thread can enter into the isolate at a time. Multi threads can share one isolate through the scheduler.

The above code is the export API of isolate, let’s look at three important keys.

  • context: the JavaScript must be in a context.
  • handle: is the reference to a JavaScript Object, responsible for managing heap objects.
  • handle scope: is a set of handles, responsible for managing handles. When a scope is free, the handles in it are free also.

Also, the isolate has an internal API that can be converted with the export API freely. The API is for internal use only and will not be open to others. The code is below:

For beginners, knowing the isolate is enough, you learn it again when you use it.

3. Compile

JavaScript compilation contains lexical analysis, parser, and AST generator. Parser’s code is below:

Parser analysis JavaScript source and generates bytecode. During the generation, token and AST are generated in turn, and they will be destroyed after generating. In addition, V8 uses the compile cache to improve efficiency. Let’s have a look at the AST tree in Figure 2.

The left of Figure 2 is JavaScript code, the right is the AST tree. Also, Figure 3 is the AST tree that output in PowerShell.

The AST is an abstract representation of the JavaScript code, used to generate bytecode. The AST code is below.

First, the compiler scans JavaScript code, and split the code into words, then push the words into the tokenizer (In tokenizer, identify the semantics of a word by its type)to generate token and AST tree, and finally generate bytecode. There are pre-defined FSAs in the compiler. As shown in Figure 4.

4. Execution

Figure 5 shows the key point of JavaScript execution, debug from here, you can see the details of bytecode execution eventually.

Let’s have a look at the execution unit.

So far, we do not mention how to generate bytecode, but we have known the key workflow and the core data structures of V8. At this point, we have the whole picture of the compiler.

Okay, that wraps it up for this share. I’ll see you guys next time, take care!

My blog is cncyclops.com. And please reach out to me if you have any issues.

WeChat: qq9123013 Email: v8blink@outlook.com


Also published here.


Written by huidou | a big fan of chrome V8
Published by HackerNoon on 2022/08/14