From e1aef2c6dba438c9eb972ae24639dc5b37316766 Mon Sep 17 00:00:00 2001 From: Georg Hopp Date: Fri, 10 Jan 2020 11:28:36 +0100 Subject: [PATCH] starting point for wasm tutorial --- tutorial/wasm-game-of-life/.gitignore | 7 +++ tutorial/wasm-game-of-life/Cargo.toml | 34 ++++++++++++ tutorial/wasm-game-of-life/README.md | 69 +++++++++++++++++++++++++ tutorial/wasm-game-of-life/src/lib.rs | 19 +++++++ tutorial/wasm-game-of-life/src/utils.rs | 10 ++++ tutorial/wasm-game-of-life/tests/web.rs | 13 +++++ 6 files changed, 152 insertions(+) create mode 100644 tutorial/wasm-game-of-life/.gitignore create mode 100644 tutorial/wasm-game-of-life/Cargo.toml create mode 100644 tutorial/wasm-game-of-life/README.md create mode 100644 tutorial/wasm-game-of-life/src/lib.rs create mode 100644 tutorial/wasm-game-of-life/src/utils.rs create mode 100644 tutorial/wasm-game-of-life/tests/web.rs diff --git a/tutorial/wasm-game-of-life/.gitignore b/tutorial/wasm-game-of-life/.gitignore new file mode 100644 index 0000000..a766eb8 --- /dev/null +++ b/tutorial/wasm-game-of-life/.gitignore @@ -0,0 +1,7 @@ +/target +**/*.rs.bk +Cargo.lock +bin/ +pkg/ +wasm-pack/ +wasm-pack.log diff --git a/tutorial/wasm-game-of-life/Cargo.toml b/tutorial/wasm-game-of-life/Cargo.toml new file mode 100644 index 0000000..f19795d --- /dev/null +++ b/tutorial/wasm-game-of-life/Cargo.toml @@ -0,0 +1,34 @@ +[package] +name = "wasm-game-of-life" +version = "0.1.0" +authors = ["hopp@silpion.de"] +edition = "2018" + +[lib] +crate-type = ["cdylib", "rlib"] + +[features] +default = ["console_error_panic_hook"] + +[dependencies] +wasm-bindgen = "0.2" + +# The `console_error_panic_hook` crate provides better debugging of panics by +# logging them with `console.error`. This is great for development, but requires +# all the `std::fmt` and `std::panicking` infrastructure, so isn't great for +# code size when deploying. +console_error_panic_hook = { version = "0.1.1", optional = true } + +# `wee_alloc` is a tiny allocator for wasm that is only ~1K in code size +# compared to the default allocator's ~10K. It is slower than the default +# allocator, however. +# +# Unfortunately, `wee_alloc` requires nightly Rust when targeting wasm for now. +wee_alloc = { version = "0.4.2", optional = true } + +[dev-dependencies] +wasm-bindgen-test = "0.2" + +[profile.release] +# Tell `rustc` to optimize for small code size. +opt-level = "s" diff --git a/tutorial/wasm-game-of-life/README.md b/tutorial/wasm-game-of-life/README.md new file mode 100644 index 0000000..1e4617a --- /dev/null +++ b/tutorial/wasm-game-of-life/README.md @@ -0,0 +1,69 @@ +
+ +

wasm-pack-template

+ + A template for kick starting a Rust and WebAssembly project using wasm-pack. + +

+ Build Status +

+ +

+ Tutorial + | + Chat +

+ + Built with 🦀🕸 by The Rust and WebAssembly Working Group +
+ +## About + +[**📚 Read this template tutorial! 📚**][template-docs] + +This template is designed for compiling Rust libraries into WebAssembly and +publishing the resulting package to NPM. + +Be sure to check out [other `wasm-pack` tutorials online][tutorials] for other +templates and usages of `wasm-pack`. + +[tutorials]: https://rustwasm.github.io/docs/wasm-pack/tutorials/index.html +[template-docs]: https://rustwasm.github.io/docs/wasm-pack/tutorials/npm-browser-packages/index.html + +## 🚴 Usage + +### 🐑 Use `cargo generate` to Clone this Template + +[Learn more about `cargo generate` here.](https://github.com/ashleygwilliams/cargo-generate) + +``` +cargo generate --git https://github.com/rustwasm/wasm-pack-template.git --name my-project +cd my-project +``` + +### 🛠️ Build with `wasm-pack build` + +``` +wasm-pack build +``` + +### 🔬 Test in Headless Browsers with `wasm-pack test` + +``` +wasm-pack test --headless --firefox +``` + +### 🎁 Publish to NPM with `wasm-pack publish` + +``` +wasm-pack publish +``` + +## 🔋 Batteries Included + +* [`wasm-bindgen`](https://github.com/rustwasm/wasm-bindgen) for communicating + between WebAssembly and JavaScript. +* [`console_error_panic_hook`](https://github.com/rustwasm/console_error_panic_hook) + for logging panic messages to the developer console. +* [`wee_alloc`](https://github.com/rustwasm/wee_alloc), an allocator optimized + for small code size. diff --git a/tutorial/wasm-game-of-life/src/lib.rs b/tutorial/wasm-game-of-life/src/lib.rs new file mode 100644 index 0000000..7f20bc7 --- /dev/null +++ b/tutorial/wasm-game-of-life/src/lib.rs @@ -0,0 +1,19 @@ +mod utils; + +use wasm_bindgen::prelude::*; + +// When the `wee_alloc` feature is enabled, use `wee_alloc` as the global +// allocator. +#[cfg(feature = "wee_alloc")] +#[global_allocator] +static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT; + +#[wasm_bindgen] +extern { + fn alert(s: &str); +} + +#[wasm_bindgen] +pub fn greet() { + alert("Hello, {{project-name}}!"); +} diff --git a/tutorial/wasm-game-of-life/src/utils.rs b/tutorial/wasm-game-of-life/src/utils.rs new file mode 100644 index 0000000..b1d7929 --- /dev/null +++ b/tutorial/wasm-game-of-life/src/utils.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(); +} diff --git a/tutorial/wasm-game-of-life/tests/web.rs b/tutorial/wasm-game-of-life/tests/web.rs new file mode 100644 index 0000000..de5c1da --- /dev/null +++ b/tutorial/wasm-game-of-life/tests/web.rs @@ -0,0 +1,13 @@ +//! Test suite for the Web and headless browsers. + +#![cfg(target_arch = "wasm32")] + +extern crate wasm_bindgen_test; +use wasm_bindgen_test::*; + +wasm_bindgen_test_configure!(run_in_browser); + +#[wasm_bindgen_test] +fn pass() { + assert_eq!(1 + 1, 2); +}