6.5 Registration and instantiation
Overview
All modules are prepared to enter v8. This occurs in two steps:
Compiling and registering the module.
Instantiating the module.
Finally, they are loaded into v8, ready for execution or evaluation.
Registration
Let's review how registration works because we have some imports in this example. This time, the process will be a bit longer since the imports also need to be loaded into V8. Here's a detailed explanation:
load_main_module The first step involves loading the main module.
Recursive Loading and Preparation After that, the process performs recursive loading and prepares the modules for registration.
Loop Over Pending List and Module Registration Next, there is a loop that goes through a list of pending modules and registers them.
register_and_recurse Within the registration process, there's a loop that handles both registration and recursion.
Loop Over Imports Another loop iterates over all the imports of a module.
Adding Imports to Pending List During this iteration, if an import is not yet registered, it is added to the pending list for later processing.
The "load_module" loop continues registering modules until the pending list becomes empty. Here is an abbreviated code of all the three functions (only import specific code is shown, rest is suppressed):
The process is pretty simple. It's similar to creating the module graph. But, to ensure everything is covered, we'll review all the steps that lead to registering modules for the hello world v2 program.
Step 1
Compile and register module file:///Users/mayankc/Work/source/denoExamples/helloV2.ts
.
ID
Module
1
file:///Users/mayankc/Work/source/denoExamples/helloLogV2.ts
Step 2
Go through imports and add them to the pending list
add
https://deno.land/x/machine_id/mod.ts
to the pending listadd
npm:nanoid
to the pending list
Step 3
Compile and register module https://deno.land/x/machine_id/mod.ts
.
ID
Module
2
https://deno.land/x/machine_id/mod.ts
There are further external dependencies from machine_id module.
Step 4
Compile and register module https://deno.land/x/machine_id/mod.ts
.
ID
Module
3
npm:nanoid or file:///Users/mayankc/Work/source/denoExamples/node_modules/.deno/nanoid@4.0.2/node_modules/nanoid/index.js
Step 5
Go through imports and add them to the pending list
add file:///Users/mayankc/Work/source/denoExamples/node_modules/.deno/nanoid@4.0.2/node_modules/nanoid/url-alphabet/index.js to the pending list
Step 6
Compile and register module file:///Users/mayankc/Work/source/denoExamples/node_modules/.deno/nanoid@4.0.2/node_modules/nanoid/url-alphabet/index.js.
ID
Module
4
file:///Users/mayankc/Work/source/denoExamples/node_modules/.deno/nanoid@4.0.2/node_modules/nanoid/url-alphabet/index.js
Step 7
There are no further imports, nothing gets added to the pending list.
Step 8
Since the pending list is empty, the registration process is complete. All the modules that were imported from the main module have been compiled and registered.
It's crucial to understand that these modules have been loaded separately into V8, without any connection between them for now. The actual connection will occur when they are instantiated.
Instantiation
Regardless of how many imports or modules there are, instantiation only occurs in the root or main module. To remind you, here is the relevant code where "instantiate" is called.
In the previous chapter, we learned about the mod_instantiate function. This function helps set up a module in v8, which is like a place where JavaScript code runs. In the last chapter's hello world program, we didn't need any callbacks because that program didn't use any external code.
But things are a bit different now. In this chapter, we're working with modules that have imports. An import is like a way to bring in code from another module. When we have imports, the v8 engine wants us to provide a callback to help it figure out where to find the imported modules.
Here's how it works:
We start by creating the main module.
For each import in each module:
V8 asks us for a module handle using a callback.
In this callback, V8 tells us which module it wants to find and the module that's asking for it. This helps V8 connect the dots correctly.
This process continues, going deeper into each module, until all the modules are taken care of.
It's like solving a puzzle one piece at a time. Each module is a piece, and V8 needs us to tell it where to find the pieces it's missing. This whole process happens in a loop, like a chain reaction, until everything is set up.
Remember, it's a looping process, so we don't have to set up each module individually. We just start with the main module, and V8 takes care of the rest, step by step.
Now, let's see how all of this works with our updated hello world v2 program.
Step 1
Instantiate main module id=1.
Step 2
Receive callback to resolve:
Resolve
Referrer
npm:nanoid
file:///Users/mayankc/Work/source/denoExamples/helloV2.ts
Step 3
Receive callback to resolve:
Resolve
Referrer
https://deno.land/x/machine_id/mod.ts
file:///Users/mayankc/Work/source/denoExamples/helloV2.ts
Step 4
Receive callback to resolve:
Resolve
Referrer
crypto
https://deno.land/x/machine_id/mod.ts
Yes, even built-in modules like crypto needs a resolve callback. They're built-in for us, but not for V8.
Step 5
Receive callback to resolve:
Resolve
Referrer
crypto
https://deno.land/x/machine_id/mod.ts
Step 6
Receive callback to resolve:
Resolve
Referrer
./url-alphabet/index.js
npm:nanoid or file:///Users/mayankc/Work/source/denoExamples/node_modules/.deno/nanoid@4.0.2/node_modules/nanoid/index.js
--
After all the import callbacks are successfully resolved, v8 completes the module setup. Both the main module and its imports are prepared and set up inside v8 through a recursive process. They are now set to be evaluated.
Before we move on to evaluation, let's understand how operations (ops) are registered. This is important because our hello world v2 program includes both synchronous and asynchronous ops. We've touched upon the ops registration briefly in a previous chapter. Before we proceed to execute the program, let's take a closer look at this registration process.
Last updated