Skip to content
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

Use global Iterator.prototype for down-level generators #59514

Merged
merged 2 commits into from
Aug 8, 2024

Conversation

rbuckton
Copy link
Member

@rbuckton rbuckton commented Aug 2, 2024

If you are compiling generators with --target ES5 --lib esnext the types of those generators will also include the iterator helpers methods like map, filter, etc., though our down-level generator emit will not have those methods even when running with newer editions of V8 that do support them:

// @target: es5
// @lib: esnext

const ar = [1, 2, 3, 4];
ar.values().map(i => i * 2); // [2, 4, 6, 8]

function * f() {
  yield * [1, 2, 3, 4];
}
f().map(i => i * 2); // error: 'f(...).map' is not a function

Even when not running in latest V8, iterator helpers can be polyfilled, e.g.:

if (typeof Iterator === "undefined") {
    let Iterator = function () {};
    Iterator.prototype = Object.getPrototypeOf(Object.getPrototypeOf([].values()));
    Iterator.prototype.map = ...;
    Iterator.prototype.filter = ...;
    globalThis.Iterator = Iterator;
}

To better support both scenarios, this makes a small change to our __generator helper to use the global Iterator.prototype object as the prototype for down-level generators, if it is present.

This also does the same for our __asyncGenerator helper when a global AsyncIterator.prototype object is present, even though async iterator helpers are still at Stage 2. Support for AsyncIterator.prototype seems harmless enough to add at this time but can be postponed to a later PR if necessary.

Please note that this will also require the same change in tslib.

Fixes #59513

@typescript-bot typescript-bot added Author: Team For Uncommitted Bug PR for untriaged, rejected, closed or missing bug labels Aug 2, 2024
@rbuckton
Copy link
Member Author

rbuckton commented Aug 2, 2024

@typescript-bot: pack this

@typescript-bot
Copy link
Collaborator

typescript-bot commented Aug 2, 2024

Starting jobs; this comment will be updated as builds start and complete.

Command Status Results
pack this ✅ Started ✅ Results

@typescript-bot
Copy link
Collaborator

typescript-bot commented Aug 2, 2024

Hey @rbuckton, I've packed this into an installable tgz. You can install it for testing by referencing it in your package.json like so:

{
    "devDependencies": {
        "typescript": "https://typescript.visualstudio.com/cf7ac146-d525-443c-b23c-0d58337efebc/_apis/build/builds/163106/artifacts?artifactName=tgz&fileId=8BF6932B3E902A06BC464B148F6BF64BA47E5AF53B48D26761BF34AF2611EE6D02&fileName=/typescript-5.6.0-insiders.20240802.tgz"
    }
}

and then running npm install.


There is also a playground for this build and an npm module you can use via "typescript": "npm:@typescript-deploys/pr-build@5.6.0-pr-59514-2".;

Copy link
Member

@jakebailey jakebailey left a comment

Choose a reason for hiding this comment

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

This needs a tslib update too, right?

@@ -1102,8 +1102,8 @@ const generatorHelper: UnscopedEmitHelper = {
priority: 6,
text: `
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
Copy link
Member

Choose a reason for hiding this comment

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

I assume it's not a problem if someone redefines the global Iterator, given we don't care if someone does that for other global symbols?

Copy link
Member Author

Choose a reason for hiding this comment

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

Correct

@rbuckton
Copy link
Member Author

rbuckton commented Aug 8, 2024

This needs a tslib update too, right?

Correct. The tslib PR is microsoft/tslib#267

@rbuckton rbuckton merged commit 67d0fc1 into main Aug 8, 2024
32 checks passed
@rbuckton rbuckton deleted the generator-helper-prototype branch August 8, 2024 16:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Author: Team For Uncommitted Bug PR for untriaged, rejected, closed or missing bug
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Generators compiled with --target es5 --lib esnext do not support iterator helpers
3 participants