The default bug view has changed. See this FAQ.

Array.reduce should ignore holes

VERIFIED FIXED

Status

()

Core
JavaScript Engine
VERIFIED FIXED
10 years ago
10 years ago

People

(Reporter: nanto_vi (TOYAMA Nao), Assigned: mrbkap)

Tracking

Trunk
x86
Windows XP
Points:
---
Bug Flags:
in-testsuite +

Firefox Tracking Flags

(Not tracked)

Details

Attachments

(2 attachments)

(Reporter)

Description

10 years ago
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
(Assignee)

Comment 1

10 years ago
Created attachment 270081 [details] [diff] [review]
Proposed fix
Assignee: general → mrbkap
Status: NEW → ASSIGNED
(Assignee)

Updated

10 years ago
Attachment #270081 - Flags: review?(brendan)
(Assignee)

Comment 2

10 years ago
Created attachment 270082 [details]
Small shell-based testcases

This is the interesting subset of testcases, I think.

Updated

10 years ago
Attachment #270081 - Flags: review?(brendan) → review+
(Assignee)

Comment 3

10 years ago
Fix checked into trunk.
Status: ASSIGNED → RESOLVED
Last Resolved: 10 years ago
Flags: in-testsuite?
Resolution: --- → FIXED
I updated the MDC implementations for this.

http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Objects:Array:reduce
http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Objects:Array:reduceRight

Comment 5

10 years ago
/cvsroot/mozilla/js/tests/js1_6/Array/regress-386030.js,v  <--  regress-386030.js
initial revision: 1.1
Flags: in-testsuite? → in-testsuite+

Comment 6

10 years ago
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.

Comment 7

10 years ago
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.