@tsplc/compiler 0.6.1 → 0.6.2

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.
@@ -23,8 +23,9 @@ import { Emitter, type Frame, type Referents } from "./frame";
23
23
  import type { GenericTypes } from "./generics";
24
24
  import { EntryImports } from "./imports";
25
25
  import { Initializers } from "./initializers";
26
- import { type ArrayLayout, arrayLayout, Layouts, unusedName } from "./layouts";
26
+ import { type ArrayLayout, arrayLayout, Layouts } from "./layouts";
27
27
  import { type Model, type ModelNode, type Root, tsPath } from "./model";
28
+ import { Names, routineNames, unusedName } from "./names";
28
29
  import { referenceLayout, referenceType } from "./references";
29
30
  import { StatementSpecializer } from "./statements";
30
31
  import { Storage } from "./storage";
@@ -51,7 +52,9 @@ export class Specializer implements RoutineTable {
51
52
  private readonly dialect: DialectText;
52
53
  readonly expressions: ExpressionSpecializer;
53
54
  private readonly statements: StatementSpecializer;
54
- readonly imports = new EntryImports();
55
+ readonly imports: EntryImports;
56
+ /** The names the generated modules use, which routines, types and imports share. */
57
+ private readonly names: Names;
55
58
  /** The source of each routine specialized so far, in the order each was finished. */
56
59
  private readonly finished: Mapped[] = [];
57
60
  /** Each async routine specialized for an instance, by routine name, which `cancelAsync(this)` cancels. */
@@ -76,22 +79,31 @@ export class Specializer implements RoutineTable {
76
79
  {
77
80
  kept,
78
81
  named,
82
+ enumRefusal,
79
83
  }: {
80
84
  /** Modules the compiled program keeps as written. */
81
85
  kept: (file: string) => boolean;
82
86
  /** Modules whose exported types the compiled program imports by name rather than copies. */
83
87
  named: (source: ts.SourceFile) => boolean;
88
+ /** Why the generated modules can't import an enum from the module that declares it; undefined when they can. */
89
+ enumRefusal: (declaration: ts.EnumDeclaration) => string | undefined;
84
90
  },
85
91
  rootModule: (root: Root) => string,
86
92
  ) {
87
93
  const checker = generics.checker;
88
94
  this.checker = checker;
95
+ this.names = new Names(
96
+ routineNames(sources.filter((source) => !kept(source.fileName))),
97
+ );
98
+ this.imports = new EntryImports(this.names);
89
99
  this.layouts = new Layouts(
90
100
  model,
91
101
  checker,
92
102
  kept,
93
103
  sources.filter(named),
94
104
  (node) => this.storage.storageType(node),
105
+ this.names,
106
+ enumRefusal,
95
107
  );
96
108
  this.storage = new Storage(
97
109
  model,
@@ -178,9 +190,10 @@ export class Specializer implements RoutineTable {
178
190
  }
179
191
  const name = unusedName(
180
192
  `tsplcCancel_${identityDigest(this.model.identity(self) ?? String(self.id))}`,
181
- (candidate) => this.results.has(candidate),
193
+ (candidate) => this.routineNamed(candidate),
182
194
  );
183
195
  this.results.set(name, {});
196
+ this.names.declare(name);
184
197
  this.cancellers.set(self, name);
185
198
  this.imports.addSdk(sdkModules.async, "cancelAsync");
186
199
  return name;
@@ -566,11 +579,22 @@ export class Specializer implements RoutineTable {
566
579
  const base = specialized
567
580
  ? `${named}_${identityDigest([instance ?? "", ...stable].join("\0"))}`
568
581
  : named;
569
- const name = unusedName(base, (candidate) => this.results.has(candidate));
582
+ const name = unusedName(base, (candidate) => this.routineNamed(candidate));
570
583
  this.results.set(name, {});
584
+ this.names.declare(name);
571
585
  return name;
572
586
  }
573
587
 
588
+ /** Whether the generated entry module already uses `name`, for a routine or an import. */
589
+ named(name: string): boolean {
590
+ return this.routineNamed(name);
591
+ }
592
+
593
+ /** Whether a routine can't take `name`: another has it, or the entry module imports something as it. */
594
+ private routineNamed(name: string): boolean {
595
+ return this.results.has(name) || this.names.importedAs(name);
596
+ }
597
+
574
598
  private bindParameter(
575
599
  name: ts.BindingName,
576
600
  arg: Value,
@@ -36,7 +36,7 @@ interface FoldedField {
36
36
  }
37
37
 
38
38
  /** A stored field of a flattened root: a global of the root's storage module. */
39
- interface RootField {
39
+ export interface RootField {
40
40
  name: string;
41
41
  layout: string;
42
42
  declaration: ts.Node | undefined;
@@ -283,10 +283,11 @@ export class Storage {
283
283
  private reach(root: Root): string {
284
284
  const module = this.rootModule(root);
285
285
  const pascal = pascalCase(basename(module, ".ts"));
286
- const namespace = this.typeNames.has(pascal) ? `${pascal}Root` : pascal;
287
286
  this.storedRoots.add(root);
288
- this.imports.namespaces.set(namespace, module);
289
- return namespace;
287
+ return this.imports.namespace(
288
+ module,
289
+ this.typeNames.has(pascal) ? `${pascal}Root` : pascal,
290
+ );
290
291
  }
291
292
 
292
293
  /** Stores a root whether or not scan code reads it, as exported data must be. */
@@ -599,12 +600,11 @@ export class Storage {
599
600
  };
600
601
  }
601
602
 
602
- /** A binding in a module the program keeps, read by its own name. */
603
+ /** A binding in a module the program keeps, read through the entry's import of it. */
603
604
  private keptRoot(root: Root): Value {
604
- this.imports.kept.add(root.file, root.name);
605
605
  return {
606
606
  kind: "code",
607
- text: root.name,
607
+ text: this.imports.kept.add(root.file, root.name),
608
608
  declared: this.checker.getTypeAtLocation(root.declaration.name),
609
609
  place: !(ts.getCombinedNodeFlags(root.declaration) & ts.NodeFlags.Const),
610
610
  };