Skip to content

JS engine: generators and async functions (items 7.7–7.9) - #304

Merged
atiernan merged 8 commits into
mainfrom
claude/js-generators-async
Nov 25, 2026
Merged

atiernan merged 8 commits into
mainfrom
claude/js-generators-async

Conversation

@atiernan

@atiernan atiernan commented Nov 24, 2026 •

Copy link
Copy Markdown
Member

Scope

Phase 7 items 7.7 to 7.9: generators and async functions now run. The compiler and the op list already had them (Yield, Await, GeneratorStart, YieldDelegate, the yield*/for await iteration ops); the interpreter reported them as not implemented. This PR adds the runtime half.

What works

  • Generator functions, expressions and methods (function*, yield, yield*), generator objects with next, return and throw (§27.5), including return/throw through finally blocks, suspended-start and completed states, and the "already running" TypeError.
  • Iteration over generators: for-of, spread, destructuring, yield* over any iterable (with throw/return delegation and the iterator result passed through as it is).
  • Async functions, async methods and async arrow functions (§27.7) on the existing Promise and job queue: await of values, promises and thenables, rejections thrown at the await, errors in parameters rejecting the promise, job ordering.
  • Async generators (§27.6) with the request queue (next/return/throw queued, AsyncGeneratorAwaitReturn, DrainQueue), for await (over async iterables and, via %AsyncFromSyncIteratorPrototype%, sync ones), and yield* in async generators.
  • The intrinsics: %GeneratorFunction.prototype%, %GeneratorPrototype%, %AsyncFunction.prototype%, %AsyncIteratorPrototype%, %AsyncGeneratorFunction.prototype%, %AsyncGeneratorPrototype%, %AsyncFromSyncIteratorPrototype%; generator and async functions are not constructors.

Design (clazel-js-vm/src/suspend.rs): a call of one of these functions pushes an ordinary frame of kind KIND_SUSPENDABLE. A suspension op copies the frame (header, registers, resume pc, and the two registers the op names for the resumption) into slots of the generator object (a host object, so nothing is hidden from the future collector) and leaves the frame as Return does. A resumption pushes the copy back with no caller (RET_NONE), writes the sent value and the mode, and runs the loop on it, as a native's call into JS does. The compiled code after each suspension already dispatches on the mode. Async functions settle their promise on Return and when a throw leaves the frame (in unwind). The VM has no Promise of its own: the built-ins install PromiseOps (NewPromiseCapability, PromiseResolve, PerformPromiseThen).

Verification. No op is new and the spec is unchanged: spec::fast_path already sends the suspension ops to their handlers, a Return from a frame that is not plain is not fast, and FnInfo::fast_callable excludes these functions. One lemma is added, lemma_suspendable_return_not_fast (a Return from a suspendable frame always calls its handler), which the interpreter relies on. Because there is no new op or spec function, there is no new mutation. The crate stays at tier 0.

Test262

