OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 // Flags: --harmony-typedarrays |
| 6 |
| 7 var typedArrayConstructors = [ |
| 8 Uint8Array, |
| 9 Int8Array, |
| 10 Uint16Array, |
| 11 Int16Array, |
| 12 Uint32Array, |
| 13 Int32Array, |
| 14 Uint8ClampedArray, |
| 15 Float32Array, |
| 16 Float64Array]; |
| 17 |
| 18 function TestTypedArrayForEach(constructor) { |
| 19 assertEquals(1, constructor.prototype.forEach.length); |
| 20 |
| 21 var a = new constructor(2); |
| 22 a[0] = 0; |
| 23 a[1] = 1; |
| 24 |
| 25 var count = 0; |
| 26 a.forEach(function (n) { count++; }); |
| 27 assertEquals(2, count); |
| 28 |
| 29 // Use specified object as this object when calling the function. |
| 30 var o = { value: 42 }; |
| 31 var result = []; |
| 32 a.forEach(function (n, index, array) { result.push(this.value); }, o); |
| 33 assertArrayEquals([42, 42], result); |
| 34 |
| 35 // Modify the original array. |
| 36 count = 0; |
| 37 a.forEach(function (n, index, array) { array[index] = n + 1; count++ }); |
| 38 assertEquals(2, count); |
| 39 assertArrayEquals([1, 2], a); |
| 40 |
| 41 // The %TypedArray%.forEach() method should not work when |
| 42 // transplanted to objects that are not typed arrays. |
| 43 a = [1, 2, 3]; |
| 44 a.forEach = constructor.prototype.forEach; |
| 45 assertThrows(function () { a.forEach(function (x) {}) }, TypeError); |
| 46 } |
| 47 |
| 48 for (i = 0; i < typedArrayConstructors.length; i++) { |
| 49 TestTypedArrayForEach(typedArrayConstructors[i]); |
| 50 } |
| 51 |
| 52 // Transplanting the method from one typed array to another |
| 53 // typed array should work normally. |
| 54 var a = new Int8Array(4); |
| 55 a.forEach = Uint32Array.prototype.forEach; |
| 56 var count = 0; |
| 57 a.forEach(function (x) { count++ }); |
| 58 assertEquals(a.length, count); |
OLD | NEW |