@gkoreli/ghostty-vt-js 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (201) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +89 -0
  3. package/THIRD_PARTY_NOTICES.md +45 -0
  4. package/dist/browser-terminal/buffer.d.ts +93 -0
  5. package/dist/browser-terminal/buffer.d.ts.map +1 -0
  6. package/dist/browser-terminal/buffer.js +337 -0
  7. package/dist/browser-terminal/buffer.js.map +1 -0
  8. package/dist/browser-terminal/encoding.d.ts +17 -0
  9. package/dist/browser-terminal/encoding.d.ts.map +1 -0
  10. package/dist/browser-terminal/encoding.js +22 -0
  11. package/dist/browser-terminal/encoding.js.map +1 -0
  12. package/dist/browser-terminal/event-emitter.d.ts +8 -0
  13. package/dist/browser-terminal/event-emitter.d.ts.map +1 -0
  14. package/dist/browser-terminal/event-emitter.js +27 -0
  15. package/dist/browser-terminal/event-emitter.js.map +1 -0
  16. package/dist/browser-terminal/frame-hold.d.ts +13 -0
  17. package/dist/browser-terminal/frame-hold.d.ts.map +1 -0
  18. package/dist/browser-terminal/frame-hold.js +17 -0
  19. package/dist/browser-terminal/frame-hold.js.map +1 -0
  20. package/dist/browser-terminal/geometry.d.ts +229 -0
  21. package/dist/browser-terminal/geometry.d.ts.map +1 -0
  22. package/dist/browser-terminal/geometry.js +299 -0
  23. package/dist/browser-terminal/geometry.js.map +1 -0
  24. package/dist/browser-terminal/ghostty.d.ts +478 -0
  25. package/dist/browser-terminal/ghostty.d.ts.map +1 -0
  26. package/dist/browser-terminal/ghostty.js +1212 -0
  27. package/dist/browser-terminal/ghostty.js.map +1 -0
  28. package/dist/browser-terminal/gpu/adapter.d.ts +70 -0
  29. package/dist/browser-terminal/gpu/adapter.d.ts.map +1 -0
  30. package/dist/browser-terminal/gpu/adapter.js +260 -0
  31. package/dist/browser-terminal/gpu/adapter.js.map +1 -0
  32. package/dist/browser-terminal/gpu/core/canvas-glyph-rasterizer.d.ts +33 -0
  33. package/dist/browser-terminal/gpu/core/canvas-glyph-rasterizer.d.ts.map +1 -0
  34. package/dist/browser-terminal/gpu/core/canvas-glyph-rasterizer.js +159 -0
  35. package/dist/browser-terminal/gpu/core/canvas-glyph-rasterizer.js.map +1 -0
  36. package/dist/browser-terminal/gpu/core/context-loss-watchdog.d.ts +35 -0
  37. package/dist/browser-terminal/gpu/core/context-loss-watchdog.d.ts.map +1 -0
  38. package/dist/browser-terminal/gpu/core/context-loss-watchdog.js +73 -0
  39. package/dist/browser-terminal/gpu/core/context-loss-watchdog.js.map +1 -0
  40. package/dist/browser-terminal/gpu/core/frame.d.ts +68 -0
  41. package/dist/browser-terminal/gpu/core/frame.d.ts.map +1 -0
  42. package/dist/browser-terminal/gpu/core/frame.js +167 -0
  43. package/dist/browser-terminal/gpu/core/frame.js.map +1 -0
  44. package/dist/browser-terminal/gpu/core/glyph-atlas.d.ts +136 -0
  45. package/dist/browser-terminal/gpu/core/glyph-atlas.d.ts.map +1 -0
  46. package/dist/browser-terminal/gpu/core/glyph-atlas.js +281 -0
  47. package/dist/browser-terminal/gpu/core/glyph-atlas.js.map +1 -0
  48. package/dist/browser-terminal/gpu/core/procedural-shapes.d.ts +24 -0
  49. package/dist/browser-terminal/gpu/core/procedural-shapes.d.ts.map +1 -0
  50. package/dist/browser-terminal/gpu/core/procedural-shapes.js +316 -0
  51. package/dist/browser-terminal/gpu/core/procedural-shapes.js.map +1 -0
  52. package/dist/browser-terminal/gpu/core/webgl2-renderer.d.ts +107 -0
  53. package/dist/browser-terminal/gpu/core/webgl2-renderer.d.ts.map +1 -0
  54. package/dist/browser-terminal/gpu/core/webgl2-renderer.js +698 -0
  55. package/dist/browser-terminal/gpu/core/webgl2-renderer.js.map +1 -0
  56. package/dist/browser-terminal/gpu/create-renderer.d.ts +15 -0
  57. package/dist/browser-terminal/gpu/create-renderer.d.ts.map +1 -0
  58. package/dist/browser-terminal/gpu/create-renderer.js +33 -0
  59. package/dist/browser-terminal/gpu/create-renderer.js.map +1 -0
  60. package/dist/browser-terminal/index.d.ts +47 -0
  61. package/dist/browser-terminal/index.d.ts.map +1 -0
  62. package/dist/browser-terminal/index.js +75 -0
  63. package/dist/browser-terminal/index.js.map +1 -0
  64. package/dist/browser-terminal/input-handler.d.ts +262 -0
  65. package/dist/browser-terminal/input-handler.d.ts.map +1 -0
  66. package/dist/browser-terminal/input-handler.js +978 -0
  67. package/dist/browser-terminal/input-handler.js.map +1 -0
  68. package/dist/browser-terminal/interfaces.d.ts +230 -0
  69. package/dist/browser-terminal/interfaces.d.ts.map +1 -0
  70. package/dist/browser-terminal/interfaces.js +5 -0
  71. package/dist/browser-terminal/interfaces.js.map +1 -0
  72. package/dist/browser-terminal/link-detector.d.ts +76 -0
  73. package/dist/browser-terminal/link-detector.d.ts.map +1 -0
  74. package/dist/browser-terminal/link-detector.js +165 -0
  75. package/dist/browser-terminal/link-detector.js.map +1 -0
  76. package/dist/browser-terminal/providers/osc8-link-provider.d.ts +56 -0
  77. package/dist/browser-terminal/providers/osc8-link-provider.d.ts.map +1 -0
  78. package/dist/browser-terminal/providers/osc8-link-provider.js +204 -0
  79. package/dist/browser-terminal/providers/osc8-link-provider.js.map +1 -0
  80. package/dist/browser-terminal/providers/url-regex-provider.d.ts +68 -0
  81. package/dist/browser-terminal/providers/url-regex-provider.d.ts.map +1 -0
  82. package/dist/browser-terminal/providers/url-regex-provider.js +107 -0
  83. package/dist/browser-terminal/providers/url-regex-provider.js.map +1 -0
  84. package/dist/browser-terminal/renderer-interface.d.ts +59 -0
  85. package/dist/browser-terminal/renderer-interface.d.ts.map +1 -0
  86. package/dist/browser-terminal/renderer-interface.js +2 -0
  87. package/dist/browser-terminal/renderer-interface.js.map +1 -0
  88. package/dist/browser-terminal/renderer.d.ts +254 -0
  89. package/dist/browser-terminal/renderer.d.ts.map +1 -0
  90. package/dist/browser-terminal/renderer.js +905 -0
  91. package/dist/browser-terminal/renderer.js.map +1 -0
  92. package/dist/browser-terminal/scrollbar-overlay.d.ts +45 -0
  93. package/dist/browser-terminal/scrollbar-overlay.d.ts.map +1 -0
  94. package/dist/browser-terminal/scrollbar-overlay.js +89 -0
  95. package/dist/browser-terminal/scrollbar-overlay.js.map +1 -0
  96. package/dist/browser-terminal/selection-manager.d.ts +180 -0
  97. package/dist/browser-terminal/selection-manager.d.ts.map +1 -0
  98. package/dist/browser-terminal/selection-manager.js +913 -0
  99. package/dist/browser-terminal/selection-manager.js.map +1 -0
  100. package/dist/browser-terminal/terminal.d.ts +423 -0
  101. package/dist/browser-terminal/terminal.d.ts.map +1 -0
  102. package/dist/browser-terminal/terminal.js +1754 -0
  103. package/dist/browser-terminal/terminal.js.map +1 -0
  104. package/dist/browser-terminal/types.d.ts +595 -0
  105. package/dist/browser-terminal/types.d.ts.map +1 -0
  106. package/dist/browser-terminal/types.js +467 -0
  107. package/dist/browser-terminal/types.js.map +1 -0
  108. package/dist/browser-terminal/viewport-scroller.d.ts +94 -0
  109. package/dist/browser-terminal/viewport-scroller.d.ts.map +1 -0
  110. package/dist/browser-terminal/viewport-scroller.js +159 -0
  111. package/dist/browser-terminal/viewport-scroller.js.map +1 -0
  112. package/dist/browser-terminal/vt/effects.d.ts +114 -0
  113. package/dist/browser-terminal/vt/effects.d.ts.map +1 -0
  114. package/dist/browser-terminal/vt/effects.js +143 -0
  115. package/dist/browser-terminal/vt/effects.js.map +1 -0
  116. package/dist/browser-terminal/vt/enums.d.ts +155 -0
  117. package/dist/browser-terminal/vt/enums.d.ts.map +1 -0
  118. package/dist/browser-terminal/vt/enums.js +161 -0
  119. package/dist/browser-terminal/vt/enums.js.map +1 -0
  120. package/dist/browser-terminal/vt/mouse.d.ts +166 -0
  121. package/dist/browser-terminal/vt/mouse.d.ts.map +1 -0
  122. package/dist/browser-terminal/vt/mouse.js +259 -0
  123. package/dist/browser-terminal/vt/mouse.js.map +1 -0
  124. package/dist/browser-terminal/vt/paste.d.ts +53 -0
  125. package/dist/browser-terminal/vt/paste.d.ts.map +1 -0
  126. package/dist/browser-terminal/vt/paste.js +89 -0
  127. package/dist/browser-terminal/vt/paste.js.map +1 -0
  128. package/dist/browser-terminal/vt/render-state.d.ts +208 -0
  129. package/dist/browser-terminal/vt/render-state.d.ts.map +1 -0
  130. package/dist/browser-terminal/vt/render-state.js +776 -0
  131. package/dist/browser-terminal/vt/render-state.js.map +1 -0
  132. package/dist/browser-terminal/vt/style.d.ts +54 -0
  133. package/dist/browser-terminal/vt/style.d.ts.map +1 -0
  134. package/dist/browser-terminal/vt/style.js +92 -0
  135. package/dist/browser-terminal/vt/style.js.map +1 -0
  136. package/dist/browser-terminal/wasm-loader.d.ts +39 -0
  137. package/dist/browser-terminal/wasm-loader.d.ts.map +1 -0
  138. package/dist/browser-terminal/wasm-loader.js +73 -0
  139. package/dist/browser-terminal/wasm-loader.js.map +1 -0
  140. package/dist/browser-terminal/write-buffer.d.ts +40 -0
  141. package/dist/browser-terminal/write-buffer.d.ts.map +1 -0
  142. package/dist/browser-terminal/write-buffer.js +144 -0
  143. package/dist/browser-terminal/write-buffer.js.map +1 -0
  144. package/dist/protocol/index.d.ts +135 -0
  145. package/dist/protocol/index.d.ts.map +1 -0
  146. package/dist/protocol/index.js +76 -0
  147. package/dist/protocol/index.js.map +1 -0
  148. package/dist/react/index.d.ts +59 -0
  149. package/dist/react/index.d.ts.map +1 -0
  150. package/dist/react/index.js +146 -0
  151. package/dist/react/index.js.map +1 -0
  152. package/dist/server/index.d.ts +130 -0
  153. package/dist/server/index.d.ts.map +1 -0
  154. package/dist/server/index.js +191 -0
  155. package/dist/server/index.js.map +1 -0
  156. package/dist/terminal-screen-emulator/emulator.d.ts +66 -0
  157. package/dist/terminal-screen-emulator/emulator.d.ts.map +1 -0
  158. package/dist/terminal-screen-emulator/emulator.js +286 -0
  159. package/dist/terminal-screen-emulator/emulator.js.map +1 -0
  160. package/dist/terminal-screen-emulator/index.d.ts +82 -0
  161. package/dist/terminal-screen-emulator/index.d.ts.map +1 -0
  162. package/dist/terminal-screen-emulator/index.js +92 -0
  163. package/dist/terminal-screen-emulator/index.js.map +1 -0
  164. package/dist/terminal-screen-emulator/types.d.ts +382 -0
  165. package/dist/terminal-screen-emulator/types.d.ts.map +1 -0
  166. package/dist/terminal-screen-emulator/types.js +91 -0
  167. package/dist/terminal-screen-emulator/types.js.map +1 -0
  168. package/dist/terminal-screen-emulator/wasm-loader.d.ts +71 -0
  169. package/dist/terminal-screen-emulator/wasm-loader.d.ts.map +1 -0
  170. package/dist/terminal-screen-emulator/wasm-loader.js +162 -0
  171. package/dist/terminal-screen-emulator/wasm-loader.js.map +1 -0
  172. package/dist/themes/index.d.ts +6 -0
  173. package/dist/themes/index.d.ts.map +1 -0
  174. package/dist/themes/index.js +51 -0
  175. package/dist/themes/index.js.map +1 -0
  176. package/dist/wasm/alloc.d.ts +170 -0
  177. package/dist/wasm/alloc.d.ts.map +1 -0
  178. package/dist/wasm/alloc.js +307 -0
  179. package/dist/wasm/alloc.js.map +1 -0
  180. package/dist/wasm/compile.d.ts +73 -0
  181. package/dist/wasm/compile.d.ts.map +1 -0
  182. package/dist/wasm/compile.js +82 -0
  183. package/dist/wasm/compile.js.map +1 -0
  184. package/dist/wasm/exports.d.ts +234 -0
  185. package/dist/wasm/exports.d.ts.map +1 -0
  186. package/dist/wasm/exports.js +15 -0
  187. package/dist/wasm/exports.js.map +1 -0
  188. package/dist/wasm/fn-table.d.ts +119 -0
  189. package/dist/wasm/fn-table.d.ts.map +1 -0
  190. package/dist/wasm/fn-table.js +168 -0
  191. package/dist/wasm/fn-table.js.map +1 -0
  192. package/dist/wasm/memory.d.ts +59 -0
  193. package/dist/wasm/memory.d.ts.map +1 -0
  194. package/dist/wasm/memory.js +89 -0
  195. package/dist/wasm/memory.js.map +1 -0
  196. package/dist/wasm/type-layouts.d.ts +61 -0
  197. package/dist/wasm/type-layouts.d.ts.map +1 -0
  198. package/dist/wasm/type-layouts.js +32 -0
  199. package/dist/wasm/type-layouts.js.map +1 -0
  200. package/package.json +94 -0
  201. package/wasm/ghostty-vt.wasm +0 -0
