@native-sdk/cli 0.4.3 → 0.5.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 (100) hide show
  1. package/build/app.zig +308 -72
  2. package/build/ts_run.mjs +63 -0
  3. package/build.zig +336 -7
  4. package/build.zig.zon +1 -0
  5. package/package.json +15 -10
  6. package/packages/core/package-lock.json +43 -0
  7. package/packages/core/package.json +37 -0
  8. package/packages/core/rt/rt.zig +1579 -0
  9. package/packages/core/sdk/bytes_text_methods.d.ts +88 -0
  10. package/packages/core/sdk/core.ts +694 -0
  11. package/packages/core/sdk/events.ts +137 -0
  12. package/packages/core/sdk/text.ts +519 -0
  13. package/packages/core/src/checker.ts +1869 -0
  14. package/packages/core/src/cli.ts +68 -0
  15. package/packages/core/src/devhost.ts +291 -0
  16. package/packages/core/src/devhost_resolver.mjs +22 -0
  17. package/packages/core/src/diagnostics.ts +417 -0
  18. package/packages/core/src/emitter.ts +8815 -0
  19. package/packages/core/src/infer.ts +1515 -0
  20. package/packages/core/src/modules.ts +173 -0
  21. package/packages/core/src/ownership.ts +740 -0
  22. package/packages/core/src/text_polyfill.ts +337 -0
  23. package/packages/core/src/text_tables.ts +408 -0
  24. package/packages/core/src/transpile.ts +146 -0
  25. package/packages/core/src/typed_ast.ts +536 -0
  26. package/packages/core/src/types.ts +815 -0
  27. package/skill-data/core/SKILL.md +3 -0
  28. package/skill-data/native-ui/SKILL.md +30 -0
  29. package/skill-data/ts-core/SKILL.md +493 -0
  30. package/skill-data/zig/SKILL.md +208 -0
  31. package/skills/native-sdk/SKILL.md +1 -1
  32. package/src/app_runner/root.zig +75 -9
  33. package/src/app_runner/ts_core_main.zig +231 -0
  34. package/src/platform/linux/gtk_host.c +226 -21
  35. package/src/platform/windows/webview2_host.cpp +16 -10
  36. package/src/primitives/app_manifest/comptime_scene.zig +341 -0
  37. package/src/primitives/app_manifest/root.zig +13 -0
  38. package/src/primitives/app_manifest/types.zig +14 -0
  39. package/src/primitives/app_manifest/validation.zig +18 -0
  40. package/src/primitives/app_manifest/web_layer.zig +366 -0
  41. package/src/primitives/canvas/commands.zig +14 -7
  42. package/src/primitives/canvas/lazy_tls.zig +73 -0
  43. package/src/primitives/canvas/render_generic_resources.zig +22 -15
  44. package/src/primitives/canvas/root.zig +6 -0
  45. package/src/primitives/canvas/tests.zig +1 -0
  46. package/src/primitives/canvas/text_atlas.zig +40 -32
  47. package/src/primitives/canvas/text_layout_cache.zig +29 -22
  48. package/src/primitives/canvas/text_measure_cache.zig +56 -41
  49. package/src/primitives/canvas/text_spans.zig +39 -26
  50. package/src/primitives/canvas/tokens.zig +54 -0
  51. package/src/primitives/canvas/ui.zig +125 -0
  52. package/src/primitives/canvas/ui_markup.zig +22 -3
  53. package/src/primitives/canvas/ui_markup_compiled.zig +97 -7
  54. package/src/primitives/canvas/ui_markup_compiled_tests.zig +275 -0
  55. package/src/primitives/canvas/ui_markup_contract.zig +68 -14
  56. package/src/primitives/canvas/ui_markup_contract_tests.zig +257 -2
  57. package/src/primitives/canvas/ui_markup_reflect.zig +214 -0
  58. package/src/primitives/canvas/ui_markup_view.zig +105 -1
  59. package/src/primitives/canvas/ui_markup_view_tests.zig +336 -0
  60. package/src/primitives/canvas/ui_schema.zig +8 -0
  61. package/src/primitives/canvas/ui_schema_tests.zig +5 -4
  62. package/src/primitives/canvas/widget_invalidation.zig +14 -7
  63. package/src/primitives/canvas/widget_render.zig +25 -15
  64. package/src/primitives/canvas/widget_render_controls.zig +15 -9
  65. package/src/primitives/canvas/widget_runtime_tests.zig +44 -0
  66. package/src/root.zig +7 -0
  67. package/src/runtime/api.zig +7 -0
  68. package/src/runtime/bridge_responses.zig +2 -0
  69. package/src/runtime/builtin_bridge.zig +6 -0
  70. package/src/runtime/canvas_frame.zig +57 -36
  71. package/src/runtime/canvas_images.zig +9 -3
  72. package/src/runtime/canvas_widget_control_tests.zig +5 -6
  73. package/src/runtime/canvas_widget_runtime.zig +7 -4
  74. package/src/runtime/effects.zig +490 -2
  75. package/src/runtime/effects_host_tests.zig +594 -0
  76. package/src/runtime/flow.zig +8 -0
  77. package/src/runtime/platform_bridge_tests.zig +1 -0
  78. package/src/runtime/root.zig +18 -0
  79. package/src/runtime/session_replay.zig +7 -1
  80. package/src/runtime/session_tests.zig +35 -8
  81. package/src/runtime/tests.zig +2 -0
  82. package/src/runtime/ts_core_host.zig +1505 -0
  83. package/src/runtime/ts_core_host_tests.zig +1408 -0
  84. package/src/runtime/ts_ui_app.zig +469 -0
  85. package/src/runtime/ui_app.zig +58 -0
  86. package/src/runtime/view_canvas.zig +24 -17
  87. package/src/runtime/view_widget_tree.zig +2 -2
  88. package/src/runtime/web_layer_gate_tests.zig +242 -0
  89. package/src/runtime/window_storage.zig +5 -0
  90. package/src/runtime/window_view_runtime.zig +1 -0
  91. package/src/tooling/buildgraph.zig +7 -1
  92. package/src/tooling/codesign.zig +212 -105
  93. package/src/tooling/doctor.zig +78 -0
  94. package/src/tooling/manifest.zig +314 -1
  95. package/src/tooling/package.zig +722 -18
  96. package/src/tooling/raw_manifest.zig +16 -0
  97. package/src/tooling/root.zig +1 -0
  98. package/src/tooling/templates.zig +911 -88
  99. package/src/tooling/ts_core.zig +486 -0
  100. package/src/tooling/verbs.zig +22 -0
