Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 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: --allow-natives-syntax |
| 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 CheckTypedArrayIsNeutered(array) { |
| 19 | assertEquals(0, array.byteLength); |
| 20 | assertEquals(0, array.byteOffset); |
| 21 | assertEquals(0, array.length); |
| 22 | } |
| 23 | |
| 24 | function TestTypedArrayForEach(constructor) { |
| 25 | assertEquals(1, constructor.prototype.every.length); |
| 26 | |
| 27 | var a = new constructor(3); |
| 28 | a[0] = 0; |
| 29 | a[1] = 1; |
| 30 | a[2] = 2; |
| 31 | |
| 32 | var result = a.every(function (n) { return n < 2; }); |
| 33 | assertFalse(result); |
| 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); |
| 40 | |
| 41 | // Use specified object as this object when calling the function. |
| 42 | var o = { value: 42 }; |
| 43 | result = a.every(function (n, index, array) { return n == index && n < this.value; }, o); |
| 44 | assertEquals(true, 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); |
| 51 | |
| 52 | // Modify the original array. |
| 53 | count = 0; |
| 54 | result = a.every(function (n, index, array) { |
| 55 | array[index] = n + 1; count++; return true; |
| 56 | }); |
| 57 | assertEquals(3, count); |
| 58 | assertEquals(true, result); |
| 59 | assertArrayEquals([1, 2, 3], a); |
| 60 | |
| 61 | // Check that values passed as second argument are wrapped into |
| 62 | // objects when calling into sloppy mode functions. |
| 63 | function CheckWrapping(value, wrapper) { |
| 64 | var wrappedValue = new wrapper(value); |
| 65 | |
| 66 | a.every(function () { |
| 67 | assertEquals("object", typeof this); |
| 68 | assertEquals(wrappedValue, this); |
| 69 | }, value); |
| 70 | |
| 71 | a.every(function () { |
| 72 | "use strict"; |
| 73 | assertEquals(typeof value, typeof this); |
| 74 | assertEquals(value, this); |
| 75 | }, value); |
| 76 | } |
| 77 | CheckWrapping(true, Boolean); |
| 78 | CheckWrapping(false, Boolean); |
| 79 | CheckWrapping("xxx", String); |
| 80 | CheckWrapping(42, Number); |
| 81 | CheckWrapping(3.14, Number); |
| 82 | CheckWrapping({}, Object); |
| 83 | |
| 84 | // Neutering the buffer backing the typed array mid-way should |
| 85 | // still make .forEach() finish, and the array should keep being |
| 86 | // empty after neutering it. |
| 87 | count = 0; |
| 88 | a = new constructor(3); |
| 89 | result = a.every(function (n, index, array) { |
| 90 | assertFalse(array[index] === undefined); // don't get here if neutered |
| 91 | if (count > 0) %ArrayBufferNeuter(array.buffer); |
| 92 | array[index] = n + 1; |
| 93 | count++; |
| 94 | return count > 1 ? array[index] === undefined : true; |
| 95 | }); |
| 96 | assertEquals(2, count); |
| 97 | assertEquals(true, result); |
| 98 | CheckTypedArrayIsNeutered(a); |
| 99 | assertEquals(undefined, a[0]); |
| 100 | |
| 101 | // The method must work for typed arrays created from ArrayBuffer. |
| 102 | // The length of the ArrayBuffer is chosen so it is a multiple of |
| 103 | // all lengths of the typed array items. |
| 104 | a = new constructor(new ArrayBuffer(64)); |
| 105 | count = 0; |
| 106 | result = a.every(function (n) { return n == 0; }); |
| 107 | assertEquals(result, true); |
| 108 | |
| 109 | // Externalizing the array mid-way accessing the .buffer property |
| 110 | // should work. |
| 111 | a = new constructor(2); |
| 112 | count = 0; |
| 113 | var buffer = undefined; |
| 114 | a.every(function (n, index, array) { |
| 115 | if (count++ > 0) |
| 116 | buffer = array.buffer; |
| 117 | return true; |
| 118 | }); |
| 119 | assertEquals(2, count); |
| 120 | assertTrue(!!buffer); |
| 121 | assertEquals("ArrayBuffer", %_ClassOf(buffer)); |
| 122 | assertSame(buffer, a.buffer); |
| 123 | |
| 124 | // The %TypedArray%.every() method should not work when |
| 125 | // transplanted to objects that are not typed arrays. |
| 126 | assertThrows(function () { constructor.prototype.every.call([1, 2, 3], function (x) {}) }, TypeError); |
| 127 | assertThrows(function () { constructor.prototype.every.call("abc", function (x) {}) }, TypeError); |
| 128 | assertThrows(function () { constructor.prototype.every.call({}, function (x) {}) }, TypeError); |
| 129 | assertThrows(function () { constructor.prototype.every.call(0, function (x) {}) }, TypeError); |
| 130 | |
| 131 | // Method must be useable on instances of other typed arrays. |
| 132 | for (var i = 0; i < typedArrayConstructors.length; i++) { |
| 133 | count = 0; |
| 134 | a = new typedArrayConstructors[i](4); |
| 135 | constructor.prototype.every.call(a, function (x) { count++; return true; }); |
| 136 | assertEquals(a.length, count); |
| 137 | } |
| 138 | |
| 139 | // Shadowing length doesn't affect every, unlike Array.prototype.every |
| 140 | a = new constructor([1, 2]); |
| 141 | Object.defineProperty(a, 'length', {value: 1}); |
| 142 | var x = 0; |
| 143 | assertEquals(a.every(function(elt) { x += elt; return true; }), true); |
| 144 | assertEquals(x, 3); |
| 145 | assertEquals(Array.prototype.every.call(a, |
| 146 | function(elt) { x += elt; return true; }), true); |
| 147 | assertEquals(x, 4); |
| 148 | } |
| 149 | |
| 150 | for (i = 0; i < typedArrayConstructors.length; i++) { |
| 151 | TestTypedArrayForEach(typedArrayConstructors[i]); |
| 152 | } |