@tsplc/compiler 0.2.21 → 0.4.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 (115) hide show
  1. package/README.md +1 -1
  2. package/dist/cli.js +280 -223
  3. package/dist/cli.js.map +12 -12
  4. package/dist/codesys.js +452 -330
  5. package/dist/codesys.js.map +24 -24
  6. package/dist/elaborate-capture.js +112 -7
  7. package/dist/elaborate-capture.js.map +5 -4
  8. package/dist/index.js +5682 -5407
  9. package/dist/index.js.map +39 -37
  10. package/dist/ir.js +1154 -327
  11. package/dist/ir.js.map +23 -20
  12. package/dist/project.js +68 -29
  13. package/dist/project.js.map +4 -4
  14. package/dist/retain.js +8 -0
  15. package/dist/retain.js.map +10 -0
  16. package/dist/server-check.js +59 -0
  17. package/dist/server-check.js.map +11 -0
  18. package/dist/telemetry.js +19 -10
  19. package/dist/telemetry.js.map +3 -3
  20. package/package.json +33 -12
  21. package/src/codesys/config.ts +1 -1
  22. package/src/codesys/debug.ts +7 -21
  23. package/src/codesys/engineering.ts +8 -12
  24. package/src/codesys/global-lists.ts +19 -17
  25. package/src/codesys/global-names.ts +3 -3
  26. package/src/codesys/http.ts +37 -34
  27. package/src/codesys/index.ts +5 -22
  28. package/src/codesys/instrumentation.ts +3 -1
  29. package/src/codesys/io-paths.ts +2 -1
  30. package/src/codesys/manifest.ts +35 -18
  31. package/src/codesys/numeric.ts +2 -4
  32. package/src/codesys/plan.ts +94 -26
  33. package/src/codesys/publication.ts +4 -3
  34. package/src/codesys/routines.ts +2 -2
  35. package/src/codesys/sources.ts +1 -0
  36. package/src/codesys/st-files.ts +86 -50
  37. package/src/codesys/storage.ts +44 -9
  38. package/src/codesys/task-access.ts +32 -10
  39. package/src/codesys/telemetry.ts +93 -46
  40. package/src/codesys/types.ts +81 -3
  41. package/src/codesys/vendor.ts +1 -1
  42. package/src/frontend/analyzer.ts +15 -14
  43. package/src/frontend/async.ts +23 -21
  44. package/src/frontend/blocks.ts +7 -6
  45. package/src/frontend/configuration.ts +8 -6
  46. package/src/frontend/const-functions.ts +5 -5
  47. package/src/frontend/cooperative.ts +2 -1
  48. package/src/frontend/coroutines.ts +24 -21
  49. package/src/frontend/elaborate/calls.ts +12 -7
  50. package/src/frontend/elaborate/capture-protocol.ts +103 -0
  51. package/src/frontend/elaborate/capture.ts +48 -19
  52. package/src/frontend/elaborate/dialect.ts +2 -2
  53. package/src/frontend/elaborate/expressions.ts +9 -6
  54. package/src/frontend/elaborate/frame.ts +1 -1
  55. package/src/frontend/elaborate/graph.ts +27 -43
  56. package/src/frontend/elaborate/index.ts +52 -17
  57. package/src/frontend/elaborate/initializers.ts +3 -2
  58. package/src/frontend/elaborate/layouts.ts +2 -2
  59. package/src/frontend/elaborate/model.ts +34 -21
  60. package/src/frontend/elaborate/retention.ts +206 -0
  61. package/src/frontend/elaborate/specialize.ts +7 -6
  62. package/src/frontend/elaborate/statements.ts +47 -38
  63. package/src/frontend/elaborate/storage.ts +26 -6
  64. package/src/frontend/exposure.ts +6 -6
  65. package/src/frontend/expressions.ts +26 -25
  66. package/src/frontend/intrinsics.ts +3 -3
  67. package/src/frontend/module-graph.ts +1 -1
  68. package/src/frontend/positions.ts +2 -1
  69. package/src/frontend/promise-chains.ts +1 -0
  70. package/src/frontend/promises.ts +8 -7
  71. package/src/frontend/routines.ts +3 -2
  72. package/src/frontend/sdk.ts +2 -2
  73. package/src/frontend/services.ts +23 -20
  74. package/src/frontend/statements.ts +8 -6
  75. package/src/frontend/tasks.ts +11 -6
  76. package/src/frontend/telemetry.ts +1 -1
  77. package/src/frontend/types.ts +11 -9
  78. package/src/frontend/typescript.ts +92 -42
  79. package/src/index.ts +2 -1
  80. package/src/ir/aggregates.ts +4 -3
  81. package/src/ir/allocation.ts +2 -1
  82. package/src/ir/core-model.ts +38 -8
  83. package/src/ir/declarations.ts +12 -12
  84. package/src/ir/domains.ts +8 -8
  85. package/src/ir/fold.ts +2 -2
  86. package/src/ir/index.ts +53 -40
  87. package/src/ir/job-errors.ts +22 -11
  88. package/src/ir/layouts.ts +3 -2
  89. package/src/ir/link.ts +6 -24
  90. package/src/ir/model.ts +3 -2
  91. package/src/ir/normalization-error.ts +1 -1
  92. package/src/ir/normalize.ts +19 -12
  93. package/src/ir/numeric.ts +1 -0
  94. package/src/{codesys → ir}/operands.ts +2 -6
  95. package/src/ir/ranges.ts +2 -2
  96. package/src/ir/routine.ts +35 -20
  97. package/src/ir/scalars.ts +3 -3
  98. package/src/ir/share.ts +862 -0
  99. package/src/ir/temporaries.ts +236 -0
  100. package/src/ir/types.ts +1 -1
  101. package/src/project/artifacts.ts +12 -2
  102. package/src/project/cli.ts +42 -44
  103. package/src/project/debugger.ts +15 -9
  104. package/src/project/index.ts +1 -1
  105. package/src/project/plant.ts +34 -23
  106. package/src/project/project-commands.ts +92 -73
  107. package/src/project/project.ts +91 -42
  108. package/src/project/release.ts +50 -57
  109. package/src/project/server-check.ts +54 -0
  110. package/src/project/server.ts +60 -41
  111. package/src/project/terminal.ts +3 -1
  112. package/stdlib/retain.ts +2 -0
  113. package/types/globals.d.ts +1 -0
  114. package/types/retain.d.ts +6 -0
  115. package/types/telemetry.d.ts +2 -2
