@native-sdk/cli 0.5.2 → 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 (123) hide show
  1. package/build/app.zig +41 -1
  2. package/build.zig +264 -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/sdk/events.ts +39 -2
  10. package/packages/core/src/checker.ts +1 -1
  11. package/packages/core/src/emitter.ts +2625 -262
  12. package/packages/core/src/infer.ts +53 -7
  13. package/skill-data/automation/SKILL.md +7 -4
  14. package/skill-data/core/references/app-model-runtime.md +3 -0
  15. package/skill-data/native-ui/SKILL.md +4 -2
  16. package/skill-data/ts-core/SKILL.md +9 -1
  17. package/src/app_runner/root.zig +106 -1
  18. package/src/app_runner/ts_core_main.zig +4 -0
  19. package/src/automation/protocol.zig +20 -1
  20. package/src/embed/host.zig +27 -0
  21. package/src/embed/tests.zig +187 -0
  22. package/src/embed/ui_host.zig +4 -0
  23. package/src/platform/linux/gtk_host.c +112 -3
  24. package/src/platform/linux/root.zig +71 -0
  25. package/src/platform/macos/appkit_host.h +59 -2
  26. package/src/platform/macos/appkit_host.m +663 -34
  27. package/src/platform/macos/cef_host.mm +262 -10
  28. package/src/platform/macos/root.zig +126 -4
  29. package/src/platform/null_platform.zig +298 -5
  30. package/src/platform/null_platform_tests.zig +39 -0
  31. package/src/platform/root.zig +36 -0
  32. package/src/platform/types.zig +247 -8
  33. package/src/platform/windows/root.zig +134 -0
  34. package/src/platform/windows/webview2_host.cpp +152 -40
  35. package/src/primitives/app_manifest/comptime_scene.zig +5 -0
  36. package/src/primitives/app_manifest/root.zig +1 -0
  37. package/src/primitives/app_manifest/types.zig +32 -0
  38. package/src/primitives/canvas/chart.zig +7 -7
  39. package/src/primitives/canvas/chart_tests.zig +66 -0
  40. package/src/primitives/canvas/commands.zig +48 -0
  41. package/src/primitives/canvas/events.zig +161 -0
  42. package/src/primitives/canvas/font_ttf.zig +155 -8
  43. package/src/primitives/canvas/font_ttf_tests.zig +686 -0
  44. package/src/primitives/canvas/reference.zig +67 -8
  45. package/src/primitives/canvas/render_fingerprints.zig +8 -0
  46. package/src/primitives/canvas/root.zig +25 -0
  47. package/src/primitives/canvas/tests.zig +1 -0
  48. package/src/primitives/canvas/ui.zig +46 -9
  49. package/src/primitives/canvas/ui_markup.zig +82 -34
  50. package/src/primitives/canvas/ui_markup_binary.zig +4 -1
  51. package/src/primitives/canvas/ui_markup_binary_tests.zig +25 -0
  52. package/src/primitives/canvas/ui_markup_compiled.zig +63 -11
  53. package/src/primitives/canvas/ui_markup_compiled_tests.zig +88 -0
  54. package/src/primitives/canvas/ui_markup_contract.zig +10 -1
  55. package/src/primitives/canvas/ui_markup_contract_tests.zig +50 -0
  56. package/src/primitives/canvas/ui_markup_huge_document_tests.zig +154 -0
  57. package/src/primitives/canvas/ui_markup_tests.zig +55 -9
  58. package/src/primitives/canvas/ui_markup_view.zig +81 -14
  59. package/src/primitives/canvas/ui_markup_view_tests.zig +219 -7
  60. package/src/primitives/canvas/ui_schema.zig +29 -0
  61. package/src/primitives/canvas/ui_schema_tests.zig +11 -11
  62. package/src/primitives/canvas/ui_tests.zig +31 -0
  63. package/src/primitives/canvas/vector.zig +258 -141
  64. package/src/primitives/canvas/widget_access.zig +1 -1
  65. package/src/primitives/canvas/widget_builtin_tests.zig +129 -0
  66. package/src/primitives/canvas/widget_layout.zig +9 -4
  67. package/src/primitives/canvas/widget_render.zig +78 -40
  68. package/src/primitives/canvas/widget_render_controls.zig +15 -4
  69. package/src/primitives/canvas/widget_semantics.zig +6 -1
  70. package/src/primitives/canvas/widget_semantics_tests.zig +168 -0
  71. package/src/primitives/canvas/widget_text_input.zig +118 -10
  72. package/src/primitives/canvas/widgets.zig +16 -0
  73. package/src/root.zig +12 -0
  74. package/src/runtime/api.zig +15 -0
  75. package/src/runtime/automation_commands.zig +33 -0
  76. package/src/runtime/automation_widget_dispatch.zig +64 -0
  77. package/src/runtime/bridge_payload.zig +7 -0
  78. package/src/runtime/bridge_responses.zig +5 -0
  79. package/src/runtime/builtin_bridge.zig +9 -4
  80. package/src/runtime/canvas_font_tests.zig +711 -5
  81. package/src/runtime/canvas_fonts.zig +120 -25
  82. package/src/runtime/canvas_frame_helpers.zig +15 -0
  83. package/src/runtime/canvas_image_tests.zig +221 -0
  84. package/src/runtime/canvas_images.zig +78 -19
  85. package/src/runtime/canvas_limits.zig +44 -8
  86. package/src/runtime/canvas_widget_clipboard_tests.zig +123 -0
  87. package/src/runtime/canvas_widget_context_menu_tests.zig +55 -0
  88. package/src/runtime/canvas_widget_events.zig +24 -4
  89. package/src/runtime/canvas_widget_runtime.zig +24 -7
  90. package/src/runtime/canvas_widget_state.zig +6 -1
  91. package/src/runtime/canvas_widget_text_tests.zig +277 -0
  92. package/src/runtime/core.zig +148 -9
  93. package/src/runtime/effects.zig +1664 -43
  94. package/src/runtime/effects_fetch_tests.zig +89 -1
  95. package/src/runtime/effects_file_tests.zig +41 -1
  96. package/src/runtime/effects_image_tests.zig +1304 -0
  97. package/src/runtime/effects_tests.zig +153 -0
  98. package/src/runtime/flow.zig +44 -0
  99. package/src/runtime/gpu_surface_events.zig +7 -0
  100. package/src/runtime/media_surface.zig +726 -0
  101. package/src/runtime/media_surface_tests.zig +1476 -0
  102. package/src/runtime/root.zig +20 -0
  103. package/src/runtime/session_blobs.zig +689 -0
  104. package/src/runtime/session_journal.zig +111 -20
  105. package/src/runtime/session_record.zig +27 -2
  106. package/src/runtime/session_replay.zig +168 -16
  107. package/src/runtime/session_tests.zig +2118 -4
  108. package/src/runtime/shell_layout.zig +7 -0
  109. package/src/runtime/testdata/fonts/NotoSansSC-Receipt.ttf +0 -0
  110. package/src/runtime/testdata/fonts/OFL.txt +93 -0
  111. package/src/runtime/tests.zig +3 -0
  112. package/src/runtime/ts_core_host.zig +439 -4
  113. package/src/runtime/ts_core_host_tests.zig +41 -0
  114. package/src/runtime/ts_ui_app.zig +63 -0
  115. package/src/runtime/ui_app.zig +169 -6
  116. package/src/runtime/ui_app_tests.zig +334 -0
  117. package/src/runtime/ui_app_window_tests.zig +468 -0
  118. package/src/runtime/window_command_bridge_tests.zig +117 -0
  119. package/src/runtime/window_storage.zig +32 -0
  120. package/src/runtime/window_views.zig +61 -1
  121. package/src/tooling/manifest.zig +75 -0
  122. package/src/tooling/raw_manifest.zig +2 -0
  123. package/src/tooling/templates.zig +219 -1
