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

+46
View File
@@ -0,0 +1,46 @@
#!/usr/bin/env bash
# Build every component: MoonBit -> core wasm -> component -> jco transpile.
# scripts/build.sh [component-name ...] (default: all in components.txt)
# Outputs: build/components/<n>.wasm (+ <n>.meta.json), build/jco/<n>/ (JS host glue).
# Components build independently: rebuilding one never touches another's output.
set -euo pipefail
ROOT="$(cd "$(dirname "$0")/.." && pwd)"; cd "$ROOT"
export PATH="$ROOT/.tools/bin:$HOME/.moon/bin:$PATH"
for t in moon wasm-tools; do
command -v "$t" >/dev/null || { echo "error: $t not found (run scripts/setup-toolchain.sh; install MoonBit)" >&2; exit 1; }
done
JCO="$ROOT/host/node_modules/.bin/jco"
[ -x "$JCO" ] || { echo "error: jco missing; run: (cd host && npm ci)" >&2; exit 1; }
want=("$@")
mkdir -p build/components build/jco
wasm-tools component wit wit >/dev/null # the WIT package itself must parse
echo ">> moon build (wasm, release)"
moon build --target wasm --release 2>&1 | grep -v -E 'Warning|^\s*$|[│╭╰─]' || true
[ "${PIPESTATUS[0]}" -eq 0 ] || { echo "error: moon build failed" >&2; exit 1; }
while read -r name world; do
case "$name" in ''|'#'*) continue;; esac
if [ ${#want[@]} -gt 0 ] && [[ ! " ${want[*]} " =~ " $name " ]]; then continue; fi
core="_build/wasm/release/build/components/$name/gen/gen.wasm"
[ -f "$core" ] || { echo "error: $core missing" >&2; exit 1; }
out="build/components/$name.wasm"
wasm-tools component embed wit "$core" --world "$world" --encoding utf16 -o "build/components/$name.embed.wasm"
wasm-tools component new "build/components/$name.embed.wasm" -o "$out"
rm -f "build/components/$name.embed.wasm"
wasm-tools validate --features component-model "$out"
# Conformance: the component must be a valid implementation of its world.
wasm-tools component targets --world "$world" wit "$out" >/dev/null
# Capability manifest: which host interfaces the binary imports.
imports="$(wasm-tools component wit "$out" | sed -n 's/^ import moonplasma:desktop\/\([a-z-]*\)@.*;/\1/p' | grep -v '^types$' | sort | paste -sd, - || true)"
exports="$(wasm-tools component wit "$out" | sed -n 's/^ export moonplasma:desktop\/\([a-z-]*\)@.*;/\1/p' | sort | paste -sd, -)"
printf '{"name":"%s","world":"%s","imports":[%s],"exports":[%s]}\n' "$name" "$world" \
"$(echo "$imports" | sed 's/\([a-z-]\+\)/"\1"/g')" "$(echo "$exports" | sed 's/\([a-z-]\+\)/"\1"/g')" \
> "build/components/$name.meta.json"
rm -rf "build/jco/$name"
"$JCO" transpile "$out" -o "build/jco/$name" --instantiation async --no-typescript --name "$name" >/dev/null
echo "{\"type\":\"module\"}" > "build/jco/$name/package.json"
printf ' %-14s %-16s %7s bytes imports=[%s]\n' "$name" "$world" "$(stat -c %s "$out")" "$imports"
done < scripts/components.txt
echo "ok"
+5
View File
@@ -0,0 +1,5 @@
# <component dir name> <WIT world>
clock-digital clock-widget
clock-binary clock-widget
launcher launcher-widget
shell shell
+47
View File
@@ -0,0 +1,47 @@
#!/usr/bin/env bash
# (Re)generate the MoonBit guest bindings for every component from wit/.
#
# Shared import bindings land in ./interface and ./world (one copy for all
# components); each component's export bindings land in components/<n>/gen.
# The first run also writes moon.pkg for each package; later runs pass
# --ignore-stub so hand-edited moon.pkg / stub.mbt files are preserved.
#
# scripts/gen-bindings.sh regenerate in place
# scripts/gen-bindings.sh --check regenerate into a temp dir and fail if the
# committed bindings differ (order-insensitive:
# wit-bindgen 0.63 emits helper fns in varying order)
set -euo pipefail
ROOT="$(cd "$(dirname "$0")/.." && pwd)"; cd "$ROOT"
export PATH="$ROOT/.tools/bin:$PATH"
mkdir -p .tools
log="$ROOT/.tools/bindgen.log"
generate() { # <out-dir> <ignore-stub: 0|1>
local out="$1" ignore="$2" name world extra
while read -r name world; do
case "$name" in ''|'#'*) continue;; esac
extra=(); [ "$ignore" = 1 ] && extra+=(--ignore-stub)
wit-bindgen moonbit wit --world "$world" --out-dir "$out" \
--gen-dir "components/$name/gen" --project-name moonplasma/desktop \
--ignore-module-file "${extra[@]}" >/dev/null 2>"$log" || { cat "$log" >&2; exit 1; }
done < scripts/components.txt
}
if [ "${1:-}" = "--check" ]; then
tmp="$(mktemp -d)"; trap 'rm -rf "$tmp"' EXIT
generate "$tmp" 0
bad=0
while IFS= read -r f; do
rel="${f#"$tmp"/}"
case "$rel" in */moon.pkg|*/README.md) continue;; esac # hand-edited / docs
if [ ! -f "$rel" ] || ! diff -q <(sort "$f") <(sort "$rel") >/dev/null; then
echo "stale binding: $rel" >&2; bad=1
fi
done < <(find "$tmp" -type f)
[ "$bad" = 0 ] || { echo "error: bindings differ from wit/; run scripts/gen-bindings.sh and commit" >&2; exit 1; }
echo "bindings up to date"
else
first=0; [ -d components/shell/gen ] || first=1
generate . $((1 - first))
echo "bindings regenerated"
fi
+54
View File
@@ -0,0 +1,54 @@
#!/usr/bin/env node
// Drive the browser host with headless Chromium and save PNG screenshots.
// CHROME_PATH=/path/to/chrome node scripts/screenshot.mjs <outdir> [--tag name]
// Uses playwright-core (devDependency of host/) with an already-installed
// Chromium; nothing is downloaded here.
import { mkdir } from 'node:fs/promises';
import { join } from 'node:path';
import { createRequire } from 'node:module';
import { fileURLToPath } from 'node:url';
import { serve } from '../host/serve.mjs';
const require = createRequire(join(fileURLToPath(import.meta.url), '..', '..', 'host', 'package.json'));
const { chromium } = require('playwright-core');
const outdir = process.argv[2];
if (!outdir) { console.error('usage: screenshot.mjs <outdir> [--tag name]'); process.exit(2); }
const tagI = process.argv.indexOf('--tag');
const tag = tagI > 0 ? process.argv[tagI + 1] : 'desktop';
const exe = process.env.CHROME_PATH;
if (!exe) { console.error('error: set CHROME_PATH to a Chromium/Chrome executable'); process.exit(2); }
await mkdir(outdir, { recursive: true });
const srv = await serve(0);
const url = `http://127.0.0.1:${srv.address().port}/host/web/index.html`;
const browser = await chromium.launch({ executablePath: exe, args: ['--no-sandbox'] });
const page = await browser.newPage({ viewport: { width: 1280, height: 748 } });
const errors = [];
page.on('pageerror', (e) => errors.push(String(e)));
page.on('console', (m) => { if (m.type() === 'error') errors.push(m.text()); });
const T = 'time=2026-11-24T14:07:37';
const shot = async (name, query, steps) => {
await page.goto(`${url}?${T}&${query}`);
await page.waitForFunction(() => window.moonPlasma?.ready || window.moonPlasmaError, null, { timeout: 30000 });
const err = await page.evaluate(() => window.moonPlasmaError);
if (err) throw new Error(`host failed in browser: ${err}`);
if (steps) await steps();
const f = join(outdir, `${tag}-${name}.png`);
await page.locator('#screen').screenshot({ path: f });
console.log(f);
};
const canvasPoint = async (x, y) => { const b = await page.locator('#screen').boundingBox(); return [b.x + x, b.y + y]; };
const click = async (x, y) => { const [px, py] = await canvasPoint(x, y); await page.mouse.move(px, py); await page.mouse.down(); await page.mouse.up(); };
await shot('1-desktop', 'config=desktop.json');
await shot('2-launcher-open', 'config=desktop.json', async () => {
await click(60, 700);
await page.keyboard.type('s');
const [px, py] = await canvasPoint(100, 450); await page.mouse.move(px, py);
});
await shot('3-binary-clock-swapped', 'config=desktop.json', async () => {
await page.evaluate(() => window.moonPlasma.swap('clock', 'clock-binary'));
});
await shot('4-day-theme', 'config=desktop.json&theme=moon-day', async () => { await click(60, 700); });
await browser.close(); srv.close();
if (errors.length) { console.error('browser errors:\n' + errors.join('\n')); process.exit(1); }
+34
View File
@@ -0,0 +1,34 @@
#!/usr/bin/env bash
# Install wasm-tools, wit-bindgen and wasmtime (static release binaries) into
# ./.tools/bin without root. jco is installed by `npm ci` in host/.
# MoonBit itself is installed separately (https://www.moonbitlang.com/download).
set -euo pipefail
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
# shellcheck source=versions.env
source "$ROOT/scripts/versions.env"
BIN="$ROOT/.tools/bin"
mkdir -p "$BIN"
tmp="$(mktemp -d)"; trap 'rm -rf "$tmp"' EXIT
have() { [ -x "$BIN/$1" ] && "$BIN/$1" --version 2>/dev/null | grep -q "$2"; }
arch="$(uname -m)"
[ "$arch" = "x86_64" ] || { echo "error: only x86_64 linux is wired up in setup-toolchain.sh (got $arch)" >&2; exit 1; }
if ! have wasm-tools "$WASM_TOOLS_VERSION"; then
echo ">> wasm-tools $WASM_TOOLS_VERSION"
curl -fsSL "https://github.com/bytecodealliance/wasm-tools/releases/download/v$WASM_TOOLS_VERSION/wasm-tools-$WASM_TOOLS_VERSION-x86_64-linux.tar.gz" | tar xz -C "$tmp"
cp "$tmp"/wasm-tools-*/wasm-tools "$BIN/"
fi
if ! have wit-bindgen "$WIT_BINDGEN_VERSION"; then
echo ">> wit-bindgen $WIT_BINDGEN_VERSION"
curl -fsSL "https://github.com/bytecodealliance/wit-bindgen/releases/download/v$WIT_BINDGEN_VERSION/wit-bindgen-$WIT_BINDGEN_VERSION-x86_64-linux.tar.gz" | tar xz -C "$tmp"
cp "$tmp"/wit-bindgen-*/wit-bindgen "$BIN/"
fi
if ! have wasmtime "$WASMTIME_VERSION"; then
echo ">> wasmtime $WASMTIME_VERSION"
curl -fsSL "https://github.com/bytecodealliance/wasmtime/releases/download/v$WASMTIME_VERSION/wasmtime-v$WASMTIME_VERSION-x86_64-linux.tar.xz" | tar xJ -C "$tmp"
cp "$tmp"/wasmtime-*/wasmtime "$BIN/"
fi
echo "installed into $BIN:"
"$BIN/wasm-tools" --version; "$BIN/wit-bindgen" --version; "$BIN/wasmtime" --version
+30
View File
@@ -0,0 +1,30 @@
#!/usr/bin/env bash
# Full verification, identical to CI:
# WIT parses -> bindings are fresh -> moon check/test (pure logic) ->
# build all components -> conformance -> host integration tests.
set -euo pipefail
ROOT="$(cd "$(dirname "$0")/.." && pwd)"; cd "$ROOT"
export PATH="$ROOT/.tools/bin:$HOME/.moon/bin:$PATH"
step() { printf '\n== %s\n' "$*"; }
step "bindings are in sync with wit/"
scripts/gen-bindings.sh --check
step "moon check"
moon check
step "moon test (pure logic, wasm target)"
moon test
step "build components"
scripts/build.sh
if command -v wasmtime >/dev/null; then
step "wasmtime accepts every component (compile only; hosts supply the imports)"
for f in build/components/*.wasm; do
wasmtime compile "$f" -o "$(mktemp -u).cwasm" >/dev/null && echo " ok $(basename "$f")"
done
fi
step "host integration tests (jco + Node)"
(cd host && npm test)
+5
View File
@@ -0,0 +1,5 @@
# Pinned toolchain versions (see docs/TOOLCHAIN.md).
WASM_TOOLS_VERSION=1.263.0
WIT_BINDGEN_VERSION=0.63.0
WASMTIME_VERSION=51.0.0
JCO_VERSION=1.42.0