-
Notifications
You must be signed in to change notification settings - Fork 12.8k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Use global Iterator.prototype
for down-level generators
#59514
Conversation
@typescript-bot: pack this |
Hey @rbuckton, I've packed this into an installable tgz. You can install it for testing by referencing it in your
and then running There is also a playground for this build and an npm module you can use via |
There was a problem hiding this 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); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correct
Correct. The tslib PR is microsoft/tslib#267 |
If you are compiling generators with
--target ES5 --lib esnext
the types of those generators will also include the iterator helpers methods likemap
,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:Even when not running in latest V8, iterator helpers can be polyfilled, e.g.:
To better support both scenarios, this makes a small change to our
__generator
helper to use the globalIterator.prototype
object as the prototype for down-level generators, if it is present.This also does the same for our
__asyncGenerator
helper when a globalAsyncIterator.prototype
object is present, even though async iterator helpers are still at Stage 2. Support forAsyncIterator.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