Here is my implementation following the Rust + WASM book.
This goes through the first half of the section for creating the life game, so still using the pre
before putting
as a canvas.
Primarily the pieces of work that are being done are happening in src/lib.rs
and www/index.js
.
Before continuing, you must also have wasm-pack installed.
Then in the directory, you can run wasm-pack build
which should create a pkg/
, this is how we are able to connect
our own package to be consumed by the browser. From there you can move into the www
directory (cd www/
),
install the packages npm install
and run npm run start
and it should be running http://localhost:8080.
During the stream, we got through all the steps and we encountered a HUGE output. Turns out the output was composed of two things:
- The
.wat
output - An error with
wasm-opt
This is the error output I saw:
[wasm-validator error in module] unexpected true: Exported global cannot be mutable, on
global$0
Fatal: error in validating input
Error: failed to execute `wasm-opt`: exited with exit code: 1
full command: "/Users/<current_user>/Library/Caches/.wasm-pack/wasm-opt-a528729925722b63/wasm-opt" "/<path_to_project>/wasm-game-of-life/pkg/wasm_game_of_life_bg.wasm" "-o" "/<path_to_project>/wasm-game-of-life/pkg/wasm_game_of_life_bg.wasm-opt.wasm" "-O"
To disable `wasm-opt`, add `wasm-opt = false` to your package metadata in your `Cargo.toml`.
There was a comment on a related issue that solved it for me.
Adding this to Cargo.toml
:
[package.metadata.wasm-pack.profile.release]
wasm-opt = ["-Oz", "--enable-mutable-globals"]