behavior-contracts 0.11.23 → 0.12.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/cli-contract.yaml +27 -20
- package/dist/cli.js +3 -3
- package/dist/cli.js.map +1 -1
- package/dist/generated/contract.d.ts.map +1 -1
- package/dist/generated/contract.js +2 -2
- package/dist/generated/contract.js.map +1 -1
- package/dist/generated/program.js +4 -4
- package/dist/generated/program.js.map +1 -1
- package/dist/generator/core.d.ts +13 -10
- package/dist/generator/core.d.ts.map +1 -1
- package/dist/generator/core.js.map +1 -1
- package/dist/generator/emit-native-dynamic.d.ts +203 -0
- package/dist/generator/emit-native-dynamic.d.ts.map +1 -0
- package/dist/generator/emit-native-dynamic.js +733 -0
- package/dist/generator/emit-native-dynamic.js.map +1 -0
- package/dist/generator/emit-native-php.d.ts +4 -0
- package/dist/generator/emit-native-php.d.ts.map +1 -0
- package/dist/generator/emit-native-php.js +668 -0
- package/dist/generator/emit-native-php.js.map +1 -0
- package/dist/generator/emit-native-python.d.ts +4 -0
- package/dist/generator/emit-native-python.d.ts.map +1 -0
- package/dist/generator/emit-native-python.js +592 -0
- package/dist/generator/emit-native-python.js.map +1 -0
- package/dist/generator/emit-php.d.ts +4 -0
- package/dist/generator/emit-php.d.ts.map +1 -1
- package/dist/generator/emit-php.js +6 -2
- package/dist/generator/emit-php.js.map +1 -1
- package/dist/generator/emit-python.d.ts +4 -0
- package/dist/generator/emit-python.d.ts.map +1 -1
- package/dist/generator/emit-python.js +2 -2
- package/dist/generator/emit-python.js.map +1 -1
- package/dist/generator/emit-typed-native-go.d.ts.map +1 -1
- package/dist/generator/emit-typed-native-go.js +31 -542
- 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 +33 -461
- package/dist/generator/emit-typed-native-rust.js.map +1 -1
- package/dist/generator/index.d.ts +2 -0
- package/dist/generator/index.d.ts.map +1 -1
- package/dist/generator/index.js +9 -1
- package/dist/generator/index.js.map +1 -1
- package/dist/generator/native-coverage.d.ts +162 -0
- package/dist/generator/native-coverage.d.ts.map +1 -0
- package/dist/generator/native-coverage.js +412 -0
- package/dist/generator/native-coverage.js.map +1 -0
- package/dist/generator/native-expr.d.ts +1 -1
- package/dist/generator/native-expr.d.ts.map +1 -1
- package/dist/generator/typed.d.ts +6 -0
- package/dist/generator/typed.d.ts.map +1 -1
- package/dist/generator/typed.js +17 -0
- package/dist/generator/typed.js.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,733 @@
|
|
|
1
|
+
import { isControlGate, mapTransformExpr, nodeWirePassthrough } from "../behavior.js";
|
|
2
|
+
import { entryPortOrder, GeneratorFailure } from "./core.js";
|
|
3
|
+
import { buildDeBoxPlan } from "./emit-shared-debox.js";
|
|
4
|
+
import { NativeCoverage, buildTypedNodes, fanoutItemsElemRef, mapElemRowRef, resolveOverOrFail, } from "./native-coverage.js";
|
|
5
|
+
import { NativeExprCompiler } from "./native-expr.js";
|
|
6
|
+
import { analyzeSequentialOps, classifyExpr, execPlan, nodeNarrowedPaths, opsLiteralOf } from "./straightline.js";
|
|
7
|
+
import { buildTypePlan, deriveTypeRef, findDecl, inputPortIsOptional, inputPortTypeRef, lowerProducedAwareRead, makeOutputFieldTypeCheck, typeRefAtPath, } from "./typed.js";
|
|
8
|
+
/** sanitize — the identifier fragment of a node id (shared by every generated local name). */
|
|
9
|
+
export function sanitize(id) {
|
|
10
|
+
return id.replace(/[^A-Za-z0-9_]/g, "_");
|
|
11
|
+
}
|
|
12
|
+
/** leafSymbol — the op-agnostic transport symbol a covered leaf is called through: the spec-fixed
|
|
13
|
+
* `leafSymbol` first, then the `--leaf-transport` override, then the in-package default (#176 order). */
|
|
14
|
+
function leafSymbol(be, component, leaf, specSymbol) {
|
|
15
|
+
return specSymbol ?? leaf?.symbols[component] ?? be.defaultLeafSymbol(component);
|
|
16
|
+
}
|
|
17
|
+
/** typeKey — the type IDENTITY the output-slot check compares (nominal: a named type is its name). */
|
|
18
|
+
function typeKey(ref) {
|
|
19
|
+
switch (ref.kind) {
|
|
20
|
+
case "scalar":
|
|
21
|
+
return ref.scalar;
|
|
22
|
+
case "value":
|
|
23
|
+
return "value";
|
|
24
|
+
case "named":
|
|
25
|
+
return ref.name;
|
|
26
|
+
case "opt":
|
|
27
|
+
return `opt(${typeKey(ref.inner)})`;
|
|
28
|
+
case "arr":
|
|
29
|
+
return `arr(${typeKey(ref.elem)})`;
|
|
30
|
+
case "map":
|
|
31
|
+
return `map(${typeKey(ref.value)})`;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
/** the per-node policy as the call site applies it (an invalid Kind is rejected by the Portability Guard). */
|
|
35
|
+
function policyOf(meta, nodeId) {
|
|
36
|
+
if (typeof meta.policy === "string")
|
|
37
|
+
return meta.policy;
|
|
38
|
+
throw new GeneratorFailure("UNSUPPORTED_NODE_STRAIGHTLINE", `node '${nodeId}': unknown policy kind '${meta.policy.invalid}'`);
|
|
39
|
+
}
|
|
40
|
+
// ── the generator ───────────────────────────────────────────────────────────────────────
|
|
41
|
+
class DynModuleEmitter {
|
|
42
|
+
be;
|
|
43
|
+
ctx;
|
|
44
|
+
coverage;
|
|
45
|
+
check;
|
|
46
|
+
/** the leaf transport symbols the module calls (distinct, first-use order). */
|
|
47
|
+
leafSymbols = [];
|
|
48
|
+
uses = { threads: false };
|
|
49
|
+
constructor(be, ctx) {
|
|
50
|
+
this.be = be;
|
|
51
|
+
this.ctx = ctx;
|
|
52
|
+
this.check = makeOutputFieldTypeCheck(be.surface, `the declared output type`, typeKey);
|
|
53
|
+
this.coverage = new NativeCoverage(be.lang, be.surface, (node, comp, plan, typedNodes, expected, { asBinding, narrowed }) => {
|
|
54
|
+
this.compilePort(node, comp, plan, typedNodes, (h) => typedNodes.has(h), "port", expected, {
|
|
55
|
+
asBinding,
|
|
56
|
+
asBase: asBinding === undefined ? undefined : this.be.local("probe"),
|
|
57
|
+
narrowed,
|
|
58
|
+
fresh: () => "probe",
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
emit() {
|
|
63
|
+
const native = this.coverage.assertCovered(this.ctx.ir.components);
|
|
64
|
+
const plan = buildTypePlan(this.ctx.ir);
|
|
65
|
+
const runners = native.map((c) => new RunnerEmitter(this, c, plan).emit());
|
|
66
|
+
return this.be.module(this.ctx, runners, this.leafSymbols, this.uses);
|
|
67
|
+
}
|
|
68
|
+
useLeaf(component, specSymbol) {
|
|
69
|
+
const sym = leafSymbol(this.be, component, this.ctx.leafTransport, specSymbol);
|
|
70
|
+
if (!this.leafSymbols.includes(sym))
|
|
71
|
+
this.leafSymbols.push(sym);
|
|
72
|
+
return sym;
|
|
73
|
+
}
|
|
74
|
+
/** resolveHead — the ONE ref-head resolver of every expression in a runner: an element binding, a prior
|
|
75
|
+
* node's typed local, or an input port. */
|
|
76
|
+
resolveHead(comp, plan, typedNodes, isPrior, opts) {
|
|
77
|
+
return (head) => {
|
|
78
|
+
if (opts?.asBinding !== undefined && opts.asBase !== undefined && head === opts.asBinding.name)
|
|
79
|
+
return { expr: opts.asBase, ref: opts.asBinding.ref };
|
|
80
|
+
if (isPrior(head))
|
|
81
|
+
return { expr: this.be.local(`t_${sanitize(head)}`), ref: typedNodes.get(head) };
|
|
82
|
+
const ref = inputPortTypeRef(comp.inputPorts?.[head], plan);
|
|
83
|
+
return ref === undefined ? null : { expr: this.be.paramRef(head), ref };
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
/** compilePort — lower a port expression to its DECLARED port type (#173) — the ONE port lowering, asked
|
|
87
|
+
* by the coverage probe and used by emission. */
|
|
88
|
+
compilePort(node, comp, plan, typedNodes, isPrior, where, expected, opts) {
|
|
89
|
+
if (expected === undefined)
|
|
90
|
+
throw new GeneratorFailure("UNSUPPORTED_NODE_STRAIGHTLINE", `${this.be.surface}: ${where}: the declared port type is not statically resolvable (portSchemas type has no native lowering) — fail-closed`);
|
|
91
|
+
const resolveDecl = (name) => findDecl(plan, name).fields;
|
|
92
|
+
try {
|
|
93
|
+
const be = this.be.exprBackend(this.resolveHead(comp, plan, typedNodes, isPrior, opts), plan, opts.fresh);
|
|
94
|
+
return new NativeExprCompiler(be, opts.narrowed).compilePortTo(node, expected, resolveDecl);
|
|
95
|
+
}
|
|
96
|
+
catch (e) {
|
|
97
|
+
if (!(e instanceof GeneratorFailure))
|
|
98
|
+
throw e;
|
|
99
|
+
throw new GeneratorFailure("UNSUPPORTED_NODE_STRAIGHTLINE", `${this.be.surface}: ${where} ${JSON.stringify(node)} does not lower to a native value: ${e.message}`);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
/** one component's runner. */
|
|
104
|
+
class RunnerEmitter {
|
|
105
|
+
m;
|
|
106
|
+
be;
|
|
107
|
+
comp;
|
|
108
|
+
plan;
|
|
109
|
+
typedNodes;
|
|
110
|
+
metas;
|
|
111
|
+
idToPos = new Map();
|
|
112
|
+
tmp = 0;
|
|
113
|
+
constructor(m, comp, plan) {
|
|
114
|
+
this.m = m;
|
|
115
|
+
this.be = m.be;
|
|
116
|
+
this.comp = comp;
|
|
117
|
+
this.plan = plan;
|
|
118
|
+
this.typedNodes = buildTypedNodes(comp, plan);
|
|
119
|
+
const ep = execPlan(comp);
|
|
120
|
+
this.metas = analyzeSequentialOps(opsLiteralOf(comp), ep.order, comp);
|
|
121
|
+
ep.order.forEach((opIdx, k) => this.idToPos.set(comp.body[opIdx].id, k));
|
|
122
|
+
}
|
|
123
|
+
fresh = () => `x${++this.tmp}`;
|
|
124
|
+
v(name) {
|
|
125
|
+
return this.be.local(name);
|
|
126
|
+
}
|
|
127
|
+
t(id) {
|
|
128
|
+
return this.v(`t_${sanitize(id)}`);
|
|
129
|
+
}
|
|
130
|
+
produced(id) {
|
|
131
|
+
return this.v(`produced_${sanitize(id)}`);
|
|
132
|
+
}
|
|
133
|
+
priorAt(head, atPos) {
|
|
134
|
+
const p = this.idToPos.get(head);
|
|
135
|
+
return p !== undefined && p < atPos;
|
|
136
|
+
}
|
|
137
|
+
emit() {
|
|
138
|
+
const comp = this.comp;
|
|
139
|
+
const ep = execPlan(comp);
|
|
140
|
+
const body = [];
|
|
141
|
+
for (const i of ep.order) {
|
|
142
|
+
const n = comp.body[i];
|
|
143
|
+
if (!isControlGate(n))
|
|
144
|
+
body.push(this.be.assign(this.t(n.id), this.be.none));
|
|
145
|
+
body.push(this.be.assign(this.produced(n.id), this.be.bool(false)));
|
|
146
|
+
}
|
|
147
|
+
for (let k = 0; k < ep.order.length; k++) {
|
|
148
|
+
const i = ep.order[k];
|
|
149
|
+
const stage = ep.parallelStageOf.get(i);
|
|
150
|
+
if (stage !== undefined && stage[0] === i) {
|
|
151
|
+
body.push(...this.parallelStage(stage, k));
|
|
152
|
+
k += stage.length - 1;
|
|
153
|
+
continue;
|
|
154
|
+
}
|
|
155
|
+
body.push(...this.arm(i, k));
|
|
156
|
+
}
|
|
157
|
+
body.push(this.be.ret(this.output()));
|
|
158
|
+
const order = entryPortOrder(comp, this.m.ctx.declaredEntryPortOrder);
|
|
159
|
+
let firstOmittable = order.length;
|
|
160
|
+
while (firstOmittable > 0 && inputPortIsOptional(comp.inputPorts?.[order[firstOmittable - 1]]))
|
|
161
|
+
firstOmittable--;
|
|
162
|
+
const params = order.map((p, i) => this.be.paramDecl(p, i >= firstOmittable));
|
|
163
|
+
return this.be.renderFunction(comp, params, body);
|
|
164
|
+
}
|
|
165
|
+
/** one node at execution position `k` (sequential). */
|
|
166
|
+
arm(i, k) {
|
|
167
|
+
const n = this.comp.body[i];
|
|
168
|
+
// a node whose parent runs LATER is always skipped (plan.ts preflight: an unsettled parent is not ok).
|
|
169
|
+
if (this.metas[i].alwaysSkip)
|
|
170
|
+
return [{ comment: `op '${n.id}': its parent runs later — always unproduced` }];
|
|
171
|
+
if ("cond" in n)
|
|
172
|
+
return this.condArm(n, k);
|
|
173
|
+
if ("fanout" in n)
|
|
174
|
+
return this.fanoutArm(n, k, i);
|
|
175
|
+
if ("map" in n)
|
|
176
|
+
return this.mapArm(n, k, i);
|
|
177
|
+
return this.componentRefArm(n, k, i);
|
|
178
|
+
}
|
|
179
|
+
// ── gates ──
|
|
180
|
+
/** the parent gate (+ the bindField null-binding gate) around a node's statements. */
|
|
181
|
+
gated(node, k, inner) {
|
|
182
|
+
let out = inner;
|
|
183
|
+
if (node.parent === undefined || !this.priorAt(node.parent, k))
|
|
184
|
+
return out;
|
|
185
|
+
const parentNode = this.comp.body.find((b) => b.id === node.parent);
|
|
186
|
+
if (node.bindField !== undefined && parentNode !== undefined && !isControlGate(parentNode)) {
|
|
187
|
+
// #260: the gate exists only when the bound field can be null (an opt field) — a required field is
|
|
188
|
+
// always present on a produced parent, so it never skips.
|
|
189
|
+
const bf = typeRefAtPath(this.typedNodes.get(node.parent), [node.bindField], this.plan);
|
|
190
|
+
if (bf === null)
|
|
191
|
+
throw new GeneratorFailure("UNSUPPORTED_NODE_STRAIGHTLINE", `${this.be.surface}: node '${node.id}' bindField '${node.bindField}' is not a field of parent '${node.parent}'`);
|
|
192
|
+
if (bf.kind === "opt") {
|
|
193
|
+
const bound = this.v(`bound_${sanitize(node.id)}`);
|
|
194
|
+
out = [this.be.assign(bound, this.be.get(this.t(node.parent), node.bindField)), { if: this.be.isNotNone(bound), then: out }];
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
return [{ if: this.produced(node.parent), then: out }];
|
|
198
|
+
}
|
|
199
|
+
// ── ports ──
|
|
200
|
+
ports(ports, schemas, k, where, opts) {
|
|
201
|
+
const stmts = [];
|
|
202
|
+
const entries = [];
|
|
203
|
+
for (const pn of Object.keys(ports)) {
|
|
204
|
+
const c = this.m.compilePort(ports[pn], this.comp, this.plan, this.typedNodes, (h) => this.priorAt(h, k), `${where} port '${pn}'`, inputPortTypeRef(schemas?.[pn], this.plan), { ...opts, fresh: this.fresh });
|
|
205
|
+
stmts.push(...c.stmts);
|
|
206
|
+
entries.push({ key: pn, expr: c.expr });
|
|
207
|
+
}
|
|
208
|
+
return { stmts, entries };
|
|
209
|
+
}
|
|
210
|
+
// ── de-box ──
|
|
211
|
+
/** the de-box of a value `src` against `ref`, assigned to `target` (statements). */
|
|
212
|
+
deBox(ref, src, target, nodeId, passthrough) {
|
|
213
|
+
const p = buildDeBoxPlan(ref, { from: "top" }, { model: nodeId, field: nodeId, plan: this.plan }, passthrough);
|
|
214
|
+
const v = this.deBoxValue(p, src);
|
|
215
|
+
return [...v.stmts, this.be.assign(target, v.expr)];
|
|
216
|
+
}
|
|
217
|
+
/** the de-boxed value of `src` (a side-effect-free read) per the plan node: statements that build it plus
|
|
218
|
+
* the expression holding it. A scalar / opaque value is an expression; a record / list / map is built by
|
|
219
|
+
* statements into a fresh local. */
|
|
220
|
+
deBoxValue(p, src) {
|
|
221
|
+
const be = this.be;
|
|
222
|
+
if (p.optDepth === 0)
|
|
223
|
+
return this.deBoxBase(p, src);
|
|
224
|
+
// an optional position: null stays null (the optional layers collapse at the wire — present/absent).
|
|
225
|
+
const base = this.deBoxBase(p, src);
|
|
226
|
+
if (base.stmts.length === 0)
|
|
227
|
+
return { stmts: [], expr: be.ternary(be.isNone(src), be.none, base.expr) };
|
|
228
|
+
const t = this.v(`d${++this.tmp}`);
|
|
229
|
+
return { stmts: [{ if: be.isNone(src), then: [be.assign(t, be.none)], else: [...base.stmts, be.assign(t, base.expr)] }], expr: t };
|
|
230
|
+
}
|
|
231
|
+
deBoxBase(p, src) {
|
|
232
|
+
const be = this.be;
|
|
233
|
+
switch (p.k) {
|
|
234
|
+
case "value":
|
|
235
|
+
return { stmts: [], expr: src };
|
|
236
|
+
case "scalar":
|
|
237
|
+
return { stmts: [], expr: be.deScalar(p.scalar, src, p.model, p.field, p.expectedType) };
|
|
238
|
+
case "record": {
|
|
239
|
+
const rec = this.v(`d${++this.tmp}`);
|
|
240
|
+
const out = [{ if: be.not(be.isRecord(src)), then: [be.deMismatch(p.model, p.field, p.expectedType, src)] }, be.assign(rec, be.recordLit([]))];
|
|
241
|
+
for (const f of p.fields) {
|
|
242
|
+
const absent = f.plan.optDepth > 0 ? [be.setField(rec, f.name, be.none)] : [be.deMissing(f.plan.model, f.plan.field, f.plan.expectedType)];
|
|
243
|
+
// a required scalar / opaque field reads its value once — no local; anything else binds it first.
|
|
244
|
+
const direct = f.plan.optDepth === 0 && (f.plan.k === "scalar" || f.plan.k === "value");
|
|
245
|
+
let present;
|
|
246
|
+
if (direct)
|
|
247
|
+
present = [be.setField(rec, f.name, this.deBoxValue(f.plan, be.get(src, f.name)).expr)];
|
|
248
|
+
else {
|
|
249
|
+
const fv = this.v(`d${++this.tmp}`);
|
|
250
|
+
const v = this.deBoxValue(f.plan, fv);
|
|
251
|
+
present = [be.assign(fv, be.get(src, f.name)), ...v.stmts, be.setField(rec, f.name, v.expr)];
|
|
252
|
+
}
|
|
253
|
+
out.push({ if: be.has(src, f.name), then: present, else: absent });
|
|
254
|
+
}
|
|
255
|
+
return { stmts: out, expr: rec };
|
|
256
|
+
}
|
|
257
|
+
case "list": {
|
|
258
|
+
const lst = this.v(`d${++this.tmp}`);
|
|
259
|
+
const el = this.v(`d${++this.tmp}`);
|
|
260
|
+
const v = this.deBoxValue(p.elem, el);
|
|
261
|
+
return {
|
|
262
|
+
stmts: [
|
|
263
|
+
{ if: be.not(be.isList(src)), then: [be.deMismatch(p.model, p.field, p.expectedType, src)] },
|
|
264
|
+
be.assign(lst, be.listLit([])),
|
|
265
|
+
{ forEach: { v: el, over: src }, body: [...v.stmts, be.append(lst, v.expr)] },
|
|
266
|
+
],
|
|
267
|
+
expr: lst,
|
|
268
|
+
};
|
|
269
|
+
}
|
|
270
|
+
case "map": {
|
|
271
|
+
const mp = this.v(`d${++this.tmp}`);
|
|
272
|
+
const key = this.v(`d${++this.tmp}`);
|
|
273
|
+
const val = this.v(`d${++this.tmp}`);
|
|
274
|
+
const v = this.deBoxValue(p.value, val);
|
|
275
|
+
return {
|
|
276
|
+
stmts: [
|
|
277
|
+
{ if: be.not(be.isRecord(src)), then: [be.deMismatch(p.model, p.field, p.expectedType, src)] },
|
|
278
|
+
be.assign(mp, be.mapLit([])),
|
|
279
|
+
be.forEntries(key, val, src, [
|
|
280
|
+
// map keys are arbitrary leaf strings: "__proto__" fails closed exactly as on every other
|
|
281
|
+
// object-building path, in every language (#319).
|
|
282
|
+
{ if: be.eq(key, be.str("__proto__")), then: [be.deForbiddenKey(key)] },
|
|
283
|
+
...v.stmts,
|
|
284
|
+
be.setKey(mp, key, v.expr),
|
|
285
|
+
]),
|
|
286
|
+
],
|
|
287
|
+
expr: mp,
|
|
288
|
+
};
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
// ── componentRef ──
|
|
293
|
+
componentRefArm(node, k, i) {
|
|
294
|
+
const be = this.be;
|
|
295
|
+
const s = sanitize(node.id);
|
|
296
|
+
const policy = policyOf(this.metas[i], node.id);
|
|
297
|
+
const { stmts, entries } = this.ports(node.ports, node.portSchemas, k, `op '${node.id}'`, { narrowed: nodeNarrowedPaths(this.comp, node) });
|
|
298
|
+
const payload = this.v(`payload_${s}`);
|
|
299
|
+
const w = this.v(`w_${s}`);
|
|
300
|
+
const err = this.v(`err_${s}`);
|
|
301
|
+
const call = be.leafCall(this.m.useLeaf(node.component, node.leafSymbol), payload);
|
|
302
|
+
const debox = this.deBox(this.typedNodes.get(node.id), w, this.t(node.id), node.id, nodeWirePassthrough(node));
|
|
303
|
+
const inner = [...stmts, be.assign(payload, be.payload(entries))];
|
|
304
|
+
if (policy === "continue") {
|
|
305
|
+
// continue: a failed Component produces no Port (downstream skip). A de-box mismatch is NOT a
|
|
306
|
+
// Component failure — the interpreter checks the declared outType outside the policy interpretation.
|
|
307
|
+
const ok = this.v(`ok_${s}`);
|
|
308
|
+
inner.push(be.assign(ok, be.bool(false)), { try: [be.assign(w, call), be.assign(ok, be.bool(true))], err, catch: [] }, { if: ok, then: [...debox, be.assign(this.produced(node.id), be.bool(true))] });
|
|
309
|
+
}
|
|
310
|
+
else {
|
|
311
|
+
inner.push({ try: [be.assign(w, call)], err, catch: [be.raise(be.opFailed(node.id, policy, err), err)] });
|
|
312
|
+
inner.push(...debox, be.assign(this.produced(node.id), be.bool(true)));
|
|
313
|
+
}
|
|
314
|
+
return [{ comment: `op '${node.id}' (${node.component}${node.parent ? ", parent:" + node.parent : ""})` }, ...this.gated(node, k, inner)];
|
|
315
|
+
}
|
|
316
|
+
// ── cond ──
|
|
317
|
+
condArm(node, k) {
|
|
318
|
+
const be = this.be;
|
|
319
|
+
const c = node.cond;
|
|
320
|
+
const resolveHead = this.m.resolveHead(this.comp, this.plan, this.typedNodes, (h) => this.priorAt(h, k));
|
|
321
|
+
const compiler = new NativeExprCompiler(be.exprBackend(resolveHead, this.plan, this.fresh));
|
|
322
|
+
const cond = compiler.compileBool(c.if);
|
|
323
|
+
if (isControlGate(node)) {
|
|
324
|
+
const inner = [...cond.stmts, be.assign(this.produced(node.id), cond.expr)];
|
|
325
|
+
return [{ comment: `control-gate cond '${node.id}'` }, ...this.gated({ id: node.id, parent: c.parent }, k, inner)];
|
|
326
|
+
}
|
|
327
|
+
const nodeRef = this.typedNodes.get(node.id);
|
|
328
|
+
const resolveDecl = (name) => findDecl(this.plan, name).fields;
|
|
329
|
+
const thenC = compiler.compileTo(c.then, nodeRef, resolveDecl);
|
|
330
|
+
const elseC = compiler.compileTo(c.else, nodeRef, resolveDecl);
|
|
331
|
+
const inner = [
|
|
332
|
+
...cond.stmts,
|
|
333
|
+
{ if: cond.expr, then: [...thenC.stmts, be.assign(this.t(node.id), thenC.expr)], else: [...elseC.stmts, be.assign(this.t(node.id), elseC.expr)] },
|
|
334
|
+
be.assign(this.produced(node.id), be.bool(true)),
|
|
335
|
+
];
|
|
336
|
+
return [{ comment: `cond '${node.id}'` }, ...this.gated({ id: node.id, parent: c.parent }, k, inner)];
|
|
337
|
+
}
|
|
338
|
+
// ── over ──
|
|
339
|
+
/** the native read of a covered `over` collection (a null optional collection iterates EMPTY — #177 (A)). */
|
|
340
|
+
overRead(source, optArr) {
|
|
341
|
+
const be = this.be;
|
|
342
|
+
const raw = source.kind === "node"
|
|
343
|
+
? source.path.reduce((acc, f) => be.get(acc, f), this.t(source.head))
|
|
344
|
+
: be.paramRef(source.port);
|
|
345
|
+
return optArr ? be.ternary(be.isNone(raw), be.listLit([]), raw) : raw;
|
|
346
|
+
}
|
|
347
|
+
/** a component failure of a map/fanout node under its Error Policy Kind: `continue` leaves it unproduced. */
|
|
348
|
+
onFailure(nodeId, policy, err, skipped, inLoop) {
|
|
349
|
+
if (policy === "continue")
|
|
350
|
+
return inLoop ? [this.be.assign(skipped, this.be.bool(true)), this.be.brk] : [this.be.assign(skipped, this.be.bool(true))];
|
|
351
|
+
return [this.be.raise(this.be.opFailed(nodeId, policy, err), err)];
|
|
352
|
+
}
|
|
353
|
+
// ── map ──
|
|
354
|
+
mapArm(node, k, i) {
|
|
355
|
+
const be = this.be;
|
|
356
|
+
const m = node.map;
|
|
357
|
+
const s = sanitize(node.id);
|
|
358
|
+
const policy = policyOf(this.metas[i], node.id);
|
|
359
|
+
const arrRef = this.typedNodes.get(node.id);
|
|
360
|
+
const over = resolveOverOrFail(this.be.lang, this.comp, m.over, this.typedNodes, this.plan, node.id, "map");
|
|
361
|
+
const asBinding = { name: m.as, ref: over.elemRef, plan: this.plan };
|
|
362
|
+
const el = this.v(`e_${s}`);
|
|
363
|
+
const idx = this.v(`i_${s}`);
|
|
364
|
+
const overL = this.v(`over_${s}`);
|
|
365
|
+
const narrowed = nodeNarrowedPaths(this.comp, node);
|
|
366
|
+
const guard = () => {
|
|
367
|
+
if (m.when === undefined)
|
|
368
|
+
return null;
|
|
369
|
+
const resolveHead = this.m.resolveHead(this.comp, this.plan, this.typedNodes, (h) => this.priorAt(h, k), { asBinding, asBase: el });
|
|
370
|
+
const g = new NativeExprCompiler(be.exprBackend(resolveHead, this.plan, this.fresh)).compileBool(m.when);
|
|
371
|
+
return { stmts: g.stmts, expr: g.expr };
|
|
372
|
+
};
|
|
373
|
+
const keepGate = (onSkip) => {
|
|
374
|
+
const g = guard();
|
|
375
|
+
return g === null ? [] : [...g.stmts, { if: be.not(g.expr), then: onSkip }];
|
|
376
|
+
};
|
|
377
|
+
const inner = [be.assign(overL, this.overRead(over.source, over.optArr))];
|
|
378
|
+
const transform = mapTransformExpr(m);
|
|
379
|
+
if (transform !== undefined) {
|
|
380
|
+
// a pure-expression map (#222): no transport, no wire, no de-box — each element is materialized to the
|
|
381
|
+
// DECLARED element type through the same declared-type lowering a port uses.
|
|
382
|
+
const tc = this.m.compilePort(transform, this.comp, this.plan, this.typedNodes, (h) => this.priorAt(h, k), `map '${node.id}' transform`, arrRef.elem, { asBinding, asBase: el, narrowed, fresh: this.fresh });
|
|
383
|
+
const out = this.v(`col_${s}`);
|
|
384
|
+
inner.push(be.assign(out, be.listLit([])), { forEach: { v: el, over: overL }, body: [...keepGate([be.cont]), ...tc.stmts, be.append(out, tc.expr)] }, be.assign(this.t(node.id), out), be.assign(this.produced(node.id), be.bool(true)));
|
|
385
|
+
return [{ comment: `map '${node.id}' (pure expression)` }, ...this.gated({ id: node.id, parent: m.parent }, k, inner)];
|
|
386
|
+
}
|
|
387
|
+
const sym = this.m.useLeaf(m.component, m.leafSymbol);
|
|
388
|
+
const col = this.v(`col_${s}`);
|
|
389
|
+
const kept = this.v(`kept_${s}`);
|
|
390
|
+
const skipped = this.v(`skip_${s}`);
|
|
391
|
+
const err = this.v(`err_${s}`);
|
|
392
|
+
const r = this.v(`r_${s}`);
|
|
393
|
+
const hasInto = m.into !== undefined;
|
|
394
|
+
const portsOf = () => this.ports(m.ports ?? {}, m.portSchemas, k, `map '${node.id}'`, { asBinding, asBase: el, narrowed });
|
|
395
|
+
inner.push(be.assign(col, be.listLit([])), be.assign(kept, be.listLit([])));
|
|
396
|
+
if (policy === "continue")
|
|
397
|
+
inner.push(be.assign(skipped, be.bool(false)));
|
|
398
|
+
if (m.batched === true) {
|
|
399
|
+
const items = this.v(`items_${s}`);
|
|
400
|
+
const pe = portsOf();
|
|
401
|
+
inner.push(be.assign(items, be.listLit([])), {
|
|
402
|
+
forEnum: { i: idx, v: el, over: overL },
|
|
403
|
+
body: [...keepGate([be.cont]), ...pe.stmts, be.append(items, be.batchItem(pe.entries)), be.append(kept, idx)],
|
|
404
|
+
});
|
|
405
|
+
const onOk = [
|
|
406
|
+
{ if: be.not(be.and(be.isList(r), be.eq(be.len(r), be.len(items)))), then: [be.raise(be.batchMismatch("MAP_BATCH_RESULT_MISMATCH", `map '${node.id}': batched handler must return a list aligned to items`, be.len(items), r))] },
|
|
407
|
+
be.assign(col, r),
|
|
408
|
+
];
|
|
409
|
+
const ok = this.v(`ok_${s}`);
|
|
410
|
+
inner.push({
|
|
411
|
+
if: be.lt(be.intLit("0"), be.len(items)),
|
|
412
|
+
then: [
|
|
413
|
+
be.assign(ok, be.bool(false)),
|
|
414
|
+
{ try: [be.assign(r, be.leafCall(sym, be.batchPayload(items))), be.assign(ok, be.bool(true))], err, catch: this.onFailure(node.id, policy, err, skipped, false) },
|
|
415
|
+
{ if: ok, then: onOk },
|
|
416
|
+
],
|
|
417
|
+
});
|
|
418
|
+
}
|
|
419
|
+
else {
|
|
420
|
+
const pe = portsOf();
|
|
421
|
+
const elemFailure = m.elementPolicy === "skip" ? [be.cont] : this.onFailure(node.id, policy, err, skipped, true);
|
|
422
|
+
const ok = this.v(`ok_${s}`);
|
|
423
|
+
inner.push({
|
|
424
|
+
forEnum: { i: idx, v: el, over: overL },
|
|
425
|
+
body: [
|
|
426
|
+
...keepGate([be.cont]),
|
|
427
|
+
...pe.stmts,
|
|
428
|
+
be.assign(ok, be.bool(false)),
|
|
429
|
+
{ try: [be.assign(r, be.leafCall(sym, be.payload(pe.entries))), be.assign(ok, be.bool(true))], err, catch: elemFailure },
|
|
430
|
+
{ if: ok, then: [be.append(col, r), be.append(kept, idx)] },
|
|
431
|
+
],
|
|
432
|
+
});
|
|
433
|
+
}
|
|
434
|
+
// the raw result, assembled exactly as run_behavior assembles it, then de-boxed once (arr of element).
|
|
435
|
+
const raw = this.v(`raw_${s}`);
|
|
436
|
+
const assemble = [];
|
|
437
|
+
if (!hasInto)
|
|
438
|
+
assemble.push(be.assign(raw, col));
|
|
439
|
+
else {
|
|
440
|
+
// into: the over list, each kept element augmented with its aligned result; a guard/element-skipped
|
|
441
|
+
// element passes through unchanged (a connection-typed into gets the empty connection — #182).
|
|
442
|
+
const into = m.into;
|
|
443
|
+
const kk = this.v(`k_${s}`);
|
|
444
|
+
const iv = this.v(`iv_${s}`);
|
|
445
|
+
const aug = this.v(`aug_${s}`);
|
|
446
|
+
const skipBranch = m.relationKind === "connection"
|
|
447
|
+
? [be.assign(iv, be.recordLit([{ key: "items", expr: be.listLit([]) }, { key: "cursor", expr: be.none }]))]
|
|
448
|
+
: [be.append(raw, el), be.cont];
|
|
449
|
+
assemble.push(be.assign(raw, be.listLit([])), be.assign(kk, be.intLit("0")), {
|
|
450
|
+
forEnum: { i: idx, v: el, over: overL },
|
|
451
|
+
body: [
|
|
452
|
+
{
|
|
453
|
+
if: be.and(be.lt(kk, be.len(kept)), be.eq(be.index(kept, kk), idx)),
|
|
454
|
+
then: [be.assign(iv, be.index(col, kk)), be.assign(kk, `${kk} + 1`)],
|
|
455
|
+
else: skipBranch,
|
|
456
|
+
},
|
|
457
|
+
{
|
|
458
|
+
if: be.not(be.isRecord(el)),
|
|
459
|
+
then: [be.raise(be.error("MAP_INTO_ELEMENT_NOT_OBJECT", be.messageWithInt(`map '${node.id}': 'into' requires object elements (element `, idx, " is not an object)")))],
|
|
460
|
+
},
|
|
461
|
+
...be.copyWith(aug, el, into, iv),
|
|
462
|
+
be.append(raw, aug),
|
|
463
|
+
],
|
|
464
|
+
});
|
|
465
|
+
}
|
|
466
|
+
const tail = [...assemble, ...this.deBox(arrRef, raw, this.t(node.id), node.id, nodeWirePassthrough(node)), be.assign(this.produced(node.id), be.bool(true))];
|
|
467
|
+
if (policy === "continue")
|
|
468
|
+
inner.push({ if: be.not(skipped), then: tail });
|
|
469
|
+
else
|
|
470
|
+
inner.push(...tail);
|
|
471
|
+
return [{ comment: `map '${node.id}' (${m.component}, ${m.batched === true ? "batched" : "per-element"})` }, ...this.gated({ id: node.id, parent: m.parent }, k, inner)];
|
|
472
|
+
}
|
|
473
|
+
// ── fanout ──
|
|
474
|
+
fanoutArm(node, k, i) {
|
|
475
|
+
const be = this.be;
|
|
476
|
+
const f = node.fanout;
|
|
477
|
+
const s = sanitize(node.id);
|
|
478
|
+
const policy = policyOf(this.metas[i], node.id);
|
|
479
|
+
const connRef = this.typedNodes.get(node.id);
|
|
480
|
+
const elemRef = fanoutItemsElemRef(be.lang, node, connRef, this.plan);
|
|
481
|
+
// the dedupe key is a STRING field of the connection items element (the native dedupe identity).
|
|
482
|
+
const elemDecl = elemRef.kind === "named" ? findDecl(this.plan, elemRef.name) : undefined;
|
|
483
|
+
const dk = elemDecl?.fields.find((fd) => fd.name === f.dedupeKey);
|
|
484
|
+
if (elemRef.kind !== "named" || dk === undefined || dk.type.kind !== "scalar" || dk.type.scalar !== "string")
|
|
485
|
+
throw new GeneratorFailure("UNSUPPORTED_NODE_STRAIGHTLINE", `${be.surface} fanout (v3): node '${node.id}' dedupeKey '${f.dedupeKey}' must be a string field of the connection items element type.`);
|
|
486
|
+
const over = resolveOverOrFail(be.lang, this.comp, f.over, this.typedNodes, this.plan, node.id, "fanout");
|
|
487
|
+
const asBinding = { name: f.as, ref: over.elemRef, plan: this.plan };
|
|
488
|
+
const el = this.v(`e_${s}`);
|
|
489
|
+
const overL = this.v(`over_${s}`);
|
|
490
|
+
const ip = this.v(`ip_${s}`);
|
|
491
|
+
const r = this.v(`r_${s}`);
|
|
492
|
+
const err = this.v(`err_${s}`);
|
|
493
|
+
const ok = this.v(`ok_${s}`);
|
|
494
|
+
const skipped = this.v(`skip_${s}`);
|
|
495
|
+
const items = this.v(`items_${s}`);
|
|
496
|
+
const seen = this.v(`seen_${s}`);
|
|
497
|
+
const body = this.v(`b_${s}`);
|
|
498
|
+
const key = this.v(`kv_${s}`);
|
|
499
|
+
const raw = this.v(`raw_${s}`);
|
|
500
|
+
const pe = this.ports(f.ports, f.portSchemas, k, `fanout '${node.id}'`, { asBinding, asBase: el });
|
|
501
|
+
const sym = this.m.useLeaf(f.component, f.leafSymbol);
|
|
502
|
+
// THE ONE dedupe/drop (mirrors fanoutDedupDrop): a body with no dedupe key (null / not a record / absent
|
|
503
|
+
// key) is dangling — dropped under drop:"dangling", kept otherwise; first-seen dedupe by the key; the
|
|
504
|
+
// implicitSource field is stripped. A non-string key is not deduped (the de-box below rejects it).
|
|
505
|
+
const dedupe = [
|
|
506
|
+
be.assign(key, be.ternary(be.isRecord(body), be.getOrNone(body, f.dedupeKey), be.none)),
|
|
507
|
+
{ if: be.isNone(key), then: f.drop === "dangling" ? [be.cont] : [be.append(items, body), be.cont] },
|
|
508
|
+
{ if: be.isString(key), then: [{ if: be.setHas(seen, key), then: [be.cont] }, be.setAdd(seen, key)] },
|
|
509
|
+
];
|
|
510
|
+
if (f.implicitSource !== undefined)
|
|
511
|
+
dedupe.push({ if: be.has(body, f.implicitSource), then: be.copyWithout(body, body, f.implicitSource) });
|
|
512
|
+
dedupe.push(be.append(items, body));
|
|
513
|
+
const inner = [
|
|
514
|
+
be.assign(overL, this.overRead(over.source, over.optArr)),
|
|
515
|
+
be.assign(ip, be.listLit([])),
|
|
516
|
+
{ forEach: { v: el, over: overL }, body: [...pe.stmts, be.append(ip, be.batchItem(pe.entries))] },
|
|
517
|
+
be.assign(items, be.listLit([])),
|
|
518
|
+
];
|
|
519
|
+
if (policy === "continue")
|
|
520
|
+
inner.push(be.assign(skipped, be.bool(false)));
|
|
521
|
+
inner.push({
|
|
522
|
+
if: be.lt(be.intLit("0"), be.len(ip)),
|
|
523
|
+
then: [
|
|
524
|
+
be.assign(ok, be.bool(false)),
|
|
525
|
+
{ try: [be.assign(r, be.leafCall(sym, be.batchPayload(ip))), be.assign(ok, be.bool(true))], err, catch: this.onFailure(node.id, policy, err, skipped, false) },
|
|
526
|
+
{
|
|
527
|
+
if: ok,
|
|
528
|
+
then: [
|
|
529
|
+
{ if: be.not(be.and(be.isList(r), be.eq(be.len(r), be.len(ip)))), then: [be.raise(be.batchMismatch("FANOUT_BATCH_RESULT_MISMATCH", `fanout '${node.id}': batched handler must return a list aligned to the deduped id list`, be.len(ip), r))] },
|
|
530
|
+
be.assign(seen, be.emptySet),
|
|
531
|
+
{ forEach: { v: body, over: r }, body: dedupe },
|
|
532
|
+
],
|
|
533
|
+
},
|
|
534
|
+
],
|
|
535
|
+
});
|
|
536
|
+
const tail = [
|
|
537
|
+
be.assign(raw, be.recordLit([{ key: "items", expr: items }, { key: "cursor", expr: be.none }])),
|
|
538
|
+
...this.deBox(connRef, raw, this.t(node.id), node.id, nodeWirePassthrough(node)),
|
|
539
|
+
be.assign(this.produced(node.id), be.bool(true)),
|
|
540
|
+
];
|
|
541
|
+
if (policy === "continue")
|
|
542
|
+
inner.push({ if: be.not(skipped), then: tail });
|
|
543
|
+
else
|
|
544
|
+
inner.push(...tail);
|
|
545
|
+
return [{ comment: `fanout '${node.id}' (${f.component})` }, ...this.gated({ id: node.id, parent: f.parent }, k, inner)];
|
|
546
|
+
}
|
|
547
|
+
// ── a real-concurrency stage ──
|
|
548
|
+
parallelStage(stage, k0) {
|
|
549
|
+
const be = this.be;
|
|
550
|
+
const noDispatchMember = (i) => {
|
|
551
|
+
const n = this.comp.body[i];
|
|
552
|
+
return "cond" in n || ("map" in n && mapTransformExpr(n.map) !== undefined);
|
|
553
|
+
};
|
|
554
|
+
// no worker pool when the language has none (php) or when no member dispatches (cond / pure-transform
|
|
555
|
+
// members only — evaluated in ascending order, exactly as the commit would).
|
|
556
|
+
if (!be.threads || stage.every(noDispatchMember))
|
|
557
|
+
return stage.flatMap((i, j) => this.arm(i, k0 + j));
|
|
558
|
+
this.m.uses.threads = true;
|
|
559
|
+
// python: the run_plan parallel protocol — preflight (ascending, from settled prior stages) decides who
|
|
560
|
+
// runs; the runnable componentRef members are dispatched on a bounded pool (ports are evaluated INSIDE
|
|
561
|
+
// the worker, as exec_node does); the commit walks the stage in ascending index order, so the failure
|
|
562
|
+
// precedence is the interpreter's (a worker's exception surfaces at its member's commit). A cond /
|
|
563
|
+
// pure-transform member has no dispatch and is evaluated at its commit position.
|
|
564
|
+
const noDispatch = noDispatchMember;
|
|
565
|
+
const dispatched = stage.filter((i) => !noDispatch(i));
|
|
566
|
+
const out = [{ comment: `real-concurrency stage {${stage.map((i) => this.comp.body[i].id).join(", ")}} (bound=${execPlan(this.comp).concurrency})` }];
|
|
567
|
+
const pool = this.v(`pool_${k0}`);
|
|
568
|
+
const workers = Math.max(1, Math.min(execPlan(this.comp).concurrency, dispatched.length));
|
|
569
|
+
const submits = [];
|
|
570
|
+
for (const i of dispatched) {
|
|
571
|
+
const node = this.comp.body[i];
|
|
572
|
+
if (this.metas[i].alwaysSkip)
|
|
573
|
+
continue;
|
|
574
|
+
const k = k0 + stage.indexOf(i);
|
|
575
|
+
const s = sanitize(node.id);
|
|
576
|
+
const run = this.v(`run_${s}`);
|
|
577
|
+
out.push(be.assign(run, be.bool(false)), ...this.gated(node, k, [be.assign(run, be.bool(true))]));
|
|
578
|
+
const { stmts, entries } = this.ports(node.ports, node.portSchemas, k, `op '${node.id}'`, { narrowed: nodeNarrowedPaths(this.comp, node) });
|
|
579
|
+
const err = this.v(`err_${s}`);
|
|
580
|
+
const payload = this.v(`payload_${s}`);
|
|
581
|
+
out.push({
|
|
582
|
+
def: `work_${s}`,
|
|
583
|
+
body: [
|
|
584
|
+
...stmts,
|
|
585
|
+
be.assign(payload, be.payload(entries)),
|
|
586
|
+
{ try: [be.ret(be.pair(be.bool(true), be.leafCall(this.m.useLeaf(node.component, node.leafSymbol), payload)))], err, catch: [be.ret(be.pair(be.bool(false), err))] },
|
|
587
|
+
],
|
|
588
|
+
});
|
|
589
|
+
submits.push(be.assign(this.v(`f_${s}`), be.ternary(run, be.submit(pool, `work_${s}`), be.none)));
|
|
590
|
+
}
|
|
591
|
+
const commits = [];
|
|
592
|
+
for (let j = 0; j < stage.length; j++) {
|
|
593
|
+
const i = stage[j];
|
|
594
|
+
if (noDispatch(i) || this.metas[i].alwaysSkip) {
|
|
595
|
+
commits.push(...this.arm(i, k0 + j));
|
|
596
|
+
continue;
|
|
597
|
+
}
|
|
598
|
+
const node = this.comp.body[i];
|
|
599
|
+
const s = sanitize(node.id);
|
|
600
|
+
const policy = policyOf(this.metas[i], node.id);
|
|
601
|
+
const ok = this.v(`ok_${s}`);
|
|
602
|
+
const w = this.v(`w_${s}`);
|
|
603
|
+
const produce = [...this.deBox(this.typedNodes.get(node.id), w, this.t(node.id), node.id, nodeWirePassthrough(node)), be.assign(this.produced(node.id), be.bool(true))];
|
|
604
|
+
commits.push({
|
|
605
|
+
if: this.v(`run_${s}`),
|
|
606
|
+
then: [
|
|
607
|
+
be.unpack(ok, w, be.result(this.v(`f_${s}`))),
|
|
608
|
+
policy === "continue" ? { if: ok, then: produce } : { if: be.not(ok), then: [be.raise(be.opFailed(node.id, policy, w), w)] },
|
|
609
|
+
...(policy === "continue" ? [] : produce),
|
|
610
|
+
],
|
|
611
|
+
});
|
|
612
|
+
}
|
|
613
|
+
out.push({ withPool: { pool, workers }, body: [...submits, ...commits] });
|
|
614
|
+
return out;
|
|
615
|
+
}
|
|
616
|
+
// ── output ──
|
|
617
|
+
output() {
|
|
618
|
+
const comp = this.comp;
|
|
619
|
+
const outRef = comp.outputType !== undefined ? deriveTypeRef(comp.outputType, this.plan) : undefined;
|
|
620
|
+
const ep = execPlan(comp);
|
|
621
|
+
const skippable = new Map();
|
|
622
|
+
for (const i of ep.order) {
|
|
623
|
+
if (!this.metas[i].canSkip)
|
|
624
|
+
continue;
|
|
625
|
+
const n = comp.body[i];
|
|
626
|
+
skippable.set(n.id, "map" in n ? n.map.relationKind : "fanout" in n ? "connection" : n.relationKind);
|
|
627
|
+
}
|
|
628
|
+
const spell = {
|
|
629
|
+
producedFlag: (head) => this.produced(head),
|
|
630
|
+
optSome: (valueExpr) => valueExpr,
|
|
631
|
+
zero: (ref) => this.be.zero(ref, this.plan),
|
|
632
|
+
conditional: (cond, present, absent) => this.be.ternary(cond, present, absent),
|
|
633
|
+
};
|
|
634
|
+
const out = this.outValue(comp.output, outRef, "<output>", skippable, spell);
|
|
635
|
+
this.m.check(comp.name, "<output>", outRef, out.ref);
|
|
636
|
+
return out.expr;
|
|
637
|
+
}
|
|
638
|
+
/** the ONE output recursion (the go/rust `emitTypedValue` shape): a node-result reference (produced-aware
|
|
639
|
+
* when the node can skip), a declared obj / arr assembly, a string concat, or a scalar literal. */
|
|
640
|
+
outValue(node, expected, fieldPath, skippable, spell) {
|
|
641
|
+
const be = this.be;
|
|
642
|
+
const e = classifyExpr(node);
|
|
643
|
+
if (e.kind === "ref" && this.typedNodes.has(e.path[0])) {
|
|
644
|
+
const head = e.path[0];
|
|
645
|
+
const value = typeRefAtPath(this.typedNodes.get(head), e.path.slice(1), this.plan);
|
|
646
|
+
if (value === null)
|
|
647
|
+
throw new GeneratorFailure("UNSUPPORTED_NODE_STRAIGHTLINE", `${be.surface} output: '${JSON.stringify(node)}' does not resolve to a field of node '${head}'`);
|
|
648
|
+
const valueExpr = e.path.slice(1).reduce((acc, f) => be.get(acc, f), this.t(head));
|
|
649
|
+
if (!skippable.has(head))
|
|
650
|
+
return { expr: valueExpr, ref: value };
|
|
651
|
+
return lowerProducedAwareRead({
|
|
652
|
+
path: e.path,
|
|
653
|
+
relationKind: skippable.get(head),
|
|
654
|
+
optional: e.opt,
|
|
655
|
+
valueExpr,
|
|
656
|
+
value,
|
|
657
|
+
declared: expected,
|
|
658
|
+
compName: this.comp.name,
|
|
659
|
+
fieldPath,
|
|
660
|
+
check: this.m.check,
|
|
661
|
+
spell,
|
|
662
|
+
plan: this.plan,
|
|
663
|
+
});
|
|
664
|
+
}
|
|
665
|
+
if (e.kind === "ref")
|
|
666
|
+
throw new GeneratorFailure("UNSUPPORTED_NODE_STRAIGHTLINE", `${be.surface} output: a ref to a non-node binding '${e.path[0]}' is not natively lowerable`);
|
|
667
|
+
if (node !== null && typeof node === "object" && !Array.isArray(node)) {
|
|
668
|
+
const keys = Object.keys(node);
|
|
669
|
+
if (keys.length === 1 && keys[0] === "obj" && expected?.kind === "named") {
|
|
670
|
+
const decl = findDecl(this.plan, expected.name);
|
|
671
|
+
const fields = node.obj;
|
|
672
|
+
const entries = decl.fields.map((f) => {
|
|
673
|
+
const childPath = fieldPath === "" ? f.name : `${fieldPath}.${f.name}`;
|
|
674
|
+
const sub = this.outValue(fields[f.name], f.type, childPath, skippable, spell);
|
|
675
|
+
this.m.check(this.comp.name, childPath, f.type, sub.ref);
|
|
676
|
+
return { key: f.name, expr: sub.expr };
|
|
677
|
+
});
|
|
678
|
+
return { expr: be.recordLit(entries), ref: expected };
|
|
679
|
+
}
|
|
680
|
+
if (keys.length === 1 && keys[0] === "arr" && expected?.kind === "arr") {
|
|
681
|
+
const items = node.arr.map((el, idx) => {
|
|
682
|
+
const elemPath = `${fieldPath}[${idx}]`;
|
|
683
|
+
const sub = this.outValue(el, expected.elem, elemPath, skippable, spell);
|
|
684
|
+
this.m.check(this.comp.name, elemPath, expected.elem, sub.ref);
|
|
685
|
+
return sub.expr;
|
|
686
|
+
});
|
|
687
|
+
return { expr: be.listLit(items), ref: expected };
|
|
688
|
+
}
|
|
689
|
+
if (keys.length === 1 && keys[0] === "concat" && expected?.kind === "scalar" && expected.scalar === "string") {
|
|
690
|
+
const parts = node.concat.map((p, idx) => this.outValue(p, { kind: "scalar", scalar: "string" }, `${fieldPath}(concat operand ${idx})`, skippable, spell).expr);
|
|
691
|
+
return { expr: be.concatStr(parts), ref: expected };
|
|
692
|
+
}
|
|
693
|
+
if (keys.length === 1)
|
|
694
|
+
throw new GeneratorFailure("UNSUPPORTED_NODE_STRAIGHTLINE", `${be.surface} output: operator '${keys[0]}' at the output position is not supported (the output lowers ref / obj / arr / concat).`);
|
|
695
|
+
}
|
|
696
|
+
if (expected?.kind === "scalar")
|
|
697
|
+
return { expr: this.scalarLit(node, expected.scalar), ref: expected };
|
|
698
|
+
if (node === null)
|
|
699
|
+
return { expr: be.none, ref: undefined };
|
|
700
|
+
if (typeof node === "boolean")
|
|
701
|
+
return { expr: be.bool(node), ref: undefined };
|
|
702
|
+
if (typeof node === "string")
|
|
703
|
+
return { expr: be.str(node), ref: undefined };
|
|
704
|
+
return { expr: be.none, ref: undefined };
|
|
705
|
+
}
|
|
706
|
+
scalarLit(node, scalar) {
|
|
707
|
+
const be = this.be;
|
|
708
|
+
if (scalar === "string" && typeof node === "string")
|
|
709
|
+
return be.str(node);
|
|
710
|
+
if (scalar === "bool" && typeof node === "boolean")
|
|
711
|
+
return be.bool(node);
|
|
712
|
+
if (scalar === "int") {
|
|
713
|
+
if (node !== null && typeof node === "object" && typeof node.int === "string")
|
|
714
|
+
return be.intLit(node.int);
|
|
715
|
+
if (typeof node === "number")
|
|
716
|
+
return be.intLit(String(node));
|
|
717
|
+
}
|
|
718
|
+
if (scalar === "float") {
|
|
719
|
+
if (node !== null && typeof node === "object" && typeof node.float === "number")
|
|
720
|
+
return be.floatLit(node.float);
|
|
721
|
+
if (typeof node === "number")
|
|
722
|
+
return be.floatLit(node);
|
|
723
|
+
}
|
|
724
|
+
if (scalar === "null")
|
|
725
|
+
return be.none;
|
|
726
|
+
throw new GeneratorFailure("UNSUPPORTED_NODE_STRAIGHTLINE", `${be.surface} output: scalar literal ${JSON.stringify(node)} does not match the declared ${scalar}`);
|
|
727
|
+
}
|
|
728
|
+
}
|
|
729
|
+
/** emitDynamicNative — the module of a dynamically typed native target. */
|
|
730
|
+
export function emitDynamicNative(ctx, be) {
|
|
731
|
+
return new DynModuleEmitter(be, ctx).emit();
|
|
732
|
+
}
|
|
733
|
+
//# sourceMappingURL=emit-native-dynamic.js.map
|