Closed Bug 1242043 Opened 5 years ago Closed 5 years ago

{Array,%TypedArray%}.prototype.{i,lastI}ndexOf should never return -0

Categories

(Core :: JavaScript Engine, defect)

defect
Not set
normal

Tracking

()

RESOLVED FIXED
mozilla47
Tracking Status
firefox47 --- fixed

People

(Reporter: jorendorff, Assigned: Waldo)

References

Details

(Keywords: dev-doc-complete)

Attachments

(1 file)

Section 5.2:

"Mathematical operations such as addition, subtraction, negation, multiplication, division, and the mathematical functions defined later in this clause should always be understood as computing exact mathematical results on mathematical real numbers, which unless otherwise noted do not include infinities and do not include a negative zero that is distinguished from positive zero."

"The mathematical function min(x1, x2, ..., xN) produces the mathematically smallest of x1 through xN."

22.1.3.15: https://tc39.github.io/ecma262/#sec-array.prototype.lastindexof

"If n ≥ 0, let k be min(n, len - 1)."

At this point, even if n is -0, k is set to 0. Our implementation could be fixed by adding

    if (k === 0)
        k = 0;

at the right point.

Pretty awful.
Simple testcase:

  assertEq([0].lastIndexOf(0, -0), +0);
And also

js> assertEq([0].indexOf(0, -0), +0);
typein:2:1 Error: Assertion failed: got -0, expected 0
Stack:
  @typein:2:1

...except, not.  That algorithm simply says, "If n >= 0, then let k be n."  That doesn't pass the number through a mathematical operation, so it never converts to +0.  Spec bug?  Seems like the two functions should be consistent in never returning -0.  And indeed, bterlson notes https://github.com/tc39/ecma262/pull/316 just now in #jslang.

Here's a patch making the methods consistent.  We could avoid adding 0 until very slightly later, for some of this stuff, but I think it makes things slightly less scrutable overall.
Attachment #8711256 - Flags: review?(jorendorff)
Assignee: nobody → jwalden+bmo
Status: NEW → ASSIGNED
Comment on attachment 8711256 [details] [diff] [review]
{Array,%TypedArray%}.prototype.{i,lastI}ndexOf should never return -0

Review of attachment 8711256 [details] [diff] [review]:
-----------------------------------------------------------------

All right!
Attachment #8711256 - Flags: review?(jorendorff) → review+
https://hg.mozilla.org/mozilla-central/rev/aa58a439e77a
Status: ASSIGNED → RESOLVED
Closed: 5 years ago
Resolution: --- → FIXED
Target Milestone: --- → mozilla47
You need to log in before you can comment on or make changes to this bug.