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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
70 changes: 70 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
name: test

on:
push:
branches:
- master
- main
pull_request:

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6
- uses: erlef/setup-beam@v1
with:
otp-version: "29"
gleam-version: "1.16.0"
rebar3-version: "3"
# elixir-version: "1"

# --- WASM spec-suite toolchain --------------------------------------------
# The conformance runner (test/scribbler/conformance) drives the official
# WebAssembly spec-suite through the compiler. The full fixture set is
# gitignored and regenerated from a PINNED testsuite revision by vendor.sh,
# which needs wabt (wat2wasm / wast2json / spectest-interp). Versions live in
# test/scribbler/conformance/vendor/PIN so this stays the single source of truth.
- name: Read pinned toolchain versions
id: pin
run: |
PIN=test/scribbler/conformance/vendor/PIN
echo "wabt=$(sed -n 's/^WABT_VERSION=//p' "$PIN")" >> "$GITHUB_OUTPUT"
echo "sha=$(sed -n 's/^TESTSUITE_SHA=//p' "$PIN")" >> "$GITHUB_OUTPUT"

- name: Install wabt (pinned)
run: |
v="${{ steps.pin.outputs.wabt }}"
url="https://github.com/WebAssembly/wabt/releases/download/${v}/wabt-${v}-linux-x64.tar.gz"
curl -fsSL "$url" -o /tmp/wabt.tar.gz
mkdir -p /tmp/wabt
tar -xzf /tmp/wabt.tar.gz -C /tmp/wabt --strip-components=1
echo "/tmp/wabt/bin" >> "$GITHUB_PATH"

- name: Verify wabt is on PATH
run: |
wat2wasm --version
command -v wast2json
command -v spectest-interp

# Cache the cloned testsuite by its pinned SHA so we don't re-clone every run.
- name: Cache vendored testsuite clone
uses: actions/cache@v4
with:
path: build/conformance-vendor
key: wasm-testsuite-${{ steps.pin.outputs.sha }}

# Resolves every dependency INCLUDING carder — no extra step is needed for it,
# because carder is a git dependency pinned by manifest.toml, which `gleam deps
# download` fetches like any other package.
- run: gleam deps download

# Regenerate the FULL allowlisted spec-suite fixtures (gitignored locally), so
# `gleam test` runs the whole conformance allowlist rather than only the small
# committed subset. The runner gate is: zero genuine FAILS (skips are expected
# and reported for constructs beyond the current frontend slice).
- name: Vendor WASM spec-suite fixtures (full allowlist)
run: bash test/scribbler/conformance/vendor/vendor.sh

- run: gleam test
- run: gleam format --check src test
42 changes: 42 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
*.beam
*.ez
/build
erl_crash.dump
.idea/

