FastWave2.0/src-tauri/src/component_manager.rs

55 lines
1.7 KiB
Rust
Raw Normal View History

2024-06-25 16:29:33 +00:00
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(())
}