@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
@@ -12,8 +12,10 @@
12
12
  //! at most `max_expression_nodes` nodes — termination is structural, not a
13
13
  //! property anyone has to prove per expression.
14
14
  //!
15
- //! Engine seam: the parser and type checker are comptime-callable and
16
- //! allocation-free (fixed arrays). Each engine resolves the tree's binding
15
+ //! Engine seam: the reference parser and type checker are comptime-callable
16
+ //! and allocation-free (fixed arrays). Runtime canonicalization uses the
17
+ //! installed compiled grammar with exact native literal-conversion capabilities.
18
+ //! Each engine resolves the tree's binding
17
19
  //! nodes to `Value`s its own way (interpreter: scope chain at runtime;
18
20
  //! compiled engine: comptime-unrolled field access), then hands them to the
19
21
  //! ONE `eval` below — identical inputs through identical arithmetic and
@@ -293,6 +295,13 @@ pub fn parse(source: []const u8, tree: *ExprTree, diagnostic: *Diagnostic) bool
293
295
  return true;
294
296
  }
295
297
 
298
+ /// Runtime compiled grammar with the original allocation-free parser retained
299
+ /// for compile-time consumers and the independent native reference.
300
+ pub fn parseWithPolicy(owner: ?Policy, source: []const u8, tree: *ExprTree, diagnostic: *Diagnostic) bool {
301
+ if (owner) |callback| return @import("ui_markup_parser.zig").parse(callback, source, tree, diagnostic);
302
+ return parse(source, tree, diagnostic);
303
+ }
304
+
296
305
  const ExprParseError = error{ExprSyntax};
297
306
 