@@ -0,0 +1,307 @@
1
+ /**
2
+ * @module wasm/alloc
3
+ *
4
+ * Centralized allocation helpers over Ghostty's WASM allocators.
5
+ *
6
+ * The C ABI for `libghostty-vt` exposes six allocator pairs:
7
+ * - `ghostty_wasm_alloc_u8` / `_free_u8`
8
+ * - `ghostty_wasm_alloc_u8_array` / `_free_u8_array`
9
+ * - `ghostty_wasm_alloc_u16_array` / `_free_u16_array`
10
+ * - `ghostty_wasm_alloc_usize` / `_free_usize`
11
+ * - `ghostty_wasm_alloc_opaque` / `_free_opaque`
12
+ *
13
+ * Higher layers (Terminal, RenderState, GridRef in `browser-terminal/`) used to
14
+ * call these directly — alloc + DataView write + WASM call + read + free,
15
+ * dozens of times per frame, often in tight per-cell loops. That pattern has
16
+ * three documented problems (audit findings B6/B7/B9):
17
+ *
18
+ * - **B6 — memory-grow safety:** TypedArray views detach when `memory.grow()`
19
+ * runs. Code that captures a `Uint8Array(memory.buffer)` once and reads
20
+ * from it across multiple WASM calls can read garbage. Every reader in this
21
+ * module re-derives the view at use time.
22
+ *
23
+ * - **B7 — per-cell churn:** the row-cells walk alloc'd a fresh u64 slot and
24
+ * two 3-byte color slots per cell. For a 120×40 viewport that's 14k
25
+ * allocator round-trips per frame.
26
+ *
27
+ * - **B9 — per-frame churn:** colors-struct + style-struct + scratch buffers
28
+ * were re-allocated on every render-state read. They're trivially poolable.
29
+ *
30
+ * This module exposes two complementary owners:
31
+ *
32
+ * - {@link ScratchPool} — long-lived slots (u8, 2/4/8-byte, opaque, usize),
33
+ * borrowed and reused for the lifetime of the wrapper. Zeroed on borrow.
34
+ *
35
+ * - {@link FrameArena} — bump allocator backed by a single growable u8 array.
36
+ * `borrow(size)` returns a pointer; `reset()` rewinds the bump cursor.
37
+ * Use for per-frame structs that don't outlive the renderer's frame loop.
38
+ *
39
+ * Both delegate to the same C ABI; nothing here invents new shapes. The
40
+ * scratch pool slots map 1:1 to the underlying allocator pairs.
41
+ */
42
+ // =============================================================================
43
+ // View helpers — never cache, always re-derive
44
+ // =============================================================================
45
+ /**
46
+ * Get a fresh `DataView` over the WASM linear memory.
47
+ *
48
+ * VT_NOTE (B6): callers must call this on every use, not cache the result.
49
+ * `WebAssembly.Memory.grow()` invalidates all existing TypedArray and DataView
50
+ * views on the same buffer.
51
+ */
52
+ export function dataView(memory) {
53
+ return new DataView(memory.buffer);
54
+ }
55
+ /** Get a fresh `Uint8Array` covering the full WASM memory. See {@link dataView}. */
56
+ export function bytes(memory) {
57
+ return new Uint8Array(memory.buffer);
58
+ }
59
+ /** Get a fresh `Uint8Array` slice from `[ptr, ptr+len)` in WASM memory. */
60
+ export function bytesSlice(memory, ptr, len) {
61
+ return new Uint8Array(memory.buffer, ptr, len);
62
+ }
63
+ // =============================================================================
64
+ // ScratchPool — long-lived borrowed slots
65
+ // =============================================================================
66
+ /**
67
+ * Pool of long-lived scratch slots.
68
+ *
69
+ * Each slot is allocated lazily on first borrow and re-used until the pool
70
+ * is disposed. The pool zeroes the slot on borrow so callers don't need to
71
+ * memset before writing.
72
+ *
73
+ * Sizing:
74
+ * - `u8` — single byte (matches `ghostty_wasm_alloc_u8`)
75
+ * - `u16`, `u32`, `u64` — 2/4/8-byte slots (backed by `_u8_array`)
76
+ * - `usize` — pointer-sized slot (matches `ghostty_wasm_alloc_usize`)
77
+ * - `opaque` — pointer-sized slot for opaque handle out-params (matches
78
+ * `ghostty_wasm_alloc_opaque`)
79
+ *
80
+ * Lifetime: borrow → write → call → read → done. The slot stays valid until
81
+ * the pool is disposed; callers must not free it themselves.
82
+ */
83
+ export class ScratchPool {
84
+ exports;
85
+ _disposed = false;
86
+ // Lazily allocated slots.
87
+ slotU8;
88
+ slotU16; // 2-byte u8_array
89
+ slotU32; // 4-byte u8_array
90
+ slotU64; // 8-byte u8_array
91
+ slotUsize;
92
+ slotOpaque;
93
+ constructor(exports) {
94
+ this.exports = exports;
95
+ }
96
+ /** Borrow the single-byte slot, zeroed. */
97
+ u8() {
98
+ if (this.slotU8 === undefined)
99
+ this.slotU8 = this.exports.ghostty_wasm_alloc_u8();
100
+ new Uint8Array(this.exports.memory.buffer)[this.slotU8] = 0;
101
+ return this.slotU8;
102
+ }
103
+ /** Borrow the 2-byte slot, zeroed. */
104
+ u16() {
105
+ if (this.slotU16 === undefined)
106
+ this.slotU16 = this.exports.ghostty_wasm_alloc_u8_array(2);
107
+ new DataView(this.exports.memory.buffer).setUint16(this.slotU16, 0, true);
108
+ return this.slotU16;
109
+ }
110
+ /** Borrow the 4-byte slot, zeroed. */
111
+ u32() {
112
+ if (this.slotU32 === undefined)
113
+ this.slotU32 = this.exports.ghostty_wasm_alloc_u8_array(4);
114
+ new DataView(this.exports.memory.buffer).setUint32(this.slotU32, 0, true);
115
+ return this.slotU32;
116
+ }
117
+ /** Borrow the 8-byte slot, zeroed. Used for u64 reads via `getBigUint64`. */
118
+ u64() {
119
+ if (this.slotU64 === undefined)
120
+ this.slotU64 = this.exports.ghostty_wasm_alloc_u8_array(8);
121
+ const view = new DataView(this.exports.memory.buffer);
122
+ view.setUint32(this.slotU64, 0, true);
123
+ view.setUint32(this.slotU64 + 4, 0, true);
124
+ return this.slotU64;
125
+ }
126
+ /** Borrow the pointer-sized slot for `usize` out-parameters, zeroed. */
127
+ usize() {
128
+ if (this.slotUsize === undefined)
129
+ this.slotUsize = this.exports.ghostty_wasm_alloc_usize();
130
+ // wasm32: usize is 4 bytes.
131
+ new DataView(this.exports.memory.buffer).setUint32(this.slotUsize, 0, true);
132
+ return this.slotUsize;
133
+ }
134
+ /** Borrow the pointer-sized slot for opaque-handle out-parameters, zeroed. */
135
+ opaque() {
136
+ if (this.slotOpaque === undefined)
137
+ this.slotOpaque = this.exports.ghostty_wasm_alloc_opaque();
138
+ new DataView(this.exports.memory.buffer).setUint32(this.slotOpaque, 0, true);
139
+ return this.slotOpaque;
140
+ }
141
+ /** Free every slot ever borrowed. Idempotent. */
142
+ dispose() {
143
+ if (this._disposed)
144
+ return;
145
+ this._disposed = true;
146
+ const e = this.exports;
147
+ if (this.slotU8 !== undefined)
148
+ e.ghostty_wasm_free_u8(this.slotU8);
149
+ if (this.slotU16 !== undefined)
150
+ e.ghostty_wasm_free_u8_array(this.slotU16, 2);
151
+ if (this.slotU32 !== undefined)
152
+ e.ghostty_wasm_free_u8_array(this.slotU32, 4);
153
+ if (this.slotU64 !== undefined)
154
+ e.ghostty_wasm_free_u8_array(this.slotU64, 8);
155
+ if (this.slotUsize !== undefined)
156
+ e.ghostty_wasm_free_usize(this.slotUsize);
157
+ if (this.slotOpaque !== undefined)
158
+ e.ghostty_wasm_free_opaque(this.slotOpaque);
159
+ this.slotU8 = undefined;
160
+ this.slotU16 = undefined;
161
+ this.slotU32 = undefined;
162
+ this.slotU64 = undefined;
163
+ this.slotUsize = undefined;
164
+ this.slotOpaque = undefined;
165
+ }
166
+ }
167
+ // =============================================================================
168
+ // FrameArena — bump allocator for short-lived frame allocations
169
+ // =============================================================================
170
+ /**
171
+ * A bump allocator backed by a single growable u8 array in WASM memory.
172
+ *
173
+ * Use for short-lived structs that should not outlive the current render
174
+ * frame: per-cell BG/FG color slots, per-row style structs, the per-call
175
+ * u64 slot used by `ghostty_render_state_row_cells_get(RAW)`, etc.
176
+ *
177
+ * Lifecycle:
178
+ * - `borrow(size, align?)` returns a pointer into the arena, advancing the
179
+ * bump cursor by `size` (rounded up to alignment, default 1).
180
+ * - `reset()` rewinds the cursor to 0 — all previously-borrowed pointers
181
+ * are now invalid, but the underlying buffer is reused.
182
+ * - `dispose()` frees the underlying buffer.
183
+ *
184
+ * Growth: if a borrow would exceed capacity, the arena re-allocates a larger
185
+ * backing buffer. Existing pointers from prior borrows are NOT preserved
186
+ * across growth — the contract is: borrow + use + reset, all within one
187
+ * frame, no cross-frame pointer reuse.
188
+ *
189
+ * VT_NOTE (B6): callers must derive any TypedArray view from the arena's
190
+ * pointer using `bytes()` / `dataView()` from this module — never cache.
191
+ * The arena's own backing buffer can be replaced on growth.
192
+ */
193
+ export class FrameArena {
194
+ exports;
195
+ base = 0;
196
+ capacity = 0;
197
+ cursor = 0;
198
+ _disposed = false;
199
+ constructor(exports, initialCapacity = 1024) {
200
+ this.exports = exports;
201
+ this.grow(initialCapacity);
202
+ }
203
+ /**
204
+ * Reserve `size` bytes inside the arena. Returns the WASM pointer to the
205
+ * start of the reservation. Rounds the cursor up to `alignment` first.
206
+ *
207
+ * VT_NOTE: if a borrow would exceed capacity, the arena grows by
208
+ * re-allocating its backing buffer. **All previously-returned pointers
209
+ * become invalid** at that moment. Callers that hold multiple borrows
210
+ * across one frame must call {@link reserve} up-front, or call
211
+ * {@link reset} between borrows. Triggering a mid-frame grow with
212
+ * pointers still in flight is a use-after-free.
213
+ */
214
+ borrow(size, alignment = 1) {
215
+ if (this._disposed)
216
+ throw new Error("FrameArena has been disposed");
217
+ if (alignment > 1) {
218
+ const rem = this.cursor % alignment;
219
+ if (rem !== 0)
220
+ this.cursor += alignment - rem;
221
+ }
222
+ if (this.cursor + size > this.capacity) {
223
+ // Grow to at least the requested size (with headroom).
224
+ const needed = Math.max(this.capacity * 2, this.cursor + size);
225
+ this.grow(needed);
226
+ }
227
+ const ptr = this.base + this.cursor;
228
+ // Zero the reservation — same contract as `allocZeroed` so callers don't
229
+ // have to memset before writing partial structs.
230
+ new Uint8Array(this.exports.memory.buffer, ptr, size).fill(0);
231
+ this.cursor += size;
232
+ return ptr;
233
+ }
234
+ /** Rewind the cursor to 0. All previously-borrowed pointers become invalid. */
235
+ reset() {
236
+ this.cursor = 0;
237
+ }
238
+ /**
239
+ * Pre-grow the arena to at least `size` bytes so subsequent borrows up to
240
+ * that size are guaranteed not to trigger a re-allocation. Callers that
241
+ * hand out multiple pointers per frame should call this once with a
242
+ * conservative upper bound before the first borrow.
243
+ */
244
+ reserve(size) {
245
+ if (this._disposed)
246
+ throw new Error("FrameArena has been disposed");
247
+ if (size > this.capacity)
248
+ this.grow(size);
249
+ }
250
+ /** Free the backing buffer. Idempotent. */
251
+ dispose() {
252
+ if (this._disposed)
253
+ return;
254
+ this._disposed = true;
255
+ if (this.capacity > 0) {
256
+ this.exports.ghostty_wasm_free_u8_array(this.base, this.capacity);
257
+ }
258
+ this.base = 0;
259
+ this.capacity = 0;
260
+ this.cursor = 0;
261
+ }
262
+ grow(newCapacity) {
263
+ if (this.capacity > 0) {
264
+ this.exports.ghostty_wasm_free_u8_array(this.base, this.capacity);
265
+ }
266
+ this.base = this.exports.ghostty_wasm_alloc_u8_array(newCapacity);
267
+ this.capacity = newCapacity;
268
+ // Cursor stays at its current value — anything previously borrowed is
269
+ // already gone. Callers that survive growth must `reset()` first.
270
+ }
271
+ }
272
+ // =============================================================================
273
+ // One-shot helpers — for places that genuinely need alloc + use + free
274
+ // =============================================================================
275
+ /**
276
+ * Allocate a zero-filled u8 buffer of the given size and return its pointer.
277
+ *
278
+ * Caller owns the pointer and must `freeBytes()` it. For repeated allocations
279
+ * within a single frame, prefer {@link FrameArena.borrow}.
280
+ */
281
+ export function allocZeroedBytes(exports, size) {
282
+ const ptr = exports.ghostty_wasm_alloc_u8_array(size);
283
+ new Uint8Array(exports.memory.buffer, ptr, size).fill(0);
284
+ return ptr;
285
+ }
286
+ /** Free a buffer previously returned by {@link allocZeroedBytes}. */
287
+ export function freeBytes(exports, ptr, size) {
288
+ exports.ghostty_wasm_free_u8_array(ptr, size);
289
+ }
290
+ /**
291
+ * Run `fn` with a freshly-allocated zero-filled u8 buffer of the given size,
292
+ * freeing the buffer afterwards (success or throw).
293
+ *
294
+ * Use sparingly — for sub-frame work prefer the arena. Reserved for
295
+ * lifecycle-tied allocations (`createTerminal` options, palette setters)
296
+ * where the buffer's lifetime is naturally a single function call.
297
+ */
298
+ export function withBytes(exports, size, fn) {
299
+ const ptr = allocZeroedBytes(exports, size);
300
+ try {
301
+ return fn(ptr);
302
+ }
303
+ finally {
304
+ freeBytes(exports, ptr, size);
305
+ }
306
+ }
307
+ //# sourceMappingURL=alloc.js.map
@@ -0,0 +1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jB,CAAC;YAAS,CAAC;QACT,SAAS,CAAC,OAAO,EAAE,GAAG,EAAE,IAAI,CAAC,CAAC;IAChC,CAAC;AACH,CAAC"}
@@ -0,0 +1,73 @@
1
+ /**
2
+ * @module wasm/compile
3
+ *
4
+ * Platform-agnostic compile + instantiate boilerplate for the
5
+ * `ghostty-vt.wasm` binary. The bytes-loading step is intentionally
6
+ * NOT included here — that's the one piece that differs between
7
+ * environments (Node uses `fs.readFile`, the browser uses `fetch`).
8
+ *
9
+ * Consumers in each environment supply the bytes via
10
+ * {@link compileFromBytes} and then call {@link instantiateModule} to
11
+ * get a ready-to-use {@link WasmEmulatorInstance}.
12
+ *
13
+ * The compiled module is cached as a singleton — subsequent calls to
14
+ * {@link compileFromBytes} reuse the cached module, which is significantly
15
+ * faster than recompiling each time.
16
+ *
17
+ * @internal This module is not part of the public API.
18
+ */
19
+ import type { GhosttyWasmExports } from "./exports.js";
20
+ import { type WasmTypeLayouts } from "./type-layouts.js";
21
+ /**
22
+ * A fully instantiated WASM emulator, ready to create terminals.
23
+ *
24
+ * Contains the WASM exports and the pre-parsed type layouts needed
25
+ * to interact with the binary's struct-based API.
26
+ */
27
+ export interface WasmEmulatorInstance {
28
+ /** Direct access to WASM exported functions. */
29
+ exports: GhosttyWasmExports;
30
+ /** Self-describing struct layouts parsed from the binary. */
31
+ typeLayouts: WasmTypeLayouts;
32
+ }
33
+ /**
34
+ * Compile a WASM module from raw bytes, caching the result as a singleton.
35
+ *
36
+ * The platform-specific loader (Node `fs.readFile`, browser `fetch`, etc.)
37
+ * is responsible for producing the bytes; this helper handles compilation
38
+ * and the cache.
39
+ *
40
+ * The parameter is `BufferSource` rather than `Uint8Array` so it accepts both
41
+ * `ArrayBuffer` (from `response.arrayBuffer()` in browsers) and
42
+ * `Uint8Array<ArrayBuffer>` (from `readFile()` in node). The default
43
+ * `Uint8Array<ArrayBufferLike>` since TS 5.7 includes `SharedArrayBuffer`-backed
44
+ * views, which `WebAssembly.compile` rejects — `BufferSource` is the union
45
+ * `WebAssembly.compile` actually accepts.
46
+ *
47
+ * @param bytes - The raw `ghostty-vt.wasm` binary contents.
48
+ * @returns The compiled WebAssembly module (cached after the first call).
49
+ */
50
+ export declare function compileFromBytes(bytes: BufferSource): Promise<WebAssembly.Module>;
51
+ /**
52
+ * Instantiate a compiled WASM module into an independent emulator instance.
53
+ *
54
+ * Each instance has its own WASM linear memory and can operate independently.
55
+ * Multiple instances can be created from the same compiled module without
56
+ * interference.
57
+ *
58
+ * The `env.log` import is provided as a no-op — Ghostty's WASM build expects
59
+ * a log host function, but emulator instances never produce log output in
60
+ * practice.
61
+ *
62
+ * @param module - A previously compiled `ghostty-vt.wasm` module.
63
+ * @returns A ready-to-use WASM emulator instance with parsed type layouts.
64
+ */
65
+ export declare function instantiateModule(module: WebAssembly.Module): Promise<WasmEmulatorInstance>;
66
+ /**
67
+ * Reset the module cache, forcing recompilation on next use.
68
+ *
69
+ * Primarily useful for testing or when the WASM binary has been
70
+ * replaced on disk and you want to pick up the new version.
71
+ */
72
+ export declare function resetWasmCache(): void;
73
+ //# sourceMappingURL=compile.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"compile.d.ts","sourceRoot":"","sources":["../../src/wasm/compile.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAEH,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC;AACvD,OAAO,EAAoB,KAAK,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAI3E;;;;;GAKG;AACH,MAAM,WAAW,oBAAoB;IACnC,gDAAgD;IAChD,OAAO,EAAE,kBAAkB,CAAC;IAC5B,6DAA6D;IAC7D,WAAW,EAAE,eAAe,CAAC;CAC9B;AAUD;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAsB,gBAAgB,CAAC,KAAK,EAAE,YAAY,GAAG,OAAO,CAAC,WAAW,CAAC,MAAM,CAAC,CAIvF;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAsB,iBAAiB,CACrC,MAAM,EAAE,WAAW,CAAC,MAAM,GACzB,OAAO,CAAC,oBAAoB,CAAC,CAW/B;AAED;;;;;GAKG;AACH,wBAAgB,cAAc,IAAI,IAAI,CAErC"}
@@ -0,0 +1,82 @@
1
+ /**
2
+ * @module wasm/compile
3
+ *
4
+ * Platform-agnostic compile + instantiate boilerplate for the
5
+ * `ghostty-vt.wasm` binary. The bytes-loading step is intentionally
6
+ * NOT included here — that's the one piece that differs between
7
+ * environments (Node uses `fs.readFile`, the browser uses `fetch`).
8
+ *
9
+ * Consumers in each environment supply the bytes via
10
+ * {@link compileFromBytes} and then call {@link instantiateModule} to
11
+ * get a ready-to-use {@link WasmEmulatorInstance}.
12
+ *
13
+ * The compiled module is cached as a singleton — subsequent calls to
14
+ * {@link compileFromBytes} reuse the cached module, which is significantly
15
+ * faster than recompiling each time.
16
+ *
17
+ * @internal This module is not part of the public API.
18
+ */
19
+ import { parseTypeLayouts } from "./type-layouts.js";
20
+ // ─── Module Singleton ────────────────────────────────────────────────────────
21
+ /**
22
+ * Cached compiled WASM module. Compilation is expensive (~50ms) but only
23
+ * needs to happen once. Instantiation from a compiled module is fast (~1ms).
24
+ */
25
+ let compiledModule = null;
26
+ /**
27
+ * Compile a WASM module from raw bytes, caching the result as a singleton.
28
+ *
29
+ * The platform-specific loader (Node `fs.readFile`, browser `fetch`, etc.)
30
+ * is responsible for producing the bytes; this helper handles compilation
31
+ * and the cache.
32
+ *
33
+ * The parameter is `BufferSource` rather than `Uint8Array` so it accepts both
34
+ * `ArrayBuffer` (from `response.arrayBuffer()` in browsers) and
35
+ * `Uint8Array<ArrayBuffer>` (from `readFile()` in node). The default
36
+ * `Uint8Array<ArrayBufferLike>` since TS 5.7 includes `SharedArrayBuffer`-backed
37
+ * views, which `WebAssembly.compile` rejects — `BufferSource` is the union
38
+ * `WebAssembly.compile` actually accepts.
39
+ *
40
+ * @param bytes - The raw `ghostty-vt.wasm` binary contents.
41
+ * @returns The compiled WebAssembly module (cached after the first call).
42
+ */
43
+ export async function compileFromBytes(bytes) {
44
+ if (compiledModule)
45
+ return compiledModule;
46
+ compiledModule = await WebAssembly.compile(bytes);
47
+ return compiledModule;
48
+ }
49
+ /**
50
+ * Instantiate a compiled WASM module into an independent emulator instance.
51
+ *
52
+ * Each instance has its own WASM linear memory and can operate independently.
53
+ * Multiple instances can be created from the same compiled module without
54
+ * interference.
55
+ *
56
+ * The `env.log` import is provided as a no-op — Ghostty's WASM build expects
57
+ * a log host function, but emulator instances never produce log output in
58
+ * practice.
59
+ *
60
+ * @param module - A previously compiled `ghostty-vt.wasm` module.
61
+ * @returns A ready-to-use WASM emulator instance with parsed type layouts.
62
+ */
63
+ export async function instantiateModule(module) {
64
+ const instance = await WebAssembly.instantiate(module, {
65
+ env: {
66
+ log: () => { },
67
+ },
68
+ });
69
+ const exports = instance.exports;
70
+ const typeLayouts = parseTypeLayouts(exports);
71
+ return { exports, typeLayouts };
72
+ }
73
+ /**
74
+ * Reset the module cache, forcing recompilation on next use.
75
+ *
76
+ * Primarily useful for testing or when the WASM binary has been
77
+ * replaced on disk and you want to pick up the new version.
78
+ */
79
+ export function resetWasmCache() {
80
+ compiledModule = null;
81
+ }
82
+ //# sourceMappingURL=compile.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"compile.js","sourceRoot":"","sources":["../../src/wasm/compile.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAGH,OAAO,EAAE,gBAAgB,EAAwB,MAAM,mBAAmB,CAAC;AAiB3E,gFAAgF;AAEhF;;;GAGG;AACH,IAAI,cAAc,GAA8B,IAAI,CAAC;AAErD;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,CAAC,KAAK,UAAU,gBAAgB,CAAC,KAAmB;IACxD,IAAI,cAAc;QAAE,OAAO,cAAc,CAAC;IAC1C,cAAc,GAAG,MAAM,WAAW,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;IAClD,OAAO,cAAc,CAAC;AACxB,CAAC;AAED;;;;;;;;;;;;;GAaG;AACH,MAAM,CAAC,KAAK,UAAU,iBAAiB,CACrC,MAA0B;IAE1B,MAAM,QAAQ,GAAG,MAAM,WAAW,CAAC,WAAW,CAAC,MAAM,EAAE;QACrD,GAAG,EAAE;YACH,GAAG,EAAE,GAAG,EAAE,GAAE,CAAC;SACd;KACF,CAAC,CAAC;IAEH,MAAM,OAAO,GAAG,QAAQ,CAAC,OAAwC,CAAC;IAClE,MAAM,WAAW,GAAG,gBAAgB,CAAC,OAAO,CAAC,CAAC;IAE9C,OAAO,EAAE,OAAO,EAAE,WAAW,EAAE,CAAC;AAClC,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,cAAc;IAC5B,cAAc,GAAG,IAAI,CAAC;AACxB,CAAC"}