component_manager, strict_eval.js

This commit is contained in:
Martin Kavík 2024-06-25 18:29:33 +02:00
parent ae4c76ed41
commit 3778469de9
11 changed files with 119 additions and 14 deletions

View file

@ -241,8 +241,10 @@ impl HeaderPanel {
if raw_event.shift_key() {
// @TODO move `prevent_default` to MZ API (next to the `pass_to_parent` method?)
raw_event.prevent_default();
let result = script_bridge::strict_eval(&script.lock_ref());
Task::start(clone!((script, command_result) async move {
let result = script_bridge::strict_eval(&script.lock_ref()).await;
command_result.set(Some(result));
}));
}
}
})

View file

@ -15,6 +15,8 @@ use browser as platform;
type Filename = String;
type JavascriptCode = String;
type AddedDecodersCount = usize;
type DecoderPath = String;
pub async fn show_window() {
platform::show_window().await
@ -58,3 +60,7 @@ pub async fn load_signal_and_get_timeline(
pub async fn unload_signal(signal_ref: wellen::SignalRef) {
platform::unload_signal(signal_ref).await
}
pub async fn add_decoders(decoder_paths: Vec<DecoderPath>) -> AddedDecodersCount {
platform::add_decoders(decoder_paths).await
}

View file

@ -1,7 +1,7 @@
use shared::wellen_helpers;
use std::sync::Mutex;
use wellen::simple::Waveform;
use zoon::*;
use zoon::{*, eprintln};
#[derive(Default)]
struct BrowserPlatformStore {
@ -120,3 +120,11 @@ pub(super) async fn unload_signal(signal_ref: wellen::SignalRef) {
let waveform = waveform_lock.as_mut().unwrap_throw();
waveform.unload_signals(&[signal_ref]);
}
pub(super) async fn add_decoders(
_decoder_paths: Vec<super::DecoderPath>,
) -> super::AddedDecodersCount {
// @TODO error message for user
eprintln!("Adding decoders is not supported in the browser.");
0
}

View file

@ -56,6 +56,13 @@ pub(super) async fn unload_signal(signal_ref: wellen::SignalRef) {
.unwrap_throw()
}
pub(super) async fn add_decoders(
decoder_paths: Vec<super::DecoderPath>,
) -> super::AddedDecodersCount {
serde_wasm_bindgen::from_value(tauri_glue::add_decoders(decoder_paths).await.unwrap_throw())
.unwrap_throw()
}
mod tauri_glue {
use zoon::*;
@ -86,5 +93,10 @@ mod tauri_glue {
#[wasm_bindgen(catch)]
pub async fn unload_signal(signal_ref_index: usize) -> Result<(), JsValue>;
#[wasm_bindgen(catch)]
pub async fn add_decoders(
decoder_paths: Vec<super::super::DecoderPath>,
) -> Result<JsValue, JsValue>;
}
}

View file

@ -1,16 +1,15 @@
use crate::STORE;
use crate::{platform, STORE};
use wellen::GetItem;
use zoon::*;
type FullVarName = String;
type AddedDecodersCount = usize;
type DecoderPath = String;
#[wasm_bindgen(
inline_js = r#"export function strict_eval(code) { "use strict"; return eval?.(`${code}`) }"#
)]
#[wasm_bindgen(module = "/typescript/bundles/strict_eval.js")]
extern "C" {
#[wasm_bindgen(catch)]
pub fn strict_eval(code: &str) -> Result<JsValue, JsValue>;
pub async fn strict_eval(code: &str) -> Result<JsValue, JsValue>;
}
#[wasm_bindgen]
@ -70,8 +69,7 @@ impl FW {
}
/// JS: `FW.add_decoders(["test_files/components/rust_decoder/rust_decoder.wasm"])` -> `1`
pub fn add_decoders(decoder_paths: Vec<DecoderPath>) -> usize {
zoon::println!("decoders: {decoder_paths:#?}");
0
pub async fn add_decoders(decoder_paths: Vec<DecoderPath>) -> AddedDecodersCount {
platform::add_decoders(decoder_paths).await
}
}

View file

@ -96,7 +96,7 @@ impl WaveformPanel {
if let Some(javascript_code) =
platform::load_file_with_selected_vars(None).await
{
match script_bridge::strict_eval(&javascript_code) {
match script_bridge::strict_eval(&javascript_code).await {
Ok(js_value) => {
zoon::println!("File with selected vars loaded: {js_value:?}")
}
@ -153,7 +153,7 @@ impl WaveformPanel {
if let Some(javascript_code) =
platform::load_file_with_selected_vars(Some(file)).await
{
match script_bridge::strict_eval(&javascript_code) {
match script_bridge::strict_eval(&javascript_code).await {
Ok(js_value) => zoon::println!(
"File with selected vars loaded: {js_value:?}"
),

View file

@ -0,0 +1,5 @@
"use strict";
export async function strict_eval(code) {
return await eval(code)
}

View file

@ -2536,7 +2536,11 @@ async function load_signal_and_get_timeline(signal_ref_index, timeline_zoom, tim
async function unload_signal(signal_ref_index) {
return await invoke2("unload_signal", { signal_ref_index });
}
async function add_decoders(decoder_paths) {
return await invoke2("add_decoders", { decoder_paths });
}
export {
add_decoders,
get_hierarchy,
load_file_with_selected_vars,
load_signal_and_get_timeline,

View file

@ -9,6 +9,8 @@ type JavascriptCode = string;
type WellenHierarchy = unknown;
type Timeline = unknown;
type VarFormat = unknown;
type AddedDecodersCount = number;
type DecoderPath = string;
export async function show_window(): Promise<void> {
return await invoke("show_window");
@ -47,3 +49,7 @@ export async function load_signal_and_get_timeline(
export async function unload_signal(signal_ref_index: number): Promise<void> {
return await invoke("unload_signal", { signal_ref_index });
}
export async function add_decoders(decoder_paths: Array<DecoderPath>): Promise<AddedDecodersCount> {
return await invoke("add_decoders", { decoder_paths });
}

View file

@ -0,0 +1,54 @@
use crate::{AddedDecodersCount, DecoderPath};
use wasmtime::*;
pub fn add_decoders(decoder_paths: Vec<DecoderPath>) -> AddedDecodersCount {
println!("decoders in Tauri: {decoder_paths:#?}");
wasmtime_test().unwrap();
decoder_paths.len()
}
fn wasmtime_test() -> 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 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());
},
)?;
// 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<u32> = Store::new(&engine, 4);
// 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")?;
// And finally we can call the wasm!
hello.call(&mut store, ())?;
Ok(())
}

View file

@ -5,6 +5,10 @@ use wellen::simple::Waveform;
type Filename = String;
type JavascriptCode = String;
type AddedDecodersCount = usize;
type DecoderPath = String;
mod component_manager;
#[derive(Default)]
struct Store {
@ -91,6 +95,11 @@ async fn unload_signal(signal_ref_index: usize, store: tauri::State<'_, Store>)
Ok(())
}
#[tauri::command(rename_all = "snake_case")]
async fn add_decoders(decoder_paths: Vec<DecoderPath>) -> Result<AddedDecodersCount, ()> {
Ok(component_manager::add_decoders(decoder_paths))
}
#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
// https://github.com/tauri-apps/tauri/issues/8462
@ -109,6 +118,7 @@ pub fn run() {
get_hierarchy,
load_signal_and_get_timeline,
unload_signal,
add_decoders,
])
.run(tauri::generate_context!())
.expect("error while running tauri application");