@@ -1,14 +1,14 @@
1
- import ts from "typescript";
2
1
  import { dirname, join, resolve } from "node:path";
3
2
  import {
4
- error,
5
3
  type Constant,
6
4
  type Diagnostic,
5
+ error,
7
6
  type SourceSpan,
8
7
  type TypeId,
9
8
  } from "@tsplc/compiler/ir";
9
+ import { generatedDirectory } from "@tsplc/config/project-files";
10
+ import ts from "typescript";
10
11
  import { runtimeSources } from "../module-graph.ts";
11
- import { inSdk, namesSdk } from "../sdk.ts";
12
12
  import {
13
13
  copied,
14
14
  from,
@@ -16,16 +16,17 @@ import {
16
16
  type Mapped,
17
17
  type Positions,
18
18
  } from "../positions.ts";
19
- import { captureGraph, type CapturedValue } from "./graph.ts";
19
+ import { inSdk, namesSdk } from "../sdk.ts";
20
+ import type { CompileDebug } from "../typescript.ts";
21
+ import type { CapturedValue } from "./capture-protocol.ts";
22
+ import { captureGraph } from "./graph.ts";
20
23
  import { importPath } from "./imports.ts";
21
24
  import { Model, type Root } from "./model.ts";
25
+ import { Retention, retainedDeclarations } from "./retention.ts";
22
26
  import { closingBrace, Specializer } from "./specialize.ts";
23
27
  import { setterName } from "./storage.ts";
24
28
  import { SpecializeError } from "./values.ts";
25
29
 
26
- /** Directory of the modules elaboration generates, beside the entry module. */
27
- const generatedDirectory = ".tsplc";
28
-
29
30
  /**
30
31
  * A module the compiled program keeps as written: only types, enums, constants and the service
31
32
  * client. Everything else is plain TypeScript that elaboration compiles from its setup graph.
@@ -105,10 +106,15 @@ const constantInitializer = (
105
106
  return false;
106
107
  };
107
108
 
108
- /** Whether the program is plain TypeScript: classes with constructors, or setup code building state. */
109
+ /**
110
+ * Whether the program is plain TypeScript: it declares a class that is not a function block, one
111
+ * extending the SDK's `FunctionBlock` however the module names it.
112
+ */
109
113
  const needsElaboration = (
110
114
  sources: readonly ts.SourceFile[],
111
115
  kept: (file: string) => boolean,
116
+ checker: ts.TypeChecker,
117
+ sdk: string,
112
118
  ): boolean =>
113
119
  sources.some(
114
120
  (source) =>
@@ -118,7 +124,7 @@ const needsElaboration = (
118
124
  ts.isClassDeclaration(statement) &&
119
125
  !statement.heritageClauses?.some((clause) =>
120
126
  clause.types.some((type) =>
121
- type.expression.getText().startsWith("FunctionBlock"),
127
+ namesSdk(checker, sdk, type.expression, "FunctionBlock"),
122
128
  ),
123
129
  ),
124
130
  ),
@@ -174,6 +180,7 @@ export const lowerElaboration = (
174
180
  entries: Readonly<Record<string, { module: string; export: string }>>,
175
181
  root: string,
176
182
  positions: Positions,
183
+ debug: Pick<CompileDebug, "stackTraces">,
177
184
  ): {
178
185
  sources: Map<string, Mapped>;
179
186
  diagnostics: Diagnostic[];
@@ -199,7 +206,7 @@ export const lowerElaboration = (
199
206
  .map((source) => source.fileName),
200
207
  );
201
208
  const isKept = (file: string) => kept.has(file);
202
- if (!needsElaboration(runtime, isKept)) {
209
+ if (!needsElaboration(runtime, isKept, checker, sdk)) {
203
210
  return result;
204
211
  }
205
212
  const [entry, ...otherEntries] = Object.values(entries);
@@ -224,6 +231,7 @@ export const lowerElaboration = (
224
231
  const rootModule = (item: Root) => `${directory}/${item.name}.ts`;
225
232
 
226
233
  try {
234
+ const declarations = retainedDeclarations(runtime, checker, sdk);
227
235
  const graph = captureGraph(entryFile, runtime);
228
236
  const model = new Model(graph, checker, program, entryFile);
229
237
  const specializer = new Specializer(
@@ -245,9 +253,14 @@ export const lowerElaboration = (
245
253
  }
246
254
 
247
255
  // Writing the globals declares the types they use, so they come before the types module.
256
+ const retention = new Retention(model, specializer.storage, declarations);
248
257
  const globals = new Map<Root, Mapped[]>();
249
258
  for (const item of specializer.storage.roots) {
250
- globals.set(item, storageGlobals(specializer, item));
259
+ globals.set(item, storageGlobals(specializer, retention, item));
260
+ }
261
+ const [unretained] = retention.unclaimed();
262
+ if (unretained) {
263
+ throw unretained;
251
264
  }
252
265
  const typesFile = `${directory}/types.ts`;
253
266
  const typeImports = (file: string) => [
@@ -334,7 +347,7 @@ export const lowerElaboration = (
334
347
  }
335
348
  }
336
349
  } catch (cause) {
337
- diagnostics.push(failure(cause, positions));
350
+ diagnostics.push(failure(cause, positions, debug));
338
351
  }
339
352
  return result;
340
353
  };
@@ -343,9 +356,14 @@ export const lowerElaboration = (
343
356
  * The globals of a root's storage module: one per stored field of a record or instance, or the
344
357
  * root itself. Other modules can't assign an imported binding, so each global comes with a setter.
345
358
  */
346
- const storageGlobals = (specializer: Specializer, item: Root): Mapped[] => {
359
+ const storageGlobals = (
360
+ specializer: Specializer,
361
+ retention: Retention,
362
+ item: Root,
363
+ ): Mapped[] => {
347
364
  const { model, checker, layouts, storage, initializers } = specializer;
348
365
  const lines: Mapped[] = [];
366
+ const retained: string[] = [];
349
367
  const global = (
350
368
  name: string,
351
369
  layout: string,
@@ -353,8 +371,13 @@ const storageGlobals = (specializer: Specializer, item: Root): Mapped[] => {
353
371
  declaration: ts.Node | undefined,
354
372
  ) => {
355
373
  const origin = declaration ?? item.declaration;
374
+ const kept = retention.global(item, storage.flattened(item) ? name : null);
375
+ if (kept) {
376
+ retained.push(name);
377
+ }
378
+ const value = kept ? `retain(${initial})` : initial;
356
379
  lines.push(
357
- from(origin, `export let ${name}: ${layout} = ${initial};`),
380
+ from(origin, `export let ${name}: ${layout} = ${value};`),
358
381
  from(
359
382
  origin,
360
383
  `export function ${setterName(name)}(value: ${layout}): void { ${name} = value; }`,
@@ -416,7 +439,15 @@ const storageGlobals = (specializer: Specializer, item: Root): Mapped[] => {
416
439
  );
417
440
  }
418
441
  }
419
- return lines;
442
+ return retained.length
443
+ ? [
444
+ from(
445
+ item.declaration,
446
+ 'import { retain } from "@tsplc/compiler/types";',
447
+ ),
448
+ ...lines,
449
+ ]
450
+ : lines;
420
451
  };
421
452
 
422
453
  /** A plain module reduced to the types other modules may import from it. */
@@ -438,7 +469,11 @@ const typesOnly = (source: ts.SourceFile): Mapped =>
438
469
  );
439
470
 
440
471
  /** An elaboration failure as a diagnostic, at the node specialization stopped at when it knows one. */
441
- const failure = (cause: unknown, positions: Positions): Diagnostic => {
472
+ const failure = (
473
+ cause: unknown,
474
+ positions: Positions,
475
+ debug: Pick<CompileDebug, "stackTraces">,
476
+ ): Diagnostic => {
442
477
  if (cause instanceof SpecializeError) {
443
478
  return error("PLC1010", cause.message, positions.node(cause.node));
444
479
  }
@@ -447,7 +482,7 @@ const failure = (cause: unknown, positions: Positions): Diagnostic => {
447
482
  }
448
483
  return error(
449
484
  "PLC1010",
450
- process.env.TSPLC_DEBUG ? String(cause.stack) : cause.message,
485
+ debug.stackTraces ? String(cause.stack) : cause.message,
451
486
  );
452
487
  };
453
488
 
@@ -1,6 +1,7 @@
1
+ // biome-ignore-all lint/style/noNonNullAssertion: Retain existing non-null assertions during the tooling migration; changing their contracts requires a separate review.
1
2
  import ts from "typescript";
2
- import type { CapturedValue } from "./graph.ts";
3
- import { recordTypes, type Layouts } from "./layouts.ts";
3
+ import type { CapturedValue } from "./capture-protocol.ts";
4
+ import { type Layouts, recordTypes } from "./layouts.ts";
4
5
  import type { Model, ModelNode } from "./model.ts";
5
6
  import type { Storage } from "./storage.ts";
6
7
 
@@ -1,6 +1,6 @@
1
- import ts from "typescript";
2
1
  import { primitiveTypes } from "@tsplc/compiler/ir";
3
- import type { CapturedValue } from "./graph.ts";
2
+ import ts from "typescript";
3
+ import type { CapturedValue } from "./capture-protocol.ts";
4
4
  import { ModuleImports } from "./imports.ts";
5
5
  import type { Model } from "./model.ts";
6
6
 
@@ -1,9 +1,14 @@
1
1
  import ts from "typescript";
2
- import type { CapturedGraph, CapturedNode, CapturedValue } from "./graph.ts";
2
+ import type {
3
+ CapturedGraph,
4
+ CapturedNode,
5
+ CapturedValue,
6
+ } from "./capture-protocol.ts";
3
7
 
4
8
  /** One step from an owner to the object it holds. */
5
9
  export type Step =
6
- { kind: "field"; name: string } | { kind: "index"; index: number };
10
+ | { kind: "field"; name: string }
11
+ | { kind: "index"; index: number };
7
12
 
8
13
  /** Where a captured object is stored: a root binding and the steps to it. */
9
14
  export interface Owner {
@@ -11,7 +16,7 @@ export interface Owner {
11
16
  steps: readonly Step[];
12
17
  }
13
18
 
14
- /** The TypeScript path of an owned object or field, as the program names it: `program.diverters.binTrackers[10]`. */
19
+ /** The TypeScript path of an owned object or field, as the program names it: `program.diverters.sampleCounters[10]`. */
15
20
  export const tsPath = (owner: Owner): string =>
16
21
  owner.root.name + owner.steps.map(stepPath).join("");
17
22
 
@@ -257,29 +262,37 @@ export class Model {
257
262
  );
258
263
  }
259
264
 
265
+ /**
266
+ * Gives each object the root's shortest path to it as its owner, breadth first: an object in an array whose
267
+ * elements also refer to each other, as a chain does, is stored in the array, not inside its neighbour.
268
+ */
260
269
  private own(
261
270
  value: CapturedValue,
262
271
  owner: Owner,
263
272
  type: ts.Type | undefined,
264
273
  ): void {
265
- const node = this.node(value);
266
- if (!node || node.owner) {
267
- return;
268
- }
269
- node.owner = owner;
270
- node.type = type;
271
- const element =
272
- node.captured.kind === "array" && type
273
- ? this.elementType(type)
274
- : undefined;
275
- for (const [step, held] of children(node.captured)) {
276
- this.own(
277
- held,
278
- { root: owner.root, steps: [...owner.steps, step] },
279
- step.kind === "index"
280
- ? element
281
- : this.capturedFieldType(node, step.name),
282
- );
274
+ const queue = [{ owner, type, value }];
275
+ for (const next of queue) {
276
+ const node = this.node(next.value);
277
+ if (!node || node.owner) {
278
+ continue;
279
+ }
280
+ node.owner = next.owner;
281
+ node.type = next.type;
282
+ const element =
283
+ node.captured.kind === "array" && next.type
284
+ ? this.elementType(next.type)
285
+ : undefined;
286
+ for (const [step, held] of children(node.captured)) {
287
+ queue.push({
288
+ owner: { root: next.owner.root, steps: [...next.owner.steps, step] },
289
+ type:
290
+ step.kind === "index"
291
+ ? element
292
+ : this.capturedFieldType(node, step.name),
293
+ value: held,
294
+ });
295
+ }
283
296
  }
284
297
  }
285
298
  }
@@ -0,0 +1,206 @@
1
+ import ts from "typescript";
2
+ import { namesSdk } from "../sdk.ts";
3
+ import type { CapturedValue } from "./capture-protocol.ts";
4
+ import {
5
+ children,
6
+ type Model,
7
+ type ModelNode,
8
+ type Owner,
9
+ type Root,
10
+ tsPath,
11
+ } from "./model.ts";
12
+ import type { Storage } from "./storage.ts";
13
+ import { SpecializeError } from "./values.ts";
14
+
15
+ /** A declaration whose initializer retains a primitive: a module binding or a class field. */
16
+ type RetainedDeclaration = ts.VariableDeclaration | ts.PropertyDeclaration;
17
+
18
+ /** Whether a value of `type` has no identity setup can mark, so retaining it needs its declaration. */
19
+ const primitive = (type: ts.Type): boolean => {
20
+ if (type.isUnion()) {
21
+ return type.types.every(primitive);
22
+ }
23
+ if (type.isIntersection()) {
24
+ return type.types.some(primitive);
25
+ }
26
+ return !(type.flags & (ts.TypeFlags.Object | ts.TypeFlags.NonPrimitive));
27
+ };
28
+
29
+ /** The declaration a `retain` call initializes, when it retains a primitive there. */
30
+ const retainedDeclaration = (
31
+ call: ts.CallExpression,
32
+ ): RetainedDeclaration | undefined => {
33
+ const parent = call.parent;
34
+ if (ts.isPropertyDeclaration(parent) && parent.initializer === call) {
35
+ return parent;
36
+ }
37
+ if (
38
+ ts.isVariableDeclaration(parent) &&
39
+ parent.initializer === call &&
40
+ ts.isVariableDeclarationList(parent.parent) &&
41
+ ts.isVariableStatement(parent.parent.parent) &&
42
+ ts.isSourceFile(parent.parent.parent.parent)
43
+ ) {
44
+ return parent;
45
+ }
46
+ return undefined;
47
+ };
48
+
49
+ /**
50
+ * The declarations that retain a primitive. Setup marks the objects it retains as it runs, but a
51
+ * primitive has no identity, so its declaration is found here, in the source. Checked before setup
52
+ * runs, so a `retain` setup can't evaluate is reported where it is written.
53
+ */
54
+ export const retainedDeclarations = (
55
+ sources: readonly ts.SourceFile[],
56
+ checker: ts.TypeChecker,
57
+ sdk: string,
58
+ ): ReadonlySet<ts.Node> => {
59
+ const found = new Set<ts.Node>();
60
+ const visit = (node: ts.Node): void => {
61
+ if (
62
+ ts.isCallExpression(node) &&
63
+ namesSdk(checker, sdk, node.expression, "retain")
64
+ ) {
65
+ const symbol = checker.getSymbolAtLocation(node.expression);
66
+ if (!symbol || !(symbol.flags & ts.SymbolFlags.Alias)) {
67
+ throw new SpecializeError(
68
+ node,
69
+ 'Setup runs under Bun, where the global retain has no value: import { retain } from "@tsplc/compiler/retain".',
70
+ );
71
+ }
72
+ const [argument] = node.arguments;
73
+ if (argument && primitive(checker.getTypeAtLocation(argument))) {
74
+ const declaration = retainedDeclaration(node);
75
+ if (!declaration) {
76
+ throw new SpecializeError(
77
+ node,
78
+ "A primitive is retained only as a module let's or a class field's initializer; retain the record that holds it.",
79
+ );
80
+ }
81
+ found.add(declaration);
82
+ }
83
+ }
84
+ ts.forEachChild(node, visit);
85
+ };
86
+ for (const source of sources) {
87
+ visit(source);
88
+ }
89
+ return found;
90
+ };
91
+
92
+ /**
93
+ * Which storage globals are `RETAIN`. CODESYS retains whole variables, so a global is retained when
94
+ * it holds anything setup retained: an object it passed to `retain`, or a field or binding whose
95
+ * declaration retains a primitive. Every retained thing must end up in a retained global.
96
+ */
97
+ export class Retention {
98
+ private readonly objects: ReadonlySet<number>;
99
+ private readonly claimed = new Set<number | ts.Node>();
100
+
101
+ constructor(
102
+ private readonly model: Model,
103
+ private readonly storage: Storage,
104
+ private readonly declarations: ReadonlySet<ts.Node>,
105
+ ) {
106
+ this.objects = new Set(model.graph.retained);
107
+ }
108
+
109
+ /** Whether the global holding a root, or one field of a flattened root, is retained. */
110
+ global(root: Root, field: string | null): boolean {
111
+ const node = this.model.node(root.value);
112
+ if (field === null || !node) {
113
+ return this.claim(
114
+ this.declarations.has(root.declaration) ? root.declaration : null,
115
+ this.holds(root.value, { root, steps: [] }),
116
+ );
117
+ }
118
+ const member =
119
+ node.captured.kind === "instance"
120
+ ? this.model.member(this.model.classOf(node), field)
121
+ : undefined;
122
+ const value: CapturedValue = (node.captured.kind === "instance" ||
123
+ node.captured.kind === "object"
124
+ ? node.captured.fields[field]
125
+ : undefined) ?? { kind: "undefined" };
126
+ const held = this.holds(value, {
127
+ root,
128
+ steps: [{ kind: "field", name: field }],
129
+ });
130
+ const declared = member && this.declarations.has(member) ? member : null;
131
+ const whole = this.objects.has(node.id) ? node.id : null;
132
+ return [this.claim(declared, held), this.claim(whole, held)].some(Boolean);
133
+ }
134
+
135
+ /** Each retained thing no retained global holds, as the error that reports it. */
136
+ unclaimed(): Error[] {
137
+ const objects = [...this.objects]
138
+ .filter((id) => !this.claimed.has(id))
139
+ .map((id) => {
140
+ const owner = this.model.nodes[id]?.owner;
141
+ return new Error(
142
+ `${owner ? tsPath(owner) : "A retained object"}: retain retains nothing the PLC stores; retain state scan code writes, or exported data.`,
143
+ );
144
+ });
145
+ const declarations = [...this.declarations]
146
+ .filter((declaration) => !this.claimed.has(declaration))
147
+ .map(
148
+ (declaration) =>
149
+ new SpecializeError(
150
+ declaration,
151
+ "retain retains nothing the PLC stores; retain state scan code writes, or exported data.",
152
+ ),
153
+ );
154
+ return [...objects, ...declarations];
155
+ }
156
+
157
+ /** Claims a mark for the global being decided, and says whether that global is retained. */
158
+ private claim(mark: number | ts.Node | null, held: boolean): boolean {
159
+ if (mark === null) {
160
+ return held;
161
+ }
162
+ this.claimed.add(mark);
163
+ return true;
164
+ }
165
+
166
+ /**
167
+ * Whether what a global stores at `at` holds something retained, claiming each mark it finds.
168
+ * An object stored elsewhere is that place's to retain.
169
+ */
170
+ private holds(value: CapturedValue, at: Owner): boolean {
171
+ const node = this.model.node(value);
172
+ if (!node?.owner || node.owner.root !== at.root) {
173
+ return false;
174
+ }
175
+ if (tsPath(node.owner) !== tsPath(at)) {
176
+ return false;
177
+ }
178
+ const marks = [
179
+ ...(this.objects.has(node.id) && this.storage.hasState(node)
180
+ ? [node.id]
181
+ : []),
182
+ ...this.retainedMembers(node),
183
+ ];
184
+ for (const mark of marks) {
185
+ this.claimed.add(mark);
186
+ }
187
+ const inner = children(node.captured).map(([step, held]) =>
188
+ this.holds(held, { root: at.root, steps: [...at.steps, step] }),
189
+ );
190
+ return marks.length > 0 || inner.some(Boolean);
191
+ }
192
+
193
+ /** The fields of an instance the PLC stores whose declarations retain a primitive. */
194
+ private retainedMembers(node: ModelNode): ts.Node[] {
195
+ if (node.captured.kind !== "instance") {
196
+ return [];
197
+ }
198
+ return this.model
199
+ .fields(this.model.classOf(node))
200
+ .filter(
201
+ (member) =>
202
+ this.declarations.has(member) &&
203
+ this.storage.stored(node, member.name.getText()),
204
+ );
205
+ }
206
+ }
@@ -1,5 +1,6 @@
1
+ // biome-ignore-all lint/style/noNonNullAssertion: Retain existing non-null assertions during the tooling migration; changing their contracts requires a separate review.
1
2
  import ts from "typescript";
2
- import { from, joined, mapped, type Mapped } from "../positions.ts";
3
+ import { from, joined, type Mapped, mapped } from "../positions.ts";
3
4
  import { DialectText } from "./dialect.ts";
4
5
  import {
5
6
  ExpressionSpecializer,
@@ -11,14 +12,14 @@ import { Emitter, type Frame } from "./frame.ts";
11
12
  import { EntryImports } from "./imports.ts";
12
13
  import { Initializers } from "./initializers.ts";
13
14
  import { Layouts, unusedName } from "./layouts.ts";
14
- import { tsPath, type Model, type ModelNode, type Root } from "./model.ts";
15
+ import { type Model, type ModelNode, type Root, tsPath } from "./model.ts";
15
16
  import { StatementSpecializer } from "./statements.ts";
16
17
  import { Storage } from "./storage.ts";
17
18
  import {
18
19
  constant,
19
- SpecializeError,
20
20
  type Env,
21
21
  type FunctionLike,
22
+ SpecializeError,
22
23
  type Value,
23
24
  } from "./values.ts";
24
25
 
@@ -118,9 +119,9 @@ export class Specializer implements RoutineTable {
118
119
  signature && this.checker.getReturnTypeOfSignature(signature);
119
120
  const isAsync = Boolean(
120
121
  ts.canHaveModifiers(declaration) &&
121
- ts
122
- .getModifiers(declaration)
123
- ?.some((modifier) => modifier.kind === ts.SyntaxKind.AsyncKeyword),
122
+ ts
123
+ .getModifiers(declaration)
124
+ ?.some((modifier) => modifier.kind === ts.SyntaxKind.AsyncKeyword),
124
125
  );
125
126
  const frame: Frame = {
126
127
  emitter: new Emitter(declaration),