From a2a0bab4d3c04a48a73b9d4d4f15b12e015181f7 Mon Sep 17 00:00:00 2001 From: Ilia Date: Fri, 15 Apr 2022 13:57:09 +0300 Subject: [PATCH] feat(wip): initial rust wasm project --- README.md | 8 +++++++- scripts/build/wasm.sh | 26 ++------------------------ src/wasm/.gitignore | 5 +---- src/wasm/build.sh | 4 ---- wasm/.gitignore | 2 ++ wasm/Cargo.toml | 34 ++++++++++++++++++++++++++++++++++ wasm/src/lib.rs | 1 + wasm/src/utils.rs | 10 ++++++++++ 8 files changed, 57 insertions(+), 33 deletions(-) delete mode 100755 src/wasm/build.sh create mode 100644 wasm/.gitignore create mode 100644 wasm/Cargo.toml create mode 100644 wasm/src/lib.rs create mode 100644 wasm/src/utils.rs diff --git a/README.md b/README.md index 5097245..f738a8c 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,13 @@ https://istudyatuni.github.io/mandelbrot ## Build WASM -You need docker +You need rust and cargo (for installing build tool). Install `wasm-pack`: + +```bash +cargo install wasm-pack +``` + +Build: ```bash ./scripts/build/wasm.sh diff --git a/scripts/build/wasm.sh b/scripts/build/wasm.sh index 9f247e2..15e9ff0 100755 --- a/scripts/build/wasm.sh +++ b/scripts/build/wasm.sh @@ -1,26 +1,4 @@ #!/bin/bash -# optimization -O='-O3' - -PARAMS="$O mandelbrot.cpp -o mandelbrot.js --s NO_EXIT_RUNTIME=1 --s EXPORTED_RUNTIME_METHODS=ccall,cwrap,getValue --s EXPORTED_FUNCTIONS=_calcPlane,_malloc,_free --s ALLOW_MEMORY_GROWTH=1 --s EXPORT_ES6=1 --s MALLOC=emmalloc --s MODULARIZE=1 --s ENVIRONMENT=web --ffast-math -$@" - -cd src/wasm - -if [[ $GITHUB_ACTIONS == true ]]; then - em++ $PARAMS -else - docker run --rm -v "$(pwd):/src" -u "$(id -u):$(id -g)" emscripten/emsdk em++ $PARAMS -fi - -# options EXPORT_ES6, MODULARIZE, EXPORTED_RUNTIME_METHODS from https://stackoverflow.com/q/53309095 +cd wasm +wasm-pack build --release --out-dir=../src/wasm diff --git a/src/wasm/.gitignore b/src/wasm/.gitignore index b715609..f59ec20 100644 --- a/src/wasm/.gitignore +++ b/src/wasm/.gitignore @@ -1,4 +1 @@ -*.js -*.out -*.txt -*.wasm +* \ No newline at end of file diff --git a/src/wasm/build.sh b/src/wasm/build.sh deleted file mode 100755 index 4aba89f..0000000 --- a/src/wasm/build.sh +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/bash -#for testing - -clang++ -std=c++20 mandelbrot.cpp -o mandelbrot.out && ./mandelbrot.out diff --git a/wasm/.gitignore b/wasm/.gitignore new file mode 100644 index 0000000..96ef6c0 --- /dev/null +++ b/wasm/.gitignore @@ -0,0 +1,2 @@ +/target +Cargo.lock diff --git a/wasm/Cargo.toml b/wasm/Cargo.toml new file mode 100644 index 0000000..d2dcfe9 --- /dev/null +++ b/wasm/Cargo.toml @@ -0,0 +1,34 @@ +[package] +name = "wasm" +version = "0.1.0" +edition = "2021" + +[lib] +crate-type = ["cdylib", "rlib"] + +[features] +default = ["console_error_panic_hook"] + +[dependencies] +js-sys = "0.3.57" +wasm-bindgen = "0.2.63" + +# 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.6", 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.5", optional = true } + +# [dev-dependencies] +# wasm-bindgen-test = "0.3.13" + +[profile.release] +# Tell `rustc` to optimize for small code size. +opt-level = "s" diff --git a/wasm/src/lib.rs b/wasm/src/lib.rs new file mode 100644 index 0000000..efad3b7 --- /dev/null +++ b/wasm/src/lib.rs @@ -0,0 +1 @@ +use wasm_bindgen::prelude::*; diff --git a/wasm/src/utils.rs b/wasm/src/utils.rs new file mode 100644 index 0000000..b1d7929 --- /dev/null +++ b/wasm/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(); +}