From 1312c30c7255d6700b75f268d071aef1cdace9b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Kav=C3=ADk?= Date: Sat, 15 Jun 2024 23:15:28 +0200 Subject: [PATCH] script_bridge.rs, index.js --- MoonZoon.toml | 6 +++++- backend/index.js | 2 ++ backend/src/main.rs | 4 ++++ frontend/src/main.rs | 1 + frontend/src/script_bridge.rs | 21 +++++++++++++++++++++ 5 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 backend/index.js create mode 100644 frontend/src/script_bridge.rs diff --git a/MoonZoon.toml b/MoonZoon.toml index d53ee41..d06702c 100644 --- a/MoonZoon.toml +++ b/MoonZoon.toml @@ -1,7 +1,9 @@ port = 8080 # port = 8443 https = false -cache_busting = true +# @TODO how to import `pkg/frontend.js` with enabled cache busting? +# @TODO add a switch to enable Typescript generator in mzoon? +cache_busting = false backend_log_level = "warn" # "error" / "warn" / "info" / "debug" / "trace" [redirect] @@ -23,4 +25,6 @@ frontend = [ backend = [ "backend/Cargo.toml", "backend/src", + "backend/index.js", + "backend/style.css", ] diff --git a/backend/index.js b/backend/index.js new file mode 100644 index 0000000..b1896bc --- /dev/null +++ b/backend/index.js @@ -0,0 +1,2 @@ +import { FW } from '/_api/pkg/frontend.js'; +window.FW = FW; diff --git a/backend/src/main.rs b/backend/src/main.rs index 1b86c9a..7043f81 100644 --- a/backend/src/main.rs +++ b/backend/src/main.rs @@ -5,6 +5,10 @@ async fn frontend() -> Frontend { "" + )).append_to_head(concat!( + "" )) } diff --git a/frontend/src/main.rs b/frontend/src/main.rs index e551c3d..847d045 100644 --- a/frontend/src/main.rs +++ b/frontend/src/main.rs @@ -2,6 +2,7 @@ use std::rc::Rc; use zoon::*; mod platform; +mod script_bridge; mod controls_panel; use controls_panel::ControlsPanel; diff --git a/frontend/src/script_bridge.rs b/frontend/src/script_bridge.rs new file mode 100644 index 0000000..027524e --- /dev/null +++ b/frontend/src/script_bridge.rs @@ -0,0 +1,21 @@ +use zoon::{*, println}; + +#[wasm_bindgen(inline_js = r#"export function strict_eval(code) { return eval?.(`"use strict"; ${code};`) }"#)] +extern "C" { + #[wasm_bindgen(catch)] + pub fn strict_eval(code: &str) -> Result; +} + +#[wasm_bindgen] +pub struct FW; + +#[wasm_bindgen] +impl FW { + pub fn do_something() { + println!("Command result: {:#?}", strict_eval("FW.do_something_else();")); + } + + pub fn do_something_else() { + println!("ELSE!"); + } +}