JavaScript: generator functions, yield and yield* in ES2015 realms

A realm made with Options::es2015 reads function* declarations and
expressions, *name() methods in object literals, yield and yield*, as
ECMA-262 11th edition sections 14.4 and 25.4 define them. A suspended
generator keeps its frame in its object; next, return and throw on
%GeneratorPrototype% push it back on the interpreter's frame stack, so
a resumption takes fuel and counts against the frame limit, and return
runs the finally blocks around the yield. Resuming a running generator
throws a TypeError. yield* passes next, throw and return on to its
iterator and closes one without throw before throwing a TypeError.
%GeneratorFunction% is reached through a generator function's
constructor. Outside generators, sloppy code still reads yield as a
name, and a realm without the option reads function* as a syntax error.

An engine that #targetengine kept from a .jsx run has no generator
built-ins, so a generator function called there throws a TypeError.

cargo xtask test262 runs the generator folders: 509 of 549 runs pass
(94 of 96 before). Docs: docs/mcp.md, docs/target-app-parity.md.
This commit is contained in:
dotCooCoo committed 2026-11-22 08:15:47 -08:00
1 parent f1f0911ddb
commit 30eb12aba3
20 files changed
+1312 -53

No files matched your search

+33
View File
@@ -205,6 +205,29 @@ pub(crate) enum Op {
ToStr,
/// Pushes the template object of `templates[i]`, the same object for each evaluation.
TemplateObject(u32),
/// Ends a generator function's call once its parameters are initialized: the frame becomes a
/// new generator object, which the call returns. The first `next` goes on after it.
GeneratorStart,
/// `value` → `received mode`: suspends the generator, giving its caller `{ value, done: false }`
/// (or, when true, the value itself, an iterator result from `yield*`'s delegate). It goes on
/// when the generator is resumed, with the value it was resumed with and how (0 `next`, 1
/// `throw`, 2 `return`).
Yield(bool),
/// `received mode` → `received`: goes on for `next`, throws the value for `throw`, and jumps
/// with it for `return`.
Resume(u32),
/// `value` → `iterator next`: GetIterator (ECMA-262 11th edition, section 7.4.1).
GetIterator,
/// `iterator next received mode` → `iterator next mode method iterator received`, ready for
/// `Call(1)`: the delegate's `next`, `throw` or `return`. With `return` and no `return` method
/// it jumps with `iterator next received`; with `throw` and no `throw` method it closes the
/// iterator and throws a `TypeError` (section 14.4.14).
DelegateCall(u32),
/// `mode result` → `result` when the iterator result is not done; when it is, jumps with
/// `mode value`. A result that is not an object is a `TypeError`.
DelegateResult(u32),
/// `mode value` → `value`; for `return`, jumps with it.
DelegateDone(u32),
}
/// The names a block declares, entered with [`Op::PushBlock`].
@@ -299,6 +322,9 @@ pub(crate) struct Code {
/// The function has default or rest parameters: its parameters start uninitialized, its
/// code initializes them and then enters its body with [`Op::BodyEnv`].
pub(crate) param_init: bool,
/// The code is a generator function's: a call makes a generator object ([`Op::GeneratorStart`]),
/// and it is not a constructor.
pub(crate) generator: bool,
}
impl Code {
@@ -381,6 +407,12 @@ fn effect(op: Op, finally_tables: &[Vec<u32>]) -> Result<Effect, String> {
Op::ArraySpread | Op::NewSpread => e(2, 1),
Op::CallSpread(_) => e(3, 1),
Op::ToStr => e(1, 1),
Op::GeneratorStart => e(0, 0),
Op::Yield(_) => e(1, 2),
Op::Resume(t) | Op::DelegateDone(t) => Effect { pops: 2, next: Some(1), jumps: vec![(t, 1)] },
Op::GetIterator => e(1, 2),
Op::DelegateCall(t) => Effect { pops: 2, next: Some(4), jumps: vec![(t, 1)] },
Op::DelegateResult(t) => Effect { pops: 2, next: Some(1), jumps: vec![(t, 2)] },
})
}
@@ -415,6 +447,7 @@ fn check_indices(op: Op, code: &Code) -> Result<(), String> {
Op::PushBlock(i) => check(i, code.scopes.len(), "scope"),
Op::TemplateObject(i) => check(i, code.templates.len(), "template"),
Op::RegExp(i) => check(i, code.regexps.len(), "regular expression"),
Op::GeneratorStart | Op::Yield(_) if !code.generator => Err("a generator instruction outside a generator function".into()),
_ => Ok(()),
}
}