Skip to content

feat(docs): runnable client-side Dang playground on front page - #84

Merged
vito merged 11 commits into
mainfrom
front-page-wasm-playground
Jun 4, 2026
Merged

vito merged 11 commits into
mainfrom
front-page-wasm-playground

Conversation

@vito

@vito vito commented Jun 4, 2026

Copy link
Copy Markdown
Owner

What

Makes the docs front page run Dang client-side in the browser, à la Go's
runnable examples. The marquee snippet is editable, and Run evaluates it via a
WebAssembly build of the real interpreter — no server round-trip.

How it works

  • pkg/dang now compiles to js/wasm. The only blocker was
    process_unix.go using syscall.SysProcAttr.Setpgid (absent on wasm); it's
    narrowed to !windows && !js with no-op stubs in a new process_js.go. The
    non-CGO PEG parser path was already there behind build tags.
  • cmd/dang-playground is a wasm module exposing dangEval(source) to JS.
    It runs the same ParseWithRecovery → InferFormsWithPhases → EvalNode
    pipeline as the REPL, with fresh stdlib scopes and no GraphQL/Dagger
    backend
    . Returns {ok, value, output, error, stage} (stage =
    parse/type/eval).
  • Docs: a \dang-playground{{{ … }}} Booklit macro (triple-brace verbatim,
    so braces/quotes/backticks pass through). playground.js progressively
    enhances each block into a textarea + Run button + output panel, lazily
    fetching the wasm (and wasm_exec.js) on first Run so other pages stay light.
    Without JS it degrades to a plain code block. build.sh compiles the wasm and
    stages Go's shim into docs/js/; both are gitignored build artifacts.

Verification

Ran the real wasm through Node + a DOM shim simulating a Run click:

  • [1,2,3,4].filter { x => x % 2 == 0 }.map { x => x * 10 } → => [20, 40]
  • editing to 1 + "oops" → Type error: cannot use String! as Int!
  • the shipped front-page snippet → [Hello, world!, Hello, Dang!, Hello, you!]

Native go build ./..., go vet, the js/wasm build, and the full
pkg/dang test suite all pass.

⚠️ No real browser was available in my environment, so the JS↔wasm logic is
verified headlessly but the visual layout is not — worth an eyeball in the
preview. The wasm is ~11MB uncompressed (~2-3MB gzipped), fetched only on first
Run.

Possible follow-ups

  • Syntax highlighting inside the editor (currently a plain themed textarea).
  • Pre-gzip the wasm to shrink the first-Run download.

🤖 Generated with Claude Code

vito added 3 commits June 4, 2026 16:18
Evaluating pure Dang (no GraphQL imports) needs nothing platform-specific,
but process_unix.go referenced syscall.SysProcAttr.Setpgid, which does not
exist on js/wasm and broke the build. Narrow that file to !windows && !js and
add no-op process-group stubs for js, so the package compiles for the browser
playground while leaving native service management unchanged.

Signed-off-by: Alex Suraci <[email protected]>
Add cmd/dang-playground, a js/wasm module that exposes a single dangEval(source)
function to JavaScript. It runs the same parse, type-check, and evaluate
pipeline as the REPL with fresh standard-library scopes and no GraphQL backend,
returning a result object that distinguishes parse, type, and runtime errors.
This backs the runnable code snippets on the documentation site.

Signed-off-by: Alex Suraci <[email protected]>
Add a \dang-playground{{{ ... }}} macro that turns a verbatim Dang snippet into
an editable widget. Without JavaScript it degrades to a plain code block; with
it, playground.js enhances the block into a textarea plus Run button, lazily
fetching the wasm module (and wasm_exec.js) on first run so other pages stay
light. build.sh now compiles cmd/dang-playground to docs/js and stages Go's
wasm shim; both are build artifacts and are gitignored. The front page seeds
the widget with a small type-and-map example.

Signed-off-by: Alex Suraci <[email protected]>
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Jun 4, 2026 •

Copy link
Copy Markdown

vito added 2 commits June 4, 2026 16:38
Add the assets the playground editor needs to syntax-highlight in the browser:
the web-tree-sitter runtime (tree-sitter.js, tree-sitter.wasm), the grammar
compiled to wasm from treesitter/ (tree-sitter-dang.wasm), and a copy of the
shared highlights.scm query. These are committed rather than built at deploy
time because the docs build environment only has Go, not the tree-sitter CLI,
and the first wasi-sdk download is large. regen-highlight-assets.sh rebuilds
them from the in-repo grammar when it changes, the same way src/parser.c is a
committed generated artifact.

