@native-sdk/cli 0.11.0 → 0.11.1

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 (182) hide show
  1. package/build/app.zig +73 -37
  2. package/build.zig +239 -5
  3. package/native-sdk.d.ts +5 -1
  4. package/package.json +13 -12
  5. package/packages/core/compile-surface/core.ts +30 -0
  6. package/packages/core/package-lock.json +6 -6
  7. package/packages/core/package.json +3 -3
  8. package/packages/core/scripts/scriptc-toolchain.json +44 -44
  9. package/packages/core/scripts/stage_external_core.mjs +27 -16
  10. package/packages/core/sdk/core.d.ts +24 -1
  11. package/packages/core/sdk/core.ts +51 -1
  12. package/packages/core/sdk/events.d.ts +5 -0
  13. package/packages/core/sdk/events.ts +10 -0
  14. package/packages/core/src/bridge_descriptors.ts +3 -3
  15. package/packages/core/src/checker.ts +20 -0
  16. package/packages/core/src/cli.ts +2 -11
  17. package/packages/core/src/context_menus.ts +76 -0
  18. package/packages/core/src/diagnostics.ts +7 -0
  19. package/packages/core/src/extent_program.ts +53 -0
  20. package/packages/core/src/frontend.ts +12 -10
  21. package/packages/core/src/markup_canonical.ts +140 -0
  22. package/packages/core/src/markup_construction.ts +189 -0
  23. package/packages/core/src/markup_display.ts +43 -0
  24. package/packages/core/src/markup_document.ts +226 -0
  25. package/packages/core/src/markup_elements.ts +225 -0
  26. package/packages/core/src/markup_expressions.ts +79 -0
  27. package/packages/core/src/markup_imports.ts +146 -0
  28. package/packages/core/src/markup_parser.ts +228 -0
  29. package/packages/core/src/markup_runtime.ts +93 -0
  30. package/packages/core/src/markup_scalars.ts +143 -0
  31. package/packages/core/src/markup_shapes.ts +243 -0
  32. package/packages/core/src/markup_specialized.ts +242 -0
  33. package/packages/core/src/markup_text.ts +86 -0
  34. package/packages/core/src/markup_values.ts +336 -0
  35. package/packages/core/src/portable_sources.json +24 -0
  36. package/packages/core/src/registered_font.ts +2 -3
  37. package/packages/core/src/runtime_policy.ts +24 -0
  38. package/packages/core/src/scalar_text.ts +1 -3
  39. package/packages/core/src/scroll_mutation.ts +86 -0
  40. package/packages/core/src/scroll_tree.ts +86 -0
  41. package/packages/core/src/service_contract.ts +53 -2
  42. package/packages/core/src/sqlite_cli.ts +3 -46
  43. package/packages/core/src/sqlite_codegen.ts +84 -0
  44. package/packages/core/src/staged_rebuild.ts +60 -0
  45. package/packages/core/src/view_bridge.ts +161 -0
  46. package/packages/core/src/view_check.ts +145 -0
  47. package/packages/core/src/view_evaluator.ts +823 -0
  48. package/packages/core/src/view_frontend.ts +340 -218
  49. package/packages/core/src/view_program.ts +221 -0
  50. package/packages/core/src/view_reload.ts +166 -0
  51. package/packages/core/src/view_reload_watch.ts +98 -0
  52. package/packages/core/src/view_sources.ts +34 -0
  53. package/packages/core/src/virtual_windows.ts +88 -0
  54. package/packages/core/src/web_panes.ts +67 -0
  55. package/packages/core/src/widget_continuity.ts +37 -0
  56. package/packages/core/src/widget_reconcile.ts +63 -0
  57. package/packages/core/src/widget_retention.ts +62 -0
  58. package/skill-data/native-ui/SKILL.md +2 -2
  59. package/skill-data/ts-core/SKILL.md +5 -3
  60. package/skill-data/ts-services/references/service-surface.md +29 -5
  61. package/src/app_runner/root.zig +14 -0
  62. package/src/app_runner/ts_compiled_view.zig +88 -17
  63. package/src/app_runner/ts_core_main.zig +43 -5
  64. package/src/app_runner/ts_core_mobile.zig +1 -1
  65. package/src/automation/server.zig +56 -3
  66. package/src/embed/host.zig +2 -0
  67. package/src/embed/tests.zig +2 -0
  68. package/src/embed/ui_host.zig +2 -0
  69. package/src/platform/keyboard_input.zig +32 -0
  70. package/src/platform/linux/root.zig +2 -0
  71. package/src/platform/macos/appkit_host.h +7 -2
  72. package/src/platform/macos/appkit_host.m +31 -9
  73. package/src/platform/macos/cef_host.mm +15 -1
  74. package/src/platform/macos/global_shortcuts.zig +138 -0
  75. package/src/platform/macos/key_codes.zig +102 -0
  76. package/src/platform/macos/keyboard_input.zig +81 -0
  77. package/src/platform/macos/root.zig +25 -3
  78. package/src/platform/macos/webview_inspection_test.m +106 -0
  79. package/src/platform/null_platform.zig +104 -0
  80. package/src/platform/root.zig +14 -0
  81. package/src/platform/types.zig +102 -0
  82. package/src/platform/windows/global_shortcuts.zig +157 -0
  83. package/src/platform/windows/key_codes.zig +63 -0
  84. package/src/platform/windows/keyboard_input.zig +136 -0
  85. package/src/platform/windows/root.zig +15 -0
  86. package/src/primitives/canvas/decision_reuse.zig +133 -0
  87. package/src/primitives/canvas/font_ttf.zig +27 -3
  88. package/src/primitives/canvas/fonts/Geist-Bold.ttf +0 -0
  89. package/src/primitives/canvas/fonts/Geist-Medium.ttf +0 -0
  90. package/src/primitives/canvas/fonts/OFL.txt +93 -0
  91. package/src/primitives/canvas/fonts/README.md +15 -0
  92. package/src/primitives/canvas/intrinsic_measure_policy.zig +4 -1
  93. package/src/primitives/canvas/markdown_tests.zig +3 -3
  94. package/src/primitives/canvas/reference.zig +3 -5
  95. package/src/primitives/canvas/reference_tests.zig +58 -0
  96. package/src/primitives/canvas/registered_font_policy.zig +1 -1
  97. package/src/primitives/canvas/root.zig +7 -4
  98. package/src/primitives/canvas/text_metrics.zig +32 -39
  99. package/src/primitives/canvas/text_metrics_tests.zig +52 -7
  100. package/src/primitives/canvas/text_query_policy.zig +142 -3
  101. package/src/primitives/canvas/text_scalar_policy.zig +80 -2
  102. package/src/primitives/canvas/tokens.zig +9 -0
  103. package/src/primitives/canvas/ui.zig +1 -0
  104. package/src/primitives/canvas/ui_markup.zig +69 -14
  105. package/src/primitives/canvas/ui_markup_canonical.zig +237 -0
  106. package/src/primitives/canvas/ui_markup_compiled.zig +1407 -23
  107. package/src/primitives/canvas/ui_markup_compiled_values.zig +76 -0
  108. package/src/primitives/canvas/ui_markup_construction.zig +146 -0
  109. package/src/primitives/canvas/ui_markup_display.zig +82 -0
  110. package/src/primitives/canvas/ui_markup_document.zig +196 -0
  111. package/src/primitives/canvas/ui_markup_elements.zig +163 -0
  112. package/src/primitives/canvas/ui_markup_expr.zig +192 -2
  113. package/src/primitives/canvas/ui_markup_imports.zig +262 -0
  114. package/src/primitives/canvas/ui_markup_parser.zig +117 -0
  115. package/src/primitives/canvas/ui_markup_runtime.zig +76 -0
  116. package/src/primitives/canvas/ui_markup_scalars.zig +115 -0
  117. package/src/primitives/canvas/ui_markup_shapes.zig +224 -0
  118. package/src/primitives/canvas/ui_markup_specialized.zig +106 -0
  119. package/src/primitives/canvas/ui_markup_text.zig +128 -0
  120. package/src/primitives/canvas/ui_markup_values.zig +368 -0
  121. package/src/primitives/canvas/ui_markup_view.zig +2817 -74
  122. package/src/primitives/canvas/virtual_extent_policy.zig +58 -0
  123. package/src/primitives/canvas/virtual_extents.zig +47 -33
  124. package/src/primitives/canvas/widget_metric_policy.zig +4 -1
  125. package/src/primitives/canvas/widget_runtime.zig +2 -1
  126. package/src/root.zig +5 -0
  127. package/src/runtime/api.zig +1 -0
  128. package/src/runtime/bridge_descriptor_policy.zig +1 -1
  129. package/src/runtime/bridge_payload.zig +2 -0
  130. package/src/runtime/canvas_widget_context_menu.zig +179 -0
  131. package/src/runtime/canvas_widget_context_menu_test_cases.zig +1147 -0
  132. package/src/runtime/canvas_widget_context_menu_tests.zig +23 -1098
  133. package/src/runtime/canvas_widget_runtime.zig +306 -92
  134. package/src/runtime/context_menu_policy.zig +112 -0
  135. package/src/runtime/core.zig +14 -0
  136. package/src/runtime/effects.zig +172 -4
  137. package/src/runtime/effects_host_tests.zig +25 -0
  138. package/src/runtime/flow.zig +1 -0
  139. package/src/runtime/flow_reference.zig +1 -0
  140. package/src/runtime/global_shortcuts.zig +75 -0
  141. package/src/runtime/keyboard_input.zig +86 -0
  142. package/src/runtime/root.zig +3 -0
  143. package/src/runtime/scroll_mutation_policy.zig +130 -0
  144. package/src/runtime/scroll_mutation_runtime.zig +158 -0
  145. package/src/runtime/scroll_tree_policy.zig +99 -0
  146. package/src/runtime/session_record.zig +7 -0
  147. package/src/runtime/staged_rebuild_policy.zig +83 -0
  148. package/src/runtime/system_services.zig +19 -0
  149. package/src/runtime/tests.zig +1 -0
  150. package/src/runtime/ts_core_host.zig +126 -0
  151. package/src/runtime/ts_ui_app.zig +24 -0
  152. package/src/runtime/ui_app.zig +274 -31
  153. package/src/runtime/ui_app_tests.zig +48 -179
  154. package/src/runtime/ui_app_window_tests.zig +133 -0
  155. package/src/runtime/view_reload.zig +226 -0
  156. package/src/runtime/view_widget_scroll.zig +30 -21
  157. package/src/runtime/view_widget_tree.zig +43 -13
  158. package/src/runtime/virtual_window_policy.zig +128 -0
  159. package/src/runtime/web_pane_policy.zig +111 -0
  160. package/src/runtime/web_pane_test_cases.zig +204 -0
  161. package/src/runtime/widget_continuity_policy.zig +73 -0
  162. package/src/runtime/widget_reconcile_policy.zig +81 -0
  163. package/src/runtime/widget_retention_policy.zig +75 -0
  164. package/src/runtime/window_views.zig +19 -4
  165. package/src/security/root.zig +1 -0
  166. package/src/tooling/templates.zig +8 -0
  167. package/src/tooling/ts_core.zig +84 -1
  168. package/src/tooling/verbs.zig +79 -15
  169. package/tools/corewire/core.test.ts +25 -3
  170. package/tools/corewire/core_abi.zig +4 -0
  171. package/tools/corewire/core_contract.ts +1 -1
  172. package/tools/corewire/core_goldens.json +75 -75
  173. package/tools/corewire/core_projection.ts +3 -2
  174. package/tools/corewire/emit_mirror.ts +19 -9
  175. package/tools/corewire/facade_templates.ts +1 -1
  176. package/tools/corewire/intake.test.ts +4 -0
  177. package/tools/corewire/intake_goldens.json +50 -50
  178. package/tools/corewire/invocation.test.ts +4 -0
  179. package/tools/corewire/invocation_goldens.json +3 -3
  180. package/tools/corewire/mirror_templates.ts +4 -0
  181. package/tools/corewire/profile_emission.ts +2 -0
  182. package/tools/corewire/sidecar.zig +2 -0
@@ -13,6 +13,12 @@ const std = @import("std");
13
13
  const canvas = @import("root.zig");
14
14
  const markup = @import("ui_markup.zig");
15
15
  const ui_provenance = @import("ui_provenance.zig");
16
+ const construction = @import("ui_markup_construction.zig");
17
+ const elements = @import("ui_markup_elements.zig");
18
+ const specialized = @import("ui_markup_specialized.zig");
19
+ const value_programs = @import("ui_markup_values.zig");
20
+ const runtime_programs = @import("ui_markup_runtime.zig");
21
+ const shape_programs = @import("ui_markup_shapes.zig");
16
22
 
17
23
  pub const BuildError = error{ MarkupBuild, OutOfMemory };
18
24
 
