|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import test from 'node:test'; |
| 3 | +import { CollectionBudget, ExpressionBudget } from '../src/lang/expression-budget.js'; |
| 4 | +import { resolveReferences } from '../src/lang/graph.js'; |
| 5 | + |
| 6 | +function copy(source: unknown[], slots = 1_048_576): unknown[] { |
| 7 | + return resolveReferences( |
| 8 | + { $ref: 'source' }, |
| 9 | + new Map([['source', source]]), |
| 10 | + new CollectionBudget(slots, 'fixture budget'), |
| 11 | + ) as unknown[]; |
| 12 | +} |
| 13 | + |
| 14 | +test('graph and expression array copies keep native cloning without allocating one descriptor per slot', (t) => { |
| 15 | + const source = Array.from<unknown>({ length: 64 }).fill(1); |
| 16 | + source.splice(8, 8, false, -0, NaN, 3n, '', null, undefined, 'text'); |
| 17 | + const native = t.mock.method(globalThis, 'structuredClone'); |
| 18 | + const describe = t.mock.method(Object, 'getOwnPropertyDescriptor'); |
| 19 | + const a = copy(source, source.length); |
| 20 | + const b = new ExpressionBudget().copy(source) as unknown[]; |
| 21 | + assert.notEqual(a, b); |
| 22 | + assert.notEqual(a, source); |
| 23 | + assert.deepEqual(a, source); |
| 24 | + assert.deepEqual(b, source); |
| 25 | + a[0] = 7; |
| 26 | + assert.equal(b[0], 1); |
| 27 | + assert.equal(source[0], 1); |
| 28 | + assert.equal(native.mock.calls.filter(({ arguments: args }) => args[0] === source).length, 2); |
| 29 | + if (typeof Reflect.get(Object.prototype, '__lookupGetter__') === 'function') |
| 30 | + assert.equal(describe.mock.calls.filter(({ arguments: args }) => args[0] === source).length, 0); |
| 31 | +}); |
| 32 | + |
| 33 | +test('flat template arrays pay the exact second charge without a second walk', (t) => { |
| 34 | + const source = Array.from({ length: 64 }, (_, i) => i); |
| 35 | + const scope = new Map([['source', source]]); |
| 36 | + const template = { $template: '{{source}}' }; |
| 37 | + const budget = new CollectionBudget(2 * source.length, 'template slots'); |
| 38 | + const walk = t.mock.method(budget, 'visitShape'); |
| 39 | + assert.equal(resolveReferences(template, scope, budget), JSON.stringify(source)); |
| 40 | + if (typeof Reflect.get(Object.prototype, '__lookupGetter__') === 'function') |
| 41 | + assert.equal(walk.mock.callCount(), 1); |
| 42 | + assert.throws( |
| 43 | + () => resolveReferences(template, scope, new CollectionBudget(127, 'template slots')), |
| 44 | + { |
| 45 | + code: 'expression_budget_exceeded', |
| 46 | + }, |
| 47 | + ); |
| 48 | +}); |
| 49 | + |
| 50 | +test('a template checks the cloned descendants when an indexed getter supplies an object', () => { |
| 51 | + let reads = 0; |
| 52 | + const source = Array(4).fill(1); |
| 53 | + Object.defineProperty(source, 2, { |
| 54 | + enumerable: true, |
| 55 | + get() { |
| 56 | + reads++; |
| 57 | + return { n: [2, 3] }; |
| 58 | + }, |
| 59 | + }); |
| 60 | + const scope = new Map([['source', source]]); |
| 61 | + const render = (slots: number) => |
| 62 | + resolveReferences( |
| 63 | + { $template: '{{source}}' }, |
| 64 | + scope, |
| 65 | + new CollectionBudget(slots, 'template slots'), |
| 66 | + ); |
| 67 | + assert.equal(render(11), '[1,1,{"n":[2,3]},1]'); |
| 68 | + assert.throws(() => render(10), { code: 'expression_budget_exceeded' }); |
| 69 | + assert.equal(reads, 2); |
| 70 | +}); |
| 71 | + |
| 72 | +test('mixed arrays keep alias and cycle identity and charge each nested object once per copy', () => { |
| 73 | + const leaf = { n: 1 }; |
| 74 | + const source: unknown[] = Array(32).fill(null); |
| 75 | + source[27] = leaf; |
| 76 | + source[28] = leaf; |
| 77 | + source[31] = source; |
| 78 | + const cloned = copy(source, 33); |
| 79 | + assert.equal(cloned[27], cloned[28]); |
| 80 | + assert.notEqual(cloned[27], leaf); |
| 81 | + assert.equal(cloned[31], cloned); |
| 82 | + assert.throws(() => copy(source, 32), { code: 'expression_budget_exceeded' }); |
| 83 | +}); |
| 84 | + |
| 85 | +test('copy preflight does not read accessors and native cloning reads them once in order', () => { |
| 86 | + const reads: number[] = []; |
| 87 | + const source: unknown[] = Array(64).fill(0); |
| 88 | + Object.defineProperty(source, 10, { |
| 89 | + enumerable: true, |
| 90 | + get() { |
| 91 | + reads.push(10); |
| 92 | + source[11] = 'changed'; |
| 93 | + return 10; |
| 94 | + }, |
| 95 | + }); |
| 96 | + Object.defineProperty(source, 37, { |
| 97 | + enumerable: true, |
| 98 | + get() { |
| 99 | + reads.push(37); |
| 100 | + return 37; |
| 101 | + }, |
| 102 | + }); |
| 103 | + source[63] = { nested: true }; |
| 104 | + assert.throws(() => copy(source, 64), { code: 'expression_budget_exceeded' }); |
| 105 | + assert.deepEqual(reads, []); |
| 106 | + const cloned = copy(source, 65); |
| 107 | + assert.deepEqual(reads, [10, 37]); |
| 108 | + assert.equal(cloned[10], 10); |
| 109 | + assert.equal(cloned[11], 'changed'); |
| 110 | + assert.equal(cloned[37], 37); |
| 111 | +}); |
| 112 | + |
| 113 | +test('sparse, non-enumerable and named array properties retain native clone behavior', () => { |
| 114 | + const sparse: unknown[] = Array(64); |
| 115 | + sparse[9] = 'only'; |
| 116 | + Object.defineProperty(sparse, 12, { value: { field: true } }); |
| 117 | + Object.defineProperty(sparse, '__proto__', { enumerable: true, value: { tag: 'own' } }); |
| 118 | + Object.defineProperty(sparse, '062', { enumerable: true, value: 'named' }); |
| 119 | + assert.deepEqual(copy(sparse), structuredClone(sparse)); |
| 120 | + assert.throws(() => copy(sparse, 64), { code: 'expression_budget_exceeded' }); |
| 121 | + const noncloneable = Array(32).fill(1); |
| 122 | + noncloneable[31] = Symbol('not cloneable'); |
| 123 | + assert.throws(() => copy(noncloneable), { name: 'DataCloneError' }); |
| 124 | + assert.equal(new ExpressionBudget().copy(noncloneable), null); |
| 125 | + let proxyReads = 0; |
| 126 | + const proxy = new Proxy(Array(32).fill(1), { |
| 127 | + get(target, key, receiver) { |
| 128 | + if (key !== 'length') proxyReads++; |
| 129 | + return Reflect.get(target, key, receiver); |
| 130 | + }, |
| 131 | + }); |
| 132 | + assert.throws(() => copy(proxy), { name: 'DataCloneError' }); |
| 133 | + assert.equal(proxyReads, 0); |
| 134 | +}); |
| 135 | + |
| 136 | +test('a named field cannot disguise a hole or cause an inherited getter to be read', () => { |
| 137 | + const source = Array<unknown>(32).fill(0); |
| 138 | + delete source[7]; |
| 139 | + Object.defineProperty(source, '007', { enumerable: true, value: 'named' }); |
| 140 | + const proto = Object.create(Array.prototype); |
| 141 | + Object.defineProperty(proto, 7, { get: () => assert.fail('inherited getter ran') }); |
| 142 | + Object.setPrototypeOf(source, proto); |
| 143 | + assert.equal(Object.keys(source).length, source.length); |
| 144 | + const cloned = copy(source, 32); |
| 145 | + assert.equal(Object.hasOwn(cloned, 7), false); |
| 146 | + assert.equal(Object.getOwnPropertyDescriptor(cloned, '007')?.value, 'named'); |
| 147 | + assert.equal(Object.getPrototypeOf(cloned), Array.prototype); |
| 148 | +}); |
0 commit comments