@tsplc/compiler 0.5.5 → 0.5.7

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.
Files changed (80) hide show
  1. package/dist/bun-runtime.js +152 -31
  2. package/dist/bun-runtime.js.map +10 -9
  3. package/dist/cli.js +8 -5
  4. package/dist/cli.js.map +3 -3
  5. package/dist/codesys.js +260 -85
  6. package/dist/codesys.js.map +9 -9
  7. package/dist/collections.js +395 -0
  8. package/dist/collections.js.map +10 -0
  9. package/dist/elaborate-capture.js +62 -4
  10. package/dist/elaborate-capture.js.map +4 -4
  11. package/dist/index.js +5322 -1058
  12. package/dist/index.js.map +30 -23
  13. package/dist/ir.js +122 -1
  14. package/dist/ir.js.map +6 -6
  15. package/dist/project.js +8 -5
  16. package/dist/project.js.map +3 -3
  17. package/package.json +14 -9
  18. package/src/codesys/emit.ts +8 -2
  19. package/src/codesys/http.ts +114 -72
  20. package/src/codesys/instrumentation.ts +30 -6
  21. package/src/codesys/numeric.ts +61 -0
  22. package/src/codesys/routines.ts +39 -9
  23. package/src/codesys/st-files.ts +73 -6
  24. package/src/codesys/telemetry.ts +70 -35
  25. package/src/frontend/control-flow.ts +752 -0
  26. package/src/frontend/coroutines.ts +171 -7
  27. package/src/frontend/elaborate/array-searches.ts +216 -0
  28. package/src/frontend/elaborate/assignments.ts +352 -0
  29. package/src/frontend/elaborate/calls.ts +467 -42
  30. package/src/frontend/elaborate/capture-protocol.ts +19 -0
  31. package/src/frontend/elaborate/capture.ts +80 -2
  32. package/src/frontend/elaborate/dialect.ts +116 -2
  33. package/src/frontend/elaborate/expressions.ts +927 -80
  34. package/src/frontend/elaborate/filtered-arrays.ts +607 -0
  35. package/src/frontend/elaborate/frame.ts +44 -1
  36. package/src/frontend/elaborate/generics.ts +137 -0
  37. package/src/frontend/elaborate/graph.ts +91 -0
  38. package/src/frontend/elaborate/index.ts +30 -5
  39. package/src/frontend/elaborate/initializers.ts +15 -1
  40. package/src/frontend/elaborate/iterators.ts +1677 -0
  41. package/src/frontend/elaborate/layouts.ts +41 -19
  42. package/src/frontend/elaborate/model.ts +64 -8
  43. package/src/frontend/elaborate/references.ts +749 -0
  44. package/src/frontend/elaborate/specialize.ts +459 -29
  45. package/src/frontend/elaborate/statements.ts +552 -133
  46. package/src/frontend/elaborate/storage.ts +151 -10
  47. package/src/frontend/elaborate/values.ts +72 -0
  48. package/src/frontend/elaborate/writes.ts +302 -1
  49. package/src/frontend/intrinsics.ts +32 -0
  50. package/src/frontend/module-graph.ts +2 -1
  51. package/src/frontend/positions.ts +12 -5
  52. package/src/frontend/typescript.ts +5 -1
  53. package/src/ir/core-model.ts +2 -6
  54. package/src/ir/fold.ts +30 -1
  55. package/src/ir/index.ts +5 -0
  56. package/src/ir/model.ts +53 -2
  57. package/src/ir/scalars.ts +126 -0
  58. package/src/runtime/.runtime-VaUSOu/server/router.ts +17 -0
  59. package/src/runtime/.runtime-VaUSOu/src/cell.ts +45 -0
  60. package/src/runtime/.runtime-VaUSOu/src/controller.ts +58 -0
  61. package/src/runtime/.runtime-VaUSOu/src/lane.ts +6 -0
  62. package/src/runtime/.runtime-VaUSOu/src/main.ts +11 -0
  63. package/src/runtime/.runtime-VaUSOu/src/services.ts +8 -0
  64. package/src/runtime/.runtime-VaUSOu/tsconfig.json +13 -0
  65. package/src/runtime/.runtime-VaUSOu/tsplc.config.ts +31 -0
  66. package/src/runtime/.runtime-ybIxYh/server/router.ts +17 -0
  67. package/src/runtime/.runtime-ybIxYh/src/cell.ts +45 -0
  68. package/src/runtime/.runtime-ybIxYh/src/controller.ts +58 -0
  69. package/src/runtime/.runtime-ybIxYh/src/lane.ts +6 -0
  70. package/src/runtime/.runtime-ybIxYh/src/main.ts +11 -0
  71. package/src/runtime/.runtime-ybIxYh/src/services.ts +8 -0
  72. package/src/runtime/.runtime-ybIxYh/tsconfig.json +13 -0
  73. package/src/runtime/.runtime-ybIxYh/tsplc.config.ts +31 -0
  74. package/src/runtime/async.ts +15 -12
  75. package/src/runtime/launch.ts +11 -4
  76. package/src/runtime/runtime.ts +26 -23
  77. package/src/runtime/telemetry.ts +14 -6
  78. package/src/runtime/transport.ts +172 -0
  79. package/stdlib/collections.ts +514 -0
  80. package/types/index.d.ts +18 -0
