@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
@@ -228,6 +228,13 @@ pub const Parser = struct {
228
228
  return .{ .arena = arena, .source = source };
229
229
  }
230
230
 
231
+ /// Use the installed runtime grammar while retaining the native parser
232
+ /// for compile-time/tooling consumers and independent reference tests.
233
+ pub fn parseWithPolicy(self: *Parser, owner: ?expr.Policy) ParseError!MarkupDocument {
234
+ if (owner) |callback| return @import("ui_markup_document.zig").parse(callback, self);
235
+ return self.parse();
236
+ }
237
+
231
238
  /// Parse a document: comments and whitespace around zero or more
232
239
  /// top-level `<import>` nodes, then zero or more `<template>`
233
240
  /// definitions, then at most one root element. A file that is all
@@ -765,6 +772,13 @@ pub fn parseAttrExpression(value: []const u8) ?Expression {
765
772
  return .{ .expression = inner };
766
773
  }
767
774
 
775
+ /// Runtime raw classification shares the compiled canonical grammar. The
776
+ /// original parser remains comptime-callable and is the native reference.
777
+ pub fn parseAttrExpressionWithPolicy(owner: ?expr.Policy, value: []const u8) ?Expression {
778
+ if (owner) |callback| return @import("ui_markup_canonical.zig").expression(callback, value);
779
+ return parseAttrExpression(value);
780
+ }
781
+
768
782
  /// Structural check of an attribute expression, shared by the validator
769
783
  /// (what `native markup check` runs, with no model in hand): syntax,
770
784
  /// bounds, function names and arity, and the type discipline over the
@@ -900,32 +914,46 @@ pub fn attrTyped(attribute: MarkupAttr) TypedAttrValue {
900
914
  return classifyAttrValue(attribute.name, attribute.value);
901
915
  }
902
916
 
917
+ /// Cached typed values keep their original payload and tree identity;
918
+ /// uncached runtime attributes use the compiled canonical classifier.
919
+ pub fn attrTypedWithPolicy(owner: ?expr.Policy, attribute: MarkupAttr) TypedAttrValue {
920
+ if (owner) |callback| return @import("ui_markup_canonical.zig").typedAttribute(callback, attribute);
921
+ return attrTyped(attribute);
922
+ }
923
+
903
924
  /// Canonicalize a parsed (and, for imports, resolved) document: every
904
925
  /// attribute value and interpolating text run parses once into its typed
905
926
  /// form. Nodes are copied; the input document stays valid.
906
927
  pub fn canonicalize(arena: std.mem.Allocator, document: MarkupDocument) error{OutOfMemory}!MarkupDocument {
928
+ return canonicalizeWithPolicy(null, arena, document);
929
+ }
930
+
931
+ /// Runtime canonicalization uses compiled typed-tree coordination and
932
+ /// expression grammar; native retains allocation and borrowed source storage.
933
+ pub fn canonicalizeWithPolicy(owner: ?expr.Policy, arena: std.mem.Allocator, document: MarkupDocument) error{OutOfMemory}!MarkupDocument {
934
+ if (owner) |callback| return @import("ui_markup_canonical.zig").canonicalize(callback, arena, document);
907
935
  var out = document;
908
936
  if (document.templates.len > 0) {
909
937
  const templates = try arena.alloc(MarkupNode, document.templates.len);
910
938
  for (document.templates, 0..) |template_node, index| {
911
- templates[index] = try canonicalizeNode(arena, template_node);
939
+ templates[index] = try canonicalizeNode(owner, arena, template_node);
912
940
  }
913
941
  out.templates = templates;
914
942
  }
915
943
  if (document.root) |root| {
916
- out.root = try canonicalizeNode(arena, root);
944
+ out.root = try canonicalizeNode(owner, arena, root);
917
945
  }
918
946
  return out;
919
947
  }
920
948
 
