Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Practical WebAssembly Components with Wasmtime

A tutorial series on building a Rust plugin system using the WebAssembly Component Model and Wasmtime.

The Problem

Without the Component Model, passing data between a host and a Wasm plugin was manual and error-prone. You wrote bytes into Wasm memory with pointer arithmetic, reconstructed strings with from_raw_parts inside unsafe blocks, and serialized structured data to flat byte arrays. Every plugin needed its own memory management protocol. One wrong offset and you’re debugging memory corruption.

The Component Model fixes this. You define your interface in WIT (a language-agnostic interface definition), and wit-bindgen generates type-safe bindings on both sides. The host passes a Rust string. The plugin receives a Rust string. No serialization. No pointer arithmetic. No unsafe.

Overview

This series shows how to build a plugin architecture using Wasmtime and the Component Model. The Component Model handles all the plumbing — memory, strings, type safety — natively, so you focus on the interesting parts.

Parts

  1. Hello, Components — Define a WIT interface, write a plugin that takes a string and returns its length, and run it with Wasmtime
  2. Building a Plugin Host — Pass structured data (records, lists) across the boundary and build a host that loads multiple plugins
  3. Real World — An mdbook Preprocessor — Build a real mdbook preprocessor, add host imports, and see the full architecture
  4. Error Handling and Resources — Handle errors across the boundary with result<T, E>, pass stateful handles with WIT resource
  5. Composing Components — Wire multiple components together with wac, build pipelines where components call each other directly
  6. Building a Real Plugin System — Put it all together: discover, load, chain, and handle errors in a production-style plugin pipeline
  7. Testing Across the Boundary — Unit tests, integration tests, and contract tests for Wasm component systems — three levels that catch different bugs