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

`array.indexOf(x, -0)` unnecessarily coerces its start offset to `+0` #1327

Closed
isiahmeadows opened this Issue Oct 11, 2018 · 2 comments

Comments

Projects
None yet
2 participants
@isiahmeadows

isiahmeadows commented Oct 11, 2018

In step 7.a of Array.prototype.indexOf's algorithm, it says "If n is -0, let k be +0; else let k be n.". This could be simplified to "Let k be n." just like in Array.prototype.indexOf without observable effect, since ToString(-0) yields "0", not "-0".

Also, as an aside, it might be a good idea to fuse steps 6-8 of Array.prototype.indexOf and steps 6-7 of Array.prototype.includes into a shared abstract operation that does basically this and let the case of n = len initially fall out naturally, simplifying the spec text for both operations (and the corresponding typed array operations):

CoerceOffsetWithLength(n, len):

  1. Assert: ToInteger(n) is n.
  2. Assert: ToInteger(len) is len.
  3. If nlen, return len.
  4. If n is -0, return +0.
  5. If n ≥ 0, return n.
  6. If -nlen, return n + len.
  7. Return 0.
  8. Assert: ToString(r) is an integer index, where r is the return value.
@zenparsing

This comment has been minimized.

Show comment
Hide comment
@zenparsing

zenparsing Oct 11, 2018

Contributor

Wouldn't that allow the algorithm to return -0 in 9.c?

Contributor

zenparsing commented Oct 11, 2018

Wouldn't that allow the algorithm to return -0 in 9.c?

@isiahmeadows

This comment has been minimized.

Show comment
Hide comment
@isiahmeadows

isiahmeadows Oct 11, 2018

Good point. You'd have to coerce it in that case, although you could wait until the end to do so. I updated the aside to take that into consideration.

isiahmeadows commented Oct 11, 2018

Good point. You'd have to coerce it in that case, although you could wait until the end to do so. I updated the aside to take that into consideration.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment