3 Enhancing portability and security with Wasm components

 

This chapter covers

  • How Wasm components provide enhanced portability and security
  • Building a Wasm component with JavaScript
  • Building a custom runtime for a Wasm component with Python
  • Implementing host-side functionality from a WIT file in Rust

In chapter 2, we built our first Wasm app and compiled it into a Wasm module—the thing most people think of when they hear "Wasm." But as we saw, a module on its own is stuck with only simple types like integers and floats. You can build more complex types from those, but that means both the guest and the Wasm runtime need to agree on the exact memory layout. For any real application, that's a headache.

This is where WIT comes in. It gives us a way to define higher-level types without all the manual layout work. But WIT isn't the whole story. It's part of something bigger: the Wasm Component Model. The model uses WIT as its interface definition language (IDL) and serves as a wrapper around Wasm modules. The goal is to let different Wasm applications talk to each other without all the manual memory wrangling or those fragile, implicit agreements about where structures live in linear memory.

3.1 Benefits of Wasm components

3.1.1 Cross-language portability

3.1.2 Shared-nothing security

3.1.3 Only-what-you-need security and improved vulnerability mitigation response

3.2 Building a componentized "Hello, World" guest application with JavaScript

3.2.1 Setting up the project and installing the toolchain

3.2.2 Implementing the component logic

3.2.3 Compiling the Wasm component

3.3 Building a Python host for a JavaScript bundled Wasm component

3.3.1 Setting up the project and installing the toolchain

3.3.2 Implementing the host logic

3.3.3 Running the Python host

3.4 Expanding the example project with Wasm components

3.4.1 Improving the WIT file for the key-value store

3.4.2 Creating the Smart CMS host

3.4.3 Implementing and running a guest Wasm component inside our smart CMS host

3.5 Summary