@@ -65,6 +71,11 @@ pub fn MarkupView(comptime ModelT: type, comptime MsgT: type) type {
65
71
  /// Build the view for the current model. Compatible with the
66
72
  /// hand-written `view(ui, model)` shape.
67
73
  pub fn build(self: *Self, ui: *Ui, model: *const ModelT) BuildError!Ui.Node {
74
+ if (ui.expression_policy) |owner| return self.buildOwned(owner, ui, model);
75
+ return self.buildReference(ui, model);
76
+ }
77
+
78
+ fn buildReference(self: *Self, ui: *Ui, model: *const ModelT) BuildError!Ui.Node {
68
79
  if (self.document.imports.len > 0) {
69
80
  return self.failNode(self.document.imports[0], markup.import_unresolved_message);
70
81
  }
@@ -72,7 +83,7 @@ pub fn MarkupView(comptime ModelT: type, comptime MsgT: type) type {
72
83
  self.diagnostic = .{ .line = 1, .column = 1, .message = markup.component_file_view_message };
73
84
  return error.MarkupBuild;
74
85
  };
75
- var scope = Scope{ .model = model, .arena = ui.arena };
86
+ var scope = Scope{ .model = model, .arena = ui.arena, .expression_policy = ui.expression_policy };
76
87
  return self.buildNode(ui, &scope, root);
77
88
  }
78
89
 
@@ -124,61 +135,2162 @@ pub fn MarkupView(comptime ModelT: type, comptime MsgT: type) type {
124
135
 
125
136
  const max_scope_depth = 16;
126
137
 
127
- const Scope = struct {
128
- model: *const ModelT,
129
- /// The build arena, threaded to arena-taking scalar binding fns
130
- /// (`pub fn summary(m: *const Model, arena: std.mem.Allocator)
131
- /// []const u8`). Strings they produce live exactly as long as
132
- /// the built tree.
133
- arena: std.mem.Allocator,
134
- entries: [max_scope_depth]ScopeEntry = undefined,
135
- len: usize = 0,
136
- /// Bindings resolve entries[floor..len] then the model: a
137
- /// template body sees its args and its own loop variables but
138
- /// not the loop variables at the expansion site.
139
- floor: usize = 0,
140
- /// Template expansion depth: a hard cap on runtime recursion
141
- /// for documents the validator never saw. Legit nesting is
142
- /// bounded by define-before-use (checked per expansion via
143
- /// `template_ctx`) plus lexical slot-content depth.
144
- use_depth: usize = 0,
145
- /// Index of the template whose body is currently building, or
146
- /// null in root/consumer scope. A use inside a body may only
147
- /// reference earlier templates (the validator's rule, enforced
148
- /// again here so an unvalidated document cannot recurse).
149
- template_ctx: ?usize = null,
150
- /// Template instantiation chain for provenance stamping: the
151
- /// `<use>` sites (outermost first) that put the node currently
152
- /// building where it is. Arena-allocated snapshots — pushed by
153
- /// `buildUse`, restored by `<slot/>` content — so every
154
- /// stamped `NodeSource` shares one immutable slice.
155
- source_chain: []const ui_provenance.UseSite = &.{},
138
+ const Scope = struct {
139
+ model: *const ModelT,
140
+ /// The build arena, threaded to arena-taking scalar binding fns
141
+ /// (`pub fn summary(m: *const Model, arena: std.mem.Allocator)
142
+ /// []const u8`). Strings they produce live exactly as long as
143
+ /// the built tree.
144
+ arena: std.mem.Allocator,
145
+ expression_policy: ?markup.expr.Policy = null,
146
+ entries: [max_scope_depth]ScopeEntry = undefined,
147
+ len: usize = 0,
148
+ /// Bindings resolve entries[floor..len] then the model: a
149
+ /// template body sees its args and its own loop variables but
150
+ /// not the loop variables at the expansion site.
151
+ floor: usize = 0,
152
+ /// Template expansion depth: a hard cap on runtime recursion
153
+ /// for documents the validator never saw. Legit nesting is
154
+ /// bounded by define-before-use (checked per expansion via
155
+ /// `template_ctx`) plus lexical slot-content depth.
156
+ use_depth: usize = 0,
157
+ /// Index of the template whose body is currently building, or
158
+ /// null in root/consumer scope. A use inside a body may only
159
+ /// reference earlier templates (the validator's rule, enforced
160
+ /// again here so an unvalidated document cannot recurse).
161
+ template_ctx: ?usize = null,
162
+ /// Template instantiation chain for provenance stamping: the
163
+ /// `<use>` sites (outermost first) that put the node currently
164
+ /// building where it is. Arena-allocated snapshots — pushed by
165
+ /// `buildUse`, restored by `<slot/>` content — so every
166
+ /// stamped `NodeSource` shares one immutable slice.
167
+ source_chain: []const ui_provenance.UseSite = &.{},
168
+
169
+ fn lookup(self: *const Scope, head: []const u8) ?*const ScopeEntry {
170
+ if (self.expression_policy) |owner| {
171
+ var request = construction.Request.init(4);
172
+ defer request.deinit();
173
+ request.words(&.{ self.len, self.floor });
174
+ request.blob(head);
175
+ for (self.entries[0..self.len]) |entry| request.blob(entry.name);
176
+ const found = request.decision(owner);
177
+ if (found.action != 7 or found.fields[0] > self.len) @panic("invalid construction lookup");
178
+ return if (found.fields[0] == 0) null else &self.entries[found.fields[0] - 1];
179
+ }
180
+ var index = self.len;
181
+ while (index > self.floor) {
182
+ index -= 1;
183
+ if (std.mem.eql(u8, self.entries[index].name, head)) return &self.entries[index];
184
+ }
185
+ return null;
186
+ }
187
+
188
+ /// The innermost slot capture visible to the current template
189
+ /// body (never looks below the floor: an inner template with
190
+ /// no use-site children must not see an outer capture).
191
+ fn slotCapture(self: *const Scope) ?SlotCapture {
192
+ var index = self.len;
193
+ while (index > self.floor) {
194
+ index -= 1;
195
+ if (self.entries[index].payload == .slot) return self.entries[index].payload.slot;
196
+ }
197
+ return null;
198
+ }
199
+
200
+ fn slotCaptureOwned(self: *const Scope, owner: markup.expr.Policy) ?SlotCapture {
201
+ var request = value_programs.Request.init(32);
202
+ defer request.deinit();
203
+ request.words(&.{ self.len, self.floor });
204
+ for (self.entries[0..self.len]) |entry| request.word(@intFromBool(entry.payload == .slot));
205
+ const row = request.decision(owner);
206
+ if (row.action != 87 or row.fields[0] > self.len) @panic("invalid lexical capture capability");
207
+ return if (row.fields[0] == 0) null else self.entries[row.fields[0] - 1].payload.slot;
208
+ }
209
+ };
210
+
211
+ fn resourceAttrOwned(self: *Self, owner: markup.expr.Policy, scope: *Scope, node: markup.MarkupNode, options: *Ui.ElementOptions, attribute: markup.MarkupAttr, comptime channel: u8) BuildError!void {
212
+ const typed = markup.attrTypedWithPolicy(owner, attribute);
213
+ var request = value_programs.Request.init(0);
214
+ defer request.deinit();
215
+ request.word(channel);
216
+ request.blob(node.name);
217
+ request.word(@intFromBool(typed == .binding));
218
+ const admission = request.decision(owner);
219
+ if (admission.action == 255) return self.failVoid(node, value_programs.message(admission.code));
220
+ if (admission.action != 1) @panic("invalid resource admission");
221
+ const value = try self.evalBinding(scope, node, typed.binding, true);
222
+ const action = value_programs.decide(owner, 1, &.{ channel, value_programs.kind(value), @intFromBool(value == .integer and value.integer >= 0) });
223
+ switch (action.action) {
224
+ 2 => if (channel == 2) {
225
+ options.pty = @intCast(value.integer);
226
+ } else {
227
+ options.image = @intCast(value.integer);
228
+ },
229
+ 3 => if (channel == 2) {
230
+ options.pty_name = value.string;
231
+ } else unreachable,
232
+ 255 => return self.failVoid(node, value_programs.message(action.code)),
233
+ else => @panic("invalid resource publication"),
234
+ }
235
+ }
236
+
237
+ fn elementAdmissionOwned(self: *Self, owner: markup.expr.Policy, node: markup.MarkupNode, family: u8) BuildError!void {
238
+ var request = value_programs.Request.init(2);
239
+ defer request.deinit();
240
+ request.word(family);
241
+ request.blob(node.name);
242
+ switch (family) {
243
+ 0 => request.names(&markup.known_icon_attr_element_names),
244
+ 1 => request.names(&markup.known_anchor_element_names),
245
+ 2 => {
246
+ comptime var count = 0;
247
+ inline for (markup.schema.elements) |entry| {
248
+ if (entry.rule_hook == null and entry.hit_target) count += 1;
249
+ }
250
+ request.word(count);
251
+ inline for (markup.schema.elements) |entry| {
252
+ if (comptime entry.rule_hook == null and entry.hit_target) request.blob(entry.name);
253
+ }
254
+ },
255
+ else => @panic("invalid element family"),
256
+ }
257
+ const action = request.decision(owner);
258
+ if (action.action == 255) return self.failVoid(node, value_programs.message(action.code));
259
+ if (action.action != 1) @panic("invalid value element admission");
260
+ }
261
+
262
+ fn applyButtonIconAttrOwned(self: *Self, owner: markup.expr.Policy, scope: *Scope, node: markup.MarkupNode, options: *Ui.ElementOptions, attribute: markup.MarkupAttr) BuildError!void {
263
+ try self.elementAdmissionOwned(owner, node, 0);
264
+ switch (value_programs.iconValue(owner, attribute.value, markup.button_icon_message)) {
265
+ .builtin, .app => |name| options.icon = name,
266
+ .binding => options.icon = try self.stringAttr(scope, node, attribute, markup.button_icon_message),
267
+ .invalid => |message| return self.failVoid(node, message),
268
+ }
269
+ }
270
+
271
+ fn applyAnchorOwned(self: *Self, owner: markup.expr.Policy, node: markup.MarkupNode, options: *Ui.ElementOptions, raw: []const u8, comptime kind: u8) BuildError!void {
272
+ try self.elementAdmissionOwned(owner, node, 1);
273
+ const admission = value_programs.decide(owner, 3, &.{ kind, 0, @intFromBool(node.attr("anchor") != null), 1 });
274
+ if (admission.action == 255) return self.failVoid(node, value_programs.message(admission.code));
275
+ if (admission.action != 1) @panic("invalid anchor admission");
276
+ const T = if (kind == 0) canvas.WidgetAnchorPlacement else if (kind == 1) canvas.WidgetAnchorAlignment else f32;
277
+ const converted: ?T = if (kind == 2) std.fmt.parseFloat(f32, raw) catch null else enumValueOwned(T, owner, raw);
278
+ const action = value_programs.decide(owner, 3, &.{ kind, 1, 1, @intFromBool(converted != null) });
279
+ if (action.action == 255) return self.failVoid(node, value_programs.message(action.code));
280
+ if (action.action != 1) @panic("invalid anchor conversion");
281
+ if (kind == 0) options.anchor = converted.? else if (kind == 1) options.anchor_alignment = converted.? else options.anchor_offset = converted.?;
282
+ }
283
+
284
+ fn applyQuietHoverAttrOwned(self: *Self, owner: markup.expr.Policy, scope: *Scope, node: markup.MarkupNode, options: *Ui.ElementOptions, attribute: markup.MarkupAttr) BuildError!void {
285
+ try self.elementAdmissionOwned(owner, node, 2);
286
+ options.style.quiet_hover = markup.expr.truthyWithPolicy(owner, try self.evalAttrExpression(scope, node, attribute));
287
+ }
288
+
289
+ fn setOptionFieldOwned(self: *Self, owner: markup.expr.Policy, scope: *Scope, node: markup.MarkupNode, options: *Ui.ElementOptions, comptime field: []const u8, attribute: markup.MarkupAttr) BuildError!void {
290
+ const T = @TypeOf(@field(options, field));
291
+ const optional = @typeInfo(T) == .optional;
292
+ const Target = if (optional) @typeInfo(T).optional.child else T;
293
+ const class = comptime if (optional and @typeInfo(Target) != .bool and @typeInfo(Target) != .@"enum" and @typeInfo(Target) != .float) 0 else value_programs.typeClass(Target);
294
+ const value = try self.evalAttrExpression(scope, node, attribute);
295
+ const within = if (comptime class == 4) value == .integer and value.integer >= 0 and value.integer <= std.math.maxInt(Target) else false;
296
+ const action = value_programs.decide(owner, 4, &.{ class, value_programs.kind(value), @intFromBool(within) });
297
+ if (action.action == 255) return self.failVoid(node, value_programs.message(action.code));
298
+ if (action.action != class) @panic("invalid option conversion capability");
299
+ switch (comptime class) {
300
+ 1 => @field(options, field) = switch (value) {
301
+ .float => |v| v,
302
+ .integer => |v| @floatFromInt(v),
303
+ else => unreachable,
304
+ },
305
+ 2 => @field(options, field) = markup.expr.truthyWithPolicy(owner, value),
306
+ 3 => @field(options, field) = enumValueOwned(Target, owner, value.string) orelse return self.failVoid(node, value_programs.message(22)),
307
+ 4 => @field(options, field) = @intCast(value.integer),
308
+ 5 => @field(options, field) = value.string,
309
+ else => unreachable,
310
+ }
311
+ }
312
+
313
+ fn constructMessageOwned(self: *Self, owner: markup.expr.Policy, scope: *Scope, node: markup.MarkupNode, expression: markup.MessageExpression, comptime drag: bool) BuildError!MsgT {
314
+ @setEvalBranchQuota(scan_quota);
315
+ var request = value_programs.Request.init(6);
316
+ defer request.deinit();
317
+ request.word(@intFromBool(drag));
318
+ request.blob(expression.tag);
319
+ request.word(@intFromBool(expression.payload.len != 0));
320
+ const fields = @typeInfo(MsgT).@"union".fields;
321
+ request.word(fields.len);
322
+ inline for (fields) |field| request.blob(field.name);
323
+ inline for (fields) |field| request.word(if (field.type == void) 0 else if (comptime reflect.declaredWidgetDragDropRecord(field.type)) 2 else 1);
324
+ const action = request.decision(owner);
325
+ if (action.action == 255) return self.failMsg(node, value_programs.message(action.code));
326
+ inline for (fields, 0..) |field, i| if (action.fields[0] == i) {
327
+ if (comptime drag) {
328
+ if (comptime reflect.declaredWidgetDragDropRecord(field.type)) {
329
+ if (action.action != 42) @panic("invalid drag capability");
330
+ const value = try self.evalBinding(scope, node, expression.payload, true);
331
+ var payload = std.mem.zeroes(field.type);
332
+ payload.sourceId = try self.coerce(scope, @FieldType(field.type, "sourceId"), node, value);
333
+ return @unionInit(MsgT, field.name, payload);
334
+ }
335
+ } else if (comptime field.type == void) {
336
+ if (action.action != 40) @panic("invalid void message capability");
337
+ return @unionInit(MsgT, field.name, {});
338
+ } else {
339
+ if (action.action != 41) @panic("invalid payload message capability");
340
+ const value = try self.evalBinding(scope, node, expression.payload, true);
341
+ return @unionInit(MsgT, field.name, try self.coerce(scope, field.type, node, value));
342
+ }
343
+ @panic("invalid typed message selection");
344
+ };
345
+ @panic("invalid message field index");
346
+ }
347
+
348
+ fn coerceOwned(self: *Self, owner: markup.expr.Policy, scope: *Scope, comptime T: type, node: markup.MarkupNode, value: Value) BuildError!T {
349
+ _ = scope;
350
+ const class = comptime value_programs.typeClass(T);
351
+ const within = if (comptime class == 4) value == .integer and value.integer >= std.math.minInt(T) and value.integer <= std.math.maxInt(T) else false;
352
+ const action = value_programs.decide(owner, 5, &.{ class, value_programs.kind(value), @intFromBool(within) });
353
+ if (action.action == 255) return self.failCoerce(T, node);
354
+ if (action.action != class) @panic("invalid payload conversion capability");
355
+ return switch (comptime class) {
356
+ 1 => switch (value) {
357
+ .float => |v| @floatCast(v),
358
+ .integer => |v| @floatFromInt(v),
359
+ else => unreachable,
360
+ },
361
+ 2 => markup.expr.truthyWithPolicy(owner, value),
362
+ 3 => enumValueOwned(T, owner, value.string) orelse self.failCoerce(T, node),
363
+ 4 => @intCast(value.integer),
364
+ 5 => value.string,
365
+ else => unreachable,
366
+ };
367
+ }
368
+
369
+ fn callbackClass(comptime T: type, comptime family: u8) usize {
370
+ return switch (family) {
371
+ 0 => if (T == canvas.TextInputEvent) 1 else if (reflect.declaredTextInputUnion(T)) 2 else 0,
372
+ 1 => if (T == canvas.ScrollState) 1 else if (reflect.declaredScrollStateRecord(T)) 2 else 0,
373
+ 2 => if (T == canvas.TerminalState) 1 else if (reflect.declaredTerminalStateRecord(T)) 2 else 0,
374
+ 3 => if (reflect.declaredLegacyScrollStateRecord(T)) 1 else 0,
375
+ 4 => if (T == f32) 1 else if (T == f64) 2 else 0,
376
+ 5 => if (reflect.valueArmClass(T)) |class| if (class == .identity) 1 else 2 else 0,
377
+ else => unreachable,
378
+ };
379
+ }
380
+
381
+ fn callbackSelection(owner: markup.expr.Policy, tag: []const u8, comptime family: u8) elements.Record {
382
+ @setEvalBranchQuota(scan_quota);
383
+ var request = value_programs.Request.init(7);
384
+ defer request.deinit();
385
+ request.blob(tag);
386
+ const fields = @typeInfo(MsgT).@"union".fields;
387
+ request.word(fields.len);
388
+ inline for (fields) |field| request.blob(field.name);
389
+ inline for (fields) |field| request.word(comptime callbackClass(field.type, family));
390
+ return request.decision(owner);
391
+ }
392
+
393
+ fn evalAttributeExpressionOwned(self: *Self, owner: markup.expr.Policy, scope: *Scope, node: markup.MarkupNode, attribute: markup.MarkupAttr, attribute_aware: bool) BuildError!Value {
394
+ const typed = markup.attrTypedWithPolicy(owner, attribute);
395
+ const kind: usize = switch (typed) {
396
+ .literal => 0,
397
+ .binding => 1,
398
+ .equals => 2,
399
+ .expression => 3,
400
+ .message => 4,
401
+ .invalid => 5,
402
+ };
403
+ var request = value_programs.Request.init(8);
404
+ defer request.deinit();
405
+ request.words(&.{ kind, @intFromBool(attribute_aware) });
406
+ var program = try request.program(owner);
407
+ defer program.deinit();
408
+ var left: Value = undefined;
409
+ var right: Value = undefined;
410
+ while (program.next()) |step| switch (step.action) {
411
+ 50 => return value_programs.literal(owner, typed.literal, attribute.name),
412
+ 51 => return value_programs.literal(owner, typed.literal, null),
413
+ 52 => return self.evalBinding(scope, node, typed.binding, true),
414
+ 53 => left = try self.evalBinding(scope, node, typed.equals.left, false),
415
+ 54 => right = try self.evalBinding(scope, node, typed.equals.right, false),
416
+ 55 => return .{ .boolean = markup.expr.equalWithPolicy(owner, left, right) },
417
+ 56 => return self.evalExpressionForm(scope, node, typed.expression),
418
+ 255 => return self.failValue(node, value_programs.message(step.code)),
419
+ else => @panic("invalid attribute evaluation capability"),
420
+ };
421
+ @panic("attribute program did not return");
422
+ }
423
+
424
+ fn evalExpressionFormOwned(self: *Self, owner: markup.expr.Policy, scope: *Scope, node: markup.MarkupNode, form: markup.TypedExprRef) BuildError!Value {
425
+ const action = value_programs.decide(owner, 9, &.{@intFromBool(form.tree != null)});
426
+ return switch (action.action) {
427
+ 57 => self.evalParsedTree(scope, node, form.tree.?),
428
+ 58 => self.evalExpressionTree(scope, node, form.inner),
429
+ else => @panic("invalid expression form capability"),
430
+ };
431
+ }
432
+
433
+ fn evalParsedTreeOwned(self: *Self, owner: markup.expr.Policy, scope: *Scope, node: markup.MarkupNode, tree: *const markup.expr.ExprTree) BuildError!Value {
434
+ var request = value_programs.Request.init(10);
435
+ defer request.deinit();
436
+ request.word(tree.len);
437
+ for (tree.nodes[0..tree.len]) |entry| request.words(&.{ @intFromBool(entry.kind == .binding), @intFromBool(entry.comparison_operand) });
438
+ var program = try request.program(owner);
439
+ defer program.deinit();
440
+ var resolved: [markup.expr.max_expression_nodes]Value = undefined;
441
+ while (program.next()) |step| switch (step.action) {
442
+ 59 => resolved[step.fields[0]] = try self.evalBinding(scope, node, tree.nodes[step.fields[0]].text, step.fields[1] != 0),
443
+ 60 => return switch (try markup.expr.evalWithPolicy(owner, tree, &resolved, scope.arena)) {
444
+ .value => |value| value,
445
+ .fail => |message| self.failValue(node, message),
446
+ },
447
+ else => @panic("invalid binding evaluation capability"),
448
+ };
449
+ @panic("expression program did not return");
450
+ }
451
+
452
+ fn bindingResultOwned(self: *Self, owner: markup.expr.Policy, scope: *Scope, node: markup.MarkupNode, comptime T: type, ptr: *const T, path: []const u8, allow_arena: bool, domain: usize, head: []const u8) BuildError!Value {
453
+ const value = resolveOnOwned(T, owner, ptr, path, if (allow_arena) scope.arena else null);
454
+ var action = value_programs.decide(owner, 12, &.{ domain, 0, @intFromBool(value != null), @intFromBool(allow_arena) });
455
+ if (action.action == 67) {
456
+ const retry = resolveOnOwned(T, owner, ptr, path, scope.arena);
457
+ action = value_programs.decide(owner, 12, &.{ domain, 1, @intFromBool(retry != null), @intFromBool(allow_arena) });
458
+ }
459
+ if (action.action == 75) {
460
+ const buffer = if (comptime T == ModelT) fieldIsTextBuffer(ModelT, head) else false;
461
+ action = value_programs.decide(owner, 12, &.{ domain, 2, @intFromBool(buffer), @intFromBool(allow_arena) });
462
+ }
463
+ return switch (action.action) {
464
+ 66 => value.?,
465
+ 255 => self.failValue(node, value_programs.message(action.code)),
466
+ else => @panic("invalid binding result capability"),
467
+ };
468
+ }
469
+
470
+ fn evalBindingOwned(self: *Self, owner: markup.expr.Policy, scope: *Scope, node: markup.MarkupNode, path: []const u8, allow_arena: bool) BuildError!Value {
471
+ @setEvalBranchQuota(scan_quota);
472
+ var request = value_programs.Request.init(11);
473
+ defer request.deinit();
474
+ request.blob(path);
475
+ request.word(scope.floor);
476
+ request.word(scope.len);
477
+ for (scope.entries[0..scope.len]) |entry| request.blob(entry.name);
478
+ for (scope.entries[0..scope.len]) |entry| request.word(@intFromEnum(std.meta.activeTag(entry.payload)));
479
+ const action = request.decision(owner);
480
+ switch (action.action) {
481
+ 61, 62 => {
482
+ const entry = scope.entries[action.fields[0]].payload.item;
483
+ inline for (item_types, 0..) |Item, index| if (entry.type_index == index) {
484
+ const ptr: *const Item = @ptrCast(@alignCast(entry.ptr));
485
+ if (action.action == 61) return self.bindingResultOwned(owner, scope, node, Item, ptr, path[action.fields[1]..], allow_arena, 0, "");
486
+ const value = valueOf(Item, ptr.*);
487
+ const result = value_programs.decide(owner, 12, &.{ 3, 0, @intFromBool(value != null), @intFromBool(allow_arena) });
488
+ return switch (result.action) {
489
+ 66 => value.?,
490
+ 255 => self.failValue(node, value_programs.message(result.code)),
491
+ else => @panic("invalid item value capability"),
492
+ };
493
+ };
494
+ unreachable;
495
+ },
496
+ 63 => return scope.entries[action.fields[0]].payload.value,
497
+ 64 => {
498
+ const window = scope.entries[action.fields[0]].payload.virtual;
499
+ const value = resolveOnOwned(canvas.VirtualListRange, owner, &window.range, path[action.fields[1]..], null);
500
+ const result = value_programs.decide(owner, 12, &.{ 2, 0, @intFromBool(value != null), 1 });
501
+ return switch (result.action) {
502
+ 66 => value.?,
503
+ 255 => self.failValue(node, value_programs.message(result.code)),
504
+ else => @panic("invalid virtual value capability"),
505
+ };
506
+ },
507
+ 65 => return self.bindingResultOwned(owner, scope, node, ModelT, scope.model, path, allow_arena, 1, path[0..action.fields[0]]),
508
+ 255 => return self.failValue(node, value_programs.message(action.code)),
509
+ else => @panic("invalid scope binding capability"),
510
+ }
511
+ }
512
+
513
+ fn inputConstructorOwned(owner: markup.expr.Policy, tag: []const u8) ?Ui.InputMsgFn {
514
+ @setEvalBranchQuota(scan_quota);
515
+ const action = callbackSelection(owner, tag, 0);
516
+ if (action.action == 0) return null;
517
+ if (action.action != 43) @panic("invalid callback selection");
518
+ inline for (@typeInfo(MsgT).@"union".fields, 0..) |field, i| if (action.fields[0] == i) {
519
+ const class = comptime callbackClass(field.type, 0);
520
+ if (action.fields[1] != class) @panic("invalid callback class");
521
+ if (comptime class == 1) return Ui.inputMsg(@field(std.meta.Tag(MsgT), field.name));
522
+ if (comptime class == 2) return Ui.translatedInputMsg(@field(std.meta.Tag(MsgT), field.name), field.type);
523
+ @panic("unavailable callback capability");
524
+ };
525
+ @panic("invalid callback field index");
526
+ }
527
+ fn scrollConstructorOwned(owner: markup.expr.Policy, tag: []const u8) ?Ui.ScrollMsgFn {
528
+ @setEvalBranchQuota(scan_quota);
529
+ const action = callbackSelection(owner, tag, 1);
530
+ if (action.action == 0) return null;
531
+ if (action.action != 43) @panic("invalid callback selection");
532
+ inline for (@typeInfo(MsgT).@"union".fields, 0..) |field, i| if (action.fields[0] == i) {
533
+ const class = comptime callbackClass(field.type, 1);
534
+ if (action.fields[1] != class) @panic("invalid callback class");
535
+ if (comptime class == 1) return Ui.scrollMsg(@field(std.meta.Tag(MsgT), field.name));
536
+ if (comptime class == 2) return Ui.translatedScrollMsg(@field(std.meta.Tag(MsgT), field.name), field.type);
537
+ @panic("unavailable callback capability");
538
+ };
539
+ @panic("invalid callback field index");
540
+ }
541
+ fn terminalConstructorOwned(owner: markup.expr.Policy, tag: []const u8) ?Ui.TerminalMsgFn {
542
+ @setEvalBranchQuota(scan_quota);
543
+ const action = callbackSelection(owner, tag, 2);
544
+ if (action.action == 0) return null;
545
+ if (action.action != 43) @panic("invalid callback selection");
546
+ inline for (@typeInfo(MsgT).@"union".fields, 0..) |field, i| if (action.fields[0] == i) {
547
+ const class = comptime callbackClass(field.type, 2);
548
+ if (action.fields[1] != class) @panic("invalid callback class");
549
+ if (comptime class == 1) return Ui.terminalMsg(@field(std.meta.Tag(MsgT), field.name));
550
+ if (comptime class == 2) return Ui.translatedTerminalMsg(@field(std.meta.Tag(MsgT), field.name), field.type);
551
+ @panic("unavailable callback capability");
552
+ };
553
+ @panic("invalid callback field index");
554
+ }
555
+ fn resizeConstructorOwned(owner: markup.expr.Policy, tag: []const u8) ?Ui.ValueMsgFn {
556
+ @setEvalBranchQuota(scan_quota);
557
+ const action = callbackSelection(owner, tag, 4);
558
+ if (action.action == 0) return null;
559
+ if (action.action != 43) @panic("invalid callback selection");
560
+ inline for (@typeInfo(MsgT).@"union".fields, 0..) |field, i| if (action.fields[0] == i) {
561
+ const class = comptime callbackClass(field.type, 4);
562
+ if (action.fields[1] != class) @panic("invalid callback class");
563
+ if (comptime class == 1) return Ui.valueMsg(@field(std.meta.Tag(MsgT), field.name));
564
+ if (comptime class == 2) return Ui.translatedValueMsg(@field(std.meta.Tag(MsgT), field.name), field.type);
565
+ @panic("unavailable callback capability");
566
+ };
567
+ @panic("invalid callback field index");
568
+ }
569
+ fn valueConstructorOwned(owner: markup.expr.Policy, tag: []const u8) ?Ui.ValueMsgFn {
570
+ @setEvalBranchQuota(scan_quota);
571
+ const action = callbackSelection(owner, tag, 5);
572
+ if (action.action == 0) return null;
573
+ if (action.action != 43) @panic("invalid callback selection");
574
+ inline for (@typeInfo(MsgT).@"union".fields, 0..) |field, i| if (action.fields[0] == i) {
575
+ const class = comptime callbackClass(field.type, 5);
576
+ if (action.fields[1] != class) @panic("invalid callback class");
577
+ if (comptime class == 1) return Ui.valueMsg(@field(std.meta.Tag(MsgT), field.name));
578
+ if (comptime class == 2) return Ui.translatedValueMsg(@field(std.meta.Tag(MsgT), field.name), field.type);
579
+ @panic("unavailable callback capability");
580
+ };
581
+ @panic("invalid callback field index");
582
+ }
583
+ fn legacyScrollTagOwned(owner: markup.expr.Policy, tag: []const u8) bool {
584
+ const action = callbackSelection(owner, tag, 3);
585
+ return switch (action.action) {
586
+ 0 => false,
587
+ 43 => true,
588
+ else => @panic("invalid legacy callback selection"),
589
+ };
590
+ }
591
+
592
+ fn iterateModelItemsOwned(self: *Self, owner: markup.expr.Policy, ui: *Ui, comptime Item: type, scope: *Scope, each: []const u8) BuildError!?[]const Item {
593
+ @setEvalBranchQuota(scan_quota);
594
+ _ = self;
595
+ const fields = @typeInfo(ModelT).@"struct".fields;
596
+ const decls = @typeInfo(ModelT).@"struct".decls;
597
+ var request = runtime_programs.Request.init(0);
598
+ defer request.deinit();
599
+ request.blob(each);
600
+ request.word(fields.len);
601
+ inline for (fields) |field| request.blob(field.name);
602
+ inline for (fields) |field| request.word(comptime @intFromBool(sliceElement(field.type) != null and sliceElement(field.type).? == Item));
603
+ request.word(decls.len);
604
+ inline for (decls) |decl| request.blob(decl.name);
605
+ inline for (decls) |decl| request.word(comptime collectionDeclClass(@TypeOf(@field(ModelT, decl.name)), Item));
606
+ const action = request.decision(owner);
607
+ if (action.action == 0) return null;
608
+ if (action.action == 1) {
609
+ inline for (fields, 0..) |field, i| if (action.fields[0] == i) {
610
+ if (comptime sliceElement(field.type) != null and sliceElement(field.type).? == Item) return asSlice(Item, &@field(scope.model, field.name));
611
+ @panic("unavailable collection field");
612
+ };
613
+ } else if (action.action == 2) {
614
+ inline for (decls, 0..) |decl, i| if (action.fields[0] == i) {
615
+ const class = comptime collectionDeclClass(@TypeOf(@field(ModelT, decl.name)), Item);
616
+ if (action.fields[1] != class) @panic("invalid collection callback class");
617
+ if (comptime class == 1) return asSlice(Item, &@field(ModelT, decl.name));
618
+ if (comptime class == 2) return @field(ModelT, decl.name)(scope.model);
619
+ if (comptime class == 3) return @field(ModelT, decl.name)(scope.model, ui.arena);
620
+ @panic("unavailable collection declaration");
621
+ };
622
+ }
623
+ @panic("invalid collection capability");
624
+ }
625
+
626
+ fn collectionDeclClass(comptime Decl: type, comptime Item: type) usize {
627
+ if (comptime sliceElement(Decl) != null and sliceElement(Decl).? == Item) return 1;
628
+ if (comptime isItemFn(Decl, Item, false)) return 2;
629
+ if (comptime isItemFn(Decl, Item, true)) return 3;
630
+ return 0;
631
+ }
632
+
633
+ fn itemKeyOwned(self: *Self, owner: markup.expr.Policy, comptime Item: type, item: *const Item, node: markup.MarkupNode, field: []const u8) BuildError!canvas.UiKey {
634
+ const value = resolveOnOwned(Item, owner, item, field, null);
635
+ const action = runtime_programs.decide(owner, 1, &.{ @intFromBool(value != null), if (value) |v| value_programs.kind(v) else 0 });
636
+ return switch (action.action) {
637
+ 1 => canvas.uiKey(@as(u64, @bitCast(value.?.integer))),
638
+ 2 => canvas.uiKey(value.?.string),
639
+ 255 => self.failKey(node, runtime_programs.message(action.code)),
640
+ else => @panic("invalid item key capability"),
641
+ };
642
+ }
643
+
644
+ fn applyImageSourceAttrsOwned(self: *Self, owner: markup.expr.Policy, scope: *Scope, node: markup.MarkupNode, options: *Ui.ElementOptions) BuildError!void {
645
+ var request = runtime_programs.Request.init(2);
646
+ defer request.deinit();
647
+ request.node(node, false);
648
+ var program = try request.program(owner);
649
+ defer program.deinit();
650
+ while (program.next()) |action| switch (action.action) {
651
+ // Preserve native result-location writes, including the optional
652
+ // tag and fields initialized before a later expression fails.
653
+ 20 => options.image_src = .{
654
+ .x = try self.imageCoordinateOwned(&program, scope, node, 0),
655
+ .y = try self.imageCoordinateOwned(&program, scope, node, 1),
656
+ .width = try self.imageCoordinateOwned(&program, scope, node, 2),
657
+ .height = try self.imageCoordinateOwned(&program, scope, node, 3),
658
+ },
659
+ 22 => {},
660
+ 255 => return self.failVoid(node, runtime_programs.message(action.code)),
661
+ else => @panic("invalid image rectangle capability"),
662
+ };
663
+ }
664
+
665
+ fn imageCoordinateOwned(self: *Self, program: *@import("ui_markup_elements.zig").Program, scope: *Scope, node: markup.MarkupNode, field: usize) BuildError!f32 {
666
+ const action = program.next() orelse @panic("missing image coordinate");
667
+ if (action.action != 21 or action.fields[1] != field) @panic("invalid image coordinate storage");
668
+ return self.floatAttr(scope, node, node.attrs[action.fields[0]]);
669
+ }
670
+
671
+ fn stringAttrOwned(self: *Self, owner: markup.expr.Policy, scope: *Scope, node: markup.MarkupNode, attribute: markup.MarkupAttr, message: []const u8) BuildError![]const u8 {
672
+ const value = try self.evalAttrExpression(scope, node, attribute);
673
+ const action = runtime_programs.decide(owner, 3, &.{ 0, value_programs.kind(value) });
674
+ return switch (action.action) {
675
+ 1 => value.string,
676
+ 255 => self.failValue(node, message),
677
+ else => @panic("invalid text admission"),
678
+ };
679
+ }
680
+
681
+ fn floatAttrOwned(self: *Self, owner: markup.expr.Policy, scope: *Scope, node: markup.MarkupNode, attribute: markup.MarkupAttr) BuildError!f32 {
682
+ const value = try self.evalAttrExpression(scope, node, attribute);
683
+ const action = runtime_programs.decide(owner, 3, &.{ 1, value_programs.kind(value) });
684
+ return switch (action.action) {
685
+ 2 => value.float,
686
+ 3 => @floatFromInt(value.integer),
687
+ 255 => self.failValue(node, runtime_programs.message(action.code)),
688
+ else => @panic("invalid number admission"),
689
+ };
690
+ }
691
+
692
+ fn richConstructorSelection(owner: markup.expr.Policy, tag: []const u8, comptime details: bool) @import("ui_markup_elements.zig").Record {
693
+ var request = runtime_programs.Request.init(4);
694
+ defer request.deinit();
695
+ request.blob(tag);
696
+ const fields = @typeInfo(MsgT).@"union".fields;
697
+ request.word(fields.len);
698
+ inline for (fields) |field| request.blob(field.name);
699
+ inline for (fields) |field| request.word(comptime @intFromBool(if (details) field.type == usize or field.type == i64 else field.type == []const u8));
700
+ return request.decision(owner);
701
+ }
702
+
703
+ fn linkConstructorOwned(owner: markup.expr.Policy, tag: []const u8) ?Ui.LinkMsgFn {
704
+ @setEvalBranchQuota(scan_quota);
705
+ const action = richConstructorSelection(owner, tag, false);
706
+ if (action.action == 0) return null;
707
+ if (action.action != 1) @panic("invalid link constructor selection");
708
+ inline for (@typeInfo(MsgT).@"union".fields, 0..) |field, i| if (action.fields[0] == i) {
709
+ if (comptime field.type == []const u8) return Ui.linkMsg(@field(std.meta.Tag(MsgT), field.name));
710
+ @panic("unavailable link constructor");
711
+ };
712
+ @panic("invalid link constructor index");
713
+ }
714
+
715
+ fn detailsConstructorOwned(owner: markup.expr.Policy, tag: []const u8) ?*const fn (index: usize) MsgT {
716
+ @setEvalBranchQuota(scan_quota);
717
+ const action = richConstructorSelection(owner, tag, true);
718
+ if (action.action == 0) return null;
719
+ if (action.action != 1) @panic("invalid details constructor selection");
720
+ inline for (@typeInfo(MsgT).@"union".fields, 0..) |field, i| if (action.fields[0] == i) {
721
+ if (comptime field.type == usize or field.type == i64) return Md.detailsMsg(@field(std.meta.Tag(MsgT), field.name));
722
+ @panic("unavailable details constructor");
723
+ };
724
+ @panic("invalid details constructor index");
725
+ }
726
+
727
+ fn interpolatedTextOwned(self: *Self, owner: markup.expr.Policy, ui: *Ui, scope: *Scope, node: markup.MarkupNode) BuildError![]const u8 {
728
+ var request = runtime_programs.Request.init(5);
729
+ defer request.deinit();
730
+ request.node(node, true);
731
+ const action = request.decision(owner);
732
+ return switch (action.action) {
733
+ 10 => "",
734
+ 11 => self.runText(ui, scope, node, node.children[0]),
735
+ 255 => self.failText(if (action.fields[0] == 0) node else node.children[action.fields[0] - 1], runtime_programs.message(action.code)),
736
+ else => @panic("invalid text run admission"),
737
+ };
738
+ }
739
+
740
+ fn runTextOwned(self: *Self, owner: markup.expr.Policy, ui: *Ui, scope: *Scope, node: markup.MarkupNode, run: markup.MarkupNode) BuildError![]const u8 {
741
+ var request = runtime_programs.Request.init(6);
742
+ defer request.deinit();
743
+ request.blob(run.text);
744
+ request.word(@intFromBool(run.typed_text != null));
745
+ request.word(if (run.typed_text) |segments| segments.len else 0);
746
+ if (run.typed_text) |segments| for (segments) |segment| request.word(switch (segment) {
747
+ .literal => 0,
748
+ .binding => 1,
749
+ .expression => 2,
750
+ .unterminated => 3,
751
+ });
752
+ var program = try request.program(owner);
753
+ defer program.deinit();
754
+ var out: std.ArrayListUnmanaged(u8) = .empty;
755
+ while (program.next()) |action| switch (action.action) {
756
+ 40 => return run.text,
757
+ 41 => try out.appendSlice(ui.arena, run.typed_text.?[action.fields[0]].literal),
758
+ 42 => try markup.expr.appendValueWithPolicy(scope.expression_policy, &out, ui.arena, try self.evalBinding(scope, node, run.typed_text.?[action.fields[0]].binding, true)),
759
+ 43 => try markup.expr.appendValueWithPolicy(scope.expression_policy, &out, ui.arena, try self.evalExpressionForm(scope, node, run.typed_text.?[action.fields[0]].expression)),
760
+ 44 => try out.appendSlice(ui.arena, run.text[action.fields[0]..action.fields[1]]),
761
+ 45 => try markup.expr.appendValueWithPolicy(scope.expression_policy, &out, ui.arena, try self.evalBinding(scope, node, run.text[action.fields[0]..action.fields[1]], true)),
762
+ 46 => try markup.expr.appendValueWithPolicy(scope.expression_policy, &out, ui.arena, try self.evalExpressionTree(scope, node, run.text[action.fields[0]..action.fields[1]])),
763
+ 255 => return self.failText(node, runtime_programs.message(action.code)),
764
+ else => @panic("invalid text interpolation capability"),
765
+ };
766
+ return out.items;
767
+ }
768
+
769
+ fn collectionProgram(owner: markup.expr.Policy, comptime channel: u8) error{OutOfMemory}!@import("ui_markup_elements.zig").Program {
770
+ var request = runtime_programs.Request.init(7);
771
+ defer request.deinit();
772
+ request.words(&.{ channel, item_types.len });
773
+ inline for (item_types) |Item| request.word(comptime collectionItemClass(Item));
774
+ return request.program(owner);
775
+ }
776
+
777
+ fn collectionItemClass(comptime Item: type) usize {
778
+ if (Item == f32) return 1;
779
+ if (Item == f64) return 2;
780
+ if (Item == []const u8) return 3;
781
+ if (Item == bool) return 4;
782
+ if (reflect.isMarkdownImageItem(Item)) return 5;
783
+ return 0;
784
+ }
785
+
786
+ fn f32ItemsOwned(self: *Self, owner: markup.expr.Policy, ui: *Ui, scope: *Scope, node: markup.MarkupNode, path: []const u8) BuildError![]const f32 {
787
+ @setEvalBranchQuota(scan_quota);
788
+ var program = try collectionProgram(owner, 0);
789
+ defer program.deinit();
790
+ while (program.next()) |action| switch (action.action) {
791
+ 30 => inline for (item_types, 0..) |Item, i| {
792
+ if (comptime Item == f32 or Item == f64) {
793
+ if (action.fields[0] == i) {
794
+ if (action.fields[1] != comptime collectionItemClass(Item)) @panic("invalid collection item class");
795
+ if (try self.iterateItems(ui, Item, i, scope, path)) |items| {
796
+ if (comptime Item == f32) return items;
797
+ const narrowed = ui.arena.alloc(f32, items.len) catch return self.failText(node, markup.series_values_message);
798
+ for (narrowed, items) |*slot, value| slot.* = @floatCast(value);
799
+ return narrowed;
800
+ }
801
+ }
802
+ }
803
+ },
804
+ 31 => return self.failText(node, markup.series_values_message),
805
+ else => @panic("invalid collection selection capability"),
806
+ };
807
+ @panic("missing collection terminal");
808
+ }
809
+
810
+ fn stringItemsOwned(self: *Self, owner: markup.expr.Policy, ui: *Ui, scope: *Scope, node: markup.MarkupNode, path: []const u8, message: []const u8) BuildError![]const []const u8 {
811
+ @setEvalBranchQuota(scan_quota);
812
+ var program = try collectionProgram(owner, 1);
813
+ defer program.deinit();
814
+ while (program.next()) |action| switch (action.action) {
815
+ 30 => inline for (item_types, 0..) |Item, i| {
816
+ if (comptime Item == []const u8) {
817
+ if (action.fields[0] == i) {
818
+ if (action.fields[1] != comptime collectionItemClass(Item)) @panic("invalid collection item class");
819
+ if (try self.iterateItems(ui, Item, i, scope, path)) |items| {
820
+ return items;
821
+ }
822
+ }
823
+ }
824
+ },
825
+ 31 => return self.failText(node, message),
826
+ else => @panic("invalid collection selection capability"),
827
+ };
828
+ @panic("missing collection terminal");
829
+ }
830
+
831
+ fn boolItemsOwned(self: *Self, owner: markup.expr.Policy, ui: *Ui, scope: *Scope, node: markup.MarkupNode, path: []const u8) BuildError![]const bool {
832
+ @setEvalBranchQuota(scan_quota);
833
+ var program = try collectionProgram(owner, 2);
834
+ defer program.deinit();
835
+ while (program.next()) |action| switch (action.action) {
836
+ 30 => inline for (item_types, 0..) |Item, i| {
837
+ if (comptime Item == bool) {
838
+ if (action.fields[0] == i) {
839
+ if (action.fields[1] != comptime collectionItemClass(Item)) @panic("invalid collection item class");
840
+ if (try self.iterateItems(ui, Item, i, scope, path)) |items| {
841
+ return items;
842
+ }
843
+ }
844
+ }
845
+ },
846
+ 31 => return self.failText(node, markup.markdown_details_expanded_message),
847
+ else => @panic("invalid collection selection capability"),
848
+ };
849
+ @panic("missing collection terminal");
850
+ }
851
+
852
+ fn markdownImageItemsOwned(self: *Self, owner: markup.expr.Policy, ui: *Ui, scope: *Scope, node: markup.MarkupNode, path: []const u8) BuildError![]const canvas.markdown.ResolvedImage {
853
+ @setEvalBranchQuota(scan_quota);
854
+ var program = try collectionProgram(owner, 3);
855
+ defer program.deinit();
856
+ while (program.next()) |action| switch (action.action) {
857
+ 30 => inline for (item_types, 0..) |Item, i| {
858
+ if (comptime reflect.isMarkdownImageItem(Item)) {
859
+ if (action.fields[0] == i) {
860
+ if (action.fields[1] != comptime collectionItemClass(Item)) @panic("invalid collection item class");
861
+ if (try self.iterateItems(ui, Item, i, scope, path)) |items| {
862
+ return @import("markdown_image_binding.zig").convert(ui.arena, items) catch return self.failText(node, markup.markdown_images_message);
863
+ }
864
+ }
865
+ }
866
+ },
867
+ 31 => return self.failText(node, markup.markdown_images_message),
868
+ else => @panic("invalid collection selection capability"),
869
+ };
870
+ @panic("missing collection terminal");
871
+ }
872
+
873
+ fn buildOwned(self: *Self, owner: markup.expr.Policy, ui: *Ui, model: *const ModelT) BuildError!Ui.Node {
874
+ const action = construction.decide(owner, 0, &.{ self.document.imports.len, @intFromBool(self.document.root != null) });
875
+ if (action.action == 255) {
876
+ if (action.code == 1) return self.failNode(self.document.imports[0], construction.message(action.code));
877
+ if (action.code != 2) @panic("invalid root admission");
878
+ self.diagnostic = .{ .line = 1, .column = 1, .message = construction.message(action.code) };
879
+ return error.MarkupBuild;
880
+ }
881
+ if (action.action != 0) @panic("invalid root action");
882
+ var scope = Scope{ .model = model, .arena = ui.arena, .expression_policy = owner };
883
+ return self.buildNode(ui, &scope, self.document.root.?);
884
+ }
885
+ fn buildNodeOwned(self: *Self, owner: markup.expr.Policy, ui: *Ui, scope: *Scope, node: markup.MarkupNode) BuildError!Ui.Node {
886
+ const action = construction.decide(owner, 1, &.{@intFromEnum(node.kind)});
887
+ return switch (action.action) {
888
+ 1 => self.buildElement(ui, scope, node),
889
+ 2 => self.buildUse(ui, scope, node),
890
+ 255 => self.failNode(node, construction.message(action.code)),
891
+ else => @panic("invalid node construction action"),
892
+ };
893
+ }
894
+ fn buildChildListOwned(self: *Self, owner: markup.expr.Policy, ui: *Ui, scope: *Scope, children: []const markup.MarkupNode, out: *std.ArrayListUnmanaged(Ui.Node)) BuildError!void {
895
+ var index: usize = 0;
896
+ while (index < children.len) : (index += 1) {
897
+ const child = children[index];
898
+ const next: usize = if (index + 1 < children.len) @intFromEnum(children[index + 1].kind) else 9;
899
+ const action = construction.decide(owner, 2, &.{ @intFromEnum(child.kind), next, @intFromBool(child.attrEntry("test") != null) });
900
+ switch (action.action) {
901
+ 1 => try out.append(ui.arena, try self.buildElement(ui, scope, child)),
902
+ 2 => try out.append(ui.arena, try self.buildUse(ui, scope, child)),
903
+ 3 => try self.buildSlot(ui, scope, child, out),
904
+ 4 => {
905
+ const else_index = index + 1;
906
+ index += action.fields[0];
907
+ const count = try self.buildFor(ui, scope, child, out);
908
+ const branch = construction.decide(owner, 3, &.{ @intFromEnum(child.kind), action.fields[0], count });
909
+ if (branch.action == 6) try self.buildChildren(ui, scope, children[else_index], out) else if (branch.action != 0) @panic("invalid loop branch action");
910
+ },
911
+ 5 => {
912
+ const condition = try self.evalAttrExpression(scope, child, child.attrEntry("test").?);
913
+ const branch = construction.decide(owner, 3, &.{ @intFromEnum(child.kind), @intFromBool(next == @intFromEnum(markup.MarkupNodeKind.else_block)), @intFromBool(markup.expr.truthyWithPolicy(owner, condition)) });
914
+ if (branch.action == 6) try self.buildChildren(ui, scope, children[index + branch.fields[0]], out) else if (branch.action != 0) @panic("invalid condition branch action");
915
+ index += branch.fields[1];
916
+ },
917
+ 255 => return self.failVoid(child, construction.message(action.code)),
918
+ else => @panic("invalid child construction action"),
919
+ }
920
+ }
921
+ }
922
+ fn buildSlotOwned(self: *Self, owner: markup.expr.Policy, ui: *Ui, scope: *Scope, node: markup.MarkupNode, out: *std.ArrayListUnmanaged(Ui.Node)) BuildError!void {
923
+ var request = construction.Request.init(5);
924
+ defer request.deinit();
925
+ request.words(&.{ node.attrs.len, node.children.len, if (scope.template_ctx) |ctx| ctx + 1 else 0, scope.len, scope.floor });
926
+ for (scope.entries[0..scope.len]) |entry| request.words(&.{ @intFromEnum(std.meta.activeTag(entry.payload)), if (entry.payload == .slot) entry.payload.slot.nodes.len else 0 });
927
+ const action = request.decision(owner);
928
+ if (action.action == 255) return self.failVoid(if (action.code == 24) node.children[0] else node, construction.message(action.code));
929
+ if (action.action != 8 or action.fields[0] > scope.len) @panic("invalid slot action");
930
+ if (action.fields[0] == 0) return;
931
+ const capture = scope.entries[action.fields[0] - 1].payload.slot;
932
+ const saved_len = scope.len;
933
+ const saved_floor = scope.floor;
934
+ const saved_ctx = scope.template_ctx;
935
+ const saved_chain = scope.source_chain;
936
+ var saved_entries: [max_scope_depth]ScopeEntry = undefined;
937
+ @memcpy(saved_entries[0 .. saved_len - capture.len], scope.entries[capture.len..saved_len]);
938
+ scope.len = capture.len;
939
+ scope.floor = capture.floor;
940
+ scope.template_ctx = capture.template_ctx;
941
+ scope.source_chain = capture.chain;
942
+ defer {
943
+ @memcpy(scope.entries[capture.len..saved_len], saved_entries[0 .. saved_len - capture.len]);
944
+ scope.len = saved_len;
945
+ scope.floor = saved_floor;
946
+ scope.template_ctx = saved_ctx;
947
+ scope.source_chain = saved_chain;
948
+ }
949
+ try self.buildChildList(ui, scope, capture.nodes, out);
950
+ }
951
+ fn buildForOwned(self: *Self, owner: markup.expr.Policy, ui: *Ui, scope: *Scope, node: markup.MarkupNode, out: *std.ArrayListUnmanaged(Ui.Node)) BuildError!usize {
952
+ @setEvalBranchQuota(scan_quota);
953
+ var request = construction.Request.init(6);
954
+ defer request.deinit();
955
+ request.words(&.{ @intFromBool(node.attr("each") != null), @intFromBool(node.attr("as") != null), scope.len, node.children.len });
956
+ for (node.children) |child| request.word(@intFromEnum(child.kind));
957
+ const admission = request.decision(owner);
958
+ if (admission.action == 255) return self.failVoid(if (admission.fields[0] == 0) node else node.children[admission.fields[0] - 1], construction.message(admission.code));
959
+ if (admission.action != 0) @panic("invalid loop admission");
960
+ var cursor: usize = 0;
961
+ while (true) {
962
+ const action = construction.decide(owner, 7, &.{ 0, cursor, item_types.len, 0 });
963
+ if (action.action == 255) return self.failVoid(node, construction.message(action.code));
964
+ if (action.action != 9 or action.fields[0] >= item_types.len) @panic("invalid iterable capability");
965
+ cursor = action.fields[1];
966
+ inline for (item_types, 0..) |Item, type_index| {
967
+ if (action.fields[0] == type_index) {
968
+ if (try self.iterateItems(ui, Item, type_index, scope, node.attr("each").?)) |items| {
969
+ try self.buildItemsOwned(owner, ui, Item, type_index, scope, node, items, out);
970
+ return items.len;
971
+ }
972
+ }
973
+ }
974
+ }
975
+ }
976
+ fn buildItemsOwned(self: *Self, owner: markup.expr.Policy, ui: *Ui, comptime Item: type, comptime type_index: usize, scope: *Scope, node: markup.MarkupNode, items: []const Item, out: *std.ArrayListUnmanaged(Ui.Node)) BuildError!void {
977
+ var cursor: usize = 0;
978
+ while (true) {
979
+ const action = construction.decide(owner, 7, &.{ 1, cursor, items.len, scope.len });
980
+ if (action.action == 0) return;
981
+ if (action.action != 9 or action.fields[0] >= items.len) @panic("invalid item capability");
982
+ cursor = action.fields[1];
983
+ const item = &items[action.fields[0]];
984
+ scope.entries[scope.len] = .{ .name = node.attr("as").?, .payload = .{ .item = .{ .type_index = type_index, .ptr = @ptrCast(item) } } };
985
+ scope.len = action.fields[2];
986
+ // Pop only this iteration's entry. An early nested template
987
+ // failure can leave published scope entries in the reference.
988
+ defer scope.len -= action.fields[2] - action.fields[3];
989
+ const first_emitted = out.items.len;
990
+ try self.buildChildren(ui, scope, node, out);
991
+ const after = construction.decide(owner, 7, &.{ 3, 0, 0, @intFromBool(node.attr("key") != null) });
992
+ if (after.action == 14) {
993
+ const base = try self.itemKeyOwned(owner, Item, item, node, node.attr("key").?);
994
+ var slot: usize = 0;
995
+ const emitted = out.items[first_emitted..];
996
+ while (slot < emitted.len) {
997
+ const built = &emitted[slot];
998
+ const stamp = construction.decide(owner, 7, &.{ 2, slot, emitted.len, @intFromBool(built.key != null or built.global_key != null) });
999
+ if (stamp.action == 10) built.key = try canvas.forSlotKey(ui.arena, base, stamp.fields[0]) else if (stamp.action != 9) @panic("invalid key stamping capability");
1000
+ slot = stamp.fields[1];
1001
+ }
1002
+ } else if (after.action != 0) @panic("invalid post-body capability");
1003
+ }
1004
+ }
1005
+ fn iterateItemsOwned(self: *Self, owner: markup.expr.Policy, ui: *Ui, comptime Item: type, comptime type_index: usize, scope: *Scope, each: []const u8) BuildError!?[]const Item {
1006
+ @setEvalBranchQuota(scan_quota);
1007
+ const entry = scope.lookup(each);
1008
+ const action = construction.decide(owner, 8, &.{ @intFromBool(entry != null), if (entry) |e| @intFromEnum(std.meta.activeTag(e.payload)) else 0, if (entry != null and entry.?.payload == .slice) entry.?.payload.slice.type_index else 0, type_index });
1009
+ switch (action.action) {
1010
+ 0 => return null,
1011
+ 11 => {
1012
+ const slice = entry.?.payload.slice;
1013
+ const ptr: [*]const Item = @ptrCast(@alignCast(slice.ptr));
1014
+ return ptr[0..slice.len];
1015
+ },
1016
+ 12 => return self.iterateModelItems(ui, Item, scope, each),
1017
+ else => @panic("invalid model iterable capability"),
1018
+ }
1019
+ }
1020
+ fn buildUseOwned(self: *Self, owner: markup.expr.Policy, ui: *Ui, scope: *Scope, node: markup.MarkupNode) BuildError!Ui.Node {
1021
+ var lookup = construction.Request.init(10);
1022
+ defer lookup.deinit();
1023
+ const named = node.attr("template");
1024
+ lookup.words(&.{ scope.len, scope.floor, if (scope.template_ctx) |ctx| ctx + 1 else 0, scope.use_depth, @intFromBool(named != null) });
1025
+ lookup.blob(named orelse "");
1026
+ lookup.word(self.document.templates.len);
1027
+ for (self.document.templates) |definition| {
1028
+ const name = definition.attr("name");
1029
+ lookup.word(@intFromBool(name != null));
1030
+ lookup.blob(name orelse "");
1031
+ }
1032
+ const selected = lookup.decision(owner);
1033
+ if (selected.action == 255) return self.failNode(node, construction.message(selected.code));
1034
+ if (selected.action != 20 or selected.fields[0] >= self.document.templates.len) @panic("invalid template lookup capability");
1035
+ var request = construction.Request.init(12);
1036
+ defer request.deinit();
1037
+ request.words(&.{ scope.len, scope.floor, if (scope.template_ctx) |ctx| ctx + 1 else 0, scope.use_depth, selected.fields[0] });
1038
+ request.useSite(node);
1039
+ request.node(self.document.templates[selected.fields[0]], true);
1040
+ var program = try request.program(owner);
1041
+ defer program.deinit();
1042
+ const saved_len = scope.len;
1043
+ const saved_floor = scope.floor;
1044
+ const saved_ctx = scope.template_ctx;
1045
+ const saved_chain = scope.source_chain;
1046
+ var template_index: usize = 0;
1047
+ var args_bytes: []const u8 = "";
1048
+ var forwarded_press: ?MsgT = null;
1049
+ var entry_slot: usize = 0;
1050
+ var entry_name: []const u8 = "";
1051
+ var payload: ScopeEntry.Payload = undefined;
1052
+ while (program.next()) |action| {
1053
+ const f = action.fields;
1054
+ switch (action.action) {
1055
+ 20 => {
1056
+ template_index = f[0];
1057
+ args_bytes = if (f[1] == 0) "" else self.document.templates[template_index].attrs[f[1] - 1].value;
1058
+ },
1059
+ 21 => {
1060
+ var options: Ui.ElementOptions = .{};
1061
+ try self.applyMessageAttr(scope, node, &options, node.attrs[f[0]]);
1062
+ forwarded_press = options.on_press;
1063
+ },
1064
+ 22 => {
1065
+ entry_slot = f[0];
1066
+ entry_name = args_bytes[f[1]..f[2]];
1067
+ },
1068
+ 23 => payload = try self.argPayload(ui, scope, node, node.attrs[f[0]]),
1069
+ 24 => payload = .{ .value = value_programs.literal(owner, args_bytes[f[0]..f[1]], null) },
1070
+ 25 => scope.entries[entry_slot] = .{ .name = entry_name, .payload = payload, .defaulted = action.code == 1 },
1071
+ 26 => scope.entries[f[0]] = .{ .name = "", .payload = .{ .slot = .{ .nodes = node.children, .len = saved_len, .floor = saved_floor, .template_ctx = saved_ctx, .chain = saved_chain } } },
1072
+ 27 => {
1073
+ scope.len = f[0];
1074
+ scope.floor = f[1];
1075
+ scope.template_ctx = f[2] - 1;
1076
+ scope.use_depth = f[3];
1077
+ },
1078
+ 28 => if (ui.provenance_sink != null) {
1079
+ const chain = try ui.arena.alloc(ui_provenance.UseSite, saved_chain.len + 1);
1080
+ @memcpy(chain[0..saved_chain.len], saved_chain);
1081
+ chain[saved_chain.len] = .{ .src_path = node.src_path, .span = node.span, .line = node.line, .column = node.column };
1082
+ scope.source_chain = chain;
1083
+ },
1084
+ 29 => {
1085
+ defer {
1086
+ scope.len = saved_len;
1087
+ scope.floor = saved_floor;
1088
+ scope.use_depth -= 1;
1089
+ scope.template_ctx = saved_ctx;
1090
+ scope.source_chain = saved_chain;
1091
+ }
1092
+ if (program.next() != null) @panic("trailing template capability");
1093
+ return self.buildElementForwardedPress(ui, scope, self.document.templates[template_index].children[0], forwarded_press);
1094
+ },
1095
+ 255 => return self.failNode(request.nodes.items[f[0]], construction.message(action.code)),
1096
+ else => @panic("invalid template construction action"),
1097
+ }
1098
+ }
1099
+ @panic("incomplete template program");
1100
+ }
1101
+ fn argPayloadOwned(self: *Self, owner: markup.expr.Policy, ui: *Ui, scope: *Scope, node: markup.MarkupNode, attribute: markup.MarkupAttr) BuildError!ScopeEntry.Payload {
1102
+ @setEvalBranchQuota(scan_quota);
1103
+ const typed = markup.attrTypedWithPolicy(owner, attribute);
1104
+ const path = if (typed == .binding) typed.binding else "";
1105
+ const parts = if (typed == .binding) value_programs.bindingPath(owner, path) else null;
1106
+ const entry = if (parts) |binding| scope.lookup(binding.head) else null;
1107
+ const action = construction.decide(owner, 9, &.{ if (typed == .invalid) 0 else if (typed == .binding) 1 else 2, @intFromBool(entry != null), if (entry) |e| @intFromEnum(std.meta.activeTag(e.payload)) else 0, @intFromBool(parts != null and parts.?.tail != null) });
1108
+ switch (action.action) {
1109
+ 255 => return self.failPayload(node, construction.message(action.code)),
1110
+ 11 => return entry.?.payload,
1111
+ 12 => {
1112
+ var cursor: usize = 0;
1113
+ while (true) {
1114
+ var request = construction.Request.init(11);
1115
+ defer request.deinit();
1116
+ request.words(&.{ cursor, item_types.len });
1117
+ inline for (item_types) |Item| request.word(@intFromBool(Item == u8));
1118
+ const next = request.decision(owner);
1119
+ if (next.action == 0) break;
1120
+ if (next.action != 9 or next.fields[0] >= item_types.len) @panic("invalid argument iterable capability");
1121
+ cursor = next.fields[1];
1122
+ inline for (item_types, 0..) |Item, type_index| {
1123
+ if (comptime Item != u8) {
1124
+ if (next.fields[0] == type_index) {
1125
+ if (try self.iterateItems(ui, Item, type_index, scope, path)) |items| return .{ .slice = .{ .type_index = type_index, .ptr = @ptrCast(items.ptr), .len = items.len } };
1126
+ }
1127
+ }
1128
+ }
1129
+ }
1130
+ },
1131
+ 13 => {},
1132
+ else => @panic("invalid argument construction action"),
1133
+ }
1134
+ return .{ .value = try self.evalUnclassifiedExpression(scope, node, attribute) };
1135
+ }
1136
+
1137
+ fn iterateModelItems(self: *Self, ui: *Ui, comptime Item: type, scope: *Scope, each: []const u8) BuildError!?[]const Item {
1138
+ if (scope.expression_policy) |owner| return self.iterateModelItemsOwned(owner, ui, Item, scope, each);
1139
+ return self.iterateModelItemsReference(ui, Item, scope, each);
1140
+ }
1141
+
1142
+ fn iterateModelItemsReference(self: *Self, ui: *Ui, comptime Item: type, scope: *Scope, each: []const u8) BuildError!?[]const Item {
1143
+ @setEvalBranchQuota(scan_quota);
1144
+ _ = self;
1145
+ const model = scope.model;
1146
+ inline for (@typeInfo(ModelT).@"struct".fields) |field| {
1147
+ if (comptime sliceElement(field.type) != null and sliceElement(field.type).? == Item) {
1148
+ if (std.mem.eql(u8, field.name, each)) {
1149
+ return asSlice(Item, &@field(model, field.name));
1150
+ }
1151
+ }
1152
+ }
1153
+ inline for (@typeInfo(ModelT).@"struct".decls) |decl| {
1154
+ const DeclType = @TypeOf(@field(ModelT, decl.name));
1155
+ if (comptime sliceElement(DeclType) != null and sliceElement(DeclType).? == Item) {
1156
+ if (std.mem.eql(u8, decl.name, each)) {
1157
+ return asSlice(Item, &@field(ModelT, decl.name));
1158
+ }
1159
+ }
1160
+ if (comptime isItemFn(DeclType, Item, false)) {
1161
+ if (std.mem.eql(u8, decl.name, each)) {
1162
+ return @field(ModelT, decl.name)(model);
1163
+ }
1164
+ }
1165
+ if (comptime isItemFn(DeclType, Item, true)) {
1166
+ if (std.mem.eql(u8, decl.name, each)) {
1167
+ return @field(ModelT, decl.name)(model, ui.arena);
1168
+ }
1169
+ }
1170
+ }
1171
+ return null;
1172
+ }
1173
+
1174
+ /// Direct capability witnesses for differential SDK tests only.
1175
+ pub const RuntimeTest = if (@import("builtin").is_test) struct {
1176
+ pub fn text(view: *Self, ui: *Ui, model: *const ModelT, run: markup.MarkupNode) BuildError![]const u8 {
1177
+ var scope = Scope{ .model = model, .arena = ui.arena, .expression_policy = ui.expression_policy };
1178
+ return view.runText(ui, &scope, run, run);
1179
+ }
1180
+ pub fn key(view: *Self, ui: *Ui, comptime Item: type, item: *const Item, field: []const u8) BuildError!canvas.UiKey {
1181
+ const node = view.document.root.?;
1182
+ if (ui.expression_policy) |owner| return view.itemKeyOwned(owner, Item, item, node, field);
1183
+ return view.itemKey(Item, item, node, field);
1184
+ }
1185
+ pub fn link(owner: ?markup.expr.Policy, tag: []const u8) ?Ui.LinkMsgFn {
1186
+ return if (owner) |policy| linkConstructorOwned(policy, tag) else linkConstructor(tag);
1187
+ }
1188
+ pub fn details(owner: ?markup.expr.Policy, tag: []const u8) ?*const fn (index: usize) MsgT {
1189
+ return if (owner) |policy| detailsConstructorOwned(policy, tag) else detailsConstructor(tag);
1190
+ }
1191
+ } else void;
1192
+
1193
+ /// Initialized scope observation for SDK differential tests only.
1194
+ pub const ConstructionTest = if (@import("builtin").is_test) struct {
1195
+ pub const Result = struct { node: ?Ui.Node = null, failure: ?BuildError = null, scope: Scope };
1196
+ pub fn run(view: *Self, ui: *Ui, model: *const ModelT, length: usize, floor: usize, depth: usize, context: ?usize) Result {
1197
+ var scope = Scope{ .model = model, .arena = ui.arena, .expression_policy = ui.expression_policy, .len = length, .floor = floor, .use_depth = depth, .template_ctx = context };
1198
+ for (&scope.entries) |*entry| entry.* = .{ .name = "retained", .payload = .{ .value = .{ .boolean = true } } };
1199
+ const built = view.buildNode(ui, &scope, view.document.root.?);
1200
+ if (built) |node| return .{ .node = node, .scope = scope } else |err| return .{ .failure = err, .scope = scope };
1201
+ }
1202
+ } else void;
1203
+
1204
+ /// Observe partial attribute state and failures without element linting.
1205
+ pub const ElementTest = if (@import("builtin").is_test) struct {
1206
+ pub const Result = struct { options: Ui.ElementOptions, failure: ?BuildError = null };
1207
+ pub fn run(view: *Self, ui: *Ui, model: *const ModelT, node: markup.MarkupNode, event_only: bool) Result {
1208
+ var scope = Scope{ .model = model, .arena = ui.arena, .expression_policy = ui.expression_policy };
1209
+ var options: Ui.ElementOptions = .{};
1210
+ const result = if (event_only) view.applyMessageAttr(&scope, node, &options, node.attrs[0]) else view.applyAttrs(&scope, node, &options);
1211
+ result catch |err| return .{ .options = options, .failure = err };
1212
+ return .{ .options = options };
1213
+ }
1214
+ } else void;
1215
+
1216
+ /// Borrowed byte-path capabilities for SDK differential tests.
1217
+ pub const PostprocessingTest = if (@import("builtin").is_test) struct {
1218
+ pub const bindingPath = value_programs.bindingPath;
1219
+ pub const referenceHead = pathHead;
1220
+ pub const referenceTail = pathTail;
1221
+ } else struct {};
1222
+
1223
+ /// Exact borrowed literals and initialized lexical captures for SDK tests.
1224
+ pub const LiteralTest = if (@import("builtin").is_test) struct {
1225
+ pub const literal = value_programs.literal;
1226
+ pub const reference = reflect.literalValue;
1227
+ pub const referenceAttribute = reflect.literalValueForAttribute;
1228
+ pub const Capture = SlotCapture;
1229
+ pub fn capture(owner: ?markup.expr.Policy, model: *const ModelT, captures: []const ?Capture, floor: usize) ?Capture {
1230
+ std.debug.assert(captures.len <= max_scope_depth and floor <= captures.len);
1231
+ var scope = Scope{ .model = model, .arena = std.testing.allocator, .expression_policy = owner, .len = captures.len, .floor = floor };
1232
+ for (captures, 0..) |captured, index| scope.entries[index] = .{
1233
+ .name = "",
1234
+ .payload = if (captured) |value| .{ .slot = value } else .{ .value = .{ .boolean = false } },
1235
+ };
1236
+ return if (owner) |policy| scope.slotCaptureOwned(policy) else scope.slotCapture();
1237
+ }
1238
+ } else struct {};
1239
+
1240
+ /// Direct capability observation for metadata differential tests.
1241
+ pub const MetadataTest = if (@import("builtin").is_test) struct {
1242
+ pub const iconValue = value_programs.iconValue;
1243
+ pub const language = value_programs.language;
1244
+ pub const lineNumbers = value_programs.lineNumbers;
1245
+ } else struct {};
1246
+
1247
+ /// Direct capability observation for option-admission differential tests.
1248
+ pub const OptionTest = if (@import("builtin").is_test) struct {
1249
+ pub const enumValue = enumValueOwned;
1250
+ pub const seriesKind = value_programs.seriesKind;
1251
+ pub const spanScale = value_programs.spanScale;
1252
+ } else struct {};
1253
+
1254
+ /// Direct numeric option capabilities for differential SDK tests.
1255
+ pub const NumericTest = if (@import("builtin").is_test) struct {
1256
+ pub const integerOption = value_programs.integerOption;
1257
+ pub const virtualExtent = value_programs.virtualExtent;
1258
+ pub const virtualAnchor = value_programs.virtualAnchor;
1259
+ pub const virtualRange = value_programs.virtualRange;
1260
+ pub const virtualExtentRequired = value_programs.virtualExtentRequired;
1261
+ pub const virtualAttribute = value_programs.virtualAttribute;
1262
+ } else struct {};
1263
+
1264
+ fn virtualCountAttrOwned(self: *Self, owner: markup.expr.Policy, scope: *Scope, node: markup.MarkupNode, name: []const u8) BuildError!usize {
1265
+ const attribute = node.attrEntry(name);
1266
+ const admission = value_programs.virtualAttribute(owner, 0, name, attribute != null);
1267
+ if (admission.action == 0) return admission.fields[0];
1268
+ if (admission.action != 1) @panic("invalid virtual count attribute capability");
1269
+ const value = try self.evalUnclassifiedExpression(scope, node, attribute.?);
1270
+ const result = value_programs.integerOption(owner, 0, value, 0);
1271
+ return switch (result.action) {
1272
+ 1 => @intCast(value.integer),
1273
+ 255 => self.failValue(node, value_programs.message(result.code)),
1274
+ else => @panic("invalid virtual count capability"),
1275
+ };
1276
+ }
1277
+
1278
+ fn virtualExtentAttrOwned(self: *Self, owner: markup.expr.Policy, scope: *Scope, node: markup.MarkupNode, name: []const u8) BuildError!f32 {
1279
+ const attribute = node.attrEntry(name);
1280
+ const admission = value_programs.virtualAttribute(owner, 1, name, attribute != null);
1281
+ if (admission.action == 0) return @floatFromInt(admission.fields[0]);
1282
+ if (admission.action != 1) @panic("invalid virtual extent attribute capability");
1283
+ const value = try self.floatAttr(scope, node, attribute.?);
1284
+ if (!value_programs.virtualExtent(owner, value)) return self.failValue(node, value_programs.message(51));
1285
+ return value;
1286
+ }
1287
+
1288
+ /// Initialized lexical payload observation for differential SDK tests.
1289
+ pub const ValueTest = if (@import("builtin").is_test) struct {
1290
+ pub const Result = struct { value: ?Value = null, failure: ?BuildError = null, length: usize, floor: usize };
1291
+ pub fn run(view: *Self, ui: *Ui, model: *const ModelT, path: []const u8, allow_arena: bool, mode: u8) Result {
1292
+ var scope = Scope{ .model = model, .arena = ui.arena, .expression_policy = ui.expression_policy };
1293
+ for (&scope.entries) |*entry| entry.* = .{ .name = "retained", .payload = .{ .value = .{ .boolean = false } } };
1294
+ if (mode != 0) {
1295
+ scope.entries[0] = .{ .name = "arg", .payload = .{ .value = .{ .boolean = true } } };
1296
+ scope.len = 1;
1297
+ }
1298
+ if (mode == 2) scope.entries[0].payload = .{ .slice = .{ .ptr = model, .type_index = 0, .len = 0 } };
1299
+ if (mode == 3) scope.entries[0].payload = .{ .virtual = .{ .range = .{}, .options = .{ .id = "test", .item_count = 0, .item_extent = 10 }, .consumed = &ui.failed } };
1300
+ if (mode == 4) scope.floor = scope.len;
1301
+ const result = view.evalBinding(&scope, view.document.root.?, path, allow_arena);
1302
+ if (result) |value| return .{ .value = value, .length = scope.len, .floor = scope.floor } else |err| return .{ .failure = err, .length = scope.len, .floor = scope.floor };
1303
+ }
1304
+ } else void;
1305
+
1306
+ /// Complete specialized values and consumed/scope state for differential tests.
1307
+ pub const SpecializedTest = if (@import("builtin").is_test) struct {
1308
+ pub const Result = struct {
1309
+ node: ?Ui.Node = null,
1310
+ series: ?canvas.ChartSeries = null,
1311
+ span: ?canvas.TextSpan = null,
1312
+ failure: ?BuildError = null,
1313
+ consumed: bool = false,
1314
+ length: usize = 0,
1315
+ floor: usize = 0,
1316
+ context: ?usize = null,
1317
+ };
1318
+ pub fn run(view: *Self, ui: *Ui, model: *const ModelT, mode: u8) Result {
1319
+ var scope = Scope{ .model = model, .arena = ui.arena, .expression_policy = ui.expression_policy };
1320
+ var consumed = false;
1321
+ if (mode == 4) {
1322
+ const options: Ui.VirtualListOptions = .{ .id = "test", .item_count = 2, .item_extent = 10, .viewport_fallback = 100 };
1323
+ scope.entries[0] = .{ .name = "window", .payload = .{ .virtual = .{ .options = options, .range = ui.virtualWindow(options), .consumed = &consumed } } };
1324
+ scope.len = 1;
1325
+ }
1326
+ var result: Result = .{};
1327
+ switch (mode) {
1328
+ 1 => result.span = view.buildSpan(ui, &scope, view.document.root.?) catch |err| blk: {
1329
+ result.failure = err;
1330
+ break :blk null;
1331
+ },
1332
+ 2 => result.series = view.buildSeries(ui, &scope, view.document.root.?) catch |err| blk: {
1333
+ result.failure = err;
1334
+ break :blk null;
1335
+ },
1336
+ 3 => result.node = view.buildSpanParagraph(ui, &scope, view.document.root.?, .{}) catch |err| blk: {
1337
+ result.failure = err;
1338
+ break :blk null;
1339
+ },
1340
+ 4 => result.node = view.buildVirtualList(ui, &scope, view.document.root.?) catch |err| blk: {
1341
+ result.failure = err;
1342
+ break :blk null;
1343
+ },
1344
+ else => result.node = view.buildNode(ui, &scope, view.document.root.?) catch |err| blk: {
1345
+ result.failure = err;
1346
+ break :blk null;
1347
+ },
1348
+ }
1349
+ result.consumed = consumed;
1350
+ result.length = scope.len;
1351
+ result.floor = scope.floor;
1352
+ result.context = scope.template_ctx;
1353
+ return result;
1354
+ }
1355
+ } else void;
1356
+
1357
+ fn specializedFormOwned(self: *Self, owner: markup.expr.Policy, node: markup.MarkupNode, channel: u8, typed: markup.TypedAttrValue) BuildError!u8 {
1358
+ const row = value_programs.specializedForm(owner, channel, typed);
1359
+ if (row.action == 255) return self.failVoid(node, value_programs.message(row.code));
1360
+ return row.action;
1361
+ }
1362
+
1363
+ fn specializedTextOwned(self: *Self, owner: markup.expr.Policy, node: markup.MarkupNode, channel: u8, value: Value) BuildError![]const u8 {
1364
+ const row = value_programs.specializedValue(owner, channel, value);
1365
+ return switch (row.action) {
1366
+ 1 => value.string,
1367
+ 255 => self.failVoid(node, value_programs.message(row.code)),
1368
+ else => @panic("invalid specialized text capability"),
1369
+ };
1370
+ }
1371
+
1372
+ fn codeDiffSpecOwned(self: *Self, owner: markup.expr.Policy, scope: *Scope, node: markup.MarkupNode, attribute: markup.MarkupAttr) BuildError![]const u8 {
1373
+ // Metadata uses raw grammar before evaluation, even with a typed cache.
1374
+ const parsed = markup.parseAttrExpressionWithPolicy(owner, attribute.value);
1375
+ const typed: markup.TypedAttrValue = if (parsed) |value| switch (value) {
1376
+ .literal => |text| .{ .literal = text },
1377
+ .binding => |path| .{ .binding = path },
1378
+ .equals => |sides| .{ .equals = .{ .left = sides.left, .right = sides.right } },
1379
+ .expression => |inner| .{ .expression = .{ .inner = inner } },
1380
+ } else .invalid;
1381
+ const admission = try self.specializedFormOwned(owner, node, 13, typed);
1382
+ return switch (admission) {
1383
+ 1 => typed.literal,
1384
+ 2 => self.specializedTextOwned(owner, node, 3, try self.evalAttrExpression(scope, node, attribute)),
1385
+ else => @panic("invalid code metadata capability"),
1386
+ };
1387
+ }
1388
+
1389
+ fn presenceFlagOwned(self: *Self, owner: markup.expr.Policy, scope: *Scope, node: markup.MarkupNode, attribute: markup.MarkupAttr) BuildError!bool {
1390
+ const row = value_programs.decide(owner, 29, &.{attribute.value.len});
1391
+ return switch (row.action) {
1392
+ 1 => true,
1393
+ 2 => markup.expr.truthyWithPolicy(owner, try self.evalAttrExpression(scope, node, attribute)),
1394
+ else => @panic("invalid presence flag capability"),
1395
+ };
1396
+ }
1397
+
1398
+ fn buildStepperStepsOwned(self: *Self, owner: markup.expr.Policy, ui: *Ui, scope: *Scope, node: markup.MarkupNode, options: Ui.StepperOptions) BuildError!Ui.Node {
1399
+ var selection = value_programs.Request.init(30);
1400
+ defer selection.deinit();
1401
+ selection.words(&.{ 0, node.children.len });
1402
+ selection.blob(if (node.children.len > 0) @tagName(node.children[0].kind) else "");
1403
+ const selected = selection.decision(owner);
1404
+ const capture = switch (selected.action) {
1405
+ 1 => null,
1406
+ 2 => blk: {
1407
+ const captured = scope.slotCaptureOwned(owner);
1408
+ const admission = value_programs.decide(owner, 30, &.{ 1, @intFromBool(captured != null) });
1409
+ if (admission.action == 255) return self.failNode(node.children[0], value_programs.message(admission.code));
1410
+ if (admission.action != 1) @panic("invalid stepper capture capability");
1411
+ break :blk captured;
1412
+ },
1413
+ else => @panic("invalid stepper selection capability"),
1414
+ };
1415
+ const step_nodes = if (capture) |captured| captured.nodes else node.children;
1416
+ const steps = try ui.arena.alloc(Ui.StepperStep, step_nodes.len);
1417
+ var saved_entries: [max_scope_depth]ScopeEntry = undefined;
1418
+ const saved_len = scope.len;
1419
+ const saved_floor = scope.floor;
1420
+ const saved_ctx = scope.template_ctx;
1421
+ if (capture) |captured| {
1422
+ for (scope.entries[captured.len..saved_len], 0..) |entry, offset| saved_entries[offset] = entry;
1423
+ scope.len = captured.len;
1424
+ scope.floor = captured.floor;
1425
+ scope.template_ctx = captured.template_ctx;
1426
+ }
1427
+ defer if (capture) |captured| {
1428
+ for (saved_entries[0 .. saved_len - captured.len], 0..) |entry, offset| scope.entries[captured.len + offset] = entry;
1429
+ scope.len = saved_len;
1430
+ scope.floor = saved_floor;
1431
+ scope.template_ctx = saved_ctx;
1432
+ };
1433
+ for (step_nodes, 0..) |child, index| {
1434
+ var request = specialized.Request.init(14);
1435
+ defer request.deinit();
1436
+ request.node(child, true);
1437
+ request.word(@intFromEnum(child.kind));
1438
+ var program = try request.program(owner);
1439
+ defer program.deinit();
1440
+ while (program.next()) |step| switch (step.action) {
1441
+ 101 => steps[index] = .{ .label = try self.interpolatedText(ui, scope, child) },
1442
+ 255 => return self.failNode(child, specialized.message(step.code)),
1443
+ else => @panic("invalid step capability"),
1444
+ };
1445
+ }
1446
+ return ui.stepper(options, steps);
1447
+ }
1448
+
1449
+ fn buildSpanParagraphOwned(self: *Self, owner: markup.expr.Policy, ui: *Ui, scope: *Scope, node: markup.MarkupNode, options: Ui.ElementOptions) BuildError!Ui.Node {
1450
+ var request = specialized.Request.init(12);
1451
+ defer request.deinit();
1452
+ request.node(node, true);
1453
+ var program = try request.program(owner);
1454
+ defer program.deinit();
1455
+ var spans: []canvas.TextSpan = &.{};
1456
+ while (program.next()) |step| switch (step.action) {
1457
+ 100 => spans = try ui.arena.alloc(canvas.TextSpan, node.children.len),
1458
+ 101 => spans[step.fields[0]] = .{ .text = try self.runText(ui, scope, node, node.children[step.fields[0]]) },
1459
+ 102 => spans[step.fields[0]] = try self.buildSpan(ui, scope, node.children[step.fields[0]]),
1460
+ 200 => return ui.paragraph(options, spans),
1461
+ 255 => return self.failNode(if (step.fields[0] == 0) node else node.children[step.fields[0] - 1], specialized.message(step.code)),
1462
+ else => @panic("invalid paragraph capability"),
1463
+ };
1464
+ @panic("paragraph program did not publish");
1465
+ }
1466
+
1467
+ fn buildSpanOwned(self: *Self, owner: markup.expr.Policy, ui: *Ui, scope: *Scope, node: markup.MarkupNode) BuildError!canvas.TextSpan {
1468
+ var request = specialized.Request.init(13);
1469
+ defer request.deinit();
1470
+ request.node(node, true);
1471
+ var program = try request.program(owner);
1472
+ defer program.deinit();
1473
+ var span = canvas.TextSpan{};
1474
+ while (program.next()) |step| {
1475
+ if (step.action == 100) {
1476
+ if (shape_programs.shape(owner, node, 1)) |info| {
1477
+ self.diagnostic = .{ .line = info.line, .column = info.column, .message = info.message, .path = info.path };
1478
+ return error.MarkupBuild;
1479
+ }
1480
+ continue;
1481
+ }
1482
+ if (step.action == 101) {
1483
+ span.text = try self.runText(ui, scope, node, node.children[0]);
1484
+ continue;
1485
+ }
1486
+ if (step.action == 200) return span;
1487
+ const attribute = node.attrs[step.fields[0]];
1488
+ switch (step.action) {
1489
+ 1 => {
1490
+ const text = try self.stringAttr(scope, node, attribute, markup.span_weight_value_message);
1491
+ span.weight = enumValueOwned(canvas.TextSpanWeight, owner, text) orelse return self.failValue(node, markup.span_weight_value_message);
1492
+ },
1493
+ 2 => {
1494
+ const value = try self.evalAttrExpression(scope, node, attribute);
1495
+ const admission = value_programs.specializedValue(owner, 4, value);
1496
+ const multiplier = switch (admission.action) {
1497
+ 1 => value.float,
1498
+ 2 => @as(f32, @floatFromInt(value.integer)),
1499
+ 255 => return self.failValue(node, value_programs.message(admission.code)),
1500
+ else => @panic("invalid span numeric capability"),
1501
+ };
1502
+ if (!value_programs.spanScale(owner, multiplier)) return self.failValue(node, markup.span_scale_value_message);
1503
+ span.scale = multiplier;
1504
+ },
1505
+ 3 => span.monospace = markup.expr.truthyWithPolicy(scope.expression_policy, try self.evalAttrExpression(scope, node, attribute)),
1506
+ 4 => span.italic = markup.expr.truthyWithPolicy(scope.expression_policy, try self.evalAttrExpression(scope, node, attribute)),
1507
+ 5 => span.underline = markup.expr.truthyWithPolicy(scope.expression_policy, try self.evalAttrExpression(scope, node, attribute)),
1508
+ 6 => span.color = enumValueOwned(canvas.TextSpanColor, owner, attribute.value) orelse return self.failValue(node, markup.unknown_color_token_message),
1509
+ else => @panic("invalid span capability"),
1510
+ }
1511
+ }
1512
+ @panic("span program did not publish");
1513
+ }
1514
+
1515
+ fn buildVirtualWindowOwned(self: *Self, owner: markup.expr.Policy, ui: *Ui, scope: *Scope, node: markup.MarkupNode) BuildError!Ui.Node {
1516
+ @setEvalBranchQuota(scan_quota);
1517
+ var nested = false;
1518
+ for (scope.entries[scope.floor..scope.len]) |entry| {
1519
+ if (entry.payload == .virtual) nested = true;
1520
+ }
1521
+ const identity = node.attr("id") orelse "";
1522
+ const id = canvas.globalWidgetId(.scroll_view, .{ .str = identity });
1523
+ var duplicate = false;
1524
+ for (ui.virtualWindows()) |prior| {
1525
+ if (prior.id == id) duplicate = true;
1526
+ }
1527
+ var request = specialized.Request.init(0);
1528
+ defer request.deinit();
1529
+ request.node(node, true);
1530
+ request.words(&.{ @intFromBool(nested), @intFromBool(scope.len >= max_scope_depth), @intFromBool(ui.virtualWindows().len >= canvas.max_virtual_windows), @intFromBool(duplicate) });
1531
+ var program = try request.program(owner);
1532
+ defer program.deinit();
1533
+ var options: Ui.VirtualListOptions = .{ .id = identity, .item_count = 0, .item_extent = 0 };
1534
+ var consumed: *bool = undefined;
1535
+ var built: ?Ui.Node = null;
1536
+ const saved_len = scope.len;
1537
+ defer scope.len = saved_len;
1538
+ while (program.next()) |step| switch (step.action) {
1539
+ 1 => options.item_count = try self.virtualCountAttrOwned(owner, scope, node, "item-count"),
1540
+ 2 => options.item_extent = try self.virtualExtentAttrOwned(owner, scope, node, "item-extent"),
1541
+ 3 => options.gap = try self.virtualExtentAttrOwned(owner, scope, node, "gap"),
1542
+ 4 => options.overscan = try self.virtualCountAttrOwned(owner, scope, node, "overscan"),
1543
+ 5 => options.viewport_fallback = try self.virtualExtentAttrOwned(owner, scope, node, "viewport-fallback"),
1544
+ 6 => {
1545
+ options.index_base = try self.virtualCountAttrOwned(owner, scope, node, "index-base");
1546
+ options.anchor = value_programs.virtualAnchor(Ui.VirtualListAnchor, owner, node.attr("anchor") orelse "");
1547
+ },
1548
+ 7 => {
1549
+ const estimate = node.attr("extent-estimate").?;
1550
+ if (comptime @hasDecl(ModelT, "virtualExtentHelper") and @hasDecl(ModelT, "virtualExtentEstimate")) {
1551
+ const helper = ModelT.virtualExtentHelper(estimate) orelse return self.failNode(node, "extent-estimate requires a numeric index helper");
1552
+ options.extent_estimate = ModelT.virtualExtentEstimate;
1553
+ options.extent_context = @ptrFromInt(@as(usize, helper) + 1);
1554
+ } else return self.failNode(node, "extent-estimate requires a stable compiled helper capability");
1555
+ },
1556
+ 100 => {
1557
+ if (value_programs.virtualRange(owner, options.index_base, options.item_count)) |message| return self.failNode(node, message);
1558
+ },
1559
+ 101 => {
1560
+ if (value_programs.virtualExtentRequired(owner, options.item_extent, options.extent_estimate != null)) |message| return self.failNode(node, message);
1561
+ },
1562
+ 102 => {
1563
+ consumed = try ui.arena.create(bool);
1564
+ consumed.* = false;
1565
+ scope.entries[scope.len] = .{ .name = node.attr("as").?, .payload = .{ .virtual = .{ .options = options, .range = ui.virtualWindow(options), .consumed = consumed } } };
1566
+ scope.len += 1;
1567
+ },
1568
+ 103 => built = try self.buildNode(ui, scope, node.children[0]),
1569
+ 200 => {
1570
+ if (specialized.check(owner, 17, &.{ 2, @intFromBool(consumed.*) })) |message| return self.failNode(node, message);
1571
+ return built.?;
1572
+ },
1573
+ 255 => return self.failNode(node, specialized.message(step.code)),
1574
+ else => @panic("invalid virtual window capability"),
1575
+ };
1576
+ @panic("virtual window program did not publish");
1577
+ }
1578
+
1579
+ fn buildVirtualListOwned(self: *Self, owner: markup.expr.Policy, ui: *Ui, scope: *Scope, node: markup.MarkupNode) BuildError!Ui.Node {
1580
+ @setEvalBranchQuota(scan_quota);
1581
+ var request = specialized.Request.init(1);
1582
+ defer request.deinit();
1583
+ request.node(node, true);
1584
+ request.word(@intFromBool(scope.len >= max_scope_depth));
1585
+ var program = try request.program(owner);
1586
+ defer program.deinit();
1587
+ // Only the lookup capability may populate this borrowed scope entry.
1588
+ var window: ?*const ScopeEntry = null;
1589
+ while (program.next()) |step| switch (step.action) {
1590
+ 100 => {
1591
+ window = scope.lookup(node.attr("window").?);
1592
+ const valid = window != null and window.?.payload == .virtual;
1593
+ const consumed = if (valid) window.?.payload.virtual.consumed.* else false;
1594
+ if (specialized.check(owner, 18, &.{ @intFromBool(valid), @intFromBool(consumed) })) |message| return self.failNode(node, message);
1595
+ window.?.payload.virtual.consumed.* = true;
1596
+ },
1597
+ 101 => {
1598
+ const resolved = window.?.payload.virtual;
1599
+ const each = node.attr("each").?;
1600
+ const name = node.attr("as").?;
1601
+ inline for (@typeInfo(ModelT).@"struct".decls) |decl| {
1602
+ const Decl = @TypeOf(@field(ModelT, decl.name));
1603
+ if (comptime reflect.isVirtualItemFn(ModelT, Decl)) {
1604
+ if (std.mem.eql(u8, decl.name, each)) {
1605
+ const info = @typeInfo(Decl).@"fn";
1606
+ const Parameter = info.params[1].type.?;
1607
+ const Range = reflect.Pointee(Parameter);
1608
+ var range: Range = undefined;
1609
+ inline for (@typeInfo(Range).@"struct".fields) |field| {
1610
+ const source = @field(resolved.range, field.name);
1611
+ @field(range, field.name) = switch (@typeInfo(field.type)) {
1612
+ .int => @intCast(source),
1613
+ .float => if (@typeInfo(@TypeOf(source)) == .int) @floatFromInt(source) else @floatCast(source),
1614
+ else => unreachable,
1615
+ };
1616
+ }
1617
+ const argument = if (Parameter == Range) range else &range;
1618
+ const items = if (comptime info.params.len == 3) @field(ModelT, decl.name)(scope.model, argument, ui.arena) else @field(ModelT, decl.name)(scope.model, argument);
1619
+ const Item = reflect.sliceElement(info.return_type.?).?;
1620
+ const type_index = comptime blk: {
1621
+ for (item_types, 0..) |candidate, index| if (candidate == Item) break :blk index;
1622
+ unreachable;
1623
+ };
1624
+ var rows: std.ArrayListUnmanaged(Ui.Node) = .empty;
1625
+ var row_request = specialized.Request.init(15);
1626
+ defer row_request.deinit();
1627
+ row_request.words(&.{ items.len, resolved.range.itemCount() });
1628
+ var row_program = try row_request.program(owner);
1629
+ defer row_program.deinit();
1630
+ while (row_program.next()) |row_step| switch (row_step.action) {
1631
+ 1 => {
1632
+ const item = &items[row_step.fields[0]];
1633
+ scope.entries[scope.len] = .{ .name = name, .payload = .{ .item = .{ .type_index = type_index, .ptr = @ptrCast(item) } } };
1634
+ scope.len += 1;
1635
+ const built = self.buildNode(ui, scope, node.children[0]);
1636
+ scope.len -= 1;
1637
+ const row = try built;
1638
+ if (specialized.check(owner, 19, &.{@intFromBool(row.key != null or row.global_key != null)})) |message| return self.failNode(node, message);
1639
+ try rows.append(ui.arena, row);
1640
+ },
1641
+ 100 => return self.buildVirtualProxyOwned(owner, ui, scope, node, resolved.options, resolved.range, rows.items),
1642
+ 200 => @panic("virtual proxy did not publish"),
1643
+ 255 => return self.failNode(node, specialized.message(row_step.code)),
1644
+ else => @panic("invalid virtual row capability"),
1645
+ };
1646
+ }
1647
+ }
1648
+ }
1649
+ return self.failNode(node, "virtual-list each requires a Model-first complete range query");
1650
+ },
1651
+ 255 => return self.failNode(node, specialized.message(step.code)),
1652
+ else => @panic("invalid virtual list capability"),
1653
+ };
1654
+ @panic("virtual list program did not publish");
1655
+ }
1656
+
1657
+ fn buildVirtualProxyOwned(self: *Self, owner: markup.expr.Policy, ui: *Ui, scope: *Scope, node: markup.MarkupNode, original: Ui.VirtualListOptions, range: canvas.VirtualListRange, rows: []const Ui.Node) BuildError!Ui.Node {
1658
+ var request = specialized.Request.init(16);
1659
+ defer request.deinit();
1660
+ request.node(node, false);
1661
+ var program = try request.program(owner);
1662
+ defer program.deinit();
1663
+ var attributes: std.ArrayListUnmanaged(markup.MarkupAttr) = .empty;
1664
+ var element: Ui.ElementOptions = .{};
1665
+ while (program.next()) |step| switch (step.action) {
1666
+ 1 => try attributes.append(ui.arena, node.attrs[step.fields[0]]),
1667
+ 100 => {
1668
+ var proxy = node;
1669
+ proxy.name = "scroll";
1670
+ proxy.attrs = attributes.items;
1671
+ try self.applyAttrs(scope, proxy, &element);
1672
+ },
1673
+ 200 => {
1674
+ var options = original;
1675
+ options.width = element.width;
1676
+ options.height = element.height;
1677
+ options.min_width = element.min_width;
1678
+ options.grow = element.grow;
1679
+ options.padding = element.padding orelse 0;
1680
+ options.style_tokens = element.style_tokens;
1681
+ options.semantics = element.semantics;
1682
+ options.overscroll = element.overscroll;
1683
+ options.on_scroll = element.on_scroll;
1684
+ options.on_reach_end = element.on_reach_end;
1685
+ options.on_reach_start = element.on_reach_start;
1686
+ return ui.virtualList(options, range, .{rows});
1687
+ },
1688
+ else => @panic("invalid virtual proxy capability"),
1689
+ };
1690
+ @panic("virtual proxy program did not publish");
1691
+ }
1692
+
1693
+ fn buildMarkdownOwned(self: *Self, owner: markup.expr.Policy, ui: *Ui, scope: *Scope, node: markup.MarkupNode) BuildError!Ui.Node {
1694
+ var options: Md.Options = .{};
1695
+ var source_text: ?[]const u8 = null;
1696
+
1697
+ var request = specialized.Request.init(2);
1698
+ defer request.deinit();
1699
+ request.node(node, true);
1700
+ var program = try request.program(owner);
1701
+ defer program.deinit();
1702
+
1703
+ while (program.next()) |step| switch (step.action) {
1704
+ 1 => {
1705
+ const attribute = node.attrs[step.fields[0]];
1706
+ const typed = markup.attrTypedWithPolicy(owner, attribute);
1707
+ _ = try self.specializedFormOwned(owner, node, 0, typed);
1708
+ const value = try self.evalBinding(scope, node, typed.binding, true);
1709
+ source_text = try self.specializedTextOwned(owner, node, 0, value);
1710
+ },
1711
+ 2 => {
1712
+ const attribute = node.attrs[step.fields[0]];
1713
+ const typed = markup.attrTypedWithPolicy(owner, attribute);
1714
+ _ = try self.specializedFormOwned(owner, node, 1, typed);
1715
+ options.on_link = linkConstructorOwned(owner, typed.message.tag) orelse {
1716
+ return self.failNode(node, markup.markdown_on_link_message);
1717
+ };
1718
+ },
1719
+ 3 => {
1720
+ const attribute = node.attrs[step.fields[0]];
1721
+ const typed = markup.attrTypedWithPolicy(owner, attribute);
1722
+ _ = try self.specializedFormOwned(owner, node, 2, typed);
1723
+ options.on_details = detailsConstructorOwned(owner, typed.message.tag) orelse {
1724
+ return self.failNode(node, markup.markdown_on_details_message);
1725
+ };
1726
+ },
1727
+ 4 => {
1728
+ const attribute = node.attrs[step.fields[0]];
1729
+ const typed = markup.attrTypedWithPolicy(owner, attribute);
1730
+ _ = try self.specializedFormOwned(owner, node, 3, typed);
1731
+ options.details_expanded = try self.boolItems(ui, scope, node, typed.binding);
1732
+ },
1733
+ 5 => {
1734
+ const attribute = node.attrs[step.fields[0]];
1735
+ const typed = markup.attrTypedWithPolicy(owner, attribute);
1736
+ _ = try self.specializedFormOwned(owner, node, 4, typed);
1737
+ options.images = try self.markdownImageItems(ui, scope, node, typed.binding);
1738
+ },
1739
+ 6 => {
1740
+ const attribute = node.attrs[step.fields[0]];
1741
+ const typed = markup.attrTypedWithPolicy(owner, attribute);
1742
+ _ = try self.specializedFormOwned(owner, node, 5, typed);
1743
+ const value = try self.evalAttrExpression(scope, node, attribute);
1744
+ const admission = value_programs.specializedValue(owner, 2, value);
1745
+ switch (admission.action) {
1746
+ 0 => {},
1747
+ 1 => options.issue_link_base = value.string,
1748
+ 255 => return self.failNode(node, value_programs.message(admission.code)),
1749
+ else => @panic("invalid issue link base capability"),
1750
+ }
1751
+ },
1752
+ 200 => return Md.view(ui, source_text.?, options),
1753
+ 255 => return self.failNode(if (step.fields[0] == 0) node else node.children[step.fields[0] - 1], specialized.message(step.code)),
1754
+ else => @panic("invalid specialized builder capability"),
1755
+ };
1756
+ @panic("specialized program did not publish");
1757
+ }
1758
+
1759
+ fn buildCodeOwned(self: *Self, owner: markup.expr.Policy, ui: *Ui, scope: *Scope, node: markup.MarkupNode) BuildError!Ui.Node {
1760
+ var options: Ui.CodeOptions = .{};
1761
+ var source_text: ?[]const u8 = null;
1762
+ var added_lines_storage: [canvas.code.max_diff_lines]usize = undefined;
1763
+ var removed_lines_storage: [canvas.code.max_diff_lines]usize = undefined;
1764
+
1765
+ var request = specialized.Request.init(3);
1766
+ defer request.deinit();
1767
+ request.node(node, true);
1768
+ var program = try request.program(owner);
1769
+ defer program.deinit();
1770
+
1771
+ while (program.next()) |step| switch (step.action) {
1772
+ 1 => {
1773
+ const attribute = node.attrs[step.fields[0]];
1774
+ const typed = markup.attrTypedWithPolicy(owner, attribute);
1775
+ _ = try self.specializedFormOwned(owner, node, 6, typed);
1776
+ source_text = try self.specializedTextOwned(owner, node, 1, try self.evalBinding(scope, node, typed.binding, true));
1777
+ },
1778
+ 2 => {
1779
+ const attribute = node.attrs[step.fields[0]];
1780
+ const typed = markup.attrTypedWithPolicy(owner, attribute);
1781
+ _ = try self.specializedFormOwned(owner, node, 7, typed);
1782
+ options.language = value_programs.language(owner, typed.literal) orelse
1783
+ return self.failNode(node, markup.code_language_message);
1784
+ },
1785
+ 3 => {
1786
+ const attribute = node.attrs[step.fields[0]];
1787
+ options.line_numbers = try self.presenceFlagOwned(owner, scope, node, attribute);
1788
+ },
1789
+ 4 => {
1790
+ const attribute = node.attrs[step.fields[0]];
1791
+ const spec = try self.codeDiffSpecOwned(owner, scope, node, attribute);
1792
+ options.added_lines = value_programs.lineNumbers(owner, spec, &added_lines_storage) orelse
1793
+ return self.failNode(node, markup.code_diff_lines_message);
1794
+ },
1795
+ 5 => {
1796
+ const attribute = node.attrs[step.fields[0]];
1797
+ const spec = try self.codeDiffSpecOwned(owner, scope, node, attribute);
1798
+ options.removed_lines = value_programs.lineNumbers(owner, spec, &removed_lines_storage) orelse
1799
+ return self.failNode(node, markup.code_diff_lines_message);
1800
+ },
1801
+ 6 => {
1802
+ const attribute = node.attrs[step.fields[0]];
1803
+ options.editable = try self.presenceFlagOwned(owner, scope, node, attribute);
1804
+ },
1805
+ 7 => {
1806
+ const attribute = node.attrs[step.fields[0]];
1807
+ const typed = markup.attrTypedWithPolicy(owner, attribute);
1808
+ _ = try self.specializedFormOwned(owner, node, 8, typed);
1809
+ options.on_input = inputConstructorOwned(owner, typed.message.tag) orelse {
1810
+ return self.failNode(node, "on-input tag must carry a TextInputEvent payload");
1811
+ };
1812
+ },
1813
+ 8 => {
1814
+ const attribute = node.attrs[step.fields[0]];
1815
+ options.wrap = try self.presenceFlagOwned(owner, scope, node, attribute);
1816
+ },
1817
+ 9 => {
1818
+ const attribute = node.attrs[step.fields[0]];
1819
+ options.width = try self.floatAttr(scope, node, attribute);
1820
+ },
1821
+ 10 => {
1822
+ const attribute = node.attrs[step.fields[0]];
1823
+ options.height = try self.floatAttr(scope, node, attribute);
1824
+ },
1825
+ 11 => {
1826
+ const attribute = node.attrs[step.fields[0]];
1827
+ options.min_width = try self.floatAttr(scope, node, attribute);
1828
+ },
1829
+ 12 => {
1830
+ const attribute = node.attrs[step.fields[0]];
1831
+ options.grow = try self.floatAttr(scope, node, attribute);
1832
+ },
1833
+ 13 => {
1834
+ const attribute = node.attrs[step.fields[0]];
1835
+ options.key = try self.attrKey(scope, node, attribute);
1836
+ },
1837
+ 14 => {
1838
+ const attribute = node.attrs[step.fields[0]];
1839
+ options.global_key = try self.attrKey(scope, node, attribute);
1840
+ },
1841
+ 15 => {
1842
+ const attribute = node.attrs[step.fields[0]];
1843
+ options.semantics.label = try self.stringAttr(scope, node, attribute, "label expects text");
1844
+ },
1845
+ 200 => return ui.code(options, source_text.?),
1846
+ 255 => return self.failNode(if (step.fields[0] == 0) node else node.children[step.fields[0] - 1], specialized.message(step.code)),
1847
+ else => @panic("invalid specialized builder capability"),
1848
+ };
1849
+ @panic("specialized program did not publish");
1850
+ }
1851
+
1852
+ fn buildStepperOwned(self: *Self, owner: markup.expr.Policy, ui: *Ui, scope: *Scope, node: markup.MarkupNode) BuildError!Ui.Node {
1853
+ var options: Ui.StepperOptions = .{};
1854
+
1855
+ var request = specialized.Request.init(4);
1856
+ defer request.deinit();
1857
+ request.node(node, true);
1858
+ var program = try request.program(owner);
1859
+ defer program.deinit();
1860
+
1861
+ while (program.next()) |step| switch (step.action) {
1862
+ 1 => {
1863
+ const attribute = node.attrs[step.fields[0]];
1864
+ const value = try self.evalAttrExpression(scope, node, attribute);
1865
+ const admission = value_programs.integerOption(owner, 1, value, 0);
1866
+ options.active = switch (admission.action) {
1867
+ 1 => @intCast(value.integer),
1868
+ 2 => 0,
1869
+ 255 => return self.failNode(node, value_programs.message(admission.code)),
1870
+ else => @panic("invalid stepper integer capability"),
1871
+ };
1872
+ },
1873
+ 2 => {
1874
+ const attribute = node.attrs[step.fields[0]];
1875
+ options.key = try self.attrKey(scope, node, attribute);
1876
+ },
1877
+ 3 => {
1878
+ const attribute = node.attrs[step.fields[0]];
1879
+ options.global_key = try self.attrKey(scope, node, attribute);
1880
+ },
1881
+ 4 => {
1882
+ const attribute = node.attrs[step.fields[0]];
1883
+ options.semantics.label = try self.stringAttr(scope, node, attribute, "label expects text");
1884
+ },
1885
+ 100 => return self.buildStepperStepsOwned(owner, ui, scope, node, options),
1886
+ 255 => return self.failNode(if (step.fields[0] == 0) node else node.children[step.fields[0] - 1], specialized.message(step.code)),
1887
+ else => @panic("invalid specialized builder capability"),
1888
+ };
1889
+ @panic("specialized program did not publish");
1890
+ }
1891
+
1892
+ fn buildInputGroupOwned(self: *Self, owner: markup.expr.Policy, ui: *Ui, scope: *Scope, node: markup.MarkupNode) BuildError!Ui.Node {
1893
+ var options: Ui.InputGroupOptions = .{};
1894
+
1895
+ var request = specialized.Request.init(5);
1896
+ defer request.deinit();
1897
+ request.node(node, true);
1898
+ var program = try request.program(owner);
1899
+ defer program.deinit();
1900
+ var entry: ?Ui.Node = null;
1901
+ var actions: ?Ui.Node = null;
1902
+ while (program.next()) |step| switch (step.action) {
1903
+ 1 => {
1904
+ const attribute = node.attrs[step.fields[0]];
1905
+ options.semantics.label = try self.stringAttr(scope, node, attribute, "label expects text");
1906
+ },
1907
+ 2 => {
1908
+ const attribute = node.attrs[step.fields[0]];
1909
+ options.width = try self.floatAttr(scope, node, attribute);
1910
+ },
1911
+ 3 => {
1912
+ const attribute = node.attrs[step.fields[0]];
1913
+ options.height = try self.floatAttr(scope, node, attribute);
1914
+ },
1915
+ 4 => {
1916
+ const attribute = node.attrs[step.fields[0]];
1917
+ options.min_width = try self.floatAttr(scope, node, attribute);
1918
+ },
1919
+ 5 => {
1920
+ const attribute = node.attrs[step.fields[0]];
1921
+ options.grow = try self.floatAttr(scope, node, attribute);
1922
+ },
1923
+ 6 => {
1924
+ const attribute = node.attrs[step.fields[0]];
1925
+ options.key = try self.attrKey(scope, node, attribute);
1926
+ },
1927
+ 7 => {
1928
+ const attribute = node.attrs[step.fields[0]];
1929
+ options.global_key = try self.attrKey(scope, node, attribute);
1930
+ },
1931
+ 100 => entry = try self.buildElement(ui, scope, node.children[step.fields[0]]),
1932
+ 101 => actions = try self.buildInputGroupActions(ui, scope, node.children[step.fields[0]]),
1933
+ 200 => return ui.inputGroup(options, entry.?, actions),
1934
+ 255 => return self.failNode(if (step.fields[0] == 0) node else node.children[step.fields[0] - 1], specialized.message(step.code)),
1935
+ else => @panic("invalid specialized builder capability"),
1936
+ };
1937
+ @panic("specialized program did not publish");
1938
+ }
1939
+
1940
+ fn buildInputGroupActionsOwned(self: *Self, owner: markup.expr.Policy, ui: *Ui, scope: *Scope, node: markup.MarkupNode) BuildError!Ui.Node {
1941
+ var options: Ui.InputGroupActionsOptions = .{};
1942
+
1943
+ var request = specialized.Request.init(6);
1944
+ defer request.deinit();
1945
+ request.node(node, true);
1946
+ var program = try request.program(owner);
1947
+ defer program.deinit();
1948
+ var children: std.ArrayListUnmanaged(Ui.Node) = .empty;
1949
+ while (program.next()) |step| switch (step.action) {
1950
+ 1 => {
1951
+ const attribute = node.attrs[step.fields[0]];
1952
+ options.gap = try self.floatAttr(scope, node, attribute);
1953
+ },
1954
+ 2 => {
1955
+ const attribute = node.attrs[step.fields[0]];
1956
+ options.key = try self.attrKey(scope, node, attribute);
1957
+ },
1958
+ 3 => {
1959
+ const attribute = node.attrs[step.fields[0]];
1960
+ options.global_key = try self.attrKey(scope, node, attribute);
1961
+ },
1962
+ 100 => try self.buildChildren(ui, scope, node, &children),
1963
+ 200 => return ui.inputGroupActions(options, @as([]const Ui.Node, children.items)),
1964
+ 255 => return self.failNode(if (step.fields[0] == 0) node else node.children[step.fields[0] - 1], specialized.message(step.code)),
1965
+ else => @panic("invalid specialized builder capability"),
1966
+ };
1967
+ @panic("specialized program did not publish");
1968
+ }
1969
+
1970
+ fn buildTimelineOwned(self: *Self, owner: markup.expr.Policy, ui: *Ui, scope: *Scope, node: markup.MarkupNode) BuildError!Ui.Node {
1971
+ var options: Ui.TimelineOptions = .{};
1972
+
1973
+ var request = specialized.Request.init(7);
1974
+ defer request.deinit();
1975
+ request.node(node, true);
1976
+ var program = try request.program(owner);
1977
+ defer program.deinit();
1978
+ var children: std.ArrayListUnmanaged(Ui.Node) = .empty;
1979
+ while (program.next()) |step| switch (step.action) {
1980
+ 1 => {
1981
+ const attribute = node.attrs[step.fields[0]];
1982
+ options.gap = try self.floatAttr(scope, node, attribute);
1983
+ },
1984
+ 2 => {
1985
+ const attribute = node.attrs[step.fields[0]];
1986
+ options.grow = try self.floatAttr(scope, node, attribute);
1987
+ },
1988
+ 3 => {
1989
+ const attribute = node.attrs[step.fields[0]];
1990
+ options.key = try self.attrKey(scope, node, attribute);
1991
+ },
1992
+ 4 => {
1993
+ const attribute = node.attrs[step.fields[0]];
1994
+ options.global_key = try self.attrKey(scope, node, attribute);
1995
+ },
1996
+ 5 => {
1997
+ const attribute = node.attrs[step.fields[0]];
1998
+ options.semantics.label = try self.stringAttr(scope, node, attribute, "label expects text");
1999
+ },
2000
+ 100 => try self.buildChildren(ui, scope, node, &children),
2001
+ 200 => return ui.timeline(options, @as([]const Ui.Node, children.items)),
2002
+ 255 => return self.failNode(if (step.fields[0] == 0) node else node.children[step.fields[0] - 1], specialized.message(step.code)),
2003
+ else => @panic("invalid specialized builder capability"),
2004
+ };
2005
+ @panic("specialized program did not publish");
2006
+ }
2007
+
2008
+ fn buildTimelineItemOwned(self: *Self, owner: markup.expr.Policy, ui: *Ui, scope: *Scope, node: markup.MarkupNode, forwarded_press: ?MsgT) BuildError!Ui.Node {
2009
+ var options: Ui.TimelineItemOptions = .{ .title = "" };
2010
+
2011
+ var request = specialized.Request.init(8);
2012
+ defer request.deinit();
2013
+ request.node(node, true);
2014
+ var program = try request.program(owner);
2015
+ defer program.deinit();
2016
+
2017
+ while (program.next()) |step| switch (step.action) {
2018
+ 1 => {
2019
+ const attribute = node.attrs[step.fields[0]];
2020
+ options.title = try self.stringAttr(scope, node, attribute, markup.timeline_item_text_attr_message);
2021
+ },
2022
+ 2 => {
2023
+ const attribute = node.attrs[step.fields[0]];
2024
+ options.description = try self.stringAttr(scope, node, attribute, markup.timeline_item_text_attr_message);
2025
+ },
2026
+ 3 => {
2027
+ const attribute = node.attrs[step.fields[0]];
2028
+ options.meta = try self.stringAttr(scope, node, attribute, markup.timeline_item_text_attr_message);
2029
+ },
2030
+ 4 => {
2031
+ const attribute = node.attrs[step.fields[0]];
2032
+ options.indicator = try self.stringAttr(scope, node, attribute, markup.timeline_item_text_attr_message);
2033
+ },
2034
+ 5 => {
2035
+ const attribute = node.attrs[step.fields[0]];
2036
+ // Vector icon indicator: the shared icon value grammar,
2037
+ // like every icon attribute.
2038
+ switch (value_programs.iconValue(owner, attribute.value, markup.button_icon_message)) {
2039
+ .builtin, .app => |name| options.icon = name,
2040
+ .binding => options.icon = try self.stringAttr(scope, node, attribute, markup.button_icon_message),
2041
+ .invalid => |message| return self.failVoid(node, message),
2042
+ }
2043
+ },
2044
+ 6 => {
2045
+ const attribute = node.attrs[step.fields[0]];
2046
+ const text = try self.stringAttr(scope, node, attribute, "expected an option name");
2047
+ options.variant = enumValueOwned(canvas.WidgetVariant, owner, text) orelse {
2048
+ return self.failNode(node, "unknown option value");
2049
+ };
2050
+ },
2051
+ 7 => {
2052
+ const attribute = node.attrs[step.fields[0]];
2053
+ options.connector = markup.expr.truthyWithPolicy(scope.expression_policy, try self.evalAttrExpression(scope, node, attribute));
2054
+ },
2055
+ 8 => {
2056
+ const attribute = node.attrs[step.fields[0]];
2057
+ options.selected = markup.expr.truthyWithPolicy(scope.expression_policy, try self.evalAttrExpression(scope, node, attribute));
2058
+ },
2059
+ 9 => {
2060
+ const attribute = node.attrs[step.fields[0]];
2061
+ // Reuse the full message-attr machinery (payload
2062
+ // bindings included) through a scratch options value.
2063
+ var scratch: Ui.ElementOptions = .{};
2064
+ try self.applyMessageAttr(scope, node, &scratch, attribute);
2065
+ options.on_press = scratch.on_press;
2066
+ },
2067
+ 10 => {
2068
+ return self.failNode(node, markup.timeline_item_press_only_message);
2069
+ },
2070
+ 11 => {
2071
+ const attribute = node.attrs[step.fields[0]];
2072
+ options.key = try self.attrKey(scope, node, attribute);
2073
+ },
2074
+ 12 => {
2075
+ const attribute = node.attrs[step.fields[0]];
2076
+ options.global_key = try self.attrKey(scope, node, attribute);
2077
+ },
2078
+ 200 => {
2079
+ if (value_programs.forwardedPress(owner, forwarded_press != null)) options.on_press = forwarded_press.?;
2080
+ return ui.timelineItem(options);
2081
+ },
2082
+ 255 => return self.failNode(if (step.fields[0] == 0) node else node.children[step.fields[0] - 1], specialized.message(step.code)),
2083
+ else => @panic("invalid specialized builder capability"),
2084
+ };
2085
+ @panic("specialized program did not publish");
2086
+ }
2087
+
2088
+ fn buildChartOwned(self: *Self, owner: markup.expr.Policy, ui: *Ui, scope: *Scope, node: markup.MarkupNode) BuildError!Ui.Node {
2089
+ var options: Ui.ChartOptions = .{};
2090
+
2091
+ var request = specialized.Request.init(9);
2092
+ defer request.deinit();
2093
+ request.node(node, true);
2094
+ var program = try request.program(owner);
2095
+ defer program.deinit();
2096
+ var series: []canvas.ChartSeries = &.{};
2097
+ while (program.next()) |step| switch (step.action) {
2098
+ 1 => {
2099
+ return self.failNode(node, markup.chart_display_only_message);
2100
+ },
2101
+ 2 => {
2102
+ const attribute = node.attrs[step.fields[0]];
2103
+ options.y_min = try self.floatAttr(scope, node, attribute);
2104
+ },
2105
+ 3 => {
2106
+ const attribute = node.attrs[step.fields[0]];
2107
+ options.y_max = try self.floatAttr(scope, node, attribute);
2108
+ },
2109
+ 4 => {
2110
+ const attribute = node.attrs[step.fields[0]];
2111
+ const value = try self.evalAttrExpression(scope, node, attribute);
2112
+ const admission = value_programs.integerOption(owner, 2, value, std.math.maxInt(u8));
2113
+ options.grid_lines = switch (admission.action) {
2114
+ 1 => @intCast(value.integer),
2115
+ 255 => return self.failNode(node, value_programs.message(admission.code)),
2116
+ else => @panic("invalid chart integer capability"),
2117
+ };
2118
+ },
2119
+ 5 => {
2120
+ const attribute = node.attrs[step.fields[0]];
2121
+ options.baseline = markup.expr.truthyWithPolicy(scope.expression_policy, try self.evalAttrExpression(scope, node, attribute));
2122
+ },
2123
+ 6 => {
2124
+ const attribute = node.attrs[step.fields[0]];
2125
+ const typed = markup.attrTypedWithPolicy(owner, attribute);
2126
+ _ = try self.specializedFormOwned(owner, node, 9, typed);
2127
+ options.x_labels = try self.stringItems(ui, scope, node, typed.binding, markup.chart_x_labels_message);
2128
+ },
2129
+ 7 => {
2130
+ const attribute = node.attrs[step.fields[0]];
2131
+ options.y_labels = markup.expr.truthyWithPolicy(scope.expression_policy, try self.evalAttrExpression(scope, node, attribute));
2132
+ },
2133
+ 8 => {
2134
+ const attribute = node.attrs[step.fields[0]];
2135
+ options.hover_details = markup.expr.truthyWithPolicy(scope.expression_policy, try self.evalAttrExpression(scope, node, attribute));
2136
+ },
2137
+ 9 => {
2138
+ const attribute = node.attrs[step.fields[0]];
2139
+ options.stroke_width = try self.floatAttr(scope, node, attribute);
2140
+ },
2141
+ 10 => {
2142
+ const attribute = node.attrs[step.fields[0]];
2143
+ options.width = try self.floatAttr(scope, node, attribute);
2144
+ },
2145
+ 11 => {
2146
+ const attribute = node.attrs[step.fields[0]];
2147
+ options.height = try self.floatAttr(scope, node, attribute);
2148
+ },
2149
+ 12 => {
2150
+ const attribute = node.attrs[step.fields[0]];
2151
+ options.grow = try self.floatAttr(scope, node, attribute);
2152
+ },
2153
+ 13 => {
2154
+ const attribute = node.attrs[step.fields[0]];
2155
+ options.padding = try self.floatAttr(scope, node, attribute);
2156
+ },
2157
+ 14 => {
2158
+ const attribute = node.attrs[step.fields[0]];
2159
+ options.key = try self.attrKey(scope, node, attribute);
2160
+ },
2161
+ 15 => {
2162
+ const attribute = node.attrs[step.fields[0]];
2163
+ options.global_key = try self.attrKey(scope, node, attribute);
2164
+ },
2165
+ 16 => {
2166
+ const attribute = node.attrs[step.fields[0]];
2167
+ options.semantics.label = try self.stringAttr(scope, node, attribute, "label expects text");
2168
+ },
2169
+ 100 => series = try ui.arena.alloc(canvas.ChartSeries, node.children.len),
2170
+ 101 => series[step.fields[0]] = try self.buildSeries(ui, scope, node.children[step.fields[0]]),
2171
+ 200 => return ui.chart(options, series),
2172
+ 255 => return self.failNode(if (step.fields[0] == 0) node else node.children[step.fields[0] - 1], specialized.message(step.code)),
2173
+ else => @panic("invalid specialized builder capability"),
2174
+ };
2175
+ @panic("specialized program did not publish");
2176
+ }
2177
+
2178
+ fn buildVideoOwned(self: *Self, owner: markup.expr.Policy, ui: *Ui, scope: *Scope, node: markup.MarkupNode) BuildError!Ui.Node {
2179
+ var options: Ui.VideoOptions = .{};
2180
+
2181
+ var request = specialized.Request.init(10);
2182
+ defer request.deinit();
2183
+ request.node(node, true);
2184
+ var program = try request.program(owner);
2185
+ defer program.deinit();
2186
+
2187
+ while (program.next()) |step| switch (step.action) {
2188
+ 1 => {
2189
+ const attribute = node.attrs[step.fields[0]];
2190
+ options.src = try self.stringAttr(scope, node, attribute, markup.video_src_message);
2191
+ },
2192
+ 2 => {
2193
+ const attribute = node.attrs[step.fields[0]];
2194
+ options.controls = try self.presenceFlagOwned(owner, scope, node, attribute);
2195
+ },
2196
+ 3 => {
2197
+ const attribute = node.attrs[step.fields[0]];
2198
+ options.autoplay = try self.presenceFlagOwned(owner, scope, node, attribute);
2199
+ },
2200
+ 4 => {
2201
+ const attribute = node.attrs[step.fields[0]];
2202
+ options.loop = try self.presenceFlagOwned(owner, scope, node, attribute);
2203
+ },
2204
+ 5 => {
2205
+ const attribute = node.attrs[step.fields[0]];
2206
+ options.muted = try self.presenceFlagOwned(owner, scope, node, attribute);
2207
+ },
2208
+ 6 => {
2209
+ const attribute = node.attrs[step.fields[0]];
2210
+ options.width = try self.floatAttr(scope, node, attribute);
2211
+ },
2212
+ 7 => {
2213
+ const attribute = node.attrs[step.fields[0]];
2214
+ options.height = try self.floatAttr(scope, node, attribute);
2215
+ },
2216
+ 8 => {
2217
+ const attribute = node.attrs[step.fields[0]];
2218
+ options.grow = try self.floatAttr(scope, node, attribute);
2219
+ },
2220
+ 9 => {
2221
+ const attribute = node.attrs[step.fields[0]];
2222
+ options.label = try self.stringAttr(scope, node, attribute, "label expects text");
2223
+ },
2224
+ 10 => {
2225
+ const attribute = node.attrs[step.fields[0]];
2226
+ options.key = try self.attrKey(scope, node, attribute);
2227
+ },
2228
+ 11 => {
2229
+ const attribute = node.attrs[step.fields[0]];
2230
+ options.global_key = try self.attrKey(scope, node, attribute);
2231
+ },
2232
+ 200 => return ui.video(options),
2233
+ 255 => return self.failNode(if (step.fields[0] == 0) node else node.children[step.fields[0] - 1], specialized.message(step.code)),
2234
+ else => @panic("invalid specialized builder capability"),
2235
+ };
2236
+ @panic("specialized program did not publish");
2237
+ }
2238
+
2239
+ fn buildSeriesOwned(self: *Self, owner: markup.expr.Policy, ui: *Ui, scope: *Scope, node: markup.MarkupNode) BuildError!canvas.ChartSeries {
2240
+ var series = canvas.ChartSeries{};
156
2241
 
157
- fn lookup(self: *const Scope, head: []const u8) ?*const ScopeEntry {
158
- var index = self.len;
159
- while (index > self.floor) {
160
- index -= 1;
161
- if (std.mem.eql(u8, self.entries[index].name, head)) return &self.entries[index];
162
- }
163
- return null;
164
- }
2242
+ var request = specialized.Request.init(11);
2243
+ defer request.deinit();
2244
+ request.node(node, true);
2245
+ var program = try request.program(owner);
2246
+ defer program.deinit();
165
2247
 
166
- /// The innermost slot capture visible to the current template
167
- /// body (never looks below the floor: an inner template with
168
- /// no use-site children must not see an outer capture).
169
- fn slotCapture(self: *const Scope) ?SlotCapture {
170
- var index = self.len;
171
- while (index > self.floor) {
172
- index -= 1;
173
- if (self.entries[index].payload == .slot) return self.entries[index].payload.slot;
174
- }
175
- return null;
176
- }
177
- };
2248
+ while (program.next()) |step| switch (step.action) {
2249
+ 1 => {
2250
+ const attribute = node.attrs[step.fields[0]];
2251
+ // Closed literal vocabulary: area is the markup
2252
+ // spelling of a filled line; band stays with the
2253
+ // builder (it needs a paired lower edge).
2254
+ const typed = markup.attrTypedWithPolicy(owner, attribute);
2255
+ _ = try self.specializedFormOwned(owner, node, 10, typed);
2256
+ const selected = value_programs.seriesKind(owner, typed.literal) orelse return self.failVoid(node, markup.series_kind_message);
2257
+ series.kind = selected.kind;
2258
+ // A later kind attribute does not clear an earlier area fill.
2259
+ if (selected.fill) series.fill = true;
2260
+ },
2261
+ 2 => {
2262
+ const attribute = node.attrs[step.fields[0]];
2263
+ const typed = markup.attrTypedWithPolicy(owner, attribute);
2264
+ _ = try self.specializedFormOwned(owner, node, 11, typed);
2265
+ series.values = try self.f32Items(ui, scope, node, typed.binding);
2266
+ },
2267
+ 3 => {
2268
+ const attribute = node.attrs[step.fields[0]];
2269
+ const typed = markup.attrTypedWithPolicy(owner, attribute);
2270
+ _ = try self.specializedFormOwned(owner, node, 12, typed);
2271
+ series.color = enumValueOwned(canvas.ChartSeriesColor, owner, typed.literal) orelse {
2272
+ return self.failVoid(node, markup.series_color_message);
2273
+ };
2274
+ },
2275
+ 4 => {
2276
+ const attribute = node.attrs[step.fields[0]];
2277
+ series.label = try self.stringAttr(scope, node, attribute, markup.series_label_message);
2278
+ },
2279
+ 200 => return series,
2280
+ 255 => return self.failVoid(if (step.fields[0] == 0) node else node.children[step.fields[0] - 1], specialized.message(step.code)),
2281
+ else => @panic("invalid specialized builder capability"),
2282
+ };
2283
+ @panic("specialized program did not publish");
2284
+ }
178
2285
 
179
2286
  // ------------------------------------------------------ building
180
2287
 
181
2288
  fn buildNode(self: *Self, ui: *Ui, scope: *Scope, node: markup.MarkupNode) BuildError!Ui.Node {
2289
+ if (scope.expression_policy) |owner| return self.buildNodeOwned(owner, ui, scope, node);
2290
+ return self.buildNodeReference(ui, scope, node);
2291
+ }
2292
+
2293
+ fn buildNodeReference(self: *Self, ui: *Ui, scope: *Scope, node: markup.MarkupNode) BuildError!Ui.Node {
182
2294
  switch (node.kind) {
183
2295
  .element => return self.buildElement(ui, scope, node),
184
2296
  .use_block => return self.buildUse(ui, scope, node),
@@ -200,6 +2312,51 @@ pub fn MarkupView(comptime ModelT: type, comptime MsgT: type) type {
200
2312
  }
201
2313
 
202
2314
  fn buildElementForwardedPress(self: *Self, ui: *Ui, scope: *Scope, node: markup.MarkupNode, forwarded_press: ?MsgT) BuildError!Ui.Node {
2315
+ if (scope.expression_policy) |owner| return self.buildElementForwardedPressOwned(owner, ui, scope, node, forwarded_press);
2316
+ return self.buildElementForwardedPressReference(ui, scope, node, forwarded_press);
2317
+ }
2318
+
2319
+ fn buildElementForwardedPressOwned(self: *Self, owner: markup.expr.Policy, ui: *Ui, scope: *Scope, node: markup.MarkupNode, forwarded_press: ?MsgT) BuildError!Ui.Node {
2320
+ var request = value_programs.Request.init(33);
2321
+ defer request.deinit();
2322
+ request.words(&.{ @intFromBool(forwarded_press != null), @intFromBool(ui.provenance_sink != null) });
2323
+ var program = try request.program(owner);
2324
+ defer program.deinit();
2325
+ var built: ?Ui.Node = null;
2326
+ while (program.next()) |step| switch (step.action) {
2327
+ 88 => built = try self.buildElementInner(ui, scope, node, forwarded_press),
2328
+ 89 => {
2329
+ built.?.on_press = forwarded_press.?;
2330
+ built.?.widget.semantics.focusable = true;
2331
+ },
2332
+ 90 => {
2333
+ const source = try ui.arena.create(ui_provenance.NodeSource);
2334
+ source.* = .{ .src_path = node.src_path, .span = node.span, .line = node.line, .column = node.column, .chain = scope.source_chain };
2335
+ built.?.source = source;
2336
+ },
2337
+ 91 => {
2338
+ if (program.next() != null) @panic("trailing element postprocessing capability");
2339
+ return built orelse @panic("element postprocessing did not build a node");
2340
+ },
2341
+ else => @panic("invalid element postprocessing capability"),
2342
+ };
2343
+ @panic("incomplete element postprocessing program");
2344
+ }
2345
+
2346
+ fn lowerTabsTriggersOwned(owner: markup.expr.Policy, children: []Ui.Node) BuildError!void {
2347
+ var request = value_programs.Request.init(34);
2348
+ defer request.deinit();
2349
+ request.word(children.len);
2350
+ for (children) |child| request.blob(@tagName(child.widget.kind));
2351
+ var program = try request.program(owner);
2352
+ defer program.deinit();
2353
+ while (program.next()) |step| switch (step.action) {
2354
+ 92 => children[step.fields[0]].widget.kind = .segmented_control,
2355
+ else => @panic("invalid tab trigger capability"),
2356
+ };
2357
+ }
2358
+
2359
+ fn buildElementForwardedPressReference(self: *Self, ui: *Ui, scope: *Scope, node: markup.MarkupNode, forwarded_press: ?MsgT) BuildError!Ui.Node {
203
2360
  var built = try self.buildElementInner(ui, scope, node, forwarded_press);
204
2361
  if (forwarded_press) |msg| {
205
2362
  // A press declared on <use> belongs to the expanded root.
@@ -222,7 +2379,289 @@ pub fn MarkupView(comptime ModelT: type, comptime MsgT: type) type {
222
2379
  return built;
223
2380
  }
224
2381
 
2382
+ fn buildElementInnerOwned(self: *Self, owner: markup.expr.Policy, ui: *Ui, scope: *Scope, node: markup.MarkupNode, forwarded_press: ?MsgT) BuildError!Ui.Node {
2383
+ var lookup = elements.Request.init(0);
2384
+ defer lookup.deinit();
2385
+ lookup.blob(node.name);
2386
+ lookup.word(element_kind_table.len);
2387
+ for (element_kind_table) |entry| lookup.blob(entry.name);
2388
+ const route = lookup.decision(owner);
2389
+ if (route.action == 255) return self.failNode(node, elements.message(route.code));
2390
+ if (route.action == 1) return switch (route.fields[0]) {
2391
+ 0 => self.buildVirtualWindow(ui, scope, node),
2392
+ 1 => self.buildVirtualList(ui, scope, node),
2393
+ 2 => self.buildMarkdown(ui, scope, node),
2394
+ 3 => self.buildCode(ui, scope, node),
2395
+ 4 => self.buildStepper(ui, scope, node),
2396
+ 5 => self.buildTimeline(ui, scope, node),
2397
+ 6 => self.buildTimelineItem(ui, scope, node, forwarded_press),
2398
+ 7 => self.buildChart(ui, scope, node),
2399
+ 8 => self.buildVideo(ui, scope, node),
2400
+ 9 => self.buildInputGroup(ui, scope, node),
2401
+ else => @panic("invalid specialized element capability"),
2402
+ };
2403
+ if (route.action != 0 or route.fields[0] >= element_kind_table.len) @panic("invalid element schema selection");
2404
+ const kind = element_kind_table[route.fields[0]].kind;
2405
+ const schema = markup.schema.elementByName(node.name).?;
2406
+ var request = elements.Request.init(1);
2407
+ defer request.deinit();
2408
+ request.node(node, true);
2409
+ request.blob(@tagName(kind));
2410
+ request.words(&.{ @intFromBool(canvas.widgetKindHitTarget(kind)), @intFromBool(canvas.widgetKindStacksChildren(kind)), @intFromBool(elementTakesText(kind)), @intFromBool(elementTakesChildren(kind)) });
2411
+ request.word(switch (schema.a11y_name) {
2412
+ .none => 0,
2413
+ .image => 1,
2414
+ .control => 2,
2415
+ .editable => 3,
2416
+ .radiogroup => 4,
2417
+ });
2418
+ request.words(&.{ @intFromBool(schema.takes_text), @intFromBool(markup.schema.elementHoldsChildren(schema)) });
2419
+ request.names(&markup.known_non_hit_target_element_names);
2420
+ var dead_count: usize = 0;
2421
+ for (markup.schema.events) |entry| if (entry.dead_on_non_hit_target) {
2422
+ dead_count += 1;
2423
+ };
2424
+ request.word(dead_count);
2425
+ for (markup.schema.events) |entry| if (entry.dead_on_non_hit_target) {
2426
+ request.blob(entry.name);
2427
+ };
2428
+ request.names(&markup.overflow_value_names);
2429
+ request.names(&markup.known_text_size_value_names);
2430
+ request.names(&markup.known_control_size_value_names);
2431
+ request.names(&markup.schema.role_names);
2432
+ request.names(&markup.schema.container_role_names);
2433
+ var program = try request.program(owner);
2434
+ defer program.deinit();
2435
+ var inner = node;
2436
+ var options: Ui.ElementOptions = .{};
2437
+ var children: std.ArrayListUnmanaged(Ui.Node) = .empty;
2438
+ var built: ?Ui.Node = null;
2439
+ while (program.next()) |step| switch (step.action) {
2440
+ 10, 11 => {
2441
+ const child = node.children[step.fields[0]];
2442
+ const info = shape_programs.shape(owner, child, if (step.action == 10) 0 else 2);
2443
+ if (info) |diagnostic| {
2444
+ self.diagnostic = .{ .line = diagnostic.line, .column = diagnostic.column, .message = diagnostic.message, .path = diagnostic.path };
2445
+ return error.MarkupBuild;
2446
+ }
2447
+ },
2448
+ 12 => {
2449
+ const filtered = try ui.arena.alloc(markup.MarkupNode, node.children.len);
2450
+ var length: usize = 0;
2451
+ for (node.children, 0..) |child, i| {
2452
+ if (i + 1 == step.fields[0] or i + 1 == step.fields[1]) continue;
2453
+ filtered[length] = child;
2454
+ length += 1;
2455
+ }
2456
+ inner.children = filtered[0..length];
2457
+ },
2458
+ 15 => try self.applyAttrs(scope, node, &options),
2459
+ 16 => {
2460
+ var menu_children: std.ArrayListUnmanaged(Ui.Node) = .empty;
2461
+ try self.buildChildList(ui, scope, node.children[step.fields[0]].children, &menu_children);
2462
+ options.context_menu = ui.contextMenuItemsFromNodes(menu_children.items);
2463
+ },
2464
+ 20 => {
2465
+ const name_attr = node.attrs[step.fields[0]];
2466
+ switch (value_programs.iconValue(owner, name_attr.value, markup.icon_name_message)) {
2467
+ .builtin => |name| {
2468
+ var icon = ui.el(kind, options, .{});
2469
+ icon.widget.text = name;
2470
+ return icon;
2471
+ },
2472
+ .app => |spelled| options.icon = spelled,
2473
+ .binding => options.icon = try self.stringAttr(scope, node, name_attr, markup.icon_name_message),
2474
+ .invalid => |message| return self.failNode(node, message),
2475
+ }
2476
+ return ui.el(kind, options, .{});
2477
+ },
2478
+ 21 => {
2479
+ var text = ui.el(kind, options, .{});
2480
+ if (step.fields[0] != 0) text.widget.text = try self.interpolatedText(ui, scope, inner);
2481
+ if (step.fields[1] != 0) text.widget.image_fit = .cover;
2482
+ return text;
2483
+ },
2484
+ 22 => try self.buildChildren(ui, scope, inner, &children),
2485
+ 23 => try lowerTabsTriggersOwned(owner, children.items),
2486
+ 24 => built = ui.el(kind, options, @as([]const Ui.Node, children.items)),
2487
+ 25 => {
2488
+ const pill = node.children[step.fields[0]];
2489
+ built.?.widget.text = try self.interpolatedText(ui, scope, pill);
2490
+ built.?.widget.text_alignment = if (pill.attr("text-alignment")) |raw|
2491
+ enumValueOwned(canvas.TextAlign, owner, raw) orelse return self.failNode(pill, markup.reactions_alignment_value_message)
2492
+ else
2493
+ .end;
2494
+ },
2495
+ 26 => return self.buildSpanParagraph(ui, scope, inner, options),
2496
+ 255 => return self.failNode(if (step.fields[0] == 0) node else node.children[step.fields[0] - 1], elements.message(step.code)),
2497
+ else => @panic("invalid element construction capability"),
2498
+ };
2499
+ return built orelse @panic("element program did not publish a node");
2500
+ }
2501
+
2502
+ fn applyAttrsOwned(self: *Self, owner: markup.expr.Policy, scope: *Scope, node: markup.MarkupNode, options: *Ui.ElementOptions) BuildError!void {
2503
+ var request = elements.Request.init(2);
2504
+ defer request.deinit();
2505
+ request.node(node, false);
2506
+ request.word(color_style_attr_fields.len);
2507
+ for (color_style_attr_fields) |entry| request.blob(entry.markup);
2508
+ request.word(attr_names.len);
2509
+ for (attr_names) |entry| request.blob(entry.markup);
2510
+ var program = try request.program(owner);
2511
+ defer program.deinit();
2512
+ while (program.next()) |step| {
2513
+ if (step.action == 255) return self.failVoid(node, elements.message(step.code));
2514
+ if (step.action == 20) {
2515
+ try self.applyImageSourceAttrsOwned(owner, scope, node, options);
2516
+ continue;
2517
+ }
2518
+ const attribute = node.attrs[step.fields[0]];
2519
+ switch (step.action) {
2520
+ 1 => try self.applyMessageAttr(scope, node, options, attribute),
2521
+ 2 => options.key = try self.attrKey(scope, node, attribute),
2522
+ 3 => options.global_key = try self.attrKey(scope, node, attribute),
2523
+ 4 => options.semantics.focusable = switch (try self.evalAttrExpression(scope, node, attribute)) {
2524
+ .boolean => |flag| flag,
2525
+ else => return self.failVoid(node, "focusable expects a boolean"),
2526
+ },
2527
+ 5 => {
2528
+ const value = try self.evalAttrExpression(scope, node, attribute);
2529
+ const text = switch (value) {
2530
+ .string => |text| text,
2531
+ else => return self.failVoid(node, "role expects a role name"),
2532
+ };
2533
+ options.semantics.role = enumValueOwned(canvas.WidgetRole, owner, text) orelse return self.failVoid(node, "unknown role");
2534
+ },
2535
+ 6 => options.semantics.label = switch (try self.evalAttrExpression(scope, node, attribute)) {
2536
+ .string => |text| text,
2537
+ else => return self.failVoid(node, "label expects text"),
2538
+ },
2539
+ 7 => try self.applyImageAttr(scope, node, options, attribute),
2540
+ 8 => try self.applySurfaceAttr(scope, node, options, attribute),
2541
+ 9 => try self.applyPtyAttr(scope, node, options, attribute),
2542
+ 10 => try self.applyButtonIconAttr(scope, node, options, attribute),
2543
+ 11 => try self.applyAnchorOwned(owner, node, options, attribute.value, 0),
2544
+ 12 => try self.applyAnchorOwned(owner, node, options, attribute.value, 1),
2545
+ 13 => try self.applyAnchorOwned(owner, node, options, attribute.value, 2),
2546
+ 14 => try self.applyQuietHoverAttr(scope, node, options, attribute),
2547
+ 15 => {
2548
+ const literal = markup.attrTypedWithPolicy(owner, attribute).literal;
2549
+ if (step.fields[1] == color_style_attr_fields.len) {
2550
+ options.style_tokens.radius = enumValueOwned(canvas.RadiusTokenName, owner, literal) orelse return self.failVoid(node, markup.unknown_radius_token_message);
2551
+ } else {
2552
+ var found = false;
2553
+ inline for (color_style_attr_fields, 0..) |entry, i| if (step.fields[1] == i) {
2554
+ @field(options.style_tokens, entry.zig) = enumValueOwned(canvas.ColorTokenName, owner, literal) orelse return self.failVoid(node, markup.unknown_color_token_message);
2555
+ found = true;
2556
+ };
2557
+ if (!found) @panic("invalid style reflection capability");
2558
+ }
2559
+ },
2560
+ 16 => {
2561
+ var found = false;
2562
+ inline for (attr_names, 0..) |entry, i| if (step.fields[1] == i) {
2563
+ try self.setOptionField(scope, node, options, entry.zig, attribute);
2564
+ found = true;
2565
+ };
2566
+ if (!found) @panic("invalid option reflection capability");
2567
+ },
2568
+ else => @panic("invalid attribute capability"),
2569
+ }
2570
+ }
2571
+ }
2572
+
2573
+ fn applyMessageAttrOwned(self: *Self, owner: markup.expr.Policy, scope: *Scope, node: markup.MarkupNode, options: *Ui.ElementOptions, attribute: markup.MarkupAttr) BuildError!void {
2574
+ const typed = markup.attrTypedWithPolicy(owner, attribute);
2575
+ const expression = if (typed == .message) typed.message else markup.MessageExpression{ .tag = "", .payload = "" };
2576
+ var request = elements.Request.init(3);
2577
+ defer request.deinit();
2578
+ request.word(@intFromBool(typed == .message));
2579
+ request.blob(attribute.name[3..]);
2580
+ request.blob(node.name);
2581
+ request.word(@intFromBool(expression.payload.len != 0));
2582
+ request.names(&markup.known_dismiss_element_names);
2583
+ var program = try request.program(owner);
2584
+ defer program.deinit();
2585
+ var msg: MsgT = undefined;
2586
+ while (program.next()) |step| switch (step.action) {
2587
+ 1 => {
2588
+ options.on_input = inputConstructorOwned(owner, expression.tag) orelse return self.failVoid(node, "on-input tag must carry a TextInputEvent payload");
2589
+ return;
2590
+ },
2591
+ 2 => {
2592
+ options.on_scroll = scrollConstructorOwned(owner, expression.tag) orelse {
2593
+ if (legacyScrollTagOwned(owner, expression.tag)) return self.failVoid(node, markup.on_scroll_legacy_payload_message);
2594
+ return self.failVoid(node, markup.on_scroll_payload_message);
2595
+ };
2596
+ return;
2597
+ },
2598
+ 3 => {
2599
+ options.on_terminal = terminalConstructorOwned(owner, expression.tag) orelse return self.failVoid(node, markup.on_terminal_payload_message);
2600
+ return;
2601
+ },
2602
+ 4 => {
2603
+ options.on_resize = resizeConstructorOwned(owner, expression.tag) orelse return self.failVoid(node, markup.on_resize_payload_message);
2604
+ return;
2605
+ },
2606
+ 5 => {
2607
+ options.on_drag = try self.constructDragMessage(scope, node, expression);
2608
+ return;
2609
+ },
2610
+ 6 => if (valueConstructorOwned(owner, expression.tag)) |make| {
2611
+ options.on_value = make;
2612
+ return;
2613
+ },
2614
+ 7 => msg = try self.constructMessage(scope, node, expression),
2615
+ 8 => switch (step.fields[0]) {
2616
+ 0 => options.on_press = msg,
2617
+ 1 => options.on_double_press = msg,
2618
+ 2 => options.on_toggle = msg,
2619
+ 3 => options.on_change = msg,
2620
+ 4 => options.on_submit = msg,
2621
+ 5 => options.on_dismiss = msg,
2622
+ 6 => options.on_hold = msg,
2623
+ 7 => options.on_hover_enter = msg,
2624
+ 8 => options.on_hover_leave = msg,
2625
+ 9 => options.on_reach_start = msg,
2626
+ 10 => options.on_reach_end = msg,
2627
+ else => @panic("invalid event storage capability"),
2628
+ },
2629
+ 255 => return self.failVoid(node, elements.message(step.code)),
2630
+ else => @panic("invalid event capability"),
2631
+ };
2632
+ }
2633
+
2634
+ fn attrKeyOwned(self: *Self, owner: markup.expr.Policy, scope: *Scope, node: markup.MarkupNode, attribute: markup.MarkupAttr) BuildError!?canvas.UiKey {
2635
+ const typed = markup.attrTypedWithPolicy(owner, attribute);
2636
+ var defaulted = false;
2637
+ var bare = false;
2638
+ if (typed == .binding) {
2639
+ const parts = value_programs.bindingPath(owner, typed.binding);
2640
+ if (scope.lookup(parts.head)) |entry| defaulted = entry.defaulted;
2641
+ bare = parts.tail == null;
2642
+ }
2643
+ const value = try self.evalAttrExpression(scope, node, attribute);
2644
+ const step = elements.decide(owner, 4, &.{ @intFromBool(typed == .binding), @intFromBool(defaulted), @intFromBool(bare), switch (value) {
2645
+ .integer => 0,
2646
+ .float => 1,
2647
+ .string => 2,
2648
+ .boolean => 3,
2649
+ }, @intFromBool(value == .string and value.string.len == 0) });
2650
+ return switch (step.action) {
2651
+ 0 => null,
2652
+ 1 => canvas.uiKey(@as(u64, @bitCast(value.integer))),
2653
+ 2 => canvas.uiKey(value.string),
2654
+ 255 => self.failKey(node, elements.message(step.code)),
2655
+ else => @panic("invalid key capability"),
2656
+ };
2657
+ }
2658
+
225
2659
  fn buildElementInner(self: *Self, ui: *Ui, scope: *Scope, node: markup.MarkupNode, forwarded_press: ?MsgT) BuildError!Ui.Node {
2660
+ if (scope.expression_policy) |owner| return self.buildElementInnerOwned(owner, ui, scope, node, forwarded_press);
2661
+ return self.buildElementInnerReference(ui, scope, node, forwarded_press);
2662
+ }
2663
+
2664
+ fn buildElementInnerReference(self: *Self, ui: *Ui, scope: *Scope, node: markup.MarkupNode, forwarded_press: ?MsgT) BuildError!Ui.Node {
226
2665
  if (std.mem.eql(u8, node.name, "virtual-window")) return self.buildVirtualWindow(ui, scope, node);
227
2666
  if (std.mem.eql(u8, node.name, "virtual-list")) return self.buildVirtualList(ui, scope, node);
228
2667
  if (std.mem.eql(u8, node.name, "markdown")) {
@@ -576,6 +3015,11 @@ pub fn MarkupView(comptime ModelT: type, comptime MsgT: type) type {
576
3015
  }
577
3016
 
578
3017
  fn buildChildList(self: *Self, ui: *Ui, scope: *Scope, children: []const markup.MarkupNode, out: *std.ArrayListUnmanaged(Ui.Node)) BuildError!void {
3018
+ if (scope.expression_policy) |owner| return self.buildChildListOwned(owner, ui, scope, children, out);
3019
+ return self.buildChildListReference(ui, scope, children, out);
3020
+ }
3021
+
3022
+ fn buildChildListReference(self: *Self, ui: *Ui, scope: *Scope, children: []const markup.MarkupNode, out: *std.ArrayListUnmanaged(Ui.Node)) BuildError!void {
579
3023
  var index: usize = 0;
580
3024
  while (index < children.len) : (index += 1) {
581
3025
  const child = children[index];
@@ -608,7 +3052,7 @@ pub fn MarkupView(comptime ModelT: type, comptime MsgT: type) type {
608
3052
  else_node = children[index + 1];
609
3053
  index += 1;
610
3054
  }
611
- if (condition.truthy()) {
3055
+ if (markup.expr.truthyWithPolicy(scope.expression_policy, condition)) {
612
3056
  try self.buildChildren(ui, scope, child, out);
613
3057
  } else if (else_node) |else_block| {
614
3058
  try self.buildChildren(ui, scope, else_block, out);
@@ -629,6 +3073,11 @@ pub fn MarkupView(comptime ModelT: type, comptime MsgT: type) type {
629
3073
  /// shared), so structural ids and bindings behave exactly as if
630
3074
  /// the content were built at the use site and inserted here.
631
3075
  fn buildSlot(self: *Self, ui: *Ui, scope: *Scope, node: markup.MarkupNode, out: *std.ArrayListUnmanaged(Ui.Node)) BuildError!void {
3076
+ if (scope.expression_policy) |owner| return self.buildSlotOwned(owner, ui, scope, node, out);
3077
+ return self.buildSlotReference(ui, scope, node, out);
3078
+ }
3079
+
3080
+ fn buildSlotReference(self: *Self, ui: *Ui, scope: *Scope, node: markup.MarkupNode, out: *std.ArrayListUnmanaged(Ui.Node)) BuildError!void {
632
3081
  if (node.attrs.len > 0) return self.failVoid(node, markup.slot_attrs_message);
633
3082
  if (node.children.len > 0) return self.failVoid(node.children[0], markup.slot_children_message);
634
3083
  const capture = scope.slotCapture() orelse {
@@ -684,6 +3133,11 @@ pub fn MarkupView(comptime ModelT: type, comptime MsgT: type) type {
684
3133
  }
685
3134
 
686
3135
  fn buildVirtualWindow(self: *Self, ui: *Ui, scope: *Scope, node: markup.MarkupNode) BuildError!Ui.Node {
3136
+ if (scope.expression_policy) |owner| return self.buildVirtualWindowOwned(owner, ui, scope, node);
3137
+ return self.buildVirtualWindowReference(ui, scope, node);
3138
+ }
3139
+
3140
+ fn buildVirtualWindowReference(self: *Self, ui: *Ui, scope: *Scope, node: markup.MarkupNode) BuildError!Ui.Node {
687
3141
  @setEvalBranchQuota(scan_quota);
688
3142
  for (scope.entries[scope.floor..scope.len]) |entry| if (entry.payload == .virtual) return self.failNode(node, "virtual-window declarations cannot nest");
689
3143
  if (scope.len >= max_scope_depth or node.children.len != 1 or (node.children[0].kind != .element and node.children[0].kind != .use_block)) return self.failNode(node, "virtual-window requires one root child");
@@ -722,6 +3176,11 @@ pub fn MarkupView(comptime ModelT: type, comptime MsgT: type) type {
722
3176
  }
723
3177
 
724
3178
  fn buildVirtualList(self: *Self, ui: *Ui, scope: *Scope, node: markup.MarkupNode) BuildError!Ui.Node {
3179
+ if (scope.expression_policy) |owner| return self.buildVirtualListOwned(owner, ui, scope, node);
3180
+ return self.buildVirtualListReference(ui, scope, node);
3181
+ }
3182
+
3183
+ fn buildVirtualListReference(self: *Self, ui: *Ui, scope: *Scope, node: markup.MarkupNode) BuildError!Ui.Node {
725
3184
  @setEvalBranchQuota(scan_quota);
726
3185
  const window = scope.lookup(node.attr("window") orelse return self.failNode(node, "virtual-list requires window")) orelse return self.failNode(node, "virtual-list requires an enclosing window");
727
3186
  if (window.payload != .virtual) return self.failNode(node, "virtual-list requires an enclosing window");
@@ -794,6 +3253,11 @@ pub fn MarkupView(comptime ModelT: type, comptime MsgT: type) type {
794
3253
  }
795
3254
 
796
3255
  fn buildFor(self: *Self, ui: *Ui, scope: *Scope, node: markup.MarkupNode, out: *std.ArrayListUnmanaged(Ui.Node)) BuildError!usize {
3256
+ if (scope.expression_policy) |owner| return self.buildForOwned(owner, ui, scope, node, out);
3257
+ return self.buildForReference(ui, scope, node, out);
3258
+ }
3259
+
3260
+ fn buildForReference(self: *Self, ui: *Ui, scope: *Scope, node: markup.MarkupNode, out: *std.ArrayListUnmanaged(Ui.Node)) BuildError!usize {
797
3261
  @setEvalBranchQuota(scan_quota);
798
3262
  const each = node.attr("each") orelse return self.failVoid(node, "for requires an each attribute");
799
3263
  const as_name = node.attr("as") orelse return self.failVoid(node, "for requires an as attribute");
@@ -842,6 +3306,11 @@ pub fn MarkupView(comptime ModelT: type, comptime MsgT: type) type {
842
3306
  /// in scope, or on the model a field, a public array declaration,
843
3307
  /// or a public function (with or without arena).
844
3308
  fn iterateItems(self: *Self, ui: *Ui, comptime Item: type, comptime type_index: usize, scope: *Scope, each: []const u8) BuildError!?[]const Item {
3309
+ if (scope.expression_policy) |owner| return self.iterateItemsOwned(owner, ui, Item, type_index, scope, each);
3310
+ return self.iterateItemsReference(ui, Item, type_index, scope, each);
3311
+ }
3312
+
3313
+ fn iterateItemsReference(self: *Self, ui: *Ui, comptime Item: type, comptime type_index: usize, scope: *Scope, each: []const u8) BuildError!?[]const Item {
845
3314
  @setEvalBranchQuota(scan_quota);
846
3315
  _ = self;
847
3316
  if (scope.lookup(each)) |entry| {
@@ -895,6 +3364,11 @@ pub fn MarkupView(comptime ModelT: type, comptime MsgT: type) type {
895
3364
  /// required; without `on-details`/`details-expanded` the details
896
3365
  /// blocks render collapsed and inert (Md.view's null defaults).
897
3366
  fn buildMarkdown(self: *Self, ui: *Ui, scope: *Scope, node: markup.MarkupNode) BuildError!Ui.Node {
3367
+ if (scope.expression_policy) |owner| return self.buildMarkdownOwned(owner, ui, scope, node);
3368
+ return self.buildMarkdownReference(ui, scope, node);
3369
+ }
3370
+
3371
+ fn buildMarkdownReference(self: *Self, ui: *Ui, scope: *Scope, node: markup.MarkupNode) BuildError!Ui.Node {
898
3372
  if (node.children.len != 0) {
899
3373
  return self.failNode(node.children[0], markup.markdown_children_message);
900
3374
  }
@@ -966,6 +3440,11 @@ pub fn MarkupView(comptime ModelT: type, comptime MsgT: type) type {
966
3440
  // ----------------------------------------------------------- code
967
3441
 
968
3442
  fn buildCode(self: *Self, ui: *Ui, scope: *Scope, node: markup.MarkupNode) BuildError!Ui.Node {
3443
+ if (scope.expression_policy) |owner| return self.buildCodeOwned(owner, ui, scope, node);
3444
+ return self.buildCodeReference(ui, scope, node);
3445
+ }
3446
+
3447
+ fn buildCodeReference(self: *Self, ui: *Ui, scope: *Scope, node: markup.MarkupNode) BuildError!Ui.Node {
969
3448
  if (node.children.len != 0) return self.failNode(node.children[0], markup.code_children_message);
970
3449
  var options: Ui.CodeOptions = .{};
971
3450
  var source_text: ?[]const u8 = null;
@@ -1072,7 +3551,7 @@ pub fn MarkupView(comptime ModelT: type, comptime MsgT: type) type {
1072
3551
 
1073
3552
  fn codeFlagAttr(self: *Self, scope: *Scope, node: markup.MarkupNode, attribute: markup.MarkupAttr) BuildError!bool {
1074
3553
  if (attribute.value.len == 0) return true;
1075
- return (try self.evalAttrExpression(scope, node, attribute)).truthy();
3554
+ return markup.expr.truthyWithPolicy(scope.expression_policy, try self.evalAttrExpression(scope, node, attribute));
1076
3555
  }
1077
3556
 
1078
3557
  // ------------------------------------------------ stepper/timeline
@@ -1082,6 +3561,11 @@ pub fn MarkupView(comptime ModelT: type, comptime MsgT: type) type {
1082
3561
  /// completed/active/pending states derive from position against
1083
3562
  /// the active index, mirroring `Ui.stepper`.
1084
3563
  fn buildStepper(self: *Self, ui: *Ui, scope: *Scope, node: markup.MarkupNode) BuildError!Ui.Node {
3564
+ if (scope.expression_policy) |owner| return self.buildStepperOwned(owner, ui, scope, node);
3565
+ return self.buildStepperReference(ui, scope, node);
3566
+ }
3567
+
3568
+ fn buildStepperReference(self: *Self, ui: *Ui, scope: *Scope, node: markup.MarkupNode) BuildError!Ui.Node {
1085
3569
  var options: Ui.StepperOptions = .{};
1086
3570
  var has_active = false;
1087
3571
  for (node.attrs) |attribute| {
@@ -1162,6 +3646,11 @@ pub fn MarkupView(comptime ModelT: type, comptime MsgT: type) type {
1162
3646
  /// through `Ui.inputGroup` so the chrome-dissolve and
1163
3647
  /// focus-within treatment match Zig-built groups.
1164
3648
  fn buildInputGroup(self: *Self, ui: *Ui, scope: *Scope, node: markup.MarkupNode) BuildError!Ui.Node {
3649
+ if (scope.expression_policy) |owner| return self.buildInputGroupOwned(owner, ui, scope, node);
3650
+ return self.buildInputGroupReference(ui, scope, node);
3651
+ }
3652
+
3653
+ fn buildInputGroupReference(self: *Self, ui: *Ui, scope: *Scope, node: markup.MarkupNode) BuildError!Ui.Node {
1165
3654
  var options: Ui.InputGroupOptions = .{};
1166
3655
  for (node.attrs) |attribute| {
1167
3656
  if (std.mem.eql(u8, attribute.name, "kind")) continue;
@@ -1223,6 +3712,11 @@ pub fn MarkupView(comptime ModelT: type, comptime MsgT: type) type {
1223
3712
  /// build through the ordinary pass (structure tags work) and
1224
3713
  /// lower through `Ui.inputGroupActions`.
1225
3714
  fn buildInputGroupActions(self: *Self, ui: *Ui, scope: *Scope, node: markup.MarkupNode) BuildError!Ui.Node {
3715
+ if (scope.expression_policy) |owner| return self.buildInputGroupActionsOwned(owner, ui, scope, node);
3716
+ return self.buildInputGroupActionsReference(ui, scope, node);
3717
+ }
3718
+
3719
+ fn buildInputGroupActionsReference(self: *Self, ui: *Ui, scope: *Scope, node: markup.MarkupNode) BuildError!Ui.Node {
1226
3720
  var options: Ui.InputGroupActionsOptions = .{};
1227
3721
  for (node.attrs) |attribute| {
1228
3722
  if (std.mem.eql(u8, attribute.name, "kind")) continue;
@@ -1252,6 +3746,11 @@ pub fn MarkupView(comptime ModelT: type, comptime MsgT: type) type {
1252
3746
  /// timeline-item elements (structure tags work); mirrors
1253
3747
  /// `Ui.timeline`.
1254
3748
  fn buildTimeline(self: *Self, ui: *Ui, scope: *Scope, node: markup.MarkupNode) BuildError!Ui.Node {
3749
+ if (scope.expression_policy) |owner| return self.buildTimelineOwned(owner, ui, scope, node);
3750
+ return self.buildTimelineReference(ui, scope, node);
3751
+ }
3752
+
3753
+ fn buildTimelineReference(self: *Self, ui: *Ui, scope: *Scope, node: markup.MarkupNode) BuildError!Ui.Node {
1255
3754
  var options: Ui.TimelineOptions = .{};
1256
3755
  for (node.attrs) |attribute| {
1257
3756
  if (std.mem.eql(u8, attribute.name, "kind")) continue;
@@ -1286,6 +3785,11 @@ pub fn MarkupView(comptime ModelT: type, comptime MsgT: type) type {
1286
3785
  /// meta="..." variant="primary" on-press="open_step:{entry.slot}"/>`:
1287
3786
  /// one composite ledger item; mirrors `Ui.timelineItem`.
1288
3787
  fn buildTimelineItem(self: *Self, ui: *Ui, scope: *Scope, node: markup.MarkupNode, forwarded_press: ?MsgT) BuildError!Ui.Node {
3788
+ if (scope.expression_policy) |owner| return self.buildTimelineItemOwned(owner, ui, scope, node, forwarded_press);
3789
+ return self.buildTimelineItemReference(ui, scope, node, forwarded_press);
3790
+ }
3791
+
3792
+ fn buildTimelineItemReference(self: *Self, ui: *Ui, scope: *Scope, node: markup.MarkupNode, forwarded_press: ?MsgT) BuildError!Ui.Node {
1289
3793
  if (node.children.len != 0) {
1290
3794
  return self.failNode(node.children[0], markup.timeline_item_children_message);
1291
3795
  }
@@ -1328,11 +3832,11 @@ pub fn MarkupView(comptime ModelT: type, comptime MsgT: type) type {
1328
3832
  continue;
1329
3833
  }
1330
3834
  if (std.mem.eql(u8, attribute.name, "connector")) {
1331
- options.connector = (try self.evalAttrExpression(scope, node, attribute)).truthy();
3835
+ options.connector = markup.expr.truthyWithPolicy(scope.expression_policy, try self.evalAttrExpression(scope, node, attribute));
1332
3836
  continue;
1333
3837
  }
1334
3838
  if (std.mem.eql(u8, attribute.name, "selected")) {
1335
- options.selected = (try self.evalAttrExpression(scope, node, attribute)).truthy();
3839
+ options.selected = markup.expr.truthyWithPolicy(scope.expression_policy, try self.evalAttrExpression(scope, node, attribute));
1336
3840
  continue;
1337
3841
  }
1338
3842
  if (std.mem.eql(u8, attribute.name, "on-press")) {
@@ -1372,6 +3876,11 @@ pub fn MarkupView(comptime ModelT: type, comptime MsgT: type) type {
1372
3876
  /// (slice-valued template args included); the series set itself is
1373
3877
  /// static — the data varies, the plot shape does not.
1374
3878
  fn buildChart(self: *Self, ui: *Ui, scope: *Scope, node: markup.MarkupNode) BuildError!Ui.Node {
3879
+ if (scope.expression_policy) |owner| return self.buildChartOwned(owner, ui, scope, node);
3880
+ return self.buildChartReference(ui, scope, node);
3881
+ }
3882
+
3883
+ fn buildChartReference(self: *Self, ui: *Ui, scope: *Scope, node: markup.MarkupNode) BuildError!Ui.Node {
1375
3884
  var options: Ui.ChartOptions = .{};
1376
3885
  for (node.attrs) |attribute| {
1377
3886
  if (std.mem.eql(u8, attribute.name, "kind")) continue;
@@ -1398,7 +3907,7 @@ pub fn MarkupView(comptime ModelT: type, comptime MsgT: type) type {
1398
3907
  continue;
1399
3908
  }
1400
3909
  if (std.mem.eql(u8, attribute.name, "baseline")) {
1401
- options.baseline = (try self.evalAttrExpression(scope, node, attribute)).truthy();
3910
+ options.baseline = markup.expr.truthyWithPolicy(scope.expression_policy, try self.evalAttrExpression(scope, node, attribute));
1402
3911
  continue;
1403
3912
  }
1404
3913
  if (std.mem.eql(u8, attribute.name, "x-labels")) {
@@ -1408,11 +3917,11 @@ pub fn MarkupView(comptime ModelT: type, comptime MsgT: type) type {
1408
3917
  continue;
1409
3918
  }
1410
3919
  if (std.mem.eql(u8, attribute.name, "y-labels")) {
1411
- options.y_labels = (try self.evalAttrExpression(scope, node, attribute)).truthy();
3920
+ options.y_labels = markup.expr.truthyWithPolicy(scope.expression_policy, try self.evalAttrExpression(scope, node, attribute));
1412
3921
  continue;
1413
3922
  }
1414
3923
  if (std.mem.eql(u8, attribute.name, "hover-details")) {
1415
- options.hover_details = (try self.evalAttrExpression(scope, node, attribute)).truthy();
3924
+ options.hover_details = markup.expr.truthyWithPolicy(scope.expression_policy, try self.evalAttrExpression(scope, node, attribute));
1416
3925
  continue;
1417
3926
  }
1418
3927
  if (std.mem.eql(u8, attribute.name, "stroke-width")) {
@@ -1469,6 +3978,11 @@ pub fn MarkupView(comptime ModelT: type, comptime MsgT: type) type {
1469
3978
  /// closed attribute set; flags accept bare presence (the
1470
3979
  /// boolean-attribute convention), literals, and bindings.
1471
3980
  fn buildVideo(self: *Self, ui: *Ui, scope: *Scope, node: markup.MarkupNode) BuildError!Ui.Node {
3981
+ if (scope.expression_policy) |owner| return self.buildVideoOwned(owner, ui, scope, node);
3982
+ return self.buildVideoReference(ui, scope, node);
3983
+ }
3984
+
3985
+ fn buildVideoReference(self: *Self, ui: *Ui, scope: *Scope, node: markup.MarkupNode) BuildError!Ui.Node {
1472
3986
  if (node.children.len > 0) {
1473
3987
  return self.failNode(node.children[0], markup.video_children_message);
1474
3988
  }
@@ -1525,10 +4039,15 @@ pub fn MarkupView(comptime ModelT: type, comptime MsgT: type) type {
1525
4039
  /// valued flag evaluates truthy like every other flag attr.
1526
4040
  fn videoFlagAttr(self: *Self, scope: *Scope, node: markup.MarkupNode, attribute: markup.MarkupAttr) BuildError!bool {
1527
4041
  if (attribute.value.len == 0) return true;
1528
- return (try self.evalAttrExpression(scope, node, attribute)).truthy();
4042
+ return markup.expr.truthyWithPolicy(scope.expression_policy, try self.evalAttrExpression(scope, node, attribute));
1529
4043
  }
1530
4044
 
1531
4045
  fn buildSeries(self: *Self, ui: *Ui, scope: *Scope, node: markup.MarkupNode) BuildError!canvas.ChartSeries {
4046
+ if (scope.expression_policy) |owner| return self.buildSeriesOwned(owner, ui, scope, node);
4047
+ return self.buildSeriesReference(ui, scope, node);
4048
+ }
4049
+
4050
+ fn buildSeriesReference(self: *Self, ui: *Ui, scope: *Scope, node: markup.MarkupNode) BuildError!canvas.ChartSeries {
1532
4051
  if (node.children.len != 0) {
1533
4052
  return self.failVoid(node.children[0], markup.series_children_message);
1534
4053
  }
@@ -1586,6 +4105,11 @@ pub fn MarkupView(comptime ModelT: type, comptime MsgT: type) type {
1586
4105
  /// (the subset's one float class), and refusing them would leave
1587
4106
  /// markup charts unreachable from a TS model.
1588
4107
  fn f32Items(self: *Self, ui: *Ui, scope: *Scope, node: markup.MarkupNode, path: []const u8) BuildError![]const f32 {
4108
+ if (scope.expression_policy) |owner| return self.f32ItemsOwned(owner, ui, scope, node, path);
4109
+ return self.f32ItemsReference(ui, scope, node, path);
4110
+ }
4111
+
4112
+ fn f32ItemsReference(self: *Self, ui: *Ui, scope: *Scope, node: markup.MarkupNode, path: []const u8) BuildError![]const f32 {
1589
4113
  @setEvalBranchQuota(scan_quota);
1590
4114
  inline for (item_types, 0..) |Item, type_index| {
1591
4115
  if (comptime (Item == f32)) {
@@ -1607,6 +4131,11 @@ pub fn MarkupView(comptime ModelT: type, comptime MsgT: type) type {
1607
4131
  }
1608
4132
 
1609
4133
  fn stringAttr(self: *Self, scope: *Scope, node: markup.MarkupNode, attribute: markup.MarkupAttr, message: []const u8) BuildError![]const u8 {
4134
+ if (scope.expression_policy) |owner| return self.stringAttrOwned(owner, scope, node, attribute, message);
4135
+ return self.stringAttrReference(scope, node, attribute, message);
4136
+ }
4137
+
4138
+ fn stringAttrReference(self: *Self, scope: *Scope, node: markup.MarkupNode, attribute: markup.MarkupAttr, message: []const u8) BuildError![]const u8 {
1610
4139
  const value = try self.evalAttrExpression(scope, node, attribute);
1611
4140
  return switch (value) {
1612
4141
  .string => |text| text,
@@ -1615,6 +4144,11 @@ pub fn MarkupView(comptime ModelT: type, comptime MsgT: type) type {
1615
4144
  }
1616
4145
 
1617
4146
  fn floatAttr(self: *Self, scope: *Scope, node: markup.MarkupNode, attribute: markup.MarkupAttr) BuildError!f32 {
4147
+ if (scope.expression_policy) |owner| return self.floatAttrOwned(owner, scope, node, attribute);
4148
+ return self.floatAttrReference(scope, node, attribute);
4149
+ }
4150
+
4151
+ fn floatAttrReference(self: *Self, scope: *Scope, node: markup.MarkupNode, attribute: markup.MarkupAttr) BuildError!f32 {
1618
4152
  const value = try self.evalAttrExpression(scope, node, attribute);
1619
4153
  return switch (value) {
1620
4154
  .float => |float| float,
@@ -1655,6 +4189,11 @@ pub fn MarkupView(comptime ModelT: type, comptime MsgT: type) type {
1655
4189
  /// slice through the same sources `for each` accepts (scope slice
1656
4190
  /// args shadow model fields, pub decls, and fns).
1657
4191
  fn stringItems(self: *Self, ui: *Ui, scope: *Scope, node: markup.MarkupNode, path: []const u8, message: []const u8) BuildError![]const []const u8 {
4192
+ if (scope.expression_policy) |owner| return self.stringItemsOwned(owner, ui, scope, node, path, message);
4193
+ return self.stringItemsReference(ui, scope, node, path, message);
4194
+ }
4195
+
4196
+ fn stringItemsReference(self: *Self, ui: *Ui, scope: *Scope, node: markup.MarkupNode, path: []const u8, message: []const u8) BuildError![]const []const u8 {
1658
4197
  @setEvalBranchQuota(scan_quota);
1659
4198
  inline for (item_types, 0..) |Item, type_index| {
1660
4199
  if (comptime (Item == []const u8)) {
@@ -1670,6 +4209,11 @@ pub fn MarkupView(comptime ModelT: type, comptime MsgT: type) type {
1670
4209
  /// same sources `for each` accepts (scope slice args shadow model
1671
4210
  /// fields, pub decls, and fns).
1672
4211
  fn boolItems(self: *Self, ui: *Ui, scope: *Scope, node: markup.MarkupNode, path: []const u8) BuildError![]const bool {
4212
+ if (scope.expression_policy) |owner| return self.boolItemsOwned(owner, ui, scope, node, path);
4213
+ return self.boolItemsReference(ui, scope, node, path);
4214
+ }
4215
+
4216
+ fn boolItemsReference(self: *Self, ui: *Ui, scope: *Scope, node: markup.MarkupNode, path: []const u8) BuildError![]const bool {
1673
4217
  @setEvalBranchQuota(scan_quota);
1674
4218
  inline for (item_types, 0..) |Item, type_index| {
1675
4219
  if (comptime (Item == bool)) {
@@ -1684,6 +4228,11 @@ pub fn MarkupView(comptime ModelT: type, comptime MsgT: type) type {
1684
4228
  /// Resolve Markdown's registered source-to-image mapping through the
1685
4229
  /// same bounded iterable reflection used by `for each`.
1686
4230
  fn markdownImageItems(self: *Self, ui: *Ui, scope: *Scope, node: markup.MarkupNode, path: []const u8) BuildError![]const canvas.markdown.ResolvedImage {
4231
+ if (scope.expression_policy) |owner| return self.markdownImageItemsOwned(owner, ui, scope, node, path);
4232
+ return self.markdownImageItemsReference(ui, scope, node, path);
4233
+ }
4234
+
4235
+ fn markdownImageItemsReference(self: *Self, ui: *Ui, scope: *Scope, node: markup.MarkupNode, path: []const u8) BuildError![]const canvas.markdown.ResolvedImage {
1687
4236
  @setEvalBranchQuota(scan_quota);
1688
4237
  inline for (item_types, 0..) |Item, type_index| {
1689
4238
  if (comptime (reflect.isMarkdownImageItem(Item))) {
@@ -1709,6 +4258,11 @@ pub fn MarkupView(comptime ModelT: type, comptime MsgT: type) type {
1709
4258
  /// chain at the expansion site, exactly as if the body were
1710
4259
  /// written inline.
1711
4260
  fn buildUse(self: *Self, ui: *Ui, scope: *Scope, node: markup.MarkupNode) BuildError!Ui.Node {
4261
+ if (scope.expression_policy) |owner| return self.buildUseOwned(owner, ui, scope, node);
4262
+ return self.buildUseReference(ui, scope, node);
4263
+ }
4264
+
4265
+ fn buildUseReference(self: *Self, ui: *Ui, scope: *Scope, node: markup.MarkupNode) BuildError!Ui.Node {
1712
4266
  @setEvalBranchQuota(scan_quota);
1713
4267
  const template_name = node.attr("template") orelse {
1714
4268
  return self.failNode(node, markup.use_template_attr_message);
@@ -1840,6 +4394,11 @@ pub fn MarkupView(comptime ModelT: type, comptime MsgT: type) type {
1840
4394
  /// `for each`) binds as a slice; anything else evaluates to a
1841
4395
  /// `Value` at the use site.
1842
4396
  fn argPayload(self: *Self, ui: *Ui, scope: *Scope, node: markup.MarkupNode, attribute: markup.MarkupAttr) BuildError!ScopeEntry.Payload {
4397
+ if (scope.expression_policy) |owner| return self.argPayloadOwned(owner, ui, scope, node, attribute);
4398
+ return self.argPayloadReference(ui, scope, node, attribute);
4399
+ }
4400
+
4401
+ fn argPayloadReference(self: *Self, ui: *Ui, scope: *Scope, node: markup.MarkupNode, attribute: markup.MarkupAttr) BuildError!ScopeEntry.Payload {
1843
4402
  @setEvalBranchQuota(scan_quota);
1844
4403
  const typed = markup.attrTyped(attribute);
1845
4404
  if (typed == .invalid) {
@@ -1899,6 +4458,11 @@ pub fn MarkupView(comptime ModelT: type, comptime MsgT: type) type {
1899
4458
  // ---------------------------------------------------- attributes
1900
4459
 
1901
4460
  fn applyAttrs(self: *Self, scope: *Scope, node: markup.MarkupNode, options: *Ui.ElementOptions) BuildError!void {
4461
+ if (scope.expression_policy) |owner| return self.applyAttrsOwned(owner, scope, node, options);
4462
+ return self.applyAttrsReference(scope, node, options);
4463
+ }
4464
+
4465
+ fn applyAttrsReference(self: *Self, scope: *Scope, node: markup.MarkupNode, options: *Ui.ElementOptions) BuildError!void {
1902
4466
  try self.applyImageSourceAttrs(scope, node, options);
1903
4467
  for (node.attrs) |attribute| {
1904
4468
  if (std.mem.eql(u8, attribute.name, "kind")) continue;
@@ -2044,6 +4608,11 @@ pub fn MarkupView(comptime ModelT: type, comptime MsgT: type) type {
2044
4608
  /// initials fallback). Image-backed buttons remain a separate
2045
4609
  /// asset-lifecycle proposal.
2046
4610
  fn applyImageAttr(self: *Self, scope: *Scope, node: markup.MarkupNode, options: *Ui.ElementOptions, attribute: markup.MarkupAttr) BuildError!void {
4611
+ if (scope.expression_policy) |owner| return self.resourceAttrOwned(owner, scope, node, options, attribute, 0);
4612
+ return self.applyImageAttrReference(scope, node, options, attribute);
4613
+ }
4614
+
4615
+ fn applyImageAttrReference(self: *Self, scope: *Scope, node: markup.MarkupNode, options: *Ui.ElementOptions, attribute: markup.MarkupAttr) BuildError!void {
2047
4616
  if (!std.mem.eql(u8, node.name, "avatar") and !std.mem.eql(u8, node.name, "image")) {
2048
4617
  return self.failVoid(node, markup.image_binding_element_message);
2049
4618
  }
@@ -2068,6 +4637,11 @@ pub fn MarkupView(comptime ModelT: type, comptime MsgT: type) type {
2068
4637
  /// meaning; repeat the validator's shape checks for hot-reloaded
2069
4638
  /// documents that reached the interpreter without validation.
2070
4639
  fn applyImageSourceAttrs(self: *Self, scope: *Scope, node: markup.MarkupNode, options: *Ui.ElementOptions) BuildError!void {
4640
+ if (scope.expression_policy) |owner| return self.applyImageSourceAttrsOwned(owner, scope, node, options);
4641
+ return self.applyImageSourceAttrsReference(scope, node, options);
4642
+ }
4643
+
4644
+ fn applyImageSourceAttrsReference(self: *Self, scope: *Scope, node: markup.MarkupNode, options: *Ui.ElementOptions) BuildError!void {
2071
4645
  var any = false;
2072
4646
  for (markup.image_source_attr_names) |name| {
2073
4647
  if (node.attr(name) != null) any = true;
@@ -2094,6 +4668,11 @@ pub fn MarkupView(comptime ModelT: type, comptime MsgT: type) type {
2094
4668
  /// exactly). The id rides `options.image` into
2095
4669
  /// `Widget.image_id`, the media surface's surface-id channel.
2096
4670
  fn applySurfaceAttr(self: *Self, scope: *Scope, node: markup.MarkupNode, options: *Ui.ElementOptions, attribute: markup.MarkupAttr) BuildError!void {
4671
+ if (scope.expression_policy) |owner| return self.resourceAttrOwned(owner, scope, node, options, attribute, 1);
4672
+ return self.applySurfaceAttrReference(scope, node, options, attribute);
4673
+ }
4674
+
4675
+ fn applySurfaceAttrReference(self: *Self, scope: *Scope, node: markup.MarkupNode, options: *Ui.ElementOptions, attribute: markup.MarkupAttr) BuildError!void {
2097
4676
  if (!std.mem.eql(u8, node.name, "media-surface")) {
2098
4677
  return self.failVoid(node, markup.media_surface_surface_element_message);
2099
4678
  }
@@ -2118,6 +4697,11 @@ pub fn MarkupView(comptime ModelT: type, comptime MsgT: type) type {
2118
4697
  /// grammar exactly). The key rides `options.pty` into
2119
4698
  /// `Widget.terminal.pty`.
2120
4699
  fn applyPtyAttr(self: *Self, scope: *Scope, node: markup.MarkupNode, options: *Ui.ElementOptions, attribute: markup.MarkupAttr) BuildError!void {
4700
+ if (scope.expression_policy) |owner| return self.resourceAttrOwned(owner, scope, node, options, attribute, 2);
4701
+ return self.applyPtyAttrReference(scope, node, options, attribute);
4702
+ }
4703
+
4704
+ fn applyPtyAttrReference(self: *Self, scope: *Scope, node: markup.MarkupNode, options: *Ui.ElementOptions, attribute: markup.MarkupAttr) BuildError!void {
2121
4705
  if (!std.mem.eql(u8, node.name, "terminal")) {
2122
4706
  return self.failVoid(node, markup.terminal_pty_element_message);
2123
4707
  }
@@ -2141,6 +4725,11 @@ pub fn MarkupView(comptime ModelT: type, comptime MsgT: type) type {
2141
4725
  /// inside the element so icon + label are one hit target with one
2142
4726
  /// tint. Mirrors the validator and the compiled engine.
2143
4727
  fn applyButtonIconAttr(self: *Self, scope: *Scope, node: markup.MarkupNode, options: *Ui.ElementOptions, attribute: markup.MarkupAttr) BuildError!void {
4728
+ if (scope.expression_policy) |owner| return self.applyButtonIconAttrOwned(owner, scope, node, options, attribute);
4729
+ return self.applyButtonIconAttrReference(scope, node, options, attribute);
4730
+ }
4731
+
4732
+ fn applyButtonIconAttrReference(self: *Self, scope: *Scope, node: markup.MarkupNode, options: *Ui.ElementOptions, attribute: markup.MarkupAttr) BuildError!void {
2144
4733
  if (!markup.iconAttrElement(node.name)) {
2145
4734
  return self.failVoid(node, markup.button_icon_element_message);
2146
4735
  }
@@ -2192,11 +4781,16 @@ pub fn MarkupView(comptime ModelT: type, comptime MsgT: type) type {
2192
4781
  /// channels. Applied to `options.style`, so no flat option field
2193
4782
  /// backs it (mirrors the validator and the compiled engine).
2194
4783
  fn applyQuietHoverAttr(self: *Self, scope: *Scope, node: markup.MarkupNode, options: *Ui.ElementOptions, attribute: markup.MarkupAttr) BuildError!void {
4784
+ if (scope.expression_policy) |owner| return self.applyQuietHoverAttrOwned(owner, scope, node, options, attribute);
4785
+ return self.applyQuietHoverAttrReference(scope, node, options, attribute);
4786
+ }
4787
+
4788
+ fn applyQuietHoverAttrReference(self: *Self, scope: *Scope, node: markup.MarkupNode, options: *Ui.ElementOptions, attribute: markup.MarkupAttr) BuildError!void {
2195
4789
  if (!markup.hitTargetElement(node.name)) {
2196
4790
  return self.failVoid(node, markup.quiet_hover_element_message);
2197
4791
  }
2198
4792
  const value = try self.evalAttrExpression(scope, node, attribute);
2199
- options.style.quiet_hover = value.truthy();
4793
+ options.style.quiet_hover = markup.expr.truthyWithPolicy(scope.expression_policy, value);
2200
4794
  }
2201
4795
 
2202
4796
  fn applyOptionAttr(self: *Self, scope: *Scope, node: markup.MarkupNode, options: *Ui.ElementOptions, attribute: markup.MarkupAttr) BuildError!bool {
@@ -2210,6 +4804,11 @@ pub fn MarkupView(comptime ModelT: type, comptime MsgT: type) type {
2210
4804
  }
2211
4805
 
2212
4806
  fn setOptionField(self: *Self, scope: *Scope, node: markup.MarkupNode, options: *Ui.ElementOptions, comptime field: []const u8, attribute: markup.MarkupAttr) BuildError!void {
4807
+ if (scope.expression_policy) |owner| return self.setOptionFieldOwned(owner, scope, node, options, field, attribute);
4808
+ return self.setOptionFieldReference(scope, node, options, field, attribute);
4809
+ }
4810
+
4811
+ fn setOptionFieldReference(self: *Self, scope: *Scope, node: markup.MarkupNode, options: *Ui.ElementOptions, comptime field: []const u8, attribute: markup.MarkupAttr) BuildError!void {
2213
4812
  const FieldType = @TypeOf(@field(options, field));
2214
4813
  const value = try self.evalAttrExpression(scope, node, attribute);
2215
4814
  switch (@typeInfo(FieldType)) {
@@ -2218,9 +4817,9 @@ pub fn MarkupView(comptime ModelT: type, comptime MsgT: type) type {
2218
4817
  .integer => |int| @floatFromInt(int),
2219
4818
  else => return self.failVoid(node, "expected a number"),
2220
4819
  },
2221
- .bool => @field(options, field) = value.truthy(),
4820
+ .bool => @field(options, field) = markup.expr.truthyWithPolicy(scope.expression_policy, value),
2222
4821
  .optional => |optional| switch (@typeInfo(optional.child)) {
2223
- .bool => @field(options, field) = value.truthy(),
4822
+ .bool => @field(options, field) = markup.expr.truthyWithPolicy(scope.expression_policy, value),
2224
4823
  .@"enum" => {
2225
4824
  const text = switch (value) {
2226
4825
  .string => |text| text,
@@ -2270,6 +4869,11 @@ pub fn MarkupView(comptime ModelT: type, comptime MsgT: type) type {
2270
4869
  }
2271
4870
 
2272
4871
  fn attrKey(self: *Self, scope: *Scope, node: markup.MarkupNode, attribute: markup.MarkupAttr) BuildError!?canvas.UiKey {
4872
+ if (scope.expression_policy) |owner| return self.attrKeyOwned(owner, scope, node, attribute);
4873
+ return self.attrKeyReference(scope, node, attribute);
4874
+ }
4875
+
4876
+ fn attrKeyReference(self: *Self, scope: *Scope, node: markup.MarkupNode, attribute: markup.MarkupAttr) BuildError!?canvas.UiKey {
2273
4877
  const omitted_default = switch (markup.attrTyped(attribute)) {
2274
4878
  .binding => |path| if (scope.lookup(pathHead(path))) |entry|
2275
4879
  entry.defaulted and pathTail(path) == null
@@ -2291,6 +4895,11 @@ pub fn MarkupView(comptime ModelT: type, comptime MsgT: type) type {
2291
4895
  }
2292
4896
 
2293
4897
  fn applyMessageAttr(self: *Self, scope: *Scope, node: markup.MarkupNode, options: *Ui.ElementOptions, attribute: markup.MarkupAttr) BuildError!void {
4898
+ if (scope.expression_policy) |owner| return self.applyMessageAttrOwned(owner, scope, node, options, attribute);
4899
+ return self.applyMessageAttrReference(scope, node, options, attribute);
4900
+ }
4901
+
4902
+ fn applyMessageAttrReference(self: *Self, scope: *Scope, node: markup.MarkupNode, options: *Ui.ElementOptions, attribute: markup.MarkupAttr) BuildError!void {
2294
4903
  const typed = markup.attrTyped(attribute);
2295
4904
  if (typed != .message) {
2296
4905
  return self.failVoid(node, "invalid message expression: on-* takes a Msg tag (\"add\") or tag with one binding payload (\"toggle:{item.id}\")");
@@ -2400,6 +5009,11 @@ pub fn MarkupView(comptime ModelT: type, comptime MsgT: type) type {
2400
5009
  }
2401
5010
 
2402
5011
  fn constructMessage(self: *Self, scope: *Scope, node: markup.MarkupNode, expression: markup.MessageExpression) BuildError!MsgT {
5012
+ if (scope.expression_policy) |owner| return self.constructMessageOwned(owner, scope, node, expression, false);
5013
+ return self.constructMessageReference(scope, node, expression);
5014
+ }
5015
+
5016
+ fn constructMessageReference(self: *Self, scope: *Scope, node: markup.MarkupNode, expression: markup.MessageExpression) BuildError!MsgT {
2403
5017
  @setEvalBranchQuota(scan_quota);
2404
5018
  inline for (@typeInfo(MsgT).@"union".fields) |field| {
2405
5019
  if (std.mem.eql(u8, field.name, expression.tag)) {
@@ -2413,13 +5027,18 @@ pub fn MarkupView(comptime ModelT: type, comptime MsgT: type) type {
2413
5027
  return self.failMsg(node, "message requires a payload");
2414
5028
  }
2415
5029
  const value = try self.evalBinding(scope, node, expression.payload, true);
2416
- return @unionInit(MsgT, field.name, try self.coerce(field.type, node, value));
5030
+ return @unionInit(MsgT, field.name, try self.coerce(scope, field.type, node, value));
2417
5031
  }
2418
5032
  }
2419
5033
  return self.failMsg(node, "unknown message tag");
2420
5034
  }
2421
5035
 
2422
5036
  fn constructDragMessage(self: *Self, scope: *Scope, node: markup.MarkupNode, expression: markup.MessageExpression) BuildError!MsgT {
5037
+ if (scope.expression_policy) |owner| return self.constructMessageOwned(owner, scope, node, expression, true);
5038
+ return self.constructDragMessageReference(scope, node, expression);
5039
+ }
5040
+
5041
+ fn constructDragMessageReference(self: *Self, scope: *Scope, node: markup.MarkupNode, expression: markup.MessageExpression) BuildError!MsgT {
2423
5042
  if (expression.payload.len == 0) return self.failMsg(node, markup.on_drag_payload_message);
2424
5043
  @setEvalBranchQuota(scan_quota);
2425
5044
  inline for (@typeInfo(MsgT).@"union".fields) |field| {
@@ -2427,14 +5046,19 @@ pub fn MarkupView(comptime ModelT: type, comptime MsgT: type) type {
2427
5046
  if (comptime !reflect.declaredWidgetDragDropRecord(field.type)) return self.failMsg(node, markup.on_drag_payload_message);
2428
5047
  const value = try self.evalBinding(scope, node, expression.payload, true);
2429
5048
  var payload: field.type = std.mem.zeroes(field.type);
2430
- payload.sourceId = try self.coerce(@FieldType(field.type, "sourceId"), node, value);
5049
+ payload.sourceId = try self.coerce(scope, @FieldType(field.type, "sourceId"), node, value);
2431
5050
  return @unionInit(MsgT, field.name, payload);
2432
5051
  }
2433
5052
  }
2434
5053
  return self.failMsg(node, markup.on_drag_payload_message);
2435
5054
  }
2436
5055
 
2437
- fn coerce(self: *Self, comptime T: type, node: markup.MarkupNode, value: Value) BuildError!T {
5056
+ fn coerce(self: *Self, scope: *Scope, comptime T: type, node: markup.MarkupNode, value: Value) BuildError!T {
5057
+ if (scope.expression_policy) |owner| return self.coerceOwned(owner, scope, T, node, value);
5058
+ return self.coerceReference(scope, T, node, value);
5059
+ }
5060
+
5061
+ fn coerceReference(self: *Self, scope: *Scope, comptime T: type, node: markup.MarkupNode, value: Value) BuildError!T {
2438
5062
  return switch (@typeInfo(T)) {
2439
5063
  // Range-checked against the payload's own integer type
2440
5064
  // before the cast (the option-attribute pattern): a
@@ -2462,7 +5086,7 @@ pub fn MarkupView(comptime ModelT: type, comptime MsgT: type) type {
2462
5086
  .string => |text| text,
2463
5087
  else => self.failCoerce(T, node),
2464
5088
  },
2465
- .bool => value.truthy(),
5089
+ .bool => markup.expr.truthyWithPolicy(scope.expression_policy, value),
2466
5090
  else => self.failCoerce(T, node),
2467
5091
  };
2468
5092
  }
@@ -2588,6 +5212,11 @@ pub fn MarkupView(comptime ModelT: type, comptime MsgT: type) type {
2588
5212
  /// level, not per frame per use — the typed-document pass's whole
2589
5213
  /// point for the interpreter.
2590
5214
  fn evalAttributeExpression(self: *Self, scope: *Scope, node: markup.MarkupNode, attribute: markup.MarkupAttr, attribute_aware: bool) BuildError!Value {
5215
+ if (scope.expression_policy) |owner| return self.evalAttributeExpressionOwned(owner, scope, node, attribute, attribute_aware);
5216
+ return self.evalAttributeExpressionReference(scope, node, attribute, attribute_aware);
5217
+ }
5218
+
5219
+ fn evalAttributeExpressionReference(self: *Self, scope: *Scope, node: markup.MarkupNode, attribute: markup.MarkupAttr, attribute_aware: bool) BuildError!Value {
2591
5220
  return switch (markup.attrTyped(attribute)) {
2592
5221
  .literal => |text| if (attribute_aware)
2593
5222
  literalValueForAttribute(text, attribute.name)
@@ -2597,7 +5226,8 @@ pub fn MarkupView(comptime ModelT: type, comptime MsgT: type) type {
2597
5226
  // Arena-computed bindings are excluded from equality on
2598
5227
  // purpose: comparing freshly formatted strings is a smell —
2599
5228
  // compare the source fields, or bind a bool-returning fn.
2600
- .equals => |sides| .{ .boolean = Value.eql(
5229
+ .equals => |sides| .{ .boolean = markup.expr.equalWithPolicy(
5230
+ scope.expression_policy,
2601
5231
  try self.evalBinding(scope, node, sides.left, false),
2602
5232
  try self.evalBinding(scope, node, sides.right, false),
2603
5233
  ) },
@@ -2619,6 +5249,11 @@ pub fn MarkupView(comptime ModelT: type, comptime MsgT: type) type {
2619
5249
  /// the document was never canonicalized), and the re-parse
2620
5250
  /// surfaces the exact teaching diagnostic.
2621
5251
  fn evalExpressionForm(self: *Self, scope: *Scope, node: markup.MarkupNode, form: markup.TypedExprRef) BuildError!Value {
5252
+ if (scope.expression_policy) |owner| return self.evalExpressionFormOwned(owner, scope, node, form);
5253
+ return self.evalExpressionFormReference(scope, node, form);
5254
+ }
5255
+
5256
+ fn evalExpressionFormReference(self: *Self, scope: *Scope, node: markup.MarkupNode, form: markup.TypedExprRef) BuildError!Value {
2622
5257
  if (form.tree) |tree| return self.evalParsedTree(scope, node, tree);
2623
5258
  return self.evalExpressionTree(scope, node, form.inner);
2624
5259
  }
@@ -2633,13 +5268,18 @@ pub fn MarkupView(comptime ModelT: type, comptime MsgT: type) type {
2633
5268
  fn evalExpressionTree(self: *Self, scope: *Scope, node: markup.MarkupNode, inner: []const u8) BuildError!Value {
2634
5269
  var tree: markup.expr.ExprTree = .{};
2635
5270
  var diagnostic: markup.expr.Diagnostic = .{};
2636
- if (!markup.expr.parse(inner, &tree, &diagnostic)) {
5271
+ if (!markup.expr.parseWithPolicy(scope.expression_policy, inner, &tree, &diagnostic)) {
2637
5272
  return self.failValue(node, diagnostic.message);
2638
5273
  }
2639
5274
  return self.evalParsedTree(scope, node, &tree);
2640
5275
  }
2641
5276
 
2642
5277
  fn evalParsedTree(self: *Self, scope: *Scope, node: markup.MarkupNode, tree: *const markup.expr.ExprTree) BuildError!Value {
5278
+ if (scope.expression_policy) |owner| return self.evalParsedTreeOwned(owner, scope, node, tree);
5279
+ return self.evalParsedTreeReference(scope, node, tree);
5280
+ }
5281
+
5282
+ fn evalParsedTreeReference(self: *Self, scope: *Scope, node: markup.MarkupNode, tree: *const markup.expr.ExprTree) BuildError!Value {
2643
5283
  var values: [markup.expr.max_expression_nodes]Value = undefined;
2644
5284
  for (tree.nodes[0..tree.len], 0..) |expr_node, index| {
2645
5285
  if (expr_node.kind != .binding) continue;
@@ -2647,7 +5287,7 @@ pub fn MarkupView(comptime ModelT: type, comptime MsgT: type) type {
2647
5287
  // same teaching rule as `{a == b}`.
2648
5288
  values[index] = try self.evalBinding(scope, node, expr_node.text, !expr_node.comparison_operand);
2649
5289
  }
2650
- return switch (try markup.expr.eval(tree, &values, scope.arena)) {
5290
+ return switch (try markup.expr.evalWithPolicy(scope.expression_policy, tree, &values, scope.arena)) {
2651
5291
  .value => |value| value,
2652
5292
  .fail => |message| self.failValue(node, message),
2653
5293
  };
@@ -2658,6 +5298,11 @@ pub fn MarkupView(comptime ModelT: type, comptime MsgT: type) type {
2658
5298
  /// is — text interpolation, attribute values, message payloads —
2659
5299
  /// except inside `{a == b}` equality).
2660
5300
  fn evalBinding(self: *Self, scope: *Scope, node: markup.MarkupNode, path: []const u8, allow_arena: bool) BuildError!Value {
5301
+ if (scope.expression_policy) |owner| return self.evalBindingOwned(owner, scope, node, path, allow_arena);
5302
+ return self.evalBindingReference(scope, node, path, allow_arena);
5303
+ }
5304
+
5305
+ fn evalBindingReference(self: *Self, scope: *Scope, node: markup.MarkupNode, path: []const u8, allow_arena: bool) BuildError!Value {
2661
5306
  @setEvalBranchQuota(scan_quota);
2662
5307
  const head = pathHead(path);
2663
5308
  const arena: ?std.mem.Allocator = if (allow_arena) scope.arena else null;
@@ -2717,6 +5362,11 @@ pub fn MarkupView(comptime ModelT: type, comptime MsgT: type) type {
2717
5362
  /// announces as ONE text run (spans are visual, never semantic
2718
5363
  /// children). Mirrors the validator and the compiled engine.
2719
5364
  fn buildSpanParagraph(self: *Self, ui: *Ui, scope: *Scope, node: markup.MarkupNode, options: Ui.ElementOptions) BuildError!Ui.Node {
5365
+ if (scope.expression_policy) |owner| return self.buildSpanParagraphOwned(owner, ui, scope, node, options);
5366
+ return self.buildSpanParagraphReference(ui, scope, node, options);
5367
+ }
5368
+
5369
+ fn buildSpanParagraphReference(self: *Self, ui: *Ui, scope: *Scope, node: markup.MarkupNode, options: Ui.ElementOptions) BuildError!Ui.Node {
2720
5370
  // A span paragraph always word-wraps (builder parity), so the
2721
5371
  // single-line policies are dead data here. Mirrors the
2722
5372
  // validator and the compiled engine's compile error.
@@ -2749,6 +5399,11 @@ pub fn MarkupView(comptime ModelT: type, comptime MsgT: type) type {
2749
5399
  /// else at the base size, and a silently dead binding is worse
2750
5400
  /// than a diagnostic.
2751
5401
  fn buildSpan(self: *Self, ui: *Ui, scope: *Scope, node: markup.MarkupNode) BuildError!canvas.TextSpan {
5402
+ if (scope.expression_policy) |owner| return self.buildSpanOwned(owner, ui, scope, node);
5403
+ return self.buildSpanReference(ui, scope, node);
5404
+ }
5405
+
5406
+ fn buildSpanReference(self: *Self, ui: *Ui, scope: *Scope, node: markup.MarkupNode) BuildError!canvas.TextSpan {
2752
5407
  if (markup.spanShapeError(node)) |info| {
2753
5408
  self.diagnostic = .{ .line = info.line, .column = info.column, .message = info.message, .path = info.path };
2754
5409
  return error.MarkupBuild;
@@ -2771,11 +5426,11 @@ pub fn MarkupView(comptime ModelT: type, comptime MsgT: type) type {
2771
5426
  }
2772
5427
  span.scale = multiplier;
2773
5428
  } else if (std.mem.eql(u8, attribute.name, "mono")) {
2774
- span.monospace = (try self.evalAttrExpression(scope, node, attribute)).truthy();
5429
+ span.monospace = markup.expr.truthyWithPolicy(scope.expression_policy, try self.evalAttrExpression(scope, node, attribute));
2775
5430
  } else if (std.mem.eql(u8, attribute.name, "italic")) {
2776
- span.italic = (try self.evalAttrExpression(scope, node, attribute)).truthy();
5431
+ span.italic = markup.expr.truthyWithPolicy(scope.expression_policy, try self.evalAttrExpression(scope, node, attribute));
2777
5432
  } else if (std.mem.eql(u8, attribute.name, "underline")) {
2778
- span.underline = (try self.evalAttrExpression(scope, node, attribute)).truthy();
5433
+ span.underline = markup.expr.truthyWithPolicy(scope.expression_policy, try self.evalAttrExpression(scope, node, attribute));
2779
5434
  } else if (std.mem.eql(u8, attribute.name, "foreground")) {
2780
5435
  span.color = std.meta.stringToEnum(canvas.TextSpanColor, attribute.value) orelse {
2781
5436
  return self.failValue(node, markup.unknown_color_token_message);
@@ -2788,6 +5443,11 @@ pub fn MarkupView(comptime ModelT: type, comptime MsgT: type) type {
2788
5443
  }
2789
5444
 
2790
5445
  fn interpolatedText(self: *Self, ui: *Ui, scope: *Scope, node: markup.MarkupNode) BuildError![]const u8 {
5446
+ if (scope.expression_policy) |owner| return self.interpolatedTextOwned(owner, ui, scope, node);
5447
+ return self.interpolatedTextReference(ui, scope, node);
5448
+ }
5449
+
5450
+ fn interpolatedTextReference(self: *Self, ui: *Ui, scope: *Scope, node: markup.MarkupNode) BuildError![]const u8 {
2791
5451
  if (node.children.len > 1) return self.failText(node, "text elements take a single run of text");
2792
5452
  for (node.children) |child| {
2793
5453
  if (child.kind != .text) {
@@ -2806,6 +5466,11 @@ pub fn MarkupView(comptime ModelT: type, comptime MsgT: type) type {
2806
5466
  /// into the build arena: the shared body behind plain text leaves
2807
5467
  /// and every span-paragraph run. `node` positions diagnostics.
2808
5468
  fn runText(self: *Self, ui: *Ui, scope: *Scope, node: markup.MarkupNode, run: markup.MarkupNode) BuildError![]const u8 {
5469
+ if (scope.expression_policy) |owner| return self.runTextOwned(owner, ui, scope, node, run);
5470
+ return self.runTextReference(ui, scope, node, run);
5471
+ }
5472
+
5473
+ fn runTextReference(self: *Self, ui: *Ui, scope: *Scope, node: markup.MarkupNode, run: markup.MarkupNode) BuildError![]const u8 {
2809
5474
  const source = run.text;
2810
5475
  const segments = run.typed_text;
2811
5476
  if (std.mem.indexOfScalar(u8, source, '{') == null) return source;
@@ -2817,8 +5482,8 @@ pub fn MarkupView(comptime ModelT: type, comptime MsgT: type) type {
2817
5482
  for (typed_segments) |segment| {
2818
5483
  switch (segment) {
2819
5484
  .literal => |text| try out.appendSlice(ui.arena, text),
2820
- .binding => |path| try appendValue(&out, ui.arena, try self.evalBinding(scope, node, path, true)),
2821
- .expression => |form| try appendValue(&out, ui.arena, try self.evalExpressionForm(scope, node, form)),
5485
+ .binding => |path| try markup.expr.appendValueWithPolicy(scope.expression_policy, &out, ui.arena, try self.evalBinding(scope, node, path, true)),
5486
+ .expression => |form| try markup.expr.appendValueWithPolicy(scope.expression_policy, &out, ui.arena, try self.evalExpressionForm(scope, node, form)),
2822
5487
  .unterminated => return self.failText(node, "unterminated interpolation"),
2823
5488
  }
2824
5489
  }
@@ -2835,7 +5500,7 @@ pub fn MarkupView(comptime ModelT: type, comptime MsgT: type) type {
2835
5500
  try self.evalBinding(scope, node, inner, true)
2836
5501
  else
2837
5502
  try self.evalExpressionTree(scope, node, inner);
2838
- try appendValue(&out, ui.arena, value);
5503
+ try markup.expr.appendValueWithPolicy(scope.expression_policy, &out, ui.arena, value);
2839
5504
  rest = rest[close + 1 ..];
2840
5505
  }
2841
5506
  try out.appendSlice(ui.arena, rest);
@@ -2961,6 +5626,84 @@ pub fn asSlice(comptime Item: type, value: anytype) []const Item {
2961
5626
  /// when `arena` is null), and bounded model conventions (a
2962
5627
  /// `field_count`-style pair is the author's job; the resolver only follows
2963
5628
  /// what exists).
5629
+ /// Runtime-only reflected access. The program owns raw path splitting and
5630
+ /// field/declaration precedence; these capabilities retain native types, exact
5631
+ /// scalar conversion, borrowed aliases and callback/allocator ownership.
5632
+ fn scalarDeclClass(comptime T: type, comptime Decl: type) usize {
5633
+ switch (@typeInfo(Decl)) {
5634
+ .@"fn" => |info| {
5635
+ if (info.params.len == 1 and info.return_type != null and info.params[0].type == *const T) return 1;
5636
+ if (comptime isArenaScalarFn(T, Decl)) return 2;
5637
+ },
5638
+ else => {},
5639
+ }
5640
+ return 0;
5641
+ }
5642
+
5643
+ fn resolveOnOwned(comptime T: type, owner: markup.expr.Policy, value: *const T, path: []const u8, arena: ?std.mem.Allocator) ?Value {
5644
+ @setEvalBranchQuota(comptime typeScanQuota(T));
5645
+ const class = comptime switch (@typeInfo(T)) {
5646
+ .@"struct" => 1,
5647
+ .pointer => |info| if (info.size == .one and info.is_const) 2 else 0,
5648
+ else => 0,
5649
+ };
5650
+ var request = value_programs.Request.init(13);
5651
+ defer request.deinit();
5652
+ request.word(class);
5653
+ request.blob(path);
5654
+ request.word(@intFromBool(arena != null));
5655
+ if (comptime class == 1) {
5656
+ const info = @typeInfo(T).@"struct";
5657
+ request.word(info.fields.len);
5658
+ inline for (info.fields) |field| request.blob(field.name);
5659
+ request.word(info.decls.len);
5660
+ inline for (info.decls) |decl| request.blob(decl.name);
5661
+ inline for (info.decls) |decl| request.word(comptime scalarDeclClass(T, @TypeOf(@field(T, decl.name))));
5662
+ } else request.words(&.{ 0, 0 });
5663
+ const action = request.decision(owner);
5664
+ if (action.action == 0) return null;
5665
+ if (comptime class == 2) {
5666
+ if (action.action != 70) @panic("invalid transparent pointer capability");
5667
+ return resolveOnOwned(@typeInfo(T).pointer.child, owner, value.*, path, arena);
5668
+ } else if (comptime class == 1) {
5669
+ switch (action.action) {
5670
+ 68 => inline for (@typeInfo(T).@"struct".fields, 0..) |field, i| {
5671
+ if (action.fields[0] == i) {
5672
+ if (action.fields[1] != 0) return resolveOnOwned(field.type, owner, &@field(value, field.name), path[action.fields[1]..], arena);
5673
+ return valueOf(field.type, @field(value, field.name));
5674
+ }
5675
+ },
5676
+ 69 => inline for (@typeInfo(T).@"struct".decls, 0..) |decl, i| {
5677
+ if (action.fields[0] == i) {
5678
+ const kind = comptime scalarDeclClass(T, @TypeOf(@field(T, decl.name)));
5679
+ if (action.fields[1] != kind) @panic("invalid scalar callback class");
5680
+ if (comptime kind == 1) return valueOf(@typeInfo(@TypeOf(@field(T, decl.name))).@"fn".return_type.?, @field(T, decl.name)(value));
5681
+ if (comptime kind == 2) return valueOf(@typeInfo(@TypeOf(@field(T, decl.name))).@"fn".return_type.?, @field(T, decl.name)(value, arena.?));
5682
+ @panic("unavailable scalar callback");
5683
+ }
5684
+ },
5685
+ else => @panic("invalid reflected access capability"),
5686
+ }
5687
+ }
5688
+ @panic("invalid reflected field index");
5689
+ }
5690
+
5691
+ fn enumValueOwned(comptime T: type, owner: markup.expr.Policy, text: []const u8) ?T {
5692
+ var request = value_programs.Request.init(15);
5693
+ defer request.deinit();
5694
+ request.blob(text);
5695
+ const fields = @typeInfo(T).@"enum".fields;
5696
+ request.word(fields.len);
5697
+ inline for (fields) |field| request.blob(field.name);
5698
+ const action = request.decision(owner);
5699
+ if (action.action == 0) return null;
5700
+ if (action.action != 74) @panic("invalid enum capability");
5701
+ inline for (fields, 0..) |field, i| if (action.fields[0] == i) {
5702
+ return @field(T, field.name);
5703
+ };
5704
+ @panic("invalid enum field index");
5705
+ }
5706
+
2964
5707
  fn resolveOn(comptime T: type, value: *const T, path: []const u8, arena: ?std.mem.Allocator) ?Value {
2965
5708
  @setEvalBranchQuota(comptime typeScanQuota(T));
2966
5709
  const head = pathHead(path);