| 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.forEach.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 assertEquals(false, result); |
| 33 assertEquals(2, count); | 34 |
| 35 var result = a.every(function (n) { return n > 2; }); |
| 36 assertEquals(false, 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 assertEquals(false, 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) { array[index] = n + 1; count++; r
eturn true; }); |
| 44 assertEquals(2, count); | 55 assertEquals(3, count); |
| 45 assertArrayEquals([1, 2], a); | 56 assertEquals(true, result); |
| 57 assertArrayEquals([1, 2, 3], a); |
| 46 | 58 |
| 47 // Check that values passed as second argument are wrapped into | 59 // Check that values passed as second argument are wrapped into |
| 48 // objects when calling into sloppy mode functions. | 60 // objects when calling into sloppy mode functions. |
| 49 function CheckWrapping(value, wrapper) { | 61 function CheckWrapping(value, wrapper) { |
| 50 var wrappedValue = new wrapper(value); | 62 var wrappedValue = new wrapper(value); |
| 51 | 63 |
| 52 a.forEach(function () { | 64 a.every(function () { |
| 53 assertEquals("object", typeof this); | 65 assertEquals("object", typeof this); |
| 54 assertEquals(wrappedValue, this); | 66 assertEquals(wrappedValue, this); |
| 55 }, value); | 67 }, value); |
| 56 | 68 |
| 57 a.forEach(function () { | 69 a.every(function () { |
| 58 "use strict"; | 70 "use strict"; |
| 59 assertEquals(typeof value, typeof this); | 71 assertEquals(typeof value, typeof this); |
| 60 assertEquals(value, this); | 72 assertEquals(value, this); |
| 61 }, value); | 73 }, value); |
| 62 } | 74 } |
| 63 CheckWrapping(true, Boolean); | 75 CheckWrapping(true, Boolean); |
| 64 CheckWrapping(false, Boolean); | 76 CheckWrapping(false, Boolean); |
| 65 CheckWrapping("xxx", String); | 77 CheckWrapping("xxx", String); |
| 66 CheckWrapping(42, Number); | 78 CheckWrapping(42, Number); |
| 67 CheckWrapping(3.14, Number); | 79 CheckWrapping(3.14, Number); |
| 68 CheckWrapping({}, Object); | 80 CheckWrapping({}, Object); |
| 69 | 81 |
| 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 | 82 // Neutering the buffer backing the typed array mid-way should |
| 88 // still make .forEach() finish, and the array should keep being | 83 // still make .forEach() finish, and the array should keep being |
| 89 // empty after neutering it. | 84 // empty after neutering it. |
| 90 count = 0; | 85 count = 0; |
| 91 a.forEach(function (n, index, array) { | 86 a = new constructor(2); |
| 87 result = a.every(function (n, index, array) { |
| 92 if (count > 0) %ArrayBufferNeuter(array.buffer); | 88 if (count > 0) %ArrayBufferNeuter(array.buffer); |
| 93 array[index] = n + 1; | 89 array[index] = n + 1; |
| 94 count++; | 90 count++; |
| 91 return count > 1 ? array[index] === undefined : true; |
| 95 }); | 92 }); |
| 96 assertEquals(2, count); | 93 assertEquals(2, count); |
| 94 assertEquals(true, result); |
| 97 CheckTypedArrayIsNeutered(a); | 95 CheckTypedArrayIsNeutered(a); |
| 98 assertEquals(undefined, a[0]); | 96 assertEquals(undefined, a[0]); |
| 99 | 97 |
| 100 // The method must work for typed arrays created from ArrayBuffer. | 98 // The method must work for typed arrays created from ArrayBuffer. |
| 101 // The length of the ArrayBuffer is chosen so it is a multiple of | 99 // The length of the ArrayBuffer is chosen so it is a multiple of |
| 102 // all lengths of the typed array items. | 100 // all lengths of the typed array items. |
| 103 a = new constructor(new ArrayBuffer(64)); | 101 a = new constructor(new ArrayBuffer(64)); |
| 104 count = 0; | 102 count = 0; |
| 105 a.forEach(function (n) { count++ }); | 103 result = a.every(function (n) { return n == 0; }); |
| 106 assertEquals(a.length, count); | 104 assertEquals(result, true); |
| 107 | 105 |
| 108 // Externalizing the array mid-way accessing the .buffer property | 106 // Externalizing the array mid-way accessing the .buffer property |
| 109 // should work. | 107 // should work. |
| 110 a = new constructor(2); | 108 a = new constructor(2); |
| 111 count = 0; | 109 count = 0; |
| 112 var buffer = undefined; | 110 var buffer = undefined; |
| 113 a.forEach(function (n, index, array) { | 111 a.every(function (n, index, array) { |
| 114 if (count++ > 0) | 112 if (count++ > 0) |
| 115 buffer = array.buffer; | 113 buffer = array.buffer; |
| 114 return true; |
| 116 }); | 115 }); |
| 117 assertEquals(2, count); | 116 assertEquals(2, count); |
| 118 assertTrue(!!buffer); | 117 assertTrue(!!buffer); |
| 119 assertEquals("ArrayBuffer", %_ClassOf(buffer)); | 118 assertEquals("ArrayBuffer", %_ClassOf(buffer)); |
| 120 assertSame(buffer, a.buffer); | 119 assertSame(buffer, a.buffer); |
| 121 | 120 |
| 122 // The %TypedArray%.forEach() method should not work when | 121 // The %TypedArray%.every() method should not work when |
| 123 // transplanted to objects that are not typed arrays. | 122 // transplanted to objects that are not typed arrays. |
| 124 assertThrows(function () { constructor.prototype.forEach.call([1, 2, 3], funct
ion (x) {}) }, TypeError); | 123 assertThrows(function () { constructor.prototype.every.call([1, 2, 3], functio
n (x) {}) }, TypeError); |
| 125 assertThrows(function () { constructor.prototype.forEach.call("abc", function
(x) {}) }, TypeError); | 124 assertThrows(function () { constructor.prototype.every.call("abc", function (x
) {}) }, TypeError); |
| 126 assertThrows(function () { constructor.prototype.forEach.call({}, function (x)
{}) }, TypeError); | 125 assertThrows(function () { constructor.prototype.every.call({}, function (x) {
}) }, TypeError); |
| 127 assertThrows(function () { constructor.prototype.forEach.call(0, function (x)
{}) }, TypeError); | 126 assertThrows(function () { constructor.prototype.every.call(0, function (x) {}
) }, TypeError); |
| 128 | 127 |
| 129 // Method must be useable on instances of other typed arrays. | 128 // Method must be useable on instances of other typed arrays. |
| 130 for (var i = 0; i < typedArrayConstructors.length; i++) { | 129 for (var i = 0; i < typedArrayConstructors.length; i++) { |
| 131 count = 0; | 130 count = 0; |
| 132 a = new typedArrayConstructors[i](4); | 131 a = new typedArrayConstructors[i](4); |
| 133 constructor.prototype.forEach.call(a, function (x) { count++ }); | 132 constructor.prototype.every.call(a, function (x) { count++; return true; }); |
| 134 assertEquals(a.length, count); | 133 assertEquals(a.length, count); |
| 135 } | 134 } |
| 136 } | 135 } |
| 137 | 136 |
| 138 for (i = 0; i < typedArrayConstructors.length; i++) { | 137 for (i = 0; i < typedArrayConstructors.length; i++) { |
| 139 TestTypedArrayForEach(typedArrayConstructors[i]); | 138 TestTypedArrayForEach(typedArrayConstructors[i]); |
| 140 } | 139 } |
| OLD | NEW |