55 lines
1.7 KiB
Rust
55 lines
1.7 KiB
Rust
![]() |
use crate::{AddedDecodersCount, DecoderPath};
|
||
|
use wasmtime::*;
|
||
|
|
||
|
pub fn add_decoders(decoder_paths: Vec<DecoderPath>) -> AddedDecodersCount {
|
||
|
println!("decoders in Tauri: {decoder_paths:#?}");
|
||
|
|
||
|
wasmtime_test().unwrap();
|
||
|
|
||
|
decoder_paths.len()
|
||
|
}
|
||
|
|
||
|
fn wasmtime_test() -> wasmtime::Result<()> {
|
||
|
let engine = Engine::default();
|
||
|
|
||
|
// Modules can be compiled through either the text or binary format
|
||
|
let wat = r#"
|
||
|
(module
|
||
|
(import "host" "host_func" (func $host_hello (param i32)))
|
||
|
|
||
|
(func (export "hello")
|
||
|
i32.const 3
|
||
|
call $host_hello)
|
||
|
)
|
||
|
"#;
|
||
|
let module = Module::new(&engine, wat)?;
|
||
|
|
||
|
// Host functionality can be arbitrary Rust functions and is provided
|
||
|
// to guests through a `Linker`.
|
||
|
let mut linker = Linker::new(&engine);
|
||
|
linker.func_wrap(
|
||
|
"host",
|
||
|
"host_func",
|
||
|
|caller: Caller<'_, u32>, param: i32| {
|
||
|
println!("Got {} from WebAssembly", param);
|
||
|
println!("my host state is: {}", caller.data());
|
||
|
},
|
||
|
)?;
|
||
|
|
||
|
// All wasm objects operate within the context of a "store". Each
|
||
|
// `Store` has a type parameter to store host-specific data, which in
|
||
|
// this case we're using `4` for.
|
||
|
let mut store: Store<u32> = Store::new(&engine, 4);
|
||
|
|
||
|
// Instantiation of a module requires specifying its imports and then
|
||
|
// afterwards we can fetch exports by name, as well as asserting the
|
||
|
// type signature of the function with `get_typed_func`.
|
||
|
let instance = linker.instantiate(&mut store, &module)?;
|
||
|
let hello = instance.get_typed_func::<(), ()>(&mut store, "hello")?;
|
||
|
|
||
|
// And finally we can call the wasm!
|
||
|
hello.call(&mut store, ())?;
|
||
|
|
||
|
Ok(())
|
||
|
}
|