fix(code-runtime): accept deeply nested JSON

This commit is contained in:
Tianyi Cui
2026-07-22 02:13:37 +08:00
parent 3f93d4ddbd
commit f2d86b232e
7 changed files with 119 additions and 44 deletions
@@ -74,6 +74,26 @@ describe('WorkerCodeRuntime — programs and bindings (real workers)', () => {
expect(calls).toEqual([{ n: 1 }])
})
it('bridges a deeply nested lossless JSON argument, resolution, and completion', async () => {
const { runtime } = await setup()
const result = await runtime.run({
program: `
let value = 'leaf';
for (let depth = 0; depth < 3_000; depth++) value = [value];
return await tools.echo(value);
`,
bindings: tools({ echo: async args => args }),
})
expect(result.error).toBeUndefined()
let cursor = result.value
for (let depth = 0; depth < 3_000; depth++) {
expect(Array.isArray(cursor)).toBe(true)
cursor = Array.isArray(cursor) ? cursor[0] : undefined
}
expect(cursor).toBe('leaf')
})
it('reports non-erasable syntax as an exception without spawning a worker', async () => {
const { runtime } = await setup()
const result = await runtime.run({ program: 'enum E { A }\nreturn 1', bindings: [] })
@@ -64,6 +64,18 @@ describe('snapshotCodeJsonValue', () => {
expect(snapshot[0]?.['__proto__']).toEqual({ safe: true })
})
it('accepts deeply nested valid JSON without using the JavaScript call stack', () => {
let value: unknown = 'leaf'
for (let depth = 0; depth < 5_000; depth++) value = [value]
let cursor = snapshotCodeJsonValue(value)
for (let depth = 0; depth < 5_000; depth++) {
expect(Array.isArray(cursor)).toBe(true)
cursor = Array.isArray(cursor) ? cursor[0] : undefined
}
expect(cursor).toBe('leaf')
})
it('rejects exotic containers, sparse arrays, cycles, and invalid children', () => {
class ExoticObject {
readonly value = 1