@native-sdk/cli 0.5.3 → 0.5.4

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 (79) hide show
  1. package/build/app.zig +41 -1
  2. package/build.zig +223 -3
  3. package/native-sdk.d.ts +17 -1
  4. package/package.json +9 -9
  5. package/packages/core/package-lock.json +2 -2
  6. package/packages/core/package.json +1 -1
  7. package/packages/core/rt/rt.zig +138 -3
  8. package/packages/core/sdk/core.ts +225 -3
  9. package/packages/core/src/emitter.ts +2586 -259
  10. package/skill-data/native-ui/SKILL.md +3 -2
  11. package/skill-data/ts-core/SKILL.md +9 -1
  12. package/src/app_runner/root.zig +81 -1
  13. package/src/app_runner/ts_core_main.zig +4 -0
  14. package/src/embed/tests.zig +119 -1
  15. package/src/platform/linux/gtk_host.c +112 -3
  16. package/src/platform/linux/root.zig +71 -0
  17. package/src/platform/macos/appkit_host.h +48 -2
  18. package/src/platform/macos/appkit_host.m +533 -34
  19. package/src/platform/macos/cef_host.mm +262 -10
  20. package/src/platform/macos/root.zig +92 -4
  21. package/src/platform/null_platform.zig +298 -5
  22. package/src/platform/root.zig +33 -0
  23. package/src/platform/types.zig +138 -4
  24. package/src/platform/windows/root.zig +134 -0
  25. package/src/platform/windows/webview2_host.cpp +152 -40
  26. package/src/primitives/app_manifest/comptime_scene.zig +5 -0
  27. package/src/primitives/app_manifest/root.zig +1 -0
  28. package/src/primitives/app_manifest/types.zig +32 -0
  29. package/src/primitives/canvas/tests.zig +1 -0
  30. package/src/primitives/canvas/ui.zig +11 -7
  31. package/src/primitives/canvas/ui_markup.zig +58 -34
  32. package/src/primitives/canvas/ui_markup_binary.zig +4 -1
  33. package/src/primitives/canvas/ui_markup_compiled.zig +23 -8
  34. package/src/primitives/canvas/ui_markup_compiled_tests.zig +55 -0
  35. package/src/primitives/canvas/ui_markup_contract.zig +1 -1
  36. package/src/primitives/canvas/ui_markup_contract_tests.zig +26 -0
  37. package/src/primitives/canvas/ui_markup_huge_document_tests.zig +154 -0
  38. package/src/primitives/canvas/ui_markup_tests.zig +29 -9
  39. package/src/primitives/canvas/ui_markup_view.zig +29 -11
  40. package/src/primitives/canvas/ui_markup_view_tests.zig +150 -9
  41. package/src/primitives/canvas/ui_schema.zig +13 -0
  42. package/src/primitives/canvas/ui_schema_tests.zig +5 -4
  43. package/src/root.zig +7 -0
  44. package/src/runtime/api.zig +6 -4
  45. package/src/runtime/bridge_payload.zig +7 -0
  46. package/src/runtime/bridge_responses.zig +5 -0
  47. package/src/runtime/builtin_bridge.zig +9 -4
  48. package/src/runtime/canvas_font_tests.zig +439 -2
  49. package/src/runtime/canvas_fonts.zig +61 -6
  50. package/src/runtime/canvas_image_tests.zig +195 -0
  51. package/src/runtime/canvas_images.zig +45 -12
  52. package/src/runtime/core.zig +58 -11
  53. package/src/runtime/effects.zig +1664 -43
  54. package/src/runtime/effects_fetch_tests.zig +89 -1
  55. package/src/runtime/effects_file_tests.zig +41 -1
  56. package/src/runtime/effects_image_tests.zig +1304 -0
  57. package/src/runtime/effects_tests.zig +153 -0
  58. package/src/runtime/flow.zig +17 -0
  59. package/src/runtime/root.zig +11 -0
  60. package/src/runtime/session_blobs.zig +689 -0
  61. package/src/runtime/session_journal.zig +85 -21
  62. package/src/runtime/session_record.zig +27 -2
  63. package/src/runtime/session_replay.zig +168 -16
  64. package/src/runtime/session_tests.zig +1992 -0
  65. package/src/runtime/shell_layout.zig +7 -0
  66. package/src/runtime/testdata/fonts/NotoSansSC-Receipt.ttf +0 -0
  67. package/src/runtime/testdata/fonts/OFL.txt +93 -0
  68. package/src/runtime/tests.zig +2 -0
  69. package/src/runtime/ts_core_host.zig +439 -4
  70. package/src/runtime/ts_core_host_tests.zig +41 -0
  71. package/src/runtime/ts_ui_app.zig +7 -0
  72. package/src/runtime/ui_app.zig +111 -6
  73. package/src/runtime/ui_app_window_tests.zig +468 -0
  74. package/src/runtime/window_command_bridge_tests.zig +117 -0
  75. package/src/runtime/window_storage.zig +32 -0
  76. package/src/runtime/window_views.zig +61 -1
  77. package/src/tooling/manifest.zig +75 -0
  78. package/src/tooling/raw_manifest.zig +2 -0
  79. package/src/tooling/templates.zig +219 -1
