liveBook · Manning

WebAssembly module

As explained in Server-Side WebAssembly

A Wasm module is a compiled WebAssembly unit containing code and associated definitions that a runtime can instantiate and execute.

A WebAssembly (Wasm) module is the core, raw form of Wasm: a compiled binary built for execution by a JavaScript engine or server-side Wasm runtime. Its low-level foundation consists of four basic types and linear memory, enabling fast, sandboxed execution without assumptions about a particular operating system or platform. section 2.3.2 section 4.0 section 2.3.4

In Rust, configure a library to emit a module by setting `crate-type = ["cdylib"]`, then compile for a Wasm target. `wasm32-unknown-unknown` produces a generic module with no designated OS dependencies; release builds apply optimizations. The resulting `.wasm` file can be executed by a compatible runtime. section 2.3.2 section 2.3.4

Listing 2.2 hello_world_guest Cargo.toml file
[package]
name = "hello_world_guest"
version = "0.1.0"
edition = "2021"

[lib]
crate-type = ["cdylib"]

[dependencies]

This Cargo manifest turns the Rust guest library into a dynamic-library output suitable for building as a `.wasm` module. section 2.3.2

A raw Hello, World guest

A host can place a string in a module’s linear memory and call guest code using integer offsets and lengths. The guest returns data through the same shared memory arrangement. This works, but changing offsets or string-prefix lengths can corrupt results; multiple modules with different memory-management assumptions make custom contracts fragile. section 2.0 section 2.3.6

Modules deliberately leave richer data exchange to custom host contracts. Wasm components wrap modules and use WebAssembly Interface Types (WIT) plus the Canonical ABI to represent richer values and communicate with more generic hosts. Converting a module to a component wraps it, but does not by itself add WASI Preview 2 APIs. section 4.0 section 4.3

For distribution, a Wasm binary packages application logic rather than a guest userspace and system libraries. Compared with container images, this can yield smaller artifacts and faster startup. A Wasm module can also serve as a container entrypoint, run through a Wasm runtime managed by `containerd-shim-wasm`. section 7.1 section 7.2 section 7.3

How Server-Side WebAssembly covers it

How deep it goes
Explains compilation, linear-memory data exchange, component wrapping, and container deployment, while deferring richer module data management to components.
Examples
Uses a Rust Hello, World guest, a scratch-based Wasm container, and a converted Flask container binary.
What it assumes
Teaches raw Wasm string handling through a Rust project while requiring Rust tooling and Cargo configuration for compilation.
What you can do after
Builds from scratch Rust Wasm modules, inspects or wraps them with wasm-tools, and packages them as container entrypoints.
How it connects
Connects linear memory, Wasm runtimes, guest-host architecture, Wasm components, Canonical ABI, WIT, WASI, and containers.

Also explained in

Compare how 2 books explain WebAssembly module →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Server-Side WebAssembly. The section links point to the parts of the book it draws on.