liveBook · Manning

WasmEdge

As explained in Refactoring to Rust

The WasmEdge SDK is a software development kit that provides APIs and functions for integrating WebAssembly modules with the WasmEdge runtime and host system.

WasmEdge is a WebAssembly runtime with SDKs for embedding Wasm modules in applications. It provides the runner and integration tools needed to move a piece of application logic into a Wasm module without handling all the low-level tooling directly. WASI supplies the interface through which the module uses runtime-provided capabilities. section 10.1

A Rust search application can move into Wasm in small steps:

  • Keep the search function and its data structures in a library, separate from the executable that calls them. section 10.2
  • Add the wasm32-wasi target, compile the executable with cargo build --target wasm32-wasi, and run the resulting .wasm file with wasmedge. section 10.2
  • To call a function rather than start the executable, export it with #[no_mangle] and invoke it using wasmedge --reactor. section 10.3

The two calling styles have different interfaces. Running the module as an executable lets the search example read a term from command-line arguments. Calling its exported function directly uses Wasm-supported primitive values, so the example accepts numeric page and offset arguments and fixes the search term inside the function. Passing strings or other complex data directly requires an additional contract between module and runtime. section 10.2 section 10.3

Embed the search module in a Rust CLI

Add wasmedge-sdk = "0.11.2" to the host CLI. Configure a VM with WASI enabled, register the compiled .wasm file, then call its exported static_search function with params!(0, 1). The same host can call _start to run the module’s executable entry point instead. section 10.4

Listing 10.23 journal_cli/src/main.rs: Calling the main Wasm function
fn main() -> Result<(), Box<dyn std::error::Error>>{
...
    vm.register_module_from_file(
        target.as_str(), &wasm_file)?
        .run_func(
            Some(target.as_str()),
            "static_search", params!(0, 1))?; #1
    Ok(())
}

The register_module_from_file and run_func call shows how the host invokes the exported search function with two numeric arguments. section 10.4

Choose the runner when executing a Wasm binary is enough; use the SDK when a Rust application needs to load and call a module itself. Direct function calls make the interface more explicit, but complex values require more work than the numeric arguments shown here. section 10.3 section 10.4

How Refactoring to Rust covers it

How deep it goes
Walks through runner commands, an exported function, and SDK-based VM calls; defers the mechanism for passing complex values.
Examples
Uses a Hello World module and a search CLI that calls static_search or _start.
What it assumes
Builds on a Rust search function and Cargo projects while introducing the WASI target and VM setup through examples.
What you can do after
Build it from scratch as a Rust CLI that registers and calls a Wasm search module with wasmedge-sdk.
How it connects
Connects WebAssembly, WASI, ABI, Rust Cargo, and VM embedding through a search module.

Related concepts

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