Last Comment Bug 386030 - Array.reduce should ignore holes
: Array.reduce should ignore holes
Product: Core
Classification: Components
Component: JavaScript Engine (show other bugs)
: Trunk
: x86 Windows XP
: -- normal (vote)
: ---
Assigned To: Blake Kaplan (:mrbkap)
: Jason Orendorff [:jorendorff]
Depends on:
  Show dependency treegraph
Reported: 2007-06-27 03:16 PDT by nanto_vi (TOYAMA Nao)
Modified: 2007-09-18 05:11 PDT (History)
2 users (show)
bob: in‑testsuite+
See Also:
Crash Signature:
QA Whiteboard:
Iteration: ---
Points: ---
Has Regression Range: ---
Has STR: ---

Proposed fix (1.32 KB, patch)
2007-06-27 15:37 PDT, Blake Kaplan (:mrbkap)
brendan: review+
Details | Diff | Splinter Review
Small shell-based testcases (585 bytes, text/plain)
2007-06-27 15:38 PDT, Blake Kaplan (:mrbkap)
no flags Details

Description nanto_vi (TOYAMA Nao) 2007-06-27 03:16:08 PDT
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.

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);
js> // If initial is given, the first hole is ignored.
js> a = new Array(2); a[1] = 3; a.reduce(add, 0);
js> a = new Array(2); a[1] = 3; a.reduce(add);
js> a = new Array(2); a[0] = 3; a.reduceRight(add);
js> uneval(new Array(2).reduce(add))
(void 0)

js> a = new Array(2); a[1] = 3; a.reduce(add);
js> a = new Array(2); a[0] = 3; a.reduceRight(add);
js> uneval(new Array(2).reduce(add))
TypeError: reduce of empty array with no initial value
Comment 1 Blake Kaplan (:mrbkap) 2007-06-27 15:37:08 PDT
Created attachment 270081 [details] [diff] [review]
Proposed fix
Comment 2 Blake Kaplan (:mrbkap) 2007-06-27 15:38:41 PDT
Created attachment 270082 [details]
Small shell-based testcases

This is the interesting subset of testcases, I think.
Comment 3 Blake Kaplan (:mrbkap) 2007-06-27 17:05:11 PDT
Fix checked into trunk.
Comment 5 Bob Clary [:bc:] 2007-07-12 18:14:15 PDT
/cvsroot/mozilla/js/tests/js1_6/Array/regress-386030.js,v  <--  regress-386030.js
initial revision: 1.1
Comment 6 Bob Clary [:bc:] 2007-07-12 18:27:41 PDT
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 Bob Clary [:bc:] 2007-09-18 05:11:03 PDT
verified fixed 1.9.0 linux/mac*/windows

Note You need to log in before you can comment on or make changes to this bug.