298
307
  const ExprParser = struct {
@@ -888,6 +897,187 @@ pub fn eval(tree: *const ExprTree, values: *[max_expression_nodes]Value, arena:
888
897
  return .{ .value = values[tree.root] };
889
898
  }
890
899
 
900
+ /// Compiled runtime programs carry only bounded metadata. Values and their
901
+ /// byte lifetimes remain in the caller's arena; exact integers never pass
902
+ /// through a floating-point transport. The original evaluator above remains
903
+ /// the native reference and the path for native extensions without an owner.
904
+ pub const Policy = *const fn ([]const u8, []u8) usize;
905
+
906
+ pub fn equalWithPolicy(owner: ?Policy, a: Value, b: Value) bool {
907
+ const policy = owner orelse return Value.eql(a, b);
908
+ return @import("ui_markup_scalars.zig").evaluate(policy, 9, a, b).value.boolean;
909
+ }
910
+
911
+ pub fn truthyWithPolicy(owner: ?Policy, value: Value) bool {
912
+ const policy = owner orelse return value.truthy();
913
+ return @import("ui_markup_scalars.zig").evaluate(policy, 36, value, .{ .boolean = false }).value.boolean;
914
+ }
915
+
916
+ pub fn appendValueWithPolicy(owner: ?Policy, out: *std.ArrayListUnmanaged(u8), arena: std.mem.Allocator, value: Value) error{OutOfMemory}!void {
917
+ if (owner) |policy| return @import("ui_markup_display.zig").append(policy, out, arena, value);
918
+ return appendValue(out, arena, value);
919
+ }
920
+
921
+ pub fn evalWithPolicy(owner: ?Policy, tree: *const ExprTree, values: *[max_expression_nodes]Value, arena: std.mem.Allocator) error{OutOfMemory}!EvalOutcome {
922
+ const policy = owner orelse return eval(tree, values, arena);
923
+ if (tree.len == 0 or tree.len > max_expression_nodes or tree.root >= tree.len) @panic("invalid expression tree budget");
924
+ var input: [8 + max_expression_nodes * 12]u8 = @splat(0);
925
+ input[0..4].* = .{ 102, 1, @intCast(tree.len), @intCast(tree.root) };
926
+ for (tree.nodes[0..tree.len], 0..) |node, i| {
927
+ const at = 8 + i * 12;
928
+ input[at] = @intFromEnum(node.kind);
929
+ input[at + 1] = @intFromEnum(node.function);
930
+ input[at + 2] = node.arg_count;
931
+ if (node.kind == .binding) input[at + 3] = @as(u8, @intFromEnum(kindOf(values[i]))) + 1;
932
+ input[at + 4] = @intCast(node.lhs);
933
+ input[at + 5] = @intCast(node.rhs);
934
+ for (node.args, 0..) |arg, j| input[at + 6 + j] = @intCast(arg);
935
+ }
936
+ var output: [8 + max_expression_nodes * 2 * 4]u8 = undefined;
937
+ const size = policy(input[0 .. 8 + tree.len * 12], &output);
938
+ if (size < 8 or size > output.len or output[0] != 102 or output[1] != 1 or output[3] != tree.root or
939
+ size != 8 + @as(usize, output[2]) * 4 or std.mem.readInt(u32, output[4..8], .little) != 0)
940
+ @panic("invalid expression program header");
941
+ // Admit the complete program before allocating or touching any slot.
942
+ var cursor: usize = 0;
943
+ var digits = false;
944
+ var failed = false;
945
+ for (0..output[2]) |i| {
946
+ const at = 8 + i * 4;
947
+ const slot = output[at];
948
+ const capability = output[at + 1];
949
+ const teaching = output[at + 2];
950
+ const result_kind = output[at + 3];
951
+ if (failed or slot != cursor or slot >= tree.len or capability > 35) @panic("invalid expression program progress");
952
+ const node = tree.nodes[slot];
953
+ if (capability == 17) {
954
+ if (digits or teaching != 0 or result_kind != 0 or node.kind != .call or
955
+ !(node.function == .fixed or node.function == .pad or (node.function == .percent and node.arg_count > 1)))
956
+ @panic("invalid expression digits capability");
957
+ digits = true;
958
+ } else {
959
+ if (capability == 35) {
960
+ if (result_kind != 0 or !(teaching >= 1 and teaching <= 5 or teaching >= 16 and teaching <= 32))
961
+ @panic("invalid expression teaching capability");
962
+ failed = true;
963
+ } else {
964
+ const expected: u8 = switch (node.kind) {
965
+ .literal_int, .literal_float, .literal_string, .literal_bool => 0,
966
+ .binding => 1,
967
+ .call => 18 + @as(u8, @intFromEnum(node.function)),
968
+ else => @as(u8, @intFromEnum(node.kind)) - 3,
969
+ };
970
+ if (capability != expected or teaching != 0 or result_kind < 1 or result_kind > 4)
971
+ @panic("invalid expression storage capability");
972
+ }
973
+ cursor += 1;
974
+ digits = false;
975
+ }
976
+ }
977
+ if (digits or (!failed and cursor != tree.len)) @panic("incomplete expression program");
978
+ for (0..output[2]) |i| {
979
+ const at = 8 + i * 4;
980
+ const slot = output[at];
981
+ const node = tree.nodes[slot];
982
+ const capability = output[at + 1];
983
+ if (capability == 35) return .{ .fail = programMessage(output[at + 2]) };
984
+ if (capability == 17) {
985
+ if (digitsArg(values[node.args[1]]) == null) return .{ .fail = digits_range_message };
986
+ continue;
987
+ }
988
+ if (capability == 1) {
989
+ if (@as(u8, @intFromEnum(kindOf(values[slot]))) + 1 != output[at + 3]) @panic("invalid expression binding kind");
990
+ continue;
991
+ }
992
+ const outcome = try expressionCapability(policy, capability, node, values, arena);
993
+ switch (outcome) {
994
+ .fail => return outcome,
995
+ .value => |value| {
996
+ if (@as(u8, @intFromEnum(kindOf(value))) + 1 != output[at + 3]) @panic("invalid expression result kind");
997
+ values[slot] = value;
998
+ },
999
+ }
1000
+ }
1001
+ return .{ .value = values[tree.root] };
1002
+ }
1003
+
1004
+ fn programMessage(code: u8) []const u8 {
1005
+ return switch (code) {
1006
+ 1 => negate_type_message,
1007
+ 2 => logic_type_message,
1008
+ 3 => arithmetic_type_message,
1009
+ 4 => ordering_type_message,
1010
+ 5 => digits_range_message,
1011
+ 16...32 => signatureFor(@enumFromInt(code - 16)),
1012
+ else => unreachable,
1013
+ };
1014
+ }
1015
+
1016
+ /// Explicit value/storage capabilities. The program selects their order;
1017
+ /// the scalar owner preserves exact integers through copied limbs, while
1018
+ /// native supplies original f32 primitives and deterministic formatting.
1019
+ fn expressionCapability(owner: Policy, capability: u8, node: ExprNode, values: *[max_expression_nodes]Value, arena: std.mem.Allocator) error{OutOfMemory}!EvalOutcome {
1020
+ const value: Value = switch (capability) {
1021
+ 0 => switch (node.kind) {
1022
+ .literal_int => .{ .integer = node.int },
1023
+ .literal_float => .{ .float = node.float },
1024
+ .literal_string => .{ .string = node.text },
1025
+ .literal_bool => .{ .boolean = node.boolean },
1026
+ else => unreachable,
1027
+ },
1028
+ 2...7 => return @import("ui_markup_scalars.zig").evaluate(owner, capability, values[node.lhs], values[node.rhs]),
1029
+ 8 => blk: {
1030
+ var out: std.ArrayListUnmanaged(u8) = .empty;
1031
+ try appendValueWithPolicy(owner, &out, arena, values[node.lhs]);
1032
+ try appendValueWithPolicy(owner, &out, arena, values[node.rhs]);
1033
+ break :blk .{ .string = out.items };
1034
+ },
1035
+ 9...16 => return @import("ui_markup_scalars.zig").evaluate(owner, capability, values[node.lhs], values[node.rhs]),
1036
+ 19, 21...26, 33, 34 => return textCapability(owner, node, values, arena),
1037
+ 18, 20 => blk: {
1038
+ const digits = if (node.function == .percent and node.arg_count == 1) 0 else digitsArg(values[node.args[1]]) orelse return .{ .fail = digits_range_message };
1039
+ const operand = values[node.args[0]];
1040
+ if (operand != .integer and operand != .float) return .{ .fail = signatureFor(node.function) };
1041
+ const bytes = (try @import("ui_markup_display.zig").fixed(owner, operand, digits, node.function == .percent, arena)) orelse
1042
+ return .{ .fail = non_finite_message };
1043
+ break :blk .{ .string = bytes };
1044
+ },
1045
+ 27...32 => return @import("ui_markup_scalars.zig").evaluate(owner, capability, values[node.args[0]], values[node.args[1]]),
1046
+ else => unreachable,
1047
+ };
1048
+ return .{ .value = value };
1049
+ }
1050
+
1051
+ fn textCapability(owner: Policy, node: ExprNode, values: *[max_expression_nodes]Value, arena: std.mem.Allocator) error{OutOfMemory}!EvalOutcome {
1052
+ const text = @import("ui_markup_text.zig");
1053
+ const a = values[node.args[0]];
1054
+ const signature = signatureFor(node.function);
1055
+ const bytes = switch (node.function) {
1056
+ .upper, .lower => if (a == .string)
1057
+ try text.map(owner, if (node.function == .upper) 0 else 1, a.string, arena)
1058
+ else
1059
+ return .{ .fail = signature },
1060
+ .trim => if (a == .string) text.trim(owner, a.string) else return .{ .fail = signature },
1061
+ .thousands => if (a == .integer) try text.decimal(owner, 3, a.integer, 0, arena) else return .{ .fail = signature },
1062
+ .pad => blk: {
1063
+ const width = digitsArg(values[node.args[1]]) orelse return .{ .fail = digits_range_message };
1064
+ if (a != .integer) return .{ .fail = signature };
1065
+ break :blk try text.decimal(owner, 4, a.integer, width, arena);
1066
+ },
1067
+ .date, .time, .datetime => blk: {
1068
+ if (a != .integer) return .{ .fail = signature };
1069
+ const mode: u8 = if (node.function == .date) 5 else if (node.function == .time) 6 else 7;
1070
+ break :blk (try text.timestamp(owner, mode, a.integer, arena)) orelse return .{ .fail = timestamp_range_message };
1071
+ },
1072
+ .plural => blk: {
1073
+ if (a != .integer or values[node.args[1]] != .string or values[node.args[2]] != .string) return .{ .fail = signature };
1074
+ break :blk values[node.args[text.plural(owner, a.integer)]].string;
1075
+ },
1076
+ else => unreachable,
1077
+ };
1078
+ return .{ .value = .{ .string = bytes } };
1079
+ }
1080
+
891
1081
  fn arithmetic(kind: NodeKind, left: Value, right: Value) error{OutOfMemory}!EvalOutcome {
892
1082
  if (left == .integer and right == .integer) {
893
1083
  const a = left.integer;
@@ -0,0 +1,262 @@
1
+ //! Copied import programs and explicit source/tree/arena capabilities.
2
+ //! No source pointer or allocator handle crosses the compiled boundary.
3
+ const std = @import("std");
4
+ const m = @import("ui_markup.zig");
5
+ const scratch = std.heap.page_allocator;
6
+ const Stamp = *const fn (std.mem.Allocator, m.MarkupNode, []const u8) error{OutOfMemory}!m.MarkupNode;
7
+
8
+ const Packet = struct {
9
+ bytes: std.ArrayListUnmanaged(u8) = .empty,
10
+ fn init(stage: u8) !Packet {
11
+ var self: Packet = .{};
12
+ errdefer self.deinit();
13
+ try self.bytes.appendSlice(scratch, &.{ 108, 1, stage, 0 });
14
+ return self;
15
+ }
16
+ fn deinit(self: *Packet) void {
17
+ self.bytes.deinit(scratch);
18
+ }
19
+ fn word(self: *Packet, value: usize) !void {
20
+ if (value > 9007199254740991) @panic("import transport integer exceeds exact range");
21
+ var raw: [8]u8 = undefined;
22
+ std.mem.writeInt(u64, &raw, @bitCast(@as(f64, @floatFromInt(value))), .little);
23
+ try self.bytes.appendSlice(scratch, &raw);
24
+ }
25
+ fn blob(self: *Packet, value: []const u8) !void {
26
+ try self.word(value.len);
27
+ try self.bytes.appendSlice(scratch, value);
28
+ }
29
+ fn decimal(self: *Packet, value: usize) !void {
30
+ var buffer: [40]u8 = undefined;
31
+ try self.blob(std.fmt.bufPrint(&buffer, "{d}", .{value}) catch unreachable);
32
+ }
33
+ fn run(self: *const Packet, owner: m.expr.Policy) !Program {
34
+ const capacity = std.math.add(usize, 512, std.math.mul(usize, self.bytes.items.len, 2) catch return error.OutOfMemory) catch return error.OutOfMemory;
35
+ const output = try scratch.alloc(u8, capacity);
36
+ errdefer scratch.free(output);
37
+ const size = owner(self.bytes.items, output);
38
+ if (size < 32 or size > capacity or output[0] != 108 or output[1] != 1 or output[2] != self.bytes.items[2] or output[3] == 0 or output[3] > 15)
39
+ @panic("invalid compiled import response");
40
+ for (output[4..8]) |b| if (b != 0) @panic("invalid import reserved bytes");
41
+ return .{ .storage = output, .bytes = output[0..size], .action = output[3], .a = number(output, 8), .b = number(output, 16), .c = number(output, 24) };
42
+ }
43
+ };
44
+ fn number(bytes: []const u8, at: usize) usize {
45
+ if (at > bytes.len or bytes.len - at < 8) @panic("short import integer");
46
+ const value: f64 = @bitCast(std.mem.readInt(u64, bytes[at..][0..8], .little));
47
+ if (!std.math.isFinite(value) or value < 0 or value > 9007199254740991 or value != @trunc(value) or value >= @as(f64, @floatFromInt(std.math.maxInt(usize))))
48
+ @panic("invalid compiled import integer");
49
+ return @intFromFloat(value);
50
+ }
51
+ const Program = struct {
52
+ storage: []u8,
53
+ bytes: []const u8,
54
+ action: u8,
55
+ a: usize,
56
+ b: usize,
57
+ c: usize,
58
+ fn deinit(self: *Program) void {
59
+ scratch.free(self.storage);
60
+ }
61
+ fn payload(self: Program) []const u8 {
62
+ return self.bytes[32..];
63
+ }
64
+ fn formatted(self: Program, arena: std.mem.Allocator) ![]const u8 {
65
+ var out = try std.Io.Writer.Allocating.initCapacity(arena, self.b);
66
+ defer out.deinit();
67
+ const bytes = self.payload();
68
+ var at: usize = 0;
69
+ for (0..self.a) |_| {
70
+ const length = number(bytes, at);
71
+ at += 8;
72
+ if (length > bytes.len - at) @panic("short formatted import chunk");
73
+ out.writer.writeAll(bytes[at..][0..length]) catch return error.OutOfMemory;
74
+ at += length;
75
+ }
76
+ if (at != bytes.len) @panic("trailing formatted import chunks");
77
+ return out.toOwnedSlice();
78
+ }
79
+ };
80
+
81
+ pub fn resolve(owner: m.expr.Policy, arena: std.mem.Allocator, root_dir: []const u8, root_name: []const u8, source: []const u8, loader: m.ImportLoader, diagnostic: *m.MarkupErrorInfo, stamp: Stamp) m.ResolveError!m.MarkupDocument {
82
+ var resolver: Resolver = .{ .owner = owner, .arena = arena, .root_dir = root_dir, .loader = loader, .diagnostic = diagnostic, .stamp = stamp };
83
+ const root = try resolver.visit(root_name, source, 0);
84
+ return .{ .imports = &.{}, .templates = resolver.templates.items, .root = root, .source_bytes = resolver.source_bytes };
85
+ }
86
+ const Resolver = struct {
87
+ owner: m.expr.Policy,
88
+ arena: std.mem.Allocator,
89
+ root_dir: []const u8,
90
+ loader: m.ImportLoader,
91
+ diagnostic: *m.MarkupErrorInfo,
92
+ stamp: Stamp,
93
+ templates: std.ArrayListUnmanaged(m.MarkupNode) = .empty,
94
+ visited: std.ArrayListUnmanaged([]const u8) = .empty,
95
+ chain: [m.max_import_depth][]const u8 = undefined,
96
+ file_count: usize = 0,
97
+ source_bytes: usize = 0,
98
+
99
+ fn visit(self: *Resolver, path: []const u8, source: []const u8, depth: usize) m.ResolveError!?m.MarkupNode {
100
+ if (depth >= self.chain.len) @panic("invalid compiled import recursion");
101
+ self.chain[depth] = path;
102
+ self.source_bytes += source.len;
103
+ var parser = m.Parser.init(self.arena, source);
104
+ const document = parser.parseWithPolicy(self.owner) catch |err| {
105
+ if (err == error.MarkupSyntax) {
106
+ self.diagnostic.* = parser.diagnostic;
107
+ self.diagnostic.path = path;
108
+ }
109
+ return err;
110
+ };
111
+ var phase: usize = 0;
112
+ var cursor: usize = 0;
113
+ while (true) {
114
+ var packet = try Packet.init(2);
115
+ defer packet.deinit();
116
+ for ([_]usize{ phase, cursor, document.imports.len, document.templates.len, @intFromBool(document.root != null), depth }) |value| try packet.word(value);
117
+ var program = try packet.run(self.owner);
118
+ defer program.deinit();
119
+ switch (program.action) {
120
+ 9 => {
121
+ if (program.a >= document.imports.len or program.b != 0 or program.c != program.a + 1) @panic("invalid import selection");
122
+ try self.importFile(path, document.imports[program.a], depth);
123
+ phase = program.b;
124
+ cursor = program.c;
125
+ },
126
+ 10 => {
127
+ if (program.a >= document.templates.len or program.b != 1 or program.c != program.a + 1) @panic("invalid template selection");
128
+ try self.template(path, document.templates[program.a]);
129
+ phase = program.b;
130
+ cursor = program.c;
131
+ },
132
+ 11 => return self.fail(path, document.root orelse @panic("missing rejected import root"), message(program.a)),
133
+ 12 => return try self.stamp(self.arena, document.root orelse @panic("missing selected import root"), path),
134
+ 13 => return null,
135
+ else => @panic("invalid compiled visit action"),
136
+ }
137
+ }
138
+ }
139
+ fn fail(self: *Resolver, path: []const u8, node: m.MarkupNode, text: []const u8) m.ResolveError {
140
+ self.diagnostic.* = .{ .line = node.line, .column = node.column, .message = text, .path = path };
141
+ return error.MarkupImport;
142
+ }
143
+ fn importFile(self: *Resolver, path: []const u8, node: m.MarkupNode, depth: usize) m.ResolveError!void {
144
+ var shape = try Packet.init(0);
145
+ defer shape.deinit();
146
+ try shape.blob(self.root_dir);
147
+ try shape.blob(path);
148
+ try shape.word(m.max_import_path_len);
149
+ try shape.word(node.children.len);
150
+ try shape.word(node.attrs.len);
151
+ for (node.attrs) |attribute| {
152
+ try shape.blob(attribute.name);
153
+ try shape.blob(attribute.value);
154
+ }
155
+ var selected = try shape.run(self.owner);
156
+ defer selected.deinit();
157
+ switch (selected.action) {
158
+ 1 => return self.fail(path, node, message(selected.a)),
159
+ 2 => {
160
+ if (selected.b >= node.attrs.len) @panic("invalid import attribute selection");
161
+ const attribute = node.attrs[selected.b];
162
+ self.diagnostic.* = .{ .line = attribute.line, .column = attribute.column, .message = message(selected.a), .path = path };
163
+ return error.MarkupImport;
164
+ },
165
+ 3 => {
166
+ if (node.children.len == 0) @panic("missing rejected import child");
167
+ return self.fail(path, node.children[0], message(selected.a));
168
+ },
169
+ 4 => {},
170
+ else => @panic("invalid compiled import path action"),
171
+ }
172
+ // Keep the reference path duplication before every graph decision,
173
+ // including dedupe and cycle failures.
174
+ if (selected.payload().len > m.max_import_path_len) @panic("oversized compiled import path");
175
+ const resolved = try self.arena.dupe(u8, selected.payload());
176
+ var admission = try Packet.init(1);
177
+ defer admission.deinit();
178
+ try admission.blob(resolved);
179
+ for ([_]usize{ depth, self.file_count, depth + 1 }) |value| try admission.word(value);
180
+ for (self.chain[0 .. depth + 1]) |link| try admission.blob(link);
181
+ try admission.word(self.visited.items.len);
182
+ for (self.visited.items) |seen| try admission.blob(seen);
183
+ var program = try admission.run(self.owner);
184
+ defer program.deinit();
185
+ switch (program.action) {
186
+ 1 => return self.fail(path, node, message(program.a)),
187
+ 5 => {
188
+ var out: std.ArrayListUnmanaged(u8) = .empty;
189
+ const bytes = program.payload();
190
+ var at: usize = 0;
191
+ for (0..program.a) |_| {
192
+ const length = number(bytes, at);
193
+ at += 8;
194
+ if (length > bytes.len - at) @panic("short import diagnostic chunk");
195
+ try out.appendSlice(self.arena, bytes[at..][0..length]);
196
+ at += length;
197
+ }
198
+ if (at != bytes.len) @panic("trailing import diagnostic chunks");
199
+ return self.fail(path, node, out.items);
200
+ },
201
+ 6 => return,
202
+ 7, 8 => {
203
+ if (program.a != self.file_count + 1) @panic("invalid import file counter");
204
+ self.file_count = program.a;
205
+ if (program.action == 8) return self.fail(path, node, m.import_count_message);
206
+ },
207
+ else => @panic("invalid import admission action"),
208
+ }
209
+ const child_source = self.loader.load(self.loader.context, self.arena, resolved) orelse {
210
+ var missing = try Packet.init(4);
211
+ defer missing.deinit();
212
+ try missing.blob(resolved);
213
+ var text = try missing.run(self.owner);
214
+ defer text.deinit();
215
+ if (text.action != 15) @panic("invalid missing import diagnostic");
216
+ return self.fail(path, node, try text.formatted(self.arena));
217
+ };
218
+ _ = try self.visit(resolved, child_source, depth + 1);
219
+ try self.visited.append(self.arena, resolved);
220
+ }
221
+ fn template(self: *Resolver, path: []const u8, node: m.MarkupNode) m.ResolveError!void {
222
+ var packet = try Packet.init(3);
223
+ defer packet.deinit();
224
+ const name = node.attr("name");
225
+ try packet.word(self.templates.items.len);
226
+ try packet.word(@intFromBool(name != null));
227
+ try packet.blob(name orelse "");
228
+ for (self.templates.items) |existing| {
229
+ const prior = existing.attr("name");
230
+ try packet.word(@intFromBool(prior != null));
231
+ try packet.blob(prior orelse "");
232
+ try packet.blob(existing.src_path);
233
+ try packet.decimal(existing.line);
234
+ try packet.decimal(existing.column);
235
+ }
236
+ var program = try packet.run(self.owner);
237
+ defer program.deinit();
238
+ switch (program.action) {
239
+ 1 => return self.fail(path, node, message(program.a)),
240
+ 15 => return self.fail(path, node, try program.formatted(self.arena)),
241
+ 14 => try self.templates.append(self.arena, try self.stamp(self.arena, node, path)),
242
+ else => @panic("invalid template admission action"),
243
+ }
244
+ }
245
+ };
246
+ fn message(code: usize) []const u8 {
247
+ return switch (code) {
248
+ 1 => m.import_children_message,
249
+ 2 => m.import_attrs_message,
250
+ 3 => m.import_src_message,
251
+ 4 => m.import_src_too_long_message,
252
+ 5 => m.import_src_absolute_message,
253
+ 6 => m.import_src_separator_message,
254
+ 7 => m.import_src_extension_message,
255
+ 8 => m.import_src_escape_message,
256
+ 9 => m.import_depth_message,
257
+ 10 => m.import_count_message,
258
+ 11 => m.max_templates_message,
259
+ 12 => m.import_view_root_message,
260
+ else => @panic("invalid compiled import diagnostic"),
261
+ };
262
+ }
@@ -0,0 +1,117 @@
1
+ //! Copied runtime expression parser transport. Native supplies source/tree
2
+ //! storage and exact numeric literal conversion; the compiled owner chooses
3
+ //! grammar, requested spans, admission order, nodes and teaching diagnostics.
4
+ const std = @import("std");
5
+ const expr = @import("ui_markup_expr.zig");
6
+
7
+ pub fn parse(owner: expr.Policy, source: []const u8, tree: *expr.ExprTree, diagnostic: *expr.Diagnostic) bool {
8
+ var input: [521]u8 = @splat(0);
9
+ input[0] = 106;
10
+ input[1] = 1;
11
+ const len: usize = @min(source.len, 257);
12
+ std.mem.writeInt(u16, input[2..4], @intCast(len), .little);
13
+ const size = if (len > 256) 8 else 9 + len * 2;
14
+ if (len <= 256) @memcpy(input[8 .. 8 + len], source);
15
+ var numbers: [257]expr.Value = undefined;
16
+ var output: [1296]u8 = undefined;
17
+ // Each continuation admits one previously unseen literal. At most 64
18
+ // nodes can be accepted, with one further numeric probe before overflow.
19
+ for (0..66) |_| {
20
+ const count = owner(input[0..size], &output);
21
+ if (count < 16 or count > output.len or output[0] != 106 or output[1] != 1 or
22
+ output[2] > 2 or output[4] > 64 or count != 16 + @as(usize, output[4]) * 20)
23
+ @panic("invalid compiled expression parser response");
24
+ for (output[12..16]) |byte| if (byte != 0) @panic("invalid parser reserved byte");
25
+ if (output[2] == 2) {
26
+ const start = word(&output, 8);
27
+ const length = word(&output, 10);
28
+ if (len > 256 or length == 0 or start >= len or length > len - start or input[8 + len + start] != 0)
29
+ @panic("invalid compiled numeric span");
30
+ const text = source[start .. start + length];
31
+ const value: ?expr.Value = if (std.mem.indexOfScalar(u8, text, '.') != null) blk: {
32
+ const value = std.fmt.parseFloat(f32, text) catch break :blk null;
33
+ break :blk if (std.math.isFinite(value)) .{ .float = value } else null;
34
+ } else blk: {
35
+ const value = std.fmt.parseInt(i64, text, 10) catch break :blk null;
36
+ break :blk .{ .integer = value };
37
+ };
38
+ input[8 + len + start] = if (value != null) 1 else 2;
39
+ if (value) |number| numbers[start] = number;
40
+ continue;
41
+ }
42
+ if (len <= 256) {
43
+ tree.len = output[4];
44
+ for (0..tree.len) |i| {
45
+ const at = 16 + i * 20;
46
+ const kind = output[at];
47
+ const start = word(&output, at + 14);
48
+ const length = word(&output, at + 16);
49
+ if (kind > @intFromEnum(expr.NodeKind.call) or output[at + 1] > @intFromEnum(expr.FunctionId.pad) or
50
+ output[at + 2] > 4 or output[at + 3] > 1 or output[at + 10] > 1 or output[at + 11] != 0 or
51
+ word(&output, at + 18) != 0 or start > len or length > len - start or word(&output, at + 12) > len)
52
+ @panic("invalid compiled expression node");
53
+ var node: expr.ExprNode = .{
54
+ .kind = @enumFromInt(kind),
55
+ .function = @enumFromInt(output[at + 1]),
56
+ .arg_count = output[at + 2],
57
+ .boolean = output[at + 3] == 1,
58
+ .lhs = output[at + 4],
59
+ .rhs = output[at + 5],
60
+ .comparison_operand = output[at + 10] == 1,
61
+ .offset = @intCast(word(&output, at + 12)),
62
+ };
63
+ if ((kind == 5 or kind == 6) and node.lhs >= i or
64
+ (kind >= 7 and kind <= 19) and (node.lhs >= i or node.rhs >= i))
65
+ @panic("invalid compiled expression operands");
66
+ for (0..4) |a| {
67
+ node.args[a] = output[at + 6 + a];
68
+ if (a < node.arg_count and node.args[a] >= i) @panic("invalid compiled expression argument");
69
+ }
70
+ switch (node.kind) {
71
+ .literal_int, .literal_float => {
72
+ if (start >= len or input[8 + len + start] != 1) @panic("unadmitted expression literal");
73
+ const number = numbers[start];
74
+ if (node.kind == .literal_int) node.int = number.integer else node.float = number.float;
75
+ },
76
+ .binding, .literal_string => node.text = source[start .. start + length],
77
+ else => {},
78
+ }
79
+ tree.nodes[i] = node;
80
+ }
81
+ }
82
+ if (output[2] == 1) {
83
+ diagnostic.* = .{ .offset = word(&output, 6), .message = message(output[3]) };
84
+ return false;
85
+ }
86
+ if (tree.len == 0 or output[5] >= tree.len) @panic("invalid compiled expression root");
87
+ tree.root = output[5];
88
+ return true;
89
+ }
90
+ @panic("compiled expression parser did not converge");
91
+ }
92
+
93
+ fn word(bytes: []const u8, at: usize) usize {
94
+ return std.mem.readInt(u16, bytes[at..][0..2], .little);
95
+ }
96
+
97
+ fn message(code: u8) []const u8 {
98
+ if (code >= 32 and code < 32 + expr.function_specs.len) return expr.function_specs[code - 32].signature;
99
+ return switch (code) {
100
+ 1 => expr.empty_expression_message,
101
+ 2 => expr.expression_too_long_message,
102
+ 3 => expr.expression_too_many_nodes_message,
103
+ 4 => expr.expression_too_deep_message,
104
+ 5 => expr.expression_too_many_args_message,
105
+ 6 => expr.comparison_chain_message,
106
+ 7 => expr.unterminated_string_message,
107
+ 8 => expr.integer_literal_overflow_message,
108
+ 9 => expr.number_literal_range_message,
109
+ 10 => expr.unknown_function_message,
110
+ 11 => expr.expected_operand_message,
111
+ 12 => expr.expected_operator_message,
112
+ 13 => expr.expected_close_paren_message,
113
+ 14 => expr.call_syntax_message,
114
+ 15 => expr.clock_function_message,
115
+ else => @panic("invalid compiled parser diagnostic"),
116
+ };
117
+ }
@@ -0,0 +1,76 @@
1
+ //! Runtime markup facts and explicit native storage/callback capabilities.
2
+ const std = @import("std");
3
+ const markup = @import("ui_markup.zig");
4
+ const elements = @import("ui_markup_elements.zig");
5
+ const scratch = std.heap.page_allocator;
6
+ pub const Request = struct {
7
+ inner: elements.Request,
8
+ pub fn init(mode: u8) Request {
9
+ var inner = elements.Request.init(mode);
10
+ inner.bytes.items[0] = 114;
11
+ return .{ .inner = inner };
12
+ }
13
+ pub fn deinit(self: *Request) void {
14
+ self.inner.deinit();
15
+ }
16
+ pub fn word(self: *Request, value: usize) void {
17
+ self.inner.word(value);
18
+ }
19
+ pub fn words(self: *Request, values: []const usize) void {
20
+ self.inner.words(values);
21
+ }
22
+ pub fn blob(self: *Request, value: []const u8) void {
23
+ self.inner.blob(value);
24
+ }
25
+ pub fn names(self: *Request, value: []const []const u8) void {
26
+ self.inner.names(value);
27
+ }
28
+ pub fn node(self: *Request, value: markup.MarkupNode, children: bool) void {
29
+ self.inner.node(value, children);
30
+ }
31
+ pub fn decision(self: *Request, owner: markup.expr.Policy) elements.Record {
32
+ var result = self.program(owner) catch @panic("runtime markup transport allocation failed");
33
+ defer result.deinit();
34
+ const row = result.next() orelse @panic("missing runtime markup decision");
35
+ if (result.next() != null) @panic("extra runtime markup decision");
36
+ return row;
37
+ }
38
+ pub fn program(self: *Request, owner: markup.expr.Policy) error{OutOfMemory}!elements.Program {
39
+ self.inner.bytes.items[3] = 0;
40
+ var sizing: [16]u8 = undefined;
41
+ if (owner(self.inner.bytes.items, &sizing) != sizing.len) @panic("invalid runtime markup sizing");
42
+ const count = header(&sizing, 0);
43
+ const size = std.math.add(usize, 16, std.math.mul(usize, count, 48) catch return error.OutOfMemory) catch return error.OutOfMemory;
44
+ const out = try scratch.alloc(u8, size);
45
+ errdefer scratch.free(out);
46
+ self.inner.bytes.items[3] = 1;
47
+ if (owner(self.inner.bytes.items, out) != size or header(out, 1) != count) @panic("invalid runtime markup program");
48
+ return .{ .bytes = out };
49
+ }
50
+ };
51
+ fn header(bytes: []const u8, phase: u8) usize {
52
+ if (bytes.len < 16 or bytes[0] != 114 or bytes[1] != 1 or bytes[2] != phase) @panic("invalid runtime markup header");
53
+ for (bytes[3..8]) |byte| if (byte != 0) @panic("invalid runtime markup reserved byte");
54
+ return elements.number(bytes, 8);
55
+ }
56
+ pub fn decide(owner: markup.expr.Policy, mode: u8, facts: []const usize) elements.Record {
57
+ var request = Request.init(mode);
58
+ defer request.deinit();
59
+ request.words(facts);
60
+ return request.decision(owner);
61
+ }
62
+ pub fn message(code: u8) []const u8 {
63
+ return switch (code) {
64
+ 1 => "key does not name a field on the item",
65
+ 2 => "key fields must be integers or strings",
66
+ 3 => markup.image_source_element_message,
67
+ 4 => markup.image_source_binding_message,
68
+ 5 => markup.image_source_complete_message,
69
+ 7 => "expected a number",
70
+ 8 => "text elements take a single run of text",
71
+ 9 => markup.span_text_only_message,
72
+ 10 => "text elements may only contain text",
73
+ 11 => "unterminated interpolation",
74
+ else => @panic("invalid runtime markup diagnostic"),
75
+ };
76
+ }