Vendored from the pinned commit e91219cb, byte for byte (SOURCE.md updated, table regenerated; 7,533 cases with Date's directory from main): language/statements/{generators,async-function,async-generator}, language/expressions/{generators,async-function,async-arrow-function,async-generator,await,yield}, built-ins/{GeneratorPrototype,GeneratorFunction,AsyncFunction,AsyncGeneratorPrototype,AsyncGeneratorFunction,AsyncFromSyncIteratorPrototype,AsyncIteratorPrototype}.

The built-ins runner (conformance_builtins) now runs async tests (real doneprintHandle.js, then a microtask checkpoint), runs the harness files these tests include (propertyHelper.js, compareArray.js, asyncHelpers.js, isConstructor.js), and has assert.compareArray.

Whole table (7,533 tests after vendoring and the merge of main with Date, #303; main's status table had 5,212 before both):

Runner pass expected failures fail on main
clazel-js-builtins conformance_builtins (with the built-ins) 2,908 4,625 0 545 / 5,212
clazel-js-vm conformance_interp (interpreter only) 584 6,949 0 456 / 5,212

built-ins/Date in the built-ins runner after the merge: 573 pass, 21 xfail, 0 fail. Part of the built-ins runner's gain outside these directories comes from running the included harness files and async tests everywhere.

The vendored directories (built-ins runner):

Directory pass xfail fail
language/statements/generators 224 42 0
language/expressions/generators 238 52 0
language/expressions/yield 58 5 0
built-ins/GeneratorPrototype 61 0 0
built-ins/GeneratorFunction 9 14 0
language/statements/async-function 42 32 0
language/expressions/async-function 50 43 0
language/expressions/async-arrow-function 27 33 0
language/expressions/await 19 3 0
built-ins/AsyncFunction 14 4 0
language/statements/async-generator 265 36 0
language/expressions/async-generator 528 95 0
built-ins/AsyncGeneratorPrototype 48 0 0
built-ins/AsyncGeneratorFunction 9 14 0
built-ins/AsyncFromSyncIteratorPrototype 76 0 0
built-ins/AsyncIteratorPrototype 3 10 0
total 1,671 383 0

Of the 383 expected failures, 289 are negative parse/early tests (the parser's evidence, not run) and 9 are newer than the pinned edition. The other 85, by reason: with (WithHas not in the interpreter yet) 41; the GeneratorFunction/AsyncFunction/AsyncGeneratorFunction constructors (CreateDynamicFunction, as Function) 24; $262.createRealm (one realm) 7; eval missing 5; wellKnownIntrinsicObjects.js not run 4; the unmapped sloppy arguments object 2; Function.prototype's caller/arguments 1; BigInt literals 1. So 1,671 of the 1,756 tests that run pass, and none fails.

Five tests are new KNOWN_GAPS entries of the built-ins runner, each for a gap outside this change (eval, the parameter map of arguments, Function.prototype's restricted properties). The interpreter-only runner classes built-ins/ and array iteration (no Array.prototype[@@iterator] there) as needing the built-ins, as the Date branch does for built-ins/.

Checks

  • cargo xtask verify --part crate clazel-js-vm: Kani 10 of 10 harnesses, Verus 84 verified, 0 errors (83 on main, plus the new lemma), mutations 237 of 237 killed by a theorem, conformance passed: CRATE PART PASSED.
  • cargo clippy --workspace --all-targets, cargo fmt --check, cargo test --workspace --no-fail-fast (374 test targets, 0 failures) and cargo xtask check pass.
  • cargo xtask verify --changed (on the merged branch, before the test split): final line RESULT: FAILED. The only failing step was tests under Miri: every test passed, but string_match_search_split_replace took 309.7 s, over the 300 s limit. Every other step passed: Kani (clazel-js-vm 10/10, clazel-js-syntax 7/7), Verus (clazel-js-lexer 386 verified, clazel-js-syntax 482, 0 errors), and mutations (clazel-js-vm 237/237, clazel-js-lexer 28/28, clazel-js-syntax 6/6 killed by a theorem; clazel-jit 39 killed by a theorem, 5 incidentally, 0 survived).
  • That test and json_parse are now split (commit bd15df1, no case dropped). Under Miri the pieces take 101 to 191 s (json_stringify, unchanged, 201 s). verify has not been rerun since the split.
  • CI's full verification has not run yet; the proof jobs need a reviewer's approval.

Not done

  • Top-level await in modules is compiled (the module body is an async body), but clazel-js does not evaluate modules yet (Scope::evaluate_module is not implemented), so it is untested.
  • The GeneratorFunction, AsyncFunction and AsyncGeneratorFunction constructors throw "not implemented", like Function (CreateDynamicFunction needs the compiler at run time).
  • Resumption runs on the host stack (a nested run of the loop, as a native calling JS does): generators that resume each other through yield* nest one level per generator and count against the native depth limit (32 by default), past which the resumption is a RangeError.
  • test/language/statements/for-await-of (1,234 files, mostly destructuring) is not vendored. for await is covered by the async generator directories and the unit tests.
  • No new spec or mutations: no op was added and spec.rs is unchanged. The handlers stay tier 0, and Test262 is their evidence, as phase-7.md 7.4 says of handler ops.
  • The interpreter-only runner still classes async tests as needing the built-ins (it has no Promise).

🤖 Generated with Claude Code

atiernan and others added 6 commits November 24, 2026 18:48
The suspension ops the compiler already emits (GeneratorStart, Yield,
YieldDelegate, Await) and the iteration ops of yield* and for await
(GetAsyncIterator, IterCallMethod, IterComplete, IterValue) now run.
A suspension copies the frame's header, registers and resume pc into
the generator object's slots and returns as Return does; a resumption
pushes the copy back with no caller and runs the loop on it. Async
functions settle their promise on return and on an escaping throw.
The promises come from the built-ins through PromiseOps. No op is new,
and spec::fast_path already sends each of these to its handler.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
%GeneratorFunction.prototype%, %GeneratorPrototype% (next, return,
throw), %AsyncFunction.prototype%, %AsyncIteratorPrototype%,
%AsyncGeneratorFunction.prototype%, %AsyncGeneratorPrototype% and
%AsyncFromSyncIteratorPrototype%, and the Promise operations the VM's
await uses. Tests of generators, yield*, async functions, async
generators and for await.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
From the pinned commit e91219cb, byte for byte: language/statements
generators, async-function and async-generator; language/expressions
generators, async-function, async-arrow-function, async-generator,
await and yield; built-ins GeneratorPrototype, GeneratorFunction,
AsyncFunction, AsyncGeneratorPrototype, AsyncGeneratorFunction,
AsyncFromSyncIteratorPrototype and AsyncIteratorPrototype. The table
is regenerated with cargo xtask testdata (6939 cases).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The built-ins runner now runs async tests (doneprintHandle.js and a
microtask checkpoint), the harness files the generator tests include,
and assert.compareArray; it prints the counts of each generator and
async function directory. Five tests that fail for gaps outside this
change (eval, the unmapped arguments object, Function.prototype's
restricted properties) are listed as known gaps. The interpreter-only
runner classes built-ins/ and array iteration as needing the built-ins.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
lemma_suspendable_return_not_fast: in a frame of kind suspendable,
Return always calls its handler, which is where the interpreter
completes the generator or settles the async function's promise.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@atiernan
atiernan temporarily deployed to full-verification November 24, 2026 20:16 — with GitHub Actions Inactive
Both sides' intrinsics are kept (38), with Date's World hook. SOURCE.md
lists both vendored subsets; the Test262 table is regenerated (7,533
cases).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@atiernan
atiernan temporarily deployed to full-verification November 24, 2026 22:36 — with GitHub Actions Inactive
string_match_search_split_replace becomes match/matchAll, search/split
and replace/replaceAll; json_parse moves its nesting and reviver cases
into a test of their own. Every case is kept; each piece stays under
the Miri time limit as the Runtime's setup grows.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@atiernan
atiernan temporarily deployed to full-verification November 25, 2026 01:39 — with GitHub Actions Inactive
@atiernan
atiernan added this pull request to the merge queue Nov 25, 2026
Merged via the queue into main with commit 2cc5836 Nov 25, 2026
25 checks passed
atiernan added a commit that referenced this pull request Nov 25, 2026
…s-input (#300)

Conflicts in clazel-js-vm's interp.rs and clazel-js's runtime.rs: the
lazy accessor maker and main's world hook are both kept.

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

This branch was previously deployed

1 inactive deployment
full-verification — bd15df16 Deployed Nov 25, 2026 by atiernan via Approve the proofs #824
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