Debian Codegraph @

js/src/tests/non262/TypedArray/subarray-validation.js

raw
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5 // Summary: Ensure typed array validation is called for TypedArray.prototype.subarray.
6
7 const otherGlobal = newGlobal({newCompartment: true});
8 const typedArrayLengths = [0, 1, 1024];
9
10 // Note: subarray uses CallTypedArrayMethodIfWrapped, which results in throwing
11 // a TypeError from the wrong Realm if cross-compartment. The browser
12 // runner doesn't support the "newCompartment" option, so it can't create
13 // cross-compartment globals, which means it throws the error from the
14 // correct Realm.
15 const eitherGlobalTypeError = {
16 [Symbol.hasInstance](obj) {
17 return obj instanceof TypeError || obj instanceof otherGlobal.TypeError;
18 }
19 };
20
21 function createTestCases(TAConstructor, constructor, constructorCrossRealm) {
22 let testCases = [];
23 testCases.push({
24 species: constructor,
25 method: TAConstructor.prototype.subarray,
26 error: TypeError,
27 });
28 testCases.push({
29 species: constructorCrossRealm,
30 method: TAConstructor.prototype.subarray,
31 error: TypeError,
32 });
33 testCases.push({
34 species: constructor,
35 method: otherGlobal[TAConstructor.name].prototype.subarray,
36 error: eitherGlobalTypeError,
37 });
38 return testCases;
39 }
40
41 // Throws TypeError when the returned value is not a typed array.
42 for (const TAConstructor of anyTypedArrayConstructors) {
43 let callCount = 0, expectedCallCount = 0;
44 function NoTypedArrayConstructor(...args) {
45 let a = [];
46 callCount += 1;
47 return a;
48 }
49 function NoTypedArrayConstructorCrossRealm(...args) {
50 let a = new otherGlobal.Array();
51 callCount += 1;
52 return a;
53 }
54 let testCases = createTestCases(TAConstructor, NoTypedArrayConstructor, NoTypedArrayConstructorCrossRealm);
55
56 for (let {species, method, error} of testCases) {
57 for (let length of typedArrayLengths) {
58 let ta = new TAConstructor(length);
59 ta.constructor = {[Symbol.species]: species};
60 assertThrowsInstanceOf(() => method.call(ta, 0), error);
61 assertEq(callCount, ++expectedCallCount);
62 }
63 }
64
65 for (let {species, method, error} of testCases) {
66 for (let length of typedArrayLengths) {
67 let ta = new TAConstructor(length);
68 ta.constructor = {[Symbol.species]: species};
69 assertThrowsInstanceOf(() => method.call(ta, 0, 0), error);
70 assertEq(callCount, ++expectedCallCount);
71 }
72 }
73 }
74
75 // Throws TypeError exception when returned array is detached.
76 if (typeof detachArrayBuffer === "function") {
77 for (const TAConstructor of typedArrayConstructors) {
78 let callCount = 0, expectedCallCount = 0;
79 function DetachConstructor(...args) {
80 let a = new TAConstructor(...args);
81 detachArrayBuffer(a.buffer);
82 callCount += 1;
83 return a;
84 }
85 function DetachConstructorCrossRealm(...args) {
86 let a = new otherGlobal[TAConstructor.name](...args);
87 // Note: TypedArray |a| is (currently) created in this global, not
88 // |otherGlobal|, because a typed array and its buffer must
89 // use the same compartment.
90 detachArrayBuffer(a.buffer);
91 callCount += 1;
92 return a;
93 }
94 let testCases = createTestCases(TAConstructor, DetachConstructor, DetachConstructorCrossRealm);
95
96 for (let {species, method, error} of testCases) {
97 for (let length of typedArrayLengths) {
98 let ta = new TAConstructor(length);
99 ta.constructor = {[Symbol.species]: species};
100 assertThrowsInstanceOf(() => method.call(ta, 0), error);
101 assertEq(callCount, ++expectedCallCount);
102 }
103 }
104
105 for (let {species, method, error} of testCases) {
106 for (let length of typedArrayLengths) {
107 let ta = new TAConstructor(length);
108 ta.constructor = {[Symbol.species]: species};
109 assertThrowsInstanceOf(() => method.call(ta, 0, 0), error);
110 assertEq(callCount, ++expectedCallCount);
111 }
112 }
113 }
114 }
115
116 if (typeof reportCompare === "function")
117 reportCompare(0, 0);