@native-sdk/cli 0.4.4 → 0.5.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 (83) hide show
  1. package/build/app.zig +187 -6
  2. package/build/ts_run.mjs +63 -0
  3. package/build.zig +213 -1
  4. package/build.zig.zon +1 -0
  5. package/package.json +15 -10
  6. package/packages/core/package-lock.json +43 -0
  7. package/packages/core/package.json +37 -0
  8. package/packages/core/rt/rt.zig +1579 -0
  9. package/packages/core/sdk/bytes_text_methods.d.ts +88 -0
  10. package/packages/core/sdk/core.ts +694 -0
  11. package/packages/core/sdk/events.ts +137 -0
  12. package/packages/core/sdk/text.ts +519 -0
  13. package/packages/core/src/checker.ts +1869 -0
  14. package/packages/core/src/cli.ts +68 -0
  15. package/packages/core/src/devhost.ts +291 -0
  16. package/packages/core/src/devhost_resolver.mjs +22 -0
  17. package/packages/core/src/diagnostics.ts +417 -0
  18. package/packages/core/src/emitter.ts +8815 -0
  19. package/packages/core/src/infer.ts +1515 -0
  20. package/packages/core/src/modules.ts +173 -0
  21. package/packages/core/src/ownership.ts +740 -0
  22. package/packages/core/src/text_polyfill.ts +337 -0
  23. package/packages/core/src/text_tables.ts +408 -0
  24. package/packages/core/src/transpile.ts +146 -0
  25. package/packages/core/src/typed_ast.ts +536 -0
  26. package/packages/core/src/types.ts +815 -0
  27. package/skill-data/native-ui/SKILL.md +14 -0
  28. package/skill-data/ts-core/SKILL.md +493 -0
  29. package/skill-data/zig/SKILL.md +10 -0
  30. package/src/app_runner/root.zig +24 -0
  31. package/src/app_runner/ts_core_main.zig +231 -0
  32. package/src/primitives/app_manifest/comptime_scene.zig +341 -0
  33. package/src/primitives/app_manifest/root.zig +5 -0
  34. package/src/primitives/canvas/commands.zig +14 -7
  35. package/src/primitives/canvas/lazy_tls.zig +73 -0
  36. package/src/primitives/canvas/render_generic_resources.zig +22 -15
  37. package/src/primitives/canvas/root.zig +6 -0
  38. package/src/primitives/canvas/tests.zig +1 -0
  39. package/src/primitives/canvas/text_atlas.zig +40 -32
  40. package/src/primitives/canvas/text_layout_cache.zig +29 -22
  41. package/src/primitives/canvas/text_measure_cache.zig +56 -41
  42. package/src/primitives/canvas/text_spans.zig +39 -26
  43. package/src/primitives/canvas/tokens.zig +54 -0
  44. package/src/primitives/canvas/ui.zig +125 -0
  45. package/src/primitives/canvas/ui_markup.zig +22 -3
  46. package/src/primitives/canvas/ui_markup_compiled.zig +97 -7
  47. package/src/primitives/canvas/ui_markup_compiled_tests.zig +275 -0
  48. package/src/primitives/canvas/ui_markup_contract.zig +68 -14
  49. package/src/primitives/canvas/ui_markup_contract_tests.zig +257 -2
  50. package/src/primitives/canvas/ui_markup_reflect.zig +214 -0
  51. package/src/primitives/canvas/ui_markup_view.zig +105 -1
  52. package/src/primitives/canvas/ui_markup_view_tests.zig +336 -0
  53. package/src/primitives/canvas/ui_schema.zig +8 -0
  54. package/src/primitives/canvas/ui_schema_tests.zig +5 -4
  55. package/src/primitives/canvas/widget_invalidation.zig +14 -7
  56. package/src/primitives/canvas/widget_render.zig +25 -15
  57. package/src/primitives/canvas/widget_render_controls.zig +15 -9
  58. package/src/primitives/canvas/widget_runtime_tests.zig +44 -0
  59. package/src/root.zig +7 -0
  60. package/src/runtime/canvas_frame.zig +57 -36
  61. package/src/runtime/canvas_images.zig +9 -3
  62. package/src/runtime/canvas_widget_control_tests.zig +5 -6
  63. package/src/runtime/canvas_widget_runtime.zig +7 -4
  64. package/src/runtime/effects.zig +490 -2
  65. package/src/runtime/effects_host_tests.zig +594 -0
  66. package/src/runtime/root.zig +18 -0
  67. package/src/runtime/session_replay.zig +7 -1
  68. package/src/runtime/tests.zig +2 -0
  69. package/src/runtime/ts_core_host.zig +1505 -0
  70. package/src/runtime/ts_core_host_tests.zig +1408 -0
  71. package/src/runtime/ts_ui_app.zig +469 -0
  72. package/src/runtime/ui_app.zig +58 -0
  73. package/src/runtime/view_canvas.zig +24 -17
  74. package/src/runtime/view_widget_tree.zig +2 -2
  75. package/src/tooling/codesign.zig +212 -105
  76. package/src/tooling/doctor.zig +78 -0
  77. package/src/tooling/manifest.zig +47 -0
  78. package/src/tooling/package.zig +124 -12
  79. package/src/tooling/raw_manifest.zig +15 -0
  80. package/src/tooling/root.zig +1 -0
  81. package/src/tooling/templates.zig +683 -28
  82. package/src/tooling/ts_core.zig +486 -0
  83. package/src/tooling/verbs.zig +17 -0
