codehag / documenting-invariants

Proposal to document design invariants in TC39

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Invariants: Prototype of (async) iterators

Jack-Works opened this issue · comments

Description

Iterators/generators should have %IteratorPrototype% as its prototype, or in its prototype chain before any ECMAScript code runs.

Async iterators/generators should have %AsyncIteratorPrototype% as its prototype, or in its prototype chain before any ECMAScript code runs.

Violations?

At the current point, there is nothing violating this in the 262 spec.

Rationale: Why did we have this invariant?

Make Iterator Helpers work for all built-in iterators. (initial discussion tc39/ecma262#2045 (comment))

Iterators/generators should have %IteratorPrototype% as its prototype, or in its prototype chain before any ECMAScript code runs.

  • Users are free to create iterators and set the prototype to pretty much anything they like, so this can presumably only be a constraint on "built-in" iterators (i.e., iterators created by built-in functions).

  • An iterator doesn't exist before any ECMAScript code runs, so it doesn't make sense to talk about its prototype chain before any ECMAScript code runs.

So I think a better way to express this invariant would be:

  • When a built-in function creates an iterator, it should specify its prototype to be either %IteratorPrototype% or some %FooIteratorPrototype%.

  • Every %FooIteratorPrototype% should have %IteratorPrototype% on its prototype chain when it's created.