| OLD | NEW |
| 1 // Copyright 2014 the V8 project authors. All rights reserved. | 1 // Copyright 2014 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 // Flags: --harmony-arrays --allow-natives-syntax | 5 // Flags: --harmony-arrays --allow-natives-syntax |
| 6 | 6 |
| 7 var typedArrayConstructors = [ | 7 var typedArrayConstructors = [ |
| 8 Uint8Array, | 8 Uint8Array, |
| 9 Int8Array, | 9 Int8Array, |
| 10 Uint16Array, | 10 Uint16Array, |
| 11 Int16Array, | 11 Int16Array, |
| 12 Uint32Array, | 12 Uint32Array, |
| 13 Int32Array, | 13 Int32Array, |
| 14 Uint8ClampedArray, | 14 Uint8ClampedArray, |
| 15 Float32Array, | 15 Float32Array, |
| 16 Float64Array]; | 16 Float64Array]; |
| 17 | 17 |
| 18 function CheckTypedArrayIsNeutered(array) { | 18 function CheckTypedArrayIsNeutered(array) { |
| 19 assertEquals(0, array.byteLength); | 19 assertEquals(0, array.byteLength); |
| 20 assertEquals(0, array.byteOffset); | 20 assertEquals(0, array.byteOffset); |
| 21 assertEquals(0, array.length); | 21 assertEquals(0, array.length); |
| 22 } | 22 } |
| 23 | 23 |
| 24 function TestTypedArrayForEach(constructor) { | 24 function TestTypedArrayForEach(constructor) { |
| 25 assertEquals(1, constructor.prototype.forEach.length); | 25 assertEquals(1, constructor.prototype.every.length); |
| 26 | 26 |
| 27 var a = new constructor(2); | 27 var a = new constructor(3); |
| 28 a[0] = 0; | 28 a[0] = 0; |
| 29 a[1] = 1; | 29 a[1] = 1; |
| 30 a[2] = 2; |
| 30 | 31 |
| 31 var count = 0; | 32 var result = a.every(function (n) { return n < 2; }); |
| 32 a.forEach(function (n) { count++; }); | 33 assertFalse(result); |
| 33 assertEquals(2, count); | 34 |
| 35 var result = a.every(function (n) { return n > 2; }); |
| 36 assertFalse(result); |
| 37 |
| 38 var result = a.every(function (n) { return n >= 0; }); |
| 39 assertEquals(true, result); |
| 34 | 40 |
| 35 // Use specified object as this object when calling the function. | 41 // Use specified object as this object when calling the function. |
| 36 var o = { value: 42 }; | 42 var o = { value: 42 }; |
| 37 var result = []; | 43 result = a.every(function (n, index, array) { return n == index && n < this.va
lue; }, o); |
| 38 a.forEach(function (n, index, array) { result.push(this.value); }, o); | 44 assertEquals(true, result); |
| 39 assertArrayEquals([42, 42], result); | 45 |
| 46 // Early exit happens when appropriate |
| 47 count = 0; |
| 48 result = a.every(function () { count++; return false; }); |
| 49 assertEquals(1, count); |
| 50 assertFalse(result); |
| 40 | 51 |
| 41 // Modify the original array. | 52 // Modify the original array. |
| 42 count = 0; | 53 count = 0; |
| 43 a.forEach(function (n, index, array) { array[index] = n + 1; count++ }); | 54 result = a.every(function (n, index, array) { |
| 44 assertEquals(2, count); | 55 array[index] = n + 1; count++; return true; |
| 45 assertArrayEquals([1, 2], a); | 56 }); |
| 57 assertEquals(3, count); |
| 58 assertEquals(true, result); |
| 59 assertArrayEquals([1, 2, 3], a); |
| 46 | 60 |
| 47 // Check that values passed as second argument are wrapped into | 61 // Check that values passed as second argument are wrapped into |
| 48 // objects when calling into sloppy mode functions. | 62 // objects when calling into sloppy mode functions. |
| 49 function CheckWrapping(value, wrapper) { | 63 function CheckWrapping(value, wrapper) { |
| 50 var wrappedValue = new wrapper(value); | 64 var wrappedValue = new wrapper(value); |
| 51 | 65 |
| 52 a.forEach(function () { | 66 a.every(function () { |
| 53 assertEquals("object", typeof this); | 67 assertEquals("object", typeof this); |
| 54 assertEquals(wrappedValue, this); | 68 assertEquals(wrappedValue, this); |
| 55 }, value); | 69 }, value); |
| 56 | 70 |
| 57 a.forEach(function () { | 71 a.every(function () { |
| 58 "use strict"; | 72 "use strict"; |
| 59 assertEquals(typeof value, typeof this); | 73 assertEquals(typeof value, typeof this); |
| 60 assertEquals(value, this); | 74 assertEquals(value, this); |
| 61 }, value); | 75 }, value); |
| 62 } | 76 } |
| 63 CheckWrapping(true, Boolean); | 77 CheckWrapping(true, Boolean); |
| 64 CheckWrapping(false, Boolean); | 78 CheckWrapping(false, Boolean); |
| 65 CheckWrapping("xxx", String); | 79 CheckWrapping("xxx", String); |
| 66 CheckWrapping(42, Number); | 80 CheckWrapping(42, Number); |
| 67 CheckWrapping(3.14, Number); | 81 CheckWrapping(3.14, Number); |
| 68 CheckWrapping({}, Object); | 82 CheckWrapping({}, Object); |
| 69 | 83 |
| 70 // Throw before completing iteration, only the first element | |
| 71 // should be modified when thorwing mid-way. | |
| 72 count = 0; | |
| 73 a[0] = 42; | |
| 74 a[1] = 42; | |
| 75 try { | |
| 76 a.forEach(function (n, index, array) { | |
| 77 if (count > 0) throw "meh"; | |
| 78 array[index] = n + 1; | |
| 79 count++; | |
| 80 }); | |
| 81 } catch (e) { | |
| 82 } | |
| 83 assertEquals(1, count); | |
| 84 assertEquals(43, a[0]); | |
| 85 assertEquals(42, a[1]); | |
| 86 | |
| 87 // Neutering the buffer backing the typed array mid-way should | 84 // Neutering the buffer backing the typed array mid-way should |
| 88 // still make .forEach() finish, and the array should keep being | 85 // still make .forEach() finish, and the array should keep being |
| 89 // empty after neutering it. | 86 // empty after neutering it. |
| 90 count = 0; | 87 count = 0; |
| 91 a.forEach(function (n, index, array) { | 88 a = new constructor(2); |
| 89 result = a.every(function (n, index, array) { |
| 92 if (count > 0) %ArrayBufferNeuter(array.buffer); | 90 if (count > 0) %ArrayBufferNeuter(array.buffer); |
| 93 array[index] = n + 1; | 91 array[index] = n + 1; |
| 94 count++; | 92 count++; |
| 93 return count > 1 ? array[index] === undefined : true; |
| 95 }); | 94 }); |
| 96 assertEquals(2, count); | 95 assertEquals(2, count); |
| 96 assertEquals(true, result); |
| 97 CheckTypedArrayIsNeutered(a); | 97 CheckTypedArrayIsNeutered(a); |
| 98 assertEquals(undefined, a[0]); | 98 assertEquals(undefined, a[0]); |
| 99 | 99 |
| 100 // The method must work for typed arrays created from ArrayBuffer. | 100 // The method must work for typed arrays created from ArrayBuffer. |
| 101 // The length of the ArrayBuffer is chosen so it is a multiple of | 101 // The length of the ArrayBuffer is chosen so it is a multiple of |
| 102 // all lengths of the typed array items. | 102 // all lengths of the typed array items. |
| 103 a = new constructor(new ArrayBuffer(64)); | 103 a = new constructor(new ArrayBuffer(64)); |
| 104 count = 0; | 104 count = 0; |
| 105 a.forEach(function (n) { count++ }); | 105 result = a.every(function (n) { return n == 0; }); |
| 106 assertEquals(a.length, count); | 106 assertEquals(result, true); |
| 107 | 107 |
| 108 // Externalizing the array mid-way accessing the .buffer property | 108 // Externalizing the array mid-way accessing the .buffer property |
| 109 // should work. | 109 // should work. |
| 110 a = new constructor(2); | 110 a = new constructor(2); |
| 111 count = 0; | 111 count = 0; |
| 112 var buffer = undefined; | 112 var buffer = undefined; |
| 113 a.forEach(function (n, index, array) { | 113 a.every(function (n, index, array) { |
| 114 if (count++ > 0) | 114 if (count++ > 0) |
| 115 buffer = array.buffer; | 115 buffer = array.buffer; |
| 116 return true; |
| 116 }); | 117 }); |
| 117 assertEquals(2, count); | 118 assertEquals(2, count); |
| 118 assertTrue(!!buffer); | 119 assertTrue(!!buffer); |
| 119 assertEquals("ArrayBuffer", %_ClassOf(buffer)); | 120 assertEquals("ArrayBuffer", %_ClassOf(buffer)); |
| 120 assertSame(buffer, a.buffer); | 121 assertSame(buffer, a.buffer); |
| 121 | 122 |
| 122 // The %TypedArray%.forEach() method should not work when | 123 // The %TypedArray%.every() method should not work when |
| 123 // transplanted to objects that are not typed arrays. | 124 // transplanted to objects that are not typed arrays. |
| 124 assertThrows(function () { constructor.prototype.forEach.call([1, 2, 3], funct
ion (x) {}) }, TypeError); | 125 assertThrows(function () { constructor.prototype.every.call([1, 2, 3], functio
n (x) {}) }, TypeError); |
| 125 assertThrows(function () { constructor.prototype.forEach.call("abc", function
(x) {}) }, TypeError); | 126 assertThrows(function () { constructor.prototype.every.call("abc", function (x
) {}) }, TypeError); |
| 126 assertThrows(function () { constructor.prototype.forEach.call({}, function (x)
{}) }, TypeError); | 127 assertThrows(function () { constructor.prototype.every.call({}, function (x) {
}) }, TypeError); |
| 127 assertThrows(function () { constructor.prototype.forEach.call(0, function (x)
{}) }, TypeError); | 128 assertThrows(function () { constructor.prototype.every.call(0, function (x) {}
) }, TypeError); |
| 128 | 129 |
| 129 // Method must be useable on instances of other typed arrays. | 130 // Method must be useable on instances of other typed arrays. |
| 130 for (var i = 0; i < typedArrayConstructors.length; i++) { | 131 for (var i = 0; i < typedArrayConstructors.length; i++) { |
| 131 count = 0; | 132 count = 0; |
| 132 a = new typedArrayConstructors[i](4); | 133 a = new typedArrayConstructors[i](4); |
| 133 constructor.prototype.forEach.call(a, function (x) { count++ }); | 134 constructor.prototype.every.call(a, function (x) { count++; return true; }); |
| 134 assertEquals(a.length, count); | 135 assertEquals(a.length, count); |
| 135 } | 136 } |
| 136 } | 137 } |
| 137 | 138 |
| 138 for (i = 0; i < typedArrayConstructors.length; i++) { | 139 for (i = 0; i < typedArrayConstructors.length; i++) { |
| 139 TestTypedArrayForEach(typedArrayConstructors[i]); | 140 TestTypedArrayForEach(typedArrayConstructors[i]); |
| 140 } | 141 } |
| OLD | NEW |