3
0
mirror of https://github.com/larvit/adf-codec.git synced 2026-11-18 22:25:44 +01:00

Answer architecture round 1: the guard refuses only the measured overflow errors, the README keeps such a document read-only, and items 106 and 107 take the heap and the carry's indentation

This commit is contained in:
2026-10-11 20:40:21 +02:00
committed by Lillem4n
parent 4b3bce8d02
commit fdf7f1c2b7
6 changed files with 42 additions and 7 deletions
+4 -1
View File
@@ -8,7 +8,10 @@ type Pending = string | { depth: number; value: JsonValue }
export function serializeCanonicalJson(value: JsonValue, spelling: JsonSpelling): string {
const text: string[] = []
writeCanonicalJson(value, spelling, (piece) => text.push(piece) > 0)
writeCanonicalJson(value, spelling, (piece) => {
text.push(piece)
return true
})
return text.join('')
}
+6 -1
View File
@@ -32,7 +32,7 @@ test('every ConvertErrorCode is the code of a production call site, and every ca
assert.deepEqual(calledCodes(), declaredCodes())
})
test('refuses where SpiderMonkey runs out of string, and lets any other throw through', () => {
test('refuses where SpiderMonkey runs out of string, and lets a stack overflow or any other throw through', () => {
const overflow = new Error('allocation size overflow')
overflow.name = 'InternalError'
assert.deepEqual(
@@ -41,6 +41,11 @@ test('refuses where SpiderMonkey runs out of string, and lets any other throw th
}),
{ error: { code: 'unsupported-node-shape', message: 'the output runs past what this JavaScript engine holds: allocation size overflow', path: [] }, ok: false },
)
assert.throws(() =>
withinEngineLimits(() => {
throw new RangeError('Maximum call stack size exceeded')
}),
)
assert.throws(() =>
withinEngineLimits(() => {
throw new TypeError('a bug')
+5 -2
View File
@@ -42,12 +42,15 @@ export function success<T>(value: T): { ok: true; value: T } {
return { ok: true, value }
}
// Any string concatenation throws past the engine's longest string: RangeError in V8 and JavaScriptCore, InternalError in SpiderMonkey.
// Measured past the longest string or array: V8 throws the first two, JavaScriptCore the third, SpiderMonkey the fourth. A stack overflow stays a throw.
const engineLimitFaults = ['RangeError: Invalid array length', 'RangeError: Invalid string length', 'RangeError: Out of memory', 'InternalError: allocation size overflow']
// Any string concatenation can throw one, so this wraps a whole conversion.
export function withinEngineLimits<T>(convert: () => Result<T>): Result<T> {
try {
return convert()
} catch (error) {
if (!(error instanceof RangeError || (error instanceof Error && error.name === 'InternalError'))) throw error
if (!(error instanceof Error) || !engineLimitFaults.includes(`${error.name}: ${error.message}`)) throw error
return failure('unsupported-node-shape', `the output runs past what this JavaScript engine holds: ${error.message}`, [])
}
}