most basic working helloworldish example

This commit is contained in:
Scott Richmond 2024-11-24 22:49:22 -05:00
commit 8d39036c53
7 changed files with 177 additions and 0 deletions

124
Cargo.lock generated Normal file
View File

@ -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"

15
Cargo.toml Normal file
View File

@ -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']}

2
justfile Normal file
View File

@ -0,0 +1,2 @@
build:
wasm-pack build --target web --out-dir web/dist

20
src/lib.rs Normal file
View File

@ -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}"));
}

1
things.js Normal file
View File

@ -0,0 +1 @@
export function foo () {return "bar"}

10
web/index.html Normal file
View File

@ -0,0 +1,10 @@
<!doctype html>
<html>
<head>
<meta charset = "utf-8" />
<title>Rust + Wasm Minimal Setup</title>
</head>
<body>
<script type = "module" src = "./index.js"></script>
</body>
</html>

5
web/index.js Normal file
View File

@ -0,0 +1,5 @@
import init, * as wasm from "./dist/rustwasm.js"
await init()
wasm.greet()