| 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-arrays --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 TestTypedArrayLastIndexOf(constructor) { |
| 25 assertEquals(1, constructor.prototype.lastIndexOf.length); |
| 26 |
| 27 // Negative cases. |
| 28 assertEquals(-1, constructor.of().lastIndexOf(1)); |
| 29 var a = constructor.of(1, 2, 3, 1, 2, 3, 1); |
| 30 assertEquals(-1, a.lastIndexOf(4)); |
| 31 |
| 32 assertEquals(5, a.lastIndexOf(3)); |
| 33 // Index out of range. |
| 34 assertEquals(4, a.lastIndexOf(2, a.length)); |
| 35 // Index in range. |
| 36 assertEquals(1, a.lastIndexOf(2, 2)); |
| 37 // Negative index in range. |
| 38 assertEquals(0, a.lastIndexOf(1, -5)); |
| 39 // Negative index out of range. |
| 40 assertEquals(-1, a.lastIndexOf(1, -10)); |
| 41 |
| 42 // Typed arrays cannot store "undefined", so even when creating one |
| 43 // with "undefined" values, those are coerced to numbers, which means |
| 44 // .lastIndexOf() cannot ever return a valid index for "undefined". |
| 45 assertEquals(-1, a.lastIndexOf(undefined)); |
| 46 assertEquals(-1, constructor.of(undefined).lastIndexOf(undefined)); |
| 47 |
| 48 // Neutering the buffer backing the typed array should make |
| 49 // .lastIndexOf() always return -1 afterwards. |
| 50 a = constructor.of(1, 2, 3, 4, 5, 6); |
| 51 %ArrayBufferNeuter(a.buffer); |
| 52 CheckTypedArrayIsNeutered(a); |
| 53 assertEquals(-1, a.lastIndexOf(4)); |
| 54 |
| 55 // The method must work for typed arrays created from ArrayBuffer. |
| 56 // The length of the ArrayBuffer is chosen so it is a multiple of |
| 57 // all lengths of the typed array items. |
| 58 a = new constructor(new ArrayBuffer(64)); |
| 59 a[4] = 42; |
| 60 assertEquals(4, a.lastIndexOf(42)); |
| 61 |
| 62 // Externalizing the array by using the .buffer property should not |
| 63 // affect operation of .lastIndexOf(). |
| 64 a = constructor.of(1, 2, 3, 4, 5, 6); |
| 65 var buffer = a.buffer; |
| 66 assertTrue(!!buffer); |
| 67 assertEquals("ArrayBuffer", %_ClassOf(buffer)); |
| 68 assertSame(buffer, a.buffer); |
| 69 assertEquals(2, a.lastIndexOf(3)); |
| 70 |
| 71 // The %TypedArray%.prototype.lastIndexOf() method should not work |
| 72 // when transplanted to objects that are not typed arrays. |
| 73 assertThrows(function () { constructor.prototype.lastIndexOf.call([1, 2, 3], 0
); }, TypeError); |
| 74 assertThrows(function () { constructor.prototype.lastIndexOf.call("abc", 0); }
, TypeError); |
| 75 assertThrows(function () { constructor.prototype.lastIndexOf.call({}, 0); }, T
ypeError); |
| 76 assertThrows(function () { constructor.prototype.lastIndexOf.call(0, 0); }, Ty
peError); |
| 77 |
| 78 // Method must be useable on instances of other typed arrays. |
| 79 for (var otherConstructor of typedArrayConstructors) { |
| 80 a = otherConstructor.of(1, 2, 3, 4); |
| 81 assertEquals(2, constructor.prototype.lastIndexOf.call(a, 3)); |
| 82 } |
| 83 } |
| 84 |
| 85 for (var constructor of typedArrayConstructors) { |
| 86 TestTypedArrayLastIndexOf(constructor); |
| 87 } |
| OLD | NEW |