Debian Codegraph @

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