@native-sdk/cli 0.7.1 → 0.8.0

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 (94) hide show
  1. package/build/app.zig +273 -65
  2. package/build/ts_run.mjs +6 -8
  3. package/build.zig +341 -361
  4. package/native-sdk.d.ts +6 -3
  5. package/package.json +13 -11
  6. package/packages/core/compile-surface/core.ts +559 -0
  7. package/packages/core/package-lock.json +375 -12
  8. package/packages/core/package.json +9 -6
  9. package/packages/core/scripts/compiler_typecheck.mjs +58 -0
  10. package/packages/core/scripts/gen_case_tables.mjs +130 -0
  11. package/packages/core/scripts/gen_declarations.mjs +97 -0
  12. package/packages/core/scripts/run_external_core_compiler.mjs +108 -0
  13. package/packages/core/scripts/stage_external_core.mjs +148 -0
  14. package/packages/core/sdk/core.d.ts +363 -0
  15. package/packages/core/sdk/events.d.ts +65 -0
  16. package/packages/core/sdk/text.d.ts +51 -0
  17. package/packages/core/src/checker.ts +3 -3
  18. package/packages/core/src/cli.ts +40 -38
  19. package/packages/core/src/contract.ts +749 -0
  20. package/packages/core/src/devhost.ts +4 -3
  21. package/packages/core/src/diagnostics.ts +5 -7
  22. package/packages/core/src/{transpile.ts → frontend.ts} +38 -50
  23. package/packages/core/src/infer.ts +1 -1
  24. package/packages/core/src/typed_ast.ts +16 -15
  25. package/packages/core/src/wyhash.ts +79 -0
  26. package/skill-data/native-ui/SKILL.md +4 -2
  27. package/skill-data/ts-core/SKILL.md +29 -29
  28. package/src/platform/macos/appkit_host.m +51 -3
  29. package/src/platform/types.zig +4 -0
  30. package/src/platform/windows/gpu_surface_renderer.cpp +2280 -0
  31. package/src/platform/windows/gpu_surface_renderer.h +86 -0
  32. package/src/platform/windows/root.zig +298 -20
  33. package/src/platform/windows/webview2_host.cpp +343 -33
  34. package/src/primitives/canvas/code.zig +50 -0
  35. package/src/primitives/canvas/code_tests.zig +174 -1
  36. package/src/primitives/canvas/frame.zig +45 -1
  37. package/src/primitives/canvas/gpu.zig +6 -0
  38. package/src/primitives/canvas/render_tests.zig +93 -0
  39. package/src/primitives/canvas/root.zig +2 -0
  40. package/src/primitives/canvas/serialization.zig +150 -21
  41. package/src/primitives/canvas/themes/geist.zig +51 -5
  42. package/src/primitives/canvas/tokens.zig +33 -4
  43. package/src/primitives/canvas/ui.zig +52 -7
  44. package/src/primitives/canvas/ui_markup.zig +38 -1
  45. package/src/primitives/canvas/ui_markup_compiled.zig +28 -0
  46. package/src/primitives/canvas/ui_markup_contract.zig +9 -0
  47. package/src/primitives/canvas/ui_markup_view.zig +28 -0
  48. package/src/primitives/canvas/ui_markup_view_tests.zig +10 -2
  49. package/src/primitives/canvas/ui_schema.zig +5 -0
  50. package/src/primitives/canvas/ui_schema_tests.zig +4 -3
  51. package/src/primitives/canvas/ui_tests.zig +14 -0
  52. package/src/primitives/canvas/widget_builtin_tests.zig +513 -13
  53. package/src/primitives/canvas/widget_invalidation.zig +11 -1
  54. package/src/primitives/canvas/widget_layout.zig +260 -21
  55. package/src/primitives/canvas/widget_metrics.zig +45 -9
  56. package/src/primitives/canvas/widget_render.zig +220 -70
  57. package/src/primitives/canvas/widget_render_controls.zig +79 -21
  58. package/src/primitives/canvas/widget_render_style.zig +52 -8
  59. package/src/primitives/canvas/widget_text_input.zig +2 -0
  60. package/src/primitives/canvas/widgets.zig +53 -2
  61. package/src/runtime/canvas_frame.zig +60 -7
  62. package/src/runtime/canvas_frame_invalidation_tests.zig +80 -0
  63. package/src/runtime/canvas_frame_packet_tests.zig +83 -9
  64. package/src/runtime/canvas_frame_patch_tests.zig +16 -0
  65. package/src/runtime/canvas_images.zig +8 -2
  66. package/src/runtime/canvas_widget_clipboard_tests.zig +1 -1
  67. package/src/runtime/canvas_widget_runtime.zig +11 -8
  68. package/src/runtime/effects.zig +6 -0
  69. package/src/runtime/effects_tests.zig +30 -0
  70. package/src/runtime/gpu_surface_events.zig +38 -7
  71. package/src/runtime/media_surface_tests.zig +1 -1
  72. package/src/runtime/runtime_event_tests.zig +74 -0
  73. package/src/runtime/ts_core_host.zig +38 -38
  74. package/src/runtime/ts_ui_app.zig +12 -12
  75. package/src/runtime/ui_app.zig +10 -8
  76. package/src/runtime/ui_app_tests.zig +67 -0
  77. package/src/runtime/view.zig +6 -0
  78. package/src/runtime/view_canvas.zig +15 -0
  79. package/src/runtime/view_widget_text.zig +6 -4
  80. package/src/runtime/window_view_runtime.zig +1 -0
  81. package/src/tooling/manifest.zig +32 -0
  82. package/src/tooling/raw_manifest.zig +4 -0
  83. package/src/tooling/templates.zig +34 -26
  84. package/src/tooling/ts_core.zig +55 -34
  85. package/src/tooling/verbs.zig +1 -1
  86. package/tools/corewire/core_abi.zig +169 -0
  87. package/tools/corewire/emit.zig +2418 -0
  88. package/tools/corewire/emit_facade.zig +3984 -0
  89. package/tools/corewire/emit_profile.zig +501 -0
  90. package/tools/corewire/main.zig +561 -0
  91. package/tools/corewire/shim_rt.zig +632 -0
  92. package/tools/corewire/sidecar.zig +2507 -0
  93. package/packages/core/rt/rt.zig +0 -1986
  94. package/packages/core/src/emitter.ts +0 -11898
