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