@mica-js/simulator 2.1.0 → 2.1.1
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 +147 -0
- package/dist/index.js +2345 -781
- package/dist/index.js.map +1 -1
- package/dist/sandbox.js +2 -2
- package/package.json +8 -4
package/dist/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { a as copyBytePayload, c as samePayload, i as simulationRuntimeTopicService, l as targetKey, n as simulationRuntimeEventTransfer, o as copyPayload, r as simulationRuntimeService, s as payloadMatches } from "./rpc-D-9O3TZf.js";
|
|
2
2
|
import { Emitter } from "@mica-js/core/lifecycle";
|
|
3
3
|
import { BuildError, createJitBlockBuilder, validateBoundBlockIR, validateDeviceIR } from "@mica-js/device-model";
|
|
4
|
-
import { compileBody, compileComputation, compileDirectJit } from "@mica-js/compiler";
|
|
4
|
+
import { RESUMABLE_RECEIVE, checkBoolean, compileBody, compileComputation, compileDirectJit, compileResumableBody, createBytes, createEnum, createRecord, equalValues, nonNull, readIndex } from "@mica-js/compiler";
|
|
5
5
|
import { addTime, timePs } from "@mica-js/core";
|
|
6
6
|
import { assetSrcError } from "@mica-js/document";
|
|
7
7
|
import { deriveSchematicTopology, deviceMetadataSymbol, isAssetReference, parseDeviceMetadata, parseDeviceReference, parseSchematic, symbolRole } from "@mica-js/document/schematic";
|
|
@@ -86,6 +86,10 @@ function prepareBlock(component) {
|
|
|
86
86
|
key: component.nodes.join(",")
|
|
87
87
|
};
|
|
88
88
|
}
|
|
89
|
+
/** Ordered endpoint list per pin; multiplicity stays visible without hashing. */
|
|
90
|
+
function describeEdges(edges) {
|
|
91
|
+
return edges.map((edge) => edge.to).join(",");
|
|
92
|
+
}
|
|
89
93
|
function prepareTopology(nodes, pinEdges) {
|
|
90
94
|
const topology = new DisjointSet(nodes.length);
|
|
91
95
|
for (const edges of pinEdges) for (const edge of edges) topology.join(edge.from, edge.to);
|
|
@@ -106,8 +110,26 @@ function prepareTopology(nodes, pinEdges) {
|
|
|
106
110
|
let floatingComponents = 0;
|
|
107
111
|
const blocks = [];
|
|
108
112
|
const edgeBlocks = [];
|
|
113
|
+
const nodeComponents = new Array(nodes.length);
|
|
114
|
+
const nodeUniform = new Array(nodes.length);
|
|
115
|
+
let componentIndex = 0;
|
|
109
116
|
for (const component of groups.values()) {
|
|
110
|
-
|
|
117
|
+
let reference;
|
|
118
|
+
let floating = true;
|
|
119
|
+
let uniform = 1;
|
|
120
|
+
for (const node of component.nodes) {
|
|
121
|
+
const potential = nodes[node].reference;
|
|
122
|
+
if (potential === void 0) continue;
|
|
123
|
+
floating = false;
|
|
124
|
+
if (reference === void 0) reference = potential;
|
|
125
|
+
else if (potential !== reference) uniform = 0;
|
|
126
|
+
}
|
|
127
|
+
if (floating) floatingComponents++;
|
|
128
|
+
for (const node of component.nodes) {
|
|
129
|
+
nodeComponents[node] = componentIndex;
|
|
130
|
+
nodeUniform[node] = uniform;
|
|
131
|
+
}
|
|
132
|
+
componentIndex++;
|
|
111
133
|
for (const block of solveBlocks(component, nodes)) {
|
|
112
134
|
const index = blocks.length;
|
|
113
135
|
blocks.push(prepareBlock(block));
|
|
@@ -118,7 +140,9 @@ function prepareTopology(nodes, pinEdges) {
|
|
|
118
140
|
blocks,
|
|
119
141
|
edgeBlocks,
|
|
120
142
|
components: groups.size,
|
|
121
|
-
floatingComponents
|
|
143
|
+
floatingComponents,
|
|
144
|
+
nodeComponents,
|
|
145
|
+
nodeUniform
|
|
122
146
|
};
|
|
123
147
|
}
|
|
124
148
|
var cleanZero = (value) => value === 0 ? 0 : value;
|
|
@@ -320,6 +344,10 @@ function substitute(factorization, right, fail) {
|
|
|
320
344
|
}
|
|
321
345
|
return solution;
|
|
322
346
|
}
|
|
347
|
+
/** Changed pins publish in ascending board slot order; slots are already that order. */
|
|
348
|
+
function ascendingSlot(first, second) {
|
|
349
|
+
return first - second;
|
|
350
|
+
}
|
|
323
351
|
function sumPair(first, second) {
|
|
324
352
|
first = 0 + first;
|
|
325
353
|
const total = first + second;
|
|
@@ -375,21 +403,29 @@ function createElectricalNetwork(inputPins, inputNets) {
|
|
|
375
403
|
voltage: net.voltage
|
|
376
404
|
}));
|
|
377
405
|
for (const pin of pins) if (pin.low === void 0 !== (pin.high === void 0) || pin.low !== void 0 && (!Number.isFinite(pin.low) || !Number.isFinite(pin.high) || !(pin.low < pin.high))) throw new ElectricalError("INVALID_ELECTRICAL_PARAMETER", "Pin thresholds must both be omitted or finite with low < high.", pinLocation(pin));
|
|
378
|
-
const requirePin = (pin,
|
|
406
|
+
const requirePin = (pin, network) => {
|
|
379
407
|
const slot = pinSlots.get(pin);
|
|
380
|
-
if (slot === void 0) throw new ElectricalError("INVALID_ELECTRICAL_PARAMETER", "Electrical endpoint is not part of this board.",
|
|
408
|
+
if (slot === void 0) throw new ElectricalError("INVALID_ELECTRICAL_PARAMETER", "Electrical endpoint is not part of this board.", network === void 0 ? pinLocation(pin) : { network });
|
|
381
409
|
return slot;
|
|
382
410
|
};
|
|
411
|
+
const requireEndpoint = (pin, owner) => {
|
|
412
|
+
const slot = pinSlots.get(pin);
|
|
413
|
+
if (slot === void 0) throw new ElectricalError("INVALID_ELECTRICAL_PARAMETER", "Electrical endpoint is not part of this board.", pinLocation(owner));
|
|
414
|
+
return slot;
|
|
415
|
+
};
|
|
416
|
+
const validResistance = (resistance) => Number.isFinite(resistance) && resistance >= .001 && resistance <= 1e9;
|
|
383
417
|
for (const net of nets) {
|
|
384
418
|
if (net.voltage !== void 0 && !Number.isFinite(net.voltage)) throw new ElectricalError("INVALID_ELECTRICAL_PARAMETER", "Reference must be finite.", { network: net.id });
|
|
385
419
|
let first;
|
|
386
420
|
for (const pin of net.pins) {
|
|
387
|
-
const slot = requirePin(pin,
|
|
421
|
+
const slot = requirePin(pin, net.id);
|
|
388
422
|
if (first === void 0) first = slot;
|
|
389
423
|
else assembly.join(first, slot);
|
|
390
424
|
}
|
|
391
425
|
}
|
|
392
426
|
const nodes = [];
|
|
427
|
+
/** Pin slots per node in ascending board order, so samples publish without lookups. */
|
|
428
|
+
const nodeSlots = [];
|
|
393
429
|
const rootNodes = /* @__PURE__ */ new Map();
|
|
394
430
|
const pinNodes = pins.map((pin, index) => {
|
|
395
431
|
const root = assembly.find(index);
|
|
@@ -402,13 +438,15 @@ function createElectricalNetwork(inputPins, inputNets) {
|
|
|
402
438
|
networks: [],
|
|
403
439
|
reference: void 0
|
|
404
440
|
});
|
|
441
|
+
nodeSlots.push([]);
|
|
405
442
|
}
|
|
406
443
|
nodes[node].pins.push(pin);
|
|
444
|
+
nodeSlots[node].push(index);
|
|
407
445
|
return node;
|
|
408
446
|
});
|
|
409
447
|
for (const net of nets) {
|
|
410
448
|
if (net.pins.length === 0) continue;
|
|
411
|
-
const node = nodes[pinNodes[requirePin(net.pins[0],
|
|
449
|
+
const node = nodes[pinNodes[requirePin(net.pins[0], net.id)]];
|
|
412
450
|
node.networks.push(net.id);
|
|
413
451
|
if (net.voltage === void 0) continue;
|
|
414
452
|
const reference = cleanZero(net.voltage);
|
|
@@ -451,243 +489,623 @@ function createElectricalNetwork(inputPins, inputNets) {
|
|
|
451
489
|
const intents = new Array(pins.length);
|
|
452
490
|
const pinEdges = Array.from({ length: pins.length }, () => []);
|
|
453
491
|
const unchangedPins = Object.freeze([]);
|
|
492
|
+
/** Undirected incidence count per node, used to prove a node has no other branches. */
|
|
493
|
+
const nodeDegree = new Int32Array(nodes.length);
|
|
494
|
+
const updatedSlots = [];
|
|
495
|
+
const updatedIntents = [];
|
|
496
|
+
const updatedEdges = [];
|
|
497
|
+
const replacedEdges = [];
|
|
498
|
+
const pendingPins = [];
|
|
499
|
+
const pendingSamples = [];
|
|
500
|
+
const seenSlots = new Int32Array(pins.length);
|
|
501
|
+
let seenStamp = 0;
|
|
502
|
+
let preparedStale = false;
|
|
503
|
+
let descriptorsStale = false;
|
|
504
|
+
/** Component arrays are copied before a local update mutates them, because admitted
|
|
505
|
+
* topology snapshots keep referencing the arrays they were created with. */
|
|
506
|
+
let componentArraysOwned = false;
|
|
507
|
+
const syncDegrees = (previous, next) => {
|
|
508
|
+
for (const edge of previous) if (next.indexOf(edge) === -1) {
|
|
509
|
+
nodeDegree[edge.from] = nodeDegree[edge.from] - 1;
|
|
510
|
+
nodeDegree[edge.to] = nodeDegree[edge.to] - 1;
|
|
511
|
+
}
|
|
512
|
+
for (const edge of next) if (previous.indexOf(edge) === -1) {
|
|
513
|
+
nodeDegree[edge.from] = nodeDegree[edge.from] + 1;
|
|
514
|
+
nodeDegree[edge.to] = nodeDegree[edge.to] + 1;
|
|
515
|
+
}
|
|
516
|
+
};
|
|
454
517
|
const statistics = {
|
|
455
518
|
solves: 0,
|
|
456
519
|
components: 0,
|
|
457
520
|
floatingComponents: 0,
|
|
458
521
|
factorizations: 0,
|
|
459
522
|
factorizationReuses: 0,
|
|
460
|
-
cacheHits: 0
|
|
523
|
+
cacheHits: 0,
|
|
524
|
+
leafPlans: 0,
|
|
525
|
+
leafPropagations: 0
|
|
526
|
+
};
|
|
527
|
+
const emptySlots = Object.freeze([]);
|
|
528
|
+
/** Dirty slots supplied by the caller, deduplicated in place without an iterator per pin. */
|
|
529
|
+
const inputSlots = [];
|
|
530
|
+
/** Changed pin slots of the last solve in ascending board order. */
|
|
531
|
+
const changedSlots = [];
|
|
532
|
+
/** Admitted leaf shapes per pin slot; a pin normally alternates between two references. */
|
|
533
|
+
const leafPlans = new Array(pins.length);
|
|
534
|
+
const leafSlots = [];
|
|
535
|
+
const leafIntents = [];
|
|
536
|
+
const leafEdges = [];
|
|
537
|
+
const leafTargets = [];
|
|
538
|
+
/** Matched admitted shape per batch position, so publishing reuses its bound payload. */
|
|
539
|
+
const leafPayloads = [];
|
|
540
|
+
/** Shapes proven by the current solve; they are admitted only after it is accepted. */
|
|
541
|
+
const pendingAdmissions = [];
|
|
542
|
+
/** Canonical immutable samples; published samples never change once created. */
|
|
543
|
+
const referenceSamples = /* @__PURE__ */ new Map();
|
|
544
|
+
const nextStamp = () => {
|
|
545
|
+
if (++seenStamp === 2147483647) {
|
|
546
|
+
seenSlots.fill(0);
|
|
547
|
+
seenStamp = 1;
|
|
548
|
+
}
|
|
549
|
+
return seenStamp;
|
|
550
|
+
};
|
|
551
|
+
const sampleFor = (voltage, level) => {
|
|
552
|
+
let entry = referenceSamples.get(voltage);
|
|
553
|
+
if (entry === void 0) {
|
|
554
|
+
if (referenceSamples.size >= 256) referenceSamples.clear();
|
|
555
|
+
entry = {};
|
|
556
|
+
referenceSamples.set(voltage, entry);
|
|
557
|
+
}
|
|
558
|
+
let sample = entry[level];
|
|
559
|
+
if (sample === void 0) {
|
|
560
|
+
sample = Object.freeze({
|
|
561
|
+
voltage: cleanZero(voltage),
|
|
562
|
+
level,
|
|
563
|
+
status: "valid"
|
|
564
|
+
});
|
|
565
|
+
entry[level] = sample;
|
|
566
|
+
}
|
|
567
|
+
return sample;
|
|
568
|
+
};
|
|
569
|
+
/**
|
|
570
|
+
* Records `(pin, target, resistance)` once the general path proved the shape: one branch
|
|
571
|
+
* from a leaf node straight onto a fixed reference. Replays are only allowed while the
|
|
572
|
+
* intent still names that branch, the pin still has one incident edge, the pin node is
|
|
573
|
+
* still an unknown leaf and the target component is still uniformly referenced.
|
|
574
|
+
*/
|
|
575
|
+
const pinLevel = (pin, voltage) => pin.low === void 0 ? "unknown" : voltage <= pin.low ? "low" : voltage >= pin.high ? "high" : "unknown";
|
|
576
|
+
/**
|
|
577
|
+
* Bind the publish payload of one admitted shape: the switching pin's node, its pins in
|
|
578
|
+
* board order, and the immutable sample each of them takes on the fixed target voltage.
|
|
579
|
+
* A replay therefore commits precomputed samples instead of re-deriving voltages, levels
|
|
580
|
+
* and canonical samples on every publication.
|
|
581
|
+
*/
|
|
582
|
+
const leafPlan = (slot, branch, target, edges) => {
|
|
583
|
+
const node = pinNodes[slot];
|
|
584
|
+
const nodePins = nodeSlots[node];
|
|
585
|
+
const voltage = nodes[target].reference;
|
|
586
|
+
const slots = [];
|
|
587
|
+
const planPins = [];
|
|
588
|
+
const samples = [];
|
|
589
|
+
for (let at = 0; at < nodePins.length; at++) {
|
|
590
|
+
const nodeSlot = nodePins[at];
|
|
591
|
+
const pinned = pins[nodeSlot];
|
|
592
|
+
slots.push(nodeSlot);
|
|
593
|
+
planPins.push(pinned);
|
|
594
|
+
samples.push(sampleFor(voltage, pinLevel(pinned, voltage)));
|
|
595
|
+
}
|
|
596
|
+
return Object.freeze({
|
|
597
|
+
targetPin: branch.to,
|
|
598
|
+
resistance: branch.resistance,
|
|
599
|
+
target,
|
|
600
|
+
edges,
|
|
601
|
+
node,
|
|
602
|
+
voltage,
|
|
603
|
+
slots: Object.freeze(slots),
|
|
604
|
+
pins: Object.freeze(planPins),
|
|
605
|
+
samples: Object.freeze(samples)
|
|
606
|
+
});
|
|
607
|
+
};
|
|
608
|
+
const admitLeaf = (slot, branch, target, edges) => {
|
|
609
|
+
const plan = leafPlan(slot, branch, target, edges);
|
|
610
|
+
const plans = leafPlans[slot];
|
|
611
|
+
if (plans === void 0) {
|
|
612
|
+
leafPlans[slot] = [plan];
|
|
613
|
+
statistics.leafPlans++;
|
|
614
|
+
return;
|
|
615
|
+
}
|
|
616
|
+
for (const existing of plans) if (existing.targetPin === branch.to && existing.resistance === branch.resistance) return;
|
|
617
|
+
if (plans.length < 2) plans.push(plan);
|
|
618
|
+
else plans[plans.length - 1] = plan;
|
|
619
|
+
statistics.leafPlans++;
|
|
620
|
+
};
|
|
621
|
+
const flushAdmissions = () => {
|
|
622
|
+
for (let index = 0; index < pendingAdmissions.length; index++) {
|
|
623
|
+
const admission = pendingAdmissions[index];
|
|
624
|
+
admitLeaf(admission.slot, admission.branch, admission.target, admission.edges);
|
|
625
|
+
}
|
|
626
|
+
pendingAdmissions.length = 0;
|
|
627
|
+
};
|
|
628
|
+
const matchLeaf = (slot, intent) => {
|
|
629
|
+
const topology = prepared;
|
|
630
|
+
if (topology === void 0 || intent.length !== 1) return void 0;
|
|
631
|
+
const plans = leafPlans[slot];
|
|
632
|
+
if (plans === void 0) return void 0;
|
|
633
|
+
const previous = pinEdges[slot];
|
|
634
|
+
if (previous.length !== 1 || nodes[previous[0].to].reference === void 0) return void 0;
|
|
635
|
+
const from = pinNodes[slot];
|
|
636
|
+
if (nodeDegree[from] !== 1 || nodes[from].reference !== void 0) return void 0;
|
|
637
|
+
const branch = intent[0];
|
|
638
|
+
for (let index = 0; index < plans.length; index++) {
|
|
639
|
+
const plan = plans[index];
|
|
640
|
+
if (plan.targetPin !== branch.to || plan.resistance !== branch.resistance) continue;
|
|
641
|
+
return topology.nodeUniform[plan.target] === 1 ? plan : void 0;
|
|
642
|
+
}
|
|
643
|
+
};
|
|
644
|
+
/**
|
|
645
|
+
* Inserts into an ascending list. A touched batch holds a handful of pins, so an
|
|
646
|
+
* in-place insertion beats a comparator sort of the published slot order.
|
|
647
|
+
*/
|
|
648
|
+
const pushOrdered = (list, slot) => {
|
|
649
|
+
let position = list.length;
|
|
650
|
+
while (position > 0 && list[position - 1] > slot) {
|
|
651
|
+
list[position] = list[position - 1];
|
|
652
|
+
position--;
|
|
653
|
+
}
|
|
654
|
+
list[position] = slot;
|
|
655
|
+
};
|
|
656
|
+
/**
|
|
657
|
+
* Publishes the accepted leaf batch: every pin on a touched node gets the reference
|
|
658
|
+
* voltage, samples are committed as one batch, then intents, edges and degrees follow.
|
|
659
|
+
* No pin, sample, intent or degree changes before the whole batch is accepted.
|
|
660
|
+
*/
|
|
661
|
+
const applyLeaf = (count) => {
|
|
662
|
+
if (!componentArraysOwned) {
|
|
663
|
+
prepared = {
|
|
664
|
+
...prepared,
|
|
665
|
+
nodeComponents: prepared.nodeComponents.slice(),
|
|
666
|
+
nodeUniform: prepared.nodeUniform.slice()
|
|
667
|
+
};
|
|
668
|
+
componentArraysOwned = true;
|
|
669
|
+
}
|
|
670
|
+
const nodeComponents = prepared.nodeComponents;
|
|
671
|
+
const nodeUniform = prepared.nodeUniform;
|
|
672
|
+
changedSlots.length = 0;
|
|
673
|
+
pendingPins.length = 0;
|
|
674
|
+
pendingSamples.length = 0;
|
|
675
|
+
for (let position = 0; position < count; position++) {
|
|
676
|
+
const node = pinNodes[leafSlots[position]];
|
|
677
|
+
const target = leafTargets[position];
|
|
678
|
+
nodeComponents[node] = nodeComponents[target];
|
|
679
|
+
nodeUniform[node] = nodeUniform[target];
|
|
680
|
+
const plan = leafPayloads[position];
|
|
681
|
+
if (plan !== void 0) {
|
|
682
|
+
if (publishedVoltages[node] === plan.voltage) continue;
|
|
683
|
+
const planSlots = plan.slots;
|
|
684
|
+
for (let at = 0; at < planSlots.length; at++) {
|
|
685
|
+
const pinned = plan.pins[at];
|
|
686
|
+
const sample = plan.samples[at];
|
|
687
|
+
if (pinned.sample === sample) continue;
|
|
688
|
+
pushOrdered(changedSlots, planSlots[at]);
|
|
689
|
+
pendingPins.push(pinned);
|
|
690
|
+
pendingSamples.push(sample);
|
|
691
|
+
}
|
|
692
|
+
continue;
|
|
693
|
+
}
|
|
694
|
+
const voltage = nodes[target].reference;
|
|
695
|
+
if (publishedVoltages[node] === voltage) continue;
|
|
696
|
+
const slots = nodeSlots[node];
|
|
697
|
+
for (let at = 0; at < slots.length; at++) {
|
|
698
|
+
const slot = slots[at];
|
|
699
|
+
const pin = pins[slot];
|
|
700
|
+
const level = pin.low === void 0 ? "unknown" : voltage <= pin.low ? "low" : voltage >= pin.high ? "high" : "unknown";
|
|
701
|
+
const previous = pin.sample;
|
|
702
|
+
if (previous.voltage !== voltage || previous.level !== level || previous.status !== "valid") {
|
|
703
|
+
pushOrdered(changedSlots, slot);
|
|
704
|
+
pendingPins.push(pin);
|
|
705
|
+
pendingSamples.push(sampleFor(voltage, level));
|
|
706
|
+
}
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
for (let position = 0; position < pendingPins.length; position++) pendingPins[position].sample = pendingSamples[position];
|
|
710
|
+
for (let position = 0; position < count; position++) {
|
|
711
|
+
const slot = leafSlots[position];
|
|
712
|
+
const target = leafTargets[position];
|
|
713
|
+
const previousTarget = pinEdges[slot][0].to;
|
|
714
|
+
publishedVoltages[pinNodes[slot]] = nodes[target].reference;
|
|
715
|
+
if (previousTarget !== target) {
|
|
716
|
+
nodeDegree[previousTarget] = nodeDegree[previousTarget] - 1;
|
|
717
|
+
nodeDegree[target] = nodeDegree[target] + 1;
|
|
718
|
+
}
|
|
719
|
+
intents[slot] = leafIntents[position];
|
|
720
|
+
pinEdges[slot] = leafEdges[position];
|
|
721
|
+
}
|
|
722
|
+
preparedStale = true;
|
|
723
|
+
statistics.solves++;
|
|
724
|
+
statistics.leafPropagations++;
|
|
461
725
|
};
|
|
462
726
|
return {
|
|
463
727
|
get stats() {
|
|
464
728
|
return Object.freeze({ ...statistics });
|
|
465
729
|
},
|
|
466
730
|
solve(changedPins) {
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
const
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
resistance: branch.resistance,
|
|
496
|
-
conductance: 1 / branch.resistance
|
|
497
|
-
};
|
|
498
|
-
edges.push(edge);
|
|
499
|
-
if (old?.to === to) replacements.set(old, edge);
|
|
500
|
-
else topologyChanged = true;
|
|
731
|
+
const changed = runSolve(gatherPins(changedPins));
|
|
732
|
+
if (changed.length === 0) return unchangedPins;
|
|
733
|
+
const published = [];
|
|
734
|
+
for (let position = 0; position < changed.length; position++) published.push(pins[changed[position]]);
|
|
735
|
+
return Object.freeze(published);
|
|
736
|
+
},
|
|
737
|
+
solveSlots(changedSlotNumbers) {
|
|
738
|
+
if (changedSlotNumbers === void 0) return runSolve(void 0);
|
|
739
|
+
const reusable = changedSlotNumbers instanceof Set || Array.isArray(changedSlotNumbers) || ArrayBuffer.isView(changedSlotNumbers) && Symbol.iterator in changedSlotNumbers;
|
|
740
|
+
if (!reusable) {
|
|
741
|
+
inputSlots.length = 0;
|
|
742
|
+
for (const slot of changedSlotNumbers) inputSlots.push(slot);
|
|
743
|
+
}
|
|
744
|
+
const probe = reusable ? changedSlotNumbers : inputSlots;
|
|
745
|
+
const gathered = () => reusable ? gatherSlots(changedSlotNumbers) : inputSlots;
|
|
746
|
+
if (prepared !== void 0) {
|
|
747
|
+
const stamp = nextStamp();
|
|
748
|
+
let count = 0;
|
|
749
|
+
let complete = true;
|
|
750
|
+
for (const slot of probe) {
|
|
751
|
+
if (seenSlots[slot] === stamp) continue;
|
|
752
|
+
seenSlots[slot] = stamp;
|
|
753
|
+
const intent = pins[slot].intent;
|
|
754
|
+
if (intent === intents[slot]) continue;
|
|
755
|
+
const plan = matchLeaf(slot, intent);
|
|
756
|
+
if (plan === void 0) {
|
|
757
|
+
complete = false;
|
|
758
|
+
break;
|
|
501
759
|
}
|
|
760
|
+
leafSlots[count] = slot;
|
|
761
|
+
leafIntents[count] = intent;
|
|
762
|
+
leafEdges[count] = plan.edges;
|
|
763
|
+
leafTargets[count] = plan.target;
|
|
764
|
+
leafPayloads[count] = plan;
|
|
765
|
+
count++;
|
|
766
|
+
}
|
|
767
|
+
if (complete) {
|
|
768
|
+
if (count === 0) return runSolve(gathered());
|
|
769
|
+
applyLeaf(count);
|
|
770
|
+
return changedSlots.length === 0 ? emptySlots : changedSlots;
|
|
502
771
|
}
|
|
503
|
-
if (edges.length !== previous.length) topologyChanged = true;
|
|
504
|
-
updates.set(index, {
|
|
505
|
-
intent,
|
|
506
|
-
edges
|
|
507
|
-
});
|
|
508
772
|
}
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
773
|
+
return runSolve(gathered());
|
|
774
|
+
}
|
|
775
|
+
};
|
|
776
|
+
function gatherPins(changedPins) {
|
|
777
|
+
if (changedPins === void 0) return void 0;
|
|
778
|
+
inputSlots.length = 0;
|
|
779
|
+
for (const pin of changedPins) inputSlots.push(requirePin(pin));
|
|
780
|
+
return inputSlots;
|
|
781
|
+
}
|
|
782
|
+
function gatherSlots(changedSlotNumbers) {
|
|
783
|
+
if (changedSlotNumbers === void 0) return void 0;
|
|
784
|
+
inputSlots.length = 0;
|
|
785
|
+
for (const slot of changedSlotNumbers) inputSlots.push(slot);
|
|
786
|
+
return inputSlots;
|
|
787
|
+
}
|
|
788
|
+
/**
|
|
789
|
+
* One atomically published solve over `input` (dirty slots) or, when it is omitted, over
|
|
790
|
+
* every pin. Returns the changed slots in ascending board order.
|
|
791
|
+
*/
|
|
792
|
+
function runSolve(input) {
|
|
793
|
+
const initial = prepared === void 0;
|
|
794
|
+
pendingAdmissions.length = 0;
|
|
795
|
+
if (prepared !== void 0 && !preparedStale && input !== void 0 && input.length === 0) {
|
|
796
|
+
statistics.solves++;
|
|
797
|
+
statistics.cacheHits += prepared.blocks.length;
|
|
798
|
+
return emptySlots;
|
|
799
|
+
}
|
|
800
|
+
updatedSlots.length = 0;
|
|
801
|
+
updatedIntents.length = 0;
|
|
802
|
+
updatedEdges.length = 0;
|
|
803
|
+
replacedEdges.length = 0;
|
|
804
|
+
let stamp = nextStamp();
|
|
805
|
+
let topologyChanged = initial;
|
|
806
|
+
let leafSwitch = prepared !== void 0 && input !== void 0;
|
|
807
|
+
if (leafSwitch) {
|
|
808
|
+
let count = 0;
|
|
809
|
+
for (let position = 0; position < input.length; position++) {
|
|
810
|
+
const slot = input[position];
|
|
811
|
+
if (seenSlots[slot] === stamp) continue;
|
|
812
|
+
seenSlots[slot] = stamp;
|
|
813
|
+
const intent = pins[slot].intent;
|
|
814
|
+
if (intent === intents[slot]) continue;
|
|
815
|
+
const plan = matchLeaf(slot, intent);
|
|
816
|
+
if (plan === void 0) {
|
|
817
|
+
count = -1;
|
|
818
|
+
break;
|
|
513
819
|
}
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
820
|
+
leafSlots[count] = slot;
|
|
821
|
+
leafIntents[count] = intent;
|
|
822
|
+
leafEdges[count] = plan.edges;
|
|
823
|
+
leafTargets[count] = plan.target;
|
|
824
|
+
leafPayloads[count] = plan;
|
|
825
|
+
count++;
|
|
826
|
+
}
|
|
827
|
+
if (count > 0) {
|
|
828
|
+
applyLeaf(count);
|
|
829
|
+
return changedSlots.length === 0 ? emptySlots : changedSlots;
|
|
830
|
+
}
|
|
831
|
+
stamp = nextStamp();
|
|
832
|
+
}
|
|
833
|
+
const scanAll = initial || input === void 0;
|
|
834
|
+
for (let position = 0; position < (scanAll ? pins.length : input.length); position++) {
|
|
835
|
+
const index = scanAll ? position : input[position];
|
|
836
|
+
if (!scanAll) {
|
|
837
|
+
if (seenSlots[index] === stamp) continue;
|
|
838
|
+
seenSlots[index] = stamp;
|
|
839
|
+
}
|
|
840
|
+
const pin = pins[index];
|
|
841
|
+
const intent = pin.intent;
|
|
842
|
+
if (intent === intents[index]) continue;
|
|
843
|
+
const from = pinNodes[index];
|
|
844
|
+
const previous = pinEdges[index];
|
|
845
|
+
const edges = [];
|
|
846
|
+
for (const branch of intent) {
|
|
847
|
+
if (!validResistance(branch.resistance)) validateResistance(branch.resistance, pinLocation(pin));
|
|
848
|
+
const to = pinNodes[requireEndpoint(branch.to, pin)];
|
|
849
|
+
if (from === to) continue;
|
|
850
|
+
const old = previous[edges.length];
|
|
851
|
+
if (old?.to === to && old.resistance === branch.resistance) edges.push(old);
|
|
529
852
|
else {
|
|
530
|
-
const
|
|
531
|
-
|
|
532
|
-
|
|
853
|
+
const edge = {
|
|
854
|
+
pin: index,
|
|
855
|
+
index: edges.length,
|
|
856
|
+
from,
|
|
857
|
+
to,
|
|
858
|
+
resistance: branch.resistance,
|
|
859
|
+
conductance: 1 / branch.resistance
|
|
860
|
+
};
|
|
861
|
+
edges.push(edge);
|
|
862
|
+
if (old?.to === to) replacedEdges.push(old);
|
|
863
|
+
else topologyChanged = true;
|
|
533
864
|
}
|
|
865
|
+
}
|
|
866
|
+
if (edges.length !== previous.length) topologyChanged = true;
|
|
867
|
+
const leafShape = previous.length === 1 && edges.length === 1 && nodeDegree[from] === 1 && nodes[from].reference === void 0 && nodes[previous[0].to].reference !== void 0 && nodes[edges[0].to].reference !== void 0;
|
|
868
|
+
if (leafSwitch && !leafShape) leafSwitch = false;
|
|
869
|
+
if (leafShape && intent.length === 1 && edges[0] !== previous[0]) pendingAdmissions.push({
|
|
870
|
+
slot: index,
|
|
871
|
+
branch: intent[0],
|
|
872
|
+
target: edges[0].to,
|
|
873
|
+
edges
|
|
874
|
+
});
|
|
875
|
+
updatedSlots.push(index);
|
|
876
|
+
updatedIntents.push(intent);
|
|
877
|
+
updatedEdges.push(edges);
|
|
878
|
+
}
|
|
879
|
+
if (leafSwitch && topologyChanged) {
|
|
880
|
+
const nodeUniform = prepared.nodeUniform;
|
|
881
|
+
for (let position = 0; position < updatedSlots.length; position++) if (nodeUniform[updatedEdges[position][0].to] !== 1) {
|
|
882
|
+
leafSwitch = false;
|
|
883
|
+
break;
|
|
884
|
+
}
|
|
885
|
+
}
|
|
886
|
+
if (leafSwitch && topologyChanged) {
|
|
887
|
+
for (let position = 0; position < updatedSlots.length; position++) {
|
|
888
|
+
leafSlots[position] = updatedSlots[position];
|
|
889
|
+
leafIntents[position] = updatedIntents[position];
|
|
890
|
+
leafEdges[position] = updatedEdges[position];
|
|
891
|
+
leafTargets[position] = updatedEdges[position][0].to;
|
|
892
|
+
leafPayloads[position] = void 0;
|
|
893
|
+
}
|
|
894
|
+
flushAdmissions();
|
|
895
|
+
applyLeaf(updatedSlots.length);
|
|
896
|
+
return changedSlots.length === 0 ? emptySlots : changedSlots;
|
|
897
|
+
}
|
|
898
|
+
if (preparedStale) {
|
|
899
|
+
prepared = void 0;
|
|
900
|
+
preparedStale = false;
|
|
901
|
+
descriptorsStale = true;
|
|
902
|
+
snapshot = void 0;
|
|
903
|
+
cache = /* @__PURE__ */ new Map();
|
|
904
|
+
transitions.clear();
|
|
905
|
+
transitionAges.clear();
|
|
906
|
+
transitionClock = 0;
|
|
907
|
+
topologyChanged = true;
|
|
908
|
+
}
|
|
909
|
+
const updates = /* @__PURE__ */ new Map();
|
|
910
|
+
for (let position = 0; position < updatedSlots.length; position++) updates.set(updatedSlots[position], {
|
|
911
|
+
intent: updatedIntents[position],
|
|
912
|
+
edges: updatedEdges[position]
|
|
913
|
+
});
|
|
914
|
+
if (prepared !== void 0 && !topologyChanged && replacedEdges.length === 0) {
|
|
915
|
+
for (const [index, update] of updates) {
|
|
916
|
+
intents[index] = update.intent;
|
|
917
|
+
pinEdges[index] = update.edges;
|
|
918
|
+
}
|
|
919
|
+
statistics.solves++;
|
|
920
|
+
statistics.cacheHits += prepared.blocks.length;
|
|
921
|
+
flushAdmissions();
|
|
922
|
+
return emptySlots;
|
|
923
|
+
}
|
|
924
|
+
let topology = prepared;
|
|
925
|
+
let transitionKey;
|
|
926
|
+
let nextDescriptions;
|
|
927
|
+
let restored;
|
|
928
|
+
const dirtyBlocks = /* @__PURE__ */ new Map();
|
|
929
|
+
if (topologyChanged) {
|
|
930
|
+
if (descriptorsStale) {
|
|
931
|
+
descriptorsStale = false;
|
|
932
|
+
nextDescriptions = pinEdges.map((edges, index) => describeEdges(updates.get(index)?.edges ?? edges));
|
|
534
933
|
} else {
|
|
535
|
-
|
|
536
|
-
for (const
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
const
|
|
550
|
-
if (
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
934
|
+
nextDescriptions = pinDescriptions.slice();
|
|
935
|
+
for (const [index, update] of updates) nextDescriptions[index] = describeEdges(update.edges);
|
|
936
|
+
}
|
|
937
|
+
transitionKey = nextDescriptions.join(";");
|
|
938
|
+
restored = transitions.get(transitionKey);
|
|
939
|
+
if (restored !== void 0) topology = restoreTopology(restored.topology, pinEdges, updates);
|
|
940
|
+
else {
|
|
941
|
+
const nextEdges = pinEdges.slice();
|
|
942
|
+
for (const [index, update] of updates) nextEdges[index] = update.edges;
|
|
943
|
+
topology = prepareTopology(nodes, nextEdges);
|
|
944
|
+
}
|
|
945
|
+
} else {
|
|
946
|
+
const dirty = /* @__PURE__ */ new Set();
|
|
947
|
+
for (const edge of replacedEdges) {
|
|
948
|
+
const index = topology.edgeBlocks[edge.pin]?.[edge.index];
|
|
949
|
+
if (index !== void 0) dirty.add(index);
|
|
950
|
+
}
|
|
951
|
+
for (const index of dirty) dirtyBlocks.set(index, rebindBlock(topology.blocks[index], pinEdges, updates));
|
|
952
|
+
}
|
|
953
|
+
const nextCache = /* @__PURE__ */ new Map();
|
|
954
|
+
const pendingVoltages = /* @__PURE__ */ new Map();
|
|
955
|
+
let factorizations = 0;
|
|
956
|
+
let factorizationReuses = 0;
|
|
957
|
+
let cacheHits = topologyChanged ? 0 : topology.blocks.length - dirtyBlocks.size;
|
|
958
|
+
for (const block of topologyChanged ? topology.blocks : dirtyBlocks.values()) {
|
|
959
|
+
const { component, key } = block;
|
|
960
|
+
const previous = cache.get(key);
|
|
961
|
+
if (previous !== void 0 && sameConfiguration(previous, component)) {
|
|
962
|
+
cacheHits++;
|
|
963
|
+
nextCache.set(key, previous);
|
|
964
|
+
continue;
|
|
965
|
+
}
|
|
966
|
+
const voltages = new Map(component.nodes.map((node) => [node, nodes[node].reference ?? null]));
|
|
967
|
+
const references = component.nodes.filter((node) => nodes[node].reference !== void 0);
|
|
968
|
+
const physical = component.physical ?? component;
|
|
969
|
+
const fail = (reason) => {
|
|
970
|
+
const networkIds = physical.nodes.flatMap((node) => [...nodes[node].networks]);
|
|
971
|
+
const resources = physical.nodes.flatMap((node) => nodes[node].pins.map((pin) => pin.id));
|
|
972
|
+
const firstPin = nodes[physical.nodes[0]].pins[0];
|
|
973
|
+
throw new ElectricalError("ELECTRICAL_SOLVE_FAILED", reason, {
|
|
974
|
+
...pinLocation(firstPin),
|
|
975
|
+
...networkIds[0] === void 0 ? {} : { network: networkIds[0] },
|
|
976
|
+
networks: Object.freeze(networkIds),
|
|
977
|
+
component: Object.freeze(resources)
|
|
978
|
+
});
|
|
979
|
+
};
|
|
980
|
+
let factorization;
|
|
981
|
+
let referenceProgram;
|
|
982
|
+
if (references.length !== 0) {
|
|
983
|
+
const unknown = component.nodes.filter((node) => nodes[node].reference === void 0);
|
|
984
|
+
for (const node of references) voltages.set(node, nodes[node].reference);
|
|
985
|
+
const firstReference = nodes[references[0]].reference;
|
|
986
|
+
if (references.every((node) => nodes[node].reference === firstReference)) for (const node of unknown) voltages.set(node, firstReference);
|
|
987
|
+
else if (unknown.length !== 0) {
|
|
988
|
+
const count = unknown.length;
|
|
989
|
+
const slots = new Map(unknown.map((node, index) => [node, index]));
|
|
990
|
+
let conductanceScale = 0;
|
|
991
|
+
let voltageScale = 0;
|
|
992
|
+
let coupled = false;
|
|
993
|
+
for (const node of references) voltageScale = Math.max(voltageScale, Math.abs(nodes[node].reference));
|
|
994
|
+
for (const edge of component.edges) {
|
|
995
|
+
const from = slots.has(edge.from);
|
|
996
|
+
const to = slots.has(edge.to);
|
|
997
|
+
if (from || to) conductanceScale = Math.max(conductanceScale, edge.conductance);
|
|
998
|
+
if (from && to) coupled = true;
|
|
999
|
+
}
|
|
1000
|
+
if (!(conductanceScale > 0) || !(voltageScale > 0)) fail("Invalid equation scaling.");
|
|
1001
|
+
if (!coupled) {
|
|
1002
|
+
referenceProgram = referencePrograms.find((program) => program.matches(component.edges, unknown));
|
|
1003
|
+
if (referenceProgram === void 0) {
|
|
1004
|
+
referenceProgram = compileReferenceProgram(unknown, component.edges, slots, nodes);
|
|
1005
|
+
if (referenceProgram !== void 0) {
|
|
1006
|
+
referencePrograms.push(referenceProgram);
|
|
1007
|
+
if (referencePrograms.length > topologyTransitionLimit) referencePrograms.shift();
|
|
598
1008
|
}
|
|
599
1009
|
}
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
1010
|
+
}
|
|
1011
|
+
const matrix = new Float64Array(coupled ? count * count : 0);
|
|
1012
|
+
let anchors;
|
|
1013
|
+
let right;
|
|
1014
|
+
if (referenceProgram !== void 0) ({anchors, right} = referenceProgram.assemble(component.edges, conductanceScale, voltageScale));
|
|
1015
|
+
else {
|
|
1016
|
+
const anchorTerms = Array.from({ length: count }, () => []);
|
|
1017
|
+
const rightTerms = Array.from({ length: count }, () => []);
|
|
1018
|
+
for (const edge of component.edges) {
|
|
1019
|
+
const from = slots.get(edge.from);
|
|
1020
|
+
const to = slots.get(edge.to);
|
|
1021
|
+
const conductance = edge.conductance / conductanceScale;
|
|
1022
|
+
if (from !== void 0 && to !== void 0) {
|
|
1023
|
+
const slot = Math.min(from, to) * count + Math.max(from, to);
|
|
1024
|
+
matrix[slot] = matrix[slot] + conductance;
|
|
1025
|
+
} else if (from !== void 0 || to !== void 0) {
|
|
1026
|
+
const slot = from ?? to;
|
|
1027
|
+
const reference = nodes[from === void 0 ? edge.from : edge.to].reference;
|
|
1028
|
+
anchorTerms[slot].push(conductance);
|
|
1029
|
+
rightTerms[slot].push(conductance * (reference / voltageScale));
|
|
620
1030
|
}
|
|
621
|
-
anchors = Float64Array.from(anchorTerms, (terms) => sum(terms));
|
|
622
|
-
right = Float64Array.from(rightTerms, (terms) => sum(terms));
|
|
623
|
-
}
|
|
624
|
-
if (previous?.factorization !== void 0 && arraysEqual(previous.factorization.matrix, matrix) && arraysEqual(previous.factorization.anchors, anchors)) {
|
|
625
|
-
factorization = previous.factorization;
|
|
626
|
-
factorizationReuses++;
|
|
627
|
-
} else {
|
|
628
|
-
factorization = factorize(matrix, anchors, fail);
|
|
629
|
-
factorizations++;
|
|
630
1031
|
}
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
1032
|
+
anchors = Float64Array.from(anchorTerms, (terms) => sum(terms));
|
|
1033
|
+
right = Float64Array.from(rightTerms, (terms) => sum(terms));
|
|
1034
|
+
}
|
|
1035
|
+
if (previous?.factorization !== void 0 && arraysEqual(previous.factorization.matrix, matrix) && arraysEqual(previous.factorization.anchors, anchors)) {
|
|
1036
|
+
factorization = previous.factorization;
|
|
1037
|
+
factorizationReuses++;
|
|
1038
|
+
} else {
|
|
1039
|
+
factorization = factorize(matrix, anchors, fail);
|
|
1040
|
+
factorizations++;
|
|
637
1041
|
}
|
|
1042
|
+
const solution = substitute(factorization, right, fail);
|
|
1043
|
+
unknown.forEach((node, index) => {
|
|
1044
|
+
const voltage = cleanZero(solution[index] * voltageScale);
|
|
1045
|
+
if (!Number.isFinite(voltage)) fail("Non-finite node voltage.");
|
|
1046
|
+
voltages.set(node, voltage);
|
|
1047
|
+
});
|
|
638
1048
|
}
|
|
639
|
-
nextCache.set(key, {
|
|
640
|
-
edges: component.edges,
|
|
641
|
-
voltages: component.nodes.map((node) => voltages.get(node)),
|
|
642
|
-
factorization
|
|
643
|
-
});
|
|
644
|
-
for (const [node, voltage] of voltages) if (publishedVoltages[node] !== voltage) pendingVoltages.set(node, voltage);
|
|
645
1049
|
}
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
1050
|
+
nextCache.set(key, {
|
|
1051
|
+
edges: component.edges,
|
|
1052
|
+
voltages: component.nodes.map((node) => voltages.get(node)),
|
|
1053
|
+
factorization
|
|
1054
|
+
});
|
|
1055
|
+
for (const [node, voltage] of voltages) if (publishedVoltages[node] !== voltage) pendingVoltages.set(node, voltage);
|
|
1056
|
+
}
|
|
1057
|
+
const pending = /* @__PURE__ */ new Map();
|
|
1058
|
+
changedSlots.length = 0;
|
|
1059
|
+
for (const [node, voltage] of pendingVoltages) {
|
|
1060
|
+
const slots = nodeSlots[node];
|
|
1061
|
+
for (let at = 0; at < slots.length; at++) {
|
|
1062
|
+
const slot = slots[at];
|
|
1063
|
+
const pin = pins[slot];
|
|
649
1064
|
const level = voltage === null || pin.low === void 0 ? "unknown" : voltage <= pin.low ? "low" : voltage >= pin.high ? "high" : "unknown";
|
|
650
1065
|
const status = voltage === null ? "floating" : "valid";
|
|
651
1066
|
const previous = pin.sample;
|
|
652
1067
|
const dirty = previous.voltage !== voltage || previous.level !== level || previous.status !== status;
|
|
653
|
-
if (dirty)
|
|
1068
|
+
if (dirty) changedSlots.push(slot);
|
|
654
1069
|
if (dirty || statistics.solves === 0) pending.set(pin, Object.freeze({
|
|
655
1070
|
voltage: voltage === null ? null : cleanZero(voltage),
|
|
656
1071
|
level,
|
|
657
1072
|
status
|
|
658
1073
|
}));
|
|
659
1074
|
}
|
|
660
|
-
changed.sort((first, second) => pinSlots.get(first) - pinSlots.get(second));
|
|
661
|
-
for (const [pin, sample] of pending) pin.sample = sample;
|
|
662
|
-
for (const [node, voltage] of pendingVoltages) publishedVoltages[node] = voltage;
|
|
663
|
-
for (const [index, update] of updates) {
|
|
664
|
-
intents[index] = update.intent;
|
|
665
|
-
pinEdges[index] = update.edges;
|
|
666
|
-
}
|
|
667
|
-
if (topologyChanged) cache = nextCache;
|
|
668
|
-
else {
|
|
669
|
-
for (const [key, value] of nextCache) cache.set(key, value);
|
|
670
|
-
for (const [index, block] of dirtyBlocks) topology.blocks[index] = block;
|
|
671
|
-
}
|
|
672
|
-
prepared = topology;
|
|
673
|
-
if (topologyChanged) {
|
|
674
|
-
pinDescriptions = nextDescriptions;
|
|
675
|
-
snapshot = restored !== void 0 && topology === restored.topology ? restored : {
|
|
676
|
-
id: restored?.id ?? nextTopologyId++,
|
|
677
|
-
cost: restored?.cost ?? topologyCost(topology),
|
|
678
|
-
topology
|
|
679
|
-
};
|
|
680
|
-
rememberTransition(transitionKey, snapshot);
|
|
681
|
-
}
|
|
682
|
-
statistics.solves++;
|
|
683
|
-
statistics.components = topology.components;
|
|
684
|
-
statistics.floatingComponents = topology.floatingComponents;
|
|
685
|
-
statistics.factorizations += factorizations;
|
|
686
|
-
statistics.factorizationReuses += factorizationReuses;
|
|
687
|
-
statistics.cacheHits += cacheHits;
|
|
688
|
-
return changed.length === 0 ? unchangedPins : Object.freeze(changed);
|
|
689
1075
|
}
|
|
690
|
-
|
|
1076
|
+
if (changedSlots.length > 1) changedSlots.sort(ascendingSlot);
|
|
1077
|
+
for (const [pin, sample] of pending) pin.sample = sample;
|
|
1078
|
+
for (const [node, voltage] of pendingVoltages) publishedVoltages[node] = voltage;
|
|
1079
|
+
for (const [index, update] of updates) {
|
|
1080
|
+
syncDegrees(pinEdges[index], update.edges);
|
|
1081
|
+
intents[index] = update.intent;
|
|
1082
|
+
pinEdges[index] = update.edges;
|
|
1083
|
+
}
|
|
1084
|
+
if (topologyChanged) cache = nextCache;
|
|
1085
|
+
else {
|
|
1086
|
+
for (const [key, value] of nextCache) cache.set(key, value);
|
|
1087
|
+
for (const [index, block] of dirtyBlocks) topology.blocks[index] = block;
|
|
1088
|
+
}
|
|
1089
|
+
prepared = topology;
|
|
1090
|
+
componentArraysOwned = false;
|
|
1091
|
+
if (topologyChanged) {
|
|
1092
|
+
pinDescriptions = nextDescriptions;
|
|
1093
|
+
snapshot = restored !== void 0 && topology === restored.topology ? restored : {
|
|
1094
|
+
id: restored?.id ?? nextTopologyId++,
|
|
1095
|
+
cost: restored?.cost ?? topologyCost(topology),
|
|
1096
|
+
topology
|
|
1097
|
+
};
|
|
1098
|
+
rememberTransition(transitionKey, snapshot);
|
|
1099
|
+
}
|
|
1100
|
+
statistics.solves++;
|
|
1101
|
+
statistics.components = topology.components;
|
|
1102
|
+
statistics.floatingComponents = topology.floatingComponents;
|
|
1103
|
+
statistics.factorizations += factorizations;
|
|
1104
|
+
statistics.factorizationReuses += factorizationReuses;
|
|
1105
|
+
statistics.cacheHits += cacheHits;
|
|
1106
|
+
flushAdmissions();
|
|
1107
|
+
return changedSlots.length === 0 ? emptySlots : changedSlots;
|
|
1108
|
+
}
|
|
691
1109
|
}
|
|
692
1110
|
//#endregion
|
|
693
1111
|
//#region src/queue.ts
|
|
@@ -802,66 +1220,6 @@ var Fifo = class {
|
|
|
802
1220
|
}
|
|
803
1221
|
};
|
|
804
1222
|
//#endregion
|
|
805
|
-
//#region src/polling.ts
|
|
806
|
-
function isPollingInstruction(body, pc, pcName) {
|
|
807
|
-
const definitions = /* @__PURE__ */ new Map();
|
|
808
|
-
const redirects = [];
|
|
809
|
-
const visit = (region) => {
|
|
810
|
-
for (const operation of region.operations) {
|
|
811
|
-
if ("result" in operation) definitions.set(operation.result, operation);
|
|
812
|
-
switch (operation.kind) {
|
|
813
|
-
case "constant":
|
|
814
|
-
case "unary":
|
|
815
|
-
case "binary":
|
|
816
|
-
case "numeric":
|
|
817
|
-
case "field":
|
|
818
|
-
case "length":
|
|
819
|
-
case "index":
|
|
820
|
-
case "isNone":
|
|
821
|
-
case "nonNull":
|
|
822
|
-
case "optional":
|
|
823
|
-
case "local":
|
|
824
|
-
case "localRead":
|
|
825
|
-
case "localWrite":
|
|
826
|
-
case "memoryLength":
|
|
827
|
-
case "load8":
|
|
828
|
-
case "load16le":
|
|
829
|
-
case "load16be":
|
|
830
|
-
case "load32le":
|
|
831
|
-
case "load32be":
|
|
832
|
-
case "readSample":
|
|
833
|
-
case "yield": break;
|
|
834
|
-
case "read":
|
|
835
|
-
if (operation.resource === "derived" || operation.name === pcName) return false;
|
|
836
|
-
break;
|
|
837
|
-
case "if":
|
|
838
|
-
if (!visit(operation.yes) || operation.no && !visit(operation.no)) return false;
|
|
839
|
-
break;
|
|
840
|
-
case "select":
|
|
841
|
-
if (!visit(operation.yes.body) || !visit(operation.no.body)) return false;
|
|
842
|
-
break;
|
|
843
|
-
case "and":
|
|
844
|
-
case "or":
|
|
845
|
-
if (!visit(operation.right.body)) return false;
|
|
846
|
-
break;
|
|
847
|
-
case "match":
|
|
848
|
-
if (operation.arms.some((arm) => !visit(arm.body)) || operation.otherwise && !visit(operation.otherwise)) return false;
|
|
849
|
-
break;
|
|
850
|
-
case "redirect":
|
|
851
|
-
redirects.push(operation.target);
|
|
852
|
-
break;
|
|
853
|
-
default: return false;
|
|
854
|
-
}
|
|
855
|
-
}
|
|
856
|
-
return true;
|
|
857
|
-
};
|
|
858
|
-
if (!visit(body)) return false;
|
|
859
|
-
return redirects.some((target) => {
|
|
860
|
-
const definition = definitions.get(target);
|
|
861
|
-
return definition?.kind !== "constant" || definition.constant.kind === "number" && definition.constant.value === pc;
|
|
862
|
-
});
|
|
863
|
-
}
|
|
864
|
-
//#endregion
|
|
865
1223
|
//#region src/values.ts
|
|
866
1224
|
function dataObject(value, fields) {
|
|
867
1225
|
if (value === null || typeof value !== "object" || Object.getPrototypeOf(value) !== Object.prototype && Object.getPrototypeOf(value) !== null) throw new TypeError("Expected a plain data object");
|
|
@@ -1025,123 +1383,666 @@ function nextRandom(s) {
|
|
|
1025
1383
|
return result / 4294967296;
|
|
1026
1384
|
}
|
|
1027
1385
|
//#endregion
|
|
1028
|
-
//#region src/
|
|
1029
|
-
var
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
super(diagnostic.message);
|
|
1035
|
-
this.name = "SimulationError";
|
|
1036
|
-
this.code = diagnostic.code;
|
|
1037
|
-
this.diagnostic = diagnostic;
|
|
1038
|
-
this.related = diagnostic.related;
|
|
1039
|
-
}
|
|
1386
|
+
//#region src/observations.ts
|
|
1387
|
+
var unaryOperators = {
|
|
1388
|
+
positive: "+",
|
|
1389
|
+
negate: "-",
|
|
1390
|
+
not: "!",
|
|
1391
|
+
bitNot: "~"
|
|
1040
1392
|
};
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1393
|
+
var binaryOperators = {
|
|
1394
|
+
add: "+",
|
|
1395
|
+
subtract: "-",
|
|
1396
|
+
multiply: "*",
|
|
1397
|
+
divide: "/",
|
|
1398
|
+
remainder: "%",
|
|
1399
|
+
power: "**",
|
|
1400
|
+
lessThan: "<",
|
|
1401
|
+
lessThanOrEqual: "<=",
|
|
1402
|
+
greaterThan: ">",
|
|
1403
|
+
greaterThanOrEqual: ">=",
|
|
1404
|
+
concat: "+",
|
|
1405
|
+
bitAnd: "&",
|
|
1406
|
+
bitOr: "|",
|
|
1407
|
+
bitXor: "^",
|
|
1408
|
+
shiftLeft: "<<",
|
|
1409
|
+
shiftRight: ">>",
|
|
1410
|
+
shiftRightUnsigned: ">>>"
|
|
1411
|
+
};
|
|
1412
|
+
var numericBuiltins = /* @__PURE__ */ new Set([
|
|
1413
|
+
"isNaN",
|
|
1414
|
+
"isFinite",
|
|
1415
|
+
"isInteger",
|
|
1416
|
+
"isSafeInteger",
|
|
1417
|
+
"abs",
|
|
1418
|
+
"floor",
|
|
1419
|
+
"ceil",
|
|
1420
|
+
"trunc",
|
|
1421
|
+
"round",
|
|
1422
|
+
"sqrt",
|
|
1423
|
+
"min",
|
|
1424
|
+
"max",
|
|
1425
|
+
"pow"
|
|
1426
|
+
]);
|
|
1427
|
+
var numberBuiltins = /* @__PURE__ */ new Set([
|
|
1428
|
+
"isNaN",
|
|
1429
|
+
"isFinite",
|
|
1430
|
+
"isInteger",
|
|
1431
|
+
"isSafeInteger"
|
|
1432
|
+
]);
|
|
1433
|
+
function load(kind, memory, offset) {
|
|
1434
|
+
const size = kind.startsWith("load8") ? 1 : kind.startsWith("load16") ? 2 : 4;
|
|
1435
|
+
const at = range(memory.bytes, offset, size);
|
|
1436
|
+
if (size === 1) return memory.bytes[at];
|
|
1437
|
+
return size === 2 ? memory.view.getUint16(at, kind.endsWith("le")) : memory.view.getUint32(at, kind.endsWith("le"));
|
|
1051
1438
|
}
|
|
1052
|
-
function
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
result = compileBody(region);
|
|
1056
|
-
compiledBodies.set(region, result);
|
|
1057
|
-
}
|
|
1058
|
-
return result;
|
|
1439
|
+
function copyBytes(memory, offset, length) {
|
|
1440
|
+
const at = range(memory.bytes, offset, length);
|
|
1441
|
+
return memory.bytes.slice(at, at + Number(length));
|
|
1059
1442
|
}
|
|
1060
|
-
function
|
|
1061
|
-
|
|
1062
|
-
if (!result) {
|
|
1063
|
-
result = compileComputation(value);
|
|
1064
|
-
compiledComputations.set(value, result);
|
|
1065
|
-
}
|
|
1066
|
-
return result(host);
|
|
1443
|
+
function assertFalse(message) {
|
|
1444
|
+
throw new Error(message === void 0 ? "Assertion failed" : String(message));
|
|
1067
1445
|
}
|
|
1068
|
-
function
|
|
1069
|
-
|
|
1446
|
+
function sameBytes(left, right) {
|
|
1447
|
+
for (let index = 0; index < left.length; index++) if (left[index] !== right[index]) return false;
|
|
1448
|
+
return true;
|
|
1070
1449
|
}
|
|
1071
|
-
var
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1450
|
+
var helpers = Object.freeze({
|
|
1451
|
+
checkBoolean,
|
|
1452
|
+
createBytes,
|
|
1453
|
+
createEnum,
|
|
1454
|
+
createRecord,
|
|
1455
|
+
equalValues,
|
|
1456
|
+
nonNull,
|
|
1457
|
+
readIndex,
|
|
1458
|
+
load,
|
|
1459
|
+
copyBytes,
|
|
1460
|
+
assertFalse,
|
|
1461
|
+
sameBytes
|
|
1462
|
+
});
|
|
1463
|
+
var Unsupported = class extends Error {};
|
|
1464
|
+
function reject(reason) {
|
|
1465
|
+
throw new Unsupported(reason);
|
|
1083
1466
|
}
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1467
|
+
/** Emits one generated reader; every unsupported operation rejects the whole plan. */
|
|
1468
|
+
var ReaderEmitter = class {
|
|
1469
|
+
target;
|
|
1470
|
+
bindings = [];
|
|
1471
|
+
bindingIds = /* @__PURE__ */ new Map();
|
|
1472
|
+
slots = [];
|
|
1473
|
+
nested = [];
|
|
1474
|
+
named = /* @__PURE__ */ new Map();
|
|
1475
|
+
visiting = /* @__PURE__ */ new Set();
|
|
1476
|
+
/** Result ids already defined by an emitted operation, for static legality checks. */
|
|
1477
|
+
results = /* @__PURE__ */ new Set();
|
|
1478
|
+
buffers = [[]];
|
|
1479
|
+
depth = 0;
|
|
1480
|
+
serial = 0;
|
|
1481
|
+
constructor(target) {
|
|
1482
|
+
this.target = target;
|
|
1483
|
+
this.bindings.push(target);
|
|
1484
|
+
this.bindingIds.set(target, 0);
|
|
1088
1485
|
}
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
}
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1486
|
+
build(declaration, path) {
|
|
1487
|
+
const read = this.nestedDerived(declaration);
|
|
1488
|
+
if (path.length === 1) this.line(`return ${read}();`);
|
|
1489
|
+
else {
|
|
1490
|
+
this.line(`let projected = ${read}();`);
|
|
1491
|
+
for (let index = 1; index < path.length; index++) this.line(`projected = projected[${this.ref(path[index])}];`);
|
|
1492
|
+
this.line("return projected;");
|
|
1493
|
+
}
|
|
1494
|
+
return [
|
|
1495
|
+
"function () {",
|
|
1496
|
+
...this.nested.map((text) => text),
|
|
1497
|
+
...this.buffers[0].map((text) => ` ${text}`),
|
|
1498
|
+
"}"
|
|
1499
|
+
].join("\n");
|
|
1500
|
+
}
|
|
1501
|
+
slotsUsed() {
|
|
1502
|
+
return this.slots;
|
|
1503
|
+
}
|
|
1504
|
+
bindingTable() {
|
|
1505
|
+
return this.bindings;
|
|
1506
|
+
}
|
|
1507
|
+
get lines() {
|
|
1508
|
+
return this.buffers[this.buffers.length - 1];
|
|
1509
|
+
}
|
|
1510
|
+
line(text) {
|
|
1511
|
+
this.lines.push(`${" ".repeat(this.depth)}${text}`);
|
|
1512
|
+
}
|
|
1513
|
+
block(header, body) {
|
|
1514
|
+
this.line(`${header} {`);
|
|
1515
|
+
this.depth++;
|
|
1516
|
+
body();
|
|
1517
|
+
this.depth--;
|
|
1518
|
+
this.line("}");
|
|
1519
|
+
}
|
|
1520
|
+
bind(value) {
|
|
1521
|
+
if (!(value !== null && typeof value === "object") && Object.is(value, -0)) return this.bindings.push(value) - 1;
|
|
1522
|
+
const existing = this.bindingIds.get(value);
|
|
1523
|
+
if (existing !== void 0) return existing;
|
|
1524
|
+
const index = this.bindings.push(value) - 1;
|
|
1525
|
+
this.bindingIds.set(value, index);
|
|
1526
|
+
return index;
|
|
1527
|
+
}
|
|
1528
|
+
ref(value) {
|
|
1529
|
+
return `B[${this.bind(value)}]`;
|
|
1530
|
+
}
|
|
1531
|
+
slot(slot) {
|
|
1532
|
+
if (!this.slots.includes(slot)) this.slots.push(slot);
|
|
1533
|
+
}
|
|
1534
|
+
nestedSource(name, params, body) {
|
|
1535
|
+
this.buffers.push([]);
|
|
1536
|
+
const outer = this.depth;
|
|
1537
|
+
this.depth = 0;
|
|
1538
|
+
try {
|
|
1539
|
+
body();
|
|
1540
|
+
} finally {
|
|
1541
|
+
this.depth = outer;
|
|
1542
|
+
}
|
|
1543
|
+
const lines = this.buffers.pop();
|
|
1544
|
+
const text = [
|
|
1545
|
+
`function ${name}(${params.join(", ")}) {`,
|
|
1546
|
+
...lines,
|
|
1547
|
+
"}"
|
|
1548
|
+
].join("\n");
|
|
1549
|
+
this.nested.push(text);
|
|
1550
|
+
return name;
|
|
1551
|
+
}
|
|
1552
|
+
nestedDerived(declaration) {
|
|
1553
|
+
const key = `derived:${declaration.name}`;
|
|
1554
|
+
const existing = this.named.get(key);
|
|
1555
|
+
if (existing) return existing;
|
|
1556
|
+
if (this.visiting.has(key)) reject(`recursive derived ${declaration.name}`);
|
|
1557
|
+
const name = `d${this.serial++}`;
|
|
1558
|
+
this.named.set(key, name);
|
|
1559
|
+
this.visiting.add(key);
|
|
1560
|
+
try {
|
|
1561
|
+
this.nestedSource(name, [], () => this.region(declaration.value.body, void 0));
|
|
1562
|
+
} finally {
|
|
1563
|
+
this.visiting.delete(key);
|
|
1564
|
+
}
|
|
1565
|
+
return name;
|
|
1566
|
+
}
|
|
1567
|
+
nestedFunction(name, declaration) {
|
|
1568
|
+
const key = `function:${name}`;
|
|
1569
|
+
const existing = this.named.get(key);
|
|
1570
|
+
if (existing) return existing;
|
|
1571
|
+
if (this.visiting.has(key)) reject(`recursive pure call ${name}`);
|
|
1572
|
+
const id = `f${this.serial++}`;
|
|
1573
|
+
this.named.set(key, id);
|
|
1574
|
+
this.visiting.add(key);
|
|
1575
|
+
try {
|
|
1576
|
+
const params = declaration.params.map((_, index) => `p${index}`);
|
|
1577
|
+
this.nestedSource(id, params, () => {
|
|
1578
|
+
const outer = this.params;
|
|
1579
|
+
this.params = params;
|
|
1580
|
+
try {
|
|
1581
|
+
this.region(declaration.body, void 0);
|
|
1582
|
+
} finally {
|
|
1583
|
+
this.params = outer;
|
|
1584
|
+
}
|
|
1134
1585
|
});
|
|
1586
|
+
} finally {
|
|
1587
|
+
this.visiting.delete(key);
|
|
1588
|
+
}
|
|
1589
|
+
return id;
|
|
1590
|
+
}
|
|
1591
|
+
params;
|
|
1592
|
+
value(id) {
|
|
1593
|
+
if (!this.results.has(id)) reject(`result ${id} is used before it is defined`);
|
|
1594
|
+
return `v${id}`;
|
|
1595
|
+
}
|
|
1596
|
+
defineResult(id) {
|
|
1597
|
+
this.results.add(id);
|
|
1598
|
+
return `v${id}`;
|
|
1599
|
+
}
|
|
1600
|
+
region(region, output) {
|
|
1601
|
+
for (const operation of region.operations) this.operation(operation, output);
|
|
1602
|
+
}
|
|
1603
|
+
valueRegion(region, slot) {
|
|
1604
|
+
const label = `s${this.serial++}`;
|
|
1605
|
+
this.line(`${label}: {`);
|
|
1606
|
+
this.depth++;
|
|
1607
|
+
this.region(region.body, {
|
|
1608
|
+
slot,
|
|
1609
|
+
label
|
|
1135
1610
|
});
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1611
|
+
this.depth--;
|
|
1612
|
+
this.line("}");
|
|
1613
|
+
}
|
|
1614
|
+
argument(argument) {
|
|
1615
|
+
if (argument.kind !== "value") reject("resource argument");
|
|
1616
|
+
return this.value(argument.value);
|
|
1617
|
+
}
|
|
1618
|
+
memoryRef(ref, kind) {
|
|
1619
|
+
if (ref.kind === "parameter") reject(`parameterised memory ${kind}`);
|
|
1620
|
+
const memory = this.target.memoryPlan(ref.name);
|
|
1621
|
+
this.slot(memory.slot);
|
|
1622
|
+
return memory;
|
|
1623
|
+
}
|
|
1624
|
+
pinRef(ref, kind) {
|
|
1625
|
+
if (ref.kind === "parameter") reject(`parameterised pin ${kind}`);
|
|
1626
|
+
const pin = this.target.pins.get(ref.name);
|
|
1627
|
+
const slot = this.target.sampleResources.get(ref.name);
|
|
1628
|
+
if (!pin || !slot) reject(`unknown pin ${ref.name}`);
|
|
1629
|
+
this.slot(slot);
|
|
1630
|
+
return pin;
|
|
1631
|
+
}
|
|
1632
|
+
operation(operation, output) {
|
|
1633
|
+
const assign = (expression) => {
|
|
1634
|
+
if (!("result" in operation)) reject("resultless operation");
|
|
1635
|
+
this.line(`const ${this.defineResult(operation.result)} = ${expression};`);
|
|
1636
|
+
};
|
|
1637
|
+
const child = (region) => this.region(region, output);
|
|
1638
|
+
switch (operation.kind) {
|
|
1639
|
+
case "constant": {
|
|
1640
|
+
const constant = operation.constant;
|
|
1641
|
+
if (constant.kind === "bytes") assign(`H.createBytes(${this.ref(Object.freeze(Array.from(createBytes(constant.value))))})`);
|
|
1642
|
+
else if (constant.kind === "enum") assign(this.ref(createEnum(constant.name, constant.member)));
|
|
1643
|
+
else if (constant.kind === "record") assign(this.ref(createRecord(constant.fields.map((field) => [field.name, field.value]))));
|
|
1644
|
+
else if (constant.kind === "none") assign("null");
|
|
1645
|
+
else assign(this.ref(constant.value));
|
|
1646
|
+
break;
|
|
1647
|
+
}
|
|
1648
|
+
case "read":
|
|
1649
|
+
if (operation.resource === "state") {
|
|
1650
|
+
const slot = this.target.stateResources.get(operation.name);
|
|
1651
|
+
if (!slot || !this.target.states.has(operation.name)) reject(`unknown state ${operation.name}`);
|
|
1652
|
+
this.slot(slot);
|
|
1653
|
+
assign(`${this.ref(this.target.states)}.get(${this.ref(operation.name)})`);
|
|
1654
|
+
} else if (operation.resource === "derived") {
|
|
1655
|
+
const declaration = this.target.declarations.get(operation.name);
|
|
1656
|
+
if (declaration?.kind !== "derived") reject(`unknown derived ${operation.name}`);
|
|
1657
|
+
assign(`${this.nestedDerived(declaration)}()`);
|
|
1658
|
+
} else assign(`${this.ref(this.target)}.read(${this.ref(operation.resource)}, ${this.ref(operation.name)})`);
|
|
1659
|
+
break;
|
|
1660
|
+
case "parameter": {
|
|
1661
|
+
const params = this.params;
|
|
1662
|
+
if (!params || operation.index >= params.length) reject("value parameter outside a call");
|
|
1663
|
+
assign(params[operation.index]);
|
|
1664
|
+
break;
|
|
1665
|
+
}
|
|
1666
|
+
case "unary": {
|
|
1667
|
+
const operator = unaryOperators[operation.operator];
|
|
1668
|
+
if (!operator) reject(`unsupported unary ${operation.operator}`);
|
|
1669
|
+
assign(`(${operator}${this.value(operation.value)})`);
|
|
1670
|
+
break;
|
|
1671
|
+
}
|
|
1672
|
+
case "binary":
|
|
1673
|
+
if (operation.operator === "equal" || operation.operator === "notEqual") assign(`${operation.operator === "notEqual" ? "!" : ""}H.equalValues(${this.value(operation.left)}, ${this.value(operation.right)})`);
|
|
1674
|
+
else {
|
|
1675
|
+
const operator = binaryOperators[operation.operator];
|
|
1676
|
+
if (!operator) reject(`unsupported binary ${operation.operator}`);
|
|
1677
|
+
assign(`(${this.value(operation.left)} ${operator} ${this.value(operation.right)})`);
|
|
1678
|
+
}
|
|
1679
|
+
break;
|
|
1680
|
+
case "numeric":
|
|
1681
|
+
if (!numericBuiltins.has(operation.builtin)) reject(`unsupported builtin ${operation.builtin}`);
|
|
1682
|
+
assign(`${numberBuiltins.has(operation.builtin) ? "Number" : "Math"}.${operation.builtin}(${operation.args.map((id) => this.value(id)).join(", ")})`);
|
|
1683
|
+
break;
|
|
1684
|
+
case "record":
|
|
1685
|
+
assign(`H.createRecord([${operation.fields.map((field) => `[${this.ref(field.name)}, ${this.value(field.value)}]`).join(", ")}])`);
|
|
1686
|
+
break;
|
|
1687
|
+
case "field":
|
|
1688
|
+
assign(`${this.value(operation.value)}[${this.ref(operation.field)}]`);
|
|
1689
|
+
break;
|
|
1690
|
+
case "bytes":
|
|
1691
|
+
assign(`H.createBytes([${operation.elements.map((id) => this.value(id)).join(", ")}])`);
|
|
1692
|
+
break;
|
|
1693
|
+
case "length":
|
|
1694
|
+
assign(`${this.value(operation.value)}.length`);
|
|
1695
|
+
break;
|
|
1696
|
+
case "index":
|
|
1697
|
+
assign(`H.readIndex(${this.value(operation.value)}, ${this.value(operation.index)})`);
|
|
1698
|
+
break;
|
|
1699
|
+
case "optional":
|
|
1700
|
+
assign(this.value(operation.value));
|
|
1701
|
+
break;
|
|
1702
|
+
case "isNone":
|
|
1703
|
+
assign(`(${this.value(operation.value)} === null)`);
|
|
1704
|
+
break;
|
|
1705
|
+
case "nonNull":
|
|
1706
|
+
assign(`H.nonNull(${this.value(operation.value)})`);
|
|
1707
|
+
break;
|
|
1708
|
+
case "localRead":
|
|
1709
|
+
assign(`l${operation.local}`);
|
|
1710
|
+
break;
|
|
1711
|
+
case "local":
|
|
1712
|
+
this.line(`let l${operation.local} = ${this.value(operation.initial)};`);
|
|
1713
|
+
break;
|
|
1714
|
+
case "localWrite":
|
|
1715
|
+
this.line(`l${operation.local} = ${this.value(operation.value)};`);
|
|
1716
|
+
break;
|
|
1717
|
+
case "memoryLength":
|
|
1718
|
+
assign(`${this.ref(this.memoryRef(operation.memory, operation.kind))}.bytes.length`);
|
|
1719
|
+
break;
|
|
1720
|
+
case "load8":
|
|
1721
|
+
case "load16le":
|
|
1722
|
+
case "load16be":
|
|
1723
|
+
case "load32le":
|
|
1724
|
+
case "load32be":
|
|
1725
|
+
assign(`H.load(${this.ref(operation.kind)}, ${this.ref(this.memoryRef(operation.memory, operation.kind))}, ${this.value(operation.offset)})`);
|
|
1726
|
+
break;
|
|
1727
|
+
case "copyBytes": {
|
|
1728
|
+
const memory = this.memoryRef(operation.memory, operation.kind);
|
|
1729
|
+
assign(`H.copyBytes(${this.ref(memory)}, ${this.value(operation.offset)}, ${this.value(operation.length)})`);
|
|
1730
|
+
break;
|
|
1731
|
+
}
|
|
1732
|
+
case "readSample":
|
|
1733
|
+
assign(`${this.ref(this.pinRef(operation.pin, operation.kind))}.sample`);
|
|
1734
|
+
break;
|
|
1735
|
+
case "callValue": {
|
|
1736
|
+
if (!operation.pure) reject("impure call");
|
|
1737
|
+
const declaration = this.target.declarations.get(operation.name);
|
|
1738
|
+
if (declaration?.kind !== "function" || !declaration.pure) reject(`unknown pure function ${operation.name}`);
|
|
1739
|
+
assign(`${this.nestedFunction(operation.name, declaration)}(${operation.args.map((argument) => this.argument(argument)).join(", ")})`);
|
|
1740
|
+
break;
|
|
1741
|
+
}
|
|
1742
|
+
case "if":
|
|
1743
|
+
this.block(`if (H.checkBoolean(${this.value(operation.condition)}))`, () => child(operation.yes));
|
|
1744
|
+
if (operation.no) this.block("else", () => child(operation.no));
|
|
1745
|
+
break;
|
|
1746
|
+
case "match":
|
|
1747
|
+
operation.arms.forEach((arm, index) => {
|
|
1748
|
+
const pattern = arm.pattern;
|
|
1749
|
+
const constant = pattern.kind === "none" ? null : pattern.kind === "enum" ? createEnum(pattern.name, pattern.member) : pattern.value;
|
|
1750
|
+
this.block(`${index ? "else if" : "if"} (H.equalValues(${this.value(operation.value)}, ${this.ref(constant)}))`, () => child(arm.body));
|
|
1751
|
+
});
|
|
1752
|
+
if (operation.otherwise) this.block(operation.arms.length ? "else" : "", () => child(operation.otherwise));
|
|
1753
|
+
break;
|
|
1754
|
+
case "while":
|
|
1755
|
+
this.block("while (true)", () => {
|
|
1756
|
+
const slot = `s${this.serial++}`;
|
|
1757
|
+
this.line(`let ${slot};`);
|
|
1758
|
+
this.valueRegion(operation.test, slot);
|
|
1759
|
+
this.line(`if (!H.checkBoolean(${slot})) break;`);
|
|
1760
|
+
this.block("", () => child(operation.body));
|
|
1761
|
+
});
|
|
1762
|
+
break;
|
|
1763
|
+
case "loop":
|
|
1764
|
+
this.block("while (true)", () => child(operation.body));
|
|
1765
|
+
break;
|
|
1766
|
+
case "and":
|
|
1767
|
+
case "or": {
|
|
1768
|
+
const slot = this.defineResult(operation.result);
|
|
1769
|
+
this.line(`let ${slot} = H.checkBoolean(${this.value(operation.left)});`);
|
|
1770
|
+
this.block(`if (${operation.kind === "or" ? "!" : ""}${slot})`, () => {
|
|
1771
|
+
this.valueRegion(operation.right, slot);
|
|
1772
|
+
this.line(`${slot} = H.checkBoolean(${slot});`);
|
|
1773
|
+
});
|
|
1774
|
+
break;
|
|
1775
|
+
}
|
|
1776
|
+
case "select": {
|
|
1777
|
+
const slot = this.defineResult(operation.result);
|
|
1778
|
+
this.line(`let ${slot};`);
|
|
1779
|
+
this.block(`if (H.checkBoolean(${this.value(operation.condition)}))`, () => this.valueRegion(operation.yes, slot));
|
|
1780
|
+
this.block("else", () => this.valueRegion(operation.no, slot));
|
|
1781
|
+
break;
|
|
1782
|
+
}
|
|
1783
|
+
case "assert":
|
|
1784
|
+
this.block(`if (!H.checkBoolean(${this.value(operation.condition)}))`, () => {
|
|
1785
|
+
if (operation.message) {
|
|
1786
|
+
const slot = `s${this.serial++}`;
|
|
1787
|
+
this.line(`let ${slot};`);
|
|
1788
|
+
this.valueRegion(operation.message, slot);
|
|
1789
|
+
this.line(`H.assertFalse(${slot});`);
|
|
1790
|
+
} else this.line("H.assertFalse(undefined);");
|
|
1791
|
+
});
|
|
1792
|
+
break;
|
|
1793
|
+
case "return":
|
|
1794
|
+
this.line("return;");
|
|
1795
|
+
break;
|
|
1796
|
+
case "returnValue":
|
|
1797
|
+
this.line(`return ${this.value(operation.value)};`);
|
|
1798
|
+
break;
|
|
1799
|
+
case "break":
|
|
1800
|
+
this.line("break;");
|
|
1801
|
+
break;
|
|
1802
|
+
case "continue":
|
|
1803
|
+
this.line("continue;");
|
|
1804
|
+
break;
|
|
1805
|
+
case "yield":
|
|
1806
|
+
if (!output) reject("yield outside a value region");
|
|
1807
|
+
this.line(`${output.slot} = ${this.value(operation.value)};`);
|
|
1808
|
+
this.line(`break ${output.label};`);
|
|
1809
|
+
break;
|
|
1810
|
+
default: {
|
|
1811
|
+
const kind = operation.kind;
|
|
1812
|
+
reject(`unsupported operation ${String(kind)}`);
|
|
1813
|
+
}
|
|
1814
|
+
}
|
|
1815
|
+
}
|
|
1816
|
+
};
|
|
1817
|
+
function fieldType(declarations, type, field) {
|
|
1818
|
+
if (!type) return void 0;
|
|
1819
|
+
if (type.kind === "optional") return void 0;
|
|
1820
|
+
if (type.kind === "record") {
|
|
1821
|
+
const declaration = declarations.get(type.name);
|
|
1822
|
+
if (declaration?.kind !== "record") return void 0;
|
|
1823
|
+
const entry = declaration.fields.find((candidate) => candidate.name === field);
|
|
1824
|
+
return entry ? { kind: entry.type } : void 0;
|
|
1825
|
+
}
|
|
1826
|
+
if (type.kind === "sample") {
|
|
1827
|
+
if (field === "voltage") return {
|
|
1828
|
+
kind: "optional",
|
|
1829
|
+
inner: { kind: "number" }
|
|
1830
|
+
};
|
|
1831
|
+
if (field === "level" || field === "status") return {
|
|
1832
|
+
kind: "enum",
|
|
1833
|
+
name: "Logic"
|
|
1834
|
+
};
|
|
1835
|
+
}
|
|
1836
|
+
}
|
|
1837
|
+
function resolvePathType(declarations, type, path) {
|
|
1838
|
+
let current = type;
|
|
1839
|
+
for (let index = 1; index < path.length; index++) current = fieldType(declarations, current, path[index]);
|
|
1840
|
+
return { type: current };
|
|
1841
|
+
}
|
|
1842
|
+
function primitive(type) {
|
|
1843
|
+
if (!type) return false;
|
|
1844
|
+
if (type.kind === "optional") return primitive(type.inner);
|
|
1845
|
+
return [
|
|
1846
|
+
"number",
|
|
1847
|
+
"bool",
|
|
1848
|
+
"string",
|
|
1849
|
+
"enum"
|
|
1850
|
+
].includes(type.kind);
|
|
1851
|
+
}
|
|
1852
|
+
/**
|
|
1853
|
+
* Stable observation comparison for the declared source type. Numbers compare NaN as
|
|
1854
|
+
* equal and treat positive and negative zero as equal; bytes compare length and
|
|
1855
|
+
* content; records and samples compare their declared fields recursively; nullables
|
|
1856
|
+
* compare none first. Language equality is deliberately not reused here.
|
|
1857
|
+
*/
|
|
1858
|
+
function comparison(declarations, type, left, right) {
|
|
1859
|
+
if (!type) return void 0;
|
|
1860
|
+
switch (type.kind) {
|
|
1861
|
+
case "number": return `(${left} === ${right} || (${left} !== ${left} && ${right} !== ${right}))`;
|
|
1862
|
+
case "bool":
|
|
1863
|
+
case "string":
|
|
1864
|
+
case "enum": return `(${left} === ${right})`;
|
|
1865
|
+
case "bytes": return `(${left} === ${right} || (${left} !== null && ${right} !== null && typeof ${left} === "object" && typeof ${right} === "object" && ${left}.length === ${right}.length && H.sameBytes(${left}, ${right})))`;
|
|
1866
|
+
case "sample": {
|
|
1867
|
+
const fields = [
|
|
1868
|
+
"voltage",
|
|
1869
|
+
"level",
|
|
1870
|
+
"status"
|
|
1871
|
+
].map((field) => comparison(declarations, fieldType(declarations, type, field), `${left}[${JSON.stringify(field)}]`, `${right}[${JSON.stringify(field)}]`));
|
|
1872
|
+
if (fields.some((field) => field === void 0)) return void 0;
|
|
1873
|
+
return `(${left} === ${right} || (${left} !== null && ${right} !== null && typeof ${left} === "object" && typeof ${right} === "object" && ${fields.join(" && ")}))`;
|
|
1874
|
+
}
|
|
1875
|
+
case "record": {
|
|
1876
|
+
const declaration = declarations.get(type.name);
|
|
1877
|
+
if (declaration?.kind !== "record" || !declaration.fields.length) return void 0;
|
|
1878
|
+
const fields = declaration.fields.map((field) => comparison(declarations, { kind: field.type }, `${left}[${JSON.stringify(field.name)}]`, `${right}[${JSON.stringify(field.name)}]`));
|
|
1879
|
+
if (fields.some((field) => field === void 0)) return void 0;
|
|
1880
|
+
return `(${left} === ${right} || (${left} !== null && ${right} !== null && typeof ${left} === "object" && typeof ${right} === "object" && ${fields.join(" && ")}))`;
|
|
1881
|
+
}
|
|
1882
|
+
case "optional": {
|
|
1883
|
+
const inner = comparison(declarations, type.inner, left, right);
|
|
1884
|
+
if (inner === void 0) return void 0;
|
|
1885
|
+
return `(${left} === ${right} || (${left} === null ? ${right} === null : (${right} !== null && ${inner})))`;
|
|
1886
|
+
}
|
|
1887
|
+
default: return;
|
|
1888
|
+
}
|
|
1889
|
+
}
|
|
1890
|
+
/** Bind a source path to one generated reader and one generated comparator. */
|
|
1891
|
+
function compileObservation(target, key, path) {
|
|
1892
|
+
const declaration = target.declarations.get(path[0]);
|
|
1893
|
+
if (declaration?.kind !== "derived") return {
|
|
1894
|
+
ok: false,
|
|
1895
|
+
reason: "source root is not a derived declaration"
|
|
1896
|
+
};
|
|
1897
|
+
const { type } = resolvePathType(target.declarations, declaration.type, path);
|
|
1898
|
+
if (type === void 0) return {
|
|
1899
|
+
ok: false,
|
|
1900
|
+
reason: "source field type is not resolvable"
|
|
1901
|
+
};
|
|
1902
|
+
if (type.kind === "intent") return {
|
|
1903
|
+
ok: false,
|
|
1904
|
+
reason: "intent has no stable content comparison"
|
|
1905
|
+
};
|
|
1906
|
+
try {
|
|
1907
|
+
const emitter = new ReaderEmitter(target);
|
|
1908
|
+
const source = emitter.build(declaration, path);
|
|
1909
|
+
const evaluate = new Function("B", "H", `"use strict"; return (${source});`)(emitter.bindingTable(), helpers);
|
|
1910
|
+
const comparisonSource = `function (a, b) { return ${comparison(target.declarations, type, "a", "b") ?? "H.same(a, b)"}; }`;
|
|
1911
|
+
const comparer = new Function("H", `"use strict"; return (${comparisonSource});`)({
|
|
1912
|
+
...helpers,
|
|
1913
|
+
same
|
|
1914
|
+
});
|
|
1915
|
+
return {
|
|
1916
|
+
ok: true,
|
|
1917
|
+
plan: {
|
|
1918
|
+
key,
|
|
1919
|
+
path: [...path],
|
|
1920
|
+
source,
|
|
1921
|
+
comparison: comparisonSource,
|
|
1922
|
+
slots: emitter.slotsUsed(),
|
|
1923
|
+
evaluate,
|
|
1924
|
+
equals: comparer,
|
|
1925
|
+
needsSnapshot: !primitive(type)
|
|
1926
|
+
}
|
|
1927
|
+
};
|
|
1139
1928
|
} catch (cause) {
|
|
1140
|
-
|
|
1141
|
-
|
|
1929
|
+
return {
|
|
1930
|
+
ok: false,
|
|
1931
|
+
reason: cause instanceof Unsupported ? cause.message : `unexpected code generation failure: ${cause instanceof Error ? cause.message : String(cause)}`
|
|
1932
|
+
};
|
|
1933
|
+
}
|
|
1934
|
+
}
|
|
1935
|
+
//#endregion
|
|
1936
|
+
//#region src/runtime/execution.ts
|
|
1937
|
+
var compiledBodies = /* @__PURE__ */ new WeakMap();
|
|
1938
|
+
var resumablePrograms = /* @__PURE__ */ new WeakMap();
|
|
1939
|
+
var compiledComputations = /* @__PURE__ */ new WeakMap();
|
|
1940
|
+
var MAX_TIME = 72e14;
|
|
1941
|
+
function time(value) {
|
|
1942
|
+
if (typeof value !== "number" || !Number.isFinite(value) || !Number.isInteger(value)) throw new TypeError("Time must be a finite integer");
|
|
1943
|
+
if (value < 0 || value > 72e14) throw new RangeError("Time out of range");
|
|
1944
|
+
return value === 0 ? 0 : value;
|
|
1945
|
+
}
|
|
1946
|
+
function body(region) {
|
|
1947
|
+
let result = compiledBodies.get(region);
|
|
1948
|
+
if (!result) {
|
|
1949
|
+
result = compileBody(region);
|
|
1950
|
+
compiledBodies.set(region, result);
|
|
1951
|
+
}
|
|
1952
|
+
return result;
|
|
1953
|
+
}
|
|
1954
|
+
function resumable(region) {
|
|
1955
|
+
let result = resumablePrograms.get(region);
|
|
1956
|
+
if (result === void 0) {
|
|
1957
|
+
result = compileResumableBody(region, { bindResources: true }) ?? null;
|
|
1958
|
+
resumablePrograms.set(region, result);
|
|
1959
|
+
}
|
|
1960
|
+
return result ?? void 0;
|
|
1961
|
+
}
|
|
1962
|
+
function frame(program) {
|
|
1963
|
+
return {
|
|
1964
|
+
pc: program.start,
|
|
1965
|
+
slots: new Array(program.slotCount),
|
|
1966
|
+
kind: 0,
|
|
1967
|
+
duration: 0,
|
|
1968
|
+
channel: "",
|
|
1969
|
+
value: void 0
|
|
1970
|
+
};
|
|
1971
|
+
}
|
|
1972
|
+
function computation(value, host) {
|
|
1973
|
+
let result = compiledComputations.get(value);
|
|
1974
|
+
if (!result) {
|
|
1975
|
+
result = compileComputation(value);
|
|
1976
|
+
compiledComputations.set(value, result);
|
|
1977
|
+
}
|
|
1978
|
+
return result(host);
|
|
1979
|
+
}
|
|
1980
|
+
//#endregion
|
|
1981
|
+
//#region src/types.ts
|
|
1982
|
+
var SimulationError = class extends Error {
|
|
1983
|
+
code;
|
|
1984
|
+
diagnostic;
|
|
1985
|
+
related;
|
|
1986
|
+
constructor(diagnostic) {
|
|
1987
|
+
super(diagnostic.message);
|
|
1988
|
+
this.name = "SimulationError";
|
|
1989
|
+
this.code = diagnostic.code;
|
|
1990
|
+
this.diagnostic = diagnostic;
|
|
1991
|
+
this.related = diagnostic.related;
|
|
1142
1992
|
}
|
|
1993
|
+
};
|
|
1994
|
+
//#endregion
|
|
1995
|
+
//#region src/runtime/errors.ts
|
|
1996
|
+
function thenable(value) {
|
|
1997
|
+
return value !== null && (typeof value === "object" || typeof value === "function") && typeof value.then === "function";
|
|
1143
1998
|
}
|
|
1999
|
+
var AsyncHookError = class extends TypeError {
|
|
2000
|
+
code = "ASYNC_HOOK_RESULT";
|
|
2001
|
+
constructor() {
|
|
2002
|
+
super("Device hooks must return synchronously");
|
|
2003
|
+
}
|
|
2004
|
+
};
|
|
2005
|
+
var ElectricalParameterError = class extends RangeError {
|
|
2006
|
+
code = "INVALID_ELECTRICAL_PARAMETER";
|
|
2007
|
+
};
|
|
2008
|
+
function electricalNumber(value) {
|
|
2009
|
+
if (typeof value !== "number" || !Number.isFinite(value)) throw new ElectricalParameterError("Electrical parameters must be finite numbers");
|
|
2010
|
+
return value;
|
|
2011
|
+
}
|
|
2012
|
+
function acceptSync(value) {
|
|
2013
|
+
if (thenable(value)) {
|
|
2014
|
+
Promise.resolve(value).catch(() => {});
|
|
2015
|
+
throw new AsyncHookError();
|
|
2016
|
+
}
|
|
2017
|
+
}
|
|
2018
|
+
function modelLocation(cause) {
|
|
2019
|
+
if (!(cause instanceof BuildError)) return {};
|
|
2020
|
+
const diagnostic = cause.diagnostics[0];
|
|
2021
|
+
if (!diagnostic) return {};
|
|
2022
|
+
return {
|
|
2023
|
+
...diagnostic.path ? { path: Object.freeze([...diagnostic.path]) } : {},
|
|
2024
|
+
...diagnostic.source ? { source: snapshot(diagnostic.source) } : {}
|
|
2025
|
+
};
|
|
2026
|
+
}
|
|
2027
|
+
function standalone(code, cause, phase) {
|
|
2028
|
+
return new SimulationError(Object.freeze({
|
|
2029
|
+
code,
|
|
2030
|
+
message: cause instanceof Error ? cause.message : String(cause),
|
|
2031
|
+
phase,
|
|
2032
|
+
time: null,
|
|
2033
|
+
cause,
|
|
2034
|
+
...modelLocation(cause),
|
|
2035
|
+
related: Object.freeze([])
|
|
2036
|
+
}));
|
|
2037
|
+
}
|
|
2038
|
+
//#endregion
|
|
2039
|
+
//#region src/runtime/instance.ts
|
|
1144
2040
|
var EMPTY_MESSAGES = Object.freeze([]);
|
|
2041
|
+
/** Membership test without allocating: every key of `next` is already in `previous`. */
|
|
2042
|
+
function hasEvery(previous, next) {
|
|
2043
|
+
for (const slot of next.keys()) if (!previous.has(slot)) return false;
|
|
2044
|
+
return true;
|
|
2045
|
+
}
|
|
1145
2046
|
function createContext(owner, board) {
|
|
1146
2047
|
return Object.freeze({
|
|
1147
2048
|
get props() {
|
|
@@ -1186,6 +2087,7 @@ var Instance = class {
|
|
|
1186
2087
|
processes = [];
|
|
1187
2088
|
reactions = [];
|
|
1188
2089
|
derived = /* @__PURE__ */ new Map();
|
|
2090
|
+
derivedBuffers = /* @__PURE__ */ new Map();
|
|
1189
2091
|
stateResources = /* @__PURE__ */ new Map();
|
|
1190
2092
|
memoryResources = /* @__PURE__ */ new Map();
|
|
1191
2093
|
sampleResources = /* @__PURE__ */ new Map();
|
|
@@ -1193,6 +2095,13 @@ var Instance = class {
|
|
|
1193
2095
|
dependencyStack = [];
|
|
1194
2096
|
observations = /* @__PURE__ */ new WeakMap();
|
|
1195
2097
|
staticObservations = /* @__PURE__ */ new WeakMap();
|
|
2098
|
+
resistorIntents = /* @__PURE__ */ new Map();
|
|
2099
|
+
sampleEntries = /* @__PURE__ */ new Map();
|
|
2100
|
+
stateSlots = /* @__PURE__ */ new Map();
|
|
2101
|
+
/** Per-instance handles for the fixed resources a resumable process body binds. */
|
|
2102
|
+
resourceHandles = /* @__PURE__ */ new Map();
|
|
2103
|
+
memoryPlans = /* @__PURE__ */ new Map();
|
|
2104
|
+
observationFrames = /* @__PURE__ */ new WeakMap();
|
|
1196
2105
|
evaluating = /* @__PURE__ */ new Set();
|
|
1197
2106
|
random;
|
|
1198
2107
|
host;
|
|
@@ -1218,46 +2127,44 @@ var Instance = class {
|
|
|
1218
2127
|
this.host = {
|
|
1219
2128
|
read: (kind, name) => this.read(kind, name),
|
|
1220
2129
|
write: (name, value) => {
|
|
1221
|
-
|
|
2130
|
+
const slot = this.stateSlot(name);
|
|
1222
2131
|
this.states.set(name, snapshot(value));
|
|
1223
|
-
board.invalidate(
|
|
2132
|
+
this.board.invalidate(slot);
|
|
1224
2133
|
},
|
|
1225
2134
|
sample: (ref) => {
|
|
1226
|
-
|
|
1227
|
-
this.track(this.sampleResources, pin.name);
|
|
1228
|
-
return pin.sample;
|
|
2135
|
+
return this.readSample(ref);
|
|
1229
2136
|
},
|
|
1230
2137
|
intent: (pin, value) => {
|
|
1231
2138
|
const target = this.pin(pin);
|
|
2139
|
+
if (target.intent === value) return;
|
|
1232
2140
|
target.intent = value;
|
|
1233
|
-
board.
|
|
2141
|
+
board.dirtyElectricalSlots.add(board.electricalSlot(target));
|
|
1234
2142
|
board.electricalDirty = true;
|
|
1235
2143
|
},
|
|
1236
2144
|
resistor: (pin, resistance) => {
|
|
1237
2145
|
const r = electricalNumber(resistance);
|
|
1238
2146
|
if (r < .001 || r > 1e9) throw new ElectricalParameterError("Resistance outside [0.001, 1e9]");
|
|
1239
|
-
return
|
|
1240
|
-
to: this.pin(pin),
|
|
1241
|
-
resistance: r
|
|
1242
|
-
})]);
|
|
2147
|
+
return this.resistorIntent(pin, r);
|
|
1243
2148
|
},
|
|
1244
2149
|
memoryLength: (memory) => this.memory(memory).length,
|
|
1245
2150
|
load: (kind, memory, offset) => {
|
|
1246
|
-
this.
|
|
1247
|
-
const
|
|
1248
|
-
|
|
1249
|
-
const
|
|
1250
|
-
|
|
2151
|
+
const plan = this.memoryPlan(memory);
|
|
2152
|
+
const size = kind.startsWith("load8") ? 1 : kind.startsWith("load16") ? 2 : 4;
|
|
2153
|
+
const at = range(plan.bytes, offset, size);
|
|
2154
|
+
const dependencies = this.dependencyStack.at(-1);
|
|
2155
|
+
if (dependencies) dependencies.set(plan.slot, plan.slot.version);
|
|
2156
|
+
if (size === 1) return plan.bytes[at];
|
|
2157
|
+
return size === 2 ? plan.view.getUint16(at, kind.endsWith("le")) : plan.view.getUint32(at, kind.endsWith("le"));
|
|
1251
2158
|
},
|
|
1252
2159
|
store: (kind, memory, offset, value) => {
|
|
1253
|
-
const
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
this.
|
|
2160
|
+
const plan = this.memoryPlan(memory);
|
|
2161
|
+
const size = kind.startsWith("store8") ? 1 : kind.startsWith("store16") ? 2 : 4;
|
|
2162
|
+
const at = range(plan.bytes, offset, size);
|
|
2163
|
+
const n = integer(value, "stored value");
|
|
2164
|
+
if (size === 1) plan.bytes[at] = n;
|
|
2165
|
+
else if (size === 2) plan.view.setUint16(at, n, kind.endsWith("le"));
|
|
2166
|
+
else plan.view.setUint32(at, n, kind.endsWith("le"));
|
|
2167
|
+
this.memoryChangedPlan(plan);
|
|
1261
2168
|
},
|
|
1262
2169
|
copyBytes: (memory, offset, length) => {
|
|
1263
2170
|
this.track(this.memoryResources, String(memory));
|
|
@@ -1274,24 +2181,37 @@ var Instance = class {
|
|
|
1274
2181
|
call: (name, args) => this.invoke(name, args),
|
|
1275
2182
|
assert: (condition, message) => {
|
|
1276
2183
|
if (condition !== true) throw new Error(message === void 0 ? "Assertion failed" : String(message));
|
|
1277
|
-
}
|
|
2184
|
+
},
|
|
2185
|
+
bind: (specs) => this.bindResources(specs)
|
|
1278
2186
|
};
|
|
1279
2187
|
this.context = createContext(this, board);
|
|
1280
2188
|
}
|
|
1281
2189
|
track(resources, name) {
|
|
2190
|
+
this.trackSlot(resources.get(name));
|
|
2191
|
+
}
|
|
2192
|
+
trackSlot(slot) {
|
|
1282
2193
|
const dependencies = this.dependencyStack.at(-1);
|
|
1283
|
-
if (dependencies)
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
2194
|
+
if (dependencies) dependencies.set(slot, slot.version);
|
|
2195
|
+
}
|
|
2196
|
+
/** Read-only evidence that a process frame bound per-instance resource handles. */
|
|
2197
|
+
bindingReport() {
|
|
2198
|
+
return Object.freeze({
|
|
2199
|
+
instance: this.id,
|
|
2200
|
+
handles: this.resourceHandles.size,
|
|
2201
|
+
bound: Object.freeze([...this.resourceHandles.keys()].sort()),
|
|
2202
|
+
processes: Object.freeze(this.processes.map((process) => Object.freeze({
|
|
2203
|
+
name: process.declaration.kind === "on" ? process.declaration.name : "start",
|
|
2204
|
+
bound: process.frame?.resources?.length ?? 0
|
|
2205
|
+
})))
|
|
2206
|
+
});
|
|
1287
2207
|
}
|
|
1288
2208
|
dependenciesCurrent(dependencies) {
|
|
1289
2209
|
if (!dependencies) return false;
|
|
1290
2210
|
for (const [slot, version] of dependencies) if (slot.version !== version) return false;
|
|
1291
2211
|
return true;
|
|
1292
2212
|
}
|
|
1293
|
-
evaluate(compute) {
|
|
1294
|
-
const dependencies = /* @__PURE__ */ new Map();
|
|
2213
|
+
evaluate(compute, target) {
|
|
2214
|
+
const dependencies = target ?? /* @__PURE__ */ new Map();
|
|
1295
2215
|
this.dependencyStack.push(dependencies);
|
|
1296
2216
|
try {
|
|
1297
2217
|
return {
|
|
@@ -1304,6 +2224,17 @@ var Instance = class {
|
|
|
1304
2224
|
if (parent) for (const [key, version] of dependencies) parent.set(key, version);
|
|
1305
2225
|
}
|
|
1306
2226
|
}
|
|
2227
|
+
/** Leaf form of :func:`evaluate` for a source walk: no closure and no result wrapper. */
|
|
2228
|
+
evaluateSource(path, dependencies) {
|
|
2229
|
+
this.dependencyStack.push(dependencies);
|
|
2230
|
+
try {
|
|
2231
|
+
return snapshot(this.source(path));
|
|
2232
|
+
} finally {
|
|
2233
|
+
this.dependencyStack.pop();
|
|
2234
|
+
const parent = this.dependencyStack.at(-1);
|
|
2235
|
+
if (parent) for (const [key, version] of dependencies) parent.set(key, version);
|
|
2236
|
+
}
|
|
2237
|
+
}
|
|
1307
2238
|
observe(observation, path) {
|
|
1308
2239
|
const read = this.staticObservations.get(observation);
|
|
1309
2240
|
if (read) return read();
|
|
@@ -1324,25 +2255,40 @@ var Instance = class {
|
|
|
1324
2255
|
this.staticObservations.set(observation, read);
|
|
1325
2256
|
return value;
|
|
1326
2257
|
}
|
|
1327
|
-
const
|
|
1328
|
-
|
|
1329
|
-
if (
|
|
1330
|
-
|
|
2258
|
+
const frames = this.observationFrames.get(observation);
|
|
2259
|
+
let target;
|
|
2260
|
+
if (frames === void 0) {
|
|
2261
|
+
target = /* @__PURE__ */ new Map();
|
|
2262
|
+
this.observationFrames.set(observation, {
|
|
2263
|
+
first: target,
|
|
2264
|
+
second: /* @__PURE__ */ new Map(),
|
|
2265
|
+
alternate: true
|
|
2266
|
+
});
|
|
2267
|
+
} else {
|
|
2268
|
+
target = frames.alternate ? frames.second : frames.first;
|
|
2269
|
+
frames.alternate = !frames.alternate;
|
|
2270
|
+
target.clear();
|
|
1331
2271
|
}
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
2272
|
+
const observed = this.evaluateSource(path, target);
|
|
2273
|
+
const previous = this.observations.get(observation);
|
|
2274
|
+
if (previous === void 0 || previous.size !== target.size || !hasEvery(previous, target)) {
|
|
2275
|
+
if (previous) {
|
|
2276
|
+
for (const slot of previous.keys()) if (!target.has(slot) && slot.subscribers.delete(observation) && !slot.subscribers.size) this.observerRevision++;
|
|
2277
|
+
}
|
|
2278
|
+
for (const slot of target.keys()) if (!previous?.has(slot)) {
|
|
2279
|
+
if (!slot.subscribers.size) this.observerRevision++;
|
|
2280
|
+
slot.subscribers.add(observation);
|
|
2281
|
+
}
|
|
1335
2282
|
}
|
|
1336
|
-
this.observations.set(observation,
|
|
1337
|
-
return observed
|
|
2283
|
+
this.observations.set(observation, target);
|
|
2284
|
+
return observed;
|
|
1338
2285
|
}
|
|
1339
2286
|
observationReader(observation, path) {
|
|
1340
2287
|
return this.staticObservations.get(observation) ?? (() => this.observe(observation, path));
|
|
1341
2288
|
}
|
|
1342
2289
|
read(kind, name) {
|
|
1343
2290
|
if (kind === "state") {
|
|
1344
|
-
|
|
1345
|
-
this.track(this.stateResources, name);
|
|
2291
|
+
this.trackSlot(this.stateSlot(name));
|
|
1346
2292
|
return this.states.get(name);
|
|
1347
2293
|
}
|
|
1348
2294
|
if (kind === "derived") {
|
|
@@ -1350,7 +2296,12 @@ var Instance = class {
|
|
|
1350
2296
|
if (!cached || !this.dependenciesCurrent(cached.dependencies)) {
|
|
1351
2297
|
const declaration = this.declarations.get(name);
|
|
1352
2298
|
if (declaration?.kind !== "derived") throw new TypeError(`Unknown derived ${name}`);
|
|
1353
|
-
|
|
2299
|
+
let buffer = this.derivedBuffers.get(name);
|
|
2300
|
+
if (buffer === void 0) {
|
|
2301
|
+
buffer = /* @__PURE__ */ new Map();
|
|
2302
|
+
this.derivedBuffers.set(name, buffer);
|
|
2303
|
+
} else buffer.clear();
|
|
2304
|
+
cached = this.evaluate(() => computation(declaration.value, this.host), buffer);
|
|
1354
2305
|
this.derived.set(name, cached);
|
|
1355
2306
|
}
|
|
1356
2307
|
const parent = this.dependencyStack.at(-1);
|
|
@@ -1391,13 +2342,87 @@ var Instance = class {
|
|
|
1391
2342
|
if (!pin) throw new TypeError(`Unknown pin ${String(ref)}`);
|
|
1392
2343
|
return pin;
|
|
1393
2344
|
}
|
|
2345
|
+
/**
|
|
2346
|
+
* One frozen, reusable intent value per (pin, resistance); intents are immutable. The
|
|
2347
|
+
* outer map is keyed by pin name and the inner one by resistance, so the hot path builds
|
|
2348
|
+
* no key string. Bounds: at most 64 names, and at most 8 resistances per name; a full
|
|
2349
|
+
* level is cleared wholesale (no least-recently-used eviction), so identity is only
|
|
2350
|
+
* reused while both levels stay under their limits.
|
|
2351
|
+
*/
|
|
2352
|
+
resistorIntent(pin, resistance) {
|
|
2353
|
+
const name = typeof pin === "string" ? pin : String(pin);
|
|
2354
|
+
let byResistance = this.resistorIntents.get(name);
|
|
2355
|
+
if (byResistance === void 0) {
|
|
2356
|
+
if (this.resistorIntents.size >= 64) this.resistorIntents.clear();
|
|
2357
|
+
byResistance = /* @__PURE__ */ new Map();
|
|
2358
|
+
this.resistorIntents.set(name, byResistance);
|
|
2359
|
+
}
|
|
2360
|
+
let intent = byResistance.get(resistance);
|
|
2361
|
+
if (intent === void 0) {
|
|
2362
|
+
if (byResistance.size >= 8) byResistance.clear();
|
|
2363
|
+
intent = Object.freeze([Object.freeze({
|
|
2364
|
+
to: this.pin(name),
|
|
2365
|
+
resistance
|
|
2366
|
+
})]);
|
|
2367
|
+
byResistance.set(resistance, intent);
|
|
2368
|
+
}
|
|
2369
|
+
return intent;
|
|
2370
|
+
}
|
|
2371
|
+
readSample(ref) {
|
|
2372
|
+
const name = typeof ref === "string" ? ref : String(ref);
|
|
2373
|
+
let entry = this.sampleEntries.get(name);
|
|
2374
|
+
if (entry === void 0) {
|
|
2375
|
+
const pin = this.pin(name);
|
|
2376
|
+
entry = {
|
|
2377
|
+
pin,
|
|
2378
|
+
slot: this.sampleResources.get(pin.name)
|
|
2379
|
+
};
|
|
2380
|
+
if (this.sampleEntries.size >= 512) this.sampleEntries.clear();
|
|
2381
|
+
this.sampleEntries.set(name, entry);
|
|
2382
|
+
}
|
|
2383
|
+
const dependencies = this.dependencyStack.at(-1);
|
|
2384
|
+
if (dependencies) dependencies.set(entry.slot, entry.slot.version);
|
|
2385
|
+
return entry.pin.sample;
|
|
2386
|
+
}
|
|
1394
2387
|
memory(ref) {
|
|
1395
2388
|
const bytes = this.memories.get(String(ref));
|
|
1396
2389
|
if (!bytes) throw new TypeError(`Unknown memory ${String(ref)}`);
|
|
1397
2390
|
return bytes;
|
|
1398
2391
|
}
|
|
2392
|
+
/** Cached resource bookkeeping for state and memory, resolved once per declaration name. */
|
|
2393
|
+
stateSlot(name) {
|
|
2394
|
+
let slot = this.stateSlots.get(name);
|
|
2395
|
+
if (slot === void 0) {
|
|
2396
|
+
if (!this.states.has(name)) throw new TypeError(`Unknown state ${name}`);
|
|
2397
|
+
slot = this.stateResources.get(name);
|
|
2398
|
+
this.stateSlots.set(name, slot);
|
|
2399
|
+
}
|
|
2400
|
+
return slot;
|
|
2401
|
+
}
|
|
2402
|
+
memoryPlan(ref) {
|
|
2403
|
+
const name = typeof ref === "string" ? ref : String(ref);
|
|
2404
|
+
let plan = this.memoryPlans.get(name);
|
|
2405
|
+
if (plan === void 0) {
|
|
2406
|
+
const bytes = this.memories.get(name);
|
|
2407
|
+
if (!bytes) throw new TypeError(`Unknown memory ${name}`);
|
|
2408
|
+
plan = {
|
|
2409
|
+
name,
|
|
2410
|
+
bytes,
|
|
2411
|
+
view: this.memoryViews.get(name),
|
|
2412
|
+
slot: this.memoryResources.get(name)
|
|
2413
|
+
};
|
|
2414
|
+
this.memoryPlans.set(name, plan);
|
|
2415
|
+
}
|
|
2416
|
+
return plan;
|
|
2417
|
+
}
|
|
1399
2418
|
memoryChanged(name) {
|
|
1400
|
-
this.
|
|
2419
|
+
this.memoryChangedSlot(name, this.memoryPlan(name).slot);
|
|
2420
|
+
}
|
|
2421
|
+
memoryChangedPlan(plan) {
|
|
2422
|
+
this.memoryChangedSlot(plan.name, plan.slot);
|
|
2423
|
+
}
|
|
2424
|
+
memoryChangedSlot(name, slot) {
|
|
2425
|
+
this.board.invalidate(slot);
|
|
1401
2426
|
for (const jit of this.jits) if (jit.options.code === name) {
|
|
1402
2427
|
jit.cache.clear();
|
|
1403
2428
|
jit.revision++;
|
|
@@ -1408,6 +2433,120 @@ var Instance = class {
|
|
|
1408
2433
|
else jit.direct = void 0;
|
|
1409
2434
|
}
|
|
1410
2435
|
}
|
|
2436
|
+
/**
|
|
2437
|
+
* Handles for the fixed names a resumable process body uses. They live on the instance,
|
|
2438
|
+
* so a shared program text binds each instance (and each process frame) separately, and
|
|
2439
|
+
* every read still resolves the current sample or state value.
|
|
2440
|
+
*/
|
|
2441
|
+
bindResources(specs) {
|
|
2442
|
+
const handles = new Array(specs.length);
|
|
2443
|
+
for (let index = 0; index < specs.length; index++) {
|
|
2444
|
+
const spec = specs[index];
|
|
2445
|
+
const key = `${spec[0]}\0${spec[1]}`;
|
|
2446
|
+
let handle = this.resourceHandles.get(key);
|
|
2447
|
+
if (handle === void 0) {
|
|
2448
|
+
handle = this.resourceHandle(spec[0], spec[1]);
|
|
2449
|
+
this.resourceHandles.set(key, handle);
|
|
2450
|
+
}
|
|
2451
|
+
handles[index] = handle;
|
|
2452
|
+
}
|
|
2453
|
+
return handles;
|
|
2454
|
+
}
|
|
2455
|
+
/**
|
|
2456
|
+
* Each method repeats the generic host operation exactly: name resolution stays lazy so
|
|
2457
|
+
* an invalid name still fails at the same operation, dependency slots are still
|
|
2458
|
+
* recorded, state writes still snapshot, and memory stores still run the code-memory
|
|
2459
|
+
* invalidation that clears JIT caches.
|
|
2460
|
+
*/
|
|
2461
|
+
resourceHandle(kind, name) {
|
|
2462
|
+
if (kind === "pin") {
|
|
2463
|
+
let pin;
|
|
2464
|
+
let slot = -1;
|
|
2465
|
+
let sample;
|
|
2466
|
+
const resolve = () => {
|
|
2467
|
+
if (pin === void 0) {
|
|
2468
|
+
pin = this.pin(name);
|
|
2469
|
+
slot = this.board.electricalSlot(pin);
|
|
2470
|
+
sample = this.sampleResources.get(name);
|
|
2471
|
+
}
|
|
2472
|
+
return pin;
|
|
2473
|
+
};
|
|
2474
|
+
return Object.freeze({
|
|
2475
|
+
sample: () => {
|
|
2476
|
+
const current = resolve();
|
|
2477
|
+
const dependencies = this.dependencyStack.at(-1);
|
|
2478
|
+
if (dependencies) dependencies.set(sample, sample.version);
|
|
2479
|
+
return current.sample;
|
|
2480
|
+
},
|
|
2481
|
+
intent: (value) => {
|
|
2482
|
+
const current = resolve();
|
|
2483
|
+
if (current.intent === value) return;
|
|
2484
|
+
current.intent = value;
|
|
2485
|
+
this.board.dirtyElectricalSlots.add(slot);
|
|
2486
|
+
this.board.electricalDirty = true;
|
|
2487
|
+
},
|
|
2488
|
+
resistor: (resistance) => {
|
|
2489
|
+
const r = electricalNumber(resistance);
|
|
2490
|
+
if (r < .001 || r > 1e9) throw new ElectricalParameterError("Resistance outside [0.001, 1e9]");
|
|
2491
|
+
return this.resistorIntent(name, r);
|
|
2492
|
+
}
|
|
2493
|
+
});
|
|
2494
|
+
}
|
|
2495
|
+
if (kind === "state") {
|
|
2496
|
+
let slot;
|
|
2497
|
+
const resolve = () => slot ??= this.stateSlot(name);
|
|
2498
|
+
const states = this.states;
|
|
2499
|
+
return Object.freeze({
|
|
2500
|
+
read: () => {
|
|
2501
|
+
const current = resolve();
|
|
2502
|
+
const dependencies = this.dependencyStack.at(-1);
|
|
2503
|
+
if (dependencies) dependencies.set(current, current.version);
|
|
2504
|
+
return states.get(name);
|
|
2505
|
+
},
|
|
2506
|
+
write: (value) => {
|
|
2507
|
+
const current = resolve();
|
|
2508
|
+
states.set(name, snapshot(value));
|
|
2509
|
+
this.board.invalidate(current);
|
|
2510
|
+
}
|
|
2511
|
+
});
|
|
2512
|
+
}
|
|
2513
|
+
let plan;
|
|
2514
|
+
const resolve = () => plan ??= this.memoryPlan(name);
|
|
2515
|
+
const read = (size, little, offset) => {
|
|
2516
|
+
const current = resolve();
|
|
2517
|
+
const at = range(current.bytes, offset, size);
|
|
2518
|
+
const dependencies = this.dependencyStack.at(-1);
|
|
2519
|
+
if (dependencies) dependencies.set(current.slot, current.slot.version);
|
|
2520
|
+
return size === 1 ? current.bytes[at] : size === 2 ? current.view.getUint16(at, little) : current.view.getUint32(at, little);
|
|
2521
|
+
};
|
|
2522
|
+
const write = (size, little, offset, value) => {
|
|
2523
|
+
const current = resolve();
|
|
2524
|
+
const at = range(current.bytes, offset, size);
|
|
2525
|
+
const n = integer(value, "stored value");
|
|
2526
|
+
if (size === 1) current.bytes[at] = n;
|
|
2527
|
+
else if (size === 2) current.view.setUint16(at, n, little);
|
|
2528
|
+
else current.view.setUint32(at, n, little);
|
|
2529
|
+
this.memoryChangedSlot(name, current.slot);
|
|
2530
|
+
};
|
|
2531
|
+
return Object.freeze({
|
|
2532
|
+
load8: (offset) => read(1, false, offset),
|
|
2533
|
+
load16le: (offset) => read(2, true, offset),
|
|
2534
|
+
load16be: (offset) => read(2, false, offset),
|
|
2535
|
+
load32le: (offset) => read(4, true, offset),
|
|
2536
|
+
load32be: (offset) => read(4, false, offset),
|
|
2537
|
+
store8: (offset, value) => write(1, false, offset, value),
|
|
2538
|
+
store16le: (offset, value) => write(2, true, offset, value),
|
|
2539
|
+
store16be: (offset, value) => write(2, false, offset, value),
|
|
2540
|
+
store32le: (offset, value) => write(4, true, offset, value),
|
|
2541
|
+
store32be: (offset, value) => write(4, false, offset, value),
|
|
2542
|
+
length: () => this.memory(name).length,
|
|
2543
|
+
copy: (offset, length) => {
|
|
2544
|
+
this.track(this.memoryResources, name);
|
|
2545
|
+
const bytes = this.memory(name), at = range(bytes, offset, length);
|
|
2546
|
+
return bytes.slice(at, at + Number(length));
|
|
2547
|
+
}
|
|
2548
|
+
});
|
|
2549
|
+
}
|
|
1411
2550
|
prepare() {
|
|
1412
2551
|
for (const declaration of this.entry.model.declarations) if (declaration.kind === "pin") this.pins.set(declaration.name, {
|
|
1413
2552
|
id: `${this.id}\0${declaration.name}`,
|
|
@@ -1473,6 +2612,7 @@ var Instance = class {
|
|
|
1473
2612
|
this.processes.push({
|
|
1474
2613
|
instance: this,
|
|
1475
2614
|
declaration,
|
|
2615
|
+
resumable: resumable(declaration.body),
|
|
1476
2616
|
...declaration.kind === "on" ? { source: declaration.sourcePath } : {},
|
|
1477
2617
|
state: "idle",
|
|
1478
2618
|
pending: false
|
|
@@ -1617,87 +2757,539 @@ var Instance = class {
|
|
|
1617
2757
|
polling.set(pc, predicate);
|
|
1618
2758
|
}
|
|
1619
2759
|
}
|
|
1620
|
-
const jit = {
|
|
1621
|
-
instance: this,
|
|
1622
|
-
options: Object.freeze({
|
|
1623
|
-
...options,
|
|
1624
|
-
...options.beforeInstruction ? { beforeInstruction: Object.freeze({ ...options.beforeInstruction }) } : {}
|
|
1625
|
-
}),
|
|
1626
|
-
cache: /* @__PURE__ */ new Map(),
|
|
1627
|
-
polling,
|
|
1628
|
-
revision: 0,
|
|
1629
|
-
directRevision: -1,
|
|
1630
|
-
directObserverRevision: -1,
|
|
1631
|
-
directStateGuards: /* @__PURE__ */ new Set(),
|
|
1632
|
-
directMemoryGuards: /* @__PURE__ */ new Set(),
|
|
1633
|
-
directHookReads: /* @__PURE__ */ new Set(),
|
|
1634
|
-
directPrograms: /* @__PURE__ */ new Map(),
|
|
1635
|
-
directProgramRevision: -1,
|
|
1636
|
-
direct: void 0,
|
|
1637
|
-
directSkip: void 0,
|
|
1638
|
-
pollReads: void 0,
|
|
1639
|
-
currentPc: void 0,
|
|
1640
|
-
automaticPolling: false,
|
|
1641
|
-
pollingWait: false,
|
|
1642
|
-
pollingPeriod: 0,
|
|
1643
|
-
pollingOrigin: 0,
|
|
1644
|
-
active: true,
|
|
1645
|
-
stopped: false,
|
|
1646
|
-
queued: false,
|
|
1647
|
-
execution: void 0
|
|
1648
|
-
};
|
|
1649
|
-
this.jits.push(jit);
|
|
1650
|
-
this.board.jits.push(jit);
|
|
1651
|
-
const disposable = { dispose: () => {
|
|
1652
|
-
if (!jit.active) return;
|
|
1653
|
-
jit.active = false;
|
|
1654
|
-
jit.execution = void 0;
|
|
1655
|
-
delete jit.predicate;
|
|
1656
|
-
this.board.waitingJits.delete(jit);
|
|
1657
|
-
jit.cache.clear();
|
|
1658
|
-
jit.directPrograms.clear();
|
|
2760
|
+
const jit = {
|
|
2761
|
+
instance: this,
|
|
2762
|
+
options: Object.freeze({
|
|
2763
|
+
...options,
|
|
2764
|
+
...options.beforeInstruction ? { beforeInstruction: Object.freeze({ ...options.beforeInstruction }) } : {}
|
|
2765
|
+
}),
|
|
2766
|
+
cache: /* @__PURE__ */ new Map(),
|
|
2767
|
+
polling,
|
|
2768
|
+
revision: 0,
|
|
2769
|
+
directRevision: -1,
|
|
2770
|
+
directObserverRevision: -1,
|
|
2771
|
+
directStateGuards: /* @__PURE__ */ new Set(),
|
|
2772
|
+
directMemoryGuards: /* @__PURE__ */ new Set(),
|
|
2773
|
+
directHookReads: /* @__PURE__ */ new Set(),
|
|
2774
|
+
directPrograms: /* @__PURE__ */ new Map(),
|
|
2775
|
+
directProgramRevision: -1,
|
|
2776
|
+
direct: void 0,
|
|
2777
|
+
directSkip: void 0,
|
|
2778
|
+
pollReads: void 0,
|
|
2779
|
+
currentPc: void 0,
|
|
2780
|
+
automaticPolling: false,
|
|
2781
|
+
pollingWait: false,
|
|
2782
|
+
pollingPeriod: 0,
|
|
2783
|
+
pollingOrigin: 0,
|
|
2784
|
+
active: true,
|
|
2785
|
+
stopped: false,
|
|
2786
|
+
queued: false,
|
|
2787
|
+
execution: void 0
|
|
2788
|
+
};
|
|
2789
|
+
this.jits.push(jit);
|
|
2790
|
+
this.board.jits.push(jit);
|
|
2791
|
+
const disposable = { dispose: () => {
|
|
2792
|
+
if (!jit.active) return;
|
|
2793
|
+
jit.active = false;
|
|
2794
|
+
jit.execution = void 0;
|
|
2795
|
+
delete jit.predicate;
|
|
2796
|
+
this.board.waitingJits.delete(jit);
|
|
2797
|
+
jit.cache.clear();
|
|
2798
|
+
jit.directPrograms.clear();
|
|
2799
|
+
jit.direct = void 0;
|
|
2800
|
+
jit.pollReads = void 0;
|
|
2801
|
+
} };
|
|
2802
|
+
this.resources.push(disposable);
|
|
2803
|
+
return disposable;
|
|
2804
|
+
}
|
|
2805
|
+
loadBytes(name, bytes, offset) {
|
|
2806
|
+
this.guard();
|
|
2807
|
+
if (this.board.phase !== "idle" && !(this.board.phase === "initialize" && this.board.current === this)) throw this.board.error("INVALID_CALL_STATE", "Code loading is not allowed in this phase", this);
|
|
2808
|
+
if (typeof name !== "string" || !this.jits.some((jit) => jit.active && jit.options.code === name)) throw this.board.error("INVALID_BINDING", "Memory is not bound as code", this);
|
|
2809
|
+
if (!(bytes instanceof Uint8Array)) throw new TypeError("Code must be Uint8Array");
|
|
2810
|
+
const memory = this.memory(name), at = range(memory, offset, bytes.length);
|
|
2811
|
+
if (bytes.length) {
|
|
2812
|
+
memory.set(bytes.slice(), at);
|
|
2813
|
+
this.memoryChanged(name);
|
|
2814
|
+
}
|
|
2815
|
+
}
|
|
2816
|
+
source(path) {
|
|
2817
|
+
const declaration = this.declarations.get(path[0]);
|
|
2818
|
+
let value;
|
|
2819
|
+
let firstField = 1;
|
|
2820
|
+
if (declaration?.kind === "pin") {
|
|
2821
|
+
this.track(this.sampleResources, declaration.name);
|
|
2822
|
+
if (path[1] !== "sample") throw new TypeError(`Invalid source ${path.join(".")}`);
|
|
2823
|
+
value = this.pin(declaration.name).sample;
|
|
2824
|
+
firstField = 2;
|
|
2825
|
+
} else if (declaration?.kind === "state" || declaration?.kind === "derived") value = this.read(declaration.kind, declaration.name);
|
|
2826
|
+
else throw new TypeError(`Invalid source ${path.join(".")}`);
|
|
2827
|
+
for (let index = firstField; index < path.length; index++) value = value[path[index]];
|
|
2828
|
+
return value;
|
|
2829
|
+
}
|
|
2830
|
+
};
|
|
2831
|
+
//#endregion
|
|
2832
|
+
//#region src/polling.ts
|
|
2833
|
+
function isPollingInstruction(body, pc, pcName) {
|
|
2834
|
+
const definitions = /* @__PURE__ */ new Map();
|
|
2835
|
+
const redirects = [];
|
|
2836
|
+
const visit = (region) => {
|
|
2837
|
+
for (const operation of region.operations) {
|
|
2838
|
+
if ("result" in operation) definitions.set(operation.result, operation);
|
|
2839
|
+
switch (operation.kind) {
|
|
2840
|
+
case "constant":
|
|
2841
|
+
case "unary":
|
|
2842
|
+
case "binary":
|
|
2843
|
+
case "numeric":
|
|
2844
|
+
case "field":
|
|
2845
|
+
case "length":
|
|
2846
|
+
case "index":
|
|
2847
|
+
case "isNone":
|
|
2848
|
+
case "nonNull":
|
|
2849
|
+
case "optional":
|
|
2850
|
+
case "local":
|
|
2851
|
+
case "localRead":
|
|
2852
|
+
case "localWrite":
|
|
2853
|
+
case "memoryLength":
|
|
2854
|
+
case "load8":
|
|
2855
|
+
case "load16le":
|
|
2856
|
+
case "load16be":
|
|
2857
|
+
case "load32le":
|
|
2858
|
+
case "load32be":
|
|
2859
|
+
case "readSample":
|
|
2860
|
+
case "yield": break;
|
|
2861
|
+
case "read":
|
|
2862
|
+
if (operation.resource === "derived" || operation.name === pcName) return false;
|
|
2863
|
+
break;
|
|
2864
|
+
case "if":
|
|
2865
|
+
if (!visit(operation.yes) || operation.no && !visit(operation.no)) return false;
|
|
2866
|
+
break;
|
|
2867
|
+
case "select":
|
|
2868
|
+
if (!visit(operation.yes.body) || !visit(operation.no.body)) return false;
|
|
2869
|
+
break;
|
|
2870
|
+
case "and":
|
|
2871
|
+
case "or":
|
|
2872
|
+
if (!visit(operation.right.body)) return false;
|
|
2873
|
+
break;
|
|
2874
|
+
case "match":
|
|
2875
|
+
if (operation.arms.some((arm) => !visit(arm.body)) || operation.otherwise && !visit(operation.otherwise)) return false;
|
|
2876
|
+
break;
|
|
2877
|
+
case "redirect":
|
|
2878
|
+
redirects.push(operation.target);
|
|
2879
|
+
break;
|
|
2880
|
+
default: return false;
|
|
2881
|
+
}
|
|
2882
|
+
}
|
|
2883
|
+
return true;
|
|
2884
|
+
};
|
|
2885
|
+
if (!visit(body)) return false;
|
|
2886
|
+
return redirects.some((target) => {
|
|
2887
|
+
const definition = definitions.get(target);
|
|
2888
|
+
return definition?.kind !== "constant" || definition.constant.kind === "number" && definition.constant.value === pc;
|
|
2889
|
+
});
|
|
2890
|
+
}
|
|
2891
|
+
//#endregion
|
|
2892
|
+
//#region src/runtime/jit.ts
|
|
2893
|
+
/** Large programs dispatch through shards; small ones stay on the faster monolithic form. */
|
|
2894
|
+
var DIRECT_SHARD_SIZE = 8;
|
|
2895
|
+
function refreshDirectGuards(jit, hookReads) {
|
|
2896
|
+
let changed = false;
|
|
2897
|
+
const instance = jit.instance;
|
|
2898
|
+
const hookCount = jit.directHookReads.size;
|
|
2899
|
+
if (hookReads) for (const slot of hookReads.keys()) jit.directHookReads.add(slot);
|
|
2900
|
+
if (jit.directObserverRevision === instance.observerRevision && hookCount === jit.directHookReads.size) return;
|
|
2901
|
+
jit.directObserverRevision = instance.observerRevision;
|
|
2902
|
+
for (const [name, slot] of instance.stateResources) {
|
|
2903
|
+
const blocked = slot.subscribers.size > 0 || jit.directHookReads.has(slot);
|
|
2904
|
+
if (jit.directStateGuards.has(name) === blocked) continue;
|
|
2905
|
+
if (blocked) jit.directStateGuards.add(name);
|
|
2906
|
+
else jit.directStateGuards.delete(name);
|
|
2907
|
+
changed = true;
|
|
2908
|
+
}
|
|
2909
|
+
for (const [name, slot] of instance.memoryResources) {
|
|
2910
|
+
const blocked = slot.subscribers.size > 0 || jit.directHookReads.has(slot) || name === jit.options.code;
|
|
2911
|
+
if (jit.directMemoryGuards.has(name) === blocked) continue;
|
|
2912
|
+
if (blocked) jit.directMemoryGuards.add(name);
|
|
2913
|
+
else jit.directMemoryGuards.delete(name);
|
|
2914
|
+
changed = true;
|
|
2915
|
+
}
|
|
2916
|
+
if (changed) {
|
|
2917
|
+
jit.direct = void 0;
|
|
2918
|
+
jit.directRevision = -1;
|
|
2919
|
+
jit.directSkip = void 0;
|
|
2920
|
+
}
|
|
2921
|
+
}
|
|
2922
|
+
/**
|
|
2923
|
+
* Direct programs advance `frame.time` between host calls, while the board only adopts the
|
|
2924
|
+
* final time after `run` returns. Publish the current frame time before every host effect so
|
|
2925
|
+
* channel publications, electrical intents and diagnostics carry the instruction's time.
|
|
2926
|
+
*/
|
|
2927
|
+
function directHost(board, instance, frame) {
|
|
2928
|
+
const publishTime = () => {
|
|
2929
|
+
board.time = frame.time;
|
|
2930
|
+
};
|
|
2931
|
+
return {
|
|
2932
|
+
send: (name, value) => {
|
|
2933
|
+
publishTime();
|
|
2934
|
+
instance.host.send(name, value);
|
|
2935
|
+
},
|
|
2936
|
+
sample: (pin) => {
|
|
2937
|
+
publishTime();
|
|
2938
|
+
return instance.host.sample(pin);
|
|
2939
|
+
},
|
|
2940
|
+
intent: (pin, value) => {
|
|
2941
|
+
publishTime();
|
|
2942
|
+
instance.host.intent(pin, value);
|
|
2943
|
+
},
|
|
2944
|
+
resistor: (pin, resistance) => {
|
|
2945
|
+
publishTime();
|
|
2946
|
+
return instance.host.resistor(pin, resistance);
|
|
2947
|
+
},
|
|
2948
|
+
notify: (name) => {
|
|
2949
|
+
publishTime();
|
|
2950
|
+
instance.host.notify(name);
|
|
2951
|
+
},
|
|
2952
|
+
now: () => {
|
|
2953
|
+
publishTime();
|
|
2954
|
+
return instance.host.now();
|
|
2955
|
+
},
|
|
2956
|
+
random: () => {
|
|
2957
|
+
publishTime();
|
|
2958
|
+
return instance.host.random();
|
|
2959
|
+
},
|
|
2960
|
+
assert: (condition, message) => {
|
|
2961
|
+
publishTime();
|
|
2962
|
+
instance.host.assert(condition, message);
|
|
2963
|
+
}
|
|
2964
|
+
};
|
|
2965
|
+
}
|
|
2966
|
+
function createDirectFrame(board, instance, stateNames, memoryNames, program) {
|
|
2967
|
+
const frame = {
|
|
2968
|
+
states: new Array(stateNames.length),
|
|
2969
|
+
memories: memoryNames.map((name) => instance.memory(name)),
|
|
2970
|
+
locals: new Array(program.localCount),
|
|
2971
|
+
stateDirty: new Array(Math.ceil(stateNames.length / 32)).fill(0),
|
|
2972
|
+
memoryDirty: new Array(Math.ceil(memoryNames.length / 32)).fill(0),
|
|
2973
|
+
time: board.time,
|
|
2974
|
+
due: board.time,
|
|
2975
|
+
resume: 0
|
|
2976
|
+
};
|
|
2977
|
+
frame.host = directHost(board, instance, frame);
|
|
2978
|
+
return frame;
|
|
2979
|
+
}
|
|
2980
|
+
function runDirect(board, jit, hookReads) {
|
|
2981
|
+
const instance = jit.instance;
|
|
2982
|
+
const pending = Boolean(jit.direct?.frame.resume);
|
|
2983
|
+
if (!pending) {
|
|
2984
|
+
const pc = finite(instance.states.get(jit.options.pc), "pc");
|
|
2985
|
+
if (board.execution !== "throughput" || board.jits.length !== 1 || jit.polling.has(pc) || jit.cache.get(pc)?.polling) return false;
|
|
2986
|
+
refreshDirectGuards(jit, hookReads);
|
|
2987
|
+
if (jit.directSkip === pc) return false;
|
|
2988
|
+
if (jit.directRevision !== jit.revision) {
|
|
2989
|
+
if (jit.directProgramRevision !== jit.revision) {
|
|
2990
|
+
jit.directPrograms.clear();
|
|
2991
|
+
jit.directProgramRevision = jit.revision;
|
|
2992
|
+
}
|
|
2993
|
+
const key = JSON.stringify([[...jit.directStateGuards].sort(), [...jit.directMemoryGuards].sort()]);
|
|
2994
|
+
const cached = jit.directPrograms.get(key);
|
|
2995
|
+
if (cached) {
|
|
2996
|
+
jit.direct = cached;
|
|
2997
|
+
jit.directRevision = jit.revision;
|
|
2998
|
+
} else {
|
|
2999
|
+
const stateNames = [...instance.states.keys()].filter((name) => {
|
|
3000
|
+
const declaration = instance.declarations.get(name);
|
|
3001
|
+
return declaration.type.kind === "number" || declaration.type.kind === "bool";
|
|
3002
|
+
});
|
|
3003
|
+
const memoryNames = [...instance.memories.keys()];
|
|
3004
|
+
const states = new Map(stateNames.map((name, index) => [name, index]));
|
|
3005
|
+
const memories = new Map(memoryNames.map((name, index) => [name, index]));
|
|
3006
|
+
const instructions = [...jit.cache.values()].filter((instruction) => !instruction.polling && !jit.polling.has(instruction.pc));
|
|
3007
|
+
const program = compileDirectJit(instructions, {
|
|
3008
|
+
states,
|
|
3009
|
+
memories,
|
|
3010
|
+
pc: {
|
|
3011
|
+
name: jit.options.pc,
|
|
3012
|
+
index: states.get(jit.options.pc)
|
|
3013
|
+
},
|
|
3014
|
+
blockedStates: jit.directStateGuards,
|
|
3015
|
+
blockedMemories: jit.directMemoryGuards,
|
|
3016
|
+
host: true,
|
|
3017
|
+
split: true,
|
|
3018
|
+
shardSize: DIRECT_SHARD_SIZE
|
|
3019
|
+
});
|
|
3020
|
+
jit.directRevision = jit.revision;
|
|
3021
|
+
jit.direct = program ? {
|
|
3022
|
+
program,
|
|
3023
|
+
stateReads: program.readStates.map((name) => [name, states.get(name)]),
|
|
3024
|
+
stateWrites: program.writeStates.map((name) => [name, states.get(name)]),
|
|
3025
|
+
memoryWrites: program.writeMemories.map((name) => [name, memories.get(name)]),
|
|
3026
|
+
revision: jit.revision,
|
|
3027
|
+
frame: createDirectFrame(board, instance, stateNames, memoryNames, program)
|
|
3028
|
+
} : void 0;
|
|
3029
|
+
if (!jit.direct) jit.directPrograms.delete(key);
|
|
3030
|
+
else {
|
|
3031
|
+
if (jit.directPrograms.size >= 8) jit.directPrograms.delete(jit.directPrograms.keys().next().value);
|
|
3032
|
+
jit.directPrograms.set(key, jit.direct);
|
|
3033
|
+
}
|
|
3034
|
+
}
|
|
3035
|
+
}
|
|
3036
|
+
if (!jit.direct) return false;
|
|
3037
|
+
}
|
|
3038
|
+
const direct = jit.direct;
|
|
3039
|
+
const frame = direct.frame;
|
|
3040
|
+
if (pending) frame.stopAfterInstruction = true;
|
|
3041
|
+
for (const [name, index] of direct.stateReads) frame.states[index] = instance.states.get(name);
|
|
3042
|
+
frame.time = board.time;
|
|
3043
|
+
frame.stateDirty.fill(0);
|
|
3044
|
+
frame.memoryDirty.fill(0);
|
|
3045
|
+
const start = board.time;
|
|
3046
|
+
let reason;
|
|
3047
|
+
try {
|
|
3048
|
+
reason = direct.program.run(frame, Math.min(board.executionEnd, board.events.time));
|
|
3049
|
+
} finally {
|
|
3050
|
+
board.time = frame.time;
|
|
3051
|
+
for (const [name, index] of direct.stateWrites) if ((frame.stateDirty[index >>> 5] & 1 << (index & 31)) !== 0) instance.host.write(name, frame.states[index]);
|
|
3052
|
+
for (const [name, index] of direct.memoryWrites) if ((frame.memoryDirty[index >>> 5] & 1 << (index & 31)) !== 0) instance.memoryChanged(name);
|
|
3053
|
+
}
|
|
3054
|
+
board.boundary();
|
|
3055
|
+
if (reason === "fallback") {
|
|
3056
|
+
jit.directSkip = finite(instance.states.get(jit.options.pc), "pc");
|
|
3057
|
+
if (board.time === start && board.time < board.executionEnd) return false;
|
|
3058
|
+
} else if (reason === "stopped") {
|
|
3059
|
+
jit.stopped = true;
|
|
3060
|
+
return true;
|
|
3061
|
+
} else if (reason === "invalidated") {
|
|
3062
|
+
frame.stopAfterInstruction = false;
|
|
3063
|
+
if (direct.revision !== jit.revision) {
|
|
1659
3064
|
jit.direct = void 0;
|
|
1660
|
-
jit.
|
|
1661
|
-
}
|
|
1662
|
-
this.resources.push(disposable);
|
|
1663
|
-
return disposable;
|
|
3065
|
+
jit.directRevision = -1;
|
|
3066
|
+
}
|
|
1664
3067
|
}
|
|
1665
|
-
|
|
1666
|
-
|
|
1667
|
-
|
|
1668
|
-
|
|
1669
|
-
|
|
1670
|
-
|
|
1671
|
-
|
|
1672
|
-
|
|
1673
|
-
|
|
3068
|
+
if (frame.resume && frame.due > board.time) board.events.push(frame.due, () => board.queueJit(jit));
|
|
3069
|
+
else board.queueJit(jit);
|
|
3070
|
+
return true;
|
|
3071
|
+
}
|
|
3072
|
+
function pollingDependencies(jit, dependencies) {
|
|
3073
|
+
const result = new Map(dependencies);
|
|
3074
|
+
const instance = jit.instance;
|
|
3075
|
+
for (const slot of [instance.stateResources.get(jit.options.pc), instance.memoryResources.get(jit.options.code)]) result.set(slot, slot.version);
|
|
3076
|
+
return result;
|
|
3077
|
+
}
|
|
3078
|
+
function jitSignal(board, jit, signal) {
|
|
3079
|
+
if (signal.kind === "elapse") {
|
|
3080
|
+
const duration = time(signal.duration);
|
|
3081
|
+
if (duration === 0) return false;
|
|
3082
|
+
const due = time(board.time + duration);
|
|
3083
|
+
board.events.push(due, () => board.queueJit(jit));
|
|
3084
|
+
board.boundary();
|
|
3085
|
+
return true;
|
|
3086
|
+
}
|
|
3087
|
+
if (signal.kind === "waitUntil") {
|
|
3088
|
+
const checked = jit.instance.evaluate(() => jit.instance.syncCall(signal.predicate));
|
|
3089
|
+
if (checked.value === true) return false;
|
|
3090
|
+
jit.predicate = signal.predicate;
|
|
3091
|
+
jit.pollingWait = false;
|
|
3092
|
+
jit.dependencies = checked.dependencies;
|
|
3093
|
+
board.waitingJits.add(jit);
|
|
3094
|
+
board.boundary();
|
|
3095
|
+
return true;
|
|
3096
|
+
}
|
|
3097
|
+
if (signal.kind === "stop") {
|
|
3098
|
+
jit.stopped = true;
|
|
3099
|
+
jit.execution = void 0;
|
|
3100
|
+
board.boundary();
|
|
3101
|
+
return true;
|
|
3102
|
+
}
|
|
3103
|
+
if (signal.kind === "redirect") {
|
|
3104
|
+
finite(signal.pc, "redirect target");
|
|
3105
|
+
const duration = time(signal.delay);
|
|
3106
|
+
jit.instance.host.write(jit.options.pc, signal.pc);
|
|
3107
|
+
jit.execution = void 0;
|
|
3108
|
+
const polling = jit.automaticPolling && duration > 0 && signal.pc === jit.currentPc && jit.pollReads;
|
|
3109
|
+
const dependencies = polling ? pollingDependencies(jit, polling) : void 0;
|
|
3110
|
+
jit.automaticPolling = false;
|
|
3111
|
+
jit.pollReads = void 0;
|
|
3112
|
+
board.boundary();
|
|
3113
|
+
if (duration === 0) board.queueJit(jit);
|
|
3114
|
+
else board.events.push(time(board.time + duration), () => {
|
|
3115
|
+
if (!jit.active || jit.stopped) return;
|
|
3116
|
+
if (dependencies && jit.instance.dependenciesCurrent(dependencies)) {
|
|
3117
|
+
jit.dependencies = dependencies;
|
|
3118
|
+
jit.pollingWait = true;
|
|
3119
|
+
jit.pollingPeriod = duration;
|
|
3120
|
+
jit.pollingOrigin = board.time;
|
|
3121
|
+
board.waitingJits.add(jit);
|
|
3122
|
+
} else board.queueJit(jit);
|
|
3123
|
+
});
|
|
3124
|
+
return true;
|
|
3125
|
+
}
|
|
3126
|
+
throw new TypeError("Invalid JIT suspension");
|
|
3127
|
+
}
|
|
3128
|
+
function runJit(board, jit) {
|
|
3129
|
+
jit.queued = false;
|
|
3130
|
+
if (!jit.active || jit.stopped) return;
|
|
3131
|
+
board.current = jit.instance;
|
|
3132
|
+
if (jit.direct?.frame.resume) {
|
|
3133
|
+
runDirect(board, jit);
|
|
3134
|
+
return;
|
|
3135
|
+
}
|
|
3136
|
+
if (!jit.execution) {
|
|
3137
|
+
jit.automaticPolling = false;
|
|
3138
|
+
jit.pollReads = void 0;
|
|
3139
|
+
let hookReads;
|
|
3140
|
+
let hookPending = false;
|
|
3141
|
+
const hook = jit.options.beforeInstruction;
|
|
3142
|
+
if (hook) {
|
|
3143
|
+
const kind = jit.instance.declarations.get(hook.when).kind;
|
|
3144
|
+
const priorPc = jit.instance.states.get(jit.options.pc);
|
|
3145
|
+
const checked = board.execution === "throughput" && (board.jits.length === 1 || jit.polling.has(priorPc)) ? jit.instance.evaluate(() => jit.instance.read(kind, hook.when)) : void 0;
|
|
3146
|
+
const handled = (checked ? checked.value : jit.instance.read(kind, hook.when)) === true;
|
|
3147
|
+
if (handled) {
|
|
3148
|
+
const execution = jit.instance.invoke(hook.call);
|
|
3149
|
+
const step = execution.next();
|
|
3150
|
+
if (!step.done) {
|
|
3151
|
+
jit.execution = execution;
|
|
3152
|
+
if (jitSignal(board, jit, step.value)) return;
|
|
3153
|
+
throw new TypeError("Invalid hook suspension");
|
|
3154
|
+
}
|
|
3155
|
+
}
|
|
3156
|
+
hookReads = checked?.dependencies;
|
|
3157
|
+
hookPending = handled;
|
|
3158
|
+
}
|
|
3159
|
+
const pc = finite(jit.instance.states.get(jit.options.pc), "pc");
|
|
3160
|
+
if (board.execution === "throughput") {
|
|
3161
|
+
const predicate = jit.polling.get(pc);
|
|
3162
|
+
if (predicate) {
|
|
3163
|
+
const checked = jit.instance.evaluate(() => jit.instance.syncCall(predicate));
|
|
3164
|
+
if (checked.value !== true) {
|
|
3165
|
+
jit.dependencies = pollingDependencies(jit, new Map([...hookReads ?? [], ...checked.dependencies]));
|
|
3166
|
+
jit.pollingWait = true;
|
|
3167
|
+
jit.pollingPeriod = 0;
|
|
3168
|
+
board.waitingJits.add(jit);
|
|
3169
|
+
board.boundary();
|
|
3170
|
+
return;
|
|
3171
|
+
}
|
|
3172
|
+
}
|
|
3173
|
+
if (!hookPending && board.jits.length === 1 && runDirect(board, jit, hookReads)) return;
|
|
3174
|
+
}
|
|
3175
|
+
let instruction = jit.cache.get(pc);
|
|
3176
|
+
if (!instruction) {
|
|
3177
|
+
const prior = board.phase;
|
|
3178
|
+
board.phase = "translate";
|
|
3179
|
+
let valid = true;
|
|
3180
|
+
try {
|
|
3181
|
+
const code = bytesView(jit.instance.memory(jit.options.code), () => {
|
|
3182
|
+
if (!valid) throw board.error("OBJECT_DISPOSED", "Translation view expired", jit.instance);
|
|
3183
|
+
});
|
|
3184
|
+
const result = jit.options.translate(Object.freeze({
|
|
3185
|
+
pc,
|
|
3186
|
+
code
|
|
3187
|
+
}), createJitBlockBuilder({ device: jit.instance.entry.model }));
|
|
3188
|
+
acceptSync(result);
|
|
3189
|
+
const block = validateBoundBlockIR(result, jit.instance.entry.model);
|
|
3190
|
+
if (block.instructions[0]?.pc !== pc) throw new TypeError("Translated first pc does not match");
|
|
3191
|
+
const instructions = block.instructions.map((instruction) => ({
|
|
3192
|
+
pc: instruction.pc,
|
|
3193
|
+
nextPc: instruction.nextPc,
|
|
3194
|
+
body: instruction.body,
|
|
3195
|
+
polling: isPollingInstruction(instruction.body, instruction.pc, jit.options.pc),
|
|
3196
|
+
execute: body(instruction.body)
|
|
3197
|
+
}));
|
|
3198
|
+
for (let index = instructions.length - 1; index >= 0; index--) {
|
|
3199
|
+
const entry = instructions[index];
|
|
3200
|
+
jit.cache.set(entry.pc, entry);
|
|
3201
|
+
}
|
|
3202
|
+
jit.revision++;
|
|
3203
|
+
jit.directSkip = void 0;
|
|
3204
|
+
instruction = instructions[0];
|
|
3205
|
+
} catch (cause) {
|
|
3206
|
+
throw board.wrap(cause, "JIT_FAILED", "translate", jit.instance);
|
|
3207
|
+
} finally {
|
|
3208
|
+
valid = false;
|
|
3209
|
+
board.phase = prior;
|
|
3210
|
+
}
|
|
1674
3211
|
}
|
|
3212
|
+
jit.currentPc = pc;
|
|
3213
|
+
jit.automaticPolling = board.execution === "throughput" && board.jits.length === 1 && instruction.polling && !hookPending && (!hook || hookReads !== void 0);
|
|
3214
|
+
jit.pollReads = hookReads;
|
|
3215
|
+
jit.instance.host.write(jit.options.pc, instruction.nextPc);
|
|
3216
|
+
jit.execution = instruction.execute(jit.instance.host);
|
|
1675
3217
|
}
|
|
1676
|
-
|
|
1677
|
-
const
|
|
1678
|
-
|
|
1679
|
-
|
|
1680
|
-
|
|
1681
|
-
|
|
1682
|
-
|
|
1683
|
-
|
|
1684
|
-
|
|
1685
|
-
|
|
1686
|
-
|
|
1687
|
-
|
|
1688
|
-
|
|
3218
|
+
for (;;) {
|
|
3219
|
+
const step = jit.automaticPolling ? (() => {
|
|
3220
|
+
const checked = jit.instance.evaluate(() => jit.execution.next());
|
|
3221
|
+
jit.pollReads = new Map([...jit.pollReads ?? [], ...checked.dependencies]);
|
|
3222
|
+
return checked.value;
|
|
3223
|
+
})() : jit.execution.next();
|
|
3224
|
+
if (step.done) {
|
|
3225
|
+
jit.execution = void 0;
|
|
3226
|
+
board.boundary();
|
|
3227
|
+
board.queueJit(jit);
|
|
3228
|
+
return;
|
|
3229
|
+
}
|
|
3230
|
+
if (jitSignal(board, jit, step.value)) return;
|
|
1689
3231
|
}
|
|
1690
|
-
}
|
|
3232
|
+
}
|
|
3233
|
+
//#endregion
|
|
3234
|
+
//#region src/runtime.ts
|
|
3235
|
+
var OBSERVATION_PROBE = Symbol.for("mica-js.simulator/observations");
|
|
3236
|
+
var PROCESS_PROBE = Symbol.for("mica-js.simulator/processes");
|
|
3237
|
+
var EMPTY_QUEUE_ENTRY = Object.freeze({});
|
|
3238
|
+
var libraries = /* @__PURE__ */ new WeakMap();
|
|
3239
|
+
function createDeviceLibrary(entries) {
|
|
3240
|
+
try {
|
|
3241
|
+
if (!Array.isArray(entries)) throw new TypeError("Expected device entries");
|
|
3242
|
+
const seen = /* @__PURE__ */ new Set();
|
|
3243
|
+
const captured = entries.map((entry) => {
|
|
3244
|
+
dataObject(entry, [
|
|
3245
|
+
"id",
|
|
3246
|
+
"model",
|
|
3247
|
+
"create"
|
|
3248
|
+
]);
|
|
3249
|
+
const id = identity(entry.id);
|
|
3250
|
+
if (seen.has(id)) throw new TypeError(`Duplicate device type ${id}`);
|
|
3251
|
+
seen.add(id);
|
|
3252
|
+
if (typeof entry.create !== "function") throw new TypeError("Expected device factory");
|
|
3253
|
+
let model;
|
|
3254
|
+
try {
|
|
3255
|
+
model = validateDeviceIR(entry.model);
|
|
3256
|
+
} catch (cause) {
|
|
3257
|
+
throw standalone(cause instanceof BuildError && Object.getOwnPropertyDescriptor(cause, "code")?.value === "CHANNEL_CONSUMER_CONFLICT" ? "CHANNEL_CONSUMER_CONFLICT" : "MODEL_VALIDATION_FAILED", cause, "library");
|
|
3258
|
+
}
|
|
3259
|
+
return Object.freeze({
|
|
3260
|
+
id,
|
|
3261
|
+
model,
|
|
3262
|
+
create: entry.create
|
|
3263
|
+
});
|
|
3264
|
+
});
|
|
3265
|
+
const library = Object.freeze({});
|
|
3266
|
+
libraries.set(library, captured);
|
|
3267
|
+
return library;
|
|
3268
|
+
} catch (cause) {
|
|
3269
|
+
if (cause instanceof SimulationError) throw cause;
|
|
3270
|
+
throw standalone("INVALID_CONFIGURATION", cause, "library");
|
|
3271
|
+
}
|
|
3272
|
+
}
|
|
1691
3273
|
var BoardRuntime = class {
|
|
1692
3274
|
time = 0;
|
|
1693
3275
|
status = "ready";
|
|
1694
3276
|
phase = "assembly";
|
|
1695
3277
|
current;
|
|
1696
3278
|
electricalDirty = true;
|
|
1697
|
-
|
|
3279
|
+
/** Dirty electrical slots: the publish plan maps them straight onto sample slots. */
|
|
3280
|
+
dirtyElectricalSlots = /* @__PURE__ */ new Set();
|
|
1698
3281
|
observations = new ActivationQueue();
|
|
1699
3282
|
reactionBatch = [];
|
|
3283
|
+
changeObservations = [];
|
|
3284
|
+
changePrevious = [];
|
|
3285
|
+
changeCurrent = [];
|
|
3286
|
+
plans = [];
|
|
3287
|
+
skippedPlans = [];
|
|
1700
3288
|
sampleSlots = /* @__PURE__ */ new Map();
|
|
3289
|
+
/** Electrical slot → published sample slot, bound once so publishing needs no lookup. */
|
|
3290
|
+
sampleSlotsBySlot = [];
|
|
3291
|
+
/** Electrical endpoint → its board slot, used to record dirty pins by slot. */
|
|
3292
|
+
electricalSlots = /* @__PURE__ */ new Map();
|
|
1701
3293
|
byId = /* @__PURE__ */ new Map();
|
|
1702
3294
|
dispatchDepth = 0;
|
|
1703
3295
|
initialized = false;
|
|
@@ -1809,8 +3401,11 @@ var BoardRuntime = class {
|
|
|
1809
3401
|
}));
|
|
1810
3402
|
const pins = [];
|
|
1811
3403
|
for (const instance of this.instances) for (const pin of instance.pins.values()) {
|
|
3404
|
+
const slot = instance.sampleResources.get(pin.name);
|
|
1812
3405
|
pins.push(pin);
|
|
1813
|
-
this.sampleSlots.set(pin,
|
|
3406
|
+
this.sampleSlots.set(pin, slot);
|
|
3407
|
+
this.electricalSlots.set(pin, this.sampleSlotsBySlot.length);
|
|
3408
|
+
this.sampleSlotsBySlot.push(slot);
|
|
1814
3409
|
}
|
|
1815
3410
|
this.network = createElectricalNetwork(pins, nets);
|
|
1816
3411
|
this.phase = "initialize";
|
|
@@ -1829,6 +3424,14 @@ var BoardRuntime = class {
|
|
|
1829
3424
|
}
|
|
1830
3425
|
this.current = void 0;
|
|
1831
3426
|
this.phase = "idle";
|
|
3427
|
+
Object.defineProperty(this, OBSERVATION_PROBE, {
|
|
3428
|
+
value: () => this.observationReport(),
|
|
3429
|
+
enumerable: false
|
|
3430
|
+
});
|
|
3431
|
+
Object.defineProperty(this, PROCESS_PROBE, {
|
|
3432
|
+
value: () => this.processReport(),
|
|
3433
|
+
enumerable: false
|
|
3434
|
+
});
|
|
1832
3435
|
} catch (cause) {
|
|
1833
3436
|
const error = cause instanceof SimulationError ? cause : this.wrap(cause, "INVALID_CONFIGURATION", "assembly", this.current);
|
|
1834
3437
|
this.primary = error;
|
|
@@ -1839,6 +3442,41 @@ var BoardRuntime = class {
|
|
|
1839
3442
|
get isFaulted() {
|
|
1840
3443
|
return this.status === "faulted";
|
|
1841
3444
|
}
|
|
3445
|
+
/**
|
|
3446
|
+
* Read-only diagnostic view of generated observation chains. It is reachable only via
|
|
3447
|
+
* `Symbol.for('mica-js.simulator/observations')` and is not part of the public SDK.
|
|
3448
|
+
*/
|
|
3449
|
+
observationReport() {
|
|
3450
|
+
return Object.freeze({
|
|
3451
|
+
plans: Object.freeze(this.plans.map((chain) => Object.freeze({
|
|
3452
|
+
key: chain.plan.key,
|
|
3453
|
+
path: Object.freeze([...chain.plan.path]),
|
|
3454
|
+
receivers: chain.members.length,
|
|
3455
|
+
slots: chain.plan.slots.length,
|
|
3456
|
+
source: chain.plan.source,
|
|
3457
|
+
comparison: chain.plan.comparison
|
|
3458
|
+
}))),
|
|
3459
|
+
skipped: Object.freeze(this.skippedPlans.map((entry) => Object.freeze({ ...entry })))
|
|
3460
|
+
});
|
|
3461
|
+
}
|
|
3462
|
+
/**
|
|
3463
|
+
* Read-only diagnostic view of process execution: bodies admitted as resumable blocks
|
|
3464
|
+
* versus bodies kept on the generator path. Reachable only via
|
|
3465
|
+
* `Symbol.for('mica-js.simulator/processes')`; not part of the public SDK.
|
|
3466
|
+
*/
|
|
3467
|
+
processReport() {
|
|
3468
|
+
const resumable = [];
|
|
3469
|
+
const generic = [];
|
|
3470
|
+
for (const instance of this.instances) for (const process of instance.processes) {
|
|
3471
|
+
const key = `${instance.id}/${process.declaration.kind === "on" ? process.declaration.name : "start"}`;
|
|
3472
|
+
(process.resumable ? resumable : generic).push(key);
|
|
3473
|
+
}
|
|
3474
|
+
return Object.freeze({
|
|
3475
|
+
resumable: Object.freeze(resumable),
|
|
3476
|
+
generic: Object.freeze(generic),
|
|
3477
|
+
bindings: Object.freeze(this.instances.map((instance) => instance.bindingReport()))
|
|
3478
|
+
});
|
|
3479
|
+
}
|
|
1842
3480
|
validity(instance) {
|
|
1843
3481
|
if (this.status === "disposed" || instance && !instance.valid) throw this.error("OBJECT_DISPOSED", "Object is disposed", instance);
|
|
1844
3482
|
}
|
|
@@ -1974,14 +3612,19 @@ var BoardRuntime = class {
|
|
|
1974
3612
|
}
|
|
1975
3613
|
invalidate(slot) {
|
|
1976
3614
|
slot.version++;
|
|
3615
|
+
if (slot.subscribers.size === 0) return;
|
|
1977
3616
|
for (const observation of slot.subscribers) this.observations.add(observation.order);
|
|
1978
3617
|
}
|
|
3618
|
+
/** Pin slot of an electrical endpoint; bound once because the board order is fixed. */
|
|
3619
|
+
electricalSlot(pin) {
|
|
3620
|
+
return this.electricalSlots.get(pin);
|
|
3621
|
+
}
|
|
1979
3622
|
publish() {
|
|
1980
3623
|
if (!this.electricalDirty) return;
|
|
1981
|
-
const changed = this.network.
|
|
1982
|
-
this.
|
|
3624
|
+
const changed = this.network.solveSlots(this.dirtyElectricalSlots);
|
|
3625
|
+
this.dirtyElectricalSlots.clear();
|
|
1983
3626
|
this.electricalDirty = false;
|
|
1984
|
-
for (
|
|
3627
|
+
for (let index = 0; index < changed.length; index++) this.invalidate(this.sampleSlotsBySlot[changed[index]]);
|
|
1985
3628
|
}
|
|
1986
3629
|
bindObservation(observation, path) {
|
|
1987
3630
|
const instance = observation.instance;
|
|
@@ -1989,11 +3632,141 @@ var BoardRuntime = class {
|
|
|
1989
3632
|
observation.readSource = instance.observationReader(observation, path);
|
|
1990
3633
|
observation.order = this.observations.register(observation);
|
|
1991
3634
|
}
|
|
3635
|
+
/**
|
|
3636
|
+
* Static sources whose root is a derived declaration are grouped into one generated
|
|
3637
|
+
* chain per (instance, path): the reader, the stable comparator and the dependency
|
|
3638
|
+
* slots are bound once at baseline and shared by every receiver. Every other source
|
|
3639
|
+
* keeps the generic path.
|
|
3640
|
+
*/
|
|
1992
3641
|
baseline() {
|
|
1993
3642
|
this.publish();
|
|
3643
|
+
const entries = [];
|
|
1994
3644
|
for (const instance of this.instances) {
|
|
1995
|
-
|
|
1996
|
-
for (const
|
|
3645
|
+
const prefix = `${instance.id}\0`;
|
|
3646
|
+
for (const process of instance.processes) if (process.source && instance.declarations.get(process.source[0])?.kind !== "notification") entries.push({
|
|
3647
|
+
observation: process,
|
|
3648
|
+
path: process.source,
|
|
3649
|
+
key: prefix + process.source.join("\0"),
|
|
3650
|
+
instance
|
|
3651
|
+
});
|
|
3652
|
+
for (const reaction of instance.reactions) {
|
|
3653
|
+
const path = reaction.declaration.sourcePath;
|
|
3654
|
+
entries.push({
|
|
3655
|
+
observation: reaction,
|
|
3656
|
+
path,
|
|
3657
|
+
key: prefix + path.join("\0"),
|
|
3658
|
+
instance
|
|
3659
|
+
});
|
|
3660
|
+
}
|
|
3661
|
+
}
|
|
3662
|
+
const groups = /* @__PURE__ */ new Map();
|
|
3663
|
+
for (const entry of entries) {
|
|
3664
|
+
if (groups.has(entry.key)) continue;
|
|
3665
|
+
const compilation = compileObservation(entry.instance, entry.key, entry.path);
|
|
3666
|
+
if (!compilation.ok) {
|
|
3667
|
+
this.skippedPlans.push({
|
|
3668
|
+
key: entry.key,
|
|
3669
|
+
reason: compilation.reason
|
|
3670
|
+
});
|
|
3671
|
+
groups.set(entry.key, void 0);
|
|
3672
|
+
continue;
|
|
3673
|
+
}
|
|
3674
|
+
const chain = {
|
|
3675
|
+
plan: compilation.plan,
|
|
3676
|
+
instance: entry.instance,
|
|
3677
|
+
members: [],
|
|
3678
|
+
dependencies: /* @__PURE__ */ new Map(),
|
|
3679
|
+
entry: {
|
|
3680
|
+
dependencies: /* @__PURE__ */ new Map(),
|
|
3681
|
+
value: void 0
|
|
3682
|
+
},
|
|
3683
|
+
order: 0,
|
|
3684
|
+
previous: void 0,
|
|
3685
|
+
bound: false
|
|
3686
|
+
};
|
|
3687
|
+
groups.set(entry.key, chain);
|
|
3688
|
+
this.plans.push(chain);
|
|
3689
|
+
}
|
|
3690
|
+
for (const entry of entries) {
|
|
3691
|
+
const chain = groups.get(entry.key);
|
|
3692
|
+
if (chain === void 0) {
|
|
3693
|
+
this.bindObservation(entry.observation, entry.path);
|
|
3694
|
+
continue;
|
|
3695
|
+
}
|
|
3696
|
+
if (chain.bound) entry.observation.order = this.observations.register(EMPTY_QUEUE_ENTRY);
|
|
3697
|
+
else {
|
|
3698
|
+
chain.bound = true;
|
|
3699
|
+
chain.order = this.observations.register(chain);
|
|
3700
|
+
entry.observation.order = chain.order;
|
|
3701
|
+
this.bindChain(chain);
|
|
3702
|
+
}
|
|
3703
|
+
chain.members.push(entry.observation);
|
|
3704
|
+
entry.observation.previous = chain.previous;
|
|
3705
|
+
}
|
|
3706
|
+
}
|
|
3707
|
+
bindChain(chain) {
|
|
3708
|
+
const instance = chain.instance;
|
|
3709
|
+
for (const slot of chain.plan.slots) {
|
|
3710
|
+
if (!slot.subscribers.size) instance.observerRevision++;
|
|
3711
|
+
slot.subscribers.add(chain);
|
|
3712
|
+
}
|
|
3713
|
+
const value = chain.plan.evaluate();
|
|
3714
|
+
chain.previous = chain.plan.needsSnapshot ? snapshot(value) : value;
|
|
3715
|
+
this.commitChain(chain, value);
|
|
3716
|
+
}
|
|
3717
|
+
/** Keep the derived cache populated with the value the chain just computed. */
|
|
3718
|
+
commitChain(chain, value) {
|
|
3719
|
+
const path = chain.plan.path;
|
|
3720
|
+
if (path.length !== 1) return;
|
|
3721
|
+
for (const slot of chain.plan.slots) chain.dependencies.set(slot, slot.version);
|
|
3722
|
+
const entry = chain.entry;
|
|
3723
|
+
if (entry.value !== value) entry.value = value;
|
|
3724
|
+
entry.dependencies = chain.dependencies;
|
|
3725
|
+
if (chain.instance.derived.get(path[0]) !== entry) chain.instance.derived.set(path[0], entry);
|
|
3726
|
+
}
|
|
3727
|
+
collectChain(chain) {
|
|
3728
|
+
const value = chain.plan.evaluate();
|
|
3729
|
+
const current = chain.plan.needsSnapshot ? snapshot(value) : value;
|
|
3730
|
+
this.commitChain(chain, value);
|
|
3731
|
+
if (chain.plan.equals(chain.previous, current)) return;
|
|
3732
|
+
const previous = chain.previous;
|
|
3733
|
+
chain.previous = current;
|
|
3734
|
+
const members = chain.members;
|
|
3735
|
+
const member = members[0];
|
|
3736
|
+
if (members.length === 1 && !this.changeObservations.length) {
|
|
3737
|
+
member.previous = current;
|
|
3738
|
+
this.applyChange(member, previous, current);
|
|
3739
|
+
return;
|
|
3740
|
+
}
|
|
3741
|
+
for (const entry of members) {
|
|
3742
|
+
entry.previous = current;
|
|
3743
|
+
this.recordChange(entry, previous, current);
|
|
3744
|
+
}
|
|
3745
|
+
}
|
|
3746
|
+
applyChange(observation, previous, current) {
|
|
3747
|
+
if (observation.declaration.kind === "reaction") {
|
|
3748
|
+
const reaction = observation;
|
|
3749
|
+
reaction.frame = {
|
|
3750
|
+
previous,
|
|
3751
|
+
current,
|
|
3752
|
+
captures: []
|
|
3753
|
+
};
|
|
3754
|
+
this.reactionBatch.push(reaction);
|
|
3755
|
+
} else this.enqueue(observation);
|
|
3756
|
+
}
|
|
3757
|
+
recordChange(observation, previous, current) {
|
|
3758
|
+
const list = this.changeObservations;
|
|
3759
|
+
let at = list.length;
|
|
3760
|
+
const order = observation.order;
|
|
3761
|
+
while (at > 0 && list[at - 1].order > order) at--;
|
|
3762
|
+
if (at === list.length) {
|
|
3763
|
+
list.push(observation);
|
|
3764
|
+
this.changePrevious.push(previous);
|
|
3765
|
+
this.changeCurrent.push(current);
|
|
3766
|
+
} else {
|
|
3767
|
+
list.splice(at, 0, observation);
|
|
3768
|
+
this.changePrevious.splice(at, 0, previous);
|
|
3769
|
+
this.changeCurrent.splice(at, 0, current);
|
|
1997
3770
|
}
|
|
1998
3771
|
}
|
|
1999
3772
|
boundary() {
|
|
@@ -2002,21 +3775,31 @@ var BoardRuntime = class {
|
|
|
2002
3775
|
if (!this.observations.size) return;
|
|
2003
3776
|
const reactions = this.reactionBatch;
|
|
2004
3777
|
reactions.length = 0;
|
|
3778
|
+
const changes = this.changeObservations;
|
|
3779
|
+
changes.length = 0;
|
|
3780
|
+
this.changePrevious.length = 0;
|
|
3781
|
+
this.changeCurrent.length = 0;
|
|
2005
3782
|
try {
|
|
2006
|
-
|
|
3783
|
+
const batch = this.observations.take();
|
|
3784
|
+
for (let index = 0; index < batch.length; index++) {
|
|
3785
|
+
const observation = batch[index];
|
|
3786
|
+
if ("plan" in observation) {
|
|
3787
|
+
this.collectChain(observation);
|
|
3788
|
+
continue;
|
|
3789
|
+
}
|
|
2007
3790
|
const current = observation.readSource();
|
|
2008
3791
|
if (same(observation.previous, current)) continue;
|
|
2009
3792
|
const previous = observation.previous;
|
|
2010
3793
|
observation.previous = current;
|
|
2011
|
-
if (
|
|
2012
|
-
|
|
2013
|
-
|
|
2014
|
-
|
|
2015
|
-
|
|
2016
|
-
|
|
2017
|
-
|
|
2018
|
-
|
|
2019
|
-
|
|
3794
|
+
if (changes.length) this.recordChange(observation, previous, current);
|
|
3795
|
+
else this.applyChange(observation, previous, current);
|
|
3796
|
+
}
|
|
3797
|
+
const count = changes.length;
|
|
3798
|
+
if (count) {
|
|
3799
|
+
for (let index = 0; index < count; index++) this.applyChange(changes[index], this.changePrevious[index], this.changeCurrent[index]);
|
|
3800
|
+
changes.length = 0;
|
|
3801
|
+
this.changePrevious.length = 0;
|
|
3802
|
+
this.changeCurrent.length = 0;
|
|
2020
3803
|
}
|
|
2021
3804
|
if (!reactions.length) return;
|
|
2022
3805
|
for (const reaction of reactions) if (reaction.declaration.captures.length) reaction.frame = {
|
|
@@ -2034,6 +3817,11 @@ var BoardRuntime = class {
|
|
|
2034
3817
|
}
|
|
2035
3818
|
}
|
|
2036
3819
|
drive(process, value) {
|
|
3820
|
+
const program = process.resumable;
|
|
3821
|
+
if (program) {
|
|
3822
|
+
this.driveResumable(process, program, value);
|
|
3823
|
+
return;
|
|
3824
|
+
}
|
|
2037
3825
|
this.current = process.instance;
|
|
2038
3826
|
process.state = "running";
|
|
2039
3827
|
process.execution ??= body(process.declaration.body)(process.instance.host);
|
|
@@ -2041,7 +3829,7 @@ var BoardRuntime = class {
|
|
|
2041
3829
|
const step = process.execution.next(value);
|
|
2042
3830
|
if (step.done) {
|
|
2043
3831
|
this.boundary();
|
|
2044
|
-
|
|
3832
|
+
process.execution = void 0;
|
|
2045
3833
|
process.state = "idle";
|
|
2046
3834
|
if (process.pending) {
|
|
2047
3835
|
process.pending = false;
|
|
@@ -2068,292 +3856,61 @@ var BoardRuntime = class {
|
|
|
2068
3856
|
if (duration === 0) {
|
|
2069
3857
|
process.state = "queued";
|
|
2070
3858
|
this.models.push(process);
|
|
2071
|
-
} else
|
|
2072
|
-
process.
|
|
2073
|
-
|
|
2074
|
-
|
|
2075
|
-
|
|
2076
|
-
|
|
2077
|
-
}
|
|
2078
|
-
refreshDirectGuards(jit, hookReads) {
|
|
2079
|
-
let changed = false;
|
|
2080
|
-
const instance = jit.instance;
|
|
2081
|
-
const hookCount = jit.directHookReads.size;
|
|
2082
|
-
if (hookReads) for (const slot of hookReads.keys()) jit.directHookReads.add(slot);
|
|
2083
|
-
if (jit.directObserverRevision === instance.observerRevision && hookCount === jit.directHookReads.size) return;
|
|
2084
|
-
jit.directObserverRevision = instance.observerRevision;
|
|
2085
|
-
for (const [name, slot] of instance.stateResources) {
|
|
2086
|
-
const blocked = slot.subscribers.size > 0 || jit.directHookReads.has(slot);
|
|
2087
|
-
if (jit.directStateGuards.has(name) === blocked) continue;
|
|
2088
|
-
if (blocked) jit.directStateGuards.add(name);
|
|
2089
|
-
else jit.directStateGuards.delete(name);
|
|
2090
|
-
changed = true;
|
|
2091
|
-
}
|
|
2092
|
-
for (const [name, slot] of instance.memoryResources) {
|
|
2093
|
-
const blocked = slot.subscribers.size > 0 || jit.directHookReads.has(slot) || name === jit.options.code;
|
|
2094
|
-
if (jit.directMemoryGuards.has(name) === blocked) continue;
|
|
2095
|
-
if (blocked) jit.directMemoryGuards.add(name);
|
|
2096
|
-
else jit.directMemoryGuards.delete(name);
|
|
2097
|
-
changed = true;
|
|
2098
|
-
}
|
|
2099
|
-
if (changed) {
|
|
2100
|
-
jit.direct = void 0;
|
|
2101
|
-
jit.directRevision = -1;
|
|
2102
|
-
jit.directSkip = void 0;
|
|
2103
|
-
}
|
|
2104
|
-
}
|
|
2105
|
-
runDirect(jit, hookReads) {
|
|
2106
|
-
const instance = jit.instance;
|
|
2107
|
-
const pending = Boolean(jit.direct?.frame.resume);
|
|
2108
|
-
if (!pending) {
|
|
2109
|
-
const pc = finite(instance.states.get(jit.options.pc), "pc");
|
|
2110
|
-
if (this.execution !== "throughput" || this.jits.length !== 1 || jit.polling.has(pc) || jit.cache.get(pc)?.polling) return false;
|
|
2111
|
-
this.refreshDirectGuards(jit, hookReads);
|
|
2112
|
-
if (jit.directSkip === pc) return false;
|
|
2113
|
-
if (jit.directRevision !== jit.revision) {
|
|
2114
|
-
if (jit.directProgramRevision !== jit.revision) {
|
|
2115
|
-
jit.directPrograms.clear();
|
|
2116
|
-
jit.directProgramRevision = jit.revision;
|
|
2117
|
-
}
|
|
2118
|
-
const key = JSON.stringify([[...jit.directStateGuards].sort(), [...jit.directMemoryGuards].sort()]);
|
|
2119
|
-
if (jit.directPrograms.has(key)) {
|
|
2120
|
-
jit.direct = jit.directPrograms.get(key);
|
|
2121
|
-
jit.directRevision = jit.revision;
|
|
2122
|
-
} else {
|
|
2123
|
-
const stateNames = [...instance.states.keys()].filter((name) => {
|
|
2124
|
-
const declaration = instance.declarations.get(name);
|
|
2125
|
-
return declaration.type.kind === "number" || declaration.type.kind === "bool";
|
|
2126
|
-
});
|
|
2127
|
-
const memoryNames = [...instance.memories.keys()];
|
|
2128
|
-
const states = new Map(stateNames.map((name, index) => [name, index]));
|
|
2129
|
-
const memories = new Map(memoryNames.map((name, index) => [name, index]));
|
|
2130
|
-
const program = compileDirectJit([...jit.cache.values()].filter((instruction) => !instruction.polling && !jit.polling.has(instruction.pc)), {
|
|
2131
|
-
states,
|
|
2132
|
-
memories,
|
|
2133
|
-
pc: {
|
|
2134
|
-
name: jit.options.pc,
|
|
2135
|
-
index: states.get(jit.options.pc)
|
|
2136
|
-
},
|
|
2137
|
-
blockedStates: jit.directStateGuards,
|
|
2138
|
-
blockedMemories: jit.directMemoryGuards
|
|
2139
|
-
});
|
|
2140
|
-
jit.directRevision = jit.revision;
|
|
2141
|
-
jit.direct = program ? {
|
|
2142
|
-
program,
|
|
2143
|
-
stateReads: program.readStates.map((name) => [name, states.get(name)]),
|
|
2144
|
-
stateWrites: program.writeStates.map((name) => [name, states.get(name)]),
|
|
2145
|
-
memoryWrites: program.writeMemories.map((name) => [name, memories.get(name)]),
|
|
2146
|
-
revision: jit.revision,
|
|
2147
|
-
frame: {
|
|
2148
|
-
states: new Array(stateNames.length),
|
|
2149
|
-
memories: memoryNames.map((name) => instance.memory(name)),
|
|
2150
|
-
locals: new Array(program.localCount),
|
|
2151
|
-
stateDirty: new Array(Math.ceil(stateNames.length / 32)).fill(0),
|
|
2152
|
-
memoryDirty: new Array(Math.ceil(memoryNames.length / 32)).fill(0),
|
|
2153
|
-
time: this.time,
|
|
2154
|
-
due: this.time,
|
|
2155
|
-
resume: 0
|
|
2156
|
-
}
|
|
2157
|
-
} : void 0;
|
|
2158
|
-
if (jit.directPrograms.size >= 8) jit.directPrograms.delete(jit.directPrograms.keys().next().value);
|
|
2159
|
-
jit.directPrograms.set(key, jit.direct);
|
|
2160
|
-
}
|
|
3859
|
+
} else {
|
|
3860
|
+
process.wake ??= () => {
|
|
3861
|
+
process.state = "queued";
|
|
3862
|
+
this.models.push(process);
|
|
3863
|
+
};
|
|
3864
|
+
this.events.push(due, process.wake);
|
|
2161
3865
|
}
|
|
2162
|
-
if (!jit.direct) return false;
|
|
2163
|
-
}
|
|
2164
|
-
const direct = jit.direct;
|
|
2165
|
-
const frame = direct.frame;
|
|
2166
|
-
if (pending) frame.stopAfterInstruction = true;
|
|
2167
|
-
for (const [name, index] of direct.stateReads) frame.states[index] = instance.states.get(name);
|
|
2168
|
-
frame.time = this.time;
|
|
2169
|
-
frame.stateDirty.fill(0);
|
|
2170
|
-
frame.memoryDirty.fill(0);
|
|
2171
|
-
const start = this.time;
|
|
2172
|
-
let reason;
|
|
2173
|
-
try {
|
|
2174
|
-
reason = direct.program.run(frame, Math.min(this.executionEnd, this.events.time));
|
|
2175
|
-
} finally {
|
|
2176
|
-
this.time = frame.time;
|
|
2177
|
-
for (const [name, index] of direct.stateWrites) if ((frame.stateDirty[index >>> 5] & 1 << (index & 31)) !== 0) instance.host.write(name, frame.states[index]);
|
|
2178
|
-
for (const [name, index] of direct.memoryWrites) if ((frame.memoryDirty[index >>> 5] & 1 << (index & 31)) !== 0) instance.memoryChanged(name);
|
|
2179
|
-
}
|
|
2180
|
-
this.boundary();
|
|
2181
|
-
if (reason === "fallback") {
|
|
2182
|
-
jit.directSkip = finite(instance.states.get(jit.options.pc), "pc");
|
|
2183
|
-
if (this.time === start && this.time < this.executionEnd) return false;
|
|
2184
|
-
} else if (reason === "stopped") {
|
|
2185
|
-
jit.stopped = true;
|
|
2186
|
-
return true;
|
|
2187
|
-
} else if (reason === "invalidated") {
|
|
2188
|
-
frame.stopAfterInstruction = false;
|
|
2189
|
-
if (direct.revision !== jit.revision) {
|
|
2190
|
-
jit.direct = void 0;
|
|
2191
|
-
jit.directRevision = -1;
|
|
2192
|
-
}
|
|
2193
|
-
}
|
|
2194
|
-
if (frame.resume && frame.due > this.time) this.events.push(frame.due, () => this.queueJit(jit));
|
|
2195
|
-
else this.queueJit(jit);
|
|
2196
|
-
return true;
|
|
2197
|
-
}
|
|
2198
|
-
pollingDependencies(jit, dependencies) {
|
|
2199
|
-
const result = new Map(dependencies);
|
|
2200
|
-
const instance = jit.instance;
|
|
2201
|
-
for (const slot of [instance.stateResources.get(jit.options.pc), instance.memoryResources.get(jit.options.code)]) result.set(slot, slot.version);
|
|
2202
|
-
return result;
|
|
2203
|
-
}
|
|
2204
|
-
jitSignal(jit, signal) {
|
|
2205
|
-
if (signal.kind === "elapse") {
|
|
2206
|
-
const duration = time(signal.duration);
|
|
2207
|
-
if (duration === 0) return false;
|
|
2208
|
-
const due = time(this.time + duration);
|
|
2209
|
-
this.events.push(due, () => this.queueJit(jit));
|
|
2210
|
-
this.boundary();
|
|
2211
|
-
return true;
|
|
2212
|
-
}
|
|
2213
|
-
if (signal.kind === "waitUntil") {
|
|
2214
|
-
const checked = jit.instance.evaluate(() => jit.instance.syncCall(signal.predicate));
|
|
2215
|
-
if (checked.value === true) return false;
|
|
2216
|
-
jit.predicate = signal.predicate;
|
|
2217
|
-
jit.pollingWait = false;
|
|
2218
|
-
jit.dependencies = checked.dependencies;
|
|
2219
|
-
this.waitingJits.add(jit);
|
|
2220
|
-
this.boundary();
|
|
2221
|
-
return true;
|
|
2222
|
-
}
|
|
2223
|
-
if (signal.kind === "stop") {
|
|
2224
|
-
jit.stopped = true;
|
|
2225
|
-
jit.execution = void 0;
|
|
2226
|
-
this.boundary();
|
|
2227
|
-
return true;
|
|
2228
|
-
}
|
|
2229
|
-
if (signal.kind === "redirect") {
|
|
2230
|
-
finite(signal.pc, "redirect target");
|
|
2231
|
-
const duration = time(signal.delay);
|
|
2232
|
-
jit.instance.host.write(jit.options.pc, signal.pc);
|
|
2233
|
-
jit.execution = void 0;
|
|
2234
|
-
const polling = jit.automaticPolling && duration > 0 && signal.pc === jit.currentPc && jit.pollReads;
|
|
2235
|
-
const dependencies = polling ? this.pollingDependencies(jit, polling) : void 0;
|
|
2236
|
-
jit.automaticPolling = false;
|
|
2237
|
-
jit.pollReads = void 0;
|
|
2238
|
-
this.boundary();
|
|
2239
|
-
if (duration === 0) this.queueJit(jit);
|
|
2240
|
-
else this.events.push(time(this.time + duration), () => {
|
|
2241
|
-
if (!jit.active || jit.stopped) return;
|
|
2242
|
-
if (dependencies && jit.instance.dependenciesCurrent(dependencies)) {
|
|
2243
|
-
jit.dependencies = dependencies;
|
|
2244
|
-
jit.pollingWait = true;
|
|
2245
|
-
jit.pollingPeriod = duration;
|
|
2246
|
-
jit.pollingOrigin = this.time;
|
|
2247
|
-
this.waitingJits.add(jit);
|
|
2248
|
-
} else this.queueJit(jit);
|
|
2249
|
-
});
|
|
2250
|
-
return true;
|
|
2251
|
-
}
|
|
2252
|
-
throw new TypeError("Invalid JIT suspension");
|
|
2253
|
-
}
|
|
2254
|
-
runJit(jit) {
|
|
2255
|
-
jit.queued = false;
|
|
2256
|
-
if (!jit.active || jit.stopped) return;
|
|
2257
|
-
this.current = jit.instance;
|
|
2258
|
-
if (jit.direct?.frame.resume) {
|
|
2259
|
-
this.runDirect(jit);
|
|
2260
3866
|
return;
|
|
2261
3867
|
}
|
|
2262
|
-
|
|
2263
|
-
|
|
2264
|
-
|
|
2265
|
-
|
|
2266
|
-
|
|
2267
|
-
|
|
2268
|
-
|
|
2269
|
-
|
|
2270
|
-
|
|
2271
|
-
|
|
2272
|
-
|
|
2273
|
-
|
|
2274
|
-
|
|
2275
|
-
|
|
2276
|
-
|
|
2277
|
-
|
|
2278
|
-
|
|
2279
|
-
|
|
2280
|
-
}
|
|
2281
|
-
}
|
|
2282
|
-
hookReads = checked?.dependencies;
|
|
2283
|
-
hookPending = handled;
|
|
2284
|
-
}
|
|
2285
|
-
const pc = finite(jit.instance.states.get(jit.options.pc), "pc");
|
|
2286
|
-
if (this.execution === "throughput") {
|
|
2287
|
-
const predicate = jit.polling.get(pc);
|
|
2288
|
-
if (predicate) {
|
|
2289
|
-
const checked = jit.instance.evaluate(() => jit.instance.syncCall(predicate));
|
|
2290
|
-
if (checked.value !== true) {
|
|
2291
|
-
jit.dependencies = this.pollingDependencies(jit, new Map([...hookReads ?? [], ...checked.dependencies]));
|
|
2292
|
-
jit.pollingWait = true;
|
|
2293
|
-
jit.pollingPeriod = 0;
|
|
2294
|
-
this.waitingJits.add(jit);
|
|
2295
|
-
this.boundary();
|
|
2296
|
-
return;
|
|
2297
|
-
}
|
|
3868
|
+
}
|
|
3869
|
+
/**
|
|
3870
|
+
* Same suspension contract as `drive`, driven by the body's own pc and reused frame:
|
|
3871
|
+
* the body reports ticks/receive through the frame and returns whether it suspended.
|
|
3872
|
+
*/
|
|
3873
|
+
driveResumable(process, program, value) {
|
|
3874
|
+
this.current = process.instance;
|
|
3875
|
+
process.state = "running";
|
|
3876
|
+
const state = process.frame ??= frame(program);
|
|
3877
|
+
let input = value;
|
|
3878
|
+
for (;;) {
|
|
3879
|
+
state.value = input;
|
|
3880
|
+
if (!program.run(process.instance.host, void 0, void 0, state)) {
|
|
3881
|
+
this.boundary();
|
|
3882
|
+
process.state = "idle";
|
|
3883
|
+
if (process.pending) {
|
|
3884
|
+
process.pending = false;
|
|
3885
|
+
this.enqueue(process);
|
|
2298
3886
|
}
|
|
2299
|
-
|
|
3887
|
+
return;
|
|
2300
3888
|
}
|
|
2301
|
-
|
|
2302
|
-
|
|
2303
|
-
|
|
2304
|
-
|
|
2305
|
-
|
|
2306
|
-
try {
|
|
2307
|
-
const code = bytesView(jit.instance.memory(jit.options.code), () => {
|
|
2308
|
-
if (!valid) throw this.error("OBJECT_DISPOSED", "Translation view expired", jit.instance);
|
|
2309
|
-
});
|
|
2310
|
-
const result = jit.options.translate(Object.freeze({
|
|
2311
|
-
pc,
|
|
2312
|
-
code
|
|
2313
|
-
}), createJitBlockBuilder({ device: jit.instance.entry.model }));
|
|
2314
|
-
acceptSync(result);
|
|
2315
|
-
const block = validateBoundBlockIR(result, jit.instance.entry.model);
|
|
2316
|
-
if (block.instructions[0]?.pc !== pc) throw new TypeError("Translated first pc does not match");
|
|
2317
|
-
const instructions = block.instructions.map((instruction) => ({
|
|
2318
|
-
pc: instruction.pc,
|
|
2319
|
-
nextPc: instruction.nextPc,
|
|
2320
|
-
body: instruction.body,
|
|
2321
|
-
polling: isPollingInstruction(instruction.body, instruction.pc, jit.options.pc),
|
|
2322
|
-
execute: body(instruction.body)
|
|
2323
|
-
}));
|
|
2324
|
-
for (let index = instructions.length - 1; index >= 0; index--) {
|
|
2325
|
-
const entry = instructions[index];
|
|
2326
|
-
jit.cache.set(entry.pc, entry);
|
|
2327
|
-
}
|
|
2328
|
-
jit.revision++;
|
|
2329
|
-
jit.directSkip = void 0;
|
|
2330
|
-
instruction = instructions[0];
|
|
2331
|
-
} catch (cause) {
|
|
2332
|
-
throw this.wrap(cause, "JIT_FAILED", "translate", jit.instance);
|
|
2333
|
-
} finally {
|
|
2334
|
-
valid = false;
|
|
2335
|
-
this.phase = prior;
|
|
3889
|
+
if (state.kind === RESUMABLE_RECEIVE) {
|
|
3890
|
+
const channel = process.instance.channels.get(state.channel);
|
|
3891
|
+
if (channel.incoming.length) {
|
|
3892
|
+
input = channel.incoming.shift().value;
|
|
3893
|
+
continue;
|
|
2336
3894
|
}
|
|
2337
|
-
|
|
2338
|
-
|
|
2339
|
-
jit.automaticPolling = this.execution === "throughput" && this.jits.length === 1 && instruction.polling && !hookPending && (!hook || hookReads !== void 0);
|
|
2340
|
-
jit.pollReads = hookReads;
|
|
2341
|
-
jit.instance.host.write(jit.options.pc, instruction.nextPc);
|
|
2342
|
-
jit.execution = instruction.execute(jit.instance.host);
|
|
2343
|
-
}
|
|
2344
|
-
for (;;) {
|
|
2345
|
-
const step = jit.automaticPolling ? (() => {
|
|
2346
|
-
const checked = jit.instance.evaluate(() => jit.execution.next());
|
|
2347
|
-
jit.pollReads = new Map([...jit.pollReads ?? [], ...checked.dependencies]);
|
|
2348
|
-
return checked.value;
|
|
2349
|
-
})() : jit.execution.next();
|
|
2350
|
-
if (step.done) {
|
|
2351
|
-
jit.execution = void 0;
|
|
3895
|
+
process.state = "waiting";
|
|
3896
|
+
channel.receiver = process;
|
|
2352
3897
|
this.boundary();
|
|
2353
|
-
this.queueJit(jit);
|
|
2354
3898
|
return;
|
|
2355
3899
|
}
|
|
2356
|
-
|
|
3900
|
+
const duration = state.duration, due = time(this.time + duration);
|
|
3901
|
+
process.state = "waiting";
|
|
3902
|
+
this.boundary();
|
|
3903
|
+
if (duration === 0) {
|
|
3904
|
+
process.state = "queued";
|
|
3905
|
+
this.models.push(process);
|
|
3906
|
+
} else {
|
|
3907
|
+
process.wake ??= () => {
|
|
3908
|
+
process.state = "queued";
|
|
3909
|
+
this.models.push(process);
|
|
3910
|
+
};
|
|
3911
|
+
this.events.push(due, process.wake);
|
|
3912
|
+
}
|
|
3913
|
+
return;
|
|
2357
3914
|
}
|
|
2358
3915
|
}
|
|
2359
3916
|
runUntil(target) {
|
|
@@ -2377,11 +3934,12 @@ var BoardRuntime = class {
|
|
|
2377
3934
|
if (channel.pending.length) for (const message of channel.pending.drain()) channel.incoming.push(message);
|
|
2378
3935
|
if (channel.receiver && channel.incoming.length) {
|
|
2379
3936
|
const process = channel.receiver;
|
|
2380
|
-
|
|
3937
|
+
channel.receiver = void 0;
|
|
2381
3938
|
const received = channel.incoming.shift().value;
|
|
2382
3939
|
this.events.push(this.time, () => {
|
|
2383
3940
|
process.state = "queued";
|
|
2384
|
-
|
|
3941
|
+
process.resumeValue = received;
|
|
3942
|
+
this.models.push(process);
|
|
2385
3943
|
});
|
|
2386
3944
|
}
|
|
2387
3945
|
}
|
|
@@ -2393,18 +3951,19 @@ var BoardRuntime = class {
|
|
|
2393
3951
|
if (this.models.length) {
|
|
2394
3952
|
const process = this.models.shift();
|
|
2395
3953
|
const value = process.resumeValue;
|
|
2396
|
-
|
|
3954
|
+
process.resumeValue = void 0;
|
|
2397
3955
|
this.drive(process, value);
|
|
2398
3956
|
if (this.isFaulted) throw this.primary;
|
|
2399
3957
|
continue;
|
|
2400
3958
|
}
|
|
2401
3959
|
let pollingBoundary = Infinity;
|
|
2402
|
-
if (this.waitingJits.size) for (
|
|
3960
|
+
if (this.waitingJits.size) for (let index = 0; index < this.jits.length; index++) {
|
|
3961
|
+
const jit = this.jits[index];
|
|
2403
3962
|
if (!jit.active || jit.stopped || !this.waitingJits.has(jit)) continue;
|
|
2404
3963
|
if (jit.instance.dependenciesCurrent(jit.dependencies)) {
|
|
2405
3964
|
if (jit.pollingWait && jit.pollingPeriod > 0) {
|
|
2406
3965
|
const lastStart = MAX_TIME - (MAX_TIME - jit.pollingOrigin) % jit.pollingPeriod;
|
|
2407
|
-
if (lastStart <
|
|
3966
|
+
if (lastStart < 72e14 && lastStart < end) {
|
|
2408
3967
|
if (lastStart === this.time) {
|
|
2409
3968
|
jit.pollingWait = false;
|
|
2410
3969
|
delete jit.dependencies;
|
|
@@ -2434,7 +3993,7 @@ var BoardRuntime = class {
|
|
|
2434
3993
|
}
|
|
2435
3994
|
}
|
|
2436
3995
|
if (this.time < end && this.readyJits.length) {
|
|
2437
|
-
|
|
3996
|
+
runJit(this, this.readyJits.shift());
|
|
2438
3997
|
if (this.isFaulted) throw this.primary;
|
|
2439
3998
|
continue;
|
|
2440
3999
|
}
|
|
@@ -2504,6 +4063,11 @@ var BoardRuntime = class {
|
|
|
2504
4063
|
this.readyJits.clear();
|
|
2505
4064
|
this.observations.clear();
|
|
2506
4065
|
this.reactionBatch.length = 0;
|
|
4066
|
+
this.plans.length = 0;
|
|
4067
|
+
this.skippedPlans.length = 0;
|
|
4068
|
+
this.changeObservations.length = 0;
|
|
4069
|
+
this.changePrevious.length = 0;
|
|
4070
|
+
this.changeCurrent.length = 0;
|
|
2507
4071
|
this.sampleSlots.clear();
|
|
2508
4072
|
this.listeners.clear();
|
|
2509
4073
|
this.errors.dispose();
|