From 8d39036c53ea6227e50133d0689d6df33ade3a5c Mon Sep 17 00:00:00 2001 From: Scott Richmond Date: Sun, 24 Nov 2024 22:49:22 -0500 Subject: [PATCH] most basic working helloworldish example --- Cargo.lock | 124 +++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 15 ++++++ justfile | 2 + src/lib.rs | 20 ++++++++ things.js | 1 + web/index.html | 10 ++++ web/index.js | 5 ++ 7 files changed, 177 insertions(+) create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 justfile create mode 100644 src/lib.rs create mode 100644 things.js create mode 100644 web/index.html create mode 100644 web/index.js diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..20174e2 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,124 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "bumpalo" +version = "3.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "log" +version = "0.4.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" + +[[package]] +name = "once_cell" +version = "1.20.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1261fe7e33c73b354eab43b1273a57c8f967d0391e80353e51f764ac02cf6775" + +[[package]] +name = "proc-macro2" +version = "1.0.92" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "37d3544b3f2748c54e147655edb5025752e2303145b5aefb3c3ea2c78b973bb0" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.37" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "rustwasm" +version = "0.1.0" +dependencies = [ + "wasm-bindgen", +] + +[[package]] +name = "syn" +version = "2.0.89" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44d46482f1c1c87acd84dea20c1bf5ebff4c757009ed6bf19cfd36fb10e92c4e" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "adb9e6ca4f869e1180728b7950e35922a7fc6397f7b641499e8f3ef06e50dc83" + +[[package]] +name = "wasm-bindgen" +version = "0.2.95" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "128d1e363af62632b8eb57219c8fd7877144af57558fb2ef0368d0087bddeb2e" +dependencies = [ + "cfg-if", + "once_cell", + "wasm-bindgen-macro", +] + +[[package]] +name = "wasm-bindgen-backend" +version = "0.2.95" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb6dd4d3ca0ddffd1dd1c9c04f94b868c37ff5fac97c30b97cff2d74fce3a358" +dependencies = [ + "bumpalo", + "log", + "once_cell", + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.95" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e79384be7f8f5a9dd5d7167216f022090cf1f9ec128e6e6a482a2cb5c5422c56" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.95" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26c6ab57572f7a24a4985830b120de1594465e5d500f24afe89e16b4e833ef68" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-backend", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.95" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "65fc09f10666a9f147042251e0dda9c18f166ff7de300607007e96bdebc1068d" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..490c91d --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "rustwasm" +version = "0.1.0" +edition = "2021" + +[dependencies] +wasm-bindgen = "0.2" + +# this is necessary to build the wasm blob +[lib] +crate-type = ["cdylib"] + +# suppress the lint warning for the #[wasm_bindgen] directive in lib.rs +[lints.rust] +unexpected_cfgs = { level = "warn", check-cfg = ['wasm_bindgen']} diff --git a/justfile b/justfile new file mode 100644 index 0000000..36d788b --- /dev/null +++ b/justfile @@ -0,0 +1,2 @@ +build: + wasm-pack build --target web --out-dir web/dist diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..4107685 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,20 @@ +use wasm_bindgen::prelude::*; + +// the js file must be in the RUST project +// it gets bundled +#[wasm_bindgen(module = "/things.js")] +extern "C" { + fn foo() -> String; +} + +#[wasm_bindgen] +extern "C" { + fn alert(s: String); +} + +// 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}")); +} diff --git a/things.js b/things.js new file mode 100644 index 0000000..504f57f --- /dev/null +++ b/things.js @@ -0,0 +1 @@ +export function foo () {return "bar"} diff --git a/web/index.html b/web/index.html new file mode 100644 index 0000000..d07327e --- /dev/null +++ b/web/index.html @@ -0,0 +1,10 @@ + + + + + Rust + Wasm Minimal Setup + + + + + diff --git a/web/index.js b/web/index.js new file mode 100644 index 0000000..65dec06 --- /dev/null +++ b/web/index.js @@ -0,0 +1,5 @@ +import init, * as wasm from "./dist/rustwasm.js" + +await init() +wasm.greet() +