@@ -79,17 +79,18 @@ pub const GlyphAtlasPlanner = struct {
79
79
  }
80
80
  const use_index = estimated_glyphs >= plan_key_index.min_entries_for_index and
81
81
  plan_key_index.fitsHashSlots(glyph_atlas_index_slots, self.entries.len);
82
- if (use_index) glyph_atlas_plan_index.reset();
82
+ const plan_index: ?*GlyphAtlasIndex = if (use_index) &glyph_atlas_index_scratch.get().plan else null;
83
+ if (plan_index) |index| index.reset();
83
84
  for (display_list.commands, 0..) |command, command_index| {
84
85
  switch (command) {
85
- .draw_text => |value| try self.consumeText(value, command_index, use_index),
86
+ .draw_text => |value| try self.consumeText(value, command_index, plan_index),
86
87
  else => {},
87
88
  }
88
89
  }
89
90
  return .{ .entries = self.entries[0..self.len] };
90
91
  }
91
92
 
92
- fn consumeText(self: *GlyphAtlasPlanner, text: anytype, command_index: usize, use_index: bool) Error!void {
93
+ fn consumeText(self: *GlyphAtlasPlanner, text: anytype, command_index: usize, plan_index: ?*GlyphAtlasIndex) Error!void {
93
94
  if (text.glyphs.len > 0) {
94
95
  for (text.glyphs, 0..) |glyph, glyph_index| {
95
96
  const key = GlyphAtlasKey{
@@ -99,7 +100,7 @@ pub const GlyphAtlasPlanner = struct {
99
100
  .subpixel_x = subpixelBucket(text.origin.x + glyph.x),
100
101
  .subpixel_y = subpixelBucket(text.origin.y + glyph.y),
101
102
  };
102
- try self.appendUnique(key, command_index, glyph_index, use_index);
103
+ try self.appendUnique(key, command_index, glyph_index, plan_index);
103
104
  }
104
105
  return;
105
106
  }
@@ -121,15 +122,15 @@ pub const GlyphAtlasPlanner = struct {
121
122
  .subpixel_x = subpixelBucket(text.origin.x + @as(f32, @floatFromInt(scalar_index)) * text.size * 0.5),
122
123
  .subpixel_y = subpixelBucket(text.origin.y),
123
124
  };
124
- try self.appendUnique(key, command_index, scalar_index, use_index);
125
+ try self.appendUnique(key, command_index, scalar_index, plan_index);
125
126
  }
126
127
  }
127
128
 
128
- fn appendUnique(self: *GlyphAtlasPlanner, key: GlyphAtlasKey, command_index: usize, glyph_index: usize, use_index: bool) Error!void {
129
+ fn appendUnique(self: *GlyphAtlasPlanner, key: GlyphAtlasKey, command_index: usize, glyph_index: usize, plan_index: ?*GlyphAtlasIndex) Error!void {
129
130
  var probe: GlyphAtlasIndex.Probe = undefined;
130
- if (use_index) {
131
+ if (plan_index) |index| {
131
132
  probe = GlyphAtlasIndex.probe(glyphAtlasKeyHash(key));
132
- while (glyph_atlas_plan_index.next(&probe)) |candidate| {
133
+ while (index.next(&probe)) |candidate| {
133
134
  if (glyphAtlasKeysEqual(self.entries[candidate].key, key)) return;
134
135
  }
135
136
  } else {
@@ -144,7 +145,7 @@ pub const GlyphAtlasPlanner = struct {
144
145
  .glyph_index = glyph_index,
145
146
  };
146
147
  self.len += 1;
147
- if (use_index) glyph_atlas_plan_index.insert(probe, @intCast(self.len - 1));
148
+ if (plan_index) |index| index.insert(probe, @intCast(self.len - 1));
148
149
  }
149
150
  };
150
151
 
@@ -225,22 +226,23 @@ pub const GlyphAtlasCachePlanner = struct {
225
226
  previous.len >= plan_key_index.min_entries_for_index) and
226
227
  plan_key_index.fitsHashSlots(glyph_atlas_cache_index_slots, previous.len) and
227
228
  plan_key_index.fitsHashSlots(glyph_atlas_cache_index_slots, plan.entries.len + previous.len);
228
- if (use_index) {
229
- glyph_atlas_cache_previous_index.reset();
229
+ const index_scratch: ?*GlyphAtlasIndexScratch = if (use_index) glyph_atlas_index_scratch.get() else null;
230
+ if (index_scratch) |scratch| {
231
+ scratch.cache_previous.reset();
230
232
  for (previous, 0..) |entry, index| {
231
233
  var p = GlyphAtlasCacheIndex.probe(glyphAtlasKeyHash(entry.key));
232
- while (glyph_atlas_cache_previous_index.next(&p)) |_| {}
233
- glyph_atlas_cache_previous_index.insert(p, @intCast(index));
234
+ while (scratch.cache_previous.next(&p)) |_| {}
235
+ scratch.cache_previous.insert(p, @intCast(index));
234
236
  }
235
- glyph_atlas_cache_entry_index.reset();
237
+ scratch.cache_entry.reset();
236
238
  }
237
239
 
238
240
  for (plan.entries, 0..) |entry, atlas_index| {
239
241
  const previous_index = blk: {
240
- if (use_index) {
242
+ if (index_scratch) |scratch| {
241
243
  const key_hash = glyphAtlasKeyHash(entry.key);
242
- if (self.entryIndexProbe(entry.key, key_hash)) |_| continue;
243
- break :blk findGlyphAtlasCacheEntryIndexed(previous, entry.key, key_hash);
244
+ if (self.entryIndexProbe(&scratch.cache_entry, entry.key, key_hash)) |_| continue;
245
+ break :blk findGlyphAtlasCacheEntryIndexed(&scratch.cache_previous, previous, entry.key, key_hash);
244
246
  }
245
247
  if (findGlyphAtlasCacheEntry(self.entries[0..self.entry_len], entry.key) != null) continue;
246
248
  break :blk findGlyphAtlasCacheEntry(previous, entry.key);
@@ -248,7 +250,7 @@ pub const GlyphAtlasCachePlanner = struct {
248
250
  try self.appendEntryMaybeIndexed(.{
249
251
  .key = entry.key,
250
252
  .last_used_frame = frame_index,
251
- }, use_index);
253
+ }, if (index_scratch) |scratch| &scratch.cache_entry else null);
252
254
  try self.appendAction(.{
253
255
  .kind = if (previous_index == null) .upload else .retain,
254
256
  .key = entry.key,
@@ -258,13 +260,13 @@ pub const GlyphAtlasCachePlanner = struct {
258
260
  }
259
261
 
260
262
  for (previous, 0..) |entry, previous_index| {
261
- if (use_index) {
262
- if (self.entryIndexProbe(entry.key, glyphAtlasKeyHash(entry.key))) |_| continue;
263
+ if (index_scratch) |scratch| {
264
+ if (self.entryIndexProbe(&scratch.cache_entry, entry.key, glyphAtlasKeyHash(entry.key))) |_| continue;
263
265
  } else if (findGlyphAtlasCacheEntry(self.entries[0..self.entry_len], entry.key) != null) {
264
266
  continue;
265
267
  }
266
268
  if (shouldRetainUnusedCacheEntry(frame_index, entry.last_used_frame, retention_frames) and self.hasEntryCapacity()) {
267
- try self.appendEntryMaybeIndexed(entry, use_index);
269
+ try self.appendEntryMaybeIndexed(entry, if (index_scratch) |scratch| &scratch.cache_entry else null);
268
270
  try self.appendAction(.{
269
271
  .kind = .retain,
270
272
  .key = entry.key,
@@ -288,20 +290,20 @@ pub const GlyphAtlasCachePlanner = struct {
288
290
  /// First appended entry equal to `key`, walking the entry index's
289
291
  /// probe chain — the indexed equivalent of scanning
290
292
  /// `self.entries[0..self.entry_len]`.
291
- fn entryIndexProbe(self: *GlyphAtlasCachePlanner, key: GlyphAtlasKey, key_hash: u64) ?usize {
293
+ fn entryIndexProbe(self: *GlyphAtlasCachePlanner, entry_index: *const GlyphAtlasCacheIndex, key: GlyphAtlasKey, key_hash: u64) ?usize {
292
294
  var p = GlyphAtlasCacheIndex.probe(key_hash);
293
- while (glyph_atlas_cache_entry_index.next(&p)) |candidate| {
295
+ while (entry_index.next(&p)) |candidate| {
294
296
  if (glyphAtlasKeysEqual(self.entries[candidate].key, key)) return candidate;
295
297
  }
296
298
  return null;
297
299
  }
298
300
 
299
- fn appendEntryMaybeIndexed(self: *GlyphAtlasCachePlanner, entry: GlyphAtlasCacheEntry, use_index: bool) Error!void {
301
+ fn appendEntryMaybeIndexed(self: *GlyphAtlasCachePlanner, entry: GlyphAtlasCacheEntry, entry_index: ?*GlyphAtlasCacheIndex) Error!void {
300
302
  try self.appendEntry(entry);
301
- if (use_index) {
303
+ if (entry_index) |index| {
302
304
  var p = GlyphAtlasCacheIndex.probe(glyphAtlasKeyHash(entry.key));
303
- while (glyph_atlas_cache_entry_index.next(&p)) |_| {}
304
- glyph_atlas_cache_entry_index.insert(p, @intCast(self.entry_len - 1));
305
+ while (index.next(&p)) |_| {}
306
+ index.insert(p, @intCast(self.entry_len - 1));
305
307
  }
306
308
  }
307
309
 
@@ -366,13 +368,19 @@ const glyph_atlas_index_slots = 16384;
366
368
  const glyph_atlas_cache_index_slots = 32768;
367
369
  const GlyphAtlasIndex = plan_key_index.HashSlots(glyph_atlas_index_slots);
368
370
  const GlyphAtlasCacheIndex = plan_key_index.HashSlots(glyph_atlas_cache_index_slots);
369
- threadlocal var glyph_atlas_plan_index: GlyphAtlasIndex = .{};
370
- threadlocal var glyph_atlas_cache_previous_index: GlyphAtlasCacheIndex = .{};
371
- threadlocal var glyph_atlas_cache_entry_index: GlyphAtlasCacheIndex = .{};
371
+ // Lazily heap-allocated per thread (320 KiB of probe tables): reset per
372
+ // build, so first-use init on the planning thread is the only contract —
373
+ // threads that never plan glyphs never allocate it.
374
+ const GlyphAtlasIndexScratch = struct {
375
+ plan: GlyphAtlasIndex = .{},
376
+ cache_previous: GlyphAtlasCacheIndex = .{},
377
+ cache_entry: GlyphAtlasCacheIndex = .{},
378
+ };
379
+ const glyph_atlas_index_scratch = @import("lazy_tls.zig").LazyTls(GlyphAtlasIndexScratch);
372
380
 
373
- fn findGlyphAtlasCacheEntryIndexed(previous: []const GlyphAtlasCacheEntry, key: GlyphAtlasKey, key_hash: u64) ?usize {
381
+ fn findGlyphAtlasCacheEntryIndexed(previous_index: *const GlyphAtlasCacheIndex, previous: []const GlyphAtlasCacheEntry, key: GlyphAtlasKey, key_hash: u64) ?usize {
374
382
  var p = GlyphAtlasCacheIndex.probe(key_hash);
375
- while (glyph_atlas_cache_previous_index.next(&p)) |candidate| {
383
+ while (previous_index.next(&p)) |candidate| {
376
384
  if (glyphAtlasKeysEqual(previous[candidate].key, key)) return candidate;
377
385
  }
378
386
  return null;
@@ -136,22 +136,23 @@ pub const TextLayoutCachePlanner = struct {
136
136
  previous.len >= plan_key_index.min_entries_for_index) and
137
137
  plan_key_index.fitsHashSlots(text_layout_cache_index_slots, previous.len) and
138
138
  plan_key_index.fitsHashSlots(text_layout_cache_index_slots, plans.len + previous.len);
139
- if (use_index) {
140
- text_layout_cache_previous_index.reset();
139
+ const index_scratch: ?*TextLayoutCacheIndexScratch = if (use_index) text_layout_cache_index_scratch.get() else null;
140
+ if (index_scratch) |scratch| {
141
+ scratch.previous.reset();
141
142
  for (previous, 0..) |entry, index| {
142
143
  var p = TextLayoutCacheIndex.probe(textLayoutKeyHash(entry.key));
143
- while (text_layout_cache_previous_index.next(&p)) |_| {}
144
- text_layout_cache_previous_index.insert(p, @intCast(index));
144
+ while (scratch.previous.next(&p)) |_| {}
145
+ scratch.previous.insert(p, @intCast(index));
145
146
  }
146
- text_layout_cache_entry_index.reset();
147
+ scratch.entry.reset();
147
148
  }
148
149
 
149
150
  for (plans, 0..) |plan, layout_index| {
150
151
  const previous_index = blk: {
151
- if (use_index) {
152
+ if (index_scratch) |scratch| {
152
153
  const key_hash = textLayoutKeyHash(plan.key);
153
- if (self.entryIndexProbe(plan.key, key_hash)) |_| continue;
154
- break :blk findTextLayoutCacheEntryIndexed(previous, plan.key, key_hash);
154
+ if (self.entryIndexProbe(&scratch.entry, plan.key, key_hash)) |_| continue;
155
+ break :blk findTextLayoutCacheEntryIndexed(&scratch.previous, previous, plan.key, key_hash);
155
156
  }
156
157
  if (findTextLayoutCacheEntry(self.entries[0..self.entry_len], plan.key) != null) continue;
157
158
  break :blk findTextLayoutCacheEntry(previous, plan.key);
@@ -162,7 +163,7 @@ pub const TextLayoutCachePlanner = struct {
162
163
  .line_count = plan.lineCount(),
163
164
  .bounds = plan.layout.bounds,
164
165
  .last_used_frame = frame_index,
165
- }, use_index);
166
+ }, if (index_scratch) |scratch| &scratch.entry else null);
166
167
  try self.appendAction(.{
167
168
  .kind = if (previous_index == null) .upload else .retain,
168
169
  .key = plan.key,
@@ -172,13 +173,13 @@ pub const TextLayoutCachePlanner = struct {
172
173
  }
173
174
 
174
175
  for (previous, 0..) |entry, index| {
175
- if (use_index) {
176
- if (self.entryIndexProbe(entry.key, textLayoutKeyHash(entry.key))) |_| continue;
176
+ if (index_scratch) |scratch| {
177
+ if (self.entryIndexProbe(&scratch.entry, entry.key, textLayoutKeyHash(entry.key))) |_| continue;
177
178
  } else if (findTextLayoutCacheEntry(self.entries[0..self.entry_len], entry.key) != null) {
178
179
  continue;
179
180
  }
180
181
  if (shouldRetainUnusedCacheEntry(frame_index, entry.last_used_frame, retention_frames) and self.hasEntryCapacity()) {
181
- try self.appendEntryMaybeIndexed(entry, use_index);
182
+ try self.appendEntryMaybeIndexed(entry, if (index_scratch) |scratch| &scratch.entry else null);
182
183
  try self.appendAction(.{
183
184
  .kind = .retain,
184
185
  .key = entry.key,
@@ -202,20 +203,20 @@ pub const TextLayoutCachePlanner = struct {
202
203
  /// First appended entry equal to `key`, walking the entry index's
203
204
  /// probe chain — the indexed equivalent of scanning
204
205
  /// `self.entries[0..self.entry_len]`.
205
- fn entryIndexProbe(self: *TextLayoutCachePlanner, key: TextLayoutKey, key_hash: u64) ?usize {
206
+ fn entryIndexProbe(self: *TextLayoutCachePlanner, entry_index: *const TextLayoutCacheIndex, key: TextLayoutKey, key_hash: u64) ?usize {
206
207
  var p = TextLayoutCacheIndex.probe(key_hash);
207
- while (text_layout_cache_entry_index.next(&p)) |candidate| {
208
+ while (entry_index.next(&p)) |candidate| {
208
209
  if (textLayoutKeysEqual(self.entries[candidate].key, key)) return candidate;
209
210
  }
210
211
  return null;
211
212
  }
212
213
 
213
- fn appendEntryMaybeIndexed(self: *TextLayoutCachePlanner, entry: TextLayoutCacheEntry, use_index: bool) Error!void {
214
+ fn appendEntryMaybeIndexed(self: *TextLayoutCachePlanner, entry: TextLayoutCacheEntry, entry_index: ?*TextLayoutCacheIndex) Error!void {
214
215
  try self.appendEntry(entry);
215
- if (use_index) {
216
+ if (entry_index) |index| {
216
217
  var p = TextLayoutCacheIndex.probe(textLayoutKeyHash(entry.key));
217
- while (text_layout_cache_entry_index.next(&p)) |_| {}
218
- text_layout_cache_entry_index.insert(p, @intCast(self.entry_len - 1));
218
+ while (index.next(&p)) |_| {}
219
+ index.insert(p, @intCast(self.entry_len - 1));
219
220
  }
220
221
  }
221
222
 
@@ -249,12 +250,18 @@ fn findTextLayoutCacheEntry(entries: []const TextLayoutCacheEntry, key: TextLayo
249
250
  /// bound; bigger inputs fall back to the linear scans.
250
251
  const text_layout_cache_index_slots = 8192;
251
252
  const TextLayoutCacheIndex = plan_key_index.HashSlots(text_layout_cache_index_slots);
252
- threadlocal var text_layout_cache_previous_index: TextLayoutCacheIndex = .{};
253
- threadlocal var text_layout_cache_entry_index: TextLayoutCacheIndex = .{};
253
+ // Lazily heap-allocated per thread (64 KiB of probe tables): reset per
254
+ // build, so first-use init on the planning thread is the only contract —
255
+ // threads that never plan text layouts never allocate it.
256
+ const TextLayoutCacheIndexScratch = struct {
257
+ previous: TextLayoutCacheIndex = .{},
258
+ entry: TextLayoutCacheIndex = .{},
259
+ };
260
+ const text_layout_cache_index_scratch = @import("lazy_tls.zig").LazyTls(TextLayoutCacheIndexScratch);
254
261
 
255
- fn findTextLayoutCacheEntryIndexed(previous: []const TextLayoutCacheEntry, key: TextLayoutKey, key_hash: u64) ?usize {
262
+ fn findTextLayoutCacheEntryIndexed(previous_index: *const TextLayoutCacheIndex, previous: []const TextLayoutCacheEntry, key: TextLayoutKey, key_hash: u64) ?usize {
256
263
  var p = TextLayoutCacheIndex.probe(key_hash);
257
- while (text_layout_cache_previous_index.next(&p)) |candidate| {
264
+ while (previous_index.next(&p)) |candidate| {
258
265
  if (textLayoutKeysEqual(previous[candidate].key, key)) return candidate;
259
266
  }
260
267
  return null;
@@ -123,24 +123,36 @@ const AdvanceEntry = struct {
123
123
  last_used: u64 = 0,
124
124
  };
125
125
 
126
- threadlocal var advance_entries: [advance_cache_capacity]AdvanceEntry = @splat(.{});
127
- threadlocal var advance_storage: [advance_cache_capacity][max_cached_advance_run_bytes]f32 = undefined;
128
- threadlocal var oversize_key: AdvanceKey = .{};
129
- threadlocal var oversize_storage: [max_batched_advance_run_bytes]f32 = undefined;
130
- /// Monotonic use tick driving least-recently-used eviction.
131
- threadlocal var advance_use_tick: u64 = 0;
132
- /// Fetch statistics for tests and the render benchmark: how many
133
- /// batched provider calls actually happened vs how many lookups were
134
- /// answered from retained entries.
135
- threadlocal var advance_fetch_count: u64 = 0;
136
- threadlocal var advance_hit_count: u64 = 0;
126
+ /// The whole per-thread cache behind one lazily heap-allocated pointer:
127
+ /// at ~2.25 MiB this was the single largest block in the static TLS
128
+ /// template every OS thread's loader had to clone. Init happens on the
129
+ /// first `textRunAdvances`/`cachedTextRunAdvances` call of a thread —
130
+ /// the measuring thread by construction — so threads that never measure
131
+ /// text never allocate it. The storage arrays carry no default and stay
132
+ /// uninitialized, exactly like the `= undefined` statics they replace.
133
+ const AdvanceCache = struct {
134
+ entries: [advance_cache_capacity]AdvanceEntry = @splat(.{}),
135
+ storage: [advance_cache_capacity][max_cached_advance_run_bytes]f32,
136
+ oversize_key: AdvanceKey = .{},
137
+ oversize_storage: [max_batched_advance_run_bytes]f32,
138
+ /// Monotonic use tick driving least-recently-used eviction.
139
+ use_tick: u64 = 0,
140
+ /// Fetch statistics for tests and the render benchmark: how many
141
+ /// batched provider calls actually happened vs how many lookups were
142
+ /// answered from retained entries.
143
+ fetch_count: u64 = 0,
144
+ hit_count: u64 = 0,
145
+ };
146
+ const advance_cache = @import("lazy_tls.zig").LazyTls(AdvanceCache);
137
147
 
138
148
  pub fn textAdvanceFetchCount() u64 {
139
- return advance_fetch_count;
149
+ const cache = advance_cache.peek() orelse return 0;
150
+ return cache.fetch_count;
140
151
  }
141
152
 
142
153
  pub fn textAdvanceHitCount() u64 {
143
- return advance_hit_count;
154
+ const cache = advance_cache.peek() orelse return 0;
155
+ return cache.hit_count;
144
156
  }
145
157
 
146
158
  fn advanceKeyFor(provider: *const TextMeasureProvider, font_id: FontId, size: f32, text: []const u8) AdvanceKey {
@@ -192,32 +204,33 @@ pub fn textRunAdvances(provider: *const TextMeasureProvider, font_id: FontId, si
192
204
  if (text.len == 0) return &.{};
193
205
  if (text.len > max_batched_advance_run_bytes) return null;
194
206
 
207
+ const cache = advance_cache.get();
195
208
  const key = advanceKeyFor(provider, font_id, size, text);
196
- advance_use_tick += 1;
209
+ cache.use_tick += 1;
197
210
 
198
211
  if (text.len > max_cached_advance_run_bytes) {
199
212
  // Oversize runs: one uncached scratch slot, memoized against
200
213
  // itself so repeated fetches of the same long run (the line
201
214
  // breaker, then elision, then bounds) still pay one call.
202
- if (advanceKeysEqual(oversize_key, key)) {
203
- advance_hit_count += 1;
204
- return oversize_storage[0..text.len];
215
+ if (advanceKeysEqual(cache.oversize_key, key)) {
216
+ cache.hit_count += 1;
217
+ return cache.oversize_storage[0..text.len];
205
218
  }
206
- oversize_key = .{};
207
- advance_fetch_count += 1;
208
- if (!provider.measureAdvances(font_id, size, text, oversize_storage[0..text.len])) return null;
209
- if (!advancesValid(oversize_storage[0..text.len])) return null;
210
- oversize_key = key;
211
- return oversize_storage[0..text.len];
219
+ cache.oversize_key = .{};
220
+ cache.fetch_count += 1;
221
+ if (!provider.measureAdvances(font_id, size, text, cache.oversize_storage[0..text.len])) return null;
222
+ if (!advancesValid(cache.oversize_storage[0..text.len])) return null;
223
+ cache.oversize_key = key;
224
+ return cache.oversize_storage[0..text.len];
212
225
  }
213
226
 
214
227
  var victim: usize = 0;
215
228
  var victim_tick: u64 = std.math.maxInt(u64);
216
- for (&advance_entries, 0..) |*entry, index| {
229
+ for (&cache.entries, 0..) |*entry, index| {
217
230
  if (advanceKeysEqual(entry.key, key)) {
218
- entry.last_used = advance_use_tick;
219
- advance_hit_count += 1;
220
- return advance_storage[index][0..text.len];
231
+ entry.last_used = cache.use_tick;
232
+ cache.hit_count += 1;
233
+ return cache.storage[index][0..text.len];
221
234
  }
222
235
  // Unused slots evict first (tick 0), then the least recently
223
236
  // used entry — honest bounded retention, no clock heuristics.
@@ -228,12 +241,12 @@ pub fn textRunAdvances(provider: *const TextMeasureProvider, font_id: FontId, si
228
241
  }
229
242
  }
230
243
 
231
- advance_entries[victim].key = .{};
232
- advance_fetch_count += 1;
233
- if (!provider.measureAdvances(font_id, size, text, advance_storage[victim][0..text.len])) return null;
234
- if (!advancesValid(advance_storage[victim][0..text.len])) return null;
235
- advance_entries[victim] = .{ .key = key, .last_used = advance_use_tick };
236
- return advance_storage[victim][0..text.len];
244
+ cache.entries[victim].key = .{};
245
+ cache.fetch_count += 1;
246
+ if (!provider.measureAdvances(font_id, size, text, cache.storage[victim][0..text.len])) return null;
247
+ if (!advancesValid(cache.storage[victim][0..text.len])) return null;
248
+ cache.entries[victim] = .{ .key = key, .last_used = cache.use_tick };
249
+ return cache.storage[victim][0..text.len];
237
250
  }
238
251
 
239
252
  /// Peek: the run's advances IF already retained (or sitting in the
@@ -248,20 +261,22 @@ pub fn cachedTextRunAdvances(provider: *const TextMeasureProvider, font_id: Font
248
261
  if (provider.measure_advances_fn == null) return null;
249
262
  if (text.len == 0) return &.{};
250
263
  if (text.len > max_batched_advance_run_bytes) return null;
264
+ // Peek must not allocate a cache that nothing has fetched into yet.
265
+ const cache = advance_cache.peek() orelse return null;
251
266
  const key = advanceKeyFor(provider, font_id, size, text);
252
267
  if (text.len > max_cached_advance_run_bytes) {
253
- if (advanceKeysEqual(oversize_key, key)) {
254
- advance_hit_count += 1;
255
- return oversize_storage[0..text.len];
268
+ if (advanceKeysEqual(cache.oversize_key, key)) {
269
+ cache.hit_count += 1;
270
+ return cache.oversize_storage[0..text.len];
256
271
  }
257
272
  return null;
258
273
  }
259
- for (&advance_entries, 0..) |*entry, index| {
274
+ for (&cache.entries, 0..) |*entry, index| {
260
275
  if (advanceKeysEqual(entry.key, key)) {
261
- advance_use_tick += 1;
262
- entry.last_used = advance_use_tick;
263
- advance_hit_count += 1;
264
- return advance_storage[index][0..text.len];
276
+ cache.use_tick += 1;
277
+ entry.last_used = cache.use_tick;
278
+ cache.hit_count += 1;
279
+ return cache.storage[index][0..text.len];
265
280
  }
266
281
  }
267
282
  return null;
@@ -354,12 +354,13 @@ const LayoutState = struct {
354
354
  /// to the pre-cache behavior (goldens, signatures, reference renders).
355
355
  pub fn layoutTextSpans(spans: []const TextSpan, options: TextSpanLayoutOptions, runs_storage: []TextSpanRun) TextSpanLayout {
356
356
  if (options.measure != null and runs_storage.len >= max_text_span_runs_per_paragraph) {
357
+ const cache = span_wrap_cache.get();
357
358
  const key = spanWrapKey(spans, options);
358
- if (findSpanWrapEntry(key)) |entry_index| {
359
- if (rebaseSpanWrapEntry(entry_index, spans, options, runs_storage)) |layout| return layout;
359
+ if (findSpanWrapEntry(cache, key)) |entry_index| {
360
+ if (rebaseSpanWrapEntry(cache, entry_index, spans, options, runs_storage)) |layout| return layout;
360
361
  }
361
362
  const layout = layoutTextSpansUncached(spans, options, runs_storage);
362
- storeSpanWrapEntry(key, spans, layout);
363
+ storeSpanWrapEntry(cache, key, spans, layout);
363
364
  return layout;
364
365
  }
365
366
  return layoutTextSpansUncached(spans, options, runs_storage);
@@ -502,19 +503,31 @@ const SpanWrapEntry = struct {
502
503
  last_used: u64 = 0,
503
504
  };
504
505
 
505
- threadlocal var span_wrap_entries: [span_wrap_cache_capacity]SpanWrapEntry = @splat(.{});
506
- threadlocal var span_wrap_runs: [span_wrap_cache_capacity][max_text_span_runs_per_paragraph]SpanWrapRun = undefined;
507
- threadlocal var span_wrap_use_tick: u64 = 0;
508
- threadlocal var span_wrap_hit_count: u64 = 0;
509
- threadlocal var span_wrap_miss_count: u64 = 0;
506
+ /// The whole per-thread wrap cache behind one lazily heap-allocated
507
+ /// pointer (~1 MiB that would otherwise sit in the static TLS template
508
+ /// every OS thread's loader clones). Init happens on the first cached
509
+ /// `layoutTextSpans` call of a thread — the layout thread by
510
+ /// construction — so threads that never wrap spans never allocate it.
511
+ /// `runs` carries no default and stays uninitialized, exactly like the
512
+ /// `= undefined` static it replaces.
513
+ const SpanWrapCache = struct {
514
+ entries: [span_wrap_cache_capacity]SpanWrapEntry = @splat(.{}),
515
+ runs: [span_wrap_cache_capacity][max_text_span_runs_per_paragraph]SpanWrapRun,
516
+ use_tick: u64 = 0,
517
+ hit_count: u64 = 0,
518
+ miss_count: u64 = 0,
519
+ };
520
+ const span_wrap_cache = @import("lazy_tls.zig").LazyTls(SpanWrapCache);
510
521
 
511
522
  /// Cache observability for tests and benchmarks.
512
523
  pub fn textSpanWrapCacheHitCount() u64 {
513
- return span_wrap_hit_count;
524
+ const cache = span_wrap_cache.peek() orelse return 0;
525
+ return cache.hit_count;
514
526
  }
515
527
 
516
528
  pub fn textSpanWrapCacheMissCount() u64 {
517
- return span_wrap_miss_count;
529
+ const cache = span_wrap_cache.peek() orelse return 0;
530
+ return cache.miss_count;
518
531
  }
519
532
 
520
533
  fn spanWrapKey(spans: []const TextSpan, options: TextSpanLayoutOptions) SpanWrapKey {
@@ -567,20 +580,20 @@ fn spanWrapKeysEqual(a: SpanWrapKey, b: SpanWrapKey) bool {
567
580
  a.generation == b.generation;
568
581
  }
569
582
 
570
- fn findSpanWrapEntry(key: SpanWrapKey) ?usize {
571
- for (&span_wrap_entries, 0..) |*entry, index| {
583
+ fn findSpanWrapEntry(cache: *SpanWrapCache, key: SpanWrapKey) ?usize {
584
+ for (&cache.entries, 0..) |*entry, index| {
572
585
  if (spanWrapKeysEqual(entry.key, key)) {
573
- span_wrap_use_tick += 1;
574
- entry.last_used = span_wrap_use_tick;
575
- span_wrap_hit_count += 1;
586
+ cache.use_tick += 1;
587
+ entry.last_used = cache.use_tick;
588
+ cache.hit_count += 1;
576
589
  return index;
577
590
  }
578
591
  }
579
- span_wrap_miss_count += 1;
592
+ cache.miss_count += 1;
580
593
  return null;
581
594
  }
582
595
 
583
- fn storeSpanWrapEntry(key: SpanWrapKey, spans: []const TextSpan, layout: TextSpanLayout) void {
596
+ fn storeSpanWrapEntry(cache: *SpanWrapCache, key: SpanWrapKey, spans: []const TextSpan, layout: TextSpanLayout) void {
584
597
  if (layout.runs.len > max_text_span_runs_per_paragraph) return;
585
598
  // Offsets require every run to alias its span's bytes; the breaker
586
599
  // only ever emits subslices of span.text, so a failure here would be
@@ -591,17 +604,17 @@ fn storeSpanWrapEntry(key: SpanWrapKey, spans: []const TextSpan, layout: TextSpa
591
604
 
592
605
  var victim: usize = 0;
593
606
  var victim_tick: u64 = std.math.maxInt(u64);
594
- for (&span_wrap_entries, 0..) |*entry, index| {
607
+ for (&cache.entries, 0..) |*entry, index| {
595
608
  const tick = if (entry.key.used) entry.last_used else 0;
596
609
  if (tick < victim_tick) {
597
610
  victim_tick = tick;
598
611
  victim = index;
599
612
  }
600
613
  }
601
- span_wrap_use_tick += 1;
614
+ cache.use_tick += 1;
602
615
  for (layout.runs, 0..) |run, run_index| {
603
616
  const offset = spanRunOffset(spans, run).?;
604
- span_wrap_runs[victim][run_index] = .{
617
+ cache.runs[victim][run_index] = .{
605
618
  .span_index = @intCast(run.span_index),
606
619
  .text_start = @intCast(offset),
607
620
  .text_len = @intCast(run.text.len),
@@ -611,14 +624,14 @@ fn storeSpanWrapEntry(key: SpanWrapKey, spans: []const TextSpan, layout: TextSpa
611
624
  .baseline = run.baseline,
612
625
  };
613
626
  }
614
- span_wrap_entries[victim] = .{
627
+ cache.entries[victim] = .{
615
628
  .key = key,
616
629
  .run_len = layout.runs.len,
617
630
  .line_count = layout.line_count,
618
631
  .line_height = layout.line_height,
619
632
  .size = layout.size,
620
633
  .truncated = layout.truncated,
621
- .last_used = span_wrap_use_tick,
634
+ .last_used = cache.use_tick,
622
635
  };
623
636
  }
624
637
 
@@ -639,13 +652,13 @@ fn spanRunOffset(spans: []const TextSpan, run: TextSpanRun) ?usize {
639
652
  /// offset does not fit the current spans — only reachable through a
640
653
  /// content-hash collision, and answered by re-laying-out instead of
641
654
  /// serving mismatched geometry.
642
- fn rebaseSpanWrapEntry(entry_index: usize, spans: []const TextSpan, options: TextSpanLayoutOptions, runs_storage: []TextSpanRun) ?TextSpanLayout {
643
- const entry = &span_wrap_entries[entry_index];
644
- for (span_wrap_runs[entry_index][0..entry.run_len]) |cached| {
655
+ fn rebaseSpanWrapEntry(cache: *SpanWrapCache, entry_index: usize, spans: []const TextSpan, options: TextSpanLayoutOptions, runs_storage: []TextSpanRun) ?TextSpanLayout {
656
+ const entry = &cache.entries[entry_index];
657
+ for (cache.runs[entry_index][0..entry.run_len]) |cached| {
645
658
  if (cached.span_index >= spans.len) return null;
646
659
  if (cached.text_start + cached.text_len > spans[cached.span_index].text.len) return null;
647
660
  }
648
- for (span_wrap_runs[entry_index][0..entry.run_len], 0..) |cached, run_index| {
661
+ for (cache.runs[entry_index][0..entry.run_len], 0..) |cached, run_index| {
649
662
  const span = spans[cached.span_index];
650
663
  runs_storage[run_index] = .{
651
664
  .span_index = cached.span_index,
@@ -1423,6 +1423,60 @@ pub const ControlTokenOverrides = struct {
1423
1423
  }
1424
1424
  };
1425
1425
 
1426
+ /// The ONE-accent brand statement as a token override bundle: the
1427
+ /// manifest theme surface (`app.zon`'s `.theme_accent`) resolves through
1428
+ /// this, and Zig apps can layer the same bundle over any pack. Three
1429
+ /// slots carry an accent identity, plus the slider's own table:
1430
+ ///
1431
+ /// - `colors.accent`/`accent_text`: the filled-primary pair — selected
1432
+ /// fills, "Playing"-style primary badges, filled primary buttons, and
1433
+ /// every control fill deriving from the accent channel. The knockout
1434
+ /// ink derives from the accent's relative luminance (white over dark
1435
+ /// accents, near-black over light ones) so the pair stays readable in
1436
+ /// both schemes without a second manifest knob.
1437
+ /// - `colors.focus_ring`: packs spend their identity hue on focus; with
1438
+ /// the identity moved, the ring follows so focus and accent chrome
1439
+ /// read as one system.
1440
+ /// - `controls.slider.active_background`: the slider table states its
1441
+ /// own hue for the filled range rather than deriving from the accent
1442
+ /// channel, so the same move is stated once more or a seek scrubber
1443
+ /// would keep the pack's stock hue.
1444
+ ///
1445
+ /// High-contrast composition is the CALLER's rule (accessibility beats
1446
+ /// brand): the runtime skips this bundle when the system asks for high
1447
+ /// contrast, taking the pack's own loud register untouched.
1448
+ pub fn accentOverrides(accent: Color) DesignTokenOverrides {
1449
+ return .{
1450
+ .colors = .{
1451
+ .accent = accent,
1452
+ .accent_text = accentKnockoutInk(accent),
1453
+ .focus_ring = accent,
1454
+ },
1455
+ .controls = .{
1456
+ .slider = .{ .active_background = accent },
1457
+ },
1458
+ };
1459
+ }
1460
+
1461
+ /// The readable knockout ink over one accent fill: WHITE whenever it
1462
+ /// clears the 4.5:1 text bar over the accent (the ink packs pair with
1463
+ /// every solid identity fill — the convention brands expect), and the
1464
+ /// near-black text ink only when the accent is light enough that white
1465
+ /// honestly cannot (1.05 / (L + 0.05) < 4.5).
1466
+ fn accentKnockoutInk(accent: Color) Color {
1467
+ const luminance = 0.2126 * srgbToLinear(accent.r) +
1468
+ 0.7152 * srgbToLinear(accent.g) +
1469
+ 0.0722 * srgbToLinear(accent.b);
1470
+ // White clears 4.5:1 up to L = 1.05/4.5 - 0.05 ≈ 0.18333.
1471
+ if (luminance <= 0.18333) return Color.rgb8(255, 255, 255);
1472
+ return Color.rgb8(10, 10, 10);
1473
+ }
1474
+
1475
+ fn srgbToLinear(channel: f32) f32 {
1476
+ if (channel <= 0.04045) return channel / 12.92;
1477
+ return std.math.pow(f32, (channel + 0.055) / 1.055, 2.4);
1478
+ }
1479
+
1426
1480
  pub const DesignTokenOverrides = struct {
1427
1481
  colors: ColorTokenOverrides = .{},
1428
1482
  typography: TypographyTokenOverrides = .{},