package/build/app.zig CHANGED
@@ -54,9 +54,10 @@ pub const AppOptions = struct {
54
54
  };
55
55
 
56
56
  /// Which core the app tree carries. No flag and no config anywhere: the
57
- /// tree IS the truth — `src/core.ts` is a TypeScript core (transpiled at
58
- /// build time, run through generated wiring), `src/main.zig` a Zig one,
59
- /// and both at once is a teaching error naming the two files.
57
+ /// tree IS the truth — `src/core.ts` is a TypeScript core (compiled to
58
+ /// native code at build time, run through generated wiring),
59
+ /// `src/main.zig` a Zig one, and both at once is a teaching error
60
+ /// naming the two files.
60
61
  const CoreTree = enum { zig, ts, both, neither };
61
62
 
62
63
  fn detectCoreTree(b: *std.Build, app_root: []const u8) CoreTree {
@@ -73,15 +74,39 @@ fn appFileExists(b: *std.Build, app_root: []const u8, sub_path: []const u8) bool
73
74
  return true;
74
75
  }
75
76
 
77
+ /// How a TypeScript core compiles: through the external core compiler,
78
+ /// always. The frontend checks the core and emits its contract sidecar,
79
+ /// corewire projects the compile entry and profile, the exact-pinned
80
+ /// external toolchain builds the archive, and the app links corewire's
81
+ /// generated mirror over it. `.core_compiler` in app.zon (and the
82
+ /// `-Dcore-compiler` flag) accept only "external" — the value exists so
83
+ /// a stated choice stays stateable; the removed transpiled lane's
84
+ /// spelling is refused with a teaching (see resolveCoreCompiler).
85
+ const core_compiler_teaching =
86
+ "\ncore_compiler = \"transpiler\" names the removed TS-to-Zig transpiled lane (v0.7.0" ++
87
+ " removed it): a TypeScript core compiles through the external core compiler now, and" ++
88
+ " that is the default.\nDelete the setting (or spell it \"external\").\n";
89
+
76
90
  /// The staged TypeScript-core wiring: one generated directory holding the
77
- /// transpiled core (core.zig), its rt kernel, the app's markup, and the
78
- /// SDK's generated-wiring entry (ts_core_main.zig as main.zig). Built once
79
- /// per app build and shared by the exe and test modules.
91
+ /// core module (the generated mirror with its staged shim runtime), the
92
+ /// app's markup, and the SDK's generated-wiring entry (ts_core_main.zig
93
+ /// as main.zig) — plus the compiled-core archive the app module links
94
+ /// behind the mirror. Built once per app build and shared by the exe
95
+ /// and test modules.
80
96
  const TsCoreStage = struct {
81
97
  main_root: std.Build.LazyPath,
98
+ /// The compiled-core archive: the app module links it (with libc,
99
+ /// for the toolchain's runtime) beside the staged mirror.
100
+ archive: std.Build.LazyPath,
101
+ };
102
+
103
+ /// The frontend's own sources — the staleness set of every build step
104
+ /// that runs it (a frontend edit re-checks every core).
105
+ const frontend_sources = [_][]const u8{
106
+ "checker.ts", "cli.ts", "contract.ts", "diagnostics.ts", "frontend.ts", "infer.ts", "modules.ts", "ownership.ts", "typed_ast.ts", "types.ts", "wyhash.ts",
82
107
  };
83
108
 
84
- /// Whether the transpiler's TypeScript compiler (@typescript/old, the
109
+ /// Whether the frontend's TypeScript compiler (@typescript/old, the
85
110
  /// exactly pinned npm alias of the real `typescript` package) RESOLVES
86
111
  /// from the SDK's packages/core, by node's ancestor node_modules walk —
87
112
  /// at the SDK's exactly pinned VERSION. The same semantics the CLI gates
