Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,23 @@ jobs:
/usr/local/cargo/registry
/usr/local/cargo/git
wasm/target
key: ${{ runner.os }}-${{ runner.arch }}-cargo-${{ hashFiles('wasm/Cargo.toml', 'fixtures/generators/Cargo.toml', 'app/yarn.lock') }}
key: ${{ runner.os }}-${{ runner.arch }}-cargo-${{ hashFiles('wasm/Cargo.toml', 'fixtures/generators/Cargo.toml', 'yarn.lock') }}
restore-keys: ${{ runner.os }}-${{ runner.arch }}-cargo-

# Yarn Berry's global cache holds the *built* `comline-simulator` archive
# (post-`prepack`). A hit here means `yarn install` skips the Rust build
# entirely: it only re-runs when `app/yarn.lock` changes (a sim bump).
# entirely: it only re-runs when `yarn.lock` changes (a sim bump).
- name: Cache yarn
uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
with:
path: ~/.yarn/berry/cache
key: ${{ runner.os }}-${{ runner.arch }}-yarn-${{ hashFiles('app/yarn.lock') }}
key: ${{ runner.os }}-${{ runner.arch }}-yarn-${{ hashFiles('yarn.lock') }}
restore-keys: ${{ runner.os }}-${{ runner.arch }}-yarn-

# Into the kit (kit/pkg-editor), which the app and the kit's tests
# import it from.
- name: Build editor WASM
run: wasm-pack build wasm --release --target web --out-dir ../app/src/wasm --out-name comline_playground_wasm
run: wasm-pack build wasm --release --target web --out-dir ../kit/pkg-editor --out-name comline_playground_wasm

- name: Test editor crate
run: cargo test --manifest-path wasm/Cargo.toml
Expand Down Expand Up @@ -91,25 +93,25 @@ jobs:
with:
node-version: 22.23.5

# `app/.yarnrc.yml` points `yarnPath` at the committed Yarn release;
# `.yarnrc.yml` points `yarnPath` at the committed Yarn release;
# corepack provides the `yarn` command that hands over to it.
- name: Yarn
run: corepack enable

# `yarn install` pulls `comline-simulator` as a git dependency and builds
# its WASM in `prepack` (lean `pkg/` + scripted `pkg-script/`), unless the
# yarn cache above already has it. The image has cargo, wasm-bindgen and
# wasm-opt. Yarn's `npmMinimalAgeGate` (its default, one day) stays on.
# One install for the workspace (app/ and kit/). It pulls
# `comline-simulator`, the kit's git dependency, and builds its WASM in
# `prepack` (lean `pkg/` + scripted `pkg-script/`), unless the yarn cache
# above already has it. The image has cargo, wasm-bindgen and wasm-opt.
# Yarn's `npmMinimalAgeGate` (its default, one day) stays on.
- name: Install
working-directory: app
run: yarn install

- name: Test (simulate view)
working-directory: app
# The kit's tests (the simulate view, the shape), then the app's
# (examples, the WASI generator runner).
- name: Test
run: yarn test

- name: Build site
working-directory: app
run: yarn build

- if: github.event_name != 'pull_request' && github.ref == 'refs/heads/master'
Expand Down
18 changes: 9 additions & 9 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ node_modules
/app/dist

# wasm-pack output for the editor wasm (regenerated by `yarn wasm` / CI)
/app/src/wasm
/kit/pkg-editor