# Conformance fixtures: the full vendored spec-suite (wast2json output) is LARGE and
# regenerated on demand by test/scribbler/conformance/vendor/vendor.sh, so it is NOT
# committed. Only a small CURATED subset (negated below) is tracked so that `gleam test`
# runs in a fresh checkout. (The cloned testsuite lives under /build, already ignored.)
/test/scribbler/conformance/fixtures/*
# i64 is the flagship: assert_return + assert_trap (integer div/rem) + assert_invalid,
# all passing through our pipeline. The rest add control/locals/recursion variety.
!/test/scribbler/conformance/fixtures/i64.json
!/test/scribbler/conformance/fixtures/i64.*.wasm
!/test/scribbler/conformance/fixtures/nop.json
!/test/scribbler/conformance/fixtures/nop.*.wasm
!/test/scribbler/conformance/fixtures/int_literals.json
!/test/scribbler/conformance/fixtures/int_literals.*.wasm
!/test/scribbler/conformance/fixtures/local_get.json
!/test/scribbler/conformance/fixtures/local_get.*.wasm
!/test/scribbler/conformance/fixtures/labels.json
!/test/scribbler/conformance/fixtures/labels.*.wasm
!/test/scribbler/conformance/fixtures/fac.json
!/test/scribbler/conformance/fixtures/fac.*.wasm
# Exception-handling fixtures: the 4 convertible official EH `.wast` files
# (`wast2json --enable-exceptions`), kept in a SUBDIRECTORY so the main conformance glob
# does not see them. Committed so `eh_conformance_test` runs without a full re-vendor;
# regenerated by vendor.sh's EH section.
!/test/scribbler/conformance/fixtures/eh/
!/test/scribbler/conformance/fixtures/eh/*.json
!/test/scribbler/conformance/fixtures/eh/*.wasm
# GC fixtures: the official GC `.wast` suite converted with `wasm-tools json-from-wast`
# (wabt cannot parse GC text — issue #2530), kept in a SUBDIRECTORY so the main conformance
# glob does not see them. Committed so `gc_conformance_test` runs on a fresh checkout
# without wasm-tools; regenerate/expand with vendor.sh's GC section.
!/test/scribbler/conformance/fixtures/gc/
!/test/scribbler/conformance/fixtures/gc/*.json
!/test/scribbler/conformance/fixtures/gc/*.wasm

# macOS Finder metadata
.DS_Store
93 changes: 93 additions & 0 deletions CLAUDE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# scribbler

The WebAssembly frontend for the [carder](https://github.com/scarletindustries/carder) compiler backend — it decodes, validates and lowers wasm into carder's IR. A Gleam project (targets Erlang/BEAM by default). Built and tested with the standard Gleam toolchain — **Gleam 1.16**, Erlang/OTP, and `gleeunit` for tests.

---

## Definition of Done

A change is **not done** until all of the following hold. Treat this as a hard gate, not a checklist to skim.

### 1. Tests pass and new code is tested

- **Always run the existing tests** before and after a change: `gleam test`. A change that breaks existing tests is not done.
- **Always write tests for new code.** Every new public function or behavior gets test coverage.
- **Write objective tests against the spec, not the implementation.** Do *not* write tests that merely lock in whatever the current code happens to output (change-detector tests). Go back to the **original specification** for the behavior — for WebAssembly semantics that means the [WebAssembly spec](https://webassembly.github.io/spec/), for any other behavior the relevant RFC/standard/design doc — and assert what the spec says *should* happen. If a test and the spec disagree, the spec wins and the code is wrong.
- When a bug is found, first add a failing test that encodes the correct (spec-defined) behavior, then fix the code until it passes.

### 2. Every function is documented for the next agent

- **Always write documentation comments** so a future agent can understand the code without reading the body. Documentation is research speed for whoever comes next — invest in it.
- Document the **contract**, not a restatement of the name. For each public function describe:
- **What** it does (the intent / invariant it upholds).
- **Parameters** — meaning, units, accepted ranges, and any assumptions.
- **Return value** — including the semantics of `Result(a, e)` / `Option(a)`: what `Ok`/`Error`/`Some`/`None` each mean here.
- **Failure modes** — what inputs produce `Error`, and anything that can panic (`let assert`, `panic`, partial functions).
- Use Gleam doc comments:
- `////` — module-level docs at the top of a file (what this module is for).
- `///` — documentation for the function / type / constant that immediately follows. These render in `gleam docs` and show in editor hovers.
- `//` — ordinary inline comment (not documentation).

```gleam
/// Decodes a single LEB128-encoded unsigned integer from `bytes`.
///
/// Returns `Ok(#(value, rest))` where `rest` is the unconsumed tail, or
/// `Error(Truncated)` if the input ends mid-number. Per the WebAssembly
/// spec, values wider than 32 bits are rejected with `Error(Overflow)`.
pub fn decode_uleb128(bytes: BitArray) -> Result(#(Int, BitArray), DecodeError) {
// ...
}
```

### 3. Formatting and build are clean

- **Always run `gleam format`.** CI runs `gleam format --check src test` and **will fail the build if the code is not formatted** — so format before every commit and never push unformatted code.
- `gleam build` compiles with no warnings.

---

## Commands

| Task | Command |
|------|---------|
| Install/resolve deps | `gleam deps download` |
| Build | `gleam build` |
| Run the entry point | `gleam run` |
| Run all tests | `gleam test` |
| Format code | `gleam format` |
| Check formatting (CI) | `gleam format --check src test` |
| Generate HTML docs | `gleam docs build` |

The typical inner loop: **edit → `gleam format` → `gleam test`**.

---

## Project layout & conventions

- Entry point: `src/scribbler.gleam` (`pub fn main`).
- Add new modules under `src/scribbler/` and import them as `import scribbler/<module>` (e.g. `src/scribbler/wasm/decode.gleam` → `import scribbler/wasm/decode`).
- Tests live under `test/`, mirroring the `src/` layout. `gleeunit` auto-discovers every function whose name ends in `_test`. Run a focused module with `gleam test -- <module>`.
- This is a **Gleam** codebase — ignore any parent-directory JavaScript/Bun guidance; it does not apply here. To target JavaScript instead of Erlang, set `target = "javascript"` in `gleam.toml`.
- Prefer total functions returning `Result`/`Option` over partial functions; reserve `let assert`/`panic` for genuinely-impossible states and document them when used.

---

## Relationship to carder

scribbler is the WebAssembly **frontend** for the carder compiler **backend**. scribbler owns the wasm binary/text format — decode, validate, canonicalise, lower — and stops at carder's IR. Everything from the IR down (the middle-end/optimizer, Core Erlang codegen, the BEAM runtime, the run-ABI, the shared CLI vocabulary) belongs to carder.

- **carder is an ordinary Gleam package dependency.** It is resolved through `gleam.toml`/`manifest.toml` like any other dep. Call its public API; don't reach around it.
- **NEVER vendor or fork carder source into this repo.** No copied-in modules, no "temporary" local patches to backend code. If carder needs a change, change it in carder and bump the dependency.
- **scribbler must never define a module under `src/carder/`.** Gleam hard-errors on a duplicate module path across the dependency graph, so a stray `src/carder/anything.gleam` here breaks the build for everyone.
- **Every Erlang FFI file in this repo must be named `scribbler_*_ffi.erl`.** Unlike Gleam modules, duplicate `.erl` module atoms across two packages produce **no compiler error and no warning** — one silently shadows the other, and the failure shows up as inexplicable runtime behavior. The name prefix is the only defense.

For WebAssembly semantics the authority is the [WebAssembly spec](https://webassembly.github.io/spec/) — not carder's behavior, and not whatever this code currently does. Cite it in doc comments where a decision is spec-mandated.

---

## Commits & pull requests

- **Never Claude-brand commits or PRs.** Do **not** add `Co-Authored-By: Claude ...` trailers to commit messages, and do **not** add "Generated with Claude Code" (or any similar attribution) to PR bodies or commit messages.
- Write commit messages describing the change and its intent, as a human author would.
- **Commit frequently.** Each commit should be a single logical unit of work — small, self-contained, and independently reviewable. Prefer many focused commits over one large one, to make review easier.
- Only commit or push when explicitly asked. If on `main`, branch first.
Loading
Loading