Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(414)

Side by Side Diff: src/array.js

Issue 614733002: Array.prototype.{reduce, reduceRight}: Wrong order of operations when determining initial value. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | test/mjsunit/array-reduce.js » ('j') | test/mjsunit/array-reduce.js » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 "use strict"; 5 "use strict";
6 6
7 // This file relies on the fact that the following declarations have been made 7 // This file relies on the fact that the following declarations have been made
8 // in runtime.js: 8 // in runtime.js:
9 // var $Array = global.Array; 9 // var $Array = global.Array;
10 10
(...skipping 1376 matching lines...) Expand 10 before | Expand all | Expand 10 after
1387 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.reduce"); 1387 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.reduce");
1388 1388
1389 // Pull out the length so that modifications to the length in the 1389 // Pull out the length so that modifications to the length in the
1390 // loop will not affect the looping and side effects are visible. 1390 // loop will not affect the looping and side effects are visible.
1391 var array = ToObject(this); 1391 var array = ToObject(this);
1392 var length = ToUint32(array.length); 1392 var length = ToUint32(array.length);
1393 1393
1394 if (!IS_SPEC_FUNCTION(callback)) { 1394 if (!IS_SPEC_FUNCTION(callback)) {
1395 throw MakeTypeError('called_non_callable', [callback]); 1395 throw MakeTypeError('called_non_callable', [callback]);
1396 } 1396 }
1397 if (length == 0 && IS_UNDEFINED(current)) {
1398 throw MakeTypeError('reduce_no_initial', []);
1399 }
wingo 2014/10/07 11:04:31 I think the test should be %_ArgumentsLength() < 2
arv (Not doing code reviews) 2014/10/07 14:16:02 Yup. The spec says: "If initialValue is present, t
Diego Pino 2014/10/09 10:18:57 What I intended to code with that line was step 7
1397 1400
1398 var i = 0; 1401 var i = 0;
1399 find_initial: if (%_ArgumentsLength() < 2) { 1402 find_initial: if (%_ArgumentsLength() < 2) {
1400 for (; i < length; i++) { 1403 for (; i < length; i++) {
1401 current = array[i]; 1404 if (i in array) {
1402 if (!IS_UNDEFINED(current) || i in array) { 1405 current = array[i++];
1403 i++;
1404 break find_initial; 1406 break find_initial;
1405 } 1407 }
1406 } 1408 }
1407 throw MakeTypeError('reduce_no_initial', []); 1409 throw MakeTypeError('reduce_no_initial', []);
1408 } 1410 }
1409 1411
1410 var receiver = %GetDefaultReceiver(callback); 1412 var receiver = %GetDefaultReceiver(callback);
1411 var stepping = DEBUG_IS_ACTIVE && %DebugCallbackSupportsStepping(callback); 1413 var stepping = DEBUG_IS_ACTIVE && %DebugCallbackSupportsStepping(callback);
1412 for (; i < length; i++) { 1414 for (; i < length; i++) {
1413 if (i in array) { 1415 if (i in array) {
(...skipping 10 matching lines...) Expand all
1424 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.reduceRight"); 1426 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.reduceRight");
1425 1427
1426 // Pull out the length so that side effects are visible before the 1428 // Pull out the length so that side effects are visible before the
1427 // callback function is checked. 1429 // callback function is checked.
1428 var array = ToObject(this); 1430 var array = ToObject(this);
1429 var length = ToUint32(array.length); 1431 var length = ToUint32(array.length);
1430 1432
1431 if (!IS_SPEC_FUNCTION(callback)) { 1433 if (!IS_SPEC_FUNCTION(callback)) {
1432 throw MakeTypeError('called_non_callable', [callback]); 1434 throw MakeTypeError('called_non_callable', [callback]);
1433 } 1435 }
1436 if (length == 0 && IS_UNDEFINED(current)) {
1437 throw MakeTypeError('reduce_no_initial', []);
1438 }
wingo 2014/10/07 11:04:31 likewise
arv (Not doing code reviews) 2014/10/07 14:16:02 Yup. https://people.mozilla.org/~jorendorff/es6-dr
1434 1439
1435 var i = length - 1; 1440 var i = length - 1;
1436 find_initial: if (%_ArgumentsLength() < 2) { 1441 find_initial: if (%_ArgumentsLength() < 2) {
1437 for (; i >= 0; i--) { 1442 for (; i >= 0; i--) {
1438 current = array[i]; 1443 if (i in array) {
1439 if (!IS_UNDEFINED(current) || i in array) { 1444 current = array[i--];
1440 i--;
1441 break find_initial; 1445 break find_initial;
1442 } 1446 }
1443 } 1447 }
1444 throw MakeTypeError('reduce_no_initial', []); 1448 throw MakeTypeError('reduce_no_initial', []);
1445 } 1449 }
1446 1450
1447 var receiver = %GetDefaultReceiver(callback); 1451 var receiver = %GetDefaultReceiver(callback);
1448 var stepping = DEBUG_IS_ACTIVE && %DebugCallbackSupportsStepping(callback); 1452 var stepping = DEBUG_IS_ACTIVE && %DebugCallbackSupportsStepping(callback);
1449 for (; i >= 0; i--) { 1453 for (; i >= 0; i--) {
1450 if (i in array) { 1454 if (i in array) {
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
1547 )); 1551 ));
1548 1552
1549 SetUpLockedPrototype(InternalPackedArray, $Array(), $Array( 1553 SetUpLockedPrototype(InternalPackedArray, $Array(), $Array(
1550 "join", getFunction("join", ArrayJoin), 1554 "join", getFunction("join", ArrayJoin),
1551 "pop", getFunction("pop", ArrayPop), 1555 "pop", getFunction("pop", ArrayPop),
1552 "push", getFunction("push", ArrayPush) 1556 "push", getFunction("push", ArrayPush)
1553 )); 1557 ));
1554 } 1558 }
1555 1559
1556 SetUpArray(); 1560 SetUpArray();
OLDNEW
« no previous file with comments | « no previous file | test/mjsunit/array-reduce.js » ('j') | test/mjsunit/array-reduce.js » ('J')

Powered by Google App Engine
This is Rietveld 408576698