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

Removal of Object type-check from GetIterator() algorithm #746

Open
caitp opened this issue Dec 6, 2016 · 13 comments
Open

Removal of Object type-check from GetIterator() algorithm #746

caitp opened this issue Dec 6, 2016 · 13 comments
Labels
good first issue needs consensus This needs committee consensus before it can be eligible to be merged. needs specification text The proposal should specify ecmarkup changes that can easily merge into tc39/ecma262/spec.html.

Comments

@caitp
Copy link
Contributor

caitp commented Dec 6, 2016

The If Type(iterator) is not Object, throw a TypeError exception. step is generally not implemented by engines, see https://jsfiddle.net/gj3uaw1t/ for an example. V8 does implement this step for yield*, but not other uses of GetIterator().

It would be cool if we could remove this from the spec and have common behaviour among implementations (it seems a recent version of SpiderMonkey has added the typecheck, breaking compatibility between FF and Chrome/Safari in this very unlikely situation).

@ljharb
Copy link
Member

ljharb commented Dec 6, 2016

Wouldn't it be better for engines to implement what's in the spec? Is there a good reason it can't be implemented?

@caitp
Copy link
Contributor Author

caitp commented Dec 6, 2016

@ljharb it can be, but:

  1. the typecheck isn't really useful, since non-Objects can still have iterator methods on their prototype chain
  2. the typecheck takes up extra code (e.g. in https://codereview.chromium.org/2557593004/patch/20001/30028 7 bytes of additional bytecode to account for something that doesn't happen in practice, and even if it does happen, it's okay). It would be nice to be able to remove that gunk since it doesn't really do anything useful

@ljharb
Copy link
Member

ljharb commented Dec 6, 2016

The usefulness I see is that it effectively prevents anyone from returning a primitive as an iterator, thus discouraging patching of built in prototypes - if the user wants to patch the built in prototype, they'd have to pass Object(4) or similar to be able to use it as the iteration result.

@caitp
Copy link
Contributor Author

caitp commented Dec 6, 2016

In @domenic's words, these type checks aren't very common to other parts of the language, so maybe it's a bit weird that GetIterator() behaves this way. Since it's unlikely that anyone will create an iterator method that returns a non-undefined, non-null, non-Object, maybe it's not the right place to discourage manipulation of builtin prototypes.

I mostly just want to avoid growing bytecode, since we want to keep this stuff as small as possible, and since most implementations don't implement it as spec'd in the first place, it's not breaking anything to change this.

@littledan
Copy link
Member

There are other unexpected type checks in iteration whose motivation is hard to discern, for example if a .return() method exists, there is a type check that it returned an object. cc @GeorgNeis

@domenic
Copy link
Member

domenic commented Dec 6, 2016

In general these type checks are very surprising to me, given that we allow primitives in most of JS, e.g. by using GetV().

@littledan
Copy link
Member

IIRC @allenwb had a consistency argument. Was it something like, that we would have these object checks in other cases where we use the object, so they should be extended to cases where we don't?

@domenic
Copy link
Member

domenic commented Dec 6, 2016

I'd be interested to hear such an argument. To me, consistency cuts in favor of removing the checks, given the widespread use of GetV(), including how it is used by Invoke(). We certainly have no precedent of saying "methods must be invoked on objects, not primitives" and I'd say we in fact have the opposite precedent.

For example, 1 can serve as a thenable, if Number.prototype has been patched with a then method.

@caitp
Copy link
Contributor Author

caitp commented Dec 6, 2016

IIRC @allenwb had a consistency argument. Was it something like, that we would have these object checks in other cases where we use the object, so they should be extended to cases where we don't?

there is no point where GetIterator() is used that its return value isn't immediately used as an object (either by IteratorStep() or IteratorClose(), which both perform GetMethod() or Invoke(), which defer to GetV as said above))

@getify

This comment has been minimized.

@caitp

This comment has been minimized.

@ljharb
Copy link
Member

ljharb commented Mar 21, 2018

@caitp this seems like it should be a Needs-Consensus PR, and then we can discuss it in the meeting. Any interest in filing it?

@caitp
Copy link
Contributor Author

caitp commented Mar 21, 2018

I likely won’t have time to put that together

@littledan littledan added good first issue needs specification text The proposal should specify ecmarkup changes that can easily merge into tc39/ecma262/spec.html. labels Apr 4, 2018
@ljharb ljharb added the needs consensus This needs committee consensus before it can be eligible to be merged. label Jan 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue needs consensus This needs committee consensus before it can be eligible to be merged. needs specification text The proposal should specify ecmarkup changes that can easily merge into tc39/ecma262/spec.html.
Projects
None yet
Development

No branches or pull requests

5 participants