@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
|
@@ -1,9 +1,12 @@
|
|
|
1
|
+
import { readFileSync, realpathSync } from "node:fs";
|
|
2
|
+
import type { PluginBuilder } from "bun";
|
|
3
|
+
import ts from "typescript";
|
|
1
4
|
import { z } from "zod";
|
|
2
5
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
6
|
+
// The files the compiler and its setup subprocess (capture.ts) exchange: the request names the
|
|
7
|
+
// program's runtime files and the bindings each declares; the graph is what setup built. Setup
|
|
8
|
+
// and the Bun runtime both load the program's modules with a registry of those bindings.
|
|
9
|
+
|
|
7
10
|
/** An arrow or function expression setup may store, and the names it captures from around it. */
|
|
8
11
|
const closureSiteSchema = z.strictObject({
|
|
9
12
|
start: z.number().int().nonnegative(),
|
|
@@ -12,33 +15,94 @@ const closureSiteSchema = z.strictObject({
|
|
|
12
15
|
});
|
|
13
16
|
export type ClosureSite = z.infer<typeof closureSiteSchema>;
|
|
14
17
|
|
|
18
|
+
const bindingNamesSchema = z.strictObject({
|
|
19
|
+
values: z.array(z.string()),
|
|
20
|
+
classes: z.array(z.string()),
|
|
21
|
+
functions: z.array(z.string()),
|
|
22
|
+
});
|
|
23
|
+
export type BindingNames = z.infer<typeof bindingNamesSchema>;
|
|
24
|
+
|
|
15
25
|
export const captureRequestSchema = z.strictObject({
|
|
16
26
|
entry: z.string().nonempty(),
|
|
17
27
|
files: z.array(z.string().nonempty()),
|
|
18
28
|
/** By file, each closure whose creation setup notes, with what it captured. */
|
|
19
29
|
closures: z.record(z.string(), z.array(closureSiteSchema)),
|
|
20
|
-
bindings: z.record(
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
}),
|
|
27
|
-
),
|
|
30
|
+
bindings: z.record(z.string(), bindingNamesSchema),
|
|
31
|
+
/**
|
|
32
|
+
* What each of the program's `env()` calls reads, by name. Only those values: the rest of the
|
|
33
|
+
* environment, secrets the program never reads among it, stays out of this file.
|
|
34
|
+
*/
|
|
35
|
+
configuration: z.record(z.string(), z.string()),
|
|
28
36
|
});
|
|
29
37
|
export type CaptureRequest = z.infer<typeof captureRequestSchema>;
|
|
30
38
|
|
|
39
|
+
/** The names a declaration's name binds: itself, or every name in its destructuring pattern. */
|
|
40
|
+
export const boundNames = (name: ts.BindingName): ts.Identifier[] =>
|
|
41
|
+
ts.isIdentifier(name)
|
|
42
|
+
? [name]
|
|
43
|
+
: name.elements.flatMap((element) =>
|
|
44
|
+
ts.isOmittedExpression(element) ? [] : boundNames(element.name),
|
|
45
|
+
);
|
|
46
|
+
|
|
47
|
+
/** The top-level bindings a module declares: values, classes and functions, arrow functions among them. */
|
|
48
|
+
export const bindingNames = (source: ts.SourceFile): BindingNames => {
|
|
49
|
+
const found: BindingNames = { values: [], classes: [], functions: [] };
|
|
50
|
+
for (const statement of source.statements) {
|
|
51
|
+
if (ts.isVariableStatement(statement)) {
|
|
52
|
+
for (const declaration of statement.declarationList.declarations) {
|
|
53
|
+
// A stored callback names its binding, so a function a variable holds is a function.
|
|
54
|
+
if (
|
|
55
|
+
ts.isIdentifier(declaration.name) &&
|
|
56
|
+
declaration.initializer &&
|
|
57
|
+
(ts.isArrowFunction(declaration.initializer) ||
|
|
58
|
+
ts.isFunctionExpression(declaration.initializer))
|
|
59
|
+
) {
|
|
60
|
+
found.functions.push(declaration.name.text);
|
|
61
|
+
} else {
|
|
62
|
+
found.values.push(
|
|
63
|
+
...boundNames(declaration.name).map((name) => name.text),
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
} else if (ts.isClassDeclaration(statement) && statement.name) {
|
|
68
|
+
found.classes.push(statement.name.text);
|
|
69
|
+
} else if (
|
|
70
|
+
ts.isFunctionDeclaration(statement) &&
|
|
71
|
+
statement.name &&
|
|
72
|
+
statement.body
|
|
73
|
+
) {
|
|
74
|
+
found.functions.push(statement.name.text);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return found;
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
const registryExport = "__tsplcBindings";
|
|
81
|
+
|
|
31
82
|
/**
|
|
32
|
-
* A module's
|
|
33
|
-
*
|
|
34
|
-
* and functions themselves.
|
|
83
|
+
* A module's top-level bindings as a host reads them: each value through a getter, so a `let`
|
|
84
|
+
* reads as it is now, and its classes and functions themselves.
|
|
35
85
|
*/
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
)
|
|
41
|
-
|
|
86
|
+
const registrySchema = z.object({
|
|
87
|
+
values: z.record(
|
|
88
|
+
z.string(),
|
|
89
|
+
z.custom<() => unknown>((value) => typeof value === "function"),
|
|
90
|
+
),
|
|
91
|
+
classes: z.record(z.string(), z.unknown()),
|
|
92
|
+
functions: z.record(z.string(), z.unknown()),
|
|
93
|
+
});
|
|
94
|
+
export type Registry = z.infer<typeof registrySchema>;
|
|
95
|
+
|
|
96
|
+
/** The registry a module loaded through `loadWithBindingRegistry` exports; undefined for any other module. */
|
|
97
|
+
export const bindingRegistry = (
|
|
98
|
+
loaded: Record<string, unknown>,
|
|
99
|
+
): Registry | undefined => {
|
|
100
|
+
const registry = registrySchema.safeParse(loaded[registryExport]);
|
|
101
|
+
return registry.success ? registry.data : undefined;
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
const withBindingRegistry = (source: string, names: BindingNames) =>
|
|
105
|
+
`${source}\n;export const ${registryExport} = { values: { ${names.values
|
|
42
106
|
.map((name) => `${JSON.stringify(name)}: () => ${name}`)
|
|
43
107
|
.join(", ")} }, classes: { ${names.classes
|
|
44
108
|
.map((name) => `${JSON.stringify(name)}: ${name}`)
|
|
@@ -46,6 +110,37 @@ export const withBindingRegistry = (
|
|
|
46
110
|
.map((name) => `${JSON.stringify(name)}: ${name}`)
|
|
47
111
|
.join(", ")} } };\n`;
|
|
48
112
|
|
|
113
|
+
const escapeRegExp = (text: string) =>
|
|
114
|
+
text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Has Bun load each of `files` with its binding registry: `module` turns the file's source into
|
|
118
|
+
* the source that runs and the bindings it exports.
|
|
119
|
+
*/
|
|
120
|
+
export const loadWithBindingRegistry = (
|
|
121
|
+
build: PluginBuilder,
|
|
122
|
+
files: readonly string[],
|
|
123
|
+
module: (loaded: { file: string; source: string }) => {
|
|
124
|
+
source: string;
|
|
125
|
+
bindings: BindingNames;
|
|
126
|
+
},
|
|
127
|
+
): void => {
|
|
128
|
+
// Bun loads modules by their real path; the compiler may name them through a symlink.
|
|
129
|
+
const named = new Map(files.map((file) => [realpathSync(file), file]));
|
|
130
|
+
for (const [path, file] of named) {
|
|
131
|
+
build.onLoad({ filter: new RegExp(`^${escapeRegExp(path)}$`) }, () => {
|
|
132
|
+
const { source, bindings } = module({
|
|
133
|
+
file,
|
|
134
|
+
source: readFileSync(path, "utf8"),
|
|
135
|
+
});
|
|
136
|
+
return {
|
|
137
|
+
contents: withBindingRegistry(source, bindings),
|
|
138
|
+
loader: path.endsWith(".tsx") ? "tsx" : "ts",
|
|
139
|
+
};
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
|
|
49
144
|
const capturedValueSchema = z.discriminatedUnion("kind", [
|
|
50
145
|
z.strictObject({ kind: z.literal("undefined") }),
|
|
51
146
|
// JSON has no NaN or infinity; capture rejects them rather than write null.
|
|
@@ -86,6 +181,8 @@ const capturedNodeSchema = z.discriminatedUnion("kind", [
|
|
|
86
181
|
name: z.string(),
|
|
87
182
|
}),
|
|
88
183
|
z.strictObject({ kind: z.literal("service"), path: z.string() }),
|
|
184
|
+
/** What `tasks.define` or `backgroundTasks.define` returned: a task the PLC runs, which scan code calls through its binding. */
|
|
185
|
+
z.strictObject({ kind: z.literal("task") }),
|
|
89
186
|
/** A function setup made inside another, by where it is written, with the values it captured. */
|
|
90
187
|
z.strictObject({
|
|
91
188
|
kind: z.literal("closure"),
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
// biome-ignore-all lint/style/noNonNullAssertion: Retain existing non-null assertions during the tooling migration; changing their contracts requires a separate review.
|
|
2
1
|
/**
|
|
3
2
|
* Runs a program's setup code in Bun and writes the object graph it builds: every module binding,
|
|
4
3
|
* and every object, array, class instance and function reachable from them. The compiler runs
|
|
@@ -8,24 +7,30 @@
|
|
|
8
7
|
*/
|
|
9
8
|
|
|
10
9
|
import { readFileSync, realpathSync, writeFileSync } from "node:fs";
|
|
10
|
+
import { nanosecondsPerMillisecond } from "@tsplc/iec";
|
|
11
|
+
import { elementaryValue } from "@tsplc/iec/arithmetic";
|
|
11
12
|
import { plugin } from "bun";
|
|
13
|
+
import { durationScales } from "../intrinsics";
|
|
14
|
+
import { sdkModules, telemetryMethods } from "../sdk";
|
|
12
15
|
import {
|
|
16
|
+
bindingRegistry,
|
|
13
17
|
type CapturedGraph,
|
|
14
18
|
type CapturedNode,
|
|
15
19
|
type CapturedValue,
|
|
16
20
|
type CaptureRequest,
|
|
17
21
|
type ClosureSite,
|
|
18
22
|
captureRequestSchema,
|
|
19
|
-
|
|
23
|
+
loadWithBindingRegistry,
|
|
24
|
+
type Registry,
|
|
20
25
|
} from "./capture-protocol";
|
|
21
26
|
|
|
27
|
+
const [, , requestFile, graphFile] = process.argv;
|
|
28
|
+
if (!requestFile || !graphFile) {
|
|
29
|
+
throw new Error("Usage: bun capture.ts <request.json> <graph.json>");
|
|
30
|
+
}
|
|
22
31
|
const request: CaptureRequest = captureRequestSchema.parse(
|
|
23
|
-
JSON.parse(readFileSync(
|
|
32
|
+
JSON.parse(readFileSync(requestFile, "utf8")),
|
|
24
33
|
);
|
|
25
|
-
// Bun loads modules by their real path; the compiler may name them through a symlink.
|
|
26
|
-
const real = new Map(request.files.map((file) => [realpathSync(file), file]));
|
|
27
|
-
const runtime = new Set(real.keys());
|
|
28
|
-
const exportName = "__tsplcCapture";
|
|
29
34
|
|
|
30
35
|
const servicePath = Symbol("tsplc service path");
|
|
31
36
|
/** A service client: any property is a deeper procedure path, and its identity names that path. */
|
|
@@ -38,86 +43,171 @@ const service = (path: readonly string[]): unknown =>
|
|
|
38
43
|
},
|
|
39
44
|
});
|
|
40
45
|
|
|
46
|
+
const taskMark = Symbol("tsplc task");
|
|
47
|
+
/** A task `define` made: scan code starts and reads it, so setup may only store it. */
|
|
48
|
+
const task = (): unknown =>
|
|
49
|
+
Object.assign(
|
|
50
|
+
() => {
|
|
51
|
+
throw new Error("Setup code started a PLC task.");
|
|
52
|
+
},
|
|
53
|
+
{ [taskMark]: true },
|
|
54
|
+
);
|
|
55
|
+
|
|
56
|
+
/** Objects setup passed to `retain`. A retained primitive has no identity; the compiler finds those in the source. */
|
|
57
|
+
const retainedObjects = new Set<object>();
|
|
58
|
+
|
|
59
|
+
const unsupported = (name: string) => (): never => {
|
|
60
|
+
throw new Error(`${name} cannot run during PLC setup.`);
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
/** A class setup can import, but not construct. */
|
|
64
|
+
const unsupportedClass = (name: string) =>
|
|
65
|
+
class {
|
|
66
|
+
constructor() {
|
|
67
|
+
throw new Error(`${name} cannot run during PLC setup.`);
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
/** A TIME constructor: setup holds a duration as milliseconds. */
|
|
72
|
+
const duration = (name: string) => {
|
|
73
|
+
const scale = durationScales.get(name);
|
|
74
|
+
if (scale === undefined) {
|
|
75
|
+
throw new Error(`The SDK has no duration ${name}.`);
|
|
76
|
+
}
|
|
77
|
+
return (value: number): number =>
|
|
78
|
+
value * Number(scale / nanosecondsPerMillisecond);
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
/** A stub for each value a declaration file exports; its brand symbols exist only as types. */
|
|
82
|
+
type Stubs<Module> = Record<
|
|
83
|
+
Exclude<keyof Module, "timeBrand" | "calendarBrand" | "textBrand">,
|
|
84
|
+
unknown
|
|
85
|
+
>;
|
|
86
|
+
|
|
87
|
+
/** The SDK's modules as setup runs them, by the specifier a program imports each by. */
|
|
88
|
+
const setupSdk: Readonly<Record<string, Readonly<Record<string, unknown>>>> = {
|
|
89
|
+
// Setup runs before the first scan, so the clock reads 0 there.
|
|
90
|
+
[sdkModules.clock]: {
|
|
91
|
+
now: () => 0,
|
|
92
|
+
} satisfies typeof import("../../../types/clock"),
|
|
93
|
+
// Records describe scans, so setup has none to make.
|
|
94
|
+
[sdkModules.telemetry]: {
|
|
95
|
+
telemetry: Object.fromEntries(
|
|
96
|
+
telemetryMethods.map((name) => [name, unsupported(`telemetry.${name}`)]),
|
|
97
|
+
),
|
|
98
|
+
onTelemetry: () => () => {},
|
|
99
|
+
} satisfies Stubs<typeof import("../../../types/telemetry")>,
|
|
100
|
+
// Retention belongs to storage, so setup notes each object it retains for the compiler. What
|
|
101
|
+
// persists is a whole module binding, which the compiler finds in the source.
|
|
102
|
+
[sdkModules.retain]: {
|
|
103
|
+
retain: <T>(value: T): T => {
|
|
104
|
+
if (typeof value === "object" && value !== null) {
|
|
105
|
+
retainedObjects.add(value);
|
|
106
|
+
}
|
|
107
|
+
return value;
|
|
108
|
+
},
|
|
109
|
+
persist: <T>(value: T): T => value,
|
|
110
|
+
} satisfies typeof import("../../../types/retain"),
|
|
111
|
+
// The SDK's types module has no runtime of its own; a module importing a time constructor still loads.
|
|
112
|
+
[sdkModules.types]: {
|
|
113
|
+
...elementaryValue,
|
|
114
|
+
ms: duration("ms"),
|
|
115
|
+
seconds: duration("seconds"),
|
|
116
|
+
minutes: duration("minutes"),
|
|
117
|
+
trunc: Math.trunc,
|
|
118
|
+
expt: Math.pow,
|
|
119
|
+
exact: unsupported("exact"),
|
|
120
|
+
wrap: unsupported("wrap"),
|
|
121
|
+
saturate: unsupported("saturate"),
|
|
122
|
+
wrapAdd: unsupported("wrapAdd"),
|
|
123
|
+
wrapSubtract: unsupported("wrapSubtract"),
|
|
124
|
+
wrapMultiply: unsupported("wrapMultiply"),
|
|
125
|
+
saturatingAdd: unsupported("saturatingAdd"),
|
|
126
|
+
saturatingSubtract: unsupported("saturatingSubtract"),
|
|
127
|
+
saturatingMultiply: unsupported("saturatingMultiply"),
|
|
128
|
+
div: unsupported("div"),
|
|
129
|
+
defaultValue: unsupported("defaultValue"),
|
|
130
|
+
copy: unsupported("copy"),
|
|
131
|
+
entries: unsupported("entries"),
|
|
132
|
+
ref: unsupported("ref"),
|
|
133
|
+
retain: unsupported("retain"),
|
|
134
|
+
persist: unsupported("persist"),
|
|
135
|
+
timeToMilliseconds: unsupported("timeToMilliseconds"),
|
|
136
|
+
// Setup holds a calendar value as the text it was written in, which the compiler checks.
|
|
137
|
+
date: (value: string): string => value,
|
|
138
|
+
timeOfDay: (value: string): string => value,
|
|
139
|
+
dateAndTime: (value: string): string => value,
|
|
140
|
+
FunctionBlock: unsupportedClass("FunctionBlock"),
|
|
141
|
+
Blinker: unsupportedClass("Blinker"),
|
|
142
|
+
OnDelayTimer: unsupportedClass("OnDelayTimer"),
|
|
143
|
+
OffDelayTimer: unsupportedClass("OffDelayTimer"),
|
|
144
|
+
PulseTimer: unsupportedClass("PulseTimer"),
|
|
145
|
+
RisingEdge: unsupportedClass("RisingEdge"),
|
|
146
|
+
FallingEdge: unsupportedClass("FallingEdge"),
|
|
147
|
+
UpCounter: unsupportedClass("UpCounter"),
|
|
148
|
+
DownCounter: unsupportedClass("DownCounter"),
|
|
149
|
+
UpDownCounter: unsupportedClass("UpDownCounter"),
|
|
150
|
+
} satisfies Stubs<typeof import("../../../types/index")>,
|
|
151
|
+
[sdkModules.async]: {
|
|
152
|
+
createServiceClient: () => service([]),
|
|
153
|
+
backgroundTasks: { define: task },
|
|
154
|
+
tasks: { define: task },
|
|
155
|
+
cooperativeTasks: { define: unsupported("cooperativeTasks.define") },
|
|
156
|
+
fetch: unsupported("fetch"),
|
|
157
|
+
sleep: unsupported("sleep"),
|
|
158
|
+
cancelAsync: unsupported("cancelAsync"),
|
|
159
|
+
asyncDiagnostics: unsupported("asyncDiagnostics"),
|
|
160
|
+
errorCode: unsupported("errorCode"),
|
|
161
|
+
opcuaCall: unsupported("opcuaCall"),
|
|
162
|
+
} satisfies Stubs<typeof import("../../../types/async")>,
|
|
163
|
+
// The compiler resolved each configuration read before setup, from the build's environment.
|
|
164
|
+
[sdkModules.config]: {
|
|
165
|
+
env: (name: string): string => {
|
|
166
|
+
const value = request.configuration[name];
|
|
167
|
+
if (value === undefined) {
|
|
168
|
+
throw new Error(`env cannot read ${name} during PLC setup.`);
|
|
169
|
+
}
|
|
170
|
+
return value;
|
|
171
|
+
},
|
|
172
|
+
} satisfies Stubs<typeof import("../../../types/config")>,
|
|
173
|
+
};
|
|
174
|
+
|
|
41
175
|
plugin({
|
|
42
176
|
name: "tsplc-capture",
|
|
43
177
|
setup(build) {
|
|
44
|
-
build.
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
request.bindings[real.get(path)!]!,
|
|
58
|
-
exportName,
|
|
59
|
-
),
|
|
60
|
-
loader: "ts",
|
|
61
|
-
}),
|
|
62
|
-
);
|
|
63
|
-
// Setup runs before the first scan, so the clock reads 0 there.
|
|
64
|
-
build.module("@tsplc/compiler/clock", () => ({
|
|
65
|
-
contents: "export const now = () => 0;",
|
|
66
|
-
loader: "js",
|
|
67
|
-
}));
|
|
68
|
-
// Records describe scans, so setup has none to make.
|
|
69
|
-
build.module("@tsplc/compiler/telemetry", () => ({
|
|
70
|
-
contents: `const unsupported = (name) => () => { throw new Error("telemetry." + name + " cannot run during PLC setup."); };
|
|
71
|
-
export const telemetry = Object.fromEntries(["capture", "info", "warn", "error", "captureException", "text"].map((name) => [name, unsupported(name)]));
|
|
72
|
-
export const onTelemetry = () => () => {};`,
|
|
73
|
-
loader: "js",
|
|
74
|
-
}));
|
|
75
|
-
// Retention belongs to storage, so setup notes each object it retains for the compiler.
|
|
76
|
-
build.module("@tsplc/compiler/retain", () => ({
|
|
77
|
-
contents:
|
|
78
|
-
"export const retain = (value) => globalThis.__tsplcRetain(value);",
|
|
79
|
-
loader: "js",
|
|
80
|
-
}));
|
|
81
|
-
// The SDK's types module has no runtime of its own; a module importing a time constructor still loads.
|
|
82
|
-
build.module("@tsplc/compiler/types", () => ({
|
|
83
|
-
contents: `const unsupported = (name) => () => { throw new Error(name + " cannot run during PLC setup."); };
|
|
84
|
-
export const ms = (value) => value, seconds = (value) => value * 1000, minutes = (value) => value * 60000;
|
|
85
|
-
export const exact = unsupported("exact"), wrap = unsupported("wrap"), saturate = unsupported("saturate");
|
|
86
|
-
export const wrapAdd = unsupported("wrapAdd"), wrapSubtract = unsupported("wrapSubtract"), wrapMultiply = unsupported("wrapMultiply");
|
|
87
|
-
export const saturatingAdd = unsupported("saturatingAdd"), saturatingSubtract = unsupported("saturatingSubtract"), saturatingMultiply = unsupported("saturatingMultiply");
|
|
88
|
-
export const div = unsupported("div"), defaultValue = unsupported("defaultValue"), copy = unsupported("copy"), entries = unsupported("entries");
|
|
89
|
-
export const ref = unsupported("ref"), retain = unsupported("retain"), timeToMilliseconds = unsupported("timeToMilliseconds");
|
|
90
|
-
export const date = unsupported("date"), timeOfDay = unsupported("timeOfDay"), dateAndTime = unsupported("dateAndTime");
|
|
91
|
-
export const sqrt = Math.sqrt, ln = Math.log, log10 = Math.log10, exp = Math.exp, sin = Math.sin, cos = Math.cos, tan = Math.tan;
|
|
92
|
-
export const asin = Math.asin, acos = Math.acos, atan = Math.atan, trunc = Math.trunc, expt = Math.pow;`,
|
|
93
|
-
loader: "js",
|
|
94
|
-
}));
|
|
95
|
-
build.module("@tsplc/compiler/async", () => ({
|
|
96
|
-
contents: `export const createServiceClient = () => globalThis.__tsplcService([]);
|
|
97
|
-
const unsupported = (name) => () => { throw new Error(name + " cannot run during PLC setup."); };
|
|
98
|
-
export const sleep = unsupported("sleep"), fetch = unsupported("fetch"), cancelAsync = unsupported("cancelAsync");
|
|
99
|
-
export const errorCode = unsupported("errorCode"), asyncDiagnostics = unsupported("asyncDiagnostics"), opcuaCall = unsupported("opcuaCall");`,
|
|
100
|
-
loader: "js",
|
|
101
|
-
}));
|
|
178
|
+
loadWithBindingRegistry(build, request.files, ({ file, source }) => {
|
|
179
|
+
const bindings = request.bindings[file];
|
|
180
|
+
if (!bindings) {
|
|
181
|
+
throw new Error(`The capture request names no bindings of ${file}.`);
|
|
182
|
+
}
|
|
183
|
+
return {
|
|
184
|
+
source: withClosureNotes(source, file, request.closures[file] ?? []),
|
|
185
|
+
bindings,
|
|
186
|
+
};
|
|
187
|
+
});
|
|
188
|
+
for (const [specifier, exports] of Object.entries(setupSdk)) {
|
|
189
|
+
build.module(specifier, () => ({ exports, loader: "object" }));
|
|
190
|
+
}
|
|
102
191
|
},
|
|
103
192
|
});
|
|
104
|
-
(globalThis as { __tsplcService?: unknown }).__tsplcService = service;
|
|
105
193
|
|
|
106
194
|
/** Closures setup made, with where each is written and a reader of what it captured. */
|
|
107
195
|
const closures = new Map<
|
|
108
196
|
unknown,
|
|
109
197
|
{ file: string; start: number; names: string[]; captured: () => unknown[] }
|
|
110
198
|
>();
|
|
111
|
-
(globalThis
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
) =>
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
199
|
+
Object.assign(globalThis, {
|
|
200
|
+
__tsplcClosure: (
|
|
201
|
+
file: string,
|
|
202
|
+
start: number,
|
|
203
|
+
names: string[],
|
|
204
|
+
made: unknown,
|
|
205
|
+
captured: () => unknown[],
|
|
206
|
+
) => {
|
|
207
|
+
closures.set(made, { file, start, names, captured });
|
|
208
|
+
return made;
|
|
209
|
+
},
|
|
210
|
+
});
|
|
121
211
|
|
|
122
212
|
/**
|
|
123
213
|
* Wraps each closure in the source so making it notes it. The wrapper keeps the expression's value,
|
|
@@ -140,7 +230,7 @@ const withClosureNotes = (
|
|
|
140
230
|
text: `), () => [${site.captures.join(", ")}])`,
|
|
141
231
|
},
|
|
142
232
|
]);
|
|
143
|
-
//
|
|
233
|
+
// At one offset, a closing wrapper goes before an opening one.
|
|
144
234
|
inserts.sort((left, right) => right.at - left.at || left.order - right.order);
|
|
145
235
|
return inserts.reduce(
|
|
146
236
|
(result, insert) =>
|
|
@@ -148,31 +238,16 @@ const withClosureNotes = (
|
|
|
148
238
|
text,
|
|
149
239
|
);
|
|
150
240
|
};
|
|
151
|
-
/** Objects setup passed to `retain`. A retained primitive has no identity; the compiler finds those in the source. */
|
|
152
|
-
const retainedObjects = new Set<object>();
|
|
153
|
-
(globalThis as { __tsplcRetain?: unknown }).__tsplcRetain = (
|
|
154
|
-
value: unknown,
|
|
155
|
-
) => {
|
|
156
|
-
if (typeof value === "object" && value !== null) {
|
|
157
|
-
retainedObjects.add(value);
|
|
158
|
-
}
|
|
159
|
-
return value;
|
|
160
|
-
};
|
|
161
|
-
|
|
162
|
-
/** The bindings each runtime module exports for capture, as `bindings` above adds them. */
|
|
163
|
-
interface Registry {
|
|
164
|
-
values: Record<string, () => unknown>;
|
|
165
|
-
classes: Record<string, unknown>;
|
|
166
|
-
functions: Record<string, unknown>;
|
|
167
|
-
}
|
|
168
|
-
|
|
169
241
|
const modules = new Map<string, Registry>();
|
|
170
242
|
for (const file of [
|
|
171
243
|
request.entry,
|
|
172
244
|
...request.files.filter((file) => file !== request.entry),
|
|
173
245
|
]) {
|
|
174
|
-
const
|
|
175
|
-
|
|
246
|
+
const registry = bindingRegistry(await import(realpathSync(file)));
|
|
247
|
+
if (!registry) {
|
|
248
|
+
throw new Error(`${file} loaded without its bindings.`);
|
|
249
|
+
}
|
|
250
|
+
modules.set(file, registry);
|
|
176
251
|
}
|
|
177
252
|
|
|
178
253
|
const classes = new Map<unknown, { file: string; name: string }>();
|
|
@@ -189,11 +264,8 @@ for (const [file, registry] of modules) {
|
|
|
189
264
|
const nodes: CapturedNode[] = [];
|
|
190
265
|
const ids = new Map<object, number>();
|
|
191
266
|
const capture = (value: unknown, where: string): CapturedValue => {
|
|
192
|
-
if (value === undefined) {
|
|
193
|
-
return { kind: "undefined" };
|
|
194
|
-
}
|
|
195
267
|
// null is the PLC's one empty value, as undefined is.
|
|
196
|
-
if (value === null) {
|
|
268
|
+
if (value === undefined || value === null) {
|
|
197
269
|
return { kind: "undefined" };
|
|
198
270
|
}
|
|
199
271
|
if (typeof value === "number") {
|
|
@@ -222,11 +294,13 @@ const capture = (value: unknown, where: string): CapturedValue => {
|
|
|
222
294
|
ids.set(value, id);
|
|
223
295
|
nodes.push({ kind: "object", fields: {} });
|
|
224
296
|
if (typeof value === "function") {
|
|
225
|
-
const path = (value
|
|
297
|
+
const path: unknown = Reflect.get(value, servicePath);
|
|
226
298
|
const named = functions.get(value) ?? classes.get(value);
|
|
227
299
|
const closure = closures.get(value);
|
|
228
|
-
if (path
|
|
300
|
+
if (typeof path === "string") {
|
|
229
301
|
nodes[id] = { kind: "service", path };
|
|
302
|
+
} else if (Reflect.get(value, taskMark) === true) {
|
|
303
|
+
nodes[id] = { kind: "task" };
|
|
230
304
|
} else if (named) {
|
|
231
305
|
nodes[id] = { kind: "function", ...named };
|
|
232
306
|
} else if (closure) {
|
|
@@ -275,15 +349,28 @@ const capture = (value: unknown, where: string): CapturedValue => {
|
|
|
275
349
|
return { kind: "node", id };
|
|
276
350
|
};
|
|
277
351
|
|
|
352
|
+
/** What a binding holds; undefined for one the module never initialized, which reading throws on. */
|
|
353
|
+
const initializedValue = (
|
|
354
|
+
read: () => unknown,
|
|
355
|
+
): { value: unknown } | undefined => {
|
|
356
|
+
try {
|
|
357
|
+
return { value: read() };
|
|
358
|
+
} catch (cause) {
|
|
359
|
+
if (cause instanceof ReferenceError) {
|
|
360
|
+
return undefined;
|
|
361
|
+
}
|
|
362
|
+
throw cause;
|
|
363
|
+
}
|
|
364
|
+
};
|
|
365
|
+
|
|
278
366
|
const graph: CapturedGraph = { nodes, bindings: [], retained: [] };
|
|
279
367
|
for (const [file, registry] of modules) {
|
|
280
368
|
for (const [name, read] of Object.entries(registry.values)) {
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
} catch {
|
|
285
|
-
continue; // A binding the module never initialized.
|
|
369
|
+
const initialized = initializedValue(read);
|
|
370
|
+
if (!initialized) {
|
|
371
|
+
continue;
|
|
286
372
|
}
|
|
373
|
+
const { value } = initialized;
|
|
287
374
|
if (
|
|
288
375
|
typeof value === "function" &&
|
|
289
376
|
(functions.has(value) || classes.has(value))
|
|
@@ -298,4 +385,4 @@ graph.retained = [...retainedObjects].flatMap((value) => {
|
|
|
298
385
|
const id = ids.get(value);
|
|
299
386
|
return id === undefined ? [] : [id];
|
|
300
387
|
});
|
|
301
|
-
writeFileSync(
|
|
388
|
+
writeFileSync(graphFile, JSON.stringify(graph));
|