fix(web): render oversized terminal graphemes without crashing (#7809)

Co-authored-by: Simone <[email protected]>
This commit is contained in:
SimoneandSimone authored and GitHub committed 2026-08-21 17:02:04 -04:00
1 parent 9b5d416872
commit c3e37094e0
2 files changed
+58 -5

No files matched your search

@@ -0,0 +1,36 @@
import { describe, expect, it } from "vite-plus/test";
import { ghosttyCellText } from "./core";
function codepointView(codepoints: ReadonlyArray<number>): DataView {
const view = new DataView(new ArrayBuffer(codepoints.length * 4));
codepoints.forEach((codepoint, index) => view.setUint32(index * 4, codepoint, true));
return view;
}
describe("ghosttyCellText", () => {
it("converts oversized grapheme clusters without hitting engine spread limits", () => {
// A program printing one base character followed by a huge run of
// combining marks packs the whole cluster into one cell; spreading that
// many arguments into String.fromCodePoint once overflows the call stack.
const graphemeLength = 130_000;
const view = new DataView(new ArrayBuffer(graphemeLength * 4));
for (let index = 0; index < graphemeLength; index += 1) {
view.setUint32(index * 4, index === 0 ? "a".codePointAt(0)! : 0x301, true);
}
const text = ghosttyCellText(view, graphemeLength);
expect(text.length).toBe(graphemeLength);
expect(text.codePointAt(0)).toBe("a".codePointAt(0));
expect(text.codePointAt(1)).toBe(0x301);
expect(text.codePointAt(graphemeLength - 1)).toBe(0x301);
});
it("converts small clusters including astral codepoints", () => {
const text = ghosttyCellText(codepointView([0x1f642, 0x20e3]), 2);
expect([...text]).toEqual(["\u{1F642}", "\u{20E3}"]);
});
it("returns an empty string for empty cells", () => {
expect(ghosttyCellText(codepointView([]), 0)).toBe("");
});
});
+22 -5
View File
@@ -166,6 +166,27 @@ function sameColor(left: GhosttyColor, right: GhosttyColor): boolean {
return left.r === right.r && left.g === right.g && left.b === right.b;
}
/**
* A terminal program can print one base character followed by a huge run of
* combining marks, packing hundreds of thousands of codepoints into a single
* cell that still fits the scrollback buffer. Engines cap spread-call
* arguments far below that, so convert in bounded chunks instead of spreading
* every codepoint into String.fromCodePoint at once.
*/
export function ghosttyCellText(codepointView: DataView, graphemeLength: number): string {
const CHUNK_SIZE = 4_096;
let text = "";
for (let start = 0; start < graphemeLength; start += CHUNK_SIZE) {
const count = Math.min(CHUNK_SIZE, graphemeLength - start);
const codes = new Array<number>(count);
for (let index = 0; index < count; index += 1) {
codes[index] = codepointView.getUint32((start + index) * 4, true);
}
text += String.fromCodePoint(...codes);
}
return text;
}
export class GhosttyTerminalCore {
private readonly runtime: GhosttyRuntime;
private terminalSlot = 0;
@@ -960,11 +981,7 @@ export class GhosttyTerminalCore {
// Read through a DataView: the byte-array allocator guarantees no
// 4-byte alignment, which a Uint32Array view would require.
const codepointView = this.runtime.view(codepoints, bufferSize);
const codes: number[] = [];
for (let index = 0; index < graphemeLength; index += 1) {
codes.push(codepointView.getUint32(index * 4, true));
}
text = String.fromCodePoint(...codes);
text = ghosttyCellText(codepointView, graphemeLength);
}
this.runtime.free(codepoints, bufferSize);
}