-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Fix generator prototype chain to more correctly match ES2015 spec #264
Conversation
…w (Generator).prototype inherits from %IteratorPrototype%.
I see that CI failed for node 0.12 and 0.10, where |
…ort %IteratorPrototype% and environments that don't support resetting prototypes.
I reworked the PR to be tolerant of environments where Looks like it passes CI on all the platforms now; please let me know what you think, and thanks again! |
Thanks for reporting (and implementing) this! Since this is a runtime library, brevity and minimizing assumptions are of the essence. To that end, I think we can do without the var IteratorPrototype = {};
var getProto = Object.getPrototypeOf;
if (getProto) {
IteratorPrototype = getProto(getProto(values([])));
if (getProto(IteratorPrototype) !== Object.prototype) {
IteratorPrototype = {};
}
} Note that Can you think of any problems with that implementation? If that code works, then I think we can just do var Gp = GeneratorFunctionPrototype.prototype =
Generator.prototype = Object.create(IteratorPrototype); to avoid needing to call |
Thanks for the feedback! Clever idea to avoid I think this will basically work (I'll test it), but I'd prefer to be a little more careful checking the results of We'd also need to make sure that
I've tried this implementation, and it passes tests in node v6, v0.12, and v0.10. It's a little more wordy than your version, but I think it's a hair more defensive. I'll push it now because I have it, but please of course feel free to tell me that you want something different. Thanks! |
… a very helpful code review by @benjamn
Thanks so much for merging this! 🎉 |
Thanks so much for your work on such a cool project!
I found what I think is a spec-compliance bug with the generator prototype chain. According to section 25.3.1 (emphasis mine):
This means that
%GeneratorPrototype%
should not have aSymbol.iterator
property, but its prototype should. In addition,%GeneratorPrototype%'
s prototype should be the same as the prototype of%ArrayIteratorPrototype%
,%MapIteratorPrototype%
,%SetIteratorPrototype%
, and%StringIteratorPrototype%
.In the current code base, though,
%GeneratorPrototype%
(calledGp
in the code) is given aSymbol.iterator
property and has its prototype set toObject.prototype
. As I found out in a PR to core-js, some older browsers that support iterators evidently don't have%IteratorPrototype%
, so this behavior is reasonable in those browsers, but I think it should probably be correct in browsers that do have%IteratorPrototype%
.This PR changes the unit test that checks the prototype chain for a
Symbol.iterator
property. The current test implementation is not spec-compliant and fails in both currently shipping Chrome and Firefox.In this patch, if
%IteratorPrototype%
can't be found, it falls back to the existing behavior.Again, thanks so much for this project, and please let me know what I can do to improve this PR!