Skip to content

About

Lightweight helper functions for WasmGC interop.

Resources

Code of conduct

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

fsharp-wasm-runtime

Part of the fsharp-wasm platform: the browser. Builds on core. Used by ide, backend (the canvas client), newsletter. All projects · Roadmap · Tests · Benchmarks · Live demos

The official browser runtime for F# compiled to WebAssembly GC (.NET 10 / F# 10).

Write 100% F# browser applications with clean Browser.* APIs. No JavaScript application code. Thin, modular JS glue only.

open Browser.Elmish.Core
open Browser.Html

let view model dispatch =
    article {
        class' "counter-demo"
        h2 "Counter"
        el "p" { class' "count-display"; string model.Count }
        div {
            button { onClick (fun () -> dispatch Decrement); "−" }
            button { onClick (fun () -> dispatch Increment); "+" }
        }
    }

let start () = run "counter-root" { Init = init; Update = update; View = view }

Live: every demo runs in the browser, compiled from F# by fsharp-wasm — see the showcase site. ♥ Sponsor the project.

Features

  • Browser.* API — DOM, events, canvas, Web Components, fetch/WebSocket, storage, IndexedDB, workers, pub/sub, timers, WebGPU, CSS
  • HTML builder — div { class' "card"; h1 "Title"; button { onClick …; "+" } }, plus active patterns for CSS
  • Elmish loop — model–update–view with commands, ~60 lines of F#
  • Native Web Components defined entirely in F#
  • No JavaScript application code — ~600 lines of glue in total, one small file per browser API
  • Tested in a real browser — each of the twelve demos is driven by Playwright/Chromium (npm run test:e2e)

Architecture

F# source
    ↓  fsharp-wasm (F# → WasmGC)
.wasm module            ← a JS import is an F# function marked `nativeOnly`
    ↓  instantiate with { builtins: ["js-string"] }
runtime/*.js            ← glue: DOM, canvas, storage, workers, …
    ↓
DOM / Browser APIs

WebAssembly cannot reach the DOM, so a thin JS layer is unavoidable; this runtime makes it small, typed and invisible to the F# author. The compiler marshals what is not a plain number:

F# at the JS boundary in JavaScript
string argument / result a native JS string (the engine's string builtins); a JS null arrives as ""
string array result a JS array, read element-wise
int / float / bool / Handle a number
function argument, e.g. cb: string -> unit a JS function; calling it re-enters the module through the __cb export
callback parameters int, bool, float, string, string array; up to four

Everything asynchronous (IndexedDB, workers, timers, pub/sub) therefore takes a callback — in an Elmish program the callback dispatches a message. A module cannot block on a JS promise.

Browser support: WebAssembly GC and the JS String Builtins — Chrome / Edge 130+, Firefox 134+. (To my knowledge Safari has not shipped the string builtins yet.)

Quick Start

Prerequisites

Important: this is not the standard fable dotnet global tool from NuGet. The upstream Fable 4.x only targets JavaScript. You need the compiler from this org.

The compiler

Use a release of fsharp-wasm: its self-contained binary is downloaded once into ~/.cache/fsharp-wasm and checked against the release's SHA256SUMS. Needs a .NET 10 SDK on the PATH (the compiler cracks projects through it), nothing else.

export FSHARP_WASM_VERSION=v0.1.0-experimental   # a release tag; or put the tag in a `.fsharp-wasm-version` file
# or an unpacked release:
export FSHARP_WASM_BIN=/path/to/fsharp-wasm

Working on the compiler itself? FSHARP_WASM_DIR=/path/to/fsharp-wasm builds and runs its CLI from a checkout (which needs its patched F# compiler, see that repository). Nothing in this repository is a submodule of the compiler or the other way round.

Setup

git clone https://github.com/fsharp-wasm/fsharp-wasm-runtime.git
cd fsharp-wasm-runtime
npm install

Build

npm run build                      # every demo → dist/
node build/build.js --demo todo    # one demo

Run demos

npx serve dist -l 8080             # open http://localhost:8080

Test

npm run test:e2e                   # drives every demo and the landing page in Chromium (Playwright)
node tests/e2e/demos.mjs counter   # one demo

Showcase site

site/ is what GitHub Pages serves: a landing page plus the prebuilt demos, so deploying needs no compiler. After changing F# or glue code, refresh it:

npm run site                       # build all demos, copy them (wasm + pages + glue) into site/

Project Structure

fsharp-wasm-runtime/
├── runtime/          # JS glue, one ES module per browser API (+ boot.js: imports, instantiate)
├── src/
│   ├── Browser/      # Browser.* API (one F# module per area)
│   ├── Html/         # HTML builder (Elements, Attrs, CE builder)
│   └── Elmish/       # Model–update–view loop
├── demos/            # 10 demo applications (F# project + page each)
├── tests/
│   ├── e2e/          # Chromium end-to-end tests (demos + landing page)
│   ├── Integration/  # Playwright spec (npx playwright test)
│   └── Unit/
├── build/            # build.js (demos → dist/), site.mjs (dist/ → site/)
├── site/             # the deployable showcase: landing page + prebuilt demos
├── docs/
└── index.html        # demo gallery

Browser.* Modules

Module Description
Browser.Core Handle type, constants
Browser.Dom Element creation, manipulation, queries
Browser.Events Event listeners (click, input, etc.)
Browser.Canvas Canvas 2D drawing API
Browser.Components Native Web Components
Browser.Net Fetch API + WebSocket
Browser.PubSub Local event bus (pub/sub messaging)
Browser.Storage localStorage / sessionStorage
Browser.IndexedDb IndexedDB wrapper
Browser.Workers Web Workers
Browser.Timing setTimeout, setInterval, rAF
Browser.Gpu WebGPU (basic)
Browser.Css CSS DSL with active patterns
Browser.Quotations Meta-programming with quotations

Demos

Demo Description
Counter Classic Elmish MVU counter
Todo Todo list with input, toggle, remove
Canvas Mandelbrot Pure F# computation → Canvas, next to a JavaScript twin — same picture
Web Component Native <fs-counter> custom element
Realtime Pub/Sub A message bus wired from F#
IndexedDB CRUD Persistent key-value storage
Worker Actor Web Worker message passing
WebGPU Basic GPU availability check + canvas
Benchmark Thirteen kernels, F# vs JavaScript, timed in a worker; chart, spreads, JSON export — results in docs/benchmarks.md
Benchmarks (results) Speed, size and network load times, recorded and public: /benchmarks/, docs/benchmarks.md
Full Dashboard All features combined

Documentation

License

Apache License 2.0 — see NOTICE.

About

Lightweight helper functions for WasmGC interop.

Resources

Code of conduct

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages