|
| 1 | +const shared = [{ id: 0 }, { id: 1 }, { id: 2 }, { id: 3 }]; |
| 2 | + |
| 3 | +function pushed(...values) { |
| 4 | + const array = []; |
| 5 | + for (const value of values) |
| 6 | + array.push(value); |
| 7 | + return array; |
| 8 | +} |
| 9 | + |
| 10 | +class Derived extends Array { } |
| 11 | + |
| 12 | +class Species extends Array { |
| 13 | + static result; |
| 14 | + static get [Symbol.species]() { return function () { return Species.result; }; } |
| 15 | +} |
| 16 | + |
| 17 | +class StampField extends (function (object) { return object; }) { |
| 18 | + #field = 1; |
| 19 | + static has(object) { return #field in object; } |
| 20 | +} |
| 21 | + |
| 22 | +class StampMethod extends (function (object) { return object; }) { |
| 23 | + #method() { } |
| 24 | + static has(object) { return #method in object; } |
| 25 | +} |
| 26 | + |
| 27 | +const makers = [ |
| 28 | + () => [], |
| 29 | + () => [1, 2, 3], |
| 30 | + () => [1.5, 2.5, 3.5], |
| 31 | + () => ["a", "b", "c"], |
| 32 | + () => pushed(1, 2, 3), |
| 33 | + () => pushed(1.5, 2.5, 3.5), |
| 34 | + () => pushed(shared[0], shared[1], shared[2]), |
| 35 | + () => pushed(1, 2.5, shared[0], "a", undefined, null), |
| 36 | + () => [1, , 3], |
| 37 | + () => { const array = pushed(shared[0], shared[1], shared[2]); delete array[1]; return array; }, |
| 38 | + () => new Array(4), |
| 39 | + () => { const array = [1, 2, 3]; array.length = 6; return array; }, |
| 40 | + () => { const array = [1, 2, 3]; array.foo = shared[3]; return array; }, |
| 41 | + () => { const array = [1, 2, 3]; Object.defineProperty(array, 1, { get() { return 42; }, set(value) { }, enumerable: true, configurable: true }); return array; }, |
| 42 | + () => { const array = [1, 2, 3]; Object.defineProperty(array, 1, { value: 2, writable: false, enumerable: false, configurable: true }); return array; }, |
| 43 | + () => { const array = [1, 2, 3]; Object.defineProperty(array, "foo", { get() { return 42; }, configurable: true }); return array; }, |
| 44 | + () => { const array = [1, 2, 3]; Object.defineProperty(array, "length", { writable: false }); return array; }, |
| 45 | + () => Object.preventExtensions([1, 2, 3]), |
| 46 | + () => Object.seal([1, 2, 3]), |
| 47 | + () => { const array = [1, 2, 3]; $vm.ensureArrayStorage(array); array.unshift(0); array.shift(); return array; }, |
| 48 | + () => Derived.from([1, 2, 3]), |
| 49 | + () => ({ 0: 1, 1: 2, 2: 3 }), |
| 50 | + () => ({ 0: shared[0], 2: shared[1], foo: 1, length: 3 }), |
| 51 | +]; |
| 52 | + |
| 53 | +const partiallyFrozenMakers = [ |
| 54 | + () => Object.preventExtensions([]), |
| 55 | + () => { const array = [1, 2]; Object.defineProperty(array, 0, { writable: false, configurable: false }); Object.defineProperty(array, 1, { writable: false, configurable: false }); return array; }, |
| 56 | + () => { const array = [1, 2]; Object.defineProperty(array, 0, { configurable: false }); Object.defineProperty(array, 1, { configurable: false }); return Object.preventExtensions(array); }, |
| 57 | + () => { const array = [1, 2]; Object.defineProperty(array, 0, { writable: false, configurable: false }); Object.defineProperty(array, 1, { writable: false, configurable: false }); return Object.preventExtensions(array); }, |
| 58 | + () => { const array = [1, 2]; Object.defineProperty(array, 0, { writable: false, configurable: false }); Object.defineProperty(array, 1, { writable: false }); Object.defineProperty(array, "length", { writable: false }); return Object.preventExtensions(array); }, |
| 59 | +]; |
| 60 | + |
| 61 | +const sparseMakers = [ |
| 62 | + () => { const array = [1, 2, 3]; array[100000] = 4; return array; }, |
| 63 | + () => { const array = [1, 2, 3]; array[100000] = 4; array.foo = 1; delete array[1]; return array; }, |
| 64 | +]; |
| 65 | + |
| 66 | +const syntaxOperations = [ |
| 67 | + "a[0] = v", |
| 68 | + "a[1] = v", |
| 69 | + "a[2] = a[2]", |
| 70 | + "a[a.length] = v", |
| 71 | + "a[a.length + 10] = v", |
| 72 | + "a[4294967294] = v", |
| 73 | + "a[4294967295] = v", |
| 74 | + "a[-1] = v", |
| 75 | + "a.foo = v", |
| 76 | + "a.bar = v", |
| 77 | + "a.length = 0", |
| 78 | + "a.length = a.length", |
| 79 | + "a.length = a.length + 1", |
| 80 | + "a.length = -1", |
| 81 | + "return delete a[0]", |
| 82 | + "return delete a[1]", |
| 83 | + "return delete a[a.length]", |
| 84 | + "return delete a.length", |
| 85 | + "return delete a.foo", |
| 86 | + "a.__proto__ = null", |
| 87 | + "const o = Object.create(a); o[0] = v; return Reflect.ownKeys(o)", |
| 88 | + "const o = Object.create(a); o[1] = v; return Reflect.ownKeys(o)", |
| 89 | + "const o = Object.create(a); o[a.length] = v; return Reflect.ownKeys(o)", |
| 90 | + "const o = Object.create(a); o.length = 0; return Reflect.ownKeys(o)", |
| 91 | + "const o = Object.create(a); o.foo = v; return Reflect.ownKeys(o)", |
| 92 | + "const o = [1, 2, 3]; Object.setPrototypeOf(o, a); delete o[0]; o[0] = v; return [Reflect.ownKeys(o), o[0]]", |
| 93 | +]; |
| 94 | + |
| 95 | +const callOperations = [ |
| 96 | + "return Array.prototype.push.call(a, v)", |
| 97 | + "return Array.prototype.push.call(a)", |
| 98 | + "return Array.prototype.pop.call(a)", |
| 99 | + "return Array.prototype.shift.call(a)", |
| 100 | + "return Array.prototype.unshift.call(a, v)", |
| 101 | + "return Array.prototype.unshift.call(a)", |
| 102 | + "return Array.prototype.splice.call(a, 0, 1)", |
| 103 | + "return Array.prototype.splice.call(a, 0, 0)", |
| 104 | + "return Array.prototype.splice.call(a, 0, 0, v)", |
| 105 | + "return Array.prototype.splice.call(a, 0, 1, v)", |
| 106 | + "return Array.prototype.reverse.call(a)", |
| 107 | + "return Array.prototype.sort.call(a)", |
| 108 | + "return Array.prototype.sort.call(a, () => 0)", |
| 109 | + "return Array.prototype.fill.call(a, v)", |
| 110 | + "return Array.prototype.fill.call(a, v, 0, 0)", |
| 111 | + "return Array.prototype.copyWithin.call(a, 0, 1)", |
| 112 | + "return Object.defineProperty(a, 0, { value: v })", |
| 113 | + "return Object.defineProperty(a, 0, { value: a[0] })", |
| 114 | + "return Object.defineProperty(a, 0, { })", |
| 115 | + "return Object.defineProperty(a, 0, { writable: true })", |
| 116 | + "return Object.defineProperty(a, 0, { writable: false })", |
| 117 | + "return Object.defineProperty(a, 0, { configurable: true })", |
| 118 | + "return Object.defineProperty(a, 0, { enumerable: false })", |
| 119 | + "return Object.defineProperty(a, 0, { get() { } })", |
| 120 | + "return Object.defineProperty(a, 1, { value: v })", |
| 121 | + "return Object.defineProperty(a, 1, { get() { } })", |
| 122 | + "return Object.defineProperty(a, a.length, { value: v })", |
| 123 | + "return Object.defineProperty(a, a.length, { })", |
| 124 | + "return Object.defineProperty(a, 'length', { value: 0 })", |
| 125 | + "return Object.defineProperty(a, 'length', { value: a.length })", |
| 126 | + "return Object.defineProperty(a, 'length', { value: a.length + 1 })", |
| 127 | + "return Object.defineProperty(a, 'length', { writable: true })", |
| 128 | + "return Object.defineProperty(a, 'length', { writable: false })", |
| 129 | + "return Object.defineProperty(a, 'foo', { value: v })", |
| 130 | + "return Object.defineProperty(a, 'bar', { value: v })", |
| 131 | + "return Reflect.set(a, 0, v)", |
| 132 | + "return Reflect.set(a, a.length, v)", |
| 133 | + "return Reflect.set({ }, 0, v, a)", |
| 134 | + "return Reflect.set({ }, a.length, v, a)", |
| 135 | + "return Reflect.set({ }, 'length', 0, a)", |
| 136 | + "return Reflect.set({ }, 'bar', 0, a)", |
| 137 | + "const o = { }; return [Reflect.set(a, 0, v, o), o]", |
| 138 | + "const o = { }; return [Reflect.set(a, a.length, v, o), o]", |
| 139 | + "const o = { }; return [Reflect.set(a, 'length', 0, o), o]", |
| 140 | + "return Reflect.defineProperty(a, 0, { value: v })", |
| 141 | + "return Reflect.defineProperty(a, a.length, { value: v })", |
| 142 | + "return Reflect.deleteProperty(a, 0)", |
| 143 | + "return Reflect.setPrototypeOf(a, null)", |
| 144 | + "return Reflect.setPrototypeOf(a, Reflect.getPrototypeOf(a))", |
| 145 | + "return Object.setPrototypeOf(a, Object.prototype)", |
| 146 | + "return Object.freeze(a)", |
| 147 | + "return Object.seal(a)", |
| 148 | + "return Object.preventExtensions(a)", |
| 149 | + "return Array.prototype.slice.call(a)", |
| 150 | + "const o = Array.prototype.slice.call(a); o[0] = v; o.push(v); return o", |
| 151 | + "return Array.prototype.concat.call(a, [v])", |
| 152 | + "const o = Array.prototype.concat.call(a, []); o[0] = v; return o", |
| 153 | + "return [v].concat(a)", |
| 154 | + "return Array.isArray(a) ? [...a] : null", |
| 155 | + "return Array.prototype.map.call(a, x => x)", |
| 156 | + "return Array.prototype.filter.call(a, x => true)", |
| 157 | + "return Array.prototype.includes.call(a, a[1])", |
| 158 | + "return Array.prototype.includes.call(a, undefined)", |
| 159 | + "return Array.prototype.indexOf.call(a, a[2])", |
| 160 | + "return Array.prototype.join.call(a)", |
| 161 | + "return Array.prototype.with.call(a, 0, v)", |
| 162 | + "return Array.prototype.toSorted.call(a)", |
| 163 | + "return Array.from(a)", |
| 164 | + "return Object.keys(a)", |
| 165 | + "return Object.entries(a)", |
| 166 | + "return Object.assign({ }, a)", |
| 167 | + "return { ...a }", |
| 168 | + "return JSON.stringify(a)", |
| 169 | + "const keys = []; for (const key in a) keys.push(key); return keys", |
| 170 | + "return [0 in a, 1 in a, a.length in a, 'length' in a, Object.hasOwn(a, 1)]", |
| 171 | + "return [a[0], a[1], a[2], a[3], a[a.length], a.length]", |
| 172 | + "new StampField(a); a[0] = v; return StampField.has(a)", |
| 173 | + "new StampMethod(a); a[0] = v; return StampMethod.has(a)", |
| 174 | + "Species.result = a; return Species.from([v, v]).map(x => x)", |
| 175 | + "Species.result = a; return Species.from([v, v]).filter(x => true)", |
| 176 | + "Species.result = a; return Species.from([v, v]).slice(0)", |
| 177 | + "return Array.from.call(function () { return a; }, [v])", |
| 178 | + "return Array.of.call(function () { return a; }, v)", |
| 179 | + "return JSON.parse('[1,[2,3]]', function (key, value) { if (key === '0' && this !== a) this[1] = a; return value === 2 ? v : value; })", |
| 180 | +]; |
| 181 | + |
| 182 | +function show(value, self) { |
| 183 | + if (typeof value === "function") |
| 184 | + return "function"; |
| 185 | + if (typeof value !== "object" || !value) |
| 186 | + return typeof value + ":" + String(value); |
| 187 | + if (value === self) |
| 188 | + return "self"; |
| 189 | + return (Array.isArray(value) ? "array" : "object") + "{" + state(value, self) + "}"; |
| 190 | +} |
| 191 | + |
| 192 | +function state(object, self) { |
| 193 | + let result = ""; |
| 194 | + let sealed = !Object.isExtensible(object); |
| 195 | + let frozen = sealed; |
| 196 | + for (const key of Reflect.ownKeys(object)) { |
| 197 | + const descriptor = Reflect.getOwnPropertyDescriptor(object, key); |
| 198 | + if (descriptor.configurable) |
| 199 | + sealed = frozen = false; |
| 200 | + if (descriptor.writable) |
| 201 | + frozen = false; |
| 202 | + result += String(key) + "="; |
| 203 | + result += "value" in descriptor ? show(descriptor.value, self) + (descriptor.writable ? "W" : "-") : "accessor"; |
| 204 | + result += (descriptor.enumerable ? "E" : "-") + (descriptor.configurable ? "C" : "-") + ","; |
| 205 | + } |
| 206 | + result += Object.isExtensible(object) ? "X" : "-"; |
| 207 | + result += sealed ? "S" : "-"; |
| 208 | + result += frozen ? "F" : "-"; |
| 209 | + if (Object.isSealed(object) !== sealed || Object.isFrozen(object) !== frozen) |
| 210 | + throw new Error(`isSealed ${Object.isSealed(object)} and isFrozen ${Object.isFrozen(object)} for ${result}`); |
| 211 | + const prototype = Reflect.getPrototypeOf(object); |
| 212 | + result += prototype === Array.prototype ? "A" : prototype === Object.prototype ? "O" : prototype === Derived.prototype ? "D" : prototype === null ? "N" : "?"; |
| 213 | + return result; |
| 214 | +} |
| 215 | + |
| 216 | +function definePerProperty(object, frozen) { |
| 217 | + Reflect.preventExtensions(object); |
| 218 | + for (const key of Reflect.ownKeys(object)) { |
| 219 | + const descriptor = Reflect.getOwnPropertyDescriptor(object, key); |
| 220 | + Object.defineProperty(object, key, frozen && "value" in descriptor ? { writable: false, configurable: false } : { configurable: false }); |
| 221 | + } |
| 222 | + return object; |
| 223 | +} |
| 224 | + |
| 225 | +function run(operation, object) { |
| 226 | + let result; |
| 227 | + try { |
| 228 | + result = "returned " + show(operation(object, shared[3]), object); |
| 229 | + } catch (error) { |
| 230 | + result = "threw " + String(error); |
| 231 | + } |
| 232 | + return result + " / " + state(object, object); |
| 233 | +} |
| 234 | + |
| 235 | +function compile(source) { |
| 236 | + const operation = new Function("a", "v", source); |
| 237 | + noDFG(operation); |
| 238 | + return [source, operation]; |
| 239 | +} |
| 240 | + |
| 241 | +const compiledSyntaxOperations = syntaxOperations.flatMap(source => [compile(source), compile("'use strict';" + source)]); |
| 242 | +const compiledOperations = compiledSyntaxOperations.concat(callOperations.map(compile)); |
| 243 | + |
| 244 | +function test(make, compiled, frozen) { |
| 245 | + const setIntegrityLevel = frozen ? Object.freeze : Object.seal; |
| 246 | + const indexingMode = $vm.indexingMode(make()); |
| 247 | + state(make()); |
| 248 | + const expectedState = state(definePerProperty(make(), frozen)); |
| 249 | + const actualState = state(setIntegrityLevel(make())); |
| 250 | + if (actualState !== expectedState) |
| 251 | + throw new Error(`${make}: expected ${expectedState} but got ${actualState}`); |
| 252 | + if (!actualState.includes(frozen ? "F" : "S", actualState.lastIndexOf(","))) |
| 253 | + throw new Error(`${make}: bad state ${actualState}`); |
| 254 | + for (const [source, operation] of compiled) { |
| 255 | + const expected = run(operation, definePerProperty(make(), frozen)); |
| 256 | + const actual = run(operation, setIntegrityLevel(make())); |
| 257 | + if (actual !== expected) |
| 258 | + throw new Error(`${make} ${source}: expected ${expected} but got ${actual}`); |
| 259 | + } |
| 260 | + if ($vm.indexingMode(make()) !== indexingMode) |
| 261 | + throw new Error(`${make}: expected ${indexingMode} but got ${$vm.indexingMode(make())}`); |
| 262 | +} |
| 263 | + |
| 264 | +function testExistingPrototype(frozen) { |
| 265 | + for (const source of ["[1, 2, 3]", "({ 0: 1, 1: 2, 2: 3 })"]) { |
| 266 | + const realm = createGlobalObject(); |
| 267 | + const prototype = realm.eval(source); |
| 268 | + const child = Object.setPrototypeOf(realm.eval("[, , 3]"), prototype); |
| 269 | + (frozen ? Object.freeze : Object.seal)(prototype); |
| 270 | + child[0] = 42; |
| 271 | + if (Object.hasOwn(child, 0) === frozen) |
| 272 | + throw new Error(`${source}: bad own property 0 of the child`); |
| 273 | + } |
| 274 | +} |
| 275 | + |
| 276 | +function testIntegrityLevel(frozen) { |
| 277 | + for (const make of makers) |
| 278 | + test(make, compiledOperations, frozen); |
| 279 | + for (const make of sparseMakers) |
| 280 | + test(make, compiledSyntaxOperations, frozen); |
| 281 | + for (const make of partiallyFrozenMakers) |
| 282 | + test(make, [], frozen); |
| 283 | + testExistingPrototype(frozen); |
| 284 | +} |
0 commit comments