diagram plugins

This commit is contained in:
Martin Kavík 2024-11-25 22:13:39 +01:00
parent 1cdb558823
commit e8a0051ea7
29 changed files with 1781 additions and 267 deletions

7
Cargo.lock generated
View file

@ -4238,6 +4238,13 @@ dependencies = [
"wit-bindgen-rt",
]
[[package]]
name = "rust_diagram_connector"
version = "0.1.0"
dependencies = [
"wit-bindgen-rt",
]
[[package]]
name = "rustc-demangle"
version = "0.1.23"

View file

@ -3,7 +3,9 @@ members = [
"frontend",
"backend",
"shared",
"src-tauri", "test_files/components/rust_decoder",
"src-tauri",
"test_files/components/rust_decoder",
"test_files/components/rust_diagram_connector",
]
resolver = "2"

View file

@ -76,5 +76,15 @@ mod js_bridge {
#[wasm_bindgen(method)]
pub fn draw_diagram_element(this: &ExcalidrawController, excalidraw_element: JsValue);
#[wasm_bindgen(method)]
pub fn listen_for_component_text_changes(
this: &ExcalidrawController,
component_id: &str,
on_change: &Closure<dyn Fn(String)>,
);
#[wasm_bindgen(method)]
pub fn set_component_text(this: &ExcalidrawController, component_id: &str, text: &str);
}
}

View file

@ -1,4 +1,5 @@
use std::sync::Arc;
use shared::DiagramConnectorMessage;
use std::{mem, sync::Arc};
use zoon::*;
mod platform;
@ -53,11 +54,53 @@ static STORE: Lazy<Store> = lazy::default();
fn main() {
start_app("app", root);
Task::start(async {
// https://github.com/tauri-apps/tauri/issues/5170
Timer::sleep(100).await;
platform::show_window().await;
});
Task::start(async {
platform::listen_diagram_connectors_messages(|message| {
match message {
DiagramConnectorMessage::ListenForComponentTextChanges {
diagram_connector_name,
component_id,
} => {
let closure = Closure::new({
// @TODO Rcs/Arcs?
let diagram_connector_name = diagram_connector_name.clone();
let component_id = component_id.clone();
move |text| {
Task::start(platform::notify_diagram_connector_text_change(
diagram_connector_name.clone(),
component_id.clone(),
text,
));
}
});
STORE
.excalidraw_canvas_controller
.lock_ref()
.lock_ref()
.as_ref()
.unwrap_throw()
.listen_for_component_text_changes(&component_id, &closure);
// @TODO don't forget
mem::forget(closure);
}
DiagramConnectorMessage::SetComponentText { component_id, text } => STORE
.excalidraw_canvas_controller
.lock_ref()
.lock_ref()
.as_ref()
.unwrap_throw()
.set_component_text(&component_id, &text),
}
})
.await
});
}
fn root() -> impl Element {

View file

@ -4,6 +4,7 @@
// NOTE: `FASTWAVE_PLATFORM` is set in `Makefile.toml` tasks and then in `build.rs`
use crate::STORE;
use shared::DiagramConnectorMessage;
#[cfg(FASTWAVE_PLATFORM = "TAURI")]
mod tauri;
@ -17,10 +18,17 @@ use browser as platform;
type Filename = String;
type JavascriptCode = String;
type AddedDecodersCount = usize;
type RemovedDecodersCount = usize;
type DecoderPath = String;
type AddedDiagramConnectorsCount = usize;
type RemovedDiagramConnectorsCount = usize;
type DiagramConnectorPath = String;
type DiagramConnectorName = String;
type ComponentId = String;
pub async fn show_window() {
platform::show_window().await
}
@ -85,3 +93,29 @@ async fn redraw_all_timeline_rows() {
controller.redraw_all_rows().await
}
}
pub async fn add_diagram_connectors(
diagram_connector_paths: Vec<DecoderPath>,
) -> AddedDecodersCount {
let count = platform::add_diagram_connectors(diagram_connector_paths).await;
count
}
pub async fn remove_all_diagram_connectors() -> RemovedDecodersCount {
let count = platform::remove_all_diagram_connectors().await;
count
}
pub async fn listen_diagram_connectors_messages(
on_message: impl FnMut(DiagramConnectorMessage) + 'static,
) {
platform::listen_diagram_connectors_messages(on_message).await;
}
pub async fn notify_diagram_connector_text_change(
diagram_connector: DiagramConnectorName,
component_id: ComponentId,
text: String,
) {
platform::notify_diagram_connector_text_change(diagram_connector, component_id, text).await;
}

View file

@ -136,3 +136,33 @@ pub(super) async fn remove_all_decoders() -> super::RemovedDecodersCount {
eprintln!("Removing decoders is not supported in the browser.");
0
}
pub(super) async fn add_diagram_connectors(
diagram_connector_paths: Vec<super::DecoderPath>,
) -> super::AddedDecodersCount {
// @TODO error message for user
eprintln!("Adding diagram connectors is not supported in the browser.");
0
}
pub(super) async fn remove_all_diagram_connectors() -> super::RemovedDiagramConnectorsCount {
// @TODO error message for user
eprintln!("Removing diagram connectors is not supported in the browser.");
0
}
pub async fn listen_diagram_connectors_messages(
on_message: impl FnMut(DiagramConnectorMessage) + 'static,
) {
// @TODO error message for user
eprintln!("Removing listen for diagram connectors messages is not supported in the browser.");
}
pub async fn notify_diagram_connector_text_change(
diagram_connector: DiagramConnectorName,
component_id: ComponentId,
text: String,
) {
// @TODO error message for user
eprintln!("Diagram connectors notifications are not supported in the browser.");
}

View file

@ -1,3 +1,4 @@
use shared::DiagramConnectorMessage;
use zoon::*;
pub(super) async fn show_window() {
@ -68,6 +69,44 @@ pub(super) async fn remove_all_decoders() -> super::RemovedDecodersCount {
.unwrap_throw()
}
pub(super) async fn add_diagram_connectors(
diagram_connector_paths: Vec<super::DecoderPath>,
) -> super::AddedDiagramConnectorsCount {
serde_wasm_bindgen::from_value(
tauri_glue::add_diagram_connectors(diagram_connector_paths)
.await
.unwrap_throw(),
)
.unwrap_throw()
}
pub(super) async fn remove_all_diagram_connectors() -> super::RemovedDiagramConnectorsCount {
serde_wasm_bindgen::from_value(
tauri_glue::remove_all_diagram_connectors()
.await
.unwrap_throw(),
)
.unwrap_throw()
}
pub(super) async fn listen_diagram_connectors_messages(
mut on_message: impl FnMut(DiagramConnectorMessage) + 'static,
) {
let on_message =
move |message: JsValue| on_message(serde_wasm_bindgen::from_value(message).unwrap_throw());
tauri_glue::listen_diagram_connectors_messages(Closure::new(on_message).into_js_value()).await
}
pub(super) async fn notify_diagram_connector_text_change(
diagram_connector: super::DiagramConnectorName,
component_id: super::ComponentId,
text: String,
) {
tauri_glue::notify_diagram_connector_text_change(diagram_connector, component_id, text)
.await
.unwrap_throw();
}
mod tauri_glue {
use zoon::*;
@ -106,5 +145,22 @@ mod tauri_glue {
#[wasm_bindgen(catch)]
pub async fn remove_all_decoders() -> Result<JsValue, JsValue>;
#[wasm_bindgen(catch)]
pub async fn add_diagram_connectors(
diagram_connector_paths: Vec<super::super::DiagramConnectorPath>,
) -> Result<JsValue, JsValue>;
#[wasm_bindgen(catch)]
pub async fn remove_all_diagram_connectors() -> Result<JsValue, JsValue>;
pub async fn listen_diagram_connectors_messages(on_event: JsValue);
#[wasm_bindgen(catch)]
pub async fn notify_diagram_connector_text_change(
diagram_connector: super::super::DiagramConnectorName,
component_id: super::super::ComponentId,
text: String,
) -> Result<(), JsValue>;
}
}

View file

@ -3,10 +3,15 @@ use wellen::GetItem;
use zoon::*;
type FullVarName = String;
type AddedDecodersCount = usize;
type RemovedDecodersCount = usize;
type DecoderPath = String;
type AddedDiagramConnectorsCount = usize;
type RemovedDiagramConnectorsCount = usize;
type DiagramConnectorPath = String;
#[wasm_bindgen(module = "/typescript/bundles/strict_eval.js")]
extern "C" {
#[wasm_bindgen(catch)]
@ -91,4 +96,16 @@ impl FW {
controller.draw_diagram_element(excalidraw_element)
}
}
/// JS: `FW.add_diagram_connectors(["../test_files/components/rust_diagram_connector/rust_diagram_connector.wasm"])` -> `1`
pub async fn add_diagram_connectors(
connector_paths: Vec<DiagramConnectorPath>,
) -> AddedDiagramConnectorsCount {
platform::add_diagram_connectors(connector_paths).await
}
/// JS: `FW.remove_all_diagram_connectors()` -> `5`
pub async fn remove_all_diagram_connectors() -> RemovedDiagramConnectorsCount {
platform::remove_all_diagram_connectors().await
}
}

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