@@ -99,17 +124,13 @@ const TsCoreStage = struct {
99
124
  /// (nested under the package on global prefixes, hoisted to the project
100
125
  /// root on local ones, pnpm's sibling node_modules).
101
126
  ///
102
- /// The @typescript/typescript6 wrapper is deliberately NOT probed:
103
- /// nothing imports it at run time (typed_ast.ts bypasses its one-line
104
- /// re-export on purpose), so holding the wrapper's resolution — or the
105
- /// alias's version as seen FROM the wrapper's origin — against the pin
106
- /// can only FALSE-REJECT healthy trees: npm's own conflict shape hoists
107
- /// a consumer's conflicting `@typescript/old` at the project root while
108
- /// our exact pin lands nested under the CLI, which is precisely the copy
109
- /// runtime loads from packages/core; a consumer's own shadowing wrapper
110
- /// must not sway the verdict either. The wrapper stays a DECLARED
111
- /// dependency in both manifests — it is just not what validation vouches
112
- /// for (see the twin's doc comment).
127
+ /// A stray `@typescript/typescript6` compat wrapper in a consumer tree
128
+ /// (a former dependency of this package, or the consumer's own) is
129
+ /// deliberately NOT probed: nothing imports it at run time, and holding
130
+ /// the alias's version as seen FROM a wrapper's origin against the pin
131
+ /// can only FALSE-REJECT healthy trees — a consumer's hoisted conflicting
132
+ /// `@typescript/old` wins the walk from there while the copy runtime
133
+ /// actually loads sits correctly pinned under packages/core.
113
134
  ///
114
135
  /// Resolvable means the alias's manifest AND its entrypoint are present
115
136
  /// (see tsAliasedCompilerVersion for node's error shape and the
@@ -217,6 +238,29 @@ fn tsParseQuotedManifestValue(manifest_json: []const u8, comptime key: []const u
217
238
  return suffix;
218
239
  }
219
240
 
241
+ /// The external core compiler's entry module (scriptc's dist/main.js),
242
+ /// resolved by node's ancestor node_modules walk from the SDK's
243
+ /// packages/core — the same origin the frontend toolchain resolves from
244
+ /// (tsAliasedCompilerVersion's walk, kept in lockstep). Repo checkouts
245
+ /// install it there with `npm ci`; the npm-installed CLI carries the
246
+ /// compiler as a regular dependency, nested under the package on global
247
+ /// prefixes and hoisted to the project root on local ones.
248
+ fn tsExternalCompilerJs(b: *std.Build, dep: *std.Build.Dependency) ?[]const u8 {
249
+ const io = b.graph.io;
250
+ const sdk_root = tsSdkRoot(b.allocator, io, dep);
251
+ var dir: []const u8 = b.pathJoin(&.{ sdk_root, "packages", "core" });
252
+ while (true) {
253
+ if (!std.mem.eql(u8, std.fs.path.basename(dir), "node_modules")) {
254
+ const candidate = b.pathJoin(&.{ dir, "node_modules", "scriptc", "dist", "main.js" });
255
+ found: {
256
+ std.Io.Dir.cwd().access(io, candidate, .{}) catch break :found;
257
+ return candidate;
258
+ }
259
+ }
260
+ dir = std.fs.path.dirname(dir) orelse return null;
261
+ }
262
+ }
263
+
220
264
  /// The SDK dependency's real root, resolved the way both the toolchain
221
265
  /// check and its teaching name it.
222
266
  fn tsSdkRoot(allocator: std.mem.Allocator, io: std.Io, dep: *std.Build.Dependency) []const u8 {
@@ -224,15 +268,19 @@ fn tsSdkRoot(allocator: std.mem.Allocator, io: std.Io, dep: *std.Build.Dependenc
224
268
  return std.Io.Dir.cwd().realPathFileAlloc(io, raw_root, allocator) catch raw_root;
225
269
  }
226
270
 
227
- fn tsCoreStage(b: *std.Build, dep: *std.Build.Dependency, app_root: []const u8) TsCoreStage {
271
+ /// The TS-core preflight — the markup view, node, and the frontend
272
+ /// toolchain gate — returning the resolved node program. The frontend
273
+ /// (the subset checker and the contract-sidecar emitter) runs under
274
+ /// node; the compile itself is the external toolchain's.
275
+ fn tsCorePreflight(b: *std.Build, dep: *std.Build.Dependency, app_root: []const u8) []const u8 {
228
276
  if (!appFileExists(b, app_root, "src/app.native")) {
229
277
  @panic("\nthis app has a TypeScript core (src/core.ts) but no view: TS apps render markup," ++
230
- " so add src/app.native (the whole view tier binds the core's emitted model)\n");
278
+ " so add src/app.native (the whole view tier binds the core's model)\n");
231
279
  }
232
280
  const node = b.findProgram(&.{"node"}, &.{}) catch {
233
- @panic("\nbuilding a TypeScript app core needs node on PATH (the @native-sdk/core transpiler runs at" ++
234
- " build time; the binary it emits ships no JS runtime).\nInstall Node.js 22.15+ (on the 23 line: 23.5+)" ++
235
- " — https://nodejs.org or `brew install node` — and re-run.\n");
281
+ @panic("\nbuilding a TypeScript app core needs node on PATH (the @native-sdk/core frontend checks the" ++
282
+ " core at build time; the binary you ship carries no JS runtime).\nInstall Node.js 22.15+ (on the 23" ++
283
+ " line: 23.5+) — https://nodejs.org or `brew install node` — and re-run.\n");
236
284
  };
237
285
  switch (tsToolchainResolution(b, dep)) {
238
286
  .resolved => {},
@@ -246,7 +294,7 @@ fn tsCoreStage(b: *std.Build, dep: *std.Build.Dependency, app_root: []const u8)
246
294
  const sdk_root = tsSdkRoot(dep.builder.allocator, dep.builder.graph.io, dep);
247
295
  std.debug.print(
248
296
  \\
249
- \\error: the @native-sdk/core transpiler cannot resolve its TypeScript toolchain
297
+ \\error: the @native-sdk/core frontend cannot resolve its TypeScript toolchain
250
298
  \\(its compiler, @typescript/old). On a repo checkout, install it once with:
251
299
  \\ cd {s}/packages/core && npm ci --include=dev
252
300
  \\(An npm-installed @native-sdk/cli carries the toolchain automatically; if it
@@ -264,7 +312,7 @@ fn tsCoreStage(b: *std.Build, dep: *std.Build.Dependency, app_root: []const u8)
264
312
  // to move.
265
313
  std.debug.print(
266
314
  \\
267
- \\error: the @native-sdk/core transpiler's TypeScript compiler resolves at the
315
+ \\error: the @native-sdk/core frontend's TypeScript compiler resolves at the
268
316
  \\wrong version: @typescript/old resolves to typescript {s}, but the SDK pins
269
317
  \\npm:typescript@{s}. Another package in this tree pins a conflicting
270
318
  \\@typescript/old - align it with the SDK's pin (or remove it) and reinstall,
@@ -275,43 +323,164 @@ fn tsCoreStage(b: *std.Build, dep: *std.Build.Dependency, app_root: []const u8)
275
323
  std.process.exit(1);
276
324
  },
277
325
  }
326
+ return node;
327
+ }
278
328
 
279
- // The transpiler runs through build/ts_run.mjs, not as `node cli.ts`:
280
- // on the npm-installed layout the transpiler's .ts sources live inside
281
- // node_modules, where node refuses its builtin type stripping — the
282
- // runner strips those modules with the transpiler's own installed
283
- // TypeScript and is a pass-through on a repo checkout.
284
- const transpile = b.addSystemCommand(&.{node});
285
- transpile.addFileArg(dep.path("build/ts_run.mjs"));
286
- transpile.addFileArg(dep.path("packages/core/src/cli.ts"));
287
- transpile.addFileArg(b.path(appPath(b, app_root, "src/core.ts")));
288
- transpile.addArg("-o");
289
- const emitted_core = transpile.addOutputFileArg("core.zig");
290
- // The transpiler reads its own sources, the SDK modules, and the core's
291
- // WHOLE import graph at run time; declare them so a transpiler upgrade
292
- // or an edit to ANY module of a multi-file core re-emits it. The graph
293
- // is declared as every .ts under the app's src/ (a superset of the
294
- // reachable imports: over-approximation only re-runs the transpile,
295
- // never misses a stale input). Failure mode: the checker's NS
296
- // diagnostics stream to stderr verbatim — they are the teaching layer,
297
- // nothing wraps them.
298
- addTsDirInputs(b, dep.builder, transpile, "packages/core/sdk");
299
- addAppTsDirInputs(b, transpile, appPath(b, app_root, "src"));
300
- const transpiler_sources = [_][]const u8{
301
- "checker.ts", "cli.ts", "diagnostics.ts", "emitter.ts", "infer.ts", "modules.ts", "transpile.ts", "typed_ast.ts", "types.ts",
302
- };
303
- for (transpiler_sources) |source| {
304
- transpile.addFileInput(dep.path(b.fmt("packages/core/src/{s}", .{source})));
329
+ /// The TypeScript-core compile lane: the frontend checks the core and
330
+ /// emits its contract sidecar, corewire projects the generated compile
331
+ /// entry and library-mode profile, the stager assembles the compile
332
+ /// tree (author sources with the mechanical staging transforms, the
333
+ /// staged SDK modules, the static compile surface), the exact-pinned
334
+ /// external toolchain builds the archive AND co-emits the archive's own
335
+ /// contract sidecar, and corewire generates the mirror module from THAT
336
+ /// document — so the boot identity fence always pairs the mirror with
337
+ /// its own compile. The staged module directory carries core.zig (the
338
+ /// mirror) + its staged runtime + app.native + main.zig, so the
339
+ /// generated wiring imports one fixed shape.
340
+ fn tsCoreStage(b: *std.Build, dep: *std.Build.Dependency, app_root: []const u8, app_name: []const u8) TsCoreStage {
341
+ const node = tsCorePreflight(b, dep, app_root);
342
+
343
+ // The frontend, in check-only mode: the subset checker and the
344
+ // contract sidecar, no emission. Every check-time teaching gates the
345
+ // compile here — the checker's NS diagnostics stream to stderr
346
+ // verbatim; nothing wraps them. The frontend runs through
347
+ // build/ts_run.mjs, not as `node cli.ts`: on the npm-installed
348
+ // layout the frontend's .ts sources live inside node_modules, where
349
+ // node refuses its builtin type stripping — the runner strips those
350
+ // modules with the frontend's own installed TypeScript and is a
351
+ // pass-through on a repo checkout. The frontend reads its own
352
+ // sources, the SDK modules, and the core's WHOLE import graph at
353
+ // run time; declare them all so an edit to ANY module of a
354
+ // multi-file core re-checks it (every .ts under src/ is a superset
355
+ // of the reachable imports: over-approximation only re-runs the
356
+ // check, never misses a stale input).
357
+ const check = b.addSystemCommand(&.{node});
358
+ check.addFileArg(dep.path("build/ts_run.mjs"));
359
+ check.addFileArg(dep.path("packages/core/src/cli.ts"));
360
+ check.addFileArg(b.path(appPath(b, app_root, "src/core.ts")));
361
+ check.addArg("--contract");
362
+ const contract = check.addOutputFileArg("core.contract.json");
363
+ // The document's entry spelling is app-relative (the sidecar/facade
364
+ // contract carries no machine paths).
365
+ check.addArgs(&.{ "--contract-entry", "src/core.ts" });
366
+ addTsDirInputs(b, dep.builder, check, "packages/core/sdk");
367
+ addAppTsDirInputs(b, check, appPath(b, app_root, "src"));
368
+ for (frontend_sources) |source| {
369
+ check.addFileInput(dep.path(b.fmt("packages/core/src/{s}", .{source})));
305
370
  }
306
371
 
307
- // The wiring imports core.zig and app.native relatively; the emitted
308
- // core imports rt.zig relatively: stage all four into one directory.
372
+ // corewire, compiled from the SDK dependency for the build host: one
373
+ // invocation projects the generated compile entry and its profile,
374
+ // so the profile's entry spelling and the facade file can never skew.
375
+ const corewire_exe = corewireExe(b, dep);
376
+ const project = b.addRunArtifact(corewire_exe);
377
+ project.addArg("--sidecar");
378
+ project.addFileArg(contract);
379
+ project.addArg("--facade");
380
+ const facade = project.addOutputFileArg("core_facade.ts");
381
+ project.addArg("--profile");
382
+ const profile = project.addOutputFileArg("core_profile.json");
383
+
384
+ // The compile stage: author sources + staged SDK + static surface +
385
+ // generated entry/profile, one scratch tree.
386
+ const stage_run = b.addSystemCommand(&.{node});
387
+ stage_run.addFileArg(dep.path("packages/core/scripts/stage_external_core.mjs"));
388
+ stage_run.addArg("--src");
389
+ stage_run.addDirectoryArg(b.path(appPath(b, app_root, "src")));
390
+ stage_run.addArg("--sdk");
391
+ stage_run.addDirectoryArg(dep.path("packages/core/sdk"));
392
+ stage_run.addArg("--static");
393
+ stage_run.addFileArg(dep.path("packages/core/compile-surface/core.ts"));
394
+ stage_run.addArg("--facade");
395
+ stage_run.addFileArg(facade);
396
+ stage_run.addArg("--profile");
397
+ stage_run.addFileArg(profile);
398
+ stage_run.addArg("--out");
399
+ const stage_dir = stage_run.addOutputDirectoryArg("stage");
400
+
401
+ // The external compile itself: driver-verified against the SDK's
402
+ // exact pin, archive normalized, the co-emitted sidecar captured.
403
+ const symbol_name = externalCoreSymbolName(b, app_name);
404
+ const compile = b.addSystemCommand(&.{node});
405
+ compile.addFileArg(dep.path("packages/core/scripts/run_external_core_compiler.mjs"));
406
+ compile.addArg("--stage");
407
+ compile.addDirectoryArg(stage_dir);
408
+ compile.addArgs(&.{ "--name", symbol_name });
409
+ compile.addArg("--manifest");
410
+ compile.addFileArg(dep.path("packages/core/package.json"));
411
+ compile.addArg("--out-archive");
412
+ const archive = compile.addOutputFileArg(b.fmt("lib{s}.a", .{symbol_name}));
413
+ compile.addArg("--out-sidecar");
414
+ const compiled_sidecar = compile.addOutputFileArg("core.contract.json");
415
+ if (b.graph.environ_map.get("NATIVE_SDK_CORE_COMPILER")) |override| {
416
+ // The development override: point at any toolchain command; the
417
+ // driver still refuses a release other than the SDK's pin.
418
+ compile.addArgs(&.{ "--compiler", override });
419
+ } else {
420
+ const compiler_js = tsExternalCompilerJs(b, dep) orelse {
421
+ const sdk_root = tsSdkRoot(dep.builder.allocator, dep.builder.graph.io, dep);
422
+ std.debug.print(
423
+ \\
424
+ \\error: the external core compiler is not installed (TypeScript cores compile
425
+ \\through it). It ships as an exact-pinned dependency of the SDK's packages/core
426
+ \\— install it once with:
427
+ \\ cd {s}/packages/core && npm ci
428
+ \\(or point NATIVE_SDK_CORE_COMPILER at the pinned release's command; an
429
+ \\npm-installed @native-sdk/cli carries the compiler automatically — if it is
430
+ \\missing there, the install is broken: reinstall @native-sdk/cli).
431
+ \\
432
+ \\
433
+ , .{sdk_root});
434
+ std.process.exit(1);
435
+ };
436
+ compile.addArg("--compiler-js");
437
+ compile.addFileArg(.{ .cwd_relative = compiler_js });
438
+ }
439
+
440
+ // The mirror, generated from the archive's OWN co-emitted contract.
441
+ const mirror = b.addRunArtifact(corewire_exe);
442
+ mirror.addArg("--sidecar");
443
+ mirror.addFileArg(compiled_sidecar);
444
+ mirror.addArg("--out");
445
+ const shim = mirror.addOutputFileArg("core_shim.zig");
446
+
447
+ // The staged module directory: the mirror is the app's core.zig,
448
+ // and it imports its staged shim runtime relatively.
309
449
  const staged = b.addWriteFiles();
310
- _ = staged.addCopyFile(emitted_core, "core.zig");
311
- _ = staged.addCopyFile(dep.path("packages/core/rt/rt.zig"), "rt.zig");
450
+ _ = staged.addCopyFile(shim, "core.zig");
451
+ _ = staged.addCopyFile(dep.path("tools/corewire/shim_rt.zig"), "shim_rt.zig");
452
+ _ = staged.addCopyFile(dep.path("tools/corewire/core_abi.zig"), "core_abi.zig");
312
453
  _ = staged.addCopyFile(b.path(appPath(b, app_root, "src/app.native")), "app.native");
313
454
  const main_root = staged.addCopyFile(dep.path("src/app_runner/ts_core_main.zig"), "main.zig");
314
- return .{ .main_root = main_root };
455
+ return .{ .main_root = main_root, .archive = archive };
456
+ }
457
+
458
+ /// corewire (the contract-sidecar shim generator), compiled from the SDK
459
+ /// dependency's sources for the build host.
460
+ fn corewireExe(b: *std.Build, dep: *std.Build.Dependency) *std.Build.Step.Compile {
461
+ const mod = b.createModule(.{
462
+ .root_source_file = dep.path("tools/corewire/main.zig"),
463
+ .target = b.graph.host,
464
+ .optimize = .Debug,
465
+ });
466
+ return b.addExecutable(.{
467
+ .name = "corewire",
468
+ .root_module = mod,
469
+ .use_llvm = useLlvmWorkaround(b.graph.host),
470
+ });
471
+ }
472
+
473
+ /// The archive's symbol-safe stem (`<app>_core` with every non-identifier
474
+ /// byte folded to '_'), the -o name the external compile builds under.
475
+ fn externalCoreSymbolName(b: *std.Build, app_name: []const u8) []const u8 {
476
+ const stem = b.fmt("{s}_core", .{app_name});
477
+ const sanitized = b.dupe(stem);
478
+ for (sanitized) |*char| {
479
+ const ok = (char.* >= 'a' and char.* <= 'z') or (char.* >= 'A' and char.* <= 'Z') or
480
+ (char.* >= '0' and char.* <= '9') or char.* == '_';
481
+ if (!ok) char.* = '_';
482
+ }
483
+ return sanitized;
315
484
  }
316
485
 
317
486
  /// Declare every .ts file in an SDK-relative directory as a file input of
@@ -502,18 +671,37 @@ pub fn addAppArtifacts(b: *std.Build, dep: *std.Build.Dependency, app_options: A
502
671
  " src/main.zig,\nor keep src/main.zig and delete src/core.ts. (Other Zig files under" ++
503
672
  " src/ are fine either way.)\n");
504
673
  }
505
- const ts_stage: ?TsCoreStage = if (core_tree == .ts) tsCoreStage(b, dep, app_options.app_root) else null;
674
+ const app_config = appManifestBuildConfig(b, app_options.app_root);
675
+ // The core-compiler setting names the one lane there is; the flag
676
+ // overrides app.zon's `.core_compiler` and both exist so a stated
677
+ // choice stays stateable (and so the removed lane's spelling teaches
678
+ // instead of failing opaquely).
679
+ if (b.option([]const u8, "core-compiler", "How a TypeScript core compiles: external (the default and only lane)")) |flag| {
680
+ if (std.mem.eql(u8, flag, "transpiler")) @panic(core_compiler_teaching);
681
+ if (!std.mem.eql(u8, flag, "external")) @panic("\n-Dcore-compiler must be \"external\" (the default and only lane)\n");
682
+ if (core_tree != .ts) {
683
+ @panic("\n-Dcore-compiler applies to TypeScript cores (src/core.ts) only; this app has" ++
684
+ " a Zig core.\nDrop the flag or port the core to TypeScript.\n");
685
+ }
686
+ }
687
+ // Mobile targets are taught BEFORE lane selection: TypeScript cores
688
+ // are desktop-only until the external core toolchain grows mobile
689
+ // targets; Zig/markup cores stay fully supported on mobile.
690
+ if (core_tree == .ts and (target.result.os.tag == .ios or target.result.abi.isAndroid())) {
691
+ @panic("\nTypeScript app cores are desktop-only today: the external core compiler does not" ++
692
+ " target mobile yet.\nBuild for a desktop target, or port the core to a Zig" ++
693
+ " `mobileOptions` app — Zig and markup cores are fully supported on mobile.\n");
694
+ }
695
+ const ts_stage: ?TsCoreStage = if (core_tree == .ts)
696
+ tsCoreStage(b, dep, app_options.app_root, app_options.name)
697
+ else
698
+ null;
506
699
 
507
700
  // Mobile targets get the embed static library as a `lib` step: the
508
701
  // artifact the toolkit-owned iOS host (and any hand-written shim)
509
702
  // links, so `native dev|package --target ios` works against every
510
703
  // standard app build — generated graph or ejected — with nothing but
511
704
  // `-Dtarget`. Desktop targets keep the step absent.
512
- if (ts_stage != null and (target.result.os.tag == .ios or target.result.abi.isAndroid())) {
513
- @panic("\nTypeScript app cores build desktop apps today; the mobile embed library for TS" ++
514
- " cores lands with the mobile host tier.\nBuild for a desktop target, or port the core" ++
515
- " to a Zig `mobileOptions` app for mobile.\n");
516
- }
517
705
  if (target.result.os.tag == .ios or target.result.abi.isAndroid()) {
518
706
  addMobileLibWithTarget(b, dep, target, optimize, .{
519
707
  .name = app_options.name,
@@ -542,7 +730,6 @@ pub fn addAppArtifacts(b: *std.Build, dep: *std.Build.Dependency, app_options: A
542
730
  if (selected_platform == .windows and target.result.os.tag != .windows) {
543
731
  @panic("-Dplatform=windows requires a Windows target");
544
732
  }
545
- const app_config = appManifestBuildConfig(b, app_options.app_root);
546
733
  const web_engine = web_engine_override orelse app_config.web_engine;
547
734
  const cef_dir = cef_dir_override orelse defaultCefDir(selected_platform, app_config.cef_dir);
548
735
  const cef_auto_install = cef_auto_install_override orelse app_config.cef_auto_install;
@@ -789,8 +976,8 @@ fn appModule(b: *std.Build, dep: *std.Build.Dependency, target: std.Build.Resolv
789
976
 
790
977
  const app_mod = if (ts_stage) |stage|
791
978
  // TypeScript core: the app module roots at the staged generated
792
- // wiring (ts_core_main.zig beside the transpiled core.zig, its rt
793
- // kernel, and the app's markup).
979
+ // wiring (ts_core_main.zig beside the mirror core.zig, its shim
980
+ // runtime, and the app's markup).
794
981
  b.createModule(.{
795
982
  .root_source_file = stage.main_root,
796
983
  .target = target,
@@ -804,6 +991,12 @@ fn appModule(b: *std.Build, dep: *std.Build.Dependency, target: std.Build.Resolv
804
991
  // The wiring derives scene/identity/security from app.zon itself.
805
992
  app_mod.addImport("app_manifest_zon", manifest_mod);
806
993
  }
994
+ if (ts_stage) |stage| {
995
+ // The compiled-core archive links behind the staged mirror; the
996
+ // toolchain's runtime needs libc.
997
+ app_mod.link_libc = true;
998
+ app_mod.addObjectFile(stage.archive);
999
+ }
807
1000
  return app_mod;
808
1001
  }
809
1002
 
@@ -1040,6 +1233,7 @@ fn linkPlatform(b: *std.Build, dep: *std.Build.Dependency, target: std.Build.Res
1040
1233
  // fails the compile by design if it cannot be found.
1041
1234
  app_mod.addIncludePath(dep.path("third_party/webview2/include"));
1042
1235
  app_mod.addCSourceFile(.{ .file = dep.path("src/platform/windows/webview2_host.cpp"), .flags = &.{"-std=c++17"} });
1236
+ app_mod.addCSourceFile(.{ .file = dep.path("src/platform/windows/gpu_surface_renderer.cpp"), .flags = &.{"-std=c++17"} });
1043
1237
  // WebView2Loader.dll rides next to the installed app
1044
1238
  // executable: the host loads it at runtime to discover
1045
1239
  // the machine's WebView2 runtime. Canvas apps never
@@ -1064,6 +1258,7 @@ fn linkPlatform(b: *std.Build, dep: *std.Build.Dependency, target: std.Build.Res
1064
1258
  // WebViewNotFound the moment an app actually uses a
1065
1259
  // WebView.
1066
1260
  app_mod.addCSourceFile(.{ .file = dep.path("src/platform/windows/webview2_host.cpp"), .flags = &.{ "-std=c++17", "-DNATIVE_SDK_ALLOW_WEBVIEW2_STUB" } });
1261
+ app_mod.addCSourceFile(.{ .file = dep.path("src/platform/windows/gpu_surface_renderer.cpp"), .flags = &.{"-std=c++17"} });
1067
1262
  },
1068
1263
  .chromium => {
1069
1264
  const cef_check = addCefCheck(b, target, cef_dir);
@@ -1075,6 +1270,7 @@ fn linkPlatform(b: *std.Build, dep: *std.Build.Dependency, target: std.Build.Res
1075
1270
  const include_arg = b.fmt("-I{s}", .{cef_dir});
1076
1271
  const define_arg = b.fmt("-DNATIVE_SDK_CEF_DIR=\"{s}\"", .{cef_dir});
1077
1272
  app_mod.addCSourceFile(.{ .file = dep.path("src/platform/windows/cef_host.cpp"), .flags = &.{ "-std=c++17", include_arg, define_arg } });
1273
+ app_mod.addCSourceFile(.{ .file = dep.path("src/platform/windows/gpu_surface_renderer.cpp"), .flags = &.{"-std=c++17"} });
1078
1274
  app_mod.addObjectFile(b.path(b.fmt("{s}/libcef_dll_wrapper/libcef_dll_wrapper.lib", .{cef_dir})));
1079
1275
  app_mod.addLibraryPath(b.path(b.fmt("{s}/Release", .{cef_dir})));
1080
1276
  },
@@ -1083,6 +1279,11 @@ fn linkPlatform(b: *std.Build, dep: *std.Build.Dependency, target: std.Build.Res
1083
1279
  app_mod.linkSystemLibrary("c++", .{});
1084
1280
  app_mod.linkSystemLibrary("user32", .{});
1085
1281
  app_mod.linkSystemLibrary("gdi32", .{});
1282
+ // Retained gpu_surface packets are composited into a hardware
1283
+ // Direct2D target; DirectWrite draws the engine-measured text
1284
+ // runs (including registered in-memory fonts) on that target.
1285
+ app_mod.linkSystemLibrary("d2d1", .{});
1286
+ app_mod.linkSystemLibrary("dwrite", .{});
1086
1287
  app_mod.linkSystemLibrary("imm32", .{});
1087
1288
  app_mod.linkSystemLibrary("comctl32", .{});
1088
1289
  app_mod.linkSystemLibrary("ole32", .{});
@@ -1198,6 +1399,7 @@ const InferenceManifest = struct {
1198
1399
  capabilities: []const []const u8 = &.{},
1199
1400
  web_engine: []const u8 = "system",
1200
1401
  webview_layer: []const u8 = "auto",
1402
+ core_compiler: []const u8 = "external",
1201
1403
  cef: struct {
1202
1404
  dir: []const u8 = "third_party/cef/macos",
1203
1405
  auto_install: bool = false,
@@ -1238,6 +1440,12 @@ fn appManifestBuildConfig(b: *std.Build, app_root: []const u8) AppManifestBuildC
1238
1440
  const source_z = b.allocator.dupeZ(u8, source) catch return fallback;
1239
1441
  @setEvalBranchQuota(2000);
1240
1442
  const raw = std.zon.parse.fromSliceAlloc(InferenceManifest, b.allocator, source_z, null, .{ .ignore_unknown_fields = true }) catch return fallback;
1443
+ // `.core_compiler` names the one lane there is; validated here so
1444
+ // the removed transpiled lane's spelling teaches at configure time.
1445
+ if (!std.mem.eql(u8, raw.core_compiler, "external")) {
1446
+ if (std.mem.eql(u8, raw.core_compiler, "transpiler")) @panic(core_compiler_teaching);
1447
+ @panic("\napp.zon .core_compiler must be \"external\" (the default and only lane)\n");
1448
+ }
1241
1449
  return .{
1242
1450
  .web_engine = web_layer_contract.parseWebEngine(raw.web_engine) orelse .system,
1243
1451
  .cef_dir = raw.cef.dir,
package/build/ts_run.mjs CHANGED
@@ -61,15 +61,13 @@ if (typeof module.registerHooks !== 'function') {
61
61
  load(url, context, nextLoad) {
62
62
  if (url.startsWith('file:') && url.endsWith('.ts')) {
63
63
  const filePath = fileURLToPath(url);
64
- // The transpiler's own pinned compiler, resolved from the target
64
+ // The frontend's own pinned compiler, resolved from the target
65
65
  // module's location (packages/core/node_modules after the taught
66
- // `npm ci`, or the dependency npm installed beside the CLI). The
67
- // ALIAS is required directly — not the @typescript/typescript6
68
- // wrapper — because the wrapper's re-export resolves
69
- // "@typescript/old" from the WRAPPER's own location, where a
70
- // consumer tree's conflicting hoisted copy would win nearest-wins
71
- // over our exact pin; resolving from the target finds our own
72
- // nested/hoisted pin first (same reasoning as typed_ast.ts).
66
+ // `npm ci`, or the dependency npm installed beside the CLI):
67
+ // resolving from the target finds our own nested/hoisted exact
68
+ // pin first, so a consumer tree's conflicting hoisted typescript
69
+ // never wins nearest-wins over it (same reasoning as
70
+ // typed_ast.ts).
73
71
  if (ts === null) {
74
72
  try {
75
73
  ts = createRequire(targetPath)('@typescript/old');