opencode/packages/codemode-rust/conformance/cases/std-json-stringify-depth.js
Sebastian Herrlinger a7f1c106a1 fix(codemode): JSON.stringify stops at the reference's depths
The reference's native stringify throws past depth 32 and its internal walk, with a replacer or a property list, past 100, a primitive included: RangeError "Value exceeds the maximum depth of N.". Rust allowed 512. Recorded in the std-json-stringify-depth case and a busy loop of failing walks.
2026-11-18 18:56:12 +01:00

28 lines
981 B
JavaScript

const nest = (n, v, wrap) => {
let o = v
for (let i = 0; i < n; i++) o = wrap(o)
return o
}
const t = (f) => {
try {
return f()
} catch (e) {
return [e.name, e.message]
}
}
const obj = (o) => ({ o })
const arr = (o) => [o]
return [
t(() => JSON.stringify(nest(32, 1, obj)).length),
t(() => JSON.stringify(nest(33, 1, obj)).length),
t(() => JSON.stringify(nest(32, {}, obj)).length),
t(() => JSON.stringify(nest(33, {}, obj)).length),
t(() => JSON.stringify(nest(32, 1, arr)).length),
t(() => JSON.stringify(nest(33, 1, arr)).length),
t(() => JSON.stringify(nest(33, undefined, obj)).length),
t(() => JSON.stringify(nest(100, 1, obj), (k, v) => v).length),
t(() => JSON.stringify(nest(101, 1, obj), (k, v) => v).length),
t(() => JSON.stringify(nest(40, 1, obj), null, 1).length),
t(() => JSON.stringify(nest(40, { toJSON: () => 1 }, obj)).length),
t(() => JSON.stringify([nest(20, { toJSON: () => 1 }, obj), nest(40, 1, obj)]).length),
]