Skip to content

Editorial: simply context preservation logic for generators #132

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

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.

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.

3 participants