921
- fn canonicalizeNode(arena: std.mem.Allocator, node: MarkupNode) error{OutOfMemory}!MarkupNode {
949
+ fn canonicalizeNode(owner: ?expr.Policy, arena: std.mem.Allocator, node: MarkupNode) error{OutOfMemory}!MarkupNode {
922
950
  var out = node;
923
951
  if (node.attrs.len > 0) {
924
952
  const attrs = try arena.alloc(MarkupAttr, node.attrs.len);
925
953
  for (node.attrs, 0..) |attribute, index| {
926
954
  attrs[index] = attribute;
927
955
  const slot = try arena.create(TypedAttrValue);
928
- slot.* = typedValueOf(arena, attribute) catch |err| switch (err) {
956
+ slot.* = typedValueOf(owner, arena, attribute) catch |err| switch (err) {
929
957
  error.OutOfMemory => return error.OutOfMemory,
930
958
  };
931
959
  attrs[index].typed = slot;
@@ -933,40 +961,40 @@ fn canonicalizeNode(arena: std.mem.Allocator, node: MarkupNode) error{OutOfMemor
933
961
  out.attrs = attrs;
934
962
  }
935
963
  if (node.kind == .text and std.mem.indexOfScalar(u8, node.text, '{') != null) {
936
- out.typed_text = try typedTextSegments(arena, node.text);
964
+ out.typed_text = try typedTextSegments(owner, arena, node.text);
937
965
  }
938
966
  if (node.children.len > 0) {
939
967
  const children = try arena.alloc(MarkupNode, node.children.len);
940
968
  for (node.children, 0..) |child, index| {
941
- children[index] = try canonicalizeNode(arena, child);
969
+ children[index] = try canonicalizeNode(owner, arena, child);
942
970
  }
943
971
  out.children = children;
944
972
  }
945
973
  return out;
946
974
  }
947
975
 
948
- fn typedValueOf(arena: std.mem.Allocator, attribute: MarkupAttr) error{OutOfMemory}!TypedAttrValue {
976
+ fn typedValueOf(owner: ?expr.Policy, arena: std.mem.Allocator, attribute: MarkupAttr) error{OutOfMemory}!TypedAttrValue {
949
977
  var typed = classifyAttrValue(attribute.name, attribute.value);
950
978
  if (typed == .expression) {
951
- typed.expression.tree = try parsedTree(arena, typed.expression.inner);
979
+ typed.expression.tree = try parsedTree(owner, arena, typed.expression.inner);
952
980
  }
953
981
  return typed;
954
982
  }
955
983
 
956
984
  /// Parse one expression's tree into the arena; null when it does not
957
985
  /// parse (the engines re-derive the diagnostic at the use site).
958
- fn parsedTree(arena: std.mem.Allocator, inner: []const u8) error{OutOfMemory}!?*const expr.ExprTree {
986
+ fn parsedTree(owner: ?expr.Policy, arena: std.mem.Allocator, inner: []const u8) error{OutOfMemory}!?*const expr.ExprTree {
959
987
  const tree = try arena.create(expr.ExprTree);
960
988
  tree.* = .{};
961
989
  var diagnostic: expr.Diagnostic = .{};
962
- if (!expr.parse(inner, tree, &diagnostic)) return null;
990
+ if (!expr.parseWithPolicy(owner, inner, tree, &diagnostic)) return null;
963
991
  return tree;
964
992
  }
965
993
 
966
994
  /// Split an interpolating text run into typed segments, mirroring the
967
995
  /// engines' scan byte for byte: literal chunks between braces, `{path}`
968
996
  /// bindings, full expressions, and the unterminated tail case.
969
- fn typedTextSegments(arena: std.mem.Allocator, text: []const u8) error{OutOfMemory}![]const TypedTextSegment {
997
+ fn typedTextSegments(owner: ?expr.Policy, arena: std.mem.Allocator, text: []const u8) error{OutOfMemory}![]const TypedTextSegment {
970
998
  var segments: std.ArrayListUnmanaged(TypedTextSegment) = .empty;
971
999
  var rest = text;
972
1000
  while (std.mem.indexOfScalar(u8, rest, '{')) |open| {
@@ -979,7 +1007,7 @@ fn typedTextSegments(arena: std.mem.Allocator, text: []const u8) error{OutOfMemo
979
1007
  if (isBindingPath(inner)) {
980
1008
  try segments.append(arena, .{ .binding = inner });
981
1009
  } else {
982
- try segments.append(arena, .{ .expression = .{ .inner = inner, .tree = try parsedTree(arena, inner) } });
1010
+ try segments.append(arena, .{ .expression = .{ .inner = inner, .tree = try parsedTree(owner, arena, inner) } });
983
1011
  }
984
1012
  rest = rest[close + 1 ..];
985
1013
  }
@@ -3987,7 +4015,19 @@ pub fn resolveImports(
3987
4015
  loader: ImportLoader,
3988
4016
  diagnostic: *MarkupErrorInfo,
3989
4017
  ) ResolveError!MarkupDocument {
3990
- return resolveImportsFromRoot(arena, dirnamePath(root_name), root_name, root_source, loader, diagnostic);
4018
+ return resolveImportsWithPolicy(null, arena, root_name, root_source, loader, diagnostic);
4019
+ }
4020
+
4021
+ /// Runtime parser ownership is propagated through every imported document.
4022
+ pub fn resolveImportsWithPolicy(
4023
+ owner: ?expr.Policy,
4024
+ arena: std.mem.Allocator,
4025
+ root_name: []const u8,
4026
+ root_source: []const u8,
4027
+ loader: ImportLoader,
4028
+ diagnostic: *MarkupErrorInfo,
4029
+ ) ResolveError!MarkupDocument {
4030
+ return resolveImportsFromRootWithPolicy(owner, arena, dirnamePath(root_name), root_name, root_source, loader, diagnostic);
3991
4031
  }
3992
4032
 
3993
4033
  /// Resolve an import closure against an explicit markup root. App-wide
@@ -4004,7 +4044,21 @@ pub fn resolveImportsFromRoot(
4004
4044
  loader: ImportLoader,
4005
4045
  diagnostic: *MarkupErrorInfo,
4006
4046
  ) ResolveError!MarkupDocument {
4047
+ return resolveImportsFromRootWithPolicy(null, arena, root_dir, root_name, root_source, loader, diagnostic);
4048
+ }
4049
+
4050
+ pub fn resolveImportsFromRootWithPolicy(
4051
+ owner: ?expr.Policy,
4052
+ arena: std.mem.Allocator,
4053
+ root_dir: []const u8,
4054
+ root_name: []const u8,
4055
+ root_source: []const u8,
4056
+ loader: ImportLoader,
4057
+ diagnostic: *MarkupErrorInfo,
4058
+ ) ResolveError!MarkupDocument {
4059
+ if (owner) |callback| return @import("ui_markup_imports.zig").resolve(callback, arena, root_dir, root_name, root_source, loader, diagnostic, stampSourcePath);
4007
4060
  var resolver = ImportResolver{
4061
+ .parser_policy = owner,
4008
4062
  .arena = arena,
4009
4063
  .root_dir = root_dir,
4010
4064
  .loader = loader,
@@ -4015,6 +4069,7 @@ pub fn resolveImportsFromRoot(
4015
4069
  }
4016
4070
 
4017
4071
  const ImportResolver = struct {
4072
+ parser_policy: ?expr.Policy = null,
4018
4073
  arena: std.mem.Allocator,
4019
4074
  root_dir: []const u8,
4020
4075
  loader: ImportLoader,
@@ -4038,7 +4093,7 @@ const ImportResolver = struct {
4038
4093
  self.chain[depth] = path;
4039
4094
  self.source_bytes += source.len;
4040
4095
  var parser = Parser.init(self.arena, source);
4041
- const document = parser.parse() catch |err| {
4096
+ const document = parser.parseWithPolicy(self.parser_policy) catch |err| {
4042
4097
  if (err == error.MarkupSyntax) {
4043
4098
  self.diagnostic.* = parser.diagnostic;
4044
4099
  self.diagnostic.path = path;
@@ -0,0 +1,237 @@
1
+ //! Typed-document action transport and borrowed storage capabilities.
2
+ const std = @import("std");
3
+ const m = @import("ui_markup.zig");
4
+ const scratch = std.heap.page_allocator;
5
+
6
+ const Packet = struct {
7
+ bytes: std.ArrayListUnmanaged(u8) = .empty,
8
+ fn word(self: *Packet, value: usize) !void {
9
+ if (value > 9007199254740991) @panic("canonical integer exceeds exact transport range");
10
+ var raw: [8]u8 = undefined;
11
+ std.mem.writeInt(u64, &raw, @bitCast(@as(f64, @floatFromInt(value))), .little);
12
+ try self.bytes.appendSlice(scratch, &raw);
13
+ }
14
+ fn blob(self: *Packet, bytes: []const u8) !void {
15
+ try self.word(bytes.len);
16
+ try self.bytes.appendSlice(scratch, bytes);
17
+ }
18
+ fn node(self: *Packet, value: m.MarkupNode) error{OutOfMemory}!void {
19
+ try self.word(@intFromEnum(value.kind));
20
+ try self.word(value.attrs.len);
21
+ for (value.attrs) |attribute| {
22
+ try self.blob(attribute.name);
23
+ try self.blob(attribute.value);
24
+ }
25
+ try self.blob(value.text);
26
+ try self.word(value.children.len);
27
+ for (value.children) |child| try self.node(child);
28
+ }
29
+ };
30
+ fn number(bytes: []const u8, at: usize) usize {
31
+ const value: f64 = @bitCast(std.mem.readInt(u64, bytes[at..][0..8], .little));
32
+ if (!std.math.isFinite(value) or value < 0 or value > 9007199254740991 or value != @trunc(value) or value >= @as(f64, @floatFromInt(std.math.maxInt(usize))))
33
+ @panic("invalid compiled canonical integer");
34
+ return @intFromFloat(value);
35
+ }
36
+
37
+ fn classify(owner: m.expr.Policy, name: []const u8, value: []const u8, channel: u8) m.TypedAttrValue {
38
+ var request: Packet = .{};
39
+ defer request.bytes.deinit(scratch);
40
+ request.bytes.appendSlice(scratch, &.{ 109, 1, 1, channel }) catch @panic("attribute transport allocation failed");
41
+ request.blob(name) catch @panic("attribute transport allocation failed");
42
+ request.blob(value) catch @panic("attribute transport allocation failed");
43
+ var out: [64]u8 = undefined;
44
+ if (owner(request.bytes.items, &out) != out.len or out[0] != 109 or out[1] != 1 or out[2] != 1 or number(&out, 8) != 1)
45
+ @panic("invalid compiled attribute classification");
46
+ for (out[3..8]) |byte| if (byte != 0) @panic("invalid attribute reserved byte");
47
+ var reader: Reader = .{ .owner = owner, .arena = scratch, .tape = out[16..] };
48
+ const action = reader.next();
49
+ const f = action.fields;
50
+ if (action.action != 11 or f[0] != 0) @panic("invalid attribute action");
51
+ return switch (action.code) {
52
+ 1 => .{ .literal = Reader.span(value, f[1], f[2]) },
53
+ 2 => .{ .binding = Reader.span(value, f[1], f[2]) },
54
+ 3 => .{ .equals = .{ .left = Reader.span(value, f[1], f[2]), .right = Reader.span(value, f[3], f[4]) } },
55
+ 4 => .{ .expression = .{ .inner = Reader.span(value, f[1], f[2]) } },
56
+ 5 => .{ .message = .{ .tag = Reader.span(value, f[1], f[2]), .payload = if (f[3] == f[4]) "" else Reader.span(value, f[3], f[4]) } },
57
+ 6 => .invalid,
58
+ else => @panic("invalid attribute kind"),
59
+ };
60
+ }
61
+
62
+ pub fn typedAttribute(owner: m.expr.Policy, input: m.MarkupAttr) m.TypedAttrValue {
63
+ if (input.typed) |typed| return typed.*;
64
+ return classify(owner, input.name, input.value, 2);
65
+ }
66
+
67
+ pub fn expression(owner: m.expr.Policy, value: []const u8) ?m.Expression {
68
+ return switch (classify(owner, "", value, 1)) {
69
+ .literal => |text| .{ .literal = text },
70
+ .binding => |path| .{ .binding = path },
71
+ .equals => |sides| .{ .equals = .{ .left = sides.left, .right = sides.right } },
72
+ .expression => |form| .{ .expression = form.inner },
73
+ .invalid => null,
74
+ .message => @panic("message returned for raw expression"),
75
+ };
76
+ }
77
+ pub fn canonicalize(owner: m.expr.Policy, arena: std.mem.Allocator, document: m.MarkupDocument) error{OutOfMemory}!m.MarkupDocument {
78
+ var request: Packet = .{};
79
+ defer request.bytes.deinit(scratch);
80
+ try request.bytes.appendSlice(scratch, &.{ 109, 1, 0, 0 });
81
+ try request.word(document.templates.len);
82
+ try request.word(@intFromBool(document.root != null));
83
+ for (document.templates) |node| try request.node(node);
84
+ if (document.root) |node| try request.node(node);
85
+ var header: [16]u8 = undefined;
86
+ if (owner(request.bytes.items, &header) != 16 or header[0] != 109 or header[1] != 1 or header[2] != 0)
87
+ @panic("invalid canonical sizing response");
88
+ for (header[3..8]) |b| if (b != 0) @panic("invalid canonical sizing reserved bytes");
89
+ const count = number(&header, 8);
90
+ if (count == 0) @panic("empty canonical tape");
91
+ const size = std.math.add(usize, 16, std.math.mul(usize, count, 48) catch return error.OutOfMemory) catch return error.OutOfMemory;
92
+ const tape = try scratch.alloc(u8, size);
93
+ defer scratch.free(tape);
94
+ request.bytes.items[2] = 1;
95
+ if (owner(request.bytes.items, tape) != size or tape[0] != 109 or tape[1] != 1 or tape[2] != 1 or number(tape, 8) != count)
96
+ @panic("invalid canonical tape");
97
+ for (tape[3..8]) |b| if (b != 0) @panic("invalid canonical tape reserved bytes");
98
+ var reader: Reader = .{ .owner = owner, .arena = arena, .tape = tape[16..] };
99
+ return reader.document(document);
100
+ }
101
+ const Record = struct { action: u8, code: u8, fields: [5]usize };
102
+ const Reader = struct {
103
+ owner: m.expr.Policy,
104
+ arena: std.mem.Allocator,
105
+ tape: []const u8,
106
+ cursor: usize = 0,
107
+
108
+ fn next(self: *Reader) Record {
109
+ if (self.cursor >= self.tape.len) @panic("truncated canonical tape");
110
+ const raw = self.tape[self.cursor..][0..48];
111
+ self.cursor += 48;
112
+ for (raw[2..8]) |b| if (b != 0) @panic("invalid canonical record reserved bytes");
113
+ var result: Record = .{ .action = raw[0], .code = raw[1], .fields = undefined };
114
+ for (&result.fields, 0..) |*field, i| field.* = number(raw, 8 + i * 8);
115
+ return result;
116
+ }
117
+ fn span(bytes: []const u8, start: usize, end: usize) []const u8 {
118
+ if (start > end or end > bytes.len) @panic("invalid compiled canonical span");
119
+ return bytes[start..end];
120
+ }
121
+ fn parsedTree(self: *Reader, inner: []const u8) !?*const m.expr.ExprTree {
122
+ const tree = try self.arena.create(m.expr.ExprTree);
123
+ tree.* = .{};
124
+ var diagnostic: m.expr.Diagnostic = .{};
125
+ if (!m.expr.parseWithPolicy(self.owner, inner, tree, &diagnostic)) return null;
126
+ return tree;
127
+ }
128
+ fn document(self: *Reader, input: m.MarkupDocument) error{OutOfMemory}!m.MarkupDocument {
129
+ var out = input;
130
+ var templates: []m.MarkupNode = &.{};
131
+ var next_template: usize = 0;
132
+ var root_stored = false;
133
+ while (true) {
134
+ const action = self.next();
135
+ switch (action.action) {
136
+ 1 => {
137
+ if (templates.len != 0 or action.fields[0] == 0 or action.fields[0] != input.templates.len) @panic("invalid canonical templates allocation");
138
+ templates = try self.arena.alloc(m.MarkupNode, action.fields[0]);
139
+ out.templates = templates;
140
+ },
141
+ 2 => {
142
+ if (action.fields[0] != next_template or next_template >= templates.len) @panic("invalid canonical template selection");
143
+ templates[next_template] = try self.node(input.templates[next_template]);
144
+ next_template += 1;
145
+ },
146
+ 3 => {
147
+ if (root_stored or next_template != templates.len) @panic("invalid canonical root selection");
148
+ out.root = try self.node(input.root orelse @panic("missing canonical root"));
149
+ root_stored = true;
150
+ },
151
+ 4 => {
152
+ if (self.cursor != self.tape.len or next_template != input.templates.len or root_stored != (input.root != null)) @panic("incomplete canonical document");
153
+ return out;
154
+ },
155
+ else => @panic("invalid canonical document action"),
156
+ }
157
+ }
158
+ }
159
+ fn node(self: *Reader, input: m.MarkupNode) error{OutOfMemory}!m.MarkupNode {
160
+ var out = input;
161
+ var attrs: []m.MarkupAttr = &.{};
162
+ var children: []m.MarkupNode = &.{};
163
+ var segments: std.ArrayListUnmanaged(m.TypedTextSegment) = .empty;
164
+ var next_attr: usize = 0;
165
+ var next_child: usize = 0;
166
+ var text_open = false;
167
+ while (true) {
168
+ const action = self.next();
169
+ const f = action.fields;
170
+ switch (action.action) {
171
+ 10 => {
172
+ if (attrs.len != 0 or f[0] == 0 or f[0] != input.attrs.len) @panic("invalid canonical attributes allocation");
173
+ attrs = try self.arena.alloc(m.MarkupAttr, f[0]);
174
+ out.attrs = attrs;
175
+ },
176
+ 11 => {
177
+ if (f[0] != next_attr or next_attr >= attrs.len) @panic("invalid canonical attribute selection");
178
+ const attribute = input.attrs[next_attr];
179
+ attrs[next_attr] = attribute;
180
+ const slot = try self.arena.create(m.TypedAttrValue);
181
+ slot.* = switch (action.code) {
182
+ 1 => .{ .literal = span(attribute.value, f[1], f[2]) },
183
+ 2 => .{ .binding = span(attribute.value, f[1], f[2]) },
184
+ 3 => .{ .equals = .{ .left = span(attribute.value, f[1], f[2]), .right = span(attribute.value, f[3], f[4]) } },
185
+ 4 => blk: {
186
+ const inner = span(attribute.value, f[1], f[2]);
187
+ break :blk .{ .expression = .{ .inner = inner, .tree = try self.parsedTree(inner) } };
188
+ },
189
+ 5 => .{ .message = .{ .tag = span(attribute.value, f[1], f[2]), .payload = if (f[3] == f[4]) "" else span(attribute.value, f[3], f[4]) } },
190
+ 6 => .invalid,
191
+ else => @panic("invalid compiled attribute kind"),
192
+ };
193
+ attrs[next_attr].typed = slot;
194
+ next_attr += 1;
195
+ },
196
+ 12 => {
197
+ if (text_open) @panic("duplicate canonical text open");
198
+ text_open = true;
199
+ },
200
+ 13 => {
201
+ if (!text_open) @panic("canonical segment outside text");
202
+ const segment: m.TypedTextSegment = switch (action.code) {
203
+ 1 => .{ .literal = span(input.text, f[0], f[1]) },
204
+ 2 => .{ .binding = span(input.text, f[0], f[1]) },
205
+ 3 => blk: {
206
+ const inner = span(input.text, f[0], f[1]);
207
+ break :blk .{ .expression = .{ .inner = inner, .tree = try self.parsedTree(inner) } };
208
+ },
209
+ 4 => .unterminated,
210
+ else => @panic("invalid compiled text segment kind"),
211
+ };
212
+ try segments.append(self.arena, segment);
213
+ },
214
+ 14 => {
215
+ if (!text_open) @panic("canonical text close without open");
216
+ out.typed_text = segments.items;
217
+ text_open = false;
218
+ },
219
+ 15 => {
220
+ if (children.len != 0 or f[0] == 0 or f[0] != input.children.len) @panic("invalid canonical children allocation");
221
+ children = try self.arena.alloc(m.MarkupNode, f[0]);
222
+ out.children = children;
223
+ },
224
+ 16 => {
225
+ if (f[0] != next_child or next_child >= children.len) @panic("invalid canonical child selection");
226
+ children[next_child] = try self.node(input.children[next_child]);
227
+ next_child += 1;
228
+ },
229
+ 17 => {
230
+ if (text_open or next_attr != input.attrs.len or next_child != input.children.len) @panic("incomplete canonical node");
231
+ return out;
232
+ },
233
+ else => @panic("invalid canonical node action"),
234
+ }
235
+ }
236
+ }
237
+ };