From d5f992c28c1c0919ac84cbcf1b4292de56b23d6a Mon Sep 17 00:00:00 2001 From: Scott Richmond Date: Sun, 24 Nov 2024 23:25:14 -0500 Subject: [PATCH] additional CLI JS runtime notes --- readme.md | 4 ++++ src/lib.rs | 7 +++++-- things.js | 4 ++++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/readme.md b/readme.md index adf7635..15591c0 100644 --- a/readme.md +++ b/readme.md @@ -10,3 +10,7 @@ For use in the Rust Ludus backend. * The canonical reference for all this is https://rustwasm.github.io/wasm-bindgen/introduction.html. * Look into what thi.ng does with Zig in its WASM packages: https://github.com/thi-ng/umbrella/tree/develop/packages, especially https://github.com/thi-ng/umbrella/tree/develop/packages/wasm-api. - It's looking like `--target web` is the most appropriate to use in 2024, since they use ES modules and not CommonJS, which is what everything else uses (and necessitates the build step). See https://rustwasm.github.io/wasm-pack/book/commands/build.html. + - Bun can run the `index.js` file just fine. + - Deno can, too, but it requires file read access, since that's kind of Deno's thing. + - Node cannot. See https://rustwasm.github.io/wasm-pack/book/prerequisites/considerations.html. You have to polyfill `fetch` with `node-fetch` (https://github.com/node-fetch/node-fetch). + * Obligatory: stop trying to make fetch happen. diff --git a/src/lib.rs b/src/lib.rs index 4107685..4a73754 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,6 +5,7 @@ use wasm_bindgen::prelude::*; #[wasm_bindgen(module = "/things.js")] extern "C" { fn foo() -> String; + fn print(s: String); } #[wasm_bindgen] @@ -15,6 +16,8 @@ extern "C" { // without the line in Cargo.toml, this throws a lint warning #[wasm_bindgen] pub fn greet() { - let from_js = foo(); - alert(format!("Hello, {from_js}")); + let s = foo(); + let out = format!("Hi there from Rust. I got {s}"); + alert(out.clone()); + print(out); } diff --git a/things.js b/things.js index 504f57f..4dc02af 100644 --- a/things.js +++ b/things.js @@ -1 +1,5 @@ export function foo () {return "bar"} + +export function print (s) { + console.log(s) +}