mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-29 11:42:17 +00:00
The reference keeps an array's elements in a host JavaScript array, which
JavaScriptCore stores sparsely, so a[4294967294] = x works and makes the
length 4294967295. Here a write that far failed with 'Invalid array length'.
An array now turns sparse when a write lands past the longest dense array
(MAX_ARRAY_LEN): its items keep the dense prefix, the SPARSE object flag is
set, and its other elements and its length are its own properties, so
snapshots and reachability carry them unchanged. Element reads and writes,
in, hasOwnProperty, delete, keys, for-in, length (which makes it dense again
once short enough), push, pop, at, slice, includes, indexOf and lastIndexOf
work on it as in the reference, and the callback methods, reduce, flat and
flatMap visit only the elements it holds. Code that walks every element
makes the array dense first and fails with the reference's errors when it
is too long ('Invalid array length', 'Out of memory' for sort), where the
reference either fails or runs on for minutes. Logs show its first
elements. Arrays at most MAX_ARRAY_LEN long, every one the benchmarks make
among them, stay dense and take the same paths as before.
Setting length out of range and spreading too many values now say
'Invalid array length' without a period, as the reference does. Touches
object.rs (SPARSE), ops.rs (element and length paths, define, iteration),
convert.rs, host.rs, web.rs (structured clone), object.rs, buffer.rs.
The last nine test262 files leave rust-skipped.txt, which is now empty.
35 lines
2.4 KiB
JavaScript
35 lines
2.4 KiB
JavaScript
const out = []
|
|
const t = (label, f) => { try { const r = f(); out.push(label + " " + JSON.stringify(r)) } catch (e) { out.push(label + " " + e.constructor.name + ": " + e.message) } }
|
|
const mk = () => { const a = [1, 2]; a[4294967294] = "end"; a.x = 1; return a }
|
|
t("len", () => mk().length)
|
|
t("get", () => { const a = mk(); return [a[0], a[2], a[4294967294], a[4294967293], 4294967294 in a, 3 in a, a.hasOwnProperty(4294967294)] })
|
|
t("keys", () => Object.keys(mk()))
|
|
t("forin", () => { const r = []; for (const k in mk()) r.push(k); return r })
|
|
t("entries", () => Object.entries(mk()))
|
|
t("del", () => { const a = mk(); delete a[4294967294]; return [a.length, a[4294967294], Object.keys(a)] })
|
|
t("setlen", () => { const a = mk(); a.length = 3; return [a.length, a[4294967294], Object.keys(a)] })
|
|
t("setlenbig", () => { const a = mk(); a.length = 4294967295; return a.length })
|
|
t("setlenmid", () => { const a = mk(); a.length = 20000000; return a.length })
|
|
t("write2", () => { const a = mk(); a[10] = 5; a[4294967293] = 6; return [a.length, a[10], a[9], Object.keys(a)] })
|
|
t("spread", () => [...mk()].length)
|
|
t("map", () => mk().map((x) => x).length)
|
|
t("slice", () => mk().slice(0, 3))
|
|
t("sliceEnd", () => mk().slice(-1))
|
|
t("push", () => { const a = mk(); return [a.push(9), a.length] })
|
|
t("push2", () => { const a = []; a[4294967294] = 1; return a.push(1, 2) })
|
|
t("pop", () => { const a = mk(); return [a.pop(), a.length] })
|
|
t("at", () => mk().at(-1))
|
|
t("find", () => mk().findLast((x) => x === "end"))
|
|
t("sort", () => mk().sort().length)
|
|
t("fill", () => mk().fill(0, 0, 2).slice(0, 2))
|
|
t("isArray", () => Array.isArray(mk()))
|
|
t("structured", () => structuredClone(mk()).length)
|
|
t("assign", () => Object.keys(Object.assign({}, mk())))
|
|
t("objspread", () => Object.keys({ ...mk() }))
|
|
t("freeze", () => { const a = mk(); Object.freeze(a); return Object.isFrozen(a) })
|
|
t("mid", () => { const a = []; a[20000000] = 1; return [a.length, a[20000000]] })
|
|
t("mid2", () => { const a = []; a[5000000] = 1; return a.length })
|
|
t("shrink", () => { const a = []; a[30000000] = "z"; a[5] = "f"; a.length = 10; return [a.length, a[5], Object.keys(a), JSON.stringify(a)] })
|
|
t("pushok", () => { const a = []; a[30000000] = 1; return [a.push(2, 3), a.length, a[30000002], a.pop(), a.length] })
|
|
t("indexOf", () => { const a = mk(); return [a.indexOf("end", 4294967290), a.indexOf(true, Infinity), a.lastIndexOf("end", 4294967294), a.lastIndexOf(2, -4294967294), a.indexOf("end", -1), a.includes("end", 4294967290)] })
|
|
return out
|