@native-sdk/cli 0.10.1 → 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 (555) hide show
  1. package/README.md +8 -6
  2. package/bin/native.js +2 -1
  3. package/build/app.zig +393 -97
  4. package/build/corewire.zig +53 -0
  5. package/build/test_shards.zig +49 -0
  6. package/build/ts_run.mjs +3 -3
  7. package/build.zig +2030 -83
  8. package/native-sdk.d.ts +5 -1
  9. package/package.json +20 -14
  10. package/packages/core/compile-surface/core.ts +164 -72
  11. package/packages/core/package-lock.json +75 -357
  12. package/packages/core/package.json +25 -4
  13. package/packages/core/scripts/compiler_command.mjs +17 -12
  14. package/packages/core/scripts/compiler_typecheck.mjs +1 -0
  15. package/packages/core/scripts/gen_declarations.mjs +3 -2
  16. package/packages/core/scripts/gen_service_surface.mjs +22 -10
  17. package/packages/core/scripts/install_scriptc.mjs +182 -0
  18. package/packages/core/scripts/invoke_scriptc.mjs +18 -0
  19. package/packages/core/scripts/merge_static_archives.mjs +6 -1
  20. package/packages/core/scripts/npm_static_spike.mjs +2 -2
  21. package/packages/core/scripts/run_external_core_compiler.mjs +22 -10
  22. package/packages/core/scripts/run_external_service_compiler.mjs +15 -15
  23. package/packages/core/scripts/run_library_compiler.mjs +51 -0
  24. package/packages/core/scripts/run_native_tests.mjs +22 -0
  25. package/packages/core/scripts/scriptc-toolchain.json +101 -0
  26. package/packages/core/scripts/scriptc_toolchain.mjs +121 -0
  27. package/packages/core/scripts/stage_external_core.mjs +68 -1
  28. package/packages/core/scripts/surface_manifest_diff.mjs +19 -12
  29. package/packages/core/sdk/bytes.d.ts +2 -0
  30. package/packages/core/sdk/bytes.ts +86 -0
  31. package/packages/core/sdk/core.d.ts +332 -5
  32. package/packages/core/sdk/core.ts +371 -85
  33. package/packages/core/sdk/events.d.ts +252 -0
  34. package/packages/core/sdk/events.ts +240 -2
  35. package/packages/core/sdk/internal/image_identity.d.ts +7 -0
  36. package/packages/core/sdk/internal/image_identity.ts +71 -0
  37. package/packages/core/sdk/internal/markdown_policy.d.ts +5 -0
  38. package/packages/core/sdk/internal/markdown_policy.ts +878 -0
  39. package/packages/core/sdk/text.d.ts +27 -0
  40. package/packages/core/sdk/text.ts +206 -15
  41. package/packages/core/sdk/theme.d.ts +251 -0
  42. package/packages/core/sdk/theme.ts +267 -0
  43. package/packages/core/src/authorization.ts +137 -0
  44. package/packages/core/src/automation_commands.ts +201 -0
  45. package/packages/core/src/automation_interactions.ts +92 -0
  46. package/packages/core/src/bridge_descriptors.ts +104 -0
  47. package/packages/core/src/builtin_coordination.ts +172 -0
  48. package/packages/core/src/canvas_frame_coordination.ts +81 -0
  49. package/packages/core/src/canvas_presentation_lifecycle.ts +84 -0
  50. package/packages/core/src/canvas_scene_observation.ts +145 -0
  51. package/packages/core/src/chart_content.ts +161 -0
  52. package/packages/core/src/chart_plans.ts +432 -0
  53. package/packages/core/src/checker.ts +186 -19
  54. package/packages/core/src/cli.ts +2 -11
  55. package/packages/core/src/code_content.ts +580 -0
  56. package/packages/core/src/command_coordination.ts +140 -0
  57. package/packages/core/src/component_composition.ts +94 -0
  58. package/packages/core/src/component_construction.ts +149 -0
  59. package/packages/core/src/context_menus.ts +76 -0
  60. package/packages/core/src/contract.ts +25 -7
  61. package/packages/core/src/control_appearance.ts +232 -0
  62. package/packages/core/src/control_commands.ts +115 -0
  63. package/packages/core/src/control_content.ts +105 -0
  64. package/packages/core/src/control_geometry.ts +107 -0
  65. package/packages/core/src/control_payloads.ts +119 -0
  66. package/packages/core/src/control_primitives.ts +134 -0
  67. package/packages/core/src/devhost.ts +82 -7
  68. package/packages/core/src/devhost_journal.mjs +30 -7
  69. package/packages/core/src/diagnostics.ts +7 -0
  70. package/packages/core/src/effect_plans.ts +71 -0
  71. package/packages/core/src/extent_program.ts +53 -0
  72. package/packages/core/src/flow_measurement.ts +121 -0
  73. package/packages/core/src/font_registry.ts +37 -0
  74. package/packages/core/src/frontend.ts +12 -10
  75. package/packages/core/src/glyph_atlas.ts +86 -0
  76. package/packages/core/src/gpu_packet_preparation.ts +130 -0
  77. package/packages/core/src/gpu_planning.ts +123 -0
  78. package/packages/core/src/gpu_presentation.ts +55 -0
  79. package/packages/core/src/host_flow.ts +89 -0
  80. package/packages/core/src/host_input_regions.ts +165 -0
  81. package/packages/core/src/image_registry.ts +66 -0
  82. package/packages/core/src/indicator_plans.ts +132 -0
  83. package/packages/core/src/infer.ts +150 -8
  84. package/packages/core/src/input_composition.ts +116 -0
  85. package/packages/core/src/intrinsic_measure.ts +100 -0
  86. package/packages/core/src/layout_coordination.ts +207 -0
  87. package/packages/core/src/leaf_plans.ts +134 -0
  88. package/packages/core/src/markup_canonical.ts +140 -0
  89. package/packages/core/src/markup_construction.ts +189 -0
  90. package/packages/core/src/markup_development.ts +160 -0
  91. package/packages/core/src/markup_display.ts +43 -0
  92. package/packages/core/src/markup_document.ts +226 -0
  93. package/packages/core/src/markup_elements.ts +225 -0
  94. package/packages/core/src/markup_expressions.ts +79 -0
  95. package/packages/core/src/markup_imports.ts +146 -0
  96. package/packages/core/src/markup_parser.ts +228 -0
  97. package/packages/core/src/markup_runtime.ts +93 -0
  98. package/packages/core/src/markup_scalars.ts +143 -0
  99. package/packages/core/src/markup_shapes.ts +243 -0
  100. package/packages/core/src/markup_specialized.ts +242 -0
  101. package/packages/core/src/markup_text.ts +86 -0
  102. package/packages/core/src/markup_values.ts +336 -0
  103. package/packages/core/src/measurement_coordination.ts +185 -0
  104. package/packages/core/src/modules.ts +1 -1
  105. package/packages/core/src/motion_sampling.ts +100 -0
  106. package/packages/core/src/paragraph_layout.ts +192 -0
  107. package/packages/core/src/portable_sources.json +98 -0
  108. package/packages/core/src/portable_sources.ts +17 -0
  109. package/packages/core/src/registered_font.ts +92 -0
  110. package/packages/core/src/render_animation_lifecycle.ts +128 -0
  111. package/packages/core/src/render_coordination.ts +170 -0
  112. package/packages/core/src/render_resources.ts +234 -0
  113. package/packages/core/src/runtime_policy.ts +3954 -0
  114. package/packages/core/src/scalar_text.ts +82 -0
  115. package/packages/core/src/scroll_mutation.ts +86 -0
  116. package/packages/core/src/scroll_tree.ts +86 -0
  117. package/packages/core/src/service_contract.ts +55 -3
  118. package/packages/core/src/session_codecs.ts +309 -0
  119. package/packages/core/src/shell_admission.ts +126 -0
  120. package/packages/core/src/shell_coordination.ts +93 -0
  121. package/packages/core/src/sqlite_cli.ts +14 -46
  122. package/packages/core/src/sqlite_codegen.ts +84 -0
  123. package/packages/core/src/staged_rebuild.ts +60 -0
  124. package/packages/core/src/status_validation.ts +232 -0
  125. package/packages/core/src/stream_policy.ts +63 -0
  126. package/packages/core/src/surface_recipes.ts +136 -0
  127. package/packages/core/src/system_admission.ts +60 -0
  128. package/packages/core/src/terminal_paint.ts +436 -0
  129. package/packages/core/src/text_document_width.ts +81 -0
  130. package/packages/core/src/text_measurement_cache.ts +69 -0
  131. package/packages/core/src/text_query_layout.ts +179 -0
  132. package/packages/core/src/text_run_layout.ts +410 -0
  133. package/packages/core/src/text_span_queries.ts +355 -0
  134. package/packages/core/src/typed_ast.ts +18 -1
  135. package/packages/core/src/types.ts +7 -5
  136. package/packages/core/src/vector_effects.ts +98 -0
  137. package/packages/core/src/vector_raster.ts +243 -0
  138. package/packages/core/src/view_bridge.ts +161 -0
  139. package/packages/core/src/view_check.ts +145 -0
  140. package/packages/core/src/view_components.ts +2415 -0
  141. package/packages/core/src/view_coordination.ts +198 -0
  142. package/packages/core/src/view_evaluator.ts +823 -0
  143. package/packages/core/src/view_frontend.ts +1102 -0
  144. package/packages/core/src/view_program.ts +221 -0
  145. package/packages/core/src/view_reload.ts +166 -0
  146. package/packages/core/src/view_reload_watch.ts +98 -0
  147. package/packages/core/src/view_sources.ts +34 -0
  148. package/packages/core/src/virtual_windows.ts +88 -0
  149. package/packages/core/src/web_panes.ts +67 -0
  150. package/packages/core/src/widget_audits.ts +202 -0
  151. package/packages/core/src/widget_changes.ts +258 -0
  152. package/packages/core/src/widget_continuity.ts +37 -0
  153. package/packages/core/src/widget_metrics.ts +139 -0
  154. package/packages/core/src/widget_motion.ts +171 -0
  155. package/packages/core/src/widget_paint.ts +272 -0
  156. package/packages/core/src/widget_paint_walk.ts +203 -0
  157. package/packages/core/src/widget_presentation.ts +141 -0
  158. package/packages/core/src/widget_reconcile.ts +63 -0
  159. package/packages/core/src/widget_retention.ts +62 -0
  160. package/packages/core/src/widget_routing.ts +262 -0
  161. package/packages/core/src/window_lifecycle.ts +92 -0
  162. package/packages/core/testing/index.ts +430 -0
  163. package/schemas/app.schema.json +13 -0
  164. package/skill-data/core/SKILL.md +2 -2
  165. package/skill-data/native-ui/SKILL.md +77 -18
  166. package/skill-data/ts-core/SKILL.md +117 -11
  167. package/skill-data/ts-services/SKILL.md +1 -1
  168. package/skill-data/ts-services/references/service-surface.md +288 -144
  169. package/src/app_runner/root.zig +39 -12
  170. package/src/app_runner/ts_compiled_view.zig +1702 -0
  171. package/src/app_runner/ts_core_main.zig +185 -14
  172. package/src/app_runner/ts_core_mobile.zig +25 -13
  173. package/src/automation/server.zig +57 -6
  174. package/src/automation/snapshot.zig +9 -637
  175. package/src/automation/snapshot_reference.zig +656 -0
  176. package/src/automation/snapshot_serialization.zig +566 -0
  177. package/src/embed/host.zig +2 -0
  178. package/src/embed/tests.zig +2 -0
  179. package/src/embed/ui_host.zig +2 -0
  180. package/src/platform/keyboard_input.zig +32 -0
  181. package/src/platform/linux/gtk_host.c +36 -24
  182. package/src/platform/linux/gtk_pixels.h +118 -0
  183. package/src/platform/linux/gtk_pixels_test.c +241 -0
  184. package/src/platform/linux/root.zig +4 -0
  185. package/src/platform/macos/appkit_host.h +7 -2
  186. package/src/platform/macos/appkit_host.m +219 -39
  187. package/src/platform/macos/cef_host.mm +15 -1
  188. package/src/platform/macos/global_shortcuts.zig +138 -0
  189. package/src/platform/macos/key_codes.zig +102 -0
  190. package/src/platform/macos/keyboard_input.zig +81 -0
  191. package/src/platform/macos/pointer_motion_test.m +96 -0
  192. package/src/platform/macos/root.zig +25 -3
  193. package/src/platform/macos/scroll_driver_test.m +105 -0
  194. package/src/platform/macos/spectrum_bins.h +34 -0
  195. package/src/platform/macos/spectrum_bins_test.c +56 -0
  196. package/src/platform/macos/text_keyboard_test.m +80 -0
  197. package/src/platform/macos/webview_inspection_test.m +106 -0
  198. package/src/platform/macos/widget_focus_test.m +66 -0
  199. package/src/platform/null_platform.zig +130 -2
  200. package/src/platform/null_platform_tests.zig +13 -0
  201. package/src/platform/root.zig +15 -0
  202. package/src/platform/types.zig +115 -0
  203. package/src/platform/windows/global_shortcuts.zig +157 -0
  204. package/src/platform/windows/key_codes.zig +63 -0
  205. package/src/platform/windows/keyboard_input.zig +136 -0
  206. package/src/platform/windows/root.zig +17 -0
  207. package/src/platform/windows/webview2_host.cpp +7 -2
  208. package/src/primitives/app_manifest/comptime_scene.zig +1 -1
  209. package/src/primitives/app_manifest/root.zig +2 -0
  210. package/src/primitives/app_manifest/tests.zig +4 -1
  211. package/src/primitives/app_manifest/validation.zig +2 -0
  212. package/src/primitives/canvas/a11y_audit.zig +3 -1
  213. package/src/primitives/canvas/canvas_frame_coordination_policy.zig +134 -0
  214. package/src/primitives/canvas/chart_content_policy.zig +75 -0
  215. package/src/primitives/canvas/chart_plan_policy.zig +196 -0
  216. package/src/primitives/canvas/code_content_policy.zig +162 -0
  217. package/src/primitives/canvas/commands.zig +29 -1
  218. package/src/primitives/canvas/component_composition.zig +125 -0
  219. package/src/primitives/canvas/component_composition_policy.zig +44 -0
  220. package/src/primitives/canvas/component_construction_policy.zig +187 -0
  221. package/src/primitives/canvas/container_layout_policy.zig +87 -0
  222. package/src/primitives/canvas/control_appearance_policy.zig +142 -0
  223. package/src/primitives/canvas/control_command_policy.zig +75 -0
  224. package/src/primitives/canvas/control_content_policy.zig +90 -0
  225. package/src/primitives/canvas/control_geometry_policy.zig +165 -0
  226. package/src/primitives/canvas/control_payload_policy.zig +48 -0
  227. package/src/primitives/canvas/control_primitive_policy.zig +129 -0
  228. package/src/primitives/canvas/decision_reuse.zig +133 -0
  229. package/src/primitives/canvas/effect_plan_policy.zig +96 -0
  230. package/src/primitives/canvas/events.zig +1027 -1
  231. package/src/primitives/canvas/flow_measurement_policy.zig +237 -0
  232. package/src/primitives/canvas/font_ttf.zig +27 -3
  233. package/src/primitives/canvas/fonts/Geist-Bold.ttf +0 -0
  234. package/src/primitives/canvas/fonts/Geist-Medium.ttf +0 -0
  235. package/src/primitives/canvas/fonts/OFL.txt +93 -0
  236. package/src/primitives/canvas/fonts/README.md +15 -0
  237. package/src/primitives/canvas/frame.zig +92 -23
  238. package/src/primitives/canvas/glyph_atlas_policy.zig +40 -0
  239. package/src/primitives/canvas/gpu.zig +10 -0
  240. package/src/primitives/canvas/gpu_packet_preparation_policy.zig +325 -0
  241. package/src/primitives/canvas/gpu_planning_policy.zig +364 -0
  242. package/src/primitives/canvas/gpu_presentation_policy.zig +74 -0
  243. package/src/primitives/canvas/grid_layout_policy.zig +51 -0
  244. package/src/primitives/canvas/indicator_plan_policy.zig +128 -0
  245. package/src/primitives/canvas/intrinsic_layout_policy.zig +87 -0
  246. package/src/primitives/canvas/intrinsic_measure_policy.zig +131 -0
  247. package/src/primitives/canvas/layout_audit.zig +5 -2
  248. package/src/primitives/canvas/layout_coordination_policy.zig +262 -0
  249. package/src/primitives/canvas/leaf_plan_policy.zig +117 -0
  250. package/src/primitives/canvas/markdown.zig +13 -3
  251. package/src/primitives/canvas/markdown_content_policy.zig +227 -0
  252. package/src/primitives/canvas/markdown_image_binding.zig +81 -0
  253. package/src/primitives/canvas/markdown_tests.zig +3 -3
  254. package/src/primitives/canvas/measurement_coordination_policy.zig +288 -0
  255. package/src/primitives/canvas/numeric_capabilities.zig +14 -0
  256. package/src/primitives/canvas/reference.zig +15 -11
  257. package/src/primitives/canvas/reference_tests.zig +58 -0
  258. package/src/primitives/canvas/registered_font_policy.zig +152 -0
  259. package/src/primitives/canvas/render.zig +48 -0
  260. package/src/primitives/canvas/render_cache_policy.zig +184 -0
  261. package/src/primitives/canvas/render_coordination_policy.zig +163 -0
  262. package/src/primitives/canvas/render_damage_policy.zig +160 -0
  263. package/src/primitives/canvas/render_effects.zig +14 -0
  264. package/src/primitives/canvas/render_generic_resources.zig +14 -0
  265. package/src/primitives/canvas/render_images.zig +14 -0
  266. package/src/primitives/canvas/render_layers.zig +14 -0
  267. package/src/primitives/canvas/render_override_policy.zig +119 -0
  268. package/src/primitives/canvas/render_paths.zig +14 -0
  269. package/src/primitives/canvas/render_plan_policy.zig +199 -0
  270. package/src/primitives/canvas/render_resource_policy.zig +476 -0
  271. package/src/primitives/canvas/root.zig +107 -4
  272. package/src/primitives/canvas/semantic_tree_policy.zig +158 -0
  273. package/src/primitives/canvas/serialization.zig +8 -1
  274. package/src/primitives/canvas/surface_layout_policy.zig +36 -0
  275. package/src/primitives/canvas/surface_recipe_policy.zig +231 -0
  276. package/src/primitives/canvas/terminal_grid.zig +10 -696
  277. package/src/primitives/canvas/terminal_grid_reference.zig +709 -0
  278. package/src/primitives/canvas/terminal_paint_policy.zig +191 -0
  279. package/src/primitives/canvas/text.zig +1 -0
  280. package/src/primitives/canvas/text_atlas.zig +113 -0
  281. package/src/primitives/canvas/text_cache_policy.zig +94 -0
  282. package/src/primitives/canvas/text_document_policy.zig +120 -0
  283. package/src/primitives/canvas/text_layout.zig +51 -2
  284. package/src/primitives/canvas/text_layout_cache.zig +29 -0
  285. package/src/primitives/canvas/text_layout_hash.zig +1 -0
  286. package/src/primitives/canvas/text_layout_types.zig +3 -0
  287. package/src/primitives/canvas/text_measure_cache.zig +46 -0
  288. package/src/primitives/canvas/text_measurement_policy.zig +124 -0
  289. package/src/primitives/canvas/text_metrics.zig +53 -37
  290. package/src/primitives/canvas/text_metrics_tests.zig +52 -7
  291. package/src/primitives/canvas/text_query_policy.zig +295 -0
  292. package/src/primitives/canvas/text_run_policy.zig +252 -0
  293. package/src/primitives/canvas/text_scalar_policy.zig +234 -0
  294. package/src/primitives/canvas/text_span_query_policy.zig +232 -0
  295. package/src/primitives/canvas/text_spans.zig +280 -8
  296. package/src/primitives/canvas/text_tests.zig +10 -0
  297. package/src/primitives/canvas/tokens.zig +47 -0
  298. package/src/primitives/canvas/ui.zig +251 -83
  299. package/src/primitives/canvas/ui_markup.zig +207 -46
  300. package/src/primitives/canvas/ui_markup_canonical.zig +237 -0
  301. package/src/primitives/canvas/ui_markup_compiled.zig +1724 -80
  302. package/src/primitives/canvas/ui_markup_compiled_tests.zig +225 -0
  303. package/src/primitives/canvas/ui_markup_compiled_values.zig +76 -0
  304. package/src/primitives/canvas/ui_markup_construction.zig +146 -0
  305. package/src/primitives/canvas/ui_markup_contract.zig +146 -14
  306. package/src/primitives/canvas/ui_markup_contract_tests.zig +77 -0
  307. package/src/primitives/canvas/ui_markup_display.zig +82 -0
  308. package/src/primitives/canvas/ui_markup_document.zig +196 -0
  309. package/src/primitives/canvas/ui_markup_elements.zig +163 -0
  310. package/src/primitives/canvas/ui_markup_expr.zig +192 -2
  311. package/src/primitives/canvas/ui_markup_imports.zig +262 -0
  312. package/src/primitives/canvas/ui_markup_parser.zig +117 -0
  313. package/src/primitives/canvas/ui_markup_reflect.zig +56 -0
  314. package/src/primitives/canvas/ui_markup_runtime.zig +76 -0
  315. package/src/primitives/canvas/ui_markup_scalars.zig +115 -0
  316. package/src/primitives/canvas/ui_markup_shapes.zig +224 -0
  317. package/src/primitives/canvas/ui_markup_specialized.zig +106 -0
  318. package/src/primitives/canvas/ui_markup_tests.zig +116 -0
  319. package/src/primitives/canvas/ui_markup_text.zig +128 -0
  320. package/src/primitives/canvas/ui_markup_values.zig +368 -0
  321. package/src/primitives/canvas/ui_markup_view.zig +3026 -57
  322. package/src/primitives/canvas/ui_markup_view_tests.zig +142 -10
  323. package/src/primitives/canvas/ui_schema.zig +13 -1
  324. package/src/primitives/canvas/ui_schema_tests.zig +11 -7
  325. package/src/primitives/canvas/ui_tests.zig +1 -0
  326. package/src/primitives/canvas/vector.zig +36 -2
  327. package/src/primitives/canvas/vector_effect_policy.zig +236 -0
  328. package/src/primitives/canvas/vector_raster_policy.zig +190 -0
  329. package/src/primitives/canvas/virtual_extent_policy.zig +350 -0
  330. package/src/primitives/canvas/virtual_extents.zig +113 -3
  331. package/src/primitives/canvas/virtual_flow_policy.zig +108 -0
  332. package/src/primitives/canvas/widget_audit_policy.zig +293 -0
  333. package/src/primitives/canvas/widget_change_policy.zig +298 -0
  334. package/src/primitives/canvas/widget_invalidation.zig +113 -2
  335. package/src/primitives/canvas/widget_layout.zig +673 -128
  336. package/src/primitives/canvas/widget_layout_tests.zig +4 -0
  337. package/src/primitives/canvas/widget_metric_policy.zig +129 -0
  338. package/src/primitives/canvas/widget_metrics.zig +45 -6
  339. package/src/primitives/canvas/widget_paint_policy.zig +166 -0
  340. package/src/primitives/canvas/widget_paint_walk_policy.zig +252 -0
  341. package/src/primitives/canvas/widget_presentation_policy.zig +168 -0
  342. package/src/primitives/canvas/widget_render.zig +744 -74
  343. package/src/primitives/canvas/widget_render_controls.zig +1217 -133
  344. package/src/primitives/canvas/widget_render_scroll.zig +11 -3
  345. package/src/primitives/canvas/widget_render_style.zig +94 -0
  346. package/src/primitives/canvas/widget_render_surfaces.zig +61 -50
  347. package/src/primitives/canvas/widget_routing.zig +75 -0
  348. package/src/primitives/canvas/widget_routing_policy.zig +135 -0
  349. package/src/primitives/canvas/widget_runtime.zig +21 -1
  350. package/src/primitives/canvas/widget_semantics.zig +25 -0
  351. package/src/primitives/canvas/widget_text_input.zig +86 -23
  352. package/src/primitives/canvas/widget_text_select.zig +1 -0
  353. package/src/primitives/canvas/widget_tree.zig +8 -1
  354. package/src/primitives/canvas/widgets.zig +42 -12
  355. package/src/primitives/canvas/wrapped_layout_policy.zig +104 -0
  356. package/src/root.zig +26 -0
  357. package/src/runtime/accessibility_policy.zig +122 -0
  358. package/src/runtime/api.zig +29 -0
  359. package/src/runtime/app_dispatch_policy.zig +68 -0
  360. package/src/runtime/authorization_policy.zig +150 -0
  361. package/src/runtime/automation_command_policy.zig +241 -0
  362. package/src/runtime/automation_interaction_policy.zig +107 -0
  363. package/src/runtime/automation_snapshot.zig +52 -22
  364. package/src/runtime/automation_snapshot_reference.zig +290 -0
  365. package/src/runtime/automation_widget_dispatch.zig +267 -355
  366. package/src/runtime/automation_widget_dispatch_reference.zig +900 -0
  367. package/src/runtime/blob_policy.zig +58 -0
  368. package/src/runtime/blob_reference.zig +64 -0
  369. package/src/runtime/bridge_descriptor_policy.zig +206 -0
  370. package/src/runtime/bridge_payload.zig +2 -0
  371. package/src/runtime/builtin_bridge.zig +180 -44
  372. package/src/runtime/builtin_coordination_policy.zig +121 -0
  373. package/src/runtime/callback_policy_test_reference.zig +97 -0
  374. package/src/runtime/canvas_drag_policy.zig +94 -0
  375. package/src/runtime/canvas_fonts.zig +38 -10
  376. package/src/runtime/canvas_frame.zig +590 -134
  377. package/src/runtime/canvas_frame_helpers.zig +1 -0
  378. package/src/runtime/canvas_hover_policy.zig +131 -0
  379. package/src/runtime/canvas_images.zig +59 -30
  380. package/src/runtime/canvas_keyboard_focus_policy.zig +154 -0
  381. package/src/runtime/canvas_limits.zig +1 -1
  382. package/src/runtime/canvas_presentation_lifecycle_policy.zig +86 -0
  383. package/src/runtime/canvas_scene_observation_policy.zig +181 -0
  384. package/src/runtime/canvas_widget_context_menu.zig +183 -4
  385. package/src/runtime/canvas_widget_context_menu_test_cases.zig +1147 -0
  386. package/src/runtime/canvas_widget_context_menu_tests.zig +23 -1098
  387. package/src/runtime/canvas_widget_display.zig +110 -9
  388. package/src/runtime/canvas_widget_events.zig +718 -328
  389. package/src/runtime/canvas_widget_runtime.zig +720 -150
  390. package/src/runtime/canvas_widget_scroll_drivers.zig +39 -1
  391. package/src/runtime/canvas_widget_scroll_tests.zig +31 -0
  392. package/src/runtime/canvas_widget_state.zig +93 -56
  393. package/src/runtime/channel_sources.zig +80 -0
  394. package/src/runtime/command_policy.zig +161 -0
  395. package/src/runtime/component_policy.zig +83 -0
  396. package/src/runtime/context_menu_policy.zig +112 -0
  397. package/src/runtime/core.zig +170 -6
  398. package/src/runtime/desktop_files.zig +189 -0
  399. package/src/runtime/effects.zig +638 -33
  400. package/src/runtime/effects_host_tests.zig +25 -0
  401. package/src/runtime/effects_tests.zig +28 -0
  402. package/src/runtime/file_policy_test_reference.zig +113 -0
  403. package/src/runtime/flow.zig +419 -341
  404. package/src/runtime/flow_reference.zig +1427 -0
  405. package/src/runtime/font_registry_policy.zig +72 -0
  406. package/src/runtime/frame_profile.zig +24 -33
  407. package/src/runtime/global_shortcuts.zig +75 -0
  408. package/src/runtime/gpu_surface_events.zig +97 -35
  409. package/src/runtime/host_event_pump.zig +303 -0
  410. package/src/runtime/host_event_pump_reference.zig +268 -0
  411. package/src/runtime/host_flow_policy.zig +155 -0
  412. package/src/runtime/host_input_regions_policy.zig +156 -0
  413. package/src/runtime/image_registry_policy.zig +94 -0
  414. package/src/runtime/input_composition_policy.zig +138 -0
  415. package/src/runtime/journal_policy.zig +66 -0
  416. package/src/runtime/journal_reference.zig +53 -0
  417. package/src/runtime/keyboard_input.zig +86 -0
  418. package/src/runtime/lifecycle_policy_test_reference.zig +97 -0
  419. package/src/runtime/markup_development_policy.zig +91 -0
  420. package/src/runtime/media_surface.zig +40 -1
  421. package/src/runtime/motion_sampling_policy.zig +159 -0
  422. package/src/runtime/recording_policy.zig +42 -0
  423. package/src/runtime/render_animation_policy.zig +206 -0
  424. package/src/runtime/replay_policy.zig +66 -0
  425. package/src/runtime/replay_policy_reference.zig +326 -0
  426. package/src/runtime/replay_progress_policy.zig +64 -0
  427. package/src/runtime/replay_progress_reference.zig +34 -0
  428. package/src/runtime/request_policy_test_reference.zig +188 -0
  429. package/src/runtime/root.zig +16 -0
  430. package/src/runtime/scroll_mutation_policy.zig +130 -0
  431. package/src/runtime/scroll_mutation_runtime.zig +158 -0
  432. package/src/runtime/scroll_tree_policy.zig +99 -0
  433. package/src/runtime/session_blobs.zig +157 -58
  434. package/src/runtime/session_blobs_reference.zig +692 -0
  435. package/src/runtime/session_codec.zig +361 -0
  436. package/src/runtime/session_codec_reference.zig +1027 -0
  437. package/src/runtime/session_journal.zig +246 -983
  438. package/src/runtime/session_record.zig +231 -45
  439. package/src/runtime/session_replay.zig +157 -415
  440. package/src/runtime/session_tests.zig +552 -0
  441. package/src/runtime/shell_admission_policy.zig +225 -0
  442. package/src/runtime/shell_coordination_policy.zig +138 -0
  443. package/src/runtime/shell_layout.zig +126 -1
  444. package/src/runtime/snapshot_policy.zig +60 -0
  445. package/src/runtime/snapshot_reference.zig +64 -0
  446. package/src/runtime/staged_rebuild_policy.zig +83 -0
  447. package/src/runtime/state.zig +16 -0
  448. package/src/runtime/status_validation.zig +190 -0
  449. package/src/runtime/system_admission_policy.zig +140 -0
  450. package/src/runtime/system_services.zig +65 -36
  451. package/src/runtime/tests.zig +2 -0
  452. package/src/runtime/ts_canvas_hooks.zig +261 -0
  453. package/src/runtime/ts_canvas_hooks_tests.zig +279 -0
  454. package/src/runtime/ts_canvas_icons.zig +234 -0
  455. package/src/runtime/ts_core_host.zig +2451 -411
  456. package/src/runtime/ts_core_host_tests.zig +1982 -0
  457. package/src/runtime/ts_test_host.zig +699 -0
  458. package/src/runtime/ts_ui_app.zig +1111 -45
  459. package/src/runtime/ui_app.zig +1381 -650
  460. package/src/runtime/ui_app_provenance.zig +147 -5
  461. package/src/runtime/ui_app_provenance_reference.zig +370 -0
  462. package/src/runtime/ui_app_tests.zig +667 -188
  463. package/src/runtime/ui_app_window_tests.zig +222 -0
  464. package/src/runtime/view.zig +25 -0
  465. package/src/runtime/view_canvas.zig +30 -6
  466. package/src/runtime/view_coordination_policy.zig +147 -0
  467. package/src/runtime/view_reload.zig +226 -0
  468. package/src/runtime/view_widget_control.zig +51 -14
  469. package/src/runtime/view_widget_scroll.zig +46 -26
  470. package/src/runtime/view_widget_text.zig +153 -68
  471. package/src/runtime/view_widget_tree.zig +503 -69
  472. package/src/runtime/virtual_window_policy.zig +128 -0
  473. package/src/runtime/web_pane_policy.zig +111 -0
  474. package/src/runtime/web_pane_test_cases.zig +204 -0
  475. package/src/runtime/webview_view_bridge_tests.zig +2 -0
  476. package/src/runtime/widget_continuity_policy.zig +73 -0
  477. package/src/runtime/widget_motion_policy.zig +238 -0
  478. package/src/runtime/widget_reconcile_policy.zig +81 -0
  479. package/src/runtime/widget_retention_policy.zig +75 -0
  480. package/src/runtime/window_command_bridge_tests.zig +63 -0
  481. package/src/runtime/window_lifecycle_policy.zig +127 -0
  482. package/src/runtime/window_storage.zig +74 -8
  483. package/src/runtime/window_view_runtime.zig +148 -33
  484. package/src/runtime/window_views.zig +179 -11
  485. package/src/security/root.zig +1 -0
  486. package/src/tooling/components/identity_tests.zig +62 -36
  487. package/src/tooling/components/stepper.native +24 -0
  488. package/src/tooling/components/stepper.zig +0 -19
  489. package/src/tooling/components/timeline-item.native +22 -0
  490. package/src/tooling/components/timeline.native +5 -3
  491. package/src/tooling/components/timeline_item.zig +0 -32
  492. package/src/tooling/eject_components.zig +76 -8
  493. package/src/tooling/ios.zig +66 -12
  494. package/src/tooling/manifest.zig +35 -0
  495. package/src/tooling/raw_manifest.zig +6 -0
  496. package/src/tooling/templates.zig +23 -3
  497. package/src/tooling/ts_core.zig +97 -4
  498. package/src/tooling/verbs.zig +87 -1
  499. package/tools/corewire/build_profile.mjs +54 -0
  500. package/tools/corewire/contract_intake.ts +303 -0
  501. package/tools/corewire/contract_intake.zig +82 -0
  502. package/tools/corewire/core.test.ts +188 -0
  503. package/tools/corewire/core_abi.zig +55 -0
  504. package/tools/corewire/core_cases.ts +233 -0
  505. package/tools/corewire/core_contract.ts +407 -0
  506. package/tools/corewire/core_emission.ts +136 -0
  507. package/tools/corewire/core_emission.zig +69 -0
  508. package/tools/corewire/core_goldens.json +3110 -0
  509. package/tools/corewire/core_policy.ts +14 -0
  510. package/tools/corewire/core_policy.zig +48 -0
  511. package/tools/corewire/core_projection.ts +361 -0
  512. package/tools/corewire/core_vocabulary.ts +6 -0
  513. package/tools/corewire/emit.zig +19 -1590
  514. package/tools/corewire/emit_facade.ts +714 -0
  515. package/tools/corewire/emit_facade.zig +10 -3752
  516. package/tools/corewire/emit_mirror.ts +300 -0
  517. package/tools/corewire/emit_profile.ts +50 -0
  518. package/tools/corewire/emit_service.ts +331 -0
  519. package/tools/corewire/emit_service.zig +23 -842
  520. package/tools/corewire/entry.test.ts +95 -0
  521. package/tools/corewire/entry_coordination.ts +127 -0
  522. package/tools/corewire/entry_coordination.zig +190 -0
  523. package/tools/corewire/facade_templates.ts +85 -0
  524. package/tools/corewire/filesystem.test.ts +166 -0
  525. package/tools/corewire/filesystem_coordination.ts +213 -0
  526. package/tools/corewire/filesystem_coordination.zig +245 -0
  527. package/tools/corewire/intake.test.ts +39 -0
  528. package/tools/corewire/intake_cases.ts +55 -0
  529. package/tools/corewire/intake_goldens.json +12237 -0
  530. package/tools/corewire/invocation.test.ts +28 -0
  531. package/tools/corewire/invocation.ts +175 -0
  532. package/tools/corewire/invocation.zig +136 -0
  533. package/tools/corewire/invocation_cases.ts +60 -0
  534. package/tools/corewire/invocation_fixture.ts +33 -0
  535. package/tools/corewire/invocation_goldens.json +1143 -0
  536. package/tools/corewire/invocation_tests.zig +111 -0
  537. package/tools/corewire/invocation_usage.ts +2 -0
  538. package/tools/corewire/lossless_projection.ts +96 -0
  539. package/tools/corewire/main.zig +7 -680
  540. package/tools/corewire/main_reference.zig +415 -0
  541. package/tools/corewire/mirror_templates.ts +59 -0
  542. package/tools/corewire/profile.test.ts +248 -0
  543. package/tools/corewire/profile.zig +56 -0
  544. package/tools/corewire/profile_emission.ts +204 -0
  545. package/tools/corewire/profile_library.json +29 -0
  546. package/tools/corewire/profile_tests.zig +136 -0
  547. package/tools/corewire/service.test.ts +75 -0
  548. package/tools/corewire/service_cases.ts +61 -0
  549. package/tools/corewire/service_contract.zig +40 -146
  550. package/tools/corewire/service_goldens.json +650 -0
  551. package/tools/corewire/service_projection.zig +52 -0
  552. package/tools/corewire/service_templates.ts +14 -0
  553. package/tools/corewire/shim_rt.zig +9 -0
  554. package/tools/corewire/sidecar.zig +269 -1438
  555. package/tools/corewire/emit_profile.zig +0 -521
