2 Building server-side applications with Wasm modules

 

This chapter covers

  • Building a Wasm "Hello, World" with Rust
  • Understanding Wasm memory and data types
  • Understanding the guest-host architecture of server-side Wasm applications
  • Writing and compiling Rust code to Wasm
  • Executing a Wasm module with a custom Wasm runtime

In this chapter, we're going to build a simple "Hello, World" application. As illustrated in figure 2.1, our app will take a string as input and output that string with "Hello, " in front of it. While this example might seem straightforward, implementing it in Wasm is far from trivial—Wasm doesn't have a built-in string type, so handling text requires a low-level approach.

Before exploring more advanced features, we'll work through this challenge using Wasm's raw capabilities. This will give us a deep understanding of how data is managed at this level and lay the groundwork for appreciating the more efficient techniques introduced later.

Figure 2.1 High-level view of our "Hello, World" application.

2.1 Understanding Wasm's building blocks

We will begin by rolling up our sleeves and working with the fundamental elements that Wasm offers out of the box: linear memory, and four primitive data types (i.e., integers and floats, both in 32- and 64-bit sizes).

2.2 Understanding the guest-host architecture of server-side Wasm

2.3 Creating a Wasm app using Rust

2.3.1 Creating a Rust library project

2.3.2 Specifying compilation to a Wasm module

2.3.3 Writing the code for our application

2.3.4 Compiling the Rust code into a Wasm module

2.3.5 Executing the Wasm module with the custom host

2.3.6 Reflecting on the "Hello, World" application

2.4 Building on Wasm’s simple types with WebAssembly Interface Types

2.5 Building the foundation of a smart content management system (CMS)

2.6 Summary