Browse the documentation

WebAssembly in the browser

The WasmLife sample runs Conway's Game of Life, written in plain Python, in the browser on the .NET WebAssembly runtime.

samples/WasmLife in the Typhon repository runs Conway’s Game of Life in the browser. The rules are plain Python in life.ty, which CPython runs as well. Typhon compiles them in a Microsoft.NET.Sdk.WebAssembly project, and the .NET WebAssembly runtime runs them in the browser. JavaScript draws the board on a canvas and calls a small Typhon module, browser.ty, for every generation.

Project

FileContent
life.tyThe Game of Life: a World class on a wrapping grid, patterns in run-length encoding, and a console demo in its __main__ block.
browser.tyThe world the page shows and the functions JavaScript calls, with types the JavaScript interop can pass.
JSExports.csOne [JSExport] method per function of browser.ty.
wwwroot/index.html, wwwroot/main.jsThe page, which starts .NET, runs the animation loop and draws the board.

The project builds with the .NET SDK alone. The first restore downloads the WebAssembly runtime from NuGet, and no workload is needed. life.ty has a __main__ block of its own, so the project sets TyphonMainModule to browser, and the __main__ block of browser.ty becomes the entry point of the app.

Types at the boundary

The JavaScript interop passes only some .NET types, among them int, double, bool, string and arrays of them. A Python int is a 64-bit long, and a list[int] is a List<long>, which the interop does not pass. The functions in browser.ty therefore use .NET types in their signatures and convert their results. An excerpt:

from life import PATTERNS, World

world = World(96, 64)


def step(generations: int32) -> int32:
    """Advances the world by some generations; returns how many cells changed in the last one."""
    changed = 0
    for _ in range(generations):
        changed = world.step()
    return int32(changed)


def cells() -> Array[uint8]:
    """Every cell's age (0 = dead), row by row, capped at 255 so that it fits a byte."""
    return [uint8(min(age, 255)) for age in world.cells].ToArray()

cells() returns a byte[], which arrives in JavaScript as a Uint8Array. JavaScript keeps no game state. After each step it asks for the cells and draws them.

Exports in C#

@JSExport on a Typhon function compiles to the [JSExport] attribute, but nothing is exported. The attribute only marks the method for the JSExport source generator, which writes the marshalling code. Typhon is a source generator too, and Roslyn runs all source generators on the same input, so the JSExport generator does not see the methods that Typhon generates. JSExports.cs therefore declares each export as a one-line C# method that calls the Typhon function:

[JSExport] public static int step(int generations) => browser.step(generations);
[JSExport] public static byte[] cells() => browser.cells();

For the same reason, Typhon code cannot declare [JSImport] functions. The page pulls its data from .NET, and .NET code does not call into the page.

Running the sample

python3 samples/WasmLife/life.ty
tools/typhon/typhon run samples/WasmLife/life.ty
dotnet run --project samples/WasmLife

The first two commands run the console demo of life.ty, and CPython and Typhon print the same output. dotnet run starts a development server at http://localhost:5080. dotnet publish samples/WasmLife -c Release writes static files that any web server can serve. They take about 5.8 MB, or 1.8 MB with Brotli compression.