@mica-js/simulator 2.3.0 → 2.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +14 -8
- package/dist/index.js +280 -298
- package/dist/index.js.map +1 -1
- package/dist/rpc-BB5wM0QN.js +289 -0
- package/dist/rpc-BB5wM0QN.js.map +1 -0
- package/dist/sandbox.js +2 -2
- package/package.json +3 -2
- package/dist/rpc-D-9O3TZf.js +0 -108
- package/dist/rpc-D-9O3TZf.js.map +0 -1
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
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-
|
|
1
|
+
import { S as snapshot, _ as nextRandom, a as copyBytePayload, b as range, c as samePayload, d as captureValue, f as dataObject, g as integer, h as identity, i as simulationRuntimeTopicService, l as targetKey, m as finite, n as simulationRuntimeEventTransfer, o as copyPayload, p as externalValue, r as simulationRuntimeService, s as payloadMatches, u as bytesView, v as nonnegative, x as same, y as randomState } from "./rpc-BB5wM0QN.js";
|
|
2
2
|
import { Emitter } from "@mica-js/core/lifecycle";
|
|
3
3
|
import { BuildError, createJitBlockBuilder, validateBoundBlockIR, validateDeviceIR } from "@mica-js/device-model";
|
|
4
4
|
import { RESUMABLE_RECEIVE, checkBoolean, compileBody, compileComputation, compileDirectJit, compileResumableBody, createBytes, createEnum, createRecord, equalValues, nonNull, readIndex } from "@mica-js/compiler";
|
|
@@ -32,8 +32,22 @@ var ElectricalError = class extends Error {
|
|
|
32
32
|
function validateResistance(resistance, location = {}) {
|
|
33
33
|
if (!Number.isFinite(resistance) || resistance < .001 || resistance > 1e9) throw new ElectricalError("INVALID_ELECTRICAL_PARAMETER", "Resistance must be finite and in [0.001, 1000000000] ohms.", location);
|
|
34
34
|
}
|
|
35
|
-
|
|
35
|
+
/**
|
|
36
|
+
* Descriptors and admitted graphs share these buckets. The budget covers a whole-board
|
|
37
|
+
* working set: a 288-pin board steps through roughly 1.2k distinct shapes in 300 ms, so
|
|
38
|
+
* the former limit of eight discarded every descriptor before its second observation could
|
|
39
|
+
* ever admit a graph.
|
|
40
|
+
*/
|
|
41
|
+
var topologyTransitionLimit = 4096;
|
|
42
|
+
/** Compiled reference programs cost far more per entry than a graph descriptor. */
|
|
43
|
+
var referenceProgramLimit = 8;
|
|
36
44
|
var topologySnapshotLimit = 65536;
|
|
45
|
+
/** FNV-1a over the pin slot and its ordered endpoints: the key without the 288 strings. */
|
|
46
|
+
function hashEdges(slot, edges) {
|
|
47
|
+
let hash = Math.imul(2166136261 ^ slot, 16777619);
|
|
48
|
+
for (let index = 0; index < edges.length; index++) hash = Math.imul(hash ^ edges[index].to, 16777619);
|
|
49
|
+
return Math.imul(hash ^ edges.length, 16777619) >>> 0;
|
|
50
|
+
}
|
|
37
51
|
function topologyCost(topology) {
|
|
38
52
|
let cost = 0;
|
|
39
53
|
const physicals = /* @__PURE__ */ new Set();
|
|
@@ -52,7 +66,7 @@ function rebindBlock(block, pinEdges, updates) {
|
|
|
52
66
|
let edges;
|
|
53
67
|
for (let index = 0; index < block.component.edges.length; index++) {
|
|
54
68
|
const edge = block.component.edges[index];
|
|
55
|
-
const next = (updates
|
|
69
|
+
const next = (updates[edge.pin] ?? pinEdges[edge.pin])[edge.index];
|
|
56
70
|
if (edge.from === next.from && edge.to === next.to && edge.resistance === next.resistance) continue;
|
|
57
71
|
edges ??= block.component.edges.slice();
|
|
58
72
|
edges[index] = next;
|
|
@@ -66,6 +80,11 @@ function rebindBlock(block, pinEdges, updates) {
|
|
|
66
80
|
}
|
|
67
81
|
};
|
|
68
82
|
}
|
|
83
|
+
/**
|
|
84
|
+
* Every block edge is re-read from the committed pin edges. A bucket key records
|
|
85
|
+
* connectivity only, so a resistance-only change keeps its key and still has to be
|
|
86
|
+
* repaired in blocks that no pin of this solve touches.
|
|
87
|
+
*/
|
|
69
88
|
function restoreTopology(topology, pinEdges, updates) {
|
|
70
89
|
let blocks;
|
|
71
90
|
for (let index = 0; index < topology.blocks.length; index++) {
|
|
@@ -86,10 +105,6 @@ function prepareBlock(component) {
|
|
|
86
105
|
key: component.nodes.join(",")
|
|
87
106
|
};
|
|
88
107
|
}
|
|
89
|
-
/** Ordered endpoint list per pin; multiplicity stays visible without hashing. */
|
|
90
|
-
function describeEdges(edges) {
|
|
91
|
-
return edges.map((edge) => edge.to).join(",");
|
|
92
|
-
}
|
|
93
108
|
function prepareTopology(nodes, pinEdges) {
|
|
94
109
|
const topology = new DisjointSet(nodes.length);
|
|
95
110
|
for (const edges of pinEdges) for (const edge of edges) topology.join(edge.from, edge.to);
|
|
@@ -459,30 +474,50 @@ function createElectricalNetwork(inputPins, inputNets) {
|
|
|
459
474
|
let cache = /* @__PURE__ */ new Map();
|
|
460
475
|
const referencePrograms = [];
|
|
461
476
|
let prepared;
|
|
462
|
-
let snapshot;
|
|
463
477
|
let nextTopologyId = 0;
|
|
464
|
-
const transitionAges = /* @__PURE__ */ new Map();
|
|
465
|
-
let transitionClock = 0;
|
|
466
478
|
const transitions = /* @__PURE__ */ new Map();
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
479
|
+
/**
|
|
480
|
+
* Per-pin endpoint hash. Their exact integer summary is the bucket key, so a repeated
|
|
481
|
+
* shape costs one hash per dirty pin instead of a 288-entry string join.
|
|
482
|
+
*/
|
|
483
|
+
const pinHashes = new Uint32Array(pins.length);
|
|
484
|
+
let topologyHash = 0;
|
|
485
|
+
let topologySpread = 0;
|
|
486
|
+
for (let slot = 0; slot < pins.length; slot++) {
|
|
487
|
+
const hash = hashEdges(slot, []);
|
|
488
|
+
pinHashes[slot] = hash;
|
|
489
|
+
topologyHash += hash;
|
|
490
|
+
topologySpread ^= hash;
|
|
491
|
+
}
|
|
492
|
+
const setPinHash = (slot, edges) => {
|
|
493
|
+
const hash = hashEdges(slot, edges);
|
|
494
|
+
const previous = pinHashes[slot];
|
|
495
|
+
if (hash === previous) return;
|
|
496
|
+
pinHashes[slot] = hash;
|
|
497
|
+
topologyHash += hash - previous;
|
|
498
|
+
topologySpread ^= previous ^ hash;
|
|
499
|
+
};
|
|
500
|
+
/** Confirms a bucket hit against the exact hashes the stored graph was built from. */
|
|
501
|
+
const lookupTransition = (key, updated) => {
|
|
502
|
+
const candidate = transitions.get(key);
|
|
503
|
+
if (candidate === void 0) return void 0;
|
|
504
|
+
const hashes = candidate.hashes;
|
|
505
|
+
if (hashes.length !== pinHashes.length) return void 0;
|
|
506
|
+
for (let slot = 0; slot < hashes.length; slot++) {
|
|
507
|
+
const edges = updated[slot];
|
|
508
|
+
const hash = edges === void 0 ? pinHashes[slot] : hashEdges(slot, edges);
|
|
509
|
+
if (hashes[slot] !== hash) return void 0;
|
|
510
|
+
}
|
|
511
|
+
return candidate;
|
|
512
|
+
};
|
|
513
|
+
const rememberTransition = (key, admitted) => {
|
|
514
|
+
if (admitted.cost > topologySnapshotLimit) return;
|
|
515
|
+
if (!transitions.has(key)) transitions.set(key, void 0);
|
|
516
|
+
else if (transitions.get(key) !== admitted) transitions.set(key, admitted);
|
|
517
|
+
while (transitions.size > topologyTransitionLimit) {
|
|
518
|
+
const oldest = transitions.keys().next().value;
|
|
519
|
+
if (oldest === void 0) break;
|
|
484
520
|
transitions.delete(oldest);
|
|
485
|
-
transitionAges.delete(oldest);
|
|
486
521
|
}
|
|
487
522
|
};
|
|
488
523
|
const publishedVoltages = new Array(nodes.length);
|
|
@@ -494,13 +529,16 @@ function createElectricalNetwork(inputPins, inputNets) {
|
|
|
494
529
|
const updatedSlots = [];
|
|
495
530
|
const updatedIntents = [];
|
|
496
531
|
const updatedEdges = [];
|
|
532
|
+
/** Dirty edges indexed by slot, so rebinding avoids a Map lookup per block edge. */
|
|
533
|
+
const updatedEdgeLists = new Array(pins.length);
|
|
534
|
+
const updatedEdgeSlots = [];
|
|
497
535
|
const replacedEdges = [];
|
|
498
536
|
const pendingPins = [];
|
|
499
537
|
const pendingSamples = [];
|
|
500
538
|
const seenSlots = new Int32Array(pins.length);
|
|
501
539
|
let seenStamp = 0;
|
|
502
|
-
|
|
503
|
-
let
|
|
540
|
+
/** Only the matrix partition is out of date; component arrays and buckets stay valid. */
|
|
541
|
+
let blocksStale = false;
|
|
504
542
|
/** Component arrays are copied before a local update mutates them, because admitted
|
|
505
543
|
* topology snapshots keep referencing the arrays they were created with. */
|
|
506
544
|
let componentArraysOwned = false;
|
|
@@ -522,7 +560,8 @@ function createElectricalNetwork(inputPins, inputNets) {
|
|
|
522
560
|
factorizationReuses: 0,
|
|
523
561
|
cacheHits: 0,
|
|
524
562
|
leafPlans: 0,
|
|
525
|
-
leafPropagations: 0
|
|
563
|
+
leafPropagations: 0,
|
|
564
|
+
topologyPrepares: 0
|
|
526
565
|
};
|
|
527
566
|
const emptySlots = Object.freeze([]);
|
|
528
567
|
/** Dirty slots supplied by the caller, deduplicated in place without an iterator per pin. */
|
|
@@ -570,7 +609,7 @@ function createElectricalNetwork(inputPins, inputNets) {
|
|
|
570
609
|
* Records `(pin, target, resistance)` once the general path proved the shape: one branch
|
|
571
610
|
* from a leaf node straight onto a fixed reference. Replays are only allowed while the
|
|
572
611
|
* intent still names that branch, the pin still has one incident edge, the pin node is
|
|
573
|
-
* still an unknown leaf and the target
|
|
612
|
+
* still an unknown leaf and the target node still carries a fixed potential.
|
|
574
613
|
*/
|
|
575
614
|
const pinLevel = (pin, voltage) => pin.low === void 0 ? "unknown" : voltage <= pin.low ? "low" : voltage >= pin.high ? "high" : "unknown";
|
|
576
615
|
/**
|
|
@@ -626,8 +665,7 @@ function createElectricalNetwork(inputPins, inputNets) {
|
|
|
626
665
|
pendingAdmissions.length = 0;
|
|
627
666
|
};
|
|
628
667
|
const matchLeaf = (slot, intent) => {
|
|
629
|
-
|
|
630
|
-
if (topology === void 0 || intent.length !== 1) return void 0;
|
|
668
|
+
if (prepared === void 0 || intent.length !== 1) return void 0;
|
|
631
669
|
const plans = leafPlans[slot];
|
|
632
670
|
if (plans === void 0) return void 0;
|
|
633
671
|
const previous = pinEdges[slot];
|
|
@@ -638,7 +676,7 @@ function createElectricalNetwork(inputPins, inputNets) {
|
|
|
638
676
|
for (let index = 0; index < plans.length; index++) {
|
|
639
677
|
const plan = plans[index];
|
|
640
678
|
if (plan.targetPin !== branch.to || plan.resistance !== branch.resistance) continue;
|
|
641
|
-
return
|
|
679
|
+
return nodes[plan.target].reference === void 0 ? void 0 : plan;
|
|
642
680
|
}
|
|
643
681
|
};
|
|
644
682
|
/**
|
|
@@ -717,9 +755,11 @@ function createElectricalNetwork(inputPins, inputNets) {
|
|
|
717
755
|
nodeDegree[target] = nodeDegree[target] + 1;
|
|
718
756
|
}
|
|
719
757
|
intents[slot] = leafIntents[position];
|
|
720
|
-
|
|
758
|
+
const edges = leafEdges[position];
|
|
759
|
+
pinEdges[slot] = edges;
|
|
760
|
+
setPinHash(slot, edges);
|
|
721
761
|
}
|
|
722
|
-
|
|
762
|
+
blocksStale = true;
|
|
723
763
|
statistics.solves++;
|
|
724
764
|
statistics.leafPropagations++;
|
|
725
765
|
};
|
|
@@ -792,7 +832,7 @@ function createElectricalNetwork(inputPins, inputNets) {
|
|
|
792
832
|
function runSolve(input) {
|
|
793
833
|
const initial = prepared === void 0;
|
|
794
834
|
pendingAdmissions.length = 0;
|
|
795
|
-
if (prepared !== void 0 && !
|
|
835
|
+
if (prepared !== void 0 && !blocksStale && input !== void 0 && input.length === 0) {
|
|
796
836
|
statistics.solves++;
|
|
797
837
|
statistics.cacheHits += prepared.blocks.length;
|
|
798
838
|
return emptySlots;
|
|
@@ -876,13 +916,6 @@ function createElectricalNetwork(inputPins, inputNets) {
|
|
|
876
916
|
updatedIntents.push(intent);
|
|
877
917
|
updatedEdges.push(edges);
|
|
878
918
|
}
|
|
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
919
|
if (leafSwitch && topologyChanged) {
|
|
887
920
|
for (let position = 0; position < updatedSlots.length; position++) {
|
|
888
921
|
leafSlots[position] = updatedSlots[position];
|
|
@@ -895,15 +928,9 @@ function createElectricalNetwork(inputPins, inputNets) {
|
|
|
895
928
|
applyLeaf(updatedSlots.length);
|
|
896
929
|
return changedSlots.length === 0 ? emptySlots : changedSlots;
|
|
897
930
|
}
|
|
898
|
-
if (
|
|
899
|
-
|
|
900
|
-
preparedStale = false;
|
|
901
|
-
descriptorsStale = true;
|
|
902
|
-
snapshot = void 0;
|
|
931
|
+
if (blocksStale) {
|
|
932
|
+
blocksStale = false;
|
|
903
933
|
cache = /* @__PURE__ */ new Map();
|
|
904
|
-
transitions.clear();
|
|
905
|
-
transitionAges.clear();
|
|
906
|
-
transitionClock = 0;
|
|
907
934
|
topologyChanged = true;
|
|
908
935
|
}
|
|
909
936
|
const updates = /* @__PURE__ */ new Map();
|
|
@@ -922,25 +949,33 @@ function createElectricalNetwork(inputPins, inputNets) {
|
|
|
922
949
|
return emptySlots;
|
|
923
950
|
}
|
|
924
951
|
let topology = prepared;
|
|
925
|
-
let
|
|
926
|
-
let nextDescriptions;
|
|
952
|
+
let bucket;
|
|
927
953
|
let restored;
|
|
928
954
|
const dirtyBlocks = /* @__PURE__ */ new Map();
|
|
955
|
+
for (let position = 0; position < updatedEdgeSlots.length; position++) updatedEdgeLists[updatedEdgeSlots[position]] = void 0;
|
|
956
|
+
updatedEdgeSlots.length = 0;
|
|
957
|
+
for (const [index, update] of updates) {
|
|
958
|
+
updatedEdgeLists[index] = update.edges;
|
|
959
|
+
updatedEdgeSlots.push(index);
|
|
960
|
+
}
|
|
929
961
|
if (topologyChanged) {
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
962
|
+
let hash = topologyHash;
|
|
963
|
+
let spread = topologySpread;
|
|
964
|
+
for (const [index, update] of updates) {
|
|
965
|
+
const next = hashEdges(index, update.edges);
|
|
966
|
+
const previous = pinHashes[index];
|
|
967
|
+
if (next === previous) continue;
|
|
968
|
+
hash += next - previous;
|
|
969
|
+
spread ^= previous ^ next;
|
|
936
970
|
}
|
|
937
|
-
|
|
938
|
-
restored =
|
|
939
|
-
if (restored !== void 0) topology = restoreTopology(restored.topology, pinEdges,
|
|
971
|
+
bucket = `${hash}:${spread}`;
|
|
972
|
+
restored = lookupTransition(bucket, updatedEdgeLists);
|
|
973
|
+
if (restored !== void 0) topology = restoreTopology(restored.topology, pinEdges, updatedEdgeLists);
|
|
940
974
|
else {
|
|
941
975
|
const nextEdges = pinEdges.slice();
|
|
942
976
|
for (const [index, update] of updates) nextEdges[index] = update.edges;
|
|
943
977
|
topology = prepareTopology(nodes, nextEdges);
|
|
978
|
+
statistics.topologyPrepares++;
|
|
944
979
|
}
|
|
945
980
|
} else {
|
|
946
981
|
const dirty = /* @__PURE__ */ new Set();
|
|
@@ -948,9 +983,9 @@ function createElectricalNetwork(inputPins, inputNets) {
|
|
|
948
983
|
const index = topology.edgeBlocks[edge.pin]?.[edge.index];
|
|
949
984
|
if (index !== void 0) dirty.add(index);
|
|
950
985
|
}
|
|
951
|
-
for (const index of dirty) dirtyBlocks.set(index, rebindBlock(topology.blocks[index], pinEdges,
|
|
986
|
+
for (const index of dirty) dirtyBlocks.set(index, rebindBlock(topology.blocks[index], pinEdges, updatedEdgeLists));
|
|
952
987
|
}
|
|
953
|
-
|
|
988
|
+
let nextCache;
|
|
954
989
|
const pendingVoltages = /* @__PURE__ */ new Map();
|
|
955
990
|
let factorizations = 0;
|
|
956
991
|
let factorizationReuses = 0;
|
|
@@ -960,7 +995,7 @@ function createElectricalNetwork(inputPins, inputNets) {
|
|
|
960
995
|
const previous = cache.get(key);
|
|
961
996
|
if (previous !== void 0 && sameConfiguration(previous, component)) {
|
|
962
997
|
cacheHits++;
|
|
963
|
-
nextCache.set(key, previous);
|
|
998
|
+
(nextCache ??= /* @__PURE__ */ new Map()).set(key, previous);
|
|
964
999
|
continue;
|
|
965
1000
|
}
|
|
966
1001
|
const voltages = new Map(component.nodes.map((node) => [node, nodes[node].reference ?? null]));
|
|
@@ -1004,7 +1039,7 @@ function createElectricalNetwork(inputPins, inputNets) {
|
|
|
1004
1039
|
referenceProgram = compileReferenceProgram(unknown, component.edges, slots, nodes);
|
|
1005
1040
|
if (referenceProgram !== void 0) {
|
|
1006
1041
|
referencePrograms.push(referenceProgram);
|
|
1007
|
-
if (referencePrograms.length >
|
|
1042
|
+
if (referencePrograms.length > referenceProgramLimit) referencePrograms.shift();
|
|
1008
1043
|
}
|
|
1009
1044
|
}
|
|
1010
1045
|
}
|
|
@@ -1047,11 +1082,12 @@ function createElectricalNetwork(inputPins, inputNets) {
|
|
|
1047
1082
|
});
|
|
1048
1083
|
}
|
|
1049
1084
|
}
|
|
1050
|
-
|
|
1085
|
+
const entry = {
|
|
1051
1086
|
edges: component.edges,
|
|
1052
1087
|
voltages: component.nodes.map((node) => voltages.get(node)),
|
|
1053
1088
|
factorization
|
|
1054
|
-
}
|
|
1089
|
+
};
|
|
1090
|
+
(nextCache ??= /* @__PURE__ */ new Map()).set(key, entry);
|
|
1055
1091
|
for (const [node, voltage] of voltages) if (publishedVoltages[node] !== voltage) pendingVoltages.set(node, voltage);
|
|
1056
1092
|
}
|
|
1057
1093
|
const pending = /* @__PURE__ */ new Map();
|
|
@@ -1080,22 +1116,23 @@ function createElectricalNetwork(inputPins, inputNets) {
|
|
|
1080
1116
|
syncDegrees(pinEdges[index], update.edges);
|
|
1081
1117
|
intents[index] = update.intent;
|
|
1082
1118
|
pinEdges[index] = update.edges;
|
|
1119
|
+
setPinHash(index, update.edges);
|
|
1083
1120
|
}
|
|
1084
|
-
if (topologyChanged) cache = nextCache;
|
|
1121
|
+
if (topologyChanged) cache = nextCache ?? /* @__PURE__ */ new Map();
|
|
1085
1122
|
else {
|
|
1086
|
-
for (const [key, value] of nextCache) cache.set(key, value);
|
|
1123
|
+
if (nextCache !== void 0) for (const [key, value] of nextCache) cache.set(key, value);
|
|
1087
1124
|
for (const [index, block] of dirtyBlocks) topology.blocks[index] = block;
|
|
1088
1125
|
}
|
|
1089
1126
|
prepared = topology;
|
|
1090
1127
|
componentArraysOwned = false;
|
|
1091
1128
|
if (topologyChanged) {
|
|
1092
|
-
|
|
1093
|
-
snapshot = restored !== void 0 && topology === restored.topology ? restored : {
|
|
1129
|
+
const admitted = restored !== void 0 && topology === restored.topology ? restored : {
|
|
1094
1130
|
id: restored?.id ?? nextTopologyId++,
|
|
1095
1131
|
cost: restored?.cost ?? topologyCost(topology),
|
|
1096
|
-
topology
|
|
1132
|
+
topology,
|
|
1133
|
+
hashes: restored?.hashes ?? pinHashes.slice()
|
|
1097
1134
|
};
|
|
1098
|
-
rememberTransition(
|
|
1135
|
+
rememberTransition(bucket, admitted);
|
|
1099
1136
|
}
|
|
1100
1137
|
statistics.solves++;
|
|
1101
1138
|
statistics.components = topology.components;
|
|
@@ -1220,169 +1257,6 @@ var Fifo = class {
|
|
|
1220
1257
|
}
|
|
1221
1258
|
};
|
|
1222
1259
|
//#endregion
|
|
1223
|
-
//#region src/values.ts
|
|
1224
|
-
function dataObject(value, fields) {
|
|
1225
|
-
if (value === null || typeof value !== "object" || Object.getPrototypeOf(value) !== Object.prototype && Object.getPrototypeOf(value) !== null) throw new TypeError("Expected a plain data object");
|
|
1226
|
-
for (const key of Reflect.ownKeys(value)) {
|
|
1227
|
-
if (!("value" in Object.getOwnPropertyDescriptor(value, key))) throw new TypeError("Accessor properties are not allowed");
|
|
1228
|
-
if (typeof key !== "string" || fields && !fields.includes(key)) throw new TypeError(`Unknown field ${String(key)}`);
|
|
1229
|
-
}
|
|
1230
|
-
}
|
|
1231
|
-
function finite(value, name = "value") {
|
|
1232
|
-
if (typeof value !== "number" || !Number.isFinite(value)) throw new TypeError(`${name} must be finite`);
|
|
1233
|
-
return value;
|
|
1234
|
-
}
|
|
1235
|
-
function integer(value, name = "value") {
|
|
1236
|
-
if (typeof value !== "number" || !Number.isSafeInteger(value)) throw new TypeError(`${name} must be a safe integer`);
|
|
1237
|
-
return value;
|
|
1238
|
-
}
|
|
1239
|
-
function nonnegative(value, name = "value") {
|
|
1240
|
-
const n = integer(value, name);
|
|
1241
|
-
if (n < 0) throw new RangeError(`${name} must be nonnegative`);
|
|
1242
|
-
return n;
|
|
1243
|
-
}
|
|
1244
|
-
function identity(value) {
|
|
1245
|
-
if (typeof value !== "string" || value.length === 0) throw new TypeError("Expected a nonempty identifier");
|
|
1246
|
-
return value;
|
|
1247
|
-
}
|
|
1248
|
-
function range(memory, offset, length) {
|
|
1249
|
-
const at = nonnegative(offset, "offset");
|
|
1250
|
-
const size = nonnegative(length, "length");
|
|
1251
|
-
if (at > memory.length || size > memory.length - at) throw new RangeError("Memory range is out of bounds");
|
|
1252
|
-
return at;
|
|
1253
|
-
}
|
|
1254
|
-
function same(a, b) {
|
|
1255
|
-
if (a === b || typeof a === "number" && typeof b === "number" && Number.isNaN(a) && Number.isNaN(b)) return true;
|
|
1256
|
-
if (a === null || b === null || typeof a !== "object" || typeof b !== "object") return false;
|
|
1257
|
-
const left = Object.keys(a), right = Object.keys(b);
|
|
1258
|
-
return left.length === right.length && left.every((key) => Object.hasOwn(b, key) && same(a[key], b[key]));
|
|
1259
|
-
}
|
|
1260
|
-
function snapshot(value) {
|
|
1261
|
-
if (value === null || typeof value !== "object") return value;
|
|
1262
|
-
if (value instanceof Uint8Array) return value.slice();
|
|
1263
|
-
if (Array.isArray(value)) return Object.freeze(value.map(snapshot));
|
|
1264
|
-
return Object.freeze(Object.fromEntries(Object.entries(value).map(([key, item]) => [key, snapshot(item)])));
|
|
1265
|
-
}
|
|
1266
|
-
function bytesView(bytes, check = () => {}) {
|
|
1267
|
-
const target = Object.freeze({
|
|
1268
|
-
get length() {
|
|
1269
|
-
check();
|
|
1270
|
-
return bytes.length;
|
|
1271
|
-
},
|
|
1272
|
-
get byteLength() {
|
|
1273
|
-
check();
|
|
1274
|
-
return bytes.length;
|
|
1275
|
-
},
|
|
1276
|
-
slice(start, end) {
|
|
1277
|
-
check();
|
|
1278
|
-
return bytes.slice(start, end);
|
|
1279
|
-
},
|
|
1280
|
-
*[Symbol.iterator]() {
|
|
1281
|
-
for (let i = 0; i < bytes.length; i++) {
|
|
1282
|
-
check();
|
|
1283
|
-
yield bytes[i];
|
|
1284
|
-
}
|
|
1285
|
-
check();
|
|
1286
|
-
}
|
|
1287
|
-
});
|
|
1288
|
-
return new Proxy(target, {
|
|
1289
|
-
get(object, key, receiver) {
|
|
1290
|
-
check();
|
|
1291
|
-
if (typeof key === "string" && /^(0|[1-9]\d*)$/.test(key)) return bytes[Number(key)];
|
|
1292
|
-
return Reflect.get(object, key, receiver);
|
|
1293
|
-
},
|
|
1294
|
-
set() {
|
|
1295
|
-
throw new TypeError("Bytes are read-only");
|
|
1296
|
-
},
|
|
1297
|
-
defineProperty() {
|
|
1298
|
-
throw new TypeError("Bytes are read-only");
|
|
1299
|
-
}
|
|
1300
|
-
});
|
|
1301
|
-
}
|
|
1302
|
-
function captureValue(type, value, declarations, property = false) {
|
|
1303
|
-
if (type.kind === "optional") {
|
|
1304
|
-
if (value === null) return null;
|
|
1305
|
-
return captureValue(type.inner, value, declarations, property);
|
|
1306
|
-
}
|
|
1307
|
-
switch (type.kind) {
|
|
1308
|
-
case "number":
|
|
1309
|
-
if (typeof value !== "number") throw new TypeError("Expected number");
|
|
1310
|
-
if (property && !Number.isFinite(value)) throw new TypeError("Property must be finite");
|
|
1311
|
-
return value;
|
|
1312
|
-
case "bool":
|
|
1313
|
-
if (typeof value !== "boolean") throw new TypeError("Expected boolean");
|
|
1314
|
-
return value;
|
|
1315
|
-
case "string":
|
|
1316
|
-
if (typeof value !== "string") throw new TypeError("Expected string");
|
|
1317
|
-
return value;
|
|
1318
|
-
case "bytes":
|
|
1319
|
-
if (!(value instanceof Uint8Array)) throw new TypeError("Expected Uint8Array");
|
|
1320
|
-
return value.slice();
|
|
1321
|
-
case "enum": {
|
|
1322
|
-
const members = type.name === "Logic" ? [
|
|
1323
|
-
"low",
|
|
1324
|
-
"high",
|
|
1325
|
-
"unknown"
|
|
1326
|
-
] : type.name === "SampleStatus" ? ["valid", "floating"] : type.name === "Overflow" ? [
|
|
1327
|
-
"drop_new",
|
|
1328
|
-
"drop_oldest",
|
|
1329
|
-
"error"
|
|
1330
|
-
] : declarations.get(type.name)?.members;
|
|
1331
|
-
if (typeof value !== "string" || !members?.includes(value)) throw new TypeError(`Invalid ${type.name} member`);
|
|
1332
|
-
return value;
|
|
1333
|
-
}
|
|
1334
|
-
case "record": {
|
|
1335
|
-
dataObject(value);
|
|
1336
|
-
const declaration = declarations.get(type.name);
|
|
1337
|
-
return Object.freeze(Object.fromEntries(declaration.fields.map((field) => [field.name, captureValue({ kind: field.type }, value[field.name], declarations)])));
|
|
1338
|
-
}
|
|
1339
|
-
case "sample":
|
|
1340
|
-
case "intent": return value;
|
|
1341
|
-
}
|
|
1342
|
-
}
|
|
1343
|
-
function externalValue(value) {
|
|
1344
|
-
if (value instanceof Uint8Array) return bytesView(value.slice());
|
|
1345
|
-
return snapshot(value);
|
|
1346
|
-
}
|
|
1347
|
-
function randomState(seed, id, override) {
|
|
1348
|
-
if (override !== void 0) {
|
|
1349
|
-
if (!Array.isArray(override) || override.length !== 4) throw new TypeError("randomState must contain four words");
|
|
1350
|
-
for (const word of override) {
|
|
1351
|
-
integer(word);
|
|
1352
|
-
if (word < 0 || word > 4294967295) throw new RangeError("randomState word out of range");
|
|
1353
|
-
}
|
|
1354
|
-
if (!override.some(Boolean)) throw new RangeError("randomState cannot be zero");
|
|
1355
|
-
return Uint32Array.from(override);
|
|
1356
|
-
}
|
|
1357
|
-
let h = 2166136261;
|
|
1358
|
-
const encoded = new TextEncoder().encode(id);
|
|
1359
|
-
const source = new Uint8Array(4 + encoded.length);
|
|
1360
|
-
new DataView(source.buffer).setUint32(0, seed, true);
|
|
1361
|
-
source.set(encoded, 4);
|
|
1362
|
-
for (const byte of source) h = Math.imul(h ^ byte, 16777619) >>> 0;
|
|
1363
|
-
const result = /* @__PURE__ */ new Uint32Array(4);
|
|
1364
|
-
for (let i = 0; i < 4; i++) {
|
|
1365
|
-
h = h + 2654435769 >>> 0;
|
|
1366
|
-
let z = Math.imul(h ^ h >>> 16, 2246822507) >>> 0;
|
|
1367
|
-
z = Math.imul(z ^ z >>> 13, 3266489909) >>> 0;
|
|
1368
|
-
result[i] = (z ^ z >>> 16) >>> 0;
|
|
1369
|
-
}
|
|
1370
|
-
if (!result.some(Boolean)) result[0] = 1;
|
|
1371
|
-
return result;
|
|
1372
|
-
}
|
|
1373
|
-
function nextRandom(s) {
|
|
1374
|
-
const rot = (x, k) => (x << k | x >>> 32 - k) >>> 0;
|
|
1375
|
-
const result = Math.imul(rot(Math.imul(s[1], 5), 7), 9) >>> 0;
|
|
1376
|
-
const t = s[1] << 9;
|
|
1377
|
-
s[2] = s[2] ^ s[0];
|
|
1378
|
-
s[3] = s[3] ^ s[1];
|
|
1379
|
-
s[1] = s[1] ^ s[2];
|
|
1380
|
-
s[0] = s[0] ^ s[3];
|
|
1381
|
-
s[2] = s[2] ^ t;
|
|
1382
|
-
s[3] = rot(s[3], 11);
|
|
1383
|
-
return result / 4294967296;
|
|
1384
|
-
}
|
|
1385
|
-
//#endregion
|
|
1386
1260
|
//#region src/observations.ts
|
|
1387
1261
|
var unaryOperators = {
|
|
1388
1262
|
positive: "+",
|
|
@@ -2046,15 +1920,15 @@ function hasEvery(previous, next) {
|
|
|
2046
1920
|
function createContext(owner, board) {
|
|
2047
1921
|
return Object.freeze({
|
|
2048
1922
|
get props() {
|
|
2049
|
-
board.
|
|
1923
|
+
board.assertReadAllowed(owner);
|
|
2050
1924
|
return Object.freeze(Object.fromEntries([...owner.props].map(([key, value]) => [key, externalValue(value)])));
|
|
2051
1925
|
},
|
|
2052
1926
|
get inputs() {
|
|
2053
|
-
owner.
|
|
1927
|
+
owner.assertCallAllowed();
|
|
2054
1928
|
return owner.inputs();
|
|
2055
1929
|
},
|
|
2056
1930
|
get memory() {
|
|
2057
|
-
board.
|
|
1931
|
+
board.assertReadAllowed(owner);
|
|
2058
1932
|
return owner.publicMemory();
|
|
2059
1933
|
},
|
|
2060
1934
|
topics: board.topicBridge(owner),
|
|
@@ -2674,18 +2548,18 @@ var Instance = class {
|
|
|
2674
2548
|
}
|
|
2675
2549
|
this.memorySizes.clear();
|
|
2676
2550
|
}
|
|
2677
|
-
|
|
2678
|
-
this.board.
|
|
2551
|
+
assertCallAllowed() {
|
|
2552
|
+
this.board.assertCallAllowed(this);
|
|
2679
2553
|
}
|
|
2680
2554
|
inputs() {
|
|
2681
2555
|
const result = Object.create(null);
|
|
2682
2556
|
for (const declaration of this.entry.model.declarations) {
|
|
2683
2557
|
if (declaration.kind === "channel" && declaration.direction === "input") result[declaration.name] = Object.freeze({ send: (value) => {
|
|
2684
|
-
this.
|
|
2558
|
+
this.assertCallAllowed();
|
|
2685
2559
|
this.send(declaration.name, value, true);
|
|
2686
2560
|
} });
|
|
2687
2561
|
if (declaration.kind === "notification" && declaration.direction === "input") result[declaration.name] = Object.freeze({ notify: () => {
|
|
2688
|
-
this.
|
|
2562
|
+
this.assertCallAllowed();
|
|
2689
2563
|
this.notificationInputs.add(declaration.name);
|
|
2690
2564
|
} });
|
|
2691
2565
|
}
|
|
@@ -2694,7 +2568,7 @@ var Instance = class {
|
|
|
2694
2568
|
publicMemory() {
|
|
2695
2569
|
const result = Object.create(null);
|
|
2696
2570
|
for (const declaration of this.entry.model.declarations) if (declaration.kind === "memory" && declaration.public) result[declaration.name] = bytesView(this.memory(declaration.name), () => {
|
|
2697
|
-
this.board.
|
|
2571
|
+
this.board.assertAlive(this);
|
|
2698
2572
|
if (![
|
|
2699
2573
|
"idle",
|
|
2700
2574
|
"initialize",
|
|
@@ -2725,7 +2599,7 @@ var Instance = class {
|
|
|
2725
2599
|
});
|
|
2726
2600
|
}
|
|
2727
2601
|
attach(options) {
|
|
2728
|
-
this.
|
|
2602
|
+
this.assertCallAllowed();
|
|
2729
2603
|
if (this.board.phase !== "initialize" || this.board.current !== this) throw this.board.error("INVALID_CALL_STATE", "attachJit is initialization-only", this);
|
|
2730
2604
|
dataObject(options, [
|
|
2731
2605
|
"entry",
|
|
@@ -2806,7 +2680,7 @@ var Instance = class {
|
|
|
2806
2680
|
return disposable;
|
|
2807
2681
|
}
|
|
2808
2682
|
loadBytes(name, bytes, offset) {
|
|
2809
|
-
this.
|
|
2683
|
+
this.assertCallAllowed();
|
|
2810
2684
|
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);
|
|
2811
2685
|
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);
|
|
2812
2686
|
if (!(bytes instanceof Uint8Array)) throw new TypeError("Code must be Uint8Array");
|
|
@@ -2832,6 +2706,18 @@ var Instance = class {
|
|
|
2832
2706
|
}
|
|
2833
2707
|
};
|
|
2834
2708
|
//#endregion
|
|
2709
|
+
//#region src/runtime/publish-gate.ts
|
|
2710
|
+
var gates = /* @__PURE__ */ new WeakMap();
|
|
2711
|
+
/** Attach the gate to the facade the board hands out; never exposed through the package entry. */
|
|
2712
|
+
function registerPublishGate(bridge, gate) {
|
|
2713
|
+
gates.set(bridge, gate);
|
|
2714
|
+
}
|
|
2715
|
+
function publishGate(bridge) {
|
|
2716
|
+
const gate = gates.get(bridge);
|
|
2717
|
+
if (gate === void 0) throw new TypeError("Topic bridge was not created by a board");
|
|
2718
|
+
return gate;
|
|
2719
|
+
}
|
|
2720
|
+
//#endregion
|
|
2835
2721
|
//#region src/polling.ts
|
|
2836
2722
|
function isPollingInstruction(body, pc, pcName) {
|
|
2837
2723
|
const definitions = /* @__PURE__ */ new Map();
|
|
@@ -2895,6 +2781,44 @@ function isPollingInstruction(body, pc, pcName) {
|
|
|
2895
2781
|
//#region src/runtime/jit.ts
|
|
2896
2782
|
/** Large programs dispatch through shards; small ones stay on the faster monolithic form. */
|
|
2897
2783
|
var DIRECT_SHARD_SIZE = 8;
|
|
2784
|
+
var barriers = /* @__PURE__ */ new WeakMap();
|
|
2785
|
+
function directBarriers(jit) {
|
|
2786
|
+
let entry = barriers.get(jit);
|
|
2787
|
+
if (entry === void 0) {
|
|
2788
|
+
entry = {
|
|
2789
|
+
stateMask: [],
|
|
2790
|
+
memoryMask: []
|
|
2791
|
+
};
|
|
2792
|
+
barriers.set(jit, entry);
|
|
2793
|
+
}
|
|
2794
|
+
return entry;
|
|
2795
|
+
}
|
|
2796
|
+
function directStats(jit) {
|
|
2797
|
+
const counted = jit;
|
|
2798
|
+
return counted.directStats ??= {
|
|
2799
|
+
directCompiles: 0,
|
|
2800
|
+
directCalls: 0,
|
|
2801
|
+
directFallbacks: 0,
|
|
2802
|
+
directDeadlines: 0,
|
|
2803
|
+
directStopped: 0
|
|
2804
|
+
};
|
|
2805
|
+
}
|
|
2806
|
+
/** Publish the current guard sets into the masks the compiled blocks test at entry. */
|
|
2807
|
+
function syncDirectBarriers(jit) {
|
|
2808
|
+
const entry = directBarriers(jit);
|
|
2809
|
+
const { stateIndex, memoryIndex } = entry;
|
|
2810
|
+
if (stateIndex === void 0 || memoryIndex === void 0) return;
|
|
2811
|
+
const fill = (mask, index, guards) => {
|
|
2812
|
+
mask.fill(0);
|
|
2813
|
+
for (const name of guards) {
|
|
2814
|
+
const slot = index.get(name);
|
|
2815
|
+
if (slot === void 0) continue;
|
|
2816
|
+
mask[slot >>> 5] = (mask[slot >>> 5] ?? 0) | 1 << (slot & 31);
|
|
2817
|
+
}
|
|
2818
|
+
};
|
|
2819
|
+
fill(entry.stateMask, stateIndex, jit.directStateGuards);
|
|
2820
|
+
fill(entry.memoryMask, memoryIndex, jit.directMemoryGuards);
|
|
2821
|
+
}
|
|
2898
2822
|
function refreshDirectGuards(jit, hookReads) {
|
|
2899
2823
|
let changed = false;
|
|
2900
2824
|
const instance = jit.instance;
|
|
@@ -2917,8 +2841,7 @@ function refreshDirectGuards(jit, hookReads) {
|
|
|
2917
2841
|
changed = true;
|
|
2918
2842
|
}
|
|
2919
2843
|
if (changed) {
|
|
2920
|
-
jit
|
|
2921
|
-
jit.directRevision = -1;
|
|
2844
|
+
syncDirectBarriers(jit);
|
|
2922
2845
|
jit.directSkip = void 0;
|
|
2923
2846
|
}
|
|
2924
2847
|
}
|
|
@@ -2966,7 +2889,7 @@ function directHost(board, instance, frame) {
|
|
|
2966
2889
|
}
|
|
2967
2890
|
};
|
|
2968
2891
|
}
|
|
2969
|
-
function createDirectFrame(board, instance, stateNames, memoryNames, program) {
|
|
2892
|
+
function createDirectFrame(board, instance, stateNames, memoryNames, program, barrier) {
|
|
2970
2893
|
const frame = {
|
|
2971
2894
|
states: new Array(stateNames.length),
|
|
2972
2895
|
memories: memoryNames.map((name) => instance.memory(name)),
|
|
@@ -2978,6 +2901,8 @@ function createDirectFrame(board, instance, stateNames, memoryNames, program) {
|
|
|
2978
2901
|
resume: 0
|
|
2979
2902
|
};
|
|
2980
2903
|
frame.host = directHost(board, instance, frame);
|
|
2904
|
+
frame.blockedStates = barrier.stateMask;
|
|
2905
|
+
frame.blockedMemories = barrier.memoryMask;
|
|
2981
2906
|
return frame;
|
|
2982
2907
|
}
|
|
2983
2908
|
function runDirect(board, jit, hookReads) {
|
|
@@ -2993,11 +2918,7 @@ function runDirect(board, jit, hookReads) {
|
|
|
2993
2918
|
jit.directPrograms.clear();
|
|
2994
2919
|
jit.directProgramRevision = jit.revision;
|
|
2995
2920
|
}
|
|
2996
|
-
const key = JSON.stringify([
|
|
2997
|
-
[...jit.directStateGuards].sort(),
|
|
2998
|
-
[...jit.directMemoryGuards].sort(),
|
|
2999
|
-
[...jit.delays.keys()].sort((left, right) => left - right)
|
|
3000
|
-
]);
|
|
2921
|
+
const key = JSON.stringify([...jit.delays.keys()].sort((left, right) => left - right));
|
|
3001
2922
|
const cached = jit.directPrograms.get(key);
|
|
3002
2923
|
if (cached) {
|
|
3003
2924
|
jit.direct = cached;
|
|
@@ -3027,27 +2948,29 @@ function runDirect(board, jit, hookReads) {
|
|
|
3027
2948
|
name: jit.options.pc,
|
|
3028
2949
|
index: states.get(jit.options.pc)
|
|
3029
2950
|
},
|
|
3030
|
-
blockedStates: jit.directStateGuards,
|
|
3031
|
-
blockedMemories: jit.directMemoryGuards,
|
|
3032
2951
|
host: true,
|
|
3033
2952
|
split: true,
|
|
3034
2953
|
shardSize: DIRECT_SHARD_SIZE,
|
|
3035
2954
|
delayLoops
|
|
3036
2955
|
});
|
|
3037
2956
|
jit.directRevision = jit.revision;
|
|
2957
|
+
if (program) directStats(jit).directCompiles++;
|
|
2958
|
+
const barrier = directBarriers(jit);
|
|
2959
|
+
barrier.stateIndex = states;
|
|
2960
|
+
barrier.memoryIndex = memories;
|
|
2961
|
+
barrier.stateMask.length = Math.ceil(stateNames.length / 32);
|
|
2962
|
+
barrier.memoryMask.length = Math.ceil(memoryNames.length / 32);
|
|
2963
|
+
syncDirectBarriers(jit);
|
|
3038
2964
|
jit.direct = program ? {
|
|
3039
2965
|
program,
|
|
3040
2966
|
stateReads: program.readStates.map((name) => [name, states.get(name)]),
|
|
3041
2967
|
stateWrites: program.writeStates.map((name) => [name, states.get(name)]),
|
|
3042
2968
|
memoryWrites: program.writeMemories.map((name) => [name, memories.get(name)]),
|
|
3043
2969
|
revision: jit.revision,
|
|
3044
|
-
frame: createDirectFrame(board, instance, stateNames, memoryNames, program)
|
|
2970
|
+
frame: createDirectFrame(board, instance, stateNames, memoryNames, program, barrier)
|
|
3045
2971
|
} : void 0;
|
|
3046
2972
|
if (!jit.direct) jit.directPrograms.delete(key);
|
|
3047
|
-
else
|
|
3048
|
-
if (jit.directPrograms.size >= 8) jit.directPrograms.delete(jit.directPrograms.keys().next().value);
|
|
3049
|
-
jit.directPrograms.set(key, jit.direct);
|
|
3050
|
-
}
|
|
2973
|
+
else jit.directPrograms.set(key, jit.direct);
|
|
3051
2974
|
}
|
|
3052
2975
|
}
|
|
3053
2976
|
if (!jit.direct) return false;
|
|
@@ -3061,18 +2984,29 @@ function runDirect(board, jit, hookReads) {
|
|
|
3061
2984
|
frame.memoryDirty.fill(0);
|
|
3062
2985
|
const start = board.time;
|
|
3063
2986
|
let reason;
|
|
2987
|
+
let wrote = false;
|
|
2988
|
+
const stats = directStats(jit);
|
|
2989
|
+
stats.directCalls++;
|
|
3064
2990
|
try {
|
|
3065
2991
|
reason = direct.program.run(frame, Math.min(board.executionEnd, board.events.time));
|
|
3066
2992
|
} finally {
|
|
3067
2993
|
board.time = frame.time;
|
|
3068
|
-
for (const [name, index] of direct.stateWrites) if ((frame.stateDirty[index >>> 5] & 1 << (index & 31)) !== 0)
|
|
3069
|
-
|
|
2994
|
+
for (const [name, index] of direct.stateWrites) if ((frame.stateDirty[index >>> 5] & 1 << (index & 31)) !== 0) {
|
|
2995
|
+
instance.host.write(name, frame.states[index]);
|
|
2996
|
+
wrote = true;
|
|
2997
|
+
}
|
|
2998
|
+
for (const [name, index] of direct.memoryWrites) if ((frame.memoryDirty[index >>> 5] & 1 << (index & 31)) !== 0) {
|
|
2999
|
+
instance.memoryChanged(name);
|
|
3000
|
+
wrote = true;
|
|
3001
|
+
}
|
|
3070
3002
|
}
|
|
3071
|
-
board.boundary();
|
|
3003
|
+
if (reason !== "fallback" || wrote || board.time !== start) board.boundary();
|
|
3072
3004
|
if (reason === "fallback") {
|
|
3005
|
+
stats.directFallbacks++;
|
|
3073
3006
|
jit.directSkip = finite(instance.states.get(jit.options.pc), "pc");
|
|
3074
3007
|
if (board.time === start && board.time < board.executionEnd) return false;
|
|
3075
3008
|
} else if (reason === "stopped") {
|
|
3009
|
+
stats.directStopped++;
|
|
3076
3010
|
jit.stopped = true;
|
|
3077
3011
|
return true;
|
|
3078
3012
|
} else if (reason === "invalidated") {
|
|
@@ -3081,7 +3015,7 @@ function runDirect(board, jit, hookReads) {
|
|
|
3081
3015
|
jit.direct = void 0;
|
|
3082
3016
|
jit.directRevision = -1;
|
|
3083
3017
|
}
|
|
3084
|
-
}
|
|
3018
|
+
} else stats.directDeadlines++;
|
|
3085
3019
|
if (frame.resume && frame.due > board.time) board.events.push(frame.due, () => board.queueJit(jit));
|
|
3086
3020
|
else board.queueJit(jit);
|
|
3087
3021
|
return true;
|
|
@@ -3313,6 +3247,8 @@ var BoardRuntime = class {
|
|
|
3313
3247
|
errors = new Emitter();
|
|
3314
3248
|
diagnostics = new Emitter();
|
|
3315
3249
|
listeners = /* @__PURE__ */ new Set();
|
|
3250
|
+
/** Owner-less listeners of the shared set: the host's direct subscriptions on `topics`. */
|
|
3251
|
+
hostListeners = 0;
|
|
3316
3252
|
models = new Fifo();
|
|
3317
3253
|
events = new EventQueue();
|
|
3318
3254
|
readyJits = new Fifo();
|
|
@@ -3494,18 +3430,26 @@ var BoardRuntime = class {
|
|
|
3494
3430
|
bindings: Object.freeze(this.instances.map((instance) => instance.bindingReport()))
|
|
3495
3431
|
});
|
|
3496
3432
|
}
|
|
3497
|
-
|
|
3433
|
+
assertAlive(instance) {
|
|
3498
3434
|
if (this.status === "disposed" || instance && !instance.valid) throw this.error("OBJECT_DISPOSED", "Object is disposed", instance);
|
|
3499
3435
|
}
|
|
3500
|
-
|
|
3501
|
-
|
|
3436
|
+
/** Admission for a call that may change board state or is visible to a device. */
|
|
3437
|
+
assertCallAllowed(instance) {
|
|
3438
|
+
this.admit(instance, false);
|
|
3439
|
+
}
|
|
3440
|
+
/** Admission for a read-only entry point: a faulted board stays inspectable. */
|
|
3441
|
+
assertReadAllowed(instance) {
|
|
3442
|
+
this.admit(instance, true);
|
|
3443
|
+
}
|
|
3444
|
+
admit(instance, allowFaulted) {
|
|
3445
|
+
this.assertAlive(instance);
|
|
3502
3446
|
if (instance && [
|
|
3503
3447
|
"execute",
|
|
3504
3448
|
"translate",
|
|
3505
3449
|
"dispose",
|
|
3506
3450
|
"assembly"
|
|
3507
3451
|
].includes(this.phase)) throw this.error("INVALID_CALL_STATE", "Operation is not allowed in this phase", instance);
|
|
3508
|
-
if (!
|
|
3452
|
+
if (!allowFaulted && this.status === "faulted") throw this.error("BOARD_FAULTED", "Board is faulted", instance);
|
|
3509
3453
|
}
|
|
3510
3454
|
error(code, message, instance, cause, phase) {
|
|
3511
3455
|
const electrical = cause instanceof ElectricalError ? cause : void 0;
|
|
@@ -3571,19 +3515,20 @@ var BoardRuntime = class {
|
|
|
3571
3515
|
this.fault(this.error("REPORTED_ERROR", cause instanceof Error ? cause.message : String(cause), instance, cause, "report"));
|
|
3572
3516
|
}
|
|
3573
3517
|
topicBridge(owner) {
|
|
3574
|
-
|
|
3518
|
+
const facade = Object.freeze({
|
|
3575
3519
|
onDidReceive: ((listener, thisArgs, disposables) => {
|
|
3576
|
-
this.
|
|
3520
|
+
this.assertCallAllowed(owner);
|
|
3577
3521
|
const entry = {
|
|
3578
3522
|
...owner ? { owner } : {},
|
|
3579
3523
|
call: (message) => listener.call(thisArgs, message)
|
|
3580
3524
|
};
|
|
3581
3525
|
this.listeners.add(entry);
|
|
3526
|
+
if (!owner) this.hostListeners++;
|
|
3582
3527
|
let active = true;
|
|
3583
3528
|
const disposable = { dispose: () => {
|
|
3584
3529
|
if (active) {
|
|
3585
3530
|
active = false;
|
|
3586
|
-
this.listeners.delete(entry)
|
|
3531
|
+
if (this.listeners.delete(entry) && !owner) this.hostListeners--;
|
|
3587
3532
|
}
|
|
3588
3533
|
} };
|
|
3589
3534
|
disposables?.push(disposable);
|
|
@@ -3591,7 +3536,7 @@ var BoardRuntime = class {
|
|
|
3591
3536
|
return disposable;
|
|
3592
3537
|
}),
|
|
3593
3538
|
publish: (topic, payload) => {
|
|
3594
|
-
this.
|
|
3539
|
+
this.assertCallAllowed(owner);
|
|
3595
3540
|
if (typeof topic !== "string") throw new TypeError("Topic must be a string");
|
|
3596
3541
|
this.dispatchDepth++;
|
|
3597
3542
|
try {
|
|
@@ -3614,6 +3559,11 @@ var BoardRuntime = class {
|
|
|
3614
3559
|
}
|
|
3615
3560
|
}
|
|
3616
3561
|
});
|
|
3562
|
+
registerPublishGate(facade, {
|
|
3563
|
+
assertPublishAllowed: () => this.assertCallAllowed(owner),
|
|
3564
|
+
hostListeners: () => this.hostListeners
|
|
3565
|
+
});
|
|
3566
|
+
return facade;
|
|
3617
3567
|
}
|
|
3618
3568
|
enqueue(process) {
|
|
3619
3569
|
if (process.state === "idle") {
|
|
@@ -3931,7 +3881,7 @@ var BoardRuntime = class {
|
|
|
3931
3881
|
}
|
|
3932
3882
|
}
|
|
3933
3883
|
runUntil(target) {
|
|
3934
|
-
this.
|
|
3884
|
+
this.assertAlive();
|
|
3935
3885
|
if (this.phase !== "idle" || this.dispatchDepth) throw this.error("INVALID_CALL_STATE", "Board is busy");
|
|
3936
3886
|
if (this.status === "faulted") throw this.error("BOARD_FAULTED", "Board is faulted");
|
|
3937
3887
|
const end = time(target);
|
|
@@ -4087,6 +4037,7 @@ var BoardRuntime = class {
|
|
|
4087
4037
|
this.changeCurrent.length = 0;
|
|
4088
4038
|
this.sampleSlots.clear();
|
|
4089
4039
|
this.listeners.clear();
|
|
4040
|
+
this.hostListeners = 0;
|
|
4090
4041
|
this.errors.dispose();
|
|
4091
4042
|
this.diagnostics.dispose();
|
|
4092
4043
|
this.status = "disposed";
|
|
@@ -4286,10 +4237,9 @@ function createSimulationRuntime(options = {}) {
|
|
|
4286
4237
|
if (disposed) throw new SimulationRuntimeError("disposed", "Simulation runtime has been disposed");
|
|
4287
4238
|
};
|
|
4288
4239
|
const flush = (loaded) => {
|
|
4289
|
-
for (const output of loaded.outputs.splice(0)) {
|
|
4290
|
-
|
|
4291
|
-
|
|
4292
|
-
if (topic.delivery === "level") {
|
|
4240
|
+
if (publishes.hasListeners) for (const output of loaded.outputs.splice(0)) {
|
|
4241
|
+
if (output.delivery === "level") {
|
|
4242
|
+
const key = targetKey(output.symbol, output.port);
|
|
4293
4243
|
if (published.has(key) && samePayload(published.get(key), output.payload)) continue;
|
|
4294
4244
|
if (publishes.hasListeners) published.set(key, copyPayload(output.payload));
|
|
4295
4245
|
}
|
|
@@ -4298,10 +4248,11 @@ function createSimulationRuntime(options = {}) {
|
|
|
4298
4248
|
symbol: output.symbol,
|
|
4299
4249
|
port: output.port
|
|
4300
4250
|
},
|
|
4301
|
-
payload:
|
|
4251
|
+
payload: output.payload,
|
|
4302
4252
|
simTime
|
|
4303
4253
|
});
|
|
4304
4254
|
}
|
|
4255
|
+
else loaded.outputs.length = 0;
|
|
4305
4256
|
for (const diagnostic of loaded.diagnostics.splice(0)) diagnostics.fire(diagnostic.runtimeCode === void 0 ? {
|
|
4306
4257
|
...diagnostic,
|
|
4307
4258
|
simTime
|
|
@@ -4338,12 +4289,12 @@ function createSimulationRuntime(options = {}) {
|
|
|
4338
4289
|
const prepared = await prepareSimulation(input, options.importer ?? importBlobModule, assertActive);
|
|
4339
4290
|
assertActive();
|
|
4340
4291
|
if (current !== generation) return;
|
|
4341
|
-
install(buildLoadedRuntime(prepared));
|
|
4292
|
+
install(buildLoadedRuntime(prepared, () => publishes.hasListeners));
|
|
4342
4293
|
},
|
|
4343
4294
|
async reset() {
|
|
4344
4295
|
assertActive();
|
|
4345
4296
|
++generation;
|
|
4346
|
-
if (active !== void 0) install(buildLoadedRuntime(active.prepared));
|
|
4297
|
+
if (active !== void 0) install(buildLoadedRuntime(active.prepared, () => publishes.hasListeners));
|
|
4347
4298
|
else {
|
|
4348
4299
|
pending.length = 0;
|
|
4349
4300
|
published.clear();
|
|
@@ -4406,7 +4357,7 @@ function createSimulationRuntime(options = {}) {
|
|
|
4406
4357
|
}
|
|
4407
4358
|
};
|
|
4408
4359
|
}
|
|
4409
|
-
function buildLoadedRuntime(prepared) {
|
|
4360
|
+
function buildLoadedRuntime(prepared, publishObserved) {
|
|
4410
4361
|
const outputs = [];
|
|
4411
4362
|
const found = [];
|
|
4412
4363
|
const inputs = /* @__PURE__ */ new Map();
|
|
@@ -4417,7 +4368,7 @@ function buildLoadedRuntime(prepared) {
|
|
|
4417
4368
|
model: instance.device.model,
|
|
4418
4369
|
create: (context) => instance.device.create({
|
|
4419
4370
|
...context,
|
|
4420
|
-
topics: scopeTopics(context.topics, instance, outputs, found)
|
|
4371
|
+
topics: scopeTopics(context.topics, instance, outputs, found, publishObserved)
|
|
4421
4372
|
})
|
|
4422
4373
|
};
|
|
4423
4374
|
})), {
|
|
@@ -4446,19 +4397,45 @@ function buildLoadedRuntime(prepared) {
|
|
|
4446
4397
|
subscriptions: [board.onDidError((event) => project(event, true)), board.onDidDiagnostic((event) => project(event, false))]
|
|
4447
4398
|
};
|
|
4448
4399
|
}
|
|
4449
|
-
/**
|
|
4450
|
-
|
|
4400
|
+
/**
|
|
4401
|
+
* Board topics are opaque global strings; this boundary gives each factory local port names.
|
|
4402
|
+
*
|
|
4403
|
+
* Exported for the fan-out regression test, which drives it against a real `Board`: every accepted
|
|
4404
|
+
* output must still reach `board.topics` listeners even when the instance has no scoped subscriber.
|
|
4405
|
+
*
|
|
4406
|
+
* `publishObserved` reports whether anything outside the board consumes published messages; with no
|
|
4407
|
+
* scoped subscriber, no owner-less bridge listener and no consumer, an accepted output is dropped
|
|
4408
|
+
* before it is copied, fanned out or queued, while the bridge's internal publish gate keeps the
|
|
4409
|
+
* board's call admission.
|
|
4410
|
+
*/
|
|
4411
|
+
function scopeTopics(bridge, instance, outputs, diagnostics, publishObserved) {
|
|
4451
4412
|
const prefix = `${encodeURIComponent(instance.symbol)}/`;
|
|
4413
|
+
const gate = publishGate(bridge);
|
|
4414
|
+
let scopedListeners = 0;
|
|
4415
|
+
const unobserved = () => scopedListeners === 0 && gate.hostListeners() === 0 && !publishObserved();
|
|
4452
4416
|
return {
|
|
4453
|
-
onDidReceive: ((listener, thisArgs, disposables) =>
|
|
4454
|
-
|
|
4455
|
-
|
|
4456
|
-
|
|
4457
|
-
|
|
4458
|
-
|
|
4459
|
-
|
|
4460
|
-
|
|
4461
|
-
|
|
4417
|
+
onDidReceive: ((listener, thisArgs, disposables) => {
|
|
4418
|
+
const subscription = bridge.onDidReceive((message) => {
|
|
4419
|
+
if (!message.topic.startsWith(prefix)) return;
|
|
4420
|
+
const port = message.topic.slice(prefix.length);
|
|
4421
|
+
if (instance.device.topics.get(port)?.direction !== "input") return;
|
|
4422
|
+
return listener.call(thisArgs, {
|
|
4423
|
+
topic: port,
|
|
4424
|
+
payload: copyPayload(message.payload)
|
|
4425
|
+
});
|
|
4426
|
+
}, void 0, void 0);
|
|
4427
|
+
scopedListeners++;
|
|
4428
|
+
let active = true;
|
|
4429
|
+
const disposable = { dispose: () => {
|
|
4430
|
+
if (active) {
|
|
4431
|
+
active = false;
|
|
4432
|
+
scopedListeners--;
|
|
4433
|
+
subscription.dispose();
|
|
4434
|
+
}
|
|
4435
|
+
} };
|
|
4436
|
+
disposables?.push(disposable);
|
|
4437
|
+
return disposable;
|
|
4438
|
+
}),
|
|
4462
4439
|
publish(port, payload) {
|
|
4463
4440
|
const topic = instance.device.topics.get(port);
|
|
4464
4441
|
if (topic?.direction !== "output" || !payloadMatches(topic.type, payload)) {
|
|
@@ -4471,12 +4448,17 @@ function scopeTopics(bridge, instance, outputs, diagnostics) {
|
|
|
4471
4448
|
});
|
|
4472
4449
|
return;
|
|
4473
4450
|
}
|
|
4451
|
+
if (unobserved()) {
|
|
4452
|
+
gate.assertPublishAllowed();
|
|
4453
|
+
return;
|
|
4454
|
+
}
|
|
4474
4455
|
const snapshot = copyPayload(payload);
|
|
4475
4456
|
bridge.publish(scopedTopic(instance.symbol, port), snapshot);
|
|
4476
4457
|
outputs.push({
|
|
4477
4458
|
symbol: instance.symbol,
|
|
4478
4459
|
port,
|
|
4479
|
-
payload: snapshot
|
|
4460
|
+
payload: snapshot,
|
|
4461
|
+
delivery: topic.delivery
|
|
4480
4462
|
});
|
|
4481
4463
|
}
|
|
4482
4464
|
};
|