diff options
| author | Brian Cully <bjc@spork.org> | 2025-08-07 11:40:05 -0400 |
|---|---|---|
| committer | Brian Cully <bjc@spork.org> | 2025-08-07 11:42:35 -0400 |
| commit | b2e411de770a437dbdf31408b877e2952759e57a (patch) | |
| tree | ecb124526bca7c8aa1a6a48999188a393bce1c71 | |
| download | addem-rust-wasm-b2e411de770a437dbdf31408b877e2952759e57a.tar.gz addem-rust-wasm-b2e411de770a437dbdf31408b877e2952759e57a.zip | |
WORKING add test
| -rw-r--r-- | .dir-locals.el | 7 | ||||
| -rw-r--r-- | .envrc | 1 | ||||
| -rw-r--r-- | .gitignore | 2 | ||||
| -rw-r--r-- | Cargo.lock | 133 | ||||
| -rw-r--r-- | Cargo.toml | 10 | ||||
| -rw-r--r-- | Makefile | 7 | ||||
| -rw-r--r-- | index.html | 18 | ||||
| -rw-r--r-- | main.mjs | 16 | ||||
| -rw-r--r-- | shell.nix | 21 | ||||
| -rw-r--r-- | src/lib.rs | 17 |
10 files changed, 232 insertions, 0 deletions
diff --git a/.dir-locals.el b/.dir-locals.el new file mode 100644 index 0000000..55b82b2 --- /dev/null +++ b/.dir-locals.el @@ -0,0 +1,7 @@ +;;; Directory Local Variables -*- no-byte-compile: t -*- +;;; For more information see (info "(emacs) Directory Variables") + +((nil . ((compile-command . "make serve"))) + (js-base-mode . ((js-indent-level . 4) + (js-chain-indent . t) + (js-indent-first-init . dynamic)))) @@ -0,0 +1 @@ +use nix diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..54954ce --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/target +/pkg diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..217555e --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,133 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "addem" +version = "0.1.0" +dependencies = [ + "wasm-bindgen", +] + +[[package]] +name = "bumpalo" +version = "3.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "46c5e41b57b8bba42a04676d81cb89e9ee8e859a1a66f80a5a72e1cb76b34d43" + +[[package]] +name = "cfg-if" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9555578bc9e57714c812a1f84e4fc5b4d21fcb063490c624de019f7464c91268" + +[[package]] +name = "log" +version = "0.4.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "13dc2df351e3202783a1fe0d44375f7295ffb4049267b0f3018346dc122a1d94" + +[[package]] +name = "once_cell" +version = "1.21.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d" + +[[package]] +name = "proc-macro2" +version = "1.0.95" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "02b3e5e68a3a1a02aad3ec490a98007cbc13c37cbe84a3cd7b8e406d76e7f778" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1885c039570dc00dcb4ff087a89e185fd56bae234ddc7f056a945bf36467248d" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "rustversion" +version = "1.0.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a0d197bd2c9dc6e53b84da9556a69ba4cdfab8619eb41a8bd1cc2027a0f6b1d" + +[[package]] +name = "syn" +version = "2.0.104" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "17b6f705963418cdb9927482fa304bc562ece2fdd4f616084c50b7023b435a40" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512" + +[[package]] +name = "wasm-bindgen" +version = "0.2.100" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1edc8929d7499fc4e8f0be2262a241556cfc54a0bea223790e71446f2aab1ef5" +dependencies = [ + "cfg-if", + "once_cell", + "rustversion", + "wasm-bindgen-macro", +] + +[[package]] +name = "wasm-bindgen-backend" +version = "0.2.100" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2f0a0651a5c2bc21487bde11ee802ccaf4c51935d0d3d42a6101f98161700bc6" +dependencies = [ + "bumpalo", + "log", + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.100" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7fe63fc6d09ed3792bd0897b314f53de8e16568c2b3f7982f468c0bf9bd0b407" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.100" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ae87ea40c9f689fc23f209965b6fb8a99ad69aeeb0231408be24920604395de" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-backend", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.100" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a05d73b933a847d6cccdda8f838a22ff101ad9bf93e33684f39c1f5f0eece3d" +dependencies = [ + "unicode-ident", +] diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..64c79ab --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "addem" +version = "0.1.0" +edition = "2024" + +[lib] +crate-type = ["cdylib"] + +[dependencies] +wasm-bindgen = "0.2" diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..f37f0ac --- /dev/null +++ b/Makefile @@ -0,0 +1,7 @@ +# the --target is currently necessary or firefox will return +# "disallowed mime type" -bjc 7-aug-2025 +build: + wasm-pack build --target web + +serve: build + python -m http.server 8118 diff --git a/index.html b/index.html new file mode 100644 index 0000000..1aad79c --- /dev/null +++ b/index.html @@ -0,0 +1,18 @@ +<!DOCTYPE html> +<html> + <head> + <title>wasm test</title> + </head> + + <body> + <h1>wasm test</h1> + <input type='number' id='number-input1' placeholder='5'> + <input type='number' id='number-input2' placeholder='3'> + <button onclick='calculate()'>calc</button> + <p> + result: <span id='result'></span> + </p> + + <script src='./main.mjs' type='module'></script> + </body> +</html> diff --git a/main.mjs b/main.mjs new file mode 100644 index 0000000..086b876 --- /dev/null +++ b/main.mjs @@ -0,0 +1,16 @@ +import init from './pkg/addem.js'; + +async function run() { + console.debug('run'); + const foo = await init(); + console.debug('init done', foo); + window.calculate = _ => { + console.debug('calc'); + const inp1 = document.getElementById('number-input1').value; + const inp2 = document.getElementById('number-input2').value; + const res = foo.add(parseInt(inp1), parseInt(inp2)); + document.getElementById('result').textContent = res; + } +} + +document.addEventListener('DOMContentLoaded', run); diff --git a/shell.nix b/shell.nix new file mode 100644 index 0000000..ab0fd32 --- /dev/null +++ b/shell.nix @@ -0,0 +1,21 @@ +{ pkgs ? import <nixpkgs> {} }: + +pkgs.mkShellNoCC { + packages = with pkgs; [ + rustc + clang # yes, it's necessary or ‘cc’ can't be found. -bjc 2025-aug-7 + lld # ibid. + cargo + rust-analyzer + wasm-pack + # the only thing better than needing cargo's infinite dependencies + # is needing npm's as well, just so we can use a bundler built for + # another, wildly different, registry. + nodePackages.npm + + # for http.server + python3 + ]; + + CARGO_HOME = "/data/bjc/cargo"; +} diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..7db4248 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,17 @@ +use wasm_bindgen::prelude::wasm_bindgen; + +#[wasm_bindgen] +pub fn add(left: u32, right: u32) -> u32 { + left + right +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn it_works() { + let result = add(2, 2); + assert_eq!(result, 4); + } +} |