@@ -233,6 +233,201 @@ test "registerCanvasImageBytes refuses reserved media-surface ids before decodin
233
233
  try std.testing.expectEqual(@as(usize, 1), harness.null_platform.image_decode_count);
234
234
  }
235
235
 
236
+ // ------------------------------------------------------ lazy slot buffers
237
+
238
+ /// A started GPU harness whose runtime froze `runtime_allocator` at
239
+ /// init. The runtime's `owned_allocator` captures `Options.allocator`
240
+ /// in `initAt` and mutating `options.allocator` on a live runtime
241
+ /// deliberately retargets nothing, so a test allocator must be injected
242
+ /// through the real capture site: re-initialize the runtime in place
243
+ /// with the harness's own platform and trace wiring (nothing is
244
+ /// heap-owned yet, so the re-init leaks nothing).
245
+ fn startedGpuHarnessWithRuntimeAllocator(gpa: std.mem.Allocator, runtime_allocator: std.mem.Allocator) !*TestHarness() {
246
+ const harness = try startedGpuHarness(gpa);
247
+ errdefer harness.destroy(gpa);
248
+ core.Runtime.initAt(&harness.runtime, .{
249
+ .platform = harness.null_platform.platform(),
250
+ .trace_sink = harness.trace_sink.sink(),
251
+ .allocator = runtime_allocator,
252
+ .environ = std.testing.environ,
253
+ });
254
+ // Match TestHarness().init: tests fail loud on handler/update errors.
255
+ harness.runtime.dispatch_error_policy = .propagate;
256
+ return harness;
257
+ }
258
+
259
+ /// The registered pixels for `id`, looked up through the same
260
+ /// `ReferenceImage` set the renderers consume — compaction correctness
261
+ /// is judged where the pixels are actually read.
262
+ fn registeredPixelsById(runtime: *core.Runtime, id: canvas.ImageId) ?[]const u8 {
263
+ for (runtime.registeredCanvasImages()) |resource| {
264
+ if (resource.id == id) return resource.pixels;
265
+ }
266
+ return null;
267
+ }
268
+
269
+ test "a fresh runtime allocates zero registered-image bytes until the first registration" {
270
+ // Count every runtime-allocator call: construction, startup, and
271
+ // view creation perform NONE — slot buffer storage is on-demand at
272
+ // a slot's first registration. (The regression pinned here: an
273
+ // embedded pool put 16 x 1 MiB = 16 MiB in every Runtime before
274
+ // any image existed; the media-texture-pool regression's twin.)
275
+ var counting = std.testing.FailingAllocator.init(std.testing.allocator, .{});
276
+ const harness = try startedGpuHarnessWithRuntimeAllocator(std.testing.allocator, counting.allocator());
277
+ defer harness.destroy(std.testing.allocator);
278
+ var app_state: RegistryApp = .{};
279
+ try harness.start(app_state.app());
280
+ _ = try harness.runtime.createView(.{
281
+ .window_id = 1,
282
+ .label = "canvas",
283
+ .kind = .gpu_surface,
284
+ .frame = geometry.RectF.init(0, 0, 240, 140),
285
+ });
286
+ try std.testing.expectEqual(@as(usize, 0), counting.allocations);
287
+
288
+ // The first registration is the first allocation: exactly one,
289
+ // exactly the slot budget (freed by Runtime.deinit through
290
+ // harness.destroy — the leak-checked test allocator backs it).
291
+ const red = [_]u8{ 255, 0, 0, 255 };
292
+ try harness.runtime.registerCanvasImage(1, 1, 1, &red);
293
+ try std.testing.expectEqual(@as(usize, 1), counting.allocations);
294
+ try std.testing.expectEqual(canvas_limits.max_registered_canvas_image_pixel_bytes, counting.allocated_bytes);
295
+
296
+ // Replacing an id's pixels reuses the slot buffer: the count stays
297
+ // one — the allocation is per used slot, not per registration.
298
+ const blue = [_]u8{ 0, 0, 255, 255 };
299
+ try harness.runtime.registerCanvasImage(1, 1, 1, &blue);
300
+ try std.testing.expectEqual(@as(usize, 1), counting.allocations);
301
+
302
+ // A second id claims a second slot: one more allocation.
303
+ try harness.runtime.registerCanvasImage(2, 1, 1, &red);
304
+ try std.testing.expectEqual(@as(usize, 2), counting.allocations);
305
+
306
+ // Unregister keeps vacated buffers for reuse (deinit is the only
307
+ // free), so churn never touches the allocator again: the footprint
308
+ // is bounded by the high-water slot count.
309
+ try std.testing.expect(harness.runtime.unregisterCanvasImage(1));
310
+ try std.testing.expect(harness.runtime.unregisterCanvasImage(2));
311
+ try harness.runtime.registerCanvasImage(3, 1, 1, &blue);
312
+ try harness.runtime.registerCanvasImage(4, 1, 1, &red);
313
+ try std.testing.expectEqual(@as(usize, 2), counting.allocations);
314
+ }
315
+
316
+ test "image slot buffer ownership freezes at init: a mutated options.allocator sees zero activity" {
317
+ // The hazard pinned here: `Runtime.options` is public and mutable,
318
+ // so if the lazy slot allocation and deinit's free both read
319
+ // `options.allocator` LIVE, swapping it between registration and
320
+ // deinit frees through the wrong allocator — silent UB. Ownership
321
+ // must freeze into `owned_allocator` at init instead.
322
+ var frozen = std.testing.FailingAllocator.init(std.testing.allocator, .{});
323
+ const harness = try startedGpuHarnessWithRuntimeAllocator(std.testing.allocator, frozen.allocator());
324
+ defer harness.destroy(std.testing.allocator);
325
+ var app_state: RegistryApp = .{};
326
+ try harness.start(app_state.app());
327
+ const red = [_]u8{ 255, 0, 0, 255 };
328
+ try harness.runtime.registerCanvasImage(7, 1, 1, &red);
329
+ try std.testing.expectEqual(@as(usize, 1), frozen.allocations);
330
+
331
+ // Sabotage: swap options.allocator AFTER the registration
332
+ // allocated. fail_index = 0 poisons the swapped-in allocator (any
333
+ // allocation through it refuses), and its counters pin that deinit
334
+ // routes NOTHING here — neither an alloc nor, critically, the free.
335
+ var poisoned = std.testing.FailingAllocator.init(std.testing.allocator, .{ .fail_index = 0 });
336
+ harness.runtime.options.allocator = poisoned.allocator();
337
+
338
+ harness.runtime.deinit();
339
+ try std.testing.expectEqual(@as(usize, 1), frozen.deallocations);
340
+ try std.testing.expectEqual(canvas_limits.max_registered_canvas_image_pixel_bytes, frozen.freed_bytes);
341
+ try std.testing.expectEqual(@as(usize, 0), poisoned.allocations);
342
+ try std.testing.expectEqual(@as(usize, 0), poisoned.deallocations);
343
+ try std.testing.expectEqual(@as(usize, 0), poisoned.freed_bytes);
344
+ // harness.destroy's second deinit finds the buffers already
345
+ // returned (deinit resets them to empty) — no double free through
346
+ // either allocator.
347
+ }
348
+
349
+ test "unregister compacts by pointer swap: surviving pixels stay intact and vacated buffers are reused" {
350
+ var counting = std.testing.FailingAllocator.init(std.testing.allocator, .{});
351
+ const harness = try startedGpuHarnessWithRuntimeAllocator(std.testing.allocator, counting.allocator());
352
+ defer harness.destroy(std.testing.allocator);
353
+ var app_state: RegistryApp = .{};
354
+ try harness.start(app_state.app());
355
+
356
+ const colors = [_][4]u8{
357
+ .{ 255, 0, 0, 255 },
358
+ .{ 0, 255, 0, 255 },
359
+ .{ 0, 0, 255, 255 },
360
+ .{ 255, 255, 0, 255 },
361
+ };
362
+ for (colors, 1..) |color, id| {
363
+ try harness.runtime.registerCanvasImage(id, 1, 1, &color);
364
+ }
365
+ try std.testing.expectEqual(@as(usize, 4), counting.allocations);
366
+
367
+ // Removing a middle id compacts the last entry into its slot; every
368
+ // survivor's pixels must read back exactly through the renderer set.
369
+ try std.testing.expect(harness.runtime.unregisterCanvasImage(2));
370
+ try std.testing.expectEqual(@as(usize, 3), harness.runtime.registeredCanvasImageCount());
371
+ try std.testing.expect(registeredPixelsById(&harness.runtime, 2) == null);
372
+ try std.testing.expectEqualSlices(u8, &colors[0], registeredPixelsById(&harness.runtime, 1).?);
373
+ try std.testing.expectEqualSlices(u8, &colors[2], registeredPixelsById(&harness.runtime, 3).?);
374
+ try std.testing.expectEqualSlices(u8, &colors[3], registeredPixelsById(&harness.runtime, 4).?);
375
+
376
+ // A second compaction swaps again: the moved entry from the first
377
+ // pass moves once more and its pixels still hold.
378
+ try std.testing.expect(harness.runtime.unregisterCanvasImage(1));
379
+ try std.testing.expectEqual(@as(usize, 2), harness.runtime.registeredCanvasImageCount());
380
+ try std.testing.expectEqualSlices(u8, &colors[2], registeredPixelsById(&harness.runtime, 3).?);
381
+ try std.testing.expectEqualSlices(u8, &colors[3], registeredPixelsById(&harness.runtime, 4).?);
382
+
383
+ // Re-registering into the vacated slots reuses their parked buffers:
384
+ // the allocation count never moves past the high-water mark, and the
385
+ // fresh pixels land next to the untouched survivors.
386
+ const teal = [_]u8{ 0, 128, 128, 255 };
387
+ const gray = [_]u8{ 90, 90, 90, 255 };
388
+ try harness.runtime.registerCanvasImage(5, 1, 1, &teal);
389
+ try harness.runtime.registerCanvasImage(6, 1, 1, &gray);
390
+ try std.testing.expectEqual(@as(usize, 4), counting.allocations);
391
+ try std.testing.expectEqual(@as(usize, 4), harness.runtime.registeredCanvasImageCount());
392
+ try std.testing.expectEqualSlices(u8, &teal, registeredPixelsById(&harness.runtime, 5).?);
393
+ try std.testing.expectEqualSlices(u8, &gray, registeredPixelsById(&harness.runtime, 6).?);
394
+ try std.testing.expectEqualSlices(u8, &colors[2], registeredPixelsById(&harness.runtime, 3).?);
395
+ try std.testing.expectEqualSlices(u8, &colors[3], registeredPixelsById(&harness.runtime, 4).?);
396
+ }
397
+
398
+ test "a failed slot-buffer allocation refuses cleanly and a retry succeeds after memory heals" {
399
+ var counting = std.testing.FailingAllocator.init(std.testing.allocator, .{});
400
+ const harness = try startedGpuHarnessWithRuntimeAllocator(std.testing.allocator, counting.allocator());
401
+ defer harness.destroy(std.testing.allocator);
402
+ var app_state: RegistryApp = .{};
403
+ try harness.start(app_state.app());
404
+
405
+ const red = [_]u8{ 255, 0, 0, 255 };
406
+ const blue = [_]u8{ 0, 0, 255, 255 };
407
+ try harness.runtime.registerCanvasImage(1, 1, 1, &red);
408
+
409
+ // The next slot's lazy allocation fails: the registration refuses
410
+ // with the honest allocator error and the registry is EXACTLY as it
411
+ // was — the earlier id intact, the failed id absent, no committed
412
+ // slot without pixels.
413
+ counting.fail_index = counting.alloc_index;
414
+ try std.testing.expectError(error.OutOfMemory, harness.runtime.registerCanvasImage(2, 1, 1, &blue));
415
+ try std.testing.expectEqual(@as(usize, 1), harness.runtime.registeredCanvasImageCount());
416
+ try std.testing.expect(harness.runtime.registeredCanvasImage(2) == null);
417
+ try std.testing.expectEqualSlices(u8, &red, registeredPixelsById(&harness.runtime, 1).?);
418
+
419
+ // Replacing an ALREADY-registered id needs no allocation, so it
420
+ // still succeeds while memory is exhausted.
421
+ try harness.runtime.registerCanvasImage(1, 1, 1, &blue);
422
+ try std.testing.expectEqualSlices(u8, &blue, registeredPixelsById(&harness.runtime, 1).?);
423
+
424
+ // Memory recovers: the same registration retries and lands.
425
+ counting.fail_index = std.math.maxInt(usize);
426
+ try harness.runtime.registerCanvasImage(2, 1, 1, &blue);
427
+ try std.testing.expectEqual(@as(usize, 2), harness.runtime.registeredCanvasImageCount());
428
+ try std.testing.expectEqualSlices(u8, &blue, registeredPixelsById(&harness.runtime, 2).?);
429
+ }
430
+
236
431
  // ---------------------------------------------------------------- avatar app