@@ -0,0 +1,337 @@
1
+ // The node-side half of the byte-text method surface: install the everyday
2
+ // string methods on Uint8Array.prototype with EXACTLY the semantics rt.zig's
3
+ // text helpers give the native build — same generated case tables
4
+ // (text_tables.ts, the mirror of rt.zig's), same strict UTF-8 walk, same
5
+ // invalid-byte passthrough, same aliasing (trim returns views, an at-length
6
+ // pad returns the receiver). The devhost installs this before importing a
7
+ // core, and the run-fidelity suite proves node ≡ native over the whole
8
+ // surface.
9
+ //
10
+ // This is HARNESS code, not subset code: it runs under node only (the
11
+ // native build lowers the same method calls onto rt.zig directly).
12
+
13
+ import { SIMPLE_UPPER_RANGES, SIMPLE_LOWER_RANGES } from "./text_tables.ts";
14
+
15
+ /// Simple case mapping of one code point over the flat quadruple table
16
+ /// [lo, count, stride, delta] — binary search, unmapped maps to itself
17
+ /// (the mirror of rt.zig's caseMapLookup).
18
+ function caseMapLookup(ranges: readonly number[], cp: number): number {
19
+ let lo = 0;
20
+ let hi = ranges.length / 4;
21
+ while (lo < hi) {
22
+ const mid = lo + ((hi - lo) >> 1);
23
+ const rLo = ranges[mid * 4];
24
+ const count = ranges[mid * 4 + 1];
25
+ const stride = ranges[mid * 4 + 2];
26
+ const last = rLo + (count - 1) * stride;
27
+ if (cp < rLo) {
28
+ hi = mid;
29
+ } else if (cp > last) {
30
+ lo = mid + 1;
31
+ } else {
32
+ if ((cp - rLo) % stride === 0) return cp + ranges[mid * 4 + 3];
33
+ return cp;
34
+ }
35
+ }
36
+ return cp;
37
+ }
38
+
39
+ interface Utf8Step {
40
+ readonly cp: number;
41
+ readonly len: number;
42
+ readonly valid: boolean;
43
+ }
44
+
45
+ const INVALID: Utf8Step = { cp: 0, len: 1, valid: false };
46
+
47
+ /// One strict UTF-8 decode step (RFC 3629 well-formed: no overlongs, no
48
+ /// surrogates, nothing above U+10FFFF) — the mirror of rt.zig's
49
+ /// utf8DecodeAt. Invalid means: pass bytes[i] through, advance one byte.
50
+ function utf8DecodeAt(bytes: Uint8Array, i: number): Utf8Step {
51
+ const b0 = bytes[i];
52
+ if (b0 < 0x80) return { cp: b0, len: 1, valid: true };
53
+ if (b0 < 0xc2) return INVALID;
54
+ if (b0 < 0xe0) {
55
+ if (i + 1 >= bytes.length) return INVALID;
56
+ const b1 = bytes[i + 1];
57
+ if ((b1 & 0xc0) !== 0x80) return INVALID;
58
+ return { cp: ((b0 & 0x1f) << 6) | (b1 & 0x3f), len: 2, valid: true };
59
+ }
60
+ if (b0 < 0xf0) {
61
+ if (i + 2 >= bytes.length) return INVALID;
62
+ const b1 = bytes[i + 1];
63
+ const b2 = bytes[i + 2];
64
+ if ((b1 & 0xc0) !== 0x80 || (b2 & 0xc0) !== 0x80) return INVALID;
65
+ if (b0 === 0xe0 && b1 < 0xa0) return INVALID; // overlong
66
+ if (b0 === 0xed && b1 > 0x9f) return INVALID; // surrogate
67
+ return { cp: ((b0 & 0x0f) << 12) | ((b1 & 0x3f) << 6) | (b2 & 0x3f), len: 3, valid: true };
68
+ }
69
+ if (b0 < 0xf5) {
70
+ if (i + 3 >= bytes.length) return INVALID;
71
+ const b1 = bytes[i + 1];
72
+ const b2 = bytes[i + 2];
73
+ const b3 = bytes[i + 3];
74
+ if ((b1 & 0xc0) !== 0x80 || (b2 & 0xc0) !== 0x80 || (b3 & 0xc0) !== 0x80) return INVALID;
75
+ if (b0 === 0xf0 && b1 < 0x90) return INVALID; // overlong
76
+ if (b0 === 0xf4 && b1 > 0x8f) return INVALID; // above U+10FFFF
77
+ return {
78
+ cp: ((b0 & 0x07) << 18) | ((b1 & 0x3f) << 12) | ((b2 & 0x3f) << 6) | (b3 & 0x3f),
79
+ len: 4,
80
+ valid: true,
81
+ };
82
+ }
83
+ return INVALID;
84
+ }
85
+
86
+ /// Decode the well-formed sequence ENDING at `end` (exclusive) — the
87
+ /// backward walk for trimEnd (rt.zig's utf8DecodeBefore).
88
+ function utf8DecodeBefore(bytes: Uint8Array, end: number): Utf8Step {
89
+ for (let back = 1; back <= 4 && back <= end; back++) {
90
+ const b = bytes[end - back];
91
+ if ((b & 0xc0) !== 0x80) {
92
+ const step = utf8DecodeAt(bytes, end - back);
93
+ if (step.valid && step.len === back) return step;
94
+ return INVALID;
95
+ }
96
+ }
97
+ return INVALID;
98
+ }
99
+
100
+ function utf8EncodedLen(cp: number): number {
101
+ if (cp < 0x80) return 1;
102
+ if (cp < 0x800) return 2;
103
+ if (cp < 0x10000) return 3;
104
+ return 4;
105
+ }
106
+
107
+ function utf8EncodeInto(out: Uint8Array, at: number, cp: number): number {
108
+ if (cp < 0x80) {
109
+ out[at] = cp;
110
+ return 1;
111
+ }
112
+ if (cp < 0x800) {
113
+ out[at] = 0xc0 | (cp >> 6);
114
+ out[at + 1] = 0x80 | (cp & 0x3f);
115
+ return 2;
116
+ }
117
+ if (cp < 0x10000) {
118
+ out[at] = 0xe0 | (cp >> 12);
119
+ out[at + 1] = 0x80 | ((cp >> 6) & 0x3f);
120
+ out[at + 2] = 0x80 | (cp & 0x3f);
121
+ return 3;
122
+ }
123
+ out[at] = 0xf0 | (cp >> 18);
124
+ out[at + 1] = 0x80 | ((cp >> 12) & 0x3f);
125
+ out[at + 2] = 0x80 | ((cp >> 6) & 0x3f);
126
+ out[at + 3] = 0x80 | (cp & 0x3f);
127
+ return 4;
128
+ }
129
+
130
+ /// The JS WhiteSpace + LineTerminator set (what String.prototype.trim
131
+ /// strips) — the mirror of rt.zig's isJsWhitespace.
132
+ function isJsWhitespace(cp: number): boolean {
133
+ return (
134
+ (cp >= 0x09 && cp <= 0x0d) ||
135
+ cp === 0x20 ||
136
+ cp === 0xa0 ||
137
+ cp === 0x1680 ||
138
+ (cp >= 0x2000 && cp <= 0x200a) ||
139
+ cp === 0x2028 ||
140
+ cp === 0x2029 ||
141
+ cp === 0x202f ||
142
+ cp === 0x205f ||
143
+ cp === 0x3000 ||
144
+ cp === 0xfeff
145
+ );
146
+ }
147
+
148
+ function caseMap(bytes: Uint8Array, ranges: readonly number[]): Uint8Array {
149
+ // Exact-size two-pass, like rt.zig's textCaseMap.
150
+ let outLen = 0;
151
+ for (let i = 0; i < bytes.length; ) {
152
+ const step = utf8DecodeAt(bytes, i);
153
+ if (!step.valid) {
154
+ outLen += 1;
155
+ i += 1;
156
+ continue;
157
+ }
158
+ outLen += utf8EncodedLen(caseMapLookup(ranges, step.cp));
159
+ i += step.len;
160
+ }
161
+ const out = new Uint8Array(outLen);
162
+ let at = 0;
163
+ for (let i = 0; i < bytes.length; ) {
164
+ const step = utf8DecodeAt(bytes, i);
165
+ if (!step.valid) {
166
+ out[at] = bytes[i];
167
+ at += 1;
168
+ i += 1;
169
+ continue;
170
+ }
171
+ at += utf8EncodeInto(out, at, caseMapLookup(ranges, step.cp));
172
+ i += step.len;
173
+ }
174
+ return out;
175
+ }
176
+
177
+ function trimStartOf(bytes: Uint8Array): Uint8Array {
178
+ let i = 0;
179
+ while (i < bytes.length) {
180
+ const step = utf8DecodeAt(bytes, i);
181
+ if (!step.valid || !isJsWhitespace(step.cp)) break;
182
+ i += step.len;
183
+ }
184
+ return bytes.subarray(i);
185
+ }
186
+
187
+ function trimEndOf(bytes: Uint8Array): Uint8Array {
188
+ let end = bytes.length;
189
+ while (end > 0) {
190
+ const step = utf8DecodeBefore(bytes, end);
191
+ if (!step.valid || !isJsWhitespace(step.cp)) break;
192
+ end -= step.len;
193
+ }
194
+ return bytes.subarray(0, end);
195
+ }
196
+
197
+ function substringIndexOf(hay: Uint8Array, needle: Uint8Array): number {
198
+ if (needle.length > hay.length) return -1;
199
+ outer: for (let s = 0; s + needle.length <= hay.length; s++) {
200
+ for (let i = 0; i < needle.length; i++) {
201
+ if (hay[s + i] !== needle[i]) continue outer;
202
+ }
203
+ return s;
204
+ }
205
+ return -1;
206
+ }
207
+
208
+ function substringLastIndexOf(hay: Uint8Array, needle: Uint8Array): number {
209
+ if (needle.length > hay.length) return -1;
210
+ outer: for (let s = hay.length - needle.length; s >= 0; s--) {
211
+ for (let i = 0; i < needle.length; i++) {
212
+ if (hay[s + i] !== needle[i]) continue outer;
213
+ }
214
+ return s;
215
+ }
216
+ return -1;
217
+ }
218
+
219
+ function define(name: string, fn: (this: Uint8Array, ...args: never[]) => unknown): void {
220
+ Object.defineProperty(Uint8Array.prototype, name, {
221
+ value: fn,
222
+ writable: true,
223
+ configurable: true,
224
+ enumerable: false,
225
+ });
226
+ }
227
+
228
+ let installed = false;
229
+
230
+ /// Define the byte-text methods on Uint8Array.prototype. Idempotent; the
231
+ /// devhost (and the run-fidelity node driver) call it before importing a
232
+ /// core. `includes`/`indexOf`/`lastIndexOf` dispatch by argument type —
233
+ /// a bytes needle takes the substring search, a number keeps the
234
+ /// TypedArray element search node already ships.
235
+ export function installTextMethods(): void {
236
+ if (installed) return;
237
+ installed = true;
238
+
239
+ const proto = Uint8Array.prototype as unknown as Record<string, (...args: unknown[]) => unknown>;
240
+ const nativeIncludes = proto.includes;
241
+ const nativeIndexOf = proto.indexOf;
242
+ const nativeLastIndexOf = proto.lastIndexOf;
243
+
244
+ define("toUpperCase", function (this: Uint8Array): Uint8Array {
245
+ return caseMap(this, SIMPLE_UPPER_RANGES);
246
+ });
247
+ define("toLowerCase", function (this: Uint8Array): Uint8Array {
248
+ return caseMap(this, SIMPLE_LOWER_RANGES);
249
+ });
250
+ define("repeat", function (this: Uint8Array, count: number): Uint8Array {
251
+ const n = Math.trunc(count);
252
+ // The exact String.prototype.repeat refusal (native panics here).
253
+ if (n < 0 || n === Infinity) throw new RangeError(`Invalid count value: ${count}`);
254
+ const times = Number.isNaN(n) ? 0 : n;
255
+ const out = new Uint8Array(this.length * times);
256
+ for (let k = 0; k < times; k++) out.set(this, k * this.length);
257
+ return out;
258
+ });
259
+ define("startsWith", function (this: Uint8Array, needle: Uint8Array): boolean {
260
+ if (needle.length > this.length) return false;
261
+ for (let i = 0; i < needle.length; i++) if (this[i] !== needle[i]) return false;
262
+ return true;
263
+ });
264
+ define("endsWith", function (this: Uint8Array, needle: Uint8Array): boolean {
265
+ if (needle.length > this.length) return false;
266
+ const base = this.length - needle.length;
267
+ for (let i = 0; i < needle.length; i++) if (this[base + i] !== needle[i]) return false;
268
+ return true;
269
+ });
270
+ define("includes", function (this: Uint8Array, needle: unknown, ...rest: unknown[]): boolean {
271
+ if (needle instanceof Uint8Array) return substringIndexOf(this, needle) !== -1;
272
+ return nativeIncludes.call(this, needle, ...rest) as boolean;
273
+ });
274
+ define("indexOf", function (this: Uint8Array, needle: unknown, ...rest: unknown[]): number {
275
+ if (needle instanceof Uint8Array) return substringIndexOf(this, needle);
276
+ return nativeIndexOf.call(this, needle, ...rest) as number;
277
+ });
278
+ define("lastIndexOf", function (this: Uint8Array, needle: unknown, ...rest: unknown[]): number {
279
+ if (needle instanceof Uint8Array) return substringLastIndexOf(this, needle);
280
+ return nativeLastIndexOf.call(this, needle, ...rest) as number;
281
+ });
282
+ define("padStart", function (this: Uint8Array, n: number, fill?: Uint8Array): Uint8Array {
283
+ const fillBytes = fill ?? Uint8Array.of(0x20);
284
+ if (n <= this.length || fillBytes.length === 0) return this;
285
+ const out = new Uint8Array(n);
286
+ const pad = n - this.length;
287
+ for (let off = 0; off < pad; off += fillBytes.length) {
288
+ out.set(fillBytes.subarray(0, Math.min(fillBytes.length, pad - off)), off);
289
+ }
290
+ out.set(this, pad);
291
+ return out;
292
+ });
293
+ define("padEnd", function (this: Uint8Array, n: number, fill?: Uint8Array): Uint8Array {
294
+ const fillBytes = fill ?? Uint8Array.of(0x20);
295
+ if (n <= this.length || fillBytes.length === 0) return this;
296
+ const out = new Uint8Array(n);
297
+ out.set(this, 0);
298
+ for (let off = this.length; off < n; off += fillBytes.length) {
299
+ out.set(fillBytes.subarray(0, Math.min(fillBytes.length, n - off)), off);
300
+ }
301
+ return out;
302
+ });
303
+ define("trimStart", function (this: Uint8Array): Uint8Array {
304
+ return trimStartOf(this);
305
+ });
306
+ define("trimEnd", function (this: Uint8Array): Uint8Array {
307
+ return trimEndOf(this);
308
+ });
309
+ define("trim", function (this: Uint8Array): Uint8Array {
310
+ return trimEndOf(trimStartOf(this));
311
+ });
312
+ define("split", function (this: Uint8Array, sep: Uint8Array): Uint8Array[] {
313
+ if (sep.length === 0) return [this];
314
+ const out: Uint8Array[] = [];
315
+ let start = 0;
316
+ for (let i = 0; i + sep.length <= this.length; ) {
317
+ let hit = true;
318
+ for (let k = 0; k < sep.length; k++) {
319
+ if (this[i + k] !== sep[k]) {
320
+ hit = false;
321
+ break;
322
+ }
323
+ }
324
+ if (hit) {
325
+ out.push(this.subarray(start, i));
326
+ i += sep.length;
327
+ start = i;
328
+ } else {
329
+ i += 1;
330
+ }
331
+ }
332
+ out.push(this.subarray(start));
333
+ return out;
334
+ });
335
+ // `.at` stays node's own TypedArray method — its byte semantics are
336
+ // already exactly the surface's.
337
+ }