behavior-contracts 0.11.8 → 0.11.9
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/dist/behavior.d.ts +20 -0
- package/dist/behavior.d.ts.map +1 -1
- package/dist/behavior.js +40 -4
- package/dist/behavior.js.map +1 -1
- package/dist/generator/async-plan.js +4 -3
- package/dist/generator/async-plan.js.map +1 -1
- package/dist/generator/emit-typed-native-go.d.ts.map +1 -1
- package/dist/generator/emit-typed-native-go.js +8 -11
- package/dist/generator/emit-typed-native-go.js.map +1 -1
- package/dist/generator/emit-typed-native-rust.d.ts.map +1 -1
- package/dist/generator/emit-typed-native-rust.js +164 -81
- package/dist/generator/emit-typed-native-rust.js.map +1 -1
- package/dist/generator/typed.d.ts +1 -25
- package/dist/generator/typed.d.ts.map +1 -1
- package/dist/generator/typed.js +0 -31
- package/dist/generator/typed.js.map +1 -1
- package/dist/lowering.d.ts.map +1 -1
- package/dist/lowering.js +10 -3
- package/dist/lowering.js.map +1 -1
- package/dist/runtime.d.ts +1 -1
- package/dist/runtime.d.ts.map +1 -1
- package/dist/runtime.js +1 -1
- package/dist/runtime.js.map +1 -1
- package/dist/spec-versions.d.ts +1 -1
- package/dist/spec-versions.js +1 -1
- package/dist/spec-versions.js.map +1 -1
- package/dist/type-gate.d.ts +13 -1
- package/dist/type-gate.d.ts.map +1 -1
- package/dist/type-gate.js +29 -2
- package/dist/type-gate.js.map +1 -1
- package/package.json +2 -2
|
@@ -2,7 +2,7 @@ import { entryPortOrder, GeneratorFailure } from "./core.js";
|
|
|
2
2
|
import { mapTransformExpr, isControlGate, mapNodeElemIsOpaqueWire, nodeWirePassthrough } from "../behavior.js";
|
|
3
3
|
import { rustStraightlineInternals } from "./emit-shared-rust.js";
|
|
4
4
|
import { rustTypedInternals } from "./emit-shared-rust-typed.js";
|
|
5
|
-
import { buildTypePlan, deriveTypeRef, findDecl,
|
|
5
|
+
import { buildTypePlan, deriveTypeRef, findDecl, inputPortIsOptional, inputPortTypeRef } from "./typed.js";
|
|
6
6
|
import { concurrencyPlan, execPlan, analyzeSequentialOps, opsLiteralOf, classifyExpr, nodeNarrowedPaths } from "./straightline.js";
|
|
7
7
|
import { NativeExprCompiler } from "./native-expr.js";
|
|
8
8
|
import { buildAsyncPlans } from "./async-plan.js";
|
|
@@ -1233,15 +1233,13 @@ function coveredMapNode(n, bodyIds, comp) {
|
|
|
1233
1233
|
if (n === null || typeof n !== "object" || !("map" in n))
|
|
1234
1234
|
return false;
|
|
1235
1235
|
const m = n.map;
|
|
1236
|
-
// #108 (rust twin of the go predicate): a guarded map (`when`) IS covered with NO `into`. #177/#182:
|
|
1237
|
-
//
|
|
1238
|
-
//
|
|
1239
|
-
//
|
|
1240
|
-
//
|
|
1241
|
-
//
|
|
1242
|
-
//
|
|
1243
|
-
if (m.when !== undefined && m.into !== undefined && !guardedMapIntoIsCoverable(n, comp))
|
|
1244
|
-
return false;
|
|
1236
|
+
// #108 (rust twin of the go predicate): a guarded map (`when`) IS covered with NO `into`. #177/#182/#232:
|
|
1237
|
+
// a `map...into` ALWAYS has a well-defined unproduced value for the into field — OPT (unproduced → None,
|
|
1238
|
+
// #177) OR relationKind:connection (unproduced → the empty connection {items:[],cursor:null}, #182).
|
|
1239
|
+
// Byte-equal to run_behavior applyInto. The type gate (`mapIntoHoldsUnproduced`, run for every component
|
|
1240
|
+
// at the codegen entry) rejects anything else BEFORE any emitter sees it (#232). over element type must be
|
|
1241
|
+
// statically resolvable: prior-node arr field, or an input array port carrying `elemType` (BC does not
|
|
1242
|
+
// infer — consumer-interface C3).
|
|
1245
1243
|
if (m.relationKind !== undefined && m.relationKind !== "connection")
|
|
1246
1244
|
return false;
|
|
1247
1245
|
// A map node must carry an outType (its ELEMENT type per the recorder SoT — authoring.ts recordMap
|
|
@@ -1555,19 +1553,8 @@ function movableRefs(comp) {
|
|
|
1555
1553
|
function emitNativeRawRunner(comp, plan, ap, leaf, declared) {
|
|
1556
1554
|
const ep = execPlan(comp);
|
|
1557
1555
|
const order = ep.order;
|
|
1558
|
-
const hasConcurrency = ep.parallelStageOf.size > 0;
|
|
1559
1556
|
const fn = sanitize(comp.name);
|
|
1560
1557
|
const isAsync = ap.runnerIsAsync;
|
|
1561
|
-
// bc#97: the async runner awaits each async handler at its call site (derived per-node). A real-
|
|
1562
|
-
// concurrency stage (bc#87) dispatches handlers from scoped std threads — that mechanism cannot host
|
|
1563
|
-
// `.await` (a std thread is not an async task). Fail closed LOUDLY (rather than emit code that won't
|
|
1564
|
-
// compile) iff the runner is async AND has a concurrency stage. NOTE: with per-node granularity a
|
|
1565
|
-
// consumer CAN keep a concurrency-stage component sync (declare only its OTHER terminals async) — only
|
|
1566
|
-
// a component that both concurrency-stages AND awaits is rejected. Async + concurrent-stage (issuing
|
|
1567
|
-
// the stage's futures with join!) is a documented follow-up.
|
|
1568
|
-
if (isAsync && hasConcurrency) {
|
|
1569
|
-
throw new GeneratorFailure("UNSUPPORTED_NODE_STRAIGHTLINE", `rust typed-native async (bc#97): component '${comp.name}' has a bc#87 real-concurrency stage AND an async terminal handler, but a scoped-std-thread stage cannot host \`.await\`. Async + concurrent-stage (join! over the stage's futures) is a follow-up — declare this component's terminals sync, or split the concurrency stage.`);
|
|
1570
|
-
}
|
|
1571
1558
|
const typedNodes = new Map();
|
|
1572
1559
|
// #125: control-gate は typeless(typedNodes に載せない)。map ノードは produced-array ref に正規化
|
|
1573
1560
|
// (recorder SoT の element outType を {arr:element} へラップ / legacy {arr} はそのまま)。
|
|
@@ -1608,7 +1595,11 @@ function emitNativeRawRunner(comp, plan, ap, leaf, declared) {
|
|
|
1608
1595
|
lines.push(`// results are typed struct cells; a relation child reads the parent's REAL struct result via`);
|
|
1609
1596
|
lines.push(`// direct field access (child-present decision from the real parent value — relation /`);
|
|
1610
1597
|
lines.push(`// connection converge). A real-concurrency stage (bc#87) is static parallel orchestration —`);
|
|
1611
|
-
|
|
1598
|
+
// #231: the dispatch MECHANISM a stage uses is derived from its members' async-ness (a std thread cannot
|
|
1599
|
+
// host `.await`), so the banner names the one this component actually emits.
|
|
1600
|
+
lines.push([...new Set(ep.parallelStageOf.values())].some((s) => stageAwaits(comp, s, ap))
|
|
1601
|
+
? `// the stage's futures joined (bounded by the static plan.concurrency) call the transport; preflight +`
|
|
1602
|
+
: `// scoped worker threads (bounded by the static plan.concurrency) call the transport; preflight +`);
|
|
1612
1603
|
lines.push(`// interpret are committed in ascending index order so the value / op multiset / failure`);
|
|
1613
1604
|
lines.push(`// precedence byte-match run_behavior. The output is a typed struct/value assembled by struct`);
|
|
1614
1605
|
lines.push(`// literal + field access — the consumer keeps it native.`);
|
|
@@ -1643,7 +1634,7 @@ function emitNativeRawRunner(comp, plan, ap, leaf, declared) {
|
|
|
1643
1634
|
const i = order[k];
|
|
1644
1635
|
const stage = ep.parallelStageOf.get(i);
|
|
1645
1636
|
if (stage !== undefined && stage[0] === i) {
|
|
1646
|
-
lines.push(emitParallelStageArm(comp, stage, k, ep.concurrency, metas, typedNodes, plan, priorNodeAt, leaf));
|
|
1637
|
+
lines.push(emitParallelStageArm(comp, stage, k, ep.concurrency, metas, typedNodes, plan, priorNodeAt, leaf, ap));
|
|
1647
1638
|
k += stage.length - 1;
|
|
1648
1639
|
continue;
|
|
1649
1640
|
}
|
|
@@ -1762,21 +1753,36 @@ function emitNativeRawRunner(comp, plan, ap, leaf, declared) {
|
|
|
1762
1753
|
return lines.join("\n");
|
|
1763
1754
|
}
|
|
1764
1755
|
/**
|
|
1765
|
-
*
|
|
1766
|
-
*
|
|
1756
|
+
* stageAwaits (#231) — the ONE derivation of a real-concurrency stage's dispatch MECHANISM: a std thread
|
|
1757
|
+
* cannot host `.await`, so a stage dispatches by joining its members' futures iff ANY member awaits, and
|
|
1758
|
+
* by scoped std threads otherwise. Read per-node from the SHARED language-agnostic async propagation pass
|
|
1759
|
+
* (async-plan.ts) — the same SSoT the sequential arms consume. A no-dispatch member (cond / pure-expression
|
|
1760
|
+
* map) has no catalog component, so `nodeIsAsync` is false for it and it never flips the mechanism.
|
|
1761
|
+
*/
|
|
1762
|
+
function stageAwaits(comp, stage, ap) {
|
|
1763
|
+
return stage.some((i) => ap.nodeIsAsync(comp.body[i].id));
|
|
1764
|
+
}
|
|
1765
|
+
/**
|
|
1766
|
+
* emitParallelStageArm (bc#87/#94/#231, rust CONCRETE) — EXPLICIT static parallel orchestration for a
|
|
1767
|
+
* real-concurrency stage. Reproduces run_plan_parallel's deterministic protocol so the observed value / op
|
|
1767
1768
|
* multiset / failure precedence byte-match run_behavior:
|
|
1768
1769
|
* 1. PREFLIGHT (ascending): parent-produced gate + build each member's CONCRETE ports struct.
|
|
1769
|
-
* 2. DISPATCH (bounded
|
|
1770
|
-
* members from a shared atomic cursor in ascending
|
|
1771
|
-
*
|
|
1770
|
+
* 2. DISPATCH (bounded — ONE phase, two mechanisms picked by the members' derived async-ness):
|
|
1771
|
+
* - SYNC members: scoped std threads pull runnable members from a shared atomic cursor in ascending
|
|
1772
|
+
* order and call the leaf transport. Each result wire lands in a per-member Mutex slot.
|
|
1773
|
+
* - ASYNC members (#231): a std thread cannot host `.await`, so the stage's FUTURES are joined
|
|
1774
|
+
* instead — one lazily-built future per RUNNABLE member (a skipped member gets no future at all),
|
|
1775
|
+
* driven by a `std::future::poll_fn` scheduler that admits members in ascending order and keeps at
|
|
1776
|
+
* most `workers` of them in flight. A future that is never polled has never started, so "in flight"
|
|
1777
|
+
* IS "admitted and unsettled" — that is how the static bound is enforced without a runtime, without
|
|
1778
|
+
* the `futures` crate and without boxing. Each result lands in a per-member plain Option slot (the
|
|
1779
|
+
* scheduler is one task on one thread — no lock to take).
|
|
1772
1780
|
* 3. COMMIT (ascending index): interpret policy + move the concrete row into the outType cell —
|
|
1773
|
-
* lowest-index failure wins.
|
|
1781
|
+
* lowest-index failure wins. IDENTICAL for both mechanisms (only the slot read differs).
|
|
1774
1782
|
*/
|
|
1775
|
-
function emitParallelStageArm(comp, stage, atPos, concurrency, metas, typedNodes, plan, priorNodeAt, leaf) {
|
|
1783
|
+
function emitParallelStageArm(comp, stage, atPos, concurrency, metas, typedNodes, plan, priorNodeAt, leaf, ap) {
|
|
1776
1784
|
const lines = [];
|
|
1777
1785
|
const ids = stage.map((i) => comp.body[i].id);
|
|
1778
|
-
lines.push(` // ── bc#87 real-concurrency stage {${ids.map((x) => rustStrLit(x)).join(", ")}} — static parallel`);
|
|
1779
|
-
lines.push(` // orchestration (scoped threads, bound=${concurrency}); preflight+commit in ascending index order.`);
|
|
1780
1786
|
// #125/#108: a stage member may be a cond node — a pure Expression (NO handler/dispatch). The scoped-
|
|
1781
1787
|
// thread orchestration governs only the componentRef fan-out members; a cond is settled in ascending
|
|
1782
1788
|
// preflight order — BEFORE any dispatch — via the SAME emitCondArm the sequential path uses (no forked
|
|
@@ -1790,6 +1796,9 @@ function emitParallelStageArm(comp, stage, atPos, concurrency, metas, typedNodes
|
|
|
1790
1796
|
const noDispatch = (i) => "cond" in comp.body[i] || ("map" in comp.body[i] && mapTransformExpr(comp.body[i].map) !== undefined);
|
|
1791
1797
|
const condMembers = stage.filter(noDispatch);
|
|
1792
1798
|
const refMembers = stage.filter((i) => !noDispatch(i));
|
|
1799
|
+
const stageIsAsync = stageAwaits(comp, stage, ap);
|
|
1800
|
+
lines.push(` // ── bc#87 real-concurrency stage {${ids.map((x) => rustStrLit(x)).join(", ")}} — static parallel`);
|
|
1801
|
+
lines.push(` // orchestration (${stageIsAsync ? "joined futures" : "scoped threads"}, bound=${concurrency}); preflight+commit in ascending index order.`);
|
|
1793
1802
|
for (const i of condMembers) {
|
|
1794
1803
|
const n = comp.body[i];
|
|
1795
1804
|
lines.push("cond" in n
|
|
@@ -1797,9 +1806,9 @@ function emitParallelStageArm(comp, stage, atPos, concurrency, metas, typedNodes
|
|
|
1797
1806
|
: // 純式 map は leaf を呼ばないので async 性を持たない(sync 既定)。
|
|
1798
1807
|
emitMapArm(comp, n, atPos + stage.indexOf(i), typedNodes, plan, priorNodeAt, leaf, false, metas[i].policy));
|
|
1799
1808
|
}
|
|
1800
|
-
// A cond-ONLY stage carries no handler fan-out — the conds above ARE the whole stage. Skip the
|
|
1801
|
-
//
|
|
1802
|
-
//
|
|
1809
|
+
// A cond-ONLY stage carries no handler fan-out — the conds above ARE the whole stage. Skip the dispatch
|
|
1810
|
+
// block entirely (an empty one is dead scaffolding clippy rejects: never_loop / match_single_binding on
|
|
1811
|
+
// a zero-job scope, and a zero-member poll_fn scheduler is likewise dead).
|
|
1803
1812
|
if (refMembers.length === 0)
|
|
1804
1813
|
return lines.join("\n");
|
|
1805
1814
|
lines.push(` {`);
|
|
@@ -1853,63 +1862,137 @@ function emitParallelStageArm(comp, stage, atPos, concurrency, metas, typedNodes
|
|
|
1853
1862
|
for (const c of closers)
|
|
1854
1863
|
lines.push(c);
|
|
1855
1864
|
}
|
|
1856
|
-
// 2) DISPATCH (bounded
|
|
1857
|
-
//
|
|
1858
|
-
//
|
|
1859
|
-
//
|
|
1860
|
-
//
|
|
1865
|
+
// 2) DISPATCH (bounded). Each runnable member calls the leaf TRANSPORT directly for its CONCRETE ports
|
|
1866
|
+
// and stores the returned wire in its slot; the ascending COMMIT phase de-boxes it (so the WIRE — not
|
|
1867
|
+
// the decode — is what crosses the dispatch). #125: the fan-out (slots / jobs / workers) spans the
|
|
1868
|
+
// COMPONENTREF members only — a control gate dispatches no handler (committed in preflight above), so it
|
|
1869
|
+
// is not a job. The wire's concrete type is inferred from the transport's return
|
|
1870
|
+
// (Result<_, BehaviorError>).
|
|
1861
1871
|
const nMembers = refMembers.length;
|
|
1862
1872
|
const boundN = Math.min(concurrency, nMembers);
|
|
1863
1873
|
const jobsVar = `jobs_${sanitize(comp.name)}`;
|
|
1864
|
-
|
|
1865
|
-
|
|
1866
|
-
|
|
1867
|
-
|
|
1868
|
-
lines.push(` let
|
|
1869
|
-
|
|
1870
|
-
|
|
1871
|
-
|
|
1872
|
-
|
|
1873
|
-
|
|
1874
|
-
});
|
|
1875
|
-
lines.push(` let cursor = std::sync::atomic::AtomicUsize::new(0);`);
|
|
1876
|
-
lines.push(` let workers = ${boundN}usize.min(${jobsVar}.len());`);
|
|
1877
|
-
lines.push(` std::thread::scope(|scope| {`);
|
|
1878
|
-
lines.push(` for _ in 0..workers {`);
|
|
1879
|
-
lines.push(` scope.spawn(|| loop {`);
|
|
1880
|
-
lines.push(` let j = cursor.fetch_add(1, std::sync::atomic::Ordering::SeqCst);`);
|
|
1881
|
-
lines.push(` if j >= ${jobsVar}.len() { break; }`);
|
|
1882
|
-
lines.push(` let slot = ${jobsVar}[j];`);
|
|
1883
|
-
lines.push(` match slot {`);
|
|
1884
|
-
refMembers.forEach((i, slot) => {
|
|
1874
|
+
// the RUNNABLE-member job list + the static bound are mechanism-independent: both dispatch forms pull
|
|
1875
|
+
// jobs in ascending order and hold at most `workers` of them at a time. It is built BEFORE any payload is
|
|
1876
|
+
// taken — TAKING the payload is what dispatch does, so the list has to be read off the intact payloads.
|
|
1877
|
+
const emitJobList = () => {
|
|
1878
|
+
lines.push(` let mut ${jobsVar}: Vec<usize> = Vec::new();`);
|
|
1879
|
+
refMembers.forEach((i, slot) => {
|
|
1880
|
+
lines.push(` if payload_${sanitize(comp.body[i].id)}.lock().unwrap().is_some() { ${jobsVar}.push(${slot}); }`);
|
|
1881
|
+
});
|
|
1882
|
+
};
|
|
1883
|
+
const leafSym = (i) => {
|
|
1885
1884
|
const node = comp.body[i];
|
|
1886
|
-
|
|
1887
|
-
|
|
1888
|
-
|
|
1889
|
-
//
|
|
1890
|
-
|
|
1891
|
-
|
|
1892
|
-
|
|
1893
|
-
|
|
1894
|
-
|
|
1895
|
-
|
|
1896
|
-
|
|
1897
|
-
|
|
1898
|
-
|
|
1899
|
-
|
|
1900
|
-
|
|
1901
|
-
|
|
1902
|
-
|
|
1885
|
+
return resolveLeafSymbol(node.component, leaf, node.leafSymbol);
|
|
1886
|
+
};
|
|
1887
|
+
if (stageIsAsync) {
|
|
1888
|
+
// #231 ASYNC dispatch: one future per RUNNABLE member (a skipped member's `payload_<s>` is None, so
|
|
1889
|
+
// `Option::map` yields NO future — it is never constructed and never polled), pinned on the stack. An
|
|
1890
|
+
// async member's future IS the transport call; a SYNC member in the same stage is wrapped in an async
|
|
1891
|
+
// block so it starts when the scheduler admits it (calling it eagerly would ignore the bound). The
|
|
1892
|
+
// scheduler below is the join: it admits members in ASCENDING order, keeps at most `workers` in flight
|
|
1893
|
+
// (a future that has not been polled has not started), and settles each into a plain Option slot — one
|
|
1894
|
+
// task on one thread, so no lock. No `futures` crate, no boxing: every future is a concrete stack type.
|
|
1895
|
+
// The future MOVES the payload out of its slot (same take-not-clone as the scoped-thread arm).
|
|
1896
|
+
emitJobList();
|
|
1897
|
+
for (const i of refMembers) {
|
|
1898
|
+
const s = sanitize(comp.body[i].id);
|
|
1899
|
+
const taken = `payload_${s}.lock().unwrap().take()`;
|
|
1900
|
+
const fut = ap.nodeIsAsync(comp.body[i].id)
|
|
1901
|
+
? `${taken}.map(${leafSym(i)})`
|
|
1902
|
+
: `${taken}.map(|payload| async move { ${leafSym(i)}(payload) })`;
|
|
1903
|
+
lines.push(` let mut fut_${s} = std::pin::pin!(${fut});`);
|
|
1904
|
+
lines.push(` let mut slot_${s}: Option<Result<_, BehaviorError>> = None;`);
|
|
1905
|
+
}
|
|
1906
|
+
lines.push(` let workers = ${boundN}usize.min(${jobsVar}.len());`);
|
|
1907
|
+
lines.push(` let mut settled = [false; ${nMembers}];`);
|
|
1908
|
+
lines.push(` let mut admitted = 0usize;`);
|
|
1909
|
+
lines.push(` let mut in_flight = 0usize;`);
|
|
1910
|
+
lines.push(` std::future::poll_fn(|cx| {`);
|
|
1911
|
+
lines.push(` loop {`);
|
|
1912
|
+
lines.push(` while in_flight < workers && admitted < ${jobsVar}.len() {`);
|
|
1913
|
+
lines.push(` admitted += 1;`);
|
|
1914
|
+
lines.push(` in_flight += 1;`);
|
|
1915
|
+
lines.push(` }`);
|
|
1916
|
+
lines.push(` let mut progressed = false;`);
|
|
1917
|
+
lines.push(` for (j, slot) in ${jobsVar}.iter().copied().enumerate().take(admitted) {`);
|
|
1918
|
+
lines.push(` if settled[j] { continue; }`);
|
|
1919
|
+
lines.push(` let ready = match slot {`);
|
|
1920
|
+
refMembers.forEach((i, slot) => {
|
|
1921
|
+
const s = sanitize(comp.body[i].id);
|
|
1922
|
+
lines.push(` ${slot} => {`);
|
|
1923
|
+
lines.push(` let f = fut_${s}.as_mut().as_pin_mut().expect("runnable member has a future");`);
|
|
1924
|
+
lines.push(` match std::future::Future::poll(f, cx) {`);
|
|
1925
|
+
lines.push(` std::task::Poll::Ready(r) => {`);
|
|
1926
|
+
lines.push(` slot_${s} = Some(r);`);
|
|
1927
|
+
lines.push(` true`);
|
|
1928
|
+
lines.push(` }`);
|
|
1929
|
+
lines.push(` std::task::Poll::Pending => false,`);
|
|
1930
|
+
lines.push(` }`);
|
|
1931
|
+
lines.push(` }`);
|
|
1932
|
+
});
|
|
1933
|
+
lines.push(` _ => unreachable!(),`);
|
|
1934
|
+
lines.push(` };`);
|
|
1935
|
+
lines.push(` if ready {`);
|
|
1936
|
+
lines.push(` settled[j] = true;`);
|
|
1937
|
+
lines.push(` in_flight -= 1;`);
|
|
1938
|
+
lines.push(` progressed = true;`);
|
|
1939
|
+
lines.push(` }`);
|
|
1940
|
+
lines.push(` }`);
|
|
1941
|
+
lines.push(` if admitted == ${jobsVar}.len() && in_flight == 0 {`);
|
|
1942
|
+
lines.push(` return std::task::Poll::Ready(());`);
|
|
1943
|
+
lines.push(` }`);
|
|
1944
|
+
lines.push(` if !progressed { return std::task::Poll::Pending; }`);
|
|
1945
|
+
lines.push(` }`);
|
|
1946
|
+
lines.push(` })`);
|
|
1947
|
+
lines.push(` .await;`);
|
|
1948
|
+
}
|
|
1949
|
+
else {
|
|
1950
|
+
// SYNC dispatch: bounded scoped std threads pulling from a shared atomic cursor in ascending order.
|
|
1951
|
+
// The result wire impls Send so it crosses the scoped threads.
|
|
1952
|
+
for (const i of refMembers) {
|
|
1953
|
+
lines.push(` let slot_${sanitize(comp.body[i].id)}: std::sync::Mutex<Option<Result<_, BehaviorError>>> = std::sync::Mutex::new(None);`);
|
|
1954
|
+
}
|
|
1955
|
+
emitJobList();
|
|
1956
|
+
lines.push(` let cursor = std::sync::atomic::AtomicUsize::new(0);`);
|
|
1957
|
+
lines.push(` let workers = ${boundN}usize.min(${jobsVar}.len());`);
|
|
1958
|
+
lines.push(` std::thread::scope(|scope| {`);
|
|
1959
|
+
lines.push(` for _ in 0..workers {`);
|
|
1960
|
+
lines.push(` scope.spawn(|| loop {`);
|
|
1961
|
+
lines.push(` let j = cursor.fetch_add(1, std::sync::atomic::Ordering::SeqCst);`);
|
|
1962
|
+
lines.push(` if j >= ${jobsVar}.len() { break; }`);
|
|
1963
|
+
lines.push(` let slot = ${jobsVar}[j];`);
|
|
1964
|
+
lines.push(` match slot {`);
|
|
1965
|
+
refMembers.forEach((i, slot) => {
|
|
1966
|
+
const s = sanitize(comp.body[i].id);
|
|
1967
|
+
// DIRECT leaf transport call — ONE op-agnostic symbol, ONE WireRow payload (the preflight-built ports,
|
|
1968
|
+
// MOVED into the owned-arg call — the worker that wins the slot takes it). No `bound` (BC's internal
|
|
1969
|
+
// produced/skip gate is already applied in preflight — the leaf never sees it).
|
|
1970
|
+
lines.push(` ${slot} => {`);
|
|
1971
|
+
lines.push(` let payload = payload_${s}.lock().unwrap().take().expect("runnable member has ports");`);
|
|
1972
|
+
lines.push(` let row = ${leafSym(i)}(payload);`);
|
|
1973
|
+
lines.push(` *slot_${s}.lock().unwrap() = Some(row);`);
|
|
1974
|
+
lines.push(` }`);
|
|
1975
|
+
});
|
|
1976
|
+
lines.push(` _ => unreachable!(),`);
|
|
1977
|
+
lines.push(` };`);
|
|
1978
|
+
lines.push(` });`);
|
|
1979
|
+
lines.push(` }`);
|
|
1980
|
+
lines.push(` });`);
|
|
1981
|
+
}
|
|
1982
|
+
// 3) COMMIT (ascending index): interpret + move the concrete row into the outType cell. IDENTICAL for
|
|
1983
|
+
// both dispatch mechanisms — only the slot read differs (a joined-futures slot needs no lock).
|
|
1903
1984
|
refMembers.forEach((i) => {
|
|
1904
1985
|
const node = comp.body[i];
|
|
1905
1986
|
const meta = metas[i];
|
|
1906
1987
|
const s = sanitize(node.id);
|
|
1907
1988
|
const ref = typedNodes.get(node.id);
|
|
1908
1989
|
// A member that ran wrote its slot; taking it is both the "did it run" test and the move of the wire
|
|
1909
|
-
// out of the stage (the payload was already taken
|
|
1990
|
+
// out of the stage (the payload was already taken at dispatch, so it can no longer answer that).
|
|
1910
1991
|
// bind first: an `if let` scrutinee keeps its temporaries — including the MutexGuard — alive for the
|
|
1911
|
-
// whole body, which outlives the borrow the body needs.
|
|
1912
|
-
|
|
1992
|
+
// whole body, which outlives the borrow the body needs. A joined-futures slot is a plain Option (one
|
|
1993
|
+
// task on one thread), so it takes without a lock.
|
|
1994
|
+
const takeSlot = stageIsAsync ? `slot_${s}.take()` : `slot_${s}.lock().unwrap().take()`;
|
|
1995
|
+
lines.push(` let taken_${s} = ${takeSlot};`);
|
|
1913
1996
|
lines.push(` if let Some(out_${s}) = taken_${s} {`);
|
|
1914
1997
|
if (meta.policy === "continue") {
|
|
1915
1998
|
// continue: a member whose COMPONENT failed commits no Port. A de-box mismatch still propagates —
|