# Yarn Berry (app/) — keep the pinned release, drop the rest
/app/.yarn/*
!/app/.yarn/patches
!/app/.yarn/plugins
!/app/.yarn/releases
!/app/.yarn/sdks
!/app/.yarn/versions
/app/.pnp.*
# Yarn Berry (the workspace root) — keep the pinned release, drop the rest
/.yarn/*
!/.yarn/patches
!/.yarn/plugins
!/.yarn/releases
!/.yarn/sdks
!/.yarn/versions
/.pnp.*

.DS_Store
.idea/
File renamed without changes.
7 changes: 4 additions & 3 deletions app/.yarnrc.yml → .yarnrc.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
yarnPath: .yarn/releases/yarn-4.18.0.cjs
nodeLinker: node-modules

# `comline-simulator` is the sim engine — fetched from git and built to WASM in
# its `prepack`/`prepare` on install. Yarn 4 gates git deps behind this
# allow-list and disables lifecycle scripts by default; the build needs both on.
# `comline-simulator` is the sim engine (the kit's dependency) — fetched from
# git and built to WASM in its `prepack`/`prepare` on install. Yarn 4 gates git
# deps behind this allow-list and disables lifecycle scripts by default; the
# build needs both on.
approvedGitRepositories:
- "https://github.com/ComlineProject/simulator.git"
- "https://github.com/ComlineProject/examples.git"
Expand Down
57 changes: 41 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,39 +28,64 @@ wasm/ comline-playground-wasm — the editor's Rust crate, wasm-bindgen surf
semantic_tokens / hover / completions — the LSP handlers verbatim
deps: comline-core, comline-codegen, comline-codegen-rust,
comline-codegen-typescript, comline-language-server (git rev)
app/ a Vite site: a CodeMirror 6 editor (highlighting / diagnostics /
hover / autocomplete all from the editor WASM's LSP, in a Web
Worker), plus the **simulate** view — a thin canvas / inspector /
frame-log over the sim WASM. `app/src/sim/` is that view; the engine
is the `comline-simulator` git dependency (ComlineProject/simulator),
which builds itself to WASM on `npm install` — `pkg/` (lean) plus
`pkg-script/` (Rhai, code-split and fetched only when a `script`
behaviour is picked).
kit/ comline-web-kit — what the playground shares with the tutorial and
other Comline pages, as TypeScript that Vite compiles:
comline-web-kit/editor a CodeMirror 6 editor (highlighting /
diagnostics / hover / autocomplete all
from the editor WASM's LSP) and the Web
Worker that runs that WASM
comline-web-kit/sim the **simulate** view: a canvas /
inspector / frame log over the sim WASM
comline-web-kit/sim/scripted the Rhai engine's loader, passed to
`createSim` to offer the `script`
behaviour
comline-web-kit/sim.css its styles, scoped under `.cl-sim` and
themed with `--cl-*` properties
The sim engine is the `comline-simulator` git dependency
(ComlineProject/simulator), which builds itself to WASM on install
— `pkg/` (lean) plus `pkg-script/` (Rhai, code-split and fetched
only when a `script` behaviour is picked). `kit/pkg-editor/` is the
editor WASM, built by `yarn wasm` (or the kit's `prepack`).
app/ the playground site (Vite): files, tabs, problems, generated
output and examples around the kit's editor and simulate view
fixtures/generators/ a hostile WASI generator (one misbehaviour per
name) for app/src/wasi-generator.test.ts
.github/workflows/deploy.yml build editor WASM → npm install (builds the sim
WASM) → build site → deploy to Pages
.github/workflows/deploy.yml build editor WASM → yarn install (builds the sim
WASM) → test → build site → deploy to Pages
```

## Develop

```sh
cd app
npm install # also clones + builds comline-simulator to WASM (needs cargo)
npm run dev # editor WASM, then Vite
yarn install # app/ and kit/; also clones + builds comline-simulator to WASM (needs cargo)
yarn dev # editor WASM, then Vite
yarn test # the kit's tests, then the app's
```

Needs a Rust toolchain with the `wasm32-unknown-unknown` target (for the editor
WASM via [`wasm-pack`](https://rustwasm.github.io/wasm-pack/), and for
`comline-simulator`'s install-time build — `npm install` compiles
`comline-simulator`'s install-time build — `yarn install` compiles
`wasm-bindgen-cli` once, then caches it in `~/.cargo`). `wasm-opt` trims the
output when present. `COMLINE_SIMULATOR_SCRIPT=0 npm install` skips the ~2 MB
output when present. `COMLINE_SIMULATOR_SCRIPT=0 yarn install` skips the ~2 MB
scripted build.

## Using the kit elsewhere

Another site depends on the kit by git, at a commit of this repo:

```json
"comline-web-kit": "git+https://github.com/ComlineProject/playground.git#workspace=comline-web-kit&commit=<sha>"
```

Yarn clones the repo, and the kit's `prepack` builds the editor WASM. A site
that only uses `comline-web-kit/sim` sets `COMLINE_KIT_EDITOR=0` to skip that
build. Its Vite config needs `optimizeDeps.exclude: ["comline-web-kit"]`, so
the kit's `?url` WASM imports and its worker aren't pre-bundled.

## Deploy

Push to `master`. The workflow builds the editor WASM (`wasm-pack`), runs
`npm install` (which builds `comline-simulator` to WASM), builds the site with
`yarn install` (which builds `comline-simulator` to WASM), the tests, builds the site with
Vite (`base: "./"`, so it works under `/<repo>/`), and publishes `app/dist` to
Pages. Enable Pages → "GitHub Actions" in the repo settings once.

Expand Down
5 changes: 2 additions & 3 deletions app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
"private": true,
"type": "module",
"scripts": {
"wasm": "wasm-pack build ../wasm --release --target web --out-dir ../app/src/wasm --out-name comline_playground_wasm",
"dev": "yarn wasm && vite",
"dev": "vite",
"build": "tsc --noEmit && vite build",
"preview": "vite preview",
"test": "tsc -p tsconfig.test.json && node --import tsx --test \"src/**/*.test.ts\""
Expand All @@ -24,7 +23,7 @@
"@codemirror/view": "^6.34.0",
"@lezer/highlight": "^1.2.3",
"comline-examples": "git+https://github.com/ComlineProject/examples.git#f63894988ea2cc5db98ff97cc0fbdfceb5af6bb7",
"comline-simulator": "git+https://github.com/ComlineProject/simulator.git#564851a8fa00709cd94db8ce8ab1c4325a691fc4"
"comline-web-kit": "workspace:*"
},
"devDependencies": {
"@types/node": "^20.19.43",
Expand Down
6 changes: 3 additions & 3 deletions app/src/examples.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import { test } from "node:test";

import examples from "comline-examples";

const initWasm = (await import("./wasm/comline_playground_wasm.js")).default;
const { compile_project } = await import("./wasm/comline_playground_wasm.js");
const initWasm = (await import("comline-web-kit/editor-wasm")).default;
const { compile_project } = await import("comline-web-kit/editor-wasm");
await initWasm(
readFileSync(fileURLToPath(new URL("./wasm/comline_playground_wasm_bg.wasm", import.meta.url))),
readFileSync(fileURLToPath(import.meta.resolve("comline-web-kit/editor-wasm/bg.wasm"))),
);

interface Example {
Expand Down
85 changes: 20 additions & 65 deletions app/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,19 @@ import { Compartment, EditorState, type Extension } from "@codemirror/state";
import { EditorView } from "@codemirror/view";

import {
createCompiler,
makeState,
mountEditor,
readOnlyExtensions,
type EditorBridge,
type CompileProjectResult,
type EditorContext,
} from "./editor.ts";
import type {
CompileProjectResult,
CompletionItem,
FileInput,
GeneratedFile,
GenerateResult,
Hover,
LspPosition,
SemanticTokens,
} from "./worker.ts";
import { createSim, type ProjectShape, type SimView } from "./sim/index.ts";
import initSimWasm from "comline-simulator";
import simWasmUrl from "comline-simulator/pkg/comline_simulator_bg.wasm?url";
type FileInput,
type GeneratedFile,
type LspPosition,
} from "comline-web-kit/editor";
import { createSim, loadSim, type SimView } from "comline-web-kit/sim";
import { loadScripted } from "comline-web-kit/sim/scripted";
import "comline-web-kit/sim.css";
import examplesData from "comline-examples";

// ── examples ────────────────────────────────────────────────────────────
Expand Down Expand Up @@ -106,50 +100,17 @@ const langCompartment = new Compartment();
const langCache = new Map<string, Extension | null>();
const langLoading = new Set<string>();

// ── worker plumbing ─────────────────────────────────────────────────────
const worker = new Worker(new URL("./worker.ts", import.meta.url), { type: "module" });
let seq = 0;
const pending = new Map<number, (v: unknown) => void>();

worker.onmessage = (e: MessageEvent<{ id: number; result?: unknown; error?: string }>) => {
const resolve = pending.get(e.data.id);
if (!resolve) return;
pending.delete(e.data.id);
resolve(e.data.error ? { __error: e.data.error } : e.data.result);
};

function call<T>(msg: Record<string, unknown>): Promise<T> {
const id = ++seq;
return new Promise((resolve) => {
pending.set(id, resolve as (v: unknown) => void);
worker.postMessage({ id, ...msg });
});
}

async function unwrap<T>(msg: Record<string, unknown>, fallback: T): Promise<T> {
const r = await call<T | { __error: string }>(msg);
if (r && typeof r === "object" && "__error" in r) {
// ── the compile worker ──────────────────────────────────────────────────
const compiler = createCompiler({
onError: () => {
statusEl.textContent = "wasm error";
statusEl.className = "status err";
return fallback;
}
return r as T;
}

const bridge: EditorBridge = {
compileProject: (f) =>
unwrap<CompileProjectResult>({ cmd: "compileProject", files: f }, { files: [] }),
semanticTokens: (source) =>
unwrap<SemanticTokens>({ cmd: "semanticTokens", source }, { data: [] }),
hover: (files, activePath, line, character) =>
unwrap<Hover | null>({ cmd: "hover", files, activePath, line, character }, null),
completions: (source, line, character) =>
unwrap<CompletionItem[]>({ cmd: "completions", source, line, character }, []),
};
},
});

// ── editor ──────────────────────────────────────────────────────────────
const ctx: EditorContext = {
bridge,
bridge: compiler,
project,
activeName: () => activeFile()?.name ?? "",
onDocChanged: (d) => {
Expand Down Expand Up @@ -505,10 +466,7 @@ async function renderOutput() {
destroyGenCode();
viewEl.innerHTML = `<p class="muted">generating…</p>`;
}
const res = await unwrap<GenerateResult>(
{ cmd: "generateProject", files: project(), target: targetSel.value, mode },
{ files: [], error: "wasm error" },
);
const res = await compiler.generateProject(project(), targetSel.value, mode);
if (currentView !== "output") return;
if (res.error) {
genFiles = null;
Expand Down Expand Up @@ -681,7 +639,7 @@ function scheduleRefresh() {
}

async function refresh() {
lastProject = await bridge.compileProject(project());
lastProject = await compiler.compileProject(project());
let problems = 0;
let parseFail = false;
for (const f of lastProject.files) {
Expand Down Expand Up @@ -732,13 +690,10 @@ let sim: SimView | null = null;
let simLoaded = false;

async function enterSimulate() {
await initSimWasm({ module_or_path: simWasmUrl }); // idempotent
const shape = await unwrap<ProjectShape>(
{ cmd: "describeProject", files: project() },
{ schemas: [] },
);
await loadSim(); // idempotent
const shape = await compiler.describeProject(project());
if (!sim) {
sim = createSim();
sim = createSim({ loadScripted });
simEl.append(sim.el);
}
// A `#s=…` fragment restores a shared topology, but only on the first entry.
Expand Down
6 changes: 0 additions & 6 deletions app/src/sim/index.ts

This file was deleted.

Loading
Loading