| 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.slice. |
| 6 |
|
| 7 |
const otherGlobal = newGlobal({newCompartment: true}); |
| 8 |
const typedArrayLengths = [0, 1, 1024]; |
| 9 |
|
| 10 |
// Note: slice 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.slice, |
| 26 |
error: TypeError, |
| 27 |
}); |
| 28 |
testCases.push({ |
| 29 |
species: constructorCrossRealm, |
| 30 |
method: TAConstructor.prototype.slice, |
| 31 |
error: TypeError, |
| 32 |
}); |
| 33 |
testCases.push({ |
| 34 |
species: constructor, |
| 35 |
method: otherGlobal[TAConstructor.name].prototype.slice, |
| 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 too small. |
| 76 |
for (const TAConstructor of anyTypedArrayConstructors) { |
| 77 |
let callCount = 0, expectedCallCount = 0; |
| 78 |
function TooSmallConstructor(length) { |
| 79 |
let a = new TAConstructor(Math.max(length - 1, 0)); |
| 80 |
callCount += 1; |
| 81 |
return a; |
| 82 |
} |
| 83 |
function TooSmallConstructorCrossRealm(length) { |
| 84 |
let a = new otherGlobal[TAConstructor.name](Math.max(length - 1, 0)); |
| 85 |
callCount += 1; |
| 86 |
return a; |
| 87 |
} |
| 88 |
let testCases = createTestCases(TAConstructor, TooSmallConstructor, TooSmallConstructorCrossRealm); |
| 89 |
|
| 90 |
for (let {species, method, error} of testCases) { |
| 91 |
for (let length of typedArrayLengths) { |
| 92 |
let ta = new TAConstructor(length); |
| 93 |
ta.constructor = {[Symbol.species]: species}; |
| 94 |
|
| 95 |
// Passes when the length is zero. |
| 96 |
if (length === 0) { |
| 97 |
let result = method.call(ta, 0); |
| 98 |
assertEq(result.length, 0); |
| 99 |
} else { |
| 100 |
assertThrowsInstanceOf(() => method.call(ta, 0), error); |
| 101 |
} |
| 102 |
assertEq(callCount, ++expectedCallCount); |
| 103 |
} |
| 104 |
} |
| 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, 0, 0); |
| 111 |
assertEq(result.length, 0); |
| 112 |
assertEq(callCount, ++expectedCallCount); |
| 113 |
} |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
// No exception when array is larger than requested. |
| 118 |
for (const TAConstructor of anyTypedArrayConstructors) { |
| 119 |
const extraLength = 1; |
| 120 |
|
| 121 |
let callCount = 0, expectedCallCount = 0; |
| 122 |
function TooLargeConstructor(length) { |
| 123 |
let a = new TAConstructor(length + extraLength); |
| 124 |
callCount += 1; |
| 125 |
return a; |
| 126 |
} |
| 127 |
function TooLargeConstructorCrossRealm(length) { |
| 128 |
let a = new otherGlobal[TAConstructor.name](length + extraLength); |
| 129 |
callCount += 1; |
| 130 |
return a; |
| 131 |
} |
| 132 |
let testCases = createTestCases(TAConstructor, TooLargeConstructor, TooLargeConstructorCrossRealm); |
| 133 |
|
| 134 |
for (let {species, method, error} of testCases) { |
| 135 |
for (let length of typedArrayLengths) { |
| 136 |
let ta = new TAConstructor(length); |
| 137 |
ta.constructor = {[Symbol.species]: species}; |
| 138 |
let result = method.call(ta, 0); |
| 139 |
assertEq(result.length, length + extraLength); |
| 140 |
assertEq(callCount, ++expectedCallCount); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
for (let {species, method, error} of testCases) { |
| 145 |
for (let length of typedArrayLengths) { |
| 146 |
let ta = new TAConstructor(length); |
| 147 |
ta.constructor = {[Symbol.species]: species}; |
| 148 |
let result = method.call(ta, 0, 0); |
| 149 |
assertEq(result.length, extraLength); |
| 150 |
assertEq(callCount, ++expectedCallCount); |
| 151 |
} |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
// Throws TypeError exception when returned array is detached. |
| 156 |
if (typeof detachArrayBuffer === "function") { |
| 157 |
for (const TAConstructor of typedArrayConstructors) { |
| 158 |
let callCount = 0, expectedCallCount = 0; |
| 159 |
function DetachConstructor(...args) { |
| 160 |
let a = new TAConstructor(...args); |
| 161 |
detachArrayBuffer(a.buffer); |
| 162 |
callCount += 1; |
| 163 |
return a; |
| 164 |
} |
| 165 |
function DetachConstructorCrossRealm(...args) { |
| 166 |
let a = new otherGlobal[TAConstructor.name](...args); |
| 167 |
otherGlobal.detachArrayBuffer(a.buffer); |
| 168 |
callCount += 1; |
| 169 |
return a; |
| 170 |
} |
| 171 |
let testCases = createTestCases(TAConstructor, DetachConstructor, DetachConstructorCrossRealm); |
| 172 |
|
| 173 |
for (let {species, method, error} of testCases) { |
| 174 |
for (let length of typedArrayLengths) { |
| 175 |
let ta = new TAConstructor(length); |
| 176 |
ta.constructor = {[Symbol.species]: species}; |
| 177 |
assertThrowsInstanceOf(() => method.call(ta, 0), error); |
| 178 |
assertEq(callCount, ++expectedCallCount); |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
for (let {species, method, error} of testCases) { |
| 183 |
for (let length of typedArrayLengths) { |
| 184 |
let ta = new TAConstructor(length); |
| 185 |
ta.constructor = {[Symbol.species]: species}; |
| 186 |
assertThrowsInstanceOf(() => method.call(ta, 0, 0), error); |
| 187 |
assertEq(callCount, ++expectedCallCount); |
| 188 |
} |
| 189 |
} |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
if (typeof reportCompare === "function") |
| 194 |
reportCompare(0, 0); |