@scriptc/compiler 0.0.29 → 0.0.31

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 (49) hide show
  1. package/ambient/scriptc-node-fallback.d.ts +5 -13
  2. package/dist/backend/emission/emit-async.d.ts +1 -0
  3. package/dist/backend/emission/emit-async.js +27 -1
  4. package/dist/backend/emission/emit-async.js.map +1 -1
  5. package/dist/backend/emission/emit-exprs.js +109 -18
  6. package/dist/backend/emission/emit-exprs.js.map +1 -1
  7. package/dist/backend/emission/emitter.d.ts +1 -0
  8. package/dist/backend/emission/emitter.js +25 -3
  9. package/dist/backend/emission/emitter.js.map +1 -1
  10. package/dist/backend/llvm/emitter.js +152 -0
  11. package/dist/backend/llvm/emitter.js.map +1 -1
  12. package/dist/backend/mangle.d.ts +1 -0
  13. package/dist/backend/mangle.js +3 -0
  14. package/dist/backend/mangle.js.map +1 -1
  15. package/dist/diagnostics/diagnostic.d.ts +23 -1
  16. package/dist/diagnostics/diagnostic.js +34 -0
  17. package/dist/diagnostics/diagnostic.js.map +1 -1
  18. package/dist/frontend/lowering/lower-calls.d.ts +3 -1
  19. package/dist/frontend/lowering/lower-calls.js +201 -37
  20. package/dist/frontend/lowering/lower-calls.js.map +1 -1
  21. package/dist/frontend/lowering/lower-containers.d.ts +6 -5
  22. package/dist/frontend/lowering/lower-containers.js +27 -13
  23. package/dist/frontend/lowering/lower-containers.js.map +1 -1
  24. package/dist/frontend/lowering/lower-exprs.d.ts +11 -1
  25. package/dist/frontend/lowering/lower-exprs.js +398 -49
  26. package/dist/frontend/lowering/lower-exprs.js.map +1 -1
  27. package/dist/frontend/lowering/lower-server.d.ts +5 -0
  28. package/dist/frontend/lowering/lower-server.js +105 -55
  29. package/dist/frontend/lowering/lower-server.js.map +1 -1
  30. package/dist/frontend/lowering/lower-stmts.js +74 -11
  31. package/dist/frontend/lowering/lower-stmts.js.map +1 -1
  32. package/dist/frontend/lowering/lowerer.d.ts +50 -12
  33. package/dist/frontend/lowering/lowerer.js +84 -15
  34. package/dist/frontend/lowering/lowerer.js.map +1 -1
  35. package/dist/index.js +35 -0
  36. package/dist/index.js.map +1 -1
  37. package/dist/ir/nodes.d.ts +52 -19
  38. package/dist/ir/nodes.js +2 -2
  39. package/dist/ir/nodes.js.map +1 -1
  40. package/dist/ir/validate.js +67 -9
  41. package/dist/ir/validate.js.map +1 -1
  42. package/dist/library/profile.d.ts +33 -0
  43. package/dist/library/profile.js +161 -3
  44. package/dist/library/profile.js.map +1 -1
  45. package/dist/library/sidecar.d.ts +3 -2
  46. package/dist/library/sidecar.js +7 -3
  47. package/dist/library/sidecar.js.map +1 -1
  48. package/package.json +2 -2
  49. package/surface-manifest.json +1 -1