@@ -1,1590 +1,13 @@
1
- //! core_shim.zig emission: turn a validated contract sidecar into the
2
- //! Zig mirror module the app wiring imports in place of transpiler
3
- //! output.
4
- //!
5
- //! The mirror is the load-bearing move of the whole design: every seam
6
- //! that reflects over an app core at comptime — the markup engines, the
7
- //! adapter's channel stamping, the bridge's tag table, the
8
- //! model-contract emit — keeps its `@typeInfo` worldview because the
9
- //! generated module declares REAL Zig types with the sidecar's field
10
- //! names, declaration order, and type spellings. Fidelity rules, each
11
- //! matching the transpiler's emission so the reflecting seams cannot
12
- //! tell the two lanes apart:
13
- //!
14
- //! - bytes spell `[]const u8`; numbers spell `f64` or `i64` per the
15
- //! sidecar's per-slot class — `u64` where the slot's integer_slots
16
- //! attestation refines the i64 spelling to the unsigned class, so the
17
- //! decoder reads the slot's 8 wire bytes unsigned; every non-attested
18
- //! numeric slot stays f64. Enums are `enum(u8)` with explicit
19
- //! declaration-order values; node references spell `*const T`.
20
- //! - The message union is `union(enum)` in sidecar arm order — the arm
21
- //! index IS the wire tag.
22
- //! - Synthesized type-table names (the schema's `<Container>_<member>`
23
- //! pattern for tabled anonymous records) are emitted INLINE at their
24
- //! one reference site, exactly where the transpiler emits an inline
25
- //! struct — a named top-level declaration would change nothing for
26
- //! layout but everything for `@typeName`-carrying artifacts.
27
- //! - Exported helpers become Model methods (`fn (self: *const Model)`,
28
- //! plus the build-arena form) forwarding to the core's helper_call
29
- //! entry; `view_unbound` tuples mirror the unbound lists.
30
- //!
31
- //! Executable surface: dispatch stubs route each arm through the ABI
32
- //! entry its payload descriptor selects, the snapshot decoder
33
- //! materializes the committed model into a mirror value after every
34
- //! dispatch, and the boot path runs the identity fence before anything
35
- //! else. All extern references live in core_abi.zig (staged beside the
36
- //! output), so ABI ratification cannot touch this emitter.
37
-
1
+ //! Compiled TypeScript projection over the native structural decoder.
38
2
  const std = @import("std");
