diff --git a/Cargo.lock b/Cargo.lock index 3ce8a32..c2a369a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -14,6 +14,16 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "console_error_panic_hook" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a06aeb73f470f66dcdbf7223caeebb85984942f22f1adb2a088cf9668146bbbc" +dependencies = [ + "cfg-if", + "wasm-bindgen", +] + [[package]] name = "gamarjoba" version = "0.1.0" @@ -21,6 +31,14 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "game-of-life" +version = "0.1.0" +dependencies = [ + "console_error_panic_hook", + "wasm-bindgen", +] + [[package]] name = "log" version = "0.4.20" diff --git a/game-of-life/Cargo.toml b/game-of-life/Cargo.toml index e7a5a94..bb8a36b 100644 --- a/game-of-life/Cargo.toml +++ b/game-of-life/Cargo.toml @@ -5,4 +5,16 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[lib] +crate-type = ["cdylib", "rlib"] + +[features] +default = ["console_error_panic_hook"] + [dependencies] +wasm-bindgen = "0.2.84" +console_error_panic_hook = { version = "0.1.7", optional = true } + +[profile.release] +# Tell `rustc` to optimize for small code size. +opt-level = "s" diff --git a/game-of-life/justfile b/game-of-life/justfile new file mode 100644 index 0000000..3d1b34c --- /dev/null +++ b/game-of-life/justfile @@ -0,0 +1,8 @@ +_default: + just --list + +wasm_pack := "~/.cargo/bin/wasm-pack" + +# Use wasm-pack to build +build: + {{wasm_pack}} build diff --git a/game-of-life/src/lib.rs b/game-of-life/src/lib.rs index 7d12d9a..aacbc2d 100644 --- a/game-of-life/src/lib.rs +++ b/game-of-life/src/lib.rs @@ -1,14 +1,13 @@ -pub fn add(left: usize, right: usize) -> usize { - left + right +mod util; + +use wasm_bindgen::prelude::*; + +#[wasm_bindgen] +extern "C" { + fn alert(s: &str); } -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn it_works() { - let result = add(2, 2); - assert_eq!(result, 4); - } +#[wasm_bindgen] +pub fn greet() { + alert("Hello, {{project-name}}!"); } diff --git a/game-of-life/src/util.rs b/game-of-life/src/util.rs new file mode 100644 index 0000000..b1d7929 --- /dev/null +++ b/game-of-life/src/util.rs @@ -0,0 +1,10 @@ +pub fn set_panic_hook() { + // When the `console_error_panic_hook` feature is enabled, we can call the + // `set_panic_hook` function at least once during initialization, and then + // we will get better error messages if our code ever panics. + // + // For more details see + // https://github.com/rustwasm/console_error_panic_hook#readme + #[cfg(feature = "console_error_panic_hook")] + console_error_panic_hook::set_once(); +}