35 stable releases

new 2.3.1 Jan 14, 2025
2.2.0 Nov 26, 2024
1.21.0 Sep 23, 2024
1.19.1 Jul 11, 2024
0.0.2 Oct 27, 2020

#56 in WebAssembly

Download history 830/week @ 2024-09-25 291/week @ 2024-10-02 799/week @ 2024-10-09 580/week @ 2024-10-16 624/week @ 2024-10-23 253/week @ 2024-10-30 267/week @ 2024-11-06 226/week @ 2024-11-13 684/week @ 2024-11-20 576/week @ 2024-11-27 968/week @ 2024-12-04 1095/week @ 2024-12-11 1420/week @ 2024-12-18 1055/week @ 2024-12-25 983/week @ 2025-01-01 1230/week @ 2025-01-08

4,792 downloads per month
Used in 2 crates (via wascc-host)

Apache-2.0

70KB
1.5K SLoC

Wasmtime Engine Provider

crates.io license

This is a pluggable engine provider for the waPC RPC exchange protocol. This engine implements WebAssemblyEngineProvider for the the Bytecode Alliance's wasmtime WebAssembly runtime.

Usage

use wasmtime_provider::WasmtimeEngineProviderBuilder;
use wapc::WapcHost;
use std::error::Error;

pub fn main() -> Result<(), Box<dyn Error>> {

  // Sample host callback that prints the operation a WASM module requested.
  let host_callback = |id: u64, bd: &str, ns: &str, op: &str, payload: &[u8]| {
    println!("Guest {} invoked '{}->{}:{}' with a {} byte payload",
    id, bd, ns, op, payload.len());
    // Return success with zero-byte payload.
    Ok(vec![])
  };

  let file = "../../wasm/crates/wasm-basic/build/wasm_basic.wasm";
  let module_bytes = std::fs::read(file)?;

  let engine = WasmtimeEngineProviderBuilder::new()
    .module_bytes(&module_bytes)
    .build()?;
  let host = WapcHost::new(Box::new(engine), Some(Box::new(host_callback)))?;

  let res = host.call("ping", b"payload bytes")?;
  assert_eq!(res, b"payload bytes");

  Ok(())
}

async Support

The async feature enables the usage of this provider inside of an async context.

Note: this feature relies on the tokio runtime.

Check the WasmtimeEngineProviderAsync for more details.

Creating a new instance

The WasmtimeEngineProviderBuilder is used to create new instances of WasmtimeEngineProvider and WasmtimeEngineProviderAsync.

Fresh instances of the engines can be created by using pre-initialized instances like WasmtimeEngineProviderPre and WasmtimeEngineProviderAsyncPre.

Examples

Running ping demo

cargo run -p wasmtime-provider \
    --example wasmtime-demo \
    ./wasm/crates/wasm-basic/build/wasm_basic.wasm \
    ping "hi"

Running codec and module hotswapping demo

cargo run -p wasmtime-provider \
    --example wasmtime-hash-mreplace \
    AlexName

See also

Dependencies

~33–47MB
~876K SLoC