The WASM bindings crate
Create a thin layer to define WebAssembly bindings:
cargo new --lib wasm_bindings
cd wasm_bindings
Dependencies
We will add dependencies to:
wasm-bindgenfor creating WebAssembly bindings, and- to our own
computationcrate using thewasmfeature.
Recall that
wasmfeature ofcomputationcrate enables thewasmfeature oforx-parallel.
Update par_wasm/wasm_bindings/Cargo.toml as follows:
[package]
name = "wasm_bindings"
version = "0.1.0"
edition = "2024"
publish = false
[lib]
crate-type = ["cdylib", "rlib"]
[dependencies]
computation = { path = "../computation", features = ["wasm"] }
wasm-bindgen = "0.2"
Exposed functions
Update par_wasm/wasm_bindings/src/lib.rs as follows:
#![allow(unused)]
fn main() {
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
pub fn calculate_fibonacci(workload: u32, num_threads: u32) -> u64 {
computation::calculate_fibonacci(workload as usize, num_threads as usize)
}
#[wasm_bindgen]
pub fn mandelbrot_checksum(limit: u32, num_threads: u32) -> u32 {
computation::mandelbrot_checksum(limit as usize, num_threads as usize) as u32
}
}
Notice that we keep this layer as thin as possible:
- we make necessary type conversions,
- call our computation crate functions.
Build (optional)
You may try building this crate before implementing the frontend:
RUSTUP_TOOLCHAIN=nightly \
CARGO_TARGET_WASM32_UNKNOWN_UNKNOWN_RUSTFLAGS='-C target-feature=+atomics -C link-arg=--shared-memory -C link-arg=--max-memory=1073741824 -C link-arg=--import-memory -C link-arg=--export=__heap_base -C link-arg=--export=__wasm_init_tls -C link-arg=--export=__tls_size -C link-arg=--export=__tls_align -C link-arg=--export=__tls_base' \
cargo build \
--target wasm32-unknown-unknown \
--release \
-Z build-std=panic_abort,std
These flags enable multi-threaded WebAssembly execution:
+atomicsand--shared-memoryenable atomic operations and shared linear memory for thread coordination,--max-memorysets the memory limit,--import-memoryallows the host to provide memory, and the__*exports expose thread-local storage (TLS) setup functions needed for proper thread initialization.
As we will see in the next section, this build step will be automated usingorx-parallel-wasm.
One level up into par_wasm directory:
cd ..