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
  • Typescript code
  • Javascript code
  1. CHAPTER 5 - FOUNDATIONS

5.2 Basic hello world

Typescript code

Let's begin by exploring the most straightforward hello world program, written using Typescript. This program could also be referred to as a console print program, as it outputs information to the console. Throughout this discussion, we'll use the terms "hello world" and "console print" interchangeably.

There are two key motivations for using Typescript over Javascript within the context of Deno:

  1. Deno inherently supports Typescript, making it the preferred choice for developing user-level programs. While Deno does provide support for Javascript, the core essence of using Deno could be undermined if Typescript isn't used.

  2. Creating a program in Typescript allows us to observe the process of converting it into Javascript. It's important to recall that the V8 engine, which powers Deno, can only execute Javascript code.

Now, let's look at the simplest console print program written in TypeScript. This program is slightly different from a typical console log program. We made this variation to show how TypeScript works and how it is converted to JavaScript. By examining this program, we can gain valuable insight into how Deno works with different programming languages.

The file name is helloLog.ts.

The location of the source file is:

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

The purpose of indicating the path of the source file is to demonstrate how it is retrieved during the compilation of the program. This provides insight into the process of fetching the necessary files. Let's take a look at the content present within the "helloLog.ts" source file:

// File helloLog.ts

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

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

printNumber(1);
printString("One");

The program is quite straightforward. It consists of just two functions: one for displaying numbers and the other for showing strings. It might not seem like much, but these functions are enough to understand the very basics of Deno.

Javascript code

As we discussed earlier, Deno comes with built-in support for Typescript. However, it's important to note that the V8 engine, which powers Deno, is designed to execute Javascript code exclusively. This means that before code can run in V8, any Typescript code must be transformed into Javascript. Deno takes on the task of converting Typescript to Javascript prior to loading and running it within the V8 engine.

To achieve this conversion, Deno relies on a Rust-based tool called the SWC compiler. This compiler is responsible for translating Typescript code into its Javascript counterpart, ensuring that it becomes compatible with the V8 engine's execution environment. This translation process is a pivotal step in the Deno ecosystem, enabling developers to seamlessly work with Typescript while still benefiting from the performance and capabilities of the V8 engine.

Additionally, when there's a need to perform type-checking during the startup phase, Deno leverages Microsoft's TSC compiler. This step is crucial in ensuring that the Typescript code adheres to the specified type rules and structures, helping catch potential errors early in the development process. By incorporating TSC into its workflow, Deno provides developers with a comprehensive solution for managing Typescript code from conversion to type validation.

The following is the output of the converted input file:

// File helloLog.ts
function printNumber(input) {
    console.log(input);
}
function printString(input) {
    console.log(input);
}
printNumber(1);
printString("One");
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImZpbGU6Ly8vVXNlcnMvbWF5YW5rYy9Xb3JrL3NvdXJjZS9kZW5vRXhhbXBsZXMvaGVsbG9Mb2cudHMiXSwic291cmNlc0NvbnRlbnQiOlsiLy8gRmlsZSBoZWxsb0xvZy50c1xuXG5mdW5jdGlvbiBwcmludE51bWJlcihpbnB1dDogbnVtYmVyKSB7XG4gIGNvbnNvbGUubG9nKGlucHV0KTtcbn1cblxuZnVuY3Rpb24gcHJpbnRTdHJpbmcoaW5wdXQ6IHN0cmluZykge1xuICBjb25zb2xlLmxvZyhpbnB1dCk7XG59XG5cbnByaW50TnVtYmVyKDEpO1xucHJpbnRTdHJpbmcoXCJPbmVcIik7XG4iXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsbUJBQW1CO0FBRW5CLFNBQVMsWUFBWSxLQUFhO0lBQ2hDLFFBQVEsSUFBSTtBQUNkO0FBRUEsU0FBUyxZQUFZLEtBQWE7SUFDaEMsUUFBUSxJQUFJO0FBQ2Q7QUFFQSxZQUFZO0FBQ1osWUFBWSJ9

Converting the code was a simple process. During compilation, the argument types were checked and aligned, and then the specific type details were removed to create clean JavaScript code.

Note that there is a comment at the end about "sourceMappingURL." This comment is for Deno's internal use and helps it function. This program is simple because it doesn't have imports or other operations. We will discuss imports and operations in more detail in the next chapter.

Although this program is basic, it helps us understand the fundamental concepts. Let's run it now:

> deno run helloLog.ts 
1
One

Now, let's start our journey by examining the main program of Deno. This will give us a solid foundation to build on as we move forward.

Previous5.1 Hello world programNext5.3 Main program of Deno

Last updated 10 months ago