mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-29 11:42:17 +00:00
Mutating Array methods run the reference's check of what their first write would hit: adding to a non-extensible array, deleting from a sealed one, or writing a frozen one each fail with the reference's message, sealed arrays no longer pop or shift, and reverse leaves a frozen array of fewer than two items alone. sort checks its comparator first and leaves the array as it was when the comparator throws; it used to empty it. Callback methods of Array, Map, Set and Uint8Array.sort name themselves in the reference's 'Array.map expects a function callback.' forEach, map, filter, some, every, flatMap and reduceRight skip indexes a callback cut off by shrinking the array, and includes and indexOf on an empty array return before converting fromIndex. Array.from checks its mapper first, maps an iterable as it reads it and closes the iterator when the mapper throws (an endless iterator ran out of memory), and rejects primitives and promises. Object.fromEntries reads its entries one at a time, closes the iterator on a bad entry, and accepts only entries without opaque references, like the reference. Array.prototype is an array, and an arguments object is never frozen merely by being non-extensible. On top of the array holes: mutating methods check mutability once (push returns at once for an array with no such flags), and an index read past an array's end or at a hole finds Array.prototype's own items, which store an index without interning it. Touches ops.rs (inherited_elem).
31 lines
2.4 KiB
JavaScript
31 lines
2.4 KiB
JavaScript
const out = []
|
|
for (const [label, a] of [["one", Object.freeze([1])], ["empty", Object.freeze([])], ["two", Object.freeze([1, 2])], ["sealed1", Object.seal([1])], ["ne1", Object.preventExtensions([1])]]) {
|
|
try { a.reverse(); out.push(label + " ok") } catch (e) { out.push(label + " " + e.message) }
|
|
}
|
|
try { Object.fromEntries(["ab"]) } catch (e) { out.push(e.constructor.name + ": " + e.message) }
|
|
try { Object.fromEntries([null]) } catch (e) { out.push(e.constructor.name + ": " + e.message) }
|
|
let steps = 0, closed = 0
|
|
const it = { [Symbol.iterator]() { return { next() { steps++; return { value: "x", done: false } }, return() { closed++; return {} } } } }
|
|
try { Object.fromEntries(it) } catch (e) { out.push(e.message, steps, closed) }
|
|
steps = 0; closed = 0
|
|
try { Array.from(it, () => { throw new Error("map") }) } catch (e) { out.push(e.message, steps, closed) }
|
|
const src = [127, 4, 8]
|
|
out.push(JSON.stringify(Array.from(src, (v, i) => { if (i + 1 < src.length) src[i + 1] = 127; return v })))
|
|
const ar = (function () { return arguments })(1, 2)
|
|
Object.preventExtensions(ar)
|
|
out.push(Object.isFrozen(ar), Object.isSealed(ar), Object.isFrozen(Object.preventExtensions((function () { return arguments })())))
|
|
out.push(JSON.stringify(Object.fromEntries([["a", 1], new Map([[0, "k"], [1, "v"]])])))
|
|
|
|
|
|
for (const a of [[3, 5], [5], [[1], 7], [{ length: 2 }, "x"]]) { try { out.push(JSON.stringify(Array.from(...a))) } catch (e) { out.push(e.message) } } try { Object.fromEntries(5) } catch (e) { out.push(e.message) } try { Object.fromEntries([() => 1]) ; out.push("fn ok") } catch (e) { out.push(e.message) }
|
|
|
|
for (const a of [[function (a, b) {}], [new Date(0)], [new Error("x")], [Promise.resolve(1)], [{ length: 3, 1: "b" }, (v, i) => v ?? i], [new Map([[1, 2]])], ["héllo", (c) => c.toUpperCase()], [new Set([1, 2]).values()], [null], [undefined], [true], [{ length: -1 }], [{ length: "2" }], [[1, 2], undefined], [[1, 2], (x) => x * 2, null]]) {
|
|
try { out.push(JSON.stringify(Array.from(...a))) } catch (e) { out.push(e.constructor.name + ": " + e.message) }
|
|
}
|
|
const gg = (function* () { yield 1; yield 2 })()
|
|
out.push(JSON.stringify(Array.from(gg, (x) => x + 1)))
|
|
out.push(JSON.stringify(Object.fromEntries(new Map([["a", 1], [2, "b"]]))))
|
|
try { Object.fromEntries([[{ toString() { return "k" } }, 1]]) } catch (e) { out.push(e.message) }
|
|
out.push(JSON.stringify(Object.fromEntries([[{ a: 1 }, 1], [[1, 2], 2], [null, 3], [undefined, 4], [5, 6]])))
|
|
return out
|
|
|