|
| 1 | +function shouldBe(actual, expected) { |
| 2 | + if (!Object.is(actual, expected)) |
| 3 | + throw new Error(`bad value: expected ${String(expected)} but got ${String(actual)}`); |
| 4 | +} |
| 5 | + |
| 6 | +function shouldBeArray(actual, expected) { |
| 7 | + shouldBe(Array.isArray(actual), true); |
| 8 | + shouldBe(actual.length, expected.length); |
| 9 | + for (let i = 0; i < expected.length; ++i) |
| 10 | + shouldBe(actual[i], expected[i]); |
| 11 | +} |
| 12 | + |
| 13 | +function shouldThrow(func, errorType) { |
| 14 | + let error; |
| 15 | + try { |
| 16 | + func(); |
| 17 | + } catch (e) { |
| 18 | + error = e; |
| 19 | + } |
| 20 | + if (!(error instanceof errorType)) |
| 21 | + throw new Error(`bad error: ${String(error)}`); |
| 22 | +} |
| 23 | + |
| 24 | +function spread(iterable) { |
| 25 | + return [...iterable]; |
| 26 | +} |
| 27 | +noInline(spread); |
| 28 | + |
| 29 | +function spreadWithOthers(iterable) { |
| 30 | + return [1, ...iterable, 2, ...iterable]; |
| 31 | +} |
| 32 | +noInline(spreadWithOthers); |
| 33 | + |
| 34 | +function collect() { |
| 35 | + return Array.prototype.slice.call(arguments); |
| 36 | +} |
| 37 | +noInline(collect); |
| 38 | + |
| 39 | +function callSpread(iterable) { |
| 40 | + return collect(...iterable); |
| 41 | +} |
| 42 | +noInline(callSpread); |
| 43 | + |
| 44 | +class Collector { |
| 45 | + constructor(...args) { |
| 46 | + this.args = args; |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +function constructSpread(iterable) { |
| 51 | + return new Collector(...iterable).args; |
| 52 | +} |
| 53 | +noInline(constructSpread); |
| 54 | + |
| 55 | +function elements(typedArray) { |
| 56 | + let result = []; |
| 57 | + for (let i = 0; i < typedArray.length; ++i) |
| 58 | + result.push(typedArray[i]); |
| 59 | + return result; |
| 60 | +} |
| 61 | + |
| 62 | +function makeTypedArray(constructor, values) { |
| 63 | + let typedArray = new constructor(19); |
| 64 | + for (let i = 0; i < typedArray.length; ++i) |
| 65 | + typedArray[i] = values[i % values.length]; |
| 66 | + return typedArray; |
| 67 | +} |
| 68 | + |
| 69 | +const typedArrays = [ |
| 70 | + makeTypedArray(Int8Array, [-128, -1, 0, 1, 127]), |
| 71 | + makeTypedArray(Uint8Array, [0, 1, 128, 255]), |
| 72 | + makeTypedArray(Uint8ClampedArray, [0, 1, 128, 255]), |
| 73 | + makeTypedArray(Int16Array, [-32768, -1, 0, 32767]), |
| 74 | + makeTypedArray(Uint16Array, [0, 1, 32768, 65535]), |
| 75 | + makeTypedArray(Int32Array, [-2147483648, -1, 0, 2147483647]), |
| 76 | + makeTypedArray(Uint32Array, [0, 1, 2147483647, 2147483648, 4294967295]), |
| 77 | + makeTypedArray(Float16Array, [0, -0, 1.5, NaN, Infinity, -Infinity, 65504]), |
| 78 | + makeTypedArray(Float32Array, [0, -0, 1.5, NaN, Infinity, -Infinity, 16777216, 0.1]), |
| 79 | + makeTypedArray(Float64Array, [0, -0, 1.5, NaN, Infinity, -Infinity, 9007199254740992, 0.1, 4294967296]), |
| 80 | + makeTypedArray(BigInt64Array, [-9223372036854775808n, -1n, 0n, 1n, 9223372036854775807n]), |
| 81 | + makeTypedArray(BigUint64Array, [0n, 1n, 9223372036854775808n, 18446744073709551615n]), |
| 82 | + new Uint8Array(0), |
| 83 | + new Uint16Array(new Uint16Array([1, 2, 3, 4, 5, 6]).buffer, 4, 3), |
| 84 | +]; |
| 85 | +const expectedElements = typedArrays.map(elements); |
| 86 | +const expectedElementsWithOthers = expectedElements.map(expected => [1, ...expected, 2, ...expected]); |
| 87 | + |
| 88 | +for (let i = 0; i < testLoopCount; ++i) { |
| 89 | + let index = i % typedArrays.length; |
| 90 | + let typedArray = typedArrays[index]; |
| 91 | + let expected = expectedElements[index]; |
| 92 | + shouldBeArray(spread(typedArray), expected); |
| 93 | + shouldBeArray(callSpread(typedArray), expected); |
| 94 | + shouldBeArray(constructSpread(typedArray), expected); |
| 95 | + shouldBeArray(spreadWithOthers(typedArray), expectedElementsWithOthers[index]); |
| 96 | +} |
| 97 | + |
| 98 | +{ |
| 99 | + let typedArray = new Uint8Array([1, 2, 3]); |
| 100 | + let first = spread(typedArray); |
| 101 | + let second = spread(typedArray); |
| 102 | + shouldBe(first === second, false); |
| 103 | + first[0] = 42; |
| 104 | + first.push(4); |
| 105 | + shouldBeArray(first, [42, 2, 3, 4]); |
| 106 | + shouldBeArray(second, [1, 2, 3]); |
| 107 | + shouldBeArray(elements(typedArray), [1, 2, 3]); |
| 108 | + typedArray[1] = 9; |
| 109 | + shouldBeArray(second, [1, 2, 3]); |
| 110 | + shouldBeArray(spread(typedArray), [1, 9, 3]); |
| 111 | +} |
| 112 | + |
| 113 | +{ |
| 114 | + let typedArray = new Uint8Array([1, 2, 3]); |
| 115 | + typedArray.buffer.transfer(); |
| 116 | + shouldThrow(() => spread(typedArray), TypeError); |
| 117 | +} |
| 118 | + |
| 119 | +shouldThrow(() => spread(new DataView(new ArrayBuffer(8))), TypeError); |
| 120 | + |
| 121 | +{ |
| 122 | + let buffer = new ArrayBuffer(8, { maxByteLength: 16 }); |
| 123 | + let whole = new Uint8Array(buffer); |
| 124 | + for (let i = 0; i < 8; ++i) |
| 125 | + whole[i] = i + 1; |
| 126 | + let fixed = new Uint8Array(buffer, 4, 4); |
| 127 | + let tracking = new Uint8Array(buffer, 4); |
| 128 | + let trackingUint16 = new Uint16Array(buffer, 2); |
| 129 | + shouldBeArray(spread(whole), [1, 2, 3, 4, 5, 6, 7, 8]); |
| 130 | + shouldBeArray(spread(fixed), [5, 6, 7, 8]); |
| 131 | + shouldBeArray(spread(tracking), [5, 6, 7, 8]); |
| 132 | + shouldBeArray(spread(trackingUint16), elements(trackingUint16)); |
| 133 | + |
| 134 | + buffer.resize(7); |
| 135 | + shouldBeArray(spread(whole), [1, 2, 3, 4, 5, 6, 7]); |
| 136 | + shouldThrow(() => spread(fixed), TypeError); |
| 137 | + shouldBeArray(spread(tracking), [5, 6, 7]); |
| 138 | + shouldBeArray(spread(trackingUint16), elements(trackingUint16)); |
| 139 | + |
| 140 | + buffer.resize(3); |
| 141 | + shouldBeArray(spread(whole), [1, 2, 3]); |
| 142 | + shouldThrow(() => spread(fixed), TypeError); |
| 143 | + shouldThrow(() => spread(tracking), TypeError); |
| 144 | + shouldBeArray(spread(trackingUint16), []); |
| 145 | + |
| 146 | + buffer.resize(1); |
| 147 | + shouldThrow(() => spread(trackingUint16), TypeError); |
| 148 | + |
| 149 | + buffer.resize(16); |
| 150 | + shouldBeArray(spread(whole), [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]); |
| 151 | + shouldBeArray(spread(fixed), [0, 0, 0, 0]); |
| 152 | + shouldBeArray(spread(tracking), [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]); |
| 153 | + shouldBeArray(spread(trackingUint16), [0, 0, 0, 0, 0, 0, 0]); |
| 154 | +} |
| 155 | + |
| 156 | +{ |
| 157 | + let buffer = new SharedArrayBuffer(4, { maxByteLength: 8 }); |
| 158 | + let tracking = new Uint8Array(buffer); |
| 159 | + let fixed = new Uint8Array(buffer, 1, 2); |
| 160 | + tracking.set([1, 2, 3, 4]); |
| 161 | + shouldBeArray(spread(tracking), [1, 2, 3, 4]); |
| 162 | + shouldBeArray(spread(fixed), [2, 3]); |
| 163 | + buffer.grow(8); |
| 164 | + shouldBeArray(spread(tracking), [1, 2, 3, 4, 0, 0, 0, 0]); |
| 165 | + shouldBeArray(spread(fixed), [2, 3]); |
| 166 | +} |
| 167 | + |
| 168 | +{ |
| 169 | + let typedArray = new Uint8Array([1, 2, 3]); |
| 170 | + typedArray[Symbol.iterator] = function* () { |
| 171 | + yield 42; |
| 172 | + }; |
| 173 | + shouldBeArray(spread(typedArray), [42]); |
| 174 | +} |
| 175 | + |
| 176 | +{ |
| 177 | + class Derived extends Uint8Array { } |
| 178 | + shouldBeArray(spread(new Derived([1, 2, 3])), [1, 2, 3]); |
| 179 | + |
| 180 | + class DerivedWithIterator extends Uint8Array { |
| 181 | + *[Symbol.iterator]() { |
| 182 | + yield 42; |
| 183 | + } |
| 184 | + } |
| 185 | + shouldBeArray(spread(new DerivedWithIterator([1, 2, 3])), [42]); |
| 186 | +} |
| 187 | + |
| 188 | +{ |
| 189 | + let typedArray = new Uint8Array([1, 2, 3]); |
| 190 | + Object.setPrototypeOf(typedArray, { |
| 191 | + __proto__: Uint8Array.prototype, |
| 192 | + *[Symbol.iterator]() { |
| 193 | + yield 42; |
| 194 | + } |
| 195 | + }); |
| 196 | + shouldBeArray(spread(typedArray), [42]); |
| 197 | +} |
| 198 | + |
| 199 | +function testInRealm(modify, check) { |
| 200 | + let realm = createGlobalObject(); |
| 201 | + let typedArrays = [ |
| 202 | + new realm.Uint8Array([1, 2, 3]), |
| 203 | + new realm.Float64Array([1.5, 2.5]), |
| 204 | + new realm.BigInt64Array([1n, 2n]), |
| 205 | + ]; |
| 206 | + shouldBeArray(spread(typedArrays[0]), [1, 2, 3]); |
| 207 | + shouldBeArray(spread(typedArrays[1]), [1.5, 2.5]); |
| 208 | + shouldBeArray(spread(typedArrays[2]), [1n, 2n]); |
| 209 | + modify(realm); |
| 210 | + check(typedArrays); |
| 211 | + shouldBeArray(spread(new Uint8Array([1, 2, 3])), [1, 2, 3]); |
| 212 | + shouldBeArray(spread(new Float64Array([1.5, 2.5])), [1.5, 2.5]); |
| 213 | +} |
| 214 | + |
| 215 | +testInRealm(realm => { |
| 216 | + realm.Uint8Array.prototype[Symbol.iterator] = function* () { |
| 217 | + yield 42; |
| 218 | + }; |
| 219 | +}, typedArrays => { |
| 220 | + shouldBeArray(spread(typedArrays[0]), [42]); |
| 221 | + shouldBeArray(spread(typedArrays[1]), [1.5, 2.5]); |
| 222 | + shouldBeArray(spread(typedArrays[2]), [1n, 2n]); |
| 223 | +}); |
| 224 | + |
| 225 | +testInRealm(realm => { |
| 226 | + Object.getPrototypeOf(realm.Uint8Array.prototype)[Symbol.iterator] = function* () { |
| 227 | + yield this.length; |
| 228 | + }; |
| 229 | +}, typedArrays => { |
| 230 | + shouldBeArray(spread(typedArrays[0]), [3]); |
| 231 | + shouldBeArray(spread(typedArrays[1]), [2]); |
| 232 | + shouldBeArray(spread(typedArrays[2]), [2]); |
| 233 | +}); |
| 234 | + |
| 235 | +testInRealm(realm => { |
| 236 | + let arrayIteratorPrototype = Object.getPrototypeOf(new realm.Array()[Symbol.iterator]()); |
| 237 | + let count = 0; |
| 238 | + arrayIteratorPrototype.next = function () { |
| 239 | + return { done: count++ & 1, value: 42 }; |
| 240 | + }; |
| 241 | +}, typedArrays => { |
| 242 | + shouldBeArray(spread(typedArrays[0]), [42]); |
| 243 | + shouldBeArray(spread(typedArrays[1]), [42]); |
| 244 | + shouldBeArray(spread(typedArrays[2]), [42]); |
| 245 | +}); |
| 246 | + |
| 247 | +testInRealm(realm => { |
| 248 | + Object.setPrototypeOf(realm.Float64Array.prototype, { |
| 249 | + __proto__: Object.getPrototypeOf(realm.Float64Array.prototype), |
| 250 | + *[Symbol.iterator]() { |
| 251 | + yield 42; |
| 252 | + } |
| 253 | + }); |
| 254 | +}, typedArrays => { |
| 255 | + shouldBeArray(spread(typedArrays[0]), [1, 2, 3]); |
| 256 | + shouldBeArray(spread(typedArrays[1]), [42]); |
| 257 | + shouldBeArray(spread(typedArrays[2]), [1n, 2n]); |
| 258 | +}); |
0 commit comments