@@ -0,0 +1,395 @@
1
+ // packages/compiler/stdlib/collections.ts
2
+ var linearLimit = 32;
3
+
4
+ class BoundedQueue {
5
+ capacity;
6
+ options;
7
+ items;
8
+ first = 0;
9
+ count = 0;
10
+ overflowed = 0;
11
+ blank;
12
+ constructor(capacity, blank, options) {
13
+ this.capacity = capacity;
14
+ this.options = options;
15
+ this.items = Array.from({ length: capacity }, blank);
16
+ this.blank = blank();
17
+ }
18
+ item(slot) {
19
+ return this.items[slot % this.capacity] ?? this.blank;
20
+ }
21
+ get length() {
22
+ return this.count;
23
+ }
24
+ push(item) {
25
+ if (this.count === this.capacity) {
26
+ this.overflowed += 1;
27
+ if (this.options.onFull === "reject") {
28
+ return false;
29
+ }
30
+ this.first = (this.first + 1) % this.capacity;
31
+ this.count -= 1;
32
+ }
33
+ this.items[(this.first + this.count) % this.capacity] = item;
34
+ this.count += 1;
35
+ return true;
36
+ }
37
+ shift() {
38
+ if (this.count === 0) {
39
+ return;
40
+ }
41
+ const item = this.item(this.first);
42
+ this.first = (this.first + 1) % this.capacity;
43
+ this.count -= 1;
44
+ return item;
45
+ }
46
+ pop() {
47
+ if (this.count === 0) {
48
+ return;
49
+ }
50
+ this.count -= 1;
51
+ return this.item(this.first + this.count);
52
+ }
53
+ peek() {
54
+ return this.count === 0 ? undefined : this.item(this.first);
55
+ }
56
+ at(offset) {
57
+ return offset < this.count ? this.item(this.first + offset) : undefined;
58
+ }
59
+ update(offset, change) {
60
+ if (offset >= this.count) {
61
+ return false;
62
+ }
63
+ const slot = (this.first + offset) % this.capacity;
64
+ const item = structuredClone(this.items[slot % this.capacity] ?? this.blank);
65
+ change(item);
66
+ this.items[slot % this.capacity] = item;
67
+ return true;
68
+ }
69
+ clear() {
70
+ this.first = 0;
71
+ this.count = 0;
72
+ }
73
+ *values() {
74
+ for (let offset = 0;offset < this.capacity; offset++) {
75
+ if (offset >= this.count) {
76
+ return;
77
+ }
78
+ yield this.items[(this.first + offset) % this.capacity] ?? this.blank;
79
+ }
80
+ }
81
+ }
82
+
83
+ class BoundedList {
84
+ capacity;
85
+ options;
86
+ items;
87
+ count = 0;
88
+ overflowed = 0;
89
+ blank;
90
+ constructor(capacity, blank, options) {
91
+ this.capacity = capacity;
92
+ this.options = options;
93
+ this.items = Array.from({ length: capacity }, blank);
94
+ this.blank = blank();
95
+ }
96
+ item(slot) {
97
+ return this.items[slot % this.capacity] ?? this.blank;
98
+ }
99
+ get length() {
100
+ return this.count;
101
+ }
102
+ push(item) {
103
+ if (this.count === this.capacity) {
104
+ this.overflowed += 1;
105
+ if (this.options.onFull === "reject") {
106
+ return false;
107
+ }
108
+ this.removeAt(0);
109
+ }
110
+ this.items[this.count % this.capacity] = item;
111
+ this.count += 1;
112
+ return true;
113
+ }
114
+ at(index) {
115
+ return index < this.count ? this.item(index) : undefined;
116
+ }
117
+ update(index, change) {
118
+ if (index >= this.count) {
119
+ return false;
120
+ }
121
+ const item = structuredClone(this.items[index % this.capacity] ?? this.blank);
122
+ change(item);
123
+ this.items[index % this.capacity] = item;
124
+ return true;
125
+ }
126
+ removeAt(index) {
127
+ if (index >= this.count) {
128
+ return false;
129
+ }
130
+ for (let slot = 0;slot < this.capacity; slot++) {
131
+ if (slot >= index && slot + 1 < this.count) {
132
+ this.items[slot % this.capacity] = this.item(slot + 1);
133
+ }
134
+ }
135
+ this.count -= 1;
136
+ return true;
137
+ }
138
+ findIndex(test) {
139
+ for (let slot = 0;slot < this.capacity; slot++) {
140
+ if (slot < this.count && test(this.item(slot))) {
141
+ return slot;
142
+ }
143
+ }
144
+ return -1;
145
+ }
146
+ remove(test) {
147
+ for (let slot = 0;slot < this.capacity; slot++) {
148
+ if (slot < this.count && test(this.item(slot))) {
149
+ return this.removeAt(slot);
150
+ }
151
+ }
152
+ return false;
153
+ }
154
+ clear() {
155
+ this.count = 0;
156
+ }
157
+ *values() {
158
+ for (let slot = 0;slot < this.capacity; slot++) {
159
+ if (slot >= this.count) {
160
+ return;
161
+ }
162
+ yield this.items[slot % this.capacity] ?? this.blank;
163
+ }
164
+ }
165
+ }
166
+
167
+ class BoundedMap {
168
+ capacity;
169
+ options;
170
+ hashed;
171
+ slots;
172
+ slotKeys;
173
+ slotValues;
174
+ used;
175
+ ages;
176
+ added = 0;
177
+ count = 0;
178
+ blankKey;
179
+ blankValue;
180
+ overflowed = 0;
181
+ constructor(capacity, blankKey, blank, options) {
182
+ this.capacity = capacity;
183
+ this.options = options;
184
+ this.hashed = capacity > linearLimit && typeof blankKey === "number";
185
+ this.slots = this.hashed ? capacity * 2 : capacity;
186
+ this.slotKeys = Array.from({ length: this.slots }, () => blankKey);
187
+ this.slotValues = Array.from({ length: this.slots }, blank);
188
+ this.used = Array.from({ length: this.slots }, () => false);
189
+ this.ages = Array.from({ length: this.slots }, () => 0);
190
+ this.blankKey = blankKey;
191
+ this.blankValue = blank();
192
+ }
193
+ keyAt(slot) {
194
+ return this.slotKeys[slot % this.slots] ?? this.blankKey;
195
+ }
196
+ valueAt(slot) {
197
+ return this.slotValues[slot % this.slots] ?? this.blankValue;
198
+ }
199
+ usedAt(slot) {
200
+ return this.used[slot % this.slots] ?? false;
201
+ }
202
+ ageAt(slot) {
203
+ return this.ages[slot % this.slots] ?? 0;
204
+ }
205
+ get size() {
206
+ return this.count;
207
+ }
208
+ has(key) {
209
+ return this.slotOf(key) >= 0;
210
+ }
211
+ get(key) {
212
+ const found = this.slotOf(key);
213
+ if (found < 0) {
214
+ return;
215
+ }
216
+ return this.valueAt(found);
217
+ }
218
+ set(key, value) {
219
+ const found = this.slotOf(key);
220
+ if (found >= 0) {
221
+ const slot = found;
222
+ this.slotValues[slot % this.slots] = value;
223
+ return true;
224
+ }
225
+ if (this.count === this.capacity) {
226
+ this.overflowed += 1;
227
+ if (this.options.onFull === "reject") {
228
+ return false;
229
+ }
230
+ this.removeSlot(this.oldestSlot());
231
+ }
232
+ const slot = this.freeSlot(key);
233
+ this.slotKeys[slot % this.slots] = key;
234
+ this.slotValues[slot % this.slots] = value;
235
+ this.used[slot % this.slots] = true;
236
+ this.ages[slot % this.slots] = this.added;
237
+ this.added += 1;
238
+ this.count += 1;
239
+ return true;
240
+ }
241
+ update(key, change) {
242
+ const found = this.slotOf(key);
243
+ if (found < 0) {
244
+ return false;
245
+ }
246
+ const slot = found;
247
+ const entry = structuredClone(this.slotValues[slot % this.slots] ?? this.blankValue);
248
+ change(entry);
249
+ this.slotValues[slot % this.slots] = entry;
250
+ return true;
251
+ }
252
+ delete(key) {
253
+ const found = this.slotOf(key);
254
+ if (found < 0) {
255
+ return false;
256
+ }
257
+ this.removeSlot(found);
258
+ return true;
259
+ }
260
+ clear() {
261
+ for (let slot = 0;slot < this.slots; slot++) {
262
+ this.used[slot % this.slots] = false;
263
+ }
264
+ this.count = 0;
265
+ }
266
+ *keys() {
267
+ for (let slot = 0;slot < this.slots; slot++) {
268
+ if (this.usedAt(slot)) {
269
+ yield this.keyAt(slot);
270
+ }
271
+ }
272
+ }
273
+ *values() {
274
+ for (let slot = 0;slot < this.slots; slot++) {
275
+ if (this.usedAt(slot)) {
276
+ yield this.slotValues[slot % this.slots] ?? this.blankValue;
277
+ }
278
+ }
279
+ }
280
+ slotOf(key) {
281
+ if (!this.hashed) {
282
+ for (let slot = 0;slot < this.slots; slot++) {
283
+ if (this.usedAt(slot) && this.keyAt(slot) === key) {
284
+ return slot;
285
+ }
286
+ }
287
+ return -1;
288
+ }
289
+ const home = this.home(key);
290
+ for (let probe = 0;probe < this.slots; probe++) {
291
+ const slot = (home + probe) % this.slots;
292
+ if (!this.usedAt(slot)) {
293
+ return -1;
294
+ }
295
+ if (this.keyAt(slot) === key) {
296
+ return slot;
297
+ }
298
+ }
299
+ return -1;
300
+ }
301
+ freeSlot(key) {
302
+ const home = this.hashed ? this.home(key) : 0;
303
+ for (let probe = 0;probe < this.slots; probe++) {
304
+ const slot = (home + probe) % this.slots;
305
+ if (!this.usedAt(slot)) {
306
+ return slot;
307
+ }
308
+ }
309
+ return 0;
310
+ }
311
+ home(key) {
312
+ if (typeof key !== "number") {
313
+ return 0;
314
+ }
315
+ const remainder = key % this.slots;
316
+ return remainder < 0 ? remainder + this.slots : remainder;
317
+ }
318
+ oldestSlot() {
319
+ let oldest = -1;
320
+ for (let slot = 0;slot < this.slots; slot++) {
321
+ if (this.usedAt(slot) && (oldest < 0 || this.ageAt(slot) - this.ageAt(oldest) < 0)) {
322
+ oldest = slot;
323
+ }
324
+ }
325
+ return oldest;
326
+ }
327
+ removeSlot(found) {
328
+ if (found < 0) {
329
+ return;
330
+ }
331
+ const removed = found;
332
+ this.used[removed % this.slots] = false;
333
+ this.count -= 1;
334
+ if (!this.hashed) {
335
+ return;
336
+ }
337
+ let hole = removed % this.slots;
338
+ for (let probe = 1;probe < this.slots; probe++) {
339
+ const next = (removed + probe) % this.slots;
340
+ if (!this.usedAt(next)) {
341
+ return;
342
+ }
343
+ const home = this.home(this.keyAt(next));
344
+ const stays = hole <= next ? home > hole && home <= next : home > hole || home <= next;
345
+ if (!stays) {
346
+ this.slotKeys[hole % this.slots] = this.keyAt(next);
347
+ this.slotValues[hole % this.slots] = this.valueAt(next);
348
+ this.ages[hole % this.slots] = this.ageAt(next);
349
+ this.used[hole % this.slots] = true;
350
+ this.used[next % this.slots] = false;
351
+ hole = next;
352
+ }
353
+ }
354
+ }
355
+ }
356
+
357
+ class BoundedSet {
358
+ capacity;
359
+ members;
360
+ constructor(capacity, blankKey, options) {
361
+ this.capacity = capacity;
362
+ this.members = new BoundedMap(capacity, blankKey, () => false, options);
363
+ }
364
+ get size() {
365
+ return this.members.size;
366
+ }
367
+ get overflowed() {
368
+ return this.members.overflowed;
369
+ }
370
+ has(key) {
371
+ return this.members.has(key);
372
+ }
373
+ add(key) {
374
+ return this.members.set(key, true);
375
+ }
376
+ delete(key) {
377
+ return this.members.delete(key);
378
+ }
379
+ clear() {
380
+ this.members.clear();
381
+ }
382
+ *values() {
383
+ yield* this.members.keys();
384
+ }
385
+ }
386
+ export {
387
+ BoundedList,
388
+ BoundedMap,
389
+ BoundedQueue,
390
+ BoundedSet,
391
+ linearLimit
392
+ };
393
+
394
+ //# debugId=4FC9E2DB2410051864756E2164756E21
395
+ //# sourceMappingURL=collections.js.map
@@ -0,0 +1,10 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../stdlib/collections.ts"],
4
+ "sourcesContent": [
5
+ "// PLC implementation source: a plain TypeScript program's collections. Setup fixes each one's\n// capacity, so the PLC stores it in arrays allocated once, and every loop over it is bounded by\n// that capacity. The same classes run under Bun for setup, tests and simulation.\nimport type { UINT } from \"../types/index\";\n\n/** Bun's, at setup and in simulation; the PLC compiles it as the dialect's `copy`. */\ndeclare function structuredClone<T>(value: T): T;\n\n/** What a full collection does with one more item: refuse it, or make room by dropping the oldest. */\nexport type OnFull = \"reject\" | \"dropOldest\";\n\nexport interface BoundedOptions {\n\treadonly onFull: OnFull;\n}\n\n/** Above this capacity a map with number keys hashes them; at or below it, a scan of every slot is as quick. */\nexport const linearLimit = 32;\n\n/** A first-in, first-out queue of at most `capacity` items, in a ring. */\nexport class BoundedQueue<T> {\n\tprivate readonly items: T[];\n\tprivate first: UINT = 0;\n\tprivate count: UINT = 0;\n\t/** How many items the queue refused or dropped because it was full. */\n\toverflowed = 0;\n\n\t/** What an empty slot holds; reads fall back to it where TypeScript can't see the slot is filled. */\n\tprivate readonly blank: T;\n\n\tconstructor(\n\t\treadonly capacity: number,\n\t\tblank: () => T,\n\t\tprivate readonly options: BoundedOptions,\n\t) {\n\t\tthis.items = Array.from({ length: capacity }, blank);\n\t\tthis.blank = blank();\n\t}\n\n\tprivate item(slot: UINT): T {\n\t\treturn this.items[slot % this.capacity] ?? this.blank;\n\t}\n\n\tget length(): number {\n\t\treturn this.count;\n\t}\n\n\t/** Adds an item at the back; false when the queue is full and refuses it. */\n\tpush(item: T): boolean {\n\t\tif (this.count === this.capacity) {\n\t\t\tthis.overflowed += 1;\n\t\t\tif (this.options.onFull === \"reject\") {\n\t\t\t\treturn false;\n\t\t\t}\n\t\t\tthis.first = (this.first + 1) % this.capacity;\n\t\t\tthis.count -= 1;\n\t\t}\n\t\tthis.items[(this.first + this.count) % this.capacity] = item;\n\t\tthis.count += 1;\n\t\treturn true;\n\t}\n\n\t/** Takes the item at the front, or undefined when empty. */\n\tshift(): T | undefined {\n\t\tif (this.count === 0) {\n\t\t\treturn undefined;\n\t\t}\n\t\tconst item = this.item(this.first);\n\t\tthis.first = (this.first + 1) % this.capacity;\n\t\tthis.count -= 1;\n\t\treturn item;\n\t}\n\n\t/** Takes the item at the back, or undefined when empty. */\n\tpop(): T | undefined {\n\t\tif (this.count === 0) {\n\t\t\treturn undefined;\n\t\t}\n\t\tthis.count -= 1;\n\t\treturn this.item(this.first + this.count);\n\t}\n\n\t/** The item at the front, left in place. */\n\tpeek(): T | undefined {\n\t\treturn this.count === 0 ? undefined : this.item(this.first);\n\t}\n\n\t/** The item `offset` from the front, or undefined past the back. */\n\tat(offset: UINT): T | undefined {\n\t\treturn offset < this.count ? this.item(this.first + offset) : undefined;\n\t}\n\n\t/** Changes the item `offset` from the front in place; false past the back. */\n\tupdate(offset: UINT, change: (item: T) => void): boolean {\n\t\tif (offset >= this.count) {\n\t\t\treturn false;\n\t\t}\n\t\tconst slot: UINT = (this.first + offset) % this.capacity;\n\t\t// A copy, changed and stored back: an element at a runtime slot can't be lent out.\n\t\tconst item = structuredClone(\n\t\t\tthis.items[slot % this.capacity] ?? this.blank,\n\t\t);\n\t\tchange(item);\n\t\tthis.items[slot % this.capacity] = item;\n\t\treturn true;\n\t}\n\n\tclear(): void {\n\t\tthis.first = 0;\n\t\tthis.count = 0;\n\t}\n\n\t*values(): Generator<T> {\n\t\tfor (let offset: UINT = 0; offset < this.capacity; offset++) {\n\t\t\tif (offset >= this.count) {\n\t\t\t\treturn;\n\t\t\t}\n\t\t\t// The stored item itself, so a loop that changes it changes the queue's.\n\t\t\tyield this.items[(this.first + offset) % this.capacity] ?? this.blank;\n\t\t}\n\t}\n}\n\n/** A list of at most `capacity` items in order, which can be removed from anywhere. */\nexport class BoundedList<T> {\n\tprivate readonly items: T[];\n\tprivate count: UINT = 0;\n\t/** How many items the list refused or dropped because it was full. */\n\toverflowed = 0;\n\n\t/** What an empty slot holds; reads fall back to it where TypeScript can't see the slot is filled. */\n\tprivate readonly blank: T;\n\n\tconstructor(\n\t\treadonly capacity: number,\n\t\tblank: () => T,\n\t\tprivate readonly options: BoundedOptions,\n\t) {\n\t\tthis.items = Array.from({ length: capacity }, blank);\n\t\tthis.blank = blank();\n\t}\n\n\tprivate item(slot: UINT): T {\n\t\treturn this.items[slot % this.capacity] ?? this.blank;\n\t}\n\n\tget length(): number {\n\t\treturn this.count;\n\t}\n\n\t/** Adds an item at the end; false when the list is full and refuses it. */\n\tpush(item: T): boolean {\n\t\tif (this.count === this.capacity) {\n\t\t\tthis.overflowed += 1;\n\t\t\tif (this.options.onFull === \"reject\") {\n\t\t\t\treturn false;\n\t\t\t}\n\t\t\tthis.removeAt(0);\n\t\t}\n\t\tthis.items[this.count % this.capacity] = item;\n\t\tthis.count += 1;\n\t\treturn true;\n\t}\n\n\tat(index: UINT): T | undefined {\n\t\treturn index < this.count ? this.item(index) : undefined;\n\t}\n\n\t/** Changes the item at `index` in place; false past the end. */\n\tupdate(index: UINT, change: (item: T) => void): boolean {\n\t\tif (index >= this.count) {\n\t\t\treturn false;\n\t\t}\n\t\t// A copy, changed and stored back: an element at a runtime slot can't be lent out.\n\t\tconst item = structuredClone(\n\t\t\tthis.items[index % this.capacity] ?? this.blank,\n\t\t);\n\t\tchange(item);\n\t\tthis.items[index % this.capacity] = item;\n\t\treturn true;\n\t}\n\n\t/** Removes the item at `index`, moving the ones after it up; false past the end. */\n\tremoveAt(index: UINT): boolean {\n\t\tif (index >= this.count) {\n\t\t\treturn false;\n\t\t}\n\t\tfor (let slot: UINT = 0; slot < this.capacity; slot++) {\n\t\t\tif (slot >= index && slot + 1 < this.count) {\n\t\t\t\tthis.items[slot % this.capacity] = this.item(slot + 1);\n\t\t\t}\n\t\t}\n\t\tthis.count -= 1;\n\t\treturn true;\n\t}\n\n\t/** The index of the first item that passes, or -1. */\n\tfindIndex(test: (item: T) => boolean): number {\n\t\tfor (let slot: UINT = 0; slot < this.capacity; slot++) {\n\t\t\tif (slot < this.count && test(this.item(slot))) {\n\t\t\t\treturn slot;\n\t\t\t}\n\t\t}\n\t\treturn -1;\n\t}\n\n\t/** Removes the first item that passes; false when none does. */\n\tremove(test: (item: T) => boolean): boolean {\n\t\tfor (let slot: UINT = 0; slot < this.capacity; slot++) {\n\t\t\tif (slot < this.count && test(this.item(slot))) {\n\t\t\t\treturn this.removeAt(slot);\n\t\t\t}\n\t\t}\n\t\treturn false;\n\t}\n\n\tclear(): void {\n\t\tthis.count = 0;\n\t}\n\n\t*values(): Generator<T> {\n\t\tfor (let slot: UINT = 0; slot < this.capacity; slot++) {\n\t\t\tif (slot >= this.count) {\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tyield this.items[slot % this.capacity] ?? this.blank;\n\t\t}\n\t}\n}\n\n/**\n * A map of at most `capacity` entries. Up to `linearLimit` entries, or with string keys, a lookup\n * scans every slot; a larger map with number keys hashes them into twice as many slots, probing\n * from each key's home slot and closing the gap a deletion leaves.\n */\nexport class BoundedMap<K extends number | string, V> {\n\tprivate readonly hashed: boolean;\n\tprivate readonly slots: number;\n\tprivate readonly slotKeys: K[];\n\tprivate readonly slotValues: V[];\n\tprivate readonly used: boolean[];\n\t/** When each slot's entry was added, for dropping the oldest. */\n\tprivate readonly ages: number[];\n\tprivate added = 0;\n\tprivate count = 0;\n\t/** What an empty slot holds; reads fall back to them where TypeScript can't see a slot is filled. */\n\tprivate readonly blankKey: K;\n\tprivate readonly blankValue: V;\n\t/** How many entries the map refused or dropped because it was full. */\n\toverflowed = 0;\n\n\tconstructor(\n\t\treadonly capacity: number,\n\t\tblankKey: K,\n\t\tblank: () => V,\n\t\tprivate readonly options: BoundedOptions,\n\t) {\n\t\tthis.hashed = capacity > linearLimit && typeof blankKey === \"number\";\n\t\tthis.slots = this.hashed ? capacity * 2 : capacity;\n\t\tthis.slotKeys = Array.from({ length: this.slots }, () => blankKey);\n\t\tthis.slotValues = Array.from({ length: this.slots }, blank);\n\t\tthis.used = Array.from({ length: this.slots }, () => false);\n\t\tthis.ages = Array.from({ length: this.slots }, () => 0);\n\t\tthis.blankKey = blankKey;\n\t\tthis.blankValue = blank();\n\t}\n\n\tprivate keyAt(slot: UINT): K {\n\t\treturn this.slotKeys[slot % this.slots] ?? this.blankKey;\n\t}\n\n\tprivate valueAt(slot: UINT): V {\n\t\treturn this.slotValues[slot % this.slots] ?? this.blankValue;\n\t}\n\n\tprivate usedAt(slot: UINT): boolean {\n\t\treturn this.used[slot % this.slots] ?? false;\n\t}\n\n\tprivate ageAt(slot: UINT): number {\n\t\treturn this.ages[slot % this.slots] ?? 0;\n\t}\n\n\tget size(): number {\n\t\treturn this.count;\n\t}\n\n\thas(key: K): boolean {\n\t\treturn this.slotOf(key) >= 0;\n\t}\n\n\t/** The entry for `key`, as a copy, or undefined. Change a stored entry with `update`. */\n\tget(key: K): V | undefined {\n\t\tconst found = this.slotOf(key);\n\t\tif (found < 0) {\n\t\t\treturn undefined;\n\t\t}\n\t\treturn this.valueAt(found);\n\t}\n\n\t/** Stores the entry for `key`; false when the map is full and refuses a new key. */\n\tset(key: K, value: V): boolean {\n\t\tconst found = this.slotOf(key);\n\t\tif (found >= 0) {\n\t\t\tconst slot: UINT = found;\n\t\t\tthis.slotValues[slot % this.slots] = value;\n\t\t\treturn true;\n\t\t}\n\t\tif (this.count === this.capacity) {\n\t\t\tthis.overflowed += 1;\n\t\t\tif (this.options.onFull === \"reject\") {\n\t\t\t\treturn false;\n\t\t\t}\n\t\t\tthis.removeSlot(this.oldestSlot());\n\t\t}\n\t\tconst slot = this.freeSlot(key);\n\t\tthis.slotKeys[slot % this.slots] = key;\n\t\tthis.slotValues[slot % this.slots] = value;\n\t\tthis.used[slot % this.slots] = true;\n\t\tthis.ages[slot % this.slots] = this.added;\n\t\tthis.added += 1;\n\t\tthis.count += 1;\n\t\treturn true;\n\t}\n\n\t/** Changes the stored entry for `key` in place; false when there is none. */\n\tupdate(key: K, change: (entry: V) => void): boolean {\n\t\tconst found = this.slotOf(key);\n\t\tif (found < 0) {\n\t\t\treturn false;\n\t\t}\n\t\tconst slot: UINT = found;\n\t\t// A copy, changed and stored back: an element at a runtime slot can't be lent out.\n\t\tconst entry = structuredClone(\n\t\t\tthis.slotValues[slot % this.slots] ?? this.blankValue,\n\t\t);\n\t\tchange(entry);\n\t\tthis.slotValues[slot % this.slots] = entry;\n\t\treturn true;\n\t}\n\n\tdelete(key: K): boolean {\n\t\tconst found = this.slotOf(key);\n\t\tif (found < 0) {\n\t\t\treturn false;\n\t\t}\n\t\tthis.removeSlot(found);\n\t\treturn true;\n\t}\n\n\tclear(): void {\n\t\tfor (let slot: UINT = 0; slot < this.slots; slot++) {\n\t\t\tthis.used[slot % this.slots] = false;\n\t\t}\n\t\tthis.count = 0;\n\t}\n\n\t*keys(): Generator<K> {\n\t\tfor (let slot: UINT = 0; slot < this.slots; slot++) {\n\t\t\tif (this.usedAt(slot)) {\n\t\t\t\tyield this.keyAt(slot);\n\t\t\t}\n\t\t}\n\t}\n\n\t*values(): Generator<V> {\n\t\tfor (let slot: UINT = 0; slot < this.slots; slot++) {\n\t\t\tif (this.usedAt(slot)) {\n\t\t\t\tyield this.slotValues[slot % this.slots] ?? this.blankValue;\n\t\t\t}\n\t\t}\n\t}\n\n\t/** The slot holding `key`, or -1. */\n\tprivate slotOf(key: K): number {\n\t\tif (!this.hashed) {\n\t\t\tfor (let slot: UINT = 0; slot < this.slots; slot++) {\n\t\t\t\tif (this.usedAt(slot) && this.keyAt(slot) === key) {\n\t\t\t\t\treturn slot;\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn -1;\n\t\t}\n\t\tconst home: UINT = this.home(key);\n\t\tfor (let probe: UINT = 0; probe < this.slots; probe++) {\n\t\t\tconst slot: UINT = (home + probe) % this.slots;\n\t\t\tif (!this.usedAt(slot)) {\n\t\t\t\treturn -1;\n\t\t\t}\n\t\t\tif (this.keyAt(slot) === key) {\n\t\t\t\treturn slot;\n\t\t\t}\n\t\t}\n\t\treturn -1;\n\t}\n\n\t/** The slot a new key goes in: the first free one, from its home slot when hashed. */\n\tprivate freeSlot(key: K): UINT {\n\t\tconst home: UINT = this.hashed ? this.home(key) : 0;\n\t\tfor (let probe: UINT = 0; probe < this.slots; probe++) {\n\t\t\tconst slot: UINT = (home + probe) % this.slots;\n\t\t\tif (!this.usedAt(slot)) {\n\t\t\t\treturn slot;\n\t\t\t}\n\t\t}\n\t\treturn 0;\n\t}\n\n\t/** The slot a number key hashes to. */\n\tprivate home(key: K): number {\n\t\tif (typeof key !== \"number\") {\n\t\t\treturn 0;\n\t\t}\n\t\tconst remainder = key % this.slots;\n\t\treturn remainder < 0 ? remainder + this.slots : remainder;\n\t}\n\n\t/** The slot whose entry was added first. */\n\tprivate oldestSlot(): number {\n\t\tlet oldest = -1;\n\t\tfor (let slot: UINT = 0; slot < this.slots; slot++) {\n\t\t\tif (\n\t\t\t\tthis.usedAt(slot) &&\n\t\t\t\t(oldest < 0 || this.ageAt(slot) - this.ageAt(oldest) < 0)\n\t\t\t) {\n\t\t\t\toldest = slot;\n\t\t\t}\n\t\t}\n\t\treturn oldest;\n\t}\n\n\t/**\n\t * Empties a slot. A hashed map then moves back each entry after it that could sit earlier, so no\n\t * lookup stops at the gap before reaching its key.\n\t */\n\tprivate removeSlot(found: number): void {\n\t\tif (found < 0) {\n\t\t\treturn;\n\t\t}\n\t\tconst removed: UINT = found;\n\t\tthis.used[removed % this.slots] = false;\n\t\tthis.count -= 1;\n\t\tif (!this.hashed) {\n\t\t\treturn;\n\t\t}\n\t\tlet hole: UINT = removed % this.slots;\n\t\tfor (let probe: UINT = 1; probe < this.slots; probe++) {\n\t\t\tconst next: UINT = (removed + probe) % this.slots;\n\t\t\tif (!this.usedAt(next)) {\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tconst home: UINT = this.home(this.keyAt(next));\n\t\t\tconst stays =\n\t\t\t\thole <= next\n\t\t\t\t\t? home > hole && home <= next\n\t\t\t\t\t: home > hole || home <= next;\n\t\t\tif (!stays) {\n\t\t\t\tthis.slotKeys[hole % this.slots] = this.keyAt(next);\n\t\t\t\tthis.slotValues[hole % this.slots] = this.valueAt(next);\n\t\t\t\tthis.ages[hole % this.slots] = this.ageAt(next);\n\t\t\t\tthis.used[hole % this.slots] = true;\n\t\t\t\tthis.used[next % this.slots] = false;\n\t\t\t\thole = next;\n\t\t\t}\n\t\t}\n\t}\n}\n\n/** A set of at most `capacity` keys. */\nexport class BoundedSet<K extends number | string> {\n\tprivate readonly members: BoundedMap<K, boolean>;\n\n\tconstructor(\n\t\treadonly capacity: number,\n\t\tblankKey: K,\n\t\toptions: BoundedOptions,\n\t) {\n\t\tthis.members = new BoundedMap<K, boolean>(\n\t\t\tcapacity,\n\t\t\tblankKey,\n\t\t\t() => false,\n\t\t\toptions,\n\t\t);\n\t}\n\n\tget size(): number {\n\t\treturn this.members.size;\n\t}\n\n\t/** How many keys the set refused or dropped because it was full. */\n\tget overflowed(): number {\n\t\treturn this.members.overflowed;\n\t}\n\n\thas(key: K): boolean {\n\t\treturn this.members.has(key);\n\t}\n\n\t/** Adds the key; false when the set is full and refuses it. */\n\tadd(key: K): boolean {\n\t\treturn this.members.set(key, true);\n\t}\n\n\tdelete(key: K): boolean {\n\t\treturn this.members.delete(key);\n\t}\n\n\tclear(): void {\n\t\tthis.members.clear();\n\t}\n\n\t*values(): Generator<K> {\n\t\tyield* this.members.keys();\n\t}\n}\n"
6
+ ],
7
+ "mappings": ";AAgBO,IAAM,cAAc;AAAA;AAGpB,MAAM,aAAgB;AAA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AW;AAAA,IAClB,OAAO,KAAK,QAAQ;AAAA;AAAA,MAIjB,UAAU,GAAW;AAAA,IACxB,OAAO,KAAK,QAAQ;AAAA;AAAA,EAGrB,GAAG,CAAC,KAAiB;AAAA,IACpB,OAAO,KAAK,QAAQ,IAAI,GAAG;AAAA;AAAA,EAI5B,GAAG,CAAC,KAAiB;AAAA,IACpB,OAAO,KAAK,QAAQ,IAAI,KAAK,IAAI;AAAA;AAAA,EAGlC,MAAM,CAAC,KAAiB;AAAA,IACvB,OAAO,KAAK,QAAQ,OAAO,GAAG;AAAA;AAAA,EAG/B,KAAK,GAAS;AAAA,IACb,KAAK,QAAQ,MAAM;AAAA;AAAA,GAGnB,MAAM,GAAiB;AAAA,IACvB,OAAO,KAAK,QAAQ,KAAK;AAAA;AAE3B;",
8
+ "debugId": "4FC9E2DB2410051864756E2164756E21",
9
+ "names": []
10
+ }
@@ -5,9 +5,15 @@ var {plugin } = globalThis.Bun;
5
5
 
