M0: WIT package, MoonBit components, JS host, docs, CI (#1)
CI / test (push) Successful in 19s

This commit was merged in pull request #1.
This commit is contained in:
nandi committed 2026-11-24 23:51:26 +00:00
1 parent e5548fc067
commit 880ecd4102
141 files changed
+9681 -1

No files matched your search

+124
View File
@@ -0,0 +1,124 @@
# Toolchain: what works today (verified 2026-11-24)
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 (`moonplasma: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 `moon-plasma` 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
```
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 moonplasma/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 moonplasma:desktop/types@0.1.0;
# import moonplasma:desktop/clock@0.1.0;
# import moonplasma:desktop/theme@0.1.0;
# export moonplasma: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.