Debian Codegraph @

js/src/tests/non262/TypedArray/map-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.map.
6
7 const otherGlobal = typeof newGlobal === "function" ? newGlobal() : undefined;
8 const typedArrayLengths = [0, 1, 1024];
9
10 function createTestCases(TAConstructor, constructor, constructorCrossRealm) {
11 let testCases = [];
12 testCases.push({
13 species: constructor,
14 method: TAConstructor.prototype.map,
15 error: TypeError,
16 });
17 if (otherGlobal) {
18 testCases.push({
19 species: constructorCrossRealm,
20 method: TAConstructor.prototype.map,
21 error: TypeError,
22 });
23 testCases.push({
24 species: constructor,
25 method: otherGlobal[TAConstructor.name].prototype.map,
26 error: otherGlobal.TypeError,
27 });
28 }
29 return testCases;
30 }
31
32 // Throws TypeError when the returned value is not a typed array.
33 for (const TAConstructor of anyTypedArrayConstructors) {
34 let callCount = 0, expectedCallCount = 0;
35 function NoTypedArrayConstructor(...args) {
36 let a = [];
37 callCount += 1;
38 return a;
39 }
40 function NoTypedArrayConstructorCrossRealm(...args) {
41 let a = new otherGlobal.Array();
42 callCount += 1;
43 return a;
44 }
45 let testCases = createTestCases(TAConstructor, NoTypedArrayConstructor, NoTypedArrayConstructorCrossRealm);
46
47 for (let {species, method, error} of testCases) {
48 for (let length of typedArrayLengths) {
49 let ta = new TAConstructor(length);
50 ta.constructor = {[Symbol.species]: species};
51 assertThrowsInstanceOf(() => method.call(ta, () => 123), error);
52 assertEq(callCount, ++expectedCallCount);
53 }
54 }
55 }
56
57 // Throws TypeError exception when returned array is too small.
58 for (const TAConstructor of anyTypedArrayConstructors) {
59 let callCount = 0, expectedCallCount = 0;
60 function TooSmallConstructor(length) {
61 let a = new TAConstructor(Math.max(length - 1, 0));
62 callCount += 1;
63 return a;
64 }
65 function TooSmallConstructorCrossRealm(length) {
66 let a = new otherGlobal[TAConstructor.name](Math.max(length - 1, 0));
67 callCount += 1;
68 return a;
69 }
70 let testCases = createTestCases(TAConstructor, TooSmallConstructor, TooSmallConstructorCrossRealm);
71
72 for (let {species, method, error} of testCases) {
73 for (let length of typedArrayLengths) {
74 let ta = new TAConstructor(length);
75 ta.constructor = {[Symbol.species]: species};
76
77 // Passes when the length is zero.
78 if (length === 0) {
79 let result = method.call(ta, () => 123);
80 assertEq(result.length, 0);
81 } else {
82 assertThrowsInstanceOf(() => method.call(ta, () => 123), error);
83 }
84 assertEq(callCount, ++expectedCallCount);
85 }
86 }
87 }
88
89 // No exception when array is larger than requested.
90 for (const TAConstructor of anyTypedArrayConstructors) {
91 const extraLength = 1;
92
93 let callCount = 0, expectedCallCount = 0;
94 function TooLargeConstructor(length) {
95 let a = new TAConstructor(length + extraLength);
96 callCount += 1;
97 return a;
98 }
99 function TooLargeConstructorCrossRealm(length) {
100 let a = new otherGlobal[TAConstructor.name](length + extraLength);
101 callCount += 1;
102 return a;
103 }
104 let testCases = createTestCases(TAConstructor, TooLargeConstructor, TooLargeConstructorCrossRealm);
105
106 for (let {species, method, error} of testCases) {
107 for (let length of typedArrayLengths) {
108 let ta = new TAConstructor(length);
109 ta.constructor = {[Symbol.species]: species};
110 let result = method.call(ta, () => 123);
111 assertEq(result.length, length + extraLength);
112 assertEq(result[0], (length === 0 ? 0 : 123));
113 assertEq(result[length + extraLength - 1], 0);
114 assertEq(callCount, ++expectedCallCount);
115 }
116 }
117 }
118
119 // Throws TypeError exception when returned array is detached.
120 if (typeof detachArrayBuffer === "function") {
121 for (const TAConstructor of typedArrayConstructors) {
122 let callCount = 0, expectedCallCount = 0;
123 function DetachConstructor(...args) {
124 let a = new TAConstructor(...args);
125 detachArrayBuffer(a.buffer);
126 callCount += 1;
127 return a;
128 }
129 function DetachConstructorCrossRealm(...args) {
130 let a = new otherGlobal[TAConstructor.name](...args);
131 otherGlobal.detachArrayBuffer(a.buffer);
132 callCount += 1;
133 return a;
134 }
135 let testCases = createTestCases(TAConstructor, DetachConstructor, DetachConstructorCrossRealm);
136
137 for (let {species, method, error} of testCases) {
138 for (let length of typedArrayLengths) {
139 let ta = new TAConstructor(length);
140 ta.constructor = {[Symbol.species]: species};
141 assertThrowsInstanceOf(() => method.call(ta, () => 123), error);
142 assertEq(callCount, ++expectedCallCount);
143 }
144 }
145 }
146 }
147
148 if (typeof reportCompare === "function")
149 reportCompare(0, 0);