@tsplc/compiler 0.2.14 → 0.2.17
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.
- package/backends/codesys/debug.ts +509 -0
- package/backends/codesys/emit.ts +701 -9
- package/backends/codesys/http.ts +110 -34
- package/backends/codesys/index.ts +2 -1
- package/backends/codesys/io-paths.ts +3 -1
- package/backends/codesys/manifest.ts +312 -0
- package/backends/codesys/plan.ts +39 -9
- package/backends/codesys/publication.ts +2 -2
- package/backends/codesys/storage.ts +10 -3
- package/backends/codesys/task-access.ts +51 -1
- package/backends/codesys/telemetry.ts +593 -0
- package/backends/codesys/types.ts +2 -1
- package/dist/cli.js +190 -33
- package/dist/cli.js.map +6 -6
- package/dist/codesys.js +2148 -432
- package/dist/codesys.js.map +14 -11
- package/dist/elaborate-capture.js +7 -1
- package/dist/elaborate-capture.js.map +3 -3
- package/dist/index.js +877 -459
- package/dist/index.js.map +16 -15
- package/dist/ir.js +87 -23
- package/dist/ir.js.map +10 -9
- package/dist/project.js +42 -9
- package/dist/project.js.map +3 -3
- package/dist/telemetry.js +83 -0
- package/dist/telemetry.js.map +12 -0
- package/ir/core-model.ts +13 -0
- package/ir/core.ts +1 -0
- package/ir/errors.ts +20 -0
- package/ir/index.ts +1 -0
- package/ir/link.ts +17 -8
- package/ir/model.ts +39 -0
- package/ir/routine.ts +62 -29
- package/ir/types.ts +7 -1
- package/package.json +12 -6
- package/project/cli.ts +96 -6
- package/project/plant.ts +58 -9
- package/project/project.ts +56 -10
- package/src/frontend/async.ts +284 -195
- package/src/frontend/configuration.ts +7 -21
- package/src/frontend/const-functions.ts +44 -38
- package/src/frontend/elaborate/capture.ts +7 -0
- package/src/frontend/elaborate/index.ts +135 -61
- package/src/frontend/elaborate/layouts.ts +8 -0
- package/src/frontend/elaborate/model.ts +13 -0
- package/src/frontend/elaborate/specialize.ts +302 -111
- package/src/frontend/exposure.ts +28 -26
- package/src/frontend/positions.ts +272 -0
- package/src/frontend/promises.ts +52 -47
- package/src/frontend/services.ts +16 -26
- package/src/frontend/types.ts +14 -0
- package/src/frontend/typescript.ts +185 -65
- package/stdlib/telemetry.ts +72 -0
- package/types/telemetry.d.ts +44 -0
package/src/frontend/exposure.ts
CHANGED
|
@@ -3,11 +3,21 @@ import ts from "typescript";
|
|
|
3
3
|
import { createHash } from "node:crypto";
|
|
4
4
|
import { dirname } from "node:path";
|
|
5
5
|
import { error, type Diagnostic } from "@tsplc/compiler/ir";
|
|
6
|
+
import {
|
|
7
|
+
copied,
|
|
8
|
+
edited,
|
|
9
|
+
from,
|
|
10
|
+
joined,
|
|
11
|
+
mapped,
|
|
12
|
+
type Edit,
|
|
13
|
+
type Mapped,
|
|
14
|
+
type Positions,
|
|
15
|
+
} from "./positions.ts";
|
|
6
16
|
|
|
7
17
|
/** Telemetry is sampled by its authored task and published read-only by the backend. */
|
|
8
|
-
export const lowerExposure = (program: ts.Program, sdk: string) => {
|
|
18
|
+
export const lowerExposure = (program: ts.Program, sdk: string, positions: Positions) => {
|
|
9
19
|
const checker = program.getTypeChecker();
|
|
10
|
-
const sources = new Map<string,
|
|
20
|
+
const sources = new Map<string, Mapped>();
|
|
11
21
|
const diagnostics: Diagnostic[] = [];
|
|
12
22
|
const variables = new Map<string, { name: string; version: string }>();
|
|
13
23
|
const names = new Set<string>();
|
|
@@ -18,8 +28,8 @@ export const lowerExposure = (program: ts.Program, sdk: string) => {
|
|
|
18
28
|
};
|
|
19
29
|
for (const source of runtimeSources(program)) {
|
|
20
30
|
if (source.isDeclarationFile) continue;
|
|
21
|
-
const edits:
|
|
22
|
-
const extra:
|
|
31
|
+
const edits: Edit[] = [];
|
|
32
|
+
const extra: Mapped[] = [];
|
|
23
33
|
const visit = (node: ts.Node): void => {
|
|
24
34
|
if (
|
|
25
35
|
!ts.isCallExpression(node) ||
|
|
@@ -31,19 +41,8 @@ export const lowerExposure = (program: ts.Program, sdk: string) => {
|
|
|
31
41
|
ts.forEachChild(node, visit);
|
|
32
42
|
return;
|
|
33
43
|
}
|
|
34
|
-
const fail = (message: string) =>
|
|
35
|
-
|
|
36
|
-
position = source.getLineAndCharacterOfPosition(start);
|
|
37
|
-
diagnostics.push(
|
|
38
|
-
error("PLC1014", message, {
|
|
39
|
-
file: source.fileName,
|
|
40
|
-
start,
|
|
41
|
-
end: node.end,
|
|
42
|
-
line: position.line + 1,
|
|
43
|
-
column: position.character + 1,
|
|
44
|
-
}),
|
|
45
|
-
);
|
|
46
|
-
};
|
|
44
|
+
const fail = (message: string) =>
|
|
45
|
+
diagnostics.push(error("PLC1014", message, positions.node(node)));
|
|
47
46
|
const [label, values] = node.arguments;
|
|
48
47
|
if (
|
|
49
48
|
!label ||
|
|
@@ -74,7 +73,7 @@ export const lowerExposure = (program: ts.Program, sdk: string) => {
|
|
|
74
73
|
return;
|
|
75
74
|
}
|
|
76
75
|
names.add(label.text);
|
|
77
|
-
const fields: { name: string; type: string; value:
|
|
76
|
+
const fields: { name: string; type: string; value: Mapped }[] = [];
|
|
78
77
|
for (const property of values.properties) {
|
|
79
78
|
if (
|
|
80
79
|
!ts.isPropertyAssignment(property) ||
|
|
@@ -109,7 +108,7 @@ export const lowerExposure = (program: ts.Program, sdk: string) => {
|
|
|
109
108
|
);
|
|
110
109
|
return;
|
|
111
110
|
}
|
|
112
|
-
fields.push({ name: property.name.text, type: annotation, value: value
|
|
111
|
+
fields.push({ name: property.name.text, type: annotation, value: copied(value) });
|
|
113
112
|
}
|
|
114
113
|
if (!fields.length) {
|
|
115
114
|
fail("Expose at least one telemetry field.");
|
|
@@ -127,21 +126,24 @@ export const lowerExposure = (program: ts.Program, sdk: string) => {
|
|
|
127
126
|
)
|
|
128
127
|
.digest("hex");
|
|
129
128
|
extra.push(
|
|
130
|
-
|
|
129
|
+
from(
|
|
130
|
+
node,
|
|
131
|
+
`export interface ${record}{contractVersion:STRING<64>;${fields.map((field) => `${field.name}:${field.type}`).join(";")}}\nexport let ${name}:${record}=defaultValue<${record}>();`,
|
|
132
|
+
),
|
|
131
133
|
);
|
|
132
134
|
variables.set(`${source.fileName}:${name}`, { name: label.text, version });
|
|
133
135
|
edits.push({
|
|
134
136
|
start: node.getStart(source),
|
|
135
137
|
end: node.end,
|
|
136
|
-
text: `{${name}.contractVersion=${JSON.stringify(version)};${
|
|
138
|
+
text: mapped`{${name}.contractVersion=${JSON.stringify(version)};${joined(
|
|
139
|
+
fields.map((field) => mapped`${name}.${field.name}=${field.value};`),
|
|
140
|
+
"",
|
|
141
|
+
)}}`,
|
|
137
142
|
});
|
|
138
143
|
};
|
|
139
144
|
visit(source);
|
|
140
|
-
if (
|
|
141
|
-
|
|
142
|
-
for (const edit of edits.sort((a, b) => b.start - a.start))
|
|
143
|
-
text = text.slice(0, edit.start) + edit.text + text.slice(edit.end);
|
|
144
|
-
sources.set(source.fileName, text + "\n" + extra.join("\n"));
|
|
145
|
+
if (edits.length)
|
|
146
|
+
sources.set(source.fileName, mapped`${edited(source, edits)}\n${joined(extra, "\n")}`);
|
|
145
147
|
}
|
|
146
148
|
return { sources, diagnostics, variables };
|
|
147
149
|
};
|
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
import type { SourceSpan } from "@tsplc/compiler/ir";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Where a run of lowered text, `start`..`end`, came from. Copied text maps offset for offset
|
|
6
|
+
* onto `from`..`to` of `file`; generated text maps as a whole to the node it was generated from.
|
|
7
|
+
*/
|
|
8
|
+
interface Segment {
|
|
9
|
+
start: number;
|
|
10
|
+
end: number;
|
|
11
|
+
file: string;
|
|
12
|
+
from: number;
|
|
13
|
+
to: number;
|
|
14
|
+
copied: boolean;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** Text a lowering pass writes, with the source each part of it came from. Segments are in order. */
|
|
18
|
+
export class Mapped {
|
|
19
|
+
constructor(
|
|
20
|
+
readonly text: string,
|
|
21
|
+
readonly segments: readonly Segment[] = [],
|
|
22
|
+
) {}
|
|
23
|
+
|
|
24
|
+
toString(): string {
|
|
25
|
+
return this.text;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export type Part = string | number | Mapped;
|
|
30
|
+
|
|
31
|
+
const append = (segments: Segment[], text: Part, offset: number): void => {
|
|
32
|
+
if (text instanceof Mapped)
|
|
33
|
+
for (const segment of text.segments)
|
|
34
|
+
segments.push({ ...segment, start: segment.start + offset, end: segment.end + offset });
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
/** A template tag that concatenates like a template literal and keeps the origins of `Mapped` parts. */
|
|
38
|
+
export const mapped = (strings: TemplateStringsArray, ...parts: readonly Part[]): Mapped => {
|
|
39
|
+
let text = strings[0]!;
|
|
40
|
+
const segments: Segment[] = [];
|
|
41
|
+
parts.forEach((part, index) => {
|
|
42
|
+
append(segments, part, text.length);
|
|
43
|
+
text += `${part}${strings[index + 1]}`;
|
|
44
|
+
});
|
|
45
|
+
return new Mapped(text, segments);
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
export const joined = (parts: readonly Part[], separator: string): Mapped => {
|
|
49
|
+
let text = "";
|
|
50
|
+
const segments: Segment[] = [];
|
|
51
|
+
parts.forEach((part, index) => {
|
|
52
|
+
if (index) text += separator;
|
|
53
|
+
append(segments, part, text.length);
|
|
54
|
+
text += String(part);
|
|
55
|
+
});
|
|
56
|
+
return new Mapped(text, segments);
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
const fill = (text: Part, file: string, from: number, to: number): Mapped => {
|
|
60
|
+
const value = text instanceof Mapped ? text : new Mapped(String(text));
|
|
61
|
+
const segments: Segment[] = [];
|
|
62
|
+
let cursor = 0;
|
|
63
|
+
const gap = (end: number) => {
|
|
64
|
+
if (end > cursor) segments.push({ start: cursor, end, file, from, to, copied: false });
|
|
65
|
+
};
|
|
66
|
+
for (const segment of value.segments) {
|
|
67
|
+
gap(segment.start);
|
|
68
|
+
segments.push(segment);
|
|
69
|
+
cursor = segment.end;
|
|
70
|
+
}
|
|
71
|
+
gap(value.text.length);
|
|
72
|
+
return new Mapped(value.text, segments);
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
/** Text generated from a node: the parts without an origin of their own map to the node. */
|
|
76
|
+
export const from = (node: ts.Node, text: Part): Mapped => {
|
|
77
|
+
const source = node.getSourceFile();
|
|
78
|
+
return fill(text, source.fileName, node.getStart(source), node.end);
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
/** A node's source text, copied. */
|
|
82
|
+
export const copied = (node: ts.Node): Mapped => {
|
|
83
|
+
const source = node.getSourceFile();
|
|
84
|
+
const start = node.getStart(source);
|
|
85
|
+
return new Mapped(source.text.slice(start, node.end), [
|
|
86
|
+
{
|
|
87
|
+
start: 0,
|
|
88
|
+
end: node.end - start,
|
|
89
|
+
file: source.fileName,
|
|
90
|
+
from: start,
|
|
91
|
+
to: node.end,
|
|
92
|
+
copied: true,
|
|
93
|
+
},
|
|
94
|
+
]);
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
export const slice = (text: Mapped, start: number, end: number): Mapped => {
|
|
98
|
+
const segments: Segment[] = [];
|
|
99
|
+
for (const segment of text.segments) {
|
|
100
|
+
if (segment.end <= start || segment.start >= end) continue;
|
|
101
|
+
const first = Math.max(segment.start, start),
|
|
102
|
+
last = Math.min(segment.end, end);
|
|
103
|
+
segments.push({
|
|
104
|
+
...segment,
|
|
105
|
+
start: first - start,
|
|
106
|
+
end: last - start,
|
|
107
|
+
...(segment.copied && {
|
|
108
|
+
from: segment.from + first - segment.start,
|
|
109
|
+
to: segment.from + last - segment.start,
|
|
110
|
+
}),
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
return new Mapped(text.text.slice(start, end), segments);
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
export interface Edit {
|
|
117
|
+
start: number;
|
|
118
|
+
end: number;
|
|
119
|
+
text: Part;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Text with edits applied. Unedited text keeps its origin; the parts of an edit's text without an
|
|
124
|
+
* origin of their own map to the text the edit replaces. Edits at one position apply last first.
|
|
125
|
+
*/
|
|
126
|
+
export const edited = (base: ts.SourceFile | Mapped, edits: readonly Edit[]): Mapped => {
|
|
127
|
+
const text =
|
|
128
|
+
base instanceof Mapped
|
|
129
|
+
? base
|
|
130
|
+
: new Mapped(base.text, [
|
|
131
|
+
{
|
|
132
|
+
start: 0,
|
|
133
|
+
end: base.text.length,
|
|
134
|
+
file: base.fileName,
|
|
135
|
+
from: 0,
|
|
136
|
+
to: base.text.length,
|
|
137
|
+
copied: true,
|
|
138
|
+
},
|
|
139
|
+
]);
|
|
140
|
+
const ordered = edits
|
|
141
|
+
.map((edit, index) => ({ edit, index }))
|
|
142
|
+
.sort((left, right) => left.edit.start - right.edit.start || right.index - left.index);
|
|
143
|
+
const parts: Mapped[] = [];
|
|
144
|
+
let cursor = 0;
|
|
145
|
+
for (const { edit } of ordered) {
|
|
146
|
+
if (edit.start > cursor) parts.push(slice(text, cursor, edit.start));
|
|
147
|
+
const origin = resolve(text.segments, edit.start, edit.end);
|
|
148
|
+
parts.push(
|
|
149
|
+
origin ? fill(edit.text, origin.file, origin.from, origin.to) : mapped`${edit.text}`,
|
|
150
|
+
);
|
|
151
|
+
cursor = Math.max(cursor, edit.end);
|
|
152
|
+
}
|
|
153
|
+
parts.push(slice(text, cursor, text.text.length));
|
|
154
|
+
return joined(parts, "");
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
/** The first segment that ends after `offset`. */
|
|
158
|
+
const search = (segments: readonly Segment[], offset: number): number => {
|
|
159
|
+
let low = 0,
|
|
160
|
+
high = segments.length;
|
|
161
|
+
while (low < high) {
|
|
162
|
+
const middle = (low + high) >> 1;
|
|
163
|
+
if (segments[middle]!.end > offset) high = middle;
|
|
164
|
+
else low = middle + 1;
|
|
165
|
+
}
|
|
166
|
+
return low;
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
/** Where `start`..`end` of mapped text came from; a range across origins maps to its start's. */
|
|
170
|
+
const resolve = (
|
|
171
|
+
segments: readonly Segment[],
|
|
172
|
+
start: number,
|
|
173
|
+
end: number,
|
|
174
|
+
): { file: string; from: number; to: number } | undefined => {
|
|
175
|
+
const first = search(segments, start);
|
|
176
|
+
const head = segments[first];
|
|
177
|
+
if (!head || head.start >= Math.max(end, start + 1)) return undefined;
|
|
178
|
+
let last = end > start ? search(segments, end - 1) : first;
|
|
179
|
+
if (!segments[last] || segments[last]!.start >= end) last--;
|
|
180
|
+
const tail = last > first ? segments[last]! : head;
|
|
181
|
+
const from = head.copied ? head.from + Math.max(0, start - head.start) : head.from;
|
|
182
|
+
const until = (segment: Segment) =>
|
|
183
|
+
segment.copied ? segment.from + Math.min(end, segment.end) - segment.start : segment.to;
|
|
184
|
+
const to = until(tail);
|
|
185
|
+
if (tail.file === head.file && to >= from) return { file: head.file, from, to };
|
|
186
|
+
return { file: head.file, from, to: Math.max(from, until(head)) };
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
/** A span of a source as its text reads. */
|
|
190
|
+
export const sourceSpan = (source: ts.SourceFile, start: number, end: number): SourceSpan => {
|
|
191
|
+
const position = source.getLineAndCharacterOfPosition(start);
|
|
192
|
+
return {
|
|
193
|
+
file: source.fileName,
|
|
194
|
+
start,
|
|
195
|
+
end,
|
|
196
|
+
line: position.line + 1,
|
|
197
|
+
column: position.character + 1,
|
|
198
|
+
};
|
|
199
|
+
};
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Maps offsets in the sources the frontend reads, rewritten by any number of lowerings, back to the
|
|
203
|
+
* files the user wrote. Each lowering's output is composed with the maps of the text it read.
|
|
204
|
+
*/
|
|
205
|
+
export class Positions {
|
|
206
|
+
private readonly maps = new Map<string, readonly Segment[]>();
|
|
207
|
+
/** The user's files as written, for positions in files lowering has since rewritten. */
|
|
208
|
+
private readonly originals = new Map<string, ts.SourceFile>();
|
|
209
|
+
|
|
210
|
+
/** Records a lowering's rewritten and generated sources; `program` is the program it read. */
|
|
211
|
+
record(sources: ReadonlyMap<string, Mapped>, program: ts.Program): void {
|
|
212
|
+
// Every source of one lowering maps into the text it read, so compose them all before replacing any map.
|
|
213
|
+
const composed = new Map<string, Segment[]>();
|
|
214
|
+
for (const [file, text] of sources) {
|
|
215
|
+
const segments: Segment[] = [];
|
|
216
|
+
for (const segment of text.segments) {
|
|
217
|
+
const previous = this.maps.get(segment.file);
|
|
218
|
+
if (!previous) {
|
|
219
|
+
if (!this.originals.has(segment.file))
|
|
220
|
+
this.originals.set(segment.file, program.getSourceFile(segment.file)!);
|
|
221
|
+
segments.push(segment);
|
|
222
|
+
} else if (segment.copied) {
|
|
223
|
+
for (let index = search(previous, segment.from); index < previous.length; index++) {
|
|
224
|
+
const inner = previous[index]!;
|
|
225
|
+
if (inner.start >= segment.to) break;
|
|
226
|
+
const first = Math.max(inner.start, segment.from),
|
|
227
|
+
last = Math.min(inner.end, segment.to);
|
|
228
|
+
segments.push({
|
|
229
|
+
start: segment.start + first - segment.from,
|
|
230
|
+
end: segment.start + last - segment.from,
|
|
231
|
+
file: inner.file,
|
|
232
|
+
from: inner.copied ? inner.from + first - inner.start : inner.from,
|
|
233
|
+
to: inner.copied ? inner.from + last - inner.start : inner.to,
|
|
234
|
+
copied: inner.copied,
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
} else {
|
|
238
|
+
const origin = resolve(previous, segment.from, segment.to);
|
|
239
|
+
if (origin) segments.push({ ...segment, ...origin });
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
composed.set(file, segments);
|
|
243
|
+
}
|
|
244
|
+
for (const [file, segments] of composed) this.maps.set(file, segments);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/** The user's source and offsets that `start`..`end` of a source the frontend reads came from. */
|
|
248
|
+
private origin(source: ts.SourceFile, start: number, end: number) {
|
|
249
|
+
const segments = this.maps.get(source.fileName);
|
|
250
|
+
const origin = segments && resolve(segments, start, end);
|
|
251
|
+
if (!origin) return { source, from: start, to: end };
|
|
252
|
+
return { source: this.originals.get(origin.file)!, from: origin.from, to: origin.to };
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
readonly span = (source: ts.SourceFile, start: number, end: number): SourceSpan => {
|
|
256
|
+
const origin = this.origin(source, start, end);
|
|
257
|
+
return sourceSpan(origin.source, origin.from, origin.to);
|
|
258
|
+
};
|
|
259
|
+
|
|
260
|
+
readonly node = (node: ts.Node): SourceSpan => {
|
|
261
|
+
const source = node.getSourceFile();
|
|
262
|
+
return this.span(source, node.getStart(source), node.end);
|
|
263
|
+
};
|
|
264
|
+
|
|
265
|
+
/** The user's text a node came from, on one line and at most `limit` characters. */
|
|
266
|
+
excerpt(node: ts.Node, limit = 120): string {
|
|
267
|
+
const source = node.getSourceFile();
|
|
268
|
+
const origin = this.origin(source, node.getStart(source), node.end);
|
|
269
|
+
const text = origin.source.text.slice(origin.from, origin.to).replace(/\s+/g, " ").trim();
|
|
270
|
+
return text.length > limit ? `${text.slice(0, limit - 1)}…` : text;
|
|
271
|
+
}
|
|
272
|
+
}
|
package/src/frontend/promises.ts
CHANGED
|
@@ -2,29 +2,33 @@ import { runtimeSources } from "./module-graph.ts";
|
|
|
2
2
|
import ts from "typescript";
|
|
3
3
|
import { dirname, relative } from "node:path";
|
|
4
4
|
import { error, type Diagnostic } from "@tsplc/compiler/ir";
|
|
5
|
+
import {
|
|
6
|
+
edited,
|
|
7
|
+
from,
|
|
8
|
+
joined,
|
|
9
|
+
mapped,
|
|
10
|
+
type Edit,
|
|
11
|
+
type Mapped,
|
|
12
|
+
type Positions,
|
|
13
|
+
} from "./positions.ts";
|
|
5
14
|
|
|
6
15
|
type Routine = ts.FunctionDeclaration | ts.MethodDeclaration;
|
|
7
16
|
type Handler = ts.ArrowFunction | ts.FunctionExpression | Routine;
|
|
8
|
-
interface Edit {
|
|
9
|
-
start: number;
|
|
10
|
-
end: number;
|
|
11
|
-
text: string;
|
|
12
|
-
}
|
|
13
17
|
interface Part {
|
|
14
18
|
kind: "then" | "catch";
|
|
15
19
|
handlers: readonly ts.Expression[];
|
|
16
20
|
}
|
|
17
21
|
|
|
18
22
|
/** Normalize promise syntax into named coroutines; scheduling stays in async lowering. */
|
|
19
|
-
export const lowerPromises = (program: ts.Program, sdk: string) => {
|
|
23
|
+
export const lowerPromises = (program: ts.Program, sdk: string, positions: Positions) => {
|
|
20
24
|
const checker = program.getTypeChecker();
|
|
21
|
-
const sources = new Map<string,
|
|
25
|
+
const sources = new Map<string, Mapped>(),
|
|
22
26
|
diagnostics: Diagnostic[] = [];
|
|
23
27
|
const runtimeBlocks = new Map<string, { kind: "mailbox" }>();
|
|
24
28
|
const files = runtimeSources(program).filter((source) => !source.isDeclarationFile);
|
|
25
|
-
const additions = new Map<ts.SourceFile,
|
|
29
|
+
const additions = new Map<ts.SourceFile, Mapped[]>(),
|
|
26
30
|
edits = new Map<ts.SourceFile, Edit[]>();
|
|
27
|
-
const methods = new Map<ts.ClassDeclaration,
|
|
31
|
+
const methods = new Map<ts.ClassDeclaration, Mapped[]>();
|
|
28
32
|
let sequence = 0;
|
|
29
33
|
const symbol = (node: ts.Node): ts.Symbol | undefined => {
|
|
30
34
|
let value = checker.getSymbolAtLocation(node);
|
|
@@ -40,23 +44,17 @@ export const lowerPromises = (program: ts.Program, sdk: string) => {
|
|
|
40
44
|
)
|
|
41
45
|
);
|
|
42
46
|
};
|
|
43
|
-
const diagnosticFor = (node: ts.Node, message: string) =>
|
|
44
|
-
|
|
45
|
-
start = node.getStart(source),
|
|
46
|
-
position = source.getLineAndCharacterOfPosition(start);
|
|
47
|
-
return error("PLC1012", message, {
|
|
48
|
-
file: source.fileName,
|
|
49
|
-
start,
|
|
50
|
-
end: node.end,
|
|
51
|
-
line: position.line + 1,
|
|
52
|
-
column: position.character + 1,
|
|
53
|
-
});
|
|
54
|
-
};
|
|
47
|
+
const diagnosticFor = (node: ts.Node, message: string) =>
|
|
48
|
+
error("PLC1012", message, positions.node(node));
|
|
55
49
|
const fail: (node: ts.Node, message: string) => never = (node, message) => {
|
|
56
50
|
throw { promiseDiagnostic: diagnosticFor(node, message) };
|
|
57
51
|
};
|
|
58
|
-
|
|
59
|
-
|
|
52
|
+
/** Adds generated code to the end of a module, or to a class, mapped to the node it was made for. */
|
|
53
|
+
const append = (scope: ts.SourceFile | ts.ClassDeclaration, origin: ts.Node, text: string) => {
|
|
54
|
+
if (ts.isClassDeclaration(scope))
|
|
55
|
+
methods.set(scope, [...(methods.get(scope) ?? []), from(origin, text)]);
|
|
56
|
+
else additions.set(scope, [...(additions.get(scope) ?? []), from(origin, text)]);
|
|
57
|
+
};
|
|
60
58
|
const edit = (node: ts.Node, text: string) => {
|
|
61
59
|
const source = node.getSourceFile();
|
|
62
60
|
edits.set(source, [
|
|
@@ -146,7 +144,11 @@ export const lowerPromises = (program: ts.Program, sdk: string) => {
|
|
|
146
144
|
if (!alias) {
|
|
147
145
|
alias = `TsplcPromiseType${sequence++}`;
|
|
148
146
|
aliases.set(node, alias);
|
|
149
|
-
append(
|
|
147
|
+
append(
|
|
148
|
+
node.getSourceFile(),
|
|
149
|
+
node,
|
|
150
|
+
`export type ${alias}=${node.getText(node.getSourceFile())};`,
|
|
151
|
+
);
|
|
150
152
|
}
|
|
151
153
|
const path = relative(dirname(source.fileName), node.getSourceFile().fileName).replaceAll(
|
|
152
154
|
"\\",
|
|
@@ -158,6 +160,7 @@ export const lowerPromises = (program: ts.Program, sdk: string) => {
|
|
|
158
160
|
typeImports.set(source, importedAliases);
|
|
159
161
|
append(
|
|
160
162
|
source,
|
|
163
|
+
node,
|
|
161
164
|
`import type {${alias}} from ${JSON.stringify(path.startsWith(".") ? path : `./${path}`)};`,
|
|
162
165
|
);
|
|
163
166
|
return alias;
|
|
@@ -281,17 +284,19 @@ export const lowerPromises = (program: ts.Program, sdk: string) => {
|
|
|
281
284
|
}
|
|
282
285
|
return node.getChildren().some((child) => ts.isTypeNode(child) && hasBorrow(child, seen));
|
|
283
286
|
};
|
|
284
|
-
|
|
287
|
+
/** The first node that needs the shared diagnostics block, which its module declares. */
|
|
288
|
+
let diagnosticOwner: ts.Node | undefined;
|
|
285
289
|
const diag = () => {
|
|
286
|
-
if (!
|
|
290
|
+
if (!diagnosticOwner) throw new Error("Missing async diagnostics owner");
|
|
287
291
|
return "tsplcPromiseDiagnostics";
|
|
288
292
|
};
|
|
289
293
|
const imported = new Set<ts.SourceFile>();
|
|
290
|
-
const ensureImports = (source: ts.SourceFile) => {
|
|
294
|
+
const ensureImports = (source: ts.SourceFile, origin: ts.Node) => {
|
|
291
295
|
if (imported.has(source)) return;
|
|
292
296
|
imported.add(source);
|
|
293
297
|
append(
|
|
294
298
|
source,
|
|
299
|
+
origin,
|
|
295
300
|
`import {cooperativeTasks as tsplcCooperative,errorCode as tsplcErrorCode} from "@tsplc/compiler/async";import {wrapAdd as tsplcAsyncWrapAdd} from "@tsplc/compiler/types";`,
|
|
296
301
|
);
|
|
297
302
|
};
|
|
@@ -308,8 +313,8 @@ export const lowerPromises = (program: ts.Program, sdk: string) => {
|
|
|
308
313
|
ts.isMethodDeclaration(owner) && ts.isClassDeclaration(owner.parent)
|
|
309
314
|
? owner.parent
|
|
310
315
|
: undefined;
|
|
311
|
-
ensureImports(source);
|
|
312
|
-
|
|
316
|
+
ensureImports(source, expression);
|
|
317
|
+
diagnosticOwner ??= expression;
|
|
313
318
|
const captures = new Map<ts.Symbol, { name: string; type: string; argument: string }>();
|
|
314
319
|
const collectCaptures = (handler: Handler) => {
|
|
315
320
|
const visit = (node: ts.Node): void => {
|
|
@@ -436,8 +441,7 @@ export const lowerPromises = (program: ts.Program, sdk: string) => {
|
|
|
436
441
|
extra?: { type: string; argument: string },
|
|
437
442
|
) => {
|
|
438
443
|
const text = `${classOwner ? "private " : "export "}async ${classOwner ? "" : "function "}${name}(${[signature, extra ? `promiseInput:${extra.type}` : ""].filter(Boolean).join(",")}):Promise<${type}>{${body}}`;
|
|
439
|
-
|
|
440
|
-
else append(source, text);
|
|
444
|
+
append(classOwner ?? source, expression, text);
|
|
441
445
|
return `${classOwner ? "this." : ""}${name}(${[args, extra?.argument ?? ""].filter(Boolean).join(",")})`;
|
|
442
446
|
};
|
|
443
447
|
const finish = (call: string, type: string, discard = false) =>
|
|
@@ -557,8 +561,7 @@ export const lowerPromises = (program: ts.Program, sdk: string) => {
|
|
|
557
561
|
...params.map((value) => `${value.name}:${value.type}`),
|
|
558
562
|
].join(",");
|
|
559
563
|
const text = `${classOwner ? "private " : "export "}async ${classOwner ? "" : "function "}${name}(${handlerParams}):Promise<${type}>{${code}}`;
|
|
560
|
-
|
|
561
|
-
else append(source, text);
|
|
564
|
+
append(classOwner ?? source, declaration, text);
|
|
562
565
|
return {
|
|
563
566
|
call: `${classOwner ? "this." : ""}${name}(${[...(parameter && inputType !== "void" ? [input] : []), ...params.map((value) => value.name)].join(",")})`,
|
|
564
567
|
type,
|
|
@@ -613,8 +616,7 @@ export const lowerPromises = (program: ts.Program, sdk: string) => {
|
|
|
613
616
|
const site = `tsplcPromiseSite${sequence++}`;
|
|
614
617
|
const callable = prefix.slice(0, prefix.indexOf("("));
|
|
615
618
|
const declaration = `${classOwner ? "private " : "const "}${site}=tsplcCooperative.define(${callable});`;
|
|
616
|
-
|
|
617
|
-
else append(source, declaration);
|
|
619
|
+
append(classOwner ?? source, expression, declaration);
|
|
618
620
|
const scope = classOwner ?? source;
|
|
619
621
|
scopes.set(scope, [...(scopes.get(scope) ?? []), site]);
|
|
620
622
|
return `${classOwner ? "this." : ""}${site}.start(${params.map((value) => value.expression).join(",")});`;
|
|
@@ -635,7 +637,7 @@ export const lowerPromises = (program: ts.Program, sdk: string) => {
|
|
|
635
637
|
return;
|
|
636
638
|
}
|
|
637
639
|
if (ts.isCallExpression(node) && official(node.expression, "asyncDiagnostics")) {
|
|
638
|
-
|
|
640
|
+
diagnosticOwner ??= node;
|
|
639
641
|
diagnosticsCalls.push(node);
|
|
640
642
|
return;
|
|
641
643
|
}
|
|
@@ -703,16 +705,21 @@ export const lowerPromises = (program: ts.Program, sdk: string) => {
|
|
|
703
705
|
const body = (scopes.get(scope) ?? [])
|
|
704
706
|
.map((site) => `${ts.isClassDeclaration(scope) ? "this." : ""}${site}.cancel();`)
|
|
705
707
|
.join("");
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
708
|
+
append(
|
|
709
|
+
scope,
|
|
710
|
+
node,
|
|
711
|
+
ts.isClassDeclaration(scope)
|
|
712
|
+
? `private ${name}():void{${body}}`
|
|
713
|
+
: `function ${name}():void{${body}}`,
|
|
714
|
+
);
|
|
709
715
|
edit(node, `${ts.isClassDeclaration(scope) ? "this." : ""}${name}()`);
|
|
710
716
|
}
|
|
711
|
-
if (
|
|
712
|
-
const source =
|
|
713
|
-
ensureImports(source);
|
|
717
|
+
if (diagnosticOwner) {
|
|
718
|
+
const source = diagnosticOwner.getSourceFile();
|
|
719
|
+
ensureImports(source, diagnosticOwner);
|
|
714
720
|
append(
|
|
715
721
|
source,
|
|
722
|
+
diagnosticOwner,
|
|
716
723
|
`export class TsplcPromiseDiagnostics extends FunctionBlock<{}>{private count:UDINT=0;private lastError:UDINT=0;public report(code:UDINT,count:UDINT):BOOL{this.count=tsplcAsyncWrapAdd<UDINT>(this.count,count);this.lastError=code;return true;}public snapshot():{valid:BOOL;count:UDINT;lastError:UDINT}{return {valid:true,count:this.count,lastError:this.lastError};}protected execute(input:{}):void{}}
|
|
717
724
|
export const tsplcPromiseDiagnostics=new TsplcPromiseDiagnostics();`,
|
|
718
725
|
);
|
|
@@ -724,6 +731,7 @@ export const tsplcPromiseDiagnostics=new TsplcPromiseDiagnostics();`,
|
|
|
724
731
|
if (file !== source) {
|
|
725
732
|
const path = relative(dirname(file.fileName), source.fileName).replaceAll("\\", "/");
|
|
726
733
|
append(
|
|
734
|
+
file,
|
|
727
735
|
file,
|
|
728
736
|
`import {${diag()}} from ${JSON.stringify(path.startsWith(".") ? path : `./${path}`)};`,
|
|
729
737
|
);
|
|
@@ -735,17 +743,14 @@ export const tsplcPromiseDiagnostics=new TsplcPromiseDiagnostics();`,
|
|
|
735
743
|
const source = owner.getSourceFile();
|
|
736
744
|
edits.set(source, [
|
|
737
745
|
...(edits.get(source) ?? []),
|
|
738
|
-
{ start: owner.end - 1, end: owner.end - 1, text:
|
|
746
|
+
{ start: owner.end - 1, end: owner.end - 1, text: mapped`\n${joined(texts, "\n")}` },
|
|
739
747
|
]);
|
|
740
748
|
}
|
|
741
749
|
for (const source of files) {
|
|
742
750
|
const changes = edits.get(source) ?? [],
|
|
743
751
|
extra = additions.get(source) ?? [];
|
|
744
752
|
if (!changes.length && !extra.length) continue;
|
|
745
|
-
|
|
746
|
-
for (const change of changes.sort((left, right) => right.start - left.start))
|
|
747
|
-
text = text.slice(0, change.start) + change.text + text.slice(change.end);
|
|
748
|
-
sources.set(source.fileName, text + "\n" + extra.join("\n"));
|
|
753
|
+
sources.set(source.fileName, mapped`${edited(source, changes)}\n${joined(extra, "\n")}`);
|
|
749
754
|
}
|
|
750
755
|
return { sources, diagnostics, runtimeBlocks };
|
|
751
756
|
};
|