2024-07-07 12:53:30 +00:00
|
|
|
use crate::{AddedDecodersCount, DecoderPath, RemovedDecodersCount};
|
2024-07-07 21:53:37 +00:00
|
|
|
use once_cell::sync::Lazy;
|
|
|
|
use std::sync::Arc;
|
2024-07-08 00:08:20 +00:00
|
|
|
use tauri::async_runtime::{Mutex, RwLock};
|
2024-07-05 01:30:17 +00:00
|
|
|
use wasmtime::component::{Component as WasmtimeComponent, *};
|
2024-07-07 20:26:44 +00:00
|
|
|
use wasmtime::{AsContextMut, Engine, Store};
|
2024-07-04 14:16:50 +00:00
|
|
|
use wasmtime_wasi::{WasiCtx, WasiView};
|
2024-06-25 16:29:33 +00:00
|
|
|
|
2024-07-03 22:30:13 +00:00
|
|
|
bindgen!();
|
|
|
|
|
2024-07-08 00:08:20 +00:00
|
|
|
pub static DECODERS: Lazy<Arc<RwLock<Vec<Component>>>> = Lazy::new(<_>::default);
|
2024-07-07 20:26:44 +00:00
|
|
|
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
|
|
|
|
});
|
2024-07-08 00:08:20 +00:00
|
|
|
pub static STORE: Lazy<Arc<Mutex<Store<State>>>> = Lazy::new(|| {
|
2024-07-07 20:26:44 +00:00
|
|
|
let store = Store::new(
|
|
|
|
&ENGINE,
|
|
|
|
State {
|
|
|
|
ctx: WasiCtx::builder().build(),
|
|
|
|
table: ResourceTable::new(),
|
|
|
|
},
|
|
|
|
);
|
|
|
|
Arc::new(Mutex::new(store))
|
|
|
|
});
|
|
|
|
|
2024-07-08 00:08:20 +00:00
|
|
|
pub struct State {
|
2024-07-04 14:16:50 +00:00
|
|
|
ctx: WasiCtx,
|
|
|
|
table: ResourceTable,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl WasiView for State {
|
2024-07-05 01:30:17 +00:00
|
|
|
fn ctx(&mut self) -> &mut WasiCtx {
|
|
|
|
&mut self.ctx
|
|
|
|
}
|
|
|
|
fn table(&mut self) -> &mut ResourceTable {
|
|
|
|
&mut self.table
|
|
|
|
}
|
2024-07-04 14:16:50 +00:00
|
|
|
}
|
2024-07-03 22:30:13 +00:00
|
|
|
|
2024-07-04 14:16:50 +00:00
|
|
|
impl component::decoder::host::Host for State {
|
2024-07-05 00:09:26 +00:00
|
|
|
fn log(&mut self, message: String) {
|
2024-07-03 22:30:13 +00:00
|
|
|
println!("Decoder: {message}");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-07 20:26:44 +00:00
|
|
|
pub async fn remove_all_decoders() -> RemovedDecodersCount {
|
2024-07-08 00:08:20 +00:00
|
|
|
let mut decoders = DECODERS.write().await;
|
2024-07-07 20:26:44 +00:00
|
|
|
let decoders_count = decoders.len();
|
|
|
|
decoders.clear();
|
|
|
|
decoders_count
|
2024-07-07 12:53:30 +00:00
|
|
|
}
|
|
|
|
|
2024-07-07 20:26:44 +00:00
|
|
|
// @TODO Make println work on Windows in release mode?
|
2024-07-05 01:30:17 +00:00
|
|
|
// https://github.com/tauri-apps/tauri/discussions/8626
|
|
|
|
|
2024-07-05 00:09:26 +00:00
|
|
|
// @TODO Remove / improve comments below
|
|
|
|
// Testing
|
2024-07-08 00:23:48 +00:00
|
|
|
//
|
|
|
|
// Rust
|
2024-07-03 22:30:13 +00:00
|
|
|
// FW.add_decoders(["../test_files/components/rust_decoder/rust_decoder.wasm"])
|
2024-07-07 21:53:37 +00:00
|
|
|
// FW.add_decoders(["../test_files/components/rust_decoder/rust_decoder.wasm", "../test_files/components/rust_decoder/rust_decoder.wasm"])
|
2024-07-08 00:23:48 +00:00
|
|
|
//
|
|
|
|
// JS
|
2024-07-03 22:30:13 +00:00
|
|
|
// FW.add_decoders(["../test_files/components/javascript_decoder/javascript_decoder.wasm"])
|
2024-07-08 00:23:48 +00:00
|
|
|
//
|
|
|
|
// Python
|
2024-07-03 22:30:13 +00:00
|
|
|
// FW.add_decoders(["../test_files/components/python_decoder/python_decoder.wasm"])
|
2024-07-08 00:23:48 +00:00
|
|
|
//
|
|
|
|
// Remove all
|
|
|
|
// FW.remove_all_decoders()
|
|
|
|
//
|
|
|
|
// All Debug
|
|
|
|
// FW.add_decoders(["../test_files/components/rust_decoder/rust_decoder.wasm", "../test_files/components/javascript_decoder/javascript_decoder.wasm", "../test_files/components/python_decoder/python_decoder.wasm"])
|
|
|
|
//
|
|
|
|
// All Release
|
|
|
|
// FW.add_decoders(["../../test_files/components/rust_decoder/rust_decoder.wasm", "../../test_files/components/javascript_decoder/javascript_decoder.wasm", "../../test_files/components/python_decoder/python_decoder.wasm"])
|
2024-07-07 20:26:44 +00:00
|
|
|
pub async fn add_decoders(decoder_paths: Vec<DecoderPath>) -> AddedDecodersCount {
|
2024-07-08 12:30:49 +00:00
|
|
|
println!("Decoders: {decoder_paths:#?}");
|
2024-07-05 00:09:26 +00:00
|
|
|
println!("Current dir: {:#?}", std::env::current_dir().unwrap());
|
2024-06-25 16:29:33 +00:00
|
|
|
|
2024-07-07 21:53:37 +00:00
|
|
|
let mut added_decoders_count = 0;
|
|
|
|
|
2024-07-08 12:38:30 +00:00
|
|
|
// @TODO (?) New thread to prevent "Cannot start a runtime from within a runtime."
|
|
|
|
// when a call to a component fails / panics
|
|
|
|
// std::thread::spawn(move || {
|
|
|
|
// futures::executor::block_on(async move {
|
2024-07-07 21:53:37 +00:00
|
|
|
for decoder_path in decoder_paths {
|
|
|
|
if let Err(error) = add_decoder(&decoder_path).await {
|
2024-07-04 14:16:50 +00:00
|
|
|
eprintln!("add_decoders error: {error:?}");
|
2024-07-07 21:53:37 +00:00
|
|
|
} else {
|
|
|
|
added_decoders_count += 1;
|
2024-07-04 14:16:50 +00:00
|
|
|
}
|
2024-07-07 21:53:37 +00:00
|
|
|
}
|
2024-07-08 12:38:30 +00:00
|
|
|
// })
|
|
|
|
// }).join().unwrap();
|
2024-06-25 16:29:33 +00:00
|
|
|
|
2024-07-07 21:53:37 +00:00
|
|
|
added_decoders_count
|
2024-06-25 16:29:33 +00:00
|
|
|
}
|
|
|
|
|
2024-07-07 20:26:44 +00:00
|
|
|
async fn add_decoder(path: &str) -> wasmtime::Result<()> {
|
|
|
|
let wasmtime_component = WasmtimeComponent::from_file(&ENGINE, path)?;
|
2024-07-05 01:30:17 +00:00
|
|
|
|
2024-07-07 20:26:44 +00:00
|
|
|
let mut store_lock = STORE.lock().await;
|
|
|
|
let mut store = store_lock.as_context_mut();
|
2024-06-25 16:29:33 +00:00
|
|
|
|
2024-07-07 20:26:44 +00:00
|
|
|
let component = Component::instantiate(&mut store, &wasmtime_component, &LINKER)?;
|
2024-06-25 16:29:33 +00:00
|
|
|
|
2024-07-05 01:30:17 +00:00
|
|
|
println!(
|
|
|
|
"Decoder name: {}",
|
|
|
|
component
|
|
|
|
.component_decoder_decoder()
|
|
|
|
.call_name(&mut store)?
|
|
|
|
);
|
|
|
|
component
|
|
|
|
.component_decoder_decoder()
|
|
|
|
.call_init(&mut store)?;
|
2024-06-25 16:29:33 +00:00
|
|
|
|
2024-07-08 00:08:20 +00:00
|
|
|
DECODERS.write().await.push(component);
|
2024-07-07 20:26:44 +00:00
|
|
|
|
2024-06-25 16:29:33 +00:00
|
|
|
Ok(())
|
|
|
|
}
|