237
432
 
238
433
  const avatar_canvas_label = "avatar-canvas";
@@ -5,8 +5,12 @@
5
5
  //!
6
6
  //! The registry is the missing bridge between fetched/decoded bytes and
7
7
  //! the canvas image pipeline, which was already id+fingerprint based end
8
- //! to end: `registerCanvasImage` copies pixels into a bounded
9
- //! runtime-owned pool, and the frame planner threads the registered set
8
+ //! to end: `registerCanvasImage` copies pixels into a bounded set of
9
+ //! runtime-owned slot buffers (each one lazy slot-budget allocation
10
+ //! from the runtime's init-frozen `owned_allocator` at the slot's first
11
+ //! use, freed only by `Runtime.deinit` — a runtime that never registers
12
+ //! an image allocates nothing), and the frame planner threads the
13
+ //! registered set
10
14
  //! into `CanvasFrameOptions.image_resources` for every view — the CPU
11
15
  //! reference renderer (presentation, screenshots, goldens) and the GPU
12
16
  //! packet planner (upload/retain/evict actions keyed by pixel
@@ -34,7 +38,7 @@ pub const max_registered_canvas_images = canvas_limits.max_registered_canvas_ima
34
38
  pub const max_registered_canvas_image_pixel_bytes = canvas_limits.max_registered_canvas_image_pixel_bytes;