6
6
  // packages/compiler/src/frontend/elaborate/capture-protocol.ts
7
7
  import { z } from "zod";
8
+ var closureSiteSchema = z.strictObject({
9
+ start: z.number().int().nonnegative(),
10
+ end: z.number().int().nonnegative(),
11
+ captures: z.array(z.string())
12
+ });
8
13
  var captureRequestSchema = z.strictObject({
9
14
  entry: z.string().nonempty(),
10
15
  files: z.array(z.string().nonempty()),
16
+ closures: z.record(z.string(), z.array(closureSiteSchema)),
11
17
  bindings: z.record(z.string(), z.strictObject({
12
18
  values: z.array(z.string()),
13
19
  classes: z.array(z.string()),
@@ -49,7 +55,13 @@ var capturedNodeSchema = z.discriminatedUnion("kind", [
49
55
  file: z.string(),
50
56
  name: z.string()
51
57
  }),
52
- z.strictObject({ kind: z.literal("service"), path: z.string() })
58
+ z.strictObject({ kind: z.literal("service"), path: z.string() }),
59
+ z.strictObject({
60
+ kind: z.literal("closure"),
61
+ file: z.string(),
62
+ start: z.number().int().nonnegative(),
63
+ captures: z.array(z.strictObject({ name: z.string(), value: capturedValueSchema }))
64
+ })
53
65
  ]);
54
66
  var capturedGraphSchema = z.strictObject({
55
67
  nodes: z.array(capturedNodeSchema),
@@ -103,7 +115,7 @@ plugin({
103
115
  build.onLoad({
104
116
  filter: new RegExp(`^(${[...runtime].map((file) => file.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")).join("|")})$`)
105
117
  }, ({ path }) => ({
106
- contents: withBindingRegistry(readFileSync(path, "utf8"), request.bindings[real.get(path)], exportName),
118
+ contents: withBindingRegistry(withClosureNotes(readFileSync(path, "utf8"), real.get(path), request.closures[real.get(path)] ?? []), request.bindings[real.get(path)], exportName),
107
119
  loader: "ts"
108
120
  }));
109
121
  build.module("@tsplc/compiler/clock", () => ({
@@ -120,6 +132,19 @@ plugin({
120
132
  contents: "export const retain = (value) => globalThis.__tsplcRetain(value);",
121
133
  loader: "js"
122
134
  }));
135
+ build.module("@tsplc/compiler/types", () => ({
136
+ contents: `const unsupported = (name) => () => { throw new Error(name + " cannot run during PLC setup."); };
137
+ export const ms = (value) => value, seconds = (value) => value * 1000, minutes = (value) => value * 60000;
138
+ export const exact = unsupported("exact"), wrap = unsupported("wrap"), saturate = unsupported("saturate");
139
+ export const wrapAdd = unsupported("wrapAdd"), wrapSubtract = unsupported("wrapSubtract"), wrapMultiply = unsupported("wrapMultiply");
140
+ export const saturatingAdd = unsupported("saturatingAdd"), saturatingSubtract = unsupported("saturatingSubtract"), saturatingMultiply = unsupported("saturatingMultiply");
141
+ export const div = unsupported("div"), defaultValue = unsupported("defaultValue"), copy = unsupported("copy"), entries = unsupported("entries");
142
+ export const ref = unsupported("ref"), retain = unsupported("retain"), timeToMilliseconds = unsupported("timeToMilliseconds");
143
+ export const date = unsupported("date"), timeOfDay = unsupported("timeOfDay"), dateAndTime = unsupported("dateAndTime");
144
+ export const sqrt = Math.sqrt, ln = Math.log, log10 = Math.log10, exp = Math.exp, sin = Math.sin, cos = Math.cos, tan = Math.tan;
145
+ export const asin = Math.asin, acos = Math.acos, atan = Math.atan, trunc = Math.trunc, expt = Math.pow;`,
146
+ loader: "js"
147
+ }));
123
148
  build.module("@tsplc/compiler/async", () => ({
124
149
  contents: `export const createServiceClient = () => globalThis.__tsplcService([]);
125
150
  const unsupported = (name) => () => { throw new Error(name + " cannot run during PLC setup."); };
@@ -130,6 +155,27 @@ plugin({
130
155
  }
131
156
  });
132
157
  globalThis.__tsplcService = service;
158
+ var closures = new Map;
159
+ globalThis.__tsplcClosure = (file, start, names, made, captured) => {
160
+ closures.set(made, { file, start, names, captured });
161
+ return made;
162
+ };
163
+ var withClosureNotes = (text, file, sites) => {
164
+ const inserts = sites.flatMap((site) => [
165
+ {
166
+ at: site.start,
167
+ order: 1,
168
+ text: `globalThis.__tsplcClosure(${JSON.stringify(file)}, ${site.start}, ${JSON.stringify(site.captures)}, (`
169
+ },
170
+ {
171
+ at: site.end,
172
+ order: 0,
173
+ text: `), () => [${site.captures.join(", ")}])`
174
+ }
175
+ ]);
176
+ inserts.sort((left, right) => right.at - left.at || left.order - right.order);
177
+ return inserts.reduce((result, insert) => result.slice(0, insert.at) + insert.text + result.slice(insert.at), text);
178
+ };
133
179
  var retainedObjects = new Set;
134
180
  globalThis.__tsplcRetain = (value) => {
135
181
  if (typeof value === "object" && value !== null) {
@@ -162,7 +208,7 @@ var capture = (value, where) => {
162
208
  return { kind: "undefined" };
163
209
  }
164
210
  if (value === null) {
165
- throw new Error(`${where}: null has no PLC value; use undefined.`);
211
+ return { kind: "undefined" };
166
212
  }
167
213
  if (typeof value === "number") {
168
214
  if (!Number.isFinite(value)) {
@@ -192,10 +238,22 @@ var capture = (value, where) => {
192
238
  if (typeof value === "function") {
193
239
  const path = value[servicePath];
194
240
  const named = functions.get(value) ?? classes.get(value);
241
+ const closure = closures.get(value);
195
242
  if (path !== undefined) {
196
243
  nodes[id] = { kind: "service", path };
197
244
  } else if (named) {
198
245
  nodes[id] = { kind: "function", ...named };
246
+ } else if (closure) {
247
+ const values = closure.captured();
248
+ nodes[id] = {
249
+ kind: "closure",
250
+ file: closure.file,
251
+ start: closure.start,
252
+ captures: closure.names.map((name, index) => ({
253
+ name,
254
+ value: capture(values[index], `${where} (captured ${name})`)
255
+ }))
256
+ };
199
257
  } else {
200
258
  throw new Error(`${where}: setup stored a function that is not a module-level function, so the PLC can't call it.`);
201
259
  }
@@ -242,5 +300,5 @@ graph.retained = [...retainedObjects].flatMap((value) => {
242
300
  });
243
301
  writeFileSync(process.argv[3], JSON.stringify(graph));
244
302
 
245
- //# debugId=CB159B9CC2D8061E64756E2164756E21
303
+ //# debugId=8DB148593AA2AB7B64756E2164756E21
246
304
  //# sourceMappingURL=elaborate-capture.js.map