3
0
mirror of https://github.com/larvit/adf-codec.git synced 2026-11-19 14:45:44 +01:00

Return a result where a conversion's text would pass the engine's longest string

This commit is contained in:
2026-10-11 20:30:42 +02:00
parent 0b3f6e8973
commit 775528f9fd
14 changed files with 105 additions and 30 deletions
+10
View File
@@ -41,3 +41,13 @@ export function positioned<T>(result: Result<T>, position: SourcePosition): Resu
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.
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
return failure('unsupported-node-shape', `the output runs past what this JavaScript engine holds: ${error.message}`, [])
}
}