11 unstable releases (3 breaking)

0.4.2 Aug 22, 2024
0.4.0 May 11, 2024
0.3.0 Mar 10, 2024
0.2.0 Dec 11, 2023
0.1.4 Sep 30, 2023

#87 in WebAssembly

Download history 89/week @ 2024-10-23 46/week @ 2024-10-30 30/week @ 2024-11-06 207/week @ 2024-11-13 135/week @ 2024-11-20 98/week @ 2024-11-27 129/week @ 2024-12-04 137/week @ 2024-12-11 38/week @ 2024-12-18 18/week @ 2025-01-01 139/week @ 2025-01-08 80/week @ 2025-01-15 34/week @ 2025-01-22 490/week @ 2025-01-29 356/week @ 2025-02-05

985 downloads per month
Used in 13 crates (10 directly)

MIT/Apache

70KB
1K SLoC

wasm_runtime_layer

Crates.io Docs.rs

wasm_runtime_layer creates a thin abstraction over WebAssembly runtimes, allowing for backend-agnostic host code. The interface is based upon the wasmtime and wasmi crates, but may be implemented for any runtime.

Usage

To use this crate, first instantiate a backend runtime. The runtime may be any value that implements backend::WasmEngine. Some runtimes are already implemented as additional crates. Then, one can create an Engine from the backend runtime, and use it to initialize modules and instances:

// 1. Instantiate a runtime
let engine = Engine::new(wasmi_runtime_layer::Engine::default());
let mut store = Store::new(&engine, ());

// 2. Create modules and instances, similar to other runtimes
let module_bin = wat::parse_str(
    r#"
(module
(type $t0 (func (param i32) (result i32)))
(func $add_one (export "add_one") (type $t0) (param $p0 i32) (result i32)
    local.get $p0
    i32.const 1
    i32.add))
"#,
)
.unwrap();

let module = Module::new(&engine, std::io::Cursor::new(&module_bin)).unwrap();
let instance = Instance::new(&mut store, &module, &Imports::default()).unwrap();

let add_one = instance
    .get_export(&store, "add_one")
    .unwrap()
    .into_func()
    .unwrap();
        
let mut result = [Value::I32(0)];
add_one
    .call(&mut store, &[Value::I32(42)], &mut result)
    .unwrap();

assert_eq!(result[0], Value::I32(43));

Backends

  • wasmi_runtime_layer - Implements the WasmEngine trait for wrappers around wasmtime::Engine instances.
  • wasmtime_runtime_layer - Implements the WasmEngine trait for wrappers around wasmtime::Engine instances.
  • js_wasm_runtime_layer - Implements a wasm engine targeting the browser's WebAssembly API on wasm32-unknown-unknown targets.
  • pyodide-webassembly-runtime-layer - Implements a wasm engine targeting the browser's WebAssembly API when running as a Python extension module inside Pyodide.

Contributions for additional backend implementations are welcome!

Testing

To run the tests for wasmi and wasmtime, run:

cargo test

For the wasm32 target, you can use the slower interpreter wasmi, or the native JIT accelerated browser backend.

To test the backends, you need to install wasm-pack.

You can then run:

wasm-pack test --node

Dependencies

~0.5–1MB
~22K SLoC

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy