@tsplc/compiler 0.5.13 → 0.5.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +33 -8
- package/dist/cli.js +794 -621
- package/dist/cli.js.map +19 -16
- package/dist/clock.js +5 -2
- package/dist/clock.js.map +5 -4
- package/dist/codesys.js +4162 -4157
- package/dist/codesys.js.map +43 -29
- package/dist/index.js +7852 -6549
- package/dist/index.js.map +53 -50
- package/dist/ir.js +2482 -2277
- package/dist/ir.js.map +28 -25
- package/dist/retain.js +3 -1
- package/dist/retain.js.map +3 -3
- package/dist/signals.js +115 -0
- package/dist/signals.js.map +10 -0
- package/dist/telemetry.js +25 -20
- package/dist/telemetry.js.map +6 -6
- package/package.json +19 -39
- package/src/codesys/config.ts +3 -3
- package/src/codesys/debug.ts +92 -108
- package/src/codesys/emit.ts +100 -88
- package/src/codesys/endpoints.ts +111 -0
- package/src/codesys/engineering.ts +30 -54
- 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 +85 -1401
- package/src/codesys/expressions.ts +41 -33
- package/src/codesys/global-lists.ts +114 -94
- package/src/codesys/global-names.ts +12 -14
- package/src/codesys/http.ts +428 -388
- package/src/codesys/instrumentation.ts +142 -148
- package/src/codesys/io-paths.ts +31 -23
- package/src/codesys/loops.ts +101 -0
- package/src/codesys/manifest.ts +269 -269
- package/src/codesys/names.ts +10 -33
- package/src/codesys/numeric.ts +89 -42
- package/src/codesys/plan.ts +202 -190
- package/src/codesys/programs.ts +23 -16
- package/src/codesys/publication.ts +13 -86
- package/src/codesys/required.ts +8 -0
- package/src/codesys/routines.ts +103 -489
- package/src/codesys/scheduling.ts +96 -0
- package/src/codesys/service-channels.ts +23 -0
- package/src/codesys/sources.ts +34 -28
- package/src/codesys/st-files.ts +112 -105
- package/src/codesys/statements.ts +409 -0
- package/src/codesys/storage.ts +41 -23
- package/src/codesys/symbols.ts +130 -119
- package/src/codesys/task-access.ts +65 -221
- package/src/codesys/telemetry-settings.ts +19 -0
- package/src/codesys/telemetry.ts +185 -148
- package/src/codesys/types.ts +69 -109
- package/src/codesys/vendor.ts +13 -50
- package/src/frontend/analyzer.ts +66 -111
- package/src/frontend/async.ts +316 -128
- package/src/frontend/blocks.ts +178 -154
- package/src/frontend/configuration.ts +49 -153
- package/src/frontend/const-functions.ts +50 -40
- package/src/frontend/control-flow.ts +164 -129
- package/src/frontend/cooperative.ts +17 -9
- package/src/frontend/coroutines.ts +539 -441
- package/src/frontend/dialect-names.ts +104 -0
- package/src/frontend/elaborate/array-searches.ts +77 -69
- package/src/frontend/elaborate/assignments.ts +68 -77
- package/src/frontend/elaborate/calls.ts +878 -512
- package/src/frontend/elaborate/capture-protocol.ts +118 -21
- package/src/frontend/elaborate/capture.ts +196 -109
- package/src/frontend/elaborate/declarations.ts +124 -0
- package/src/frontend/elaborate/dialect.ts +119 -46
- package/src/frontend/elaborate/expressions.ts +763 -442
- package/src/frontend/elaborate/filtered-arrays.ts +127 -91
- package/src/frontend/elaborate/frame.ts +50 -2
- package/src/frontend/elaborate/generics.ts +20 -34
- package/src/frontend/elaborate/graph.ts +13 -87
- package/src/frontend/elaborate/imports.ts +0 -5
- package/src/frontend/elaborate/index.ts +416 -169
- package/src/frontend/elaborate/initializers.ts +64 -42
- package/src/frontend/elaborate/iterators.ts +228 -364
- package/src/frontend/elaborate/layouts.ts +477 -116
- package/src/frontend/elaborate/model.ts +113 -55
- package/src/frontend/elaborate/references.ts +116 -138
- package/src/frontend/elaborate/retention.ts +137 -59
- package/src/frontend/elaborate/specialize.ts +617 -308
- package/src/frontend/elaborate/statements.ts +436 -247
- package/src/frontend/elaborate/storage.ts +280 -213
- package/src/frontend/elaborate/time-constructors.ts +34 -0
- package/src/frontend/elaborate/values.ts +77 -7
- package/src/frontend/elaborate/writes.ts +98 -84
- package/src/frontend/expressions.ts +149 -151
- package/src/frontend/generated-names.ts +3 -3
- package/src/frontend/intrinsics.ts +31 -17
- package/src/frontend/lowering-key.ts +23 -0
- package/src/frontend/module-graph.ts +105 -43
- package/src/frontend/positions.ts +43 -35
- package/src/frontend/promise-chains.ts +226 -266
- package/src/frontend/promises.ts +88 -67
- package/src/frontend/routines.ts +122 -65
- package/src/frontend/sdk.ts +104 -5
- package/src/frontend/services.ts +149 -102
- package/src/frontend/statements.ts +60 -64
- package/src/frontend/tasks.ts +151 -96
- package/src/frontend/telemetry.ts +1 -1
- package/src/frontend/types.ts +366 -181
- package/src/frontend/typescript.ts +239 -112
- package/src/index.ts +2 -1
- package/src/ir/aggregates.ts +90 -75
- package/src/ir/allocation.ts +29 -18
- package/src/ir/conditions.ts +38 -78
- package/src/ir/core-model.ts +128 -11
- package/src/ir/declarations.ts +18 -32
- package/src/ir/diagnostics.ts +25 -18
- package/src/ir/domains.ts +26 -23
- package/src/ir/fold.ts +55 -50
- package/src/ir/identity.ts +10 -0
- package/src/ir/index.ts +31 -7
- package/src/ir/job-errors.ts +30 -8
- package/src/ir/layouts.ts +63 -34
- package/src/ir/link.ts +11 -46
- package/src/ir/model.ts +25 -6
- package/src/ir/normalize.ts +3 -28
- package/src/ir/numeric.ts +45 -18
- package/src/ir/operands.ts +102 -0
- package/src/ir/ranges.ts +242 -67
- package/src/ir/routine.ts +126 -183
- package/src/ir/scalars.ts +29 -25
- package/src/ir/share.ts +323 -343
- package/src/ir/short-circuit.ts +140 -0
- package/src/ir/tables.ts +111 -0
- package/src/ir/temporaries.ts +15 -113
- package/src/ir/types.ts +32 -8
- package/src/project/artifacts.ts +143 -71
- package/src/project/cli.ts +104 -87
- package/src/project/debugger.ts +48 -34
- package/src/project/deployments.ts +12 -68
- package/src/project/flags.ts +14 -0
- package/src/project/plant.ts +293 -225
- package/src/project/project-commands.ts +254 -192
- package/src/project/project.ts +184 -141
- package/src/project/release.ts +22 -28
- package/src/project/server.ts +22 -26
- package/src/project/terminal.ts +13 -2
- package/src/runtime/async.ts +17 -13
- package/src/runtime/launch.ts +5 -2
- package/src/runtime/main.ts +8 -3
- package/src/runtime/missing-file.ts +3 -0
- package/src/runtime/process.ts +24 -19
- package/src/runtime/program.ts +20 -53
- package/src/runtime/retain.ts +18 -10
- package/src/runtime/runtime.ts +47 -60
- package/src/runtime/settings.ts +27 -0
- package/src/runtime/storage.ts +49 -74
- package/src/runtime/symbols.ts +19 -75
- package/src/runtime/telemetry.ts +37 -14
- package/src/runtime/transport.ts +26 -30
- package/stdlib/clock.ts +4 -2
- package/stdlib/plc-time.ts +2 -0
- package/stdlib/retain.ts +2 -0
- package/stdlib/signals.ts +142 -0
- package/stdlib/telemetry.ts +5 -12
- package/types/dialect.d.ts +49 -0
- package/types/globals.d.ts +2 -42
- package/types/index.d.ts +3 -1
- package/types/retain.d.ts +8 -2
- package/types/telemetry.d.ts +3 -0
- package/dist/bun-runtime.js +0 -1472
- package/dist/bun-runtime.js.map +0 -26
- package/dist/elaborate-capture.js +0 -304
- package/dist/elaborate-capture.js.map +0 -11
- package/dist/math.js +0 -24
- package/dist/math.js.map +0 -10
- package/dist/project.js +0 -626
- package/dist/project.js.map +0 -12
- package/dist/server-check.js +0 -59
- package/dist/server-check.js.map +0 -11
- package/src/frontend/exposure.ts +0 -181
- package/src/project/index.ts +0 -6
- package/src/runtime/.runtime-65O4JH/server/router.ts +0 -17
- package/src/runtime/.runtime-65O4JH/src/cell.ts +0 -45
- package/src/runtime/.runtime-65O4JH/src/controller.ts +0 -58
- package/src/runtime/.runtime-65O4JH/src/lane.ts +0 -6
- package/src/runtime/.runtime-65O4JH/src/main.ts +0 -11
- package/src/runtime/.runtime-65O4JH/src/services.ts +0 -8
- package/src/runtime/.runtime-65O4JH/tsconfig.json +0 -13
- package/src/runtime/.runtime-65O4JH/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-Brw2kO/server/router.ts +0 -17
- package/src/runtime/.runtime-Brw2kO/src/cell.ts +0 -45
- package/src/runtime/.runtime-Brw2kO/src/controller.ts +0 -58
- package/src/runtime/.runtime-Brw2kO/src/lane.ts +0 -6
- package/src/runtime/.runtime-Brw2kO/src/main.ts +0 -11
- package/src/runtime/.runtime-Brw2kO/src/services.ts +0 -8
- package/src/runtime/.runtime-Brw2kO/tsconfig.json +0 -13
- package/src/runtime/.runtime-Brw2kO/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-NHfu1D/server/router.ts +0 -17
- package/src/runtime/.runtime-NHfu1D/src/cell.ts +0 -45
- package/src/runtime/.runtime-NHfu1D/src/controller.ts +0 -58
- package/src/runtime/.runtime-NHfu1D/src/lane.ts +0 -6
- package/src/runtime/.runtime-NHfu1D/src/main.ts +0 -11
- package/src/runtime/.runtime-NHfu1D/src/services.ts +0 -8
- package/src/runtime/.runtime-NHfu1D/tsconfig.json +0 -13
- package/src/runtime/.runtime-NHfu1D/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-VaUSOu/server/router.ts +0 -17
- package/src/runtime/.runtime-VaUSOu/src/cell.ts +0 -45
- package/src/runtime/.runtime-VaUSOu/src/controller.ts +0 -58
- package/src/runtime/.runtime-VaUSOu/src/lane.ts +0 -6
- package/src/runtime/.runtime-VaUSOu/src/main.ts +0 -11
- package/src/runtime/.runtime-VaUSOu/src/services.ts +0 -8
- package/src/runtime/.runtime-VaUSOu/tsconfig.json +0 -13
- package/src/runtime/.runtime-VaUSOu/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-WbtNvR/server/router.ts +0 -17
- package/src/runtime/.runtime-WbtNvR/src/cell.ts +0 -45
- package/src/runtime/.runtime-WbtNvR/src/controller.ts +0 -58
- package/src/runtime/.runtime-WbtNvR/src/lane.ts +0 -6
- package/src/runtime/.runtime-WbtNvR/src/main.ts +0 -11
- package/src/runtime/.runtime-WbtNvR/src/services.ts +0 -8
- package/src/runtime/.runtime-WbtNvR/tsconfig.json +0 -13
- package/src/runtime/.runtime-WbtNvR/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-opcua-GMPUt8/server/router.ts +0 -17
- package/src/runtime/.runtime-opcua-GMPUt8/src/cell.ts +0 -45
- package/src/runtime/.runtime-opcua-GMPUt8/src/controller.ts +0 -58
- package/src/runtime/.runtime-opcua-GMPUt8/src/lane.ts +0 -6
- package/src/runtime/.runtime-opcua-GMPUt8/src/main.ts +0 -11
- package/src/runtime/.runtime-opcua-GMPUt8/src/services.ts +0 -8
- package/src/runtime/.runtime-opcua-GMPUt8/tsconfig.json +0 -13
- package/src/runtime/.runtime-opcua-GMPUt8/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-opcua-UwafxO/server/router.ts +0 -17
- package/src/runtime/.runtime-opcua-UwafxO/src/cell.ts +0 -45
- package/src/runtime/.runtime-opcua-UwafxO/src/controller.ts +0 -58
- package/src/runtime/.runtime-opcua-UwafxO/src/lane.ts +0 -6
- package/src/runtime/.runtime-opcua-UwafxO/src/main.ts +0 -11
- package/src/runtime/.runtime-opcua-UwafxO/src/services.ts +0 -8
- package/src/runtime/.runtime-opcua-UwafxO/tsconfig.json +0 -13
- package/src/runtime/.runtime-opcua-UwafxO/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-opcua-ecaSee/server/router.ts +0 -17
- package/src/runtime/.runtime-opcua-ecaSee/src/cell.ts +0 -45
- package/src/runtime/.runtime-opcua-ecaSee/src/controller.ts +0 -58
- package/src/runtime/.runtime-opcua-ecaSee/src/lane.ts +0 -6
- package/src/runtime/.runtime-opcua-ecaSee/src/main.ts +0 -11
- package/src/runtime/.runtime-opcua-ecaSee/src/services.ts +0 -8
- package/src/runtime/.runtime-opcua-ecaSee/tsconfig.json +0 -13
- package/src/runtime/.runtime-opcua-ecaSee/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-opcua-y91UCm/server/router.ts +0 -17
- package/src/runtime/.runtime-opcua-y91UCm/src/cell.ts +0 -45
- package/src/runtime/.runtime-opcua-y91UCm/src/controller.ts +0 -58
- package/src/runtime/.runtime-opcua-y91UCm/src/lane.ts +0 -6
- package/src/runtime/.runtime-opcua-y91UCm/src/main.ts +0 -11
- package/src/runtime/.runtime-opcua-y91UCm/src/services.ts +0 -8
- package/src/runtime/.runtime-opcua-y91UCm/tsconfig.json +0 -13
- package/src/runtime/.runtime-opcua-y91UCm/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-ybIxYh/server/router.ts +0 -17
- package/src/runtime/.runtime-ybIxYh/src/cell.ts +0 -45
- package/src/runtime/.runtime-ybIxYh/src/controller.ts +0 -58
- package/src/runtime/.runtime-ybIxYh/src/lane.ts +0 -6
- package/src/runtime/.runtime-ybIxYh/src/main.ts +0 -11
- package/src/runtime/.runtime-ybIxYh/src/services.ts +0 -8
- package/src/runtime/.runtime-ybIxYh/tsconfig.json +0 -13
- package/src/runtime/.runtime-ybIxYh/tsplc.config.ts +0 -31
- package/stdlib/math.ts +0 -23
- package/types/plc.d.ts +0 -4
package/src/runtime/telemetry.ts
CHANGED
|
@@ -1,22 +1,21 @@
|
|
|
1
1
|
import { httpErrors } from "@tsplc/compiler/ir";
|
|
2
|
+
import type { TelemetryBatch } from "@tsplc/server/telemetry";
|
|
2
3
|
import { plcBufferBytes } from "@tsplc/server/wire";
|
|
4
|
+
import { telemetrySendIntervalMs } from "../codesys/telemetry-settings";
|
|
3
5
|
|
|
4
6
|
/** A record as the program's telemetry calls make it; see `telemetryRecord` in `@tsplc/server`. */
|
|
5
7
|
export type TelemetryRecord = Readonly<Record<string, unknown>>;
|
|
6
8
|
|
|
7
|
-
/** How long a sender waits for a full batch before it sends what it has, and before it retries. */
|
|
8
|
-
const batchWaitMs = 200;
|
|
9
|
-
|
|
10
9
|
interface Queued {
|
|
11
10
|
sequence: number;
|
|
12
11
|
text: string;
|
|
13
12
|
}
|
|
14
13
|
|
|
15
14
|
/**
|
|
16
|
-
* Each task's records and the sender that
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
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.
|
|
20
19
|
*/
|
|
21
20
|
export const createTelemetry = (options: {
|
|
22
21
|
/** The build id, as the debug manifest names it. */
|
|
@@ -25,7 +24,10 @@ export const createTelemetry = (options: {
|
|
|
25
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. */
|
|
26
25
|
post: ((body: string) => Promise<unknown>) | null;
|
|
27
26
|
clock: () => number;
|
|
27
|
+
/** Says once that records are dropped for want of a server. */
|
|
28
|
+
log: (line: string) => void;
|
|
28
29
|
}) => {
|
|
30
|
+
const unsent = { reported: false };
|
|
29
31
|
const tasks = new Map<
|
|
30
32
|
string,
|
|
31
33
|
{
|
|
@@ -70,8 +72,19 @@ export const createTelemetry = (options: {
|
|
|
70
72
|
/** The records from the oldest unsent on that fit one batch beside its other fields. */
|
|
71
73
|
const batchOf = (task: string, scan: number) => {
|
|
72
74
|
const state = stateOf(task);
|
|
73
|
-
const
|
|
74
|
-
|
|
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
|
+
};
|
|
75
88
|
const first = state.queued[0]?.sequence ?? state.next;
|
|
76
89
|
const texts: string[] = [];
|
|
77
90
|
for (const queued of state.queued) {
|
|
@@ -88,15 +101,25 @@ export const createTelemetry = (options: {
|
|
|
88
101
|
const send = (task: string, scan: number) => {
|
|
89
102
|
const state = stateOf(task);
|
|
90
103
|
const { post } = options;
|
|
91
|
-
if (!post
|
|
92
|
-
if (!
|
|
93
|
-
|
|
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
|
+
);
|
|
94
110
|
}
|
|
111
|
+
state.queued = [];
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
if (state.sending || !state.queued.length) {
|
|
95
115
|
return;
|
|
96
116
|
}
|
|
97
117
|
const batch = batchOf(task, scan);
|
|
98
118
|
const full = batch.count < state.queued.length;
|
|
99
|
-
if (
|
|
119
|
+
if (
|
|
120
|
+
!full &&
|
|
121
|
+
options.clock() - state.waitingSince < telemetrySendIntervalMs
|
|
122
|
+
) {
|
|
100
123
|
return;
|
|
101
124
|
}
|
|
102
125
|
if (!batch.count) {
|
|
@@ -115,7 +138,7 @@ export const createTelemetry = (options: {
|
|
|
115
138
|
.then(settle)
|
|
116
139
|
.catch((reason: unknown) => {
|
|
117
140
|
// The server can't take these records, so they are dropped rather than sent forever.
|
|
118
|
-
if (reason === httpErrors.
|
|
141
|
+
if (reason === httpErrors.refused) {
|
|
119
142
|
state.dropped += sent.length;
|
|
120
143
|
settle();
|
|
121
144
|
return;
|
package/src/runtime/transport.ts
CHANGED
|
@@ -2,9 +2,17 @@ import { readFile } from "node:fs/promises";
|
|
|
2
2
|
import { join } from "node:path";
|
|
3
3
|
import { httpErrors, jobError } from "@tsplc/compiler/ir";
|
|
4
4
|
import { emittedFiles } from "@tsplc/config/project-files";
|
|
5
|
+
import { elementaryType, integerRange } from "@tsplc/iec";
|
|
5
6
|
import type { PublishedScalar } from "@tsplc/opcua/server";
|
|
6
|
-
import {
|
|
7
|
-
|
|
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";
|
|
8
16
|
import { type HttpSettings, httpCall } from "./async";
|
|
9
17
|
|
|
10
18
|
/**
|
|
@@ -25,44 +33,32 @@ export const httpTransport = (
|
|
|
25
33
|
httpCall(settings, `${baseUrl}${route}`, { method: "POST", body }),
|
|
26
34
|
});
|
|
27
35
|
|
|
28
|
-
/** One slot of a route, as the build lists it in `machines.ts`: the global symbols it is exchanged through. */
|
|
29
|
-
const serviceChannel = z.strictObject({
|
|
30
|
-
path: z.string(),
|
|
31
|
-
request: z.string(),
|
|
32
|
-
requestId: z.string(),
|
|
33
|
-
reply: z.string(),
|
|
34
|
-
replyId: z.string(),
|
|
35
|
-
epoch: z.string(),
|
|
36
|
-
});
|
|
37
|
-
export type ServiceChannel = z.infer<typeof serviceChannel>;
|
|
38
|
-
|
|
39
36
|
/** The slots the build compiled, from the emitted `machines.ts`. */
|
|
40
37
|
export const readServiceChannels = async (
|
|
41
38
|
st: string,
|
|
42
|
-
): Promise<ServiceChannel[]> =>
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
throw new Error(`${emittedFiles.machines} lists no service channels.`);
|
|
47
|
-
}
|
|
48
|
-
return z.array(serviceChannel).parse(JSON.parse(listed[1]));
|
|
49
|
-
};
|
|
39
|
+
): Promise<ServiceChannel[]> =>
|
|
40
|
+
parseServiceChannels(await readFile(join(st, emittedFiles.machines), "utf8"));
|
|
41
|
+
|
|
42
|
+
const [, udintMaximum] = integerRange(elementaryType("UDINT"));
|
|
50
43
|
|
|
51
44
|
/** A slot's sequence is a UDINT; wrapping to 0 retires its epoch, as the compiled block does. */
|
|
52
|
-
const sequenceLimit =
|
|
45
|
+
const sequenceLimit = Number(udintMaximum) + 1;
|
|
53
46
|
|
|
54
|
-
/** How often a waiting call looks for its reply
|
|
55
|
-
const replyPollMs =
|
|
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;
|
|
56
49
|
|
|
57
|
-
/**
|
|
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
|
+
*/
|
|
58
54
|
const decodeReply = (rest: string): unknown => {
|
|
59
55
|
if (Buffer.byteLength(rest, "utf8") < 4) {
|
|
60
56
|
throw jobError.malformedResponse;
|
|
61
57
|
}
|
|
62
|
-
if (rest.startsWith(
|
|
63
|
-
throw httpErrors.
|
|
58
|
+
if (rest.startsWith(`${slotStatus.refused}|`)) {
|
|
59
|
+
throw httpErrors.refused;
|
|
64
60
|
}
|
|
65
|
-
if (!rest.startsWith(
|
|
61
|
+
if (!rest.startsWith(`${slotStatus.ok}|`)) {
|
|
66
62
|
throw jobError.network;
|
|
67
63
|
}
|
|
68
64
|
try {
|
|
@@ -140,8 +136,8 @@ export const opcuaSlots = (
|
|
|
140
136
|
throw jobError.network;
|
|
141
137
|
}
|
|
142
138
|
const epoch = read(channel.epoch);
|
|
143
|
-
const identity =
|
|
144
|
-
const request =
|
|
139
|
+
const identity = slotIdentity(epoch, slot.sequence);
|
|
140
|
+
const request = encodeSlotRequest(identity, body);
|
|
145
141
|
if (Buffer.byteLength(request, "utf8") > plcBufferBytes) {
|
|
146
142
|
throw jobError.tooLarge;
|
|
147
143
|
}
|
package/stdlib/clock.ts
CHANGED
|
@@ -1,2 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import { plcTime } from "./plc-time";
|
|
2
|
+
|
|
3
|
+
/** Milliseconds since the process started, as the PLC's `now()` counts them in a DINT; used when a program runs under Bun. */
|
|
4
|
+
export const now = (): number => plcTime() | 0;
|
package/stdlib/retain.ts
CHANGED
|
@@ -1,2 +1,4 @@
|
|
|
1
1
|
/** `retain` as a program running under Bun sees it: retention is the PLC's, so the value passes through. */
|
|
2
2
|
export const retain = <T>(value: T): T => value;
|
|
3
|
+
/** `persist` as a program running under Bun sees it: as `retain`, the value passes through. */
|
|
4
|
+
export const persist = <T>(value: T): T => value;
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
// PLC implementation source: timers and edge detectors for a plain TypeScript program. Each is an
|
|
2
|
+
// ordinary class the compiler specializes like a program's own, and the same classes run under Bun
|
|
3
|
+
// for tests and simulation. Every update takes the scan's `now()`, so one scan reads one time.
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Milliseconds since `last`, the previous update's `now()`. The PLC clock is a DINT that wraps
|
|
7
|
+
* after 24.8 days: on the PLC the subtraction wraps with it, and under Bun the one negative step
|
|
8
|
+
* across the wrap counts as none, so a timer measures scan by scan instead of from a start time.
|
|
9
|
+
*/
|
|
10
|
+
const elapsedSince = (last: number | undefined, now: number): number =>
|
|
11
|
+
last === undefined ? 0 : Math.max(now - last, 0);
|
|
12
|
+
|
|
13
|
+
/** Turns its output on once the input has been on for `durationMs`, and off as soon as the input goes off. IEC `TON`. */
|
|
14
|
+
export class OnDelayTimer {
|
|
15
|
+
private heldMs = 0;
|
|
16
|
+
private previous = false;
|
|
17
|
+
private lastNow: number | undefined;
|
|
18
|
+
|
|
19
|
+
constructor(readonly durationMs: number) {}
|
|
20
|
+
|
|
21
|
+
/** How long the input has been on, up to `durationMs`. */
|
|
22
|
+
get elapsedMs(): number {
|
|
23
|
+
return this.heldMs;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
get output(): boolean {
|
|
27
|
+
return this.previous && this.heldMs >= this.durationMs;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Takes this scan's input and `now()`, and returns the output. */
|
|
31
|
+
update(now: number, input: boolean): boolean {
|
|
32
|
+
const step = elapsedSince(this.lastNow, now);
|
|
33
|
+
this.lastNow = now;
|
|
34
|
+
this.heldMs =
|
|
35
|
+
input && this.previous
|
|
36
|
+
? Math.min(this.heldMs + step, this.durationMs)
|
|
37
|
+
: 0;
|
|
38
|
+
this.previous = input;
|
|
39
|
+
return this.output;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** Turns its output on with the input, and off once the input has been off for `durationMs`. IEC `TOF`. */
|
|
44
|
+
export class OffDelayTimer {
|
|
45
|
+
private offMs = 0;
|
|
46
|
+
private previous = false;
|
|
47
|
+
private on = false;
|
|
48
|
+
private lastNow: number | undefined;
|
|
49
|
+
|
|
50
|
+
constructor(readonly durationMs: number) {}
|
|
51
|
+
|
|
52
|
+
/** How long the input has been off since the output last turned on, up to `durationMs`. */
|
|
53
|
+
get elapsedMs(): number {
|
|
54
|
+
return this.offMs;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
get output(): boolean {
|
|
58
|
+
return this.on;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** Takes this scan's input and `now()`, and returns the output. */
|
|
62
|
+
update(now: number, input: boolean): boolean {
|
|
63
|
+
const step = elapsedSince(this.lastNow, now);
|
|
64
|
+
this.lastNow = now;
|
|
65
|
+
if (input) {
|
|
66
|
+
this.on = true;
|
|
67
|
+
this.offMs = 0;
|
|
68
|
+
} else if (this.on) {
|
|
69
|
+
this.offMs = this.previous
|
|
70
|
+
? 0
|
|
71
|
+
: Math.min(this.offMs + step, this.durationMs);
|
|
72
|
+
this.on = this.offMs < this.durationMs;
|
|
73
|
+
}
|
|
74
|
+
this.previous = input;
|
|
75
|
+
return this.on;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Turns its output on for `durationMs` when the input rises, whatever the input does meanwhile. A
|
|
81
|
+
* new pulse needs the input to rise again after the last one ends. IEC `TP`.
|
|
82
|
+
*/
|
|
83
|
+
export class PulseTimer {
|
|
84
|
+
private onMs = 0;
|
|
85
|
+
private previous = false;
|
|
86
|
+
private on = false;
|
|
87
|
+
private lastNow: number | undefined;
|
|
88
|
+
|
|
89
|
+
constructor(readonly durationMs: number) {}
|
|
90
|
+
|
|
91
|
+
/** How long the current pulse has lasted, up to `durationMs`. */
|
|
92
|
+
get elapsedMs(): number {
|
|
93
|
+
return this.onMs;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
get output(): boolean {
|
|
97
|
+
return this.on;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** Takes this scan's input and `now()`, and returns the output. */
|
|
101
|
+
update(now: number, input: boolean): boolean {
|
|
102
|
+
const step = elapsedSince(this.lastNow, now);
|
|
103
|
+
this.lastNow = now;
|
|
104
|
+
if (this.on) {
|
|
105
|
+
this.onMs = Math.min(this.onMs + step, this.durationMs);
|
|
106
|
+
this.on = this.onMs < this.durationMs;
|
|
107
|
+
} else if (input && !this.previous) {
|
|
108
|
+
this.on = true;
|
|
109
|
+
this.onMs = 0;
|
|
110
|
+
}
|
|
111
|
+
this.previous = input;
|
|
112
|
+
return this.on;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** True for the one scan in which its input goes from off to on, including an input already on at the first scan. IEC `R_TRIG`. */
|
|
117
|
+
export class RisingEdge {
|
|
118
|
+
private previous = false;
|
|
119
|
+
/** Whether the last update saw the input rise. */
|
|
120
|
+
rose = false;
|
|
121
|
+
|
|
122
|
+
/** Takes this scan's input, and returns whether it rose. */
|
|
123
|
+
update(input: boolean): boolean {
|
|
124
|
+
this.rose = input && !this.previous;
|
|
125
|
+
this.previous = input;
|
|
126
|
+
return this.rose;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** True for the one scan in which its input goes from on to off. An input off from the first scan never falls. */
|
|
131
|
+
export class FallingEdge {
|
|
132
|
+
private previous = false;
|
|
133
|
+
/** Whether the last update saw the input fall. */
|
|
134
|
+
fell = false;
|
|
135
|
+
|
|
136
|
+
/** Takes this scan's input, and returns whether it fell. */
|
|
137
|
+
update(input: boolean): boolean {
|
|
138
|
+
this.fell = this.previous && !input;
|
|
139
|
+
this.previous = input;
|
|
140
|
+
return this.fell;
|
|
141
|
+
}
|
|
142
|
+
}
|
package/stdlib/telemetry.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { relative } from "node:path";
|
|
2
2
|
import { fileURLToPath } from "node:url";
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
3
|
+
import { failureCode, failureName } from "../src/ir/job-errors";
|
|
4
|
+
import { plcTime } from "./plc-time";
|
|
5
5
|
|
|
6
6
|
type Reported = Readonly<Record<string, unknown>>;
|
|
7
7
|
class Text {
|
|
@@ -35,13 +35,6 @@ const reportedValue = (value: Value) => {
|
|
|
35
35
|
return value;
|
|
36
36
|
};
|
|
37
37
|
|
|
38
|
-
const exceptionName = (error: unknown) => {
|
|
39
|
-
if (typeof error === "number") {
|
|
40
|
-
return errorName(error);
|
|
41
|
-
}
|
|
42
|
-
return error instanceof Error ? error.message : String(error);
|
|
43
|
-
};
|
|
44
|
-
|
|
45
38
|
const report = (record: Reported, properties: Properties = {}) =>
|
|
46
39
|
collector?.({
|
|
47
40
|
...record,
|
|
@@ -53,7 +46,7 @@ const report = (record: Reported, properties: Properties = {}) =>
|
|
|
53
46
|
),
|
|
54
47
|
source: source(),
|
|
55
48
|
scan: 0,
|
|
56
|
-
time:
|
|
49
|
+
time: plcTime(),
|
|
57
50
|
});
|
|
58
51
|
|
|
59
52
|
/** What a PLC program's `telemetry` calls do under Bun; see `types/telemetry.d.ts`. */
|
|
@@ -68,8 +61,8 @@ export const telemetry = {
|
|
|
68
61
|
report({ kind: "log", level: "error", message }, properties),
|
|
69
62
|
// Under Bun a failure can be a thrown Error rather than a code; it is sent as code 0 with its message.
|
|
70
63
|
captureException: (error: unknown, properties?: Properties) => {
|
|
71
|
-
const code =
|
|
72
|
-
const name =
|
|
64
|
+
const code = failureCode(error);
|
|
65
|
+
const name = failureName(error);
|
|
73
66
|
report(
|
|
74
67
|
{ kind: "exception", code, ...(name && { name }), handled: true },
|
|
75
68
|
properties,
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import "./data-types";
|
|
2
|
+
|
|
3
|
+
// The tsPLC dialect's globals: the language the compiler lowers a program into, whose generated
|
|
4
|
+
// code calls these ambiently. A program's own source can't use them (the frontend rejects them),
|
|
5
|
+
// and no package entry exports this file, so editors don't offer them. Ambient spellings point to
|
|
6
|
+
// the canonical SDK types; the frontend checks this declaration identity rather than trusting a
|
|
7
|
+
// same-named user alias.
|
|
8
|
+
declare global {
|
|
9
|
+
type Input<T> = import("./index").Input<T>;
|
|
10
|
+
type Output<T> = import("./index").Output<T>;
|
|
11
|
+
|
|
12
|
+
type InOut<T> = import("./index").InOut<T>;
|
|
13
|
+
type FunctionBlock<
|
|
14
|
+
I,
|
|
15
|
+
O = Record<never, never>,
|
|
16
|
+
> = import("./index").FunctionBlock<I, O>;
|
|
17
|
+
const FunctionBlock: typeof import("./index").FunctionBlock;
|
|
18
|
+
type Blinker = import("./index").Blinker;
|
|
19
|
+
const Blinker: typeof import("./index").Blinker;
|
|
20
|
+
type OnDelayTimer = import("./index").OnDelayTimer;
|
|
21
|
+
const OnDelayTimer: typeof import("./index").OnDelayTimer;
|
|
22
|
+
type OffDelayTimer = import("./index").OffDelayTimer;
|
|
23
|
+
const OffDelayTimer: typeof import("./index").OffDelayTimer;
|
|
24
|
+
type PulseTimer = import("./index").PulseTimer;
|
|
25
|
+
const PulseTimer: typeof import("./index").PulseTimer;
|
|
26
|
+
type RisingEdge = import("./index").RisingEdge;
|
|
27
|
+
const RisingEdge: typeof import("./index").RisingEdge;
|
|
28
|
+
type FallingEdge = import("./index").FallingEdge;
|
|
29
|
+
const FallingEdge: typeof import("./index").FallingEdge;
|
|
30
|
+
type UpCounter = import("./index").UpCounter;
|
|
31
|
+
const UpCounter: typeof import("./index").UpCounter;
|
|
32
|
+
type DownCounter = import("./index").DownCounter;
|
|
33
|
+
const DownCounter: typeof import("./index").DownCounter;
|
|
34
|
+
type UpDownCounter = import("./index").UpDownCounter;
|
|
35
|
+
const UpDownCounter: typeof import("./index").UpDownCounter;
|
|
36
|
+
const ref: typeof import("./index").ref;
|
|
37
|
+
const retain: typeof import("./index").retain;
|
|
38
|
+
const ms: typeof import("./index").ms;
|
|
39
|
+
const seconds: typeof import("./index").seconds;
|
|
40
|
+
const minutes: typeof import("./index").minutes;
|
|
41
|
+
const timeToMilliseconds: typeof import("./index").timeToMilliseconds;
|
|
42
|
+
const date: typeof import("./index").date;
|
|
43
|
+
const timeOfDay: typeof import("./index").timeOfDay;
|
|
44
|
+
const dateAndTime: typeof import("./index").dateAndTime;
|
|
45
|
+
|
|
46
|
+
const defaultValue: typeof import("./index").defaultValue;
|
|
47
|
+
const copy: typeof import("./index").copy;
|
|
48
|
+
const entries: typeof import("./index").entries;
|
|
49
|
+
}
|
package/types/globals.d.ts
CHANGED
|
@@ -1,50 +1,10 @@
|
|
|
1
1
|
import "./data-types";
|
|
2
2
|
|
|
3
|
-
//
|
|
4
|
-
//
|
|
3
|
+
// What every PLC source sees without an import: the IEC type names (from data-types) and
|
|
4
|
+
// VoidFunction. The SDK's functions and classes are imported from their modules.
|
|
5
5
|
declare global {
|
|
6
6
|
interface VoidFunction {
|
|
7
7
|
// biome-ignore lint/style/useShorthandFunctionType: This declaration must merge with lib.dom's interface.
|
|
8
8
|
(): void;
|
|
9
9
|
}
|
|
10
|
-
type Input<T> = import("./index").Input<T>;
|
|
11
|
-
type Output<T> = import("./index").Output<T>;
|
|
12
|
-
|
|
13
|
-
type InOut<T> = import("./index").InOut<T>;
|
|
14
|
-
type FunctionBlock<
|
|
15
|
-
I,
|
|
16
|
-
O = Record<never, never>,
|
|
17
|
-
> = import("./index").FunctionBlock<I, O>;
|
|
18
|
-
const FunctionBlock: typeof import("./index").FunctionBlock;
|
|
19
|
-
type Blinker = import("./index").Blinker;
|
|
20
|
-
const Blinker: typeof import("./index").Blinker;
|
|
21
|
-
type OnDelayTimer = import("./index").OnDelayTimer;
|
|
22
|
-
const OnDelayTimer: typeof import("./index").OnDelayTimer;
|
|
23
|
-
type OffDelayTimer = import("./index").OffDelayTimer;
|
|
24
|
-
const OffDelayTimer: typeof import("./index").OffDelayTimer;
|
|
25
|
-
type PulseTimer = import("./index").PulseTimer;
|
|
26
|
-
const PulseTimer: typeof import("./index").PulseTimer;
|
|
27
|
-
type RisingEdge = import("./index").RisingEdge;
|
|
28
|
-
const RisingEdge: typeof import("./index").RisingEdge;
|
|
29
|
-
type FallingEdge = import("./index").FallingEdge;
|
|
30
|
-
const FallingEdge: typeof import("./index").FallingEdge;
|
|
31
|
-
type UpCounter = import("./index").UpCounter;
|
|
32
|
-
const UpCounter: typeof import("./index").UpCounter;
|
|
33
|
-
type DownCounter = import("./index").DownCounter;
|
|
34
|
-
const DownCounter: typeof import("./index").DownCounter;
|
|
35
|
-
type UpDownCounter = import("./index").UpDownCounter;
|
|
36
|
-
const UpDownCounter: typeof import("./index").UpDownCounter;
|
|
37
|
-
const ref: typeof import("./index").ref;
|
|
38
|
-
const retain: typeof import("./index").retain;
|
|
39
|
-
const ms: typeof import("./index").ms;
|
|
40
|
-
const seconds: typeof import("./index").seconds;
|
|
41
|
-
const minutes: typeof import("./index").minutes;
|
|
42
|
-
const timeToMilliseconds: typeof import("./index").timeToMilliseconds;
|
|
43
|
-
const date: typeof import("./index").date;
|
|
44
|
-
const timeOfDay: typeof import("./index").timeOfDay;
|
|
45
|
-
const dateAndTime: typeof import("./index").dateAndTime;
|
|
46
|
-
|
|
47
|
-
const defaultValue: typeof import("./index").defaultValue;
|
|
48
|
-
const copy: typeof import("./index").copy;
|
|
49
|
-
const entries: typeof import("./index").entries;
|
|
50
10
|
}
|
package/types/index.d.ts
CHANGED
|
@@ -25,7 +25,8 @@ export declare function exact<T extends number | bigint>(
|
|
|
25
25
|
export declare function wrap<T extends number | bigint>(
|
|
26
26
|
value: number | bigint,
|
|
27
27
|
): T;
|
|
28
|
-
// Float-to-float saturation clips to finite target endpoints, then rounds to its width.
|
|
28
|
+
// Float-to-float saturation clips to finite target endpoints, then rounds to its width. A float
|
|
29
|
+
// saturated to an integer type drops its fraction toward zero, then clips; NaN becomes 0.
|
|
29
30
|
export declare function saturate<T extends number | bigint>(
|
|
30
31
|
value: number | bigint,
|
|
31
32
|
): T;
|
|
@@ -97,6 +98,7 @@ export type DATE_AND_TIME = string & {
|
|
|
97
98
|
export type InOut<T> = T;
|
|
98
99
|
export declare function ref<T>(value: T): InOut<T>;
|
|
99
100
|
export declare function retain<T>(value: T): T;
|
|
101
|
+
export declare function persist<T>(value: T): T;
|
|
100
102
|
|
|
101
103
|
export declare function ms(value: number): TIME;
|
|
102
104
|
export declare function seconds(value: number): TIME;
|
package/types/retain.d.ts
CHANGED
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Keeps state through a warm restart: `retain(initializer)` returns its argument, and the PLC
|
|
3
|
-
* declares the storage that holds it `RETAIN`. A
|
|
4
|
-
*
|
|
3
|
+
* declares the storage that holds it `RETAIN`. A program imports it from here, because its setup
|
|
4
|
+
* and unit tests run under Bun (docs/02-language/03-state-and-io.md).
|
|
5
5
|
*/
|
|
6
6
|
export { retain } from "./index";
|
|
7
|
+
/**
|
|
8
|
+
* Keeps a module binding's state through a download and a cold reset too: `persist(initializer)`
|
|
9
|
+
* returns its argument, and the PLC declares the storage that holds it `PERSISTENT RETAIN`, in the
|
|
10
|
+
* application's one persistent list (docs/02-language/03-state-and-io.md).
|
|
11
|
+
*/
|
|
12
|
+
export { persist } from "./index";
|
package/types/telemetry.d.ts
CHANGED
|
@@ -42,3 +42,6 @@ export declare const telemetry: Telemetry;
|
|
|
42
42
|
export declare function onTelemetry(
|
|
43
43
|
collector: (record: Readonly<Record<string, unknown>>) => void,
|
|
44
44
|
): () => void;
|
|
45
|
+
|
|
46
|
+
// biome-ignore lint/complexity/noUselessEmptyExport: without it a declaration file exports every top-level name, and `textBrand` has no value.
|
|
47
|
+
export {};
|