@@ -65,6 +65,18 @@ const builtinBridgeErrorMessage = support.builtinBridgeErrorMessage;
65
65
  const testViewByLabel = support.testViewByLabel;
66
66
  const testCanvasWidgetPartId = support.testCanvasWidgetPartId;
67
67
 
68
+ test "the builder's presented-text store mirrors the per-view draw-text budget" {
69
+ // The chart path-element lockstep's sibling: presented single-line
70
+ // bytes persist into `Builder.text_bytes`, and the store can only
71
+ // overflow (falling back to raw bytes under the forced clip) on a
72
+ // frame the runtime's per-view display-list copy would refuse
73
+ // anyway. Keep the two budgets from drifting.
74
+ try std.testing.expectEqual(
75
+ @import("canvas_limits.zig").max_canvas_text_bytes_per_view,
76
+ canvas.max_display_list_text_bytes,
77
+ );
78
+ }
79
+
68
80
  test "runtime exposes retained canvas widget text geometry" {
69
81
  const TestApp = struct {
70
82
  fn app(self: *@This()) App {
@@ -588,6 +600,271 @@ test "plain Enter inserts a newline in a canvas textarea; chorded Enter never ed
588
600
  try std.testing.expectEqualStrings("First\n", retained.nodes[1].widget.text);
589
601
  }
590
602
 
603
+ test "Enter in a single-line input never inserts, even when the host stuffs a newline into the key event" {
604
+ const TestApp = struct {
605
+ fn app(self: *@This()) App {
606
+ return .{ .context = self, .name = "gpu-widget-input-enter", .source = platform.WebViewSource.html("<h1>Hello</h1>") };
607
+ }
608
+ };
609
+
610
+ const harness = try TestHarness().create(std.testing.allocator, .{});
611
+ defer harness.destroy(std.testing.allocator);
612
+ harness.null_platform.gpu_surfaces = true;
613
+ var app_state: TestApp = .{};
614
+ const app = app_state.app();
615
+ try harness.start(app);
616
+
617
+ _ = try harness.runtime.createView(.{
618
+ .window_id = 1,
619
+ .label = "canvas",
620
+ .kind = .gpu_surface,
621
+ .frame = geometry.RectF.init(0, 0, 260, 160),
622
+ });
623
+
624
+ const input = canvas.Widget{
625
+ .id = 2,
626
+ .kind = .input,
627
+ .frame = geometry.RectF.init(12, 16, 180, 32),
628
+ .text = "Draft",
629
+ .semantics = .{ .label = "Title" },
630
+ };
631
+ var nodes: [2]canvas.WidgetLayoutNode = undefined;
632
+ const layout = try canvas.layoutWidgetTree(.{ .kind = .stack, .children = &.{input} }, geometry.RectF.init(0, 0, 260, 160), &nodes);
633
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", layout);
634
+
635
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
636
+ .window_id = 1,
637
+ .label = "canvas",
638
+ .kind = .pointer_down,
639
+ .x = 100,
640
+ .y = 30,
641
+ } });
642
+ try std.testing.expectEqual(@as(canvas.ObjectId, 2), harness.runtime.views[0].canvas_widget_focused_id);
643
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
644
+ .window_id = 1,
645
+ .label = "canvas",
646
+ .kind = .key_down,
647
+ .key = "a",
648
+ .modifiers = .{ .primary = true, .command = true },
649
+ } });
650
+
651
+ // The macOS-host shape: Return as a bare `enter` keydown. Enter in a
652
+ // single-line field submits; it is NOT an edit.
653
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
654
+ .window_id = 1,
655
+ .label = "canvas",
656
+ .kind = .key_down,
657
+ .key = "enter",
658
+ } });
659
+ var retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
660
+ try std.testing.expectEqualStrings("Draft", retained.nodes[1].widget.text);
661
+ try std.testing.expectEqualDeep(canvas.TextSelection{ .anchor = 0, .focus = 5 }, retained.nodes[1].widget.text_selection.?);
662
+
663
+ // A host that stuffs the newline into the key event's text payload
664
+ // still edits nothing: the sanitized insert strips to empty and
665
+ // suppresses — the live selection is not deleted.
666
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
667
+ .window_id = 1,
668
+ .label = "canvas",
669
+ .kind = .key_down,
670
+ .key = "enter",
671
+ .text = "\n",
672
+ } });
673
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
674
+ .window_id = 1,
675
+ .label = "canvas",
676
+ .kind = .key_down,
677
+ .key = "enter",
678
+ .text = "\r",
679
+ } });
680
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
681
+ try std.testing.expectEqualStrings("Draft", retained.nodes[1].widget.text);
682
+ try std.testing.expectEqualDeep(canvas.TextSelection{ .anchor = 0, .focus = 5 }, retained.nodes[1].widget.text_selection.?);
683
+ }
684
+
685
+ test "automation set_text with line breaks lands sanitized in single-line fields and raw in textareas" {
686
+ const TestApp = struct {
687
+ fn app(self: *@This()) App {
688
+ return .{ .context = self, .name = "gpu-widget-set-text-breaks", .source = platform.WebViewSource.html("<h1>Hello</h1>") };
689
+ }
690
+ };
691
+
692
+ const harness = try TestHarness().create(std.testing.allocator, .{});
693
+ defer harness.destroy(std.testing.allocator);
694
+ harness.null_platform.gpu_surfaces = true;
695
+ var app_state: TestApp = .{};
696
+ const app = app_state.app();
697
+ try harness.start(app);
698
+
699
+ _ = try harness.runtime.createView(.{
700
+ .window_id = 1,
701
+ .label = "canvas",
702
+ .kind = .gpu_surface,
703
+ .frame = geometry.RectF.init(0, 0, 320, 220),
704
+ });
705
+
706
+ const children = [_]canvas.Widget{
707
+ .{
708
+ .id = 2,
709
+ .kind = .search_field,
710
+ .frame = geometry.RectF.init(12, 16, 200, 36),
711
+ .text = "",
712
+ .semantics = .{ .label = "Query" },
713
+ },
714
+ .{
715
+ .id = 3,
716
+ .kind = .textarea,
717
+ .frame = geometry.RectF.init(12, 64, 280, 100),
718
+ .text = "",
719
+ .semantics = .{ .label = "Notes" },
720
+ },
721
+ };
722
+ var nodes: [3]canvas.WidgetLayoutNode = undefined;
723
+ const layout = try canvas.layoutWidgetTree(.{ .kind = .stack, .children = &children }, geometry.RectF.init(0, 0, 320, 220), &nodes);
724
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", layout);
725
+
726
+ // Automation set_text rides the REAL text-input event path, so its
727
+ // line breaks sanitize at the same seam a paste does.
728
+ try dispatchAutomationWidgetAction(&harness.runtime, app, .{ .view_label = "canvas", .id = 2, .action = .set_text, .value = "edge\ncustomers\r\nfirst" });
729
+ var retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
730
+ try std.testing.expectEqualStrings("edgecustomersfirst", retained.nodes[1].widget.text);
731
+
732
+ // The textarea takes the same payload verbatim.
733
+ try dispatchAutomationWidgetAction(&harness.runtime, app, .{ .view_label = "canvas", .id = 3, .action = .set_text, .value = "edge\ncustomers\r\nfirst" });
734
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
735
+ try std.testing.expectEqualStrings("edge\ncustomers\r\nfirst", retained.nodes[2].widget.text);
736
+ }
737
+
738
+ test "ime composition with a newline sanitizes into single-line fields before commit" {
739
+ const TestApp = struct {
740
+ fn app(self: *@This()) App {
741
+ return .{ .context = self, .name = "gpu-widget-ime-newline", .source = platform.WebViewSource.html("<h1>Hello</h1>") };
742
+ }
743
+ };
744
+
745
+ const harness = try TestHarness().create(std.testing.allocator, .{});
746
+ defer harness.destroy(std.testing.allocator);
747
+ harness.null_platform.gpu_surfaces = true;
748
+ var app_state: TestApp = .{};
749
+ const app = app_state.app();
750
+ try harness.start(app);
751
+
752
+ _ = try harness.runtime.createView(.{
753
+ .window_id = 1,
754
+ .label = "canvas",
755
+ .kind = .gpu_surface,
756
+ .frame = geometry.RectF.init(0, 0, 260, 120),
757
+ });
758
+
759
+ const text_field = canvas.Widget{
760
+ .id = 2,
761
+ .kind = .text_field,
762
+ .frame = geometry.RectF.init(12, 16, 180, 36),
763
+ .text = "Cafe",
764
+ .semantics = .{ .label = "Name" },
765
+ };
766
+ var nodes: [2]canvas.WidgetLayoutNode = undefined;
767
+ const layout = try canvas.layoutWidgetTree(.{ .kind = .stack, .children = &.{text_field} }, geometry.RectF.init(0, 0, 260, 120), &nodes);
768
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", layout);
769
+
770
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
771
+ .window_id = 1,
772
+ .label = "canvas",
773
+ .kind = .pointer_down,
774
+ .x = 100,
775
+ .y = 30,
776
+ } });
777
+ try std.testing.expectEqual(@as(canvas.ObjectId, 2), harness.runtime.views[0].canvas_widget_focused_id);
778
+
779
+ // A composition PREVIEW carrying a newline sanitizes at the same
780
+ // seam every insert does — the preview in the retained editor holds
781
+ // the stripped bytes, so the COMMIT (which lands whatever the
782
+ // preview holds) can never commit a line break.
783
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
784
+ .window_id = 1,
785
+ .label = "canvas",
786
+ .kind = .ime_set_composition,
787
+ .text = " au\nlait",
788
+ .composition_cursor = 8,
789
+ } });
790
+ var retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
791
+ try std.testing.expectEqualStrings("Cafe aulait", retained.nodes[1].widget.text);
792
+ try std.testing.expectEqualDeep(canvas.TextRange.init(4, 11), retained.nodes[1].widget.text_composition.?);
793
+
794
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
795
+ .window_id = 1,
796
+ .label = "canvas",
797
+ .kind = .ime_commit_composition,
798
+ } });
799
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
800
+ try std.testing.expectEqualStrings("Cafe aulait", retained.nodes[1].widget.text);
801
+ try std.testing.expect(retained.nodes[1].widget.text_composition == null);
802
+ }
803
+
804
+ test "a model-set single-line value with a newline paints one line inside the field" {
805
+ const TestApp = struct {
806
+ fn app(self: *@This()) App {
807
+ return .{ .context = self, .name = "gpu-widget-value-newline", .source = platform.WebViewSource.html("<h1>Hello</h1>") };
808
+ }
809
+ };
810
+
811
+ const harness = try TestHarness().create(std.testing.allocator, .{});
812
+ defer harness.destroy(std.testing.allocator);
813
+ harness.null_platform.gpu_surfaces = true;
814
+ var app_state: TestApp = .{};
815
+ const app = app_state.app();
816
+ try harness.start(app);
817
+
818
+ _ = try harness.runtime.createView(.{
819
+ .window_id = 1,
820
+ .label = "canvas",
821
+ .kind = .gpu_surface,
822
+ .frame = geometry.RectF.init(0, 0, 320, 120),
823
+ });
824
+
825
+ // A Zig core can put a newline into a field's VALUE today; the
826
+ // sanitize seam only guards EDITS. The retained value keeps the raw
827
+ // bytes (semantics and automation report honestly), but the painted
828
+ // text presents the breaks as spaces on ONE line, under a forced
829
+ // content-rect clip — nothing escapes the rounded border.
830
+ const input = canvas.Widget{
831
+ .id = 2,
832
+ .kind = .input,
833
+ .frame = geometry.RectF.init(12, 16, 200, 32),
834
+ .text = "one\ntwo",
835
+ .semantics = .{ .label = "Title" },
836
+ };
837
+ var nodes: [2]canvas.WidgetLayoutNode = undefined;
838
+ const layout = try canvas.layoutWidgetTree(.{ .kind = .stack, .children = &.{input} }, geometry.RectF.init(0, 0, 320, 120), &nodes);
839
+ _ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", layout);
840
+ _ = try harness.runtime.emitCanvasWidgetDisplayList(1, "canvas", .{});
841
+
842
+ const display_list = try harness.runtime.canvasDisplayList(1, "canvas");
843
+ var saw_presented_text = false;
844
+ var saw_clip = false;
845
+ for (display_list.commands) |command| {
846
+ switch (command) {
847
+ .draw_text => |text| {
848
+ if (text.id == testCanvasWidgetPartId(2, 3)) {
849
+ try std.testing.expectEqualStrings("one two", text.text);
850
+ saw_presented_text = true;
851
+ }
852
+ },
853
+ .push_clip => |clip| {
854
+ if (clip.id == testCanvasWidgetPartId(2, 16)) saw_clip = true;
855
+ },
856
+ else => {},
857
+ }
858
+ }
859
+ try std.testing.expect(saw_presented_text);
860
+ try std.testing.expect(saw_clip);
861
+
862
+ // Semantics (and therefore automation and assistive tech) still read
863
+ // the RAW model value: presentation never rewrites the model.
864
+ const snapshot = harness.runtime.automationSnapshot("Widgets");
865
+ try std.testing.expectEqualStrings("one\ntwo", snapshot.widgets[0].text_value);
866
+ }
867
+
591
868
  test "runtime applies ime composition edits to canvas text fields" {
592
869
  const TestApp = struct {
593
870
  fn app(self: *@This()) App {
@@ -15,6 +15,7 @@ const shell_layout = @import("shell_layout.zig");
15
15
  const canvas_frame_helpers = @import("canvas_frame.zig");
16
16
  const canvas_limits = @import("canvas_limits.zig");
17
17
  const runtime_canvas_images = @import("canvas_images.zig");
18
+ const runtime_media_surface = @import("media_surface.zig");
18
19
  const runtime_canvas_fonts = @import("canvas_fonts.zig");
19
20
  const runtime_canvas_widget_context_menu = @import("canvas_widget_context_menu.zig");
20
21
  const runtime_canvas_widget_display = @import("canvas_widget_display.zig");
@@ -178,6 +179,19 @@ pub const DispatchErrorPolicy = enum { degrade, propagate };
178
179
 
179
180
  pub const Runtime = struct {
180
181
  options: Options,
182
+ /// The allocator that owns every runtime-lifetime heap allocation
183
+ /// (registered canvas font bytes, registered canvas image slot
184
+ /// buffers, adopted media-surface texture buffers). Captured from
185
+ /// `Options.allocator` at init precisely
186
+ /// because `options` is public and mutable: allocation and free of
187
+ /// owned storage can be a whole runtime lifetime apart, and they
188
+ /// must resolve through ONE allocator identity. Mutating
189
+ /// `options.allocator` on a live runtime does not retarget this —
190
+ /// bytes are always returned to the allocator that made them. Every
191
+ /// on-demand owner (registration and adoption allocs, refusal-path
192
+ /// frees, the `deinit` frees) goes through this field, never
193
+ /// through `options.allocator`.
194
+ owned_allocator: std.mem.Allocator,
181
195
  surface: platform.Surface,
182
196
  appearance: platform.Appearance = .{},
183
197
  windows: [platform.max_windows]RuntimeWindow = undefined,
@@ -348,20 +362,51 @@ pub const Runtime = struct {
348
362
  canvas_frame_render_override_samples: [max_canvas_render_overrides_per_view]canvas.CanvasRenderOverride = undefined,
349
363
  canvas_frame_render_override_combined: [max_canvas_render_overrides_per_view]canvas.CanvasRenderOverride = undefined,
350
364
  /// Runtime-registered canvas images (see canvas_images.zig): entry
351
- /// metadata, the per-slot pixel pool, and the `ReferenceImage` scratch
352
- /// 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.
353
374
  canvas_image_entries: [canvas_limits.max_registered_canvas_images]runtime_canvas_images.CanvasImageEntry = [_]runtime_canvas_images.CanvasImageEntry{.{}} ** canvas_limits.max_registered_canvas_images,
354
375
  canvas_image_count: usize = 0,
355
- canvas_image_pixels: [canvas_limits.max_registered_canvas_images][canvas_limits.max_registered_canvas_image_pixel_bytes]u8 = undefined,
356
- canvas_image_resources_scratch: [canvas_limits.max_registered_canvas_images]canvas.ReferenceImage = undefined,
376
+ canvas_image_pixels: [canvas_limits.max_registered_canvas_images][]u8 = [_][]u8{&.{}} ** canvas_limits.max_registered_canvas_images,
377
+ /// `ReferenceImage` scratch the frame planner hands to renderers
378
+ /// each plan: the registered images plus the adopted media-surface
379
+ /// textures (appended as `presentation_only` entries).
380
+ canvas_image_resources_scratch: [canvas_limits.max_registered_canvas_images + canvas_limits.max_media_surface_channels]canvas.ReferenceImage = undefined,
381
+ /// Adopted media-surface textures (see media_surface.zig): entry
382
+ /// metadata and the per-channel pixel buffers the loop thread owns.
383
+ /// Each buffer is ONE lazy frame-budget allocation from the frozen
384
+ /// `owned_allocator` at the entry's first adoption (freed only by
385
+ /// `deinit`) — zero media use costs zero bytes, where an embedded
386
+ /// pool at the budget would put 4 x 8 MiB = 32 MiB in EVERY Runtime
387
+ /// (the registered-font-pool regression's twin, measured on the
388
+ /// docs wasm preview host as +32 MB per component tile before any
389
+ /// producer existed). The cross-thread mailbox producers push into
390
+ /// is process-lived module state, deliberately NOT here — a
391
+ /// producer outliving this runtime must never reach runtime memory.
392
+ media_surface_entries: [canvas_limits.max_media_surface_channels]runtime_media_surface.MediaSurfaceTextureEntry = [_]runtime_media_surface.MediaSurfaceTextureEntry{.{}} ** canvas_limits.max_media_surface_channels,
393
+ media_surface_count: usize = 0,
394
+ media_surface_pixels: [canvas_limits.max_media_surface_channels][]u8 = [_][]u8{&.{}} ** canvas_limits.max_media_surface_channels,
395
+ /// Process-unique tag stamped on mailbox slots this runtime claims
396
+ /// (0 until the first acquire): slot ownership survives allocator
397
+ /// address reuse across runtimes in one process.
398
+ media_surface_runtime_tag: u64 = 0,
357
399
  /// Runtime-registered canvas fonts (see canvas_fonts.zig): entry
358
- /// metadata, the per-slot TrueType byte pool with parsed face views
359
- /// over it, the `ReferenceFont` scratch the frame planner hands to
360
- /// renderers, and the font-aware measure provider installed on first
361
- /// registration for platforms without host-side text measurement.
400
+ /// metadata carrying each face's heap-owned TrueType bytes (exact
401
+ /// file size, allocated from the init-frozen `owned_allocator` at
402
+ /// registration —
403
+ /// zero fonts cost zero bytes; freed only by `deinit`, registration
404
+ /// being permanent), parsed face views over those bytes, the
405
+ /// `ReferenceFont` scratch the frame planner hands to renderers, and
406
+ /// the font-aware measure provider installed on first registration
407
+ /// for platforms without host-side text measurement.
362
408
  canvas_font_entries: [canvas_limits.max_registered_canvas_fonts]runtime_canvas_fonts.CanvasFontEntry = [_]runtime_canvas_fonts.CanvasFontEntry{.{}} ** canvas_limits.max_registered_canvas_fonts,
363
409
  canvas_font_count: usize = 0,
364
- canvas_font_bytes: [canvas_limits.max_registered_canvas_fonts][canvas_limits.max_registered_canvas_font_bytes]u8 = undefined,
365
410
  canvas_font_faces: [canvas_limits.max_registered_canvas_fonts]canvas.font_ttf.Face = undefined,
366
411
  canvas_font_resources_scratch: [canvas_limits.max_registered_canvas_fonts]canvas.ReferenceFont = undefined,
367
412
  canvas_font_measure_provider: canvas.TextMeasureProvider = .{ .measure_fn = runtime_canvas_fonts.unboundCanvasFontMeasure },
@@ -388,6 +433,10 @@ pub const Runtime = struct {
388
433
  }
389
434
  }
390
435
  self.options = options;
436
+ // Freeze the ownership allocator now (see the field doc):
437
+ // `options` stays publicly mutable, but the identity that owns
438
+ // on-demand storage must not move under live allocations.
439
+ self.owned_allocator = options.allocator;
391
440
  self.surface = options.platform.surface();
392
441
  // The profile rings exceed the small-default copy bound above;
393
442
  // assign explicitly so the disabled state is never undefined.
@@ -409,6 +458,72 @@ pub const Runtime = struct {
409
458
  canvas.bumpTextMeasureGeneration();
410
459
  }
411
460
 
461
+ /// Release the runtime's heap-owned on-demand storage (registered
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,
472
+ /// measure providers, adopted textures, and the resource slices
473
+ /// built over them are invalid past this point. Process-lifetime
474
+ /// embeddings (the app runner) may skip it — the default
475
+ /// allocator's pages die with the process, and the run loop's exit
476
+ /// defer already disarms the wakes — but embedders that create and
477
+ /// destroy runtimes in one process (tests, the docs wasm preview
478
+ /// host) call it to return the storage.
479
+ pub fn deinit(self: *Runtime) void {
480
+ self.disarmMediaSurfaceWakes();
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
+ }
506
+ // Through the frozen identity, never `options.allocator`:
507
+ // an embedder may have swapped the public option since these
508
+ // bytes were made, and a free must hit the allocator that
509
+ // allocated.
510
+ self.owned_allocator.free(entry.bytes);
511
+ }
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;
519
+ for (&self.media_surface_pixels) |*buffer| {
520
+ if (buffer.len != 0) self.owned_allocator.free(buffer.*);
521
+ buffer.* = &.{};
522
+ }
523
+ self.media_surface_entries = [_]runtime_media_surface.MediaSurfaceTextureEntry{.{}} ** canvas_limits.max_media_surface_channels;
524
+ self.media_surface_count = 0;
525
+ }
526
+
412
527
  fn fieldHasSmallDefault(comptime field: std.builtin.Type.StructField) bool {
413
528
  // Large fixed-capacity arrays default to undefined; skip writing
414
529
  // them so construction touches kilobytes, not megabytes.
@@ -479,6 +594,8 @@ pub const Runtime = struct {
479
594
  pub const focusWindow = WindowViewMethods.focusWindow;
480
595
  pub const closeWindow = WindowViewMethods.closeWindow;
481
596
  pub const minimizeWindow = WindowViewMethods.minimizeWindow;
597
+ pub const showWindow = WindowViewMethods.showWindow;
598
+ pub const quitApp = WindowViewMethods.quitApp;
482
599
  pub const createShellWindow = WindowViewMethods.createShellWindow;
483
600
  pub const createSourcelessShellWindow = WindowViewMethods.createSourcelessShellWindow;
484
601
  pub const createShellViews = WindowViewMethods.createShellViews;
@@ -716,6 +833,13 @@ pub const Runtime = struct {
716
833
  pub const registeredCanvasImageCount = CanvasImageMethods.registeredCanvasImageCount;
717
834
  pub const canvasImageRegistryBinding = CanvasImageMethods.canvasImageRegistryBinding;
718
835
 
836
+ const MediaSurfaceMethods = runtime_media_surface.RuntimeMediaSurfaces(Runtime);
837
+ pub const acquireMediaSurfaceProducer = MediaSurfaceMethods.acquireMediaSurfaceProducer;
838
+ pub const adoptMediaSurfaceFrames = MediaSurfaceMethods.adoptMediaSurfaceFrames;
839
+ pub const disarmMediaSurfaceWakes = MediaSurfaceMethods.disarmMediaSurfaceWakes;
840
+ pub const adoptedMediaSurfaceTexture = MediaSurfaceMethods.adoptedMediaSurfaceTexture;
841
+ const adoptedMediaSurfaceTextures = MediaSurfaceMethods.adoptedMediaSurfaceTextures;
842
+
719
843
  const CanvasFontMethods = runtime_canvas_fonts.RuntimeCanvasFonts(Runtime);
720
844
  pub const registerCanvasFont = CanvasFontMethods.registerCanvasFont;
721
845
  pub const registeredCanvasFonts = CanvasFontMethods.registeredCanvasFonts;
@@ -837,6 +961,14 @@ pub fn TestHarness() type {
837
961
  }
838
962
 
839
963
  pub fn destroy(self: *Self, gpa: std.mem.Allocator) void {
964
+ // The harness embeds the runtime's platform: deinit both
965
+ // returns the runtime's heap-owned storage (registered font
966
+ // bytes, lazily allocated image and media buffers) to the leak-checked
967
+ // test allocator AND disarms the wake bindings, so a
968
+ // producer handle a test keeps past destroy (the orphan
969
+ // tests) can never wake the freed host — the run loop's exit
970
+ // defer for real apps, this line for harness-driven ones.
971
+ self.runtime.deinit();
840
972
  gpa.destroy(self);
841
973
  }
842
974
 
@@ -846,6 +978,13 @@ pub fn TestHarness() type {
846
978
  Runtime.initAt(&self.runtime, .{
847
979
  .platform = self.null_platform.platform(),
848
980
  .trace_sink = self.trace_sink.sink(),
981
+ // On-demand runtime storage (registered font bytes,
982
+ // registered image slot buffers, adopted media-surface
983
+ // texture buffers) routes through
984
+ // the leak-checked test allocator (freed by `destroy`
985
+ // via Runtime.deinit), so a registration or adoption
986
+ // that leaks fails the test that made it.
987
+ .allocator = if (builtin.is_test) std.testing.allocator else std.heap.page_allocator,
849
988
  // Real-executor effect tests spawn processes that must
850
989
  // see the parent environment (HOME, PATH), exactly as
851
990
  // the app runner threads it from `std.process.Init`.