Add example wasm component and runner which embeds the component

This commit is contained in:
michaelpivato
2024-05-11 14:22:49 +09:30
commit fca84b9240
10 changed files with 1560 additions and 0 deletions

37
runner/src/main.rs Normal file
View 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(())
}