Closed Bug 386030 Opened 17 years ago Closed 17 years ago

Array.reduce should ignore holes

Categories

(Core :: JavaScript Engine, defect)

x86
Windows XP
defect
Not set
normal

Tracking

()

VERIFIED FIXED

People

(Reporter: nanto, Assigned: mrbkap)

Details

Attachments

(2 files)

Array extra methods that are introduced in JS 1.6 ignore hole elements.  But reduce/reduceRight don't if the initial value isn't given and the hole element is the first element (reduce) or the last element (reduceRight).  To keep consitency, reduce/reduceRight should start iteration from the first non-hole element or throw "reduce of empty array with no initial value" error if no such element exists.

Actual:
js> function add(a, b) { return a + b; }
js> // Non-first hole elements are ignored.
js> a = new Array(2); a[0] = 3; a.reduce(add);
3
js> // If initial is given, the first hole is ignored.
js> a = new Array(2); a[1] = 3; a.reduce(add, 0);
3
js> a = new Array(2); a[1] = 3; a.reduce(add);
NaN
js> a = new Array(2); a[0] = 3; a.reduceRight(add);
NaN
js> uneval(new Array(2).reduce(add))
(void 0)

Expected:
js> a = new Array(2); a[1] = 3; a.reduce(add);
3
js> a = new Array(2); a[0] = 3; a.reduceRight(add);
3
js> uneval(new Array(2).reduce(add))
TypeError: reduce of empty array with no initial value
Attached patch Proposed fixSplinter Review
Assignee: general → mrbkap
Status: NEW → ASSIGNED
Attachment #270081 - Flags: review?(brendan)
This is the interesting subset of testcases, I think.
Attachment #270081 - Flags: review?(brendan) → review+
Fix checked into trunk.
Status: ASSIGNED → RESOLVED
Closed: 17 years ago
Flags: in-testsuite?
Resolution: --- → FIXED
/cvsroot/mozilla/js/tests/js1_6/Array/regress-386030.js,v  <--  regress-386030.js
initial revision: 1.1
Flags: in-testsuite? → in-testsuite+
this should have been in js1_7. I'll move it when I add reduce* object detection to skip the reduce tests on the 1.8 branch.
verified fixed 1.9.0 linux/mac*/windows
Status: RESOLVED → VERIFIED
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: