@tsplc/compiler 0.2.14 → 0.2.16
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/async.ts
CHANGED
|
@@ -4,23 +4,55 @@ import { dirname, resolve, relative } from "node:path";
|
|
|
4
4
|
import { cooperativeSite, taskSite } from "./cooperative.ts";
|
|
5
5
|
import { staticString } from "./configuration.ts";
|
|
6
6
|
import { error, type Diagnostic, type FunctionBlock, type PlcProgram } from "@tsplc/compiler/ir";
|
|
7
|
+
import {
|
|
8
|
+
copied,
|
|
9
|
+
edited,
|
|
10
|
+
from,
|
|
11
|
+
joined,
|
|
12
|
+
mapped,
|
|
13
|
+
slice,
|
|
14
|
+
type Edit,
|
|
15
|
+
type Mapped,
|
|
16
|
+
type Part,
|
|
17
|
+
type Positions,
|
|
18
|
+
} from "./positions.ts";
|
|
19
|
+
|
|
20
|
+
type Job = NonNullable<FunctionBlock["job"]>;
|
|
7
21
|
|
|
8
22
|
export interface AsyncLowering {
|
|
9
|
-
sources: Map<string,
|
|
23
|
+
sources: Map<string, Mapped>;
|
|
10
24
|
blocks: Map<string, NonNullable<FunctionBlock["runtime"]>>;
|
|
25
|
+
/** The async function each generated block runs, by `<file>:<block>`. */
|
|
26
|
+
jobs: Map<string, Job>;
|
|
11
27
|
programs: Record<string, { module: string; export: string }>;
|
|
12
28
|
schedules: Map<string, NonNullable<PlcProgram["background"]>>;
|
|
13
29
|
diagnostics: Diagnostic[];
|
|
14
30
|
continuations: Map<string, NonNullable<PlcProgram["continuation"]>>;
|
|
15
31
|
}
|
|
16
32
|
|
|
17
|
-
|
|
33
|
+
/** Statements that end in a semicolon. */
|
|
34
|
+
const terminated = (node: ts.Node) =>
|
|
35
|
+
ts.isExpressionStatement(node) ||
|
|
36
|
+
ts.isVariableStatement(node) ||
|
|
37
|
+
ts.isReturnStatement(node) ||
|
|
38
|
+
ts.isThrowStatement(node) ||
|
|
39
|
+
ts.isBreakOrContinueStatement(node);
|
|
40
|
+
|
|
41
|
+
/** The call an await waits on. */
|
|
42
|
+
const awaitedCall = (node: ts.Node) => (ts.isAwaitExpression(node) ? node.expression : node);
|
|
43
|
+
|
|
44
|
+
const copyValue = (value: Part, aggregate: boolean) => (aggregate ? mapped`copy(${value})` : value);
|
|
18
45
|
|
|
19
46
|
/** Lower suspension into ordinary typed blocks before the PLC semantic pass. */
|
|
20
|
-
export const lowerAsync = (
|
|
47
|
+
export const lowerAsync = (
|
|
48
|
+
program: ts.Program,
|
|
49
|
+
sdk: string,
|
|
50
|
+
positions: Positions,
|
|
51
|
+
): AsyncLowering => {
|
|
21
52
|
const result: AsyncLowering = {
|
|
22
53
|
sources: new Map(),
|
|
23
54
|
blocks: new Map(),
|
|
55
|
+
jobs: new Map(),
|
|
24
56
|
programs: {},
|
|
25
57
|
schedules: new Map(),
|
|
26
58
|
diagnostics: [],
|
|
@@ -39,18 +71,8 @@ export const lowerAsync = (program: ts.Program, sdk: string): AsyncLowering => {
|
|
|
39
71
|
s.declarations?.some((d) => d.getSourceFile().fileName.startsWith(dirname(sdk) + "/"))
|
|
40
72
|
);
|
|
41
73
|
};
|
|
42
|
-
const diagnosticFor = (node: ts.Node, message: string): Diagnostic =>
|
|
43
|
-
|
|
44
|
-
const start = node.getStart(source);
|
|
45
|
-
const position = source.getLineAndCharacterOfPosition(start);
|
|
46
|
-
return error("PLC1010", message, {
|
|
47
|
-
file: source.fileName,
|
|
48
|
-
start,
|
|
49
|
-
end: node.end,
|
|
50
|
-
line: position.line + 1,
|
|
51
|
-
column: position.character + 1,
|
|
52
|
-
});
|
|
53
|
-
};
|
|
74
|
+
const diagnosticFor = (node: ts.Node, message: string): Diagnostic =>
|
|
75
|
+
error("PLC1010", message, positions.node(node));
|
|
54
76
|
const fail: (node: ts.Node, message: string) => never = (node, message) => {
|
|
55
77
|
throw { asyncDiagnostic: diagnosticFor(node, message) };
|
|
56
78
|
};
|
|
@@ -89,7 +111,6 @@ export const lowerAsync = (program: ts.Program, sdk: string): AsyncLowering => {
|
|
|
89
111
|
if (!functions.size) return result;
|
|
90
112
|
const aliasPrefix = (fn: Routine) =>
|
|
91
113
|
`TsplcAsync${fn.name!.getText(fn.getSourceFile())}${fn.getStart(fn.getSourceFile())}`;
|
|
92
|
-
const printer = ts.createPrinter({ removeComments: false });
|
|
93
114
|
let sequence = 0;
|
|
94
115
|
const ownedContinuations = new Map<ts.Symbol, string[]>();
|
|
95
116
|
const ownedJobs = new Map<
|
|
@@ -99,8 +120,8 @@ export const lowerAsync = (program: ts.Program, sdk: string): AsyncLowering => {
|
|
|
99
120
|
for (const source of runtimeSources(program)) {
|
|
100
121
|
if (source.isDeclarationFile) continue;
|
|
101
122
|
try {
|
|
102
|
-
const replacements:
|
|
103
|
-
extra:
|
|
123
|
+
const replacements: Edit[] = [],
|
|
124
|
+
extra: Mapped[] = [];
|
|
104
125
|
const text = (node: ts.Node) => node.getText(source);
|
|
105
126
|
for (const fn of functions.values()) {
|
|
106
127
|
if (fn.getSourceFile() !== source) continue;
|
|
@@ -199,7 +220,8 @@ export const lowerAsync = (program: ts.Program, sdk: string): AsyncLowering => {
|
|
|
199
220
|
called.getSourceFile().fileName,
|
|
200
221
|
).replaceAll("\\", "/");
|
|
201
222
|
const statement = `import type {${type}} from ${JSON.stringify(path.startsWith(".") ? path : `./${path}`)};`;
|
|
202
|
-
if (!extra.
|
|
223
|
+
if (!extra.some((item) => item.text === statement))
|
|
224
|
+
extra.push(from(node, statement));
|
|
203
225
|
}
|
|
204
226
|
}
|
|
205
227
|
}
|
|
@@ -214,78 +236,80 @@ export const lowerAsync = (program: ts.Program, sdk: string): AsyncLowering => {
|
|
|
214
236
|
ts.forEachChild(node, collect);
|
|
215
237
|
};
|
|
216
238
|
collect(fn.body);
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
),
|
|
253
|
-
);
|
|
254
|
-
}
|
|
255
|
-
return ts.visitEachChild(n, visit, context);
|
|
256
|
-
};
|
|
257
|
-
return ts.visitNode(root, visit) as typeof root;
|
|
258
|
-
},
|
|
259
|
-
]);
|
|
260
|
-
const value = printer.printNode(
|
|
261
|
-
ts.EmitHint.Unspecified,
|
|
262
|
-
transformed.transformed[0]!,
|
|
263
|
-
source,
|
|
239
|
+
// The user's text with locals moved onto the block, so positions in it map exactly.
|
|
240
|
+
const rewrite = (node: ts.Node): Mapped => {
|
|
241
|
+
const edits: Edit[] = [];
|
|
242
|
+
const visit = (n: ts.Node): void => {
|
|
243
|
+
const replacement = (() => {
|
|
244
|
+
if (
|
|
245
|
+
method &&
|
|
246
|
+
ts.isPropertyAccessExpression(n) &&
|
|
247
|
+
n.expression.kind === ts.SyntaxKind.ThisKeyword
|
|
248
|
+
)
|
|
249
|
+
return `this[${JSON.stringify(n.name.text)}]`;
|
|
250
|
+
if (
|
|
251
|
+
ts.isIdentifier(n) &&
|
|
252
|
+
!(ts.isPropertyAccessExpression(n.parent) && n.parent.name === n) &&
|
|
253
|
+
!(ts.isPropertyAssignment(n.parent) && n.parent.name === n)
|
|
254
|
+
) {
|
|
255
|
+
const field = fields.get(symbol(n)!);
|
|
256
|
+
if (field) return `this.${field.name}`;
|
|
257
|
+
}
|
|
258
|
+
if (ts.isShorthandPropertyAssignment(n)) {
|
|
259
|
+
const value = checker.getShorthandAssignmentValueSymbol(n),
|
|
260
|
+
field = value && fields.get(value);
|
|
261
|
+
if (field) return `${n.name.text}: this.${field.name}`;
|
|
262
|
+
}
|
|
263
|
+
return undefined;
|
|
264
|
+
})();
|
|
265
|
+
if (replacement === undefined) ts.forEachChild(n, visit);
|
|
266
|
+
else edits.push({ start: n.getStart(source), end: n.end, text: replacement });
|
|
267
|
+
};
|
|
268
|
+
visit(node);
|
|
269
|
+
const text = copied(node);
|
|
270
|
+
const start = node.getStart(source);
|
|
271
|
+
const result = edited(
|
|
272
|
+
text,
|
|
273
|
+
edits.map((edit) => ({ ...edit, start: edit.start - start, end: edit.end - start })),
|
|
264
274
|
);
|
|
265
|
-
|
|
266
|
-
return
|
|
275
|
+
// A statement written without its semicolon is followed by more code here.
|
|
276
|
+
return terminated(node) && !result.text.endsWith(";") ? mapped`${result};` : result;
|
|
267
277
|
};
|
|
268
|
-
const nodes = new Map<number,
|
|
278
|
+
const nodes = new Map<number, Mapped>();
|
|
269
279
|
const yielding = new Set<number>();
|
|
280
|
+
const steps: Job["steps"][number][] = [];
|
|
270
281
|
let nextId = 1;
|
|
271
|
-
|
|
282
|
+
/** A step of the job: `body` runs while it is current; `origin` is the source it runs. */
|
|
283
|
+
const state = (
|
|
284
|
+
body: Part,
|
|
285
|
+
origin: ts.Node,
|
|
286
|
+
kind: Job["steps"][number]["kind"],
|
|
287
|
+
yields = false,
|
|
288
|
+
) => {
|
|
272
289
|
const id = nextId++;
|
|
273
|
-
nodes.set(id, body);
|
|
290
|
+
nodes.set(id, from(origin, body));
|
|
291
|
+
steps.push({
|
|
292
|
+
step: id,
|
|
293
|
+
kind,
|
|
294
|
+
span: positions.node(origin),
|
|
295
|
+
text: positions.excerpt(kind === "await" ? awaitedCall(origin) : origin),
|
|
296
|
+
});
|
|
274
297
|
if (yields) yielding.add(id);
|
|
275
298
|
return id;
|
|
276
299
|
};
|
|
277
300
|
const children: string[] = [],
|
|
278
301
|
resets: string[] = [],
|
|
279
302
|
cancels: string[] = [];
|
|
280
|
-
const finish = (value:
|
|
281
|
-
`this.value = ${copyValue(value, resultAggregate)}; this.busy = false; this.done = true; this.step = 0;`;
|
|
282
|
-
const failure = (error:
|
|
283
|
-
`this.error = ${error}; if(this.error === 0)this.error=4; this.busy = false; this.done = true; this.step = 0;`;
|
|
303
|
+
const finish = (value: Part) =>
|
|
304
|
+
mapped`this.value = ${copyValue(value, resultAggregate)}; this.busy = false; this.done = true; this.step = 0;`;
|
|
305
|
+
const failure = (error: Part) =>
|
|
306
|
+
mapped`this.error = ${error}; if(this.error === 0)this.error=4; this.busy = false; this.done = true; this.step = 0;`;
|
|
307
|
+
type Reject = (error: Part, cancellable?: boolean) => Part;
|
|
284
308
|
const awaitState = (
|
|
285
309
|
awaited: ts.AwaitExpression | ts.CallExpression,
|
|
286
310
|
target: string | undefined,
|
|
287
311
|
next: number,
|
|
288
|
-
reject:
|
|
312
|
+
reject: Reject,
|
|
289
313
|
): number => {
|
|
290
314
|
const expression = ts.isAwaitExpression(awaited) ? awaited.expression : awaited;
|
|
291
315
|
if (!ts.isCallExpression(expression))
|
|
@@ -301,10 +325,14 @@ export const lowerAsync = (program: ts.Program, sdk: string): AsyncLowering => {
|
|
|
301
325
|
children.push(`private ${child} = new OnDelayTimer();`);
|
|
302
326
|
resets.push(`this.${child}.update({enabled:false,duration:ms(0)});`);
|
|
303
327
|
const poll = state(
|
|
304
|
-
`this.${child}.update({enabled:true,duration:${rewrite(call.arguments[0]!)}}); if(this.${child}.out.done) { this.step = ${next}; }`,
|
|
328
|
+
mapped`this.${child}.update({enabled:true,duration:${rewrite(call.arguments[0]!)}}); if(this.${child}.out.done) { this.step = ${next}; }`,
|
|
329
|
+
awaited,
|
|
330
|
+
"await",
|
|
305
331
|
);
|
|
306
332
|
return state(
|
|
307
|
-
`this.${child}.update({enabled:true,duration:${rewrite(call.arguments[0]!)}});this.step=${poll};`,
|
|
333
|
+
mapped`this.${child}.update({enabled:true,duration:${rewrite(call.arguments[0]!)}});this.step=${poll};`,
|
|
334
|
+
awaited,
|
|
335
|
+
"await",
|
|
308
336
|
true,
|
|
309
337
|
);
|
|
310
338
|
}
|
|
@@ -353,7 +381,7 @@ export const lowerAsync = (program: ts.Program, sdk: string): AsyncLowering => {
|
|
|
353
381
|
const responseType = text(call.typeArguments[0]!),
|
|
354
382
|
httpName = `Tsplc${rpc ? "Rpc" : "Http"}${sequence++}`;
|
|
355
383
|
let requestType = `${httpName}Request`;
|
|
356
|
-
if (!json) extra.push(`interface ${requestType} {reserved:BOOL}`);
|
|
384
|
+
if (!json) extra.push(from(call, `interface ${requestType} {reserved:BOOL}`));
|
|
357
385
|
if (json) {
|
|
358
386
|
if (call.typeArguments[1]) requestType = text(call.typeArguments[1]);
|
|
359
387
|
else {
|
|
@@ -371,9 +399,14 @@ export const lowerAsync = (program: ts.Program, sdk: string): AsyncLowering => {
|
|
|
371
399
|
);
|
|
372
400
|
}
|
|
373
401
|
}
|
|
374
|
-
extra.push(
|
|
402
|
+
extra.push(
|
|
403
|
+
from(
|
|
404
|
+
call,
|
|
405
|
+
`interface ${httpName}Input {start:BOOL;cancel:BOOL;request:${requestType}}
|
|
375
406
|
interface ${httpName}Output {busy:BOOL;done:BOOL;error:UDINT;result:${responseType}}
|
|
376
|
-
class ${httpName} extends FunctionBlock<${httpName}Input,${httpName}Output> { protected execute(input:${httpName}Input):${httpName}Output {return {busy:false,done:false,error:0,result:defaultValue<${responseType}>()};} }
|
|
407
|
+
class ${httpName} extends FunctionBlock<${httpName}Input,${httpName}Output> { protected execute(input:${httpName}Input):${httpName}Output {return {busy:false,done:false,error:0,result:defaultValue<${responseType}>()};} }`,
|
|
408
|
+
),
|
|
409
|
+
);
|
|
377
410
|
result.blocks.set(
|
|
378
411
|
`${source.fileName}:${httpName}`,
|
|
379
412
|
rpc
|
|
@@ -393,7 +426,9 @@ class ${httpName} extends FunctionBlock<${httpName}Input,${httpName}Output> { pr
|
|
|
393
426
|
`this.${child}.update({start:false,cancel:true,request:defaultValue<${requestType}>()});`,
|
|
394
427
|
);
|
|
395
428
|
return state(
|
|
396
|
-
`this.${child}.update({start:true,cancel:false,request:${json ? rewrite(json) : `defaultValue<${requestType}>()`}}); if(this.${child}.out.done) { if(this.${child}.out.error !== 0) {${reject(`this.${child}.out.error`, true)}} else {${target ? `${target} = ${copyValue(`this.${child}.out.result`, Boolean(checker.getAwaitedType(checker.getTypeAtLocation(call))!.flags & ts.TypeFlags.Object))};` : ""}this.step = ${next};} }`,
|
|
429
|
+
mapped`this.${child}.update({start:true,cancel:false,request:${json ? rewrite(json) : `defaultValue<${requestType}>()`}}); if(this.${child}.out.done) { if(this.${child}.out.error !== 0) {${reject(`this.${child}.out.error`, true)}} else {${target ? mapped`${target} = ${copyValue(`this.${child}.out.result`, Boolean(checker.getAwaitedType(checker.getTypeAtLocation(call))!.flags & ts.TypeFlags.Object))};` : ""}this.step = ${next};} }`,
|
|
430
|
+
awaited,
|
|
431
|
+
"await",
|
|
397
432
|
);
|
|
398
433
|
}
|
|
399
434
|
const jobDeclaration = symbol(name)?.declarations?.find(
|
|
@@ -419,10 +454,14 @@ class ${httpName} extends FunctionBlock<${httpName}Input,${httpName}Output> { pr
|
|
|
419
454
|
`if(this.${borrow}){${receiver}.cancel();const borrowed=${receiver}.snapshot();if(!borrowed.valid||borrowed.busy)return;if(!${receiver}.reset())return;this.${borrow}=false;}`,
|
|
420
455
|
);
|
|
421
456
|
const poll = state(
|
|
422
|
-
`const borrowed=${receiver}.snapshot();if(borrowed.valid&&borrowed.done){if(${receiver}.reset()){this.${borrow}=false;if(borrowed.error!==0){${reject("borrowed.error", true)}}else{${target ? `${target}=${copyValue("borrowed.result", Boolean(checker.getAwaitedType(checker.getTypeAtLocation(call))!.flags & ts.TypeFlags.Object))};` : ""}this.step=${next};}}}`,
|
|
457
|
+
mapped`const borrowed=${receiver}.snapshot();if(borrowed.valid&&borrowed.done){if(${receiver}.reset()){this.${borrow}=false;if(borrowed.error!==0){${reject("borrowed.error", true)}}else{${target ? mapped`${target}=${copyValue("borrowed.result", Boolean(checker.getAwaitedType(checker.getTypeAtLocation(call))!.flags & ts.TypeFlags.Object))};` : ""}this.step=${next};}}}`,
|
|
458
|
+
awaited,
|
|
459
|
+
"await",
|
|
423
460
|
);
|
|
424
461
|
return state(
|
|
425
|
-
`const available=${receiver}.snapshot();if(!available.valid)return;if(available.busy||available.done){${reject("7")}}else{const accepted:BOOL=${receiver}.start(${call.arguments.map(rewrite)
|
|
462
|
+
mapped`const available=${receiver}.snapshot();if(!available.valid)return;if(available.busy||available.done){${reject("7")}}else{const accepted:BOOL=${receiver}.start(${joined(call.arguments.map(rewrite), ",")});if(accepted){this.${borrow}=true;this.step=${poll};}}`,
|
|
463
|
+
awaited,
|
|
464
|
+
"await",
|
|
426
465
|
true,
|
|
427
466
|
);
|
|
428
467
|
}
|
|
@@ -442,10 +481,14 @@ class ${httpName} extends FunctionBlock<${httpName}Input,${httpName}Output> { pr
|
|
|
442
481
|
`if(this.${borrow}){${receiver}.${calledMethod.advance}({cancel:true});const cancellingChild=${receiver}.${calledMethod.snapshot}();if(cancellingChild.busy)return;${receiver}.${calledMethod.reset}();this.${borrow}=false;}`,
|
|
443
482
|
);
|
|
444
483
|
const poll = state(
|
|
445
|
-
`${receiver}.${calledMethod.advance}({cancel:false});const childState=${receiver}.${calledMethod.snapshot}();if(childState.done){${receiver}.${calledMethod.reset}();this.${borrow}=false;if(childState.error!==0){${reject("childState.error", true)}}else{${target ? `${target}=${copyValue("childState.result", Boolean(checker.getAwaitedType(checker.getTypeAtLocation(call))!.flags & ts.TypeFlags.Object))};` : ""}this.step=${next};}}`,
|
|
484
|
+
mapped`${receiver}.${calledMethod.advance}({cancel:false});const childState=${receiver}.${calledMethod.snapshot}();if(childState.done){${receiver}.${calledMethod.reset}();this.${borrow}=false;if(childState.error!==0){${reject("childState.error", true)}}else{${target ? mapped`${target}=${copyValue("childState.result", Boolean(checker.getAwaitedType(checker.getTypeAtLocation(call))!.flags & ts.TypeFlags.Object))};` : ""}this.step=${next};}}`,
|
|
485
|
+
awaited,
|
|
486
|
+
"await",
|
|
446
487
|
);
|
|
447
488
|
return state(
|
|
448
|
-
`const accepted:BOOL=${receiver}.${calledMethod.start}(${call.arguments.map(rewrite)
|
|
489
|
+
mapped`const accepted:BOOL=${receiver}.${calledMethod.start}(${joined(call.arguments.map(rewrite), ",")});if(!accepted){${reject("7")}}else{this.${borrow}=true;${receiver}.${calledMethod.advance}({cancel:false});this.step=${poll};}`,
|
|
490
|
+
awaited,
|
|
491
|
+
"await",
|
|
449
492
|
true,
|
|
450
493
|
);
|
|
451
494
|
}
|
|
@@ -457,17 +500,21 @@ class ${httpName} extends FunctionBlock<${httpName}Input,${httpName}Output> { pr
|
|
|
457
500
|
`this.${child}.update({cancel:true});const cancellingChild=this.${child}.snapshot();if(cancellingChild.busy)return;`,
|
|
458
501
|
);
|
|
459
502
|
const poll = state(
|
|
460
|
-
`this.${child}.update({cancel:false}); const childState = this.${child}.snapshot(); if(childState.done) {if(childState.error !== 0) {${reject("childState.error", true)}} else {${target ? `${target} = ${copyValue("childState.result", Boolean(checker.getAwaitedType(checker.getTypeAtLocation(call))!.flags & ts.TypeFlags.Object))};` : ""}this.step = ${next};} }`,
|
|
503
|
+
mapped`this.${child}.update({cancel:false}); const childState = this.${child}.snapshot(); if(childState.done) {if(childState.error !== 0) {${reject("childState.error", true)}} else {${target ? mapped`${target} = ${copyValue("childState.result", Boolean(checker.getAwaitedType(checker.getTypeAtLocation(call))!.flags & ts.TypeFlags.Object))};` : ""}this.step = ${next};} }`,
|
|
504
|
+
awaited,
|
|
505
|
+
"await",
|
|
461
506
|
);
|
|
462
507
|
return state(
|
|
463
|
-
`const accepted:BOOL = this.${child}.start(${call.arguments.map(rewrite)
|
|
508
|
+
mapped`const accepted:BOOL = this.${child}.start(${joined(call.arguments.map(rewrite), ",")}); if(accepted){this.${child}.update({cancel:false});this.step = ${poll};}`,
|
|
509
|
+
awaited,
|
|
510
|
+
"await",
|
|
464
511
|
true,
|
|
465
512
|
);
|
|
466
513
|
};
|
|
467
514
|
const block = (
|
|
468
515
|
statements: readonly ts.Statement[],
|
|
469
516
|
next: number,
|
|
470
|
-
reject:
|
|
517
|
+
reject: Reject = failure,
|
|
471
518
|
): number => {
|
|
472
519
|
let entry = next;
|
|
473
520
|
for (const stmt of [...statements].reverse()) {
|
|
@@ -479,7 +526,9 @@ class ${httpName} extends FunctionBlock<${httpName}Input,${httpName}Output> { pr
|
|
|
479
526
|
const yes = block([stmt.thenStatement], entry, reject),
|
|
480
527
|
no = stmt.elseStatement ? block([stmt.elseStatement], entry, reject) : entry;
|
|
481
528
|
entry = state(
|
|
482
|
-
`if(${rewrite(stmt.expression)}) this.step = ${yes}; else this.step = ${no};`,
|
|
529
|
+
mapped`if(${rewrite(stmt.expression)}) this.step = ${yes}; else this.step = ${no};`,
|
|
530
|
+
stmt.expression,
|
|
531
|
+
"statement",
|
|
483
532
|
);
|
|
484
533
|
continue;
|
|
485
534
|
}
|
|
@@ -490,9 +539,9 @@ class ${httpName} extends FunctionBlock<${httpName}Input,${httpName}Output> { pr
|
|
|
490
539
|
const declaration = stmt.catchClause.variableDeclaration;
|
|
491
540
|
const field = declaration && fields.get(symbol(declaration.name)!);
|
|
492
541
|
entry = block(stmt.tryBlock.statements, entry, (error, cancellable) => {
|
|
493
|
-
const recover = `${field ? `this.${field.name} = ${error};` : ""}this.step = ${caught};`;
|
|
542
|
+
const recover = mapped`${field ? mapped`this.${field.name} = ${error};` : ""}this.step = ${caught};`;
|
|
494
543
|
return cancellable
|
|
495
|
-
? `if(${error} === 1) {${failure(error)}} else {${recover}}`
|
|
544
|
+
? mapped`if(${error} === 1) {${failure(error)}} else {${recover}}`
|
|
496
545
|
: recover;
|
|
497
546
|
});
|
|
498
547
|
continue;
|
|
@@ -505,13 +554,13 @@ class ${httpName} extends FunctionBlock<${httpName}Input,${httpName}Output> { pr
|
|
|
505
554
|
)
|
|
506
555
|
)
|
|
507
556
|
fail(stmt, "Async throw requires a numeric error code.");
|
|
508
|
-
entry = state(reject(rewrite(stmt.expression)));
|
|
557
|
+
entry = state(reject(rewrite(stmt.expression)), stmt, "throw");
|
|
509
558
|
continue;
|
|
510
559
|
}
|
|
511
560
|
if (ts.isReturnStatement(stmt)) {
|
|
512
561
|
if (!stmt.expression) {
|
|
513
562
|
if (!returnsVoid) fail(stmt, "Async routines must return a value.");
|
|
514
|
-
entry = state(finish("true"));
|
|
563
|
+
entry = state(finish("true"), stmt, "return");
|
|
515
564
|
continue;
|
|
516
565
|
}
|
|
517
566
|
const adopted =
|
|
@@ -525,10 +574,10 @@ class ${httpName} extends FunctionBlock<${httpName}Input,${httpName}Output> { pr
|
|
|
525
574
|
? awaitState(
|
|
526
575
|
suspension,
|
|
527
576
|
returnsVoid ? undefined : "this.value",
|
|
528
|
-
state(finish(returnsVoid ? "true" : "this.value")),
|
|
577
|
+
state(finish(returnsVoid ? "true" : "this.value"), stmt, "return"),
|
|
529
578
|
reject,
|
|
530
579
|
)
|
|
531
|
-
: state(finish(rewrite(stmt.expression)));
|
|
580
|
+
: state(finish(rewrite(stmt.expression)), stmt, "return");
|
|
532
581
|
continue;
|
|
533
582
|
}
|
|
534
583
|
if (ts.isVariableStatement(stmt)) {
|
|
@@ -539,7 +588,9 @@ class ${httpName} extends FunctionBlock<${httpName}Input,${httpName}Output> { pr
|
|
|
539
588
|
entry = ts.isAwaitExpression(dec.initializer)
|
|
540
589
|
? awaitState(dec.initializer, target, entry, reject)
|
|
541
590
|
: state(
|
|
542
|
-
`${target} = ${copyValue(rewrite(dec.initializer), field.aggregate)}; this.step = ${entry};`,
|
|
591
|
+
mapped`${target} = ${copyValue(rewrite(dec.initializer), field.aggregate)}; this.step = ${entry};`,
|
|
592
|
+
dec,
|
|
593
|
+
"statement",
|
|
543
594
|
);
|
|
544
595
|
}
|
|
545
596
|
continue;
|
|
@@ -555,31 +606,45 @@ class ${httpName} extends FunctionBlock<${httpName}Input,${httpName}Output> { pr
|
|
|
555
606
|
);
|
|
556
607
|
if (ts.isWhileStatement(stmt) || ts.isDoStatement(stmt))
|
|
557
608
|
fail(stmt, "Unbounded loops are not supported in async routines.");
|
|
558
|
-
entry = state(`${rewrite(stmt)} this.step = ${entry}
|
|
609
|
+
entry = state(mapped`${rewrite(stmt)} this.step = ${entry};`, stmt, "statement");
|
|
559
610
|
}
|
|
560
611
|
return entry;
|
|
561
612
|
};
|
|
562
|
-
|
|
613
|
+
// Running off the end of the body: a void routine returns, any other fails.
|
|
614
|
+
const end = fn.body.getLastToken(source)!;
|
|
615
|
+
const entry = block(
|
|
616
|
+
fn.body.statements,
|
|
617
|
+
returnsVoid ? state(finish("true"), end, "return") : state(failure("4"), end, "throw"),
|
|
618
|
+
);
|
|
563
619
|
const exported = fn.modifiers?.some((m) => m.kind === ts.SyntaxKind.ExportKeyword)
|
|
564
620
|
? "export "
|
|
565
621
|
: "";
|
|
566
622
|
const aliases = `export type ${aliasPrefix(fn)}Result = ${returnType};\n${parameters.map((p, i) => `export type ${aliasPrefix(fn)}Argument${i} = ${p.type};`).join("\n")}`;
|
|
567
|
-
const generated = `${aliases}\n${exported}class ${fn.name!.getText(source)} extends FunctionBlock<{cancel:BOOL}> {
|
|
623
|
+
const generated = mapped`${aliases}\n${exported}class ${fn.name!.getText(source)} extends FunctionBlock<{cancel:BOOL}> {
|
|
568
624
|
private step:UINT=0; private cancelling:BOOL=false; private busy:BOOL=false; private done:BOOL=false; private error:UDINT=0; private value:${returnType}=defaultValue<${returnType}>();
|
|
569
625
|
${[...fields.values()].map((f) => `private ${f.name}:${f.type}=defaultValue<${f.type}>();`).join("\n")}
|
|
570
626
|
${children.join("\n")}
|
|
571
627
|
start(${parameters.map((p) => `${p.name}:${p.type}`).join(",")}):BOOL {if(this.busy || this.done)return false; ${parameters.map((p) => `this.${p.field} = ${copyValue(p.name, p.aggregate)};`).join(" ")} ${resets.join(" ")} this.value = defaultValue<${returnType}>(); this.error=0;this.cancelling=false;this.busy=true;this.done=false;this.step=${entry};return true;}
|
|
572
628
|
reset():void {${resets.join(" ")} this.busy=false;this.cancelling=false;this.done=false;this.error=0;this.step=0;}
|
|
573
629
|
snapshot():{busy:BOOL;done:BOOL;error:UDINT;result:${returnType}} {return {busy:this.busy,done:this.done,error:this.error,result:${copyValue("this.value", resultAggregate)}};}
|
|
574
|
-
protected execute(input:{cancel:BOOL}):void {if(!this.busy)return;if(input.cancel)this.cancelling=true;if(this.cancelling){${cancels.map((cancel) => `{${cancel}}`).join(" ")}${failure("1")}return;}for(let transition:DINT=0;transition<${nodes.size + 1};transition++){const previous:UINT=this.step;switch(this.step){${
|
|
630
|
+
protected execute(input:{cancel:BOOL}):void {if(!this.busy)return;if(input.cancel)this.cancelling=true;if(this.cancelling){${cancels.map((cancel) => `{${cancel}}`).join(" ")}${failure("1")}return;}for(let transition:DINT=0;transition<${nodes.size + 1};transition++){const previous:UINT=this.step;switch(this.step){${joined(
|
|
631
|
+
[...nodes].map(([id, body]) => mapped`case ${id}: {${body}} break;`),
|
|
632
|
+
"\n",
|
|
633
|
+
)} }if(!this.busy||this.step===previous${[...yielding].map((id) => `||previous===${id}`).join("")})return;}}
|
|
575
634
|
|
|
576
635
|
}`;
|
|
577
|
-
if (!method)
|
|
636
|
+
if (!method) {
|
|
578
637
|
replacements.push({ start: fn.getStart(source), end: fn.end, text: generated });
|
|
579
|
-
|
|
638
|
+
result.jobs.set(`${source.fileName}:${fn.name.getText(source)}`, {
|
|
639
|
+
name: fn.name.getText(source),
|
|
640
|
+
span: positions.node(fn),
|
|
641
|
+
steps,
|
|
642
|
+
});
|
|
643
|
+
} else {
|
|
644
|
+
// The block's members join the method's class, renamed apart from its own members.
|
|
580
645
|
const parsed = ts.createSourceFile(
|
|
581
646
|
source.fileName,
|
|
582
|
-
generated,
|
|
647
|
+
generated.text,
|
|
583
648
|
ts.ScriptTarget.Latest,
|
|
584
649
|
true,
|
|
585
650
|
);
|
|
@@ -594,70 +659,61 @@ protected execute(input:{cancel:BOOL}):void {if(!this.busy)return;if(input.cance
|
|
|
594
659
|
names.set("reset", method.reset);
|
|
595
660
|
names.set("snapshot", method.snapshot);
|
|
596
661
|
names.set("execute", method.advance);
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
)
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
return ts.visitEachChild(node, visit, context);
|
|
648
|
-
};
|
|
649
|
-
return ts.visitNode(root, visit) as typeof root;
|
|
650
|
-
},
|
|
651
|
-
]);
|
|
652
|
-
const generatedClass = transformed.transformed[0]!;
|
|
662
|
+
// Only the members remain, one per line.
|
|
663
|
+
const members = worker.members;
|
|
664
|
+
const edits: Edit[] = [
|
|
665
|
+
...members.map((member, index) => ({
|
|
666
|
+
start: index ? members[index - 1]!.end : 0,
|
|
667
|
+
end: member.getStart(parsed),
|
|
668
|
+
text: index ? "\n" : "",
|
|
669
|
+
})),
|
|
670
|
+
{ start: members.at(-1)!.end, end: generated.text.length, text: "" },
|
|
671
|
+
];
|
|
672
|
+
const visit = (node: ts.Node): void => {
|
|
673
|
+
if (
|
|
674
|
+
ts.isElementAccessExpression(node) &&
|
|
675
|
+
node.expression.kind === ts.SyntaxKind.ThisKeyword &&
|
|
676
|
+
ts.isStringLiteral(node.argumentExpression)
|
|
677
|
+
) {
|
|
678
|
+
edits.push({
|
|
679
|
+
start: node.getStart(parsed),
|
|
680
|
+
end: node.end,
|
|
681
|
+
text: `this.${node.argumentExpression.text}`,
|
|
682
|
+
});
|
|
683
|
+
return;
|
|
684
|
+
}
|
|
685
|
+
const renamed =
|
|
686
|
+
(ts.isPropertyAccessExpression(node) &&
|
|
687
|
+
node.expression.kind === ts.SyntaxKind.ThisKeyword) ||
|
|
688
|
+
ts.isPropertyDeclaration(node) ||
|
|
689
|
+
ts.isMethodDeclaration(node)
|
|
690
|
+
? node.name
|
|
691
|
+
: undefined;
|
|
692
|
+
if (renamed && ts.isIdentifier(renamed) && names.has(renamed.text)) {
|
|
693
|
+
const name = names.get(renamed.text)!;
|
|
694
|
+
// Generated methods are called from the owner's other members.
|
|
695
|
+
const modifiers = ts.isMethodDeclaration(node) ? node.modifiers : undefined;
|
|
696
|
+
if (modifiers?.length)
|
|
697
|
+
edits.push({
|
|
698
|
+
start: modifiers[0]!.getStart(parsed),
|
|
699
|
+
end: modifiers.at(-1)!.end,
|
|
700
|
+
text: "public",
|
|
701
|
+
});
|
|
702
|
+
edits.push({
|
|
703
|
+
start: renamed.getStart(parsed),
|
|
704
|
+
end: renamed.end,
|
|
705
|
+
text: ts.isMethodDeclaration(node) && !modifiers?.length ? `public ${name}` : name,
|
|
706
|
+
});
|
|
707
|
+
if (ts.isPropertyAccessExpression(node)) return;
|
|
708
|
+
}
|
|
709
|
+
ts.forEachChild(node, visit);
|
|
710
|
+
};
|
|
711
|
+
visit(worker);
|
|
653
712
|
replacements.push({
|
|
654
713
|
start: fn.getStart(source),
|
|
655
714
|
end: fn.end,
|
|
656
|
-
text:
|
|
657
|
-
.map((member) => printer.printNode(ts.EmitHint.Unspecified, member, parsed))
|
|
658
|
-
.join("\n"),
|
|
715
|
+
text: edited(generated, edits),
|
|
659
716
|
});
|
|
660
|
-
transformed.dispose();
|
|
661
717
|
}
|
|
662
718
|
}
|
|
663
719
|
const workTypes = (fn: Routine, dec: ts.VariableDeclaration | ts.PropertyDeclaration) => {
|
|
@@ -702,7 +758,10 @@ protected execute(input:{cancel:BOOL}):void {if(!this.busy)return;if(input.cance
|
|
|
702
758
|
"/",
|
|
703
759
|
);
|
|
704
760
|
extra.push(
|
|
705
|
-
|
|
761
|
+
from(
|
|
762
|
+
dec,
|
|
763
|
+
`import type {${valueType},${params.map((p) => p.type).join(",")}} from ${JSON.stringify(path.startsWith(".") ? path : `./${path}`)};`,
|
|
764
|
+
),
|
|
706
765
|
);
|
|
707
766
|
}
|
|
708
767
|
const valueAggregate = Boolean(promised.flags & ts.TypeFlags.Object);
|
|
@@ -777,15 +836,23 @@ protected execute(input:{cancel:BOOL}):void {if(!this.busy)return;if(input.cance
|
|
|
777
836
|
} else {
|
|
778
837
|
const name = `TsplcTaskSite${sequence++}`,
|
|
779
838
|
entry = `tsplcContinuation${sequence++}`;
|
|
780
|
-
extra.push(
|
|
781
|
-
|
|
839
|
+
extra.push(
|
|
840
|
+
from(
|
|
841
|
+
dec,
|
|
842
|
+
`class ${name} extends FunctionBlock<{}>{${site.body}protected execute(input:{}):void{}}
|
|
843
|
+
export function ${entry}():void{${dec.name.text}.poll();}`,
|
|
844
|
+
),
|
|
845
|
+
);
|
|
782
846
|
replacements.push({
|
|
783
847
|
start: init.getStart(source),
|
|
784
848
|
end: init.end,
|
|
785
849
|
text: `new ${name}()`,
|
|
786
850
|
});
|
|
787
851
|
result.programs[entry] = { module: resolve(source.fileName), export: entry };
|
|
788
|
-
result.continuations.set(entry, {
|
|
852
|
+
result.continuations.set(entry, {
|
|
853
|
+
instance: dec.name.text,
|
|
854
|
+
file: positions.node(dec).file,
|
|
855
|
+
});
|
|
789
856
|
}
|
|
790
857
|
continue;
|
|
791
858
|
}
|
|
@@ -852,15 +919,23 @@ export function ${entry}():void{${dec.name.text}.poll();}`);
|
|
|
852
919
|
} else {
|
|
853
920
|
const name = `TsplcCooperativeSite${sequence++}`,
|
|
854
921
|
entry = `tsplcContinuation${sequence++}`;
|
|
855
|
-
extra.push(
|
|
856
|
-
|
|
922
|
+
extra.push(
|
|
923
|
+
from(
|
|
924
|
+
dec,
|
|
925
|
+
`class ${name} extends FunctionBlock<{}>{${site.body}protected execute(input:{}):void{}}
|
|
926
|
+
export function ${entry}():void{${dec.name.text}.poll();}`,
|
|
927
|
+
),
|
|
928
|
+
);
|
|
857
929
|
replacements.push({
|
|
858
930
|
start: init.getStart(source),
|
|
859
931
|
end: init.end,
|
|
860
932
|
text: `new ${name}()`,
|
|
861
933
|
});
|
|
862
934
|
result.programs[entry] = { module: resolve(source.fileName), export: entry };
|
|
863
|
-
result.continuations.set(entry, {
|
|
935
|
+
result.continuations.set(entry, {
|
|
936
|
+
instance: dec.name.text,
|
|
937
|
+
file: positions.node(dec).file,
|
|
938
|
+
});
|
|
864
939
|
}
|
|
865
940
|
continue;
|
|
866
941
|
}
|
|
@@ -883,7 +958,10 @@ export function ${entry}():void{${dec.name.text}.poll();}`);
|
|
|
883
958
|
const requestType = `{ready:BOOL;${params.map((p) => `${p.field}:${p.type}`).join(";")}}`;
|
|
884
959
|
const snapshotType = `{valid:BOOL;busy:BOOL;done:BOOL;cancelRequested:BOOL;error:UDINT;result:${valueType}}`;
|
|
885
960
|
const emptySnapshot = `{valid:true,busy:this.pending || this.active,done:this.completed,cancelRequested:this.cancelRequested,error:this.failure,result:${copyValue("this.value", valueAggregate)}}`;
|
|
886
|
-
extra.push(
|
|
961
|
+
extra.push(
|
|
962
|
+
from(
|
|
963
|
+
dec,
|
|
964
|
+
`class ${mailbox} extends FunctionBlock<{}> {
|
|
887
965
|
private pending:BOOL=false;private active:BOOL=false;private completed:BOOL=false;private cancelRequested:BOOL=false;private failure:UDINT=0;private value:${valueType}=defaultValue<${valueType}>();
|
|
888
966
|
${params.map((p) => `private ${p.field}:${p.type}=defaultValue<${p.type}>();`).join("\n")}
|
|
889
967
|
start(${params.map((p) => `${p.name}:${p.type}`).join(",")}):BOOL {if(this.pending || this.active || this.completed)return false;${params.map((p) => `this.${p.field}=${copyValue(p.name, p.aggregate)};`).join("")}this.pending=true;return true;}
|
|
@@ -894,7 +972,9 @@ cancel():BOOL {if(this.pending){this.pending=false;this.completed=true;this.fail
|
|
|
894
972
|
reset():BOOL {if(this.pending || this.active)return false;this.completed=false;this.failure=0;this.cancelRequested=false;this.value=defaultValue<${valueType}>();return true;}
|
|
895
973
|
protected execute(input:{}):void {}
|
|
896
974
|
}
|
|
897
|
-
|
|
975
|
+
`,
|
|
976
|
+
),
|
|
977
|
+
);
|
|
898
978
|
const receiver = owner ? `this.${dec.name.text}` : dec.name.text;
|
|
899
979
|
const workerReceiver = owner ? `this.${worker}` : worker;
|
|
900
980
|
const run = `const request=${receiver}.take();if(request.ready){const accepted:BOOL=${workerReceiver}.start(${params.map((p) => `request.${p.field}`).join(",")});}const control=${receiver}.snapshot();if(!control.valid)return;${workerReceiver}.update({cancel:control.cancelRequested});const state=${workerReceiver}.snapshot();if(state.done){const published:BOOL=${receiver}.publish(state.result,state.error);if(published)${workerReceiver}.reset();}`;
|
|
@@ -903,12 +983,18 @@ protected execute(input:{}):void {}
|
|
|
903
983
|
replacements.push({
|
|
904
984
|
start: owner.end - 1,
|
|
905
985
|
end: owner.end - 1,
|
|
906
|
-
text:
|
|
986
|
+
text: from(
|
|
987
|
+
dec,
|
|
988
|
+
`private ${worker}=new ${text(init.arguments[0]!)}();public ${entry}():void{${run}}`,
|
|
989
|
+
),
|
|
907
990
|
});
|
|
908
991
|
result.blocks.set(`${source.fileName}:${owner.name.text}`, { kind: "backgroundHost" });
|
|
909
992
|
} else
|
|
910
993
|
extra.push(
|
|
911
|
-
|
|
994
|
+
from(
|
|
995
|
+
dec,
|
|
996
|
+
`const ${worker}=new ${text(init.arguments[0]!)}();export function ${entry}():void{${run}}`,
|
|
997
|
+
),
|
|
912
998
|
);
|
|
913
999
|
result.blocks.set(`${source.fileName}:${mailbox}`, { kind: "mailbox" });
|
|
914
1000
|
const opts = new Map(
|
|
@@ -965,24 +1051,21 @@ protected execute(input:{}):void {}
|
|
|
965
1051
|
});
|
|
966
1052
|
}
|
|
967
1053
|
if (replacements.length) {
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
changed = changed.slice(0, edit.start) + edit.text + changed.slice(edit.end);
|
|
1054
|
+
// Generated classes go first, other declarations last.
|
|
1055
|
+
const added = joined(extra, "\n");
|
|
971
1056
|
const generated = ts.createSourceFile(
|
|
972
1057
|
source.fileName,
|
|
973
|
-
|
|
1058
|
+
added.text,
|
|
974
1059
|
ts.ScriptTarget.Latest,
|
|
975
1060
|
true,
|
|
976
1061
|
);
|
|
977
|
-
const
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
.join("\n");
|
|
985
|
-
result.sources.set(source.fileName, before + "\n" + changed + "\n" + after);
|
|
1062
|
+
const part = (node: ts.Node) => slice(added, node.getStart(generated), node.end);
|
|
1063
|
+
const before = generated.statements.filter(ts.isClassDeclaration).map(part);
|
|
1064
|
+
const after = generated.statements.filter((n) => !ts.isClassDeclaration(n)).map(part);
|
|
1065
|
+
result.sources.set(
|
|
1066
|
+
source.fileName,
|
|
1067
|
+
mapped`${joined(before, "\n")}\n${edited(source, replacements)}\n${joined(after, "\n")}`,
|
|
1068
|
+
);
|
|
986
1069
|
}
|
|
987
1070
|
} catch (cause) {
|
|
988
1071
|
if (typeof cause === "object" && cause !== null && "asyncDiagnostic" in cause)
|
|
@@ -992,7 +1075,7 @@ protected execute(input:{}):void {}
|
|
|
992
1075
|
}
|
|
993
1076
|
for (const source of runtimeSources(program)) {
|
|
994
1077
|
if (source.isDeclarationFile) continue;
|
|
995
|
-
const entries:
|
|
1078
|
+
const entries: Mapped[] = [];
|
|
996
1079
|
// `new Block()` allocates one instance; `defaultValue<FixedArray<Block, N, Lower>>()` one per element.
|
|
997
1080
|
const allocated = (node: ts.Node) => {
|
|
998
1081
|
if (ts.isNewExpression(node)) return { block: node.expression };
|
|
@@ -1037,16 +1120,22 @@ protected execute(input:{}):void {}
|
|
|
1037
1120
|
for (const poll of polls ?? []) {
|
|
1038
1121
|
const entry = `tsplcContinuation${sequence++}`;
|
|
1039
1122
|
entries.push(
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1123
|
+
from(
|
|
1124
|
+
node,
|
|
1125
|
+
instance.first === undefined
|
|
1126
|
+
? `export function ${entry}():void{${name}.${poll}();}`
|
|
1127
|
+
: `export function ${entry}():void{for(let element:DINT=${instance.first};element<=${instance.last};element++){${name}[element].${poll}();}}`,
|
|
1128
|
+
),
|
|
1043
1129
|
);
|
|
1044
1130
|
result.programs[entry] = { module: resolve(source.fileName), export: entry };
|
|
1045
|
-
result.continuations.set(entry, {
|
|
1131
|
+
result.continuations.set(entry, {
|
|
1132
|
+
instance: name,
|
|
1133
|
+
file: positions.node(declaration).file,
|
|
1134
|
+
});
|
|
1046
1135
|
}
|
|
1047
1136
|
for (const job of jobs ?? []) {
|
|
1048
1137
|
const entry = `tsplcBackground${sequence++}`;
|
|
1049
|
-
entries.push(`export function ${entry}():void{${name}.${job.method}();}`);
|
|
1138
|
+
entries.push(from(node, `export function ${entry}():void{${name}.${job.method}();}`));
|
|
1050
1139
|
result.programs[entry] = { module: resolve(source.fileName), export: entry };
|
|
1051
1140
|
result.schedules.set(entry, { ...job.schedule, worker: name });
|
|
1052
1141
|
}
|
|
@@ -1058,7 +1147,7 @@ protected execute(input:{}):void {}
|
|
|
1058
1147
|
if (entries.length)
|
|
1059
1148
|
result.sources.set(
|
|
1060
1149
|
source.fileName,
|
|
1061
|
-
|
|
1150
|
+
mapped`${result.sources.get(source.fileName) ?? edited(source, [])}\n${joined(entries, "\n")}`,
|
|
1062
1151
|
);
|
|
1063
1152
|
}
|
|
1064
1153
|
return result;
|