wshine wasm gol - TheEvergreenStateCollege/upper-division-cs-23-24 GitHub Wiki

2024-04-02

was getting a node/openssl error: ERR_OSSL_EVP_UNSUPPORTED when trying to run the web server via npm start.

solved by downgrading from node 21 to 14

2024-04-14

wasm-pack build from root of project

npm run start from www/

Section 4.4

exercise solution states the cells function can be modified as such:

    pub fn cells(&self) -> *const u32 {
        self.cells.as_slice().as_ptr()
    }

This resulted in a compiler error since cells.as_slice().as_ptr() was returning a *const usize.

Current solution that seems to work.

    pub fn cells(&self) -> *const usize {
        self.cells.as_slice().as_ptr()
    }
testing

If working from wsl. you will likely not have successful tests if using the default firefox available via apt which actually installs via snap... not sure why this is a thing.

install firefox on linux

using wsl version 2 and a firefox installation that was not a snap package I was able to get headless and non headless tests working when using wasm-pack test --firefox.

debugging

adding this section to Cargo.toml was not enough to get a more informative stack trace that had actual function names.

[profile.release]
debug = true

Additionally the command presented in the guide wasm-pack build --debug is deprecated and I was only able to get debug symbols using wasm-pack build --dev

game of life rules
cell rules
  • Any live cell with fewer than two live neighbours dies, as if caused by underpopulation
  • Any live cell with two or threee live neighbours lives on to the next generation.
  • Any live cell with more than three live neighbours die, as if by overpopulation.
  • Any dead cell with exactly three live neighbours becomes a live cell as if by reproduction
infinite universe

we do not have infinite memory and compute power so we have to work around that:

  1. Keep track of which subset of the universe has interesting things happening and expand
  2. create a fixed size universe. Some patterns will be impacted because the edges cells have less neighbors
  3. fixed size periodic universe. cells on the edge have neighbors that wrap around to the other side of the universe

this implementation uses the third choice

Rust & JS

JS garbage collected heap is distinct from WebAssembly's linear memory space. WebAssemly has no direct access to the garbage collected heap. But Javascript can read and write to WebAssembly linear memory space (only as an ArrayBuffer of scalar values).

WebAssembly functions take and return scalar values.

things we want to optimize:

  • minimizing copying into and out of the webAssembly linear memory.
  • minimizing serializing and deserializing.

universe is a flat array.

index(row, column, universe) = row * width(universe) + column

delta row and modulus is used to iterate over adjacent cells without having to write special cases for edges of the universe.

⚠️ **GitHub.com Fallback** ⚠️