Closed
Bug 1242043
Opened 9 years ago
Closed 9 years ago
{Array,%TypedArray%}.prototype.{i,lastI}ndexOf should never return -0
Categories
(Core :: JavaScript Engine, defect)
Core
JavaScript Engine
Tracking
()
RESOLVED
FIXED
mozilla47
Tracking | Status | |
---|---|---|
firefox47 | --- | fixed |
People
(Reporter: jorendorff, Assigned: Waldo)
References
Details
(Keywords: dev-doc-complete)
Attachments
(1 file)
4.28 KB,
patch
|
jorendorff
:
review+
|
Details | Diff | Splinter Review |
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.
Assignee | ||
Comment 1•9 years ago
|
||
Simple testcase:
assertEq([0].lastIndexOf(0, -0), +0);
Assignee | ||
Comment 2•9 years ago
|
||
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 | ||
Updated•9 years ago
|
Assignee: nobody → jwalden+bmo
Status: NEW → ASSIGNED
Updated•9 years ago
|
Keywords: dev-doc-needed
Reporter | ||
Comment 3•9 years ago
|
||
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+
Comment 5•9 years ago
|
||
bugherder |
Status: ASSIGNED → RESOLVED
Closed: 9 years ago
status-firefox47:
--- → fixed
Resolution: --- → FIXED
Target Milestone: --- → mozilla47
Comment 6•9 years ago
|
||
bugherder |
Comment 7•9 years ago
|
||
Mentioned in Firefox 47 for devs:
https://developer.mozilla.org/en-US/Firefox/Releases/47#JavaScript
Small compat note on the reference pages:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/lastIndexOf
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/indexOf
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/lastIndexOf
Keywords: dev-doc-needed → dev-doc-complete
Summary: Array.prototype.lastIndexOf should not return -0 → {Array,%TypedArray%}.prototype.{i,lastI}ndexOf should never return -0
You need to log in
before you can comment on or make changes to this bug.
Description
•