@ -7,17 +7,64 @@ import * as React from 'react'
import * as ReactDOM from 'react-dom/client'
export class ExcalidrawController {
api: ExcalidrawImperativeAPI | undefined
api: Promise<ExcalidrawImperativeAPI>
resolve_api: (api: ExcalidrawImperativeAPI) => void
constructor() {}
constructor() {
this.resolve_api = (api) => {};
this.api = new Promise(resolve => {
this.resolve_api = (api) => resolve(api)
});
}
draw_diagram_element(excalidraw_element: ExcalidrawElement) {
if (typeof this.api !== 'undefined') {
const elements = this.api.getSceneElements()
this.api.updateScene({
this.api.then(api => {
const elements = api.getSceneElements()
api.updateScene({
elements: elements.concat(excalidraw_element)
})
}
});
}
listen_for_component_text_changes(id: string, on_change: (text: string) => void) {
this.api.then(api => {
let old_text: string | null = null;
api.onChange((elements: readonly ExcalidrawElement[]) => {
const element = elements.find(element => element.id === id);
if (typeof element !== 'undefined') {
if (element.type === 'text') {
if (old_text === null) {
old_text = element.text;
on_change(old_text);
} else {
if (old_text !== element.text) {
old_text = element.text;
on_change(old_text);
}
}
}
}
})
})
}
set_component_text(id: string, text: string) {
this.api.then(api => {
let element_found = false;
const elements = api.getSceneElements().map(element => {
if (element.id === id) {
element_found = true;
return { ...element, text: text, originalText: text }
} else {
return element
}
});
if (element_found) {
api.updateScene({
elements
})
}
})
}
async init(parent_element: HTMLElement) {
@ -37,7 +84,7 @@ export class ExcalidrawController {
// Font family: Code
currentItemFontFamily: 3,
}}}
excalidrawAPI={(api) => this.api = api}
excalidrawAPI={(api) => this.resolve_api(api)}
>
<MainMenu>
<MainMenu.DefaultItems.LoadScene />

View file

@ -1,18 +1,26 @@
// @TODO use TS and Tauri bindgens to make this code properly typed
import { core } from '@tauri-apps/api'
import { core, event } from '@tauri-apps/api'
const invoke = core.invoke;
const listen = event.listen;
type Filename = string;
type JavascriptCode = string;
type WellenHierarchy = unknown;
type Timeline = unknown;
type VarFormat = unknown;
type AddedDecodersCount = number;
type RemovedDecodersCount = number;
type DecoderPath = string;
type AddedDiagramConnectorsCount = number;
type RemovedDiagramConnectorsCount = number;
type DiagramConnectorPath = string;
type DiagramConnectorName = string;
type ComponentId = string;
export async function show_window(): Promise<void> {
return await invoke("show_window");
}
@ -58,3 +66,19 @@ export async function add_decoders(decoder_paths: Array<DecoderPath>): Promise<A
export async function remove_all_decoders(): Promise<RemovedDecodersCount> {
return await invoke("remove_all_decoders");
}
export async function add_diagram_connectors(diagram_connector_paths: Array<DiagramConnectorPath>): Promise<AddedDiagramConnectorsCount> {
return await invoke("add_diagram_connectors", { diagram_connector_paths });
}
export async function remove_all_diagram_connectors(): Promise<RemovedDiagramConnectorsCount> {
return await invoke("remove_all_diagram_connectors");
}
export async function listen_diagram_connectors_messages(on_message: (message: any) => void) {
return await listen("diagram_connector_message", (message) => on_message(message.payload));
}
export async function notify_diagram_connector_text_change(diagram_connector: DiagramConnectorName, component_id: ComponentId, text: string): Promise<void> {
return await invoke("notify_diagram_connector_text_change", { diagram_connector, component_id, text });
}

View file

@ -30,3 +30,16 @@ pub struct TimeLineBlockLabel {
pub x: u32,
pub y: u32,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(crate = "serde")]
pub enum DiagramConnectorMessage {
ListenForComponentTextChanges {
diagram_connector_name: String,
component_id: String,
},
SetComponentText {
component_id: String,
text: String,
},
}

View file

@ -1,125 +1,2 @@
use crate::{AddedDecodersCount, DecoderPath, RemovedDecodersCount};
use once_cell::sync::Lazy;
use std::sync::Arc;
use tauri::async_runtime::{Mutex, RwLock};
use wasmtime::component::{Component as WasmtimeComponent, *};
use wasmtime::{AsContextMut, Engine, Store};
use wasmtime_wasi::{WasiCtx, WasiView};
bindgen!();
pub static DECODERS: Lazy<Arc<RwLock<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
});
pub 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))
});
pub struct State {
ctx: WasiCtx,
table: ResourceTable,
}
impl WasiView for State {
fn ctx(&mut self) -> &mut WasiCtx {
&mut self.ctx
}
fn table(&mut self) -> &mut ResourceTable {
&mut self.table
}
}
impl component::decoder::host::Host for State {
fn log(&mut self, message: String) {
println!("Decoder: {message}");
}
}
pub async fn remove_all_decoders() -> RemovedDecodersCount {
let mut decoders = DECODERS.write().await;
let decoders_count = decoders.len();
decoders.clear();
decoders_count
}
// @TODO Make println work on Windows in release mode?
// https://github.com/tauri-apps/tauri/discussions/8626
// @TODO Remove / improve comments below
// Testing
//
// Rust
// FW.add_decoders(["../test_files/components/rust_decoder/rust_decoder.wasm"])
// FW.add_decoders(["../test_files/components/rust_decoder/rust_decoder.wasm", "../test_files/components/rust_decoder/rust_decoder.wasm"])
//
// JS
// FW.add_decoders(["../test_files/components/javascript_decoder/javascript_decoder.wasm"])
//
// Python
// FW.add_decoders(["../test_files/components/python_decoder/python_decoder.wasm"])
//
// 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"])
pub async fn add_decoders(decoder_paths: Vec<DecoderPath>) -> AddedDecodersCount {
println!("Decoders: {decoder_paths:#?}");
println!("Current dir: {:#?}", std::env::current_dir().unwrap());
let mut added_decoders_count = 0;
// @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 {
for decoder_path in decoder_paths {
if let Err(error) = add_decoder(&decoder_path).await {
eprintln!("add_decoders error: {error:?}");
} else {
added_decoders_count += 1;
}
}
// })
// }).join().unwrap();
added_decoders_count
}
async fn add_decoder(path: &str) -> wasmtime::Result<()> {
let wasmtime_component = WasmtimeComponent::from_file(&ENGINE, path)?;
let mut store_lock = STORE.lock().await;
let mut store = store_lock.as_context_mut();
let component = Component::instantiate(&mut store, &wasmtime_component, &LINKER)?;
println!(
"Decoder name: {}",
component
.component_decoder_decoder()
.call_name(&mut store)?
);
component
.component_decoder_decoder()
.call_init(&mut store)?;
DECODERS.write().await.push(component);
Ok(())
}
pub mod decoders;
pub mod diagram_connectors;

