@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
@@ -0,0 +1,366 @@
1
+ //! The web-layer inference contract: the ONE definition of when an app
2
+ //! ships the embedded web layer. An app is WEB when it declares web use
3
+ //! (a `.frontend` block, the `"webview"` capability, a `.shell` webview
4
+ //! view) or the RESOLVED web engine is Chromium; otherwise it is
5
+ //! native-only and the platform host compiles without the embedded
6
+ //! layer. `.webview_layer = "include"|"exclude"` overrides the
7
+ //! inference — and an exclude that contradicts a web declaration is a
8
+ //! refused conflict, never a silently broken app.
9
+ //!
10
+ //! Every boundary consumes this file and feeds it the inputs that
11
+ //! boundary owns:
12
+ //!
13
+ //! - The standard build graph (build/app.zig) sees `-Dweb-engine` and
14
+ //! `-Dweb-layer`: its resolved engine is the flag orelse app.zon, its
15
+ //! effective layer setting is the flag orelse app.zon, and a conflict
16
+ //! is a configure-time error.
17
+ //! - The CLI (src/tooling/manifest.zig `webLayer`/`webLayerResolved`,
18
+ //! consumed by src/tooling/package.zig) sees `--web-engine` and the
19
+ //! package verb's `--web-layer`: each flag beats its app.zon field the
20
+ //! same way the `-D` options do in the build graph, and a conflict
21
+ //! refuses the package. Both standard graph shapes (build/app.zig and
22
+ //! the generated template's build.zig) forward their RESOLVED layer
23
+ //! decision to `native package` as `--web-layer include|exclude`, so a
24
+ //! graph-driven package never re-infers what the graph already decided
25
+ //! and the packaged artifact structurally agrees with the exe.
26
+ //! - The app runner (src/app_runner/root.zig) evaluates the contract at
27
+ //! comptime over the app.zon import. It NEVER sees the `-Dweb-engine`
28
+ //! flag, so its engine input is the manifest's own engine: the
29
+ //! runner's guard fires on every manifest-visible web declaration,
30
+ //! while an engine resolved to Chromium by flag alone stays a
31
+ //! configure-time error in the build graph, which does see the flag.
32
+
33
+ const std = @import("std");
34
+ const types = @import("types.zig");
35
+
36
+ pub const WebEngine = types.WebEngine;
37
+ pub const WebViewLayer = types.WebViewLayer;
38
+
39
+ /// A web declaration: one reason an app needs the embedded web layer.
40
+ /// Scanning stops at the first hit, in this order, so teaching messages
41
+ /// stay deterministic.
42
+ pub const Declaration = enum {
43
+ frontend,
44
+ capability,
45
+ shell_webview,
46
+ chromium_engine,
47
+ /// Not a manifest field: a lenient parse (the build graph reading
48
+ /// app.zon it may not fully understand) could not read the manifest
49
+ /// at all. Absence of proof is not proof of a native-only app, so an
50
+ /// unreadable manifest counts as a web declaration — over-inclusion
51
+ /// costs binary size, a wrong exclusion breaks declared webviews.
52
+ unreadable_manifest,
53
+
54
+ /// The teaching-message half of a declaration, shared so every
55
+ /// boundary names the same cause the same way.
56
+ pub fn text(self: Declaration) []const u8 {
57
+ return switch (self) {
58
+ .frontend => "a .frontend block",
59
+ .capability => "the \"webview\" capability",
60
+ .shell_webview => "a .shell webview view",
61
+ .chromium_engine => "the Chromium web engine",
62
+ .unreadable_manifest => "an app.zon the build graph could not parse",
63
+ };
64
+ }
65
+
66
+ fn reason(self: Declaration) Reason {
67
+ return switch (self) {
68
+ .frontend => .frontend,
69
+ .capability => .capability,
70
+ .shell_webview => .shell_webview,
71
+ .chromium_engine => .chromium_engine,
72
+ .unreadable_manifest => .unreadable_manifest,
73
+ };
74
+ }
75
+ };
76
+
77
+ /// Why the web layer is (or is not) in the build, for verdict lines.
78
+ pub const Reason = enum {
79
+ inferred_native_only,
80
+ declared_exclude,
81
+ capability,
82
+ frontend,
83
+ shell_webview,
84
+ chromium_engine,
85
+ declared_include,
86
+ unreadable_manifest,
87
+ };
88
+
89
+ pub const Decision = struct {
90
+ enabled: bool,
91
+ reason: Reason,
92
+ };
93
+
94
+ pub const Error = error{WebViewLayerConflict};
95
+
96
+ /// The decision half of the contract: `include` forces the layer on,
97
+ /// `auto` infers it from the declarations, and `exclude` promises a
98
+ /// native-only app — so an exclude alongside any web declaration is a
99
+ /// contradiction this returns as an error for the boundary to report,
100
+ /// never resolved silently in either direction.
101
+ pub fn decide(setting: WebViewLayer, declaration: ?Declaration) Error!Decision {
102
+ return switch (setting) {
103
+ .include => .{ .enabled = true, .reason = .declared_include },
104
+ .exclude => if (declaration != null) error.WebViewLayerConflict else .{ .enabled = false, .reason = .declared_exclude },
105
+ .auto => if (declaration) |value| .{ .enabled = true, .reason = value.reason() } else .{ .enabled = false, .reason = .inferred_native_only },
106
+ };
107
+ }
108
+
109
+ /// The full inference over manifest-shaped data plus the resolved
110
+ /// engine and the effective layer setting (override orelse manifest).
111
+ pub fn infer(manifest: anytype, resolved_engine: WebEngine, setting: WebViewLayer) Error!Decision {
112
+ return decide(setting, webDeclaration(manifest, resolved_engine));
113
+ }
114
+
115
+ /// The first web declaration for a build: the manifest scan, then the
116
+ /// RESOLVED engine. Boundaries that resolve an engine flag pass the
117
+ /// resolved value; boundaries without one (validation, the runner) pass
118
+ /// the manifest's own engine.
119
+ pub fn webDeclaration(manifest: anytype, resolved_engine: WebEngine) ?Declaration {
120
+ return foldEngine(manifestDeclaration(manifest), resolved_engine);
121
+ }
122
+
123
+ /// Fold the resolved engine into an already-computed manifest scan, for
124
+ /// boundaries (the build graph) that scan at parse time and resolve the
125
+ /// engine later.
126
+ pub fn foldEngine(declaration: ?Declaration, resolved_engine: WebEngine) ?Declaration {
127
+ if (declaration) |value| return value;
128
+ return if (resolved_engine == .chromium) .chromium_engine else null;
129
+ }
130
+
131
+ /// The manifest half of the scan, duck-typed so every boundary feeds
132
+ /// the shape it has: the runner's comptime app.zon import (fields may
133
+ /// be absent, lists are tuples), a lenient build-graph parse and the
134
+ /// CLI's parsed metadata (string kinds, slice lists), and the typed
135
+ /// `Manifest` (Capability unions, ViewKind enums). The engine is NOT
136
+ /// part of this scan — pass the resolved engine to `webDeclaration`.
137
+ pub fn manifestDeclaration(manifest: anytype) ?Declaration {
138
+ if (declaresFrontend(manifest)) return .frontend;
139
+ if (declaresWebviewCapability(manifest)) return .capability;
140
+ if (declaresShellWebviewView(manifest)) return .shell_webview;
141
+ return null;
142
+ }
143
+
144
+ pub fn parseWebEngine(value: []const u8) ?WebEngine {
145
+ if (std.mem.eql(u8, value, "system")) return .system;
146
+ if (std.mem.eql(u8, value, "chromium")) return .chromium;
147
+ return null;
148
+ }
149
+
150
+ pub fn parseWebViewLayer(value: []const u8) ?WebViewLayer {
151
+ if (std.mem.eql(u8, value, "auto")) return .auto;
152
+ if (std.mem.eql(u8, value, "include")) return .include;
153
+ if (std.mem.eql(u8, value, "exclude")) return .exclude;
154
+ return null;
155
+ }
156
+
157
+ fn declaresFrontend(manifest: anytype) bool {
158
+ if (comptime !@hasField(@TypeOf(manifest), "frontend")) return false;
159
+ const frontend = manifest.frontend;
160
+ const T = @TypeOf(frontend);
161
+ if (comptime T == @TypeOf(null)) return false;
162
+ if (comptime @typeInfo(T) == .optional) return frontend != null;
163
+ // A ZON `.frontend = .{ ... }` block: present means declared.
164
+ return true;
165
+ }
166
+
167
+ fn declaresWebviewCapability(manifest: anytype) bool {
168
+ if (comptime !@hasField(@TypeOf(manifest), "capabilities")) return false;
169
+ return anyElement(manifest.capabilities, isWebviewName);
170
+ }
171
+
172
+ fn declaresShellWebviewView(manifest: anytype) bool {
173
+ if (comptime !@hasField(@TypeOf(manifest), "shell")) return false;
174
+ const shell = manifest.shell;
175
+ if (comptime !@hasField(@TypeOf(shell), "windows")) return false;
176
+ return anyElement(shell.windows, windowHasWebviewView);
177
+ }
178
+
179
+ fn windowHasWebviewView(window: anytype) bool {
180
+ if (comptime !@hasField(@TypeOf(window), "views")) return false;
181
+ return anyElement(window.views, viewIsWebview);
182
+ }
183
+
184
+ fn viewIsWebview(view: anytype) bool {
185
+ if (comptime !@hasField(@TypeOf(view), "kind")) return false;
186
+ return isWebviewName(view.kind);
187
+ }
188
+
189
+ /// Whether a capability or view kind names the webview: an enum tag
190
+ /// (`ViewKind`), a tagged-union tag (`Capability`), or string data (the
191
+ /// ZON import and parsed-metadata shapes).
192
+ fn isWebviewName(value: anytype) bool {
193
+ return switch (@typeInfo(@TypeOf(value))) {
194
+ .@"enum" => value == .webview,
195
+ .@"union" => std.meta.activeTag(value) == .webview,
196
+ else => std.mem.eql(u8, value, "webview"),
197
+ };
198
+ }
199
+
200
+ /// Whether any element of `list` matches. Slices and arrays iterate at
201
+ /// runtime; ZON tuples iterate inline so the whole scan stays usable in
202
+ /// the runner's comptime evaluation.
203
+ fn anyElement(list: anytype, comptime match: anytype) bool {
204
+ switch (@typeInfo(@TypeOf(list))) {
205
+ .pointer, .array => {
206
+ for (list) |element| {
207
+ if (match(element)) return true;
208
+ }
209
+ return false;
210
+ },
211
+ .@"struct" => |info| {
212
+ if (comptime info.fields.len == 0) return false;
213
+ inline for (list) |element| {
214
+ if (match(element)) return true;
215
+ }
216
+ return false;
217
+ },
218
+ else => return false,
219
+ }
220
+ }
221
+
222
+ // ---------------------------------------------------------------------------
223
+ // Contract matrix: every boundary shape, every inference row.
224
+ // ---------------------------------------------------------------------------
225
+
226
+ /// The typed-Manifest shape (what validation.zig scans).
227
+ const typed = struct {
228
+ const webview_capability = [_]types.Capability{.webview};
229
+ const canvas_capabilities = [_]types.Capability{ .native_views, .gpu_surfaces };
230
+ const webview_views = [_]types.ShellView{.{ .label = "content", .kind = .webview }};
231
+ const toolbar_views = [_]types.ShellView{.{ .label = "bar", .kind = .toolbar }};
232
+ const webview_windows = [_]types.ShellWindow{.{ .label = "main", .views = &webview_views }};
233
+ const toolbar_windows = [_]types.ShellWindow{.{ .label = "main", .views = &toolbar_views }};
234
+ };
235
+
236
+ /// The parsed-metadata shape (what the CLI's manifest tooling scans):
237
+ /// string kinds, slice lists, an optional frontend block.
238
+ const MetadataShape = struct {
239
+ const View = struct { kind: []const u8 };
240
+ const Window = struct { views: []const View = &.{} };
241
+ capabilities: []const []const u8 = &.{},
242
+ frontend: ?struct {} = null,
243
+ shell: struct { windows: []const Window = &.{} } = .{},
244
+ };
245
+
246
+ test "web-layer contract matrix agrees across boundary shapes" {
247
+ // Each row runs the scan over three shapes of the same manifest —
248
+ // the runner's comptime ZON import (tuples, absent fields), the
249
+ // typed Manifest, and the CLI's parsed metadata — and every shape
250
+ // must produce the same declaration. The build graph's lenient
251
+ // parse is the metadata shape with narrower fields, and its adapter
252
+ // (build/app.zig resolveWebLayer) is `foldEngine` + `decide`, both
253
+ // exercised below.
254
+
255
+ // Canvas-only: nothing declared, native-only under auto.
256
+ try std.testing.expectEqual(null, manifestDeclaration(.{ .capabilities = .{ "native_views", "gpu_surfaces" } }));
257
+ try std.testing.expectEqual(null, manifestDeclaration(types.Manifest{
258
+ .identity = .{ .id = "dev.example.canvas", .name = "canvas" },
259
+ .version = .{ .major = 1, .minor = 0, .patch = 0 },
260
+ .capabilities = &typed.canvas_capabilities,
261
+ }));
262
+ try std.testing.expectEqual(null, manifestDeclaration(MetadataShape{ .capabilities = &.{ "native_views", "gpu_surfaces" } }));
263
+ {
264
+ const decision = try infer(.{}, .system, .auto);
265
+ try std.testing.expect(!decision.enabled);
266
+ try std.testing.expectEqual(Reason.inferred_native_only, decision.reason);
267
+ }
268
+
269
+ // The "webview" capability.
270
+ try std.testing.expectEqual(Declaration.capability, manifestDeclaration(.{ .capabilities = .{"webview"} }));
271
+ try std.testing.expectEqual(Declaration.capability, manifestDeclaration(types.Manifest{
272
+ .identity = .{ .id = "dev.example.a", .name = "a" },
273
+ .version = .{ .major = 1, .minor = 0, .patch = 0 },
274
+ .capabilities = &typed.webview_capability,
275
+ }));
276
+ try std.testing.expectEqual(Declaration.capability, manifestDeclaration(MetadataShape{ .capabilities = &.{"webview"} }));
277
+
278
+ // A .frontend block.
279
+ try std.testing.expectEqual(Declaration.frontend, manifestDeclaration(.{ .frontend = .{ .dist = "frontend/dist" } }));
280
+ try std.testing.expectEqual(Declaration.frontend, manifestDeclaration(types.Manifest{
281
+ .identity = .{ .id = "dev.example.b", .name = "b" },
282
+ .version = .{ .major = 1, .minor = 0, .patch = 0 },
283
+ .frontend = .{},
284
+ }));
285
+ try std.testing.expectEqual(Declaration.frontend, manifestDeclaration(MetadataShape{ .frontend = .{} }));
286
+
287
+ // A .shell webview view (and a webview-free shell stays native).
288
+ try std.testing.expectEqual(Declaration.shell_webview, manifestDeclaration(.{ .shell = .{ .windows = .{.{ .views = .{.{ .kind = "webview" }} }} } }));
289
+ try std.testing.expectEqual(Declaration.shell_webview, manifestDeclaration(types.Manifest{
290
+ .identity = .{ .id = "dev.example.c", .name = "c" },
291
+ .version = .{ .major = 1, .minor = 0, .patch = 0 },
292
+ .shell = .{ .windows = &typed.webview_windows },
293
+ }));
294
+ try std.testing.expectEqual(Declaration.shell_webview, manifestDeclaration(MetadataShape{ .shell = .{ .windows = &.{.{ .views = &.{.{ .kind = "webview" }} }} } }));
295
+ try std.testing.expectEqual(null, manifestDeclaration(types.Manifest{
296
+ .identity = .{ .id = "dev.example.d", .name = "d" },
297
+ .version = .{ .major = 1, .minor = 0, .patch = 0 },
298
+ .shell = .{ .windows = &typed.toolbar_windows },
299
+ }));
300
+
301
+ // The Chromium engine is web intent wherever it was resolved: from
302
+ // app.zon (every boundary sees it) or from a flag (`-Dweb-engine` /
303
+ // `--web-engine`, seen by the build graph and the CLI; the runner
304
+ // never sees flags and covers only the manifest engine — the split
305
+ // this file's module doc pins down).
306
+ try std.testing.expectEqual(Declaration.chromium_engine, webDeclaration(.{}, .chromium));
307
+ try std.testing.expectEqual(Declaration.chromium_engine, foldEngine(null, .chromium));
308
+ {
309
+ const decision = try infer(.{}, .chromium, .auto);
310
+ try std.testing.expect(decision.enabled);
311
+ try std.testing.expectEqual(Reason.chromium_engine, decision.reason);
312
+ }
313
+
314
+ // A vestigial explicit `.web_engine = "system"` is NOT web intent.
315
+ try std.testing.expectEqual(null, webDeclaration(.{ .web_engine = "system" }, .system));
316
+ try std.testing.expectEqual(WebEngine.system, parseWebEngine("system"));
317
+
318
+ // Explicit overrides win in both directions.
319
+ {
320
+ const included = try infer(.{}, .system, .include);
321
+ try std.testing.expect(included.enabled);
322
+ try std.testing.expectEqual(Reason.declared_include, included.reason);
323
+ const excluded = try infer(.{}, .system, .exclude);
324
+ try std.testing.expect(!excluded.enabled);
325
+ try std.testing.expectEqual(Reason.declared_exclude, excluded.reason);
326
+ }
327
+
328
+ // Exclude + any web declaration is a refused conflict — including
329
+ // an engine resolved to Chromium with a web-free manifest.
330
+ try std.testing.expectError(error.WebViewLayerConflict, infer(.{ .capabilities = .{"webview"} }, .system, .exclude));
331
+ try std.testing.expectError(error.WebViewLayerConflict, infer(.{}, .chromium, .exclude));
332
+ try std.testing.expectError(error.WebViewLayerConflict, decide(.exclude, .unreadable_manifest));
333
+
334
+ // The package boundary's forwarded resolution: the build graphs hand
335
+ // `native package` their computed decision as `--web-layer
336
+ // include|exclude`, and replaying that setting through `decide` must
337
+ // reproduce the same enabled-ness for every row the graph can reach
338
+ // — forwarding the resolution is lossless, so the exe and the
339
+ // package it lands in can never disagree about the web layer.
340
+ const forward_settings = [_]WebViewLayer{ .auto, .include, .exclude };
341
+ const forward_declarations = [_]?Declaration{ null, .capability, .chromium_engine };
342
+ for (forward_settings) |setting| {
343
+ for (forward_declarations) |declaration| {
344
+ // A refused conflict never configures, so the graph has
345
+ // nothing to forward for that row.
346
+ const original = decide(setting, declaration) catch continue;
347
+ const replayed = try decide(if (original.enabled) .include else .exclude, declaration);
348
+ try std.testing.expectEqual(original.enabled, replayed.enabled);
349
+ }
350
+ }
351
+
352
+ // The runner's comptime evaluation: the same scan over a ZON-import
353
+ // shape, forced through comptime.
354
+ comptime {
355
+ if (manifestDeclaration(.{ .capabilities = .{ "native_views", "gpu_surfaces" } }) != null) @compileError("canvas manifest must scan native-only");
356
+ if (manifestDeclaration(.{ .capabilities = .{"webview"} }) != Declaration.capability) @compileError("webview capability must scan as a declaration");
357
+ if (webDeclaration(.{ .web_engine = "chromium" }, parseWebEngine("chromium").?) != Declaration.chromium_engine) @compileError("manifest chromium engine must scan as a declaration");
358
+ }
359
+ }
360
+
361
+ test "web-layer declarations carry the shared teaching text" {
362
+ try std.testing.expectEqualStrings("a .frontend block", Declaration.frontend.text());
363
+ try std.testing.expectEqualStrings("the \"webview\" capability", Declaration.capability.text());
364
+ try std.testing.expectEqualStrings("a .shell webview view", Declaration.shell_webview.text());
365
+ try std.testing.expectEqualStrings("the Chromium web engine", Declaration.chromium_engine.text());
366
+ }
@@ -191,8 +191,14 @@ pub const DisplayList = struct {
191
191
  /// half-full bound; small or oversized lists keep the linear scans.
192
192
  const diff_id_index_slots = 4096;
193
193
  const DiffIdIndex = plan_key_index.HashSlots(diff_id_index_slots);
194
- threadlocal var diff_previous_id_index: DiffIdIndex = .{};
195
- threadlocal var diff_next_id_index: DiffIdIndex = .{};
194
+ // Lazily heap-allocated per thread (32 KiB of probe tables): reset per
195
+ // diff, so first-use init on the diffing thread is the only contract —
196
+ // threads that never diff never allocate it.
197
+ const DiffIdScratch = struct {
198
+ previous: DiffIdIndex = .{},
199
+ next: DiffIdIndex = .{},
200
+ };
201
+ const diff_id_scratch = @import("lazy_tls.zig").LazyTls(DiffIdScratch);
196
202
 
197
203
  /// Fill `table` with the keyed commands' id->index mapping, erroring on
198
204
  /// the duplicate ids `validateUniqueObjectIds` rejects — one pass does
@@ -229,9 +235,10 @@ fn diffDisplayLists(previous: DisplayList, next: DisplayList, output: []DiffChan
229
235
  next.commands.len >= plan_key_index.min_entries_for_index) and
230
236
  plan_key_index.fitsHashSlots(diff_id_index_slots, previous.commands.len) and
231
237
  plan_key_index.fitsHashSlots(diff_id_index_slots, next.commands.len);
232
- if (use_index) {
233
- try buildDiffIdIndex(previous, &diff_previous_id_index);
234
- try buildDiffIdIndex(next, &diff_next_id_index);
238
+ const id_scratch: ?*DiffIdScratch = if (use_index) diff_id_scratch.get() else null;
239
+ if (id_scratch) |scratch| {
240
+ try buildDiffIdIndex(previous, &scratch.previous);
241
+ try buildDiffIdIndex(next, &scratch.next);
235
242
  } else {
236
243
  try validateUniqueObjectIds(previous);
237
244
  try validateUniqueObjectIds(next);
@@ -260,7 +267,7 @@ fn diffDisplayLists(previous: DisplayList, next: DisplayList, output: []DiffChan
260
267
 
261
268
  for (previous.commands, 0..) |previous_command, previous_index| {
262
269
  const id = previous_command.objectId() orelse continue;
263
- const next_lookup = if (use_index) findCommandByIdIndexed(next, &diff_next_id_index, id) else next.findCommandById(id);
270
+ const next_lookup = if (id_scratch) |scratch| findCommandByIdIndexed(next, &scratch.next, id) else next.findCommandById(id);
264
271
  const next_ref = next_lookup orelse {
265
272
  try appendDiffChange(output, &len, .{
266
273
  .kind = .removed,
@@ -284,7 +291,7 @@ fn diffDisplayLists(previous: DisplayList, next: DisplayList, output: []DiffChan
284
291
 
285
292
  for (next.commands, 0..) |next_command, next_index| {
286
293
  const id = next_command.objectId() orelse continue;
287
- const previous_lookup = if (use_index) findCommandByIdIndexed(previous, &diff_previous_id_index, id) else previous.findCommandById(id);
294
+ const previous_lookup = if (id_scratch) |scratch| findCommandByIdIndexed(previous, &scratch.previous, id) else previous.findCommandById(id);
288
295
  if (previous_lookup == null) {
289
296
  try appendDiffChange(output, &len, .{
290
297
  .kind = .added,
@@ -0,0 +1,73 @@
1
+ //! Lazily heap-allocated per-thread scratch.
2
+ //!
3
+ //! Large `threadlocal` arrays land in the executable's static TLS
4
+ //! template, and the OS loader materializes that whole template for
5
+ //! EVERY thread of the process — window host, COM, accessibility, and
6
+ //! worker threads all pay for the full multi-megabyte canvas planner
7
+ //! scratch even though only a runtime loop thread ever touches it
8
+ //! (measured on Windows as ~6.5 MiB of heap-backed private working set
9
+ //! per thread). `LazyTls` keeps only one pointer in static TLS: the
10
+ //! backing storage is heap-allocated the first time a thread actually
11
+ //! asks for it, so threads that never plan a frame pay eight bytes
12
+ //! instead of megabytes.
13
+ //!
14
+ //! Semantics match the `threadlocal var scratch: T = .{}` it replaces:
15
+ //! each thread gets its own instance, initialized to the struct's field
16
+ //! defaults on that thread's first access. Fields declared WITHOUT a
17
+ //! default stay uninitialized, matching the `= undefined` statics they
18
+ //! replace. The instance lives until process exit — one long-lived
19
+ //! runtime loop thread per process is the designed shape, and a static
20
+ //! TLS block was process-lifetime address space per thread too.
21
+ //!
22
+ //! Allocation failure panics: this is the render path's fixed scratch,
23
+ //! sized at compile time, and a process that cannot commit it cannot
24
+ //! render at all — the old static-TLS commit would have failed thread
25
+ //! creation under the same pressure.
26
+
27
+ const std = @import("std");
28
+
29
+ pub fn LazyTls(comptime T: type) type {
30
+ return struct {
31
+ threadlocal var instance: ?*T = null;
32
+
33
+ /// This thread's instance, allocated and default-initialized on
34
+ /// first use. The pointer is stable for the thread's lifetime,
35
+ /// so hot loops may hoist it once per operation.
36
+ pub fn get() *T {
37
+ return instance orelse create();
38
+ }
39
+
40
+ /// This thread's instance only if something already used it —
41
+ /// for stats accessors that must observe without allocating.
42
+ pub fn peek() ?*T {
43
+ return instance;
44
+ }
45
+
46
+ fn create() *T {
47
+ const ptr = std.heap.page_allocator.create(T) catch
48
+ @panic("out of memory allocating per-thread canvas scratch");
49
+ inline for (@typeInfo(T).@"struct".fields) |field| {
50
+ if (comptime field.defaultValue()) |value| @field(ptr, field.name) = value;
51
+ }
52
+ instance = ptr;
53
+ return ptr;
54
+ }
55
+ };
56
+ }
57
+
58
+ test "lazy tls initializes defaults once per access pattern" {
59
+ const Scratch = struct {
60
+ counter: u64 = 7,
61
+ buffer: [32]u8, // no default: stays uninitialized, like `= undefined`
62
+ };
63
+ const tls = LazyTls(Scratch);
64
+ try std.testing.expectEqual(@as(?*Scratch, null), tls.peek());
65
+ const first = tls.get();
66
+ try std.testing.expectEqual(@as(u64, 7), first.counter);
67
+ first.counter += 1;
68
+ first.buffer[0] = 42;
69
+ const second = tls.get();
70
+ try std.testing.expectEqual(first, second);
71
+ try std.testing.expectEqual(@as(u64, 8), second.counter);
72
+ try std.testing.expectEqual(@as(?*Scratch, first), tls.peek());
73
+ }
@@ -231,30 +231,31 @@ pub const RenderResourceCachePlanner = struct {
231
231
  previous.len >= plan_key_index.min_entries_for_index) and
232
232
  plan_key_index.fitsHashSlots(resource_cache_index_slots, previous.len) and
233
233
  plan_key_index.fitsHashSlots(resource_cache_index_slots, resource_plan.resources.len);
234
- if (use_index) {
235
- resource_cache_previous_index.reset();
234
+ const index_scratch: ?*ResourceCacheIndexScratch = if (use_index) resource_cache_index_scratch.get() else null;
235
+ if (index_scratch) |scratch| {
236
+ scratch.previous.reset();
236
237
  for (previous, 0..) |entry, index| {
237
238
  var p = ResourceCacheIndex.probe(renderResourceKeyHash(entry.key));
238
- while (resource_cache_previous_index.next(&p)) |_| {}
239
- resource_cache_previous_index.insert(p, @intCast(index));
239
+ while (scratch.previous.next(&p)) |_| {}
240
+ scratch.previous.insert(p, @intCast(index));
240
241
  }
241
- resource_cache_entry_index.reset();
242
+ scratch.entry.reset();
242
243
  }
243
244
 
244
245
  for (resource_plan.resources, 0..) |resource, resource_index| {
245
246
  const key = renderResourceKey(resource);
246
247
  const key_hash = if (use_index) renderResourceKeyHash(key) else 0;
247
- if (use_index) {
248
+ if (index_scratch) |scratch| {
248
249
  var p = ResourceCacheIndex.probe(key_hash);
249
250
  var duplicate = false;
250
- while (resource_cache_entry_index.next(&p)) |candidate| {
251
+ while (scratch.entry.next(&p)) |candidate| {
251
252
  if (renderResourceKeysEqual(self.entries[candidate].key, key)) {
252
253
  duplicate = true;
253
254
  break;
254
255
  }
255
256
  }
256
257
  if (duplicate) continue;
257
- const previous_index = findRenderResourceCacheEntryIndexed(previous, key, key_hash);
258
+ const previous_index = findRenderResourceCacheEntryIndexed(&scratch.previous, previous, key, key_hash);
258
259
  try self.appendAction(.{
259
260
  .kind = if (previous_index == null) .upload else .retain,
260
261
  .key = key,
@@ -265,7 +266,7 @@ pub const RenderResourceCachePlanner = struct {
265
266
  .key = key,
266
267
  .last_used_frame = frame_index,
267
268
  });
268
- resource_cache_entry_index.insert(p, @intCast(self.entry_len - 1));
269
+ scratch.entry.insert(p, @intCast(self.entry_len - 1));
269
270
  continue;
270
271
  }
271
272
  if (findRenderResourceCacheEntry(self.entries[0..self.entry_len], key) != null) continue;
@@ -284,10 +285,10 @@ pub const RenderResourceCachePlanner = struct {
284
285
  }
285
286
 
286
287
  for (previous, 0..) |entry, cache_index| {
287
- if (use_index) {
288
+ if (index_scratch) |scratch| {
288
289
  var p = ResourceCacheIndex.probe(renderResourceKeyHash(entry.key));
289
290
  var kept = false;
290
- while (resource_cache_entry_index.next(&p)) |candidate| {
291
+ while (scratch.entry.next(&p)) |candidate| {
291
292
  if (renderResourceKeysEqual(self.entries[candidate].key, entry.key)) {
292
293
  kept = true;
293
294
  break;
@@ -346,14 +347,20 @@ fn findRenderResourceCacheEntry(entries: []const RenderResourceCacheEntry, key:
346
347
  /// half-full bound; bigger inputs fall back to the linear scans.
347
348
  const resource_cache_index_slots = 4096;
348
349
  const ResourceCacheIndex = plan_key_index.HashSlots(resource_cache_index_slots);
349
- threadlocal var resource_cache_previous_index: ResourceCacheIndex = .{};
350
- threadlocal var resource_cache_entry_index: ResourceCacheIndex = .{};
350
+ // Lazily heap-allocated per thread (32 KiB of probe tables): reset per
351
+ // build, so first-use init on the planning thread is the only contract —
352
+ // threads that never plan resources never allocate it.
353
+ const ResourceCacheIndexScratch = struct {
354
+ previous: ResourceCacheIndex = .{},
355
+ entry: ResourceCacheIndex = .{},
356
+ };
357
+ const resource_cache_index_scratch = @import("lazy_tls.zig").LazyTls(ResourceCacheIndexScratch);
351
358
 
352
359
  /// The chain's first equal candidate is the lowest-index equal entry —
353
360
  /// the exact value the linear scan returned.
354
- fn findRenderResourceCacheEntryIndexed(previous: []const RenderResourceCacheEntry, key: RenderResourceKey, key_hash: u64) ?usize {
361
+ fn findRenderResourceCacheEntryIndexed(previous_index: *const ResourceCacheIndex, previous: []const RenderResourceCacheEntry, key: RenderResourceKey, key_hash: u64) ?usize {
355
362
  var p = ResourceCacheIndex.probe(key_hash);
356
- while (resource_cache_previous_index.next(&p)) |candidate| {
363
+ while (previous_index.next(&p)) |candidate| {
357
364
  if (renderResourceKeysEqual(previous[candidate].key, key)) return candidate;
358
365
  }
359
366
  return null;
@@ -370,6 +370,7 @@ pub const PixelSnapTokenOverrides = token_model.PixelSnapTokenOverrides;
370
370
  pub const ControlVisualTokenOverrides = token_model.ControlVisualTokenOverrides;
371
371
  pub const ControlTokenOverrides = token_model.ControlTokenOverrides;
372
372
  pub const DesignTokenOverrides = token_model.DesignTokenOverrides;
373
+ pub const accentOverrides = token_model.accentOverrides;
373
374
  pub const DesignTokens = token_model.DesignTokens;
374
375
 
375
376
  // Canvas widget model and built-in factories live in `widgets.zig`; root keeps the public API stable.
@@ -469,6 +470,11 @@ pub const markdown = @import("markdown.zig");
469
470
  // the runtime's keyed diffs (see plan_key_index.zig).
470
471
  pub const plan_key_index = @import("plan_key_index.zig");
471
472
 
473
+ // Lazily heap-allocated per-thread scratch: keeps the large planner
474
+ // buffers out of the static TLS template every OS thread must clone
475
+ // (see lazy_tls.zig for the working-set numbers).
476
+ pub const lazy_tls = @import("lazy_tls.zig");
477
+
472
478
  // Experimental markup front-end lives in `ui_markup.zig` / `ui_markup_view.zig`
473
479
  // (runtime parse + interpret: the dev/hot-reload engine) and
474
480
  // `ui_markup_compiled.zig` (comptime parse: the release engine, no parser in
@@ -27,4 +27,5 @@ test {
27
27
  _ = @import("markdown_hostile_tests.zig");
28
28
  _ = @import("layout_audit_tests.zig");
29
29
  _ = @import("a11y_audit_tests.zig");
30
+ _ = @import("lazy_tls.zig");
30
31
  }