@tsplc/compiler 0.4.3 → 0.5.0

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/package.json CHANGED
@@ -17,7 +17,7 @@
17
17
  "url": "git+https://github.com/rip-technologies/tsplc.git",
18
18
  "directory": "packages/compiler"
19
19
  },
20
- "version": "0.4.3",
20
+ "version": "0.5.0",
21
21
  "type": "module",
22
22
  "exports": {
23
23
  ".": {
@@ -101,17 +101,17 @@
101
101
  "bun": ">=1.4.3"
102
102
  },
103
103
  "dependencies": {
104
- "@tsplc/agent": "0.4.3",
105
- "@tsplc/build": "0.4.3",
106
- "@tsplc/config": "0.4.3",
107
- "@tsplc/debug": "0.4.3",
108
- "@tsplc/opcua": "0.4.3",
109
- "@tsplc/typegen": "0.4.3",
104
+ "@tsplc/agent": "0.5.0",
105
+ "@tsplc/build": "0.5.0",
106
+ "@tsplc/config": "0.5.0",
107
+ "@tsplc/debug": "0.5.0",
108
+ "@tsplc/opcua": "0.5.0",
109
+ "@tsplc/typegen": "0.5.0",
110
110
  "typescript": "6.0.3",
111
111
  "zod": "4.3.6",
112
112
  "@types/bun": "1.4.2",
113
- "@tsplc/server": "0.4.3",
114
- "@tsplc/iec": "0.4.3"
113
+ "@tsplc/server": "0.5.0",
114
+ "@tsplc/iec": "0.5.0"
115
115
  },
116
116
  "bin": {
117
117
  "tsplc": "./dist/cli.js"
@@ -417,7 +417,7 @@ const httpServiceBlock = (
417
417
  network: string,
418
418
  /** The requesting job's id input and this block's request counter, sent as `x-tsplc-request`. */
419
419
  trace: { job: string; sequence: string } | undefined,
420
- ): GeneratedObject =>
420
+ ): [GeneratedObject, GeneratedObject] =>
421
421
  nativeHttp({
422
422
  id: block.id,
423
423
  name,
@@ -470,12 +470,14 @@ export const nativeHttp = ({
470
470
  decode: string;
471
471
  /** The requesting job's id input and this block's request counter, sent as `x-tsplc-request`. */
472
472
  trace?: { job: string; sequence: string };
473
- }): GeneratedObject => {
473
+ }): [GeneratedObject, GeneratedObject] => {
474
474
  const requestHeader = trace
475
475
  ? `AppendWire(UDINT_TO_STRING(${trace.job}));AppendWire(':');AppendWire(UDINT_TO_STRING(${trace.sequence}));AppendWire('$R$NContent-Length: ');`
476
476
  : "";
477
+ const release =
478
+ "resolver(xExecute:=FALSE,itfIPAddress:=remoteIP);client(xEnable:=FALSE);writer(xExecute:=FALSE);reader(xEnable:=FALSE);timeout(IN:=FALSE);";
477
479
  // Helpers are emitted as native methods under the HTTP block by the emitter.
478
- return {
480
+ const block: GeneratedObject = {
479
481
  id,
480
482
  kind: "functionBlock",
481
483
  name,
@@ -513,7 +515,7 @@ END_VAR
513
515
  // The host goes in the resolver call, not an initial value: an online change keeps the values
514
516
  // an instance already holds, so a changed host would otherwise wait for a download.
515
517
  implementation: `IF NOT ${port("start")} OR ${port("cancel")} THEN
516
- resolver(xExecute:=FALSE,itfIPAddress:=remoteIP);client(xEnable:=FALSE); writer(xExecute:=FALSE); reader(xEnable:=FALSE); timeout(IN:=FALSE);
518
+ ${release}
517
519
  stage:=0; ${port("busy")}:=FALSE; ${port("done")}:=FALSE; ${port("error")}:=0;
518
520
  RETURN;
519
521
  END_IF;
@@ -573,11 +575,28 @@ IF stage=3 THEN
573
575
  END_IF;
574
576
  END_IF;
575
577
  IF stage=90 THEN
576
- resolver(xExecute:=FALSE,itfIPAddress:=remoteIP);client(xEnable:=FALSE);writer(xExecute:=FALSE);reader(xEnable:=FALSE);timeout(IN:=FALSE);
578
+ ${release}
577
579
  ${port("busy")}:=FALSE;${port("done")}:=TRUE;
578
580
  END_IF;
579
581
  `,
580
582
  };
583
+ // CODESYS calls FB_exit before a download, reset or application stop discards the instance, and
584
+ // with bInCopyCode before an online change copies it. A connection still open then keeps the
585
+ // Net Base Services task in a socket call, and the runtime can't delete the old application's
586
+ // tasks until it restarts. A request this interrupts completes as a network error rather than
587
+ // being sent again, since tsPLC never repeats a remote side effect.
588
+ const exit: GeneratedObject = {
589
+ id: `${id}:FB_exit`,
590
+ parent: id,
591
+ kind: "method",
592
+ name: "FB_exit",
593
+ declaration:
594
+ "METHOD PUBLIC FB_exit : BOOL\nVAR_INPUT\n bInCopyCode : BOOL;\nEND_VAR",
595
+ implementation: `IF stage>0 AND stage<90 THEN ${port("error")}:=${jobError.network};stage:=90;END_IF;
596
+ ${release}
597
+ FB_exit:=TRUE;`,
598
+ };
599
+ return [block, exit];
581
600
  };
582
601
 
583
602
  export const httpCertificateVerifier = (
@@ -848,8 +867,8 @@ END_VAR`,
848
867
  symbols.targetTypes,
849
868
  );
850
869
  const requestFields = instrumentation.fields.get(fn.id);
851
- const generated = rpcGlobal
852
- ? opcuaServiceBlock(fn, name, codecs, endpoint, rpcGlobal)
870
+ const [generated, ...lifecycle] = rpcGlobal
871
+ ? [opcuaServiceBlock(fn, name, codecs, endpoint, rpcGlobal)]
853
872
  : httpServiceBlock(
854
873
  fn,
855
874
  name,
@@ -862,6 +881,7 @@ END_VAR`,
862
881
  );
863
882
  objects.push(
864
883
  generated,
884
+ ...lifecycle,
865
885
  ...httpMethods(generated.id, endpoint.transport),
866
886
  ...httpCertificateVerifier(generated.id, generated.name, endpoint),
867
887
  );
@@ -623,7 +623,7 @@ export function telemetryObjects({
623
623
  const helpers = httpMethods(writer).filter((item) =>
624
624
  ["ByteLength", "Append", "WriteString"].includes(item.name),
625
625
  );
626
- const client = nativeHttp({
626
+ const [client, clientExit] = nativeHttp({
627
627
  id: telemetryClient,
628
628
  name: names.client,
629
629
  ports: `VAR_INPUT\nstart : BOOL;\ncancel : BOOL;\nEND_VAR\nVAR_IN_OUT\nbody : STRING(${plcBufferBytes});\nEND_VAR\nVAR_OUTPUT\nbusy : BOOL;\ndone : BOOL;\nerror : UDINT;\nEND_VAR`,
@@ -773,6 +773,7 @@ WriteString(text);
773
773
  ...failures,
774
774
  ...trace,
775
775
  client,
776
+ clientExit,
776
777
  ...httpMethods(telemetryClient),
777
778
  ...httpCertificateVerifier(telemetryClient, names.client, endpoint),
778
779
  ];
@@ -29,6 +29,19 @@ interface LoopHeader {
29
29
  existing: boolean;
30
30
  }
31
31
 
32
+ /**
33
+ * A case's statements, out of the braces that scope its declarations, as elaboration writes every case: they end where the
34
+ * braces do, and blocks lower without a scope of their own.
35
+ */
36
+ const unbraced = (
37
+ statements: readonly ts.Statement[],
38
+ ): readonly ts.Statement[] => {
39
+ const [only] = statements;
40
+ return statements.length === 1 && only && ts.isBlock(only)
41
+ ? unbraced(only.statements)
42
+ : statements;
43
+ };
44
+
32
45
  /**
33
46
  * Lowers one routine's statements. Expressions, and the scope facts that
34
47
  * declarations, branches and loops establish, go through its ExpressionLowering.
@@ -235,10 +248,11 @@ export class StatementLowering {
235
248
  } else {
236
249
  labels.push(this.caseValue(clause, value.type));
237
250
  }
238
- if (!clause.statements.length) {
251
+ const statements = unbraced(clause.statements);
252
+ if (!statements.length) {
239
253
  continue;
240
254
  }
241
- const body = this.caseBody(clause);
255
+ const body = this.caseBody(clause, statements);
242
256
  if (labels.length) {
243
257
  cases.push({ values: labels.splice(0), body });
244
258
  }
@@ -271,10 +285,13 @@ export class StatementLowering {
271
285
  }
272
286
 
273
287
  /** A case's statements without the break that ends it. */
274
- private caseBody(clause: ts.CaseOrDefaultClause): Statement[] {
275
- const last = clause.statements.at(-1);
288
+ private caseBody(
289
+ clause: ts.CaseOrDefaultClause,
290
+ statements: readonly ts.Statement[],
291
+ ): Statement[] {
292
+ const last = statements.at(-1);
276
293
  if (last && ts.isBreakStatement(last) && !last.label) {
277
- return this.statements(clause.statements.slice(0, -1));
294
+ return this.statements(statements.slice(0, -1));
278
295
  }
279
296
  if (!(last && ts.isReturnStatement(last))) {
280
297
  this.fail(
@@ -283,7 +300,7 @@ export class StatementLowering {
283
300
  "Switch cases must end with break or return; fallthrough is prohibited.",
284
301
  );
285
302
  }
286
- return this.statements(clause.statements);
303
+ return this.statements(statements);
287
304
  }
288
305
 
289
306
  private localDeclarations(node: ts.VariableStatement): Statement[] {