@tsplc/compiler 0.4.4 → 0.5.1
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/dist/bun-runtime.js +1345 -0
- package/dist/bun-runtime.js.map +25 -0
- package/dist/cli.js +251 -46
- package/dist/cli.js.map +8 -6
- package/dist/codesys.js +11 -6
- package/dist/codesys.js.map +3 -3
- package/dist/elaborate-capture.js +5 -5
- package/dist/elaborate-capture.js.map +4 -4
- package/dist/index.js +15 -9
- package/dist/index.js.map +5 -5
- package/dist/project.js +45 -20
- package/dist/project.js.map +5 -4
- package/package.json +13 -9
- package/src/codesys/manifest.ts +8 -1
- package/src/frontend/elaborate/capture-protocol.ts +18 -0
- package/src/frontend/elaborate/capture.ts +3 -11
- package/src/frontend/elaborate/graph.ts +3 -2
- package/src/frontend/statements.ts +23 -6
- package/src/project/cli.ts +5 -0
- package/src/project/project-commands.ts +60 -6
- package/src/project/project.ts +4 -0
- package/src/runtime/async.ts +200 -0
- package/src/runtime/launch.ts +49 -0
- package/src/runtime/main.ts +54 -0
- package/src/runtime/process.ts +172 -0
- package/src/runtime/program.ts +142 -0
- package/src/runtime/retain.ts +100 -0
- package/src/runtime/runtime.ts +329 -0
- package/src/runtime/storage.ts +481 -0
- package/src/runtime/symbols.ts +153 -0
- package/src/runtime/telemetry.ts +123 -0
|
@@ -0,0 +1,481 @@
|
|
|
1
|
+
import type { DebugManifest, DebugType } from "@tsplc/debug/manifest";
|
|
2
|
+
import { typeOf } from "@tsplc/debug/paths";
|
|
3
|
+
import { elementaryType } from "@tsplc/iec";
|
|
4
|
+
import { isArrayIndexSegment, symbolPathSegments } from "@tsplc/opcua/contract";
|
|
5
|
+
import type { PublishedScalar, PublishedValue } from "@tsplc/opcua/server";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* The program's storage as the PLC keeps it. The program runs on its own objects; this module
|
|
9
|
+
* finds each PLC symbol among them through the build's debug manifest, which names every stored
|
|
10
|
+
* root's TypeScript path and target symbol, and converts values between the PLC's layout and the
|
|
11
|
+
* program's: a string-literal union is a string here and an enum there, `T | undefined` is
|
|
12
|
+
* `{ has, value }` there, and a `Record<Enum, T>` is an array indexed by the enum's values.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/** Where a value lives in the program's objects: read and written as the program reads and writes it. */
|
|
16
|
+
interface Place {
|
|
17
|
+
get(): unknown;
|
|
18
|
+
set(value: unknown): void;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** A PLC value: a scalar's, or an enum's numeric value. */
|
|
22
|
+
type Scalar = PublishedValue;
|
|
23
|
+
|
|
24
|
+
type RecordType = Extract<DebugType, { kind: "record" }>;
|
|
25
|
+
type ArrayType = Extract<DebugType, { kind: "array" }>;
|
|
26
|
+
type EnumType = Extract<DebugType, { kind: "enum" }>;
|
|
27
|
+
|
|
28
|
+
const joinSegments = (segments: readonly string[]) =>
|
|
29
|
+
segments.reduce((prefix, segment) =>
|
|
30
|
+
segment.startsWith("[") ? prefix + segment : `${prefix}.${segment}`,
|
|
31
|
+
);
|
|
32
|
+
|
|
33
|
+
/** The property a TypeScript path segment names: a field, or an index or numeric key without brackets. */
|
|
34
|
+
const propertyOf = (segment: string) =>
|
|
35
|
+
isArrayIndexSegment(segment) ? segment.slice(1, -1) : segment;
|
|
36
|
+
|
|
37
|
+
const isObject = (value: unknown): value is Record<string, unknown> =>
|
|
38
|
+
typeof value === "object" && value !== null;
|
|
39
|
+
|
|
40
|
+
/** The value at `key` of the object at `parent`; writing through a missing holder does nothing. */
|
|
41
|
+
const fieldPlace = (parent: Place, key: string): Place => ({
|
|
42
|
+
get: () => {
|
|
43
|
+
const holder = parent.get();
|
|
44
|
+
return isObject(holder) ? holder[key] : undefined;
|
|
45
|
+
},
|
|
46
|
+
set: (value) => {
|
|
47
|
+
const holder = parent.get();
|
|
48
|
+
if (isObject(holder)) {
|
|
49
|
+
holder[key] = value;
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* `T | undefined` is stored as the record `{ has, value }`, which the compiler names
|
|
56
|
+
* `Optional_<T>`; a literal union holds undefined as its enum's `Undefined` member instead.
|
|
57
|
+
*/
|
|
58
|
+
const optionalValue = (type: RecordType) => {
|
|
59
|
+
const [has, value, ...rest] = type.fields;
|
|
60
|
+
return type.name.startsWith("Optional_") &&
|
|
61
|
+
has?.name === "has" &&
|
|
62
|
+
value?.name === "value" &&
|
|
63
|
+
!rest.length
|
|
64
|
+
? value
|
|
65
|
+
: undefined;
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
const integerWrap = (bits: number, signed: boolean) => {
|
|
69
|
+
switch (bits) {
|
|
70
|
+
case 8:
|
|
71
|
+
return signed
|
|
72
|
+
? (value: number) => (value << 24) >> 24
|
|
73
|
+
: (value: number) => value & 0xff;
|
|
74
|
+
case 16:
|
|
75
|
+
return signed
|
|
76
|
+
? (value: number) => (value << 16) >> 16
|
|
77
|
+
: (value: number) => value & 0xffff;
|
|
78
|
+
default:
|
|
79
|
+
return signed
|
|
80
|
+
? (value: number) => value | 0
|
|
81
|
+
: (value: number) => value >>> 0;
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
/** `text` cut to at most `capacity` UTF-8 bytes, at a character boundary. */
|
|
86
|
+
const truncateUtf8 = (text: string, capacity: number) => {
|
|
87
|
+
if (Buffer.byteLength(text, "utf8") <= capacity) {
|
|
88
|
+
return text;
|
|
89
|
+
}
|
|
90
|
+
const kept: string[] = [];
|
|
91
|
+
let bytes = 0;
|
|
92
|
+
for (const character of text) {
|
|
93
|
+
bytes += Buffer.byteLength(character, "utf8");
|
|
94
|
+
if (bytes > capacity) {
|
|
95
|
+
break;
|
|
96
|
+
}
|
|
97
|
+
kept.push(character);
|
|
98
|
+
}
|
|
99
|
+
return kept.join("");
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* What storing a value of `type` does to it in the PLC, where TypeScript would keep it as it is:
|
|
104
|
+
* integers wrap to their width, a REAL rounds to single precision and a STRING keeps its first
|
|
105
|
+
* `capacity` bytes. Undefined for types whose values TypeScript already stores as the PLC does.
|
|
106
|
+
*/
|
|
107
|
+
export const storedValue = (
|
|
108
|
+
type: DebugType,
|
|
109
|
+
): ((value: unknown) => unknown) | undefined => {
|
|
110
|
+
if (type.kind !== "scalar") {
|
|
111
|
+
return undefined;
|
|
112
|
+
}
|
|
113
|
+
if (type.capacity !== undefined) {
|
|
114
|
+
const { capacity } = type;
|
|
115
|
+
return type.iec === "WSTRING"
|
|
116
|
+
? (value) =>
|
|
117
|
+
typeof value === "string" ? value.slice(0, capacity) : value
|
|
118
|
+
: (value) =>
|
|
119
|
+
typeof value === "string" ? truncateUtf8(value, capacity) : value;
|
|
120
|
+
}
|
|
121
|
+
const elementary = elementaryType(type.iec);
|
|
122
|
+
if (!elementary) {
|
|
123
|
+
return undefined;
|
|
124
|
+
}
|
|
125
|
+
if (elementary.family === "real") {
|
|
126
|
+
return elementary.bits === 32
|
|
127
|
+
? (value) => (typeof value === "number" ? Math.fround(value) : value)
|
|
128
|
+
: undefined;
|
|
129
|
+
}
|
|
130
|
+
const integer =
|
|
131
|
+
elementary.family === "integer" || elementary.family === "bits";
|
|
132
|
+
const signed = elementary.family === "integer" && elementary.signed;
|
|
133
|
+
if (elementary.family === "duration" && elementary.bits === 32) {
|
|
134
|
+
return storedValue({ kind: "scalar", iec: "UDINT" });
|
|
135
|
+
}
|
|
136
|
+
if (!integer) {
|
|
137
|
+
return undefined;
|
|
138
|
+
}
|
|
139
|
+
if (elementary.bits === 64) {
|
|
140
|
+
return (value) => {
|
|
141
|
+
if (typeof value !== "bigint") {
|
|
142
|
+
return value;
|
|
143
|
+
}
|
|
144
|
+
return signed ? BigInt.asIntN(64, value) : BigInt.asUintN(64, value);
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
const wrap = integerWrap(elementary.bits, signed);
|
|
148
|
+
return (value) =>
|
|
149
|
+
typeof value === "number" && Number.isFinite(value) ? wrap(value) : value;
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
/** A scalar's PLC value when the program holds nothing there, as for a variant field it lacks. */
|
|
153
|
+
const scalarDefault = (type: DebugType): Scalar => {
|
|
154
|
+
if (type.kind === "enum") {
|
|
155
|
+
return Math.min(...Object.values(type.members));
|
|
156
|
+
}
|
|
157
|
+
if (type.kind !== "scalar") {
|
|
158
|
+
return 0;
|
|
159
|
+
}
|
|
160
|
+
if (type.capacity !== undefined) {
|
|
161
|
+
return "";
|
|
162
|
+
}
|
|
163
|
+
const elementary = elementaryType(type.iec);
|
|
164
|
+
if (elementary?.family === "bool") {
|
|
165
|
+
return false;
|
|
166
|
+
}
|
|
167
|
+
const wide =
|
|
168
|
+
(elementary?.family === "integer" || elementary?.family === "bits") &&
|
|
169
|
+
elementary.bits === 64;
|
|
170
|
+
return wide ? 0n : 0;
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
export const createStorage = (
|
|
174
|
+
manifest: DebugManifest,
|
|
175
|
+
/** A module-level binding of the program by name, or undefined when there is none. */
|
|
176
|
+
binding: (name: string) => unknown,
|
|
177
|
+
) => {
|
|
178
|
+
const type = (id: string) => typeOf(manifest, id);
|
|
179
|
+
const textMaps = new Map<
|
|
180
|
+
EnumType,
|
|
181
|
+
ReturnType<typeof textMapsOf> | undefined
|
|
182
|
+
>();
|
|
183
|
+
/** A string-literal union's strings by member value and back; undefined for a TypeScript enum. */
|
|
184
|
+
const enumTexts = (enumeration: EnumType) => {
|
|
185
|
+
if (!textMaps.has(enumeration)) {
|
|
186
|
+
textMaps.set(enumeration, textMapsOf(enumeration));
|
|
187
|
+
}
|
|
188
|
+
return textMaps.get(enumeration);
|
|
189
|
+
};
|
|
190
|
+
const textMapsOf = (enumeration: EnumType) => {
|
|
191
|
+
const texts = enumeration.texts;
|
|
192
|
+
if (!texts) {
|
|
193
|
+
return undefined;
|
|
194
|
+
}
|
|
195
|
+
const byText = new Map(
|
|
196
|
+
Object.entries(texts).flatMap(([member, text]) => {
|
|
197
|
+
const value = enumeration.members[member];
|
|
198
|
+
return value === undefined ? [] : [[text, value] as const];
|
|
199
|
+
}),
|
|
200
|
+
);
|
|
201
|
+
const byValue = new Map(
|
|
202
|
+
Object.entries(enumeration.members).map(([member, value]) => [
|
|
203
|
+
value,
|
|
204
|
+
texts[member],
|
|
205
|
+
]),
|
|
206
|
+
);
|
|
207
|
+
return { byText, byValue };
|
|
208
|
+
};
|
|
209
|
+
|
|
210
|
+
/** The program's key for index `index` of an array: its position, or the enum member's key of a `Record<Enum, T>`. */
|
|
211
|
+
const elementKey = (array: ArrayType, index: number) => {
|
|
212
|
+
const keyType = array.index === undefined ? undefined : type(array.index);
|
|
213
|
+
if (keyType?.kind !== "enum") {
|
|
214
|
+
return String(index - array.lower);
|
|
215
|
+
}
|
|
216
|
+
return enumTexts(keyType)?.byValue.get(index) ?? String(index);
|
|
217
|
+
};
|
|
218
|
+
|
|
219
|
+
/** The program's value for a PLC scalar of `scalarType`. */
|
|
220
|
+
const programValue = (scalarType: DebugType, value: Scalar): unknown => {
|
|
221
|
+
if (scalarType.kind === "enum") {
|
|
222
|
+
const texts = enumTexts(scalarType);
|
|
223
|
+
return texts ? texts.byValue.get(Number(value)) : value;
|
|
224
|
+
}
|
|
225
|
+
return value;
|
|
226
|
+
};
|
|
227
|
+
|
|
228
|
+
/** The PLC's value of the program's `value` for a scalar of `scalarType`. */
|
|
229
|
+
const plcValue = (scalarType: DebugType, value: unknown): Scalar => {
|
|
230
|
+
if (scalarType.kind === "enum") {
|
|
231
|
+
const texts = enumTexts(scalarType);
|
|
232
|
+
if (texts) {
|
|
233
|
+
return typeof value === "string"
|
|
234
|
+
? (texts.byText.get(value) ?? scalarDefault(scalarType))
|
|
235
|
+
: (scalarType.members.Undefined ?? scalarDefault(scalarType));
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
if (
|
|
239
|
+
typeof value === "boolean" ||
|
|
240
|
+
typeof value === "number" ||
|
|
241
|
+
typeof value === "bigint" ||
|
|
242
|
+
typeof value === "string"
|
|
243
|
+
) {
|
|
244
|
+
return value;
|
|
245
|
+
}
|
|
246
|
+
return scalarDefault(scalarType);
|
|
247
|
+
};
|
|
248
|
+
|
|
249
|
+
/** The program's value for a whole PLC default of `valueType`, for an optional that becomes present. */
|
|
250
|
+
const defaultValue = (valueType: DebugType): unknown => {
|
|
251
|
+
switch (valueType.kind) {
|
|
252
|
+
case "scalar":
|
|
253
|
+
case "enum":
|
|
254
|
+
return programValue(valueType, scalarDefault(valueType));
|
|
255
|
+
case "array":
|
|
256
|
+
return Array.from({ length: valueType.length }, () =>
|
|
257
|
+
defaultValue(type(valueType.element)),
|
|
258
|
+
);
|
|
259
|
+
case "record":
|
|
260
|
+
if (optionalValue(valueType)) {
|
|
261
|
+
return undefined;
|
|
262
|
+
}
|
|
263
|
+
return Object.fromEntries(
|
|
264
|
+
valueType.fields.map((field) => [
|
|
265
|
+
field.name,
|
|
266
|
+
defaultValue(type(field.type)),
|
|
267
|
+
]),
|
|
268
|
+
);
|
|
269
|
+
}
|
|
270
|
+
};
|
|
271
|
+
|
|
272
|
+
/** One step down from a PLC value at `place` to its member or element named `segment`. */
|
|
273
|
+
const child = (
|
|
274
|
+
parent: { type: DebugType; place: Place },
|
|
275
|
+
segment: string,
|
|
276
|
+
): { type: DebugType; place: Place } | undefined => {
|
|
277
|
+
const { place } = parent;
|
|
278
|
+
if (parent.type.kind === "array" && isArrayIndexSegment(segment)) {
|
|
279
|
+
const index = Number(segment.slice(1, -1));
|
|
280
|
+
const array = parent.type;
|
|
281
|
+
if (index < array.lower || index >= array.lower + array.length) {
|
|
282
|
+
return undefined;
|
|
283
|
+
}
|
|
284
|
+
return {
|
|
285
|
+
type: type(array.element),
|
|
286
|
+
place: fieldPlace(place, elementKey(array, index)),
|
|
287
|
+
};
|
|
288
|
+
}
|
|
289
|
+
if (parent.type.kind !== "record") {
|
|
290
|
+
return undefined;
|
|
291
|
+
}
|
|
292
|
+
const optional = optionalValue(parent.type);
|
|
293
|
+
if (optional && segment === "has") {
|
|
294
|
+
return {
|
|
295
|
+
type: { kind: "scalar", iec: "BOOL" },
|
|
296
|
+
place: {
|
|
297
|
+
get: () => place.get() !== undefined,
|
|
298
|
+
set: (present) => {
|
|
299
|
+
if (!present) {
|
|
300
|
+
place.set(undefined);
|
|
301
|
+
} else if (place.get() === undefined) {
|
|
302
|
+
place.set(defaultValue(type(optional.type)));
|
|
303
|
+
}
|
|
304
|
+
},
|
|
305
|
+
},
|
|
306
|
+
};
|
|
307
|
+
}
|
|
308
|
+
if (optional && segment === "value") {
|
|
309
|
+
return { type: type(optional.type), place };
|
|
310
|
+
}
|
|
311
|
+
const field = parent.type.fields.find(
|
|
312
|
+
(candidate) => candidate.target === segment,
|
|
313
|
+
);
|
|
314
|
+
return (
|
|
315
|
+
field && { type: type(field.type), place: fieldPlace(place, field.name) }
|
|
316
|
+
);
|
|
317
|
+
};
|
|
318
|
+
|
|
319
|
+
/** The place of a stored root, by its TypeScript path such as `program.diverters`. */
|
|
320
|
+
const rootPlace = (path: string): Place | undefined => {
|
|
321
|
+
const [name, ...steps] = symbolPathSegments(path);
|
|
322
|
+
if (name === undefined || binding(name) === undefined) {
|
|
323
|
+
return undefined;
|
|
324
|
+
}
|
|
325
|
+
const start: Place = {
|
|
326
|
+
get: () => binding(name),
|
|
327
|
+
set: () => {
|
|
328
|
+
throw new Error(
|
|
329
|
+
`${name} is a module binding; the runtime can't replace it.`,
|
|
330
|
+
);
|
|
331
|
+
},
|
|
332
|
+
};
|
|
333
|
+
return steps.reduce(
|
|
334
|
+
(place, step) => fieldPlace(place, propertyOf(step)),
|
|
335
|
+
start,
|
|
336
|
+
);
|
|
337
|
+
};
|
|
338
|
+
|
|
339
|
+
const roots = manifest.roots.flatMap((root) => {
|
|
340
|
+
const place = rootPlace(root.path);
|
|
341
|
+
return place ? [{ ...root, place }] : [];
|
|
342
|
+
});
|
|
343
|
+
const rootsByTarget = new Map(roots.map((root) => [root.target, root]));
|
|
344
|
+
|
|
345
|
+
/** The PLC value at a symbol path, such as `Line.stations[3].work.bins`, among the program's objects. */
|
|
346
|
+
const locate = (path: string) => {
|
|
347
|
+
const segments = symbolPathSegments(path);
|
|
348
|
+
// The longest prefix that names a root: a root can sit inside another root's list.
|
|
349
|
+
const length = Array.from(
|
|
350
|
+
{ length: segments.length },
|
|
351
|
+
(_, offset) => segments.length - offset,
|
|
352
|
+
).find((prefix) =>
|
|
353
|
+
rootsByTarget.has(joinSegments(segments.slice(0, prefix))),
|
|
354
|
+
);
|
|
355
|
+
const root =
|
|
356
|
+
length === undefined
|
|
357
|
+
? undefined
|
|
358
|
+
: rootsByTarget.get(joinSegments(segments.slice(0, length)));
|
|
359
|
+
if (!root) {
|
|
360
|
+
return undefined;
|
|
361
|
+
}
|
|
362
|
+
return segments
|
|
363
|
+
.slice(length)
|
|
364
|
+
.reduce<{ type: DebugType; place: Place } | undefined>(
|
|
365
|
+
(node, segment) => node && child(node, segment),
|
|
366
|
+
{ type: type(root.type), place: root.place },
|
|
367
|
+
);
|
|
368
|
+
};
|
|
369
|
+
|
|
370
|
+
/** Reads and writes the scalar at a symbol path as the PLC holds it; undefined when the program has no such storage. */
|
|
371
|
+
const scalar = (path: string): PublishedScalar | undefined => {
|
|
372
|
+
const node = locate(path);
|
|
373
|
+
if (!node || (node.type.kind !== "scalar" && node.type.kind !== "enum")) {
|
|
374
|
+
return undefined;
|
|
375
|
+
}
|
|
376
|
+
const { type: scalarType, place } = node;
|
|
377
|
+
return {
|
|
378
|
+
read: () => plcValue(scalarType, place.get()),
|
|
379
|
+
write: (value) => place.set(programValue(scalarType, value)),
|
|
380
|
+
};
|
|
381
|
+
};
|
|
382
|
+
|
|
383
|
+
/**
|
|
384
|
+
* Makes every stored field keep its value as the PLC stores it (`storedValue`), however the
|
|
385
|
+
* program assigns it: each becomes an accessor, and an object or array stored in a field is
|
|
386
|
+
* treated the same way, also when the program replaces it.
|
|
387
|
+
*/
|
|
388
|
+
const keepStoredValues = () => {
|
|
389
|
+
const kept = new WeakSet<object>();
|
|
390
|
+
const keep = (
|
|
391
|
+
holder: Record<string, unknown>,
|
|
392
|
+
key: string,
|
|
393
|
+
valueType: DebugType,
|
|
394
|
+
) => {
|
|
395
|
+
const descriptor = Object.getOwnPropertyDescriptor(holder, key);
|
|
396
|
+
if (
|
|
397
|
+
!descriptor ||
|
|
398
|
+
!("value" in descriptor) ||
|
|
399
|
+
!descriptor.writable ||
|
|
400
|
+
!descriptor.configurable
|
|
401
|
+
) {
|
|
402
|
+
return;
|
|
403
|
+
}
|
|
404
|
+
const store = storing(valueType);
|
|
405
|
+
if (!store) {
|
|
406
|
+
return;
|
|
407
|
+
}
|
|
408
|
+
let stored = store(descriptor.value);
|
|
409
|
+
Object.defineProperty(holder, key, {
|
|
410
|
+
configurable: true,
|
|
411
|
+
enumerable: descriptor.enumerable,
|
|
412
|
+
get: () => stored,
|
|
413
|
+
set: (value: unknown) => {
|
|
414
|
+
stored = store(value);
|
|
415
|
+
},
|
|
416
|
+
});
|
|
417
|
+
};
|
|
418
|
+
/** What storing into a field of `valueType` does: converts a scalar, or treats an object's own fields. */
|
|
419
|
+
const storing = (
|
|
420
|
+
valueType: DebugType,
|
|
421
|
+
): ((value: unknown) => unknown) | undefined => {
|
|
422
|
+
if (valueType.kind === "record") {
|
|
423
|
+
const optional = optionalValue(valueType);
|
|
424
|
+
if (optional) {
|
|
425
|
+
return storing(type(optional.type));
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
if (valueType.kind === "record" || valueType.kind === "array") {
|
|
429
|
+
return (value) => {
|
|
430
|
+
within(valueType, value);
|
|
431
|
+
return value;
|
|
432
|
+
};
|
|
433
|
+
}
|
|
434
|
+
return storedValue(valueType);
|
|
435
|
+
};
|
|
436
|
+
/** Treats the fields or elements of a stored object or array. */
|
|
437
|
+
const within = (valueType: DebugType, value: unknown) => {
|
|
438
|
+
if (!isObject(value) || kept.has(value)) {
|
|
439
|
+
return;
|
|
440
|
+
}
|
|
441
|
+
kept.add(value);
|
|
442
|
+
if (valueType.kind === "record") {
|
|
443
|
+
for (const field of valueType.fields) {
|
|
444
|
+
keep(value, field.name, type(field.type));
|
|
445
|
+
}
|
|
446
|
+
return;
|
|
447
|
+
}
|
|
448
|
+
if (valueType.kind === "array") {
|
|
449
|
+
for (
|
|
450
|
+
let index = valueType.lower;
|
|
451
|
+
index < valueType.lower + valueType.length;
|
|
452
|
+
index++
|
|
453
|
+
) {
|
|
454
|
+
keep(value, elementKey(valueType, index), type(valueType.element));
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
};
|
|
458
|
+
for (const root of roots) {
|
|
459
|
+
const rootType = type(root.type);
|
|
460
|
+
const [name, ...steps] = symbolPathSegments(root.path);
|
|
461
|
+
const holderPath = steps.slice(0, -1);
|
|
462
|
+
const last = steps.at(-1);
|
|
463
|
+
if (name === undefined || last === undefined) {
|
|
464
|
+
// A module binding itself has no holder to treat; what it holds still does.
|
|
465
|
+
within(rootType, root.place.get());
|
|
466
|
+
continue;
|
|
467
|
+
}
|
|
468
|
+
const start: Place = { get: () => binding(name), set: () => {} };
|
|
469
|
+
const holder = holderPath
|
|
470
|
+
.reduce((place, step) => fieldPlace(place, propertyOf(step)), start)
|
|
471
|
+
.get();
|
|
472
|
+
if (isObject(holder)) {
|
|
473
|
+
keep(holder, propertyOf(last), rootType);
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
};
|
|
477
|
+
|
|
478
|
+
return { scalar, keepStoredValues };
|
|
479
|
+
};
|
|
480
|
+
|
|
481
|
+
export type Storage = ReturnType<typeof createStorage>;
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import { elementaryType } from "@tsplc/iec";
|
|
2
|
+
import type { PublishedGlobal, PublishedType } from "@tsplc/opcua/server";
|
|
3
|
+
import type {
|
|
4
|
+
Declaration,
|
|
5
|
+
GlobalList,
|
|
6
|
+
TypeReference,
|
|
7
|
+
} from "@tsplc/typegen/declarations";
|
|
8
|
+
|
|
9
|
+
/** An integer literal of an emitted declaration, such as an array bound or a STRING capacity. */
|
|
10
|
+
const integer = (text: string, where: string) => {
|
|
11
|
+
if (!/^-?\d+$/.test(text.trim())) {
|
|
12
|
+
throw new Error(`${where}: ${text} is not an integer literal.`);
|
|
13
|
+
}
|
|
14
|
+
return Number(text);
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
/** An enum's members with their values: a member without one follows the member before it. */
|
|
18
|
+
const enumMembers = (declaration: Extract<Declaration, { kind: "enum" }>) => {
|
|
19
|
+
const members: { name: string; value: number }[] = [];
|
|
20
|
+
for (const member of declaration.members) {
|
|
21
|
+
members.push({
|
|
22
|
+
name: member.name,
|
|
23
|
+
value:
|
|
24
|
+
member.value === undefined
|
|
25
|
+
? (members.at(-1)?.value ?? -1) + 1
|
|
26
|
+
: integer(member.value, declaration.name),
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
return members;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* The emitted program's globals as CODESYS declares them: the ones a symbol attribute publishes,
|
|
34
|
+
* with the access it grants, and the retained ones.
|
|
35
|
+
*/
|
|
36
|
+
export const plcGlobals = (declarations: {
|
|
37
|
+
types: readonly Declaration[];
|
|
38
|
+
globals: readonly GlobalList[];
|
|
39
|
+
}) => {
|
|
40
|
+
const types = new Map(
|
|
41
|
+
declarations.types.map((declaration) => [
|
|
42
|
+
declaration.name.toUpperCase(),
|
|
43
|
+
declaration,
|
|
44
|
+
]),
|
|
45
|
+
);
|
|
46
|
+
const resolve = (reference: TypeReference, where: string): PublishedType => {
|
|
47
|
+
switch (reference.kind) {
|
|
48
|
+
case "string":
|
|
49
|
+
return {
|
|
50
|
+
kind: "scalar",
|
|
51
|
+
iec: `${reference.name}(${integer(reference.length, where)})`,
|
|
52
|
+
};
|
|
53
|
+
case "array": {
|
|
54
|
+
const [dimension, ...more] = reference.dimensions;
|
|
55
|
+
if (!dimension || more.length) {
|
|
56
|
+
throw new Error(
|
|
57
|
+
`${where}: only one-dimensional arrays are published.`,
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
return {
|
|
61
|
+
kind: "array",
|
|
62
|
+
lower: integer(dimension.lower, where),
|
|
63
|
+
upper: integer(dimension.upper, where),
|
|
64
|
+
element: resolve(reference.element, where),
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
case "named": {
|
|
68
|
+
if (elementaryType(reference.name)) {
|
|
69
|
+
return { kind: "scalar", iec: reference.name.toUpperCase() };
|
|
70
|
+
}
|
|
71
|
+
const declaration = types.get(reference.name.toUpperCase());
|
|
72
|
+
if (!declaration) {
|
|
73
|
+
throw new Error(`${where}: type ${reference.name} is not declared.`);
|
|
74
|
+
}
|
|
75
|
+
return declared(declaration);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
const declared = (declaration: Declaration): PublishedType => {
|
|
80
|
+
switch (declaration.kind) {
|
|
81
|
+
case "alias":
|
|
82
|
+
return resolve(declaration.type, declaration.name);
|
|
83
|
+
case "enum":
|
|
84
|
+
return {
|
|
85
|
+
kind: "enum",
|
|
86
|
+
name: declaration.name,
|
|
87
|
+
base: declaration.base,
|
|
88
|
+
members: enumMembers(declaration),
|
|
89
|
+
};
|
|
90
|
+
case "struct":
|
|
91
|
+
return {
|
|
92
|
+
kind: "struct",
|
|
93
|
+
name: declaration.name,
|
|
94
|
+
members: declaration.members.map((member) => ({
|
|
95
|
+
name: member.name,
|
|
96
|
+
type: resolve(member.type, `${declaration.name}.${member.name}`),
|
|
97
|
+
})),
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
const variables = declarations.globals.flatMap((list) =>
|
|
102
|
+
list.constant
|
|
103
|
+
? []
|
|
104
|
+
: list.members.map((member) => ({
|
|
105
|
+
list: list.name,
|
|
106
|
+
name: member.name,
|
|
107
|
+
member,
|
|
108
|
+
})),
|
|
109
|
+
);
|
|
110
|
+
const global = (
|
|
111
|
+
variable: (typeof variables)[number],
|
|
112
|
+
access: PublishedGlobal["access"],
|
|
113
|
+
): PublishedGlobal => ({
|
|
114
|
+
list: variable.list,
|
|
115
|
+
name: variable.name,
|
|
116
|
+
access,
|
|
117
|
+
type: resolve(variable.member.type, `${variable.list}.${variable.name}`),
|
|
118
|
+
});
|
|
119
|
+
return {
|
|
120
|
+
published: variables.flatMap((variable) =>
|
|
121
|
+
variable.member.symbol ? [global(variable, variable.member.symbol)] : [],
|
|
122
|
+
),
|
|
123
|
+
retained: variables.flatMap((variable) =>
|
|
124
|
+
variable.member.retain ? [global(variable, "read")] : [],
|
|
125
|
+
),
|
|
126
|
+
};
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
/** Every scalar of a global, by symbol path, with its type: each struct member and array element. */
|
|
130
|
+
export const scalarPaths = (
|
|
131
|
+
global: Pick<PublishedGlobal, "list" | "name" | "type">,
|
|
132
|
+
): { path: string; type: PublishedType }[] => {
|
|
133
|
+
const walk = (
|
|
134
|
+
path: string,
|
|
135
|
+
type: PublishedType,
|
|
136
|
+
): { path: string; type: PublishedType }[] => {
|
|
137
|
+
switch (type.kind) {
|
|
138
|
+
case "scalar":
|
|
139
|
+
case "enum":
|
|
140
|
+
return [{ path, type }];
|
|
141
|
+
case "struct":
|
|
142
|
+
return type.members.flatMap((member) =>
|
|
143
|
+
walk(`${path}.${member.name}`, member.type),
|
|
144
|
+
);
|
|
145
|
+
case "array":
|
|
146
|
+
return Array.from(
|
|
147
|
+
{ length: type.upper - type.lower + 1 },
|
|
148
|
+
(_, offset) => walk(`${path}[${type.lower + offset}]`, type.element),
|
|
149
|
+
).flat();
|
|
150
|
+
}
|
|
151
|
+
};
|
|
152
|
+
return walk(`${global.list}.${global.name}`, global.type);
|
|
153
|
+
};
|