mirror of
https://github.com/larvit/adf-codec.git
synced 2026-11-18 22:25:44 +01:00
Answer architecture round 2: 1 🔴, 1 🟡; the CommonMark ask answers yes or no, the memo's callers name themselves, the bound's policy reads at its constant, Firefox's bare out-of-memory string is engine-limit, and the floor entry no longer points at item 110
This commit is contained in:
@@ -108,22 +108,23 @@ function interruptsParagraph(node: AdfNode): boolean {
|
||||
function emitBlock(node: AdfNode, path: ConvertErrorPath, depth: number, writing: Writing): Result<EmittedBlock> {
|
||||
const model = blockNodeModel(node.type)
|
||||
if (model === undefined) return carriedFence(carriedBlock(node, path, depth))
|
||||
const readable = readableBlock(node, path, depth, writing, false)
|
||||
const readable = readableBlock(node, path, depth, writing, 'walked')
|
||||
if (readable !== undefined) return readable
|
||||
return emitDirectiveBlock(node, model, path, depth, () => walkBlocks(nodeContent(node), path, depth + 1, writing))
|
||||
}
|
||||
|
||||
export function commonMarkSpelling(node: AdfNode, path: ConvertErrorPath, depth: number, writing: Writing & { memo: SpellingMemo }): Result<null> | undefined {
|
||||
const readable = readableBlock(node, path, depth, writing, true)
|
||||
if (readable === undefined) return undefined
|
||||
// Whether the node takes a CommonMark spelling, rather than its directive.
|
||||
export function commonMarkSpelling(node: AdfNode, path: ConvertErrorPath, depth: number, writing: Writing & { memo: SpellingMemo }): Result<boolean> {
|
||||
const readable = readableBlock(node, path, depth, writing, 'asked')
|
||||
if (readable === undefined) return success(false)
|
||||
if (!readable.ok) return readable
|
||||
const fits = withinLongest(readable.value.text)
|
||||
if (!fits.ok) return fits
|
||||
return readable.value.spelling === 'directive' ? undefined : success(null)
|
||||
return success(readable.value.spelling !== 'directive')
|
||||
}
|
||||
|
||||
// Only an asked node's spelling is kept, and reading it drops it: the walk above spells the node into its own text, so no level of a subtree holds that text twice.
|
||||
function readableBlock(node: AdfNode, path: ConvertErrorPath, depth: number, writing: Writing, asked: boolean): Result<EmittedBlock> | undefined {
|
||||
function readableBlock(node: AdfNode, path: ConvertErrorPath, depth: number, writing: Writing, caller: 'asked' | 'walked'): Result<EmittedBlock> | undefined {
|
||||
const { memo } = writing
|
||||
const kept = memo?.get(node)
|
||||
memo?.delete(node)
|
||||
@@ -133,8 +134,8 @@ function readableBlock(node: AdfNode, path: ConvertErrorPath, depth: number, wri
|
||||
if (depth <= kept.depth) return success({ ...kept.block, headroom: kept.block.headroom + kept.depth - depth })
|
||||
}
|
||||
const spelled = spellReadableBlock(node, path, depth, writing)
|
||||
if (asked && spelled === undefined) memo?.set(node, { block: undefined, depth })
|
||||
if (asked && spelled?.ok === true) memo?.set(node, { block: spelled.value, depth })
|
||||
if (caller === 'asked' && spelled === undefined) memo?.set(node, { block: undefined, depth })
|
||||
if (caller === 'asked' && spelled?.ok === true) memo?.set(node, { block: spelled.value, depth })
|
||||
return spelled
|
||||
}
|
||||
|
||||
|
||||
@@ -39,6 +39,12 @@ test('refuses a string past the bound as too long on every engine, and any other
|
||||
assert.equal(code('Map maximum size exceeded'), 'engine-limit')
|
||||
assert.equal(code('Set maximum size exceeded'), 'engine-limit')
|
||||
assert.equal(code('invalid array length'), 'engine-limit')
|
||||
assert.deepEqual(
|
||||
withinEngineLimits(() => {
|
||||
throw 'out of memory'
|
||||
}),
|
||||
{ error: { code: 'engine-limit', message: 'the conversion passed this JavaScript engine\'s longest string or array, or ran it out of memory (out of memory)', path: [] }, ok: false },
|
||||
)
|
||||
assert.deepEqual(
|
||||
withinEngineLimits(() => success('x')),
|
||||
{ ok: true, value: 'x' },
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { failure, success, type Result } from '../result.ts'
|
||||
|
||||
// V8's longest string (docs/decisions.md §Markdown past V8's longest string is refused on every engine).
|
||||
// V8's longest string, refused on every engine so a document converts alike everywhere (docs/decisions.md §Markdown past V8's longest string is refused on every engine).
|
||||
export const longestMarkdown = 2 ** 29 - 24
|
||||
|
||||
// Measured on Firefox 155, Node 24 and Bun 1.4, and SpiderMonkey's array length from its js.msg. SpiderMonkey's overflow and V8's string overflow both mean a string past longestMarkdown; the others name an engine's own limit or its memory running short.
|
||||
@@ -38,11 +38,17 @@ export function withinEngineLimits<T>(convert: () => Result<T>): Result<T> {
|
||||
try {
|
||||
return convert()
|
||||
} catch (error) {
|
||||
// SpiderMonkey throws a bare string where it finds no memory for a buffer (measured on Firefox 155).
|
||||
if (error === 'out of memory') return engineLimit(error)
|
||||
if (!(error instanceof Error)) throw error
|
||||
const thrown = `${error.name}: ${error.message}`
|
||||
if (pastLongestString.includes(thrown)) return tooLong()
|
||||
// A stack overflow is a bug (docs/decisions.md §Nothing recurses unbounded), so it stays a throw.
|
||||
if (!pastEngineLimit.includes(thrown)) throw error
|
||||
return failure('engine-limit', `the conversion passed this JavaScript engine's longest string or array, or ran it out of memory (${error.message})`, [])
|
||||
return engineLimit(error.message)
|
||||
}
|
||||
}
|
||||
|
||||
function engineLimit<T>(thrown: string): Result<T> {
|
||||
return failure('engine-limit', `the conversion passed this JavaScript engine's longest string or array, or ran it out of memory (${thrown})`, [])
|
||||
}
|
||||
|
||||
@@ -263,9 +263,9 @@ function directiveNode(block: DirectiveBlock, reading: Reading, path: ConvertErr
|
||||
const built = directiveBody(read.value, block.blocks, reading, path, depth)
|
||||
if (!built.ok) return built
|
||||
// Refused here so V8's throw past the bound is placed at this directive, like the bound check on other engines.
|
||||
const readable = withinEngineLimits<null | undefined>(() => commonMarkSpelling(built.value, path, depth, { flavour: 'lossless', memo: reading.memo }) ?? success(undefined))
|
||||
const readable = withinEngineLimits(() => commonMarkSpelling(built.value, path, depth, { flavour: 'lossless', memo: reading.memo }))
|
||||
if (!readable.ok) return readable
|
||||
if (readable.value === undefined) return built
|
||||
if (!readable.value) return built
|
||||
return failure('unsupported-node-shape', `${built.value.type} takes the CommonMark spelling, not the directive form`, path)
|
||||
}
|
||||
|
||||
|
||||
@@ -131,9 +131,9 @@ function portableSequence(blocks: readonly Sourced[], reduction: Reduction): Res
|
||||
for (let next = pending.pop(); next !== undefined; next = pending.pop()) {
|
||||
const block = numberedPastMarkers(next.block)
|
||||
const listed = block.type === 'bulletList' || block.type === 'orderedList'
|
||||
const spelled = listed && block === next.block ? commonMarkSpelling(block, next.path, reduction.depth, { flavour: 'portable', memo: reduction.memo }) : undefined
|
||||
if (spelled !== undefined && !spelled.ok) return spelled
|
||||
if (!listed || spelled !== undefined) {
|
||||
const spelled = listed && block === next.block ? commonMarkSpelling(block, next.path, reduction.depth, { flavour: 'portable', memo: reduction.memo }) : success(!listed)
|
||||
if (!spelled.ok) return spelled
|
||||
if (spelled.value) {
|
||||
sequence.push(block)
|
||||
continue
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user