Skip to content

Conversation

nicolo-ribaudo
Copy link
Member

@nicolo-ribaudo nicolo-ribaudo commented Jul 31, 2025

This PR is editorial-only, and changes nothing observable about how the proposal handles generators.

Rather than storing the initial generator context in an internal slot in the generator itself, with this PR we snapshot/restore across yield and yield* operations (for yield*, restoring it after each call to the inner iterator). This has a couple advantages:

  • the spec diff is generally smaller (note: a large part of the diff is in package-lock.json because I had to update ecmarkup. The actual diff is +104 -278).
  • it matches the mental model that many of us have in the generator-related discussion, where we say that the context "is preserved across yield"
  • it will make it simpler to write the spec for https://github.com/tc39/proposal-async-context-disposable, since that proposal will actually need to save/restore across yield rather than using the initial generator context

@andreubotella since you originally wrote this part of the proposal spec, what do you think about this editorial change?

@andreubotella
Copy link
Member

Would it make sense to change the Yield() spec operation instead (or perhaps both GeneratorYield() and AsyncGeneratorYield()), since there are spec algorithms that call it? For Await(), its behavior is defined in terms of PerformPromiseThen, which already handles the context propagation.

@nicolo-ribaudo
Copy link
Member Author

Both GeneratorYield and AsyncGeneratorYield are currently used in many more places for internal spec algorithms, where we do not actually want to save/restore, like String.prototype[Symbol.iterator]().next

@nicolo-ribaudo
Copy link
Member Author

I opened tc39/ecma262#3663. If we go with the second proposed approach in that issue, than we can move some of the handling inside the Yield AO.

Copy link
Member

@jridgewell jridgewell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice. Now for using we just need to update AsyncGeneratorYield/GeneratorYield to restore the previous binding.

@legendecas legendecas merged commit 9a04bcd into tc39:master Aug 19, 2025
5 checks passed
@nicolo-ribaudo nicolo-ribaudo deleted the generator-refactor branch August 20, 2025 06:54
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.

4 participants