Signed-off-by: Alex Suraci <[email protected]>
Layer a transparent textarea over a highlighted <pre> so the playground shows
colored code while staying editable. Highlighting reuses the in-repo grammar
and highlights.scm query via web-tree-sitter, mapping capture names to the same
chroma palette the static code blocks use, and re-highlights on each keystroke.
The grammar loads eagerly (~140KB gzipped) and independently of the heavy eval
module, so editing feels instant; if it fails to load the editor degrades to
plain text. Asset URLs resolve relative to playground.js so they work under any
base path.

Signed-off-by: Alex Suraci <[email protected]>
@vito

vito commented Jun 4, 2026

Copy link
Copy Markdown
Owner Author

Added syntax highlighting to the playground editor (2 new commits).

Approach — reuse the in-repo tree-sitter grammar + highlights.scm (same
source of truth as the editors), rather than a separate highlighter:

  • The editor is now a transparent <textarea> layered over a highlighted
    <pre>, re-highlighted on each keystroke. Caret alignment is exact (the
    highlighted HTML round-trips back to the source character-for-character).
  • Highlighting runs via web-tree-sitter loading tree-sitter-dang.wasm
    (built from treesitter/ with tree-sitter build --wasm) and the existing
    highlights.scm query. Captures map to the same chroma palette the static
    code blocks use.
  • It loads eagerly but is tiny (~140KB gzipped) and independent of the 11MB
    eval module, so the editor colors in immediately while Run stays lazy. If the
    grammar fails to load, the editor degrades to plain (uncolored) text.

Note on the committed assets: the 4 highlight assets are committed rather
than built at deploy time, because the docs build env has Go but (almost
certainly) not the tree-sitter CLI, and the first wasi-sdk fetch is ~114MB.
docs/regen-highlight-assets.sh rebuilds them from the grammar — same pattern
as the committed treesitter/src/parser.c. Happy to switch to build-time
generation instead if the deploy image has the tree-sitter CLI.

vito added 6 commits June 4, 2026 17:03
Instead of committing the web-tree-sitter runtime, grammar wasm, and highlight
query, build.sh now generates them on the fly via build-highlight-assets.sh:
it builds the grammar with the tree-sitter CLI, copies highlights.scm from the
editors/zed submodule, and fetches the pinned web-tree-sitter runtime with curl
(no npm). The four assets are gitignored alongside the eval wasm. Generation is
best-effort: if the tooling is unavailable the docs still build and the
playground editor falls back to plain, uncolored text.

Signed-off-by: Alex Suraci <[email protected]>
Cloudflare Pages' build image has neither the tree-sitter CLI nor initialized
submodules, so build-highlight-assets.sh now self-provisions: it downloads a
pinned prebuilt tree-sitter binary when none is on PATH, and falls back to
fetching highlights.scm from the editors/zed submodule's pinned revision on
GitHub when the submodule isn't checked out. Both paths were verified to
produce byte-identical assets to a local build.

Signed-off-by: Alex Suraci <[email protected]>
The feedback script runs before the playground script and appended a
"feedback" link to every <pre>, including the playground's fallback block. The
playground then seeded its editor from that block's textContent, so the word
"feedback" leaked into the editable source. Skip playground elements in the
feedback system, and defensively read the seed source with any injected
controls stripped.

Signed-off-by: Alex Suraci <[email protected]>
Every prebuilt tree-sitter that auto-downloads wasi-sdk for `build --wasm`
(0.26.x) is built against glibc 2.39, which is newer than the Cloudflare Pages
build image; older binaries instead require emcc/docker. So the build image
can't produce the grammar wasm at all. Commit it as a generated artifact
(like treesitter/src/parser.c) and have build.sh fetch only the query and
web-tree-sitter runtime, which are pure curl and work anywhere.
build-highlight-assets.sh gains a --runtime-only mode for that build-time
fetch; running it with no arguments still rebuilds the committed grammar wasm
when the grammar changes.

Signed-off-by: Alex Suraci <[email protected]>
It's a served asset, not an executable.

Signed-off-by: Alex Suraci <[email protected]>
The committed docs/js/tree-sitter-dang.wasm is generated from the grammar, so
it should track grammar changes like the other generated sources. hack/generate
now rebuilds it (via build-highlight-assets.sh --grammar-only) right after the
tree-sitter parser is regenerated. The script gained --grammar-only alongside
--runtime-only, and normalizes the wasm's file mode so regeneration produces no
spurious diff.

Signed-off-by: Alex Suraci <[email protected]>
@vito
vito merged commit a77a183 into main Jun 4, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant