@tsplc/compiler 0.2.17 → 0.2.18
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/README.md +2 -2
- package/dist/cli.js +920 -943
- package/dist/cli.js.map +13 -10
- package/dist/codesys.js +3893 -3352
- package/dist/codesys.js.map +29 -20
- package/dist/elaborate-capture.js +33 -17
- package/dist/elaborate-capture.js.map +3 -3
- package/dist/index.js +8787 -6855
- package/dist/index.js.map +40 -20
- package/dist/ir.js +1575 -1072
- package/dist/ir.js.map +21 -17
- package/dist/math.js.map +2 -2
- package/dist/project.js +154 -60
- package/dist/project.js.map +5 -5
- package/dist/telemetry.js +27 -9
- package/dist/telemetry.js.map +5 -5
- package/package.json +112 -112
- package/src/codesys/config.ts +50 -0
- package/src/codesys/debug.ts +670 -0
- package/src/codesys/emit.ts +142 -0
- package/src/codesys/engineering.ts +109 -0
- package/src/codesys/expressions.ts +92 -0
- package/src/codesys/global-lists.ts +250 -0
- package/src/codesys/global-names.ts +68 -0
- package/src/codesys/http.ts +890 -0
- package/src/codesys/index.ts +30 -0
- package/src/codesys/instrumentation.ts +430 -0
- package/src/codesys/io-paths.ts +140 -0
- package/src/codesys/manifest.ts +643 -0
- package/src/codesys/names.ts +81 -0
- package/src/codesys/numeric.ts +409 -0
- package/src/codesys/operands.ts +62 -0
- package/src/codesys/plan.ts +605 -0
- package/src/codesys/programs.ts +53 -0
- package/src/codesys/publication.ts +171 -0
- package/src/codesys/routines.ts +603 -0
- package/src/codesys/sources.ts +71 -0
- package/src/codesys/st-files.ts +115 -0
- package/src/codesys/storage.ts +97 -0
- package/src/codesys/symbols.ts +220 -0
- package/src/codesys/task-access.ts +320 -0
- package/src/codesys/telemetry.ts +732 -0
- package/src/codesys/types.ts +250 -0
- package/src/codesys/vendor.ts +150 -0
- package/src/frontend/analyzer.ts +794 -0
- package/src/frontend/async.ts +317 -1137
- package/src/frontend/blocks.ts +537 -403
- package/src/frontend/configuration.ts +176 -144
- package/src/frontend/const-functions.ts +183 -148
- package/src/frontend/cooperative.ts +73 -43
- package/src/frontend/coroutines.ts +1032 -0
- package/src/frontend/elaborate/calls.ts +535 -0
- package/src/frontend/elaborate/capture.ts +157 -101
- package/src/frontend/elaborate/dialect.ts +257 -0
- package/src/frontend/elaborate/expressions.ts +992 -0
- package/src/frontend/elaborate/frame.ts +53 -0
- package/src/frontend/elaborate/graph.ts +116 -71
- package/src/frontend/elaborate/imports.ts +70 -0
- package/src/frontend/elaborate/index.ts +435 -331
- package/src/frontend/elaborate/initializers.ts +198 -0
- package/src/frontend/elaborate/layouts.ts +591 -384
- package/src/frontend/elaborate/model.ts +264 -169
- package/src/frontend/elaborate/specialize.ts +343 -2586
- package/src/frontend/elaborate/statements.ts +606 -0
- package/src/frontend/elaborate/storage.ts +546 -0
- package/src/frontend/elaborate/values.ts +58 -0
- package/src/frontend/elaborate/writes.ts +100 -0
- package/src/frontend/exposure.ts +172 -140
- package/src/frontend/expressions.ts +1383 -0
- package/src/frontend/intrinsics.ts +337 -0
- package/src/frontend/module-graph.ts +89 -59
- package/src/frontend/positions.ts +271 -200
- package/src/frontend/promise-chains.ts +897 -0
- package/src/frontend/promises.ts +390 -747
- package/src/frontend/routines.ts +181 -0
- package/src/frontend/sdk.ts +82 -0
- package/src/frontend/services.ts +506 -372
- package/src/frontend/statements.ts +855 -0
- package/src/frontend/tasks.ts +471 -0
- package/src/frontend/telemetry.ts +174 -0
- package/src/frontend/types.ts +845 -551
- package/src/frontend/typescript.ts +323 -2543
- package/src/index.ts +5 -5
- package/src/ir/aggregates.ts +275 -0
- package/src/ir/allocation.ts +84 -0
- package/src/ir/core-model.ts +146 -0
- package/src/ir/declarations.ts +295 -0
- package/src/ir/diagnostics.ts +61 -0
- package/src/ir/domains.ts +152 -0
- package/src/ir/fold.ts +342 -0
- package/src/ir/index.ts +90 -0
- package/{ir/errors.ts → src/ir/job-errors.ts} +12 -10
- package/src/ir/layouts.ts +181 -0
- package/src/ir/link.ts +354 -0
- package/src/ir/model.ts +386 -0
- package/{ir → src/ir}/normalization-error.ts +4 -4
- package/src/ir/normalize.ts +70 -0
- package/src/ir/numeric.ts +171 -0
- package/src/ir/ranges.ts +256 -0
- package/src/ir/routine.ts +1163 -0
- package/src/ir/scalars.ts +321 -0
- package/src/ir/types.ts +127 -0
- package/src/project/artifacts.ts +342 -0
- package/src/project/cli.ts +177 -0
- package/src/project/debugger.ts +82 -0
- package/src/project/index.ts +6 -0
- package/src/project/plant.ts +467 -0
- package/src/project/project-commands.ts +372 -0
- package/src/project/project.ts +457 -0
- package/src/project/release.ts +153 -0
- package/src/project/server.ts +150 -0
- package/src/project/terminal.ts +32 -0
- package/stdlib/math.ts +14 -10
- package/stdlib/telemetry.ts +71 -47
- package/types/async.d.ts +46 -40
- package/types/data-types.d.ts +26 -26
- package/types/globals.d.ts +41 -38
- package/types/index.d.ts +96 -51
- package/types/plc.d.ts +3 -4
- package/types/telemetry.d.ts +14 -14
- package/backends/codesys/config.ts +0 -54
- package/backends/codesys/debug.ts +0 -509
- package/backends/codesys/emit.ts +0 -1738
- package/backends/codesys/engineering.ts +0 -88
- package/backends/codesys/global-names.ts +0 -49
- package/backends/codesys/http.ts +0 -675
- package/backends/codesys/index.ts +0 -13
- package/backends/codesys/io-paths.ts +0 -105
- package/backends/codesys/manifest.ts +0 -312
- package/backends/codesys/names.ts +0 -76
- package/backends/codesys/numeric.ts +0 -356
- package/backends/codesys/plan.ts +0 -457
- package/backends/codesys/publication.ts +0 -59
- package/backends/codesys/storage.ts +0 -70
- package/backends/codesys/task-access.ts +0 -293
- package/backends/codesys/telemetry.ts +0 -593
- package/backends/codesys/types.ts +0 -179
- package/backends/codesys/vendor.ts +0 -108
- package/ir/aggregates.ts +0 -333
- package/ir/allocation.ts +0 -74
- package/ir/core-model.ts +0 -129
- package/ir/core.ts +0 -64
- package/ir/declarations.ts +0 -208
- package/ir/diagnostics.ts +0 -43
- package/ir/domains.ts +0 -96
- package/ir/index.ts +0 -8
- package/ir/link.ts +0 -185
- package/ir/model.ts +0 -346
- package/ir/routine.ts +0 -859
- package/ir/scalars.ts +0 -675
- package/ir/types.ts +0 -76
- package/project/artifacts.ts +0 -277
- package/project/cli.ts +0 -395
- package/project/index.ts +0 -1
- package/project/plant.ts +0 -509
- package/project/project.ts +0 -358
- package/project/server.ts +0 -113
|
@@ -2,158 +2,193 @@ import { runtimeSources } from "./module-graph.ts";
|
|
|
2
2
|
import ts from "typescript";
|
|
3
3
|
import { error, type Diagnostic } from "@tsplc/compiler/ir";
|
|
4
4
|
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
5
|
+
copied,
|
|
6
|
+
edited,
|
|
7
|
+
from,
|
|
8
|
+
joined,
|
|
9
|
+
mapped,
|
|
10
|
+
type Edit,
|
|
11
|
+
type Mapped,
|
|
12
|
+
type Part,
|
|
13
|
+
type Positions,
|
|
13
14
|
} from "./positions.ts";
|
|
14
15
|
|
|
16
|
+
/** An arrow body as a function body: a block as written, or its expression returned (or, for void, evaluated). */
|
|
17
|
+
const functionBody = (
|
|
18
|
+
body: ts.ConciseBody,
|
|
19
|
+
result: ts.TypeNode | undefined,
|
|
20
|
+
): Part => {
|
|
21
|
+
if (ts.isBlock(body)) {
|
|
22
|
+
return copied(body);
|
|
23
|
+
}
|
|
24
|
+
return result?.kind === ts.SyntaxKind.VoidKeyword
|
|
25
|
+
? mapped`{ ${copied(body)}; }`
|
|
26
|
+
: mapped`{ return ${copied(body)}; }`;
|
|
27
|
+
};
|
|
28
|
+
|
|
15
29
|
/** Normalize static arrow bindings so all routine forms use the same PLC analysis. */
|
|
16
|
-
export const lowerConstFunctions = (
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
30
|
+
export const lowerConstFunctions = (
|
|
31
|
+
program: ts.Program,
|
|
32
|
+
positions: Positions,
|
|
33
|
+
) => {
|
|
34
|
+
const checker = program.getTypeChecker();
|
|
35
|
+
const printer = ts.createPrinter();
|
|
36
|
+
const sources = new Map<string, Mapped>();
|
|
37
|
+
const diagnostics: Diagnostic[] = [];
|
|
21
38
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
39
|
+
for (const source of runtimeSources(program)) {
|
|
40
|
+
if (source.isDeclarationFile) {
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
const edits: Edit[] = [];
|
|
44
|
+
for (const statement of source.statements) {
|
|
45
|
+
const routines = ts.isClassDeclaration(statement)
|
|
46
|
+
? statement.members.filter(ts.isMethodDeclaration)
|
|
47
|
+
: [statement];
|
|
48
|
+
for (const routine of routines) {
|
|
49
|
+
if (
|
|
50
|
+
(!ts.isFunctionDeclaration(routine) &&
|
|
51
|
+
!ts.isMethodDeclaration(routine)) ||
|
|
52
|
+
routine.type ||
|
|
53
|
+
!routine.body ||
|
|
54
|
+
!routine.modifiers?.some(
|
|
55
|
+
(item) => item.kind === ts.SyntaxKind.AsyncKeyword,
|
|
56
|
+
)
|
|
57
|
+
) {
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
const signature = checker.getSignatureFromDeclaration(routine);
|
|
61
|
+
const result =
|
|
62
|
+
signature &&
|
|
63
|
+
checker.getAwaitedType(checker.getReturnTypeOfSignature(signature));
|
|
64
|
+
if (result && result.flags & ts.TypeFlags.Void) {
|
|
65
|
+
const position = routine.body.getStart(source);
|
|
66
|
+
edits.push({
|
|
67
|
+
start: position,
|
|
68
|
+
end: position,
|
|
69
|
+
text: from(routine, ":Promise<void> "),
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
for (const statement of source.statements) {
|
|
75
|
+
if (
|
|
76
|
+
!ts.isVariableStatement(statement) ||
|
|
77
|
+
!statement.declarationList.declarations.some(
|
|
78
|
+
(declaration) =>
|
|
79
|
+
declaration.initializer &&
|
|
80
|
+
ts.isArrowFunction(declaration.initializer),
|
|
81
|
+
)
|
|
82
|
+
) {
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
54
85
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
86
|
+
const print = (node: ts.Node) =>
|
|
87
|
+
printer.printNode(ts.EmitHint.Unspecified, node, source);
|
|
88
|
+
const declarations: Mapped[] = [];
|
|
89
|
+
for (const declaration of statement.declarationList.declarations) {
|
|
90
|
+
const arrow = declaration.initializer;
|
|
91
|
+
if (!arrow || !ts.isArrowFunction(arrow)) {
|
|
92
|
+
declarations.push(
|
|
93
|
+
from(
|
|
94
|
+
declaration,
|
|
95
|
+
print(
|
|
96
|
+
ts.factory.updateVariableStatement(
|
|
97
|
+
statement,
|
|
98
|
+
statement.modifiers,
|
|
99
|
+
ts.factory.updateVariableDeclarationList(
|
|
100
|
+
statement.declarationList,
|
|
101
|
+
[declaration],
|
|
102
|
+
),
|
|
103
|
+
),
|
|
104
|
+
),
|
|
105
|
+
),
|
|
106
|
+
);
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
if (
|
|
110
|
+
!(statement.declarationList.flags & ts.NodeFlags.Const) ||
|
|
111
|
+
!ts.isIdentifier(declaration.name)
|
|
112
|
+
) {
|
|
113
|
+
diagnostics.push(
|
|
114
|
+
error(
|
|
115
|
+
"PLC1007",
|
|
116
|
+
"PLC routines require named const bindings; mutable function values are unsupported.",
|
|
117
|
+
positions.node(declaration),
|
|
118
|
+
),
|
|
119
|
+
);
|
|
120
|
+
continue;
|
|
121
|
+
}
|
|
89
122
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
123
|
+
const contextual =
|
|
124
|
+
declaration.type && ts.isFunctionTypeNode(declaration.type)
|
|
125
|
+
? declaration.type
|
|
126
|
+
: undefined;
|
|
127
|
+
const signature = checker
|
|
128
|
+
.getTypeAtLocation(declaration.name)
|
|
129
|
+
.getCallSignatures()[0];
|
|
130
|
+
const returnsVoid =
|
|
131
|
+
signature &&
|
|
132
|
+
Boolean(
|
|
133
|
+
checker.getReturnTypeOfSignature(signature).flags &
|
|
134
|
+
ts.TypeFlags.Void,
|
|
135
|
+
);
|
|
136
|
+
const inferredResult =
|
|
137
|
+
signature &&
|
|
138
|
+
checker.getAwaitedType(checker.getReturnTypeOfSignature(signature));
|
|
139
|
+
const asyncVoid =
|
|
140
|
+
arrow.modifiers?.some(
|
|
141
|
+
(item) => item.kind === ts.SyntaxKind.AsyncKeyword,
|
|
142
|
+
) &&
|
|
143
|
+
inferredResult &&
|
|
144
|
+
inferredResult.flags & ts.TypeFlags.Void;
|
|
145
|
+
const inferredType = asyncVoid
|
|
146
|
+
? ts.factory.createTypeReferenceNode("Promise", [
|
|
147
|
+
ts.factory.createKeywordTypeNode(ts.SyntaxKind.VoidKeyword),
|
|
148
|
+
])
|
|
149
|
+
: undefined;
|
|
150
|
+
const result =
|
|
151
|
+
arrow.type ??
|
|
152
|
+
contextual?.type ??
|
|
153
|
+
(returnsVoid
|
|
154
|
+
? ts.factory.createKeywordTypeNode(ts.SyntaxKind.VoidKeyword)
|
|
155
|
+
: inferredType);
|
|
156
|
+
const parameters = arrow.parameters.map((parameter, index) =>
|
|
157
|
+
ts.factory.updateParameterDeclaration(
|
|
158
|
+
parameter,
|
|
159
|
+
parameter.modifiers,
|
|
160
|
+
parameter.dotDotDotToken,
|
|
161
|
+
parameter.name,
|
|
162
|
+
parameter.questionToken,
|
|
163
|
+
parameter.type ?? contextual?.parameters[index]?.type,
|
|
164
|
+
parameter.initializer,
|
|
165
|
+
),
|
|
166
|
+
);
|
|
167
|
+
// The header is printed; the body stays the user's text, so its positions map exactly.
|
|
168
|
+
const head = print(
|
|
169
|
+
ts.factory.createFunctionDeclaration(
|
|
170
|
+
[...(statement.modifiers ?? []), ...(arrow.modifiers ?? [])],
|
|
171
|
+
undefined,
|
|
172
|
+
declaration.name,
|
|
173
|
+
arrow.typeParameters ?? contextual?.typeParameters,
|
|
174
|
+
parameters,
|
|
175
|
+
result,
|
|
176
|
+
ts.factory.createBlock([]),
|
|
177
|
+
),
|
|
178
|
+
).replace(/\{\s*\}$/, "");
|
|
179
|
+
declarations.push(
|
|
180
|
+
from(declaration, mapped`${head}${functionBody(arrow.body, result)}`),
|
|
181
|
+
);
|
|
182
|
+
}
|
|
183
|
+
edits.push({
|
|
184
|
+
start: statement.getStart(source),
|
|
185
|
+
end: statement.end,
|
|
186
|
+
text: joined(declarations, "\n"),
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
if (edits.length) {
|
|
190
|
+
sources.set(source.fileName, edited(source, edits));
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
return { sources, diagnostics };
|
|
159
194
|
};
|
|
@@ -1,37 +1,54 @@
|
|
|
1
|
+
import { mapped, type Part } from "./positions.ts";
|
|
2
|
+
|
|
1
3
|
interface Parameter {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
4
|
+
name: string;
|
|
5
|
+
type: string;
|
|
6
|
+
aggregate: boolean;
|
|
7
|
+
}
|
|
8
|
+
/** A generated site: its members, its poll method, and what its own methods are called in an owner. */
|
|
9
|
+
export interface Site {
|
|
10
|
+
body: string;
|
|
11
|
+
poll: string;
|
|
12
|
+
renames: ReadonlyMap<string, string>;
|
|
5
13
|
}
|
|
6
14
|
interface Worker {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
15
|
+
start: string;
|
|
16
|
+
advance: string;
|
|
17
|
+
snapshot: string;
|
|
18
|
+
reset: string;
|
|
19
|
+
declaration: string;
|
|
12
20
|
}
|
|
13
21
|
|
|
22
|
+
/** A value as generated code stores it: records and arrays are copied, as TypeScript would share them. */
|
|
23
|
+
export const copyValue = (value: Part, aggregate: boolean) =>
|
|
24
|
+
aggregate ? mapped`copy(${value})` : value;
|
|
25
|
+
|
|
14
26
|
/** A fixed call-site frame; callbacks and ordinary state stay in the caller task. */
|
|
15
27
|
export const cooperativeSite = (
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
) => {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
28
|
+
name: string,
|
|
29
|
+
parameters: readonly Parameter[],
|
|
30
|
+
worker: Worker,
|
|
31
|
+
inline: boolean,
|
|
32
|
+
): Site => {
|
|
33
|
+
const prefix = inline ? name : "site";
|
|
34
|
+
const field = (label: string) => `${prefix}_${label}`;
|
|
35
|
+
const member = (label: string) => `this.${field(label)}`;
|
|
36
|
+
const method = (label: string) => (inline ? `${name}_${label}` : label);
|
|
37
|
+
const invoke = (label: string, args = "") => `this.${method(label)}(${args})`;
|
|
38
|
+
const copy = (value: string, aggregate: boolean) =>
|
|
39
|
+
aggregate ? `copy(${value})` : value;
|
|
40
|
+
const faults = `${member("faults")}=tsplcAsyncWrapAdd<UDINT>(${member("faults")},1);${member("lastError")}=7;`;
|
|
41
|
+
const signature = parameters
|
|
42
|
+
.map((parameter) => `${parameter.name}:${parameter.type}`)
|
|
43
|
+
.join(",");
|
|
44
|
+
const args = parameters.map((parameter) => parameter.name).join(",");
|
|
45
|
+
const queued = parameters
|
|
46
|
+
.map((parameter) => member(`queued_${parameter.name}`))
|
|
47
|
+
.join(",");
|
|
48
|
+
const fields = `private ${field("active")}:BOOL=false;private ${field("fresh")}:BOOL=false;private ${field("cancelled")}:BOOL=false;private ${field("queued")}:BOOL=false;private ${field("faults")}:UDINT=0;private ${field("lastError")}:UDINT=0;
|
|
32
49
|
${parameters.map((parameter) => `private ${field(`queued_${parameter.name}`)}:${parameter.type}=defaultValue<${parameter.type}>();`).join("\n")}
|
|
33
50
|
${worker.declaration}`;
|
|
34
|
-
|
|
51
|
+
const body = `${fields}
|
|
35
52
|
public ${method("begin")}(${signature}):void{const accepted:BOOL=${worker.start}(${args});if(!accepted){${faults}return;}${member("active")}=true;${member("fresh")}=true;${member("cancelled")}=false;${worker.advance}({cancel:false});}
|
|
36
53
|
public ${method("start")}(${signature}):void{if(${member("active")}){if(${member("cancelled")}&&!${member("queued")}){${parameters.map((parameter) => `${member(`queued_${parameter.name}`)}=${copy(parameter.name, parameter.aggregate)};`).join("")}${member("queued")}=true;}else{${faults}}return;}${invoke("begin", args)};}
|
|
37
54
|
public ${method("cancel")}():void{${member("cancelled")}=true;${member("queued")}=false;}
|
|
@@ -44,32 +61,45 @@ if(!${member("cancelled")}&&state.error!==0&&state.error!==1){${member("faults")
|
|
|
44
61
|
${worker.reset}();${member("active")}=false;
|
|
45
62
|
if(${member("queued")}){${member("queued")}=false;${invoke("begin", queued)};}
|
|
46
63
|
}`;
|
|
47
|
-
|
|
64
|
+
return {
|
|
65
|
+
body,
|
|
66
|
+
poll: method("poll"),
|
|
67
|
+
renames: new Map(
|
|
68
|
+
["start", "cancel"].map((member) => [member, method(member)]),
|
|
69
|
+
),
|
|
70
|
+
};
|
|
48
71
|
};
|
|
49
72
|
|
|
50
73
|
/** A named task: the owner reads its state and result instead of attaching a callback. */
|
|
51
74
|
export const taskSite = (
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
) => {
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
75
|
+
name: string,
|
|
76
|
+
parameters: readonly Parameter[],
|
|
77
|
+
worker: Worker,
|
|
78
|
+
inline: boolean,
|
|
79
|
+
value: { type: string; aggregate: boolean },
|
|
80
|
+
): Site => {
|
|
81
|
+
const method = (label: string) => (inline ? `${name}_${label}` : label);
|
|
82
|
+
const cancelled = `this.${inline ? `${name}_cancelled` : "cancelled"}`;
|
|
83
|
+
const signature = parameters
|
|
84
|
+
.map((parameter) => `${parameter.name}:${parameter.type}`)
|
|
85
|
+
.join(",");
|
|
86
|
+
const args = parameters.map((parameter) => parameter.name).join(",");
|
|
87
|
+
const result = value.aggregate ? "copy(state.result)" : "state.result";
|
|
88
|
+
const body = `private ${cancelled.slice(5)}:BOOL=false;
|
|
64
89
|
${worker.declaration}
|
|
65
90
|
public ${method("start")}(${signature}):BOOL{const accepted:BOOL=${worker.start}(${args});if(accepted){${cancelled}=false;${worker.advance}({cancel:false});}return accepted;}
|
|
66
91
|
public ${method("cancel")}():BOOL{const state=${worker.snapshot}();if(!state.busy&&!state.done)return false;${cancelled}=true;return true;}
|
|
67
92
|
public ${method("snapshot")}():{valid:BOOL;busy:BOOL;done:BOOL;cancelRequested:BOOL;error:UDINT;result:${value.type}}{const state=${worker.snapshot}();if(${cancelled}&&state.done)return {valid:true,busy:false,done:true,cancelRequested:false,error:1,result:defaultValue<${value.type}>()};return {valid:true,busy:state.busy,done:state.done,cancelRequested:${cancelled},error:state.error,result:${result}};}
|
|
68
93
|
public ${method("reset")}():BOOL{const state=${worker.snapshot}();if(state.busy)return false;${worker.reset}();${cancelled}=false;return true;}
|
|
69
94
|
public ${method("poll")}():void{${worker.advance}({cancel:${cancelled}});}`;
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
95
|
+
return {
|
|
96
|
+
body,
|
|
97
|
+
poll: method("poll"),
|
|
98
|
+
renames: new Map(
|
|
99
|
+
["start", "cancel", "snapshot", "reset"].map((member) => [
|
|
100
|
+
member,
|
|
101
|
+
method(member),
|
|
102
|
+
]),
|
|
103
|
+
),
|
|
104
|
+
};
|
|
75
105
|
};
|