@native-sdk/cli 0.7.2 → 0.8.0

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 (41) hide show
  1. package/build/app.zig +265 -65
  2. package/build/ts_run.mjs +6 -8
  3. package/build.zig +292 -349
  4. package/package.json +13 -11
  5. package/packages/core/compile-surface/core.ts +559 -0
  6. package/packages/core/package-lock.json +375 -12
  7. package/packages/core/package.json +9 -6
  8. package/packages/core/scripts/compiler_typecheck.mjs +58 -0
  9. package/packages/core/scripts/gen_case_tables.mjs +130 -0
  10. package/packages/core/scripts/gen_declarations.mjs +97 -0
  11. package/packages/core/scripts/run_external_core_compiler.mjs +108 -0
  12. package/packages/core/scripts/stage_external_core.mjs +148 -0
  13. package/packages/core/sdk/core.d.ts +363 -0
  14. package/packages/core/sdk/events.d.ts +65 -0
  15. package/packages/core/sdk/text.d.ts +51 -0
  16. package/packages/core/src/checker.ts +3 -3
  17. package/packages/core/src/cli.ts +40 -38
  18. package/packages/core/src/contract.ts +749 -0
  19. package/packages/core/src/devhost.ts +4 -3
  20. package/packages/core/src/diagnostics.ts +5 -7
  21. package/packages/core/src/{transpile.ts → frontend.ts} +38 -50
  22. package/packages/core/src/infer.ts +1 -1
  23. package/packages/core/src/typed_ast.ts +16 -15
  24. package/packages/core/src/wyhash.ts +79 -0
  25. package/skill-data/ts-core/SKILL.md +29 -29
  26. package/src/runtime/ts_core_host.zig +38 -38
  27. package/src/runtime/ts_ui_app.zig +12 -12
  28. package/src/tooling/manifest.zig +32 -0
  29. package/src/tooling/raw_manifest.zig +4 -0
  30. package/src/tooling/templates.zig +25 -20
  31. package/src/tooling/ts_core.zig +55 -34
  32. package/src/tooling/verbs.zig +1 -1
  33. package/tools/corewire/core_abi.zig +169 -0
  34. package/tools/corewire/emit.zig +2418 -0
  35. package/tools/corewire/emit_facade.zig +3984 -0
  36. package/tools/corewire/emit_profile.zig +501 -0
  37. package/tools/corewire/main.zig +561 -0
  38. package/tools/corewire/shim_rt.zig +632 -0
  39. package/tools/corewire/sidecar.zig +2507 -0
  40. package/packages/core/rt/rt.zig +0 -1986
  41. package/packages/core/src/emitter.ts +0 -11898