39
3
  const sidecar_mod = @import("sidecar.zig");
40
-
41
4
  const Sidecar = sidecar_mod.Sidecar;
42
- const TypeRef = sidecar_mod.TypeRef;
43
- const Payload = sidecar_mod.Payload;
44
-
45
- /// Comptime string scans pay for every arm and every byte in its name.
46
- /// Keep the generated consistency fence independent of Zig's default
47
- /// 1000-backwards-branch quota: the sidecar admits 256 arms, and long legal
48
- /// TypeScript identifiers must remain legal here too. The fixed floor covers
49
- /// the surrounding reflection; the linear terms leave generous headroom over
50
- /// `std.mem.eql`'s scalar comptime path.
51
- fn msgTagConsistencyQuota(arms: []const sidecar_mod.MsgArm) u32 {
52
- var name_bytes: u64 = 0;
53
- for (arms) |arm| name_bytes += arm.name.len;
54
- const quota: u64 = 100_000 + @as(u64, arms.len) * 1_024 + name_bytes * 256;
55
- return @intCast(@min(quota, std.math.maxInt(u32)));
56
- }
57
-
58
- /// The thirteen text-input event tags the markup engines recognize
59
- /// structurally; a union payload carrying exactly these dispatches
60
- /// through the ABI's text_input entry.
61
- const text_input_event_tags = [_][]const u8{
62
- "insert_text", "delete_backward", "delete_forward", "delete_word_backward",
63
- "delete_word_forward", "delete_to_start", "delete_to_line_start", "clear",
64
- "move_caret", "set_selection", "set_composition", "commit_composition",
65
- "cancel_composition",
66
- };
67
-
68
- /// Whether a union payload declares the text-input event shape the markup
69
- /// engines route through the dedicated ABI entry: exactly the thirteen tags,
70
- /// with the same structural payload vocabulary. This is public because the
71
- /// generated facade must make the identical decision when selecting the one
72
- /// sentinel-saturating decoder; one contract cannot have two recognizers.
73
- pub fn isTextInputUnion(sidecar: Sidecar, type_name: []const u8) bool {
74
- const entry = sidecar_mod.findUnion(sidecar.types, type_name) orelse return false;
75
- if (entry.arms.len != text_input_event_tags.len) return false;
76
- outer: for (text_input_event_tags) |tag_name| {
77
- for (entry.arms) |arm| {
78
- if (std.mem.eql(u8, arm.name, tag_name)) continue :outer;
79
- }
80
- return false;
81
- }
82
- for (entry.arms) |arm| {
83
- if (std.mem.eql(u8, arm.name, "insert_text")) {
84
- if (arm.payload != .bytes) return false;
85
- } else if (std.mem.eql(u8, arm.name, "move_caret")) {
86
- if (!isTextCaretMoveRecord(sidecar, arm.payload)) return false;
87
- } else if (std.mem.eql(u8, arm.name, "set_selection")) {
88
- if (!isTextSelectionRecord(sidecar, arm.payload)) return false;
89
- } else if (std.mem.eql(u8, arm.name, "set_composition")) {
90
- if (!isTextCompositionRecord(sidecar, arm.payload)) return false;
91
- } else if (arm.payload != .void) {
92
- return false;
93
- }
94
- }
95
- return true;
96
- }
97
-
98
- fn textRecordOf(sidecar: Sidecar, ref: TypeRef) ?*const sidecar_mod.Struct {
99
- return switch (ref) {
100
- .value => |name| sidecar_mod.findStruct(sidecar.types, name),
101
- else => null,
102
- };
103
- }
104
-
105
- fn isTextNumericRef(ref: TypeRef) bool {
106
- return ref == .f64 or ref == .i64;
107
- }
108
-
109
- fn isTextCaretMoveRecord(sidecar: Sidecar, ref: TypeRef) bool {
110
- const record = textRecordOf(sidecar, ref) orelse return false;
111
- if (record.fields.len != 2) return false;
112
- const caret_members = [_][]const u8{ "previous", "next", "previous_word", "next_word", "start", "end" };
113
- var direction_ok = false;
114
- var extend_ok = false;
115
- for (record.fields) |field| {
116
- if (std.mem.eql(u8, field.name, "extend")) {
117
- extend_ok = field.type == .bool;
118
- } else if (std.mem.eql(u8, field.name, "direction")) {
119
- const members = switch (field.type) {
120
- .enum_ref => |name| (sidecar_mod.findEnum(sidecar.types, name) orelse return false).members,
121
- else => return false,
122
- };
123
- if (members.len != caret_members.len) return false;
124
- outer: for (caret_members) |member| {
125
- for (members) |declared| {
126
- if (std.mem.eql(u8, declared, member)) continue :outer;
127
- }
128
- return false;
129
- }
130
- direction_ok = true;
131
- }
132
- }
133
- return direction_ok and extend_ok;
134
- }
135
-
136
- fn isTextSelectionRecord(sidecar: Sidecar, ref: TypeRef) bool {
137
- const record = textRecordOf(sidecar, ref) orelse return false;
138
- if (record.fields.len != 2) return false;
139
- var anchor_ok = false;
140
- var focus_ok = false;
141
- for (record.fields) |field| {
142
- if (std.mem.eql(u8, field.name, "anchor")) anchor_ok = isTextNumericRef(field.type);
143
- if (std.mem.eql(u8, field.name, "focus")) focus_ok = isTextNumericRef(field.type);
144
- }
145
- return anchor_ok and focus_ok;
146
- }
147
-
148
- fn isTextCompositionRecord(sidecar: Sidecar, ref: TypeRef) bool {
149
- const record = textRecordOf(sidecar, ref) orelse return false;
150
- if (record.fields.len != 2) return false;
151
- var text_ok = false;
152
- var cursor_ok = false;
153
- for (record.fields) |field| {
154
- if (std.mem.eql(u8, field.name, "text")) text_ok = field.type == .bytes;
155
- if (std.mem.eql(u8, field.name, "cursor")) {
156
- cursor_ok = switch (field.type) {
157
- .optional => |inner| isTextNumericRef(inner.*),
158
- else => false,
159
- };
160
- }
161
- }
162
- return text_ok and cursor_ok;
163
- }
164
-
165
- /// The scroll-state field vocabulary — the TWO-AXIS record, eight
166
- /// per-axis fields — in the TS spelling (the emitted-core mirror keeps
167
- /// the author's names) and the canvas spelling. The order is
168
- /// `canvas.ScrollState`'s declaration order, which is also the ABI
169
- /// entry's parameter order. The retired one-axis quartet
170
- /// (`{offset, velocity, viewportExtent, contentExtent}`) is NOT scroll
171
- /// state anymore: a record carrying it rides the generic record entry,
172
- /// and the markup engines refuse to bind `on-scroll` to it with a
173
- /// teaching that names these fields.
174
- const scroll_state_fields_ts = [_][]const u8{
175
- "offsetX", "offsetY",
176
- "velocityX", "velocityY",
177
- "viewportExtentX", "viewportExtentY",
178
- "contentExtentX", "contentExtentY",
179
- };
180
- const scroll_state_fields_canvas = [_][]const u8{
181
- "offset_x", "offset_y",
182
- "velocity_x", "velocity_y",
183
- "viewport_extent_x", "viewport_extent_y",
184
- "content_extent_x", "content_extent_y",
185
- };
186
-
187
5
  pub const Error = error{ Refused, OutOfMemory };
188
6
 
