This commit is contained in:
Martin Kavík 2024-07-07 22:26:44 +02:00
parent b2055ff22d
commit 87e650b3a8
4 changed files with 44 additions and 29 deletions

1
Cargo.lock generated
View file

@ -1691,6 +1691,7 @@ checksum = "9fc0510504f03c51ada170672ac806f1f105a88aa97a5281117e1ddc3368e51a"
name = "fastwave"
version = "0.1.0"
dependencies = [
"once_cell",
"serde",
"serde_json",
"shared",

View file

@ -23,6 +23,7 @@ serde = { version = "1.0", features = ["derive"] }
tauri = { version = "=2.0.0-beta.22", features = ["macos-private-api", "linux-ipc-protocol"] }
tauri-plugin-window-state = "=2.0.0-beta.9"
tauri-plugin-dialog = "=2.0.0-beta.9"
once_cell = "1.19.0"
# wasmtime = "22.0.0"
# wasmtime-wasi = "22.0.0"

View file

@ -1,10 +1,32 @@
use crate::{AddedDecodersCount, DecoderPath, RemovedDecodersCount};
use wasmtime::component::{Component as WasmtimeComponent, *};
use wasmtime::{Engine, Store};
use wasmtime::{AsContextMut, Engine, Store};
use wasmtime_wasi::{WasiCtx, WasiView};
use once_cell::sync::Lazy;
use tauri::async_runtime::Mutex;
use std::sync::Arc;
bindgen!();
static DECODERS: Lazy<Arc<Mutex<Vec<Component>>>> = Lazy::new(<_>::default);
static ENGINE: Lazy<Engine> = Lazy::new(<_>::default);
static LINKER: Lazy<Linker<State>> = Lazy::new(|| {
let mut linker = Linker::new(&ENGINE);
wasmtime_wasi::add_to_linker_sync(&mut linker).unwrap();
Component::add_to_linker(&mut linker, |state: &mut State| state).unwrap();
linker
});
static STORE: Lazy<Arc<Mutex<Store<State>>>> = Lazy::new(|| {
let store = Store::new(
&ENGINE,
State {
ctx: WasiCtx::builder().build(),
table: ResourceTable::new(),
},
);
Arc::new(Mutex::new(store))
});
struct State {
ctx: WasiCtx,
table: ResourceTable,
@ -25,11 +47,14 @@ impl component::decoder::host::Host for State {
}
}
pub fn remove_all_decoders() -> RemovedDecodersCount {
156
pub async fn remove_all_decoders() -> RemovedDecodersCount {
let mut decoders = DECODERS.lock().await;
let decoders_count = decoders.len();
decoders.clear();
decoders_count
}
// @TODO Make println work on Windows?
// @TODO Make println work on Windows in release mode?
// https://github.com/tauri-apps/tauri/discussions/8626
// @TODO Remove / improve comments below
@ -37,42 +62,28 @@ pub fn remove_all_decoders() -> RemovedDecodersCount {
// FW.add_decoders(["../test_files/components/rust_decoder/rust_decoder.wasm"])
// FW.add_decoders(["../test_files/components/javascript_decoder/javascript_decoder.wasm"])
// FW.add_decoders(["../test_files/components/python_decoder/python_decoder.wasm"])
pub fn add_decoders(decoder_paths: Vec<DecoderPath>) -> AddedDecodersCount {
pub async fn add_decoders(decoder_paths: Vec<DecoderPath>) -> AddedDecodersCount {
println!("decoders in Tauri: {decoder_paths:#?}");
println!("Current dir: {:#?}", std::env::current_dir().unwrap());
let decoder_paths_len = decoder_paths.len();
// New thread to prevent panics caused by running runtime in runtime
// @TODO replace with Tokio's spawn_blocking?
std::thread::spawn(move || {
if let Err(error) = add_decoder(&decoder_paths[0]) {
tauri::async_runtime::spawn_blocking(move || async move {
if let Err(error) = add_decoder(&decoder_paths[0]).await {
eprintln!("add_decoders error: {error:?}");
}
})
.join()
.unwrap();
}).await.unwrap().await;
decoder_paths_len
}
fn add_decoder(path: &str) -> wasmtime::Result<()> {
let engine = Engine::default();
async fn add_decoder(path: &str) -> wasmtime::Result<()> {
let wasmtime_component = WasmtimeComponent::from_file(&ENGINE, path)?;
let wasmtime_component = WasmtimeComponent::from_file(&engine, path)?;
let mut store_lock = STORE.lock().await;
let mut store = store_lock.as_context_mut();
let mut linker = Linker::new(&engine);
wasmtime_wasi::add_to_linker_sync(&mut linker)?;
Component::add_to_linker(&mut linker, |state: &mut State| state)?;
let mut store = Store::new(
&engine,
State {
ctx: WasiCtx::builder().build(),
table: ResourceTable::new(),
},
);
let component = Component::instantiate(&mut store, &wasmtime_component, &linker)?;
let component = Component::instantiate(&mut store, &wasmtime_component, &LINKER)?;
println!(
"Decoder name: {}",
@ -84,5 +95,7 @@ fn add_decoder(path: &str) -> wasmtime::Result<()> {
.component_decoder_decoder()
.call_init(&mut store)?;
DECODERS.lock().await.push(component);
Ok(())
}

View file

@ -98,12 +98,12 @@ async fn unload_signal(signal_ref_index: usize, store: tauri::State<'_, Store>)
#[tauri::command(rename_all = "snake_case")]
async fn add_decoders(decoder_paths: Vec<DecoderPath>) -> Result<AddedDecodersCount, ()> {
Ok(component_manager::add_decoders(decoder_paths))
Ok(component_manager::add_decoders(decoder_paths).await)
}
#[tauri::command(rename_all = "snake_case")]
async fn remove_all_decoders() -> Result<RemovedDecodersCount, ()> {
Ok(component_manager::remove_all_decoders())
Ok(component_manager::remove_all_decoders().await)
}
#[cfg_attr(mobile, tauri::mobile_entry_point)]