diff --git a/frontend/src/script_bridge.rs b/frontend/src/script_bridge.rs index 2e57894..4b2b88e 100644 --- a/frontend/src/script_bridge.rs +++ b/frontend/src/script_bridge.rs @@ -68,7 +68,7 @@ impl FW { Vec::new() } - /// JS: `FW.add_decoders(["test_files/components/rust_decoder/rust_decoder.wasm"])` -> `1` + /// JS: `FW.add_decoders(["../test_files/components/rust_decoder/rust_decoder.wasm"])` -> `1` pub async fn add_decoders(decoder_paths: Vec) -> AddedDecodersCount { platform::add_decoders(decoder_paths).await } diff --git a/src-tauri/src/component_manager.rs b/src-tauri/src/component_manager.rs index 6d4ec6a..58181cb 100644 --- a/src-tauri/src/component_manager.rs +++ b/src-tauri/src/component_manager.rs @@ -1,54 +1,43 @@ use crate::{AddedDecodersCount, DecoderPath}; -use wasmtime::*; +use wasmtime::component::{*, Component as WasmtimeComponent}; +use wasmtime::{Engine, Store}; +bindgen!(); + +struct LinkedState; + +impl component::decoder::host::Host for LinkedState { + fn log(&mut self, message: String) -> () { + println!("Decoder: {message}"); + } +} + +// 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) -> AddedDecodersCount { println!("decoders in Tauri: {decoder_paths:#?}"); + println!("{:#?}", std::env::current_dir()); - wasmtime_test().unwrap(); + if let Err(error) = add_decoder(&decoder_paths[0]) { + eprintln!("add_decoders error: {error:?}"); + } decoder_paths.len() } -fn wasmtime_test() -> wasmtime::Result<()> { +fn add_decoder(path: &str) -> 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 wasmtime_component = WasmtimeComponent::from_file(&engine, path)?; + 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()); - }, - )?; + Component::add_to_linker(&mut linker, |state: &mut LinkedState| state)?; - // 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 = Store::new(&engine, 4); + let mut store = Store::new(&engine, LinkedState); - // 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")?; + let (component, _instance) = Component::instantiate(&mut store, &wasmtime_component, &linker)?; - // And finally we can call the wasm! - hello.call(&mut store, ())?; + component.component_decoder_decoder().call_init(&mut store)?; Ok(()) } diff --git a/src-tauri/wit/world.wit b/src-tauri/wit/world.wit new file mode 100644 index 0000000..9205cff --- /dev/null +++ b/src-tauri/wit/world.wit @@ -0,0 +1,16 @@ +package component:decoder; + +interface host { + log: func(message: string); +} + +interface decoder { + init: func(); + name: func() -> string; + format-signal-value: func(value: string) -> string; +} + +world component { + import host; + export decoder; +} diff --git a/test_files/components/javascript_decoder/wit/world.wit b/test_files/components/javascript_decoder/wit/world.wit index 777841a..9205cff 100644 --- a/test_files/components/javascript_decoder/wit/world.wit +++ b/test_files/components/javascript_decoder/wit/world.wit @@ -1,4 +1,4 @@ -package component:javascript-decoder; +package component:decoder; interface host { log: func(message: string); diff --git a/test_files/components/python_decoder/README.md b/test_files/components/python_decoder/README.md index cbd48df..9c7fe5d 100644 --- a/test_files/components/python_decoder/README.md +++ b/test_files/components/python_decoder/README.md @@ -7,5 +7,4 @@ How to create and build the Python component: 5. Create the `src` folder with the file `app.py` 6. Create the `wit` folder with the file `world.wit` 7. Update code as needed -8. `componentize-py --wit-path wit/world.wit bindings src/bindings` -9. `componentize-py --wit-path wit/world.wit componentize src.app --output python_decoder.wasm` +8. `rm -rf src/bindings && componentize-py --wit-path wit/world.wit bindings src/bindings && componentize-py --wit-path wit/world.wit componentize src.app --output python_decoder.wasm` diff --git a/test_files/components/python_decoder/python_decoder.wasm b/test_files/components/python_decoder/python_decoder.wasm index 8c894a1..0acc62a 100644 Binary files a/test_files/components/python_decoder/python_decoder.wasm and b/test_files/components/python_decoder/python_decoder.wasm differ diff --git a/test_files/components/python_decoder/wit/world.wit b/test_files/components/python_decoder/wit/world.wit index 27b8204..9205cff 100644 --- a/test_files/components/python_decoder/wit/world.wit +++ b/test_files/components/python_decoder/wit/world.wit @@ -1,4 +1,4 @@ -package component:python-decoder; +package component:decoder; interface host { log: func(message: string); diff --git a/test_files/components/rust_decoder/README.md b/test_files/components/rust_decoder/README.md index dca09a8..1375faf 100644 --- a/test_files/components/rust_decoder/README.md +++ b/test_files/components/rust_decoder/README.md @@ -4,4 +4,4 @@ How to create and build the Rust component: 2. `cargo component new rust_decoder --lib` 3. `cd rust_decoder` 4. Update code as needed -5. `cargo +nightly component build --release --artifact-dir . -Z unstable-options` +5. `cargo component build --release --target wasm32-unknown-unknown && cp ../../../target/wasm32-unknown-unknown/release/rust_decoder.wasm .` diff --git a/test_files/components/rust_decoder/rust_decoder.wasm b/test_files/components/rust_decoder/rust_decoder.wasm index fdcfc80..4898d11 100644 Binary files a/test_files/components/rust_decoder/rust_decoder.wasm and b/test_files/components/rust_decoder/rust_decoder.wasm differ diff --git a/test_files/components/rust_decoder/src/bindings.rs b/test_files/components/rust_decoder/src/bindings.rs index 3de2b72..b8ac8a9 100644 --- a/test_files/components/rust_decoder/src/bindings.rs +++ b/test_files/components/rust_decoder/src/bindings.rs @@ -3,7 +3,7 @@ #[allow(dead_code)] pub mod component { #[allow(dead_code)] - pub mod rust_decoder { + pub mod decoder { #[allow(dead_code, clippy::all)] pub mod host { #[used] @@ -19,7 +19,7 @@ pub mod component { let len0 = vec0.len(); #[cfg(target_arch = "wasm32")] - #[link(wasm_import_module = "component:rust-decoder/host")] + #[link(wasm_import_module = "component:decoder/host")] extern "C" { #[link_name = "log"] fn wit_import(_: *mut u8, _: usize); @@ -40,7 +40,7 @@ pub mod exports { #[allow(dead_code)] pub mod component { #[allow(dead_code)] - pub mod rust_decoder { + pub mod decoder { #[allow(dead_code, clippy::all)] pub mod decoder { #[used] @@ -112,33 +112,33 @@ pub mod exports { } #[doc(hidden)] - macro_rules! __export_component_rust_decoder_decoder_cabi{ + macro_rules! __export_component_decoder_decoder_cabi{ ($ty:ident with_types_in $($path_to_types:tt)*) => (const _: () = { - #[export_name = "component:rust-decoder/decoder#init"] + #[export_name = "component:decoder/decoder#init"] unsafe extern "C" fn export_init() { $($path_to_types)*::_export_init_cabi::<$ty>() } - #[export_name = "component:rust-decoder/decoder#name"] + #[export_name = "component:decoder/decoder#name"] unsafe extern "C" fn export_name() -> *mut u8 { $($path_to_types)*::_export_name_cabi::<$ty>() } - #[export_name = "cabi_post_component:rust-decoder/decoder#name"] + #[export_name = "cabi_post_component:decoder/decoder#name"] unsafe extern "C" fn _post_return_name(arg0: *mut u8,) { $($path_to_types)*::__post_return_name::<$ty>(arg0) } - #[export_name = "component:rust-decoder/decoder#format-signal-value"] + #[export_name = "component:decoder/decoder#format-signal-value"] unsafe extern "C" fn export_format_signal_value(arg0: *mut u8,arg1: usize,) -> *mut u8 { $($path_to_types)*::_export_format_signal_value_cabi::<$ty>(arg0, arg1) } - #[export_name = "cabi_post_component:rust-decoder/decoder#format-signal-value"] + #[export_name = "cabi_post_component:decoder/decoder#format-signal-value"] unsafe extern "C" fn _post_return_format_signal_value(arg0: *mut u8,) { $($path_to_types)*::__post_return_format_signal_value::<$ty>(arg0) } };); } #[doc(hidden)] - pub(crate) use __export_component_rust_decoder_decoder_cabi; + pub(crate) use __export_component_decoder_decoder_cabi; #[repr(align(4))] struct _RetArea([::core::mem::MaybeUninit; 8]); static mut _RET_AREA: _RetArea = _RetArea([::core::mem::MaybeUninit::uninit(); 8]); @@ -194,7 +194,7 @@ mod _rt { macro_rules! __export_component_impl { ($ty:ident) => (self::export!($ty with_types_in self);); ($ty:ident with_types_in $($path_to_types_root:tt)*) => ( - $($path_to_types_root)*::exports::component::rust_decoder::decoder::__export_component_rust_decoder_decoder_cabi!($ty with_types_in $($path_to_types_root)*::exports::component::rust_decoder::decoder); + $($path_to_types_root)*::exports::component::decoder::decoder::__export_component_decoder_decoder_cabi!($ty with_types_in $($path_to_types_root)*::exports::component::decoder::decoder); ) } #[doc(inline)] @@ -203,14 +203,14 @@ pub(crate) use __export_component_impl as export; #[cfg(target_arch = "wasm32")] #[link_section = "component-type:wit-bindgen:0.25.0:component:encoded world"] #[doc(hidden)] -pub static __WIT_BINDGEN_COMPONENT_TYPE: [u8; 330] = *b"\ -\0asm\x0d\0\x01\0\0\x19\x16wit-component-encoding\x04\0\x07\xca\x01\x01A\x02\x01\ -A\x04\x01B\x02\x01@\x01\x07messages\x01\0\x04\0\x03log\x01\0\x03\x01\x1bcomponen\ -t:rust-decoder/host\x05\0\x01B\x06\x01@\0\x01\0\x04\0\x04init\x01\0\x01@\0\0s\x04\ -\0\x04name\x01\x01\x01@\x01\x05values\0s\x04\0\x13format-signal-value\x01\x02\x04\ -\x01\x1ecomponent:rust-decoder/decoder\x05\x01\x04\x01\x20component:rust-decoder\ -/component\x04\0\x0b\x0f\x01\0\x09component\x03\0\0\0G\x09producers\x01\x0cproce\ -ssed-by\x02\x0dwit-component\x070.208.1\x10wit-bindgen-rust\x060.25.0"; +pub static __WIT_BINDGEN_COMPONENT_TYPE: [u8; 315] = *b"\ +\0asm\x0d\0\x01\0\0\x19\x16wit-component-encoding\x04\0\x07\xbb\x01\x01A\x02\x01\ +A\x04\x01B\x02\x01@\x01\x07messages\x01\0\x04\0\x03log\x01\0\x03\x01\x16componen\ +t:decoder/host\x05\0\x01B\x06\x01@\0\x01\0\x04\0\x04init\x01\0\x01@\0\0s\x04\0\x04\ +name\x01\x01\x01@\x01\x05values\0s\x04\0\x13format-signal-value\x01\x02\x04\x01\x19\ +component:decoder/decoder\x05\x01\x04\x01\x1bcomponent:decoder/component\x04\0\x0b\ +\x0f\x01\0\x09component\x03\0\0\0G\x09producers\x01\x0cprocessed-by\x02\x0dwit-c\ +omponent\x070.208.1\x10wit-bindgen-rust\x060.25.0"; #[inline(never)] #[doc(hidden)] diff --git a/test_files/components/rust_decoder/src/lib.rs b/test_files/components/rust_decoder/src/lib.rs index 16a5882..103c773 100644 --- a/test_files/components/rust_decoder/src/lib.rs +++ b/test_files/components/rust_decoder/src/lib.rs @@ -1,8 +1,8 @@ #[allow(warnings)] mod bindings; -use bindings::component::rust_decoder::host; -use bindings::exports::component::rust_decoder::decoder; +use bindings::component::decoder::host; +use bindings::exports::component::decoder::decoder; macro_rules! log { ($($arg:tt)*) => (host::log(&format!($($arg)*))) diff --git a/test_files/components/rust_decoder/wit/world.wit b/test_files/components/rust_decoder/wit/world.wit index 7beda12..9205cff 100644 --- a/test_files/components/rust_decoder/wit/world.wit +++ b/test_files/components/rust_decoder/wit/world.wit @@ -1,4 +1,4 @@ -package component:rust-decoder; +package component:decoder; interface host { log: func(message: string);