How the WASM core works
ghostty-webgpu keeps all terminal semantics inside Ghostty’s unpatched libghostty-vt compiled to WebAssembly. TypeScript handles only browser integration: DOM measurement, keyboard and pointer events, font rasterization, and submitting frames to the GPU, Canvas, or DOM.
You will learn
How bytes travel from
write()throughghostty-vt.wasmandbridge.wasmto the screen- The four layered APIs from headless core to mounted DOM or worker terminal
- How the main-thread entry and worker entry divide work across threads
- The difference between live emulator state and submitted visual frames
Data flow from bytes to pixels
Section titled “Data flow from bytes to pixels”PTY bytes | vlibghostty-vt (ghostty-vt.wasm) screen, modes, scrollback, damage flags | +---> bridge.wasm (Zig frame builder) | -> WebGPU / WebGL2 | +---> Direct row reader -> Canvas 2D / DOMEvery escape sequence, line-wrapping rule, resize reflow, selection coordinate, and Unicode width comes straight from libghostty-vt. Because every renderer and every read API inspects that same native state, handing off from Canvas 2D to WebGPU at startup or recovering from a lost GPU device never loses screen text or scrollback.
Four layers of abstraction
Section titled “Four layers of abstraction”You can adopt the stack at whichever level fits your environment:
GhosttyRuntime(ghostty-webgpu/core): loads and instantiatesghostty-vt.wasmandbridge.wasmin a browser or Node with no DOM globals.GhosttyTerminal(ghostty-webgpu/core): headless terminal instance for writing bytes, resizing, reading lines, and querying geometry.TerminalSession(ghostty-webgpu/core): adds appearance management, selection history, mouse and key encoding, link providers, and typed events.Terminal(ghostty-webgpuorghostty-webgpu/worker): mounts the DOM host, measures and fits the container synchronously duringopen(), wires focus, IME, pointer, scrollbar, and accessibility, and drives the renderer.
Why libghostty-vt stays unpatched upstream
Maintaining a forked VT parser drifts from upstream fixes quickly. The checked-in ghostty-vt.wasm binary is compiled cleanly from the exact upstream Ghostty commit, Zig compiler version, and SHA-256 hashes recorded in ghostty-vt.provenance.json. See ADR 001 for the full rationale.
Main thread vs. worker boundary
Section titled “Main thread vs. worker boundary”- Main entry (
ghostty-webgpu): after asyncTerminal.create()andopen(), native mutations (write,setTheme,readLines) execute synchronously on the page thread. Duringopen(),TerminalHostcomputes the container fit before creating the renderer so frame one lands at the true size. - Worker entry (
ghostty-webgpu/worker): runslibghostty-vt,bridge.wasm, and the WebGPU or WebGL2 renderer inside a dedicated worker (orTerminalWorkerPool) bound to anOffscreenCanvas. The page forwards layout updates and input overMessagePort; the worker sends back compact text-row deltas only for rows whose text changed.
Live state vs. submitted frames
Section titled “Live state vs. submitted frames”terminal.write() parses bytes into libghostty-vt right away, while painting coalesces onto the scheduler’s animation frame (unless presentation: 'immediate' is enabled).
- Call
readLines()when you want the exact emulator buffer right now. - Read
visibleLines(),submittedFrame, or subscribe toonFrameandonTextwhen you want the coherent frame last painted to the screen.
Explore the generated classes in the API reference: GhosttyRuntime, GhosttyTerminal, TerminalSession, and Terminal.
Recap
One unpatched
libghostty-vtinstance owns all terminal state across all four renderers.Use
ghostty-webgpu/corefor headless Node or browser logic,ghostty-webgpufor main-thread UI, andghostty-webgpu/workerfor off-main-thread rendering.Use
readLines()for live buffer state andvisibleLines()orsubmittedFramefor what is currently on screen.