2024-07-07 12:53:30 +00:00
|
|
|
use crate::{AddedDecodersCount, DecoderPath, RemovedDecodersCount};
|
2024-07-05 01:30:17 +00:00
|
|
|
use wasmtime::component::{Component as WasmtimeComponent, *};
|
2024-07-03 22:30:13 +00:00
|
|
|
use wasmtime::{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-04 14:16:50 +00:00
|
|
|
struct State {
|
|
|
|
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 12:53:30 +00:00
|
|
|
pub fn remove_all_decoders() -> RemovedDecodersCount {
|
|
|
|
156
|
|
|
|
}
|
|
|
|
|
2024-07-05 01:30:17 +00:00
|
|
|
// @TODO Make println work on Windows?
|
|
|
|
// https://github.com/tauri-apps/tauri/discussions/8626
|
|
|
|
|
2024-07-05 00:09:26 +00:00
|
|
|
// @TODO Remove / improve comments below
|
|
|
|
// Testing
|
2024-07-03 22:30:13 +00:00
|
|
|
// 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"])
|
2024-06-25 16:29:33 +00:00
|
|
|
pub fn add_decoders(decoder_paths: Vec<DecoderPath>) -> AddedDecodersCount {
|
|
|
|
println!("decoders in Tauri: {decoder_paths:#?}");
|
2024-07-05 00:09:26 +00:00
|
|
|
println!("Current dir: {:#?}", std::env::current_dir().unwrap());
|
2024-07-04 14:16:50 +00:00
|
|
|
let decoder_paths_len = decoder_paths.len();
|
2024-06-25 16:29:33 +00:00
|
|
|
|
2024-07-05 00:09:26 +00:00
|
|
|
// New thread to prevent panics caused by running runtime in runtime
|
|
|
|
// @TODO replace with Tokio's spawn_blocking?
|
2024-07-04 14:16:50 +00:00
|
|
|
std::thread::spawn(move || {
|
|
|
|
if let Err(error) = add_decoder(&decoder_paths[0]) {
|
|
|
|
eprintln!("add_decoders error: {error:?}");
|
|
|
|
}
|
2024-07-05 01:30:17 +00:00
|
|
|
})
|
|
|
|
.join()
|
|
|
|
.unwrap();
|
2024-06-25 16:29:33 +00:00
|
|
|
|
2024-07-04 14:16:50 +00:00
|
|
|
decoder_paths_len
|
2024-06-25 16:29:33 +00:00
|
|
|
}
|
|
|
|
|
2024-07-03 22:30:13 +00:00
|
|
|
fn add_decoder(path: &str) -> wasmtime::Result<()> {
|
2024-06-25 16:29:33 +00:00
|
|
|
let engine = Engine::default();
|
2024-07-04 14:16:50 +00:00
|
|
|
|
2024-07-03 22:30:13 +00:00
|
|
|
let wasmtime_component = WasmtimeComponent::from_file(&engine, path)?;
|
2024-07-05 01:30:17 +00:00
|
|
|
|
2024-07-03 22:30:13 +00:00
|
|
|
let mut linker = Linker::new(&engine);
|
2024-07-04 14:16:50 +00:00
|
|
|
wasmtime_wasi::add_to_linker_sync(&mut linker)?;
|
|
|
|
Component::add_to_linker(&mut linker, |state: &mut State| state)?;
|
2024-06-25 16:29:33 +00:00
|
|
|
|
2024-07-05 01:30:17 +00:00
|
|
|
let mut store = Store::new(
|
|
|
|
&engine,
|
|
|
|
State {
|
|
|
|
ctx: WasiCtx::builder().build(),
|
|
|
|
table: ResourceTable::new(),
|
|
|
|
},
|
|
|
|
);
|
2024-06-25 16:29:33 +00:00
|
|
|
|
2024-07-05 01:30:17 +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
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|