Files
chandra/docs/TOOLCHAIN.md
T
nandiandClaude Opus 5.5 5ca7242615
CI / test (push) Canceled after 0s
Deploy to the handset / build-and-deploy (push) Successful in 3m11s
Tests: the native suite in seconds, every test on its own, all cores
- scripts/test-native.py (just t WORD): builds once, runs each test in its own
  process in parallel, longest first, with a timeout; CI and just test use it.
- Tests no longer search this machine's apps and icon themes.
- Compiled wasm modules are cached between test processes (CHANDRA_WASM_CACHE).
- draw_circle fills its middle with spans (same pixels); encode_png writes into
  one buffer, with a CRC table.
- The ffprobe helper always waited out its 20 s: its done file was empty.
- The fake polkit helper no longer depends on which shell sh is.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-29 23:45:29 -08:00

135 lines
7.7 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Toolchain: what works today (verified 2026-11-24)
> This page records the M0 toolchain proof (browser/Node host via jco). Since then the
> components also run natively under wasmtime: the host is the MoonBit
> [wasmhost](../wasmhost/README.md) on the wasmtime C API (an earlier Rust host on the
> wasmtime crate was removed on 2026-11-26). For the current build see the
> [README](../README.md). Pinned versions live in `scripts/versions.env`.
Everything here was run on x86_64 Linux (glibc 2.43 locally; CI uses
`node:20-bookworm`, glibc 2.36), without root.
## Verdict
**MoonBit can produce real WebAssembly Components today**, via the official
`wit-bindgen moonbit` backend plus `wasm-tools component new`. No MoonBit
compiler flag is needed: MoonBit emits a *core* wasm module (linear-memory
`wasm` target) that exports the canonical-ABI symbols `wit-bindgen` generated,
and `wasm-tools` wraps it into a component.
| Piece | Status | Notes |
|---|---|---|
| `moon build --target wasm` | works | Used for all components (linear memory, own GC/alloc). |
| `moon build --target wasm-gc` | not usable for components | The canonical ABI needs linear memory + `cabi_realloc`; wit-bindgen's MoonBit backend targets `wasm` only (`preferred_target = "wasm"` in the generated `moon.mod`). |
| `wit-bindgen moonbit` (0.63.0) | works | Generates guest bindings: types, imports, `declare pub fn` stubs for exports, `link.exports` config. Handles records, variants, enums, options, results, lists, strings. Resources/async/streams exist in the backend but are **not** used here. |
| `wasm-tools component embed/new` (1.263.0) | works | `--encoding utf16` (MoonBit strings are UTF-16). |
| `wasm-tools component targets` | works | Used as a **WIT conformance test**: asserts each component implements its declared world. |
| `jco transpile` (1.42.0) | works | Component → JS + core wasm. Used for the M0 host. `--instantiation async` lets the host pass a *per-instance* import object, which is how capability grants work. |
| `wasmtime` (51.0.0) | validates/compiles | `wasmtime compile` accepts every component. We do **not** run them under wasmtime yet: a component with host imports needs an embedder (Rust/C-API) to supply them — `wasmtime run` cannot. That is M4. |
| mooncakes packages | see below | Nothing needed at M0. |
### Pipeline
```
wit/*.wit ──wit-bindgen moonbit──▶ interface/ world/ components/<n>/gen/ (committed)
│ + stub.mbt (hand-written, thin) → lib/* (pure logic, unit-tested)
moon build --target wasm --release ──▶ _build/.../gen.wasm (core module)
wasm-tools component embed wit --world W --encoding utf16 ─▶ wasm-tools component new ─▶ build/components/<n>.wasm
wasm-tools component targets --world W wit <n>.wasm (conformance)
jco transpile --instantiation async ─▶ build/jco/<n>/ (host glue)
```
Component sizes are 24–33 KB each (release build, no wasm-opt).
### Workflow details / gotchas found
* `wit-bindgen moonbit` does **not** write a stub for exported functions in this
version; export packages contain `declare pub fn …` and you implement them in
a `stub.mbt` *in the same package*. We keep `stub.mbt` to adapters only; logic
lives in `lib/*` packages so `moon test` can cover it.
* Using one `--out-dir .` with a distinct `--gen-dir components/<n>/gen` per
component makes all components share **one** copy of the imported types
(`interface/…/types`), so `lib/*` helpers work across components.
* Generated helper functions are emitted in nondeterministic order, so
`scripts/gen-bindings.sh --check` compares sorted lines, not bytes.
* `moon.mod.json` is no longer accepted by this moon (0.1.20261119): use `moon.mod`.
* jco strips the `@version` from import keys (`chandra:desktop/clock`), and
maps WIT `variant` → `{tag, val}`, `enum` → string, `option` → value/`undefined`,
`result` import returns → `{tag:'ok'|'err', val}` (or throw).
* `moon test` runs on the wasm target through `moonrun`; packages that import the
generated `types` package are testable (the extern "wasm" helpers are only called
at the boundary).
* wit-bindgen 0.63 moonbit emits deprecation warnings under newer moon
(`#deprecated pub extend`); harmless.
### Packages surveyed on mooncakes.io (for later milestones)
| Package | Relevance | Backends / caveat |
|---|---|---|
| `mizchi/wit` 0.4.x | WIT parser/resolver in MoonBit | could power an in-MoonBit `chandra` CLI that inspects worlds; not needed at M0 |
| `moonrockz/wit` | WIT scanner/CST/formatter | same |
| `mizchi/wite` 0.11 | component-aware analyzer/optimizer (`wite` can verify a component against WIT, compose WAC) | candidate replacement for `wasm-tools` size/verify steps |
| `moonbitlang/async` | async runtime (native/…); not a component-model async | the component-model async ABI (WASIp3) is in wit-bindgen's backend but out of scope until hosts support it |
| `moonbitlang/core/json` | JSON | core, all backends — for the settings store (M3) |
The `wasm2elf-mb`, `moonview`, `webby`, `curl-wasm`, `moon-mud` and `moon-gpui`
checkouts on this machine were consulted for conventions only; nothing is
imported from them. (`moon-gpui` currently contains only a README here — the
`docs/PORTING.md` referenced in the task is not present in `/workspace/moon-gpui`.)
## Exact setup
```bash
# 1. MoonBit (not installed by our scripts)
curl -fsSL https://cli.moonbitlang.com/install/unix.sh | bash # moon 0.1.20261119
export PATH="$HOME/.moon/bin:$PATH"
# 2. wasm-tools, wit-bindgen, wasmtime → ./.tools/bin (versions in scripts/versions.env)
scripts/setup-toolchain.sh
# 3. jco + playwright-core → host/node_modules (Node ≥ 20)
(cd host && npm ci)
# 4. build every component, then test everything
scripts/build.sh # or: scripts/build.sh clock-binary (one component only)
scripts/test.sh
```
While working, `scripts/test-native.py WORD` (or `just t WORD`) runs the native tests whose file or
name contains WORD, in seconds: each test in its own process, on all cores. Without a word it runs
them all.
Pinned versions: moon 0.1.20261119 (moonc `738607b`), wasm-tools 1.263.0,
wit-bindgen 0.63.0, wasmtime 51.0.0, jco 1.42.0, Node 20/24.
### Hello-world proof (what `scripts/build.sh` does for `clock-digital`)
```bash
wit-bindgen moonbit wit --world clock-widget --out-dir . \
--gen-dir components/clock-digital/gen --project-name chandra/desktop --ignore-module-file
moon build --target wasm --release
wasm-tools component embed wit _build/wasm/release/build/components/clock-digital/gen/gen.wasm \
--world clock-widget --encoding utf16 -o e.wasm
wasm-tools component new e.wasm -o build/components/clock-digital.wasm
wasm-tools component wit build/components/clock-digital.wasm
# world root { import chandra:desktop/types@0.1.0;
# import chandra:desktop/clock@0.1.0;
# import chandra:desktop/theme@0.1.0;
# export chandra:desktop/plasmoid@0.1.0; }
jco transpile build/components/clock-digital.wasm -o build/jco/clock-digital --instantiation async
node host/src/cli.mjs --time 2026-11-24T14:07:00 # instantiates all components, prints the desktop
```
### Browser screenshots
`scripts/screenshot.mjs` drives the browser host in headless Chromium through
`playwright-core`. It needs a Chromium **headless shell** binary; locally:
```bash
export CHROME_PATH=$(ls ~/.cache/ms-playwright/chromium_headless_shell-*/*/chrome-headless-shell)
node scripts/screenshot.mjs docs/screenshots --tag m0
```
(The full `chrome` binary under `chromium-*/` hung on launch in this sandbox; the headless shell works.)
CI does not take screenshots (no browser in the job image); the Node host tests cover the same code path minus Canvas2D.