View file

@ -0,0 +1,125 @@
use crate::{AddedDecodersCount, DecoderPath, RemovedDecodersCount};
use once_cell::sync::Lazy;
use std::sync::Arc;
use tauri::async_runtime::{Mutex, RwLock};
use wasmtime::component::{Component as WasmtimeComponent, *};
use wasmtime::{AsContextMut, Engine, Store};
use wasmtime_wasi::{WasiCtx, WasiView};
bindgen!(in "wit/decoder");
pub static DECODERS: Lazy<Arc<RwLock<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
});
pub 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))
});
pub struct State {
ctx: WasiCtx,
table: ResourceTable,
}
impl WasiView for State {
fn ctx(&mut self) -> &mut WasiCtx {
&mut self.ctx
}
fn table(&mut self) -> &mut ResourceTable {
&mut self.table
}
}
impl component::decoder::host::Host for State {
fn log(&mut self, message: String) {
println!("Decoder: {message}");
}
}
pub async fn remove_all_decoders() -> RemovedDecodersCount {
let mut decoders = DECODERS.write().await;
let decoders_count = decoders.len();
decoders.clear();
decoders_count
}
// @TODO Make println work on Windows in release mode?
// https://github.com/tauri-apps/tauri/discussions/8626
// @TODO Remove / improve comments below
// Testing
//
// Rust
// FW.add_decoders(["../test_files/components/rust_decoder/rust_decoder.wasm"])
// FW.add_decoders(["../test_files/components/rust_decoder/rust_decoder.wasm", "../test_files/components/rust_decoder/rust_decoder.wasm"])
//
// JS
// FW.add_decoders(["../test_files/components/javascript_decoder/javascript_decoder.wasm"])
//
// Python
// FW.add_decoders(["../test_files/components/python_decoder/python_decoder.wasm"])
//
// 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"])
pub async fn add_decoders(decoder_paths: Vec<DecoderPath>) -> AddedDecodersCount {
println!("Decoders: {decoder_paths:#?}");
println!("Current dir: {:#?}", std::env::current_dir().unwrap());
let mut added_decoders_count = 0;
// @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 {
for decoder_path in decoder_paths {
if let Err(error) = add_decoder(&decoder_path).await {
eprintln!("add_decoders error: {error:?}");
} else {
added_decoders_count += 1;
}
}
// })
// }).join().unwrap();
added_decoders_count
}
async fn add_decoder(path: &str) -> wasmtime::Result<()> {
let wasmtime_component = WasmtimeComponent::from_file(&ENGINE, path)?;
let mut store_lock = STORE.lock().await;
let mut store = store_lock.as_context_mut();
let component = Component::instantiate(&mut store, &wasmtime_component, &LINKER)?;
println!(
"Decoder name: {}",
component
.component_decoder_decoder()
.call_name(&mut store)?
);
component
.component_decoder_decoder()
.call_init(&mut store)?;
DECODERS.write().await.push(component);
Ok(())
}

View file

