feat: add wit-bindgen-sample container
This commit is contained in:
1367
__wasm/wit-bindgen-sample/container/Cargo.lock
generated
Normal file
1367
__wasm/wit-bindgen-sample/container/Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
11
__wasm/wit-bindgen-sample/container/Cargo.toml
Normal file
11
__wasm/wit-bindgen-sample/container/Cargo.toml
Normal file
@@ -0,0 +1,11 @@
|
||||
[package]
|
||||
name = "container"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
anyhow = "1.0.58"
|
||||
wasmtime = "0.38.1"
|
||||
wit-bindgen-wasmtime = { path = '../wit-bindgen/crates/wasmtime' }
|
||||
72
__wasm/wit-bindgen-sample/container/src/main.rs
Normal file
72
__wasm/wit-bindgen-sample/container/src/main.rs
Normal file
@@ -0,0 +1,72 @@
|
||||
use anyhow::Result;
|
||||
use wasmtime::{Config, Engine, Instance, Linker, Module, Store};
|
||||
|
||||
use container::*;
|
||||
|
||||
wit_bindgen_wasmtime::export!("../container.wit");
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct MyContainer;
|
||||
|
||||
impl Container for MyContainer {
|
||||
fn fetch(&mut self, s: &str) -> String {
|
||||
format!("FETCHED: {}", s)
|
||||
}
|
||||
}
|
||||
|
||||
wit_bindgen_wasmtime::import!("../exports.wit");
|
||||
|
||||
|
||||
fn main() -> Result<()> {
|
||||
use exports::*;
|
||||
let wasm = "../engine/target/wasm32-unknown-unknown/debug/engine.wasm";
|
||||
let (exports, mut store) = instantiate(
|
||||
wasm,
|
||||
|linker| container::add_to_linker(linker, |cx| -> &mut MyContainer { &mut cx.imports }),
|
||||
|store, module, linker| Exports::instantiate(store, module, linker, |cx| &mut cx.exports),
|
||||
)?;
|
||||
let a = exports.eval_javascript(&mut store, "[script]");
|
||||
|
||||
println!("Hello, world! {:?}", a);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
struct Context<I, E> {
|
||||
imports: I,
|
||||
exports: E,
|
||||
}
|
||||
|
||||
fn instantiate<I: Default, E: Default, T>(
|
||||
wasm: &str,
|
||||
add_imports: impl FnOnce(&mut Linker<Context<I, E>>) -> Result<()>,
|
||||
mk_exports: impl FnOnce(
|
||||
&mut Store<Context<I, E>>,
|
||||
&Module,
|
||||
&mut Linker<Context<I, E>>,
|
||||
) -> Result<(T, Instance)>,
|
||||
) -> Result<(T, Store<Context<I, E>>)> {
|
||||
let engine = Engine::new(&default_config()?)?;
|
||||
let module = Module::from_file(&engine, wasm)?;
|
||||
|
||||
let mut linker = Linker::new(&engine);
|
||||
add_imports(&mut linker)?;
|
||||
|
||||
let mut store = Store::new(
|
||||
&engine,
|
||||
Context {
|
||||
imports: I::default(),
|
||||
exports: E::default(),
|
||||
},
|
||||
);
|
||||
let (exports, _instance) = mk_exports(&mut store, &module, &mut linker)?;
|
||||
Ok((exports, store))
|
||||
}
|
||||
|
||||
fn default_config() -> Result<Config> {
|
||||
// Create an engine with caching enabled to assist with iteration in this project.
|
||||
let mut config = Config::new();
|
||||
config.cache_config_load_default()?;
|
||||
config.wasm_backtrace_details(wasmtime::WasmBacktraceDetails::Enable);
|
||||
Ok(config)
|
||||
}
|
||||
Reference in New Issue
Block a user