@@ -1,1986 +0,0 @@
1
- //! The app-core runtime kernel ("rt"): the fixed, hand-written module the
2
- //! transpiler's output links against. Two memory regions, no GC, no
3
- //! free-lists, no headers:
4
- //!
5
- //! frame arena — bump allocator, holds every transient the update builds
6
- //! (spread results, map/filter outputs, string builds).
7
- //! Reset wholesale after each dispatch commits.
8
- //! model heap — two-space bump allocator, holds the committed model
9
- //! graph. Commit copies only frame-resident nodes in
10
- //! (O(new nodes)); replaced nodes leak until a watermark
11
- //! triggers a full copying compaction into the other space
12
- //! (O(live model), deterministic, at a dispatch boundary).
13
- //!
14
- //! Address-range checks classify pointers: in-frame → must copy at commit;
15
- //! in-heap or static/rodata → persists, share as-is.
16
- //!
17
- //! Capacities are comptime parameters: the emitted core instantiates
18
- //! `Kernel(.{ .frame_cap = ..., .heap_cap = ... })` from the app's transpile
19
- //! options (`--frame-cap` / `--heap-cap` on the CLI) and re-exports it as
20
- //! `core.rt`. Both regions are FIXED at build time — v1 never grows them at
21
- //! runtime, so binaries stay allocation-free and replay stays trivially
22
- //! deterministic (a growth event could shift compaction cadence and address
23
- //! ranges mid-run). Overflow is a loud teaching panic naming the knob.
24
- //! Instantiations with equal options memoize to one shared kernel, so a
25
- //! hand-written harness importing `core.rt` and the emitted core always see
26
- //! the same arenas.
27
- //!
28
- //! The command builders at the bottom encode `Cmd` effect values (the
29
- //! versioned wire format under `cmd_format_version`) into the frame arena;
30
- //! hosts consume them after commit, before the frame resets.
31
-
32
- const std = @import("std");
33
-
34
- // ------------------------------------------------------------- byte text
35
- //
36
- // The everyday string-method surface on core text (`Uint8Array` bytes):
37
- // toUpperCase/toLowerCase (Unicode SIMPLE case mapping — the tables below),
38
- // repeat, startsWith/endsWith/includes, padStart/padEnd, trim family,
39
- // indexOf/lastIndexOf, split, at. Byte-honest semantics throughout: lengths
40
- // and offsets are BYTE lengths and offsets, search is byte-wise, and the
41
- // node devhost polyfills the same methods from the same generated tables
42
- // (src/text_polyfill.ts + src/text_tables.ts), so node and native produce
43
- // identical bytes by construction.
44
- //
45
- // Case mapping is Unicode SIMPLE case mapping (UnicodeData.txt fields
46
- // 12/13): code point -> code point, locale-independent, no special casing
47
- // (ß stays ß, no Turkish dotless-i) — full/locale casing would need
48
- // SpecialCasing tables and ambient locale state whose results differ
49
- // across runtimes. UTF-8 in, UTF-8 out; bytes that are not well-formed
50
- // UTF-8 pass through unchanged, one byte at a time.
51
-
52
- /// One compressed run of the simple case-mapping table: `count` mapped code
53
- /// points at lo, lo+stride, ...; each maps to itself + delta. stride2 marks
54
- /// the alternating upper/lower pairs (U+0100 Ā / U+0101 ā). 8 bytes/range.
55
- const CaseRange = packed struct(u64) {
56
- lo: u21,
57
- count: u16,
58
- stride2: bool,
59
- delta: i26,
60
- };
61
-
62
- // ---- BEGIN GENERATED CASE TABLES ----
63
- // Unicode 17.0.0 simple case mappings (UnicodeData.txt fields 12/13).
64
- // GENERATED by scripts/gen_case_tables.mjs — do not edit by hand; regenerate with:
65
- // node scripts/gen_case_tables.mjs [path/to/UnicodeData.txt]
66
- // 204 + 186 ranges, 8 bytes each = 3120 bytes of table data.
67
-
68
- const simple_upper_ranges = [_]CaseRange{
69
- .{ .lo = 0x61, .count = 26, .stride2 = false, .delta = -32 },
70
- .{ .lo = 0xB5, .count = 1, .stride2 = false, .delta = 743 },
71
- .{ .lo = 0xE0, .count = 23, .stride2 = false, .delta = -32 },
72
- .{ .lo = 0xF8, .count = 7, .stride2 = false, .delta = -32 },
73
- .{ .lo = 0xFF, .count = 1, .stride2 = false, .delta = 121 },
74
- .{ .lo = 0x101, .count = 24, .stride2 = true, .delta = -1 },
75
- .{ .lo = 0x131, .count = 1, .stride2 = false, .delta = -232 },
76
- .{ .lo = 0x133, .count = 3, .stride2 = true, .delta = -1 },
77
- .{ .lo = 0x13A, .count = 8, .stride2 = true, .delta = -1 },
78
- .{ .lo = 0x14B, .count = 23, .stride2 = true, .delta = -1 },
79
- .{ .lo = 0x17A, .count = 3, .stride2 = true, .delta = -1 },
80
- .{ .lo = 0x17F, .count = 1, .stride2 = false, .delta = -300 },
81
- .{ .lo = 0x180, .count = 1, .stride2 = false, .delta = 195 },
82
- .{ .lo = 0x183, .count = 2, .stride2 = true, .delta = -1 },
83
- .{ .lo = 0x188, .count = 1, .stride2 = false, .delta = -1 },
84
- .{ .lo = 0x18C, .count = 1, .stride2 = false, .delta = -1 },
85
- .{ .lo = 0x192, .count = 1, .stride2 = false, .delta = -1 },
86
- .{ .lo = 0x195, .count = 1, .stride2 = false, .delta = 97 },
87
- .{ .lo = 0x199, .count = 1, .stride2 = false, .delta = -1 },
88
- .{ .lo = 0x19A, .count = 1, .stride2 = false, .delta = 163 },
89
- .{ .lo = 0x19B, .count = 1, .stride2 = false, .delta = 42561 },
90
- .{ .lo = 0x19E, .count = 1, .stride2 = false, .delta = 130 },
91
- .{ .lo = 0x1A1, .count = 3, .stride2 = true, .delta = -1 },
92
- .{ .lo = 0x1A8, .count = 1, .stride2 = false, .delta = -1 },
93
- .{ .lo = 0x1AD, .count = 1, .stride2 = false, .delta = -1 },
94
- .{ .lo = 0x1B0, .count = 1, .stride2 = false, .delta = -1 },
95
- .{ .lo = 0x1B4, .count = 2, .stride2 = true, .delta = -1 },
96
- .{ .lo = 0x1B9, .count = 1, .stride2 = false, .delta = -1 },
97
- .{ .lo = 0x1BD, .count = 1, .stride2 = false, .delta = -1 },
98
- .{ .lo = 0x1BF, .count = 1, .stride2 = false, .delta = 56 },
99
- .{ .lo = 0x1C5, .count = 1, .stride2 = false, .delta = -1 },
100
- .{ .lo = 0x1C6, .count = 1, .stride2 = false, .delta = -2 },
101
- .{ .lo = 0x1C8, .count = 1, .stride2 = false, .delta = -1 },
102
- .{ .lo = 0x1C9, .count = 1, .stride2 = false, .delta = -2 },
103
- .{ .lo = 0x1CB, .count = 1, .stride2 = false, .delta = -1 },
104
- .{ .lo = 0x1CC, .count = 1, .stride2 = false, .delta = -2 },
105
- .{ .lo = 0x1CE, .count = 8, .stride2 = true, .delta = -1 },
106
- .{ .lo = 0x1DD, .count = 1, .stride2 = false, .delta = -79 },
107
- .{ .lo = 0x1DF, .count = 9, .stride2 = true, .delta = -1 },
108
- .{ .lo = 0x1F2, .count = 1, .stride2 = false, .delta = -1 },
109
- .{ .lo = 0x1F3, .count = 1, .stride2 = false, .delta = -2 },
110
- .{ .lo = 0x1F5, .count = 1, .stride2 = false, .delta = -1 },
111
- .{ .lo = 0x1F9, .count = 20, .stride2 = true, .delta = -1 },
112
- .{ .lo = 0x223, .count = 9, .stride2 = true, .delta = -1 },
113
- .{ .lo = 0x23C, .count = 1, .stride2 = false, .delta = -1 },
114
- .{ .lo = 0x23F, .count = 2, .stride2 = false, .delta = 10815 },
115
- .{ .lo = 0x242, .count = 1, .stride2 = false, .delta = -1 },
116
- .{ .lo = 0x247, .count = 5, .stride2 = true, .delta = -1 },
117
- .{ .lo = 0x250, .count = 1, .stride2 = false, .delta = 10783 },
118
- .{ .lo = 0x251, .count = 1, .stride2 = false, .delta = 10780 },
119
- .{ .lo = 0x252, .count = 1, .stride2 = false, .delta = 10782 },
120
- .{ .lo = 0x253, .count = 1, .stride2 = false, .delta = -210 },
121
- .{ .lo = 0x254, .count = 1, .stride2 = false, .delta = -206 },
122
- .{ .lo = 0x256, .count = 2, .stride2 = false, .delta = -205 },
123
- .{ .lo = 0x259, .count = 1, .stride2 = false, .delta = -202 },
124
- .{ .lo = 0x25B, .count = 1, .stride2 = false, .delta = -203 },
125
- .{ .lo = 0x25C, .count = 1, .stride2 = false, .delta = 42319 },
126
- .{ .lo = 0x260, .count = 1, .stride2 = false, .delta = -205 },
127
- .{ .lo = 0x261, .count = 1, .stride2 = false, .delta = 42315 },
128
- .{ .lo = 0x263, .count = 1, .stride2 = false, .delta = -207 },
129
- .{ .lo = 0x264, .count = 1, .stride2 = false, .delta = 42343 },
130
- .{ .lo = 0x265, .count = 1, .stride2 = false, .delta = 42280 },
131
- .{ .lo = 0x266, .count = 1, .stride2 = false, .delta = 42308 },
132
- .{ .lo = 0x268, .count = 1, .stride2 = false, .delta = -209 },
133
- .{ .lo = 0x269, .count = 1, .stride2 = false, .delta = -211 },
134
- .{ .lo = 0x26A, .count = 1, .stride2 = false, .delta = 42308 },
135
- .{ .lo = 0x26B, .count = 1, .stride2 = false, .delta = 10743 },
136
- .{ .lo = 0x26C, .count = 1, .stride2 = false, .delta = 42305 },
137
- .{ .lo = 0x26F, .count = 1, .stride2 = false, .delta = -211 },
138
- .{ .lo = 0x271, .count = 1, .stride2 = false, .delta = 10749 },
139
- .{ .lo = 0x272, .count = 1, .stride2 = false, .delta = -213 },
140
- .{ .lo = 0x275, .count = 1, .stride2 = false, .delta = -214 },
141
- .{ .lo = 0x27D, .count = 1, .stride2 = false, .delta = 10727 },
142
- .{ .lo = 0x280, .count = 1, .stride2 = false, .delta = -218 },
143
- .{ .lo = 0x282, .count = 1, .stride2 = false, .delta = 42307 },
144
- .{ .lo = 0x283, .count = 1, .stride2 = false, .delta = -218 },
145
- .{ .lo = 0x287, .count = 1, .stride2 = false, .delta = 42282 },
146
- .{ .lo = 0x288, .count = 1, .stride2 = false, .delta = -218 },
147
- .{ .lo = 0x289, .count = 1, .stride2 = false, .delta = -69 },
148
- .{ .lo = 0x28A, .count = 2, .stride2 = false, .delta = -217 },
149
- .{ .lo = 0x28C, .count = 1, .stride2 = false, .delta = -71 },
150
- .{ .lo = 0x292, .count = 1, .stride2 = false, .delta = -219 },
151
- .{ .lo = 0x29D, .count = 1, .stride2 = false, .delta = 42261 },
152
- .{ .lo = 0x29E, .count = 1, .stride2 = false, .delta = 42258 },
153
- .{ .lo = 0x345, .count = 1, .stride2 = false, .delta = 84 },
154
- .{ .lo = 0x371, .count = 2, .stride2 = true, .delta = -1 },
155
- .{ .lo = 0x377, .count = 1, .stride2 = false, .delta = -1 },
156
- .{ .lo = 0x37B, .count = 3, .stride2 = false, .delta = 130 },
157
- .{ .lo = 0x3AC, .count = 1, .stride2 = false, .delta = -38 },
158
- .{ .lo = 0x3AD, .count = 3, .stride2 = false, .delta = -37 },
159
- .{ .lo = 0x3B1, .count = 17, .stride2 = false, .delta = -32 },
160
- .{ .lo = 0x3C2, .count = 1, .stride2 = false, .delta = -31 },
161
- .{ .lo = 0x3C3, .count = 9, .stride2 = false, .delta = -32 },
162
- .{ .lo = 0x3CC, .count = 1, .stride2 = false, .delta = -64 },
163
- .{ .lo = 0x3CD, .count = 2, .stride2 = false, .delta = -63 },
164
- .{ .lo = 0x3D0, .count = 1, .stride2 = false, .delta = -62 },
165
- .{ .lo = 0x3D1, .count = 1, .stride2 = false, .delta = -57 },
166
- .{ .lo = 0x3D5, .count = 1, .stride2 = false, .delta = -47 },
167
- .{ .lo = 0x3D6, .count = 1, .stride2 = false, .delta = -54 },
168
- .{ .lo = 0x3D7, .count = 1, .stride2 = false, .delta = -8 },
169
- .{ .lo = 0x3D9, .count = 12, .stride2 = true, .delta = -1 },
170
- .{ .lo = 0x3F0, .count = 1, .stride2 = false, .delta = -86 },
171
- .{ .lo = 0x3F1, .count = 1, .stride2 = false, .delta = -80 },
172
- .{ .lo = 0x3F2, .count = 1, .stride2 = false, .delta = 7 },
173
- .{ .lo = 0x3F3, .count = 1, .stride2 = false, .delta = -116 },
174
- .{ .lo = 0x3F5, .count = 1, .stride2 = false, .delta = -96 },
175
- .{ .lo = 0x3F8, .count = 1, .stride2 = false, .delta = -1 },
176
- .{ .lo = 0x3FB, .count = 1, .stride2 = false, .delta = -1 },
177
- .{ .lo = 0x430, .count = 32, .stride2 = false, .delta = -32 },
178
- .{ .lo = 0x450, .count = 16, .stride2 = false, .delta = -80 },
179
- .{ .lo = 0x461, .count = 17, .stride2 = true, .delta = -1 },
180
- .{ .lo = 0x48B, .count = 27, .stride2 = true, .delta = -1 },
181
- .{ .lo = 0x4C2, .count = 7, .stride2 = true, .delta = -1 },
182
- .{ .lo = 0x4CF, .count = 1, .stride2 = false, .delta = -15 },
183
- .{ .lo = 0x4D1, .count = 48, .stride2 = true, .delta = -1 },
184
- .{ .lo = 0x561, .count = 38, .stride2 = false, .delta = -48 },
185
- .{ .lo = 0x10D0, .count = 43, .stride2 = false, .delta = 3008 },
186
- .{ .lo = 0x10FD, .count = 3, .stride2 = false, .delta = 3008 },
187
- .{ .lo = 0x13F8, .count = 6, .stride2 = false, .delta = -8 },
188
- .{ .lo = 0x1C80, .count = 1, .stride2 = false, .delta = -6254 },
189
- .{ .lo = 0x1C81, .count = 1, .stride2 = false, .delta = -6253 },
190
- .{ .lo = 0x1C82, .count = 1, .stride2 = false, .delta = -6244 },
191
- .{ .lo = 0x1C83, .count = 2, .stride2 = false, .delta = -6242 },
192
- .{ .lo = 0x1C85, .count = 1, .stride2 = false, .delta = -6243 },
193
- .{ .lo = 0x1C86, .count = 1, .stride2 = false, .delta = -6236 },
194
- .{ .lo = 0x1C87, .count = 1, .stride2 = false, .delta = -6181 },
195
- .{ .lo = 0x1C88, .count = 1, .stride2 = false, .delta = 35266 },
196
- .{ .lo = 0x1C8A, .count = 1, .stride2 = false, .delta = -1 },
197
- .{ .lo = 0x1D79, .count = 1, .stride2 = false, .delta = 35332 },
198
- .{ .lo = 0x1D7D, .count = 1, .stride2 = false, .delta = 3814 },
199
- .{ .lo = 0x1D8E, .count = 1, .stride2 = false, .delta = 35384 },
200
- .{ .lo = 0x1E01, .count = 75, .stride2 = true, .delta = -1 },
201
- .{ .lo = 0x1E9B, .count = 1, .stride2 = false, .delta = -59 },
202
- .{ .lo = 0x1EA1, .count = 48, .stride2 = true, .delta = -1 },
203
- .{ .lo = 0x1F00, .count = 8, .stride2 = false, .delta = 8 },
204
- .{ .lo = 0x1F10, .count = 6, .stride2 = false, .delta = 8 },
205
- .{ .lo = 0x1F20, .count = 8, .stride2 = false, .delta = 8 },
206
- .{ .lo = 0x1F30, .count = 8, .stride2 = false, .delta = 8 },
207
- .{ .lo = 0x1F40, .count = 6, .stride2 = false, .delta = 8 },
208
- .{ .lo = 0x1F51, .count = 4, .stride2 = true, .delta = 8 },
209
- .{ .lo = 0x1F60, .count = 8, .stride2 = false, .delta = 8 },
210
- .{ .lo = 0x1F70, .count = 2, .stride2 = false, .delta = 74 },
211
- .{ .lo = 0x1F72, .count = 4, .stride2 = false, .delta = 86 },
212
- .{ .lo = 0x1F76, .count = 2, .stride2 = false, .delta = 100 },
213
- .{ .lo = 0x1F78, .count = 2, .stride2 = false, .delta = 128 },
214
- .{ .lo = 0x1F7A, .count = 2, .stride2 = false, .delta = 112 },
215
- .{ .lo = 0x1F7C, .count = 2, .stride2 = false, .delta = 126 },
216
- .{ .lo = 0x1F80, .count = 8, .stride2 = false, .delta = 8 },
217
- .{ .lo = 0x1F90, .count = 8, .stride2 = false, .delta = 8 },
218
- .{ .lo = 0x1FA0, .count = 8, .stride2 = false, .delta = 8 },
219
- .{ .lo = 0x1FB0, .count = 2, .stride2 = false, .delta = 8 },
220
- .{ .lo = 0x1FB3, .count = 1, .stride2 = false, .delta = 9 },
221
- .{ .lo = 0x1FBE, .count = 1, .stride2 = false, .delta = -7205 },
222
- .{ .lo = 0x1FC3, .count = 1, .stride2 = false, .delta = 9 },
223
- .{ .lo = 0x1FD0, .count = 2, .stride2 = false, .delta = 8 },
224
- .{ .lo = 0x1FE0, .count = 2, .stride2 = false, .delta = 8 },
225
- .{ .lo = 0x1FE5, .count = 1, .stride2 = false, .delta = 7 },
226
- .{ .lo = 0x1FF3, .count = 1, .stride2 = false, .delta = 9 },
227
- .{ .lo = 0x214E, .count = 1, .stride2 = false, .delta = -28 },
228
- .{ .lo = 0x2170, .count = 16, .stride2 = false, .delta = -16 },
229
- .{ .lo = 0x2184, .count = 1, .stride2 = false, .delta = -1 },
230
- .{ .lo = 0x24D0, .count = 26, .stride2 = false, .delta = -26 },
231
- .{ .lo = 0x2C30, .count = 48, .stride2 = false, .delta = -48 },
232
- .{ .lo = 0x2C61, .count = 1, .stride2 = false, .delta = -1 },
233
- .{ .lo = 0x2C65, .count = 1, .stride2 = false, .delta = -10795 },
234
- .{ .lo = 0x2C66, .count = 1, .stride2 = false, .delta = -10792 },
235
- .{ .lo = 0x2C68, .count = 3, .stride2 = true, .delta = -1 },
236
- .{ .lo = 0x2C73, .count = 1, .stride2 = false, .delta = -1 },
237
- .{ .lo = 0x2C76, .count = 1, .stride2 = false, .delta = -1 },
238
- .{ .lo = 0x2C81, .count = 50, .stride2 = true, .delta = -1 },
239
- .{ .lo = 0x2CEC, .count = 2, .stride2 = true, .delta = -1 },
240
- .{ .lo = 0x2CF3, .count = 1, .stride2 = false, .delta = -1 },
241
- .{ .lo = 0x2D00, .count = 38, .stride2 = false, .delta = -7264 },
242
- .{ .lo = 0x2D27, .count = 1, .stride2 = false, .delta = -7264 },
243
- .{ .lo = 0x2D2D, .count = 1, .stride2 = false, .delta = -7264 },
244
- .{ .lo = 0xA641, .count = 23, .stride2 = true, .delta = -1 },
245
- .{ .lo = 0xA681, .count = 14, .stride2 = true, .delta = -1 },
246
- .{ .lo = 0xA723, .count = 7, .stride2 = true, .delta = -1 },
247
- .{ .lo = 0xA733, .count = 31, .stride2 = true, .delta = -1 },
248
- .{ .lo = 0xA77A, .count = 2, .stride2 = true, .delta = -1 },
249
- .{ .lo = 0xA77F, .count = 5, .stride2 = true, .delta = -1 },
250
- .{ .lo = 0xA78C, .count = 1, .stride2 = false, .delta = -1 },
251
- .{ .lo = 0xA791, .count = 2, .stride2 = true, .delta = -1 },
252
- .{ .lo = 0xA794, .count = 1, .stride2 = false, .delta = 48 },
253
- .{ .lo = 0xA797, .count = 10, .stride2 = true, .delta = -1 },
254
- .{ .lo = 0xA7B5, .count = 8, .stride2 = true, .delta = -1 },
255
- .{ .lo = 0xA7C8, .count = 2, .stride2 = true, .delta = -1 },
256
- .{ .lo = 0xA7CD, .count = 8, .stride2 = true, .delta = -1 },
257
- .{ .lo = 0xA7F6, .count = 1, .stride2 = false, .delta = -1 },
258
- .{ .lo = 0xAB53, .count = 1, .stride2 = false, .delta = -928 },
259
- .{ .lo = 0xAB70, .count = 80, .stride2 = false, .delta = -38864 },
260
- .{ .lo = 0xFF41, .count = 26, .stride2 = false, .delta = -32 },
261
- .{ .lo = 0x10428, .count = 40, .stride2 = false, .delta = -40 },
262
- .{ .lo = 0x104D8, .count = 36, .stride2 = false, .delta = -40 },
263
- .{ .lo = 0x10597, .count = 11, .stride2 = false, .delta = -39 },
264
- .{ .lo = 0x105A3, .count = 15, .stride2 = false, .delta = -39 },
265
- .{ .lo = 0x105B3, .count = 7, .stride2 = false, .delta = -39 },
266
- .{ .lo = 0x105BB, .count = 2, .stride2 = false, .delta = -39 },
267
- .{ .lo = 0x10CC0, .count = 51, .stride2 = false, .delta = -64 },
268
- .{ .lo = 0x10D70, .count = 22, .stride2 = false, .delta = -32 },
269
- .{ .lo = 0x118C0, .count = 32, .stride2 = false, .delta = -32 },
270
- .{ .lo = 0x16E60, .count = 32, .stride2 = false, .delta = -32 },
271
- .{ .lo = 0x16EBB, .count = 25, .stride2 = false, .delta = -27 },
272
- .{ .lo = 0x1E922, .count = 34, .stride2 = false, .delta = -34 },
273
- };
274
-
275
- const simple_lower_ranges = [_]CaseRange{
276
- .{ .lo = 0x41, .count = 26, .stride2 = false, .delta = 32 },
277
- .{ .lo = 0xC0, .count = 23, .stride2 = false, .delta = 32 },
278
- .{ .lo = 0xD8, .count = 7, .stride2 = false, .delta = 32 },
279
- .{ .lo = 0x100, .count = 24, .stride2 = true, .delta = 1 },
280
- .{ .lo = 0x130, .count = 1, .stride2 = false, .delta = -199 },
281
- .{ .lo = 0x132, .count = 3, .stride2 = true, .delta = 1 },
282
- .{ .lo = 0x139, .count = 8, .stride2 = true, .delta = 1 },
283
- .{ .lo = 0x14A, .count = 23, .stride2 = true, .delta = 1 },
284
- .{ .lo = 0x178, .count = 1, .stride2 = false, .delta = -121 },
285
- .{ .lo = 0x179, .count = 3, .stride2 = true, .delta = 1 },
286
- .{ .lo = 0x181, .count = 1, .stride2 = false, .delta = 210 },
287
- .{ .lo = 0x182, .count = 2, .stride2 = true, .delta = 1 },
288
- .{ .lo = 0x186, .count = 1, .stride2 = false, .delta = 206 },
289
- .{ .lo = 0x187, .count = 1, .stride2 = false, .delta = 1 },
290
- .{ .lo = 0x189, .count = 2, .stride2 = false, .delta = 205 },
291
- .{ .lo = 0x18B, .count = 1, .stride2 = false, .delta = 1 },
292
- .{ .lo = 0x18E, .count = 1, .stride2 = false, .delta = 79 },
293
- .{ .lo = 0x18F, .count = 1, .stride2 = false, .delta = 202 },
294
- .{ .lo = 0x190, .count = 1, .stride2 = false, .delta = 203 },
295
- .{ .lo = 0x191, .count = 1, .stride2 = false, .delta = 1 },
296
- .{ .lo = 0x193, .count = 1, .stride2 = false, .delta = 205 },
297
- .{ .lo = 0x194, .count = 1, .stride2 = false, .delta = 207 },
298
- .{ .lo = 0x196, .count = 1, .stride2 = false, .delta = 211 },
299
- .{ .lo = 0x197, .count = 1, .stride2 = false, .delta = 209 },
300
- .{ .lo = 0x198, .count = 1, .stride2 = false, .delta = 1 },
301
- .{ .lo = 0x19C, .count = 1, .stride2 = false, .delta = 211 },
302
- .{ .lo = 0x19D, .count = 1, .stride2 = false, .delta = 213 },
303
- .{ .lo = 0x19F, .count = 1, .stride2 = false, .delta = 214 },
304
- .{ .lo = 0x1A0, .count = 3, .stride2 = true, .delta = 1 },
305
- .{ .lo = 0x1A6, .count = 1, .stride2 = false, .delta = 218 },
306
- .{ .lo = 0x1A7, .count = 1, .stride2 = false, .delta = 1 },
307
- .{ .lo = 0x1A9, .count = 1, .stride2 = false, .delta = 218 },
308
- .{ .lo = 0x1AC, .count = 1, .stride2 = false, .delta = 1 },
309
- .{ .lo = 0x1AE, .count = 1, .stride2 = false, .delta = 218 },
310
- .{ .lo = 0x1AF, .count = 1, .stride2 = false, .delta = 1 },
311
- .{ .lo = 0x1B1, .count = 2, .stride2 = false, .delta = 217 },
312
- .{ .lo = 0x1B3, .count = 2, .stride2 = true, .delta = 1 },
313
- .{ .lo = 0x1B7, .count = 1, .stride2 = false, .delta = 219 },
314
- .{ .lo = 0x1B8, .count = 1, .stride2 = false, .delta = 1 },
315
- .{ .lo = 0x1BC, .count = 1, .stride2 = false, .delta = 1 },
316
- .{ .lo = 0x1C4, .count = 1, .stride2 = false, .delta = 2 },
317
- .{ .lo = 0x1C5, .count = 1, .stride2 = false, .delta = 1 },
318
- .{ .lo = 0x1C7, .count = 1, .stride2 = false, .delta = 2 },
319
- .{ .lo = 0x1C8, .count = 1, .stride2 = false, .delta = 1 },
320
- .{ .lo = 0x1CA, .count = 1, .stride2 = false, .delta = 2 },
321
- .{ .lo = 0x1CB, .count = 9, .stride2 = true, .delta = 1 },
322
- .{ .lo = 0x1DE, .count = 9, .stride2 = true, .delta = 1 },
323
- .{ .lo = 0x1F1, .count = 1, .stride2 = false, .delta = 2 },
324
- .{ .lo = 0x1F2, .count = 2, .stride2 = true, .delta = 1 },
325
- .{ .lo = 0x1F6, .count = 1, .stride2 = false, .delta = -97 },
326
- .{ .lo = 0x1F7, .count = 1, .stride2 = false, .delta = -56 },
327
- .{ .lo = 0x1F8, .count = 20, .stride2 = true, .delta = 1 },
328
- .{ .lo = 0x220, .count = 1, .stride2 = false, .delta = -130 },
329
- .{ .lo = 0x222, .count = 9, .stride2 = true, .delta = 1 },
330
- .{ .lo = 0x23A, .count = 1, .stride2 = false, .delta = 10795 },
331
- .{ .lo = 0x23B, .count = 1, .stride2 = false, .delta = 1 },
332
- .{ .lo = 0x23D, .count = 1, .stride2 = false, .delta = -163 },
333
- .{ .lo = 0x23E, .count = 1, .stride2 = false, .delta = 10792 },
334
- .{ .lo = 0x241, .count = 1, .stride2 = false, .delta = 1 },
335
- .{ .lo = 0x243, .count = 1, .stride2 = false, .delta = -195 },
336
- .{ .lo = 0x244, .count = 1, .stride2 = false, .delta = 69 },
337
- .{ .lo = 0x245, .count = 1, .stride2 = false, .delta = 71 },
338
- .{ .lo = 0x246, .count = 5, .stride2 = true, .delta = 1 },
339
- .{ .lo = 0x370, .count = 2, .stride2 = true, .delta = 1 },
340
- .{ .lo = 0x376, .count = 1, .stride2 = false, .delta = 1 },
341
- .{ .lo = 0x37F, .count = 1, .stride2 = false, .delta = 116 },
342
- .{ .lo = 0x386, .count = 1, .stride2 = false, .delta = 38 },
343
- .{ .lo = 0x388, .count = 3, .stride2 = false, .delta = 37 },
344
- .{ .lo = 0x38C, .count = 1, .stride2 = false, .delta = 64 },
345
- .{ .lo = 0x38E, .count = 2, .stride2 = false, .delta = 63 },
346
- .{ .lo = 0x391, .count = 17, .stride2 = false, .delta = 32 },
347
- .{ .lo = 0x3A3, .count = 9, .stride2 = false, .delta = 32 },
348
- .{ .lo = 0x3CF, .count = 1, .stride2 = false, .delta = 8 },
349
- .{ .lo = 0x3D8, .count = 12, .stride2 = true, .delta = 1 },
350
- .{ .lo = 0x3F4, .count = 1, .stride2 = false, .delta = -60 },
351
- .{ .lo = 0x3F7, .count = 1, .stride2 = false, .delta = 1 },
352
- .{ .lo = 0x3F9, .count = 1, .stride2 = false, .delta = -7 },
353
- .{ .lo = 0x3FA, .count = 1, .stride2 = false, .delta = 1 },
354
- .{ .lo = 0x3FD, .count = 3, .stride2 = false, .delta = -130 },
355
- .{ .lo = 0x400, .count = 16, .stride2 = false, .delta = 80 },
356
- .{ .lo = 0x410, .count = 32, .stride2 = false, .delta = 32 },
357
- .{ .lo = 0x460, .count = 17, .stride2 = true, .delta = 1 },
358
- .{ .lo = 0x48A, .count = 27, .stride2 = true, .delta = 1 },
359
- .{ .lo = 0x4C0, .count = 1, .stride2 = false, .delta = 15 },
360
- .{ .lo = 0x4C1, .count = 7, .stride2 = true, .delta = 1 },
361
- .{ .lo = 0x4D0, .count = 48, .stride2 = true, .delta = 1 },
362
- .{ .lo = 0x531, .count = 38, .stride2 = false, .delta = 48 },
363
- .{ .lo = 0x10A0, .count = 38, .stride2 = false, .delta = 7264 },
364
- .{ .lo = 0x10C7, .count = 1, .stride2 = false, .delta = 7264 },
365
- .{ .lo = 0x10CD, .count = 1, .stride2 = false, .delta = 7264 },
366
- .{ .lo = 0x13A0, .count = 80, .stride2 = false, .delta = 38864 },
367
- .{ .lo = 0x13F0, .count = 6, .stride2 = false, .delta = 8 },
368
- .{ .lo = 0x1C89, .count = 1, .stride2 = false, .delta = 1 },
369
- .{ .lo = 0x1C90, .count = 43, .stride2 = false, .delta = -3008 },
370
- .{ .lo = 0x1CBD, .count = 3, .stride2 = false, .delta = -3008 },
371
- .{ .lo = 0x1E00, .count = 75, .stride2 = true, .delta = 1 },
372
- .{ .lo = 0x1E9E, .count = 1, .stride2 = false, .delta = -7615 },
373
- .{ .lo = 0x1EA0, .count = 48, .stride2 = true, .delta = 1 },
374
- .{ .lo = 0x1F08, .count = 8, .stride2 = false, .delta = -8 },
375
- .{ .lo = 0x1F18, .count = 6, .stride2 = false, .delta = -8 },
376
- .{ .lo = 0x1F28, .count = 8, .stride2 = false, .delta = -8 },
377
- .{ .lo = 0x1F38, .count = 8, .stride2 = false, .delta = -8 },
378
- .{ .lo = 0x1F48, .count = 6, .stride2 = false, .delta = -8 },
379
- .{ .lo = 0x1F59, .count = 4, .stride2 = true, .delta = -8 },
380
- .{ .lo = 0x1F68, .count = 8, .stride2 = false, .delta = -8 },
381
- .{ .lo = 0x1F88, .count = 8, .stride2 = false, .delta = -8 },
382
- .{ .lo = 0x1F98, .count = 8, .stride2 = false, .delta = -8 },
383
- .{ .lo = 0x1FA8, .count = 8, .stride2 = false, .delta = -8 },
384
- .{ .lo = 0x1FB8, .count = 2, .stride2 = false, .delta = -8 },
385
- .{ .lo = 0x1FBA, .count = 2, .stride2 = false, .delta = -74 },
386
- .{ .lo = 0x1FBC, .count = 1, .stride2 = false, .delta = -9 },
387
- .{ .lo = 0x1FC8, .count = 4, .stride2 = false, .delta = -86 },
388
- .{ .lo = 0x1FCC, .count = 1, .stride2 = false, .delta = -9 },
389
- .{ .lo = 0x1FD8, .count = 2, .stride2 = false, .delta = -8 },
390
- .{ .lo = 0x1FDA, .count = 2, .stride2 = false, .delta = -100 },
391
- .{ .lo = 0x1FE8, .count = 2, .stride2 = false, .delta = -8 },
392
- .{ .lo = 0x1FEA, .count = 2, .stride2 = false, .delta = -112 },
393
- .{ .lo = 0x1FEC, .count = 1, .stride2 = false, .delta = -7 },
394
- .{ .lo = 0x1FF8, .count = 2, .stride2 = false, .delta = -128 },
395
- .{ .lo = 0x1FFA, .count = 2, .stride2 = false, .delta = -126 },
396
- .{ .lo = 0x1FFC, .count = 1, .stride2 = false, .delta = -9 },
397
- .{ .lo = 0x2126, .count = 1, .stride2 = false, .delta = -7517 },
398
- .{ .lo = 0x212A, .count = 1, .stride2 = false, .delta = -8383 },
399
- .{ .lo = 0x212B, .count = 1, .stride2 = false, .delta = -8262 },
400
- .{ .lo = 0x2132, .count = 1, .stride2 = false, .delta = 28 },
401
- .{ .lo = 0x2160, .count = 16, .stride2 = false, .delta = 16 },
402
- .{ .lo = 0x2183, .count = 1, .stride2 = false, .delta = 1 },
403
- .{ .lo = 0x24B6, .count = 26, .stride2 = false, .delta = 26 },
404
- .{ .lo = 0x2C00, .count = 48, .stride2 = false, .delta = 48 },
405
- .{ .lo = 0x2C60, .count = 1, .stride2 = false, .delta = 1 },
406
- .{ .lo = 0x2C62, .count = 1, .stride2 = false, .delta = -10743 },
407
- .{ .lo = 0x2C63, .count = 1, .stride2 = false, .delta = -3814 },
408
- .{ .lo = 0x2C64, .count = 1, .stride2 = false, .delta = -10727 },
409
- .{ .lo = 0x2C67, .count = 3, .stride2 = true, .delta = 1 },
410
- .{ .lo = 0x2C6D, .count = 1, .stride2 = false, .delta = -10780 },
411
- .{ .lo = 0x2C6E, .count = 1, .stride2 = false, .delta = -10749 },
412
- .{ .lo = 0x2C6F, .count = 1, .stride2 = false, .delta = -10783 },
413
- .{ .lo = 0x2C70, .count = 1, .stride2 = false, .delta = -10782 },
414
- .{ .lo = 0x2C72, .count = 1, .stride2 = false, .delta = 1 },
415
- .{ .lo = 0x2C75, .count = 1, .stride2 = false, .delta = 1 },
416
- .{ .lo = 0x2C7E, .count = 2, .stride2 = false, .delta = -10815 },
417
- .{ .lo = 0x2C80, .count = 50, .stride2 = true, .delta = 1 },
418
- .{ .lo = 0x2CEB, .count = 2, .stride2 = true, .delta = 1 },
419
- .{ .lo = 0x2CF2, .count = 1, .stride2 = false, .delta = 1 },
420
- .{ .lo = 0xA640, .count = 23, .stride2 = true, .delta = 1 },
421
- .{ .lo = 0xA680, .count = 14, .stride2 = true, .delta = 1 },
422
- .{ .lo = 0xA722, .count = 7, .stride2 = true, .delta = 1 },
423
- .{ .lo = 0xA732, .count = 31, .stride2 = true, .delta = 1 },
424
- .{ .lo = 0xA779, .count = 2, .stride2 = true, .delta = 1 },
425
- .{ .lo = 0xA77D, .count = 1, .stride2 = false, .delta = -35332 },
426
- .{ .lo = 0xA77E, .count = 5, .stride2 = true, .delta = 1 },
427
- .{ .lo = 0xA78B, .count = 1, .stride2 = false, .delta = 1 },
428
- .{ .lo = 0xA78D, .count = 1, .stride2 = false, .delta = -42280 },
429
- .{ .lo = 0xA790, .count = 2, .stride2 = true, .delta = 1 },
430
- .{ .lo = 0xA796, .count = 10, .stride2 = true, .delta = 1 },
431
- .{ .lo = 0xA7AA, .count = 1, .stride2 = false, .delta = -42308 },
432
- .{ .lo = 0xA7AB, .count = 1, .stride2 = false, .delta = -42319 },
433
- .{ .lo = 0xA7AC, .count = 1, .stride2 = false, .delta = -42315 },
434
- .{ .lo = 0xA7AD, .count = 1, .stride2 = false, .delta = -42305 },
435
- .{ .lo = 0xA7AE, .count = 1, .stride2 = false, .delta = -42308 },
436
- .{ .lo = 0xA7B0, .count = 1, .stride2 = false, .delta = -42258 },
437
- .{ .lo = 0xA7B1, .count = 1, .stride2 = false, .delta = -42282 },
438
- .{ .lo = 0xA7B2, .count = 1, .stride2 = false, .delta = -42261 },
439
- .{ .lo = 0xA7B3, .count = 1, .stride2 = false, .delta = 928 },
440
- .{ .lo = 0xA7B4, .count = 8, .stride2 = true, .delta = 1 },
441
- .{ .lo = 0xA7C4, .count = 1, .stride2 = false, .delta = -48 },
442
- .{ .lo = 0xA7C5, .count = 1, .stride2 = false, .delta = -42307 },
443
- .{ .lo = 0xA7C6, .count = 1, .stride2 = false, .delta = -35384 },
444
- .{ .lo = 0xA7C7, .count = 2, .stride2 = true, .delta = 1 },
445
- .{ .lo = 0xA7CB, .count = 1, .stride2 = false, .delta = -42343 },
446
- .{ .lo = 0xA7CC, .count = 8, .stride2 = true, .delta = 1 },
447
- .{ .lo = 0xA7DC, .count = 1, .stride2 = false, .delta = -42561 },
448
- .{ .lo = 0xA7F5, .count = 1, .stride2 = false, .delta = 1 },
449
- .{ .lo = 0xFF21, .count = 26, .stride2 = false, .delta = 32 },
450
- .{ .lo = 0x10400, .count = 40, .stride2 = false, .delta = 40 },
451
- .{ .lo = 0x104B0, .count = 36, .stride2 = false, .delta = 40 },
452
- .{ .lo = 0x10570, .count = 11, .stride2 = false, .delta = 39 },
453
- .{ .lo = 0x1057C, .count = 15, .stride2 = false, .delta = 39 },
454
- .{ .lo = 0x1058C, .count = 7, .stride2 = false, .delta = 39 },
455
- .{ .lo = 0x10594, .count = 2, .stride2 = false, .delta = 39 },
456
- .{ .lo = 0x10C80, .count = 51, .stride2 = false, .delta = 64 },
457
- .{ .lo = 0x10D50, .count = 22, .stride2 = false, .delta = 32 },
458
- .{ .lo = 0x118A0, .count = 32, .stride2 = false, .delta = 32 },
459
- .{ .lo = 0x16E40, .count = 32, .stride2 = false, .delta = 32 },
460
- .{ .lo = 0x16EA0, .count = 25, .stride2 = false, .delta = 27 },
461
- .{ .lo = 0x1E900, .count = 34, .stride2 = false, .delta = 34 },
462
- };
463
- // ---- END GENERATED CASE TABLES ----
464
-
465
- /// Simple case mapping of one code point through a range table (binary
466
- /// search; unmapped code points map to themselves).
467
- fn caseMapLookup(ranges: []const CaseRange, cp: u21) u21 {
468
- var lo: usize = 0;
469
- var hi: usize = ranges.len;
470
- while (lo < hi) {
471
- const mid = lo + (hi - lo) / 2;
472
- const r = ranges[mid];
473
- const stride: u32 = if (r.stride2) 2 else 1;
474
- const last: u32 = @as(u32, r.lo) + (@as(u32, r.count) - 1) * stride;
475
- if (cp < r.lo) {
476
- hi = mid;
477
- } else if (cp > last) {
478
- lo = mid + 1;
479
- } else {
480
- if ((cp - r.lo) % stride == 0) {
481
- return @intCast(@as(i32, cp) + r.delta);
482
- }
483
- return cp;
484
- }
485
- }
486
- return cp;
487
- }
488
-
489
- /// One strict UTF-8 decode step (RFC 3629 well-formed: no overlongs, no
490
- /// surrogates, nothing above U+10FFFF). `valid = false` means the byte at
491
- /// `i` does not begin a well-formed sequence — the caller passes that ONE
492
- /// byte through unchanged and re-tries at the next byte. The node polyfill
493
- /// walks bytes with exactly this rule.
494
- const Utf8Step = struct { cp: u21, len: u3, valid: bool };
495
-
496
- fn utf8DecodeAt(bytes: []const u8, i: usize) Utf8Step {
497
- const invalid: Utf8Step = .{ .cp = 0, .len = 1, .valid = false };
498
- const b0 = bytes[i];
499
- if (b0 < 0x80) return .{ .cp = b0, .len = 1, .valid = true };
500
- if (b0 < 0xC2) return invalid; // continuation byte or overlong lead
501
- if (b0 < 0xE0) {
502
- if (i + 1 >= bytes.len) return invalid;
503
- const b1 = bytes[i + 1];
504
- if (b1 & 0xC0 != 0x80) return invalid;
505
- return .{ .cp = (@as(u21, b0 & 0x1F) << 6) | (b1 & 0x3F), .len = 2, .valid = true };
506
- }
507
- if (b0 < 0xF0) {
508
- if (i + 2 >= bytes.len) return invalid;
509
- const b1 = bytes[i + 1];
510
- const b2 = bytes[i + 2];
511
- if (b1 & 0xC0 != 0x80 or b2 & 0xC0 != 0x80) return invalid;
512
- if (b0 == 0xE0 and b1 < 0xA0) return invalid; // overlong
513
- if (b0 == 0xED and b1 > 0x9F) return invalid; // surrogate
514
- return .{ .cp = (@as(u21, b0 & 0x0F) << 12) | (@as(u21, b1 & 0x3F) << 6) | (b2 & 0x3F), .len = 3, .valid = true };
515
- }
516
- if (b0 < 0xF5) {
517
- if (i + 3 >= bytes.len) return invalid;
518
- const b1 = bytes[i + 1];
519
- const b2 = bytes[i + 2];
520
- const b3 = bytes[i + 3];
521
- if (b1 & 0xC0 != 0x80 or b2 & 0xC0 != 0x80 or b3 & 0xC0 != 0x80) return invalid;
522
- if (b0 == 0xF0 and b1 < 0x90) return invalid; // overlong
523
- if (b0 == 0xF4 and b1 > 0x8F) return invalid; // above U+10FFFF
524
- return .{
525
- .cp = (@as(u21, b0 & 0x07) << 18) | (@as(u21, b1 & 0x3F) << 12) | (@as(u21, b2 & 0x3F) << 6) | (b3 & 0x3F),
526
- .len = 4,
527
- .valid = true,
528
- };
529
- }
530
- return invalid;
531
- }
532
-
533
- /// Decode the well-formed sequence ENDING at `end` (exclusive), for the
534
- /// backward walks (trimEnd). `valid = false` when the bytes before `end`
535
- /// are not one whole well-formed sequence.
536
- fn utf8DecodeBefore(bytes: []const u8, end: usize) Utf8Step {
537
- const invalid: Utf8Step = .{ .cp = 0, .len = 1, .valid = false };
538
- var back: usize = 1;
539
- while (back <= 4 and back <= end) : (back += 1) {
540
- const b = bytes[end - back];
541
- if (b & 0xC0 != 0x80) {
542
- const step = utf8DecodeAt(bytes, end - back);
543
- if (step.valid and step.len == back) return step;
544
- return invalid;
545
- }
546
- }
547
- return invalid;
548
- }
549
-
550
- fn utf8EncodedLen(cp: u21) usize {
551
- if (cp < 0x80) return 1;
552
- if (cp < 0x800) return 2;
553
- if (cp < 0x10000) return 3;
554
- return 4;
555
- }
556
-
557
- fn utf8EncodeInto(out: []u8, at: usize, cp: u21) usize {
558
- if (cp < 0x80) {
559
- out[at] = @intCast(cp);
560
- return 1;
561
- }
562
- if (cp < 0x800) {
563
- out[at] = 0xC0 | @as(u8, @intCast(cp >> 6));
564
- out[at + 1] = 0x80 | @as(u8, @intCast(cp & 0x3F));
565
- return 2;
566
- }
567
- if (cp < 0x10000) {
568
- out[at] = 0xE0 | @as(u8, @intCast(cp >> 12));
569
- out[at + 1] = 0x80 | @as(u8, @intCast((cp >> 6) & 0x3F));
570
- out[at + 2] = 0x80 | @as(u8, @intCast(cp & 0x3F));
571
- return 3;
572
- }
573
- out[at] = 0xF0 | @as(u8, @intCast(cp >> 18));
574
- out[at + 1] = 0x80 | @as(u8, @intCast((cp >> 12) & 0x3F));
575
- out[at + 2] = 0x80 | @as(u8, @intCast((cp >> 6) & 0x3F));
576
- out[at + 3] = 0x80 | @as(u8, @intCast(cp & 0x3F));
577
- return 4;
578
- }
579
-
580
- /// The JS WhiteSpace + LineTerminator set — exactly the code points
581
- /// `String.prototype.trim` strips (probed against node; ES2025 11.2/12.3).
582
- fn isJsWhitespace(cp: u21) bool {
583
- return switch (cp) {
584
- 0x09...0x0D, 0x20, 0xA0, 0x1680, 0x2000...0x200A, 0x2028, 0x2029, 0x202F, 0x205F, 0x3000, 0xFEFF => true,
585
- else => false,
586
- };
587
- }
588
-
589
- /// App-configurable kernel capacities (bytes). The defaults carry a small
590
- /// desktop app comfortably; raise them per app through the transpiler's
591
- /// capacity options rather than editing this file.
592
- pub const Options = struct {
593
- /// Frame arena capacity: the transient budget of ONE dispatch.
594
- frame_cap: usize = 1 << 20, // 1 MiB
595
- /// Model heap capacity PER SPACE (two spaces exist): the committed
596
- /// model graph plus not-yet-compacted garbage must fit in one space.
597
- heap_cap: usize = 1 << 20, // 1 MiB
598
- };
599
-
600
- /// The default-capacity kernel. `Kernel(.{})` memoizes to this instance.
601
- pub const default = Kernel(.{});
602
-
603
- pub fn Kernel(comptime opts: Options) type {
604
- return struct {
605
- pub const frame_cap: usize = opts.frame_cap;
606
- pub const heap_cap: usize = opts.heap_cap;
607
- pub const heap_watermark: usize = heap_cap * 3 / 4;
608
-
609
- var frame_buf: [frame_cap]u8 align(16) = undefined;
610
- var frame_off: usize = 0;
611
-
612
- var heap_bufs: [2][heap_cap]u8 align(16) = undefined;
613
- var heap_cur: usize = 0;
614
- var heap_off: usize = 0;
615
-
616
- // ---------------------------------------------------------- stats
617
-
618
- pub var stat_frame_last: usize = 0; // frame bytes used by the last dispatch
619
- pub var stat_frame_peak: usize = 0; // max frame bytes over any dispatch
620
- pub var stat_commit_last: usize = 0; // heap bytes committed by the last dispatch
621
- pub var stat_compactions: u64 = 0;
622
-
623
- // ---------------------------------------------------- frame arena
624
-
625
- pub fn frameAlloc(comptime T: type, n: usize) []T {
626
- const size = n *% @sizeOf(T);
627
- const aligned = std.mem.alignForward(usize, frame_off, @alignOf(T));
628
- const next = aligned + size;
629
- if (next > frame_cap) {
630
- @panic(std.fmt.comptimePrint(
631
- "frame arena overflow: one dispatch built more than the {d}-byte frame capacity; raise it with the frameCap transpile option (--frame-cap, rt.Options.frame_cap)",
632
- .{frame_cap},
633
- ));
634
- }
635
- frame_off = next;
636
- const ptr: [*]T = @ptrCast(@alignCast(frame_buf[aligned..].ptr));
637
- return ptr[0..n];
638
- }
639
-
640
- pub fn frameCreate(comptime T: type, value: T) *T {
641
- const slot = frameAlloc(T, 1);
642
- slot[0] = value;
643
- return &slot[0];
644
- }
645
-
646
- /// Capacity growth for the push-builder lowering (`const out: T[] = []` +
647
- /// `out.push(x)` in a loop): bump-allocate double the capacity and copy the
648
- /// filled prefix. The abandoned block is frame-transient by construction —
649
- /// the whole arena resets after the dispatch commits.
650
- pub fn frameGrow(comptime T: type, old: []T) []T {
651
- const cap = if (old.len == 0) 4 else old.len * 2;
652
- const out = frameAlloc(T, cap);
653
- @memcpy(out[0..old.len], old);
654
- return out;
655
- }
656
-
657
- pub fn frameReset() void {
658
- stat_frame_last = frame_off;
659
- if (frame_off > stat_frame_peak) stat_frame_peak = frame_off;
660
- frame_off = 0;
661
- }
662
-
663
- pub fn inFrame(addr: usize) bool {
664
- const base = @intFromPtr(&frame_buf);
665
- return addr >= base and addr < base + frame_cap;
666
- }
667
-
668
- // ----------------------------------------------------- model heap
669
-
670
- pub fn heapAlloc(comptime T: type, n: usize) []T {
671
- const size = n *% @sizeOf(T);
672
- const aligned = std.mem.alignForward(usize, heap_off, @alignOf(T));
673
- const next = aligned + size;
674
- if (next > heap_cap) {
675
- @panic(std.fmt.comptimePrint(
676
- "model heap overflow: the committed model outgrew the {d}-byte per-space heap capacity; raise it with the heapCap transpile option (--heap-cap, rt.Options.heap_cap)",
677
- .{heap_cap},
678
- ));
679
- }
680
- heap_off = next;
681
- const ptr: [*]T = @ptrCast(@alignCast(heap_bufs[heap_cur][aligned..].ptr));
682
- return ptr[0..n];
683
- }
684
-
685
- pub fn heapCreate(comptime T: type, value: T) *T {
686
- const slot = heapAlloc(T, 1);
687
- slot[0] = value;
688
- return &slot[0];
689
- }
690
-
691
- pub fn heapUsed() usize {
692
- return heap_off;
693
- }
694
-
695
- /// Flip to the other space (compaction target). The old space stays readable
696
- /// until the copy completes; its bytes are dead afterwards.
697
- pub fn heapFlip() void {
698
- heap_cur ^= 1;
699
- heap_off = 0;
700
- }
701
-
702
- pub fn currentHeapBase() usize {
703
- return @intFromPtr(&heap_bufs[heap_cur]);
704
- }
705
-
706
- pub fn inCurrentHeap(addr: usize) bool {
707
- const base = @intFromPtr(&heap_bufs[heap_cur]);
708
- return addr >= base and addr < base + heap_cap;
709
- }
710
-
711
- // ------------------------------------------------------ number ops
712
- //
713
- // Float helpers for the sites where Zig's builtins differ from JS number
714
- // semantics. @min/@max resolve a NaN operand to the other value and pick
715
- // either zero when the magnitudes tie; JS Math.min/max propagate NaN and
716
- // order -0 below +0. @round rounds half away from zero; JS Math.round is
717
- // half toward positive infinity, decides the .5 boundary on the exact value
718
- // (never on a lossy x + 0.5), and keeps the zero sign of negative inputs.
719
-
720
- pub fn jsMin(a: f64, b: f64) f64 {
721
- if (std.math.isNan(a) or std.math.isNan(b)) return std.math.nan(f64);
722
- if (a == 0.0 and b == 0.0) return if (std.math.signbit(a)) a else b;
723
- return if (a < b) a else b;
724
- }
725
-
726
- pub fn jsMax(a: f64, b: f64) f64 {
727
- if (std.math.isNan(a) or std.math.isNan(b)) return std.math.nan(f64);
728
- if (a == 0.0 and b == 0.0) return if (std.math.signbit(a)) b else a;
729
- return if (a > b) a else b;
730
- }
731
-
732
- /// JS Math.sign on floats: NaN stays NaN and a zero keeps its sign (the
733
- /// comparisons are false for NaN and both zeros, so `x` falls through).
734
- pub fn jsSign(x: f64) f64 {
735
- if (x > 0) return 1.0;
736
- if (x < 0) return -1.0;
737
- return x;
738
- }
739
-
740
- /// JS Number.isInteger on a float value: finite and integral (-0 included;
741
- /// NaN and the infinities fail the comparisons/finiteness test).
742
- pub fn jsIsInteger(x: f64) bool {
743
- return std.math.isFinite(x) and @floor(x) == x;
744
- }
745
-
746
- pub fn jsRound(x: f64) f64 {
747
- if (!std.math.isFinite(x)) return x;
748
- const fl = @floor(x);
749
- // Deciding on the fractional part (not on a lossy x + 0.5) keeps the .5
750
- // boundary exact: x - fl is exact wherever the true fraction can sit near
751
- // one half, and elsewhere its rounding cannot carry the comparison across
752
- // the boundary.
753
- const rounded = if (x - fl >= 0.5) fl + 1.0 else fl;
754
- if (rounded == 0.0 and std.math.signbit(x)) return -0.0;
755
- return rounded;
756
- }
757
-
758
- /// JS `Array.prototype.slice` index resolution: a negative index counts from
759
- /// the end, then everything clamps into [0, len]. Emitted `.slice` sites
760
- /// resolve both bounds through here so out-of-range arguments produce the
761
- /// clamped copy JS produces, never an index trap.
762
- pub fn sliceIndex(len: usize, v: i64) usize {
763
- if (v < 0) {
764
- const from_end = @as(i64, @intCast(len)) + v;
765
- return if (from_end < 0) 0 else @intCast(from_end);
766
- }
767
- if (v > @as(i64, @intCast(len))) return len;
768
- return @intCast(v);
769
- }
770
-
771
- /// JS `TypedArray.prototype.subarray` bound resolution: both bounds resolve
772
- /// the slice way (negatives count from the end, everything clamps into
773
- /// [0, len]) and a crossed range is empty. A view, never a copy. `to` is
774
- /// null when the source omits the second argument (view to the end).
775
- pub fn subarray(bytes: []const u8, from: i64, to: ?i64) []const u8 {
776
- const lo = sliceIndex(bytes.len, from);
777
- const hi = @max(lo, if (to) |t| sliceIndex(bytes.len, t) else bytes.len);
778
- return bytes[lo..hi];
779
- }
780
-
781
- // -------------------------------------------------------- byte text
782
- //
783
- // The emitted lowerings of the everyday string methods on bytes
784
- // (`s.toUpperCase()`, `s.repeat(3)`, ...). Semantics are pinned to
785
- // the file-header contract: byte-honest measurements, Unicode
786
- // SIMPLE case mapping over the generated tables, invalid UTF-8
787
- // passed through unchanged, and results either fresh frame-arena
788
- // bytes (case map, repeat, pad, split's array) or views of the
789
- // receiver (trim family, at-length pads) — the node polyfill
790
- // aliases identically.
791
-
792
- /// Shared case-map walk: exact-size two-pass (measure, then fill).
793
- fn textCaseMap(bytes: []const u8, ranges: []const CaseRange) []const u8 {
794
- var out_len: usize = 0;
795
- var i: usize = 0;
796
- while (i < bytes.len) {
797
- const step = utf8DecodeAt(bytes, i);
798
- if (!step.valid) {
799
- out_len += 1;
800
- i += 1;
801
- continue;
802
- }
803
- out_len += utf8EncodedLen(caseMapLookup(ranges, step.cp));
804
- i += step.len;
805
- }
806
- const out = frameAlloc(u8, out_len);
807
- var at: usize = 0;
808
- i = 0;
809
- while (i < bytes.len) {
810
- const step = utf8DecodeAt(bytes, i);
811
- if (!step.valid) {
812
- out[at] = bytes[i];
813
- at += 1;
814
- i += 1;
815
- continue;
816
- }
817
- at += utf8EncodeInto(out, at, caseMapLookup(ranges, step.cp));
818
- i += step.len;
819
- }
820
- return out;
821
- }
822
-
823
- pub fn textUpper(bytes: []const u8) []const u8 {
824
- return textCaseMap(bytes, &simple_upper_ranges);
825
- }
826
-
827
- pub fn textLower(bytes: []const u8) []const u8 {
828
- return textCaseMap(bytes, &simple_lower_ranges);
829
- }
830
-
831
- /// `.repeat(n)`. A negative count is JS's RangeError site; builtins
832
- /// never throw here, so the native form is the deterministic panic
833
- /// an uncaught throw would be — guard the count first.
834
- pub fn textRepeat(bytes: []const u8, n: i64) []const u8 {
835
- if (n < 0) @panic("repeat count is negative: JS throws RangeError here — guard the count (n >= 0) before calling .repeat");
836
- const times: usize = @intCast(n);
837
- const out = frameAlloc(u8, bytes.len * times);
838
- var off: usize = 0;
839
- var k: usize = 0;
840
- while (k < times) : (k += 1) {
841
- @memcpy(out[off..][0..bytes.len], bytes);
842
- off += bytes.len;
843
- }
844
- return out;
845
- }
846
-
847
- /// `.indexOf(needle)` with a bytes needle: first byte offset of the
848
- /// substring, -1 when absent; the empty needle matches at 0 (JS).
849
- pub fn textIndexOf(hay: []const u8, needle: []const u8) i64 {
850
- if (needle.len > hay.len) return -1;
851
- var s: usize = 0;
852
- while (s + needle.len <= hay.len) : (s += 1) {
853
- if (std.mem.eql(u8, hay[s..][0..needle.len], needle)) return @intCast(s);
854
- }
855
- return -1;
856
- }
857
-
858
- /// `.lastIndexOf(needle)` with a bytes needle: last byte offset,
859
- /// -1 when absent; the empty needle matches at hay.len (JS).
860
- pub fn textLastIndexOf(hay: []const u8, needle: []const u8) i64 {
861
- if (needle.len > hay.len) return -1;
862
- var s: usize = hay.len - needle.len;
863
- while (true) : (s -= 1) {
864
- if (std.mem.eql(u8, hay[s..][0..needle.len], needle)) return @intCast(s);
865
- if (s == 0) return -1;
866
- }
867
- }
868
-
869
- pub fn textIncludes(hay: []const u8, needle: []const u8) bool {
870
- return textIndexOf(hay, needle) != -1;
871
- }
872
-
873
- pub fn textStartsWith(hay: []const u8, needle: []const u8) bool {
874
- return needle.len <= hay.len and std.mem.eql(u8, hay[0..needle.len], needle);
875
- }
876
-
877
- pub fn textEndsWith(hay: []const u8, needle: []const u8) bool {
878
- return needle.len <= hay.len and std.mem.eql(u8, hay[hay.len - needle.len ..], needle);
879
- }
880
-
881
- /// The number-needle forms (JS TypedArray element search,
882
- /// SameValueZero over byte values: 66.5 matches nothing, -0
883
- /// matches 0, NaN matches nothing — a u8 element is never NaN).
884
- pub fn textIndexOfByte(hay: []const u8, x: f64) i64 {
885
- for (hay, 0..) |b, i| {
886
- if (@as(f64, @floatFromInt(b)) == x) return @intCast(i);
887
- }
888
- return -1;
889
- }
890
-
891
- pub fn textLastIndexOfByte(hay: []const u8, x: f64) i64 {
892
- var i: usize = hay.len;
893
- while (i > 0) {
894
- i -= 1;
895
- if (@as(f64, @floatFromInt(hay[i])) == x) return @intCast(i);
896
- }
897
- return -1;
898
- }
899
-
900
- pub fn textIncludesByte(hay: []const u8, x: f64) bool {
901
- return textIndexOfByte(hay, x) != -1;
902
- }
903
-
904
- /// `.padStart(n, fill)` — n is the target BYTE length. At-or-under
905
- /// target (or an empty fill) returns the receiver itself, exactly
906
- /// the polyfill's aliasing; the last fill repetition truncates (JS).
907
- pub fn textPadStart(bytes: []const u8, n: i64, fill: []const u8) []const u8 {
908
- if (n <= 0) return bytes;
909
- const target: usize = @intCast(n);
910
- if (target <= bytes.len or fill.len == 0) return bytes;
911
- const out = frameAlloc(u8, target);
912
- const pad = target - bytes.len;
913
- var off: usize = 0;
914
- while (off < pad) {
915
- const chunk = @min(fill.len, pad - off);
916
- @memcpy(out[off..][0..chunk], fill[0..chunk]);
917
- off += chunk;
918
- }
919
- @memcpy(out[pad..], bytes);
920
- return out;
921
- }
922
-
923
- pub fn textPadEnd(bytes: []const u8, n: i64, fill: []const u8) []const u8 {
924
- if (n <= 0) return bytes;
925
- const target: usize = @intCast(n);
926
- if (target <= bytes.len or fill.len == 0) return bytes;
927
- const out = frameAlloc(u8, target);
928
- @memcpy(out[0..bytes.len], bytes);
929
- var off: usize = bytes.len;
930
- while (off < target) {
931
- const chunk = @min(fill.len, target - off);
932
- @memcpy(out[off..][0..chunk], fill[0..chunk]);
933
- off += chunk;
934
- }
935
- return out;
936
- }
937
-
938
- /// The trim family strips the JS whitespace set (WhiteSpace +
939
- /// LineTerminator, decoded over UTF-8) and returns a VIEW — no
940
- /// copy. A byte run that is not well-formed UTF-8 is not
941
- /// whitespace, so trimming stops there.
942
- pub fn textTrimStart(bytes: []const u8) []const u8 {
943
- var i: usize = 0;
944
- while (i < bytes.len) {
945
- const step = utf8DecodeAt(bytes, i);
946
- if (!step.valid or !isJsWhitespace(step.cp)) break;
947
- i += step.len;
948
- }
949
- return bytes[i..];
950
- }
951
-
952
- pub fn textTrimEnd(bytes: []const u8) []const u8 {
953
- var end: usize = bytes.len;
954
- while (end > 0) {
955
- const step = utf8DecodeBefore(bytes, end);
956
- if (!step.valid or !isJsWhitespace(step.cp)) break;
957
- end -= step.len;
958
- }
959
- return bytes[0..end];
960
- }
961
-
962
- pub fn textTrim(bytes: []const u8) []const u8 {
963
- return textTrimEnd(textTrimStart(bytes));
964
- }
965
-
966
- /// `.split(sep)` with a bytes separator: non-overlapping, left to
967
- /// right, String.split shapes exactly (leading/trailing/adjacent
968
- /// separators produce empty elements; no match returns [whole]).
969
- /// Elements are VIEWS into the receiver; the array itself is fresh
970
- /// frame-arena memory (locally owned by the caller). An EMPTY
971
- /// separator returns [whole] — per-code-point splitting is the
972
- /// taught stop at compile time; this pins the dynamic case.
973
- pub fn textSplit(hay: []const u8, sep: []const u8) [][]const u8 {
974
- if (sep.len == 0) {
975
- const whole = frameAlloc([]const u8, 1);
976
- whole[0] = hay;
977
- return whole;
978
- }
979
- var count: usize = 1;
980
- var i: usize = 0;
981
- while (i + sep.len <= hay.len) {
982
- if (std.mem.eql(u8, hay[i..][0..sep.len], sep)) {
983
- count += 1;
984
- i += sep.len;
985
- } else {
986
- i += 1;
987
- }
988
- }
989
- const out = frameAlloc([]const u8, count);
990
- var part: usize = 0;
991
- var start: usize = 0;
992
- i = 0;
993
- while (i + sep.len <= hay.len) {
994
- if (std.mem.eql(u8, hay[i..][0..sep.len], sep)) {
995
- out[part] = hay[start..i];
996
- part += 1;
997
- i += sep.len;
998
- start = i;
999
- } else {
1000
- i += 1;
1001
- }
1002
- }
1003
- out[part] = hay[start..];
1004
- return out;
1005
- }
1006
-
1007
- /// `.at(i)` — byte access: the byte value at a (negative counts
1008
- /// from the end) byte index, or the empty (JS undefined) when out
1009
- /// of range.
1010
- pub fn textAt(bytes: []const u8, i: i64) ?i64 {
1011
- const idx = if (i < 0) i + @as(i64, @intCast(bytes.len)) else i;
1012
- if (idx < 0 or idx >= @as(i64, @intCast(bytes.len))) return null;
1013
- return @as(i64, bytes[@intCast(idx)]);
1014
- }
1015
-
1016
- // -------------------------------------------------------- commands
1017
- //
1018
- // Cmd wire format v3 — the byte encoding of the command value an effectful
1019
- // `update` (or a pair-returning `initialModel`) returns alongside the next
1020
- // model (`UpdateResult.cmd` / `InitResult.cmd` in emitted cores). A command
1021
- // value is a flat sequence of op records; the empty slice means "no
1022
- // effects", and batching is plain concatenation:
1023
- //
1024
- // persist [op 0x01]
1025
- // now [op 0x02][msg_tag u8]
1026
- // host [op 0x03][name_len u8][name bytes][argc u8][arg f64 LE] * argc
1027
- // host_bytes [op 0x04][name_len u8][name bytes][payload_len u32 LE][payload bytes]
1028
- // request [op 0x05][name_len u8][name bytes][key_len u8][key bytes]
1029
- // [ok_tag u8][err_tag u8][payload_len u32 LE][payload bytes]
1030
- // cancel [op 0x06][key_len u8][key bytes]
1031
- // read_file [op 0x07][key_len u8][key bytes][ok_tag u8][err_tag u8]
1032
- // [path_len u32 LE][path bytes]
1033
- // write_file [op 0x08][key_len u8][key bytes][ok_tag u8][err_tag u8]
1034
- // [path_len u32 LE][path bytes][bytes_len u32 LE][bytes]
1035
- // fetch [op 0x09][key_len u8][key bytes][ok_tag u8][err_tag u8]
1036
- // [method u8][timeout_ms u32 LE][url_len u32 LE][url bytes]
1037
- // [header_count u8] then per header, in TS-field-name
1038
- // (code-unit) sort order: [name_len u8][name bytes]
1039
- // [value_len u32 LE][value bytes], then
1040
- // [body_len u32 LE][body bytes]. Header NAMES are
1041
- // compile-time text; header VALUES may be runtime
1042
- // bytes — the record is built at dispatch time, so a
1043
- // runtime value encodes exactly like a literal one
1044
- // (same length-prefixed field, no layout change).
1045
- // clip_write [op 0x0A][bytes_len u32 LE][bytes]
1046
- // clip_read [op 0x0B][key_len u8][key bytes][ok_tag u8][err_tag u8]
1047
- // delay [op 0x0C][key_len u8][key bytes][after_ms f64 LE][msg_tag u8]
1048
- // spawn [op 0x0D][key_len u8][key bytes][line_tag u8][exit_tag u8]
1049
- // [err_tag u8][mode u8][argc u8] then per argv element:
1050
- // [arg_len u32 LE][arg bytes], then [stdin_len u32 LE][stdin]
1051
- // audio_play [op 0x0E][key_len u8][key bytes][event_tag u8]
1052
- // [path_len u32 LE][path][url_len u32 LE][url]
1053
- // [cache_len u32 LE][cache_path][expected_bytes f64 LE]
1054
- // audio_ctl [op 0x0F][key_len u8][key bytes][verb u8][value f64 LE]
1055
- // window_show [op 0x10][label_len u8][label bytes]
1056
- // quit_app [op 0x11]
1057
- // image_load [op 0x12][id f64 LE][event_tag u8]
1058
- // [path_len u32 LE][path][url_len u32 LE][url]
1059
- // [cache_len u32 LE][cache_path][expected_bytes f64 LE]
1060
- // image_cancel [op 0x13][id f64 LE]
1061
- // image_unregister [op 0x14][id f64 LE]
1062
- // channel_open [op 0x15][key f64 LE][event_tag u8]
1063
- // channel_close [op 0x16][key f64 LE]
1064
- // video_load [op 0x17][key_len u8][key][event_tag u8]
1065
- // [surface f64 LE][path_len u32 LE][path]
1066
- // [url_len u32 LE][url][flags u8]
1067
- // (flags bit0 = autoplay, bit1 = loop, bit2 = muted)
1068
- // video_ctl [op 0x18][key_len u8][key][verb u8][value f64 LE]
1069
- // pty_spawn [op 0x19][key_len u8][key bytes][event_tag u8]
1070
- // [cols f64 LE][rows f64 LE][term_len u8][term bytes]
1071
- // then [argc u8] and per argv element:
1072
- // [arg_len u32 LE][arg bytes]
1073
- // pty_write [op 0x1A][key_len u8][key bytes][bytes_len u32 LE][bytes]
1074
- // pty_resize [op 0x1B][key_len u8][key bytes][cols f64 LE][rows f64 LE]
1075
- // pty_kill [op 0x1C][key_len u8][key bytes]
1076
- //
1077
- // v2 is additive over v1: the 0x01-0x03 records are byte-identical to v1,
1078
- // so a v1 effect log replays under a v2 reader unchanged. The named-op
1079
- // records 0x07-0x0C are additive within v2 the same way, and so are the
1080
- // streaming records 0x0D-0x0F: no existing record's bytes change, new
1081
- // opcodes only. v3 appends the window-verb records 0x10-0x11 under the
1082
- // same rule, and the image records 0x12-0x14, channel records
1083
- // 0x15-0x16, and pty records 0x19-0x1C are additive within v3
1084
- // the same way: every existing record is byte-identical, new opcodes
1085
- // only (a host predating an opcode refuses it loudly, naming this
1086
- // version). The video records 0x17-0x18 join additively under
1087
- // the same rule.
1088
- //
1089
- // `method` is the closed HTTP verb set in declaration order of `CmdFetchMethod`
1090
- // (GET 0, POST 1, PUT 2, DELETE 3, PATCH 4, HEAD 5). `timeout_ms` 0 means
1091
- // "the host engine's default fetch timeout" — the wire never bakes the
1092
- // default in, so re-tuning the engine re-tunes old cores too.
1093
- //
1094
- // `msg_tag` (and `ok_tag`/`err_tag`) is the declaration-order index of the
1095
- // app's Msg arm the runtime dispatches with the result payload; scalar args
1096
- // are f64 because every subset number is one. Enum/tag payloads ride
1097
- // declaration order across this whole channel (R5), so reordering Msg arms
1098
- // or CmdOp members is a format break: bump `cmd_format_version` when one
1099
- // moves, and for any record-layout change. Hosts that persist or transport
1100
- // a command log must store the version alongside the bytes; readers refuse
1101
- // other versions loudly rather than misreading yesterday's shape.
1102
- //
1103
- // Record semantics for the host:
1104
- // host_bytes fire-and-forget host command carrying one bytes payload
1105
- // (raw `Uint8Array` app data, or a host record — see the
1106
- // record encoding below).
1107
- // request a keyed, routed host command: the host performs `name`
1108
- // with `payload` and dispatches exactly one result Msg —
1109
- // the `ok_tag` arm with the result bytes as its single
1110
- // payload field on success, or the `err_tag` arm with the
1111
- // error bytes on failure. `key` (may be empty = unkeyed)
1112
- // names the in-flight effect for dedup/cancel: issuing a
1113
- // request whose key is already in flight replaces the old
1114
- // one (its result is dropped), and `cancel` with the same
1115
- // key drops it without dispatching either arm.
1116
- // cancel drop the in-flight keyed effect with this key, if
1117
- // any, SILENTLY: a cancelled request, named engine
1118
- // effect (read_file/write_file/fetch/clip_read), or
1119
- // delay dispatches neither arm — its result is simply
1120
- // dropped. The exception is a live spawn stream: cancel
1121
- // ends the child and its err arm delivers "cancelled"
1122
- // (killing a process is an observable event, kept
1123
- // loud). An open audio stream is not cancel's to end —
1124
- // close it with audio_ctl stop.
1125
- // read_file routed whole-file read: exactly one terminal — the ok
1126
- // arm with the file's bytes as its single bytes payload,
1127
- // or the err arm with the failure reason as bytes
1128
- // ("not_found", "io_failed", "truncated", "rejected").
1129
- // write_file routed whole-file write (parent directories created,
1130
- // existing file replaced whole): exactly one terminal —
1131
- // the ok arm, which carries NO payload (a successful
1132
- // write has nothing to report beyond success), or the
1133
- // err arm with the reason bytes ("io_failed",
1134
- // "rejected").
1135
- // fetch routed buffered HTTP(S) exchange: exactly one terminal
1136
- // — the ok arm, whose payload is a two-field record of
1137
- // one number field (the real HTTP status, non-2xx
1138
- // included) and one bytes field (the whole body), or the
1139
- // err arm with the reason bytes ("connect_failed",
1140
- // "tls_failed", "protocol_failed", "timed_out",
1141
- // "rejected", "truncated" — a body over the engine's
1142
- // buffered bound routes err rather than passing a
1143
- // silently cut body as ok).
1144
- // clip_write fire-and-forget clipboard write; an over-bound or
1145
- // refused write is dropped (documented, not silent-by-
1146
- // accident: there is no route to report on).
1147
- // clip_read routed clipboard read: the ok arm with the clipboard
1148
- // text as its single bytes payload, or the err arm with
1149
- // the reason bytes ("failed", "rejected").
1150
- // delay a keyed ONE-SHOT timer: after `after_ms` the host
1151
- // dispatches the `msg_tag` arm once, with the fire time
1152
- // (fractional ms) as its single number payload — the
1153
- // same arm shape `now` and Sub timer fires target.
1154
- // Re-issuing a live delay key re-arms it from now (the
1155
- // debounce discipline); `cancel` with the key drops it
1156
- // silently.
1157
- // spawn a STREAMING effect: run `argv` (each element its own
1158
- // bytes field; `mode` 0 streams stdout lines, 1 collects
1159
- // whole stdout), writing `stdin` to the child once when
1160
- // non-empty. Unlike every op above, one spawn dispatches
1161
- // MANY result Msgs across dispatches: in line mode each
1162
- // stdout line dispatches the `line_tag` arm with the
1163
- // line bytes (line_tag 0xFF = no line routing, lines are
1164
- // dropped), and exactly ONE terminal follows — a clean
1165
- // `.exited` end dispatches the `exit_tag` arm (line
1166
- // mode: the exit code as its single number payload;
1167
- // collect mode: a two-field code/output record, one
1168
- // number field and one bytes field matched by type),
1169
- // while every other end (signaled, cancelled, rejected,
1170
- // spawn_failed, or a collect stdout over the engine's
1171
- // 512 KiB bound, reason bytes "truncated") dispatches
1172
- // the `err_tag` arm with the reason bytes. The key is
1173
- // live for the whole stream: `cancel` mid-stream ends
1174
- // the child and the err arm runs with "cancelled" —
1175
- // killing a process is an observable event, kept loud —
1176
- // and a spawn whose key is already streaming REJECTS
1177
- // the new spawn (err arm "rejected"): a running
1178
- // subprocess is never killed implicitly; cancel it
1179
- // first.
1180
- // audio_play open (or replace — one player is the whole surface)
1181
- // the keyed audio EVENT STREAM: resolve the source
1182
- // cascade (local `path` first, then `url`, cached at
1183
- // `cache_path` and integrity-gated by `expected_bytes`;
1184
- // 0 = unknown size, empty fields = absent) and start
1185
- // playback. Every playback event — loaded, position
1186
- // ticks, completed, failed, rejected, spectrum — then
1187
- // dispatches the `event_tag` arm until `audio_ctl` stop
1188
- // (or a replacing audio_play) closes the stream. The
1189
- // arm is a six-field record built by name: `state` (an
1190
- // enum whose members are exactly loaded/position/
1191
- // completed/failed/rejected/spectrum, matched by member
1192
- // NAME so declaration order is the app's), position_ms
1193
- // and duration_ms (numbers), playing and buffering
1194
- // (booleans), and bands (bytes: the 32 spectrum band
1195
- // magnitudes, all zeros outside `.spectrum` events).
1196
- // Failure is never silent: an unplayable source is one
1197
- // `.failed` event, a refused command one `.rejected`.
1198
- // image_load a routed one-shot image load, keyed by the app's own
1199
- // numeric ImageId (`id`, an f64-carried positive integer
1200
- // — the SAME model-owned id markup binds via
1201
- // <image image="{id}"/> / <avatar image="{id}"/>):
1202
- // resolve the source cascade (local `path` first, then a
1203
- // verified cache entry at `cache_path`, then `url` —
1204
- // audio_play's resolution order; `expected_bytes` is the
1205
- // cache integrity gate, 0 = unknown), decode through the
1206
- // platform codec, register the pixels under the id, and
1207
- // dispatch exactly ONE `event_tag` arm — a five-field
1208
- // record built by name: `id` (a number, the requested
1209
- // ImageId echoed verbatim so concurrent loads sharing
1210
- // one arm stay distinguishable; an id the wire cannot
1211
- // carry exactly echoes 0), `state` (an enum whose
1212
- // members are exactly the fifteen image outcome names,
1213
- // matched by member NAME), width and height (numbers,
1214
- // the decoded dimensions on "loaded"), and status
1215
- // (number, the HTTP status for url loads that
1216
- // performed an exchange; 0 when none occurred —
1217
- // local paths, cache hits — so a cached "loaded"
1218
- // is distinguishable from a network one). One load
1219
- // per id at a time: a duplicate live id dispatches
1220
- // "rejected" (the spawn discipline — a load in flight
1221
- // is never replaced implicitly).
1222
- // image_cancel end the in-flight load under `id`, if any, LOUDLY:
1223
- // the load's one terminal arrives as its own event arm
1224
- // with state "cancelled" (ending an in-flight load is
1225
- // an observable event, spawn's cancel discipline), and
1226
- // the id is free for a fresh load once that terminal
1227
- // lands. Aimed at an id with no live load — or one the
1228
- // wire cannot carry exactly — it is a no-op, the same
1229
- // idle no-op audio_ctl keeps: whatever it aimed at is
1230
- // already gone. Image loads are keyed by their numeric
1231
- // id, so the string-keyed `cancel` record never
1232
- // touches them.
1233
- // image_unregister
1234
- // free the registry slot under `id`: the pixels are
1235
- // released, views referencing the id draw their
1236
- // fallback on the next frame, and the slot is open
1237
- // for another load. Registration's own discipline in
1238
- // reverse — synchronous registry surgery, NOT an
1239
- // effect: no Msg follows, and an id with no
1240
- // registration (or one the wire cannot carry
1241
- // exactly) is a no-op, image_cancel's idle rule. It
1242
- // frees only the CURRENT registration: a load in
1243
- // flight under the id is untouched and its terminal
1244
- // still registers — cancel the load first to keep
1245
- // the slot free.
1246
- // channel_open open an EXTERNAL-SOURCE CHANNEL, keyed by the app's
1247
- // own numeric key (`key`, an f64-carried positive
1248
- // integer below 2^53 — the image records' id
1249
- // convention): the host stages a long-lived,
1250
- // thread-safe posting seam its NATIVE side feeds
1251
- // (embedders and platform-services extensions post
1252
- // bytes from their own threads; transpiled cores are
1253
- // single-threaded and never post — they open, close,
1254
- // and receive). Every channel event then dispatches
1255
- // the `event_tag` arm — a five-field record built by
1256
- // name: `key` (a number, the channel key echoed so
1257
- // concurrent channels sharing one arm stay
1258
- // distinguishable; a key the wire cannot carry exactly
1259
- // echoes 0), `state` (an enum whose members are
1260
- // exactly data/closed/rejected, matched by member
1261
- // NAME), `bytes` (the post's payload; empty outside
1262
- // "data" events), and `droppedPending`/`droppedTotal`
1263
- // (numbers — the honest back-pressure counters: posts
1264
- // refused since the previous event, and over the
1265
- // channel's whole life). The entry is non-retiring
1266
- // the spawn way: "data" events flow across dispatches
1267
- // until the one "closed" terminal (channel_close) —
1268
- // or a "rejected" terminal for an open the host
1269
- // refused (duplicate live key, full table, occupied
1270
- // engine key) — retires it. One channel per key at a
1271
- // time, never replaced implicitly.
1272
- // channel_close close the open channel under `key`, if any:
1273
- // staged posts flush, exactly one "closed" event
1274
- // (final drop totals aboard) dispatches the entry's
1275
- // own event arm, and the key frees. A key naming no
1276
- // open channel — or one the wire cannot carry exactly
1277
- // — is a no-op, audio_ctl's idle rule. Channels are
1278
- // keyed by their numeric key, so the string-keyed
1279
- // `cancel` record never touches them — this is their
1280
- // close, the way audio_ctl stop is audio's.
1281
- // pty_spawn open a PSEUDO-TERMINAL SESSION — a spawn with a
1282
- // different transport: run `argv` (the spawn record's
1283
- // argv encoding and budgets) on a fresh pty whose
1284
- // initial grid is `cols` x `rows` and whose TERM is
1285
- // `term` (empty = the engine's default TERM — the
1286
- // fetch-timeout convention: the wire never bakes the
1287
- // default in). Non-retiring the spawn way: every
1288
- // session event dispatches the `event_tag` arm — a
1289
- // six-field record built by name: `state` (an enum
1290
- // whose members are exactly output/exit, matched by
1291
- // member NAME), `bytes` (a coalesced batch of child
1292
- // output; empty outside "output" events), `code` (a
1293
- // number, the child's exit code; -1 for every
1294
- // non-exited end), `reason` (an enum whose members
1295
- // are exactly the five spawn exit reasons —
1296
- // exited/signaled/cancelled/rejected/spawn_failed —
1297
- // matched by member NAME), `signal` (a number, the
1298
- // fatal signal after a signaled end, else 0), and
1299
- // `droppedWrites` (a number: pty_write payloads
1300
- // refused over the session's life — never silence).
1301
- // "output" events flow across dispatches until the
1302
- // exactly-one "exit" terminal retires the entry (a
1303
- // refused spawn — duplicate live key, full table,
1304
- // bad grid — is one "exit" with reason "rejected";
1305
- // a transport that could not start is one with
1306
- // "spawn_failed"). One session per key at a time,
1307
- // never replaced implicitly: kill it first, the
1308
- // spawn discipline.
1309
- // pty_write write bytes toward the session's child — keystrokes
1310
- // and pastes, fire-and-forget: a key naming no open
1311
- // session is a no-op (the exit was already on its
1312
- // way), and refused payloads count into the exit's
1313
- // `droppedWrites`, never silence.
1314
- // pty_resize push a new grid to the session so the child
1315
- // receives SIGWINCH — fire-and-forget like
1316
- // pty_write; a key naming no open session is a
1317
- // no-op.
1318
- // pty_kill terminate the session's child. LOUD, the spawn
1319
- // cancel discipline: the session's one "exit"
1320
- // terminal arrives through its own event arm with
1321
- // reason "cancelled" and the key frees once it
1322
- // lands. A key naming no open session is a no-op.
1323
- // Sessions are their own family's to end — the
1324
- // string-keyed `cancel` record never touches them,
1325
- // the way audio_ctl stop is audio's close.
1326
- // audio_ctl drive the open stream's playback in place, by verb
1327
- // (`CmdAudioVerb` declaration order): pause 0, resume
1328
- // 1, stop 2, seek 3 (`value` = position ms), volume 4
1329
- // (`value` clamped 0..1). `value` is 0 when the verb
1330
- // takes none. Control verbs are fire-and-forget — no
1331
- // result of their own; their consequences arrive on
1332
- // the keyed event stream — and a verb whose key names
1333
- // no open stream is a no-op (the playback it aimed at
1334
- // is already gone). `stop` closes the stream: no
1335
- // events for that key after it.
1336
- // video_load open (or replace — one player is the whole surface)
1337
- // the keyed video EVENT STREAM: claim the media-surface
1338
- // texture channel `surface` names (the SAME model-owned
1339
- // id a video widget binds — decoded frames flow
1340
- // platform-side into it, never through this channel),
1341
- // resolve the source cascade (local `path` first, then
1342
- // `url`; empty fields = absent) and start playback per
1343
- // `flags` (bit0 = autoplay, bit1 = loop, bit2 = muted).
1344
- // Every playback event — loaded, position ticks,
1345
- // completed, failed, rejected — then dispatches the
1346
- // `event_tag` arm until `video_ctl` stop (or a
1347
- // replacing video_load) closes the stream. The arm is
1348
- // a seven-field record built by name: `state` (an enum
1349
- // whose members are exactly loaded/position/completed/
1350
- // failed/rejected, matched by member NAME so
1351
- // declaration order is the app's), positionMs and
1352
- // durationMs (numbers), playing and buffering
1353
- // (booleans), and width and height (numbers: the
1354
- // stream's decoded pixel dimensions on "loaded", 0
1355
- // elsewhere). Failure is never silent: an unplayable
1356
- // source is one `.failed` event, a refused command one
1357
- // `.rejected`.
1358
- // video_ctl drive the open stream's playback in place, by verb
1359
- // (`CmdVideoVerb` declaration order): play 0, pause 1,
1360
- // stop 2, seek 3 (`value` = position ms), volume 4
1361
- // (`value` clamped 0..1), muted 5 and loop 6 (`value`
1362
- // 0 = off, non-zero = on). `value` is 0 when the verb
1363
- // takes none. Control verbs are fire-and-forget —
1364
- // their consequences arrive on the keyed event stream
1365
- // — and a verb whose key names no open stream is a
1366
- // no-op. `stop` closes the stream: no events for that
1367
- // key after it.
1368
- // window_show fire-and-forget window verb, LABEL-addressed (the
1369
- // declared window label, the same address the Zig
1370
- // tier's `fx.showWindow` takes): un-hide + activate
1371
- // the window — the counterpart to a
1372
- // `close_policy = "hide"` hide and the tray "Open"
1373
- // consequence; also restores a minimized window. No
1374
- // result Msg; the window's own frame event carries
1375
- // the state. An unknown label is a no-op.
1376
- // quit_app fire-and-forget graceful terminate: the host quits
1377
- // through the SAME shutdown path a last-window close
1378
- // takes (app_shutdown emits, the stop hook runs
1379
- // exactly once, a recording session seals its
1380
- // journal). No payload, no result Msg.
1381
- //
1382
- // Key discipline is ONE rule across request, the named engine ops
1383
- // (read_file/write_file/fetch/clip_read), and delay: issuing an
1384
- // effect whose key is already in flight REPLACES the old one (the
1385
- // superseded effect's result is dropped — no message), and `cancel`
1386
- // drops it silently. Empty keys are unkeyed (never cancellable, never
1387
- // colliding). The ONE exception is a live spawn key, which REJECTS
1388
- // the duplicate (err arm "rejected"): a running subprocess is never
1389
- // killed implicitly — cancel it first. audio_play also replaces on
1390
- // key reuse, per the engine underneath: one player is the whole
1391
- // surface — and video_load keeps the same rule on its own single
1392
- // player.
1393
- //
1394
- // Sub vs streams, the design line: `Sub` descriptors are DECLARATIVE
1395
- // recurring effects derived from the model after every commit (the
1396
- // host reconciles them; the app never opens or closes one). spawn,
1397
- // audio_play, and video_load are Cmd-INITIATED STREAMS: imperative
1398
- // opens with a keyed lifecycle the app drives (cancel / audio_ctl
1399
- // stop / video_ctl stop), delivering many results until their
1400
- // terminal (spawn) or close (audio, video) retires the key.
1401
- //
1402
- // Host record payload encoding (the `Cmd.host`/`Cmd.request` record form,
1403
- // produced identically by the SDK under node and by emitted Zig): fields
1404
- // sorted by TS field name (code-unit order), concatenated with no field
1405
- // headers — number -> f64 LE (8 bytes), boolean -> u8 0/1, Uint8Array ->
1406
- // [len u32 LE][bytes]. The decoding contract is the record's TS interface,
1407
- // which both ends derive from at build time.
1408
- //
1409
- // Lifetime: command bytes are frame-arena resident. The host consumes (or
1410
- // copies) them after the model commit and BEFORE frameReset — the same
1411
- // boundary the committed model crosses.
1412
-
1413
- pub const cmd_format_version: u32 = 3;
1414
-
1415
- /// An encoded command value: op records per the layout above.
1416
- pub const Cmd = []const u8;
1417
-
1418
- pub const CmdOp = enum(u8) {
1419
- persist = 0x01,
1420
- now = 0x02,
1421
- host = 0x03,
1422
- host_bytes = 0x04,
1423
- request = 0x05,
1424
- cancel = 0x06,
1425
- read_file = 0x07,
1426
- write_file = 0x08,
1427
- fetch = 0x09,
1428
- clip_write = 0x0A,
1429
- clip_read = 0x0B,
1430
- delay = 0x0C,
1431
- spawn = 0x0D,
1432
- audio_play = 0x0E,
1433
- audio_ctl = 0x0F,
1434
- window_show = 0x10,
1435
- quit_app = 0x11,
1436
- image_load = 0x12,
1437
- image_cancel = 0x13,
1438
- image_unregister = 0x14,
1439
- channel_open = 0x15,
1440
- channel_close = 0x16,
1441
- video_load = 0x17,
1442
- video_ctl = 0x18,
1443
- pty_spawn = 0x19,
1444
- pty_write = 0x1A,
1445
- pty_resize = 0x1B,
1446
- pty_kill = 0x1C,
1447
- };
1448
-
1449
- /// The spawn record's "no line routing" sentinel: a `line_tag` of
1450
- /// 0xFF means stdout lines dispatch nothing (collect spawns, and
1451
- /// line spawns that only care about the exit).
1452
- pub const spawn_no_line_tag: u8 = 0xFF;
1453
-
1454
- /// The spawn record's output mode, wire value = declaration order.
1455
- pub const CmdSpawnMode = enum(u8) {
1456
- lines = 0,
1457
- collect = 1,
1458
- };
1459
-
1460
- /// The audio_ctl verb set, wire value = declaration order.
1461
- pub const CmdAudioVerb = enum(u8) {
1462
- pause = 0,
1463
- @"resume" = 1,
1464
- stop = 2,
1465
- seek = 3,
1466
- volume = 4,
1467
- };
1468
-
1469
- /// The video_ctl verb set, wire value = declaration order.
1470
- pub const CmdVideoVerb = enum(u8) {
1471
- play = 0,
1472
- pause = 1,
1473
- stop = 2,
1474
- seek = 3,
1475
- volume = 4,
1476
- muted = 5,
1477
- loop = 6,
1478
- };
1479
-
1480
- /// The closed HTTP verb set of the fetch record, wire value =
1481
- /// declaration order.
1482
- pub const CmdFetchMethod = enum(u8) {
1483
- GET = 0,
1484
- POST = 1,
1485
- PUT = 2,
1486
- DELETE = 3,
1487
- PATCH = 4,
1488
- HEAD = 5,
1489
- };
1490
-
1491
- /// One fetch request header, encoded in TS-field-name sort order.
1492
- /// Names are compile-time string literals in the subset; values are
1493
- /// compile-time literals or runtime bytes — both ride the record's
1494
- /// length-prefixed value field, written when the command is built.
1495
- pub const CmdFetchHeader = struct {
1496
- name: []const u8,
1497
- value: []const u8,
1498
- };
1499
-
1500
- pub const cmd_none: Cmd = &.{};
1501
-
1502
- pub fn cmdPersist() Cmd {
1503
- const out = frameAlloc(u8, 1);
1504
- out[0] = @intFromEnum(CmdOp.persist);
1505
- return out;
1506
- }
1507
-
1508
- pub fn cmdNow(msg_tag: u8) Cmd {
1509
- const out = frameAlloc(u8, 2);
1510
- out[0] = @intFromEnum(CmdOp.now);
1511
- out[1] = msg_tag;
1512
- return out;
1513
- }
1514
-
1515
- pub fn cmdHost(name: []const u8, args: []const f64) Cmd {
1516
- std.debug.assert(name.len <= 255);
1517
- std.debug.assert(args.len <= 255);
1518
- const out = frameAlloc(u8, 3 + name.len + args.len * 8);
1519
- out[0] = @intFromEnum(CmdOp.host);
1520
- out[1] = @intCast(name.len);
1521
- @memcpy(out[2..][0..name.len], name);
1522
- out[2 + name.len] = @intCast(args.len);
1523
- for (args, 0..) |arg, i| {
1524
- std.mem.writeInt(u64, out[3 + name.len + i * 8 ..][0..8], @bitCast(arg), .little);
1525
- }
1526
- return out;
1527
- }
1528
-
1529
- pub fn cmdHostBytes(name: []const u8, payload: []const u8) Cmd {
1530
- std.debug.assert(name.len <= 255);
1531
- std.debug.assert(payload.len <= std.math.maxInt(u32));
1532
- const out = frameAlloc(u8, 2 + name.len + 4 + payload.len);
1533
- out[0] = @intFromEnum(CmdOp.host_bytes);
1534
- out[1] = @intCast(name.len);
1535
- @memcpy(out[2..][0..name.len], name);
1536
- std.mem.writeInt(u32, out[2 + name.len ..][0..4], @intCast(payload.len), .little);
1537
- @memcpy(out[2 + name.len + 4 ..][0..payload.len], payload);
1538
- return out;
1539
- }
1540
-
1541
- pub fn cmdRequest(name: []const u8, key: []const u8, ok_tag: u8, err_tag: u8, payload: []const u8) Cmd {
1542
- std.debug.assert(name.len <= 255);
1543
- std.debug.assert(key.len <= 255);
1544
- std.debug.assert(payload.len <= std.math.maxInt(u32));
1545
- const out = frameAlloc(u8, 2 + name.len + 1 + key.len + 2 + 4 + payload.len);
1546
- out[0] = @intFromEnum(CmdOp.request);
1547
- out[1] = @intCast(name.len);
1548
- @memcpy(out[2..][0..name.len], name);
1549
- var off: usize = 2 + name.len;
1550
- out[off] = @intCast(key.len);
1551
- @memcpy(out[off + 1 ..][0..key.len], key);
1552
- off += 1 + key.len;
1553
- out[off] = ok_tag;
1554
- out[off + 1] = err_tag;
1555
- std.mem.writeInt(u32, out[off + 2 ..][0..4], @intCast(payload.len), .little);
1556
- @memcpy(out[off + 6 ..][0..payload.len], payload);
1557
- return out;
1558
- }
1559
-
1560
- pub fn cmdCancel(key: []const u8) Cmd {
1561
- std.debug.assert(key.len <= 255);
1562
- const out = frameAlloc(u8, 2 + key.len);
1563
- out[0] = @intFromEnum(CmdOp.cancel);
1564
- out[1] = @intCast(key.len);
1565
- @memcpy(out[2..][0..key.len], key);
1566
- return out;
1567
- }
1568
-
1569
- /// The shared routed-op prefix: [op][key_len][key][ok_tag][err_tag].
1570
- fn writeRoutedHead(out: []u8, op: CmdOp, key: []const u8, ok_tag: u8, err_tag: u8) usize {
1571
- out[0] = @intFromEnum(op);
1572
- out[1] = @intCast(key.len);
1573
- @memcpy(out[2..][0..key.len], key);
1574
- out[2 + key.len] = ok_tag;
1575
- out[3 + key.len] = err_tag;
1576
- return 4 + key.len;
1577
- }
1578
-
1579
- /// A u32-LE-length-prefixed field ([len u32 LE][bytes]).
1580
- fn writeLongBytes(out: []u8, at: usize, bytes: []const u8) usize {
1581
- std.mem.writeInt(u32, out[at..][0..4], @intCast(bytes.len), .little);
1582
- @memcpy(out[at + 4 ..][0..bytes.len], bytes);
1583
- return at + 4 + bytes.len;
1584
- }
1585
-
1586
- pub fn cmdReadFile(key: []const u8, ok_tag: u8, err_tag: u8, path: []const u8) Cmd {
1587
- std.debug.assert(key.len <= 255);
1588
- std.debug.assert(path.len <= std.math.maxInt(u32));
1589
- const out = frameAlloc(u8, 4 + key.len + 4 + path.len);
1590
- var off = writeRoutedHead(out, .read_file, key, ok_tag, err_tag);
1591
- off = writeLongBytes(out, off, path);
1592
- return out;
1593
- }
1594
-
1595
- pub fn cmdWriteFile(key: []const u8, ok_tag: u8, err_tag: u8, path: []const u8, bytes: []const u8) Cmd {
1596
- std.debug.assert(key.len <= 255);
1597
- std.debug.assert(path.len <= std.math.maxInt(u32));
1598
- std.debug.assert(bytes.len <= std.math.maxInt(u32));
1599
- const out = frameAlloc(u8, 4 + key.len + 4 + path.len + 4 + bytes.len);
1600
- var off = writeRoutedHead(out, .write_file, key, ok_tag, err_tag);
1601
- off = writeLongBytes(out, off, path);
1602
- off = writeLongBytes(out, off, bytes);
1603
- return out;
1604
- }
1605
-
1606
- pub fn cmdFetch(
1607
- key: []const u8,
1608
- ok_tag: u8,
1609
- err_tag: u8,
1610
- method: CmdFetchMethod,
1611
- timeout_ms: u32,
1612
- url: []const u8,
1613
- headers: []const CmdFetchHeader,
1614
- body: []const u8,
1615
- ) Cmd {
1616
- std.debug.assert(key.len <= 255);
1617
- std.debug.assert(url.len <= std.math.maxInt(u32));
1618
- std.debug.assert(headers.len <= 255);
1619
- std.debug.assert(body.len <= std.math.maxInt(u32));
1620
- var header_bytes: usize = 0;
1621
- for (headers) |h| {
1622
- std.debug.assert(h.name.len <= 255);
1623
- header_bytes += 1 + h.name.len + 4 + h.value.len;
1624
- }
1625
- const out = frameAlloc(u8, 4 + key.len + 1 + 4 + 4 + url.len + 1 + header_bytes + 4 + body.len);
1626
- var off = writeRoutedHead(out, .fetch, key, ok_tag, err_tag);
1627
- out[off] = @intFromEnum(method);
1628
- std.mem.writeInt(u32, out[off + 1 ..][0..4], timeout_ms, .little);
1629
- off += 5;
1630
- off = writeLongBytes(out, off, url);
1631
- out[off] = @intCast(headers.len);
1632
- off += 1;
1633
- for (headers) |h| {
1634
- out[off] = @intCast(h.name.len);
1635
- @memcpy(out[off + 1 ..][0..h.name.len], h.name);
1636
- off += 1 + h.name.len;
1637
- off = writeLongBytes(out, off, h.value);
1638
- }
1639
- off = writeLongBytes(out, off, body);
1640
- return out;
1641
- }
1642
-
1643
- pub fn cmdClipWrite(bytes: []const u8) Cmd {
1644
- std.debug.assert(bytes.len <= std.math.maxInt(u32));
1645
- const out = frameAlloc(u8, 1 + 4 + bytes.len);
1646
- out[0] = @intFromEnum(CmdOp.clip_write);
1647
- _ = writeLongBytes(out, 1, bytes);
1648
- return out;
1649
- }
1650
-
1651
- pub fn cmdClipRead(key: []const u8, ok_tag: u8, err_tag: u8) Cmd {
1652
- std.debug.assert(key.len <= 255);
1653
- const out = frameAlloc(u8, 4 + key.len);
1654
- _ = writeRoutedHead(out, .clip_read, key, ok_tag, err_tag);
1655
- return out;
1656
- }
1657
-
1658
- pub fn cmdDelay(key: []const u8, after_ms: f64, msg_tag: u8) Cmd {
1659
- std.debug.assert(key.len <= 255);
1660
- const out = frameAlloc(u8, 2 + key.len + 8 + 1);
1661
- out[0] = @intFromEnum(CmdOp.delay);
1662
- out[1] = @intCast(key.len);
1663
- @memcpy(out[2..][0..key.len], key);
1664
- std.mem.writeInt(u64, out[2 + key.len ..][0..8], @bitCast(after_ms), .little);
1665
- out[2 + key.len + 8] = msg_tag;
1666
- return out;
1667
- }
1668
-
1669
- pub fn cmdSpawn(
1670
- key: []const u8,
1671
- line_tag: u8,
1672
- exit_tag: u8,
1673
- err_tag: u8,
1674
- mode: CmdSpawnMode,
1675
- argv: []const []const u8,
1676
- stdin: []const u8,
1677
- ) Cmd {
1678
- std.debug.assert(key.len <= 255);
1679
- std.debug.assert(argv.len >= 1 and argv.len <= 255);
1680
- std.debug.assert(stdin.len <= std.math.maxInt(u32));
1681
- var argv_bytes: usize = 0;
1682
- for (argv) |arg| {
1683
- std.debug.assert(arg.len <= std.math.maxInt(u32));
1684
- argv_bytes += 4 + arg.len;
1685
- }
1686
- const out = frameAlloc(u8, 2 + key.len + 3 + 1 + 1 + argv_bytes + 4 + stdin.len);
1687
- out[0] = @intFromEnum(CmdOp.spawn);
1688
- out[1] = @intCast(key.len);
1689
- @memcpy(out[2..][0..key.len], key);
1690
- var off: usize = 2 + key.len;
1691
- out[off] = line_tag;
1692
- out[off + 1] = exit_tag;
1693
- out[off + 2] = err_tag;
1694
- out[off + 3] = @intFromEnum(mode);
1695
- out[off + 4] = @intCast(argv.len);
1696
- off += 5;
1697
- for (argv) |arg| off = writeLongBytes(out, off, arg);
1698
- _ = writeLongBytes(out, off, stdin);
1699
- return out;
1700
- }
1701
-
1702
- pub fn cmdAudioPlay(
1703
- key: []const u8,
1704
- event_tag: u8,
1705
- path: []const u8,
1706
- url: []const u8,
1707
- cache_path: []const u8,
1708
- expected_bytes: f64,
1709
- ) Cmd {
1710
- std.debug.assert(key.len <= 255);
1711
- std.debug.assert(path.len <= std.math.maxInt(u32));
1712
- std.debug.assert(url.len <= std.math.maxInt(u32));
1713
- std.debug.assert(cache_path.len <= std.math.maxInt(u32));
1714
- const out = frameAlloc(u8, 2 + key.len + 1 + 4 + path.len + 4 + url.len + 4 + cache_path.len + 8);
1715
- out[0] = @intFromEnum(CmdOp.audio_play);
1716
- out[1] = @intCast(key.len);
1717
- @memcpy(out[2..][0..key.len], key);
1718
- var off: usize = 2 + key.len;
1719
- out[off] = event_tag;
1720
- off += 1;
1721
- off = writeLongBytes(out, off, path);
1722
- off = writeLongBytes(out, off, url);
1723
- off = writeLongBytes(out, off, cache_path);
1724
- std.mem.writeInt(u64, out[off..][0..8], @bitCast(expected_bytes), .little);
1725
- return out;
1726
- }
1727
-
1728
- pub fn cmdAudioCtl(key: []const u8, verb: CmdAudioVerb, value: f64) Cmd {
1729
- std.debug.assert(key.len <= 255);
1730
- const out = frameAlloc(u8, 2 + key.len + 1 + 8);
1731
- out[0] = @intFromEnum(CmdOp.audio_ctl);
1732
- out[1] = @intCast(key.len);
1733
- @memcpy(out[2..][0..key.len], key);
1734
- out[2 + key.len] = @intFromEnum(verb);
1735
- std.mem.writeInt(u64, out[2 + key.len + 1 ..][0..8], @bitCast(value), .little);
1736
- return out;
1737
- }
1738
-
1739
- pub fn cmdWindowShow(label: []const u8) Cmd {
1740
- // The emitter's byte gate on the literal label is the
1741
- // build-time teaching; this is the loud runtime backstop.
1742
- // A std.debug.assert compiles out of ReleaseFast, where the
1743
- // @intCast below would then truncate the length byte and
1744
- // corrupt the wire record silently — panic in every build
1745
- // mode instead.
1746
- if (label.len > 255) @panic("Cmd.showWindow label over 255 bytes");
1747
- const out = frameAlloc(u8, 2 + label.len);
1748
- out[0] = @intFromEnum(CmdOp.window_show);
1749
- out[1] = @intCast(label.len);
1750
- @memcpy(out[2..][0..label.len], label);
1751
- return out;
1752
- }
1753
-
1754
- pub fn cmdQuitApp() Cmd {
1755
- const out = frameAlloc(u8, 1);
1756
- out[0] = @intFromEnum(CmdOp.quit_app);
1757
- return out;
1758
- }
1759
-
1760
- pub fn cmdImageLoad(
1761
- id: f64,
1762
- event_tag: u8,
1763
- path: []const u8,
1764
- url: []const u8,
1765
- cache_path: []const u8,
1766
- expected_bytes: f64,
1767
- ) Cmd {
1768
- std.debug.assert(path.len <= std.math.maxInt(u32));
1769
- std.debug.assert(url.len <= std.math.maxInt(u32));
1770
- std.debug.assert(cache_path.len <= std.math.maxInt(u32));
1771
- const out = frameAlloc(u8, 1 + 8 + 1 + 4 + path.len + 4 + url.len + 4 + cache_path.len + 8);
1772
- out[0] = @intFromEnum(CmdOp.image_load);
1773
- std.mem.writeInt(u64, out[1..][0..8], @bitCast(id), .little);
1774
- out[9] = event_tag;
1775
- var off: usize = 10;
1776
- off = writeLongBytes(out, off, path);
1777
- off = writeLongBytes(out, off, url);
1778
- off = writeLongBytes(out, off, cache_path);
1779
- std.mem.writeInt(u64, out[off..][0..8], @bitCast(expected_bytes), .little);
1780
- return out;
1781
- }
1782
-
1783
- pub fn cmdImageCancel(id: f64) Cmd {
1784
- const out = frameAlloc(u8, 1 + 8);
1785
- out[0] = @intFromEnum(CmdOp.image_cancel);
1786
- std.mem.writeInt(u64, out[1..][0..8], @bitCast(id), .little);
1787
- return out;
1788
- }
1789
-
1790
- pub fn cmdImageUnregister(id: f64) Cmd {
1791
- const out = frameAlloc(u8, 1 + 8);
1792
- out[0] = @intFromEnum(CmdOp.image_unregister);
1793
- std.mem.writeInt(u64, out[1..][0..8], @bitCast(id), .little);
1794
- return out;
1795
- }
1796
-
1797
- pub fn cmdChannelOpen(key: f64, event_tag: u8) Cmd {
1798
- const out = frameAlloc(u8, 1 + 8 + 1);
1799
- out[0] = @intFromEnum(CmdOp.channel_open);
1800
- std.mem.writeInt(u64, out[1..][0..8], @bitCast(key), .little);
1801
- out[9] = event_tag;
1802
- return out;
1803
- }
1804
-
1805
- pub fn cmdChannelClose(key: f64) Cmd {
1806
- const out = frameAlloc(u8, 1 + 8);
1807
- out[0] = @intFromEnum(CmdOp.channel_close);
1808
- std.mem.writeInt(u64, out[1..][0..8], @bitCast(key), .little);
1809
- return out;
1810
- }
1811
-
1812
- pub fn cmdVideoLoad(
1813
- key: []const u8,
1814
- event_tag: u8,
1815
- surface: f64,
1816
- path: []const u8,
1817
- url: []const u8,
1818
- flags: u8,
1819
- ) Cmd {
1820
- // The emitter's byte gate on the literal key is the
1821
- // build-time teaching; this is the loud runtime backstop.
1822
- // A std.debug.assert compiles out of ReleaseFast, where the
1823
- // @intCast below would then truncate the length byte and
1824
- // corrupt the wire record silently — panic in every build
1825
- // mode instead.
1826
- if (key.len > 255) @panic("Cmd.videoLoad key over 255 bytes");
1827
- std.debug.assert(path.len <= std.math.maxInt(u32));
1828
- std.debug.assert(url.len <= std.math.maxInt(u32));
1829
- const out = frameAlloc(u8, 2 + key.len + 1 + 8 + 4 + path.len + 4 + url.len + 1);
1830
- out[0] = @intFromEnum(CmdOp.video_load);
1831
- out[1] = @intCast(key.len);
1832
- @memcpy(out[2..][0..key.len], key);
1833
- var off: usize = 2 + key.len;
1834
- out[off] = event_tag;
1835
- std.mem.writeInt(u64, out[off + 1 ..][0..8], @bitCast(surface), .little);
1836
- off += 9;
1837
- off = writeLongBytes(out, off, path);
1838
- off = writeLongBytes(out, off, url);
1839
- out[off] = flags;
1840
- return out;
1841
- }
1842
-
1843
- pub fn cmdVideoCtl(key: []const u8, verb: CmdVideoVerb, value: f64) Cmd {
1844
- // The same loud backstop as cmdVideoLoad: the length byte
1845
- // must never truncate silently in ReleaseFast.
1846
- if (key.len > 255) @panic("Cmd.video verb key over 255 bytes");
1847
- const out = frameAlloc(u8, 2 + key.len + 1 + 8);
1848
- out[0] = @intFromEnum(CmdOp.video_ctl);
1849
- out[1] = @intCast(key.len);
1850
- @memcpy(out[2..][0..key.len], key);
1851
- out[2 + key.len] = @intFromEnum(verb);
1852
- std.mem.writeInt(u64, out[2 + key.len + 1 ..][0..8], @bitCast(value), .little);
1853
- return out;
1854
- }
1855
-
1856
- pub fn cmdPtySpawn(
1857
- key: []const u8,
1858
- event_tag: u8,
1859
- cols: f64,
1860
- rows: f64,
1861
- term: []const u8,
1862
- argv: []const []const u8,
1863
- ) Cmd {
1864
- std.debug.assert(key.len <= 255);
1865
- std.debug.assert(term.len <= 255);
1866
- std.debug.assert(argv.len >= 1 and argv.len <= 255);
1867
- var argv_bytes: usize = 0;
1868
- for (argv) |arg| {
1869
- std.debug.assert(arg.len <= std.math.maxInt(u32));
1870
- argv_bytes += 4 + arg.len;
1871
- }
1872
- const out = frameAlloc(u8, 2 + key.len + 1 + 8 + 8 + 1 + term.len + 1 + argv_bytes);
1873
- out[0] = @intFromEnum(CmdOp.pty_spawn);
1874
- out[1] = @intCast(key.len);
1875
- @memcpy(out[2..][0..key.len], key);
1876
- var off: usize = 2 + key.len;
1877
- out[off] = event_tag;
1878
- std.mem.writeInt(u64, out[off + 1 ..][0..8], @bitCast(cols), .little);
1879
- std.mem.writeInt(u64, out[off + 9 ..][0..8], @bitCast(rows), .little);
1880
- off += 17;
1881
- out[off] = @intCast(term.len);
1882
- @memcpy(out[off + 1 ..][0..term.len], term);
1883
- off += 1 + term.len;
1884
- out[off] = @intCast(argv.len);
1885
- off += 1;
1886
- for (argv) |arg| off = writeLongBytes(out, off, arg);
1887
- return out;
1888
- }
1889
-
1890
- pub fn cmdPtyWrite(key: []const u8, bytes: []const u8) Cmd {
1891
- std.debug.assert(key.len <= 255);
1892
- std.debug.assert(bytes.len <= std.math.maxInt(u32));
1893
- const out = frameAlloc(u8, 2 + key.len + 4 + bytes.len);
1894
- out[0] = @intFromEnum(CmdOp.pty_write);
1895
- out[1] = @intCast(key.len);
1896
- @memcpy(out[2..][0..key.len], key);
1897
- _ = writeLongBytes(out, 2 + key.len, bytes);
1898
- return out;
1899
- }
1900
-
1901
- pub fn cmdPtyResize(key: []const u8, cols: f64, rows: f64) Cmd {
1902
- std.debug.assert(key.len <= 255);
1903
- const out = frameAlloc(u8, 2 + key.len + 8 + 8);
1904
- out[0] = @intFromEnum(CmdOp.pty_resize);
1905
- out[1] = @intCast(key.len);
1906
- @memcpy(out[2..][0..key.len], key);
1907
- std.mem.writeInt(u64, out[2 + key.len ..][0..8], @bitCast(cols), .little);
1908
- std.mem.writeInt(u64, out[2 + key.len + 8 ..][0..8], @bitCast(rows), .little);
1909
- return out;
1910
- }
1911
-
1912
- pub fn cmdPtyKill(key: []const u8) Cmd {
1913
- std.debug.assert(key.len <= 255);
1914
- const out = frameAlloc(u8, 2 + key.len);
1915
- out[0] = @intFromEnum(CmdOp.pty_kill);
1916
- out[1] = @intCast(key.len);
1917
- @memcpy(out[2..][0..key.len], key);
1918
- return out;
1919
- }
1920
-
1921
- pub fn cmdBatch(cmds: []const Cmd) Cmd {
1922
- var total: usize = 0;
1923
- for (cmds) |c| total += c.len;
1924
- if (total == 0) return cmd_none;
1925
- const out = frameAlloc(u8, total);
1926
- var off: usize = 0;
1927
- for (cmds) |c| {
1928
- @memcpy(out[off..][0..c.len], c);
1929
- off += c.len;
1930
- }
1931
- return out;
1932
- }
1933
-
1934
- // --------------------------------------------------- subscriptions
1935
- //
1936
- // Sub wire format — the byte encoding of the descriptor value an app's
1937
- // `subscriptions(model)` returns (versioned together with the command
1938
- // channel under `cmd_format_version`). A subscription value is a flat
1939
- // sequence of records; the empty slice means "no subscriptions", and
1940
- // batching is plain concatenation:
1941
- //
1942
- // timer [op 0x01][key_len u8][key bytes][every_ms f64 LE][msg_tag u8]
1943
- //
1944
- // Subscriptions are DECLARATIVE: after every commit the host calls
1945
- // `subscriptions` on the committed model (before frameReset — the bytes
1946
- // are frame-arena resident) and reconciles the returned set against its
1947
- // active timers by key. A key not previously active arms a timer whose
1948
- // first fire is `every_ms` from now; a key whose `every_ms` changed
1949
- // re-arms the same way; an active key absent from the set is cancelled.
1950
- // Each fire dispatches the `msg_tag` arm with the current time (ms) as
1951
- // its single number payload field, then reconciles again against the
1952
- // new committed model.
1953
-
1954
- /// An encoded subscription set: records per the layout above.
1955
- pub const Sub = []const u8;
1956
-
1957
- pub const SubOp = enum(u8) { timer = 0x01 };
1958
-
1959
- pub const sub_none: Sub = &.{};
1960
-
1961
- pub fn subTimer(key: []const u8, every_ms: f64, msg_tag: u8) Sub {
1962
- std.debug.assert(key.len <= 255);
1963
- const out = frameAlloc(u8, 2 + key.len + 8 + 1);
1964
- out[0] = @intFromEnum(SubOp.timer);
1965
- out[1] = @intCast(key.len);
1966
- @memcpy(out[2..][0..key.len], key);
1967
- std.mem.writeInt(u64, out[2 + key.len ..][0..8], @bitCast(every_ms), .little);
1968
- out[2 + key.len + 8] = msg_tag;
1969
- return out;
1970
- }
1971
-
1972
- pub fn subBatch(subs: []const Sub) Sub {
1973
- return cmdBatch(subs);
1974
- }
1975
-
1976
- pub fn resetAll() void {
1977
- frame_off = 0;
1978
- heap_cur = 0;
1979
- heap_off = 0;
1980
- stat_frame_last = 0;
1981
- stat_frame_peak = 0;
1982
- stat_commit_last = 0;
1983
- stat_compactions = 0;
1984
- }
1985
- };
1986
- }