@ -0,0 +1,273 @@
use crate::{
AddedDiagramConnectorsCount, ComponentId, DiagramConnectorName, DiagramConnectorPath,
RemovedDiagramConnectorsCount, APP_HANDLE, WAVEFORM,
};
use once_cell::sync::Lazy;
use shared::{DiagramConnectorMessage, VarFormat};
use std::sync::Arc;
use tauri::async_runtime::{Mutex, RwLock};
use tauri::Manager;
use wasmtime::component::{Component as WasmtimeComponent, *};
use wasmtime::{AsContextMut, Engine, Store};
use wasmtime_wasi::{WasiCtx, WasiView};
use wellen::GetItem;
bindgen!(in "wit/diagram_connector");
pub static DIAGRAM_CONNECTORS: Lazy<Arc<RwLock<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
});
pub 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))
});
pub struct State {
ctx: WasiCtx,
table: ResourceTable,
}
impl WasiView for State {
fn ctx(&mut self) -> &mut WasiCtx {
&mut self.ctx
}
fn table(&mut self) -> &mut ResourceTable {
&mut self.table
}
}
impl component::diagram_connector::host::Host for State {
fn log(&mut self, message: String) {
println!("Diagram Connector: {message}");
}
fn listen_for_component_text_changes(
&mut self,
diagram_connector_name: String,
component_id: String,
) {
let message = DiagramConnectorMessage::ListenForComponentTextChanges {
diagram_connector_name,
component_id,
};
APP_HANDLE
.read()
.unwrap()
.as_ref()
.unwrap()
.emit("diagram_connector_message", message)
.unwrap();
}
fn set_component_text(&mut self, component_id: String, text: String) {
let message = DiagramConnectorMessage::SetComponentText { component_id, text };
APP_HANDLE
.read()
.unwrap()
.as_ref()
.unwrap()
.emit("diagram_connector_message", message)
.unwrap();
}
// @TODO `resource` in WIT or async in the future
// @TODO move business logic to the diagram connector
fn address_and_way(&mut self, time_text: String) -> Result<(String, Option<u32>), ()> {
let input_time = time_text.parse::<u64>().map_err(|error| {
eprintln!("Failed to parse time_text '{time_text}', error: {error:#}");
})?;
let waveform_wrapper = WAVEFORM.read().unwrap();
let mut maybe_waveform = waveform_wrapper.try_write().unwrap();
let waveform = maybe_waveform.as_mut().unwrap();
let hierarchy = waveform.hierarchy();
// @TODO remove
// let timescale = hierarchy.timescale().unwrap();
// println!("Timescale: {timescale:#?}");
let refill_valid_ref = hierarchy
.lookup_var(
&["TOP", "VexiiRiscv"],
&"FetchL1Plugin_logic_refill_start_valid",
)
.unwrap();
let refill_address_ref = hierarchy
.lookup_var(
&["TOP", "VexiiRiscv"],
&"FetchL1Plugin_logic_refill_start_address",
)
.unwrap();
let refill_way_ref = hierarchy
.lookup_var(
&["TOP", "VexiiRiscv"],
&"FetchL1Plugin_logic_refill_start_wayToAllocate",
)
.unwrap();
let refill_valid_var = hierarchy.get(refill_valid_ref);
let refill_address_var = hierarchy.get(refill_address_ref);
let refill_way_var = hierarchy.get(refill_way_ref);
let refill_valid_signal_ref = refill_valid_var.signal_ref();
let refill_address_signal_ref = refill_address_var.signal_ref();
let refill_way_signal_ref = refill_way_var.signal_ref();
let mut time_table_idx = None;
for (idx, time) in waveform.time_table().iter().enumerate() {
if *time >= input_time {
time_table_idx = Some(idx as u32);
break;
}
}
let Some(time_table_idx) = time_table_idx else {
eprintln!("time_table_idx is None");
Err(())?
};
waveform.load_signals_multi_threaded(&[
refill_valid_signal_ref,
refill_address_signal_ref,
refill_way_signal_ref,
]);
let refill_valid_signal = waveform.get_signal(refill_valid_signal_ref).unwrap();
let refill_valid_offset = refill_valid_signal.get_offset(time_table_idx).unwrap();
let refill_valid_value = refill_valid_signal
.get_value_at(&refill_valid_offset, 0)
.to_string();
let refill_address_signal = waveform.get_signal(refill_address_signal_ref).unwrap();
let refill_address_offset = refill_address_signal.get_offset(time_table_idx).unwrap();
let refill_address_value = refill_address_signal.get_value_at(&refill_address_offset, 0);
let refill_address_value = VarFormat::Hexadecimal.format(refill_address_value);
if refill_valid_value == "0" {
return Ok((refill_address_value, None));
}
let refill_way_signal = waveform.get_signal(refill_way_signal_ref).unwrap();
let refill_way_offset = refill_way_signal.get_offset(time_table_idx).unwrap();
let refill_way_value = refill_way_signal.get_value_at(&refill_way_offset, 0);
let refill_way_value = VarFormat::Unsigned.format(refill_way_value);
Ok((
refill_address_value,
Some(refill_way_value.parse().unwrap()),
))
}
}
pub async fn remove_all_diagram_connectors() -> RemovedDiagramConnectorsCount {
let mut diagram_connectors = DIAGRAM_CONNECTORS.write().await;
let diagram_connectors_count = diagram_connectors.len();
diagram_connectors.clear();
diagram_connectors_count
}
// @TODO Make println work on Windows in release mode?
// https://github.com/tauri-apps/tauri/discussions/8626
// @TODO Remove / improve comments below
// Testing
//
// Rust
// FW.add_diagram_connectors(["../test_files/components/rust_diagram_connector/rust_diagram_connector.wasm"])
//
// Remove all
// FW.remove_all_diagram_connectors()
//
// All Debug
// FW.add_diagram_connectors(["../test_files/components/rust_diagram_connector/rust_diagram_connector.wasm"])
//
// All Release
// FW.add_diagram_connectors(["../../test_files/components/rust_diagram_connector/rust_diagram_connector.wasm"])
pub async fn add_diagram_connectors(
diagram_connector_paths: Vec<DiagramConnectorPath>,
) -> AddedDiagramConnectorsCount {
println!("Diagram Connectors: {diagram_connector_paths:#?}");
println!("Current dir: {:#?}", std::env::current_dir().unwrap());
let mut added_diagram_connectors_count = 0;
// @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 {
for diagram_connector_path in diagram_connector_paths {
if let Err(error) = add_diagram_connector(&diagram_connector_path).await {
eprintln!("add_diagram_connectors error: {error:?}");
} else {
added_diagram_connectors_count += 1;
}
}
// })
// }).join().unwrap();
added_diagram_connectors_count
}
async fn add_diagram_connector(path: &str) -> wasmtime::Result<()> {
let wasmtime_component = WasmtimeComponent::from_file(&ENGINE, path)?;
let mut store_lock = STORE.lock().await;
let mut store = store_lock.as_context_mut();
let component = Component::instantiate(&mut store, &wasmtime_component, &LINKER)?;
println!(
"Diagram Connector name: {}",
component
.component_diagram_connector_diagram_connector()
.call_name(&mut store)?
);
component
.component_diagram_connector_diagram_connector()
.call_init(&mut store)?;
DIAGRAM_CONNECTORS.write().await.push(component);
Ok(())
}
// @TODO rename `ComponentId` everywhere to something like `DiagramElementId`?
// @TODO get rid of unwraps
pub async fn notify_diagram_connector_text_change(
diagram_connector: DiagramConnectorName,
component_id: ComponentId,
text: String,
) {
let mut store_lock = STORE.lock().await;
let mut store = store_lock.as_context_mut();
let diagram_connectors = DIAGRAM_CONNECTORS.read().await;
// @TODO store diagram_collectors in a hashmap/btreemap?
let diagram_connector = diagram_connectors
.iter()
.find(|diagram_collector| {
let name = diagram_collector
.component_diagram_connector_diagram_connector()
.call_name(&mut store)
.unwrap();
name == diagram_connector
})
.unwrap();
diagram_connector
.component_diagram_connector_diagram_connector()
.call_on_component_text_changed(&mut store, &component_id, &text)
.unwrap();
}

View file