189
- /// Emit the complete core_shim.zig source. Deterministic: a pure
190
- /// function of the sidecar value (V13's generator-side counterpart,
191
- /// pinned by a test).
192
7
  pub fn emit(arena: std.mem.Allocator, sidecar: Sidecar, diags: *sidecar_mod.Diagnostics) Error![]const u8 {
193
- var emitter = Emitter{
194
- .arena = arena,
195
- .sidecar = sidecar,
196
- .diags = diags,
197
- .out = .empty,
198
- };
199
- try emitter.run();
200
- if (diags.hasErrors()) return error.Refused;
201
- return emitter.out.items;
8
+ return @import("core_emission.zig").emit(arena, sidecar, .mirror, diags);
202
9
  }
203
10
 
204
- const Emitter = struct {
205
- arena: std.mem.Allocator,
206
- sidecar: Sidecar,
207
- diags: *sidecar_mod.Diagnostics,
208
- out: std.ArrayListUnmanaged(u8),
209
- /// Table entries emitted inline at their single reference site
210
- /// (see inlinedNames).
211
- inlined: []const []const u8 = &.{},
212
-
213
- fn print(self: *Emitter, comptime fmt: []const u8, args: anytype) Error!void {
214
- const text = try std.fmt.allocPrint(self.arena, fmt, args);
215
- try self.out.appendSlice(self.arena, text);
216
- }
217
-
218
- fn raw(self: *Emitter, text: []const u8) Error!void {
219
- try self.out.appendSlice(self.arena, text);
220
- }
221
-
222
- fn run(self: *Emitter) Error!void {
223
- try self.validateEmissionNames();
224
- self.validateChannelShapes();
225
- try self.validateArmStorage();
226
- try self.validateIntegerAttestations();
227
- if (self.diags.hasErrors()) return;
228
- self.inlined = try self.inlinedNames();
229
- try self.header();
230
- try self.mirrorTypes();
231
- try self.modelStruct();
232
- try self.msgUnion();
233
- try self.wiringAliases();
234
- try self.tagTable();
235
- try self.entryPoints();
236
- try self.channels();
237
- try self.helperPlumbing();
238
- try self.snapshotPlumbing();
239
- }
240
-
241
- // -------------------------------------------- emission-name space
242
- //
243
- // The generated module declares more than the mirror types: the rt
244
- // block, the tag table, the entry points, and (per wired channel)
245
- // the fixed event-record vocabulary. A sidecar whose own names
246
- // collide with those declarations would generate a module that
247
- // cannot compile — refuse at tool time with the exact name instead.
248
-
249
- fn validateEmissionNames(self: *Emitter) Error!void {
250
- var reserved: std.ArrayListUnmanaged([]const u8) = .empty;
251
- try reserved.appendSlice(self.arena, &.{
252
- "std", "shim_rt", "core_abi", "abi",
253
- "rt", "msg_tags", "boot", "initialModel",
254
- "update", "commitModelRoot", "sidecar_build_id", "sidecar_model_fingerprint",
255
- "sidecar_abi_version", "sidecar_snapshot_format", "deterministic", "async_free",
256
- "snapshotModel", "modelSnapshot", "persistenceSnapshot", "restoreModel",
257
- "referenceAttestedExports",
258
- });
259
- // Parameter and local names the generated bodies bind: a
260
- // module-level type under any of them would be shadowed, which
261
- // the compiler refuses.
262
- try reserved.appendSlice(self.arena, &.{
263
- "T", "n", "model", "msg", "payload", "encoded",
264
- "cmd_ptr", "cmd_len", "subs_ptr", "subs_len", "snap_ptr", "snap_len",
265
- "out", "out_ptr", "out_len", "tag", "tag_name", "name",
266
- "frame", "key", "pinch", "value", "self", "index",
267
- "args", "args_tuple", "allocator", "fields", "field", "next",
268
- "helper_args",
269
- // The unbound-list tuples nest inside Model and Msg, where
270
- // they would shadow a same-named top-level type at every
271
- // field-type resolution inside those structs.
272
- "view_unbound",
273
- });
274
- for (self.sidecar.model_helpers) |helper| {
275
- for (helper.params, 0..) |_, param_index| {
276
- try reserved.append(self.arena, try std.fmt.allocPrint(self.arena, "p{d}", .{param_index}));
277
- }
278
- }
279
- // The host layer wires channels by PROBING these declaration
280
- // names (`@hasDecl` — export presence IS the wiring decision),
281
- // so they are off limits even when the channel is absent: a
282
- // mere type under one of these names would falsely activate
283
- // the channel and then fail as a non-function.
284
- try reserved.appendSlice(self.arena, &.{
285
- "subscriptions", "commandMsg", "frameMsg", "keyMsg",
286
- "pinchMsg", "dropMsg", "appearanceMsg", "chromeMsg",
287
- "envMsgs",
288
- });
289
- // The wiring aliases (wiringAliases) claim the host layer's
290
- // fixed spellings whenever the sidecar's own names differ.
291
- if (!std.mem.eql(u8, self.sidecar.model, "Model")) try reserved.append(self.arena, "Model");
292
- if (!std.mem.eql(u8, self.sidecar.msg.name, "Msg")) try reserved.append(self.arena, "Msg");
293
- // Optional glue reserves its name only when it is emitted.
294
- if (self.sidecar.model_helpers.len > 0) try reserved.append(self.arena, "callHelper");
295
- const chan = self.sidecar.channels;
296
- if (chan.command_msg or chan.frame_msg or chan.key_msg or chan.pinch_msg or chan.drop_msg) {
297
- try reserved.appendSlice(self.arena, &.{ "msgFromEnvelope", "envelope", "header" });
298
- }
299
- if (self.sidecar.init_returns_cmd) try reserved.append(self.arena, "InitResult");
300
- if (self.sidecar.update_returns_cmd) try reserved.append(self.arena, "UpdateResult");
301
- if (self.sidecar.channels.frame_msg) try reserved.append(self.arena, "FrameEvent");
302
- if (self.sidecar.channels.key_msg) try reserved.append(self.arena, "KeyEvent");
303
- if (self.sidecar.channels.pinch_msg) try reserved.appendSlice(self.arena, &.{ "PinchEvent", "PinchPhase" });
304
- if (self.sidecar.channels.drop_msg) try reserved.appendSlice(self.arena, &.{ "FileDropEvent", "FileDropPoint" });
305
-
306
- const table_names = try self.allTableNames();
307
- for (table_names) |name| {
308
- if (nameListed(reserved.items, name)) {
309
- self.diags.flag("types", "type name \"{s}\" collides with a declaration the generated shim itself must make; rename the type in the core source", .{name});
310
- }
311
- }
312
- if (nameListed(reserved.items, self.sidecar.msg.name) or nameListed(table_names, self.sidecar.msg.name)) {
313
- self.diags.flag("msg.name", "message union name \"{s}\" collides with another declaration of the generated shim; rename the union in the core source", .{self.sidecar.msg.name});
314
- }
315
- // Helper methods share the model struct's member namespace with
316
- // its fields — and "view_unbound" is the opt-out tuple's
317
- // spelling, which every reflecting consumer reads as data, so a
318
- // helper must never take it. A helper may not take a reserved
319
- // name either: methods shadow file-scope declarations inside
320
- // the struct, so a helper named after generated glue (callHelper
321
- // above all) would capture the glue's own call sites.
322
- for (self.sidecar.model_helpers) |helper| {
323
- if (nameListed(reserved.items, helper.name)) {
324
- self.diags.flag("model_helpers", "helper \"{s}\" collides with a declaration the generated shim itself must make (methods shadow file-scope names inside the model struct); rename the helper in the core source", .{helper.name});
325
- }
326
- // The same shadowing applies to the mirror's own type
327
- // declarations: a field spelled `row: Row` inside the model
328
- // struct resolves Row to a method of that name first.
329
- if (nameListed(table_names, helper.name) or std.mem.eql(u8, helper.name, self.sidecar.msg.name)) {
330
- self.diags.flag("model_helpers", "helper \"{s}\" shadows the type of the same name inside the model struct, where field types resolve; rename one in the core source", .{helper.name});
331
- }
332
- }
333
- if (sidecar_mod.findStruct(self.sidecar.types, self.sidecar.model)) |model| {
334
- for (self.sidecar.model_helpers) |helper| {
335
- if (std.mem.eql(u8, helper.name, "view_unbound")) {
336
- self.diags.flag("model_helpers", "helper \"view_unbound\" takes the unbound-list declaration's spelling — the contract reflection reads that name as the opt-out tuple; rename the helper in the core source", .{});
337
- }
338
- for (model.fields) |field| {
339
- if (std.mem.eql(u8, helper.name, field.name)) {
340
- self.diags.flag("model_helpers", "helper \"{s}\" collides with the model field of the same name — the mirror declares helpers as model methods, one member namespace; rename one in the core source", .{helper.name});
341
- }
342
- }
343
- }
344
- if (self.sidecar.model_unbound.len > 0) {
345
- for (model.fields) |field| {
346
- if (std.mem.eql(u8, field.name, "view_unbound")) {
347
- self.diags.flag("model_unbound", "the model field \"view_unbound\" collides with the unbound-list declaration the mirror must make; rename the field in the core source", .{});
348
- }
349
- }
350
- }
351
- }
352
- if (self.sidecar.msg.unbound.len > 0) {
353
- for (self.sidecar.msg.arms) |arm| {
354
- if (std.mem.eql(u8, arm.name, "view_unbound")) {
355
- self.diags.flag("msg.unbound", "the message arm \"view_unbound\" collides with the unbound-list declaration the mirror must make; rename the arm in the core source", .{});
356
- }
357
- }
358
- }
359
- }
360
-
361
- // ------------------------------------------- channel arm shapes
362
- //
363
- // The appearance and chrome channels have no function entry point:
364
- // the host constructs the named arm's record itself, so the arm
365
- // must carry the exact structural vocabulary the adapter builds by
366
- // field name. The adapter re-proves this at comptime over the
367
- // mirror; checking here moves the teaching to tool time instead of
368
- // a compile error inside generated wiring.
369
-
370
- fn validateChannelShapes(self: *Emitter) void {
371
- if (self.sidecar.channels.appearance_msg) |arm_name| {
372
- const teaching = "the appearance arm must carry exactly {{ colorScheme: a light/dark enum, reduceMotion: bool, highContrast: bool }} — the host builds this record by field name";
373
- const record = self.channelArmRecord(arm_name) orelse {
374
- self.diags.flag("channels.appearance_msg", teaching, .{});
375
- return;
376
- };
377
- if (!self.isAppearanceRecord(record)) {
378
- self.diags.flag("channels.appearance_msg", teaching, .{});
379
- }
380
- }
381
- if (self.sidecar.channels.chrome_msg) |arm_name| {
382
- const teaching = "the chrome arm must carry exactly {{ insets: top/right/bottom/left numbers, buttons: x/y/width/height numbers, tabsProjected: bool }} — the host builds this record by field name";
383
- const record = self.channelArmRecord(arm_name) orelse {
384
- self.diags.flag("channels.chrome_msg", teaching, .{});
385
- return;
386
- };
387
- if (!self.isChromeRecord(record)) {
388
- self.diags.flag("channels.chrome_msg", teaching, .{});
389
- }
390
- }
391
- }
392
-
393
- fn channelArmRecord(self: *Emitter, arm_name: []const u8) ?*const sidecar_mod.Struct {
394
- const arm = sidecar_mod.findArm(self.sidecar.msg, arm_name) orelse return null;
395
- return switch (arm.payload) {
396
- .record => |name| sidecar_mod.findStruct(self.sidecar.types, name),
397
- else => null,
398
- };
399
- }
400
-
401
- fn isAppearanceRecord(self: *Emitter, record: *const sidecar_mod.Struct) bool {
402
- if (record.fields.len != 3) return false;
403
- var scheme_ok = false;
404
- var reduce_ok = false;
405
- var contrast_ok = false;
406
- for (record.fields) |field| {
407
- if (std.mem.eql(u8, field.name, "colorScheme")) {
408
- const members = switch (field.type) {
409
- .enum_ref => |name| (sidecar_mod.findEnum(self.sidecar.types, name) orelse return false).members,
410
- else => return false,
411
- };
412
- if (members.len != 2) return false;
413
- var light = false;
414
- var dark = false;
415
- for (members) |member| {
416
- if (std.mem.eql(u8, member, "light")) light = true;
417
- if (std.mem.eql(u8, member, "dark")) dark = true;
418
- }
419
- scheme_ok = light and dark;
420
- } else if (std.mem.eql(u8, field.name, "reduceMotion")) {
421
- reduce_ok = field.type == .bool;
422
- } else if (std.mem.eql(u8, field.name, "highContrast")) {
423
- contrast_ok = field.type == .bool;
424
- }
425
- }
426
- return scheme_ok and reduce_ok and contrast_ok;
427
- }
428
-
429
- fn isChromeRecord(self: *Emitter, record: *const sidecar_mod.Struct) bool {
430
- if (record.fields.len != 3) return false;
431
- var insets_ok = false;
432
- var buttons_ok = false;
433
- var tabs_ok = false;
434
- for (record.fields) |field| {
435
- if (std.mem.eql(u8, field.name, "insets")) {
436
- insets_ok = self.isNumericRecord(field.type, &.{ "top", "right", "bottom", "left" });
437
- } else if (std.mem.eql(u8, field.name, "buttons")) {
438
- buttons_ok = self.isNumericRecord(field.type, &.{ "x", "y", "width", "height" });
439
- } else if (std.mem.eql(u8, field.name, "tabsProjected")) {
440
- tabs_ok = field.type == .bool;
441
- }
442
- }
443
- return insets_ok and buttons_ok and tabs_ok;
444
- }
445
-
446
- /// A BY-VALUE record reference for structural host vocabularies. A node
447
- /// reference mirrors as a pointer and therefore cannot satisfy a record
448
- /// the host constructs directly by field name.
449
- fn recordOf(self: *Emitter, ref: TypeRef) ?*const sidecar_mod.Struct {
450
- return switch (ref) {
451
- .value => |name| sidecar_mod.findStruct(self.sidecar.types, name),
452
- else => null,
453
- };
454
- }
455
-
456
- fn isNumericRef(ref: TypeRef) bool {
457
- return ref == .f64 or ref == .i64;
458
- }
459
-
460
- fn isNumericRecord(self: *Emitter, ref: TypeRef, names: []const []const u8) bool {
461
- const record = self.recordOf(ref) orelse return false;
462
- if (record.fields.len != names.len) return false;
463
- outer: for (names) |name| {
464
- for (record.fields) |field| {
465
- if (std.mem.eql(u8, field.name, name)) {
466
- if (!isNumericRef(field.type)) return false;
467
- continue :outer;
468
- }
469
- }
470
- return false;
471
- }
472
- return true;
473
- }
474
-
475
- /// Records the type table stores BY REFERENCE anywhere (`node`
476
- /// TypeRefs — committed-heap node types, the model root included)
477
- /// also ride by reference when they double as a message arm payload
478
- /// (`picked: *const Item`, verified against the shipped emitter) —
479
- /// a storage fact the record payload family cannot carry, so
480
- /// mirroring the arm by value would silently change the reflected
481
- /// layout. Refuse that shape until the schema says storage
482
- /// (SCHEMA-GAPS.md). Value-stored records — the corpus's
483
- /// synthesized inline records, event shapes, and value-promoted
484
- /// records like a text-input union's caret and selection payloads —
485
- /// stay by value in arms exactly as the emission keeps them.
486
- fn validateArmStorage(self: *Emitter) Error!void {
487
- for (self.sidecar.msg.arms) |arm| {
488
- switch (arm.payload) {
489
- .record => |name| {
490
- if (self.nodeStored(name)) {
491
- self.diags.flag("msg.arms", "arm \"{s}\": record \"{s}\" is stored by reference in the model graph, and the compiled core keeps that storage when the record doubles as an arm payload — a fact the record payload family cannot carry; keep the arm's payload a value-stored record", .{ arm.name, name });
492
- }
493
- },
494
- else => {},
495
- }
496
- }
497
- }
498
-
499
- /// Whether any reference in the contract stores this record by
500
- /// reference (or it is the committed model root itself).
501
- fn nodeStored(self: *Emitter, name: []const u8) bool {
502
- if (std.mem.eql(u8, self.sidecar.model, name)) return true;
503
- for (self.sidecar.types.structs) |entry| {
504
- for (entry.fields) |field| {
505
- if (refNodeNames(field.type, name)) return true;
506
- }
507
- }
508
- for (self.sidecar.types.unions) |entry| {
509
- for (entry.arms) |arm| {
510
- if (arm.payload != .void and refNodeNames(arm.payload, name)) return true;
511
- }
512
- }
513
- for (self.sidecar.model_helpers) |helper| {
514
- if (refNodeNames(helper.returns, name)) return true;
515
- for (helper.params) |param| {
516
- if (refNodeNames(param, name)) return true;
517
- }
518
- }
519
- return false;
520
- }
521
-
522
- fn allTableNames(self: *Emitter) Error![]const []const u8 {
523
- var names: std.ArrayListUnmanaged([]const u8) = .empty;
524
- for (self.sidecar.types.structs) |entry| try names.append(self.arena, entry.name);
525
- for (self.sidecar.types.enums) |entry| try names.append(self.arena, entry.name);
526
- for (self.sidecar.types.unions) |entry| try names.append(self.arena, entry.name);
527
- return names.items;
528
- }
529
-
530
- // ------------------------------------------------------- preamble
531
-
532
- fn header(self: *Emitter) Error!void {
533
- try self.print(
534
- \\//! Generated by corewire from this app's core.contract.json — the
535
- \\//! host-side mirror of a compiled TypeScript core. Every type below
536
- \\//! reproduces the compiled core's contract exactly (field names,
537
- \\//! declaration order, number classes), so the markup engines, the
538
- \\//! adapter, the bridge, and the model-contract step reflect over it
539
- \\//! the way they reflect over transpiler output. Do not edit;
540
- \\//! regenerate from the sidecar.
541
- \\//!
542
- \\//! Compiled core identity: entry {s}, compiler {s},
543
- \\//! build_id {x:0>16} (checked against the object's exported getter
544
- \\//! before the first dispatch).
545
- \\
546
- \\const std = @import("std");
547
- \\const shim_rt = @import("shim_rt.zig");
548
- \\const core_abi = @import("core_abi.zig");
549
- \\const abi = core_abi.Bindings("{f}");
550
- \\
551
- \\/// The sidecar's identity facts, restated for the boot-time pairing
552
- \\/// fence (the out-of-graph backstop: a cached or hand-copied core
553
- \\/// must never dispatch against another build's tag order).
554
- \\pub const sidecar_build_id: u64 = 0x{x:0>16};
555
- \\pub const sidecar_model_fingerprint: u64 = 0x{x:0>16};
556
- \\pub const sidecar_abi_version: u32 = {d};
557
- \\pub const sidecar_snapshot_format: u32 = {d};
558
- \\
559
- \\/// The compiler's module-graph attestations, restated so host
560
- \\/// policy can gate on them: record/replay arms only against a
561
- \\/// deterministic core, and loop-free-core policies read
562
- \\/// async_free. Computed facts of the compiled graph — the sidecar
563
- \\/// records verdicts, never hopes.
564
- \\pub const deterministic: bool = {};
565
- \\pub const async_free: bool = {};
566
- \\
567
- \\/// The kernel surface the host layer drives, forwarded to the shim
568
- \\/// arena and the core's own frame reset (one cycle boundary for
569
- \\/// both sides).
570
- \\pub const rt = struct {{
571
- \\ pub const Cmd = []const u8;
572
- \\ pub const Sub = []const u8;
573
- \\ pub const cmd_none: Cmd = &.{{}};
574
- \\ pub const sub_none: Sub = &.{{}};
575
- \\ pub fn frameAllocator() std.mem.Allocator {{
576
- \\ return shim_rt.frameAllocator();
577
- \\ }}
578
- \\ pub fn frameAlloc(comptime T: type, n: usize) []T {{
579
- \\ return shim_rt.frameAlloc(T, n);
580
- \\ }}
581
- \\ pub fn frameCreate(comptime T: type, value: T) *T {{
582
- \\ return shim_rt.frameCreate(T, value);
583
- \\ }}
584
- \\ pub fn decodeExact(comptime T: type, bytes: []const u8) T {{
585
- \\ return shim_rt.decodeExact(T, bytes, shim_rt.frameAllocator());
586
- \\ }}
587
- \\ pub fn copyServiceBytes(bytes: []const u8) []const u8 {{
588
- \\ return shim_rt.frameAllocator().dupe(u8, bytes) catch @panic("service result allocation failed");
589
- \\ }}
590
- \\ pub fn frameReset() void {{
591
- \\ shim_rt.frameReset();
592
- \\ abi.frame_reset();
593
- \\ }}
594
- \\ pub fn resetAll() void {{
595
- \\ shim_rt.resetAll();
596
- \\ }}
597
- \\}};
598
- , .{
599
- try commentText(self.arena, self.sidecar.entry),
600
- try commentText(self.arena, self.sidecar.compiler_version),
601
- self.sidecar.build_id,
602
- std.zig.fmtString(self.sidecar.abi.prefix),
603
- self.sidecar.build_id,
604
- self.sidecar.model_fingerprint,
605
- self.sidecar.abi_version,
606
- self.sidecar.abi.snapshot_format,
607
- self.sidecar.deterministic,
608
- self.sidecar.async_free,
609
- });
610
- }
611
-
612
- // ------------------------------------------- attested integer class
613
- //
614
- // The sidecar's one integer TypeRef spelling is i64; the matching
615
- // integer_slots entry may refine it to the u64 class, and the
616
- // mirror must decode what the attestation says (8-byte unsigned LE
617
- // instead of two's complement). The reader validates the bijection
618
- // before emission ever runs, so a lookup miss here simply means the
619
- // slot keeps its i64 spelling.
620
-
621
- fn attestedClass(self: *Emitter, slot: []const u8) ?sidecar_mod.IntegerClass {
622
- for (self.sidecar.integer_slots) |entry| {
623
- if (std.mem.eql(u8, entry.slot, slot)) return entry.class;
624
- }
625
- return null;
626
- }
627
-
628
- /// The Zig spelling of an i64-spelled slot, per its attestation.
629
- fn integerSpelling(self: *Emitter, slot: ?[]const u8) []const u8 {
630
- const path = slot orelse return "i64";
631
- const class = self.attestedClass(path) orelse return "i64";
632
- return @tagName(class);
633
- }
634
-
635
- /// The exact-crossing conversion for an integer-classed dispatch
636
- /// payload (the scalar entries carry f64): the signed guard or its
637
- /// unsigned twin, per the slot's attested class.
638
- fn exactCall(self: *Emitter, slot: []const u8, inner: []const u8) Error![]const u8 {
639
- const converter = if (self.attestedClass(slot) == .u64) "exactF64Unsigned" else "exactF64";
640
- return std.fmt.allocPrint(self.arena, "shim_rt.{s}({s})", .{ converter, inner });
641
- }
642
-
643
- /// A slot path in the sidecar's grammar: `<Container>.<member>`.
644
- fn slotPath(self: *Emitter, container: []const u8, member: []const u8) Error![]const u8 {
645
- return std.fmt.allocPrint(self.arena, "{s}.{s}", .{ container, member });
646
- }
647
-
648
- /// A number_bytes arm's number-field slot path:
649
- /// `<msgName>.<arm>.<field>`.
650
- fn numberBytesSlot(self: *Emitter, arm: []const u8, field: []const u8) Error![]const u8 {
651
- return std.fmt.allocPrint(self.arena, "{s}.{s}.{s}", .{ self.sidecar.msg.name, arm, field });
652
- }
653
-
654
- /// The Zig spelling of a number descriptor's payload, per class and
655
- /// attestation.
656
- fn numberSpelling(self: *Emitter, class: sidecar_mod.NumberClass, slot: []const u8) Error![]const u8 {
657
- return switch (class) {
658
- .f64 => "f64",
659
- .i64 => self.integerSpelling(slot),
660
- };
661
- }
662
-
663
- // ------------------------------------ host-supplied signed slots
664
- //
665
- // Two structural vocabularies carry slots the HOST fills with
666
- // values that go negative: a scroll arm's offsets and velocities
667
- // (rubber-banding, upward flicks) and the window-control cluster's
668
- // position (signed content coordinates). A u64 attestation on such
669
- // a slot could never carry those values — the generated wiring
670
- // would trap converting them — so the checker refuses the
671
- // attestation at tool time instead. Everything the host supplies
672
- // non-negative stays attestable: scroll extents, selection bounds,
673
- // chrome insets and cluster sizes — and a scroll-shaped record the
674
- // host never dispatches into (a model-only metrics record) is not
675
- // this rule's business at all.
676
-
677
- /// The scroll-state axes the host supplies signed, in both
678
- /// spellings (the TS mirror's and the canvas core's).
679
- const signed_scroll_axes = [_][]const u8{
680
- "offsetX", "offsetY", "velocityX", "velocityY",
681
- "offset_x", "offset_y", "velocity_x", "velocity_y",
682
- };
683
-
684
- fn validateIntegerAttestations(self: *Emitter) Error!void {
685
- // Only records a message arm routes through the scroll entry
686
- // receive host scroll values.
687
- for (self.sidecar.msg.arms) |arm| {
688
- const record_name = switch (arm.payload) {
689
- .record => |name| name,
690
- else => continue,
691
- };
692
- if (self.scrollStateFields(record_name) == null) continue;
693
- const entry = sidecar_mod.findStruct(self.sidecar.types, record_name) orelse continue;
694
- for (entry.fields) |field| {
695
- if (field.type != .i64) continue;
696
- if (!nameListed(&signed_scroll_axes, field.name)) continue;
697
- const slot = try self.slotPath(entry.name, field.name);
698
- if (self.attestedClass(slot) == .u64) {
699
- self.diags.flag("integer_slots", "\"{s}\" is a signed scroll-state axis (offsets and velocities go negative during rubber-banding and upward flicks) — the u64 class cannot carry those values; keep offset and velocity fields i64 or f64", .{slot});
700
- }
701
- }
702
- }
703
- if (self.sidecar.channels.chrome_msg) |arm_name| {
704
- if (self.channelArmRecord(arm_name)) |record| {
705
- for (record.fields) |field| {
706
- // Insets and cluster sizes are non-negative overlay
707
- // extents; only the cluster's position is signed.
708
- if (!std.mem.eql(u8, field.name, "buttons")) continue;
709
- const nested = self.recordOf(field.type) orelse continue;
710
- for (nested.fields) |geometry| {
711
- if (geometry.type != .i64) continue;
712
- if (!std.mem.eql(u8, geometry.name, "x") and !std.mem.eql(u8, geometry.name, "y")) continue;
713
- const slot = try self.slotPath(nested.name, geometry.name);
714
- if (self.attestedClass(slot) == .u64) {
715
- self.diags.flag("integer_slots", "\"{s}\" is the window-control cluster's position, reported in signed content coordinates — the u64 class cannot carry it; keep buttons x and y i64 or f64", .{slot});
716
- }
717
- }
718
- }
719
- }
720
- }
721
- }
722
-
723
- // --------------------------------------------------- mirror types
724
-
725
- /// Inline a table entry only when the pattern matches AND the entry
726
- /// is referenced exactly once in the whole sidecar: an authored type
727
- /// that merely spells like the pattern but is shared across sites
728
- /// stays a named top-level declaration (inlining it at one site
729
- /// would leave the others dangling).
730
- fn inlinedNames(self: *Emitter) Error![]const []const u8 {
731
- return inlinedTableNames(self.arena, self.sidecar);
732
- }
733
-
734
- fn mirrorTypes(self: *Emitter) Error!void {
735
- const inlined = self.inlined;
736
- for (self.sidecar.types.enums) |entry| {
737
- // The 256-member bound is validated by the reader
738
- // (sidecar.zig) before emission ever runs.
739
- try self.print("\npub const {f} = enum(u8) {{", .{ident(entry.name)});
740
- for (entry.members, 0..) |member, index| {
741
- try self.print("\n {f} = {d},", .{ ident(member), index });
742
- }
743
- try self.raw("\n};\n");
744
- }
745
- for (self.sidecar.types.structs) |entry| {
746
- if (std.mem.eql(u8, entry.name, self.sidecar.model)) continue;
747
- if (nameListed(inlined, entry.name)) continue;
748
- try self.print("\npub const {f} = struct {{", .{ident(entry.name)});
749
- for (entry.fields) |field| {
750
- try self.print("\n {f}: {s},", .{ ident(field.name), try self.spellRef(field.type, entry.name, field.name, try self.slotPath(entry.name, field.name)) });
751
- }
752
- try self.raw("\n};\n");
753
- }
754
- for (self.sidecar.types.unions) |entry| {
755
- if (nameListed(inlined, entry.name)) continue;
756
- try self.print("\npub const {f} = union(enum) {{", .{ident(entry.name)});
757
- for (entry.arms) |arm| {
758
- if (arm.payload == .void) {
759
- try self.print("\n {f},", .{ident(arm.name)});
760
- } else {
761
- try self.print("\n {f}: {s},", .{ ident(arm.name), try self.spellRef(arm.payload, entry.name, arm.name, try self.slotPath(entry.name, arm.name)) });
762
- }
763
- }
764
- try self.raw("\n};\n");
765
- }
766
- }
767
-
768
- /// The one TypeRef-to-Zig-spelling authority. `container`/`member`
769
- /// name the reference site so synthesized entries inline here;
770
- /// `slot` is the site's slot path in the sidecar's grammar (null
771
- /// where no path form exists), consulted for the attested integer
772
- /// class. Slice elements have no slot path — the format-1 grammar
773
- /// cannot attest them, so they always keep the i64 spelling.
774
- fn spellRef(self: *Emitter, ref: TypeRef, container: []const u8, member: []const u8, slot: ?[]const u8) Error![]const u8 {
775
- return switch (ref) {
776
- .bool => "bool",
777
- .f64 => "f64",
778
- .i64 => self.integerSpelling(slot),
779
- .bytes => "[]const u8",
780
- .void => "void",
781
- .optional => |inner| try std.fmt.allocPrint(self.arena, "?{s}", .{try self.spellRef(inner.*, container, member, slot)}),
782
- .slice => |elem| try std.fmt.allocPrint(self.arena, "[]const {s}", .{try self.spellRef(elem.*, container, member, null)}),
783
- .node => |name| try std.fmt.allocPrint(self.arena, "*const {s}", .{try self.spellNamed(name, container, member)}),
784
- .value => |name| try self.spellNamed(name, container, member),
785
- .enum_ref, .union_ref => |name| try std.fmt.allocPrint(self.arena, "{f}", .{ident(name)}),
786
- };
787
- }
788
-
789
- fn spellNamed(self: *Emitter, name: []const u8, container: []const u8, member: []const u8) Error![]const u8 {
790
- if (!isSynthesizedRef(container, member, name) or !nameListed(self.inlined, name)) {
791
- return std.fmt.allocPrint(self.arena, "{f}", .{ident(name)});
792
- }
793
- const entry = sidecar_mod.findStruct(self.sidecar.types, name) orelse
794
- return std.fmt.allocPrint(self.arena, "{f}", .{ident(name)});
795
- var text: std.ArrayListUnmanaged(u8) = .empty;
796
- try text.appendSlice(self.arena, "struct {");
797
- for (entry.fields, 0..) |field, index| {
798
- if (index > 0) try text.appendSlice(self.arena, ",");
799
- const piece = try std.fmt.allocPrint(self.arena, " {f}: {s}", .{ ident(field.name), try self.spellRef(field.type, entry.name, field.name, try self.slotPath(entry.name, field.name)) });
800
- try text.appendSlice(self.arena, piece);
801
- }
802
- try text.appendSlice(self.arena, " }");
803
- return text.items;
804
- }
805
-
806
- // ---------------------------------------------------------- model
807
-
808
- fn modelStruct(self: *Emitter) Error!void {
809
- const model = sidecar_mod.findStruct(self.sidecar.types, self.sidecar.model) orelse {
810
- self.diags.flag("model", "\"{s}\" names no struct in the type table", .{self.sidecar.model});
811
- return;
812
- };
813
- try self.print("\npub const {f} = struct {{", .{ident(model.name)});
814
- for (model.fields) |field| {
815
- try self.print("\n {f}: {s},", .{ ident(field.name), try self.spellRef(field.type, model.name, field.name, try self.slotPath(model.name, field.name)) });
816
- }
817
- if (self.sidecar.model_helpers.len > 0) try self.raw("\n");
818
- for (self.sidecar.model_helpers, 0..) |helper, index| {
819
- const returns = try self.spellRef(helper.returns, "helpers", helper.name, try std.fmt.allocPrint(self.arena, "helpers.{s}.return", .{helper.name}));
820
- if (helper.arena) {
821
- // The build-arena scalar form: the view build hands its
822
- // arena; the decoded result lives there.
823
- try self.print("\n pub fn {f}(self: *const {f}, arena: std.mem.Allocator) {s} {{\n _ = self;\n return callHelper({s}, {d}, &.{{}}, arena);\n }}", .{ ident(helper.name), ident(model.name), returns, returns, index });
824
- if (helper.params.len > 0) {
825
- self.diags.flag("model_helpers", "helper \"{s}\": the arena form takes no extra parameters (the arena is the second parameter by contract)", .{helper.name});
826
- }
827
- continue;
828
- }
829
- if (helper.params.len == 0) {
830
- try self.print("\n pub fn {f}(self: *const {f}) {s} {{\n _ = self;\n return callHelper({s}, {d}, &.{{}}, shim_rt.frameAllocator());\n }}", .{ ident(helper.name), ident(model.name), returns, returns, index });
831
- } else {
832
- // Extra parameters cross as their canonical encodings,
833
- // concatenated in order (a tuple encodes as exactly
834
- // that).
835
- var params: std.ArrayListUnmanaged(u8) = .empty;
836
- var tuple: std.ArrayListUnmanaged(u8) = .empty;
837
- for (helper.params, 0..) |param, param_index| {
838
- const spelled = try self.spellRef(param, "helpers", helper.name, try std.fmt.allocPrint(self.arena, "helpers.{s}.params[{d}]", .{ helper.name, param_index }));
839
- try params.appendSlice(self.arena, try std.fmt.allocPrint(self.arena, ", p{d}: {s}", .{ param_index, spelled }));
840
- if (param_index > 0) try tuple.appendSlice(self.arena, ", ");
841
- try tuple.appendSlice(self.arena, try std.fmt.allocPrint(self.arena, "p{d}", .{param_index}));
842
- }
843
- try self.print("\n pub fn {f}(self: *const {f}{s}) {s} {{\n _ = self;\n const args_tuple = .{{ {s} }};\n const args = shim_rt.encodeAlloc(@TypeOf(args_tuple), args_tuple, shim_rt.frameAllocator());\n return callHelper({s}, {d}, args, shim_rt.frameAllocator());\n }}", .{ ident(helper.name), ident(model.name), params.items, returns, tuple.items, returns, index });
844
- }
845
- }
846
- try self.unboundDecl(self.sidecar.model_unbound);
847
- try self.raw("\n};\n");
848
- }
849
-
850
- fn unboundDecl(self: *Emitter, names: []const []const u8) Error!void {
851
- if (names.len == 0) return;
852
- try self.raw("\n\n pub const view_unbound = .{");
853
- for (names, 0..) |name, index| {
854
- if (index > 0) try self.raw(",");
855
- try self.print(" \"{f}\"", .{std.zig.fmtString(name)});
856
- }
857
- try self.raw(" };");
858
- }
859
-
860
- // ------------------------------------------------------------ msg
861
-
862
- fn msgUnion(self: *Emitter) Error!void {
863
- try self.print("\npub const {f} = union(enum) {{", .{ident(self.sidecar.msg.name)});
864
- for (self.sidecar.msg.arms) |arm| {
865
- switch (arm.payload) {
866
- .void => try self.print("\n {f},", .{ident(arm.name)}),
867
- .bytes => try self.print("\n {f}: []const u8,", .{ident(arm.name)}),
868
- .number => |class| try self.print("\n {f}: {s},", .{ ident(arm.name), try self.numberSpelling(class, try self.slotPath(self.sidecar.msg.name, arm.name)) }),
869
- // The two-field family carries no field-order fact; the
870
- // mirror declares the number field first, the emitted
871
- // convention of every producer of this shape. A record
872
- // declared bytes-first must ride the record family (its
873
- // table entry carries order explicitly) — see
874
- // SCHEMA-GAPS.md.
875
- .number_bytes => |desc| try self.print("\n {f}: struct {{ {f}: {s}, {f}: []const u8 }},", .{ ident(arm.name), ident(desc.number_field), try self.numberSpelling(desc.number_class, try self.numberBytesSlot(arm.name, desc.number_field)), ident(desc.bytes_field) }),
876
- .record => |name| try self.print("\n {f}: {s},", .{ ident(arm.name), try self.spellNamed(name, self.sidecar.msg.name, arm.name) }),
877
- .union_ref, .enum_ref => |name| try self.print("\n {f}: {f},", .{ ident(arm.name), ident(name) }),
878
- .scalar => |ref| try self.print("\n {f}: {s},", .{ ident(arm.name), try self.spellRef(ref, self.sidecar.msg.name, arm.name, try self.slotPath(self.sidecar.msg.name, arm.name)) }),
879
- }
880
- }
881
- try self.unboundDecl(self.sidecar.msg.unbound);
882
- try self.raw("\n};\n");
883
- }
884
-
885
- /// The host wiring names its roots by fixed spelling — the staged
886
- /// main re-exports `core.Model` and `core.Msg`, and the adapter and
887
- /// bridge reflect those decls — so a sidecar whose own names differ
888
- /// gets aliases (same types; aliases are transparent to every
889
- /// reflecting consumer).
890
- fn wiringAliases(self: *Emitter) Error!void {
891
- if (!std.mem.eql(u8, self.sidecar.model, "Model")) {
892
- try self.print("\n/// The host wiring's spelling for the root state type.\npub const Model = {f};\n", .{ident(self.sidecar.model)});
893
- }
894
- if (!std.mem.eql(u8, self.sidecar.msg.name, "Msg")) {
895
- try self.print("\n/// The host wiring's spelling for the message union.\npub const Msg = {f};\n", .{ident(self.sidecar.msg.name)});
896
- }
897
- }
898
-
899
- fn tagTable(self: *Emitter) Error!void {
900
- const consistency_quota = msgTagConsistencyQuota(self.sidecar.msg.arms);
901
- try self.raw(
902
- \\
903
- \\/// Declaration-order wire tags: the arm's index in this table IS
904
- \\/// its wire tag (dense, u8). The sidecar is the tag authority; the
905
- \\/// compiled object's lowering assigns the same order, and the
906
- \\/// boot-time build_id fence proves both came from one compile.
907
- \\pub const msg_tags = [_][]const u8{
908
- );
909
- for (self.sidecar.msg.arms) |arm| {
910
- try self.print("\n \"{f}\",", .{std.zig.fmtString(arm.name)});
911
- }
912
- try self.raw("\n};\n");
913
- try self.print(
914
- \\
915
- \\comptime {{
916
- \\ @setEvalBranchQuota({d});
917
- \\ // The union and the tag table are emitted from one arm list;
918
- \\ // hold them equal anyway so a hand edit cannot skew dispatch.
919
- \\ const fields = @typeInfo({f}).@"union".fields;
920
- \\ if (fields.len != msg_tags.len) @compileError("core_shim: Msg arm count does not match msg_tags");
921
- \\ for (fields, msg_tags) |field, tag_name| {{
922
- \\ if (!std.mem.eql(u8, field.name, tag_name)) @compileError("core_shim: Msg arm names do not match msg_tags");
923
- \\ }}
924
- \\}}
925
- \\
926
- , .{ consistency_quota, ident(self.sidecar.msg.name) });
927
- }
928
-
929
- // --------------------------------------------------- entry points
930
-
931
- fn entryPoints(self: *Emitter) Error!void {
932
- const model_name = try std.fmt.allocPrint(self.arena, "{f}", .{ident(self.sidecar.model)});
933
-
934
- // Boot. Export attestation first (V11's link-time half: the
935
- // bindings resolve lazily, so referencing every attested
936
- // symbol here turns a missing export into a LINK failure
937
- // instead of a latent hole), then identity (pure getters, the
938
- // staleness fence), then the sink, then init — the ABI's
939
- // required ordering.
940
- try self.raw(
941
- \\
942
- \\fn boot() void {
943
- \\ referenceAttestedExports();
944
- \\ shim_rt.verifyIdentity(abi.abi_version_fn(), abi.build_id(), sidecar_abi_version, sidecar_build_id);
945
- \\ abi.set_panic_sink(shim_rt.panicSink, null);
946
- \\ abi.init();
947
- \\}
948
- \\
949
- \\/// The sidecar's abi.exports list is a biconditional attestation:
950
- \\/// every listed symbol exists in the object, and the object
951
- \\/// exports nothing else under the prefix. Referencing each listed
952
- \\/// symbol makes the LINKER prove the "exists" direction against
953
- \\/// the real binary. The "nothing else" direction is not provable
954
- \\/// from the consumer side — a linker ignores unreferenced extra
955
- \\/// exports, and this generator never opens the object — so it
956
- \\/// belongs to the producer's conformance suite, which probes the
957
- \\/// object's full symbol table at compile time.
958
- \\fn referenceAttestedExports() void {
959
- \\
960
- );
961
- for (self.sidecar.abi.exports) |suffix| {
962
- const binding = if (std.mem.eql(u8, suffix, "abi_version")) "abi_version_fn" else suffix;
963
- try self.print(" std.mem.doNotOptimizeAway(abi.{f});\n", .{ident(binding)});
964
- }
965
- try self.raw("}\n");
966
-
967
- if (self.sidecar.init_returns_cmd) {
968
- try self.print(
969
- \\
970
- \\pub const InitResult = struct {{ model: *const {s}, cmd: rt.Cmd }};
971
- \\
972
- \\pub fn bootCommand() rt.Cmd {{
973
- \\ var cmd_ptr: [*]const u8 = undefined;
974
- \\ var cmd_len: usize = 0;
975
- \\ abi.boot_cmd(&cmd_ptr, &cmd_len);
976
- \\ return cmd_ptr[0..cmd_len];
977
- \\}}
978
- \\
979
- \\pub fn initialModel() InitResult {{
980
- \\ boot();
981
- \\ return .{{ .model = snapshotModel(), .cmd = bootCommand() }};
982
- \\}}
983
- \\
984
- , .{model_name});
985
- } else {
986
- try self.print(
987
- \\
988
- \\pub fn initialModel() *const {s} {{
989
- \\ boot();
990
- \\ return snapshotModel();
991
- \\}}
992
- \\
993
- , .{model_name});
994
- }
995
-
996
- if (self.sidecar.update_returns_cmd) {
997
- try self.print(
998
- \\
999
- \\pub const UpdateResult = struct {{ model: *const {s}, cmd: rt.Cmd }};
1000
- \\
1001
- \\pub fn update(model: *const {s}, msg: {f}) UpdateResult {{
1002
- \\
1003
- , .{ model_name, model_name, ident(self.sidecar.msg.name) });
1004
- } else {
1005
- try self.print(
1006
- \\
1007
- \\pub fn update(model: *const {s}, msg: {f}) *const {s} {{
1008
- \\
1009
- , .{ model_name, ident(self.sidecar.msg.name), model_name });
1010
- }
1011
- try self.raw(
1012
- \\ // The compiled core owns the committed state; the mirror root
1013
- \\ // the host passes is a decoded copy, so only tag and payload
1014
- \\ // cross the boundary.
1015
- \\ _ = model;
1016
- \\ var cmd_ptr: [*]const u8 = undefined;
1017
- \\ var cmd_len: usize = 0;
1018
- \\ switch (msg) {
1019
- \\
1020
- );
1021
- for (self.sidecar.msg.arms, 0..) |arm, tag| {
1022
- try self.dispatchArm(arm, tag);
1023
- }
1024
- try self.raw(" }\n");
1025
- if (self.sidecar.update_returns_cmd) {
1026
- try self.raw(" return .{ .model = snapshotModel(), .cmd = cmd_ptr[0..cmd_len] };\n}\n");
1027
- } else {
1028
- // Every dispatch ABI receives these as out-pointers even when
1029
- // the authored update returns only Model. Passing their addresses
1030
- // is already a use; discarding them again is a Zig 0.16 error
1031
- // ("pointless discard of local variable"). The core owns and
1032
- // ignores the empty command envelope in this shape.
1033
- try self.raw(" return snapshotModel();\n}\n");
1034
- }
1035
-
1036
- if (self.sidecar.has_subscriptions) {
1037
- try self.print(
1038
- \\
1039
- \\pub fn subscriptions(model: *const {s}) rt.Sub {{
1040
- \\ // The core derives subscriptions from its own committed
1041
- \\ // model; the mirror root only satisfies the signature the
1042
- \\ // host layer expects.
1043
- \\ _ = model;
1044
- \\ var subs_ptr: [*]const u8 = undefined;
1045
- \\ var subs_len: usize = 0;
1046
- \\ abi.subscriptions(&subs_ptr, &subs_len);
1047
- \\ return subs_ptr[0..subs_len];
1048
- \\}}
1049
- \\
1050
- , .{model_name});
1051
- }
1052
-
1053
- try self.print(
1054
- \\
1055
- \\/// Commit happens inside every dispatch entry core-side; the
1056
- \\/// decoded mirror is already the committed value, so the frame-end
1057
- \\/// commit is identity here.
1058
- \\pub fn commitModelRoot(next: *const {s}) *const {s} {{
1059
- \\ return next;
1060
- \\}}
1061
- \\
1062
- , .{ model_name, model_name });
1063
- }
1064
-
1065
- fn dispatchArm(self: *Emitter, arm: sidecar_mod.MsgArm, tag: usize) Error!void {
1066
- const name = try std.fmt.allocPrint(self.arena, "{f}", .{ident(arm.name)});
1067
- switch (arm.payload) {
1068
- .void => try self.print(" .{s} => abi.dispatch_void({d}, &cmd_ptr, &cmd_len),\n", .{ name, tag }),
1069
- .bytes => try self.print(" .{s} => |payload| abi.dispatch_bytes({d}, payload.ptr, payload.len, &cmd_ptr, &cmd_len),\n", .{ name, tag }),
1070
- .number => |class| switch (class) {
1071
- .f64 => try self.print(" .{s} => |payload| abi.dispatch_number({d}, payload, &cmd_ptr, &cmd_len),\n", .{ name, tag }),
1072
- .i64 => try self.print(" .{s} => |payload| abi.dispatch_number({d}, {s}, &cmd_ptr, &cmd_len),\n", .{ name, tag, try self.exactCall(try self.slotPath(self.sidecar.msg.name, arm.name), "payload") }),
1073
- },
1074
- .number_bytes => |desc| {
1075
- const number_expr = switch (desc.number_class) {
1076
- .f64 => try std.fmt.allocPrint(self.arena, "payload.{f}", .{ident(desc.number_field)}),
1077
- .i64 => try self.exactCall(try self.numberBytesSlot(arm.name, desc.number_field), try std.fmt.allocPrint(self.arena, "payload.{f}", .{ident(desc.number_field)})),
1078
- };
1079
- try self.print(" .{s} => |payload| abi.dispatch_number_bytes({d}, {s}, payload.{f}.ptr, payload.{f}.len, &cmd_ptr, &cmd_len),\n", .{ name, tag, number_expr, ident(desc.bytes_field), ident(desc.bytes_field) });
1080
- },
1081
- .enum_ref => try self.print(" .{s} => |payload| abi.dispatch_enum({d}, @intCast(@intFromEnum(payload)), &cmd_ptr, &cmd_len),\n", .{ name, tag }),
1082
- .union_ref => |type_name| {
1083
- if (isTextInputUnion(self.sidecar, type_name)) {
1084
- try self.print(" .{s} => |payload| {{\n const encoded = shim_rt.encodeAlloc(@TypeOf(payload), payload, shim_rt.frameAllocator());\n abi.dispatch_text_input({d}, encoded.ptr, encoded.len, &cmd_ptr, &cmd_len);\n }},\n", .{ name, tag });
1085
- } else {
1086
- try self.print(" .{s} => |payload| {{\n const encoded = shim_rt.encodeAlloc(@TypeOf(payload), payload, shim_rt.frameAllocator());\n abi.dispatch_record({d}, encoded.ptr, encoded.len, &cmd_ptr, &cmd_len);\n }},\n", .{ name, tag });
1087
- }
1088
- },
1089
- .record => |type_name| {
1090
- if (self.scrollStateFields(type_name)) |fields| {
1091
- var scalars: std.ArrayListUnmanaged(u8) = .empty;
1092
- for (fields, 0..) |field, index| {
1093
- if (index > 0) try scalars.appendSlice(self.arena, ", ");
1094
- const record = sidecar_mod.findStruct(self.sidecar.types, type_name).?;
1095
- const expr = switch (record.fields[field].type) {
1096
- .i64 => try self.exactCall(try self.slotPath(type_name, record.fields[field].name), try std.fmt.allocPrint(self.arena, "payload.{f}", .{ident(record.fields[field].name)})),
1097
- else => try std.fmt.allocPrint(self.arena, "payload.{f}", .{ident(record.fields[field].name)}),
1098
- };
1099
- try scalars.appendSlice(self.arena, expr);
1100
- }
1101
- try self.print(" .{s} => |payload| abi.dispatch_scroll_state({d}, {s}, &cmd_ptr, &cmd_len),\n", .{ name, tag, scalars.items });
1102
- } else {
1103
- try self.print(" .{s} => |payload| {{\n const encoded = shim_rt.encodeAlloc(@TypeOf(payload), payload, shim_rt.frameAllocator());\n abi.dispatch_record({d}, encoded.ptr, encoded.len, &cmd_ptr, &cmd_len);\n }},\n", .{ name, tag });
1104
- }
1105
- },
1106
- .scalar => |ref| switch (ref) {
1107
- .bool => try self.print(" .{s} => |payload| abi.dispatch_bool({d}, @intFromBool(payload), &cmd_ptr, &cmd_len),\n", .{ name, tag }),
1108
- .f64 => try self.print(" .{s} => |payload| abi.dispatch_number({d}, payload, &cmd_ptr, &cmd_len),\n", .{ name, tag }),
1109
- .i64 => try self.print(" .{s} => |payload| abi.dispatch_number({d}, {s}, &cmd_ptr, &cmd_len),\n", .{ name, tag, try self.exactCall(try self.slotPath(self.sidecar.msg.name, arm.name), "payload") }),
1110
- .bytes => try self.print(" .{s} => |payload| abi.dispatch_bytes({d}, payload.ptr, payload.len, &cmd_ptr, &cmd_len),\n", .{ name, tag }),
1111
- else => self.diags.flag("msg.arms", "arm \"{s}\": ABI version 1 has no dispatch entry for this scalar shape (bool, number, and bytes scalars only)", .{arm.name}),
1112
- },
1113
- }
1114
- }
1115
-
1116
- /// Declaration-order field indexes of a scroll-state record, in the
1117
- /// ABI entry's parameter order (the eight per-axis scalars,
1118
- /// offset_x through content_extent_y) — or null when the record is
1119
- /// not that shape.
1120
- fn scrollStateFields(self: *Emitter, type_name: []const u8) ?[8]usize {
1121
- const entry = sidecar_mod.findStruct(self.sidecar.types, type_name) orelse return null;
1122
- if (entry.fields.len != 8) return null;
1123
- const spellings = [_][8][]const u8{ scroll_state_fields_ts, scroll_state_fields_canvas };
1124
- for (spellings) |names| {
1125
- var indexes: [8]usize = undefined;
1126
- var all_found = true;
1127
- for (names, 0..) |field_name, position| {
1128
- indexes[position] = for (entry.fields, 0..) |field, index| {
1129
- const numeric = field.type == .f64 or field.type == .i64;
1130
- if (std.mem.eql(u8, field.name, field_name) and numeric) break index;
1131
- } else {
1132
- all_found = false;
1133
- break;
1134
- };
1135
- if (!all_found) break;
1136
- }
1137
- if (all_found) return indexes;
1138
- }
1139
- return null;
1140
- }
1141
-
1142
- // -------------------------------------------------------- channels
1143
-
1144
- fn channels(self: *Emitter) Error!void {
1145
- const chan = self.sidecar.channels;
1146
- const any_fn_channel = chan.command_msg or chan.frame_msg or chan.key_msg or chan.pinch_msg or chan.drop_msg;
1147
-
1148
- if (chan.command_msg) {
1149
- try self.print(
1150
- \\
1151
- \\/// Menus, shortcuts, and chrome tabs dispatch through the core's
1152
- \\/// exported command mapper.
1153
- \\pub fn commandMsg(name: []const u8) ?{f} {{
1154
- \\ var out_ptr: [*]const u8 = &shim_rt.channel_out_guard;
1155
- \\ var out_len: usize = 0;
1156
- \\ abi.command_msg(name.ptr, name.len, &out_ptr, &out_len);
1157
- \\ return msgFromEnvelope(shim_rt.channelEnvelopeBytes(out_ptr, out_len));
1158
- \\}}
1159
- \\
1160
- , .{ident(self.sidecar.msg.name)});
1161
- }
1162
- if (chan.frame_msg) {
1163
- try self.print(
1164
- \\
1165
- \\/// The presented-frame channel's record: canvas points plus the
1166
- \\/// frame clock in fractional milliseconds. The adapter builds it
1167
- \\/// by field name, exactly as for transpiler output.
1168
- \\pub const FrameEvent = struct {{
1169
- \\ width: f64,
1170
- \\ height: f64,
1171
- \\ timestampMs: f64,
1172
- \\ intervalMs: f64,
1173
- \\}};
1174
- \\
1175
- \\pub fn frameMsg(model: *const {f}, frame: FrameEvent) ?{f} {{
1176
- \\ _ = model;
1177
- \\ var out_ptr: [*]const u8 = &shim_rt.channel_out_guard;
1178
- \\ var out_len: usize = 0;
1179
- \\ abi.frame_msg(frame.width, frame.height, frame.timestampMs, frame.intervalMs, &out_ptr, &out_len);
1180
- \\ return msgFromEnvelope(shim_rt.channelEnvelopeBytes(out_ptr, out_len));
1181
- \\}}
1182
- \\
1183
- , .{ ident(self.sidecar.model), ident(self.sidecar.msg.name) });
1184
- }
1185
- if (chan.key_msg) {
1186
- try self.print(
1187
- \\
1188
- \\/// The key-fallback channel's record: the lowercased key name
1189
- \\/// plus the four modifier booleans.
1190
- \\pub const KeyEvent = struct {{
1191
- \\ key: []const u8,
1192
- \\ shift: bool,
1193
- \\ control: bool,
1194
- \\ alt: bool,
1195
- \\ super: bool,
1196
- \\}};
1197
- \\
1198
- \\pub fn keyMsg(key: KeyEvent) ?{f} {{
1199
- \\ var out_ptr: [*]const u8 = &shim_rt.channel_out_guard;
1200
- \\ var out_len: usize = 0;
1201
- \\ abi.key_msg(key.key.ptr, key.key.len, @intFromBool(key.shift), @intFromBool(key.control), @intFromBool(key.alt), @intFromBool(key.super), &out_ptr, &out_len);
1202
- \\ return msgFromEnvelope(shim_rt.channelEnvelopeBytes(out_ptr, out_len));
1203
- \\}}
1204
- \\
1205
- , .{ident(self.sidecar.msg.name)});
1206
- }
1207
- if (chan.pinch_msg) {
1208
- try self.print(
1209
- \\
1210
- \\pub const PinchPhase = enum(u8) {{ begin = 0, change = 1, end = 2 }};
1211
- \\
1212
- \\/// The pinch channel's record: window/view source identity, the
1213
- \\/// multiplicative magnification delta, and the view-local anchor.
1214
- \\pub const PinchEvent = struct {{
1215
- \\ windowId: f64,
1216
- \\ label: []const u8,
1217
- \\ phase: PinchPhase,
1218
- \\ scale: f64,
1219
- \\ x: f64,
1220
- \\ y: f64,
1221
- \\}};
1222
- \\
1223
- \\pub fn pinchMsg(pinch: PinchEvent) ?{f} {{
1224
- \\ var out_ptr: [*]const u8 = &shim_rt.channel_out_guard;
1225
- \\ var out_len: usize = 0;
1226
- \\ abi.pinch_msg(pinch.windowId, pinch.label.ptr, pinch.label.len, @intCast(@intFromEnum(pinch.phase)), pinch.scale, pinch.x, pinch.y, &out_ptr, &out_len);
1227
- \\ return msgFromEnvelope(shim_rt.channelEnvelopeBytes(out_ptr, out_len));
1228
- \\}}
1229
- \\
1230
- , .{ident(self.sidecar.msg.name)});
1231
- }
1232
- if (chan.drop_msg) {
1233
- try self.print(
1234
- \\
1235
- \\/// A file drop's optional view-local point.
1236
- \\pub const FileDropPoint = struct {{
1237
- \\ x: f64,
1238
- \\ y: f64,
1239
- \\}};
1240
- \\
1241
- \\/// The file-drop channel's source identity, optional point,
1242
- \\/// and byte-text path list.
1243
- \\pub const FileDropEvent = struct {{
1244
- \\ windowId: f64,
1245
- \\ viewLabel: []const u8,
1246
- \\ point: ?FileDropPoint,
1247
- \\ paths: []const []const u8,
1248
- \\}};
1249
- \\
1250
- \\pub fn dropMsg(drop: FileDropEvent) ?{f} {{
1251
- \\ const encoded = shim_rt.encodeAlloc(FileDropEvent, drop, shim_rt.frameAllocator());
1252
- \\ var out_ptr: [*]const u8 = &shim_rt.channel_out_guard;
1253
- \\ var out_len: usize = 0;
1254
- \\ abi.drop_msg(encoded.ptr, encoded.len, &out_ptr, &out_len);
1255
- \\ return msgFromEnvelope(shim_rt.channelEnvelopeBytes(out_ptr, out_len));
1256
- \\}}
1257
- \\
1258
- , .{ident(self.sidecar.msg.name)});
1259
- }
1260
-
1261
- if (chan.appearance_msg) |arm_name| {
1262
- try self.print("\n/// The arm the host fills with the structural appearance record.\npub const appearanceMsg = \"{f}\";\n", .{std.zig.fmtString(arm_name)});
1263
- }
1264
- if (chan.chrome_msg) |arm_name| {
1265
- try self.print("\n/// The arm the host fills with the structural window-chrome record.\npub const chromeMsg = \"{f}\";\n", .{std.zig.fmtString(arm_name)});
1266
- }
1267
- if (chan.env_msgs.len > 0) {
1268
- try self.raw(
1269
- \\
1270
- \\/// The launch-time environment channel: each variable present at
1271
- \\/// launch dispatches one journaled Msg on its bytes arm, right
1272
- \\/// after the boot command. The core itself never reads the
1273
- \\/// environment; replay carries the recorded values.
1274
- \\pub const envMsgs = .{
1275
- \\
1276
- );
1277
- for (chan.env_msgs) |entry| {
1278
- try self.print(" .{{ .env = \"{f}\", .msg = \"{f}\" }},\n", .{ std.zig.fmtString(entry.env), std.zig.fmtString(entry.msg) });
1279
- }
1280
- try self.raw("};\n");
1281
- }
1282
-
1283
- if (any_fn_channel) {
1284
- try self.print(
1285
- \\
1286
- \\/// Unpack a channel entry's bytes envelope — [produced u8][tag u8]
1287
- \\/// [payload…]: the header says whether a message was produced, the
1288
- \\/// wire tag picks the arm, and the payload rides the canonical
1289
- \\/// value encoding of that arm's mirror payload type. Malformed
1290
- \\/// framing panics with a teaching in shim_rt.channelEnvelope; a
1291
- \\/// tag past the declared arms panics here.
1292
- \\fn msgFromEnvelope(envelope: []const u8) ?{f} {{
1293
- \\ const header = shim_rt.channelEnvelope(envelope);
1294
- \\ if (!header.produced) return null;
1295
- \\ switch (header.tag) {{
1296
- \\
1297
- , .{ident(self.sidecar.msg.name)});
1298
- for (self.sidecar.msg.arms, 0..) |arm, tag| {
1299
- if (arm.payload == .void) {
1300
- try self.print(" {d} => {{\n shim_rt.assertVoidPayload(header.payload);\n return .{f};\n }},\n", .{ tag, ident(arm.name) });
1301
- } else {
1302
- try self.print(" {d} => return .{{ .{f} = shim_rt.decodeExact(@FieldType({f}, \"{f}\"), header.payload, shim_rt.frameAllocator()) }},\n", .{ tag, ident(arm.name), ident(self.sidecar.msg.name), std.zig.fmtString(arm.name) });
1303
- }
1304
- }
1305
- try self.raw(
1306
- \\ else => @panic("the compiled core's channel envelope carries a message tag past the declared arms — the core and its contract sidecar disagree; rebuild the app so both come from one compile"),
1307
- \\ }
1308
- \\}
1309
- \\
1310
- );
1311
- }
1312
- }
1313
-
1314
- // ----------------------------------------------------- helper glue
1315
-
1316
- fn helperPlumbing(self: *Emitter) Error!void {
1317
- if (self.sidecar.model_helpers.len == 0) return;
1318
- try self.raw(
1319
- \\
1320
- \\/// Call the compiled core's exported helper at `index` (the
1321
- \\/// sidecar's model_helpers order IS the call index) and decode the
1322
- \\/// result. An unknown index traps core-side through the panic
1323
- \\/// sink; there is no status to check. Results are read-arena
1324
- \\/// resident, the same until-next-dispatch lifetime view builds get
1325
- \\/// from transpiler output.
1326
- \\fn callHelper(comptime T: type, index: u32, args: []const u8, allocator: std.mem.Allocator) T {
1327
- \\ var out_ptr: [*]const u8 = undefined;
1328
- \\ var out_len: usize = 0;
1329
- \\ abi.helper_call(index, args.ptr, args.len, &out_ptr, &out_len);
1330
- \\ return shim_rt.decodeExact(T, out_ptr[0..out_len], allocator);
1331
- \\}
1332
- \\
1333
- );
1334
- }
1335
-
1336
- // -------------------------------------------------- snapshot glue
1337
-
1338
- fn snapshotPlumbing(self: *Emitter) Error!void {
1339
- try self.print(
1340
- \\
1341
- \\/// Borrow the compiled core's positional model-mirror bytes.
1342
- \\/// Valid until the next core ABI call.
1343
- \\pub fn modelSnapshot() []const u8 {{
1344
- \\ var snap_ptr: [*]const u8 = undefined;
1345
- \\ var snap_len: usize = 0;
1346
- \\ abi.model_snapshot(&snap_ptr, &snap_len);
1347
- \\ return snap_ptr[0..snap_len];
1348
- \\}}
1349
- \\
1350
- \\/// Borrow the tagged, length-delimited durable snapshot
1351
- \\/// (persistence snapshot format {d}). Valid until the next core
1352
- \\/// ABI call.
1353
- \\pub fn persistenceSnapshot() []const u8 {{
1354
- \\ var snap_ptr: [*]const u8 = undefined;
1355
- \\ var snap_len: usize = 0;
1356
- \\ abi.persist_snapshot(&snap_ptr, &snap_len);
1357
- \\ return snap_ptr[0..snap_len];
1358
- \\}}
1359
- \\
1360
- \\/// Decode the committed model into the shim read arena.
1361
- \\fn snapshotModel() *const {f} {{
1362
- \\ return shim_rt.decodeSnapshot({f}, modelSnapshot());
1363
- \\}}
1364
- \\
1365
- \\/// Replace the compiled core's committed model, then refresh the
1366
- \\/// read-arena mirror returned to the host.
1367
- \\pub fn restoreModel(snapshot: []const u8) *const {f} {{
1368
- \\ var out_ptr: [*]const u8 = undefined;
1369
- \\ var out_len: usize = 0;
1370
- \\ abi.restore_model(snapshot.ptr, snapshot.len, &out_ptr, &out_len);
1371
- \\ if (out_len != 0) @panic("core_shim: restore_model returned bytes; ABI version 2 requires an empty result");
1372
- \\ return snapshotModel();
1373
- \\}}
1374
- \\
1375
- \\
1376
- \\/// Run the app's pure migration hook and copy the resulting
1377
- \\/// current-format snapshot out of the core result arena. Null is
1378
- \\/// the hook's closed failure result (including no hook).
1379
- \\pub fn migrateModel(snapshot: []const u8, from_version: u64, allocator: std.mem.Allocator) ?[]u8 {{
1380
- \\ var out_ptr: [*]const u8 = undefined;
1381
- \\ var out_len: usize = 0;
1382
- \\ abi.migrate_model(snapshot.ptr, snapshot.len, @floatFromInt(from_version), &out_ptr, &out_len);
1383
- \\ if (out_len < 1 or out_ptr[0] != 1) return null;
1384
- \\ return allocator.dupe(u8, out_ptr[1..out_len]) catch null;
1385
- \\}}
1386
- \\
1387
- , .{
1388
- self.sidecar.abi.snapshot_format,
1389
- ident(self.sidecar.model),
1390
- ident(self.sidecar.model),
1391
- ident(self.sidecar.model),
1392
- });
1393
- }
1394
- };
1395
-
1396
- // ------------------------------------------------- shared analysis
1397
- //
1398
- // Both projections of the sidecar — the Zig mirror and the TypeScript
1399
- // facade — must agree on which table entries are synthesized anonymous
1400
- // records, or the two sides would declare different type surfaces for
1401
- // one contract.
1402
-
1403
- /// A table entry whose name follows the schema's synthesized pattern
1404
- /// for a reference site (`<Container>_<member>`).
1405
- ///
1406
- /// Current sidecars carry an `origin` for authored declarations and null
1407
- /// for synthesized names, so provenance decides first. Older sidecars have
1408
- /// no origins; their null entries retain the name-pattern fallback so the
1409
- /// reader remains backward compatible with that generation.
1410
- pub fn isSynthesizedRef(container: []const u8, member: []const u8, name: []const u8) bool {
1411
- if (name.len != container.len + 1 + member.len) return false;
1412
- return std.mem.startsWith(u8, name, container) and
1413
- name[container.len] == '_' and
1414
- std.mem.endsWith(u8, name, member);
1415
- }
1416
-
1417
- /// The table entries emitted inline at their single reference site:
1418
- /// pattern-matching names referenced exactly once in the whole sidecar.
1419
- pub fn inlinedTableNames(arena: std.mem.Allocator, sidecar: Sidecar) error{OutOfMemory}![]const []const u8 {
1420
- var candidates: std.ArrayListUnmanaged([]const u8) = .empty;
1421
- for (sidecar.types.structs) |entry| {
1422
- for (entry.fields) |field| {
1423
- try noteCandidate(&candidates, arena, entry.name, field.name, field.type);
1424
- }
1425
- }
1426
- for (sidecar.types.unions) |entry| {
1427
- for (entry.arms) |arm| {
1428
- try noteCandidate(&candidates, arena, entry.name, arm.name, arm.payload);
1429
- }
1430
- }
1431
- for (sidecar.msg.arms) |arm| {
1432
- switch (arm.payload) {
1433
- .record => |name| if (isSynthesizedRef(sidecar.msg.name, arm.name, name)) {
1434
- try candidates.append(arena, name);
1435
- },
1436
- else => {},
1437
- }
1438
- }
1439
- var names: std.ArrayListUnmanaged([]const u8) = .empty;
1440
- for (candidates.items) |name| {
1441
- if (referenceCount(sidecar, name) != 1) continue;
1442
- // A genuine synthesized record carries at least two fields (a
1443
- // one-field inline arm collapses to a bare payload at emission
1444
- // and a zero-field arm is void, so neither ever gets tabled).
1445
- // Inlining a smaller pattern-named record would let the facade
1446
- // compile collapse it while the mirror keeps a record — the two
1447
- // projections must never diverge, so such records stay named.
1448
- const entry = sidecar_mod.findStruct(sidecar.types, name) orelse continue;
1449
- // A declaring module proves this is an authored type even when its
1450
- // spelling happens to match the synthesized-name convention.
1451
- if (entry.origin != null) continue;
1452
- if (entry.fields.len < 2) continue;
1453
- try names.append(arena, name);
1454
- }
1455
- return names.items;
1456
- }
1457
-
1458
- fn noteCandidate(names: *std.ArrayListUnmanaged([]const u8), arena: std.mem.Allocator, container: []const u8, member: []const u8, ref: TypeRef) error{OutOfMemory}!void {
1459
- switch (ref) {
1460
- .optional => |inner| try noteCandidate(names, arena, container, member, inner.*),
1461
- .slice => |elem| try noteCandidate(names, arena, container, member, elem.*),
1462
- // VALUE references only: inlining flattens the record into its
1463
- // one site, which is a by-value layout — a node-stored payload
1464
- // keeps its named declaration (and its pointer) however its
1465
- // name is spelled.
1466
- .value => |name| if (isSynthesizedRef(container, member, name)) {
1467
- try names.append(arena, name);
1468
- },
1469
- else => {},
1470
- }
1471
- }
1472
-
1473
- /// How many reference sites name a table entry, across the whole
1474
- /// sidecar (model root, table fields and arms, msg descriptors, helper
1475
- /// signatures).
1476
- pub fn referenceCount(sidecar: Sidecar, name: []const u8) usize {
1477
- var count: usize = 0;
1478
- if (std.mem.eql(u8, sidecar.model, name)) count += 1;
1479
- for (sidecar.types.structs) |entry| {
1480
- for (entry.fields) |field| count += refNames(field.type, name);
1481
- }
1482
- for (sidecar.types.unions) |entry| {
1483
- for (entry.arms) |arm| count += refNames(arm.payload, name);
1484
- }
1485
- for (sidecar.msg.arms) |arm| {
1486
- switch (arm.payload) {
1487
- .record, .union_ref, .enum_ref => |payload_name| {
1488
- if (std.mem.eql(u8, payload_name, name)) count += 1;
1489
- },
1490
- .scalar => |ref| count += refNames(ref, name),
1491
- else => {},
1492
- }
1493
- }
1494
- for (sidecar.model_helpers) |helper| {
1495
- count += refNames(helper.returns, name);
1496
- for (helper.params) |param| count += refNames(param, name);
1497
- }
1498
- return count;
1499
- }
1500
-
1501
- /// Whether `ref` contains a by-reference (`node`) reference to `name`.
1502
- fn refNodeNames(ref: TypeRef, name: []const u8) bool {
1503
- return switch (ref) {
1504
- .optional => |inner| refNodeNames(inner.*, name),
1505
- .slice => |elem| refNodeNames(elem.*, name),
1506
- .node => |ref_name| std.mem.eql(u8, ref_name, name),
1507
- else => false,
1508
- };
1509
- }
1510
-
1511
- fn refNames(ref: TypeRef, name: []const u8) usize {
1512
- return switch (ref) {
1513
- .optional => |inner| refNames(inner.*, name),
1514
- .slice => |elem| refNames(elem.*, name),
1515
- .node, .value, .enum_ref, .union_ref => |ref_name| @intFromBool(std.mem.eql(u8, ref_name, name)),
1516
- else => 0,
1517
- };
1518
- }
1519
-
1520
- fn nameListed(names: []const []const u8, name: []const u8) bool {
1521
- for (names) |candidate| {
1522
- if (std.mem.eql(u8, candidate, name)) return true;
1523
- }
1524
- return false;
1525
- }
1526
-
1527
- /// Sidecar strings quoted inside the generated module's comments:
1528
- /// control characters would break the comment line, so they become
1529
- /// spaces (comments are provenance, never load-bearing).
1530
- fn commentText(arena: std.mem.Allocator, text: []const u8) error{OutOfMemory}![]const u8 {
1531
- const out = try arena.dupe(u8, text);
1532
- for (out) |*char| {
1533
- if (char.* < 0x20 or char.* == 0x7f) char.* = ' ';
1534
- }
1535
- // U+2028/U+2029 are line terminators to a TypeScript scanner: blank
1536
- // their UTF-8 bytes so provenance text cannot end the comment early.
1537
- var index: usize = 0;
1538
- while (index + 2 < out.len) : (index += 1) {
1539
- if (out[index] == 0xe2 and out[index + 1] == 0x80 and (out[index + 2] == 0xa8 or out[index + 2] == 0xa9)) {
1540
- out[index] = ' ';
1541
- out[index + 1] = ' ';
1542
- out[index + 2] = ' ';
1543
- }
1544
- }
1545
- return out;
1546
- }
1547
-
1548
- // -------------------------------------------------- identifier safety
1549
-
1550
- /// Emit a sidecar name as a Zig identifier, `@"..."`-quoting anything
1551
- /// that is not a plain identifier (keywords, leading digits, exotic
1552
- /// characters) — the author's spelling survives either way, and every
1553
- /// reflecting consumer reads the unquoted name.
1554
- const Ident = struct {
1555
- name: []const u8,
1556
-
1557
- pub fn format(self: Ident, writer: *std.Io.Writer) std.Io.Writer.Error!void {
1558
- if (isPlainIdentifier(self.name)) {
1559
- try writer.writeAll(self.name);
1560
- } else {
1561
- try writer.print("@\"{f}\"", .{std.zig.fmtString(self.name)});
1562
- }
1563
- }
1564
- };
1565
-
1566
- fn ident(name: []const u8) Ident {
1567
- return .{ .name = name };
1568
- }
1569
-
1570
- fn isPlainIdentifier(name: []const u8) bool {
1571
- if (name.len == 0) return false;
1572
- // `_` is the discard token, not a declarable identifier.
1573
- if (std.mem.eql(u8, name, "_")) return false;
1574
- if (name[0] >= '0' and name[0] <= '9') return false;
1575
- for (name) |char| {
1576
- const ok = (char >= 'a' and char <= 'z') or (char >= 'A' and char <= 'Z') or
1577
- (char >= '0' and char <= '9') or char == '_';
1578
- if (!ok) return false;
1579
- }
1580
- if (std.zig.Token.keywords.has(name)) return false;
1581
- // Primitive type names shadow poorly; quote them too.
1582
- if (std.zig.isPrimitive(name)) return false;
1583
- return true;
1584
- }
1585
-
1586
- // --------------------------------------------------------------- tests
1587
-
1588
11
  const testing = std.testing;
1589
12
 
1590
13
  fn emitFromJson(arena: std.mem.Allocator, json: []const u8) ![]const u8 {
@@ -1780,7 +203,7 @@ test "a u64 attestation on chrome geometry refuses at check time" {
1780
203
  // class cannot carry.
1781
204
  const source =
1782
205
  \\{
1783
- \\ "format": 1, "wire_version": 7, "abi_version": 2,
206
+ \\ "format": 1, "wire_version": 10, "abi_version": 2,
1784
207
  \\ "compiler_version": "0.0.1", "entry": "src/core.ts",
1785
208
  \\ "source_hash": "00000000c0ffee00", "build_id": "00000000b01dface", "model_fingerprint": "00000000a11ce001",
1786
209
  \\ "types": {
@@ -1963,7 +386,7 @@ test "a shared authored type spelling like a synthesized name stays a top-level
1963
386
  // first site would leave the second dangling.
1964
387
  const source =
1965
388
  \\{
1966
- \\ "format": 1, "wire_version": 7, "abi_version": 2,
389
+ \\ "format": 1, "wire_version": 10, "abi_version": 2,
1967
390
  \\ "compiler_version": "0.0.1", "entry": "src/core.ts",
1968
391
  \\ "source_hash": "00000000c0ffee00", "build_id": "00000000b01dface", "model_fingerprint": "00000000a11ce001",
1969
392
  \\ "types": {
@@ -2400,7 +823,7 @@ test "a chrome arm holding its insets by reference refuses" {
2400
823
  // node (by-reference) insets record cannot take that construction.
2401
824
  const source =
2402
825
  \\{
2403
- \\ "format": 1, "wire_version": 7, "abi_version": 2,
826
+ \\ "format": 1, "wire_version": 10, "abi_version": 2,
2404
827
  \\ "compiler_version": "0.0.1", "entry": "src/core.ts",
2405
828
  \\ "source_hash": "00000000c0ffee00", "build_id": "00000000b01dface", "model_fingerprint": "00000000a11ce001",
2406
829
  \\ "types": {
@@ -2517,14 +940,20 @@ test "a single-use authored pattern-named record stays declared by provenance" {
2517
940
  }
2518
941
 
2519
942
  test "keywords and exotic names are quoted" {
2520
- try testing.expect(!isPlainIdentifier("error"));
2521
- try testing.expect(!isPlainIdentifier("test"));
2522
- try testing.expect(!isPlainIdentifier("u8"));
2523
- try testing.expect(!isPlainIdentifier("1abc"));
2524
- try testing.expect(!isPlainIdentifier("_"));
2525
- try testing.expect(isPlainIdentifier("_x"));
2526
- try testing.expect(isPlainIdentifier("super"));
2527
- try testing.expect(isPlainIdentifier("chatScrollTop"));
943
+ var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
944
+ defer arena_state.deinit();
945
+ const arena = arena_state.allocator();
946
+ for ([_][]const u8{ "error", "test", "u8", "1abc", "_", "_x", "super", "chatScrollTop" }, 0..) |name, index| {
947
+ const source = try std.mem.replaceOwned(u8, arena, sidecar_mod.minimal_valid_json, "label", name);
948
+ const generated = try emitFromJson(arena, source);
949
+ const field = if (index < 5)
950
+ try std.fmt.allocPrint(arena, "@\"{s}\": []const u8", .{name})
951
+ else
952
+ try std.fmt.allocPrint(arena, "{s}: []const u8", .{name});
953
+ try testing.expect(std.mem.indexOf(u8, generated, field) != null);
954
+ const tree = try std.zig.Ast.parse(arena, try arena.dupeZ(u8, generated), .zig);
955
+ try testing.expectEqual(@as(usize, 0), tree.errors.len);
956
+ }
2528
957
  }
2529
958
 
2530
959
  test "an appearance channel on a wrong-shaped arm refuses with a teaching" {