# Game of Life example

Continuously updates a slightly modified variant of Conway's Game of Life (opens new window) and visualizes its state on a canvas.

# Contents

  • Exporting functions from a WebAssembly module.
  • Calling functions exported from WebAssembly.
  • Importing configuration values from JavaScript.
  • Instantiating the module's memory in JavaScript and import it using --importMemory.
  • Speeding up a program by forcing helper functions in a hot path to always @inline.
  • Utilizing JavaScript's Math instead of native libm to reduce module size via --use Math=JSMath.
  • Reacting to user input by directly modifying an input image buffer.
  • Finding out about WebAssembly's unintuitive byte order.
  • And finally: Continuously updating an input to an output image buffer and rendering the output image buffer.
  • Featuring: Clicking and drawing lots of stuff.

# Example

NOTE

The example makes a couple assumptions. For instance, using the entire memory of the program as the image buffer as in this example is only possible because we know that no interferring static memory segments will be created, which is achieved by

  • using JavaScript's Math instead of native libm (usually adds lookup tables),
  • not using a more sophisticated runtime (typically adds bookkeeping) and
  • the rest of the example being relatively simple (i.e. no strings or similar).

As soon as these conditions are no longer met, one would instead either reserve some space by specifying a suitable --memoryBase or export a dynamically instantiated chunk of memory, like an Uint32Array, and utilize it as the input and output image buffers both in WebAssembly and in JavaScript.

# Running locally

Instructions are identical to those of the Mandelbrot example.