OLD | NEW |
| (Empty) |
1 // Copyright 2015 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-species | |
6 | |
7 // Subclasses of %TypedArray% construct themselves under map, etc | |
8 | |
9 var typedArrayConstructors = [ | |
10 Uint8Array, | |
11 Int8Array, | |
12 Uint16Array, | |
13 Int16Array, | |
14 Uint32Array, | |
15 Int32Array, | |
16 Uint8ClampedArray, | |
17 Float32Array, | |
18 Float64Array | |
19 ]; | |
20 | |
21 for (let constructor of typedArrayConstructors) { | |
22 class MyTypedArray extends constructor { } | |
23 assertEquals(MyTypedArray, new MyTypedArray().map(()=>0).constructor); | |
24 assertEquals(MyTypedArray, new MyTypedArray().filter(()=>{}).constructor); | |
25 assertEquals(MyTypedArray, new MyTypedArray().slice().constructor); | |
26 } | |
27 | |
28 // Subclasses can override @@species to return the another class | |
29 | |
30 for (let constructor of typedArrayConstructors) { | |
31 class MyTypedArray extends constructor { } | |
32 class MyOtherTypedArray extends constructor { | |
33 static get [Symbol.species]() { return MyTypedArray; } | |
34 } | |
35 assertEquals(MyTypedArray, new MyOtherTypedArray().map(()=>0).constructor); | |
36 assertEquals(MyTypedArray, new MyOtherTypedArray().filter(()=>{}).constructor)
; | |
37 assertEquals(MyTypedArray, new MyOtherTypedArray().slice().constructor); | |
38 } | |
39 | |
40 // TypedArray too-short and non-TypedArray error checking | |
41 | |
42 for (let constructor of typedArrayConstructors) { | |
43 class MyShortTypedArray extends constructor { | |
44 constructor(length) { super(length - 1); } | |
45 } | |
46 assertThrows(() => new MyShortTypedArray(5).map(()=>0), TypeError); | |
47 assertThrows(() => new MyShortTypedArray(5).filter(()=>true), TypeError); | |
48 assertThrows(() => new MyShortTypedArray(5).slice(), TypeError); | |
49 | |
50 class MyNonTypedArray extends constructor { | |
51 static get [Symbol.species]() { return Array; } | |
52 } | |
53 assertThrows(() => new MyNonTypedArray().map(()=>0), TypeError); | |
54 assertThrows(() => new MyNonTypedArray().filter(()=>{}), TypeError); | |
55 assertThrows(() => new MyNonTypedArray().slice(), TypeError); | |
56 } | |
57 | |
58 // Defaults when constructor or @@species is missing or non-constructor | |
59 | |
60 for (let constructor of typedArrayConstructors) { | |
61 class MyDefaultTypedArray extends constructor { | |
62 static get [Symbol.species]() { return undefined; } | |
63 } | |
64 assertEquals(constructor, new MyDefaultTypedArray().map(()=>0).constructor); | |
65 | |
66 class MyOtherDefaultTypedArray extends constructor { } | |
67 assertEquals(MyOtherDefaultTypedArray, new MyOtherDefaultTypedArray().map(()=>
0).constructor); | |
68 MyOtherDefaultTypedArray.prototype.constructor = undefined; | |
69 assertEquals(constructor, new MyOtherDefaultTypedArray().map(()=>0).constructo
r); | |
70 } | |
71 | |
72 // Exceptions propagated when getting constructor @@species throws | |
73 | |
74 class SpeciesError extends Error { } | |
75 class ConstructorError extends Error { } | |
76 | |
77 for (let constructor of typedArrayConstructors) { | |
78 class MyThrowingArray extends constructor { | |
79 static get [Symbol.species]() { throw new SpeciesError; } | |
80 } | |
81 assertThrows(() => new MyThrowingArray().map(()=>{}), SpeciesError); | |
82 Object.defineProperty(MyThrowingArray.prototype, 'constructor', { | |
83 get() { throw new ConstructorError; } | |
84 }); | |
85 assertThrows(() => new MyThrowingArray().map(()=>{}), ConstructorError); | |
86 } | |
OLD | NEW |