@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,168 @@
|
|
|
1
|
+
import { readFile } from "node:fs/promises";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { httpErrors, jobError } from "@tsplc/compiler/ir";
|
|
4
|
+
import { emittedFiles } from "@tsplc/config/project-files";
|
|
5
|
+
import { elementaryType, integerRange } from "@tsplc/iec";
|
|
6
|
+
import type { PublishedScalar } from "@tsplc/opcua/server";
|
|
7
|
+
import {
|
|
8
|
+
encodeSlotRequest,
|
|
9
|
+
gatewayPollMs,
|
|
10
|
+
plcBufferBytes,
|
|
11
|
+
type ServiceChannel,
|
|
12
|
+
slotIdentity,
|
|
13
|
+
slotStatus,
|
|
14
|
+
} from "@tsplc/server/wire";
|
|
15
|
+
import { parseServiceChannels } from "../codesys/service-channels";
|
|
16
|
+
import { type HttpSettings, httpCall } from "./async";
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* How the program's service calls and telemetry reach the app server, as `server.transport`
|
|
20
|
+
* compiled them. A call rejects with the compiled call's error code, a number, never an Error.
|
|
21
|
+
*/
|
|
22
|
+
export interface Transport {
|
|
23
|
+
/** Sends a JSON body to a route, such as a procedure's `/tsplc/services/…` or `/tsplc/telemetry`. */
|
|
24
|
+
call: (route: string, body: string) => Promise<unknown>;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** POSTs each body to the route under the server's base URL, with its origin's bearer token. */
|
|
28
|
+
export const httpTransport = (
|
|
29
|
+
settings: HttpSettings,
|
|
30
|
+
baseUrl: string,
|
|
31
|
+
): Transport => ({
|
|
32
|
+
call: (route, body) =>
|
|
33
|
+
httpCall(settings, `${baseUrl}${route}`, { method: "POST", body }),
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
/** The slots the build compiled, from the emitted `machines.ts`. */
|
|
37
|
+
export const readServiceChannels = async (
|
|
38
|
+
st: string,
|
|
39
|
+
): Promise<ServiceChannel[]> =>
|
|
40
|
+
parseServiceChannels(await readFile(join(st, emittedFiles.machines), "utf8"));
|
|
41
|
+
|
|
42
|
+
const [, udintMaximum] = integerRange(elementaryType("UDINT"));
|
|
43
|
+
|
|
44
|
+
/** A slot's sequence is a UDINT; wrapping to 0 retires its epoch, as the compiled block does. */
|
|
45
|
+
const sequenceLimit = Number(udintMaximum) + 1;
|
|
46
|
+
|
|
47
|
+
/** How often a waiting call looks for its reply, a few times within each of the gateway's polls. */
|
|
48
|
+
const replyPollMs = gatewayPollMs / 4;
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* A reply's status and body after its identity, read as the compiled block reads them: at least
|
|
52
|
+
* 4 bytes, `0|<json>`, `4|…` for a request the server refused, else a failure.
|
|
53
|
+
*/
|
|
54
|
+
const decodeReply = (rest: string): unknown => {
|
|
55
|
+
if (Buffer.byteLength(rest, "utf8") < 4) {
|
|
56
|
+
throw jobError.malformedResponse;
|
|
57
|
+
}
|
|
58
|
+
if (rest.startsWith(`${slotStatus.refused}|`)) {
|
|
59
|
+
throw httpErrors.refused;
|
|
60
|
+
}
|
|
61
|
+
if (!rest.startsWith(`${slotStatus.ok}|`)) {
|
|
62
|
+
throw jobError.network;
|
|
63
|
+
}
|
|
64
|
+
try {
|
|
65
|
+
return JSON.parse(rest.slice(2));
|
|
66
|
+
} catch {
|
|
67
|
+
throw jobError.malformedResponse;
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* The OPC UA slots a compiled program exchanges its calls through, held in this process and
|
|
73
|
+
* published as the program's globals are, so the app server's gateway serves them as it serves a
|
|
74
|
+
* PLC's. Each call claims a free slot of its route whose epoch the gateway has set, writes its
|
|
75
|
+
* request and then commits it with its identity, and waits for the reply that names it.
|
|
76
|
+
*/
|
|
77
|
+
export const opcuaSlots = (
|
|
78
|
+
channels: readonly ServiceChannel[],
|
|
79
|
+
timeoutMs: number,
|
|
80
|
+
) => {
|
|
81
|
+
const values = new Map<string, string>(
|
|
82
|
+
channels.flatMap((channel) =>
|
|
83
|
+
[
|
|
84
|
+
channel.request,
|
|
85
|
+
channel.requestId,
|
|
86
|
+
channel.reply,
|
|
87
|
+
channel.replyId,
|
|
88
|
+
channel.epoch,
|
|
89
|
+
].map((path) => [path, ""] as const),
|
|
90
|
+
),
|
|
91
|
+
);
|
|
92
|
+
const read = (path: string) => values.get(path) ?? "";
|
|
93
|
+
const slots = new Map(
|
|
94
|
+
channels.map((channel) => [channel, { inUse: false, sequence: 0 }]),
|
|
95
|
+
);
|
|
96
|
+
|
|
97
|
+
const awaitReply = async (
|
|
98
|
+
channel: ServiceChannel,
|
|
99
|
+
epoch: string,
|
|
100
|
+
identity: string,
|
|
101
|
+
deadline: number,
|
|
102
|
+
): Promise<unknown> => {
|
|
103
|
+
const prefix = `${identity}|`;
|
|
104
|
+
for (;;) {
|
|
105
|
+
if (performance.now() >= deadline) {
|
|
106
|
+
throw jobError.timeout;
|
|
107
|
+
}
|
|
108
|
+
if (read(channel.epoch) !== epoch) {
|
|
109
|
+
throw jobError.network;
|
|
110
|
+
}
|
|
111
|
+
const reply = read(channel.reply);
|
|
112
|
+
if (read(channel.replyId) === identity && reply.startsWith(prefix)) {
|
|
113
|
+
return decodeReply(reply.slice(prefix.length));
|
|
114
|
+
}
|
|
115
|
+
await Bun.sleep(replyPollMs);
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
const call = async (route: string, body: string): Promise<unknown> => {
|
|
120
|
+
const deadline = performance.now() + timeoutMs;
|
|
121
|
+
const channel = channels.find(
|
|
122
|
+
(candidate) =>
|
|
123
|
+
candidate.path === route &&
|
|
124
|
+
read(candidate.epoch) !== "" &&
|
|
125
|
+
!slots.get(candidate)?.inUse,
|
|
126
|
+
);
|
|
127
|
+
const slot = channel && slots.get(channel);
|
|
128
|
+
if (!channel || !slot) {
|
|
129
|
+
throw jobError.noRequestSlot;
|
|
130
|
+
}
|
|
131
|
+
slot.inUse = true;
|
|
132
|
+
try {
|
|
133
|
+
slot.sequence = (slot.sequence + 1) % sequenceLimit;
|
|
134
|
+
if (slot.sequence === 0) {
|
|
135
|
+
values.set(channel.epoch, "");
|
|
136
|
+
throw jobError.network;
|
|
137
|
+
}
|
|
138
|
+
const epoch = read(channel.epoch);
|
|
139
|
+
const identity = slotIdentity(epoch, slot.sequence);
|
|
140
|
+
const request = encodeSlotRequest(identity, body);
|
|
141
|
+
if (Buffer.byteLength(request, "utf8") > plcBufferBytes) {
|
|
142
|
+
throw jobError.tooLarge;
|
|
143
|
+
}
|
|
144
|
+
values.set(channel.request, request);
|
|
145
|
+
// The identity commits the request: the gateway ignores a request it doesn't name.
|
|
146
|
+
values.set(channel.requestId, identity);
|
|
147
|
+
return await awaitReply(channel, epoch, identity, deadline);
|
|
148
|
+
} finally {
|
|
149
|
+
values.set(channel.requestId, "");
|
|
150
|
+
values.set(channel.request, "");
|
|
151
|
+
slot.inUse = false;
|
|
152
|
+
}
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
return {
|
|
156
|
+
transport: { call } satisfies Transport,
|
|
157
|
+
/** The published scalar of a slot's symbol, or undefined for any other path. */
|
|
158
|
+
scalar: (path: string): PublishedScalar | undefined =>
|
|
159
|
+
values.has(path)
|
|
160
|
+
? {
|
|
161
|
+
read: () => read(path),
|
|
162
|
+
write: (value) => {
|
|
163
|
+
values.set(path, String(value));
|
|
164
|
+
},
|
|
165
|
+
}
|
|
166
|
+
: undefined,
|
|
167
|
+
};
|
|
168
|
+
};
|
package/stdlib/clock.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { plcTime } from "./plc-time";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Milliseconds since the process started, as the PLC's `now()` counts them in a DINT; used when a
|
|
5
|
+
* program runs under Bun. Each call reads the clock: holding one time for a scan is the Bun
|
|
6
|
+
* runtime's to do, as only it knows when a scan runs.
|
|
7
|
+
*/
|
|
8
|
+
export const now = (): number => plcTime() | 0;
|
|
@@ -0,0 +1,534 @@
|
|
|
1
|
+
// PLC implementation source: a plain TypeScript program's collections. Setup fixes each one's
|
|
2
|
+
// capacity, so the PLC stores it in arrays allocated once, and every loop over it is bounded by
|
|
3
|
+
// that capacity. The same classes run under Bun for setup, tests and simulation.
|
|
4
|
+
import type { UINT } from "../types/index";
|
|
5
|
+
|
|
6
|
+
/** Bun's, at setup and in simulation; the PLC compiles it as the dialect's `copy`. */
|
|
7
|
+
declare function structuredClone<T>(value: T): T;
|
|
8
|
+
|
|
9
|
+
/** What a full collection does with one more item: refuse it, or make room by dropping the oldest. */
|
|
10
|
+
export type OnFull = "reject" | "dropOldest";
|
|
11
|
+
|
|
12
|
+
export interface BoundedOptions {
|
|
13
|
+
readonly onFull: OnFull;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** Above this capacity a map with number keys hashes them; at or below it, a scan of every slot is as quick. */
|
|
17
|
+
export const linearLimit = 32;
|
|
18
|
+
|
|
19
|
+
/** A first-in, first-out queue of at most `capacity` items, in a ring. */
|
|
20
|
+
export class BoundedQueue<T> {
|
|
21
|
+
private readonly items: T[];
|
|
22
|
+
private first: UINT = 0;
|
|
23
|
+
private count: UINT = 0;
|
|
24
|
+
/** How many items the queue refused or dropped because it was full. */
|
|
25
|
+
overflowed = 0;
|
|
26
|
+
|
|
27
|
+
/** What an empty slot holds; reads fall back to it where TypeScript can't see the slot is filled. */
|
|
28
|
+
private readonly blank: T;
|
|
29
|
+
|
|
30
|
+
constructor(
|
|
31
|
+
readonly capacity: number,
|
|
32
|
+
blank: () => T,
|
|
33
|
+
private readonly options: BoundedOptions,
|
|
34
|
+
) {
|
|
35
|
+
this.items = Array.from({ length: capacity }, blank);
|
|
36
|
+
this.blank = blank();
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
private item(slot: UINT): T {
|
|
40
|
+
return this.items[slot % this.capacity] ?? this.blank;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
get length(): number {
|
|
44
|
+
return this.count;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** Adds an item at the back; false when the queue is full and refuses it. */
|
|
48
|
+
push(item: T): boolean {
|
|
49
|
+
if (this.count === this.capacity) {
|
|
50
|
+
this.overflowed += 1;
|
|
51
|
+
if (this.options.onFull === "reject") {
|
|
52
|
+
return false;
|
|
53
|
+
}
|
|
54
|
+
this.first = (this.first + 1) % this.capacity;
|
|
55
|
+
this.count -= 1;
|
|
56
|
+
}
|
|
57
|
+
this.items[(this.first + this.count) % this.capacity] = item;
|
|
58
|
+
this.count += 1;
|
|
59
|
+
return true;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** Takes the item at the front, or undefined when empty. */
|
|
63
|
+
shift(): T | undefined {
|
|
64
|
+
if (this.count === 0) {
|
|
65
|
+
return undefined;
|
|
66
|
+
}
|
|
67
|
+
const item = this.item(this.first);
|
|
68
|
+
this.first = (this.first + 1) % this.capacity;
|
|
69
|
+
this.count -= 1;
|
|
70
|
+
return item;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** Takes the item at the back, or undefined when empty. */
|
|
74
|
+
pop(): T | undefined {
|
|
75
|
+
if (this.count === 0) {
|
|
76
|
+
return undefined;
|
|
77
|
+
}
|
|
78
|
+
this.count -= 1;
|
|
79
|
+
return this.item(this.first + this.count);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** The item at the front, left in place. */
|
|
83
|
+
peek(): T | undefined {
|
|
84
|
+
return this.count === 0 ? undefined : this.item(this.first);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** The item `offset` from the front, or undefined past the back. */
|
|
88
|
+
at(offset: UINT): T | undefined {
|
|
89
|
+
return offset < this.count ? this.item(this.first + offset) : undefined;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** Changes the item `offset` from the front in place; false past the back. */
|
|
93
|
+
update(offset: UINT, change: (item: T) => void): boolean {
|
|
94
|
+
if (offset >= this.count) {
|
|
95
|
+
return false;
|
|
96
|
+
}
|
|
97
|
+
const slot: UINT = (this.first + offset) % this.capacity;
|
|
98
|
+
// A copy, changed and stored back: an element at a runtime slot can't be lent out.
|
|
99
|
+
const item = structuredClone(
|
|
100
|
+
this.items[slot % this.capacity] ?? this.blank,
|
|
101
|
+
);
|
|
102
|
+
change(item);
|
|
103
|
+
this.items[slot % this.capacity] = item;
|
|
104
|
+
return true;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
clear(): void {
|
|
108
|
+
this.first = 0;
|
|
109
|
+
this.count = 0;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
*values(): Generator<T> {
|
|
113
|
+
for (let offset: UINT = 0; offset < this.capacity; offset++) {
|
|
114
|
+
if (offset >= this.count) {
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
// The stored item itself, so a loop that changes it changes the queue's.
|
|
118
|
+
yield this.items[(this.first + offset) % this.capacity] ?? this.blank;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** A list of at most `capacity` items in order, which can be removed from anywhere. */
|
|
124
|
+
export class BoundedList<T> {
|
|
125
|
+
private readonly items: T[];
|
|
126
|
+
private count: UINT = 0;
|
|
127
|
+
/** How many items the list refused or dropped because it was full. */
|
|
128
|
+
overflowed = 0;
|
|
129
|
+
|
|
130
|
+
/** What an empty slot holds; reads fall back to it where TypeScript can't see the slot is filled. */
|
|
131
|
+
private readonly blank: T;
|
|
132
|
+
|
|
133
|
+
constructor(
|
|
134
|
+
readonly capacity: number,
|
|
135
|
+
blank: () => T,
|
|
136
|
+
private readonly options: BoundedOptions,
|
|
137
|
+
) {
|
|
138
|
+
this.items = Array.from({ length: capacity }, blank);
|
|
139
|
+
this.blank = blank();
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
private item(slot: UINT): T {
|
|
143
|
+
return this.items[slot % this.capacity] ?? this.blank;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
get length(): number {
|
|
147
|
+
return this.count;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/** Adds an item at the end; false when the list is full and refuses it. */
|
|
151
|
+
push(item: T): boolean {
|
|
152
|
+
if (this.count === this.capacity) {
|
|
153
|
+
this.overflowed += 1;
|
|
154
|
+
if (this.options.onFull === "reject") {
|
|
155
|
+
return false;
|
|
156
|
+
}
|
|
157
|
+
this.removeAt(0);
|
|
158
|
+
}
|
|
159
|
+
this.items[this.count % this.capacity] = item;
|
|
160
|
+
this.count += 1;
|
|
161
|
+
return true;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
at(index: UINT): T | undefined {
|
|
165
|
+
return index < this.count ? this.item(index) : undefined;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/** Changes the item at `index` in place; false past the end. */
|
|
169
|
+
update(index: UINT, change: (item: T) => void): boolean {
|
|
170
|
+
if (index >= this.count) {
|
|
171
|
+
return false;
|
|
172
|
+
}
|
|
173
|
+
// A copy, changed and stored back: an element at a runtime slot can't be lent out.
|
|
174
|
+
const item = structuredClone(
|
|
175
|
+
this.items[index % this.capacity] ?? this.blank,
|
|
176
|
+
);
|
|
177
|
+
change(item);
|
|
178
|
+
this.items[index % this.capacity] = item;
|
|
179
|
+
return true;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/** Removes the item at `index`, moving the ones after it up; false past the end. */
|
|
183
|
+
removeAt(index: UINT): boolean {
|
|
184
|
+
if (index >= this.count) {
|
|
185
|
+
return false;
|
|
186
|
+
}
|
|
187
|
+
for (let slot: UINT = 0; slot < this.capacity; slot++) {
|
|
188
|
+
if (slot >= index && slot + 1 < this.count) {
|
|
189
|
+
this.items[slot % this.capacity] = this.item(slot + 1);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
this.count -= 1;
|
|
193
|
+
return true;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/** The index of the first item that passes, or -1. */
|
|
197
|
+
findIndex(test: (item: T) => boolean): number {
|
|
198
|
+
for (let slot: UINT = 0; slot < this.capacity; slot++) {
|
|
199
|
+
if (slot < this.count && test(this.item(slot))) {
|
|
200
|
+
return slot;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
return -1;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/** Removes the first item that passes; false when none does. */
|
|
207
|
+
remove(test: (item: T) => boolean): boolean {
|
|
208
|
+
for (let slot: UINT = 0; slot < this.capacity; slot++) {
|
|
209
|
+
if (slot < this.count && test(this.item(slot))) {
|
|
210
|
+
return this.removeAt(slot);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
return false;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
clear(): void {
|
|
217
|
+
this.count = 0;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
*values(): Generator<T> {
|
|
221
|
+
for (let slot: UINT = 0; slot < this.capacity; slot++) {
|
|
222
|
+
if (slot >= this.count) {
|
|
223
|
+
return;
|
|
224
|
+
}
|
|
225
|
+
yield this.items[slot % this.capacity] ?? this.blank;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* A map of at most `capacity` entries. Up to `linearLimit` entries, or with string keys, a lookup
|
|
232
|
+
* scans every slot; a larger map with number keys hashes them into twice as many slots, probing
|
|
233
|
+
* from each key's home slot and closing the gap a deletion leaves.
|
|
234
|
+
*/
|
|
235
|
+
export class BoundedMap<K extends number | string, V> {
|
|
236
|
+
private readonly hashed: boolean;
|
|
237
|
+
private readonly slots: number;
|
|
238
|
+
private readonly slotKeys: K[];
|
|
239
|
+
private readonly slotValues: V[];
|
|
240
|
+
private readonly used: boolean[];
|
|
241
|
+
/** When each slot's entry was added, for dropping the oldest. */
|
|
242
|
+
private readonly ages: number[];
|
|
243
|
+
private added = 0;
|
|
244
|
+
private count = 0;
|
|
245
|
+
/** What an empty slot holds; reads fall back to them where TypeScript can't see a slot is filled. */
|
|
246
|
+
private readonly blankKey: K;
|
|
247
|
+
private readonly blankValue: V;
|
|
248
|
+
/** How many entries the map refused or dropped because it was full. */
|
|
249
|
+
overflowed = 0;
|
|
250
|
+
|
|
251
|
+
constructor(
|
|
252
|
+
readonly capacity: number,
|
|
253
|
+
blankKey: K,
|
|
254
|
+
blank: () => V,
|
|
255
|
+
private readonly options: BoundedOptions,
|
|
256
|
+
) {
|
|
257
|
+
this.hashed = capacity > linearLimit && typeof blankKey === "number";
|
|
258
|
+
this.slots = this.hashed ? capacity * 2 : capacity;
|
|
259
|
+
this.slotKeys = Array.from({ length: this.slots }, () => blankKey);
|
|
260
|
+
this.slotValues = Array.from({ length: this.slots }, blank);
|
|
261
|
+
this.used = Array.from({ length: this.slots }, () => false);
|
|
262
|
+
this.ages = Array.from({ length: this.slots }, () => 0);
|
|
263
|
+
this.blankKey = blankKey;
|
|
264
|
+
this.blankValue = blank();
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
private keyAt(slot: UINT): K {
|
|
268
|
+
return this.slotKeys[slot % this.slots] ?? this.blankKey;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
private valueAt(slot: UINT): V {
|
|
272
|
+
return this.slotValues[slot % this.slots] ?? this.blankValue;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
private usedAt(slot: UINT): boolean {
|
|
276
|
+
return this.used[slot % this.slots] ?? false;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
private ageAt(slot: UINT): number {
|
|
280
|
+
return this.ages[slot % this.slots] ?? 0;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
get size(): number {
|
|
284
|
+
return this.count;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
has(key: K): boolean {
|
|
288
|
+
return this.slotOf(key) >= 0;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
/** The entry for `key`, as a copy, or undefined. Change a stored entry with `update`. */
|
|
292
|
+
get(key: K): V | undefined {
|
|
293
|
+
const found = this.slotOf(key);
|
|
294
|
+
if (found < 0) {
|
|
295
|
+
return undefined;
|
|
296
|
+
}
|
|
297
|
+
return this.valueAt(found);
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
/** Stores the entry for `key`; false when the map is full and refuses a new key. */
|
|
301
|
+
set(key: K, value: V): boolean {
|
|
302
|
+
const found = this.slotOf(key);
|
|
303
|
+
if (found >= 0) {
|
|
304
|
+
const slot: UINT = found;
|
|
305
|
+
this.slotValues[slot % this.slots] = value;
|
|
306
|
+
return true;
|
|
307
|
+
}
|
|
308
|
+
if (this.count === this.capacity) {
|
|
309
|
+
this.overflowed += 1;
|
|
310
|
+
if (this.options.onFull === "reject") {
|
|
311
|
+
return false;
|
|
312
|
+
}
|
|
313
|
+
this.removeSlot(this.oldestSlot());
|
|
314
|
+
}
|
|
315
|
+
const slot = this.freeSlot(key);
|
|
316
|
+
this.slotKeys[slot % this.slots] = key;
|
|
317
|
+
this.slotValues[slot % this.slots] = value;
|
|
318
|
+
this.used[slot % this.slots] = true;
|
|
319
|
+
this.ages[slot % this.slots] = this.added;
|
|
320
|
+
this.added += 1;
|
|
321
|
+
this.count += 1;
|
|
322
|
+
return true;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/** Changes the stored entry for `key` in place; false when there is none. */
|
|
326
|
+
update(key: K, change: (entry: V) => void): boolean {
|
|
327
|
+
const found = this.slotOf(key);
|
|
328
|
+
if (found < 0) {
|
|
329
|
+
return false;
|
|
330
|
+
}
|
|
331
|
+
const slot: UINT = found;
|
|
332
|
+
// A copy, changed and stored back: an element at a runtime slot can't be lent out.
|
|
333
|
+
const entry = structuredClone(
|
|
334
|
+
this.slotValues[slot % this.slots] ?? this.blankValue,
|
|
335
|
+
);
|
|
336
|
+
change(entry);
|
|
337
|
+
this.slotValues[slot % this.slots] = entry;
|
|
338
|
+
return true;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
delete(key: K): boolean {
|
|
342
|
+
const found = this.slotOf(key);
|
|
343
|
+
if (found < 0) {
|
|
344
|
+
return false;
|
|
345
|
+
}
|
|
346
|
+
this.removeSlot(found);
|
|
347
|
+
return true;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
clear(): void {
|
|
351
|
+
for (let slot: UINT = 0; slot < this.slots; slot++) {
|
|
352
|
+
this.used[slot % this.slots] = false;
|
|
353
|
+
}
|
|
354
|
+
this.count = 0;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
*keys(): Generator<K> {
|
|
358
|
+
for (let slot: UINT = 0; slot < this.slots; slot++) {
|
|
359
|
+
if (this.usedAt(slot)) {
|
|
360
|
+
yield this.keyAt(slot);
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
*values(): Generator<V> {
|
|
366
|
+
for (let slot: UINT = 0; slot < this.slots; slot++) {
|
|
367
|
+
if (this.usedAt(slot)) {
|
|
368
|
+
yield this.slotValues[slot % this.slots] ?? this.blankValue;
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
/** The slot holding `key`, or -1. */
|
|
374
|
+
private slotOf(key: K): number {
|
|
375
|
+
if (!this.hashed) {
|
|
376
|
+
for (let slot: UINT = 0; slot < this.slots; slot++) {
|
|
377
|
+
if (this.usedAt(slot) && this.keyAt(slot) === key) {
|
|
378
|
+
return slot;
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
return -1;
|
|
382
|
+
}
|
|
383
|
+
const home: UINT = this.home(key);
|
|
384
|
+
for (let probe: UINT = 0; probe < this.slots; probe++) {
|
|
385
|
+
const slot: UINT = (home + probe) % this.slots;
|
|
386
|
+
if (!this.usedAt(slot)) {
|
|
387
|
+
return -1;
|
|
388
|
+
}
|
|
389
|
+
if (this.keyAt(slot) === key) {
|
|
390
|
+
return slot;
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
return -1;
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
/** The slot a new key goes in: the first free one, from its home slot when hashed. */
|
|
397
|
+
private freeSlot(key: K): UINT {
|
|
398
|
+
const home: UINT = this.hashed ? this.home(key) : 0;
|
|
399
|
+
for (let probe: UINT = 0; probe < this.slots; probe++) {
|
|
400
|
+
const slot: UINT = (home + probe) % this.slots;
|
|
401
|
+
if (!this.usedAt(slot)) {
|
|
402
|
+
return slot;
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
return 0;
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
/** The slot a number key hashes to. */
|
|
409
|
+
private home(key: K): number {
|
|
410
|
+
if (typeof key !== "number") {
|
|
411
|
+
return 0;
|
|
412
|
+
}
|
|
413
|
+
const remainder = key % this.slots;
|
|
414
|
+
return remainder < 0 ? remainder + this.slots : remainder;
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
/** The slot whose entry was added first. */
|
|
418
|
+
private oldestSlot(): number {
|
|
419
|
+
let oldest = -1;
|
|
420
|
+
for (let slot: UINT = 0; slot < this.slots; slot++) {
|
|
421
|
+
if (
|
|
422
|
+
this.usedAt(slot) &&
|
|
423
|
+
(oldest < 0 || this.ageAt(slot) - this.ageAt(oldest) < 0)
|
|
424
|
+
) {
|
|
425
|
+
oldest = slot;
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
return oldest;
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
/**
|
|
432
|
+
* Empties a slot. A hashed map then moves back each entry after it that could sit earlier, so no
|
|
433
|
+
* lookup stops at the gap before reaching its key.
|
|
434
|
+
*/
|
|
435
|
+
private removeSlot(found: number): void {
|
|
436
|
+
if (found < 0) {
|
|
437
|
+
return;
|
|
438
|
+
}
|
|
439
|
+
const removed: UINT = found;
|
|
440
|
+
this.used[removed % this.slots] = false;
|
|
441
|
+
this.count -= 1;
|
|
442
|
+
if (!this.hashed) {
|
|
443
|
+
return;
|
|
444
|
+
}
|
|
445
|
+
let hole: UINT = removed % this.slots;
|
|
446
|
+
for (let probe: UINT = 1; probe < this.slots; probe++) {
|
|
447
|
+
const next: UINT = (removed + probe) % this.slots;
|
|
448
|
+
if (!this.usedAt(next)) {
|
|
449
|
+
return;
|
|
450
|
+
}
|
|
451
|
+
const home: UINT = this.home(this.keyAt(next));
|
|
452
|
+
const stays =
|
|
453
|
+
hole <= next
|
|
454
|
+
? home > hole && home <= next
|
|
455
|
+
: home > hole || home <= next;
|
|
456
|
+
if (!stays) {
|
|
457
|
+
this.slotKeys[hole % this.slots] = this.keyAt(next);
|
|
458
|
+
this.slotValues[hole % this.slots] = this.valueAt(next);
|
|
459
|
+
this.ages[hole % this.slots] = this.ageAt(next);
|
|
460
|
+
this.used[hole % this.slots] = true;
|
|
461
|
+
this.used[next % this.slots] = false;
|
|
462
|
+
hole = next;
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
/** A set of at most `capacity` keys. */
|
|
469
|
+
export class BoundedSet<K extends number | string> {
|
|
470
|
+
private readonly members: BoundedMap<K, boolean>;
|
|
471
|
+
|
|
472
|
+
constructor(
|
|
473
|
+
readonly capacity: number,
|
|
474
|
+
blankKey: K,
|
|
475
|
+
options: BoundedOptions,
|
|
476
|
+
) {
|
|
477
|
+
this.members = new BoundedMap<K, boolean>(
|
|
478
|
+
capacity,
|
|
479
|
+
blankKey,
|
|
480
|
+
() => false,
|
|
481
|
+
options,
|
|
482
|
+
);
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
get size(): number {
|
|
486
|
+
return this.members.size;
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
/** How many keys the set refused or dropped because it was full. */
|
|
490
|
+
get overflowed(): number {
|
|
491
|
+
return this.members.overflowed;
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
has(key: K): boolean {
|
|
495
|
+
return this.members.has(key);
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
/** Adds the key; false when the set is full and refuses it. */
|
|
499
|
+
add(key: K): boolean {
|
|
500
|
+
return this.members.set(key, true);
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
delete(key: K): boolean {
|
|
504
|
+
return this.members.delete(key);
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
clear(): void {
|
|
508
|
+
this.members.clear();
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
*values(): Generator<K> {
|
|
512
|
+
yield* this.members.keys();
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
/**
|
|
517
|
+
* A record holding what `create` makes for each member of a numeric enum, in the enum's order: the
|
|
518
|
+
* PLC's array indexed by that enum. For setup, where a literal would list every member by hand.
|
|
519
|
+
*/
|
|
520
|
+
export function enumRecord<Key extends number, T>(
|
|
521
|
+
members: Readonly<Record<string, Key | string>>,
|
|
522
|
+
create: (member: Key) => T,
|
|
523
|
+
): Record<Key, T>;
|
|
524
|
+
export function enumRecord(
|
|
525
|
+
members: Readonly<Record<string, number | string>>,
|
|
526
|
+
create: (member: number) => unknown,
|
|
527
|
+
): Record<number, unknown> {
|
|
528
|
+
return Object.fromEntries(
|
|
529
|
+
Object.values(members)
|
|
530
|
+
// A numeric enum also maps each value back to its name.
|
|
531
|
+
.filter((member) => typeof member === "number")
|
|
532
|
+
.map((member) => [member, create(member)]),
|
|
533
|
+
);
|
|
534
|
+
}
|