Skip to content

Commit

Permalink
feat(wip): initial rust wasm project
Browse files Browse the repository at this point in the history
  • Loading branch information
istudyatuni committed Apr 15, 2022
1 parent d081232 commit a2a0bab
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 33 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 2 additions & 24 deletions scripts/build/wasm.sh
Original file line number Diff line number Diff line change
@@ -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
5 changes: 1 addition & 4 deletions src/wasm/.gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
*.js
*.out
*.txt
*.wasm
*
4 changes: 0 additions & 4 deletions src/wasm/build.sh

This file was deleted.

2 changes: 2 additions & 0 deletions wasm/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/target
Cargo.lock
34 changes: 34 additions & 0 deletions wasm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
1 change: 1 addition & 0 deletions wasm/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
use wasm_bindgen::prelude::*;
10 changes: 10 additions & 0 deletions wasm/src/utils.rs
Original file line number Diff line number Diff line change
@@ -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();
}

0 comments on commit a2a0bab

Please sign in to comment.