Crates.io | wasmtime-provider |
lib.rs | wasmtime-provider |
version | 2.2.0 |
source | src |
created_at | 2020-09-23 16:45:14.09876 |
updated_at | 2024-11-26 09:00:25.817064 |
description | A wasmtime engine provider for the waPC host |
homepage | https://wapc.io |
repository | |
max_upload_size | |
id | 292123 |
size | 171,899 |
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.
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
SupportThe 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.
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
].
cargo run -p wasmtime-provider \
--example wasmtime-demo \
./wasm/crates/wasm-basic/build/wasm_basic.wasm \
ping "hi"
cargo run -p wasmtime-provider \
--example wasmtime-hash-mreplace \
AlexName