Skip to content

Commit 0af83db

Browse files
committed
call global javascript function from rust (ugly)
1 parent 5b8ed84 commit 0af83db

File tree

5 files changed

+18
-10
lines changed

5 files changed

+18
-10
lines changed

wasm_test_site/shim.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
function writeToDom(str) {
2+
document.body.appendChild(str);
3+
}

wasm_test_site/src/lib.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ fn log(string: impl AsRef<str>) {
44
web_sys::console::log_1(&JsValue::from_str(string.as_ref()));
55
}
66

7+
#[wasm_bindgen(module = "/shim.js")]
8+
extern "C" {
9+
fn writeToDom(s: String);
10+
}
11+
712
#[wasm_bindgen]
813
pub fn greet() {
914
console_error_panic_hook::set_once();
@@ -17,7 +22,7 @@ pub fn test_hydroflow() -> web_sys::js_sys::Promise {
1722

1823
let mut df = hydroflow::hydroflow_syntax! {
1924
// https://hydro.run/docs/hydroflow/quickstart/example_1_simplest
20-
source_iter(0..10) -> for_each(|n| log(format!("Hello {}", n)));
25+
source_iter(0..10) -> for_each(|n| writeToDom(format!("Hello {}", n)));
2126
};
2227

2328
wasm_bindgen_futures::future_to_promise(async move {

wasm_test_site/www/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
import init, { greet, test_hydroflow } from "wasm_test_site";
22
let { memory } = await init();
3+
4+
(window as any).writeToDom = function(str: string) {
5+
document.body.appendChild(document.createTextNode(str));
6+
document.body.append(document.createElement('br'));
7+
};
8+
39
greet();
410
test_hydroflow();

wasm_test_site/www/package-lock.json

Lines changed: 2 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

wasm_test_site/www/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,4 @@
2727
"vite-plugin-html": "^3.2.0",
2828
"vite-plugin-wasm-pack": "^0.1.12"
2929
}
30-
}
30+
}

0 commit comments

Comments
 (0)