The Internals of Deno
  • The Internals of Deno
  • Audience
  • Reviews
  • Translations
  • Formats
  • Contents
  • Chapter 1 - INTRODUCTION
    • 1.0 Cover page
    • 1.1 Introduction
    • 1.2 History of Deno
    • 1.3 About Deno
    • 1.4 Releases
    • 1.5 The Deno Company
    • 1.6 Deno's source
    • 1.7 What's next
  • Chapter 2 - ARCHITECTURE
    • 2.0 Cover page
    • 2.1 Architecture
    • 2.2 Overall architecture
    • 2.3 Programming Languages
    • 2.4 Deno components
    • 2.5 OPs
    • 2.6 TSC/SWC
    • 2.7 Rusty_v8
    • 2.8 Tokio
    • 2.9 V8
    • 2.10 What's next
  • CHAPTER 3 - THREADING MODEL
    • 3.0 Cover page
    • 3.1 Threading model
    • 3.2 Default threading model
    • 3.3 Asynchronous green threads
    • 3.4 What's next
  • CHAPTER 4 - BRIDGE
    • 4.0 Cover page
    • 4.1 The bridge
    • 4.2 Print
    • 4.3 Encode and decode
    • 4.4 What's next
  • CHAPTER 5 - FOUNDATIONS
    • 5.0 Cover page
    • 5.1 Hello world program
    • 5.2 Basic hello world
    • 5.3 Main program of Deno
    • 5.4 Module Specifier
    • 5.5 CLI Factory
    • 5.6 Permissions
    • 5.7 Main Worker
    • 5.8 JS Runtime
    • 5.9 Run main module
    • 5.10 Load module
    • 5.11 Recursive module loading
    • 5.12 Module graphs
    • 5.13 File fetching
    • 5.14 Transpile
    • 5.15 Register / compile module
    • 5.16 Instantiate module
    • 5.17 Evaluate module
    • 5.18 What's next
  • CHAPTER 6 - IMPORTS AND OPS
    • 6.0 Cover page
    • 6.1 Imports and ops
    • 6.2 Hello world program v2
    • 6.3 Module graph with imports
    • 6.4 Transpile
    • 6.5 Registration and instantiation
    • 6.6 Registration of ops
    • 6.7 Evaluate module
    • 6.8 Sync OPs
    • 6.9 Debug logs
    • 6.10 What's next
  • CHAPTER 7 - LOCAL AND SESSION STORAGE
    • 7.0 Cover page
    • 7.1 Introduction
    • 7.2 Local storage
    • 7.3 Session storage
    • 7.4 What's next
  • AFTERWORD
    • Afterword
Powered by GitBook
On this page
  1. CHAPTER 6 - IMPORTS AND OPS

6.2 Hello world program v2

TS and JS code

We'll continue to use Typescript for our examples. The program is the same, with some additions:

  • There are two imports with one import has another inside it

  • There is a synchronous op

  • There is an asynchronous op

  • Also, there are some console logs which are also synchronous ops

The file name is helloV2.ts.

The location of the source file is:

/Users/mayankc/Work/source/denoExamples/helloV2.ts

Here is the code that we'll use to understand some new concepts:

import { nanoid } from "npm:nanoid";
import { getMachineId } from "https://deno.land/x/machine_id/mod.ts";

const id = nanoid();
const machineId = await getMachineId();
const homeDir = Deno.env.get("HOME");

function printNumber(input: number) {
  console.log(input);
}

function printString(input: string) {
  console.log(input);
}

printNumber(1);
printString("One");
console.log("Nanoid=", id, ", MachineId=", machineId, ", homeDir=", homeDir);

This remains a straightforward program consisting of a single TypeScript file. At the program's outset, two imports are used to access ES modules:

  • nanoid

  • machine_id

The 'nanoid' module is sourced from the NPM registry, while the 'machine_id' module is acquired from Deno's registry.

The program can be partitioned into three distinct segments:

  1. Import Section: The initial portion encompasses the two imports, serving the purpose of illustrating the internal mechanics of the import process.

  2. Synchronous and Asynchronous Operations: This phase involves invocations of both synchronous and asynchronous operations. The operations involving 'nanoid' and 'env.get' are categorized as synchronous, whereas the 'getMachineId' operation is asynchronous in nature.

  3. TypeScript Code: The subsequent part entails the inclusion of code extracted from 'helloLog.ts', showcasing the conversion process from TypeScript to JavaScript. This step is taken to offer a demonstration of how TypeScript code translates into its JavaScript equivalent.

Following the conversion to JavaScript, the program's structure transforms as illustrated below:

import { nanoid } from "npm:nanoid";
import { getMachineId } from "https://deno.land/x/machine_id/mod.ts";
const id = nanoid();
const machineId = await getMachineId();
const homeDir = Deno.env.get("HOME");
function printNumber(input) {
  console.log(input);
}
function printString(input) {
  console.log(input);
}
printNumber(1);
printString("One");
console.log("Nanoid=", id, ", MachineId=", machineId, ", homeDir=", homeDir);
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJz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UFHLENBQUM7QUFDZDtBQUVBLFlBQVk7QUFDWixZQUFZO0FBQ1osUUFBUSxHQUFHLENBQUMsV0FBVyxJQUFJLGdCQUFnQixXQUFXLGNBQWMifQ==

This section discusses the similarities and slight differences in the current version of the 'hello world' program compared to the previous one. The primary changes revolve around imports and operations (ops). It's worth noting that some ops, like console.log, were present in the previous version as well.

--

Let's take a moment to review. The primary worker in Deno operates through two main phases:

  • Module loading

  • Module evaluation

The process of loading a module can vary due to the presence of imports.

Now, we will discuss creating a module graph for a program with imports. This involves showing how modules connect and interact in a program.

To create a module graph, Deno analyzes the code to find all imported modules and their dependencies. It then organizes these modules in a structured way, showing their connections.

Previous6.1 Imports and opsNext6.3 Module graph with imports

Last updated 10 months ago