DECODERS
This commit is contained in:
parent
b2055ff22d
commit
87e650b3a8
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -1691,6 +1691,7 @@ checksum = "9fc0510504f03c51ada170672ac806f1f105a88aa97a5281117e1ddc3368e51a"
|
||||||
name = "fastwave"
|
name = "fastwave"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"once_cell",
|
||||||
"serde",
|
"serde",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
"shared",
|
"shared",
|
||||||
|
|
|
@ -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 = { version = "=2.0.0-beta.22", features = ["macos-private-api", "linux-ipc-protocol"] }
|
||||||
tauri-plugin-window-state = "=2.0.0-beta.9"
|
tauri-plugin-window-state = "=2.0.0-beta.9"
|
||||||
tauri-plugin-dialog = "=2.0.0-beta.9"
|
tauri-plugin-dialog = "=2.0.0-beta.9"
|
||||||
|
once_cell = "1.19.0"
|
||||||
|
|
||||||
# wasmtime = "22.0.0"
|
# wasmtime = "22.0.0"
|
||||||
# wasmtime-wasi = "22.0.0"
|
# wasmtime-wasi = "22.0.0"
|
||||||
|
|
|
@ -1,10 +1,32 @@
|
||||||
use crate::{AddedDecodersCount, DecoderPath, RemovedDecodersCount};
|
use crate::{AddedDecodersCount, DecoderPath, RemovedDecodersCount};
|
||||||
use wasmtime::component::{Component as WasmtimeComponent, *};
|
use wasmtime::component::{Component as WasmtimeComponent, *};
|
||||||
use wasmtime::{Engine, Store};
|
use wasmtime::{AsContextMut, Engine, Store};
|
||||||
use wasmtime_wasi::{WasiCtx, WasiView};
|
use wasmtime_wasi::{WasiCtx, WasiView};
|
||||||
|
use once_cell::sync::Lazy;
|
||||||
|
use tauri::async_runtime::Mutex;
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
bindgen!();
|
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 {
|
struct State {
|
||||||
ctx: WasiCtx,
|
ctx: WasiCtx,
|
||||||
table: ResourceTable,
|
table: ResourceTable,
|
||||||
|
@ -25,11 +47,14 @@ impl component::decoder::host::Host for State {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn remove_all_decoders() -> RemovedDecodersCount {
|
pub async fn remove_all_decoders() -> RemovedDecodersCount {
|
||||||
156
|
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
|
// https://github.com/tauri-apps/tauri/discussions/8626
|
||||||
|
|
||||||
// @TODO Remove / improve comments below
|
// @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/rust_decoder/rust_decoder.wasm"])
|
||||||
// FW.add_decoders(["../test_files/components/javascript_decoder/javascript_decoder.wasm"])
|
// FW.add_decoders(["../test_files/components/javascript_decoder/javascript_decoder.wasm"])
|
||||||
// FW.add_decoders(["../test_files/components/python_decoder/python_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!("decoders in Tauri: {decoder_paths:#?}");
|
||||||
println!("Current dir: {:#?}", std::env::current_dir().unwrap());
|
println!("Current dir: {:#?}", std::env::current_dir().unwrap());
|
||||||
let decoder_paths_len = decoder_paths.len();
|
let decoder_paths_len = decoder_paths.len();
|
||||||
|
|
||||||
// New thread to prevent panics caused by running runtime in runtime
|
// New thread to prevent panics caused by running runtime in runtime
|
||||||
// @TODO replace with Tokio's spawn_blocking?
|
tauri::async_runtime::spawn_blocking(move || async move {
|
||||||
std::thread::spawn(move || {
|
if let Err(error) = add_decoder(&decoder_paths[0]).await {
|
||||||
if let Err(error) = add_decoder(&decoder_paths[0]) {
|
|
||||||
eprintln!("add_decoders error: {error:?}");
|
eprintln!("add_decoders error: {error:?}");
|
||||||
}
|
}
|
||||||
})
|
}).await.unwrap().await;
|
||||||
.join()
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
decoder_paths_len
|
decoder_paths_len
|
||||||
}
|
}
|
||||||
|
|
||||||
fn add_decoder(path: &str) -> wasmtime::Result<()> {
|
async fn add_decoder(path: &str) -> wasmtime::Result<()> {
|
||||||
let engine = Engine::default();
|
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);
|
let component = Component::instantiate(&mut store, &wasmtime_component, &LINKER)?;
|
||||||
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)?;
|
|
||||||
|
|
||||||
println!(
|
println!(
|
||||||
"Decoder name: {}",
|
"Decoder name: {}",
|
||||||
|
@ -84,5 +95,7 @@ fn add_decoder(path: &str) -> wasmtime::Result<()> {
|
||||||
.component_decoder_decoder()
|
.component_decoder_decoder()
|
||||||
.call_init(&mut store)?;
|
.call_init(&mut store)?;
|
||||||
|
|
||||||
|
DECODERS.lock().await.push(component);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -98,12 +98,12 @@ async fn unload_signal(signal_ref_index: usize, store: tauri::State<'_, Store>)
|
||||||
|
|
||||||
#[tauri::command(rename_all = "snake_case")]
|
#[tauri::command(rename_all = "snake_case")]
|
||||||
async fn add_decoders(decoder_paths: Vec<DecoderPath>) -> Result<AddedDecodersCount, ()> {
|
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")]
|
#[tauri::command(rename_all = "snake_case")]
|
||||||
async fn remove_all_decoders() -> Result<RemovedDecodersCount, ()> {
|
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)]
|
#[cfg_attr(mobile, tauri::mobile_entry_point)]
|
||||||
|
|
Loading…
Reference in a new issue