@tsplc/tmp-compiler 0.0.0-stage → 0.6.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +41 -2
- package/dist/cli.js +2325 -0
- package/dist/cli.js.map +26 -0
- package/dist/clock.js +11 -0
- package/dist/clock.js.map +11 -0
- package/dist/codesys.js +6558 -0
- package/dist/codesys.js.map +51 -0
- package/dist/collections.js +399 -0
- package/dist/collections.js.map +10 -0
- package/dist/index.js +16076 -0
- package/dist/index.js.map +61 -0
- package/dist/ir.js +3948 -0
- package/dist/ir.js.map +35 -0
- package/dist/retain.js +10 -0
- package/dist/retain.js.map +10 -0
- package/dist/signals.js +115 -0
- package/dist/signals.js.map +10 -0
- package/dist/telemetry.js +121 -0
- package/dist/telemetry.js.map +12 -0
- package/package.json +122 -4
- package/src/codesys/config.ts +50 -0
- package/src/codesys/debug.ts +655 -0
- package/src/codesys/emit.ts +185 -0
- package/src/codesys/endpoints.ts +111 -0
- package/src/codesys/engineering.ts +96 -0
- package/src/codesys/explain/expressions.ts +445 -0
- package/src/codesys/explain/flags.ts +134 -0
- package/src/codesys/explain/parameters.ts +53 -0
- package/src/codesys/explain/rows.ts +182 -0
- package/src/codesys/explain/tables.ts +79 -0
- package/src/codesys/explain/tasks.ts +68 -0
- package/src/codesys/explain/writes.ts +497 -0
- package/src/codesys/explain.ts +149 -0
- package/src/codesys/expressions.ts +100 -0
- package/src/codesys/global-lists.ts +284 -0
- package/src/codesys/global-names.ts +66 -0
- package/src/codesys/http.ts +1078 -0
- package/src/codesys/index.ts +13 -0
- package/src/codesys/instrumentation.ts +574 -0
- package/src/codesys/io-paths.ts +149 -0
- package/src/codesys/loops.ts +101 -0
- package/src/codesys/manifest.ts +684 -0
- package/src/codesys/names.ts +58 -0
- package/src/codesys/numeric.ts +516 -0
- package/src/codesys/plan.ts +688 -0
- package/src/codesys/programs.ts +138 -0
- package/src/codesys/publication.ts +99 -0
- package/src/codesys/required.ts +8 -0
- package/src/codesys/routines.ts +247 -0
- package/src/codesys/scan-clock.ts +103 -0
- package/src/codesys/scheduling.ts +96 -0
- package/src/codesys/service-channels.ts +24 -0
- package/src/codesys/sources.ts +118 -0
- package/src/codesys/st-files.ts +219 -0
- package/src/codesys/statements.ts +409 -0
- package/src/codesys/storage.ts +150 -0
- package/src/codesys/symbols.ts +240 -0
- package/src/codesys/task-access.ts +186 -0
- package/src/codesys/telemetry-settings.ts +19 -0
- package/src/codesys/telemetry.ts +852 -0
- package/src/codesys/types.ts +310 -0
- package/src/codesys/vendor.ts +113 -0
- package/src/frontend/analyzer.ts +779 -0
- package/src/frontend/async.ts +530 -0
- package/src/frontend/blocks.ts +578 -0
- package/src/frontend/configuration.ts +83 -0
- package/src/frontend/const-functions.ts +204 -0
- package/src/frontend/control-flow.ts +787 -0
- package/src/frontend/cooperative.ts +114 -0
- package/src/frontend/coroutines.ts +1336 -0
- package/src/frontend/dialect-names.ts +104 -0
- package/src/frontend/elaborate/array-searches.ts +224 -0
- package/src/frontend/elaborate/assignments.ts +343 -0
- package/src/frontend/elaborate/calls.ts +1342 -0
- package/src/frontend/elaborate/capture-protocol.ts +237 -0
- package/src/frontend/elaborate/capture.ts +388 -0
- package/src/frontend/elaborate/declarations.ts +124 -0
- package/src/frontend/elaborate/dialect.ts +473 -0
- package/src/frontend/elaborate/expressions.ts +2171 -0
- package/src/frontend/elaborate/filtered-arrays.ts +643 -0
- package/src/frontend/elaborate/frame.ts +145 -0
- package/src/frontend/elaborate/generics.ts +123 -0
- package/src/frontend/elaborate/graph.ts +141 -0
- package/src/frontend/elaborate/imports.ts +105 -0
- package/src/frontend/elaborate/index.ts +851 -0
- package/src/frontend/elaborate/initializers.ts +235 -0
- package/src/frontend/elaborate/iterators.ts +1541 -0
- package/src/frontend/elaborate/layouts.ts +1025 -0
- package/src/frontend/elaborate/model.ts +478 -0
- package/src/frontend/elaborate/names.ts +134 -0
- package/src/frontend/elaborate/reduce.ts +900 -0
- package/src/frontend/elaborate/references.ts +727 -0
- package/src/frontend/elaborate/retention.ts +284 -0
- package/src/frontend/elaborate/specialize.ts +1137 -0
- package/src/frontend/elaborate/statements.ts +1220 -0
- package/src/frontend/elaborate/storage.ts +836 -0
- package/src/frontend/elaborate/time-constructors.ts +34 -0
- package/src/frontend/elaborate/values.ts +200 -0
- package/src/frontend/elaborate/writes.ts +415 -0
- package/src/frontend/expressions.ts +1382 -0
- package/src/frontend/generated-names.ts +95 -0
- package/src/frontend/intrinsics.ts +382 -0
- package/src/frontend/lowering-key.ts +23 -0
- package/src/frontend/module-graph.ts +156 -0
- package/src/frontend/positions.ts +359 -0
- package/src/frontend/promise-chains.ts +872 -0
- package/src/frontend/promises.ts +419 -0
- package/src/frontend/routines.ts +239 -0
- package/src/frontend/sdk.ts +187 -0
- package/src/frontend/services.ts +609 -0
- package/src/frontend/statements.ts +871 -0
- package/src/frontend/tasks.ts +526 -0
- package/src/frontend/telemetry.ts +174 -0
- package/src/frontend/types.ts +1073 -0
- package/src/frontend/typescript.ts +651 -0
- package/src/index.ts +9 -0
- package/src/ir/aggregates.ts +291 -0
- package/src/ir/allocation.ts +96 -0
- package/src/ir/conditions.ts +855 -0
- package/src/ir/core-model.ts +289 -0
- package/src/ir/declarations.ts +281 -0
- package/src/ir/diagnostics.ts +68 -0
- package/src/ir/domains.ts +151 -0
- package/src/ir/fold.ts +279 -0
- package/src/ir/identity.ts +19 -0
- package/src/ir/index.ts +137 -0
- package/src/ir/job-errors.ts +69 -0
- package/src/ir/layouts.ts +211 -0
- package/src/ir/link.ts +302 -0
- package/src/ir/model.ts +394 -0
- package/src/ir/normalization-error.ts +10 -0
- package/src/ir/normalize.ts +52 -0
- package/src/ir/numeric.ts +163 -0
- package/src/ir/operands.ts +160 -0
- package/src/ir/ranges.ts +420 -0
- package/src/ir/routine.ts +1116 -0
- package/src/ir/scalars.ts +453 -0
- package/src/ir/share.ts +790 -0
- package/src/ir/short-circuit.ts +140 -0
- package/src/ir/tables.ts +111 -0
- package/src/ir/temporaries.ts +138 -0
- package/src/ir/types.ts +151 -0
- package/src/project/artifacts.ts +424 -0
- package/src/project/cli.ts +211 -0
- package/src/project/debugger.ts +111 -0
- package/src/project/deployments.ts +387 -0
- package/src/project/flags.ts +14 -0
- package/src/project/git.ts +35 -0
- package/src/project/plant.ts +629 -0
- package/src/project/project-commands.ts +507 -0
- package/src/project/project.ts +559 -0
- package/src/project/release.ts +152 -0
- package/src/project/server-check.ts +54 -0
- package/src/project/server.ts +165 -0
- package/src/project/terminal.ts +45 -0
- package/src/runtime/async.ts +207 -0
- package/src/runtime/launch.ts +59 -0
- package/src/runtime/main.ts +59 -0
- package/src/runtime/missing-file.ts +3 -0
- package/src/runtime/process.ts +177 -0
- package/src/runtime/program.ts +109 -0
- package/src/runtime/retain.ts +108 -0
- package/src/runtime/runtime.ts +324 -0
- package/src/runtime/settings.ts +27 -0
- package/src/runtime/storage.ts +456 -0
- package/src/runtime/symbols.ts +97 -0
- package/src/runtime/telemetry.ts +154 -0
- package/src/runtime/transport.ts +168 -0
- package/stdlib/clock.ts +8 -0
- package/stdlib/collections.ts +534 -0
- package/stdlib/plc-time.ts +2 -0
- package/stdlib/retain.ts +4 -0
- package/stdlib/signals.ts +142 -0
- package/stdlib/telemetry.ts +89 -0
- package/types/async.d.ts +87 -0
- package/types/clock.d.ts +8 -0
- package/types/config.d.ts +2 -0
- package/types/data-types.d.ts +31 -0
- package/types/dialect.d.ts +49 -0
- package/types/globals.d.ts +10 -0
- package/types/index.d.ts +212 -0
- package/types/retain.d.ts +12 -0
- package/types/telemetry.d.ts +47 -0
|
@@ -0,0 +1,456 @@
|
|
|
1
|
+
import { optionalRecord } from "@tsplc/compiler/ir";
|
|
2
|
+
import type { DebugManifest, DebugType } from "@tsplc/debug/manifest";
|
|
3
|
+
import { typeOf } from "@tsplc/debug/paths";
|
|
4
|
+
import { elementaryType, fitsNumber, isIntegerValued } from "@tsplc/iec";
|
|
5
|
+
import { applyIntegerPolicy, roundFloat } from "@tsplc/iec/arithmetic";
|
|
6
|
+
import {
|
|
7
|
+
arrayIndex,
|
|
8
|
+
joinPath,
|
|
9
|
+
symbolPathSegments,
|
|
10
|
+
} from "@tsplc/opcua/contract";
|
|
11
|
+
import type { PublishedScalar, PublishedValue } from "@tsplc/opcua/server";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* The program's storage as the PLC keeps it. The program runs on its own objects; this module
|
|
15
|
+
* finds each PLC symbol among them through the build's debug manifest, which names every stored
|
|
16
|
+
* root's TypeScript path and target symbol, and converts values between the PLC's layout and the
|
|
17
|
+
* program's: a string-literal union is a string here and an enum there, `T | undefined` is
|
|
18
|
+
* `{ has, value }` there, and a `Record<Enum, T>` is an array indexed by the enum's values.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
/** Where a value lives in the program's objects: read and written as the program reads and writes it. */
|
|
22
|
+
interface Place {
|
|
23
|
+
get(): unknown;
|
|
24
|
+
set(value: unknown): void;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** A PLC value: a scalar's, or an enum's numeric value. */
|
|
28
|
+
type Scalar = PublishedValue;
|
|
29
|
+
|
|
30
|
+
type RecordType = Extract<DebugType, { kind: "record" }>;
|
|
31
|
+
type ArrayType = Extract<DebugType, { kind: "array" }>;
|
|
32
|
+
type EnumType = Extract<DebugType, { kind: "enum" }>;
|
|
33
|
+
|
|
34
|
+
const joinSegments = (segments: readonly string[]) =>
|
|
35
|
+
segments.reduce(joinPath, "");
|
|
36
|
+
|
|
37
|
+
/** The property a TypeScript path segment names: a field, or an index or numeric key without brackets. */
|
|
38
|
+
const propertyOf = (segment: string) => {
|
|
39
|
+
const index = arrayIndex(segment);
|
|
40
|
+
return index === undefined ? segment : String(index);
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
const isObject = (value: unknown): value is Record<string, unknown> =>
|
|
44
|
+
typeof value === "object" && value !== null;
|
|
45
|
+
|
|
46
|
+
const fieldPlace = (parent: Place, key: string): Place => ({
|
|
47
|
+
get: () => {
|
|
48
|
+
const holder = parent.get();
|
|
49
|
+
return isObject(holder) ? holder[key] : undefined;
|
|
50
|
+
},
|
|
51
|
+
set: (value) => {
|
|
52
|
+
const holder = parent.get();
|
|
53
|
+
if (isObject(holder)) {
|
|
54
|
+
holder[key] = value;
|
|
55
|
+
}
|
|
56
|
+
},
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* The value field of an optional's record: `T | undefined` is stored as `optionalRecord`; a
|
|
61
|
+
* literal union holds undefined as its enum's `Undefined` member instead.
|
|
62
|
+
*/
|
|
63
|
+
const optionalValue = (type: RecordType) => {
|
|
64
|
+
const [has, value, ...rest] = type.fields;
|
|
65
|
+
return type.name.startsWith(optionalRecord.prefix) &&
|
|
66
|
+
has?.name === optionalRecord.has &&
|
|
67
|
+
value?.name === optionalRecord.value &&
|
|
68
|
+
!rest.length
|
|
69
|
+
? value
|
|
70
|
+
: undefined;
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
/** `text` cut to at most `capacity` UTF-8 bytes, at a character boundary. */
|
|
74
|
+
const truncateUtf8 = (text: string, capacity: number) => {
|
|
75
|
+
if (Buffer.byteLength(text, "utf8") <= capacity) {
|
|
76
|
+
return text;
|
|
77
|
+
}
|
|
78
|
+
const kept: string[] = [];
|
|
79
|
+
let bytes = 0;
|
|
80
|
+
for (const character of text) {
|
|
81
|
+
bytes += Buffer.byteLength(character, "utf8");
|
|
82
|
+
if (bytes > capacity) {
|
|
83
|
+
break;
|
|
84
|
+
}
|
|
85
|
+
kept.push(character);
|
|
86
|
+
}
|
|
87
|
+
return kept.join("");
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* What storing a value of `type` does to it in the PLC, where TypeScript would keep it as it is:
|
|
92
|
+
* integers wrap to their width, a REAL rounds to single precision and a STRING keeps its first
|
|
93
|
+
* `capacity` bytes. Undefined for types whose values TypeScript already stores as the PLC does.
|
|
94
|
+
*/
|
|
95
|
+
const storedValue = (
|
|
96
|
+
type: DebugType,
|
|
97
|
+
): ((value: unknown) => unknown) | undefined => {
|
|
98
|
+
if (type.kind !== "scalar") {
|
|
99
|
+
return undefined;
|
|
100
|
+
}
|
|
101
|
+
if (type.capacity !== undefined) {
|
|
102
|
+
const { capacity } = type;
|
|
103
|
+
return type.iec === "WSTRING"
|
|
104
|
+
? (value) =>
|
|
105
|
+
typeof value === "string" ? value.slice(0, capacity) : value
|
|
106
|
+
: (value) =>
|
|
107
|
+
typeof value === "string" ? truncateUtf8(value, capacity) : value;
|
|
108
|
+
}
|
|
109
|
+
const elementary = elementaryType(type.iec);
|
|
110
|
+
if (!elementary) {
|
|
111
|
+
return undefined;
|
|
112
|
+
}
|
|
113
|
+
if (elementary.family === "real") {
|
|
114
|
+
return elementary.bits === 32
|
|
115
|
+
? (value) =>
|
|
116
|
+
typeof value === "number" ? roundFloat(value, elementary) : value
|
|
117
|
+
: undefined;
|
|
118
|
+
}
|
|
119
|
+
if (elementary.family === "duration" && elementary.bits === 32) {
|
|
120
|
+
return storedValue({ kind: "scalar", iec: "UDINT" });
|
|
121
|
+
}
|
|
122
|
+
if (!isIntegerValued(elementary)) {
|
|
123
|
+
return undefined;
|
|
124
|
+
}
|
|
125
|
+
const wrap = (value: bigint) => applyIntegerPolicy(value, elementary, "wrap");
|
|
126
|
+
if (!fitsNumber(elementary)) {
|
|
127
|
+
return (value) => (typeof value === "bigint" ? wrap(value) : value);
|
|
128
|
+
}
|
|
129
|
+
return (value) =>
|
|
130
|
+
typeof value === "number" && Number.isFinite(value)
|
|
131
|
+
? Number(wrap(BigInt(Math.trunc(value))))
|
|
132
|
+
: value;
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
/** A scalar's PLC value when the program holds nothing there, as for a variant field it lacks. */
|
|
136
|
+
const scalarDefault = (type: DebugType): Scalar => {
|
|
137
|
+
if (type.kind === "enum") {
|
|
138
|
+
// As the emitted ST initialises an enum (ir's `defaultValue`): its zero member, else its first.
|
|
139
|
+
const values = Object.values(type.members);
|
|
140
|
+
return values.includes(0) ? 0 : (values[0] ?? 0);
|
|
141
|
+
}
|
|
142
|
+
if (type.kind !== "scalar") {
|
|
143
|
+
return 0;
|
|
144
|
+
}
|
|
145
|
+
if (type.capacity !== undefined) {
|
|
146
|
+
return "";
|
|
147
|
+
}
|
|
148
|
+
const elementary = elementaryType(type.iec);
|
|
149
|
+
if (elementary?.family === "bool") {
|
|
150
|
+
return false;
|
|
151
|
+
}
|
|
152
|
+
return isIntegerValued(elementary) && !fitsNumber(elementary) ? 0n : 0;
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
export const createStorage = (
|
|
156
|
+
manifest: DebugManifest,
|
|
157
|
+
/** A module-level binding of the program by name, or undefined when there is none. */
|
|
158
|
+
binding: (name: string) => unknown,
|
|
159
|
+
) => {
|
|
160
|
+
const type = (id: string) => typeOf(manifest, id);
|
|
161
|
+
const textMaps = new Map<
|
|
162
|
+
EnumType,
|
|
163
|
+
ReturnType<typeof textMapsOf> | undefined
|
|
164
|
+
>();
|
|
165
|
+
/** A string-literal union's strings by member value and back; undefined for a TypeScript enum. */
|
|
166
|
+
const enumTexts = (enumeration: EnumType) => {
|
|
167
|
+
if (!textMaps.has(enumeration)) {
|
|
168
|
+
textMaps.set(enumeration, textMapsOf(enumeration));
|
|
169
|
+
}
|
|
170
|
+
return textMaps.get(enumeration);
|
|
171
|
+
};
|
|
172
|
+
const textMapsOf = (enumeration: EnumType) => {
|
|
173
|
+
const texts = enumeration.texts;
|
|
174
|
+
if (!texts) {
|
|
175
|
+
return undefined;
|
|
176
|
+
}
|
|
177
|
+
const byText = new Map(
|
|
178
|
+
Object.entries(texts).flatMap(([member, text]) => {
|
|
179
|
+
const value = enumeration.members[member];
|
|
180
|
+
return value === undefined ? [] : [[text, value] as const];
|
|
181
|
+
}),
|
|
182
|
+
);
|
|
183
|
+
const byValue = new Map(
|
|
184
|
+
Object.entries(enumeration.members).map(([member, value]) => [
|
|
185
|
+
value,
|
|
186
|
+
texts[member],
|
|
187
|
+
]),
|
|
188
|
+
);
|
|
189
|
+
return { byText, byValue };
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
/** The program's key for index `index` of an array: its position, or the enum member's key of a `Record<Enum, T>`. */
|
|
193
|
+
const elementKey = (array: ArrayType, index: number) => {
|
|
194
|
+
const keyType = array.index === undefined ? undefined : type(array.index);
|
|
195
|
+
if (keyType?.kind !== "enum") {
|
|
196
|
+
return String(index - array.lower);
|
|
197
|
+
}
|
|
198
|
+
return enumTexts(keyType)?.byValue.get(index) ?? String(index);
|
|
199
|
+
};
|
|
200
|
+
|
|
201
|
+
/** The program's value for a PLC scalar of `scalarType`. */
|
|
202
|
+
const programValue = (scalarType: DebugType, value: Scalar): unknown => {
|
|
203
|
+
if (scalarType.kind === "enum") {
|
|
204
|
+
const texts = enumTexts(scalarType);
|
|
205
|
+
return texts ? texts.byValue.get(Number(value)) : value;
|
|
206
|
+
}
|
|
207
|
+
return value;
|
|
208
|
+
};
|
|
209
|
+
|
|
210
|
+
/** The PLC's value of the program's `value` for a scalar of `scalarType`. */
|
|
211
|
+
const plcValue = (scalarType: DebugType, value: unknown): Scalar => {
|
|
212
|
+
if (scalarType.kind === "enum") {
|
|
213
|
+
const texts = enumTexts(scalarType);
|
|
214
|
+
if (texts) {
|
|
215
|
+
return typeof value === "string"
|
|
216
|
+
? (texts.byText.get(value) ?? scalarDefault(scalarType))
|
|
217
|
+
: (scalarType.members.Undefined ?? scalarDefault(scalarType));
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
if (
|
|
221
|
+
typeof value === "boolean" ||
|
|
222
|
+
typeof value === "number" ||
|
|
223
|
+
typeof value === "bigint" ||
|
|
224
|
+
typeof value === "string"
|
|
225
|
+
) {
|
|
226
|
+
return value;
|
|
227
|
+
}
|
|
228
|
+
return scalarDefault(scalarType);
|
|
229
|
+
};
|
|
230
|
+
|
|
231
|
+
/** The program's value for a whole PLC default of `valueType`, for an optional that becomes present. */
|
|
232
|
+
const defaultValue = (valueType: DebugType): unknown => {
|
|
233
|
+
switch (valueType.kind) {
|
|
234
|
+
case "scalar":
|
|
235
|
+
case "enum":
|
|
236
|
+
return programValue(valueType, scalarDefault(valueType));
|
|
237
|
+
case "array":
|
|
238
|
+
return Array.from({ length: valueType.length }, () =>
|
|
239
|
+
defaultValue(type(valueType.element)),
|
|
240
|
+
);
|
|
241
|
+
case "record":
|
|
242
|
+
if (optionalValue(valueType)) {
|
|
243
|
+
return undefined;
|
|
244
|
+
}
|
|
245
|
+
return Object.fromEntries(
|
|
246
|
+
valueType.fields.map((field) => [
|
|
247
|
+
field.name,
|
|
248
|
+
defaultValue(type(field.type)),
|
|
249
|
+
]),
|
|
250
|
+
);
|
|
251
|
+
}
|
|
252
|
+
};
|
|
253
|
+
|
|
254
|
+
/** One step down from a PLC value at `place` to its member or element named `segment`. */
|
|
255
|
+
const child = (
|
|
256
|
+
parent: { type: DebugType; place: Place },
|
|
257
|
+
segment: string,
|
|
258
|
+
): { type: DebugType; place: Place } | undefined => {
|
|
259
|
+
const { place } = parent;
|
|
260
|
+
const index = arrayIndex(segment);
|
|
261
|
+
if (parent.type.kind === "array" && index !== undefined) {
|
|
262
|
+
const array = parent.type;
|
|
263
|
+
if (index < array.lower || index >= array.lower + array.length) {
|
|
264
|
+
return undefined;
|
|
265
|
+
}
|
|
266
|
+
return {
|
|
267
|
+
type: type(array.element),
|
|
268
|
+
place: fieldPlace(place, elementKey(array, index)),
|
|
269
|
+
};
|
|
270
|
+
}
|
|
271
|
+
if (parent.type.kind !== "record") {
|
|
272
|
+
return undefined;
|
|
273
|
+
}
|
|
274
|
+
const optional = optionalValue(parent.type);
|
|
275
|
+
if (optional && segment === optionalRecord.has) {
|
|
276
|
+
return {
|
|
277
|
+
type: { kind: "scalar", iec: "BOOL" },
|
|
278
|
+
place: {
|
|
279
|
+
get: () => place.get() !== undefined,
|
|
280
|
+
set: (present) => {
|
|
281
|
+
if (!present) {
|
|
282
|
+
place.set(undefined);
|
|
283
|
+
} else if (place.get() === undefined) {
|
|
284
|
+
place.set(defaultValue(type(optional.type)));
|
|
285
|
+
}
|
|
286
|
+
},
|
|
287
|
+
},
|
|
288
|
+
};
|
|
289
|
+
}
|
|
290
|
+
if (optional && segment === optionalRecord.value) {
|
|
291
|
+
return { type: type(optional.type), place };
|
|
292
|
+
}
|
|
293
|
+
const field = parent.type.fields.find(
|
|
294
|
+
(candidate) => candidate.target === segment,
|
|
295
|
+
);
|
|
296
|
+
return (
|
|
297
|
+
field && { type: type(field.type), place: fieldPlace(place, field.name) }
|
|
298
|
+
);
|
|
299
|
+
};
|
|
300
|
+
|
|
301
|
+
/** The place `steps` below the module binding `name`. */
|
|
302
|
+
const placeBelow = (name: string, steps: readonly string[]): Place =>
|
|
303
|
+
steps.reduce<Place>((place, step) => fieldPlace(place, propertyOf(step)), {
|
|
304
|
+
get: () => binding(name),
|
|
305
|
+
set: () => {
|
|
306
|
+
throw new Error(
|
|
307
|
+
`${name} is a module binding; the runtime can't replace it.`,
|
|
308
|
+
);
|
|
309
|
+
},
|
|
310
|
+
});
|
|
311
|
+
|
|
312
|
+
/** The place of a stored root, by its TypeScript path such as `program.diverters`. */
|
|
313
|
+
const rootPlace = (path: string): Place | undefined => {
|
|
314
|
+
const [name, ...steps] = symbolPathSegments(path);
|
|
315
|
+
if (name === undefined || binding(name) === undefined) {
|
|
316
|
+
return undefined;
|
|
317
|
+
}
|
|
318
|
+
return placeBelow(name, steps);
|
|
319
|
+
};
|
|
320
|
+
|
|
321
|
+
const roots = manifest.roots.flatMap((root) => {
|
|
322
|
+
const place = rootPlace(root.path);
|
|
323
|
+
return place ? [{ ...root, place }] : [];
|
|
324
|
+
});
|
|
325
|
+
const rootsByTarget = new Map(roots.map((root) => [root.target, root]));
|
|
326
|
+
|
|
327
|
+
/** The PLC value at a symbol path, such as `Line.stations[3].work.bins`, among the program's objects. */
|
|
328
|
+
const locate = (path: string) => {
|
|
329
|
+
const segments = symbolPathSegments(path);
|
|
330
|
+
// The longest prefix that names a root: a root can sit inside another root's list.
|
|
331
|
+
const length = Array.from(
|
|
332
|
+
{ length: segments.length },
|
|
333
|
+
(_, offset) => segments.length - offset,
|
|
334
|
+
).find((prefix) =>
|
|
335
|
+
rootsByTarget.has(joinSegments(segments.slice(0, prefix))),
|
|
336
|
+
);
|
|
337
|
+
const root =
|
|
338
|
+
length === undefined
|
|
339
|
+
? undefined
|
|
340
|
+
: rootsByTarget.get(joinSegments(segments.slice(0, length)));
|
|
341
|
+
if (!root) {
|
|
342
|
+
return undefined;
|
|
343
|
+
}
|
|
344
|
+
return segments
|
|
345
|
+
.slice(length)
|
|
346
|
+
.reduce<{ type: DebugType; place: Place } | undefined>(
|
|
347
|
+
(node, segment) => node && child(node, segment),
|
|
348
|
+
{ type: type(root.type), place: root.place },
|
|
349
|
+
);
|
|
350
|
+
};
|
|
351
|
+
|
|
352
|
+
/** Reads and writes the scalar at a symbol path as the PLC holds it; undefined when the program has no such storage. */
|
|
353
|
+
const scalar = (path: string): PublishedScalar | undefined => {
|
|
354
|
+
const node = locate(path);
|
|
355
|
+
if (!node || (node.type.kind !== "scalar" && node.type.kind !== "enum")) {
|
|
356
|
+
return undefined;
|
|
357
|
+
}
|
|
358
|
+
const { type: scalarType, place } = node;
|
|
359
|
+
return {
|
|
360
|
+
read: () => plcValue(scalarType, place.get()),
|
|
361
|
+
write: (value) => place.set(programValue(scalarType, value)),
|
|
362
|
+
};
|
|
363
|
+
};
|
|
364
|
+
|
|
365
|
+
/**
|
|
366
|
+
* Makes every stored field keep its value as the PLC stores it (`storedValue`), however the
|
|
367
|
+
* program assigns it: each becomes an accessor, and an object or array stored in a field is
|
|
368
|
+
* treated the same way, also when the program replaces it.
|
|
369
|
+
*/
|
|
370
|
+
const keepStoredValues = () => {
|
|
371
|
+
const kept = new WeakSet<object>();
|
|
372
|
+
const keep = (
|
|
373
|
+
holder: Record<string, unknown>,
|
|
374
|
+
key: string,
|
|
375
|
+
valueType: DebugType,
|
|
376
|
+
) => {
|
|
377
|
+
const descriptor = Object.getOwnPropertyDescriptor(holder, key);
|
|
378
|
+
if (
|
|
379
|
+
!descriptor ||
|
|
380
|
+
!("value" in descriptor) ||
|
|
381
|
+
!descriptor.writable ||
|
|
382
|
+
!descriptor.configurable
|
|
383
|
+
) {
|
|
384
|
+
return;
|
|
385
|
+
}
|
|
386
|
+
const store = storing(valueType);
|
|
387
|
+
if (!store) {
|
|
388
|
+
return;
|
|
389
|
+
}
|
|
390
|
+
let stored = store(descriptor.value);
|
|
391
|
+
Object.defineProperty(holder, key, {
|
|
392
|
+
configurable: true,
|
|
393
|
+
enumerable: descriptor.enumerable,
|
|
394
|
+
get: () => stored,
|
|
395
|
+
set: (value: unknown) => {
|
|
396
|
+
stored = store(value);
|
|
397
|
+
},
|
|
398
|
+
});
|
|
399
|
+
};
|
|
400
|
+
/** What storing into a field of `valueType` does: converts a scalar, or treats an object's own fields. */
|
|
401
|
+
const storing = (
|
|
402
|
+
valueType: DebugType,
|
|
403
|
+
): ((value: unknown) => unknown) | undefined => {
|
|
404
|
+
if (valueType.kind === "record") {
|
|
405
|
+
const optional = optionalValue(valueType);
|
|
406
|
+
if (optional) {
|
|
407
|
+
return storing(type(optional.type));
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
if (valueType.kind === "record" || valueType.kind === "array") {
|
|
411
|
+
return (value) => {
|
|
412
|
+
within(valueType, value);
|
|
413
|
+
return value;
|
|
414
|
+
};
|
|
415
|
+
}
|
|
416
|
+
return storedValue(valueType);
|
|
417
|
+
};
|
|
418
|
+
/** Treats the fields or elements of a stored object or array. */
|
|
419
|
+
const within = (valueType: DebugType, value: unknown) => {
|
|
420
|
+
if (!isObject(value) || kept.has(value)) {
|
|
421
|
+
return;
|
|
422
|
+
}
|
|
423
|
+
kept.add(value);
|
|
424
|
+
if (valueType.kind === "record") {
|
|
425
|
+
for (const field of valueType.fields) {
|
|
426
|
+
keep(value, field.name, type(field.type));
|
|
427
|
+
}
|
|
428
|
+
return;
|
|
429
|
+
}
|
|
430
|
+
if (valueType.kind === "array") {
|
|
431
|
+
for (
|
|
432
|
+
let index = valueType.lower;
|
|
433
|
+
index < valueType.lower + valueType.length;
|
|
434
|
+
index++
|
|
435
|
+
) {
|
|
436
|
+
keep(value, elementKey(valueType, index), type(valueType.element));
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
};
|
|
440
|
+
for (const root of roots) {
|
|
441
|
+
const rootType = type(root.type);
|
|
442
|
+
const [name, ...steps] = symbolPathSegments(root.path);
|
|
443
|
+
const last = steps.at(-1);
|
|
444
|
+
if (name === undefined || last === undefined) {
|
|
445
|
+
within(rootType, root.place.get());
|
|
446
|
+
continue;
|
|
447
|
+
}
|
|
448
|
+
const holder = placeBelow(name, steps.slice(0, -1)).get();
|
|
449
|
+
if (isObject(holder)) {
|
|
450
|
+
keep(holder, propertyOf(last), rootType);
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
};
|
|
454
|
+
|
|
455
|
+
return { scalar, keepStoredValues };
|
|
456
|
+
};
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { elementaryType, stringTypeText } from "@tsplc/iec";
|
|
2
|
+
import type { PublishedGlobal, PublishedType } from "@tsplc/opcua/server";
|
|
3
|
+
import {
|
|
4
|
+
type Declaration,
|
|
5
|
+
type GlobalList,
|
|
6
|
+
resolveDeclarations,
|
|
7
|
+
type TypeReference,
|
|
8
|
+
} from "@tsplc/typegen/declarations";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* The emitted program's globals as CODESYS declares them: the ones a symbol attribute publishes,
|
|
12
|
+
* with the access it grants, and the retained ones.
|
|
13
|
+
*/
|
|
14
|
+
export const plcGlobals = (declarations: {
|
|
15
|
+
types: readonly Declaration[];
|
|
16
|
+
globals: readonly GlobalList[];
|
|
17
|
+
}) => {
|
|
18
|
+
const resolved = resolveDeclarations(declarations);
|
|
19
|
+
const resolve = (reference: TypeReference, where: string): PublishedType => {
|
|
20
|
+
switch (reference.kind) {
|
|
21
|
+
case "string":
|
|
22
|
+
return {
|
|
23
|
+
kind: "scalar",
|
|
24
|
+
iec: stringTypeText({
|
|
25
|
+
wide: reference.name === "WSTRING",
|
|
26
|
+
capacity: resolved.integer(reference.length),
|
|
27
|
+
}),
|
|
28
|
+
};
|
|
29
|
+
case "array": {
|
|
30
|
+
const [dimension, ...more] = resolved.bounds(reference);
|
|
31
|
+
if (!dimension || more.length) {
|
|
32
|
+
throw new Error(
|
|
33
|
+
`${where}: only one-dimensional arrays are published.`,
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
return {
|
|
37
|
+
kind: "array",
|
|
38
|
+
lower: dimension.start,
|
|
39
|
+
upper: dimension.end,
|
|
40
|
+
element: resolve(reference.element, where),
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
case "named":
|
|
44
|
+
return elementaryType(reference.name)
|
|
45
|
+
? { kind: "scalar", iec: reference.name.toUpperCase() }
|
|
46
|
+
: declared(resolved.declaration(reference.name));
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
const declared = (declaration: Declaration): PublishedType => {
|
|
50
|
+
switch (declaration.kind) {
|
|
51
|
+
case "alias":
|
|
52
|
+
return resolve(declaration.type, declaration.name);
|
|
53
|
+
case "enum":
|
|
54
|
+
return {
|
|
55
|
+
kind: "enum",
|
|
56
|
+
name: declaration.name,
|
|
57
|
+
base: declaration.base,
|
|
58
|
+
members: resolved.enumMembers(declaration),
|
|
59
|
+
};
|
|
60
|
+
case "struct":
|
|
61
|
+
return {
|
|
62
|
+
kind: "struct",
|
|
63
|
+
name: declaration.name,
|
|
64
|
+
members: declaration.members.map((member) => ({
|
|
65
|
+
name: member.name,
|
|
66
|
+
type: resolve(member.type, `${declaration.name}.${member.name}`),
|
|
67
|
+
})),
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
const variables = declarations.globals.flatMap((list) =>
|
|
72
|
+
list.constant
|
|
73
|
+
? []
|
|
74
|
+
: list.members.map((member) => ({
|
|
75
|
+
list: list.name,
|
|
76
|
+
name: member.name,
|
|
77
|
+
member,
|
|
78
|
+
})),
|
|
79
|
+
);
|
|
80
|
+
const global = (
|
|
81
|
+
variable: (typeof variables)[number],
|
|
82
|
+
access: PublishedGlobal["access"],
|
|
83
|
+
): PublishedGlobal => ({
|
|
84
|
+
list: variable.list,
|
|
85
|
+
name: variable.name,
|
|
86
|
+
access,
|
|
87
|
+
type: resolve(variable.member.type, `${variable.list}.${variable.name}`),
|
|
88
|
+
});
|
|
89
|
+
return {
|
|
90
|
+
published: variables.flatMap((variable) =>
|
|
91
|
+
variable.member.symbol ? [global(variable, variable.member.symbol)] : [],
|
|
92
|
+
),
|
|
93
|
+
retained: variables.flatMap((variable) =>
|
|
94
|
+
variable.member.retain ? [global(variable, "read")] : [],
|
|
95
|
+
),
|
|
96
|
+
};
|
|
97
|
+
};
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import { httpErrors } from "@tsplc/compiler/ir";
|
|
2
|
+
import type { TelemetryBatch } from "@tsplc/server/telemetry";
|
|
3
|
+
import { plcBufferBytes } from "@tsplc/server/wire";
|
|
4
|
+
import { telemetrySendIntervalMs } from "../codesys/telemetry-settings";
|
|
5
|
+
|
|
6
|
+
/** A record as the program's telemetry calls make it; see `telemetryRecord` in `@tsplc/server`. */
|
|
7
|
+
export type TelemetryRecord = Readonly<Record<string, unknown>>;
|
|
8
|
+
|
|
9
|
+
interface Queued {
|
|
10
|
+
sequence: number;
|
|
11
|
+
text: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Each task's records and the sender that hands them to `post`, as the compiled program's do: a
|
|
16
|
+
* ring of `capacity` records whose oldest unsent records are overwritten and counted as dropped
|
|
17
|
+
* when it fills, and a sender that runs at the start of the task's scans and posts one batch at a
|
|
18
|
+
* time, at most the PLC's HTTP buffer.
|
|
19
|
+
*/
|
|
20
|
+
export const createTelemetry = (options: {
|
|
21
|
+
/** The build id, as the debug manifest names it. */
|
|
22
|
+
build: string;
|
|
23
|
+
capacity: number;
|
|
24
|
+
/** Posts a batch's JSON, rejecting with a job error code; null when the program has no server to send to, which drops records. */
|
|
25
|
+
post: ((body: string) => Promise<unknown>) | null;
|
|
26
|
+
clock: () => number;
|
|
27
|
+
/** Says once that records are dropped for want of a server. */
|
|
28
|
+
log: (line: string) => void;
|
|
29
|
+
}) => {
|
|
30
|
+
const unsent = { reported: false };
|
|
31
|
+
const tasks = new Map<
|
|
32
|
+
string,
|
|
33
|
+
{
|
|
34
|
+
queued: Queued[];
|
|
35
|
+
next: number;
|
|
36
|
+
dropped: number;
|
|
37
|
+
sending: boolean;
|
|
38
|
+
/** When the oldest unsent record was queued, or the last failed send. */
|
|
39
|
+
waitingSince: number;
|
|
40
|
+
}
|
|
41
|
+
>();
|
|
42
|
+
const stateOf = (task: string) => {
|
|
43
|
+
const known = tasks.get(task);
|
|
44
|
+
if (known) {
|
|
45
|
+
return known;
|
|
46
|
+
}
|
|
47
|
+
const created = {
|
|
48
|
+
queued: [],
|
|
49
|
+
next: 0,
|
|
50
|
+
dropped: 0,
|
|
51
|
+
sending: false,
|
|
52
|
+
waitingSince: 0,
|
|
53
|
+
};
|
|
54
|
+
tasks.set(task, created);
|
|
55
|
+
return created;
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
const record = (task: string, value: TelemetryRecord) => {
|
|
59
|
+
const state = stateOf(task);
|
|
60
|
+
const text = JSON.stringify(value);
|
|
61
|
+
if (!state.queued.length) {
|
|
62
|
+
state.waitingSince = options.clock();
|
|
63
|
+
}
|
|
64
|
+
state.queued.push({ sequence: state.next, text });
|
|
65
|
+
state.next += 1;
|
|
66
|
+
if (state.queued.length > options.capacity) {
|
|
67
|
+
state.queued.shift();
|
|
68
|
+
state.dropped += 1;
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
/** The records from the oldest unsent on that fit one batch beside its other fields. */
|
|
73
|
+
const batchOf = (task: string, scan: number) => {
|
|
74
|
+
const state = stateOf(task);
|
|
75
|
+
const now = options.clock();
|
|
76
|
+
// The records are JSON already, so they are spliced in after the batch's other fields.
|
|
77
|
+
const envelope = (records: readonly string[], first: number) => {
|
|
78
|
+
const fields = JSON.stringify({
|
|
79
|
+
build: options.build,
|
|
80
|
+
task,
|
|
81
|
+
now,
|
|
82
|
+
scan,
|
|
83
|
+
first,
|
|
84
|
+
dropped: state.dropped,
|
|
85
|
+
} satisfies Omit<TelemetryBatch, "records">);
|
|
86
|
+
return `${fields.slice(0, -1)},"records":[${records.join(",")}]}`;
|
|
87
|
+
};
|
|
88
|
+
const first = state.queued[0]?.sequence ?? state.next;
|
|
89
|
+
const texts: string[] = [];
|
|
90
|
+
for (const queued of state.queued) {
|
|
91
|
+
const candidate = envelope([...texts, queued.text], first);
|
|
92
|
+
if (Buffer.byteLength(candidate, "utf8") > plcBufferBytes) {
|
|
93
|
+
break;
|
|
94
|
+
}
|
|
95
|
+
texts.push(queued.text);
|
|
96
|
+
}
|
|
97
|
+
return { body: envelope(texts, first), count: texts.length };
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
/** The sender's turn at the start of a scan of `task`. */
|
|
101
|
+
const send = (task: string, scan: number) => {
|
|
102
|
+
const state = stateOf(task);
|
|
103
|
+
const { post } = options;
|
|
104
|
+
if (!post) {
|
|
105
|
+
if (state.queued.length && !unsent.reported) {
|
|
106
|
+
unsent.reported = true;
|
|
107
|
+
options.log(
|
|
108
|
+
"Telemetry records are dropped: set server.baseUrlEnv and that environment variable to the app server's base URL.",
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
state.queued = [];
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
if (state.sending || !state.queued.length) {
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
const batch = batchOf(task, scan);
|
|
118
|
+
const full = batch.count < state.queued.length;
|
|
119
|
+
if (
|
|
120
|
+
!full &&
|
|
121
|
+
options.clock() - state.waitingSince < telemetrySendIntervalMs
|
|
122
|
+
) {
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
if (!batch.count) {
|
|
126
|
+
// One record larger than a batch can never be sent.
|
|
127
|
+
state.queued.shift();
|
|
128
|
+
state.dropped += 1;
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
const sent = state.queued.slice(0, batch.count);
|
|
132
|
+
state.sending = true;
|
|
133
|
+
const settle = () => {
|
|
134
|
+
state.queued = state.queued.filter((queued) => !sent.includes(queued));
|
|
135
|
+
state.waitingSince = options.clock();
|
|
136
|
+
};
|
|
137
|
+
post(batch.body)
|
|
138
|
+
.then(settle)
|
|
139
|
+
.catch((reason: unknown) => {
|
|
140
|
+
// The server can't take these records, so they are dropped rather than sent forever.
|
|
141
|
+
if (reason === httpErrors.refused) {
|
|
142
|
+
state.dropped += sent.length;
|
|
143
|
+
settle();
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
state.waitingSince = options.clock();
|
|
147
|
+
})
|
|
148
|
+
.finally(() => {
|
|
149
|
+
state.sending = false;
|
|
150
|
+
});
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
return { record, send };
|
|
154
|
+
};
|