Skip to content

Phase 7.15 (script half): scripts run as the parser reaches them; document.write; proven incremental driver - #294

Closed
atiernan wants to merge 9 commits into
claude/phase7-integrationfrom
claude/html-scripts
Closed

atiernan wants to merge 9 commits into
claude/phase7-integrationfrom
claude/html-scripts

Conversation

@atiernan

@atiernan atiernan commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

Phase 7 item 7.15, HTML integration: the script half. Base: claude/phase7-integration (#286).

What landed

clazel-treebuilder: an incremental driver, proven (tier 2 stays tier 2).

  • Contract first, in its own commit: contract::dispatch_seq, incremental_new_ensures and incremental_pump_ensures.
  • exec::incremental::Incremental: the proven tokenizer and step over an input stream that grows at the insertion point. pump pauses after any step that asks for an action (ScriptReady), at the insertion point, and at the end of an input that is not closed (a document.open parser). This is Phase 4's "a driver, not a restructured spec".
  • Verus (--verify-only-module exec::incremental: 15 verified, 0 errors) proves:
    • every call meets its callee's contract;
    • the invariant (parser_inv, reachable_inv, the tokenizer's abstracts/wf, the insertion point in range) holds after every method;
    • every pump ends;
    • the parser after a pump is the parser before, given some tokens by the spec's dispatch.
  • Not proven: that those tokens are the tokenizer's over the input, so not equality with parse (with document.write there is no spec parse to equal). As evidence, all 1,761 html5lib document cases also run through the driver, and each gives parse_real's tree.
  • The scripting flag: html5lib cases that name no flag are counted with scripting off as before, and must now also give the same tree with scripting on. All 1,931 do.
  • tests/incremental.rs exercises the driver as a script host would.

clazel-js (two small API changes, both from the README's open questions).

  • Native closures can be re-entered (open question 14, as its proposal said). Scope::function/define_method now take Fn + Send + Sync, and the table holds an Arc. document.write needs this: it runs the parser, which runs a script, which calls document.write. The test that pinned Error::Busy now checks a three-deep re-entrant call.
  • Scope::run_jobs: a microtask checkpoint from inside a scope, used by "clean up after running script". Runtime::run_jobs calls it.

clazel-web::scripting (tier 0): HTML's script processing over clazel-js's public API only. Each module cites its HTML sections and steps (marked †: from memory, as phase-7.md's convention says) and lists its modelling choices.

  • element, "prepare the script element" (§4.12.1.1) acting on ScriptReady:
    • classic and module scripts, inline and external (fetched through the embedder: Page::take_requests / Page::fetched);
    • async, defer, and the pending parsing-blocking script, which blocks the tokenizer;
    • nomodule, and event/for;
    • "mark as ready" and "execute the script element", with document.currentScript;
    • "create a classic script" and "run a classic script": exceptions are reported, and a microtask checkpoint runs when the last script returns.
  • parser:
    • the "text" mode's end tag "script" steps 1 and 5 to 10 (script nesting level, parser pause flag, insertion point);
    • the blocked tokenizer and its resumption;
    • "abort a parser";
    • "the end": deferred scripts, DOMContentLoaded, and the load event's wait for async scripts.
  • write: document.write and writeln edit the input at the insertion point, and the written text is parsed before the rest (nested writes re-enter the parser). document.open and document.close include the script-created parser after load.
  • module: the HTML half of module scripts (§8.1.4, §8.1.5):
    • the module map;
    • "fetch a single module script", with its MIME type check;
    • specifier resolution, URL-like only, since import maps are Phase 8;
    • HostLoadImportedModule, "fetch the descendants of and link", "run a module script" with rejection reporting.
  • javascript_url: "evaluate a javascript: URL" in the page.
  • time: performance.now(), coarsened to 100 µs, on the embedder's monotonic clock.
  • globals: the minimum the scripts need before 7.12, added through the embedding API and to be replaced by 7.12:
    • document: write, writeln, open, close, and currentScript as a data property the page sets around each classic script;
    • performance.now;
    • one wrapper host object per node, so currentScript keeps its identity.
  • Tests (tests/scripting.rs):
    • 25 unit tests, each naming its algorithm and step;
    • html5lib scripted_*.dat: pass=2 xfail=4 fail=0 total=6. Both document.write cases pass, including the nested one. The 4 listed cases need getElementById, setAttribute, querySelector and appendChild (7.12).
    • 21 vendored WPT execution-timing/ pages, run with a stand-in for testharness.js/testlib.js (tests/support/standin-testharness.js): pass=17 xfail=4 fail=0. The 4 listed pages need eval (006), Location (028), DOM methods (067), and DOM plus setTimeout (068).

clazel-renderer: a click on a javascript: link sends no Navigate; the URL is the page's own to evaluate. A test checks this, and was checked to fail without the change.

What did not land

  • Module scripts do not run. clazel-js's parse_module, load_requested_modules, link, evaluate_module and every promise operation return NotImplemented (7.8). Every module script ends in an engine report and an error event at its element. Dynamic import() and top-level await wait on the same engine pieces; the HTML side (HostLoadImportedModule, the module map, rejection reporting) is written and compiles, but is untested end to end.

  • The renderer does not run page scripts. clazel-renderer still parses with scripting off through parse_real. Not done here:

    • hosting scripting::Page in the renderer, with FetchScript and ScriptResponse/ScriptBody/ScriptEnd mapped to take_requests/fetched;
    • the event loop replacing the one-shot pipeline;
    • deleting Phase 2's kind 1 line format.

    Until then a clicked javascript: link does nothing. This is 7.15's renderer half.

  • A typed javascript: URL is not refused by the kernel yet. Another agent is editing clazel-kernel, so the change is described instead of made:

    • A javascript: URL has an opaque origin, so lock_of_url gives it Opaque(id) and open_tab/navigate_tab commit it today.
    • The change: lock_of_url (or open_tab, navigate_tab and navigate) refuses a URL whose scheme is javascript, as an invalid tuple origin is refused today. OpenTab/NavigateTab are then ignored, and a renderer's Navigate gets error_bad_url.
    • Plus a theorem that no renderer is ever committed at a URL whose scheme is javascript.
  • javascript: URLs never replace the document. The engine's run_script returns no completion value (its README says why), so the result is never a string.

  • WPT through the real testharness.js: the engine cannot run it yet. It throws at self, and it needs try/catch with thrown errors, RegExp and Promise.

Deviations, each listed in its module

  • Events are recorded, not dispatched. load, error, DOMContentLoaded and readystatechange go into Page::take_events, since no listeners exist before 7.12. The WPT runner stands in for the load event by calling the page's onload itself.
  • Tasks the standard queues run at once, at the end of the step that queues them: the error event for a script with an empty or bad src, and an inline module script's "mark as ready". Each Page call is one task. "Spin the event loop" is a return of Progress::Waiting.
  • Parser-inserted means "prepared from the parser's ScriptReady". The tree builder does not report the script start tag's "set the parser document".
  • document.open starts a new tree and bumps a document generation, so old wrappers and prepared scripts are stale rather than aliased. It aborts a parser still active at nesting level 0 and empties its lists. The URL and history update, event-listener erasure and the iframe flags have nothing to act on.
  • The incremental driver:
    • a tokenizer step that looks ahead (markup declaration open, named character references) can read past the insertion point;
    • newline normalization applies to each write on its own;
    • the explicit EOF is at the end of the input.
  • Not modelled:
    • CSP;
    • render-blocking;
    • style sheets that block scripts;
    • integrity, referrerpolicy and fetchpriority;
    • the speculative parser;
    • "muted errors": every error is reported in full, and the kernel keeps a renderer to its own site;
    • the encoding-parsing of src with the document's encoding (UTF-8 is used);
    • the unload counter;
    • the throw-on-dynamic-markup-insertion counter;
    • XML documents.
  • change the encoding with a tentative confidence aborts the parser and returns Progress::Reparse(encoding). That is the standard's "navigate to the document again", left to the embedder.
  • Import maps (Phase 8) and speculationrules scripts do nothing. An import map gives an engine report.

Verification

cargo xtask verify --changed origin/claude/phase7-integration, run locally on commit 60e6258. Its final line was RESULT: FAILED: it did not reach CHANGED CRATES PASSED.

The selected crates were the 9 changed ones and their dependents: clazel, clazel-forms, clazel-js, clazel-layout, clazel-renderer, clazel-style, clazel-text, clazel-treebuilder and clazel-web.

What passed:

  • the static checks, rustfmt, clippy and tests;
  • tests under Miri;
  • Kani on clazel-layout and clazel-style;
  • Verus on clazel-treebuilder (1835 verified, 0 errors) and clazel-forms;
  • proof mutation tests on clazel-treebuilder and clazel-forms;
  • conformance for clazel, clazel-layout, clazel-style, clazel-treebuilder, clazel-renderer, clazel-forms and clazel-web.

The log also has a block for the engine crates, every step PASS: Kani on clazel-js-vm and clazel-js-regexp, Verus and mutations on clazel-jit and clazel-js-vm, and conformance for clazel-js-vm, clazel-js-builtins and clazel-js-regexp.

The one failure is "status table and images". Its message is: no committed row to carry over for clazel-deflate, clazel-gif, clazel-image-shape, clazel-jit, clazel-jpeg, clazel-js-builtins, clazel-png, clazel-svg and clazel-webp. These crates have no row in the committed docs/STATUS.md on the base branch, and none of them is touched here. The rule says STATUS.md is never edited by hand, so I left it; CI's table job writes those rows.

After that run, one commit (1d1492a) only changed the WPT runner's Miri stride. That test took 201 s under Miri with a stride of 7; with 11 it is expected near 130 s.

Under Miri, measured separately: html5lib_scripted took 111 s; each other scripting test took 15 to 60 s; the treebuilder suite with the incremental run added took 53 s.

CI's full verification has not run on this PR yet. Its proof jobs need a reviewer's approval of the full-verification environment. Nothing here raises a tier, adds an axiom, touches clazel-sys, STATUS.md, SPEC-REVIEWS.md or SYS-AUDITS.md, or removes a test, mutation or theorem.

🤖 Generated with Claude Code

atiernan and others added 8 commits November 23, 2026 10:46
dispatch_seq folds the spec's one-token step over a sequence of tokens;
incremental_new_ensures and incremental_pump_ensures state what the
incremental driver (exec::incremental, next commit) is to meet: the
initial parser, and a pump that leaves the parser where the spec's step
takes it over some tokens, returning for actions, at the insertion point,
or once stopped.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Scope::function and define_method take Fn + Send + Sync, and the table
holds each closure in an Arc that a call clones, so JavaScript that calls a
native function while it is running runs it again, nested, instead of
getting Error::Busy. HTML's document.write needs this: it runs the parser,
which runs a script, which calls document.write (item 7.15). The test that
pinned Busy now checks a three-deep re-entrant call.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… step's contract

exec::incremental::Incremental is Phase 4's "a driver, not a restructured
spec": the proven tokenizer and step over an input stream that grows at
the insertion point (document.write), pausing after every step that asks
for an action (ScriptReady), at the insertion point, and at the end of an
input that is not closed (a document.open parser). Verus proves that every
call meets its callee's contract, that the invariant holds after every
method, that every pump ends, and incremental_pump_ensures: the parser after
a pump is the parser before given some tokens by the spec's step. It is not
proven equal to parse; the html5lib document cases run through it and give
parse_real's tree, and cases naming no scripting flag are now checked with
scripting on as well (all 1,931 give the same tree).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
HTML's "clean up after running script" performs a microtask checkpoint when
the last script on the stack returns; a host that runs a page's scripts
from one scope (item 7.15) reaches the job queue there. Runtime::run_jobs
now calls it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
scripting::Page parses a document with the scripting flag on through
clazel-treebuilder's incremental driver and runs its scripts through
clazel-js's public API, its state in the engine's Host:

- "prepare the script element" (HTML 4.12.1.1) on ScriptReady: the type
  string, classic and module scripts, inline and external ones (fetched by
  the embedder: take_requests and fetched), async, defer, the pending
  parsing-blocking script, nomodule, event/for; mark as ready; execute the
  script element with document.currentScript; create and run a classic
  script, reporting exceptions, with a microtask checkpoint when the last
  script returns;
- the text mode's end tag script steps (script nesting level, parser pause
  flag, insertion point), the tokenizer blocked on a parsing-blocking
  script, abort a parser, and the end (deferred scripts, DOMContentLoaded,
  the load event's wait for async scripts);
- document.write, writeln, open and close, as edits of the input stream at
  the insertion point;
- module scripts' HTML half (module map, fetch a single module script,
  HostLoadImportedModule, fetch the descendants of and link, run a module
  script), which ends in the engine's NotImplemented today;
- evaluate a javascript: URL, and performance.now() coarsened to 100 us;
- the minimum globals before 7.12's bindings: document (write, writeln,
  open, close, currentScript), performance.now, a wrapper per node.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…tion-timing pages

tests/scripting.rs checks each algorithm step: inline, external, async and
defer ordering, the parsing-blocking script, document.write (nested, split
tags, from external scripts), open and close, currentScript, javascript:
URLs, module scripts reaching the engine, performance.now's coarsening,
and a meta charset's reparse. It runs the six html5lib scripted_*.dat
tree-construction cases (2 pass, 4 listed: they need 7.12's DOM methods)
and 21 vendored WPT execution-timing pages with a stand-in for
testharness.js and testlib.js, which the engine cannot run yet (17 pass,
4 listed: eval, Location, DOM methods, timers).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A click on a link whose URL is a javascript: URL sends no Navigate: the URL
is the page's to evaluate in its own renderer (item 7.15), and the kernel
never sees it. This renderer does not host scripting::Page yet, so the
click does nothing for now.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…Miri

The run took 201 s under Miri with every 7th page; two pages keep it near
130 s, well under the 300 s limit.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
atiernan added a commit that referenced this pull request Nov 23, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
atiernan added a commit that referenced this pull request Nov 23, 2026
Both sides made native closures re-entrant. One API: every closure is an
Arc<NativeFn> that is Send + Sync (#294's design); Scope::shared_function
(#295's name) stays as an alias of Scope::function, and SharedNativeFn is
an alias of NativeFn. Cargo.toml dependencies are the union.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@atiernan

Copy link
Copy Markdown
Member Author

Contained in #298 (renderer pipeline, 7.17), which merges this branch together with the other half so they verify as one. Branch kept.

@atiernan atiernan closed this Nov 23, 2026

This branch is waiting to be deployed

1 waiting deployment
full-verification — 1d1492a3 Waiting Nov 23, 2026 by atiernan via Approve the proofs #785
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