Add example wasm component and runner which embeds the component
This commit is contained in:
7
runner/Cargo.toml
Normal file
7
runner/Cargo.toml
Normal file
@@ -0,0 +1,7 @@
|
||||
[package]
|
||||
name = "wasmtime-test"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
wasmtime = {version = "20.0.2", features = ["component-model"]}
|
||||
37
runner/src/main.rs
Normal file
37
runner/src/main.rs
Normal file
@@ -0,0 +1,37 @@
|
||||
use wasmtime::{
|
||||
component::{bindgen, Component, Linker},
|
||||
Config, Engine, Store,
|
||||
};
|
||||
|
||||
// Defaults to 'wit' folder adjacent to cargo.toml.
|
||||
// Change folder by using: bindgen!(in "other/with/folder")
|
||||
bindgen!();
|
||||
|
||||
struct TestState {
|
||||
name: String,
|
||||
}
|
||||
|
||||
impl TestImports for TestState {
|
||||
fn hello(&mut self) -> std::result::Result<(), wasmtime::Error> {
|
||||
println!("{}", self.name);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn main() -> wasmtime::Result<()> {
|
||||
let mut config = Config::new();
|
||||
config.wasm_component_model(true);
|
||||
let engine = Engine::new(&config)?;
|
||||
let component = Component::from_file(&engine, "target/wasm32-wasi/release/component.wasm")?;
|
||||
let mut linker = Linker::new(&engine);
|
||||
Test::add_to_linker(&mut linker, |state: &mut TestState| state)?;
|
||||
let mut store = Store::new(
|
||||
&engine,
|
||||
TestState {
|
||||
name: "Hello World".to_string(),
|
||||
},
|
||||
);
|
||||
let (bindings, _) = Test::instantiate(&mut store, &component, &linker)?;
|
||||
bindings.call_greet(&mut store)?;
|
||||
Ok(())
|
||||
}
|
||||
6
runner/wit/test.wit
Normal file
6
runner/wit/test.wit
Normal file
@@ -0,0 +1,6 @@
|
||||
package my:project;
|
||||
|
||||
world test {
|
||||
import hello: func();
|
||||
export greet: func();
|
||||
}
|
||||
Reference in New Issue
Block a user