@ -1,20 +1,32 @@
use once_cell::sync::Lazy;
use std::fs;
use tauri::async_runtime::RwLock;
use std::sync::{Arc, RwLock as StdRwLock};
use tauri::{async_runtime::RwLock, AppHandle};
use tauri_plugin_dialog::DialogExt;
use wasmtime::AsContextMut;
use wellen::simple::Waveform;
type Filename = String;
type JavascriptCode = String;
type AddedDecodersCount = usize;
type RemovedDecodersCount = usize;
type DecoderPath = String;
type AddedDiagramConnectorsCount = usize;
type RemovedDiagramConnectorsCount = usize;
type DiagramConnectorPath = String;
type DiagramConnectorName = String;
type ComponentId = String;
mod component_manager;
pub static APP_HANDLE: Lazy<Arc<StdRwLock<Option<AppHandle>>>> = Lazy::new(<_>::default);
pub static WAVEFORM: Lazy<StdRwLock<Arc<RwLock<Option<Waveform>>>>> = Lazy::new(<_>::default);
#[derive(Default)]
struct Store {
waveform: RwLock<Option<Waveform>>,
waveform: Arc<RwLock<Option<Waveform>>>,
}
#[tauri::command(rename_all = "snake_case")]
@ -37,6 +49,7 @@ async fn pick_and_load_waveform(
panic!("Waveform file reading failed")
};
*store.waveform.write().await = Some(waveform);
*WAVEFORM.write().unwrap() = Arc::clone(&store.waveform);
Ok(Some(file_response.name.unwrap()))
}
@ -92,8 +105,8 @@ async fn load_signal_and_get_timeline(
// @TODO Workaround? Is it a problem only for non-Rust components? Is it needed only when there is a problem in the component (e.g. "`Err` value: wasm trap: cannot enter component instance"?)
// let value = std::thread::spawn(move || {
// futures::executor::block_on(async move {
let decoders = component_manager::DECODERS.read().await;
let mut store_lock = component_manager::STORE.lock().await;
let decoders = component_manager::decoders::DECODERS.read().await;
let mut store_lock = component_manager::decoders::STORE.lock().await;
let mut store = store_lock.as_context_mut();
for decoder in decoders.iter() {
@ -127,12 +140,43 @@ async fn unload_signal(signal_ref_index: usize, store: tauri::State<'_, Store>)
#[tauri::command(rename_all = "snake_case")]
async fn add_decoders(decoder_paths: Vec<DecoderPath>) -> Result<AddedDecodersCount, ()> {
Ok(component_manager::add_decoders(decoder_paths).await)
Ok(component_manager::decoders::add_decoders(decoder_paths).await)
}
#[tauri::command(rename_all = "snake_case")]
async fn remove_all_decoders() -> Result<RemovedDecodersCount, ()> {
Ok(component_manager::remove_all_decoders().await)
Ok(component_manager::decoders::remove_all_decoders().await)
}
#[tauri::command(rename_all = "snake_case")]
async fn add_diagram_connectors(
diagram_connector_paths: Vec<DiagramConnectorPath>,
) -> Result<AddedDiagramConnectorsCount, ()> {
Ok(
component_manager::diagram_connectors::add_diagram_connectors(diagram_connector_paths)
.await,
)
}
#[tauri::command(rename_all = "snake_case")]
async fn remove_all_diagram_connectors() -> Result<RemovedDiagramConnectorsCount, ()> {
Ok(component_manager::diagram_connectors::remove_all_diagram_connectors().await)
}
#[tauri::command(rename_all = "snake_case")]
async fn notify_diagram_connector_text_change(
diagram_connector: DiagramConnectorName,
component_id: ComponentId,
text: String,
) -> Result<(), ()> {
Ok(
component_manager::diagram_connectors::notify_diagram_connector_text_change(
diagram_connector,
component_id,
text,
)
.await,
)
}
#[cfg_attr(mobile, tauri::mobile_entry_point)]
@ -145,7 +189,7 @@ pub fn run() {
.manage(Store::default())
.plugin(tauri_plugin_window_state::Builder::default().build())
.plugin(tauri_plugin_dialog::init())
// Npte: Add all handlers to `frontend/src/tauri_bridge.rs`
// Note: Add all handlers to `frontend/src/tauri_bridge.rs`
.invoke_handler(tauri::generate_handler![
show_window,
pick_and_load_waveform,
@ -155,7 +199,14 @@ pub fn run() {
unload_signal,
add_decoders,
remove_all_decoders,
add_diagram_connectors,
remove_all_diagram_connectors,
notify_diagram_connector_text_change,
])
.setup(|app| {
*APP_HANDLE.write().unwrap() = Some(app.handle().to_owned());
Ok(())
})
.run(tauri::generate_context!())
.expect("error while running tauri application");
}

View file

@ -0,0 +1,19 @@
package component:diagram-connector;
interface host {
log: func(message: string);
listen-for-component-text-changes: func(diagram-connect-name: string, component-id: string);
set-component-text: func(component-id: string, text: string);
address-and-way: func(time-text: string) -> result<tuple<string, option<u32>>>;
}
interface diagram-connector {
init: func();
name: func() -> string;
on-component-text-changed: func(component-id: string, text: string);
}
world component {
import host;
export diagram-connector;
}

1
test_files/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
sv39_mmu_cache_sim/

View file

@ -0,0 +1,431 @@
{
"type": "excalidraw",
"version": 2,
"source": "http://localhost:8080",
"elements": [
{
"type": "text",
"version": 174,
"versionNonce": 1552336377,
"isDeleted": false,
"id": "xPhIioz8O3Vjc7QcLfYes",
"fillStyle": "solid",
"strokeWidth": 2,
"strokeStyle": "solid",
"roughness": 0,
"opacity": 100,
"angle": 0,
"x": 60,
"y": 120,
"strokeColor": "#1e1e1e",
"backgroundColor": "transparent",
"width": 1153.125,
"height": 57.599999999999994,
"seed": 1317703821,
"groupIds": [],
"frameId": null,
"roundness": null,
"boundElements": [],
"updated": 1732565113996,
"link": null,
"locked": false,
"fontSize": 16,
"fontFamily": 3,
"text": "1. Extract 'test_files/sv39_mmu_cache_sim.zip' to 'test_files/sv39_mmu_cache_sim'\n2. Load file 'test_files/sv39_mmu_cache_sim/fst/wave.fst'\n3. Run command 'FW.add_diagram_connectors([\"../test_files/components/rust_diagram_connector/rust_diagram_connector.wasm\"])'",
"textAlign": "left",
"verticalAlign": "top",
"containerId": null,
"originalText": "1. Extract 'test_files/sv39_mmu_cache_sim.zip' to 'test_files/sv39_mmu_cache_sim'\n2. Load file 'test_files/sv39_mmu_cache_sim/fst/wave.fst'\n3. Run command 'FW.add_diagram_connectors([\"../test_files/components/rust_diagram_connector/rust_diagram_connector.wasm\"])'",
"lineHeight": 1.2,
"baseline": 54
},
{
"type": "rectangle",
"version": 133,
"versionNonce": 631769070,
"isDeleted": false,
"id": "WORIbkpEkf5c85sK8DhU-",
"fillStyle": "solid",
"strokeWidth": 4,
"strokeStyle": "solid",
"roughness": 0,
"opacity": 100,
"angle": 0,
"x": 460,
"y": 220,
"strokeColor": "#1e1e1e",
"backgroundColor": "#a5d8ff",
"width": 300,
"height": 440.00000000000006,
"seed": 1957721512,
"groupIds": [],
"frameId": null,
"roundness": {
"type": 3
},
"boundElements": [],
"updated": 1732565668461,
"link": null,
"locked": false
},
{
"type": "text",
"version": 90,
"versionNonce": 1920851711,
"isDeleted": false,
"id": "mBGA6gGm1aDhGnBNB8gPz",
"fillStyle": "solid",
"strokeWidth": 2,
"strokeStyle": "solid",
"roughness": 0,
"opacity": 100,
"angle": 0,
"x": 541.5560073852539,
"y": 260,
"strokeColor": "#1e1e1e",
"backgroundColor": "transparent",
"width": 144.50794982910156,
"height": 35,
"seed": 1040757672,
"groupIds": [],
"frameId": null,
"roundness": null,
"boundElements": [],
"updated": 1732566083274,
"link": null,
"locked": false,
"fontSize": 28,
"fontFamily": 1,
"text": "SV39 MMU",
"textAlign": "center",
"verticalAlign": "top",
"containerId": null,
"originalText": "SV39 MMU",
"lineHeight": 1.25,
"baseline": 24
},
{
"type": "text",
"version": 153,
"versionNonce": 302406585,
"isDeleted": false,
"id": "8eNVs3Eiiy6Ns1KqVK-zj",
"fillStyle": "solid",
"strokeWidth": 2,
"strokeStyle": "solid",
"roughness": 0,
"opacity": 100,
"angle": 0,
"x": 480,
"y": 440,
"strokeColor": "#1e1e1e",
"backgroundColor": "transparent",
"width": 73.369140625,
"height": 23,
"seed": 1170025128,
"groupIds": [],
"frameId": null,
"roundness": null,
"boundElements": [],
"updated": 1732565113996,
"link": null,
"locked": false,
"fontSize": 20,
"fontFamily": 2,
"text": "Address",
"textAlign": "left",
"verticalAlign": "top",
"containerId": null,
"originalText": "Address",
"lineHeight": 1.15,
"baseline": 18
},
{
"type": "rectangle",
"version": 116,
"versionNonce": 57845422,
"isDeleted": false,
"id": "IhK6hwJEwdKZ9bG8aTkN7",
"fillStyle": "solid",
"strokeWidth": 2,
"strokeStyle": "solid",
"roughness": 0,
"opacity": 100,
"angle": 0,
"x": 480,
"y": 473.5732012743411,
"strokeColor": "#1e1e1e",
"backgroundColor": "transparent",
"width": 260,
"height": 40,
"seed": 590079912,
"groupIds": [],
"frameId": null,
"roundness": {
"type": 3
},
"boundElements": [
{
"type": "text",
"id": "ITxhJ7NtZ74YFd9JQ0_pl"
}
],
"updated": 1732565668739,
"link": null,
"locked": false
},
{
"type": "text",
"version": 37,
"versionNonce": 761868063,
"isDeleted": false,
"id": "ITxhJ7NtZ74YFd9JQ0_pl",
"fillStyle": "solid",
"strokeWidth": 2,
"strokeStyle": "solid",
"roughness": 0,
"opacity": 100,
"angle": 0,
"x": 486.953125,
"y": 481.5732012743411,
"strokeColor": "#1e1e1e",
"backgroundColor": "transparent",
"width": 246.09375,
"height": 24,
"seed": 684450162,
"groupIds": [],
"frameId": null,
"roundness": null,
"boundElements": [],
"updated": 1732566156524,
"link": null,
"locked": false,
"fontSize": 20,
"fontFamily": 3,
"text": " UNKNOWN ",
"textAlign": "center",
"verticalAlign": "middle",
"containerId": "IhK6hwJEwdKZ9bG8aTkN7",
"originalText": " UNKNOWN ",
"lineHeight": 1.2,
"baseline": 19
},
{
"type": "text",
"version": 301,
"versionNonce": 873757849,
"isDeleted": false,
"id": "Lcp_kf4ZNFv0MtP5S6q91",
"fillStyle": "solid",
"strokeWidth": 2,
"strokeStyle": "solid",
"roughness": 0,
"opacity": 100,
"angle": 0,
"x": 480,
"y": 554.9486030561815,
"strokeColor": "#1e1e1e",
"backgroundColor": "transparent",
"width": 56.69921875,
"height": 23,
"seed": 1685601960,
"groupIds": [],
"frameId": null,
"roundness": null,
"boundElements": [],
"updated": 1732565113996,
"link": null,
"locked": false,
"fontSize": 20,
"fontFamily": 2,
"text": "Status",
"textAlign": "left",
"verticalAlign": "top",
"containerId": null,
"originalText": "Status",
"lineHeight": 1.15,
"baseline": 18
},
{
"type": "text",
"version": 148,
"versionNonce": 586282359,
"isDeleted": false,
"id": "cHL582ZJrMvSgzJTCSVQg",
"fillStyle": "solid",
"strokeWidth": 2,
"strokeStyle": "solid",
"roughness": 0,
"opacity": 100,
"angle": 0,
"x": 480,
"y": 320,
"strokeColor": "#1e1e1e",
"backgroundColor": "transparent",
"width": 83.701171875,
"height": 23,
"seed": 1428509096,
"groupIds": [],
"frameId": null,
"roundness": null,
"boundElements": [],
"updated": 1732565113996,
"link": null,
"locked": false,
"fontSize": 20,
"fontFamily": 2,
"text": "Time (ps)",
"textAlign": "left",
"verticalAlign": "top",
"containerId": null,
"originalText": "Time (ps)",
"lineHeight": 1.15,
"baseline": 18
},
{
"type": "rectangle",
"version": 160,
"versionNonce": 896237945,
"isDeleted": false,
"id": "KsSzJicTZlMzyYamex99O",
"fillStyle": "solid",
"strokeWidth": 2,
"strokeStyle": "solid",
"roughness": 0,
"opacity": 100,
"angle": 0,
"x": 480,
"y": 352,
"strokeColor": "#1e1e1e",
"backgroundColor": "#ffffff",
"width": 260,
"height": 40,
"seed": 825642664,
"groupIds": [],
"frameId": null,
"roundness": {
"type": 3
},
"boundElements": [
{
"type": "text",
"id": "afXu8_6Kqfq-q2IsjtAcP"
}
],
"updated": 1732565113996,
"link": null,
"locked": false
},
{
"type": "text",
"version": 170,
"versionNonce": 1243754135,
"isDeleted": false,
"id": "afXu8_6Kqfq-q2IsjtAcP",
"fillStyle": "solid",
"strokeWidth": 2,
"strokeStyle": "solid",
"roughness": 0,
"opacity": 100,
"angle": 0,
"x": 485,
"y": 360,
"strokeColor": "#1e1e1e",
"backgroundColor": "transparent",
"width": 46.875,
"height": 24,
"seed": 902183336,
"groupIds": [],
"frameId": null,
"roundness": null,
"boundElements": [],
"updated": 1732565113996,
"link": null,
"locked": false,
"fontSize": 20,
"fontFamily": 3,
"text": "3125",
"textAlign": "left",
"verticalAlign": "middle",
"containerId": "KsSzJicTZlMzyYamex99O",
"originalText": "3125",
"lineHeight": 1.2,
"baseline": 20
},
{
"type": "rectangle",
"version": 42,
"versionNonce": 55503449,
"isDeleted": false,
"id": "4l74x5uAeTeBuxx_8t_QY",
"fillStyle": "solid",
"strokeWidth": 2,
"strokeStyle": "solid",
"roughness": 0,
"opacity": 100,
"angle": 0,
"x": 480,
"y": 584.4962525171356,
"strokeColor": "#1e1e1e",
"backgroundColor": "transparent",
"width": 260,
"height": 40,
"seed": 595254712,
"groupIds": [],
"frameId": null,
"roundness": {
"type": 3
},
"boundElements": [
{
"type": "text",
"id": "0iH5yRbH4IEseV3mnof3A"
}
],
"updated": 1732565113996,
"link": null,
"locked": false
},
{
"type": "text",
"version": 321,
"versionNonce": 940641777,
"isDeleted": false,
"id": "0iH5yRbH4IEseV3mnof3A",
"fillStyle": "solid",
"strokeWidth": 2,
"strokeStyle": "solid",
"roughness": 0,
"opacity": 100,
"angle": 0,
"x": 486.953125,
"y": 592.4962525171356,
"strokeColor": "#1e1e1e",
"backgroundColor": "transparent",
"width": 246.09375,
"height": 24,
"seed": 2011882973,
"groupIds": [],
"frameId": null,
"roundness": null,
"boundElements": [],
"updated": 1732566143666,
"link": null,
"locked": false,
"fontSize": 20,
"fontFamily": 3,
"text": " UNKNOWN ",
"textAlign": "center",
"verticalAlign": "middle",
"containerId": "4l74x5uAeTeBuxx_8t_QY",
"originalText": " UNKNOWN ",
"lineHeight": 1.2,
"baseline": 19
}
],
"appState": {
"gridSize": 20,
"viewBackgroundColor": "#f5faff"
},
"files": {}
}

View file

@ -0,0 +1,10 @@
{
"rust-analyzer.check.overrideCommand": [
"cargo",
"component",
"check",
"--workspace",
"--all-targets",
"--message-format=json"
],
}

View file

@ -0,0 +1,26 @@
[package]
name = "rust_diagram_connector"
version.workspace = true
edition.workspace = true
repository.workspace = true
authors.workspace = true
readme.workspace = true
publish.workspace = true
[dependencies]
wit-bindgen-rt = { version = "0.26.0", features = ["bitflags"] }
[lib]
crate-type = ["cdylib"]
[profile.release]
codegen-units = 1
opt-level = "s"
debug = false
strip = true
lto = true
[package.metadata.component]
package = "component:rust-diagram-connector"
[package.metadata.component.dependencies]

View file

@ -0,0 +1,7 @@
How to create and build the Rust component:
1. `cargo install cargo-component`
2. `cargo component new rust_diagram_connector --lib`
3. `cd rust_diagram_connector`
4. Update code as needed
5. `cargo component build --release --target wasm32-unknown-unknown && cp ../../../target/wasm32-unknown-unknown/release/rust_diagram_connector.wasm .`

View file

@ -0,0 +1,329 @@
// Generated by `wit-bindgen` 0.25.0. DO NOT EDIT!
// Options used:
#[allow(dead_code)]
pub mod component {
#[allow(dead_code)]
pub mod diagram_connector {
#[allow(dead_code, clippy::all)]
pub mod host {
#[used]
#[doc(hidden)]
#[cfg(target_arch = "wasm32")]
static __FORCE_SECTION_REF: fn() =
super::super::super::__link_custom_section_describing_imports;
use super::super::super::_rt;
#[allow(unused_unsafe, clippy::all)]
pub fn log(message: &str) {
unsafe {
let vec0 = message;
let ptr0 = vec0.as_ptr().cast::<u8>();
let len0 = vec0.len();
#[cfg(target_arch = "wasm32")]
#[link(wasm_import_module = "component:diagram-connector/host")]
extern "C" {
#[link_name = "log"]
fn wit_import(_: *mut u8, _: usize);
}
#[cfg(not(target_arch = "wasm32"))]
fn wit_import(_: *mut u8, _: usize) {
unreachable!()
}
wit_import(ptr0.cast_mut(), len0);
}
}
#[allow(unused_unsafe, clippy::all)]
pub fn listen_for_component_text_changes(
diagram_connect_name: &str,
component_id: &str,
) {
unsafe {
let vec0 = diagram_connect_name;
let ptr0 = vec0.as_ptr().cast::<u8>();
let len0 = vec0.len();
let vec1 = component_id;
let ptr1 = vec1.as_ptr().cast::<u8>();
let len1 = vec1.len();
#[cfg(target_arch = "wasm32")]
#[link(wasm_import_module = "component:diagram-connector/host")]
extern "C" {
#[link_name = "listen-for-component-text-changes"]
fn wit_import(_: *mut u8, _: usize, _: *mut u8, _: usize);
}
#[cfg(not(target_arch = "wasm32"))]
fn wit_import(_: *mut u8, _: usize, _: *mut u8, _: usize) {
unreachable!()
}
wit_import(ptr0.cast_mut(), len0, ptr1.cast_mut(), len1);
}
}
#[allow(unused_unsafe, clippy::all)]
pub fn set_component_text(component_id: &str, text: &str) {
unsafe {
let vec0 = component_id;
let ptr0 = vec0.as_ptr().cast::<u8>();
let len0 = vec0.len();
let vec1 = text;
let ptr1 = vec1.as_ptr().cast::<u8>();
let len1 = vec1.len();
#[cfg(target_arch = "wasm32")]
#[link(wasm_import_module = "component:diagram-connector/host")]
extern "C" {
#[link_name = "set-component-text"]
fn wit_import(_: *mut u8, _: usize, _: *mut u8, _: usize);
}
#[cfg(not(target_arch = "wasm32"))]
fn wit_import(_: *mut u8, _: usize, _: *mut u8, _: usize) {
unreachable!()
}
wit_import(ptr0.cast_mut(), len0, ptr1.cast_mut(), len1);
}
}
#[allow(unused_unsafe, clippy::all)]
pub fn address_and_way(time_text: &str) -> Result<(_rt::String, Option<u32>), ()> {
unsafe {
#[repr(align(4))]
struct RetArea([::core::mem::MaybeUninit<u8>; 20]);
let mut ret_area = RetArea([::core::mem::MaybeUninit::uninit(); 20]);
let vec0 = time_text;
let ptr0 = vec0.as_ptr().cast::<u8>();
let len0 = vec0.len();
let ptr1 = ret_area.0.as_mut_ptr().cast::<u8>();
#[cfg(target_arch = "wasm32")]
#[link(wasm_import_module = "component:diagram-connector/host")]
extern "C" {
#[link_name = "address-and-way"]
fn wit_import(_: *mut u8, _: usize, _: *mut u8);
}
#[cfg(not(target_arch = "wasm32"))]
fn wit_import(_: *mut u8, _: usize, _: *mut u8) {
unreachable!()
}
wit_import(ptr0.cast_mut(), len0, ptr1);
let l2 = i32::from(*ptr1.add(0).cast::<u8>());
match l2 {
0 => {
let e = {
let l3 = *ptr1.add(4).cast::<*mut u8>();
let l4 = *ptr1.add(8).cast::<usize>();
let len5 = l4;
let bytes5 = _rt::Vec::from_raw_parts(l3.cast(), len5, len5);
let l6 = i32::from(*ptr1.add(12).cast::<u8>());
(
_rt::string_lift(bytes5),
match l6 {
0 => None,
1 => {
let e = {
let l7 = *ptr1.add(16).cast::<i32>();
l7 as u32
};
Some(e)
}
_ => _rt::invalid_enum_discriminant(),
},
)
};
Ok(e)
}
1 => {
let e = ();
Err(e)
}
_ => _rt::invalid_enum_discriminant(),
}
}
}
}
}
}
#[allow(dead_code)]
pub mod exports {
#[allow(dead_code)]
pub mod component {
#[allow(dead_code)]
pub mod diagram_connector {
#[allow(dead_code, clippy::all)]
pub mod diagram_connector {
#[used]
#[doc(hidden)]
#[cfg(target_arch = "wasm32")]
static __FORCE_SECTION_REF: fn() =
super::super::super::super::__link_custom_section_describing_imports;
use super::super::super::super::_rt;
#[doc(hidden)]
#[allow(non_snake_case)]
pub unsafe fn _export_init_cabi<T: Guest>() {
#[cfg(target_arch = "wasm32")]
_rt::run_ctors_once();
T::init();
}
#[doc(hidden)]
#[allow(non_snake_case)]
pub unsafe fn _export_name_cabi<T: Guest>() -> *mut u8 {
#[cfg(target_arch = "wasm32")]
_rt::run_ctors_once();
let result0 = T::name();
let ptr1 = _RET_AREA.0.as_mut_ptr().cast::<u8>();
let vec2 = (result0.into_bytes()).into_boxed_slice();
let ptr2 = vec2.as_ptr().cast::<u8>();
let len2 = vec2.len();
::core::mem::forget(vec2);
*ptr1.add(4).cast::<usize>() = len2;
*ptr1.add(0).cast::<*mut u8>() = ptr2.cast_mut();
ptr1
}
#[doc(hidden)]
#[allow(non_snake_case)]
pub unsafe fn __post_return_name<T: Guest>(arg0: *mut u8) {
let l0 = *arg0.add(0).cast::<*mut u8>();
let l1 = *arg0.add(4).cast::<usize>();
_rt::cabi_dealloc(l0, l1, 1);
}
#[doc(hidden)]
#[allow(non_snake_case)]
pub unsafe fn _export_on_component_text_changed_cabi<T: Guest>(
arg0: *mut u8,
arg1: usize,
arg2: *mut u8,
arg3: usize,
) {
#[cfg(target_arch = "wasm32")]
_rt::run_ctors_once();
let len0 = arg1;
let bytes0 = _rt::Vec::from_raw_parts(arg0.cast(), len0, len0);
let len1 = arg3;
let bytes1 = _rt::Vec::from_raw_parts(arg2.cast(), len1, len1);
T::on_component_text_changed(
_rt::string_lift(bytes0),
_rt::string_lift(bytes1),
);
}
pub trait Guest {
fn init();
fn name() -> _rt::String;
fn on_component_text_changed(component_id: _rt::String, text: _rt::String);
}
#[doc(hidden)]
macro_rules! __export_component_diagram_connector_diagram_connector_cabi{
($ty:ident with_types_in $($path_to_types:tt)*) => (const _: () = {
#[export_name = "component:diagram-connector/diagram-connector#init"]
unsafe extern "C" fn export_init() {
$($path_to_types)*::_export_init_cabi::<$ty>()
}
#[export_name = "component:diagram-connector/diagram-connector#name"]
unsafe extern "C" fn export_name() -> *mut u8 {
$($path_to_types)*::_export_name_cabi::<$ty>()
}
#[export_name = "cabi_post_component:diagram-connector/diagram-connector#name"]
unsafe extern "C" fn _post_return_name(arg0: *mut u8,) {
$($path_to_types)*::__post_return_name::<$ty>(arg0)
}
#[export_name = "component:diagram-connector/diagram-connector#on-component-text-changed"]
unsafe extern "C" fn export_on_component_text_changed(arg0: *mut u8,arg1: usize,arg2: *mut u8,arg3: usize,) {
$($path_to_types)*::_export_on_component_text_changed_cabi::<$ty>(arg0, arg1, arg2, arg3)
}
};);
}
#[doc(hidden)]
pub(crate) use __export_component_diagram_connector_diagram_connector_cabi;
#[repr(align(4))]
struct _RetArea([::core::mem::MaybeUninit<u8>; 8]);
static mut _RET_AREA: _RetArea = _RetArea([::core::mem::MaybeUninit::uninit(); 8]);
}
}
}
}
mod _rt {
pub use alloc_crate::string::String;
pub use alloc_crate::vec::Vec;
pub unsafe fn string_lift(bytes: Vec<u8>) -> String {
if cfg!(debug_assertions) {
String::from_utf8(bytes).unwrap()
} else {
String::from_utf8_unchecked(bytes)
}
}
pub unsafe fn invalid_enum_discriminant<T>() -> T {
if cfg!(debug_assertions) {
panic!("invalid enum discriminant")
} else {
core::hint::unreachable_unchecked()
}
}
#[cfg(target_arch = "wasm32")]
pub fn run_ctors_once() {
wit_bindgen_rt::run_ctors_once();
}
pub unsafe fn cabi_dealloc(ptr: *mut u8, size: usize, align: usize) {
if size == 0 {
return;
}
let layout = alloc::Layout::from_size_align_unchecked(size, align);
alloc::dealloc(ptr as *mut u8, layout);
}
extern crate alloc as alloc_crate;
pub use alloc_crate::alloc;
}
/// Generates `#[no_mangle]` functions to export the specified type as the
/// root implementation of all generated traits.
///
/// For more information see the documentation of `wit_bindgen::generate!`.
///
/// ```rust
/// # macro_rules! export{ ($($t:tt)*) => (); }
/// # trait Guest {}
/// struct MyType;
///
/// impl Guest for MyType {
/// // ...
/// }
///
/// export!(MyType);
/// ```
#[allow(unused_macros)]
#[doc(hidden)]
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::diagram_connector::diagram_connector::__export_component_diagram_connector_diagram_connector_cabi!($ty with_types_in $($path_to_types_root)*::exports::component::diagram_connector::diagram_connector);
)
}
#[doc(inline)]
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; 550] = *b"\
\0asm\x0d\0\x01\0\0\x19\x16wit-component-encoding\x04\0\x07\xa6\x03\x01A\x02\x01\
A\x04\x01B\x0b\x01@\x01\x07messages\x01\0\x04\0\x03log\x01\0\x01@\x02\x14diagram\
-connect-names\x0ccomponent-ids\x01\0\x04\0!listen-for-component-text-changes\x01\
\x01\x01@\x02\x0ccomponent-ids\x04texts\x01\0\x04\0\x12set-component-text\x01\x02\
\x01ky\x01o\x02s\x03\x01j\x01\x04\0\x01@\x01\x09time-texts\0\x05\x04\0\x0faddres\
s-and-way\x01\x06\x03\x01\x20component:diagram-connector/host\x05\0\x01B\x06\x01\
@\0\x01\0\x04\0\x04init\x01\0\x01@\0\0s\x04\0\x04name\x01\x01\x01@\x02\x0ccompon\
ent-ids\x04texts\x01\0\x04\0\x19on-component-text-changed\x01\x02\x04\x01-compon\
ent:diagram-connector/diagram-connector\x05\x01\x04\x01%component:diagram-connec\
tor/component\x04\0\x0b\x0f\x01\0\x09component\x03\0\0\0G\x09producers\x01\x0cpr\
ocessed-by\x02\x0dwit-component\x070.208.1\x10wit-bindgen-rust\x060.25.0";
#[inline(never)]
#[doc(hidden)]
#[cfg(target_arch = "wasm32")]
pub fn __link_custom_section_describing_imports() {
wit_bindgen_rt::maybe_link_cabi_realloc();
}

View file

@ -0,0 +1,63 @@
use std::borrow::Cow;
use std::cell::RefCell;
#[allow(warnings)]
mod bindings;
use bindings::component::diagram_connector::host;
use bindings::exports::component::diagram_connector::diagram_connector;
macro_rules! log {
($($arg:tt)*) => (host::log(&format!($($arg)*)))
}
static NAME: &str = "Rust Test Diagram Connector";
// Note: Ids from `test_files/cache_diagram.excalidraw`
const ADDRESS_COMPONENT_ID: &str = "ITxhJ7NtZ74YFd9JQ0_pl";
const TIME_COMPONENT_ID: &str = "afXu8_6Kqfq-q2IsjtAcP";
const STATUS_COMPONENT_ID: &str = "0iH5yRbH4IEseV3mnof3A";
thread_local! {
static TIME_TEXT: RefCell<String> = <_>::default();
}
struct Component;
impl diagram_connector::Guest for Component {
fn init() {
host::listen_for_component_text_changes(NAME, TIME_COMPONENT_ID);
log!("'{NAME}' initialized")
}
fn name() -> String {
NAME.to_string()
}
fn on_component_text_changed(component_id: String, text: String) {
match component_id.as_str() {
TIME_COMPONENT_ID => {
TIME_TEXT.set(text);
refresh_fields();
}
_ => (),
}
}
}
fn refresh_fields() {
let way = TIME_TEXT.with_borrow(|time_text| host::address_and_way(&time_text));
let Ok((address, way)) = way else {
return;
};
host::set_component_text(ADDRESS_COMPONENT_ID, &address);
let status_text: Cow<str> = if let Some(way) = way {
format!("VALID, WAY: {way}").into()
} else {
"NOT VALID".into()
};
host::set_component_text(STATUS_COMPONENT_ID, &status_text);
}
bindings::export!(Component with_types_in bindings);

View file

@ -0,0 +1,19 @@
package component:diagram-connector;
interface host {
log: func(message: string);
listen-for-component-text-changes: func(diagram-connect-name: string, component-id: string);
set-component-text: func(component-id: string, text: string);
address-and-way: func(time-text: string) -> result<tuple<string, option<u32>>>;
}
interface diagram-connector {
init: func();
name: func() -> string;
on-component-text-changed: func(component-id: string, text: string);
}
world component {
import host;
export diagram-connector;
}

Binary file not shown.