@@ -215,6 +215,14 @@ export interface LowerOptions {
215
215
  * imports; without this option ambient declarations keep Node's ordinary
216
216
  * ReferenceError behavior. */
217
217
  ffiImports?: readonly IrFfiImport[];
218
+ /** LIBRARY mode's host-callback surface marker: the `ffiImports` above
219
+ * are profile-declared callback channels, not native-manifest bindings.
220
+ * Flips the binding diagnostics to the library flavor (SC4024), keeps a
221
+ * channel legal when no program declaration references it (an unused
222
+ * channel is capacity, not an error), and refuses a CALL of any other
223
+ * program-authored signature-only ambient function with the callback
224
+ * teaching instead of the ambient ReferenceError lowering. */
225
+ libraryCallbacks?: boolean;
218
226
  /** Coverage-only external host type surfaces. Their declarations inform
219
227
  * the checker, while every runtime value use remains an SC1010 fence. */
220
228
  externalTypes?: ReadonlyMap<string, string>;
@@ -243,6 +251,8 @@ export interface LowererMode {
243
251
  startupCrash?: StartupCrash | null;
244
252
  /** The build's outbound native FFI declarations. */
245
253
  ffiImports?: readonly IrFfiImport[];
254
+ /** LowerOptions.libraryCallbacks (see there). */
255
+ libraryCallbacks?: boolean;
246
256
  /** Program-validated ambient declaration symbols for each FFI name.
247
257
  * Undefined in discovery's legacy call-local validation path. */
248
258
  ffiBindingSymbols?: ReadonlyMap<string, ReadonlySet<ts.Symbol>>;
@@ -446,6 +456,17 @@ export declare class Lowerer {
446
456
  * aliases, so this carries the var/let form the checker cannot.
447
457
  * Scoped strictly by narrowingAliases (lowerIf / lowerCondition). */
448
458
  readonly aliasNarrowTypes: Map<ts.Symbol, ts.Type>;
459
+ /** Locals widened beyond the checker's type because an inferred indexed
460
+ * read can be absent at runtime. Bare reads preserve that union until a
461
+ * surrounding JavaScript guard/default consumes it. */
462
+ readonly runtimeOptionalLocals: Set<IrLocal>;
463
+ /** All storage slots widened for runtime absence, including slots whose
464
+ * current control-flow branch has temporarily narrowed the value. */
465
+ readonly runtimeOptionalStorageLocals: Set<IrLocal>;
466
+ /** Capture entries and their origin share one mutable box. Normalize each
467
+ * entry to the origin so writes and flow proofs stay synchronized. */
468
+ readonly runtimeOptionalRoots: Map<IrLocal, IrLocal>;
469
+ runtimeOptionalRootOf(local: IrLocal): IrLocal;
449
470
  /** Runs `fn` with the given aliased-typeof narrows applied (and restored
450
471
  * after) — the branch-scoping primitive. */
451
472
  narrowingAliases<T>(narrows: readonly {
@@ -759,6 +780,7 @@ export declare class Lowerer {
759
780
  readonly startupCrash: StartupCrash | null;
760
781
  /** Outbound native bindings by their source-level ambient name. */
761
782
  readonly ffiImports: readonly IrFfiImport[];
783
+ readonly libraryCallbacks: boolean;
762
784
  readonly ffiImportsByName: ReadonlyMap<string, IrFfiImport>;
763
785
  /** Non-null after whole-program FFI declaration validation. */
764
786
  readonly ffiBindingSymbols: ReadonlyMap<string, ReadonlySet<ts.Symbol>> | null;
@@ -883,10 +905,7 @@ export declare class Lowerer {
883
905
  cjsLocalModuleBindingOf(expr: ts.Expression): boolean;
884
906
  /** Assignment-target resolution: a function local (possibly captured) or
885
907
  * a module global. tsc has already rejected writes to consts. */
886
- resolveWritable(ident: ts.Identifier): {
887
- id: string;
888
- type: IrType;
889
- } | null;
908
+ resolveWritable(ident: ts.Identifier): IrLocal | null;
890
909
  fnSigOf(ident: ts.Identifier): FnSig | null;
891
910
  globalOf(ident: ts.Identifier): IrGlobal | null;
892
911
  splitFiles(): FileParts[];
@@ -1025,6 +1044,15 @@ export declare class Lowerer {
1025
1044
  /** formatIrType with this Lowerer's registries (records and unions expand
1026
1045
  * to their structure in diagnostics). */
1027
1046
  fmt(t: IrType): string;
1047
+ /** Whether an IR value has JavaScript Array identity. Homogeneous arrays
1048
+ * use the native array representation; non-empty fixed tuples use a
1049
+ * positional record shape so their slots can keep distinct types, but
1050
+ * Array.isArray must still answer true for both representations. */
1051
+ isArrayValueType(t: IrType): boolean;
1052
+ /** Runtime tags of every JavaScript-array arm in one union. Kept beside
1053
+ * isArrayValueType so Array.isArray's predicate and its narrowing bridge
1054
+ * cannot disagree about fixed tuple arms. */
1055
+ arrayValueTags(unionId: string): number[];
1028
1056
  /** isJsonSafeType with this Lowerer's registries — the shared fence for
1029
1057
  * what JSON.stringify accepts and what a checked cast can validate. */
1030
1058
  jsonSafe(t: IrType): boolean;
@@ -1048,14 +1076,24 @@ export declare class Lowerer {
1048
1076
  * arm before the JSON round trip). */
1049
1077
  boundaryExitSafe(t: IrType): boolean;
1050
1078
  isIslandExpr(node: ts.Expression): boolean;
1051
- /** True when this node's CHECKER type is `any[]`/`unknown[]` — the type
1052
- * tsc's Array.isArray predicate narrows readonly arrays to (its `arg is
1053
- * any[]` quirk), and what a union collapses to when such an arm absorbs
1054
- * its siblings. The VALUE behind it can still be a real static array
1055
- * (maybeNarrow's isArray bridge extracts the union's array arm), so
1056
- * receiver-typed dispatch falls back to the LOWERED type under this
1057
- * test. */
1079
+ /** True when this node's CHECKER type is proven `any[]`/`unknown[]`,
1080
+ * directly or through the intersection tsc builds for a readonly tuple
1081
+ * union (`(Model | readonly [Model, Command]) & any[]`; TS7 distributes
1082
+ * this over the union arms). These are forms of the `arg is any[]`
1083
+ * Array.isArray predicate; the VALUE behind them can still be a real
1084
+ * static array/tuple (maybeNarrow's bridge extracts the union's one
1085
+ * array-valued arm), so receiver-typed dispatch falls back to the LOWERED
1086
+ * type under this test. */
1058
1087
  checkerAnyArray(node: ts.Expression): boolean;
1088
+ /** Type-level half of checkerAnyArray, for narrowing sites that already
1089
+ * queried the checker type from a general AST node. */
1090
+ checkerAnyArrayType(t: ts.Type): boolean;
1091
+ /** The lowered static array/tuple value behind checkerAnyArray's
1092
+ * synthetic `any[]` spelling. Array.isArray can leave readonly tuple
1093
+ * unions as intersections that do not map directly, while maybeNarrow
1094
+ * still extracts the runtime-proven array arm from the original union.
1095
+ * Receiver dispatchers use this bridge instead of the synthetic type. */
1096
+ checkerArrayValue(node: ts.Expression): IrExpr | null;
1059
1097
  /** Substitutes a bound type parameter anywhere inside mapType's recursion
1060
1098
  * (`T`, `T[]`, `{ v: T }`, `(x: T) => T` all resolve). Inert outside
1061
1099
  * generic instantiation. */
@@ -1836,7 +1874,7 @@ export declare class Lowerer {
1836
1874
  lowerShorthandValue(prop: ts.ShorthandPropertyAssignment): IrExpr;
1837
1875
  rejectThisInObjectMethod(node: ts.Node): void;
1838
1876
  lowerElementAccess(expr: ts.ElementAccessExpression): IrExpr;
1839
- lowerRecordKeyRead(expr: ts.ElementAccessExpression, shapeId: string, shape: IrRecordShape): IrExpr;
1877
+ lowerRecordKeyRead(expr: ts.ElementAccessExpression, shapeId: string, shape: IrRecordShape, includeUndefined?: boolean): IrExpr;
1840
1878
  lowerElementWrite(expr: ts.BinaryExpression): IrStmt;
1841
1879
  ensureString(e: IrExpr, node: ts.Node): IrExpr;
1842
1880
  lowerTemplate(expr: ts.TemplateExpression): IrExpr;
@@ -177,12 +177,14 @@ export function lowerToIr(program, entry, moduleOrder, options = {}) {
177
177
  });
178
178
  }
179
179
  const ffiImports = options.ffiImports ?? [];
180
+ const libraryCallbacks = options.libraryCallbacks ?? false;
180
181
  const externalTypes = options.externalTypes ?? new Map();
181
182
  const externalTypeSpecifiersByFile = options.externalTypeSpecifiersByFile ??
182
183
  directExternalTypeSpecifiersByFile(externalTypes);
183
184
  const validation = new Lowerer(program, entry, moduleOrder, dynamic, {
184
185
  targetPlatform,
185
186
  ffiImports,
187
+ libraryCallbacks,
186
188
  externalTypes,
187
189
  externalTypeSpecifiersByFile,
188
190
  });
@@ -198,6 +200,7 @@ export function lowerToIr(program, entry, moduleOrder, options = {}) {
198
200
  : new Lowerer(program, entry, moduleOrder, dynamic, {
199
201
  targetPlatform,
200
202
  ffiImports,
203
+ libraryCallbacks,
201
204
  externalTypes,
202
205
  externalTypeSpecifiersByFile,
203
206
  ffiBindingSymbols: ffiValidation.symbolsByName,
@@ -208,6 +211,7 @@ export function lowerToIr(program, entry, moduleOrder, options = {}) {
208
211
  targetPlatform,
209
212
  startupCrash,
210
213
  ffiImports,
214
+ libraryCallbacks,
211
215
  ffiBindingSymbols: ffiValidation.symbolsByName,
212
216
  externalTypes,
213
217
  externalTypeSpecifiersByFile,
@@ -225,6 +229,7 @@ export function lowerToIr(program, entry, moduleOrder, options = {}) {
225
229
  alreadyFlushed: emit.flushedSymbols,
226
230
  targetPlatform,
227
231
  ffiImports,
232
+ libraryCallbacks,
228
233
  ffiBindingSymbols: ffiValidation.symbolsByName,
229
234
  externalTypes,
230
235
  externalTypeSpecifiersByFile,
@@ -624,6 +629,19 @@ export class Lowerer {
624
629
  * aliases, so this carries the var/let form the checker cannot.
625
630
  * Scoped strictly by narrowingAliases (lowerIf / lowerCondition). */
626
631
  aliasNarrowTypes = new Map();
632
+ /** Locals widened beyond the checker's type because an inferred indexed
633
+ * read can be absent at runtime. Bare reads preserve that union until a
634
+ * surrounding JavaScript guard/default consumes it. */
635
+ runtimeOptionalLocals = new Set();
636
+ /** All storage slots widened for runtime absence, including slots whose
637
+ * current control-flow branch has temporarily narrowed the value. */
638
+ runtimeOptionalStorageLocals = new Set();
639
+ /** Capture entries and their origin share one mutable box. Normalize each
640
+ * entry to the origin so writes and flow proofs stay synchronized. */
641
+ runtimeOptionalRoots = new Map();
642
+ runtimeOptionalRootOf(local) {
643
+ return this.runtimeOptionalRoots.get(local) ?? local;
644
+ }
627
645
  /** Runs `fn` with the given aliased-typeof narrows applied (and restored
628
646
  * after) — the branch-scoping primitive. */
629
647
  narrowingAliases(narrows, fn) {
@@ -939,6 +957,7 @@ export class Lowerer {
939
957
  startupCrash;
940
958
  /** Outbound native bindings by their source-level ambient name. */
941
959
  ffiImports;
960
+ libraryCallbacks;
942
961
  ffiImportsByName;
943
962
  /** Non-null after whole-program FFI declaration validation. */
944
963
  ffiBindingSymbols;
@@ -996,6 +1015,7 @@ export class Lowerer {
996
1015
  this.targetPlatform = mode.targetPlatform ?? process.platform;
997
1016
  this.startupCrash = mode.startupCrash ?? null;
998
1017
  this.ffiImports = mode.ffiImports ?? [];
1018
+ this.libraryCallbacks = mode.libraryCallbacks ?? false;
999
1019
  this.ffiImportsByName = new Map(this.ffiImports.map((entry) => [entry.name, entry]));
1000
1020
  this.ffiBindingSymbols = mode.ffiBindingSymbols ?? null;
1001
1021
  this.externalTypes = mode.externalTypes ?? new Map();
@@ -1545,10 +1565,10 @@ export class Lowerer {
1545
1565
  this.unsupported("SC1090", ident, "assignments to catch bindings");
1546
1566
  }
1547
1567
  if (local)
1548
- return { id: local.id, type: local.type };
1568
+ return local;
1549
1569
  const g = this.globalOf(ident);
1550
1570
  if (g)
1551
- return { id: g.id, type: g.type };
1571
+ return g;
1552
1572
  return null;
1553
1573
  }
1554
1574
  fnSigOf(ident) {
@@ -2663,6 +2683,20 @@ export class Lowerer {
2663
2683
  fmt(t) {
2664
2684
  return formatIrType(t, this.shapes, this.unions);
2665
2685
  }
2686
+ /** Whether an IR value has JavaScript Array identity. Homogeneous arrays
2687
+ * use the native array representation; non-empty fixed tuples use a
2688
+ * positional record shape so their slots can keep distinct types, but
2689
+ * Array.isArray must still answer true for both representations. */
2690
+ isArrayValueType(t) {
2691
+ return t.kind === "array" || (t.kind === "record" && this.shapes.get(t.shapeId)?.tuple === true);
2692
+ }
2693
+ /** Runtime tags of every JavaScript-array arm in one union. Kept beside
2694
+ * isArrayValueType so Array.isArray's predicate and its narrowing bridge
2695
+ * cannot disagree about fixed tuple arms. */
2696
+ arrayValueTags(unionId) {
2697
+ const def = this.unions.get(unionId);
2698
+ return def ? def.arms.flatMap((arm, tag) => (this.isArrayValueType(arm) ? [tag] : [])) : [];
2699
+ }
2666
2700
  /** isJsonSafeType with this Lowerer's registries — the shared fence for
2667
2701
  * what JSON.stringify accepts and what a checked cast can validate. */
2668
2702
  jsonSafe(t) {
@@ -2696,18 +2730,49 @@ export class Lowerer {
2696
2730
  isIslandExpr(node) {
2697
2731
  return isIslandExpr(this, node);
2698
2732
  }
2699
- /** True when this node's CHECKER type is `any[]`/`unknown[]` — the type
2700
- * tsc's Array.isArray predicate narrows readonly arrays to (its `arg is
2701
- * any[]` quirk), and what a union collapses to when such an arm absorbs
2702
- * its siblings. The VALUE behind it can still be a real static array
2703
- * (maybeNarrow's isArray bridge extracts the union's array arm), so
2704
- * receiver-typed dispatch falls back to the LOWERED type under this
2705
- * test. */
2733
+ /** True when this node's CHECKER type is proven `any[]`/`unknown[]`,
2734
+ * directly or through the intersection tsc builds for a readonly tuple
2735
+ * union (`(Model | readonly [Model, Command]) & any[]`; TS7 distributes
2736
+ * this over the union arms). These are forms of the `arg is any[]`
2737
+ * Array.isArray predicate; the VALUE behind them can still be a real
2738
+ * static array/tuple (maybeNarrow's bridge extracts the union's one
2739
+ * array-valued arm), so receiver-typed dispatch falls back to the LOWERED
2740
+ * type under this test. */
2706
2741
  checkerAnyArray(node) {
2707
- const t = this.typeOf(node);
2708
- return (this.checker.isArrayType(t) &&
2709
- ((this.checker.getTypeArguments(t)[0]?.flags ?? 0) &
2710
- (ts.TypeFlags.Any | ts.TypeFlags.Unknown)) !== 0);
2742
+ return this.checkerAnyArrayType(this.typeOf(node));
2743
+ }
2744
+ /** Type-level half of checkerAnyArray, for narrowing sites that already
2745
+ * queried the checker type from a general AST node. */
2746
+ checkerAnyArrayType(t) {
2747
+ const isAnyArray = (part) => this.checker.isArrayType(part) &&
2748
+ ((this.checker.getTypeArguments(part)[0]?.flags ?? 0) &
2749
+ (ts.TypeFlags.Any | ts.TypeFlags.Unknown)) !== 0;
2750
+ const visit = (part) => {
2751
+ if (isAnyArray(part))
2752
+ return true;
2753
+ // 5.9 leaves `(U) & any[]` as one intersection; 7 distributes it
2754
+ // into `(A & any[]) | (B & any[])`. An intersection is array-proven
2755
+ // when one constituent is; a union is array-proven only when EVERY
2756
+ // arm is, so an ordinary `any[] | string` never qualifies.
2757
+ if ((part.flags & ts.TypeFlags.Intersection) !== 0) {
2758
+ return part.getTypes().some(visit);
2759
+ }
2760
+ if (part.isUnionType())
2761
+ return part.getTypes().every(visit);
2762
+ return false;
2763
+ };
2764
+ return visit(t);
2765
+ }
2766
+ /** The lowered static array/tuple value behind checkerAnyArray's
2767
+ * synthetic `any[]` spelling. Array.isArray can leave readonly tuple
2768
+ * unions as intersections that do not map directly, while maybeNarrow
2769
+ * still extracts the runtime-proven array arm from the original union.
2770
+ * Receiver dispatchers use this bridge instead of the synthetic type. */
2771
+ checkerArrayValue(node) {
2772
+ if (!this.checkerAnyArray(node))
2773
+ return null;
2774
+ const value = this.lowerExpr(node);
2775
+ return this.isArrayValueType(value.type) ? value : null;
2711
2776
  }
2712
2777
  /** Substitutes a bound type parameter anywhere inside mapType's recursion
2713
2778
  * (`T`, `T[]`, `{ v: T }`, `(x: T) => T` all resolve). Inert outside
@@ -6575,6 +6640,10 @@ export class Lowerer {
6575
6640
  ctx.captureBySymbol.set(symbol, entry);
6576
6641
  ctx.captures.push({ localId: entry.id, name: entry.name, type: entry.type });
6577
6642
  ctx.captureSources.push(parentEntry.id);
6643
+ const runtimeOptionalRoot = this.runtimeOptionalRootOf(parentEntry);
6644
+ if (this.runtimeOptionalStorageLocals.has(runtimeOptionalRoot)) {
6645
+ this.runtimeOptionalRoots.set(entry, runtimeOptionalRoot);
6646
+ }
6578
6647
  }
6579
6648
  parentEntry = entry;
6580
6649
  }
@@ -6995,8 +7064,8 @@ export class Lowerer {
6995
7064
  lowerElementAccess(expr) {
6996
7065
  return lowerElementAccess(this, expr);
6997
7066
  }
6998
- lowerRecordKeyRead(expr, shapeId, shape) {
6999
- return lowerRecordKeyRead(this, expr, shapeId, shape);
7067
+ lowerRecordKeyRead(expr, shapeId, shape, includeUndefined = false) {
7068
+ return lowerRecordKeyRead(this, expr, shapeId, shape, includeUndefined);
7000
7069
  }
7001
7070
  lowerElementWrite(expr) {
7002
7071
  return lowerElementWrite(this, expr);