35
39
 
36
40
  /// One registered image's metadata; pixels live in the runtime's slot
37
- /// pool at the same index.
41
+ /// buffer at the same index.
38
42
  pub const CanvasImageEntry = struct {
39
43
  id: canvas.ImageId = 0,
40
44
  width: usize = 0,
@@ -76,7 +80,11 @@ pub fn RuntimeCanvasImages(comptime Runtime: type) type {
76
80
  /// dimensions or a pixel slice that is not exactly
77
81
  /// `width * height * 4`), `error.ImageTooLarge` (over the
78
82
  /// per-image slot bound), `error.ImageRegistryFull` (all
79
- /// `max_registered_canvas_images` slots hold other ids).
83
+ /// `max_registered_canvas_images` slots hold other ids),
84
+ /// `error.OutOfMemory` (the slot's pixel buffer — one lazy
85
+ /// slot-budget heap allocation at the slot's first use — could
86
+ /// not be allocated; the registry is unchanged and the same
87
+ /// registration can be retried once memory recovers).
80
88
  pub fn registerCanvasImage(self: *Runtime, id: canvas.ImageId, width: usize, height: usize, rgba8: []const u8) anyerror!void {
81
89
  if (id == 0) return error.InvalidImageId;
82
90
  // The high bit is the media-surface texture namespace
@@ -93,10 +101,26 @@ pub fn RuntimeCanvasImages(comptime Runtime: type) type {
93
101
 
94
102
  const index = findCanvasImageIndex(self, id) orelse blk: {
95
103
  if (self.canvas_image_count >= max_registered_canvas_images) return error.ImageRegistryFull;
96
- const index = self.canvas_image_count;
97
- self.canvas_image_count += 1;
98
- break :blk index;
104
+ break :blk self.canvas_image_count;
99
105
  };
106
+ if (self.canvas_image_pixels[index].len == 0) {
107
+ // The slot's pixel buffer, allocated LAZILY at the
108
+ // slot's first registration (one slot-budget block from
109
+ // the runtime's FROZEN `owned_allocator` — never the
110
+ // live `options.allocator`, which is public and mutable,
111
+ // so a swap between this allocation and `Runtime.deinit`'s
112
+ // free must not split the alloc/free pair across
113
+ // allocators). Re-registrations and unregister/register
114
+ // churn reuse slot buffers, so a runtime allocates at
115
+ // most once per high-water slot; a runtime that never
116
+ // registers an image allocates nothing — an embedded
117
+ // pool at the budget was 16 MiB in every Runtime, the
118
+ // media-texture-pool regression's twin. This is the only
119
+ // failable step past validation and it runs BEFORE any
120
+ // registry mutation, so an OOM refusal leaves the
121
+ // registry exactly as it was and the caller can retry.
122
+ self.canvas_image_pixels[index] = try self.owned_allocator.alloc(u8, max_registered_canvas_image_pixel_bytes);
123
+ }
100
124
  @memcpy(self.canvas_image_pixels[index][0..byte_len], rgba8);
101
125
  self.canvas_image_entries[index] = .{
102
126
  .id = id,
@@ -104,6 +128,7 @@ pub fn RuntimeCanvasImages(comptime Runtime: type) type {
104
128
  .height = height,
105
129
  .byte_len = byte_len,
106
130
  };
131
+ if (index == self.canvas_image_count) self.canvas_image_count += 1;
107
132
  // No pixel push here: GPU packet hosts receive the bytes
108
133
  // through the binary upload side-channel when a packet's
109
134
  // upload cache action first references the new content
@@ -149,13 +174,21 @@ pub fn RuntimeCanvasImages(comptime Runtime: type) type {
149
174
  const last = self.canvas_image_count - 1;
150
175
  if (index != last) {
151
176
  self.canvas_image_entries[index] = self.canvas_image_entries[last];
152
- @memcpy(
153
- self.canvas_image_pixels[index][0..self.canvas_image_entries[index].byte_len],
154
- self.canvas_image_pixels[last][0..self.canvas_image_entries[index].byte_len],
155
- );
177
+ // Slot buffers are whole-budget heap blocks, so
178
+ // compaction swaps the POINTERS — the last entry's
179
+ // pixels move to `index` without copying a byte, and
180
+ // the freed id's buffer parks on the vacated last slot.
181
+ std.mem.swap([]u8, &self.canvas_image_pixels[index], &self.canvas_image_pixels[last]);
156
182
  }
157
183
  self.canvas_image_entries[last] = .{};
158
184
  self.canvas_image_count = last;
185
+ // The vacated slot KEEPS its buffer for reuse: like the
186
+ // media-surface texture pool, `Runtime.deinit` is the only
187
+ // free — one ownership story for every buffer, no
188
+ // conditional lifetime to reason about. Unregister/register
189
+ // churn (avatar refresh loops) never touches the allocator
190
+ // again, and the footprint stays bounded by the high-water
191
+ // slot count, never lifetime registrations.
159
192
  // Best-effort drop of the platform-side texture: platforms
160
193
  // without the upload seam report UnsupportedService, and a
161
194
  // failed removal only costs the host a stale (unreferenced)
@@ -168,7 +201,7 @@ pub fn RuntimeCanvasImages(comptime Runtime: type) type {
168
201
 
169
202
  /// The registered set as the `ReferenceImage` slice both
170
203
  /// renderers consume, rebuilt into runtime scratch (pixels are
171
- /// borrowed from the slot pool, valid until the next
204
+ /// borrowed from the slot buffers, valid until the next
172
205
  /// register/unregister), with the adopted media-surface
173
206
  /// textures appended as `presentation_only` entries — GPU and
174
207
  /// packet hosts upload and composite those, the deterministic
@@ -180,8 +180,9 @@ pub const DispatchErrorPolicy = enum { degrade, propagate };
180
180
  pub const Runtime = struct {
181
181
  options: Options,
182
182
  /// The allocator that owns every runtime-lifetime heap allocation
183
- /// (registered canvas font bytes, adopted media-surface texture
184
- /// buffers). Captured from `Options.allocator` at init precisely
183
+ /// (registered canvas font bytes, registered canvas image slot
184
+ /// buffers, adopted media-surface texture buffers). Captured from
185
+ /// `Options.allocator` at init precisely
185
186
  /// because `options` is public and mutable: allocation and free of
186
187
  /// owned storage can be a whole runtime lifetime apart, and they
187
188
  /// must resolve through ONE allocator identity. Mutating
@@ -361,11 +362,18 @@ pub const Runtime = struct {
361
362
  canvas_frame_render_override_samples: [max_canvas_render_overrides_per_view]canvas.CanvasRenderOverride = undefined,
362
363
  canvas_frame_render_override_combined: [max_canvas_render_overrides_per_view]canvas.CanvasRenderOverride = undefined,
363
364
  /// Runtime-registered canvas images (see canvas_images.zig): entry
364
- /// metadata, the per-slot pixel pool, and the `ReferenceImage` scratch
365
- /// the frame planner hands to renderers each plan.
365
+ /// metadata, the per-slot pixel buffers, and the `ReferenceImage`
366
+ /// scratch the frame planner hands to renderers each plan. Each
367
+ /// slot's buffer is ONE lazy slot-budget allocation from the frozen
368
+ /// `owned_allocator` at the slot's first registration (freed only by
369
+ /// `deinit`; unregister compaction swaps pointers and keeps vacated
370
+ /// buffers for reuse) — zero registered images cost zero bytes,
371
+ /// where an embedded pool at the budget would put 16 x 1 MiB =
372
+ /// 16 MiB in EVERY Runtime, the media-texture-pool regression's
373
+ /// twin.
366
374
  canvas_image_entries: [canvas_limits.max_registered_canvas_images]runtime_canvas_images.CanvasImageEntry = [_]runtime_canvas_images.CanvasImageEntry{.{}} ** canvas_limits.max_registered_canvas_images,
367
375
  canvas_image_count: usize = 0,
368
- canvas_image_pixels: [canvas_limits.max_registered_canvas_images][canvas_limits.max_registered_canvas_image_pixel_bytes]u8 = undefined,
376
+ canvas_image_pixels: [canvas_limits.max_registered_canvas_images][]u8 = [_][]u8{&.{}} ** canvas_limits.max_registered_canvas_images,
369
377
  /// `ReferenceImage` scratch the frame planner hands to renderers
370
378
  /// each plan: the registered images plus the adopted media-surface
371
379
  /// textures (appended as `presentation_only` entries).
@@ -451,10 +459,16 @@ pub const Runtime = struct {
451
459
  }
452
460
 
453
461
  /// Release the runtime's heap-owned on-demand storage (registered
454
- /// canvas font bytes and adopted media-surface texture buffers,
455
- /// allocated from the init-frozen `owned_allocator`) and disarm the
456
- /// media-surface wake bindings (a producer thread must never wake a
457
- /// dead host). Ends the runtime's life: parsed faces, atlas keys,
462
+ /// canvas font bytes, registered canvas image slot buffers, and
463
+ /// adopted media-surface texture buffers, allocated from the
464
+ /// init-frozen `owned_allocator`), return each registered font's
465
+ /// host-side registration (through the unregister owner captured
466
+ /// into the entry at registration time — host font state is
467
+ /// per-process, this runtime's ids are not, and live `options` may
468
+ /// no longer name the host that received the registration), and
469
+ /// disarm the media-surface wake bindings (a producer
470
+ /// thread must never wake a dead host). Ends the runtime's life:
471
+ /// parsed faces, atlas keys,
458
472
  /// measure providers, adopted textures, and the resource slices
459
473
  /// built over them are invalid past this point. Process-lifetime
460
474
  /// embeddings (the app runner) may skip it — the default
@@ -465,6 +479,30 @@ pub const Runtime = struct {
465
479
  pub fn deinit(self: *Runtime) void {
466
480
  self.disarmMediaSurfaceWakes();
467
481
  for (self.canvas_font_entries[0..self.canvas_font_count]) |entry| {
482
+ // Return the HOST side of the registration before the bytes:
483
+ // registration pushed the face to platforms with host-side
484
+ // text (macOS installs a CoreText descriptor plus caches,
485
+ // keyed by an id that is only permanent per-RUNTIME while
486
+ // that host state is per-process), so a deinit that freed
487
+ // only the Zig-side bytes would leave an embedder cycling
488
+ // runtimes growing host font state for the process lifetime.
489
+ // Through the entry's owner captured at registration time,
490
+ // never live `options`: the option is publicly mutable, and
491
+ // an embedder that swapped the platform after registering
492
+ // must see this land on the host that holds the
493
+ // registration, not on the current platform or a null seam
494
+ // (the `owned_allocator` identity discipline two lines
495
+ // down, applied to the host). Best-effort by design —
496
+ // deinit cannot fail, and a null owner means the platform
497
+ // at registration time retained nothing to return. The
498
+ // entry's ownership token rides along so the host removes
499
+ // ONLY this runtime's registration: a later runtime may
500
+ // have re-registered the id (last wins), and presenting a
501
+ // stale token is a no-op accept — the newer runtime's live
502
+ // face survives this deinit.
503
+ if (entry.host_unregister_fn) |host_unregister_fn| {
504
+ host_unregister_fn(entry.host_unregister_context, entry.id, entry.host_registration_token) catch {};
505
+ }
468
506
  // Through the frozen identity, never `options.allocator`:
469
507
  // an embedder may have swapped the public option since these
470
508
  // bytes were made, and a free must hit the allocator that
@@ -472,6 +510,12 @@ pub const Runtime = struct {
472
510
  self.owned_allocator.free(entry.bytes);
473
511
  }
474
512
  self.canvas_font_count = 0;
513
+ for (&self.canvas_image_pixels) |*buffer| {
514
+ if (buffer.len != 0) self.owned_allocator.free(buffer.*);
515
+ buffer.* = &.{};
516
+ }
517
+ self.canvas_image_entries = [_]runtime_canvas_images.CanvasImageEntry{.{}} ** canvas_limits.max_registered_canvas_images;
518
+ self.canvas_image_count = 0;
475
519
  for (&self.media_surface_pixels) |*buffer| {
476
520
  if (buffer.len != 0) self.owned_allocator.free(buffer.*);
477
521
  buffer.* = &.{};
@@ -550,6 +594,8 @@ pub const Runtime = struct {
550
594
  pub const focusWindow = WindowViewMethods.focusWindow;
551
595
  pub const closeWindow = WindowViewMethods.closeWindow;
552
596
  pub const minimizeWindow = WindowViewMethods.minimizeWindow;
597
+ pub const showWindow = WindowViewMethods.showWindow;
598
+ pub const quitApp = WindowViewMethods.quitApp;
553
599
  pub const createShellWindow = WindowViewMethods.createShellWindow;
554
600
  pub const createSourcelessShellWindow = WindowViewMethods.createSourcelessShellWindow;
555
601
  pub const createShellViews = WindowViewMethods.createShellViews;
@@ -917,7 +963,7 @@ pub fn TestHarness() type {
917
963
  pub fn destroy(self: *Self, gpa: std.mem.Allocator) void {
918
964
  // The harness embeds the runtime's platform: deinit both
919
965
  // returns the runtime's heap-owned storage (registered font
920
- // bytes, lazily allocated media buffers) to the leak-checked
966
+ // bytes, lazily allocated image and media buffers) to the leak-checked
921
967
  // test allocator AND disarms the wake bindings, so a
922
968
  // producer handle a test keeps past destroy (the orphan
923
969
  // tests) can never wake the freed host — the run loop's exit
@@ -933,7 +979,8 @@ pub fn TestHarness() type {
933
979
  .platform = self.null_platform.platform(),
934
980
  .trace_sink = self.trace_sink.sink(),
935
981
  // On-demand runtime storage (registered font bytes,
936
- // adopted media-surface texture buffers) routes through
982
+ // registered image slot buffers, adopted media-surface
983
+ // texture buffers) routes through
937
984
  // the leak-checked test allocator (freed by `destroy`
938
985
  // via Runtime.deinit), so a registration or adoption
939
986
  // that leaks fails the test that made it.