Skip to content

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() through ghostty-vt.wasm and bridge.wasm to 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
PTY bytes
|
v
libghostty-vt (ghostty-vt.wasm)
screen, modes, scrollback, damage flags
|
+---> bridge.wasm (Zig frame builder)
| -> WebGPU / WebGL2
|
+---> Direct row reader
-> Canvas 2D / DOM

Every 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.

You can adopt the stack at whichever level fits your environment:

  1. GhosttyRuntime (ghostty-webgpu/core): loads and instantiates ghostty-vt.wasm and bridge.wasm in a browser or Node with no DOM globals.
  2. GhosttyTerminal (ghostty-webgpu/core): headless terminal instance for writing bytes, resizing, reading lines, and querying geometry.
  3. TerminalSession (ghostty-webgpu/core): adds appearance management, selection history, mouse and key encoding, link providers, and typed events.
  4. Terminal (ghostty-webgpu or ghostty-webgpu/worker): mounts the DOM host, measures and fits the container synchronously during open(), 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 entry (ghostty-webgpu): after async Terminal.create() and open(), native mutations (write, setTheme, readLines) execute synchronously on the page thread. During open(), TerminalHost computes the container fit before creating the renderer so frame one lands at the true size.
  • Worker entry (ghostty-webgpu/worker): runs libghostty-vt, bridge.wasm, and the WebGPU or WebGL2 renderer inside a dedicated worker (or TerminalWorkerPool) bound to an OffscreenCanvas. The page forwards layout updates and input over MessagePort; the worker sends back compact text-row deltas only for rows whose text changed.

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 to onFrame and onText when 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-vt instance owns all terminal state across all four renderers.

  • Use ghostty-webgpu/core for headless Node or browser logic, ghostty-webgpu for main-thread UI, and ghostty-webgpu/worker for off-main-thread rendering.

  • Use readLines() for live buffer state and visibleLines() or submittedFrame for what is currently on screen.